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, 2016 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.
29 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
32 /* Portions Copyright 2010 Robert Milkowski */
35 #include <sys/zfs_context.h>
36 #include <sys/dmu_objset.h>
37 #include <sys/dsl_dir.h>
38 #include <sys/dsl_dataset.h>
39 #include <sys/dsl_prop.h>
40 #include <sys/dsl_pool.h>
41 #include <sys/dsl_synctask.h>
42 #include <sys/dsl_deleg.h>
43 #include <sys/dnode.h>
46 #include <sys/dmu_tx.h>
49 #include <sys/dmu_impl.h>
50 #include <sys/zfs_ioctl.h>
52 #include <sys/zfs_onexit.h>
53 #include <sys/dsl_destroy.h>
55 #include <sys/policy.h>
58 * Needed to close a window in dnode_move() that allows the objset to be freed
59 * before it can be safely accessed.
64 * Tunable to overwrite the maximum number of threads for the parallization
65 * of dmu_objset_find_dp, needed to speed up the import of pools with many
67 * Default is 4 times the number of leaf vdevs.
69 int dmu_find_threads
= 0;
72 * Backfill lower metadnode objects after this many have been freed.
73 * Backfilling negatively impacts object creation rates, so only do it
74 * if there are enough holes to fill.
76 int dmu_rescan_dnode_threshold
= 1 << DN_MAX_INDBLKSHIFT
;
78 static void dmu_objset_find_dp_cb(void *arg
);
83 rw_init(&os_lock
, NULL
, RW_DEFAULT
, NULL
);
93 dmu_objset_spa(objset_t
*os
)
99 dmu_objset_zil(objset_t
*os
)
105 dmu_objset_pool(objset_t
*os
)
109 if ((ds
= os
->os_dsl_dataset
) != NULL
&& ds
->ds_dir
)
110 return (ds
->ds_dir
->dd_pool
);
112 return (spa_get_dsl(os
->os_spa
));
116 dmu_objset_ds(objset_t
*os
)
118 return (os
->os_dsl_dataset
);
122 dmu_objset_type(objset_t
*os
)
124 return (os
->os_phys
->os_type
);
128 dmu_objset_name(objset_t
*os
, char *buf
)
130 dsl_dataset_name(os
->os_dsl_dataset
, buf
);
134 dmu_objset_id(objset_t
*os
)
136 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
138 return (ds
? ds
->ds_object
: 0);
142 dmu_objset_dnodesize(objset_t
*os
)
144 return (os
->os_dnodesize
);
148 dmu_objset_syncprop(objset_t
*os
)
150 return (os
->os_sync
);
154 dmu_objset_logbias(objset_t
*os
)
156 return (os
->os_logbias
);
160 checksum_changed_cb(void *arg
, uint64_t newval
)
165 * Inheritance should have been done by now.
167 ASSERT(newval
!= ZIO_CHECKSUM_INHERIT
);
169 os
->os_checksum
= zio_checksum_select(newval
, ZIO_CHECKSUM_ON_VALUE
);
173 compression_changed_cb(void *arg
, uint64_t newval
)
178 * Inheritance and range checking should have been done by now.
180 ASSERT(newval
!= ZIO_COMPRESS_INHERIT
);
182 os
->os_compress
= zio_compress_select(os
->os_spa
, newval
,
187 copies_changed_cb(void *arg
, uint64_t newval
)
192 * Inheritance and range checking should have been done by now.
195 ASSERT(newval
<= spa_max_replication(os
->os_spa
));
197 os
->os_copies
= newval
;
201 dedup_changed_cb(void *arg
, uint64_t newval
)
204 spa_t
*spa
= os
->os_spa
;
205 enum zio_checksum checksum
;
208 * Inheritance should have been done by now.
210 ASSERT(newval
!= ZIO_CHECKSUM_INHERIT
);
212 checksum
= zio_checksum_dedup_select(spa
, newval
, ZIO_CHECKSUM_OFF
);
214 os
->os_dedup_checksum
= checksum
& ZIO_CHECKSUM_MASK
;
215 os
->os_dedup_verify
= !!(checksum
& ZIO_CHECKSUM_VERIFY
);
219 primary_cache_changed_cb(void *arg
, uint64_t newval
)
224 * Inheritance and range checking should have been done by now.
226 ASSERT(newval
== ZFS_CACHE_ALL
|| newval
== ZFS_CACHE_NONE
||
227 newval
== ZFS_CACHE_METADATA
);
229 os
->os_primary_cache
= newval
;
233 secondary_cache_changed_cb(void *arg
, uint64_t newval
)
238 * Inheritance and range checking should have been done by now.
240 ASSERT(newval
== ZFS_CACHE_ALL
|| newval
== ZFS_CACHE_NONE
||
241 newval
== ZFS_CACHE_METADATA
);
243 os
->os_secondary_cache
= newval
;
247 sync_changed_cb(void *arg
, uint64_t newval
)
252 * Inheritance and range checking should have been done by now.
254 ASSERT(newval
== ZFS_SYNC_STANDARD
|| newval
== ZFS_SYNC_ALWAYS
||
255 newval
== ZFS_SYNC_DISABLED
);
257 os
->os_sync
= newval
;
259 zil_set_sync(os
->os_zil
, newval
);
263 redundant_metadata_changed_cb(void *arg
, uint64_t newval
)
268 * Inheritance and range checking should have been done by now.
270 ASSERT(newval
== ZFS_REDUNDANT_METADATA_ALL
||
271 newval
== ZFS_REDUNDANT_METADATA_MOST
);
273 os
->os_redundant_metadata
= newval
;
277 dnodesize_changed_cb(void *arg
, uint64_t newval
)
282 case ZFS_DNSIZE_LEGACY
:
283 os
->os_dnodesize
= DNODE_MIN_SIZE
;
285 case ZFS_DNSIZE_AUTO
:
287 * Choose a dnode size that will work well for most
288 * workloads if the user specified "auto". Future code
289 * improvements could dynamically select a dnode size
290 * based on observed workload patterns.
292 os
->os_dnodesize
= DNODE_MIN_SIZE
* 2;
299 os
->os_dnodesize
= newval
;
305 logbias_changed_cb(void *arg
, uint64_t newval
)
309 ASSERT(newval
== ZFS_LOGBIAS_LATENCY
||
310 newval
== ZFS_LOGBIAS_THROUGHPUT
);
311 os
->os_logbias
= newval
;
313 zil_set_logbias(os
->os_zil
, newval
);
317 recordsize_changed_cb(void *arg
, uint64_t newval
)
321 os
->os_recordsize
= newval
;
325 dmu_objset_byteswap(void *buf
, size_t size
)
327 objset_phys_t
*osp
= buf
;
329 ASSERT(size
== OBJSET_OLD_PHYS_SIZE
|| size
== sizeof (objset_phys_t
));
330 dnode_byteswap(&osp
->os_meta_dnode
);
331 byteswap_uint64_array(&osp
->os_zil_header
, sizeof (zil_header_t
));
332 osp
->os_type
= BSWAP_64(osp
->os_type
);
333 osp
->os_flags
= BSWAP_64(osp
->os_flags
);
334 if (size
== sizeof (objset_phys_t
)) {
335 dnode_byteswap(&osp
->os_userused_dnode
);
336 dnode_byteswap(&osp
->os_groupused_dnode
);
341 dmu_objset_open_impl(spa_t
*spa
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
347 ASSERT(ds
== NULL
|| MUTEX_HELD(&ds
->ds_opening_lock
));
349 os
= kmem_zalloc(sizeof (objset_t
), KM_SLEEP
);
350 os
->os_dsl_dataset
= ds
;
353 if (!BP_IS_HOLE(os
->os_rootbp
)) {
354 arc_flags_t aflags
= ARC_FLAG_WAIT
;
356 SET_BOOKMARK(&zb
, ds
? ds
->ds_object
: DMU_META_OBJSET
,
357 ZB_ROOT_OBJECT
, ZB_ROOT_LEVEL
, ZB_ROOT_BLKID
);
359 if (DMU_OS_IS_L2CACHEABLE(os
))
360 aflags
|= ARC_FLAG_L2CACHE
;
361 if (DMU_OS_IS_L2COMPRESSIBLE(os
))
362 aflags
|= ARC_FLAG_L2COMPRESS
;
364 dprintf_bp(os
->os_rootbp
, "reading %s", "");
365 err
= arc_read(NULL
, spa
, os
->os_rootbp
,
366 arc_getbuf_func
, &os
->os_phys_buf
,
367 ZIO_PRIORITY_SYNC_READ
, ZIO_FLAG_CANFAIL
, &aflags
, &zb
);
369 kmem_free(os
, sizeof (objset_t
));
370 /* convert checksum errors into IO errors */
372 err
= SET_ERROR(EIO
);
376 /* Increase the blocksize if we are permitted. */
377 if (spa_version(spa
) >= SPA_VERSION_USERSPACE
&&
378 arc_buf_size(os
->os_phys_buf
) < sizeof (objset_phys_t
)) {
379 arc_buf_t
*buf
= arc_buf_alloc(spa
,
380 sizeof (objset_phys_t
), &os
->os_phys_buf
,
382 bzero(buf
->b_data
, sizeof (objset_phys_t
));
383 bcopy(os
->os_phys_buf
->b_data
, buf
->b_data
,
384 arc_buf_size(os
->os_phys_buf
));
385 (void) arc_buf_remove_ref(os
->os_phys_buf
,
387 os
->os_phys_buf
= buf
;
390 os
->os_phys
= os
->os_phys_buf
->b_data
;
391 os
->os_flags
= os
->os_phys
->os_flags
;
393 int size
= spa_version(spa
) >= SPA_VERSION_USERSPACE
?
394 sizeof (objset_phys_t
) : OBJSET_OLD_PHYS_SIZE
;
395 os
->os_phys_buf
= arc_buf_alloc(spa
, size
,
396 &os
->os_phys_buf
, ARC_BUFC_METADATA
);
397 os
->os_phys
= os
->os_phys_buf
->b_data
;
398 bzero(os
->os_phys
, size
);
402 * Note: the changed_cb will be called once before the register
403 * func returns, thus changing the checksum/compression from the
404 * default (fletcher2/off). Snapshots don't need to know about
405 * checksum/compression/copies.
408 err
= dsl_prop_register(ds
,
409 zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE
),
410 primary_cache_changed_cb
, os
);
412 err
= dsl_prop_register(ds
,
413 zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE
),
414 secondary_cache_changed_cb
, os
);
416 if (!ds
->ds_is_snapshot
) {
418 err
= dsl_prop_register(ds
,
419 zfs_prop_to_name(ZFS_PROP_CHECKSUM
),
420 checksum_changed_cb
, os
);
423 err
= dsl_prop_register(ds
,
424 zfs_prop_to_name(ZFS_PROP_COMPRESSION
),
425 compression_changed_cb
, os
);
428 err
= dsl_prop_register(ds
,
429 zfs_prop_to_name(ZFS_PROP_COPIES
),
430 copies_changed_cb
, os
);
433 err
= dsl_prop_register(ds
,
434 zfs_prop_to_name(ZFS_PROP_DEDUP
),
435 dedup_changed_cb
, os
);
438 err
= dsl_prop_register(ds
,
439 zfs_prop_to_name(ZFS_PROP_LOGBIAS
),
440 logbias_changed_cb
, os
);
443 err
= dsl_prop_register(ds
,
444 zfs_prop_to_name(ZFS_PROP_SYNC
),
445 sync_changed_cb
, os
);
448 err
= dsl_prop_register(ds
,
450 ZFS_PROP_REDUNDANT_METADATA
),
451 redundant_metadata_changed_cb
, os
);
454 err
= dsl_prop_register(ds
,
455 zfs_prop_to_name(ZFS_PROP_RECORDSIZE
),
456 recordsize_changed_cb
, os
);
459 err
= dsl_prop_register(ds
,
460 zfs_prop_to_name(ZFS_PROP_DNODESIZE
),
461 dnodesize_changed_cb
, os
);
465 VERIFY(arc_buf_remove_ref(os
->os_phys_buf
,
467 kmem_free(os
, sizeof (objset_t
));
471 /* It's the meta-objset. */
472 os
->os_checksum
= ZIO_CHECKSUM_FLETCHER_4
;
473 os
->os_compress
= ZIO_COMPRESS_ON
;
474 os
->os_copies
= spa_max_replication(spa
);
475 os
->os_dedup_checksum
= ZIO_CHECKSUM_OFF
;
476 os
->os_dedup_verify
= B_FALSE
;
477 os
->os_logbias
= ZFS_LOGBIAS_LATENCY
;
478 os
->os_sync
= ZFS_SYNC_STANDARD
;
479 os
->os_primary_cache
= ZFS_CACHE_ALL
;
480 os
->os_secondary_cache
= ZFS_CACHE_ALL
;
481 os
->os_dnodesize
= DNODE_MIN_SIZE
;
484 if (ds
== NULL
|| !ds
->ds_is_snapshot
)
485 os
->os_zil_header
= os
->os_phys
->os_zil_header
;
486 os
->os_zil
= zil_alloc(os
, &os
->os_zil_header
);
488 for (i
= 0; i
< TXG_SIZE
; i
++) {
489 list_create(&os
->os_dirty_dnodes
[i
], sizeof (dnode_t
),
490 offsetof(dnode_t
, dn_dirty_link
[i
]));
491 list_create(&os
->os_free_dnodes
[i
], sizeof (dnode_t
),
492 offsetof(dnode_t
, dn_dirty_link
[i
]));
494 list_create(&os
->os_dnodes
, sizeof (dnode_t
),
495 offsetof(dnode_t
, dn_link
));
496 list_create(&os
->os_downgraded_dbufs
, sizeof (dmu_buf_impl_t
),
497 offsetof(dmu_buf_impl_t
, db_link
));
499 list_link_init(&os
->os_evicting_node
);
501 mutex_init(&os
->os_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
502 mutex_init(&os
->os_obj_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
503 mutex_init(&os
->os_user_ptr_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
505 dnode_special_open(os
, &os
->os_phys
->os_meta_dnode
,
506 DMU_META_DNODE_OBJECT
, &os
->os_meta_dnode
);
507 if (arc_buf_size(os
->os_phys_buf
) >= sizeof (objset_phys_t
)) {
508 dnode_special_open(os
, &os
->os_phys
->os_userused_dnode
,
509 DMU_USERUSED_OBJECT
, &os
->os_userused_dnode
);
510 dnode_special_open(os
, &os
->os_phys
->os_groupused_dnode
,
511 DMU_GROUPUSED_OBJECT
, &os
->os_groupused_dnode
);
519 dmu_objset_from_ds(dsl_dataset_t
*ds
, objset_t
**osp
)
523 mutex_enter(&ds
->ds_opening_lock
);
524 if (ds
->ds_objset
== NULL
) {
526 err
= dmu_objset_open_impl(dsl_dataset_get_spa(ds
),
527 ds
, dsl_dataset_get_blkptr(ds
), &os
);
530 mutex_enter(&ds
->ds_lock
);
531 ASSERT(ds
->ds_objset
== NULL
);
533 mutex_exit(&ds
->ds_lock
);
536 *osp
= ds
->ds_objset
;
537 mutex_exit(&ds
->ds_opening_lock
);
542 * Holds the pool while the objset is held. Therefore only one objset
543 * can be held at a time.
546 dmu_objset_hold(const char *name
, void *tag
, objset_t
**osp
)
552 err
= dsl_pool_hold(name
, tag
, &dp
);
555 err
= dsl_dataset_hold(dp
, name
, tag
, &ds
);
557 dsl_pool_rele(dp
, tag
);
561 err
= dmu_objset_from_ds(ds
, osp
);
563 dsl_dataset_rele(ds
, tag
);
564 dsl_pool_rele(dp
, tag
);
571 dmu_objset_own_impl(dsl_dataset_t
*ds
, dmu_objset_type_t type
,
572 boolean_t readonly
, void *tag
, objset_t
**osp
)
576 err
= dmu_objset_from_ds(ds
, osp
);
578 dsl_dataset_disown(ds
, tag
);
579 } else if (type
!= DMU_OST_ANY
&& type
!= (*osp
)->os_phys
->os_type
) {
580 dsl_dataset_disown(ds
, tag
);
581 return (SET_ERROR(EINVAL
));
582 } else if (!readonly
&& dsl_dataset_is_snapshot(ds
)) {
583 dsl_dataset_disown(ds
, tag
);
584 return (SET_ERROR(EROFS
));
590 * dsl_pool must not be held when this is called.
591 * Upon successful return, there will be a longhold on the dataset,
592 * and the dsl_pool will not be held.
595 dmu_objset_own(const char *name
, dmu_objset_type_t type
,
596 boolean_t readonly
, void *tag
, objset_t
**osp
)
602 err
= dsl_pool_hold(name
, FTAG
, &dp
);
605 err
= dsl_dataset_own(dp
, name
, tag
, &ds
);
607 dsl_pool_rele(dp
, FTAG
);
610 err
= dmu_objset_own_impl(ds
, type
, readonly
, tag
, osp
);
611 dsl_pool_rele(dp
, FTAG
);
617 dmu_objset_own_obj(dsl_pool_t
*dp
, uint64_t obj
, dmu_objset_type_t type
,
618 boolean_t readonly
, void *tag
, objset_t
**osp
)
623 err
= dsl_dataset_own_obj(dp
, obj
, tag
, &ds
);
627 return (dmu_objset_own_impl(ds
, type
, readonly
, tag
, osp
));
631 dmu_objset_rele(objset_t
*os
, void *tag
)
633 dsl_pool_t
*dp
= dmu_objset_pool(os
);
634 dsl_dataset_rele(os
->os_dsl_dataset
, tag
);
635 dsl_pool_rele(dp
, tag
);
639 * When we are called, os MUST refer to an objset associated with a dataset
640 * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner
641 * == tag. We will then release and reacquire ownership of the dataset while
642 * holding the pool config_rwlock to avoid intervening namespace or ownership
645 * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to
646 * release the hold on its dataset and acquire a new one on the dataset of the
647 * same name so that it can be partially torn down and reconstructed.
650 dmu_objset_refresh_ownership(objset_t
*os
, void *tag
)
653 dsl_dataset_t
*ds
, *newds
;
654 char name
[MAXNAMELEN
];
656 ds
= os
->os_dsl_dataset
;
657 VERIFY3P(ds
, !=, NULL
);
658 VERIFY3P(ds
->ds_owner
, ==, tag
);
659 VERIFY(dsl_dataset_long_held(ds
));
661 dsl_dataset_name(ds
, name
);
662 dp
= dmu_objset_pool(os
);
663 dsl_pool_config_enter(dp
, FTAG
);
664 dmu_objset_disown(os
, tag
);
665 VERIFY0(dsl_dataset_own(dp
, name
, tag
, &newds
));
666 VERIFY3P(newds
, ==, os
->os_dsl_dataset
);
667 dsl_pool_config_exit(dp
, FTAG
);
671 dmu_objset_disown(objset_t
*os
, void *tag
)
673 dsl_dataset_disown(os
->os_dsl_dataset
, tag
);
677 dmu_objset_evict_dbufs(objset_t
*os
)
682 dn_marker
= kmem_alloc(sizeof (dnode_t
), KM_SLEEP
);
684 mutex_enter(&os
->os_lock
);
685 dn
= list_head(&os
->os_dnodes
);
688 * Skip dnodes without holds. We have to do this dance
689 * because dnode_add_ref() only works if there is already a
690 * hold. If the dnode has no holds, then it has no dbufs.
692 if (dnode_add_ref(dn
, FTAG
)) {
693 list_insert_after(&os
->os_dnodes
, dn
, dn_marker
);
694 mutex_exit(&os
->os_lock
);
696 dnode_evict_dbufs(dn
);
697 dnode_rele(dn
, FTAG
);
699 mutex_enter(&os
->os_lock
);
700 dn
= list_next(&os
->os_dnodes
, dn_marker
);
701 list_remove(&os
->os_dnodes
, dn_marker
);
703 dn
= list_next(&os
->os_dnodes
, dn
);
706 mutex_exit(&os
->os_lock
);
708 kmem_free(dn_marker
, sizeof (dnode_t
));
710 if (DMU_USERUSED_DNODE(os
) != NULL
) {
711 dnode_evict_dbufs(DMU_GROUPUSED_DNODE(os
));
712 dnode_evict_dbufs(DMU_USERUSED_DNODE(os
));
714 dnode_evict_dbufs(DMU_META_DNODE(os
));
718 * Objset eviction processing is split into into two pieces.
719 * The first marks the objset as evicting, evicts any dbufs that
720 * have a refcount of zero, and then queues up the objset for the
721 * second phase of eviction. Once os->os_dnodes has been cleared by
722 * dnode_buf_pageout()->dnode_destroy(), the second phase is executed.
723 * The second phase closes the special dnodes, dequeues the objset from
724 * the list of those undergoing eviction, and finally frees the objset.
726 * NOTE: Due to asynchronous eviction processing (invocation of
727 * dnode_buf_pageout()), it is possible for the meta dnode for the
728 * objset to have no holds even though os->os_dnodes is not empty.
731 dmu_objset_evict(objset_t
*os
)
735 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
737 for (t
= 0; t
< TXG_SIZE
; t
++)
738 ASSERT(!dmu_objset_is_dirty(os
, t
));
741 dsl_prop_unregister_all(ds
, os
);
746 dmu_objset_evict_dbufs(os
);
748 mutex_enter(&os
->os_lock
);
749 spa_evicting_os_register(os
->os_spa
, os
);
750 if (list_is_empty(&os
->os_dnodes
)) {
751 mutex_exit(&os
->os_lock
);
752 dmu_objset_evict_done(os
);
754 mutex_exit(&os
->os_lock
);
759 dmu_objset_evict_done(objset_t
*os
)
761 ASSERT3P(list_head(&os
->os_dnodes
), ==, NULL
);
763 dnode_special_close(&os
->os_meta_dnode
);
764 if (DMU_USERUSED_DNODE(os
)) {
765 dnode_special_close(&os
->os_userused_dnode
);
766 dnode_special_close(&os
->os_groupused_dnode
);
768 zil_free(os
->os_zil
);
770 VERIFY(arc_buf_remove_ref(os
->os_phys_buf
, &os
->os_phys_buf
));
773 * This is a barrier to prevent the objset from going away in
774 * dnode_move() until we can safely ensure that the objset is still in
775 * use. We consider the objset valid before the barrier and invalid
778 rw_enter(&os_lock
, RW_READER
);
781 mutex_destroy(&os
->os_lock
);
782 mutex_destroy(&os
->os_obj_lock
);
783 mutex_destroy(&os
->os_user_ptr_lock
);
784 spa_evicting_os_deregister(os
->os_spa
, os
);
785 kmem_free(os
, sizeof (objset_t
));
789 dmu_objset_snap_cmtime(objset_t
*os
)
791 return (dsl_dir_snap_cmtime(os
->os_dsl_dataset
->ds_dir
));
794 /* called from dsl for meta-objset */
796 dmu_objset_create_impl(spa_t
*spa
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
797 dmu_objset_type_t type
, dmu_tx_t
*tx
)
802 ASSERT(dmu_tx_is_syncing(tx
));
805 VERIFY0(dmu_objset_from_ds(ds
, &os
));
807 VERIFY0(dmu_objset_open_impl(spa
, NULL
, bp
, &os
));
809 mdn
= DMU_META_DNODE(os
);
811 dnode_allocate(mdn
, DMU_OT_DNODE
, DNODE_BLOCK_SIZE
, DN_MAX_INDBLKSHIFT
,
812 DMU_OT_NONE
, 0, DNODE_MIN_SLOTS
, tx
);
815 * We don't want to have to increase the meta-dnode's nlevels
816 * later, because then we could do it in quescing context while
817 * we are also accessing it in open context.
819 * This precaution is not necessary for the MOS (ds == NULL),
820 * because the MOS is only updated in syncing context.
821 * This is most fortunate: the MOS is the only objset that
822 * needs to be synced multiple times as spa_sync() iterates
823 * to convergence, so minimizing its dn_nlevels matters.
829 * Determine the number of levels necessary for the meta-dnode
830 * to contain DN_MAX_OBJECT dnodes.
832 while ((uint64_t)mdn
->dn_nblkptr
<< (mdn
->dn_datablkshift
+
833 (levels
- 1) * (mdn
->dn_indblkshift
- SPA_BLKPTRSHIFT
)) <
834 DN_MAX_OBJECT
* sizeof (dnode_phys_t
))
837 mdn
->dn_next_nlevels
[tx
->tx_txg
& TXG_MASK
] =
838 mdn
->dn_nlevels
= levels
;
841 ASSERT(type
!= DMU_OST_NONE
);
842 ASSERT(type
!= DMU_OST_ANY
);
843 ASSERT(type
< DMU_OST_NUMTYPES
);
844 os
->os_phys
->os_type
= type
;
845 if (dmu_objset_userused_enabled(os
)) {
846 os
->os_phys
->os_flags
|= OBJSET_FLAG_USERACCOUNTING_COMPLETE
;
847 os
->os_flags
= os
->os_phys
->os_flags
;
850 dsl_dataset_dirty(ds
, tx
);
855 typedef struct dmu_objset_create_arg
{
856 const char *doca_name
;
858 void (*doca_userfunc
)(objset_t
*os
, void *arg
,
859 cred_t
*cr
, dmu_tx_t
*tx
);
861 dmu_objset_type_t doca_type
;
863 } dmu_objset_create_arg_t
;
867 dmu_objset_create_check(void *arg
, dmu_tx_t
*tx
)
869 dmu_objset_create_arg_t
*doca
= arg
;
870 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
875 if (strchr(doca
->doca_name
, '@') != NULL
)
876 return (SET_ERROR(EINVAL
));
878 error
= dsl_dir_hold(dp
, doca
->doca_name
, FTAG
, &pdd
, &tail
);
882 dsl_dir_rele(pdd
, FTAG
);
883 return (SET_ERROR(EEXIST
));
885 error
= dsl_fs_ss_limit_check(pdd
, 1, ZFS_PROP_FILESYSTEM_LIMIT
, NULL
,
887 dsl_dir_rele(pdd
, FTAG
);
893 dmu_objset_create_sync(void *arg
, dmu_tx_t
*tx
)
895 dmu_objset_create_arg_t
*doca
= arg
;
896 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
904 VERIFY0(dsl_dir_hold(dp
, doca
->doca_name
, FTAG
, &pdd
, &tail
));
906 obj
= dsl_dataset_create_sync(pdd
, tail
, NULL
, doca
->doca_flags
,
907 doca
->doca_cred
, tx
);
909 VERIFY0(dsl_dataset_hold_obj(pdd
->dd_pool
, obj
, FTAG
, &ds
));
910 bp
= dsl_dataset_get_blkptr(ds
);
911 os
= dmu_objset_create_impl(pdd
->dd_pool
->dp_spa
,
912 ds
, bp
, doca
->doca_type
, tx
);
914 if (doca
->doca_userfunc
!= NULL
) {
915 doca
->doca_userfunc(os
, doca
->doca_userarg
,
916 doca
->doca_cred
, tx
);
919 spa_history_log_internal_ds(ds
, "create", tx
, "");
920 zvol_create_minors(dp
->dp_spa
, doca
->doca_name
, B_TRUE
);
922 dsl_dataset_rele(ds
, FTAG
);
923 dsl_dir_rele(pdd
, FTAG
);
927 dmu_objset_create(const char *name
, dmu_objset_type_t type
, uint64_t flags
,
928 void (*func
)(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
), void *arg
)
930 dmu_objset_create_arg_t doca
;
932 doca
.doca_name
= name
;
933 doca
.doca_cred
= CRED();
934 doca
.doca_flags
= flags
;
935 doca
.doca_userfunc
= func
;
936 doca
.doca_userarg
= arg
;
937 doca
.doca_type
= type
;
939 return (dsl_sync_task(name
,
940 dmu_objset_create_check
, dmu_objset_create_sync
, &doca
,
941 5, ZFS_SPACE_CHECK_NORMAL
));
944 typedef struct dmu_objset_clone_arg
{
945 const char *doca_clone
;
946 const char *doca_origin
;
948 } dmu_objset_clone_arg_t
;
952 dmu_objset_clone_check(void *arg
, dmu_tx_t
*tx
)
954 dmu_objset_clone_arg_t
*doca
= arg
;
958 dsl_dataset_t
*origin
;
959 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
961 if (strchr(doca
->doca_clone
, '@') != NULL
)
962 return (SET_ERROR(EINVAL
));
964 error
= dsl_dir_hold(dp
, doca
->doca_clone
, FTAG
, &pdd
, &tail
);
968 dsl_dir_rele(pdd
, FTAG
);
969 return (SET_ERROR(EEXIST
));
972 error
= dsl_fs_ss_limit_check(pdd
, 1, ZFS_PROP_FILESYSTEM_LIMIT
, NULL
,
975 dsl_dir_rele(pdd
, FTAG
);
976 return (SET_ERROR(EDQUOT
));
978 dsl_dir_rele(pdd
, FTAG
);
980 error
= dsl_dataset_hold(dp
, doca
->doca_origin
, FTAG
, &origin
);
984 /* You can only clone snapshots, not the head datasets. */
985 if (!origin
->ds_is_snapshot
) {
986 dsl_dataset_rele(origin
, FTAG
);
987 return (SET_ERROR(EINVAL
));
989 dsl_dataset_rele(origin
, FTAG
);
995 dmu_objset_clone_sync(void *arg
, dmu_tx_t
*tx
)
997 dmu_objset_clone_arg_t
*doca
= arg
;
998 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1001 dsl_dataset_t
*origin
, *ds
;
1003 char namebuf
[MAXNAMELEN
];
1005 VERIFY0(dsl_dir_hold(dp
, doca
->doca_clone
, FTAG
, &pdd
, &tail
));
1006 VERIFY0(dsl_dataset_hold(dp
, doca
->doca_origin
, FTAG
, &origin
));
1008 obj
= dsl_dataset_create_sync(pdd
, tail
, origin
, 0,
1009 doca
->doca_cred
, tx
);
1011 VERIFY0(dsl_dataset_hold_obj(pdd
->dd_pool
, obj
, FTAG
, &ds
));
1012 dsl_dataset_name(origin
, namebuf
);
1013 spa_history_log_internal_ds(ds
, "clone", tx
,
1014 "origin=%s (%llu)", namebuf
, origin
->ds_object
);
1015 zvol_create_minors(dp
->dp_spa
, doca
->doca_clone
, B_TRUE
);
1016 dsl_dataset_rele(ds
, FTAG
);
1017 dsl_dataset_rele(origin
, FTAG
);
1018 dsl_dir_rele(pdd
, FTAG
);
1022 dmu_objset_clone(const char *clone
, const char *origin
)
1024 dmu_objset_clone_arg_t doca
;
1026 doca
.doca_clone
= clone
;
1027 doca
.doca_origin
= origin
;
1028 doca
.doca_cred
= CRED();
1030 return (dsl_sync_task(clone
,
1031 dmu_objset_clone_check
, dmu_objset_clone_sync
, &doca
,
1032 5, ZFS_SPACE_CHECK_NORMAL
));
1036 dmu_objset_snapshot_one(const char *fsname
, const char *snapname
)
1039 char *longsnap
= kmem_asprintf("%s@%s", fsname
, snapname
);
1040 nvlist_t
*snaps
= fnvlist_alloc();
1042 fnvlist_add_boolean(snaps
, longsnap
);
1044 err
= dsl_dataset_snapshot(snaps
, NULL
, NULL
);
1045 fnvlist_free(snaps
);
1050 dmu_objset_sync_dnodes(list_t
*list
, list_t
*newlist
, dmu_tx_t
*tx
)
1054 while ((dn
= list_head(list
))) {
1055 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
);
1056 ASSERT(dn
->dn_dbuf
->db_data_pending
);
1058 * Initialize dn_zio outside dnode_sync() because the
1059 * meta-dnode needs to set it ouside dnode_sync().
1061 dn
->dn_zio
= dn
->dn_dbuf
->db_data_pending
->dr_zio
;
1064 ASSERT3U(dn
->dn_nlevels
, <=, DN_MAX_LEVELS
);
1065 list_remove(list
, dn
);
1068 (void) dnode_add_ref(dn
, newlist
);
1069 list_insert_tail(newlist
, dn
);
1078 dmu_objset_write_ready(zio_t
*zio
, arc_buf_t
*abuf
, void *arg
)
1082 blkptr_t
*bp
= zio
->io_bp
;
1084 dnode_phys_t
*dnp
= &os
->os_phys
->os_meta_dnode
;
1086 ASSERT(!BP_IS_EMBEDDED(bp
));
1087 ASSERT3P(bp
, ==, os
->os_rootbp
);
1088 ASSERT3U(BP_GET_TYPE(bp
), ==, DMU_OT_OBJSET
);
1089 ASSERT0(BP_GET_LEVEL(bp
));
1092 * Update rootbp fill count: it should be the number of objects
1093 * allocated in the object set (not counting the "special"
1094 * objects that are stored in the objset_phys_t -- the meta
1095 * dnode and user/group accounting objects).
1098 for (i
= 0; i
< dnp
->dn_nblkptr
; i
++)
1099 bp
->blk_fill
+= BP_GET_FILL(&dnp
->dn_blkptr
[i
]);
1104 dmu_objset_write_done(zio_t
*zio
, arc_buf_t
*abuf
, void *arg
)
1106 blkptr_t
*bp
= zio
->io_bp
;
1107 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
1110 if (zio
->io_flags
& ZIO_FLAG_IO_REWRITE
) {
1111 ASSERT(BP_EQUAL(bp
, bp_orig
));
1113 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
1114 dmu_tx_t
*tx
= os
->os_synctx
;
1116 (void) dsl_dataset_block_kill(ds
, bp_orig
, tx
, B_TRUE
);
1117 dsl_dataset_block_born(ds
, bp
, tx
);
1121 /* called from dsl */
1123 dmu_objset_sync(objset_t
*os
, zio_t
*pio
, dmu_tx_t
*tx
)
1126 zbookmark_phys_t zb
;
1130 list_t
*newlist
= NULL
;
1131 dbuf_dirty_record_t
*dr
;
1133 dprintf_ds(os
->os_dsl_dataset
, "txg=%llu\n", tx
->tx_txg
);
1135 ASSERT(dmu_tx_is_syncing(tx
));
1136 /* XXX the write_done callback should really give us the tx... */
1139 if (os
->os_dsl_dataset
== NULL
) {
1141 * This is the MOS. If we have upgraded,
1142 * spa_max_replication() could change, so reset
1145 os
->os_copies
= spa_max_replication(os
->os_spa
);
1149 * Create the root block IO
1151 SET_BOOKMARK(&zb
, os
->os_dsl_dataset
?
1152 os
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
1153 ZB_ROOT_OBJECT
, ZB_ROOT_LEVEL
, ZB_ROOT_BLKID
);
1154 arc_release(os
->os_phys_buf
, &os
->os_phys_buf
);
1156 dmu_write_policy(os
, NULL
, 0, 0, &zp
);
1158 zio
= arc_write(pio
, os
->os_spa
, tx
->tx_txg
,
1159 os
->os_rootbp
, os
->os_phys_buf
, DMU_OS_IS_L2CACHEABLE(os
),
1160 DMU_OS_IS_L2COMPRESSIBLE(os
),
1161 &zp
, dmu_objset_write_ready
, NULL
, NULL
, dmu_objset_write_done
,
1162 os
, ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);
1165 * Sync special dnodes - the parent IO for the sync is the root block
1167 DMU_META_DNODE(os
)->dn_zio
= zio
;
1168 dnode_sync(DMU_META_DNODE(os
), tx
);
1170 os
->os_phys
->os_flags
= os
->os_flags
;
1172 if (DMU_USERUSED_DNODE(os
) &&
1173 DMU_USERUSED_DNODE(os
)->dn_type
!= DMU_OT_NONE
) {
1174 DMU_USERUSED_DNODE(os
)->dn_zio
= zio
;
1175 dnode_sync(DMU_USERUSED_DNODE(os
), tx
);
1176 DMU_GROUPUSED_DNODE(os
)->dn_zio
= zio
;
1177 dnode_sync(DMU_GROUPUSED_DNODE(os
), tx
);
1180 txgoff
= tx
->tx_txg
& TXG_MASK
;
1182 if (dmu_objset_userused_enabled(os
)) {
1183 newlist
= &os
->os_synced_dnodes
;
1185 * We must create the list here because it uses the
1186 * dn_dirty_link[] of this txg.
1188 list_create(newlist
, sizeof (dnode_t
),
1189 offsetof(dnode_t
, dn_dirty_link
[txgoff
]));
1192 dmu_objset_sync_dnodes(&os
->os_free_dnodes
[txgoff
], newlist
, tx
);
1193 dmu_objset_sync_dnodes(&os
->os_dirty_dnodes
[txgoff
], newlist
, tx
);
1195 list
= &DMU_META_DNODE(os
)->dn_dirty_records
[txgoff
];
1196 while ((dr
= list_head(list
))) {
1197 ASSERT0(dr
->dr_dbuf
->db_level
);
1198 list_remove(list
, dr
);
1200 zio_nowait(dr
->dr_zio
);
1203 /* Enable dnode backfill if enough objects have been freed. */
1204 if (os
->os_freed_dnodes
>= dmu_rescan_dnode_threshold
) {
1205 os
->os_rescan_dnodes
= B_TRUE
;
1206 os
->os_freed_dnodes
= 0;
1210 * Free intent log blocks up to this tx.
1212 zil_sync(os
->os_zil
, tx
);
1213 os
->os_phys
->os_zil_header
= os
->os_zil_header
;
1218 dmu_objset_is_dirty(objset_t
*os
, uint64_t txg
)
1220 return (!list_is_empty(&os
->os_dirty_dnodes
[txg
& TXG_MASK
]) ||
1221 !list_is_empty(&os
->os_free_dnodes
[txg
& TXG_MASK
]));
1224 static objset_used_cb_t
*used_cbs
[DMU_OST_NUMTYPES
];
1227 dmu_objset_register_type(dmu_objset_type_t ost
, objset_used_cb_t
*cb
)
1233 dmu_objset_userused_enabled(objset_t
*os
)
1235 return (spa_version(os
->os_spa
) >= SPA_VERSION_USERSPACE
&&
1236 used_cbs
[os
->os_phys
->os_type
] != NULL
&&
1237 DMU_USERUSED_DNODE(os
) != NULL
);
1241 do_userquota_update(objset_t
*os
, uint64_t used
, uint64_t flags
,
1242 uint64_t user
, uint64_t group
, boolean_t subtract
, dmu_tx_t
*tx
)
1244 if ((flags
& DNODE_FLAG_USERUSED_ACCOUNTED
)) {
1245 int64_t delta
= DNODE_MIN_SIZE
+ used
;
1248 VERIFY3U(0, ==, zap_increment_int(os
, DMU_USERUSED_OBJECT
,
1250 VERIFY3U(0, ==, zap_increment_int(os
, DMU_GROUPUSED_OBJECT
,
1256 dmu_objset_do_userquota_updates(objset_t
*os
, dmu_tx_t
*tx
)
1259 list_t
*list
= &os
->os_synced_dnodes
;
1261 ASSERT(list_head(list
) == NULL
|| dmu_objset_userused_enabled(os
));
1263 while ((dn
= list_head(list
))) {
1265 ASSERT(!DMU_OBJECT_IS_SPECIAL(dn
->dn_object
));
1266 ASSERT(dn
->dn_phys
->dn_type
== DMU_OT_NONE
||
1267 dn
->dn_phys
->dn_flags
&
1268 DNODE_FLAG_USERUSED_ACCOUNTED
);
1270 /* Allocate the user/groupused objects if necessary. */
1271 if (DMU_USERUSED_DNODE(os
)->dn_type
== DMU_OT_NONE
) {
1272 VERIFY(0 == zap_create_claim(os
,
1273 DMU_USERUSED_OBJECT
,
1274 DMU_OT_USERGROUP_USED
, DMU_OT_NONE
, 0, tx
));
1275 VERIFY(0 == zap_create_claim(os
,
1276 DMU_GROUPUSED_OBJECT
,
1277 DMU_OT_USERGROUP_USED
, DMU_OT_NONE
, 0, tx
));
1281 * We intentionally modify the zap object even if the
1282 * net delta is zero. Otherwise
1283 * the block of the zap obj could be shared between
1284 * datasets but need to be different between them after
1288 flags
= dn
->dn_id_flags
;
1290 if (flags
& DN_ID_OLD_EXIST
) {
1291 do_userquota_update(os
, dn
->dn_oldused
, dn
->dn_oldflags
,
1292 dn
->dn_olduid
, dn
->dn_oldgid
, B_TRUE
, tx
);
1294 if (flags
& DN_ID_NEW_EXIST
) {
1295 do_userquota_update(os
, DN_USED_BYTES(dn
->dn_phys
),
1296 dn
->dn_phys
->dn_flags
, dn
->dn_newuid
,
1297 dn
->dn_newgid
, B_FALSE
, tx
);
1300 mutex_enter(&dn
->dn_mtx
);
1302 dn
->dn_oldflags
= 0;
1303 if (dn
->dn_id_flags
& DN_ID_NEW_EXIST
) {
1304 dn
->dn_olduid
= dn
->dn_newuid
;
1305 dn
->dn_oldgid
= dn
->dn_newgid
;
1306 dn
->dn_id_flags
|= DN_ID_OLD_EXIST
;
1307 if (dn
->dn_bonuslen
== 0)
1308 dn
->dn_id_flags
|= DN_ID_CHKED_SPILL
;
1310 dn
->dn_id_flags
|= DN_ID_CHKED_BONUS
;
1312 dn
->dn_id_flags
&= ~(DN_ID_NEW_EXIST
);
1313 mutex_exit(&dn
->dn_mtx
);
1315 list_remove(list
, dn
);
1316 dnode_rele(dn
, list
);
1321 * Returns a pointer to data to find uid/gid from
1323 * If a dirty record for transaction group that is syncing can't
1324 * be found then NULL is returned. In the NULL case it is assumed
1325 * the uid/gid aren't changing.
1328 dmu_objset_userquota_find_data(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1330 dbuf_dirty_record_t
*dr
, **drp
;
1333 if (db
->db_dirtycnt
== 0)
1334 return (db
->db
.db_data
); /* Nothing is changing */
1336 for (drp
= &db
->db_last_dirty
; (dr
= *drp
) != NULL
; drp
= &dr
->dr_next
)
1337 if (dr
->dr_txg
== tx
->tx_txg
)
1345 DB_DNODE_ENTER(dr
->dr_dbuf
);
1346 dn
= DB_DNODE(dr
->dr_dbuf
);
1348 if (dn
->dn_bonuslen
== 0 &&
1349 dr
->dr_dbuf
->db_blkid
== DMU_SPILL_BLKID
)
1350 data
= dr
->dt
.dl
.dr_data
->b_data
;
1352 data
= dr
->dt
.dl
.dr_data
;
1354 DB_DNODE_EXIT(dr
->dr_dbuf
);
1361 dmu_objset_userquota_get_ids(dnode_t
*dn
, boolean_t before
, dmu_tx_t
*tx
)
1363 objset_t
*os
= dn
->dn_objset
;
1365 dmu_buf_impl_t
*db
= NULL
;
1366 uint64_t *user
= NULL
;
1367 uint64_t *group
= NULL
;
1368 int flags
= dn
->dn_id_flags
;
1370 boolean_t have_spill
= B_FALSE
;
1372 if (!dmu_objset_userused_enabled(dn
->dn_objset
))
1375 if (before
&& (flags
& (DN_ID_CHKED_BONUS
|DN_ID_OLD_EXIST
|
1376 DN_ID_CHKED_SPILL
)))
1379 if (before
&& dn
->dn_bonuslen
!= 0)
1380 data
= DN_BONUS(dn
->dn_phys
);
1381 else if (!before
&& dn
->dn_bonuslen
!= 0) {
1384 mutex_enter(&db
->db_mtx
);
1385 data
= dmu_objset_userquota_find_data(db
, tx
);
1387 data
= DN_BONUS(dn
->dn_phys
);
1389 } else if (dn
->dn_bonuslen
== 0 && dn
->dn_bonustype
== DMU_OT_SA
) {
1392 if (RW_WRITE_HELD(&dn
->dn_struct_rwlock
))
1393 rf
|= DB_RF_HAVESTRUCT
;
1394 error
= dmu_spill_hold_by_dnode(dn
,
1395 rf
| DB_RF_MUST_SUCCEED
,
1396 FTAG
, (dmu_buf_t
**)&db
);
1398 mutex_enter(&db
->db_mtx
);
1399 data
= (before
) ? db
->db
.db_data
:
1400 dmu_objset_userquota_find_data(db
, tx
);
1401 have_spill
= B_TRUE
;
1403 mutex_enter(&dn
->dn_mtx
);
1404 dn
->dn_id_flags
|= DN_ID_CHKED_BONUS
;
1405 mutex_exit(&dn
->dn_mtx
);
1411 user
= &dn
->dn_olduid
;
1412 group
= &dn
->dn_oldgid
;
1414 user
= &dn
->dn_newuid
;
1415 group
= &dn
->dn_newgid
;
1419 * Must always call the callback in case the object
1420 * type has changed and that type isn't an object type to track
1422 error
= used_cbs
[os
->os_phys
->os_type
](dn
->dn_bonustype
, data
,
1426 * Preserve existing uid/gid when the callback can't determine
1427 * what the new uid/gid are and the callback returned EEXIST.
1428 * The EEXIST error tells us to just use the existing uid/gid.
1429 * If we don't know what the old values are then just assign
1430 * them to 0, since that is a new file being created.
1432 if (!before
&& data
== NULL
&& error
== EEXIST
) {
1433 if (flags
& DN_ID_OLD_EXIST
) {
1434 dn
->dn_newuid
= dn
->dn_olduid
;
1435 dn
->dn_newgid
= dn
->dn_oldgid
;
1444 mutex_exit(&db
->db_mtx
);
1446 mutex_enter(&dn
->dn_mtx
);
1447 if (error
== 0 && before
)
1448 dn
->dn_id_flags
|= DN_ID_OLD_EXIST
;
1449 if (error
== 0 && !before
)
1450 dn
->dn_id_flags
|= DN_ID_NEW_EXIST
;
1453 dn
->dn_id_flags
|= DN_ID_CHKED_SPILL
;
1455 dn
->dn_id_flags
|= DN_ID_CHKED_BONUS
;
1457 mutex_exit(&dn
->dn_mtx
);
1459 dmu_buf_rele((dmu_buf_t
*)db
, FTAG
);
1463 dmu_objset_userspace_present(objset_t
*os
)
1465 return (os
->os_phys
->os_flags
&
1466 OBJSET_FLAG_USERACCOUNTING_COMPLETE
);
1470 dmu_objset_userspace_upgrade(objset_t
*os
)
1475 if (dmu_objset_userspace_present(os
))
1477 if (!dmu_objset_userused_enabled(os
))
1478 return (SET_ERROR(ENOTSUP
));
1479 if (dmu_objset_is_snapshot(os
))
1480 return (SET_ERROR(EINVAL
));
1483 * We simply need to mark every object dirty, so that it will be
1484 * synced out and now accounted. If this is called
1485 * concurrently, or if we already did some work before crashing,
1486 * that's fine, since we track each object's accounted state
1490 for (obj
= 0; err
== 0; err
= dmu_object_next(os
, &obj
, FALSE
, 0)) {
1495 if (issig(JUSTLOOKING
) && issig(FORREAL
))
1496 return (SET_ERROR(EINTR
));
1498 objerr
= dmu_bonus_hold(os
, obj
, FTAG
, &db
);
1501 tx
= dmu_tx_create(os
);
1502 dmu_tx_hold_bonus(tx
, obj
);
1503 objerr
= dmu_tx_assign(tx
, TXG_WAIT
);
1508 dmu_buf_will_dirty(db
, tx
);
1509 dmu_buf_rele(db
, FTAG
);
1513 os
->os_flags
|= OBJSET_FLAG_USERACCOUNTING_COMPLETE
;
1514 txg_wait_synced(dmu_objset_pool(os
), 0);
1519 dmu_objset_space(objset_t
*os
, uint64_t *refdbytesp
, uint64_t *availbytesp
,
1520 uint64_t *usedobjsp
, uint64_t *availobjsp
)
1522 dsl_dataset_space(os
->os_dsl_dataset
, refdbytesp
, availbytesp
,
1523 usedobjsp
, availobjsp
);
1527 dmu_objset_fsid_guid(objset_t
*os
)
1529 return (dsl_dataset_fsid_guid(os
->os_dsl_dataset
));
1533 dmu_objset_fast_stat(objset_t
*os
, dmu_objset_stats_t
*stat
)
1535 stat
->dds_type
= os
->os_phys
->os_type
;
1536 if (os
->os_dsl_dataset
)
1537 dsl_dataset_fast_stat(os
->os_dsl_dataset
, stat
);
1541 dmu_objset_stats(objset_t
*os
, nvlist_t
*nv
)
1543 ASSERT(os
->os_dsl_dataset
||
1544 os
->os_phys
->os_type
== DMU_OST_META
);
1546 if (os
->os_dsl_dataset
!= NULL
)
1547 dsl_dataset_stats(os
->os_dsl_dataset
, nv
);
1549 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_TYPE
,
1550 os
->os_phys
->os_type
);
1551 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_USERACCOUNTING
,
1552 dmu_objset_userspace_present(os
));
1556 dmu_objset_is_snapshot(objset_t
*os
)
1558 if (os
->os_dsl_dataset
!= NULL
)
1559 return (os
->os_dsl_dataset
->ds_is_snapshot
);
1565 dmu_snapshot_realname(objset_t
*os
, char *name
, char *real
, int maxlen
,
1566 boolean_t
*conflict
)
1568 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
1571 if (dsl_dataset_phys(ds
)->ds_snapnames_zapobj
== 0)
1572 return (SET_ERROR(ENOENT
));
1574 return (zap_lookup_norm(ds
->ds_dir
->dd_pool
->dp_meta_objset
,
1575 dsl_dataset_phys(ds
)->ds_snapnames_zapobj
, name
, 8, 1, &ignored
,
1576 MT_FIRST
, real
, maxlen
, conflict
));
1580 dmu_snapshot_list_next(objset_t
*os
, int namelen
, char *name
,
1581 uint64_t *idp
, uint64_t *offp
, boolean_t
*case_conflict
)
1583 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
1584 zap_cursor_t cursor
;
1585 zap_attribute_t attr
;
1587 ASSERT(dsl_pool_config_held(dmu_objset_pool(os
)));
1589 if (dsl_dataset_phys(ds
)->ds_snapnames_zapobj
== 0)
1590 return (SET_ERROR(ENOENT
));
1592 zap_cursor_init_serialized(&cursor
,
1593 ds
->ds_dir
->dd_pool
->dp_meta_objset
,
1594 dsl_dataset_phys(ds
)->ds_snapnames_zapobj
, *offp
);
1596 if (zap_cursor_retrieve(&cursor
, &attr
) != 0) {
1597 zap_cursor_fini(&cursor
);
1598 return (SET_ERROR(ENOENT
));
1601 if (strlen(attr
.za_name
) + 1 > namelen
) {
1602 zap_cursor_fini(&cursor
);
1603 return (SET_ERROR(ENAMETOOLONG
));
1606 (void) strcpy(name
, attr
.za_name
);
1608 *idp
= attr
.za_first_integer
;
1610 *case_conflict
= attr
.za_normalization_conflict
;
1611 zap_cursor_advance(&cursor
);
1612 *offp
= zap_cursor_serialize(&cursor
);
1613 zap_cursor_fini(&cursor
);
1619 dmu_snapshot_lookup(objset_t
*os
, const char *name
, uint64_t *value
)
1621 return (dsl_dataset_snap_lookup(os
->os_dsl_dataset
, name
, value
));
1625 dmu_dir_list_next(objset_t
*os
, int namelen
, char *name
,
1626 uint64_t *idp
, uint64_t *offp
)
1628 dsl_dir_t
*dd
= os
->os_dsl_dataset
->ds_dir
;
1629 zap_cursor_t cursor
;
1630 zap_attribute_t attr
;
1632 /* there is no next dir on a snapshot! */
1633 if (os
->os_dsl_dataset
->ds_object
!=
1634 dsl_dir_phys(dd
)->dd_head_dataset_obj
)
1635 return (SET_ERROR(ENOENT
));
1637 zap_cursor_init_serialized(&cursor
,
1638 dd
->dd_pool
->dp_meta_objset
,
1639 dsl_dir_phys(dd
)->dd_child_dir_zapobj
, *offp
);
1641 if (zap_cursor_retrieve(&cursor
, &attr
) != 0) {
1642 zap_cursor_fini(&cursor
);
1643 return (SET_ERROR(ENOENT
));
1646 if (strlen(attr
.za_name
) + 1 > namelen
) {
1647 zap_cursor_fini(&cursor
);
1648 return (SET_ERROR(ENAMETOOLONG
));
1651 (void) strcpy(name
, attr
.za_name
);
1653 *idp
= attr
.za_first_integer
;
1654 zap_cursor_advance(&cursor
);
1655 *offp
= zap_cursor_serialize(&cursor
);
1656 zap_cursor_fini(&cursor
);
1661 typedef struct dmu_objset_find_ctx
{
1665 int (*dc_func
)(dsl_pool_t
*, dsl_dataset_t
*, void *);
1668 kmutex_t
*dc_error_lock
;
1670 } dmu_objset_find_ctx_t
;
1673 dmu_objset_find_dp_impl(dmu_objset_find_ctx_t
*dcp
)
1675 dsl_pool_t
*dp
= dcp
->dc_dp
;
1676 dmu_objset_find_ctx_t
*child_dcp
;
1680 zap_attribute_t
*attr
;
1684 /* don't process if there already was an error */
1685 if (*dcp
->dc_error
!= 0)
1688 err
= dsl_dir_hold_obj(dp
, dcp
->dc_ddobj
, NULL
, FTAG
, &dd
);
1692 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1693 if (dd
->dd_myname
[0] == '$') {
1694 dsl_dir_rele(dd
, FTAG
);
1698 thisobj
= dsl_dir_phys(dd
)->dd_head_dataset_obj
;
1699 attr
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
1702 * Iterate over all children.
1704 if (dcp
->dc_flags
& DS_FIND_CHILDREN
) {
1705 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
,
1706 dsl_dir_phys(dd
)->dd_child_dir_zapobj
);
1707 zap_cursor_retrieve(&zc
, attr
) == 0;
1708 (void) zap_cursor_advance(&zc
)) {
1709 ASSERT3U(attr
->za_integer_length
, ==,
1711 ASSERT3U(attr
->za_num_integers
, ==, 1);
1713 child_dcp
= kmem_alloc(sizeof (*child_dcp
), KM_SLEEP
);
1715 child_dcp
->dc_ddobj
= attr
->za_first_integer
;
1716 if (dcp
->dc_tq
!= NULL
)
1717 (void) taskq_dispatch(dcp
->dc_tq
,
1718 dmu_objset_find_dp_cb
, child_dcp
, TQ_SLEEP
);
1720 dmu_objset_find_dp_impl(child_dcp
);
1722 zap_cursor_fini(&zc
);
1726 * Iterate over all snapshots.
1728 if (dcp
->dc_flags
& DS_FIND_SNAPSHOTS
) {
1730 err
= dsl_dataset_hold_obj(dp
, thisobj
, FTAG
, &ds
);
1735 snapobj
= dsl_dataset_phys(ds
)->ds_snapnames_zapobj
;
1736 dsl_dataset_rele(ds
, FTAG
);
1738 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
, snapobj
);
1739 zap_cursor_retrieve(&zc
, attr
) == 0;
1740 (void) zap_cursor_advance(&zc
)) {
1741 ASSERT3U(attr
->za_integer_length
, ==,
1743 ASSERT3U(attr
->za_num_integers
, ==, 1);
1745 err
= dsl_dataset_hold_obj(dp
,
1746 attr
->za_first_integer
, FTAG
, &ds
);
1749 err
= dcp
->dc_func(dp
, ds
, dcp
->dc_arg
);
1750 dsl_dataset_rele(ds
, FTAG
);
1754 zap_cursor_fini(&zc
);
1758 dsl_dir_rele(dd
, FTAG
);
1759 kmem_free(attr
, sizeof (zap_attribute_t
));
1767 err
= dsl_dataset_hold_obj(dp
, thisobj
, FTAG
, &ds
);
1770 err
= dcp
->dc_func(dp
, ds
, dcp
->dc_arg
);
1771 dsl_dataset_rele(ds
, FTAG
);
1775 mutex_enter(dcp
->dc_error_lock
);
1776 /* only keep first error */
1777 if (*dcp
->dc_error
== 0)
1778 *dcp
->dc_error
= err
;
1779 mutex_exit(dcp
->dc_error_lock
);
1782 kmem_free(dcp
, sizeof (*dcp
));
1786 dmu_objset_find_dp_cb(void *arg
)
1788 dmu_objset_find_ctx_t
*dcp
= arg
;
1789 dsl_pool_t
*dp
= dcp
->dc_dp
;
1792 * We need to get a pool_config_lock here, as there are several
1793 * asssert(pool_config_held) down the stack. Getting a lock via
1794 * dsl_pool_config_enter is risky, as it might be stalled by a
1795 * pending writer. This would deadlock, as the write lock can
1796 * only be granted when our parent thread gives up the lock.
1797 * The _prio interface gives us priority over a pending writer.
1799 dsl_pool_config_enter_prio(dp
, FTAG
);
1801 dmu_objset_find_dp_impl(dcp
);
1803 dsl_pool_config_exit(dp
, FTAG
);
1807 * Find objsets under and including ddobj, call func(ds) on each.
1808 * The order for the enumeration is completely undefined.
1809 * func is called with dsl_pool_config held.
1812 dmu_objset_find_dp(dsl_pool_t
*dp
, uint64_t ddobj
,
1813 int func(dsl_pool_t
*, dsl_dataset_t
*, void *), void *arg
, int flags
)
1818 dmu_objset_find_ctx_t
*dcp
;
1821 mutex_init(&err_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1822 dcp
= kmem_alloc(sizeof (*dcp
), KM_SLEEP
);
1825 dcp
->dc_ddobj
= ddobj
;
1826 dcp
->dc_func
= func
;
1828 dcp
->dc_flags
= flags
;
1829 dcp
->dc_error_lock
= &err_lock
;
1830 dcp
->dc_error
= &error
;
1832 if ((flags
& DS_FIND_SERIALIZE
) || dsl_pool_config_held_writer(dp
)) {
1834 * In case a write lock is held we can't make use of
1835 * parallelism, as down the stack of the worker threads
1836 * the lock is asserted via dsl_pool_config_held.
1837 * In case of a read lock this is solved by getting a read
1838 * lock in each worker thread, which isn't possible in case
1839 * of a writer lock. So we fall back to the synchronous path
1841 * In the future it might be possible to get some magic into
1842 * dsl_pool_config_held in a way that it returns true for
1843 * the worker threads so that a single lock held from this
1844 * thread suffices. For now, stay single threaded.
1846 dmu_objset_find_dp_impl(dcp
);
1847 mutex_destroy(&err_lock
);
1852 ntasks
= dmu_find_threads
;
1854 ntasks
= vdev_count_leaves(dp
->dp_spa
) * 4;
1855 tq
= taskq_create("dmu_objset_find", ntasks
, maxclsyspri
, ntasks
,
1858 kmem_free(dcp
, sizeof (*dcp
));
1859 mutex_destroy(&err_lock
);
1861 return (SET_ERROR(ENOMEM
));
1865 /* dcp will be freed by task */
1866 (void) taskq_dispatch(tq
, dmu_objset_find_dp_cb
, dcp
, TQ_SLEEP
);
1869 * PORTING: this code relies on the property of taskq_wait to wait
1870 * until no more tasks are queued and no more tasks are active. As
1871 * we always queue new tasks from within other tasks, task_wait
1872 * reliably waits for the full recursion to finish, even though we
1873 * enqueue new tasks after taskq_wait has been called.
1874 * On platforms other than illumos, taskq_wait may not have this
1879 mutex_destroy(&err_lock
);
1885 * Find all objsets under name, and for each, call 'func(child_name, arg)'.
1886 * The dp_config_rwlock must not be held when this is called, and it
1887 * will not be held when the callback is called.
1888 * Therefore this function should only be used when the pool is not changing
1889 * (e.g. in syncing context), or the callback can deal with the possible races.
1892 dmu_objset_find_impl(spa_t
*spa
, const char *name
,
1893 int func(const char *, void *), void *arg
, int flags
)
1896 dsl_pool_t
*dp
= spa_get_dsl(spa
);
1899 zap_attribute_t
*attr
;
1904 dsl_pool_config_enter(dp
, FTAG
);
1906 err
= dsl_dir_hold(dp
, name
, FTAG
, &dd
, NULL
);
1908 dsl_pool_config_exit(dp
, FTAG
);
1912 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1913 if (dd
->dd_myname
[0] == '$') {
1914 dsl_dir_rele(dd
, FTAG
);
1915 dsl_pool_config_exit(dp
, FTAG
);
1919 thisobj
= dsl_dir_phys(dd
)->dd_head_dataset_obj
;
1920 attr
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
1923 * Iterate over all children.
1925 if (flags
& DS_FIND_CHILDREN
) {
1926 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
,
1927 dsl_dir_phys(dd
)->dd_child_dir_zapobj
);
1928 zap_cursor_retrieve(&zc
, attr
) == 0;
1929 (void) zap_cursor_advance(&zc
)) {
1930 ASSERT3U(attr
->za_integer_length
, ==,
1932 ASSERT3U(attr
->za_num_integers
, ==, 1);
1934 child
= kmem_asprintf("%s/%s", name
, attr
->za_name
);
1935 dsl_pool_config_exit(dp
, FTAG
);
1936 err
= dmu_objset_find_impl(spa
, child
,
1938 dsl_pool_config_enter(dp
, FTAG
);
1943 zap_cursor_fini(&zc
);
1946 dsl_dir_rele(dd
, FTAG
);
1947 dsl_pool_config_exit(dp
, FTAG
);
1948 kmem_free(attr
, sizeof (zap_attribute_t
));
1954 * Iterate over all snapshots.
1956 if (flags
& DS_FIND_SNAPSHOTS
) {
1957 err
= dsl_dataset_hold_obj(dp
, thisobj
, FTAG
, &ds
);
1962 snapobj
= dsl_dataset_phys(ds
)->ds_snapnames_zapobj
;
1963 dsl_dataset_rele(ds
, FTAG
);
1965 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
, snapobj
);
1966 zap_cursor_retrieve(&zc
, attr
) == 0;
1967 (void) zap_cursor_advance(&zc
)) {
1968 ASSERT3U(attr
->za_integer_length
, ==,
1970 ASSERT3U(attr
->za_num_integers
, ==, 1);
1972 child
= kmem_asprintf("%s@%s",
1973 name
, attr
->za_name
);
1974 dsl_pool_config_exit(dp
, FTAG
);
1975 err
= func(child
, arg
);
1976 dsl_pool_config_enter(dp
, FTAG
);
1981 zap_cursor_fini(&zc
);
1985 dsl_dir_rele(dd
, FTAG
);
1986 kmem_free(attr
, sizeof (zap_attribute_t
));
1987 dsl_pool_config_exit(dp
, FTAG
);
1992 /* Apply to self. */
1993 return (func(name
, arg
));
1997 * See comment above dmu_objset_find_impl().
2000 dmu_objset_find(char *name
, int func(const char *, void *), void *arg
,
2006 error
= spa_open(name
, &spa
, FTAG
);
2009 error
= dmu_objset_find_impl(spa
, name
, func
, arg
, flags
);
2010 spa_close(spa
, FTAG
);
2015 dmu_objset_set_user(objset_t
*os
, void *user_ptr
)
2017 ASSERT(MUTEX_HELD(&os
->os_user_ptr_lock
));
2018 os
->os_user_ptr
= user_ptr
;
2022 dmu_objset_get_user(objset_t
*os
)
2024 ASSERT(MUTEX_HELD(&os
->os_user_ptr_lock
));
2025 return (os
->os_user_ptr
);
2029 * Determine name of filesystem, given name of snapshot.
2030 * buf must be at least MAXNAMELEN bytes
2033 dmu_fsname(const char *snapname
, char *buf
)
2035 char *atp
= strchr(snapname
, '@');
2037 return (SET_ERROR(EINVAL
));
2038 if (atp
- snapname
>= MAXNAMELEN
)
2039 return (SET_ERROR(ENAMETOOLONG
));
2040 (void) strlcpy(buf
, snapname
, atp
- snapname
+ 1);
2044 #if defined(_KERNEL) && defined(HAVE_SPL)
2045 EXPORT_SYMBOL(dmu_objset_zil
);
2046 EXPORT_SYMBOL(dmu_objset_pool
);
2047 EXPORT_SYMBOL(dmu_objset_ds
);
2048 EXPORT_SYMBOL(dmu_objset_type
);
2049 EXPORT_SYMBOL(dmu_objset_name
);
2050 EXPORT_SYMBOL(dmu_objset_hold
);
2051 EXPORT_SYMBOL(dmu_objset_own
);
2052 EXPORT_SYMBOL(dmu_objset_rele
);
2053 EXPORT_SYMBOL(dmu_objset_disown
);
2054 EXPORT_SYMBOL(dmu_objset_from_ds
);
2055 EXPORT_SYMBOL(dmu_objset_create
);
2056 EXPORT_SYMBOL(dmu_objset_clone
);
2057 EXPORT_SYMBOL(dmu_objset_stats
);
2058 EXPORT_SYMBOL(dmu_objset_fast_stat
);
2059 EXPORT_SYMBOL(dmu_objset_spa
);
2060 EXPORT_SYMBOL(dmu_objset_space
);
2061 EXPORT_SYMBOL(dmu_objset_fsid_guid
);
2062 EXPORT_SYMBOL(dmu_objset_find
);
2063 EXPORT_SYMBOL(dmu_objset_byteswap
);
2064 EXPORT_SYMBOL(dmu_objset_evict_dbufs
);
2065 EXPORT_SYMBOL(dmu_objset_snap_cmtime
);
2066 EXPORT_SYMBOL(dmu_objset_dnodesize
);
2068 EXPORT_SYMBOL(dmu_objset_sync
);
2069 EXPORT_SYMBOL(dmu_objset_is_dirty
);
2070 EXPORT_SYMBOL(dmu_objset_create_impl
);
2071 EXPORT_SYMBOL(dmu_objset_open_impl
);
2072 EXPORT_SYMBOL(dmu_objset_evict
);
2073 EXPORT_SYMBOL(dmu_objset_register_type
);
2074 EXPORT_SYMBOL(dmu_objset_do_userquota_updates
);
2075 EXPORT_SYMBOL(dmu_objset_userquota_get_ids
);
2076 EXPORT_SYMBOL(dmu_objset_userused_enabled
);
2077 EXPORT_SYMBOL(dmu_objset_userspace_upgrade
);
2078 EXPORT_SYMBOL(dmu_objset_userspace_present
);