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, 2014 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.
29 /* Portions Copyright 2010 Robert Milkowski */
32 #include <sys/zfs_context.h>
33 #include <sys/dmu_objset.h>
34 #include <sys/dsl_dir.h>
35 #include <sys/dsl_dataset.h>
36 #include <sys/dsl_prop.h>
37 #include <sys/dsl_pool.h>
38 #include <sys/dsl_synctask.h>
39 #include <sys/dsl_deleg.h>
40 #include <sys/dnode.h>
43 #include <sys/dmu_tx.h>
46 #include <sys/dmu_impl.h>
47 #include <sys/zfs_ioctl.h>
49 #include <sys/zfs_onexit.h>
50 #include <sys/dsl_destroy.h>
53 * Needed to close a window in dnode_move() that allows the objset to be freed
54 * before it can be safely accessed.
61 rw_init(&os_lock
, NULL
, RW_DEFAULT
, NULL
);
71 dmu_objset_spa(objset_t
*os
)
77 dmu_objset_zil(objset_t
*os
)
83 dmu_objset_pool(objset_t
*os
)
87 if ((ds
= os
->os_dsl_dataset
) != NULL
&& ds
->ds_dir
)
88 return (ds
->ds_dir
->dd_pool
);
90 return (spa_get_dsl(os
->os_spa
));
94 dmu_objset_ds(objset_t
*os
)
96 return (os
->os_dsl_dataset
);
100 dmu_objset_type(objset_t
*os
)
102 return (os
->os_phys
->os_type
);
106 dmu_objset_name(objset_t
*os
, char *buf
)
108 dsl_dataset_name(os
->os_dsl_dataset
, buf
);
112 dmu_objset_id(objset_t
*os
)
114 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
116 return (ds
? ds
->ds_object
: 0);
120 dmu_objset_syncprop(objset_t
*os
)
122 return (os
->os_sync
);
126 dmu_objset_logbias(objset_t
*os
)
128 return (os
->os_logbias
);
132 checksum_changed_cb(void *arg
, uint64_t newval
)
137 * Inheritance should have been done by now.
139 ASSERT(newval
!= ZIO_CHECKSUM_INHERIT
);
141 os
->os_checksum
= zio_checksum_select(newval
, ZIO_CHECKSUM_ON_VALUE
);
145 compression_changed_cb(void *arg
, uint64_t newval
)
150 * Inheritance and range checking should have been done by now.
152 ASSERT(newval
!= ZIO_COMPRESS_INHERIT
);
154 os
->os_compress
= zio_compress_select(newval
, ZIO_COMPRESS_ON_VALUE
);
158 copies_changed_cb(void *arg
, uint64_t newval
)
163 * Inheritance and range checking should have been done by now.
166 ASSERT(newval
<= spa_max_replication(os
->os_spa
));
168 os
->os_copies
= newval
;
172 dedup_changed_cb(void *arg
, uint64_t newval
)
175 spa_t
*spa
= os
->os_spa
;
176 enum zio_checksum checksum
;
179 * Inheritance should have been done by now.
181 ASSERT(newval
!= ZIO_CHECKSUM_INHERIT
);
183 checksum
= zio_checksum_dedup_select(spa
, newval
, ZIO_CHECKSUM_OFF
);
185 os
->os_dedup_checksum
= checksum
& ZIO_CHECKSUM_MASK
;
186 os
->os_dedup_verify
= !!(checksum
& ZIO_CHECKSUM_VERIFY
);
190 primary_cache_changed_cb(void *arg
, uint64_t newval
)
195 * Inheritance and range checking should have been done by now.
197 ASSERT(newval
== ZFS_CACHE_ALL
|| newval
== ZFS_CACHE_NONE
||
198 newval
== ZFS_CACHE_METADATA
);
200 os
->os_primary_cache
= newval
;
204 secondary_cache_changed_cb(void *arg
, uint64_t newval
)
209 * Inheritance and range checking should have been done by now.
211 ASSERT(newval
== ZFS_CACHE_ALL
|| newval
== ZFS_CACHE_NONE
||
212 newval
== ZFS_CACHE_METADATA
);
214 os
->os_secondary_cache
= newval
;
218 sync_changed_cb(void *arg
, uint64_t newval
)
223 * Inheritance and range checking should have been done by now.
225 ASSERT(newval
== ZFS_SYNC_STANDARD
|| newval
== ZFS_SYNC_ALWAYS
||
226 newval
== ZFS_SYNC_DISABLED
);
228 os
->os_sync
= newval
;
230 zil_set_sync(os
->os_zil
, newval
);
234 redundant_metadata_changed_cb(void *arg
, uint64_t newval
)
239 * Inheritance and range checking should have been done by now.
241 ASSERT(newval
== ZFS_REDUNDANT_METADATA_ALL
||
242 newval
== ZFS_REDUNDANT_METADATA_MOST
);
244 os
->os_redundant_metadata
= newval
;
248 logbias_changed_cb(void *arg
, uint64_t newval
)
252 ASSERT(newval
== ZFS_LOGBIAS_LATENCY
||
253 newval
== ZFS_LOGBIAS_THROUGHPUT
);
254 os
->os_logbias
= newval
;
256 zil_set_logbias(os
->os_zil
, newval
);
260 recordsize_changed_cb(void *arg
, uint64_t newval
)
264 os
->os_recordsize
= newval
;
268 dmu_objset_byteswap(void *buf
, size_t size
)
270 objset_phys_t
*osp
= buf
;
272 ASSERT(size
== OBJSET_OLD_PHYS_SIZE
|| size
== sizeof (objset_phys_t
));
273 dnode_byteswap(&osp
->os_meta_dnode
);
274 byteswap_uint64_array(&osp
->os_zil_header
, sizeof (zil_header_t
));
275 osp
->os_type
= BSWAP_64(osp
->os_type
);
276 osp
->os_flags
= BSWAP_64(osp
->os_flags
);
277 if (size
== sizeof (objset_phys_t
)) {
278 dnode_byteswap(&osp
->os_userused_dnode
);
279 dnode_byteswap(&osp
->os_groupused_dnode
);
284 dmu_objset_open_impl(spa_t
*spa
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
290 ASSERT(ds
== NULL
|| MUTEX_HELD(&ds
->ds_opening_lock
));
292 os
= kmem_zalloc(sizeof (objset_t
), KM_SLEEP
);
293 os
->os_dsl_dataset
= ds
;
296 if (!BP_IS_HOLE(os
->os_rootbp
)) {
297 uint32_t aflags
= ARC_WAIT
;
299 SET_BOOKMARK(&zb
, ds
? ds
->ds_object
: DMU_META_OBJSET
,
300 ZB_ROOT_OBJECT
, ZB_ROOT_LEVEL
, ZB_ROOT_BLKID
);
302 if (DMU_OS_IS_L2CACHEABLE(os
))
303 aflags
|= ARC_L2CACHE
;
304 if (DMU_OS_IS_L2COMPRESSIBLE(os
))
305 aflags
|= ARC_L2COMPRESS
;
307 dprintf_bp(os
->os_rootbp
, "reading %s", "");
308 err
= arc_read(NULL
, spa
, os
->os_rootbp
,
309 arc_getbuf_func
, &os
->os_phys_buf
,
310 ZIO_PRIORITY_SYNC_READ
, ZIO_FLAG_CANFAIL
, &aflags
, &zb
);
312 kmem_free(os
, sizeof (objset_t
));
313 /* convert checksum errors into IO errors */
315 err
= SET_ERROR(EIO
);
319 /* Increase the blocksize if we are permitted. */
320 if (spa_version(spa
) >= SPA_VERSION_USERSPACE
&&
321 arc_buf_size(os
->os_phys_buf
) < sizeof (objset_phys_t
)) {
322 arc_buf_t
*buf
= arc_buf_alloc(spa
,
323 sizeof (objset_phys_t
), &os
->os_phys_buf
,
325 bzero(buf
->b_data
, sizeof (objset_phys_t
));
326 bcopy(os
->os_phys_buf
->b_data
, buf
->b_data
,
327 arc_buf_size(os
->os_phys_buf
));
328 (void) arc_buf_remove_ref(os
->os_phys_buf
,
330 os
->os_phys_buf
= buf
;
333 os
->os_phys
= os
->os_phys_buf
->b_data
;
334 os
->os_flags
= os
->os_phys
->os_flags
;
336 int size
= spa_version(spa
) >= SPA_VERSION_USERSPACE
?
337 sizeof (objset_phys_t
) : OBJSET_OLD_PHYS_SIZE
;
338 os
->os_phys_buf
= arc_buf_alloc(spa
, size
,
339 &os
->os_phys_buf
, ARC_BUFC_METADATA
);
340 os
->os_phys
= os
->os_phys_buf
->b_data
;
341 bzero(os
->os_phys
, size
);
345 * Note: the changed_cb will be called once before the register
346 * func returns, thus changing the checksum/compression from the
347 * default (fletcher2/off). Snapshots don't need to know about
348 * checksum/compression/copies.
351 err
= dsl_prop_register(ds
,
352 zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE
),
353 primary_cache_changed_cb
, os
);
355 err
= dsl_prop_register(ds
,
356 zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE
),
357 secondary_cache_changed_cb
, os
);
359 if (!ds
->ds_is_snapshot
) {
361 err
= dsl_prop_register(ds
,
362 zfs_prop_to_name(ZFS_PROP_CHECKSUM
),
363 checksum_changed_cb
, os
);
366 err
= dsl_prop_register(ds
,
367 zfs_prop_to_name(ZFS_PROP_COMPRESSION
),
368 compression_changed_cb
, os
);
371 err
= dsl_prop_register(ds
,
372 zfs_prop_to_name(ZFS_PROP_COPIES
),
373 copies_changed_cb
, os
);
376 err
= dsl_prop_register(ds
,
377 zfs_prop_to_name(ZFS_PROP_DEDUP
),
378 dedup_changed_cb
, os
);
381 err
= dsl_prop_register(ds
,
382 zfs_prop_to_name(ZFS_PROP_LOGBIAS
),
383 logbias_changed_cb
, os
);
386 err
= dsl_prop_register(ds
,
387 zfs_prop_to_name(ZFS_PROP_SYNC
),
388 sync_changed_cb
, os
);
391 err
= dsl_prop_register(ds
,
393 ZFS_PROP_REDUNDANT_METADATA
),
394 redundant_metadata_changed_cb
, os
);
397 err
= dsl_prop_register(ds
,
398 zfs_prop_to_name(ZFS_PROP_RECORDSIZE
),
399 recordsize_changed_cb
, os
);
403 VERIFY(arc_buf_remove_ref(os
->os_phys_buf
,
405 kmem_free(os
, sizeof (objset_t
));
409 /* It's the meta-objset. */
410 os
->os_checksum
= ZIO_CHECKSUM_FLETCHER_4
;
411 os
->os_compress
= ZIO_COMPRESS_LZJB
;
412 os
->os_copies
= spa_max_replication(spa
);
413 os
->os_dedup_checksum
= ZIO_CHECKSUM_OFF
;
414 os
->os_dedup_verify
= B_FALSE
;
415 os
->os_logbias
= ZFS_LOGBIAS_LATENCY
;
416 os
->os_sync
= ZFS_SYNC_STANDARD
;
417 os
->os_primary_cache
= ZFS_CACHE_ALL
;
418 os
->os_secondary_cache
= ZFS_CACHE_ALL
;
421 if (ds
== NULL
|| !ds
->ds_is_snapshot
)
422 os
->os_zil_header
= os
->os_phys
->os_zil_header
;
423 os
->os_zil
= zil_alloc(os
, &os
->os_zil_header
);
425 for (i
= 0; i
< TXG_SIZE
; i
++) {
426 list_create(&os
->os_dirty_dnodes
[i
], sizeof (dnode_t
),
427 offsetof(dnode_t
, dn_dirty_link
[i
]));
428 list_create(&os
->os_free_dnodes
[i
], sizeof (dnode_t
),
429 offsetof(dnode_t
, dn_dirty_link
[i
]));
431 list_create(&os
->os_dnodes
, sizeof (dnode_t
),
432 offsetof(dnode_t
, dn_link
));
433 list_create(&os
->os_downgraded_dbufs
, sizeof (dmu_buf_impl_t
),
434 offsetof(dmu_buf_impl_t
, db_link
));
436 list_link_init(&os
->os_evicting_node
);
438 mutex_init(&os
->os_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
439 mutex_init(&os
->os_obj_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
440 mutex_init(&os
->os_user_ptr_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
442 dnode_special_open(os
, &os
->os_phys
->os_meta_dnode
,
443 DMU_META_DNODE_OBJECT
, &os
->os_meta_dnode
);
444 if (arc_buf_size(os
->os_phys_buf
) >= sizeof (objset_phys_t
)) {
445 dnode_special_open(os
, &os
->os_phys
->os_userused_dnode
,
446 DMU_USERUSED_OBJECT
, &os
->os_userused_dnode
);
447 dnode_special_open(os
, &os
->os_phys
->os_groupused_dnode
,
448 DMU_GROUPUSED_OBJECT
, &os
->os_groupused_dnode
);
456 dmu_objset_from_ds(dsl_dataset_t
*ds
, objset_t
**osp
)
460 mutex_enter(&ds
->ds_opening_lock
);
461 if (ds
->ds_objset
== NULL
) {
463 err
= dmu_objset_open_impl(dsl_dataset_get_spa(ds
),
464 ds
, dsl_dataset_get_blkptr(ds
), &os
);
467 mutex_enter(&ds
->ds_lock
);
468 ASSERT(ds
->ds_objset
== NULL
);
470 mutex_exit(&ds
->ds_lock
);
473 *osp
= ds
->ds_objset
;
474 mutex_exit(&ds
->ds_opening_lock
);
479 * Holds the pool while the objset is held. Therefore only one objset
480 * can be held at a time.
483 dmu_objset_hold(const char *name
, void *tag
, objset_t
**osp
)
489 err
= dsl_pool_hold(name
, tag
, &dp
);
492 err
= dsl_dataset_hold(dp
, name
, tag
, &ds
);
494 dsl_pool_rele(dp
, tag
);
498 err
= dmu_objset_from_ds(ds
, osp
);
500 dsl_dataset_rele(ds
, tag
);
501 dsl_pool_rele(dp
, tag
);
508 * dsl_pool must not be held when this is called.
509 * Upon successful return, there will be a longhold on the dataset,
510 * and the dsl_pool will not be held.
513 dmu_objset_own(const char *name
, dmu_objset_type_t type
,
514 boolean_t readonly
, void *tag
, objset_t
**osp
)
520 err
= dsl_pool_hold(name
, FTAG
, &dp
);
523 err
= dsl_dataset_own(dp
, name
, tag
, &ds
);
525 dsl_pool_rele(dp
, FTAG
);
529 err
= dmu_objset_from_ds(ds
, osp
);
530 dsl_pool_rele(dp
, FTAG
);
532 dsl_dataset_disown(ds
, tag
);
533 } else if (type
!= DMU_OST_ANY
&& type
!= (*osp
)->os_phys
->os_type
) {
534 dsl_dataset_disown(ds
, tag
);
535 return (SET_ERROR(EINVAL
));
536 } else if (!readonly
&& ds
->ds_is_snapshot
) {
537 dsl_dataset_disown(ds
, tag
);
538 return (SET_ERROR(EROFS
));
544 dmu_objset_rele(objset_t
*os
, void *tag
)
546 dsl_pool_t
*dp
= dmu_objset_pool(os
);
547 dsl_dataset_rele(os
->os_dsl_dataset
, tag
);
548 dsl_pool_rele(dp
, tag
);
552 * When we are called, os MUST refer to an objset associated with a dataset
553 * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner
554 * == tag. We will then release and reacquire ownership of the dataset while
555 * holding the pool config_rwlock to avoid intervening namespace or ownership
558 * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to
559 * release the hold on its dataset and acquire a new one on the dataset of the
560 * same name so that it can be partially torn down and reconstructed.
563 dmu_objset_refresh_ownership(objset_t
*os
, void *tag
)
566 dsl_dataset_t
*ds
, *newds
;
567 char name
[MAXNAMELEN
];
569 ds
= os
->os_dsl_dataset
;
570 VERIFY3P(ds
, !=, NULL
);
571 VERIFY3P(ds
->ds_owner
, ==, tag
);
572 VERIFY(dsl_dataset_long_held(ds
));
574 dsl_dataset_name(ds
, name
);
575 dp
= dmu_objset_pool(os
);
576 dsl_pool_config_enter(dp
, FTAG
);
577 dmu_objset_disown(os
, tag
);
578 VERIFY0(dsl_dataset_own(dp
, name
, tag
, &newds
));
579 VERIFY3P(newds
, ==, os
->os_dsl_dataset
);
580 dsl_pool_config_exit(dp
, FTAG
);
584 dmu_objset_disown(objset_t
*os
, void *tag
)
586 dsl_dataset_disown(os
->os_dsl_dataset
, tag
);
590 dmu_objset_evict_dbufs(objset_t
*os
)
595 dn_marker
= kmem_alloc(sizeof (dnode_t
), KM_SLEEP
);
597 mutex_enter(&os
->os_lock
);
598 dn
= list_head(&os
->os_dnodes
);
601 * Skip dnodes without holds. We have to do this dance
602 * because dnode_add_ref() only works if there is already a
603 * hold. If the dnode has no holds, then it has no dbufs.
605 if (dnode_add_ref(dn
, FTAG
)) {
606 list_insert_after(&os
->os_dnodes
, dn
, dn_marker
);
607 mutex_exit(&os
->os_lock
);
609 dnode_evict_dbufs(dn
);
610 dnode_rele(dn
, FTAG
);
612 mutex_enter(&os
->os_lock
);
613 dn
= list_next(&os
->os_dnodes
, dn_marker
);
614 list_remove(&os
->os_dnodes
, dn_marker
);
616 dn
= list_next(&os
->os_dnodes
, dn
);
619 mutex_exit(&os
->os_lock
);
621 kmem_free(dn_marker
, sizeof (dnode_t
));
623 if (DMU_USERUSED_DNODE(os
) != NULL
) {
624 dnode_evict_dbufs(DMU_GROUPUSED_DNODE(os
));
625 dnode_evict_dbufs(DMU_USERUSED_DNODE(os
));
627 dnode_evict_dbufs(DMU_META_DNODE(os
));
631 * Objset eviction processing is split into into two pieces.
632 * The first marks the objset as evicting, evicts any dbufs that
633 * have a refcount of zero, and then queues up the objset for the
634 * second phase of eviction. Once os->os_dnodes has been cleared by
635 * dnode_buf_pageout()->dnode_destroy(), the second phase is executed.
636 * The second phase closes the special dnodes, dequeues the objset from
637 * the list of those undergoing eviction, and finally frees the objset.
639 * NOTE: Due to asynchronous eviction processing (invocation of
640 * dnode_buf_pageout()), it is possible for the meta dnode for the
641 * objset to have no holds even though os->os_dnodes is not empty.
644 dmu_objset_evict(objset_t
*os
)
648 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
650 for (t
= 0; t
< TXG_SIZE
; t
++)
651 ASSERT(!dmu_objset_is_dirty(os
, t
));
654 if (!ds
->ds_is_snapshot
) {
655 VERIFY0(dsl_prop_unregister(ds
,
656 zfs_prop_to_name(ZFS_PROP_CHECKSUM
),
657 checksum_changed_cb
, os
));
658 VERIFY0(dsl_prop_unregister(ds
,
659 zfs_prop_to_name(ZFS_PROP_COMPRESSION
),
660 compression_changed_cb
, os
));
661 VERIFY0(dsl_prop_unregister(ds
,
662 zfs_prop_to_name(ZFS_PROP_COPIES
),
663 copies_changed_cb
, os
));
664 VERIFY0(dsl_prop_unregister(ds
,
665 zfs_prop_to_name(ZFS_PROP_DEDUP
),
666 dedup_changed_cb
, os
));
667 VERIFY0(dsl_prop_unregister(ds
,
668 zfs_prop_to_name(ZFS_PROP_LOGBIAS
),
669 logbias_changed_cb
, os
));
670 VERIFY0(dsl_prop_unregister(ds
,
671 zfs_prop_to_name(ZFS_PROP_SYNC
),
672 sync_changed_cb
, os
));
673 VERIFY0(dsl_prop_unregister(ds
,
674 zfs_prop_to_name(ZFS_PROP_REDUNDANT_METADATA
),
675 redundant_metadata_changed_cb
, os
));
676 VERIFY0(dsl_prop_unregister(ds
,
677 zfs_prop_to_name(ZFS_PROP_RECORDSIZE
),
678 recordsize_changed_cb
, os
));
680 VERIFY0(dsl_prop_unregister(ds
,
681 zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE
),
682 primary_cache_changed_cb
, os
));
683 VERIFY0(dsl_prop_unregister(ds
,
684 zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE
),
685 secondary_cache_changed_cb
, os
));
691 os
->os_evicting
= B_TRUE
;
692 dmu_objset_evict_dbufs(os
);
694 mutex_enter(&os
->os_lock
);
695 spa_evicting_os_register(os
->os_spa
, os
);
696 if (list_is_empty(&os
->os_dnodes
)) {
697 mutex_exit(&os
->os_lock
);
698 dmu_objset_evict_done(os
);
700 mutex_exit(&os
->os_lock
);
705 dmu_objset_evict_done(objset_t
*os
)
707 ASSERT3P(list_head(&os
->os_dnodes
), ==, NULL
);
709 dnode_special_close(&os
->os_meta_dnode
);
710 if (DMU_USERUSED_DNODE(os
)) {
711 dnode_special_close(&os
->os_userused_dnode
);
712 dnode_special_close(&os
->os_groupused_dnode
);
714 zil_free(os
->os_zil
);
716 VERIFY(arc_buf_remove_ref(os
->os_phys_buf
, &os
->os_phys_buf
));
719 * This is a barrier to prevent the objset from going away in
720 * dnode_move() until we can safely ensure that the objset is still in
721 * use. We consider the objset valid before the barrier and invalid
724 rw_enter(&os_lock
, RW_READER
);
727 mutex_destroy(&os
->os_lock
);
728 mutex_destroy(&os
->os_obj_lock
);
729 mutex_destroy(&os
->os_user_ptr_lock
);
730 spa_evicting_os_deregister(os
->os_spa
, os
);
731 kmem_free(os
, sizeof (objset_t
));
735 dmu_objset_snap_cmtime(objset_t
*os
)
737 return (dsl_dir_snap_cmtime(os
->os_dsl_dataset
->ds_dir
));
740 /* called from dsl for meta-objset */
742 dmu_objset_create_impl(spa_t
*spa
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
743 dmu_objset_type_t type
, dmu_tx_t
*tx
)
748 ASSERT(dmu_tx_is_syncing(tx
));
751 VERIFY0(dmu_objset_from_ds(ds
, &os
));
753 VERIFY0(dmu_objset_open_impl(spa
, NULL
, bp
, &os
));
755 mdn
= DMU_META_DNODE(os
);
757 dnode_allocate(mdn
, DMU_OT_DNODE
, 1 << DNODE_BLOCK_SHIFT
,
758 DN_MAX_INDBLKSHIFT
, DMU_OT_NONE
, 0, tx
);
761 * We don't want to have to increase the meta-dnode's nlevels
762 * later, because then we could do it in quescing context while
763 * we are also accessing it in open context.
765 * This precaution is not necessary for the MOS (ds == NULL),
766 * because the MOS is only updated in syncing context.
767 * This is most fortunate: the MOS is the only objset that
768 * needs to be synced multiple times as spa_sync() iterates
769 * to convergence, so minimizing its dn_nlevels matters.
775 * Determine the number of levels necessary for the meta-dnode
776 * to contain DN_MAX_OBJECT dnodes.
778 while ((uint64_t)mdn
->dn_nblkptr
<< (mdn
->dn_datablkshift
+
779 (levels
- 1) * (mdn
->dn_indblkshift
- SPA_BLKPTRSHIFT
)) <
780 DN_MAX_OBJECT
* sizeof (dnode_phys_t
))
783 mdn
->dn_next_nlevels
[tx
->tx_txg
& TXG_MASK
] =
784 mdn
->dn_nlevels
= levels
;
787 ASSERT(type
!= DMU_OST_NONE
);
788 ASSERT(type
!= DMU_OST_ANY
);
789 ASSERT(type
< DMU_OST_NUMTYPES
);
790 os
->os_phys
->os_type
= type
;
791 if (dmu_objset_userused_enabled(os
)) {
792 os
->os_phys
->os_flags
|= OBJSET_FLAG_USERACCOUNTING_COMPLETE
;
793 os
->os_flags
= os
->os_phys
->os_flags
;
796 dsl_dataset_dirty(ds
, tx
);
801 typedef struct dmu_objset_create_arg
{
802 const char *doca_name
;
804 void (*doca_userfunc
)(objset_t
*os
, void *arg
,
805 cred_t
*cr
, dmu_tx_t
*tx
);
807 dmu_objset_type_t doca_type
;
809 } dmu_objset_create_arg_t
;
813 dmu_objset_create_check(void *arg
, dmu_tx_t
*tx
)
815 dmu_objset_create_arg_t
*doca
= arg
;
816 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
821 if (strchr(doca
->doca_name
, '@') != NULL
)
822 return (SET_ERROR(EINVAL
));
824 error
= dsl_dir_hold(dp
, doca
->doca_name
, FTAG
, &pdd
, &tail
);
828 dsl_dir_rele(pdd
, FTAG
);
829 return (SET_ERROR(EEXIST
));
831 error
= dsl_fs_ss_limit_check(pdd
, 1, ZFS_PROP_FILESYSTEM_LIMIT
, NULL
,
833 dsl_dir_rele(pdd
, FTAG
);
839 dmu_objset_create_sync(void *arg
, dmu_tx_t
*tx
)
841 dmu_objset_create_arg_t
*doca
= arg
;
842 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
850 VERIFY0(dsl_dir_hold(dp
, doca
->doca_name
, FTAG
, &pdd
, &tail
));
852 obj
= dsl_dataset_create_sync(pdd
, tail
, NULL
, doca
->doca_flags
,
853 doca
->doca_cred
, tx
);
855 VERIFY0(dsl_dataset_hold_obj(pdd
->dd_pool
, obj
, FTAG
, &ds
));
856 bp
= dsl_dataset_get_blkptr(ds
);
857 os
= dmu_objset_create_impl(pdd
->dd_pool
->dp_spa
,
858 ds
, bp
, doca
->doca_type
, tx
);
860 if (doca
->doca_userfunc
!= NULL
) {
861 doca
->doca_userfunc(os
, doca
->doca_userarg
,
862 doca
->doca_cred
, tx
);
865 spa_history_log_internal_ds(ds
, "create", tx
, "");
866 dsl_dataset_rele(ds
, FTAG
);
867 dsl_dir_rele(pdd
, FTAG
);
871 dmu_objset_create(const char *name
, dmu_objset_type_t type
, uint64_t flags
,
872 void (*func
)(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
), void *arg
)
874 dmu_objset_create_arg_t doca
;
876 doca
.doca_name
= name
;
877 doca
.doca_cred
= CRED();
878 doca
.doca_flags
= flags
;
879 doca
.doca_userfunc
= func
;
880 doca
.doca_userarg
= arg
;
881 doca
.doca_type
= type
;
883 return (dsl_sync_task(name
,
884 dmu_objset_create_check
, dmu_objset_create_sync
, &doca
,
885 5, ZFS_SPACE_CHECK_NORMAL
));
888 typedef struct dmu_objset_clone_arg
{
889 const char *doca_clone
;
890 const char *doca_origin
;
892 } dmu_objset_clone_arg_t
;
896 dmu_objset_clone_check(void *arg
, dmu_tx_t
*tx
)
898 dmu_objset_clone_arg_t
*doca
= arg
;
902 dsl_dataset_t
*origin
;
903 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
905 if (strchr(doca
->doca_clone
, '@') != NULL
)
906 return (SET_ERROR(EINVAL
));
908 error
= dsl_dir_hold(dp
, doca
->doca_clone
, FTAG
, &pdd
, &tail
);
912 dsl_dir_rele(pdd
, FTAG
);
913 return (SET_ERROR(EEXIST
));
915 /* You can't clone across pools. */
916 if (pdd
->dd_pool
!= dp
) {
917 dsl_dir_rele(pdd
, FTAG
);
918 return (SET_ERROR(EXDEV
));
920 error
= dsl_fs_ss_limit_check(pdd
, 1, ZFS_PROP_FILESYSTEM_LIMIT
, NULL
,
923 dsl_dir_rele(pdd
, FTAG
);
924 return (SET_ERROR(EDQUOT
));
926 dsl_dir_rele(pdd
, FTAG
);
928 error
= dsl_dataset_hold(dp
, doca
->doca_origin
, FTAG
, &origin
);
932 /* You can't clone across pools. */
933 if (origin
->ds_dir
->dd_pool
!= dp
) {
934 dsl_dataset_rele(origin
, FTAG
);
935 return (SET_ERROR(EXDEV
));
938 /* You can only clone snapshots, not the head datasets. */
939 if (!origin
->ds_is_snapshot
) {
940 dsl_dataset_rele(origin
, FTAG
);
941 return (SET_ERROR(EINVAL
));
943 dsl_dataset_rele(origin
, FTAG
);
949 dmu_objset_clone_sync(void *arg
, dmu_tx_t
*tx
)
951 dmu_objset_clone_arg_t
*doca
= arg
;
952 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
955 dsl_dataset_t
*origin
, *ds
;
957 char namebuf
[MAXNAMELEN
];
959 VERIFY0(dsl_dir_hold(dp
, doca
->doca_clone
, FTAG
, &pdd
, &tail
));
960 VERIFY0(dsl_dataset_hold(dp
, doca
->doca_origin
, FTAG
, &origin
));
962 obj
= dsl_dataset_create_sync(pdd
, tail
, origin
, 0,
963 doca
->doca_cred
, tx
);
965 VERIFY0(dsl_dataset_hold_obj(pdd
->dd_pool
, obj
, FTAG
, &ds
));
966 dsl_dataset_name(origin
, namebuf
);
967 spa_history_log_internal_ds(ds
, "clone", tx
,
968 "origin=%s (%llu)", namebuf
, origin
->ds_object
);
969 dsl_dataset_rele(ds
, FTAG
);
970 dsl_dataset_rele(origin
, FTAG
);
971 dsl_dir_rele(pdd
, FTAG
);
975 dmu_objset_clone(const char *clone
, const char *origin
)
977 dmu_objset_clone_arg_t doca
;
979 doca
.doca_clone
= clone
;
980 doca
.doca_origin
= origin
;
981 doca
.doca_cred
= CRED();
983 return (dsl_sync_task(clone
,
984 dmu_objset_clone_check
, dmu_objset_clone_sync
, &doca
,
985 5, ZFS_SPACE_CHECK_NORMAL
));
989 dmu_objset_snapshot_one(const char *fsname
, const char *snapname
)
992 char *longsnap
= kmem_asprintf("%s@%s", fsname
, snapname
);
993 nvlist_t
*snaps
= fnvlist_alloc();
995 fnvlist_add_boolean(snaps
, longsnap
);
997 err
= dsl_dataset_snapshot(snaps
, NULL
, NULL
);
1003 dmu_objset_sync_dnodes(list_t
*list
, list_t
*newlist
, dmu_tx_t
*tx
)
1007 while ((dn
= list_head(list
))) {
1008 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
);
1009 ASSERT(dn
->dn_dbuf
->db_data_pending
);
1011 * Initialize dn_zio outside dnode_sync() because the
1012 * meta-dnode needs to set it ouside dnode_sync().
1014 dn
->dn_zio
= dn
->dn_dbuf
->db_data_pending
->dr_zio
;
1017 ASSERT3U(dn
->dn_nlevels
, <=, DN_MAX_LEVELS
);
1018 list_remove(list
, dn
);
1021 (void) dnode_add_ref(dn
, newlist
);
1022 list_insert_tail(newlist
, dn
);
1031 dmu_objset_write_ready(zio_t
*zio
, arc_buf_t
*abuf
, void *arg
)
1035 blkptr_t
*bp
= zio
->io_bp
;
1037 dnode_phys_t
*dnp
= &os
->os_phys
->os_meta_dnode
;
1039 ASSERT(!BP_IS_EMBEDDED(bp
));
1040 ASSERT3P(bp
, ==, os
->os_rootbp
);
1041 ASSERT3U(BP_GET_TYPE(bp
), ==, DMU_OT_OBJSET
);
1042 ASSERT0(BP_GET_LEVEL(bp
));
1045 * Update rootbp fill count: it should be the number of objects
1046 * allocated in the object set (not counting the "special"
1047 * objects that are stored in the objset_phys_t -- the meta
1048 * dnode and user/group accounting objects).
1051 for (i
= 0; i
< dnp
->dn_nblkptr
; i
++)
1052 bp
->blk_fill
+= BP_GET_FILL(&dnp
->dn_blkptr
[i
]);
1057 dmu_objset_write_done(zio_t
*zio
, arc_buf_t
*abuf
, void *arg
)
1059 blkptr_t
*bp
= zio
->io_bp
;
1060 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
1063 if (zio
->io_flags
& ZIO_FLAG_IO_REWRITE
) {
1064 ASSERT(BP_EQUAL(bp
, bp_orig
));
1066 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
1067 dmu_tx_t
*tx
= os
->os_synctx
;
1069 (void) dsl_dataset_block_kill(ds
, bp_orig
, tx
, B_TRUE
);
1070 dsl_dataset_block_born(ds
, bp
, tx
);
1074 /* called from dsl */
1076 dmu_objset_sync(objset_t
*os
, zio_t
*pio
, dmu_tx_t
*tx
)
1079 zbookmark_phys_t zb
;
1083 list_t
*newlist
= NULL
;
1084 dbuf_dirty_record_t
*dr
;
1086 dprintf_ds(os
->os_dsl_dataset
, "txg=%llu\n", tx
->tx_txg
);
1088 ASSERT(dmu_tx_is_syncing(tx
));
1089 /* XXX the write_done callback should really give us the tx... */
1092 if (os
->os_dsl_dataset
== NULL
) {
1094 * This is the MOS. If we have upgraded,
1095 * spa_max_replication() could change, so reset
1098 os
->os_copies
= spa_max_replication(os
->os_spa
);
1102 * Create the root block IO
1104 SET_BOOKMARK(&zb
, os
->os_dsl_dataset
?
1105 os
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
1106 ZB_ROOT_OBJECT
, ZB_ROOT_LEVEL
, ZB_ROOT_BLKID
);
1107 arc_release(os
->os_phys_buf
, &os
->os_phys_buf
);
1109 dmu_write_policy(os
, NULL
, 0, 0, &zp
);
1111 zio
= arc_write(pio
, os
->os_spa
, tx
->tx_txg
,
1112 os
->os_rootbp
, os
->os_phys_buf
, DMU_OS_IS_L2CACHEABLE(os
),
1113 DMU_OS_IS_L2COMPRESSIBLE(os
), &zp
, dmu_objset_write_ready
,
1114 NULL
, dmu_objset_write_done
, os
, ZIO_PRIORITY_ASYNC_WRITE
,
1115 ZIO_FLAG_MUSTSUCCEED
, &zb
);
1118 * Sync special dnodes - the parent IO for the sync is the root block
1120 DMU_META_DNODE(os
)->dn_zio
= zio
;
1121 dnode_sync(DMU_META_DNODE(os
), tx
);
1123 os
->os_phys
->os_flags
= os
->os_flags
;
1125 if (DMU_USERUSED_DNODE(os
) &&
1126 DMU_USERUSED_DNODE(os
)->dn_type
!= DMU_OT_NONE
) {
1127 DMU_USERUSED_DNODE(os
)->dn_zio
= zio
;
1128 dnode_sync(DMU_USERUSED_DNODE(os
), tx
);
1129 DMU_GROUPUSED_DNODE(os
)->dn_zio
= zio
;
1130 dnode_sync(DMU_GROUPUSED_DNODE(os
), tx
);
1133 txgoff
= tx
->tx_txg
& TXG_MASK
;
1135 if (dmu_objset_userused_enabled(os
)) {
1136 newlist
= &os
->os_synced_dnodes
;
1138 * We must create the list here because it uses the
1139 * dn_dirty_link[] of this txg.
1141 list_create(newlist
, sizeof (dnode_t
),
1142 offsetof(dnode_t
, dn_dirty_link
[txgoff
]));
1145 dmu_objset_sync_dnodes(&os
->os_free_dnodes
[txgoff
], newlist
, tx
);
1146 dmu_objset_sync_dnodes(&os
->os_dirty_dnodes
[txgoff
], newlist
, tx
);
1148 list
= &DMU_META_DNODE(os
)->dn_dirty_records
[txgoff
];
1149 while ((dr
= list_head(list
))) {
1150 ASSERT0(dr
->dr_dbuf
->db_level
);
1151 list_remove(list
, dr
);
1153 zio_nowait(dr
->dr_zio
);
1156 * Free intent log blocks up to this tx.
1158 zil_sync(os
->os_zil
, tx
);
1159 os
->os_phys
->os_zil_header
= os
->os_zil_header
;
1164 dmu_objset_is_dirty(objset_t
*os
, uint64_t txg
)
1166 return (!list_is_empty(&os
->os_dirty_dnodes
[txg
& TXG_MASK
]) ||
1167 !list_is_empty(&os
->os_free_dnodes
[txg
& TXG_MASK
]));
1170 static objset_used_cb_t
*used_cbs
[DMU_OST_NUMTYPES
];
1173 dmu_objset_register_type(dmu_objset_type_t ost
, objset_used_cb_t
*cb
)
1179 dmu_objset_userused_enabled(objset_t
*os
)
1181 return (spa_version(os
->os_spa
) >= SPA_VERSION_USERSPACE
&&
1182 used_cbs
[os
->os_phys
->os_type
] != NULL
&&
1183 DMU_USERUSED_DNODE(os
) != NULL
);
1187 do_userquota_update(objset_t
*os
, uint64_t used
, uint64_t flags
,
1188 uint64_t user
, uint64_t group
, boolean_t subtract
, dmu_tx_t
*tx
)
1190 if ((flags
& DNODE_FLAG_USERUSED_ACCOUNTED
)) {
1191 int64_t delta
= DNODE_SIZE
+ used
;
1194 VERIFY3U(0, ==, zap_increment_int(os
, DMU_USERUSED_OBJECT
,
1196 VERIFY3U(0, ==, zap_increment_int(os
, DMU_GROUPUSED_OBJECT
,
1202 dmu_objset_do_userquota_updates(objset_t
*os
, dmu_tx_t
*tx
)
1205 list_t
*list
= &os
->os_synced_dnodes
;
1207 ASSERT(list_head(list
) == NULL
|| dmu_objset_userused_enabled(os
));
1209 while ((dn
= list_head(list
))) {
1211 ASSERT(!DMU_OBJECT_IS_SPECIAL(dn
->dn_object
));
1212 ASSERT(dn
->dn_phys
->dn_type
== DMU_OT_NONE
||
1213 dn
->dn_phys
->dn_flags
&
1214 DNODE_FLAG_USERUSED_ACCOUNTED
);
1216 /* Allocate the user/groupused objects if necessary. */
1217 if (DMU_USERUSED_DNODE(os
)->dn_type
== DMU_OT_NONE
) {
1218 VERIFY(0 == zap_create_claim(os
,
1219 DMU_USERUSED_OBJECT
,
1220 DMU_OT_USERGROUP_USED
, DMU_OT_NONE
, 0, tx
));
1221 VERIFY(0 == zap_create_claim(os
,
1222 DMU_GROUPUSED_OBJECT
,
1223 DMU_OT_USERGROUP_USED
, DMU_OT_NONE
, 0, tx
));
1227 * We intentionally modify the zap object even if the
1228 * net delta is zero. Otherwise
1229 * the block of the zap obj could be shared between
1230 * datasets but need to be different between them after
1234 flags
= dn
->dn_id_flags
;
1236 if (flags
& DN_ID_OLD_EXIST
) {
1237 do_userquota_update(os
, dn
->dn_oldused
, dn
->dn_oldflags
,
1238 dn
->dn_olduid
, dn
->dn_oldgid
, B_TRUE
, tx
);
1240 if (flags
& DN_ID_NEW_EXIST
) {
1241 do_userquota_update(os
, DN_USED_BYTES(dn
->dn_phys
),
1242 dn
->dn_phys
->dn_flags
, dn
->dn_newuid
,
1243 dn
->dn_newgid
, B_FALSE
, tx
);
1246 mutex_enter(&dn
->dn_mtx
);
1248 dn
->dn_oldflags
= 0;
1249 if (dn
->dn_id_flags
& DN_ID_NEW_EXIST
) {
1250 dn
->dn_olduid
= dn
->dn_newuid
;
1251 dn
->dn_oldgid
= dn
->dn_newgid
;
1252 dn
->dn_id_flags
|= DN_ID_OLD_EXIST
;
1253 if (dn
->dn_bonuslen
== 0)
1254 dn
->dn_id_flags
|= DN_ID_CHKED_SPILL
;
1256 dn
->dn_id_flags
|= DN_ID_CHKED_BONUS
;
1258 dn
->dn_id_flags
&= ~(DN_ID_NEW_EXIST
);
1259 mutex_exit(&dn
->dn_mtx
);
1261 list_remove(list
, dn
);
1262 dnode_rele(dn
, list
);
1267 * Returns a pointer to data to find uid/gid from
1269 * If a dirty record for transaction group that is syncing can't
1270 * be found then NULL is returned. In the NULL case it is assumed
1271 * the uid/gid aren't changing.
1274 dmu_objset_userquota_find_data(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1276 dbuf_dirty_record_t
*dr
, **drp
;
1279 if (db
->db_dirtycnt
== 0)
1280 return (db
->db
.db_data
); /* Nothing is changing */
1282 for (drp
= &db
->db_last_dirty
; (dr
= *drp
) != NULL
; drp
= &dr
->dr_next
)
1283 if (dr
->dr_txg
== tx
->tx_txg
)
1291 DB_DNODE_ENTER(dr
->dr_dbuf
);
1292 dn
= DB_DNODE(dr
->dr_dbuf
);
1294 if (dn
->dn_bonuslen
== 0 &&
1295 dr
->dr_dbuf
->db_blkid
== DMU_SPILL_BLKID
)
1296 data
= dr
->dt
.dl
.dr_data
->b_data
;
1298 data
= dr
->dt
.dl
.dr_data
;
1300 DB_DNODE_EXIT(dr
->dr_dbuf
);
1307 dmu_objset_userquota_get_ids(dnode_t
*dn
, boolean_t before
, dmu_tx_t
*tx
)
1309 objset_t
*os
= dn
->dn_objset
;
1311 dmu_buf_impl_t
*db
= NULL
;
1312 uint64_t *user
= NULL
;
1313 uint64_t *group
= NULL
;
1314 int flags
= dn
->dn_id_flags
;
1316 boolean_t have_spill
= B_FALSE
;
1317 boolean_t have_bonus
= B_FALSE
;
1319 if (!dmu_objset_userused_enabled(dn
->dn_objset
))
1322 if (before
&& (flags
& (DN_ID_CHKED_BONUS
|DN_ID_OLD_EXIST
|
1323 DN_ID_CHKED_SPILL
)))
1326 if (before
&& dn
->dn_bonuslen
!= 0)
1327 data
= DN_BONUS(dn
->dn_phys
);
1328 else if (!before
&& dn
->dn_bonuslen
!= 0) {
1331 if (!RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
1332 have_bonus
= dbuf_try_add_ref((dmu_buf_t
*)db
,
1333 dn
->dn_objset
, dn
->dn_object
,
1334 DMU_BONUS_BLKID
, FTAG
);
1337 * The hold will fail if the buffer is
1338 * being evicted due to unlink, in which
1339 * case nothing needs to be done.
1344 mutex_enter(&db
->db_mtx
);
1345 data
= dmu_objset_userquota_find_data(db
, tx
);
1347 data
= DN_BONUS(dn
->dn_phys
);
1349 } else if (dn
->dn_bonuslen
== 0 && dn
->dn_bonustype
== DMU_OT_SA
) {
1352 if (RW_WRITE_HELD(&dn
->dn_struct_rwlock
))
1353 rf
|= DB_RF_HAVESTRUCT
;
1354 error
= dmu_spill_hold_by_dnode(dn
,
1355 rf
| DB_RF_MUST_SUCCEED
,
1356 FTAG
, (dmu_buf_t
**)&db
);
1358 mutex_enter(&db
->db_mtx
);
1359 data
= (before
) ? db
->db
.db_data
:
1360 dmu_objset_userquota_find_data(db
, tx
);
1361 have_spill
= B_TRUE
;
1363 mutex_enter(&dn
->dn_mtx
);
1364 dn
->dn_id_flags
|= DN_ID_CHKED_BONUS
;
1365 mutex_exit(&dn
->dn_mtx
);
1371 user
= &dn
->dn_olduid
;
1372 group
= &dn
->dn_oldgid
;
1374 user
= &dn
->dn_newuid
;
1375 group
= &dn
->dn_newgid
;
1379 * Must always call the callback in case the object
1380 * type has changed and that type isn't an object type to track
1382 error
= used_cbs
[os
->os_phys
->os_type
](dn
->dn_bonustype
, data
,
1386 * Preserve existing uid/gid when the callback can't determine
1387 * what the new uid/gid are and the callback returned EEXIST.
1388 * The EEXIST error tells us to just use the existing uid/gid.
1389 * If we don't know what the old values are then just assign
1390 * them to 0, since that is a new file being created.
1392 if (!before
&& data
== NULL
&& error
== EEXIST
) {
1393 if (flags
& DN_ID_OLD_EXIST
) {
1394 dn
->dn_newuid
= dn
->dn_olduid
;
1395 dn
->dn_newgid
= dn
->dn_oldgid
;
1404 mutex_exit(&db
->db_mtx
);
1406 mutex_enter(&dn
->dn_mtx
);
1407 if (error
== 0 && before
)
1408 dn
->dn_id_flags
|= DN_ID_OLD_EXIST
;
1409 if (error
== 0 && !before
)
1410 dn
->dn_id_flags
|= DN_ID_NEW_EXIST
;
1413 dn
->dn_id_flags
|= DN_ID_CHKED_SPILL
;
1415 dn
->dn_id_flags
|= DN_ID_CHKED_BONUS
;
1417 mutex_exit(&dn
->dn_mtx
);
1418 if (have_spill
|| have_bonus
)
1419 dmu_buf_rele((dmu_buf_t
*)db
, FTAG
);
1423 dmu_objset_userspace_present(objset_t
*os
)
1425 return (os
->os_phys
->os_flags
&
1426 OBJSET_FLAG_USERACCOUNTING_COMPLETE
);
1430 dmu_objset_userspace_upgrade(objset_t
*os
)
1435 if (dmu_objset_userspace_present(os
))
1437 if (!dmu_objset_userused_enabled(os
))
1438 return (SET_ERROR(ENOTSUP
));
1439 if (dmu_objset_is_snapshot(os
))
1440 return (SET_ERROR(EINVAL
));
1443 * We simply need to mark every object dirty, so that it will be
1444 * synced out and now accounted. If this is called
1445 * concurrently, or if we already did some work before crashing,
1446 * that's fine, since we track each object's accounted state
1450 for (obj
= 0; err
== 0; err
= dmu_object_next(os
, &obj
, FALSE
, 0)) {
1455 if (issig(JUSTLOOKING
) && issig(FORREAL
))
1456 return (SET_ERROR(EINTR
));
1458 objerr
= dmu_bonus_hold(os
, obj
, FTAG
, &db
);
1461 tx
= dmu_tx_create(os
);
1462 dmu_tx_hold_bonus(tx
, obj
);
1463 objerr
= dmu_tx_assign(tx
, TXG_WAIT
);
1468 dmu_buf_will_dirty(db
, tx
);
1469 dmu_buf_rele(db
, FTAG
);
1473 os
->os_flags
|= OBJSET_FLAG_USERACCOUNTING_COMPLETE
;
1474 txg_wait_synced(dmu_objset_pool(os
), 0);
1479 dmu_objset_space(objset_t
*os
, uint64_t *refdbytesp
, uint64_t *availbytesp
,
1480 uint64_t *usedobjsp
, uint64_t *availobjsp
)
1482 dsl_dataset_space(os
->os_dsl_dataset
, refdbytesp
, availbytesp
,
1483 usedobjsp
, availobjsp
);
1487 dmu_objset_fsid_guid(objset_t
*os
)
1489 return (dsl_dataset_fsid_guid(os
->os_dsl_dataset
));
1493 dmu_objset_fast_stat(objset_t
*os
, dmu_objset_stats_t
*stat
)
1495 stat
->dds_type
= os
->os_phys
->os_type
;
1496 if (os
->os_dsl_dataset
)
1497 dsl_dataset_fast_stat(os
->os_dsl_dataset
, stat
);
1501 dmu_objset_stats(objset_t
*os
, nvlist_t
*nv
)
1503 ASSERT(os
->os_dsl_dataset
||
1504 os
->os_phys
->os_type
== DMU_OST_META
);
1506 if (os
->os_dsl_dataset
!= NULL
)
1507 dsl_dataset_stats(os
->os_dsl_dataset
, nv
);
1509 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_TYPE
,
1510 os
->os_phys
->os_type
);
1511 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_USERACCOUNTING
,
1512 dmu_objset_userspace_present(os
));
1516 dmu_objset_is_snapshot(objset_t
*os
)
1518 if (os
->os_dsl_dataset
!= NULL
)
1519 return (os
->os_dsl_dataset
->ds_is_snapshot
);
1525 dmu_snapshot_realname(objset_t
*os
, char *name
, char *real
, int maxlen
,
1526 boolean_t
*conflict
)
1528 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
1531 if (dsl_dataset_phys(ds
)->ds_snapnames_zapobj
== 0)
1532 return (SET_ERROR(ENOENT
));
1534 return (zap_lookup_norm(ds
->ds_dir
->dd_pool
->dp_meta_objset
,
1535 dsl_dataset_phys(ds
)->ds_snapnames_zapobj
, name
, 8, 1, &ignored
,
1536 MT_FIRST
, real
, maxlen
, conflict
));
1540 dmu_snapshot_list_next(objset_t
*os
, int namelen
, char *name
,
1541 uint64_t *idp
, uint64_t *offp
, boolean_t
*case_conflict
)
1543 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
1544 zap_cursor_t cursor
;
1545 zap_attribute_t attr
;
1547 ASSERT(dsl_pool_config_held(dmu_objset_pool(os
)));
1549 if (dsl_dataset_phys(ds
)->ds_snapnames_zapobj
== 0)
1550 return (SET_ERROR(ENOENT
));
1552 zap_cursor_init_serialized(&cursor
,
1553 ds
->ds_dir
->dd_pool
->dp_meta_objset
,
1554 dsl_dataset_phys(ds
)->ds_snapnames_zapobj
, *offp
);
1556 if (zap_cursor_retrieve(&cursor
, &attr
) != 0) {
1557 zap_cursor_fini(&cursor
);
1558 return (SET_ERROR(ENOENT
));
1561 if (strlen(attr
.za_name
) + 1 > namelen
) {
1562 zap_cursor_fini(&cursor
);
1563 return (SET_ERROR(ENAMETOOLONG
));
1566 (void) strcpy(name
, attr
.za_name
);
1568 *idp
= attr
.za_first_integer
;
1570 *case_conflict
= attr
.za_normalization_conflict
;
1571 zap_cursor_advance(&cursor
);
1572 *offp
= zap_cursor_serialize(&cursor
);
1573 zap_cursor_fini(&cursor
);
1579 dmu_snapshot_lookup(objset_t
*os
, const char *name
, uint64_t *value
)
1581 return (dsl_dataset_snap_lookup(os
->os_dsl_dataset
, name
, value
));
1585 dmu_dir_list_next(objset_t
*os
, int namelen
, char *name
,
1586 uint64_t *idp
, uint64_t *offp
)
1588 dsl_dir_t
*dd
= os
->os_dsl_dataset
->ds_dir
;
1589 zap_cursor_t cursor
;
1590 zap_attribute_t attr
;
1592 /* there is no next dir on a snapshot! */
1593 if (os
->os_dsl_dataset
->ds_object
!=
1594 dsl_dir_phys(dd
)->dd_head_dataset_obj
)
1595 return (SET_ERROR(ENOENT
));
1597 zap_cursor_init_serialized(&cursor
,
1598 dd
->dd_pool
->dp_meta_objset
,
1599 dsl_dir_phys(dd
)->dd_child_dir_zapobj
, *offp
);
1601 if (zap_cursor_retrieve(&cursor
, &attr
) != 0) {
1602 zap_cursor_fini(&cursor
);
1603 return (SET_ERROR(ENOENT
));
1606 if (strlen(attr
.za_name
) + 1 > namelen
) {
1607 zap_cursor_fini(&cursor
);
1608 return (SET_ERROR(ENAMETOOLONG
));
1611 (void) strcpy(name
, attr
.za_name
);
1613 *idp
= attr
.za_first_integer
;
1614 zap_cursor_advance(&cursor
);
1615 *offp
= zap_cursor_serialize(&cursor
);
1616 zap_cursor_fini(&cursor
);
1622 * Find objsets under and including ddobj, call func(ds) on each.
1625 dmu_objset_find_dp(dsl_pool_t
*dp
, uint64_t ddobj
,
1626 int func(dsl_pool_t
*, dsl_dataset_t
*, void *), void *arg
, int flags
)
1631 zap_attribute_t
*attr
;
1635 ASSERT(dsl_pool_config_held(dp
));
1637 err
= dsl_dir_hold_obj(dp
, ddobj
, NULL
, FTAG
, &dd
);
1641 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1642 if (dd
->dd_myname
[0] == '$') {
1643 dsl_dir_rele(dd
, FTAG
);
1647 thisobj
= dsl_dir_phys(dd
)->dd_head_dataset_obj
;
1648 attr
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
1651 * Iterate over all children.
1653 if (flags
& DS_FIND_CHILDREN
) {
1654 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
,
1655 dsl_dir_phys(dd
)->dd_child_dir_zapobj
);
1656 zap_cursor_retrieve(&zc
, attr
) == 0;
1657 (void) zap_cursor_advance(&zc
)) {
1658 ASSERT3U(attr
->za_integer_length
, ==,
1660 ASSERT3U(attr
->za_num_integers
, ==, 1);
1662 err
= dmu_objset_find_dp(dp
, attr
->za_first_integer
,
1667 zap_cursor_fini(&zc
);
1670 dsl_dir_rele(dd
, FTAG
);
1671 kmem_free(attr
, sizeof (zap_attribute_t
));
1677 * Iterate over all snapshots.
1679 if (flags
& DS_FIND_SNAPSHOTS
) {
1681 err
= dsl_dataset_hold_obj(dp
, thisobj
, FTAG
, &ds
);
1686 snapobj
= dsl_dataset_phys(ds
)->ds_snapnames_zapobj
;
1687 dsl_dataset_rele(ds
, FTAG
);
1689 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
, snapobj
);
1690 zap_cursor_retrieve(&zc
, attr
) == 0;
1691 (void) zap_cursor_advance(&zc
)) {
1692 ASSERT3U(attr
->za_integer_length
, ==,
1694 ASSERT3U(attr
->za_num_integers
, ==, 1);
1696 err
= dsl_dataset_hold_obj(dp
,
1697 attr
->za_first_integer
, FTAG
, &ds
);
1700 err
= func(dp
, ds
, arg
);
1701 dsl_dataset_rele(ds
, FTAG
);
1705 zap_cursor_fini(&zc
);
1709 dsl_dir_rele(dd
, FTAG
);
1710 kmem_free(attr
, sizeof (zap_attribute_t
));
1718 err
= dsl_dataset_hold_obj(dp
, thisobj
, FTAG
, &ds
);
1721 err
= func(dp
, ds
, arg
);
1722 dsl_dataset_rele(ds
, FTAG
);
1727 * Find all objsets under name, and for each, call 'func(child_name, arg)'.
1728 * The dp_config_rwlock must not be held when this is called, and it
1729 * will not be held when the callback is called.
1730 * Therefore this function should only be used when the pool is not changing
1731 * (e.g. in syncing context), or the callback can deal with the possible races.
1734 dmu_objset_find_impl(spa_t
*spa
, const char *name
,
1735 int func(const char *, void *), void *arg
, int flags
)
1738 dsl_pool_t
*dp
= spa_get_dsl(spa
);
1741 zap_attribute_t
*attr
;
1746 dsl_pool_config_enter(dp
, FTAG
);
1748 err
= dsl_dir_hold(dp
, name
, FTAG
, &dd
, NULL
);
1750 dsl_pool_config_exit(dp
, FTAG
);
1754 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1755 if (dd
->dd_myname
[0] == '$') {
1756 dsl_dir_rele(dd
, FTAG
);
1757 dsl_pool_config_exit(dp
, FTAG
);
1761 thisobj
= dsl_dir_phys(dd
)->dd_head_dataset_obj
;
1762 attr
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
1765 * Iterate over all children.
1767 if (flags
& DS_FIND_CHILDREN
) {
1768 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
,
1769 dsl_dir_phys(dd
)->dd_child_dir_zapobj
);
1770 zap_cursor_retrieve(&zc
, attr
) == 0;
1771 (void) zap_cursor_advance(&zc
)) {
1772 ASSERT3U(attr
->za_integer_length
, ==,
1774 ASSERT3U(attr
->za_num_integers
, ==, 1);
1776 child
= kmem_asprintf("%s/%s", name
, attr
->za_name
);
1777 dsl_pool_config_exit(dp
, FTAG
);
1778 err
= dmu_objset_find_impl(spa
, child
,
1780 dsl_pool_config_enter(dp
, FTAG
);
1785 zap_cursor_fini(&zc
);
1788 dsl_dir_rele(dd
, FTAG
);
1789 dsl_pool_config_exit(dp
, FTAG
);
1790 kmem_free(attr
, sizeof (zap_attribute_t
));
1796 * Iterate over all snapshots.
1798 if (flags
& DS_FIND_SNAPSHOTS
) {
1799 err
= dsl_dataset_hold_obj(dp
, thisobj
, FTAG
, &ds
);
1804 snapobj
= dsl_dataset_phys(ds
)->ds_snapnames_zapobj
;
1805 dsl_dataset_rele(ds
, FTAG
);
1807 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
, snapobj
);
1808 zap_cursor_retrieve(&zc
, attr
) == 0;
1809 (void) zap_cursor_advance(&zc
)) {
1810 ASSERT3U(attr
->za_integer_length
, ==,
1812 ASSERT3U(attr
->za_num_integers
, ==, 1);
1814 child
= kmem_asprintf("%s@%s",
1815 name
, attr
->za_name
);
1816 dsl_pool_config_exit(dp
, FTAG
);
1817 err
= func(child
, arg
);
1818 dsl_pool_config_enter(dp
, FTAG
);
1823 zap_cursor_fini(&zc
);
1827 dsl_dir_rele(dd
, FTAG
);
1828 kmem_free(attr
, sizeof (zap_attribute_t
));
1829 dsl_pool_config_exit(dp
, FTAG
);
1834 /* Apply to self. */
1835 return (func(name
, arg
));
1839 * See comment above dmu_objset_find_impl().
1842 dmu_objset_find(char *name
, int func(const char *, void *), void *arg
,
1848 error
= spa_open(name
, &spa
, FTAG
);
1851 error
= dmu_objset_find_impl(spa
, name
, func
, arg
, flags
);
1852 spa_close(spa
, FTAG
);
1857 dmu_objset_set_user(objset_t
*os
, void *user_ptr
)
1859 ASSERT(MUTEX_HELD(&os
->os_user_ptr_lock
));
1860 os
->os_user_ptr
= user_ptr
;
1864 dmu_objset_get_user(objset_t
*os
)
1866 ASSERT(MUTEX_HELD(&os
->os_user_ptr_lock
));
1867 return (os
->os_user_ptr
);
1871 * Determine name of filesystem, given name of snapshot.
1872 * buf must be at least MAXNAMELEN bytes
1875 dmu_fsname(const char *snapname
, char *buf
)
1877 char *atp
= strchr(snapname
, '@');
1879 return (SET_ERROR(EINVAL
));
1880 if (atp
- snapname
>= MAXNAMELEN
)
1881 return (SET_ERROR(ENAMETOOLONG
));
1882 (void) strlcpy(buf
, snapname
, atp
- snapname
+ 1);
1886 #if defined(_KERNEL) && defined(HAVE_SPL)
1887 EXPORT_SYMBOL(dmu_objset_zil
);
1888 EXPORT_SYMBOL(dmu_objset_pool
);
1889 EXPORT_SYMBOL(dmu_objset_ds
);
1890 EXPORT_SYMBOL(dmu_objset_type
);
1891 EXPORT_SYMBOL(dmu_objset_name
);
1892 EXPORT_SYMBOL(dmu_objset_hold
);
1893 EXPORT_SYMBOL(dmu_objset_own
);
1894 EXPORT_SYMBOL(dmu_objset_rele
);
1895 EXPORT_SYMBOL(dmu_objset_disown
);
1896 EXPORT_SYMBOL(dmu_objset_from_ds
);
1897 EXPORT_SYMBOL(dmu_objset_create
);
1898 EXPORT_SYMBOL(dmu_objset_clone
);
1899 EXPORT_SYMBOL(dmu_objset_stats
);
1900 EXPORT_SYMBOL(dmu_objset_fast_stat
);
1901 EXPORT_SYMBOL(dmu_objset_spa
);
1902 EXPORT_SYMBOL(dmu_objset_space
);
1903 EXPORT_SYMBOL(dmu_objset_fsid_guid
);
1904 EXPORT_SYMBOL(dmu_objset_find
);
1905 EXPORT_SYMBOL(dmu_objset_byteswap
);
1906 EXPORT_SYMBOL(dmu_objset_evict_dbufs
);
1907 EXPORT_SYMBOL(dmu_objset_snap_cmtime
);
1909 EXPORT_SYMBOL(dmu_objset_sync
);
1910 EXPORT_SYMBOL(dmu_objset_is_dirty
);
1911 EXPORT_SYMBOL(dmu_objset_create_impl
);
1912 EXPORT_SYMBOL(dmu_objset_open_impl
);
1913 EXPORT_SYMBOL(dmu_objset_evict
);
1914 EXPORT_SYMBOL(dmu_objset_register_type
);
1915 EXPORT_SYMBOL(dmu_objset_do_userquota_updates
);
1916 EXPORT_SYMBOL(dmu_objset_userquota_get_ids
);
1917 EXPORT_SYMBOL(dmu_objset_userused_enabled
);
1918 EXPORT_SYMBOL(dmu_objset_userspace_upgrade
);
1919 EXPORT_SYMBOL(dmu_objset_userspace_present
);