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.
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.
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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012 by Delphix. All rights reserved.
26 /* Portions Copyright 2010 Robert Milkowski */
29 #include <sys/zfs_context.h>
30 #include <sys/dmu_objset.h>
31 #include <sys/dsl_dir.h>
32 #include <sys/dsl_dataset.h>
33 #include <sys/dsl_prop.h>
34 #include <sys/dsl_pool.h>
35 #include <sys/dsl_synctask.h>
36 #include <sys/dsl_deleg.h>
37 #include <sys/dnode.h>
40 #include <sys/dmu_tx.h>
43 #include <sys/dmu_impl.h>
44 #include <sys/zfs_ioctl.h>
46 #include <sys/zfs_onexit.h>
49 * Needed to close a window in dnode_move() that allows the objset to be freed
50 * before it can be safely accessed.
57 rw_init(&os_lock, NULL, RW_DEFAULT, NULL);
67 dmu_objset_spa(objset_t *os)
73 dmu_objset_zil(objset_t *os)
79 dmu_objset_pool(objset_t *os)
83 if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir)
84 return (ds->ds_dir->dd_pool);
86 return (spa_get_dsl(os->os_spa));
90 dmu_objset_ds(objset_t *os)
92 return (os->os_dsl_dataset);
96 dmu_objset_type(objset_t *os)
98 return (os->os_phys->os_type);
102 dmu_objset_name(objset_t *os, char *buf)
104 dsl_dataset_name(os->os_dsl_dataset, buf);
108 dmu_objset_id(objset_t *os)
110 dsl_dataset_t *ds = os->os_dsl_dataset;
112 return (ds ? ds->ds_object : 0);
116 dmu_objset_syncprop(objset_t *os)
118 return (os->os_sync);
122 dmu_objset_logbias(objset_t *os)
124 return (os->os_logbias);
128 checksum_changed_cb(void *arg, uint64_t newval)
133 * Inheritance should have been done by now.
135 ASSERT(newval != ZIO_CHECKSUM_INHERIT);
137 os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
141 compression_changed_cb(void *arg, uint64_t newval)
146 * Inheritance and range checking should have been done by now.
148 ASSERT(newval != ZIO_COMPRESS_INHERIT);
150 os->os_compress = zio_compress_select(newval, ZIO_COMPRESS_ON_VALUE);
154 copies_changed_cb(void *arg, uint64_t newval)
159 * Inheritance and range checking should have been done by now.
162 ASSERT(newval <= spa_max_replication(os->os_spa));
164 os->os_copies = newval;
168 dedup_changed_cb(void *arg, uint64_t newval)
171 spa_t *spa = os->os_spa;
172 enum zio_checksum checksum;
175 * Inheritance should have been done by now.
177 ASSERT(newval != ZIO_CHECKSUM_INHERIT);
179 checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF);
181 os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK;
182 os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY);
186 primary_cache_changed_cb(void *arg, uint64_t newval)
191 * Inheritance and range checking should have been done by now.
193 ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
194 newval == ZFS_CACHE_METADATA);
196 os->os_primary_cache = newval;
200 secondary_cache_changed_cb(void *arg, uint64_t newval)
205 * Inheritance and range checking should have been done by now.
207 ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
208 newval == ZFS_CACHE_METADATA);
210 os->os_secondary_cache = newval;
214 sync_changed_cb(void *arg, uint64_t newval)
219 * Inheritance and range checking should have been done by now.
221 ASSERT(newval == ZFS_SYNC_STANDARD || newval == ZFS_SYNC_ALWAYS ||
222 newval == ZFS_SYNC_DISABLED);
224 os->os_sync = newval;
226 zil_set_sync(os->os_zil, newval);
230 logbias_changed_cb(void *arg, uint64_t newval)
234 ASSERT(newval == ZFS_LOGBIAS_LATENCY ||
235 newval == ZFS_LOGBIAS_THROUGHPUT);
236 os->os_logbias = newval;
238 zil_set_logbias(os->os_zil, newval);
242 dmu_objset_byteswap(void *buf, size_t size)
244 objset_phys_t *osp = buf;
246 ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t));
247 dnode_byteswap(&osp->os_meta_dnode);
248 byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
249 osp->os_type = BSWAP_64(osp->os_type);
250 osp->os_flags = BSWAP_64(osp->os_flags);
251 if (size == sizeof (objset_phys_t)) {
252 dnode_byteswap(&osp->os_userused_dnode);
253 dnode_byteswap(&osp->os_groupused_dnode);
258 dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
264 ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
266 os = kmem_zalloc(sizeof (objset_t), KM_SLEEP);
267 os->os_dsl_dataset = ds;
270 if (!BP_IS_HOLE(os->os_rootbp)) {
271 uint32_t aflags = ARC_WAIT;
273 SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
274 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
276 if (DMU_OS_IS_L2CACHEABLE(os))
277 aflags |= ARC_L2CACHE;
279 dprintf_bp(os->os_rootbp, "reading %s", "");
280 err = arc_read(NULL, spa, os->os_rootbp,
281 arc_getbuf_func, &os->os_phys_buf,
282 ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
284 kmem_free(os, sizeof (objset_t));
285 /* convert checksum errors into IO errors */
291 /* Increase the blocksize if we are permitted. */
292 if (spa_version(spa) >= SPA_VERSION_USERSPACE &&
293 arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) {
294 arc_buf_t *buf = arc_buf_alloc(spa,
295 sizeof (objset_phys_t), &os->os_phys_buf,
297 bzero(buf->b_data, sizeof (objset_phys_t));
298 bcopy(os->os_phys_buf->b_data, buf->b_data,
299 arc_buf_size(os->os_phys_buf));
300 (void) arc_buf_remove_ref(os->os_phys_buf,
302 os->os_phys_buf = buf;
305 os->os_phys = os->os_phys_buf->b_data;
306 os->os_flags = os->os_phys->os_flags;
308 int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
309 sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE;
310 os->os_phys_buf = arc_buf_alloc(spa, size,
311 &os->os_phys_buf, ARC_BUFC_METADATA);
312 os->os_phys = os->os_phys_buf->b_data;
313 bzero(os->os_phys, size);
317 * Note: the changed_cb will be called once before the register
318 * func returns, thus changing the checksum/compression from the
319 * default (fletcher2/off). Snapshots don't need to know about
320 * checksum/compression/copies.
323 err = dsl_prop_register(ds, "primarycache",
324 primary_cache_changed_cb, os);
326 err = dsl_prop_register(ds, "secondarycache",
327 secondary_cache_changed_cb, os);
328 if (!dsl_dataset_is_snapshot(ds)) {
330 err = dsl_prop_register(ds, "checksum",
331 checksum_changed_cb, os);
333 err = dsl_prop_register(ds, "compression",
334 compression_changed_cb, os);
336 err = dsl_prop_register(ds, "copies",
337 copies_changed_cb, os);
339 err = dsl_prop_register(ds, "dedup",
340 dedup_changed_cb, os);
342 err = dsl_prop_register(ds, "logbias",
343 logbias_changed_cb, os);
345 err = dsl_prop_register(ds, "sync",
346 sync_changed_cb, os);
349 VERIFY(arc_buf_remove_ref(os->os_phys_buf,
350 &os->os_phys_buf) == 1);
351 kmem_free(os, sizeof (objset_t));
354 } else if (ds == NULL) {
355 /* It's the meta-objset. */
356 os->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
357 os->os_compress = ZIO_COMPRESS_LZJB;
358 os->os_copies = spa_max_replication(spa);
359 os->os_dedup_checksum = ZIO_CHECKSUM_OFF;
360 os->os_dedup_verify = 0;
363 os->os_primary_cache = ZFS_CACHE_ALL;
364 os->os_secondary_cache = ZFS_CACHE_ALL;
367 if (ds == NULL || !dsl_dataset_is_snapshot(ds))
368 os->os_zil_header = os->os_phys->os_zil_header;
369 os->os_zil = zil_alloc(os, &os->os_zil_header);
371 for (i = 0; i < TXG_SIZE; i++) {
372 list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t),
373 offsetof(dnode_t, dn_dirty_link[i]));
374 list_create(&os->os_free_dnodes[i], sizeof (dnode_t),
375 offsetof(dnode_t, dn_dirty_link[i]));
377 list_create(&os->os_dnodes, sizeof (dnode_t),
378 offsetof(dnode_t, dn_link));
379 list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
380 offsetof(dmu_buf_impl_t, db_link));
382 mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
383 mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
384 mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
386 DMU_META_DNODE(os) = dnode_special_open(os,
387 &os->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT,
389 if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) {
390 DMU_USERUSED_DNODE(os) = dnode_special_open(os,
391 &os->os_phys->os_userused_dnode, DMU_USERUSED_OBJECT,
392 &os->os_userused_dnode);
393 DMU_GROUPUSED_DNODE(os) = dnode_special_open(os,
394 &os->os_phys->os_groupused_dnode, DMU_GROUPUSED_OBJECT,
395 &os->os_groupused_dnode);
399 * We should be the only thread trying to do this because we
400 * have ds_opening_lock
403 mutex_enter(&ds->ds_lock);
404 ASSERT(ds->ds_objset == NULL);
406 mutex_exit(&ds->ds_lock);
414 dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
418 mutex_enter(&ds->ds_opening_lock);
419 *osp = ds->ds_objset;
421 err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
422 ds, dsl_dataset_get_blkptr(ds), osp);
424 mutex_exit(&ds->ds_opening_lock);
428 /* called from zpl */
430 dmu_objset_hold(const char *name, void *tag, objset_t **osp)
435 err = dsl_dataset_hold(name, tag, &ds);
439 err = dmu_objset_from_ds(ds, osp);
441 dsl_dataset_rele(ds, tag);
446 /* called from zpl */
448 dmu_objset_own(const char *name, dmu_objset_type_t type,
449 boolean_t readonly, void *tag, objset_t **osp)
454 err = dsl_dataset_own(name, B_FALSE, tag, &ds);
458 err = dmu_objset_from_ds(ds, osp);
460 dsl_dataset_disown(ds, tag);
461 } else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) {
462 dmu_objset_disown(*osp, tag);
464 } else if (!readonly && dsl_dataset_is_snapshot(ds)) {
465 dmu_objset_disown(*osp, tag);
472 dmu_objset_rele(objset_t *os, void *tag)
474 dsl_dataset_rele(os->os_dsl_dataset, tag);
478 dmu_objset_disown(objset_t *os, void *tag)
480 dsl_dataset_disown(os->os_dsl_dataset, tag);
484 dmu_objset_evict_dbufs(objset_t *os)
488 mutex_enter(&os->os_lock);
490 /* process the mdn last, since the other dnodes have holds on it */
491 list_remove(&os->os_dnodes, DMU_META_DNODE(os));
492 list_insert_tail(&os->os_dnodes, DMU_META_DNODE(os));
495 * Find the first dnode with holds. We have to do this dance
496 * because dnode_add_ref() only works if you already have a
497 * hold. If there are no holds then it has no dbufs so OK to
500 for (dn = list_head(&os->os_dnodes);
501 dn && !dnode_add_ref(dn, FTAG);
502 dn = list_next(&os->os_dnodes, dn))
506 dnode_t *next_dn = dn;
509 next_dn = list_next(&os->os_dnodes, next_dn);
510 } while (next_dn && !dnode_add_ref(next_dn, FTAG));
512 mutex_exit(&os->os_lock);
513 dnode_evict_dbufs(dn);
514 dnode_rele(dn, FTAG);
515 mutex_enter(&os->os_lock);
518 dn = list_head(&os->os_dnodes);
519 mutex_exit(&os->os_lock);
520 return (dn != DMU_META_DNODE(os));
524 dmu_objset_evict(objset_t *os)
526 dsl_dataset_t *ds = os->os_dsl_dataset;
528 for (int t = 0; t < TXG_SIZE; t++)
529 ASSERT(!dmu_objset_is_dirty(os, t));
532 if (!dsl_dataset_is_snapshot(ds)) {
533 VERIFY(0 == dsl_prop_unregister(ds, "checksum",
534 checksum_changed_cb, os));
535 VERIFY(0 == dsl_prop_unregister(ds, "compression",
536 compression_changed_cb, os));
537 VERIFY(0 == dsl_prop_unregister(ds, "copies",
538 copies_changed_cb, os));
539 VERIFY(0 == dsl_prop_unregister(ds, "dedup",
540 dedup_changed_cb, os));
541 VERIFY(0 == dsl_prop_unregister(ds, "logbias",
542 logbias_changed_cb, os));
543 VERIFY(0 == dsl_prop_unregister(ds, "sync",
544 sync_changed_cb, os));
546 VERIFY(0 == dsl_prop_unregister(ds, "primarycache",
547 primary_cache_changed_cb, os));
548 VERIFY(0 == dsl_prop_unregister(ds, "secondarycache",
549 secondary_cache_changed_cb, os));
556 * We should need only a single pass over the dnode list, since
557 * nothing can be added to the list at this point.
559 (void) dmu_objset_evict_dbufs(os);
561 dnode_special_close(&os->os_meta_dnode);
562 if (DMU_USERUSED_DNODE(os)) {
563 dnode_special_close(&os->os_userused_dnode);
564 dnode_special_close(&os->os_groupused_dnode);
566 zil_free(os->os_zil);
568 ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
570 VERIFY(arc_buf_remove_ref(os->os_phys_buf, &os->os_phys_buf) == 1);
573 * This is a barrier to prevent the objset from going away in
574 * dnode_move() until we can safely ensure that the objset is still in
575 * use. We consider the objset valid before the barrier and invalid
578 rw_enter(&os_lock, RW_READER);
581 mutex_destroy(&os->os_lock);
582 mutex_destroy(&os->os_obj_lock);
583 mutex_destroy(&os->os_user_ptr_lock);
584 kmem_free(os, sizeof (objset_t));
588 dmu_objset_snap_cmtime(objset_t *os)
590 return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
593 /* called from dsl for meta-objset */
595 dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
596 dmu_objset_type_t type, dmu_tx_t *tx)
601 ASSERT(dmu_tx_is_syncing(tx));
603 VERIFY(0 == dmu_objset_from_ds(ds, &os));
605 VERIFY(0 == dmu_objset_open_impl(spa, NULL, bp, &os));
607 mdn = DMU_META_DNODE(os);
609 dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
610 DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
613 * We don't want to have to increase the meta-dnode's nlevels
614 * later, because then we could do it in quescing context while
615 * we are also accessing it in open context.
617 * This precaution is not necessary for the MOS (ds == NULL),
618 * because the MOS is only updated in syncing context.
619 * This is most fortunate: the MOS is the only objset that
620 * needs to be synced multiple times as spa_sync() iterates
621 * to convergence, so minimizing its dn_nlevels matters.
627 * Determine the number of levels necessary for the meta-dnode
628 * to contain DN_MAX_OBJECT dnodes.
630 while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
631 (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
632 DN_MAX_OBJECT * sizeof (dnode_phys_t))
635 mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
636 mdn->dn_nlevels = levels;
639 ASSERT(type != DMU_OST_NONE);
640 ASSERT(type != DMU_OST_ANY);
641 ASSERT(type < DMU_OST_NUMTYPES);
642 os->os_phys->os_type = type;
643 if (dmu_objset_userused_enabled(os)) {
644 os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
645 os->os_flags = os->os_phys->os_flags;
648 dsl_dataset_dirty(ds, tx);
654 void (*userfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
656 dsl_dataset_t *clone_origin;
657 const char *lastname;
658 dmu_objset_type_t type;
665 dmu_objset_create_check(void *arg1, void *arg2, dmu_tx_t *tx)
667 dsl_dir_t *dd = arg1;
668 struct oscarg *oa = arg2;
669 objset_t *mos = dd->dd_pool->dp_meta_objset;
673 err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj,
674 oa->lastname, sizeof (uint64_t), 1, &ddobj);
676 return (err ? err : EEXIST);
678 if (oa->clone_origin != NULL) {
679 /* You can't clone across pools. */
680 if (oa->clone_origin->ds_dir->dd_pool != dd->dd_pool)
683 /* You can only clone snapshots, not the head datasets. */
684 if (!dsl_dataset_is_snapshot(oa->clone_origin))
692 dmu_objset_create_sync(void *arg1, void *arg2, dmu_tx_t *tx)
694 dsl_dir_t *dd = arg1;
695 spa_t *spa = dd->dd_pool->dp_spa;
696 struct oscarg *oa = arg2;
701 ASSERT(dmu_tx_is_syncing(tx));
703 obj = dsl_dataset_create_sync(dd, oa->lastname,
704 oa->clone_origin, oa->flags, oa->cr, tx);
706 VERIFY3U(0, ==, dsl_dataset_hold_obj(dd->dd_pool, obj, FTAG, &ds));
707 bp = dsl_dataset_get_blkptr(ds);
708 if (BP_IS_HOLE(bp)) {
710 dmu_objset_create_impl(spa, ds, bp, oa->type, tx);
713 oa->userfunc(os, oa->userarg, oa->cr, tx);
716 if (oa->clone_origin == NULL) {
717 spa_history_log_internal_ds(ds, "create", tx, "");
719 char namebuf[MAXNAMELEN];
720 dsl_dataset_name(oa->clone_origin, namebuf);
721 spa_history_log_internal_ds(ds, "clone", tx,
722 "origin=%s (%llu)", namebuf, oa->clone_origin->ds_object);
724 dsl_dataset_rele(ds, FTAG);
728 dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
729 void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
734 struct oscarg oa = { 0 };
736 ASSERT(strchr(name, '@') == NULL);
737 err = dsl_dir_open(name, FTAG, &pdd, &tail);
741 dsl_dir_close(pdd, FTAG);
752 err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
753 dmu_objset_create_sync, pdd, &oa, 5);
754 dsl_dir_close(pdd, FTAG);
759 dmu_objset_clone(const char *name, dsl_dataset_t *clone_origin, uint64_t flags)
764 struct oscarg oa = { 0 };
766 ASSERT(strchr(name, '@') == NULL);
767 err = dsl_dir_open(name, FTAG, &pdd, &tail);
771 dsl_dir_close(pdd, FTAG);
776 oa.clone_origin = clone_origin;
780 err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
781 dmu_objset_create_sync, pdd, &oa, 5);
782 dsl_dir_close(pdd, FTAG);
787 dmu_objset_destroy(const char *name, boolean_t defer)
792 error = dsl_dataset_own(name, B_TRUE, FTAG, &ds);
794 error = dsl_dataset_destroy(ds, FTAG, defer);
795 /* dsl_dataset_destroy() closes the ds. */
801 typedef struct snapallarg {
802 dsl_sync_task_group_t *saa_dstg;
803 boolean_t saa_needsuspend;
806 /* the following are used only if 'temporary' is set: */
807 boolean_t saa_temporary;
808 const char *saa_htag;
809 struct dsl_ds_holdarg *saa_ha;
810 dsl_dataset_t *saa_newds;
813 typedef struct snaponearg {
814 const char *soa_longname; /* long snap name */
815 const char *soa_snapname; /* short snap name */
816 snapallarg_t *soa_saa;
820 snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx)
823 snaponearg_t *soa = arg2;
824 snapallarg_t *saa = soa->soa_saa;
827 /* The props have already been checked by zfs_check_userprops(). */
829 error = dsl_dataset_snapshot_check(os->os_dsl_dataset,
830 soa->soa_snapname, tx);
834 if (saa->saa_temporary) {
836 * Ideally we would just call
837 * dsl_dataset_user_hold_check() and
838 * dsl_dataset_destroy_check() here. However the
839 * dataset we want to hold and destroy is the snapshot
840 * that we just confirmed we can create, but it won't
841 * exist until after these checks are run. Do any
842 * checks we can here and if more checks are added to
843 * those routines in the future, similar checks may be
846 if (spa_version(os->os_spa) < SPA_VERSION_USERREFS)
849 * Not checking number of tags because the tag will be
850 * unique, as it will be the only tag.
852 if (strlen(saa->saa_htag) + MAX_TAG_PREFIX_LEN >= MAXNAMELEN)
855 saa->saa_ha = kmem_alloc(sizeof (struct dsl_ds_holdarg),
857 saa->saa_ha->temphold = B_TRUE;
858 saa->saa_ha->htag = saa->saa_htag;
864 snapshot_sync(void *arg1, void *arg2, dmu_tx_t *tx)
867 dsl_dataset_t *ds = os->os_dsl_dataset;
868 snaponearg_t *soa = arg2;
869 snapallarg_t *saa = soa->soa_saa;
871 dsl_dataset_snapshot_sync(ds, soa->soa_snapname, tx);
873 if (saa->saa_props != NULL) {
875 pa.pa_props = saa->saa_props;
876 pa.pa_source = ZPROP_SRC_LOCAL;
877 dsl_props_set_sync(ds->ds_prev, &pa, tx);
880 if (saa->saa_temporary) {
881 struct dsl_ds_destroyarg da;
883 dsl_dataset_user_hold_sync(ds->ds_prev, saa->saa_ha, tx);
884 kmem_free(saa->saa_ha, sizeof (struct dsl_ds_holdarg));
886 saa->saa_newds = ds->ds_prev;
890 dsl_dataset_destroy_sync(&da, FTAG, tx);
895 snapshot_one_impl(const char *snapname, void *arg)
897 char fsname[MAXPATHLEN];
898 snapallarg_t *saa = arg;
903 (void) strlcpy(fsname, snapname, sizeof (fsname));
904 strchr(fsname, '@')[0] = '\0';
906 err = dmu_objset_hold(fsname, saa, &os);
911 * If the objset is in an inconsistent state (eg, in the process
912 * of being destroyed), don't snapshot it.
914 if (os->os_dsl_dataset->ds_phys->ds_flags & DS_FLAG_INCONSISTENT) {
915 dmu_objset_rele(os, saa);
919 if (saa->saa_needsuspend) {
920 err = zil_suspend(dmu_objset_zil(os));
922 dmu_objset_rele(os, saa);
927 soa = kmem_zalloc(sizeof (*soa), KM_SLEEP);
929 soa->soa_longname = snapname;
930 soa->soa_snapname = strchr(snapname, '@') + 1;
932 dsl_sync_task_create(saa->saa_dstg, snapshot_check, snapshot_sync,
939 * The snapshots must all be in the same pool.
942 dmu_objset_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t *errors)
944 dsl_sync_task_t *dst;
945 snapallarg_t saa = { 0 };
951 pair = nvlist_next_nvpair(snaps, NULL);
955 err = spa_open(nvpair_name(pair), &spa, FTAG);
958 saa.saa_dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
959 saa.saa_props = props;
960 saa.saa_needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
962 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
963 pair = nvlist_next_nvpair(snaps, pair)) {
964 err = snapshot_one_impl(nvpair_name(pair), &saa);
966 if (errors != NULL) {
967 fnvlist_add_int32(errors,
968 nvpair_name(pair), err);
975 * If any call to snapshot_one_impl() failed, don't execute the
976 * sync task. The error handling code below will clean up the
977 * snaponearg_t from any successful calls to
978 * snapshot_one_impl().
981 err = dsl_sync_task_group_wait(saa.saa_dstg);
985 for (dst = list_head(&saa.saa_dstg->dstg_tasks); dst;
986 dst = list_next(&saa.saa_dstg->dstg_tasks, dst)) {
987 objset_t *os = dst->dst_arg1;
988 snaponearg_t *soa = dst->dst_arg2;
989 if (dst->dst_err != 0) {
990 if (errors != NULL) {
991 fnvlist_add_int32(errors,
992 soa->soa_longname, dst->dst_err);
997 if (saa.saa_needsuspend)
998 zil_resume(dmu_objset_zil(os));
999 dmu_objset_rele(os, &saa);
1000 kmem_free(soa, sizeof (*soa));
1003 dsl_sync_task_group_destroy(saa.saa_dstg);
1004 spa_close(spa, FTAG);
1009 dmu_objset_snapshot_one(const char *fsname, const char *snapname)
1012 char *longsnap = kmem_asprintf("%s@%s", fsname, snapname);
1013 nvlist_t *snaps = fnvlist_alloc();
1015 fnvlist_add_boolean(snaps, longsnap);
1016 err = dmu_objset_snapshot(snaps, NULL, NULL);
1017 fnvlist_free(snaps);
1023 dmu_objset_snapshot_tmp(const char *snapname, const char *tag, int cleanup_fd)
1025 dsl_sync_task_t *dst;
1026 snapallarg_t saa = { 0 };
1031 err = spa_open(snapname, &spa, FTAG);
1034 saa.saa_dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
1036 saa.saa_needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
1037 saa.saa_temporary = B_TRUE;
1039 if (cleanup_fd < 0) {
1040 spa_close(spa, FTAG);
1043 if ((err = zfs_onexit_fd_hold(cleanup_fd, &minor)) != 0) {
1044 spa_close(spa, FTAG);
1048 err = snapshot_one_impl(snapname, &saa);
1051 err = dsl_sync_task_group_wait(saa.saa_dstg);
1053 for (dst = list_head(&saa.saa_dstg->dstg_tasks); dst;
1054 dst = list_next(&saa.saa_dstg->dstg_tasks, dst)) {
1055 objset_t *os = dst->dst_arg1;
1056 dsl_register_onexit_hold_cleanup(saa.saa_newds, tag, minor);
1057 if (saa.saa_needsuspend)
1058 zil_resume(dmu_objset_zil(os));
1061 if (dst->dst_err == 0 && dmu_objset_type(os) == DMU_OST_ZVOL) {
1062 char name[MAXNAMELEN];
1064 dmu_objset_name(os, name);
1065 strlcat(name, "@", sizeof(name));
1066 strlcat(name, snapname, sizeof(name));
1067 zvol_create_minors(name);
1071 dmu_objset_rele(os, &saa);
1074 zfs_onexit_fd_rele(cleanup_fd);
1075 dsl_sync_task_group_destroy(saa.saa_dstg);
1076 spa_close(spa, FTAG);
1082 dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx)
1086 while (dn = list_head(list)) {
1087 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
1088 ASSERT(dn->dn_dbuf->db_data_pending);
1090 * Initialize dn_zio outside dnode_sync() because the
1091 * meta-dnode needs to set it ouside dnode_sync().
1093 dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
1096 ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
1097 list_remove(list, dn);
1100 (void) dnode_add_ref(dn, newlist);
1101 list_insert_tail(newlist, dn);
1110 dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
1112 blkptr_t *bp = zio->io_bp;
1114 dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
1116 ASSERT(bp == os->os_rootbp);
1117 ASSERT(BP_GET_TYPE(bp) == DMU_OT_OBJSET);
1118 ASSERT(BP_GET_LEVEL(bp) == 0);
1121 * Update rootbp fill count: it should be the number of objects
1122 * allocated in the object set (not counting the "special"
1123 * objects that are stored in the objset_phys_t -- the meta
1124 * dnode and user/group accounting objects).
1127 for (int i = 0; i < dnp->dn_nblkptr; i++)
1128 bp->blk_fill += dnp->dn_blkptr[i].blk_fill;
1133 dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
1135 blkptr_t *bp = zio->io_bp;
1136 blkptr_t *bp_orig = &zio->io_bp_orig;
1139 if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
1140 ASSERT(BP_EQUAL(bp, bp_orig));
1142 dsl_dataset_t *ds = os->os_dsl_dataset;
1143 dmu_tx_t *tx = os->os_synctx;
1145 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
1146 dsl_dataset_block_born(ds, bp, tx);
1150 /* called from dsl */
1152 dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
1159 list_t *newlist = NULL;
1160 dbuf_dirty_record_t *dr;
1162 dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
1164 ASSERT(dmu_tx_is_syncing(tx));
1165 /* XXX the write_done callback should really give us the tx... */
1168 if (os->os_dsl_dataset == NULL) {
1170 * This is the MOS. If we have upgraded,
1171 * spa_max_replication() could change, so reset
1174 os->os_copies = spa_max_replication(os->os_spa);
1178 * Create the root block IO
1180 SET_BOOKMARK(&zb, os->os_dsl_dataset ?
1181 os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
1182 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
1183 arc_release(os->os_phys_buf, &os->os_phys_buf);
1185 dmu_write_policy(os, NULL, 0, 0, &zp);
1187 zio = arc_write(pio, os->os_spa, tx->tx_txg,
1188 os->os_rootbp, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os), &zp,
1189 dmu_objset_write_ready, dmu_objset_write_done, os,
1190 ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
1193 * Sync special dnodes - the parent IO for the sync is the root block
1195 DMU_META_DNODE(os)->dn_zio = zio;
1196 dnode_sync(DMU_META_DNODE(os), tx);
1198 os->os_phys->os_flags = os->os_flags;
1200 if (DMU_USERUSED_DNODE(os) &&
1201 DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
1202 DMU_USERUSED_DNODE(os)->dn_zio = zio;
1203 dnode_sync(DMU_USERUSED_DNODE(os), tx);
1204 DMU_GROUPUSED_DNODE(os)->dn_zio = zio;
1205 dnode_sync(DMU_GROUPUSED_DNODE(os), tx);
1208 txgoff = tx->tx_txg & TXG_MASK;
1210 if (dmu_objset_userused_enabled(os)) {
1211 newlist = &os->os_synced_dnodes;
1213 * We must create the list here because it uses the
1214 * dn_dirty_link[] of this txg.
1216 list_create(newlist, sizeof (dnode_t),
1217 offsetof(dnode_t, dn_dirty_link[txgoff]));
1220 dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx);
1221 dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx);
1223 list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff];
1224 while (dr = list_head(list)) {
1225 ASSERT(dr->dr_dbuf->db_level == 0);
1226 list_remove(list, dr);
1228 zio_nowait(dr->dr_zio);
1231 * Free intent log blocks up to this tx.
1233 zil_sync(os->os_zil, tx);
1234 os->os_phys->os_zil_header = os->os_zil_header;
1239 dmu_objset_is_dirty(objset_t *os, uint64_t txg)
1241 return (!list_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]) ||
1242 !list_is_empty(&os->os_free_dnodes[txg & TXG_MASK]));
1245 static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
1248 dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
1254 dmu_objset_userused_enabled(objset_t *os)
1256 return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
1257 used_cbs[os->os_phys->os_type] != NULL &&
1258 DMU_USERUSED_DNODE(os) != NULL);
1262 do_userquota_update(objset_t *os, uint64_t used, uint64_t flags,
1263 uint64_t user, uint64_t group, boolean_t subtract, dmu_tx_t *tx)
1265 if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) {
1266 int64_t delta = DNODE_SIZE + used;
1269 VERIFY3U(0, ==, zap_increment_int(os, DMU_USERUSED_OBJECT,
1271 VERIFY3U(0, ==, zap_increment_int(os, DMU_GROUPUSED_OBJECT,
1277 dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
1280 list_t *list = &os->os_synced_dnodes;
1282 ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os));
1284 while (dn = list_head(list)) {
1286 ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
1287 ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
1288 dn->dn_phys->dn_flags &
1289 DNODE_FLAG_USERUSED_ACCOUNTED);
1291 /* Allocate the user/groupused objects if necessary. */
1292 if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
1293 VERIFY(0 == zap_create_claim(os,
1294 DMU_USERUSED_OBJECT,
1295 DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1296 VERIFY(0 == zap_create_claim(os,
1297 DMU_GROUPUSED_OBJECT,
1298 DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1302 * We intentionally modify the zap object even if the
1303 * net delta is zero. Otherwise
1304 * the block of the zap obj could be shared between
1305 * datasets but need to be different between them after
1309 flags = dn->dn_id_flags;
1311 if (flags & DN_ID_OLD_EXIST) {
1312 do_userquota_update(os, dn->dn_oldused, dn->dn_oldflags,
1313 dn->dn_olduid, dn->dn_oldgid, B_TRUE, tx);
1315 if (flags & DN_ID_NEW_EXIST) {
1316 do_userquota_update(os, DN_USED_BYTES(dn->dn_phys),
1317 dn->dn_phys->dn_flags, dn->dn_newuid,
1318 dn->dn_newgid, B_FALSE, tx);
1321 mutex_enter(&dn->dn_mtx);
1323 dn->dn_oldflags = 0;
1324 if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
1325 dn->dn_olduid = dn->dn_newuid;
1326 dn->dn_oldgid = dn->dn_newgid;
1327 dn->dn_id_flags |= DN_ID_OLD_EXIST;
1328 if (dn->dn_bonuslen == 0)
1329 dn->dn_id_flags |= DN_ID_CHKED_SPILL;
1331 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1333 dn->dn_id_flags &= ~(DN_ID_NEW_EXIST);
1334 mutex_exit(&dn->dn_mtx);
1336 list_remove(list, dn);
1337 dnode_rele(dn, list);
1342 * Returns a pointer to data to find uid/gid from
1344 * If a dirty record for transaction group that is syncing can't
1345 * be found then NULL is returned. In the NULL case it is assumed
1346 * the uid/gid aren't changing.
1349 dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx)
1351 dbuf_dirty_record_t *dr, **drp;
1354 if (db->db_dirtycnt == 0)
1355 return (db->db.db_data); /* Nothing is changing */
1357 for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
1358 if (dr->dr_txg == tx->tx_txg)
1366 DB_DNODE_ENTER(dr->dr_dbuf);
1367 dn = DB_DNODE(dr->dr_dbuf);
1369 if (dn->dn_bonuslen == 0 &&
1370 dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID)
1371 data = dr->dt.dl.dr_data->b_data;
1373 data = dr->dt.dl.dr_data;
1375 DB_DNODE_EXIT(dr->dr_dbuf);
1382 dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
1384 objset_t *os = dn->dn_objset;
1386 dmu_buf_impl_t *db = NULL;
1387 uint64_t *user = NULL;
1388 uint64_t *group = NULL;
1389 int flags = dn->dn_id_flags;
1391 boolean_t have_spill = B_FALSE;
1393 if (!dmu_objset_userused_enabled(dn->dn_objset))
1396 if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST|
1397 DN_ID_CHKED_SPILL)))
1400 if (before && dn->dn_bonuslen != 0)
1401 data = DN_BONUS(dn->dn_phys);
1402 else if (!before && dn->dn_bonuslen != 0) {
1405 mutex_enter(&db->db_mtx);
1406 data = dmu_objset_userquota_find_data(db, tx);
1408 data = DN_BONUS(dn->dn_phys);
1410 } else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) {
1413 if (RW_WRITE_HELD(&dn->dn_struct_rwlock))
1414 rf |= DB_RF_HAVESTRUCT;
1415 error = dmu_spill_hold_by_dnode(dn,
1416 rf | DB_RF_MUST_SUCCEED,
1417 FTAG, (dmu_buf_t **)&db);
1419 mutex_enter(&db->db_mtx);
1420 data = (before) ? db->db.db_data :
1421 dmu_objset_userquota_find_data(db, tx);
1422 have_spill = B_TRUE;
1424 mutex_enter(&dn->dn_mtx);
1425 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1426 mutex_exit(&dn->dn_mtx);
1432 user = &dn->dn_olduid;
1433 group = &dn->dn_oldgid;
1435 user = &dn->dn_newuid;
1436 group = &dn->dn_newgid;
1440 * Must always call the callback in case the object
1441 * type has changed and that type isn't an object type to track
1443 error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data,
1447 * Preserve existing uid/gid when the callback can't determine
1448 * what the new uid/gid are and the callback returned EEXIST.
1449 * The EEXIST error tells us to just use the existing uid/gid.
1450 * If we don't know what the old values are then just assign
1451 * them to 0, since that is a new file being created.
1453 if (!before && data == NULL && error == EEXIST) {
1454 if (flags & DN_ID_OLD_EXIST) {
1455 dn->dn_newuid = dn->dn_olduid;
1456 dn->dn_newgid = dn->dn_oldgid;
1465 mutex_exit(&db->db_mtx);
1467 mutex_enter(&dn->dn_mtx);
1468 if (error == 0 && before)
1469 dn->dn_id_flags |= DN_ID_OLD_EXIST;
1470 if (error == 0 && !before)
1471 dn->dn_id_flags |= DN_ID_NEW_EXIST;
1474 dn->dn_id_flags |= DN_ID_CHKED_SPILL;
1476 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1478 mutex_exit(&dn->dn_mtx);
1480 dmu_buf_rele((dmu_buf_t *)db, FTAG);
1484 dmu_objset_userspace_present(objset_t *os)
1486 return (os->os_phys->os_flags &
1487 OBJSET_FLAG_USERACCOUNTING_COMPLETE);
1491 dmu_objset_userspace_upgrade(objset_t *os)
1496 if (dmu_objset_userspace_present(os))
1498 if (!dmu_objset_userused_enabled(os))
1500 if (dmu_objset_is_snapshot(os))
1504 * We simply need to mark every object dirty, so that it will be
1505 * synced out and now accounted. If this is called
1506 * concurrently, or if we already did some work before crashing,
1507 * that's fine, since we track each object's accounted state
1511 for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
1516 if (issig(JUSTLOOKING) && issig(FORREAL))
1519 objerr = dmu_bonus_hold(os, obj, FTAG, &db);
1522 tx = dmu_tx_create(os);
1523 dmu_tx_hold_bonus(tx, obj);
1524 objerr = dmu_tx_assign(tx, TXG_WAIT);
1529 dmu_buf_will_dirty(db, tx);
1530 dmu_buf_rele(db, FTAG);
1534 os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
1535 txg_wait_synced(dmu_objset_pool(os), 0);
1540 dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
1541 uint64_t *usedobjsp, uint64_t *availobjsp)
1543 dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
1544 usedobjsp, availobjsp);
1548 dmu_objset_fsid_guid(objset_t *os)
1550 return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
1554 dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1556 stat->dds_type = os->os_phys->os_type;
1557 if (os->os_dsl_dataset)
1558 dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
1562 dmu_objset_stats(objset_t *os, nvlist_t *nv)
1564 ASSERT(os->os_dsl_dataset ||
1565 os->os_phys->os_type == DMU_OST_META);
1567 if (os->os_dsl_dataset != NULL)
1568 dsl_dataset_stats(os->os_dsl_dataset, nv);
1570 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
1571 os->os_phys->os_type);
1572 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
1573 dmu_objset_userspace_present(os));
1577 dmu_objset_is_snapshot(objset_t *os)
1579 if (os->os_dsl_dataset != NULL)
1580 return (dsl_dataset_is_snapshot(os->os_dsl_dataset));
1586 dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
1587 boolean_t *conflict)
1589 dsl_dataset_t *ds = os->os_dsl_dataset;
1592 if (ds->ds_phys->ds_snapnames_zapobj == 0)
1595 return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
1596 ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST,
1597 real, maxlen, conflict));
1601 dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
1602 uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1604 dsl_dataset_t *ds = os->os_dsl_dataset;
1605 zap_cursor_t cursor;
1606 zap_attribute_t attr;
1608 if (ds->ds_phys->ds_snapnames_zapobj == 0)
1611 zap_cursor_init_serialized(&cursor,
1612 ds->ds_dir->dd_pool->dp_meta_objset,
1613 ds->ds_phys->ds_snapnames_zapobj, *offp);
1615 if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1616 zap_cursor_fini(&cursor);
1620 if (strlen(attr.za_name) + 1 > namelen) {
1621 zap_cursor_fini(&cursor);
1622 return (ENAMETOOLONG);
1625 (void) strcpy(name, attr.za_name);
1627 *idp = attr.za_first_integer;
1629 *case_conflict = attr.za_normalization_conflict;
1630 zap_cursor_advance(&cursor);
1631 *offp = zap_cursor_serialize(&cursor);
1632 zap_cursor_fini(&cursor);
1638 dmu_dir_list_next(objset_t *os, int namelen, char *name,
1639 uint64_t *idp, uint64_t *offp)
1641 dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
1642 zap_cursor_t cursor;
1643 zap_attribute_t attr;
1645 /* there is no next dir on a snapshot! */
1646 if (os->os_dsl_dataset->ds_object !=
1647 dd->dd_phys->dd_head_dataset_obj)
1650 zap_cursor_init_serialized(&cursor,
1651 dd->dd_pool->dp_meta_objset,
1652 dd->dd_phys->dd_child_dir_zapobj, *offp);
1654 if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1655 zap_cursor_fini(&cursor);
1659 if (strlen(attr.za_name) + 1 > namelen) {
1660 zap_cursor_fini(&cursor);
1661 return (ENAMETOOLONG);
1664 (void) strcpy(name, attr.za_name);
1666 *idp = attr.za_first_integer;
1667 zap_cursor_advance(&cursor);
1668 *offp = zap_cursor_serialize(&cursor);
1669 zap_cursor_fini(&cursor);
1675 int (*func)(const char *, void *);
1681 findfunc(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
1683 struct findarg *fa = arg;
1684 return (fa->func(dsname, fa->arg));
1688 * Find all objsets under name, and for each, call 'func(child_name, arg)'.
1689 * Perhaps change all callers to use dmu_objset_find_spa()?
1692 dmu_objset_find(const char *name, int func(const char *, void *), void *arg,
1698 return (dmu_objset_find_spa(NULL, name, findfunc, &fa, flags));
1702 * Find all objsets under name, call func on each
1705 dmu_objset_find_spa(spa_t *spa, const char *name,
1706 int func(spa_t *, uint64_t, const char *, void *), void *arg, int flags)
1712 zap_attribute_t *attr;
1718 name = spa_name(spa);
1719 err = dsl_dir_open_spa(spa, name, FTAG, &dd, NULL);
1723 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1724 if (dd->dd_myname[0] == '$') {
1725 dsl_dir_close(dd, FTAG);
1729 thisobj = dd->dd_phys->dd_head_dataset_obj;
1730 attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1734 * Iterate over all children.
1736 if (flags & DS_FIND_CHILDREN) {
1737 for (zap_cursor_init(&zc, dp->dp_meta_objset,
1738 dd->dd_phys->dd_child_dir_zapobj);
1739 zap_cursor_retrieve(&zc, attr) == 0;
1740 (void) zap_cursor_advance(&zc)) {
1741 ASSERT(attr->za_integer_length == sizeof (uint64_t));
1742 ASSERT(attr->za_num_integers == 1);
1744 child = kmem_asprintf("%s/%s", name, attr->za_name);
1745 err = dmu_objset_find_spa(spa, child, func, arg, flags);
1750 zap_cursor_fini(&zc);
1753 dsl_dir_close(dd, FTAG);
1754 kmem_free(attr, sizeof (zap_attribute_t));
1760 * Iterate over all snapshots.
1762 if (flags & DS_FIND_SNAPSHOTS) {
1763 if (!dsl_pool_sync_context(dp))
1764 rw_enter(&dp->dp_config_rwlock, RW_READER);
1765 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1766 if (!dsl_pool_sync_context(dp))
1767 rw_exit(&dp->dp_config_rwlock);
1770 uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
1771 dsl_dataset_rele(ds, FTAG);
1773 for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
1774 zap_cursor_retrieve(&zc, attr) == 0;
1775 (void) zap_cursor_advance(&zc)) {
1776 ASSERT(attr->za_integer_length ==
1778 ASSERT(attr->za_num_integers == 1);
1780 child = kmem_asprintf("%s@%s",
1781 name, attr->za_name);
1782 err = func(spa, attr->za_first_integer,
1788 zap_cursor_fini(&zc);
1792 dsl_dir_close(dd, FTAG);
1793 kmem_free(attr, sizeof (zap_attribute_t));
1799 * Apply to self if appropriate.
1801 err = func(spa, thisobj, name, arg);
1807 dmu_objset_prefetch(const char *name, void *arg)
1811 if (dsl_dataset_hold(name, FTAG, &ds))
1814 if (!BP_IS_HOLE(&ds->ds_phys->ds_bp)) {
1815 mutex_enter(&ds->ds_opening_lock);
1816 if (ds->ds_objset == NULL) {
1817 uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH;
1820 SET_BOOKMARK(&zb, ds->ds_object, ZB_ROOT_OBJECT,
1821 ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
1823 (void) arc_read(NULL, dsl_dataset_get_spa(ds),
1824 &ds->ds_phys->ds_bp, NULL, NULL,
1825 ZIO_PRIORITY_ASYNC_READ,
1826 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
1829 mutex_exit(&ds->ds_opening_lock);
1832 dsl_dataset_rele(ds, FTAG);
1837 dmu_objset_set_user(objset_t *os, void *user_ptr)
1839 ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1840 os->os_user_ptr = user_ptr;
1844 dmu_objset_get_user(objset_t *os)
1846 ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1847 return (os->os_user_ptr);