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.
26 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27 * Copyright (c) 2015 Nexenta Systems, Inc. All rights reserved.
28 * Copyright (c) 2015, STRATO AG, Inc. All rights reserved.
31 /* Portions Copyright 2010 Robert Milkowski */
34 #include <sys/zfs_context.h>
35 #include <sys/dmu_objset.h>
36 #include <sys/dsl_dir.h>
37 #include <sys/dsl_dataset.h>
38 #include <sys/dsl_prop.h>
39 #include <sys/dsl_pool.h>
40 #include <sys/dsl_synctask.h>
41 #include <sys/dsl_deleg.h>
42 #include <sys/dnode.h>
45 #include <sys/dmu_tx.h>
48 #include <sys/dmu_impl.h>
49 #include <sys/zfs_ioctl.h>
51 #include <sys/zfs_onexit.h>
52 #include <sys/dsl_destroy.h>
56 * Needed to close a window in dnode_move() that allows the objset to be freed
57 * before it can be safely accessed.
62 * Tunable to overwrite the maximum number of threads for the parallization
63 * of dmu_objset_find_dp, needed to speed up the import of pools with many
65 * Default is 4 times the number of leaf vdevs.
67 int dmu_find_threads
= 0;
69 static void dmu_objset_find_dp_cb(void *arg
);
74 rw_init(&os_lock
, NULL
, RW_DEFAULT
, NULL
);
84 dmu_objset_spa(objset_t
*os
)
90 dmu_objset_zil(objset_t
*os
)
96 dmu_objset_pool(objset_t
*os
)
100 if ((ds
= os
->os_dsl_dataset
) != NULL
&& ds
->ds_dir
)
101 return (ds
->ds_dir
->dd_pool
);
103 return (spa_get_dsl(os
->os_spa
));
107 dmu_objset_ds(objset_t
*os
)
109 return (os
->os_dsl_dataset
);
113 dmu_objset_type(objset_t
*os
)
115 return (os
->os_phys
->os_type
);
119 dmu_objset_name(objset_t
*os
, char *buf
)
121 dsl_dataset_name(os
->os_dsl_dataset
, buf
);
125 dmu_objset_id(objset_t
*os
)
127 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
129 return (ds
? ds
->ds_object
: 0);
133 dmu_objset_syncprop(objset_t
*os
)
135 return (os
->os_sync
);
139 dmu_objset_logbias(objset_t
*os
)
141 return (os
->os_logbias
);
145 checksum_changed_cb(void *arg
, uint64_t newval
)
150 * Inheritance should have been done by now.
152 ASSERT(newval
!= ZIO_CHECKSUM_INHERIT
);
154 os
->os_checksum
= zio_checksum_select(newval
, ZIO_CHECKSUM_ON_VALUE
);
158 compression_changed_cb(void *arg
, uint64_t newval
)
163 * Inheritance and range checking should have been done by now.
165 ASSERT(newval
!= ZIO_COMPRESS_INHERIT
);
167 os
->os_compress
= zio_compress_select(os
->os_spa
, newval
,
172 copies_changed_cb(void *arg
, uint64_t newval
)
177 * Inheritance and range checking should have been done by now.
180 ASSERT(newval
<= spa_max_replication(os
->os_spa
));
182 os
->os_copies
= newval
;
186 dedup_changed_cb(void *arg
, uint64_t newval
)
189 spa_t
*spa
= os
->os_spa
;
190 enum zio_checksum checksum
;
193 * Inheritance should have been done by now.
195 ASSERT(newval
!= ZIO_CHECKSUM_INHERIT
);
197 checksum
= zio_checksum_dedup_select(spa
, newval
, ZIO_CHECKSUM_OFF
);
199 os
->os_dedup_checksum
= checksum
& ZIO_CHECKSUM_MASK
;
200 os
->os_dedup_verify
= !!(checksum
& ZIO_CHECKSUM_VERIFY
);
204 primary_cache_changed_cb(void *arg
, uint64_t newval
)
209 * Inheritance and range checking should have been done by now.
211 ASSERT(newval
== ZFS_CACHE_ALL
|| newval
== ZFS_CACHE_NONE
||
212 newval
== ZFS_CACHE_METADATA
);
214 os
->os_primary_cache
= newval
;
218 secondary_cache_changed_cb(void *arg
, uint64_t newval
)
223 * Inheritance and range checking should have been done by now.
225 ASSERT(newval
== ZFS_CACHE_ALL
|| newval
== ZFS_CACHE_NONE
||
226 newval
== ZFS_CACHE_METADATA
);
228 os
->os_secondary_cache
= newval
;
232 sync_changed_cb(void *arg
, uint64_t newval
)
237 * Inheritance and range checking should have been done by now.
239 ASSERT(newval
== ZFS_SYNC_STANDARD
|| newval
== ZFS_SYNC_ALWAYS
||
240 newval
== ZFS_SYNC_DISABLED
);
242 os
->os_sync
= newval
;
244 zil_set_sync(os
->os_zil
, newval
);
248 redundant_metadata_changed_cb(void *arg
, uint64_t newval
)
253 * Inheritance and range checking should have been done by now.
255 ASSERT(newval
== ZFS_REDUNDANT_METADATA_ALL
||
256 newval
== ZFS_REDUNDANT_METADATA_MOST
);
258 os
->os_redundant_metadata
= newval
;
262 logbias_changed_cb(void *arg
, uint64_t newval
)
266 ASSERT(newval
== ZFS_LOGBIAS_LATENCY
||
267 newval
== ZFS_LOGBIAS_THROUGHPUT
);
268 os
->os_logbias
= newval
;
270 zil_set_logbias(os
->os_zil
, newval
);
274 recordsize_changed_cb(void *arg
, uint64_t newval
)
278 os
->os_recordsize
= newval
;
282 dmu_objset_byteswap(void *buf
, size_t size
)
284 objset_phys_t
*osp
= buf
;
286 ASSERT(size
== OBJSET_OLD_PHYS_SIZE
|| size
== sizeof (objset_phys_t
));
287 dnode_byteswap(&osp
->os_meta_dnode
);
288 byteswap_uint64_array(&osp
->os_zil_header
, sizeof (zil_header_t
));
289 osp
->os_type
= BSWAP_64(osp
->os_type
);
290 osp
->os_flags
= BSWAP_64(osp
->os_flags
);
291 if (size
== sizeof (objset_phys_t
)) {
292 dnode_byteswap(&osp
->os_userused_dnode
);
293 dnode_byteswap(&osp
->os_groupused_dnode
);
298 dmu_objset_open_impl(spa_t
*spa
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
304 ASSERT(ds
== NULL
|| MUTEX_HELD(&ds
->ds_opening_lock
));
306 os
= kmem_zalloc(sizeof (objset_t
), KM_SLEEP
);
307 os
->os_dsl_dataset
= ds
;
310 if (!BP_IS_HOLE(os
->os_rootbp
)) {
311 arc_flags_t aflags
= ARC_FLAG_WAIT
;
313 SET_BOOKMARK(&zb
, ds
? ds
->ds_object
: DMU_META_OBJSET
,
314 ZB_ROOT_OBJECT
, ZB_ROOT_LEVEL
, ZB_ROOT_BLKID
);
316 if (DMU_OS_IS_L2CACHEABLE(os
))
317 aflags
|= ARC_FLAG_L2CACHE
;
318 if (DMU_OS_IS_L2COMPRESSIBLE(os
))
319 aflags
|= ARC_FLAG_L2COMPRESS
;
321 dprintf_bp(os
->os_rootbp
, "reading %s", "");
322 err
= arc_read(NULL
, spa
, os
->os_rootbp
,
323 arc_getbuf_func
, &os
->os_phys_buf
,
324 ZIO_PRIORITY_SYNC_READ
, ZIO_FLAG_CANFAIL
, &aflags
, &zb
);
326 kmem_free(os
, sizeof (objset_t
));
327 /* convert checksum errors into IO errors */
329 err
= SET_ERROR(EIO
);
333 /* Increase the blocksize if we are permitted. */
334 if (spa_version(spa
) >= SPA_VERSION_USERSPACE
&&
335 arc_buf_size(os
->os_phys_buf
) < sizeof (objset_phys_t
)) {
336 arc_buf_t
*buf
= arc_buf_alloc(spa
,
337 sizeof (objset_phys_t
), &os
->os_phys_buf
,
339 bzero(buf
->b_data
, sizeof (objset_phys_t
));
340 bcopy(os
->os_phys_buf
->b_data
, buf
->b_data
,
341 arc_buf_size(os
->os_phys_buf
));
342 (void) arc_buf_remove_ref(os
->os_phys_buf
,
344 os
->os_phys_buf
= buf
;
347 os
->os_phys
= os
->os_phys_buf
->b_data
;
348 os
->os_flags
= os
->os_phys
->os_flags
;
350 int size
= spa_version(spa
) >= SPA_VERSION_USERSPACE
?
351 sizeof (objset_phys_t
) : OBJSET_OLD_PHYS_SIZE
;
352 os
->os_phys_buf
= arc_buf_alloc(spa
, size
,
353 &os
->os_phys_buf
, ARC_BUFC_METADATA
);
354 os
->os_phys
= os
->os_phys_buf
->b_data
;
355 bzero(os
->os_phys
, size
);
359 * Note: the changed_cb will be called once before the register
360 * func returns, thus changing the checksum/compression from the
361 * default (fletcher2/off). Snapshots don't need to know about
362 * checksum/compression/copies.
365 err
= dsl_prop_register(ds
,
366 zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE
),
367 primary_cache_changed_cb
, os
);
369 err
= dsl_prop_register(ds
,
370 zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE
),
371 secondary_cache_changed_cb
, os
);
373 if (!ds
->ds_is_snapshot
) {
375 err
= dsl_prop_register(ds
,
376 zfs_prop_to_name(ZFS_PROP_CHECKSUM
),
377 checksum_changed_cb
, os
);
380 err
= dsl_prop_register(ds
,
381 zfs_prop_to_name(ZFS_PROP_COMPRESSION
),
382 compression_changed_cb
, os
);
385 err
= dsl_prop_register(ds
,
386 zfs_prop_to_name(ZFS_PROP_COPIES
),
387 copies_changed_cb
, os
);
390 err
= dsl_prop_register(ds
,
391 zfs_prop_to_name(ZFS_PROP_DEDUP
),
392 dedup_changed_cb
, os
);
395 err
= dsl_prop_register(ds
,
396 zfs_prop_to_name(ZFS_PROP_LOGBIAS
),
397 logbias_changed_cb
, os
);
400 err
= dsl_prop_register(ds
,
401 zfs_prop_to_name(ZFS_PROP_SYNC
),
402 sync_changed_cb
, os
);
405 err
= dsl_prop_register(ds
,
407 ZFS_PROP_REDUNDANT_METADATA
),
408 redundant_metadata_changed_cb
, os
);
411 err
= dsl_prop_register(ds
,
412 zfs_prop_to_name(ZFS_PROP_RECORDSIZE
),
413 recordsize_changed_cb
, os
);
417 VERIFY(arc_buf_remove_ref(os
->os_phys_buf
,
419 kmem_free(os
, sizeof (objset_t
));
423 /* It's the meta-objset. */
424 os
->os_checksum
= ZIO_CHECKSUM_FLETCHER_4
;
425 os
->os_compress
= ZIO_COMPRESS_ON
;
426 os
->os_copies
= spa_max_replication(spa
);
427 os
->os_dedup_checksum
= ZIO_CHECKSUM_OFF
;
428 os
->os_dedup_verify
= B_FALSE
;
429 os
->os_logbias
= ZFS_LOGBIAS_LATENCY
;
430 os
->os_sync
= ZFS_SYNC_STANDARD
;
431 os
->os_primary_cache
= ZFS_CACHE_ALL
;
432 os
->os_secondary_cache
= ZFS_CACHE_ALL
;
435 if (ds
== NULL
|| !ds
->ds_is_snapshot
)
436 os
->os_zil_header
= os
->os_phys
->os_zil_header
;
437 os
->os_zil
= zil_alloc(os
, &os
->os_zil_header
);
439 for (i
= 0; i
< TXG_SIZE
; i
++) {
440 list_create(&os
->os_dirty_dnodes
[i
], sizeof (dnode_t
),
441 offsetof(dnode_t
, dn_dirty_link
[i
]));
442 list_create(&os
->os_free_dnodes
[i
], sizeof (dnode_t
),
443 offsetof(dnode_t
, dn_dirty_link
[i
]));
445 list_create(&os
->os_dnodes
, sizeof (dnode_t
),
446 offsetof(dnode_t
, dn_link
));
447 list_create(&os
->os_downgraded_dbufs
, sizeof (dmu_buf_impl_t
),
448 offsetof(dmu_buf_impl_t
, db_link
));
450 list_link_init(&os
->os_evicting_node
);
452 mutex_init(&os
->os_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
453 mutex_init(&os
->os_obj_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
454 mutex_init(&os
->os_user_ptr_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
456 dnode_special_open(os
, &os
->os_phys
->os_meta_dnode
,
457 DMU_META_DNODE_OBJECT
, &os
->os_meta_dnode
);
458 if (arc_buf_size(os
->os_phys_buf
) >= sizeof (objset_phys_t
)) {
459 dnode_special_open(os
, &os
->os_phys
->os_userused_dnode
,
460 DMU_USERUSED_OBJECT
, &os
->os_userused_dnode
);
461 dnode_special_open(os
, &os
->os_phys
->os_groupused_dnode
,
462 DMU_GROUPUSED_OBJECT
, &os
->os_groupused_dnode
);
470 dmu_objset_from_ds(dsl_dataset_t
*ds
, objset_t
**osp
)
474 mutex_enter(&ds
->ds_opening_lock
);
475 if (ds
->ds_objset
== NULL
) {
477 err
= dmu_objset_open_impl(dsl_dataset_get_spa(ds
),
478 ds
, dsl_dataset_get_blkptr(ds
), &os
);
481 mutex_enter(&ds
->ds_lock
);
482 ASSERT(ds
->ds_objset
== NULL
);
484 mutex_exit(&ds
->ds_lock
);
487 *osp
= ds
->ds_objset
;
488 mutex_exit(&ds
->ds_opening_lock
);
493 * Holds the pool while the objset is held. Therefore only one objset
494 * can be held at a time.
497 dmu_objset_hold(const char *name
, void *tag
, objset_t
**osp
)
503 err
= dsl_pool_hold(name
, tag
, &dp
);
506 err
= dsl_dataset_hold(dp
, name
, tag
, &ds
);
508 dsl_pool_rele(dp
, tag
);
512 err
= dmu_objset_from_ds(ds
, osp
);
514 dsl_dataset_rele(ds
, tag
);
515 dsl_pool_rele(dp
, tag
);
522 dmu_objset_own_impl(dsl_dataset_t
*ds
, dmu_objset_type_t type
,
523 boolean_t readonly
, void *tag
, objset_t
**osp
)
527 err
= dmu_objset_from_ds(ds
, osp
);
529 dsl_dataset_disown(ds
, tag
);
530 } else if (type
!= DMU_OST_ANY
&& type
!= (*osp
)->os_phys
->os_type
) {
531 dsl_dataset_disown(ds
, tag
);
532 return (SET_ERROR(EINVAL
));
533 } else if (!readonly
&& dsl_dataset_is_snapshot(ds
)) {
534 dsl_dataset_disown(ds
, tag
);
535 return (SET_ERROR(EROFS
));
541 * dsl_pool must not be held when this is called.
542 * Upon successful return, there will be a longhold on the dataset,
543 * and the dsl_pool will not be held.
546 dmu_objset_own(const char *name
, dmu_objset_type_t type
,
547 boolean_t readonly
, void *tag
, objset_t
**osp
)
553 err
= dsl_pool_hold(name
, FTAG
, &dp
);
556 err
= dsl_dataset_own(dp
, name
, tag
, &ds
);
558 dsl_pool_rele(dp
, FTAG
);
561 err
= dmu_objset_own_impl(ds
, type
, readonly
, tag
, osp
);
562 dsl_pool_rele(dp
, FTAG
);
568 dmu_objset_own_obj(dsl_pool_t
*dp
, uint64_t obj
, dmu_objset_type_t type
,
569 boolean_t readonly
, void *tag
, objset_t
**osp
)
574 err
= dsl_dataset_own_obj(dp
, obj
, tag
, &ds
);
578 return (dmu_objset_own_impl(ds
, type
, readonly
, tag
, osp
));
582 dmu_objset_rele(objset_t
*os
, void *tag
)
584 dsl_pool_t
*dp
= dmu_objset_pool(os
);
585 dsl_dataset_rele(os
->os_dsl_dataset
, tag
);
586 dsl_pool_rele(dp
, tag
);
590 * When we are called, os MUST refer to an objset associated with a dataset
591 * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner
592 * == tag. We will then release and reacquire ownership of the dataset while
593 * holding the pool config_rwlock to avoid intervening namespace or ownership
596 * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to
597 * release the hold on its dataset and acquire a new one on the dataset of the
598 * same name so that it can be partially torn down and reconstructed.
601 dmu_objset_refresh_ownership(objset_t
*os
, void *tag
)
604 dsl_dataset_t
*ds
, *newds
;
605 char name
[MAXNAMELEN
];
607 ds
= os
->os_dsl_dataset
;
608 VERIFY3P(ds
, !=, NULL
);
609 VERIFY3P(ds
->ds_owner
, ==, tag
);
610 VERIFY(dsl_dataset_long_held(ds
));
612 dsl_dataset_name(ds
, name
);
613 dp
= dmu_objset_pool(os
);
614 dsl_pool_config_enter(dp
, FTAG
);
615 dmu_objset_disown(os
, tag
);
616 VERIFY0(dsl_dataset_own(dp
, name
, tag
, &newds
));
617 VERIFY3P(newds
, ==, os
->os_dsl_dataset
);
618 dsl_pool_config_exit(dp
, FTAG
);
622 dmu_objset_disown(objset_t
*os
, void *tag
)
624 dsl_dataset_disown(os
->os_dsl_dataset
, tag
);
628 dmu_objset_evict_dbufs(objset_t
*os
)
633 dn_marker
= kmem_alloc(sizeof (dnode_t
), KM_SLEEP
);
635 mutex_enter(&os
->os_lock
);
636 dn
= list_head(&os
->os_dnodes
);
639 * Skip dnodes without holds. We have to do this dance
640 * because dnode_add_ref() only works if there is already a
641 * hold. If the dnode has no holds, then it has no dbufs.
643 if (dnode_add_ref(dn
, FTAG
)) {
644 list_insert_after(&os
->os_dnodes
, dn
, dn_marker
);
645 mutex_exit(&os
->os_lock
);
647 dnode_evict_dbufs(dn
);
648 dnode_rele(dn
, FTAG
);
650 mutex_enter(&os
->os_lock
);
651 dn
= list_next(&os
->os_dnodes
, dn_marker
);
652 list_remove(&os
->os_dnodes
, dn_marker
);
654 dn
= list_next(&os
->os_dnodes
, dn
);
657 mutex_exit(&os
->os_lock
);
659 kmem_free(dn_marker
, sizeof (dnode_t
));
661 if (DMU_USERUSED_DNODE(os
) != NULL
) {
662 dnode_evict_dbufs(DMU_GROUPUSED_DNODE(os
));
663 dnode_evict_dbufs(DMU_USERUSED_DNODE(os
));
665 dnode_evict_dbufs(DMU_META_DNODE(os
));
669 * Objset eviction processing is split into into two pieces.
670 * The first marks the objset as evicting, evicts any dbufs that
671 * have a refcount of zero, and then queues up the objset for the
672 * second phase of eviction. Once os->os_dnodes has been cleared by
673 * dnode_buf_pageout()->dnode_destroy(), the second phase is executed.
674 * The second phase closes the special dnodes, dequeues the objset from
675 * the list of those undergoing eviction, and finally frees the objset.
677 * NOTE: Due to asynchronous eviction processing (invocation of
678 * dnode_buf_pageout()), it is possible for the meta dnode for the
679 * objset to have no holds even though os->os_dnodes is not empty.
682 dmu_objset_evict(objset_t
*os
)
686 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
688 for (t
= 0; t
< TXG_SIZE
; t
++)
689 ASSERT(!dmu_objset_is_dirty(os
, t
));
692 dsl_prop_unregister_all(ds
, os
);
697 dmu_objset_evict_dbufs(os
);
699 mutex_enter(&os
->os_lock
);
700 spa_evicting_os_register(os
->os_spa
, os
);
701 if (list_is_empty(&os
->os_dnodes
)) {
702 mutex_exit(&os
->os_lock
);
703 dmu_objset_evict_done(os
);
705 mutex_exit(&os
->os_lock
);
710 dmu_objset_evict_done(objset_t
*os
)
712 ASSERT3P(list_head(&os
->os_dnodes
), ==, NULL
);
714 dnode_special_close(&os
->os_meta_dnode
);
715 if (DMU_USERUSED_DNODE(os
)) {
716 dnode_special_close(&os
->os_userused_dnode
);
717 dnode_special_close(&os
->os_groupused_dnode
);
719 zil_free(os
->os_zil
);
721 VERIFY(arc_buf_remove_ref(os
->os_phys_buf
, &os
->os_phys_buf
));
724 * This is a barrier to prevent the objset from going away in
725 * dnode_move() until we can safely ensure that the objset is still in
726 * use. We consider the objset valid before the barrier and invalid
729 rw_enter(&os_lock
, RW_READER
);
732 mutex_destroy(&os
->os_lock
);
733 mutex_destroy(&os
->os_obj_lock
);
734 mutex_destroy(&os
->os_user_ptr_lock
);
735 spa_evicting_os_deregister(os
->os_spa
, os
);
736 kmem_free(os
, sizeof (objset_t
));
740 dmu_objset_snap_cmtime(objset_t
*os
)
742 return (dsl_dir_snap_cmtime(os
->os_dsl_dataset
->ds_dir
));
745 /* called from dsl for meta-objset */
747 dmu_objset_create_impl(spa_t
*spa
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
748 dmu_objset_type_t type
, dmu_tx_t
*tx
)
753 ASSERT(dmu_tx_is_syncing(tx
));
756 VERIFY0(dmu_objset_from_ds(ds
, &os
));
758 VERIFY0(dmu_objset_open_impl(spa
, NULL
, bp
, &os
));
760 mdn
= DMU_META_DNODE(os
);
762 dnode_allocate(mdn
, DMU_OT_DNODE
, 1 << DNODE_BLOCK_SHIFT
,
763 DN_MAX_INDBLKSHIFT
, DMU_OT_NONE
, 0, tx
);
766 * We don't want to have to increase the meta-dnode's nlevels
767 * later, because then we could do it in quescing context while
768 * we are also accessing it in open context.
770 * This precaution is not necessary for the MOS (ds == NULL),
771 * because the MOS is only updated in syncing context.
772 * This is most fortunate: the MOS is the only objset that
773 * needs to be synced multiple times as spa_sync() iterates
774 * to convergence, so minimizing its dn_nlevels matters.
780 * Determine the number of levels necessary for the meta-dnode
781 * to contain DN_MAX_OBJECT dnodes.
783 while ((uint64_t)mdn
->dn_nblkptr
<< (mdn
->dn_datablkshift
+
784 (levels
- 1) * (mdn
->dn_indblkshift
- SPA_BLKPTRSHIFT
)) <
785 DN_MAX_OBJECT
* sizeof (dnode_phys_t
))
788 mdn
->dn_next_nlevels
[tx
->tx_txg
& TXG_MASK
] =
789 mdn
->dn_nlevels
= levels
;
792 ASSERT(type
!= DMU_OST_NONE
);
793 ASSERT(type
!= DMU_OST_ANY
);
794 ASSERT(type
< DMU_OST_NUMTYPES
);
795 os
->os_phys
->os_type
= type
;
796 if (dmu_objset_userused_enabled(os
)) {
797 os
->os_phys
->os_flags
|= OBJSET_FLAG_USERACCOUNTING_COMPLETE
;
798 os
->os_flags
= os
->os_phys
->os_flags
;
801 dsl_dataset_dirty(ds
, tx
);
806 typedef struct dmu_objset_create_arg
{
807 const char *doca_name
;
809 void (*doca_userfunc
)(objset_t
*os
, void *arg
,
810 cred_t
*cr
, dmu_tx_t
*tx
);
812 dmu_objset_type_t doca_type
;
814 } dmu_objset_create_arg_t
;
818 dmu_objset_create_check(void *arg
, dmu_tx_t
*tx
)
820 dmu_objset_create_arg_t
*doca
= arg
;
821 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
826 if (strchr(doca
->doca_name
, '@') != NULL
)
827 return (SET_ERROR(EINVAL
));
829 error
= dsl_dir_hold(dp
, doca
->doca_name
, FTAG
, &pdd
, &tail
);
833 dsl_dir_rele(pdd
, FTAG
);
834 return (SET_ERROR(EEXIST
));
836 error
= dsl_fs_ss_limit_check(pdd
, 1, ZFS_PROP_FILESYSTEM_LIMIT
, NULL
,
838 dsl_dir_rele(pdd
, FTAG
);
844 dmu_objset_create_sync(void *arg
, dmu_tx_t
*tx
)
846 dmu_objset_create_arg_t
*doca
= arg
;
847 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
855 VERIFY0(dsl_dir_hold(dp
, doca
->doca_name
, FTAG
, &pdd
, &tail
));
857 obj
= dsl_dataset_create_sync(pdd
, tail
, NULL
, doca
->doca_flags
,
858 doca
->doca_cred
, tx
);
860 VERIFY0(dsl_dataset_hold_obj(pdd
->dd_pool
, obj
, FTAG
, &ds
));
861 bp
= dsl_dataset_get_blkptr(ds
);
862 os
= dmu_objset_create_impl(pdd
->dd_pool
->dp_spa
,
863 ds
, bp
, doca
->doca_type
, tx
);
865 if (doca
->doca_userfunc
!= NULL
) {
866 doca
->doca_userfunc(os
, doca
->doca_userarg
,
867 doca
->doca_cred
, tx
);
870 spa_history_log_internal_ds(ds
, "create", tx
, "");
871 dsl_dataset_rele(ds
, FTAG
);
872 dsl_dir_rele(pdd
, FTAG
);
876 dmu_objset_create(const char *name
, dmu_objset_type_t type
, uint64_t flags
,
877 void (*func
)(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
), void *arg
)
879 dmu_objset_create_arg_t doca
;
881 doca
.doca_name
= name
;
882 doca
.doca_cred
= CRED();
883 doca
.doca_flags
= flags
;
884 doca
.doca_userfunc
= func
;
885 doca
.doca_userarg
= arg
;
886 doca
.doca_type
= type
;
888 return (dsl_sync_task(name
,
889 dmu_objset_create_check
, dmu_objset_create_sync
, &doca
,
890 5, ZFS_SPACE_CHECK_NORMAL
));
893 typedef struct dmu_objset_clone_arg
{
894 const char *doca_clone
;
895 const char *doca_origin
;
897 } dmu_objset_clone_arg_t
;
901 dmu_objset_clone_check(void *arg
, dmu_tx_t
*tx
)
903 dmu_objset_clone_arg_t
*doca
= arg
;
907 dsl_dataset_t
*origin
;
908 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
910 if (strchr(doca
->doca_clone
, '@') != NULL
)
911 return (SET_ERROR(EINVAL
));
913 error
= dsl_dir_hold(dp
, doca
->doca_clone
, FTAG
, &pdd
, &tail
);
917 dsl_dir_rele(pdd
, FTAG
);
918 return (SET_ERROR(EEXIST
));
921 error
= dsl_fs_ss_limit_check(pdd
, 1, ZFS_PROP_FILESYSTEM_LIMIT
, NULL
,
924 dsl_dir_rele(pdd
, FTAG
);
925 return (SET_ERROR(EDQUOT
));
927 dsl_dir_rele(pdd
, FTAG
);
929 error
= dsl_dataset_hold(dp
, doca
->doca_origin
, FTAG
, &origin
);
933 /* You can only clone snapshots, not the head datasets. */
934 if (!origin
->ds_is_snapshot
) {
935 dsl_dataset_rele(origin
, FTAG
);
936 return (SET_ERROR(EINVAL
));
938 dsl_dataset_rele(origin
, FTAG
);
944 dmu_objset_clone_sync(void *arg
, dmu_tx_t
*tx
)
946 dmu_objset_clone_arg_t
*doca
= arg
;
947 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
950 dsl_dataset_t
*origin
, *ds
;
952 char namebuf
[MAXNAMELEN
];
954 VERIFY0(dsl_dir_hold(dp
, doca
->doca_clone
, FTAG
, &pdd
, &tail
));
955 VERIFY0(dsl_dataset_hold(dp
, doca
->doca_origin
, FTAG
, &origin
));
957 obj
= dsl_dataset_create_sync(pdd
, tail
, origin
, 0,
958 doca
->doca_cred
, tx
);
960 VERIFY0(dsl_dataset_hold_obj(pdd
->dd_pool
, obj
, FTAG
, &ds
));
961 dsl_dataset_name(origin
, namebuf
);
962 spa_history_log_internal_ds(ds
, "clone", tx
,
963 "origin=%s (%llu)", namebuf
, origin
->ds_object
);
964 dsl_dataset_rele(ds
, FTAG
);
965 dsl_dataset_rele(origin
, FTAG
);
966 dsl_dir_rele(pdd
, FTAG
);
970 dmu_objset_clone(const char *clone
, const char *origin
)
972 dmu_objset_clone_arg_t doca
;
974 doca
.doca_clone
= clone
;
975 doca
.doca_origin
= origin
;
976 doca
.doca_cred
= CRED();
978 return (dsl_sync_task(clone
,
979 dmu_objset_clone_check
, dmu_objset_clone_sync
, &doca
,
980 5, ZFS_SPACE_CHECK_NORMAL
));
984 dmu_objset_snapshot_one(const char *fsname
, const char *snapname
)
987 char *longsnap
= kmem_asprintf("%s@%s", fsname
, snapname
);
988 nvlist_t
*snaps
= fnvlist_alloc();
990 fnvlist_add_boolean(snaps
, longsnap
);
992 err
= dsl_dataset_snapshot(snaps
, NULL
, NULL
);
998 dmu_objset_sync_dnodes(list_t
*list
, list_t
*newlist
, dmu_tx_t
*tx
)
1002 while ((dn
= list_head(list
))) {
1003 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
);
1004 ASSERT(dn
->dn_dbuf
->db_data_pending
);
1006 * Initialize dn_zio outside dnode_sync() because the
1007 * meta-dnode needs to set it ouside dnode_sync().
1009 dn
->dn_zio
= dn
->dn_dbuf
->db_data_pending
->dr_zio
;
1012 ASSERT3U(dn
->dn_nlevels
, <=, DN_MAX_LEVELS
);
1013 list_remove(list
, dn
);
1016 (void) dnode_add_ref(dn
, newlist
);
1017 list_insert_tail(newlist
, dn
);
1026 dmu_objset_write_ready(zio_t
*zio
, arc_buf_t
*abuf
, void *arg
)
1030 blkptr_t
*bp
= zio
->io_bp
;
1032 dnode_phys_t
*dnp
= &os
->os_phys
->os_meta_dnode
;
1034 ASSERT(!BP_IS_EMBEDDED(bp
));
1035 ASSERT3P(bp
, ==, os
->os_rootbp
);
1036 ASSERT3U(BP_GET_TYPE(bp
), ==, DMU_OT_OBJSET
);
1037 ASSERT0(BP_GET_LEVEL(bp
));
1040 * Update rootbp fill count: it should be the number of objects
1041 * allocated in the object set (not counting the "special"
1042 * objects that are stored in the objset_phys_t -- the meta
1043 * dnode and user/group accounting objects).
1046 for (i
= 0; i
< dnp
->dn_nblkptr
; i
++)
1047 bp
->blk_fill
+= BP_GET_FILL(&dnp
->dn_blkptr
[i
]);
1052 dmu_objset_write_done(zio_t
*zio
, arc_buf_t
*abuf
, void *arg
)
1054 blkptr_t
*bp
= zio
->io_bp
;
1055 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
1058 if (zio
->io_flags
& ZIO_FLAG_IO_REWRITE
) {
1059 ASSERT(BP_EQUAL(bp
, bp_orig
));
1061 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
1062 dmu_tx_t
*tx
= os
->os_synctx
;
1064 (void) dsl_dataset_block_kill(ds
, bp_orig
, tx
, B_TRUE
);
1065 dsl_dataset_block_born(ds
, bp
, tx
);
1069 /* called from dsl */
1071 dmu_objset_sync(objset_t
*os
, zio_t
*pio
, dmu_tx_t
*tx
)
1074 zbookmark_phys_t zb
;
1078 list_t
*newlist
= NULL
;
1079 dbuf_dirty_record_t
*dr
;
1081 dprintf_ds(os
->os_dsl_dataset
, "txg=%llu\n", tx
->tx_txg
);
1083 ASSERT(dmu_tx_is_syncing(tx
));
1084 /* XXX the write_done callback should really give us the tx... */
1087 if (os
->os_dsl_dataset
== NULL
) {
1089 * This is the MOS. If we have upgraded,
1090 * spa_max_replication() could change, so reset
1093 os
->os_copies
= spa_max_replication(os
->os_spa
);
1097 * Create the root block IO
1099 SET_BOOKMARK(&zb
, os
->os_dsl_dataset
?
1100 os
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
1101 ZB_ROOT_OBJECT
, ZB_ROOT_LEVEL
, ZB_ROOT_BLKID
);
1102 arc_release(os
->os_phys_buf
, &os
->os_phys_buf
);
1104 dmu_write_policy(os
, NULL
, 0, 0, &zp
);
1106 zio
= arc_write(pio
, os
->os_spa
, tx
->tx_txg
,
1107 os
->os_rootbp
, os
->os_phys_buf
, DMU_OS_IS_L2CACHEABLE(os
),
1108 DMU_OS_IS_L2COMPRESSIBLE(os
), &zp
, dmu_objset_write_ready
,
1109 NULL
, dmu_objset_write_done
, os
, ZIO_PRIORITY_ASYNC_WRITE
,
1110 ZIO_FLAG_MUSTSUCCEED
, &zb
);
1113 * Sync special dnodes - the parent IO for the sync is the root block
1115 DMU_META_DNODE(os
)->dn_zio
= zio
;
1116 dnode_sync(DMU_META_DNODE(os
), tx
);
1118 os
->os_phys
->os_flags
= os
->os_flags
;
1120 if (DMU_USERUSED_DNODE(os
) &&
1121 DMU_USERUSED_DNODE(os
)->dn_type
!= DMU_OT_NONE
) {
1122 DMU_USERUSED_DNODE(os
)->dn_zio
= zio
;
1123 dnode_sync(DMU_USERUSED_DNODE(os
), tx
);
1124 DMU_GROUPUSED_DNODE(os
)->dn_zio
= zio
;
1125 dnode_sync(DMU_GROUPUSED_DNODE(os
), tx
);
1128 txgoff
= tx
->tx_txg
& TXG_MASK
;
1130 if (dmu_objset_userused_enabled(os
)) {
1131 newlist
= &os
->os_synced_dnodes
;
1133 * We must create the list here because it uses the
1134 * dn_dirty_link[] of this txg.
1136 list_create(newlist
, sizeof (dnode_t
),
1137 offsetof(dnode_t
, dn_dirty_link
[txgoff
]));
1140 dmu_objset_sync_dnodes(&os
->os_free_dnodes
[txgoff
], newlist
, tx
);
1141 dmu_objset_sync_dnodes(&os
->os_dirty_dnodes
[txgoff
], newlist
, tx
);
1143 list
= &DMU_META_DNODE(os
)->dn_dirty_records
[txgoff
];
1144 while ((dr
= list_head(list
))) {
1145 ASSERT0(dr
->dr_dbuf
->db_level
);
1146 list_remove(list
, dr
);
1148 zio_nowait(dr
->dr_zio
);
1151 * Free intent log blocks up to this tx.
1153 zil_sync(os
->os_zil
, tx
);
1154 os
->os_phys
->os_zil_header
= os
->os_zil_header
;
1159 dmu_objset_is_dirty(objset_t
*os
, uint64_t txg
)
1161 return (!list_is_empty(&os
->os_dirty_dnodes
[txg
& TXG_MASK
]) ||
1162 !list_is_empty(&os
->os_free_dnodes
[txg
& TXG_MASK
]));
1165 static objset_used_cb_t
*used_cbs
[DMU_OST_NUMTYPES
];
1168 dmu_objset_register_type(dmu_objset_type_t ost
, objset_used_cb_t
*cb
)
1174 dmu_objset_userused_enabled(objset_t
*os
)
1176 return (spa_version(os
->os_spa
) >= SPA_VERSION_USERSPACE
&&
1177 used_cbs
[os
->os_phys
->os_type
] != NULL
&&
1178 DMU_USERUSED_DNODE(os
) != NULL
);
1182 do_userquota_update(objset_t
*os
, uint64_t used
, uint64_t flags
,
1183 uint64_t user
, uint64_t group
, boolean_t subtract
, dmu_tx_t
*tx
)
1185 if ((flags
& DNODE_FLAG_USERUSED_ACCOUNTED
)) {
1186 int64_t delta
= DNODE_SIZE
+ used
;
1189 VERIFY3U(0, ==, zap_increment_int(os
, DMU_USERUSED_OBJECT
,
1191 VERIFY3U(0, ==, zap_increment_int(os
, DMU_GROUPUSED_OBJECT
,
1197 dmu_objset_do_userquota_updates(objset_t
*os
, dmu_tx_t
*tx
)
1200 list_t
*list
= &os
->os_synced_dnodes
;
1202 ASSERT(list_head(list
) == NULL
|| dmu_objset_userused_enabled(os
));
1204 while ((dn
= list_head(list
))) {
1206 ASSERT(!DMU_OBJECT_IS_SPECIAL(dn
->dn_object
));
1207 ASSERT(dn
->dn_phys
->dn_type
== DMU_OT_NONE
||
1208 dn
->dn_phys
->dn_flags
&
1209 DNODE_FLAG_USERUSED_ACCOUNTED
);
1211 /* Allocate the user/groupused objects if necessary. */
1212 if (DMU_USERUSED_DNODE(os
)->dn_type
== DMU_OT_NONE
) {
1213 VERIFY(0 == zap_create_claim(os
,
1214 DMU_USERUSED_OBJECT
,
1215 DMU_OT_USERGROUP_USED
, DMU_OT_NONE
, 0, tx
));
1216 VERIFY(0 == zap_create_claim(os
,
1217 DMU_GROUPUSED_OBJECT
,
1218 DMU_OT_USERGROUP_USED
, DMU_OT_NONE
, 0, tx
));
1222 * We intentionally modify the zap object even if the
1223 * net delta is zero. Otherwise
1224 * the block of the zap obj could be shared between
1225 * datasets but need to be different between them after
1229 flags
= dn
->dn_id_flags
;
1231 if (flags
& DN_ID_OLD_EXIST
) {
1232 do_userquota_update(os
, dn
->dn_oldused
, dn
->dn_oldflags
,
1233 dn
->dn_olduid
, dn
->dn_oldgid
, B_TRUE
, tx
);
1235 if (flags
& DN_ID_NEW_EXIST
) {
1236 do_userquota_update(os
, DN_USED_BYTES(dn
->dn_phys
),
1237 dn
->dn_phys
->dn_flags
, dn
->dn_newuid
,
1238 dn
->dn_newgid
, B_FALSE
, tx
);
1241 mutex_enter(&dn
->dn_mtx
);
1243 dn
->dn_oldflags
= 0;
1244 if (dn
->dn_id_flags
& DN_ID_NEW_EXIST
) {
1245 dn
->dn_olduid
= dn
->dn_newuid
;
1246 dn
->dn_oldgid
= dn
->dn_newgid
;
1247 dn
->dn_id_flags
|= DN_ID_OLD_EXIST
;
1248 if (dn
->dn_bonuslen
== 0)
1249 dn
->dn_id_flags
|= DN_ID_CHKED_SPILL
;
1251 dn
->dn_id_flags
|= DN_ID_CHKED_BONUS
;
1253 dn
->dn_id_flags
&= ~(DN_ID_NEW_EXIST
);
1254 mutex_exit(&dn
->dn_mtx
);
1256 list_remove(list
, dn
);
1257 dnode_rele(dn
, list
);
1262 * Returns a pointer to data to find uid/gid from
1264 * If a dirty record for transaction group that is syncing can't
1265 * be found then NULL is returned. In the NULL case it is assumed
1266 * the uid/gid aren't changing.
1269 dmu_objset_userquota_find_data(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1271 dbuf_dirty_record_t
*dr
, **drp
;
1274 if (db
->db_dirtycnt
== 0)
1275 return (db
->db
.db_data
); /* Nothing is changing */
1277 for (drp
= &db
->db_last_dirty
; (dr
= *drp
) != NULL
; drp
= &dr
->dr_next
)
1278 if (dr
->dr_txg
== tx
->tx_txg
)
1286 DB_DNODE_ENTER(dr
->dr_dbuf
);
1287 dn
= DB_DNODE(dr
->dr_dbuf
);
1289 if (dn
->dn_bonuslen
== 0 &&
1290 dr
->dr_dbuf
->db_blkid
== DMU_SPILL_BLKID
)
1291 data
= dr
->dt
.dl
.dr_data
->b_data
;
1293 data
= dr
->dt
.dl
.dr_data
;
1295 DB_DNODE_EXIT(dr
->dr_dbuf
);
1302 dmu_objset_userquota_get_ids(dnode_t
*dn
, boolean_t before
, dmu_tx_t
*tx
)
1304 objset_t
*os
= dn
->dn_objset
;
1306 dmu_buf_impl_t
*db
= NULL
;
1307 uint64_t *user
= NULL
;
1308 uint64_t *group
= NULL
;
1309 int flags
= dn
->dn_id_flags
;
1311 boolean_t have_spill
= B_FALSE
;
1313 if (!dmu_objset_userused_enabled(dn
->dn_objset
))
1316 if (before
&& (flags
& (DN_ID_CHKED_BONUS
|DN_ID_OLD_EXIST
|
1317 DN_ID_CHKED_SPILL
)))
1320 if (before
&& dn
->dn_bonuslen
!= 0)
1321 data
= DN_BONUS(dn
->dn_phys
);
1322 else if (!before
&& dn
->dn_bonuslen
!= 0) {
1325 mutex_enter(&db
->db_mtx
);
1326 data
= dmu_objset_userquota_find_data(db
, tx
);
1328 data
= DN_BONUS(dn
->dn_phys
);
1330 } else if (dn
->dn_bonuslen
== 0 && dn
->dn_bonustype
== DMU_OT_SA
) {
1333 if (RW_WRITE_HELD(&dn
->dn_struct_rwlock
))
1334 rf
|= DB_RF_HAVESTRUCT
;
1335 error
= dmu_spill_hold_by_dnode(dn
,
1336 rf
| DB_RF_MUST_SUCCEED
,
1337 FTAG
, (dmu_buf_t
**)&db
);
1339 mutex_enter(&db
->db_mtx
);
1340 data
= (before
) ? db
->db
.db_data
:
1341 dmu_objset_userquota_find_data(db
, tx
);
1342 have_spill
= B_TRUE
;
1344 mutex_enter(&dn
->dn_mtx
);
1345 dn
->dn_id_flags
|= DN_ID_CHKED_BONUS
;
1346 mutex_exit(&dn
->dn_mtx
);
1352 user
= &dn
->dn_olduid
;
1353 group
= &dn
->dn_oldgid
;
1355 user
= &dn
->dn_newuid
;
1356 group
= &dn
->dn_newgid
;
1360 * Must always call the callback in case the object
1361 * type has changed and that type isn't an object type to track
1363 error
= used_cbs
[os
->os_phys
->os_type
](dn
->dn_bonustype
, data
,
1367 * Preserve existing uid/gid when the callback can't determine
1368 * what the new uid/gid are and the callback returned EEXIST.
1369 * The EEXIST error tells us to just use the existing uid/gid.
1370 * If we don't know what the old values are then just assign
1371 * them to 0, since that is a new file being created.
1373 if (!before
&& data
== NULL
&& error
== EEXIST
) {
1374 if (flags
& DN_ID_OLD_EXIST
) {
1375 dn
->dn_newuid
= dn
->dn_olduid
;
1376 dn
->dn_newgid
= dn
->dn_oldgid
;
1385 mutex_exit(&db
->db_mtx
);
1387 mutex_enter(&dn
->dn_mtx
);
1388 if (error
== 0 && before
)
1389 dn
->dn_id_flags
|= DN_ID_OLD_EXIST
;
1390 if (error
== 0 && !before
)
1391 dn
->dn_id_flags
|= DN_ID_NEW_EXIST
;
1394 dn
->dn_id_flags
|= DN_ID_CHKED_SPILL
;
1396 dn
->dn_id_flags
|= DN_ID_CHKED_BONUS
;
1398 mutex_exit(&dn
->dn_mtx
);
1400 dmu_buf_rele((dmu_buf_t
*)db
, FTAG
);
1404 dmu_objset_userspace_present(objset_t
*os
)
1406 return (os
->os_phys
->os_flags
&
1407 OBJSET_FLAG_USERACCOUNTING_COMPLETE
);
1411 dmu_objset_userspace_upgrade(objset_t
*os
)
1416 if (dmu_objset_userspace_present(os
))
1418 if (!dmu_objset_userused_enabled(os
))
1419 return (SET_ERROR(ENOTSUP
));
1420 if (dmu_objset_is_snapshot(os
))
1421 return (SET_ERROR(EINVAL
));
1424 * We simply need to mark every object dirty, so that it will be
1425 * synced out and now accounted. If this is called
1426 * concurrently, or if we already did some work before crashing,
1427 * that's fine, since we track each object's accounted state
1431 for (obj
= 0; err
== 0; err
= dmu_object_next(os
, &obj
, FALSE
, 0)) {
1436 if (issig(JUSTLOOKING
) && issig(FORREAL
))
1437 return (SET_ERROR(EINTR
));
1439 objerr
= dmu_bonus_hold(os
, obj
, FTAG
, &db
);
1442 tx
= dmu_tx_create(os
);
1443 dmu_tx_hold_bonus(tx
, obj
);
1444 objerr
= dmu_tx_assign(tx
, TXG_WAIT
);
1449 dmu_buf_will_dirty(db
, tx
);
1450 dmu_buf_rele(db
, FTAG
);
1454 os
->os_flags
|= OBJSET_FLAG_USERACCOUNTING_COMPLETE
;
1455 txg_wait_synced(dmu_objset_pool(os
), 0);
1460 dmu_objset_space(objset_t
*os
, uint64_t *refdbytesp
, uint64_t *availbytesp
,
1461 uint64_t *usedobjsp
, uint64_t *availobjsp
)
1463 dsl_dataset_space(os
->os_dsl_dataset
, refdbytesp
, availbytesp
,
1464 usedobjsp
, availobjsp
);
1468 dmu_objset_fsid_guid(objset_t
*os
)
1470 return (dsl_dataset_fsid_guid(os
->os_dsl_dataset
));
1474 dmu_objset_fast_stat(objset_t
*os
, dmu_objset_stats_t
*stat
)
1476 stat
->dds_type
= os
->os_phys
->os_type
;
1477 if (os
->os_dsl_dataset
)
1478 dsl_dataset_fast_stat(os
->os_dsl_dataset
, stat
);
1482 dmu_objset_stats(objset_t
*os
, nvlist_t
*nv
)
1484 ASSERT(os
->os_dsl_dataset
||
1485 os
->os_phys
->os_type
== DMU_OST_META
);
1487 if (os
->os_dsl_dataset
!= NULL
)
1488 dsl_dataset_stats(os
->os_dsl_dataset
, nv
);
1490 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_TYPE
,
1491 os
->os_phys
->os_type
);
1492 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_USERACCOUNTING
,
1493 dmu_objset_userspace_present(os
));
1497 dmu_objset_is_snapshot(objset_t
*os
)
1499 if (os
->os_dsl_dataset
!= NULL
)
1500 return (os
->os_dsl_dataset
->ds_is_snapshot
);
1506 dmu_snapshot_realname(objset_t
*os
, char *name
, char *real
, int maxlen
,
1507 boolean_t
*conflict
)
1509 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
1512 if (dsl_dataset_phys(ds
)->ds_snapnames_zapobj
== 0)
1513 return (SET_ERROR(ENOENT
));
1515 return (zap_lookup_norm(ds
->ds_dir
->dd_pool
->dp_meta_objset
,
1516 dsl_dataset_phys(ds
)->ds_snapnames_zapobj
, name
, 8, 1, &ignored
,
1517 MT_FIRST
, real
, maxlen
, conflict
));
1521 dmu_snapshot_list_next(objset_t
*os
, int namelen
, char *name
,
1522 uint64_t *idp
, uint64_t *offp
, boolean_t
*case_conflict
)
1524 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
1525 zap_cursor_t cursor
;
1526 zap_attribute_t attr
;
1528 ASSERT(dsl_pool_config_held(dmu_objset_pool(os
)));
1530 if (dsl_dataset_phys(ds
)->ds_snapnames_zapobj
== 0)
1531 return (SET_ERROR(ENOENT
));
1533 zap_cursor_init_serialized(&cursor
,
1534 ds
->ds_dir
->dd_pool
->dp_meta_objset
,
1535 dsl_dataset_phys(ds
)->ds_snapnames_zapobj
, *offp
);
1537 if (zap_cursor_retrieve(&cursor
, &attr
) != 0) {
1538 zap_cursor_fini(&cursor
);
1539 return (SET_ERROR(ENOENT
));
1542 if (strlen(attr
.za_name
) + 1 > namelen
) {
1543 zap_cursor_fini(&cursor
);
1544 return (SET_ERROR(ENAMETOOLONG
));
1547 (void) strcpy(name
, attr
.za_name
);
1549 *idp
= attr
.za_first_integer
;
1551 *case_conflict
= attr
.za_normalization_conflict
;
1552 zap_cursor_advance(&cursor
);
1553 *offp
= zap_cursor_serialize(&cursor
);
1554 zap_cursor_fini(&cursor
);
1560 dmu_snapshot_lookup(objset_t
*os
, const char *name
, uint64_t *value
)
1562 return (dsl_dataset_snap_lookup(os
->os_dsl_dataset
, name
, value
));
1566 dmu_dir_list_next(objset_t
*os
, int namelen
, char *name
,
1567 uint64_t *idp
, uint64_t *offp
)
1569 dsl_dir_t
*dd
= os
->os_dsl_dataset
->ds_dir
;
1570 zap_cursor_t cursor
;
1571 zap_attribute_t attr
;
1573 /* there is no next dir on a snapshot! */
1574 if (os
->os_dsl_dataset
->ds_object
!=
1575 dsl_dir_phys(dd
)->dd_head_dataset_obj
)
1576 return (SET_ERROR(ENOENT
));
1578 zap_cursor_init_serialized(&cursor
,
1579 dd
->dd_pool
->dp_meta_objset
,
1580 dsl_dir_phys(dd
)->dd_child_dir_zapobj
, *offp
);
1582 if (zap_cursor_retrieve(&cursor
, &attr
) != 0) {
1583 zap_cursor_fini(&cursor
);
1584 return (SET_ERROR(ENOENT
));
1587 if (strlen(attr
.za_name
) + 1 > namelen
) {
1588 zap_cursor_fini(&cursor
);
1589 return (SET_ERROR(ENAMETOOLONG
));
1592 (void) strcpy(name
, attr
.za_name
);
1594 *idp
= attr
.za_first_integer
;
1595 zap_cursor_advance(&cursor
);
1596 *offp
= zap_cursor_serialize(&cursor
);
1597 zap_cursor_fini(&cursor
);
1602 typedef struct dmu_objset_find_ctx
{
1606 int (*dc_func
)(dsl_pool_t
*, dsl_dataset_t
*, void *);
1609 kmutex_t
*dc_error_lock
;
1611 } dmu_objset_find_ctx_t
;
1614 dmu_objset_find_dp_impl(dmu_objset_find_ctx_t
*dcp
)
1616 dsl_pool_t
*dp
= dcp
->dc_dp
;
1617 dmu_objset_find_ctx_t
*child_dcp
;
1621 zap_attribute_t
*attr
;
1625 /* don't process if there already was an error */
1626 if (*dcp
->dc_error
!= 0)
1629 err
= dsl_dir_hold_obj(dp
, dcp
->dc_ddobj
, NULL
, FTAG
, &dd
);
1633 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1634 if (dd
->dd_myname
[0] == '$') {
1635 dsl_dir_rele(dd
, FTAG
);
1639 thisobj
= dsl_dir_phys(dd
)->dd_head_dataset_obj
;
1640 attr
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
1643 * Iterate over all children.
1645 if (dcp
->dc_flags
& DS_FIND_CHILDREN
) {
1646 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
,
1647 dsl_dir_phys(dd
)->dd_child_dir_zapobj
);
1648 zap_cursor_retrieve(&zc
, attr
) == 0;
1649 (void) zap_cursor_advance(&zc
)) {
1650 ASSERT3U(attr
->za_integer_length
, ==,
1652 ASSERT3U(attr
->za_num_integers
, ==, 1);
1654 child_dcp
= kmem_alloc(sizeof (*child_dcp
), KM_SLEEP
);
1656 child_dcp
->dc_ddobj
= attr
->za_first_integer
;
1657 if (dcp
->dc_tq
!= NULL
)
1658 (void) taskq_dispatch(dcp
->dc_tq
,
1659 dmu_objset_find_dp_cb
, child_dcp
, TQ_SLEEP
);
1661 dmu_objset_find_dp_impl(child_dcp
);
1663 zap_cursor_fini(&zc
);
1667 * Iterate over all snapshots.
1669 if (dcp
->dc_flags
& DS_FIND_SNAPSHOTS
) {
1671 err
= dsl_dataset_hold_obj(dp
, thisobj
, FTAG
, &ds
);
1676 snapobj
= dsl_dataset_phys(ds
)->ds_snapnames_zapobj
;
1677 dsl_dataset_rele(ds
, FTAG
);
1679 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
, snapobj
);
1680 zap_cursor_retrieve(&zc
, attr
) == 0;
1681 (void) zap_cursor_advance(&zc
)) {
1682 ASSERT3U(attr
->za_integer_length
, ==,
1684 ASSERT3U(attr
->za_num_integers
, ==, 1);
1686 err
= dsl_dataset_hold_obj(dp
,
1687 attr
->za_first_integer
, FTAG
, &ds
);
1690 err
= dcp
->dc_func(dp
, ds
, dcp
->dc_arg
);
1691 dsl_dataset_rele(ds
, FTAG
);
1695 zap_cursor_fini(&zc
);
1699 dsl_dir_rele(dd
, FTAG
);
1700 kmem_free(attr
, sizeof (zap_attribute_t
));
1708 err
= dsl_dataset_hold_obj(dp
, thisobj
, FTAG
, &ds
);
1711 err
= dcp
->dc_func(dp
, ds
, dcp
->dc_arg
);
1712 dsl_dataset_rele(ds
, FTAG
);
1716 mutex_enter(dcp
->dc_error_lock
);
1717 /* only keep first error */
1718 if (*dcp
->dc_error
== 0)
1719 *dcp
->dc_error
= err
;
1720 mutex_exit(dcp
->dc_error_lock
);
1723 kmem_free(dcp
, sizeof (*dcp
));
1727 dmu_objset_find_dp_cb(void *arg
)
1729 dmu_objset_find_ctx_t
*dcp
= arg
;
1730 dsl_pool_t
*dp
= dcp
->dc_dp
;
1733 * We need to get a pool_config_lock here, as there are several
1734 * asssert(pool_config_held) down the stack. Getting a lock via
1735 * dsl_pool_config_enter is risky, as it might be stalled by a
1736 * pending writer. This would deadlock, as the write lock can
1737 * only be granted when our parent thread gives up the lock.
1738 * The _prio interface gives us priority over a pending writer.
1740 dsl_pool_config_enter_prio(dp
, FTAG
);
1742 dmu_objset_find_dp_impl(dcp
);
1744 dsl_pool_config_exit(dp
, FTAG
);
1748 * Find objsets under and including ddobj, call func(ds) on each.
1749 * The order for the enumeration is completely undefined.
1750 * func is called with dsl_pool_config held.
1753 dmu_objset_find_dp(dsl_pool_t
*dp
, uint64_t ddobj
,
1754 int func(dsl_pool_t
*, dsl_dataset_t
*, void *), void *arg
, int flags
)
1759 dmu_objset_find_ctx_t
*dcp
;
1762 mutex_init(&err_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1763 dcp
= kmem_alloc(sizeof (*dcp
), KM_SLEEP
);
1766 dcp
->dc_ddobj
= ddobj
;
1767 dcp
->dc_func
= func
;
1769 dcp
->dc_flags
= flags
;
1770 dcp
->dc_error_lock
= &err_lock
;
1771 dcp
->dc_error
= &error
;
1773 if ((flags
& DS_FIND_SERIALIZE
) || dsl_pool_config_held_writer(dp
)) {
1775 * In case a write lock is held we can't make use of
1776 * parallelism, as down the stack of the worker threads
1777 * the lock is asserted via dsl_pool_config_held.
1778 * In case of a read lock this is solved by getting a read
1779 * lock in each worker thread, which isn't possible in case
1780 * of a writer lock. So we fall back to the synchronous path
1782 * In the future it might be possible to get some magic into
1783 * dsl_pool_config_held in a way that it returns true for
1784 * the worker threads so that a single lock held from this
1785 * thread suffices. For now, stay single threaded.
1787 dmu_objset_find_dp_impl(dcp
);
1792 ntasks
= dmu_find_threads
;
1794 ntasks
= vdev_count_leaves(dp
->dp_spa
) * 4;
1795 tq
= taskq_create("dmu_objset_find", ntasks
, maxclsyspri
, ntasks
,
1798 kmem_free(dcp
, sizeof (*dcp
));
1799 return (SET_ERROR(ENOMEM
));
1803 /* dcp will be freed by task */
1804 (void) taskq_dispatch(tq
, dmu_objset_find_dp_cb
, dcp
, TQ_SLEEP
);
1807 * PORTING: this code relies on the property of taskq_wait to wait
1808 * until no more tasks are queued and no more tasks are active. As
1809 * we always queue new tasks from within other tasks, task_wait
1810 * reliably waits for the full recursion to finish, even though we
1811 * enqueue new tasks after taskq_wait has been called.
1812 * On platforms other than illumos, taskq_wait may not have this
1817 mutex_destroy(&err_lock
);
1823 * Find all objsets under name, and for each, call 'func(child_name, arg)'.
1824 * The dp_config_rwlock must not be held when this is called, and it
1825 * will not be held when the callback is called.
1826 * Therefore this function should only be used when the pool is not changing
1827 * (e.g. in syncing context), or the callback can deal with the possible races.
1830 dmu_objset_find_impl(spa_t
*spa
, const char *name
,
1831 int func(const char *, void *), void *arg
, int flags
)
1834 dsl_pool_t
*dp
= spa_get_dsl(spa
);
1837 zap_attribute_t
*attr
;
1842 dsl_pool_config_enter(dp
, FTAG
);
1844 err
= dsl_dir_hold(dp
, name
, FTAG
, &dd
, NULL
);
1846 dsl_pool_config_exit(dp
, FTAG
);
1850 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1851 if (dd
->dd_myname
[0] == '$') {
1852 dsl_dir_rele(dd
, FTAG
);
1853 dsl_pool_config_exit(dp
, FTAG
);
1857 thisobj
= dsl_dir_phys(dd
)->dd_head_dataset_obj
;
1858 attr
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
1861 * Iterate over all children.
1863 if (flags
& DS_FIND_CHILDREN
) {
1864 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
,
1865 dsl_dir_phys(dd
)->dd_child_dir_zapobj
);
1866 zap_cursor_retrieve(&zc
, attr
) == 0;
1867 (void) zap_cursor_advance(&zc
)) {
1868 ASSERT3U(attr
->za_integer_length
, ==,
1870 ASSERT3U(attr
->za_num_integers
, ==, 1);
1872 child
= kmem_asprintf("%s/%s", name
, attr
->za_name
);
1873 dsl_pool_config_exit(dp
, FTAG
);
1874 err
= dmu_objset_find_impl(spa
, child
,
1876 dsl_pool_config_enter(dp
, FTAG
);
1881 zap_cursor_fini(&zc
);
1884 dsl_dir_rele(dd
, FTAG
);
1885 dsl_pool_config_exit(dp
, FTAG
);
1886 kmem_free(attr
, sizeof (zap_attribute_t
));
1892 * Iterate over all snapshots.
1894 if (flags
& DS_FIND_SNAPSHOTS
) {
1895 err
= dsl_dataset_hold_obj(dp
, thisobj
, FTAG
, &ds
);
1900 snapobj
= dsl_dataset_phys(ds
)->ds_snapnames_zapobj
;
1901 dsl_dataset_rele(ds
, FTAG
);
1903 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
, snapobj
);
1904 zap_cursor_retrieve(&zc
, attr
) == 0;
1905 (void) zap_cursor_advance(&zc
)) {
1906 ASSERT3U(attr
->za_integer_length
, ==,
1908 ASSERT3U(attr
->za_num_integers
, ==, 1);
1910 child
= kmem_asprintf("%s@%s",
1911 name
, attr
->za_name
);
1912 dsl_pool_config_exit(dp
, FTAG
);
1913 err
= func(child
, arg
);
1914 dsl_pool_config_enter(dp
, FTAG
);
1919 zap_cursor_fini(&zc
);
1923 dsl_dir_rele(dd
, FTAG
);
1924 kmem_free(attr
, sizeof (zap_attribute_t
));
1925 dsl_pool_config_exit(dp
, FTAG
);
1930 /* Apply to self. */
1931 return (func(name
, arg
));
1935 * See comment above dmu_objset_find_impl().
1938 dmu_objset_find(char *name
, int func(const char *, void *), void *arg
,
1944 error
= spa_open(name
, &spa
, FTAG
);
1947 error
= dmu_objset_find_impl(spa
, name
, func
, arg
, flags
);
1948 spa_close(spa
, FTAG
);
1953 dmu_objset_set_user(objset_t
*os
, void *user_ptr
)
1955 ASSERT(MUTEX_HELD(&os
->os_user_ptr_lock
));
1956 os
->os_user_ptr
= user_ptr
;
1960 dmu_objset_get_user(objset_t
*os
)
1962 ASSERT(MUTEX_HELD(&os
->os_user_ptr_lock
));
1963 return (os
->os_user_ptr
);
1967 * Determine name of filesystem, given name of snapshot.
1968 * buf must be at least MAXNAMELEN bytes
1971 dmu_fsname(const char *snapname
, char *buf
)
1973 char *atp
= strchr(snapname
, '@');
1975 return (SET_ERROR(EINVAL
));
1976 if (atp
- snapname
>= MAXNAMELEN
)
1977 return (SET_ERROR(ENAMETOOLONG
));
1978 (void) strlcpy(buf
, snapname
, atp
- snapname
+ 1);
1982 #if defined(_KERNEL) && defined(HAVE_SPL)
1983 EXPORT_SYMBOL(dmu_objset_zil
);
1984 EXPORT_SYMBOL(dmu_objset_pool
);
1985 EXPORT_SYMBOL(dmu_objset_ds
);
1986 EXPORT_SYMBOL(dmu_objset_type
);
1987 EXPORT_SYMBOL(dmu_objset_name
);
1988 EXPORT_SYMBOL(dmu_objset_hold
);
1989 EXPORT_SYMBOL(dmu_objset_own
);
1990 EXPORT_SYMBOL(dmu_objset_rele
);
1991 EXPORT_SYMBOL(dmu_objset_disown
);
1992 EXPORT_SYMBOL(dmu_objset_from_ds
);
1993 EXPORT_SYMBOL(dmu_objset_create
);
1994 EXPORT_SYMBOL(dmu_objset_clone
);
1995 EXPORT_SYMBOL(dmu_objset_stats
);
1996 EXPORT_SYMBOL(dmu_objset_fast_stat
);
1997 EXPORT_SYMBOL(dmu_objset_spa
);
1998 EXPORT_SYMBOL(dmu_objset_space
);
1999 EXPORT_SYMBOL(dmu_objset_fsid_guid
);
2000 EXPORT_SYMBOL(dmu_objset_find
);
2001 EXPORT_SYMBOL(dmu_objset_byteswap
);
2002 EXPORT_SYMBOL(dmu_objset_evict_dbufs
);
2003 EXPORT_SYMBOL(dmu_objset_snap_cmtime
);
2005 EXPORT_SYMBOL(dmu_objset_sync
);
2006 EXPORT_SYMBOL(dmu_objset_is_dirty
);
2007 EXPORT_SYMBOL(dmu_objset_create_impl
);
2008 EXPORT_SYMBOL(dmu_objset_open_impl
);
2009 EXPORT_SYMBOL(dmu_objset_evict
);
2010 EXPORT_SYMBOL(dmu_objset_register_type
);
2011 EXPORT_SYMBOL(dmu_objset_do_userquota_updates
);
2012 EXPORT_SYMBOL(dmu_objset_userquota_get_ids
);
2013 EXPORT_SYMBOL(dmu_objset_userused_enabled
);
2014 EXPORT_SYMBOL(dmu_objset_userspace_upgrade
);
2015 EXPORT_SYMBOL(dmu_objset_userspace_present
);