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/policy.h>
59 #include <sys/spa_impl.h>
60 #include <sys/dmu_send.h>
63 * Needed to close a window in dnode_move() that allows the objset to be freed
64 * before it can be safely accessed.
69 * Tunable to overwrite the maximum number of threads for the parallelization
70 * of dmu_objset_find_dp, needed to speed up the import of pools with many
72 * Default is 4 times the number of leaf vdevs.
74 int dmu_find_threads
= 0;
77 * Backfill lower metadnode objects after this many have been freed.
78 * Backfilling negatively impacts object creation rates, so only do it
79 * if there are enough holes to fill.
81 int dmu_rescan_dnode_threshold
= 1 << DN_MAX_INDBLKSHIFT
;
83 static char *upgrade_tag
= "upgrade_tag";
85 static void dmu_objset_find_dp_cb(void *arg
);
87 static void dmu_objset_upgrade(objset_t
*os
, dmu_objset_upgrade_cb_t cb
);
88 static void dmu_objset_upgrade_stop(objset_t
*os
);
93 rw_init(&os_lock
, NULL
, RW_DEFAULT
, NULL
);
103 dmu_objset_spa(objset_t
*os
)
109 dmu_objset_zil(objset_t
*os
)
115 dmu_objset_pool(objset_t
*os
)
119 if ((ds
= os
->os_dsl_dataset
) != NULL
&& ds
->ds_dir
)
120 return (ds
->ds_dir
->dd_pool
);
122 return (spa_get_dsl(os
->os_spa
));
126 dmu_objset_ds(objset_t
*os
)
128 return (os
->os_dsl_dataset
);
132 dmu_objset_type(objset_t
*os
)
134 return (os
->os_phys
->os_type
);
138 dmu_objset_name(objset_t
*os
, char *buf
)
140 dsl_dataset_name(os
->os_dsl_dataset
, buf
);
144 dmu_objset_id(objset_t
*os
)
146 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
148 return (ds
? ds
->ds_object
: 0);
152 dmu_objset_dnodesize(objset_t
*os
)
154 return (os
->os_dnodesize
);
158 dmu_objset_syncprop(objset_t
*os
)
160 return (os
->os_sync
);
164 dmu_objset_logbias(objset_t
*os
)
166 return (os
->os_logbias
);
170 checksum_changed_cb(void *arg
, uint64_t newval
)
175 * Inheritance should have been done by now.
177 ASSERT(newval
!= ZIO_CHECKSUM_INHERIT
);
179 os
->os_checksum
= zio_checksum_select(newval
, ZIO_CHECKSUM_ON_VALUE
);
183 compression_changed_cb(void *arg
, uint64_t newval
)
188 * Inheritance and range checking should have been done by now.
190 ASSERT(newval
!= ZIO_COMPRESS_INHERIT
);
192 os
->os_compress
= zio_compress_select(os
->os_spa
, newval
,
197 copies_changed_cb(void *arg
, uint64_t newval
)
202 * Inheritance and range checking should have been done by now.
205 ASSERT(newval
<= spa_max_replication(os
->os_spa
));
207 os
->os_copies
= newval
;
211 dedup_changed_cb(void *arg
, uint64_t newval
)
214 spa_t
*spa
= os
->os_spa
;
215 enum zio_checksum checksum
;
218 * Inheritance should have been done by now.
220 ASSERT(newval
!= ZIO_CHECKSUM_INHERIT
);
222 checksum
= zio_checksum_dedup_select(spa
, newval
, ZIO_CHECKSUM_OFF
);
224 os
->os_dedup_checksum
= checksum
& ZIO_CHECKSUM_MASK
;
225 os
->os_dedup_verify
= !!(checksum
& ZIO_CHECKSUM_VERIFY
);
229 primary_cache_changed_cb(void *arg
, uint64_t newval
)
234 * Inheritance and range checking should have been done by now.
236 ASSERT(newval
== ZFS_CACHE_ALL
|| newval
== ZFS_CACHE_NONE
||
237 newval
== ZFS_CACHE_METADATA
);
239 os
->os_primary_cache
= newval
;
243 secondary_cache_changed_cb(void *arg
, uint64_t newval
)
248 * Inheritance and range checking should have been done by now.
250 ASSERT(newval
== ZFS_CACHE_ALL
|| newval
== ZFS_CACHE_NONE
||
251 newval
== ZFS_CACHE_METADATA
);
253 os
->os_secondary_cache
= newval
;
257 sync_changed_cb(void *arg
, uint64_t newval
)
262 * Inheritance and range checking should have been done by now.
264 ASSERT(newval
== ZFS_SYNC_STANDARD
|| newval
== ZFS_SYNC_ALWAYS
||
265 newval
== ZFS_SYNC_DISABLED
);
267 os
->os_sync
= newval
;
269 zil_set_sync(os
->os_zil
, newval
);
273 redundant_metadata_changed_cb(void *arg
, uint64_t newval
)
278 * Inheritance and range checking should have been done by now.
280 ASSERT(newval
== ZFS_REDUNDANT_METADATA_ALL
||
281 newval
== ZFS_REDUNDANT_METADATA_MOST
);
283 os
->os_redundant_metadata
= newval
;
287 dnodesize_changed_cb(void *arg
, uint64_t newval
)
292 case ZFS_DNSIZE_LEGACY
:
293 os
->os_dnodesize
= DNODE_MIN_SIZE
;
295 case ZFS_DNSIZE_AUTO
:
297 * Choose a dnode size that will work well for most
298 * workloads if the user specified "auto". Future code
299 * improvements could dynamically select a dnode size
300 * based on observed workload patterns.
302 os
->os_dnodesize
= DNODE_MIN_SIZE
* 2;
309 os
->os_dnodesize
= newval
;
315 logbias_changed_cb(void *arg
, uint64_t newval
)
319 ASSERT(newval
== ZFS_LOGBIAS_LATENCY
||
320 newval
== ZFS_LOGBIAS_THROUGHPUT
);
321 os
->os_logbias
= newval
;
323 zil_set_logbias(os
->os_zil
, newval
);
327 recordsize_changed_cb(void *arg
, uint64_t newval
)
331 os
->os_recordsize
= newval
;
335 dmu_objset_byteswap(void *buf
, size_t size
)
337 objset_phys_t
*osp
= buf
;
339 ASSERT(size
== OBJSET_OLD_PHYS_SIZE
|| size
== sizeof (objset_phys_t
));
340 dnode_byteswap(&osp
->os_meta_dnode
);
341 byteswap_uint64_array(&osp
->os_zil_header
, sizeof (zil_header_t
));
342 osp
->os_type
= BSWAP_64(osp
->os_type
);
343 osp
->os_flags
= BSWAP_64(osp
->os_flags
);
344 if (size
== sizeof (objset_phys_t
)) {
345 dnode_byteswap(&osp
->os_userused_dnode
);
346 dnode_byteswap(&osp
->os_groupused_dnode
);
351 * The hash is a CRC-based hash of the objset_t pointer and the object number.
354 dnode_hash(const objset_t
*os
, uint64_t obj
)
356 uintptr_t osv
= (uintptr_t)os
;
357 uint64_t crc
= -1ULL;
359 ASSERT(zfs_crc64_table
[128] == ZFS_CRC64_POLY
);
361 * The low 6 bits of the pointer don't have much entropy, because
362 * the objset_t is larger than 2^6 bytes long.
364 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (osv
>> 6)) & 0xFF];
365 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 0)) & 0xFF];
366 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 8)) & 0xFF];
367 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 16)) & 0xFF];
369 crc
^= (osv
>>14) ^ (obj
>>24);
375 dnode_multilist_index_func(multilist_t
*ml
, void *obj
)
378 return (dnode_hash(dn
->dn_objset
, dn
->dn_object
) %
379 multilist_get_num_sublists(ml
));
383 dmu_objset_open_impl(spa_t
*spa
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
389 ASSERT(ds
== NULL
|| MUTEX_HELD(&ds
->ds_opening_lock
));
391 os
= kmem_zalloc(sizeof (objset_t
), KM_SLEEP
);
392 os
->os_dsl_dataset
= ds
;
395 if (!BP_IS_HOLE(os
->os_rootbp
)) {
396 arc_flags_t aflags
= ARC_FLAG_WAIT
;
398 enum zio_flag zio_flags
= ZIO_FLAG_CANFAIL
;
399 SET_BOOKMARK(&zb
, ds
? ds
->ds_object
: DMU_META_OBJSET
,
400 ZB_ROOT_OBJECT
, ZB_ROOT_LEVEL
, ZB_ROOT_BLKID
);
402 if (DMU_OS_IS_L2CACHEABLE(os
))
403 aflags
|= ARC_FLAG_L2CACHE
;
405 if (ds
!= NULL
&& ds
->ds_dir
->dd_crypto_obj
!= 0) {
406 ASSERT3U(BP_GET_COMPRESS(bp
), ==, ZIO_COMPRESS_OFF
);
407 ASSERT(BP_IS_AUTHENTICATED(bp
));
408 zio_flags
|= ZIO_FLAG_RAW
;
411 dprintf_bp(os
->os_rootbp
, "reading %s", "");
412 err
= arc_read(NULL
, spa
, os
->os_rootbp
,
413 arc_getbuf_func
, &os
->os_phys_buf
,
414 ZIO_PRIORITY_SYNC_READ
, zio_flags
, &aflags
, &zb
);
416 kmem_free(os
, sizeof (objset_t
));
417 /* convert checksum errors into IO errors */
419 err
= SET_ERROR(EIO
);
423 /* Increase the blocksize if we are permitted. */
424 if (spa_version(spa
) >= SPA_VERSION_USERSPACE
&&
425 arc_buf_size(os
->os_phys_buf
) < sizeof (objset_phys_t
)) {
426 arc_buf_t
*buf
= arc_alloc_buf(spa
, &os
->os_phys_buf
,
427 ARC_BUFC_METADATA
, sizeof (objset_phys_t
));
428 bzero(buf
->b_data
, sizeof (objset_phys_t
));
429 bcopy(os
->os_phys_buf
->b_data
, buf
->b_data
,
430 arc_buf_size(os
->os_phys_buf
));
431 arc_buf_destroy(os
->os_phys_buf
, &os
->os_phys_buf
);
432 os
->os_phys_buf
= buf
;
435 os
->os_phys
= os
->os_phys_buf
->b_data
;
436 os
->os_flags
= os
->os_phys
->os_flags
;
438 int size
= spa_version(spa
) >= SPA_VERSION_USERSPACE
?
439 sizeof (objset_phys_t
) : OBJSET_OLD_PHYS_SIZE
;
440 os
->os_phys_buf
= arc_alloc_buf(spa
, &os
->os_phys_buf
,
441 ARC_BUFC_METADATA
, size
);
442 os
->os_phys
= os
->os_phys_buf
->b_data
;
443 bzero(os
->os_phys
, size
);
447 * Note: the changed_cb will be called once before the register
448 * func returns, thus changing the checksum/compression from the
449 * default (fletcher2/off). Snapshots don't need to know about
450 * checksum/compression/copies.
453 boolean_t needlock
= B_FALSE
;
455 os
->os_encrypted
= (ds
->ds_dir
->dd_crypto_obj
!= 0);
458 * Note: it's valid to open the objset if the dataset is
459 * long-held, in which case the pool_config lock will not
462 if (!dsl_pool_config_held(dmu_objset_pool(os
))) {
464 dsl_pool_config_enter(dmu_objset_pool(os
), FTAG
);
467 err
= dsl_prop_register(ds
,
468 zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE
),
469 primary_cache_changed_cb
, os
);
471 err
= dsl_prop_register(ds
,
472 zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE
),
473 secondary_cache_changed_cb
, os
);
475 if (!ds
->ds_is_snapshot
) {
477 err
= dsl_prop_register(ds
,
478 zfs_prop_to_name(ZFS_PROP_CHECKSUM
),
479 checksum_changed_cb
, os
);
482 err
= dsl_prop_register(ds
,
483 zfs_prop_to_name(ZFS_PROP_COMPRESSION
),
484 compression_changed_cb
, os
);
487 err
= dsl_prop_register(ds
,
488 zfs_prop_to_name(ZFS_PROP_COPIES
),
489 copies_changed_cb
, os
);
492 err
= dsl_prop_register(ds
,
493 zfs_prop_to_name(ZFS_PROP_DEDUP
),
494 dedup_changed_cb
, os
);
497 err
= dsl_prop_register(ds
,
498 zfs_prop_to_name(ZFS_PROP_LOGBIAS
),
499 logbias_changed_cb
, os
);
502 err
= dsl_prop_register(ds
,
503 zfs_prop_to_name(ZFS_PROP_SYNC
),
504 sync_changed_cb
, os
);
507 err
= dsl_prop_register(ds
,
509 ZFS_PROP_REDUNDANT_METADATA
),
510 redundant_metadata_changed_cb
, os
);
513 err
= dsl_prop_register(ds
,
514 zfs_prop_to_name(ZFS_PROP_RECORDSIZE
),
515 recordsize_changed_cb
, os
);
518 err
= dsl_prop_register(ds
,
519 zfs_prop_to_name(ZFS_PROP_DNODESIZE
),
520 dnodesize_changed_cb
, os
);
524 dsl_pool_config_exit(dmu_objset_pool(os
), FTAG
);
526 arc_buf_destroy(os
->os_phys_buf
, &os
->os_phys_buf
);
527 kmem_free(os
, sizeof (objset_t
));
531 /* It's the meta-objset. */
532 os
->os_checksum
= ZIO_CHECKSUM_FLETCHER_4
;
533 os
->os_compress
= ZIO_COMPRESS_ON
;
534 os
->os_encrypted
= B_FALSE
;
535 os
->os_copies
= spa_max_replication(spa
);
536 os
->os_dedup_checksum
= ZIO_CHECKSUM_OFF
;
537 os
->os_dedup_verify
= B_FALSE
;
538 os
->os_logbias
= ZFS_LOGBIAS_LATENCY
;
539 os
->os_sync
= ZFS_SYNC_STANDARD
;
540 os
->os_primary_cache
= ZFS_CACHE_ALL
;
541 os
->os_secondary_cache
= ZFS_CACHE_ALL
;
542 os
->os_dnodesize
= DNODE_MIN_SIZE
;
545 if (ds
== NULL
|| !ds
->ds_is_snapshot
)
546 os
->os_zil_header
= os
->os_phys
->os_zil_header
;
547 os
->os_zil
= zil_alloc(os
, &os
->os_zil_header
);
549 for (i
= 0; i
< TXG_SIZE
; i
++) {
550 os
->os_dirty_dnodes
[i
] = multilist_create(sizeof (dnode_t
),
551 offsetof(dnode_t
, dn_dirty_link
[i
]),
552 dnode_multilist_index_func
);
554 list_create(&os
->os_dnodes
, sizeof (dnode_t
),
555 offsetof(dnode_t
, dn_link
));
556 list_create(&os
->os_downgraded_dbufs
, sizeof (dmu_buf_impl_t
),
557 offsetof(dmu_buf_impl_t
, db_link
));
559 list_link_init(&os
->os_evicting_node
);
561 mutex_init(&os
->os_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
562 mutex_init(&os
->os_userused_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
563 mutex_init(&os
->os_obj_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
564 mutex_init(&os
->os_user_ptr_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
565 os
->os_obj_next_percpu_len
= boot_ncpus
;
566 os
->os_obj_next_percpu
= kmem_zalloc(os
->os_obj_next_percpu_len
*
567 sizeof (os
->os_obj_next_percpu
[0]), KM_SLEEP
);
569 dnode_special_open(os
, &os
->os_phys
->os_meta_dnode
,
570 DMU_META_DNODE_OBJECT
, &os
->os_meta_dnode
);
571 if (arc_buf_size(os
->os_phys_buf
) >= sizeof (objset_phys_t
)) {
572 dnode_special_open(os
, &os
->os_phys
->os_userused_dnode
,
573 DMU_USERUSED_OBJECT
, &os
->os_userused_dnode
);
574 dnode_special_open(os
, &os
->os_phys
->os_groupused_dnode
,
575 DMU_GROUPUSED_OBJECT
, &os
->os_groupused_dnode
);
578 mutex_init(&os
->os_upgrade_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
585 dmu_objset_from_ds(dsl_dataset_t
*ds
, objset_t
**osp
)
590 * We shouldn't be doing anything with dsl_dataset_t's unless the
591 * pool_config lock is held, or the dataset is long-held.
593 ASSERT(dsl_pool_config_held(ds
->ds_dir
->dd_pool
) ||
594 dsl_dataset_long_held(ds
));
596 mutex_enter(&ds
->ds_opening_lock
);
597 if (ds
->ds_objset
== NULL
) {
599 rrw_enter(&ds
->ds_bp_rwlock
, RW_READER
, FTAG
);
600 err
= dmu_objset_open_impl(dsl_dataset_get_spa(ds
),
601 ds
, dsl_dataset_get_blkptr(ds
), &os
);
602 rrw_exit(&ds
->ds_bp_rwlock
, FTAG
);
605 mutex_enter(&ds
->ds_lock
);
606 ASSERT(ds
->ds_objset
== NULL
);
608 mutex_exit(&ds
->ds_lock
);
611 *osp
= ds
->ds_objset
;
612 mutex_exit(&ds
->ds_opening_lock
);
617 * Holds the pool while the objset is held. Therefore only one objset
618 * can be held at a time.
621 dmu_objset_hold_flags(const char *name
, boolean_t decrypt
, void *tag
,
627 ds_hold_flags_t flags
= (decrypt
) ? DS_HOLD_FLAG_DECRYPT
: 0;
629 err
= dsl_pool_hold(name
, tag
, &dp
);
632 err
= dsl_dataset_hold_flags(dp
, name
, flags
, tag
, &ds
);
634 dsl_pool_rele(dp
, tag
);
638 err
= dmu_objset_from_ds(ds
, osp
);
640 dsl_dataset_rele(ds
, tag
);
641 dsl_pool_rele(dp
, tag
);
648 dmu_objset_hold(const char *name
, void *tag
, objset_t
**osp
)
650 return (dmu_objset_hold_flags(name
, B_FALSE
, tag
, osp
));
654 dmu_objset_own_impl(dsl_dataset_t
*ds
, dmu_objset_type_t type
,
655 boolean_t readonly
, boolean_t decrypt
, void *tag
, objset_t
**osp
)
659 err
= dmu_objset_from_ds(ds
, osp
);
662 } else if (type
!= DMU_OST_ANY
&& type
!= (*osp
)->os_phys
->os_type
) {
663 return (SET_ERROR(EINVAL
));
664 } else if (!readonly
&& dsl_dataset_is_snapshot(ds
)) {
665 return (SET_ERROR(EROFS
));
666 } else if (!readonly
&& decrypt
&&
667 dsl_dir_incompatible_encryption_version(ds
->ds_dir
)) {
668 return (SET_ERROR(EROFS
));
671 /* if we are decrypting, we can now check MACs in os->os_phys_buf */
672 if (decrypt
&& arc_is_unauthenticated((*osp
)->os_phys_buf
)) {
673 err
= arc_untransform((*osp
)->os_phys_buf
, (*osp
)->os_spa
,
674 ds
->ds_object
, B_FALSE
);
678 ASSERT0(arc_is_unauthenticated((*osp
)->os_phys_buf
));
685 * dsl_pool must not be held when this is called.
686 * Upon successful return, there will be a longhold on the dataset,
687 * and the dsl_pool will not be held.
690 dmu_objset_own(const char *name
, dmu_objset_type_t type
,
691 boolean_t readonly
, boolean_t decrypt
, void *tag
, objset_t
**osp
)
696 ds_hold_flags_t flags
= (decrypt
) ? DS_HOLD_FLAG_DECRYPT
: 0;
698 err
= dsl_pool_hold(name
, FTAG
, &dp
);
701 err
= dsl_dataset_own(dp
, name
, flags
, tag
, &ds
);
703 dsl_pool_rele(dp
, FTAG
);
706 err
= dmu_objset_own_impl(ds
, type
, readonly
, decrypt
, tag
, osp
);
708 dsl_dataset_disown(ds
, flags
, tag
);
709 dsl_pool_rele(dp
, FTAG
);
713 /* user accounting requires the dataset to be decrypted */
714 if (dmu_objset_userobjspace_upgradable(*osp
) &&
715 (ds
->ds_dir
->dd_crypto_obj
== 0 || decrypt
))
716 dmu_objset_userobjspace_upgrade(*osp
);
718 dsl_pool_rele(dp
, FTAG
);
723 dmu_objset_own_obj(dsl_pool_t
*dp
, uint64_t obj
, dmu_objset_type_t type
,
724 boolean_t readonly
, boolean_t decrypt
, void *tag
, objset_t
**osp
)
728 ds_hold_flags_t flags
= (decrypt
) ? DS_HOLD_FLAG_DECRYPT
: 0;
730 err
= dsl_dataset_own_obj(dp
, obj
, flags
, tag
, &ds
);
734 err
= dmu_objset_own_impl(ds
, type
, readonly
, decrypt
, tag
, osp
);
736 dsl_dataset_disown(ds
, flags
, tag
);
744 dmu_objset_rele_flags(objset_t
*os
, boolean_t decrypt
, void *tag
)
746 ds_hold_flags_t flags
= (decrypt
) ? DS_HOLD_FLAG_DECRYPT
: 0;
748 dsl_pool_t
*dp
= dmu_objset_pool(os
);
749 dsl_dataset_rele_flags(os
->os_dsl_dataset
, flags
, tag
);
750 dsl_pool_rele(dp
, tag
);
754 dmu_objset_rele(objset_t
*os
, void *tag
)
756 dmu_objset_rele_flags(os
, B_FALSE
, tag
);
760 * When we are called, os MUST refer to an objset associated with a dataset
761 * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner
762 * == tag. We will then release and reacquire ownership of the dataset while
763 * holding the pool config_rwlock to avoid intervening namespace or ownership
766 * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to
767 * release the hold on its dataset and acquire a new one on the dataset of the
768 * same name so that it can be partially torn down and reconstructed.
771 dmu_objset_refresh_ownership(objset_t
*os
, boolean_t decrypt
, void *tag
)
774 dsl_dataset_t
*ds
, *newds
;
775 char name
[ZFS_MAX_DATASET_NAME_LEN
];
777 ds
= os
->os_dsl_dataset
;
778 VERIFY3P(ds
, !=, NULL
);
779 VERIFY3P(ds
->ds_owner
, ==, tag
);
780 VERIFY(dsl_dataset_long_held(ds
));
782 dsl_dataset_name(ds
, name
);
783 dp
= dmu_objset_pool(os
);
784 dsl_pool_config_enter(dp
, FTAG
);
785 dmu_objset_disown(os
, decrypt
, tag
);
786 VERIFY0(dsl_dataset_own(dp
, name
,
787 (decrypt
) ? DS_HOLD_FLAG_DECRYPT
: 0, tag
, &newds
));
788 VERIFY3P(newds
, ==, os
->os_dsl_dataset
);
789 dsl_pool_config_exit(dp
, FTAG
);
793 dmu_objset_disown(objset_t
*os
, boolean_t decrypt
, void *tag
)
796 * Stop upgrading thread
798 dmu_objset_upgrade_stop(os
);
799 dsl_dataset_disown(os
->os_dsl_dataset
,
800 (decrypt
) ? DS_HOLD_FLAG_DECRYPT
: 0, tag
);
804 dmu_objset_evict_dbufs(objset_t
*os
)
809 dn_marker
= kmem_alloc(sizeof (dnode_t
), KM_SLEEP
);
811 mutex_enter(&os
->os_lock
);
812 dn
= list_head(&os
->os_dnodes
);
815 * Skip dnodes without holds. We have to do this dance
816 * because dnode_add_ref() only works if there is already a
817 * hold. If the dnode has no holds, then it has no dbufs.
819 if (dnode_add_ref(dn
, FTAG
)) {
820 list_insert_after(&os
->os_dnodes
, dn
, dn_marker
);
821 mutex_exit(&os
->os_lock
);
823 dnode_evict_dbufs(dn
);
824 dnode_rele(dn
, FTAG
);
826 mutex_enter(&os
->os_lock
);
827 dn
= list_next(&os
->os_dnodes
, dn_marker
);
828 list_remove(&os
->os_dnodes
, dn_marker
);
830 dn
= list_next(&os
->os_dnodes
, dn
);
833 mutex_exit(&os
->os_lock
);
835 kmem_free(dn_marker
, sizeof (dnode_t
));
837 if (DMU_USERUSED_DNODE(os
) != NULL
) {
838 dnode_evict_dbufs(DMU_GROUPUSED_DNODE(os
));
839 dnode_evict_dbufs(DMU_USERUSED_DNODE(os
));
841 dnode_evict_dbufs(DMU_META_DNODE(os
));
845 * Objset eviction processing is split into into two pieces.
846 * The first marks the objset as evicting, evicts any dbufs that
847 * have a refcount of zero, and then queues up the objset for the
848 * second phase of eviction. Once os->os_dnodes has been cleared by
849 * dnode_buf_pageout()->dnode_destroy(), the second phase is executed.
850 * The second phase closes the special dnodes, dequeues the objset from
851 * the list of those undergoing eviction, and finally frees the objset.
853 * NOTE: Due to asynchronous eviction processing (invocation of
854 * dnode_buf_pageout()), it is possible for the meta dnode for the
855 * objset to have no holds even though os->os_dnodes is not empty.
858 dmu_objset_evict(objset_t
*os
)
860 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
862 for (int t
= 0; t
< TXG_SIZE
; t
++)
863 ASSERT(!dmu_objset_is_dirty(os
, t
));
866 dsl_prop_unregister_all(ds
, os
);
871 dmu_objset_evict_dbufs(os
);
873 mutex_enter(&os
->os_lock
);
874 spa_evicting_os_register(os
->os_spa
, os
);
875 if (list_is_empty(&os
->os_dnodes
)) {
876 mutex_exit(&os
->os_lock
);
877 dmu_objset_evict_done(os
);
879 mutex_exit(&os
->os_lock
);
886 dmu_objset_evict_done(objset_t
*os
)
888 ASSERT3P(list_head(&os
->os_dnodes
), ==, NULL
);
890 dnode_special_close(&os
->os_meta_dnode
);
891 if (DMU_USERUSED_DNODE(os
)) {
892 dnode_special_close(&os
->os_userused_dnode
);
893 dnode_special_close(&os
->os_groupused_dnode
);
895 zil_free(os
->os_zil
);
897 arc_buf_destroy(os
->os_phys_buf
, &os
->os_phys_buf
);
900 * This is a barrier to prevent the objset from going away in
901 * dnode_move() until we can safely ensure that the objset is still in
902 * use. We consider the objset valid before the barrier and invalid
905 rw_enter(&os_lock
, RW_READER
);
908 kmem_free(os
->os_obj_next_percpu
,
909 os
->os_obj_next_percpu_len
* sizeof (os
->os_obj_next_percpu
[0]));
911 mutex_destroy(&os
->os_lock
);
912 mutex_destroy(&os
->os_userused_lock
);
913 mutex_destroy(&os
->os_obj_lock
);
914 mutex_destroy(&os
->os_user_ptr_lock
);
915 mutex_destroy(&os
->os_upgrade_lock
);
916 for (int i
= 0; i
< TXG_SIZE
; i
++) {
917 multilist_destroy(os
->os_dirty_dnodes
[i
]);
919 spa_evicting_os_deregister(os
->os_spa
, os
);
920 kmem_free(os
, sizeof (objset_t
));
924 dmu_objset_snap_cmtime(objset_t
*os
)
926 return (dsl_dir_snap_cmtime(os
->os_dsl_dataset
->ds_dir
));
930 dmu_objset_create_impl_dnstats(spa_t
*spa
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
931 dmu_objset_type_t type
, int levels
, int blksz
, int ibs
, dmu_tx_t
*tx
)
936 ASSERT(dmu_tx_is_syncing(tx
));
939 blksz
= DNODE_BLOCK_SIZE
;
941 ibs
= DN_MAX_INDBLKSHIFT
;
944 VERIFY0(dmu_objset_from_ds(ds
, &os
));
946 VERIFY0(dmu_objset_open_impl(spa
, NULL
, bp
, &os
));
948 mdn
= DMU_META_DNODE(os
);
950 dnode_allocate(mdn
, DMU_OT_DNODE
, blksz
, ibs
, DMU_OT_NONE
, 0,
951 DNODE_MIN_SLOTS
, tx
);
954 * We don't want to have to increase the meta-dnode's nlevels
955 * later, because then we could do it in quescing context while
956 * we are also accessing it in open context.
958 * This precaution is not necessary for the MOS (ds == NULL),
959 * because the MOS is only updated in syncing context.
960 * This is most fortunate: the MOS is the only objset that
961 * needs to be synced multiple times as spa_sync() iterates
962 * to convergence, so minimizing its dn_nlevels matters.
969 * Determine the number of levels necessary for the
970 * meta-dnode to contain DN_MAX_OBJECT dnodes. Note
971 * that in order to ensure that we do not overflow
972 * 64 bits, there has to be a nlevels that gives us a
973 * number of blocks > DN_MAX_OBJECT but < 2^64.
974 * Therefore, (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)
975 * (10) must be less than (64 - log2(DN_MAX_OBJECT))
978 while ((uint64_t)mdn
->dn_nblkptr
<<
979 (mdn
->dn_datablkshift
- DNODE_SHIFT
+ (levels
- 1) *
980 (mdn
->dn_indblkshift
- SPA_BLKPTRSHIFT
)) <
985 mdn
->dn_next_nlevels
[tx
->tx_txg
& TXG_MASK
] =
986 mdn
->dn_nlevels
= levels
;
989 ASSERT(type
!= DMU_OST_NONE
);
990 ASSERT(type
!= DMU_OST_ANY
);
991 ASSERT(type
< DMU_OST_NUMTYPES
);
992 os
->os_phys
->os_type
= type
;
995 * Enable user accounting if it is enabled and this is not an
998 if (dmu_objset_userused_enabled(os
) &&
999 (!os
->os_encrypted
|| !dmu_objset_is_receiving(os
))) {
1000 os
->os_phys
->os_flags
|= OBJSET_FLAG_USERACCOUNTING_COMPLETE
;
1001 if (dmu_objset_userobjused_enabled(os
)) {
1002 ds
->ds_feature_activation_needed
[
1003 SPA_FEATURE_USEROBJ_ACCOUNTING
] = B_TRUE
;
1004 os
->os_phys
->os_flags
|=
1005 OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE
;
1007 os
->os_flags
= os
->os_phys
->os_flags
;
1010 dsl_dataset_dirty(ds
, tx
);
1015 /* called from dsl for meta-objset */
1017 dmu_objset_create_impl(spa_t
*spa
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
1018 dmu_objset_type_t type
, dmu_tx_t
*tx
)
1020 return (dmu_objset_create_impl_dnstats(spa
, ds
, bp
, type
, 0, 0, 0, tx
));
1023 typedef struct dmu_objset_create_arg
{
1024 const char *doca_name
;
1026 void (*doca_userfunc
)(objset_t
*os
, void *arg
,
1027 cred_t
*cr
, dmu_tx_t
*tx
);
1029 dmu_objset_type_t doca_type
;
1030 uint64_t doca_flags
;
1031 dsl_crypto_params_t
*doca_dcp
;
1032 } dmu_objset_create_arg_t
;
1036 dmu_objset_create_check(void *arg
, dmu_tx_t
*tx
)
1038 dmu_objset_create_arg_t
*doca
= arg
;
1039 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1044 if (strchr(doca
->doca_name
, '@') != NULL
)
1045 return (SET_ERROR(EINVAL
));
1047 if (strlen(doca
->doca_name
) >= ZFS_MAX_DATASET_NAME_LEN
)
1048 return (SET_ERROR(ENAMETOOLONG
));
1050 error
= dsl_dir_hold(dp
, doca
->doca_name
, FTAG
, &pdd
, &tail
);
1054 dsl_dir_rele(pdd
, FTAG
);
1055 return (SET_ERROR(EEXIST
));
1058 error
= dmu_objset_create_crypt_check(pdd
, doca
->doca_dcp
);
1060 dsl_dir_rele(pdd
, FTAG
);
1064 error
= dsl_fs_ss_limit_check(pdd
, 1, ZFS_PROP_FILESYSTEM_LIMIT
, NULL
,
1067 dsl_dir_rele(pdd
, FTAG
);
1073 dmu_objset_create_sync(void *arg
, dmu_tx_t
*tx
)
1075 dmu_objset_create_arg_t
*doca
= arg
;
1076 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1085 VERIFY0(dsl_dir_hold(dp
, doca
->doca_name
, FTAG
, &pdd
, &tail
));
1087 obj
= dsl_dataset_create_sync(pdd
, tail
, NULL
, doca
->doca_flags
,
1088 doca
->doca_cred
, doca
->doca_dcp
, tx
);
1090 VERIFY0(dsl_dataset_hold_obj_flags(pdd
->dd_pool
, obj
,
1091 DS_HOLD_FLAG_DECRYPT
, FTAG
, &ds
));
1092 rrw_enter(&ds
->ds_bp_rwlock
, RW_READER
, FTAG
);
1093 bp
= dsl_dataset_get_blkptr(ds
);
1094 os
= dmu_objset_create_impl(pdd
->dd_pool
->dp_spa
,
1095 ds
, bp
, doca
->doca_type
, tx
);
1096 rrw_exit(&ds
->ds_bp_rwlock
, FTAG
);
1098 if (doca
->doca_userfunc
!= NULL
) {
1099 doca
->doca_userfunc(os
, doca
->doca_userarg
,
1100 doca
->doca_cred
, tx
);
1104 * The doca_userfunc() may write out some data that needs to be
1105 * encrypted if the dataset is encrypted (specifically the root
1106 * directory). This data must be written out before the encryption
1107 * key mapping is removed by dsl_dataset_rele_flags(). Force the
1108 * I/O to occur immediately by invoking the relevant sections of
1111 if (os
->os_encrypted
) {
1112 dsl_dataset_t
*tmpds
= NULL
;
1113 boolean_t need_sync_done
= B_FALSE
;
1115 mutex_enter(&ds
->ds_lock
);
1116 ds
->ds_owner
= FTAG
;
1117 mutex_exit(&ds
->ds_lock
);
1119 rzio
= zio_root(dp
->dp_spa
, NULL
, NULL
, ZIO_FLAG_MUSTSUCCEED
);
1120 tmpds
= txg_list_remove_this(&dp
->dp_dirty_datasets
, ds
,
1122 if (tmpds
!= NULL
) {
1123 dsl_dataset_sync(ds
, rzio
, tx
);
1124 need_sync_done
= B_TRUE
;
1126 VERIFY0(zio_wait(rzio
));
1128 dmu_objset_do_userquota_updates(os
, tx
);
1129 taskq_wait(dp
->dp_sync_taskq
);
1131 rzio
= zio_root(dp
->dp_spa
, NULL
, NULL
, ZIO_FLAG_MUSTSUCCEED
);
1132 tmpds
= txg_list_remove_this(&dp
->dp_dirty_datasets
, ds
,
1134 if (tmpds
!= NULL
) {
1135 dmu_buf_rele(ds
->ds_dbuf
, ds
);
1136 dsl_dataset_sync(ds
, rzio
, tx
);
1138 VERIFY0(zio_wait(rzio
));
1141 dsl_dataset_sync_done(ds
, tx
);
1143 mutex_enter(&ds
->ds_lock
);
1144 ds
->ds_owner
= NULL
;
1145 mutex_exit(&ds
->ds_lock
);
1148 spa_history_log_internal_ds(ds
, "create", tx
, "");
1149 zvol_create_minors(dp
->dp_spa
, doca
->doca_name
, B_TRUE
);
1151 dsl_dataset_rele_flags(ds
, DS_HOLD_FLAG_DECRYPT
, FTAG
);
1152 dsl_dir_rele(pdd
, FTAG
);
1156 dmu_objset_create(const char *name
, dmu_objset_type_t type
, uint64_t flags
,
1157 dsl_crypto_params_t
*dcp
, dmu_objset_create_sync_func_t func
, void *arg
)
1159 dmu_objset_create_arg_t doca
;
1160 dsl_crypto_params_t tmp_dcp
= { 0 };
1162 doca
.doca_name
= name
;
1163 doca
.doca_cred
= CRED();
1164 doca
.doca_flags
= flags
;
1165 doca
.doca_userfunc
= func
;
1166 doca
.doca_userarg
= arg
;
1167 doca
.doca_type
= type
;
1170 * Some callers (mostly for testing) do not provide a dcp on their
1171 * own but various code inside the sync task will require it to be
1172 * allocated. Rather than adding NULL checks throughout this code
1173 * or adding dummy dcp's to all of the callers we simply create a
1174 * dummy one here and use that. This zero dcp will have the same
1175 * effect as asking for inheritence of all encryption params.
1177 doca
.doca_dcp
= (dcp
!= NULL
) ? dcp
: &tmp_dcp
;
1179 return (dsl_sync_task(name
,
1180 dmu_objset_create_check
, dmu_objset_create_sync
, &doca
,
1181 6, ZFS_SPACE_CHECK_NORMAL
));
1184 typedef struct dmu_objset_clone_arg
{
1185 const char *doca_clone
;
1186 const char *doca_origin
;
1188 } dmu_objset_clone_arg_t
;
1192 dmu_objset_clone_check(void *arg
, dmu_tx_t
*tx
)
1194 dmu_objset_clone_arg_t
*doca
= arg
;
1198 dsl_dataset_t
*origin
;
1199 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1201 if (strchr(doca
->doca_clone
, '@') != NULL
)
1202 return (SET_ERROR(EINVAL
));
1204 if (strlen(doca
->doca_clone
) >= ZFS_MAX_DATASET_NAME_LEN
)
1205 return (SET_ERROR(ENAMETOOLONG
));
1207 error
= dsl_dir_hold(dp
, doca
->doca_clone
, FTAG
, &pdd
, &tail
);
1211 dsl_dir_rele(pdd
, FTAG
);
1212 return (SET_ERROR(EEXIST
));
1215 error
= dsl_fs_ss_limit_check(pdd
, 1, ZFS_PROP_FILESYSTEM_LIMIT
, NULL
,
1218 dsl_dir_rele(pdd
, FTAG
);
1219 return (SET_ERROR(EDQUOT
));
1222 error
= dsl_dataset_hold(dp
, doca
->doca_origin
, FTAG
, &origin
);
1224 dsl_dir_rele(pdd
, FTAG
);
1228 /* You can only clone snapshots, not the head datasets. */
1229 if (!origin
->ds_is_snapshot
) {
1230 dsl_dataset_rele(origin
, FTAG
);
1231 dsl_dir_rele(pdd
, FTAG
);
1232 return (SET_ERROR(EINVAL
));
1235 error
= dmu_objset_clone_crypt_check(pdd
, origin
->ds_dir
);
1237 dsl_dataset_rele(origin
, FTAG
);
1238 dsl_dir_rele(pdd
, FTAG
);
1242 dsl_dataset_rele(origin
, FTAG
);
1243 dsl_dir_rele(pdd
, FTAG
);
1249 dmu_objset_clone_sync(void *arg
, dmu_tx_t
*tx
)
1251 dmu_objset_clone_arg_t
*doca
= arg
;
1252 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1255 dsl_dataset_t
*origin
, *ds
;
1257 char namebuf
[ZFS_MAX_DATASET_NAME_LEN
];
1259 VERIFY0(dsl_dir_hold(dp
, doca
->doca_clone
, FTAG
, &pdd
, &tail
));
1260 VERIFY0(dsl_dataset_hold(dp
, doca
->doca_origin
, FTAG
, &origin
));
1262 obj
= dsl_dataset_create_sync(pdd
, tail
, origin
, 0,
1263 doca
->doca_cred
, NULL
, tx
);
1265 VERIFY0(dsl_dataset_hold_obj(pdd
->dd_pool
, obj
, FTAG
, &ds
));
1266 dsl_dataset_name(origin
, namebuf
);
1267 spa_history_log_internal_ds(ds
, "clone", tx
,
1268 "origin=%s (%llu)", namebuf
, origin
->ds_object
);
1269 zvol_create_minors(dp
->dp_spa
, doca
->doca_clone
, B_TRUE
);
1270 dsl_dataset_rele(ds
, FTAG
);
1271 dsl_dataset_rele(origin
, FTAG
);
1272 dsl_dir_rele(pdd
, FTAG
);
1276 dmu_objset_clone(const char *clone
, const char *origin
)
1278 dmu_objset_clone_arg_t doca
;
1280 doca
.doca_clone
= clone
;
1281 doca
.doca_origin
= origin
;
1282 doca
.doca_cred
= CRED();
1284 return (dsl_sync_task(clone
,
1285 dmu_objset_clone_check
, dmu_objset_clone_sync
, &doca
,
1286 6, ZFS_SPACE_CHECK_NORMAL
));
1290 dmu_objset_snapshot_one(const char *fsname
, const char *snapname
)
1293 char *longsnap
= kmem_asprintf("%s@%s", fsname
, snapname
);
1294 nvlist_t
*snaps
= fnvlist_alloc();
1296 fnvlist_add_boolean(snaps
, longsnap
);
1298 err
= dsl_dataset_snapshot(snaps
, NULL
, NULL
);
1299 fnvlist_free(snaps
);
1304 dmu_objset_upgrade_task_cb(void *data
)
1306 objset_t
*os
= data
;
1308 mutex_enter(&os
->os_upgrade_lock
);
1309 os
->os_upgrade_status
= EINTR
;
1310 if (!os
->os_upgrade_exit
) {
1311 mutex_exit(&os
->os_upgrade_lock
);
1313 os
->os_upgrade_status
= os
->os_upgrade_cb(os
);
1314 mutex_enter(&os
->os_upgrade_lock
);
1316 os
->os_upgrade_exit
= B_TRUE
;
1317 os
->os_upgrade_id
= 0;
1318 mutex_exit(&os
->os_upgrade_lock
);
1319 dsl_dataset_long_rele(dmu_objset_ds(os
), upgrade_tag
);
1323 dmu_objset_upgrade(objset_t
*os
, dmu_objset_upgrade_cb_t cb
)
1325 if (os
->os_upgrade_id
!= 0)
1328 ASSERT(dsl_pool_config_held(dmu_objset_pool(os
)));
1329 dsl_dataset_long_hold(dmu_objset_ds(os
), upgrade_tag
);
1331 mutex_enter(&os
->os_upgrade_lock
);
1332 if (os
->os_upgrade_id
== 0 && os
->os_upgrade_status
== 0) {
1333 os
->os_upgrade_exit
= B_FALSE
;
1334 os
->os_upgrade_cb
= cb
;
1335 os
->os_upgrade_id
= taskq_dispatch(
1336 os
->os_spa
->spa_upgrade_taskq
,
1337 dmu_objset_upgrade_task_cb
, os
, TQ_SLEEP
);
1338 if (os
->os_upgrade_id
== TASKQID_INVALID
) {
1339 dsl_dataset_long_rele(dmu_objset_ds(os
), upgrade_tag
);
1340 os
->os_upgrade_status
= ENOMEM
;
1343 mutex_exit(&os
->os_upgrade_lock
);
1347 dmu_objset_upgrade_stop(objset_t
*os
)
1349 mutex_enter(&os
->os_upgrade_lock
);
1350 os
->os_upgrade_exit
= B_TRUE
;
1351 if (os
->os_upgrade_id
!= 0) {
1352 taskqid_t id
= os
->os_upgrade_id
;
1354 os
->os_upgrade_id
= 0;
1355 mutex_exit(&os
->os_upgrade_lock
);
1357 if ((taskq_cancel_id(os
->os_spa
->spa_upgrade_taskq
, id
)) == 0) {
1358 dsl_dataset_long_rele(dmu_objset_ds(os
), upgrade_tag
);
1360 txg_wait_synced(os
->os_spa
->spa_dsl_pool
, 0);
1362 mutex_exit(&os
->os_upgrade_lock
);
1367 dmu_objset_sync_dnodes(multilist_sublist_t
*list
, dmu_tx_t
*tx
)
1371 while ((dn
= multilist_sublist_head(list
)) != NULL
) {
1372 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
);
1373 ASSERT(dn
->dn_dbuf
->db_data_pending
);
1375 * Initialize dn_zio outside dnode_sync() because the
1376 * meta-dnode needs to set it ouside dnode_sync().
1378 dn
->dn_zio
= dn
->dn_dbuf
->db_data_pending
->dr_zio
;
1381 ASSERT3U(dn
->dn_nlevels
, <=, DN_MAX_LEVELS
);
1382 multilist_sublist_remove(list
, dn
);
1384 multilist_t
*newlist
= dn
->dn_objset
->os_synced_dnodes
;
1385 if (newlist
!= NULL
) {
1386 (void) dnode_add_ref(dn
, newlist
);
1387 multilist_insert(newlist
, dn
);
1396 dmu_objset_write_ready(zio_t
*zio
, arc_buf_t
*abuf
, void *arg
)
1398 blkptr_t
*bp
= zio
->io_bp
;
1400 dnode_phys_t
*dnp
= &os
->os_phys
->os_meta_dnode
;
1403 ASSERT(!BP_IS_EMBEDDED(bp
));
1404 ASSERT3U(BP_GET_TYPE(bp
), ==, DMU_OT_OBJSET
);
1405 ASSERT0(BP_GET_LEVEL(bp
));
1408 * Update rootbp fill count: it should be the number of objects
1409 * allocated in the object set (not counting the "special"
1410 * objects that are stored in the objset_phys_t -- the meta
1411 * dnode and user/group accounting objects).
1413 for (int i
= 0; i
< dnp
->dn_nblkptr
; i
++)
1414 fill
+= BP_GET_FILL(&dnp
->dn_blkptr
[i
]);
1416 BP_SET_FILL(bp
, fill
);
1418 if (os
->os_dsl_dataset
!= NULL
)
1419 rrw_enter(&os
->os_dsl_dataset
->ds_bp_rwlock
, RW_WRITER
, FTAG
);
1420 *os
->os_rootbp
= *bp
;
1421 if (os
->os_dsl_dataset
!= NULL
)
1422 rrw_exit(&os
->os_dsl_dataset
->ds_bp_rwlock
, FTAG
);
1427 dmu_objset_write_done(zio_t
*zio
, arc_buf_t
*abuf
, void *arg
)
1429 blkptr_t
*bp
= zio
->io_bp
;
1430 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
1433 if (zio
->io_flags
& ZIO_FLAG_IO_REWRITE
) {
1434 ASSERT(BP_EQUAL(bp
, bp_orig
));
1436 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
1437 dmu_tx_t
*tx
= os
->os_synctx
;
1439 (void) dsl_dataset_block_kill(ds
, bp_orig
, tx
, B_TRUE
);
1440 dsl_dataset_block_born(ds
, bp
, tx
);
1442 kmem_free(bp
, sizeof (*bp
));
1445 typedef struct sync_dnodes_arg
{
1446 multilist_t
*sda_list
;
1447 int sda_sublist_idx
;
1448 multilist_t
*sda_newlist
;
1450 } sync_dnodes_arg_t
;
1453 sync_dnodes_task(void *arg
)
1455 sync_dnodes_arg_t
*sda
= arg
;
1457 multilist_sublist_t
*ms
=
1458 multilist_sublist_lock(sda
->sda_list
, sda
->sda_sublist_idx
);
1460 dmu_objset_sync_dnodes(ms
, sda
->sda_tx
);
1462 multilist_sublist_unlock(ms
);
1464 kmem_free(sda
, sizeof (*sda
));
1468 /* called from dsl */
1470 dmu_objset_sync(objset_t
*os
, zio_t
*pio
, dmu_tx_t
*tx
)
1473 zbookmark_phys_t zb
;
1477 dbuf_dirty_record_t
*dr
;
1478 blkptr_t
*blkptr_copy
= kmem_alloc(sizeof (*os
->os_rootbp
), KM_SLEEP
);
1479 *blkptr_copy
= *os
->os_rootbp
;
1481 dprintf_ds(os
->os_dsl_dataset
, "txg=%llu\n", tx
->tx_txg
);
1483 ASSERT(dmu_tx_is_syncing(tx
));
1484 /* XXX the write_done callback should really give us the tx... */
1487 if (os
->os_dsl_dataset
== NULL
) {
1489 * This is the MOS. If we have upgraded,
1490 * spa_max_replication() could change, so reset
1493 os
->os_copies
= spa_max_replication(os
->os_spa
);
1497 * Create the root block IO
1499 SET_BOOKMARK(&zb
, os
->os_dsl_dataset
?
1500 os
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
1501 ZB_ROOT_OBJECT
, ZB_ROOT_LEVEL
, ZB_ROOT_BLKID
);
1502 arc_release(os
->os_phys_buf
, &os
->os_phys_buf
);
1504 dmu_write_policy(os
, NULL
, 0, 0, &zp
);
1507 * If we are either claiming the ZIL or doing a raw receive write out
1508 * the os_phys_buf raw. Neither of these actions will effect the MAC
1511 if (arc_is_unauthenticated(os
->os_phys_buf
) || os
->os_next_write_raw
) {
1512 ASSERT(os
->os_encrypted
);
1513 os
->os_next_write_raw
= B_FALSE
;
1514 arc_convert_to_raw(os
->os_phys_buf
,
1515 os
->os_dsl_dataset
->ds_object
, ZFS_HOST_BYTEORDER
,
1516 DMU_OT_OBJSET
, NULL
, NULL
, NULL
);
1519 zio
= arc_write(pio
, os
->os_spa
, tx
->tx_txg
,
1520 blkptr_copy
, os
->os_phys_buf
, DMU_OS_IS_L2CACHEABLE(os
),
1521 &zp
, dmu_objset_write_ready
, NULL
, NULL
, dmu_objset_write_done
,
1522 os
, ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);
1525 * Sync special dnodes - the parent IO for the sync is the root block
1527 DMU_META_DNODE(os
)->dn_zio
= zio
;
1528 dnode_sync(DMU_META_DNODE(os
), tx
);
1530 os
->os_phys
->os_flags
= os
->os_flags
;
1532 if (DMU_USERUSED_DNODE(os
) &&
1533 DMU_USERUSED_DNODE(os
)->dn_type
!= DMU_OT_NONE
) {
1534 DMU_USERUSED_DNODE(os
)->dn_zio
= zio
;
1535 dnode_sync(DMU_USERUSED_DNODE(os
), tx
);
1536 DMU_GROUPUSED_DNODE(os
)->dn_zio
= zio
;
1537 dnode_sync(DMU_GROUPUSED_DNODE(os
), tx
);
1540 txgoff
= tx
->tx_txg
& TXG_MASK
;
1542 if (dmu_objset_userused_enabled(os
) &&
1543 (!os
->os_encrypted
|| !dmu_objset_is_receiving(os
))) {
1545 * We must create the list here because it uses the
1546 * dn_dirty_link[] of this txg. But it may already
1547 * exist because we call dsl_dataset_sync() twice per txg.
1549 if (os
->os_synced_dnodes
== NULL
) {
1550 os
->os_synced_dnodes
=
1551 multilist_create(sizeof (dnode_t
),
1552 offsetof(dnode_t
, dn_dirty_link
[txgoff
]),
1553 dnode_multilist_index_func
);
1555 ASSERT3U(os
->os_synced_dnodes
->ml_offset
, ==,
1556 offsetof(dnode_t
, dn_dirty_link
[txgoff
]));
1561 i
< multilist_get_num_sublists(os
->os_dirty_dnodes
[txgoff
]); i
++) {
1562 sync_dnodes_arg_t
*sda
= kmem_alloc(sizeof (*sda
), KM_SLEEP
);
1563 sda
->sda_list
= os
->os_dirty_dnodes
[txgoff
];
1564 sda
->sda_sublist_idx
= i
;
1566 (void) taskq_dispatch(dmu_objset_pool(os
)->dp_sync_taskq
,
1567 sync_dnodes_task
, sda
, 0);
1568 /* callback frees sda */
1570 taskq_wait(dmu_objset_pool(os
)->dp_sync_taskq
);
1572 list
= &DMU_META_DNODE(os
)->dn_dirty_records
[txgoff
];
1573 while ((dr
= list_head(list
)) != NULL
) {
1574 ASSERT0(dr
->dr_dbuf
->db_level
);
1575 list_remove(list
, dr
);
1577 zio_nowait(dr
->dr_zio
);
1580 /* Enable dnode backfill if enough objects have been freed. */
1581 if (os
->os_freed_dnodes
>= dmu_rescan_dnode_threshold
) {
1582 os
->os_rescan_dnodes
= B_TRUE
;
1583 os
->os_freed_dnodes
= 0;
1587 * Free intent log blocks up to this tx.
1589 zil_sync(os
->os_zil
, tx
);
1590 os
->os_phys
->os_zil_header
= os
->os_zil_header
;
1595 dmu_objset_is_dirty(objset_t
*os
, uint64_t txg
)
1597 return (!multilist_is_empty(os
->os_dirty_dnodes
[txg
& TXG_MASK
]));
1600 static objset_used_cb_t
*used_cbs
[DMU_OST_NUMTYPES
];
1603 dmu_objset_register_type(dmu_objset_type_t ost
, objset_used_cb_t
*cb
)
1609 dmu_objset_userused_enabled(objset_t
*os
)
1611 return (spa_version(os
->os_spa
) >= SPA_VERSION_USERSPACE
&&
1612 used_cbs
[os
->os_phys
->os_type
] != NULL
&&
1613 DMU_USERUSED_DNODE(os
) != NULL
);
1617 dmu_objset_userobjused_enabled(objset_t
*os
)
1619 return (dmu_objset_userused_enabled(os
) &&
1620 spa_feature_is_enabled(os
->os_spa
, SPA_FEATURE_USEROBJ_ACCOUNTING
));
1623 typedef struct userquota_node
{
1624 /* must be in the first filed, see userquota_update_cache() */
1625 char uqn_id
[20 + DMU_OBJACCT_PREFIX_LEN
];
1627 avl_node_t uqn_node
;
1630 typedef struct userquota_cache
{
1631 avl_tree_t uqc_user_deltas
;
1632 avl_tree_t uqc_group_deltas
;
1633 } userquota_cache_t
;
1636 userquota_compare(const void *l
, const void *r
)
1638 const userquota_node_t
*luqn
= l
;
1639 const userquota_node_t
*ruqn
= r
;
1643 * NB: can only access uqn_id because userquota_update_cache() doesn't
1644 * pass in an entire userquota_node_t.
1646 rv
= strcmp(luqn
->uqn_id
, ruqn
->uqn_id
);
1648 return (AVL_ISIGN(rv
));
1652 do_userquota_cacheflush(objset_t
*os
, userquota_cache_t
*cache
, dmu_tx_t
*tx
)
1655 userquota_node_t
*uqn
;
1657 ASSERT(dmu_tx_is_syncing(tx
));
1660 while ((uqn
= avl_destroy_nodes(&cache
->uqc_user_deltas
,
1661 &cookie
)) != NULL
) {
1663 * os_userused_lock protects against concurrent calls to
1664 * zap_increment_int(). It's needed because zap_increment_int()
1665 * is not thread-safe (i.e. not atomic).
1667 mutex_enter(&os
->os_userused_lock
);
1668 VERIFY0(zap_increment(os
, DMU_USERUSED_OBJECT
,
1669 uqn
->uqn_id
, uqn
->uqn_delta
, tx
));
1670 mutex_exit(&os
->os_userused_lock
);
1671 kmem_free(uqn
, sizeof (*uqn
));
1673 avl_destroy(&cache
->uqc_user_deltas
);
1676 while ((uqn
= avl_destroy_nodes(&cache
->uqc_group_deltas
,
1677 &cookie
)) != NULL
) {
1678 mutex_enter(&os
->os_userused_lock
);
1679 VERIFY0(zap_increment(os
, DMU_GROUPUSED_OBJECT
,
1680 uqn
->uqn_id
, uqn
->uqn_delta
, tx
));
1681 mutex_exit(&os
->os_userused_lock
);
1682 kmem_free(uqn
, sizeof (*uqn
));
1684 avl_destroy(&cache
->uqc_group_deltas
);
1688 userquota_update_cache(avl_tree_t
*avl
, const char *id
, int64_t delta
)
1690 userquota_node_t
*uqn
;
1693 ASSERT(strlen(id
) < sizeof (uqn
->uqn_id
));
1695 * Use id directly for searching because uqn_id is the first field of
1696 * userquota_node_t and fields after uqn_id won't be accessed in
1699 uqn
= avl_find(avl
, (const void *)id
, &idx
);
1701 uqn
= kmem_zalloc(sizeof (*uqn
), KM_SLEEP
);
1702 strlcpy(uqn
->uqn_id
, id
, sizeof (uqn
->uqn_id
));
1703 avl_insert(avl
, uqn
, idx
);
1705 uqn
->uqn_delta
+= delta
;
1709 do_userquota_update(userquota_cache_t
*cache
, uint64_t used
, uint64_t flags
,
1710 uint64_t user
, uint64_t group
, boolean_t subtract
)
1712 if ((flags
& DNODE_FLAG_USERUSED_ACCOUNTED
)) {
1713 int64_t delta
= DNODE_MIN_SIZE
+ used
;
1719 (void) sprintf(name
, "%llx", (longlong_t
)user
);
1720 userquota_update_cache(&cache
->uqc_user_deltas
, name
, delta
);
1722 (void) sprintf(name
, "%llx", (longlong_t
)group
);
1723 userquota_update_cache(&cache
->uqc_group_deltas
, name
, delta
);
1728 do_userobjquota_update(userquota_cache_t
*cache
, uint64_t flags
,
1729 uint64_t user
, uint64_t group
, boolean_t subtract
)
1731 if (flags
& DNODE_FLAG_USEROBJUSED_ACCOUNTED
) {
1732 char name
[20 + DMU_OBJACCT_PREFIX_LEN
];
1733 int delta
= subtract
? -1 : 1;
1735 (void) snprintf(name
, sizeof (name
), DMU_OBJACCT_PREFIX
"%llx",
1737 userquota_update_cache(&cache
->uqc_user_deltas
, name
, delta
);
1739 (void) snprintf(name
, sizeof (name
), DMU_OBJACCT_PREFIX
"%llx",
1741 userquota_update_cache(&cache
->uqc_group_deltas
, name
, delta
);
1745 typedef struct userquota_updates_arg
{
1747 int uua_sublist_idx
;
1749 } userquota_updates_arg_t
;
1752 userquota_updates_task(void *arg
)
1754 userquota_updates_arg_t
*uua
= arg
;
1755 objset_t
*os
= uua
->uua_os
;
1756 dmu_tx_t
*tx
= uua
->uua_tx
;
1758 userquota_cache_t cache
= { { 0 } };
1760 multilist_sublist_t
*list
=
1761 multilist_sublist_lock(os
->os_synced_dnodes
, uua
->uua_sublist_idx
);
1763 ASSERT(multilist_sublist_head(list
) == NULL
||
1764 dmu_objset_userused_enabled(os
));
1765 avl_create(&cache
.uqc_user_deltas
, userquota_compare
,
1766 sizeof (userquota_node_t
), offsetof(userquota_node_t
, uqn_node
));
1767 avl_create(&cache
.uqc_group_deltas
, userquota_compare
,
1768 sizeof (userquota_node_t
), offsetof(userquota_node_t
, uqn_node
));
1770 while ((dn
= multilist_sublist_head(list
)) != NULL
) {
1772 ASSERT(!DMU_OBJECT_IS_SPECIAL(dn
->dn_object
));
1773 ASSERT(dn
->dn_phys
->dn_type
== DMU_OT_NONE
||
1774 dn
->dn_phys
->dn_flags
&
1775 DNODE_FLAG_USERUSED_ACCOUNTED
);
1777 flags
= dn
->dn_id_flags
;
1779 if (flags
& DN_ID_OLD_EXIST
) {
1780 do_userquota_update(&cache
,
1781 dn
->dn_oldused
, dn
->dn_oldflags
,
1782 dn
->dn_olduid
, dn
->dn_oldgid
, B_TRUE
);
1783 do_userobjquota_update(&cache
, dn
->dn_oldflags
,
1784 dn
->dn_olduid
, dn
->dn_oldgid
, B_TRUE
);
1786 if (flags
& DN_ID_NEW_EXIST
) {
1787 do_userquota_update(&cache
,
1788 DN_USED_BYTES(dn
->dn_phys
), dn
->dn_phys
->dn_flags
,
1789 dn
->dn_newuid
, dn
->dn_newgid
, B_FALSE
);
1790 do_userobjquota_update(&cache
, dn
->dn_phys
->dn_flags
,
1791 dn
->dn_newuid
, dn
->dn_newgid
, B_FALSE
);
1794 mutex_enter(&dn
->dn_mtx
);
1796 dn
->dn_oldflags
= 0;
1797 if (dn
->dn_id_flags
& DN_ID_NEW_EXIST
) {
1798 dn
->dn_olduid
= dn
->dn_newuid
;
1799 dn
->dn_oldgid
= dn
->dn_newgid
;
1800 dn
->dn_id_flags
|= DN_ID_OLD_EXIST
;
1801 if (dn
->dn_bonuslen
== 0)
1802 dn
->dn_id_flags
|= DN_ID_CHKED_SPILL
;
1804 dn
->dn_id_flags
|= DN_ID_CHKED_BONUS
;
1806 dn
->dn_id_flags
&= ~(DN_ID_NEW_EXIST
);
1807 mutex_exit(&dn
->dn_mtx
);
1809 multilist_sublist_remove(list
, dn
);
1810 dnode_rele(dn
, os
->os_synced_dnodes
);
1812 do_userquota_cacheflush(os
, &cache
, tx
);
1813 multilist_sublist_unlock(list
);
1814 kmem_free(uua
, sizeof (*uua
));
1818 dmu_objset_do_userquota_updates(objset_t
*os
, dmu_tx_t
*tx
)
1820 if (!dmu_objset_userused_enabled(os
))
1823 /* if this is a raw receive just return and handle accounting later */
1824 if (os
->os_encrypted
&& dmu_objset_is_receiving(os
))
1827 /* Allocate the user/groupused objects if necessary. */
1828 if (DMU_USERUSED_DNODE(os
)->dn_type
== DMU_OT_NONE
) {
1829 VERIFY0(zap_create_claim(os
,
1830 DMU_USERUSED_OBJECT
,
1831 DMU_OT_USERGROUP_USED
, DMU_OT_NONE
, 0, tx
));
1832 VERIFY0(zap_create_claim(os
,
1833 DMU_GROUPUSED_OBJECT
,
1834 DMU_OT_USERGROUP_USED
, DMU_OT_NONE
, 0, tx
));
1838 i
< multilist_get_num_sublists(os
->os_synced_dnodes
); i
++) {
1839 userquota_updates_arg_t
*uua
=
1840 kmem_alloc(sizeof (*uua
), KM_SLEEP
);
1842 uua
->uua_sublist_idx
= i
;
1844 /* note: caller does taskq_wait() */
1845 (void) taskq_dispatch(dmu_objset_pool(os
)->dp_sync_taskq
,
1846 userquota_updates_task
, uua
, 0);
1847 /* callback frees uua */
1852 * Returns a pointer to data to find uid/gid from
1854 * If a dirty record for transaction group that is syncing can't
1855 * be found then NULL is returned. In the NULL case it is assumed
1856 * the uid/gid aren't changing.
1859 dmu_objset_userquota_find_data(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1861 dbuf_dirty_record_t
*dr
, **drp
;
1864 if (db
->db_dirtycnt
== 0)
1865 return (db
->db
.db_data
); /* Nothing is changing */
1867 for (drp
= &db
->db_last_dirty
; (dr
= *drp
) != NULL
; drp
= &dr
->dr_next
)
1868 if (dr
->dr_txg
== tx
->tx_txg
)
1876 DB_DNODE_ENTER(dr
->dr_dbuf
);
1877 dn
= DB_DNODE(dr
->dr_dbuf
);
1879 if (dn
->dn_bonuslen
== 0 &&
1880 dr
->dr_dbuf
->db_blkid
== DMU_SPILL_BLKID
)
1881 data
= dr
->dt
.dl
.dr_data
->b_data
;
1883 data
= dr
->dt
.dl
.dr_data
;
1885 DB_DNODE_EXIT(dr
->dr_dbuf
);
1892 dmu_objset_userquota_get_ids(dnode_t
*dn
, boolean_t before
, dmu_tx_t
*tx
)
1894 objset_t
*os
= dn
->dn_objset
;
1896 dmu_buf_impl_t
*db
= NULL
;
1897 uint64_t *user
= NULL
;
1898 uint64_t *group
= NULL
;
1899 int flags
= dn
->dn_id_flags
;
1901 boolean_t have_spill
= B_FALSE
;
1903 if (!dmu_objset_userused_enabled(dn
->dn_objset
))
1907 * Raw receives introduce a problem with user accounting. Raw
1908 * receives cannot update the user accounting info because the
1909 * user ids and the sizes are encrypted. To guarantee that we
1910 * never end up with bad user accounting, we simply disable it
1911 * during raw receives. We also disable this for normal receives
1912 * so that an incremental raw receive may be done on top of an
1913 * existing non-raw receive.
1915 if (os
->os_encrypted
&& dmu_objset_is_receiving(os
))
1918 if (before
&& (flags
& (DN_ID_CHKED_BONUS
|DN_ID_OLD_EXIST
|
1919 DN_ID_CHKED_SPILL
)))
1922 if (before
&& dn
->dn_bonuslen
!= 0)
1923 data
= DN_BONUS(dn
->dn_phys
);
1924 else if (!before
&& dn
->dn_bonuslen
!= 0) {
1927 mutex_enter(&db
->db_mtx
);
1928 data
= dmu_objset_userquota_find_data(db
, tx
);
1930 data
= DN_BONUS(dn
->dn_phys
);
1932 } else if (dn
->dn_bonuslen
== 0 && dn
->dn_bonustype
== DMU_OT_SA
) {
1935 if (RW_WRITE_HELD(&dn
->dn_struct_rwlock
))
1936 rf
|= DB_RF_HAVESTRUCT
;
1937 error
= dmu_spill_hold_by_dnode(dn
,
1938 rf
| DB_RF_MUST_SUCCEED
,
1939 FTAG
, (dmu_buf_t
**)&db
);
1941 mutex_enter(&db
->db_mtx
);
1942 data
= (before
) ? db
->db
.db_data
:
1943 dmu_objset_userquota_find_data(db
, tx
);
1944 have_spill
= B_TRUE
;
1946 mutex_enter(&dn
->dn_mtx
);
1947 dn
->dn_id_flags
|= DN_ID_CHKED_BONUS
;
1948 mutex_exit(&dn
->dn_mtx
);
1954 user
= &dn
->dn_olduid
;
1955 group
= &dn
->dn_oldgid
;
1957 user
= &dn
->dn_newuid
;
1958 group
= &dn
->dn_newgid
;
1962 * Must always call the callback in case the object
1963 * type has changed and that type isn't an object type to track
1965 error
= used_cbs
[os
->os_phys
->os_type
](dn
->dn_bonustype
, data
,
1969 * Preserve existing uid/gid when the callback can't determine
1970 * what the new uid/gid are and the callback returned EEXIST.
1971 * The EEXIST error tells us to just use the existing uid/gid.
1972 * If we don't know what the old values are then just assign
1973 * them to 0, since that is a new file being created.
1975 if (!before
&& data
== NULL
&& error
== EEXIST
) {
1976 if (flags
& DN_ID_OLD_EXIST
) {
1977 dn
->dn_newuid
= dn
->dn_olduid
;
1978 dn
->dn_newgid
= dn
->dn_oldgid
;
1987 mutex_exit(&db
->db_mtx
);
1989 mutex_enter(&dn
->dn_mtx
);
1990 if (error
== 0 && before
)
1991 dn
->dn_id_flags
|= DN_ID_OLD_EXIST
;
1992 if (error
== 0 && !before
)
1993 dn
->dn_id_flags
|= DN_ID_NEW_EXIST
;
1996 dn
->dn_id_flags
|= DN_ID_CHKED_SPILL
;
1998 dn
->dn_id_flags
|= DN_ID_CHKED_BONUS
;
2000 mutex_exit(&dn
->dn_mtx
);
2002 dmu_buf_rele((dmu_buf_t
*)db
, FTAG
);
2006 dmu_objset_userspace_present(objset_t
*os
)
2008 return (os
->os_phys
->os_flags
&
2009 OBJSET_FLAG_USERACCOUNTING_COMPLETE
);
2013 dmu_objset_userobjspace_present(objset_t
*os
)
2015 return (os
->os_phys
->os_flags
&
2016 OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE
);
2020 dmu_objset_space_upgrade(objset_t
*os
)
2026 * We simply need to mark every object dirty, so that it will be
2027 * synced out and now accounted. If this is called
2028 * concurrently, or if we already did some work before crashing,
2029 * that's fine, since we track each object's accounted state
2033 for (obj
= 0; err
== 0; err
= dmu_object_next(os
, &obj
, FALSE
, 0)) {
2038 mutex_enter(&os
->os_upgrade_lock
);
2039 if (os
->os_upgrade_exit
)
2040 err
= SET_ERROR(EINTR
);
2041 mutex_exit(&os
->os_upgrade_lock
);
2045 if (issig(JUSTLOOKING
) && issig(FORREAL
))
2046 return (SET_ERROR(EINTR
));
2048 objerr
= dmu_bonus_hold(os
, obj
, FTAG
, &db
);
2051 tx
= dmu_tx_create(os
);
2052 dmu_tx_hold_bonus(tx
, obj
);
2053 objerr
= dmu_tx_assign(tx
, TXG_WAIT
);
2055 dmu_buf_rele(db
, FTAG
);
2059 dmu_buf_will_dirty(db
, tx
);
2060 dmu_buf_rele(db
, FTAG
);
2067 dmu_objset_userspace_upgrade(objset_t
*os
)
2071 if (dmu_objset_userspace_present(os
))
2073 if (dmu_objset_is_snapshot(os
))
2074 return (SET_ERROR(EINVAL
));
2075 if (!dmu_objset_userused_enabled(os
))
2076 return (SET_ERROR(ENOTSUP
));
2078 err
= dmu_objset_space_upgrade(os
);
2082 os
->os_flags
|= OBJSET_FLAG_USERACCOUNTING_COMPLETE
;
2083 txg_wait_synced(dmu_objset_pool(os
), 0);
2088 dmu_objset_userobjspace_upgrade_cb(objset_t
*os
)
2092 if (dmu_objset_userobjspace_present(os
))
2094 if (dmu_objset_is_snapshot(os
))
2095 return (SET_ERROR(EINVAL
));
2096 if (!dmu_objset_userobjused_enabled(os
))
2097 return (SET_ERROR(ENOTSUP
));
2099 dmu_objset_ds(os
)->ds_feature_activation_needed
[
2100 SPA_FEATURE_USEROBJ_ACCOUNTING
] = B_TRUE
;
2102 err
= dmu_objset_space_upgrade(os
);
2106 os
->os_flags
|= OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE
;
2107 txg_wait_synced(dmu_objset_pool(os
), 0);
2112 dmu_objset_userobjspace_upgrade(objset_t
*os
)
2114 dmu_objset_upgrade(os
, dmu_objset_userobjspace_upgrade_cb
);
2118 dmu_objset_userobjspace_upgradable(objset_t
*os
)
2120 return (dmu_objset_type(os
) == DMU_OST_ZFS
&&
2121 !dmu_objset_is_snapshot(os
) &&
2122 dmu_objset_userobjused_enabled(os
) &&
2123 !dmu_objset_userobjspace_present(os
));
2127 dmu_objset_space(objset_t
*os
, uint64_t *refdbytesp
, uint64_t *availbytesp
,
2128 uint64_t *usedobjsp
, uint64_t *availobjsp
)
2130 dsl_dataset_space(os
->os_dsl_dataset
, refdbytesp
, availbytesp
,
2131 usedobjsp
, availobjsp
);
2135 dmu_objset_fsid_guid(objset_t
*os
)
2137 return (dsl_dataset_fsid_guid(os
->os_dsl_dataset
));
2141 dmu_objset_fast_stat(objset_t
*os
, dmu_objset_stats_t
*stat
)
2143 stat
->dds_type
= os
->os_phys
->os_type
;
2144 if (os
->os_dsl_dataset
)
2145 dsl_dataset_fast_stat(os
->os_dsl_dataset
, stat
);
2149 dmu_objset_stats(objset_t
*os
, nvlist_t
*nv
)
2151 ASSERT(os
->os_dsl_dataset
||
2152 os
->os_phys
->os_type
== DMU_OST_META
);
2154 if (os
->os_dsl_dataset
!= NULL
)
2155 dsl_dataset_stats(os
->os_dsl_dataset
, nv
);
2157 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_TYPE
,
2158 os
->os_phys
->os_type
);
2159 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_USERACCOUNTING
,
2160 dmu_objset_userspace_present(os
));
2164 dmu_objset_is_snapshot(objset_t
*os
)
2166 if (os
->os_dsl_dataset
!= NULL
)
2167 return (os
->os_dsl_dataset
->ds_is_snapshot
);
2173 dmu_snapshot_realname(objset_t
*os
, char *name
, char *real
, int maxlen
,
2174 boolean_t
*conflict
)
2176 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
2179 if (dsl_dataset_phys(ds
)->ds_snapnames_zapobj
== 0)
2180 return (SET_ERROR(ENOENT
));
2182 return (zap_lookup_norm(ds
->ds_dir
->dd_pool
->dp_meta_objset
,
2183 dsl_dataset_phys(ds
)->ds_snapnames_zapobj
, name
, 8, 1, &ignored
,
2184 MT_NORMALIZE
, real
, maxlen
, conflict
));
2188 dmu_snapshot_list_next(objset_t
*os
, int namelen
, char *name
,
2189 uint64_t *idp
, uint64_t *offp
, boolean_t
*case_conflict
)
2191 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
2192 zap_cursor_t cursor
;
2193 zap_attribute_t attr
;
2195 ASSERT(dsl_pool_config_held(dmu_objset_pool(os
)));
2197 if (dsl_dataset_phys(ds
)->ds_snapnames_zapobj
== 0)
2198 return (SET_ERROR(ENOENT
));
2200 zap_cursor_init_serialized(&cursor
,
2201 ds
->ds_dir
->dd_pool
->dp_meta_objset
,
2202 dsl_dataset_phys(ds
)->ds_snapnames_zapobj
, *offp
);
2204 if (zap_cursor_retrieve(&cursor
, &attr
) != 0) {
2205 zap_cursor_fini(&cursor
);
2206 return (SET_ERROR(ENOENT
));
2209 if (strlen(attr
.za_name
) + 1 > namelen
) {
2210 zap_cursor_fini(&cursor
);
2211 return (SET_ERROR(ENAMETOOLONG
));
2214 (void) strcpy(name
, attr
.za_name
);
2216 *idp
= attr
.za_first_integer
;
2218 *case_conflict
= attr
.za_normalization_conflict
;
2219 zap_cursor_advance(&cursor
);
2220 *offp
= zap_cursor_serialize(&cursor
);
2221 zap_cursor_fini(&cursor
);
2227 dmu_snapshot_lookup(objset_t
*os
, const char *name
, uint64_t *value
)
2229 return (dsl_dataset_snap_lookup(os
->os_dsl_dataset
, name
, value
));
2233 dmu_dir_list_next(objset_t
*os
, int namelen
, char *name
,
2234 uint64_t *idp
, uint64_t *offp
)
2236 dsl_dir_t
*dd
= os
->os_dsl_dataset
->ds_dir
;
2237 zap_cursor_t cursor
;
2238 zap_attribute_t attr
;
2240 /* there is no next dir on a snapshot! */
2241 if (os
->os_dsl_dataset
->ds_object
!=
2242 dsl_dir_phys(dd
)->dd_head_dataset_obj
)
2243 return (SET_ERROR(ENOENT
));
2245 zap_cursor_init_serialized(&cursor
,
2246 dd
->dd_pool
->dp_meta_objset
,
2247 dsl_dir_phys(dd
)->dd_child_dir_zapobj
, *offp
);
2249 if (zap_cursor_retrieve(&cursor
, &attr
) != 0) {
2250 zap_cursor_fini(&cursor
);
2251 return (SET_ERROR(ENOENT
));
2254 if (strlen(attr
.za_name
) + 1 > namelen
) {
2255 zap_cursor_fini(&cursor
);
2256 return (SET_ERROR(ENAMETOOLONG
));
2259 (void) strcpy(name
, attr
.za_name
);
2261 *idp
= attr
.za_first_integer
;
2262 zap_cursor_advance(&cursor
);
2263 *offp
= zap_cursor_serialize(&cursor
);
2264 zap_cursor_fini(&cursor
);
2269 typedef struct dmu_objset_find_ctx
{
2273 char *dc_ddname
; /* last component of ddobj's name */
2274 int (*dc_func
)(dsl_pool_t
*, dsl_dataset_t
*, void *);
2277 kmutex_t
*dc_error_lock
;
2279 } dmu_objset_find_ctx_t
;
2282 dmu_objset_find_dp_impl(dmu_objset_find_ctx_t
*dcp
)
2284 dsl_pool_t
*dp
= dcp
->dc_dp
;
2288 zap_attribute_t
*attr
;
2292 /* don't process if there already was an error */
2293 if (*dcp
->dc_error
!= 0)
2297 * Note: passing the name (dc_ddname) here is optional, but it
2298 * improves performance because we don't need to call
2299 * zap_value_search() to determine the name.
2301 err
= dsl_dir_hold_obj(dp
, dcp
->dc_ddobj
, dcp
->dc_ddname
, FTAG
, &dd
);
2305 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
2306 if (dd
->dd_myname
[0] == '$') {
2307 dsl_dir_rele(dd
, FTAG
);
2311 thisobj
= dsl_dir_phys(dd
)->dd_head_dataset_obj
;
2312 attr
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
2315 * Iterate over all children.
2317 if (dcp
->dc_flags
& DS_FIND_CHILDREN
) {
2318 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
,
2319 dsl_dir_phys(dd
)->dd_child_dir_zapobj
);
2320 zap_cursor_retrieve(&zc
, attr
) == 0;
2321 (void) zap_cursor_advance(&zc
)) {
2322 ASSERT3U(attr
->za_integer_length
, ==,
2324 ASSERT3U(attr
->za_num_integers
, ==, 1);
2326 dmu_objset_find_ctx_t
*child_dcp
=
2327 kmem_alloc(sizeof (*child_dcp
), KM_SLEEP
);
2329 child_dcp
->dc_ddobj
= attr
->za_first_integer
;
2330 child_dcp
->dc_ddname
= spa_strdup(attr
->za_name
);
2331 if (dcp
->dc_tq
!= NULL
)
2332 (void) taskq_dispatch(dcp
->dc_tq
,
2333 dmu_objset_find_dp_cb
, child_dcp
, TQ_SLEEP
);
2335 dmu_objset_find_dp_impl(child_dcp
);
2337 zap_cursor_fini(&zc
);
2341 * Iterate over all snapshots.
2343 if (dcp
->dc_flags
& DS_FIND_SNAPSHOTS
) {
2345 err
= dsl_dataset_hold_obj(dp
, thisobj
, FTAG
, &ds
);
2350 snapobj
= dsl_dataset_phys(ds
)->ds_snapnames_zapobj
;
2351 dsl_dataset_rele(ds
, FTAG
);
2353 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
, snapobj
);
2354 zap_cursor_retrieve(&zc
, attr
) == 0;
2355 (void) zap_cursor_advance(&zc
)) {
2356 ASSERT3U(attr
->za_integer_length
, ==,
2358 ASSERT3U(attr
->za_num_integers
, ==, 1);
2360 err
= dsl_dataset_hold_obj(dp
,
2361 attr
->za_first_integer
, FTAG
, &ds
);
2364 err
= dcp
->dc_func(dp
, ds
, dcp
->dc_arg
);
2365 dsl_dataset_rele(ds
, FTAG
);
2369 zap_cursor_fini(&zc
);
2373 kmem_free(attr
, sizeof (zap_attribute_t
));
2376 dsl_dir_rele(dd
, FTAG
);
2383 err
= dsl_dataset_hold_obj(dp
, thisobj
, FTAG
, &ds
);
2386 * Note: we hold the dir while calling dsl_dataset_hold_obj() so
2387 * that the dir will remain cached, and we won't have to re-instantiate
2388 * it (which could be expensive due to finding its name via
2389 * zap_value_search()).
2391 dsl_dir_rele(dd
, FTAG
);
2394 err
= dcp
->dc_func(dp
, ds
, dcp
->dc_arg
);
2395 dsl_dataset_rele(ds
, FTAG
);
2399 mutex_enter(dcp
->dc_error_lock
);
2400 /* only keep first error */
2401 if (*dcp
->dc_error
== 0)
2402 *dcp
->dc_error
= err
;
2403 mutex_exit(dcp
->dc_error_lock
);
2406 if (dcp
->dc_ddname
!= NULL
)
2407 spa_strfree(dcp
->dc_ddname
);
2408 kmem_free(dcp
, sizeof (*dcp
));
2412 dmu_objset_find_dp_cb(void *arg
)
2414 dmu_objset_find_ctx_t
*dcp
= arg
;
2415 dsl_pool_t
*dp
= dcp
->dc_dp
;
2418 * We need to get a pool_config_lock here, as there are several
2419 * asssert(pool_config_held) down the stack. Getting a lock via
2420 * dsl_pool_config_enter is risky, as it might be stalled by a
2421 * pending writer. This would deadlock, as the write lock can
2422 * only be granted when our parent thread gives up the lock.
2423 * The _prio interface gives us priority over a pending writer.
2425 dsl_pool_config_enter_prio(dp
, FTAG
);
2427 dmu_objset_find_dp_impl(dcp
);
2429 dsl_pool_config_exit(dp
, FTAG
);
2433 * Find objsets under and including ddobj, call func(ds) on each.
2434 * The order for the enumeration is completely undefined.
2435 * func is called with dsl_pool_config held.
2438 dmu_objset_find_dp(dsl_pool_t
*dp
, uint64_t ddobj
,
2439 int func(dsl_pool_t
*, dsl_dataset_t
*, void *), void *arg
, int flags
)
2444 dmu_objset_find_ctx_t
*dcp
;
2447 mutex_init(&err_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
2448 dcp
= kmem_alloc(sizeof (*dcp
), KM_SLEEP
);
2451 dcp
->dc_ddobj
= ddobj
;
2452 dcp
->dc_ddname
= NULL
;
2453 dcp
->dc_func
= func
;
2455 dcp
->dc_flags
= flags
;
2456 dcp
->dc_error_lock
= &err_lock
;
2457 dcp
->dc_error
= &error
;
2459 if ((flags
& DS_FIND_SERIALIZE
) || dsl_pool_config_held_writer(dp
)) {
2461 * In case a write lock is held we can't make use of
2462 * parallelism, as down the stack of the worker threads
2463 * the lock is asserted via dsl_pool_config_held.
2464 * In case of a read lock this is solved by getting a read
2465 * lock in each worker thread, which isn't possible in case
2466 * of a writer lock. So we fall back to the synchronous path
2468 * In the future it might be possible to get some magic into
2469 * dsl_pool_config_held in a way that it returns true for
2470 * the worker threads so that a single lock held from this
2471 * thread suffices. For now, stay single threaded.
2473 dmu_objset_find_dp_impl(dcp
);
2474 mutex_destroy(&err_lock
);
2479 ntasks
= dmu_find_threads
;
2481 ntasks
= vdev_count_leaves(dp
->dp_spa
) * 4;
2482 tq
= taskq_create("dmu_objset_find", ntasks
, maxclsyspri
, ntasks
,
2485 kmem_free(dcp
, sizeof (*dcp
));
2486 mutex_destroy(&err_lock
);
2488 return (SET_ERROR(ENOMEM
));
2492 /* dcp will be freed by task */
2493 (void) taskq_dispatch(tq
, dmu_objset_find_dp_cb
, dcp
, TQ_SLEEP
);
2496 * PORTING: this code relies on the property of taskq_wait to wait
2497 * until no more tasks are queued and no more tasks are active. As
2498 * we always queue new tasks from within other tasks, task_wait
2499 * reliably waits for the full recursion to finish, even though we
2500 * enqueue new tasks after taskq_wait has been called.
2501 * On platforms other than illumos, taskq_wait may not have this
2506 mutex_destroy(&err_lock
);
2512 * Find all objsets under name, and for each, call 'func(child_name, arg)'.
2513 * The dp_config_rwlock must not be held when this is called, and it
2514 * will not be held when the callback is called.
2515 * Therefore this function should only be used when the pool is not changing
2516 * (e.g. in syncing context), or the callback can deal with the possible races.
2519 dmu_objset_find_impl(spa_t
*spa
, const char *name
,
2520 int func(const char *, void *), void *arg
, int flags
)
2523 dsl_pool_t
*dp
= spa_get_dsl(spa
);
2526 zap_attribute_t
*attr
;
2531 dsl_pool_config_enter(dp
, FTAG
);
2533 err
= dsl_dir_hold(dp
, name
, FTAG
, &dd
, NULL
);
2535 dsl_pool_config_exit(dp
, FTAG
);
2539 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
2540 if (dd
->dd_myname
[0] == '$') {
2541 dsl_dir_rele(dd
, FTAG
);
2542 dsl_pool_config_exit(dp
, FTAG
);
2546 thisobj
= dsl_dir_phys(dd
)->dd_head_dataset_obj
;
2547 attr
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
2550 * Iterate over all children.
2552 if (flags
& DS_FIND_CHILDREN
) {
2553 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
,
2554 dsl_dir_phys(dd
)->dd_child_dir_zapobj
);
2555 zap_cursor_retrieve(&zc
, attr
) == 0;
2556 (void) zap_cursor_advance(&zc
)) {
2557 ASSERT3U(attr
->za_integer_length
, ==,
2559 ASSERT3U(attr
->za_num_integers
, ==, 1);
2561 child
= kmem_asprintf("%s/%s", name
, attr
->za_name
);
2562 dsl_pool_config_exit(dp
, FTAG
);
2563 err
= dmu_objset_find_impl(spa
, child
,
2565 dsl_pool_config_enter(dp
, FTAG
);
2570 zap_cursor_fini(&zc
);
2573 dsl_dir_rele(dd
, FTAG
);
2574 dsl_pool_config_exit(dp
, FTAG
);
2575 kmem_free(attr
, sizeof (zap_attribute_t
));
2581 * Iterate over all snapshots.
2583 if (flags
& DS_FIND_SNAPSHOTS
) {
2584 err
= dsl_dataset_hold_obj(dp
, thisobj
, FTAG
, &ds
);
2589 snapobj
= dsl_dataset_phys(ds
)->ds_snapnames_zapobj
;
2590 dsl_dataset_rele(ds
, FTAG
);
2592 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
, snapobj
);
2593 zap_cursor_retrieve(&zc
, attr
) == 0;
2594 (void) zap_cursor_advance(&zc
)) {
2595 ASSERT3U(attr
->za_integer_length
, ==,
2597 ASSERT3U(attr
->za_num_integers
, ==, 1);
2599 child
= kmem_asprintf("%s@%s",
2600 name
, attr
->za_name
);
2601 dsl_pool_config_exit(dp
, FTAG
);
2602 err
= func(child
, arg
);
2603 dsl_pool_config_enter(dp
, FTAG
);
2608 zap_cursor_fini(&zc
);
2612 dsl_dir_rele(dd
, FTAG
);
2613 kmem_free(attr
, sizeof (zap_attribute_t
));
2614 dsl_pool_config_exit(dp
, FTAG
);
2619 /* Apply to self. */
2620 return (func(name
, arg
));
2624 * See comment above dmu_objset_find_impl().
2627 dmu_objset_find(char *name
, int func(const char *, void *), void *arg
,
2633 error
= spa_open(name
, &spa
, FTAG
);
2636 error
= dmu_objset_find_impl(spa
, name
, func
, arg
, flags
);
2637 spa_close(spa
, FTAG
);
2642 dmu_objset_incompatible_encryption_version(objset_t
*os
)
2644 return (dsl_dir_incompatible_encryption_version(
2645 os
->os_dsl_dataset
->ds_dir
));
2649 dmu_objset_set_user(objset_t
*os
, void *user_ptr
)
2651 ASSERT(MUTEX_HELD(&os
->os_user_ptr_lock
));
2652 os
->os_user_ptr
= user_ptr
;
2656 dmu_objset_get_user(objset_t
*os
)
2658 ASSERT(MUTEX_HELD(&os
->os_user_ptr_lock
));
2659 return (os
->os_user_ptr
);
2663 * Determine name of filesystem, given name of snapshot.
2664 * buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes
2667 dmu_fsname(const char *snapname
, char *buf
)
2669 char *atp
= strchr(snapname
, '@');
2671 return (SET_ERROR(EINVAL
));
2672 if (atp
- snapname
>= ZFS_MAX_DATASET_NAME_LEN
)
2673 return (SET_ERROR(ENAMETOOLONG
));
2674 (void) strlcpy(buf
, snapname
, atp
- snapname
+ 1);
2679 * Call when we think we're going to write/free space in open context to track
2680 * the amount of dirty data in the open txg, which is also the amount
2681 * of memory that can not be evicted until this txg syncs.
2684 dmu_objset_willuse_space(objset_t
*os
, int64_t space
, dmu_tx_t
*tx
)
2686 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
2687 int64_t aspace
= spa_get_worst_case_asize(os
->os_spa
, space
);
2690 dsl_dir_willuse_space(ds
->ds_dir
, aspace
, tx
);
2691 dsl_pool_dirty_space(dmu_tx_pool(tx
), space
, tx
);
2695 #if defined(_KERNEL) && defined(HAVE_SPL)
2696 EXPORT_SYMBOL(dmu_objset_zil
);
2697 EXPORT_SYMBOL(dmu_objset_pool
);
2698 EXPORT_SYMBOL(dmu_objset_ds
);
2699 EXPORT_SYMBOL(dmu_objset_type
);
2700 EXPORT_SYMBOL(dmu_objset_name
);
2701 EXPORT_SYMBOL(dmu_objset_hold
);
2702 EXPORT_SYMBOL(dmu_objset_hold_flags
);
2703 EXPORT_SYMBOL(dmu_objset_own
);
2704 EXPORT_SYMBOL(dmu_objset_rele
);
2705 EXPORT_SYMBOL(dmu_objset_rele_flags
);
2706 EXPORT_SYMBOL(dmu_objset_disown
);
2707 EXPORT_SYMBOL(dmu_objset_from_ds
);
2708 EXPORT_SYMBOL(dmu_objset_create
);
2709 EXPORT_SYMBOL(dmu_objset_clone
);
2710 EXPORT_SYMBOL(dmu_objset_stats
);
2711 EXPORT_SYMBOL(dmu_objset_fast_stat
);
2712 EXPORT_SYMBOL(dmu_objset_spa
);
2713 EXPORT_SYMBOL(dmu_objset_space
);
2714 EXPORT_SYMBOL(dmu_objset_fsid_guid
);
2715 EXPORT_SYMBOL(dmu_objset_find
);
2716 EXPORT_SYMBOL(dmu_objset_byteswap
);
2717 EXPORT_SYMBOL(dmu_objset_evict_dbufs
);
2718 EXPORT_SYMBOL(dmu_objset_snap_cmtime
);
2719 EXPORT_SYMBOL(dmu_objset_dnodesize
);
2721 EXPORT_SYMBOL(dmu_objset_sync
);
2722 EXPORT_SYMBOL(dmu_objset_is_dirty
);
2723 EXPORT_SYMBOL(dmu_objset_create_impl_dnstats
);
2724 EXPORT_SYMBOL(dmu_objset_create_impl
);
2725 EXPORT_SYMBOL(dmu_objset_open_impl
);
2726 EXPORT_SYMBOL(dmu_objset_evict
);
2727 EXPORT_SYMBOL(dmu_objset_register_type
);
2728 EXPORT_SYMBOL(dmu_objset_do_userquota_updates
);
2729 EXPORT_SYMBOL(dmu_objset_userquota_get_ids
);
2730 EXPORT_SYMBOL(dmu_objset_userused_enabled
);
2731 EXPORT_SYMBOL(dmu_objset_userspace_upgrade
);
2732 EXPORT_SYMBOL(dmu_objset_userspace_present
);
2733 EXPORT_SYMBOL(dmu_objset_userobjused_enabled
);
2734 EXPORT_SYMBOL(dmu_objset_userobjspace_upgrade
);
2735 EXPORT_SYMBOL(dmu_objset_userobjspace_upgradable
);
2736 EXPORT_SYMBOL(dmu_objset_userobjspace_present
);