4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
25 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
26 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
28 * Copyright (c) 2015, STRATO AG, Inc. All rights reserved.
29 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
30 * Copyright 2017 Nexenta Systems, Inc.
31 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
34 /* Portions Copyright 2010 Robert Milkowski */
36 #include <sys/zfeature.h>
38 #include <sys/zfs_context.h>
39 #include <sys/dmu_objset.h>
40 #include <sys/dsl_dir.h>
41 #include <sys/dsl_dataset.h>
42 #include <sys/dsl_prop.h>
43 #include <sys/dsl_pool.h>
44 #include <sys/dsl_synctask.h>
45 #include <sys/dsl_deleg.h>
46 #include <sys/dnode.h>
49 #include <sys/dmu_tx.h>
52 #include <sys/dmu_impl.h>
53 #include <sys/zfs_ioctl.h>
55 #include <sys/zfs_onexit.h>
56 #include <sys/dsl_destroy.h>
58 #include <sys/zfeature.h>
59 #include <sys/policy.h>
60 #include <sys/spa_impl.h>
61 #include <sys/dmu_send.h>
62 #include <sys/zfs_project.h>
63 #include "zfs_namecheck.h"
66 * Needed to close a window in dnode_move() that allows the objset to be freed
67 * before it can be safely accessed.
72 * Tunable to overwrite the maximum number of threads for the parallelization
73 * of dmu_objset_find_dp, needed to speed up the import of pools with many
75 * Default is 4 times the number of leaf vdevs.
77 int dmu_find_threads
= 0;
80 * Backfill lower metadnode objects after this many have been freed.
81 * Backfilling negatively impacts object creation rates, so only do it
82 * if there are enough holes to fill.
84 int dmu_rescan_dnode_threshold
= 1 << DN_MAX_INDBLKSHIFT
;
86 static char *upgrade_tag
= "upgrade_tag";
88 static void dmu_objset_find_dp_cb(void *arg
);
90 static void dmu_objset_upgrade(objset_t
*os
, dmu_objset_upgrade_cb_t cb
);
91 static void dmu_objset_upgrade_stop(objset_t
*os
);
96 rw_init(&os_lock
, NULL
, RW_DEFAULT
, NULL
);
100 dmu_objset_fini(void)
102 rw_destroy(&os_lock
);
106 dmu_objset_spa(objset_t
*os
)
112 dmu_objset_zil(objset_t
*os
)
118 dmu_objset_pool(objset_t
*os
)
122 if ((ds
= os
->os_dsl_dataset
) != NULL
&& ds
->ds_dir
)
123 return (ds
->ds_dir
->dd_pool
);
125 return (spa_get_dsl(os
->os_spa
));
129 dmu_objset_ds(objset_t
*os
)
131 return (os
->os_dsl_dataset
);
135 dmu_objset_type(objset_t
*os
)
137 return (os
->os_phys
->os_type
);
141 dmu_objset_name(objset_t
*os
, char *buf
)
143 dsl_dataset_name(os
->os_dsl_dataset
, buf
);
147 dmu_objset_id(objset_t
*os
)
149 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
151 return (ds
? ds
->ds_object
: 0);
155 dmu_objset_dnodesize(objset_t
*os
)
157 return (os
->os_dnodesize
);
161 dmu_objset_syncprop(objset_t
*os
)
163 return (os
->os_sync
);
167 dmu_objset_logbias(objset_t
*os
)
169 return (os
->os_logbias
);
173 checksum_changed_cb(void *arg
, uint64_t newval
)
178 * Inheritance should have been done by now.
180 ASSERT(newval
!= ZIO_CHECKSUM_INHERIT
);
182 os
->os_checksum
= zio_checksum_select(newval
, ZIO_CHECKSUM_ON_VALUE
);
186 compression_changed_cb(void *arg
, uint64_t newval
)
191 * Inheritance and range checking should have been done by now.
193 ASSERT(newval
!= ZIO_COMPRESS_INHERIT
);
195 os
->os_compress
= zio_compress_select(os
->os_spa
, newval
,
200 copies_changed_cb(void *arg
, uint64_t newval
)
205 * Inheritance and range checking should have been done by now.
208 ASSERT(newval
<= spa_max_replication(os
->os_spa
));
210 os
->os_copies
= newval
;
214 dedup_changed_cb(void *arg
, uint64_t newval
)
217 spa_t
*spa
= os
->os_spa
;
218 enum zio_checksum checksum
;
221 * Inheritance should have been done by now.
223 ASSERT(newval
!= ZIO_CHECKSUM_INHERIT
);
225 checksum
= zio_checksum_dedup_select(spa
, newval
, ZIO_CHECKSUM_OFF
);
227 os
->os_dedup_checksum
= checksum
& ZIO_CHECKSUM_MASK
;
228 os
->os_dedup_verify
= !!(checksum
& ZIO_CHECKSUM_VERIFY
);
232 primary_cache_changed_cb(void *arg
, uint64_t newval
)
237 * Inheritance and range checking should have been done by now.
239 ASSERT(newval
== ZFS_CACHE_ALL
|| newval
== ZFS_CACHE_NONE
||
240 newval
== ZFS_CACHE_METADATA
);
242 os
->os_primary_cache
= newval
;
246 secondary_cache_changed_cb(void *arg
, uint64_t newval
)
251 * Inheritance and range checking should have been done by now.
253 ASSERT(newval
== ZFS_CACHE_ALL
|| newval
== ZFS_CACHE_NONE
||
254 newval
== ZFS_CACHE_METADATA
);
256 os
->os_secondary_cache
= newval
;
260 sync_changed_cb(void *arg
, uint64_t newval
)
265 * Inheritance and range checking should have been done by now.
267 ASSERT(newval
== ZFS_SYNC_STANDARD
|| newval
== ZFS_SYNC_ALWAYS
||
268 newval
== ZFS_SYNC_DISABLED
);
270 os
->os_sync
= newval
;
272 zil_set_sync(os
->os_zil
, newval
);
276 redundant_metadata_changed_cb(void *arg
, uint64_t newval
)
281 * Inheritance and range checking should have been done by now.
283 ASSERT(newval
== ZFS_REDUNDANT_METADATA_ALL
||
284 newval
== ZFS_REDUNDANT_METADATA_MOST
);
286 os
->os_redundant_metadata
= newval
;
290 dnodesize_changed_cb(void *arg
, uint64_t newval
)
295 case ZFS_DNSIZE_LEGACY
:
296 os
->os_dnodesize
= DNODE_MIN_SIZE
;
298 case ZFS_DNSIZE_AUTO
:
300 * Choose a dnode size that will work well for most
301 * workloads if the user specified "auto". Future code
302 * improvements could dynamically select a dnode size
303 * based on observed workload patterns.
305 os
->os_dnodesize
= DNODE_MIN_SIZE
* 2;
312 os
->os_dnodesize
= newval
;
318 smallblk_changed_cb(void *arg
, uint64_t newval
)
323 * Inheritance and range checking should have been done by now.
325 ASSERT(newval
<= SPA_OLD_MAXBLOCKSIZE
);
326 ASSERT(ISP2(newval
));
328 os
->os_zpl_special_smallblock
= newval
;
332 logbias_changed_cb(void *arg
, uint64_t newval
)
336 ASSERT(newval
== ZFS_LOGBIAS_LATENCY
||
337 newval
== ZFS_LOGBIAS_THROUGHPUT
);
338 os
->os_logbias
= newval
;
340 zil_set_logbias(os
->os_zil
, newval
);
344 recordsize_changed_cb(void *arg
, uint64_t newval
)
348 os
->os_recordsize
= newval
;
352 dmu_objset_byteswap(void *buf
, size_t size
)
354 objset_phys_t
*osp
= buf
;
356 ASSERT(size
== OBJSET_PHYS_SIZE_V1
|| size
== OBJSET_PHYS_SIZE_V2
||
357 size
== sizeof (objset_phys_t
));
358 dnode_byteswap(&osp
->os_meta_dnode
);
359 byteswap_uint64_array(&osp
->os_zil_header
, sizeof (zil_header_t
));
360 osp
->os_type
= BSWAP_64(osp
->os_type
);
361 osp
->os_flags
= BSWAP_64(osp
->os_flags
);
362 if (size
>= OBJSET_PHYS_SIZE_V2
) {
363 dnode_byteswap(&osp
->os_userused_dnode
);
364 dnode_byteswap(&osp
->os_groupused_dnode
);
365 if (size
>= sizeof (objset_phys_t
))
366 dnode_byteswap(&osp
->os_projectused_dnode
);
371 * The hash is a CRC-based hash of the objset_t pointer and the object number.
374 dnode_hash(const objset_t
*os
, uint64_t obj
)
376 uintptr_t osv
= (uintptr_t)os
;
377 uint64_t crc
= -1ULL;
379 ASSERT(zfs_crc64_table
[128] == ZFS_CRC64_POLY
);
381 * The low 6 bits of the pointer don't have much entropy, because
382 * the objset_t is larger than 2^6 bytes long.
384 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (osv
>> 6)) & 0xFF];
385 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 0)) & 0xFF];
386 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 8)) & 0xFF];
387 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 16)) & 0xFF];
389 crc
^= (osv
>>14) ^ (obj
>>24);
395 dnode_multilist_index_func(multilist_t
*ml
, void *obj
)
398 return (dnode_hash(dn
->dn_objset
, dn
->dn_object
) %
399 multilist_get_num_sublists(ml
));
403 * Instantiates the objset_t in-memory structure corresponding to the
404 * objset_phys_t that's pointed to by the specified blkptr_t.
407 dmu_objset_open_impl(spa_t
*spa
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
413 ASSERT(ds
== NULL
|| MUTEX_HELD(&ds
->ds_opening_lock
));
416 * The $ORIGIN dataset (if it exists) doesn't have an associated
417 * objset, so there's no reason to open it. The $ORIGIN dataset
418 * will not exist on pools older than SPA_VERSION_ORIGIN.
420 if (ds
!= NULL
&& spa_get_dsl(spa
) != NULL
&&
421 spa_get_dsl(spa
)->dp_origin_snap
!= NULL
) {
422 ASSERT3P(ds
->ds_dir
, !=,
423 spa_get_dsl(spa
)->dp_origin_snap
->ds_dir
);
426 os
= kmem_zalloc(sizeof (objset_t
), KM_SLEEP
);
427 os
->os_dsl_dataset
= ds
;
430 if (!BP_IS_HOLE(os
->os_rootbp
)) {
431 arc_flags_t aflags
= ARC_FLAG_WAIT
;
434 enum zio_flag zio_flags
= ZIO_FLAG_CANFAIL
;
435 SET_BOOKMARK(&zb
, ds
? ds
->ds_object
: DMU_META_OBJSET
,
436 ZB_ROOT_OBJECT
, ZB_ROOT_LEVEL
, ZB_ROOT_BLKID
);
438 if (DMU_OS_IS_L2CACHEABLE(os
))
439 aflags
|= ARC_FLAG_L2CACHE
;
441 if (ds
!= NULL
&& ds
->ds_dir
->dd_crypto_obj
!= 0) {
442 ASSERT3U(BP_GET_COMPRESS(bp
), ==, ZIO_COMPRESS_OFF
);
443 ASSERT(BP_IS_AUTHENTICATED(bp
));
444 zio_flags
|= ZIO_FLAG_RAW
;
447 dprintf_bp(os
->os_rootbp
, "reading %s", "");
448 err
= arc_read(NULL
, spa
, os
->os_rootbp
,
449 arc_getbuf_func
, &os
->os_phys_buf
,
450 ZIO_PRIORITY_SYNC_READ
, zio_flags
, &aflags
, &zb
);
452 kmem_free(os
, sizeof (objset_t
));
453 /* convert checksum errors into IO errors */
455 err
= SET_ERROR(EIO
);
459 if (spa_version(spa
) < SPA_VERSION_USERSPACE
)
460 size
= OBJSET_PHYS_SIZE_V1
;
461 else if (!spa_feature_is_enabled(spa
,
462 SPA_FEATURE_PROJECT_QUOTA
))
463 size
= OBJSET_PHYS_SIZE_V2
;
465 size
= sizeof (objset_phys_t
);
467 /* Increase the blocksize if we are permitted. */
468 if (arc_buf_size(os
->os_phys_buf
) < size
) {
469 arc_buf_t
*buf
= arc_alloc_buf(spa
, &os
->os_phys_buf
,
470 ARC_BUFC_METADATA
, size
);
471 bzero(buf
->b_data
, size
);
472 bcopy(os
->os_phys_buf
->b_data
, buf
->b_data
,
473 arc_buf_size(os
->os_phys_buf
));
474 arc_buf_destroy(os
->os_phys_buf
, &os
->os_phys_buf
);
475 os
->os_phys_buf
= buf
;
478 os
->os_phys
= os
->os_phys_buf
->b_data
;
479 os
->os_flags
= os
->os_phys
->os_flags
;
481 int size
= spa_version(spa
) >= SPA_VERSION_USERSPACE
?
482 sizeof (objset_phys_t
) : OBJSET_PHYS_SIZE_V1
;
483 os
->os_phys_buf
= arc_alloc_buf(spa
, &os
->os_phys_buf
,
484 ARC_BUFC_METADATA
, size
);
485 os
->os_phys
= os
->os_phys_buf
->b_data
;
486 bzero(os
->os_phys
, size
);
489 * These properties will be filled in by the logic in zfs_get_zplprop()
490 * when they are queried for the first time.
492 os
->os_version
= OBJSET_PROP_UNINITIALIZED
;
493 os
->os_normalization
= OBJSET_PROP_UNINITIALIZED
;
494 os
->os_utf8only
= OBJSET_PROP_UNINITIALIZED
;
495 os
->os_casesensitivity
= OBJSET_PROP_UNINITIALIZED
;
498 * Note: the changed_cb will be called once before the register
499 * func returns, thus changing the checksum/compression from the
500 * default (fletcher2/off). Snapshots don't need to know about
501 * checksum/compression/copies.
504 boolean_t needlock
= B_FALSE
;
506 os
->os_encrypted
= (ds
->ds_dir
->dd_crypto_obj
!= 0);
509 * Note: it's valid to open the objset if the dataset is
510 * long-held, in which case the pool_config lock will not
513 if (!dsl_pool_config_held(dmu_objset_pool(os
))) {
515 dsl_pool_config_enter(dmu_objset_pool(os
), FTAG
);
518 err
= dsl_prop_register(ds
,
519 zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE
),
520 primary_cache_changed_cb
, os
);
522 err
= dsl_prop_register(ds
,
523 zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE
),
524 secondary_cache_changed_cb
, os
);
526 if (!ds
->ds_is_snapshot
) {
528 err
= dsl_prop_register(ds
,
529 zfs_prop_to_name(ZFS_PROP_CHECKSUM
),
530 checksum_changed_cb
, os
);
533 err
= dsl_prop_register(ds
,
534 zfs_prop_to_name(ZFS_PROP_COMPRESSION
),
535 compression_changed_cb
, os
);
538 err
= dsl_prop_register(ds
,
539 zfs_prop_to_name(ZFS_PROP_COPIES
),
540 copies_changed_cb
, os
);
543 err
= dsl_prop_register(ds
,
544 zfs_prop_to_name(ZFS_PROP_DEDUP
),
545 dedup_changed_cb
, os
);
548 err
= dsl_prop_register(ds
,
549 zfs_prop_to_name(ZFS_PROP_LOGBIAS
),
550 logbias_changed_cb
, os
);
553 err
= dsl_prop_register(ds
,
554 zfs_prop_to_name(ZFS_PROP_SYNC
),
555 sync_changed_cb
, os
);
558 err
= dsl_prop_register(ds
,
560 ZFS_PROP_REDUNDANT_METADATA
),
561 redundant_metadata_changed_cb
, os
);
564 err
= dsl_prop_register(ds
,
565 zfs_prop_to_name(ZFS_PROP_RECORDSIZE
),
566 recordsize_changed_cb
, os
);
569 err
= dsl_prop_register(ds
,
570 zfs_prop_to_name(ZFS_PROP_DNODESIZE
),
571 dnodesize_changed_cb
, os
);
574 err
= dsl_prop_register(ds
,
576 ZFS_PROP_SPECIAL_SMALL_BLOCKS
),
577 smallblk_changed_cb
, os
);
581 dsl_pool_config_exit(dmu_objset_pool(os
), FTAG
);
583 arc_buf_destroy(os
->os_phys_buf
, &os
->os_phys_buf
);
584 kmem_free(os
, sizeof (objset_t
));
588 /* It's the meta-objset. */
589 os
->os_checksum
= ZIO_CHECKSUM_FLETCHER_4
;
590 os
->os_compress
= ZIO_COMPRESS_ON
;
591 os
->os_encrypted
= B_FALSE
;
592 os
->os_copies
= spa_max_replication(spa
);
593 os
->os_dedup_checksum
= ZIO_CHECKSUM_OFF
;
594 os
->os_dedup_verify
= B_FALSE
;
595 os
->os_logbias
= ZFS_LOGBIAS_LATENCY
;
596 os
->os_sync
= ZFS_SYNC_STANDARD
;
597 os
->os_primary_cache
= ZFS_CACHE_ALL
;
598 os
->os_secondary_cache
= ZFS_CACHE_ALL
;
599 os
->os_dnodesize
= DNODE_MIN_SIZE
;
602 if (ds
== NULL
|| !ds
->ds_is_snapshot
)
603 os
->os_zil_header
= os
->os_phys
->os_zil_header
;
604 os
->os_zil
= zil_alloc(os
, &os
->os_zil_header
);
606 for (i
= 0; i
< TXG_SIZE
; i
++) {
607 os
->os_dirty_dnodes
[i
] = multilist_create(sizeof (dnode_t
),
608 offsetof(dnode_t
, dn_dirty_link
[i
]),
609 dnode_multilist_index_func
);
611 list_create(&os
->os_dnodes
, sizeof (dnode_t
),
612 offsetof(dnode_t
, dn_link
));
613 list_create(&os
->os_downgraded_dbufs
, sizeof (dmu_buf_impl_t
),
614 offsetof(dmu_buf_impl_t
, db_link
));
616 list_link_init(&os
->os_evicting_node
);
618 mutex_init(&os
->os_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
619 mutex_init(&os
->os_userused_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
620 mutex_init(&os
->os_obj_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
621 mutex_init(&os
->os_user_ptr_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
622 os
->os_obj_next_percpu_len
= boot_ncpus
;
623 os
->os_obj_next_percpu
= kmem_zalloc(os
->os_obj_next_percpu_len
*
624 sizeof (os
->os_obj_next_percpu
[0]), KM_SLEEP
);
626 dnode_special_open(os
, &os
->os_phys
->os_meta_dnode
,
627 DMU_META_DNODE_OBJECT
, &os
->os_meta_dnode
);
628 if (OBJSET_BUF_HAS_USERUSED(os
->os_phys_buf
)) {
629 dnode_special_open(os
, &os
->os_phys
->os_userused_dnode
,
630 DMU_USERUSED_OBJECT
, &os
->os_userused_dnode
);
631 dnode_special_open(os
, &os
->os_phys
->os_groupused_dnode
,
632 DMU_GROUPUSED_OBJECT
, &os
->os_groupused_dnode
);
633 if (OBJSET_BUF_HAS_PROJECTUSED(os
->os_phys_buf
))
634 dnode_special_open(os
,
635 &os
->os_phys
->os_projectused_dnode
,
636 DMU_PROJECTUSED_OBJECT
, &os
->os_projectused_dnode
);
639 mutex_init(&os
->os_upgrade_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
646 dmu_objset_from_ds(dsl_dataset_t
*ds
, objset_t
**osp
)
651 * We shouldn't be doing anything with dsl_dataset_t's unless the
652 * pool_config lock is held, or the dataset is long-held.
654 ASSERT(dsl_pool_config_held(ds
->ds_dir
->dd_pool
) ||
655 dsl_dataset_long_held(ds
));
657 mutex_enter(&ds
->ds_opening_lock
);
658 if (ds
->ds_objset
== NULL
) {
660 rrw_enter(&ds
->ds_bp_rwlock
, RW_READER
, FTAG
);
661 err
= dmu_objset_open_impl(dsl_dataset_get_spa(ds
),
662 ds
, dsl_dataset_get_blkptr(ds
), &os
);
663 rrw_exit(&ds
->ds_bp_rwlock
, FTAG
);
666 mutex_enter(&ds
->ds_lock
);
667 ASSERT(ds
->ds_objset
== NULL
);
669 mutex_exit(&ds
->ds_lock
);
672 *osp
= ds
->ds_objset
;
673 mutex_exit(&ds
->ds_opening_lock
);
678 * Holds the pool while the objset is held. Therefore only one objset
679 * can be held at a time.
682 dmu_objset_hold_flags(const char *name
, boolean_t decrypt
, void *tag
,
688 ds_hold_flags_t flags
= (decrypt
) ? DS_HOLD_FLAG_DECRYPT
: 0;
690 err
= dsl_pool_hold(name
, tag
, &dp
);
693 err
= dsl_dataset_hold_flags(dp
, name
, flags
, tag
, &ds
);
695 dsl_pool_rele(dp
, tag
);
699 err
= dmu_objset_from_ds(ds
, osp
);
701 dsl_dataset_rele(ds
, tag
);
702 dsl_pool_rele(dp
, tag
);
709 dmu_objset_hold(const char *name
, void *tag
, objset_t
**osp
)
711 return (dmu_objset_hold_flags(name
, B_FALSE
, tag
, osp
));
715 dmu_objset_own_impl(dsl_dataset_t
*ds
, dmu_objset_type_t type
,
716 boolean_t readonly
, boolean_t decrypt
, void *tag
, objset_t
**osp
)
720 err
= dmu_objset_from_ds(ds
, osp
);
723 } else if (type
!= DMU_OST_ANY
&& type
!= (*osp
)->os_phys
->os_type
) {
724 return (SET_ERROR(EINVAL
));
725 } else if (!readonly
&& dsl_dataset_is_snapshot(ds
)) {
726 return (SET_ERROR(EROFS
));
727 } else if (!readonly
&& decrypt
&&
728 dsl_dir_incompatible_encryption_version(ds
->ds_dir
)) {
729 return (SET_ERROR(EROFS
));
732 /* if we are decrypting, we can now check MACs in os->os_phys_buf */
733 if (decrypt
&& arc_is_unauthenticated((*osp
)->os_phys_buf
)) {
736 SET_BOOKMARK(&zb
, ds
->ds_object
, ZB_ROOT_OBJECT
,
737 ZB_ROOT_LEVEL
, ZB_ROOT_BLKID
);
738 err
= arc_untransform((*osp
)->os_phys_buf
, (*osp
)->os_spa
,
743 ASSERT0(arc_is_unauthenticated((*osp
)->os_phys_buf
));
750 * dsl_pool must not be held when this is called.
751 * Upon successful return, there will be a longhold on the dataset,
752 * and the dsl_pool will not be held.
755 dmu_objset_own(const char *name
, dmu_objset_type_t type
,
756 boolean_t readonly
, boolean_t decrypt
, void *tag
, objset_t
**osp
)
761 ds_hold_flags_t flags
= (decrypt
) ? DS_HOLD_FLAG_DECRYPT
: 0;
763 err
= dsl_pool_hold(name
, FTAG
, &dp
);
766 err
= dsl_dataset_own(dp
, name
, flags
, tag
, &ds
);
768 dsl_pool_rele(dp
, FTAG
);
771 err
= dmu_objset_own_impl(ds
, type
, readonly
, decrypt
, tag
, osp
);
773 dsl_dataset_disown(ds
, flags
, tag
);
774 dsl_pool_rele(dp
, FTAG
);
779 * User accounting requires the dataset to be decrypted and rw.
780 * We also don't begin user accounting during claiming to help
781 * speed up pool import times and to keep this txg reserved
782 * completely for recovery work.
784 if ((dmu_objset_userobjspace_upgradable(*osp
) ||
785 dmu_objset_projectquota_upgradable(*osp
)) &&
786 !readonly
&& !dp
->dp_spa
->spa_claiming
&&
787 (ds
->ds_dir
->dd_crypto_obj
== 0 || decrypt
))
788 dmu_objset_id_quota_upgrade(*osp
);
790 dsl_pool_rele(dp
, FTAG
);
795 dmu_objset_own_obj(dsl_pool_t
*dp
, uint64_t obj
, dmu_objset_type_t type
,
796 boolean_t readonly
, boolean_t decrypt
, void *tag
, objset_t
**osp
)
800 ds_hold_flags_t flags
= (decrypt
) ? DS_HOLD_FLAG_DECRYPT
: 0;
802 err
= dsl_dataset_own_obj(dp
, obj
, flags
, tag
, &ds
);
806 err
= dmu_objset_own_impl(ds
, type
, readonly
, decrypt
, tag
, osp
);
808 dsl_dataset_disown(ds
, flags
, tag
);
816 dmu_objset_rele_flags(objset_t
*os
, boolean_t decrypt
, void *tag
)
818 ds_hold_flags_t flags
= (decrypt
) ? DS_HOLD_FLAG_DECRYPT
: 0;
820 dsl_pool_t
*dp
= dmu_objset_pool(os
);
821 dsl_dataset_rele_flags(os
->os_dsl_dataset
, flags
, tag
);
822 dsl_pool_rele(dp
, tag
);
826 dmu_objset_rele(objset_t
*os
, void *tag
)
828 dmu_objset_rele_flags(os
, B_FALSE
, tag
);
832 * When we are called, os MUST refer to an objset associated with a dataset
833 * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner
834 * == tag. We will then release and reacquire ownership of the dataset while
835 * holding the pool config_rwlock to avoid intervening namespace or ownership
838 * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to
839 * release the hold on its dataset and acquire a new one on the dataset of the
840 * same name so that it can be partially torn down and reconstructed.
843 dmu_objset_refresh_ownership(dsl_dataset_t
*ds
, dsl_dataset_t
**newds
,
844 boolean_t decrypt
, void *tag
)
847 char name
[ZFS_MAX_DATASET_NAME_LEN
];
849 VERIFY3P(ds
, !=, NULL
);
850 VERIFY3P(ds
->ds_owner
, ==, tag
);
851 VERIFY(dsl_dataset_long_held(ds
));
853 dsl_dataset_name(ds
, name
);
854 dp
= ds
->ds_dir
->dd_pool
;
855 dsl_pool_config_enter(dp
, FTAG
);
856 dsl_dataset_disown(ds
, decrypt
, tag
);
857 VERIFY0(dsl_dataset_own(dp
, name
,
858 (decrypt
) ? DS_HOLD_FLAG_DECRYPT
: 0, tag
, newds
));
859 dsl_pool_config_exit(dp
, FTAG
);
863 dmu_objset_disown(objset_t
*os
, boolean_t decrypt
, void *tag
)
866 * Stop upgrading thread
868 dmu_objset_upgrade_stop(os
);
869 dsl_dataset_disown(os
->os_dsl_dataset
,
870 (decrypt
) ? DS_HOLD_FLAG_DECRYPT
: 0, tag
);
874 dmu_objset_evict_dbufs(objset_t
*os
)
879 dn_marker
= kmem_alloc(sizeof (dnode_t
), KM_SLEEP
);
881 mutex_enter(&os
->os_lock
);
882 dn
= list_head(&os
->os_dnodes
);
885 * Skip dnodes without holds. We have to do this dance
886 * because dnode_add_ref() only works if there is already a
887 * hold. If the dnode has no holds, then it has no dbufs.
889 if (dnode_add_ref(dn
, FTAG
)) {
890 list_insert_after(&os
->os_dnodes
, dn
, dn_marker
);
891 mutex_exit(&os
->os_lock
);
893 dnode_evict_dbufs(dn
);
894 dnode_rele(dn
, FTAG
);
896 mutex_enter(&os
->os_lock
);
897 dn
= list_next(&os
->os_dnodes
, dn_marker
);
898 list_remove(&os
->os_dnodes
, dn_marker
);
900 dn
= list_next(&os
->os_dnodes
, dn
);
903 mutex_exit(&os
->os_lock
);
905 kmem_free(dn_marker
, sizeof (dnode_t
));
907 if (DMU_USERUSED_DNODE(os
) != NULL
) {
908 if (DMU_PROJECTUSED_DNODE(os
) != NULL
)
909 dnode_evict_dbufs(DMU_PROJECTUSED_DNODE(os
));
910 dnode_evict_dbufs(DMU_GROUPUSED_DNODE(os
));
911 dnode_evict_dbufs(DMU_USERUSED_DNODE(os
));
913 dnode_evict_dbufs(DMU_META_DNODE(os
));
917 * Objset eviction processing is split into into two pieces.
918 * The first marks the objset as evicting, evicts any dbufs that
919 * have a refcount of zero, and then queues up the objset for the
920 * second phase of eviction. Once os->os_dnodes has been cleared by
921 * dnode_buf_pageout()->dnode_destroy(), the second phase is executed.
922 * The second phase closes the special dnodes, dequeues the objset from
923 * the list of those undergoing eviction, and finally frees the objset.
925 * NOTE: Due to asynchronous eviction processing (invocation of
926 * dnode_buf_pageout()), it is possible for the meta dnode for the
927 * objset to have no holds even though os->os_dnodes is not empty.
930 dmu_objset_evict(objset_t
*os
)
932 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
934 for (int t
= 0; t
< TXG_SIZE
; t
++)
935 ASSERT(!dmu_objset_is_dirty(os
, t
));
938 dsl_prop_unregister_all(ds
, os
);
943 dmu_objset_evict_dbufs(os
);
945 mutex_enter(&os
->os_lock
);
946 spa_evicting_os_register(os
->os_spa
, os
);
947 if (list_is_empty(&os
->os_dnodes
)) {
948 mutex_exit(&os
->os_lock
);
949 dmu_objset_evict_done(os
);
951 mutex_exit(&os
->os_lock
);
958 dmu_objset_evict_done(objset_t
*os
)
960 ASSERT3P(list_head(&os
->os_dnodes
), ==, NULL
);
962 dnode_special_close(&os
->os_meta_dnode
);
963 if (DMU_USERUSED_DNODE(os
)) {
964 if (DMU_PROJECTUSED_DNODE(os
))
965 dnode_special_close(&os
->os_projectused_dnode
);
966 dnode_special_close(&os
->os_userused_dnode
);
967 dnode_special_close(&os
->os_groupused_dnode
);
969 zil_free(os
->os_zil
);
971 arc_buf_destroy(os
->os_phys_buf
, &os
->os_phys_buf
);
974 * This is a barrier to prevent the objset from going away in
975 * dnode_move() until we can safely ensure that the objset is still in
976 * use. We consider the objset valid before the barrier and invalid
979 rw_enter(&os_lock
, RW_READER
);
982 kmem_free(os
->os_obj_next_percpu
,
983 os
->os_obj_next_percpu_len
* sizeof (os
->os_obj_next_percpu
[0]));
985 mutex_destroy(&os
->os_lock
);
986 mutex_destroy(&os
->os_userused_lock
);
987 mutex_destroy(&os
->os_obj_lock
);
988 mutex_destroy(&os
->os_user_ptr_lock
);
989 mutex_destroy(&os
->os_upgrade_lock
);
990 for (int i
= 0; i
< TXG_SIZE
; i
++) {
991 multilist_destroy(os
->os_dirty_dnodes
[i
]);
993 spa_evicting_os_deregister(os
->os_spa
, os
);
994 kmem_free(os
, sizeof (objset_t
));
998 dmu_objset_snap_cmtime(objset_t
*os
)
1000 return (dsl_dir_snap_cmtime(os
->os_dsl_dataset
->ds_dir
));
1004 dmu_objset_create_impl_dnstats(spa_t
*spa
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
1005 dmu_objset_type_t type
, int levels
, int blksz
, int ibs
, dmu_tx_t
*tx
)
1010 ASSERT(dmu_tx_is_syncing(tx
));
1013 blksz
= DNODE_BLOCK_SIZE
;
1015 ibs
= DN_MAX_INDBLKSHIFT
;
1018 VERIFY0(dmu_objset_from_ds(ds
, &os
));
1020 VERIFY0(dmu_objset_open_impl(spa
, NULL
, bp
, &os
));
1022 mdn
= DMU_META_DNODE(os
);
1024 dnode_allocate(mdn
, DMU_OT_DNODE
, blksz
, ibs
, DMU_OT_NONE
, 0,
1025 DNODE_MIN_SLOTS
, tx
);
1028 * We don't want to have to increase the meta-dnode's nlevels
1029 * later, because then we could do it in quescing context while
1030 * we are also accessing it in open context.
1032 * This precaution is not necessary for the MOS (ds == NULL),
1033 * because the MOS is only updated in syncing context.
1034 * This is most fortunate: the MOS is the only objset that
1035 * needs to be synced multiple times as spa_sync() iterates
1036 * to convergence, so minimizing its dn_nlevels matters.
1043 * Determine the number of levels necessary for the
1044 * meta-dnode to contain DN_MAX_OBJECT dnodes. Note
1045 * that in order to ensure that we do not overflow
1046 * 64 bits, there has to be a nlevels that gives us a
1047 * number of blocks > DN_MAX_OBJECT but < 2^64.
1048 * Therefore, (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)
1049 * (10) must be less than (64 - log2(DN_MAX_OBJECT))
1052 while ((uint64_t)mdn
->dn_nblkptr
<<
1053 (mdn
->dn_datablkshift
- DNODE_SHIFT
+ (levels
- 1) *
1054 (mdn
->dn_indblkshift
- SPA_BLKPTRSHIFT
)) <
1059 mdn
->dn_next_nlevels
[tx
->tx_txg
& TXG_MASK
] =
1060 mdn
->dn_nlevels
= levels
;
1063 ASSERT(type
!= DMU_OST_NONE
);
1064 ASSERT(type
!= DMU_OST_ANY
);
1065 ASSERT(type
< DMU_OST_NUMTYPES
);
1066 os
->os_phys
->os_type
= type
;
1069 * Enable user accounting if it is enabled and this is not an
1070 * encrypted receive.
1072 if (dmu_objset_userused_enabled(os
) &&
1073 (!os
->os_encrypted
|| !dmu_objset_is_receiving(os
))) {
1074 os
->os_phys
->os_flags
|= OBJSET_FLAG_USERACCOUNTING_COMPLETE
;
1075 if (dmu_objset_userobjused_enabled(os
)) {
1076 ds
->ds_feature_activation_needed
[
1077 SPA_FEATURE_USEROBJ_ACCOUNTING
] = B_TRUE
;
1078 os
->os_phys
->os_flags
|=
1079 OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE
;
1081 if (dmu_objset_projectquota_enabled(os
)) {
1082 ds
->ds_feature_activation_needed
[
1083 SPA_FEATURE_PROJECT_QUOTA
] = B_TRUE
;
1084 os
->os_phys
->os_flags
|=
1085 OBJSET_FLAG_PROJECTQUOTA_COMPLETE
;
1087 os
->os_flags
= os
->os_phys
->os_flags
;
1090 dsl_dataset_dirty(ds
, tx
);
1095 /* called from dsl for meta-objset */
1097 dmu_objset_create_impl(spa_t
*spa
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
1098 dmu_objset_type_t type
, dmu_tx_t
*tx
)
1100 return (dmu_objset_create_impl_dnstats(spa
, ds
, bp
, type
, 0, 0, 0, tx
));
1103 typedef struct dmu_objset_create_arg
{
1104 const char *doca_name
;
1106 void (*doca_userfunc
)(objset_t
*os
, void *arg
,
1107 cred_t
*cr
, dmu_tx_t
*tx
);
1109 dmu_objset_type_t doca_type
;
1110 uint64_t doca_flags
;
1111 dsl_crypto_params_t
*doca_dcp
;
1112 } dmu_objset_create_arg_t
;
1116 dmu_objset_create_check(void *arg
, dmu_tx_t
*tx
)
1118 dmu_objset_create_arg_t
*doca
= arg
;
1119 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1124 if (strchr(doca
->doca_name
, '@') != NULL
)
1125 return (SET_ERROR(EINVAL
));
1127 if (strlen(doca
->doca_name
) >= ZFS_MAX_DATASET_NAME_LEN
)
1128 return (SET_ERROR(ENAMETOOLONG
));
1130 if (dataset_nestcheck(doca
->doca_name
) != 0)
1131 return (SET_ERROR(ENAMETOOLONG
));
1133 error
= dsl_dir_hold(dp
, doca
->doca_name
, FTAG
, &pdd
, &tail
);
1137 dsl_dir_rele(pdd
, FTAG
);
1138 return (SET_ERROR(EEXIST
));
1141 error
= dmu_objset_create_crypt_check(pdd
, doca
->doca_dcp
, NULL
);
1143 dsl_dir_rele(pdd
, FTAG
);
1147 error
= dsl_fs_ss_limit_check(pdd
, 1, ZFS_PROP_FILESYSTEM_LIMIT
, NULL
,
1150 dsl_dir_rele(pdd
, FTAG
);
1156 dmu_objset_create_sync(void *arg
, dmu_tx_t
*tx
)
1158 dmu_objset_create_arg_t
*doca
= arg
;
1159 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1168 VERIFY0(dsl_dir_hold(dp
, doca
->doca_name
, FTAG
, &pdd
, &tail
));
1170 obj
= dsl_dataset_create_sync(pdd
, tail
, NULL
, doca
->doca_flags
,
1171 doca
->doca_cred
, doca
->doca_dcp
, tx
);
1173 VERIFY0(dsl_dataset_hold_obj_flags(pdd
->dd_pool
, obj
,
1174 DS_HOLD_FLAG_DECRYPT
, FTAG
, &ds
));
1175 rrw_enter(&ds
->ds_bp_rwlock
, RW_READER
, FTAG
);
1176 bp
= dsl_dataset_get_blkptr(ds
);
1177 os
= dmu_objset_create_impl(pdd
->dd_pool
->dp_spa
,
1178 ds
, bp
, doca
->doca_type
, tx
);
1179 rrw_exit(&ds
->ds_bp_rwlock
, FTAG
);
1181 if (doca
->doca_userfunc
!= NULL
) {
1182 doca
->doca_userfunc(os
, doca
->doca_userarg
,
1183 doca
->doca_cred
, tx
);
1187 * The doca_userfunc() may write out some data that needs to be
1188 * encrypted if the dataset is encrypted (specifically the root
1189 * directory). This data must be written out before the encryption
1190 * key mapping is removed by dsl_dataset_rele_flags(). Force the
1191 * I/O to occur immediately by invoking the relevant sections of
1194 if (os
->os_encrypted
) {
1195 dsl_dataset_t
*tmpds
= NULL
;
1196 boolean_t need_sync_done
= B_FALSE
;
1198 mutex_enter(&ds
->ds_lock
);
1199 ds
->ds_owner
= FTAG
;
1200 mutex_exit(&ds
->ds_lock
);
1202 rzio
= zio_root(dp
->dp_spa
, NULL
, NULL
, ZIO_FLAG_MUSTSUCCEED
);
1203 tmpds
= txg_list_remove_this(&dp
->dp_dirty_datasets
, ds
,
1205 if (tmpds
!= NULL
) {
1206 dsl_dataset_sync(ds
, rzio
, tx
);
1207 need_sync_done
= B_TRUE
;
1209 VERIFY0(zio_wait(rzio
));
1211 dmu_objset_do_userquota_updates(os
, tx
);
1212 taskq_wait(dp
->dp_sync_taskq
);
1214 rzio
= zio_root(dp
->dp_spa
, NULL
, NULL
, ZIO_FLAG_MUSTSUCCEED
);
1215 tmpds
= txg_list_remove_this(&dp
->dp_dirty_datasets
, ds
,
1217 if (tmpds
!= NULL
) {
1218 dmu_buf_rele(ds
->ds_dbuf
, ds
);
1219 dsl_dataset_sync(ds
, rzio
, tx
);
1221 VERIFY0(zio_wait(rzio
));
1224 dsl_dataset_sync_done(ds
, tx
);
1226 mutex_enter(&ds
->ds_lock
);
1227 ds
->ds_owner
= NULL
;
1228 mutex_exit(&ds
->ds_lock
);
1231 spa_history_log_internal_ds(ds
, "create", tx
, "");
1232 zvol_create_minors(dp
->dp_spa
, doca
->doca_name
, B_TRUE
);
1234 dsl_dataset_rele_flags(ds
, DS_HOLD_FLAG_DECRYPT
, FTAG
);
1235 dsl_dir_rele(pdd
, FTAG
);
1239 dmu_objset_create(const char *name
, dmu_objset_type_t type
, uint64_t flags
,
1240 dsl_crypto_params_t
*dcp
, dmu_objset_create_sync_func_t func
, void *arg
)
1242 dmu_objset_create_arg_t doca
;
1243 dsl_crypto_params_t tmp_dcp
= { 0 };
1245 doca
.doca_name
= name
;
1246 doca
.doca_cred
= CRED();
1247 doca
.doca_flags
= flags
;
1248 doca
.doca_userfunc
= func
;
1249 doca
.doca_userarg
= arg
;
1250 doca
.doca_type
= type
;
1253 * Some callers (mostly for testing) do not provide a dcp on their
1254 * own but various code inside the sync task will require it to be
1255 * allocated. Rather than adding NULL checks throughout this code
1256 * or adding dummy dcp's to all of the callers we simply create a
1257 * dummy one here and use that. This zero dcp will have the same
1258 * effect as asking for inheritance of all encryption params.
1260 doca
.doca_dcp
= (dcp
!= NULL
) ? dcp
: &tmp_dcp
;
1262 return (dsl_sync_task(name
,
1263 dmu_objset_create_check
, dmu_objset_create_sync
, &doca
,
1264 6, ZFS_SPACE_CHECK_NORMAL
));
1267 typedef struct dmu_objset_clone_arg
{
1268 const char *doca_clone
;
1269 const char *doca_origin
;
1271 } dmu_objset_clone_arg_t
;
1275 dmu_objset_clone_check(void *arg
, dmu_tx_t
*tx
)
1277 dmu_objset_clone_arg_t
*doca
= arg
;
1281 dsl_dataset_t
*origin
;
1282 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1284 if (strchr(doca
->doca_clone
, '@') != NULL
)
1285 return (SET_ERROR(EINVAL
));
1287 if (strlen(doca
->doca_clone
) >= ZFS_MAX_DATASET_NAME_LEN
)
1288 return (SET_ERROR(ENAMETOOLONG
));
1290 error
= dsl_dir_hold(dp
, doca
->doca_clone
, FTAG
, &pdd
, &tail
);
1294 dsl_dir_rele(pdd
, FTAG
);
1295 return (SET_ERROR(EEXIST
));
1298 error
= dsl_fs_ss_limit_check(pdd
, 1, ZFS_PROP_FILESYSTEM_LIMIT
, NULL
,
1301 dsl_dir_rele(pdd
, FTAG
);
1302 return (SET_ERROR(EDQUOT
));
1305 error
= dsl_dataset_hold(dp
, doca
->doca_origin
, FTAG
, &origin
);
1307 dsl_dir_rele(pdd
, FTAG
);
1311 /* You can only clone snapshots, not the head datasets. */
1312 if (!origin
->ds_is_snapshot
) {
1313 dsl_dataset_rele(origin
, FTAG
);
1314 dsl_dir_rele(pdd
, FTAG
);
1315 return (SET_ERROR(EINVAL
));
1318 error
= dmu_objset_clone_crypt_check(pdd
, origin
->ds_dir
);
1320 dsl_dataset_rele(origin
, FTAG
);
1321 dsl_dir_rele(pdd
, FTAG
);
1325 dsl_dataset_rele(origin
, FTAG
);
1326 dsl_dir_rele(pdd
, FTAG
);
1332 dmu_objset_clone_sync(void *arg
, dmu_tx_t
*tx
)
1334 dmu_objset_clone_arg_t
*doca
= arg
;
1335 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1338 dsl_dataset_t
*origin
, *ds
;
1340 char namebuf
[ZFS_MAX_DATASET_NAME_LEN
];
1342 VERIFY0(dsl_dir_hold(dp
, doca
->doca_clone
, FTAG
, &pdd
, &tail
));
1343 VERIFY0(dsl_dataset_hold(dp
, doca
->doca_origin
, FTAG
, &origin
));
1345 obj
= dsl_dataset_create_sync(pdd
, tail
, origin
, 0,
1346 doca
->doca_cred
, NULL
, tx
);
1348 VERIFY0(dsl_dataset_hold_obj(pdd
->dd_pool
, obj
, FTAG
, &ds
));
1349 dsl_dataset_name(origin
, namebuf
);
1350 spa_history_log_internal_ds(ds
, "clone", tx
,
1351 "origin=%s (%llu)", namebuf
, origin
->ds_object
);
1352 zvol_create_minors(dp
->dp_spa
, doca
->doca_clone
, B_TRUE
);
1353 dsl_dataset_rele(ds
, FTAG
);
1354 dsl_dataset_rele(origin
, FTAG
);
1355 dsl_dir_rele(pdd
, FTAG
);
1359 dmu_objset_clone(const char *clone
, const char *origin
)
1361 dmu_objset_clone_arg_t doca
;
1363 doca
.doca_clone
= clone
;
1364 doca
.doca_origin
= origin
;
1365 doca
.doca_cred
= CRED();
1367 return (dsl_sync_task(clone
,
1368 dmu_objset_clone_check
, dmu_objset_clone_sync
, &doca
,
1369 6, ZFS_SPACE_CHECK_NORMAL
));
1373 dmu_objset_remap_indirects_impl(objset_t
*os
, uint64_t last_removed_txg
)
1376 uint64_t object
= 0;
1377 while ((error
= dmu_object_next(os
, &object
, B_FALSE
, 0)) == 0) {
1378 error
= dmu_object_remap_indirects(os
, object
,
1381 * If the ZPL removed the object before we managed to dnode_hold
1382 * it, we would get an ENOENT. If the ZPL declares its intent
1383 * to remove the object (dnode_free) before we manage to
1384 * dnode_hold it, we would get an EEXIST. In either case, we
1385 * want to continue remapping the other objects in the objset;
1386 * in all other cases, we want to break early.
1388 if (error
!= 0 && error
!= ENOENT
&& error
!= EEXIST
) {
1392 if (error
== ESRCH
) {
1399 dmu_objset_remap_indirects(const char *fsname
)
1402 objset_t
*os
= NULL
;
1403 uint64_t last_removed_txg
;
1404 uint64_t remap_start_txg
;
1407 error
= dmu_objset_hold(fsname
, FTAG
, &os
);
1411 dd
= dmu_objset_ds(os
)->ds_dir
;
1413 if (!spa_feature_is_enabled(dmu_objset_spa(os
),
1414 SPA_FEATURE_OBSOLETE_COUNTS
)) {
1415 dmu_objset_rele(os
, FTAG
);
1416 return (SET_ERROR(ENOTSUP
));
1419 if (dsl_dataset_is_snapshot(dmu_objset_ds(os
))) {
1420 dmu_objset_rele(os
, FTAG
);
1421 return (SET_ERROR(EINVAL
));
1425 * If there has not been a removal, we're done.
1427 last_removed_txg
= spa_get_last_removal_txg(dmu_objset_spa(os
));
1428 if (last_removed_txg
== -1ULL) {
1429 dmu_objset_rele(os
, FTAG
);
1434 * If we have remapped since the last removal, we're done.
1436 if (dsl_dir_is_zapified(dd
)) {
1437 uint64_t last_remap_txg
;
1438 if (zap_lookup(spa_meta_objset(dmu_objset_spa(os
)),
1439 dd
->dd_object
, DD_FIELD_LAST_REMAP_TXG
,
1440 sizeof (last_remap_txg
), 1, &last_remap_txg
) == 0 &&
1441 last_remap_txg
> last_removed_txg
) {
1442 dmu_objset_rele(os
, FTAG
);
1447 dsl_dataset_long_hold(dmu_objset_ds(os
), FTAG
);
1448 dsl_pool_rele(dmu_objset_pool(os
), FTAG
);
1450 remap_start_txg
= spa_last_synced_txg(dmu_objset_spa(os
));
1451 error
= dmu_objset_remap_indirects_impl(os
, last_removed_txg
);
1454 * We update the last_remap_txg to be the start txg so that
1455 * we can guarantee that every block older than last_remap_txg
1456 * that can be remapped has been remapped.
1458 error
= dsl_dir_update_last_remap_txg(dd
, remap_start_txg
);
1461 dsl_dataset_long_rele(dmu_objset_ds(os
), FTAG
);
1462 dsl_dataset_rele(dmu_objset_ds(os
), FTAG
);
1468 dmu_objset_snapshot_one(const char *fsname
, const char *snapname
)
1471 char *longsnap
= kmem_asprintf("%s@%s", fsname
, snapname
);
1472 nvlist_t
*snaps
= fnvlist_alloc();
1474 fnvlist_add_boolean(snaps
, longsnap
);
1476 err
= dsl_dataset_snapshot(snaps
, NULL
, NULL
);
1477 fnvlist_free(snaps
);
1482 dmu_objset_upgrade_task_cb(void *data
)
1484 objset_t
*os
= data
;
1486 mutex_enter(&os
->os_upgrade_lock
);
1487 os
->os_upgrade_status
= EINTR
;
1488 if (!os
->os_upgrade_exit
) {
1489 mutex_exit(&os
->os_upgrade_lock
);
1491 os
->os_upgrade_status
= os
->os_upgrade_cb(os
);
1492 mutex_enter(&os
->os_upgrade_lock
);
1494 os
->os_upgrade_exit
= B_TRUE
;
1495 os
->os_upgrade_id
= 0;
1496 mutex_exit(&os
->os_upgrade_lock
);
1497 dsl_dataset_long_rele(dmu_objset_ds(os
), upgrade_tag
);
1501 dmu_objset_upgrade(objset_t
*os
, dmu_objset_upgrade_cb_t cb
)
1503 if (os
->os_upgrade_id
!= 0)
1506 ASSERT(dsl_pool_config_held(dmu_objset_pool(os
)));
1507 dsl_dataset_long_hold(dmu_objset_ds(os
), upgrade_tag
);
1509 mutex_enter(&os
->os_upgrade_lock
);
1510 if (os
->os_upgrade_id
== 0 && os
->os_upgrade_status
== 0) {
1511 os
->os_upgrade_exit
= B_FALSE
;
1512 os
->os_upgrade_cb
= cb
;
1513 os
->os_upgrade_id
= taskq_dispatch(
1514 os
->os_spa
->spa_upgrade_taskq
,
1515 dmu_objset_upgrade_task_cb
, os
, TQ_SLEEP
);
1516 if (os
->os_upgrade_id
== TASKQID_INVALID
) {
1517 dsl_dataset_long_rele(dmu_objset_ds(os
), upgrade_tag
);
1518 os
->os_upgrade_status
= ENOMEM
;
1521 mutex_exit(&os
->os_upgrade_lock
);
1525 dmu_objset_upgrade_stop(objset_t
*os
)
1527 mutex_enter(&os
->os_upgrade_lock
);
1528 os
->os_upgrade_exit
= B_TRUE
;
1529 if (os
->os_upgrade_id
!= 0) {
1530 taskqid_t id
= os
->os_upgrade_id
;
1532 os
->os_upgrade_id
= 0;
1533 mutex_exit(&os
->os_upgrade_lock
);
1535 if ((taskq_cancel_id(os
->os_spa
->spa_upgrade_taskq
, id
)) == 0) {
1536 dsl_dataset_long_rele(dmu_objset_ds(os
), upgrade_tag
);
1538 txg_wait_synced(os
->os_spa
->spa_dsl_pool
, 0);
1540 mutex_exit(&os
->os_upgrade_lock
);
1545 dmu_objset_sync_dnodes(multilist_sublist_t
*list
, dmu_tx_t
*tx
)
1549 while ((dn
= multilist_sublist_head(list
)) != NULL
) {
1550 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
);
1551 ASSERT(dn
->dn_dbuf
->db_data_pending
);
1553 * Initialize dn_zio outside dnode_sync() because the
1554 * meta-dnode needs to set it ouside dnode_sync().
1556 dn
->dn_zio
= dn
->dn_dbuf
->db_data_pending
->dr_zio
;
1559 ASSERT3U(dn
->dn_nlevels
, <=, DN_MAX_LEVELS
);
1560 multilist_sublist_remove(list
, dn
);
1563 * If we are not doing useraccounting (os_synced_dnodes == NULL)
1564 * we are done with this dnode for this txg. Unset dn_dirty_txg
1565 * if later txgs aren't dirtying it so that future holders do
1566 * not get a stale value. Otherwise, we will do this in
1567 * userquota_updates_task() when processing has completely
1568 * finished for this txg.
1570 multilist_t
*newlist
= dn
->dn_objset
->os_synced_dnodes
;
1571 if (newlist
!= NULL
) {
1572 (void) dnode_add_ref(dn
, newlist
);
1573 multilist_insert(newlist
, dn
);
1575 mutex_enter(&dn
->dn_mtx
);
1576 if (dn
->dn_dirty_txg
== tx
->tx_txg
)
1577 dn
->dn_dirty_txg
= 0;
1578 mutex_exit(&dn
->dn_mtx
);
1587 dmu_objset_write_ready(zio_t
*zio
, arc_buf_t
*abuf
, void *arg
)
1589 blkptr_t
*bp
= zio
->io_bp
;
1591 dnode_phys_t
*dnp
= &os
->os_phys
->os_meta_dnode
;
1594 ASSERT(!BP_IS_EMBEDDED(bp
));
1595 ASSERT3U(BP_GET_TYPE(bp
), ==, DMU_OT_OBJSET
);
1596 ASSERT0(BP_GET_LEVEL(bp
));
1599 * Update rootbp fill count: it should be the number of objects
1600 * allocated in the object set (not counting the "special"
1601 * objects that are stored in the objset_phys_t -- the meta
1602 * dnode and user/group/project accounting objects).
1604 for (int i
= 0; i
< dnp
->dn_nblkptr
; i
++)
1605 fill
+= BP_GET_FILL(&dnp
->dn_blkptr
[i
]);
1607 BP_SET_FILL(bp
, fill
);
1609 if (os
->os_dsl_dataset
!= NULL
)
1610 rrw_enter(&os
->os_dsl_dataset
->ds_bp_rwlock
, RW_WRITER
, FTAG
);
1611 *os
->os_rootbp
= *bp
;
1612 if (os
->os_dsl_dataset
!= NULL
)
1613 rrw_exit(&os
->os_dsl_dataset
->ds_bp_rwlock
, FTAG
);
1618 dmu_objset_write_done(zio_t
*zio
, arc_buf_t
*abuf
, void *arg
)
1620 blkptr_t
*bp
= zio
->io_bp
;
1621 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
1624 if (zio
->io_flags
& ZIO_FLAG_IO_REWRITE
) {
1625 ASSERT(BP_EQUAL(bp
, bp_orig
));
1627 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
1628 dmu_tx_t
*tx
= os
->os_synctx
;
1630 (void) dsl_dataset_block_kill(ds
, bp_orig
, tx
, B_TRUE
);
1631 dsl_dataset_block_born(ds
, bp
, tx
);
1633 kmem_free(bp
, sizeof (*bp
));
1636 typedef struct sync_dnodes_arg
{
1637 multilist_t
*sda_list
;
1638 int sda_sublist_idx
;
1639 multilist_t
*sda_newlist
;
1641 } sync_dnodes_arg_t
;
1644 sync_dnodes_task(void *arg
)
1646 sync_dnodes_arg_t
*sda
= arg
;
1648 multilist_sublist_t
*ms
=
1649 multilist_sublist_lock(sda
->sda_list
, sda
->sda_sublist_idx
);
1651 dmu_objset_sync_dnodes(ms
, sda
->sda_tx
);
1653 multilist_sublist_unlock(ms
);
1655 kmem_free(sda
, sizeof (*sda
));
1659 /* called from dsl */
1661 dmu_objset_sync(objset_t
*os
, zio_t
*pio
, dmu_tx_t
*tx
)
1664 zbookmark_phys_t zb
;
1668 dbuf_dirty_record_t
*dr
;
1669 blkptr_t
*blkptr_copy
= kmem_alloc(sizeof (*os
->os_rootbp
), KM_SLEEP
);
1670 *blkptr_copy
= *os
->os_rootbp
;
1672 dprintf_ds(os
->os_dsl_dataset
, "txg=%llu\n", tx
->tx_txg
);
1674 ASSERT(dmu_tx_is_syncing(tx
));
1675 /* XXX the write_done callback should really give us the tx... */
1678 if (os
->os_dsl_dataset
== NULL
) {
1680 * This is the MOS. If we have upgraded,
1681 * spa_max_replication() could change, so reset
1684 os
->os_copies
= spa_max_replication(os
->os_spa
);
1688 * Create the root block IO
1690 SET_BOOKMARK(&zb
, os
->os_dsl_dataset
?
1691 os
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
1692 ZB_ROOT_OBJECT
, ZB_ROOT_LEVEL
, ZB_ROOT_BLKID
);
1693 arc_release(os
->os_phys_buf
, &os
->os_phys_buf
);
1695 dmu_write_policy(os
, NULL
, 0, 0, &zp
);
1698 * If we are either claiming the ZIL or doing a raw receive, write
1699 * out the os_phys_buf raw. Neither of these actions will effect the
1700 * MAC at this point.
1702 if (os
->os_raw_receive
||
1703 os
->os_next_write_raw
[tx
->tx_txg
& TXG_MASK
]) {
1704 ASSERT(os
->os_encrypted
);
1705 os
->os_next_write_raw
[tx
->tx_txg
& TXG_MASK
] = B_FALSE
;
1706 arc_convert_to_raw(os
->os_phys_buf
,
1707 os
->os_dsl_dataset
->ds_object
, ZFS_HOST_BYTEORDER
,
1708 DMU_OT_OBJSET
, NULL
, NULL
, NULL
);
1711 zio
= arc_write(pio
, os
->os_spa
, tx
->tx_txg
,
1712 blkptr_copy
, os
->os_phys_buf
, DMU_OS_IS_L2CACHEABLE(os
),
1713 &zp
, dmu_objset_write_ready
, NULL
, NULL
, dmu_objset_write_done
,
1714 os
, ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);
1717 * Sync special dnodes - the parent IO for the sync is the root block
1719 DMU_META_DNODE(os
)->dn_zio
= zio
;
1720 dnode_sync(DMU_META_DNODE(os
), tx
);
1722 os
->os_phys
->os_flags
= os
->os_flags
;
1724 if (DMU_USERUSED_DNODE(os
) &&
1725 DMU_USERUSED_DNODE(os
)->dn_type
!= DMU_OT_NONE
) {
1726 DMU_USERUSED_DNODE(os
)->dn_zio
= zio
;
1727 dnode_sync(DMU_USERUSED_DNODE(os
), tx
);
1728 DMU_GROUPUSED_DNODE(os
)->dn_zio
= zio
;
1729 dnode_sync(DMU_GROUPUSED_DNODE(os
), tx
);
1732 if (DMU_PROJECTUSED_DNODE(os
) &&
1733 DMU_PROJECTUSED_DNODE(os
)->dn_type
!= DMU_OT_NONE
) {
1734 DMU_PROJECTUSED_DNODE(os
)->dn_zio
= zio
;
1735 dnode_sync(DMU_PROJECTUSED_DNODE(os
), tx
);
1738 txgoff
= tx
->tx_txg
& TXG_MASK
;
1740 if (dmu_objset_userused_enabled(os
) &&
1741 (!os
->os_encrypted
|| !dmu_objset_is_receiving(os
))) {
1743 * We must create the list here because it uses the
1744 * dn_dirty_link[] of this txg. But it may already
1745 * exist because we call dsl_dataset_sync() twice per txg.
1747 if (os
->os_synced_dnodes
== NULL
) {
1748 os
->os_synced_dnodes
=
1749 multilist_create(sizeof (dnode_t
),
1750 offsetof(dnode_t
, dn_dirty_link
[txgoff
]),
1751 dnode_multilist_index_func
);
1753 ASSERT3U(os
->os_synced_dnodes
->ml_offset
, ==,
1754 offsetof(dnode_t
, dn_dirty_link
[txgoff
]));
1759 i
< multilist_get_num_sublists(os
->os_dirty_dnodes
[txgoff
]); i
++) {
1760 sync_dnodes_arg_t
*sda
= kmem_alloc(sizeof (*sda
), KM_SLEEP
);
1761 sda
->sda_list
= os
->os_dirty_dnodes
[txgoff
];
1762 sda
->sda_sublist_idx
= i
;
1764 (void) taskq_dispatch(dmu_objset_pool(os
)->dp_sync_taskq
,
1765 sync_dnodes_task
, sda
, 0);
1766 /* callback frees sda */
1768 taskq_wait(dmu_objset_pool(os
)->dp_sync_taskq
);
1770 list
= &DMU_META_DNODE(os
)->dn_dirty_records
[txgoff
];
1771 while ((dr
= list_head(list
)) != NULL
) {
1772 ASSERT0(dr
->dr_dbuf
->db_level
);
1773 list_remove(list
, dr
);
1775 zio_nowait(dr
->dr_zio
);
1778 /* Enable dnode backfill if enough objects have been freed. */
1779 if (os
->os_freed_dnodes
>= dmu_rescan_dnode_threshold
) {
1780 os
->os_rescan_dnodes
= B_TRUE
;
1781 os
->os_freed_dnodes
= 0;
1785 * Free intent log blocks up to this tx.
1787 zil_sync(os
->os_zil
, tx
);
1788 os
->os_phys
->os_zil_header
= os
->os_zil_header
;
1793 dmu_objset_is_dirty(objset_t
*os
, uint64_t txg
)
1795 return (!multilist_is_empty(os
->os_dirty_dnodes
[txg
& TXG_MASK
]));
1798 static objset_used_cb_t
*used_cbs
[DMU_OST_NUMTYPES
];
1801 dmu_objset_register_type(dmu_objset_type_t ost
, objset_used_cb_t
*cb
)
1807 dmu_objset_userused_enabled(objset_t
*os
)
1809 return (spa_version(os
->os_spa
) >= SPA_VERSION_USERSPACE
&&
1810 used_cbs
[os
->os_phys
->os_type
] != NULL
&&
1811 DMU_USERUSED_DNODE(os
) != NULL
);
1815 dmu_objset_userobjused_enabled(objset_t
*os
)
1817 return (dmu_objset_userused_enabled(os
) &&
1818 spa_feature_is_enabled(os
->os_spa
, SPA_FEATURE_USEROBJ_ACCOUNTING
));
1822 dmu_objset_projectquota_enabled(objset_t
*os
)
1824 return (used_cbs
[os
->os_phys
->os_type
] != NULL
&&
1825 DMU_PROJECTUSED_DNODE(os
) != NULL
&&
1826 spa_feature_is_enabled(os
->os_spa
, SPA_FEATURE_PROJECT_QUOTA
));
1829 typedef struct userquota_node
{
1830 /* must be in the first filed, see userquota_update_cache() */
1831 char uqn_id
[20 + DMU_OBJACCT_PREFIX_LEN
];
1833 avl_node_t uqn_node
;
1836 typedef struct userquota_cache
{
1837 avl_tree_t uqc_user_deltas
;
1838 avl_tree_t uqc_group_deltas
;
1839 avl_tree_t uqc_project_deltas
;
1840 } userquota_cache_t
;
1843 userquota_compare(const void *l
, const void *r
)
1845 const userquota_node_t
*luqn
= l
;
1846 const userquota_node_t
*ruqn
= r
;
1850 * NB: can only access uqn_id because userquota_update_cache() doesn't
1851 * pass in an entire userquota_node_t.
1853 rv
= strcmp(luqn
->uqn_id
, ruqn
->uqn_id
);
1855 return (AVL_ISIGN(rv
));
1859 do_userquota_cacheflush(objset_t
*os
, userquota_cache_t
*cache
, dmu_tx_t
*tx
)
1862 userquota_node_t
*uqn
;
1864 ASSERT(dmu_tx_is_syncing(tx
));
1867 while ((uqn
= avl_destroy_nodes(&cache
->uqc_user_deltas
,
1868 &cookie
)) != NULL
) {
1870 * os_userused_lock protects against concurrent calls to
1871 * zap_increment_int(). It's needed because zap_increment_int()
1872 * is not thread-safe (i.e. not atomic).
1874 mutex_enter(&os
->os_userused_lock
);
1875 VERIFY0(zap_increment(os
, DMU_USERUSED_OBJECT
,
1876 uqn
->uqn_id
, uqn
->uqn_delta
, tx
));
1877 mutex_exit(&os
->os_userused_lock
);
1878 kmem_free(uqn
, sizeof (*uqn
));
1880 avl_destroy(&cache
->uqc_user_deltas
);
1883 while ((uqn
= avl_destroy_nodes(&cache
->uqc_group_deltas
,
1884 &cookie
)) != NULL
) {
1885 mutex_enter(&os
->os_userused_lock
);
1886 VERIFY0(zap_increment(os
, DMU_GROUPUSED_OBJECT
,
1887 uqn
->uqn_id
, uqn
->uqn_delta
, tx
));
1888 mutex_exit(&os
->os_userused_lock
);
1889 kmem_free(uqn
, sizeof (*uqn
));
1891 avl_destroy(&cache
->uqc_group_deltas
);
1893 if (dmu_objset_projectquota_enabled(os
)) {
1895 while ((uqn
= avl_destroy_nodes(&cache
->uqc_project_deltas
,
1896 &cookie
)) != NULL
) {
1897 mutex_enter(&os
->os_userused_lock
);
1898 VERIFY0(zap_increment(os
, DMU_PROJECTUSED_OBJECT
,
1899 uqn
->uqn_id
, uqn
->uqn_delta
, tx
));
1900 mutex_exit(&os
->os_userused_lock
);
1901 kmem_free(uqn
, sizeof (*uqn
));
1903 avl_destroy(&cache
->uqc_project_deltas
);
1908 userquota_update_cache(avl_tree_t
*avl
, const char *id
, int64_t delta
)
1910 userquota_node_t
*uqn
;
1913 ASSERT(strlen(id
) < sizeof (uqn
->uqn_id
));
1915 * Use id directly for searching because uqn_id is the first field of
1916 * userquota_node_t and fields after uqn_id won't be accessed in
1919 uqn
= avl_find(avl
, (const void *)id
, &idx
);
1921 uqn
= kmem_zalloc(sizeof (*uqn
), KM_SLEEP
);
1922 strlcpy(uqn
->uqn_id
, id
, sizeof (uqn
->uqn_id
));
1923 avl_insert(avl
, uqn
, idx
);
1925 uqn
->uqn_delta
+= delta
;
1929 do_userquota_update(objset_t
*os
, userquota_cache_t
*cache
, uint64_t used
,
1930 uint64_t flags
, uint64_t user
, uint64_t group
, uint64_t project
,
1933 if (flags
& DNODE_FLAG_USERUSED_ACCOUNTED
) {
1934 int64_t delta
= DNODE_MIN_SIZE
+ used
;
1940 (void) sprintf(name
, "%llx", (longlong_t
)user
);
1941 userquota_update_cache(&cache
->uqc_user_deltas
, name
, delta
);
1943 (void) sprintf(name
, "%llx", (longlong_t
)group
);
1944 userquota_update_cache(&cache
->uqc_group_deltas
, name
, delta
);
1946 if (dmu_objset_projectquota_enabled(os
)) {
1947 (void) sprintf(name
, "%llx", (longlong_t
)project
);
1948 userquota_update_cache(&cache
->uqc_project_deltas
,
1955 do_userobjquota_update(objset_t
*os
, userquota_cache_t
*cache
, uint64_t flags
,
1956 uint64_t user
, uint64_t group
, uint64_t project
, boolean_t subtract
)
1958 if (flags
& DNODE_FLAG_USEROBJUSED_ACCOUNTED
) {
1959 char name
[20 + DMU_OBJACCT_PREFIX_LEN
];
1960 int delta
= subtract
? -1 : 1;
1962 (void) snprintf(name
, sizeof (name
), DMU_OBJACCT_PREFIX
"%llx",
1964 userquota_update_cache(&cache
->uqc_user_deltas
, name
, delta
);
1966 (void) snprintf(name
, sizeof (name
), DMU_OBJACCT_PREFIX
"%llx",
1968 userquota_update_cache(&cache
->uqc_group_deltas
, name
, delta
);
1970 if (dmu_objset_projectquota_enabled(os
)) {
1971 (void) snprintf(name
, sizeof (name
),
1972 DMU_OBJACCT_PREFIX
"%llx", (longlong_t
)project
);
1973 userquota_update_cache(&cache
->uqc_project_deltas
,
1979 typedef struct userquota_updates_arg
{
1981 int uua_sublist_idx
;
1983 } userquota_updates_arg_t
;
1986 userquota_updates_task(void *arg
)
1988 userquota_updates_arg_t
*uua
= arg
;
1989 objset_t
*os
= uua
->uua_os
;
1990 dmu_tx_t
*tx
= uua
->uua_tx
;
1992 userquota_cache_t cache
= { { 0 } };
1994 multilist_sublist_t
*list
=
1995 multilist_sublist_lock(os
->os_synced_dnodes
, uua
->uua_sublist_idx
);
1997 ASSERT(multilist_sublist_head(list
) == NULL
||
1998 dmu_objset_userused_enabled(os
));
1999 avl_create(&cache
.uqc_user_deltas
, userquota_compare
,
2000 sizeof (userquota_node_t
), offsetof(userquota_node_t
, uqn_node
));
2001 avl_create(&cache
.uqc_group_deltas
, userquota_compare
,
2002 sizeof (userquota_node_t
), offsetof(userquota_node_t
, uqn_node
));
2003 if (dmu_objset_projectquota_enabled(os
))
2004 avl_create(&cache
.uqc_project_deltas
, userquota_compare
,
2005 sizeof (userquota_node_t
), offsetof(userquota_node_t
,
2008 while ((dn
= multilist_sublist_head(list
)) != NULL
) {
2010 ASSERT(!DMU_OBJECT_IS_SPECIAL(dn
->dn_object
));
2011 ASSERT(dn
->dn_phys
->dn_type
== DMU_OT_NONE
||
2012 dn
->dn_phys
->dn_flags
&
2013 DNODE_FLAG_USERUSED_ACCOUNTED
);
2015 flags
= dn
->dn_id_flags
;
2017 if (flags
& DN_ID_OLD_EXIST
) {
2018 do_userquota_update(os
, &cache
, dn
->dn_oldused
,
2019 dn
->dn_oldflags
, dn
->dn_olduid
, dn
->dn_oldgid
,
2020 dn
->dn_oldprojid
, B_TRUE
);
2021 do_userobjquota_update(os
, &cache
, dn
->dn_oldflags
,
2022 dn
->dn_olduid
, dn
->dn_oldgid
,
2023 dn
->dn_oldprojid
, B_TRUE
);
2025 if (flags
& DN_ID_NEW_EXIST
) {
2026 do_userquota_update(os
, &cache
,
2027 DN_USED_BYTES(dn
->dn_phys
), dn
->dn_phys
->dn_flags
,
2028 dn
->dn_newuid
, dn
->dn_newgid
,
2029 dn
->dn_newprojid
, B_FALSE
);
2030 do_userobjquota_update(os
, &cache
,
2031 dn
->dn_phys
->dn_flags
, dn
->dn_newuid
, dn
->dn_newgid
,
2032 dn
->dn_newprojid
, B_FALSE
);
2035 mutex_enter(&dn
->dn_mtx
);
2037 dn
->dn_oldflags
= 0;
2038 if (dn
->dn_id_flags
& DN_ID_NEW_EXIST
) {
2039 dn
->dn_olduid
= dn
->dn_newuid
;
2040 dn
->dn_oldgid
= dn
->dn_newgid
;
2041 dn
->dn_oldprojid
= dn
->dn_newprojid
;
2042 dn
->dn_id_flags
|= DN_ID_OLD_EXIST
;
2043 if (dn
->dn_bonuslen
== 0)
2044 dn
->dn_id_flags
|= DN_ID_CHKED_SPILL
;
2046 dn
->dn_id_flags
|= DN_ID_CHKED_BONUS
;
2048 dn
->dn_id_flags
&= ~(DN_ID_NEW_EXIST
);
2049 if (dn
->dn_dirty_txg
== spa_syncing_txg(os
->os_spa
))
2050 dn
->dn_dirty_txg
= 0;
2051 mutex_exit(&dn
->dn_mtx
);
2053 multilist_sublist_remove(list
, dn
);
2054 dnode_rele(dn
, os
->os_synced_dnodes
);
2056 do_userquota_cacheflush(os
, &cache
, tx
);
2057 multilist_sublist_unlock(list
);
2058 kmem_free(uua
, sizeof (*uua
));
2062 dmu_objset_do_userquota_updates(objset_t
*os
, dmu_tx_t
*tx
)
2064 if (!dmu_objset_userused_enabled(os
))
2068 * If this is a raw receive just return and handle accounting
2069 * later when we have the keys loaded. We also don't do user
2070 * accounting during claiming since the datasets are not owned
2071 * for the duration of claiming and this txg should only be
2072 * used for recovery.
2074 if (os
->os_encrypted
&& dmu_objset_is_receiving(os
))
2077 if (tx
->tx_txg
<= os
->os_spa
->spa_claim_max_txg
)
2080 /* Allocate the user/group/project used objects if necessary. */
2081 if (DMU_USERUSED_DNODE(os
)->dn_type
== DMU_OT_NONE
) {
2082 VERIFY0(zap_create_claim(os
,
2083 DMU_USERUSED_OBJECT
,
2084 DMU_OT_USERGROUP_USED
, DMU_OT_NONE
, 0, tx
));
2085 VERIFY0(zap_create_claim(os
,
2086 DMU_GROUPUSED_OBJECT
,
2087 DMU_OT_USERGROUP_USED
, DMU_OT_NONE
, 0, tx
));
2090 if (dmu_objset_projectquota_enabled(os
) &&
2091 DMU_PROJECTUSED_DNODE(os
)->dn_type
== DMU_OT_NONE
) {
2092 VERIFY0(zap_create_claim(os
, DMU_PROJECTUSED_OBJECT
,
2093 DMU_OT_USERGROUP_USED
, DMU_OT_NONE
, 0, tx
));
2097 i
< multilist_get_num_sublists(os
->os_synced_dnodes
); i
++) {
2098 userquota_updates_arg_t
*uua
=
2099 kmem_alloc(sizeof (*uua
), KM_SLEEP
);
2101 uua
->uua_sublist_idx
= i
;
2103 /* note: caller does taskq_wait() */
2104 (void) taskq_dispatch(dmu_objset_pool(os
)->dp_sync_taskq
,
2105 userquota_updates_task
, uua
, 0);
2106 /* callback frees uua */
2111 * Returns a pointer to data to find uid/gid from
2113 * If a dirty record for transaction group that is syncing can't
2114 * be found then NULL is returned. In the NULL case it is assumed
2115 * the uid/gid aren't changing.
2118 dmu_objset_userquota_find_data(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
2120 dbuf_dirty_record_t
*dr
, **drp
;
2123 if (db
->db_dirtycnt
== 0)
2124 return (db
->db
.db_data
); /* Nothing is changing */
2126 for (drp
= &db
->db_last_dirty
; (dr
= *drp
) != NULL
; drp
= &dr
->dr_next
)
2127 if (dr
->dr_txg
== tx
->tx_txg
)
2135 DB_DNODE_ENTER(dr
->dr_dbuf
);
2136 dn
= DB_DNODE(dr
->dr_dbuf
);
2138 if (dn
->dn_bonuslen
== 0 &&
2139 dr
->dr_dbuf
->db_blkid
== DMU_SPILL_BLKID
)
2140 data
= dr
->dt
.dl
.dr_data
->b_data
;
2142 data
= dr
->dt
.dl
.dr_data
;
2144 DB_DNODE_EXIT(dr
->dr_dbuf
);
2151 dmu_objset_userquota_get_ids(dnode_t
*dn
, boolean_t before
, dmu_tx_t
*tx
)
2153 objset_t
*os
= dn
->dn_objset
;
2155 dmu_buf_impl_t
*db
= NULL
;
2156 uint64_t *user
= NULL
;
2157 uint64_t *group
= NULL
;
2158 uint64_t *project
= NULL
;
2159 int flags
= dn
->dn_id_flags
;
2161 boolean_t have_spill
= B_FALSE
;
2163 if (!dmu_objset_userused_enabled(dn
->dn_objset
))
2167 * Raw receives introduce a problem with user accounting. Raw
2168 * receives cannot update the user accounting info because the
2169 * user ids and the sizes are encrypted. To guarantee that we
2170 * never end up with bad user accounting, we simply disable it
2171 * during raw receives. We also disable this for normal receives
2172 * so that an incremental raw receive may be done on top of an
2173 * existing non-raw receive.
2175 if (os
->os_encrypted
&& dmu_objset_is_receiving(os
))
2178 if (before
&& (flags
& (DN_ID_CHKED_BONUS
|DN_ID_OLD_EXIST
|
2179 DN_ID_CHKED_SPILL
)))
2182 if (before
&& dn
->dn_bonuslen
!= 0)
2183 data
= DN_BONUS(dn
->dn_phys
);
2184 else if (!before
&& dn
->dn_bonuslen
!= 0) {
2187 mutex_enter(&db
->db_mtx
);
2188 data
= dmu_objset_userquota_find_data(db
, tx
);
2190 data
= DN_BONUS(dn
->dn_phys
);
2192 } else if (dn
->dn_bonuslen
== 0 && dn
->dn_bonustype
== DMU_OT_SA
) {
2195 if (RW_WRITE_HELD(&dn
->dn_struct_rwlock
))
2196 rf
|= DB_RF_HAVESTRUCT
;
2197 error
= dmu_spill_hold_by_dnode(dn
,
2198 rf
| DB_RF_MUST_SUCCEED
,
2199 FTAG
, (dmu_buf_t
**)&db
);
2201 mutex_enter(&db
->db_mtx
);
2202 data
= (before
) ? db
->db
.db_data
:
2203 dmu_objset_userquota_find_data(db
, tx
);
2204 have_spill
= B_TRUE
;
2206 mutex_enter(&dn
->dn_mtx
);
2207 dn
->dn_id_flags
|= DN_ID_CHKED_BONUS
;
2208 mutex_exit(&dn
->dn_mtx
);
2214 user
= &dn
->dn_olduid
;
2215 group
= &dn
->dn_oldgid
;
2216 project
= &dn
->dn_oldprojid
;
2218 user
= &dn
->dn_newuid
;
2219 group
= &dn
->dn_newgid
;
2220 project
= &dn
->dn_newprojid
;
2224 * Must always call the callback in case the object
2225 * type has changed and that type isn't an object type to track
2227 error
= used_cbs
[os
->os_phys
->os_type
](dn
->dn_bonustype
, data
,
2228 user
, group
, project
);
2231 * Preserve existing uid/gid when the callback can't determine
2232 * what the new uid/gid are and the callback returned EEXIST.
2233 * The EEXIST error tells us to just use the existing uid/gid.
2234 * If we don't know what the old values are then just assign
2235 * them to 0, since that is a new file being created.
2237 if (!before
&& data
== NULL
&& error
== EEXIST
) {
2238 if (flags
& DN_ID_OLD_EXIST
) {
2239 dn
->dn_newuid
= dn
->dn_olduid
;
2240 dn
->dn_newgid
= dn
->dn_oldgid
;
2241 dn
->dn_newprojid
= dn
->dn_oldprojid
;
2245 dn
->dn_newprojid
= ZFS_DEFAULT_PROJID
;
2251 mutex_exit(&db
->db_mtx
);
2253 mutex_enter(&dn
->dn_mtx
);
2254 if (error
== 0 && before
)
2255 dn
->dn_id_flags
|= DN_ID_OLD_EXIST
;
2256 if (error
== 0 && !before
)
2257 dn
->dn_id_flags
|= DN_ID_NEW_EXIST
;
2260 dn
->dn_id_flags
|= DN_ID_CHKED_SPILL
;
2262 dn
->dn_id_flags
|= DN_ID_CHKED_BONUS
;
2264 mutex_exit(&dn
->dn_mtx
);
2266 dmu_buf_rele((dmu_buf_t
*)db
, FTAG
);
2270 dmu_objset_userspace_present(objset_t
*os
)
2272 return (os
->os_phys
->os_flags
&
2273 OBJSET_FLAG_USERACCOUNTING_COMPLETE
);
2277 dmu_objset_userobjspace_present(objset_t
*os
)
2279 return (os
->os_phys
->os_flags
&
2280 OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE
);
2284 dmu_objset_projectquota_present(objset_t
*os
)
2286 return (os
->os_phys
->os_flags
&
2287 OBJSET_FLAG_PROJECTQUOTA_COMPLETE
);
2291 dmu_objset_space_upgrade(objset_t
*os
)
2297 * We simply need to mark every object dirty, so that it will be
2298 * synced out and now accounted. If this is called
2299 * concurrently, or if we already did some work before crashing,
2300 * that's fine, since we track each object's accounted state
2304 for (obj
= 0; err
== 0; err
= dmu_object_next(os
, &obj
, FALSE
, 0)) {
2309 mutex_enter(&os
->os_upgrade_lock
);
2310 if (os
->os_upgrade_exit
)
2311 err
= SET_ERROR(EINTR
);
2312 mutex_exit(&os
->os_upgrade_lock
);
2316 if (issig(JUSTLOOKING
) && issig(FORREAL
))
2317 return (SET_ERROR(EINTR
));
2319 objerr
= dmu_bonus_hold(os
, obj
, FTAG
, &db
);
2322 tx
= dmu_tx_create(os
);
2323 dmu_tx_hold_bonus(tx
, obj
);
2324 objerr
= dmu_tx_assign(tx
, TXG_WAIT
);
2326 dmu_buf_rele(db
, FTAG
);
2330 dmu_buf_will_dirty(db
, tx
);
2331 dmu_buf_rele(db
, FTAG
);
2338 dmu_objset_userspace_upgrade(objset_t
*os
)
2342 if (dmu_objset_userspace_present(os
))
2344 if (dmu_objset_is_snapshot(os
))
2345 return (SET_ERROR(EINVAL
));
2346 if (!dmu_objset_userused_enabled(os
))
2347 return (SET_ERROR(ENOTSUP
));
2349 err
= dmu_objset_space_upgrade(os
);
2353 os
->os_flags
|= OBJSET_FLAG_USERACCOUNTING_COMPLETE
;
2354 txg_wait_synced(dmu_objset_pool(os
), 0);
2359 dmu_objset_id_quota_upgrade_cb(objset_t
*os
)
2363 if (dmu_objset_userobjspace_present(os
) &&
2364 dmu_objset_projectquota_present(os
))
2366 if (dmu_objset_is_snapshot(os
))
2367 return (SET_ERROR(EINVAL
));
2368 if (!dmu_objset_userobjused_enabled(os
))
2369 return (SET_ERROR(ENOTSUP
));
2370 if (!dmu_objset_projectquota_enabled(os
) &&
2371 dmu_objset_userobjspace_present(os
))
2372 return (SET_ERROR(ENOTSUP
));
2374 dmu_objset_ds(os
)->ds_feature_activation_needed
[
2375 SPA_FEATURE_USEROBJ_ACCOUNTING
] = B_TRUE
;
2376 if (dmu_objset_projectquota_enabled(os
))
2377 dmu_objset_ds(os
)->ds_feature_activation_needed
[
2378 SPA_FEATURE_PROJECT_QUOTA
] = B_TRUE
;
2380 err
= dmu_objset_space_upgrade(os
);
2384 os
->os_flags
|= OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE
;
2385 if (dmu_objset_projectquota_enabled(os
))
2386 os
->os_flags
|= OBJSET_FLAG_PROJECTQUOTA_COMPLETE
;
2388 txg_wait_synced(dmu_objset_pool(os
), 0);
2393 dmu_objset_id_quota_upgrade(objset_t
*os
)
2395 dmu_objset_upgrade(os
, dmu_objset_id_quota_upgrade_cb
);
2399 dmu_objset_userobjspace_upgradable(objset_t
*os
)
2401 return (dmu_objset_type(os
) == DMU_OST_ZFS
&&
2402 !dmu_objset_is_snapshot(os
) &&
2403 dmu_objset_userobjused_enabled(os
) &&
2404 !dmu_objset_userobjspace_present(os
));
2408 dmu_objset_projectquota_upgradable(objset_t
*os
)
2410 return (dmu_objset_type(os
) == DMU_OST_ZFS
&&
2411 !dmu_objset_is_snapshot(os
) &&
2412 dmu_objset_projectquota_enabled(os
) &&
2413 !dmu_objset_projectquota_present(os
));
2417 dmu_objset_space(objset_t
*os
, uint64_t *refdbytesp
, uint64_t *availbytesp
,
2418 uint64_t *usedobjsp
, uint64_t *availobjsp
)
2420 dsl_dataset_space(os
->os_dsl_dataset
, refdbytesp
, availbytesp
,
2421 usedobjsp
, availobjsp
);
2425 dmu_objset_fsid_guid(objset_t
*os
)
2427 return (dsl_dataset_fsid_guid(os
->os_dsl_dataset
));
2431 dmu_objset_fast_stat(objset_t
*os
, dmu_objset_stats_t
*stat
)
2433 stat
->dds_type
= os
->os_phys
->os_type
;
2434 if (os
->os_dsl_dataset
)
2435 dsl_dataset_fast_stat(os
->os_dsl_dataset
, stat
);
2439 dmu_objset_stats(objset_t
*os
, nvlist_t
*nv
)
2441 ASSERT(os
->os_dsl_dataset
||
2442 os
->os_phys
->os_type
== DMU_OST_META
);
2444 if (os
->os_dsl_dataset
!= NULL
)
2445 dsl_dataset_stats(os
->os_dsl_dataset
, nv
);
2447 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_TYPE
,
2448 os
->os_phys
->os_type
);
2449 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_USERACCOUNTING
,
2450 dmu_objset_userspace_present(os
));
2454 dmu_objset_is_snapshot(objset_t
*os
)
2456 if (os
->os_dsl_dataset
!= NULL
)
2457 return (os
->os_dsl_dataset
->ds_is_snapshot
);
2463 dmu_snapshot_realname(objset_t
*os
, char *name
, char *real
, int maxlen
,
2464 boolean_t
*conflict
)
2466 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
2469 if (dsl_dataset_phys(ds
)->ds_snapnames_zapobj
== 0)
2470 return (SET_ERROR(ENOENT
));
2472 return (zap_lookup_norm(ds
->ds_dir
->dd_pool
->dp_meta_objset
,
2473 dsl_dataset_phys(ds
)->ds_snapnames_zapobj
, name
, 8, 1, &ignored
,
2474 MT_NORMALIZE
, real
, maxlen
, conflict
));
2478 dmu_snapshot_list_next(objset_t
*os
, int namelen
, char *name
,
2479 uint64_t *idp
, uint64_t *offp
, boolean_t
*case_conflict
)
2481 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
2482 zap_cursor_t cursor
;
2483 zap_attribute_t attr
;
2485 ASSERT(dsl_pool_config_held(dmu_objset_pool(os
)));
2487 if (dsl_dataset_phys(ds
)->ds_snapnames_zapobj
== 0)
2488 return (SET_ERROR(ENOENT
));
2490 zap_cursor_init_serialized(&cursor
,
2491 ds
->ds_dir
->dd_pool
->dp_meta_objset
,
2492 dsl_dataset_phys(ds
)->ds_snapnames_zapobj
, *offp
);
2494 if (zap_cursor_retrieve(&cursor
, &attr
) != 0) {
2495 zap_cursor_fini(&cursor
);
2496 return (SET_ERROR(ENOENT
));
2499 if (strlen(attr
.za_name
) + 1 > namelen
) {
2500 zap_cursor_fini(&cursor
);
2501 return (SET_ERROR(ENAMETOOLONG
));
2504 (void) strcpy(name
, attr
.za_name
);
2506 *idp
= attr
.za_first_integer
;
2508 *case_conflict
= attr
.za_normalization_conflict
;
2509 zap_cursor_advance(&cursor
);
2510 *offp
= zap_cursor_serialize(&cursor
);
2511 zap_cursor_fini(&cursor
);
2517 dmu_snapshot_lookup(objset_t
*os
, const char *name
, uint64_t *value
)
2519 return (dsl_dataset_snap_lookup(os
->os_dsl_dataset
, name
, value
));
2523 dmu_dir_list_next(objset_t
*os
, int namelen
, char *name
,
2524 uint64_t *idp
, uint64_t *offp
)
2526 dsl_dir_t
*dd
= os
->os_dsl_dataset
->ds_dir
;
2527 zap_cursor_t cursor
;
2528 zap_attribute_t attr
;
2530 /* there is no next dir on a snapshot! */
2531 if (os
->os_dsl_dataset
->ds_object
!=
2532 dsl_dir_phys(dd
)->dd_head_dataset_obj
)
2533 return (SET_ERROR(ENOENT
));
2535 zap_cursor_init_serialized(&cursor
,
2536 dd
->dd_pool
->dp_meta_objset
,
2537 dsl_dir_phys(dd
)->dd_child_dir_zapobj
, *offp
);
2539 if (zap_cursor_retrieve(&cursor
, &attr
) != 0) {
2540 zap_cursor_fini(&cursor
);
2541 return (SET_ERROR(ENOENT
));
2544 if (strlen(attr
.za_name
) + 1 > namelen
) {
2545 zap_cursor_fini(&cursor
);
2546 return (SET_ERROR(ENAMETOOLONG
));
2549 (void) strcpy(name
, attr
.za_name
);
2551 *idp
= attr
.za_first_integer
;
2552 zap_cursor_advance(&cursor
);
2553 *offp
= zap_cursor_serialize(&cursor
);
2554 zap_cursor_fini(&cursor
);
2559 typedef struct dmu_objset_find_ctx
{
2563 char *dc_ddname
; /* last component of ddobj's name */
2564 int (*dc_func
)(dsl_pool_t
*, dsl_dataset_t
*, void *);
2567 kmutex_t
*dc_error_lock
;
2569 } dmu_objset_find_ctx_t
;
2572 dmu_objset_find_dp_impl(dmu_objset_find_ctx_t
*dcp
)
2574 dsl_pool_t
*dp
= dcp
->dc_dp
;
2578 zap_attribute_t
*attr
;
2582 /* don't process if there already was an error */
2583 if (*dcp
->dc_error
!= 0)
2587 * Note: passing the name (dc_ddname) here is optional, but it
2588 * improves performance because we don't need to call
2589 * zap_value_search() to determine the name.
2591 err
= dsl_dir_hold_obj(dp
, dcp
->dc_ddobj
, dcp
->dc_ddname
, FTAG
, &dd
);
2595 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
2596 if (dd
->dd_myname
[0] == '$') {
2597 dsl_dir_rele(dd
, FTAG
);
2601 thisobj
= dsl_dir_phys(dd
)->dd_head_dataset_obj
;
2602 attr
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
2605 * Iterate over all children.
2607 if (dcp
->dc_flags
& DS_FIND_CHILDREN
) {
2608 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
,
2609 dsl_dir_phys(dd
)->dd_child_dir_zapobj
);
2610 zap_cursor_retrieve(&zc
, attr
) == 0;
2611 (void) zap_cursor_advance(&zc
)) {
2612 ASSERT3U(attr
->za_integer_length
, ==,
2614 ASSERT3U(attr
->za_num_integers
, ==, 1);
2616 dmu_objset_find_ctx_t
*child_dcp
=
2617 kmem_alloc(sizeof (*child_dcp
), KM_SLEEP
);
2619 child_dcp
->dc_ddobj
= attr
->za_first_integer
;
2620 child_dcp
->dc_ddname
= spa_strdup(attr
->za_name
);
2621 if (dcp
->dc_tq
!= NULL
)
2622 (void) taskq_dispatch(dcp
->dc_tq
,
2623 dmu_objset_find_dp_cb
, child_dcp
, TQ_SLEEP
);
2625 dmu_objset_find_dp_impl(child_dcp
);
2627 zap_cursor_fini(&zc
);
2631 * Iterate over all snapshots.
2633 if (dcp
->dc_flags
& DS_FIND_SNAPSHOTS
) {
2635 err
= dsl_dataset_hold_obj(dp
, thisobj
, FTAG
, &ds
);
2640 snapobj
= dsl_dataset_phys(ds
)->ds_snapnames_zapobj
;
2641 dsl_dataset_rele(ds
, FTAG
);
2643 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
, snapobj
);
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 err
= dsl_dataset_hold_obj(dp
,
2651 attr
->za_first_integer
, FTAG
, &ds
);
2654 err
= dcp
->dc_func(dp
, ds
, dcp
->dc_arg
);
2655 dsl_dataset_rele(ds
, FTAG
);
2659 zap_cursor_fini(&zc
);
2663 kmem_free(attr
, sizeof (zap_attribute_t
));
2666 dsl_dir_rele(dd
, FTAG
);
2673 err
= dsl_dataset_hold_obj(dp
, thisobj
, FTAG
, &ds
);
2676 * Note: we hold the dir while calling dsl_dataset_hold_obj() so
2677 * that the dir will remain cached, and we won't have to re-instantiate
2678 * it (which could be expensive due to finding its name via
2679 * zap_value_search()).
2681 dsl_dir_rele(dd
, FTAG
);
2684 err
= dcp
->dc_func(dp
, ds
, dcp
->dc_arg
);
2685 dsl_dataset_rele(ds
, FTAG
);
2689 mutex_enter(dcp
->dc_error_lock
);
2690 /* only keep first error */
2691 if (*dcp
->dc_error
== 0)
2692 *dcp
->dc_error
= err
;
2693 mutex_exit(dcp
->dc_error_lock
);
2696 if (dcp
->dc_ddname
!= NULL
)
2697 spa_strfree(dcp
->dc_ddname
);
2698 kmem_free(dcp
, sizeof (*dcp
));
2702 dmu_objset_find_dp_cb(void *arg
)
2704 dmu_objset_find_ctx_t
*dcp
= arg
;
2705 dsl_pool_t
*dp
= dcp
->dc_dp
;
2708 * We need to get a pool_config_lock here, as there are several
2709 * asssert(pool_config_held) down the stack. Getting a lock via
2710 * dsl_pool_config_enter is risky, as it might be stalled by a
2711 * pending writer. This would deadlock, as the write lock can
2712 * only be granted when our parent thread gives up the lock.
2713 * The _prio interface gives us priority over a pending writer.
2715 dsl_pool_config_enter_prio(dp
, FTAG
);
2717 dmu_objset_find_dp_impl(dcp
);
2719 dsl_pool_config_exit(dp
, FTAG
);
2723 * Find objsets under and including ddobj, call func(ds) on each.
2724 * The order for the enumeration is completely undefined.
2725 * func is called with dsl_pool_config held.
2728 dmu_objset_find_dp(dsl_pool_t
*dp
, uint64_t ddobj
,
2729 int func(dsl_pool_t
*, dsl_dataset_t
*, void *), void *arg
, int flags
)
2734 dmu_objset_find_ctx_t
*dcp
;
2737 mutex_init(&err_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
2738 dcp
= kmem_alloc(sizeof (*dcp
), KM_SLEEP
);
2741 dcp
->dc_ddobj
= ddobj
;
2742 dcp
->dc_ddname
= NULL
;
2743 dcp
->dc_func
= func
;
2745 dcp
->dc_flags
= flags
;
2746 dcp
->dc_error_lock
= &err_lock
;
2747 dcp
->dc_error
= &error
;
2749 if ((flags
& DS_FIND_SERIALIZE
) || dsl_pool_config_held_writer(dp
)) {
2751 * In case a write lock is held we can't make use of
2752 * parallelism, as down the stack of the worker threads
2753 * the lock is asserted via dsl_pool_config_held.
2754 * In case of a read lock this is solved by getting a read
2755 * lock in each worker thread, which isn't possible in case
2756 * of a writer lock. So we fall back to the synchronous path
2758 * In the future it might be possible to get some magic into
2759 * dsl_pool_config_held in a way that it returns true for
2760 * the worker threads so that a single lock held from this
2761 * thread suffices. For now, stay single threaded.
2763 dmu_objset_find_dp_impl(dcp
);
2764 mutex_destroy(&err_lock
);
2769 ntasks
= dmu_find_threads
;
2771 ntasks
= vdev_count_leaves(dp
->dp_spa
) * 4;
2772 tq
= taskq_create("dmu_objset_find", ntasks
, maxclsyspri
, ntasks
,
2775 kmem_free(dcp
, sizeof (*dcp
));
2776 mutex_destroy(&err_lock
);
2778 return (SET_ERROR(ENOMEM
));
2782 /* dcp will be freed by task */
2783 (void) taskq_dispatch(tq
, dmu_objset_find_dp_cb
, dcp
, TQ_SLEEP
);
2786 * PORTING: this code relies on the property of taskq_wait to wait
2787 * until no more tasks are queued and no more tasks are active. As
2788 * we always queue new tasks from within other tasks, task_wait
2789 * reliably waits for the full recursion to finish, even though we
2790 * enqueue new tasks after taskq_wait has been called.
2791 * On platforms other than illumos, taskq_wait may not have this
2796 mutex_destroy(&err_lock
);
2802 * Find all objsets under name, and for each, call 'func(child_name, arg)'.
2803 * The dp_config_rwlock must not be held when this is called, and it
2804 * will not be held when the callback is called.
2805 * Therefore this function should only be used when the pool is not changing
2806 * (e.g. in syncing context), or the callback can deal with the possible races.
2809 dmu_objset_find_impl(spa_t
*spa
, const char *name
,
2810 int func(const char *, void *), void *arg
, int flags
)
2813 dsl_pool_t
*dp
= spa_get_dsl(spa
);
2816 zap_attribute_t
*attr
;
2821 dsl_pool_config_enter(dp
, FTAG
);
2823 err
= dsl_dir_hold(dp
, name
, FTAG
, &dd
, NULL
);
2825 dsl_pool_config_exit(dp
, FTAG
);
2829 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
2830 if (dd
->dd_myname
[0] == '$') {
2831 dsl_dir_rele(dd
, FTAG
);
2832 dsl_pool_config_exit(dp
, FTAG
);
2836 thisobj
= dsl_dir_phys(dd
)->dd_head_dataset_obj
;
2837 attr
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
2840 * Iterate over all children.
2842 if (flags
& DS_FIND_CHILDREN
) {
2843 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
,
2844 dsl_dir_phys(dd
)->dd_child_dir_zapobj
);
2845 zap_cursor_retrieve(&zc
, attr
) == 0;
2846 (void) zap_cursor_advance(&zc
)) {
2847 ASSERT3U(attr
->za_integer_length
, ==,
2849 ASSERT3U(attr
->za_num_integers
, ==, 1);
2851 child
= kmem_asprintf("%s/%s", name
, attr
->za_name
);
2852 dsl_pool_config_exit(dp
, FTAG
);
2853 err
= dmu_objset_find_impl(spa
, child
,
2855 dsl_pool_config_enter(dp
, FTAG
);
2860 zap_cursor_fini(&zc
);
2863 dsl_dir_rele(dd
, FTAG
);
2864 dsl_pool_config_exit(dp
, FTAG
);
2865 kmem_free(attr
, sizeof (zap_attribute_t
));
2871 * Iterate over all snapshots.
2873 if (flags
& DS_FIND_SNAPSHOTS
) {
2874 err
= dsl_dataset_hold_obj(dp
, thisobj
, FTAG
, &ds
);
2879 snapobj
= dsl_dataset_phys(ds
)->ds_snapnames_zapobj
;
2880 dsl_dataset_rele(ds
, FTAG
);
2882 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
, snapobj
);
2883 zap_cursor_retrieve(&zc
, attr
) == 0;
2884 (void) zap_cursor_advance(&zc
)) {
2885 ASSERT3U(attr
->za_integer_length
, ==,
2887 ASSERT3U(attr
->za_num_integers
, ==, 1);
2889 child
= kmem_asprintf("%s@%s",
2890 name
, attr
->za_name
);
2891 dsl_pool_config_exit(dp
, FTAG
);
2892 err
= func(child
, arg
);
2893 dsl_pool_config_enter(dp
, FTAG
);
2898 zap_cursor_fini(&zc
);
2902 dsl_dir_rele(dd
, FTAG
);
2903 kmem_free(attr
, sizeof (zap_attribute_t
));
2904 dsl_pool_config_exit(dp
, FTAG
);
2909 /* Apply to self. */
2910 return (func(name
, arg
));
2914 * See comment above dmu_objset_find_impl().
2917 dmu_objset_find(char *name
, int func(const char *, void *), void *arg
,
2923 error
= spa_open(name
, &spa
, FTAG
);
2926 error
= dmu_objset_find_impl(spa
, name
, func
, arg
, flags
);
2927 spa_close(spa
, FTAG
);
2932 dmu_objset_incompatible_encryption_version(objset_t
*os
)
2934 return (dsl_dir_incompatible_encryption_version(
2935 os
->os_dsl_dataset
->ds_dir
));
2939 dmu_objset_set_user(objset_t
*os
, void *user_ptr
)
2941 ASSERT(MUTEX_HELD(&os
->os_user_ptr_lock
));
2942 os
->os_user_ptr
= user_ptr
;
2946 dmu_objset_get_user(objset_t
*os
)
2948 ASSERT(MUTEX_HELD(&os
->os_user_ptr_lock
));
2949 return (os
->os_user_ptr
);
2953 * Determine name of filesystem, given name of snapshot.
2954 * buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes
2957 dmu_fsname(const char *snapname
, char *buf
)
2959 char *atp
= strchr(snapname
, '@');
2961 return (SET_ERROR(EINVAL
));
2962 if (atp
- snapname
>= ZFS_MAX_DATASET_NAME_LEN
)
2963 return (SET_ERROR(ENAMETOOLONG
));
2964 (void) strlcpy(buf
, snapname
, atp
- snapname
+ 1);
2969 * Call when we think we're going to write/free space in open context to track
2970 * the amount of dirty data in the open txg, which is also the amount
2971 * of memory that can not be evicted until this txg syncs.
2974 dmu_objset_willuse_space(objset_t
*os
, int64_t space
, dmu_tx_t
*tx
)
2976 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
2977 int64_t aspace
= spa_get_worst_case_asize(os
->os_spa
, space
);
2980 dsl_dir_willuse_space(ds
->ds_dir
, aspace
, tx
);
2981 dsl_pool_dirty_space(dmu_tx_pool(tx
), space
, tx
);
2985 #if defined(_KERNEL)
2986 EXPORT_SYMBOL(dmu_objset_zil
);
2987 EXPORT_SYMBOL(dmu_objset_pool
);
2988 EXPORT_SYMBOL(dmu_objset_ds
);
2989 EXPORT_SYMBOL(dmu_objset_type
);
2990 EXPORT_SYMBOL(dmu_objset_name
);
2991 EXPORT_SYMBOL(dmu_objset_hold
);
2992 EXPORT_SYMBOL(dmu_objset_hold_flags
);
2993 EXPORT_SYMBOL(dmu_objset_own
);
2994 EXPORT_SYMBOL(dmu_objset_rele
);
2995 EXPORT_SYMBOL(dmu_objset_rele_flags
);
2996 EXPORT_SYMBOL(dmu_objset_disown
);
2997 EXPORT_SYMBOL(dmu_objset_from_ds
);
2998 EXPORT_SYMBOL(dmu_objset_create
);
2999 EXPORT_SYMBOL(dmu_objset_clone
);
3000 EXPORT_SYMBOL(dmu_objset_stats
);
3001 EXPORT_SYMBOL(dmu_objset_fast_stat
);
3002 EXPORT_SYMBOL(dmu_objset_spa
);
3003 EXPORT_SYMBOL(dmu_objset_space
);
3004 EXPORT_SYMBOL(dmu_objset_fsid_guid
);
3005 EXPORT_SYMBOL(dmu_objset_find
);
3006 EXPORT_SYMBOL(dmu_objset_byteswap
);
3007 EXPORT_SYMBOL(dmu_objset_evict_dbufs
);
3008 EXPORT_SYMBOL(dmu_objset_snap_cmtime
);
3009 EXPORT_SYMBOL(dmu_objset_dnodesize
);
3011 EXPORT_SYMBOL(dmu_objset_sync
);
3012 EXPORT_SYMBOL(dmu_objset_is_dirty
);
3013 EXPORT_SYMBOL(dmu_objset_create_impl_dnstats
);
3014 EXPORT_SYMBOL(dmu_objset_create_impl
);
3015 EXPORT_SYMBOL(dmu_objset_open_impl
);
3016 EXPORT_SYMBOL(dmu_objset_evict
);
3017 EXPORT_SYMBOL(dmu_objset_register_type
);
3018 EXPORT_SYMBOL(dmu_objset_do_userquota_updates
);
3019 EXPORT_SYMBOL(dmu_objset_userquota_get_ids
);
3020 EXPORT_SYMBOL(dmu_objset_userused_enabled
);
3021 EXPORT_SYMBOL(dmu_objset_userspace_upgrade
);
3022 EXPORT_SYMBOL(dmu_objset_userspace_present
);
3023 EXPORT_SYMBOL(dmu_objset_userobjused_enabled
);
3024 EXPORT_SYMBOL(dmu_objset_userobjspace_upgradable
);
3025 EXPORT_SYMBOL(dmu_objset_userobjspace_present
);
3026 EXPORT_SYMBOL(dmu_objset_projectquota_enabled
);
3027 EXPORT_SYMBOL(dmu_objset_projectquota_present
);
3028 EXPORT_SYMBOL(dmu_objset_projectquota_upgradable
);
3029 EXPORT_SYMBOL(dmu_objset_id_quota_upgrade
);