]>
Commit | Line | Data |
---|---|---|
34dc7c2f BB |
1 | /* |
2 | * CDDL HEADER START | |
3 | * | |
4 | * The contents of this file are subject to the terms of the | |
5 | * Common Development and Distribution License (the "License"). | |
6 | * You may not use this file except in compliance with the License. | |
7 | * | |
8 | * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE | |
9 | * or http://www.opensolaris.org/os/licensing. | |
10 | * See the License for the specific language governing permissions | |
11 | * and limitations under the License. | |
12 | * | |
13 | * When distributing Covered Code, include this CDDL HEADER in each | |
14 | * file and include the License file at usr/src/OPENSOLARIS.LICENSE. | |
15 | * If applicable, add the following below this CDDL HEADER, with the | |
16 | * fields enclosed by brackets "[]" replaced with your own identifying | |
17 | * information: Portions Copyright [yyyy] [name of copyright owner] | |
18 | * | |
19 | * CDDL HEADER END | |
20 | */ | |
9b7b9cd3 | 21 | |
34dc7c2f | 22 | /* |
428870ff | 23 | * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. |
64fc7762 | 24 | * Copyright (c) 2012, 2017 by Delphix. All rights reserved. |
3a17a7a9 | 25 | * Copyright (c) 2013 by Saso Kiselkov. All rights reserved. |
788eb90c | 26 | * Copyright (c) 2013, Joyent, Inc. All rights reserved. |
0c66c32d | 27 | * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. |
9c43027b | 28 | * Copyright (c) 2015, STRATO AG, Inc. All rights reserved. |
a0bd735a | 29 | * Copyright (c) 2016 Actifio, Inc. All rights reserved. |
9b7b9cd3 | 30 | * Copyright 2017 Nexenta Systems, Inc. |
c0daec32 | 31 | * Copyright (c) 2017 Open-E, Inc. All Rights Reserved. |
34dc7c2f BB |
32 | */ |
33 | ||
428870ff BB |
34 | /* Portions Copyright 2010 Robert Milkowski */ |
35 | ||
1de321e6 | 36 | #include <sys/zfeature.h> |
34dc7c2f BB |
37 | #include <sys/cred.h> |
38 | #include <sys/zfs_context.h> | |
39 | #include <sys/dmu_objset.h> | |
40 | #include <sys/dsl_dir.h> | |
41 | #include <sys/dsl_dataset.h> | |
42 | #include <sys/dsl_prop.h> | |
43 | #include <sys/dsl_pool.h> | |
44 | #include <sys/dsl_synctask.h> | |
45 | #include <sys/dsl_deleg.h> | |
46 | #include <sys/dnode.h> | |
47 | #include <sys/dbuf.h> | |
48 | #include <sys/zvol.h> | |
49 | #include <sys/dmu_tx.h> | |
34dc7c2f BB |
50 | #include <sys/zap.h> |
51 | #include <sys/zil.h> | |
52 | #include <sys/dmu_impl.h> | |
53 | #include <sys/zfs_ioctl.h> | |
428870ff | 54 | #include <sys/sa.h> |
572e2857 | 55 | #include <sys/zfs_onexit.h> |
13fe0198 | 56 | #include <sys/dsl_destroy.h> |
9c43027b | 57 | #include <sys/vdev.h> |
f74b821a | 58 | #include <sys/policy.h> |
1de321e6 | 59 | #include <sys/spa_impl.h> |
b5256303 | 60 | #include <sys/dmu_send.h> |
572e2857 BB |
61 | |
62 | /* | |
63 | * Needed to close a window in dnode_move() that allows the objset to be freed | |
64 | * before it can be safely accessed. | |
65 | */ | |
66 | krwlock_t os_lock; | |
67 | ||
9c43027b | 68 | /* |
4e33ba4c | 69 | * Tunable to overwrite the maximum number of threads for the parallelization |
9c43027b AJ |
70 | * of dmu_objset_find_dp, needed to speed up the import of pools with many |
71 | * datasets. | |
72 | * Default is 4 times the number of leaf vdevs. | |
73 | */ | |
74 | int dmu_find_threads = 0; | |
75 | ||
68cbd56e NB |
76 | /* |
77 | * Backfill lower metadnode objects after this many have been freed. | |
78 | * Backfilling negatively impacts object creation rates, so only do it | |
79 | * if there are enough holes to fill. | |
80 | */ | |
81 | int dmu_rescan_dnode_threshold = 1 << DN_MAX_INDBLKSHIFT; | |
82 | ||
c0daec32 AB |
83 | static char *upgrade_tag = "upgrade_tag"; |
84 | ||
9c43027b AJ |
85 | static void dmu_objset_find_dp_cb(void *arg); |
86 | ||
1de321e6 JX |
87 | static void dmu_objset_upgrade(objset_t *os, dmu_objset_upgrade_cb_t cb); |
88 | static void dmu_objset_upgrade_stop(objset_t *os); | |
89 | ||
572e2857 BB |
90 | void |
91 | dmu_objset_init(void) | |
92 | { | |
93 | rw_init(&os_lock, NULL, RW_DEFAULT, NULL); | |
94 | } | |
95 | ||
96 | void | |
97 | dmu_objset_fini(void) | |
98 | { | |
99 | rw_destroy(&os_lock); | |
100 | } | |
34dc7c2f BB |
101 | |
102 | spa_t * | |
103 | dmu_objset_spa(objset_t *os) | |
104 | { | |
428870ff | 105 | return (os->os_spa); |
34dc7c2f BB |
106 | } |
107 | ||
108 | zilog_t * | |
109 | dmu_objset_zil(objset_t *os) | |
110 | { | |
428870ff | 111 | return (os->os_zil); |
34dc7c2f BB |
112 | } |
113 | ||
114 | dsl_pool_t * | |
115 | dmu_objset_pool(objset_t *os) | |
116 | { | |
117 | dsl_dataset_t *ds; | |
118 | ||
428870ff | 119 | if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir) |
34dc7c2f BB |
120 | return (ds->ds_dir->dd_pool); |
121 | else | |
428870ff | 122 | return (spa_get_dsl(os->os_spa)); |
34dc7c2f BB |
123 | } |
124 | ||
125 | dsl_dataset_t * | |
126 | dmu_objset_ds(objset_t *os) | |
127 | { | |
428870ff | 128 | return (os->os_dsl_dataset); |
34dc7c2f BB |
129 | } |
130 | ||
131 | dmu_objset_type_t | |
132 | dmu_objset_type(objset_t *os) | |
133 | { | |
428870ff | 134 | return (os->os_phys->os_type); |
34dc7c2f BB |
135 | } |
136 | ||
137 | void | |
138 | dmu_objset_name(objset_t *os, char *buf) | |
139 | { | |
428870ff | 140 | dsl_dataset_name(os->os_dsl_dataset, buf); |
34dc7c2f BB |
141 | } |
142 | ||
143 | uint64_t | |
144 | dmu_objset_id(objset_t *os) | |
145 | { | |
428870ff | 146 | dsl_dataset_t *ds = os->os_dsl_dataset; |
34dc7c2f BB |
147 | |
148 | return (ds ? ds->ds_object : 0); | |
149 | } | |
150 | ||
50c957f7 NB |
151 | uint64_t |
152 | dmu_objset_dnodesize(objset_t *os) | |
153 | { | |
154 | return (os->os_dnodesize); | |
155 | } | |
156 | ||
faf0f58c | 157 | zfs_sync_type_t |
428870ff BB |
158 | dmu_objset_syncprop(objset_t *os) |
159 | { | |
160 | return (os->os_sync); | |
161 | } | |
162 | ||
faf0f58c | 163 | zfs_logbias_op_t |
428870ff BB |
164 | dmu_objset_logbias(objset_t *os) |
165 | { | |
166 | return (os->os_logbias); | |
167 | } | |
168 | ||
34dc7c2f BB |
169 | static void |
170 | checksum_changed_cb(void *arg, uint64_t newval) | |
171 | { | |
428870ff | 172 | objset_t *os = arg; |
34dc7c2f BB |
173 | |
174 | /* | |
175 | * Inheritance should have been done by now. | |
176 | */ | |
177 | ASSERT(newval != ZIO_CHECKSUM_INHERIT); | |
178 | ||
428870ff | 179 | os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE); |
34dc7c2f BB |
180 | } |
181 | ||
182 | static void | |
183 | compression_changed_cb(void *arg, uint64_t newval) | |
184 | { | |
428870ff | 185 | objset_t *os = arg; |
34dc7c2f BB |
186 | |
187 | /* | |
188 | * Inheritance and range checking should have been done by now. | |
189 | */ | |
190 | ASSERT(newval != ZIO_COMPRESS_INHERIT); | |
191 | ||
99197f03 JG |
192 | os->os_compress = zio_compress_select(os->os_spa, newval, |
193 | ZIO_COMPRESS_ON); | |
34dc7c2f BB |
194 | } |
195 | ||
196 | static void | |
197 | copies_changed_cb(void *arg, uint64_t newval) | |
198 | { | |
428870ff | 199 | objset_t *os = arg; |
34dc7c2f BB |
200 | |
201 | /* | |
202 | * Inheritance and range checking should have been done by now. | |
203 | */ | |
204 | ASSERT(newval > 0); | |
428870ff | 205 | ASSERT(newval <= spa_max_replication(os->os_spa)); |
34dc7c2f | 206 | |
428870ff BB |
207 | os->os_copies = newval; |
208 | } | |
209 | ||
210 | static void | |
211 | dedup_changed_cb(void *arg, uint64_t newval) | |
212 | { | |
213 | objset_t *os = arg; | |
214 | spa_t *spa = os->os_spa; | |
215 | enum zio_checksum checksum; | |
216 | ||
217 | /* | |
218 | * Inheritance should have been done by now. | |
219 | */ | |
220 | ASSERT(newval != ZIO_CHECKSUM_INHERIT); | |
221 | ||
222 | checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF); | |
223 | ||
224 | os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK; | |
225 | os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY); | |
34dc7c2f BB |
226 | } |
227 | ||
b128c09f BB |
228 | static void |
229 | primary_cache_changed_cb(void *arg, uint64_t newval) | |
230 | { | |
428870ff | 231 | objset_t *os = arg; |
b128c09f BB |
232 | |
233 | /* | |
234 | * Inheritance and range checking should have been done by now. | |
235 | */ | |
236 | ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE || | |
237 | newval == ZFS_CACHE_METADATA); | |
238 | ||
428870ff | 239 | os->os_primary_cache = newval; |
b128c09f BB |
240 | } |
241 | ||
242 | static void | |
243 | secondary_cache_changed_cb(void *arg, uint64_t newval) | |
244 | { | |
428870ff | 245 | objset_t *os = arg; |
b128c09f BB |
246 | |
247 | /* | |
248 | * Inheritance and range checking should have been done by now. | |
249 | */ | |
250 | ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE || | |
251 | newval == ZFS_CACHE_METADATA); | |
252 | ||
428870ff BB |
253 | os->os_secondary_cache = newval; |
254 | } | |
255 | ||
256 | static void | |
257 | sync_changed_cb(void *arg, uint64_t newval) | |
258 | { | |
259 | objset_t *os = arg; | |
260 | ||
261 | /* | |
262 | * Inheritance and range checking should have been done by now. | |
263 | */ | |
264 | ASSERT(newval == ZFS_SYNC_STANDARD || newval == ZFS_SYNC_ALWAYS || | |
265 | newval == ZFS_SYNC_DISABLED); | |
266 | ||
267 | os->os_sync = newval; | |
268 | if (os->os_zil) | |
269 | zil_set_sync(os->os_zil, newval); | |
270 | } | |
271 | ||
faf0f58c MA |
272 | static void |
273 | redundant_metadata_changed_cb(void *arg, uint64_t newval) | |
274 | { | |
275 | objset_t *os = arg; | |
276 | ||
277 | /* | |
278 | * Inheritance and range checking should have been done by now. | |
279 | */ | |
280 | ASSERT(newval == ZFS_REDUNDANT_METADATA_ALL || | |
281 | newval == ZFS_REDUNDANT_METADATA_MOST); | |
282 | ||
283 | os->os_redundant_metadata = newval; | |
284 | } | |
285 | ||
50c957f7 NB |
286 | static void |
287 | dnodesize_changed_cb(void *arg, uint64_t newval) | |
288 | { | |
289 | objset_t *os = arg; | |
290 | ||
291 | switch (newval) { | |
292 | case ZFS_DNSIZE_LEGACY: | |
293 | os->os_dnodesize = DNODE_MIN_SIZE; | |
294 | break; | |
295 | case ZFS_DNSIZE_AUTO: | |
296 | /* | |
297 | * Choose a dnode size that will work well for most | |
298 | * workloads if the user specified "auto". Future code | |
299 | * improvements could dynamically select a dnode size | |
300 | * based on observed workload patterns. | |
301 | */ | |
302 | os->os_dnodesize = DNODE_MIN_SIZE * 2; | |
303 | break; | |
304 | case ZFS_DNSIZE_1K: | |
305 | case ZFS_DNSIZE_2K: | |
306 | case ZFS_DNSIZE_4K: | |
307 | case ZFS_DNSIZE_8K: | |
308 | case ZFS_DNSIZE_16K: | |
309 | os->os_dnodesize = newval; | |
310 | break; | |
311 | } | |
312 | } | |
313 | ||
428870ff BB |
314 | static void |
315 | logbias_changed_cb(void *arg, uint64_t newval) | |
316 | { | |
317 | objset_t *os = arg; | |
318 | ||
319 | ASSERT(newval == ZFS_LOGBIAS_LATENCY || | |
320 | newval == ZFS_LOGBIAS_THROUGHPUT); | |
321 | os->os_logbias = newval; | |
322 | if (os->os_zil) | |
323 | zil_set_logbias(os->os_zil, newval); | |
b128c09f BB |
324 | } |
325 | ||
f1512ee6 MA |
326 | static void |
327 | recordsize_changed_cb(void *arg, uint64_t newval) | |
328 | { | |
329 | objset_t *os = arg; | |
330 | ||
331 | os->os_recordsize = newval; | |
332 | } | |
333 | ||
34dc7c2f BB |
334 | void |
335 | dmu_objset_byteswap(void *buf, size_t size) | |
336 | { | |
337 | objset_phys_t *osp = buf; | |
338 | ||
9babb374 | 339 | ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t)); |
34dc7c2f BB |
340 | dnode_byteswap(&osp->os_meta_dnode); |
341 | byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t)); | |
342 | osp->os_type = BSWAP_64(osp->os_type); | |
9babb374 BB |
343 | osp->os_flags = BSWAP_64(osp->os_flags); |
344 | if (size == sizeof (objset_phys_t)) { | |
345 | dnode_byteswap(&osp->os_userused_dnode); | |
346 | dnode_byteswap(&osp->os_groupused_dnode); | |
347 | } | |
34dc7c2f BB |
348 | } |
349 | ||
64fc7762 MA |
350 | /* |
351 | * The hash is a CRC-based hash of the objset_t pointer and the object number. | |
352 | */ | |
353 | static uint64_t | |
354 | dnode_hash(const objset_t *os, uint64_t obj) | |
355 | { | |
356 | uintptr_t osv = (uintptr_t)os; | |
357 | uint64_t crc = -1ULL; | |
358 | ||
359 | ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY); | |
360 | /* | |
361 | * The low 6 bits of the pointer don't have much entropy, because | |
362 | * the objset_t is larger than 2^6 bytes long. | |
363 | */ | |
364 | crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (osv >> 6)) & 0xFF]; | |
365 | crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 0)) & 0xFF]; | |
366 | crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 8)) & 0xFF]; | |
367 | crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 16)) & 0xFF]; | |
368 | ||
369 | crc ^= (osv>>14) ^ (obj>>24); | |
370 | ||
371 | return (crc); | |
372 | } | |
373 | ||
374 | unsigned int | |
375 | dnode_multilist_index_func(multilist_t *ml, void *obj) | |
376 | { | |
377 | dnode_t *dn = obj; | |
378 | return (dnode_hash(dn->dn_objset, dn->dn_object) % | |
379 | multilist_get_num_sublists(ml)); | |
380 | } | |
381 | ||
34dc7c2f BB |
382 | int |
383 | dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, | |
428870ff | 384 | objset_t **osp) |
34dc7c2f | 385 | { |
428870ff | 386 | objset_t *os; |
b128c09f | 387 | int i, err; |
34dc7c2f BB |
388 | |
389 | ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock)); | |
390 | ||
79c76d5b | 391 | os = kmem_zalloc(sizeof (objset_t), KM_SLEEP); |
428870ff BB |
392 | os->os_dsl_dataset = ds; |
393 | os->os_spa = spa; | |
394 | os->os_rootbp = bp; | |
395 | if (!BP_IS_HOLE(os->os_rootbp)) { | |
2a432414 | 396 | arc_flags_t aflags = ARC_FLAG_WAIT; |
5dbd68a3 | 397 | zbookmark_phys_t zb; |
b5256303 | 398 | enum zio_flag zio_flags = ZIO_FLAG_CANFAIL; |
428870ff BB |
399 | SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET, |
400 | ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); | |
401 | ||
402 | if (DMU_OS_IS_L2CACHEABLE(os)) | |
2a432414 | 403 | aflags |= ARC_FLAG_L2CACHE; |
34dc7c2f | 404 | |
b5256303 TC |
405 | if (ds != NULL && ds->ds_dir->dd_crypto_obj != 0) { |
406 | ASSERT3U(BP_GET_COMPRESS(bp), ==, ZIO_COMPRESS_OFF); | |
407 | ASSERT(BP_IS_AUTHENTICATED(bp)); | |
408 | zio_flags |= ZIO_FLAG_RAW; | |
409 | } | |
410 | ||
428870ff | 411 | dprintf_bp(os->os_rootbp, "reading %s", ""); |
294f6806 | 412 | err = arc_read(NULL, spa, os->os_rootbp, |
428870ff | 413 | arc_getbuf_func, &os->os_phys_buf, |
b5256303 | 414 | ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb); |
13fe0198 | 415 | if (err != 0) { |
428870ff | 416 | kmem_free(os, sizeof (objset_t)); |
b128c09f BB |
417 | /* convert checksum errors into IO errors */ |
418 | if (err == ECKSUM) | |
2e528b49 | 419 | err = SET_ERROR(EIO); |
34dc7c2f BB |
420 | return (err); |
421 | } | |
9babb374 BB |
422 | |
423 | /* Increase the blocksize if we are permitted. */ | |
424 | if (spa_version(spa) >= SPA_VERSION_USERSPACE && | |
428870ff | 425 | arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) { |
2aa34383 DK |
426 | arc_buf_t *buf = arc_alloc_buf(spa, &os->os_phys_buf, |
427 | ARC_BUFC_METADATA, sizeof (objset_phys_t)); | |
9babb374 | 428 | bzero(buf->b_data, sizeof (objset_phys_t)); |
428870ff BB |
429 | bcopy(os->os_phys_buf->b_data, buf->b_data, |
430 | arc_buf_size(os->os_phys_buf)); | |
d3c2ae1c | 431 | arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf); |
428870ff | 432 | os->os_phys_buf = buf; |
9babb374 BB |
433 | } |
434 | ||
428870ff BB |
435 | os->os_phys = os->os_phys_buf->b_data; |
436 | os->os_flags = os->os_phys->os_flags; | |
34dc7c2f | 437 | } else { |
9babb374 BB |
438 | int size = spa_version(spa) >= SPA_VERSION_USERSPACE ? |
439 | sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE; | |
2aa34383 DK |
440 | os->os_phys_buf = arc_alloc_buf(spa, &os->os_phys_buf, |
441 | ARC_BUFC_METADATA, size); | |
428870ff BB |
442 | os->os_phys = os->os_phys_buf->b_data; |
443 | bzero(os->os_phys, size); | |
34dc7c2f BB |
444 | } |
445 | ||
446 | /* | |
447 | * Note: the changed_cb will be called once before the register | |
448 | * func returns, thus changing the checksum/compression from the | |
b128c09f BB |
449 | * default (fletcher2/off). Snapshots don't need to know about |
450 | * checksum/compression/copies. | |
34dc7c2f | 451 | */ |
9b67f605 | 452 | if (ds != NULL) { |
47dfff3b MA |
453 | boolean_t needlock = B_FALSE; |
454 | ||
b5256303 TC |
455 | os->os_encrypted = (ds->ds_dir->dd_crypto_obj != 0); |
456 | ||
47dfff3b MA |
457 | /* |
458 | * Note: it's valid to open the objset if the dataset is | |
459 | * long-held, in which case the pool_config lock will not | |
460 | * be held. | |
461 | */ | |
462 | if (!dsl_pool_config_held(dmu_objset_pool(os))) { | |
463 | needlock = B_TRUE; | |
464 | dsl_pool_config_enter(dmu_objset_pool(os), FTAG); | |
465 | } | |
b5256303 | 466 | |
13fe0198 MA |
467 | err = dsl_prop_register(ds, |
468 | zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE), | |
428870ff | 469 | primary_cache_changed_cb, os); |
13fe0198 MA |
470 | if (err == 0) { |
471 | err = dsl_prop_register(ds, | |
472 | zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE), | |
428870ff | 473 | secondary_cache_changed_cb, os); |
13fe0198 | 474 | } |
0c66c32d | 475 | if (!ds->ds_is_snapshot) { |
13fe0198 MA |
476 | if (err == 0) { |
477 | err = dsl_prop_register(ds, | |
478 | zfs_prop_to_name(ZFS_PROP_CHECKSUM), | |
428870ff | 479 | checksum_changed_cb, os); |
13fe0198 MA |
480 | } |
481 | if (err == 0) { | |
482 | err = dsl_prop_register(ds, | |
483 | zfs_prop_to_name(ZFS_PROP_COMPRESSION), | |
428870ff | 484 | compression_changed_cb, os); |
13fe0198 MA |
485 | } |
486 | if (err == 0) { | |
487 | err = dsl_prop_register(ds, | |
488 | zfs_prop_to_name(ZFS_PROP_COPIES), | |
428870ff | 489 | copies_changed_cb, os); |
13fe0198 MA |
490 | } |
491 | if (err == 0) { | |
492 | err = dsl_prop_register(ds, | |
493 | zfs_prop_to_name(ZFS_PROP_DEDUP), | |
428870ff | 494 | dedup_changed_cb, os); |
13fe0198 MA |
495 | } |
496 | if (err == 0) { | |
497 | err = dsl_prop_register(ds, | |
498 | zfs_prop_to_name(ZFS_PROP_LOGBIAS), | |
428870ff | 499 | logbias_changed_cb, os); |
13fe0198 MA |
500 | } |
501 | if (err == 0) { | |
502 | err = dsl_prop_register(ds, | |
503 | zfs_prop_to_name(ZFS_PROP_SYNC), | |
428870ff | 504 | sync_changed_cb, os); |
13fe0198 | 505 | } |
faf0f58c MA |
506 | if (err == 0) { |
507 | err = dsl_prop_register(ds, | |
508 | zfs_prop_to_name( | |
509 | ZFS_PROP_REDUNDANT_METADATA), | |
510 | redundant_metadata_changed_cb, os); | |
511 | } | |
f1512ee6 MA |
512 | if (err == 0) { |
513 | err = dsl_prop_register(ds, | |
514 | zfs_prop_to_name(ZFS_PROP_RECORDSIZE), | |
515 | recordsize_changed_cb, os); | |
516 | } | |
50c957f7 NB |
517 | if (err == 0) { |
518 | err = dsl_prop_register(ds, | |
519 | zfs_prop_to_name(ZFS_PROP_DNODESIZE), | |
520 | dnodesize_changed_cb, os); | |
521 | } | |
b128c09f | 522 | } |
47dfff3b MA |
523 | if (needlock) |
524 | dsl_pool_config_exit(dmu_objset_pool(os), FTAG); | |
13fe0198 | 525 | if (err != 0) { |
d3c2ae1c | 526 | arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf); |
428870ff | 527 | kmem_free(os, sizeof (objset_t)); |
34dc7c2f BB |
528 | return (err); |
529 | } | |
9b67f605 | 530 | } else { |
34dc7c2f | 531 | /* It's the meta-objset. */ |
428870ff | 532 | os->os_checksum = ZIO_CHECKSUM_FLETCHER_4; |
99197f03 | 533 | os->os_compress = ZIO_COMPRESS_ON; |
b5256303 | 534 | os->os_encrypted = B_FALSE; |
428870ff BB |
535 | os->os_copies = spa_max_replication(spa); |
536 | os->os_dedup_checksum = ZIO_CHECKSUM_OFF; | |
faf0f58c MA |
537 | os->os_dedup_verify = B_FALSE; |
538 | os->os_logbias = ZFS_LOGBIAS_LATENCY; | |
539 | os->os_sync = ZFS_SYNC_STANDARD; | |
428870ff BB |
540 | os->os_primary_cache = ZFS_CACHE_ALL; |
541 | os->os_secondary_cache = ZFS_CACHE_ALL; | |
50c957f7 | 542 | os->os_dnodesize = DNODE_MIN_SIZE; |
34dc7c2f BB |
543 | } |
544 | ||
0c66c32d | 545 | if (ds == NULL || !ds->ds_is_snapshot) |
572e2857 | 546 | os->os_zil_header = os->os_phys->os_zil_header; |
428870ff | 547 | os->os_zil = zil_alloc(os, &os->os_zil_header); |
34dc7c2f BB |
548 | |
549 | for (i = 0; i < TXG_SIZE; i++) { | |
64fc7762 MA |
550 | os->os_dirty_dnodes[i] = multilist_create(sizeof (dnode_t), |
551 | offsetof(dnode_t, dn_dirty_link[i]), | |
552 | dnode_multilist_index_func); | |
34dc7c2f | 553 | } |
428870ff | 554 | list_create(&os->os_dnodes, sizeof (dnode_t), |
34dc7c2f | 555 | offsetof(dnode_t, dn_link)); |
428870ff | 556 | list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t), |
34dc7c2f BB |
557 | offsetof(dmu_buf_impl_t, db_link)); |
558 | ||
0c66c32d JG |
559 | list_link_init(&os->os_evicting_node); |
560 | ||
428870ff | 561 | mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL); |
64fc7762 | 562 | mutex_init(&os->os_userused_lock, NULL, MUTEX_DEFAULT, NULL); |
428870ff BB |
563 | mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL); |
564 | mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL); | |
dbeb8796 MA |
565 | os->os_obj_next_percpu_len = boot_ncpus; |
566 | os->os_obj_next_percpu = kmem_zalloc(os->os_obj_next_percpu_len * | |
567 | sizeof (os->os_obj_next_percpu[0]), KM_SLEEP); | |
428870ff | 568 | |
0c66c32d JG |
569 | dnode_special_open(os, &os->os_phys->os_meta_dnode, |
570 | DMU_META_DNODE_OBJECT, &os->os_meta_dnode); | |
428870ff | 571 | if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) { |
0c66c32d JG |
572 | dnode_special_open(os, &os->os_phys->os_userused_dnode, |
573 | DMU_USERUSED_OBJECT, &os->os_userused_dnode); | |
574 | dnode_special_open(os, &os->os_phys->os_groupused_dnode, | |
575 | DMU_GROUPUSED_OBJECT, &os->os_groupused_dnode); | |
9babb374 | 576 | } |
34dc7c2f | 577 | |
1de321e6 JX |
578 | mutex_init(&os->os_upgrade_lock, NULL, MUTEX_DEFAULT, NULL); |
579 | ||
428870ff | 580 | *osp = os; |
34dc7c2f BB |
581 | return (0); |
582 | } | |
583 | ||
428870ff BB |
584 | int |
585 | dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp) | |
34dc7c2f | 586 | { |
428870ff | 587 | int err = 0; |
34dc7c2f | 588 | |
47dfff3b MA |
589 | /* |
590 | * We shouldn't be doing anything with dsl_dataset_t's unless the | |
591 | * pool_config lock is held, or the dataset is long-held. | |
592 | */ | |
593 | ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool) || | |
594 | dsl_dataset_long_held(ds)); | |
595 | ||
34dc7c2f | 596 | mutex_enter(&ds->ds_opening_lock); |
9b67f605 MA |
597 | if (ds->ds_objset == NULL) { |
598 | objset_t *os; | |
cc9bb3e5 | 599 | rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG); |
34dc7c2f | 600 | err = dmu_objset_open_impl(dsl_dataset_get_spa(ds), |
9b67f605 | 601 | ds, dsl_dataset_get_blkptr(ds), &os); |
cc9bb3e5 | 602 | rrw_exit(&ds->ds_bp_rwlock, FTAG); |
9b67f605 MA |
603 | |
604 | if (err == 0) { | |
605 | mutex_enter(&ds->ds_lock); | |
606 | ASSERT(ds->ds_objset == NULL); | |
607 | ds->ds_objset = os; | |
608 | mutex_exit(&ds->ds_lock); | |
609 | } | |
34dc7c2f | 610 | } |
9b67f605 | 611 | *osp = ds->ds_objset; |
34dc7c2f | 612 | mutex_exit(&ds->ds_opening_lock); |
428870ff | 613 | return (err); |
34dc7c2f BB |
614 | } |
615 | ||
13fe0198 MA |
616 | /* |
617 | * Holds the pool while the objset is held. Therefore only one objset | |
618 | * can be held at a time. | |
619 | */ | |
34dc7c2f | 620 | int |
b5256303 TC |
621 | dmu_objset_hold_flags(const char *name, boolean_t decrypt, void *tag, |
622 | objset_t **osp) | |
34dc7c2f | 623 | { |
13fe0198 | 624 | dsl_pool_t *dp; |
428870ff | 625 | dsl_dataset_t *ds; |
34dc7c2f | 626 | int err; |
b5256303 | 627 | ds_hold_flags_t flags = (decrypt) ? DS_HOLD_FLAG_DECRYPT : 0; |
34dc7c2f | 628 | |
13fe0198 MA |
629 | err = dsl_pool_hold(name, tag, &dp); |
630 | if (err != 0) | |
631 | return (err); | |
b5256303 | 632 | err = dsl_dataset_hold_flags(dp, name, flags, tag, &ds); |
13fe0198 MA |
633 | if (err != 0) { |
634 | dsl_pool_rele(dp, tag); | |
428870ff | 635 | return (err); |
13fe0198 | 636 | } |
428870ff BB |
637 | |
638 | err = dmu_objset_from_ds(ds, osp); | |
13fe0198 | 639 | if (err != 0) { |
428870ff | 640 | dsl_dataset_rele(ds, tag); |
13fe0198 MA |
641 | dsl_pool_rele(dp, tag); |
642 | } | |
428870ff | 643 | |
34dc7c2f BB |
644 | return (err); |
645 | } | |
646 | ||
b5256303 TC |
647 | int |
648 | dmu_objset_hold(const char *name, void *tag, objset_t **osp) | |
649 | { | |
650 | return (dmu_objset_hold_flags(name, B_FALSE, tag, osp)); | |
651 | } | |
652 | ||
9c43027b AJ |
653 | static int |
654 | dmu_objset_own_impl(dsl_dataset_t *ds, dmu_objset_type_t type, | |
b5256303 | 655 | boolean_t readonly, boolean_t decrypt, void *tag, objset_t **osp) |
9c43027b AJ |
656 | { |
657 | int err; | |
658 | ||
659 | err = dmu_objset_from_ds(ds, osp); | |
660 | if (err != 0) { | |
b5256303 | 661 | return (err); |
9c43027b | 662 | } else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) { |
9c43027b AJ |
663 | return (SET_ERROR(EINVAL)); |
664 | } else if (!readonly && dsl_dataset_is_snapshot(ds)) { | |
9c43027b AJ |
665 | return (SET_ERROR(EROFS)); |
666 | } | |
b5256303 TC |
667 | |
668 | /* if we are decrypting, we can now check MACs in os->os_phys_buf */ | |
669 | if (decrypt && arc_is_unauthenticated((*osp)->os_phys_buf)) { | |
670 | err = arc_untransform((*osp)->os_phys_buf, (*osp)->os_spa, | |
671 | ds->ds_object, B_FALSE); | |
672 | if (err != 0) | |
673 | return (err); | |
674 | ||
675 | ASSERT0(arc_is_unauthenticated((*osp)->os_phys_buf)); | |
676 | } | |
677 | ||
678 | return (0); | |
9c43027b AJ |
679 | } |
680 | ||
13fe0198 MA |
681 | /* |
682 | * dsl_pool must not be held when this is called. | |
683 | * Upon successful return, there will be a longhold on the dataset, | |
684 | * and the dsl_pool will not be held. | |
685 | */ | |
34dc7c2f | 686 | int |
428870ff | 687 | dmu_objset_own(const char *name, dmu_objset_type_t type, |
b5256303 | 688 | boolean_t readonly, boolean_t decrypt, void *tag, objset_t **osp) |
34dc7c2f | 689 | { |
13fe0198 | 690 | dsl_pool_t *dp; |
34dc7c2f BB |
691 | dsl_dataset_t *ds; |
692 | int err; | |
b5256303 | 693 | ds_hold_flags_t flags = (decrypt) ? DS_HOLD_FLAG_DECRYPT : 0; |
34dc7c2f | 694 | |
13fe0198 MA |
695 | err = dsl_pool_hold(name, FTAG, &dp); |
696 | if (err != 0) | |
697 | return (err); | |
b5256303 | 698 | err = dsl_dataset_own(dp, name, flags, tag, &ds); |
13fe0198 MA |
699 | if (err != 0) { |
700 | dsl_pool_rele(dp, FTAG); | |
34dc7c2f | 701 | return (err); |
13fe0198 | 702 | } |
b5256303 TC |
703 | err = dmu_objset_own_impl(ds, type, readonly, decrypt, tag, osp); |
704 | if (err != 0) { | |
705 | dsl_dataset_disown(ds, flags, tag); | |
706 | dsl_pool_rele(dp, FTAG); | |
707 | return (err); | |
708 | } | |
709 | ||
4807c0ba TC |
710 | /* user accounting requires the dataset to be decrypted */ |
711 | if (dmu_objset_userobjspace_upgradable(*osp) && | |
712 | (ds->ds_dir->dd_crypto_obj == 0 || decrypt)) | |
1de321e6 JX |
713 | dmu_objset_userobjspace_upgrade(*osp); |
714 | ||
c0daec32 | 715 | dsl_pool_rele(dp, FTAG); |
b5256303 | 716 | return (0); |
34dc7c2f BB |
717 | } |
718 | ||
9c43027b AJ |
719 | int |
720 | dmu_objset_own_obj(dsl_pool_t *dp, uint64_t obj, dmu_objset_type_t type, | |
b5256303 | 721 | boolean_t readonly, boolean_t decrypt, void *tag, objset_t **osp) |
9c43027b AJ |
722 | { |
723 | dsl_dataset_t *ds; | |
724 | int err; | |
b5256303 | 725 | ds_hold_flags_t flags = (decrypt) ? DS_HOLD_FLAG_DECRYPT : 0; |
9c43027b | 726 | |
b5256303 | 727 | err = dsl_dataset_own_obj(dp, obj, flags, tag, &ds); |
9c43027b AJ |
728 | if (err != 0) |
729 | return (err); | |
730 | ||
b5256303 TC |
731 | err = dmu_objset_own_impl(ds, type, readonly, decrypt, tag, osp); |
732 | if (err != 0) { | |
733 | dsl_dataset_disown(ds, flags, tag); | |
734 | return (err); | |
735 | } | |
736 | ||
737 | return (0); | |
9c43027b AJ |
738 | } |
739 | ||
34dc7c2f | 740 | void |
b5256303 | 741 | dmu_objset_rele_flags(objset_t *os, boolean_t decrypt, void *tag) |
34dc7c2f | 742 | { |
b5256303 TC |
743 | ds_hold_flags_t flags = (decrypt) ? DS_HOLD_FLAG_DECRYPT : 0; |
744 | ||
13fe0198 | 745 | dsl_pool_t *dp = dmu_objset_pool(os); |
b5256303 | 746 | dsl_dataset_rele_flags(os->os_dsl_dataset, flags, tag); |
13fe0198 | 747 | dsl_pool_rele(dp, tag); |
428870ff | 748 | } |
b128c09f | 749 | |
b5256303 TC |
750 | void |
751 | dmu_objset_rele(objset_t *os, void *tag) | |
752 | { | |
753 | dmu_objset_rele_flags(os, B_FALSE, tag); | |
754 | } | |
755 | ||
831baf06 KW |
756 | /* |
757 | * When we are called, os MUST refer to an objset associated with a dataset | |
758 | * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner | |
759 | * == tag. We will then release and reacquire ownership of the dataset while | |
760 | * holding the pool config_rwlock to avoid intervening namespace or ownership | |
761 | * changes may occur. | |
762 | * | |
763 | * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to | |
764 | * release the hold on its dataset and acquire a new one on the dataset of the | |
765 | * same name so that it can be partially torn down and reconstructed. | |
766 | */ | |
767 | void | |
b5256303 | 768 | dmu_objset_refresh_ownership(objset_t *os, boolean_t decrypt, void *tag) |
831baf06 KW |
769 | { |
770 | dsl_pool_t *dp; | |
771 | dsl_dataset_t *ds, *newds; | |
eca7b760 | 772 | char name[ZFS_MAX_DATASET_NAME_LEN]; |
831baf06 KW |
773 | |
774 | ds = os->os_dsl_dataset; | |
775 | VERIFY3P(ds, !=, NULL); | |
776 | VERIFY3P(ds->ds_owner, ==, tag); | |
777 | VERIFY(dsl_dataset_long_held(ds)); | |
778 | ||
779 | dsl_dataset_name(ds, name); | |
780 | dp = dmu_objset_pool(os); | |
781 | dsl_pool_config_enter(dp, FTAG); | |
b5256303 TC |
782 | dmu_objset_disown(os, decrypt, tag); |
783 | VERIFY0(dsl_dataset_own(dp, name, | |
784 | (decrypt) ? DS_HOLD_FLAG_DECRYPT : 0, tag, &newds)); | |
831baf06 KW |
785 | VERIFY3P(newds, ==, os->os_dsl_dataset); |
786 | dsl_pool_config_exit(dp, FTAG); | |
787 | } | |
788 | ||
428870ff | 789 | void |
b5256303 | 790 | dmu_objset_disown(objset_t *os, boolean_t decrypt, void *tag) |
428870ff | 791 | { |
1de321e6 JX |
792 | /* |
793 | * Stop upgrading thread | |
794 | */ | |
795 | dmu_objset_upgrade_stop(os); | |
b5256303 TC |
796 | dsl_dataset_disown(os->os_dsl_dataset, |
797 | (decrypt) ? DS_HOLD_FLAG_DECRYPT : 0, tag); | |
34dc7c2f BB |
798 | } |
799 | ||
13fe0198 | 800 | void |
34dc7c2f BB |
801 | dmu_objset_evict_dbufs(objset_t *os) |
802 | { | |
0c66c32d | 803 | dnode_t *dn_marker; |
34dc7c2f BB |
804 | dnode_t *dn; |
805 | ||
0c66c32d | 806 | dn_marker = kmem_alloc(sizeof (dnode_t), KM_SLEEP); |
34dc7c2f | 807 | |
0c66c32d JG |
808 | mutex_enter(&os->os_lock); |
809 | dn = list_head(&os->os_dnodes); | |
810 | while (dn != NULL) { | |
811 | /* | |
812 | * Skip dnodes without holds. We have to do this dance | |
813 | * because dnode_add_ref() only works if there is already a | |
814 | * hold. If the dnode has no holds, then it has no dbufs. | |
815 | */ | |
816 | if (dnode_add_ref(dn, FTAG)) { | |
817 | list_insert_after(&os->os_dnodes, dn, dn_marker); | |
818 | mutex_exit(&os->os_lock); | |
34dc7c2f | 819 | |
0c66c32d JG |
820 | dnode_evict_dbufs(dn); |
821 | dnode_rele(dn, FTAG); | |
34dc7c2f | 822 | |
0c66c32d JG |
823 | mutex_enter(&os->os_lock); |
824 | dn = list_next(&os->os_dnodes, dn_marker); | |
825 | list_remove(&os->os_dnodes, dn_marker); | |
826 | } else { | |
827 | dn = list_next(&os->os_dnodes, dn); | |
828 | } | |
829 | } | |
830 | mutex_exit(&os->os_lock); | |
34dc7c2f | 831 | |
0c66c32d | 832 | kmem_free(dn_marker, sizeof (dnode_t)); |
34dc7c2f | 833 | |
0c66c32d JG |
834 | if (DMU_USERUSED_DNODE(os) != NULL) { |
835 | dnode_evict_dbufs(DMU_GROUPUSED_DNODE(os)); | |
836 | dnode_evict_dbufs(DMU_USERUSED_DNODE(os)); | |
34dc7c2f | 837 | } |
0c66c32d | 838 | dnode_evict_dbufs(DMU_META_DNODE(os)); |
34dc7c2f BB |
839 | } |
840 | ||
0c66c32d JG |
841 | /* |
842 | * Objset eviction processing is split into into two pieces. | |
843 | * The first marks the objset as evicting, evicts any dbufs that | |
844 | * have a refcount of zero, and then queues up the objset for the | |
845 | * second phase of eviction. Once os->os_dnodes has been cleared by | |
846 | * dnode_buf_pageout()->dnode_destroy(), the second phase is executed. | |
847 | * The second phase closes the special dnodes, dequeues the objset from | |
848 | * the list of those undergoing eviction, and finally frees the objset. | |
849 | * | |
850 | * NOTE: Due to asynchronous eviction processing (invocation of | |
851 | * dnode_buf_pageout()), it is possible for the meta dnode for the | |
852 | * objset to have no holds even though os->os_dnodes is not empty. | |
853 | */ | |
34dc7c2f | 854 | void |
428870ff | 855 | dmu_objset_evict(objset_t *os) |
34dc7c2f | 856 | { |
6f1ffb06 MA |
857 | dsl_dataset_t *ds = os->os_dsl_dataset; |
858 | ||
1c27024e | 859 | for (int t = 0; t < TXG_SIZE; t++) |
428870ff | 860 | ASSERT(!dmu_objset_is_dirty(os, t)); |
34dc7c2f | 861 | |
0eb21616 JG |
862 | if (ds) |
863 | dsl_prop_unregister_all(ds, os); | |
34dc7c2f | 864 | |
428870ff BB |
865 | if (os->os_sa) |
866 | sa_tear_down(os); | |
867 | ||
13fe0198 | 868 | dmu_objset_evict_dbufs(os); |
34dc7c2f | 869 | |
0c66c32d JG |
870 | mutex_enter(&os->os_lock); |
871 | spa_evicting_os_register(os->os_spa, os); | |
872 | if (list_is_empty(&os->os_dnodes)) { | |
873 | mutex_exit(&os->os_lock); | |
874 | dmu_objset_evict_done(os); | |
875 | } else { | |
876 | mutex_exit(&os->os_lock); | |
877 | } | |
b5256303 TC |
878 | |
879 | ||
0c66c32d JG |
880 | } |
881 | ||
882 | void | |
883 | dmu_objset_evict_done(objset_t *os) | |
884 | { | |
885 | ASSERT3P(list_head(&os->os_dnodes), ==, NULL); | |
886 | ||
572e2857 BB |
887 | dnode_special_close(&os->os_meta_dnode); |
888 | if (DMU_USERUSED_DNODE(os)) { | |
889 | dnode_special_close(&os->os_userused_dnode); | |
890 | dnode_special_close(&os->os_groupused_dnode); | |
9babb374 | 891 | } |
428870ff BB |
892 | zil_free(os->os_zil); |
893 | ||
d3c2ae1c | 894 | arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf); |
572e2857 BB |
895 | |
896 | /* | |
897 | * This is a barrier to prevent the objset from going away in | |
898 | * dnode_move() until we can safely ensure that the objset is still in | |
899 | * use. We consider the objset valid before the barrier and invalid | |
900 | * after the barrier. | |
901 | */ | |
902 | rw_enter(&os_lock, RW_READER); | |
903 | rw_exit(&os_lock); | |
904 | ||
dbeb8796 MA |
905 | kmem_free(os->os_obj_next_percpu, |
906 | os->os_obj_next_percpu_len * sizeof (os->os_obj_next_percpu[0])); | |
907 | ||
428870ff | 908 | mutex_destroy(&os->os_lock); |
64fc7762 | 909 | mutex_destroy(&os->os_userused_lock); |
428870ff BB |
910 | mutex_destroy(&os->os_obj_lock); |
911 | mutex_destroy(&os->os_user_ptr_lock); | |
c17486b2 | 912 | mutex_destroy(&os->os_upgrade_lock); |
64fc7762 MA |
913 | for (int i = 0; i < TXG_SIZE; i++) { |
914 | multilist_destroy(os->os_dirty_dnodes[i]); | |
915 | } | |
0c66c32d | 916 | spa_evicting_os_deregister(os->os_spa, os); |
428870ff BB |
917 | kmem_free(os, sizeof (objset_t)); |
918 | } | |
9babb374 | 919 | |
428870ff BB |
920 | timestruc_t |
921 | dmu_objset_snap_cmtime(objset_t *os) | |
922 | { | |
923 | return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir)); | |
34dc7c2f BB |
924 | } |
925 | ||
428870ff | 926 | objset_t * |
b5256303 TC |
927 | dmu_objset_create_impl_dnstats(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, |
928 | dmu_objset_type_t type, int levels, int blksz, int ibs, dmu_tx_t *tx) | |
34dc7c2f | 929 | { |
428870ff | 930 | objset_t *os; |
34dc7c2f BB |
931 | dnode_t *mdn; |
932 | ||
933 | ASSERT(dmu_tx_is_syncing(tx)); | |
13fe0198 | 934 | |
b5256303 TC |
935 | if (blksz == 0) |
936 | blksz = DNODE_BLOCK_SIZE; | |
4807c0ba | 937 | if (ibs == 0) |
b5256303 TC |
938 | ibs = DN_MAX_INDBLKSHIFT; |
939 | ||
572e2857 | 940 | if (ds != NULL) |
13fe0198 | 941 | VERIFY0(dmu_objset_from_ds(ds, &os)); |
572e2857 | 942 | else |
13fe0198 | 943 | VERIFY0(dmu_objset_open_impl(spa, NULL, bp, &os)); |
572e2857 BB |
944 | |
945 | mdn = DMU_META_DNODE(os); | |
34dc7c2f | 946 | |
b5256303 TC |
947 | dnode_allocate(mdn, DMU_OT_DNODE, blksz, ibs, DMU_OT_NONE, 0, |
948 | DNODE_MIN_SLOTS, tx); | |
34dc7c2f BB |
949 | |
950 | /* | |
951 | * We don't want to have to increase the meta-dnode's nlevels | |
952 | * later, because then we could do it in quescing context while | |
953 | * we are also accessing it in open context. | |
954 | * | |
955 | * This precaution is not necessary for the MOS (ds == NULL), | |
956 | * because the MOS is only updated in syncing context. | |
957 | * This is most fortunate: the MOS is the only objset that | |
958 | * needs to be synced multiple times as spa_sync() iterates | |
959 | * to convergence, so minimizing its dn_nlevels matters. | |
960 | */ | |
961 | if (ds != NULL) { | |
b5256303 TC |
962 | if (levels == 0) { |
963 | levels = 1; | |
964 | ||
965 | /* | |
966 | * Determine the number of levels necessary for the | |
967 | * meta-dnode to contain DN_MAX_OBJECT dnodes. Note | |
968 | * that in order to ensure that we do not overflow | |
969 | * 64 bits, there has to be a nlevels that gives us a | |
970 | * number of blocks > DN_MAX_OBJECT but < 2^64. | |
971 | * Therefore, (mdn->dn_indblkshift - SPA_BLKPTRSHIFT) | |
972 | * (10) must be less than (64 - log2(DN_MAX_OBJECT)) | |
973 | * (16). | |
974 | */ | |
975 | while ((uint64_t)mdn->dn_nblkptr << | |
976 | (mdn->dn_datablkshift - DNODE_SHIFT + (levels - 1) * | |
977 | (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) < | |
978 | DN_MAX_OBJECT) | |
979 | levels++; | |
980 | } | |
34dc7c2f BB |
981 | |
982 | mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] = | |
983 | mdn->dn_nlevels = levels; | |
984 | } | |
985 | ||
986 | ASSERT(type != DMU_OST_NONE); | |
987 | ASSERT(type != DMU_OST_ANY); | |
988 | ASSERT(type < DMU_OST_NUMTYPES); | |
428870ff | 989 | os->os_phys->os_type = type; |
b5256303 TC |
990 | |
991 | /* | |
992 | * Enable user accounting if it is enabled and this is not an | |
993 | * encrypted receive. | |
994 | */ | |
995 | if (dmu_objset_userused_enabled(os) && | |
996 | (!os->os_encrypted || !dmu_objset_is_receiving(os))) { | |
428870ff | 997 | os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE; |
1de321e6 JX |
998 | if (dmu_objset_userobjused_enabled(os)) { |
999 | ds->ds_feature_activation_needed[ | |
1000 | SPA_FEATURE_USEROBJ_ACCOUNTING] = B_TRUE; | |
1001 | os->os_phys->os_flags |= | |
1002 | OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE; | |
1003 | } | |
428870ff | 1004 | os->os_flags = os->os_phys->os_flags; |
9babb374 | 1005 | } |
34dc7c2f BB |
1006 | |
1007 | dsl_dataset_dirty(ds, tx); | |
1008 | ||
428870ff | 1009 | return (os); |
34dc7c2f BB |
1010 | } |
1011 | ||
b5256303 TC |
1012 | /* called from dsl for meta-objset */ |
1013 | objset_t * | |
1014 | dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, | |
1015 | dmu_objset_type_t type, dmu_tx_t *tx) | |
1016 | { | |
1017 | return (dmu_objset_create_impl_dnstats(spa, ds, bp, type, 0, 0, 0, tx)); | |
1018 | } | |
1019 | ||
13fe0198 MA |
1020 | typedef struct dmu_objset_create_arg { |
1021 | const char *doca_name; | |
1022 | cred_t *doca_cred; | |
1023 | void (*doca_userfunc)(objset_t *os, void *arg, | |
1024 | cred_t *cr, dmu_tx_t *tx); | |
1025 | void *doca_userarg; | |
1026 | dmu_objset_type_t doca_type; | |
1027 | uint64_t doca_flags; | |
b5256303 | 1028 | dsl_crypto_params_t *doca_dcp; |
13fe0198 | 1029 | } dmu_objset_create_arg_t; |
34dc7c2f BB |
1030 | |
1031 | /*ARGSUSED*/ | |
1032 | static int | |
13fe0198 | 1033 | dmu_objset_create_check(void *arg, dmu_tx_t *tx) |
34dc7c2f | 1034 | { |
13fe0198 MA |
1035 | dmu_objset_create_arg_t *doca = arg; |
1036 | dsl_pool_t *dp = dmu_tx_pool(tx); | |
1037 | dsl_dir_t *pdd; | |
1038 | const char *tail; | |
1039 | int error; | |
34dc7c2f | 1040 | |
13fe0198 | 1041 | if (strchr(doca->doca_name, '@') != NULL) |
2e528b49 | 1042 | return (SET_ERROR(EINVAL)); |
34dc7c2f | 1043 | |
eca7b760 IK |
1044 | if (strlen(doca->doca_name) >= ZFS_MAX_DATASET_NAME_LEN) |
1045 | return (SET_ERROR(ENAMETOOLONG)); | |
1046 | ||
13fe0198 MA |
1047 | error = dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail); |
1048 | if (error != 0) | |
1049 | return (error); | |
1050 | if (tail == NULL) { | |
1051 | dsl_dir_rele(pdd, FTAG); | |
2e528b49 | 1052 | return (SET_ERROR(EEXIST)); |
34dc7c2f | 1053 | } |
b5256303 TC |
1054 | |
1055 | error = dmu_objset_create_crypt_check(pdd, doca->doca_dcp); | |
1056 | if (error != 0) { | |
1057 | dsl_dir_rele(pdd, FTAG); | |
1058 | return (error); | |
1059 | } | |
1060 | ||
788eb90c JJ |
1061 | error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL, |
1062 | doca->doca_cred); | |
b5256303 | 1063 | |
13fe0198 | 1064 | dsl_dir_rele(pdd, FTAG); |
34dc7c2f | 1065 | |
788eb90c | 1066 | return (error); |
34dc7c2f BB |
1067 | } |
1068 | ||
1069 | static void | |
13fe0198 | 1070 | dmu_objset_create_sync(void *arg, dmu_tx_t *tx) |
34dc7c2f | 1071 | { |
13fe0198 MA |
1072 | dmu_objset_create_arg_t *doca = arg; |
1073 | dsl_pool_t *dp = dmu_tx_pool(tx); | |
1074 | dsl_dir_t *pdd; | |
1075 | const char *tail; | |
6f1ffb06 | 1076 | dsl_dataset_t *ds; |
13fe0198 | 1077 | uint64_t obj; |
6f1ffb06 | 1078 | blkptr_t *bp; |
13fe0198 | 1079 | objset_t *os; |
b5256303 | 1080 | zio_t *rzio; |
34dc7c2f | 1081 | |
13fe0198 | 1082 | VERIFY0(dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail)); |
34dc7c2f | 1083 | |
13fe0198 | 1084 | obj = dsl_dataset_create_sync(pdd, tail, NULL, doca->doca_flags, |
b5256303 | 1085 | doca->doca_cred, doca->doca_dcp, tx); |
34dc7c2f | 1086 | |
b5256303 TC |
1087 | VERIFY0(dsl_dataset_hold_obj_flags(pdd->dd_pool, obj, |
1088 | DS_HOLD_FLAG_DECRYPT, FTAG, &ds)); | |
cc9bb3e5 | 1089 | rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG); |
6f1ffb06 | 1090 | bp = dsl_dataset_get_blkptr(ds); |
13fe0198 MA |
1091 | os = dmu_objset_create_impl(pdd->dd_pool->dp_spa, |
1092 | ds, bp, doca->doca_type, tx); | |
cc9bb3e5 | 1093 | rrw_exit(&ds->ds_bp_rwlock, FTAG); |
34dc7c2f | 1094 | |
13fe0198 MA |
1095 | if (doca->doca_userfunc != NULL) { |
1096 | doca->doca_userfunc(os, doca->doca_userarg, | |
1097 | doca->doca_cred, tx); | |
34dc7c2f BB |
1098 | } |
1099 | ||
b5256303 | 1100 | /* |
4807c0ba | 1101 | * The doca_userfunc() may write out some data that needs to be |
b5256303 TC |
1102 | * encrypted if the dataset is encrypted (specifically the root |
1103 | * directory). This data must be written out before the encryption | |
1104 | * key mapping is removed by dsl_dataset_rele_flags(). Force the | |
1105 | * I/O to occur immediately by invoking the relevant sections of | |
1106 | * dsl_pool_sync(). | |
1107 | */ | |
1108 | if (os->os_encrypted) { | |
1109 | dsl_dataset_t *tmpds = NULL; | |
1110 | boolean_t need_sync_done = B_FALSE; | |
1111 | ||
4807c0ba TC |
1112 | mutex_enter(&ds->ds_lock); |
1113 | ds->ds_owner = FTAG; | |
1114 | mutex_exit(&ds->ds_lock); | |
1115 | ||
b5256303 | 1116 | rzio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); |
4807c0ba TC |
1117 | tmpds = txg_list_remove_this(&dp->dp_dirty_datasets, ds, |
1118 | tx->tx_txg); | |
b5256303 | 1119 | if (tmpds != NULL) { |
b5256303 TC |
1120 | dsl_dataset_sync(ds, rzio, tx); |
1121 | need_sync_done = B_TRUE; | |
1122 | } | |
1123 | VERIFY0(zio_wait(rzio)); | |
1124 | ||
1125 | dmu_objset_do_userquota_updates(os, tx); | |
1126 | taskq_wait(dp->dp_sync_taskq); | |
1127 | ||
1128 | rzio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); | |
4807c0ba TC |
1129 | tmpds = txg_list_remove_this(&dp->dp_dirty_datasets, ds, |
1130 | tx->tx_txg); | |
b5256303 | 1131 | if (tmpds != NULL) { |
b5256303 TC |
1132 | dmu_buf_rele(ds->ds_dbuf, ds); |
1133 | dsl_dataset_sync(ds, rzio, tx); | |
1134 | } | |
1135 | VERIFY0(zio_wait(rzio)); | |
1136 | ||
1137 | if (need_sync_done) | |
1138 | dsl_dataset_sync_done(ds, tx); | |
4807c0ba TC |
1139 | |
1140 | mutex_enter(&ds->ds_lock); | |
1141 | ds->ds_owner = NULL; | |
1142 | mutex_exit(&ds->ds_lock); | |
b5256303 TC |
1143 | } |
1144 | ||
13fe0198 | 1145 | spa_history_log_internal_ds(ds, "create", tx, ""); |
a0bd735a BP |
1146 | zvol_create_minors(dp->dp_spa, doca->doca_name, B_TRUE); |
1147 | ||
b5256303 | 1148 | dsl_dataset_rele_flags(ds, DS_HOLD_FLAG_DECRYPT, FTAG); |
13fe0198 | 1149 | dsl_dir_rele(pdd, FTAG); |
34dc7c2f BB |
1150 | } |
1151 | ||
1152 | int | |
428870ff | 1153 | dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags, |
b5256303 | 1154 | dsl_crypto_params_t *dcp, dmu_objset_create_sync_func_t func, void *arg) |
34dc7c2f | 1155 | { |
13fe0198 | 1156 | dmu_objset_create_arg_t doca; |
b5256303 | 1157 | dsl_crypto_params_t tmp_dcp = { 0 }; |
34dc7c2f | 1158 | |
13fe0198 MA |
1159 | doca.doca_name = name; |
1160 | doca.doca_cred = CRED(); | |
1161 | doca.doca_flags = flags; | |
1162 | doca.doca_userfunc = func; | |
1163 | doca.doca_userarg = arg; | |
1164 | doca.doca_type = type; | |
34dc7c2f | 1165 | |
b5256303 TC |
1166 | /* |
1167 | * Some callers (mostly for testing) do not provide a dcp on their | |
1168 | * own but various code inside the sync task will require it to be | |
1169 | * allocated. Rather than adding NULL checks throughout this code | |
1170 | * or adding dummy dcp's to all of the callers we simply create a | |
1171 | * dummy one here and use that. This zero dcp will have the same | |
1172 | * effect as asking for inheritence of all encryption params. | |
1173 | */ | |
1174 | doca.doca_dcp = (dcp != NULL) ? dcp : &tmp_dcp; | |
1175 | ||
13fe0198 | 1176 | return (dsl_sync_task(name, |
3d45fdd6 | 1177 | dmu_objset_create_check, dmu_objset_create_sync, &doca, |
b5256303 | 1178 | 6, ZFS_SPACE_CHECK_NORMAL)); |
34dc7c2f BB |
1179 | } |
1180 | ||
13fe0198 MA |
1181 | typedef struct dmu_objset_clone_arg { |
1182 | const char *doca_clone; | |
1183 | const char *doca_origin; | |
1184 | cred_t *doca_cred; | |
1185 | } dmu_objset_clone_arg_t; | |
1186 | ||
1187 | /*ARGSUSED*/ | |
1188 | static int | |
1189 | dmu_objset_clone_check(void *arg, dmu_tx_t *tx) | |
34dc7c2f | 1190 | { |
13fe0198 | 1191 | dmu_objset_clone_arg_t *doca = arg; |
428870ff BB |
1192 | dsl_dir_t *pdd; |
1193 | const char *tail; | |
13fe0198 MA |
1194 | int error; |
1195 | dsl_dataset_t *origin; | |
1196 | dsl_pool_t *dp = dmu_tx_pool(tx); | |
34dc7c2f | 1197 | |
13fe0198 | 1198 | if (strchr(doca->doca_clone, '@') != NULL) |
2e528b49 | 1199 | return (SET_ERROR(EINVAL)); |
13fe0198 | 1200 | |
eca7b760 IK |
1201 | if (strlen(doca->doca_clone) >= ZFS_MAX_DATASET_NAME_LEN) |
1202 | return (SET_ERROR(ENAMETOOLONG)); | |
1203 | ||
13fe0198 MA |
1204 | error = dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail); |
1205 | if (error != 0) | |
1206 | return (error); | |
428870ff | 1207 | if (tail == NULL) { |
13fe0198 | 1208 | dsl_dir_rele(pdd, FTAG); |
2e528b49 | 1209 | return (SET_ERROR(EEXIST)); |
34dc7c2f | 1210 | } |
1cddb8c9 | 1211 | |
788eb90c JJ |
1212 | error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL, |
1213 | doca->doca_cred); | |
1214 | if (error != 0) { | |
1215 | dsl_dir_rele(pdd, FTAG); | |
1216 | return (SET_ERROR(EDQUOT)); | |
1217 | } | |
428870ff | 1218 | |
13fe0198 | 1219 | error = dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin); |
b5256303 TC |
1220 | if (error != 0) { |
1221 | dsl_dir_rele(pdd, FTAG); | |
572e2857 | 1222 | return (error); |
b5256303 | 1223 | } |
572e2857 | 1224 | |
13fe0198 | 1225 | /* You can only clone snapshots, not the head datasets. */ |
0c66c32d | 1226 | if (!origin->ds_is_snapshot) { |
13fe0198 | 1227 | dsl_dataset_rele(origin, FTAG); |
b5256303 | 1228 | dsl_dir_rele(pdd, FTAG); |
2e528b49 | 1229 | return (SET_ERROR(EINVAL)); |
428870ff | 1230 | } |
b5256303 TC |
1231 | |
1232 | error = dmu_objset_clone_crypt_check(pdd, origin->ds_dir); | |
1233 | if (error != 0) { | |
1234 | dsl_dataset_rele(origin, FTAG); | |
1235 | dsl_dir_rele(pdd, FTAG); | |
1236 | return (error); | |
1237 | } | |
1238 | ||
13fe0198 | 1239 | dsl_dataset_rele(origin, FTAG); |
b5256303 | 1240 | dsl_dir_rele(pdd, FTAG); |
572e2857 | 1241 | |
13fe0198 | 1242 | return (0); |
9babb374 | 1243 | } |
34dc7c2f | 1244 | |
13fe0198 MA |
1245 | static void |
1246 | dmu_objset_clone_sync(void *arg, dmu_tx_t *tx) | |
34dc7c2f | 1247 | { |
13fe0198 MA |
1248 | dmu_objset_clone_arg_t *doca = arg; |
1249 | dsl_pool_t *dp = dmu_tx_pool(tx); | |
1250 | dsl_dir_t *pdd; | |
1251 | const char *tail; | |
1252 | dsl_dataset_t *origin, *ds; | |
1253 | uint64_t obj; | |
eca7b760 | 1254 | char namebuf[ZFS_MAX_DATASET_NAME_LEN]; |
6f1ffb06 | 1255 | |
13fe0198 MA |
1256 | VERIFY0(dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail)); |
1257 | VERIFY0(dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin)); | |
6f1ffb06 | 1258 | |
13fe0198 | 1259 | obj = dsl_dataset_create_sync(pdd, tail, origin, 0, |
b5256303 | 1260 | doca->doca_cred, NULL, tx); |
34dc7c2f | 1261 | |
13fe0198 MA |
1262 | VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds)); |
1263 | dsl_dataset_name(origin, namebuf); | |
1264 | spa_history_log_internal_ds(ds, "clone", tx, | |
1265 | "origin=%s (%llu)", namebuf, origin->ds_object); | |
a0bd735a | 1266 | zvol_create_minors(dp->dp_spa, doca->doca_clone, B_TRUE); |
13fe0198 MA |
1267 | dsl_dataset_rele(ds, FTAG); |
1268 | dsl_dataset_rele(origin, FTAG); | |
1269 | dsl_dir_rele(pdd, FTAG); | |
34dc7c2f BB |
1270 | } |
1271 | ||
1272 | int | |
13fe0198 | 1273 | dmu_objset_clone(const char *clone, const char *origin) |
34dc7c2f | 1274 | { |
13fe0198 | 1275 | dmu_objset_clone_arg_t doca; |
34dc7c2f | 1276 | |
13fe0198 MA |
1277 | doca.doca_clone = clone; |
1278 | doca.doca_origin = origin; | |
1279 | doca.doca_cred = CRED(); | |
572e2857 | 1280 | |
13fe0198 | 1281 | return (dsl_sync_task(clone, |
3d45fdd6 | 1282 | dmu_objset_clone_check, dmu_objset_clone_sync, &doca, |
b5256303 | 1283 | 6, ZFS_SPACE_CHECK_NORMAL)); |
6f1ffb06 MA |
1284 | } |
1285 | ||
1286 | int | |
1287 | dmu_objset_snapshot_one(const char *fsname, const char *snapname) | |
1288 | { | |
1289 | int err; | |
1290 | char *longsnap = kmem_asprintf("%s@%s", fsname, snapname); | |
1291 | nvlist_t *snaps = fnvlist_alloc(); | |
1292 | ||
1293 | fnvlist_add_boolean(snaps, longsnap); | |
6f1ffb06 | 1294 | strfree(longsnap); |
13fe0198 MA |
1295 | err = dsl_dataset_snapshot(snaps, NULL, NULL); |
1296 | fnvlist_free(snaps); | |
6f1ffb06 MA |
1297 | return (err); |
1298 | } | |
1299 | ||
1de321e6 JX |
1300 | static void |
1301 | dmu_objset_upgrade_task_cb(void *data) | |
1302 | { | |
1303 | objset_t *os = data; | |
1304 | ||
1305 | mutex_enter(&os->os_upgrade_lock); | |
1306 | os->os_upgrade_status = EINTR; | |
1307 | if (!os->os_upgrade_exit) { | |
1308 | mutex_exit(&os->os_upgrade_lock); | |
1309 | ||
1310 | os->os_upgrade_status = os->os_upgrade_cb(os); | |
1311 | mutex_enter(&os->os_upgrade_lock); | |
1312 | } | |
1313 | os->os_upgrade_exit = B_TRUE; | |
1314 | os->os_upgrade_id = 0; | |
1315 | mutex_exit(&os->os_upgrade_lock); | |
c0daec32 | 1316 | dsl_dataset_long_rele(dmu_objset_ds(os), upgrade_tag); |
1de321e6 JX |
1317 | } |
1318 | ||
1319 | static void | |
1320 | dmu_objset_upgrade(objset_t *os, dmu_objset_upgrade_cb_t cb) | |
1321 | { | |
1322 | if (os->os_upgrade_id != 0) | |
1323 | return; | |
1324 | ||
c0daec32 AB |
1325 | ASSERT(dsl_pool_config_held(dmu_objset_pool(os))); |
1326 | dsl_dataset_long_hold(dmu_objset_ds(os), upgrade_tag); | |
1327 | ||
1de321e6 JX |
1328 | mutex_enter(&os->os_upgrade_lock); |
1329 | if (os->os_upgrade_id == 0 && os->os_upgrade_status == 0) { | |
1330 | os->os_upgrade_exit = B_FALSE; | |
1331 | os->os_upgrade_cb = cb; | |
1332 | os->os_upgrade_id = taskq_dispatch( | |
1333 | os->os_spa->spa_upgrade_taskq, | |
1334 | dmu_objset_upgrade_task_cb, os, TQ_SLEEP); | |
c0daec32 AB |
1335 | if (os->os_upgrade_id == TASKQID_INVALID) { |
1336 | dsl_dataset_long_rele(dmu_objset_ds(os), upgrade_tag); | |
1de321e6 | 1337 | os->os_upgrade_status = ENOMEM; |
c0daec32 | 1338 | } |
1de321e6 JX |
1339 | } |
1340 | mutex_exit(&os->os_upgrade_lock); | |
1341 | } | |
1342 | ||
1343 | static void | |
1344 | dmu_objset_upgrade_stop(objset_t *os) | |
1345 | { | |
1346 | mutex_enter(&os->os_upgrade_lock); | |
1347 | os->os_upgrade_exit = B_TRUE; | |
1348 | if (os->os_upgrade_id != 0) { | |
1349 | taskqid_t id = os->os_upgrade_id; | |
1350 | ||
1351 | os->os_upgrade_id = 0; | |
1352 | mutex_exit(&os->os_upgrade_lock); | |
1353 | ||
c0daec32 AB |
1354 | if ((taskq_cancel_id(os->os_spa->spa_upgrade_taskq, id)) == 0) { |
1355 | dsl_dataset_long_rele(dmu_objset_ds(os), upgrade_tag); | |
1356 | } | |
4807c0ba | 1357 | txg_wait_synced(os->os_spa->spa_dsl_pool, 0); |
1de321e6 JX |
1358 | } else { |
1359 | mutex_exit(&os->os_upgrade_lock); | |
1360 | } | |
1361 | } | |
1362 | ||
34dc7c2f | 1363 | static void |
64fc7762 | 1364 | dmu_objset_sync_dnodes(multilist_sublist_t *list, dmu_tx_t *tx) |
34dc7c2f BB |
1365 | { |
1366 | dnode_t *dn; | |
1367 | ||
64fc7762 | 1368 | while ((dn = multilist_sublist_head(list)) != NULL) { |
34dc7c2f BB |
1369 | ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT); |
1370 | ASSERT(dn->dn_dbuf->db_data_pending); | |
1371 | /* | |
9babb374 BB |
1372 | * Initialize dn_zio outside dnode_sync() because the |
1373 | * meta-dnode needs to set it ouside dnode_sync(). | |
34dc7c2f BB |
1374 | */ |
1375 | dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio; | |
1376 | ASSERT(dn->dn_zio); | |
1377 | ||
1378 | ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS); | |
64fc7762 | 1379 | multilist_sublist_remove(list, dn); |
9babb374 | 1380 | |
64fc7762 MA |
1381 | multilist_t *newlist = dn->dn_objset->os_synced_dnodes; |
1382 | if (newlist != NULL) { | |
9babb374 | 1383 | (void) dnode_add_ref(dn, newlist); |
64fc7762 | 1384 | multilist_insert(newlist, dn); |
9babb374 BB |
1385 | } |
1386 | ||
34dc7c2f BB |
1387 | dnode_sync(dn, tx); |
1388 | } | |
1389 | } | |
1390 | ||
1391 | /* ARGSUSED */ | |
1392 | static void | |
428870ff | 1393 | dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg) |
34dc7c2f | 1394 | { |
b128c09f | 1395 | blkptr_t *bp = zio->io_bp; |
428870ff | 1396 | objset_t *os = arg; |
34dc7c2f | 1397 | dnode_phys_t *dnp = &os->os_phys->os_meta_dnode; |
b5256303 | 1398 | uint64_t fill = 0; |
34dc7c2f | 1399 | |
9b67f605 | 1400 | ASSERT(!BP_IS_EMBEDDED(bp)); |
13fe0198 MA |
1401 | ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_OBJSET); |
1402 | ASSERT0(BP_GET_LEVEL(bp)); | |
34dc7c2f BB |
1403 | |
1404 | /* | |
9babb374 BB |
1405 | * Update rootbp fill count: it should be the number of objects |
1406 | * allocated in the object set (not counting the "special" | |
1407 | * objects that are stored in the objset_phys_t -- the meta | |
1408 | * dnode and user/group accounting objects). | |
34dc7c2f | 1409 | */ |
1c27024e | 1410 | for (int i = 0; i < dnp->dn_nblkptr; i++) |
b5256303 TC |
1411 | fill += BP_GET_FILL(&dnp->dn_blkptr[i]); |
1412 | ||
1413 | BP_SET_FILL(bp, fill); | |
1414 | ||
cc9bb3e5 GM |
1415 | if (os->os_dsl_dataset != NULL) |
1416 | rrw_enter(&os->os_dsl_dataset->ds_bp_rwlock, RW_WRITER, FTAG); | |
1417 | *os->os_rootbp = *bp; | |
1418 | if (os->os_dsl_dataset != NULL) | |
1419 | rrw_exit(&os->os_dsl_dataset->ds_bp_rwlock, FTAG); | |
428870ff BB |
1420 | } |
1421 | ||
1422 | /* ARGSUSED */ | |
1423 | static void | |
1424 | dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg) | |
1425 | { | |
1426 | blkptr_t *bp = zio->io_bp; | |
1427 | blkptr_t *bp_orig = &zio->io_bp_orig; | |
1428 | objset_t *os = arg; | |
34dc7c2f | 1429 | |
b128c09f | 1430 | if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { |
428870ff | 1431 | ASSERT(BP_EQUAL(bp, bp_orig)); |
b128c09f | 1432 | } else { |
428870ff BB |
1433 | dsl_dataset_t *ds = os->os_dsl_dataset; |
1434 | dmu_tx_t *tx = os->os_synctx; | |
1435 | ||
1436 | (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE); | |
1437 | dsl_dataset_block_born(ds, bp, tx); | |
34dc7c2f | 1438 | } |
cc9bb3e5 | 1439 | kmem_free(bp, sizeof (*bp)); |
34dc7c2f BB |
1440 | } |
1441 | ||
64fc7762 MA |
1442 | typedef struct sync_dnodes_arg { |
1443 | multilist_t *sda_list; | |
1444 | int sda_sublist_idx; | |
1445 | multilist_t *sda_newlist; | |
1446 | dmu_tx_t *sda_tx; | |
1447 | } sync_dnodes_arg_t; | |
1448 | ||
1449 | static void | |
1450 | sync_dnodes_task(void *arg) | |
1451 | { | |
1452 | sync_dnodes_arg_t *sda = arg; | |
1453 | ||
1454 | multilist_sublist_t *ms = | |
1455 | multilist_sublist_lock(sda->sda_list, sda->sda_sublist_idx); | |
1456 | ||
1457 | dmu_objset_sync_dnodes(ms, sda->sda_tx); | |
1458 | ||
1459 | multilist_sublist_unlock(ms); | |
1460 | ||
1461 | kmem_free(sda, sizeof (*sda)); | |
1462 | } | |
1463 | ||
1464 | ||
34dc7c2f BB |
1465 | /* called from dsl */ |
1466 | void | |
428870ff | 1467 | dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx) |
34dc7c2f BB |
1468 | { |
1469 | int txgoff; | |
5dbd68a3 | 1470 | zbookmark_phys_t zb; |
428870ff | 1471 | zio_prop_t zp; |
34dc7c2f BB |
1472 | zio_t *zio; |
1473 | list_t *list; | |
1474 | dbuf_dirty_record_t *dr; | |
cc9bb3e5 GM |
1475 | blkptr_t *blkptr_copy = kmem_alloc(sizeof (*os->os_rootbp), KM_SLEEP); |
1476 | *blkptr_copy = *os->os_rootbp; | |
34dc7c2f BB |
1477 | |
1478 | dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg); | |
1479 | ||
1480 | ASSERT(dmu_tx_is_syncing(tx)); | |
1481 | /* XXX the write_done callback should really give us the tx... */ | |
1482 | os->os_synctx = tx; | |
1483 | ||
1484 | if (os->os_dsl_dataset == NULL) { | |
1485 | /* | |
1486 | * This is the MOS. If we have upgraded, | |
1487 | * spa_max_replication() could change, so reset | |
1488 | * os_copies here. | |
1489 | */ | |
1490 | os->os_copies = spa_max_replication(os->os_spa); | |
1491 | } | |
1492 | ||
1493 | /* | |
1494 | * Create the root block IO | |
1495 | */ | |
428870ff BB |
1496 | SET_BOOKMARK(&zb, os->os_dsl_dataset ? |
1497 | os->os_dsl_dataset->ds_object : DMU_META_OBJSET, | |
1498 | ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); | |
294f6806 | 1499 | arc_release(os->os_phys_buf, &os->os_phys_buf); |
b128c09f | 1500 | |
82644107 | 1501 | dmu_write_policy(os, NULL, 0, 0, &zp); |
9babb374 | 1502 | |
b5256303 TC |
1503 | /* |
1504 | * If we are either claiming the ZIL or doing a raw receive write out | |
1505 | * the os_phys_buf raw. Neither of these actions will effect the MAC | |
1506 | * at this point. | |
1507 | */ | |
1508 | if (arc_is_unauthenticated(os->os_phys_buf) || os->os_next_write_raw) { | |
1509 | ASSERT(os->os_encrypted); | |
1510 | os->os_next_write_raw = B_FALSE; | |
1511 | arc_convert_to_raw(os->os_phys_buf, | |
1512 | os->os_dsl_dataset->ds_object, ZFS_HOST_BYTEORDER, | |
1513 | DMU_OT_OBJSET, NULL, NULL, NULL); | |
1514 | } | |
1515 | ||
428870ff | 1516 | zio = arc_write(pio, os->os_spa, tx->tx_txg, |
cc9bb3e5 | 1517 | blkptr_copy, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os), |
bc77ba73 PD |
1518 | &zp, dmu_objset_write_ready, NULL, NULL, dmu_objset_write_done, |
1519 | os, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb); | |
34dc7c2f BB |
1520 | |
1521 | /* | |
9babb374 | 1522 | * Sync special dnodes - the parent IO for the sync is the root block |
34dc7c2f | 1523 | */ |
572e2857 BB |
1524 | DMU_META_DNODE(os)->dn_zio = zio; |
1525 | dnode_sync(DMU_META_DNODE(os), tx); | |
34dc7c2f | 1526 | |
9babb374 BB |
1527 | os->os_phys->os_flags = os->os_flags; |
1528 | ||
572e2857 BB |
1529 | if (DMU_USERUSED_DNODE(os) && |
1530 | DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) { | |
1531 | DMU_USERUSED_DNODE(os)->dn_zio = zio; | |
1532 | dnode_sync(DMU_USERUSED_DNODE(os), tx); | |
1533 | DMU_GROUPUSED_DNODE(os)->dn_zio = zio; | |
1534 | dnode_sync(DMU_GROUPUSED_DNODE(os), tx); | |
9babb374 BB |
1535 | } |
1536 | ||
34dc7c2f BB |
1537 | txgoff = tx->tx_txg & TXG_MASK; |
1538 | ||
b5256303 TC |
1539 | if (dmu_objset_userused_enabled(os) && |
1540 | (!os->os_encrypted || !dmu_objset_is_receiving(os))) { | |
9babb374 BB |
1541 | /* |
1542 | * We must create the list here because it uses the | |
64fc7762 MA |
1543 | * dn_dirty_link[] of this txg. But it may already |
1544 | * exist because we call dsl_dataset_sync() twice per txg. | |
9babb374 | 1545 | */ |
64fc7762 MA |
1546 | if (os->os_synced_dnodes == NULL) { |
1547 | os->os_synced_dnodes = | |
1548 | multilist_create(sizeof (dnode_t), | |
1549 | offsetof(dnode_t, dn_dirty_link[txgoff]), | |
1550 | dnode_multilist_index_func); | |
1551 | } else { | |
1552 | ASSERT3U(os->os_synced_dnodes->ml_offset, ==, | |
1553 | offsetof(dnode_t, dn_dirty_link[txgoff])); | |
1554 | } | |
9babb374 BB |
1555 | } |
1556 | ||
64fc7762 MA |
1557 | for (int i = 0; |
1558 | i < multilist_get_num_sublists(os->os_dirty_dnodes[txgoff]); i++) { | |
1559 | sync_dnodes_arg_t *sda = kmem_alloc(sizeof (*sda), KM_SLEEP); | |
1560 | sda->sda_list = os->os_dirty_dnodes[txgoff]; | |
1561 | sda->sda_sublist_idx = i; | |
1562 | sda->sda_tx = tx; | |
1563 | (void) taskq_dispatch(dmu_objset_pool(os)->dp_sync_taskq, | |
1564 | sync_dnodes_task, sda, 0); | |
1565 | /* callback frees sda */ | |
1566 | } | |
1567 | taskq_wait(dmu_objset_pool(os)->dp_sync_taskq); | |
34dc7c2f | 1568 | |
572e2857 | 1569 | list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff]; |
64fc7762 | 1570 | while ((dr = list_head(list)) != NULL) { |
13fe0198 | 1571 | ASSERT0(dr->dr_dbuf->db_level); |
34dc7c2f BB |
1572 | list_remove(list, dr); |
1573 | if (dr->dr_zio) | |
1574 | zio_nowait(dr->dr_zio); | |
1575 | } | |
68cbd56e NB |
1576 | |
1577 | /* Enable dnode backfill if enough objects have been freed. */ | |
1578 | if (os->os_freed_dnodes >= dmu_rescan_dnode_threshold) { | |
1579 | os->os_rescan_dnodes = B_TRUE; | |
1580 | os->os_freed_dnodes = 0; | |
1581 | } | |
1582 | ||
34dc7c2f BB |
1583 | /* |
1584 | * Free intent log blocks up to this tx. | |
1585 | */ | |
1586 | zil_sync(os->os_zil, tx); | |
b128c09f | 1587 | os->os_phys->os_zil_header = os->os_zil_header; |
34dc7c2f BB |
1588 | zio_nowait(zio); |
1589 | } | |
1590 | ||
428870ff BB |
1591 | boolean_t |
1592 | dmu_objset_is_dirty(objset_t *os, uint64_t txg) | |
1593 | { | |
64fc7762 | 1594 | return (!multilist_is_empty(os->os_dirty_dnodes[txg & TXG_MASK])); |
428870ff BB |
1595 | } |
1596 | ||
572e2857 | 1597 | static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES]; |
9babb374 BB |
1598 | |
1599 | void | |
1600 | dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb) | |
1601 | { | |
1602 | used_cbs[ost] = cb; | |
1603 | } | |
1604 | ||
1605 | boolean_t | |
428870ff | 1606 | dmu_objset_userused_enabled(objset_t *os) |
9babb374 BB |
1607 | { |
1608 | return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE && | |
572e2857 BB |
1609 | used_cbs[os->os_phys->os_type] != NULL && |
1610 | DMU_USERUSED_DNODE(os) != NULL); | |
9babb374 BB |
1611 | } |
1612 | ||
1de321e6 JX |
1613 | boolean_t |
1614 | dmu_objset_userobjused_enabled(objset_t *os) | |
1615 | { | |
1616 | return (dmu_objset_userused_enabled(os) && | |
1617 | spa_feature_is_enabled(os->os_spa, SPA_FEATURE_USEROBJ_ACCOUNTING)); | |
1618 | } | |
1619 | ||
9b7a83cb JX |
1620 | typedef struct userquota_node { |
1621 | /* must be in the first filed, see userquota_update_cache() */ | |
1622 | char uqn_id[20 + DMU_OBJACCT_PREFIX_LEN]; | |
1623 | int64_t uqn_delta; | |
1624 | avl_node_t uqn_node; | |
1625 | } userquota_node_t; | |
1626 | ||
1627 | typedef struct userquota_cache { | |
1628 | avl_tree_t uqc_user_deltas; | |
1629 | avl_tree_t uqc_group_deltas; | |
1630 | } userquota_cache_t; | |
1631 | ||
1632 | static int | |
1633 | userquota_compare(const void *l, const void *r) | |
1634 | { | |
1635 | const userquota_node_t *luqn = l; | |
1636 | const userquota_node_t *ruqn = r; | |
e4ffa98d | 1637 | int rv; |
9b7a83cb JX |
1638 | |
1639 | /* | |
1640 | * NB: can only access uqn_id because userquota_update_cache() doesn't | |
1641 | * pass in an entire userquota_node_t. | |
1642 | */ | |
e4ffa98d BB |
1643 | rv = strcmp(luqn->uqn_id, ruqn->uqn_id); |
1644 | ||
1645 | return (AVL_ISIGN(rv)); | |
9b7a83cb JX |
1646 | } |
1647 | ||
1648 | static void | |
1649 | do_userquota_cacheflush(objset_t *os, userquota_cache_t *cache, dmu_tx_t *tx) | |
1650 | { | |
1651 | void *cookie; | |
1652 | userquota_node_t *uqn; | |
1653 | ||
1654 | ASSERT(dmu_tx_is_syncing(tx)); | |
1655 | ||
1656 | cookie = NULL; | |
1657 | while ((uqn = avl_destroy_nodes(&cache->uqc_user_deltas, | |
1658 | &cookie)) != NULL) { | |
64fc7762 MA |
1659 | /* |
1660 | * os_userused_lock protects against concurrent calls to | |
1661 | * zap_increment_int(). It's needed because zap_increment_int() | |
1662 | * is not thread-safe (i.e. not atomic). | |
1663 | */ | |
1664 | mutex_enter(&os->os_userused_lock); | |
9b7a83cb JX |
1665 | VERIFY0(zap_increment(os, DMU_USERUSED_OBJECT, |
1666 | uqn->uqn_id, uqn->uqn_delta, tx)); | |
64fc7762 | 1667 | mutex_exit(&os->os_userused_lock); |
9b7a83cb JX |
1668 | kmem_free(uqn, sizeof (*uqn)); |
1669 | } | |
1670 | avl_destroy(&cache->uqc_user_deltas); | |
1671 | ||
1672 | cookie = NULL; | |
1673 | while ((uqn = avl_destroy_nodes(&cache->uqc_group_deltas, | |
1674 | &cookie)) != NULL) { | |
64fc7762 | 1675 | mutex_enter(&os->os_userused_lock); |
9b7a83cb JX |
1676 | VERIFY0(zap_increment(os, DMU_GROUPUSED_OBJECT, |
1677 | uqn->uqn_id, uqn->uqn_delta, tx)); | |
64fc7762 | 1678 | mutex_exit(&os->os_userused_lock); |
9b7a83cb JX |
1679 | kmem_free(uqn, sizeof (*uqn)); |
1680 | } | |
1681 | avl_destroy(&cache->uqc_group_deltas); | |
1682 | } | |
1683 | ||
1684 | static void | |
1685 | userquota_update_cache(avl_tree_t *avl, const char *id, int64_t delta) | |
1686 | { | |
1687 | userquota_node_t *uqn; | |
1688 | avl_index_t idx; | |
1689 | ||
1690 | ASSERT(strlen(id) < sizeof (uqn->uqn_id)); | |
1691 | /* | |
1692 | * Use id directly for searching because uqn_id is the first field of | |
1693 | * userquota_node_t and fields after uqn_id won't be accessed in | |
1694 | * avl_find(). | |
1695 | */ | |
1696 | uqn = avl_find(avl, (const void *)id, &idx); | |
1697 | if (uqn == NULL) { | |
1698 | uqn = kmem_zalloc(sizeof (*uqn), KM_SLEEP); | |
1f51b525 | 1699 | strlcpy(uqn->uqn_id, id, sizeof (uqn->uqn_id)); |
9b7a83cb JX |
1700 | avl_insert(avl, uqn, idx); |
1701 | } | |
1702 | uqn->uqn_delta += delta; | |
1703 | } | |
1704 | ||
428870ff | 1705 | static void |
9b7a83cb JX |
1706 | do_userquota_update(userquota_cache_t *cache, uint64_t used, uint64_t flags, |
1707 | uint64_t user, uint64_t group, boolean_t subtract) | |
428870ff BB |
1708 | { |
1709 | if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) { | |
50c957f7 | 1710 | int64_t delta = DNODE_MIN_SIZE + used; |
9b7a83cb JX |
1711 | char name[20]; |
1712 | ||
428870ff BB |
1713 | if (subtract) |
1714 | delta = -delta; | |
9b7a83cb JX |
1715 | |
1716 | (void) sprintf(name, "%llx", (longlong_t)user); | |
1717 | userquota_update_cache(&cache->uqc_user_deltas, name, delta); | |
1718 | ||
1719 | (void) sprintf(name, "%llx", (longlong_t)group); | |
1720 | userquota_update_cache(&cache->uqc_group_deltas, name, delta); | |
428870ff BB |
1721 | } |
1722 | } | |
1723 | ||
1de321e6 | 1724 | static void |
9b7a83cb JX |
1725 | do_userobjquota_update(userquota_cache_t *cache, uint64_t flags, |
1726 | uint64_t user, uint64_t group, boolean_t subtract) | |
1de321e6 JX |
1727 | { |
1728 | if (flags & DNODE_FLAG_USEROBJUSED_ACCOUNTED) { | |
1729 | char name[20 + DMU_OBJACCT_PREFIX_LEN]; | |
9b7a83cb | 1730 | int delta = subtract ? -1 : 1; |
1de321e6 JX |
1731 | |
1732 | (void) snprintf(name, sizeof (name), DMU_OBJACCT_PREFIX "%llx", | |
1733 | (longlong_t)user); | |
9b7a83cb | 1734 | userquota_update_cache(&cache->uqc_user_deltas, name, delta); |
1de321e6 JX |
1735 | |
1736 | (void) snprintf(name, sizeof (name), DMU_OBJACCT_PREFIX "%llx", | |
1737 | (longlong_t)group); | |
9b7a83cb | 1738 | userquota_update_cache(&cache->uqc_group_deltas, name, delta); |
1de321e6 JX |
1739 | } |
1740 | } | |
1741 | ||
64fc7762 MA |
1742 | typedef struct userquota_updates_arg { |
1743 | objset_t *uua_os; | |
1744 | int uua_sublist_idx; | |
1745 | dmu_tx_t *uua_tx; | |
1746 | } userquota_updates_arg_t; | |
1747 | ||
1748 | static void | |
1749 | userquota_updates_task(void *arg) | |
9babb374 | 1750 | { |
64fc7762 MA |
1751 | userquota_updates_arg_t *uua = arg; |
1752 | objset_t *os = uua->uua_os; | |
1753 | dmu_tx_t *tx = uua->uua_tx; | |
9babb374 | 1754 | dnode_t *dn; |
9b7a83cb | 1755 | userquota_cache_t cache = { { 0 } }; |
9babb374 | 1756 | |
64fc7762 MA |
1757 | multilist_sublist_t *list = |
1758 | multilist_sublist_lock(os->os_synced_dnodes, uua->uua_sublist_idx); | |
9babb374 | 1759 | |
64fc7762 MA |
1760 | ASSERT(multilist_sublist_head(list) == NULL || |
1761 | dmu_objset_userused_enabled(os)); | |
9b7a83cb JX |
1762 | avl_create(&cache.uqc_user_deltas, userquota_compare, |
1763 | sizeof (userquota_node_t), offsetof(userquota_node_t, uqn_node)); | |
1764 | avl_create(&cache.uqc_group_deltas, userquota_compare, | |
1765 | sizeof (userquota_node_t), offsetof(userquota_node_t, uqn_node)); | |
1766 | ||
64fc7762 | 1767 | while ((dn = multilist_sublist_head(list)) != NULL) { |
572e2857 | 1768 | int flags; |
9babb374 | 1769 | ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object)); |
9babb374 BB |
1770 | ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE || |
1771 | dn->dn_phys->dn_flags & | |
1772 | DNODE_FLAG_USERUSED_ACCOUNTED); | |
1773 | ||
572e2857 BB |
1774 | flags = dn->dn_id_flags; |
1775 | ASSERT(flags); | |
1776 | if (flags & DN_ID_OLD_EXIST) { | |
9b7a83cb JX |
1777 | do_userquota_update(&cache, |
1778 | dn->dn_oldused, dn->dn_oldflags, | |
1779 | dn->dn_olduid, dn->dn_oldgid, B_TRUE); | |
1780 | do_userobjquota_update(&cache, dn->dn_oldflags, | |
1781 | dn->dn_olduid, dn->dn_oldgid, B_TRUE); | |
428870ff | 1782 | } |
572e2857 | 1783 | if (flags & DN_ID_NEW_EXIST) { |
9b7a83cb JX |
1784 | do_userquota_update(&cache, |
1785 | DN_USED_BYTES(dn->dn_phys), dn->dn_phys->dn_flags, | |
1786 | dn->dn_newuid, dn->dn_newgid, B_FALSE); | |
1787 | do_userobjquota_update(&cache, dn->dn_phys->dn_flags, | |
1788 | dn->dn_newuid, dn->dn_newgid, B_FALSE); | |
428870ff BB |
1789 | } |
1790 | ||
572e2857 | 1791 | mutex_enter(&dn->dn_mtx); |
428870ff BB |
1792 | dn->dn_oldused = 0; |
1793 | dn->dn_oldflags = 0; | |
1794 | if (dn->dn_id_flags & DN_ID_NEW_EXIST) { | |
1795 | dn->dn_olduid = dn->dn_newuid; | |
1796 | dn->dn_oldgid = dn->dn_newgid; | |
1797 | dn->dn_id_flags |= DN_ID_OLD_EXIST; | |
1798 | if (dn->dn_bonuslen == 0) | |
1799 | dn->dn_id_flags |= DN_ID_CHKED_SPILL; | |
1800 | else | |
1801 | dn->dn_id_flags |= DN_ID_CHKED_BONUS; | |
1802 | } | |
1803 | dn->dn_id_flags &= ~(DN_ID_NEW_EXIST); | |
9babb374 BB |
1804 | mutex_exit(&dn->dn_mtx); |
1805 | ||
64fc7762 MA |
1806 | multilist_sublist_remove(list, dn); |
1807 | dnode_rele(dn, os->os_synced_dnodes); | |
9babb374 | 1808 | } |
9b7a83cb | 1809 | do_userquota_cacheflush(os, &cache, tx); |
64fc7762 MA |
1810 | multilist_sublist_unlock(list); |
1811 | kmem_free(uua, sizeof (*uua)); | |
1812 | } | |
1813 | ||
1814 | void | |
1815 | dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx) | |
1816 | { | |
1817 | if (!dmu_objset_userused_enabled(os)) | |
1818 | return; | |
1819 | ||
b5256303 TC |
1820 | /* if this is a raw receive just return and handle accounting later */ |
1821 | if (os->os_encrypted && dmu_objset_is_receiving(os)) | |
1822 | return; | |
1823 | ||
64fc7762 MA |
1824 | /* Allocate the user/groupused objects if necessary. */ |
1825 | if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) { | |
1826 | VERIFY0(zap_create_claim(os, | |
1827 | DMU_USERUSED_OBJECT, | |
1828 | DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx)); | |
1829 | VERIFY0(zap_create_claim(os, | |
1830 | DMU_GROUPUSED_OBJECT, | |
1831 | DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx)); | |
1832 | } | |
1833 | ||
1834 | for (int i = 0; | |
1835 | i < multilist_get_num_sublists(os->os_synced_dnodes); i++) { | |
1836 | userquota_updates_arg_t *uua = | |
1837 | kmem_alloc(sizeof (*uua), KM_SLEEP); | |
1838 | uua->uua_os = os; | |
1839 | uua->uua_sublist_idx = i; | |
1840 | uua->uua_tx = tx; | |
1841 | /* note: caller does taskq_wait() */ | |
1842 | (void) taskq_dispatch(dmu_objset_pool(os)->dp_sync_taskq, | |
1843 | userquota_updates_task, uua, 0); | |
1844 | /* callback frees uua */ | |
1845 | } | |
9babb374 BB |
1846 | } |
1847 | ||
428870ff BB |
1848 | /* |
1849 | * Returns a pointer to data to find uid/gid from | |
1850 | * | |
1851 | * If a dirty record for transaction group that is syncing can't | |
1852 | * be found then NULL is returned. In the NULL case it is assumed | |
1853 | * the uid/gid aren't changing. | |
1854 | */ | |
1855 | static void * | |
1856 | dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx) | |
1857 | { | |
1858 | dbuf_dirty_record_t *dr, **drp; | |
1859 | void *data; | |
1860 | ||
1861 | if (db->db_dirtycnt == 0) | |
1862 | return (db->db.db_data); /* Nothing is changing */ | |
1863 | ||
1864 | for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next) | |
1865 | if (dr->dr_txg == tx->tx_txg) | |
1866 | break; | |
1867 | ||
572e2857 | 1868 | if (dr == NULL) { |
428870ff | 1869 | data = NULL; |
572e2857 BB |
1870 | } else { |
1871 | dnode_t *dn; | |
1872 | ||
1873 | DB_DNODE_ENTER(dr->dr_dbuf); | |
1874 | dn = DB_DNODE(dr->dr_dbuf); | |
1875 | ||
1876 | if (dn->dn_bonuslen == 0 && | |
1877 | dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID) | |
1878 | data = dr->dt.dl.dr_data->b_data; | |
1879 | else | |
1880 | data = dr->dt.dl.dr_data; | |
1881 | ||
1882 | DB_DNODE_EXIT(dr->dr_dbuf); | |
1883 | } | |
1884 | ||
428870ff BB |
1885 | return (data); |
1886 | } | |
1887 | ||
1888 | void | |
1889 | dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx) | |
1890 | { | |
1891 | objset_t *os = dn->dn_objset; | |
1892 | void *data = NULL; | |
1893 | dmu_buf_impl_t *db = NULL; | |
a117a6d6 GW |
1894 | uint64_t *user = NULL; |
1895 | uint64_t *group = NULL; | |
428870ff BB |
1896 | int flags = dn->dn_id_flags; |
1897 | int error; | |
1898 | boolean_t have_spill = B_FALSE; | |
1899 | ||
1900 | if (!dmu_objset_userused_enabled(dn->dn_objset)) | |
1901 | return; | |
1902 | ||
b5256303 TC |
1903 | /* |
1904 | * Raw receives introduce a problem with user accounting. Raw | |
1905 | * receives cannot update the user accounting info because the | |
1906 | * user ids and the sizes are encrypted. To guarantee that we | |
1907 | * never end up with bad user accounting, we simply disable it | |
1908 | * during raw receives. We also disable this for normal receives | |
1909 | * so that an incremental raw receive may be done on top of an | |
1910 | * existing non-raw receive. | |
1911 | */ | |
1912 | if (os->os_encrypted && dmu_objset_is_receiving(os)) | |
1913 | return; | |
1914 | ||
428870ff BB |
1915 | if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST| |
1916 | DN_ID_CHKED_SPILL))) | |
1917 | return; | |
1918 | ||
1919 | if (before && dn->dn_bonuslen != 0) | |
1920 | data = DN_BONUS(dn->dn_phys); | |
1921 | else if (!before && dn->dn_bonuslen != 0) { | |
a3000f93 BB |
1922 | if (dn->dn_bonus) { |
1923 | db = dn->dn_bonus; | |
428870ff BB |
1924 | mutex_enter(&db->db_mtx); |
1925 | data = dmu_objset_userquota_find_data(db, tx); | |
1926 | } else { | |
1927 | data = DN_BONUS(dn->dn_phys); | |
1928 | } | |
1929 | } else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) { | |
1930 | int rf = 0; | |
1931 | ||
1932 | if (RW_WRITE_HELD(&dn->dn_struct_rwlock)) | |
1933 | rf |= DB_RF_HAVESTRUCT; | |
572e2857 BB |
1934 | error = dmu_spill_hold_by_dnode(dn, |
1935 | rf | DB_RF_MUST_SUCCEED, | |
428870ff BB |
1936 | FTAG, (dmu_buf_t **)&db); |
1937 | ASSERT(error == 0); | |
1938 | mutex_enter(&db->db_mtx); | |
1939 | data = (before) ? db->db.db_data : | |
1940 | dmu_objset_userquota_find_data(db, tx); | |
1941 | have_spill = B_TRUE; | |
1942 | } else { | |
1943 | mutex_enter(&dn->dn_mtx); | |
1944 | dn->dn_id_flags |= DN_ID_CHKED_BONUS; | |
1945 | mutex_exit(&dn->dn_mtx); | |
1946 | return; | |
1947 | } | |
1948 | ||
1949 | if (before) { | |
1950 | ASSERT(data); | |
1951 | user = &dn->dn_olduid; | |
1952 | group = &dn->dn_oldgid; | |
1953 | } else if (data) { | |
1954 | user = &dn->dn_newuid; | |
1955 | group = &dn->dn_newgid; | |
1956 | } | |
1957 | ||
1958 | /* | |
1959 | * Must always call the callback in case the object | |
1960 | * type has changed and that type isn't an object type to track | |
1961 | */ | |
1962 | error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data, | |
1963 | user, group); | |
1964 | ||
1965 | /* | |
1966 | * Preserve existing uid/gid when the callback can't determine | |
1967 | * what the new uid/gid are and the callback returned EEXIST. | |
1968 | * The EEXIST error tells us to just use the existing uid/gid. | |
1969 | * If we don't know what the old values are then just assign | |
1970 | * them to 0, since that is a new file being created. | |
1971 | */ | |
1972 | if (!before && data == NULL && error == EEXIST) { | |
1973 | if (flags & DN_ID_OLD_EXIST) { | |
1974 | dn->dn_newuid = dn->dn_olduid; | |
1975 | dn->dn_newgid = dn->dn_oldgid; | |
1976 | } else { | |
1977 | dn->dn_newuid = 0; | |
1978 | dn->dn_newgid = 0; | |
1979 | } | |
1980 | error = 0; | |
1981 | } | |
1982 | ||
1983 | if (db) | |
1984 | mutex_exit(&db->db_mtx); | |
1985 | ||
1986 | mutex_enter(&dn->dn_mtx); | |
1987 | if (error == 0 && before) | |
1988 | dn->dn_id_flags |= DN_ID_OLD_EXIST; | |
1989 | if (error == 0 && !before) | |
1990 | dn->dn_id_flags |= DN_ID_NEW_EXIST; | |
1991 | ||
1992 | if (have_spill) { | |
1993 | dn->dn_id_flags |= DN_ID_CHKED_SPILL; | |
1994 | } else { | |
1995 | dn->dn_id_flags |= DN_ID_CHKED_BONUS; | |
1996 | } | |
1997 | mutex_exit(&dn->dn_mtx); | |
a3000f93 | 1998 | if (have_spill) |
428870ff BB |
1999 | dmu_buf_rele((dmu_buf_t *)db, FTAG); |
2000 | } | |
2001 | ||
9babb374 BB |
2002 | boolean_t |
2003 | dmu_objset_userspace_present(objset_t *os) | |
2004 | { | |
428870ff | 2005 | return (os->os_phys->os_flags & |
9babb374 BB |
2006 | OBJSET_FLAG_USERACCOUNTING_COMPLETE); |
2007 | } | |
2008 | ||
1de321e6 JX |
2009 | boolean_t |
2010 | dmu_objset_userobjspace_present(objset_t *os) | |
2011 | { | |
2012 | return (os->os_phys->os_flags & | |
2013 | OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE); | |
2014 | } | |
2015 | ||
2016 | static int | |
2017 | dmu_objset_space_upgrade(objset_t *os) | |
9babb374 BB |
2018 | { |
2019 | uint64_t obj; | |
2020 | int err = 0; | |
2021 | ||
9babb374 BB |
2022 | /* |
2023 | * We simply need to mark every object dirty, so that it will be | |
2024 | * synced out and now accounted. If this is called | |
2025 | * concurrently, or if we already did some work before crashing, | |
2026 | * that's fine, since we track each object's accounted state | |
2027 | * independently. | |
2028 | */ | |
2029 | ||
2030 | for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) { | |
45d1cae3 | 2031 | dmu_tx_t *tx; |
9babb374 BB |
2032 | dmu_buf_t *db; |
2033 | int objerr; | |
2034 | ||
1de321e6 JX |
2035 | mutex_enter(&os->os_upgrade_lock); |
2036 | if (os->os_upgrade_exit) | |
2037 | err = SET_ERROR(EINTR); | |
2038 | mutex_exit(&os->os_upgrade_lock); | |
2039 | if (err != 0) | |
2040 | return (err); | |
2041 | ||
9babb374 | 2042 | if (issig(JUSTLOOKING) && issig(FORREAL)) |
2e528b49 | 2043 | return (SET_ERROR(EINTR)); |
9babb374 BB |
2044 | |
2045 | objerr = dmu_bonus_hold(os, obj, FTAG, &db); | |
13fe0198 | 2046 | if (objerr != 0) |
9babb374 | 2047 | continue; |
45d1cae3 | 2048 | tx = dmu_tx_create(os); |
9babb374 BB |
2049 | dmu_tx_hold_bonus(tx, obj); |
2050 | objerr = dmu_tx_assign(tx, TXG_WAIT); | |
13fe0198 | 2051 | if (objerr != 0) { |
d22323e8 | 2052 | dmu_buf_rele(db, FTAG); |
9babb374 BB |
2053 | dmu_tx_abort(tx); |
2054 | continue; | |
2055 | } | |
2056 | dmu_buf_will_dirty(db, tx); | |
2057 | dmu_buf_rele(db, FTAG); | |
2058 | dmu_tx_commit(tx); | |
2059 | } | |
1de321e6 JX |
2060 | return (0); |
2061 | } | |
2062 | ||
2063 | int | |
2064 | dmu_objset_userspace_upgrade(objset_t *os) | |
2065 | { | |
2066 | int err = 0; | |
2067 | ||
2068 | if (dmu_objset_userspace_present(os)) | |
2069 | return (0); | |
2070 | if (dmu_objset_is_snapshot(os)) | |
2071 | return (SET_ERROR(EINVAL)); | |
2072 | if (!dmu_objset_userused_enabled(os)) | |
2073 | return (SET_ERROR(ENOTSUP)); | |
2074 | ||
2075 | err = dmu_objset_space_upgrade(os); | |
2076 | if (err) | |
2077 | return (err); | |
9babb374 | 2078 | |
428870ff | 2079 | os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE; |
9babb374 BB |
2080 | txg_wait_synced(dmu_objset_pool(os), 0); |
2081 | return (0); | |
2082 | } | |
2083 | ||
1de321e6 JX |
2084 | static int |
2085 | dmu_objset_userobjspace_upgrade_cb(objset_t *os) | |
2086 | { | |
2087 | int err = 0; | |
2088 | ||
2089 | if (dmu_objset_userobjspace_present(os)) | |
2090 | return (0); | |
2091 | if (dmu_objset_is_snapshot(os)) | |
2092 | return (SET_ERROR(EINVAL)); | |
2093 | if (!dmu_objset_userobjused_enabled(os)) | |
2094 | return (SET_ERROR(ENOTSUP)); | |
2095 | ||
2096 | dmu_objset_ds(os)->ds_feature_activation_needed[ | |
2097 | SPA_FEATURE_USEROBJ_ACCOUNTING] = B_TRUE; | |
2098 | ||
2099 | err = dmu_objset_space_upgrade(os); | |
2100 | if (err) | |
2101 | return (err); | |
2102 | ||
2103 | os->os_flags |= OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE; | |
2104 | txg_wait_synced(dmu_objset_pool(os), 0); | |
2105 | return (0); | |
2106 | } | |
2107 | ||
2108 | void | |
2109 | dmu_objset_userobjspace_upgrade(objset_t *os) | |
2110 | { | |
2111 | dmu_objset_upgrade(os, dmu_objset_userobjspace_upgrade_cb); | |
2112 | } | |
2113 | ||
126ae9f4 JX |
2114 | boolean_t |
2115 | dmu_objset_userobjspace_upgradable(objset_t *os) | |
2116 | { | |
2117 | return (dmu_objset_type(os) == DMU_OST_ZFS && | |
2118 | !dmu_objset_is_snapshot(os) && | |
2119 | dmu_objset_userobjused_enabled(os) && | |
2120 | !dmu_objset_userobjspace_present(os)); | |
2121 | } | |
2122 | ||
34dc7c2f BB |
2123 | void |
2124 | dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp, | |
2125 | uint64_t *usedobjsp, uint64_t *availobjsp) | |
2126 | { | |
428870ff | 2127 | dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp, |
34dc7c2f BB |
2128 | usedobjsp, availobjsp); |
2129 | } | |
2130 | ||
2131 | uint64_t | |
2132 | dmu_objset_fsid_guid(objset_t *os) | |
2133 | { | |
428870ff | 2134 | return (dsl_dataset_fsid_guid(os->os_dsl_dataset)); |
34dc7c2f BB |
2135 | } |
2136 | ||
2137 | void | |
2138 | dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat) | |
2139 | { | |
428870ff BB |
2140 | stat->dds_type = os->os_phys->os_type; |
2141 | if (os->os_dsl_dataset) | |
2142 | dsl_dataset_fast_stat(os->os_dsl_dataset, stat); | |
34dc7c2f BB |
2143 | } |
2144 | ||
2145 | void | |
2146 | dmu_objset_stats(objset_t *os, nvlist_t *nv) | |
2147 | { | |
428870ff BB |
2148 | ASSERT(os->os_dsl_dataset || |
2149 | os->os_phys->os_type == DMU_OST_META); | |
34dc7c2f | 2150 | |
428870ff BB |
2151 | if (os->os_dsl_dataset != NULL) |
2152 | dsl_dataset_stats(os->os_dsl_dataset, nv); | |
34dc7c2f BB |
2153 | |
2154 | dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE, | |
428870ff | 2155 | os->os_phys->os_type); |
9babb374 BB |
2156 | dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING, |
2157 | dmu_objset_userspace_present(os)); | |
34dc7c2f BB |
2158 | } |
2159 | ||
2160 | int | |
2161 | dmu_objset_is_snapshot(objset_t *os) | |
2162 | { | |
428870ff | 2163 | if (os->os_dsl_dataset != NULL) |
0c66c32d | 2164 | return (os->os_dsl_dataset->ds_is_snapshot); |
34dc7c2f BB |
2165 | else |
2166 | return (B_FALSE); | |
2167 | } | |
2168 | ||
2169 | int | |
2170 | dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen, | |
2171 | boolean_t *conflict) | |
2172 | { | |
428870ff | 2173 | dsl_dataset_t *ds = os->os_dsl_dataset; |
34dc7c2f BB |
2174 | uint64_t ignored; |
2175 | ||
d683ddbb | 2176 | if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0) |
2e528b49 | 2177 | return (SET_ERROR(ENOENT)); |
34dc7c2f BB |
2178 | |
2179 | return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset, | |
d683ddbb | 2180 | dsl_dataset_phys(ds)->ds_snapnames_zapobj, name, 8, 1, &ignored, |
9b7b9cd3 | 2181 | MT_NORMALIZE, real, maxlen, conflict)); |
34dc7c2f BB |
2182 | } |
2183 | ||
2184 | int | |
2185 | dmu_snapshot_list_next(objset_t *os, int namelen, char *name, | |
2186 | uint64_t *idp, uint64_t *offp, boolean_t *case_conflict) | |
2187 | { | |
428870ff | 2188 | dsl_dataset_t *ds = os->os_dsl_dataset; |
34dc7c2f BB |
2189 | zap_cursor_t cursor; |
2190 | zap_attribute_t attr; | |
2191 | ||
13fe0198 MA |
2192 | ASSERT(dsl_pool_config_held(dmu_objset_pool(os))); |
2193 | ||
d683ddbb | 2194 | if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0) |
2e528b49 | 2195 | return (SET_ERROR(ENOENT)); |
34dc7c2f BB |
2196 | |
2197 | zap_cursor_init_serialized(&cursor, | |
2198 | ds->ds_dir->dd_pool->dp_meta_objset, | |
d683ddbb | 2199 | dsl_dataset_phys(ds)->ds_snapnames_zapobj, *offp); |
34dc7c2f BB |
2200 | |
2201 | if (zap_cursor_retrieve(&cursor, &attr) != 0) { | |
2202 | zap_cursor_fini(&cursor); | |
2e528b49 | 2203 | return (SET_ERROR(ENOENT)); |
34dc7c2f BB |
2204 | } |
2205 | ||
2206 | if (strlen(attr.za_name) + 1 > namelen) { | |
2207 | zap_cursor_fini(&cursor); | |
2e528b49 | 2208 | return (SET_ERROR(ENAMETOOLONG)); |
34dc7c2f BB |
2209 | } |
2210 | ||
2211 | (void) strcpy(name, attr.za_name); | |
2212 | if (idp) | |
2213 | *idp = attr.za_first_integer; | |
2214 | if (case_conflict) | |
2215 | *case_conflict = attr.za_normalization_conflict; | |
2216 | zap_cursor_advance(&cursor); | |
2217 | *offp = zap_cursor_serialize(&cursor); | |
2218 | zap_cursor_fini(&cursor); | |
2219 | ||
2220 | return (0); | |
2221 | } | |
2222 | ||
ebe7e575 | 2223 | int |
6772fb67 | 2224 | dmu_snapshot_lookup(objset_t *os, const char *name, uint64_t *value) |
ebe7e575 | 2225 | { |
d1d7e268 | 2226 | return (dsl_dataset_snap_lookup(os->os_dsl_dataset, name, value)); |
ebe7e575 BB |
2227 | } |
2228 | ||
34dc7c2f BB |
2229 | int |
2230 | dmu_dir_list_next(objset_t *os, int namelen, char *name, | |
2231 | uint64_t *idp, uint64_t *offp) | |
2232 | { | |
428870ff | 2233 | dsl_dir_t *dd = os->os_dsl_dataset->ds_dir; |
34dc7c2f BB |
2234 | zap_cursor_t cursor; |
2235 | zap_attribute_t attr; | |
2236 | ||
2237 | /* there is no next dir on a snapshot! */ | |
428870ff | 2238 | if (os->os_dsl_dataset->ds_object != |
d683ddbb | 2239 | dsl_dir_phys(dd)->dd_head_dataset_obj) |
2e528b49 | 2240 | return (SET_ERROR(ENOENT)); |
34dc7c2f BB |
2241 | |
2242 | zap_cursor_init_serialized(&cursor, | |
2243 | dd->dd_pool->dp_meta_objset, | |
d683ddbb | 2244 | dsl_dir_phys(dd)->dd_child_dir_zapobj, *offp); |
34dc7c2f BB |
2245 | |
2246 | if (zap_cursor_retrieve(&cursor, &attr) != 0) { | |
2247 | zap_cursor_fini(&cursor); | |
2e528b49 | 2248 | return (SET_ERROR(ENOENT)); |
34dc7c2f BB |
2249 | } |
2250 | ||
2251 | if (strlen(attr.za_name) + 1 > namelen) { | |
2252 | zap_cursor_fini(&cursor); | |
2e528b49 | 2253 | return (SET_ERROR(ENAMETOOLONG)); |
34dc7c2f BB |
2254 | } |
2255 | ||
2256 | (void) strcpy(name, attr.za_name); | |
2257 | if (idp) | |
2258 | *idp = attr.za_first_integer; | |
2259 | zap_cursor_advance(&cursor); | |
2260 | *offp = zap_cursor_serialize(&cursor); | |
2261 | zap_cursor_fini(&cursor); | |
2262 | ||
2263 | return (0); | |
2264 | } | |
2265 | ||
9c43027b AJ |
2266 | typedef struct dmu_objset_find_ctx { |
2267 | taskq_t *dc_tq; | |
2268 | dsl_pool_t *dc_dp; | |
2269 | uint64_t dc_ddobj; | |
1149ba64 | 2270 | char *dc_ddname; /* last component of ddobj's name */ |
9c43027b AJ |
2271 | int (*dc_func)(dsl_pool_t *, dsl_dataset_t *, void *); |
2272 | void *dc_arg; | |
2273 | int dc_flags; | |
2274 | kmutex_t *dc_error_lock; | |
2275 | int *dc_error; | |
2276 | } dmu_objset_find_ctx_t; | |
2277 | ||
2278 | static void | |
2279 | dmu_objset_find_dp_impl(dmu_objset_find_ctx_t *dcp) | |
b128c09f | 2280 | { |
9c43027b | 2281 | dsl_pool_t *dp = dcp->dc_dp; |
13fe0198 MA |
2282 | dsl_dir_t *dd; |
2283 | dsl_dataset_t *ds; | |
2284 | zap_cursor_t zc; | |
2285 | zap_attribute_t *attr; | |
2286 | uint64_t thisobj; | |
9c43027b | 2287 | int err = 0; |
13fe0198 | 2288 | |
9c43027b AJ |
2289 | /* don't process if there already was an error */ |
2290 | if (*dcp->dc_error != 0) | |
2291 | goto out; | |
13fe0198 | 2292 | |
1149ba64 GM |
2293 | /* |
2294 | * Note: passing the name (dc_ddname) here is optional, but it | |
2295 | * improves performance because we don't need to call | |
2296 | * zap_value_search() to determine the name. | |
2297 | */ | |
2298 | err = dsl_dir_hold_obj(dp, dcp->dc_ddobj, dcp->dc_ddname, FTAG, &dd); | |
13fe0198 | 2299 | if (err != 0) |
9c43027b | 2300 | goto out; |
13fe0198 MA |
2301 | |
2302 | /* Don't visit hidden ($MOS & $ORIGIN) objsets. */ | |
2303 | if (dd->dd_myname[0] == '$') { | |
2304 | dsl_dir_rele(dd, FTAG); | |
9c43027b | 2305 | goto out; |
13fe0198 MA |
2306 | } |
2307 | ||
d683ddbb | 2308 | thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj; |
79c76d5b | 2309 | attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); |
13fe0198 MA |
2310 | |
2311 | /* | |
2312 | * Iterate over all children. | |
2313 | */ | |
9c43027b | 2314 | if (dcp->dc_flags & DS_FIND_CHILDREN) { |
13fe0198 | 2315 | for (zap_cursor_init(&zc, dp->dp_meta_objset, |
d683ddbb | 2316 | dsl_dir_phys(dd)->dd_child_dir_zapobj); |
13fe0198 MA |
2317 | zap_cursor_retrieve(&zc, attr) == 0; |
2318 | (void) zap_cursor_advance(&zc)) { | |
2319 | ASSERT3U(attr->za_integer_length, ==, | |
2320 | sizeof (uint64_t)); | |
2321 | ASSERT3U(attr->za_num_integers, ==, 1); | |
2322 | ||
1c27024e | 2323 | dmu_objset_find_ctx_t *child_dcp = |
1149ba64 | 2324 | kmem_alloc(sizeof (*child_dcp), KM_SLEEP); |
9c43027b AJ |
2325 | *child_dcp = *dcp; |
2326 | child_dcp->dc_ddobj = attr->za_first_integer; | |
1149ba64 | 2327 | child_dcp->dc_ddname = spa_strdup(attr->za_name); |
9c43027b AJ |
2328 | if (dcp->dc_tq != NULL) |
2329 | (void) taskq_dispatch(dcp->dc_tq, | |
2330 | dmu_objset_find_dp_cb, child_dcp, TQ_SLEEP); | |
2331 | else | |
2332 | dmu_objset_find_dp_impl(child_dcp); | |
13fe0198 MA |
2333 | } |
2334 | zap_cursor_fini(&zc); | |
13fe0198 MA |
2335 | } |
2336 | ||
2337 | /* | |
2338 | * Iterate over all snapshots. | |
2339 | */ | |
9c43027b | 2340 | if (dcp->dc_flags & DS_FIND_SNAPSHOTS) { |
13fe0198 MA |
2341 | dsl_dataset_t *ds; |
2342 | err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds); | |
2343 | ||
2344 | if (err == 0) { | |
d683ddbb JG |
2345 | uint64_t snapobj; |
2346 | ||
2347 | snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj; | |
13fe0198 MA |
2348 | dsl_dataset_rele(ds, FTAG); |
2349 | ||
2350 | for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj); | |
2351 | zap_cursor_retrieve(&zc, attr) == 0; | |
2352 | (void) zap_cursor_advance(&zc)) { | |
2353 | ASSERT3U(attr->za_integer_length, ==, | |
2354 | sizeof (uint64_t)); | |
2355 | ASSERT3U(attr->za_num_integers, ==, 1); | |
2356 | ||
2357 | err = dsl_dataset_hold_obj(dp, | |
2358 | attr->za_first_integer, FTAG, &ds); | |
2359 | if (err != 0) | |
2360 | break; | |
9c43027b | 2361 | err = dcp->dc_func(dp, ds, dcp->dc_arg); |
13fe0198 MA |
2362 | dsl_dataset_rele(ds, FTAG); |
2363 | if (err != 0) | |
2364 | break; | |
2365 | } | |
2366 | zap_cursor_fini(&zc); | |
2367 | } | |
2368 | } | |
2369 | ||
13fe0198 MA |
2370 | kmem_free(attr, sizeof (zap_attribute_t)); |
2371 | ||
1149ba64 GM |
2372 | if (err != 0) { |
2373 | dsl_dir_rele(dd, FTAG); | |
9c43027b | 2374 | goto out; |
1149ba64 | 2375 | } |
13fe0198 MA |
2376 | |
2377 | /* | |
2378 | * Apply to self. | |
2379 | */ | |
2380 | err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds); | |
1149ba64 GM |
2381 | |
2382 | /* | |
2383 | * Note: we hold the dir while calling dsl_dataset_hold_obj() so | |
2384 | * that the dir will remain cached, and we won't have to re-instantiate | |
2385 | * it (which could be expensive due to finding its name via | |
2386 | * zap_value_search()). | |
2387 | */ | |
2388 | dsl_dir_rele(dd, FTAG); | |
13fe0198 | 2389 | if (err != 0) |
9c43027b AJ |
2390 | goto out; |
2391 | err = dcp->dc_func(dp, ds, dcp->dc_arg); | |
13fe0198 | 2392 | dsl_dataset_rele(ds, FTAG); |
9c43027b AJ |
2393 | |
2394 | out: | |
2395 | if (err != 0) { | |
2396 | mutex_enter(dcp->dc_error_lock); | |
2397 | /* only keep first error */ | |
2398 | if (*dcp->dc_error == 0) | |
2399 | *dcp->dc_error = err; | |
2400 | mutex_exit(dcp->dc_error_lock); | |
2401 | } | |
2402 | ||
1149ba64 GM |
2403 | if (dcp->dc_ddname != NULL) |
2404 | spa_strfree(dcp->dc_ddname); | |
9c43027b AJ |
2405 | kmem_free(dcp, sizeof (*dcp)); |
2406 | } | |
2407 | ||
2408 | static void | |
2409 | dmu_objset_find_dp_cb(void *arg) | |
2410 | { | |
2411 | dmu_objset_find_ctx_t *dcp = arg; | |
2412 | dsl_pool_t *dp = dcp->dc_dp; | |
2413 | ||
5e8cd5d1 AJ |
2414 | /* |
2415 | * We need to get a pool_config_lock here, as there are several | |
2416 | * asssert(pool_config_held) down the stack. Getting a lock via | |
2417 | * dsl_pool_config_enter is risky, as it might be stalled by a | |
2418 | * pending writer. This would deadlock, as the write lock can | |
2419 | * only be granted when our parent thread gives up the lock. | |
2420 | * The _prio interface gives us priority over a pending writer. | |
2421 | */ | |
2422 | dsl_pool_config_enter_prio(dp, FTAG); | |
9c43027b AJ |
2423 | |
2424 | dmu_objset_find_dp_impl(dcp); | |
2425 | ||
2426 | dsl_pool_config_exit(dp, FTAG); | |
2427 | } | |
2428 | ||
2429 | /* | |
2430 | * Find objsets under and including ddobj, call func(ds) on each. | |
2431 | * The order for the enumeration is completely undefined. | |
2432 | * func is called with dsl_pool_config held. | |
2433 | */ | |
2434 | int | |
2435 | dmu_objset_find_dp(dsl_pool_t *dp, uint64_t ddobj, | |
2436 | int func(dsl_pool_t *, dsl_dataset_t *, void *), void *arg, int flags) | |
2437 | { | |
2438 | int error = 0; | |
2439 | taskq_t *tq = NULL; | |
2440 | int ntasks; | |
2441 | dmu_objset_find_ctx_t *dcp; | |
2442 | kmutex_t err_lock; | |
2443 | ||
2444 | mutex_init(&err_lock, NULL, MUTEX_DEFAULT, NULL); | |
2445 | dcp = kmem_alloc(sizeof (*dcp), KM_SLEEP); | |
2446 | dcp->dc_tq = NULL; | |
2447 | dcp->dc_dp = dp; | |
2448 | dcp->dc_ddobj = ddobj; | |
1149ba64 | 2449 | dcp->dc_ddname = NULL; |
9c43027b AJ |
2450 | dcp->dc_func = func; |
2451 | dcp->dc_arg = arg; | |
2452 | dcp->dc_flags = flags; | |
2453 | dcp->dc_error_lock = &err_lock; | |
2454 | dcp->dc_error = &error; | |
2455 | ||
2456 | if ((flags & DS_FIND_SERIALIZE) || dsl_pool_config_held_writer(dp)) { | |
2457 | /* | |
2458 | * In case a write lock is held we can't make use of | |
2459 | * parallelism, as down the stack of the worker threads | |
2460 | * the lock is asserted via dsl_pool_config_held. | |
2461 | * In case of a read lock this is solved by getting a read | |
2462 | * lock in each worker thread, which isn't possible in case | |
2463 | * of a writer lock. So we fall back to the synchronous path | |
2464 | * here. | |
2465 | * In the future it might be possible to get some magic into | |
2466 | * dsl_pool_config_held in a way that it returns true for | |
2467 | * the worker threads so that a single lock held from this | |
2468 | * thread suffices. For now, stay single threaded. | |
2469 | */ | |
2470 | dmu_objset_find_dp_impl(dcp); | |
e5676636 | 2471 | mutex_destroy(&err_lock); |
9c43027b AJ |
2472 | |
2473 | return (error); | |
2474 | } | |
2475 | ||
2476 | ntasks = dmu_find_threads; | |
2477 | if (ntasks == 0) | |
2478 | ntasks = vdev_count_leaves(dp->dp_spa) * 4; | |
1229323d | 2479 | tq = taskq_create("dmu_objset_find", ntasks, maxclsyspri, ntasks, |
9c43027b AJ |
2480 | INT_MAX, 0); |
2481 | if (tq == NULL) { | |
2482 | kmem_free(dcp, sizeof (*dcp)); | |
e5676636 BB |
2483 | mutex_destroy(&err_lock); |
2484 | ||
9c43027b AJ |
2485 | return (SET_ERROR(ENOMEM)); |
2486 | } | |
2487 | dcp->dc_tq = tq; | |
2488 | ||
2489 | /* dcp will be freed by task */ | |
2490 | (void) taskq_dispatch(tq, dmu_objset_find_dp_cb, dcp, TQ_SLEEP); | |
2491 | ||
2492 | /* | |
2493 | * PORTING: this code relies on the property of taskq_wait to wait | |
2494 | * until no more tasks are queued and no more tasks are active. As | |
2495 | * we always queue new tasks from within other tasks, task_wait | |
2496 | * reliably waits for the full recursion to finish, even though we | |
2497 | * enqueue new tasks after taskq_wait has been called. | |
2498 | * On platforms other than illumos, taskq_wait may not have this | |
2499 | * property. | |
2500 | */ | |
2501 | taskq_wait(tq); | |
2502 | taskq_destroy(tq); | |
2503 | mutex_destroy(&err_lock); | |
2504 | ||
2505 | return (error); | |
b128c09f BB |
2506 | } |
2507 | ||
2508 | /* | |
13fe0198 MA |
2509 | * Find all objsets under name, and for each, call 'func(child_name, arg)'. |
2510 | * The dp_config_rwlock must not be held when this is called, and it | |
2511 | * will not be held when the callback is called. | |
2512 | * Therefore this function should only be used when the pool is not changing | |
2513 | * (e.g. in syncing context), or the callback can deal with the possible races. | |
b128c09f | 2514 | */ |
13fe0198 MA |
2515 | static int |
2516 | dmu_objset_find_impl(spa_t *spa, const char *name, | |
2517 | int func(const char *, void *), void *arg, int flags) | |
34dc7c2f BB |
2518 | { |
2519 | dsl_dir_t *dd; | |
13fe0198 | 2520 | dsl_pool_t *dp = spa_get_dsl(spa); |
b128c09f | 2521 | dsl_dataset_t *ds; |
34dc7c2f BB |
2522 | zap_cursor_t zc; |
2523 | zap_attribute_t *attr; | |
2524 | char *child; | |
b128c09f BB |
2525 | uint64_t thisobj; |
2526 | int err; | |
34dc7c2f | 2527 | |
13fe0198 MA |
2528 | dsl_pool_config_enter(dp, FTAG); |
2529 | ||
2530 | err = dsl_dir_hold(dp, name, FTAG, &dd, NULL); | |
2531 | if (err != 0) { | |
2532 | dsl_pool_config_exit(dp, FTAG); | |
34dc7c2f | 2533 | return (err); |
13fe0198 | 2534 | } |
34dc7c2f | 2535 | |
b128c09f BB |
2536 | /* Don't visit hidden ($MOS & $ORIGIN) objsets. */ |
2537 | if (dd->dd_myname[0] == '$') { | |
13fe0198 MA |
2538 | dsl_dir_rele(dd, FTAG); |
2539 | dsl_pool_config_exit(dp, FTAG); | |
b128c09f BB |
2540 | return (0); |
2541 | } | |
2542 | ||
d683ddbb | 2543 | thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj; |
79c76d5b | 2544 | attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); |
34dc7c2f BB |
2545 | |
2546 | /* | |
2547 | * Iterate over all children. | |
2548 | */ | |
2549 | if (flags & DS_FIND_CHILDREN) { | |
b128c09f | 2550 | for (zap_cursor_init(&zc, dp->dp_meta_objset, |
d683ddbb | 2551 | dsl_dir_phys(dd)->dd_child_dir_zapobj); |
34dc7c2f BB |
2552 | zap_cursor_retrieve(&zc, attr) == 0; |
2553 | (void) zap_cursor_advance(&zc)) { | |
13fe0198 MA |
2554 | ASSERT3U(attr->za_integer_length, ==, |
2555 | sizeof (uint64_t)); | |
2556 | ASSERT3U(attr->za_num_integers, ==, 1); | |
34dc7c2f | 2557 | |
428870ff | 2558 | child = kmem_asprintf("%s/%s", name, attr->za_name); |
13fe0198 MA |
2559 | dsl_pool_config_exit(dp, FTAG); |
2560 | err = dmu_objset_find_impl(spa, child, | |
2561 | func, arg, flags); | |
2562 | dsl_pool_config_enter(dp, FTAG); | |
428870ff | 2563 | strfree(child); |
13fe0198 | 2564 | if (err != 0) |
34dc7c2f BB |
2565 | break; |
2566 | } | |
2567 | zap_cursor_fini(&zc); | |
2568 | ||
13fe0198 MA |
2569 | if (err != 0) { |
2570 | dsl_dir_rele(dd, FTAG); | |
2571 | dsl_pool_config_exit(dp, FTAG); | |
34dc7c2f BB |
2572 | kmem_free(attr, sizeof (zap_attribute_t)); |
2573 | return (err); | |
2574 | } | |
2575 | } | |
2576 | ||
2577 | /* | |
2578 | * Iterate over all snapshots. | |
2579 | */ | |
b128c09f | 2580 | if (flags & DS_FIND_SNAPSHOTS) { |
b128c09f | 2581 | err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds); |
b128c09f BB |
2582 | |
2583 | if (err == 0) { | |
d683ddbb JG |
2584 | uint64_t snapobj; |
2585 | ||
2586 | snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj; | |
b128c09f BB |
2587 | dsl_dataset_rele(ds, FTAG); |
2588 | ||
2589 | for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj); | |
2590 | zap_cursor_retrieve(&zc, attr) == 0; | |
2591 | (void) zap_cursor_advance(&zc)) { | |
13fe0198 | 2592 | ASSERT3U(attr->za_integer_length, ==, |
b128c09f | 2593 | sizeof (uint64_t)); |
13fe0198 | 2594 | ASSERT3U(attr->za_num_integers, ==, 1); |
b128c09f | 2595 | |
428870ff BB |
2596 | child = kmem_asprintf("%s@%s", |
2597 | name, attr->za_name); | |
13fe0198 MA |
2598 | dsl_pool_config_exit(dp, FTAG); |
2599 | err = func(child, arg); | |
2600 | dsl_pool_config_enter(dp, FTAG); | |
428870ff | 2601 | strfree(child); |
13fe0198 | 2602 | if (err != 0) |
b128c09f BB |
2603 | break; |
2604 | } | |
2605 | zap_cursor_fini(&zc); | |
34dc7c2f | 2606 | } |
34dc7c2f BB |
2607 | } |
2608 | ||
13fe0198 | 2609 | dsl_dir_rele(dd, FTAG); |
34dc7c2f | 2610 | kmem_free(attr, sizeof (zap_attribute_t)); |
13fe0198 | 2611 | dsl_pool_config_exit(dp, FTAG); |
34dc7c2f | 2612 | |
13fe0198 | 2613 | if (err != 0) |
34dc7c2f BB |
2614 | return (err); |
2615 | ||
13fe0198 MA |
2616 | /* Apply to self. */ |
2617 | return (func(name, arg)); | |
34dc7c2f BB |
2618 | } |
2619 | ||
13fe0198 MA |
2620 | /* |
2621 | * See comment above dmu_objset_find_impl(). | |
2622 | */ | |
d164b209 | 2623 | int |
13fe0198 MA |
2624 | dmu_objset_find(char *name, int func(const char *, void *), void *arg, |
2625 | int flags) | |
d164b209 | 2626 | { |
13fe0198 MA |
2627 | spa_t *spa; |
2628 | int error; | |
d164b209 | 2629 | |
13fe0198 MA |
2630 | error = spa_open(name, &spa, FTAG); |
2631 | if (error != 0) | |
2632 | return (error); | |
2633 | error = dmu_objset_find_impl(spa, name, func, arg, flags); | |
2634 | spa_close(spa, FTAG); | |
2635 | return (error); | |
d164b209 BB |
2636 | } |
2637 | ||
34dc7c2f BB |
2638 | void |
2639 | dmu_objset_set_user(objset_t *os, void *user_ptr) | |
2640 | { | |
428870ff BB |
2641 | ASSERT(MUTEX_HELD(&os->os_user_ptr_lock)); |
2642 | os->os_user_ptr = user_ptr; | |
34dc7c2f BB |
2643 | } |
2644 | ||
2645 | void * | |
2646 | dmu_objset_get_user(objset_t *os) | |
2647 | { | |
428870ff BB |
2648 | ASSERT(MUTEX_HELD(&os->os_user_ptr_lock)); |
2649 | return (os->os_user_ptr); | |
34dc7c2f | 2650 | } |
c28b2279 | 2651 | |
13fe0198 MA |
2652 | /* |
2653 | * Determine name of filesystem, given name of snapshot. | |
eca7b760 | 2654 | * buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes |
13fe0198 MA |
2655 | */ |
2656 | int | |
2657 | dmu_fsname(const char *snapname, char *buf) | |
2658 | { | |
2659 | char *atp = strchr(snapname, '@'); | |
2660 | if (atp == NULL) | |
2e528b49 | 2661 | return (SET_ERROR(EINVAL)); |
eca7b760 | 2662 | if (atp - snapname >= ZFS_MAX_DATASET_NAME_LEN) |
2e528b49 | 2663 | return (SET_ERROR(ENAMETOOLONG)); |
13fe0198 MA |
2664 | (void) strlcpy(buf, snapname, atp - snapname + 1); |
2665 | return (0); | |
2666 | } | |
2667 | ||
3ec3bc21 BB |
2668 | /* |
2669 | * Call when we think we're going to write/free space in open context to track | |
2670 | * the amount of dirty data in the open txg, which is also the amount | |
2671 | * of memory that can not be evicted until this txg syncs. | |
2672 | */ | |
2673 | void | |
2674 | dmu_objset_willuse_space(objset_t *os, int64_t space, dmu_tx_t *tx) | |
2675 | { | |
2676 | dsl_dataset_t *ds = os->os_dsl_dataset; | |
2677 | int64_t aspace = spa_get_worst_case_asize(os->os_spa, space); | |
2678 | ||
2679 | if (ds != NULL) { | |
2680 | dsl_dir_willuse_space(ds->ds_dir, aspace, tx); | |
2681 | dsl_pool_dirty_space(dmu_tx_pool(tx), space, tx); | |
2682 | } | |
2683 | } | |
2684 | ||
c28b2279 | 2685 | #if defined(_KERNEL) && defined(HAVE_SPL) |
f0fd83be | 2686 | EXPORT_SYMBOL(dmu_objset_zil); |
c28b2279 | 2687 | EXPORT_SYMBOL(dmu_objset_pool); |
f0fd83be BB |
2688 | EXPORT_SYMBOL(dmu_objset_ds); |
2689 | EXPORT_SYMBOL(dmu_objset_type); | |
c28b2279 BB |
2690 | EXPORT_SYMBOL(dmu_objset_name); |
2691 | EXPORT_SYMBOL(dmu_objset_hold); | |
b5256303 | 2692 | EXPORT_SYMBOL(dmu_objset_hold_flags); |
c28b2279 BB |
2693 | EXPORT_SYMBOL(dmu_objset_own); |
2694 | EXPORT_SYMBOL(dmu_objset_rele); | |
b5256303 | 2695 | EXPORT_SYMBOL(dmu_objset_rele_flags); |
c28b2279 BB |
2696 | EXPORT_SYMBOL(dmu_objset_disown); |
2697 | EXPORT_SYMBOL(dmu_objset_from_ds); | |
2698 | EXPORT_SYMBOL(dmu_objset_create); | |
2699 | EXPORT_SYMBOL(dmu_objset_clone); | |
c28b2279 BB |
2700 | EXPORT_SYMBOL(dmu_objset_stats); |
2701 | EXPORT_SYMBOL(dmu_objset_fast_stat); | |
54a179e7 | 2702 | EXPORT_SYMBOL(dmu_objset_spa); |
c28b2279 BB |
2703 | EXPORT_SYMBOL(dmu_objset_space); |
2704 | EXPORT_SYMBOL(dmu_objset_fsid_guid); | |
2705 | EXPORT_SYMBOL(dmu_objset_find); | |
c28b2279 BB |
2706 | EXPORT_SYMBOL(dmu_objset_byteswap); |
2707 | EXPORT_SYMBOL(dmu_objset_evict_dbufs); | |
2708 | EXPORT_SYMBOL(dmu_objset_snap_cmtime); | |
50c957f7 | 2709 | EXPORT_SYMBOL(dmu_objset_dnodesize); |
c28b2279 BB |
2710 | |
2711 | EXPORT_SYMBOL(dmu_objset_sync); | |
2712 | EXPORT_SYMBOL(dmu_objset_is_dirty); | |
b5256303 | 2713 | EXPORT_SYMBOL(dmu_objset_create_impl_dnstats); |
c28b2279 BB |
2714 | EXPORT_SYMBOL(dmu_objset_create_impl); |
2715 | EXPORT_SYMBOL(dmu_objset_open_impl); | |
2716 | EXPORT_SYMBOL(dmu_objset_evict); | |
2717 | EXPORT_SYMBOL(dmu_objset_register_type); | |
2718 | EXPORT_SYMBOL(dmu_objset_do_userquota_updates); | |
2719 | EXPORT_SYMBOL(dmu_objset_userquota_get_ids); | |
2720 | EXPORT_SYMBOL(dmu_objset_userused_enabled); | |
2721 | EXPORT_SYMBOL(dmu_objset_userspace_upgrade); | |
2722 | EXPORT_SYMBOL(dmu_objset_userspace_present); | |
1de321e6 JX |
2723 | EXPORT_SYMBOL(dmu_objset_userobjused_enabled); |
2724 | EXPORT_SYMBOL(dmu_objset_userobjspace_upgrade); | |
126ae9f4 | 2725 | EXPORT_SYMBOL(dmu_objset_userobjspace_upgradable); |
1de321e6 | 2726 | EXPORT_SYMBOL(dmu_objset_userobjspace_present); |
c28b2279 | 2727 | #endif |