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) 2013 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>
47 #include <sys/dsl_destroy.h>
50 * Needed to close a window in dnode_move() that allows the objset to be freed
51 * before it can be safely accessed.
58 rw_init(&os_lock, NULL, RW_DEFAULT, NULL);
68 dmu_objset_spa(objset_t *os)
74 dmu_objset_zil(objset_t *os)
80 dmu_objset_pool(objset_t *os)
84 if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir)
85 return (ds->ds_dir->dd_pool);
87 return (spa_get_dsl(os->os_spa));
91 dmu_objset_ds(objset_t *os)
93 return (os->os_dsl_dataset);
97 dmu_objset_type(objset_t *os)
99 return (os->os_phys->os_type);
103 dmu_objset_name(objset_t *os, char *buf)
105 dsl_dataset_name(os->os_dsl_dataset, buf);
109 dmu_objset_id(objset_t *os)
111 dsl_dataset_t *ds = os->os_dsl_dataset;
113 return (ds ? ds->ds_object : 0);
117 dmu_objset_syncprop(objset_t *os)
119 return (os->os_sync);
123 dmu_objset_logbias(objset_t *os)
125 return (os->os_logbias);
129 checksum_changed_cb(void *arg, uint64_t newval)
134 * Inheritance should have been done by now.
136 ASSERT(newval != ZIO_CHECKSUM_INHERIT);
138 os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
142 compression_changed_cb(void *arg, uint64_t newval)
147 * Inheritance and range checking should have been done by now.
149 ASSERT(newval != ZIO_COMPRESS_INHERIT);
151 os->os_compress = zio_compress_select(newval, ZIO_COMPRESS_ON_VALUE);
155 copies_changed_cb(void *arg, uint64_t newval)
160 * Inheritance and range checking should have been done by now.
163 ASSERT(newval <= spa_max_replication(os->os_spa));
165 os->os_copies = newval;
169 dedup_changed_cb(void *arg, uint64_t newval)
172 spa_t *spa = os->os_spa;
173 enum zio_checksum checksum;
176 * Inheritance should have been done by now.
178 ASSERT(newval != ZIO_CHECKSUM_INHERIT);
180 checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF);
182 os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK;
183 os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY);
187 primary_cache_changed_cb(void *arg, uint64_t newval)
192 * Inheritance and range checking should have been done by now.
194 ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
195 newval == ZFS_CACHE_METADATA);
197 os->os_primary_cache = newval;
201 secondary_cache_changed_cb(void *arg, uint64_t newval)
206 * Inheritance and range checking should have been done by now.
208 ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
209 newval == ZFS_CACHE_METADATA);
211 os->os_secondary_cache = newval;
215 sync_changed_cb(void *arg, uint64_t newval)
220 * Inheritance and range checking should have been done by now.
222 ASSERT(newval == ZFS_SYNC_STANDARD || newval == ZFS_SYNC_ALWAYS ||
223 newval == ZFS_SYNC_DISABLED);
225 os->os_sync = newval;
227 zil_set_sync(os->os_zil, newval);
231 logbias_changed_cb(void *arg, uint64_t newval)
235 ASSERT(newval == ZFS_LOGBIAS_LATENCY ||
236 newval == ZFS_LOGBIAS_THROUGHPUT);
237 os->os_logbias = newval;
239 zil_set_logbias(os->os_zil, newval);
243 dmu_objset_byteswap(void *buf, size_t size)
245 objset_phys_t *osp = buf;
247 ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t));
248 dnode_byteswap(&osp->os_meta_dnode);
249 byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
250 osp->os_type = BSWAP_64(osp->os_type);
251 osp->os_flags = BSWAP_64(osp->os_flags);
252 if (size == sizeof (objset_phys_t)) {
253 dnode_byteswap(&osp->os_userused_dnode);
254 dnode_byteswap(&osp->os_groupused_dnode);
259 dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
265 ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
267 os = kmem_zalloc(sizeof (objset_t), KM_SLEEP);
268 os->os_dsl_dataset = ds;
271 if (!BP_IS_HOLE(os->os_rootbp)) {
272 uint32_t aflags = ARC_WAIT;
274 SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
275 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
277 if (DMU_OS_IS_L2CACHEABLE(os))
278 aflags |= ARC_L2CACHE;
280 dprintf_bp(os->os_rootbp, "reading %s", "");
281 err = arc_read(NULL, spa, os->os_rootbp,
282 arc_getbuf_func, &os->os_phys_buf,
283 ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
285 kmem_free(os, sizeof (objset_t));
286 /* convert checksum errors into IO errors */
288 err = SET_ERROR(EIO);
292 /* Increase the blocksize if we are permitted. */
293 if (spa_version(spa) >= SPA_VERSION_USERSPACE &&
294 arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) {
295 arc_buf_t *buf = arc_buf_alloc(spa,
296 sizeof (objset_phys_t), &os->os_phys_buf,
298 bzero(buf->b_data, sizeof (objset_phys_t));
299 bcopy(os->os_phys_buf->b_data, buf->b_data,
300 arc_buf_size(os->os_phys_buf));
301 (void) arc_buf_remove_ref(os->os_phys_buf,
303 os->os_phys_buf = buf;
306 os->os_phys = os->os_phys_buf->b_data;
307 os->os_flags = os->os_phys->os_flags;
309 int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
310 sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE;
311 os->os_phys_buf = arc_buf_alloc(spa, size,
312 &os->os_phys_buf, ARC_BUFC_METADATA);
313 os->os_phys = os->os_phys_buf->b_data;
314 bzero(os->os_phys, size);
318 * Note: the changed_cb will be called once before the register
319 * func returns, thus changing the checksum/compression from the
320 * default (fletcher2/off). Snapshots don't need to know about
321 * checksum/compression/copies.
324 err = dsl_prop_register(ds,
325 zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE),
326 primary_cache_changed_cb, os);
328 err = dsl_prop_register(ds,
329 zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE),
330 secondary_cache_changed_cb, os);
332 if (!dsl_dataset_is_snapshot(ds)) {
334 err = dsl_prop_register(ds,
335 zfs_prop_to_name(ZFS_PROP_CHECKSUM),
336 checksum_changed_cb, os);
339 err = dsl_prop_register(ds,
340 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
341 compression_changed_cb, os);
344 err = dsl_prop_register(ds,
345 zfs_prop_to_name(ZFS_PROP_COPIES),
346 copies_changed_cb, os);
349 err = dsl_prop_register(ds,
350 zfs_prop_to_name(ZFS_PROP_DEDUP),
351 dedup_changed_cb, os);
354 err = dsl_prop_register(ds,
355 zfs_prop_to_name(ZFS_PROP_LOGBIAS),
356 logbias_changed_cb, os);
359 err = dsl_prop_register(ds,
360 zfs_prop_to_name(ZFS_PROP_SYNC),
361 sync_changed_cb, os);
365 VERIFY(arc_buf_remove_ref(os->os_phys_buf,
367 kmem_free(os, sizeof (objset_t));
370 } else if (ds == NULL) {
371 /* It's the meta-objset. */
372 os->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
373 os->os_compress = ZIO_COMPRESS_LZJB;
374 os->os_copies = spa_max_replication(spa);
375 os->os_dedup_checksum = ZIO_CHECKSUM_OFF;
376 os->os_dedup_verify = 0;
379 os->os_primary_cache = ZFS_CACHE_ALL;
380 os->os_secondary_cache = ZFS_CACHE_ALL;
383 if (ds == NULL || !dsl_dataset_is_snapshot(ds))
384 os->os_zil_header = os->os_phys->os_zil_header;
385 os->os_zil = zil_alloc(os, &os->os_zil_header);
387 for (i = 0; i < TXG_SIZE; i++) {
388 list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t),
389 offsetof(dnode_t, dn_dirty_link[i]));
390 list_create(&os->os_free_dnodes[i], sizeof (dnode_t),
391 offsetof(dnode_t, dn_dirty_link[i]));
393 list_create(&os->os_dnodes, sizeof (dnode_t),
394 offsetof(dnode_t, dn_link));
395 list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
396 offsetof(dmu_buf_impl_t, db_link));
398 mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
399 mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
400 mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
402 DMU_META_DNODE(os) = dnode_special_open(os,
403 &os->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT,
405 if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) {
406 DMU_USERUSED_DNODE(os) = dnode_special_open(os,
407 &os->os_phys->os_userused_dnode, DMU_USERUSED_OBJECT,
408 &os->os_userused_dnode);
409 DMU_GROUPUSED_DNODE(os) = dnode_special_open(os,
410 &os->os_phys->os_groupused_dnode, DMU_GROUPUSED_OBJECT,
411 &os->os_groupused_dnode);
415 * We should be the only thread trying to do this because we
416 * have ds_opening_lock
419 mutex_enter(&ds->ds_lock);
420 ASSERT(ds->ds_objset == NULL);
422 mutex_exit(&ds->ds_lock);
430 dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
434 mutex_enter(&ds->ds_opening_lock);
435 *osp = ds->ds_objset;
437 err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
438 ds, dsl_dataset_get_blkptr(ds), osp);
440 mutex_exit(&ds->ds_opening_lock);
445 * Holds the pool while the objset is held. Therefore only one objset
446 * can be held at a time.
449 dmu_objset_hold(const char *name, void *tag, objset_t **osp)
455 err = dsl_pool_hold(name, tag, &dp);
458 err = dsl_dataset_hold(dp, name, tag, &ds);
460 dsl_pool_rele(dp, tag);
464 err = dmu_objset_from_ds(ds, osp);
466 dsl_dataset_rele(ds, tag);
467 dsl_pool_rele(dp, tag);
474 * dsl_pool must not be held when this is called.
475 * Upon successful return, there will be a longhold on the dataset,
476 * and the dsl_pool will not be held.
479 dmu_objset_own(const char *name, dmu_objset_type_t type,
480 boolean_t readonly, void *tag, objset_t **osp)
486 err = dsl_pool_hold(name, FTAG, &dp);
489 err = dsl_dataset_own(dp, name, tag, &ds);
491 dsl_pool_rele(dp, FTAG);
495 err = dmu_objset_from_ds(ds, osp);
496 dsl_pool_rele(dp, FTAG);
498 dsl_dataset_disown(ds, tag);
499 } else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) {
500 dsl_dataset_disown(ds, tag);
501 return (SET_ERROR(EINVAL));
502 } else if (!readonly && dsl_dataset_is_snapshot(ds)) {
503 dsl_dataset_disown(ds, tag);
504 return (SET_ERROR(EROFS));
510 dmu_objset_rele(objset_t *os, void *tag)
512 dsl_pool_t *dp = dmu_objset_pool(os);
513 dsl_dataset_rele(os->os_dsl_dataset, tag);
514 dsl_pool_rele(dp, tag);
518 dmu_objset_disown(objset_t *os, void *tag)
520 dsl_dataset_disown(os->os_dsl_dataset, tag);
524 dmu_objset_evict_dbufs(objset_t *os)
528 mutex_enter(&os->os_lock);
530 /* process the mdn last, since the other dnodes have holds on it */
531 list_remove(&os->os_dnodes, DMU_META_DNODE(os));
532 list_insert_tail(&os->os_dnodes, DMU_META_DNODE(os));
535 * Find the first dnode with holds. We have to do this dance
536 * because dnode_add_ref() only works if you already have a
537 * hold. If there are no holds then it has no dbufs so OK to
540 for (dn = list_head(&os->os_dnodes);
541 dn && !dnode_add_ref(dn, FTAG);
542 dn = list_next(&os->os_dnodes, dn))
546 dnode_t *next_dn = dn;
549 next_dn = list_next(&os->os_dnodes, next_dn);
550 } while (next_dn && !dnode_add_ref(next_dn, FTAG));
552 mutex_exit(&os->os_lock);
553 dnode_evict_dbufs(dn);
554 dnode_rele(dn, FTAG);
555 mutex_enter(&os->os_lock);
558 mutex_exit(&os->os_lock);
562 dmu_objset_evict(objset_t *os)
564 dsl_dataset_t *ds = os->os_dsl_dataset;
566 for (int t = 0; t < TXG_SIZE; t++)
567 ASSERT(!dmu_objset_is_dirty(os, t));
570 if (!dsl_dataset_is_snapshot(ds)) {
571 VERIFY0(dsl_prop_unregister(ds,
572 zfs_prop_to_name(ZFS_PROP_CHECKSUM),
573 checksum_changed_cb, os));
574 VERIFY0(dsl_prop_unregister(ds,
575 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
576 compression_changed_cb, os));
577 VERIFY0(dsl_prop_unregister(ds,
578 zfs_prop_to_name(ZFS_PROP_COPIES),
579 copies_changed_cb, os));
580 VERIFY0(dsl_prop_unregister(ds,
581 zfs_prop_to_name(ZFS_PROP_DEDUP),
582 dedup_changed_cb, os));
583 VERIFY0(dsl_prop_unregister(ds,
584 zfs_prop_to_name(ZFS_PROP_LOGBIAS),
585 logbias_changed_cb, os));
586 VERIFY0(dsl_prop_unregister(ds,
587 zfs_prop_to_name(ZFS_PROP_SYNC),
588 sync_changed_cb, os));
590 VERIFY0(dsl_prop_unregister(ds,
591 zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE),
592 primary_cache_changed_cb, os));
593 VERIFY0(dsl_prop_unregister(ds,
594 zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE),
595 secondary_cache_changed_cb, os));
601 dmu_objset_evict_dbufs(os);
603 dnode_special_close(&os->os_meta_dnode);
604 if (DMU_USERUSED_DNODE(os)) {
605 dnode_special_close(&os->os_userused_dnode);
606 dnode_special_close(&os->os_groupused_dnode);
608 zil_free(os->os_zil);
610 ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
612 VERIFY(arc_buf_remove_ref(os->os_phys_buf, &os->os_phys_buf));
615 * This is a barrier to prevent the objset from going away in
616 * dnode_move() until we can safely ensure that the objset is still in
617 * use. We consider the objset valid before the barrier and invalid
620 rw_enter(&os_lock, RW_READER);
623 mutex_destroy(&os->os_lock);
624 mutex_destroy(&os->os_obj_lock);
625 mutex_destroy(&os->os_user_ptr_lock);
626 kmem_free(os, sizeof (objset_t));
630 dmu_objset_snap_cmtime(objset_t *os)
632 return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
635 /* called from dsl for meta-objset */
637 dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
638 dmu_objset_type_t type, dmu_tx_t *tx)
643 ASSERT(dmu_tx_is_syncing(tx));
646 VERIFY0(dmu_objset_from_ds(ds, &os));
648 VERIFY0(dmu_objset_open_impl(spa, NULL, bp, &os));
650 mdn = DMU_META_DNODE(os);
652 dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
653 DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
656 * We don't want to have to increase the meta-dnode's nlevels
657 * later, because then we could do it in quescing context while
658 * we are also accessing it in open context.
660 * This precaution is not necessary for the MOS (ds == NULL),
661 * because the MOS is only updated in syncing context.
662 * This is most fortunate: the MOS is the only objset that
663 * needs to be synced multiple times as spa_sync() iterates
664 * to convergence, so minimizing its dn_nlevels matters.
670 * Determine the number of levels necessary for the meta-dnode
671 * to contain DN_MAX_OBJECT dnodes.
673 while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
674 (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
675 DN_MAX_OBJECT * sizeof (dnode_phys_t))
678 mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
679 mdn->dn_nlevels = levels;
682 ASSERT(type != DMU_OST_NONE);
683 ASSERT(type != DMU_OST_ANY);
684 ASSERT(type < DMU_OST_NUMTYPES);
685 os->os_phys->os_type = type;
686 if (dmu_objset_userused_enabled(os)) {
687 os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
688 os->os_flags = os->os_phys->os_flags;
691 dsl_dataset_dirty(ds, tx);
696 typedef struct dmu_objset_create_arg {
697 const char *doca_name;
699 void (*doca_userfunc)(objset_t *os, void *arg,
700 cred_t *cr, dmu_tx_t *tx);
702 dmu_objset_type_t doca_type;
704 } dmu_objset_create_arg_t;
708 dmu_objset_create_check(void *arg, dmu_tx_t *tx)
710 dmu_objset_create_arg_t *doca = arg;
711 dsl_pool_t *dp = dmu_tx_pool(tx);
716 if (strchr(doca->doca_name, '@') != NULL)
717 return (SET_ERROR(EINVAL));
719 error = dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail);
723 dsl_dir_rele(pdd, FTAG);
724 return (SET_ERROR(EEXIST));
726 dsl_dir_rele(pdd, FTAG);
732 dmu_objset_create_sync(void *arg, dmu_tx_t *tx)
734 dmu_objset_create_arg_t *doca = arg;
735 dsl_pool_t *dp = dmu_tx_pool(tx);
743 VERIFY0(dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail));
745 obj = dsl_dataset_create_sync(pdd, tail, NULL, doca->doca_flags,
746 doca->doca_cred, tx);
748 VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
749 bp = dsl_dataset_get_blkptr(ds);
750 os = dmu_objset_create_impl(pdd->dd_pool->dp_spa,
751 ds, bp, doca->doca_type, tx);
753 if (doca->doca_userfunc != NULL) {
754 doca->doca_userfunc(os, doca->doca_userarg,
755 doca->doca_cred, tx);
758 spa_history_log_internal_ds(ds, "create", tx, "");
759 dsl_dataset_rele(ds, FTAG);
760 dsl_dir_rele(pdd, FTAG);
764 dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
765 void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
767 dmu_objset_create_arg_t doca;
769 doca.doca_name = name;
770 doca.doca_cred = CRED();
771 doca.doca_flags = flags;
772 doca.doca_userfunc = func;
773 doca.doca_userarg = arg;
774 doca.doca_type = type;
776 return (dsl_sync_task(name,
777 dmu_objset_create_check, dmu_objset_create_sync, &doca, 5));
780 typedef struct dmu_objset_clone_arg {
781 const char *doca_clone;
782 const char *doca_origin;
784 } dmu_objset_clone_arg_t;
788 dmu_objset_clone_check(void *arg, dmu_tx_t *tx)
790 dmu_objset_clone_arg_t *doca = arg;
794 dsl_dataset_t *origin;
795 dsl_pool_t *dp = dmu_tx_pool(tx);
797 if (strchr(doca->doca_clone, '@') != NULL)
798 return (SET_ERROR(EINVAL));
800 error = dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail);
804 dsl_dir_rele(pdd, FTAG);
805 return (SET_ERROR(EEXIST));
807 /* You can't clone across pools. */
808 if (pdd->dd_pool != dp) {
809 dsl_dir_rele(pdd, FTAG);
810 return (SET_ERROR(EXDEV));
812 dsl_dir_rele(pdd, FTAG);
814 error = dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin);
818 /* You can't clone across pools. */
819 if (origin->ds_dir->dd_pool != dp) {
820 dsl_dataset_rele(origin, FTAG);
821 return (SET_ERROR(EXDEV));
824 /* You can only clone snapshots, not the head datasets. */
825 if (!dsl_dataset_is_snapshot(origin)) {
826 dsl_dataset_rele(origin, FTAG);
827 return (SET_ERROR(EINVAL));
829 dsl_dataset_rele(origin, FTAG);
835 dmu_objset_clone_sync(void *arg, dmu_tx_t *tx)
837 dmu_objset_clone_arg_t *doca = arg;
838 dsl_pool_t *dp = dmu_tx_pool(tx);
841 dsl_dataset_t *origin, *ds;
843 char namebuf[MAXNAMELEN];
845 VERIFY0(dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail));
846 VERIFY0(dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin));
848 obj = dsl_dataset_create_sync(pdd, tail, origin, 0,
849 doca->doca_cred, tx);
851 VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
852 dsl_dataset_name(origin, namebuf);
853 spa_history_log_internal_ds(ds, "clone", tx,
854 "origin=%s (%llu)", namebuf, origin->ds_object);
855 dsl_dataset_rele(ds, FTAG);
856 dsl_dataset_rele(origin, FTAG);
857 dsl_dir_rele(pdd, FTAG);
861 dmu_objset_clone(const char *clone, const char *origin)
863 dmu_objset_clone_arg_t doca;
865 doca.doca_clone = clone;
866 doca.doca_origin = origin;
867 doca.doca_cred = CRED();
869 return (dsl_sync_task(clone,
870 dmu_objset_clone_check, dmu_objset_clone_sync, &doca, 5));
874 dmu_objset_snapshot_one(const char *fsname, const char *snapname)
877 char *longsnap = kmem_asprintf("%s@%s", fsname, snapname);
878 nvlist_t *snaps = fnvlist_alloc();
880 fnvlist_add_boolean(snaps, longsnap);
882 err = dsl_dataset_snapshot(snaps, NULL, NULL);
888 dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx)
892 while (dn = list_head(list)) {
893 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
894 ASSERT(dn->dn_dbuf->db_data_pending);
896 * Initialize dn_zio outside dnode_sync() because the
897 * meta-dnode needs to set it ouside dnode_sync().
899 dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
902 ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
903 list_remove(list, dn);
906 (void) dnode_add_ref(dn, newlist);
907 list_insert_tail(newlist, dn);
916 dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
918 blkptr_t *bp = zio->io_bp;
920 dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
922 ASSERT3P(bp, ==, os->os_rootbp);
923 ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_OBJSET);
924 ASSERT0(BP_GET_LEVEL(bp));
927 * Update rootbp fill count: it should be the number of objects
928 * allocated in the object set (not counting the "special"
929 * objects that are stored in the objset_phys_t -- the meta
930 * dnode and user/group accounting objects).
933 for (int i = 0; i < dnp->dn_nblkptr; i++)
934 bp->blk_fill += dnp->dn_blkptr[i].blk_fill;
939 dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
941 blkptr_t *bp = zio->io_bp;
942 blkptr_t *bp_orig = &zio->io_bp_orig;
945 if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
946 ASSERT(BP_EQUAL(bp, bp_orig));
948 dsl_dataset_t *ds = os->os_dsl_dataset;
949 dmu_tx_t *tx = os->os_synctx;
951 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
952 dsl_dataset_block_born(ds, bp, tx);
956 /* called from dsl */
958 dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
965 list_t *newlist = NULL;
966 dbuf_dirty_record_t *dr;
968 dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
970 ASSERT(dmu_tx_is_syncing(tx));
971 /* XXX the write_done callback should really give us the tx... */
974 if (os->os_dsl_dataset == NULL) {
976 * This is the MOS. If we have upgraded,
977 * spa_max_replication() could change, so reset
980 os->os_copies = spa_max_replication(os->os_spa);
984 * Create the root block IO
986 SET_BOOKMARK(&zb, os->os_dsl_dataset ?
987 os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
988 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
989 arc_release(os->os_phys_buf, &os->os_phys_buf);
991 dmu_write_policy(os, NULL, 0, 0, &zp);
993 zio = arc_write(pio, os->os_spa, tx->tx_txg,
994 os->os_rootbp, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os), &zp,
995 dmu_objset_write_ready, dmu_objset_write_done, os,
996 ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
999 * Sync special dnodes - the parent IO for the sync is the root block
1001 DMU_META_DNODE(os)->dn_zio = zio;
1002 dnode_sync(DMU_META_DNODE(os), tx);
1004 os->os_phys->os_flags = os->os_flags;
1006 if (DMU_USERUSED_DNODE(os) &&
1007 DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
1008 DMU_USERUSED_DNODE(os)->dn_zio = zio;
1009 dnode_sync(DMU_USERUSED_DNODE(os), tx);
1010 DMU_GROUPUSED_DNODE(os)->dn_zio = zio;
1011 dnode_sync(DMU_GROUPUSED_DNODE(os), tx);
1014 txgoff = tx->tx_txg & TXG_MASK;
1016 if (dmu_objset_userused_enabled(os)) {
1017 newlist = &os->os_synced_dnodes;
1019 * We must create the list here because it uses the
1020 * dn_dirty_link[] of this txg.
1022 list_create(newlist, sizeof (dnode_t),
1023 offsetof(dnode_t, dn_dirty_link[txgoff]));
1026 dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx);
1027 dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx);
1029 list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff];
1030 while (dr = list_head(list)) {
1031 ASSERT0(dr->dr_dbuf->db_level);
1032 list_remove(list, dr);
1034 zio_nowait(dr->dr_zio);
1037 * Free intent log blocks up to this tx.
1039 zil_sync(os->os_zil, tx);
1040 os->os_phys->os_zil_header = os->os_zil_header;
1045 dmu_objset_is_dirty(objset_t *os, uint64_t txg)
1047 return (!list_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]) ||
1048 !list_is_empty(&os->os_free_dnodes[txg & TXG_MASK]));
1051 static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
1054 dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
1060 dmu_objset_userused_enabled(objset_t *os)
1062 return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
1063 used_cbs[os->os_phys->os_type] != NULL &&
1064 DMU_USERUSED_DNODE(os) != NULL);
1068 do_userquota_update(objset_t *os, uint64_t used, uint64_t flags,
1069 uint64_t user, uint64_t group, boolean_t subtract, dmu_tx_t *tx)
1071 if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) {
1072 int64_t delta = DNODE_SIZE + used;
1075 VERIFY3U(0, ==, zap_increment_int(os, DMU_USERUSED_OBJECT,
1077 VERIFY3U(0, ==, zap_increment_int(os, DMU_GROUPUSED_OBJECT,
1083 dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
1086 list_t *list = &os->os_synced_dnodes;
1088 ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os));
1090 while (dn = list_head(list)) {
1092 ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
1093 ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
1094 dn->dn_phys->dn_flags &
1095 DNODE_FLAG_USERUSED_ACCOUNTED);
1097 /* Allocate the user/groupused objects if necessary. */
1098 if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
1099 VERIFY(0 == zap_create_claim(os,
1100 DMU_USERUSED_OBJECT,
1101 DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1102 VERIFY(0 == zap_create_claim(os,
1103 DMU_GROUPUSED_OBJECT,
1104 DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1108 * We intentionally modify the zap object even if the
1109 * net delta is zero. Otherwise
1110 * the block of the zap obj could be shared between
1111 * datasets but need to be different between them after
1115 flags = dn->dn_id_flags;
1117 if (flags & DN_ID_OLD_EXIST) {
1118 do_userquota_update(os, dn->dn_oldused, dn->dn_oldflags,
1119 dn->dn_olduid, dn->dn_oldgid, B_TRUE, tx);
1121 if (flags & DN_ID_NEW_EXIST) {
1122 do_userquota_update(os, DN_USED_BYTES(dn->dn_phys),
1123 dn->dn_phys->dn_flags, dn->dn_newuid,
1124 dn->dn_newgid, B_FALSE, tx);
1127 mutex_enter(&dn->dn_mtx);
1129 dn->dn_oldflags = 0;
1130 if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
1131 dn->dn_olduid = dn->dn_newuid;
1132 dn->dn_oldgid = dn->dn_newgid;
1133 dn->dn_id_flags |= DN_ID_OLD_EXIST;
1134 if (dn->dn_bonuslen == 0)
1135 dn->dn_id_flags |= DN_ID_CHKED_SPILL;
1137 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1139 dn->dn_id_flags &= ~(DN_ID_NEW_EXIST);
1140 mutex_exit(&dn->dn_mtx);
1142 list_remove(list, dn);
1143 dnode_rele(dn, list);
1148 * Returns a pointer to data to find uid/gid from
1150 * If a dirty record for transaction group that is syncing can't
1151 * be found then NULL is returned. In the NULL case it is assumed
1152 * the uid/gid aren't changing.
1155 dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx)
1157 dbuf_dirty_record_t *dr, **drp;
1160 if (db->db_dirtycnt == 0)
1161 return (db->db.db_data); /* Nothing is changing */
1163 for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
1164 if (dr->dr_txg == tx->tx_txg)
1172 DB_DNODE_ENTER(dr->dr_dbuf);
1173 dn = DB_DNODE(dr->dr_dbuf);
1175 if (dn->dn_bonuslen == 0 &&
1176 dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID)
1177 data = dr->dt.dl.dr_data->b_data;
1179 data = dr->dt.dl.dr_data;
1181 DB_DNODE_EXIT(dr->dr_dbuf);
1188 dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
1190 objset_t *os = dn->dn_objset;
1192 dmu_buf_impl_t *db = NULL;
1193 uint64_t *user = NULL;
1194 uint64_t *group = NULL;
1195 int flags = dn->dn_id_flags;
1197 boolean_t have_spill = B_FALSE;
1199 if (!dmu_objset_userused_enabled(dn->dn_objset))
1202 if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST|
1203 DN_ID_CHKED_SPILL)))
1206 if (before && dn->dn_bonuslen != 0)
1207 data = DN_BONUS(dn->dn_phys);
1208 else if (!before && dn->dn_bonuslen != 0) {
1211 mutex_enter(&db->db_mtx);
1212 data = dmu_objset_userquota_find_data(db, tx);
1214 data = DN_BONUS(dn->dn_phys);
1216 } else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) {
1219 if (RW_WRITE_HELD(&dn->dn_struct_rwlock))
1220 rf |= DB_RF_HAVESTRUCT;
1221 error = dmu_spill_hold_by_dnode(dn,
1222 rf | DB_RF_MUST_SUCCEED,
1223 FTAG, (dmu_buf_t **)&db);
1225 mutex_enter(&db->db_mtx);
1226 data = (before) ? db->db.db_data :
1227 dmu_objset_userquota_find_data(db, tx);
1228 have_spill = B_TRUE;
1230 mutex_enter(&dn->dn_mtx);
1231 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1232 mutex_exit(&dn->dn_mtx);
1238 user = &dn->dn_olduid;
1239 group = &dn->dn_oldgid;
1241 user = &dn->dn_newuid;
1242 group = &dn->dn_newgid;
1246 * Must always call the callback in case the object
1247 * type has changed and that type isn't an object type to track
1249 error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data,
1253 * Preserve existing uid/gid when the callback can't determine
1254 * what the new uid/gid are and the callback returned EEXIST.
1255 * The EEXIST error tells us to just use the existing uid/gid.
1256 * If we don't know what the old values are then just assign
1257 * them to 0, since that is a new file being created.
1259 if (!before && data == NULL && error == EEXIST) {
1260 if (flags & DN_ID_OLD_EXIST) {
1261 dn->dn_newuid = dn->dn_olduid;
1262 dn->dn_newgid = dn->dn_oldgid;
1271 mutex_exit(&db->db_mtx);
1273 mutex_enter(&dn->dn_mtx);
1274 if (error == 0 && before)
1275 dn->dn_id_flags |= DN_ID_OLD_EXIST;
1276 if (error == 0 && !before)
1277 dn->dn_id_flags |= DN_ID_NEW_EXIST;
1280 dn->dn_id_flags |= DN_ID_CHKED_SPILL;
1282 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1284 mutex_exit(&dn->dn_mtx);
1286 dmu_buf_rele((dmu_buf_t *)db, FTAG);
1290 dmu_objset_userspace_present(objset_t *os)
1292 return (os->os_phys->os_flags &
1293 OBJSET_FLAG_USERACCOUNTING_COMPLETE);
1297 dmu_objset_userspace_upgrade(objset_t *os)
1302 if (dmu_objset_userspace_present(os))
1304 if (!dmu_objset_userused_enabled(os))
1305 return (SET_ERROR(ENOTSUP));
1306 if (dmu_objset_is_snapshot(os))
1307 return (SET_ERROR(EINVAL));
1310 * We simply need to mark every object dirty, so that it will be
1311 * synced out and now accounted. If this is called
1312 * concurrently, or if we already did some work before crashing,
1313 * that's fine, since we track each object's accounted state
1317 for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
1322 if (issig(JUSTLOOKING) && issig(FORREAL))
1323 return (SET_ERROR(EINTR));
1325 objerr = dmu_bonus_hold(os, obj, FTAG, &db);
1328 tx = dmu_tx_create(os);
1329 dmu_tx_hold_bonus(tx, obj);
1330 objerr = dmu_tx_assign(tx, TXG_WAIT);
1335 dmu_buf_will_dirty(db, tx);
1336 dmu_buf_rele(db, FTAG);
1340 os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
1341 txg_wait_synced(dmu_objset_pool(os), 0);
1346 dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
1347 uint64_t *usedobjsp, uint64_t *availobjsp)
1349 dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
1350 usedobjsp, availobjsp);
1354 dmu_objset_fsid_guid(objset_t *os)
1356 return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
1360 dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1362 stat->dds_type = os->os_phys->os_type;
1363 if (os->os_dsl_dataset)
1364 dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
1368 dmu_objset_stats(objset_t *os, nvlist_t *nv)
1370 ASSERT(os->os_dsl_dataset ||
1371 os->os_phys->os_type == DMU_OST_META);
1373 if (os->os_dsl_dataset != NULL)
1374 dsl_dataset_stats(os->os_dsl_dataset, nv);
1376 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
1377 os->os_phys->os_type);
1378 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
1379 dmu_objset_userspace_present(os));
1383 dmu_objset_is_snapshot(objset_t *os)
1385 if (os->os_dsl_dataset != NULL)
1386 return (dsl_dataset_is_snapshot(os->os_dsl_dataset));
1392 dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
1393 boolean_t *conflict)
1395 dsl_dataset_t *ds = os->os_dsl_dataset;
1398 if (ds->ds_phys->ds_snapnames_zapobj == 0)
1399 return (SET_ERROR(ENOENT));
1401 return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
1402 ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST,
1403 real, maxlen, conflict));
1407 dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
1408 uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1410 dsl_dataset_t *ds = os->os_dsl_dataset;
1411 zap_cursor_t cursor;
1412 zap_attribute_t attr;
1414 ASSERT(dsl_pool_config_held(dmu_objset_pool(os)));
1416 if (ds->ds_phys->ds_snapnames_zapobj == 0)
1417 return (SET_ERROR(ENOENT));
1419 zap_cursor_init_serialized(&cursor,
1420 ds->ds_dir->dd_pool->dp_meta_objset,
1421 ds->ds_phys->ds_snapnames_zapobj, *offp);
1423 if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1424 zap_cursor_fini(&cursor);
1425 return (SET_ERROR(ENOENT));
1428 if (strlen(attr.za_name) + 1 > namelen) {
1429 zap_cursor_fini(&cursor);
1430 return (SET_ERROR(ENAMETOOLONG));
1433 (void) strcpy(name, attr.za_name);
1435 *idp = attr.za_first_integer;
1437 *case_conflict = attr.za_normalization_conflict;
1438 zap_cursor_advance(&cursor);
1439 *offp = zap_cursor_serialize(&cursor);
1440 zap_cursor_fini(&cursor);
1446 dmu_dir_list_next(objset_t *os, int namelen, char *name,
1447 uint64_t *idp, uint64_t *offp)
1449 dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
1450 zap_cursor_t cursor;
1451 zap_attribute_t attr;
1453 /* there is no next dir on a snapshot! */
1454 if (os->os_dsl_dataset->ds_object !=
1455 dd->dd_phys->dd_head_dataset_obj)
1456 return (SET_ERROR(ENOENT));
1458 zap_cursor_init_serialized(&cursor,
1459 dd->dd_pool->dp_meta_objset,
1460 dd->dd_phys->dd_child_dir_zapobj, *offp);
1462 if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1463 zap_cursor_fini(&cursor);
1464 return (SET_ERROR(ENOENT));
1467 if (strlen(attr.za_name) + 1 > namelen) {
1468 zap_cursor_fini(&cursor);
1469 return (SET_ERROR(ENAMETOOLONG));
1472 (void) strcpy(name, attr.za_name);
1474 *idp = attr.za_first_integer;
1475 zap_cursor_advance(&cursor);
1476 *offp = zap_cursor_serialize(&cursor);
1477 zap_cursor_fini(&cursor);
1483 * Find objsets under and including ddobj, call func(ds) on each.
1486 dmu_objset_find_dp(dsl_pool_t *dp, uint64_t ddobj,
1487 int func(dsl_pool_t *, dsl_dataset_t *, void *), void *arg, int flags)
1492 zap_attribute_t *attr;
1496 ASSERT(dsl_pool_config_held(dp));
1498 err = dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd);
1502 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1503 if (dd->dd_myname[0] == '$') {
1504 dsl_dir_rele(dd, FTAG);
1508 thisobj = dd->dd_phys->dd_head_dataset_obj;
1509 attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1512 * Iterate over all children.
1514 if (flags & DS_FIND_CHILDREN) {
1515 for (zap_cursor_init(&zc, dp->dp_meta_objset,
1516 dd->dd_phys->dd_child_dir_zapobj);
1517 zap_cursor_retrieve(&zc, attr) == 0;
1518 (void) zap_cursor_advance(&zc)) {
1519 ASSERT3U(attr->za_integer_length, ==,
1521 ASSERT3U(attr->za_num_integers, ==, 1);
1523 err = dmu_objset_find_dp(dp, attr->za_first_integer,
1528 zap_cursor_fini(&zc);
1531 dsl_dir_rele(dd, FTAG);
1532 kmem_free(attr, sizeof (zap_attribute_t));
1538 * Iterate over all snapshots.
1540 if (flags & DS_FIND_SNAPSHOTS) {
1542 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1545 uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
1546 dsl_dataset_rele(ds, FTAG);
1548 for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
1549 zap_cursor_retrieve(&zc, attr) == 0;
1550 (void) zap_cursor_advance(&zc)) {
1551 ASSERT3U(attr->za_integer_length, ==,
1553 ASSERT3U(attr->za_num_integers, ==, 1);
1555 err = dsl_dataset_hold_obj(dp,
1556 attr->za_first_integer, FTAG, &ds);
1559 err = func(dp, ds, arg);
1560 dsl_dataset_rele(ds, FTAG);
1564 zap_cursor_fini(&zc);
1568 dsl_dir_rele(dd, FTAG);
1569 kmem_free(attr, sizeof (zap_attribute_t));
1577 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1580 err = func(dp, ds, arg);
1581 dsl_dataset_rele(ds, FTAG);
1586 * Find all objsets under name, and for each, call 'func(child_name, arg)'.
1587 * The dp_config_rwlock must not be held when this is called, and it
1588 * will not be held when the callback is called.
1589 * Therefore this function should only be used when the pool is not changing
1590 * (e.g. in syncing context), or the callback can deal with the possible races.
1593 dmu_objset_find_impl(spa_t *spa, const char *name,
1594 int func(const char *, void *), void *arg, int flags)
1597 dsl_pool_t *dp = spa_get_dsl(spa);
1600 zap_attribute_t *attr;
1605 dsl_pool_config_enter(dp, FTAG);
1607 err = dsl_dir_hold(dp, name, FTAG, &dd, NULL);
1609 dsl_pool_config_exit(dp, FTAG);
1613 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1614 if (dd->dd_myname[0] == '$') {
1615 dsl_dir_rele(dd, FTAG);
1616 dsl_pool_config_exit(dp, FTAG);
1620 thisobj = dd->dd_phys->dd_head_dataset_obj;
1621 attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1624 * Iterate over all children.
1626 if (flags & DS_FIND_CHILDREN) {
1627 for (zap_cursor_init(&zc, dp->dp_meta_objset,
1628 dd->dd_phys->dd_child_dir_zapobj);
1629 zap_cursor_retrieve(&zc, attr) == 0;
1630 (void) zap_cursor_advance(&zc)) {
1631 ASSERT3U(attr->za_integer_length, ==,
1633 ASSERT3U(attr->za_num_integers, ==, 1);
1635 child = kmem_asprintf("%s/%s", name, attr->za_name);
1636 dsl_pool_config_exit(dp, FTAG);
1637 err = dmu_objset_find_impl(spa, child,
1639 dsl_pool_config_enter(dp, FTAG);
1644 zap_cursor_fini(&zc);
1647 dsl_dir_rele(dd, FTAG);
1648 dsl_pool_config_exit(dp, FTAG);
1649 kmem_free(attr, sizeof (zap_attribute_t));
1655 * Iterate over all snapshots.
1657 if (flags & DS_FIND_SNAPSHOTS) {
1658 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1661 uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
1662 dsl_dataset_rele(ds, FTAG);
1664 for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
1665 zap_cursor_retrieve(&zc, attr) == 0;
1666 (void) zap_cursor_advance(&zc)) {
1667 ASSERT3U(attr->za_integer_length, ==,
1669 ASSERT3U(attr->za_num_integers, ==, 1);
1671 child = kmem_asprintf("%s@%s",
1672 name, attr->za_name);
1673 dsl_pool_config_exit(dp, FTAG);
1674 err = func(child, arg);
1675 dsl_pool_config_enter(dp, FTAG);
1680 zap_cursor_fini(&zc);
1684 dsl_dir_rele(dd, FTAG);
1685 kmem_free(attr, sizeof (zap_attribute_t));
1686 dsl_pool_config_exit(dp, FTAG);
1691 /* Apply to self. */
1692 return (func(name, arg));
1696 * See comment above dmu_objset_find_impl().
1699 dmu_objset_find(char *name, int func(const char *, void *), void *arg,
1705 error = spa_open(name, &spa, FTAG);
1708 error = dmu_objset_find_impl(spa, name, func, arg, flags);
1709 spa_close(spa, FTAG);
1714 dmu_objset_set_user(objset_t *os, void *user_ptr)
1716 ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1717 os->os_user_ptr = user_ptr;
1721 dmu_objset_get_user(objset_t *os)
1723 ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1724 return (os->os_user_ptr);
1728 * Determine name of filesystem, given name of snapshot.
1729 * buf must be at least MAXNAMELEN bytes
1732 dmu_fsname(const char *snapname, char *buf)
1734 char *atp = strchr(snapname, '@');
1736 return (SET_ERROR(EINVAL));
1737 if (atp - snapname >= MAXNAMELEN)
1738 return (SET_ERROR(ENAMETOOLONG));
1739 (void) strlcpy(buf, snapname, atp - snapname + 1);