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]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
25 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
26 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
28 * Copyright (c) 2015, STRATO AG, Inc. All rights reserved.
29 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
30 * Copyright 2017 Nexenta Systems, Inc.
33 /* Portions Copyright 2010 Robert Milkowski */
35 #include <sys/zfeature.h>
37 #include <sys/zfs_context.h>
38 #include <sys/dmu_objset.h>
39 #include <sys/dsl_dir.h>
40 #include <sys/dsl_dataset.h>
41 #include <sys/dsl_prop.h>
42 #include <sys/dsl_pool.h>
43 #include <sys/dsl_synctask.h>
44 #include <sys/dsl_deleg.h>
45 #include <sys/dnode.h>
48 #include <sys/dmu_tx.h>
51 #include <sys/dmu_impl.h>
52 #include <sys/zfs_ioctl.h>
54 #include <sys/zfs_onexit.h>
55 #include <sys/dsl_destroy.h>
57 #include <sys/policy.h>
58 #include <sys/spa_impl.h>
61 * Needed to close a window in dnode_move() that allows the objset to be freed
62 * before it can be safely accessed.
67 * Tunable to overwrite the maximum number of threads for the parallelization
68 * of dmu_objset_find_dp, needed to speed up the import of pools with many
70 * Default is 4 times the number of leaf vdevs.
72 int dmu_find_threads
= 0;
75 * Backfill lower metadnode objects after this many have been freed.
76 * Backfilling negatively impacts object creation rates, so only do it
77 * if there are enough holes to fill.
79 int dmu_rescan_dnode_threshold
= 1 << DN_MAX_INDBLKSHIFT
;
81 static void dmu_objset_find_dp_cb(void *arg
);
83 static void dmu_objset_upgrade(objset_t
*os
, dmu_objset_upgrade_cb_t cb
);
84 static void dmu_objset_upgrade_stop(objset_t
*os
);
89 rw_init(&os_lock
, NULL
, RW_DEFAULT
, NULL
);
99 dmu_objset_spa(objset_t
*os
)
105 dmu_objset_zil(objset_t
*os
)
111 dmu_objset_pool(objset_t
*os
)
115 if ((ds
= os
->os_dsl_dataset
) != NULL
&& ds
->ds_dir
)
116 return (ds
->ds_dir
->dd_pool
);
118 return (spa_get_dsl(os
->os_spa
));
122 dmu_objset_ds(objset_t
*os
)
124 return (os
->os_dsl_dataset
);
128 dmu_objset_type(objset_t
*os
)
130 return (os
->os_phys
->os_type
);
134 dmu_objset_name(objset_t
*os
, char *buf
)
136 dsl_dataset_name(os
->os_dsl_dataset
, buf
);
140 dmu_objset_id(objset_t
*os
)
142 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
144 return (ds
? ds
->ds_object
: 0);
148 dmu_objset_dnodesize(objset_t
*os
)
150 return (os
->os_dnodesize
);
154 dmu_objset_syncprop(objset_t
*os
)
156 return (os
->os_sync
);
160 dmu_objset_logbias(objset_t
*os
)
162 return (os
->os_logbias
);
166 checksum_changed_cb(void *arg
, uint64_t newval
)
171 * Inheritance should have been done by now.
173 ASSERT(newval
!= ZIO_CHECKSUM_INHERIT
);
175 os
->os_checksum
= zio_checksum_select(newval
, ZIO_CHECKSUM_ON_VALUE
);
179 compression_changed_cb(void *arg
, uint64_t newval
)
184 * Inheritance and range checking should have been done by now.
186 ASSERT(newval
!= ZIO_COMPRESS_INHERIT
);
188 os
->os_compress
= zio_compress_select(os
->os_spa
, newval
,
193 copies_changed_cb(void *arg
, uint64_t newval
)
198 * Inheritance and range checking should have been done by now.
201 ASSERT(newval
<= spa_max_replication(os
->os_spa
));
203 os
->os_copies
= newval
;
207 dedup_changed_cb(void *arg
, uint64_t newval
)
210 spa_t
*spa
= os
->os_spa
;
211 enum zio_checksum checksum
;
214 * Inheritance should have been done by now.
216 ASSERT(newval
!= ZIO_CHECKSUM_INHERIT
);
218 checksum
= zio_checksum_dedup_select(spa
, newval
, ZIO_CHECKSUM_OFF
);
220 os
->os_dedup_checksum
= checksum
& ZIO_CHECKSUM_MASK
;
221 os
->os_dedup_verify
= !!(checksum
& ZIO_CHECKSUM_VERIFY
);
225 primary_cache_changed_cb(void *arg
, uint64_t newval
)
230 * Inheritance and range checking should have been done by now.
232 ASSERT(newval
== ZFS_CACHE_ALL
|| newval
== ZFS_CACHE_NONE
||
233 newval
== ZFS_CACHE_METADATA
);
235 os
->os_primary_cache
= newval
;
239 secondary_cache_changed_cb(void *arg
, uint64_t newval
)
244 * Inheritance and range checking should have been done by now.
246 ASSERT(newval
== ZFS_CACHE_ALL
|| newval
== ZFS_CACHE_NONE
||
247 newval
== ZFS_CACHE_METADATA
);
249 os
->os_secondary_cache
= newval
;
253 sync_changed_cb(void *arg
, uint64_t newval
)
258 * Inheritance and range checking should have been done by now.
260 ASSERT(newval
== ZFS_SYNC_STANDARD
|| newval
== ZFS_SYNC_ALWAYS
||
261 newval
== ZFS_SYNC_DISABLED
);
263 os
->os_sync
= newval
;
265 zil_set_sync(os
->os_zil
, newval
);
269 redundant_metadata_changed_cb(void *arg
, uint64_t newval
)
274 * Inheritance and range checking should have been done by now.
276 ASSERT(newval
== ZFS_REDUNDANT_METADATA_ALL
||
277 newval
== ZFS_REDUNDANT_METADATA_MOST
);
279 os
->os_redundant_metadata
= newval
;
283 dnodesize_changed_cb(void *arg
, uint64_t newval
)
288 case ZFS_DNSIZE_LEGACY
:
289 os
->os_dnodesize
= DNODE_MIN_SIZE
;
291 case ZFS_DNSIZE_AUTO
:
293 * Choose a dnode size that will work well for most
294 * workloads if the user specified "auto". Future code
295 * improvements could dynamically select a dnode size
296 * based on observed workload patterns.
298 os
->os_dnodesize
= DNODE_MIN_SIZE
* 2;
305 os
->os_dnodesize
= newval
;
311 logbias_changed_cb(void *arg
, uint64_t newval
)
315 ASSERT(newval
== ZFS_LOGBIAS_LATENCY
||
316 newval
== ZFS_LOGBIAS_THROUGHPUT
);
317 os
->os_logbias
= newval
;
319 zil_set_logbias(os
->os_zil
, newval
);
323 recordsize_changed_cb(void *arg
, uint64_t newval
)
327 os
->os_recordsize
= newval
;
331 dmu_objset_byteswap(void *buf
, size_t size
)
333 objset_phys_t
*osp
= buf
;
335 ASSERT(size
== OBJSET_OLD_PHYS_SIZE
|| size
== sizeof (objset_phys_t
));
336 dnode_byteswap(&osp
->os_meta_dnode
);
337 byteswap_uint64_array(&osp
->os_zil_header
, sizeof (zil_header_t
));
338 osp
->os_type
= BSWAP_64(osp
->os_type
);
339 osp
->os_flags
= BSWAP_64(osp
->os_flags
);
340 if (size
== sizeof (objset_phys_t
)) {
341 dnode_byteswap(&osp
->os_userused_dnode
);
342 dnode_byteswap(&osp
->os_groupused_dnode
);
347 * The hash is a CRC-based hash of the objset_t pointer and the object number.
350 dnode_hash(const objset_t
*os
, uint64_t obj
)
352 uintptr_t osv
= (uintptr_t)os
;
353 uint64_t crc
= -1ULL;
355 ASSERT(zfs_crc64_table
[128] == ZFS_CRC64_POLY
);
357 * The low 6 bits of the pointer don't have much entropy, because
358 * the objset_t is larger than 2^6 bytes long.
360 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (osv
>> 6)) & 0xFF];
361 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 0)) & 0xFF];
362 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 8)) & 0xFF];
363 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 16)) & 0xFF];
365 crc
^= (osv
>>14) ^ (obj
>>24);
371 dnode_multilist_index_func(multilist_t
*ml
, void *obj
)
374 return (dnode_hash(dn
->dn_objset
, dn
->dn_object
) %
375 multilist_get_num_sublists(ml
));
379 dmu_objset_open_impl(spa_t
*spa
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
385 ASSERT(ds
== NULL
|| MUTEX_HELD(&ds
->ds_opening_lock
));
387 os
= kmem_zalloc(sizeof (objset_t
), KM_SLEEP
);
388 os
->os_dsl_dataset
= ds
;
391 if (!BP_IS_HOLE(os
->os_rootbp
)) {
392 arc_flags_t aflags
= ARC_FLAG_WAIT
;
394 SET_BOOKMARK(&zb
, ds
? ds
->ds_object
: DMU_META_OBJSET
,
395 ZB_ROOT_OBJECT
, ZB_ROOT_LEVEL
, ZB_ROOT_BLKID
);
397 if (DMU_OS_IS_L2CACHEABLE(os
))
398 aflags
|= ARC_FLAG_L2CACHE
;
400 dprintf_bp(os
->os_rootbp
, "reading %s", "");
401 err
= arc_read(NULL
, spa
, os
->os_rootbp
,
402 arc_getbuf_func
, &os
->os_phys_buf
,
403 ZIO_PRIORITY_SYNC_READ
, ZIO_FLAG_CANFAIL
, &aflags
, &zb
);
405 kmem_free(os
, sizeof (objset_t
));
406 /* convert checksum errors into IO errors */
408 err
= SET_ERROR(EIO
);
412 /* Increase the blocksize if we are permitted. */
413 if (spa_version(spa
) >= SPA_VERSION_USERSPACE
&&
414 arc_buf_size(os
->os_phys_buf
) < sizeof (objset_phys_t
)) {
415 arc_buf_t
*buf
= arc_alloc_buf(spa
, &os
->os_phys_buf
,
416 ARC_BUFC_METADATA
, sizeof (objset_phys_t
));
417 bzero(buf
->b_data
, sizeof (objset_phys_t
));
418 bcopy(os
->os_phys_buf
->b_data
, buf
->b_data
,
419 arc_buf_size(os
->os_phys_buf
));
420 arc_buf_destroy(os
->os_phys_buf
, &os
->os_phys_buf
);
421 os
->os_phys_buf
= buf
;
424 os
->os_phys
= os
->os_phys_buf
->b_data
;
425 os
->os_flags
= os
->os_phys
->os_flags
;
427 int size
= spa_version(spa
) >= SPA_VERSION_USERSPACE
?
428 sizeof (objset_phys_t
) : OBJSET_OLD_PHYS_SIZE
;
429 os
->os_phys_buf
= arc_alloc_buf(spa
, &os
->os_phys_buf
,
430 ARC_BUFC_METADATA
, size
);
431 os
->os_phys
= os
->os_phys_buf
->b_data
;
432 bzero(os
->os_phys
, size
);
436 * Note: the changed_cb will be called once before the register
437 * func returns, thus changing the checksum/compression from the
438 * default (fletcher2/off). Snapshots don't need to know about
439 * checksum/compression/copies.
442 boolean_t needlock
= B_FALSE
;
445 * Note: it's valid to open the objset if the dataset is
446 * long-held, in which case the pool_config lock will not
449 if (!dsl_pool_config_held(dmu_objset_pool(os
))) {
451 dsl_pool_config_enter(dmu_objset_pool(os
), FTAG
);
453 err
= dsl_prop_register(ds
,
454 zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE
),
455 primary_cache_changed_cb
, os
);
457 err
= dsl_prop_register(ds
,
458 zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE
),
459 secondary_cache_changed_cb
, os
);
461 if (!ds
->ds_is_snapshot
) {
463 err
= dsl_prop_register(ds
,
464 zfs_prop_to_name(ZFS_PROP_CHECKSUM
),
465 checksum_changed_cb
, os
);
468 err
= dsl_prop_register(ds
,
469 zfs_prop_to_name(ZFS_PROP_COMPRESSION
),
470 compression_changed_cb
, os
);
473 err
= dsl_prop_register(ds
,
474 zfs_prop_to_name(ZFS_PROP_COPIES
),
475 copies_changed_cb
, os
);
478 err
= dsl_prop_register(ds
,
479 zfs_prop_to_name(ZFS_PROP_DEDUP
),
480 dedup_changed_cb
, os
);
483 err
= dsl_prop_register(ds
,
484 zfs_prop_to_name(ZFS_PROP_LOGBIAS
),
485 logbias_changed_cb
, os
);
488 err
= dsl_prop_register(ds
,
489 zfs_prop_to_name(ZFS_PROP_SYNC
),
490 sync_changed_cb
, os
);
493 err
= dsl_prop_register(ds
,
495 ZFS_PROP_REDUNDANT_METADATA
),
496 redundant_metadata_changed_cb
, os
);
499 err
= dsl_prop_register(ds
,
500 zfs_prop_to_name(ZFS_PROP_RECORDSIZE
),
501 recordsize_changed_cb
, os
);
504 err
= dsl_prop_register(ds
,
505 zfs_prop_to_name(ZFS_PROP_DNODESIZE
),
506 dnodesize_changed_cb
, os
);
510 dsl_pool_config_exit(dmu_objset_pool(os
), FTAG
);
512 arc_buf_destroy(os
->os_phys_buf
, &os
->os_phys_buf
);
513 kmem_free(os
, sizeof (objset_t
));
517 /* It's the meta-objset. */
518 os
->os_checksum
= ZIO_CHECKSUM_FLETCHER_4
;
519 os
->os_compress
= ZIO_COMPRESS_ON
;
520 os
->os_copies
= spa_max_replication(spa
);
521 os
->os_dedup_checksum
= ZIO_CHECKSUM_OFF
;
522 os
->os_dedup_verify
= B_FALSE
;
523 os
->os_logbias
= ZFS_LOGBIAS_LATENCY
;
524 os
->os_sync
= ZFS_SYNC_STANDARD
;
525 os
->os_primary_cache
= ZFS_CACHE_ALL
;
526 os
->os_secondary_cache
= ZFS_CACHE_ALL
;
527 os
->os_dnodesize
= DNODE_MIN_SIZE
;
530 if (ds
== NULL
|| !ds
->ds_is_snapshot
)
531 os
->os_zil_header
= os
->os_phys
->os_zil_header
;
532 os
->os_zil
= zil_alloc(os
, &os
->os_zil_header
);
534 for (i
= 0; i
< TXG_SIZE
; i
++) {
535 os
->os_dirty_dnodes
[i
] = multilist_create(sizeof (dnode_t
),
536 offsetof(dnode_t
, dn_dirty_link
[i
]),
537 dnode_multilist_index_func
);
539 list_create(&os
->os_dnodes
, sizeof (dnode_t
),
540 offsetof(dnode_t
, dn_link
));
541 list_create(&os
->os_downgraded_dbufs
, sizeof (dmu_buf_impl_t
),
542 offsetof(dmu_buf_impl_t
, db_link
));
544 list_link_init(&os
->os_evicting_node
);
546 mutex_init(&os
->os_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
547 mutex_init(&os
->os_userused_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
548 mutex_init(&os
->os_obj_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
549 mutex_init(&os
->os_user_ptr_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
550 os
->os_obj_next_percpu_len
= boot_ncpus
;
551 os
->os_obj_next_percpu
= kmem_zalloc(os
->os_obj_next_percpu_len
*
552 sizeof (os
->os_obj_next_percpu
[0]), KM_SLEEP
);
554 dnode_special_open(os
, &os
->os_phys
->os_meta_dnode
,
555 DMU_META_DNODE_OBJECT
, &os
->os_meta_dnode
);
556 if (arc_buf_size(os
->os_phys_buf
) >= sizeof (objset_phys_t
)) {
557 dnode_special_open(os
, &os
->os_phys
->os_userused_dnode
,
558 DMU_USERUSED_OBJECT
, &os
->os_userused_dnode
);
559 dnode_special_open(os
, &os
->os_phys
->os_groupused_dnode
,
560 DMU_GROUPUSED_OBJECT
, &os
->os_groupused_dnode
);
563 mutex_init(&os
->os_upgrade_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
570 dmu_objset_from_ds(dsl_dataset_t
*ds
, objset_t
**osp
)
575 * We shouldn't be doing anything with dsl_dataset_t's unless the
576 * pool_config lock is held, or the dataset is long-held.
578 ASSERT(dsl_pool_config_held(ds
->ds_dir
->dd_pool
) ||
579 dsl_dataset_long_held(ds
));
581 mutex_enter(&ds
->ds_opening_lock
);
582 if (ds
->ds_objset
== NULL
) {
584 rrw_enter(&ds
->ds_bp_rwlock
, RW_READER
, FTAG
);
585 err
= dmu_objset_open_impl(dsl_dataset_get_spa(ds
),
586 ds
, dsl_dataset_get_blkptr(ds
), &os
);
587 rrw_exit(&ds
->ds_bp_rwlock
, FTAG
);
590 mutex_enter(&ds
->ds_lock
);
591 ASSERT(ds
->ds_objset
== NULL
);
593 mutex_exit(&ds
->ds_lock
);
596 *osp
= ds
->ds_objset
;
597 mutex_exit(&ds
->ds_opening_lock
);
602 * Holds the pool while the objset is held. Therefore only one objset
603 * can be held at a time.
606 dmu_objset_hold(const char *name
, void *tag
, objset_t
**osp
)
612 err
= dsl_pool_hold(name
, tag
, &dp
);
615 err
= dsl_dataset_hold(dp
, name
, tag
, &ds
);
617 dsl_pool_rele(dp
, tag
);
621 err
= dmu_objset_from_ds(ds
, osp
);
623 dsl_dataset_rele(ds
, tag
);
624 dsl_pool_rele(dp
, tag
);
631 dmu_objset_own_impl(dsl_dataset_t
*ds
, dmu_objset_type_t type
,
632 boolean_t readonly
, void *tag
, objset_t
**osp
)
636 err
= dmu_objset_from_ds(ds
, osp
);
638 dsl_dataset_disown(ds
, tag
);
639 } else if (type
!= DMU_OST_ANY
&& type
!= (*osp
)->os_phys
->os_type
) {
640 dsl_dataset_disown(ds
, tag
);
641 return (SET_ERROR(EINVAL
));
642 } else if (!readonly
&& dsl_dataset_is_snapshot(ds
)) {
643 dsl_dataset_disown(ds
, tag
);
644 return (SET_ERROR(EROFS
));
650 * dsl_pool must not be held when this is called.
651 * Upon successful return, there will be a longhold on the dataset,
652 * and the dsl_pool will not be held.
655 dmu_objset_own(const char *name
, dmu_objset_type_t type
,
656 boolean_t readonly
, void *tag
, objset_t
**osp
)
662 err
= dsl_pool_hold(name
, FTAG
, &dp
);
665 err
= dsl_dataset_own(dp
, name
, tag
, &ds
);
667 dsl_pool_rele(dp
, FTAG
);
670 err
= dmu_objset_own_impl(ds
, type
, readonly
, tag
, osp
);
671 dsl_pool_rele(dp
, FTAG
);
673 if (err
== 0 && dmu_objset_userobjspace_upgradable(*osp
))
674 dmu_objset_userobjspace_upgrade(*osp
);
680 dmu_objset_own_obj(dsl_pool_t
*dp
, uint64_t obj
, dmu_objset_type_t type
,
681 boolean_t readonly
, void *tag
, objset_t
**osp
)
686 err
= dsl_dataset_own_obj(dp
, obj
, tag
, &ds
);
690 return (dmu_objset_own_impl(ds
, type
, readonly
, tag
, osp
));
694 dmu_objset_rele(objset_t
*os
, void *tag
)
696 dsl_pool_t
*dp
= dmu_objset_pool(os
);
697 dsl_dataset_rele(os
->os_dsl_dataset
, tag
);
698 dsl_pool_rele(dp
, tag
);
702 * When we are called, os MUST refer to an objset associated with a dataset
703 * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner
704 * == tag. We will then release and reacquire ownership of the dataset while
705 * holding the pool config_rwlock to avoid intervening namespace or ownership
708 * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to
709 * release the hold on its dataset and acquire a new one on the dataset of the
710 * same name so that it can be partially torn down and reconstructed.
713 dmu_objset_refresh_ownership(objset_t
*os
, void *tag
)
716 dsl_dataset_t
*ds
, *newds
;
717 char name
[ZFS_MAX_DATASET_NAME_LEN
];
719 ds
= os
->os_dsl_dataset
;
720 VERIFY3P(ds
, !=, NULL
);
721 VERIFY3P(ds
->ds_owner
, ==, tag
);
722 VERIFY(dsl_dataset_long_held(ds
));
724 dsl_dataset_name(ds
, name
);
725 dp
= dmu_objset_pool(os
);
726 dsl_pool_config_enter(dp
, FTAG
);
727 dmu_objset_disown(os
, tag
);
728 VERIFY0(dsl_dataset_own(dp
, name
, tag
, &newds
));
729 VERIFY3P(newds
, ==, os
->os_dsl_dataset
);
730 dsl_pool_config_exit(dp
, FTAG
);
734 dmu_objset_disown(objset_t
*os
, void *tag
)
737 * Stop upgrading thread
739 dmu_objset_upgrade_stop(os
);
740 dsl_dataset_disown(os
->os_dsl_dataset
, tag
);
744 dmu_objset_evict_dbufs(objset_t
*os
)
749 dn_marker
= kmem_alloc(sizeof (dnode_t
), KM_SLEEP
);
751 mutex_enter(&os
->os_lock
);
752 dn
= list_head(&os
->os_dnodes
);
755 * Skip dnodes without holds. We have to do this dance
756 * because dnode_add_ref() only works if there is already a
757 * hold. If the dnode has no holds, then it has no dbufs.
759 if (dnode_add_ref(dn
, FTAG
)) {
760 list_insert_after(&os
->os_dnodes
, dn
, dn_marker
);
761 mutex_exit(&os
->os_lock
);
763 dnode_evict_dbufs(dn
);
764 dnode_rele(dn
, FTAG
);
766 mutex_enter(&os
->os_lock
);
767 dn
= list_next(&os
->os_dnodes
, dn_marker
);
768 list_remove(&os
->os_dnodes
, dn_marker
);
770 dn
= list_next(&os
->os_dnodes
, dn
);
773 mutex_exit(&os
->os_lock
);
775 kmem_free(dn_marker
, sizeof (dnode_t
));
777 if (DMU_USERUSED_DNODE(os
) != NULL
) {
778 dnode_evict_dbufs(DMU_GROUPUSED_DNODE(os
));
779 dnode_evict_dbufs(DMU_USERUSED_DNODE(os
));
781 dnode_evict_dbufs(DMU_META_DNODE(os
));
785 * Objset eviction processing is split into into two pieces.
786 * The first marks the objset as evicting, evicts any dbufs that
787 * have a refcount of zero, and then queues up the objset for the
788 * second phase of eviction. Once os->os_dnodes has been cleared by
789 * dnode_buf_pageout()->dnode_destroy(), the second phase is executed.
790 * The second phase closes the special dnodes, dequeues the objset from
791 * the list of those undergoing eviction, and finally frees the objset.
793 * NOTE: Due to asynchronous eviction processing (invocation of
794 * dnode_buf_pageout()), it is possible for the meta dnode for the
795 * objset to have no holds even though os->os_dnodes is not empty.
798 dmu_objset_evict(objset_t
*os
)
802 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
804 for (t
= 0; t
< TXG_SIZE
; t
++)
805 ASSERT(!dmu_objset_is_dirty(os
, t
));
808 dsl_prop_unregister_all(ds
, os
);
813 dmu_objset_evict_dbufs(os
);
815 mutex_enter(&os
->os_lock
);
816 spa_evicting_os_register(os
->os_spa
, os
);
817 if (list_is_empty(&os
->os_dnodes
)) {
818 mutex_exit(&os
->os_lock
);
819 dmu_objset_evict_done(os
);
821 mutex_exit(&os
->os_lock
);
826 dmu_objset_evict_done(objset_t
*os
)
828 ASSERT3P(list_head(&os
->os_dnodes
), ==, NULL
);
830 dnode_special_close(&os
->os_meta_dnode
);
831 if (DMU_USERUSED_DNODE(os
)) {
832 dnode_special_close(&os
->os_userused_dnode
);
833 dnode_special_close(&os
->os_groupused_dnode
);
835 zil_free(os
->os_zil
);
837 arc_buf_destroy(os
->os_phys_buf
, &os
->os_phys_buf
);
840 * This is a barrier to prevent the objset from going away in
841 * dnode_move() until we can safely ensure that the objset is still in
842 * use. We consider the objset valid before the barrier and invalid
845 rw_enter(&os_lock
, RW_READER
);
848 kmem_free(os
->os_obj_next_percpu
,
849 os
->os_obj_next_percpu_len
* sizeof (os
->os_obj_next_percpu
[0]));
851 mutex_destroy(&os
->os_lock
);
852 mutex_destroy(&os
->os_userused_lock
);
853 mutex_destroy(&os
->os_obj_lock
);
854 mutex_destroy(&os
->os_user_ptr_lock
);
855 mutex_destroy(&os
->os_upgrade_lock
);
856 for (int i
= 0; i
< TXG_SIZE
; i
++) {
857 multilist_destroy(os
->os_dirty_dnodes
[i
]);
859 spa_evicting_os_deregister(os
->os_spa
, os
);
860 kmem_free(os
, sizeof (objset_t
));
864 dmu_objset_snap_cmtime(objset_t
*os
)
866 return (dsl_dir_snap_cmtime(os
->os_dsl_dataset
->ds_dir
));
869 /* called from dsl for meta-objset */
871 dmu_objset_create_impl(spa_t
*spa
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
872 dmu_objset_type_t type
, dmu_tx_t
*tx
)
877 ASSERT(dmu_tx_is_syncing(tx
));
880 VERIFY0(dmu_objset_from_ds(ds
, &os
));
882 VERIFY0(dmu_objset_open_impl(spa
, NULL
, bp
, &os
));
884 mdn
= DMU_META_DNODE(os
);
886 dnode_allocate(mdn
, DMU_OT_DNODE
, DNODE_BLOCK_SIZE
, DN_MAX_INDBLKSHIFT
,
887 DMU_OT_NONE
, 0, DNODE_MIN_SLOTS
, tx
);
890 * We don't want to have to increase the meta-dnode's nlevels
891 * later, because then we could do it in quescing context while
892 * we are also accessing it in open context.
894 * This precaution is not necessary for the MOS (ds == NULL),
895 * because the MOS is only updated in syncing context.
896 * This is most fortunate: the MOS is the only objset that
897 * needs to be synced multiple times as spa_sync() iterates
898 * to convergence, so minimizing its dn_nlevels matters.
904 * Determine the number of levels necessary for the meta-dnode
905 * to contain DN_MAX_OBJECT dnodes. Note that in order to
906 * ensure that we do not overflow 64 bits, there has to be
907 * a nlevels that gives us a number of blocks > DN_MAX_OBJECT
908 * but < 2^64. Therefore,
909 * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT) (10) must be
910 * less than (64 - log2(DN_MAX_OBJECT)) (16).
912 while ((uint64_t)mdn
->dn_nblkptr
<<
913 (mdn
->dn_datablkshift
- DNODE_SHIFT
+
914 (levels
- 1) * (mdn
->dn_indblkshift
- SPA_BLKPTRSHIFT
)) <
918 mdn
->dn_next_nlevels
[tx
->tx_txg
& TXG_MASK
] =
919 mdn
->dn_nlevels
= levels
;
922 ASSERT(type
!= DMU_OST_NONE
);
923 ASSERT(type
!= DMU_OST_ANY
);
924 ASSERT(type
< DMU_OST_NUMTYPES
);
925 os
->os_phys
->os_type
= type
;
926 if (dmu_objset_userused_enabled(os
)) {
927 os
->os_phys
->os_flags
|= OBJSET_FLAG_USERACCOUNTING_COMPLETE
;
928 if (dmu_objset_userobjused_enabled(os
)) {
929 ds
->ds_feature_activation_needed
[
930 SPA_FEATURE_USEROBJ_ACCOUNTING
] = B_TRUE
;
931 os
->os_phys
->os_flags
|=
932 OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE
;
934 os
->os_flags
= os
->os_phys
->os_flags
;
937 dsl_dataset_dirty(ds
, tx
);
942 typedef struct dmu_objset_create_arg
{
943 const char *doca_name
;
945 void (*doca_userfunc
)(objset_t
*os
, void *arg
,
946 cred_t
*cr
, dmu_tx_t
*tx
);
948 dmu_objset_type_t doca_type
;
950 } dmu_objset_create_arg_t
;
954 dmu_objset_create_check(void *arg
, dmu_tx_t
*tx
)
956 dmu_objset_create_arg_t
*doca
= arg
;
957 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
962 if (strchr(doca
->doca_name
, '@') != NULL
)
963 return (SET_ERROR(EINVAL
));
965 if (strlen(doca
->doca_name
) >= ZFS_MAX_DATASET_NAME_LEN
)
966 return (SET_ERROR(ENAMETOOLONG
));
968 error
= dsl_dir_hold(dp
, doca
->doca_name
, FTAG
, &pdd
, &tail
);
972 dsl_dir_rele(pdd
, FTAG
);
973 return (SET_ERROR(EEXIST
));
975 error
= dsl_fs_ss_limit_check(pdd
, 1, ZFS_PROP_FILESYSTEM_LIMIT
, NULL
,
977 dsl_dir_rele(pdd
, FTAG
);
983 dmu_objset_create_sync(void *arg
, dmu_tx_t
*tx
)
985 dmu_objset_create_arg_t
*doca
= arg
;
986 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
994 VERIFY0(dsl_dir_hold(dp
, doca
->doca_name
, FTAG
, &pdd
, &tail
));
996 obj
= dsl_dataset_create_sync(pdd
, tail
, NULL
, doca
->doca_flags
,
997 doca
->doca_cred
, tx
);
999 VERIFY0(dsl_dataset_hold_obj(pdd
->dd_pool
, obj
, FTAG
, &ds
));
1000 rrw_enter(&ds
->ds_bp_rwlock
, RW_READER
, FTAG
);
1001 bp
= dsl_dataset_get_blkptr(ds
);
1002 os
= dmu_objset_create_impl(pdd
->dd_pool
->dp_spa
,
1003 ds
, bp
, doca
->doca_type
, tx
);
1004 rrw_exit(&ds
->ds_bp_rwlock
, FTAG
);
1006 if (doca
->doca_userfunc
!= NULL
) {
1007 doca
->doca_userfunc(os
, doca
->doca_userarg
,
1008 doca
->doca_cred
, tx
);
1011 spa_history_log_internal_ds(ds
, "create", tx
, "");
1012 zvol_create_minors(dp
->dp_spa
, doca
->doca_name
, B_TRUE
);
1014 dsl_dataset_rele(ds
, FTAG
);
1015 dsl_dir_rele(pdd
, FTAG
);
1019 dmu_objset_create(const char *name
, dmu_objset_type_t type
, uint64_t flags
,
1020 void (*func
)(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
), void *arg
)
1022 dmu_objset_create_arg_t doca
;
1024 doca
.doca_name
= name
;
1025 doca
.doca_cred
= CRED();
1026 doca
.doca_flags
= flags
;
1027 doca
.doca_userfunc
= func
;
1028 doca
.doca_userarg
= arg
;
1029 doca
.doca_type
= type
;
1031 return (dsl_sync_task(name
,
1032 dmu_objset_create_check
, dmu_objset_create_sync
, &doca
,
1033 5, ZFS_SPACE_CHECK_NORMAL
));
1036 typedef struct dmu_objset_clone_arg
{
1037 const char *doca_clone
;
1038 const char *doca_origin
;
1040 } dmu_objset_clone_arg_t
;
1044 dmu_objset_clone_check(void *arg
, dmu_tx_t
*tx
)
1046 dmu_objset_clone_arg_t
*doca
= arg
;
1050 dsl_dataset_t
*origin
;
1051 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1053 if (strchr(doca
->doca_clone
, '@') != NULL
)
1054 return (SET_ERROR(EINVAL
));
1056 if (strlen(doca
->doca_clone
) >= ZFS_MAX_DATASET_NAME_LEN
)
1057 return (SET_ERROR(ENAMETOOLONG
));
1059 error
= dsl_dir_hold(dp
, doca
->doca_clone
, FTAG
, &pdd
, &tail
);
1063 dsl_dir_rele(pdd
, FTAG
);
1064 return (SET_ERROR(EEXIST
));
1067 error
= dsl_fs_ss_limit_check(pdd
, 1, ZFS_PROP_FILESYSTEM_LIMIT
, NULL
,
1070 dsl_dir_rele(pdd
, FTAG
);
1071 return (SET_ERROR(EDQUOT
));
1073 dsl_dir_rele(pdd
, FTAG
);
1075 error
= dsl_dataset_hold(dp
, doca
->doca_origin
, FTAG
, &origin
);
1079 /* You can only clone snapshots, not the head datasets. */
1080 if (!origin
->ds_is_snapshot
) {
1081 dsl_dataset_rele(origin
, FTAG
);
1082 return (SET_ERROR(EINVAL
));
1084 dsl_dataset_rele(origin
, FTAG
);
1090 dmu_objset_clone_sync(void *arg
, dmu_tx_t
*tx
)
1092 dmu_objset_clone_arg_t
*doca
= arg
;
1093 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1096 dsl_dataset_t
*origin
, *ds
;
1098 char namebuf
[ZFS_MAX_DATASET_NAME_LEN
];
1100 VERIFY0(dsl_dir_hold(dp
, doca
->doca_clone
, FTAG
, &pdd
, &tail
));
1101 VERIFY0(dsl_dataset_hold(dp
, doca
->doca_origin
, FTAG
, &origin
));
1103 obj
= dsl_dataset_create_sync(pdd
, tail
, origin
, 0,
1104 doca
->doca_cred
, tx
);
1106 VERIFY0(dsl_dataset_hold_obj(pdd
->dd_pool
, obj
, FTAG
, &ds
));
1107 dsl_dataset_name(origin
, namebuf
);
1108 spa_history_log_internal_ds(ds
, "clone", tx
,
1109 "origin=%s (%llu)", namebuf
, origin
->ds_object
);
1110 zvol_create_minors(dp
->dp_spa
, doca
->doca_clone
, B_TRUE
);
1111 dsl_dataset_rele(ds
, FTAG
);
1112 dsl_dataset_rele(origin
, FTAG
);
1113 dsl_dir_rele(pdd
, FTAG
);
1117 dmu_objset_clone(const char *clone
, const char *origin
)
1119 dmu_objset_clone_arg_t doca
;
1121 doca
.doca_clone
= clone
;
1122 doca
.doca_origin
= origin
;
1123 doca
.doca_cred
= CRED();
1125 return (dsl_sync_task(clone
,
1126 dmu_objset_clone_check
, dmu_objset_clone_sync
, &doca
,
1127 5, ZFS_SPACE_CHECK_NORMAL
));
1131 dmu_objset_snapshot_one(const char *fsname
, const char *snapname
)
1134 char *longsnap
= kmem_asprintf("%s@%s", fsname
, snapname
);
1135 nvlist_t
*snaps
= fnvlist_alloc();
1137 fnvlist_add_boolean(snaps
, longsnap
);
1139 err
= dsl_dataset_snapshot(snaps
, NULL
, NULL
);
1140 fnvlist_free(snaps
);
1145 dmu_objset_upgrade_task_cb(void *data
)
1147 objset_t
*os
= data
;
1149 mutex_enter(&os
->os_upgrade_lock
);
1150 os
->os_upgrade_status
= EINTR
;
1151 if (!os
->os_upgrade_exit
) {
1152 mutex_exit(&os
->os_upgrade_lock
);
1154 os
->os_upgrade_status
= os
->os_upgrade_cb(os
);
1155 mutex_enter(&os
->os_upgrade_lock
);
1157 os
->os_upgrade_exit
= B_TRUE
;
1158 os
->os_upgrade_id
= 0;
1159 mutex_exit(&os
->os_upgrade_lock
);
1163 dmu_objset_upgrade(objset_t
*os
, dmu_objset_upgrade_cb_t cb
)
1165 if (os
->os_upgrade_id
!= 0)
1168 mutex_enter(&os
->os_upgrade_lock
);
1169 if (os
->os_upgrade_id
== 0 && os
->os_upgrade_status
== 0) {
1170 os
->os_upgrade_exit
= B_FALSE
;
1171 os
->os_upgrade_cb
= cb
;
1172 os
->os_upgrade_id
= taskq_dispatch(
1173 os
->os_spa
->spa_upgrade_taskq
,
1174 dmu_objset_upgrade_task_cb
, os
, TQ_SLEEP
);
1175 if (os
->os_upgrade_id
== TASKQID_INVALID
)
1176 os
->os_upgrade_status
= ENOMEM
;
1178 mutex_exit(&os
->os_upgrade_lock
);
1182 dmu_objset_upgrade_stop(objset_t
*os
)
1184 mutex_enter(&os
->os_upgrade_lock
);
1185 os
->os_upgrade_exit
= B_TRUE
;
1186 if (os
->os_upgrade_id
!= 0) {
1187 taskqid_t id
= os
->os_upgrade_id
;
1189 os
->os_upgrade_id
= 0;
1190 mutex_exit(&os
->os_upgrade_lock
);
1192 taskq_cancel_id(os
->os_spa
->spa_upgrade_taskq
, id
);
1194 mutex_exit(&os
->os_upgrade_lock
);
1199 dmu_objset_sync_dnodes(multilist_sublist_t
*list
, dmu_tx_t
*tx
)
1203 while ((dn
= multilist_sublist_head(list
)) != NULL
) {
1204 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
);
1205 ASSERT(dn
->dn_dbuf
->db_data_pending
);
1207 * Initialize dn_zio outside dnode_sync() because the
1208 * meta-dnode needs to set it ouside dnode_sync().
1210 dn
->dn_zio
= dn
->dn_dbuf
->db_data_pending
->dr_zio
;
1213 ASSERT3U(dn
->dn_nlevels
, <=, DN_MAX_LEVELS
);
1214 multilist_sublist_remove(list
, dn
);
1216 multilist_t
*newlist
= dn
->dn_objset
->os_synced_dnodes
;
1217 if (newlist
!= NULL
) {
1218 (void) dnode_add_ref(dn
, newlist
);
1219 multilist_insert(newlist
, dn
);
1228 dmu_objset_write_ready(zio_t
*zio
, arc_buf_t
*abuf
, void *arg
)
1232 blkptr_t
*bp
= zio
->io_bp
;
1234 dnode_phys_t
*dnp
= &os
->os_phys
->os_meta_dnode
;
1236 ASSERT(!BP_IS_EMBEDDED(bp
));
1237 ASSERT3U(BP_GET_TYPE(bp
), ==, DMU_OT_OBJSET
);
1238 ASSERT0(BP_GET_LEVEL(bp
));
1241 * Update rootbp fill count: it should be the number of objects
1242 * allocated in the object set (not counting the "special"
1243 * objects that are stored in the objset_phys_t -- the meta
1244 * dnode and user/group accounting objects).
1247 for (i
= 0; i
< dnp
->dn_nblkptr
; i
++)
1248 bp
->blk_fill
+= BP_GET_FILL(&dnp
->dn_blkptr
[i
]);
1249 if (os
->os_dsl_dataset
!= NULL
)
1250 rrw_enter(&os
->os_dsl_dataset
->ds_bp_rwlock
, RW_WRITER
, FTAG
);
1251 *os
->os_rootbp
= *bp
;
1252 if (os
->os_dsl_dataset
!= NULL
)
1253 rrw_exit(&os
->os_dsl_dataset
->ds_bp_rwlock
, FTAG
);
1258 dmu_objset_write_done(zio_t
*zio
, arc_buf_t
*abuf
, void *arg
)
1260 blkptr_t
*bp
= zio
->io_bp
;
1261 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
1264 if (zio
->io_flags
& ZIO_FLAG_IO_REWRITE
) {
1265 ASSERT(BP_EQUAL(bp
, bp_orig
));
1267 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
1268 dmu_tx_t
*tx
= os
->os_synctx
;
1270 (void) dsl_dataset_block_kill(ds
, bp_orig
, tx
, B_TRUE
);
1271 dsl_dataset_block_born(ds
, bp
, tx
);
1273 kmem_free(bp
, sizeof (*bp
));
1276 typedef struct sync_dnodes_arg
{
1277 multilist_t
*sda_list
;
1278 int sda_sublist_idx
;
1279 multilist_t
*sda_newlist
;
1281 } sync_dnodes_arg_t
;
1284 sync_dnodes_task(void *arg
)
1286 sync_dnodes_arg_t
*sda
= arg
;
1288 multilist_sublist_t
*ms
=
1289 multilist_sublist_lock(sda
->sda_list
, sda
->sda_sublist_idx
);
1291 dmu_objset_sync_dnodes(ms
, sda
->sda_tx
);
1293 multilist_sublist_unlock(ms
);
1295 kmem_free(sda
, sizeof (*sda
));
1299 /* called from dsl */
1301 dmu_objset_sync(objset_t
*os
, zio_t
*pio
, dmu_tx_t
*tx
)
1304 zbookmark_phys_t zb
;
1308 dbuf_dirty_record_t
*dr
;
1309 blkptr_t
*blkptr_copy
= kmem_alloc(sizeof (*os
->os_rootbp
), KM_SLEEP
);
1310 *blkptr_copy
= *os
->os_rootbp
;
1312 dprintf_ds(os
->os_dsl_dataset
, "txg=%llu\n", tx
->tx_txg
);
1314 ASSERT(dmu_tx_is_syncing(tx
));
1315 /* XXX the write_done callback should really give us the tx... */
1318 if (os
->os_dsl_dataset
== NULL
) {
1320 * This is the MOS. If we have upgraded,
1321 * spa_max_replication() could change, so reset
1324 os
->os_copies
= spa_max_replication(os
->os_spa
);
1328 * Create the root block IO
1330 SET_BOOKMARK(&zb
, os
->os_dsl_dataset
?
1331 os
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
1332 ZB_ROOT_OBJECT
, ZB_ROOT_LEVEL
, ZB_ROOT_BLKID
);
1333 arc_release(os
->os_phys_buf
, &os
->os_phys_buf
);
1335 dmu_write_policy(os
, NULL
, 0, 0, &zp
);
1337 zio
= arc_write(pio
, os
->os_spa
, tx
->tx_txg
,
1338 blkptr_copy
, os
->os_phys_buf
, DMU_OS_IS_L2CACHEABLE(os
),
1339 &zp
, dmu_objset_write_ready
, NULL
, NULL
, dmu_objset_write_done
,
1340 os
, ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);
1343 * Sync special dnodes - the parent IO for the sync is the root block
1345 DMU_META_DNODE(os
)->dn_zio
= zio
;
1346 dnode_sync(DMU_META_DNODE(os
), tx
);
1348 os
->os_phys
->os_flags
= os
->os_flags
;
1350 if (DMU_USERUSED_DNODE(os
) &&
1351 DMU_USERUSED_DNODE(os
)->dn_type
!= DMU_OT_NONE
) {
1352 DMU_USERUSED_DNODE(os
)->dn_zio
= zio
;
1353 dnode_sync(DMU_USERUSED_DNODE(os
), tx
);
1354 DMU_GROUPUSED_DNODE(os
)->dn_zio
= zio
;
1355 dnode_sync(DMU_GROUPUSED_DNODE(os
), tx
);
1358 txgoff
= tx
->tx_txg
& TXG_MASK
;
1360 if (dmu_objset_userused_enabled(os
)) {
1362 * We must create the list here because it uses the
1363 * dn_dirty_link[] of this txg. But it may already
1364 * exist because we call dsl_dataset_sync() twice per txg.
1366 if (os
->os_synced_dnodes
== NULL
) {
1367 os
->os_synced_dnodes
=
1368 multilist_create(sizeof (dnode_t
),
1369 offsetof(dnode_t
, dn_dirty_link
[txgoff
]),
1370 dnode_multilist_index_func
);
1372 ASSERT3U(os
->os_synced_dnodes
->ml_offset
, ==,
1373 offsetof(dnode_t
, dn_dirty_link
[txgoff
]));
1378 i
< multilist_get_num_sublists(os
->os_dirty_dnodes
[txgoff
]); i
++) {
1379 sync_dnodes_arg_t
*sda
= kmem_alloc(sizeof (*sda
), KM_SLEEP
);
1380 sda
->sda_list
= os
->os_dirty_dnodes
[txgoff
];
1381 sda
->sda_sublist_idx
= i
;
1383 (void) taskq_dispatch(dmu_objset_pool(os
)->dp_sync_taskq
,
1384 sync_dnodes_task
, sda
, 0);
1385 /* callback frees sda */
1387 taskq_wait(dmu_objset_pool(os
)->dp_sync_taskq
);
1389 list
= &DMU_META_DNODE(os
)->dn_dirty_records
[txgoff
];
1390 while ((dr
= list_head(list
)) != NULL
) {
1391 ASSERT0(dr
->dr_dbuf
->db_level
);
1392 list_remove(list
, dr
);
1394 zio_nowait(dr
->dr_zio
);
1397 /* Enable dnode backfill if enough objects have been freed. */
1398 if (os
->os_freed_dnodes
>= dmu_rescan_dnode_threshold
) {
1399 os
->os_rescan_dnodes
= B_TRUE
;
1400 os
->os_freed_dnodes
= 0;
1404 * Free intent log blocks up to this tx.
1406 zil_sync(os
->os_zil
, tx
);
1407 os
->os_phys
->os_zil_header
= os
->os_zil_header
;
1412 dmu_objset_is_dirty(objset_t
*os
, uint64_t txg
)
1414 return (!multilist_is_empty(os
->os_dirty_dnodes
[txg
& TXG_MASK
]));
1417 static objset_used_cb_t
*used_cbs
[DMU_OST_NUMTYPES
];
1420 dmu_objset_register_type(dmu_objset_type_t ost
, objset_used_cb_t
*cb
)
1426 dmu_objset_userused_enabled(objset_t
*os
)
1428 return (spa_version(os
->os_spa
) >= SPA_VERSION_USERSPACE
&&
1429 used_cbs
[os
->os_phys
->os_type
] != NULL
&&
1430 DMU_USERUSED_DNODE(os
) != NULL
);
1434 dmu_objset_userobjused_enabled(objset_t
*os
)
1436 return (dmu_objset_userused_enabled(os
) &&
1437 spa_feature_is_enabled(os
->os_spa
, SPA_FEATURE_USEROBJ_ACCOUNTING
));
1440 typedef struct userquota_node
{
1441 /* must be in the first filed, see userquota_update_cache() */
1442 char uqn_id
[20 + DMU_OBJACCT_PREFIX_LEN
];
1444 avl_node_t uqn_node
;
1447 typedef struct userquota_cache
{
1448 avl_tree_t uqc_user_deltas
;
1449 avl_tree_t uqc_group_deltas
;
1450 } userquota_cache_t
;
1453 userquota_compare(const void *l
, const void *r
)
1455 const userquota_node_t
*luqn
= l
;
1456 const userquota_node_t
*ruqn
= r
;
1460 * NB: can only access uqn_id because userquota_update_cache() doesn't
1461 * pass in an entire userquota_node_t.
1463 rv
= strcmp(luqn
->uqn_id
, ruqn
->uqn_id
);
1465 return (AVL_ISIGN(rv
));
1469 do_userquota_cacheflush(objset_t
*os
, userquota_cache_t
*cache
, dmu_tx_t
*tx
)
1472 userquota_node_t
*uqn
;
1474 ASSERT(dmu_tx_is_syncing(tx
));
1477 while ((uqn
= avl_destroy_nodes(&cache
->uqc_user_deltas
,
1478 &cookie
)) != NULL
) {
1480 * os_userused_lock protects against concurrent calls to
1481 * zap_increment_int(). It's needed because zap_increment_int()
1482 * is not thread-safe (i.e. not atomic).
1484 mutex_enter(&os
->os_userused_lock
);
1485 VERIFY0(zap_increment(os
, DMU_USERUSED_OBJECT
,
1486 uqn
->uqn_id
, uqn
->uqn_delta
, tx
));
1487 mutex_exit(&os
->os_userused_lock
);
1488 kmem_free(uqn
, sizeof (*uqn
));
1490 avl_destroy(&cache
->uqc_user_deltas
);
1493 while ((uqn
= avl_destroy_nodes(&cache
->uqc_group_deltas
,
1494 &cookie
)) != NULL
) {
1495 mutex_enter(&os
->os_userused_lock
);
1496 VERIFY0(zap_increment(os
, DMU_GROUPUSED_OBJECT
,
1497 uqn
->uqn_id
, uqn
->uqn_delta
, tx
));
1498 mutex_exit(&os
->os_userused_lock
);
1499 kmem_free(uqn
, sizeof (*uqn
));
1501 avl_destroy(&cache
->uqc_group_deltas
);
1505 userquota_update_cache(avl_tree_t
*avl
, const char *id
, int64_t delta
)
1507 userquota_node_t
*uqn
;
1510 ASSERT(strlen(id
) < sizeof (uqn
->uqn_id
));
1512 * Use id directly for searching because uqn_id is the first field of
1513 * userquota_node_t and fields after uqn_id won't be accessed in
1516 uqn
= avl_find(avl
, (const void *)id
, &idx
);
1518 uqn
= kmem_zalloc(sizeof (*uqn
), KM_SLEEP
);
1519 strlcpy(uqn
->uqn_id
, id
, sizeof (uqn
->uqn_id
));
1520 avl_insert(avl
, uqn
, idx
);
1522 uqn
->uqn_delta
+= delta
;
1526 do_userquota_update(userquota_cache_t
*cache
, uint64_t used
, uint64_t flags
,
1527 uint64_t user
, uint64_t group
, boolean_t subtract
)
1529 if ((flags
& DNODE_FLAG_USERUSED_ACCOUNTED
)) {
1530 int64_t delta
= DNODE_MIN_SIZE
+ used
;
1536 (void) sprintf(name
, "%llx", (longlong_t
)user
);
1537 userquota_update_cache(&cache
->uqc_user_deltas
, name
, delta
);
1539 (void) sprintf(name
, "%llx", (longlong_t
)group
);
1540 userquota_update_cache(&cache
->uqc_group_deltas
, name
, delta
);
1545 do_userobjquota_update(userquota_cache_t
*cache
, uint64_t flags
,
1546 uint64_t user
, uint64_t group
, boolean_t subtract
)
1548 if (flags
& DNODE_FLAG_USEROBJUSED_ACCOUNTED
) {
1549 char name
[20 + DMU_OBJACCT_PREFIX_LEN
];
1550 int delta
= subtract
? -1 : 1;
1552 (void) snprintf(name
, sizeof (name
), DMU_OBJACCT_PREFIX
"%llx",
1554 userquota_update_cache(&cache
->uqc_user_deltas
, name
, delta
);
1556 (void) snprintf(name
, sizeof (name
), DMU_OBJACCT_PREFIX
"%llx",
1558 userquota_update_cache(&cache
->uqc_group_deltas
, name
, delta
);
1562 typedef struct userquota_updates_arg
{
1564 int uua_sublist_idx
;
1566 } userquota_updates_arg_t
;
1569 userquota_updates_task(void *arg
)
1571 userquota_updates_arg_t
*uua
= arg
;
1572 objset_t
*os
= uua
->uua_os
;
1573 dmu_tx_t
*tx
= uua
->uua_tx
;
1575 userquota_cache_t cache
= { { 0 } };
1577 multilist_sublist_t
*list
=
1578 multilist_sublist_lock(os
->os_synced_dnodes
, uua
->uua_sublist_idx
);
1580 ASSERT(multilist_sublist_head(list
) == NULL
||
1581 dmu_objset_userused_enabled(os
));
1582 avl_create(&cache
.uqc_user_deltas
, userquota_compare
,
1583 sizeof (userquota_node_t
), offsetof(userquota_node_t
, uqn_node
));
1584 avl_create(&cache
.uqc_group_deltas
, userquota_compare
,
1585 sizeof (userquota_node_t
), offsetof(userquota_node_t
, uqn_node
));
1587 while ((dn
= multilist_sublist_head(list
)) != NULL
) {
1589 ASSERT(!DMU_OBJECT_IS_SPECIAL(dn
->dn_object
));
1590 ASSERT(dn
->dn_phys
->dn_type
== DMU_OT_NONE
||
1591 dn
->dn_phys
->dn_flags
&
1592 DNODE_FLAG_USERUSED_ACCOUNTED
);
1594 flags
= dn
->dn_id_flags
;
1596 if (flags
& DN_ID_OLD_EXIST
) {
1597 do_userquota_update(&cache
,
1598 dn
->dn_oldused
, dn
->dn_oldflags
,
1599 dn
->dn_olduid
, dn
->dn_oldgid
, B_TRUE
);
1600 do_userobjquota_update(&cache
, dn
->dn_oldflags
,
1601 dn
->dn_olduid
, dn
->dn_oldgid
, B_TRUE
);
1603 if (flags
& DN_ID_NEW_EXIST
) {
1604 do_userquota_update(&cache
,
1605 DN_USED_BYTES(dn
->dn_phys
), dn
->dn_phys
->dn_flags
,
1606 dn
->dn_newuid
, dn
->dn_newgid
, B_FALSE
);
1607 do_userobjquota_update(&cache
, dn
->dn_phys
->dn_flags
,
1608 dn
->dn_newuid
, dn
->dn_newgid
, B_FALSE
);
1611 mutex_enter(&dn
->dn_mtx
);
1613 dn
->dn_oldflags
= 0;
1614 if (dn
->dn_id_flags
& DN_ID_NEW_EXIST
) {
1615 dn
->dn_olduid
= dn
->dn_newuid
;
1616 dn
->dn_oldgid
= dn
->dn_newgid
;
1617 dn
->dn_id_flags
|= DN_ID_OLD_EXIST
;
1618 if (dn
->dn_bonuslen
== 0)
1619 dn
->dn_id_flags
|= DN_ID_CHKED_SPILL
;
1621 dn
->dn_id_flags
|= DN_ID_CHKED_BONUS
;
1623 dn
->dn_id_flags
&= ~(DN_ID_NEW_EXIST
);
1624 mutex_exit(&dn
->dn_mtx
);
1626 multilist_sublist_remove(list
, dn
);
1627 dnode_rele(dn
, os
->os_synced_dnodes
);
1629 do_userquota_cacheflush(os
, &cache
, tx
);
1630 multilist_sublist_unlock(list
);
1631 kmem_free(uua
, sizeof (*uua
));
1635 dmu_objset_do_userquota_updates(objset_t
*os
, dmu_tx_t
*tx
)
1637 if (!dmu_objset_userused_enabled(os
))
1640 /* Allocate the user/groupused objects if necessary. */
1641 if (DMU_USERUSED_DNODE(os
)->dn_type
== DMU_OT_NONE
) {
1642 VERIFY0(zap_create_claim(os
,
1643 DMU_USERUSED_OBJECT
,
1644 DMU_OT_USERGROUP_USED
, DMU_OT_NONE
, 0, tx
));
1645 VERIFY0(zap_create_claim(os
,
1646 DMU_GROUPUSED_OBJECT
,
1647 DMU_OT_USERGROUP_USED
, DMU_OT_NONE
, 0, tx
));
1651 i
< multilist_get_num_sublists(os
->os_synced_dnodes
); i
++) {
1652 userquota_updates_arg_t
*uua
=
1653 kmem_alloc(sizeof (*uua
), KM_SLEEP
);
1655 uua
->uua_sublist_idx
= i
;
1657 /* note: caller does taskq_wait() */
1658 (void) taskq_dispatch(dmu_objset_pool(os
)->dp_sync_taskq
,
1659 userquota_updates_task
, uua
, 0);
1660 /* callback frees uua */
1665 * Returns a pointer to data to find uid/gid from
1667 * If a dirty record for transaction group that is syncing can't
1668 * be found then NULL is returned. In the NULL case it is assumed
1669 * the uid/gid aren't changing.
1672 dmu_objset_userquota_find_data(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1674 dbuf_dirty_record_t
*dr
, **drp
;
1677 if (db
->db_dirtycnt
== 0)
1678 return (db
->db
.db_data
); /* Nothing is changing */
1680 for (drp
= &db
->db_last_dirty
; (dr
= *drp
) != NULL
; drp
= &dr
->dr_next
)
1681 if (dr
->dr_txg
== tx
->tx_txg
)
1689 DB_DNODE_ENTER(dr
->dr_dbuf
);
1690 dn
= DB_DNODE(dr
->dr_dbuf
);
1692 if (dn
->dn_bonuslen
== 0 &&
1693 dr
->dr_dbuf
->db_blkid
== DMU_SPILL_BLKID
)
1694 data
= dr
->dt
.dl
.dr_data
->b_data
;
1696 data
= dr
->dt
.dl
.dr_data
;
1698 DB_DNODE_EXIT(dr
->dr_dbuf
);
1705 dmu_objset_userquota_get_ids(dnode_t
*dn
, boolean_t before
, dmu_tx_t
*tx
)
1707 objset_t
*os
= dn
->dn_objset
;
1709 dmu_buf_impl_t
*db
= NULL
;
1710 uint64_t *user
= NULL
;
1711 uint64_t *group
= NULL
;
1712 int flags
= dn
->dn_id_flags
;
1714 boolean_t have_spill
= B_FALSE
;
1716 if (!dmu_objset_userused_enabled(dn
->dn_objset
))
1719 if (before
&& (flags
& (DN_ID_CHKED_BONUS
|DN_ID_OLD_EXIST
|
1720 DN_ID_CHKED_SPILL
)))
1723 if (before
&& dn
->dn_bonuslen
!= 0)
1724 data
= DN_BONUS(dn
->dn_phys
);
1725 else if (!before
&& dn
->dn_bonuslen
!= 0) {
1728 mutex_enter(&db
->db_mtx
);
1729 data
= dmu_objset_userquota_find_data(db
, tx
);
1731 data
= DN_BONUS(dn
->dn_phys
);
1733 } else if (dn
->dn_bonuslen
== 0 && dn
->dn_bonustype
== DMU_OT_SA
) {
1736 if (RW_WRITE_HELD(&dn
->dn_struct_rwlock
))
1737 rf
|= DB_RF_HAVESTRUCT
;
1738 error
= dmu_spill_hold_by_dnode(dn
,
1739 rf
| DB_RF_MUST_SUCCEED
,
1740 FTAG
, (dmu_buf_t
**)&db
);
1742 mutex_enter(&db
->db_mtx
);
1743 data
= (before
) ? db
->db
.db_data
:
1744 dmu_objset_userquota_find_data(db
, tx
);
1745 have_spill
= B_TRUE
;
1747 mutex_enter(&dn
->dn_mtx
);
1748 dn
->dn_id_flags
|= DN_ID_CHKED_BONUS
;
1749 mutex_exit(&dn
->dn_mtx
);
1755 user
= &dn
->dn_olduid
;
1756 group
= &dn
->dn_oldgid
;
1758 user
= &dn
->dn_newuid
;
1759 group
= &dn
->dn_newgid
;
1763 * Must always call the callback in case the object
1764 * type has changed and that type isn't an object type to track
1766 error
= used_cbs
[os
->os_phys
->os_type
](dn
->dn_bonustype
, data
,
1770 * Preserve existing uid/gid when the callback can't determine
1771 * what the new uid/gid are and the callback returned EEXIST.
1772 * The EEXIST error tells us to just use the existing uid/gid.
1773 * If we don't know what the old values are then just assign
1774 * them to 0, since that is a new file being created.
1776 if (!before
&& data
== NULL
&& error
== EEXIST
) {
1777 if (flags
& DN_ID_OLD_EXIST
) {
1778 dn
->dn_newuid
= dn
->dn_olduid
;
1779 dn
->dn_newgid
= dn
->dn_oldgid
;
1788 mutex_exit(&db
->db_mtx
);
1790 mutex_enter(&dn
->dn_mtx
);
1791 if (error
== 0 && before
)
1792 dn
->dn_id_flags
|= DN_ID_OLD_EXIST
;
1793 if (error
== 0 && !before
)
1794 dn
->dn_id_flags
|= DN_ID_NEW_EXIST
;
1797 dn
->dn_id_flags
|= DN_ID_CHKED_SPILL
;
1799 dn
->dn_id_flags
|= DN_ID_CHKED_BONUS
;
1801 mutex_exit(&dn
->dn_mtx
);
1803 dmu_buf_rele((dmu_buf_t
*)db
, FTAG
);
1807 dmu_objset_userspace_present(objset_t
*os
)
1809 return (os
->os_phys
->os_flags
&
1810 OBJSET_FLAG_USERACCOUNTING_COMPLETE
);
1814 dmu_objset_userobjspace_present(objset_t
*os
)
1816 return (os
->os_phys
->os_flags
&
1817 OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE
);
1821 dmu_objset_space_upgrade(objset_t
*os
)
1827 * We simply need to mark every object dirty, so that it will be
1828 * synced out and now accounted. If this is called
1829 * concurrently, or if we already did some work before crashing,
1830 * that's fine, since we track each object's accounted state
1834 for (obj
= 0; err
== 0; err
= dmu_object_next(os
, &obj
, FALSE
, 0)) {
1839 mutex_enter(&os
->os_upgrade_lock
);
1840 if (os
->os_upgrade_exit
)
1841 err
= SET_ERROR(EINTR
);
1842 mutex_exit(&os
->os_upgrade_lock
);
1846 if (issig(JUSTLOOKING
) && issig(FORREAL
))
1847 return (SET_ERROR(EINTR
));
1849 objerr
= dmu_bonus_hold(os
, obj
, FTAG
, &db
);
1852 tx
= dmu_tx_create(os
);
1853 dmu_tx_hold_bonus(tx
, obj
);
1854 objerr
= dmu_tx_assign(tx
, TXG_WAIT
);
1859 dmu_buf_will_dirty(db
, tx
);
1860 dmu_buf_rele(db
, FTAG
);
1867 dmu_objset_userspace_upgrade(objset_t
*os
)
1871 if (dmu_objset_userspace_present(os
))
1873 if (dmu_objset_is_snapshot(os
))
1874 return (SET_ERROR(EINVAL
));
1875 if (!dmu_objset_userused_enabled(os
))
1876 return (SET_ERROR(ENOTSUP
));
1878 err
= dmu_objset_space_upgrade(os
);
1882 os
->os_flags
|= OBJSET_FLAG_USERACCOUNTING_COMPLETE
;
1883 txg_wait_synced(dmu_objset_pool(os
), 0);
1888 dmu_objset_userobjspace_upgrade_cb(objset_t
*os
)
1892 if (dmu_objset_userobjspace_present(os
))
1894 if (dmu_objset_is_snapshot(os
))
1895 return (SET_ERROR(EINVAL
));
1896 if (!dmu_objset_userobjused_enabled(os
))
1897 return (SET_ERROR(ENOTSUP
));
1899 dmu_objset_ds(os
)->ds_feature_activation_needed
[
1900 SPA_FEATURE_USEROBJ_ACCOUNTING
] = B_TRUE
;
1902 err
= dmu_objset_space_upgrade(os
);
1906 os
->os_flags
|= OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE
;
1907 txg_wait_synced(dmu_objset_pool(os
), 0);
1912 dmu_objset_userobjspace_upgrade(objset_t
*os
)
1914 dmu_objset_upgrade(os
, dmu_objset_userobjspace_upgrade_cb
);
1918 dmu_objset_userobjspace_upgradable(objset_t
*os
)
1920 return (dmu_objset_type(os
) == DMU_OST_ZFS
&&
1921 !dmu_objset_is_snapshot(os
) &&
1922 dmu_objset_userobjused_enabled(os
) &&
1923 !dmu_objset_userobjspace_present(os
));
1927 dmu_objset_space(objset_t
*os
, uint64_t *refdbytesp
, uint64_t *availbytesp
,
1928 uint64_t *usedobjsp
, uint64_t *availobjsp
)
1930 dsl_dataset_space(os
->os_dsl_dataset
, refdbytesp
, availbytesp
,
1931 usedobjsp
, availobjsp
);
1935 dmu_objset_fsid_guid(objset_t
*os
)
1937 return (dsl_dataset_fsid_guid(os
->os_dsl_dataset
));
1941 dmu_objset_fast_stat(objset_t
*os
, dmu_objset_stats_t
*stat
)
1943 stat
->dds_type
= os
->os_phys
->os_type
;
1944 if (os
->os_dsl_dataset
)
1945 dsl_dataset_fast_stat(os
->os_dsl_dataset
, stat
);
1949 dmu_objset_stats(objset_t
*os
, nvlist_t
*nv
)
1951 ASSERT(os
->os_dsl_dataset
||
1952 os
->os_phys
->os_type
== DMU_OST_META
);
1954 if (os
->os_dsl_dataset
!= NULL
)
1955 dsl_dataset_stats(os
->os_dsl_dataset
, nv
);
1957 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_TYPE
,
1958 os
->os_phys
->os_type
);
1959 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_USERACCOUNTING
,
1960 dmu_objset_userspace_present(os
));
1964 dmu_objset_is_snapshot(objset_t
*os
)
1966 if (os
->os_dsl_dataset
!= NULL
)
1967 return (os
->os_dsl_dataset
->ds_is_snapshot
);
1973 dmu_snapshot_realname(objset_t
*os
, char *name
, char *real
, int maxlen
,
1974 boolean_t
*conflict
)
1976 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
1979 if (dsl_dataset_phys(ds
)->ds_snapnames_zapobj
== 0)
1980 return (SET_ERROR(ENOENT
));
1982 return (zap_lookup_norm(ds
->ds_dir
->dd_pool
->dp_meta_objset
,
1983 dsl_dataset_phys(ds
)->ds_snapnames_zapobj
, name
, 8, 1, &ignored
,
1984 MT_NORMALIZE
, real
, maxlen
, conflict
));
1988 dmu_snapshot_list_next(objset_t
*os
, int namelen
, char *name
,
1989 uint64_t *idp
, uint64_t *offp
, boolean_t
*case_conflict
)
1991 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
1992 zap_cursor_t cursor
;
1993 zap_attribute_t attr
;
1995 ASSERT(dsl_pool_config_held(dmu_objset_pool(os
)));
1997 if (dsl_dataset_phys(ds
)->ds_snapnames_zapobj
== 0)
1998 return (SET_ERROR(ENOENT
));
2000 zap_cursor_init_serialized(&cursor
,
2001 ds
->ds_dir
->dd_pool
->dp_meta_objset
,
2002 dsl_dataset_phys(ds
)->ds_snapnames_zapobj
, *offp
);
2004 if (zap_cursor_retrieve(&cursor
, &attr
) != 0) {
2005 zap_cursor_fini(&cursor
);
2006 return (SET_ERROR(ENOENT
));
2009 if (strlen(attr
.za_name
) + 1 > namelen
) {
2010 zap_cursor_fini(&cursor
);
2011 return (SET_ERROR(ENAMETOOLONG
));
2014 (void) strcpy(name
, attr
.za_name
);
2016 *idp
= attr
.za_first_integer
;
2018 *case_conflict
= attr
.za_normalization_conflict
;
2019 zap_cursor_advance(&cursor
);
2020 *offp
= zap_cursor_serialize(&cursor
);
2021 zap_cursor_fini(&cursor
);
2027 dmu_snapshot_lookup(objset_t
*os
, const char *name
, uint64_t *value
)
2029 return (dsl_dataset_snap_lookup(os
->os_dsl_dataset
, name
, value
));
2033 dmu_dir_list_next(objset_t
*os
, int namelen
, char *name
,
2034 uint64_t *idp
, uint64_t *offp
)
2036 dsl_dir_t
*dd
= os
->os_dsl_dataset
->ds_dir
;
2037 zap_cursor_t cursor
;
2038 zap_attribute_t attr
;
2040 /* there is no next dir on a snapshot! */
2041 if (os
->os_dsl_dataset
->ds_object
!=
2042 dsl_dir_phys(dd
)->dd_head_dataset_obj
)
2043 return (SET_ERROR(ENOENT
));
2045 zap_cursor_init_serialized(&cursor
,
2046 dd
->dd_pool
->dp_meta_objset
,
2047 dsl_dir_phys(dd
)->dd_child_dir_zapobj
, *offp
);
2049 if (zap_cursor_retrieve(&cursor
, &attr
) != 0) {
2050 zap_cursor_fini(&cursor
);
2051 return (SET_ERROR(ENOENT
));
2054 if (strlen(attr
.za_name
) + 1 > namelen
) {
2055 zap_cursor_fini(&cursor
);
2056 return (SET_ERROR(ENAMETOOLONG
));
2059 (void) strcpy(name
, attr
.za_name
);
2061 *idp
= attr
.za_first_integer
;
2062 zap_cursor_advance(&cursor
);
2063 *offp
= zap_cursor_serialize(&cursor
);
2064 zap_cursor_fini(&cursor
);
2069 typedef struct dmu_objset_find_ctx
{
2073 char *dc_ddname
; /* last component of ddobj's name */
2074 int (*dc_func
)(dsl_pool_t
*, dsl_dataset_t
*, void *);
2077 kmutex_t
*dc_error_lock
;
2079 } dmu_objset_find_ctx_t
;
2082 dmu_objset_find_dp_impl(dmu_objset_find_ctx_t
*dcp
)
2084 dsl_pool_t
*dp
= dcp
->dc_dp
;
2085 dmu_objset_find_ctx_t
*child_dcp
;
2089 zap_attribute_t
*attr
;
2093 /* don't process if there already was an error */
2094 if (*dcp
->dc_error
!= 0)
2098 * Note: passing the name (dc_ddname) here is optional, but it
2099 * improves performance because we don't need to call
2100 * zap_value_search() to determine the name.
2102 err
= dsl_dir_hold_obj(dp
, dcp
->dc_ddobj
, dcp
->dc_ddname
, FTAG
, &dd
);
2106 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
2107 if (dd
->dd_myname
[0] == '$') {
2108 dsl_dir_rele(dd
, FTAG
);
2112 thisobj
= dsl_dir_phys(dd
)->dd_head_dataset_obj
;
2113 attr
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
2116 * Iterate over all children.
2118 if (dcp
->dc_flags
& DS_FIND_CHILDREN
) {
2119 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
,
2120 dsl_dir_phys(dd
)->dd_child_dir_zapobj
);
2121 zap_cursor_retrieve(&zc
, attr
) == 0;
2122 (void) zap_cursor_advance(&zc
)) {
2123 ASSERT3U(attr
->za_integer_length
, ==,
2125 ASSERT3U(attr
->za_num_integers
, ==, 1);
2128 kmem_alloc(sizeof (*child_dcp
), KM_SLEEP
);
2130 child_dcp
->dc_ddobj
= attr
->za_first_integer
;
2131 child_dcp
->dc_ddname
= spa_strdup(attr
->za_name
);
2132 if (dcp
->dc_tq
!= NULL
)
2133 (void) taskq_dispatch(dcp
->dc_tq
,
2134 dmu_objset_find_dp_cb
, child_dcp
, TQ_SLEEP
);
2136 dmu_objset_find_dp_impl(child_dcp
);
2138 zap_cursor_fini(&zc
);
2142 * Iterate over all snapshots.
2144 if (dcp
->dc_flags
& DS_FIND_SNAPSHOTS
) {
2146 err
= dsl_dataset_hold_obj(dp
, thisobj
, FTAG
, &ds
);
2151 snapobj
= dsl_dataset_phys(ds
)->ds_snapnames_zapobj
;
2152 dsl_dataset_rele(ds
, FTAG
);
2154 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
, snapobj
);
2155 zap_cursor_retrieve(&zc
, attr
) == 0;
2156 (void) zap_cursor_advance(&zc
)) {
2157 ASSERT3U(attr
->za_integer_length
, ==,
2159 ASSERT3U(attr
->za_num_integers
, ==, 1);
2161 err
= dsl_dataset_hold_obj(dp
,
2162 attr
->za_first_integer
, FTAG
, &ds
);
2165 err
= dcp
->dc_func(dp
, ds
, dcp
->dc_arg
);
2166 dsl_dataset_rele(ds
, FTAG
);
2170 zap_cursor_fini(&zc
);
2174 kmem_free(attr
, sizeof (zap_attribute_t
));
2177 dsl_dir_rele(dd
, FTAG
);
2184 err
= dsl_dataset_hold_obj(dp
, thisobj
, FTAG
, &ds
);
2187 * Note: we hold the dir while calling dsl_dataset_hold_obj() so
2188 * that the dir will remain cached, and we won't have to re-instantiate
2189 * it (which could be expensive due to finding its name via
2190 * zap_value_search()).
2192 dsl_dir_rele(dd
, FTAG
);
2195 err
= dcp
->dc_func(dp
, ds
, dcp
->dc_arg
);
2196 dsl_dataset_rele(ds
, FTAG
);
2200 mutex_enter(dcp
->dc_error_lock
);
2201 /* only keep first error */
2202 if (*dcp
->dc_error
== 0)
2203 *dcp
->dc_error
= err
;
2204 mutex_exit(dcp
->dc_error_lock
);
2207 if (dcp
->dc_ddname
!= NULL
)
2208 spa_strfree(dcp
->dc_ddname
);
2209 kmem_free(dcp
, sizeof (*dcp
));
2213 dmu_objset_find_dp_cb(void *arg
)
2215 dmu_objset_find_ctx_t
*dcp
= arg
;
2216 dsl_pool_t
*dp
= dcp
->dc_dp
;
2219 * We need to get a pool_config_lock here, as there are several
2220 * asssert(pool_config_held) down the stack. Getting a lock via
2221 * dsl_pool_config_enter is risky, as it might be stalled by a
2222 * pending writer. This would deadlock, as the write lock can
2223 * only be granted when our parent thread gives up the lock.
2224 * The _prio interface gives us priority over a pending writer.
2226 dsl_pool_config_enter_prio(dp
, FTAG
);
2228 dmu_objset_find_dp_impl(dcp
);
2230 dsl_pool_config_exit(dp
, FTAG
);
2234 * Find objsets under and including ddobj, call func(ds) on each.
2235 * The order for the enumeration is completely undefined.
2236 * func is called with dsl_pool_config held.
2239 dmu_objset_find_dp(dsl_pool_t
*dp
, uint64_t ddobj
,
2240 int func(dsl_pool_t
*, dsl_dataset_t
*, void *), void *arg
, int flags
)
2245 dmu_objset_find_ctx_t
*dcp
;
2248 mutex_init(&err_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
2249 dcp
= kmem_alloc(sizeof (*dcp
), KM_SLEEP
);
2252 dcp
->dc_ddobj
= ddobj
;
2253 dcp
->dc_ddname
= NULL
;
2254 dcp
->dc_func
= func
;
2256 dcp
->dc_flags
= flags
;
2257 dcp
->dc_error_lock
= &err_lock
;
2258 dcp
->dc_error
= &error
;
2260 if ((flags
& DS_FIND_SERIALIZE
) || dsl_pool_config_held_writer(dp
)) {
2262 * In case a write lock is held we can't make use of
2263 * parallelism, as down the stack of the worker threads
2264 * the lock is asserted via dsl_pool_config_held.
2265 * In case of a read lock this is solved by getting a read
2266 * lock in each worker thread, which isn't possible in case
2267 * of a writer lock. So we fall back to the synchronous path
2269 * In the future it might be possible to get some magic into
2270 * dsl_pool_config_held in a way that it returns true for
2271 * the worker threads so that a single lock held from this
2272 * thread suffices. For now, stay single threaded.
2274 dmu_objset_find_dp_impl(dcp
);
2275 mutex_destroy(&err_lock
);
2280 ntasks
= dmu_find_threads
;
2282 ntasks
= vdev_count_leaves(dp
->dp_spa
) * 4;
2283 tq
= taskq_create("dmu_objset_find", ntasks
, maxclsyspri
, ntasks
,
2286 kmem_free(dcp
, sizeof (*dcp
));
2287 mutex_destroy(&err_lock
);
2289 return (SET_ERROR(ENOMEM
));
2293 /* dcp will be freed by task */
2294 (void) taskq_dispatch(tq
, dmu_objset_find_dp_cb
, dcp
, TQ_SLEEP
);
2297 * PORTING: this code relies on the property of taskq_wait to wait
2298 * until no more tasks are queued and no more tasks are active. As
2299 * we always queue new tasks from within other tasks, task_wait
2300 * reliably waits for the full recursion to finish, even though we
2301 * enqueue new tasks after taskq_wait has been called.
2302 * On platforms other than illumos, taskq_wait may not have this
2307 mutex_destroy(&err_lock
);
2313 * Find all objsets under name, and for each, call 'func(child_name, arg)'.
2314 * The dp_config_rwlock must not be held when this is called, and it
2315 * will not be held when the callback is called.
2316 * Therefore this function should only be used when the pool is not changing
2317 * (e.g. in syncing context), or the callback can deal with the possible races.
2320 dmu_objset_find_impl(spa_t
*spa
, const char *name
,
2321 int func(const char *, void *), void *arg
, int flags
)
2324 dsl_pool_t
*dp
= spa_get_dsl(spa
);
2327 zap_attribute_t
*attr
;
2332 dsl_pool_config_enter(dp
, FTAG
);
2334 err
= dsl_dir_hold(dp
, name
, FTAG
, &dd
, NULL
);
2336 dsl_pool_config_exit(dp
, FTAG
);
2340 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
2341 if (dd
->dd_myname
[0] == '$') {
2342 dsl_dir_rele(dd
, FTAG
);
2343 dsl_pool_config_exit(dp
, FTAG
);
2347 thisobj
= dsl_dir_phys(dd
)->dd_head_dataset_obj
;
2348 attr
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
2351 * Iterate over all children.
2353 if (flags
& DS_FIND_CHILDREN
) {
2354 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
,
2355 dsl_dir_phys(dd
)->dd_child_dir_zapobj
);
2356 zap_cursor_retrieve(&zc
, attr
) == 0;
2357 (void) zap_cursor_advance(&zc
)) {
2358 ASSERT3U(attr
->za_integer_length
, ==,
2360 ASSERT3U(attr
->za_num_integers
, ==, 1);
2362 child
= kmem_asprintf("%s/%s", name
, attr
->za_name
);
2363 dsl_pool_config_exit(dp
, FTAG
);
2364 err
= dmu_objset_find_impl(spa
, child
,
2366 dsl_pool_config_enter(dp
, FTAG
);
2371 zap_cursor_fini(&zc
);
2374 dsl_dir_rele(dd
, FTAG
);
2375 dsl_pool_config_exit(dp
, FTAG
);
2376 kmem_free(attr
, sizeof (zap_attribute_t
));
2382 * Iterate over all snapshots.
2384 if (flags
& DS_FIND_SNAPSHOTS
) {
2385 err
= dsl_dataset_hold_obj(dp
, thisobj
, FTAG
, &ds
);
2390 snapobj
= dsl_dataset_phys(ds
)->ds_snapnames_zapobj
;
2391 dsl_dataset_rele(ds
, FTAG
);
2393 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
, snapobj
);
2394 zap_cursor_retrieve(&zc
, attr
) == 0;
2395 (void) zap_cursor_advance(&zc
)) {
2396 ASSERT3U(attr
->za_integer_length
, ==,
2398 ASSERT3U(attr
->za_num_integers
, ==, 1);
2400 child
= kmem_asprintf("%s@%s",
2401 name
, attr
->za_name
);
2402 dsl_pool_config_exit(dp
, FTAG
);
2403 err
= func(child
, arg
);
2404 dsl_pool_config_enter(dp
, FTAG
);
2409 zap_cursor_fini(&zc
);
2413 dsl_dir_rele(dd
, FTAG
);
2414 kmem_free(attr
, sizeof (zap_attribute_t
));
2415 dsl_pool_config_exit(dp
, FTAG
);
2420 /* Apply to self. */
2421 return (func(name
, arg
));
2425 * See comment above dmu_objset_find_impl().
2428 dmu_objset_find(char *name
, int func(const char *, void *), void *arg
,
2434 error
= spa_open(name
, &spa
, FTAG
);
2437 error
= dmu_objset_find_impl(spa
, name
, func
, arg
, flags
);
2438 spa_close(spa
, FTAG
);
2443 dmu_objset_set_user(objset_t
*os
, void *user_ptr
)
2445 ASSERT(MUTEX_HELD(&os
->os_user_ptr_lock
));
2446 os
->os_user_ptr
= user_ptr
;
2450 dmu_objset_get_user(objset_t
*os
)
2452 ASSERT(MUTEX_HELD(&os
->os_user_ptr_lock
));
2453 return (os
->os_user_ptr
);
2457 * Determine name of filesystem, given name of snapshot.
2458 * buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes
2461 dmu_fsname(const char *snapname
, char *buf
)
2463 char *atp
= strchr(snapname
, '@');
2465 return (SET_ERROR(EINVAL
));
2466 if (atp
- snapname
>= ZFS_MAX_DATASET_NAME_LEN
)
2467 return (SET_ERROR(ENAMETOOLONG
));
2468 (void) strlcpy(buf
, snapname
, atp
- snapname
+ 1);
2473 * Call when we think we're going to write/free space in open context to track
2474 * the amount of dirty data in the open txg, which is also the amount
2475 * of memory that can not be evicted until this txg syncs.
2478 dmu_objset_willuse_space(objset_t
*os
, int64_t space
, dmu_tx_t
*tx
)
2480 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
2481 int64_t aspace
= spa_get_worst_case_asize(os
->os_spa
, space
);
2484 dsl_dir_willuse_space(ds
->ds_dir
, aspace
, tx
);
2485 dsl_pool_dirty_space(dmu_tx_pool(tx
), space
, tx
);
2489 #if defined(_KERNEL) && defined(HAVE_SPL)
2490 EXPORT_SYMBOL(dmu_objset_zil
);
2491 EXPORT_SYMBOL(dmu_objset_pool
);
2492 EXPORT_SYMBOL(dmu_objset_ds
);
2493 EXPORT_SYMBOL(dmu_objset_type
);
2494 EXPORT_SYMBOL(dmu_objset_name
);
2495 EXPORT_SYMBOL(dmu_objset_hold
);
2496 EXPORT_SYMBOL(dmu_objset_own
);
2497 EXPORT_SYMBOL(dmu_objset_rele
);
2498 EXPORT_SYMBOL(dmu_objset_disown
);
2499 EXPORT_SYMBOL(dmu_objset_from_ds
);
2500 EXPORT_SYMBOL(dmu_objset_create
);
2501 EXPORT_SYMBOL(dmu_objset_clone
);
2502 EXPORT_SYMBOL(dmu_objset_stats
);
2503 EXPORT_SYMBOL(dmu_objset_fast_stat
);
2504 EXPORT_SYMBOL(dmu_objset_spa
);
2505 EXPORT_SYMBOL(dmu_objset_space
);
2506 EXPORT_SYMBOL(dmu_objset_fsid_guid
);
2507 EXPORT_SYMBOL(dmu_objset_find
);
2508 EXPORT_SYMBOL(dmu_objset_byteswap
);
2509 EXPORT_SYMBOL(dmu_objset_evict_dbufs
);
2510 EXPORT_SYMBOL(dmu_objset_snap_cmtime
);
2511 EXPORT_SYMBOL(dmu_objset_dnodesize
);
2513 EXPORT_SYMBOL(dmu_objset_sync
);
2514 EXPORT_SYMBOL(dmu_objset_is_dirty
);
2515 EXPORT_SYMBOL(dmu_objset_create_impl
);
2516 EXPORT_SYMBOL(dmu_objset_open_impl
);
2517 EXPORT_SYMBOL(dmu_objset_evict
);
2518 EXPORT_SYMBOL(dmu_objset_register_type
);
2519 EXPORT_SYMBOL(dmu_objset_do_userquota_updates
);
2520 EXPORT_SYMBOL(dmu_objset_userquota_get_ids
);
2521 EXPORT_SYMBOL(dmu_objset_userused_enabled
);
2522 EXPORT_SYMBOL(dmu_objset_userspace_upgrade
);
2523 EXPORT_SYMBOL(dmu_objset_userspace_present
);
2524 EXPORT_SYMBOL(dmu_objset_userobjused_enabled
);
2525 EXPORT_SYMBOL(dmu_objset_userobjspace_upgrade
);
2526 EXPORT_SYMBOL(dmu_objset_userobjspace_upgradable
);
2527 EXPORT_SYMBOL(dmu_objset_userobjspace_present
);