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 dmu_objset_byteswap(void *buf, size_t size)
261 objset_phys_t *osp = buf;
263 ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t));
264 dnode_byteswap(&osp->os_meta_dnode);
265 byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
266 osp->os_type = BSWAP_64(osp->os_type);
267 osp->os_flags = BSWAP_64(osp->os_flags);
268 if (size == sizeof (objset_phys_t)) {
269 dnode_byteswap(&osp->os_userused_dnode);
270 dnode_byteswap(&osp->os_groupused_dnode);
275 dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
281 ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
283 os = kmem_zalloc(sizeof (objset_t), KM_SLEEP);
284 os->os_dsl_dataset = ds;
287 if (!BP_IS_HOLE(os->os_rootbp)) {
288 uint32_t aflags = ARC_WAIT;
290 SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
291 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
293 if (DMU_OS_IS_L2CACHEABLE(os))
294 aflags |= ARC_L2CACHE;
295 if (DMU_OS_IS_L2COMPRESSIBLE(os))
296 aflags |= ARC_L2COMPRESS;
298 dprintf_bp(os->os_rootbp, "reading %s", "");
299 err = arc_read(NULL, spa, os->os_rootbp,
300 arc_getbuf_func, &os->os_phys_buf,
301 ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
303 kmem_free(os, sizeof (objset_t));
304 /* convert checksum errors into IO errors */
306 err = SET_ERROR(EIO);
310 /* Increase the blocksize if we are permitted. */
311 if (spa_version(spa) >= SPA_VERSION_USERSPACE &&
312 arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) {
313 arc_buf_t *buf = arc_buf_alloc(spa,
314 sizeof (objset_phys_t), &os->os_phys_buf,
316 bzero(buf->b_data, sizeof (objset_phys_t));
317 bcopy(os->os_phys_buf->b_data, buf->b_data,
318 arc_buf_size(os->os_phys_buf));
319 (void) arc_buf_remove_ref(os->os_phys_buf,
321 os->os_phys_buf = buf;
324 os->os_phys = os->os_phys_buf->b_data;
325 os->os_flags = os->os_phys->os_flags;
327 int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
328 sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE;
329 os->os_phys_buf = arc_buf_alloc(spa, size,
330 &os->os_phys_buf, ARC_BUFC_METADATA);
331 os->os_phys = os->os_phys_buf->b_data;
332 bzero(os->os_phys, size);
336 * Note: the changed_cb will be called once before the register
337 * func returns, thus changing the checksum/compression from the
338 * default (fletcher2/off). Snapshots don't need to know about
339 * checksum/compression/copies.
342 err = dsl_prop_register(ds,
343 zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE),
344 primary_cache_changed_cb, os);
346 err = dsl_prop_register(ds,
347 zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE),
348 secondary_cache_changed_cb, os);
350 if (!dsl_dataset_is_snapshot(ds)) {
352 err = dsl_prop_register(ds,
353 zfs_prop_to_name(ZFS_PROP_CHECKSUM),
354 checksum_changed_cb, os);
357 err = dsl_prop_register(ds,
358 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
359 compression_changed_cb, os);
362 err = dsl_prop_register(ds,
363 zfs_prop_to_name(ZFS_PROP_COPIES),
364 copies_changed_cb, os);
367 err = dsl_prop_register(ds,
368 zfs_prop_to_name(ZFS_PROP_DEDUP),
369 dedup_changed_cb, os);
372 err = dsl_prop_register(ds,
373 zfs_prop_to_name(ZFS_PROP_LOGBIAS),
374 logbias_changed_cb, os);
377 err = dsl_prop_register(ds,
378 zfs_prop_to_name(ZFS_PROP_SYNC),
379 sync_changed_cb, os);
382 err = dsl_prop_register(ds,
384 ZFS_PROP_REDUNDANT_METADATA),
385 redundant_metadata_changed_cb, os);
389 VERIFY(arc_buf_remove_ref(os->os_phys_buf,
391 kmem_free(os, sizeof (objset_t));
394 } else if (ds == NULL) {
395 /* It's the meta-objset. */
396 os->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
397 os->os_compress = ZIO_COMPRESS_LZJB;
398 os->os_copies = spa_max_replication(spa);
399 os->os_dedup_checksum = ZIO_CHECKSUM_OFF;
400 os->os_dedup_verify = B_FALSE;
401 os->os_logbias = ZFS_LOGBIAS_LATENCY;
402 os->os_sync = ZFS_SYNC_STANDARD;
403 os->os_primary_cache = ZFS_CACHE_ALL;
404 os->os_secondary_cache = ZFS_CACHE_ALL;
407 if (ds == NULL || !dsl_dataset_is_snapshot(ds))
408 os->os_zil_header = os->os_phys->os_zil_header;
409 os->os_zil = zil_alloc(os, &os->os_zil_header);
411 for (i = 0; i < TXG_SIZE; i++) {
412 list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t),
413 offsetof(dnode_t, dn_dirty_link[i]));
414 list_create(&os->os_free_dnodes[i], sizeof (dnode_t),
415 offsetof(dnode_t, dn_dirty_link[i]));
417 list_create(&os->os_dnodes, sizeof (dnode_t),
418 offsetof(dnode_t, dn_link));
419 list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
420 offsetof(dmu_buf_impl_t, db_link));
422 mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
423 mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
424 mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
426 DMU_META_DNODE(os) = dnode_special_open(os,
427 &os->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT,
429 if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) {
430 DMU_USERUSED_DNODE(os) = dnode_special_open(os,
431 &os->os_phys->os_userused_dnode, DMU_USERUSED_OBJECT,
432 &os->os_userused_dnode);
433 DMU_GROUPUSED_DNODE(os) = dnode_special_open(os,
434 &os->os_phys->os_groupused_dnode, DMU_GROUPUSED_OBJECT,
435 &os->os_groupused_dnode);
439 * We should be the only thread trying to do this because we
440 * have ds_opening_lock
443 mutex_enter(&ds->ds_lock);
444 ASSERT(ds->ds_objset == NULL);
446 mutex_exit(&ds->ds_lock);
454 dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
458 mutex_enter(&ds->ds_opening_lock);
459 *osp = ds->ds_objset;
461 err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
462 ds, dsl_dataset_get_blkptr(ds), osp);
464 mutex_exit(&ds->ds_opening_lock);
469 * Holds the pool while the objset is held. Therefore only one objset
470 * can be held at a time.
473 dmu_objset_hold(const char *name, void *tag, objset_t **osp)
479 err = dsl_pool_hold(name, tag, &dp);
482 err = dsl_dataset_hold(dp, name, tag, &ds);
484 dsl_pool_rele(dp, tag);
488 err = dmu_objset_from_ds(ds, osp);
490 dsl_dataset_rele(ds, tag);
491 dsl_pool_rele(dp, tag);
498 * dsl_pool must not be held when this is called.
499 * Upon successful return, there will be a longhold on the dataset,
500 * and the dsl_pool will not be held.
503 dmu_objset_own(const char *name, dmu_objset_type_t type,
504 boolean_t readonly, void *tag, objset_t **osp)
510 err = dsl_pool_hold(name, FTAG, &dp);
513 err = dsl_dataset_own(dp, name, tag, &ds);
515 dsl_pool_rele(dp, FTAG);
519 err = dmu_objset_from_ds(ds, osp);
520 dsl_pool_rele(dp, FTAG);
522 dsl_dataset_disown(ds, tag);
523 } else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) {
524 dsl_dataset_disown(ds, tag);
525 return (SET_ERROR(EINVAL));
526 } else if (!readonly && dsl_dataset_is_snapshot(ds)) {
527 dsl_dataset_disown(ds, tag);
528 return (SET_ERROR(EROFS));
534 dmu_objset_rele(objset_t *os, void *tag)
536 dsl_pool_t *dp = dmu_objset_pool(os);
537 dsl_dataset_rele(os->os_dsl_dataset, tag);
538 dsl_pool_rele(dp, tag);
542 * When we are called, os MUST refer to an objset associated with a dataset
543 * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner
544 * == tag. We will then release and reacquire ownership of the dataset while
545 * holding the pool config_rwlock to avoid intervening namespace or ownership
548 * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to
549 * release the hold on its dataset and acquire a new one on the dataset of the
550 * same name so that it can be partially torn down and reconstructed.
553 dmu_objset_refresh_ownership(objset_t *os, void *tag)
556 dsl_dataset_t *ds, *newds;
557 char name[MAXNAMELEN];
559 ds = os->os_dsl_dataset;
560 VERIFY3P(ds, !=, NULL);
561 VERIFY3P(ds->ds_owner, ==, tag);
562 VERIFY(dsl_dataset_long_held(ds));
564 dsl_dataset_name(ds, name);
565 dp = dmu_objset_pool(os);
566 dsl_pool_config_enter(dp, FTAG);
567 dmu_objset_disown(os, tag);
568 VERIFY0(dsl_dataset_own(dp, name, tag, &newds));
569 VERIFY3P(newds, ==, os->os_dsl_dataset);
570 dsl_pool_config_exit(dp, FTAG);
574 dmu_objset_disown(objset_t *os, void *tag)
576 dsl_dataset_disown(os->os_dsl_dataset, tag);
580 dmu_objset_evict_dbufs(objset_t *os)
584 mutex_enter(&os->os_lock);
586 /* process the mdn last, since the other dnodes have holds on it */
587 list_remove(&os->os_dnodes, DMU_META_DNODE(os));
588 list_insert_tail(&os->os_dnodes, DMU_META_DNODE(os));
591 * Find the first dnode with holds. We have to do this dance
592 * because dnode_add_ref() only works if you already have a
593 * hold. If there are no holds then it has no dbufs so OK to
596 for (dn = list_head(&os->os_dnodes);
597 dn && !dnode_add_ref(dn, FTAG);
598 dn = list_next(&os->os_dnodes, dn))
602 dnode_t *next_dn = dn;
605 next_dn = list_next(&os->os_dnodes, next_dn);
606 } while (next_dn && !dnode_add_ref(next_dn, FTAG));
608 mutex_exit(&os->os_lock);
609 dnode_evict_dbufs(dn);
610 dnode_rele(dn, FTAG);
611 mutex_enter(&os->os_lock);
614 mutex_exit(&os->os_lock);
618 dmu_objset_evict(objset_t *os)
620 dsl_dataset_t *ds = os->os_dsl_dataset;
622 for (int t = 0; t < TXG_SIZE; t++)
623 ASSERT(!dmu_objset_is_dirty(os, t));
626 if (!dsl_dataset_is_snapshot(ds)) {
627 VERIFY0(dsl_prop_unregister(ds,
628 zfs_prop_to_name(ZFS_PROP_CHECKSUM),
629 checksum_changed_cb, os));
630 VERIFY0(dsl_prop_unregister(ds,
631 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
632 compression_changed_cb, os));
633 VERIFY0(dsl_prop_unregister(ds,
634 zfs_prop_to_name(ZFS_PROP_COPIES),
635 copies_changed_cb, os));
636 VERIFY0(dsl_prop_unregister(ds,
637 zfs_prop_to_name(ZFS_PROP_DEDUP),
638 dedup_changed_cb, os));
639 VERIFY0(dsl_prop_unregister(ds,
640 zfs_prop_to_name(ZFS_PROP_LOGBIAS),
641 logbias_changed_cb, os));
642 VERIFY0(dsl_prop_unregister(ds,
643 zfs_prop_to_name(ZFS_PROP_SYNC),
644 sync_changed_cb, os));
645 VERIFY0(dsl_prop_unregister(ds,
646 zfs_prop_to_name(ZFS_PROP_REDUNDANT_METADATA),
647 redundant_metadata_changed_cb, os));
649 VERIFY0(dsl_prop_unregister(ds,
650 zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE),
651 primary_cache_changed_cb, os));
652 VERIFY0(dsl_prop_unregister(ds,
653 zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE),
654 secondary_cache_changed_cb, os));
660 dmu_objset_evict_dbufs(os);
662 dnode_special_close(&os->os_meta_dnode);
663 if (DMU_USERUSED_DNODE(os)) {
664 dnode_special_close(&os->os_userused_dnode);
665 dnode_special_close(&os->os_groupused_dnode);
667 zil_free(os->os_zil);
669 ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
671 VERIFY(arc_buf_remove_ref(os->os_phys_buf, &os->os_phys_buf));
674 * This is a barrier to prevent the objset from going away in
675 * dnode_move() until we can safely ensure that the objset is still in
676 * use. We consider the objset valid before the barrier and invalid
679 rw_enter(&os_lock, RW_READER);
682 mutex_destroy(&os->os_lock);
683 mutex_destroy(&os->os_obj_lock);
684 mutex_destroy(&os->os_user_ptr_lock);
685 kmem_free(os, sizeof (objset_t));
689 dmu_objset_snap_cmtime(objset_t *os)
691 return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
694 /* called from dsl for meta-objset */
696 dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
697 dmu_objset_type_t type, dmu_tx_t *tx)
702 ASSERT(dmu_tx_is_syncing(tx));
705 VERIFY0(dmu_objset_from_ds(ds, &os));
707 VERIFY0(dmu_objset_open_impl(spa, NULL, bp, &os));
709 mdn = DMU_META_DNODE(os);
711 dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
712 DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
715 * We don't want to have to increase the meta-dnode's nlevels
716 * later, because then we could do it in quescing context while
717 * we are also accessing it in open context.
719 * This precaution is not necessary for the MOS (ds == NULL),
720 * because the MOS is only updated in syncing context.
721 * This is most fortunate: the MOS is the only objset that
722 * needs to be synced multiple times as spa_sync() iterates
723 * to convergence, so minimizing its dn_nlevels matters.
729 * Determine the number of levels necessary for the meta-dnode
730 * to contain DN_MAX_OBJECT dnodes.
732 while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
733 (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
734 DN_MAX_OBJECT * sizeof (dnode_phys_t))
737 mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
738 mdn->dn_nlevels = levels;
741 ASSERT(type != DMU_OST_NONE);
742 ASSERT(type != DMU_OST_ANY);
743 ASSERT(type < DMU_OST_NUMTYPES);
744 os->os_phys->os_type = type;
745 if (dmu_objset_userused_enabled(os)) {
746 os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
747 os->os_flags = os->os_phys->os_flags;
750 dsl_dataset_dirty(ds, tx);
755 typedef struct dmu_objset_create_arg {
756 const char *doca_name;
758 void (*doca_userfunc)(objset_t *os, void *arg,
759 cred_t *cr, dmu_tx_t *tx);
761 dmu_objset_type_t doca_type;
763 } dmu_objset_create_arg_t;
767 dmu_objset_create_check(void *arg, dmu_tx_t *tx)
769 dmu_objset_create_arg_t *doca = arg;
770 dsl_pool_t *dp = dmu_tx_pool(tx);
775 if (strchr(doca->doca_name, '@') != NULL)
776 return (SET_ERROR(EINVAL));
778 error = dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail);
782 dsl_dir_rele(pdd, FTAG);
783 return (SET_ERROR(EEXIST));
785 error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
787 dsl_dir_rele(pdd, FTAG);
793 dmu_objset_create_sync(void *arg, dmu_tx_t *tx)
795 dmu_objset_create_arg_t *doca = arg;
796 dsl_pool_t *dp = dmu_tx_pool(tx);
804 VERIFY0(dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail));
806 obj = dsl_dataset_create_sync(pdd, tail, NULL, doca->doca_flags,
807 doca->doca_cred, tx);
809 VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
810 bp = dsl_dataset_get_blkptr(ds);
811 os = dmu_objset_create_impl(pdd->dd_pool->dp_spa,
812 ds, bp, doca->doca_type, tx);
814 if (doca->doca_userfunc != NULL) {
815 doca->doca_userfunc(os, doca->doca_userarg,
816 doca->doca_cred, tx);
819 spa_history_log_internal_ds(ds, "create", tx, "");
820 dsl_dataset_rele(ds, FTAG);
821 dsl_dir_rele(pdd, FTAG);
825 dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
826 void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
828 dmu_objset_create_arg_t doca;
830 doca.doca_name = name;
831 doca.doca_cred = CRED();
832 doca.doca_flags = flags;
833 doca.doca_userfunc = func;
834 doca.doca_userarg = arg;
835 doca.doca_type = type;
837 return (dsl_sync_task(name,
838 dmu_objset_create_check, dmu_objset_create_sync, &doca, 5));
841 typedef struct dmu_objset_clone_arg {
842 const char *doca_clone;
843 const char *doca_origin;
845 } dmu_objset_clone_arg_t;
849 dmu_objset_clone_check(void *arg, dmu_tx_t *tx)
851 dmu_objset_clone_arg_t *doca = arg;
855 dsl_dataset_t *origin;
856 dsl_pool_t *dp = dmu_tx_pool(tx);
858 if (strchr(doca->doca_clone, '@') != NULL)
859 return (SET_ERROR(EINVAL));
861 error = dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail);
865 dsl_dir_rele(pdd, FTAG);
866 return (SET_ERROR(EEXIST));
868 /* You can't clone across pools. */
869 if (pdd->dd_pool != dp) {
870 dsl_dir_rele(pdd, FTAG);
871 return (SET_ERROR(EXDEV));
873 error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
876 dsl_dir_rele(pdd, FTAG);
877 return (SET_ERROR(EDQUOT));
879 dsl_dir_rele(pdd, FTAG);
881 error = dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin);
885 /* You can't clone across pools. */
886 if (origin->ds_dir->dd_pool != dp) {
887 dsl_dataset_rele(origin, FTAG);
888 return (SET_ERROR(EXDEV));
891 /* You can only clone snapshots, not the head datasets. */
892 if (!dsl_dataset_is_snapshot(origin)) {
893 dsl_dataset_rele(origin, FTAG);
894 return (SET_ERROR(EINVAL));
896 dsl_dataset_rele(origin, FTAG);
902 dmu_objset_clone_sync(void *arg, dmu_tx_t *tx)
904 dmu_objset_clone_arg_t *doca = arg;
905 dsl_pool_t *dp = dmu_tx_pool(tx);
908 dsl_dataset_t *origin, *ds;
910 char namebuf[MAXNAMELEN];
912 VERIFY0(dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail));
913 VERIFY0(dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin));
915 obj = dsl_dataset_create_sync(pdd, tail, origin, 0,
916 doca->doca_cred, tx);
918 VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
919 dsl_dataset_name(origin, namebuf);
920 spa_history_log_internal_ds(ds, "clone", tx,
921 "origin=%s (%llu)", namebuf, origin->ds_object);
922 dsl_dataset_rele(ds, FTAG);
923 dsl_dataset_rele(origin, FTAG);
924 dsl_dir_rele(pdd, FTAG);
928 dmu_objset_clone(const char *clone, const char *origin)
930 dmu_objset_clone_arg_t doca;
932 doca.doca_clone = clone;
933 doca.doca_origin = origin;
934 doca.doca_cred = CRED();
936 return (dsl_sync_task(clone,
937 dmu_objset_clone_check, dmu_objset_clone_sync, &doca, 5));
941 dmu_objset_snapshot_one(const char *fsname, const char *snapname)
944 char *longsnap = kmem_asprintf("%s@%s", fsname, snapname);
945 nvlist_t *snaps = fnvlist_alloc();
947 fnvlist_add_boolean(snaps, longsnap);
949 err = dsl_dataset_snapshot(snaps, NULL, NULL);
955 dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx)
959 while (dn = list_head(list)) {
960 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
961 ASSERT(dn->dn_dbuf->db_data_pending);
963 * Initialize dn_zio outside dnode_sync() because the
964 * meta-dnode needs to set it ouside dnode_sync().
966 dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
969 ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
970 list_remove(list, dn);
973 (void) dnode_add_ref(dn, newlist);
974 list_insert_tail(newlist, dn);
983 dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
985 blkptr_t *bp = zio->io_bp;
987 dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
989 ASSERT3P(bp, ==, os->os_rootbp);
990 ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_OBJSET);
991 ASSERT0(BP_GET_LEVEL(bp));
994 * Update rootbp fill count: it should be the number of objects
995 * allocated in the object set (not counting the "special"
996 * objects that are stored in the objset_phys_t -- the meta
997 * dnode and user/group accounting objects).
1000 for (int i = 0; i < dnp->dn_nblkptr; i++)
1001 bp->blk_fill += dnp->dn_blkptr[i].blk_fill;
1006 dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
1008 blkptr_t *bp = zio->io_bp;
1009 blkptr_t *bp_orig = &zio->io_bp_orig;
1012 if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
1013 ASSERT(BP_EQUAL(bp, bp_orig));
1015 dsl_dataset_t *ds = os->os_dsl_dataset;
1016 dmu_tx_t *tx = os->os_synctx;
1018 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
1019 dsl_dataset_block_born(ds, bp, tx);
1023 /* called from dsl */
1025 dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
1032 list_t *newlist = NULL;
1033 dbuf_dirty_record_t *dr;
1035 dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
1037 ASSERT(dmu_tx_is_syncing(tx));
1038 /* XXX the write_done callback should really give us the tx... */
1041 if (os->os_dsl_dataset == NULL) {
1043 * This is the MOS. If we have upgraded,
1044 * spa_max_replication() could change, so reset
1047 os->os_copies = spa_max_replication(os->os_spa);
1051 * Create the root block IO
1053 SET_BOOKMARK(&zb, os->os_dsl_dataset ?
1054 os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
1055 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
1056 arc_release(os->os_phys_buf, &os->os_phys_buf);
1058 dmu_write_policy(os, NULL, 0, 0, &zp);
1060 zio = arc_write(pio, os->os_spa, tx->tx_txg,
1061 os->os_rootbp, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os),
1062 DMU_OS_IS_L2COMPRESSIBLE(os), &zp, dmu_objset_write_ready,
1063 NULL, dmu_objset_write_done, os, ZIO_PRIORITY_ASYNC_WRITE,
1064 ZIO_FLAG_MUSTSUCCEED, &zb);
1067 * Sync special dnodes - the parent IO for the sync is the root block
1069 DMU_META_DNODE(os)->dn_zio = zio;
1070 dnode_sync(DMU_META_DNODE(os), tx);
1072 os->os_phys->os_flags = os->os_flags;
1074 if (DMU_USERUSED_DNODE(os) &&
1075 DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
1076 DMU_USERUSED_DNODE(os)->dn_zio = zio;
1077 dnode_sync(DMU_USERUSED_DNODE(os), tx);
1078 DMU_GROUPUSED_DNODE(os)->dn_zio = zio;
1079 dnode_sync(DMU_GROUPUSED_DNODE(os), tx);
1082 txgoff = tx->tx_txg & TXG_MASK;
1084 if (dmu_objset_userused_enabled(os)) {
1085 newlist = &os->os_synced_dnodes;
1087 * We must create the list here because it uses the
1088 * dn_dirty_link[] of this txg.
1090 list_create(newlist, sizeof (dnode_t),
1091 offsetof(dnode_t, dn_dirty_link[txgoff]));
1094 dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx);
1095 dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx);
1097 list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff];
1098 while (dr = list_head(list)) {
1099 ASSERT0(dr->dr_dbuf->db_level);
1100 list_remove(list, dr);
1102 zio_nowait(dr->dr_zio);
1105 * Free intent log blocks up to this tx.
1107 zil_sync(os->os_zil, tx);
1108 os->os_phys->os_zil_header = os->os_zil_header;
1113 dmu_objset_is_dirty(objset_t *os, uint64_t txg)
1115 return (!list_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]) ||
1116 !list_is_empty(&os->os_free_dnodes[txg & TXG_MASK]));
1119 static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
1122 dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
1128 dmu_objset_userused_enabled(objset_t *os)
1130 return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
1131 used_cbs[os->os_phys->os_type] != NULL &&
1132 DMU_USERUSED_DNODE(os) != NULL);
1136 do_userquota_update(objset_t *os, uint64_t used, uint64_t flags,
1137 uint64_t user, uint64_t group, boolean_t subtract, dmu_tx_t *tx)
1139 if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) {
1140 int64_t delta = DNODE_SIZE + used;
1143 VERIFY3U(0, ==, zap_increment_int(os, DMU_USERUSED_OBJECT,
1145 VERIFY3U(0, ==, zap_increment_int(os, DMU_GROUPUSED_OBJECT,
1151 dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
1154 list_t *list = &os->os_synced_dnodes;
1156 ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os));
1158 while (dn = list_head(list)) {
1160 ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
1161 ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
1162 dn->dn_phys->dn_flags &
1163 DNODE_FLAG_USERUSED_ACCOUNTED);
1165 /* Allocate the user/groupused objects if necessary. */
1166 if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
1167 VERIFY(0 == zap_create_claim(os,
1168 DMU_USERUSED_OBJECT,
1169 DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1170 VERIFY(0 == zap_create_claim(os,
1171 DMU_GROUPUSED_OBJECT,
1172 DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1176 * We intentionally modify the zap object even if the
1177 * net delta is zero. Otherwise
1178 * the block of the zap obj could be shared between
1179 * datasets but need to be different between them after
1183 flags = dn->dn_id_flags;
1185 if (flags & DN_ID_OLD_EXIST) {
1186 do_userquota_update(os, dn->dn_oldused, dn->dn_oldflags,
1187 dn->dn_olduid, dn->dn_oldgid, B_TRUE, tx);
1189 if (flags & DN_ID_NEW_EXIST) {
1190 do_userquota_update(os, DN_USED_BYTES(dn->dn_phys),
1191 dn->dn_phys->dn_flags, dn->dn_newuid,
1192 dn->dn_newgid, B_FALSE, tx);
1195 mutex_enter(&dn->dn_mtx);
1197 dn->dn_oldflags = 0;
1198 if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
1199 dn->dn_olduid = dn->dn_newuid;
1200 dn->dn_oldgid = dn->dn_newgid;
1201 dn->dn_id_flags |= DN_ID_OLD_EXIST;
1202 if (dn->dn_bonuslen == 0)
1203 dn->dn_id_flags |= DN_ID_CHKED_SPILL;
1205 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1207 dn->dn_id_flags &= ~(DN_ID_NEW_EXIST);
1208 mutex_exit(&dn->dn_mtx);
1210 list_remove(list, dn);
1211 dnode_rele(dn, list);
1216 * Returns a pointer to data to find uid/gid from
1218 * If a dirty record for transaction group that is syncing can't
1219 * be found then NULL is returned. In the NULL case it is assumed
1220 * the uid/gid aren't changing.
1223 dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx)
1225 dbuf_dirty_record_t *dr, **drp;
1228 if (db->db_dirtycnt == 0)
1229 return (db->db.db_data); /* Nothing is changing */
1231 for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
1232 if (dr->dr_txg == tx->tx_txg)
1240 DB_DNODE_ENTER(dr->dr_dbuf);
1241 dn = DB_DNODE(dr->dr_dbuf);
1243 if (dn->dn_bonuslen == 0 &&
1244 dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID)
1245 data = dr->dt.dl.dr_data->b_data;
1247 data = dr->dt.dl.dr_data;
1249 DB_DNODE_EXIT(dr->dr_dbuf);
1256 dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
1258 objset_t *os = dn->dn_objset;
1260 dmu_buf_impl_t *db = NULL;
1261 uint64_t *user = NULL;
1262 uint64_t *group = NULL;
1263 int flags = dn->dn_id_flags;
1265 boolean_t have_spill = B_FALSE;
1267 if (!dmu_objset_userused_enabled(dn->dn_objset))
1270 if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST|
1271 DN_ID_CHKED_SPILL)))
1274 if (before && dn->dn_bonuslen != 0)
1275 data = DN_BONUS(dn->dn_phys);
1276 else if (!before && dn->dn_bonuslen != 0) {
1279 mutex_enter(&db->db_mtx);
1280 data = dmu_objset_userquota_find_data(db, tx);
1282 data = DN_BONUS(dn->dn_phys);
1284 } else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) {
1287 if (RW_WRITE_HELD(&dn->dn_struct_rwlock))
1288 rf |= DB_RF_HAVESTRUCT;
1289 error = dmu_spill_hold_by_dnode(dn,
1290 rf | DB_RF_MUST_SUCCEED,
1291 FTAG, (dmu_buf_t **)&db);
1293 mutex_enter(&db->db_mtx);
1294 data = (before) ? db->db.db_data :
1295 dmu_objset_userquota_find_data(db, tx);
1296 have_spill = B_TRUE;
1298 mutex_enter(&dn->dn_mtx);
1299 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1300 mutex_exit(&dn->dn_mtx);
1306 user = &dn->dn_olduid;
1307 group = &dn->dn_oldgid;
1309 user = &dn->dn_newuid;
1310 group = &dn->dn_newgid;
1314 * Must always call the callback in case the object
1315 * type has changed and that type isn't an object type to track
1317 error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data,
1321 * Preserve existing uid/gid when the callback can't determine
1322 * what the new uid/gid are and the callback returned EEXIST.
1323 * The EEXIST error tells us to just use the existing uid/gid.
1324 * If we don't know what the old values are then just assign
1325 * them to 0, since that is a new file being created.
1327 if (!before && data == NULL && error == EEXIST) {
1328 if (flags & DN_ID_OLD_EXIST) {
1329 dn->dn_newuid = dn->dn_olduid;
1330 dn->dn_newgid = dn->dn_oldgid;
1339 mutex_exit(&db->db_mtx);
1341 mutex_enter(&dn->dn_mtx);
1342 if (error == 0 && before)
1343 dn->dn_id_flags |= DN_ID_OLD_EXIST;
1344 if (error == 0 && !before)
1345 dn->dn_id_flags |= DN_ID_NEW_EXIST;
1348 dn->dn_id_flags |= DN_ID_CHKED_SPILL;
1350 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1352 mutex_exit(&dn->dn_mtx);
1354 dmu_buf_rele((dmu_buf_t *)db, FTAG);
1358 dmu_objset_userspace_present(objset_t *os)
1360 return (os->os_phys->os_flags &
1361 OBJSET_FLAG_USERACCOUNTING_COMPLETE);
1365 dmu_objset_userspace_upgrade(objset_t *os)
1370 if (dmu_objset_userspace_present(os))
1372 if (!dmu_objset_userused_enabled(os))
1373 return (SET_ERROR(ENOTSUP));
1374 if (dmu_objset_is_snapshot(os))
1375 return (SET_ERROR(EINVAL));
1378 * We simply need to mark every object dirty, so that it will be
1379 * synced out and now accounted. If this is called
1380 * concurrently, or if we already did some work before crashing,
1381 * that's fine, since we track each object's accounted state
1385 for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
1390 if (issig(JUSTLOOKING) && issig(FORREAL))
1391 return (SET_ERROR(EINTR));
1393 objerr = dmu_bonus_hold(os, obj, FTAG, &db);
1396 tx = dmu_tx_create(os);
1397 dmu_tx_hold_bonus(tx, obj);
1398 objerr = dmu_tx_assign(tx, TXG_WAIT);
1403 dmu_buf_will_dirty(db, tx);
1404 dmu_buf_rele(db, FTAG);
1408 os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
1409 txg_wait_synced(dmu_objset_pool(os), 0);
1414 dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
1415 uint64_t *usedobjsp, uint64_t *availobjsp)
1417 dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
1418 usedobjsp, availobjsp);
1422 dmu_objset_fsid_guid(objset_t *os)
1424 return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
1428 dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1430 stat->dds_type = os->os_phys->os_type;
1431 if (os->os_dsl_dataset)
1432 dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
1436 dmu_objset_stats(objset_t *os, nvlist_t *nv)
1438 ASSERT(os->os_dsl_dataset ||
1439 os->os_phys->os_type == DMU_OST_META);
1441 if (os->os_dsl_dataset != NULL)
1442 dsl_dataset_stats(os->os_dsl_dataset, nv);
1444 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
1445 os->os_phys->os_type);
1446 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
1447 dmu_objset_userspace_present(os));
1451 dmu_objset_is_snapshot(objset_t *os)
1453 if (os->os_dsl_dataset != NULL)
1454 return (dsl_dataset_is_snapshot(os->os_dsl_dataset));
1460 dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
1461 boolean_t *conflict)
1463 dsl_dataset_t *ds = os->os_dsl_dataset;
1466 if (ds->ds_phys->ds_snapnames_zapobj == 0)
1467 return (SET_ERROR(ENOENT));
1469 return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
1470 ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST,
1471 real, maxlen, conflict));
1475 dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
1476 uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1478 dsl_dataset_t *ds = os->os_dsl_dataset;
1479 zap_cursor_t cursor;
1480 zap_attribute_t attr;
1482 ASSERT(dsl_pool_config_held(dmu_objset_pool(os)));
1484 if (ds->ds_phys->ds_snapnames_zapobj == 0)
1485 return (SET_ERROR(ENOENT));
1487 zap_cursor_init_serialized(&cursor,
1488 ds->ds_dir->dd_pool->dp_meta_objset,
1489 ds->ds_phys->ds_snapnames_zapobj, *offp);
1491 if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1492 zap_cursor_fini(&cursor);
1493 return (SET_ERROR(ENOENT));
1496 if (strlen(attr.za_name) + 1 > namelen) {
1497 zap_cursor_fini(&cursor);
1498 return (SET_ERROR(ENAMETOOLONG));
1501 (void) strcpy(name, attr.za_name);
1503 *idp = attr.za_first_integer;
1505 *case_conflict = attr.za_normalization_conflict;
1506 zap_cursor_advance(&cursor);
1507 *offp = zap_cursor_serialize(&cursor);
1508 zap_cursor_fini(&cursor);
1514 dmu_dir_list_next(objset_t *os, int namelen, char *name,
1515 uint64_t *idp, uint64_t *offp)
1517 dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
1518 zap_cursor_t cursor;
1519 zap_attribute_t attr;
1521 /* there is no next dir on a snapshot! */
1522 if (os->os_dsl_dataset->ds_object !=
1523 dd->dd_phys->dd_head_dataset_obj)
1524 return (SET_ERROR(ENOENT));
1526 zap_cursor_init_serialized(&cursor,
1527 dd->dd_pool->dp_meta_objset,
1528 dd->dd_phys->dd_child_dir_zapobj, *offp);
1530 if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1531 zap_cursor_fini(&cursor);
1532 return (SET_ERROR(ENOENT));
1535 if (strlen(attr.za_name) + 1 > namelen) {
1536 zap_cursor_fini(&cursor);
1537 return (SET_ERROR(ENAMETOOLONG));
1540 (void) strcpy(name, attr.za_name);
1542 *idp = attr.za_first_integer;
1543 zap_cursor_advance(&cursor);
1544 *offp = zap_cursor_serialize(&cursor);
1545 zap_cursor_fini(&cursor);
1551 * Find objsets under and including ddobj, call func(ds) on each.
1554 dmu_objset_find_dp(dsl_pool_t *dp, uint64_t ddobj,
1555 int func(dsl_pool_t *, dsl_dataset_t *, void *), void *arg, int flags)
1560 zap_attribute_t *attr;
1564 ASSERT(dsl_pool_config_held(dp));
1566 err = dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd);
1570 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1571 if (dd->dd_myname[0] == '$') {
1572 dsl_dir_rele(dd, FTAG);
1576 thisobj = dd->dd_phys->dd_head_dataset_obj;
1577 attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1580 * Iterate over all children.
1582 if (flags & DS_FIND_CHILDREN) {
1583 for (zap_cursor_init(&zc, dp->dp_meta_objset,
1584 dd->dd_phys->dd_child_dir_zapobj);
1585 zap_cursor_retrieve(&zc, attr) == 0;
1586 (void) zap_cursor_advance(&zc)) {
1587 ASSERT3U(attr->za_integer_length, ==,
1589 ASSERT3U(attr->za_num_integers, ==, 1);
1591 err = dmu_objset_find_dp(dp, attr->za_first_integer,
1596 zap_cursor_fini(&zc);
1599 dsl_dir_rele(dd, FTAG);
1600 kmem_free(attr, sizeof (zap_attribute_t));
1606 * Iterate over all snapshots.
1608 if (flags & DS_FIND_SNAPSHOTS) {
1610 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1613 uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
1614 dsl_dataset_rele(ds, FTAG);
1616 for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
1617 zap_cursor_retrieve(&zc, attr) == 0;
1618 (void) zap_cursor_advance(&zc)) {
1619 ASSERT3U(attr->za_integer_length, ==,
1621 ASSERT3U(attr->za_num_integers, ==, 1);
1623 err = dsl_dataset_hold_obj(dp,
1624 attr->za_first_integer, FTAG, &ds);
1627 err = func(dp, ds, arg);
1628 dsl_dataset_rele(ds, FTAG);
1632 zap_cursor_fini(&zc);
1636 dsl_dir_rele(dd, FTAG);
1637 kmem_free(attr, sizeof (zap_attribute_t));
1645 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1648 err = func(dp, ds, arg);
1649 dsl_dataset_rele(ds, FTAG);
1654 * Find all objsets under name, and for each, call 'func(child_name, arg)'.
1655 * The dp_config_rwlock must not be held when this is called, and it
1656 * will not be held when the callback is called.
1657 * Therefore this function should only be used when the pool is not changing
1658 * (e.g. in syncing context), or the callback can deal with the possible races.
1661 dmu_objset_find_impl(spa_t *spa, const char *name,
1662 int func(const char *, void *), void *arg, int flags)
1665 dsl_pool_t *dp = spa_get_dsl(spa);
1668 zap_attribute_t *attr;
1673 dsl_pool_config_enter(dp, FTAG);
1675 err = dsl_dir_hold(dp, name, FTAG, &dd, NULL);
1677 dsl_pool_config_exit(dp, FTAG);
1681 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1682 if (dd->dd_myname[0] == '$') {
1683 dsl_dir_rele(dd, FTAG);
1684 dsl_pool_config_exit(dp, FTAG);
1688 thisobj = dd->dd_phys->dd_head_dataset_obj;
1689 attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1692 * Iterate over all children.
1694 if (flags & DS_FIND_CHILDREN) {
1695 for (zap_cursor_init(&zc, dp->dp_meta_objset,
1696 dd->dd_phys->dd_child_dir_zapobj);
1697 zap_cursor_retrieve(&zc, attr) == 0;
1698 (void) zap_cursor_advance(&zc)) {
1699 ASSERT3U(attr->za_integer_length, ==,
1701 ASSERT3U(attr->za_num_integers, ==, 1);
1703 child = kmem_asprintf("%s/%s", name, attr->za_name);
1704 dsl_pool_config_exit(dp, FTAG);
1705 err = dmu_objset_find_impl(spa, child,
1707 dsl_pool_config_enter(dp, FTAG);
1712 zap_cursor_fini(&zc);
1715 dsl_dir_rele(dd, FTAG);
1716 dsl_pool_config_exit(dp, FTAG);
1717 kmem_free(attr, sizeof (zap_attribute_t));
1723 * Iterate over all snapshots.
1725 if (flags & DS_FIND_SNAPSHOTS) {
1726 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1729 uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
1730 dsl_dataset_rele(ds, FTAG);
1732 for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
1733 zap_cursor_retrieve(&zc, attr) == 0;
1734 (void) zap_cursor_advance(&zc)) {
1735 ASSERT3U(attr->za_integer_length, ==,
1737 ASSERT3U(attr->za_num_integers, ==, 1);
1739 child = kmem_asprintf("%s@%s",
1740 name, attr->za_name);
1741 dsl_pool_config_exit(dp, FTAG);
1742 err = func(child, arg);
1743 dsl_pool_config_enter(dp, FTAG);
1748 zap_cursor_fini(&zc);
1752 dsl_dir_rele(dd, FTAG);
1753 kmem_free(attr, sizeof (zap_attribute_t));
1754 dsl_pool_config_exit(dp, FTAG);
1759 /* Apply to self. */
1760 return (func(name, arg));
1764 * See comment above dmu_objset_find_impl().
1767 dmu_objset_find(char *name, int func(const char *, void *), void *arg,
1773 error = spa_open(name, &spa, FTAG);
1776 error = dmu_objset_find_impl(spa, name, func, arg, flags);
1777 spa_close(spa, FTAG);
1782 dmu_objset_set_user(objset_t *os, void *user_ptr)
1784 ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1785 os->os_user_ptr = user_ptr;
1789 dmu_objset_get_user(objset_t *os)
1791 ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1792 return (os->os_user_ptr);
1796 * Determine name of filesystem, given name of snapshot.
1797 * buf must be at least MAXNAMELEN bytes
1800 dmu_fsname(const char *snapname, char *buf)
1802 char *atp = strchr(snapname, '@');
1804 return (SET_ERROR(EINVAL));
1805 if (atp - snapname >= MAXNAMELEN)
1806 return (SET_ERROR(ENAMETOOLONG));
1807 (void) strlcpy(buf, snapname, atp - snapname + 1);