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
));
417 * The $ORIGIN dataset (if it exists) doesn't have an associated
418 * objset, so there's no reason to open it. The $ORIGIN dataset
419 * will not exist on pools older than SPA_VERSION_ORIGIN.
421 if (ds
!= NULL
&& spa_get_dsl(spa
) != NULL
&&
422 spa_get_dsl(spa
)->dp_origin_snap
!= NULL
) {
423 ASSERT3P(ds
->ds_dir
, !=,
424 spa_get_dsl(spa
)->dp_origin_snap
->ds_dir
);
427 os
= kmem_zalloc(sizeof (objset_t
), KM_SLEEP
);
428 os
->os_dsl_dataset
= ds
;
431 if (!BP_IS_HOLE(os
->os_rootbp
)) {
432 arc_flags_t aflags
= ARC_FLAG_WAIT
;
435 enum zio_flag zio_flags
= ZIO_FLAG_CANFAIL
;
436 SET_BOOKMARK(&zb
, ds
? ds
->ds_object
: DMU_META_OBJSET
,
437 ZB_ROOT_OBJECT
, ZB_ROOT_LEVEL
, ZB_ROOT_BLKID
);
439 if (DMU_OS_IS_L2CACHEABLE(os
))
440 aflags
|= ARC_FLAG_L2CACHE
;
442 if (ds
!= NULL
&& ds
->ds_dir
->dd_crypto_obj
!= 0) {
443 ASSERT3U(BP_GET_COMPRESS(bp
), ==, ZIO_COMPRESS_OFF
);
444 ASSERT(BP_IS_AUTHENTICATED(bp
));
445 zio_flags
|= ZIO_FLAG_RAW
;
448 dprintf_bp(os
->os_rootbp
, "reading %s", "");
449 err
= arc_read(NULL
, spa
, os
->os_rootbp
,
450 arc_getbuf_func
, &os
->os_phys_buf
,
451 ZIO_PRIORITY_SYNC_READ
, zio_flags
, &aflags
, &zb
);
453 kmem_free(os
, sizeof (objset_t
));
454 /* convert checksum errors into IO errors */
456 err
= SET_ERROR(EIO
);
460 if (spa_version(spa
) < SPA_VERSION_USERSPACE
)
461 size
= OBJSET_PHYS_SIZE_V1
;
462 else if (!spa_feature_is_enabled(spa
,
463 SPA_FEATURE_PROJECT_QUOTA
))
464 size
= OBJSET_PHYS_SIZE_V2
;
466 size
= sizeof (objset_phys_t
);
468 /* Increase the blocksize if we are permitted. */
469 if (arc_buf_size(os
->os_phys_buf
) < size
) {
470 arc_buf_t
*buf
= arc_alloc_buf(spa
, &os
->os_phys_buf
,
471 ARC_BUFC_METADATA
, size
);
472 bzero(buf
->b_data
, size
);
473 bcopy(os
->os_phys_buf
->b_data
, buf
->b_data
,
474 arc_buf_size(os
->os_phys_buf
));
475 arc_buf_destroy(os
->os_phys_buf
, &os
->os_phys_buf
);
476 os
->os_phys_buf
= buf
;
479 os
->os_phys
= os
->os_phys_buf
->b_data
;
480 os
->os_flags
= os
->os_phys
->os_flags
;
482 int size
= spa_version(spa
) >= SPA_VERSION_USERSPACE
?
483 sizeof (objset_phys_t
) : OBJSET_PHYS_SIZE_V1
;
484 os
->os_phys_buf
= arc_alloc_buf(spa
, &os
->os_phys_buf
,
485 ARC_BUFC_METADATA
, size
);
486 os
->os_phys
= os
->os_phys_buf
->b_data
;
487 bzero(os
->os_phys
, size
);
490 * These properties will be filled in by the logic in zfs_get_zplprop()
491 * when they are queried for the first time.
493 os
->os_version
= OBJSET_PROP_UNINITIALIZED
;
494 os
->os_normalization
= OBJSET_PROP_UNINITIALIZED
;
495 os
->os_utf8only
= OBJSET_PROP_UNINITIALIZED
;
496 os
->os_casesensitivity
= OBJSET_PROP_UNINITIALIZED
;
499 * Note: the changed_cb will be called once before the register
500 * func returns, thus changing the checksum/compression from the
501 * default (fletcher2/off). Snapshots don't need to know about
502 * checksum/compression/copies.
505 boolean_t needlock
= B_FALSE
;
507 os
->os_encrypted
= (ds
->ds_dir
->dd_crypto_obj
!= 0);
510 * Note: it's valid to open the objset if the dataset is
511 * long-held, in which case the pool_config lock will not
514 if (!dsl_pool_config_held(dmu_objset_pool(os
))) {
516 dsl_pool_config_enter(dmu_objset_pool(os
), FTAG
);
519 err
= dsl_prop_register(ds
,
520 zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE
),
521 primary_cache_changed_cb
, os
);
523 err
= dsl_prop_register(ds
,
524 zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE
),
525 secondary_cache_changed_cb
, os
);
527 if (!ds
->ds_is_snapshot
) {
529 err
= dsl_prop_register(ds
,
530 zfs_prop_to_name(ZFS_PROP_CHECKSUM
),
531 checksum_changed_cb
, os
);
534 err
= dsl_prop_register(ds
,
535 zfs_prop_to_name(ZFS_PROP_COMPRESSION
),
536 compression_changed_cb
, os
);
539 err
= dsl_prop_register(ds
,
540 zfs_prop_to_name(ZFS_PROP_COPIES
),
541 copies_changed_cb
, os
);
544 err
= dsl_prop_register(ds
,
545 zfs_prop_to_name(ZFS_PROP_DEDUP
),
546 dedup_changed_cb
, os
);
549 err
= dsl_prop_register(ds
,
550 zfs_prop_to_name(ZFS_PROP_LOGBIAS
),
551 logbias_changed_cb
, os
);
554 err
= dsl_prop_register(ds
,
555 zfs_prop_to_name(ZFS_PROP_SYNC
),
556 sync_changed_cb
, os
);
559 err
= dsl_prop_register(ds
,
561 ZFS_PROP_REDUNDANT_METADATA
),
562 redundant_metadata_changed_cb
, os
);
565 err
= dsl_prop_register(ds
,
566 zfs_prop_to_name(ZFS_PROP_RECORDSIZE
),
567 recordsize_changed_cb
, os
);
570 err
= dsl_prop_register(ds
,
571 zfs_prop_to_name(ZFS_PROP_DNODESIZE
),
572 dnodesize_changed_cb
, os
);
575 err
= dsl_prop_register(ds
,
577 ZFS_PROP_SPECIAL_SMALL_BLOCKS
),
578 smallblk_changed_cb
, os
);
582 dsl_pool_config_exit(dmu_objset_pool(os
), FTAG
);
584 arc_buf_destroy(os
->os_phys_buf
, &os
->os_phys_buf
);
585 kmem_free(os
, sizeof (objset_t
));
589 /* It's the meta-objset. */
590 os
->os_checksum
= ZIO_CHECKSUM_FLETCHER_4
;
591 os
->os_compress
= ZIO_COMPRESS_ON
;
592 os
->os_encrypted
= B_FALSE
;
593 os
->os_copies
= spa_max_replication(spa
);
594 os
->os_dedup_checksum
= ZIO_CHECKSUM_OFF
;
595 os
->os_dedup_verify
= B_FALSE
;
596 os
->os_logbias
= ZFS_LOGBIAS_LATENCY
;
597 os
->os_sync
= ZFS_SYNC_STANDARD
;
598 os
->os_primary_cache
= ZFS_CACHE_ALL
;
599 os
->os_secondary_cache
= ZFS_CACHE_ALL
;
600 os
->os_dnodesize
= DNODE_MIN_SIZE
;
603 if (ds
== NULL
|| !ds
->ds_is_snapshot
)
604 os
->os_zil_header
= os
->os_phys
->os_zil_header
;
605 os
->os_zil
= zil_alloc(os
, &os
->os_zil_header
);
607 for (i
= 0; i
< TXG_SIZE
; i
++) {
608 os
->os_dirty_dnodes
[i
] = multilist_create(sizeof (dnode_t
),
609 offsetof(dnode_t
, dn_dirty_link
[i
]),
610 dnode_multilist_index_func
);
612 list_create(&os
->os_dnodes
, sizeof (dnode_t
),
613 offsetof(dnode_t
, dn_link
));
614 list_create(&os
->os_downgraded_dbufs
, sizeof (dmu_buf_impl_t
),
615 offsetof(dmu_buf_impl_t
, db_link
));
617 list_link_init(&os
->os_evicting_node
);
619 mutex_init(&os
->os_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
620 mutex_init(&os
->os_userused_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
621 mutex_init(&os
->os_obj_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
622 mutex_init(&os
->os_user_ptr_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
623 os
->os_obj_next_percpu_len
= boot_ncpus
;
624 os
->os_obj_next_percpu
= kmem_zalloc(os
->os_obj_next_percpu_len
*
625 sizeof (os
->os_obj_next_percpu
[0]), KM_SLEEP
);
627 dnode_special_open(os
, &os
->os_phys
->os_meta_dnode
,
628 DMU_META_DNODE_OBJECT
, &os
->os_meta_dnode
);
629 if (OBJSET_BUF_HAS_USERUSED(os
->os_phys_buf
)) {
630 dnode_special_open(os
, &os
->os_phys
->os_userused_dnode
,
631 DMU_USERUSED_OBJECT
, &os
->os_userused_dnode
);
632 dnode_special_open(os
, &os
->os_phys
->os_groupused_dnode
,
633 DMU_GROUPUSED_OBJECT
, &os
->os_groupused_dnode
);
634 if (OBJSET_BUF_HAS_PROJECTUSED(os
->os_phys_buf
))
635 dnode_special_open(os
,
636 &os
->os_phys
->os_projectused_dnode
,
637 DMU_PROJECTUSED_OBJECT
, &os
->os_projectused_dnode
);
640 mutex_init(&os
->os_upgrade_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
647 dmu_objset_from_ds(dsl_dataset_t
*ds
, objset_t
**osp
)
652 * We shouldn't be doing anything with dsl_dataset_t's unless the
653 * pool_config lock is held, or the dataset is long-held.
655 ASSERT(dsl_pool_config_held(ds
->ds_dir
->dd_pool
) ||
656 dsl_dataset_long_held(ds
));
658 mutex_enter(&ds
->ds_opening_lock
);
659 if (ds
->ds_objset
== NULL
) {
661 rrw_enter(&ds
->ds_bp_rwlock
, RW_READER
, FTAG
);
662 err
= dmu_objset_open_impl(dsl_dataset_get_spa(ds
),
663 ds
, dsl_dataset_get_blkptr(ds
), &os
);
664 rrw_exit(&ds
->ds_bp_rwlock
, FTAG
);
667 mutex_enter(&ds
->ds_lock
);
668 ASSERT(ds
->ds_objset
== NULL
);
670 mutex_exit(&ds
->ds_lock
);
673 *osp
= ds
->ds_objset
;
674 mutex_exit(&ds
->ds_opening_lock
);
679 * Holds the pool while the objset is held. Therefore only one objset
680 * can be held at a time.
683 dmu_objset_hold_flags(const char *name
, boolean_t decrypt
, void *tag
,
689 ds_hold_flags_t flags
= (decrypt
) ? DS_HOLD_FLAG_DECRYPT
: 0;
691 err
= dsl_pool_hold(name
, tag
, &dp
);
694 err
= dsl_dataset_hold_flags(dp
, name
, flags
, tag
, &ds
);
696 dsl_pool_rele(dp
, tag
);
700 err
= dmu_objset_from_ds(ds
, osp
);
702 dsl_dataset_rele(ds
, tag
);
703 dsl_pool_rele(dp
, tag
);
710 dmu_objset_hold(const char *name
, void *tag
, objset_t
**osp
)
712 return (dmu_objset_hold_flags(name
, B_FALSE
, tag
, osp
));
716 dmu_objset_own_impl(dsl_dataset_t
*ds
, dmu_objset_type_t type
,
717 boolean_t readonly
, boolean_t decrypt
, void *tag
, objset_t
**osp
)
721 err
= dmu_objset_from_ds(ds
, osp
);
724 } else if (type
!= DMU_OST_ANY
&& type
!= (*osp
)->os_phys
->os_type
) {
725 return (SET_ERROR(EINVAL
));
726 } else if (!readonly
&& dsl_dataset_is_snapshot(ds
)) {
727 return (SET_ERROR(EROFS
));
728 } else if (!readonly
&& decrypt
&&
729 dsl_dir_incompatible_encryption_version(ds
->ds_dir
)) {
730 return (SET_ERROR(EROFS
));
733 /* if we are decrypting, we can now check MACs in os->os_phys_buf */
734 if (decrypt
&& arc_is_unauthenticated((*osp
)->os_phys_buf
)) {
737 SET_BOOKMARK(&zb
, ds
->ds_object
, ZB_ROOT_OBJECT
,
738 ZB_ROOT_LEVEL
, ZB_ROOT_BLKID
);
739 err
= arc_untransform((*osp
)->os_phys_buf
, (*osp
)->os_spa
,
744 ASSERT0(arc_is_unauthenticated((*osp
)->os_phys_buf
));
751 * dsl_pool must not be held when this is called.
752 * Upon successful return, there will be a longhold on the dataset,
753 * and the dsl_pool will not be held.
756 dmu_objset_own(const char *name
, dmu_objset_type_t type
,
757 boolean_t readonly
, boolean_t decrypt
, void *tag
, objset_t
**osp
)
762 ds_hold_flags_t flags
= (decrypt
) ? DS_HOLD_FLAG_DECRYPT
: 0;
764 err
= dsl_pool_hold(name
, FTAG
, &dp
);
767 err
= dsl_dataset_own(dp
, name
, flags
, tag
, &ds
);
769 dsl_pool_rele(dp
, FTAG
);
772 err
= dmu_objset_own_impl(ds
, type
, readonly
, decrypt
, tag
, osp
);
774 dsl_dataset_disown(ds
, flags
, tag
);
775 dsl_pool_rele(dp
, FTAG
);
780 * User accounting requires the dataset to be decrypted and rw.
781 * We also don't begin user accounting during claiming to help
782 * speed up pool import times and to keep this txg reserved
783 * completely for recovery work.
785 if ((dmu_objset_userobjspace_upgradable(*osp
) ||
786 dmu_objset_projectquota_upgradable(*osp
)) &&
787 !readonly
&& !dp
->dp_spa
->spa_claiming
&&
788 (ds
->ds_dir
->dd_crypto_obj
== 0 || decrypt
))
789 dmu_objset_id_quota_upgrade(*osp
);
791 dsl_pool_rele(dp
, FTAG
);
796 dmu_objset_own_obj(dsl_pool_t
*dp
, uint64_t obj
, dmu_objset_type_t type
,
797 boolean_t readonly
, boolean_t decrypt
, void *tag
, objset_t
**osp
)
801 ds_hold_flags_t flags
= (decrypt
) ? DS_HOLD_FLAG_DECRYPT
: 0;
803 err
= dsl_dataset_own_obj(dp
, obj
, flags
, tag
, &ds
);
807 err
= dmu_objset_own_impl(ds
, type
, readonly
, decrypt
, tag
, osp
);
809 dsl_dataset_disown(ds
, flags
, tag
);
817 dmu_objset_rele_flags(objset_t
*os
, boolean_t decrypt
, void *tag
)
819 ds_hold_flags_t flags
= (decrypt
) ? DS_HOLD_FLAG_DECRYPT
: 0;
821 dsl_pool_t
*dp
= dmu_objset_pool(os
);
822 dsl_dataset_rele_flags(os
->os_dsl_dataset
, flags
, tag
);
823 dsl_pool_rele(dp
, tag
);
827 dmu_objset_rele(objset_t
*os
, void *tag
)
829 dmu_objset_rele_flags(os
, B_FALSE
, tag
);
833 * When we are called, os MUST refer to an objset associated with a dataset
834 * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner
835 * == tag. We will then release and reacquire ownership of the dataset while
836 * holding the pool config_rwlock to avoid intervening namespace or ownership
839 * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to
840 * release the hold on its dataset and acquire a new one on the dataset of the
841 * same name so that it can be partially torn down and reconstructed.
844 dmu_objset_refresh_ownership(dsl_dataset_t
*ds
, dsl_dataset_t
**newds
,
845 boolean_t decrypt
, void *tag
)
848 char name
[ZFS_MAX_DATASET_NAME_LEN
];
850 VERIFY3P(ds
, !=, NULL
);
851 VERIFY3P(ds
->ds_owner
, ==, tag
);
852 VERIFY(dsl_dataset_long_held(ds
));
854 dsl_dataset_name(ds
, name
);
855 dp
= ds
->ds_dir
->dd_pool
;
856 dsl_pool_config_enter(dp
, FTAG
);
857 dsl_dataset_disown(ds
, decrypt
, tag
);
858 VERIFY0(dsl_dataset_own(dp
, name
,
859 (decrypt
) ? DS_HOLD_FLAG_DECRYPT
: 0, tag
, newds
));
860 dsl_pool_config_exit(dp
, FTAG
);
864 dmu_objset_disown(objset_t
*os
, boolean_t decrypt
, void *tag
)
867 * Stop upgrading thread
869 dmu_objset_upgrade_stop(os
);
870 dsl_dataset_disown(os
->os_dsl_dataset
,
871 (decrypt
) ? DS_HOLD_FLAG_DECRYPT
: 0, tag
);
875 dmu_objset_evict_dbufs(objset_t
*os
)
880 dn_marker
= kmem_alloc(sizeof (dnode_t
), KM_SLEEP
);
882 mutex_enter(&os
->os_lock
);
883 dn
= list_head(&os
->os_dnodes
);
886 * Skip dnodes without holds. We have to do this dance
887 * because dnode_add_ref() only works if there is already a
888 * hold. If the dnode has no holds, then it has no dbufs.
890 if (dnode_add_ref(dn
, FTAG
)) {
891 list_insert_after(&os
->os_dnodes
, dn
, dn_marker
);
892 mutex_exit(&os
->os_lock
);
894 dnode_evict_dbufs(dn
);
895 dnode_rele(dn
, FTAG
);
897 mutex_enter(&os
->os_lock
);
898 dn
= list_next(&os
->os_dnodes
, dn_marker
);
899 list_remove(&os
->os_dnodes
, dn_marker
);
901 dn
= list_next(&os
->os_dnodes
, dn
);
904 mutex_exit(&os
->os_lock
);
906 kmem_free(dn_marker
, sizeof (dnode_t
));
908 if (DMU_USERUSED_DNODE(os
) != NULL
) {
909 if (DMU_PROJECTUSED_DNODE(os
) != NULL
)
910 dnode_evict_dbufs(DMU_PROJECTUSED_DNODE(os
));
911 dnode_evict_dbufs(DMU_GROUPUSED_DNODE(os
));
912 dnode_evict_dbufs(DMU_USERUSED_DNODE(os
));
914 dnode_evict_dbufs(DMU_META_DNODE(os
));
918 * Objset eviction processing is split into into two pieces.
919 * The first marks the objset as evicting, evicts any dbufs that
920 * have a refcount of zero, and then queues up the objset for the
921 * second phase of eviction. Once os->os_dnodes has been cleared by
922 * dnode_buf_pageout()->dnode_destroy(), the second phase is executed.
923 * The second phase closes the special dnodes, dequeues the objset from
924 * the list of those undergoing eviction, and finally frees the objset.
926 * NOTE: Due to asynchronous eviction processing (invocation of
927 * dnode_buf_pageout()), it is possible for the meta dnode for the
928 * objset to have no holds even though os->os_dnodes is not empty.
931 dmu_objset_evict(objset_t
*os
)
933 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
935 for (int t
= 0; t
< TXG_SIZE
; t
++)
936 ASSERT(!dmu_objset_is_dirty(os
, t
));
939 dsl_prop_unregister_all(ds
, os
);
944 dmu_objset_evict_dbufs(os
);
946 mutex_enter(&os
->os_lock
);
947 spa_evicting_os_register(os
->os_spa
, os
);
948 if (list_is_empty(&os
->os_dnodes
)) {
949 mutex_exit(&os
->os_lock
);
950 dmu_objset_evict_done(os
);
952 mutex_exit(&os
->os_lock
);
959 dmu_objset_evict_done(objset_t
*os
)
961 ASSERT3P(list_head(&os
->os_dnodes
), ==, NULL
);
963 dnode_special_close(&os
->os_meta_dnode
);
964 if (DMU_USERUSED_DNODE(os
)) {
965 if (DMU_PROJECTUSED_DNODE(os
))
966 dnode_special_close(&os
->os_projectused_dnode
);
967 dnode_special_close(&os
->os_userused_dnode
);
968 dnode_special_close(&os
->os_groupused_dnode
);
970 zil_free(os
->os_zil
);
972 arc_buf_destroy(os
->os_phys_buf
, &os
->os_phys_buf
);
975 * This is a barrier to prevent the objset from going away in
976 * dnode_move() until we can safely ensure that the objset is still in
977 * use. We consider the objset valid before the barrier and invalid
980 rw_enter(&os_lock
, RW_READER
);
983 kmem_free(os
->os_obj_next_percpu
,
984 os
->os_obj_next_percpu_len
* sizeof (os
->os_obj_next_percpu
[0]));
986 mutex_destroy(&os
->os_lock
);
987 mutex_destroy(&os
->os_userused_lock
);
988 mutex_destroy(&os
->os_obj_lock
);
989 mutex_destroy(&os
->os_user_ptr_lock
);
990 mutex_destroy(&os
->os_upgrade_lock
);
991 for (int i
= 0; i
< TXG_SIZE
; i
++) {
992 multilist_destroy(os
->os_dirty_dnodes
[i
]);
994 spa_evicting_os_deregister(os
->os_spa
, os
);
995 kmem_free(os
, sizeof (objset_t
));
999 dmu_objset_snap_cmtime(objset_t
*os
)
1001 return (dsl_dir_snap_cmtime(os
->os_dsl_dataset
->ds_dir
));
1005 dmu_objset_create_impl_dnstats(spa_t
*spa
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
1006 dmu_objset_type_t type
, int levels
, int blksz
, int ibs
, dmu_tx_t
*tx
)
1011 ASSERT(dmu_tx_is_syncing(tx
));
1014 blksz
= DNODE_BLOCK_SIZE
;
1016 ibs
= DN_MAX_INDBLKSHIFT
;
1019 VERIFY0(dmu_objset_from_ds(ds
, &os
));
1021 VERIFY0(dmu_objset_open_impl(spa
, NULL
, bp
, &os
));
1023 mdn
= DMU_META_DNODE(os
);
1025 dnode_allocate(mdn
, DMU_OT_DNODE
, blksz
, ibs
, DMU_OT_NONE
, 0,
1026 DNODE_MIN_SLOTS
, tx
);
1029 * We don't want to have to increase the meta-dnode's nlevels
1030 * later, because then we could do it in quescing context while
1031 * we are also accessing it in open context.
1033 * This precaution is not necessary for the MOS (ds == NULL),
1034 * because the MOS is only updated in syncing context.
1035 * This is most fortunate: the MOS is the only objset that
1036 * needs to be synced multiple times as spa_sync() iterates
1037 * to convergence, so minimizing its dn_nlevels matters.
1044 * Determine the number of levels necessary for the
1045 * meta-dnode to contain DN_MAX_OBJECT dnodes. Note
1046 * that in order to ensure that we do not overflow
1047 * 64 bits, there has to be a nlevels that gives us a
1048 * number of blocks > DN_MAX_OBJECT but < 2^64.
1049 * Therefore, (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)
1050 * (10) must be less than (64 - log2(DN_MAX_OBJECT))
1053 while ((uint64_t)mdn
->dn_nblkptr
<<
1054 (mdn
->dn_datablkshift
- DNODE_SHIFT
+ (levels
- 1) *
1055 (mdn
->dn_indblkshift
- SPA_BLKPTRSHIFT
)) <
1060 mdn
->dn_next_nlevels
[tx
->tx_txg
& TXG_MASK
] =
1061 mdn
->dn_nlevels
= levels
;
1064 ASSERT(type
!= DMU_OST_NONE
);
1065 ASSERT(type
!= DMU_OST_ANY
);
1066 ASSERT(type
< DMU_OST_NUMTYPES
);
1067 os
->os_phys
->os_type
= type
;
1070 * Enable user accounting if it is enabled and this is not an
1071 * encrypted receive.
1073 if (dmu_objset_userused_enabled(os
) &&
1074 (!os
->os_encrypted
|| !dmu_objset_is_receiving(os
))) {
1075 os
->os_phys
->os_flags
|= OBJSET_FLAG_USERACCOUNTING_COMPLETE
;
1076 if (dmu_objset_userobjused_enabled(os
)) {
1077 ds
->ds_feature_activation
[
1078 SPA_FEATURE_USEROBJ_ACCOUNTING
] = (void *)B_TRUE
;
1079 os
->os_phys
->os_flags
|=
1080 OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE
;
1082 if (dmu_objset_projectquota_enabled(os
)) {
1083 ds
->ds_feature_activation
[
1084 SPA_FEATURE_PROJECT_QUOTA
] = (void *)B_TRUE
;
1085 os
->os_phys
->os_flags
|=
1086 OBJSET_FLAG_PROJECTQUOTA_COMPLETE
;
1088 os
->os_flags
= os
->os_phys
->os_flags
;
1091 dsl_dataset_dirty(ds
, tx
);
1096 /* called from dsl for meta-objset */
1098 dmu_objset_create_impl(spa_t
*spa
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
1099 dmu_objset_type_t type
, dmu_tx_t
*tx
)
1101 return (dmu_objset_create_impl_dnstats(spa
, ds
, bp
, type
, 0, 0, 0, tx
));
1104 typedef struct dmu_objset_create_arg
{
1105 const char *doca_name
;
1107 void (*doca_userfunc
)(objset_t
*os
, void *arg
,
1108 cred_t
*cr
, dmu_tx_t
*tx
);
1110 dmu_objset_type_t doca_type
;
1111 uint64_t doca_flags
;
1112 dsl_crypto_params_t
*doca_dcp
;
1113 } dmu_objset_create_arg_t
;
1117 dmu_objset_create_check(void *arg
, dmu_tx_t
*tx
)
1119 dmu_objset_create_arg_t
*doca
= arg
;
1120 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1122 dsl_dataset_t
*parentds
;
1127 if (strchr(doca
->doca_name
, '@') != NULL
)
1128 return (SET_ERROR(EINVAL
));
1130 if (strlen(doca
->doca_name
) >= ZFS_MAX_DATASET_NAME_LEN
)
1131 return (SET_ERROR(ENAMETOOLONG
));
1133 if (dataset_nestcheck(doca
->doca_name
) != 0)
1134 return (SET_ERROR(ENAMETOOLONG
));
1136 error
= dsl_dir_hold(dp
, doca
->doca_name
, FTAG
, &pdd
, &tail
);
1140 dsl_dir_rele(pdd
, FTAG
);
1141 return (SET_ERROR(EEXIST
));
1144 error
= dmu_objset_create_crypt_check(pdd
, doca
->doca_dcp
, NULL
);
1146 dsl_dir_rele(pdd
, FTAG
);
1150 error
= dsl_fs_ss_limit_check(pdd
, 1, ZFS_PROP_FILESYSTEM_LIMIT
, NULL
,
1153 dsl_dir_rele(pdd
, FTAG
);
1157 /* can't create below anything but filesystems (eg. no ZVOLs) */
1158 error
= dsl_dataset_hold_obj(pdd
->dd_pool
,
1159 dsl_dir_phys(pdd
)->dd_head_dataset_obj
, FTAG
, &parentds
);
1161 dsl_dir_rele(pdd
, FTAG
);
1164 error
= dmu_objset_from_ds(parentds
, &parentos
);
1166 dsl_dataset_rele(parentds
, FTAG
);
1167 dsl_dir_rele(pdd
, FTAG
);
1170 if (dmu_objset_type(parentos
) != DMU_OST_ZFS
) {
1171 dsl_dataset_rele(parentds
, FTAG
);
1172 dsl_dir_rele(pdd
, FTAG
);
1173 return (SET_ERROR(ZFS_ERR_WRONG_PARENT
));
1175 dsl_dataset_rele(parentds
, FTAG
);
1176 dsl_dir_rele(pdd
, FTAG
);
1182 dmu_objset_create_sync(void *arg
, dmu_tx_t
*tx
)
1184 dmu_objset_create_arg_t
*doca
= arg
;
1185 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1186 spa_t
*spa
= dp
->dp_spa
;
1195 VERIFY0(dsl_dir_hold(dp
, doca
->doca_name
, FTAG
, &pdd
, &tail
));
1197 obj
= dsl_dataset_create_sync(pdd
, tail
, NULL
, doca
->doca_flags
,
1198 doca
->doca_cred
, doca
->doca_dcp
, tx
);
1200 VERIFY0(dsl_dataset_hold_obj_flags(pdd
->dd_pool
, obj
,
1201 DS_HOLD_FLAG_DECRYPT
, FTAG
, &ds
));
1202 rrw_enter(&ds
->ds_bp_rwlock
, RW_READER
, FTAG
);
1203 bp
= dsl_dataset_get_blkptr(ds
);
1204 os
= dmu_objset_create_impl(spa
, ds
, bp
, doca
->doca_type
, tx
);
1205 rrw_exit(&ds
->ds_bp_rwlock
, FTAG
);
1207 if (doca
->doca_userfunc
!= NULL
) {
1208 doca
->doca_userfunc(os
, doca
->doca_userarg
,
1209 doca
->doca_cred
, tx
);
1213 * The doca_userfunc() may write out some data that needs to be
1214 * encrypted if the dataset is encrypted (specifically the root
1215 * directory). This data must be written out before the encryption
1216 * key mapping is removed by dsl_dataset_rele_flags(). Force the
1217 * I/O to occur immediately by invoking the relevant sections of
1220 if (os
->os_encrypted
) {
1221 dsl_dataset_t
*tmpds
= NULL
;
1222 boolean_t need_sync_done
= B_FALSE
;
1224 mutex_enter(&ds
->ds_lock
);
1225 ds
->ds_owner
= FTAG
;
1226 mutex_exit(&ds
->ds_lock
);
1228 rzio
= zio_root(spa
, NULL
, NULL
, ZIO_FLAG_MUSTSUCCEED
);
1229 tmpds
= txg_list_remove_this(&dp
->dp_dirty_datasets
, ds
,
1231 if (tmpds
!= NULL
) {
1232 dsl_dataset_sync(ds
, rzio
, tx
);
1233 need_sync_done
= B_TRUE
;
1235 VERIFY0(zio_wait(rzio
));
1237 dmu_objset_do_userquota_updates(os
, tx
);
1238 taskq_wait(dp
->dp_sync_taskq
);
1239 if (txg_list_member(&dp
->dp_dirty_datasets
, ds
, tx
->tx_txg
)) {
1240 ASSERT3P(ds
->ds_key_mapping
, !=, NULL
);
1241 key_mapping_rele(spa
, ds
->ds_key_mapping
, ds
);
1244 rzio
= zio_root(spa
, NULL
, NULL
, ZIO_FLAG_MUSTSUCCEED
);
1245 tmpds
= txg_list_remove_this(&dp
->dp_dirty_datasets
, ds
,
1247 if (tmpds
!= NULL
) {
1248 dmu_buf_rele(ds
->ds_dbuf
, ds
);
1249 dsl_dataset_sync(ds
, rzio
, tx
);
1251 VERIFY0(zio_wait(rzio
));
1253 if (need_sync_done
) {
1254 ASSERT3P(ds
->ds_key_mapping
, !=, NULL
);
1255 key_mapping_rele(spa
, ds
->ds_key_mapping
, ds
);
1256 dsl_dataset_sync_done(ds
, tx
);
1259 mutex_enter(&ds
->ds_lock
);
1260 ds
->ds_owner
= NULL
;
1261 mutex_exit(&ds
->ds_lock
);
1264 spa_history_log_internal_ds(ds
, "create", tx
, "");
1265 zvol_create_minors(spa
, doca
->doca_name
, B_TRUE
);
1267 dsl_dataset_rele_flags(ds
, DS_HOLD_FLAG_DECRYPT
, FTAG
);
1268 dsl_dir_rele(pdd
, FTAG
);
1272 dmu_objset_create(const char *name
, dmu_objset_type_t type
, uint64_t flags
,
1273 dsl_crypto_params_t
*dcp
, dmu_objset_create_sync_func_t func
, void *arg
)
1275 dmu_objset_create_arg_t doca
;
1276 dsl_crypto_params_t tmp_dcp
= { 0 };
1278 doca
.doca_name
= name
;
1279 doca
.doca_cred
= CRED();
1280 doca
.doca_flags
= flags
;
1281 doca
.doca_userfunc
= func
;
1282 doca
.doca_userarg
= arg
;
1283 doca
.doca_type
= type
;
1286 * Some callers (mostly for testing) do not provide a dcp on their
1287 * own but various code inside the sync task will require it to be
1288 * allocated. Rather than adding NULL checks throughout this code
1289 * or adding dummy dcp's to all of the callers we simply create a
1290 * dummy one here and use that. This zero dcp will have the same
1291 * effect as asking for inheritance of all encryption params.
1293 doca
.doca_dcp
= (dcp
!= NULL
) ? dcp
: &tmp_dcp
;
1295 return (dsl_sync_task(name
,
1296 dmu_objset_create_check
, dmu_objset_create_sync
, &doca
,
1297 6, ZFS_SPACE_CHECK_NORMAL
));
1300 typedef struct dmu_objset_clone_arg
{
1301 const char *doca_clone
;
1302 const char *doca_origin
;
1304 } dmu_objset_clone_arg_t
;
1308 dmu_objset_clone_check(void *arg
, dmu_tx_t
*tx
)
1310 dmu_objset_clone_arg_t
*doca
= arg
;
1314 dsl_dataset_t
*origin
;
1315 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1317 if (strchr(doca
->doca_clone
, '@') != NULL
)
1318 return (SET_ERROR(EINVAL
));
1320 if (strlen(doca
->doca_clone
) >= ZFS_MAX_DATASET_NAME_LEN
)
1321 return (SET_ERROR(ENAMETOOLONG
));
1323 error
= dsl_dir_hold(dp
, doca
->doca_clone
, FTAG
, &pdd
, &tail
);
1327 dsl_dir_rele(pdd
, FTAG
);
1328 return (SET_ERROR(EEXIST
));
1331 error
= dsl_fs_ss_limit_check(pdd
, 1, ZFS_PROP_FILESYSTEM_LIMIT
, NULL
,
1334 dsl_dir_rele(pdd
, FTAG
);
1335 return (SET_ERROR(EDQUOT
));
1338 error
= dsl_dataset_hold(dp
, doca
->doca_origin
, FTAG
, &origin
);
1340 dsl_dir_rele(pdd
, FTAG
);
1344 /* You can only clone snapshots, not the head datasets. */
1345 if (!origin
->ds_is_snapshot
) {
1346 dsl_dataset_rele(origin
, FTAG
);
1347 dsl_dir_rele(pdd
, FTAG
);
1348 return (SET_ERROR(EINVAL
));
1351 error
= dmu_objset_clone_crypt_check(pdd
, origin
->ds_dir
);
1353 dsl_dataset_rele(origin
, FTAG
);
1354 dsl_dir_rele(pdd
, FTAG
);
1358 dsl_dataset_rele(origin
, FTAG
);
1359 dsl_dir_rele(pdd
, FTAG
);
1365 dmu_objset_clone_sync(void *arg
, dmu_tx_t
*tx
)
1367 dmu_objset_clone_arg_t
*doca
= arg
;
1368 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1371 dsl_dataset_t
*origin
, *ds
;
1373 char namebuf
[ZFS_MAX_DATASET_NAME_LEN
];
1375 VERIFY0(dsl_dir_hold(dp
, doca
->doca_clone
, FTAG
, &pdd
, &tail
));
1376 VERIFY0(dsl_dataset_hold(dp
, doca
->doca_origin
, FTAG
, &origin
));
1378 obj
= dsl_dataset_create_sync(pdd
, tail
, origin
, 0,
1379 doca
->doca_cred
, NULL
, tx
);
1381 VERIFY0(dsl_dataset_hold_obj(pdd
->dd_pool
, obj
, FTAG
, &ds
));
1382 dsl_dataset_name(origin
, namebuf
);
1383 spa_history_log_internal_ds(ds
, "clone", tx
,
1384 "origin=%s (%llu)", namebuf
, origin
->ds_object
);
1385 zvol_create_minors(dp
->dp_spa
, doca
->doca_clone
, B_TRUE
);
1386 dsl_dataset_rele(ds
, FTAG
);
1387 dsl_dataset_rele(origin
, FTAG
);
1388 dsl_dir_rele(pdd
, FTAG
);
1392 dmu_objset_clone(const char *clone
, const char *origin
)
1394 dmu_objset_clone_arg_t doca
;
1396 doca
.doca_clone
= clone
;
1397 doca
.doca_origin
= origin
;
1398 doca
.doca_cred
= CRED();
1400 return (dsl_sync_task(clone
,
1401 dmu_objset_clone_check
, dmu_objset_clone_sync
, &doca
,
1402 6, ZFS_SPACE_CHECK_NORMAL
));
1406 dmu_objset_remap_indirects_impl(objset_t
*os
, uint64_t last_removed_txg
)
1409 uint64_t object
= 0;
1410 while ((error
= dmu_object_next(os
, &object
, B_FALSE
, 0)) == 0) {
1411 error
= dmu_object_remap_indirects(os
, object
,
1414 * If the ZPL removed the object before we managed to dnode_hold
1415 * it, we would get an ENOENT. If the ZPL declares its intent
1416 * to remove the object (dnode_free) before we manage to
1417 * dnode_hold it, we would get an EEXIST. In either case, we
1418 * want to continue remapping the other objects in the objset;
1419 * in all other cases, we want to break early.
1421 if (error
!= 0 && error
!= ENOENT
&& error
!= EEXIST
) {
1425 if (error
== ESRCH
) {
1432 dmu_objset_remap_indirects(const char *fsname
)
1435 objset_t
*os
= NULL
;
1436 uint64_t last_removed_txg
;
1437 uint64_t remap_start_txg
;
1440 error
= dmu_objset_hold(fsname
, FTAG
, &os
);
1444 dd
= dmu_objset_ds(os
)->ds_dir
;
1446 if (!spa_feature_is_enabled(dmu_objset_spa(os
),
1447 SPA_FEATURE_OBSOLETE_COUNTS
)) {
1448 dmu_objset_rele(os
, FTAG
);
1449 return (SET_ERROR(ENOTSUP
));
1452 if (dsl_dataset_is_snapshot(dmu_objset_ds(os
))) {
1453 dmu_objset_rele(os
, FTAG
);
1454 return (SET_ERROR(EINVAL
));
1458 * If there has not been a removal, we're done.
1460 last_removed_txg
= spa_get_last_removal_txg(dmu_objset_spa(os
));
1461 if (last_removed_txg
== -1ULL) {
1462 dmu_objset_rele(os
, FTAG
);
1467 * If we have remapped since the last removal, we're done.
1469 if (dsl_dir_is_zapified(dd
)) {
1470 uint64_t last_remap_txg
;
1471 if (zap_lookup(spa_meta_objset(dmu_objset_spa(os
)),
1472 dd
->dd_object
, DD_FIELD_LAST_REMAP_TXG
,
1473 sizeof (last_remap_txg
), 1, &last_remap_txg
) == 0 &&
1474 last_remap_txg
> last_removed_txg
) {
1475 dmu_objset_rele(os
, FTAG
);
1480 dsl_dataset_long_hold(dmu_objset_ds(os
), FTAG
);
1481 dsl_pool_rele(dmu_objset_pool(os
), FTAG
);
1483 remap_start_txg
= spa_last_synced_txg(dmu_objset_spa(os
));
1484 error
= dmu_objset_remap_indirects_impl(os
, last_removed_txg
);
1487 * We update the last_remap_txg to be the start txg so that
1488 * we can guarantee that every block older than last_remap_txg
1489 * that can be remapped has been remapped.
1491 error
= dsl_dir_update_last_remap_txg(dd
, remap_start_txg
);
1494 dsl_dataset_long_rele(dmu_objset_ds(os
), FTAG
);
1495 dsl_dataset_rele(dmu_objset_ds(os
), FTAG
);
1501 dmu_objset_snapshot_one(const char *fsname
, const char *snapname
)
1504 char *longsnap
= kmem_asprintf("%s@%s", fsname
, snapname
);
1505 nvlist_t
*snaps
= fnvlist_alloc();
1507 fnvlist_add_boolean(snaps
, longsnap
);
1509 err
= dsl_dataset_snapshot(snaps
, NULL
, NULL
);
1510 fnvlist_free(snaps
);
1515 dmu_objset_upgrade_task_cb(void *data
)
1517 objset_t
*os
= data
;
1519 mutex_enter(&os
->os_upgrade_lock
);
1520 os
->os_upgrade_status
= EINTR
;
1521 if (!os
->os_upgrade_exit
) {
1522 mutex_exit(&os
->os_upgrade_lock
);
1524 os
->os_upgrade_status
= os
->os_upgrade_cb(os
);
1525 mutex_enter(&os
->os_upgrade_lock
);
1527 os
->os_upgrade_exit
= B_TRUE
;
1528 os
->os_upgrade_id
= 0;
1529 mutex_exit(&os
->os_upgrade_lock
);
1530 dsl_dataset_long_rele(dmu_objset_ds(os
), upgrade_tag
);
1534 dmu_objset_upgrade(objset_t
*os
, dmu_objset_upgrade_cb_t cb
)
1536 if (os
->os_upgrade_id
!= 0)
1539 ASSERT(dsl_pool_config_held(dmu_objset_pool(os
)));
1540 dsl_dataset_long_hold(dmu_objset_ds(os
), upgrade_tag
);
1542 mutex_enter(&os
->os_upgrade_lock
);
1543 if (os
->os_upgrade_id
== 0 && os
->os_upgrade_status
== 0) {
1544 os
->os_upgrade_exit
= B_FALSE
;
1545 os
->os_upgrade_cb
= cb
;
1546 os
->os_upgrade_id
= taskq_dispatch(
1547 os
->os_spa
->spa_upgrade_taskq
,
1548 dmu_objset_upgrade_task_cb
, os
, TQ_SLEEP
);
1549 if (os
->os_upgrade_id
== TASKQID_INVALID
) {
1550 dsl_dataset_long_rele(dmu_objset_ds(os
), upgrade_tag
);
1551 os
->os_upgrade_status
= ENOMEM
;
1554 mutex_exit(&os
->os_upgrade_lock
);
1558 dmu_objset_upgrade_stop(objset_t
*os
)
1560 mutex_enter(&os
->os_upgrade_lock
);
1561 os
->os_upgrade_exit
= B_TRUE
;
1562 if (os
->os_upgrade_id
!= 0) {
1563 taskqid_t id
= os
->os_upgrade_id
;
1565 os
->os_upgrade_id
= 0;
1566 mutex_exit(&os
->os_upgrade_lock
);
1568 if ((taskq_cancel_id(os
->os_spa
->spa_upgrade_taskq
, id
)) == 0) {
1569 dsl_dataset_long_rele(dmu_objset_ds(os
), upgrade_tag
);
1571 txg_wait_synced(os
->os_spa
->spa_dsl_pool
, 0);
1573 mutex_exit(&os
->os_upgrade_lock
);
1578 dmu_objset_sync_dnodes(multilist_sublist_t
*list
, dmu_tx_t
*tx
)
1582 while ((dn
= multilist_sublist_head(list
)) != NULL
) {
1583 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
);
1584 ASSERT(dn
->dn_dbuf
->db_data_pending
);
1586 * Initialize dn_zio outside dnode_sync() because the
1587 * meta-dnode needs to set it ouside dnode_sync().
1589 dn
->dn_zio
= dn
->dn_dbuf
->db_data_pending
->dr_zio
;
1592 ASSERT3U(dn
->dn_nlevels
, <=, DN_MAX_LEVELS
);
1593 multilist_sublist_remove(list
, dn
);
1596 * If we are not doing useraccounting (os_synced_dnodes == NULL)
1597 * we are done with this dnode for this txg. Unset dn_dirty_txg
1598 * if later txgs aren't dirtying it so that future holders do
1599 * not get a stale value. Otherwise, we will do this in
1600 * userquota_updates_task() when processing has completely
1601 * finished for this txg.
1603 multilist_t
*newlist
= dn
->dn_objset
->os_synced_dnodes
;
1604 if (newlist
!= NULL
) {
1605 (void) dnode_add_ref(dn
, newlist
);
1606 multilist_insert(newlist
, dn
);
1608 mutex_enter(&dn
->dn_mtx
);
1609 if (dn
->dn_dirty_txg
== tx
->tx_txg
)
1610 dn
->dn_dirty_txg
= 0;
1611 mutex_exit(&dn
->dn_mtx
);
1620 dmu_objset_write_ready(zio_t
*zio
, arc_buf_t
*abuf
, void *arg
)
1622 blkptr_t
*bp
= zio
->io_bp
;
1624 dnode_phys_t
*dnp
= &os
->os_phys
->os_meta_dnode
;
1627 ASSERT(!BP_IS_EMBEDDED(bp
));
1628 ASSERT3U(BP_GET_TYPE(bp
), ==, DMU_OT_OBJSET
);
1629 ASSERT0(BP_GET_LEVEL(bp
));
1632 * Update rootbp fill count: it should be the number of objects
1633 * allocated in the object set (not counting the "special"
1634 * objects that are stored in the objset_phys_t -- the meta
1635 * dnode and user/group/project accounting objects).
1637 for (int i
= 0; i
< dnp
->dn_nblkptr
; i
++)
1638 fill
+= BP_GET_FILL(&dnp
->dn_blkptr
[i
]);
1640 BP_SET_FILL(bp
, fill
);
1642 if (os
->os_dsl_dataset
!= NULL
)
1643 rrw_enter(&os
->os_dsl_dataset
->ds_bp_rwlock
, RW_WRITER
, FTAG
);
1644 *os
->os_rootbp
= *bp
;
1645 if (os
->os_dsl_dataset
!= NULL
)
1646 rrw_exit(&os
->os_dsl_dataset
->ds_bp_rwlock
, FTAG
);
1651 dmu_objset_write_done(zio_t
*zio
, arc_buf_t
*abuf
, void *arg
)
1653 blkptr_t
*bp
= zio
->io_bp
;
1654 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
1657 if (zio
->io_flags
& ZIO_FLAG_IO_REWRITE
) {
1658 ASSERT(BP_EQUAL(bp
, bp_orig
));
1660 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
1661 dmu_tx_t
*tx
= os
->os_synctx
;
1663 (void) dsl_dataset_block_kill(ds
, bp_orig
, tx
, B_TRUE
);
1664 dsl_dataset_block_born(ds
, bp
, tx
);
1666 kmem_free(bp
, sizeof (*bp
));
1669 typedef struct sync_dnodes_arg
{
1670 multilist_t
*sda_list
;
1671 int sda_sublist_idx
;
1672 multilist_t
*sda_newlist
;
1674 } sync_dnodes_arg_t
;
1677 sync_dnodes_task(void *arg
)
1679 sync_dnodes_arg_t
*sda
= arg
;
1681 multilist_sublist_t
*ms
=
1682 multilist_sublist_lock(sda
->sda_list
, sda
->sda_sublist_idx
);
1684 dmu_objset_sync_dnodes(ms
, sda
->sda_tx
);
1686 multilist_sublist_unlock(ms
);
1688 kmem_free(sda
, sizeof (*sda
));
1692 /* called from dsl */
1694 dmu_objset_sync(objset_t
*os
, zio_t
*pio
, dmu_tx_t
*tx
)
1697 zbookmark_phys_t zb
;
1701 dbuf_dirty_record_t
*dr
;
1702 blkptr_t
*blkptr_copy
= kmem_alloc(sizeof (*os
->os_rootbp
), KM_SLEEP
);
1703 *blkptr_copy
= *os
->os_rootbp
;
1705 dprintf_ds(os
->os_dsl_dataset
, "txg=%llu\n", tx
->tx_txg
);
1707 ASSERT(dmu_tx_is_syncing(tx
));
1708 /* XXX the write_done callback should really give us the tx... */
1711 if (os
->os_dsl_dataset
== NULL
) {
1713 * This is the MOS. If we have upgraded,
1714 * spa_max_replication() could change, so reset
1717 os
->os_copies
= spa_max_replication(os
->os_spa
);
1721 * Create the root block IO
1723 SET_BOOKMARK(&zb
, os
->os_dsl_dataset
?
1724 os
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
1725 ZB_ROOT_OBJECT
, ZB_ROOT_LEVEL
, ZB_ROOT_BLKID
);
1726 arc_release(os
->os_phys_buf
, &os
->os_phys_buf
);
1728 dmu_write_policy(os
, NULL
, 0, 0, &zp
);
1731 * If we are either claiming the ZIL or doing a raw receive, write
1732 * out the os_phys_buf raw. Neither of these actions will effect the
1733 * MAC at this point.
1735 if (os
->os_raw_receive
||
1736 os
->os_next_write_raw
[tx
->tx_txg
& TXG_MASK
]) {
1737 ASSERT(os
->os_encrypted
);
1738 arc_convert_to_raw(os
->os_phys_buf
,
1739 os
->os_dsl_dataset
->ds_object
, ZFS_HOST_BYTEORDER
,
1740 DMU_OT_OBJSET
, NULL
, NULL
, NULL
);
1743 zio
= arc_write(pio
, os
->os_spa
, tx
->tx_txg
,
1744 blkptr_copy
, os
->os_phys_buf
, DMU_OS_IS_L2CACHEABLE(os
),
1745 &zp
, dmu_objset_write_ready
, NULL
, NULL
, dmu_objset_write_done
,
1746 os
, ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);
1749 * Sync special dnodes - the parent IO for the sync is the root block
1751 DMU_META_DNODE(os
)->dn_zio
= zio
;
1752 dnode_sync(DMU_META_DNODE(os
), tx
);
1754 os
->os_phys
->os_flags
= os
->os_flags
;
1756 if (DMU_USERUSED_DNODE(os
) &&
1757 DMU_USERUSED_DNODE(os
)->dn_type
!= DMU_OT_NONE
) {
1758 DMU_USERUSED_DNODE(os
)->dn_zio
= zio
;
1759 dnode_sync(DMU_USERUSED_DNODE(os
), tx
);
1760 DMU_GROUPUSED_DNODE(os
)->dn_zio
= zio
;
1761 dnode_sync(DMU_GROUPUSED_DNODE(os
), tx
);
1764 if (DMU_PROJECTUSED_DNODE(os
) &&
1765 DMU_PROJECTUSED_DNODE(os
)->dn_type
!= DMU_OT_NONE
) {
1766 DMU_PROJECTUSED_DNODE(os
)->dn_zio
= zio
;
1767 dnode_sync(DMU_PROJECTUSED_DNODE(os
), tx
);
1770 txgoff
= tx
->tx_txg
& TXG_MASK
;
1772 if (dmu_objset_userused_enabled(os
) &&
1773 (!os
->os_encrypted
|| !dmu_objset_is_receiving(os
))) {
1775 * We must create the list here because it uses the
1776 * dn_dirty_link[] of this txg. But it may already
1777 * exist because we call dsl_dataset_sync() twice per txg.
1779 if (os
->os_synced_dnodes
== NULL
) {
1780 os
->os_synced_dnodes
=
1781 multilist_create(sizeof (dnode_t
),
1782 offsetof(dnode_t
, dn_dirty_link
[txgoff
]),
1783 dnode_multilist_index_func
);
1785 ASSERT3U(os
->os_synced_dnodes
->ml_offset
, ==,
1786 offsetof(dnode_t
, dn_dirty_link
[txgoff
]));
1791 i
< multilist_get_num_sublists(os
->os_dirty_dnodes
[txgoff
]); i
++) {
1792 sync_dnodes_arg_t
*sda
= kmem_alloc(sizeof (*sda
), KM_SLEEP
);
1793 sda
->sda_list
= os
->os_dirty_dnodes
[txgoff
];
1794 sda
->sda_sublist_idx
= i
;
1796 (void) taskq_dispatch(dmu_objset_pool(os
)->dp_sync_taskq
,
1797 sync_dnodes_task
, sda
, 0);
1798 /* callback frees sda */
1800 taskq_wait(dmu_objset_pool(os
)->dp_sync_taskq
);
1802 list
= &DMU_META_DNODE(os
)->dn_dirty_records
[txgoff
];
1803 while ((dr
= list_head(list
)) != NULL
) {
1804 ASSERT0(dr
->dr_dbuf
->db_level
);
1805 list_remove(list
, dr
);
1807 zio_nowait(dr
->dr_zio
);
1810 /* Enable dnode backfill if enough objects have been freed. */
1811 if (os
->os_freed_dnodes
>= dmu_rescan_dnode_threshold
) {
1812 os
->os_rescan_dnodes
= B_TRUE
;
1813 os
->os_freed_dnodes
= 0;
1817 * Free intent log blocks up to this tx.
1819 zil_sync(os
->os_zil
, tx
);
1820 os
->os_phys
->os_zil_header
= os
->os_zil_header
;
1825 dmu_objset_is_dirty(objset_t
*os
, uint64_t txg
)
1827 return (!multilist_is_empty(os
->os_dirty_dnodes
[txg
& TXG_MASK
]));
1830 static objset_used_cb_t
*used_cbs
[DMU_OST_NUMTYPES
];
1833 dmu_objset_register_type(dmu_objset_type_t ost
, objset_used_cb_t
*cb
)
1839 dmu_objset_userused_enabled(objset_t
*os
)
1841 return (spa_version(os
->os_spa
) >= SPA_VERSION_USERSPACE
&&
1842 used_cbs
[os
->os_phys
->os_type
] != NULL
&&
1843 DMU_USERUSED_DNODE(os
) != NULL
);
1847 dmu_objset_userobjused_enabled(objset_t
*os
)
1849 return (dmu_objset_userused_enabled(os
) &&
1850 spa_feature_is_enabled(os
->os_spa
, SPA_FEATURE_USEROBJ_ACCOUNTING
));
1854 dmu_objset_projectquota_enabled(objset_t
*os
)
1856 return (used_cbs
[os
->os_phys
->os_type
] != NULL
&&
1857 DMU_PROJECTUSED_DNODE(os
) != NULL
&&
1858 spa_feature_is_enabled(os
->os_spa
, SPA_FEATURE_PROJECT_QUOTA
));
1861 typedef struct userquota_node
{
1862 /* must be in the first filed, see userquota_update_cache() */
1863 char uqn_id
[20 + DMU_OBJACCT_PREFIX_LEN
];
1865 avl_node_t uqn_node
;
1868 typedef struct userquota_cache
{
1869 avl_tree_t uqc_user_deltas
;
1870 avl_tree_t uqc_group_deltas
;
1871 avl_tree_t uqc_project_deltas
;
1872 } userquota_cache_t
;
1875 userquota_compare(const void *l
, const void *r
)
1877 const userquota_node_t
*luqn
= l
;
1878 const userquota_node_t
*ruqn
= r
;
1882 * NB: can only access uqn_id because userquota_update_cache() doesn't
1883 * pass in an entire userquota_node_t.
1885 rv
= strcmp(luqn
->uqn_id
, ruqn
->uqn_id
);
1887 return (AVL_ISIGN(rv
));
1891 do_userquota_cacheflush(objset_t
*os
, userquota_cache_t
*cache
, dmu_tx_t
*tx
)
1894 userquota_node_t
*uqn
;
1896 ASSERT(dmu_tx_is_syncing(tx
));
1899 while ((uqn
= avl_destroy_nodes(&cache
->uqc_user_deltas
,
1900 &cookie
)) != NULL
) {
1902 * os_userused_lock protects against concurrent calls to
1903 * zap_increment_int(). It's needed because zap_increment_int()
1904 * is not thread-safe (i.e. not atomic).
1906 mutex_enter(&os
->os_userused_lock
);
1907 VERIFY0(zap_increment(os
, DMU_USERUSED_OBJECT
,
1908 uqn
->uqn_id
, uqn
->uqn_delta
, tx
));
1909 mutex_exit(&os
->os_userused_lock
);
1910 kmem_free(uqn
, sizeof (*uqn
));
1912 avl_destroy(&cache
->uqc_user_deltas
);
1915 while ((uqn
= avl_destroy_nodes(&cache
->uqc_group_deltas
,
1916 &cookie
)) != NULL
) {
1917 mutex_enter(&os
->os_userused_lock
);
1918 VERIFY0(zap_increment(os
, DMU_GROUPUSED_OBJECT
,
1919 uqn
->uqn_id
, uqn
->uqn_delta
, tx
));
1920 mutex_exit(&os
->os_userused_lock
);
1921 kmem_free(uqn
, sizeof (*uqn
));
1923 avl_destroy(&cache
->uqc_group_deltas
);
1925 if (dmu_objset_projectquota_enabled(os
)) {
1927 while ((uqn
= avl_destroy_nodes(&cache
->uqc_project_deltas
,
1928 &cookie
)) != NULL
) {
1929 mutex_enter(&os
->os_userused_lock
);
1930 VERIFY0(zap_increment(os
, DMU_PROJECTUSED_OBJECT
,
1931 uqn
->uqn_id
, uqn
->uqn_delta
, tx
));
1932 mutex_exit(&os
->os_userused_lock
);
1933 kmem_free(uqn
, sizeof (*uqn
));
1935 avl_destroy(&cache
->uqc_project_deltas
);
1940 userquota_update_cache(avl_tree_t
*avl
, const char *id
, int64_t delta
)
1942 userquota_node_t
*uqn
;
1945 ASSERT(strlen(id
) < sizeof (uqn
->uqn_id
));
1947 * Use id directly for searching because uqn_id is the first field of
1948 * userquota_node_t and fields after uqn_id won't be accessed in
1951 uqn
= avl_find(avl
, (const void *)id
, &idx
);
1953 uqn
= kmem_zalloc(sizeof (*uqn
), KM_SLEEP
);
1954 strlcpy(uqn
->uqn_id
, id
, sizeof (uqn
->uqn_id
));
1955 avl_insert(avl
, uqn
, idx
);
1957 uqn
->uqn_delta
+= delta
;
1961 do_userquota_update(objset_t
*os
, userquota_cache_t
*cache
, uint64_t used
,
1962 uint64_t flags
, uint64_t user
, uint64_t group
, uint64_t project
,
1965 if (flags
& DNODE_FLAG_USERUSED_ACCOUNTED
) {
1966 int64_t delta
= DNODE_MIN_SIZE
+ used
;
1972 (void) sprintf(name
, "%llx", (longlong_t
)user
);
1973 userquota_update_cache(&cache
->uqc_user_deltas
, name
, delta
);
1975 (void) sprintf(name
, "%llx", (longlong_t
)group
);
1976 userquota_update_cache(&cache
->uqc_group_deltas
, name
, delta
);
1978 if (dmu_objset_projectquota_enabled(os
)) {
1979 (void) sprintf(name
, "%llx", (longlong_t
)project
);
1980 userquota_update_cache(&cache
->uqc_project_deltas
,
1987 do_userobjquota_update(objset_t
*os
, userquota_cache_t
*cache
, uint64_t flags
,
1988 uint64_t user
, uint64_t group
, uint64_t project
, boolean_t subtract
)
1990 if (flags
& DNODE_FLAG_USEROBJUSED_ACCOUNTED
) {
1991 char name
[20 + DMU_OBJACCT_PREFIX_LEN
];
1992 int delta
= subtract
? -1 : 1;
1994 (void) snprintf(name
, sizeof (name
), DMU_OBJACCT_PREFIX
"%llx",
1996 userquota_update_cache(&cache
->uqc_user_deltas
, name
, delta
);
1998 (void) snprintf(name
, sizeof (name
), DMU_OBJACCT_PREFIX
"%llx",
2000 userquota_update_cache(&cache
->uqc_group_deltas
, name
, delta
);
2002 if (dmu_objset_projectquota_enabled(os
)) {
2003 (void) snprintf(name
, sizeof (name
),
2004 DMU_OBJACCT_PREFIX
"%llx", (longlong_t
)project
);
2005 userquota_update_cache(&cache
->uqc_project_deltas
,
2011 typedef struct userquota_updates_arg
{
2013 int uua_sublist_idx
;
2015 } userquota_updates_arg_t
;
2018 userquota_updates_task(void *arg
)
2020 userquota_updates_arg_t
*uua
= arg
;
2021 objset_t
*os
= uua
->uua_os
;
2022 dmu_tx_t
*tx
= uua
->uua_tx
;
2024 userquota_cache_t cache
= { { 0 } };
2026 multilist_sublist_t
*list
=
2027 multilist_sublist_lock(os
->os_synced_dnodes
, uua
->uua_sublist_idx
);
2029 ASSERT(multilist_sublist_head(list
) == NULL
||
2030 dmu_objset_userused_enabled(os
));
2031 avl_create(&cache
.uqc_user_deltas
, userquota_compare
,
2032 sizeof (userquota_node_t
), offsetof(userquota_node_t
, uqn_node
));
2033 avl_create(&cache
.uqc_group_deltas
, userquota_compare
,
2034 sizeof (userquota_node_t
), offsetof(userquota_node_t
, uqn_node
));
2035 if (dmu_objset_projectquota_enabled(os
))
2036 avl_create(&cache
.uqc_project_deltas
, userquota_compare
,
2037 sizeof (userquota_node_t
), offsetof(userquota_node_t
,
2040 while ((dn
= multilist_sublist_head(list
)) != NULL
) {
2042 ASSERT(!DMU_OBJECT_IS_SPECIAL(dn
->dn_object
));
2043 ASSERT(dn
->dn_phys
->dn_type
== DMU_OT_NONE
||
2044 dn
->dn_phys
->dn_flags
&
2045 DNODE_FLAG_USERUSED_ACCOUNTED
);
2047 flags
= dn
->dn_id_flags
;
2049 if (flags
& DN_ID_OLD_EXIST
) {
2050 do_userquota_update(os
, &cache
, dn
->dn_oldused
,
2051 dn
->dn_oldflags
, dn
->dn_olduid
, dn
->dn_oldgid
,
2052 dn
->dn_oldprojid
, B_TRUE
);
2053 do_userobjquota_update(os
, &cache
, dn
->dn_oldflags
,
2054 dn
->dn_olduid
, dn
->dn_oldgid
,
2055 dn
->dn_oldprojid
, B_TRUE
);
2057 if (flags
& DN_ID_NEW_EXIST
) {
2058 do_userquota_update(os
, &cache
,
2059 DN_USED_BYTES(dn
->dn_phys
), dn
->dn_phys
->dn_flags
,
2060 dn
->dn_newuid
, dn
->dn_newgid
,
2061 dn
->dn_newprojid
, B_FALSE
);
2062 do_userobjquota_update(os
, &cache
,
2063 dn
->dn_phys
->dn_flags
, dn
->dn_newuid
, dn
->dn_newgid
,
2064 dn
->dn_newprojid
, B_FALSE
);
2067 mutex_enter(&dn
->dn_mtx
);
2069 dn
->dn_oldflags
= 0;
2070 if (dn
->dn_id_flags
& DN_ID_NEW_EXIST
) {
2071 dn
->dn_olduid
= dn
->dn_newuid
;
2072 dn
->dn_oldgid
= dn
->dn_newgid
;
2073 dn
->dn_oldprojid
= dn
->dn_newprojid
;
2074 dn
->dn_id_flags
|= DN_ID_OLD_EXIST
;
2075 if (dn
->dn_bonuslen
== 0)
2076 dn
->dn_id_flags
|= DN_ID_CHKED_SPILL
;
2078 dn
->dn_id_flags
|= DN_ID_CHKED_BONUS
;
2080 dn
->dn_id_flags
&= ~(DN_ID_NEW_EXIST
);
2081 if (dn
->dn_dirty_txg
== spa_syncing_txg(os
->os_spa
))
2082 dn
->dn_dirty_txg
= 0;
2083 mutex_exit(&dn
->dn_mtx
);
2085 multilist_sublist_remove(list
, dn
);
2086 dnode_rele(dn
, os
->os_synced_dnodes
);
2088 do_userquota_cacheflush(os
, &cache
, tx
);
2089 multilist_sublist_unlock(list
);
2090 kmem_free(uua
, sizeof (*uua
));
2094 dmu_objset_do_userquota_updates(objset_t
*os
, dmu_tx_t
*tx
)
2096 if (!dmu_objset_userused_enabled(os
))
2100 * If this is a raw receive just return and handle accounting
2101 * later when we have the keys loaded. We also don't do user
2102 * accounting during claiming since the datasets are not owned
2103 * for the duration of claiming and this txg should only be
2104 * used for recovery.
2106 if (os
->os_encrypted
&& dmu_objset_is_receiving(os
))
2109 if (tx
->tx_txg
<= os
->os_spa
->spa_claim_max_txg
)
2112 /* Allocate the user/group/project used objects if necessary. */
2113 if (DMU_USERUSED_DNODE(os
)->dn_type
== DMU_OT_NONE
) {
2114 VERIFY0(zap_create_claim(os
,
2115 DMU_USERUSED_OBJECT
,
2116 DMU_OT_USERGROUP_USED
, DMU_OT_NONE
, 0, tx
));
2117 VERIFY0(zap_create_claim(os
,
2118 DMU_GROUPUSED_OBJECT
,
2119 DMU_OT_USERGROUP_USED
, DMU_OT_NONE
, 0, tx
));
2122 if (dmu_objset_projectquota_enabled(os
) &&
2123 DMU_PROJECTUSED_DNODE(os
)->dn_type
== DMU_OT_NONE
) {
2124 VERIFY0(zap_create_claim(os
, DMU_PROJECTUSED_OBJECT
,
2125 DMU_OT_USERGROUP_USED
, DMU_OT_NONE
, 0, tx
));
2129 i
< multilist_get_num_sublists(os
->os_synced_dnodes
); i
++) {
2130 userquota_updates_arg_t
*uua
=
2131 kmem_alloc(sizeof (*uua
), KM_SLEEP
);
2133 uua
->uua_sublist_idx
= i
;
2135 /* note: caller does taskq_wait() */
2136 (void) taskq_dispatch(dmu_objset_pool(os
)->dp_sync_taskq
,
2137 userquota_updates_task
, uua
, 0);
2138 /* callback frees uua */
2143 * Returns a pointer to data to find uid/gid from
2145 * If a dirty record for transaction group that is syncing can't
2146 * be found then NULL is returned. In the NULL case it is assumed
2147 * the uid/gid aren't changing.
2150 dmu_objset_userquota_find_data(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
2152 dbuf_dirty_record_t
*dr
, **drp
;
2155 if (db
->db_dirtycnt
== 0)
2156 return (db
->db
.db_data
); /* Nothing is changing */
2158 for (drp
= &db
->db_last_dirty
; (dr
= *drp
) != NULL
; drp
= &dr
->dr_next
)
2159 if (dr
->dr_txg
== tx
->tx_txg
)
2167 DB_DNODE_ENTER(dr
->dr_dbuf
);
2168 dn
= DB_DNODE(dr
->dr_dbuf
);
2170 if (dn
->dn_bonuslen
== 0 &&
2171 dr
->dr_dbuf
->db_blkid
== DMU_SPILL_BLKID
)
2172 data
= dr
->dt
.dl
.dr_data
->b_data
;
2174 data
= dr
->dt
.dl
.dr_data
;
2176 DB_DNODE_EXIT(dr
->dr_dbuf
);
2183 dmu_objset_userquota_get_ids(dnode_t
*dn
, boolean_t before
, dmu_tx_t
*tx
)
2185 objset_t
*os
= dn
->dn_objset
;
2187 dmu_buf_impl_t
*db
= NULL
;
2188 uint64_t *user
= NULL
;
2189 uint64_t *group
= NULL
;
2190 uint64_t *project
= NULL
;
2191 int flags
= dn
->dn_id_flags
;
2193 boolean_t have_spill
= B_FALSE
;
2195 if (!dmu_objset_userused_enabled(dn
->dn_objset
))
2199 * Raw receives introduce a problem with user accounting. Raw
2200 * receives cannot update the user accounting info because the
2201 * user ids and the sizes are encrypted. To guarantee that we
2202 * never end up with bad user accounting, we simply disable it
2203 * during raw receives. We also disable this for normal receives
2204 * so that an incremental raw receive may be done on top of an
2205 * existing non-raw receive.
2207 if (os
->os_encrypted
&& dmu_objset_is_receiving(os
))
2210 if (before
&& (flags
& (DN_ID_CHKED_BONUS
|DN_ID_OLD_EXIST
|
2211 DN_ID_CHKED_SPILL
)))
2214 if (before
&& dn
->dn_bonuslen
!= 0)
2215 data
= DN_BONUS(dn
->dn_phys
);
2216 else if (!before
&& dn
->dn_bonuslen
!= 0) {
2219 mutex_enter(&db
->db_mtx
);
2220 data
= dmu_objset_userquota_find_data(db
, tx
);
2222 data
= DN_BONUS(dn
->dn_phys
);
2224 } else if (dn
->dn_bonuslen
== 0 && dn
->dn_bonustype
== DMU_OT_SA
) {
2227 if (RW_WRITE_HELD(&dn
->dn_struct_rwlock
))
2228 rf
|= DB_RF_HAVESTRUCT
;
2229 error
= dmu_spill_hold_by_dnode(dn
,
2230 rf
| DB_RF_MUST_SUCCEED
,
2231 FTAG
, (dmu_buf_t
**)&db
);
2233 mutex_enter(&db
->db_mtx
);
2234 data
= (before
) ? db
->db
.db_data
:
2235 dmu_objset_userquota_find_data(db
, tx
);
2236 have_spill
= B_TRUE
;
2238 mutex_enter(&dn
->dn_mtx
);
2239 dn
->dn_id_flags
|= DN_ID_CHKED_BONUS
;
2240 mutex_exit(&dn
->dn_mtx
);
2246 user
= &dn
->dn_olduid
;
2247 group
= &dn
->dn_oldgid
;
2248 project
= &dn
->dn_oldprojid
;
2250 user
= &dn
->dn_newuid
;
2251 group
= &dn
->dn_newgid
;
2252 project
= &dn
->dn_newprojid
;
2256 * Must always call the callback in case the object
2257 * type has changed and that type isn't an object type to track
2259 error
= used_cbs
[os
->os_phys
->os_type
](dn
->dn_bonustype
, data
,
2260 user
, group
, project
);
2263 * Preserve existing uid/gid when the callback can't determine
2264 * what the new uid/gid are and the callback returned EEXIST.
2265 * The EEXIST error tells us to just use the existing uid/gid.
2266 * If we don't know what the old values are then just assign
2267 * them to 0, since that is a new file being created.
2269 if (!before
&& data
== NULL
&& error
== EEXIST
) {
2270 if (flags
& DN_ID_OLD_EXIST
) {
2271 dn
->dn_newuid
= dn
->dn_olduid
;
2272 dn
->dn_newgid
= dn
->dn_oldgid
;
2273 dn
->dn_newprojid
= dn
->dn_oldprojid
;
2277 dn
->dn_newprojid
= ZFS_DEFAULT_PROJID
;
2283 mutex_exit(&db
->db_mtx
);
2285 mutex_enter(&dn
->dn_mtx
);
2286 if (error
== 0 && before
)
2287 dn
->dn_id_flags
|= DN_ID_OLD_EXIST
;
2288 if (error
== 0 && !before
)
2289 dn
->dn_id_flags
|= DN_ID_NEW_EXIST
;
2292 dn
->dn_id_flags
|= DN_ID_CHKED_SPILL
;
2294 dn
->dn_id_flags
|= DN_ID_CHKED_BONUS
;
2296 mutex_exit(&dn
->dn_mtx
);
2298 dmu_buf_rele((dmu_buf_t
*)db
, FTAG
);
2302 dmu_objset_userspace_present(objset_t
*os
)
2304 return (os
->os_phys
->os_flags
&
2305 OBJSET_FLAG_USERACCOUNTING_COMPLETE
);
2309 dmu_objset_userobjspace_present(objset_t
*os
)
2311 return (os
->os_phys
->os_flags
&
2312 OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE
);
2316 dmu_objset_projectquota_present(objset_t
*os
)
2318 return (os
->os_phys
->os_flags
&
2319 OBJSET_FLAG_PROJECTQUOTA_COMPLETE
);
2323 dmu_objset_space_upgrade(objset_t
*os
)
2329 * We simply need to mark every object dirty, so that it will be
2330 * synced out and now accounted. If this is called
2331 * concurrently, or if we already did some work before crashing,
2332 * that's fine, since we track each object's accounted state
2336 for (obj
= 0; err
== 0; err
= dmu_object_next(os
, &obj
, FALSE
, 0)) {
2341 mutex_enter(&os
->os_upgrade_lock
);
2342 if (os
->os_upgrade_exit
)
2343 err
= SET_ERROR(EINTR
);
2344 mutex_exit(&os
->os_upgrade_lock
);
2348 if (issig(JUSTLOOKING
) && issig(FORREAL
))
2349 return (SET_ERROR(EINTR
));
2351 objerr
= dmu_bonus_hold(os
, obj
, FTAG
, &db
);
2354 tx
= dmu_tx_create(os
);
2355 dmu_tx_hold_bonus(tx
, obj
);
2356 objerr
= dmu_tx_assign(tx
, TXG_WAIT
);
2358 dmu_buf_rele(db
, FTAG
);
2362 dmu_buf_will_dirty(db
, tx
);
2363 dmu_buf_rele(db
, FTAG
);
2370 dmu_objset_userspace_upgrade(objset_t
*os
)
2374 if (dmu_objset_userspace_present(os
))
2376 if (dmu_objset_is_snapshot(os
))
2377 return (SET_ERROR(EINVAL
));
2378 if (!dmu_objset_userused_enabled(os
))
2379 return (SET_ERROR(ENOTSUP
));
2381 err
= dmu_objset_space_upgrade(os
);
2385 os
->os_flags
|= OBJSET_FLAG_USERACCOUNTING_COMPLETE
;
2386 txg_wait_synced(dmu_objset_pool(os
), 0);
2391 dmu_objset_id_quota_upgrade_cb(objset_t
*os
)
2395 if (dmu_objset_userobjspace_present(os
) &&
2396 dmu_objset_projectquota_present(os
))
2398 if (dmu_objset_is_snapshot(os
))
2399 return (SET_ERROR(EINVAL
));
2400 if (!dmu_objset_userobjused_enabled(os
))
2401 return (SET_ERROR(ENOTSUP
));
2402 if (!dmu_objset_projectquota_enabled(os
) &&
2403 dmu_objset_userobjspace_present(os
))
2404 return (SET_ERROR(ENOTSUP
));
2406 dmu_objset_ds(os
)->ds_feature_activation
[
2407 SPA_FEATURE_USEROBJ_ACCOUNTING
] = (void *)B_TRUE
;
2408 if (dmu_objset_projectquota_enabled(os
))
2409 dmu_objset_ds(os
)->ds_feature_activation
[
2410 SPA_FEATURE_PROJECT_QUOTA
] = (void *)B_TRUE
;
2412 err
= dmu_objset_space_upgrade(os
);
2416 os
->os_flags
|= OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE
;
2417 if (dmu_objset_projectquota_enabled(os
))
2418 os
->os_flags
|= OBJSET_FLAG_PROJECTQUOTA_COMPLETE
;
2420 txg_wait_synced(dmu_objset_pool(os
), 0);
2425 dmu_objset_id_quota_upgrade(objset_t
*os
)
2427 dmu_objset_upgrade(os
, dmu_objset_id_quota_upgrade_cb
);
2431 dmu_objset_userobjspace_upgradable(objset_t
*os
)
2433 return (dmu_objset_type(os
) == DMU_OST_ZFS
&&
2434 !dmu_objset_is_snapshot(os
) &&
2435 dmu_objset_userobjused_enabled(os
) &&
2436 !dmu_objset_userobjspace_present(os
) &&
2437 spa_writeable(dmu_objset_spa(os
)));
2441 dmu_objset_projectquota_upgradable(objset_t
*os
)
2443 return (dmu_objset_type(os
) == DMU_OST_ZFS
&&
2444 !dmu_objset_is_snapshot(os
) &&
2445 dmu_objset_projectquota_enabled(os
) &&
2446 !dmu_objset_projectquota_present(os
) &&
2447 spa_writeable(dmu_objset_spa(os
)));
2451 dmu_objset_space(objset_t
*os
, uint64_t *refdbytesp
, uint64_t *availbytesp
,
2452 uint64_t *usedobjsp
, uint64_t *availobjsp
)
2454 dsl_dataset_space(os
->os_dsl_dataset
, refdbytesp
, availbytesp
,
2455 usedobjsp
, availobjsp
);
2459 dmu_objset_fsid_guid(objset_t
*os
)
2461 return (dsl_dataset_fsid_guid(os
->os_dsl_dataset
));
2465 dmu_objset_fast_stat(objset_t
*os
, dmu_objset_stats_t
*stat
)
2467 stat
->dds_type
= os
->os_phys
->os_type
;
2468 if (os
->os_dsl_dataset
)
2469 dsl_dataset_fast_stat(os
->os_dsl_dataset
, stat
);
2473 dmu_objset_stats(objset_t
*os
, nvlist_t
*nv
)
2475 ASSERT(os
->os_dsl_dataset
||
2476 os
->os_phys
->os_type
== DMU_OST_META
);
2478 if (os
->os_dsl_dataset
!= NULL
)
2479 dsl_dataset_stats(os
->os_dsl_dataset
, nv
);
2481 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_TYPE
,
2482 os
->os_phys
->os_type
);
2483 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_USERACCOUNTING
,
2484 dmu_objset_userspace_present(os
));
2488 dmu_objset_is_snapshot(objset_t
*os
)
2490 if (os
->os_dsl_dataset
!= NULL
)
2491 return (os
->os_dsl_dataset
->ds_is_snapshot
);
2497 dmu_snapshot_realname(objset_t
*os
, char *name
, char *real
, int maxlen
,
2498 boolean_t
*conflict
)
2500 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
2503 if (dsl_dataset_phys(ds
)->ds_snapnames_zapobj
== 0)
2504 return (SET_ERROR(ENOENT
));
2506 return (zap_lookup_norm(ds
->ds_dir
->dd_pool
->dp_meta_objset
,
2507 dsl_dataset_phys(ds
)->ds_snapnames_zapobj
, name
, 8, 1, &ignored
,
2508 MT_NORMALIZE
, real
, maxlen
, conflict
));
2512 dmu_snapshot_list_next(objset_t
*os
, int namelen
, char *name
,
2513 uint64_t *idp
, uint64_t *offp
, boolean_t
*case_conflict
)
2515 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
2516 zap_cursor_t cursor
;
2517 zap_attribute_t attr
;
2519 ASSERT(dsl_pool_config_held(dmu_objset_pool(os
)));
2521 if (dsl_dataset_phys(ds
)->ds_snapnames_zapobj
== 0)
2522 return (SET_ERROR(ENOENT
));
2524 zap_cursor_init_serialized(&cursor
,
2525 ds
->ds_dir
->dd_pool
->dp_meta_objset
,
2526 dsl_dataset_phys(ds
)->ds_snapnames_zapobj
, *offp
);
2528 if (zap_cursor_retrieve(&cursor
, &attr
) != 0) {
2529 zap_cursor_fini(&cursor
);
2530 return (SET_ERROR(ENOENT
));
2533 if (strlen(attr
.za_name
) + 1 > namelen
) {
2534 zap_cursor_fini(&cursor
);
2535 return (SET_ERROR(ENAMETOOLONG
));
2538 (void) strcpy(name
, attr
.za_name
);
2540 *idp
= attr
.za_first_integer
;
2542 *case_conflict
= attr
.za_normalization_conflict
;
2543 zap_cursor_advance(&cursor
);
2544 *offp
= zap_cursor_serialize(&cursor
);
2545 zap_cursor_fini(&cursor
);
2551 dmu_snapshot_lookup(objset_t
*os
, const char *name
, uint64_t *value
)
2553 return (dsl_dataset_snap_lookup(os
->os_dsl_dataset
, name
, value
));
2557 dmu_dir_list_next(objset_t
*os
, int namelen
, char *name
,
2558 uint64_t *idp
, uint64_t *offp
)
2560 dsl_dir_t
*dd
= os
->os_dsl_dataset
->ds_dir
;
2561 zap_cursor_t cursor
;
2562 zap_attribute_t attr
;
2564 /* there is no next dir on a snapshot! */
2565 if (os
->os_dsl_dataset
->ds_object
!=
2566 dsl_dir_phys(dd
)->dd_head_dataset_obj
)
2567 return (SET_ERROR(ENOENT
));
2569 zap_cursor_init_serialized(&cursor
,
2570 dd
->dd_pool
->dp_meta_objset
,
2571 dsl_dir_phys(dd
)->dd_child_dir_zapobj
, *offp
);
2573 if (zap_cursor_retrieve(&cursor
, &attr
) != 0) {
2574 zap_cursor_fini(&cursor
);
2575 return (SET_ERROR(ENOENT
));
2578 if (strlen(attr
.za_name
) + 1 > namelen
) {
2579 zap_cursor_fini(&cursor
);
2580 return (SET_ERROR(ENAMETOOLONG
));
2583 (void) strcpy(name
, attr
.za_name
);
2585 *idp
= attr
.za_first_integer
;
2586 zap_cursor_advance(&cursor
);
2587 *offp
= zap_cursor_serialize(&cursor
);
2588 zap_cursor_fini(&cursor
);
2593 typedef struct dmu_objset_find_ctx
{
2597 char *dc_ddname
; /* last component of ddobj's name */
2598 int (*dc_func
)(dsl_pool_t
*, dsl_dataset_t
*, void *);
2601 kmutex_t
*dc_error_lock
;
2603 } dmu_objset_find_ctx_t
;
2606 dmu_objset_find_dp_impl(dmu_objset_find_ctx_t
*dcp
)
2608 dsl_pool_t
*dp
= dcp
->dc_dp
;
2612 zap_attribute_t
*attr
;
2616 /* don't process if there already was an error */
2617 if (*dcp
->dc_error
!= 0)
2621 * Note: passing the name (dc_ddname) here is optional, but it
2622 * improves performance because we don't need to call
2623 * zap_value_search() to determine the name.
2625 err
= dsl_dir_hold_obj(dp
, dcp
->dc_ddobj
, dcp
->dc_ddname
, FTAG
, &dd
);
2629 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
2630 if (dd
->dd_myname
[0] == '$') {
2631 dsl_dir_rele(dd
, FTAG
);
2635 thisobj
= dsl_dir_phys(dd
)->dd_head_dataset_obj
;
2636 attr
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
2639 * Iterate over all children.
2641 if (dcp
->dc_flags
& DS_FIND_CHILDREN
) {
2642 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
,
2643 dsl_dir_phys(dd
)->dd_child_dir_zapobj
);
2644 zap_cursor_retrieve(&zc
, attr
) == 0;
2645 (void) zap_cursor_advance(&zc
)) {
2646 ASSERT3U(attr
->za_integer_length
, ==,
2648 ASSERT3U(attr
->za_num_integers
, ==, 1);
2650 dmu_objset_find_ctx_t
*child_dcp
=
2651 kmem_alloc(sizeof (*child_dcp
), KM_SLEEP
);
2653 child_dcp
->dc_ddobj
= attr
->za_first_integer
;
2654 child_dcp
->dc_ddname
= spa_strdup(attr
->za_name
);
2655 if (dcp
->dc_tq
!= NULL
)
2656 (void) taskq_dispatch(dcp
->dc_tq
,
2657 dmu_objset_find_dp_cb
, child_dcp
, TQ_SLEEP
);
2659 dmu_objset_find_dp_impl(child_dcp
);
2661 zap_cursor_fini(&zc
);
2665 * Iterate over all snapshots.
2667 if (dcp
->dc_flags
& DS_FIND_SNAPSHOTS
) {
2669 err
= dsl_dataset_hold_obj(dp
, thisobj
, FTAG
, &ds
);
2674 snapobj
= dsl_dataset_phys(ds
)->ds_snapnames_zapobj
;
2675 dsl_dataset_rele(ds
, FTAG
);
2677 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
, snapobj
);
2678 zap_cursor_retrieve(&zc
, attr
) == 0;
2679 (void) zap_cursor_advance(&zc
)) {
2680 ASSERT3U(attr
->za_integer_length
, ==,
2682 ASSERT3U(attr
->za_num_integers
, ==, 1);
2684 err
= dsl_dataset_hold_obj(dp
,
2685 attr
->za_first_integer
, FTAG
, &ds
);
2688 err
= dcp
->dc_func(dp
, ds
, dcp
->dc_arg
);
2689 dsl_dataset_rele(ds
, FTAG
);
2693 zap_cursor_fini(&zc
);
2697 kmem_free(attr
, sizeof (zap_attribute_t
));
2700 dsl_dir_rele(dd
, FTAG
);
2707 err
= dsl_dataset_hold_obj(dp
, thisobj
, FTAG
, &ds
);
2710 * Note: we hold the dir while calling dsl_dataset_hold_obj() so
2711 * that the dir will remain cached, and we won't have to re-instantiate
2712 * it (which could be expensive due to finding its name via
2713 * zap_value_search()).
2715 dsl_dir_rele(dd
, FTAG
);
2718 err
= dcp
->dc_func(dp
, ds
, dcp
->dc_arg
);
2719 dsl_dataset_rele(ds
, FTAG
);
2723 mutex_enter(dcp
->dc_error_lock
);
2724 /* only keep first error */
2725 if (*dcp
->dc_error
== 0)
2726 *dcp
->dc_error
= err
;
2727 mutex_exit(dcp
->dc_error_lock
);
2730 if (dcp
->dc_ddname
!= NULL
)
2731 spa_strfree(dcp
->dc_ddname
);
2732 kmem_free(dcp
, sizeof (*dcp
));
2736 dmu_objset_find_dp_cb(void *arg
)
2738 dmu_objset_find_ctx_t
*dcp
= arg
;
2739 dsl_pool_t
*dp
= dcp
->dc_dp
;
2742 * We need to get a pool_config_lock here, as there are several
2743 * asssert(pool_config_held) down the stack. Getting a lock via
2744 * dsl_pool_config_enter is risky, as it might be stalled by a
2745 * pending writer. This would deadlock, as the write lock can
2746 * only be granted when our parent thread gives up the lock.
2747 * The _prio interface gives us priority over a pending writer.
2749 dsl_pool_config_enter_prio(dp
, FTAG
);
2751 dmu_objset_find_dp_impl(dcp
);
2753 dsl_pool_config_exit(dp
, FTAG
);
2757 * Find objsets under and including ddobj, call func(ds) on each.
2758 * The order for the enumeration is completely undefined.
2759 * func is called with dsl_pool_config held.
2762 dmu_objset_find_dp(dsl_pool_t
*dp
, uint64_t ddobj
,
2763 int func(dsl_pool_t
*, dsl_dataset_t
*, void *), void *arg
, int flags
)
2768 dmu_objset_find_ctx_t
*dcp
;
2771 mutex_init(&err_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
2772 dcp
= kmem_alloc(sizeof (*dcp
), KM_SLEEP
);
2775 dcp
->dc_ddobj
= ddobj
;
2776 dcp
->dc_ddname
= NULL
;
2777 dcp
->dc_func
= func
;
2779 dcp
->dc_flags
= flags
;
2780 dcp
->dc_error_lock
= &err_lock
;
2781 dcp
->dc_error
= &error
;
2783 if ((flags
& DS_FIND_SERIALIZE
) || dsl_pool_config_held_writer(dp
)) {
2785 * In case a write lock is held we can't make use of
2786 * parallelism, as down the stack of the worker threads
2787 * the lock is asserted via dsl_pool_config_held.
2788 * In case of a read lock this is solved by getting a read
2789 * lock in each worker thread, which isn't possible in case
2790 * of a writer lock. So we fall back to the synchronous path
2792 * In the future it might be possible to get some magic into
2793 * dsl_pool_config_held in a way that it returns true for
2794 * the worker threads so that a single lock held from this
2795 * thread suffices. For now, stay single threaded.
2797 dmu_objset_find_dp_impl(dcp
);
2798 mutex_destroy(&err_lock
);
2803 ntasks
= dmu_find_threads
;
2805 ntasks
= vdev_count_leaves(dp
->dp_spa
) * 4;
2806 tq
= taskq_create("dmu_objset_find", ntasks
, maxclsyspri
, ntasks
,
2809 kmem_free(dcp
, sizeof (*dcp
));
2810 mutex_destroy(&err_lock
);
2812 return (SET_ERROR(ENOMEM
));
2816 /* dcp will be freed by task */
2817 (void) taskq_dispatch(tq
, dmu_objset_find_dp_cb
, dcp
, TQ_SLEEP
);
2820 * PORTING: this code relies on the property of taskq_wait to wait
2821 * until no more tasks are queued and no more tasks are active. As
2822 * we always queue new tasks from within other tasks, task_wait
2823 * reliably waits for the full recursion to finish, even though we
2824 * enqueue new tasks after taskq_wait has been called.
2825 * On platforms other than illumos, taskq_wait may not have this
2830 mutex_destroy(&err_lock
);
2836 * Find all objsets under name, and for each, call 'func(child_name, arg)'.
2837 * The dp_config_rwlock must not be held when this is called, and it
2838 * will not be held when the callback is called.
2839 * Therefore this function should only be used when the pool is not changing
2840 * (e.g. in syncing context), or the callback can deal with the possible races.
2843 dmu_objset_find_impl(spa_t
*spa
, const char *name
,
2844 int func(const char *, void *), void *arg
, int flags
)
2847 dsl_pool_t
*dp
= spa_get_dsl(spa
);
2850 zap_attribute_t
*attr
;
2855 dsl_pool_config_enter(dp
, FTAG
);
2857 err
= dsl_dir_hold(dp
, name
, FTAG
, &dd
, NULL
);
2859 dsl_pool_config_exit(dp
, FTAG
);
2863 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
2864 if (dd
->dd_myname
[0] == '$') {
2865 dsl_dir_rele(dd
, FTAG
);
2866 dsl_pool_config_exit(dp
, FTAG
);
2870 thisobj
= dsl_dir_phys(dd
)->dd_head_dataset_obj
;
2871 attr
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
2874 * Iterate over all children.
2876 if (flags
& DS_FIND_CHILDREN
) {
2877 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
,
2878 dsl_dir_phys(dd
)->dd_child_dir_zapobj
);
2879 zap_cursor_retrieve(&zc
, attr
) == 0;
2880 (void) zap_cursor_advance(&zc
)) {
2881 ASSERT3U(attr
->za_integer_length
, ==,
2883 ASSERT3U(attr
->za_num_integers
, ==, 1);
2885 child
= kmem_asprintf("%s/%s", name
, attr
->za_name
);
2886 dsl_pool_config_exit(dp
, FTAG
);
2887 err
= dmu_objset_find_impl(spa
, child
,
2889 dsl_pool_config_enter(dp
, FTAG
);
2894 zap_cursor_fini(&zc
);
2897 dsl_dir_rele(dd
, FTAG
);
2898 dsl_pool_config_exit(dp
, FTAG
);
2899 kmem_free(attr
, sizeof (zap_attribute_t
));
2905 * Iterate over all snapshots.
2907 if (flags
& DS_FIND_SNAPSHOTS
) {
2908 err
= dsl_dataset_hold_obj(dp
, thisobj
, FTAG
, &ds
);
2913 snapobj
= dsl_dataset_phys(ds
)->ds_snapnames_zapobj
;
2914 dsl_dataset_rele(ds
, FTAG
);
2916 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
, snapobj
);
2917 zap_cursor_retrieve(&zc
, attr
) == 0;
2918 (void) zap_cursor_advance(&zc
)) {
2919 ASSERT3U(attr
->za_integer_length
, ==,
2921 ASSERT3U(attr
->za_num_integers
, ==, 1);
2923 child
= kmem_asprintf("%s@%s",
2924 name
, attr
->za_name
);
2925 dsl_pool_config_exit(dp
, FTAG
);
2926 err
= func(child
, arg
);
2927 dsl_pool_config_enter(dp
, FTAG
);
2932 zap_cursor_fini(&zc
);
2936 dsl_dir_rele(dd
, FTAG
);
2937 kmem_free(attr
, sizeof (zap_attribute_t
));
2938 dsl_pool_config_exit(dp
, FTAG
);
2943 /* Apply to self. */
2944 return (func(name
, arg
));
2948 * See comment above dmu_objset_find_impl().
2951 dmu_objset_find(char *name
, int func(const char *, void *), void *arg
,
2957 error
= spa_open(name
, &spa
, FTAG
);
2960 error
= dmu_objset_find_impl(spa
, name
, func
, arg
, flags
);
2961 spa_close(spa
, FTAG
);
2966 dmu_objset_incompatible_encryption_version(objset_t
*os
)
2968 return (dsl_dir_incompatible_encryption_version(
2969 os
->os_dsl_dataset
->ds_dir
));
2973 dmu_objset_set_user(objset_t
*os
, void *user_ptr
)
2975 ASSERT(MUTEX_HELD(&os
->os_user_ptr_lock
));
2976 os
->os_user_ptr
= user_ptr
;
2980 dmu_objset_get_user(objset_t
*os
)
2982 ASSERT(MUTEX_HELD(&os
->os_user_ptr_lock
));
2983 return (os
->os_user_ptr
);
2987 * Determine name of filesystem, given name of snapshot.
2988 * buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes
2991 dmu_fsname(const char *snapname
, char *buf
)
2993 char *atp
= strchr(snapname
, '@');
2995 return (SET_ERROR(EINVAL
));
2996 if (atp
- snapname
>= ZFS_MAX_DATASET_NAME_LEN
)
2997 return (SET_ERROR(ENAMETOOLONG
));
2998 (void) strlcpy(buf
, snapname
, atp
- snapname
+ 1);
3003 * Call when we think we're going to write/free space in open context to track
3004 * the amount of dirty data in the open txg, which is also the amount
3005 * of memory that can not be evicted until this txg syncs.
3008 dmu_objset_willuse_space(objset_t
*os
, int64_t space
, dmu_tx_t
*tx
)
3010 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
3011 int64_t aspace
= spa_get_worst_case_asize(os
->os_spa
, space
);
3014 dsl_dir_willuse_space(ds
->ds_dir
, aspace
, tx
);
3015 dsl_pool_dirty_space(dmu_tx_pool(tx
), space
, tx
);
3019 #if defined(_KERNEL)
3020 EXPORT_SYMBOL(dmu_objset_zil
);
3021 EXPORT_SYMBOL(dmu_objset_pool
);
3022 EXPORT_SYMBOL(dmu_objset_ds
);
3023 EXPORT_SYMBOL(dmu_objset_type
);
3024 EXPORT_SYMBOL(dmu_objset_name
);
3025 EXPORT_SYMBOL(dmu_objset_hold
);
3026 EXPORT_SYMBOL(dmu_objset_hold_flags
);
3027 EXPORT_SYMBOL(dmu_objset_own
);
3028 EXPORT_SYMBOL(dmu_objset_rele
);
3029 EXPORT_SYMBOL(dmu_objset_rele_flags
);
3030 EXPORT_SYMBOL(dmu_objset_disown
);
3031 EXPORT_SYMBOL(dmu_objset_from_ds
);
3032 EXPORT_SYMBOL(dmu_objset_create
);
3033 EXPORT_SYMBOL(dmu_objset_clone
);
3034 EXPORT_SYMBOL(dmu_objset_stats
);
3035 EXPORT_SYMBOL(dmu_objset_fast_stat
);
3036 EXPORT_SYMBOL(dmu_objset_spa
);
3037 EXPORT_SYMBOL(dmu_objset_space
);
3038 EXPORT_SYMBOL(dmu_objset_fsid_guid
);
3039 EXPORT_SYMBOL(dmu_objset_find
);
3040 EXPORT_SYMBOL(dmu_objset_byteswap
);
3041 EXPORT_SYMBOL(dmu_objset_evict_dbufs
);
3042 EXPORT_SYMBOL(dmu_objset_snap_cmtime
);
3043 EXPORT_SYMBOL(dmu_objset_dnodesize
);
3045 EXPORT_SYMBOL(dmu_objset_sync
);
3046 EXPORT_SYMBOL(dmu_objset_is_dirty
);
3047 EXPORT_SYMBOL(dmu_objset_create_impl_dnstats
);
3048 EXPORT_SYMBOL(dmu_objset_create_impl
);
3049 EXPORT_SYMBOL(dmu_objset_open_impl
);
3050 EXPORT_SYMBOL(dmu_objset_evict
);
3051 EXPORT_SYMBOL(dmu_objset_register_type
);
3052 EXPORT_SYMBOL(dmu_objset_do_userquota_updates
);
3053 EXPORT_SYMBOL(dmu_objset_userquota_get_ids
);
3054 EXPORT_SYMBOL(dmu_objset_userused_enabled
);
3055 EXPORT_SYMBOL(dmu_objset_userspace_upgrade
);
3056 EXPORT_SYMBOL(dmu_objset_userspace_present
);
3057 EXPORT_SYMBOL(dmu_objset_userobjused_enabled
);
3058 EXPORT_SYMBOL(dmu_objset_userobjspace_upgradable
);
3059 EXPORT_SYMBOL(dmu_objset_userobjspace_present
);
3060 EXPORT_SYMBOL(dmu_objset_projectquota_enabled
);
3061 EXPORT_SYMBOL(dmu_objset_projectquota_present
);
3062 EXPORT_SYMBOL(dmu_objset_projectquota_upgradable
);
3063 EXPORT_SYMBOL(dmu_objset_id_quota_upgrade
);