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, 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.
28 /* Portions Copyright 2010 Robert Milkowski */
31 #include <sys/zfs_context.h>
32 #include <sys/dmu_objset.h>
33 #include <sys/dsl_dir.h>
34 #include <sys/dsl_dataset.h>
35 #include <sys/dsl_prop.h>
36 #include <sys/dsl_pool.h>
37 #include <sys/dsl_synctask.h>
38 #include <sys/dsl_deleg.h>
39 #include <sys/dnode.h>
42 #include <sys/dmu_tx.h>
45 #include <sys/dmu_impl.h>
46 #include <sys/zfs_ioctl.h>
48 #include <sys/zfs_onexit.h>
49 #include <sys/dsl_destroy.h>
52 * Needed to close a window in dnode_move() that allows the objset to be freed
53 * before it can be safely accessed.
60 rw_init(&os_lock, NULL, RW_DEFAULT, NULL);
70 dmu_objset_spa(objset_t *os)
76 dmu_objset_zil(objset_t *os)
82 dmu_objset_pool(objset_t *os)
86 if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir)
87 return (ds->ds_dir->dd_pool);
89 return (spa_get_dsl(os->os_spa));
93 dmu_objset_ds(objset_t *os)
95 return (os->os_dsl_dataset);
99 dmu_objset_type(objset_t *os)
101 return (os->os_phys->os_type);
105 dmu_objset_name(objset_t *os, char *buf)
107 dsl_dataset_name(os->os_dsl_dataset, buf);
111 dmu_objset_id(objset_t *os)
113 dsl_dataset_t *ds = os->os_dsl_dataset;
115 return (ds ? ds->ds_object : 0);
119 dmu_objset_syncprop(objset_t *os)
121 return (os->os_sync);
125 dmu_objset_logbias(objset_t *os)
127 return (os->os_logbias);
131 checksum_changed_cb(void *arg, uint64_t newval)
136 * Inheritance should have been done by now.
138 ASSERT(newval != ZIO_CHECKSUM_INHERIT);
140 os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
144 compression_changed_cb(void *arg, uint64_t newval)
149 * Inheritance and range checking should have been done by now.
151 ASSERT(newval != ZIO_COMPRESS_INHERIT);
153 os->os_compress = zio_compress_select(newval, ZIO_COMPRESS_ON_VALUE);
157 copies_changed_cb(void *arg, uint64_t newval)
162 * Inheritance and range checking should have been done by now.
165 ASSERT(newval <= spa_max_replication(os->os_spa));
167 os->os_copies = newval;
171 dedup_changed_cb(void *arg, uint64_t newval)
174 spa_t *spa = os->os_spa;
175 enum zio_checksum checksum;
178 * Inheritance should have been done by now.
180 ASSERT(newval != ZIO_CHECKSUM_INHERIT);
182 checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF);
184 os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK;
185 os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY);
189 primary_cache_changed_cb(void *arg, uint64_t newval)
194 * Inheritance and range checking should have been done by now.
196 ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
197 newval == ZFS_CACHE_METADATA);
199 os->os_primary_cache = newval;
203 secondary_cache_changed_cb(void *arg, uint64_t newval)
208 * Inheritance and range checking should have been done by now.
210 ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
211 newval == ZFS_CACHE_METADATA);
213 os->os_secondary_cache = newval;
217 sync_changed_cb(void *arg, uint64_t newval)
222 * Inheritance and range checking should have been done by now.
224 ASSERT(newval == ZFS_SYNC_STANDARD || newval == ZFS_SYNC_ALWAYS ||
225 newval == ZFS_SYNC_DISABLED);
227 os->os_sync = newval;
229 zil_set_sync(os->os_zil, newval);
233 redundant_metadata_changed_cb(void *arg, uint64_t newval)
238 * Inheritance and range checking should have been done by now.
240 ASSERT(newval == ZFS_REDUNDANT_METADATA_ALL ||
241 newval == ZFS_REDUNDANT_METADATA_MOST);
243 os->os_redundant_metadata = newval;
247 logbias_changed_cb(void *arg, uint64_t newval)
251 ASSERT(newval == ZFS_LOGBIAS_LATENCY ||
252 newval == ZFS_LOGBIAS_THROUGHPUT);
253 os->os_logbias = newval;
255 zil_set_logbias(os->os_zil, newval);
259 recordsize_changed_cb(void *arg, uint64_t newval)
263 os->os_recordsize = newval;
267 dmu_objset_byteswap(void *buf, size_t size)
269 objset_phys_t *osp = buf;
271 ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t));
272 dnode_byteswap(&osp->os_meta_dnode);
273 byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
274 osp->os_type = BSWAP_64(osp->os_type);
275 osp->os_flags = BSWAP_64(osp->os_flags);
276 if (size == sizeof (objset_phys_t)) {
277 dnode_byteswap(&osp->os_userused_dnode);
278 dnode_byteswap(&osp->os_groupused_dnode);
283 dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
289 ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
291 os = kmem_zalloc(sizeof (objset_t), KM_SLEEP);
292 os->os_dsl_dataset = ds;
295 if (!BP_IS_HOLE(os->os_rootbp)) {
296 uint32_t aflags = ARC_WAIT;
298 SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
299 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
301 if (DMU_OS_IS_L2CACHEABLE(os))
302 aflags |= ARC_L2CACHE;
303 if (DMU_OS_IS_L2COMPRESSIBLE(os))
304 aflags |= ARC_L2COMPRESS;
306 dprintf_bp(os->os_rootbp, "reading %s", "");
307 err = arc_read(NULL, spa, os->os_rootbp,
308 arc_getbuf_func, &os->os_phys_buf,
309 ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
311 kmem_free(os, sizeof (objset_t));
312 /* convert checksum errors into IO errors */
314 err = SET_ERROR(EIO);
318 /* Increase the blocksize if we are permitted. */
319 if (spa_version(spa) >= SPA_VERSION_USERSPACE &&
320 arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) {
321 arc_buf_t *buf = arc_buf_alloc(spa,
322 sizeof (objset_phys_t), &os->os_phys_buf,
324 bzero(buf->b_data, sizeof (objset_phys_t));
325 bcopy(os->os_phys_buf->b_data, buf->b_data,
326 arc_buf_size(os->os_phys_buf));
327 (void) arc_buf_remove_ref(os->os_phys_buf,
329 os->os_phys_buf = buf;
332 os->os_phys = os->os_phys_buf->b_data;
333 os->os_flags = os->os_phys->os_flags;
335 int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
336 sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE;
337 os->os_phys_buf = arc_buf_alloc(spa, size,
338 &os->os_phys_buf, ARC_BUFC_METADATA);
339 os->os_phys = os->os_phys_buf->b_data;
340 bzero(os->os_phys, size);
344 * Note: the changed_cb will be called once before the register
345 * func returns, thus changing the checksum/compression from the
346 * default (fletcher2/off). Snapshots don't need to know about
347 * checksum/compression/copies.
350 err = dsl_prop_register(ds,
351 zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE),
352 primary_cache_changed_cb, os);
354 err = dsl_prop_register(ds,
355 zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE),
356 secondary_cache_changed_cb, os);
358 if (!dsl_dataset_is_snapshot(ds)) {
360 err = dsl_prop_register(ds,
361 zfs_prop_to_name(ZFS_PROP_CHECKSUM),
362 checksum_changed_cb, os);
365 err = dsl_prop_register(ds,
366 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
367 compression_changed_cb, os);
370 err = dsl_prop_register(ds,
371 zfs_prop_to_name(ZFS_PROP_COPIES),
372 copies_changed_cb, os);
375 err = dsl_prop_register(ds,
376 zfs_prop_to_name(ZFS_PROP_DEDUP),
377 dedup_changed_cb, os);
380 err = dsl_prop_register(ds,
381 zfs_prop_to_name(ZFS_PROP_LOGBIAS),
382 logbias_changed_cb, os);
385 err = dsl_prop_register(ds,
386 zfs_prop_to_name(ZFS_PROP_SYNC),
387 sync_changed_cb, os);
390 err = dsl_prop_register(ds,
392 ZFS_PROP_REDUNDANT_METADATA),
393 redundant_metadata_changed_cb, os);
396 err = dsl_prop_register(ds,
397 zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
398 recordsize_changed_cb, os);
402 VERIFY(arc_buf_remove_ref(os->os_phys_buf,
404 kmem_free(os, sizeof (objset_t));
408 /* It's the meta-objset. */
409 os->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
410 os->os_compress = ZIO_COMPRESS_LZJB;
411 os->os_copies = spa_max_replication(spa);
412 os->os_dedup_checksum = ZIO_CHECKSUM_OFF;
413 os->os_dedup_verify = B_FALSE;
414 os->os_logbias = ZFS_LOGBIAS_LATENCY;
415 os->os_sync = ZFS_SYNC_STANDARD;
416 os->os_primary_cache = ZFS_CACHE_ALL;
417 os->os_secondary_cache = ZFS_CACHE_ALL;
420 if (ds == NULL || !dsl_dataset_is_snapshot(ds))
421 os->os_zil_header = os->os_phys->os_zil_header;
422 os->os_zil = zil_alloc(os, &os->os_zil_header);
424 for (i = 0; i < TXG_SIZE; i++) {
425 list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t),
426 offsetof(dnode_t, dn_dirty_link[i]));
427 list_create(&os->os_free_dnodes[i], sizeof (dnode_t),
428 offsetof(dnode_t, dn_dirty_link[i]));
430 list_create(&os->os_dnodes, sizeof (dnode_t),
431 offsetof(dnode_t, dn_link));
432 list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
433 offsetof(dmu_buf_impl_t, db_link));
435 mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
436 mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
437 mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
439 DMU_META_DNODE(os) = dnode_special_open(os,
440 &os->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT,
442 if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) {
443 DMU_USERUSED_DNODE(os) = dnode_special_open(os,
444 &os->os_phys->os_userused_dnode, DMU_USERUSED_OBJECT,
445 &os->os_userused_dnode);
446 DMU_GROUPUSED_DNODE(os) = dnode_special_open(os,
447 &os->os_phys->os_groupused_dnode, DMU_GROUPUSED_OBJECT,
448 &os->os_groupused_dnode);
456 dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
460 mutex_enter(&ds->ds_opening_lock);
461 if (ds->ds_objset == NULL) {
463 err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
464 ds, dsl_dataset_get_blkptr(ds), &os);
467 mutex_enter(&ds->ds_lock);
468 ASSERT(ds->ds_objset == NULL);
470 mutex_exit(&ds->ds_lock);
473 *osp = ds->ds_objset;
474 mutex_exit(&ds->ds_opening_lock);
479 * Holds the pool while the objset is held. Therefore only one objset
480 * can be held at a time.
483 dmu_objset_hold(const char *name, void *tag, objset_t **osp)
489 err = dsl_pool_hold(name, tag, &dp);
492 err = dsl_dataset_hold(dp, name, tag, &ds);
494 dsl_pool_rele(dp, tag);
498 err = dmu_objset_from_ds(ds, osp);
500 dsl_dataset_rele(ds, tag);
501 dsl_pool_rele(dp, tag);
508 * dsl_pool must not be held when this is called.
509 * Upon successful return, there will be a longhold on the dataset,
510 * and the dsl_pool will not be held.
513 dmu_objset_own(const char *name, dmu_objset_type_t type,
514 boolean_t readonly, void *tag, objset_t **osp)
520 err = dsl_pool_hold(name, FTAG, &dp);
523 err = dsl_dataset_own(dp, name, tag, &ds);
525 dsl_pool_rele(dp, FTAG);
529 err = dmu_objset_from_ds(ds, osp);
530 dsl_pool_rele(dp, FTAG);
532 dsl_dataset_disown(ds, tag);
533 } else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) {
534 dsl_dataset_disown(ds, tag);
535 return (SET_ERROR(EINVAL));
536 } else if (!readonly && dsl_dataset_is_snapshot(ds)) {
537 dsl_dataset_disown(ds, tag);
538 return (SET_ERROR(EROFS));
544 dmu_objset_rele(objset_t *os, void *tag)
546 dsl_pool_t *dp = dmu_objset_pool(os);
547 dsl_dataset_rele(os->os_dsl_dataset, tag);
548 dsl_pool_rele(dp, tag);
552 * When we are called, os MUST refer to an objset associated with a dataset
553 * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner
554 * == tag. We will then release and reacquire ownership of the dataset while
555 * holding the pool config_rwlock to avoid intervening namespace or ownership
558 * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to
559 * release the hold on its dataset and acquire a new one on the dataset of the
560 * same name so that it can be partially torn down and reconstructed.
563 dmu_objset_refresh_ownership(objset_t *os, void *tag)
566 dsl_dataset_t *ds, *newds;
567 char name[MAXNAMELEN];
569 ds = os->os_dsl_dataset;
570 VERIFY3P(ds, !=, NULL);
571 VERIFY3P(ds->ds_owner, ==, tag);
572 VERIFY(dsl_dataset_long_held(ds));
574 dsl_dataset_name(ds, name);
575 dp = dmu_objset_pool(os);
576 dsl_pool_config_enter(dp, FTAG);
577 dmu_objset_disown(os, tag);
578 VERIFY0(dsl_dataset_own(dp, name, tag, &newds));
579 VERIFY3P(newds, ==, os->os_dsl_dataset);
580 dsl_pool_config_exit(dp, FTAG);
584 dmu_objset_disown(objset_t *os, void *tag)
586 dsl_dataset_disown(os->os_dsl_dataset, tag);
590 dmu_objset_evict_dbufs(objset_t *os)
594 mutex_enter(&os->os_lock);
596 /* process the mdn last, since the other dnodes have holds on it */
597 list_remove(&os->os_dnodes, DMU_META_DNODE(os));
598 list_insert_tail(&os->os_dnodes, DMU_META_DNODE(os));
601 * Find the first dnode with holds. We have to do this dance
602 * because dnode_add_ref() only works if you already have a
603 * hold. If there are no holds then it has no dbufs so OK to
606 for (dn = list_head(&os->os_dnodes);
607 dn && !dnode_add_ref(dn, FTAG);
608 dn = list_next(&os->os_dnodes, dn))
612 dnode_t *next_dn = dn;
615 next_dn = list_next(&os->os_dnodes, next_dn);
616 } while (next_dn && !dnode_add_ref(next_dn, FTAG));
618 mutex_exit(&os->os_lock);
619 dnode_evict_dbufs(dn);
620 dnode_rele(dn, FTAG);
621 mutex_enter(&os->os_lock);
624 mutex_exit(&os->os_lock);
628 dmu_objset_evict(objset_t *os)
630 dsl_dataset_t *ds = os->os_dsl_dataset;
632 for (int t = 0; t < TXG_SIZE; t++)
633 ASSERT(!dmu_objset_is_dirty(os, t));
636 if (!dsl_dataset_is_snapshot(ds)) {
637 VERIFY0(dsl_prop_unregister(ds,
638 zfs_prop_to_name(ZFS_PROP_CHECKSUM),
639 checksum_changed_cb, os));
640 VERIFY0(dsl_prop_unregister(ds,
641 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
642 compression_changed_cb, os));
643 VERIFY0(dsl_prop_unregister(ds,
644 zfs_prop_to_name(ZFS_PROP_COPIES),
645 copies_changed_cb, os));
646 VERIFY0(dsl_prop_unregister(ds,
647 zfs_prop_to_name(ZFS_PROP_DEDUP),
648 dedup_changed_cb, os));
649 VERIFY0(dsl_prop_unregister(ds,
650 zfs_prop_to_name(ZFS_PROP_LOGBIAS),
651 logbias_changed_cb, os));
652 VERIFY0(dsl_prop_unregister(ds,
653 zfs_prop_to_name(ZFS_PROP_SYNC),
654 sync_changed_cb, os));
655 VERIFY0(dsl_prop_unregister(ds,
656 zfs_prop_to_name(ZFS_PROP_REDUNDANT_METADATA),
657 redundant_metadata_changed_cb, os));
658 VERIFY0(dsl_prop_unregister(ds,
659 zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
660 recordsize_changed_cb, os));
662 VERIFY0(dsl_prop_unregister(ds,
663 zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE),
664 primary_cache_changed_cb, os));
665 VERIFY0(dsl_prop_unregister(ds,
666 zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE),
667 secondary_cache_changed_cb, os));
673 dmu_objset_evict_dbufs(os);
675 dnode_special_close(&os->os_meta_dnode);
676 if (DMU_USERUSED_DNODE(os)) {
677 dnode_special_close(&os->os_userused_dnode);
678 dnode_special_close(&os->os_groupused_dnode);
680 zil_free(os->os_zil);
682 ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
684 VERIFY(arc_buf_remove_ref(os->os_phys_buf, &os->os_phys_buf));
687 * This is a barrier to prevent the objset from going away in
688 * dnode_move() until we can safely ensure that the objset is still in
689 * use. We consider the objset valid before the barrier and invalid
692 rw_enter(&os_lock, RW_READER);
695 mutex_destroy(&os->os_lock);
696 mutex_destroy(&os->os_obj_lock);
697 mutex_destroy(&os->os_user_ptr_lock);
698 kmem_free(os, sizeof (objset_t));
702 dmu_objset_snap_cmtime(objset_t *os)
704 return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
707 /* called from dsl for meta-objset */
709 dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
710 dmu_objset_type_t type, dmu_tx_t *tx)
715 ASSERT(dmu_tx_is_syncing(tx));
718 VERIFY0(dmu_objset_from_ds(ds, &os));
720 VERIFY0(dmu_objset_open_impl(spa, NULL, bp, &os));
722 mdn = DMU_META_DNODE(os);
724 dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
725 DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
728 * We don't want to have to increase the meta-dnode's nlevels
729 * later, because then we could do it in quescing context while
730 * we are also accessing it in open context.
732 * This precaution is not necessary for the MOS (ds == NULL),
733 * because the MOS is only updated in syncing context.
734 * This is most fortunate: the MOS is the only objset that
735 * needs to be synced multiple times as spa_sync() iterates
736 * to convergence, so minimizing its dn_nlevels matters.
742 * Determine the number of levels necessary for the meta-dnode
743 * to contain DN_MAX_OBJECT dnodes.
745 while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
746 (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
747 DN_MAX_OBJECT * sizeof (dnode_phys_t))
750 mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
751 mdn->dn_nlevels = levels;
754 ASSERT(type != DMU_OST_NONE);
755 ASSERT(type != DMU_OST_ANY);
756 ASSERT(type < DMU_OST_NUMTYPES);
757 os->os_phys->os_type = type;
758 if (dmu_objset_userused_enabled(os)) {
759 os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
760 os->os_flags = os->os_phys->os_flags;
763 dsl_dataset_dirty(ds, tx);
768 typedef struct dmu_objset_create_arg {
769 const char *doca_name;
771 void (*doca_userfunc)(objset_t *os, void *arg,
772 cred_t *cr, dmu_tx_t *tx);
774 dmu_objset_type_t doca_type;
776 } dmu_objset_create_arg_t;
780 dmu_objset_create_check(void *arg, dmu_tx_t *tx)
782 dmu_objset_create_arg_t *doca = arg;
783 dsl_pool_t *dp = dmu_tx_pool(tx);
788 if (strchr(doca->doca_name, '@') != NULL)
789 return (SET_ERROR(EINVAL));
791 error = dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail);
795 dsl_dir_rele(pdd, FTAG);
796 return (SET_ERROR(EEXIST));
798 error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
800 dsl_dir_rele(pdd, FTAG);
806 dmu_objset_create_sync(void *arg, dmu_tx_t *tx)
808 dmu_objset_create_arg_t *doca = arg;
809 dsl_pool_t *dp = dmu_tx_pool(tx);
817 VERIFY0(dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail));
819 obj = dsl_dataset_create_sync(pdd, tail, NULL, doca->doca_flags,
820 doca->doca_cred, tx);
822 VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
823 bp = dsl_dataset_get_blkptr(ds);
824 os = dmu_objset_create_impl(pdd->dd_pool->dp_spa,
825 ds, bp, doca->doca_type, tx);
827 if (doca->doca_userfunc != NULL) {
828 doca->doca_userfunc(os, doca->doca_userarg,
829 doca->doca_cred, tx);
832 spa_history_log_internal_ds(ds, "create", tx, "");
833 dsl_dataset_rele(ds, FTAG);
834 dsl_dir_rele(pdd, FTAG);
838 dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
839 void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
841 dmu_objset_create_arg_t doca;
843 doca.doca_name = name;
844 doca.doca_cred = CRED();
845 doca.doca_flags = flags;
846 doca.doca_userfunc = func;
847 doca.doca_userarg = arg;
848 doca.doca_type = type;
850 return (dsl_sync_task(name,
851 dmu_objset_create_check, dmu_objset_create_sync, &doca,
852 5, ZFS_SPACE_CHECK_NORMAL));
855 typedef struct dmu_objset_clone_arg {
856 const char *doca_clone;
857 const char *doca_origin;
859 } dmu_objset_clone_arg_t;
863 dmu_objset_clone_check(void *arg, dmu_tx_t *tx)
865 dmu_objset_clone_arg_t *doca = arg;
869 dsl_dataset_t *origin;
870 dsl_pool_t *dp = dmu_tx_pool(tx);
872 if (strchr(doca->doca_clone, '@') != NULL)
873 return (SET_ERROR(EINVAL));
875 error = dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail);
879 dsl_dir_rele(pdd, FTAG);
880 return (SET_ERROR(EEXIST));
882 /* You can't clone across pools. */
883 if (pdd->dd_pool != dp) {
884 dsl_dir_rele(pdd, FTAG);
885 return (SET_ERROR(EXDEV));
887 error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
890 dsl_dir_rele(pdd, FTAG);
891 return (SET_ERROR(EDQUOT));
893 dsl_dir_rele(pdd, FTAG);
895 error = dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin);
899 /* You can't clone across pools. */
900 if (origin->ds_dir->dd_pool != dp) {
901 dsl_dataset_rele(origin, FTAG);
902 return (SET_ERROR(EXDEV));
905 /* You can only clone snapshots, not the head datasets. */
906 if (!dsl_dataset_is_snapshot(origin)) {
907 dsl_dataset_rele(origin, FTAG);
908 return (SET_ERROR(EINVAL));
910 dsl_dataset_rele(origin, FTAG);
916 dmu_objset_clone_sync(void *arg, dmu_tx_t *tx)
918 dmu_objset_clone_arg_t *doca = arg;
919 dsl_pool_t *dp = dmu_tx_pool(tx);
922 dsl_dataset_t *origin, *ds;
924 char namebuf[MAXNAMELEN];
926 VERIFY0(dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail));
927 VERIFY0(dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin));
929 obj = dsl_dataset_create_sync(pdd, tail, origin, 0,
930 doca->doca_cred, tx);
932 VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
933 dsl_dataset_name(origin, namebuf);
934 spa_history_log_internal_ds(ds, "clone", tx,
935 "origin=%s (%llu)", namebuf, origin->ds_object);
936 dsl_dataset_rele(ds, FTAG);
937 dsl_dataset_rele(origin, FTAG);
938 dsl_dir_rele(pdd, FTAG);
942 dmu_objset_clone(const char *clone, const char *origin)
944 dmu_objset_clone_arg_t doca;
946 doca.doca_clone = clone;
947 doca.doca_origin = origin;
948 doca.doca_cred = CRED();
950 return (dsl_sync_task(clone,
951 dmu_objset_clone_check, dmu_objset_clone_sync, &doca,
952 5, ZFS_SPACE_CHECK_NORMAL));
956 dmu_objset_snapshot_one(const char *fsname, const char *snapname)
959 char *longsnap = kmem_asprintf("%s@%s", fsname, snapname);
960 nvlist_t *snaps = fnvlist_alloc();
962 fnvlist_add_boolean(snaps, longsnap);
964 err = dsl_dataset_snapshot(snaps, NULL, NULL);
970 dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx)
974 while (dn = list_head(list)) {
975 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
976 ASSERT(dn->dn_dbuf->db_data_pending);
978 * Initialize dn_zio outside dnode_sync() because the
979 * meta-dnode needs to set it ouside dnode_sync().
981 dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
984 ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
985 list_remove(list, dn);
988 (void) dnode_add_ref(dn, newlist);
989 list_insert_tail(newlist, dn);
998 dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
1000 blkptr_t *bp = zio->io_bp;
1002 dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
1004 ASSERT(!BP_IS_EMBEDDED(bp));
1005 ASSERT3P(bp, ==, os->os_rootbp);
1006 ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_OBJSET);
1007 ASSERT0(BP_GET_LEVEL(bp));
1010 * Update rootbp fill count: it should be the number of objects
1011 * allocated in the object set (not counting the "special"
1012 * objects that are stored in the objset_phys_t -- the meta
1013 * dnode and user/group accounting objects).
1016 for (int i = 0; i < dnp->dn_nblkptr; i++)
1017 bp->blk_fill += BP_GET_FILL(&dnp->dn_blkptr[i]);
1022 dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
1024 blkptr_t *bp = zio->io_bp;
1025 blkptr_t *bp_orig = &zio->io_bp_orig;
1028 if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
1029 ASSERT(BP_EQUAL(bp, bp_orig));
1031 dsl_dataset_t *ds = os->os_dsl_dataset;
1032 dmu_tx_t *tx = os->os_synctx;
1034 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
1035 dsl_dataset_block_born(ds, bp, tx);
1039 /* called from dsl */
1041 dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
1044 zbookmark_phys_t zb;
1048 list_t *newlist = NULL;
1049 dbuf_dirty_record_t *dr;
1051 dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
1053 ASSERT(dmu_tx_is_syncing(tx));
1054 /* XXX the write_done callback should really give us the tx... */
1057 if (os->os_dsl_dataset == NULL) {
1059 * This is the MOS. If we have upgraded,
1060 * spa_max_replication() could change, so reset
1063 os->os_copies = spa_max_replication(os->os_spa);
1067 * Create the root block IO
1069 SET_BOOKMARK(&zb, os->os_dsl_dataset ?
1070 os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
1071 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
1072 arc_release(os->os_phys_buf, &os->os_phys_buf);
1074 dmu_write_policy(os, NULL, 0, 0, &zp);
1076 zio = arc_write(pio, os->os_spa, tx->tx_txg,
1077 os->os_rootbp, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os),
1078 DMU_OS_IS_L2COMPRESSIBLE(os), &zp, dmu_objset_write_ready,
1079 NULL, dmu_objset_write_done, os, ZIO_PRIORITY_ASYNC_WRITE,
1080 ZIO_FLAG_MUSTSUCCEED, &zb);
1083 * Sync special dnodes - the parent IO for the sync is the root block
1085 DMU_META_DNODE(os)->dn_zio = zio;
1086 dnode_sync(DMU_META_DNODE(os), tx);
1088 os->os_phys->os_flags = os->os_flags;
1090 if (DMU_USERUSED_DNODE(os) &&
1091 DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
1092 DMU_USERUSED_DNODE(os)->dn_zio = zio;
1093 dnode_sync(DMU_USERUSED_DNODE(os), tx);
1094 DMU_GROUPUSED_DNODE(os)->dn_zio = zio;
1095 dnode_sync(DMU_GROUPUSED_DNODE(os), tx);
1098 txgoff = tx->tx_txg & TXG_MASK;
1100 if (dmu_objset_userused_enabled(os)) {
1101 newlist = &os->os_synced_dnodes;
1103 * We must create the list here because it uses the
1104 * dn_dirty_link[] of this txg.
1106 list_create(newlist, sizeof (dnode_t),
1107 offsetof(dnode_t, dn_dirty_link[txgoff]));
1110 dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx);
1111 dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx);
1113 list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff];
1114 while (dr = list_head(list)) {
1115 ASSERT0(dr->dr_dbuf->db_level);
1116 list_remove(list, dr);
1118 zio_nowait(dr->dr_zio);
1121 * Free intent log blocks up to this tx.
1123 zil_sync(os->os_zil, tx);
1124 os->os_phys->os_zil_header = os->os_zil_header;
1129 dmu_objset_is_dirty(objset_t *os, uint64_t txg)
1131 return (!list_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]) ||
1132 !list_is_empty(&os->os_free_dnodes[txg & TXG_MASK]));
1135 static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
1138 dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
1144 dmu_objset_userused_enabled(objset_t *os)
1146 return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
1147 used_cbs[os->os_phys->os_type] != NULL &&
1148 DMU_USERUSED_DNODE(os) != NULL);
1152 do_userquota_update(objset_t *os, uint64_t used, uint64_t flags,
1153 uint64_t user, uint64_t group, boolean_t subtract, dmu_tx_t *tx)
1155 if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) {
1156 int64_t delta = DNODE_SIZE + used;
1159 VERIFY3U(0, ==, zap_increment_int(os, DMU_USERUSED_OBJECT,
1161 VERIFY3U(0, ==, zap_increment_int(os, DMU_GROUPUSED_OBJECT,
1167 dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
1170 list_t *list = &os->os_synced_dnodes;
1172 ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os));
1174 while (dn = list_head(list)) {
1176 ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
1177 ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
1178 dn->dn_phys->dn_flags &
1179 DNODE_FLAG_USERUSED_ACCOUNTED);
1181 /* Allocate the user/groupused objects if necessary. */
1182 if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
1183 VERIFY(0 == zap_create_claim(os,
1184 DMU_USERUSED_OBJECT,
1185 DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1186 VERIFY(0 == zap_create_claim(os,
1187 DMU_GROUPUSED_OBJECT,
1188 DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1192 * We intentionally modify the zap object even if the
1193 * net delta is zero. Otherwise
1194 * the block of the zap obj could be shared between
1195 * datasets but need to be different between them after
1199 flags = dn->dn_id_flags;
1201 if (flags & DN_ID_OLD_EXIST) {
1202 do_userquota_update(os, dn->dn_oldused, dn->dn_oldflags,
1203 dn->dn_olduid, dn->dn_oldgid, B_TRUE, tx);
1205 if (flags & DN_ID_NEW_EXIST) {
1206 do_userquota_update(os, DN_USED_BYTES(dn->dn_phys),
1207 dn->dn_phys->dn_flags, dn->dn_newuid,
1208 dn->dn_newgid, B_FALSE, tx);
1211 mutex_enter(&dn->dn_mtx);
1213 dn->dn_oldflags = 0;
1214 if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
1215 dn->dn_olduid = dn->dn_newuid;
1216 dn->dn_oldgid = dn->dn_newgid;
1217 dn->dn_id_flags |= DN_ID_OLD_EXIST;
1218 if (dn->dn_bonuslen == 0)
1219 dn->dn_id_flags |= DN_ID_CHKED_SPILL;
1221 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1223 dn->dn_id_flags &= ~(DN_ID_NEW_EXIST);
1224 mutex_exit(&dn->dn_mtx);
1226 list_remove(list, dn);
1227 dnode_rele(dn, list);
1232 * Returns a pointer to data to find uid/gid from
1234 * If a dirty record for transaction group that is syncing can't
1235 * be found then NULL is returned. In the NULL case it is assumed
1236 * the uid/gid aren't changing.
1239 dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx)
1241 dbuf_dirty_record_t *dr, **drp;
1244 if (db->db_dirtycnt == 0)
1245 return (db->db.db_data); /* Nothing is changing */
1247 for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
1248 if (dr->dr_txg == tx->tx_txg)
1256 DB_DNODE_ENTER(dr->dr_dbuf);
1257 dn = DB_DNODE(dr->dr_dbuf);
1259 if (dn->dn_bonuslen == 0 &&
1260 dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID)
1261 data = dr->dt.dl.dr_data->b_data;
1263 data = dr->dt.dl.dr_data;
1265 DB_DNODE_EXIT(dr->dr_dbuf);
1272 dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
1274 objset_t *os = dn->dn_objset;
1276 dmu_buf_impl_t *db = NULL;
1277 uint64_t *user = NULL;
1278 uint64_t *group = NULL;
1279 int flags = dn->dn_id_flags;
1281 boolean_t have_spill = B_FALSE;
1283 if (!dmu_objset_userused_enabled(dn->dn_objset))
1286 if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST|
1287 DN_ID_CHKED_SPILL)))
1290 if (before && dn->dn_bonuslen != 0)
1291 data = DN_BONUS(dn->dn_phys);
1292 else if (!before && dn->dn_bonuslen != 0) {
1295 mutex_enter(&db->db_mtx);
1296 data = dmu_objset_userquota_find_data(db, tx);
1298 data = DN_BONUS(dn->dn_phys);
1300 } else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) {
1303 if (RW_WRITE_HELD(&dn->dn_struct_rwlock))
1304 rf |= DB_RF_HAVESTRUCT;
1305 error = dmu_spill_hold_by_dnode(dn,
1306 rf | DB_RF_MUST_SUCCEED,
1307 FTAG, (dmu_buf_t **)&db);
1309 mutex_enter(&db->db_mtx);
1310 data = (before) ? db->db.db_data :
1311 dmu_objset_userquota_find_data(db, tx);
1312 have_spill = B_TRUE;
1314 mutex_enter(&dn->dn_mtx);
1315 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1316 mutex_exit(&dn->dn_mtx);
1322 user = &dn->dn_olduid;
1323 group = &dn->dn_oldgid;
1325 user = &dn->dn_newuid;
1326 group = &dn->dn_newgid;
1330 * Must always call the callback in case the object
1331 * type has changed and that type isn't an object type to track
1333 error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data,
1337 * Preserve existing uid/gid when the callback can't determine
1338 * what the new uid/gid are and the callback returned EEXIST.
1339 * The EEXIST error tells us to just use the existing uid/gid.
1340 * If we don't know what the old values are then just assign
1341 * them to 0, since that is a new file being created.
1343 if (!before && data == NULL && error == EEXIST) {
1344 if (flags & DN_ID_OLD_EXIST) {
1345 dn->dn_newuid = dn->dn_olduid;
1346 dn->dn_newgid = dn->dn_oldgid;
1355 mutex_exit(&db->db_mtx);
1357 mutex_enter(&dn->dn_mtx);
1358 if (error == 0 && before)
1359 dn->dn_id_flags |= DN_ID_OLD_EXIST;
1360 if (error == 0 && !before)
1361 dn->dn_id_flags |= DN_ID_NEW_EXIST;
1364 dn->dn_id_flags |= DN_ID_CHKED_SPILL;
1366 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1368 mutex_exit(&dn->dn_mtx);
1370 dmu_buf_rele((dmu_buf_t *)db, FTAG);
1374 dmu_objset_userspace_present(objset_t *os)
1376 return (os->os_phys->os_flags &
1377 OBJSET_FLAG_USERACCOUNTING_COMPLETE);
1381 dmu_objset_userspace_upgrade(objset_t *os)
1386 if (dmu_objset_userspace_present(os))
1388 if (!dmu_objset_userused_enabled(os))
1389 return (SET_ERROR(ENOTSUP));
1390 if (dmu_objset_is_snapshot(os))
1391 return (SET_ERROR(EINVAL));
1394 * We simply need to mark every object dirty, so that it will be
1395 * synced out and now accounted. If this is called
1396 * concurrently, or if we already did some work before crashing,
1397 * that's fine, since we track each object's accounted state
1401 for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
1406 if (issig(JUSTLOOKING) && issig(FORREAL))
1407 return (SET_ERROR(EINTR));
1409 objerr = dmu_bonus_hold(os, obj, FTAG, &db);
1412 tx = dmu_tx_create(os);
1413 dmu_tx_hold_bonus(tx, obj);
1414 objerr = dmu_tx_assign(tx, TXG_WAIT);
1419 dmu_buf_will_dirty(db, tx);
1420 dmu_buf_rele(db, FTAG);
1424 os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
1425 txg_wait_synced(dmu_objset_pool(os), 0);
1430 dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
1431 uint64_t *usedobjsp, uint64_t *availobjsp)
1433 dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
1434 usedobjsp, availobjsp);
1438 dmu_objset_fsid_guid(objset_t *os)
1440 return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
1444 dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1446 stat->dds_type = os->os_phys->os_type;
1447 if (os->os_dsl_dataset)
1448 dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
1452 dmu_objset_stats(objset_t *os, nvlist_t *nv)
1454 ASSERT(os->os_dsl_dataset ||
1455 os->os_phys->os_type == DMU_OST_META);
1457 if (os->os_dsl_dataset != NULL)
1458 dsl_dataset_stats(os->os_dsl_dataset, nv);
1460 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
1461 os->os_phys->os_type);
1462 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
1463 dmu_objset_userspace_present(os));
1467 dmu_objset_is_snapshot(objset_t *os)
1469 if (os->os_dsl_dataset != NULL)
1470 return (dsl_dataset_is_snapshot(os->os_dsl_dataset));
1476 dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
1477 boolean_t *conflict)
1479 dsl_dataset_t *ds = os->os_dsl_dataset;
1482 if (ds->ds_phys->ds_snapnames_zapobj == 0)
1483 return (SET_ERROR(ENOENT));
1485 return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
1486 ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST,
1487 real, maxlen, conflict));
1491 dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
1492 uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1494 dsl_dataset_t *ds = os->os_dsl_dataset;
1495 zap_cursor_t cursor;
1496 zap_attribute_t attr;
1498 ASSERT(dsl_pool_config_held(dmu_objset_pool(os)));
1500 if (ds->ds_phys->ds_snapnames_zapobj == 0)
1501 return (SET_ERROR(ENOENT));
1503 zap_cursor_init_serialized(&cursor,
1504 ds->ds_dir->dd_pool->dp_meta_objset,
1505 ds->ds_phys->ds_snapnames_zapobj, *offp);
1507 if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1508 zap_cursor_fini(&cursor);
1509 return (SET_ERROR(ENOENT));
1512 if (strlen(attr.za_name) + 1 > namelen) {
1513 zap_cursor_fini(&cursor);
1514 return (SET_ERROR(ENAMETOOLONG));
1517 (void) strcpy(name, attr.za_name);
1519 *idp = attr.za_first_integer;
1521 *case_conflict = attr.za_normalization_conflict;
1522 zap_cursor_advance(&cursor);
1523 *offp = zap_cursor_serialize(&cursor);
1524 zap_cursor_fini(&cursor);
1530 dmu_dir_list_next(objset_t *os, int namelen, char *name,
1531 uint64_t *idp, uint64_t *offp)
1533 dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
1534 zap_cursor_t cursor;
1535 zap_attribute_t attr;
1537 /* there is no next dir on a snapshot! */
1538 if (os->os_dsl_dataset->ds_object !=
1539 dd->dd_phys->dd_head_dataset_obj)
1540 return (SET_ERROR(ENOENT));
1542 zap_cursor_init_serialized(&cursor,
1543 dd->dd_pool->dp_meta_objset,
1544 dd->dd_phys->dd_child_dir_zapobj, *offp);
1546 if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1547 zap_cursor_fini(&cursor);
1548 return (SET_ERROR(ENOENT));
1551 if (strlen(attr.za_name) + 1 > namelen) {
1552 zap_cursor_fini(&cursor);
1553 return (SET_ERROR(ENAMETOOLONG));
1556 (void) strcpy(name, attr.za_name);
1558 *idp = attr.za_first_integer;
1559 zap_cursor_advance(&cursor);
1560 *offp = zap_cursor_serialize(&cursor);
1561 zap_cursor_fini(&cursor);
1567 * Find objsets under and including ddobj, call func(ds) on each.
1570 dmu_objset_find_dp(dsl_pool_t *dp, uint64_t ddobj,
1571 int func(dsl_pool_t *, dsl_dataset_t *, void *), void *arg, int flags)
1576 zap_attribute_t *attr;
1580 ASSERT(dsl_pool_config_held(dp));
1582 err = dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd);
1586 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1587 if (dd->dd_myname[0] == '$') {
1588 dsl_dir_rele(dd, FTAG);
1592 thisobj = dd->dd_phys->dd_head_dataset_obj;
1593 attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1596 * Iterate over all children.
1598 if (flags & DS_FIND_CHILDREN) {
1599 for (zap_cursor_init(&zc, dp->dp_meta_objset,
1600 dd->dd_phys->dd_child_dir_zapobj);
1601 zap_cursor_retrieve(&zc, attr) == 0;
1602 (void) zap_cursor_advance(&zc)) {
1603 ASSERT3U(attr->za_integer_length, ==,
1605 ASSERT3U(attr->za_num_integers, ==, 1);
1607 err = dmu_objset_find_dp(dp, attr->za_first_integer,
1612 zap_cursor_fini(&zc);
1615 dsl_dir_rele(dd, FTAG);
1616 kmem_free(attr, sizeof (zap_attribute_t));
1622 * Iterate over all snapshots.
1624 if (flags & DS_FIND_SNAPSHOTS) {
1626 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1629 uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
1630 dsl_dataset_rele(ds, FTAG);
1632 for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
1633 zap_cursor_retrieve(&zc, attr) == 0;
1634 (void) zap_cursor_advance(&zc)) {
1635 ASSERT3U(attr->za_integer_length, ==,
1637 ASSERT3U(attr->za_num_integers, ==, 1);
1639 err = dsl_dataset_hold_obj(dp,
1640 attr->za_first_integer, FTAG, &ds);
1643 err = func(dp, ds, arg);
1644 dsl_dataset_rele(ds, FTAG);
1648 zap_cursor_fini(&zc);
1652 dsl_dir_rele(dd, FTAG);
1653 kmem_free(attr, sizeof (zap_attribute_t));
1661 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1664 err = func(dp, ds, arg);
1665 dsl_dataset_rele(ds, FTAG);
1670 * Find all objsets under name, and for each, call 'func(child_name, arg)'.
1671 * The dp_config_rwlock must not be held when this is called, and it
1672 * will not be held when the callback is called.
1673 * Therefore this function should only be used when the pool is not changing
1674 * (e.g. in syncing context), or the callback can deal with the possible races.
1677 dmu_objset_find_impl(spa_t *spa, const char *name,
1678 int func(const char *, void *), void *arg, int flags)
1681 dsl_pool_t *dp = spa_get_dsl(spa);
1684 zap_attribute_t *attr;
1689 dsl_pool_config_enter(dp, FTAG);
1691 err = dsl_dir_hold(dp, name, FTAG, &dd, NULL);
1693 dsl_pool_config_exit(dp, FTAG);
1697 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1698 if (dd->dd_myname[0] == '$') {
1699 dsl_dir_rele(dd, FTAG);
1700 dsl_pool_config_exit(dp, FTAG);
1704 thisobj = dd->dd_phys->dd_head_dataset_obj;
1705 attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1708 * Iterate over all children.
1710 if (flags & DS_FIND_CHILDREN) {
1711 for (zap_cursor_init(&zc, dp->dp_meta_objset,
1712 dd->dd_phys->dd_child_dir_zapobj);
1713 zap_cursor_retrieve(&zc, attr) == 0;
1714 (void) zap_cursor_advance(&zc)) {
1715 ASSERT3U(attr->za_integer_length, ==,
1717 ASSERT3U(attr->za_num_integers, ==, 1);
1719 child = kmem_asprintf("%s/%s", name, attr->za_name);
1720 dsl_pool_config_exit(dp, FTAG);
1721 err = dmu_objset_find_impl(spa, child,
1723 dsl_pool_config_enter(dp, FTAG);
1728 zap_cursor_fini(&zc);
1731 dsl_dir_rele(dd, FTAG);
1732 dsl_pool_config_exit(dp, FTAG);
1733 kmem_free(attr, sizeof (zap_attribute_t));
1739 * Iterate over all snapshots.
1741 if (flags & DS_FIND_SNAPSHOTS) {
1742 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1745 uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
1746 dsl_dataset_rele(ds, FTAG);
1748 for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
1749 zap_cursor_retrieve(&zc, attr) == 0;
1750 (void) zap_cursor_advance(&zc)) {
1751 ASSERT3U(attr->za_integer_length, ==,
1753 ASSERT3U(attr->za_num_integers, ==, 1);
1755 child = kmem_asprintf("%s@%s",
1756 name, attr->za_name);
1757 dsl_pool_config_exit(dp, FTAG);
1758 err = func(child, arg);
1759 dsl_pool_config_enter(dp, FTAG);
1764 zap_cursor_fini(&zc);
1768 dsl_dir_rele(dd, FTAG);
1769 kmem_free(attr, sizeof (zap_attribute_t));
1770 dsl_pool_config_exit(dp, FTAG);
1775 /* Apply to self. */
1776 return (func(name, arg));
1780 * See comment above dmu_objset_find_impl().
1783 dmu_objset_find(char *name, int func(const char *, void *), void *arg,
1789 error = spa_open(name, &spa, FTAG);
1792 error = dmu_objset_find_impl(spa, name, func, arg, flags);
1793 spa_close(spa, FTAG);
1798 dmu_objset_set_user(objset_t *os, void *user_ptr)
1800 ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1801 os->os_user_ptr = user_ptr;
1805 dmu_objset_get_user(objset_t *os)
1807 ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1808 return (os->os_user_ptr);
1812 * Determine name of filesystem, given name of snapshot.
1813 * buf must be at least MAXNAMELEN bytes
1816 dmu_fsname(const char *snapname, char *buf)
1818 char *atp = strchr(snapname, '@');
1820 return (SET_ERROR(EINVAL));
1821 if (atp - snapname >= MAXNAMELEN)
1822 return (SET_ERROR(ENAMETOOLONG));
1823 (void) strlcpy(buf, snapname, atp - snapname + 1);