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, 2018 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.
31 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
32 * Copyright (c) 2018, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
35 /* Portions Copyright 2010 Robert Milkowski */
37 #include <sys/zfeature.h>
39 #include <sys/zfs_context.h>
40 #include <sys/dmu_objset.h>
41 #include <sys/dsl_dir.h>
42 #include <sys/dsl_dataset.h>
43 #include <sys/dsl_prop.h>
44 #include <sys/dsl_pool.h>
45 #include <sys/dsl_synctask.h>
46 #include <sys/dsl_deleg.h>
47 #include <sys/dnode.h>
50 #include <sys/dmu_tx.h>
53 #include <sys/dmu_impl.h>
54 #include <sys/zfs_ioctl.h>
56 #include <sys/zfs_onexit.h>
57 #include <sys/dsl_destroy.h>
59 #include <sys/zfeature.h>
60 #include <sys/policy.h>
61 #include <sys/spa_impl.h>
62 #include <sys/dmu_recv.h>
63 #include <sys/zfs_project.h>
64 #include "zfs_namecheck.h"
67 * Needed to close a window in dnode_move() that allows the objset to be freed
68 * before it can be safely accessed.
73 * Tunable to overwrite the maximum number of threads for the parallelization
74 * of dmu_objset_find_dp, needed to speed up the import of pools with many
76 * Default is 4 times the number of leaf vdevs.
78 int dmu_find_threads
= 0;
81 * Backfill lower metadnode objects after this many have been freed.
82 * Backfilling negatively impacts object creation rates, so only do it
83 * if there are enough holes to fill.
85 int dmu_rescan_dnode_threshold
= 1 << DN_MAX_INDBLKSHIFT
;
87 static char *upgrade_tag
= "upgrade_tag";
89 static void dmu_objset_find_dp_cb(void *arg
);
91 static void dmu_objset_upgrade(objset_t
*os
, dmu_objset_upgrade_cb_t cb
);
92 static void dmu_objset_upgrade_stop(objset_t
*os
);
97 rw_init(&os_lock
, NULL
, RW_DEFAULT
, NULL
);
101 dmu_objset_fini(void)
103 rw_destroy(&os_lock
);
107 dmu_objset_spa(objset_t
*os
)
113 dmu_objset_zil(objset_t
*os
)
119 dmu_objset_pool(objset_t
*os
)
123 if ((ds
= os
->os_dsl_dataset
) != NULL
&& ds
->ds_dir
)
124 return (ds
->ds_dir
->dd_pool
);
126 return (spa_get_dsl(os
->os_spa
));
130 dmu_objset_ds(objset_t
*os
)
132 return (os
->os_dsl_dataset
);
136 dmu_objset_type(objset_t
*os
)
138 return (os
->os_phys
->os_type
);
142 dmu_objset_name(objset_t
*os
, char *buf
)
144 dsl_dataset_name(os
->os_dsl_dataset
, buf
);
148 dmu_objset_id(objset_t
*os
)
150 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
152 return (ds
? ds
->ds_object
: 0);
156 dmu_objset_dnodesize(objset_t
*os
)
158 return (os
->os_dnodesize
);
162 dmu_objset_syncprop(objset_t
*os
)
164 return (os
->os_sync
);
168 dmu_objset_logbias(objset_t
*os
)
170 return (os
->os_logbias
);
174 checksum_changed_cb(void *arg
, uint64_t newval
)
179 * Inheritance should have been done by now.
181 ASSERT(newval
!= ZIO_CHECKSUM_INHERIT
);
183 os
->os_checksum
= zio_checksum_select(newval
, ZIO_CHECKSUM_ON_VALUE
);
187 compression_changed_cb(void *arg
, uint64_t newval
)
192 * Inheritance and range checking should have been done by now.
194 ASSERT(newval
!= ZIO_COMPRESS_INHERIT
);
196 os
->os_compress
= zio_compress_select(os
->os_spa
, newval
,
201 copies_changed_cb(void *arg
, uint64_t newval
)
206 * Inheritance and range checking should have been done by now.
209 ASSERT(newval
<= spa_max_replication(os
->os_spa
));
211 os
->os_copies
= newval
;
215 dedup_changed_cb(void *arg
, uint64_t newval
)
218 spa_t
*spa
= os
->os_spa
;
219 enum zio_checksum checksum
;
222 * Inheritance should have been done by now.
224 ASSERT(newval
!= ZIO_CHECKSUM_INHERIT
);
226 checksum
= zio_checksum_dedup_select(spa
, newval
, ZIO_CHECKSUM_OFF
);
228 os
->os_dedup_checksum
= checksum
& ZIO_CHECKSUM_MASK
;
229 os
->os_dedup_verify
= !!(checksum
& ZIO_CHECKSUM_VERIFY
);
233 primary_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_primary_cache
= newval
;
247 secondary_cache_changed_cb(void *arg
, uint64_t newval
)
252 * Inheritance and range checking should have been done by now.
254 ASSERT(newval
== ZFS_CACHE_ALL
|| newval
== ZFS_CACHE_NONE
||
255 newval
== ZFS_CACHE_METADATA
);
257 os
->os_secondary_cache
= newval
;
261 sync_changed_cb(void *arg
, uint64_t newval
)
266 * Inheritance and range checking should have been done by now.
268 ASSERT(newval
== ZFS_SYNC_STANDARD
|| newval
== ZFS_SYNC_ALWAYS
||
269 newval
== ZFS_SYNC_DISABLED
);
271 os
->os_sync
= newval
;
273 zil_set_sync(os
->os_zil
, newval
);
277 redundant_metadata_changed_cb(void *arg
, uint64_t newval
)
282 * Inheritance and range checking should have been done by now.
284 ASSERT(newval
== ZFS_REDUNDANT_METADATA_ALL
||
285 newval
== ZFS_REDUNDANT_METADATA_MOST
);
287 os
->os_redundant_metadata
= newval
;
291 dnodesize_changed_cb(void *arg
, uint64_t newval
)
296 case ZFS_DNSIZE_LEGACY
:
297 os
->os_dnodesize
= DNODE_MIN_SIZE
;
299 case ZFS_DNSIZE_AUTO
:
301 * Choose a dnode size that will work well for most
302 * workloads if the user specified "auto". Future code
303 * improvements could dynamically select a dnode size
304 * based on observed workload patterns.
306 os
->os_dnodesize
= DNODE_MIN_SIZE
* 2;
313 os
->os_dnodesize
= newval
;
319 smallblk_changed_cb(void *arg
, uint64_t newval
)
324 * Inheritance and range checking should have been done by now.
326 ASSERT(newval
<= SPA_OLD_MAXBLOCKSIZE
);
327 ASSERT(ISP2(newval
));
329 os
->os_zpl_special_smallblock
= newval
;
333 logbias_changed_cb(void *arg
, uint64_t newval
)
337 ASSERT(newval
== ZFS_LOGBIAS_LATENCY
||
338 newval
== ZFS_LOGBIAS_THROUGHPUT
);
339 os
->os_logbias
= newval
;
341 zil_set_logbias(os
->os_zil
, newval
);
345 recordsize_changed_cb(void *arg
, uint64_t newval
)
349 os
->os_recordsize
= newval
;
353 dmu_objset_byteswap(void *buf
, size_t size
)
355 objset_phys_t
*osp
= buf
;
357 ASSERT(size
== OBJSET_PHYS_SIZE_V1
|| size
== OBJSET_PHYS_SIZE_V2
||
358 size
== sizeof (objset_phys_t
));
359 dnode_byteswap(&osp
->os_meta_dnode
);
360 byteswap_uint64_array(&osp
->os_zil_header
, sizeof (zil_header_t
));
361 osp
->os_type
= BSWAP_64(osp
->os_type
);
362 osp
->os_flags
= BSWAP_64(osp
->os_flags
);
363 if (size
>= OBJSET_PHYS_SIZE_V2
) {
364 dnode_byteswap(&osp
->os_userused_dnode
);
365 dnode_byteswap(&osp
->os_groupused_dnode
);
366 if (size
>= sizeof (objset_phys_t
))
367 dnode_byteswap(&osp
->os_projectused_dnode
);
372 * The hash is a CRC-based hash of the objset_t pointer and the object number.
375 dnode_hash(const objset_t
*os
, uint64_t obj
)
377 uintptr_t osv
= (uintptr_t)os
;
378 uint64_t crc
= -1ULL;
380 ASSERT(zfs_crc64_table
[128] == ZFS_CRC64_POLY
);
382 * The low 6 bits of the pointer don't have much entropy, because
383 * the objset_t is larger than 2^6 bytes long.
385 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (osv
>> 6)) & 0xFF];
386 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 0)) & 0xFF];
387 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 8)) & 0xFF];
388 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 16)) & 0xFF];
390 crc
^= (osv
>>14) ^ (obj
>>24);
396 dnode_multilist_index_func(multilist_t
*ml
, void *obj
)
399 return (dnode_hash(dn
->dn_objset
, dn
->dn_object
) %
400 multilist_get_num_sublists(ml
));
404 * Instantiates the objset_t in-memory structure corresponding to the
405 * objset_phys_t that's pointed to by the specified blkptr_t.
408 dmu_objset_open_impl(spa_t
*spa
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
414 ASSERT(ds
== NULL
|| MUTEX_HELD(&ds
->ds_opening_lock
));
415 ASSERT(!BP_IS_REDACTED(bp
));
418 * The $ORIGIN dataset (if it exists) doesn't have an associated
419 * objset, so there's no reason to open it. The $ORIGIN dataset
420 * will not exist on pools older than SPA_VERSION_ORIGIN.
422 if (ds
!= NULL
&& spa_get_dsl(spa
) != NULL
&&
423 spa_get_dsl(spa
)->dp_origin_snap
!= NULL
) {
424 ASSERT3P(ds
->ds_dir
, !=,
425 spa_get_dsl(spa
)->dp_origin_snap
->ds_dir
);
428 os
= kmem_zalloc(sizeof (objset_t
), KM_SLEEP
);
429 os
->os_dsl_dataset
= ds
;
432 if (!BP_IS_HOLE(os
->os_rootbp
)) {
433 arc_flags_t aflags
= ARC_FLAG_WAIT
;
436 enum zio_flag zio_flags
= ZIO_FLAG_CANFAIL
;
437 SET_BOOKMARK(&zb
, ds
? ds
->ds_object
: DMU_META_OBJSET
,
438 ZB_ROOT_OBJECT
, ZB_ROOT_LEVEL
, ZB_ROOT_BLKID
);
440 if (DMU_OS_IS_L2CACHEABLE(os
))
441 aflags
|= ARC_FLAG_L2CACHE
;
443 if (ds
!= NULL
&& ds
->ds_dir
->dd_crypto_obj
!= 0) {
444 ASSERT3U(BP_GET_COMPRESS(bp
), ==, ZIO_COMPRESS_OFF
);
445 ASSERT(BP_IS_AUTHENTICATED(bp
));
446 zio_flags
|= ZIO_FLAG_RAW
;
449 dprintf_bp(os
->os_rootbp
, "reading %s", "");
450 err
= arc_read(NULL
, spa
, os
->os_rootbp
,
451 arc_getbuf_func
, &os
->os_phys_buf
,
452 ZIO_PRIORITY_SYNC_READ
, zio_flags
, &aflags
, &zb
);
454 kmem_free(os
, sizeof (objset_t
));
455 /* convert checksum errors into IO errors */
457 err
= SET_ERROR(EIO
);
461 if (spa_version(spa
) < SPA_VERSION_USERSPACE
)
462 size
= OBJSET_PHYS_SIZE_V1
;
463 else if (!spa_feature_is_enabled(spa
,
464 SPA_FEATURE_PROJECT_QUOTA
))
465 size
= OBJSET_PHYS_SIZE_V2
;
467 size
= sizeof (objset_phys_t
);
469 /* Increase the blocksize if we are permitted. */
470 if (arc_buf_size(os
->os_phys_buf
) < size
) {
471 arc_buf_t
*buf
= arc_alloc_buf(spa
, &os
->os_phys_buf
,
472 ARC_BUFC_METADATA
, size
);
473 bzero(buf
->b_data
, size
);
474 bcopy(os
->os_phys_buf
->b_data
, buf
->b_data
,
475 arc_buf_size(os
->os_phys_buf
));
476 arc_buf_destroy(os
->os_phys_buf
, &os
->os_phys_buf
);
477 os
->os_phys_buf
= buf
;
480 os
->os_phys
= os
->os_phys_buf
->b_data
;
481 os
->os_flags
= os
->os_phys
->os_flags
;
483 int size
= spa_version(spa
) >= SPA_VERSION_USERSPACE
?
484 sizeof (objset_phys_t
) : OBJSET_PHYS_SIZE_V1
;
485 os
->os_phys_buf
= arc_alloc_buf(spa
, &os
->os_phys_buf
,
486 ARC_BUFC_METADATA
, size
);
487 os
->os_phys
= os
->os_phys_buf
->b_data
;
488 bzero(os
->os_phys
, size
);
491 * These properties will be filled in by the logic in zfs_get_zplprop()
492 * when they are queried for the first time.
494 os
->os_version
= OBJSET_PROP_UNINITIALIZED
;
495 os
->os_normalization
= OBJSET_PROP_UNINITIALIZED
;
496 os
->os_utf8only
= OBJSET_PROP_UNINITIALIZED
;
497 os
->os_casesensitivity
= OBJSET_PROP_UNINITIALIZED
;
500 * Note: the changed_cb will be called once before the register
501 * func returns, thus changing the checksum/compression from the
502 * default (fletcher2/off). Snapshots don't need to know about
503 * checksum/compression/copies.
506 boolean_t needlock
= B_FALSE
;
508 os
->os_encrypted
= (ds
->ds_dir
->dd_crypto_obj
!= 0);
511 * Note: it's valid to open the objset if the dataset is
512 * long-held, in which case the pool_config lock will not
515 if (!dsl_pool_config_held(dmu_objset_pool(os
))) {
517 dsl_pool_config_enter(dmu_objset_pool(os
), FTAG
);
520 err
= dsl_prop_register(ds
,
521 zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE
),
522 primary_cache_changed_cb
, os
);
524 err
= dsl_prop_register(ds
,
525 zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE
),
526 secondary_cache_changed_cb
, os
);
528 if (!ds
->ds_is_snapshot
) {
530 err
= dsl_prop_register(ds
,
531 zfs_prop_to_name(ZFS_PROP_CHECKSUM
),
532 checksum_changed_cb
, os
);
535 err
= dsl_prop_register(ds
,
536 zfs_prop_to_name(ZFS_PROP_COMPRESSION
),
537 compression_changed_cb
, os
);
540 err
= dsl_prop_register(ds
,
541 zfs_prop_to_name(ZFS_PROP_COPIES
),
542 copies_changed_cb
, os
);
545 err
= dsl_prop_register(ds
,
546 zfs_prop_to_name(ZFS_PROP_DEDUP
),
547 dedup_changed_cb
, os
);
550 err
= dsl_prop_register(ds
,
551 zfs_prop_to_name(ZFS_PROP_LOGBIAS
),
552 logbias_changed_cb
, os
);
555 err
= dsl_prop_register(ds
,
556 zfs_prop_to_name(ZFS_PROP_SYNC
),
557 sync_changed_cb
, os
);
560 err
= dsl_prop_register(ds
,
562 ZFS_PROP_REDUNDANT_METADATA
),
563 redundant_metadata_changed_cb
, os
);
566 err
= dsl_prop_register(ds
,
567 zfs_prop_to_name(ZFS_PROP_RECORDSIZE
),
568 recordsize_changed_cb
, os
);
571 err
= dsl_prop_register(ds
,
572 zfs_prop_to_name(ZFS_PROP_DNODESIZE
),
573 dnodesize_changed_cb
, os
);
576 err
= dsl_prop_register(ds
,
578 ZFS_PROP_SPECIAL_SMALL_BLOCKS
),
579 smallblk_changed_cb
, os
);
583 dsl_pool_config_exit(dmu_objset_pool(os
), FTAG
);
585 arc_buf_destroy(os
->os_phys_buf
, &os
->os_phys_buf
);
586 kmem_free(os
, sizeof (objset_t
));
590 /* It's the meta-objset. */
591 os
->os_checksum
= ZIO_CHECKSUM_FLETCHER_4
;
592 os
->os_compress
= ZIO_COMPRESS_ON
;
593 os
->os_encrypted
= B_FALSE
;
594 os
->os_copies
= spa_max_replication(spa
);
595 os
->os_dedup_checksum
= ZIO_CHECKSUM_OFF
;
596 os
->os_dedup_verify
= B_FALSE
;
597 os
->os_logbias
= ZFS_LOGBIAS_LATENCY
;
598 os
->os_sync
= ZFS_SYNC_STANDARD
;
599 os
->os_primary_cache
= ZFS_CACHE_ALL
;
600 os
->os_secondary_cache
= ZFS_CACHE_ALL
;
601 os
->os_dnodesize
= DNODE_MIN_SIZE
;
604 if (ds
== NULL
|| !ds
->ds_is_snapshot
)
605 os
->os_zil_header
= os
->os_phys
->os_zil_header
;
606 os
->os_zil
= zil_alloc(os
, &os
->os_zil_header
);
608 for (i
= 0; i
< TXG_SIZE
; i
++) {
609 os
->os_dirty_dnodes
[i
] = multilist_create(sizeof (dnode_t
),
610 offsetof(dnode_t
, dn_dirty_link
[i
]),
611 dnode_multilist_index_func
);
613 list_create(&os
->os_dnodes
, sizeof (dnode_t
),
614 offsetof(dnode_t
, dn_link
));
615 list_create(&os
->os_downgraded_dbufs
, sizeof (dmu_buf_impl_t
),
616 offsetof(dmu_buf_impl_t
, db_link
));
618 list_link_init(&os
->os_evicting_node
);
620 mutex_init(&os
->os_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
621 mutex_init(&os
->os_userused_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
622 mutex_init(&os
->os_obj_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
623 mutex_init(&os
->os_user_ptr_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
624 os
->os_obj_next_percpu_len
= boot_ncpus
;
625 os
->os_obj_next_percpu
= kmem_zalloc(os
->os_obj_next_percpu_len
*
626 sizeof (os
->os_obj_next_percpu
[0]), KM_SLEEP
);
628 dnode_special_open(os
, &os
->os_phys
->os_meta_dnode
,
629 DMU_META_DNODE_OBJECT
, &os
->os_meta_dnode
);
630 if (OBJSET_BUF_HAS_USERUSED(os
->os_phys_buf
)) {
631 dnode_special_open(os
, &os
->os_phys
->os_userused_dnode
,
632 DMU_USERUSED_OBJECT
, &os
->os_userused_dnode
);
633 dnode_special_open(os
, &os
->os_phys
->os_groupused_dnode
,
634 DMU_GROUPUSED_OBJECT
, &os
->os_groupused_dnode
);
635 if (OBJSET_BUF_HAS_PROJECTUSED(os
->os_phys_buf
))
636 dnode_special_open(os
,
637 &os
->os_phys
->os_projectused_dnode
,
638 DMU_PROJECTUSED_OBJECT
, &os
->os_projectused_dnode
);
641 mutex_init(&os
->os_upgrade_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
648 dmu_objset_from_ds(dsl_dataset_t
*ds
, objset_t
**osp
)
653 * We shouldn't be doing anything with dsl_dataset_t's unless the
654 * pool_config lock is held, or the dataset is long-held.
656 ASSERT(dsl_pool_config_held(ds
->ds_dir
->dd_pool
) ||
657 dsl_dataset_long_held(ds
));
659 mutex_enter(&ds
->ds_opening_lock
);
660 if (ds
->ds_objset
== NULL
) {
662 rrw_enter(&ds
->ds_bp_rwlock
, RW_READER
, FTAG
);
663 err
= dmu_objset_open_impl(dsl_dataset_get_spa(ds
),
664 ds
, dsl_dataset_get_blkptr(ds
), &os
);
665 rrw_exit(&ds
->ds_bp_rwlock
, FTAG
);
668 mutex_enter(&ds
->ds_lock
);
669 ASSERT(ds
->ds_objset
== NULL
);
671 mutex_exit(&ds
->ds_lock
);
674 *osp
= ds
->ds_objset
;
675 mutex_exit(&ds
->ds_opening_lock
);
680 * Holds the pool while the objset is held. Therefore only one objset
681 * can be held at a time.
684 dmu_objset_hold_flags(const char *name
, boolean_t decrypt
, void *tag
,
690 ds_hold_flags_t flags
= (decrypt
) ? DS_HOLD_FLAG_DECRYPT
: 0;
692 err
= dsl_pool_hold(name
, tag
, &dp
);
695 err
= dsl_dataset_hold_flags(dp
, name
, flags
, tag
, &ds
);
697 dsl_pool_rele(dp
, tag
);
701 err
= dmu_objset_from_ds(ds
, osp
);
703 dsl_dataset_rele(ds
, tag
);
704 dsl_pool_rele(dp
, tag
);
711 dmu_objset_hold(const char *name
, void *tag
, objset_t
**osp
)
713 return (dmu_objset_hold_flags(name
, B_FALSE
, tag
, osp
));
717 dmu_objset_own_impl(dsl_dataset_t
*ds
, dmu_objset_type_t type
,
718 boolean_t readonly
, boolean_t decrypt
, void *tag
, objset_t
**osp
)
722 err
= dmu_objset_from_ds(ds
, osp
);
725 } else if (type
!= DMU_OST_ANY
&& type
!= (*osp
)->os_phys
->os_type
) {
726 return (SET_ERROR(EINVAL
));
727 } else if (!readonly
&& dsl_dataset_is_snapshot(ds
)) {
728 return (SET_ERROR(EROFS
));
729 } else if (!readonly
&& decrypt
&&
730 dsl_dir_incompatible_encryption_version(ds
->ds_dir
)) {
731 return (SET_ERROR(EROFS
));
734 /* if we are decrypting, we can now check MACs in os->os_phys_buf */
735 if (decrypt
&& arc_is_unauthenticated((*osp
)->os_phys_buf
)) {
738 SET_BOOKMARK(&zb
, ds
->ds_object
, ZB_ROOT_OBJECT
,
739 ZB_ROOT_LEVEL
, ZB_ROOT_BLKID
);
740 err
= arc_untransform((*osp
)->os_phys_buf
, (*osp
)->os_spa
,
745 ASSERT0(arc_is_unauthenticated((*osp
)->os_phys_buf
));
752 * dsl_pool must not be held when this is called.
753 * Upon successful return, there will be a longhold on the dataset,
754 * and the dsl_pool will not be held.
757 dmu_objset_own(const char *name
, dmu_objset_type_t type
,
758 boolean_t readonly
, boolean_t decrypt
, void *tag
, objset_t
**osp
)
763 ds_hold_flags_t flags
= (decrypt
) ? DS_HOLD_FLAG_DECRYPT
: 0;
765 err
= dsl_pool_hold(name
, FTAG
, &dp
);
768 err
= dsl_dataset_own(dp
, name
, flags
, tag
, &ds
);
770 dsl_pool_rele(dp
, FTAG
);
773 err
= dmu_objset_own_impl(ds
, type
, readonly
, decrypt
, tag
, osp
);
775 dsl_dataset_disown(ds
, flags
, tag
);
776 dsl_pool_rele(dp
, FTAG
);
781 * User accounting requires the dataset to be decrypted and rw.
782 * We also don't begin user accounting during claiming to help
783 * speed up pool import times and to keep this txg reserved
784 * completely for recovery work.
786 if ((dmu_objset_userobjspace_upgradable(*osp
) ||
787 dmu_objset_projectquota_upgradable(*osp
)) &&
788 !readonly
&& !dp
->dp_spa
->spa_claiming
&&
789 (ds
->ds_dir
->dd_crypto_obj
== 0 || decrypt
))
790 dmu_objset_id_quota_upgrade(*osp
);
792 dsl_pool_rele(dp
, FTAG
);
797 dmu_objset_own_obj(dsl_pool_t
*dp
, uint64_t obj
, dmu_objset_type_t type
,
798 boolean_t readonly
, boolean_t decrypt
, void *tag
, objset_t
**osp
)
802 ds_hold_flags_t flags
= (decrypt
) ? DS_HOLD_FLAG_DECRYPT
: 0;
804 err
= dsl_dataset_own_obj(dp
, obj
, flags
, tag
, &ds
);
808 err
= dmu_objset_own_impl(ds
, type
, readonly
, decrypt
, tag
, osp
);
810 dsl_dataset_disown(ds
, flags
, tag
);
818 dmu_objset_rele_flags(objset_t
*os
, boolean_t decrypt
, void *tag
)
820 ds_hold_flags_t flags
= (decrypt
) ? DS_HOLD_FLAG_DECRYPT
: 0;
822 dsl_pool_t
*dp
= dmu_objset_pool(os
);
823 dsl_dataset_rele_flags(os
->os_dsl_dataset
, flags
, tag
);
824 dsl_pool_rele(dp
, tag
);
828 dmu_objset_rele(objset_t
*os
, void *tag
)
830 dmu_objset_rele_flags(os
, B_FALSE
, tag
);
834 * When we are called, os MUST refer to an objset associated with a dataset
835 * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner
836 * == tag. We will then release and reacquire ownership of the dataset while
837 * holding the pool config_rwlock to avoid intervening namespace or ownership
840 * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to
841 * release the hold on its dataset and acquire a new one on the dataset of the
842 * same name so that it can be partially torn down and reconstructed.
845 dmu_objset_refresh_ownership(dsl_dataset_t
*ds
, dsl_dataset_t
**newds
,
846 boolean_t decrypt
, void *tag
)
849 char name
[ZFS_MAX_DATASET_NAME_LEN
];
851 VERIFY3P(ds
, !=, NULL
);
852 VERIFY3P(ds
->ds_owner
, ==, tag
);
853 VERIFY(dsl_dataset_long_held(ds
));
855 dsl_dataset_name(ds
, name
);
856 dp
= ds
->ds_dir
->dd_pool
;
857 dsl_pool_config_enter(dp
, FTAG
);
858 dsl_dataset_disown(ds
, decrypt
, tag
);
859 VERIFY0(dsl_dataset_own(dp
, name
,
860 (decrypt
) ? DS_HOLD_FLAG_DECRYPT
: 0, tag
, newds
));
861 dsl_pool_config_exit(dp
, FTAG
);
865 dmu_objset_disown(objset_t
*os
, boolean_t decrypt
, void *tag
)
868 * Stop upgrading thread
870 dmu_objset_upgrade_stop(os
);
871 dsl_dataset_disown(os
->os_dsl_dataset
,
872 (decrypt
) ? DS_HOLD_FLAG_DECRYPT
: 0, tag
);
876 dmu_objset_evict_dbufs(objset_t
*os
)
881 dn_marker
= kmem_alloc(sizeof (dnode_t
), KM_SLEEP
);
883 mutex_enter(&os
->os_lock
);
884 dn
= list_head(&os
->os_dnodes
);
887 * Skip dnodes without holds. We have to do this dance
888 * because dnode_add_ref() only works if there is already a
889 * hold. If the dnode has no holds, then it has no dbufs.
891 if (dnode_add_ref(dn
, FTAG
)) {
892 list_insert_after(&os
->os_dnodes
, dn
, dn_marker
);
893 mutex_exit(&os
->os_lock
);
895 dnode_evict_dbufs(dn
);
896 dnode_rele(dn
, FTAG
);
898 mutex_enter(&os
->os_lock
);
899 dn
= list_next(&os
->os_dnodes
, dn_marker
);
900 list_remove(&os
->os_dnodes
, dn_marker
);
902 dn
= list_next(&os
->os_dnodes
, dn
);
905 mutex_exit(&os
->os_lock
);
907 kmem_free(dn_marker
, sizeof (dnode_t
));
909 if (DMU_USERUSED_DNODE(os
) != NULL
) {
910 if (DMU_PROJECTUSED_DNODE(os
) != NULL
)
911 dnode_evict_dbufs(DMU_PROJECTUSED_DNODE(os
));
912 dnode_evict_dbufs(DMU_GROUPUSED_DNODE(os
));
913 dnode_evict_dbufs(DMU_USERUSED_DNODE(os
));
915 dnode_evict_dbufs(DMU_META_DNODE(os
));
919 * Objset eviction processing is split into into two pieces.
920 * The first marks the objset as evicting, evicts any dbufs that
921 * have a refcount of zero, and then queues up the objset for the
922 * second phase of eviction. Once os->os_dnodes has been cleared by
923 * dnode_buf_pageout()->dnode_destroy(), the second phase is executed.
924 * The second phase closes the special dnodes, dequeues the objset from
925 * the list of those undergoing eviction, and finally frees the objset.
927 * NOTE: Due to asynchronous eviction processing (invocation of
928 * dnode_buf_pageout()), it is possible for the meta dnode for the
929 * objset to have no holds even though os->os_dnodes is not empty.
932 dmu_objset_evict(objset_t
*os
)
934 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
936 for (int t
= 0; t
< TXG_SIZE
; t
++)
937 ASSERT(!dmu_objset_is_dirty(os
, t
));
940 dsl_prop_unregister_all(ds
, os
);
945 dmu_objset_evict_dbufs(os
);
947 mutex_enter(&os
->os_lock
);
948 spa_evicting_os_register(os
->os_spa
, os
);
949 if (list_is_empty(&os
->os_dnodes
)) {
950 mutex_exit(&os
->os_lock
);
951 dmu_objset_evict_done(os
);
953 mutex_exit(&os
->os_lock
);
960 dmu_objset_evict_done(objset_t
*os
)
962 ASSERT3P(list_head(&os
->os_dnodes
), ==, NULL
);
964 dnode_special_close(&os
->os_meta_dnode
);
965 if (DMU_USERUSED_DNODE(os
)) {
966 if (DMU_PROJECTUSED_DNODE(os
))
967 dnode_special_close(&os
->os_projectused_dnode
);
968 dnode_special_close(&os
->os_userused_dnode
);
969 dnode_special_close(&os
->os_groupused_dnode
);
971 zil_free(os
->os_zil
);
973 arc_buf_destroy(os
->os_phys_buf
, &os
->os_phys_buf
);
976 * This is a barrier to prevent the objset from going away in
977 * dnode_move() until we can safely ensure that the objset is still in
978 * use. We consider the objset valid before the barrier and invalid
981 rw_enter(&os_lock
, RW_READER
);
984 kmem_free(os
->os_obj_next_percpu
,
985 os
->os_obj_next_percpu_len
* sizeof (os
->os_obj_next_percpu
[0]));
987 mutex_destroy(&os
->os_lock
);
988 mutex_destroy(&os
->os_userused_lock
);
989 mutex_destroy(&os
->os_obj_lock
);
990 mutex_destroy(&os
->os_user_ptr_lock
);
991 mutex_destroy(&os
->os_upgrade_lock
);
992 for (int i
= 0; i
< TXG_SIZE
; i
++) {
993 multilist_destroy(os
->os_dirty_dnodes
[i
]);
995 spa_evicting_os_deregister(os
->os_spa
, os
);
996 kmem_free(os
, sizeof (objset_t
));
1000 dmu_objset_snap_cmtime(objset_t
*os
)
1002 return (dsl_dir_snap_cmtime(os
->os_dsl_dataset
->ds_dir
));
1006 dmu_objset_create_impl_dnstats(spa_t
*spa
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
1007 dmu_objset_type_t type
, int levels
, int blksz
, int ibs
, dmu_tx_t
*tx
)
1012 ASSERT(dmu_tx_is_syncing(tx
));
1015 blksz
= DNODE_BLOCK_SIZE
;
1017 ibs
= DN_MAX_INDBLKSHIFT
;
1020 VERIFY0(dmu_objset_from_ds(ds
, &os
));
1022 VERIFY0(dmu_objset_open_impl(spa
, NULL
, bp
, &os
));
1024 mdn
= DMU_META_DNODE(os
);
1026 dnode_allocate(mdn
, DMU_OT_DNODE
, blksz
, ibs
, DMU_OT_NONE
, 0,
1027 DNODE_MIN_SLOTS
, tx
);
1030 * We don't want to have to increase the meta-dnode's nlevels
1031 * later, because then we could do it in quiescing context while
1032 * we are also accessing it in open context.
1034 * This precaution is not necessary for the MOS (ds == NULL),
1035 * because the MOS is only updated in syncing context.
1036 * This is most fortunate: the MOS is the only objset that
1037 * needs to be synced multiple times as spa_sync() iterates
1038 * to convergence, so minimizing its dn_nlevels matters.
1045 * Determine the number of levels necessary for the
1046 * meta-dnode to contain DN_MAX_OBJECT dnodes. Note
1047 * that in order to ensure that we do not overflow
1048 * 64 bits, there has to be a nlevels that gives us a
1049 * number of blocks > DN_MAX_OBJECT but < 2^64.
1050 * Therefore, (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)
1051 * (10) must be less than (64 - log2(DN_MAX_OBJECT))
1054 while ((uint64_t)mdn
->dn_nblkptr
<<
1055 (mdn
->dn_datablkshift
- DNODE_SHIFT
+ (levels
- 1) *
1056 (mdn
->dn_indblkshift
- SPA_BLKPTRSHIFT
)) <
1061 mdn
->dn_next_nlevels
[tx
->tx_txg
& TXG_MASK
] =
1062 mdn
->dn_nlevels
= levels
;
1065 ASSERT(type
!= DMU_OST_NONE
);
1066 ASSERT(type
!= DMU_OST_ANY
);
1067 ASSERT(type
< DMU_OST_NUMTYPES
);
1068 os
->os_phys
->os_type
= type
;
1071 * Enable user accounting if it is enabled and this is not an
1072 * encrypted receive.
1074 if (dmu_objset_userused_enabled(os
) &&
1075 (!os
->os_encrypted
|| !dmu_objset_is_receiving(os
))) {
1076 os
->os_phys
->os_flags
|= OBJSET_FLAG_USERACCOUNTING_COMPLETE
;
1077 if (dmu_objset_userobjused_enabled(os
)) {
1078 ds
->ds_feature_activation
[
1079 SPA_FEATURE_USEROBJ_ACCOUNTING
] = (void *)B_TRUE
;
1080 os
->os_phys
->os_flags
|=
1081 OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE
;
1083 if (dmu_objset_projectquota_enabled(os
)) {
1084 ds
->ds_feature_activation
[
1085 SPA_FEATURE_PROJECT_QUOTA
] = (void *)B_TRUE
;
1086 os
->os_phys
->os_flags
|=
1087 OBJSET_FLAG_PROJECTQUOTA_COMPLETE
;
1089 os
->os_flags
= os
->os_phys
->os_flags
;
1092 dsl_dataset_dirty(ds
, tx
);
1097 /* called from dsl for meta-objset */
1099 dmu_objset_create_impl(spa_t
*spa
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
1100 dmu_objset_type_t type
, dmu_tx_t
*tx
)
1102 return (dmu_objset_create_impl_dnstats(spa
, ds
, bp
, type
, 0, 0, 0, tx
));
1105 typedef struct dmu_objset_create_arg
{
1106 const char *doca_name
;
1108 void (*doca_userfunc
)(objset_t
*os
, void *arg
,
1109 cred_t
*cr
, dmu_tx_t
*tx
);
1111 dmu_objset_type_t doca_type
;
1112 uint64_t doca_flags
;
1113 dsl_crypto_params_t
*doca_dcp
;
1114 } dmu_objset_create_arg_t
;
1118 dmu_objset_create_check(void *arg
, dmu_tx_t
*tx
)
1120 dmu_objset_create_arg_t
*doca
= arg
;
1121 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1123 dsl_dataset_t
*parentds
;
1128 if (strchr(doca
->doca_name
, '@') != NULL
)
1129 return (SET_ERROR(EINVAL
));
1131 if (strlen(doca
->doca_name
) >= ZFS_MAX_DATASET_NAME_LEN
)
1132 return (SET_ERROR(ENAMETOOLONG
));
1134 if (dataset_nestcheck(doca
->doca_name
) != 0)
1135 return (SET_ERROR(ENAMETOOLONG
));
1137 error
= dsl_dir_hold(dp
, doca
->doca_name
, FTAG
, &pdd
, &tail
);
1141 dsl_dir_rele(pdd
, FTAG
);
1142 return (SET_ERROR(EEXIST
));
1145 error
= dmu_objset_create_crypt_check(pdd
, doca
->doca_dcp
, NULL
);
1147 dsl_dir_rele(pdd
, FTAG
);
1151 error
= dsl_fs_ss_limit_check(pdd
, 1, ZFS_PROP_FILESYSTEM_LIMIT
, NULL
,
1154 dsl_dir_rele(pdd
, FTAG
);
1158 /* can't create below anything but filesystems (eg. no ZVOLs) */
1159 error
= dsl_dataset_hold_obj(pdd
->dd_pool
,
1160 dsl_dir_phys(pdd
)->dd_head_dataset_obj
, FTAG
, &parentds
);
1162 dsl_dir_rele(pdd
, FTAG
);
1165 error
= dmu_objset_from_ds(parentds
, &parentos
);
1167 dsl_dataset_rele(parentds
, FTAG
);
1168 dsl_dir_rele(pdd
, FTAG
);
1171 if (dmu_objset_type(parentos
) != DMU_OST_ZFS
) {
1172 dsl_dataset_rele(parentds
, FTAG
);
1173 dsl_dir_rele(pdd
, FTAG
);
1174 return (SET_ERROR(ZFS_ERR_WRONG_PARENT
));
1176 dsl_dataset_rele(parentds
, FTAG
);
1177 dsl_dir_rele(pdd
, FTAG
);
1183 dmu_objset_create_sync(void *arg
, dmu_tx_t
*tx
)
1185 dmu_objset_create_arg_t
*doca
= arg
;
1186 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1187 spa_t
*spa
= dp
->dp_spa
;
1196 VERIFY0(dsl_dir_hold(dp
, doca
->doca_name
, FTAG
, &pdd
, &tail
));
1198 obj
= dsl_dataset_create_sync(pdd
, tail
, NULL
, doca
->doca_flags
,
1199 doca
->doca_cred
, doca
->doca_dcp
, tx
);
1201 VERIFY0(dsl_dataset_hold_obj_flags(pdd
->dd_pool
, obj
,
1202 DS_HOLD_FLAG_DECRYPT
, FTAG
, &ds
));
1203 rrw_enter(&ds
->ds_bp_rwlock
, RW_READER
, FTAG
);
1204 bp
= dsl_dataset_get_blkptr(ds
);
1205 os
= dmu_objset_create_impl(spa
, ds
, bp
, doca
->doca_type
, tx
);
1206 rrw_exit(&ds
->ds_bp_rwlock
, FTAG
);
1208 if (doca
->doca_userfunc
!= NULL
) {
1209 doca
->doca_userfunc(os
, doca
->doca_userarg
,
1210 doca
->doca_cred
, tx
);
1214 * The doca_userfunc() may write out some data that needs to be
1215 * encrypted if the dataset is encrypted (specifically the root
1216 * directory). This data must be written out before the encryption
1217 * key mapping is removed by dsl_dataset_rele_flags(). Force the
1218 * I/O to occur immediately by invoking the relevant sections of
1221 if (os
->os_encrypted
) {
1222 dsl_dataset_t
*tmpds
= NULL
;
1223 boolean_t need_sync_done
= B_FALSE
;
1225 mutex_enter(&ds
->ds_lock
);
1226 ds
->ds_owner
= FTAG
;
1227 mutex_exit(&ds
->ds_lock
);
1229 rzio
= zio_root(spa
, NULL
, NULL
, ZIO_FLAG_MUSTSUCCEED
);
1230 tmpds
= txg_list_remove_this(&dp
->dp_dirty_datasets
, ds
,
1232 if (tmpds
!= NULL
) {
1233 dsl_dataset_sync(ds
, rzio
, tx
);
1234 need_sync_done
= B_TRUE
;
1236 VERIFY0(zio_wait(rzio
));
1238 dmu_objset_do_userquota_updates(os
, tx
);
1239 taskq_wait(dp
->dp_sync_taskq
);
1240 if (txg_list_member(&dp
->dp_dirty_datasets
, ds
, tx
->tx_txg
)) {
1241 ASSERT3P(ds
->ds_key_mapping
, !=, NULL
);
1242 key_mapping_rele(spa
, ds
->ds_key_mapping
, ds
);
1245 rzio
= zio_root(spa
, NULL
, NULL
, ZIO_FLAG_MUSTSUCCEED
);
1246 tmpds
= txg_list_remove_this(&dp
->dp_dirty_datasets
, ds
,
1248 if (tmpds
!= NULL
) {
1249 dmu_buf_rele(ds
->ds_dbuf
, ds
);
1250 dsl_dataset_sync(ds
, rzio
, tx
);
1252 VERIFY0(zio_wait(rzio
));
1254 if (need_sync_done
) {
1255 ASSERT3P(ds
->ds_key_mapping
, !=, NULL
);
1256 key_mapping_rele(spa
, ds
->ds_key_mapping
, ds
);
1257 dsl_dataset_sync_done(ds
, tx
);
1260 mutex_enter(&ds
->ds_lock
);
1261 ds
->ds_owner
= NULL
;
1262 mutex_exit(&ds
->ds_lock
);
1265 spa_history_log_internal_ds(ds
, "create", tx
, "");
1266 zvol_create_minors(spa
, doca
->doca_name
, B_TRUE
);
1268 dsl_dataset_rele_flags(ds
, DS_HOLD_FLAG_DECRYPT
, FTAG
);
1269 dsl_dir_rele(pdd
, FTAG
);
1273 dmu_objset_create(const char *name
, dmu_objset_type_t type
, uint64_t flags
,
1274 dsl_crypto_params_t
*dcp
, dmu_objset_create_sync_func_t func
, void *arg
)
1276 dmu_objset_create_arg_t doca
;
1277 dsl_crypto_params_t tmp_dcp
= { 0 };
1279 doca
.doca_name
= name
;
1280 doca
.doca_cred
= CRED();
1281 doca
.doca_flags
= flags
;
1282 doca
.doca_userfunc
= func
;
1283 doca
.doca_userarg
= arg
;
1284 doca
.doca_type
= type
;
1287 * Some callers (mostly for testing) do not provide a dcp on their
1288 * own but various code inside the sync task will require it to be
1289 * allocated. Rather than adding NULL checks throughout this code
1290 * or adding dummy dcp's to all of the callers we simply create a
1291 * dummy one here and use that. This zero dcp will have the same
1292 * effect as asking for inheritance of all encryption params.
1294 doca
.doca_dcp
= (dcp
!= NULL
) ? dcp
: &tmp_dcp
;
1296 return (dsl_sync_task(name
,
1297 dmu_objset_create_check
, dmu_objset_create_sync
, &doca
,
1298 6, ZFS_SPACE_CHECK_NORMAL
));
1301 typedef struct dmu_objset_clone_arg
{
1302 const char *doca_clone
;
1303 const char *doca_origin
;
1305 } dmu_objset_clone_arg_t
;
1309 dmu_objset_clone_check(void *arg
, dmu_tx_t
*tx
)
1311 dmu_objset_clone_arg_t
*doca
= arg
;
1315 dsl_dataset_t
*origin
;
1316 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1318 if (strchr(doca
->doca_clone
, '@') != NULL
)
1319 return (SET_ERROR(EINVAL
));
1321 if (strlen(doca
->doca_clone
) >= ZFS_MAX_DATASET_NAME_LEN
)
1322 return (SET_ERROR(ENAMETOOLONG
));
1324 error
= dsl_dir_hold(dp
, doca
->doca_clone
, FTAG
, &pdd
, &tail
);
1328 dsl_dir_rele(pdd
, FTAG
);
1329 return (SET_ERROR(EEXIST
));
1332 error
= dsl_fs_ss_limit_check(pdd
, 1, ZFS_PROP_FILESYSTEM_LIMIT
, NULL
,
1335 dsl_dir_rele(pdd
, FTAG
);
1336 return (SET_ERROR(EDQUOT
));
1339 error
= dsl_dataset_hold(dp
, doca
->doca_origin
, FTAG
, &origin
);
1341 dsl_dir_rele(pdd
, FTAG
);
1345 /* You can only clone snapshots, not the head datasets. */
1346 if (!origin
->ds_is_snapshot
) {
1347 dsl_dataset_rele(origin
, FTAG
);
1348 dsl_dir_rele(pdd
, FTAG
);
1349 return (SET_ERROR(EINVAL
));
1352 dsl_dataset_rele(origin
, FTAG
);
1353 dsl_dir_rele(pdd
, FTAG
);
1359 dmu_objset_clone_sync(void *arg
, dmu_tx_t
*tx
)
1361 dmu_objset_clone_arg_t
*doca
= arg
;
1362 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1365 dsl_dataset_t
*origin
, *ds
;
1367 char namebuf
[ZFS_MAX_DATASET_NAME_LEN
];
1369 VERIFY0(dsl_dir_hold(dp
, doca
->doca_clone
, FTAG
, &pdd
, &tail
));
1370 VERIFY0(dsl_dataset_hold(dp
, doca
->doca_origin
, FTAG
, &origin
));
1372 obj
= dsl_dataset_create_sync(pdd
, tail
, origin
, 0,
1373 doca
->doca_cred
, NULL
, tx
);
1375 VERIFY0(dsl_dataset_hold_obj(pdd
->dd_pool
, obj
, FTAG
, &ds
));
1376 dsl_dataset_name(origin
, namebuf
);
1377 spa_history_log_internal_ds(ds
, "clone", tx
,
1378 "origin=%s (%llu)", namebuf
, origin
->ds_object
);
1379 zvol_create_minors(dp
->dp_spa
, doca
->doca_clone
, B_TRUE
);
1380 dsl_dataset_rele(ds
, FTAG
);
1381 dsl_dataset_rele(origin
, FTAG
);
1382 dsl_dir_rele(pdd
, FTAG
);
1386 dmu_objset_clone(const char *clone
, const char *origin
)
1388 dmu_objset_clone_arg_t doca
;
1390 doca
.doca_clone
= clone
;
1391 doca
.doca_origin
= origin
;
1392 doca
.doca_cred
= CRED();
1394 return (dsl_sync_task(clone
,
1395 dmu_objset_clone_check
, dmu_objset_clone_sync
, &doca
,
1396 6, ZFS_SPACE_CHECK_NORMAL
));
1400 dmu_objset_snapshot_one(const char *fsname
, const char *snapname
)
1403 char *longsnap
= kmem_asprintf("%s@%s", fsname
, snapname
);
1404 nvlist_t
*snaps
= fnvlist_alloc();
1406 fnvlist_add_boolean(snaps
, longsnap
);
1408 err
= dsl_dataset_snapshot(snaps
, NULL
, NULL
);
1409 fnvlist_free(snaps
);
1414 dmu_objset_upgrade_task_cb(void *data
)
1416 objset_t
*os
= data
;
1418 mutex_enter(&os
->os_upgrade_lock
);
1419 os
->os_upgrade_status
= EINTR
;
1420 if (!os
->os_upgrade_exit
) {
1421 mutex_exit(&os
->os_upgrade_lock
);
1423 os
->os_upgrade_status
= os
->os_upgrade_cb(os
);
1424 mutex_enter(&os
->os_upgrade_lock
);
1426 os
->os_upgrade_exit
= B_TRUE
;
1427 os
->os_upgrade_id
= 0;
1428 mutex_exit(&os
->os_upgrade_lock
);
1429 dsl_dataset_long_rele(dmu_objset_ds(os
), upgrade_tag
);
1433 dmu_objset_upgrade(objset_t
*os
, dmu_objset_upgrade_cb_t cb
)
1435 if (os
->os_upgrade_id
!= 0)
1438 ASSERT(dsl_pool_config_held(dmu_objset_pool(os
)));
1439 dsl_dataset_long_hold(dmu_objset_ds(os
), upgrade_tag
);
1441 mutex_enter(&os
->os_upgrade_lock
);
1442 if (os
->os_upgrade_id
== 0 && os
->os_upgrade_status
== 0) {
1443 os
->os_upgrade_exit
= B_FALSE
;
1444 os
->os_upgrade_cb
= cb
;
1445 os
->os_upgrade_id
= taskq_dispatch(
1446 os
->os_spa
->spa_upgrade_taskq
,
1447 dmu_objset_upgrade_task_cb
, os
, TQ_SLEEP
);
1448 if (os
->os_upgrade_id
== TASKQID_INVALID
) {
1449 dsl_dataset_long_rele(dmu_objset_ds(os
), upgrade_tag
);
1450 os
->os_upgrade_status
= ENOMEM
;
1453 mutex_exit(&os
->os_upgrade_lock
);
1457 dmu_objset_upgrade_stop(objset_t
*os
)
1459 mutex_enter(&os
->os_upgrade_lock
);
1460 os
->os_upgrade_exit
= B_TRUE
;
1461 if (os
->os_upgrade_id
!= 0) {
1462 taskqid_t id
= os
->os_upgrade_id
;
1464 os
->os_upgrade_id
= 0;
1465 mutex_exit(&os
->os_upgrade_lock
);
1467 if ((taskq_cancel_id(os
->os_spa
->spa_upgrade_taskq
, id
)) == 0) {
1468 dsl_dataset_long_rele(dmu_objset_ds(os
), upgrade_tag
);
1470 txg_wait_synced(os
->os_spa
->spa_dsl_pool
, 0);
1472 mutex_exit(&os
->os_upgrade_lock
);
1477 dmu_objset_sync_dnodes(multilist_sublist_t
*list
, dmu_tx_t
*tx
)
1481 while ((dn
= multilist_sublist_head(list
)) != NULL
) {
1482 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
);
1483 ASSERT(dn
->dn_dbuf
->db_data_pending
);
1485 * Initialize dn_zio outside dnode_sync() because the
1486 * meta-dnode needs to set it outside dnode_sync().
1488 dn
->dn_zio
= dn
->dn_dbuf
->db_data_pending
->dr_zio
;
1491 ASSERT3U(dn
->dn_nlevels
, <=, DN_MAX_LEVELS
);
1492 multilist_sublist_remove(list
, dn
);
1495 * If we are not doing useraccounting (os_synced_dnodes == NULL)
1496 * we are done with this dnode for this txg. Unset dn_dirty_txg
1497 * if later txgs aren't dirtying it so that future holders do
1498 * not get a stale value. Otherwise, we will do this in
1499 * userquota_updates_task() when processing has completely
1500 * finished for this txg.
1502 multilist_t
*newlist
= dn
->dn_objset
->os_synced_dnodes
;
1503 if (newlist
!= NULL
) {
1504 (void) dnode_add_ref(dn
, newlist
);
1505 multilist_insert(newlist
, dn
);
1507 mutex_enter(&dn
->dn_mtx
);
1508 if (dn
->dn_dirty_txg
== tx
->tx_txg
)
1509 dn
->dn_dirty_txg
= 0;
1510 mutex_exit(&dn
->dn_mtx
);
1519 dmu_objset_write_ready(zio_t
*zio
, arc_buf_t
*abuf
, void *arg
)
1521 blkptr_t
*bp
= zio
->io_bp
;
1523 dnode_phys_t
*dnp
= &os
->os_phys
->os_meta_dnode
;
1526 ASSERT(!BP_IS_EMBEDDED(bp
));
1527 ASSERT3U(BP_GET_TYPE(bp
), ==, DMU_OT_OBJSET
);
1528 ASSERT0(BP_GET_LEVEL(bp
));
1531 * Update rootbp fill count: it should be the number of objects
1532 * allocated in the object set (not counting the "special"
1533 * objects that are stored in the objset_phys_t -- the meta
1534 * dnode and user/group/project accounting objects).
1536 for (int i
= 0; i
< dnp
->dn_nblkptr
; i
++)
1537 fill
+= BP_GET_FILL(&dnp
->dn_blkptr
[i
]);
1539 BP_SET_FILL(bp
, fill
);
1541 if (os
->os_dsl_dataset
!= NULL
)
1542 rrw_enter(&os
->os_dsl_dataset
->ds_bp_rwlock
, RW_WRITER
, FTAG
);
1543 *os
->os_rootbp
= *bp
;
1544 if (os
->os_dsl_dataset
!= NULL
)
1545 rrw_exit(&os
->os_dsl_dataset
->ds_bp_rwlock
, FTAG
);
1550 dmu_objset_write_done(zio_t
*zio
, arc_buf_t
*abuf
, void *arg
)
1552 blkptr_t
*bp
= zio
->io_bp
;
1553 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
1556 if (zio
->io_flags
& ZIO_FLAG_IO_REWRITE
) {
1557 ASSERT(BP_EQUAL(bp
, bp_orig
));
1559 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
1560 dmu_tx_t
*tx
= os
->os_synctx
;
1562 (void) dsl_dataset_block_kill(ds
, bp_orig
, tx
, B_TRUE
);
1563 dsl_dataset_block_born(ds
, bp
, tx
);
1565 kmem_free(bp
, sizeof (*bp
));
1568 typedef struct sync_dnodes_arg
{
1569 multilist_t
*sda_list
;
1570 int sda_sublist_idx
;
1571 multilist_t
*sda_newlist
;
1573 } sync_dnodes_arg_t
;
1576 sync_dnodes_task(void *arg
)
1578 sync_dnodes_arg_t
*sda
= arg
;
1580 multilist_sublist_t
*ms
=
1581 multilist_sublist_lock(sda
->sda_list
, sda
->sda_sublist_idx
);
1583 dmu_objset_sync_dnodes(ms
, sda
->sda_tx
);
1585 multilist_sublist_unlock(ms
);
1587 kmem_free(sda
, sizeof (*sda
));
1591 /* called from dsl */
1593 dmu_objset_sync(objset_t
*os
, zio_t
*pio
, dmu_tx_t
*tx
)
1596 zbookmark_phys_t zb
;
1600 dbuf_dirty_record_t
*dr
;
1603 blkptr_t
*blkptr_copy
= kmem_alloc(sizeof (*os
->os_rootbp
), KM_SLEEP
);
1604 *blkptr_copy
= *os
->os_rootbp
;
1606 dprintf_ds(os
->os_dsl_dataset
, "txg=%llu\n", tx
->tx_txg
);
1608 ASSERT(dmu_tx_is_syncing(tx
));
1609 /* XXX the write_done callback should really give us the tx... */
1612 if (os
->os_dsl_dataset
== NULL
) {
1614 * This is the MOS. If we have upgraded,
1615 * spa_max_replication() could change, so reset
1618 os
->os_copies
= spa_max_replication(os
->os_spa
);
1622 * Create the root block IO
1624 SET_BOOKMARK(&zb
, os
->os_dsl_dataset
?
1625 os
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
1626 ZB_ROOT_OBJECT
, ZB_ROOT_LEVEL
, ZB_ROOT_BLKID
);
1627 arc_release(os
->os_phys_buf
, &os
->os_phys_buf
);
1629 dmu_write_policy(os
, NULL
, 0, 0, &zp
);
1632 * If we are either claiming the ZIL or doing a raw receive, write
1633 * out the os_phys_buf raw. Neither of these actions will effect the
1634 * MAC at this point.
1636 if (os
->os_raw_receive
||
1637 os
->os_next_write_raw
[tx
->tx_txg
& TXG_MASK
]) {
1638 ASSERT(os
->os_encrypted
);
1639 arc_convert_to_raw(os
->os_phys_buf
,
1640 os
->os_dsl_dataset
->ds_object
, ZFS_HOST_BYTEORDER
,
1641 DMU_OT_OBJSET
, NULL
, NULL
, NULL
);
1644 zio
= arc_write(pio
, os
->os_spa
, tx
->tx_txg
,
1645 blkptr_copy
, os
->os_phys_buf
, DMU_OS_IS_L2CACHEABLE(os
),
1646 &zp
, dmu_objset_write_ready
, NULL
, NULL
, dmu_objset_write_done
,
1647 os
, ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);
1650 * Sync special dnodes - the parent IO for the sync is the root block
1652 DMU_META_DNODE(os
)->dn_zio
= zio
;
1653 dnode_sync(DMU_META_DNODE(os
), tx
);
1655 os
->os_phys
->os_flags
= os
->os_flags
;
1657 if (DMU_USERUSED_DNODE(os
) &&
1658 DMU_USERUSED_DNODE(os
)->dn_type
!= DMU_OT_NONE
) {
1659 DMU_USERUSED_DNODE(os
)->dn_zio
= zio
;
1660 dnode_sync(DMU_USERUSED_DNODE(os
), tx
);
1661 DMU_GROUPUSED_DNODE(os
)->dn_zio
= zio
;
1662 dnode_sync(DMU_GROUPUSED_DNODE(os
), tx
);
1665 if (DMU_PROJECTUSED_DNODE(os
) &&
1666 DMU_PROJECTUSED_DNODE(os
)->dn_type
!= DMU_OT_NONE
) {
1667 DMU_PROJECTUSED_DNODE(os
)->dn_zio
= zio
;
1668 dnode_sync(DMU_PROJECTUSED_DNODE(os
), tx
);
1671 txgoff
= tx
->tx_txg
& TXG_MASK
;
1673 if (dmu_objset_userused_enabled(os
) &&
1674 (!os
->os_encrypted
|| !dmu_objset_is_receiving(os
))) {
1676 * We must create the list here because it uses the
1677 * dn_dirty_link[] of this txg. But it may already
1678 * exist because we call dsl_dataset_sync() twice per txg.
1680 if (os
->os_synced_dnodes
== NULL
) {
1681 os
->os_synced_dnodes
=
1682 multilist_create(sizeof (dnode_t
),
1683 offsetof(dnode_t
, dn_dirty_link
[txgoff
]),
1684 dnode_multilist_index_func
);
1686 ASSERT3U(os
->os_synced_dnodes
->ml_offset
, ==,
1687 offsetof(dnode_t
, dn_dirty_link
[txgoff
]));
1691 ml
= os
->os_dirty_dnodes
[txgoff
];
1692 num_sublists
= multilist_get_num_sublists(ml
);
1693 for (int i
= 0; i
< num_sublists
; i
++) {
1694 if (multilist_sublist_is_empty_idx(ml
, i
))
1696 sync_dnodes_arg_t
*sda
= kmem_alloc(sizeof (*sda
), KM_SLEEP
);
1698 sda
->sda_sublist_idx
= i
;
1700 (void) taskq_dispatch(dmu_objset_pool(os
)->dp_sync_taskq
,
1701 sync_dnodes_task
, sda
, 0);
1702 /* callback frees sda */
1704 taskq_wait(dmu_objset_pool(os
)->dp_sync_taskq
);
1706 list
= &DMU_META_DNODE(os
)->dn_dirty_records
[txgoff
];
1707 while ((dr
= list_head(list
)) != NULL
) {
1708 ASSERT0(dr
->dr_dbuf
->db_level
);
1709 list_remove(list
, dr
);
1711 zio_nowait(dr
->dr_zio
);
1714 /* Enable dnode backfill if enough objects have been freed. */
1715 if (os
->os_freed_dnodes
>= dmu_rescan_dnode_threshold
) {
1716 os
->os_rescan_dnodes
= B_TRUE
;
1717 os
->os_freed_dnodes
= 0;
1721 * Free intent log blocks up to this tx.
1723 zil_sync(os
->os_zil
, tx
);
1724 os
->os_phys
->os_zil_header
= os
->os_zil_header
;
1729 dmu_objset_is_dirty(objset_t
*os
, uint64_t txg
)
1731 return (!multilist_is_empty(os
->os_dirty_dnodes
[txg
& TXG_MASK
]));
1734 static objset_used_cb_t
*used_cbs
[DMU_OST_NUMTYPES
];
1737 dmu_objset_register_type(dmu_objset_type_t ost
, objset_used_cb_t
*cb
)
1743 dmu_objset_userused_enabled(objset_t
*os
)
1745 return (spa_version(os
->os_spa
) >= SPA_VERSION_USERSPACE
&&
1746 used_cbs
[os
->os_phys
->os_type
] != NULL
&&
1747 DMU_USERUSED_DNODE(os
) != NULL
);
1751 dmu_objset_userobjused_enabled(objset_t
*os
)
1753 return (dmu_objset_userused_enabled(os
) &&
1754 spa_feature_is_enabled(os
->os_spa
, SPA_FEATURE_USEROBJ_ACCOUNTING
));
1758 dmu_objset_projectquota_enabled(objset_t
*os
)
1760 return (used_cbs
[os
->os_phys
->os_type
] != NULL
&&
1761 DMU_PROJECTUSED_DNODE(os
) != NULL
&&
1762 spa_feature_is_enabled(os
->os_spa
, SPA_FEATURE_PROJECT_QUOTA
));
1765 typedef struct userquota_node
{
1766 /* must be in the first filed, see userquota_update_cache() */
1767 char uqn_id
[20 + DMU_OBJACCT_PREFIX_LEN
];
1769 avl_node_t uqn_node
;
1772 typedef struct userquota_cache
{
1773 avl_tree_t uqc_user_deltas
;
1774 avl_tree_t uqc_group_deltas
;
1775 avl_tree_t uqc_project_deltas
;
1776 } userquota_cache_t
;
1779 userquota_compare(const void *l
, const void *r
)
1781 const userquota_node_t
*luqn
= l
;
1782 const userquota_node_t
*ruqn
= r
;
1786 * NB: can only access uqn_id because userquota_update_cache() doesn't
1787 * pass in an entire userquota_node_t.
1789 rv
= strcmp(luqn
->uqn_id
, ruqn
->uqn_id
);
1791 return (AVL_ISIGN(rv
));
1795 do_userquota_cacheflush(objset_t
*os
, userquota_cache_t
*cache
, dmu_tx_t
*tx
)
1798 userquota_node_t
*uqn
;
1800 ASSERT(dmu_tx_is_syncing(tx
));
1803 while ((uqn
= avl_destroy_nodes(&cache
->uqc_user_deltas
,
1804 &cookie
)) != NULL
) {
1806 * os_userused_lock protects against concurrent calls to
1807 * zap_increment_int(). It's needed because zap_increment_int()
1808 * is not thread-safe (i.e. not atomic).
1810 mutex_enter(&os
->os_userused_lock
);
1811 VERIFY0(zap_increment(os
, DMU_USERUSED_OBJECT
,
1812 uqn
->uqn_id
, uqn
->uqn_delta
, tx
));
1813 mutex_exit(&os
->os_userused_lock
);
1814 kmem_free(uqn
, sizeof (*uqn
));
1816 avl_destroy(&cache
->uqc_user_deltas
);
1819 while ((uqn
= avl_destroy_nodes(&cache
->uqc_group_deltas
,
1820 &cookie
)) != NULL
) {
1821 mutex_enter(&os
->os_userused_lock
);
1822 VERIFY0(zap_increment(os
, DMU_GROUPUSED_OBJECT
,
1823 uqn
->uqn_id
, uqn
->uqn_delta
, tx
));
1824 mutex_exit(&os
->os_userused_lock
);
1825 kmem_free(uqn
, sizeof (*uqn
));
1827 avl_destroy(&cache
->uqc_group_deltas
);
1829 if (dmu_objset_projectquota_enabled(os
)) {
1831 while ((uqn
= avl_destroy_nodes(&cache
->uqc_project_deltas
,
1832 &cookie
)) != NULL
) {
1833 mutex_enter(&os
->os_userused_lock
);
1834 VERIFY0(zap_increment(os
, DMU_PROJECTUSED_OBJECT
,
1835 uqn
->uqn_id
, uqn
->uqn_delta
, tx
));
1836 mutex_exit(&os
->os_userused_lock
);
1837 kmem_free(uqn
, sizeof (*uqn
));
1839 avl_destroy(&cache
->uqc_project_deltas
);
1844 userquota_update_cache(avl_tree_t
*avl
, const char *id
, int64_t delta
)
1846 userquota_node_t
*uqn
;
1849 ASSERT(strlen(id
) < sizeof (uqn
->uqn_id
));
1851 * Use id directly for searching because uqn_id is the first field of
1852 * userquota_node_t and fields after uqn_id won't be accessed in
1855 uqn
= avl_find(avl
, (const void *)id
, &idx
);
1857 uqn
= kmem_zalloc(sizeof (*uqn
), KM_SLEEP
);
1858 strlcpy(uqn
->uqn_id
, id
, sizeof (uqn
->uqn_id
));
1859 avl_insert(avl
, uqn
, idx
);
1861 uqn
->uqn_delta
+= delta
;
1865 do_userquota_update(objset_t
*os
, userquota_cache_t
*cache
, uint64_t used
,
1866 uint64_t flags
, uint64_t user
, uint64_t group
, uint64_t project
,
1869 if (flags
& DNODE_FLAG_USERUSED_ACCOUNTED
) {
1870 int64_t delta
= DNODE_MIN_SIZE
+ used
;
1876 (void) sprintf(name
, "%llx", (longlong_t
)user
);
1877 userquota_update_cache(&cache
->uqc_user_deltas
, name
, delta
);
1879 (void) sprintf(name
, "%llx", (longlong_t
)group
);
1880 userquota_update_cache(&cache
->uqc_group_deltas
, name
, delta
);
1882 if (dmu_objset_projectquota_enabled(os
)) {
1883 (void) sprintf(name
, "%llx", (longlong_t
)project
);
1884 userquota_update_cache(&cache
->uqc_project_deltas
,
1891 do_userobjquota_update(objset_t
*os
, userquota_cache_t
*cache
, uint64_t flags
,
1892 uint64_t user
, uint64_t group
, uint64_t project
, boolean_t subtract
)
1894 if (flags
& DNODE_FLAG_USEROBJUSED_ACCOUNTED
) {
1895 char name
[20 + DMU_OBJACCT_PREFIX_LEN
];
1896 int delta
= subtract
? -1 : 1;
1898 (void) snprintf(name
, sizeof (name
), DMU_OBJACCT_PREFIX
"%llx",
1900 userquota_update_cache(&cache
->uqc_user_deltas
, name
, delta
);
1902 (void) snprintf(name
, sizeof (name
), DMU_OBJACCT_PREFIX
"%llx",
1904 userquota_update_cache(&cache
->uqc_group_deltas
, name
, delta
);
1906 if (dmu_objset_projectquota_enabled(os
)) {
1907 (void) snprintf(name
, sizeof (name
),
1908 DMU_OBJACCT_PREFIX
"%llx", (longlong_t
)project
);
1909 userquota_update_cache(&cache
->uqc_project_deltas
,
1915 typedef struct userquota_updates_arg
{
1917 int uua_sublist_idx
;
1919 } userquota_updates_arg_t
;
1922 userquota_updates_task(void *arg
)
1924 userquota_updates_arg_t
*uua
= arg
;
1925 objset_t
*os
= uua
->uua_os
;
1926 dmu_tx_t
*tx
= uua
->uua_tx
;
1928 userquota_cache_t cache
= { { 0 } };
1930 multilist_sublist_t
*list
=
1931 multilist_sublist_lock(os
->os_synced_dnodes
, uua
->uua_sublist_idx
);
1933 ASSERT(multilist_sublist_head(list
) == NULL
||
1934 dmu_objset_userused_enabled(os
));
1935 avl_create(&cache
.uqc_user_deltas
, userquota_compare
,
1936 sizeof (userquota_node_t
), offsetof(userquota_node_t
, uqn_node
));
1937 avl_create(&cache
.uqc_group_deltas
, userquota_compare
,
1938 sizeof (userquota_node_t
), offsetof(userquota_node_t
, uqn_node
));
1939 if (dmu_objset_projectquota_enabled(os
))
1940 avl_create(&cache
.uqc_project_deltas
, userquota_compare
,
1941 sizeof (userquota_node_t
), offsetof(userquota_node_t
,
1944 while ((dn
= multilist_sublist_head(list
)) != NULL
) {
1946 ASSERT(!DMU_OBJECT_IS_SPECIAL(dn
->dn_object
));
1947 ASSERT(dn
->dn_phys
->dn_type
== DMU_OT_NONE
||
1948 dn
->dn_phys
->dn_flags
&
1949 DNODE_FLAG_USERUSED_ACCOUNTED
);
1951 flags
= dn
->dn_id_flags
;
1953 if (flags
& DN_ID_OLD_EXIST
) {
1954 do_userquota_update(os
, &cache
, dn
->dn_oldused
,
1955 dn
->dn_oldflags
, dn
->dn_olduid
, dn
->dn_oldgid
,
1956 dn
->dn_oldprojid
, B_TRUE
);
1957 do_userobjquota_update(os
, &cache
, dn
->dn_oldflags
,
1958 dn
->dn_olduid
, dn
->dn_oldgid
,
1959 dn
->dn_oldprojid
, B_TRUE
);
1961 if (flags
& DN_ID_NEW_EXIST
) {
1962 do_userquota_update(os
, &cache
,
1963 DN_USED_BYTES(dn
->dn_phys
), dn
->dn_phys
->dn_flags
,
1964 dn
->dn_newuid
, dn
->dn_newgid
,
1965 dn
->dn_newprojid
, B_FALSE
);
1966 do_userobjquota_update(os
, &cache
,
1967 dn
->dn_phys
->dn_flags
, dn
->dn_newuid
, dn
->dn_newgid
,
1968 dn
->dn_newprojid
, B_FALSE
);
1971 mutex_enter(&dn
->dn_mtx
);
1973 dn
->dn_oldflags
= 0;
1974 if (dn
->dn_id_flags
& DN_ID_NEW_EXIST
) {
1975 dn
->dn_olduid
= dn
->dn_newuid
;
1976 dn
->dn_oldgid
= dn
->dn_newgid
;
1977 dn
->dn_oldprojid
= dn
->dn_newprojid
;
1978 dn
->dn_id_flags
|= DN_ID_OLD_EXIST
;
1979 if (dn
->dn_bonuslen
== 0)
1980 dn
->dn_id_flags
|= DN_ID_CHKED_SPILL
;
1982 dn
->dn_id_flags
|= DN_ID_CHKED_BONUS
;
1984 dn
->dn_id_flags
&= ~(DN_ID_NEW_EXIST
);
1985 if (dn
->dn_dirty_txg
== spa_syncing_txg(os
->os_spa
))
1986 dn
->dn_dirty_txg
= 0;
1987 mutex_exit(&dn
->dn_mtx
);
1989 multilist_sublist_remove(list
, dn
);
1990 dnode_rele(dn
, os
->os_synced_dnodes
);
1992 do_userquota_cacheflush(os
, &cache
, tx
);
1993 multilist_sublist_unlock(list
);
1994 kmem_free(uua
, sizeof (*uua
));
1998 dmu_objset_do_userquota_updates(objset_t
*os
, dmu_tx_t
*tx
)
2002 if (!dmu_objset_userused_enabled(os
))
2006 * If this is a raw receive just return and handle accounting
2007 * later when we have the keys loaded. We also don't do user
2008 * accounting during claiming since the datasets are not owned
2009 * for the duration of claiming and this txg should only be
2010 * used for recovery.
2012 if (os
->os_encrypted
&& dmu_objset_is_receiving(os
))
2015 if (tx
->tx_txg
<= os
->os_spa
->spa_claim_max_txg
)
2018 /* Allocate the user/group/project used objects if necessary. */
2019 if (DMU_USERUSED_DNODE(os
)->dn_type
== DMU_OT_NONE
) {
2020 VERIFY0(zap_create_claim(os
,
2021 DMU_USERUSED_OBJECT
,
2022 DMU_OT_USERGROUP_USED
, DMU_OT_NONE
, 0, tx
));
2023 VERIFY0(zap_create_claim(os
,
2024 DMU_GROUPUSED_OBJECT
,
2025 DMU_OT_USERGROUP_USED
, DMU_OT_NONE
, 0, tx
));
2028 if (dmu_objset_projectquota_enabled(os
) &&
2029 DMU_PROJECTUSED_DNODE(os
)->dn_type
== DMU_OT_NONE
) {
2030 VERIFY0(zap_create_claim(os
, DMU_PROJECTUSED_OBJECT
,
2031 DMU_OT_USERGROUP_USED
, DMU_OT_NONE
, 0, tx
));
2034 num_sublists
= multilist_get_num_sublists(os
->os_synced_dnodes
);
2035 for (int i
= 0; i
< num_sublists
; i
++) {
2036 if (multilist_sublist_is_empty_idx(os
->os_synced_dnodes
, i
))
2038 userquota_updates_arg_t
*uua
=
2039 kmem_alloc(sizeof (*uua
), KM_SLEEP
);
2041 uua
->uua_sublist_idx
= i
;
2043 /* note: caller does taskq_wait() */
2044 (void) taskq_dispatch(dmu_objset_pool(os
)->dp_sync_taskq
,
2045 userquota_updates_task
, uua
, 0);
2046 /* callback frees uua */
2051 * Returns a pointer to data to find uid/gid from
2053 * If a dirty record for transaction group that is syncing can't
2054 * be found then NULL is returned. In the NULL case it is assumed
2055 * the uid/gid aren't changing.
2058 dmu_objset_userquota_find_data(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
2060 dbuf_dirty_record_t
*dr
, **drp
;
2063 if (db
->db_dirtycnt
== 0)
2064 return (db
->db
.db_data
); /* Nothing is changing */
2066 for (drp
= &db
->db_last_dirty
; (dr
= *drp
) != NULL
; drp
= &dr
->dr_next
)
2067 if (dr
->dr_txg
== tx
->tx_txg
)
2075 DB_DNODE_ENTER(dr
->dr_dbuf
);
2076 dn
= DB_DNODE(dr
->dr_dbuf
);
2078 if (dn
->dn_bonuslen
== 0 &&
2079 dr
->dr_dbuf
->db_blkid
== DMU_SPILL_BLKID
)
2080 data
= dr
->dt
.dl
.dr_data
->b_data
;
2082 data
= dr
->dt
.dl
.dr_data
;
2084 DB_DNODE_EXIT(dr
->dr_dbuf
);
2091 dmu_objset_userquota_get_ids(dnode_t
*dn
, boolean_t before
, dmu_tx_t
*tx
)
2093 objset_t
*os
= dn
->dn_objset
;
2095 dmu_buf_impl_t
*db
= NULL
;
2096 uint64_t *user
= NULL
;
2097 uint64_t *group
= NULL
;
2098 uint64_t *project
= NULL
;
2099 int flags
= dn
->dn_id_flags
;
2101 boolean_t have_spill
= B_FALSE
;
2103 if (!dmu_objset_userused_enabled(dn
->dn_objset
))
2107 * Raw receives introduce a problem with user accounting. Raw
2108 * receives cannot update the user accounting info because the
2109 * user ids and the sizes are encrypted. To guarantee that we
2110 * never end up with bad user accounting, we simply disable it
2111 * during raw receives. We also disable this for normal receives
2112 * so that an incremental raw receive may be done on top of an
2113 * existing non-raw receive.
2115 if (os
->os_encrypted
&& dmu_objset_is_receiving(os
))
2118 if (before
&& (flags
& (DN_ID_CHKED_BONUS
|DN_ID_OLD_EXIST
|
2119 DN_ID_CHKED_SPILL
)))
2122 if (before
&& dn
->dn_bonuslen
!= 0)
2123 data
= DN_BONUS(dn
->dn_phys
);
2124 else if (!before
&& dn
->dn_bonuslen
!= 0) {
2127 mutex_enter(&db
->db_mtx
);
2128 data
= dmu_objset_userquota_find_data(db
, tx
);
2130 data
= DN_BONUS(dn
->dn_phys
);
2132 } else if (dn
->dn_bonuslen
== 0 && dn
->dn_bonustype
== DMU_OT_SA
) {
2135 if (RW_WRITE_HELD(&dn
->dn_struct_rwlock
))
2136 rf
|= DB_RF_HAVESTRUCT
;
2137 error
= dmu_spill_hold_by_dnode(dn
,
2138 rf
| DB_RF_MUST_SUCCEED
,
2139 FTAG
, (dmu_buf_t
**)&db
);
2141 mutex_enter(&db
->db_mtx
);
2142 data
= (before
) ? db
->db
.db_data
:
2143 dmu_objset_userquota_find_data(db
, tx
);
2144 have_spill
= B_TRUE
;
2146 mutex_enter(&dn
->dn_mtx
);
2147 dn
->dn_id_flags
|= DN_ID_CHKED_BONUS
;
2148 mutex_exit(&dn
->dn_mtx
);
2154 user
= &dn
->dn_olduid
;
2155 group
= &dn
->dn_oldgid
;
2156 project
= &dn
->dn_oldprojid
;
2158 user
= &dn
->dn_newuid
;
2159 group
= &dn
->dn_newgid
;
2160 project
= &dn
->dn_newprojid
;
2164 * Must always call the callback in case the object
2165 * type has changed and that type isn't an object type to track
2167 error
= used_cbs
[os
->os_phys
->os_type
](dn
->dn_bonustype
, data
,
2168 user
, group
, project
);
2171 * Preserve existing uid/gid when the callback can't determine
2172 * what the new uid/gid are and the callback returned EEXIST.
2173 * The EEXIST error tells us to just use the existing uid/gid.
2174 * If we don't know what the old values are then just assign
2175 * them to 0, since that is a new file being created.
2177 if (!before
&& data
== NULL
&& error
== EEXIST
) {
2178 if (flags
& DN_ID_OLD_EXIST
) {
2179 dn
->dn_newuid
= dn
->dn_olduid
;
2180 dn
->dn_newgid
= dn
->dn_oldgid
;
2181 dn
->dn_newprojid
= dn
->dn_oldprojid
;
2185 dn
->dn_newprojid
= ZFS_DEFAULT_PROJID
;
2191 mutex_exit(&db
->db_mtx
);
2193 mutex_enter(&dn
->dn_mtx
);
2194 if (error
== 0 && before
)
2195 dn
->dn_id_flags
|= DN_ID_OLD_EXIST
;
2196 if (error
== 0 && !before
)
2197 dn
->dn_id_flags
|= DN_ID_NEW_EXIST
;
2200 dn
->dn_id_flags
|= DN_ID_CHKED_SPILL
;
2202 dn
->dn_id_flags
|= DN_ID_CHKED_BONUS
;
2204 mutex_exit(&dn
->dn_mtx
);
2206 dmu_buf_rele((dmu_buf_t
*)db
, FTAG
);
2210 dmu_objset_userspace_present(objset_t
*os
)
2212 return (os
->os_phys
->os_flags
&
2213 OBJSET_FLAG_USERACCOUNTING_COMPLETE
);
2217 dmu_objset_userobjspace_present(objset_t
*os
)
2219 return (os
->os_phys
->os_flags
&
2220 OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE
);
2224 dmu_objset_projectquota_present(objset_t
*os
)
2226 return (os
->os_phys
->os_flags
&
2227 OBJSET_FLAG_PROJECTQUOTA_COMPLETE
);
2231 dmu_objset_space_upgrade(objset_t
*os
)
2237 * We simply need to mark every object dirty, so that it will be
2238 * synced out and now accounted. If this is called
2239 * concurrently, or if we already did some work before crashing,
2240 * that's fine, since we track each object's accounted state
2244 for (obj
= 0; err
== 0; err
= dmu_object_next(os
, &obj
, FALSE
, 0)) {
2249 mutex_enter(&os
->os_upgrade_lock
);
2250 if (os
->os_upgrade_exit
)
2251 err
= SET_ERROR(EINTR
);
2252 mutex_exit(&os
->os_upgrade_lock
);
2256 if (issig(JUSTLOOKING
) && issig(FORREAL
))
2257 return (SET_ERROR(EINTR
));
2259 objerr
= dmu_bonus_hold(os
, obj
, FTAG
, &db
);
2262 tx
= dmu_tx_create(os
);
2263 dmu_tx_hold_bonus(tx
, obj
);
2264 objerr
= dmu_tx_assign(tx
, TXG_WAIT
);
2266 dmu_buf_rele(db
, FTAG
);
2270 dmu_buf_will_dirty(db
, tx
);
2271 dmu_buf_rele(db
, FTAG
);
2278 dmu_objset_userspace_upgrade(objset_t
*os
)
2282 if (dmu_objset_userspace_present(os
))
2284 if (dmu_objset_is_snapshot(os
))
2285 return (SET_ERROR(EINVAL
));
2286 if (!dmu_objset_userused_enabled(os
))
2287 return (SET_ERROR(ENOTSUP
));
2289 err
= dmu_objset_space_upgrade(os
);
2293 os
->os_flags
|= OBJSET_FLAG_USERACCOUNTING_COMPLETE
;
2294 txg_wait_synced(dmu_objset_pool(os
), 0);
2299 dmu_objset_id_quota_upgrade_cb(objset_t
*os
)
2303 if (dmu_objset_userobjspace_present(os
) &&
2304 dmu_objset_projectquota_present(os
))
2306 if (dmu_objset_is_snapshot(os
))
2307 return (SET_ERROR(EINVAL
));
2308 if (!dmu_objset_userobjused_enabled(os
))
2309 return (SET_ERROR(ENOTSUP
));
2310 if (!dmu_objset_projectquota_enabled(os
) &&
2311 dmu_objset_userobjspace_present(os
))
2312 return (SET_ERROR(ENOTSUP
));
2314 dmu_objset_ds(os
)->ds_feature_activation
[
2315 SPA_FEATURE_USEROBJ_ACCOUNTING
] = (void *)B_TRUE
;
2316 if (dmu_objset_projectquota_enabled(os
))
2317 dmu_objset_ds(os
)->ds_feature_activation
[
2318 SPA_FEATURE_PROJECT_QUOTA
] = (void *)B_TRUE
;
2320 err
= dmu_objset_space_upgrade(os
);
2324 os
->os_flags
|= OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE
;
2325 if (dmu_objset_projectquota_enabled(os
))
2326 os
->os_flags
|= OBJSET_FLAG_PROJECTQUOTA_COMPLETE
;
2328 txg_wait_synced(dmu_objset_pool(os
), 0);
2333 dmu_objset_id_quota_upgrade(objset_t
*os
)
2335 dmu_objset_upgrade(os
, dmu_objset_id_quota_upgrade_cb
);
2339 dmu_objset_userobjspace_upgradable(objset_t
*os
)
2341 return (dmu_objset_type(os
) == DMU_OST_ZFS
&&
2342 !dmu_objset_is_snapshot(os
) &&
2343 dmu_objset_userobjused_enabled(os
) &&
2344 !dmu_objset_userobjspace_present(os
) &&
2345 spa_writeable(dmu_objset_spa(os
)));
2349 dmu_objset_projectquota_upgradable(objset_t
*os
)
2351 return (dmu_objset_type(os
) == DMU_OST_ZFS
&&
2352 !dmu_objset_is_snapshot(os
) &&
2353 dmu_objset_projectquota_enabled(os
) &&
2354 !dmu_objset_projectquota_present(os
) &&
2355 spa_writeable(dmu_objset_spa(os
)));
2359 dmu_objset_space(objset_t
*os
, uint64_t *refdbytesp
, uint64_t *availbytesp
,
2360 uint64_t *usedobjsp
, uint64_t *availobjsp
)
2362 dsl_dataset_space(os
->os_dsl_dataset
, refdbytesp
, availbytesp
,
2363 usedobjsp
, availobjsp
);
2367 dmu_objset_fsid_guid(objset_t
*os
)
2369 return (dsl_dataset_fsid_guid(os
->os_dsl_dataset
));
2373 dmu_objset_fast_stat(objset_t
*os
, dmu_objset_stats_t
*stat
)
2375 stat
->dds_type
= os
->os_phys
->os_type
;
2376 if (os
->os_dsl_dataset
)
2377 dsl_dataset_fast_stat(os
->os_dsl_dataset
, stat
);
2381 dmu_objset_stats(objset_t
*os
, nvlist_t
*nv
)
2383 ASSERT(os
->os_dsl_dataset
||
2384 os
->os_phys
->os_type
== DMU_OST_META
);
2386 if (os
->os_dsl_dataset
!= NULL
)
2387 dsl_dataset_stats(os
->os_dsl_dataset
, nv
);
2389 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_TYPE
,
2390 os
->os_phys
->os_type
);
2391 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_USERACCOUNTING
,
2392 dmu_objset_userspace_present(os
));
2396 dmu_objset_is_snapshot(objset_t
*os
)
2398 if (os
->os_dsl_dataset
!= NULL
)
2399 return (os
->os_dsl_dataset
->ds_is_snapshot
);
2405 dmu_snapshot_realname(objset_t
*os
, char *name
, char *real
, int maxlen
,
2406 boolean_t
*conflict
)
2408 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
2411 if (dsl_dataset_phys(ds
)->ds_snapnames_zapobj
== 0)
2412 return (SET_ERROR(ENOENT
));
2414 return (zap_lookup_norm(ds
->ds_dir
->dd_pool
->dp_meta_objset
,
2415 dsl_dataset_phys(ds
)->ds_snapnames_zapobj
, name
, 8, 1, &ignored
,
2416 MT_NORMALIZE
, real
, maxlen
, conflict
));
2420 dmu_snapshot_list_next(objset_t
*os
, int namelen
, char *name
,
2421 uint64_t *idp
, uint64_t *offp
, boolean_t
*case_conflict
)
2423 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
2424 zap_cursor_t cursor
;
2425 zap_attribute_t attr
;
2427 ASSERT(dsl_pool_config_held(dmu_objset_pool(os
)));
2429 if (dsl_dataset_phys(ds
)->ds_snapnames_zapobj
== 0)
2430 return (SET_ERROR(ENOENT
));
2432 zap_cursor_init_serialized(&cursor
,
2433 ds
->ds_dir
->dd_pool
->dp_meta_objset
,
2434 dsl_dataset_phys(ds
)->ds_snapnames_zapobj
, *offp
);
2436 if (zap_cursor_retrieve(&cursor
, &attr
) != 0) {
2437 zap_cursor_fini(&cursor
);
2438 return (SET_ERROR(ENOENT
));
2441 if (strlen(attr
.za_name
) + 1 > namelen
) {
2442 zap_cursor_fini(&cursor
);
2443 return (SET_ERROR(ENAMETOOLONG
));
2446 (void) strcpy(name
, attr
.za_name
);
2448 *idp
= attr
.za_first_integer
;
2450 *case_conflict
= attr
.za_normalization_conflict
;
2451 zap_cursor_advance(&cursor
);
2452 *offp
= zap_cursor_serialize(&cursor
);
2453 zap_cursor_fini(&cursor
);
2459 dmu_snapshot_lookup(objset_t
*os
, const char *name
, uint64_t *value
)
2461 return (dsl_dataset_snap_lookup(os
->os_dsl_dataset
, name
, value
));
2465 dmu_dir_list_next(objset_t
*os
, int namelen
, char *name
,
2466 uint64_t *idp
, uint64_t *offp
)
2468 dsl_dir_t
*dd
= os
->os_dsl_dataset
->ds_dir
;
2469 zap_cursor_t cursor
;
2470 zap_attribute_t attr
;
2472 /* there is no next dir on a snapshot! */
2473 if (os
->os_dsl_dataset
->ds_object
!=
2474 dsl_dir_phys(dd
)->dd_head_dataset_obj
)
2475 return (SET_ERROR(ENOENT
));
2477 zap_cursor_init_serialized(&cursor
,
2478 dd
->dd_pool
->dp_meta_objset
,
2479 dsl_dir_phys(dd
)->dd_child_dir_zapobj
, *offp
);
2481 if (zap_cursor_retrieve(&cursor
, &attr
) != 0) {
2482 zap_cursor_fini(&cursor
);
2483 return (SET_ERROR(ENOENT
));
2486 if (strlen(attr
.za_name
) + 1 > namelen
) {
2487 zap_cursor_fini(&cursor
);
2488 return (SET_ERROR(ENAMETOOLONG
));
2491 (void) strcpy(name
, attr
.za_name
);
2493 *idp
= attr
.za_first_integer
;
2494 zap_cursor_advance(&cursor
);
2495 *offp
= zap_cursor_serialize(&cursor
);
2496 zap_cursor_fini(&cursor
);
2501 typedef struct dmu_objset_find_ctx
{
2505 char *dc_ddname
; /* last component of ddobj's name */
2506 int (*dc_func
)(dsl_pool_t
*, dsl_dataset_t
*, void *);
2509 kmutex_t
*dc_error_lock
;
2511 } dmu_objset_find_ctx_t
;
2514 dmu_objset_find_dp_impl(dmu_objset_find_ctx_t
*dcp
)
2516 dsl_pool_t
*dp
= dcp
->dc_dp
;
2520 zap_attribute_t
*attr
;
2524 /* don't process if there already was an error */
2525 if (*dcp
->dc_error
!= 0)
2529 * Note: passing the name (dc_ddname) here is optional, but it
2530 * improves performance because we don't need to call
2531 * zap_value_search() to determine the name.
2533 err
= dsl_dir_hold_obj(dp
, dcp
->dc_ddobj
, dcp
->dc_ddname
, FTAG
, &dd
);
2537 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
2538 if (dd
->dd_myname
[0] == '$') {
2539 dsl_dir_rele(dd
, FTAG
);
2543 thisobj
= dsl_dir_phys(dd
)->dd_head_dataset_obj
;
2544 attr
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
2547 * Iterate over all children.
2549 if (dcp
->dc_flags
& DS_FIND_CHILDREN
) {
2550 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
,
2551 dsl_dir_phys(dd
)->dd_child_dir_zapobj
);
2552 zap_cursor_retrieve(&zc
, attr
) == 0;
2553 (void) zap_cursor_advance(&zc
)) {
2554 ASSERT3U(attr
->za_integer_length
, ==,
2556 ASSERT3U(attr
->za_num_integers
, ==, 1);
2558 dmu_objset_find_ctx_t
*child_dcp
=
2559 kmem_alloc(sizeof (*child_dcp
), KM_SLEEP
);
2561 child_dcp
->dc_ddobj
= attr
->za_first_integer
;
2562 child_dcp
->dc_ddname
= spa_strdup(attr
->za_name
);
2563 if (dcp
->dc_tq
!= NULL
)
2564 (void) taskq_dispatch(dcp
->dc_tq
,
2565 dmu_objset_find_dp_cb
, child_dcp
, TQ_SLEEP
);
2567 dmu_objset_find_dp_impl(child_dcp
);
2569 zap_cursor_fini(&zc
);
2573 * Iterate over all snapshots.
2575 if (dcp
->dc_flags
& DS_FIND_SNAPSHOTS
) {
2577 err
= dsl_dataset_hold_obj(dp
, thisobj
, FTAG
, &ds
);
2582 snapobj
= dsl_dataset_phys(ds
)->ds_snapnames_zapobj
;
2583 dsl_dataset_rele(ds
, FTAG
);
2585 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
, snapobj
);
2586 zap_cursor_retrieve(&zc
, attr
) == 0;
2587 (void) zap_cursor_advance(&zc
)) {
2588 ASSERT3U(attr
->za_integer_length
, ==,
2590 ASSERT3U(attr
->za_num_integers
, ==, 1);
2592 err
= dsl_dataset_hold_obj(dp
,
2593 attr
->za_first_integer
, FTAG
, &ds
);
2596 err
= dcp
->dc_func(dp
, ds
, dcp
->dc_arg
);
2597 dsl_dataset_rele(ds
, FTAG
);
2601 zap_cursor_fini(&zc
);
2605 kmem_free(attr
, sizeof (zap_attribute_t
));
2608 dsl_dir_rele(dd
, FTAG
);
2615 err
= dsl_dataset_hold_obj(dp
, thisobj
, FTAG
, &ds
);
2618 * Note: we hold the dir while calling dsl_dataset_hold_obj() so
2619 * that the dir will remain cached, and we won't have to re-instantiate
2620 * it (which could be expensive due to finding its name via
2621 * zap_value_search()).
2623 dsl_dir_rele(dd
, FTAG
);
2626 err
= dcp
->dc_func(dp
, ds
, dcp
->dc_arg
);
2627 dsl_dataset_rele(ds
, FTAG
);
2631 mutex_enter(dcp
->dc_error_lock
);
2632 /* only keep first error */
2633 if (*dcp
->dc_error
== 0)
2634 *dcp
->dc_error
= err
;
2635 mutex_exit(dcp
->dc_error_lock
);
2638 if (dcp
->dc_ddname
!= NULL
)
2639 spa_strfree(dcp
->dc_ddname
);
2640 kmem_free(dcp
, sizeof (*dcp
));
2644 dmu_objset_find_dp_cb(void *arg
)
2646 dmu_objset_find_ctx_t
*dcp
= arg
;
2647 dsl_pool_t
*dp
= dcp
->dc_dp
;
2650 * We need to get a pool_config_lock here, as there are several
2651 * assert(pool_config_held) down the stack. Getting a lock via
2652 * dsl_pool_config_enter is risky, as it might be stalled by a
2653 * pending writer. This would deadlock, as the write lock can
2654 * only be granted when our parent thread gives up the lock.
2655 * The _prio interface gives us priority over a pending writer.
2657 dsl_pool_config_enter_prio(dp
, FTAG
);
2659 dmu_objset_find_dp_impl(dcp
);
2661 dsl_pool_config_exit(dp
, FTAG
);
2665 * Find objsets under and including ddobj, call func(ds) on each.
2666 * The order for the enumeration is completely undefined.
2667 * func is called with dsl_pool_config held.
2670 dmu_objset_find_dp(dsl_pool_t
*dp
, uint64_t ddobj
,
2671 int func(dsl_pool_t
*, dsl_dataset_t
*, void *), void *arg
, int flags
)
2676 dmu_objset_find_ctx_t
*dcp
;
2679 mutex_init(&err_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
2680 dcp
= kmem_alloc(sizeof (*dcp
), KM_SLEEP
);
2683 dcp
->dc_ddobj
= ddobj
;
2684 dcp
->dc_ddname
= NULL
;
2685 dcp
->dc_func
= func
;
2687 dcp
->dc_flags
= flags
;
2688 dcp
->dc_error_lock
= &err_lock
;
2689 dcp
->dc_error
= &error
;
2691 if ((flags
& DS_FIND_SERIALIZE
) || dsl_pool_config_held_writer(dp
)) {
2693 * In case a write lock is held we can't make use of
2694 * parallelism, as down the stack of the worker threads
2695 * the lock is asserted via dsl_pool_config_held.
2696 * In case of a read lock this is solved by getting a read
2697 * lock in each worker thread, which isn't possible in case
2698 * of a writer lock. So we fall back to the synchronous path
2700 * In the future it might be possible to get some magic into
2701 * dsl_pool_config_held in a way that it returns true for
2702 * the worker threads so that a single lock held from this
2703 * thread suffices. For now, stay single threaded.
2705 dmu_objset_find_dp_impl(dcp
);
2706 mutex_destroy(&err_lock
);
2711 ntasks
= dmu_find_threads
;
2713 ntasks
= vdev_count_leaves(dp
->dp_spa
) * 4;
2714 tq
= taskq_create("dmu_objset_find", ntasks
, maxclsyspri
, ntasks
,
2717 kmem_free(dcp
, sizeof (*dcp
));
2718 mutex_destroy(&err_lock
);
2720 return (SET_ERROR(ENOMEM
));
2724 /* dcp will be freed by task */
2725 (void) taskq_dispatch(tq
, dmu_objset_find_dp_cb
, dcp
, TQ_SLEEP
);
2728 * PORTING: this code relies on the property of taskq_wait to wait
2729 * until no more tasks are queued and no more tasks are active. As
2730 * we always queue new tasks from within other tasks, task_wait
2731 * reliably waits for the full recursion to finish, even though we
2732 * enqueue new tasks after taskq_wait has been called.
2733 * On platforms other than illumos, taskq_wait may not have this
2738 mutex_destroy(&err_lock
);
2744 * Find all objsets under name, and for each, call 'func(child_name, arg)'.
2745 * The dp_config_rwlock must not be held when this is called, and it
2746 * will not be held when the callback is called.
2747 * Therefore this function should only be used when the pool is not changing
2748 * (e.g. in syncing context), or the callback can deal with the possible races.
2751 dmu_objset_find_impl(spa_t
*spa
, const char *name
,
2752 int func(const char *, void *), void *arg
, int flags
)
2755 dsl_pool_t
*dp
= spa_get_dsl(spa
);
2758 zap_attribute_t
*attr
;
2763 dsl_pool_config_enter(dp
, FTAG
);
2765 err
= dsl_dir_hold(dp
, name
, FTAG
, &dd
, NULL
);
2767 dsl_pool_config_exit(dp
, FTAG
);
2771 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
2772 if (dd
->dd_myname
[0] == '$') {
2773 dsl_dir_rele(dd
, FTAG
);
2774 dsl_pool_config_exit(dp
, FTAG
);
2778 thisobj
= dsl_dir_phys(dd
)->dd_head_dataset_obj
;
2779 attr
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
2782 * Iterate over all children.
2784 if (flags
& DS_FIND_CHILDREN
) {
2785 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
,
2786 dsl_dir_phys(dd
)->dd_child_dir_zapobj
);
2787 zap_cursor_retrieve(&zc
, attr
) == 0;
2788 (void) zap_cursor_advance(&zc
)) {
2789 ASSERT3U(attr
->za_integer_length
, ==,
2791 ASSERT3U(attr
->za_num_integers
, ==, 1);
2793 child
= kmem_asprintf("%s/%s", name
, attr
->za_name
);
2794 dsl_pool_config_exit(dp
, FTAG
);
2795 err
= dmu_objset_find_impl(spa
, child
,
2797 dsl_pool_config_enter(dp
, FTAG
);
2802 zap_cursor_fini(&zc
);
2805 dsl_dir_rele(dd
, FTAG
);
2806 dsl_pool_config_exit(dp
, FTAG
);
2807 kmem_free(attr
, sizeof (zap_attribute_t
));
2813 * Iterate over all snapshots.
2815 if (flags
& DS_FIND_SNAPSHOTS
) {
2816 err
= dsl_dataset_hold_obj(dp
, thisobj
, FTAG
, &ds
);
2821 snapobj
= dsl_dataset_phys(ds
)->ds_snapnames_zapobj
;
2822 dsl_dataset_rele(ds
, FTAG
);
2824 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
, snapobj
);
2825 zap_cursor_retrieve(&zc
, attr
) == 0;
2826 (void) zap_cursor_advance(&zc
)) {
2827 ASSERT3U(attr
->za_integer_length
, ==,
2829 ASSERT3U(attr
->za_num_integers
, ==, 1);
2831 child
= kmem_asprintf("%s@%s",
2832 name
, attr
->za_name
);
2833 dsl_pool_config_exit(dp
, FTAG
);
2834 err
= func(child
, arg
);
2835 dsl_pool_config_enter(dp
, FTAG
);
2840 zap_cursor_fini(&zc
);
2844 dsl_dir_rele(dd
, FTAG
);
2845 kmem_free(attr
, sizeof (zap_attribute_t
));
2846 dsl_pool_config_exit(dp
, FTAG
);
2851 /* Apply to self. */
2852 return (func(name
, arg
));
2856 * See comment above dmu_objset_find_impl().
2859 dmu_objset_find(char *name
, int func(const char *, void *), void *arg
,
2865 error
= spa_open(name
, &spa
, FTAG
);
2868 error
= dmu_objset_find_impl(spa
, name
, func
, arg
, flags
);
2869 spa_close(spa
, FTAG
);
2874 dmu_objset_incompatible_encryption_version(objset_t
*os
)
2876 return (dsl_dir_incompatible_encryption_version(
2877 os
->os_dsl_dataset
->ds_dir
));
2881 dmu_objset_set_user(objset_t
*os
, void *user_ptr
)
2883 ASSERT(MUTEX_HELD(&os
->os_user_ptr_lock
));
2884 os
->os_user_ptr
= user_ptr
;
2888 dmu_objset_get_user(objset_t
*os
)
2890 ASSERT(MUTEX_HELD(&os
->os_user_ptr_lock
));
2891 return (os
->os_user_ptr
);
2895 * Determine name of filesystem, given name of snapshot.
2896 * buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes
2899 dmu_fsname(const char *snapname
, char *buf
)
2901 char *atp
= strchr(snapname
, '@');
2903 return (SET_ERROR(EINVAL
));
2904 if (atp
- snapname
>= ZFS_MAX_DATASET_NAME_LEN
)
2905 return (SET_ERROR(ENAMETOOLONG
));
2906 (void) strlcpy(buf
, snapname
, atp
- snapname
+ 1);
2911 * Call when we think we're going to write/free space in open context
2912 * to track the amount of dirty data in the open txg, which is also the
2913 * amount of memory that can not be evicted until this txg syncs.
2915 * Note that there are two conditions where this can be called from
2918 * [1] When we just created the dataset, in which case we go on with
2919 * updating any accounting of dirty data as usual.
2920 * [2] When we are dirtying MOS data, in which case we only update the
2921 * pool's accounting of dirty data.
2924 dmu_objset_willuse_space(objset_t
*os
, int64_t space
, dmu_tx_t
*tx
)
2926 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
2927 int64_t aspace
= spa_get_worst_case_asize(os
->os_spa
, space
);
2930 dsl_dir_willuse_space(ds
->ds_dir
, aspace
, tx
);
2933 dsl_pool_dirty_space(dmu_tx_pool(tx
), space
, tx
);
2936 #if defined(_KERNEL)
2937 EXPORT_SYMBOL(dmu_objset_zil
);
2938 EXPORT_SYMBOL(dmu_objset_pool
);
2939 EXPORT_SYMBOL(dmu_objset_ds
);
2940 EXPORT_SYMBOL(dmu_objset_type
);
2941 EXPORT_SYMBOL(dmu_objset_name
);
2942 EXPORT_SYMBOL(dmu_objset_hold
);
2943 EXPORT_SYMBOL(dmu_objset_hold_flags
);
2944 EXPORT_SYMBOL(dmu_objset_own
);
2945 EXPORT_SYMBOL(dmu_objset_rele
);
2946 EXPORT_SYMBOL(dmu_objset_rele_flags
);
2947 EXPORT_SYMBOL(dmu_objset_disown
);
2948 EXPORT_SYMBOL(dmu_objset_from_ds
);
2949 EXPORT_SYMBOL(dmu_objset_create
);
2950 EXPORT_SYMBOL(dmu_objset_clone
);
2951 EXPORT_SYMBOL(dmu_objset_stats
);
2952 EXPORT_SYMBOL(dmu_objset_fast_stat
);
2953 EXPORT_SYMBOL(dmu_objset_spa
);
2954 EXPORT_SYMBOL(dmu_objset_space
);
2955 EXPORT_SYMBOL(dmu_objset_fsid_guid
);
2956 EXPORT_SYMBOL(dmu_objset_find
);
2957 EXPORT_SYMBOL(dmu_objset_byteswap
);
2958 EXPORT_SYMBOL(dmu_objset_evict_dbufs
);
2959 EXPORT_SYMBOL(dmu_objset_snap_cmtime
);
2960 EXPORT_SYMBOL(dmu_objset_dnodesize
);
2962 EXPORT_SYMBOL(dmu_objset_sync
);
2963 EXPORT_SYMBOL(dmu_objset_is_dirty
);
2964 EXPORT_SYMBOL(dmu_objset_create_impl_dnstats
);
2965 EXPORT_SYMBOL(dmu_objset_create_impl
);
2966 EXPORT_SYMBOL(dmu_objset_open_impl
);
2967 EXPORT_SYMBOL(dmu_objset_evict
);
2968 EXPORT_SYMBOL(dmu_objset_register_type
);
2969 EXPORT_SYMBOL(dmu_objset_do_userquota_updates
);
2970 EXPORT_SYMBOL(dmu_objset_userquota_get_ids
);
2971 EXPORT_SYMBOL(dmu_objset_userused_enabled
);
2972 EXPORT_SYMBOL(dmu_objset_userspace_upgrade
);
2973 EXPORT_SYMBOL(dmu_objset_userspace_present
);
2974 EXPORT_SYMBOL(dmu_objset_userobjused_enabled
);
2975 EXPORT_SYMBOL(dmu_objset_userobjspace_upgradable
);
2976 EXPORT_SYMBOL(dmu_objset_userobjspace_present
);
2977 EXPORT_SYMBOL(dmu_objset_projectquota_enabled
);
2978 EXPORT_SYMBOL(dmu_objset_projectquota_present
);
2979 EXPORT_SYMBOL(dmu_objset_projectquota_upgradable
);
2980 EXPORT_SYMBOL(dmu_objset_id_quota_upgrade
);