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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
24 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
25 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
26 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27 * Copyright (c) 2015 Nexenta Systems, Inc. All rights reserved.
28 * Copyright (c) 2015, STRATO AG, Inc. All rights reserved.
29 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
32 /* Portions Copyright 2010 Robert Milkowski */
34 #include <sys/zfeature.h>
36 #include <sys/zfs_context.h>
37 #include <sys/dmu_objset.h>
38 #include <sys/dsl_dir.h>
39 #include <sys/dsl_dataset.h>
40 #include <sys/dsl_prop.h>
41 #include <sys/dsl_pool.h>
42 #include <sys/dsl_synctask.h>
43 #include <sys/dsl_deleg.h>
44 #include <sys/dnode.h>
47 #include <sys/dmu_tx.h>
50 #include <sys/dmu_impl.h>
51 #include <sys/zfs_ioctl.h>
53 #include <sys/zfs_onexit.h>
54 #include <sys/dsl_destroy.h>
56 #include <sys/policy.h>
57 #include <sys/spa_impl.h>
60 * Needed to close a window in dnode_move() that allows the objset to be freed
61 * before it can be safely accessed.
66 * Tunable to overwrite the maximum number of threads for the parallization
67 * of dmu_objset_find_dp, needed to speed up the import of pools with many
69 * Default is 4 times the number of leaf vdevs.
71 int dmu_find_threads
= 0;
74 * Backfill lower metadnode objects after this many have been freed.
75 * Backfilling negatively impacts object creation rates, so only do it
76 * if there are enough holes to fill.
78 int dmu_rescan_dnode_threshold
= 1 << DN_MAX_INDBLKSHIFT
;
80 static void dmu_objset_find_dp_cb(void *arg
);
82 static void dmu_objset_upgrade(objset_t
*os
, dmu_objset_upgrade_cb_t cb
);
83 static void dmu_objset_upgrade_stop(objset_t
*os
);
88 rw_init(&os_lock
, NULL
, RW_DEFAULT
, NULL
);
98 dmu_objset_spa(objset_t
*os
)
104 dmu_objset_zil(objset_t
*os
)
110 dmu_objset_pool(objset_t
*os
)
114 if ((ds
= os
->os_dsl_dataset
) != NULL
&& ds
->ds_dir
)
115 return (ds
->ds_dir
->dd_pool
);
117 return (spa_get_dsl(os
->os_spa
));
121 dmu_objset_ds(objset_t
*os
)
123 return (os
->os_dsl_dataset
);
127 dmu_objset_type(objset_t
*os
)
129 return (os
->os_phys
->os_type
);
133 dmu_objset_name(objset_t
*os
, char *buf
)
135 dsl_dataset_name(os
->os_dsl_dataset
, buf
);
139 dmu_objset_id(objset_t
*os
)
141 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
143 return (ds
? ds
->ds_object
: 0);
147 dmu_objset_dnodesize(objset_t
*os
)
149 return (os
->os_dnodesize
);
153 dmu_objset_syncprop(objset_t
*os
)
155 return (os
->os_sync
);
159 dmu_objset_logbias(objset_t
*os
)
161 return (os
->os_logbias
);
165 checksum_changed_cb(void *arg
, uint64_t newval
)
170 * Inheritance should have been done by now.
172 ASSERT(newval
!= ZIO_CHECKSUM_INHERIT
);
174 os
->os_checksum
= zio_checksum_select(newval
, ZIO_CHECKSUM_ON_VALUE
);
178 compression_changed_cb(void *arg
, uint64_t newval
)
183 * Inheritance and range checking should have been done by now.
185 ASSERT(newval
!= ZIO_COMPRESS_INHERIT
);
187 os
->os_compress
= zio_compress_select(os
->os_spa
, newval
,
192 copies_changed_cb(void *arg
, uint64_t newval
)
197 * Inheritance and range checking should have been done by now.
200 ASSERT(newval
<= spa_max_replication(os
->os_spa
));
202 os
->os_copies
= newval
;
206 dedup_changed_cb(void *arg
, uint64_t newval
)
209 spa_t
*spa
= os
->os_spa
;
210 enum zio_checksum checksum
;
213 * Inheritance should have been done by now.
215 ASSERT(newval
!= ZIO_CHECKSUM_INHERIT
);
217 checksum
= zio_checksum_dedup_select(spa
, newval
, ZIO_CHECKSUM_OFF
);
219 os
->os_dedup_checksum
= checksum
& ZIO_CHECKSUM_MASK
;
220 os
->os_dedup_verify
= !!(checksum
& ZIO_CHECKSUM_VERIFY
);
224 primary_cache_changed_cb(void *arg
, uint64_t newval
)
229 * Inheritance and range checking should have been done by now.
231 ASSERT(newval
== ZFS_CACHE_ALL
|| newval
== ZFS_CACHE_NONE
||
232 newval
== ZFS_CACHE_METADATA
);
234 os
->os_primary_cache
= newval
;
238 secondary_cache_changed_cb(void *arg
, uint64_t newval
)
243 * Inheritance and range checking should have been done by now.
245 ASSERT(newval
== ZFS_CACHE_ALL
|| newval
== ZFS_CACHE_NONE
||
246 newval
== ZFS_CACHE_METADATA
);
248 os
->os_secondary_cache
= newval
;
252 sync_changed_cb(void *arg
, uint64_t newval
)
257 * Inheritance and range checking should have been done by now.
259 ASSERT(newval
== ZFS_SYNC_STANDARD
|| newval
== ZFS_SYNC_ALWAYS
||
260 newval
== ZFS_SYNC_DISABLED
);
262 os
->os_sync
= newval
;
264 zil_set_sync(os
->os_zil
, newval
);
268 redundant_metadata_changed_cb(void *arg
, uint64_t newval
)
273 * Inheritance and range checking should have been done by now.
275 ASSERT(newval
== ZFS_REDUNDANT_METADATA_ALL
||
276 newval
== ZFS_REDUNDANT_METADATA_MOST
);
278 os
->os_redundant_metadata
= newval
;
282 dnodesize_changed_cb(void *arg
, uint64_t newval
)
287 case ZFS_DNSIZE_LEGACY
:
288 os
->os_dnodesize
= DNODE_MIN_SIZE
;
290 case ZFS_DNSIZE_AUTO
:
292 * Choose a dnode size that will work well for most
293 * workloads if the user specified "auto". Future code
294 * improvements could dynamically select a dnode size
295 * based on observed workload patterns.
297 os
->os_dnodesize
= DNODE_MIN_SIZE
* 2;
304 os
->os_dnodesize
= newval
;
310 logbias_changed_cb(void *arg
, uint64_t newval
)
314 ASSERT(newval
== ZFS_LOGBIAS_LATENCY
||
315 newval
== ZFS_LOGBIAS_THROUGHPUT
);
316 os
->os_logbias
= newval
;
318 zil_set_logbias(os
->os_zil
, newval
);
322 recordsize_changed_cb(void *arg
, uint64_t newval
)
326 os
->os_recordsize
= newval
;
330 dmu_objset_byteswap(void *buf
, size_t size
)
332 objset_phys_t
*osp
= buf
;
334 ASSERT(size
== OBJSET_OLD_PHYS_SIZE
|| size
== sizeof (objset_phys_t
));
335 dnode_byteswap(&osp
->os_meta_dnode
);
336 byteswap_uint64_array(&osp
->os_zil_header
, sizeof (zil_header_t
));
337 osp
->os_type
= BSWAP_64(osp
->os_type
);
338 osp
->os_flags
= BSWAP_64(osp
->os_flags
);
339 if (size
== sizeof (objset_phys_t
)) {
340 dnode_byteswap(&osp
->os_userused_dnode
);
341 dnode_byteswap(&osp
->os_groupused_dnode
);
346 dmu_objset_open_impl(spa_t
*spa
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
352 ASSERT(ds
== NULL
|| MUTEX_HELD(&ds
->ds_opening_lock
));
354 os
= kmem_zalloc(sizeof (objset_t
), KM_SLEEP
);
355 os
->os_dsl_dataset
= ds
;
358 if (!BP_IS_HOLE(os
->os_rootbp
)) {
359 arc_flags_t aflags
= ARC_FLAG_WAIT
;
361 SET_BOOKMARK(&zb
, ds
? ds
->ds_object
: DMU_META_OBJSET
,
362 ZB_ROOT_OBJECT
, ZB_ROOT_LEVEL
, ZB_ROOT_BLKID
);
364 if (DMU_OS_IS_L2CACHEABLE(os
))
365 aflags
|= ARC_FLAG_L2CACHE
;
367 dprintf_bp(os
->os_rootbp
, "reading %s", "");
368 err
= arc_read(NULL
, spa
, os
->os_rootbp
,
369 arc_getbuf_func
, &os
->os_phys_buf
,
370 ZIO_PRIORITY_SYNC_READ
, ZIO_FLAG_CANFAIL
, &aflags
, &zb
);
372 kmem_free(os
, sizeof (objset_t
));
373 /* convert checksum errors into IO errors */
375 err
= SET_ERROR(EIO
);
379 /* Increase the blocksize if we are permitted. */
380 if (spa_version(spa
) >= SPA_VERSION_USERSPACE
&&
381 arc_buf_size(os
->os_phys_buf
) < sizeof (objset_phys_t
)) {
382 arc_buf_t
*buf
= arc_alloc_buf(spa
, &os
->os_phys_buf
,
383 ARC_BUFC_METADATA
, sizeof (objset_phys_t
));
384 bzero(buf
->b_data
, sizeof (objset_phys_t
));
385 bcopy(os
->os_phys_buf
->b_data
, buf
->b_data
,
386 arc_buf_size(os
->os_phys_buf
));
387 arc_buf_destroy(os
->os_phys_buf
, &os
->os_phys_buf
);
388 os
->os_phys_buf
= buf
;
391 os
->os_phys
= os
->os_phys_buf
->b_data
;
392 os
->os_flags
= os
->os_phys
->os_flags
;
394 int size
= spa_version(spa
) >= SPA_VERSION_USERSPACE
?
395 sizeof (objset_phys_t
) : OBJSET_OLD_PHYS_SIZE
;
396 os
->os_phys_buf
= arc_alloc_buf(spa
, &os
->os_phys_buf
,
397 ARC_BUFC_METADATA
, size
);
398 os
->os_phys
= os
->os_phys_buf
->b_data
;
399 bzero(os
->os_phys
, size
);
403 * Note: the changed_cb will be called once before the register
404 * func returns, thus changing the checksum/compression from the
405 * default (fletcher2/off). Snapshots don't need to know about
406 * checksum/compression/copies.
409 boolean_t needlock
= B_FALSE
;
412 * Note: it's valid to open the objset if the dataset is
413 * long-held, in which case the pool_config lock will not
416 if (!dsl_pool_config_held(dmu_objset_pool(os
))) {
418 dsl_pool_config_enter(dmu_objset_pool(os
), FTAG
);
420 err
= dsl_prop_register(ds
,
421 zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE
),
422 primary_cache_changed_cb
, os
);
424 err
= dsl_prop_register(ds
,
425 zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE
),
426 secondary_cache_changed_cb
, os
);
428 if (!ds
->ds_is_snapshot
) {
430 err
= dsl_prop_register(ds
,
431 zfs_prop_to_name(ZFS_PROP_CHECKSUM
),
432 checksum_changed_cb
, os
);
435 err
= dsl_prop_register(ds
,
436 zfs_prop_to_name(ZFS_PROP_COMPRESSION
),
437 compression_changed_cb
, os
);
440 err
= dsl_prop_register(ds
,
441 zfs_prop_to_name(ZFS_PROP_COPIES
),
442 copies_changed_cb
, os
);
445 err
= dsl_prop_register(ds
,
446 zfs_prop_to_name(ZFS_PROP_DEDUP
),
447 dedup_changed_cb
, os
);
450 err
= dsl_prop_register(ds
,
451 zfs_prop_to_name(ZFS_PROP_LOGBIAS
),
452 logbias_changed_cb
, os
);
455 err
= dsl_prop_register(ds
,
456 zfs_prop_to_name(ZFS_PROP_SYNC
),
457 sync_changed_cb
, os
);
460 err
= dsl_prop_register(ds
,
462 ZFS_PROP_REDUNDANT_METADATA
),
463 redundant_metadata_changed_cb
, os
);
466 err
= dsl_prop_register(ds
,
467 zfs_prop_to_name(ZFS_PROP_RECORDSIZE
),
468 recordsize_changed_cb
, os
);
471 err
= dsl_prop_register(ds
,
472 zfs_prop_to_name(ZFS_PROP_DNODESIZE
),
473 dnodesize_changed_cb
, os
);
477 dsl_pool_config_exit(dmu_objset_pool(os
), FTAG
);
479 arc_buf_destroy(os
->os_phys_buf
, &os
->os_phys_buf
);
480 kmem_free(os
, sizeof (objset_t
));
484 /* It's the meta-objset. */
485 os
->os_checksum
= ZIO_CHECKSUM_FLETCHER_4
;
486 os
->os_compress
= ZIO_COMPRESS_ON
;
487 os
->os_copies
= spa_max_replication(spa
);
488 os
->os_dedup_checksum
= ZIO_CHECKSUM_OFF
;
489 os
->os_dedup_verify
= B_FALSE
;
490 os
->os_logbias
= ZFS_LOGBIAS_LATENCY
;
491 os
->os_sync
= ZFS_SYNC_STANDARD
;
492 os
->os_primary_cache
= ZFS_CACHE_ALL
;
493 os
->os_secondary_cache
= ZFS_CACHE_ALL
;
494 os
->os_dnodesize
= DNODE_MIN_SIZE
;
497 if (ds
== NULL
|| !ds
->ds_is_snapshot
)
498 os
->os_zil_header
= os
->os_phys
->os_zil_header
;
499 os
->os_zil
= zil_alloc(os
, &os
->os_zil_header
);
501 for (i
= 0; i
< TXG_SIZE
; i
++) {
502 list_create(&os
->os_dirty_dnodes
[i
], sizeof (dnode_t
),
503 offsetof(dnode_t
, dn_dirty_link
[i
]));
504 list_create(&os
->os_free_dnodes
[i
], sizeof (dnode_t
),
505 offsetof(dnode_t
, dn_dirty_link
[i
]));
507 list_create(&os
->os_dnodes
, sizeof (dnode_t
),
508 offsetof(dnode_t
, dn_link
));
509 list_create(&os
->os_downgraded_dbufs
, sizeof (dmu_buf_impl_t
),
510 offsetof(dmu_buf_impl_t
, db_link
));
512 list_link_init(&os
->os_evicting_node
);
514 mutex_init(&os
->os_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
515 mutex_init(&os
->os_obj_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
516 mutex_init(&os
->os_user_ptr_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
518 dnode_special_open(os
, &os
->os_phys
->os_meta_dnode
,
519 DMU_META_DNODE_OBJECT
, &os
->os_meta_dnode
);
520 if (arc_buf_size(os
->os_phys_buf
) >= sizeof (objset_phys_t
)) {
521 dnode_special_open(os
, &os
->os_phys
->os_userused_dnode
,
522 DMU_USERUSED_OBJECT
, &os
->os_userused_dnode
);
523 dnode_special_open(os
, &os
->os_phys
->os_groupused_dnode
,
524 DMU_GROUPUSED_OBJECT
, &os
->os_groupused_dnode
);
527 mutex_init(&os
->os_upgrade_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
534 dmu_objset_from_ds(dsl_dataset_t
*ds
, objset_t
**osp
)
539 * We shouldn't be doing anything with dsl_dataset_t's unless the
540 * pool_config lock is held, or the dataset is long-held.
542 ASSERT(dsl_pool_config_held(ds
->ds_dir
->dd_pool
) ||
543 dsl_dataset_long_held(ds
));
545 mutex_enter(&ds
->ds_opening_lock
);
546 if (ds
->ds_objset
== NULL
) {
548 err
= dmu_objset_open_impl(dsl_dataset_get_spa(ds
),
549 ds
, dsl_dataset_get_blkptr(ds
), &os
);
552 mutex_enter(&ds
->ds_lock
);
553 ASSERT(ds
->ds_objset
== NULL
);
555 mutex_exit(&ds
->ds_lock
);
558 *osp
= ds
->ds_objset
;
559 mutex_exit(&ds
->ds_opening_lock
);
564 * Holds the pool while the objset is held. Therefore only one objset
565 * can be held at a time.
568 dmu_objset_hold(const char *name
, void *tag
, objset_t
**osp
)
574 err
= dsl_pool_hold(name
, tag
, &dp
);
577 err
= dsl_dataset_hold(dp
, name
, tag
, &ds
);
579 dsl_pool_rele(dp
, tag
);
583 err
= dmu_objset_from_ds(ds
, osp
);
585 dsl_dataset_rele(ds
, tag
);
586 dsl_pool_rele(dp
, tag
);
593 dmu_objset_own_impl(dsl_dataset_t
*ds
, dmu_objset_type_t type
,
594 boolean_t readonly
, void *tag
, objset_t
**osp
)
598 err
= dmu_objset_from_ds(ds
, osp
);
600 dsl_dataset_disown(ds
, tag
);
601 } else if (type
!= DMU_OST_ANY
&& type
!= (*osp
)->os_phys
->os_type
) {
602 dsl_dataset_disown(ds
, tag
);
603 return (SET_ERROR(EINVAL
));
604 } else if (!readonly
&& dsl_dataset_is_snapshot(ds
)) {
605 dsl_dataset_disown(ds
, tag
);
606 return (SET_ERROR(EROFS
));
612 * dsl_pool must not be held when this is called.
613 * Upon successful return, there will be a longhold on the dataset,
614 * and the dsl_pool will not be held.
617 dmu_objset_own(const char *name
, dmu_objset_type_t type
,
618 boolean_t readonly
, void *tag
, objset_t
**osp
)
624 err
= dsl_pool_hold(name
, FTAG
, &dp
);
627 err
= dsl_dataset_own(dp
, name
, tag
, &ds
);
629 dsl_pool_rele(dp
, FTAG
);
632 err
= dmu_objset_own_impl(ds
, type
, readonly
, tag
, osp
);
633 dsl_pool_rele(dp
, FTAG
);
635 if (err
== 0 && dmu_objset_userobjspace_upgradable(*osp
))
636 dmu_objset_userobjspace_upgrade(*osp
);
642 dmu_objset_own_obj(dsl_pool_t
*dp
, uint64_t obj
, dmu_objset_type_t type
,
643 boolean_t readonly
, void *tag
, objset_t
**osp
)
648 err
= dsl_dataset_own_obj(dp
, obj
, tag
, &ds
);
652 return (dmu_objset_own_impl(ds
, type
, readonly
, tag
, osp
));
656 dmu_objset_rele(objset_t
*os
, void *tag
)
658 dsl_pool_t
*dp
= dmu_objset_pool(os
);
659 dsl_dataset_rele(os
->os_dsl_dataset
, tag
);
660 dsl_pool_rele(dp
, tag
);
664 * When we are called, os MUST refer to an objset associated with a dataset
665 * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner
666 * == tag. We will then release and reacquire ownership of the dataset while
667 * holding the pool config_rwlock to avoid intervening namespace or ownership
670 * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to
671 * release the hold on its dataset and acquire a new one on the dataset of the
672 * same name so that it can be partially torn down and reconstructed.
675 dmu_objset_refresh_ownership(objset_t
*os
, void *tag
)
678 dsl_dataset_t
*ds
, *newds
;
679 char name
[ZFS_MAX_DATASET_NAME_LEN
];
681 ds
= os
->os_dsl_dataset
;
682 VERIFY3P(ds
, !=, NULL
);
683 VERIFY3P(ds
->ds_owner
, ==, tag
);
684 VERIFY(dsl_dataset_long_held(ds
));
686 dsl_dataset_name(ds
, name
);
687 dp
= dmu_objset_pool(os
);
688 dsl_pool_config_enter(dp
, FTAG
);
689 dmu_objset_disown(os
, tag
);
690 VERIFY0(dsl_dataset_own(dp
, name
, tag
, &newds
));
691 VERIFY3P(newds
, ==, os
->os_dsl_dataset
);
692 dsl_pool_config_exit(dp
, FTAG
);
696 dmu_objset_disown(objset_t
*os
, void *tag
)
699 * Stop upgrading thread
701 dmu_objset_upgrade_stop(os
);
702 dsl_dataset_disown(os
->os_dsl_dataset
, tag
);
706 dmu_objset_evict_dbufs(objset_t
*os
)
711 dn_marker
= kmem_alloc(sizeof (dnode_t
), KM_SLEEP
);
713 mutex_enter(&os
->os_lock
);
714 dn
= list_head(&os
->os_dnodes
);
717 * Skip dnodes without holds. We have to do this dance
718 * because dnode_add_ref() only works if there is already a
719 * hold. If the dnode has no holds, then it has no dbufs.
721 if (dnode_add_ref(dn
, FTAG
)) {
722 list_insert_after(&os
->os_dnodes
, dn
, dn_marker
);
723 mutex_exit(&os
->os_lock
);
725 dnode_evict_dbufs(dn
);
726 dnode_rele(dn
, FTAG
);
728 mutex_enter(&os
->os_lock
);
729 dn
= list_next(&os
->os_dnodes
, dn_marker
);
730 list_remove(&os
->os_dnodes
, dn_marker
);
732 dn
= list_next(&os
->os_dnodes
, dn
);
735 mutex_exit(&os
->os_lock
);
737 kmem_free(dn_marker
, sizeof (dnode_t
));
739 if (DMU_USERUSED_DNODE(os
) != NULL
) {
740 dnode_evict_dbufs(DMU_GROUPUSED_DNODE(os
));
741 dnode_evict_dbufs(DMU_USERUSED_DNODE(os
));
743 dnode_evict_dbufs(DMU_META_DNODE(os
));
747 * Objset eviction processing is split into into two pieces.
748 * The first marks the objset as evicting, evicts any dbufs that
749 * have a refcount of zero, and then queues up the objset for the
750 * second phase of eviction. Once os->os_dnodes has been cleared by
751 * dnode_buf_pageout()->dnode_destroy(), the second phase is executed.
752 * The second phase closes the special dnodes, dequeues the objset from
753 * the list of those undergoing eviction, and finally frees the objset.
755 * NOTE: Due to asynchronous eviction processing (invocation of
756 * dnode_buf_pageout()), it is possible for the meta dnode for the
757 * objset to have no holds even though os->os_dnodes is not empty.
760 dmu_objset_evict(objset_t
*os
)
764 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
766 for (t
= 0; t
< TXG_SIZE
; t
++)
767 ASSERT(!dmu_objset_is_dirty(os
, t
));
770 dsl_prop_unregister_all(ds
, os
);
775 dmu_objset_evict_dbufs(os
);
777 mutex_enter(&os
->os_lock
);
778 spa_evicting_os_register(os
->os_spa
, os
);
779 if (list_is_empty(&os
->os_dnodes
)) {
780 mutex_exit(&os
->os_lock
);
781 dmu_objset_evict_done(os
);
783 mutex_exit(&os
->os_lock
);
788 dmu_objset_evict_done(objset_t
*os
)
790 ASSERT3P(list_head(&os
->os_dnodes
), ==, NULL
);
792 dnode_special_close(&os
->os_meta_dnode
);
793 if (DMU_USERUSED_DNODE(os
)) {
794 dnode_special_close(&os
->os_userused_dnode
);
795 dnode_special_close(&os
->os_groupused_dnode
);
797 zil_free(os
->os_zil
);
799 arc_buf_destroy(os
->os_phys_buf
, &os
->os_phys_buf
);
802 * This is a barrier to prevent the objset from going away in
803 * dnode_move() until we can safely ensure that the objset is still in
804 * use. We consider the objset valid before the barrier and invalid
807 rw_enter(&os_lock
, RW_READER
);
810 mutex_destroy(&os
->os_lock
);
811 mutex_destroy(&os
->os_obj_lock
);
812 mutex_destroy(&os
->os_user_ptr_lock
);
813 spa_evicting_os_deregister(os
->os_spa
, os
);
814 kmem_free(os
, sizeof (objset_t
));
818 dmu_objset_snap_cmtime(objset_t
*os
)
820 return (dsl_dir_snap_cmtime(os
->os_dsl_dataset
->ds_dir
));
823 /* called from dsl for meta-objset */
825 dmu_objset_create_impl(spa_t
*spa
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
826 dmu_objset_type_t type
, dmu_tx_t
*tx
)
831 ASSERT(dmu_tx_is_syncing(tx
));
834 VERIFY0(dmu_objset_from_ds(ds
, &os
));
836 VERIFY0(dmu_objset_open_impl(spa
, NULL
, bp
, &os
));
838 mdn
= DMU_META_DNODE(os
);
840 dnode_allocate(mdn
, DMU_OT_DNODE
, DNODE_BLOCK_SIZE
, DN_MAX_INDBLKSHIFT
,
841 DMU_OT_NONE
, 0, DNODE_MIN_SLOTS
, tx
);
844 * We don't want to have to increase the meta-dnode's nlevels
845 * later, because then we could do it in quescing context while
846 * we are also accessing it in open context.
848 * This precaution is not necessary for the MOS (ds == NULL),
849 * because the MOS is only updated in syncing context.
850 * This is most fortunate: the MOS is the only objset that
851 * needs to be synced multiple times as spa_sync() iterates
852 * to convergence, so minimizing its dn_nlevels matters.
858 * Determine the number of levels necessary for the meta-dnode
859 * to contain DN_MAX_OBJECT dnodes.
861 while ((uint64_t)mdn
->dn_nblkptr
<< (mdn
->dn_datablkshift
+
862 (levels
- 1) * (mdn
->dn_indblkshift
- SPA_BLKPTRSHIFT
)) <
863 DN_MAX_OBJECT
* sizeof (dnode_phys_t
))
866 mdn
->dn_next_nlevels
[tx
->tx_txg
& TXG_MASK
] =
867 mdn
->dn_nlevels
= levels
;
870 ASSERT(type
!= DMU_OST_NONE
);
871 ASSERT(type
!= DMU_OST_ANY
);
872 ASSERT(type
< DMU_OST_NUMTYPES
);
873 os
->os_phys
->os_type
= type
;
874 if (dmu_objset_userused_enabled(os
)) {
875 os
->os_phys
->os_flags
|= OBJSET_FLAG_USERACCOUNTING_COMPLETE
;
876 if (dmu_objset_userobjused_enabled(os
)) {
877 ds
->ds_feature_activation_needed
[
878 SPA_FEATURE_USEROBJ_ACCOUNTING
] = B_TRUE
;
879 os
->os_phys
->os_flags
|=
880 OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE
;
882 os
->os_flags
= os
->os_phys
->os_flags
;
885 dsl_dataset_dirty(ds
, tx
);
890 typedef struct dmu_objset_create_arg
{
891 const char *doca_name
;
893 void (*doca_userfunc
)(objset_t
*os
, void *arg
,
894 cred_t
*cr
, dmu_tx_t
*tx
);
896 dmu_objset_type_t doca_type
;
898 } dmu_objset_create_arg_t
;
902 dmu_objset_create_check(void *arg
, dmu_tx_t
*tx
)
904 dmu_objset_create_arg_t
*doca
= arg
;
905 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
910 if (strchr(doca
->doca_name
, '@') != NULL
)
911 return (SET_ERROR(EINVAL
));
913 if (strlen(doca
->doca_name
) >= ZFS_MAX_DATASET_NAME_LEN
)
914 return (SET_ERROR(ENAMETOOLONG
));
916 error
= dsl_dir_hold(dp
, doca
->doca_name
, FTAG
, &pdd
, &tail
);
920 dsl_dir_rele(pdd
, FTAG
);
921 return (SET_ERROR(EEXIST
));
923 error
= dsl_fs_ss_limit_check(pdd
, 1, ZFS_PROP_FILESYSTEM_LIMIT
, NULL
,
925 dsl_dir_rele(pdd
, FTAG
);
931 dmu_objset_create_sync(void *arg
, dmu_tx_t
*tx
)
933 dmu_objset_create_arg_t
*doca
= arg
;
934 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
942 VERIFY0(dsl_dir_hold(dp
, doca
->doca_name
, FTAG
, &pdd
, &tail
));
944 obj
= dsl_dataset_create_sync(pdd
, tail
, NULL
, doca
->doca_flags
,
945 doca
->doca_cred
, tx
);
947 VERIFY0(dsl_dataset_hold_obj(pdd
->dd_pool
, obj
, FTAG
, &ds
));
948 bp
= dsl_dataset_get_blkptr(ds
);
949 os
= dmu_objset_create_impl(pdd
->dd_pool
->dp_spa
,
950 ds
, bp
, doca
->doca_type
, tx
);
952 if (doca
->doca_userfunc
!= NULL
) {
953 doca
->doca_userfunc(os
, doca
->doca_userarg
,
954 doca
->doca_cred
, tx
);
957 spa_history_log_internal_ds(ds
, "create", tx
, "");
958 zvol_create_minors(dp
->dp_spa
, doca
->doca_name
, B_TRUE
);
960 dsl_dataset_rele(ds
, FTAG
);
961 dsl_dir_rele(pdd
, FTAG
);
965 dmu_objset_create(const char *name
, dmu_objset_type_t type
, uint64_t flags
,
966 void (*func
)(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
), void *arg
)
968 dmu_objset_create_arg_t doca
;
970 doca
.doca_name
= name
;
971 doca
.doca_cred
= CRED();
972 doca
.doca_flags
= flags
;
973 doca
.doca_userfunc
= func
;
974 doca
.doca_userarg
= arg
;
975 doca
.doca_type
= type
;
977 return (dsl_sync_task(name
,
978 dmu_objset_create_check
, dmu_objset_create_sync
, &doca
,
979 5, ZFS_SPACE_CHECK_NORMAL
));
982 typedef struct dmu_objset_clone_arg
{
983 const char *doca_clone
;
984 const char *doca_origin
;
986 } dmu_objset_clone_arg_t
;
990 dmu_objset_clone_check(void *arg
, dmu_tx_t
*tx
)
992 dmu_objset_clone_arg_t
*doca
= arg
;
996 dsl_dataset_t
*origin
;
997 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
999 if (strchr(doca
->doca_clone
, '@') != NULL
)
1000 return (SET_ERROR(EINVAL
));
1002 if (strlen(doca
->doca_clone
) >= ZFS_MAX_DATASET_NAME_LEN
)
1003 return (SET_ERROR(ENAMETOOLONG
));
1005 error
= dsl_dir_hold(dp
, doca
->doca_clone
, FTAG
, &pdd
, &tail
);
1009 dsl_dir_rele(pdd
, FTAG
);
1010 return (SET_ERROR(EEXIST
));
1013 error
= dsl_fs_ss_limit_check(pdd
, 1, ZFS_PROP_FILESYSTEM_LIMIT
, NULL
,
1016 dsl_dir_rele(pdd
, FTAG
);
1017 return (SET_ERROR(EDQUOT
));
1019 dsl_dir_rele(pdd
, FTAG
);
1021 error
= dsl_dataset_hold(dp
, doca
->doca_origin
, FTAG
, &origin
);
1025 /* You can only clone snapshots, not the head datasets. */
1026 if (!origin
->ds_is_snapshot
) {
1027 dsl_dataset_rele(origin
, FTAG
);
1028 return (SET_ERROR(EINVAL
));
1030 dsl_dataset_rele(origin
, FTAG
);
1036 dmu_objset_clone_sync(void *arg
, dmu_tx_t
*tx
)
1038 dmu_objset_clone_arg_t
*doca
= arg
;
1039 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1042 dsl_dataset_t
*origin
, *ds
;
1044 char namebuf
[ZFS_MAX_DATASET_NAME_LEN
];
1046 VERIFY0(dsl_dir_hold(dp
, doca
->doca_clone
, FTAG
, &pdd
, &tail
));
1047 VERIFY0(dsl_dataset_hold(dp
, doca
->doca_origin
, FTAG
, &origin
));
1049 obj
= dsl_dataset_create_sync(pdd
, tail
, origin
, 0,
1050 doca
->doca_cred
, tx
);
1052 VERIFY0(dsl_dataset_hold_obj(pdd
->dd_pool
, obj
, FTAG
, &ds
));
1053 dsl_dataset_name(origin
, namebuf
);
1054 spa_history_log_internal_ds(ds
, "clone", tx
,
1055 "origin=%s (%llu)", namebuf
, origin
->ds_object
);
1056 zvol_create_minors(dp
->dp_spa
, doca
->doca_clone
, B_TRUE
);
1057 dsl_dataset_rele(ds
, FTAG
);
1058 dsl_dataset_rele(origin
, FTAG
);
1059 dsl_dir_rele(pdd
, FTAG
);
1063 dmu_objset_clone(const char *clone
, const char *origin
)
1065 dmu_objset_clone_arg_t doca
;
1067 doca
.doca_clone
= clone
;
1068 doca
.doca_origin
= origin
;
1069 doca
.doca_cred
= CRED();
1071 return (dsl_sync_task(clone
,
1072 dmu_objset_clone_check
, dmu_objset_clone_sync
, &doca
,
1073 5, ZFS_SPACE_CHECK_NORMAL
));
1077 dmu_objset_snapshot_one(const char *fsname
, const char *snapname
)
1080 char *longsnap
= kmem_asprintf("%s@%s", fsname
, snapname
);
1081 nvlist_t
*snaps
= fnvlist_alloc();
1083 fnvlist_add_boolean(snaps
, longsnap
);
1085 err
= dsl_dataset_snapshot(snaps
, NULL
, NULL
);
1086 fnvlist_free(snaps
);
1091 dmu_objset_upgrade_task_cb(void *data
)
1093 objset_t
*os
= data
;
1095 mutex_enter(&os
->os_upgrade_lock
);
1096 os
->os_upgrade_status
= EINTR
;
1097 if (!os
->os_upgrade_exit
) {
1098 mutex_exit(&os
->os_upgrade_lock
);
1100 os
->os_upgrade_status
= os
->os_upgrade_cb(os
);
1101 mutex_enter(&os
->os_upgrade_lock
);
1103 os
->os_upgrade_exit
= B_TRUE
;
1104 os
->os_upgrade_id
= 0;
1105 mutex_exit(&os
->os_upgrade_lock
);
1109 dmu_objset_upgrade(objset_t
*os
, dmu_objset_upgrade_cb_t cb
)
1111 if (os
->os_upgrade_id
!= 0)
1114 mutex_enter(&os
->os_upgrade_lock
);
1115 if (os
->os_upgrade_id
== 0 && os
->os_upgrade_status
== 0) {
1116 os
->os_upgrade_exit
= B_FALSE
;
1117 os
->os_upgrade_cb
= cb
;
1118 os
->os_upgrade_id
= taskq_dispatch(
1119 os
->os_spa
->spa_upgrade_taskq
,
1120 dmu_objset_upgrade_task_cb
, os
, TQ_SLEEP
);
1121 if (os
->os_upgrade_id
== TASKQID_INVALID
)
1122 os
->os_upgrade_status
= ENOMEM
;
1124 mutex_exit(&os
->os_upgrade_lock
);
1128 dmu_objset_upgrade_stop(objset_t
*os
)
1130 mutex_enter(&os
->os_upgrade_lock
);
1131 os
->os_upgrade_exit
= B_TRUE
;
1132 if (os
->os_upgrade_id
!= 0) {
1133 taskqid_t id
= os
->os_upgrade_id
;
1135 os
->os_upgrade_id
= 0;
1136 mutex_exit(&os
->os_upgrade_lock
);
1138 taskq_cancel_id(os
->os_spa
->spa_upgrade_taskq
, id
);
1140 mutex_exit(&os
->os_upgrade_lock
);
1145 dmu_objset_sync_dnodes(list_t
*list
, list_t
*newlist
, dmu_tx_t
*tx
)
1149 while ((dn
= list_head(list
))) {
1150 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
);
1151 ASSERT(dn
->dn_dbuf
->db_data_pending
);
1153 * Initialize dn_zio outside dnode_sync() because the
1154 * meta-dnode needs to set it ouside dnode_sync().
1156 dn
->dn_zio
= dn
->dn_dbuf
->db_data_pending
->dr_zio
;
1159 ASSERT3U(dn
->dn_nlevels
, <=, DN_MAX_LEVELS
);
1160 list_remove(list
, dn
);
1163 (void) dnode_add_ref(dn
, newlist
);
1164 list_insert_tail(newlist
, dn
);
1173 dmu_objset_write_ready(zio_t
*zio
, arc_buf_t
*abuf
, void *arg
)
1177 blkptr_t
*bp
= zio
->io_bp
;
1179 dnode_phys_t
*dnp
= &os
->os_phys
->os_meta_dnode
;
1181 ASSERT(!BP_IS_EMBEDDED(bp
));
1182 ASSERT3P(bp
, ==, os
->os_rootbp
);
1183 ASSERT3U(BP_GET_TYPE(bp
), ==, DMU_OT_OBJSET
);
1184 ASSERT0(BP_GET_LEVEL(bp
));
1187 * Update rootbp fill count: it should be the number of objects
1188 * allocated in the object set (not counting the "special"
1189 * objects that are stored in the objset_phys_t -- the meta
1190 * dnode and user/group accounting objects).
1193 for (i
= 0; i
< dnp
->dn_nblkptr
; i
++)
1194 bp
->blk_fill
+= BP_GET_FILL(&dnp
->dn_blkptr
[i
]);
1199 dmu_objset_write_done(zio_t
*zio
, arc_buf_t
*abuf
, void *arg
)
1201 blkptr_t
*bp
= zio
->io_bp
;
1202 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
1205 if (zio
->io_flags
& ZIO_FLAG_IO_REWRITE
) {
1206 ASSERT(BP_EQUAL(bp
, bp_orig
));
1208 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
1209 dmu_tx_t
*tx
= os
->os_synctx
;
1211 (void) dsl_dataset_block_kill(ds
, bp_orig
, tx
, B_TRUE
);
1212 dsl_dataset_block_born(ds
, bp
, tx
);
1216 /* called from dsl */
1218 dmu_objset_sync(objset_t
*os
, zio_t
*pio
, dmu_tx_t
*tx
)
1221 zbookmark_phys_t zb
;
1225 list_t
*newlist
= NULL
;
1226 dbuf_dirty_record_t
*dr
;
1228 dprintf_ds(os
->os_dsl_dataset
, "txg=%llu\n", tx
->tx_txg
);
1230 ASSERT(dmu_tx_is_syncing(tx
));
1231 /* XXX the write_done callback should really give us the tx... */
1234 if (os
->os_dsl_dataset
== NULL
) {
1236 * This is the MOS. If we have upgraded,
1237 * spa_max_replication() could change, so reset
1240 os
->os_copies
= spa_max_replication(os
->os_spa
);
1244 * Create the root block IO
1246 SET_BOOKMARK(&zb
, os
->os_dsl_dataset
?
1247 os
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
1248 ZB_ROOT_OBJECT
, ZB_ROOT_LEVEL
, ZB_ROOT_BLKID
);
1249 arc_release(os
->os_phys_buf
, &os
->os_phys_buf
);
1251 dmu_write_policy(os
, NULL
, 0, 0, ZIO_COMPRESS_INHERIT
, &zp
);
1253 zio
= arc_write(pio
, os
->os_spa
, tx
->tx_txg
,
1254 os
->os_rootbp
, os
->os_phys_buf
, DMU_OS_IS_L2CACHEABLE(os
),
1255 &zp
, dmu_objset_write_ready
, NULL
, NULL
, dmu_objset_write_done
,
1256 os
, ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);
1259 * Sync special dnodes - the parent IO for the sync is the root block
1261 DMU_META_DNODE(os
)->dn_zio
= zio
;
1262 dnode_sync(DMU_META_DNODE(os
), tx
);
1264 os
->os_phys
->os_flags
= os
->os_flags
;
1266 if (DMU_USERUSED_DNODE(os
) &&
1267 DMU_USERUSED_DNODE(os
)->dn_type
!= DMU_OT_NONE
) {
1268 DMU_USERUSED_DNODE(os
)->dn_zio
= zio
;
1269 dnode_sync(DMU_USERUSED_DNODE(os
), tx
);
1270 DMU_GROUPUSED_DNODE(os
)->dn_zio
= zio
;
1271 dnode_sync(DMU_GROUPUSED_DNODE(os
), tx
);
1274 txgoff
= tx
->tx_txg
& TXG_MASK
;
1276 if (dmu_objset_userused_enabled(os
)) {
1277 newlist
= &os
->os_synced_dnodes
;
1279 * We must create the list here because it uses the
1280 * dn_dirty_link[] of this txg.
1282 list_create(newlist
, sizeof (dnode_t
),
1283 offsetof(dnode_t
, dn_dirty_link
[txgoff
]));
1286 dmu_objset_sync_dnodes(&os
->os_free_dnodes
[txgoff
], newlist
, tx
);
1287 dmu_objset_sync_dnodes(&os
->os_dirty_dnodes
[txgoff
], newlist
, tx
);
1289 list
= &DMU_META_DNODE(os
)->dn_dirty_records
[txgoff
];
1290 while ((dr
= list_head(list
))) {
1291 ASSERT0(dr
->dr_dbuf
->db_level
);
1292 list_remove(list
, dr
);
1294 zio_nowait(dr
->dr_zio
);
1297 /* Enable dnode backfill if enough objects have been freed. */
1298 if (os
->os_freed_dnodes
>= dmu_rescan_dnode_threshold
) {
1299 os
->os_rescan_dnodes
= B_TRUE
;
1300 os
->os_freed_dnodes
= 0;
1304 * Free intent log blocks up to this tx.
1306 zil_sync(os
->os_zil
, tx
);
1307 os
->os_phys
->os_zil_header
= os
->os_zil_header
;
1312 dmu_objset_is_dirty(objset_t
*os
, uint64_t txg
)
1314 return (!list_is_empty(&os
->os_dirty_dnodes
[txg
& TXG_MASK
]) ||
1315 !list_is_empty(&os
->os_free_dnodes
[txg
& TXG_MASK
]));
1318 static objset_used_cb_t
*used_cbs
[DMU_OST_NUMTYPES
];
1321 dmu_objset_register_type(dmu_objset_type_t ost
, objset_used_cb_t
*cb
)
1327 dmu_objset_userused_enabled(objset_t
*os
)
1329 return (spa_version(os
->os_spa
) >= SPA_VERSION_USERSPACE
&&
1330 used_cbs
[os
->os_phys
->os_type
] != NULL
&&
1331 DMU_USERUSED_DNODE(os
) != NULL
);
1335 dmu_objset_userobjused_enabled(objset_t
*os
)
1337 return (dmu_objset_userused_enabled(os
) &&
1338 spa_feature_is_enabled(os
->os_spa
, SPA_FEATURE_USEROBJ_ACCOUNTING
));
1341 typedef struct userquota_node
{
1342 /* must be in the first filed, see userquota_update_cache() */
1343 char uqn_id
[20 + DMU_OBJACCT_PREFIX_LEN
];
1345 avl_node_t uqn_node
;
1348 typedef struct userquota_cache
{
1349 avl_tree_t uqc_user_deltas
;
1350 avl_tree_t uqc_group_deltas
;
1351 } userquota_cache_t
;
1354 userquota_compare(const void *l
, const void *r
)
1356 const userquota_node_t
*luqn
= l
;
1357 const userquota_node_t
*ruqn
= r
;
1361 * NB: can only access uqn_id because userquota_update_cache() doesn't
1362 * pass in an entire userquota_node_t.
1364 rv
= strcmp(luqn
->uqn_id
, ruqn
->uqn_id
);
1366 return (AVL_ISIGN(rv
));
1370 do_userquota_cacheflush(objset_t
*os
, userquota_cache_t
*cache
, dmu_tx_t
*tx
)
1373 userquota_node_t
*uqn
;
1375 ASSERT(dmu_tx_is_syncing(tx
));
1378 while ((uqn
= avl_destroy_nodes(&cache
->uqc_user_deltas
,
1379 &cookie
)) != NULL
) {
1380 VERIFY0(zap_increment(os
, DMU_USERUSED_OBJECT
,
1381 uqn
->uqn_id
, uqn
->uqn_delta
, tx
));
1382 kmem_free(uqn
, sizeof (*uqn
));
1384 avl_destroy(&cache
->uqc_user_deltas
);
1387 while ((uqn
= avl_destroy_nodes(&cache
->uqc_group_deltas
,
1388 &cookie
)) != NULL
) {
1389 VERIFY0(zap_increment(os
, DMU_GROUPUSED_OBJECT
,
1390 uqn
->uqn_id
, uqn
->uqn_delta
, tx
));
1391 kmem_free(uqn
, sizeof (*uqn
));
1393 avl_destroy(&cache
->uqc_group_deltas
);
1397 userquota_update_cache(avl_tree_t
*avl
, const char *id
, int64_t delta
)
1399 userquota_node_t
*uqn
;
1402 ASSERT(strlen(id
) < sizeof (uqn
->uqn_id
));
1404 * Use id directly for searching because uqn_id is the first field of
1405 * userquota_node_t and fields after uqn_id won't be accessed in
1408 uqn
= avl_find(avl
, (const void *)id
, &idx
);
1410 uqn
= kmem_zalloc(sizeof (*uqn
), KM_SLEEP
);
1411 strlcpy(uqn
->uqn_id
, id
, sizeof (uqn
->uqn_id
));
1412 avl_insert(avl
, uqn
, idx
);
1414 uqn
->uqn_delta
+= delta
;
1418 do_userquota_update(userquota_cache_t
*cache
, uint64_t used
, uint64_t flags
,
1419 uint64_t user
, uint64_t group
, boolean_t subtract
)
1421 if ((flags
& DNODE_FLAG_USERUSED_ACCOUNTED
)) {
1422 int64_t delta
= DNODE_MIN_SIZE
+ used
;
1428 (void) sprintf(name
, "%llx", (longlong_t
)user
);
1429 userquota_update_cache(&cache
->uqc_user_deltas
, name
, delta
);
1431 (void) sprintf(name
, "%llx", (longlong_t
)group
);
1432 userquota_update_cache(&cache
->uqc_group_deltas
, name
, delta
);
1437 do_userobjquota_update(userquota_cache_t
*cache
, uint64_t flags
,
1438 uint64_t user
, uint64_t group
, boolean_t subtract
)
1440 if (flags
& DNODE_FLAG_USEROBJUSED_ACCOUNTED
) {
1441 char name
[20 + DMU_OBJACCT_PREFIX_LEN
];
1442 int delta
= subtract
? -1 : 1;
1444 (void) snprintf(name
, sizeof (name
), DMU_OBJACCT_PREFIX
"%llx",
1446 userquota_update_cache(&cache
->uqc_user_deltas
, name
, delta
);
1448 (void) snprintf(name
, sizeof (name
), DMU_OBJACCT_PREFIX
"%llx",
1450 userquota_update_cache(&cache
->uqc_group_deltas
, name
, delta
);
1455 dmu_objset_do_userquota_updates(objset_t
*os
, dmu_tx_t
*tx
)
1458 list_t
*list
= &os
->os_synced_dnodes
;
1459 userquota_cache_t cache
= { { 0 } };
1461 ASSERT(list_head(list
) == NULL
|| dmu_objset_userused_enabled(os
));
1463 avl_create(&cache
.uqc_user_deltas
, userquota_compare
,
1464 sizeof (userquota_node_t
), offsetof(userquota_node_t
, uqn_node
));
1465 avl_create(&cache
.uqc_group_deltas
, userquota_compare
,
1466 sizeof (userquota_node_t
), offsetof(userquota_node_t
, uqn_node
));
1468 while ((dn
= list_head(list
))) {
1470 ASSERT(!DMU_OBJECT_IS_SPECIAL(dn
->dn_object
));
1471 ASSERT(dn
->dn_phys
->dn_type
== DMU_OT_NONE
||
1472 dn
->dn_phys
->dn_flags
&
1473 DNODE_FLAG_USERUSED_ACCOUNTED
);
1475 /* Allocate the user/groupused objects if necessary. */
1476 if (DMU_USERUSED_DNODE(os
)->dn_type
== DMU_OT_NONE
) {
1477 VERIFY0(zap_create_claim(os
, DMU_USERUSED_OBJECT
,
1478 DMU_OT_USERGROUP_USED
, DMU_OT_NONE
, 0, tx
));
1479 VERIFY0(zap_create_claim(os
, DMU_GROUPUSED_OBJECT
,
1480 DMU_OT_USERGROUP_USED
, DMU_OT_NONE
, 0, tx
));
1483 flags
= dn
->dn_id_flags
;
1485 if (flags
& DN_ID_OLD_EXIST
) {
1486 do_userquota_update(&cache
,
1487 dn
->dn_oldused
, dn
->dn_oldflags
,
1488 dn
->dn_olduid
, dn
->dn_oldgid
, B_TRUE
);
1489 do_userobjquota_update(&cache
, dn
->dn_oldflags
,
1490 dn
->dn_olduid
, dn
->dn_oldgid
, B_TRUE
);
1492 if (flags
& DN_ID_NEW_EXIST
) {
1493 do_userquota_update(&cache
,
1494 DN_USED_BYTES(dn
->dn_phys
), dn
->dn_phys
->dn_flags
,
1495 dn
->dn_newuid
, dn
->dn_newgid
, B_FALSE
);
1496 do_userobjquota_update(&cache
, dn
->dn_phys
->dn_flags
,
1497 dn
->dn_newuid
, dn
->dn_newgid
, B_FALSE
);
1500 mutex_enter(&dn
->dn_mtx
);
1502 dn
->dn_oldflags
= 0;
1503 if (dn
->dn_id_flags
& DN_ID_NEW_EXIST
) {
1504 dn
->dn_olduid
= dn
->dn_newuid
;
1505 dn
->dn_oldgid
= dn
->dn_newgid
;
1506 dn
->dn_id_flags
|= DN_ID_OLD_EXIST
;
1507 if (dn
->dn_bonuslen
== 0)
1508 dn
->dn_id_flags
|= DN_ID_CHKED_SPILL
;
1510 dn
->dn_id_flags
|= DN_ID_CHKED_BONUS
;
1512 dn
->dn_id_flags
&= ~(DN_ID_NEW_EXIST
);
1513 mutex_exit(&dn
->dn_mtx
);
1515 list_remove(list
, dn
);
1516 dnode_rele(dn
, list
);
1518 do_userquota_cacheflush(os
, &cache
, tx
);
1522 * Returns a pointer to data to find uid/gid from
1524 * If a dirty record for transaction group that is syncing can't
1525 * be found then NULL is returned. In the NULL case it is assumed
1526 * the uid/gid aren't changing.
1529 dmu_objset_userquota_find_data(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1531 dbuf_dirty_record_t
*dr
, **drp
;
1534 if (db
->db_dirtycnt
== 0)
1535 return (db
->db
.db_data
); /* Nothing is changing */
1537 for (drp
= &db
->db_last_dirty
; (dr
= *drp
) != NULL
; drp
= &dr
->dr_next
)
1538 if (dr
->dr_txg
== tx
->tx_txg
)
1546 DB_DNODE_ENTER(dr
->dr_dbuf
);
1547 dn
= DB_DNODE(dr
->dr_dbuf
);
1549 if (dn
->dn_bonuslen
== 0 &&
1550 dr
->dr_dbuf
->db_blkid
== DMU_SPILL_BLKID
)
1551 data
= dr
->dt
.dl
.dr_data
->b_data
;
1553 data
= dr
->dt
.dl
.dr_data
;
1555 DB_DNODE_EXIT(dr
->dr_dbuf
);
1562 dmu_objset_userquota_get_ids(dnode_t
*dn
, boolean_t before
, dmu_tx_t
*tx
)
1564 objset_t
*os
= dn
->dn_objset
;
1566 dmu_buf_impl_t
*db
= NULL
;
1567 uint64_t *user
= NULL
;
1568 uint64_t *group
= NULL
;
1569 int flags
= dn
->dn_id_flags
;
1571 boolean_t have_spill
= B_FALSE
;
1573 if (!dmu_objset_userused_enabled(dn
->dn_objset
))
1576 if (before
&& (flags
& (DN_ID_CHKED_BONUS
|DN_ID_OLD_EXIST
|
1577 DN_ID_CHKED_SPILL
)))
1580 if (before
&& dn
->dn_bonuslen
!= 0)
1581 data
= DN_BONUS(dn
->dn_phys
);
1582 else if (!before
&& dn
->dn_bonuslen
!= 0) {
1585 mutex_enter(&db
->db_mtx
);
1586 data
= dmu_objset_userquota_find_data(db
, tx
);
1588 data
= DN_BONUS(dn
->dn_phys
);
1590 } else if (dn
->dn_bonuslen
== 0 && dn
->dn_bonustype
== DMU_OT_SA
) {
1593 if (RW_WRITE_HELD(&dn
->dn_struct_rwlock
))
1594 rf
|= DB_RF_HAVESTRUCT
;
1595 error
= dmu_spill_hold_by_dnode(dn
,
1596 rf
| DB_RF_MUST_SUCCEED
,
1597 FTAG
, (dmu_buf_t
**)&db
);
1599 mutex_enter(&db
->db_mtx
);
1600 data
= (before
) ? db
->db
.db_data
:
1601 dmu_objset_userquota_find_data(db
, tx
);
1602 have_spill
= B_TRUE
;
1604 mutex_enter(&dn
->dn_mtx
);
1605 dn
->dn_id_flags
|= DN_ID_CHKED_BONUS
;
1606 mutex_exit(&dn
->dn_mtx
);
1612 user
= &dn
->dn_olduid
;
1613 group
= &dn
->dn_oldgid
;
1615 user
= &dn
->dn_newuid
;
1616 group
= &dn
->dn_newgid
;
1620 * Must always call the callback in case the object
1621 * type has changed and that type isn't an object type to track
1623 error
= used_cbs
[os
->os_phys
->os_type
](dn
->dn_bonustype
, data
,
1627 * Preserve existing uid/gid when the callback can't determine
1628 * what the new uid/gid are and the callback returned EEXIST.
1629 * The EEXIST error tells us to just use the existing uid/gid.
1630 * If we don't know what the old values are then just assign
1631 * them to 0, since that is a new file being created.
1633 if (!before
&& data
== NULL
&& error
== EEXIST
) {
1634 if (flags
& DN_ID_OLD_EXIST
) {
1635 dn
->dn_newuid
= dn
->dn_olduid
;
1636 dn
->dn_newgid
= dn
->dn_oldgid
;
1645 mutex_exit(&db
->db_mtx
);
1647 mutex_enter(&dn
->dn_mtx
);
1648 if (error
== 0 && before
)
1649 dn
->dn_id_flags
|= DN_ID_OLD_EXIST
;
1650 if (error
== 0 && !before
)
1651 dn
->dn_id_flags
|= DN_ID_NEW_EXIST
;
1654 dn
->dn_id_flags
|= DN_ID_CHKED_SPILL
;
1656 dn
->dn_id_flags
|= DN_ID_CHKED_BONUS
;
1658 mutex_exit(&dn
->dn_mtx
);
1660 dmu_buf_rele((dmu_buf_t
*)db
, FTAG
);
1664 dmu_objset_userspace_present(objset_t
*os
)
1666 return (os
->os_phys
->os_flags
&
1667 OBJSET_FLAG_USERACCOUNTING_COMPLETE
);
1671 dmu_objset_userobjspace_present(objset_t
*os
)
1673 return (os
->os_phys
->os_flags
&
1674 OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE
);
1678 dmu_objset_space_upgrade(objset_t
*os
)
1684 * We simply need to mark every object dirty, so that it will be
1685 * synced out and now accounted. If this is called
1686 * concurrently, or if we already did some work before crashing,
1687 * that's fine, since we track each object's accounted state
1691 for (obj
= 0; err
== 0; err
= dmu_object_next(os
, &obj
, FALSE
, 0)) {
1696 mutex_enter(&os
->os_upgrade_lock
);
1697 if (os
->os_upgrade_exit
)
1698 err
= SET_ERROR(EINTR
);
1699 mutex_exit(&os
->os_upgrade_lock
);
1703 if (issig(JUSTLOOKING
) && issig(FORREAL
))
1704 return (SET_ERROR(EINTR
));
1706 objerr
= dmu_bonus_hold(os
, obj
, FTAG
, &db
);
1709 tx
= dmu_tx_create(os
);
1710 dmu_tx_hold_bonus(tx
, obj
);
1711 objerr
= dmu_tx_assign(tx
, TXG_WAIT
);
1716 dmu_buf_will_dirty(db
, tx
);
1717 dmu_buf_rele(db
, FTAG
);
1724 dmu_objset_userspace_upgrade(objset_t
*os
)
1728 if (dmu_objset_userspace_present(os
))
1730 if (dmu_objset_is_snapshot(os
))
1731 return (SET_ERROR(EINVAL
));
1732 if (!dmu_objset_userused_enabled(os
))
1733 return (SET_ERROR(ENOTSUP
));
1735 err
= dmu_objset_space_upgrade(os
);
1739 os
->os_flags
|= OBJSET_FLAG_USERACCOUNTING_COMPLETE
;
1740 txg_wait_synced(dmu_objset_pool(os
), 0);
1745 dmu_objset_userobjspace_upgrade_cb(objset_t
*os
)
1749 if (dmu_objset_userobjspace_present(os
))
1751 if (dmu_objset_is_snapshot(os
))
1752 return (SET_ERROR(EINVAL
));
1753 if (!dmu_objset_userobjused_enabled(os
))
1754 return (SET_ERROR(ENOTSUP
));
1756 dmu_objset_ds(os
)->ds_feature_activation_needed
[
1757 SPA_FEATURE_USEROBJ_ACCOUNTING
] = B_TRUE
;
1759 err
= dmu_objset_space_upgrade(os
);
1763 os
->os_flags
|= OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE
;
1764 txg_wait_synced(dmu_objset_pool(os
), 0);
1769 dmu_objset_userobjspace_upgrade(objset_t
*os
)
1771 dmu_objset_upgrade(os
, dmu_objset_userobjspace_upgrade_cb
);
1775 dmu_objset_userobjspace_upgradable(objset_t
*os
)
1777 return (dmu_objset_type(os
) == DMU_OST_ZFS
&&
1778 !dmu_objset_is_snapshot(os
) &&
1779 dmu_objset_userobjused_enabled(os
) &&
1780 !dmu_objset_userobjspace_present(os
));
1784 dmu_objset_space(objset_t
*os
, uint64_t *refdbytesp
, uint64_t *availbytesp
,
1785 uint64_t *usedobjsp
, uint64_t *availobjsp
)
1787 dsl_dataset_space(os
->os_dsl_dataset
, refdbytesp
, availbytesp
,
1788 usedobjsp
, availobjsp
);
1792 dmu_objset_fsid_guid(objset_t
*os
)
1794 return (dsl_dataset_fsid_guid(os
->os_dsl_dataset
));
1798 dmu_objset_fast_stat(objset_t
*os
, dmu_objset_stats_t
*stat
)
1800 stat
->dds_type
= os
->os_phys
->os_type
;
1801 if (os
->os_dsl_dataset
)
1802 dsl_dataset_fast_stat(os
->os_dsl_dataset
, stat
);
1806 dmu_objset_stats(objset_t
*os
, nvlist_t
*nv
)
1808 ASSERT(os
->os_dsl_dataset
||
1809 os
->os_phys
->os_type
== DMU_OST_META
);
1811 if (os
->os_dsl_dataset
!= NULL
)
1812 dsl_dataset_stats(os
->os_dsl_dataset
, nv
);
1814 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_TYPE
,
1815 os
->os_phys
->os_type
);
1816 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_USERACCOUNTING
,
1817 dmu_objset_userspace_present(os
));
1821 dmu_objset_is_snapshot(objset_t
*os
)
1823 if (os
->os_dsl_dataset
!= NULL
)
1824 return (os
->os_dsl_dataset
->ds_is_snapshot
);
1830 dmu_snapshot_realname(objset_t
*os
, char *name
, char *real
, int maxlen
,
1831 boolean_t
*conflict
)
1833 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
1836 if (dsl_dataset_phys(ds
)->ds_snapnames_zapobj
== 0)
1837 return (SET_ERROR(ENOENT
));
1839 return (zap_lookup_norm(ds
->ds_dir
->dd_pool
->dp_meta_objset
,
1840 dsl_dataset_phys(ds
)->ds_snapnames_zapobj
, name
, 8, 1, &ignored
,
1841 MT_FIRST
, real
, maxlen
, conflict
));
1845 dmu_snapshot_list_next(objset_t
*os
, int namelen
, char *name
,
1846 uint64_t *idp
, uint64_t *offp
, boolean_t
*case_conflict
)
1848 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
1849 zap_cursor_t cursor
;
1850 zap_attribute_t attr
;
1852 ASSERT(dsl_pool_config_held(dmu_objset_pool(os
)));
1854 if (dsl_dataset_phys(ds
)->ds_snapnames_zapobj
== 0)
1855 return (SET_ERROR(ENOENT
));
1857 zap_cursor_init_serialized(&cursor
,
1858 ds
->ds_dir
->dd_pool
->dp_meta_objset
,
1859 dsl_dataset_phys(ds
)->ds_snapnames_zapobj
, *offp
);
1861 if (zap_cursor_retrieve(&cursor
, &attr
) != 0) {
1862 zap_cursor_fini(&cursor
);
1863 return (SET_ERROR(ENOENT
));
1866 if (strlen(attr
.za_name
) + 1 > namelen
) {
1867 zap_cursor_fini(&cursor
);
1868 return (SET_ERROR(ENAMETOOLONG
));
1871 (void) strcpy(name
, attr
.za_name
);
1873 *idp
= attr
.za_first_integer
;
1875 *case_conflict
= attr
.za_normalization_conflict
;
1876 zap_cursor_advance(&cursor
);
1877 *offp
= zap_cursor_serialize(&cursor
);
1878 zap_cursor_fini(&cursor
);
1884 dmu_snapshot_lookup(objset_t
*os
, const char *name
, uint64_t *value
)
1886 return (dsl_dataset_snap_lookup(os
->os_dsl_dataset
, name
, value
));
1890 dmu_dir_list_next(objset_t
*os
, int namelen
, char *name
,
1891 uint64_t *idp
, uint64_t *offp
)
1893 dsl_dir_t
*dd
= os
->os_dsl_dataset
->ds_dir
;
1894 zap_cursor_t cursor
;
1895 zap_attribute_t attr
;
1897 /* there is no next dir on a snapshot! */
1898 if (os
->os_dsl_dataset
->ds_object
!=
1899 dsl_dir_phys(dd
)->dd_head_dataset_obj
)
1900 return (SET_ERROR(ENOENT
));
1902 zap_cursor_init_serialized(&cursor
,
1903 dd
->dd_pool
->dp_meta_objset
,
1904 dsl_dir_phys(dd
)->dd_child_dir_zapobj
, *offp
);
1906 if (zap_cursor_retrieve(&cursor
, &attr
) != 0) {
1907 zap_cursor_fini(&cursor
);
1908 return (SET_ERROR(ENOENT
));
1911 if (strlen(attr
.za_name
) + 1 > namelen
) {
1912 zap_cursor_fini(&cursor
);
1913 return (SET_ERROR(ENAMETOOLONG
));
1916 (void) strcpy(name
, attr
.za_name
);
1918 *idp
= attr
.za_first_integer
;
1919 zap_cursor_advance(&cursor
);
1920 *offp
= zap_cursor_serialize(&cursor
);
1921 zap_cursor_fini(&cursor
);
1926 typedef struct dmu_objset_find_ctx
{
1930 int (*dc_func
)(dsl_pool_t
*, dsl_dataset_t
*, void *);
1933 kmutex_t
*dc_error_lock
;
1935 } dmu_objset_find_ctx_t
;
1938 dmu_objset_find_dp_impl(dmu_objset_find_ctx_t
*dcp
)
1940 dsl_pool_t
*dp
= dcp
->dc_dp
;
1941 dmu_objset_find_ctx_t
*child_dcp
;
1945 zap_attribute_t
*attr
;
1949 /* don't process if there already was an error */
1950 if (*dcp
->dc_error
!= 0)
1953 err
= dsl_dir_hold_obj(dp
, dcp
->dc_ddobj
, NULL
, FTAG
, &dd
);
1957 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1958 if (dd
->dd_myname
[0] == '$') {
1959 dsl_dir_rele(dd
, FTAG
);
1963 thisobj
= dsl_dir_phys(dd
)->dd_head_dataset_obj
;
1964 attr
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
1967 * Iterate over all children.
1969 if (dcp
->dc_flags
& DS_FIND_CHILDREN
) {
1970 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
,
1971 dsl_dir_phys(dd
)->dd_child_dir_zapobj
);
1972 zap_cursor_retrieve(&zc
, attr
) == 0;
1973 (void) zap_cursor_advance(&zc
)) {
1974 ASSERT3U(attr
->za_integer_length
, ==,
1976 ASSERT3U(attr
->za_num_integers
, ==, 1);
1978 child_dcp
= kmem_alloc(sizeof (*child_dcp
), KM_SLEEP
);
1980 child_dcp
->dc_ddobj
= attr
->za_first_integer
;
1981 if (dcp
->dc_tq
!= NULL
)
1982 (void) taskq_dispatch(dcp
->dc_tq
,
1983 dmu_objset_find_dp_cb
, child_dcp
, TQ_SLEEP
);
1985 dmu_objset_find_dp_impl(child_dcp
);
1987 zap_cursor_fini(&zc
);
1991 * Iterate over all snapshots.
1993 if (dcp
->dc_flags
& DS_FIND_SNAPSHOTS
) {
1995 err
= dsl_dataset_hold_obj(dp
, thisobj
, FTAG
, &ds
);
2000 snapobj
= dsl_dataset_phys(ds
)->ds_snapnames_zapobj
;
2001 dsl_dataset_rele(ds
, FTAG
);
2003 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
, snapobj
);
2004 zap_cursor_retrieve(&zc
, attr
) == 0;
2005 (void) zap_cursor_advance(&zc
)) {
2006 ASSERT3U(attr
->za_integer_length
, ==,
2008 ASSERT3U(attr
->za_num_integers
, ==, 1);
2010 err
= dsl_dataset_hold_obj(dp
,
2011 attr
->za_first_integer
, FTAG
, &ds
);
2014 err
= dcp
->dc_func(dp
, ds
, dcp
->dc_arg
);
2015 dsl_dataset_rele(ds
, FTAG
);
2019 zap_cursor_fini(&zc
);
2023 dsl_dir_rele(dd
, FTAG
);
2024 kmem_free(attr
, sizeof (zap_attribute_t
));
2032 err
= dsl_dataset_hold_obj(dp
, thisobj
, FTAG
, &ds
);
2035 err
= dcp
->dc_func(dp
, ds
, dcp
->dc_arg
);
2036 dsl_dataset_rele(ds
, FTAG
);
2040 mutex_enter(dcp
->dc_error_lock
);
2041 /* only keep first error */
2042 if (*dcp
->dc_error
== 0)
2043 *dcp
->dc_error
= err
;
2044 mutex_exit(dcp
->dc_error_lock
);
2047 kmem_free(dcp
, sizeof (*dcp
));
2051 dmu_objset_find_dp_cb(void *arg
)
2053 dmu_objset_find_ctx_t
*dcp
= arg
;
2054 dsl_pool_t
*dp
= dcp
->dc_dp
;
2057 * We need to get a pool_config_lock here, as there are several
2058 * asssert(pool_config_held) down the stack. Getting a lock via
2059 * dsl_pool_config_enter is risky, as it might be stalled by a
2060 * pending writer. This would deadlock, as the write lock can
2061 * only be granted when our parent thread gives up the lock.
2062 * The _prio interface gives us priority over a pending writer.
2064 dsl_pool_config_enter_prio(dp
, FTAG
);
2066 dmu_objset_find_dp_impl(dcp
);
2068 dsl_pool_config_exit(dp
, FTAG
);
2072 * Find objsets under and including ddobj, call func(ds) on each.
2073 * The order for the enumeration is completely undefined.
2074 * func is called with dsl_pool_config held.
2077 dmu_objset_find_dp(dsl_pool_t
*dp
, uint64_t ddobj
,
2078 int func(dsl_pool_t
*, dsl_dataset_t
*, void *), void *arg
, int flags
)
2083 dmu_objset_find_ctx_t
*dcp
;
2086 mutex_init(&err_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
2087 dcp
= kmem_alloc(sizeof (*dcp
), KM_SLEEP
);
2090 dcp
->dc_ddobj
= ddobj
;
2091 dcp
->dc_func
= func
;
2093 dcp
->dc_flags
= flags
;
2094 dcp
->dc_error_lock
= &err_lock
;
2095 dcp
->dc_error
= &error
;
2097 if ((flags
& DS_FIND_SERIALIZE
) || dsl_pool_config_held_writer(dp
)) {
2099 * In case a write lock is held we can't make use of
2100 * parallelism, as down the stack of the worker threads
2101 * the lock is asserted via dsl_pool_config_held.
2102 * In case of a read lock this is solved by getting a read
2103 * lock in each worker thread, which isn't possible in case
2104 * of a writer lock. So we fall back to the synchronous path
2106 * In the future it might be possible to get some magic into
2107 * dsl_pool_config_held in a way that it returns true for
2108 * the worker threads so that a single lock held from this
2109 * thread suffices. For now, stay single threaded.
2111 dmu_objset_find_dp_impl(dcp
);
2112 mutex_destroy(&err_lock
);
2117 ntasks
= dmu_find_threads
;
2119 ntasks
= vdev_count_leaves(dp
->dp_spa
) * 4;
2120 tq
= taskq_create("dmu_objset_find", ntasks
, maxclsyspri
, ntasks
,
2123 kmem_free(dcp
, sizeof (*dcp
));
2124 mutex_destroy(&err_lock
);
2126 return (SET_ERROR(ENOMEM
));
2130 /* dcp will be freed by task */
2131 (void) taskq_dispatch(tq
, dmu_objset_find_dp_cb
, dcp
, TQ_SLEEP
);
2134 * PORTING: this code relies on the property of taskq_wait to wait
2135 * until no more tasks are queued and no more tasks are active. As
2136 * we always queue new tasks from within other tasks, task_wait
2137 * reliably waits for the full recursion to finish, even though we
2138 * enqueue new tasks after taskq_wait has been called.
2139 * On platforms other than illumos, taskq_wait may not have this
2144 mutex_destroy(&err_lock
);
2150 * Find all objsets under name, and for each, call 'func(child_name, arg)'.
2151 * The dp_config_rwlock must not be held when this is called, and it
2152 * will not be held when the callback is called.
2153 * Therefore this function should only be used when the pool is not changing
2154 * (e.g. in syncing context), or the callback can deal with the possible races.
2157 dmu_objset_find_impl(spa_t
*spa
, const char *name
,
2158 int func(const char *, void *), void *arg
, int flags
)
2161 dsl_pool_t
*dp
= spa_get_dsl(spa
);
2164 zap_attribute_t
*attr
;
2169 dsl_pool_config_enter(dp
, FTAG
);
2171 err
= dsl_dir_hold(dp
, name
, FTAG
, &dd
, NULL
);
2173 dsl_pool_config_exit(dp
, FTAG
);
2177 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
2178 if (dd
->dd_myname
[0] == '$') {
2179 dsl_dir_rele(dd
, FTAG
);
2180 dsl_pool_config_exit(dp
, FTAG
);
2184 thisobj
= dsl_dir_phys(dd
)->dd_head_dataset_obj
;
2185 attr
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
2188 * Iterate over all children.
2190 if (flags
& DS_FIND_CHILDREN
) {
2191 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
,
2192 dsl_dir_phys(dd
)->dd_child_dir_zapobj
);
2193 zap_cursor_retrieve(&zc
, attr
) == 0;
2194 (void) zap_cursor_advance(&zc
)) {
2195 ASSERT3U(attr
->za_integer_length
, ==,
2197 ASSERT3U(attr
->za_num_integers
, ==, 1);
2199 child
= kmem_asprintf("%s/%s", name
, attr
->za_name
);
2200 dsl_pool_config_exit(dp
, FTAG
);
2201 err
= dmu_objset_find_impl(spa
, child
,
2203 dsl_pool_config_enter(dp
, FTAG
);
2208 zap_cursor_fini(&zc
);
2211 dsl_dir_rele(dd
, FTAG
);
2212 dsl_pool_config_exit(dp
, FTAG
);
2213 kmem_free(attr
, sizeof (zap_attribute_t
));
2219 * Iterate over all snapshots.
2221 if (flags
& DS_FIND_SNAPSHOTS
) {
2222 err
= dsl_dataset_hold_obj(dp
, thisobj
, FTAG
, &ds
);
2227 snapobj
= dsl_dataset_phys(ds
)->ds_snapnames_zapobj
;
2228 dsl_dataset_rele(ds
, FTAG
);
2230 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
, snapobj
);
2231 zap_cursor_retrieve(&zc
, attr
) == 0;
2232 (void) zap_cursor_advance(&zc
)) {
2233 ASSERT3U(attr
->za_integer_length
, ==,
2235 ASSERT3U(attr
->za_num_integers
, ==, 1);
2237 child
= kmem_asprintf("%s@%s",
2238 name
, attr
->za_name
);
2239 dsl_pool_config_exit(dp
, FTAG
);
2240 err
= func(child
, arg
);
2241 dsl_pool_config_enter(dp
, FTAG
);
2246 zap_cursor_fini(&zc
);
2250 dsl_dir_rele(dd
, FTAG
);
2251 kmem_free(attr
, sizeof (zap_attribute_t
));
2252 dsl_pool_config_exit(dp
, FTAG
);
2257 /* Apply to self. */
2258 return (func(name
, arg
));
2262 * See comment above dmu_objset_find_impl().
2265 dmu_objset_find(char *name
, int func(const char *, void *), void *arg
,
2271 error
= spa_open(name
, &spa
, FTAG
);
2274 error
= dmu_objset_find_impl(spa
, name
, func
, arg
, flags
);
2275 spa_close(spa
, FTAG
);
2280 dmu_objset_set_user(objset_t
*os
, void *user_ptr
)
2282 ASSERT(MUTEX_HELD(&os
->os_user_ptr_lock
));
2283 os
->os_user_ptr
= user_ptr
;
2287 dmu_objset_get_user(objset_t
*os
)
2289 ASSERT(MUTEX_HELD(&os
->os_user_ptr_lock
));
2290 return (os
->os_user_ptr
);
2294 * Determine name of filesystem, given name of snapshot.
2295 * buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes
2298 dmu_fsname(const char *snapname
, char *buf
)
2300 char *atp
= strchr(snapname
, '@');
2302 return (SET_ERROR(EINVAL
));
2303 if (atp
- snapname
>= ZFS_MAX_DATASET_NAME_LEN
)
2304 return (SET_ERROR(ENAMETOOLONG
));
2305 (void) strlcpy(buf
, snapname
, atp
- snapname
+ 1);
2309 #if defined(_KERNEL) && defined(HAVE_SPL)
2310 EXPORT_SYMBOL(dmu_objset_zil
);
2311 EXPORT_SYMBOL(dmu_objset_pool
);
2312 EXPORT_SYMBOL(dmu_objset_ds
);
2313 EXPORT_SYMBOL(dmu_objset_type
);
2314 EXPORT_SYMBOL(dmu_objset_name
);
2315 EXPORT_SYMBOL(dmu_objset_hold
);
2316 EXPORT_SYMBOL(dmu_objset_own
);
2317 EXPORT_SYMBOL(dmu_objset_rele
);
2318 EXPORT_SYMBOL(dmu_objset_disown
);
2319 EXPORT_SYMBOL(dmu_objset_from_ds
);
2320 EXPORT_SYMBOL(dmu_objset_create
);
2321 EXPORT_SYMBOL(dmu_objset_clone
);
2322 EXPORT_SYMBOL(dmu_objset_stats
);
2323 EXPORT_SYMBOL(dmu_objset_fast_stat
);
2324 EXPORT_SYMBOL(dmu_objset_spa
);
2325 EXPORT_SYMBOL(dmu_objset_space
);
2326 EXPORT_SYMBOL(dmu_objset_fsid_guid
);
2327 EXPORT_SYMBOL(dmu_objset_find
);
2328 EXPORT_SYMBOL(dmu_objset_byteswap
);
2329 EXPORT_SYMBOL(dmu_objset_evict_dbufs
);
2330 EXPORT_SYMBOL(dmu_objset_snap_cmtime
);
2331 EXPORT_SYMBOL(dmu_objset_dnodesize
);
2333 EXPORT_SYMBOL(dmu_objset_sync
);
2334 EXPORT_SYMBOL(dmu_objset_is_dirty
);
2335 EXPORT_SYMBOL(dmu_objset_create_impl
);
2336 EXPORT_SYMBOL(dmu_objset_open_impl
);
2337 EXPORT_SYMBOL(dmu_objset_evict
);
2338 EXPORT_SYMBOL(dmu_objset_register_type
);
2339 EXPORT_SYMBOL(dmu_objset_do_userquota_updates
);
2340 EXPORT_SYMBOL(dmu_objset_userquota_get_ids
);
2341 EXPORT_SYMBOL(dmu_objset_userused_enabled
);
2342 EXPORT_SYMBOL(dmu_objset_userspace_upgrade
);
2343 EXPORT_SYMBOL(dmu_objset_userspace_present
);
2344 EXPORT_SYMBOL(dmu_objset_userobjused_enabled
);
2345 EXPORT_SYMBOL(dmu_objset_userobjspace_upgrade
);
2346 EXPORT_SYMBOL(dmu_objset_userobjspace_upgradable
);
2347 EXPORT_SYMBOL(dmu_objset_userobjspace_present
);