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 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 #include <sys/zfs_context.h>
28 #include <sys/dmu_objset.h>
29 #include <sys/dsl_dir.h>
30 #include <sys/dsl_dataset.h>
31 #include <sys/dsl_prop.h>
32 #include <sys/dsl_pool.h>
33 #include <sys/dsl_synctask.h>
34 #include <sys/dsl_deleg.h>
35 #include <sys/dnode.h>
38 #include <sys/dmu_tx.h>
39 #include <sys/zio_checksum.h>
42 #include <sys/dmu_impl.h>
43 #include <sys/zfs_ioctl.h>
46 dmu_objset_spa(objset_t
*os
)
48 return (os
->os
->os_spa
);
52 dmu_objset_zil(objset_t
*os
)
54 return (os
->os
->os_zil
);
58 dmu_objset_pool(objset_t
*os
)
62 if ((ds
= os
->os
->os_dsl_dataset
) != NULL
&& ds
->ds_dir
)
63 return (ds
->ds_dir
->dd_pool
);
65 return (spa_get_dsl(os
->os
->os_spa
));
69 dmu_objset_ds(objset_t
*os
)
71 return (os
->os
->os_dsl_dataset
);
75 dmu_objset_type(objset_t
*os
)
77 return (os
->os
->os_phys
->os_type
);
81 dmu_objset_name(objset_t
*os
, char *buf
)
83 dsl_dataset_name(os
->os
->os_dsl_dataset
, buf
);
87 dmu_objset_id(objset_t
*os
)
89 dsl_dataset_t
*ds
= os
->os
->os_dsl_dataset
;
91 return (ds
? ds
->ds_object
: 0);
95 checksum_changed_cb(void *arg
, uint64_t newval
)
97 objset_impl_t
*osi
= arg
;
100 * Inheritance should have been done by now.
102 ASSERT(newval
!= ZIO_CHECKSUM_INHERIT
);
104 osi
->os_checksum
= zio_checksum_select(newval
, ZIO_CHECKSUM_ON_VALUE
);
108 compression_changed_cb(void *arg
, uint64_t newval
)
110 objset_impl_t
*osi
= arg
;
113 * Inheritance and range checking should have been done by now.
115 ASSERT(newval
!= ZIO_COMPRESS_INHERIT
);
117 osi
->os_compress
= zio_compress_select(newval
, ZIO_COMPRESS_ON_VALUE
);
121 copies_changed_cb(void *arg
, uint64_t newval
)
123 objset_impl_t
*osi
= arg
;
126 * Inheritance and range checking should have been done by now.
129 ASSERT(newval
<= spa_max_replication(osi
->os_spa
));
131 osi
->os_copies
= newval
;
135 primary_cache_changed_cb(void *arg
, uint64_t newval
)
137 objset_impl_t
*osi
= arg
;
140 * Inheritance and range checking should have been done by now.
142 ASSERT(newval
== ZFS_CACHE_ALL
|| newval
== ZFS_CACHE_NONE
||
143 newval
== ZFS_CACHE_METADATA
);
145 osi
->os_primary_cache
= newval
;
149 secondary_cache_changed_cb(void *arg
, uint64_t newval
)
151 objset_impl_t
*osi
= arg
;
154 * Inheritance and range checking should have been done by now.
156 ASSERT(newval
== ZFS_CACHE_ALL
|| newval
== ZFS_CACHE_NONE
||
157 newval
== ZFS_CACHE_METADATA
);
159 osi
->os_secondary_cache
= newval
;
163 dmu_objset_byteswap(void *buf
, size_t size
)
165 objset_phys_t
*osp
= buf
;
167 ASSERT(size
== OBJSET_OLD_PHYS_SIZE
|| size
== sizeof (objset_phys_t
));
168 dnode_byteswap(&osp
->os_meta_dnode
);
169 byteswap_uint64_array(&osp
->os_zil_header
, sizeof (zil_header_t
));
170 osp
->os_type
= BSWAP_64(osp
->os_type
);
171 osp
->os_flags
= BSWAP_64(osp
->os_flags
);
172 if (size
== sizeof (objset_phys_t
)) {
173 dnode_byteswap(&osp
->os_userused_dnode
);
174 dnode_byteswap(&osp
->os_groupused_dnode
);
179 dmu_objset_open_impl(spa_t
*spa
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
180 objset_impl_t
**osip
)
185 ASSERT(ds
== NULL
|| MUTEX_HELD(&ds
->ds_opening_lock
));
187 osi
= kmem_zalloc(sizeof (objset_impl_t
), KM_SLEEP
);
189 osi
->os_dsl_dataset
= ds
;
192 if (!BP_IS_HOLE(osi
->os_rootbp
)) {
193 uint32_t aflags
= ARC_WAIT
;
195 zb
.zb_objset
= ds
? ds
->ds_object
: 0;
199 if (DMU_OS_IS_L2CACHEABLE(osi
))
200 aflags
|= ARC_L2CACHE
;
202 dprintf_bp(osi
->os_rootbp
, "reading %s", "");
204 * NB: when bprewrite scrub can change the bp,
205 * and this is called from dmu_objset_open_ds_os, the bp
206 * could change, and we'll need a lock.
208 err
= arc_read_nolock(NULL
, spa
, osi
->os_rootbp
,
209 arc_getbuf_func
, &osi
->os_phys_buf
,
210 ZIO_PRIORITY_SYNC_READ
, ZIO_FLAG_CANFAIL
, &aflags
, &zb
);
212 kmem_free(osi
, sizeof (objset_impl_t
));
213 /* convert checksum errors into IO errors */
219 /* Increase the blocksize if we are permitted. */
220 if (spa_version(spa
) >= SPA_VERSION_USERSPACE
&&
221 arc_buf_size(osi
->os_phys_buf
) < sizeof (objset_phys_t
)) {
222 arc_buf_t
*buf
= arc_buf_alloc(spa
,
223 sizeof (objset_phys_t
), &osi
->os_phys_buf
,
225 bzero(buf
->b_data
, sizeof (objset_phys_t
));
226 bcopy(osi
->os_phys_buf
->b_data
, buf
->b_data
,
227 arc_buf_size(osi
->os_phys_buf
));
228 (void) arc_buf_remove_ref(osi
->os_phys_buf
,
230 osi
->os_phys_buf
= buf
;
233 osi
->os_phys
= osi
->os_phys_buf
->b_data
;
234 osi
->os_flags
= osi
->os_phys
->os_flags
;
236 int size
= spa_version(spa
) >= SPA_VERSION_USERSPACE
?
237 sizeof (objset_phys_t
) : OBJSET_OLD_PHYS_SIZE
;
238 osi
->os_phys_buf
= arc_buf_alloc(spa
, size
,
239 &osi
->os_phys_buf
, ARC_BUFC_METADATA
);
240 osi
->os_phys
= osi
->os_phys_buf
->b_data
;
241 bzero(osi
->os_phys
, size
);
245 * Note: the changed_cb will be called once before the register
246 * func returns, thus changing the checksum/compression from the
247 * default (fletcher2/off). Snapshots don't need to know about
248 * checksum/compression/copies.
251 err
= dsl_prop_register(ds
, "primarycache",
252 primary_cache_changed_cb
, osi
);
254 err
= dsl_prop_register(ds
, "secondarycache",
255 secondary_cache_changed_cb
, osi
);
256 if (!dsl_dataset_is_snapshot(ds
)) {
258 err
= dsl_prop_register(ds
, "checksum",
259 checksum_changed_cb
, osi
);
261 err
= dsl_prop_register(ds
, "compression",
262 compression_changed_cb
, osi
);
264 err
= dsl_prop_register(ds
, "copies",
265 copies_changed_cb
, osi
);
268 VERIFY(arc_buf_remove_ref(osi
->os_phys_buf
,
269 &osi
->os_phys_buf
) == 1);
270 kmem_free(osi
, sizeof (objset_impl_t
));
273 } else if (ds
== NULL
) {
274 /* It's the meta-objset. */
275 osi
->os_checksum
= ZIO_CHECKSUM_FLETCHER_4
;
276 osi
->os_compress
= ZIO_COMPRESS_LZJB
;
277 osi
->os_copies
= spa_max_replication(spa
);
278 osi
->os_primary_cache
= ZFS_CACHE_ALL
;
279 osi
->os_secondary_cache
= ZFS_CACHE_ALL
;
282 osi
->os_zil_header
= osi
->os_phys
->os_zil_header
;
283 osi
->os_zil
= zil_alloc(&osi
->os
, &osi
->os_zil_header
);
285 for (i
= 0; i
< TXG_SIZE
; i
++) {
286 list_create(&osi
->os_dirty_dnodes
[i
], sizeof (dnode_t
),
287 offsetof(dnode_t
, dn_dirty_link
[i
]));
288 list_create(&osi
->os_free_dnodes
[i
], sizeof (dnode_t
),
289 offsetof(dnode_t
, dn_dirty_link
[i
]));
291 list_create(&osi
->os_dnodes
, sizeof (dnode_t
),
292 offsetof(dnode_t
, dn_link
));
293 list_create(&osi
->os_downgraded_dbufs
, sizeof (dmu_buf_impl_t
),
294 offsetof(dmu_buf_impl_t
, db_link
));
296 mutex_init(&osi
->os_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
297 mutex_init(&osi
->os_obj_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
298 mutex_init(&osi
->os_user_ptr_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
300 osi
->os_meta_dnode
= dnode_special_open(osi
,
301 &osi
->os_phys
->os_meta_dnode
, DMU_META_DNODE_OBJECT
);
302 if (arc_buf_size(osi
->os_phys_buf
) >= sizeof (objset_phys_t
)) {
303 osi
->os_userused_dnode
= dnode_special_open(osi
,
304 &osi
->os_phys
->os_userused_dnode
, DMU_USERUSED_OBJECT
);
305 osi
->os_groupused_dnode
= dnode_special_open(osi
,
306 &osi
->os_phys
->os_groupused_dnode
, DMU_GROUPUSED_OBJECT
);
310 * We should be the only thread trying to do this because we
311 * have ds_opening_lock
314 VERIFY(NULL
== dsl_dataset_set_user_ptr(ds
, osi
,
323 dmu_objset_open_ds_os(dsl_dataset_t
*ds
, objset_t
*os
, dmu_objset_type_t type
)
327 mutex_enter(&ds
->ds_opening_lock
);
328 osi
= dsl_dataset_get_user_ptr(ds
);
332 err
= dmu_objset_open_impl(dsl_dataset_get_spa(ds
),
333 ds
, &ds
->ds_phys
->ds_bp
, &osi
);
335 mutex_exit(&ds
->ds_opening_lock
);
339 mutex_exit(&ds
->ds_opening_lock
);
342 os
->os_mode
= DS_MODE_NOHOLD
;
344 if (type
!= DMU_OST_ANY
&& type
!= os
->os
->os_phys
->os_type
)
350 dmu_objset_open_ds(dsl_dataset_t
*ds
, dmu_objset_type_t type
, objset_t
**osp
)
355 os
= kmem_alloc(sizeof (objset_t
), KM_SLEEP
);
356 err
= dmu_objset_open_ds_os(ds
, os
, type
);
358 kmem_free(os
, sizeof (objset_t
));
364 /* called from zpl */
366 dmu_objset_open(const char *name
, dmu_objset_type_t type
, int mode
,
373 ASSERT(DS_MODE_TYPE(mode
) == DS_MODE_USER
||
374 DS_MODE_TYPE(mode
) == DS_MODE_OWNER
);
376 os
= kmem_alloc(sizeof (objset_t
), KM_SLEEP
);
377 if (DS_MODE_TYPE(mode
) == DS_MODE_USER
)
378 err
= dsl_dataset_hold(name
, os
, &ds
);
380 err
= dsl_dataset_own(name
, mode
, os
, &ds
);
382 kmem_free(os
, sizeof (objset_t
));
386 err
= dmu_objset_open_ds_os(ds
, os
, type
);
388 if (DS_MODE_TYPE(mode
) == DS_MODE_USER
)
389 dsl_dataset_rele(ds
, os
);
391 dsl_dataset_disown(ds
, os
);
392 kmem_free(os
, sizeof (objset_t
));
401 dmu_objset_close(objset_t
*os
)
403 ASSERT(DS_MODE_TYPE(os
->os_mode
) == DS_MODE_USER
||
404 DS_MODE_TYPE(os
->os_mode
) == DS_MODE_OWNER
||
405 DS_MODE_TYPE(os
->os_mode
) == DS_MODE_NOHOLD
);
407 if (DS_MODE_TYPE(os
->os_mode
) == DS_MODE_USER
)
408 dsl_dataset_rele(os
->os
->os_dsl_dataset
, os
);
409 else if (DS_MODE_TYPE(os
->os_mode
) == DS_MODE_OWNER
)
410 dsl_dataset_disown(os
->os
->os_dsl_dataset
, os
);
411 kmem_free(os
, sizeof (objset_t
));
415 dmu_objset_evict_dbufs(objset_t
*os
)
417 objset_impl_t
*osi
= os
->os
;
420 mutex_enter(&osi
->os_lock
);
422 /* process the mdn last, since the other dnodes have holds on it */
423 list_remove(&osi
->os_dnodes
, osi
->os_meta_dnode
);
424 list_insert_tail(&osi
->os_dnodes
, osi
->os_meta_dnode
);
427 * Find the first dnode with holds. We have to do this dance
428 * because dnode_add_ref() only works if you already have a
429 * hold. If there are no holds then it has no dbufs so OK to
432 for (dn
= list_head(&osi
->os_dnodes
);
433 dn
&& !dnode_add_ref(dn
, FTAG
);
434 dn
= list_next(&osi
->os_dnodes
, dn
))
438 dnode_t
*next_dn
= dn
;
441 next_dn
= list_next(&osi
->os_dnodes
, next_dn
);
442 } while (next_dn
&& !dnode_add_ref(next_dn
, FTAG
));
444 mutex_exit(&osi
->os_lock
);
445 dnode_evict_dbufs(dn
);
446 dnode_rele(dn
, FTAG
);
447 mutex_enter(&osi
->os_lock
);
450 mutex_exit(&osi
->os_lock
);
451 return (list_head(&osi
->os_dnodes
) != osi
->os_meta_dnode
);
455 dmu_objset_evict(dsl_dataset_t
*ds
, void *arg
)
457 objset_impl_t
*osi
= arg
;
461 for (i
= 0; i
< TXG_SIZE
; i
++) {
462 ASSERT(list_head(&osi
->os_dirty_dnodes
[i
]) == NULL
);
463 ASSERT(list_head(&osi
->os_free_dnodes
[i
]) == NULL
);
467 if (!dsl_dataset_is_snapshot(ds
)) {
468 VERIFY(0 == dsl_prop_unregister(ds
, "checksum",
469 checksum_changed_cb
, osi
));
470 VERIFY(0 == dsl_prop_unregister(ds
, "compression",
471 compression_changed_cb
, osi
));
472 VERIFY(0 == dsl_prop_unregister(ds
, "copies",
473 copies_changed_cb
, osi
));
475 VERIFY(0 == dsl_prop_unregister(ds
, "primarycache",
476 primary_cache_changed_cb
, osi
));
477 VERIFY(0 == dsl_prop_unregister(ds
, "secondarycache",
478 secondary_cache_changed_cb
, osi
));
482 * We should need only a single pass over the dnode list, since
483 * nothing can be added to the list at this point.
486 (void) dmu_objset_evict_dbufs(&os
);
488 dnode_special_close(osi
->os_meta_dnode
);
489 if (osi
->os_userused_dnode
) {
490 dnode_special_close(osi
->os_userused_dnode
);
491 dnode_special_close(osi
->os_groupused_dnode
);
493 zil_free(osi
->os_zil
);
495 ASSERT3P(list_head(&osi
->os_dnodes
), ==, NULL
);
497 VERIFY(arc_buf_remove_ref(osi
->os_phys_buf
, &osi
->os_phys_buf
) == 1);
498 mutex_destroy(&osi
->os_lock
);
499 mutex_destroy(&osi
->os_obj_lock
);
500 mutex_destroy(&osi
->os_user_ptr_lock
);
501 kmem_free(osi
, sizeof (objset_impl_t
));
504 /* called from dsl for meta-objset */
506 dmu_objset_create_impl(spa_t
*spa
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
507 dmu_objset_type_t type
, dmu_tx_t
*tx
)
512 ASSERT(dmu_tx_is_syncing(tx
));
514 mutex_enter(&ds
->ds_opening_lock
);
515 VERIFY(0 == dmu_objset_open_impl(spa
, ds
, bp
, &osi
));
517 mutex_exit(&ds
->ds_opening_lock
);
518 mdn
= osi
->os_meta_dnode
;
520 dnode_allocate(mdn
, DMU_OT_DNODE
, 1 << DNODE_BLOCK_SHIFT
,
521 DN_MAX_INDBLKSHIFT
, DMU_OT_NONE
, 0, tx
);
524 * We don't want to have to increase the meta-dnode's nlevels
525 * later, because then we could do it in quescing context while
526 * we are also accessing it in open context.
528 * This precaution is not necessary for the MOS (ds == NULL),
529 * because the MOS is only updated in syncing context.
530 * This is most fortunate: the MOS is the only objset that
531 * needs to be synced multiple times as spa_sync() iterates
532 * to convergence, so minimizing its dn_nlevels matters.
538 * Determine the number of levels necessary for the meta-dnode
539 * to contain DN_MAX_OBJECT dnodes.
541 while ((uint64_t)mdn
->dn_nblkptr
<< (mdn
->dn_datablkshift
+
542 (levels
- 1) * (mdn
->dn_indblkshift
- SPA_BLKPTRSHIFT
)) <
543 DN_MAX_OBJECT
* sizeof (dnode_phys_t
))
546 mdn
->dn_next_nlevels
[tx
->tx_txg
& TXG_MASK
] =
547 mdn
->dn_nlevels
= levels
;
550 ASSERT(type
!= DMU_OST_NONE
);
551 ASSERT(type
!= DMU_OST_ANY
);
552 ASSERT(type
< DMU_OST_NUMTYPES
);
553 osi
->os_phys
->os_type
= type
;
554 if (dmu_objset_userused_enabled(osi
)) {
555 osi
->os_phys
->os_flags
|= OBJSET_FLAG_USERACCOUNTING_COMPLETE
;
556 osi
->os_flags
= osi
->os_phys
->os_flags
;
559 dsl_dataset_dirty(ds
, tx
);
565 void (*userfunc
)(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
);
567 dsl_dataset_t
*clone_parent
;
568 const char *lastname
;
569 dmu_objset_type_t type
;
575 dmu_objset_create_check(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
577 dsl_dir_t
*dd
= arg1
;
578 struct oscarg
*oa
= arg2
;
579 objset_t
*mos
= dd
->dd_pool
->dp_meta_objset
;
583 err
= zap_lookup(mos
, dd
->dd_phys
->dd_child_dir_zapobj
,
584 oa
->lastname
, sizeof (uint64_t), 1, &ddobj
);
586 return (err
? err
: EEXIST
);
588 if (oa
->clone_parent
!= NULL
) {
590 * You can't clone across pools.
592 if (oa
->clone_parent
->ds_dir
->dd_pool
!= dd
->dd_pool
)
596 * You can only clone snapshots, not the head datasets.
598 if (oa
->clone_parent
->ds_phys
->ds_num_children
== 0)
606 dmu_objset_create_sync(void *arg1
, void *arg2
, cred_t
*cr
, dmu_tx_t
*tx
)
608 dsl_dir_t
*dd
= arg1
;
609 struct oscarg
*oa
= arg2
;
614 ASSERT(dmu_tx_is_syncing(tx
));
616 dsobj
= dsl_dataset_create_sync(dd
, oa
->lastname
,
617 oa
->clone_parent
, oa
->flags
, cr
, tx
);
619 VERIFY(0 == dsl_dataset_hold_obj(dd
->dd_pool
, dsobj
, FTAG
, &ds
));
620 bp
= dsl_dataset_get_blkptr(ds
);
621 if (BP_IS_HOLE(bp
)) {
624 /* This is an empty dmu_objset; not a clone. */
625 osi
= dmu_objset_create_impl(dsl_dataset_get_spa(ds
),
626 ds
, bp
, oa
->type
, tx
);
629 oa
->userfunc(&osi
->os
, oa
->userarg
, cr
, tx
);
632 spa_history_internal_log(LOG_DS_CREATE
, dd
->dd_pool
->dp_spa
,
633 tx
, cr
, "dataset = %llu", dsobj
);
635 dsl_dataset_rele(ds
, FTAG
);
639 dmu_objset_create(const char *name
, dmu_objset_type_t type
,
640 objset_t
*clone_parent
, uint64_t flags
,
641 void (*func
)(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
), void *arg
)
646 struct oscarg oa
= { 0 };
648 ASSERT(strchr(name
, '@') == NULL
);
649 err
= dsl_dir_open(name
, FTAG
, &pdd
, &tail
);
653 dsl_dir_close(pdd
, FTAG
);
657 dprintf("name=%s\n", name
);
665 if (clone_parent
!= NULL
) {
667 * You can't clone to a different type.
669 if (clone_parent
->os
->os_phys
->os_type
!= type
) {
670 dsl_dir_close(pdd
, FTAG
);
673 oa
.clone_parent
= clone_parent
->os
->os_dsl_dataset
;
675 err
= dsl_sync_task_do(pdd
->dd_pool
, dmu_objset_create_check
,
676 dmu_objset_create_sync
, pdd
, &oa
, 5);
677 dsl_dir_close(pdd
, FTAG
);
682 dmu_objset_destroy(const char *name
)
688 * If it looks like we'll be able to destroy it, and there's
689 * an unplayed replay log sitting around, destroy the log.
690 * It would be nicer to do this in dsl_dataset_destroy_sync(),
691 * but the replay log objset is modified in open context.
693 error
= dmu_objset_open(name
, DMU_OST_ANY
,
694 DS_MODE_OWNER
|DS_MODE_READONLY
|DS_MODE_INCONSISTENT
, &os
);
696 dsl_dataset_t
*ds
= os
->os
->os_dsl_dataset
;
697 zil_destroy(dmu_objset_zil(os
), B_FALSE
);
699 error
= dsl_dataset_destroy(ds
, os
);
701 * dsl_dataset_destroy() closes the ds.
703 kmem_free(os
, sizeof (objset_t
));
710 * This will close the objset.
713 dmu_objset_rollback(objset_t
*os
)
718 ds
= os
->os
->os_dsl_dataset
;
720 if (!dsl_dataset_tryown(ds
, TRUE
, os
)) {
721 dmu_objset_close(os
);
725 err
= dsl_dataset_rollback(ds
, os
->os
->os_phys
->os_type
);
728 * NB: we close the objset manually because the rollback
729 * actually implicitly called dmu_objset_evict(), thus freeing
732 dsl_dataset_disown(ds
, os
);
733 kmem_free(os
, sizeof (objset_t
));
738 dsl_sync_task_group_t
*dstg
;
740 char failed
[MAXPATHLEN
];
741 boolean_t checkperms
;
746 snapshot_check(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
749 struct snaparg
*sn
= arg2
;
751 /* The props have already been checked by zfs_check_userprops(). */
753 return (dsl_dataset_snapshot_check(os
->os
->os_dsl_dataset
,
758 snapshot_sync(void *arg1
, void *arg2
, cred_t
*cr
, dmu_tx_t
*tx
)
761 dsl_dataset_t
*ds
= os
->os
->os_dsl_dataset
;
762 struct snaparg
*sn
= arg2
;
764 dsl_dataset_snapshot_sync(ds
, sn
->snapname
, cr
, tx
);
767 dsl_props_set_sync(ds
->ds_prev
, sn
->props
, cr
, tx
);
771 dmu_objset_snapshot_one(char *name
, void *arg
)
773 struct snaparg
*sn
= arg
;
777 (void) strcpy(sn
->failed
, name
);
780 * Check permissions only when requested. This only applies when
781 * doing a recursive snapshot. The permission checks for the starting
782 * dataset have already been performed in zfs_secpolicy_snapshot()
784 if (sn
->checkperms
== B_TRUE
&&
785 (err
= zfs_secpolicy_snapshot_perms(name
, CRED())))
788 err
= dmu_objset_open(name
, DMU_OST_ANY
, DS_MODE_USER
, &os
);
792 /* If the objset is in an inconsistent state, return busy */
793 if (os
->os
->os_dsl_dataset
->ds_phys
->ds_flags
& DS_FLAG_INCONSISTENT
) {
794 dmu_objset_close(os
);
799 * NB: we need to wait for all in-flight changes to get to disk,
800 * so that we snapshot those changes. zil_suspend does this as
803 err
= zil_suspend(dmu_objset_zil(os
));
805 dsl_sync_task_create(sn
->dstg
, snapshot_check
,
806 snapshot_sync
, os
, sn
, 3);
808 dmu_objset_close(os
);
815 dmu_objset_snapshot(char *fsname
, char *snapname
,
816 nvlist_t
*props
, boolean_t recursive
)
818 dsl_sync_task_t
*dst
;
823 (void) strcpy(sn
.failed
, fsname
);
825 err
= spa_open(fsname
, &spa
, FTAG
);
829 sn
.dstg
= dsl_sync_task_group_create(spa_get_dsl(spa
));
830 sn
.snapname
= snapname
;
834 sn
.checkperms
= B_TRUE
;
835 err
= dmu_objset_find(fsname
,
836 dmu_objset_snapshot_one
, &sn
, DS_FIND_CHILDREN
);
838 sn
.checkperms
= B_FALSE
;
839 err
= dmu_objset_snapshot_one(fsname
, &sn
);
843 err
= dsl_sync_task_group_wait(sn
.dstg
);
845 for (dst
= list_head(&sn
.dstg
->dstg_tasks
); dst
;
846 dst
= list_next(&sn
.dstg
->dstg_tasks
, dst
)) {
847 objset_t
*os
= dst
->dst_arg1
;
848 dsl_dataset_t
*ds
= os
->os
->os_dsl_dataset
;
850 dsl_dataset_name(ds
, sn
.failed
);
851 zil_resume(dmu_objset_zil(os
));
852 dmu_objset_close(os
);
856 (void) strcpy(fsname
, sn
.failed
);
857 dsl_sync_task_group_destroy(sn
.dstg
);
858 spa_close(spa
, FTAG
);
863 dmu_objset_sync_dnodes(list_t
*list
, list_t
*newlist
, dmu_tx_t
*tx
)
867 while (dn
= list_head(list
)) {
868 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
);
869 ASSERT(dn
->dn_dbuf
->db_data_pending
);
871 * Initialize dn_zio outside dnode_sync() because the
872 * meta-dnode needs to set it ouside dnode_sync().
874 dn
->dn_zio
= dn
->dn_dbuf
->db_data_pending
->dr_zio
;
877 ASSERT3U(dn
->dn_nlevels
, <=, DN_MAX_LEVELS
);
878 list_remove(list
, dn
);
881 (void) dnode_add_ref(dn
, newlist
);
882 list_insert_tail(newlist
, dn
);
891 ready(zio_t
*zio
, arc_buf_t
*abuf
, void *arg
)
893 blkptr_t
*bp
= zio
->io_bp
;
894 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
895 objset_impl_t
*os
= arg
;
896 dnode_phys_t
*dnp
= &os
->os_phys
->os_meta_dnode
;
898 ASSERT(bp
== os
->os_rootbp
);
899 ASSERT(BP_GET_TYPE(bp
) == DMU_OT_OBJSET
);
900 ASSERT(BP_GET_LEVEL(bp
) == 0);
903 * Update rootbp fill count: it should be the number of objects
904 * allocated in the object set (not counting the "special"
905 * objects that are stored in the objset_phys_t -- the meta
906 * dnode and user/group accounting objects).
909 for (int i
= 0; i
< dnp
->dn_nblkptr
; i
++)
910 bp
->blk_fill
+= dnp
->dn_blkptr
[i
].blk_fill
;
912 if (zio
->io_flags
& ZIO_FLAG_IO_REWRITE
) {
913 ASSERT(DVA_EQUAL(BP_IDENTITY(bp
), BP_IDENTITY(bp_orig
)));
915 if (zio
->io_bp_orig
.blk_birth
== os
->os_synctx
->tx_txg
)
916 (void) dsl_dataset_block_kill(os
->os_dsl_dataset
,
917 &zio
->io_bp_orig
, zio
, os
->os_synctx
);
918 dsl_dataset_block_born(os
->os_dsl_dataset
, bp
, os
->os_synctx
);
922 /* called from dsl */
924 dmu_objset_sync(objset_impl_t
*os
, zio_t
*pio
, dmu_tx_t
*tx
)
928 writeprops_t wp
= { 0 };
931 list_t
*newlist
= NULL
;
932 dbuf_dirty_record_t
*dr
;
934 dprintf_ds(os
->os_dsl_dataset
, "txg=%llu\n", tx
->tx_txg
);
936 ASSERT(dmu_tx_is_syncing(tx
));
937 /* XXX the write_done callback should really give us the tx... */
940 if (os
->os_dsl_dataset
== NULL
) {
942 * This is the MOS. If we have upgraded,
943 * spa_max_replication() could change, so reset
946 os
->os_copies
= spa_max_replication(os
->os_spa
);
950 * Create the root block IO
952 zb
.zb_objset
= os
->os_dsl_dataset
? os
->os_dsl_dataset
->ds_object
: 0;
954 zb
.zb_level
= -1; /* for block ordering; it's level 0 on disk */
957 wp
.wp_type
= DMU_OT_OBJSET
;
958 wp
.wp_level
= 0; /* on-disk BP level; see above */
959 wp
.wp_copies
= os
->os_copies
;
960 wp
.wp_oschecksum
= os
->os_checksum
;
961 wp
.wp_oscompress
= os
->os_compress
;
963 if (BP_IS_OLDER(os
->os_rootbp
, tx
->tx_txg
)) {
964 (void) dsl_dataset_block_kill(os
->os_dsl_dataset
,
965 os
->os_rootbp
, pio
, tx
);
968 arc_release(os
->os_phys_buf
, &os
->os_phys_buf
);
970 zio
= arc_write(pio
, os
->os_spa
, &wp
, DMU_OS_IS_L2CACHEABLE(os
),
971 tx
->tx_txg
, os
->os_rootbp
, os
->os_phys_buf
, ready
, NULL
, os
,
972 ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);
975 * Sync special dnodes - the parent IO for the sync is the root block
977 os
->os_meta_dnode
->dn_zio
= zio
;
978 dnode_sync(os
->os_meta_dnode
, tx
);
980 os
->os_phys
->os_flags
= os
->os_flags
;
982 if (os
->os_userused_dnode
&&
983 os
->os_userused_dnode
->dn_type
!= DMU_OT_NONE
) {
984 os
->os_userused_dnode
->dn_zio
= zio
;
985 dnode_sync(os
->os_userused_dnode
, tx
);
986 os
->os_groupused_dnode
->dn_zio
= zio
;
987 dnode_sync(os
->os_groupused_dnode
, tx
);
990 txgoff
= tx
->tx_txg
& TXG_MASK
;
992 if (dmu_objset_userused_enabled(os
)) {
993 newlist
= &os
->os_synced_dnodes
;
995 * We must create the list here because it uses the
996 * dn_dirty_link[] of this txg.
998 list_create(newlist
, sizeof (dnode_t
),
999 offsetof(dnode_t
, dn_dirty_link
[txgoff
]));
1002 dmu_objset_sync_dnodes(&os
->os_free_dnodes
[txgoff
], newlist
, tx
);
1003 dmu_objset_sync_dnodes(&os
->os_dirty_dnodes
[txgoff
], newlist
, tx
);
1005 list
= &os
->os_meta_dnode
->dn_dirty_records
[txgoff
];
1006 while (dr
= list_head(list
)) {
1007 ASSERT(dr
->dr_dbuf
->db_level
== 0);
1008 list_remove(list
, dr
);
1010 zio_nowait(dr
->dr_zio
);
1013 * Free intent log blocks up to this tx.
1015 zil_sync(os
->os_zil
, tx
);
1016 os
->os_phys
->os_zil_header
= os
->os_zil_header
;
1020 static objset_used_cb_t
*used_cbs
[DMU_OST_NUMTYPES
];
1023 dmu_objset_register_type(dmu_objset_type_t ost
, objset_used_cb_t
*cb
)
1029 dmu_objset_userused_enabled(objset_impl_t
*os
)
1031 return (spa_version(os
->os_spa
) >= SPA_VERSION_USERSPACE
&&
1032 used_cbs
[os
->os_phys
->os_type
] &&
1033 os
->os_userused_dnode
);
1037 dmu_objset_do_userquota_callbacks(objset_impl_t
*os
, dmu_tx_t
*tx
)
1040 list_t
*list
= &os
->os_synced_dnodes
;
1041 static const char zerobuf
[DN_MAX_BONUSLEN
] = {0};
1043 ASSERT(list_head(list
) == NULL
|| dmu_objset_userused_enabled(os
));
1045 while (dn
= list_head(list
)) {
1046 dmu_object_type_t bonustype
;
1048 ASSERT(!DMU_OBJECT_IS_SPECIAL(dn
->dn_object
));
1049 ASSERT(dn
->dn_oldphys
);
1050 ASSERT(dn
->dn_phys
->dn_type
== DMU_OT_NONE
||
1051 dn
->dn_phys
->dn_flags
&
1052 DNODE_FLAG_USERUSED_ACCOUNTED
);
1054 /* Allocate the user/groupused objects if necessary. */
1055 if (os
->os_userused_dnode
->dn_type
== DMU_OT_NONE
) {
1056 VERIFY(0 == zap_create_claim(&os
->os
,
1057 DMU_USERUSED_OBJECT
,
1058 DMU_OT_USERGROUP_USED
, DMU_OT_NONE
, 0, tx
));
1059 VERIFY(0 == zap_create_claim(&os
->os
,
1060 DMU_GROUPUSED_OBJECT
,
1061 DMU_OT_USERGROUP_USED
, DMU_OT_NONE
, 0, tx
));
1065 * If the object was not previously
1066 * accounted, pretend that it was free.
1068 if (!(dn
->dn_oldphys
->dn_flags
&
1069 DNODE_FLAG_USERUSED_ACCOUNTED
)) {
1070 bzero(dn
->dn_oldphys
, sizeof (dnode_phys_t
));
1074 * If the object was freed, use the previous bonustype.
1076 bonustype
= dn
->dn_phys
->dn_bonustype
?
1077 dn
->dn_phys
->dn_bonustype
: dn
->dn_oldphys
->dn_bonustype
;
1078 ASSERT(dn
->dn_phys
->dn_type
!= 0 ||
1079 (bcmp(DN_BONUS(dn
->dn_phys
), zerobuf
,
1080 DN_MAX_BONUSLEN
) == 0 &&
1081 DN_USED_BYTES(dn
->dn_phys
) == 0));
1082 ASSERT(dn
->dn_oldphys
->dn_type
!= 0 ||
1083 (bcmp(DN_BONUS(dn
->dn_oldphys
), zerobuf
,
1084 DN_MAX_BONUSLEN
) == 0 &&
1085 DN_USED_BYTES(dn
->dn_oldphys
) == 0));
1086 used_cbs
[os
->os_phys
->os_type
](&os
->os
, bonustype
,
1087 DN_BONUS(dn
->dn_oldphys
), DN_BONUS(dn
->dn_phys
),
1088 DN_USED_BYTES(dn
->dn_oldphys
),
1089 DN_USED_BYTES(dn
->dn_phys
), tx
);
1092 * The mutex is needed here for interlock with dnode_allocate.
1094 mutex_enter(&dn
->dn_mtx
);
1095 zio_buf_free(dn
->dn_oldphys
, sizeof (dnode_phys_t
));
1096 dn
->dn_oldphys
= NULL
;
1097 mutex_exit(&dn
->dn_mtx
);
1099 list_remove(list
, dn
);
1100 dnode_rele(dn
, list
);
1105 dmu_objset_userspace_present(objset_t
*os
)
1107 return (os
->os
->os_phys
->os_flags
&
1108 OBJSET_FLAG_USERACCOUNTING_COMPLETE
);
1112 dmu_objset_userspace_upgrade(objset_t
*os
)
1117 if (dmu_objset_userspace_present(os
))
1119 if (!dmu_objset_userused_enabled(os
->os
))
1121 if (dmu_objset_is_snapshot(os
))
1125 * We simply need to mark every object dirty, so that it will be
1126 * synced out and now accounted. If this is called
1127 * concurrently, or if we already did some work before crashing,
1128 * that's fine, since we track each object's accounted state
1132 for (obj
= 0; err
== 0; err
= dmu_object_next(os
, &obj
, FALSE
, 0)) {
1133 dmu_tx_t
*tx
= dmu_tx_create(os
);
1137 if (issig(JUSTLOOKING
) && issig(FORREAL
))
1140 objerr
= dmu_bonus_hold(os
, obj
, FTAG
, &db
);
1143 dmu_tx_hold_bonus(tx
, obj
);
1144 objerr
= dmu_tx_assign(tx
, TXG_WAIT
);
1149 dmu_buf_will_dirty(db
, tx
);
1150 dmu_buf_rele(db
, FTAG
);
1154 os
->os
->os_flags
|= OBJSET_FLAG_USERACCOUNTING_COMPLETE
;
1155 txg_wait_synced(dmu_objset_pool(os
), 0);
1160 dmu_objset_space(objset_t
*os
, uint64_t *refdbytesp
, uint64_t *availbytesp
,
1161 uint64_t *usedobjsp
, uint64_t *availobjsp
)
1163 dsl_dataset_space(os
->os
->os_dsl_dataset
, refdbytesp
, availbytesp
,
1164 usedobjsp
, availobjsp
);
1168 dmu_objset_fsid_guid(objset_t
*os
)
1170 return (dsl_dataset_fsid_guid(os
->os
->os_dsl_dataset
));
1174 dmu_objset_fast_stat(objset_t
*os
, dmu_objset_stats_t
*stat
)
1176 stat
->dds_type
= os
->os
->os_phys
->os_type
;
1177 if (os
->os
->os_dsl_dataset
)
1178 dsl_dataset_fast_stat(os
->os
->os_dsl_dataset
, stat
);
1182 dmu_objset_stats(objset_t
*os
, nvlist_t
*nv
)
1184 ASSERT(os
->os
->os_dsl_dataset
||
1185 os
->os
->os_phys
->os_type
== DMU_OST_META
);
1187 if (os
->os
->os_dsl_dataset
!= NULL
)
1188 dsl_dataset_stats(os
->os
->os_dsl_dataset
, nv
);
1190 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_TYPE
,
1191 os
->os
->os_phys
->os_type
);
1192 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_USERACCOUNTING
,
1193 dmu_objset_userspace_present(os
));
1197 dmu_objset_is_snapshot(objset_t
*os
)
1199 if (os
->os
->os_dsl_dataset
!= NULL
)
1200 return (dsl_dataset_is_snapshot(os
->os
->os_dsl_dataset
));
1206 dmu_snapshot_realname(objset_t
*os
, char *name
, char *real
, int maxlen
,
1207 boolean_t
*conflict
)
1209 dsl_dataset_t
*ds
= os
->os
->os_dsl_dataset
;
1212 if (ds
->ds_phys
->ds_snapnames_zapobj
== 0)
1215 return (zap_lookup_norm(ds
->ds_dir
->dd_pool
->dp_meta_objset
,
1216 ds
->ds_phys
->ds_snapnames_zapobj
, name
, 8, 1, &ignored
, MT_FIRST
,
1217 real
, maxlen
, conflict
));
1221 dmu_snapshot_list_next(objset_t
*os
, int namelen
, char *name
,
1222 uint64_t *idp
, uint64_t *offp
, boolean_t
*case_conflict
)
1224 dsl_dataset_t
*ds
= os
->os
->os_dsl_dataset
;
1225 zap_cursor_t cursor
;
1226 zap_attribute_t attr
;
1228 if (ds
->ds_phys
->ds_snapnames_zapobj
== 0)
1231 zap_cursor_init_serialized(&cursor
,
1232 ds
->ds_dir
->dd_pool
->dp_meta_objset
,
1233 ds
->ds_phys
->ds_snapnames_zapobj
, *offp
);
1235 if (zap_cursor_retrieve(&cursor
, &attr
) != 0) {
1236 zap_cursor_fini(&cursor
);
1240 if (strlen(attr
.za_name
) + 1 > namelen
) {
1241 zap_cursor_fini(&cursor
);
1242 return (ENAMETOOLONG
);
1245 (void) strcpy(name
, attr
.za_name
);
1247 *idp
= attr
.za_first_integer
;
1249 *case_conflict
= attr
.za_normalization_conflict
;
1250 zap_cursor_advance(&cursor
);
1251 *offp
= zap_cursor_serialize(&cursor
);
1252 zap_cursor_fini(&cursor
);
1258 dmu_dir_list_next(objset_t
*os
, int namelen
, char *name
,
1259 uint64_t *idp
, uint64_t *offp
)
1261 dsl_dir_t
*dd
= os
->os
->os_dsl_dataset
->ds_dir
;
1262 zap_cursor_t cursor
;
1263 zap_attribute_t attr
;
1265 /* there is no next dir on a snapshot! */
1266 if (os
->os
->os_dsl_dataset
->ds_object
!=
1267 dd
->dd_phys
->dd_head_dataset_obj
)
1270 zap_cursor_init_serialized(&cursor
,
1271 dd
->dd_pool
->dp_meta_objset
,
1272 dd
->dd_phys
->dd_child_dir_zapobj
, *offp
);
1274 if (zap_cursor_retrieve(&cursor
, &attr
) != 0) {
1275 zap_cursor_fini(&cursor
);
1279 if (strlen(attr
.za_name
) + 1 > namelen
) {
1280 zap_cursor_fini(&cursor
);
1281 return (ENAMETOOLONG
);
1284 (void) strcpy(name
, attr
.za_name
);
1286 *idp
= attr
.za_first_integer
;
1287 zap_cursor_advance(&cursor
);
1288 *offp
= zap_cursor_serialize(&cursor
);
1289 zap_cursor_fini(&cursor
);
1295 int (*func
)(char *, void *);
1301 findfunc(spa_t
*spa
, uint64_t dsobj
, const char *dsname
, void *arg
)
1303 struct findarg
*fa
= arg
;
1304 return (fa
->func((char *)dsname
, fa
->arg
));
1308 * Find all objsets under name, and for each, call 'func(child_name, arg)'.
1309 * Perhaps change all callers to use dmu_objset_find_spa()?
1312 dmu_objset_find(char *name
, int func(char *, void *), void *arg
, int flags
)
1317 return (dmu_objset_find_spa(NULL
, name
, findfunc
, &fa
, flags
));
1321 * Find all objsets under name, call func on each
1324 dmu_objset_find_spa(spa_t
*spa
, const char *name
,
1325 int func(spa_t
*, uint64_t, const char *, void *), void *arg
, int flags
)
1331 zap_attribute_t
*attr
;
1337 name
= spa_name(spa
);
1338 err
= dsl_dir_open_spa(spa
, name
, FTAG
, &dd
, NULL
);
1342 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1343 if (dd
->dd_myname
[0] == '$') {
1344 dsl_dir_close(dd
, FTAG
);
1348 thisobj
= dd
->dd_phys
->dd_head_dataset_obj
;
1349 attr
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
1353 * Iterate over all children.
1355 if (flags
& DS_FIND_CHILDREN
) {
1356 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
,
1357 dd
->dd_phys
->dd_child_dir_zapobj
);
1358 zap_cursor_retrieve(&zc
, attr
) == 0;
1359 (void) zap_cursor_advance(&zc
)) {
1360 ASSERT(attr
->za_integer_length
== sizeof (uint64_t));
1361 ASSERT(attr
->za_num_integers
== 1);
1363 child
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
1364 (void) strcpy(child
, name
);
1365 (void) strcat(child
, "/");
1366 (void) strcat(child
, attr
->za_name
);
1367 err
= dmu_objset_find_spa(spa
, child
, func
, arg
, flags
);
1368 kmem_free(child
, MAXPATHLEN
);
1372 zap_cursor_fini(&zc
);
1375 dsl_dir_close(dd
, FTAG
);
1376 kmem_free(attr
, sizeof (zap_attribute_t
));
1382 * Iterate over all snapshots.
1384 if (flags
& DS_FIND_SNAPSHOTS
) {
1385 if (!dsl_pool_sync_context(dp
))
1386 rw_enter(&dp
->dp_config_rwlock
, RW_READER
);
1387 err
= dsl_dataset_hold_obj(dp
, thisobj
, FTAG
, &ds
);
1388 if (!dsl_pool_sync_context(dp
))
1389 rw_exit(&dp
->dp_config_rwlock
);
1392 uint64_t snapobj
= ds
->ds_phys
->ds_snapnames_zapobj
;
1393 dsl_dataset_rele(ds
, FTAG
);
1395 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
, snapobj
);
1396 zap_cursor_retrieve(&zc
, attr
) == 0;
1397 (void) zap_cursor_advance(&zc
)) {
1398 ASSERT(attr
->za_integer_length
==
1400 ASSERT(attr
->za_num_integers
== 1);
1402 child
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
1403 (void) strcpy(child
, name
);
1404 (void) strcat(child
, "@");
1405 (void) strcat(child
, attr
->za_name
);
1406 err
= func(spa
, attr
->za_first_integer
,
1408 kmem_free(child
, MAXPATHLEN
);
1412 zap_cursor_fini(&zc
);
1416 dsl_dir_close(dd
, FTAG
);
1417 kmem_free(attr
, sizeof (zap_attribute_t
));
1423 * Apply to self if appropriate.
1425 err
= func(spa
, thisobj
, name
, arg
);
1431 dmu_objset_prefetch(char *name
, void *arg
)
1435 if (dsl_dataset_hold(name
, FTAG
, &ds
))
1438 if (!BP_IS_HOLE(&ds
->ds_phys
->ds_bp
)) {
1439 mutex_enter(&ds
->ds_opening_lock
);
1440 if (!dsl_dataset_get_user_ptr(ds
)) {
1441 uint32_t aflags
= ARC_NOWAIT
| ARC_PREFETCH
;
1444 zb
.zb_objset
= ds
->ds_object
;
1449 (void) arc_read_nolock(NULL
, dsl_dataset_get_spa(ds
),
1450 &ds
->ds_phys
->ds_bp
, NULL
, NULL
,
1451 ZIO_PRIORITY_ASYNC_READ
,
1452 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
1455 mutex_exit(&ds
->ds_opening_lock
);
1458 dsl_dataset_rele(ds
, FTAG
);
1463 dmu_objset_set_user(objset_t
*os
, void *user_ptr
)
1465 ASSERT(MUTEX_HELD(&os
->os
->os_user_ptr_lock
));
1466 os
->os
->os_user_ptr
= user_ptr
;
1470 dmu_objset_get_user(objset_t
*os
)
1472 ASSERT(MUTEX_HELD(&os
->os
->os_user_ptr_lock
));
1473 return (os
->os
->os_user_ptr
);