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 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "@(#)dmu_objset.c 1.37 08/04/27 SMI"
29 #include <sys/zfs_context.h>
30 #include <sys/dmu_objset.h>
31 #include <sys/dsl_dir.h>
32 #include <sys/dsl_dataset.h>
33 #include <sys/dsl_prop.h>
34 #include <sys/dsl_pool.h>
35 #include <sys/dsl_synctask.h>
36 #include <sys/dsl_deleg.h>
37 #include <sys/dnode.h>
40 #include <sys/dmu_tx.h>
41 #include <sys/zio_checksum.h>
44 #include <sys/dmu_impl.h>
45 #include <sys/zfs_ioctl.h>
48 dmu_objset_spa(objset_t *os)
50 return (os->os->os_spa);
54 dmu_objset_zil(objset_t *os)
56 return (os->os->os_zil);
60 dmu_objset_pool(objset_t *os)
64 if ((ds = os->os->os_dsl_dataset) != NULL && ds->ds_dir)
65 return (ds->ds_dir->dd_pool);
67 return (spa_get_dsl(os->os->os_spa));
71 dmu_objset_ds(objset_t *os)
73 return (os->os->os_dsl_dataset);
77 dmu_objset_type(objset_t *os)
79 return (os->os->os_phys->os_type);
83 dmu_objset_name(objset_t *os, char *buf)
85 dsl_dataset_name(os->os->os_dsl_dataset, buf);
89 dmu_objset_id(objset_t *os)
91 dsl_dataset_t *ds = os->os->os_dsl_dataset;
93 return (ds ? ds->ds_object : 0);
97 checksum_changed_cb(void *arg, uint64_t newval)
99 objset_impl_t *osi = arg;
102 * Inheritance should have been done by now.
104 ASSERT(newval != ZIO_CHECKSUM_INHERIT);
106 osi->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
110 compression_changed_cb(void *arg, uint64_t newval)
112 objset_impl_t *osi = arg;
115 * Inheritance and range checking should have been done by now.
117 ASSERT(newval != ZIO_COMPRESS_INHERIT);
119 osi->os_compress = zio_compress_select(newval, ZIO_COMPRESS_ON_VALUE);
123 copies_changed_cb(void *arg, uint64_t newval)
125 objset_impl_t *osi = arg;
128 * Inheritance and range checking should have been done by now.
131 ASSERT(newval <= spa_max_replication(osi->os_spa));
133 osi->os_copies = newval;
137 dmu_objset_byteswap(void *buf, size_t size)
139 objset_phys_t *osp = buf;
141 ASSERT(size == sizeof (objset_phys_t));
142 dnode_byteswap(&osp->os_meta_dnode);
143 byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
144 osp->os_type = BSWAP_64(osp->os_type);
148 dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
149 objset_impl_t **osip)
152 int i, err, checksum;
154 ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
156 osi = kmem_zalloc(sizeof (objset_impl_t), KM_SLEEP);
158 osi->os_dsl_dataset = ds;
161 if (!BP_IS_HOLE(osi->os_rootbp)) {
162 uint32_t aflags = ARC_WAIT;
164 zb.zb_objset = ds ? ds->ds_object : 0;
169 dprintf_bp(osi->os_rootbp, "reading %s", "");
170 err = arc_read(NULL, spa, osi->os_rootbp,
171 dmu_ot[DMU_OT_OBJSET].ot_byteswap,
172 arc_getbuf_func, &osi->os_phys_buf,
173 ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
175 kmem_free(osi, sizeof (objset_impl_t));
178 osi->os_phys = osi->os_phys_buf->b_data;
179 if (ds == NULL || dsl_dataset_is_snapshot(ds) == 0)
180 arc_release(osi->os_phys_buf, &osi->os_phys_buf);
182 osi->os_phys_buf = arc_buf_alloc(spa, sizeof (objset_phys_t),
183 &osi->os_phys_buf, ARC_BUFC_METADATA);
184 osi->os_phys = osi->os_phys_buf->b_data;
185 bzero(osi->os_phys, sizeof (objset_phys_t));
189 * Note: the changed_cb will be called once before the register
190 * func returns, thus changing the checksum/compression from the
191 * default (fletcher2/off). Snapshots don't need to know, and
192 * registering would complicate clone promotion.
194 if (ds && ds->ds_phys->ds_num_children == 0) {
195 err = dsl_prop_register(ds, "checksum",
196 checksum_changed_cb, osi);
198 err = dsl_prop_register(ds, "compression",
199 compression_changed_cb, osi);
201 err = dsl_prop_register(ds, "copies",
202 copies_changed_cb, osi);
204 VERIFY(arc_buf_remove_ref(osi->os_phys_buf,
205 &osi->os_phys_buf) == 1);
206 kmem_free(osi, sizeof (objset_impl_t));
209 } else if (ds == NULL) {
210 /* It's the meta-objset. */
211 osi->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
212 osi->os_compress = ZIO_COMPRESS_LZJB;
213 osi->os_copies = spa_max_replication(spa);
216 osi->os_zil = zil_alloc(&osi->os, &osi->os_phys->os_zil_header);
219 * Metadata always gets compressed and checksummed.
220 * If the data checksum is multi-bit correctable, and it's not
221 * a ZBT-style checksum, then it's suitable for metadata as well.
222 * Otherwise, the metadata checksum defaults to fletcher4.
224 checksum = osi->os_checksum;
226 if (zio_checksum_table[checksum].ci_correctable &&
227 !zio_checksum_table[checksum].ci_zbt)
228 osi->os_md_checksum = checksum;
230 osi->os_md_checksum = ZIO_CHECKSUM_FLETCHER_4;
231 osi->os_md_compress = ZIO_COMPRESS_LZJB;
233 for (i = 0; i < TXG_SIZE; i++) {
234 list_create(&osi->os_dirty_dnodes[i], sizeof (dnode_t),
235 offsetof(dnode_t, dn_dirty_link[i]));
236 list_create(&osi->os_free_dnodes[i], sizeof (dnode_t),
237 offsetof(dnode_t, dn_dirty_link[i]));
239 list_create(&osi->os_dnodes, sizeof (dnode_t),
240 offsetof(dnode_t, dn_link));
241 list_create(&osi->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
242 offsetof(dmu_buf_impl_t, db_link));
244 mutex_init(&osi->os_lock, NULL, MUTEX_DEFAULT, NULL);
245 mutex_init(&osi->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
246 mutex_init(&osi->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
248 osi->os_meta_dnode = dnode_special_open(osi,
249 &osi->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT);
252 * We should be the only thread trying to do this because we
253 * have ds_opening_lock
256 VERIFY(NULL == dsl_dataset_set_user_ptr(ds, osi,
265 dmu_objset_open_ds_os(dsl_dataset_t *ds, objset_t *os, dmu_objset_type_t type)
270 mutex_enter(&ds->ds_opening_lock);
271 osi = dsl_dataset_get_user_ptr(ds);
273 err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
274 ds, &ds->ds_phys->ds_bp, &osi);
278 mutex_exit(&ds->ds_opening_lock);
281 os->os_mode = DS_MODE_NONE;
283 if (type != DMU_OST_ANY && type != os->os->os_phys->os_type)
289 dmu_objset_open_ds(dsl_dataset_t *ds, dmu_objset_type_t type, objset_t **osp)
294 os = kmem_alloc(sizeof (objset_t), KM_SLEEP);
295 err = dmu_objset_open_ds_os(ds, os, type);
297 kmem_free(os, sizeof (objset_t));
303 /* called from zpl */
305 dmu_objset_open(const char *name, dmu_objset_type_t type, int mode,
312 ASSERT(mode != DS_MODE_NONE);
314 os = kmem_alloc(sizeof (objset_t), KM_SLEEP);
315 err = dsl_dataset_open(name, mode, os, &ds);
317 kmem_free(os, sizeof (objset_t));
321 err = dmu_objset_open_ds_os(ds, os, type);
324 kmem_free(os, sizeof (objset_t));
325 dsl_dataset_close(ds, mode, os);
333 dmu_objset_close(objset_t *os)
335 if (os->os_mode != DS_MODE_NONE)
336 dsl_dataset_close(os->os->os_dsl_dataset, os->os_mode, os);
337 kmem_free(os, sizeof (objset_t));
341 dmu_objset_evict_dbufs(objset_t *os)
343 objset_impl_t *osi = os->os;
346 mutex_enter(&osi->os_lock);
348 /* process the mdn last, since the other dnodes have holds on it */
349 list_remove(&osi->os_dnodes, osi->os_meta_dnode);
350 list_insert_tail(&osi->os_dnodes, osi->os_meta_dnode);
353 * Find the first dnode with holds. We have to do this dance
354 * because dnode_add_ref() only works if you already have a
355 * hold. If there are no holds then it has no dbufs so OK to
358 for (dn = list_head(&osi->os_dnodes);
359 dn && !dnode_add_ref(dn, FTAG);
360 dn = list_next(&osi->os_dnodes, dn))
364 dnode_t *next_dn = dn;
367 next_dn = list_next(&osi->os_dnodes, next_dn);
368 } while (next_dn && !dnode_add_ref(next_dn, FTAG));
370 mutex_exit(&osi->os_lock);
371 dnode_evict_dbufs(dn);
372 dnode_rele(dn, FTAG);
373 mutex_enter(&osi->os_lock);
376 mutex_exit(&osi->os_lock);
377 return (list_head(&osi->os_dnodes) != osi->os_meta_dnode);
381 dmu_objset_evict(dsl_dataset_t *ds, void *arg)
383 objset_impl_t *osi = arg;
387 for (i = 0; i < TXG_SIZE; i++) {
388 ASSERT(list_head(&osi->os_dirty_dnodes[i]) == NULL);
389 ASSERT(list_head(&osi->os_free_dnodes[i]) == NULL);
392 if (ds && ds->ds_phys->ds_num_children == 0) {
393 VERIFY(0 == dsl_prop_unregister(ds, "checksum",
394 checksum_changed_cb, osi));
395 VERIFY(0 == dsl_prop_unregister(ds, "compression",
396 compression_changed_cb, osi));
397 VERIFY(0 == dsl_prop_unregister(ds, "copies",
398 copies_changed_cb, osi));
402 * We should need only a single pass over the dnode list, since
403 * nothing can be added to the list at this point.
406 (void) dmu_objset_evict_dbufs(&os);
408 ASSERT3P(list_head(&osi->os_dnodes), ==, osi->os_meta_dnode);
409 ASSERT3P(list_tail(&osi->os_dnodes), ==, osi->os_meta_dnode);
410 ASSERT3P(list_head(&osi->os_meta_dnode->dn_dbufs), ==, NULL);
412 dnode_special_close(osi->os_meta_dnode);
413 zil_free(osi->os_zil);
415 VERIFY(arc_buf_remove_ref(osi->os_phys_buf, &osi->os_phys_buf) == 1);
416 mutex_destroy(&osi->os_lock);
417 mutex_destroy(&osi->os_obj_lock);
418 mutex_destroy(&osi->os_user_ptr_lock);
419 kmem_free(osi, sizeof (objset_impl_t));
422 /* called from dsl for meta-objset */
424 dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
425 dmu_objset_type_t type, dmu_tx_t *tx)
430 ASSERT(dmu_tx_is_syncing(tx));
432 mutex_enter(&ds->ds_opening_lock);
433 VERIFY(0 == dmu_objset_open_impl(spa, ds, bp, &osi));
435 mutex_exit(&ds->ds_opening_lock);
436 mdn = osi->os_meta_dnode;
438 dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
439 DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
442 * We don't want to have to increase the meta-dnode's nlevels
443 * later, because then we could do it in quescing context while
444 * we are also accessing it in open context.
446 * This precaution is not necessary for the MOS (ds == NULL),
447 * because the MOS is only updated in syncing context.
448 * This is most fortunate: the MOS is the only objset that
449 * needs to be synced multiple times as spa_sync() iterates
450 * to convergence, so minimizing its dn_nlevels matters.
456 * Determine the number of levels necessary for the meta-dnode
457 * to contain DN_MAX_OBJECT dnodes.
459 while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
460 (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
461 DN_MAX_OBJECT * sizeof (dnode_phys_t))
464 mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
465 mdn->dn_nlevels = levels;
468 ASSERT(type != DMU_OST_NONE);
469 ASSERT(type != DMU_OST_ANY);
470 ASSERT(type < DMU_OST_NUMTYPES);
471 osi->os_phys->os_type = type;
473 dsl_dataset_dirty(ds, tx);
479 void (*userfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
481 dsl_dataset_t *clone_parent;
482 const char *lastname;
483 dmu_objset_type_t type;
489 dmu_objset_create_check(void *arg1, void *arg2, dmu_tx_t *tx)
491 dsl_dir_t *dd = arg1;
492 struct oscarg *oa = arg2;
493 objset_t *mos = dd->dd_pool->dp_meta_objset;
497 err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj,
498 oa->lastname, sizeof (uint64_t), 1, &ddobj);
500 return (err ? err : EEXIST);
502 if (oa->clone_parent != NULL) {
504 * You can't clone across pools.
506 if (oa->clone_parent->ds_dir->dd_pool != dd->dd_pool)
510 * You can only clone snapshots, not the head datasets.
512 if (oa->clone_parent->ds_phys->ds_num_children == 0)
520 dmu_objset_create_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
522 dsl_dir_t *dd = arg1;
523 struct oscarg *oa = arg2;
528 ASSERT(dmu_tx_is_syncing(tx));
530 dsobj = dsl_dataset_create_sync(dd, oa->lastname,
531 oa->clone_parent, oa->flags, cr, tx);
533 VERIFY(0 == dsl_dataset_open_obj(dd->dd_pool, dsobj, NULL,
534 DS_MODE_STANDARD | DS_MODE_READONLY, FTAG, &ds));
535 bp = dsl_dataset_get_blkptr(ds);
536 if (BP_IS_HOLE(bp)) {
539 /* This is an empty dmu_objset; not a clone. */
540 osi = dmu_objset_create_impl(dsl_dataset_get_spa(ds),
541 ds, bp, oa->type, tx);
544 oa->userfunc(&osi->os, oa->userarg, cr, tx);
547 spa_history_internal_log(LOG_DS_CREATE, dd->dd_pool->dp_spa,
548 tx, cr, "dataset = %llu", dsobj);
550 dsl_dataset_close(ds, DS_MODE_STANDARD | DS_MODE_READONLY, FTAG);
554 dmu_objset_create(const char *name, dmu_objset_type_t type,
555 objset_t *clone_parent, uint64_t flags,
556 void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
561 struct oscarg oa = { 0 };
563 ASSERT(strchr(name, '@') == NULL);
564 err = dsl_dir_open(name, FTAG, &pdd, &tail);
568 dsl_dir_close(pdd, FTAG);
572 dprintf("name=%s\n", name);
580 if (clone_parent != NULL) {
582 * You can't clone to a different type.
584 if (clone_parent->os->os_phys->os_type != type) {
585 dsl_dir_close(pdd, FTAG);
588 oa.clone_parent = clone_parent->os->os_dsl_dataset;
590 err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
591 dmu_objset_create_sync, pdd, &oa, 5);
592 dsl_dir_close(pdd, FTAG);
597 dmu_objset_destroy(const char *name)
603 * If it looks like we'll be able to destroy it, and there's
604 * an unplayed replay log sitting around, destroy the log.
605 * It would be nicer to do this in dsl_dataset_destroy_sync(),
606 * but the replay log objset is modified in open context.
608 error = dmu_objset_open(name, DMU_OST_ANY,
609 DS_MODE_EXCLUSIVE|DS_MODE_READONLY, &os);
611 dsl_dataset_t *ds = os->os->os_dsl_dataset;
612 zil_destroy(dmu_objset_zil(os), B_FALSE);
615 * dsl_dataset_destroy() closes the ds.
616 * os is just used as the tag after it's freed.
618 kmem_free(os, sizeof (objset_t));
619 error = dsl_dataset_destroy(ds, os);
626 * This will close the objset.
629 dmu_objset_rollback(objset_t *os)
634 ds = os->os->os_dsl_dataset;
636 if (!dsl_dataset_tryupgrade(ds, DS_MODE_STANDARD, DS_MODE_EXCLUSIVE)) {
637 dmu_objset_close(os);
641 err = dsl_dataset_rollback(ds, os->os->os_phys->os_type);
644 * NB: we close the objset manually because the rollback
645 * actually implicitly called dmu_objset_evict(), thus freeing
648 dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, os);
649 kmem_free(os, sizeof (objset_t));
654 dsl_sync_task_group_t *dstg;
656 char failed[MAXPATHLEN];
657 boolean_t checkperms;
667 dmu_objset_snapshot_one(char *name, void *arg)
669 struct snaparg *sn = arg;
671 dmu_objset_stats_t stat;
674 (void) strcpy(sn->failed, name);
677 * Check permissions only when requested. This only applies when
678 * doing a recursive snapshot. The permission checks for the starting
679 * dataset have already been performed in zfs_secpolicy_snapshot()
681 if (sn->checkperms == B_TRUE &&
682 (err = zfs_secpolicy_snapshot_perms(name, CRED())))
685 err = dmu_objset_open(name, DMU_OST_ANY, DS_MODE_STANDARD, &os);
690 * If the objset is in an inconsistent state, return busy.
692 dmu_objset_fast_stat(os, &stat);
693 if (stat.dds_inconsistent) {
694 dmu_objset_close(os);
699 * NB: we need to wait for all in-flight changes to get to disk,
700 * so that we snapshot those changes. zil_suspend does this as
703 err = zil_suspend(dmu_objset_zil(os));
706 dsl_sync_task_create(sn->dstg, dsl_dataset_snapshot_check,
707 dsl_dataset_snapshot_sync, os->os->os_dsl_dataset,
709 osn = kmem_alloc(sizeof (struct osnode), KM_SLEEP);
711 list_insert_tail(&sn->objsets, osn);
713 dmu_objset_close(os);
720 dmu_objset_snapshot(char *fsname, char *snapname, boolean_t recursive)
722 dsl_sync_task_t *dst;
724 struct snaparg sn = { 0 };
728 (void) strcpy(sn.failed, fsname);
730 err = spa_open(fsname, &spa, FTAG);
734 sn.dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
735 sn.snapname = snapname;
736 list_create(&sn.objsets, sizeof (struct osnode),
737 offsetof(struct osnode, node));
740 sn.checkperms = B_TRUE;
741 err = dmu_objset_find(fsname,
742 dmu_objset_snapshot_one, &sn, DS_FIND_CHILDREN);
744 sn.checkperms = B_FALSE;
745 err = dmu_objset_snapshot_one(fsname, &sn);
751 err = dsl_sync_task_group_wait(sn.dstg);
753 for (dst = list_head(&sn.dstg->dstg_tasks); dst;
754 dst = list_next(&sn.dstg->dstg_tasks, dst)) {
755 dsl_dataset_t *ds = dst->dst_arg1;
757 dsl_dataset_name(ds, sn.failed);
761 while (osn = list_head(&sn.objsets)) {
762 list_remove(&sn.objsets, osn);
763 zil_resume(dmu_objset_zil(osn->os));
764 dmu_objset_close(osn->os);
765 kmem_free(osn, sizeof (struct osnode));
767 list_destroy(&sn.objsets);
770 (void) strcpy(fsname, sn.failed);
771 dsl_sync_task_group_destroy(sn.dstg);
772 spa_close(spa, FTAG);
777 dmu_objset_sync_dnodes(list_t *list, dmu_tx_t *tx)
781 while (dn = list_head(list)) {
782 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
783 ASSERT(dn->dn_dbuf->db_data_pending);
785 * Initialize dn_zio outside dnode_sync()
786 * to accomodate meta-dnode
788 dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
791 ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
792 list_remove(list, dn);
799 ready(zio_t *zio, arc_buf_t *abuf, void *arg)
801 objset_impl_t *os = arg;
802 blkptr_t *bp = os->os_rootbp;
803 dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
806 ASSERT(bp == zio->io_bp);
809 * Update rootbp fill count.
811 bp->blk_fill = 1; /* count the meta-dnode */
812 for (i = 0; i < dnp->dn_nblkptr; i++)
813 bp->blk_fill += dnp->dn_blkptr[i].blk_fill;
815 BP_SET_TYPE(bp, DMU_OT_OBJSET);
818 /* We must do this after we've set the bp's type and level */
819 if (!DVA_EQUAL(BP_IDENTITY(bp),
820 BP_IDENTITY(&zio->io_bp_orig))) {
821 if (zio->io_bp_orig.blk_birth == os->os_synctx->tx_txg)
822 dsl_dataset_block_kill(os->os_dsl_dataset,
823 &zio->io_bp_orig, NULL, os->os_synctx);
824 dsl_dataset_block_born(os->os_dsl_dataset, bp, os->os_synctx);
830 killer(zio_t *zio, arc_buf_t *abuf, void *arg)
832 objset_impl_t *os = arg;
834 ASSERT3U(zio->io_error, ==, 0);
835 arc_release(os->os_phys_buf, &os->os_phys_buf);
838 /* called from dsl */
840 dmu_objset_sync(objset_impl_t *os, zio_t *pio, dmu_tx_t *tx)
846 dbuf_dirty_record_t *dr;
848 dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
850 ASSERT(dmu_tx_is_syncing(tx));
851 /* XXX the write_done callback should really give us the tx... */
854 if (os->os_dsl_dataset == NULL) {
856 * This is the MOS. If we have upgraded,
857 * spa_max_replication() could change, so reset
860 os->os_copies = spa_max_replication(os->os_spa);
864 * Create the root block IO
866 zb.zb_objset = os->os_dsl_dataset ? os->os_dsl_dataset->ds_object : 0;
870 if (BP_IS_OLDER(os->os_rootbp, tx->tx_txg)) {
871 dsl_dataset_block_kill(os->os_dsl_dataset,
872 os->os_rootbp, pio, tx);
874 zio = arc_write(pio, os->os_spa, os->os_md_checksum,
876 dmu_get_replication_level(os, &zb, DMU_OT_OBJSET),
877 tx->tx_txg, os->os_rootbp, os->os_phys_buf, ready, killer, os,
878 ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED | ZIO_FLAG_METADATA,
882 * Sync meta-dnode - the parent IO for the sync is the root block
884 os->os_meta_dnode->dn_zio = zio;
885 dnode_sync(os->os_meta_dnode, tx);
887 txgoff = tx->tx_txg & TXG_MASK;
889 dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], tx);
890 dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], tx);
892 list = &os->os_meta_dnode->dn_dirty_records[txgoff];
893 while (dr = list_head(list)) {
894 ASSERT(dr->dr_dbuf->db_level == 0);
895 list_remove(list, dr);
897 zio_nowait(dr->dr_zio);
900 * Free intent log blocks up to this tx.
902 zil_sync(os->os_zil, tx);
907 dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
908 uint64_t *usedobjsp, uint64_t *availobjsp)
910 dsl_dataset_space(os->os->os_dsl_dataset, refdbytesp, availbytesp,
911 usedobjsp, availobjsp);
915 dmu_objset_fsid_guid(objset_t *os)
917 return (dsl_dataset_fsid_guid(os->os->os_dsl_dataset));
921 dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
923 stat->dds_type = os->os->os_phys->os_type;
924 if (os->os->os_dsl_dataset)
925 dsl_dataset_fast_stat(os->os->os_dsl_dataset, stat);
929 dmu_objset_stats(objset_t *os, nvlist_t *nv)
931 ASSERT(os->os->os_dsl_dataset ||
932 os->os->os_phys->os_type == DMU_OST_META);
934 if (os->os->os_dsl_dataset != NULL)
935 dsl_dataset_stats(os->os->os_dsl_dataset, nv);
937 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
938 os->os->os_phys->os_type);
942 dmu_objset_is_snapshot(objset_t *os)
944 if (os->os->os_dsl_dataset != NULL)
945 return (dsl_dataset_is_snapshot(os->os->os_dsl_dataset));
951 dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
954 dsl_dataset_t *ds = os->os->os_dsl_dataset;
957 if (ds->ds_phys->ds_snapnames_zapobj == 0)
960 return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
961 ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST,
962 real, maxlen, conflict));
966 dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
967 uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
969 dsl_dataset_t *ds = os->os->os_dsl_dataset;
971 zap_attribute_t attr;
973 if (ds->ds_phys->ds_snapnames_zapobj == 0)
976 zap_cursor_init_serialized(&cursor,
977 ds->ds_dir->dd_pool->dp_meta_objset,
978 ds->ds_phys->ds_snapnames_zapobj, *offp);
980 if (zap_cursor_retrieve(&cursor, &attr) != 0) {
981 zap_cursor_fini(&cursor);
985 if (strlen(attr.za_name) + 1 > namelen) {
986 zap_cursor_fini(&cursor);
987 return (ENAMETOOLONG);
990 (void) strcpy(name, attr.za_name);
992 *idp = attr.za_first_integer;
994 *case_conflict = attr.za_normalization_conflict;
995 zap_cursor_advance(&cursor);
996 *offp = zap_cursor_serialize(&cursor);
997 zap_cursor_fini(&cursor);
1003 dmu_dir_list_next(objset_t *os, int namelen, char *name,
1004 uint64_t *idp, uint64_t *offp)
1006 dsl_dir_t *dd = os->os->os_dsl_dataset->ds_dir;
1007 zap_cursor_t cursor;
1008 zap_attribute_t attr;
1010 /* there is no next dir on a snapshot! */
1011 if (os->os->os_dsl_dataset->ds_object !=
1012 dd->dd_phys->dd_head_dataset_obj)
1015 zap_cursor_init_serialized(&cursor,
1016 dd->dd_pool->dp_meta_objset,
1017 dd->dd_phys->dd_child_dir_zapobj, *offp);
1019 if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1020 zap_cursor_fini(&cursor);
1024 if (strlen(attr.za_name) + 1 > namelen) {
1025 zap_cursor_fini(&cursor);
1026 return (ENAMETOOLONG);
1029 (void) strcpy(name, attr.za_name);
1031 *idp = attr.za_first_integer;
1032 zap_cursor_advance(&cursor);
1033 *offp = zap_cursor_serialize(&cursor);
1034 zap_cursor_fini(&cursor);
1040 * Find all objsets under name, and for each, call 'func(child_name, arg)'.
1043 dmu_objset_find(char *name, int func(char *, void *), void *arg, int flags)
1049 zap_attribute_t *attr;
1053 err = dsl_dir_open(name, FTAG, &dd, NULL);
1057 /* NB: the $MOS dir doesn't have a head dataset */
1058 do_self = (dd->dd_phys->dd_head_dataset_obj != 0);
1059 attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1062 * Iterate over all children.
1064 if (flags & DS_FIND_CHILDREN) {
1065 for (zap_cursor_init(&zc, dd->dd_pool->dp_meta_objset,
1066 dd->dd_phys->dd_child_dir_zapobj);
1067 zap_cursor_retrieve(&zc, attr) == 0;
1068 (void) zap_cursor_advance(&zc)) {
1069 ASSERT(attr->za_integer_length == sizeof (uint64_t));
1070 ASSERT(attr->za_num_integers == 1);
1073 * No separating '/' because parent's name ends in /.
1075 child = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1076 /* XXX could probably just use name here */
1077 dsl_dir_name(dd, child);
1078 (void) strcat(child, "/");
1079 (void) strcat(child, attr->za_name);
1080 err = dmu_objset_find(child, func, arg, flags);
1081 kmem_free(child, MAXPATHLEN);
1085 zap_cursor_fini(&zc);
1088 dsl_dir_close(dd, FTAG);
1089 kmem_free(attr, sizeof (zap_attribute_t));
1095 * Iterate over all snapshots.
1097 if ((flags & DS_FIND_SNAPSHOTS) &&
1098 dmu_objset_open(name, DMU_OST_ANY,
1099 DS_MODE_STANDARD | DS_MODE_READONLY, &os) == 0) {
1101 snapobj = os->os->os_dsl_dataset->ds_phys->ds_snapnames_zapobj;
1102 dmu_objset_close(os);
1104 for (zap_cursor_init(&zc, dd->dd_pool->dp_meta_objset, snapobj);
1105 zap_cursor_retrieve(&zc, attr) == 0;
1106 (void) zap_cursor_advance(&zc)) {
1107 ASSERT(attr->za_integer_length == sizeof (uint64_t));
1108 ASSERT(attr->za_num_integers == 1);
1110 child = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1111 /* XXX could probably just use name here */
1112 dsl_dir_name(dd, child);
1113 (void) strcat(child, "@");
1114 (void) strcat(child, attr->za_name);
1115 err = func(child, arg);
1116 kmem_free(child, MAXPATHLEN);
1120 zap_cursor_fini(&zc);
1123 dsl_dir_close(dd, FTAG);
1124 kmem_free(attr, sizeof (zap_attribute_t));
1130 * Apply to self if appropriate.
1133 err = func(name, arg);
1138 dmu_objset_set_user(objset_t *os, void *user_ptr)
1140 ASSERT(MUTEX_HELD(&os->os->os_user_ptr_lock));
1141 os->os->os_user_ptr = user_ptr;
1145 dmu_objset_get_user(objset_t *os)
1147 ASSERT(MUTEX_HELD(&os->os->os_user_ptr_lock));
1148 return (os->os->os_user_ptr);