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 logbias_changed_cb(void *arg
, uint64_t newval
)
322 ASSERT(newval
== ZFS_LOGBIAS_LATENCY
||
323 newval
== ZFS_LOGBIAS_THROUGHPUT
);
324 os
->os_logbias
= newval
;
326 zil_set_logbias(os
->os_zil
, newval
);
330 recordsize_changed_cb(void *arg
, uint64_t newval
)
334 os
->os_recordsize
= newval
;
338 dmu_objset_byteswap(void *buf
, size_t size
)
340 objset_phys_t
*osp
= buf
;
342 ASSERT(size
== OBJSET_PHYS_SIZE_V1
|| size
== OBJSET_PHYS_SIZE_V2
||
343 size
== sizeof (objset_phys_t
));
344 dnode_byteswap(&osp
->os_meta_dnode
);
345 byteswap_uint64_array(&osp
->os_zil_header
, sizeof (zil_header_t
));
346 osp
->os_type
= BSWAP_64(osp
->os_type
);
347 osp
->os_flags
= BSWAP_64(osp
->os_flags
);
348 if (size
>= OBJSET_PHYS_SIZE_V2
) {
349 dnode_byteswap(&osp
->os_userused_dnode
);
350 dnode_byteswap(&osp
->os_groupused_dnode
);
351 if (size
>= sizeof (objset_phys_t
))
352 dnode_byteswap(&osp
->os_projectused_dnode
);
357 * The hash is a CRC-based hash of the objset_t pointer and the object number.
360 dnode_hash(const objset_t
*os
, uint64_t obj
)
362 uintptr_t osv
= (uintptr_t)os
;
363 uint64_t crc
= -1ULL;
365 ASSERT(zfs_crc64_table
[128] == ZFS_CRC64_POLY
);
367 * The low 6 bits of the pointer don't have much entropy, because
368 * the objset_t is larger than 2^6 bytes long.
370 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (osv
>> 6)) & 0xFF];
371 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 0)) & 0xFF];
372 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 8)) & 0xFF];
373 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 16)) & 0xFF];
375 crc
^= (osv
>>14) ^ (obj
>>24);
381 dnode_multilist_index_func(multilist_t
*ml
, void *obj
)
384 return (dnode_hash(dn
->dn_objset
, dn
->dn_object
) %
385 multilist_get_num_sublists(ml
));
389 * Instantiates the objset_t in-memory structure corresponding to the
390 * objset_phys_t that's pointed to by the specified blkptr_t.
393 dmu_objset_open_impl(spa_t
*spa
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
399 ASSERT(ds
== NULL
|| MUTEX_HELD(&ds
->ds_opening_lock
));
402 * The $ORIGIN dataset (if it exists) doesn't have an associated
403 * objset, so there's no reason to open it. The $ORIGIN dataset
404 * will not exist on pools older than SPA_VERSION_ORIGIN.
406 if (ds
!= NULL
&& spa_get_dsl(spa
) != NULL
&&
407 spa_get_dsl(spa
)->dp_origin_snap
!= NULL
) {
408 ASSERT3P(ds
->ds_dir
, !=,
409 spa_get_dsl(spa
)->dp_origin_snap
->ds_dir
);
412 os
= kmem_zalloc(sizeof (objset_t
), KM_SLEEP
);
413 os
->os_dsl_dataset
= ds
;
416 if (!BP_IS_HOLE(os
->os_rootbp
)) {
417 arc_flags_t aflags
= ARC_FLAG_WAIT
;
420 enum zio_flag zio_flags
= ZIO_FLAG_CANFAIL
;
421 SET_BOOKMARK(&zb
, ds
? ds
->ds_object
: DMU_META_OBJSET
,
422 ZB_ROOT_OBJECT
, ZB_ROOT_LEVEL
, ZB_ROOT_BLKID
);
424 if (DMU_OS_IS_L2CACHEABLE(os
))
425 aflags
|= ARC_FLAG_L2CACHE
;
427 if (ds
!= NULL
&& ds
->ds_dir
->dd_crypto_obj
!= 0) {
428 ASSERT3U(BP_GET_COMPRESS(bp
), ==, ZIO_COMPRESS_OFF
);
429 ASSERT(BP_IS_AUTHENTICATED(bp
));
430 zio_flags
|= ZIO_FLAG_RAW
;
433 dprintf_bp(os
->os_rootbp
, "reading %s", "");
434 err
= arc_read(NULL
, spa
, os
->os_rootbp
,
435 arc_getbuf_func
, &os
->os_phys_buf
,
436 ZIO_PRIORITY_SYNC_READ
, zio_flags
, &aflags
, &zb
);
438 kmem_free(os
, sizeof (objset_t
));
439 /* convert checksum errors into IO errors */
441 err
= SET_ERROR(EIO
);
445 if (spa_version(spa
) < SPA_VERSION_USERSPACE
)
446 size
= OBJSET_PHYS_SIZE_V1
;
447 else if (!spa_feature_is_enabled(spa
,
448 SPA_FEATURE_PROJECT_QUOTA
))
449 size
= OBJSET_PHYS_SIZE_V2
;
451 size
= sizeof (objset_phys_t
);
453 /* Increase the blocksize if we are permitted. */
454 if (arc_buf_size(os
->os_phys_buf
) < size
) {
455 arc_buf_t
*buf
= arc_alloc_buf(spa
, &os
->os_phys_buf
,
456 ARC_BUFC_METADATA
, size
);
457 bzero(buf
->b_data
, size
);
458 bcopy(os
->os_phys_buf
->b_data
, buf
->b_data
,
459 arc_buf_size(os
->os_phys_buf
));
460 arc_buf_destroy(os
->os_phys_buf
, &os
->os_phys_buf
);
461 os
->os_phys_buf
= buf
;
464 os
->os_phys
= os
->os_phys_buf
->b_data
;
465 os
->os_flags
= os
->os_phys
->os_flags
;
467 int size
= spa_version(spa
) >= SPA_VERSION_USERSPACE
?
468 sizeof (objset_phys_t
) : OBJSET_PHYS_SIZE_V1
;
469 os
->os_phys_buf
= arc_alloc_buf(spa
, &os
->os_phys_buf
,
470 ARC_BUFC_METADATA
, size
);
471 os
->os_phys
= os
->os_phys_buf
->b_data
;
472 bzero(os
->os_phys
, size
);
475 * These properties will be filled in by the logic in zfs_get_zplprop()
476 * when they are queried for the first time.
478 os
->os_version
= OBJSET_PROP_UNINITIALIZED
;
479 os
->os_normalization
= OBJSET_PROP_UNINITIALIZED
;
480 os
->os_utf8only
= OBJSET_PROP_UNINITIALIZED
;
481 os
->os_casesensitivity
= OBJSET_PROP_UNINITIALIZED
;
484 * Note: the changed_cb will be called once before the register
485 * func returns, thus changing the checksum/compression from the
486 * default (fletcher2/off). Snapshots don't need to know about
487 * checksum/compression/copies.
490 boolean_t needlock
= B_FALSE
;
492 os
->os_encrypted
= (ds
->ds_dir
->dd_crypto_obj
!= 0);
495 * Note: it's valid to open the objset if the dataset is
496 * long-held, in which case the pool_config lock will not
499 if (!dsl_pool_config_held(dmu_objset_pool(os
))) {
501 dsl_pool_config_enter(dmu_objset_pool(os
), FTAG
);
504 err
= dsl_prop_register(ds
,
505 zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE
),
506 primary_cache_changed_cb
, os
);
508 err
= dsl_prop_register(ds
,
509 zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE
),
510 secondary_cache_changed_cb
, os
);
512 if (!ds
->ds_is_snapshot
) {
514 err
= dsl_prop_register(ds
,
515 zfs_prop_to_name(ZFS_PROP_CHECKSUM
),
516 checksum_changed_cb
, os
);
519 err
= dsl_prop_register(ds
,
520 zfs_prop_to_name(ZFS_PROP_COMPRESSION
),
521 compression_changed_cb
, os
);
524 err
= dsl_prop_register(ds
,
525 zfs_prop_to_name(ZFS_PROP_COPIES
),
526 copies_changed_cb
, os
);
529 err
= dsl_prop_register(ds
,
530 zfs_prop_to_name(ZFS_PROP_DEDUP
),
531 dedup_changed_cb
, os
);
534 err
= dsl_prop_register(ds
,
535 zfs_prop_to_name(ZFS_PROP_LOGBIAS
),
536 logbias_changed_cb
, os
);
539 err
= dsl_prop_register(ds
,
540 zfs_prop_to_name(ZFS_PROP_SYNC
),
541 sync_changed_cb
, os
);
544 err
= dsl_prop_register(ds
,
546 ZFS_PROP_REDUNDANT_METADATA
),
547 redundant_metadata_changed_cb
, os
);
550 err
= dsl_prop_register(ds
,
551 zfs_prop_to_name(ZFS_PROP_RECORDSIZE
),
552 recordsize_changed_cb
, os
);
555 err
= dsl_prop_register(ds
,
556 zfs_prop_to_name(ZFS_PROP_DNODESIZE
),
557 dnodesize_changed_cb
, os
);
561 dsl_pool_config_exit(dmu_objset_pool(os
), FTAG
);
563 arc_buf_destroy(os
->os_phys_buf
, &os
->os_phys_buf
);
564 kmem_free(os
, sizeof (objset_t
));
568 /* It's the meta-objset. */
569 os
->os_checksum
= ZIO_CHECKSUM_FLETCHER_4
;
570 os
->os_compress
= ZIO_COMPRESS_ON
;
571 os
->os_encrypted
= B_FALSE
;
572 os
->os_copies
= spa_max_replication(spa
);
573 os
->os_dedup_checksum
= ZIO_CHECKSUM_OFF
;
574 os
->os_dedup_verify
= B_FALSE
;
575 os
->os_logbias
= ZFS_LOGBIAS_LATENCY
;
576 os
->os_sync
= ZFS_SYNC_STANDARD
;
577 os
->os_primary_cache
= ZFS_CACHE_ALL
;
578 os
->os_secondary_cache
= ZFS_CACHE_ALL
;
579 os
->os_dnodesize
= DNODE_MIN_SIZE
;
582 if (ds
== NULL
|| !ds
->ds_is_snapshot
)
583 os
->os_zil_header
= os
->os_phys
->os_zil_header
;
584 os
->os_zil
= zil_alloc(os
, &os
->os_zil_header
);
586 for (i
= 0; i
< TXG_SIZE
; i
++) {
587 os
->os_dirty_dnodes
[i
] = multilist_create(sizeof (dnode_t
),
588 offsetof(dnode_t
, dn_dirty_link
[i
]),
589 dnode_multilist_index_func
);
591 list_create(&os
->os_dnodes
, sizeof (dnode_t
),
592 offsetof(dnode_t
, dn_link
));
593 list_create(&os
->os_downgraded_dbufs
, sizeof (dmu_buf_impl_t
),
594 offsetof(dmu_buf_impl_t
, db_link
));
596 list_link_init(&os
->os_evicting_node
);
598 mutex_init(&os
->os_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
599 mutex_init(&os
->os_userused_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
600 mutex_init(&os
->os_obj_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
601 mutex_init(&os
->os_user_ptr_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
602 os
->os_obj_next_percpu_len
= boot_ncpus
;
603 os
->os_obj_next_percpu
= kmem_zalloc(os
->os_obj_next_percpu_len
*
604 sizeof (os
->os_obj_next_percpu
[0]), KM_SLEEP
);
606 dnode_special_open(os
, &os
->os_phys
->os_meta_dnode
,
607 DMU_META_DNODE_OBJECT
, &os
->os_meta_dnode
);
608 if (OBJSET_BUF_HAS_USERUSED(os
->os_phys_buf
)) {
609 dnode_special_open(os
, &os
->os_phys
->os_userused_dnode
,
610 DMU_USERUSED_OBJECT
, &os
->os_userused_dnode
);
611 dnode_special_open(os
, &os
->os_phys
->os_groupused_dnode
,
612 DMU_GROUPUSED_OBJECT
, &os
->os_groupused_dnode
);
613 if (OBJSET_BUF_HAS_PROJECTUSED(os
->os_phys_buf
))
614 dnode_special_open(os
,
615 &os
->os_phys
->os_projectused_dnode
,
616 DMU_PROJECTUSED_OBJECT
, &os
->os_projectused_dnode
);
619 mutex_init(&os
->os_upgrade_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
626 dmu_objset_from_ds(dsl_dataset_t
*ds
, objset_t
**osp
)
631 * We shouldn't be doing anything with dsl_dataset_t's unless the
632 * pool_config lock is held, or the dataset is long-held.
634 ASSERT(dsl_pool_config_held(ds
->ds_dir
->dd_pool
) ||
635 dsl_dataset_long_held(ds
));
637 mutex_enter(&ds
->ds_opening_lock
);
638 if (ds
->ds_objset
== NULL
) {
640 rrw_enter(&ds
->ds_bp_rwlock
, RW_READER
, FTAG
);
641 err
= dmu_objset_open_impl(dsl_dataset_get_spa(ds
),
642 ds
, dsl_dataset_get_blkptr(ds
), &os
);
643 rrw_exit(&ds
->ds_bp_rwlock
, FTAG
);
646 mutex_enter(&ds
->ds_lock
);
647 ASSERT(ds
->ds_objset
== NULL
);
649 mutex_exit(&ds
->ds_lock
);
652 *osp
= ds
->ds_objset
;
653 mutex_exit(&ds
->ds_opening_lock
);
658 * Holds the pool while the objset is held. Therefore only one objset
659 * can be held at a time.
662 dmu_objset_hold_flags(const char *name
, boolean_t decrypt
, void *tag
,
668 ds_hold_flags_t flags
= (decrypt
) ? DS_HOLD_FLAG_DECRYPT
: 0;
670 err
= dsl_pool_hold(name
, tag
, &dp
);
673 err
= dsl_dataset_hold_flags(dp
, name
, flags
, tag
, &ds
);
675 dsl_pool_rele(dp
, tag
);
679 err
= dmu_objset_from_ds(ds
, osp
);
681 dsl_dataset_rele(ds
, tag
);
682 dsl_pool_rele(dp
, tag
);
689 dmu_objset_hold(const char *name
, void *tag
, objset_t
**osp
)
691 return (dmu_objset_hold_flags(name
, B_FALSE
, tag
, osp
));
695 dmu_objset_own_impl(dsl_dataset_t
*ds
, dmu_objset_type_t type
,
696 boolean_t readonly
, boolean_t decrypt
, void *tag
, objset_t
**osp
)
700 err
= dmu_objset_from_ds(ds
, osp
);
703 } else if (type
!= DMU_OST_ANY
&& type
!= (*osp
)->os_phys
->os_type
) {
704 return (SET_ERROR(EINVAL
));
705 } else if (!readonly
&& dsl_dataset_is_snapshot(ds
)) {
706 return (SET_ERROR(EROFS
));
707 } else if (!readonly
&& decrypt
&&
708 dsl_dir_incompatible_encryption_version(ds
->ds_dir
)) {
709 return (SET_ERROR(EROFS
));
712 /* if we are decrypting, we can now check MACs in os->os_phys_buf */
713 if (decrypt
&& arc_is_unauthenticated((*osp
)->os_phys_buf
)) {
716 SET_BOOKMARK(&zb
, ds
->ds_object
, ZB_ROOT_OBJECT
,
717 ZB_ROOT_LEVEL
, ZB_ROOT_BLKID
);
718 err
= arc_untransform((*osp
)->os_phys_buf
, (*osp
)->os_spa
,
723 ASSERT0(arc_is_unauthenticated((*osp
)->os_phys_buf
));
730 * dsl_pool must not be held when this is called.
731 * Upon successful return, there will be a longhold on the dataset,
732 * and the dsl_pool will not be held.
735 dmu_objset_own(const char *name
, dmu_objset_type_t type
,
736 boolean_t readonly
, boolean_t decrypt
, void *tag
, objset_t
**osp
)
741 ds_hold_flags_t flags
= (decrypt
) ? DS_HOLD_FLAG_DECRYPT
: 0;
743 err
= dsl_pool_hold(name
, FTAG
, &dp
);
746 err
= dsl_dataset_own(dp
, name
, flags
, tag
, &ds
);
748 dsl_pool_rele(dp
, FTAG
);
751 err
= dmu_objset_own_impl(ds
, type
, readonly
, decrypt
, tag
, osp
);
753 dsl_dataset_disown(ds
, flags
, tag
);
754 dsl_pool_rele(dp
, FTAG
);
759 * User accounting requires the dataset to be decrypted and rw.
760 * We also don't begin user accounting during claiming to help
761 * speed up pool import times and to keep this txg reserved
762 * completely for recovery work.
764 if ((dmu_objset_userobjspace_upgradable(*osp
) ||
765 dmu_objset_projectquota_upgradable(*osp
)) &&
766 !readonly
&& !dp
->dp_spa
->spa_claiming
&&
767 (ds
->ds_dir
->dd_crypto_obj
== 0 || decrypt
))
768 dmu_objset_id_quota_upgrade(*osp
);
770 dsl_pool_rele(dp
, FTAG
);
775 dmu_objset_own_obj(dsl_pool_t
*dp
, uint64_t obj
, dmu_objset_type_t type
,
776 boolean_t readonly
, boolean_t decrypt
, void *tag
, objset_t
**osp
)
780 ds_hold_flags_t flags
= (decrypt
) ? DS_HOLD_FLAG_DECRYPT
: 0;
782 err
= dsl_dataset_own_obj(dp
, obj
, flags
, tag
, &ds
);
786 err
= dmu_objset_own_impl(ds
, type
, readonly
, decrypt
, tag
, osp
);
788 dsl_dataset_disown(ds
, flags
, tag
);
796 dmu_objset_rele_flags(objset_t
*os
, boolean_t decrypt
, void *tag
)
798 ds_hold_flags_t flags
= (decrypt
) ? DS_HOLD_FLAG_DECRYPT
: 0;
800 dsl_pool_t
*dp
= dmu_objset_pool(os
);
801 dsl_dataset_rele_flags(os
->os_dsl_dataset
, flags
, tag
);
802 dsl_pool_rele(dp
, tag
);
806 dmu_objset_rele(objset_t
*os
, void *tag
)
808 dmu_objset_rele_flags(os
, B_FALSE
, tag
);
812 * When we are called, os MUST refer to an objset associated with a dataset
813 * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner
814 * == tag. We will then release and reacquire ownership of the dataset while
815 * holding the pool config_rwlock to avoid intervening namespace or ownership
818 * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to
819 * release the hold on its dataset and acquire a new one on the dataset of the
820 * same name so that it can be partially torn down and reconstructed.
823 dmu_objset_refresh_ownership(dsl_dataset_t
*ds
, dsl_dataset_t
**newds
,
824 boolean_t decrypt
, void *tag
)
827 char name
[ZFS_MAX_DATASET_NAME_LEN
];
829 VERIFY3P(ds
, !=, NULL
);
830 VERIFY3P(ds
->ds_owner
, ==, tag
);
831 VERIFY(dsl_dataset_long_held(ds
));
833 dsl_dataset_name(ds
, name
);
834 dp
= ds
->ds_dir
->dd_pool
;
835 dsl_pool_config_enter(dp
, FTAG
);
836 dsl_dataset_disown(ds
, decrypt
, tag
);
837 VERIFY0(dsl_dataset_own(dp
, name
,
838 (decrypt
) ? DS_HOLD_FLAG_DECRYPT
: 0, tag
, newds
));
839 dsl_pool_config_exit(dp
, FTAG
);
843 dmu_objset_disown(objset_t
*os
, boolean_t decrypt
, void *tag
)
846 * Stop upgrading thread
848 dmu_objset_upgrade_stop(os
);
849 dsl_dataset_disown(os
->os_dsl_dataset
,
850 (decrypt
) ? DS_HOLD_FLAG_DECRYPT
: 0, tag
);
854 dmu_objset_evict_dbufs(objset_t
*os
)
859 dn_marker
= kmem_alloc(sizeof (dnode_t
), KM_SLEEP
);
861 mutex_enter(&os
->os_lock
);
862 dn
= list_head(&os
->os_dnodes
);
865 * Skip dnodes without holds. We have to do this dance
866 * because dnode_add_ref() only works if there is already a
867 * hold. If the dnode has no holds, then it has no dbufs.
869 if (dnode_add_ref(dn
, FTAG
)) {
870 list_insert_after(&os
->os_dnodes
, dn
, dn_marker
);
871 mutex_exit(&os
->os_lock
);
873 dnode_evict_dbufs(dn
);
874 dnode_rele(dn
, FTAG
);
876 mutex_enter(&os
->os_lock
);
877 dn
= list_next(&os
->os_dnodes
, dn_marker
);
878 list_remove(&os
->os_dnodes
, dn_marker
);
880 dn
= list_next(&os
->os_dnodes
, dn
);
883 mutex_exit(&os
->os_lock
);
885 kmem_free(dn_marker
, sizeof (dnode_t
));
887 if (DMU_USERUSED_DNODE(os
) != NULL
) {
888 if (DMU_PROJECTUSED_DNODE(os
) != NULL
)
889 dnode_evict_dbufs(DMU_PROJECTUSED_DNODE(os
));
890 dnode_evict_dbufs(DMU_GROUPUSED_DNODE(os
));
891 dnode_evict_dbufs(DMU_USERUSED_DNODE(os
));
893 dnode_evict_dbufs(DMU_META_DNODE(os
));
897 * Objset eviction processing is split into into two pieces.
898 * The first marks the objset as evicting, evicts any dbufs that
899 * have a refcount of zero, and then queues up the objset for the
900 * second phase of eviction. Once os->os_dnodes has been cleared by
901 * dnode_buf_pageout()->dnode_destroy(), the second phase is executed.
902 * The second phase closes the special dnodes, dequeues the objset from
903 * the list of those undergoing eviction, and finally frees the objset.
905 * NOTE: Due to asynchronous eviction processing (invocation of
906 * dnode_buf_pageout()), it is possible for the meta dnode for the
907 * objset to have no holds even though os->os_dnodes is not empty.
910 dmu_objset_evict(objset_t
*os
)
912 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
914 for (int t
= 0; t
< TXG_SIZE
; t
++)
915 ASSERT(!dmu_objset_is_dirty(os
, t
));
918 dsl_prop_unregister_all(ds
, os
);
923 dmu_objset_evict_dbufs(os
);
925 mutex_enter(&os
->os_lock
);
926 spa_evicting_os_register(os
->os_spa
, os
);
927 if (list_is_empty(&os
->os_dnodes
)) {
928 mutex_exit(&os
->os_lock
);
929 dmu_objset_evict_done(os
);
931 mutex_exit(&os
->os_lock
);
938 dmu_objset_evict_done(objset_t
*os
)
940 ASSERT3P(list_head(&os
->os_dnodes
), ==, NULL
);
942 dnode_special_close(&os
->os_meta_dnode
);
943 if (DMU_USERUSED_DNODE(os
)) {
944 if (DMU_PROJECTUSED_DNODE(os
))
945 dnode_special_close(&os
->os_projectused_dnode
);
946 dnode_special_close(&os
->os_userused_dnode
);
947 dnode_special_close(&os
->os_groupused_dnode
);
949 zil_free(os
->os_zil
);
951 arc_buf_destroy(os
->os_phys_buf
, &os
->os_phys_buf
);
954 * This is a barrier to prevent the objset from going away in
955 * dnode_move() until we can safely ensure that the objset is still in
956 * use. We consider the objset valid before the barrier and invalid
959 rw_enter(&os_lock
, RW_READER
);
962 kmem_free(os
->os_obj_next_percpu
,
963 os
->os_obj_next_percpu_len
* sizeof (os
->os_obj_next_percpu
[0]));
965 mutex_destroy(&os
->os_lock
);
966 mutex_destroy(&os
->os_userused_lock
);
967 mutex_destroy(&os
->os_obj_lock
);
968 mutex_destroy(&os
->os_user_ptr_lock
);
969 mutex_destroy(&os
->os_upgrade_lock
);
970 for (int i
= 0; i
< TXG_SIZE
; i
++) {
971 multilist_destroy(os
->os_dirty_dnodes
[i
]);
973 spa_evicting_os_deregister(os
->os_spa
, os
);
974 kmem_free(os
, sizeof (objset_t
));
978 dmu_objset_snap_cmtime(objset_t
*os
)
980 return (dsl_dir_snap_cmtime(os
->os_dsl_dataset
->ds_dir
));
984 dmu_objset_create_impl_dnstats(spa_t
*spa
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
985 dmu_objset_type_t type
, int levels
, int blksz
, int ibs
, dmu_tx_t
*tx
)
990 ASSERT(dmu_tx_is_syncing(tx
));
993 blksz
= DNODE_BLOCK_SIZE
;
995 ibs
= DN_MAX_INDBLKSHIFT
;
998 VERIFY0(dmu_objset_from_ds(ds
, &os
));
1000 VERIFY0(dmu_objset_open_impl(spa
, NULL
, bp
, &os
));
1002 mdn
= DMU_META_DNODE(os
);
1004 dnode_allocate(mdn
, DMU_OT_DNODE
, blksz
, ibs
, DMU_OT_NONE
, 0,
1005 DNODE_MIN_SLOTS
, tx
);
1008 * We don't want to have to increase the meta-dnode's nlevels
1009 * later, because then we could do it in quescing context while
1010 * we are also accessing it in open context.
1012 * This precaution is not necessary for the MOS (ds == NULL),
1013 * because the MOS is only updated in syncing context.
1014 * This is most fortunate: the MOS is the only objset that
1015 * needs to be synced multiple times as spa_sync() iterates
1016 * to convergence, so minimizing its dn_nlevels matters.
1023 * Determine the number of levels necessary for the
1024 * meta-dnode to contain DN_MAX_OBJECT dnodes. Note
1025 * that in order to ensure that we do not overflow
1026 * 64 bits, there has to be a nlevels that gives us a
1027 * number of blocks > DN_MAX_OBJECT but < 2^64.
1028 * Therefore, (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)
1029 * (10) must be less than (64 - log2(DN_MAX_OBJECT))
1032 while ((uint64_t)mdn
->dn_nblkptr
<<
1033 (mdn
->dn_datablkshift
- DNODE_SHIFT
+ (levels
- 1) *
1034 (mdn
->dn_indblkshift
- SPA_BLKPTRSHIFT
)) <
1039 mdn
->dn_next_nlevels
[tx
->tx_txg
& TXG_MASK
] =
1040 mdn
->dn_nlevels
= levels
;
1043 ASSERT(type
!= DMU_OST_NONE
);
1044 ASSERT(type
!= DMU_OST_ANY
);
1045 ASSERT(type
< DMU_OST_NUMTYPES
);
1046 os
->os_phys
->os_type
= type
;
1049 * Enable user accounting if it is enabled and this is not an
1050 * encrypted receive.
1052 if (dmu_objset_userused_enabled(os
) &&
1053 (!os
->os_encrypted
|| !dmu_objset_is_receiving(os
))) {
1054 os
->os_phys
->os_flags
|= OBJSET_FLAG_USERACCOUNTING_COMPLETE
;
1055 if (dmu_objset_userobjused_enabled(os
)) {
1056 ds
->ds_feature_activation_needed
[
1057 SPA_FEATURE_USEROBJ_ACCOUNTING
] = B_TRUE
;
1058 os
->os_phys
->os_flags
|=
1059 OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE
;
1061 if (dmu_objset_projectquota_enabled(os
)) {
1062 ds
->ds_feature_activation_needed
[
1063 SPA_FEATURE_PROJECT_QUOTA
] = B_TRUE
;
1064 os
->os_phys
->os_flags
|=
1065 OBJSET_FLAG_PROJECTQUOTA_COMPLETE
;
1067 os
->os_flags
= os
->os_phys
->os_flags
;
1070 dsl_dataset_dirty(ds
, tx
);
1075 /* called from dsl for meta-objset */
1077 dmu_objset_create_impl(spa_t
*spa
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
1078 dmu_objset_type_t type
, dmu_tx_t
*tx
)
1080 return (dmu_objset_create_impl_dnstats(spa
, ds
, bp
, type
, 0, 0, 0, tx
));
1083 typedef struct dmu_objset_create_arg
{
1084 const char *doca_name
;
1086 void (*doca_userfunc
)(objset_t
*os
, void *arg
,
1087 cred_t
*cr
, dmu_tx_t
*tx
);
1089 dmu_objset_type_t doca_type
;
1090 uint64_t doca_flags
;
1091 dsl_crypto_params_t
*doca_dcp
;
1092 } dmu_objset_create_arg_t
;
1096 dmu_objset_create_check(void *arg
, dmu_tx_t
*tx
)
1098 dmu_objset_create_arg_t
*doca
= arg
;
1099 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1104 if (strchr(doca
->doca_name
, '@') != NULL
)
1105 return (SET_ERROR(EINVAL
));
1107 if (strlen(doca
->doca_name
) >= ZFS_MAX_DATASET_NAME_LEN
)
1108 return (SET_ERROR(ENAMETOOLONG
));
1110 if (dataset_nestcheck(doca
->doca_name
) != 0)
1111 return (SET_ERROR(ENAMETOOLONG
));
1113 error
= dsl_dir_hold(dp
, doca
->doca_name
, FTAG
, &pdd
, &tail
);
1117 dsl_dir_rele(pdd
, FTAG
);
1118 return (SET_ERROR(EEXIST
));
1121 error
= dmu_objset_create_crypt_check(pdd
, doca
->doca_dcp
);
1123 dsl_dir_rele(pdd
, FTAG
);
1127 error
= dsl_fs_ss_limit_check(pdd
, 1, ZFS_PROP_FILESYSTEM_LIMIT
, NULL
,
1130 dsl_dir_rele(pdd
, FTAG
);
1136 dmu_objset_create_sync(void *arg
, dmu_tx_t
*tx
)
1138 dmu_objset_create_arg_t
*doca
= arg
;
1139 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1148 VERIFY0(dsl_dir_hold(dp
, doca
->doca_name
, FTAG
, &pdd
, &tail
));
1150 obj
= dsl_dataset_create_sync(pdd
, tail
, NULL
, doca
->doca_flags
,
1151 doca
->doca_cred
, doca
->doca_dcp
, tx
);
1153 VERIFY0(dsl_dataset_hold_obj_flags(pdd
->dd_pool
, obj
,
1154 DS_HOLD_FLAG_DECRYPT
, FTAG
, &ds
));
1155 rrw_enter(&ds
->ds_bp_rwlock
, RW_READER
, FTAG
);
1156 bp
= dsl_dataset_get_blkptr(ds
);
1157 os
= dmu_objset_create_impl(pdd
->dd_pool
->dp_spa
,
1158 ds
, bp
, doca
->doca_type
, tx
);
1159 rrw_exit(&ds
->ds_bp_rwlock
, FTAG
);
1161 if (doca
->doca_userfunc
!= NULL
) {
1162 doca
->doca_userfunc(os
, doca
->doca_userarg
,
1163 doca
->doca_cred
, tx
);
1167 * The doca_userfunc() may write out some data that needs to be
1168 * encrypted if the dataset is encrypted (specifically the root
1169 * directory). This data must be written out before the encryption
1170 * key mapping is removed by dsl_dataset_rele_flags(). Force the
1171 * I/O to occur immediately by invoking the relevant sections of
1174 if (os
->os_encrypted
) {
1175 dsl_dataset_t
*tmpds
= NULL
;
1176 boolean_t need_sync_done
= B_FALSE
;
1178 mutex_enter(&ds
->ds_lock
);
1179 ds
->ds_owner
= FTAG
;
1180 mutex_exit(&ds
->ds_lock
);
1182 rzio
= zio_root(dp
->dp_spa
, NULL
, NULL
, ZIO_FLAG_MUSTSUCCEED
);
1183 tmpds
= txg_list_remove_this(&dp
->dp_dirty_datasets
, ds
,
1185 if (tmpds
!= NULL
) {
1186 dsl_dataset_sync(ds
, rzio
, tx
);
1187 need_sync_done
= B_TRUE
;
1189 VERIFY0(zio_wait(rzio
));
1191 dmu_objset_do_userquota_updates(os
, tx
);
1192 taskq_wait(dp
->dp_sync_taskq
);
1194 rzio
= zio_root(dp
->dp_spa
, NULL
, NULL
, ZIO_FLAG_MUSTSUCCEED
);
1195 tmpds
= txg_list_remove_this(&dp
->dp_dirty_datasets
, ds
,
1197 if (tmpds
!= NULL
) {
1198 dmu_buf_rele(ds
->ds_dbuf
, ds
);
1199 dsl_dataset_sync(ds
, rzio
, tx
);
1201 VERIFY0(zio_wait(rzio
));
1204 dsl_dataset_sync_done(ds
, tx
);
1206 mutex_enter(&ds
->ds_lock
);
1207 ds
->ds_owner
= NULL
;
1208 mutex_exit(&ds
->ds_lock
);
1211 spa_history_log_internal_ds(ds
, "create", tx
, "");
1212 zvol_create_minors(dp
->dp_spa
, doca
->doca_name
, B_TRUE
);
1214 dsl_dataset_rele_flags(ds
, DS_HOLD_FLAG_DECRYPT
, FTAG
);
1215 dsl_dir_rele(pdd
, FTAG
);
1219 dmu_objset_create(const char *name
, dmu_objset_type_t type
, uint64_t flags
,
1220 dsl_crypto_params_t
*dcp
, dmu_objset_create_sync_func_t func
, void *arg
)
1222 dmu_objset_create_arg_t doca
;
1223 dsl_crypto_params_t tmp_dcp
= { 0 };
1225 doca
.doca_name
= name
;
1226 doca
.doca_cred
= CRED();
1227 doca
.doca_flags
= flags
;
1228 doca
.doca_userfunc
= func
;
1229 doca
.doca_userarg
= arg
;
1230 doca
.doca_type
= type
;
1233 * Some callers (mostly for testing) do not provide a dcp on their
1234 * own but various code inside the sync task will require it to be
1235 * allocated. Rather than adding NULL checks throughout this code
1236 * or adding dummy dcp's to all of the callers we simply create a
1237 * dummy one here and use that. This zero dcp will have the same
1238 * effect as asking for inheritance of all encryption params.
1240 doca
.doca_dcp
= (dcp
!= NULL
) ? dcp
: &tmp_dcp
;
1242 return (dsl_sync_task(name
,
1243 dmu_objset_create_check
, dmu_objset_create_sync
, &doca
,
1244 6, ZFS_SPACE_CHECK_NORMAL
));
1247 typedef struct dmu_objset_clone_arg
{
1248 const char *doca_clone
;
1249 const char *doca_origin
;
1251 } dmu_objset_clone_arg_t
;
1255 dmu_objset_clone_check(void *arg
, dmu_tx_t
*tx
)
1257 dmu_objset_clone_arg_t
*doca
= arg
;
1261 dsl_dataset_t
*origin
;
1262 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1264 if (strchr(doca
->doca_clone
, '@') != NULL
)
1265 return (SET_ERROR(EINVAL
));
1267 if (strlen(doca
->doca_clone
) >= ZFS_MAX_DATASET_NAME_LEN
)
1268 return (SET_ERROR(ENAMETOOLONG
));
1270 error
= dsl_dir_hold(dp
, doca
->doca_clone
, FTAG
, &pdd
, &tail
);
1274 dsl_dir_rele(pdd
, FTAG
);
1275 return (SET_ERROR(EEXIST
));
1278 error
= dsl_fs_ss_limit_check(pdd
, 1, ZFS_PROP_FILESYSTEM_LIMIT
, NULL
,
1281 dsl_dir_rele(pdd
, FTAG
);
1282 return (SET_ERROR(EDQUOT
));
1285 error
= dsl_dataset_hold(dp
, doca
->doca_origin
, FTAG
, &origin
);
1287 dsl_dir_rele(pdd
, FTAG
);
1291 /* You can only clone snapshots, not the head datasets. */
1292 if (!origin
->ds_is_snapshot
) {
1293 dsl_dataset_rele(origin
, FTAG
);
1294 dsl_dir_rele(pdd
, FTAG
);
1295 return (SET_ERROR(EINVAL
));
1298 error
= dmu_objset_clone_crypt_check(pdd
, origin
->ds_dir
);
1300 dsl_dataset_rele(origin
, FTAG
);
1301 dsl_dir_rele(pdd
, FTAG
);
1305 dsl_dataset_rele(origin
, FTAG
);
1306 dsl_dir_rele(pdd
, FTAG
);
1312 dmu_objset_clone_sync(void *arg
, dmu_tx_t
*tx
)
1314 dmu_objset_clone_arg_t
*doca
= arg
;
1315 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1318 dsl_dataset_t
*origin
, *ds
;
1320 char namebuf
[ZFS_MAX_DATASET_NAME_LEN
];
1322 VERIFY0(dsl_dir_hold(dp
, doca
->doca_clone
, FTAG
, &pdd
, &tail
));
1323 VERIFY0(dsl_dataset_hold(dp
, doca
->doca_origin
, FTAG
, &origin
));
1325 obj
= dsl_dataset_create_sync(pdd
, tail
, origin
, 0,
1326 doca
->doca_cred
, NULL
, tx
);
1328 VERIFY0(dsl_dataset_hold_obj(pdd
->dd_pool
, obj
, FTAG
, &ds
));
1329 dsl_dataset_name(origin
, namebuf
);
1330 spa_history_log_internal_ds(ds
, "clone", tx
,
1331 "origin=%s (%llu)", namebuf
, origin
->ds_object
);
1332 zvol_create_minors(dp
->dp_spa
, doca
->doca_clone
, B_TRUE
);
1333 dsl_dataset_rele(ds
, FTAG
);
1334 dsl_dataset_rele(origin
, FTAG
);
1335 dsl_dir_rele(pdd
, FTAG
);
1339 dmu_objset_clone(const char *clone
, const char *origin
)
1341 dmu_objset_clone_arg_t doca
;
1343 doca
.doca_clone
= clone
;
1344 doca
.doca_origin
= origin
;
1345 doca
.doca_cred
= CRED();
1347 return (dsl_sync_task(clone
,
1348 dmu_objset_clone_check
, dmu_objset_clone_sync
, &doca
,
1349 6, ZFS_SPACE_CHECK_NORMAL
));
1353 dmu_objset_remap_indirects_impl(objset_t
*os
, uint64_t last_removed_txg
)
1356 uint64_t object
= 0;
1357 while ((error
= dmu_object_next(os
, &object
, B_FALSE
, 0)) == 0) {
1358 error
= dmu_object_remap_indirects(os
, object
,
1361 * If the ZPL removed the object before we managed to dnode_hold
1362 * it, we would get an ENOENT. If the ZPL declares its intent
1363 * to remove the object (dnode_free) before we manage to
1364 * dnode_hold it, we would get an EEXIST. In either case, we
1365 * want to continue remapping the other objects in the objset;
1366 * in all other cases, we want to break early.
1368 if (error
!= 0 && error
!= ENOENT
&& error
!= EEXIST
) {
1372 if (error
== ESRCH
) {
1379 dmu_objset_remap_indirects(const char *fsname
)
1382 objset_t
*os
= NULL
;
1383 uint64_t last_removed_txg
;
1384 uint64_t remap_start_txg
;
1387 error
= dmu_objset_hold(fsname
, FTAG
, &os
);
1391 dd
= dmu_objset_ds(os
)->ds_dir
;
1393 if (!spa_feature_is_enabled(dmu_objset_spa(os
),
1394 SPA_FEATURE_OBSOLETE_COUNTS
)) {
1395 dmu_objset_rele(os
, FTAG
);
1396 return (SET_ERROR(ENOTSUP
));
1399 if (dsl_dataset_is_snapshot(dmu_objset_ds(os
))) {
1400 dmu_objset_rele(os
, FTAG
);
1401 return (SET_ERROR(EINVAL
));
1405 * If there has not been a removal, we're done.
1407 last_removed_txg
= spa_get_last_removal_txg(dmu_objset_spa(os
));
1408 if (last_removed_txg
== -1ULL) {
1409 dmu_objset_rele(os
, FTAG
);
1414 * If we have remapped since the last removal, we're done.
1416 if (dsl_dir_is_zapified(dd
)) {
1417 uint64_t last_remap_txg
;
1418 if (zap_lookup(spa_meta_objset(dmu_objset_spa(os
)),
1419 dd
->dd_object
, DD_FIELD_LAST_REMAP_TXG
,
1420 sizeof (last_remap_txg
), 1, &last_remap_txg
) == 0 &&
1421 last_remap_txg
> last_removed_txg
) {
1422 dmu_objset_rele(os
, FTAG
);
1427 dsl_dataset_long_hold(dmu_objset_ds(os
), FTAG
);
1428 dsl_pool_rele(dmu_objset_pool(os
), FTAG
);
1430 remap_start_txg
= spa_last_synced_txg(dmu_objset_spa(os
));
1431 error
= dmu_objset_remap_indirects_impl(os
, last_removed_txg
);
1434 * We update the last_remap_txg to be the start txg so that
1435 * we can guarantee that every block older than last_remap_txg
1436 * that can be remapped has been remapped.
1438 error
= dsl_dir_update_last_remap_txg(dd
, remap_start_txg
);
1441 dsl_dataset_long_rele(dmu_objset_ds(os
), FTAG
);
1442 dsl_dataset_rele(dmu_objset_ds(os
), FTAG
);
1448 dmu_objset_snapshot_one(const char *fsname
, const char *snapname
)
1451 char *longsnap
= kmem_asprintf("%s@%s", fsname
, snapname
);
1452 nvlist_t
*snaps
= fnvlist_alloc();
1454 fnvlist_add_boolean(snaps
, longsnap
);
1456 err
= dsl_dataset_snapshot(snaps
, NULL
, NULL
);
1457 fnvlist_free(snaps
);
1462 dmu_objset_upgrade_task_cb(void *data
)
1464 objset_t
*os
= data
;
1466 mutex_enter(&os
->os_upgrade_lock
);
1467 os
->os_upgrade_status
= EINTR
;
1468 if (!os
->os_upgrade_exit
) {
1469 mutex_exit(&os
->os_upgrade_lock
);
1471 os
->os_upgrade_status
= os
->os_upgrade_cb(os
);
1472 mutex_enter(&os
->os_upgrade_lock
);
1474 os
->os_upgrade_exit
= B_TRUE
;
1475 os
->os_upgrade_id
= 0;
1476 mutex_exit(&os
->os_upgrade_lock
);
1477 dsl_dataset_long_rele(dmu_objset_ds(os
), upgrade_tag
);
1481 dmu_objset_upgrade(objset_t
*os
, dmu_objset_upgrade_cb_t cb
)
1483 if (os
->os_upgrade_id
!= 0)
1486 ASSERT(dsl_pool_config_held(dmu_objset_pool(os
)));
1487 dsl_dataset_long_hold(dmu_objset_ds(os
), upgrade_tag
);
1489 mutex_enter(&os
->os_upgrade_lock
);
1490 if (os
->os_upgrade_id
== 0 && os
->os_upgrade_status
== 0) {
1491 os
->os_upgrade_exit
= B_FALSE
;
1492 os
->os_upgrade_cb
= cb
;
1493 os
->os_upgrade_id
= taskq_dispatch(
1494 os
->os_spa
->spa_upgrade_taskq
,
1495 dmu_objset_upgrade_task_cb
, os
, TQ_SLEEP
);
1496 if (os
->os_upgrade_id
== TASKQID_INVALID
) {
1497 dsl_dataset_long_rele(dmu_objset_ds(os
), upgrade_tag
);
1498 os
->os_upgrade_status
= ENOMEM
;
1501 mutex_exit(&os
->os_upgrade_lock
);
1505 dmu_objset_upgrade_stop(objset_t
*os
)
1507 mutex_enter(&os
->os_upgrade_lock
);
1508 os
->os_upgrade_exit
= B_TRUE
;
1509 if (os
->os_upgrade_id
!= 0) {
1510 taskqid_t id
= os
->os_upgrade_id
;
1512 os
->os_upgrade_id
= 0;
1513 mutex_exit(&os
->os_upgrade_lock
);
1515 if ((taskq_cancel_id(os
->os_spa
->spa_upgrade_taskq
, id
)) == 0) {
1516 dsl_dataset_long_rele(dmu_objset_ds(os
), upgrade_tag
);
1518 txg_wait_synced(os
->os_spa
->spa_dsl_pool
, 0);
1520 mutex_exit(&os
->os_upgrade_lock
);
1525 dmu_objset_sync_dnodes(multilist_sublist_t
*list
, dmu_tx_t
*tx
)
1529 while ((dn
= multilist_sublist_head(list
)) != NULL
) {
1530 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
);
1531 ASSERT(dn
->dn_dbuf
->db_data_pending
);
1533 * Initialize dn_zio outside dnode_sync() because the
1534 * meta-dnode needs to set it ouside dnode_sync().
1536 dn
->dn_zio
= dn
->dn_dbuf
->db_data_pending
->dr_zio
;
1539 ASSERT3U(dn
->dn_nlevels
, <=, DN_MAX_LEVELS
);
1540 multilist_sublist_remove(list
, dn
);
1543 * If we are not doing useraccounting (os_synced_dnodes == NULL)
1544 * we are done with this dnode for this txg. Unset dn_dirty_txg
1545 * if later txgs aren't dirtying it so that future holders do
1546 * not get a stale value. Otherwise, we will do this in
1547 * userquota_updates_task() when processing has completely
1548 * finished for this txg.
1550 multilist_t
*newlist
= dn
->dn_objset
->os_synced_dnodes
;
1551 if (newlist
!= NULL
) {
1552 (void) dnode_add_ref(dn
, newlist
);
1553 multilist_insert(newlist
, dn
);
1555 mutex_enter(&dn
->dn_mtx
);
1556 if (dn
->dn_dirty_txg
== tx
->tx_txg
)
1557 dn
->dn_dirty_txg
= 0;
1558 mutex_exit(&dn
->dn_mtx
);
1567 dmu_objset_write_ready(zio_t
*zio
, arc_buf_t
*abuf
, void *arg
)
1569 blkptr_t
*bp
= zio
->io_bp
;
1571 dnode_phys_t
*dnp
= &os
->os_phys
->os_meta_dnode
;
1574 ASSERT(!BP_IS_EMBEDDED(bp
));
1575 ASSERT3U(BP_GET_TYPE(bp
), ==, DMU_OT_OBJSET
);
1576 ASSERT0(BP_GET_LEVEL(bp
));
1579 * Update rootbp fill count: it should be the number of objects
1580 * allocated in the object set (not counting the "special"
1581 * objects that are stored in the objset_phys_t -- the meta
1582 * dnode and user/group/project accounting objects).
1584 for (int i
= 0; i
< dnp
->dn_nblkptr
; i
++)
1585 fill
+= BP_GET_FILL(&dnp
->dn_blkptr
[i
]);
1587 BP_SET_FILL(bp
, fill
);
1589 if (os
->os_dsl_dataset
!= NULL
)
1590 rrw_enter(&os
->os_dsl_dataset
->ds_bp_rwlock
, RW_WRITER
, FTAG
);
1591 *os
->os_rootbp
= *bp
;
1592 if (os
->os_dsl_dataset
!= NULL
)
1593 rrw_exit(&os
->os_dsl_dataset
->ds_bp_rwlock
, FTAG
);
1598 dmu_objset_write_done(zio_t
*zio
, arc_buf_t
*abuf
, void *arg
)
1600 blkptr_t
*bp
= zio
->io_bp
;
1601 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
1604 if (zio
->io_flags
& ZIO_FLAG_IO_REWRITE
) {
1605 ASSERT(BP_EQUAL(bp
, bp_orig
));
1607 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
1608 dmu_tx_t
*tx
= os
->os_synctx
;
1610 (void) dsl_dataset_block_kill(ds
, bp_orig
, tx
, B_TRUE
);
1611 dsl_dataset_block_born(ds
, bp
, tx
);
1613 kmem_free(bp
, sizeof (*bp
));
1616 typedef struct sync_dnodes_arg
{
1617 multilist_t
*sda_list
;
1618 int sda_sublist_idx
;
1619 multilist_t
*sda_newlist
;
1621 } sync_dnodes_arg_t
;
1624 sync_dnodes_task(void *arg
)
1626 sync_dnodes_arg_t
*sda
= arg
;
1628 multilist_sublist_t
*ms
=
1629 multilist_sublist_lock(sda
->sda_list
, sda
->sda_sublist_idx
);
1631 dmu_objset_sync_dnodes(ms
, sda
->sda_tx
);
1633 multilist_sublist_unlock(ms
);
1635 kmem_free(sda
, sizeof (*sda
));
1639 /* called from dsl */
1641 dmu_objset_sync(objset_t
*os
, zio_t
*pio
, dmu_tx_t
*tx
)
1644 zbookmark_phys_t zb
;
1648 dbuf_dirty_record_t
*dr
;
1649 blkptr_t
*blkptr_copy
= kmem_alloc(sizeof (*os
->os_rootbp
), KM_SLEEP
);
1650 *blkptr_copy
= *os
->os_rootbp
;
1652 dprintf_ds(os
->os_dsl_dataset
, "txg=%llu\n", tx
->tx_txg
);
1654 ASSERT(dmu_tx_is_syncing(tx
));
1655 /* XXX the write_done callback should really give us the tx... */
1658 if (os
->os_dsl_dataset
== NULL
) {
1660 * This is the MOS. If we have upgraded,
1661 * spa_max_replication() could change, so reset
1664 os
->os_copies
= spa_max_replication(os
->os_spa
);
1668 * Create the root block IO
1670 SET_BOOKMARK(&zb
, os
->os_dsl_dataset
?
1671 os
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
1672 ZB_ROOT_OBJECT
, ZB_ROOT_LEVEL
, ZB_ROOT_BLKID
);
1673 arc_release(os
->os_phys_buf
, &os
->os_phys_buf
);
1675 dmu_write_policy(os
, NULL
, 0, 0, &zp
);
1678 * If we are either claiming the ZIL or doing a raw receive, write
1679 * out the os_phys_buf raw. Neither of these actions will effect the
1680 * MAC at this point.
1682 if (os
->os_raw_receive
||
1683 os
->os_next_write_raw
[tx
->tx_txg
& TXG_MASK
]) {
1684 ASSERT(os
->os_encrypted
);
1685 os
->os_next_write_raw
[tx
->tx_txg
& TXG_MASK
] = B_FALSE
;
1686 arc_convert_to_raw(os
->os_phys_buf
,
1687 os
->os_dsl_dataset
->ds_object
, ZFS_HOST_BYTEORDER
,
1688 DMU_OT_OBJSET
, NULL
, NULL
, NULL
);
1691 zio
= arc_write(pio
, os
->os_spa
, tx
->tx_txg
,
1692 blkptr_copy
, os
->os_phys_buf
, DMU_OS_IS_L2CACHEABLE(os
),
1693 &zp
, dmu_objset_write_ready
, NULL
, NULL
, dmu_objset_write_done
,
1694 os
, ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);
1697 * Sync special dnodes - the parent IO for the sync is the root block
1699 DMU_META_DNODE(os
)->dn_zio
= zio
;
1700 dnode_sync(DMU_META_DNODE(os
), tx
);
1702 os
->os_phys
->os_flags
= os
->os_flags
;
1704 if (DMU_USERUSED_DNODE(os
) &&
1705 DMU_USERUSED_DNODE(os
)->dn_type
!= DMU_OT_NONE
) {
1706 DMU_USERUSED_DNODE(os
)->dn_zio
= zio
;
1707 dnode_sync(DMU_USERUSED_DNODE(os
), tx
);
1708 DMU_GROUPUSED_DNODE(os
)->dn_zio
= zio
;
1709 dnode_sync(DMU_GROUPUSED_DNODE(os
), tx
);
1712 if (DMU_PROJECTUSED_DNODE(os
) &&
1713 DMU_PROJECTUSED_DNODE(os
)->dn_type
!= DMU_OT_NONE
) {
1714 DMU_PROJECTUSED_DNODE(os
)->dn_zio
= zio
;
1715 dnode_sync(DMU_PROJECTUSED_DNODE(os
), tx
);
1718 txgoff
= tx
->tx_txg
& TXG_MASK
;
1720 if (dmu_objset_userused_enabled(os
) &&
1721 (!os
->os_encrypted
|| !dmu_objset_is_receiving(os
))) {
1723 * We must create the list here because it uses the
1724 * dn_dirty_link[] of this txg. But it may already
1725 * exist because we call dsl_dataset_sync() twice per txg.
1727 if (os
->os_synced_dnodes
== NULL
) {
1728 os
->os_synced_dnodes
=
1729 multilist_create(sizeof (dnode_t
),
1730 offsetof(dnode_t
, dn_dirty_link
[txgoff
]),
1731 dnode_multilist_index_func
);
1733 ASSERT3U(os
->os_synced_dnodes
->ml_offset
, ==,
1734 offsetof(dnode_t
, dn_dirty_link
[txgoff
]));
1739 i
< multilist_get_num_sublists(os
->os_dirty_dnodes
[txgoff
]); i
++) {
1740 sync_dnodes_arg_t
*sda
= kmem_alloc(sizeof (*sda
), KM_SLEEP
);
1741 sda
->sda_list
= os
->os_dirty_dnodes
[txgoff
];
1742 sda
->sda_sublist_idx
= i
;
1744 (void) taskq_dispatch(dmu_objset_pool(os
)->dp_sync_taskq
,
1745 sync_dnodes_task
, sda
, 0);
1746 /* callback frees sda */
1748 taskq_wait(dmu_objset_pool(os
)->dp_sync_taskq
);
1750 list
= &DMU_META_DNODE(os
)->dn_dirty_records
[txgoff
];
1751 while ((dr
= list_head(list
)) != NULL
) {
1752 ASSERT0(dr
->dr_dbuf
->db_level
);
1753 list_remove(list
, dr
);
1755 zio_nowait(dr
->dr_zio
);
1758 /* Enable dnode backfill if enough objects have been freed. */
1759 if (os
->os_freed_dnodes
>= dmu_rescan_dnode_threshold
) {
1760 os
->os_rescan_dnodes
= B_TRUE
;
1761 os
->os_freed_dnodes
= 0;
1765 * Free intent log blocks up to this tx.
1767 zil_sync(os
->os_zil
, tx
);
1768 os
->os_phys
->os_zil_header
= os
->os_zil_header
;
1773 dmu_objset_is_dirty(objset_t
*os
, uint64_t txg
)
1775 return (!multilist_is_empty(os
->os_dirty_dnodes
[txg
& TXG_MASK
]));
1778 static objset_used_cb_t
*used_cbs
[DMU_OST_NUMTYPES
];
1781 dmu_objset_register_type(dmu_objset_type_t ost
, objset_used_cb_t
*cb
)
1787 dmu_objset_userused_enabled(objset_t
*os
)
1789 return (spa_version(os
->os_spa
) >= SPA_VERSION_USERSPACE
&&
1790 used_cbs
[os
->os_phys
->os_type
] != NULL
&&
1791 DMU_USERUSED_DNODE(os
) != NULL
);
1795 dmu_objset_userobjused_enabled(objset_t
*os
)
1797 return (dmu_objset_userused_enabled(os
) &&
1798 spa_feature_is_enabled(os
->os_spa
, SPA_FEATURE_USEROBJ_ACCOUNTING
));
1802 dmu_objset_projectquota_enabled(objset_t
*os
)
1804 return (used_cbs
[os
->os_phys
->os_type
] != NULL
&&
1805 DMU_PROJECTUSED_DNODE(os
) != NULL
&&
1806 spa_feature_is_enabled(os
->os_spa
, SPA_FEATURE_PROJECT_QUOTA
));
1809 typedef struct userquota_node
{
1810 /* must be in the first filed, see userquota_update_cache() */
1811 char uqn_id
[20 + DMU_OBJACCT_PREFIX_LEN
];
1813 avl_node_t uqn_node
;
1816 typedef struct userquota_cache
{
1817 avl_tree_t uqc_user_deltas
;
1818 avl_tree_t uqc_group_deltas
;
1819 avl_tree_t uqc_project_deltas
;
1820 } userquota_cache_t
;
1823 userquota_compare(const void *l
, const void *r
)
1825 const userquota_node_t
*luqn
= l
;
1826 const userquota_node_t
*ruqn
= r
;
1830 * NB: can only access uqn_id because userquota_update_cache() doesn't
1831 * pass in an entire userquota_node_t.
1833 rv
= strcmp(luqn
->uqn_id
, ruqn
->uqn_id
);
1835 return (AVL_ISIGN(rv
));
1839 do_userquota_cacheflush(objset_t
*os
, userquota_cache_t
*cache
, dmu_tx_t
*tx
)
1842 userquota_node_t
*uqn
;
1844 ASSERT(dmu_tx_is_syncing(tx
));
1847 while ((uqn
= avl_destroy_nodes(&cache
->uqc_user_deltas
,
1848 &cookie
)) != NULL
) {
1850 * os_userused_lock protects against concurrent calls to
1851 * zap_increment_int(). It's needed because zap_increment_int()
1852 * is not thread-safe (i.e. not atomic).
1854 mutex_enter(&os
->os_userused_lock
);
1855 VERIFY0(zap_increment(os
, DMU_USERUSED_OBJECT
,
1856 uqn
->uqn_id
, uqn
->uqn_delta
, tx
));
1857 mutex_exit(&os
->os_userused_lock
);
1858 kmem_free(uqn
, sizeof (*uqn
));
1860 avl_destroy(&cache
->uqc_user_deltas
);
1863 while ((uqn
= avl_destroy_nodes(&cache
->uqc_group_deltas
,
1864 &cookie
)) != NULL
) {
1865 mutex_enter(&os
->os_userused_lock
);
1866 VERIFY0(zap_increment(os
, DMU_GROUPUSED_OBJECT
,
1867 uqn
->uqn_id
, uqn
->uqn_delta
, tx
));
1868 mutex_exit(&os
->os_userused_lock
);
1869 kmem_free(uqn
, sizeof (*uqn
));
1871 avl_destroy(&cache
->uqc_group_deltas
);
1873 if (dmu_objset_projectquota_enabled(os
)) {
1875 while ((uqn
= avl_destroy_nodes(&cache
->uqc_project_deltas
,
1876 &cookie
)) != NULL
) {
1877 mutex_enter(&os
->os_userused_lock
);
1878 VERIFY0(zap_increment(os
, DMU_PROJECTUSED_OBJECT
,
1879 uqn
->uqn_id
, uqn
->uqn_delta
, tx
));
1880 mutex_exit(&os
->os_userused_lock
);
1881 kmem_free(uqn
, sizeof (*uqn
));
1883 avl_destroy(&cache
->uqc_project_deltas
);
1888 userquota_update_cache(avl_tree_t
*avl
, const char *id
, int64_t delta
)
1890 userquota_node_t
*uqn
;
1893 ASSERT(strlen(id
) < sizeof (uqn
->uqn_id
));
1895 * Use id directly for searching because uqn_id is the first field of
1896 * userquota_node_t and fields after uqn_id won't be accessed in
1899 uqn
= avl_find(avl
, (const void *)id
, &idx
);
1901 uqn
= kmem_zalloc(sizeof (*uqn
), KM_SLEEP
);
1902 strlcpy(uqn
->uqn_id
, id
, sizeof (uqn
->uqn_id
));
1903 avl_insert(avl
, uqn
, idx
);
1905 uqn
->uqn_delta
+= delta
;
1909 do_userquota_update(objset_t
*os
, userquota_cache_t
*cache
, uint64_t used
,
1910 uint64_t flags
, uint64_t user
, uint64_t group
, uint64_t project
,
1913 if (flags
& DNODE_FLAG_USERUSED_ACCOUNTED
) {
1914 int64_t delta
= DNODE_MIN_SIZE
+ used
;
1920 (void) sprintf(name
, "%llx", (longlong_t
)user
);
1921 userquota_update_cache(&cache
->uqc_user_deltas
, name
, delta
);
1923 (void) sprintf(name
, "%llx", (longlong_t
)group
);
1924 userquota_update_cache(&cache
->uqc_group_deltas
, name
, delta
);
1926 if (dmu_objset_projectquota_enabled(os
)) {
1927 (void) sprintf(name
, "%llx", (longlong_t
)project
);
1928 userquota_update_cache(&cache
->uqc_project_deltas
,
1935 do_userobjquota_update(objset_t
*os
, userquota_cache_t
*cache
, uint64_t flags
,
1936 uint64_t user
, uint64_t group
, uint64_t project
, boolean_t subtract
)
1938 if (flags
& DNODE_FLAG_USEROBJUSED_ACCOUNTED
) {
1939 char name
[20 + DMU_OBJACCT_PREFIX_LEN
];
1940 int delta
= subtract
? -1 : 1;
1942 (void) snprintf(name
, sizeof (name
), DMU_OBJACCT_PREFIX
"%llx",
1944 userquota_update_cache(&cache
->uqc_user_deltas
, name
, delta
);
1946 (void) snprintf(name
, sizeof (name
), DMU_OBJACCT_PREFIX
"%llx",
1948 userquota_update_cache(&cache
->uqc_group_deltas
, name
, delta
);
1950 if (dmu_objset_projectquota_enabled(os
)) {
1951 (void) snprintf(name
, sizeof (name
),
1952 DMU_OBJACCT_PREFIX
"%llx", (longlong_t
)project
);
1953 userquota_update_cache(&cache
->uqc_project_deltas
,
1959 typedef struct userquota_updates_arg
{
1961 int uua_sublist_idx
;
1963 } userquota_updates_arg_t
;
1966 userquota_updates_task(void *arg
)
1968 userquota_updates_arg_t
*uua
= arg
;
1969 objset_t
*os
= uua
->uua_os
;
1970 dmu_tx_t
*tx
= uua
->uua_tx
;
1972 userquota_cache_t cache
= { { 0 } };
1974 multilist_sublist_t
*list
=
1975 multilist_sublist_lock(os
->os_synced_dnodes
, uua
->uua_sublist_idx
);
1977 ASSERT(multilist_sublist_head(list
) == NULL
||
1978 dmu_objset_userused_enabled(os
));
1979 avl_create(&cache
.uqc_user_deltas
, userquota_compare
,
1980 sizeof (userquota_node_t
), offsetof(userquota_node_t
, uqn_node
));
1981 avl_create(&cache
.uqc_group_deltas
, userquota_compare
,
1982 sizeof (userquota_node_t
), offsetof(userquota_node_t
, uqn_node
));
1983 if (dmu_objset_projectquota_enabled(os
))
1984 avl_create(&cache
.uqc_project_deltas
, userquota_compare
,
1985 sizeof (userquota_node_t
), offsetof(userquota_node_t
,
1988 while ((dn
= multilist_sublist_head(list
)) != NULL
) {
1990 ASSERT(!DMU_OBJECT_IS_SPECIAL(dn
->dn_object
));
1991 ASSERT(dn
->dn_phys
->dn_type
== DMU_OT_NONE
||
1992 dn
->dn_phys
->dn_flags
&
1993 DNODE_FLAG_USERUSED_ACCOUNTED
);
1995 flags
= dn
->dn_id_flags
;
1997 if (flags
& DN_ID_OLD_EXIST
) {
1998 do_userquota_update(os
, &cache
, dn
->dn_oldused
,
1999 dn
->dn_oldflags
, dn
->dn_olduid
, dn
->dn_oldgid
,
2000 dn
->dn_oldprojid
, B_TRUE
);
2001 do_userobjquota_update(os
, &cache
, dn
->dn_oldflags
,
2002 dn
->dn_olduid
, dn
->dn_oldgid
,
2003 dn
->dn_oldprojid
, B_TRUE
);
2005 if (flags
& DN_ID_NEW_EXIST
) {
2006 do_userquota_update(os
, &cache
,
2007 DN_USED_BYTES(dn
->dn_phys
), dn
->dn_phys
->dn_flags
,
2008 dn
->dn_newuid
, dn
->dn_newgid
,
2009 dn
->dn_newprojid
, B_FALSE
);
2010 do_userobjquota_update(os
, &cache
,
2011 dn
->dn_phys
->dn_flags
, dn
->dn_newuid
, dn
->dn_newgid
,
2012 dn
->dn_newprojid
, B_FALSE
);
2015 mutex_enter(&dn
->dn_mtx
);
2017 dn
->dn_oldflags
= 0;
2018 if (dn
->dn_id_flags
& DN_ID_NEW_EXIST
) {
2019 dn
->dn_olduid
= dn
->dn_newuid
;
2020 dn
->dn_oldgid
= dn
->dn_newgid
;
2021 dn
->dn_oldprojid
= dn
->dn_newprojid
;
2022 dn
->dn_id_flags
|= DN_ID_OLD_EXIST
;
2023 if (dn
->dn_bonuslen
== 0)
2024 dn
->dn_id_flags
|= DN_ID_CHKED_SPILL
;
2026 dn
->dn_id_flags
|= DN_ID_CHKED_BONUS
;
2028 dn
->dn_id_flags
&= ~(DN_ID_NEW_EXIST
);
2029 if (dn
->dn_dirty_txg
== spa_syncing_txg(os
->os_spa
))
2030 dn
->dn_dirty_txg
= 0;
2031 mutex_exit(&dn
->dn_mtx
);
2033 multilist_sublist_remove(list
, dn
);
2034 dnode_rele(dn
, os
->os_synced_dnodes
);
2036 do_userquota_cacheflush(os
, &cache
, tx
);
2037 multilist_sublist_unlock(list
);
2038 kmem_free(uua
, sizeof (*uua
));
2042 dmu_objset_do_userquota_updates(objset_t
*os
, dmu_tx_t
*tx
)
2044 if (!dmu_objset_userused_enabled(os
))
2048 * If this is a raw receive just return and handle accounting
2049 * later when we have the keys loaded. We also don't do user
2050 * accounting during claiming since the datasets are not owned
2051 * for the duration of claiming and this txg should only be
2052 * used for recovery.
2054 if (os
->os_encrypted
&& dmu_objset_is_receiving(os
))
2057 if (tx
->tx_txg
<= os
->os_spa
->spa_claim_max_txg
)
2060 /* Allocate the user/group/project used objects if necessary. */
2061 if (DMU_USERUSED_DNODE(os
)->dn_type
== DMU_OT_NONE
) {
2062 VERIFY0(zap_create_claim(os
,
2063 DMU_USERUSED_OBJECT
,
2064 DMU_OT_USERGROUP_USED
, DMU_OT_NONE
, 0, tx
));
2065 VERIFY0(zap_create_claim(os
,
2066 DMU_GROUPUSED_OBJECT
,
2067 DMU_OT_USERGROUP_USED
, DMU_OT_NONE
, 0, tx
));
2070 if (dmu_objset_projectquota_enabled(os
) &&
2071 DMU_PROJECTUSED_DNODE(os
)->dn_type
== DMU_OT_NONE
) {
2072 VERIFY0(zap_create_claim(os
, DMU_PROJECTUSED_OBJECT
,
2073 DMU_OT_USERGROUP_USED
, DMU_OT_NONE
, 0, tx
));
2077 i
< multilist_get_num_sublists(os
->os_synced_dnodes
); i
++) {
2078 userquota_updates_arg_t
*uua
=
2079 kmem_alloc(sizeof (*uua
), KM_SLEEP
);
2081 uua
->uua_sublist_idx
= i
;
2083 /* note: caller does taskq_wait() */
2084 (void) taskq_dispatch(dmu_objset_pool(os
)->dp_sync_taskq
,
2085 userquota_updates_task
, uua
, 0);
2086 /* callback frees uua */
2091 * Returns a pointer to data to find uid/gid from
2093 * If a dirty record for transaction group that is syncing can't
2094 * be found then NULL is returned. In the NULL case it is assumed
2095 * the uid/gid aren't changing.
2098 dmu_objset_userquota_find_data(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
2100 dbuf_dirty_record_t
*dr
, **drp
;
2103 if (db
->db_dirtycnt
== 0)
2104 return (db
->db
.db_data
); /* Nothing is changing */
2106 for (drp
= &db
->db_last_dirty
; (dr
= *drp
) != NULL
; drp
= &dr
->dr_next
)
2107 if (dr
->dr_txg
== tx
->tx_txg
)
2115 DB_DNODE_ENTER(dr
->dr_dbuf
);
2116 dn
= DB_DNODE(dr
->dr_dbuf
);
2118 if (dn
->dn_bonuslen
== 0 &&
2119 dr
->dr_dbuf
->db_blkid
== DMU_SPILL_BLKID
)
2120 data
= dr
->dt
.dl
.dr_data
->b_data
;
2122 data
= dr
->dt
.dl
.dr_data
;
2124 DB_DNODE_EXIT(dr
->dr_dbuf
);
2131 dmu_objset_userquota_get_ids(dnode_t
*dn
, boolean_t before
, dmu_tx_t
*tx
)
2133 objset_t
*os
= dn
->dn_objset
;
2135 dmu_buf_impl_t
*db
= NULL
;
2136 uint64_t *user
= NULL
;
2137 uint64_t *group
= NULL
;
2138 uint64_t *project
= NULL
;
2139 int flags
= dn
->dn_id_flags
;
2141 boolean_t have_spill
= B_FALSE
;
2143 if (!dmu_objset_userused_enabled(dn
->dn_objset
))
2147 * Raw receives introduce a problem with user accounting. Raw
2148 * receives cannot update the user accounting info because the
2149 * user ids and the sizes are encrypted. To guarantee that we
2150 * never end up with bad user accounting, we simply disable it
2151 * during raw receives. We also disable this for normal receives
2152 * so that an incremental raw receive may be done on top of an
2153 * existing non-raw receive.
2155 if (os
->os_encrypted
&& dmu_objset_is_receiving(os
))
2158 if (before
&& (flags
& (DN_ID_CHKED_BONUS
|DN_ID_OLD_EXIST
|
2159 DN_ID_CHKED_SPILL
)))
2162 if (before
&& dn
->dn_bonuslen
!= 0)
2163 data
= DN_BONUS(dn
->dn_phys
);
2164 else if (!before
&& dn
->dn_bonuslen
!= 0) {
2167 mutex_enter(&db
->db_mtx
);
2168 data
= dmu_objset_userquota_find_data(db
, tx
);
2170 data
= DN_BONUS(dn
->dn_phys
);
2172 } else if (dn
->dn_bonuslen
== 0 && dn
->dn_bonustype
== DMU_OT_SA
) {
2175 if (RW_WRITE_HELD(&dn
->dn_struct_rwlock
))
2176 rf
|= DB_RF_HAVESTRUCT
;
2177 error
= dmu_spill_hold_by_dnode(dn
,
2178 rf
| DB_RF_MUST_SUCCEED
,
2179 FTAG
, (dmu_buf_t
**)&db
);
2181 mutex_enter(&db
->db_mtx
);
2182 data
= (before
) ? db
->db
.db_data
:
2183 dmu_objset_userquota_find_data(db
, tx
);
2184 have_spill
= B_TRUE
;
2186 mutex_enter(&dn
->dn_mtx
);
2187 dn
->dn_id_flags
|= DN_ID_CHKED_BONUS
;
2188 mutex_exit(&dn
->dn_mtx
);
2194 user
= &dn
->dn_olduid
;
2195 group
= &dn
->dn_oldgid
;
2196 project
= &dn
->dn_oldprojid
;
2198 user
= &dn
->dn_newuid
;
2199 group
= &dn
->dn_newgid
;
2200 project
= &dn
->dn_newprojid
;
2204 * Must always call the callback in case the object
2205 * type has changed and that type isn't an object type to track
2207 error
= used_cbs
[os
->os_phys
->os_type
](dn
->dn_bonustype
, data
,
2208 user
, group
, project
);
2211 * Preserve existing uid/gid when the callback can't determine
2212 * what the new uid/gid are and the callback returned EEXIST.
2213 * The EEXIST error tells us to just use the existing uid/gid.
2214 * If we don't know what the old values are then just assign
2215 * them to 0, since that is a new file being created.
2217 if (!before
&& data
== NULL
&& error
== EEXIST
) {
2218 if (flags
& DN_ID_OLD_EXIST
) {
2219 dn
->dn_newuid
= dn
->dn_olduid
;
2220 dn
->dn_newgid
= dn
->dn_oldgid
;
2221 dn
->dn_newprojid
= dn
->dn_oldprojid
;
2225 dn
->dn_newprojid
= ZFS_DEFAULT_PROJID
;
2231 mutex_exit(&db
->db_mtx
);
2233 mutex_enter(&dn
->dn_mtx
);
2234 if (error
== 0 && before
)
2235 dn
->dn_id_flags
|= DN_ID_OLD_EXIST
;
2236 if (error
== 0 && !before
)
2237 dn
->dn_id_flags
|= DN_ID_NEW_EXIST
;
2240 dn
->dn_id_flags
|= DN_ID_CHKED_SPILL
;
2242 dn
->dn_id_flags
|= DN_ID_CHKED_BONUS
;
2244 mutex_exit(&dn
->dn_mtx
);
2246 dmu_buf_rele((dmu_buf_t
*)db
, FTAG
);
2250 dmu_objset_userspace_present(objset_t
*os
)
2252 return (os
->os_phys
->os_flags
&
2253 OBJSET_FLAG_USERACCOUNTING_COMPLETE
);
2257 dmu_objset_userobjspace_present(objset_t
*os
)
2259 return (os
->os_phys
->os_flags
&
2260 OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE
);
2264 dmu_objset_projectquota_present(objset_t
*os
)
2266 return (os
->os_phys
->os_flags
&
2267 OBJSET_FLAG_PROJECTQUOTA_COMPLETE
);
2271 dmu_objset_space_upgrade(objset_t
*os
)
2277 * We simply need to mark every object dirty, so that it will be
2278 * synced out and now accounted. If this is called
2279 * concurrently, or if we already did some work before crashing,
2280 * that's fine, since we track each object's accounted state
2284 for (obj
= 0; err
== 0; err
= dmu_object_next(os
, &obj
, FALSE
, 0)) {
2289 mutex_enter(&os
->os_upgrade_lock
);
2290 if (os
->os_upgrade_exit
)
2291 err
= SET_ERROR(EINTR
);
2292 mutex_exit(&os
->os_upgrade_lock
);
2296 if (issig(JUSTLOOKING
) && issig(FORREAL
))
2297 return (SET_ERROR(EINTR
));
2299 objerr
= dmu_bonus_hold(os
, obj
, FTAG
, &db
);
2302 tx
= dmu_tx_create(os
);
2303 dmu_tx_hold_bonus(tx
, obj
);
2304 objerr
= dmu_tx_assign(tx
, TXG_WAIT
);
2306 dmu_buf_rele(db
, FTAG
);
2310 dmu_buf_will_dirty(db
, tx
);
2311 dmu_buf_rele(db
, FTAG
);
2318 dmu_objset_userspace_upgrade(objset_t
*os
)
2322 if (dmu_objset_userspace_present(os
))
2324 if (dmu_objset_is_snapshot(os
))
2325 return (SET_ERROR(EINVAL
));
2326 if (!dmu_objset_userused_enabled(os
))
2327 return (SET_ERROR(ENOTSUP
));
2329 err
= dmu_objset_space_upgrade(os
);
2333 os
->os_flags
|= OBJSET_FLAG_USERACCOUNTING_COMPLETE
;
2334 txg_wait_synced(dmu_objset_pool(os
), 0);
2339 dmu_objset_id_quota_upgrade_cb(objset_t
*os
)
2343 if (dmu_objset_userobjspace_present(os
) &&
2344 dmu_objset_projectquota_present(os
))
2346 if (dmu_objset_is_snapshot(os
))
2347 return (SET_ERROR(EINVAL
));
2348 if (!dmu_objset_userobjused_enabled(os
))
2349 return (SET_ERROR(ENOTSUP
));
2350 if (!dmu_objset_projectquota_enabled(os
) &&
2351 dmu_objset_userobjspace_present(os
))
2352 return (SET_ERROR(ENOTSUP
));
2354 dmu_objset_ds(os
)->ds_feature_activation_needed
[
2355 SPA_FEATURE_USEROBJ_ACCOUNTING
] = B_TRUE
;
2356 if (dmu_objset_projectquota_enabled(os
))
2357 dmu_objset_ds(os
)->ds_feature_activation_needed
[
2358 SPA_FEATURE_PROJECT_QUOTA
] = B_TRUE
;
2360 err
= dmu_objset_space_upgrade(os
);
2364 os
->os_flags
|= OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE
;
2365 if (dmu_objset_projectquota_enabled(os
))
2366 os
->os_flags
|= OBJSET_FLAG_PROJECTQUOTA_COMPLETE
;
2368 txg_wait_synced(dmu_objset_pool(os
), 0);
2373 dmu_objset_id_quota_upgrade(objset_t
*os
)
2375 dmu_objset_upgrade(os
, dmu_objset_id_quota_upgrade_cb
);
2379 dmu_objset_userobjspace_upgradable(objset_t
*os
)
2381 return (dmu_objset_type(os
) == DMU_OST_ZFS
&&
2382 !dmu_objset_is_snapshot(os
) &&
2383 dmu_objset_userobjused_enabled(os
) &&
2384 !dmu_objset_userobjspace_present(os
));
2388 dmu_objset_projectquota_upgradable(objset_t
*os
)
2390 return (dmu_objset_type(os
) == DMU_OST_ZFS
&&
2391 !dmu_objset_is_snapshot(os
) &&
2392 dmu_objset_projectquota_enabled(os
) &&
2393 !dmu_objset_projectquota_present(os
));
2397 dmu_objset_space(objset_t
*os
, uint64_t *refdbytesp
, uint64_t *availbytesp
,
2398 uint64_t *usedobjsp
, uint64_t *availobjsp
)
2400 dsl_dataset_space(os
->os_dsl_dataset
, refdbytesp
, availbytesp
,
2401 usedobjsp
, availobjsp
);
2405 dmu_objset_fsid_guid(objset_t
*os
)
2407 return (dsl_dataset_fsid_guid(os
->os_dsl_dataset
));
2411 dmu_objset_fast_stat(objset_t
*os
, dmu_objset_stats_t
*stat
)
2413 stat
->dds_type
= os
->os_phys
->os_type
;
2414 if (os
->os_dsl_dataset
)
2415 dsl_dataset_fast_stat(os
->os_dsl_dataset
, stat
);
2419 dmu_objset_stats(objset_t
*os
, nvlist_t
*nv
)
2421 ASSERT(os
->os_dsl_dataset
||
2422 os
->os_phys
->os_type
== DMU_OST_META
);
2424 if (os
->os_dsl_dataset
!= NULL
)
2425 dsl_dataset_stats(os
->os_dsl_dataset
, nv
);
2427 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_TYPE
,
2428 os
->os_phys
->os_type
);
2429 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_USERACCOUNTING
,
2430 dmu_objset_userspace_present(os
));
2434 dmu_objset_is_snapshot(objset_t
*os
)
2436 if (os
->os_dsl_dataset
!= NULL
)
2437 return (os
->os_dsl_dataset
->ds_is_snapshot
);
2443 dmu_snapshot_realname(objset_t
*os
, char *name
, char *real
, int maxlen
,
2444 boolean_t
*conflict
)
2446 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
2449 if (dsl_dataset_phys(ds
)->ds_snapnames_zapobj
== 0)
2450 return (SET_ERROR(ENOENT
));
2452 return (zap_lookup_norm(ds
->ds_dir
->dd_pool
->dp_meta_objset
,
2453 dsl_dataset_phys(ds
)->ds_snapnames_zapobj
, name
, 8, 1, &ignored
,
2454 MT_NORMALIZE
, real
, maxlen
, conflict
));
2458 dmu_snapshot_list_next(objset_t
*os
, int namelen
, char *name
,
2459 uint64_t *idp
, uint64_t *offp
, boolean_t
*case_conflict
)
2461 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
2462 zap_cursor_t cursor
;
2463 zap_attribute_t attr
;
2465 ASSERT(dsl_pool_config_held(dmu_objset_pool(os
)));
2467 if (dsl_dataset_phys(ds
)->ds_snapnames_zapobj
== 0)
2468 return (SET_ERROR(ENOENT
));
2470 zap_cursor_init_serialized(&cursor
,
2471 ds
->ds_dir
->dd_pool
->dp_meta_objset
,
2472 dsl_dataset_phys(ds
)->ds_snapnames_zapobj
, *offp
);
2474 if (zap_cursor_retrieve(&cursor
, &attr
) != 0) {
2475 zap_cursor_fini(&cursor
);
2476 return (SET_ERROR(ENOENT
));
2479 if (strlen(attr
.za_name
) + 1 > namelen
) {
2480 zap_cursor_fini(&cursor
);
2481 return (SET_ERROR(ENAMETOOLONG
));
2484 (void) strcpy(name
, attr
.za_name
);
2486 *idp
= attr
.za_first_integer
;
2488 *case_conflict
= attr
.za_normalization_conflict
;
2489 zap_cursor_advance(&cursor
);
2490 *offp
= zap_cursor_serialize(&cursor
);
2491 zap_cursor_fini(&cursor
);
2497 dmu_snapshot_lookup(objset_t
*os
, const char *name
, uint64_t *value
)
2499 return (dsl_dataset_snap_lookup(os
->os_dsl_dataset
, name
, value
));
2503 dmu_dir_list_next(objset_t
*os
, int namelen
, char *name
,
2504 uint64_t *idp
, uint64_t *offp
)
2506 dsl_dir_t
*dd
= os
->os_dsl_dataset
->ds_dir
;
2507 zap_cursor_t cursor
;
2508 zap_attribute_t attr
;
2510 /* there is no next dir on a snapshot! */
2511 if (os
->os_dsl_dataset
->ds_object
!=
2512 dsl_dir_phys(dd
)->dd_head_dataset_obj
)
2513 return (SET_ERROR(ENOENT
));
2515 zap_cursor_init_serialized(&cursor
,
2516 dd
->dd_pool
->dp_meta_objset
,
2517 dsl_dir_phys(dd
)->dd_child_dir_zapobj
, *offp
);
2519 if (zap_cursor_retrieve(&cursor
, &attr
) != 0) {
2520 zap_cursor_fini(&cursor
);
2521 return (SET_ERROR(ENOENT
));
2524 if (strlen(attr
.za_name
) + 1 > namelen
) {
2525 zap_cursor_fini(&cursor
);
2526 return (SET_ERROR(ENAMETOOLONG
));
2529 (void) strcpy(name
, attr
.za_name
);
2531 *idp
= attr
.za_first_integer
;
2532 zap_cursor_advance(&cursor
);
2533 *offp
= zap_cursor_serialize(&cursor
);
2534 zap_cursor_fini(&cursor
);
2539 typedef struct dmu_objset_find_ctx
{
2543 char *dc_ddname
; /* last component of ddobj's name */
2544 int (*dc_func
)(dsl_pool_t
*, dsl_dataset_t
*, void *);
2547 kmutex_t
*dc_error_lock
;
2549 } dmu_objset_find_ctx_t
;
2552 dmu_objset_find_dp_impl(dmu_objset_find_ctx_t
*dcp
)
2554 dsl_pool_t
*dp
= dcp
->dc_dp
;
2558 zap_attribute_t
*attr
;
2562 /* don't process if there already was an error */
2563 if (*dcp
->dc_error
!= 0)
2567 * Note: passing the name (dc_ddname) here is optional, but it
2568 * improves performance because we don't need to call
2569 * zap_value_search() to determine the name.
2571 err
= dsl_dir_hold_obj(dp
, dcp
->dc_ddobj
, dcp
->dc_ddname
, FTAG
, &dd
);
2575 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
2576 if (dd
->dd_myname
[0] == '$') {
2577 dsl_dir_rele(dd
, FTAG
);
2581 thisobj
= dsl_dir_phys(dd
)->dd_head_dataset_obj
;
2582 attr
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
2585 * Iterate over all children.
2587 if (dcp
->dc_flags
& DS_FIND_CHILDREN
) {
2588 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
,
2589 dsl_dir_phys(dd
)->dd_child_dir_zapobj
);
2590 zap_cursor_retrieve(&zc
, attr
) == 0;
2591 (void) zap_cursor_advance(&zc
)) {
2592 ASSERT3U(attr
->za_integer_length
, ==,
2594 ASSERT3U(attr
->za_num_integers
, ==, 1);
2596 dmu_objset_find_ctx_t
*child_dcp
=
2597 kmem_alloc(sizeof (*child_dcp
), KM_SLEEP
);
2599 child_dcp
->dc_ddobj
= attr
->za_first_integer
;
2600 child_dcp
->dc_ddname
= spa_strdup(attr
->za_name
);
2601 if (dcp
->dc_tq
!= NULL
)
2602 (void) taskq_dispatch(dcp
->dc_tq
,
2603 dmu_objset_find_dp_cb
, child_dcp
, TQ_SLEEP
);
2605 dmu_objset_find_dp_impl(child_dcp
);
2607 zap_cursor_fini(&zc
);
2611 * Iterate over all snapshots.
2613 if (dcp
->dc_flags
& DS_FIND_SNAPSHOTS
) {
2615 err
= dsl_dataset_hold_obj(dp
, thisobj
, FTAG
, &ds
);
2620 snapobj
= dsl_dataset_phys(ds
)->ds_snapnames_zapobj
;
2621 dsl_dataset_rele(ds
, FTAG
);
2623 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
, snapobj
);
2624 zap_cursor_retrieve(&zc
, attr
) == 0;
2625 (void) zap_cursor_advance(&zc
)) {
2626 ASSERT3U(attr
->za_integer_length
, ==,
2628 ASSERT3U(attr
->za_num_integers
, ==, 1);
2630 err
= dsl_dataset_hold_obj(dp
,
2631 attr
->za_first_integer
, FTAG
, &ds
);
2634 err
= dcp
->dc_func(dp
, ds
, dcp
->dc_arg
);
2635 dsl_dataset_rele(ds
, FTAG
);
2639 zap_cursor_fini(&zc
);
2643 kmem_free(attr
, sizeof (zap_attribute_t
));
2646 dsl_dir_rele(dd
, FTAG
);
2653 err
= dsl_dataset_hold_obj(dp
, thisobj
, FTAG
, &ds
);
2656 * Note: we hold the dir while calling dsl_dataset_hold_obj() so
2657 * that the dir will remain cached, and we won't have to re-instantiate
2658 * it (which could be expensive due to finding its name via
2659 * zap_value_search()).
2661 dsl_dir_rele(dd
, FTAG
);
2664 err
= dcp
->dc_func(dp
, ds
, dcp
->dc_arg
);
2665 dsl_dataset_rele(ds
, FTAG
);
2669 mutex_enter(dcp
->dc_error_lock
);
2670 /* only keep first error */
2671 if (*dcp
->dc_error
== 0)
2672 *dcp
->dc_error
= err
;
2673 mutex_exit(dcp
->dc_error_lock
);
2676 if (dcp
->dc_ddname
!= NULL
)
2677 spa_strfree(dcp
->dc_ddname
);
2678 kmem_free(dcp
, sizeof (*dcp
));
2682 dmu_objset_find_dp_cb(void *arg
)
2684 dmu_objset_find_ctx_t
*dcp
= arg
;
2685 dsl_pool_t
*dp
= dcp
->dc_dp
;
2688 * We need to get a pool_config_lock here, as there are several
2689 * asssert(pool_config_held) down the stack. Getting a lock via
2690 * dsl_pool_config_enter is risky, as it might be stalled by a
2691 * pending writer. This would deadlock, as the write lock can
2692 * only be granted when our parent thread gives up the lock.
2693 * The _prio interface gives us priority over a pending writer.
2695 dsl_pool_config_enter_prio(dp
, FTAG
);
2697 dmu_objset_find_dp_impl(dcp
);
2699 dsl_pool_config_exit(dp
, FTAG
);
2703 * Find objsets under and including ddobj, call func(ds) on each.
2704 * The order for the enumeration is completely undefined.
2705 * func is called with dsl_pool_config held.
2708 dmu_objset_find_dp(dsl_pool_t
*dp
, uint64_t ddobj
,
2709 int func(dsl_pool_t
*, dsl_dataset_t
*, void *), void *arg
, int flags
)
2714 dmu_objset_find_ctx_t
*dcp
;
2717 mutex_init(&err_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
2718 dcp
= kmem_alloc(sizeof (*dcp
), KM_SLEEP
);
2721 dcp
->dc_ddobj
= ddobj
;
2722 dcp
->dc_ddname
= NULL
;
2723 dcp
->dc_func
= func
;
2725 dcp
->dc_flags
= flags
;
2726 dcp
->dc_error_lock
= &err_lock
;
2727 dcp
->dc_error
= &error
;
2729 if ((flags
& DS_FIND_SERIALIZE
) || dsl_pool_config_held_writer(dp
)) {
2731 * In case a write lock is held we can't make use of
2732 * parallelism, as down the stack of the worker threads
2733 * the lock is asserted via dsl_pool_config_held.
2734 * In case of a read lock this is solved by getting a read
2735 * lock in each worker thread, which isn't possible in case
2736 * of a writer lock. So we fall back to the synchronous path
2738 * In the future it might be possible to get some magic into
2739 * dsl_pool_config_held in a way that it returns true for
2740 * the worker threads so that a single lock held from this
2741 * thread suffices. For now, stay single threaded.
2743 dmu_objset_find_dp_impl(dcp
);
2744 mutex_destroy(&err_lock
);
2749 ntasks
= dmu_find_threads
;
2751 ntasks
= vdev_count_leaves(dp
->dp_spa
) * 4;
2752 tq
= taskq_create("dmu_objset_find", ntasks
, maxclsyspri
, ntasks
,
2755 kmem_free(dcp
, sizeof (*dcp
));
2756 mutex_destroy(&err_lock
);
2758 return (SET_ERROR(ENOMEM
));
2762 /* dcp will be freed by task */
2763 (void) taskq_dispatch(tq
, dmu_objset_find_dp_cb
, dcp
, TQ_SLEEP
);
2766 * PORTING: this code relies on the property of taskq_wait to wait
2767 * until no more tasks are queued and no more tasks are active. As
2768 * we always queue new tasks from within other tasks, task_wait
2769 * reliably waits for the full recursion to finish, even though we
2770 * enqueue new tasks after taskq_wait has been called.
2771 * On platforms other than illumos, taskq_wait may not have this
2776 mutex_destroy(&err_lock
);
2782 * Find all objsets under name, and for each, call 'func(child_name, arg)'.
2783 * The dp_config_rwlock must not be held when this is called, and it
2784 * will not be held when the callback is called.
2785 * Therefore this function should only be used when the pool is not changing
2786 * (e.g. in syncing context), or the callback can deal with the possible races.
2789 dmu_objset_find_impl(spa_t
*spa
, const char *name
,
2790 int func(const char *, void *), void *arg
, int flags
)
2793 dsl_pool_t
*dp
= spa_get_dsl(spa
);
2796 zap_attribute_t
*attr
;
2801 dsl_pool_config_enter(dp
, FTAG
);
2803 err
= dsl_dir_hold(dp
, name
, FTAG
, &dd
, NULL
);
2805 dsl_pool_config_exit(dp
, FTAG
);
2809 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
2810 if (dd
->dd_myname
[0] == '$') {
2811 dsl_dir_rele(dd
, FTAG
);
2812 dsl_pool_config_exit(dp
, FTAG
);
2816 thisobj
= dsl_dir_phys(dd
)->dd_head_dataset_obj
;
2817 attr
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
2820 * Iterate over all children.
2822 if (flags
& DS_FIND_CHILDREN
) {
2823 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
,
2824 dsl_dir_phys(dd
)->dd_child_dir_zapobj
);
2825 zap_cursor_retrieve(&zc
, attr
) == 0;
2826 (void) zap_cursor_advance(&zc
)) {
2827 ASSERT3U(attr
->za_integer_length
, ==,
2829 ASSERT3U(attr
->za_num_integers
, ==, 1);
2831 child
= kmem_asprintf("%s/%s", name
, attr
->za_name
);
2832 dsl_pool_config_exit(dp
, FTAG
);
2833 err
= dmu_objset_find_impl(spa
, child
,
2835 dsl_pool_config_enter(dp
, FTAG
);
2840 zap_cursor_fini(&zc
);
2843 dsl_dir_rele(dd
, FTAG
);
2844 dsl_pool_config_exit(dp
, FTAG
);
2845 kmem_free(attr
, sizeof (zap_attribute_t
));
2851 * Iterate over all snapshots.
2853 if (flags
& DS_FIND_SNAPSHOTS
) {
2854 err
= dsl_dataset_hold_obj(dp
, thisobj
, FTAG
, &ds
);
2859 snapobj
= dsl_dataset_phys(ds
)->ds_snapnames_zapobj
;
2860 dsl_dataset_rele(ds
, FTAG
);
2862 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
, snapobj
);
2863 zap_cursor_retrieve(&zc
, attr
) == 0;
2864 (void) zap_cursor_advance(&zc
)) {
2865 ASSERT3U(attr
->za_integer_length
, ==,
2867 ASSERT3U(attr
->za_num_integers
, ==, 1);
2869 child
= kmem_asprintf("%s@%s",
2870 name
, attr
->za_name
);
2871 dsl_pool_config_exit(dp
, FTAG
);
2872 err
= func(child
, arg
);
2873 dsl_pool_config_enter(dp
, FTAG
);
2878 zap_cursor_fini(&zc
);
2882 dsl_dir_rele(dd
, FTAG
);
2883 kmem_free(attr
, sizeof (zap_attribute_t
));
2884 dsl_pool_config_exit(dp
, FTAG
);
2889 /* Apply to self. */
2890 return (func(name
, arg
));
2894 * See comment above dmu_objset_find_impl().
2897 dmu_objset_find(char *name
, int func(const char *, void *), void *arg
,
2903 error
= spa_open(name
, &spa
, FTAG
);
2906 error
= dmu_objset_find_impl(spa
, name
, func
, arg
, flags
);
2907 spa_close(spa
, FTAG
);
2912 dmu_objset_incompatible_encryption_version(objset_t
*os
)
2914 return (dsl_dir_incompatible_encryption_version(
2915 os
->os_dsl_dataset
->ds_dir
));
2919 dmu_objset_set_user(objset_t
*os
, void *user_ptr
)
2921 ASSERT(MUTEX_HELD(&os
->os_user_ptr_lock
));
2922 os
->os_user_ptr
= user_ptr
;
2926 dmu_objset_get_user(objset_t
*os
)
2928 ASSERT(MUTEX_HELD(&os
->os_user_ptr_lock
));
2929 return (os
->os_user_ptr
);
2933 * Determine name of filesystem, given name of snapshot.
2934 * buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes
2937 dmu_fsname(const char *snapname
, char *buf
)
2939 char *atp
= strchr(snapname
, '@');
2941 return (SET_ERROR(EINVAL
));
2942 if (atp
- snapname
>= ZFS_MAX_DATASET_NAME_LEN
)
2943 return (SET_ERROR(ENAMETOOLONG
));
2944 (void) strlcpy(buf
, snapname
, atp
- snapname
+ 1);
2949 * Call when we think we're going to write/free space in open context to track
2950 * the amount of dirty data in the open txg, which is also the amount
2951 * of memory that can not be evicted until this txg syncs.
2954 dmu_objset_willuse_space(objset_t
*os
, int64_t space
, dmu_tx_t
*tx
)
2956 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
2957 int64_t aspace
= spa_get_worst_case_asize(os
->os_spa
, space
);
2960 dsl_dir_willuse_space(ds
->ds_dir
, aspace
, tx
);
2961 dsl_pool_dirty_space(dmu_tx_pool(tx
), space
, tx
);
2965 #if defined(_KERNEL)
2966 EXPORT_SYMBOL(dmu_objset_zil
);
2967 EXPORT_SYMBOL(dmu_objset_pool
);
2968 EXPORT_SYMBOL(dmu_objset_ds
);
2969 EXPORT_SYMBOL(dmu_objset_type
);
2970 EXPORT_SYMBOL(dmu_objset_name
);
2971 EXPORT_SYMBOL(dmu_objset_hold
);
2972 EXPORT_SYMBOL(dmu_objset_hold_flags
);
2973 EXPORT_SYMBOL(dmu_objset_own
);
2974 EXPORT_SYMBOL(dmu_objset_rele
);
2975 EXPORT_SYMBOL(dmu_objset_rele_flags
);
2976 EXPORT_SYMBOL(dmu_objset_disown
);
2977 EXPORT_SYMBOL(dmu_objset_from_ds
);
2978 EXPORT_SYMBOL(dmu_objset_create
);
2979 EXPORT_SYMBOL(dmu_objset_clone
);
2980 EXPORT_SYMBOL(dmu_objset_stats
);
2981 EXPORT_SYMBOL(dmu_objset_fast_stat
);
2982 EXPORT_SYMBOL(dmu_objset_spa
);
2983 EXPORT_SYMBOL(dmu_objset_space
);
2984 EXPORT_SYMBOL(dmu_objset_fsid_guid
);
2985 EXPORT_SYMBOL(dmu_objset_find
);
2986 EXPORT_SYMBOL(dmu_objset_byteswap
);
2987 EXPORT_SYMBOL(dmu_objset_evict_dbufs
);
2988 EXPORT_SYMBOL(dmu_objset_snap_cmtime
);
2989 EXPORT_SYMBOL(dmu_objset_dnodesize
);
2991 EXPORT_SYMBOL(dmu_objset_sync
);
2992 EXPORT_SYMBOL(dmu_objset_is_dirty
);
2993 EXPORT_SYMBOL(dmu_objset_create_impl_dnstats
);
2994 EXPORT_SYMBOL(dmu_objset_create_impl
);
2995 EXPORT_SYMBOL(dmu_objset_open_impl
);
2996 EXPORT_SYMBOL(dmu_objset_evict
);
2997 EXPORT_SYMBOL(dmu_objset_register_type
);
2998 EXPORT_SYMBOL(dmu_objset_do_userquota_updates
);
2999 EXPORT_SYMBOL(dmu_objset_userquota_get_ids
);
3000 EXPORT_SYMBOL(dmu_objset_userused_enabled
);
3001 EXPORT_SYMBOL(dmu_objset_userspace_upgrade
);
3002 EXPORT_SYMBOL(dmu_objset_userspace_present
);
3003 EXPORT_SYMBOL(dmu_objset_userobjused_enabled
);
3004 EXPORT_SYMBOL(dmu_objset_userobjspace_upgradable
);
3005 EXPORT_SYMBOL(dmu_objset_userobjspace_present
);
3006 EXPORT_SYMBOL(dmu_objset_projectquota_enabled
);
3007 EXPORT_SYMBOL(dmu_objset_projectquota_present
);
3008 EXPORT_SYMBOL(dmu_objset_projectquota_upgradable
);
3009 EXPORT_SYMBOL(dmu_objset_id_quota_upgrade
);