]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/dmu_objset.c
Merge branch 'lock-contention-on-arcs_mtx-final'
[mirror_zfs.git] / module / zfs / dmu_objset.c
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 */
21 /*
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
24 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
25 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
26 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27 * Copyright (c) 2015, STRATO AG, Inc. All rights reserved.
28 */
29
30 /* Portions Copyright 2010 Robert Milkowski */
31
32 #include <sys/cred.h>
33 #include <sys/zfs_context.h>
34 #include <sys/dmu_objset.h>
35 #include <sys/dsl_dir.h>
36 #include <sys/dsl_dataset.h>
37 #include <sys/dsl_prop.h>
38 #include <sys/dsl_pool.h>
39 #include <sys/dsl_synctask.h>
40 #include <sys/dsl_deleg.h>
41 #include <sys/dnode.h>
42 #include <sys/dbuf.h>
43 #include <sys/zvol.h>
44 #include <sys/dmu_tx.h>
45 #include <sys/zap.h>
46 #include <sys/zil.h>
47 #include <sys/dmu_impl.h>
48 #include <sys/zfs_ioctl.h>
49 #include <sys/sa.h>
50 #include <sys/zfs_onexit.h>
51 #include <sys/dsl_destroy.h>
52 #include <sys/vdev.h>
53
54 /*
55 * Needed to close a window in dnode_move() that allows the objset to be freed
56 * before it can be safely accessed.
57 */
58 krwlock_t os_lock;
59
60 /*
61 * Tunable to overwrite the maximum number of threads for the parallization
62 * of dmu_objset_find_dp, needed to speed up the import of pools with many
63 * datasets.
64 * Default is 4 times the number of leaf vdevs.
65 */
66 int dmu_find_threads = 0;
67
68 static void dmu_objset_find_dp_cb(void *arg);
69
70 void
71 dmu_objset_init(void)
72 {
73 rw_init(&os_lock, NULL, RW_DEFAULT, NULL);
74 }
75
76 void
77 dmu_objset_fini(void)
78 {
79 rw_destroy(&os_lock);
80 }
81
82 spa_t *
83 dmu_objset_spa(objset_t *os)
84 {
85 return (os->os_spa);
86 }
87
88 zilog_t *
89 dmu_objset_zil(objset_t *os)
90 {
91 return (os->os_zil);
92 }
93
94 dsl_pool_t *
95 dmu_objset_pool(objset_t *os)
96 {
97 dsl_dataset_t *ds;
98
99 if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir)
100 return (ds->ds_dir->dd_pool);
101 else
102 return (spa_get_dsl(os->os_spa));
103 }
104
105 dsl_dataset_t *
106 dmu_objset_ds(objset_t *os)
107 {
108 return (os->os_dsl_dataset);
109 }
110
111 dmu_objset_type_t
112 dmu_objset_type(objset_t *os)
113 {
114 return (os->os_phys->os_type);
115 }
116
117 void
118 dmu_objset_name(objset_t *os, char *buf)
119 {
120 dsl_dataset_name(os->os_dsl_dataset, buf);
121 }
122
123 uint64_t
124 dmu_objset_id(objset_t *os)
125 {
126 dsl_dataset_t *ds = os->os_dsl_dataset;
127
128 return (ds ? ds->ds_object : 0);
129 }
130
131 zfs_sync_type_t
132 dmu_objset_syncprop(objset_t *os)
133 {
134 return (os->os_sync);
135 }
136
137 zfs_logbias_op_t
138 dmu_objset_logbias(objset_t *os)
139 {
140 return (os->os_logbias);
141 }
142
143 static void
144 checksum_changed_cb(void *arg, uint64_t newval)
145 {
146 objset_t *os = arg;
147
148 /*
149 * Inheritance should have been done by now.
150 */
151 ASSERT(newval != ZIO_CHECKSUM_INHERIT);
152
153 os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
154 }
155
156 static void
157 compression_changed_cb(void *arg, uint64_t newval)
158 {
159 objset_t *os = arg;
160
161 /*
162 * Inheritance and range checking should have been done by now.
163 */
164 ASSERT(newval != ZIO_COMPRESS_INHERIT);
165
166 os->os_compress = zio_compress_select(newval, ZIO_COMPRESS_ON_VALUE);
167 }
168
169 static void
170 copies_changed_cb(void *arg, uint64_t newval)
171 {
172 objset_t *os = arg;
173
174 /*
175 * Inheritance and range checking should have been done by now.
176 */
177 ASSERT(newval > 0);
178 ASSERT(newval <= spa_max_replication(os->os_spa));
179
180 os->os_copies = newval;
181 }
182
183 static void
184 dedup_changed_cb(void *arg, uint64_t newval)
185 {
186 objset_t *os = arg;
187 spa_t *spa = os->os_spa;
188 enum zio_checksum checksum;
189
190 /*
191 * Inheritance should have been done by now.
192 */
193 ASSERT(newval != ZIO_CHECKSUM_INHERIT);
194
195 checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF);
196
197 os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK;
198 os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY);
199 }
200
201 static void
202 primary_cache_changed_cb(void *arg, uint64_t newval)
203 {
204 objset_t *os = arg;
205
206 /*
207 * Inheritance and range checking should have been done by now.
208 */
209 ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
210 newval == ZFS_CACHE_METADATA);
211
212 os->os_primary_cache = newval;
213 }
214
215 static void
216 secondary_cache_changed_cb(void *arg, uint64_t newval)
217 {
218 objset_t *os = arg;
219
220 /*
221 * Inheritance and range checking should have been done by now.
222 */
223 ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
224 newval == ZFS_CACHE_METADATA);
225
226 os->os_secondary_cache = newval;
227 }
228
229 static void
230 sync_changed_cb(void *arg, uint64_t newval)
231 {
232 objset_t *os = arg;
233
234 /*
235 * Inheritance and range checking should have been done by now.
236 */
237 ASSERT(newval == ZFS_SYNC_STANDARD || newval == ZFS_SYNC_ALWAYS ||
238 newval == ZFS_SYNC_DISABLED);
239
240 os->os_sync = newval;
241 if (os->os_zil)
242 zil_set_sync(os->os_zil, newval);
243 }
244
245 static void
246 redundant_metadata_changed_cb(void *arg, uint64_t newval)
247 {
248 objset_t *os = arg;
249
250 /*
251 * Inheritance and range checking should have been done by now.
252 */
253 ASSERT(newval == ZFS_REDUNDANT_METADATA_ALL ||
254 newval == ZFS_REDUNDANT_METADATA_MOST);
255
256 os->os_redundant_metadata = newval;
257 }
258
259 static void
260 logbias_changed_cb(void *arg, uint64_t newval)
261 {
262 objset_t *os = arg;
263
264 ASSERT(newval == ZFS_LOGBIAS_LATENCY ||
265 newval == ZFS_LOGBIAS_THROUGHPUT);
266 os->os_logbias = newval;
267 if (os->os_zil)
268 zil_set_logbias(os->os_zil, newval);
269 }
270
271 static void
272 recordsize_changed_cb(void *arg, uint64_t newval)
273 {
274 objset_t *os = arg;
275
276 os->os_recordsize = newval;
277 }
278
279 void
280 dmu_objset_byteswap(void *buf, size_t size)
281 {
282 objset_phys_t *osp = buf;
283
284 ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t));
285 dnode_byteswap(&osp->os_meta_dnode);
286 byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
287 osp->os_type = BSWAP_64(osp->os_type);
288 osp->os_flags = BSWAP_64(osp->os_flags);
289 if (size == sizeof (objset_phys_t)) {
290 dnode_byteswap(&osp->os_userused_dnode);
291 dnode_byteswap(&osp->os_groupused_dnode);
292 }
293 }
294
295 int
296 dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
297 objset_t **osp)
298 {
299 objset_t *os;
300 int i, err;
301
302 ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
303
304 os = kmem_zalloc(sizeof (objset_t), KM_SLEEP);
305 os->os_dsl_dataset = ds;
306 os->os_spa = spa;
307 os->os_rootbp = bp;
308 if (!BP_IS_HOLE(os->os_rootbp)) {
309 arc_flags_t aflags = ARC_FLAG_WAIT;
310 zbookmark_phys_t zb;
311 SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
312 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
313
314 if (DMU_OS_IS_L2CACHEABLE(os))
315 aflags |= ARC_FLAG_L2CACHE;
316 if (DMU_OS_IS_L2COMPRESSIBLE(os))
317 aflags |= ARC_FLAG_L2COMPRESS;
318
319 dprintf_bp(os->os_rootbp, "reading %s", "");
320 err = arc_read(NULL, spa, os->os_rootbp,
321 arc_getbuf_func, &os->os_phys_buf,
322 ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
323 if (err != 0) {
324 kmem_free(os, sizeof (objset_t));
325 /* convert checksum errors into IO errors */
326 if (err == ECKSUM)
327 err = SET_ERROR(EIO);
328 return (err);
329 }
330
331 /* Increase the blocksize if we are permitted. */
332 if (spa_version(spa) >= SPA_VERSION_USERSPACE &&
333 arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) {
334 arc_buf_t *buf = arc_buf_alloc(spa,
335 sizeof (objset_phys_t), &os->os_phys_buf,
336 ARC_BUFC_METADATA);
337 bzero(buf->b_data, sizeof (objset_phys_t));
338 bcopy(os->os_phys_buf->b_data, buf->b_data,
339 arc_buf_size(os->os_phys_buf));
340 (void) arc_buf_remove_ref(os->os_phys_buf,
341 &os->os_phys_buf);
342 os->os_phys_buf = buf;
343 }
344
345 os->os_phys = os->os_phys_buf->b_data;
346 os->os_flags = os->os_phys->os_flags;
347 } else {
348 int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
349 sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE;
350 os->os_phys_buf = arc_buf_alloc(spa, size,
351 &os->os_phys_buf, ARC_BUFC_METADATA);
352 os->os_phys = os->os_phys_buf->b_data;
353 bzero(os->os_phys, size);
354 }
355
356 /*
357 * Note: the changed_cb will be called once before the register
358 * func returns, thus changing the checksum/compression from the
359 * default (fletcher2/off). Snapshots don't need to know about
360 * checksum/compression/copies.
361 */
362 if (ds != NULL) {
363 err = dsl_prop_register(ds,
364 zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE),
365 primary_cache_changed_cb, os);
366 if (err == 0) {
367 err = dsl_prop_register(ds,
368 zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE),
369 secondary_cache_changed_cb, os);
370 }
371 if (!ds->ds_is_snapshot) {
372 if (err == 0) {
373 err = dsl_prop_register(ds,
374 zfs_prop_to_name(ZFS_PROP_CHECKSUM),
375 checksum_changed_cb, os);
376 }
377 if (err == 0) {
378 err = dsl_prop_register(ds,
379 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
380 compression_changed_cb, os);
381 }
382 if (err == 0) {
383 err = dsl_prop_register(ds,
384 zfs_prop_to_name(ZFS_PROP_COPIES),
385 copies_changed_cb, os);
386 }
387 if (err == 0) {
388 err = dsl_prop_register(ds,
389 zfs_prop_to_name(ZFS_PROP_DEDUP),
390 dedup_changed_cb, os);
391 }
392 if (err == 0) {
393 err = dsl_prop_register(ds,
394 zfs_prop_to_name(ZFS_PROP_LOGBIAS),
395 logbias_changed_cb, os);
396 }
397 if (err == 0) {
398 err = dsl_prop_register(ds,
399 zfs_prop_to_name(ZFS_PROP_SYNC),
400 sync_changed_cb, os);
401 }
402 if (err == 0) {
403 err = dsl_prop_register(ds,
404 zfs_prop_to_name(
405 ZFS_PROP_REDUNDANT_METADATA),
406 redundant_metadata_changed_cb, os);
407 }
408 if (err == 0) {
409 err = dsl_prop_register(ds,
410 zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
411 recordsize_changed_cb, os);
412 }
413 }
414 if (err != 0) {
415 VERIFY(arc_buf_remove_ref(os->os_phys_buf,
416 &os->os_phys_buf));
417 kmem_free(os, sizeof (objset_t));
418 return (err);
419 }
420 } else {
421 /* It's the meta-objset. */
422 os->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
423 os->os_compress = ZIO_COMPRESS_LZJB;
424 os->os_copies = spa_max_replication(spa);
425 os->os_dedup_checksum = ZIO_CHECKSUM_OFF;
426 os->os_dedup_verify = B_FALSE;
427 os->os_logbias = ZFS_LOGBIAS_LATENCY;
428 os->os_sync = ZFS_SYNC_STANDARD;
429 os->os_primary_cache = ZFS_CACHE_ALL;
430 os->os_secondary_cache = ZFS_CACHE_ALL;
431 }
432
433 if (ds == NULL || !ds->ds_is_snapshot)
434 os->os_zil_header = os->os_phys->os_zil_header;
435 os->os_zil = zil_alloc(os, &os->os_zil_header);
436
437 for (i = 0; i < TXG_SIZE; i++) {
438 list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t),
439 offsetof(dnode_t, dn_dirty_link[i]));
440 list_create(&os->os_free_dnodes[i], sizeof (dnode_t),
441 offsetof(dnode_t, dn_dirty_link[i]));
442 }
443 list_create(&os->os_dnodes, sizeof (dnode_t),
444 offsetof(dnode_t, dn_link));
445 list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
446 offsetof(dmu_buf_impl_t, db_link));
447
448 list_link_init(&os->os_evicting_node);
449
450 mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
451 mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
452 mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
453
454 dnode_special_open(os, &os->os_phys->os_meta_dnode,
455 DMU_META_DNODE_OBJECT, &os->os_meta_dnode);
456 if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) {
457 dnode_special_open(os, &os->os_phys->os_userused_dnode,
458 DMU_USERUSED_OBJECT, &os->os_userused_dnode);
459 dnode_special_open(os, &os->os_phys->os_groupused_dnode,
460 DMU_GROUPUSED_OBJECT, &os->os_groupused_dnode);
461 }
462
463 *osp = os;
464 return (0);
465 }
466
467 int
468 dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
469 {
470 int err = 0;
471
472 mutex_enter(&ds->ds_opening_lock);
473 if (ds->ds_objset == NULL) {
474 objset_t *os;
475 err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
476 ds, dsl_dataset_get_blkptr(ds), &os);
477
478 if (err == 0) {
479 mutex_enter(&ds->ds_lock);
480 ASSERT(ds->ds_objset == NULL);
481 ds->ds_objset = os;
482 mutex_exit(&ds->ds_lock);
483 }
484 }
485 *osp = ds->ds_objset;
486 mutex_exit(&ds->ds_opening_lock);
487 return (err);
488 }
489
490 /*
491 * Holds the pool while the objset is held. Therefore only one objset
492 * can be held at a time.
493 */
494 int
495 dmu_objset_hold(const char *name, void *tag, objset_t **osp)
496 {
497 dsl_pool_t *dp;
498 dsl_dataset_t *ds;
499 int err;
500
501 err = dsl_pool_hold(name, tag, &dp);
502 if (err != 0)
503 return (err);
504 err = dsl_dataset_hold(dp, name, tag, &ds);
505 if (err != 0) {
506 dsl_pool_rele(dp, tag);
507 return (err);
508 }
509
510 err = dmu_objset_from_ds(ds, osp);
511 if (err != 0) {
512 dsl_dataset_rele(ds, tag);
513 dsl_pool_rele(dp, tag);
514 }
515
516 return (err);
517 }
518
519 static int
520 dmu_objset_own_impl(dsl_dataset_t *ds, dmu_objset_type_t type,
521 boolean_t readonly, void *tag, objset_t **osp)
522 {
523 int err;
524
525 err = dmu_objset_from_ds(ds, osp);
526 if (err != 0) {
527 dsl_dataset_disown(ds, tag);
528 } else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) {
529 dsl_dataset_disown(ds, tag);
530 return (SET_ERROR(EINVAL));
531 } else if (!readonly && dsl_dataset_is_snapshot(ds)) {
532 dsl_dataset_disown(ds, tag);
533 return (SET_ERROR(EROFS));
534 }
535 return (err);
536 }
537
538 /*
539 * dsl_pool must not be held when this is called.
540 * Upon successful return, there will be a longhold on the dataset,
541 * and the dsl_pool will not be held.
542 */
543 int
544 dmu_objset_own(const char *name, dmu_objset_type_t type,
545 boolean_t readonly, void *tag, objset_t **osp)
546 {
547 dsl_pool_t *dp;
548 dsl_dataset_t *ds;
549 int err;
550
551 err = dsl_pool_hold(name, FTAG, &dp);
552 if (err != 0)
553 return (err);
554 err = dsl_dataset_own(dp, name, tag, &ds);
555 if (err != 0) {
556 dsl_pool_rele(dp, FTAG);
557 return (err);
558 }
559 err = dmu_objset_own_impl(ds, type, readonly, tag, osp);
560 dsl_pool_rele(dp, FTAG);
561
562 return (err);
563 }
564
565 int
566 dmu_objset_own_obj(dsl_pool_t *dp, uint64_t obj, dmu_objset_type_t type,
567 boolean_t readonly, void *tag, objset_t **osp)
568 {
569 dsl_dataset_t *ds;
570 int err;
571
572 err = dsl_dataset_own_obj(dp, obj, tag, &ds);
573 if (err != 0)
574 return (err);
575
576 return (dmu_objset_own_impl(ds, type, readonly, tag, osp));
577 }
578
579 void
580 dmu_objset_rele(objset_t *os, void *tag)
581 {
582 dsl_pool_t *dp = dmu_objset_pool(os);
583 dsl_dataset_rele(os->os_dsl_dataset, tag);
584 dsl_pool_rele(dp, tag);
585 }
586
587 /*
588 * When we are called, os MUST refer to an objset associated with a dataset
589 * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner
590 * == tag. We will then release and reacquire ownership of the dataset while
591 * holding the pool config_rwlock to avoid intervening namespace or ownership
592 * changes may occur.
593 *
594 * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to
595 * release the hold on its dataset and acquire a new one on the dataset of the
596 * same name so that it can be partially torn down and reconstructed.
597 */
598 void
599 dmu_objset_refresh_ownership(objset_t *os, void *tag)
600 {
601 dsl_pool_t *dp;
602 dsl_dataset_t *ds, *newds;
603 char name[MAXNAMELEN];
604
605 ds = os->os_dsl_dataset;
606 VERIFY3P(ds, !=, NULL);
607 VERIFY3P(ds->ds_owner, ==, tag);
608 VERIFY(dsl_dataset_long_held(ds));
609
610 dsl_dataset_name(ds, name);
611 dp = dmu_objset_pool(os);
612 dsl_pool_config_enter(dp, FTAG);
613 dmu_objset_disown(os, tag);
614 VERIFY0(dsl_dataset_own(dp, name, tag, &newds));
615 VERIFY3P(newds, ==, os->os_dsl_dataset);
616 dsl_pool_config_exit(dp, FTAG);
617 }
618
619 void
620 dmu_objset_disown(objset_t *os, void *tag)
621 {
622 dsl_dataset_disown(os->os_dsl_dataset, tag);
623 }
624
625 void
626 dmu_objset_evict_dbufs(objset_t *os)
627 {
628 dnode_t *dn_marker;
629 dnode_t *dn;
630
631 dn_marker = kmem_alloc(sizeof (dnode_t), KM_SLEEP);
632
633 mutex_enter(&os->os_lock);
634 dn = list_head(&os->os_dnodes);
635 while (dn != NULL) {
636 /*
637 * Skip dnodes without holds. We have to do this dance
638 * because dnode_add_ref() only works if there is already a
639 * hold. If the dnode has no holds, then it has no dbufs.
640 */
641 if (dnode_add_ref(dn, FTAG)) {
642 list_insert_after(&os->os_dnodes, dn, dn_marker);
643 mutex_exit(&os->os_lock);
644
645 dnode_evict_dbufs(dn);
646 dnode_rele(dn, FTAG);
647
648 mutex_enter(&os->os_lock);
649 dn = list_next(&os->os_dnodes, dn_marker);
650 list_remove(&os->os_dnodes, dn_marker);
651 } else {
652 dn = list_next(&os->os_dnodes, dn);
653 }
654 }
655 mutex_exit(&os->os_lock);
656
657 kmem_free(dn_marker, sizeof (dnode_t));
658
659 if (DMU_USERUSED_DNODE(os) != NULL) {
660 dnode_evict_dbufs(DMU_GROUPUSED_DNODE(os));
661 dnode_evict_dbufs(DMU_USERUSED_DNODE(os));
662 }
663 dnode_evict_dbufs(DMU_META_DNODE(os));
664 }
665
666 /*
667 * Objset eviction processing is split into into two pieces.
668 * The first marks the objset as evicting, evicts any dbufs that
669 * have a refcount of zero, and then queues up the objset for the
670 * second phase of eviction. Once os->os_dnodes has been cleared by
671 * dnode_buf_pageout()->dnode_destroy(), the second phase is executed.
672 * The second phase closes the special dnodes, dequeues the objset from
673 * the list of those undergoing eviction, and finally frees the objset.
674 *
675 * NOTE: Due to asynchronous eviction processing (invocation of
676 * dnode_buf_pageout()), it is possible for the meta dnode for the
677 * objset to have no holds even though os->os_dnodes is not empty.
678 */
679 void
680 dmu_objset_evict(objset_t *os)
681 {
682 int t;
683
684 dsl_dataset_t *ds = os->os_dsl_dataset;
685
686 for (t = 0; t < TXG_SIZE; t++)
687 ASSERT(!dmu_objset_is_dirty(os, t));
688
689 if (ds) {
690 if (!ds->ds_is_snapshot) {
691 VERIFY0(dsl_prop_unregister(ds,
692 zfs_prop_to_name(ZFS_PROP_CHECKSUM),
693 checksum_changed_cb, os));
694 VERIFY0(dsl_prop_unregister(ds,
695 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
696 compression_changed_cb, os));
697 VERIFY0(dsl_prop_unregister(ds,
698 zfs_prop_to_name(ZFS_PROP_COPIES),
699 copies_changed_cb, os));
700 VERIFY0(dsl_prop_unregister(ds,
701 zfs_prop_to_name(ZFS_PROP_DEDUP),
702 dedup_changed_cb, os));
703 VERIFY0(dsl_prop_unregister(ds,
704 zfs_prop_to_name(ZFS_PROP_LOGBIAS),
705 logbias_changed_cb, os));
706 VERIFY0(dsl_prop_unregister(ds,
707 zfs_prop_to_name(ZFS_PROP_SYNC),
708 sync_changed_cb, os));
709 VERIFY0(dsl_prop_unregister(ds,
710 zfs_prop_to_name(ZFS_PROP_REDUNDANT_METADATA),
711 redundant_metadata_changed_cb, os));
712 VERIFY0(dsl_prop_unregister(ds,
713 zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
714 recordsize_changed_cb, os));
715 }
716 VERIFY0(dsl_prop_unregister(ds,
717 zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE),
718 primary_cache_changed_cb, os));
719 VERIFY0(dsl_prop_unregister(ds,
720 zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE),
721 secondary_cache_changed_cb, os));
722 }
723
724 if (os->os_sa)
725 sa_tear_down(os);
726
727 os->os_evicting = B_TRUE;
728 dmu_objset_evict_dbufs(os);
729
730 mutex_enter(&os->os_lock);
731 spa_evicting_os_register(os->os_spa, os);
732 if (list_is_empty(&os->os_dnodes)) {
733 mutex_exit(&os->os_lock);
734 dmu_objset_evict_done(os);
735 } else {
736 mutex_exit(&os->os_lock);
737 }
738 }
739
740 void
741 dmu_objset_evict_done(objset_t *os)
742 {
743 ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
744
745 dnode_special_close(&os->os_meta_dnode);
746 if (DMU_USERUSED_DNODE(os)) {
747 dnode_special_close(&os->os_userused_dnode);
748 dnode_special_close(&os->os_groupused_dnode);
749 }
750 zil_free(os->os_zil);
751
752 VERIFY(arc_buf_remove_ref(os->os_phys_buf, &os->os_phys_buf));
753
754 /*
755 * This is a barrier to prevent the objset from going away in
756 * dnode_move() until we can safely ensure that the objset is still in
757 * use. We consider the objset valid before the barrier and invalid
758 * after the barrier.
759 */
760 rw_enter(&os_lock, RW_READER);
761 rw_exit(&os_lock);
762
763 mutex_destroy(&os->os_lock);
764 mutex_destroy(&os->os_obj_lock);
765 mutex_destroy(&os->os_user_ptr_lock);
766 spa_evicting_os_deregister(os->os_spa, os);
767 kmem_free(os, sizeof (objset_t));
768 }
769
770 timestruc_t
771 dmu_objset_snap_cmtime(objset_t *os)
772 {
773 return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
774 }
775
776 /* called from dsl for meta-objset */
777 objset_t *
778 dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
779 dmu_objset_type_t type, dmu_tx_t *tx)
780 {
781 objset_t *os;
782 dnode_t *mdn;
783
784 ASSERT(dmu_tx_is_syncing(tx));
785
786 if (ds != NULL)
787 VERIFY0(dmu_objset_from_ds(ds, &os));
788 else
789 VERIFY0(dmu_objset_open_impl(spa, NULL, bp, &os));
790
791 mdn = DMU_META_DNODE(os);
792
793 dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
794 DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
795
796 /*
797 * We don't want to have to increase the meta-dnode's nlevels
798 * later, because then we could do it in quescing context while
799 * we are also accessing it in open context.
800 *
801 * This precaution is not necessary for the MOS (ds == NULL),
802 * because the MOS is only updated in syncing context.
803 * This is most fortunate: the MOS is the only objset that
804 * needs to be synced multiple times as spa_sync() iterates
805 * to convergence, so minimizing its dn_nlevels matters.
806 */
807 if (ds != NULL) {
808 int levels = 1;
809
810 /*
811 * Determine the number of levels necessary for the meta-dnode
812 * to contain DN_MAX_OBJECT dnodes.
813 */
814 while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
815 (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
816 DN_MAX_OBJECT * sizeof (dnode_phys_t))
817 levels++;
818
819 mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
820 mdn->dn_nlevels = levels;
821 }
822
823 ASSERT(type != DMU_OST_NONE);
824 ASSERT(type != DMU_OST_ANY);
825 ASSERT(type < DMU_OST_NUMTYPES);
826 os->os_phys->os_type = type;
827 if (dmu_objset_userused_enabled(os)) {
828 os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
829 os->os_flags = os->os_phys->os_flags;
830 }
831
832 dsl_dataset_dirty(ds, tx);
833
834 return (os);
835 }
836
837 typedef struct dmu_objset_create_arg {
838 const char *doca_name;
839 cred_t *doca_cred;
840 void (*doca_userfunc)(objset_t *os, void *arg,
841 cred_t *cr, dmu_tx_t *tx);
842 void *doca_userarg;
843 dmu_objset_type_t doca_type;
844 uint64_t doca_flags;
845 } dmu_objset_create_arg_t;
846
847 /*ARGSUSED*/
848 static int
849 dmu_objset_create_check(void *arg, dmu_tx_t *tx)
850 {
851 dmu_objset_create_arg_t *doca = arg;
852 dsl_pool_t *dp = dmu_tx_pool(tx);
853 dsl_dir_t *pdd;
854 const char *tail;
855 int error;
856
857 if (strchr(doca->doca_name, '@') != NULL)
858 return (SET_ERROR(EINVAL));
859
860 error = dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail);
861 if (error != 0)
862 return (error);
863 if (tail == NULL) {
864 dsl_dir_rele(pdd, FTAG);
865 return (SET_ERROR(EEXIST));
866 }
867 error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
868 doca->doca_cred);
869 dsl_dir_rele(pdd, FTAG);
870
871 return (error);
872 }
873
874 static void
875 dmu_objset_create_sync(void *arg, dmu_tx_t *tx)
876 {
877 dmu_objset_create_arg_t *doca = arg;
878 dsl_pool_t *dp = dmu_tx_pool(tx);
879 dsl_dir_t *pdd;
880 const char *tail;
881 dsl_dataset_t *ds;
882 uint64_t obj;
883 blkptr_t *bp;
884 objset_t *os;
885
886 VERIFY0(dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail));
887
888 obj = dsl_dataset_create_sync(pdd, tail, NULL, doca->doca_flags,
889 doca->doca_cred, tx);
890
891 VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
892 bp = dsl_dataset_get_blkptr(ds);
893 os = dmu_objset_create_impl(pdd->dd_pool->dp_spa,
894 ds, bp, doca->doca_type, tx);
895
896 if (doca->doca_userfunc != NULL) {
897 doca->doca_userfunc(os, doca->doca_userarg,
898 doca->doca_cred, tx);
899 }
900
901 spa_history_log_internal_ds(ds, "create", tx, "");
902 dsl_dataset_rele(ds, FTAG);
903 dsl_dir_rele(pdd, FTAG);
904 }
905
906 int
907 dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
908 void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
909 {
910 dmu_objset_create_arg_t doca;
911
912 doca.doca_name = name;
913 doca.doca_cred = CRED();
914 doca.doca_flags = flags;
915 doca.doca_userfunc = func;
916 doca.doca_userarg = arg;
917 doca.doca_type = type;
918
919 return (dsl_sync_task(name,
920 dmu_objset_create_check, dmu_objset_create_sync, &doca,
921 5, ZFS_SPACE_CHECK_NORMAL));
922 }
923
924 typedef struct dmu_objset_clone_arg {
925 const char *doca_clone;
926 const char *doca_origin;
927 cred_t *doca_cred;
928 } dmu_objset_clone_arg_t;
929
930 /*ARGSUSED*/
931 static int
932 dmu_objset_clone_check(void *arg, dmu_tx_t *tx)
933 {
934 dmu_objset_clone_arg_t *doca = arg;
935 dsl_dir_t *pdd;
936 const char *tail;
937 int error;
938 dsl_dataset_t *origin;
939 dsl_pool_t *dp = dmu_tx_pool(tx);
940
941 if (strchr(doca->doca_clone, '@') != NULL)
942 return (SET_ERROR(EINVAL));
943
944 error = dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail);
945 if (error != 0)
946 return (error);
947 if (tail == NULL) {
948 dsl_dir_rele(pdd, FTAG);
949 return (SET_ERROR(EEXIST));
950 }
951 /* You can't clone across pools. */
952 if (pdd->dd_pool != dp) {
953 dsl_dir_rele(pdd, FTAG);
954 return (SET_ERROR(EXDEV));
955 }
956 error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
957 doca->doca_cred);
958 if (error != 0) {
959 dsl_dir_rele(pdd, FTAG);
960 return (SET_ERROR(EDQUOT));
961 }
962 dsl_dir_rele(pdd, FTAG);
963
964 error = dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin);
965 if (error != 0)
966 return (error);
967
968 /* You can't clone across pools. */
969 if (origin->ds_dir->dd_pool != dp) {
970 dsl_dataset_rele(origin, FTAG);
971 return (SET_ERROR(EXDEV));
972 }
973
974 /* You can only clone snapshots, not the head datasets. */
975 if (!origin->ds_is_snapshot) {
976 dsl_dataset_rele(origin, FTAG);
977 return (SET_ERROR(EINVAL));
978 }
979 dsl_dataset_rele(origin, FTAG);
980
981 return (0);
982 }
983
984 static void
985 dmu_objset_clone_sync(void *arg, dmu_tx_t *tx)
986 {
987 dmu_objset_clone_arg_t *doca = arg;
988 dsl_pool_t *dp = dmu_tx_pool(tx);
989 dsl_dir_t *pdd;
990 const char *tail;
991 dsl_dataset_t *origin, *ds;
992 uint64_t obj;
993 char namebuf[MAXNAMELEN];
994
995 VERIFY0(dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail));
996 VERIFY0(dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin));
997
998 obj = dsl_dataset_create_sync(pdd, tail, origin, 0,
999 doca->doca_cred, tx);
1000
1001 VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
1002 dsl_dataset_name(origin, namebuf);
1003 spa_history_log_internal_ds(ds, "clone", tx,
1004 "origin=%s (%llu)", namebuf, origin->ds_object);
1005 dsl_dataset_rele(ds, FTAG);
1006 dsl_dataset_rele(origin, FTAG);
1007 dsl_dir_rele(pdd, FTAG);
1008 }
1009
1010 int
1011 dmu_objset_clone(const char *clone, const char *origin)
1012 {
1013 dmu_objset_clone_arg_t doca;
1014
1015 doca.doca_clone = clone;
1016 doca.doca_origin = origin;
1017 doca.doca_cred = CRED();
1018
1019 return (dsl_sync_task(clone,
1020 dmu_objset_clone_check, dmu_objset_clone_sync, &doca,
1021 5, ZFS_SPACE_CHECK_NORMAL));
1022 }
1023
1024 int
1025 dmu_objset_snapshot_one(const char *fsname, const char *snapname)
1026 {
1027 int err;
1028 char *longsnap = kmem_asprintf("%s@%s", fsname, snapname);
1029 nvlist_t *snaps = fnvlist_alloc();
1030
1031 fnvlist_add_boolean(snaps, longsnap);
1032 strfree(longsnap);
1033 err = dsl_dataset_snapshot(snaps, NULL, NULL);
1034 fnvlist_free(snaps);
1035 return (err);
1036 }
1037
1038 static void
1039 dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx)
1040 {
1041 dnode_t *dn;
1042
1043 while ((dn = list_head(list))) {
1044 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
1045 ASSERT(dn->dn_dbuf->db_data_pending);
1046 /*
1047 * Initialize dn_zio outside dnode_sync() because the
1048 * meta-dnode needs to set it ouside dnode_sync().
1049 */
1050 dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
1051 ASSERT(dn->dn_zio);
1052
1053 ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
1054 list_remove(list, dn);
1055
1056 if (newlist) {
1057 (void) dnode_add_ref(dn, newlist);
1058 list_insert_tail(newlist, dn);
1059 }
1060
1061 dnode_sync(dn, tx);
1062 }
1063 }
1064
1065 /* ARGSUSED */
1066 static void
1067 dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
1068 {
1069 int i;
1070
1071 blkptr_t *bp = zio->io_bp;
1072 objset_t *os = arg;
1073 dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
1074
1075 ASSERT(!BP_IS_EMBEDDED(bp));
1076 ASSERT3P(bp, ==, os->os_rootbp);
1077 ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_OBJSET);
1078 ASSERT0(BP_GET_LEVEL(bp));
1079
1080 /*
1081 * Update rootbp fill count: it should be the number of objects
1082 * allocated in the object set (not counting the "special"
1083 * objects that are stored in the objset_phys_t -- the meta
1084 * dnode and user/group accounting objects).
1085 */
1086 bp->blk_fill = 0;
1087 for (i = 0; i < dnp->dn_nblkptr; i++)
1088 bp->blk_fill += BP_GET_FILL(&dnp->dn_blkptr[i]);
1089 }
1090
1091 /* ARGSUSED */
1092 static void
1093 dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
1094 {
1095 blkptr_t *bp = zio->io_bp;
1096 blkptr_t *bp_orig = &zio->io_bp_orig;
1097 objset_t *os = arg;
1098
1099 if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
1100 ASSERT(BP_EQUAL(bp, bp_orig));
1101 } else {
1102 dsl_dataset_t *ds = os->os_dsl_dataset;
1103 dmu_tx_t *tx = os->os_synctx;
1104
1105 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
1106 dsl_dataset_block_born(ds, bp, tx);
1107 }
1108 }
1109
1110 /* called from dsl */
1111 void
1112 dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
1113 {
1114 int txgoff;
1115 zbookmark_phys_t zb;
1116 zio_prop_t zp;
1117 zio_t *zio;
1118 list_t *list;
1119 list_t *newlist = NULL;
1120 dbuf_dirty_record_t *dr;
1121
1122 dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
1123
1124 ASSERT(dmu_tx_is_syncing(tx));
1125 /* XXX the write_done callback should really give us the tx... */
1126 os->os_synctx = tx;
1127
1128 if (os->os_dsl_dataset == NULL) {
1129 /*
1130 * This is the MOS. If we have upgraded,
1131 * spa_max_replication() could change, so reset
1132 * os_copies here.
1133 */
1134 os->os_copies = spa_max_replication(os->os_spa);
1135 }
1136
1137 /*
1138 * Create the root block IO
1139 */
1140 SET_BOOKMARK(&zb, os->os_dsl_dataset ?
1141 os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
1142 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
1143 arc_release(os->os_phys_buf, &os->os_phys_buf);
1144
1145 dmu_write_policy(os, NULL, 0, 0, &zp);
1146
1147 zio = arc_write(pio, os->os_spa, tx->tx_txg,
1148 os->os_rootbp, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os),
1149 DMU_OS_IS_L2COMPRESSIBLE(os), &zp, dmu_objset_write_ready,
1150 NULL, dmu_objset_write_done, os, ZIO_PRIORITY_ASYNC_WRITE,
1151 ZIO_FLAG_MUSTSUCCEED, &zb);
1152
1153 /*
1154 * Sync special dnodes - the parent IO for the sync is the root block
1155 */
1156 DMU_META_DNODE(os)->dn_zio = zio;
1157 dnode_sync(DMU_META_DNODE(os), tx);
1158
1159 os->os_phys->os_flags = os->os_flags;
1160
1161 if (DMU_USERUSED_DNODE(os) &&
1162 DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
1163 DMU_USERUSED_DNODE(os)->dn_zio = zio;
1164 dnode_sync(DMU_USERUSED_DNODE(os), tx);
1165 DMU_GROUPUSED_DNODE(os)->dn_zio = zio;
1166 dnode_sync(DMU_GROUPUSED_DNODE(os), tx);
1167 }
1168
1169 txgoff = tx->tx_txg & TXG_MASK;
1170
1171 if (dmu_objset_userused_enabled(os)) {
1172 newlist = &os->os_synced_dnodes;
1173 /*
1174 * We must create the list here because it uses the
1175 * dn_dirty_link[] of this txg.
1176 */
1177 list_create(newlist, sizeof (dnode_t),
1178 offsetof(dnode_t, dn_dirty_link[txgoff]));
1179 }
1180
1181 dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx);
1182 dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx);
1183
1184 list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff];
1185 while ((dr = list_head(list))) {
1186 ASSERT0(dr->dr_dbuf->db_level);
1187 list_remove(list, dr);
1188 if (dr->dr_zio)
1189 zio_nowait(dr->dr_zio);
1190 }
1191 /*
1192 * Free intent log blocks up to this tx.
1193 */
1194 zil_sync(os->os_zil, tx);
1195 os->os_phys->os_zil_header = os->os_zil_header;
1196 zio_nowait(zio);
1197 }
1198
1199 boolean_t
1200 dmu_objset_is_dirty(objset_t *os, uint64_t txg)
1201 {
1202 return (!list_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]) ||
1203 !list_is_empty(&os->os_free_dnodes[txg & TXG_MASK]));
1204 }
1205
1206 static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
1207
1208 void
1209 dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
1210 {
1211 used_cbs[ost] = cb;
1212 }
1213
1214 boolean_t
1215 dmu_objset_userused_enabled(objset_t *os)
1216 {
1217 return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
1218 used_cbs[os->os_phys->os_type] != NULL &&
1219 DMU_USERUSED_DNODE(os) != NULL);
1220 }
1221
1222 static void
1223 do_userquota_update(objset_t *os, uint64_t used, uint64_t flags,
1224 uint64_t user, uint64_t group, boolean_t subtract, dmu_tx_t *tx)
1225 {
1226 if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) {
1227 int64_t delta = DNODE_SIZE + used;
1228 if (subtract)
1229 delta = -delta;
1230 VERIFY3U(0, ==, zap_increment_int(os, DMU_USERUSED_OBJECT,
1231 user, delta, tx));
1232 VERIFY3U(0, ==, zap_increment_int(os, DMU_GROUPUSED_OBJECT,
1233 group, delta, tx));
1234 }
1235 }
1236
1237 void
1238 dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
1239 {
1240 dnode_t *dn;
1241 list_t *list = &os->os_synced_dnodes;
1242
1243 ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os));
1244
1245 while ((dn = list_head(list))) {
1246 int flags;
1247 ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
1248 ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
1249 dn->dn_phys->dn_flags &
1250 DNODE_FLAG_USERUSED_ACCOUNTED);
1251
1252 /* Allocate the user/groupused objects if necessary. */
1253 if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
1254 VERIFY(0 == zap_create_claim(os,
1255 DMU_USERUSED_OBJECT,
1256 DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1257 VERIFY(0 == zap_create_claim(os,
1258 DMU_GROUPUSED_OBJECT,
1259 DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1260 }
1261
1262 /*
1263 * We intentionally modify the zap object even if the
1264 * net delta is zero. Otherwise
1265 * the block of the zap obj could be shared between
1266 * datasets but need to be different between them after
1267 * a bprewrite.
1268 */
1269
1270 flags = dn->dn_id_flags;
1271 ASSERT(flags);
1272 if (flags & DN_ID_OLD_EXIST) {
1273 do_userquota_update(os, dn->dn_oldused, dn->dn_oldflags,
1274 dn->dn_olduid, dn->dn_oldgid, B_TRUE, tx);
1275 }
1276 if (flags & DN_ID_NEW_EXIST) {
1277 do_userquota_update(os, DN_USED_BYTES(dn->dn_phys),
1278 dn->dn_phys->dn_flags, dn->dn_newuid,
1279 dn->dn_newgid, B_FALSE, tx);
1280 }
1281
1282 mutex_enter(&dn->dn_mtx);
1283 dn->dn_oldused = 0;
1284 dn->dn_oldflags = 0;
1285 if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
1286 dn->dn_olduid = dn->dn_newuid;
1287 dn->dn_oldgid = dn->dn_newgid;
1288 dn->dn_id_flags |= DN_ID_OLD_EXIST;
1289 if (dn->dn_bonuslen == 0)
1290 dn->dn_id_flags |= DN_ID_CHKED_SPILL;
1291 else
1292 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1293 }
1294 dn->dn_id_flags &= ~(DN_ID_NEW_EXIST);
1295 mutex_exit(&dn->dn_mtx);
1296
1297 list_remove(list, dn);
1298 dnode_rele(dn, list);
1299 }
1300 }
1301
1302 /*
1303 * Returns a pointer to data to find uid/gid from
1304 *
1305 * If a dirty record for transaction group that is syncing can't
1306 * be found then NULL is returned. In the NULL case it is assumed
1307 * the uid/gid aren't changing.
1308 */
1309 static void *
1310 dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx)
1311 {
1312 dbuf_dirty_record_t *dr, **drp;
1313 void *data;
1314
1315 if (db->db_dirtycnt == 0)
1316 return (db->db.db_data); /* Nothing is changing */
1317
1318 for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
1319 if (dr->dr_txg == tx->tx_txg)
1320 break;
1321
1322 if (dr == NULL) {
1323 data = NULL;
1324 } else {
1325 dnode_t *dn;
1326
1327 DB_DNODE_ENTER(dr->dr_dbuf);
1328 dn = DB_DNODE(dr->dr_dbuf);
1329
1330 if (dn->dn_bonuslen == 0 &&
1331 dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID)
1332 data = dr->dt.dl.dr_data->b_data;
1333 else
1334 data = dr->dt.dl.dr_data;
1335
1336 DB_DNODE_EXIT(dr->dr_dbuf);
1337 }
1338
1339 return (data);
1340 }
1341
1342 void
1343 dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
1344 {
1345 objset_t *os = dn->dn_objset;
1346 void *data = NULL;
1347 dmu_buf_impl_t *db = NULL;
1348 uint64_t *user = NULL;
1349 uint64_t *group = NULL;
1350 int flags = dn->dn_id_flags;
1351 int error;
1352 boolean_t have_spill = B_FALSE;
1353 boolean_t have_bonus = B_FALSE;
1354
1355 if (!dmu_objset_userused_enabled(dn->dn_objset))
1356 return;
1357
1358 if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST|
1359 DN_ID_CHKED_SPILL)))
1360 return;
1361
1362 if (before && dn->dn_bonuslen != 0)
1363 data = DN_BONUS(dn->dn_phys);
1364 else if (!before && dn->dn_bonuslen != 0) {
1365 db = dn->dn_bonus;
1366 if (db != NULL) {
1367 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
1368 have_bonus = dbuf_try_add_ref((dmu_buf_t *)db,
1369 dn->dn_objset, dn->dn_object,
1370 DMU_BONUS_BLKID, FTAG);
1371
1372 /*
1373 * The hold will fail if the buffer is
1374 * being evicted due to unlink, in which
1375 * case nothing needs to be done.
1376 */
1377 if (!have_bonus)
1378 return;
1379 }
1380 mutex_enter(&db->db_mtx);
1381 data = dmu_objset_userquota_find_data(db, tx);
1382 } else {
1383 data = DN_BONUS(dn->dn_phys);
1384 }
1385 } else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) {
1386 int rf = 0;
1387
1388 if (RW_WRITE_HELD(&dn->dn_struct_rwlock))
1389 rf |= DB_RF_HAVESTRUCT;
1390 error = dmu_spill_hold_by_dnode(dn,
1391 rf | DB_RF_MUST_SUCCEED,
1392 FTAG, (dmu_buf_t **)&db);
1393 ASSERT(error == 0);
1394 mutex_enter(&db->db_mtx);
1395 data = (before) ? db->db.db_data :
1396 dmu_objset_userquota_find_data(db, tx);
1397 have_spill = B_TRUE;
1398 } else {
1399 mutex_enter(&dn->dn_mtx);
1400 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1401 mutex_exit(&dn->dn_mtx);
1402 return;
1403 }
1404
1405 if (before) {
1406 ASSERT(data);
1407 user = &dn->dn_olduid;
1408 group = &dn->dn_oldgid;
1409 } else if (data) {
1410 user = &dn->dn_newuid;
1411 group = &dn->dn_newgid;
1412 }
1413
1414 /*
1415 * Must always call the callback in case the object
1416 * type has changed and that type isn't an object type to track
1417 */
1418 error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data,
1419 user, group);
1420
1421 /*
1422 * Preserve existing uid/gid when the callback can't determine
1423 * what the new uid/gid are and the callback returned EEXIST.
1424 * The EEXIST error tells us to just use the existing uid/gid.
1425 * If we don't know what the old values are then just assign
1426 * them to 0, since that is a new file being created.
1427 */
1428 if (!before && data == NULL && error == EEXIST) {
1429 if (flags & DN_ID_OLD_EXIST) {
1430 dn->dn_newuid = dn->dn_olduid;
1431 dn->dn_newgid = dn->dn_oldgid;
1432 } else {
1433 dn->dn_newuid = 0;
1434 dn->dn_newgid = 0;
1435 }
1436 error = 0;
1437 }
1438
1439 if (db)
1440 mutex_exit(&db->db_mtx);
1441
1442 mutex_enter(&dn->dn_mtx);
1443 if (error == 0 && before)
1444 dn->dn_id_flags |= DN_ID_OLD_EXIST;
1445 if (error == 0 && !before)
1446 dn->dn_id_flags |= DN_ID_NEW_EXIST;
1447
1448 if (have_spill) {
1449 dn->dn_id_flags |= DN_ID_CHKED_SPILL;
1450 } else {
1451 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1452 }
1453 mutex_exit(&dn->dn_mtx);
1454 if (have_spill || have_bonus)
1455 dmu_buf_rele((dmu_buf_t *)db, FTAG);
1456 }
1457
1458 boolean_t
1459 dmu_objset_userspace_present(objset_t *os)
1460 {
1461 return (os->os_phys->os_flags &
1462 OBJSET_FLAG_USERACCOUNTING_COMPLETE);
1463 }
1464
1465 int
1466 dmu_objset_userspace_upgrade(objset_t *os)
1467 {
1468 uint64_t obj;
1469 int err = 0;
1470
1471 if (dmu_objset_userspace_present(os))
1472 return (0);
1473 if (!dmu_objset_userused_enabled(os))
1474 return (SET_ERROR(ENOTSUP));
1475 if (dmu_objset_is_snapshot(os))
1476 return (SET_ERROR(EINVAL));
1477
1478 /*
1479 * We simply need to mark every object dirty, so that it will be
1480 * synced out and now accounted. If this is called
1481 * concurrently, or if we already did some work before crashing,
1482 * that's fine, since we track each object's accounted state
1483 * independently.
1484 */
1485
1486 for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
1487 dmu_tx_t *tx;
1488 dmu_buf_t *db;
1489 int objerr;
1490
1491 if (issig(JUSTLOOKING) && issig(FORREAL))
1492 return (SET_ERROR(EINTR));
1493
1494 objerr = dmu_bonus_hold(os, obj, FTAG, &db);
1495 if (objerr != 0)
1496 continue;
1497 tx = dmu_tx_create(os);
1498 dmu_tx_hold_bonus(tx, obj);
1499 objerr = dmu_tx_assign(tx, TXG_WAIT);
1500 if (objerr != 0) {
1501 dmu_tx_abort(tx);
1502 continue;
1503 }
1504 dmu_buf_will_dirty(db, tx);
1505 dmu_buf_rele(db, FTAG);
1506 dmu_tx_commit(tx);
1507 }
1508
1509 os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
1510 txg_wait_synced(dmu_objset_pool(os), 0);
1511 return (0);
1512 }
1513
1514 void
1515 dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
1516 uint64_t *usedobjsp, uint64_t *availobjsp)
1517 {
1518 dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
1519 usedobjsp, availobjsp);
1520 }
1521
1522 uint64_t
1523 dmu_objset_fsid_guid(objset_t *os)
1524 {
1525 return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
1526 }
1527
1528 void
1529 dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1530 {
1531 stat->dds_type = os->os_phys->os_type;
1532 if (os->os_dsl_dataset)
1533 dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
1534 }
1535
1536 void
1537 dmu_objset_stats(objset_t *os, nvlist_t *nv)
1538 {
1539 ASSERT(os->os_dsl_dataset ||
1540 os->os_phys->os_type == DMU_OST_META);
1541
1542 if (os->os_dsl_dataset != NULL)
1543 dsl_dataset_stats(os->os_dsl_dataset, nv);
1544
1545 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
1546 os->os_phys->os_type);
1547 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
1548 dmu_objset_userspace_present(os));
1549 }
1550
1551 int
1552 dmu_objset_is_snapshot(objset_t *os)
1553 {
1554 if (os->os_dsl_dataset != NULL)
1555 return (os->os_dsl_dataset->ds_is_snapshot);
1556 else
1557 return (B_FALSE);
1558 }
1559
1560 int
1561 dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
1562 boolean_t *conflict)
1563 {
1564 dsl_dataset_t *ds = os->os_dsl_dataset;
1565 uint64_t ignored;
1566
1567 if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
1568 return (SET_ERROR(ENOENT));
1569
1570 return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
1571 dsl_dataset_phys(ds)->ds_snapnames_zapobj, name, 8, 1, &ignored,
1572 MT_FIRST, real, maxlen, conflict));
1573 }
1574
1575 int
1576 dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
1577 uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1578 {
1579 dsl_dataset_t *ds = os->os_dsl_dataset;
1580 zap_cursor_t cursor;
1581 zap_attribute_t attr;
1582
1583 ASSERT(dsl_pool_config_held(dmu_objset_pool(os)));
1584
1585 if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
1586 return (SET_ERROR(ENOENT));
1587
1588 zap_cursor_init_serialized(&cursor,
1589 ds->ds_dir->dd_pool->dp_meta_objset,
1590 dsl_dataset_phys(ds)->ds_snapnames_zapobj, *offp);
1591
1592 if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1593 zap_cursor_fini(&cursor);
1594 return (SET_ERROR(ENOENT));
1595 }
1596
1597 if (strlen(attr.za_name) + 1 > namelen) {
1598 zap_cursor_fini(&cursor);
1599 return (SET_ERROR(ENAMETOOLONG));
1600 }
1601
1602 (void) strcpy(name, attr.za_name);
1603 if (idp)
1604 *idp = attr.za_first_integer;
1605 if (case_conflict)
1606 *case_conflict = attr.za_normalization_conflict;
1607 zap_cursor_advance(&cursor);
1608 *offp = zap_cursor_serialize(&cursor);
1609 zap_cursor_fini(&cursor);
1610
1611 return (0);
1612 }
1613
1614 int
1615 dmu_snapshot_lookup(objset_t *os, const char *name, uint64_t *value)
1616 {
1617 return (dsl_dataset_snap_lookup(os->os_dsl_dataset, name, value));
1618 }
1619
1620 int
1621 dmu_dir_list_next(objset_t *os, int namelen, char *name,
1622 uint64_t *idp, uint64_t *offp)
1623 {
1624 dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
1625 zap_cursor_t cursor;
1626 zap_attribute_t attr;
1627
1628 /* there is no next dir on a snapshot! */
1629 if (os->os_dsl_dataset->ds_object !=
1630 dsl_dir_phys(dd)->dd_head_dataset_obj)
1631 return (SET_ERROR(ENOENT));
1632
1633 zap_cursor_init_serialized(&cursor,
1634 dd->dd_pool->dp_meta_objset,
1635 dsl_dir_phys(dd)->dd_child_dir_zapobj, *offp);
1636
1637 if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1638 zap_cursor_fini(&cursor);
1639 return (SET_ERROR(ENOENT));
1640 }
1641
1642 if (strlen(attr.za_name) + 1 > namelen) {
1643 zap_cursor_fini(&cursor);
1644 return (SET_ERROR(ENAMETOOLONG));
1645 }
1646
1647 (void) strcpy(name, attr.za_name);
1648 if (idp)
1649 *idp = attr.za_first_integer;
1650 zap_cursor_advance(&cursor);
1651 *offp = zap_cursor_serialize(&cursor);
1652 zap_cursor_fini(&cursor);
1653
1654 return (0);
1655 }
1656
1657 typedef struct dmu_objset_find_ctx {
1658 taskq_t *dc_tq;
1659 dsl_pool_t *dc_dp;
1660 uint64_t dc_ddobj;
1661 int (*dc_func)(dsl_pool_t *, dsl_dataset_t *, void *);
1662 void *dc_arg;
1663 int dc_flags;
1664 kmutex_t *dc_error_lock;
1665 int *dc_error;
1666 } dmu_objset_find_ctx_t;
1667
1668 static void
1669 dmu_objset_find_dp_impl(dmu_objset_find_ctx_t *dcp)
1670 {
1671 dsl_pool_t *dp = dcp->dc_dp;
1672 dmu_objset_find_ctx_t *child_dcp;
1673 dsl_dir_t *dd;
1674 dsl_dataset_t *ds;
1675 zap_cursor_t zc;
1676 zap_attribute_t *attr;
1677 uint64_t thisobj;
1678 int err = 0;
1679
1680 /* don't process if there already was an error */
1681 if (*dcp->dc_error != 0)
1682 goto out;
1683
1684 err = dsl_dir_hold_obj(dp, dcp->dc_ddobj, NULL, FTAG, &dd);
1685 if (err != 0)
1686 goto out;
1687
1688 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1689 if (dd->dd_myname[0] == '$') {
1690 dsl_dir_rele(dd, FTAG);
1691 goto out;
1692 }
1693
1694 thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
1695 attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1696
1697 /*
1698 * Iterate over all children.
1699 */
1700 if (dcp->dc_flags & DS_FIND_CHILDREN) {
1701 for (zap_cursor_init(&zc, dp->dp_meta_objset,
1702 dsl_dir_phys(dd)->dd_child_dir_zapobj);
1703 zap_cursor_retrieve(&zc, attr) == 0;
1704 (void) zap_cursor_advance(&zc)) {
1705 ASSERT3U(attr->za_integer_length, ==,
1706 sizeof (uint64_t));
1707 ASSERT3U(attr->za_num_integers, ==, 1);
1708
1709 child_dcp = kmem_alloc(sizeof (*child_dcp), KM_SLEEP);
1710 *child_dcp = *dcp;
1711 child_dcp->dc_ddobj = attr->za_first_integer;
1712 if (dcp->dc_tq != NULL)
1713 (void) taskq_dispatch(dcp->dc_tq,
1714 dmu_objset_find_dp_cb, child_dcp, TQ_SLEEP);
1715 else
1716 dmu_objset_find_dp_impl(child_dcp);
1717 }
1718 zap_cursor_fini(&zc);
1719 }
1720
1721 /*
1722 * Iterate over all snapshots.
1723 */
1724 if (dcp->dc_flags & DS_FIND_SNAPSHOTS) {
1725 dsl_dataset_t *ds;
1726 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1727
1728 if (err == 0) {
1729 uint64_t snapobj;
1730
1731 snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
1732 dsl_dataset_rele(ds, FTAG);
1733
1734 for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
1735 zap_cursor_retrieve(&zc, attr) == 0;
1736 (void) zap_cursor_advance(&zc)) {
1737 ASSERT3U(attr->za_integer_length, ==,
1738 sizeof (uint64_t));
1739 ASSERT3U(attr->za_num_integers, ==, 1);
1740
1741 err = dsl_dataset_hold_obj(dp,
1742 attr->za_first_integer, FTAG, &ds);
1743 if (err != 0)
1744 break;
1745 err = dcp->dc_func(dp, ds, dcp->dc_arg);
1746 dsl_dataset_rele(ds, FTAG);
1747 if (err != 0)
1748 break;
1749 }
1750 zap_cursor_fini(&zc);
1751 }
1752 }
1753
1754 dsl_dir_rele(dd, FTAG);
1755 kmem_free(attr, sizeof (zap_attribute_t));
1756
1757 if (err != 0)
1758 goto out;
1759
1760 /*
1761 * Apply to self.
1762 */
1763 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1764 if (err != 0)
1765 goto out;
1766 err = dcp->dc_func(dp, ds, dcp->dc_arg);
1767 dsl_dataset_rele(ds, FTAG);
1768
1769 out:
1770 if (err != 0) {
1771 mutex_enter(dcp->dc_error_lock);
1772 /* only keep first error */
1773 if (*dcp->dc_error == 0)
1774 *dcp->dc_error = err;
1775 mutex_exit(dcp->dc_error_lock);
1776 }
1777
1778 kmem_free(dcp, sizeof (*dcp));
1779 }
1780
1781 static void
1782 dmu_objset_find_dp_cb(void *arg)
1783 {
1784 dmu_objset_find_ctx_t *dcp = arg;
1785 dsl_pool_t *dp = dcp->dc_dp;
1786
1787 dsl_pool_config_enter(dp, FTAG);
1788
1789 dmu_objset_find_dp_impl(dcp);
1790
1791 dsl_pool_config_exit(dp, FTAG);
1792 }
1793
1794 /*
1795 * Find objsets under and including ddobj, call func(ds) on each.
1796 * The order for the enumeration is completely undefined.
1797 * func is called with dsl_pool_config held.
1798 */
1799 int
1800 dmu_objset_find_dp(dsl_pool_t *dp, uint64_t ddobj,
1801 int func(dsl_pool_t *, dsl_dataset_t *, void *), void *arg, int flags)
1802 {
1803 int error = 0;
1804 taskq_t *tq = NULL;
1805 int ntasks;
1806 dmu_objset_find_ctx_t *dcp;
1807 kmutex_t err_lock;
1808
1809 mutex_init(&err_lock, NULL, MUTEX_DEFAULT, NULL);
1810 dcp = kmem_alloc(sizeof (*dcp), KM_SLEEP);
1811 dcp->dc_tq = NULL;
1812 dcp->dc_dp = dp;
1813 dcp->dc_ddobj = ddobj;
1814 dcp->dc_func = func;
1815 dcp->dc_arg = arg;
1816 dcp->dc_flags = flags;
1817 dcp->dc_error_lock = &err_lock;
1818 dcp->dc_error = &error;
1819
1820 if ((flags & DS_FIND_SERIALIZE) || dsl_pool_config_held_writer(dp)) {
1821 /*
1822 * In case a write lock is held we can't make use of
1823 * parallelism, as down the stack of the worker threads
1824 * the lock is asserted via dsl_pool_config_held.
1825 * In case of a read lock this is solved by getting a read
1826 * lock in each worker thread, which isn't possible in case
1827 * of a writer lock. So we fall back to the synchronous path
1828 * here.
1829 * In the future it might be possible to get some magic into
1830 * dsl_pool_config_held in a way that it returns true for
1831 * the worker threads so that a single lock held from this
1832 * thread suffices. For now, stay single threaded.
1833 */
1834 dmu_objset_find_dp_impl(dcp);
1835
1836 return (error);
1837 }
1838
1839 ntasks = dmu_find_threads;
1840 if (ntasks == 0)
1841 ntasks = vdev_count_leaves(dp->dp_spa) * 4;
1842 tq = taskq_create("dmu_objset_find", ntasks, minclsyspri, ntasks,
1843 INT_MAX, 0);
1844 if (tq == NULL) {
1845 kmem_free(dcp, sizeof (*dcp));
1846 return (SET_ERROR(ENOMEM));
1847 }
1848 dcp->dc_tq = tq;
1849
1850 /* dcp will be freed by task */
1851 (void) taskq_dispatch(tq, dmu_objset_find_dp_cb, dcp, TQ_SLEEP);
1852
1853 /*
1854 * PORTING: this code relies on the property of taskq_wait to wait
1855 * until no more tasks are queued and no more tasks are active. As
1856 * we always queue new tasks from within other tasks, task_wait
1857 * reliably waits for the full recursion to finish, even though we
1858 * enqueue new tasks after taskq_wait has been called.
1859 * On platforms other than illumos, taskq_wait may not have this
1860 * property.
1861 */
1862 taskq_wait(tq);
1863 taskq_destroy(tq);
1864 mutex_destroy(&err_lock);
1865
1866 return (error);
1867 }
1868
1869 /*
1870 * Find all objsets under name, and for each, call 'func(child_name, arg)'.
1871 * The dp_config_rwlock must not be held when this is called, and it
1872 * will not be held when the callback is called.
1873 * Therefore this function should only be used when the pool is not changing
1874 * (e.g. in syncing context), or the callback can deal with the possible races.
1875 */
1876 static int
1877 dmu_objset_find_impl(spa_t *spa, const char *name,
1878 int func(const char *, void *), void *arg, int flags)
1879 {
1880 dsl_dir_t *dd;
1881 dsl_pool_t *dp = spa_get_dsl(spa);
1882 dsl_dataset_t *ds;
1883 zap_cursor_t zc;
1884 zap_attribute_t *attr;
1885 char *child;
1886 uint64_t thisobj;
1887 int err;
1888
1889 dsl_pool_config_enter(dp, FTAG);
1890
1891 err = dsl_dir_hold(dp, name, FTAG, &dd, NULL);
1892 if (err != 0) {
1893 dsl_pool_config_exit(dp, FTAG);
1894 return (err);
1895 }
1896
1897 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1898 if (dd->dd_myname[0] == '$') {
1899 dsl_dir_rele(dd, FTAG);
1900 dsl_pool_config_exit(dp, FTAG);
1901 return (0);
1902 }
1903
1904 thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
1905 attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1906
1907 /*
1908 * Iterate over all children.
1909 */
1910 if (flags & DS_FIND_CHILDREN) {
1911 for (zap_cursor_init(&zc, dp->dp_meta_objset,
1912 dsl_dir_phys(dd)->dd_child_dir_zapobj);
1913 zap_cursor_retrieve(&zc, attr) == 0;
1914 (void) zap_cursor_advance(&zc)) {
1915 ASSERT3U(attr->za_integer_length, ==,
1916 sizeof (uint64_t));
1917 ASSERT3U(attr->za_num_integers, ==, 1);
1918
1919 child = kmem_asprintf("%s/%s", name, attr->za_name);
1920 dsl_pool_config_exit(dp, FTAG);
1921 err = dmu_objset_find_impl(spa, child,
1922 func, arg, flags);
1923 dsl_pool_config_enter(dp, FTAG);
1924 strfree(child);
1925 if (err != 0)
1926 break;
1927 }
1928 zap_cursor_fini(&zc);
1929
1930 if (err != 0) {
1931 dsl_dir_rele(dd, FTAG);
1932 dsl_pool_config_exit(dp, FTAG);
1933 kmem_free(attr, sizeof (zap_attribute_t));
1934 return (err);
1935 }
1936 }
1937
1938 /*
1939 * Iterate over all snapshots.
1940 */
1941 if (flags & DS_FIND_SNAPSHOTS) {
1942 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1943
1944 if (err == 0) {
1945 uint64_t snapobj;
1946
1947 snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
1948 dsl_dataset_rele(ds, FTAG);
1949
1950 for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
1951 zap_cursor_retrieve(&zc, attr) == 0;
1952 (void) zap_cursor_advance(&zc)) {
1953 ASSERT3U(attr->za_integer_length, ==,
1954 sizeof (uint64_t));
1955 ASSERT3U(attr->za_num_integers, ==, 1);
1956
1957 child = kmem_asprintf("%s@%s",
1958 name, attr->za_name);
1959 dsl_pool_config_exit(dp, FTAG);
1960 err = func(child, arg);
1961 dsl_pool_config_enter(dp, FTAG);
1962 strfree(child);
1963 if (err != 0)
1964 break;
1965 }
1966 zap_cursor_fini(&zc);
1967 }
1968 }
1969
1970 dsl_dir_rele(dd, FTAG);
1971 kmem_free(attr, sizeof (zap_attribute_t));
1972 dsl_pool_config_exit(dp, FTAG);
1973
1974 if (err != 0)
1975 return (err);
1976
1977 /* Apply to self. */
1978 return (func(name, arg));
1979 }
1980
1981 /*
1982 * See comment above dmu_objset_find_impl().
1983 */
1984 int
1985 dmu_objset_find(char *name, int func(const char *, void *), void *arg,
1986 int flags)
1987 {
1988 spa_t *spa;
1989 int error;
1990
1991 error = spa_open(name, &spa, FTAG);
1992 if (error != 0)
1993 return (error);
1994 error = dmu_objset_find_impl(spa, name, func, arg, flags);
1995 spa_close(spa, FTAG);
1996 return (error);
1997 }
1998
1999 void
2000 dmu_objset_set_user(objset_t *os, void *user_ptr)
2001 {
2002 ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
2003 os->os_user_ptr = user_ptr;
2004 }
2005
2006 void *
2007 dmu_objset_get_user(objset_t *os)
2008 {
2009 ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
2010 return (os->os_user_ptr);
2011 }
2012
2013 /*
2014 * Determine name of filesystem, given name of snapshot.
2015 * buf must be at least MAXNAMELEN bytes
2016 */
2017 int
2018 dmu_fsname(const char *snapname, char *buf)
2019 {
2020 char *atp = strchr(snapname, '@');
2021 if (atp == NULL)
2022 return (SET_ERROR(EINVAL));
2023 if (atp - snapname >= MAXNAMELEN)
2024 return (SET_ERROR(ENAMETOOLONG));
2025 (void) strlcpy(buf, snapname, atp - snapname + 1);
2026 return (0);
2027 }
2028
2029 #if defined(_KERNEL) && defined(HAVE_SPL)
2030 EXPORT_SYMBOL(dmu_objset_zil);
2031 EXPORT_SYMBOL(dmu_objset_pool);
2032 EXPORT_SYMBOL(dmu_objset_ds);
2033 EXPORT_SYMBOL(dmu_objset_type);
2034 EXPORT_SYMBOL(dmu_objset_name);
2035 EXPORT_SYMBOL(dmu_objset_hold);
2036 EXPORT_SYMBOL(dmu_objset_own);
2037 EXPORT_SYMBOL(dmu_objset_rele);
2038 EXPORT_SYMBOL(dmu_objset_disown);
2039 EXPORT_SYMBOL(dmu_objset_from_ds);
2040 EXPORT_SYMBOL(dmu_objset_create);
2041 EXPORT_SYMBOL(dmu_objset_clone);
2042 EXPORT_SYMBOL(dmu_objset_stats);
2043 EXPORT_SYMBOL(dmu_objset_fast_stat);
2044 EXPORT_SYMBOL(dmu_objset_spa);
2045 EXPORT_SYMBOL(dmu_objset_space);
2046 EXPORT_SYMBOL(dmu_objset_fsid_guid);
2047 EXPORT_SYMBOL(dmu_objset_find);
2048 EXPORT_SYMBOL(dmu_objset_byteswap);
2049 EXPORT_SYMBOL(dmu_objset_evict_dbufs);
2050 EXPORT_SYMBOL(dmu_objset_snap_cmtime);
2051
2052 EXPORT_SYMBOL(dmu_objset_sync);
2053 EXPORT_SYMBOL(dmu_objset_is_dirty);
2054 EXPORT_SYMBOL(dmu_objset_create_impl);
2055 EXPORT_SYMBOL(dmu_objset_open_impl);
2056 EXPORT_SYMBOL(dmu_objset_evict);
2057 EXPORT_SYMBOL(dmu_objset_register_type);
2058 EXPORT_SYMBOL(dmu_objset_do_userquota_updates);
2059 EXPORT_SYMBOL(dmu_objset_userquota_get_ids);
2060 EXPORT_SYMBOL(dmu_objset_userused_enabled);
2061 EXPORT_SYMBOL(dmu_objset_userspace_upgrade);
2062 EXPORT_SYMBOL(dmu_objset_userspace_present);
2063 #endif