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) 2013 by Saso Kiselkov. All rights reserved.
24 * Copyright (c) 2012 by Delphix. All rights reserved.
27 /* Portions Copyright 2010 Robert Milkowski */
30 #include <sys/zfs_context.h>
31 #include <sys/dmu_objset.h>
32 #include <sys/dsl_dir.h>
33 #include <sys/dsl_dataset.h>
34 #include <sys/dsl_prop.h>
35 #include <sys/dsl_pool.h>
36 #include <sys/dsl_synctask.h>
37 #include <sys/dsl_deleg.h>
38 #include <sys/dnode.h>
41 #include <sys/dmu_tx.h>
44 #include <sys/dmu_impl.h>
45 #include <sys/zfs_ioctl.h>
47 #include <sys/zfs_onexit.h>
50 * Needed to close a window in dnode_move() that allows the objset to be freed
51 * before it can be safely accessed.
58 rw_init(&os_lock
, NULL
, RW_DEFAULT
, NULL
);
68 dmu_objset_spa(objset_t
*os
)
74 dmu_objset_zil(objset_t
*os
)
80 dmu_objset_pool(objset_t
*os
)
84 if ((ds
= os
->os_dsl_dataset
) != NULL
&& ds
->ds_dir
)
85 return (ds
->ds_dir
->dd_pool
);
87 return (spa_get_dsl(os
->os_spa
));
91 dmu_objset_ds(objset_t
*os
)
93 return (os
->os_dsl_dataset
);
97 dmu_objset_type(objset_t
*os
)
99 return (os
->os_phys
->os_type
);
103 dmu_objset_name(objset_t
*os
, char *buf
)
105 dsl_dataset_name(os
->os_dsl_dataset
, buf
);
109 dmu_objset_id(objset_t
*os
)
111 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
113 return (ds
? ds
->ds_object
: 0);
117 dmu_objset_syncprop(objset_t
*os
)
119 return (os
->os_sync
);
123 dmu_objset_logbias(objset_t
*os
)
125 return (os
->os_logbias
);
129 checksum_changed_cb(void *arg
, uint64_t newval
)
134 * Inheritance should have been done by now.
136 ASSERT(newval
!= ZIO_CHECKSUM_INHERIT
);
138 os
->os_checksum
= zio_checksum_select(newval
, ZIO_CHECKSUM_ON_VALUE
);
142 compression_changed_cb(void *arg
, uint64_t newval
)
147 * Inheritance and range checking should have been done by now.
149 ASSERT(newval
!= ZIO_COMPRESS_INHERIT
);
151 os
->os_compress
= zio_compress_select(newval
, ZIO_COMPRESS_ON_VALUE
);
155 copies_changed_cb(void *arg
, uint64_t newval
)
160 * Inheritance and range checking should have been done by now.
163 ASSERT(newval
<= spa_max_replication(os
->os_spa
));
165 os
->os_copies
= newval
;
169 dedup_changed_cb(void *arg
, uint64_t newval
)
172 spa_t
*spa
= os
->os_spa
;
173 enum zio_checksum checksum
;
176 * Inheritance should have been done by now.
178 ASSERT(newval
!= ZIO_CHECKSUM_INHERIT
);
180 checksum
= zio_checksum_dedup_select(spa
, newval
, ZIO_CHECKSUM_OFF
);
182 os
->os_dedup_checksum
= checksum
& ZIO_CHECKSUM_MASK
;
183 os
->os_dedup_verify
= !!(checksum
& ZIO_CHECKSUM_VERIFY
);
187 primary_cache_changed_cb(void *arg
, uint64_t newval
)
192 * Inheritance and range checking should have been done by now.
194 ASSERT(newval
== ZFS_CACHE_ALL
|| newval
== ZFS_CACHE_NONE
||
195 newval
== ZFS_CACHE_METADATA
);
197 os
->os_primary_cache
= newval
;
201 secondary_cache_changed_cb(void *arg
, uint64_t newval
)
206 * Inheritance and range checking should have been done by now.
208 ASSERT(newval
== ZFS_CACHE_ALL
|| newval
== ZFS_CACHE_NONE
||
209 newval
== ZFS_CACHE_METADATA
);
211 os
->os_secondary_cache
= newval
;
215 sync_changed_cb(void *arg
, uint64_t newval
)
220 * Inheritance and range checking should have been done by now.
222 ASSERT(newval
== ZFS_SYNC_STANDARD
|| newval
== ZFS_SYNC_ALWAYS
||
223 newval
== ZFS_SYNC_DISABLED
);
225 os
->os_sync
= newval
;
227 zil_set_sync(os
->os_zil
, newval
);
231 logbias_changed_cb(void *arg
, uint64_t newval
)
235 ASSERT(newval
== ZFS_LOGBIAS_LATENCY
||
236 newval
== ZFS_LOGBIAS_THROUGHPUT
);
237 os
->os_logbias
= newval
;
239 zil_set_logbias(os
->os_zil
, newval
);
243 dmu_objset_byteswap(void *buf
, size_t size
)
245 objset_phys_t
*osp
= buf
;
247 ASSERT(size
== OBJSET_OLD_PHYS_SIZE
|| size
== sizeof (objset_phys_t
));
248 dnode_byteswap(&osp
->os_meta_dnode
);
249 byteswap_uint64_array(&osp
->os_zil_header
, sizeof (zil_header_t
));
250 osp
->os_type
= BSWAP_64(osp
->os_type
);
251 osp
->os_flags
= BSWAP_64(osp
->os_flags
);
252 if (size
== sizeof (objset_phys_t
)) {
253 dnode_byteswap(&osp
->os_userused_dnode
);
254 dnode_byteswap(&osp
->os_groupused_dnode
);
259 dmu_objset_open_impl(spa_t
*spa
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
265 ASSERT(ds
== NULL
|| MUTEX_HELD(&ds
->ds_opening_lock
));
267 os
= kmem_zalloc(sizeof (objset_t
), KM_PUSHPAGE
);
268 os
->os_dsl_dataset
= ds
;
271 if (!BP_IS_HOLE(os
->os_rootbp
)) {
272 uint32_t aflags
= ARC_WAIT
;
274 SET_BOOKMARK(&zb
, ds
? ds
->ds_object
: DMU_META_OBJSET
,
275 ZB_ROOT_OBJECT
, ZB_ROOT_LEVEL
, ZB_ROOT_BLKID
);
277 if (DMU_OS_IS_L2CACHEABLE(os
))
278 aflags
|= ARC_L2CACHE
;
279 if (DMU_OS_IS_L2COMPRESSIBLE(os
))
280 aflags
|= ARC_L2COMPRESS
;
282 dprintf_bp(os
->os_rootbp
, "reading %s", "");
283 err
= arc_read(NULL
, spa
, os
->os_rootbp
,
284 arc_getbuf_func
, &os
->os_phys_buf
,
285 ZIO_PRIORITY_SYNC_READ
, ZIO_FLAG_CANFAIL
, &aflags
, &zb
);
287 kmem_free(os
, sizeof (objset_t
));
288 /* convert checksum errors into IO errors */
294 /* Increase the blocksize if we are permitted. */
295 if (spa_version(spa
) >= SPA_VERSION_USERSPACE
&&
296 arc_buf_size(os
->os_phys_buf
) < sizeof (objset_phys_t
)) {
297 arc_buf_t
*buf
= arc_buf_alloc(spa
,
298 sizeof (objset_phys_t
), &os
->os_phys_buf
,
300 bzero(buf
->b_data
, sizeof (objset_phys_t
));
301 bcopy(os
->os_phys_buf
->b_data
, buf
->b_data
,
302 arc_buf_size(os
->os_phys_buf
));
303 (void) arc_buf_remove_ref(os
->os_phys_buf
,
305 os
->os_phys_buf
= buf
;
308 os
->os_phys
= os
->os_phys_buf
->b_data
;
309 os
->os_flags
= os
->os_phys
->os_flags
;
311 int size
= spa_version(spa
) >= SPA_VERSION_USERSPACE
?
312 sizeof (objset_phys_t
) : OBJSET_OLD_PHYS_SIZE
;
313 os
->os_phys_buf
= arc_buf_alloc(spa
, size
,
314 &os
->os_phys_buf
, ARC_BUFC_METADATA
);
315 os
->os_phys
= os
->os_phys_buf
->b_data
;
316 bzero(os
->os_phys
, size
);
320 * Note: the changed_cb will be called once before the register
321 * func returns, thus changing the checksum/compression from the
322 * default (fletcher2/off). Snapshots don't need to know about
323 * checksum/compression/copies.
326 err
= dsl_prop_register(ds
, "primarycache",
327 primary_cache_changed_cb
, os
);
329 err
= dsl_prop_register(ds
, "secondarycache",
330 secondary_cache_changed_cb
, os
);
331 if (!dsl_dataset_is_snapshot(ds
)) {
333 err
= dsl_prop_register(ds
, "checksum",
334 checksum_changed_cb
, os
);
336 err
= dsl_prop_register(ds
, "compression",
337 compression_changed_cb
, os
);
339 err
= dsl_prop_register(ds
, "copies",
340 copies_changed_cb
, os
);
342 err
= dsl_prop_register(ds
, "dedup",
343 dedup_changed_cb
, os
);
345 err
= dsl_prop_register(ds
, "logbias",
346 logbias_changed_cb
, os
);
348 err
= dsl_prop_register(ds
, "sync",
349 sync_changed_cb
, os
);
352 VERIFY(arc_buf_remove_ref(os
->os_phys_buf
,
353 &os
->os_phys_buf
) == 1);
354 kmem_free(os
, sizeof (objset_t
));
357 } else if (ds
== NULL
) {
358 /* It's the meta-objset. */
359 os
->os_checksum
= ZIO_CHECKSUM_FLETCHER_4
;
360 os
->os_compress
= ZIO_COMPRESS_LZJB
;
361 os
->os_copies
= spa_max_replication(spa
);
362 os
->os_dedup_checksum
= ZIO_CHECKSUM_OFF
;
363 os
->os_dedup_verify
= 0;
366 os
->os_primary_cache
= ZFS_CACHE_ALL
;
367 os
->os_secondary_cache
= ZFS_CACHE_ALL
;
370 if (ds
== NULL
|| !dsl_dataset_is_snapshot(ds
))
371 os
->os_zil_header
= os
->os_phys
->os_zil_header
;
372 os
->os_zil
= zil_alloc(os
, &os
->os_zil_header
);
374 for (i
= 0; i
< TXG_SIZE
; i
++) {
375 list_create(&os
->os_dirty_dnodes
[i
], sizeof (dnode_t
),
376 offsetof(dnode_t
, dn_dirty_link
[i
]));
377 list_create(&os
->os_free_dnodes
[i
], sizeof (dnode_t
),
378 offsetof(dnode_t
, dn_dirty_link
[i
]));
380 list_create(&os
->os_dnodes
, sizeof (dnode_t
),
381 offsetof(dnode_t
, dn_link
));
382 list_create(&os
->os_downgraded_dbufs
, sizeof (dmu_buf_impl_t
),
383 offsetof(dmu_buf_impl_t
, db_link
));
385 mutex_init(&os
->os_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
386 mutex_init(&os
->os_obj_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
387 mutex_init(&os
->os_user_ptr_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
389 DMU_META_DNODE(os
) = dnode_special_open(os
,
390 &os
->os_phys
->os_meta_dnode
, DMU_META_DNODE_OBJECT
,
392 if (arc_buf_size(os
->os_phys_buf
) >= sizeof (objset_phys_t
)) {
393 DMU_USERUSED_DNODE(os
) = dnode_special_open(os
,
394 &os
->os_phys
->os_userused_dnode
, DMU_USERUSED_OBJECT
,
395 &os
->os_userused_dnode
);
396 DMU_GROUPUSED_DNODE(os
) = dnode_special_open(os
,
397 &os
->os_phys
->os_groupused_dnode
, DMU_GROUPUSED_OBJECT
,
398 &os
->os_groupused_dnode
);
402 * We should be the only thread trying to do this because we
403 * have ds_opening_lock
406 mutex_enter(&ds
->ds_lock
);
407 ASSERT(ds
->ds_objset
== NULL
);
409 mutex_exit(&ds
->ds_lock
);
417 dmu_objset_from_ds(dsl_dataset_t
*ds
, objset_t
**osp
)
421 mutex_enter(&ds
->ds_opening_lock
);
422 *osp
= ds
->ds_objset
;
424 err
= dmu_objset_open_impl(dsl_dataset_get_spa(ds
),
425 ds
, dsl_dataset_get_blkptr(ds
), osp
);
427 mutex_exit(&ds
->ds_opening_lock
);
431 /* called from zpl */
433 dmu_objset_hold(const char *name
, void *tag
, objset_t
**osp
)
438 err
= dsl_dataset_hold(name
, tag
, &ds
);
442 err
= dmu_objset_from_ds(ds
, osp
);
444 dsl_dataset_rele(ds
, tag
);
449 /* called from zpl */
451 dmu_objset_own(const char *name
, dmu_objset_type_t type
,
452 boolean_t readonly
, void *tag
, objset_t
**osp
)
457 err
= dsl_dataset_own(name
, B_FALSE
, tag
, &ds
);
461 err
= dmu_objset_from_ds(ds
, osp
);
463 dsl_dataset_disown(ds
, tag
);
464 } else if (type
!= DMU_OST_ANY
&& type
!= (*osp
)->os_phys
->os_type
) {
465 dmu_objset_disown(*osp
, tag
);
467 } else if (!readonly
&& dsl_dataset_is_snapshot(ds
)) {
468 dmu_objset_disown(*osp
, tag
);
475 dmu_objset_rele(objset_t
*os
, void *tag
)
477 dsl_dataset_rele(os
->os_dsl_dataset
, tag
);
481 dmu_objset_disown(objset_t
*os
, void *tag
)
483 dsl_dataset_disown(os
->os_dsl_dataset
, tag
);
487 dmu_objset_evict_dbufs(objset_t
*os
)
491 mutex_enter(&os
->os_lock
);
493 /* process the mdn last, since the other dnodes have holds on it */
494 list_remove(&os
->os_dnodes
, DMU_META_DNODE(os
));
495 list_insert_tail(&os
->os_dnodes
, DMU_META_DNODE(os
));
498 * Find the first dnode with holds. We have to do this dance
499 * because dnode_add_ref() only works if you already have a
500 * hold. If there are no holds then it has no dbufs so OK to
503 for (dn
= list_head(&os
->os_dnodes
);
504 dn
&& !dnode_add_ref(dn
, FTAG
);
505 dn
= list_next(&os
->os_dnodes
, dn
))
509 dnode_t
*next_dn
= dn
;
512 next_dn
= list_next(&os
->os_dnodes
, next_dn
);
513 } while (next_dn
&& !dnode_add_ref(next_dn
, FTAG
));
515 mutex_exit(&os
->os_lock
);
516 dnode_evict_dbufs(dn
);
517 dnode_rele(dn
, FTAG
);
518 mutex_enter(&os
->os_lock
);
521 dn
= list_head(&os
->os_dnodes
);
522 mutex_exit(&os
->os_lock
);
523 return (dn
!= DMU_META_DNODE(os
));
527 dmu_objset_evict(objset_t
*os
)
531 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
533 for (t
= 0; t
< TXG_SIZE
; t
++)
534 ASSERT(!dmu_objset_is_dirty(os
, t
));
537 if (!dsl_dataset_is_snapshot(ds
)) {
538 VERIFY(0 == dsl_prop_unregister(ds
, "checksum",
539 checksum_changed_cb
, os
));
540 VERIFY(0 == dsl_prop_unregister(ds
, "compression",
541 compression_changed_cb
, os
));
542 VERIFY(0 == dsl_prop_unregister(ds
, "copies",
543 copies_changed_cb
, os
));
544 VERIFY(0 == dsl_prop_unregister(ds
, "dedup",
545 dedup_changed_cb
, os
));
546 VERIFY(0 == dsl_prop_unregister(ds
, "logbias",
547 logbias_changed_cb
, os
));
548 VERIFY(0 == dsl_prop_unregister(ds
, "sync",
549 sync_changed_cb
, os
));
551 VERIFY(0 == dsl_prop_unregister(ds
, "primarycache",
552 primary_cache_changed_cb
, os
));
553 VERIFY(0 == dsl_prop_unregister(ds
, "secondarycache",
554 secondary_cache_changed_cb
, os
));
561 * We should need only a single pass over the dnode list, since
562 * nothing can be added to the list at this point.
564 (void) dmu_objset_evict_dbufs(os
);
566 dnode_special_close(&os
->os_meta_dnode
);
567 if (DMU_USERUSED_DNODE(os
)) {
568 dnode_special_close(&os
->os_userused_dnode
);
569 dnode_special_close(&os
->os_groupused_dnode
);
571 zil_free(os
->os_zil
);
573 ASSERT3P(list_head(&os
->os_dnodes
), ==, NULL
);
575 VERIFY(arc_buf_remove_ref(os
->os_phys_buf
, &os
->os_phys_buf
) == 1);
578 * This is a barrier to prevent the objset from going away in
579 * dnode_move() until we can safely ensure that the objset is still in
580 * use. We consider the objset valid before the barrier and invalid
583 rw_enter(&os_lock
, RW_READER
);
586 mutex_destroy(&os
->os_lock
);
587 mutex_destroy(&os
->os_obj_lock
);
588 mutex_destroy(&os
->os_user_ptr_lock
);
589 kmem_free(os
, sizeof (objset_t
));
593 dmu_objset_snap_cmtime(objset_t
*os
)
595 return (dsl_dir_snap_cmtime(os
->os_dsl_dataset
->ds_dir
));
598 /* called from dsl for meta-objset */
600 dmu_objset_create_impl(spa_t
*spa
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
601 dmu_objset_type_t type
, dmu_tx_t
*tx
)
606 ASSERT(dmu_tx_is_syncing(tx
));
608 VERIFY(0 == dmu_objset_from_ds(ds
, &os
));
610 VERIFY(0 == dmu_objset_open_impl(spa
, NULL
, bp
, &os
));
612 mdn
= DMU_META_DNODE(os
);
614 dnode_allocate(mdn
, DMU_OT_DNODE
, 1 << DNODE_BLOCK_SHIFT
,
615 DN_MAX_INDBLKSHIFT
, DMU_OT_NONE
, 0, tx
);
618 * We don't want to have to increase the meta-dnode's nlevels
619 * later, because then we could do it in quescing context while
620 * we are also accessing it in open context.
622 * This precaution is not necessary for the MOS (ds == NULL),
623 * because the MOS is only updated in syncing context.
624 * This is most fortunate: the MOS is the only objset that
625 * needs to be synced multiple times as spa_sync() iterates
626 * to convergence, so minimizing its dn_nlevels matters.
632 * Determine the number of levels necessary for the meta-dnode
633 * to contain DN_MAX_OBJECT dnodes.
635 while ((uint64_t)mdn
->dn_nblkptr
<< (mdn
->dn_datablkshift
+
636 (levels
- 1) * (mdn
->dn_indblkshift
- SPA_BLKPTRSHIFT
)) <
637 DN_MAX_OBJECT
* sizeof (dnode_phys_t
))
640 mdn
->dn_next_nlevels
[tx
->tx_txg
& TXG_MASK
] =
641 mdn
->dn_nlevels
= levels
;
644 ASSERT(type
!= DMU_OST_NONE
);
645 ASSERT(type
!= DMU_OST_ANY
);
646 ASSERT(type
< DMU_OST_NUMTYPES
);
647 os
->os_phys
->os_type
= type
;
648 if (dmu_objset_userused_enabled(os
)) {
649 os
->os_phys
->os_flags
|= OBJSET_FLAG_USERACCOUNTING_COMPLETE
;
650 os
->os_flags
= os
->os_phys
->os_flags
;
653 dsl_dataset_dirty(ds
, tx
);
659 void (*userfunc
)(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
);
661 dsl_dataset_t
*clone_origin
;
662 const char *lastname
;
663 dmu_objset_type_t type
;
670 dmu_objset_create_check(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
672 dsl_dir_t
*dd
= arg1
;
673 struct oscarg
*oa
= arg2
;
674 objset_t
*mos
= dd
->dd_pool
->dp_meta_objset
;
678 err
= zap_lookup(mos
, dd
->dd_phys
->dd_child_dir_zapobj
,
679 oa
->lastname
, sizeof (uint64_t), 1, &ddobj
);
681 return (err
? err
: EEXIST
);
683 if (oa
->clone_origin
!= NULL
) {
684 /* You can't clone across pools. */
685 if (oa
->clone_origin
->ds_dir
->dd_pool
!= dd
->dd_pool
)
688 /* You can only clone snapshots, not the head datasets. */
689 if (!dsl_dataset_is_snapshot(oa
->clone_origin
))
697 dmu_objset_create_sync(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
699 dsl_dir_t
*dd
= arg1
;
700 spa_t
*spa
= dd
->dd_pool
->dp_spa
;
701 struct oscarg
*oa
= arg2
;
706 ASSERT(dmu_tx_is_syncing(tx
));
708 obj
= dsl_dataset_create_sync(dd
, oa
->lastname
,
709 oa
->clone_origin
, oa
->flags
, oa
->cr
, tx
);
711 VERIFY3U(0, ==, dsl_dataset_hold_obj(dd
->dd_pool
, obj
, FTAG
, &ds
));
712 bp
= dsl_dataset_get_blkptr(ds
);
713 if (BP_IS_HOLE(bp
)) {
715 dmu_objset_create_impl(spa
, ds
, bp
, oa
->type
, tx
);
718 oa
->userfunc(os
, oa
->userarg
, oa
->cr
, tx
);
721 if (oa
->clone_origin
== NULL
) {
722 spa_history_log_internal_ds(ds
, "create", tx
, "");
724 char namebuf
[MAXNAMELEN
];
725 dsl_dataset_name(oa
->clone_origin
, namebuf
);
726 spa_history_log_internal_ds(ds
, "clone", tx
,
727 "origin=%s (%llu)", namebuf
, oa
->clone_origin
->ds_object
);
729 dsl_dataset_rele(ds
, FTAG
);
733 dmu_objset_create(const char *name
, dmu_objset_type_t type
, uint64_t flags
,
734 void (*func
)(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
), void *arg
)
739 struct oscarg oa
= { 0 };
741 ASSERT(strchr(name
, '@') == NULL
);
742 err
= dsl_dir_open(name
, FTAG
, &pdd
, &tail
);
746 dsl_dir_close(pdd
, FTAG
);
757 err
= dsl_sync_task_do(pdd
->dd_pool
, dmu_objset_create_check
,
758 dmu_objset_create_sync
, pdd
, &oa
, 5);
759 dsl_dir_close(pdd
, FTAG
);
764 dmu_objset_clone(const char *name
, dsl_dataset_t
*clone_origin
, uint64_t flags
)
769 struct oscarg oa
= { 0 };
771 ASSERT(strchr(name
, '@') == NULL
);
772 err
= dsl_dir_open(name
, FTAG
, &pdd
, &tail
);
776 dsl_dir_close(pdd
, FTAG
);
781 oa
.clone_origin
= clone_origin
;
785 err
= dsl_sync_task_do(pdd
->dd_pool
, dmu_objset_create_check
,
786 dmu_objset_create_sync
, pdd
, &oa
, 5);
787 dsl_dir_close(pdd
, FTAG
);
792 dmu_objset_destroy(const char *name
, boolean_t defer
)
797 error
= dsl_dataset_own(name
, B_TRUE
, FTAG
, &ds
);
799 error
= dsl_dataset_destroy(ds
, FTAG
, defer
);
800 /* dsl_dataset_destroy() closes the ds. */
806 typedef struct snapallarg
{
807 dsl_sync_task_group_t
*saa_dstg
;
808 boolean_t saa_needsuspend
;
811 /* the following are used only if 'temporary' is set: */
812 boolean_t saa_temporary
;
813 const char *saa_htag
;
814 struct dsl_ds_holdarg
*saa_ha
;
815 dsl_dataset_t
*saa_newds
;
818 typedef struct snaponearg
{
819 const char *soa_longname
; /* long snap name */
820 const char *soa_snapname
; /* short snap name */
821 snapallarg_t
*soa_saa
;
825 snapshot_check(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
828 snaponearg_t
*soa
= arg2
;
829 snapallarg_t
*saa
= soa
->soa_saa
;
832 /* The props have already been checked by zfs_check_userprops(). */
834 error
= dsl_dataset_snapshot_check(os
->os_dsl_dataset
,
835 soa
->soa_snapname
, tx
);
839 if (saa
->saa_temporary
) {
841 * Ideally we would just call
842 * dsl_dataset_user_hold_check() and
843 * dsl_dataset_destroy_check() here. However the
844 * dataset we want to hold and destroy is the snapshot
845 * that we just confirmed we can create, but it won't
846 * exist until after these checks are run. Do any
847 * checks we can here and if more checks are added to
848 * those routines in the future, similar checks may be
851 if (spa_version(os
->os_spa
) < SPA_VERSION_USERREFS
)
854 * Not checking number of tags because the tag will be
855 * unique, as it will be the only tag.
857 if (strlen(saa
->saa_htag
) + MAX_TAG_PREFIX_LEN
>= MAXNAMELEN
)
860 saa
->saa_ha
= kmem_alloc(sizeof (struct dsl_ds_holdarg
),
862 saa
->saa_ha
->temphold
= B_TRUE
;
863 saa
->saa_ha
->htag
= saa
->saa_htag
;
869 snapshot_sync(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
872 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
873 snaponearg_t
*soa
= arg2
;
874 snapallarg_t
*saa
= soa
->soa_saa
;
876 dsl_dataset_snapshot_sync(ds
, soa
->soa_snapname
, tx
);
878 if (saa
->saa_props
!= NULL
) {
880 pa
.pa_props
= saa
->saa_props
;
881 pa
.pa_source
= ZPROP_SRC_LOCAL
;
882 dsl_props_set_sync(ds
->ds_prev
, &pa
, tx
);
885 if (saa
->saa_temporary
) {
886 struct dsl_ds_destroyarg da
;
888 dsl_dataset_user_hold_sync(ds
->ds_prev
, saa
->saa_ha
, tx
);
889 kmem_free(saa
->saa_ha
, sizeof (struct dsl_ds_holdarg
));
891 saa
->saa_newds
= ds
->ds_prev
;
895 dsl_dataset_destroy_sync(&da
, FTAG
, tx
);
900 snapshot_one_impl(const char *snapname
, void *arg
)
903 snapallarg_t
*saa
= arg
;
908 fsname
= kmem_zalloc(MAXPATHLEN
, KM_PUSHPAGE
);
909 (void) strlcpy(fsname
, snapname
, MAXPATHLEN
);
910 strchr(fsname
, '@')[0] = '\0';
912 err
= dmu_objset_hold(fsname
, saa
, &os
);
913 kmem_free(fsname
, MAXPATHLEN
);
918 * If the objset is in an inconsistent state (eg, in the process
919 * of being destroyed), don't snapshot it.
921 if (os
->os_dsl_dataset
->ds_phys
->ds_flags
& DS_FLAG_INCONSISTENT
) {
922 dmu_objset_rele(os
, saa
);
926 if (saa
->saa_needsuspend
) {
927 err
= zil_suspend(dmu_objset_zil(os
));
929 dmu_objset_rele(os
, saa
);
934 soa
= kmem_zalloc(sizeof (*soa
), KM_PUSHPAGE
);
936 soa
->soa_longname
= snapname
;
937 soa
->soa_snapname
= strchr(snapname
, '@') + 1;
939 dsl_sync_task_create(saa
->saa_dstg
, snapshot_check
, snapshot_sync
,
946 * The snapshots must all be in the same pool.
949 dmu_objset_snapshot(nvlist_t
*snaps
, nvlist_t
*props
, nvlist_t
*errors
)
951 dsl_sync_task_t
*dst
;
952 snapallarg_t saa
= { 0 };
958 pair
= nvlist_next_nvpair(snaps
, NULL
);
962 err
= spa_open(nvpair_name(pair
), &spa
, FTAG
);
965 saa
.saa_dstg
= dsl_sync_task_group_create(spa_get_dsl(spa
));
966 saa
.saa_props
= props
;
967 saa
.saa_needsuspend
= (spa_version(spa
) < SPA_VERSION_FAST_SNAP
);
969 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
970 pair
= nvlist_next_nvpair(snaps
, pair
)) {
971 err
= snapshot_one_impl(nvpair_name(pair
), &saa
);
973 if (errors
!= NULL
) {
974 fnvlist_add_int32(errors
,
975 nvpair_name(pair
), err
);
982 * If any call to snapshot_one_impl() failed, don't execute the
983 * sync task. The error handling code below will clean up the
984 * snaponearg_t from any successful calls to
985 * snapshot_one_impl().
988 err
= dsl_sync_task_group_wait(saa
.saa_dstg
);
992 for (dst
= list_head(&saa
.saa_dstg
->dstg_tasks
); dst
;
993 dst
= list_next(&saa
.saa_dstg
->dstg_tasks
, dst
)) {
994 objset_t
*os
= dst
->dst_arg1
;
995 snaponearg_t
*soa
= dst
->dst_arg2
;
996 if (dst
->dst_err
!= 0) {
997 if (errors
!= NULL
) {
998 fnvlist_add_int32(errors
,
999 soa
->soa_longname
, dst
->dst_err
);
1004 if (saa
.saa_needsuspend
)
1005 zil_resume(dmu_objset_zil(os
));
1006 dmu_objset_rele(os
, &saa
);
1007 kmem_free(soa
, sizeof (*soa
));
1010 dsl_sync_task_group_destroy(saa
.saa_dstg
);
1011 spa_close(spa
, FTAG
);
1016 dmu_objset_snapshot_one(const char *fsname
, const char *snapname
)
1019 char *longsnap
= kmem_asprintf("%s@%s", fsname
, snapname
);
1020 nvlist_t
*snaps
= fnvlist_alloc();
1022 fnvlist_add_boolean(snaps
, longsnap
);
1023 err
= dmu_objset_snapshot(snaps
, NULL
, NULL
);
1024 fnvlist_free(snaps
);
1030 dmu_objset_snapshot_tmp(const char *snapname
, const char *tag
, int cleanup_fd
)
1032 dsl_sync_task_t
*dst
;
1033 snapallarg_t saa
= { 0 };
1038 err
= spa_open(snapname
, &spa
, FTAG
);
1041 saa
.saa_dstg
= dsl_sync_task_group_create(spa_get_dsl(spa
));
1043 saa
.saa_needsuspend
= (spa_version(spa
) < SPA_VERSION_FAST_SNAP
);
1044 saa
.saa_temporary
= B_TRUE
;
1046 if (cleanup_fd
< 0) {
1047 spa_close(spa
, FTAG
);
1050 if ((err
= zfs_onexit_fd_hold(cleanup_fd
, &minor
)) != 0) {
1051 spa_close(spa
, FTAG
);
1055 err
= snapshot_one_impl(snapname
, &saa
);
1058 err
= dsl_sync_task_group_wait(saa
.saa_dstg
);
1060 for (dst
= list_head(&saa
.saa_dstg
->dstg_tasks
); dst
;
1061 dst
= list_next(&saa
.saa_dstg
->dstg_tasks
, dst
)) {
1062 objset_t
*os
= dst
->dst_arg1
;
1063 dsl_register_onexit_hold_cleanup(saa
.saa_newds
, tag
, minor
);
1064 if (saa
.saa_needsuspend
)
1065 zil_resume(dmu_objset_zil(os
));
1066 dmu_objset_rele(os
, &saa
);
1069 zfs_onexit_fd_rele(cleanup_fd
);
1070 dsl_sync_task_group_destroy(saa
.saa_dstg
);
1071 spa_close(spa
, FTAG
);
1077 dmu_objset_sync_dnodes(list_t
*list
, list_t
*newlist
, dmu_tx_t
*tx
)
1081 while ((dn
= list_head(list
))) {
1082 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
);
1083 ASSERT(dn
->dn_dbuf
->db_data_pending
);
1085 * Initialize dn_zio outside dnode_sync() because the
1086 * meta-dnode needs to set it ouside dnode_sync().
1088 dn
->dn_zio
= dn
->dn_dbuf
->db_data_pending
->dr_zio
;
1091 ASSERT3U(dn
->dn_nlevels
, <=, DN_MAX_LEVELS
);
1092 list_remove(list
, dn
);
1095 (void) dnode_add_ref(dn
, newlist
);
1096 list_insert_tail(newlist
, dn
);
1105 dmu_objset_write_ready(zio_t
*zio
, arc_buf_t
*abuf
, void *arg
)
1109 blkptr_t
*bp
= zio
->io_bp
;
1111 dnode_phys_t
*dnp
= &os
->os_phys
->os_meta_dnode
;
1113 ASSERT(bp
== os
->os_rootbp
);
1114 ASSERT(BP_GET_TYPE(bp
) == DMU_OT_OBJSET
);
1115 ASSERT(BP_GET_LEVEL(bp
) == 0);
1118 * Update rootbp fill count: it should be the number of objects
1119 * allocated in the object set (not counting the "special"
1120 * objects that are stored in the objset_phys_t -- the meta
1121 * dnode and user/group accounting objects).
1124 for (i
= 0; i
< dnp
->dn_nblkptr
; i
++)
1125 bp
->blk_fill
+= dnp
->dn_blkptr
[i
].blk_fill
;
1130 dmu_objset_write_done(zio_t
*zio
, arc_buf_t
*abuf
, void *arg
)
1132 blkptr_t
*bp
= zio
->io_bp
;
1133 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
1136 if (zio
->io_flags
& ZIO_FLAG_IO_REWRITE
) {
1137 ASSERT(BP_EQUAL(bp
, bp_orig
));
1139 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
1140 dmu_tx_t
*tx
= os
->os_synctx
;
1142 (void) dsl_dataset_block_kill(ds
, bp_orig
, tx
, B_TRUE
);
1143 dsl_dataset_block_born(ds
, bp
, tx
);
1147 /* called from dsl */
1149 dmu_objset_sync(objset_t
*os
, zio_t
*pio
, dmu_tx_t
*tx
)
1156 list_t
*newlist
= NULL
;
1157 dbuf_dirty_record_t
*dr
;
1159 dprintf_ds(os
->os_dsl_dataset
, "txg=%llu\n", tx
->tx_txg
);
1161 ASSERT(dmu_tx_is_syncing(tx
));
1162 /* XXX the write_done callback should really give us the tx... */
1165 if (os
->os_dsl_dataset
== NULL
) {
1167 * This is the MOS. If we have upgraded,
1168 * spa_max_replication() could change, so reset
1171 os
->os_copies
= spa_max_replication(os
->os_spa
);
1175 * Create the root block IO
1177 SET_BOOKMARK(&zb
, os
->os_dsl_dataset
?
1178 os
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
1179 ZB_ROOT_OBJECT
, ZB_ROOT_LEVEL
, ZB_ROOT_BLKID
);
1180 arc_release(os
->os_phys_buf
, &os
->os_phys_buf
);
1182 dmu_write_policy(os
, NULL
, 0, 0, &zp
);
1184 zio
= arc_write(pio
, os
->os_spa
, tx
->tx_txg
,
1185 os
->os_rootbp
, os
->os_phys_buf
, DMU_OS_IS_L2CACHEABLE(os
),
1186 DMU_OS_IS_L2COMPRESSIBLE(os
), &zp
, dmu_objset_write_ready
,
1187 dmu_objset_write_done
, os
, ZIO_PRIORITY_ASYNC_WRITE
,
1188 ZIO_FLAG_MUSTSUCCEED
, &zb
);
1191 * Sync special dnodes - the parent IO for the sync is the root block
1193 DMU_META_DNODE(os
)->dn_zio
= zio
;
1194 dnode_sync(DMU_META_DNODE(os
), tx
);
1196 os
->os_phys
->os_flags
= os
->os_flags
;
1198 if (DMU_USERUSED_DNODE(os
) &&
1199 DMU_USERUSED_DNODE(os
)->dn_type
!= DMU_OT_NONE
) {
1200 DMU_USERUSED_DNODE(os
)->dn_zio
= zio
;
1201 dnode_sync(DMU_USERUSED_DNODE(os
), tx
);
1202 DMU_GROUPUSED_DNODE(os
)->dn_zio
= zio
;
1203 dnode_sync(DMU_GROUPUSED_DNODE(os
), tx
);
1206 txgoff
= tx
->tx_txg
& TXG_MASK
;
1208 if (dmu_objset_userused_enabled(os
)) {
1209 newlist
= &os
->os_synced_dnodes
;
1211 * We must create the list here because it uses the
1212 * dn_dirty_link[] of this txg.
1214 list_create(newlist
, sizeof (dnode_t
),
1215 offsetof(dnode_t
, dn_dirty_link
[txgoff
]));
1218 dmu_objset_sync_dnodes(&os
->os_free_dnodes
[txgoff
], newlist
, tx
);
1219 dmu_objset_sync_dnodes(&os
->os_dirty_dnodes
[txgoff
], newlist
, tx
);
1221 list
= &DMU_META_DNODE(os
)->dn_dirty_records
[txgoff
];
1222 while ((dr
= list_head(list
))) {
1223 ASSERT(dr
->dr_dbuf
->db_level
== 0);
1224 list_remove(list
, dr
);
1226 zio_nowait(dr
->dr_zio
);
1229 * Free intent log blocks up to this tx.
1231 zil_sync(os
->os_zil
, tx
);
1232 os
->os_phys
->os_zil_header
= os
->os_zil_header
;
1237 dmu_objset_is_dirty(objset_t
*os
, uint64_t txg
)
1239 return (!list_is_empty(&os
->os_dirty_dnodes
[txg
& TXG_MASK
]) ||
1240 !list_is_empty(&os
->os_free_dnodes
[txg
& TXG_MASK
]));
1243 static objset_used_cb_t
*used_cbs
[DMU_OST_NUMTYPES
];
1246 dmu_objset_register_type(dmu_objset_type_t ost
, objset_used_cb_t
*cb
)
1252 dmu_objset_userused_enabled(objset_t
*os
)
1254 return (spa_version(os
->os_spa
) >= SPA_VERSION_USERSPACE
&&
1255 used_cbs
[os
->os_phys
->os_type
] != NULL
&&
1256 DMU_USERUSED_DNODE(os
) != NULL
);
1260 do_userquota_update(objset_t
*os
, uint64_t used
, uint64_t flags
,
1261 uint64_t user
, uint64_t group
, boolean_t subtract
, dmu_tx_t
*tx
)
1263 if ((flags
& DNODE_FLAG_USERUSED_ACCOUNTED
)) {
1264 int64_t delta
= DNODE_SIZE
+ used
;
1267 VERIFY3U(0, ==, zap_increment_int(os
, DMU_USERUSED_OBJECT
,
1269 VERIFY3U(0, ==, zap_increment_int(os
, DMU_GROUPUSED_OBJECT
,
1275 dmu_objset_do_userquota_updates(objset_t
*os
, dmu_tx_t
*tx
)
1278 list_t
*list
= &os
->os_synced_dnodes
;
1280 ASSERT(list_head(list
) == NULL
|| dmu_objset_userused_enabled(os
));
1282 while ((dn
= list_head(list
))) {
1284 ASSERT(!DMU_OBJECT_IS_SPECIAL(dn
->dn_object
));
1285 ASSERT(dn
->dn_phys
->dn_type
== DMU_OT_NONE
||
1286 dn
->dn_phys
->dn_flags
&
1287 DNODE_FLAG_USERUSED_ACCOUNTED
);
1289 /* Allocate the user/groupused objects if necessary. */
1290 if (DMU_USERUSED_DNODE(os
)->dn_type
== DMU_OT_NONE
) {
1291 VERIFY(0 == zap_create_claim(os
,
1292 DMU_USERUSED_OBJECT
,
1293 DMU_OT_USERGROUP_USED
, DMU_OT_NONE
, 0, tx
));
1294 VERIFY(0 == zap_create_claim(os
,
1295 DMU_GROUPUSED_OBJECT
,
1296 DMU_OT_USERGROUP_USED
, DMU_OT_NONE
, 0, tx
));
1300 * We intentionally modify the zap object even if the
1301 * net delta is zero. Otherwise
1302 * the block of the zap obj could be shared between
1303 * datasets but need to be different between them after
1307 flags
= dn
->dn_id_flags
;
1309 if (flags
& DN_ID_OLD_EXIST
) {
1310 do_userquota_update(os
, dn
->dn_oldused
, dn
->dn_oldflags
,
1311 dn
->dn_olduid
, dn
->dn_oldgid
, B_TRUE
, tx
);
1313 if (flags
& DN_ID_NEW_EXIST
) {
1314 do_userquota_update(os
, DN_USED_BYTES(dn
->dn_phys
),
1315 dn
->dn_phys
->dn_flags
, dn
->dn_newuid
,
1316 dn
->dn_newgid
, B_FALSE
, tx
);
1319 mutex_enter(&dn
->dn_mtx
);
1321 dn
->dn_oldflags
= 0;
1322 if (dn
->dn_id_flags
& DN_ID_NEW_EXIST
) {
1323 dn
->dn_olduid
= dn
->dn_newuid
;
1324 dn
->dn_oldgid
= dn
->dn_newgid
;
1325 dn
->dn_id_flags
|= DN_ID_OLD_EXIST
;
1326 if (dn
->dn_bonuslen
== 0)
1327 dn
->dn_id_flags
|= DN_ID_CHKED_SPILL
;
1329 dn
->dn_id_flags
|= DN_ID_CHKED_BONUS
;
1331 dn
->dn_id_flags
&= ~(DN_ID_NEW_EXIST
);
1332 mutex_exit(&dn
->dn_mtx
);
1334 list_remove(list
, dn
);
1335 dnode_rele(dn
, list
);
1340 * Returns a pointer to data to find uid/gid from
1342 * If a dirty record for transaction group that is syncing can't
1343 * be found then NULL is returned. In the NULL case it is assumed
1344 * the uid/gid aren't changing.
1347 dmu_objset_userquota_find_data(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1349 dbuf_dirty_record_t
*dr
, **drp
;
1352 if (db
->db_dirtycnt
== 0)
1353 return (db
->db
.db_data
); /* Nothing is changing */
1355 for (drp
= &db
->db_last_dirty
; (dr
= *drp
) != NULL
; drp
= &dr
->dr_next
)
1356 if (dr
->dr_txg
== tx
->tx_txg
)
1364 DB_DNODE_ENTER(dr
->dr_dbuf
);
1365 dn
= DB_DNODE(dr
->dr_dbuf
);
1367 if (dn
->dn_bonuslen
== 0 &&
1368 dr
->dr_dbuf
->db_blkid
== DMU_SPILL_BLKID
)
1369 data
= dr
->dt
.dl
.dr_data
->b_data
;
1371 data
= dr
->dt
.dl
.dr_data
;
1373 DB_DNODE_EXIT(dr
->dr_dbuf
);
1380 dmu_objset_userquota_get_ids(dnode_t
*dn
, boolean_t before
, dmu_tx_t
*tx
)
1382 objset_t
*os
= dn
->dn_objset
;
1384 dmu_buf_impl_t
*db
= NULL
;
1385 uint64_t *user
= NULL
, *group
= NULL
;
1386 int flags
= dn
->dn_id_flags
;
1388 boolean_t have_spill
= B_FALSE
;
1390 if (!dmu_objset_userused_enabled(dn
->dn_objset
))
1393 if (before
&& (flags
& (DN_ID_CHKED_BONUS
|DN_ID_OLD_EXIST
|
1394 DN_ID_CHKED_SPILL
)))
1397 if (before
&& dn
->dn_bonuslen
!= 0)
1398 data
= DN_BONUS(dn
->dn_phys
);
1399 else if (!before
&& dn
->dn_bonuslen
!= 0) {
1402 mutex_enter(&db
->db_mtx
);
1403 data
= dmu_objset_userquota_find_data(db
, tx
);
1405 data
= DN_BONUS(dn
->dn_phys
);
1407 } else if (dn
->dn_bonuslen
== 0 && dn
->dn_bonustype
== DMU_OT_SA
) {
1410 if (RW_WRITE_HELD(&dn
->dn_struct_rwlock
))
1411 rf
|= DB_RF_HAVESTRUCT
;
1412 error
= dmu_spill_hold_by_dnode(dn
,
1413 rf
| DB_RF_MUST_SUCCEED
,
1414 FTAG
, (dmu_buf_t
**)&db
);
1416 mutex_enter(&db
->db_mtx
);
1417 data
= (before
) ? db
->db
.db_data
:
1418 dmu_objset_userquota_find_data(db
, tx
);
1419 have_spill
= B_TRUE
;
1421 mutex_enter(&dn
->dn_mtx
);
1422 dn
->dn_id_flags
|= DN_ID_CHKED_BONUS
;
1423 mutex_exit(&dn
->dn_mtx
);
1429 user
= &dn
->dn_olduid
;
1430 group
= &dn
->dn_oldgid
;
1432 user
= &dn
->dn_newuid
;
1433 group
= &dn
->dn_newgid
;
1437 * Must always call the callback in case the object
1438 * type has changed and that type isn't an object type to track
1440 error
= used_cbs
[os
->os_phys
->os_type
](dn
->dn_bonustype
, data
,
1444 * Preserve existing uid/gid when the callback can't determine
1445 * what the new uid/gid are and the callback returned EEXIST.
1446 * The EEXIST error tells us to just use the existing uid/gid.
1447 * If we don't know what the old values are then just assign
1448 * them to 0, since that is a new file being created.
1450 if (!before
&& data
== NULL
&& error
== EEXIST
) {
1451 if (flags
& DN_ID_OLD_EXIST
) {
1452 dn
->dn_newuid
= dn
->dn_olduid
;
1453 dn
->dn_newgid
= dn
->dn_oldgid
;
1462 mutex_exit(&db
->db_mtx
);
1464 mutex_enter(&dn
->dn_mtx
);
1465 if (error
== 0 && before
)
1466 dn
->dn_id_flags
|= DN_ID_OLD_EXIST
;
1467 if (error
== 0 && !before
)
1468 dn
->dn_id_flags
|= DN_ID_NEW_EXIST
;
1471 dn
->dn_id_flags
|= DN_ID_CHKED_SPILL
;
1473 dn
->dn_id_flags
|= DN_ID_CHKED_BONUS
;
1475 mutex_exit(&dn
->dn_mtx
);
1477 dmu_buf_rele((dmu_buf_t
*)db
, FTAG
);
1481 dmu_objset_userspace_present(objset_t
*os
)
1483 return (os
->os_phys
->os_flags
&
1484 OBJSET_FLAG_USERACCOUNTING_COMPLETE
);
1488 dmu_objset_userspace_upgrade(objset_t
*os
)
1493 if (dmu_objset_userspace_present(os
))
1495 if (!dmu_objset_userused_enabled(os
))
1497 if (dmu_objset_is_snapshot(os
))
1501 * We simply need to mark every object dirty, so that it will be
1502 * synced out and now accounted. If this is called
1503 * concurrently, or if we already did some work before crashing,
1504 * that's fine, since we track each object's accounted state
1508 for (obj
= 0; err
== 0; err
= dmu_object_next(os
, &obj
, FALSE
, 0)) {
1513 if (issig(JUSTLOOKING
) && issig(FORREAL
))
1516 objerr
= dmu_bonus_hold(os
, obj
, FTAG
, &db
);
1519 tx
= dmu_tx_create(os
);
1520 dmu_tx_hold_bonus(tx
, obj
);
1521 objerr
= dmu_tx_assign(tx
, TXG_WAIT
);
1526 dmu_buf_will_dirty(db
, tx
);
1527 dmu_buf_rele(db
, FTAG
);
1531 os
->os_flags
|= OBJSET_FLAG_USERACCOUNTING_COMPLETE
;
1532 txg_wait_synced(dmu_objset_pool(os
), 0);
1537 dmu_objset_space(objset_t
*os
, uint64_t *refdbytesp
, uint64_t *availbytesp
,
1538 uint64_t *usedobjsp
, uint64_t *availobjsp
)
1540 dsl_dataset_space(os
->os_dsl_dataset
, refdbytesp
, availbytesp
,
1541 usedobjsp
, availobjsp
);
1545 dmu_objset_fsid_guid(objset_t
*os
)
1547 return (dsl_dataset_fsid_guid(os
->os_dsl_dataset
));
1551 dmu_objset_fast_stat(objset_t
*os
, dmu_objset_stats_t
*stat
)
1553 stat
->dds_type
= os
->os_phys
->os_type
;
1554 if (os
->os_dsl_dataset
)
1555 dsl_dataset_fast_stat(os
->os_dsl_dataset
, stat
);
1559 dmu_objset_stats(objset_t
*os
, nvlist_t
*nv
)
1561 ASSERT(os
->os_dsl_dataset
||
1562 os
->os_phys
->os_type
== DMU_OST_META
);
1564 if (os
->os_dsl_dataset
!= NULL
)
1565 dsl_dataset_stats(os
->os_dsl_dataset
, nv
);
1567 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_TYPE
,
1568 os
->os_phys
->os_type
);
1569 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_USERACCOUNTING
,
1570 dmu_objset_userspace_present(os
));
1574 dmu_objset_is_snapshot(objset_t
*os
)
1576 if (os
->os_dsl_dataset
!= NULL
)
1577 return (dsl_dataset_is_snapshot(os
->os_dsl_dataset
));
1583 dmu_snapshot_realname(objset_t
*os
, char *name
, char *real
, int maxlen
,
1584 boolean_t
*conflict
)
1586 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
1589 if (ds
->ds_phys
->ds_snapnames_zapobj
== 0)
1592 return (zap_lookup_norm(ds
->ds_dir
->dd_pool
->dp_meta_objset
,
1593 ds
->ds_phys
->ds_snapnames_zapobj
, name
, 8, 1, &ignored
, MT_FIRST
,
1594 real
, maxlen
, conflict
));
1598 dmu_snapshot_list_next(objset_t
*os
, int namelen
, char *name
,
1599 uint64_t *idp
, uint64_t *offp
, boolean_t
*case_conflict
)
1601 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
1602 zap_cursor_t cursor
;
1603 zap_attribute_t attr
;
1605 if (ds
->ds_phys
->ds_snapnames_zapobj
== 0)
1608 zap_cursor_init_serialized(&cursor
,
1609 ds
->ds_dir
->dd_pool
->dp_meta_objset
,
1610 ds
->ds_phys
->ds_snapnames_zapobj
, *offp
);
1612 if (zap_cursor_retrieve(&cursor
, &attr
) != 0) {
1613 zap_cursor_fini(&cursor
);
1617 if (strlen(attr
.za_name
) + 1 > namelen
) {
1618 zap_cursor_fini(&cursor
);
1619 return (ENAMETOOLONG
);
1622 (void) strcpy(name
, attr
.za_name
);
1624 *idp
= attr
.za_first_integer
;
1626 *case_conflict
= attr
.za_normalization_conflict
;
1627 zap_cursor_advance(&cursor
);
1628 *offp
= zap_cursor_serialize(&cursor
);
1629 zap_cursor_fini(&cursor
);
1635 dmu_snapshot_lookup(objset_t
*os
, const char *name
, uint64_t *value
)
1637 return dsl_dataset_snap_lookup(os
->os_dsl_dataset
, name
, value
);
1641 dmu_dir_list_next(objset_t
*os
, int namelen
, char *name
,
1642 uint64_t *idp
, uint64_t *offp
)
1644 dsl_dir_t
*dd
= os
->os_dsl_dataset
->ds_dir
;
1645 zap_cursor_t cursor
;
1646 zap_attribute_t attr
;
1648 /* there is no next dir on a snapshot! */
1649 if (os
->os_dsl_dataset
->ds_object
!=
1650 dd
->dd_phys
->dd_head_dataset_obj
)
1653 zap_cursor_init_serialized(&cursor
,
1654 dd
->dd_pool
->dp_meta_objset
,
1655 dd
->dd_phys
->dd_child_dir_zapobj
, *offp
);
1657 if (zap_cursor_retrieve(&cursor
, &attr
) != 0) {
1658 zap_cursor_fini(&cursor
);
1662 if (strlen(attr
.za_name
) + 1 > namelen
) {
1663 zap_cursor_fini(&cursor
);
1664 return (ENAMETOOLONG
);
1667 (void) strcpy(name
, attr
.za_name
);
1669 *idp
= attr
.za_first_integer
;
1670 zap_cursor_advance(&cursor
);
1671 *offp
= zap_cursor_serialize(&cursor
);
1672 zap_cursor_fini(&cursor
);
1678 int (*func
)(const char *, void *);
1684 findfunc(spa_t
*spa
, uint64_t dsobj
, const char *dsname
, void *arg
)
1686 struct findarg
*fa
= arg
;
1687 return (fa
->func(dsname
, fa
->arg
));
1691 * Find all objsets under name, and for each, call 'func(child_name, arg)'.
1692 * Perhaps change all callers to use dmu_objset_find_spa()?
1695 dmu_objset_find(char *name
, int func(const char *, void *), void *arg
,
1701 return (dmu_objset_find_spa(NULL
, name
, findfunc
, &fa
, flags
));
1705 * Find all objsets under name, call func on each
1708 dmu_objset_find_spa(spa_t
*spa
, const char *name
,
1709 int func(spa_t
*, uint64_t, const char *, void *), void *arg
, int flags
)
1715 zap_attribute_t
*attr
;
1721 name
= spa_name(spa
);
1722 err
= dsl_dir_open_spa(spa
, name
, FTAG
, &dd
, NULL
);
1726 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1727 if (dd
->dd_myname
[0] == '$') {
1728 dsl_dir_close(dd
, FTAG
);
1732 thisobj
= dd
->dd_phys
->dd_head_dataset_obj
;
1733 attr
= kmem_alloc(sizeof (zap_attribute_t
), KM_PUSHPAGE
);
1737 * Iterate over all children.
1739 if (flags
& DS_FIND_CHILDREN
) {
1740 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
,
1741 dd
->dd_phys
->dd_child_dir_zapobj
);
1742 zap_cursor_retrieve(&zc
, attr
) == 0;
1743 (void) zap_cursor_advance(&zc
)) {
1744 ASSERT(attr
->za_integer_length
== sizeof (uint64_t));
1745 ASSERT(attr
->za_num_integers
== 1);
1747 child
= kmem_asprintf("%s/%s", name
, attr
->za_name
);
1748 err
= dmu_objset_find_spa(spa
, child
, func
, arg
, flags
);
1753 zap_cursor_fini(&zc
);
1756 dsl_dir_close(dd
, FTAG
);
1757 kmem_free(attr
, sizeof (zap_attribute_t
));
1763 * Iterate over all snapshots.
1765 if (flags
& DS_FIND_SNAPSHOTS
) {
1766 if (!dsl_pool_sync_context(dp
))
1767 rw_enter(&dp
->dp_config_rwlock
, RW_READER
);
1768 err
= dsl_dataset_hold_obj(dp
, thisobj
, FTAG
, &ds
);
1769 if (!dsl_pool_sync_context(dp
))
1770 rw_exit(&dp
->dp_config_rwlock
);
1773 uint64_t snapobj
= ds
->ds_phys
->ds_snapnames_zapobj
;
1774 dsl_dataset_rele(ds
, FTAG
);
1776 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
, snapobj
);
1777 zap_cursor_retrieve(&zc
, attr
) == 0;
1778 (void) zap_cursor_advance(&zc
)) {
1779 ASSERT(attr
->za_integer_length
==
1781 ASSERT(attr
->za_num_integers
== 1);
1783 child
= kmem_asprintf("%s@%s",
1784 name
, attr
->za_name
);
1785 err
= func(spa
, attr
->za_first_integer
,
1791 zap_cursor_fini(&zc
);
1795 dsl_dir_close(dd
, FTAG
);
1796 kmem_free(attr
, sizeof (zap_attribute_t
));
1802 * Apply to self if appropriate.
1804 err
= func(spa
, thisobj
, name
, arg
);
1810 dmu_objset_prefetch(const char *name
, void *arg
)
1814 if (dsl_dataset_hold(name
, FTAG
, &ds
))
1817 if (!BP_IS_HOLE(&ds
->ds_phys
->ds_bp
)) {
1818 mutex_enter(&ds
->ds_opening_lock
);
1819 if (ds
->ds_objset
== NULL
) {
1820 uint32_t aflags
= ARC_NOWAIT
| ARC_PREFETCH
;
1823 SET_BOOKMARK(&zb
, ds
->ds_object
, ZB_ROOT_OBJECT
,
1824 ZB_ROOT_LEVEL
, ZB_ROOT_BLKID
);
1826 (void) arc_read(NULL
, dsl_dataset_get_spa(ds
),
1827 &ds
->ds_phys
->ds_bp
, NULL
, NULL
,
1828 ZIO_PRIORITY_ASYNC_READ
,
1829 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
1832 mutex_exit(&ds
->ds_opening_lock
);
1835 dsl_dataset_rele(ds
, FTAG
);
1840 dmu_objset_set_user(objset_t
*os
, void *user_ptr
)
1842 ASSERT(MUTEX_HELD(&os
->os_user_ptr_lock
));
1843 os
->os_user_ptr
= user_ptr
;
1847 dmu_objset_get_user(objset_t
*os
)
1849 ASSERT(MUTEX_HELD(&os
->os_user_ptr_lock
));
1850 return (os
->os_user_ptr
);
1853 #if defined(_KERNEL) && defined(HAVE_SPL)
1854 EXPORT_SYMBOL(dmu_objset_zil
);
1855 EXPORT_SYMBOL(dmu_objset_pool
);
1856 EXPORT_SYMBOL(dmu_objset_ds
);
1857 EXPORT_SYMBOL(dmu_objset_type
);
1858 EXPORT_SYMBOL(dmu_objset_name
);
1859 EXPORT_SYMBOL(dmu_objset_hold
);
1860 EXPORT_SYMBOL(dmu_objset_own
);
1861 EXPORT_SYMBOL(dmu_objset_rele
);
1862 EXPORT_SYMBOL(dmu_objset_disown
);
1863 EXPORT_SYMBOL(dmu_objset_from_ds
);
1864 EXPORT_SYMBOL(dmu_objset_create
);
1865 EXPORT_SYMBOL(dmu_objset_clone
);
1866 EXPORT_SYMBOL(dmu_objset_destroy
);
1867 EXPORT_SYMBOL(dmu_objset_snapshot
);
1868 EXPORT_SYMBOL(dmu_objset_stats
);
1869 EXPORT_SYMBOL(dmu_objset_fast_stat
);
1870 EXPORT_SYMBOL(dmu_objset_spa
);
1871 EXPORT_SYMBOL(dmu_objset_space
);
1872 EXPORT_SYMBOL(dmu_objset_fsid_guid
);
1873 EXPORT_SYMBOL(dmu_objset_find
);
1874 EXPORT_SYMBOL(dmu_objset_find_spa
);
1875 EXPORT_SYMBOL(dmu_objset_prefetch
);
1876 EXPORT_SYMBOL(dmu_objset_byteswap
);
1877 EXPORT_SYMBOL(dmu_objset_evict_dbufs
);
1878 EXPORT_SYMBOL(dmu_objset_snap_cmtime
);
1880 EXPORT_SYMBOL(dmu_objset_sync
);
1881 EXPORT_SYMBOL(dmu_objset_is_dirty
);
1882 EXPORT_SYMBOL(dmu_objset_create_impl
);
1883 EXPORT_SYMBOL(dmu_objset_open_impl
);
1884 EXPORT_SYMBOL(dmu_objset_evict
);
1885 EXPORT_SYMBOL(dmu_objset_register_type
);
1886 EXPORT_SYMBOL(dmu_objset_do_userquota_updates
);
1887 EXPORT_SYMBOL(dmu_objset_userquota_get_ids
);
1888 EXPORT_SYMBOL(dmu_objset_userused_enabled
);
1889 EXPORT_SYMBOL(dmu_objset_userspace_upgrade
);
1890 EXPORT_SYMBOL(dmu_objset_userspace_present
);