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.
28 /* Portions Copyright 2010 Robert Milkowski */
31 #include <sys/zfs_context.h>
32 #include <sys/dmu_objset.h>
33 #include <sys/dsl_dir.h>
34 #include <sys/dsl_dataset.h>
35 #include <sys/dsl_prop.h>
36 #include <sys/dsl_pool.h>
37 #include <sys/dsl_synctask.h>
38 #include <sys/dsl_deleg.h>
39 #include <sys/dnode.h>
42 #include <sys/dmu_tx.h>
45 #include <sys/dmu_impl.h>
46 #include <sys/zfs_ioctl.h>
48 #include <sys/zfs_onexit.h>
49 #include <sys/dsl_destroy.h>
52 * Needed to close a window in dnode_move() that allows the objset to be freed
53 * before it can be safely accessed.
60 rw_init(&os_lock
, NULL
, RW_DEFAULT
, NULL
);
70 dmu_objset_spa(objset_t
*os
)
76 dmu_objset_zil(objset_t
*os
)
82 dmu_objset_pool(objset_t
*os
)
86 if ((ds
= os
->os_dsl_dataset
) != NULL
&& ds
->ds_dir
)
87 return (ds
->ds_dir
->dd_pool
);
89 return (spa_get_dsl(os
->os_spa
));
93 dmu_objset_ds(objset_t
*os
)
95 return (os
->os_dsl_dataset
);
99 dmu_objset_type(objset_t
*os
)
101 return (os
->os_phys
->os_type
);
105 dmu_objset_name(objset_t
*os
, char *buf
)
107 dsl_dataset_name(os
->os_dsl_dataset
, buf
);
111 dmu_objset_id(objset_t
*os
)
113 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
115 return (ds
? ds
->ds_object
: 0);
119 dmu_objset_syncprop(objset_t
*os
)
121 return (os
->os_sync
);
125 dmu_objset_logbias(objset_t
*os
)
127 return (os
->os_logbias
);
131 checksum_changed_cb(void *arg
, uint64_t newval
)
136 * Inheritance should have been done by now.
138 ASSERT(newval
!= ZIO_CHECKSUM_INHERIT
);
140 os
->os_checksum
= zio_checksum_select(newval
, ZIO_CHECKSUM_ON_VALUE
);
144 compression_changed_cb(void *arg
, uint64_t newval
)
149 * Inheritance and range checking should have been done by now.
151 ASSERT(newval
!= ZIO_COMPRESS_INHERIT
);
153 os
->os_compress
= zio_compress_select(newval
, ZIO_COMPRESS_ON_VALUE
);
157 copies_changed_cb(void *arg
, uint64_t newval
)
162 * Inheritance and range checking should have been done by now.
165 ASSERT(newval
<= spa_max_replication(os
->os_spa
));
167 os
->os_copies
= newval
;
171 dedup_changed_cb(void *arg
, uint64_t newval
)
174 spa_t
*spa
= os
->os_spa
;
175 enum zio_checksum checksum
;
178 * Inheritance should have been done by now.
180 ASSERT(newval
!= ZIO_CHECKSUM_INHERIT
);
182 checksum
= zio_checksum_dedup_select(spa
, newval
, ZIO_CHECKSUM_OFF
);
184 os
->os_dedup_checksum
= checksum
& ZIO_CHECKSUM_MASK
;
185 os
->os_dedup_verify
= !!(checksum
& ZIO_CHECKSUM_VERIFY
);
189 primary_cache_changed_cb(void *arg
, uint64_t newval
)
194 * Inheritance and range checking should have been done by now.
196 ASSERT(newval
== ZFS_CACHE_ALL
|| newval
== ZFS_CACHE_NONE
||
197 newval
== ZFS_CACHE_METADATA
);
199 os
->os_primary_cache
= newval
;
203 secondary_cache_changed_cb(void *arg
, uint64_t newval
)
208 * Inheritance and range checking should have been done by now.
210 ASSERT(newval
== ZFS_CACHE_ALL
|| newval
== ZFS_CACHE_NONE
||
211 newval
== ZFS_CACHE_METADATA
);
213 os
->os_secondary_cache
= newval
;
217 sync_changed_cb(void *arg
, uint64_t newval
)
222 * Inheritance and range checking should have been done by now.
224 ASSERT(newval
== ZFS_SYNC_STANDARD
|| newval
== ZFS_SYNC_ALWAYS
||
225 newval
== ZFS_SYNC_DISABLED
);
227 os
->os_sync
= newval
;
229 zil_set_sync(os
->os_zil
, newval
);
233 redundant_metadata_changed_cb(void *arg
, uint64_t newval
)
238 * Inheritance and range checking should have been done by now.
240 ASSERT(newval
== ZFS_REDUNDANT_METADATA_ALL
||
241 newval
== ZFS_REDUNDANT_METADATA_MOST
);
243 os
->os_redundant_metadata
= newval
;
247 logbias_changed_cb(void *arg
, uint64_t newval
)
251 ASSERT(newval
== ZFS_LOGBIAS_LATENCY
||
252 newval
== ZFS_LOGBIAS_THROUGHPUT
);
253 os
->os_logbias
= newval
;
255 zil_set_logbias(os
->os_zil
, newval
);
259 dmu_objset_byteswap(void *buf
, size_t size
)
261 objset_phys_t
*osp
= buf
;
263 ASSERT(size
== OBJSET_OLD_PHYS_SIZE
|| size
== sizeof (objset_phys_t
));
264 dnode_byteswap(&osp
->os_meta_dnode
);
265 byteswap_uint64_array(&osp
->os_zil_header
, sizeof (zil_header_t
));
266 osp
->os_type
= BSWAP_64(osp
->os_type
);
267 osp
->os_flags
= BSWAP_64(osp
->os_flags
);
268 if (size
== sizeof (objset_phys_t
)) {
269 dnode_byteswap(&osp
->os_userused_dnode
);
270 dnode_byteswap(&osp
->os_groupused_dnode
);
275 dmu_objset_open_impl(spa_t
*spa
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
281 ASSERT(ds
== NULL
|| MUTEX_HELD(&ds
->ds_opening_lock
));
283 os
= kmem_zalloc(sizeof (objset_t
), KM_SLEEP
);
284 os
->os_dsl_dataset
= ds
;
287 if (!BP_IS_HOLE(os
->os_rootbp
)) {
288 uint32_t aflags
= ARC_WAIT
;
290 SET_BOOKMARK(&zb
, ds
? ds
->ds_object
: DMU_META_OBJSET
,
291 ZB_ROOT_OBJECT
, ZB_ROOT_LEVEL
, ZB_ROOT_BLKID
);
293 if (DMU_OS_IS_L2CACHEABLE(os
))
294 aflags
|= ARC_L2CACHE
;
295 if (DMU_OS_IS_L2COMPRESSIBLE(os
))
296 aflags
|= ARC_L2COMPRESS
;
298 dprintf_bp(os
->os_rootbp
, "reading %s", "");
299 err
= arc_read(NULL
, spa
, os
->os_rootbp
,
300 arc_getbuf_func
, &os
->os_phys_buf
,
301 ZIO_PRIORITY_SYNC_READ
, ZIO_FLAG_CANFAIL
, &aflags
, &zb
);
303 kmem_free(os
, sizeof (objset_t
));
304 /* convert checksum errors into IO errors */
306 err
= SET_ERROR(EIO
);
310 /* Increase the blocksize if we are permitted. */
311 if (spa_version(spa
) >= SPA_VERSION_USERSPACE
&&
312 arc_buf_size(os
->os_phys_buf
) < sizeof (objset_phys_t
)) {
313 arc_buf_t
*buf
= arc_buf_alloc(spa
,
314 sizeof (objset_phys_t
), &os
->os_phys_buf
,
316 bzero(buf
->b_data
, sizeof (objset_phys_t
));
317 bcopy(os
->os_phys_buf
->b_data
, buf
->b_data
,
318 arc_buf_size(os
->os_phys_buf
));
319 (void) arc_buf_remove_ref(os
->os_phys_buf
,
321 os
->os_phys_buf
= buf
;
324 os
->os_phys
= os
->os_phys_buf
->b_data
;
325 os
->os_flags
= os
->os_phys
->os_flags
;
327 int size
= spa_version(spa
) >= SPA_VERSION_USERSPACE
?
328 sizeof (objset_phys_t
) : OBJSET_OLD_PHYS_SIZE
;
329 os
->os_phys_buf
= arc_buf_alloc(spa
, size
,
330 &os
->os_phys_buf
, ARC_BUFC_METADATA
);
331 os
->os_phys
= os
->os_phys_buf
->b_data
;
332 bzero(os
->os_phys
, size
);
336 * Note: the changed_cb will be called once before the register
337 * func returns, thus changing the checksum/compression from the
338 * default (fletcher2/off). Snapshots don't need to know about
339 * checksum/compression/copies.
342 err
= dsl_prop_register(ds
,
343 zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE
),
344 primary_cache_changed_cb
, os
);
346 err
= dsl_prop_register(ds
,
347 zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE
),
348 secondary_cache_changed_cb
, os
);
350 if (!dsl_dataset_is_snapshot(ds
)) {
352 err
= dsl_prop_register(ds
,
353 zfs_prop_to_name(ZFS_PROP_CHECKSUM
),
354 checksum_changed_cb
, os
);
357 err
= dsl_prop_register(ds
,
358 zfs_prop_to_name(ZFS_PROP_COMPRESSION
),
359 compression_changed_cb
, os
);
362 err
= dsl_prop_register(ds
,
363 zfs_prop_to_name(ZFS_PROP_COPIES
),
364 copies_changed_cb
, os
);
367 err
= dsl_prop_register(ds
,
368 zfs_prop_to_name(ZFS_PROP_DEDUP
),
369 dedup_changed_cb
, os
);
372 err
= dsl_prop_register(ds
,
373 zfs_prop_to_name(ZFS_PROP_LOGBIAS
),
374 logbias_changed_cb
, os
);
377 err
= dsl_prop_register(ds
,
378 zfs_prop_to_name(ZFS_PROP_SYNC
),
379 sync_changed_cb
, os
);
382 err
= dsl_prop_register(ds
,
384 ZFS_PROP_REDUNDANT_METADATA
),
385 redundant_metadata_changed_cb
, os
);
389 VERIFY(arc_buf_remove_ref(os
->os_phys_buf
,
391 kmem_free(os
, sizeof (objset_t
));
395 /* It's the meta-objset. */
396 os
->os_checksum
= ZIO_CHECKSUM_FLETCHER_4
;
397 os
->os_compress
= ZIO_COMPRESS_LZJB
;
398 os
->os_copies
= spa_max_replication(spa
);
399 os
->os_dedup_checksum
= ZIO_CHECKSUM_OFF
;
400 os
->os_dedup_verify
= B_FALSE
;
401 os
->os_logbias
= ZFS_LOGBIAS_LATENCY
;
402 os
->os_sync
= ZFS_SYNC_STANDARD
;
403 os
->os_primary_cache
= ZFS_CACHE_ALL
;
404 os
->os_secondary_cache
= ZFS_CACHE_ALL
;
407 if (ds
== NULL
|| !dsl_dataset_is_snapshot(ds
))
408 os
->os_zil_header
= os
->os_phys
->os_zil_header
;
409 os
->os_zil
= zil_alloc(os
, &os
->os_zil_header
);
411 for (i
= 0; i
< TXG_SIZE
; i
++) {
412 list_create(&os
->os_dirty_dnodes
[i
], sizeof (dnode_t
),
413 offsetof(dnode_t
, dn_dirty_link
[i
]));
414 list_create(&os
->os_free_dnodes
[i
], sizeof (dnode_t
),
415 offsetof(dnode_t
, dn_dirty_link
[i
]));
417 list_create(&os
->os_dnodes
, sizeof (dnode_t
),
418 offsetof(dnode_t
, dn_link
));
419 list_create(&os
->os_downgraded_dbufs
, sizeof (dmu_buf_impl_t
),
420 offsetof(dmu_buf_impl_t
, db_link
));
422 mutex_init(&os
->os_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
423 mutex_init(&os
->os_obj_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
424 mutex_init(&os
->os_user_ptr_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
426 DMU_META_DNODE(os
) = dnode_special_open(os
,
427 &os
->os_phys
->os_meta_dnode
, DMU_META_DNODE_OBJECT
,
429 if (arc_buf_size(os
->os_phys_buf
) >= sizeof (objset_phys_t
)) {
430 DMU_USERUSED_DNODE(os
) = dnode_special_open(os
,
431 &os
->os_phys
->os_userused_dnode
, DMU_USERUSED_OBJECT
,
432 &os
->os_userused_dnode
);
433 DMU_GROUPUSED_DNODE(os
) = dnode_special_open(os
,
434 &os
->os_phys
->os_groupused_dnode
, DMU_GROUPUSED_OBJECT
,
435 &os
->os_groupused_dnode
);
443 dmu_objset_from_ds(dsl_dataset_t
*ds
, objset_t
**osp
)
447 mutex_enter(&ds
->ds_opening_lock
);
448 if (ds
->ds_objset
== NULL
) {
450 err
= dmu_objset_open_impl(dsl_dataset_get_spa(ds
),
451 ds
, dsl_dataset_get_blkptr(ds
), &os
);
454 mutex_enter(&ds
->ds_lock
);
455 ASSERT(ds
->ds_objset
== NULL
);
457 mutex_exit(&ds
->ds_lock
);
460 *osp
= ds
->ds_objset
;
461 mutex_exit(&ds
->ds_opening_lock
);
466 * Holds the pool while the objset is held. Therefore only one objset
467 * can be held at a time.
470 dmu_objset_hold(const char *name
, void *tag
, objset_t
**osp
)
476 err
= dsl_pool_hold(name
, tag
, &dp
);
479 err
= dsl_dataset_hold(dp
, name
, tag
, &ds
);
481 dsl_pool_rele(dp
, tag
);
485 err
= dmu_objset_from_ds(ds
, osp
);
487 dsl_dataset_rele(ds
, tag
);
488 dsl_pool_rele(dp
, tag
);
495 * dsl_pool must not be held when this is called.
496 * Upon successful return, there will be a longhold on the dataset,
497 * and the dsl_pool will not be held.
500 dmu_objset_own(const char *name
, dmu_objset_type_t type
,
501 boolean_t readonly
, void *tag
, objset_t
**osp
)
507 err
= dsl_pool_hold(name
, FTAG
, &dp
);
510 err
= dsl_dataset_own(dp
, name
, tag
, &ds
);
512 dsl_pool_rele(dp
, FTAG
);
516 err
= dmu_objset_from_ds(ds
, osp
);
517 dsl_pool_rele(dp
, FTAG
);
519 dsl_dataset_disown(ds
, tag
);
520 } else if (type
!= DMU_OST_ANY
&& type
!= (*osp
)->os_phys
->os_type
) {
521 dsl_dataset_disown(ds
, tag
);
522 return (SET_ERROR(EINVAL
));
523 } else if (!readonly
&& dsl_dataset_is_snapshot(ds
)) {
524 dsl_dataset_disown(ds
, tag
);
525 return (SET_ERROR(EROFS
));
531 dmu_objset_rele(objset_t
*os
, void *tag
)
533 dsl_pool_t
*dp
= dmu_objset_pool(os
);
534 dsl_dataset_rele(os
->os_dsl_dataset
, tag
);
535 dsl_pool_rele(dp
, tag
);
539 * When we are called, os MUST refer to an objset associated with a dataset
540 * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner
541 * == tag. We will then release and reacquire ownership of the dataset while
542 * holding the pool config_rwlock to avoid intervening namespace or ownership
545 * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to
546 * release the hold on its dataset and acquire a new one on the dataset of the
547 * same name so that it can be partially torn down and reconstructed.
550 dmu_objset_refresh_ownership(objset_t
*os
, void *tag
)
553 dsl_dataset_t
*ds
, *newds
;
554 char name
[MAXNAMELEN
];
556 ds
= os
->os_dsl_dataset
;
557 VERIFY3P(ds
, !=, NULL
);
558 VERIFY3P(ds
->ds_owner
, ==, tag
);
559 VERIFY(dsl_dataset_long_held(ds
));
561 dsl_dataset_name(ds
, name
);
562 dp
= dmu_objset_pool(os
);
563 dsl_pool_config_enter(dp
, FTAG
);
564 dmu_objset_disown(os
, tag
);
565 VERIFY0(dsl_dataset_own(dp
, name
, tag
, &newds
));
566 VERIFY3P(newds
, ==, os
->os_dsl_dataset
);
567 dsl_pool_config_exit(dp
, FTAG
);
571 dmu_objset_disown(objset_t
*os
, void *tag
)
573 dsl_dataset_disown(os
->os_dsl_dataset
, tag
);
577 dmu_objset_evict_dbufs(objset_t
*os
)
581 mutex_enter(&os
->os_lock
);
583 /* process the mdn last, since the other dnodes have holds on it */
584 list_remove(&os
->os_dnodes
, DMU_META_DNODE(os
));
585 list_insert_tail(&os
->os_dnodes
, DMU_META_DNODE(os
));
588 * Find the first dnode with holds. We have to do this dance
589 * because dnode_add_ref() only works if you already have a
590 * hold. If there are no holds then it has no dbufs so OK to
593 for (dn
= list_head(&os
->os_dnodes
);
594 dn
&& !dnode_add_ref(dn
, FTAG
);
595 dn
= list_next(&os
->os_dnodes
, dn
))
599 dnode_t
*next_dn
= dn
;
602 next_dn
= list_next(&os
->os_dnodes
, next_dn
);
603 } while (next_dn
&& !dnode_add_ref(next_dn
, FTAG
));
605 mutex_exit(&os
->os_lock
);
606 dnode_evict_dbufs(dn
);
607 dnode_rele(dn
, FTAG
);
608 mutex_enter(&os
->os_lock
);
611 mutex_exit(&os
->os_lock
);
615 dmu_objset_evict(objset_t
*os
)
619 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
621 for (t
= 0; t
< TXG_SIZE
; t
++)
622 ASSERT(!dmu_objset_is_dirty(os
, t
));
625 if (!dsl_dataset_is_snapshot(ds
)) {
626 VERIFY0(dsl_prop_unregister(ds
,
627 zfs_prop_to_name(ZFS_PROP_CHECKSUM
),
628 checksum_changed_cb
, os
));
629 VERIFY0(dsl_prop_unregister(ds
,
630 zfs_prop_to_name(ZFS_PROP_COMPRESSION
),
631 compression_changed_cb
, os
));
632 VERIFY0(dsl_prop_unregister(ds
,
633 zfs_prop_to_name(ZFS_PROP_COPIES
),
634 copies_changed_cb
, os
));
635 VERIFY0(dsl_prop_unregister(ds
,
636 zfs_prop_to_name(ZFS_PROP_DEDUP
),
637 dedup_changed_cb
, os
));
638 VERIFY0(dsl_prop_unregister(ds
,
639 zfs_prop_to_name(ZFS_PROP_LOGBIAS
),
640 logbias_changed_cb
, os
));
641 VERIFY0(dsl_prop_unregister(ds
,
642 zfs_prop_to_name(ZFS_PROP_SYNC
),
643 sync_changed_cb
, os
));
644 VERIFY0(dsl_prop_unregister(ds
,
645 zfs_prop_to_name(ZFS_PROP_REDUNDANT_METADATA
),
646 redundant_metadata_changed_cb
, os
));
648 VERIFY0(dsl_prop_unregister(ds
,
649 zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE
),
650 primary_cache_changed_cb
, os
));
651 VERIFY0(dsl_prop_unregister(ds
,
652 zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE
),
653 secondary_cache_changed_cb
, os
));
659 dmu_objset_evict_dbufs(os
);
661 dnode_special_close(&os
->os_meta_dnode
);
662 if (DMU_USERUSED_DNODE(os
)) {
663 dnode_special_close(&os
->os_userused_dnode
);
664 dnode_special_close(&os
->os_groupused_dnode
);
666 zil_free(os
->os_zil
);
668 ASSERT3P(list_head(&os
->os_dnodes
), ==, NULL
);
670 VERIFY(arc_buf_remove_ref(os
->os_phys_buf
, &os
->os_phys_buf
));
673 * This is a barrier to prevent the objset from going away in
674 * dnode_move() until we can safely ensure that the objset is still in
675 * use. We consider the objset valid before the barrier and invalid
678 rw_enter(&os_lock
, RW_READER
);
681 mutex_destroy(&os
->os_lock
);
682 mutex_destroy(&os
->os_obj_lock
);
683 mutex_destroy(&os
->os_user_ptr_lock
);
684 kmem_free(os
, sizeof (objset_t
));
688 dmu_objset_snap_cmtime(objset_t
*os
)
690 return (dsl_dir_snap_cmtime(os
->os_dsl_dataset
->ds_dir
));
693 /* called from dsl for meta-objset */
695 dmu_objset_create_impl(spa_t
*spa
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
696 dmu_objset_type_t type
, dmu_tx_t
*tx
)
701 ASSERT(dmu_tx_is_syncing(tx
));
704 VERIFY0(dmu_objset_from_ds(ds
, &os
));
706 VERIFY0(dmu_objset_open_impl(spa
, NULL
, bp
, &os
));
708 mdn
= DMU_META_DNODE(os
);
710 dnode_allocate(mdn
, DMU_OT_DNODE
, 1 << DNODE_BLOCK_SHIFT
,
711 DN_MAX_INDBLKSHIFT
, DMU_OT_NONE
, 0, tx
);
714 * We don't want to have to increase the meta-dnode's nlevels
715 * later, because then we could do it in quescing context while
716 * we are also accessing it in open context.
718 * This precaution is not necessary for the MOS (ds == NULL),
719 * because the MOS is only updated in syncing context.
720 * This is most fortunate: the MOS is the only objset that
721 * needs to be synced multiple times as spa_sync() iterates
722 * to convergence, so minimizing its dn_nlevels matters.
728 * Determine the number of levels necessary for the meta-dnode
729 * to contain DN_MAX_OBJECT dnodes.
731 while ((uint64_t)mdn
->dn_nblkptr
<< (mdn
->dn_datablkshift
+
732 (levels
- 1) * (mdn
->dn_indblkshift
- SPA_BLKPTRSHIFT
)) <
733 DN_MAX_OBJECT
* sizeof (dnode_phys_t
))
736 mdn
->dn_next_nlevels
[tx
->tx_txg
& TXG_MASK
] =
737 mdn
->dn_nlevels
= levels
;
740 ASSERT(type
!= DMU_OST_NONE
);
741 ASSERT(type
!= DMU_OST_ANY
);
742 ASSERT(type
< DMU_OST_NUMTYPES
);
743 os
->os_phys
->os_type
= type
;
744 if (dmu_objset_userused_enabled(os
)) {
745 os
->os_phys
->os_flags
|= OBJSET_FLAG_USERACCOUNTING_COMPLETE
;
746 os
->os_flags
= os
->os_phys
->os_flags
;
749 dsl_dataset_dirty(ds
, tx
);
754 typedef struct dmu_objset_create_arg
{
755 const char *doca_name
;
757 void (*doca_userfunc
)(objset_t
*os
, void *arg
,
758 cred_t
*cr
, dmu_tx_t
*tx
);
760 dmu_objset_type_t doca_type
;
762 } dmu_objset_create_arg_t
;
766 dmu_objset_create_check(void *arg
, dmu_tx_t
*tx
)
768 dmu_objset_create_arg_t
*doca
= arg
;
769 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
774 if (strchr(doca
->doca_name
, '@') != NULL
)
775 return (SET_ERROR(EINVAL
));
777 error
= dsl_dir_hold(dp
, doca
->doca_name
, FTAG
, &pdd
, &tail
);
781 dsl_dir_rele(pdd
, FTAG
);
782 return (SET_ERROR(EEXIST
));
784 error
= dsl_fs_ss_limit_check(pdd
, 1, ZFS_PROP_FILESYSTEM_LIMIT
, NULL
,
786 dsl_dir_rele(pdd
, FTAG
);
792 dmu_objset_create_sync(void *arg
, dmu_tx_t
*tx
)
794 dmu_objset_create_arg_t
*doca
= arg
;
795 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
803 VERIFY0(dsl_dir_hold(dp
, doca
->doca_name
, FTAG
, &pdd
, &tail
));
805 obj
= dsl_dataset_create_sync(pdd
, tail
, NULL
, doca
->doca_flags
,
806 doca
->doca_cred
, tx
);
808 VERIFY0(dsl_dataset_hold_obj(pdd
->dd_pool
, obj
, FTAG
, &ds
));
809 bp
= dsl_dataset_get_blkptr(ds
);
810 os
= dmu_objset_create_impl(pdd
->dd_pool
->dp_spa
,
811 ds
, bp
, doca
->doca_type
, tx
);
813 if (doca
->doca_userfunc
!= NULL
) {
814 doca
->doca_userfunc(os
, doca
->doca_userarg
,
815 doca
->doca_cred
, tx
);
818 spa_history_log_internal_ds(ds
, "create", tx
, "");
819 dsl_dataset_rele(ds
, FTAG
);
820 dsl_dir_rele(pdd
, FTAG
);
824 dmu_objset_create(const char *name
, dmu_objset_type_t type
, uint64_t flags
,
825 void (*func
)(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
), void *arg
)
827 dmu_objset_create_arg_t doca
;
829 doca
.doca_name
= name
;
830 doca
.doca_cred
= CRED();
831 doca
.doca_flags
= flags
;
832 doca
.doca_userfunc
= func
;
833 doca
.doca_userarg
= arg
;
834 doca
.doca_type
= type
;
836 return (dsl_sync_task(name
,
837 dmu_objset_create_check
, dmu_objset_create_sync
, &doca
, 5));
840 typedef struct dmu_objset_clone_arg
{
841 const char *doca_clone
;
842 const char *doca_origin
;
844 } dmu_objset_clone_arg_t
;
848 dmu_objset_clone_check(void *arg
, dmu_tx_t
*tx
)
850 dmu_objset_clone_arg_t
*doca
= arg
;
854 dsl_dataset_t
*origin
;
855 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
857 if (strchr(doca
->doca_clone
, '@') != NULL
)
858 return (SET_ERROR(EINVAL
));
860 error
= dsl_dir_hold(dp
, doca
->doca_clone
, FTAG
, &pdd
, &tail
);
864 dsl_dir_rele(pdd
, FTAG
);
865 return (SET_ERROR(EEXIST
));
867 /* You can't clone across pools. */
868 if (pdd
->dd_pool
!= dp
) {
869 dsl_dir_rele(pdd
, FTAG
);
870 return (SET_ERROR(EXDEV
));
872 error
= dsl_fs_ss_limit_check(pdd
, 1, ZFS_PROP_FILESYSTEM_LIMIT
, NULL
,
875 dsl_dir_rele(pdd
, FTAG
);
876 return (SET_ERROR(EDQUOT
));
878 dsl_dir_rele(pdd
, FTAG
);
880 error
= dsl_dataset_hold(dp
, doca
->doca_origin
, FTAG
, &origin
);
884 /* You can't clone across pools. */
885 if (origin
->ds_dir
->dd_pool
!= dp
) {
886 dsl_dataset_rele(origin
, FTAG
);
887 return (SET_ERROR(EXDEV
));
890 /* You can only clone snapshots, not the head datasets. */
891 if (!dsl_dataset_is_snapshot(origin
)) {
892 dsl_dataset_rele(origin
, FTAG
);
893 return (SET_ERROR(EINVAL
));
895 dsl_dataset_rele(origin
, FTAG
);
901 dmu_objset_clone_sync(void *arg
, dmu_tx_t
*tx
)
903 dmu_objset_clone_arg_t
*doca
= arg
;
904 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
907 dsl_dataset_t
*origin
, *ds
;
909 char namebuf
[MAXNAMELEN
];
911 VERIFY0(dsl_dir_hold(dp
, doca
->doca_clone
, FTAG
, &pdd
, &tail
));
912 VERIFY0(dsl_dataset_hold(dp
, doca
->doca_origin
, FTAG
, &origin
));
914 obj
= dsl_dataset_create_sync(pdd
, tail
, origin
, 0,
915 doca
->doca_cred
, tx
);
917 VERIFY0(dsl_dataset_hold_obj(pdd
->dd_pool
, obj
, FTAG
, &ds
));
918 dsl_dataset_name(origin
, namebuf
);
919 spa_history_log_internal_ds(ds
, "clone", tx
,
920 "origin=%s (%llu)", namebuf
, origin
->ds_object
);
921 dsl_dataset_rele(ds
, FTAG
);
922 dsl_dataset_rele(origin
, FTAG
);
923 dsl_dir_rele(pdd
, FTAG
);
927 dmu_objset_clone(const char *clone
, const char *origin
)
929 dmu_objset_clone_arg_t doca
;
931 doca
.doca_clone
= clone
;
932 doca
.doca_origin
= origin
;
933 doca
.doca_cred
= CRED();
935 return (dsl_sync_task(clone
,
936 dmu_objset_clone_check
, dmu_objset_clone_sync
, &doca
, 5));
940 dmu_objset_snapshot_one(const char *fsname
, const char *snapname
)
943 char *longsnap
= kmem_asprintf("%s@%s", fsname
, snapname
);
944 nvlist_t
*snaps
= fnvlist_alloc();
946 fnvlist_add_boolean(snaps
, longsnap
);
948 err
= dsl_dataset_snapshot(snaps
, NULL
, NULL
);
954 dmu_objset_sync_dnodes(list_t
*list
, list_t
*newlist
, dmu_tx_t
*tx
)
958 while ((dn
= list_head(list
))) {
959 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
);
960 ASSERT(dn
->dn_dbuf
->db_data_pending
);
962 * Initialize dn_zio outside dnode_sync() because the
963 * meta-dnode needs to set it ouside dnode_sync().
965 dn
->dn_zio
= dn
->dn_dbuf
->db_data_pending
->dr_zio
;
968 ASSERT3U(dn
->dn_nlevels
, <=, DN_MAX_LEVELS
);
969 list_remove(list
, dn
);
972 (void) dnode_add_ref(dn
, newlist
);
973 list_insert_tail(newlist
, dn
);
982 dmu_objset_write_ready(zio_t
*zio
, arc_buf_t
*abuf
, void *arg
)
986 blkptr_t
*bp
= zio
->io_bp
;
988 dnode_phys_t
*dnp
= &os
->os_phys
->os_meta_dnode
;
990 ASSERT(!BP_IS_EMBEDDED(bp
));
991 ASSERT3P(bp
, ==, os
->os_rootbp
);
992 ASSERT3U(BP_GET_TYPE(bp
), ==, DMU_OT_OBJSET
);
993 ASSERT0(BP_GET_LEVEL(bp
));
996 * Update rootbp fill count: it should be the number of objects
997 * allocated in the object set (not counting the "special"
998 * objects that are stored in the objset_phys_t -- the meta
999 * dnode and user/group accounting objects).
1002 for (i
= 0; i
< dnp
->dn_nblkptr
; i
++)
1003 bp
->blk_fill
+= BP_GET_FILL(&dnp
->dn_blkptr
[i
]);
1008 dmu_objset_write_done(zio_t
*zio
, arc_buf_t
*abuf
, void *arg
)
1010 blkptr_t
*bp
= zio
->io_bp
;
1011 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
1014 if (zio
->io_flags
& ZIO_FLAG_IO_REWRITE
) {
1015 ASSERT(BP_EQUAL(bp
, bp_orig
));
1017 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
1018 dmu_tx_t
*tx
= os
->os_synctx
;
1020 (void) dsl_dataset_block_kill(ds
, bp_orig
, tx
, B_TRUE
);
1021 dsl_dataset_block_born(ds
, bp
, tx
);
1025 /* called from dsl */
1027 dmu_objset_sync(objset_t
*os
, zio_t
*pio
, dmu_tx_t
*tx
)
1030 zbookmark_phys_t zb
;
1034 list_t
*newlist
= NULL
;
1035 dbuf_dirty_record_t
*dr
;
1037 dprintf_ds(os
->os_dsl_dataset
, "txg=%llu\n", tx
->tx_txg
);
1039 ASSERT(dmu_tx_is_syncing(tx
));
1040 /* XXX the write_done callback should really give us the tx... */
1043 if (os
->os_dsl_dataset
== NULL
) {
1045 * This is the MOS. If we have upgraded,
1046 * spa_max_replication() could change, so reset
1049 os
->os_copies
= spa_max_replication(os
->os_spa
);
1053 * Create the root block IO
1055 SET_BOOKMARK(&zb
, os
->os_dsl_dataset
?
1056 os
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
1057 ZB_ROOT_OBJECT
, ZB_ROOT_LEVEL
, ZB_ROOT_BLKID
);
1058 arc_release(os
->os_phys_buf
, &os
->os_phys_buf
);
1060 dmu_write_policy(os
, NULL
, 0, 0, &zp
);
1062 zio
= arc_write(pio
, os
->os_spa
, tx
->tx_txg
,
1063 os
->os_rootbp
, os
->os_phys_buf
, DMU_OS_IS_L2CACHEABLE(os
),
1064 DMU_OS_IS_L2COMPRESSIBLE(os
), &zp
, dmu_objset_write_ready
,
1065 NULL
, dmu_objset_write_done
, os
, ZIO_PRIORITY_ASYNC_WRITE
,
1066 ZIO_FLAG_MUSTSUCCEED
, &zb
);
1069 * Sync special dnodes - the parent IO for the sync is the root block
1071 DMU_META_DNODE(os
)->dn_zio
= zio
;
1072 dnode_sync(DMU_META_DNODE(os
), tx
);
1074 os
->os_phys
->os_flags
= os
->os_flags
;
1076 if (DMU_USERUSED_DNODE(os
) &&
1077 DMU_USERUSED_DNODE(os
)->dn_type
!= DMU_OT_NONE
) {
1078 DMU_USERUSED_DNODE(os
)->dn_zio
= zio
;
1079 dnode_sync(DMU_USERUSED_DNODE(os
), tx
);
1080 DMU_GROUPUSED_DNODE(os
)->dn_zio
= zio
;
1081 dnode_sync(DMU_GROUPUSED_DNODE(os
), tx
);
1084 txgoff
= tx
->tx_txg
& TXG_MASK
;
1086 if (dmu_objset_userused_enabled(os
)) {
1087 newlist
= &os
->os_synced_dnodes
;
1089 * We must create the list here because it uses the
1090 * dn_dirty_link[] of this txg.
1092 list_create(newlist
, sizeof (dnode_t
),
1093 offsetof(dnode_t
, dn_dirty_link
[txgoff
]));
1096 dmu_objset_sync_dnodes(&os
->os_free_dnodes
[txgoff
], newlist
, tx
);
1097 dmu_objset_sync_dnodes(&os
->os_dirty_dnodes
[txgoff
], newlist
, tx
);
1099 list
= &DMU_META_DNODE(os
)->dn_dirty_records
[txgoff
];
1100 while ((dr
= list_head(list
))) {
1101 ASSERT0(dr
->dr_dbuf
->db_level
);
1102 list_remove(list
, dr
);
1104 zio_nowait(dr
->dr_zio
);
1107 * Free intent log blocks up to this tx.
1109 zil_sync(os
->os_zil
, tx
);
1110 os
->os_phys
->os_zil_header
= os
->os_zil_header
;
1115 dmu_objset_is_dirty(objset_t
*os
, uint64_t txg
)
1117 return (!list_is_empty(&os
->os_dirty_dnodes
[txg
& TXG_MASK
]) ||
1118 !list_is_empty(&os
->os_free_dnodes
[txg
& TXG_MASK
]));
1121 static objset_used_cb_t
*used_cbs
[DMU_OST_NUMTYPES
];
1124 dmu_objset_register_type(dmu_objset_type_t ost
, objset_used_cb_t
*cb
)
1130 dmu_objset_userused_enabled(objset_t
*os
)
1132 return (spa_version(os
->os_spa
) >= SPA_VERSION_USERSPACE
&&
1133 used_cbs
[os
->os_phys
->os_type
] != NULL
&&
1134 DMU_USERUSED_DNODE(os
) != NULL
);
1138 do_userquota_update(objset_t
*os
, uint64_t used
, uint64_t flags
,
1139 uint64_t user
, uint64_t group
, boolean_t subtract
, dmu_tx_t
*tx
)
1141 if ((flags
& DNODE_FLAG_USERUSED_ACCOUNTED
)) {
1142 int64_t delta
= DNODE_SIZE
+ used
;
1145 VERIFY3U(0, ==, zap_increment_int(os
, DMU_USERUSED_OBJECT
,
1147 VERIFY3U(0, ==, zap_increment_int(os
, DMU_GROUPUSED_OBJECT
,
1153 dmu_objset_do_userquota_updates(objset_t
*os
, dmu_tx_t
*tx
)
1156 list_t
*list
= &os
->os_synced_dnodes
;
1158 ASSERT(list_head(list
) == NULL
|| dmu_objset_userused_enabled(os
));
1160 while ((dn
= list_head(list
))) {
1162 ASSERT(!DMU_OBJECT_IS_SPECIAL(dn
->dn_object
));
1163 ASSERT(dn
->dn_phys
->dn_type
== DMU_OT_NONE
||
1164 dn
->dn_phys
->dn_flags
&
1165 DNODE_FLAG_USERUSED_ACCOUNTED
);
1167 /* Allocate the user/groupused objects if necessary. */
1168 if (DMU_USERUSED_DNODE(os
)->dn_type
== DMU_OT_NONE
) {
1169 VERIFY(0 == zap_create_claim(os
,
1170 DMU_USERUSED_OBJECT
,
1171 DMU_OT_USERGROUP_USED
, DMU_OT_NONE
, 0, tx
));
1172 VERIFY(0 == zap_create_claim(os
,
1173 DMU_GROUPUSED_OBJECT
,
1174 DMU_OT_USERGROUP_USED
, DMU_OT_NONE
, 0, tx
));
1178 * We intentionally modify the zap object even if the
1179 * net delta is zero. Otherwise
1180 * the block of the zap obj could be shared between
1181 * datasets but need to be different between them after
1185 flags
= dn
->dn_id_flags
;
1187 if (flags
& DN_ID_OLD_EXIST
) {
1188 do_userquota_update(os
, dn
->dn_oldused
, dn
->dn_oldflags
,
1189 dn
->dn_olduid
, dn
->dn_oldgid
, B_TRUE
, tx
);
1191 if (flags
& DN_ID_NEW_EXIST
) {
1192 do_userquota_update(os
, DN_USED_BYTES(dn
->dn_phys
),
1193 dn
->dn_phys
->dn_flags
, dn
->dn_newuid
,
1194 dn
->dn_newgid
, B_FALSE
, tx
);
1197 mutex_enter(&dn
->dn_mtx
);
1199 dn
->dn_oldflags
= 0;
1200 if (dn
->dn_id_flags
& DN_ID_NEW_EXIST
) {
1201 dn
->dn_olduid
= dn
->dn_newuid
;
1202 dn
->dn_oldgid
= dn
->dn_newgid
;
1203 dn
->dn_id_flags
|= DN_ID_OLD_EXIST
;
1204 if (dn
->dn_bonuslen
== 0)
1205 dn
->dn_id_flags
|= DN_ID_CHKED_SPILL
;
1207 dn
->dn_id_flags
|= DN_ID_CHKED_BONUS
;
1209 dn
->dn_id_flags
&= ~(DN_ID_NEW_EXIST
);
1210 mutex_exit(&dn
->dn_mtx
);
1212 list_remove(list
, dn
);
1213 dnode_rele(dn
, list
);
1218 * Returns a pointer to data to find uid/gid from
1220 * If a dirty record for transaction group that is syncing can't
1221 * be found then NULL is returned. In the NULL case it is assumed
1222 * the uid/gid aren't changing.
1225 dmu_objset_userquota_find_data(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1227 dbuf_dirty_record_t
*dr
, **drp
;
1230 if (db
->db_dirtycnt
== 0)
1231 return (db
->db
.db_data
); /* Nothing is changing */
1233 for (drp
= &db
->db_last_dirty
; (dr
= *drp
) != NULL
; drp
= &dr
->dr_next
)
1234 if (dr
->dr_txg
== tx
->tx_txg
)
1242 DB_DNODE_ENTER(dr
->dr_dbuf
);
1243 dn
= DB_DNODE(dr
->dr_dbuf
);
1245 if (dn
->dn_bonuslen
== 0 &&
1246 dr
->dr_dbuf
->db_blkid
== DMU_SPILL_BLKID
)
1247 data
= dr
->dt
.dl
.dr_data
->b_data
;
1249 data
= dr
->dt
.dl
.dr_data
;
1251 DB_DNODE_EXIT(dr
->dr_dbuf
);
1258 dmu_objset_userquota_get_ids(dnode_t
*dn
, boolean_t before
, dmu_tx_t
*tx
)
1260 objset_t
*os
= dn
->dn_objset
;
1262 dmu_buf_impl_t
*db
= NULL
;
1263 uint64_t *user
= NULL
;
1264 uint64_t *group
= NULL
;
1265 int flags
= dn
->dn_id_flags
;
1267 boolean_t have_spill
= B_FALSE
;
1269 if (!dmu_objset_userused_enabled(dn
->dn_objset
))
1272 if (before
&& (flags
& (DN_ID_CHKED_BONUS
|DN_ID_OLD_EXIST
|
1273 DN_ID_CHKED_SPILL
)))
1276 if (before
&& dn
->dn_bonuslen
!= 0)
1277 data
= DN_BONUS(dn
->dn_phys
);
1278 else if (!before
&& dn
->dn_bonuslen
!= 0) {
1281 mutex_enter(&db
->db_mtx
);
1282 data
= dmu_objset_userquota_find_data(db
, tx
);
1284 data
= DN_BONUS(dn
->dn_phys
);
1286 } else if (dn
->dn_bonuslen
== 0 && dn
->dn_bonustype
== DMU_OT_SA
) {
1289 if (RW_WRITE_HELD(&dn
->dn_struct_rwlock
))
1290 rf
|= DB_RF_HAVESTRUCT
;
1291 error
= dmu_spill_hold_by_dnode(dn
,
1292 rf
| DB_RF_MUST_SUCCEED
,
1293 FTAG
, (dmu_buf_t
**)&db
);
1295 mutex_enter(&db
->db_mtx
);
1296 data
= (before
) ? db
->db
.db_data
:
1297 dmu_objset_userquota_find_data(db
, tx
);
1298 have_spill
= B_TRUE
;
1300 mutex_enter(&dn
->dn_mtx
);
1301 dn
->dn_id_flags
|= DN_ID_CHKED_BONUS
;
1302 mutex_exit(&dn
->dn_mtx
);
1308 user
= &dn
->dn_olduid
;
1309 group
= &dn
->dn_oldgid
;
1311 user
= &dn
->dn_newuid
;
1312 group
= &dn
->dn_newgid
;
1316 * Must always call the callback in case the object
1317 * type has changed and that type isn't an object type to track
1319 error
= used_cbs
[os
->os_phys
->os_type
](dn
->dn_bonustype
, data
,
1323 * Preserve existing uid/gid when the callback can't determine
1324 * what the new uid/gid are and the callback returned EEXIST.
1325 * The EEXIST error tells us to just use the existing uid/gid.
1326 * If we don't know what the old values are then just assign
1327 * them to 0, since that is a new file being created.
1329 if (!before
&& data
== NULL
&& error
== EEXIST
) {
1330 if (flags
& DN_ID_OLD_EXIST
) {
1331 dn
->dn_newuid
= dn
->dn_olduid
;
1332 dn
->dn_newgid
= dn
->dn_oldgid
;
1341 mutex_exit(&db
->db_mtx
);
1343 mutex_enter(&dn
->dn_mtx
);
1344 if (error
== 0 && before
)
1345 dn
->dn_id_flags
|= DN_ID_OLD_EXIST
;
1346 if (error
== 0 && !before
)
1347 dn
->dn_id_flags
|= DN_ID_NEW_EXIST
;
1350 dn
->dn_id_flags
|= DN_ID_CHKED_SPILL
;
1352 dn
->dn_id_flags
|= DN_ID_CHKED_BONUS
;
1354 mutex_exit(&dn
->dn_mtx
);
1356 dmu_buf_rele((dmu_buf_t
*)db
, FTAG
);
1360 dmu_objset_userspace_present(objset_t
*os
)
1362 return (os
->os_phys
->os_flags
&
1363 OBJSET_FLAG_USERACCOUNTING_COMPLETE
);
1367 dmu_objset_userspace_upgrade(objset_t
*os
)
1372 if (dmu_objset_userspace_present(os
))
1374 if (!dmu_objset_userused_enabled(os
))
1375 return (SET_ERROR(ENOTSUP
));
1376 if (dmu_objset_is_snapshot(os
))
1377 return (SET_ERROR(EINVAL
));
1380 * We simply need to mark every object dirty, so that it will be
1381 * synced out and now accounted. If this is called
1382 * concurrently, or if we already did some work before crashing,
1383 * that's fine, since we track each object's accounted state
1387 for (obj
= 0; err
== 0; err
= dmu_object_next(os
, &obj
, FALSE
, 0)) {
1392 if (issig(JUSTLOOKING
) && issig(FORREAL
))
1393 return (SET_ERROR(EINTR
));
1395 objerr
= dmu_bonus_hold(os
, obj
, FTAG
, &db
);
1398 tx
= dmu_tx_create(os
);
1399 dmu_tx_hold_bonus(tx
, obj
);
1400 objerr
= dmu_tx_assign(tx
, TXG_WAIT
);
1405 dmu_buf_will_dirty(db
, tx
);
1406 dmu_buf_rele(db
, FTAG
);
1410 os
->os_flags
|= OBJSET_FLAG_USERACCOUNTING_COMPLETE
;
1411 txg_wait_synced(dmu_objset_pool(os
), 0);
1416 dmu_objset_space(objset_t
*os
, uint64_t *refdbytesp
, uint64_t *availbytesp
,
1417 uint64_t *usedobjsp
, uint64_t *availobjsp
)
1419 dsl_dataset_space(os
->os_dsl_dataset
, refdbytesp
, availbytesp
,
1420 usedobjsp
, availobjsp
);
1424 dmu_objset_fsid_guid(objset_t
*os
)
1426 return (dsl_dataset_fsid_guid(os
->os_dsl_dataset
));
1430 dmu_objset_fast_stat(objset_t
*os
, dmu_objset_stats_t
*stat
)
1432 stat
->dds_type
= os
->os_phys
->os_type
;
1433 if (os
->os_dsl_dataset
)
1434 dsl_dataset_fast_stat(os
->os_dsl_dataset
, stat
);
1438 dmu_objset_stats(objset_t
*os
, nvlist_t
*nv
)
1440 ASSERT(os
->os_dsl_dataset
||
1441 os
->os_phys
->os_type
== DMU_OST_META
);
1443 if (os
->os_dsl_dataset
!= NULL
)
1444 dsl_dataset_stats(os
->os_dsl_dataset
, nv
);
1446 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_TYPE
,
1447 os
->os_phys
->os_type
);
1448 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_USERACCOUNTING
,
1449 dmu_objset_userspace_present(os
));
1453 dmu_objset_is_snapshot(objset_t
*os
)
1455 if (os
->os_dsl_dataset
!= NULL
)
1456 return (dsl_dataset_is_snapshot(os
->os_dsl_dataset
));
1462 dmu_snapshot_realname(objset_t
*os
, char *name
, char *real
, int maxlen
,
1463 boolean_t
*conflict
)
1465 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
1468 if (ds
->ds_phys
->ds_snapnames_zapobj
== 0)
1469 return (SET_ERROR(ENOENT
));
1471 return (zap_lookup_norm(ds
->ds_dir
->dd_pool
->dp_meta_objset
,
1472 ds
->ds_phys
->ds_snapnames_zapobj
, name
, 8, 1, &ignored
, MT_FIRST
,
1473 real
, maxlen
, conflict
));
1477 dmu_snapshot_list_next(objset_t
*os
, int namelen
, char *name
,
1478 uint64_t *idp
, uint64_t *offp
, boolean_t
*case_conflict
)
1480 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
1481 zap_cursor_t cursor
;
1482 zap_attribute_t attr
;
1484 ASSERT(dsl_pool_config_held(dmu_objset_pool(os
)));
1486 if (ds
->ds_phys
->ds_snapnames_zapobj
== 0)
1487 return (SET_ERROR(ENOENT
));
1489 zap_cursor_init_serialized(&cursor
,
1490 ds
->ds_dir
->dd_pool
->dp_meta_objset
,
1491 ds
->ds_phys
->ds_snapnames_zapobj
, *offp
);
1493 if (zap_cursor_retrieve(&cursor
, &attr
) != 0) {
1494 zap_cursor_fini(&cursor
);
1495 return (SET_ERROR(ENOENT
));
1498 if (strlen(attr
.za_name
) + 1 > namelen
) {
1499 zap_cursor_fini(&cursor
);
1500 return (SET_ERROR(ENAMETOOLONG
));
1503 (void) strcpy(name
, attr
.za_name
);
1505 *idp
= attr
.za_first_integer
;
1507 *case_conflict
= attr
.za_normalization_conflict
;
1508 zap_cursor_advance(&cursor
);
1509 *offp
= zap_cursor_serialize(&cursor
);
1510 zap_cursor_fini(&cursor
);
1516 dmu_snapshot_lookup(objset_t
*os
, const char *name
, uint64_t *value
)
1518 return (dsl_dataset_snap_lookup(os
->os_dsl_dataset
, name
, value
));
1522 dmu_dir_list_next(objset_t
*os
, int namelen
, char *name
,
1523 uint64_t *idp
, uint64_t *offp
)
1525 dsl_dir_t
*dd
= os
->os_dsl_dataset
->ds_dir
;
1526 zap_cursor_t cursor
;
1527 zap_attribute_t attr
;
1529 /* there is no next dir on a snapshot! */
1530 if (os
->os_dsl_dataset
->ds_object
!=
1531 dd
->dd_phys
->dd_head_dataset_obj
)
1532 return (SET_ERROR(ENOENT
));
1534 zap_cursor_init_serialized(&cursor
,
1535 dd
->dd_pool
->dp_meta_objset
,
1536 dd
->dd_phys
->dd_child_dir_zapobj
, *offp
);
1538 if (zap_cursor_retrieve(&cursor
, &attr
) != 0) {
1539 zap_cursor_fini(&cursor
);
1540 return (SET_ERROR(ENOENT
));
1543 if (strlen(attr
.za_name
) + 1 > namelen
) {
1544 zap_cursor_fini(&cursor
);
1545 return (SET_ERROR(ENAMETOOLONG
));
1548 (void) strcpy(name
, attr
.za_name
);
1550 *idp
= attr
.za_first_integer
;
1551 zap_cursor_advance(&cursor
);
1552 *offp
= zap_cursor_serialize(&cursor
);
1553 zap_cursor_fini(&cursor
);
1559 * Find objsets under and including ddobj, call func(ds) on each.
1562 dmu_objset_find_dp(dsl_pool_t
*dp
, uint64_t ddobj
,
1563 int func(dsl_pool_t
*, dsl_dataset_t
*, void *), void *arg
, int flags
)
1568 zap_attribute_t
*attr
;
1572 ASSERT(dsl_pool_config_held(dp
));
1574 err
= dsl_dir_hold_obj(dp
, ddobj
, NULL
, FTAG
, &dd
);
1578 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1579 if (dd
->dd_myname
[0] == '$') {
1580 dsl_dir_rele(dd
, FTAG
);
1584 thisobj
= dd
->dd_phys
->dd_head_dataset_obj
;
1585 attr
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
1588 * Iterate over all children.
1590 if (flags
& DS_FIND_CHILDREN
) {
1591 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
,
1592 dd
->dd_phys
->dd_child_dir_zapobj
);
1593 zap_cursor_retrieve(&zc
, attr
) == 0;
1594 (void) zap_cursor_advance(&zc
)) {
1595 ASSERT3U(attr
->za_integer_length
, ==,
1597 ASSERT3U(attr
->za_num_integers
, ==, 1);
1599 err
= dmu_objset_find_dp(dp
, attr
->za_first_integer
,
1604 zap_cursor_fini(&zc
);
1607 dsl_dir_rele(dd
, FTAG
);
1608 kmem_free(attr
, sizeof (zap_attribute_t
));
1614 * Iterate over all snapshots.
1616 if (flags
& DS_FIND_SNAPSHOTS
) {
1618 err
= dsl_dataset_hold_obj(dp
, thisobj
, FTAG
, &ds
);
1621 uint64_t snapobj
= ds
->ds_phys
->ds_snapnames_zapobj
;
1622 dsl_dataset_rele(ds
, FTAG
);
1624 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
, snapobj
);
1625 zap_cursor_retrieve(&zc
, attr
) == 0;
1626 (void) zap_cursor_advance(&zc
)) {
1627 ASSERT3U(attr
->za_integer_length
, ==,
1629 ASSERT3U(attr
->za_num_integers
, ==, 1);
1631 err
= dsl_dataset_hold_obj(dp
,
1632 attr
->za_first_integer
, FTAG
, &ds
);
1635 err
= func(dp
, ds
, arg
);
1636 dsl_dataset_rele(ds
, FTAG
);
1640 zap_cursor_fini(&zc
);
1644 dsl_dir_rele(dd
, FTAG
);
1645 kmem_free(attr
, sizeof (zap_attribute_t
));
1653 err
= dsl_dataset_hold_obj(dp
, thisobj
, FTAG
, &ds
);
1656 err
= func(dp
, ds
, arg
);
1657 dsl_dataset_rele(ds
, FTAG
);
1662 * Find all objsets under name, and for each, call 'func(child_name, arg)'.
1663 * The dp_config_rwlock must not be held when this is called, and it
1664 * will not be held when the callback is called.
1665 * Therefore this function should only be used when the pool is not changing
1666 * (e.g. in syncing context), or the callback can deal with the possible races.
1669 dmu_objset_find_impl(spa_t
*spa
, const char *name
,
1670 int func(const char *, void *), void *arg
, int flags
)
1673 dsl_pool_t
*dp
= spa_get_dsl(spa
);
1676 zap_attribute_t
*attr
;
1681 dsl_pool_config_enter(dp
, FTAG
);
1683 err
= dsl_dir_hold(dp
, name
, FTAG
, &dd
, NULL
);
1685 dsl_pool_config_exit(dp
, FTAG
);
1689 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1690 if (dd
->dd_myname
[0] == '$') {
1691 dsl_dir_rele(dd
, FTAG
);
1692 dsl_pool_config_exit(dp
, FTAG
);
1696 thisobj
= dd
->dd_phys
->dd_head_dataset_obj
;
1697 attr
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
1700 * Iterate over all children.
1702 if (flags
& DS_FIND_CHILDREN
) {
1703 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
,
1704 dd
->dd_phys
->dd_child_dir_zapobj
);
1705 zap_cursor_retrieve(&zc
, attr
) == 0;
1706 (void) zap_cursor_advance(&zc
)) {
1707 ASSERT3U(attr
->za_integer_length
, ==,
1709 ASSERT3U(attr
->za_num_integers
, ==, 1);
1711 child
= kmem_asprintf("%s/%s", name
, attr
->za_name
);
1712 dsl_pool_config_exit(dp
, FTAG
);
1713 err
= dmu_objset_find_impl(spa
, child
,
1715 dsl_pool_config_enter(dp
, FTAG
);
1720 zap_cursor_fini(&zc
);
1723 dsl_dir_rele(dd
, FTAG
);
1724 dsl_pool_config_exit(dp
, FTAG
);
1725 kmem_free(attr
, sizeof (zap_attribute_t
));
1731 * Iterate over all snapshots.
1733 if (flags
& DS_FIND_SNAPSHOTS
) {
1734 err
= dsl_dataset_hold_obj(dp
, thisobj
, FTAG
, &ds
);
1737 uint64_t snapobj
= ds
->ds_phys
->ds_snapnames_zapobj
;
1738 dsl_dataset_rele(ds
, FTAG
);
1740 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
, snapobj
);
1741 zap_cursor_retrieve(&zc
, attr
) == 0;
1742 (void) zap_cursor_advance(&zc
)) {
1743 ASSERT3U(attr
->za_integer_length
, ==,
1745 ASSERT3U(attr
->za_num_integers
, ==, 1);
1747 child
= kmem_asprintf("%s@%s",
1748 name
, attr
->za_name
);
1749 dsl_pool_config_exit(dp
, FTAG
);
1750 err
= func(child
, arg
);
1751 dsl_pool_config_enter(dp
, FTAG
);
1756 zap_cursor_fini(&zc
);
1760 dsl_dir_rele(dd
, FTAG
);
1761 kmem_free(attr
, sizeof (zap_attribute_t
));
1762 dsl_pool_config_exit(dp
, FTAG
);
1767 /* Apply to self. */
1768 return (func(name
, arg
));
1772 * See comment above dmu_objset_find_impl().
1775 dmu_objset_find(char *name
, int func(const char *, void *), void *arg
,
1781 error
= spa_open(name
, &spa
, FTAG
);
1784 error
= dmu_objset_find_impl(spa
, name
, func
, arg
, flags
);
1785 spa_close(spa
, FTAG
);
1790 dmu_objset_set_user(objset_t
*os
, void *user_ptr
)
1792 ASSERT(MUTEX_HELD(&os
->os_user_ptr_lock
));
1793 os
->os_user_ptr
= user_ptr
;
1797 dmu_objset_get_user(objset_t
*os
)
1799 ASSERT(MUTEX_HELD(&os
->os_user_ptr_lock
));
1800 return (os
->os_user_ptr
);
1804 * Determine name of filesystem, given name of snapshot.
1805 * buf must be at least MAXNAMELEN bytes
1808 dmu_fsname(const char *snapname
, char *buf
)
1810 char *atp
= strchr(snapname
, '@');
1812 return (SET_ERROR(EINVAL
));
1813 if (atp
- snapname
>= MAXNAMELEN
)
1814 return (SET_ERROR(ENAMETOOLONG
));
1815 (void) strlcpy(buf
, snapname
, atp
- snapname
+ 1);
1819 #if defined(_KERNEL) && defined(HAVE_SPL)
1820 EXPORT_SYMBOL(dmu_objset_zil
);
1821 EXPORT_SYMBOL(dmu_objset_pool
);
1822 EXPORT_SYMBOL(dmu_objset_ds
);
1823 EXPORT_SYMBOL(dmu_objset_type
);
1824 EXPORT_SYMBOL(dmu_objset_name
);
1825 EXPORT_SYMBOL(dmu_objset_hold
);
1826 EXPORT_SYMBOL(dmu_objset_own
);
1827 EXPORT_SYMBOL(dmu_objset_rele
);
1828 EXPORT_SYMBOL(dmu_objset_disown
);
1829 EXPORT_SYMBOL(dmu_objset_from_ds
);
1830 EXPORT_SYMBOL(dmu_objset_create
);
1831 EXPORT_SYMBOL(dmu_objset_clone
);
1832 EXPORT_SYMBOL(dmu_objset_stats
);
1833 EXPORT_SYMBOL(dmu_objset_fast_stat
);
1834 EXPORT_SYMBOL(dmu_objset_spa
);
1835 EXPORT_SYMBOL(dmu_objset_space
);
1836 EXPORT_SYMBOL(dmu_objset_fsid_guid
);
1837 EXPORT_SYMBOL(dmu_objset_find
);
1838 EXPORT_SYMBOL(dmu_objset_byteswap
);
1839 EXPORT_SYMBOL(dmu_objset_evict_dbufs
);
1840 EXPORT_SYMBOL(dmu_objset_snap_cmtime
);
1842 EXPORT_SYMBOL(dmu_objset_sync
);
1843 EXPORT_SYMBOL(dmu_objset_is_dirty
);
1844 EXPORT_SYMBOL(dmu_objset_create_impl
);
1845 EXPORT_SYMBOL(dmu_objset_open_impl
);
1846 EXPORT_SYMBOL(dmu_objset_evict
);
1847 EXPORT_SYMBOL(dmu_objset_register_type
);
1848 EXPORT_SYMBOL(dmu_objset_do_userquota_updates
);
1849 EXPORT_SYMBOL(dmu_objset_userquota_get_ids
);
1850 EXPORT_SYMBOL(dmu_objset_userused_enabled
);
1851 EXPORT_SYMBOL(dmu_objset_userspace_upgrade
);
1852 EXPORT_SYMBOL(dmu_objset_userspace_present
);