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>
65 * Needed to close a window in dnode_move() that allows the objset to be freed
66 * before it can be safely accessed.
71 * Tunable to overwrite the maximum number of threads for the parallelization
72 * of dmu_objset_find_dp, needed to speed up the import of pools with many
74 * Default is 4 times the number of leaf vdevs.
76 int dmu_find_threads
= 0;
79 * Backfill lower metadnode objects after this many have been freed.
80 * Backfilling negatively impacts object creation rates, so only do it
81 * if there are enough holes to fill.
83 int dmu_rescan_dnode_threshold
= 1 << DN_MAX_INDBLKSHIFT
;
85 static char *upgrade_tag
= "upgrade_tag";
87 static void dmu_objset_find_dp_cb(void *arg
);
89 static void dmu_objset_upgrade(objset_t
*os
, dmu_objset_upgrade_cb_t cb
);
90 static void dmu_objset_upgrade_stop(objset_t
*os
);
95 rw_init(&os_lock
, NULL
, RW_DEFAULT
, NULL
);
101 rw_destroy(&os_lock
);
105 dmu_objset_spa(objset_t
*os
)
111 dmu_objset_zil(objset_t
*os
)
117 dmu_objset_pool(objset_t
*os
)
121 if ((ds
= os
->os_dsl_dataset
) != NULL
&& ds
->ds_dir
)
122 return (ds
->ds_dir
->dd_pool
);
124 return (spa_get_dsl(os
->os_spa
));
128 dmu_objset_ds(objset_t
*os
)
130 return (os
->os_dsl_dataset
);
134 dmu_objset_type(objset_t
*os
)
136 return (os
->os_phys
->os_type
);
140 dmu_objset_name(objset_t
*os
, char *buf
)
142 dsl_dataset_name(os
->os_dsl_dataset
, buf
);
146 dmu_objset_id(objset_t
*os
)
148 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
150 return (ds
? ds
->ds_object
: 0);
154 dmu_objset_dnodesize(objset_t
*os
)
156 return (os
->os_dnodesize
);
160 dmu_objset_syncprop(objset_t
*os
)
162 return (os
->os_sync
);
166 dmu_objset_logbias(objset_t
*os
)
168 return (os
->os_logbias
);
172 checksum_changed_cb(void *arg
, uint64_t newval
)
177 * Inheritance should have been done by now.
179 ASSERT(newval
!= ZIO_CHECKSUM_INHERIT
);
181 os
->os_checksum
= zio_checksum_select(newval
, ZIO_CHECKSUM_ON_VALUE
);
185 compression_changed_cb(void *arg
, uint64_t newval
)
190 * Inheritance and range checking should have been done by now.
192 ASSERT(newval
!= ZIO_COMPRESS_INHERIT
);
194 os
->os_compress
= zio_compress_select(os
->os_spa
, newval
,
199 copies_changed_cb(void *arg
, uint64_t newval
)
204 * Inheritance and range checking should have been done by now.
207 ASSERT(newval
<= spa_max_replication(os
->os_spa
));
209 os
->os_copies
= newval
;
213 dedup_changed_cb(void *arg
, uint64_t newval
)
216 spa_t
*spa
= os
->os_spa
;
217 enum zio_checksum checksum
;
220 * Inheritance should have been done by now.
222 ASSERT(newval
!= ZIO_CHECKSUM_INHERIT
);
224 checksum
= zio_checksum_dedup_select(spa
, newval
, ZIO_CHECKSUM_OFF
);
226 os
->os_dedup_checksum
= checksum
& ZIO_CHECKSUM_MASK
;
227 os
->os_dedup_verify
= !!(checksum
& ZIO_CHECKSUM_VERIFY
);
231 primary_cache_changed_cb(void *arg
, uint64_t newval
)
236 * Inheritance and range checking should have been done by now.
238 ASSERT(newval
== ZFS_CACHE_ALL
|| newval
== ZFS_CACHE_NONE
||
239 newval
== ZFS_CACHE_METADATA
);
241 os
->os_primary_cache
= newval
;
245 secondary_cache_changed_cb(void *arg
, uint64_t newval
)
250 * Inheritance and range checking should have been done by now.
252 ASSERT(newval
== ZFS_CACHE_ALL
|| newval
== ZFS_CACHE_NONE
||
253 newval
== ZFS_CACHE_METADATA
);
255 os
->os_secondary_cache
= newval
;
259 sync_changed_cb(void *arg
, uint64_t newval
)
264 * Inheritance and range checking should have been done by now.
266 ASSERT(newval
== ZFS_SYNC_STANDARD
|| newval
== ZFS_SYNC_ALWAYS
||
267 newval
== ZFS_SYNC_DISABLED
);
269 os
->os_sync
= newval
;
271 zil_set_sync(os
->os_zil
, newval
);
275 redundant_metadata_changed_cb(void *arg
, uint64_t newval
)
280 * Inheritance and range checking should have been done by now.
282 ASSERT(newval
== ZFS_REDUNDANT_METADATA_ALL
||
283 newval
== ZFS_REDUNDANT_METADATA_MOST
);
285 os
->os_redundant_metadata
= newval
;
289 dnodesize_changed_cb(void *arg
, uint64_t newval
)
294 case ZFS_DNSIZE_LEGACY
:
295 os
->os_dnodesize
= DNODE_MIN_SIZE
;
297 case ZFS_DNSIZE_AUTO
:
299 * Choose a dnode size that will work well for most
300 * workloads if the user specified "auto". Future code
301 * improvements could dynamically select a dnode size
302 * based on observed workload patterns.
304 os
->os_dnodesize
= DNODE_MIN_SIZE
* 2;
311 os
->os_dnodesize
= newval
;
317 logbias_changed_cb(void *arg
, uint64_t newval
)
321 ASSERT(newval
== ZFS_LOGBIAS_LATENCY
||
322 newval
== ZFS_LOGBIAS_THROUGHPUT
);
323 os
->os_logbias
= newval
;
325 zil_set_logbias(os
->os_zil
, newval
);
329 recordsize_changed_cb(void *arg
, uint64_t newval
)
333 os
->os_recordsize
= newval
;
337 dmu_objset_byteswap(void *buf
, size_t size
)
339 objset_phys_t
*osp
= buf
;
341 ASSERT(size
== OBJSET_PHYS_SIZE_V1
|| size
== OBJSET_PHYS_SIZE_V2
||
342 size
== sizeof (objset_phys_t
));
343 dnode_byteswap(&osp
->os_meta_dnode
);
344 byteswap_uint64_array(&osp
->os_zil_header
, sizeof (zil_header_t
));
345 osp
->os_type
= BSWAP_64(osp
->os_type
);
346 osp
->os_flags
= BSWAP_64(osp
->os_flags
);
347 if (size
>= OBJSET_PHYS_SIZE_V2
) {
348 dnode_byteswap(&osp
->os_userused_dnode
);
349 dnode_byteswap(&osp
->os_groupused_dnode
);
350 if (size
>= sizeof (objset_phys_t
))
351 dnode_byteswap(&osp
->os_projectused_dnode
);
356 * The hash is a CRC-based hash of the objset_t pointer and the object number.
359 dnode_hash(const objset_t
*os
, uint64_t obj
)
361 uintptr_t osv
= (uintptr_t)os
;
362 uint64_t crc
= -1ULL;
364 ASSERT(zfs_crc64_table
[128] == ZFS_CRC64_POLY
);
366 * The low 6 bits of the pointer don't have much entropy, because
367 * the objset_t is larger than 2^6 bytes long.
369 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (osv
>> 6)) & 0xFF];
370 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 0)) & 0xFF];
371 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 8)) & 0xFF];
372 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 16)) & 0xFF];
374 crc
^= (osv
>>14) ^ (obj
>>24);
380 dnode_multilist_index_func(multilist_t
*ml
, void *obj
)
383 return (dnode_hash(dn
->dn_objset
, dn
->dn_object
) %
384 multilist_get_num_sublists(ml
));
388 * Instantiates the objset_t in-memory structure corresponding to the
389 * objset_phys_t that's pointed to by the specified blkptr_t.
392 dmu_objset_open_impl(spa_t
*spa
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
398 ASSERT(ds
== NULL
|| MUTEX_HELD(&ds
->ds_opening_lock
));
401 * The $ORIGIN dataset (if it exists) doesn't have an associated
402 * objset, so there's no reason to open it. The $ORIGIN dataset
403 * will not exist on pools older than SPA_VERSION_ORIGIN.
405 if (ds
!= NULL
&& spa_get_dsl(spa
) != NULL
&&
406 spa_get_dsl(spa
)->dp_origin_snap
!= NULL
) {
407 ASSERT3P(ds
->ds_dir
, !=,
408 spa_get_dsl(spa
)->dp_origin_snap
->ds_dir
);
411 os
= kmem_zalloc(sizeof (objset_t
), KM_SLEEP
);
412 os
->os_dsl_dataset
= ds
;
415 if (!BP_IS_HOLE(os
->os_rootbp
)) {
416 arc_flags_t aflags
= ARC_FLAG_WAIT
;
419 enum zio_flag zio_flags
= ZIO_FLAG_CANFAIL
;
420 SET_BOOKMARK(&zb
, ds
? ds
->ds_object
: DMU_META_OBJSET
,
421 ZB_ROOT_OBJECT
, ZB_ROOT_LEVEL
, ZB_ROOT_BLKID
);
423 if (DMU_OS_IS_L2CACHEABLE(os
))
424 aflags
|= ARC_FLAG_L2CACHE
;
426 if (ds
!= NULL
&& ds
->ds_dir
->dd_crypto_obj
!= 0) {
427 ASSERT3U(BP_GET_COMPRESS(bp
), ==, ZIO_COMPRESS_OFF
);
428 ASSERT(BP_IS_AUTHENTICATED(bp
));
429 zio_flags
|= ZIO_FLAG_RAW
;
432 dprintf_bp(os
->os_rootbp
, "reading %s", "");
433 err
= arc_read(NULL
, spa
, os
->os_rootbp
,
434 arc_getbuf_func
, &os
->os_phys_buf
,
435 ZIO_PRIORITY_SYNC_READ
, zio_flags
, &aflags
, &zb
);
437 kmem_free(os
, sizeof (objset_t
));
438 /* convert checksum errors into IO errors */
440 err
= SET_ERROR(EIO
);
444 if (spa_version(spa
) < SPA_VERSION_USERSPACE
)
445 size
= OBJSET_PHYS_SIZE_V1
;
446 else if (!spa_feature_is_enabled(spa
,
447 SPA_FEATURE_PROJECT_QUOTA
))
448 size
= OBJSET_PHYS_SIZE_V2
;
450 size
= sizeof (objset_phys_t
);
452 /* Increase the blocksize if we are permitted. */
453 if (arc_buf_size(os
->os_phys_buf
) < size
) {
454 arc_buf_t
*buf
= arc_alloc_buf(spa
, &os
->os_phys_buf
,
455 ARC_BUFC_METADATA
, size
);
456 bzero(buf
->b_data
, size
);
457 bcopy(os
->os_phys_buf
->b_data
, buf
->b_data
,
458 arc_buf_size(os
->os_phys_buf
));
459 arc_buf_destroy(os
->os_phys_buf
, &os
->os_phys_buf
);
460 os
->os_phys_buf
= buf
;
463 os
->os_phys
= os
->os_phys_buf
->b_data
;
464 os
->os_flags
= os
->os_phys
->os_flags
;
466 int size
= spa_version(spa
) >= SPA_VERSION_USERSPACE
?
467 sizeof (objset_phys_t
) : OBJSET_PHYS_SIZE_V1
;
468 os
->os_phys_buf
= arc_alloc_buf(spa
, &os
->os_phys_buf
,
469 ARC_BUFC_METADATA
, size
);
470 os
->os_phys
= os
->os_phys_buf
->b_data
;
471 bzero(os
->os_phys
, size
);
475 * Note: the changed_cb will be called once before the register
476 * func returns, thus changing the checksum/compression from the
477 * default (fletcher2/off). Snapshots don't need to know about
478 * checksum/compression/copies.
481 boolean_t needlock
= B_FALSE
;
483 os
->os_encrypted
= (ds
->ds_dir
->dd_crypto_obj
!= 0);
486 * Note: it's valid to open the objset if the dataset is
487 * long-held, in which case the pool_config lock will not
490 if (!dsl_pool_config_held(dmu_objset_pool(os
))) {
492 dsl_pool_config_enter(dmu_objset_pool(os
), FTAG
);
495 err
= dsl_prop_register(ds
,
496 zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE
),
497 primary_cache_changed_cb
, os
);
499 err
= dsl_prop_register(ds
,
500 zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE
),
501 secondary_cache_changed_cb
, os
);
503 if (!ds
->ds_is_snapshot
) {
505 err
= dsl_prop_register(ds
,
506 zfs_prop_to_name(ZFS_PROP_CHECKSUM
),
507 checksum_changed_cb
, os
);
510 err
= dsl_prop_register(ds
,
511 zfs_prop_to_name(ZFS_PROP_COMPRESSION
),
512 compression_changed_cb
, os
);
515 err
= dsl_prop_register(ds
,
516 zfs_prop_to_name(ZFS_PROP_COPIES
),
517 copies_changed_cb
, os
);
520 err
= dsl_prop_register(ds
,
521 zfs_prop_to_name(ZFS_PROP_DEDUP
),
522 dedup_changed_cb
, os
);
525 err
= dsl_prop_register(ds
,
526 zfs_prop_to_name(ZFS_PROP_LOGBIAS
),
527 logbias_changed_cb
, os
);
530 err
= dsl_prop_register(ds
,
531 zfs_prop_to_name(ZFS_PROP_SYNC
),
532 sync_changed_cb
, os
);
535 err
= dsl_prop_register(ds
,
537 ZFS_PROP_REDUNDANT_METADATA
),
538 redundant_metadata_changed_cb
, os
);
541 err
= dsl_prop_register(ds
,
542 zfs_prop_to_name(ZFS_PROP_RECORDSIZE
),
543 recordsize_changed_cb
, os
);
546 err
= dsl_prop_register(ds
,
547 zfs_prop_to_name(ZFS_PROP_DNODESIZE
),
548 dnodesize_changed_cb
, os
);
552 dsl_pool_config_exit(dmu_objset_pool(os
), FTAG
);
554 arc_buf_destroy(os
->os_phys_buf
, &os
->os_phys_buf
);
555 kmem_free(os
, sizeof (objset_t
));
559 /* It's the meta-objset. */
560 os
->os_checksum
= ZIO_CHECKSUM_FLETCHER_4
;
561 os
->os_compress
= ZIO_COMPRESS_ON
;
562 os
->os_encrypted
= B_FALSE
;
563 os
->os_copies
= spa_max_replication(spa
);
564 os
->os_dedup_checksum
= ZIO_CHECKSUM_OFF
;
565 os
->os_dedup_verify
= B_FALSE
;
566 os
->os_logbias
= ZFS_LOGBIAS_LATENCY
;
567 os
->os_sync
= ZFS_SYNC_STANDARD
;
568 os
->os_primary_cache
= ZFS_CACHE_ALL
;
569 os
->os_secondary_cache
= ZFS_CACHE_ALL
;
570 os
->os_dnodesize
= DNODE_MIN_SIZE
;
573 if (ds
== NULL
|| !ds
->ds_is_snapshot
)
574 os
->os_zil_header
= os
->os_phys
->os_zil_header
;
575 os
->os_zil
= zil_alloc(os
, &os
->os_zil_header
);
577 for (i
= 0; i
< TXG_SIZE
; i
++) {
578 os
->os_dirty_dnodes
[i
] = multilist_create(sizeof (dnode_t
),
579 offsetof(dnode_t
, dn_dirty_link
[i
]),
580 dnode_multilist_index_func
);
582 list_create(&os
->os_dnodes
, sizeof (dnode_t
),
583 offsetof(dnode_t
, dn_link
));
584 list_create(&os
->os_downgraded_dbufs
, sizeof (dmu_buf_impl_t
),
585 offsetof(dmu_buf_impl_t
, db_link
));
587 list_link_init(&os
->os_evicting_node
);
589 mutex_init(&os
->os_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
590 mutex_init(&os
->os_userused_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
591 mutex_init(&os
->os_obj_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
592 mutex_init(&os
->os_user_ptr_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
593 os
->os_obj_next_percpu_len
= boot_ncpus
;
594 os
->os_obj_next_percpu
= kmem_zalloc(os
->os_obj_next_percpu_len
*
595 sizeof (os
->os_obj_next_percpu
[0]), KM_SLEEP
);
597 dnode_special_open(os
, &os
->os_phys
->os_meta_dnode
,
598 DMU_META_DNODE_OBJECT
, &os
->os_meta_dnode
);
599 if (OBJSET_BUF_HAS_USERUSED(os
->os_phys_buf
)) {
600 dnode_special_open(os
, &os
->os_phys
->os_userused_dnode
,
601 DMU_USERUSED_OBJECT
, &os
->os_userused_dnode
);
602 dnode_special_open(os
, &os
->os_phys
->os_groupused_dnode
,
603 DMU_GROUPUSED_OBJECT
, &os
->os_groupused_dnode
);
604 if (OBJSET_BUF_HAS_PROJECTUSED(os
->os_phys_buf
))
605 dnode_special_open(os
,
606 &os
->os_phys
->os_projectused_dnode
,
607 DMU_PROJECTUSED_OBJECT
, &os
->os_projectused_dnode
);
610 mutex_init(&os
->os_upgrade_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
617 dmu_objset_from_ds(dsl_dataset_t
*ds
, objset_t
**osp
)
622 * We shouldn't be doing anything with dsl_dataset_t's unless the
623 * pool_config lock is held, or the dataset is long-held.
625 ASSERT(dsl_pool_config_held(ds
->ds_dir
->dd_pool
) ||
626 dsl_dataset_long_held(ds
));
628 mutex_enter(&ds
->ds_opening_lock
);
629 if (ds
->ds_objset
== NULL
) {
631 rrw_enter(&ds
->ds_bp_rwlock
, RW_READER
, FTAG
);
632 err
= dmu_objset_open_impl(dsl_dataset_get_spa(ds
),
633 ds
, dsl_dataset_get_blkptr(ds
), &os
);
634 rrw_exit(&ds
->ds_bp_rwlock
, FTAG
);
637 mutex_enter(&ds
->ds_lock
);
638 ASSERT(ds
->ds_objset
== NULL
);
640 mutex_exit(&ds
->ds_lock
);
643 *osp
= ds
->ds_objset
;
644 mutex_exit(&ds
->ds_opening_lock
);
649 * Holds the pool while the objset is held. Therefore only one objset
650 * can be held at a time.
653 dmu_objset_hold_flags(const char *name
, boolean_t decrypt
, void *tag
,
659 ds_hold_flags_t flags
= (decrypt
) ? DS_HOLD_FLAG_DECRYPT
: 0;
661 err
= dsl_pool_hold(name
, tag
, &dp
);
664 err
= dsl_dataset_hold_flags(dp
, name
, flags
, tag
, &ds
);
666 dsl_pool_rele(dp
, tag
);
670 err
= dmu_objset_from_ds(ds
, osp
);
672 dsl_dataset_rele(ds
, tag
);
673 dsl_pool_rele(dp
, tag
);
680 dmu_objset_hold(const char *name
, void *tag
, objset_t
**osp
)
682 return (dmu_objset_hold_flags(name
, B_FALSE
, tag
, osp
));
686 dmu_objset_own_impl(dsl_dataset_t
*ds
, dmu_objset_type_t type
,
687 boolean_t readonly
, boolean_t decrypt
, void *tag
, objset_t
**osp
)
691 err
= dmu_objset_from_ds(ds
, osp
);
694 } else if (type
!= DMU_OST_ANY
&& type
!= (*osp
)->os_phys
->os_type
) {
695 return (SET_ERROR(EINVAL
));
696 } else if (!readonly
&& dsl_dataset_is_snapshot(ds
)) {
697 return (SET_ERROR(EROFS
));
698 } else if (!readonly
&& decrypt
&&
699 dsl_dir_incompatible_encryption_version(ds
->ds_dir
)) {
700 return (SET_ERROR(EROFS
));
703 /* if we are decrypting, we can now check MACs in os->os_phys_buf */
704 if (decrypt
&& arc_is_unauthenticated((*osp
)->os_phys_buf
)) {
707 SET_BOOKMARK(&zb
, ds
->ds_object
, ZB_ROOT_OBJECT
,
708 ZB_ROOT_LEVEL
, ZB_ROOT_BLKID
);
709 err
= arc_untransform((*osp
)->os_phys_buf
, (*osp
)->os_spa
,
714 ASSERT0(arc_is_unauthenticated((*osp
)->os_phys_buf
));
721 * dsl_pool must not be held when this is called.
722 * Upon successful return, there will be a longhold on the dataset,
723 * and the dsl_pool will not be held.
726 dmu_objset_own(const char *name
, dmu_objset_type_t type
,
727 boolean_t readonly
, boolean_t decrypt
, void *tag
, objset_t
**osp
)
732 ds_hold_flags_t flags
= (decrypt
) ? DS_HOLD_FLAG_DECRYPT
: 0;
734 err
= dsl_pool_hold(name
, FTAG
, &dp
);
737 err
= dsl_dataset_own(dp
, name
, flags
, tag
, &ds
);
739 dsl_pool_rele(dp
, FTAG
);
742 err
= dmu_objset_own_impl(ds
, type
, readonly
, decrypt
, tag
, osp
);
744 dsl_dataset_disown(ds
, flags
, tag
);
745 dsl_pool_rele(dp
, FTAG
);
750 * User accounting requires the dataset to be decrypted and rw.
751 * We also don't begin user accounting during claiming to help
752 * speed up pool import times and to keep this txg reserved
753 * completely for recovery work.
755 if ((dmu_objset_userobjspace_upgradable(*osp
) ||
756 dmu_objset_projectquota_upgradable(*osp
)) &&
757 !readonly
&& !dp
->dp_spa
->spa_claiming
&&
758 (ds
->ds_dir
->dd_crypto_obj
== 0 || decrypt
))
759 dmu_objset_id_quota_upgrade(*osp
);
761 dsl_pool_rele(dp
, FTAG
);
766 dmu_objset_own_obj(dsl_pool_t
*dp
, uint64_t obj
, dmu_objset_type_t type
,
767 boolean_t readonly
, boolean_t decrypt
, void *tag
, objset_t
**osp
)
771 ds_hold_flags_t flags
= (decrypt
) ? DS_HOLD_FLAG_DECRYPT
: 0;
773 err
= dsl_dataset_own_obj(dp
, obj
, flags
, tag
, &ds
);
777 err
= dmu_objset_own_impl(ds
, type
, readonly
, decrypt
, tag
, osp
);
779 dsl_dataset_disown(ds
, flags
, tag
);
787 dmu_objset_rele_flags(objset_t
*os
, boolean_t decrypt
, void *tag
)
789 ds_hold_flags_t flags
= (decrypt
) ? DS_HOLD_FLAG_DECRYPT
: 0;
791 dsl_pool_t
*dp
= dmu_objset_pool(os
);
792 dsl_dataset_rele_flags(os
->os_dsl_dataset
, flags
, tag
);
793 dsl_pool_rele(dp
, tag
);
797 dmu_objset_rele(objset_t
*os
, void *tag
)
799 dmu_objset_rele_flags(os
, B_FALSE
, tag
);
803 * When we are called, os MUST refer to an objset associated with a dataset
804 * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner
805 * == tag. We will then release and reacquire ownership of the dataset while
806 * holding the pool config_rwlock to avoid intervening namespace or ownership
809 * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to
810 * release the hold on its dataset and acquire a new one on the dataset of the
811 * same name so that it can be partially torn down and reconstructed.
814 dmu_objset_refresh_ownership(dsl_dataset_t
*ds
, dsl_dataset_t
**newds
,
815 boolean_t decrypt
, void *tag
)
818 char name
[ZFS_MAX_DATASET_NAME_LEN
];
820 VERIFY3P(ds
, !=, NULL
);
821 VERIFY3P(ds
->ds_owner
, ==, tag
);
822 VERIFY(dsl_dataset_long_held(ds
));
824 dsl_dataset_name(ds
, name
);
825 dp
= ds
->ds_dir
->dd_pool
;
826 dsl_pool_config_enter(dp
, FTAG
);
827 dsl_dataset_disown(ds
, decrypt
, tag
);
828 VERIFY0(dsl_dataset_own(dp
, name
,
829 (decrypt
) ? DS_HOLD_FLAG_DECRYPT
: 0, tag
, newds
));
830 dsl_pool_config_exit(dp
, FTAG
);
834 dmu_objset_disown(objset_t
*os
, boolean_t decrypt
, void *tag
)
837 * Stop upgrading thread
839 dmu_objset_upgrade_stop(os
);
840 dsl_dataset_disown(os
->os_dsl_dataset
,
841 (decrypt
) ? DS_HOLD_FLAG_DECRYPT
: 0, tag
);
845 dmu_objset_evict_dbufs(objset_t
*os
)
850 dn_marker
= kmem_alloc(sizeof (dnode_t
), KM_SLEEP
);
852 mutex_enter(&os
->os_lock
);
853 dn
= list_head(&os
->os_dnodes
);
856 * Skip dnodes without holds. We have to do this dance
857 * because dnode_add_ref() only works if there is already a
858 * hold. If the dnode has no holds, then it has no dbufs.
860 if (dnode_add_ref(dn
, FTAG
)) {
861 list_insert_after(&os
->os_dnodes
, dn
, dn_marker
);
862 mutex_exit(&os
->os_lock
);
864 dnode_evict_dbufs(dn
);
865 dnode_rele(dn
, FTAG
);
867 mutex_enter(&os
->os_lock
);
868 dn
= list_next(&os
->os_dnodes
, dn_marker
);
869 list_remove(&os
->os_dnodes
, dn_marker
);
871 dn
= list_next(&os
->os_dnodes
, dn
);
874 mutex_exit(&os
->os_lock
);
876 kmem_free(dn_marker
, sizeof (dnode_t
));
878 if (DMU_USERUSED_DNODE(os
) != NULL
) {
879 if (DMU_PROJECTUSED_DNODE(os
) != NULL
)
880 dnode_evict_dbufs(DMU_PROJECTUSED_DNODE(os
));
881 dnode_evict_dbufs(DMU_GROUPUSED_DNODE(os
));
882 dnode_evict_dbufs(DMU_USERUSED_DNODE(os
));
884 dnode_evict_dbufs(DMU_META_DNODE(os
));
888 * Objset eviction processing is split into into two pieces.
889 * The first marks the objset as evicting, evicts any dbufs that
890 * have a refcount of zero, and then queues up the objset for the
891 * second phase of eviction. Once os->os_dnodes has been cleared by
892 * dnode_buf_pageout()->dnode_destroy(), the second phase is executed.
893 * The second phase closes the special dnodes, dequeues the objset from
894 * the list of those undergoing eviction, and finally frees the objset.
896 * NOTE: Due to asynchronous eviction processing (invocation of
897 * dnode_buf_pageout()), it is possible for the meta dnode for the
898 * objset to have no holds even though os->os_dnodes is not empty.
901 dmu_objset_evict(objset_t
*os
)
903 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
905 for (int t
= 0; t
< TXG_SIZE
; t
++)
906 ASSERT(!dmu_objset_is_dirty(os
, t
));
909 dsl_prop_unregister_all(ds
, os
);
914 dmu_objset_evict_dbufs(os
);
916 mutex_enter(&os
->os_lock
);
917 spa_evicting_os_register(os
->os_spa
, os
);
918 if (list_is_empty(&os
->os_dnodes
)) {
919 mutex_exit(&os
->os_lock
);
920 dmu_objset_evict_done(os
);
922 mutex_exit(&os
->os_lock
);
929 dmu_objset_evict_done(objset_t
*os
)
931 ASSERT3P(list_head(&os
->os_dnodes
), ==, NULL
);
933 dnode_special_close(&os
->os_meta_dnode
);
934 if (DMU_USERUSED_DNODE(os
)) {
935 if (DMU_PROJECTUSED_DNODE(os
))
936 dnode_special_close(&os
->os_projectused_dnode
);
937 dnode_special_close(&os
->os_userused_dnode
);
938 dnode_special_close(&os
->os_groupused_dnode
);
940 zil_free(os
->os_zil
);
942 arc_buf_destroy(os
->os_phys_buf
, &os
->os_phys_buf
);
945 * This is a barrier to prevent the objset from going away in
946 * dnode_move() until we can safely ensure that the objset is still in
947 * use. We consider the objset valid before the barrier and invalid
950 rw_enter(&os_lock
, RW_READER
);
953 kmem_free(os
->os_obj_next_percpu
,
954 os
->os_obj_next_percpu_len
* sizeof (os
->os_obj_next_percpu
[0]));
956 mutex_destroy(&os
->os_lock
);
957 mutex_destroy(&os
->os_userused_lock
);
958 mutex_destroy(&os
->os_obj_lock
);
959 mutex_destroy(&os
->os_user_ptr_lock
);
960 mutex_destroy(&os
->os_upgrade_lock
);
961 for (int i
= 0; i
< TXG_SIZE
; i
++) {
962 multilist_destroy(os
->os_dirty_dnodes
[i
]);
964 spa_evicting_os_deregister(os
->os_spa
, os
);
965 kmem_free(os
, sizeof (objset_t
));
969 dmu_objset_snap_cmtime(objset_t
*os
)
971 return (dsl_dir_snap_cmtime(os
->os_dsl_dataset
->ds_dir
));
975 dmu_objset_create_impl_dnstats(spa_t
*spa
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
976 dmu_objset_type_t type
, int levels
, int blksz
, int ibs
, dmu_tx_t
*tx
)
981 ASSERT(dmu_tx_is_syncing(tx
));
984 blksz
= DNODE_BLOCK_SIZE
;
986 ibs
= DN_MAX_INDBLKSHIFT
;
989 VERIFY0(dmu_objset_from_ds(ds
, &os
));
991 VERIFY0(dmu_objset_open_impl(spa
, NULL
, bp
, &os
));
993 mdn
= DMU_META_DNODE(os
);
995 dnode_allocate(mdn
, DMU_OT_DNODE
, blksz
, ibs
, DMU_OT_NONE
, 0,
996 DNODE_MIN_SLOTS
, tx
);
999 * We don't want to have to increase the meta-dnode's nlevels
1000 * later, because then we could do it in quescing context while
1001 * we are also accessing it in open context.
1003 * This precaution is not necessary for the MOS (ds == NULL),
1004 * because the MOS is only updated in syncing context.
1005 * This is most fortunate: the MOS is the only objset that
1006 * needs to be synced multiple times as spa_sync() iterates
1007 * to convergence, so minimizing its dn_nlevels matters.
1014 * Determine the number of levels necessary for the
1015 * meta-dnode to contain DN_MAX_OBJECT dnodes. Note
1016 * that in order to ensure that we do not overflow
1017 * 64 bits, there has to be a nlevels that gives us a
1018 * number of blocks > DN_MAX_OBJECT but < 2^64.
1019 * Therefore, (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)
1020 * (10) must be less than (64 - log2(DN_MAX_OBJECT))
1023 while ((uint64_t)mdn
->dn_nblkptr
<<
1024 (mdn
->dn_datablkshift
- DNODE_SHIFT
+ (levels
- 1) *
1025 (mdn
->dn_indblkshift
- SPA_BLKPTRSHIFT
)) <
1030 mdn
->dn_next_nlevels
[tx
->tx_txg
& TXG_MASK
] =
1031 mdn
->dn_nlevels
= levels
;
1034 ASSERT(type
!= DMU_OST_NONE
);
1035 ASSERT(type
!= DMU_OST_ANY
);
1036 ASSERT(type
< DMU_OST_NUMTYPES
);
1037 os
->os_phys
->os_type
= type
;
1040 * Enable user accounting if it is enabled and this is not an
1041 * encrypted receive.
1043 if (dmu_objset_userused_enabled(os
) &&
1044 (!os
->os_encrypted
|| !dmu_objset_is_receiving(os
))) {
1045 os
->os_phys
->os_flags
|= OBJSET_FLAG_USERACCOUNTING_COMPLETE
;
1046 if (dmu_objset_userobjused_enabled(os
)) {
1047 ds
->ds_feature_activation_needed
[
1048 SPA_FEATURE_USEROBJ_ACCOUNTING
] = B_TRUE
;
1049 os
->os_phys
->os_flags
|=
1050 OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE
;
1052 if (dmu_objset_projectquota_enabled(os
)) {
1053 ds
->ds_feature_activation_needed
[
1054 SPA_FEATURE_PROJECT_QUOTA
] = B_TRUE
;
1055 os
->os_phys
->os_flags
|=
1056 OBJSET_FLAG_PROJECTQUOTA_COMPLETE
;
1058 os
->os_flags
= os
->os_phys
->os_flags
;
1061 dsl_dataset_dirty(ds
, tx
);
1066 /* called from dsl for meta-objset */
1068 dmu_objset_create_impl(spa_t
*spa
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
1069 dmu_objset_type_t type
, dmu_tx_t
*tx
)
1071 return (dmu_objset_create_impl_dnstats(spa
, ds
, bp
, type
, 0, 0, 0, tx
));
1074 typedef struct dmu_objset_create_arg
{
1075 const char *doca_name
;
1077 void (*doca_userfunc
)(objset_t
*os
, void *arg
,
1078 cred_t
*cr
, dmu_tx_t
*tx
);
1080 dmu_objset_type_t doca_type
;
1081 uint64_t doca_flags
;
1082 dsl_crypto_params_t
*doca_dcp
;
1083 } dmu_objset_create_arg_t
;
1087 dmu_objset_create_check(void *arg
, dmu_tx_t
*tx
)
1089 dmu_objset_create_arg_t
*doca
= arg
;
1090 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1095 if (strchr(doca
->doca_name
, '@') != NULL
)
1096 return (SET_ERROR(EINVAL
));
1098 if (strlen(doca
->doca_name
) >= ZFS_MAX_DATASET_NAME_LEN
)
1099 return (SET_ERROR(ENAMETOOLONG
));
1101 error
= dsl_dir_hold(dp
, doca
->doca_name
, FTAG
, &pdd
, &tail
);
1105 dsl_dir_rele(pdd
, FTAG
);
1106 return (SET_ERROR(EEXIST
));
1109 error
= dmu_objset_create_crypt_check(pdd
, doca
->doca_dcp
);
1111 dsl_dir_rele(pdd
, FTAG
);
1115 error
= dsl_fs_ss_limit_check(pdd
, 1, ZFS_PROP_FILESYSTEM_LIMIT
, NULL
,
1118 dsl_dir_rele(pdd
, FTAG
);
1124 dmu_objset_create_sync(void *arg
, dmu_tx_t
*tx
)
1126 dmu_objset_create_arg_t
*doca
= arg
;
1127 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1136 VERIFY0(dsl_dir_hold(dp
, doca
->doca_name
, FTAG
, &pdd
, &tail
));
1138 obj
= dsl_dataset_create_sync(pdd
, tail
, NULL
, doca
->doca_flags
,
1139 doca
->doca_cred
, doca
->doca_dcp
, tx
);
1141 VERIFY0(dsl_dataset_hold_obj_flags(pdd
->dd_pool
, obj
,
1142 DS_HOLD_FLAG_DECRYPT
, FTAG
, &ds
));
1143 rrw_enter(&ds
->ds_bp_rwlock
, RW_READER
, FTAG
);
1144 bp
= dsl_dataset_get_blkptr(ds
);
1145 os
= dmu_objset_create_impl(pdd
->dd_pool
->dp_spa
,
1146 ds
, bp
, doca
->doca_type
, tx
);
1147 rrw_exit(&ds
->ds_bp_rwlock
, FTAG
);
1149 if (doca
->doca_userfunc
!= NULL
) {
1150 doca
->doca_userfunc(os
, doca
->doca_userarg
,
1151 doca
->doca_cred
, tx
);
1155 * The doca_userfunc() may write out some data that needs to be
1156 * encrypted if the dataset is encrypted (specifically the root
1157 * directory). This data must be written out before the encryption
1158 * key mapping is removed by dsl_dataset_rele_flags(). Force the
1159 * I/O to occur immediately by invoking the relevant sections of
1162 if (os
->os_encrypted
) {
1163 dsl_dataset_t
*tmpds
= NULL
;
1164 boolean_t need_sync_done
= B_FALSE
;
1166 mutex_enter(&ds
->ds_lock
);
1167 ds
->ds_owner
= FTAG
;
1168 mutex_exit(&ds
->ds_lock
);
1170 rzio
= zio_root(dp
->dp_spa
, NULL
, NULL
, ZIO_FLAG_MUSTSUCCEED
);
1171 tmpds
= txg_list_remove_this(&dp
->dp_dirty_datasets
, ds
,
1173 if (tmpds
!= NULL
) {
1174 dsl_dataset_sync(ds
, rzio
, tx
);
1175 need_sync_done
= B_TRUE
;
1177 VERIFY0(zio_wait(rzio
));
1179 dmu_objset_do_userquota_updates(os
, tx
);
1180 taskq_wait(dp
->dp_sync_taskq
);
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 dmu_buf_rele(ds
->ds_dbuf
, ds
);
1187 dsl_dataset_sync(ds
, rzio
, tx
);
1189 VERIFY0(zio_wait(rzio
));
1192 dsl_dataset_sync_done(ds
, tx
);
1194 mutex_enter(&ds
->ds_lock
);
1195 ds
->ds_owner
= NULL
;
1196 mutex_exit(&ds
->ds_lock
);
1199 spa_history_log_internal_ds(ds
, "create", tx
, "");
1200 zvol_create_minors(dp
->dp_spa
, doca
->doca_name
, B_TRUE
);
1202 dsl_dataset_rele_flags(ds
, DS_HOLD_FLAG_DECRYPT
, FTAG
);
1203 dsl_dir_rele(pdd
, FTAG
);
1207 dmu_objset_create(const char *name
, dmu_objset_type_t type
, uint64_t flags
,
1208 dsl_crypto_params_t
*dcp
, dmu_objset_create_sync_func_t func
, void *arg
)
1210 dmu_objset_create_arg_t doca
;
1211 dsl_crypto_params_t tmp_dcp
= { 0 };
1213 doca
.doca_name
= name
;
1214 doca
.doca_cred
= CRED();
1215 doca
.doca_flags
= flags
;
1216 doca
.doca_userfunc
= func
;
1217 doca
.doca_userarg
= arg
;
1218 doca
.doca_type
= type
;
1221 * Some callers (mostly for testing) do not provide a dcp on their
1222 * own but various code inside the sync task will require it to be
1223 * allocated. Rather than adding NULL checks throughout this code
1224 * or adding dummy dcp's to all of the callers we simply create a
1225 * dummy one here and use that. This zero dcp will have the same
1226 * effect as asking for inheritance of all encryption params.
1228 doca
.doca_dcp
= (dcp
!= NULL
) ? dcp
: &tmp_dcp
;
1230 return (dsl_sync_task(name
,
1231 dmu_objset_create_check
, dmu_objset_create_sync
, &doca
,
1232 6, ZFS_SPACE_CHECK_NORMAL
));
1235 typedef struct dmu_objset_clone_arg
{
1236 const char *doca_clone
;
1237 const char *doca_origin
;
1239 } dmu_objset_clone_arg_t
;
1243 dmu_objset_clone_check(void *arg
, dmu_tx_t
*tx
)
1245 dmu_objset_clone_arg_t
*doca
= arg
;
1249 dsl_dataset_t
*origin
;
1250 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1252 if (strchr(doca
->doca_clone
, '@') != NULL
)
1253 return (SET_ERROR(EINVAL
));
1255 if (strlen(doca
->doca_clone
) >= ZFS_MAX_DATASET_NAME_LEN
)
1256 return (SET_ERROR(ENAMETOOLONG
));
1258 error
= dsl_dir_hold(dp
, doca
->doca_clone
, FTAG
, &pdd
, &tail
);
1262 dsl_dir_rele(pdd
, FTAG
);
1263 return (SET_ERROR(EEXIST
));
1266 error
= dsl_fs_ss_limit_check(pdd
, 1, ZFS_PROP_FILESYSTEM_LIMIT
, NULL
,
1269 dsl_dir_rele(pdd
, FTAG
);
1270 return (SET_ERROR(EDQUOT
));
1273 error
= dsl_dataset_hold(dp
, doca
->doca_origin
, FTAG
, &origin
);
1275 dsl_dir_rele(pdd
, FTAG
);
1279 /* You can only clone snapshots, not the head datasets. */
1280 if (!origin
->ds_is_snapshot
) {
1281 dsl_dataset_rele(origin
, FTAG
);
1282 dsl_dir_rele(pdd
, FTAG
);
1283 return (SET_ERROR(EINVAL
));
1286 error
= dmu_objset_clone_crypt_check(pdd
, origin
->ds_dir
);
1288 dsl_dataset_rele(origin
, FTAG
);
1289 dsl_dir_rele(pdd
, FTAG
);
1293 dsl_dataset_rele(origin
, FTAG
);
1294 dsl_dir_rele(pdd
, FTAG
);
1300 dmu_objset_clone_sync(void *arg
, dmu_tx_t
*tx
)
1302 dmu_objset_clone_arg_t
*doca
= arg
;
1303 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1306 dsl_dataset_t
*origin
, *ds
;
1308 char namebuf
[ZFS_MAX_DATASET_NAME_LEN
];
1310 VERIFY0(dsl_dir_hold(dp
, doca
->doca_clone
, FTAG
, &pdd
, &tail
));
1311 VERIFY0(dsl_dataset_hold(dp
, doca
->doca_origin
, FTAG
, &origin
));
1313 obj
= dsl_dataset_create_sync(pdd
, tail
, origin
, 0,
1314 doca
->doca_cred
, NULL
, tx
);
1316 VERIFY0(dsl_dataset_hold_obj(pdd
->dd_pool
, obj
, FTAG
, &ds
));
1317 dsl_dataset_name(origin
, namebuf
);
1318 spa_history_log_internal_ds(ds
, "clone", tx
,
1319 "origin=%s (%llu)", namebuf
, origin
->ds_object
);
1320 zvol_create_minors(dp
->dp_spa
, doca
->doca_clone
, B_TRUE
);
1321 dsl_dataset_rele(ds
, FTAG
);
1322 dsl_dataset_rele(origin
, FTAG
);
1323 dsl_dir_rele(pdd
, FTAG
);
1327 dmu_objset_clone(const char *clone
, const char *origin
)
1329 dmu_objset_clone_arg_t doca
;
1331 doca
.doca_clone
= clone
;
1332 doca
.doca_origin
= origin
;
1333 doca
.doca_cred
= CRED();
1335 return (dsl_sync_task(clone
,
1336 dmu_objset_clone_check
, dmu_objset_clone_sync
, &doca
,
1337 6, ZFS_SPACE_CHECK_NORMAL
));
1341 dmu_objset_remap_indirects_impl(objset_t
*os
, uint64_t last_removed_txg
)
1344 uint64_t object
= 0;
1345 while ((error
= dmu_object_next(os
, &object
, B_FALSE
, 0)) == 0) {
1346 error
= dmu_object_remap_indirects(os
, object
,
1349 * If the ZPL removed the object before we managed to dnode_hold
1350 * it, we would get an ENOENT. If the ZPL declares its intent
1351 * to remove the object (dnode_free) before we manage to
1352 * dnode_hold it, we would get an EEXIST. In either case, we
1353 * want to continue remapping the other objects in the objset;
1354 * in all other cases, we want to break early.
1356 if (error
!= 0 && error
!= ENOENT
&& error
!= EEXIST
) {
1360 if (error
== ESRCH
) {
1367 dmu_objset_remap_indirects(const char *fsname
)
1370 objset_t
*os
= NULL
;
1371 uint64_t last_removed_txg
;
1372 uint64_t remap_start_txg
;
1375 error
= dmu_objset_hold(fsname
, FTAG
, &os
);
1379 dd
= dmu_objset_ds(os
)->ds_dir
;
1381 if (!spa_feature_is_enabled(dmu_objset_spa(os
),
1382 SPA_FEATURE_OBSOLETE_COUNTS
)) {
1383 dmu_objset_rele(os
, FTAG
);
1384 return (SET_ERROR(ENOTSUP
));
1387 if (dsl_dataset_is_snapshot(dmu_objset_ds(os
))) {
1388 dmu_objset_rele(os
, FTAG
);
1389 return (SET_ERROR(EINVAL
));
1393 * If there has not been a removal, we're done.
1395 last_removed_txg
= spa_get_last_removal_txg(dmu_objset_spa(os
));
1396 if (last_removed_txg
== -1ULL) {
1397 dmu_objset_rele(os
, FTAG
);
1402 * If we have remapped since the last removal, we're done.
1404 if (dsl_dir_is_zapified(dd
)) {
1405 uint64_t last_remap_txg
;
1406 if (zap_lookup(spa_meta_objset(dmu_objset_spa(os
)),
1407 dd
->dd_object
, DD_FIELD_LAST_REMAP_TXG
,
1408 sizeof (last_remap_txg
), 1, &last_remap_txg
) == 0 &&
1409 last_remap_txg
> last_removed_txg
) {
1410 dmu_objset_rele(os
, FTAG
);
1415 dsl_dataset_long_hold(dmu_objset_ds(os
), FTAG
);
1416 dsl_pool_rele(dmu_objset_pool(os
), FTAG
);
1418 remap_start_txg
= spa_last_synced_txg(dmu_objset_spa(os
));
1419 error
= dmu_objset_remap_indirects_impl(os
, last_removed_txg
);
1422 * We update the last_remap_txg to be the start txg so that
1423 * we can guarantee that every block older than last_remap_txg
1424 * that can be remapped has been remapped.
1426 error
= dsl_dir_update_last_remap_txg(dd
, remap_start_txg
);
1429 dsl_dataset_long_rele(dmu_objset_ds(os
), FTAG
);
1430 dsl_dataset_rele(dmu_objset_ds(os
), FTAG
);
1436 dmu_objset_snapshot_one(const char *fsname
, const char *snapname
)
1439 char *longsnap
= kmem_asprintf("%s@%s", fsname
, snapname
);
1440 nvlist_t
*snaps
= fnvlist_alloc();
1442 fnvlist_add_boolean(snaps
, longsnap
);
1444 err
= dsl_dataset_snapshot(snaps
, NULL
, NULL
);
1445 fnvlist_free(snaps
);
1450 dmu_objset_upgrade_task_cb(void *data
)
1452 objset_t
*os
= data
;
1454 mutex_enter(&os
->os_upgrade_lock
);
1455 os
->os_upgrade_status
= EINTR
;
1456 if (!os
->os_upgrade_exit
) {
1457 mutex_exit(&os
->os_upgrade_lock
);
1459 os
->os_upgrade_status
= os
->os_upgrade_cb(os
);
1460 mutex_enter(&os
->os_upgrade_lock
);
1462 os
->os_upgrade_exit
= B_TRUE
;
1463 os
->os_upgrade_id
= 0;
1464 mutex_exit(&os
->os_upgrade_lock
);
1465 dsl_dataset_long_rele(dmu_objset_ds(os
), upgrade_tag
);
1469 dmu_objset_upgrade(objset_t
*os
, dmu_objset_upgrade_cb_t cb
)
1471 if (os
->os_upgrade_id
!= 0)
1474 ASSERT(dsl_pool_config_held(dmu_objset_pool(os
)));
1475 dsl_dataset_long_hold(dmu_objset_ds(os
), upgrade_tag
);
1477 mutex_enter(&os
->os_upgrade_lock
);
1478 if (os
->os_upgrade_id
== 0 && os
->os_upgrade_status
== 0) {
1479 os
->os_upgrade_exit
= B_FALSE
;
1480 os
->os_upgrade_cb
= cb
;
1481 os
->os_upgrade_id
= taskq_dispatch(
1482 os
->os_spa
->spa_upgrade_taskq
,
1483 dmu_objset_upgrade_task_cb
, os
, TQ_SLEEP
);
1484 if (os
->os_upgrade_id
== TASKQID_INVALID
) {
1485 dsl_dataset_long_rele(dmu_objset_ds(os
), upgrade_tag
);
1486 os
->os_upgrade_status
= ENOMEM
;
1489 mutex_exit(&os
->os_upgrade_lock
);
1493 dmu_objset_upgrade_stop(objset_t
*os
)
1495 mutex_enter(&os
->os_upgrade_lock
);
1496 os
->os_upgrade_exit
= B_TRUE
;
1497 if (os
->os_upgrade_id
!= 0) {
1498 taskqid_t id
= os
->os_upgrade_id
;
1500 os
->os_upgrade_id
= 0;
1501 mutex_exit(&os
->os_upgrade_lock
);
1503 if ((taskq_cancel_id(os
->os_spa
->spa_upgrade_taskq
, id
)) == 0) {
1504 dsl_dataset_long_rele(dmu_objset_ds(os
), upgrade_tag
);
1506 txg_wait_synced(os
->os_spa
->spa_dsl_pool
, 0);
1508 mutex_exit(&os
->os_upgrade_lock
);
1513 dmu_objset_sync_dnodes(multilist_sublist_t
*list
, dmu_tx_t
*tx
)
1517 while ((dn
= multilist_sublist_head(list
)) != NULL
) {
1518 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
);
1519 ASSERT(dn
->dn_dbuf
->db_data_pending
);
1521 * Initialize dn_zio outside dnode_sync() because the
1522 * meta-dnode needs to set it ouside dnode_sync().
1524 dn
->dn_zio
= dn
->dn_dbuf
->db_data_pending
->dr_zio
;
1527 ASSERT3U(dn
->dn_nlevels
, <=, DN_MAX_LEVELS
);
1528 multilist_sublist_remove(list
, dn
);
1531 * If we are not doing useraccounting (os_synced_dnodes == NULL)
1532 * we are done with this dnode for this txg. Unset dn_dirty_txg
1533 * if later txgs aren't dirtying it so that future holders do
1534 * not get a stale value. Otherwise, we will do this in
1535 * userquota_updates_task() when processing has completely
1536 * finished for this txg.
1538 multilist_t
*newlist
= dn
->dn_objset
->os_synced_dnodes
;
1539 if (newlist
!= NULL
) {
1540 (void) dnode_add_ref(dn
, newlist
);
1541 multilist_insert(newlist
, dn
);
1543 mutex_enter(&dn
->dn_mtx
);
1544 if (dn
->dn_dirty_txg
== tx
->tx_txg
)
1545 dn
->dn_dirty_txg
= 0;
1546 mutex_exit(&dn
->dn_mtx
);
1555 dmu_objset_write_ready(zio_t
*zio
, arc_buf_t
*abuf
, void *arg
)
1557 blkptr_t
*bp
= zio
->io_bp
;
1559 dnode_phys_t
*dnp
= &os
->os_phys
->os_meta_dnode
;
1562 ASSERT(!BP_IS_EMBEDDED(bp
));
1563 ASSERT3U(BP_GET_TYPE(bp
), ==, DMU_OT_OBJSET
);
1564 ASSERT0(BP_GET_LEVEL(bp
));
1567 * Update rootbp fill count: it should be the number of objects
1568 * allocated in the object set (not counting the "special"
1569 * objects that are stored in the objset_phys_t -- the meta
1570 * dnode and user/group/project accounting objects).
1572 for (int i
= 0; i
< dnp
->dn_nblkptr
; i
++)
1573 fill
+= BP_GET_FILL(&dnp
->dn_blkptr
[i
]);
1575 BP_SET_FILL(bp
, fill
);
1577 if (os
->os_dsl_dataset
!= NULL
)
1578 rrw_enter(&os
->os_dsl_dataset
->ds_bp_rwlock
, RW_WRITER
, FTAG
);
1579 *os
->os_rootbp
= *bp
;
1580 if (os
->os_dsl_dataset
!= NULL
)
1581 rrw_exit(&os
->os_dsl_dataset
->ds_bp_rwlock
, FTAG
);
1586 dmu_objset_write_done(zio_t
*zio
, arc_buf_t
*abuf
, void *arg
)
1588 blkptr_t
*bp
= zio
->io_bp
;
1589 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
1592 if (zio
->io_flags
& ZIO_FLAG_IO_REWRITE
) {
1593 ASSERT(BP_EQUAL(bp
, bp_orig
));
1595 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
1596 dmu_tx_t
*tx
= os
->os_synctx
;
1598 (void) dsl_dataset_block_kill(ds
, bp_orig
, tx
, B_TRUE
);
1599 dsl_dataset_block_born(ds
, bp
, tx
);
1601 kmem_free(bp
, sizeof (*bp
));
1604 typedef struct sync_dnodes_arg
{
1605 multilist_t
*sda_list
;
1606 int sda_sublist_idx
;
1607 multilist_t
*sda_newlist
;
1609 } sync_dnodes_arg_t
;
1612 sync_dnodes_task(void *arg
)
1614 sync_dnodes_arg_t
*sda
= arg
;
1616 multilist_sublist_t
*ms
=
1617 multilist_sublist_lock(sda
->sda_list
, sda
->sda_sublist_idx
);
1619 dmu_objset_sync_dnodes(ms
, sda
->sda_tx
);
1621 multilist_sublist_unlock(ms
);
1623 kmem_free(sda
, sizeof (*sda
));
1627 /* called from dsl */
1629 dmu_objset_sync(objset_t
*os
, zio_t
*pio
, dmu_tx_t
*tx
)
1632 zbookmark_phys_t zb
;
1636 dbuf_dirty_record_t
*dr
;
1637 blkptr_t
*blkptr_copy
= kmem_alloc(sizeof (*os
->os_rootbp
), KM_SLEEP
);
1638 *blkptr_copy
= *os
->os_rootbp
;
1640 dprintf_ds(os
->os_dsl_dataset
, "txg=%llu\n", tx
->tx_txg
);
1642 ASSERT(dmu_tx_is_syncing(tx
));
1643 /* XXX the write_done callback should really give us the tx... */
1646 if (os
->os_dsl_dataset
== NULL
) {
1648 * This is the MOS. If we have upgraded,
1649 * spa_max_replication() could change, so reset
1652 os
->os_copies
= spa_max_replication(os
->os_spa
);
1656 * Create the root block IO
1658 SET_BOOKMARK(&zb
, os
->os_dsl_dataset
?
1659 os
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
1660 ZB_ROOT_OBJECT
, ZB_ROOT_LEVEL
, ZB_ROOT_BLKID
);
1661 arc_release(os
->os_phys_buf
, &os
->os_phys_buf
);
1663 dmu_write_policy(os
, NULL
, 0, 0, &zp
);
1666 * If we are either claiming the ZIL or doing a raw receive, write
1667 * out the os_phys_buf raw. Neither of these actions will effect the
1668 * MAC at this point.
1670 if (os
->os_raw_receive
||
1671 os
->os_next_write_raw
[tx
->tx_txg
& TXG_MASK
]) {
1672 ASSERT(os
->os_encrypted
);
1673 os
->os_next_write_raw
[tx
->tx_txg
& TXG_MASK
] = B_FALSE
;
1674 arc_convert_to_raw(os
->os_phys_buf
,
1675 os
->os_dsl_dataset
->ds_object
, ZFS_HOST_BYTEORDER
,
1676 DMU_OT_OBJSET
, NULL
, NULL
, NULL
);
1679 zio
= arc_write(pio
, os
->os_spa
, tx
->tx_txg
,
1680 blkptr_copy
, os
->os_phys_buf
, DMU_OS_IS_L2CACHEABLE(os
),
1681 &zp
, dmu_objset_write_ready
, NULL
, NULL
, dmu_objset_write_done
,
1682 os
, ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);
1685 * Sync special dnodes - the parent IO for the sync is the root block
1687 DMU_META_DNODE(os
)->dn_zio
= zio
;
1688 dnode_sync(DMU_META_DNODE(os
), tx
);
1690 os
->os_phys
->os_flags
= os
->os_flags
;
1692 if (DMU_USERUSED_DNODE(os
) &&
1693 DMU_USERUSED_DNODE(os
)->dn_type
!= DMU_OT_NONE
) {
1694 DMU_USERUSED_DNODE(os
)->dn_zio
= zio
;
1695 dnode_sync(DMU_USERUSED_DNODE(os
), tx
);
1696 DMU_GROUPUSED_DNODE(os
)->dn_zio
= zio
;
1697 dnode_sync(DMU_GROUPUSED_DNODE(os
), tx
);
1700 if (DMU_PROJECTUSED_DNODE(os
) &&
1701 DMU_PROJECTUSED_DNODE(os
)->dn_type
!= DMU_OT_NONE
) {
1702 DMU_PROJECTUSED_DNODE(os
)->dn_zio
= zio
;
1703 dnode_sync(DMU_PROJECTUSED_DNODE(os
), tx
);
1706 txgoff
= tx
->tx_txg
& TXG_MASK
;
1708 if (dmu_objset_userused_enabled(os
) &&
1709 (!os
->os_encrypted
|| !dmu_objset_is_receiving(os
))) {
1711 * We must create the list here because it uses the
1712 * dn_dirty_link[] of this txg. But it may already
1713 * exist because we call dsl_dataset_sync() twice per txg.
1715 if (os
->os_synced_dnodes
== NULL
) {
1716 os
->os_synced_dnodes
=
1717 multilist_create(sizeof (dnode_t
),
1718 offsetof(dnode_t
, dn_dirty_link
[txgoff
]),
1719 dnode_multilist_index_func
);
1721 ASSERT3U(os
->os_synced_dnodes
->ml_offset
, ==,
1722 offsetof(dnode_t
, dn_dirty_link
[txgoff
]));
1727 i
< multilist_get_num_sublists(os
->os_dirty_dnodes
[txgoff
]); i
++) {
1728 sync_dnodes_arg_t
*sda
= kmem_alloc(sizeof (*sda
), KM_SLEEP
);
1729 sda
->sda_list
= os
->os_dirty_dnodes
[txgoff
];
1730 sda
->sda_sublist_idx
= i
;
1732 (void) taskq_dispatch(dmu_objset_pool(os
)->dp_sync_taskq
,
1733 sync_dnodes_task
, sda
, 0);
1734 /* callback frees sda */
1736 taskq_wait(dmu_objset_pool(os
)->dp_sync_taskq
);
1738 list
= &DMU_META_DNODE(os
)->dn_dirty_records
[txgoff
];
1739 while ((dr
= list_head(list
)) != NULL
) {
1740 ASSERT0(dr
->dr_dbuf
->db_level
);
1741 list_remove(list
, dr
);
1743 zio_nowait(dr
->dr_zio
);
1746 /* Enable dnode backfill if enough objects have been freed. */
1747 if (os
->os_freed_dnodes
>= dmu_rescan_dnode_threshold
) {
1748 os
->os_rescan_dnodes
= B_TRUE
;
1749 os
->os_freed_dnodes
= 0;
1753 * Free intent log blocks up to this tx.
1755 zil_sync(os
->os_zil
, tx
);
1756 os
->os_phys
->os_zil_header
= os
->os_zil_header
;
1761 dmu_objset_is_dirty(objset_t
*os
, uint64_t txg
)
1763 return (!multilist_is_empty(os
->os_dirty_dnodes
[txg
& TXG_MASK
]));
1766 static objset_used_cb_t
*used_cbs
[DMU_OST_NUMTYPES
];
1769 dmu_objset_register_type(dmu_objset_type_t ost
, objset_used_cb_t
*cb
)
1775 dmu_objset_userused_enabled(objset_t
*os
)
1777 return (spa_version(os
->os_spa
) >= SPA_VERSION_USERSPACE
&&
1778 used_cbs
[os
->os_phys
->os_type
] != NULL
&&
1779 DMU_USERUSED_DNODE(os
) != NULL
);
1783 dmu_objset_userobjused_enabled(objset_t
*os
)
1785 return (dmu_objset_userused_enabled(os
) &&
1786 spa_feature_is_enabled(os
->os_spa
, SPA_FEATURE_USEROBJ_ACCOUNTING
));
1790 dmu_objset_projectquota_enabled(objset_t
*os
)
1792 return (used_cbs
[os
->os_phys
->os_type
] != NULL
&&
1793 DMU_PROJECTUSED_DNODE(os
) != NULL
&&
1794 spa_feature_is_enabled(os
->os_spa
, SPA_FEATURE_PROJECT_QUOTA
));
1797 typedef struct userquota_node
{
1798 /* must be in the first filed, see userquota_update_cache() */
1799 char uqn_id
[20 + DMU_OBJACCT_PREFIX_LEN
];
1801 avl_node_t uqn_node
;
1804 typedef struct userquota_cache
{
1805 avl_tree_t uqc_user_deltas
;
1806 avl_tree_t uqc_group_deltas
;
1807 avl_tree_t uqc_project_deltas
;
1808 } userquota_cache_t
;
1811 userquota_compare(const void *l
, const void *r
)
1813 const userquota_node_t
*luqn
= l
;
1814 const userquota_node_t
*ruqn
= r
;
1818 * NB: can only access uqn_id because userquota_update_cache() doesn't
1819 * pass in an entire userquota_node_t.
1821 rv
= strcmp(luqn
->uqn_id
, ruqn
->uqn_id
);
1823 return (AVL_ISIGN(rv
));
1827 do_userquota_cacheflush(objset_t
*os
, userquota_cache_t
*cache
, dmu_tx_t
*tx
)
1830 userquota_node_t
*uqn
;
1832 ASSERT(dmu_tx_is_syncing(tx
));
1835 while ((uqn
= avl_destroy_nodes(&cache
->uqc_user_deltas
,
1836 &cookie
)) != NULL
) {
1838 * os_userused_lock protects against concurrent calls to
1839 * zap_increment_int(). It's needed because zap_increment_int()
1840 * is not thread-safe (i.e. not atomic).
1842 mutex_enter(&os
->os_userused_lock
);
1843 VERIFY0(zap_increment(os
, DMU_USERUSED_OBJECT
,
1844 uqn
->uqn_id
, uqn
->uqn_delta
, tx
));
1845 mutex_exit(&os
->os_userused_lock
);
1846 kmem_free(uqn
, sizeof (*uqn
));
1848 avl_destroy(&cache
->uqc_user_deltas
);
1851 while ((uqn
= avl_destroy_nodes(&cache
->uqc_group_deltas
,
1852 &cookie
)) != NULL
) {
1853 mutex_enter(&os
->os_userused_lock
);
1854 VERIFY0(zap_increment(os
, DMU_GROUPUSED_OBJECT
,
1855 uqn
->uqn_id
, uqn
->uqn_delta
, tx
));
1856 mutex_exit(&os
->os_userused_lock
);
1857 kmem_free(uqn
, sizeof (*uqn
));
1859 avl_destroy(&cache
->uqc_group_deltas
);
1861 if (dmu_objset_projectquota_enabled(os
)) {
1863 while ((uqn
= avl_destroy_nodes(&cache
->uqc_project_deltas
,
1864 &cookie
)) != NULL
) {
1865 mutex_enter(&os
->os_userused_lock
);
1866 VERIFY0(zap_increment(os
, DMU_PROJECTUSED_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_project_deltas
);
1876 userquota_update_cache(avl_tree_t
*avl
, const char *id
, int64_t delta
)
1878 userquota_node_t
*uqn
;
1881 ASSERT(strlen(id
) < sizeof (uqn
->uqn_id
));
1883 * Use id directly for searching because uqn_id is the first field of
1884 * userquota_node_t and fields after uqn_id won't be accessed in
1887 uqn
= avl_find(avl
, (const void *)id
, &idx
);
1889 uqn
= kmem_zalloc(sizeof (*uqn
), KM_SLEEP
);
1890 strlcpy(uqn
->uqn_id
, id
, sizeof (uqn
->uqn_id
));
1891 avl_insert(avl
, uqn
, idx
);
1893 uqn
->uqn_delta
+= delta
;
1897 do_userquota_update(objset_t
*os
, userquota_cache_t
*cache
, uint64_t used
,
1898 uint64_t flags
, uint64_t user
, uint64_t group
, uint64_t project
,
1901 if (flags
& DNODE_FLAG_USERUSED_ACCOUNTED
) {
1902 int64_t delta
= DNODE_MIN_SIZE
+ used
;
1908 (void) sprintf(name
, "%llx", (longlong_t
)user
);
1909 userquota_update_cache(&cache
->uqc_user_deltas
, name
, delta
);
1911 (void) sprintf(name
, "%llx", (longlong_t
)group
);
1912 userquota_update_cache(&cache
->uqc_group_deltas
, name
, delta
);
1914 if (dmu_objset_projectquota_enabled(os
)) {
1915 (void) sprintf(name
, "%llx", (longlong_t
)project
);
1916 userquota_update_cache(&cache
->uqc_project_deltas
,
1923 do_userobjquota_update(objset_t
*os
, userquota_cache_t
*cache
, uint64_t flags
,
1924 uint64_t user
, uint64_t group
, uint64_t project
, boolean_t subtract
)
1926 if (flags
& DNODE_FLAG_USEROBJUSED_ACCOUNTED
) {
1927 char name
[20 + DMU_OBJACCT_PREFIX_LEN
];
1928 int delta
= subtract
? -1 : 1;
1930 (void) snprintf(name
, sizeof (name
), DMU_OBJACCT_PREFIX
"%llx",
1932 userquota_update_cache(&cache
->uqc_user_deltas
, name
, delta
);
1934 (void) snprintf(name
, sizeof (name
), DMU_OBJACCT_PREFIX
"%llx",
1936 userquota_update_cache(&cache
->uqc_group_deltas
, name
, delta
);
1938 if (dmu_objset_projectquota_enabled(os
)) {
1939 (void) snprintf(name
, sizeof (name
),
1940 DMU_OBJACCT_PREFIX
"%llx", (longlong_t
)project
);
1941 userquota_update_cache(&cache
->uqc_project_deltas
,
1947 typedef struct userquota_updates_arg
{
1949 int uua_sublist_idx
;
1951 } userquota_updates_arg_t
;
1954 userquota_updates_task(void *arg
)
1956 userquota_updates_arg_t
*uua
= arg
;
1957 objset_t
*os
= uua
->uua_os
;
1958 dmu_tx_t
*tx
= uua
->uua_tx
;
1960 userquota_cache_t cache
= { { 0 } };
1962 multilist_sublist_t
*list
=
1963 multilist_sublist_lock(os
->os_synced_dnodes
, uua
->uua_sublist_idx
);
1965 ASSERT(multilist_sublist_head(list
) == NULL
||
1966 dmu_objset_userused_enabled(os
));
1967 avl_create(&cache
.uqc_user_deltas
, userquota_compare
,
1968 sizeof (userquota_node_t
), offsetof(userquota_node_t
, uqn_node
));
1969 avl_create(&cache
.uqc_group_deltas
, userquota_compare
,
1970 sizeof (userquota_node_t
), offsetof(userquota_node_t
, uqn_node
));
1971 if (dmu_objset_projectquota_enabled(os
))
1972 avl_create(&cache
.uqc_project_deltas
, userquota_compare
,
1973 sizeof (userquota_node_t
), offsetof(userquota_node_t
,
1976 while ((dn
= multilist_sublist_head(list
)) != NULL
) {
1978 ASSERT(!DMU_OBJECT_IS_SPECIAL(dn
->dn_object
));
1979 ASSERT(dn
->dn_phys
->dn_type
== DMU_OT_NONE
||
1980 dn
->dn_phys
->dn_flags
&
1981 DNODE_FLAG_USERUSED_ACCOUNTED
);
1983 flags
= dn
->dn_id_flags
;
1985 if (flags
& DN_ID_OLD_EXIST
) {
1986 do_userquota_update(os
, &cache
, dn
->dn_oldused
,
1987 dn
->dn_oldflags
, dn
->dn_olduid
, dn
->dn_oldgid
,
1988 dn
->dn_oldprojid
, B_TRUE
);
1989 do_userobjquota_update(os
, &cache
, dn
->dn_oldflags
,
1990 dn
->dn_olduid
, dn
->dn_oldgid
,
1991 dn
->dn_oldprojid
, B_TRUE
);
1993 if (flags
& DN_ID_NEW_EXIST
) {
1994 do_userquota_update(os
, &cache
,
1995 DN_USED_BYTES(dn
->dn_phys
), dn
->dn_phys
->dn_flags
,
1996 dn
->dn_newuid
, dn
->dn_newgid
,
1997 dn
->dn_newprojid
, B_FALSE
);
1998 do_userobjquota_update(os
, &cache
,
1999 dn
->dn_phys
->dn_flags
, dn
->dn_newuid
, dn
->dn_newgid
,
2000 dn
->dn_newprojid
, B_FALSE
);
2003 mutex_enter(&dn
->dn_mtx
);
2005 dn
->dn_oldflags
= 0;
2006 if (dn
->dn_id_flags
& DN_ID_NEW_EXIST
) {
2007 dn
->dn_olduid
= dn
->dn_newuid
;
2008 dn
->dn_oldgid
= dn
->dn_newgid
;
2009 dn
->dn_oldprojid
= dn
->dn_newprojid
;
2010 dn
->dn_id_flags
|= DN_ID_OLD_EXIST
;
2011 if (dn
->dn_bonuslen
== 0)
2012 dn
->dn_id_flags
|= DN_ID_CHKED_SPILL
;
2014 dn
->dn_id_flags
|= DN_ID_CHKED_BONUS
;
2016 dn
->dn_id_flags
&= ~(DN_ID_NEW_EXIST
);
2017 if (dn
->dn_dirty_txg
== spa_syncing_txg(os
->os_spa
))
2018 dn
->dn_dirty_txg
= 0;
2019 mutex_exit(&dn
->dn_mtx
);
2021 multilist_sublist_remove(list
, dn
);
2022 dnode_rele(dn
, os
->os_synced_dnodes
);
2024 do_userquota_cacheflush(os
, &cache
, tx
);
2025 multilist_sublist_unlock(list
);
2026 kmem_free(uua
, sizeof (*uua
));
2030 dmu_objset_do_userquota_updates(objset_t
*os
, dmu_tx_t
*tx
)
2032 if (!dmu_objset_userused_enabled(os
))
2036 * If this is a raw receive just return and handle accounting
2037 * later when we have the keys loaded. We also don't do user
2038 * accounting during claiming since the datasets are not owned
2039 * for the duration of claiming and this txg should only be
2040 * used for recovery.
2042 if (os
->os_encrypted
&& dmu_objset_is_receiving(os
))
2045 if (tx
->tx_txg
<= os
->os_spa
->spa_claim_max_txg
)
2048 /* Allocate the user/group/project used objects if necessary. */
2049 if (DMU_USERUSED_DNODE(os
)->dn_type
== DMU_OT_NONE
) {
2050 VERIFY0(zap_create_claim(os
,
2051 DMU_USERUSED_OBJECT
,
2052 DMU_OT_USERGROUP_USED
, DMU_OT_NONE
, 0, tx
));
2053 VERIFY0(zap_create_claim(os
,
2054 DMU_GROUPUSED_OBJECT
,
2055 DMU_OT_USERGROUP_USED
, DMU_OT_NONE
, 0, tx
));
2058 if (dmu_objset_projectquota_enabled(os
) &&
2059 DMU_PROJECTUSED_DNODE(os
)->dn_type
== DMU_OT_NONE
) {
2060 VERIFY0(zap_create_claim(os
, DMU_PROJECTUSED_OBJECT
,
2061 DMU_OT_USERGROUP_USED
, DMU_OT_NONE
, 0, tx
));
2065 i
< multilist_get_num_sublists(os
->os_synced_dnodes
); i
++) {
2066 userquota_updates_arg_t
*uua
=
2067 kmem_alloc(sizeof (*uua
), KM_SLEEP
);
2069 uua
->uua_sublist_idx
= i
;
2071 /* note: caller does taskq_wait() */
2072 (void) taskq_dispatch(dmu_objset_pool(os
)->dp_sync_taskq
,
2073 userquota_updates_task
, uua
, 0);
2074 /* callback frees uua */
2079 * Returns a pointer to data to find uid/gid from
2081 * If a dirty record for transaction group that is syncing can't
2082 * be found then NULL is returned. In the NULL case it is assumed
2083 * the uid/gid aren't changing.
2086 dmu_objset_userquota_find_data(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
2088 dbuf_dirty_record_t
*dr
, **drp
;
2091 if (db
->db_dirtycnt
== 0)
2092 return (db
->db
.db_data
); /* Nothing is changing */
2094 for (drp
= &db
->db_last_dirty
; (dr
= *drp
) != NULL
; drp
= &dr
->dr_next
)
2095 if (dr
->dr_txg
== tx
->tx_txg
)
2103 DB_DNODE_ENTER(dr
->dr_dbuf
);
2104 dn
= DB_DNODE(dr
->dr_dbuf
);
2106 if (dn
->dn_bonuslen
== 0 &&
2107 dr
->dr_dbuf
->db_blkid
== DMU_SPILL_BLKID
)
2108 data
= dr
->dt
.dl
.dr_data
->b_data
;
2110 data
= dr
->dt
.dl
.dr_data
;
2112 DB_DNODE_EXIT(dr
->dr_dbuf
);
2119 dmu_objset_userquota_get_ids(dnode_t
*dn
, boolean_t before
, dmu_tx_t
*tx
)
2121 objset_t
*os
= dn
->dn_objset
;
2123 dmu_buf_impl_t
*db
= NULL
;
2124 uint64_t *user
= NULL
;
2125 uint64_t *group
= NULL
;
2126 uint64_t *project
= NULL
;
2127 int flags
= dn
->dn_id_flags
;
2129 boolean_t have_spill
= B_FALSE
;
2131 if (!dmu_objset_userused_enabled(dn
->dn_objset
))
2135 * Raw receives introduce a problem with user accounting. Raw
2136 * receives cannot update the user accounting info because the
2137 * user ids and the sizes are encrypted. To guarantee that we
2138 * never end up with bad user accounting, we simply disable it
2139 * during raw receives. We also disable this for normal receives
2140 * so that an incremental raw receive may be done on top of an
2141 * existing non-raw receive.
2143 if (os
->os_encrypted
&& dmu_objset_is_receiving(os
))
2146 if (before
&& (flags
& (DN_ID_CHKED_BONUS
|DN_ID_OLD_EXIST
|
2147 DN_ID_CHKED_SPILL
)))
2150 if (before
&& dn
->dn_bonuslen
!= 0)
2151 data
= DN_BONUS(dn
->dn_phys
);
2152 else if (!before
&& dn
->dn_bonuslen
!= 0) {
2155 mutex_enter(&db
->db_mtx
);
2156 data
= dmu_objset_userquota_find_data(db
, tx
);
2158 data
= DN_BONUS(dn
->dn_phys
);
2160 } else if (dn
->dn_bonuslen
== 0 && dn
->dn_bonustype
== DMU_OT_SA
) {
2163 if (RW_WRITE_HELD(&dn
->dn_struct_rwlock
))
2164 rf
|= DB_RF_HAVESTRUCT
;
2165 error
= dmu_spill_hold_by_dnode(dn
,
2166 rf
| DB_RF_MUST_SUCCEED
,
2167 FTAG
, (dmu_buf_t
**)&db
);
2169 mutex_enter(&db
->db_mtx
);
2170 data
= (before
) ? db
->db
.db_data
:
2171 dmu_objset_userquota_find_data(db
, tx
);
2172 have_spill
= B_TRUE
;
2174 mutex_enter(&dn
->dn_mtx
);
2175 dn
->dn_id_flags
|= DN_ID_CHKED_BONUS
;
2176 mutex_exit(&dn
->dn_mtx
);
2182 user
= &dn
->dn_olduid
;
2183 group
= &dn
->dn_oldgid
;
2184 project
= &dn
->dn_oldprojid
;
2186 user
= &dn
->dn_newuid
;
2187 group
= &dn
->dn_newgid
;
2188 project
= &dn
->dn_newprojid
;
2192 * Must always call the callback in case the object
2193 * type has changed and that type isn't an object type to track
2195 error
= used_cbs
[os
->os_phys
->os_type
](dn
->dn_bonustype
, data
,
2196 user
, group
, project
);
2199 * Preserve existing uid/gid when the callback can't determine
2200 * what the new uid/gid are and the callback returned EEXIST.
2201 * The EEXIST error tells us to just use the existing uid/gid.
2202 * If we don't know what the old values are then just assign
2203 * them to 0, since that is a new file being created.
2205 if (!before
&& data
== NULL
&& error
== EEXIST
) {
2206 if (flags
& DN_ID_OLD_EXIST
) {
2207 dn
->dn_newuid
= dn
->dn_olduid
;
2208 dn
->dn_newgid
= dn
->dn_oldgid
;
2209 dn
->dn_newprojid
= dn
->dn_oldprojid
;
2213 dn
->dn_newprojid
= ZFS_DEFAULT_PROJID
;
2219 mutex_exit(&db
->db_mtx
);
2221 mutex_enter(&dn
->dn_mtx
);
2222 if (error
== 0 && before
)
2223 dn
->dn_id_flags
|= DN_ID_OLD_EXIST
;
2224 if (error
== 0 && !before
)
2225 dn
->dn_id_flags
|= DN_ID_NEW_EXIST
;
2228 dn
->dn_id_flags
|= DN_ID_CHKED_SPILL
;
2230 dn
->dn_id_flags
|= DN_ID_CHKED_BONUS
;
2232 mutex_exit(&dn
->dn_mtx
);
2234 dmu_buf_rele((dmu_buf_t
*)db
, FTAG
);
2238 dmu_objset_userspace_present(objset_t
*os
)
2240 return (os
->os_phys
->os_flags
&
2241 OBJSET_FLAG_USERACCOUNTING_COMPLETE
);
2245 dmu_objset_userobjspace_present(objset_t
*os
)
2247 return (os
->os_phys
->os_flags
&
2248 OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE
);
2252 dmu_objset_projectquota_present(objset_t
*os
)
2254 return (os
->os_phys
->os_flags
&
2255 OBJSET_FLAG_PROJECTQUOTA_COMPLETE
);
2259 dmu_objset_space_upgrade(objset_t
*os
)
2265 * We simply need to mark every object dirty, so that it will be
2266 * synced out and now accounted. If this is called
2267 * concurrently, or if we already did some work before crashing,
2268 * that's fine, since we track each object's accounted state
2272 for (obj
= 0; err
== 0; err
= dmu_object_next(os
, &obj
, FALSE
, 0)) {
2277 mutex_enter(&os
->os_upgrade_lock
);
2278 if (os
->os_upgrade_exit
)
2279 err
= SET_ERROR(EINTR
);
2280 mutex_exit(&os
->os_upgrade_lock
);
2284 if (issig(JUSTLOOKING
) && issig(FORREAL
))
2285 return (SET_ERROR(EINTR
));
2287 objerr
= dmu_bonus_hold(os
, obj
, FTAG
, &db
);
2290 tx
= dmu_tx_create(os
);
2291 dmu_tx_hold_bonus(tx
, obj
);
2292 objerr
= dmu_tx_assign(tx
, TXG_WAIT
);
2294 dmu_buf_rele(db
, FTAG
);
2298 dmu_buf_will_dirty(db
, tx
);
2299 dmu_buf_rele(db
, FTAG
);
2306 dmu_objset_userspace_upgrade(objset_t
*os
)
2310 if (dmu_objset_userspace_present(os
))
2312 if (dmu_objset_is_snapshot(os
))
2313 return (SET_ERROR(EINVAL
));
2314 if (!dmu_objset_userused_enabled(os
))
2315 return (SET_ERROR(ENOTSUP
));
2317 err
= dmu_objset_space_upgrade(os
);
2321 os
->os_flags
|= OBJSET_FLAG_USERACCOUNTING_COMPLETE
;
2322 txg_wait_synced(dmu_objset_pool(os
), 0);
2327 dmu_objset_id_quota_upgrade_cb(objset_t
*os
)
2331 if (dmu_objset_userobjspace_present(os
) &&
2332 dmu_objset_projectquota_present(os
))
2334 if (dmu_objset_is_snapshot(os
))
2335 return (SET_ERROR(EINVAL
));
2336 if (!dmu_objset_userobjused_enabled(os
))
2337 return (SET_ERROR(ENOTSUP
));
2338 if (!dmu_objset_projectquota_enabled(os
) &&
2339 dmu_objset_userobjspace_present(os
))
2340 return (SET_ERROR(ENOTSUP
));
2342 dmu_objset_ds(os
)->ds_feature_activation_needed
[
2343 SPA_FEATURE_USEROBJ_ACCOUNTING
] = B_TRUE
;
2344 if (dmu_objset_projectquota_enabled(os
))
2345 dmu_objset_ds(os
)->ds_feature_activation_needed
[
2346 SPA_FEATURE_PROJECT_QUOTA
] = B_TRUE
;
2348 err
= dmu_objset_space_upgrade(os
);
2352 os
->os_flags
|= OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE
;
2353 if (dmu_objset_projectquota_enabled(os
))
2354 os
->os_flags
|= OBJSET_FLAG_PROJECTQUOTA_COMPLETE
;
2356 txg_wait_synced(dmu_objset_pool(os
), 0);
2361 dmu_objset_id_quota_upgrade(objset_t
*os
)
2363 dmu_objset_upgrade(os
, dmu_objset_id_quota_upgrade_cb
);
2367 dmu_objset_userobjspace_upgradable(objset_t
*os
)
2369 return (dmu_objset_type(os
) == DMU_OST_ZFS
&&
2370 !dmu_objset_is_snapshot(os
) &&
2371 dmu_objset_userobjused_enabled(os
) &&
2372 !dmu_objset_userobjspace_present(os
));
2376 dmu_objset_projectquota_upgradable(objset_t
*os
)
2378 return (dmu_objset_type(os
) == DMU_OST_ZFS
&&
2379 !dmu_objset_is_snapshot(os
) &&
2380 dmu_objset_projectquota_enabled(os
) &&
2381 !dmu_objset_projectquota_present(os
));
2385 dmu_objset_space(objset_t
*os
, uint64_t *refdbytesp
, uint64_t *availbytesp
,
2386 uint64_t *usedobjsp
, uint64_t *availobjsp
)
2388 dsl_dataset_space(os
->os_dsl_dataset
, refdbytesp
, availbytesp
,
2389 usedobjsp
, availobjsp
);
2393 dmu_objset_fsid_guid(objset_t
*os
)
2395 return (dsl_dataset_fsid_guid(os
->os_dsl_dataset
));
2399 dmu_objset_fast_stat(objset_t
*os
, dmu_objset_stats_t
*stat
)
2401 stat
->dds_type
= os
->os_phys
->os_type
;
2402 if (os
->os_dsl_dataset
)
2403 dsl_dataset_fast_stat(os
->os_dsl_dataset
, stat
);
2407 dmu_objset_stats(objset_t
*os
, nvlist_t
*nv
)
2409 ASSERT(os
->os_dsl_dataset
||
2410 os
->os_phys
->os_type
== DMU_OST_META
);
2412 if (os
->os_dsl_dataset
!= NULL
)
2413 dsl_dataset_stats(os
->os_dsl_dataset
, nv
);
2415 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_TYPE
,
2416 os
->os_phys
->os_type
);
2417 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_USERACCOUNTING
,
2418 dmu_objset_userspace_present(os
));
2422 dmu_objset_is_snapshot(objset_t
*os
)
2424 if (os
->os_dsl_dataset
!= NULL
)
2425 return (os
->os_dsl_dataset
->ds_is_snapshot
);
2431 dmu_snapshot_realname(objset_t
*os
, char *name
, char *real
, int maxlen
,
2432 boolean_t
*conflict
)
2434 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
2437 if (dsl_dataset_phys(ds
)->ds_snapnames_zapobj
== 0)
2438 return (SET_ERROR(ENOENT
));
2440 return (zap_lookup_norm(ds
->ds_dir
->dd_pool
->dp_meta_objset
,
2441 dsl_dataset_phys(ds
)->ds_snapnames_zapobj
, name
, 8, 1, &ignored
,
2442 MT_NORMALIZE
, real
, maxlen
, conflict
));
2446 dmu_snapshot_list_next(objset_t
*os
, int namelen
, char *name
,
2447 uint64_t *idp
, uint64_t *offp
, boolean_t
*case_conflict
)
2449 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
2450 zap_cursor_t cursor
;
2451 zap_attribute_t attr
;
2453 ASSERT(dsl_pool_config_held(dmu_objset_pool(os
)));
2455 if (dsl_dataset_phys(ds
)->ds_snapnames_zapobj
== 0)
2456 return (SET_ERROR(ENOENT
));
2458 zap_cursor_init_serialized(&cursor
,
2459 ds
->ds_dir
->dd_pool
->dp_meta_objset
,
2460 dsl_dataset_phys(ds
)->ds_snapnames_zapobj
, *offp
);
2462 if (zap_cursor_retrieve(&cursor
, &attr
) != 0) {
2463 zap_cursor_fini(&cursor
);
2464 return (SET_ERROR(ENOENT
));
2467 if (strlen(attr
.za_name
) + 1 > namelen
) {
2468 zap_cursor_fini(&cursor
);
2469 return (SET_ERROR(ENAMETOOLONG
));
2472 (void) strcpy(name
, attr
.za_name
);
2474 *idp
= attr
.za_first_integer
;
2476 *case_conflict
= attr
.za_normalization_conflict
;
2477 zap_cursor_advance(&cursor
);
2478 *offp
= zap_cursor_serialize(&cursor
);
2479 zap_cursor_fini(&cursor
);
2485 dmu_snapshot_lookup(objset_t
*os
, const char *name
, uint64_t *value
)
2487 return (dsl_dataset_snap_lookup(os
->os_dsl_dataset
, name
, value
));
2491 dmu_dir_list_next(objset_t
*os
, int namelen
, char *name
,
2492 uint64_t *idp
, uint64_t *offp
)
2494 dsl_dir_t
*dd
= os
->os_dsl_dataset
->ds_dir
;
2495 zap_cursor_t cursor
;
2496 zap_attribute_t attr
;
2498 /* there is no next dir on a snapshot! */
2499 if (os
->os_dsl_dataset
->ds_object
!=
2500 dsl_dir_phys(dd
)->dd_head_dataset_obj
)
2501 return (SET_ERROR(ENOENT
));
2503 zap_cursor_init_serialized(&cursor
,
2504 dd
->dd_pool
->dp_meta_objset
,
2505 dsl_dir_phys(dd
)->dd_child_dir_zapobj
, *offp
);
2507 if (zap_cursor_retrieve(&cursor
, &attr
) != 0) {
2508 zap_cursor_fini(&cursor
);
2509 return (SET_ERROR(ENOENT
));
2512 if (strlen(attr
.za_name
) + 1 > namelen
) {
2513 zap_cursor_fini(&cursor
);
2514 return (SET_ERROR(ENAMETOOLONG
));
2517 (void) strcpy(name
, attr
.za_name
);
2519 *idp
= attr
.za_first_integer
;
2520 zap_cursor_advance(&cursor
);
2521 *offp
= zap_cursor_serialize(&cursor
);
2522 zap_cursor_fini(&cursor
);
2527 typedef struct dmu_objset_find_ctx
{
2531 char *dc_ddname
; /* last component of ddobj's name */
2532 int (*dc_func
)(dsl_pool_t
*, dsl_dataset_t
*, void *);
2535 kmutex_t
*dc_error_lock
;
2537 } dmu_objset_find_ctx_t
;
2540 dmu_objset_find_dp_impl(dmu_objset_find_ctx_t
*dcp
)
2542 dsl_pool_t
*dp
= dcp
->dc_dp
;
2546 zap_attribute_t
*attr
;
2550 /* don't process if there already was an error */
2551 if (*dcp
->dc_error
!= 0)
2555 * Note: passing the name (dc_ddname) here is optional, but it
2556 * improves performance because we don't need to call
2557 * zap_value_search() to determine the name.
2559 err
= dsl_dir_hold_obj(dp
, dcp
->dc_ddobj
, dcp
->dc_ddname
, FTAG
, &dd
);
2563 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
2564 if (dd
->dd_myname
[0] == '$') {
2565 dsl_dir_rele(dd
, FTAG
);
2569 thisobj
= dsl_dir_phys(dd
)->dd_head_dataset_obj
;
2570 attr
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
2573 * Iterate over all children.
2575 if (dcp
->dc_flags
& DS_FIND_CHILDREN
) {
2576 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
,
2577 dsl_dir_phys(dd
)->dd_child_dir_zapobj
);
2578 zap_cursor_retrieve(&zc
, attr
) == 0;
2579 (void) zap_cursor_advance(&zc
)) {
2580 ASSERT3U(attr
->za_integer_length
, ==,
2582 ASSERT3U(attr
->za_num_integers
, ==, 1);
2584 dmu_objset_find_ctx_t
*child_dcp
=
2585 kmem_alloc(sizeof (*child_dcp
), KM_SLEEP
);
2587 child_dcp
->dc_ddobj
= attr
->za_first_integer
;
2588 child_dcp
->dc_ddname
= spa_strdup(attr
->za_name
);
2589 if (dcp
->dc_tq
!= NULL
)
2590 (void) taskq_dispatch(dcp
->dc_tq
,
2591 dmu_objset_find_dp_cb
, child_dcp
, TQ_SLEEP
);
2593 dmu_objset_find_dp_impl(child_dcp
);
2595 zap_cursor_fini(&zc
);
2599 * Iterate over all snapshots.
2601 if (dcp
->dc_flags
& DS_FIND_SNAPSHOTS
) {
2603 err
= dsl_dataset_hold_obj(dp
, thisobj
, FTAG
, &ds
);
2608 snapobj
= dsl_dataset_phys(ds
)->ds_snapnames_zapobj
;
2609 dsl_dataset_rele(ds
, FTAG
);
2611 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
, snapobj
);
2612 zap_cursor_retrieve(&zc
, attr
) == 0;
2613 (void) zap_cursor_advance(&zc
)) {
2614 ASSERT3U(attr
->za_integer_length
, ==,
2616 ASSERT3U(attr
->za_num_integers
, ==, 1);
2618 err
= dsl_dataset_hold_obj(dp
,
2619 attr
->za_first_integer
, FTAG
, &ds
);
2622 err
= dcp
->dc_func(dp
, ds
, dcp
->dc_arg
);
2623 dsl_dataset_rele(ds
, FTAG
);
2627 zap_cursor_fini(&zc
);
2631 kmem_free(attr
, sizeof (zap_attribute_t
));
2634 dsl_dir_rele(dd
, FTAG
);
2641 err
= dsl_dataset_hold_obj(dp
, thisobj
, FTAG
, &ds
);
2644 * Note: we hold the dir while calling dsl_dataset_hold_obj() so
2645 * that the dir will remain cached, and we won't have to re-instantiate
2646 * it (which could be expensive due to finding its name via
2647 * zap_value_search()).
2649 dsl_dir_rele(dd
, FTAG
);
2652 err
= dcp
->dc_func(dp
, ds
, dcp
->dc_arg
);
2653 dsl_dataset_rele(ds
, FTAG
);
2657 mutex_enter(dcp
->dc_error_lock
);
2658 /* only keep first error */
2659 if (*dcp
->dc_error
== 0)
2660 *dcp
->dc_error
= err
;
2661 mutex_exit(dcp
->dc_error_lock
);
2664 if (dcp
->dc_ddname
!= NULL
)
2665 spa_strfree(dcp
->dc_ddname
);
2666 kmem_free(dcp
, sizeof (*dcp
));
2670 dmu_objset_find_dp_cb(void *arg
)
2672 dmu_objset_find_ctx_t
*dcp
= arg
;
2673 dsl_pool_t
*dp
= dcp
->dc_dp
;
2676 * We need to get a pool_config_lock here, as there are several
2677 * asssert(pool_config_held) down the stack. Getting a lock via
2678 * dsl_pool_config_enter is risky, as it might be stalled by a
2679 * pending writer. This would deadlock, as the write lock can
2680 * only be granted when our parent thread gives up the lock.
2681 * The _prio interface gives us priority over a pending writer.
2683 dsl_pool_config_enter_prio(dp
, FTAG
);
2685 dmu_objset_find_dp_impl(dcp
);
2687 dsl_pool_config_exit(dp
, FTAG
);
2691 * Find objsets under and including ddobj, call func(ds) on each.
2692 * The order for the enumeration is completely undefined.
2693 * func is called with dsl_pool_config held.
2696 dmu_objset_find_dp(dsl_pool_t
*dp
, uint64_t ddobj
,
2697 int func(dsl_pool_t
*, dsl_dataset_t
*, void *), void *arg
, int flags
)
2702 dmu_objset_find_ctx_t
*dcp
;
2705 mutex_init(&err_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
2706 dcp
= kmem_alloc(sizeof (*dcp
), KM_SLEEP
);
2709 dcp
->dc_ddobj
= ddobj
;
2710 dcp
->dc_ddname
= NULL
;
2711 dcp
->dc_func
= func
;
2713 dcp
->dc_flags
= flags
;
2714 dcp
->dc_error_lock
= &err_lock
;
2715 dcp
->dc_error
= &error
;
2717 if ((flags
& DS_FIND_SERIALIZE
) || dsl_pool_config_held_writer(dp
)) {
2719 * In case a write lock is held we can't make use of
2720 * parallelism, as down the stack of the worker threads
2721 * the lock is asserted via dsl_pool_config_held.
2722 * In case of a read lock this is solved by getting a read
2723 * lock in each worker thread, which isn't possible in case
2724 * of a writer lock. So we fall back to the synchronous path
2726 * In the future it might be possible to get some magic into
2727 * dsl_pool_config_held in a way that it returns true for
2728 * the worker threads so that a single lock held from this
2729 * thread suffices. For now, stay single threaded.
2731 dmu_objset_find_dp_impl(dcp
);
2732 mutex_destroy(&err_lock
);
2737 ntasks
= dmu_find_threads
;
2739 ntasks
= vdev_count_leaves(dp
->dp_spa
) * 4;
2740 tq
= taskq_create("dmu_objset_find", ntasks
, maxclsyspri
, ntasks
,
2743 kmem_free(dcp
, sizeof (*dcp
));
2744 mutex_destroy(&err_lock
);
2746 return (SET_ERROR(ENOMEM
));
2750 /* dcp will be freed by task */
2751 (void) taskq_dispatch(tq
, dmu_objset_find_dp_cb
, dcp
, TQ_SLEEP
);
2754 * PORTING: this code relies on the property of taskq_wait to wait
2755 * until no more tasks are queued and no more tasks are active. As
2756 * we always queue new tasks from within other tasks, task_wait
2757 * reliably waits for the full recursion to finish, even though we
2758 * enqueue new tasks after taskq_wait has been called.
2759 * On platforms other than illumos, taskq_wait may not have this
2764 mutex_destroy(&err_lock
);
2770 * Find all objsets under name, and for each, call 'func(child_name, arg)'.
2771 * The dp_config_rwlock must not be held when this is called, and it
2772 * will not be held when the callback is called.
2773 * Therefore this function should only be used when the pool is not changing
2774 * (e.g. in syncing context), or the callback can deal with the possible races.
2777 dmu_objset_find_impl(spa_t
*spa
, const char *name
,
2778 int func(const char *, void *), void *arg
, int flags
)
2781 dsl_pool_t
*dp
= spa_get_dsl(spa
);
2784 zap_attribute_t
*attr
;
2789 dsl_pool_config_enter(dp
, FTAG
);
2791 err
= dsl_dir_hold(dp
, name
, FTAG
, &dd
, NULL
);
2793 dsl_pool_config_exit(dp
, FTAG
);
2797 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
2798 if (dd
->dd_myname
[0] == '$') {
2799 dsl_dir_rele(dd
, FTAG
);
2800 dsl_pool_config_exit(dp
, FTAG
);
2804 thisobj
= dsl_dir_phys(dd
)->dd_head_dataset_obj
;
2805 attr
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
2808 * Iterate over all children.
2810 if (flags
& DS_FIND_CHILDREN
) {
2811 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
,
2812 dsl_dir_phys(dd
)->dd_child_dir_zapobj
);
2813 zap_cursor_retrieve(&zc
, attr
) == 0;
2814 (void) zap_cursor_advance(&zc
)) {
2815 ASSERT3U(attr
->za_integer_length
, ==,
2817 ASSERT3U(attr
->za_num_integers
, ==, 1);
2819 child
= kmem_asprintf("%s/%s", name
, attr
->za_name
);
2820 dsl_pool_config_exit(dp
, FTAG
);
2821 err
= dmu_objset_find_impl(spa
, child
,
2823 dsl_pool_config_enter(dp
, FTAG
);
2828 zap_cursor_fini(&zc
);
2831 dsl_dir_rele(dd
, FTAG
);
2832 dsl_pool_config_exit(dp
, FTAG
);
2833 kmem_free(attr
, sizeof (zap_attribute_t
));
2839 * Iterate over all snapshots.
2841 if (flags
& DS_FIND_SNAPSHOTS
) {
2842 err
= dsl_dataset_hold_obj(dp
, thisobj
, FTAG
, &ds
);
2847 snapobj
= dsl_dataset_phys(ds
)->ds_snapnames_zapobj
;
2848 dsl_dataset_rele(ds
, FTAG
);
2850 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
, snapobj
);
2851 zap_cursor_retrieve(&zc
, attr
) == 0;
2852 (void) zap_cursor_advance(&zc
)) {
2853 ASSERT3U(attr
->za_integer_length
, ==,
2855 ASSERT3U(attr
->za_num_integers
, ==, 1);
2857 child
= kmem_asprintf("%s@%s",
2858 name
, attr
->za_name
);
2859 dsl_pool_config_exit(dp
, FTAG
);
2860 err
= func(child
, arg
);
2861 dsl_pool_config_enter(dp
, FTAG
);
2866 zap_cursor_fini(&zc
);
2870 dsl_dir_rele(dd
, FTAG
);
2871 kmem_free(attr
, sizeof (zap_attribute_t
));
2872 dsl_pool_config_exit(dp
, FTAG
);
2877 /* Apply to self. */
2878 return (func(name
, arg
));
2882 * See comment above dmu_objset_find_impl().
2885 dmu_objset_find(char *name
, int func(const char *, void *), void *arg
,
2891 error
= spa_open(name
, &spa
, FTAG
);
2894 error
= dmu_objset_find_impl(spa
, name
, func
, arg
, flags
);
2895 spa_close(spa
, FTAG
);
2900 dmu_objset_incompatible_encryption_version(objset_t
*os
)
2902 return (dsl_dir_incompatible_encryption_version(
2903 os
->os_dsl_dataset
->ds_dir
));
2907 dmu_objset_set_user(objset_t
*os
, void *user_ptr
)
2909 ASSERT(MUTEX_HELD(&os
->os_user_ptr_lock
));
2910 os
->os_user_ptr
= user_ptr
;
2914 dmu_objset_get_user(objset_t
*os
)
2916 ASSERT(MUTEX_HELD(&os
->os_user_ptr_lock
));
2917 return (os
->os_user_ptr
);
2921 * Determine name of filesystem, given name of snapshot.
2922 * buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes
2925 dmu_fsname(const char *snapname
, char *buf
)
2927 char *atp
= strchr(snapname
, '@');
2929 return (SET_ERROR(EINVAL
));
2930 if (atp
- snapname
>= ZFS_MAX_DATASET_NAME_LEN
)
2931 return (SET_ERROR(ENAMETOOLONG
));
2932 (void) strlcpy(buf
, snapname
, atp
- snapname
+ 1);
2937 * Call when we think we're going to write/free space in open context to track
2938 * the amount of dirty data in the open txg, which is also the amount
2939 * of memory that can not be evicted until this txg syncs.
2942 dmu_objset_willuse_space(objset_t
*os
, int64_t space
, dmu_tx_t
*tx
)
2944 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
2945 int64_t aspace
= spa_get_worst_case_asize(os
->os_spa
, space
);
2948 dsl_dir_willuse_space(ds
->ds_dir
, aspace
, tx
);
2949 dsl_pool_dirty_space(dmu_tx_pool(tx
), space
, tx
);
2953 #if defined(_KERNEL)
2954 EXPORT_SYMBOL(dmu_objset_zil
);
2955 EXPORT_SYMBOL(dmu_objset_pool
);
2956 EXPORT_SYMBOL(dmu_objset_ds
);
2957 EXPORT_SYMBOL(dmu_objset_type
);
2958 EXPORT_SYMBOL(dmu_objset_name
);
2959 EXPORT_SYMBOL(dmu_objset_hold
);
2960 EXPORT_SYMBOL(dmu_objset_hold_flags
);
2961 EXPORT_SYMBOL(dmu_objset_own
);
2962 EXPORT_SYMBOL(dmu_objset_rele
);
2963 EXPORT_SYMBOL(dmu_objset_rele_flags
);
2964 EXPORT_SYMBOL(dmu_objset_disown
);
2965 EXPORT_SYMBOL(dmu_objset_from_ds
);
2966 EXPORT_SYMBOL(dmu_objset_create
);
2967 EXPORT_SYMBOL(dmu_objset_clone
);
2968 EXPORT_SYMBOL(dmu_objset_stats
);
2969 EXPORT_SYMBOL(dmu_objset_fast_stat
);
2970 EXPORT_SYMBOL(dmu_objset_spa
);
2971 EXPORT_SYMBOL(dmu_objset_space
);
2972 EXPORT_SYMBOL(dmu_objset_fsid_guid
);
2973 EXPORT_SYMBOL(dmu_objset_find
);
2974 EXPORT_SYMBOL(dmu_objset_byteswap
);
2975 EXPORT_SYMBOL(dmu_objset_evict_dbufs
);
2976 EXPORT_SYMBOL(dmu_objset_snap_cmtime
);
2977 EXPORT_SYMBOL(dmu_objset_dnodesize
);
2979 EXPORT_SYMBOL(dmu_objset_sync
);
2980 EXPORT_SYMBOL(dmu_objset_is_dirty
);
2981 EXPORT_SYMBOL(dmu_objset_create_impl_dnstats
);
2982 EXPORT_SYMBOL(dmu_objset_create_impl
);
2983 EXPORT_SYMBOL(dmu_objset_open_impl
);
2984 EXPORT_SYMBOL(dmu_objset_evict
);
2985 EXPORT_SYMBOL(dmu_objset_register_type
);
2986 EXPORT_SYMBOL(dmu_objset_do_userquota_updates
);
2987 EXPORT_SYMBOL(dmu_objset_userquota_get_ids
);
2988 EXPORT_SYMBOL(dmu_objset_userused_enabled
);
2989 EXPORT_SYMBOL(dmu_objset_userspace_upgrade
);
2990 EXPORT_SYMBOL(dmu_objset_userspace_present
);
2991 EXPORT_SYMBOL(dmu_objset_userobjused_enabled
);
2992 EXPORT_SYMBOL(dmu_objset_userobjspace_upgradable
);
2993 EXPORT_SYMBOL(dmu_objset_userobjspace_present
);
2994 EXPORT_SYMBOL(dmu_objset_projectquota_enabled
);
2995 EXPORT_SYMBOL(dmu_objset_projectquota_present
);
2996 EXPORT_SYMBOL(dmu_objset_projectquota_upgradable
);
2997 EXPORT_SYMBOL(dmu_objset_id_quota_upgrade
);