1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51544Seschrock * Common Development and Distribution License (the "License"). 61544Seschrock * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 21789Sahrens /* 221544Seschrock * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 27789Sahrens 28789Sahrens #include <sys/zfs_context.h> 29789Sahrens #include <sys/dmu_objset.h> 30789Sahrens #include <sys/dsl_dir.h> 31789Sahrens #include <sys/dsl_dataset.h> 32789Sahrens #include <sys/dsl_prop.h> 33789Sahrens #include <sys/dsl_pool.h> 342199Sahrens #include <sys/dsl_synctask.h> 35789Sahrens #include <sys/dnode.h> 36789Sahrens #include <sys/dbuf.h> 37789Sahrens #include <sys/dmu_tx.h> 38789Sahrens #include <sys/zio_checksum.h> 39789Sahrens #include <sys/zap.h> 40789Sahrens #include <sys/zil.h> 41789Sahrens #include <sys/dmu_impl.h> 42789Sahrens 43789Sahrens 44789Sahrens spa_t * 45789Sahrens dmu_objset_spa(objset_t *os) 46789Sahrens { 47789Sahrens return (os->os->os_spa); 48789Sahrens } 49789Sahrens 50789Sahrens zilog_t * 51789Sahrens dmu_objset_zil(objset_t *os) 52789Sahrens { 53789Sahrens return (os->os->os_zil); 54789Sahrens } 55789Sahrens 56789Sahrens dsl_pool_t * 57789Sahrens dmu_objset_pool(objset_t *os) 58789Sahrens { 59789Sahrens dsl_dataset_t *ds; 60789Sahrens 61789Sahrens if ((ds = os->os->os_dsl_dataset) != NULL && ds->ds_dir) 62789Sahrens return (ds->ds_dir->dd_pool); 63789Sahrens else 64789Sahrens return (spa_get_dsl(os->os->os_spa)); 65789Sahrens } 66789Sahrens 67789Sahrens dsl_dataset_t * 68789Sahrens dmu_objset_ds(objset_t *os) 69789Sahrens { 70789Sahrens return (os->os->os_dsl_dataset); 71789Sahrens } 72789Sahrens 73789Sahrens dmu_objset_type_t 74789Sahrens dmu_objset_type(objset_t *os) 75789Sahrens { 76789Sahrens return (os->os->os_phys->os_type); 77789Sahrens } 78789Sahrens 79789Sahrens void 80789Sahrens dmu_objset_name(objset_t *os, char *buf) 81789Sahrens { 82789Sahrens dsl_dataset_name(os->os->os_dsl_dataset, buf); 83789Sahrens } 84789Sahrens 85789Sahrens uint64_t 86789Sahrens dmu_objset_id(objset_t *os) 87789Sahrens { 88789Sahrens dsl_dataset_t *ds = os->os->os_dsl_dataset; 89789Sahrens 90789Sahrens return (ds ? ds->ds_object : 0); 91789Sahrens } 92789Sahrens 93789Sahrens static void 94789Sahrens checksum_changed_cb(void *arg, uint64_t newval) 95789Sahrens { 96789Sahrens objset_impl_t *osi = arg; 97789Sahrens 98789Sahrens /* 99789Sahrens * Inheritance should have been done by now. 100789Sahrens */ 101789Sahrens ASSERT(newval != ZIO_CHECKSUM_INHERIT); 102789Sahrens 103789Sahrens osi->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE); 104789Sahrens } 105789Sahrens 106789Sahrens static void 107789Sahrens compression_changed_cb(void *arg, uint64_t newval) 108789Sahrens { 109789Sahrens objset_impl_t *osi = arg; 110789Sahrens 111789Sahrens /* 112789Sahrens * Inheritance and range checking should have been done by now. 113789Sahrens */ 114789Sahrens ASSERT(newval != ZIO_COMPRESS_INHERIT); 115789Sahrens 116789Sahrens osi->os_compress = zio_compress_select(newval, ZIO_COMPRESS_ON_VALUE); 117789Sahrens } 118789Sahrens 119789Sahrens void 120789Sahrens dmu_objset_byteswap(void *buf, size_t size) 121789Sahrens { 122789Sahrens objset_phys_t *osp = buf; 123789Sahrens 124789Sahrens ASSERT(size == sizeof (objset_phys_t)); 125789Sahrens dnode_byteswap(&osp->os_meta_dnode); 126789Sahrens byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t)); 127789Sahrens osp->os_type = BSWAP_64(osp->os_type); 128789Sahrens } 129789Sahrens 1301544Seschrock int 1311544Seschrock dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, 1321544Seschrock objset_impl_t **osip) 133789Sahrens { 134789Sahrens objset_impl_t *winner, *osi; 135789Sahrens int i, err, checksum; 136789Sahrens 137789Sahrens osi = kmem_zalloc(sizeof (objset_impl_t), KM_SLEEP); 138789Sahrens osi->os.os = osi; 139789Sahrens osi->os_dsl_dataset = ds; 140789Sahrens osi->os_spa = spa; 141789Sahrens if (bp) 142789Sahrens osi->os_rootbp = *bp; 143789Sahrens osi->os_phys = zio_buf_alloc(sizeof (objset_phys_t)); 144789Sahrens if (!BP_IS_HOLE(&osi->os_rootbp)) { 1452391Smaybee uint32_t aflags = ARC_WAIT; 1461544Seschrock zbookmark_t zb; 1471544Seschrock zb.zb_objset = ds ? ds->ds_object : 0; 1481544Seschrock zb.zb_object = 0; 1491544Seschrock zb.zb_level = -1; 1501544Seschrock zb.zb_blkid = 0; 1511544Seschrock 152789Sahrens dprintf_bp(&osi->os_rootbp, "reading %s", ""); 1531544Seschrock err = arc_read(NULL, spa, &osi->os_rootbp, 154789Sahrens dmu_ot[DMU_OT_OBJSET].ot_byteswap, 155789Sahrens arc_bcopy_func, osi->os_phys, 1562391Smaybee ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb); 1571544Seschrock if (err) { 1581544Seschrock zio_buf_free(osi->os_phys, sizeof (objset_phys_t)); 1591544Seschrock kmem_free(osi, sizeof (objset_impl_t)); 1601544Seschrock return (err); 1611544Seschrock } 162789Sahrens } else { 163789Sahrens bzero(osi->os_phys, sizeof (objset_phys_t)); 164789Sahrens } 165789Sahrens 166789Sahrens /* 167789Sahrens * Note: the changed_cb will be called once before the register 168789Sahrens * func returns, thus changing the checksum/compression from the 1692082Seschrock * default (fletcher2/off). Snapshots don't need to know, and 1702082Seschrock * registering would complicate clone promotion. 171789Sahrens */ 1722082Seschrock if (ds && ds->ds_phys->ds_num_children == 0) { 173789Sahrens err = dsl_prop_register(ds, "checksum", 174789Sahrens checksum_changed_cb, osi); 1751544Seschrock if (err == 0) 1761544Seschrock err = dsl_prop_register(ds, "compression", 1771544Seschrock compression_changed_cb, osi); 1781544Seschrock if (err) { 1791544Seschrock zio_buf_free(osi->os_phys, sizeof (objset_phys_t)); 1801544Seschrock kmem_free(osi, sizeof (objset_impl_t)); 1811544Seschrock return (err); 1821544Seschrock } 1832082Seschrock } else if (ds == NULL) { 184789Sahrens /* It's the meta-objset. */ 185789Sahrens osi->os_checksum = ZIO_CHECKSUM_FLETCHER_4; 1861544Seschrock osi->os_compress = ZIO_COMPRESS_LZJB; 187789Sahrens } 188789Sahrens 1891544Seschrock osi->os_zil = zil_alloc(&osi->os, &osi->os_phys->os_zil_header); 1901544Seschrock 191789Sahrens /* 192789Sahrens * Metadata always gets compressed and checksummed. 193789Sahrens * If the data checksum is multi-bit correctable, and it's not 194789Sahrens * a ZBT-style checksum, then it's suitable for metadata as well. 195789Sahrens * Otherwise, the metadata checksum defaults to fletcher4. 196789Sahrens */ 197789Sahrens checksum = osi->os_checksum; 198789Sahrens 199789Sahrens if (zio_checksum_table[checksum].ci_correctable && 200789Sahrens !zio_checksum_table[checksum].ci_zbt) 201789Sahrens osi->os_md_checksum = checksum; 202789Sahrens else 203789Sahrens osi->os_md_checksum = ZIO_CHECKSUM_FLETCHER_4; 2041544Seschrock osi->os_md_compress = ZIO_COMPRESS_LZJB; 205789Sahrens 206789Sahrens for (i = 0; i < TXG_SIZE; i++) { 207789Sahrens list_create(&osi->os_dirty_dnodes[i], sizeof (dnode_t), 208789Sahrens offsetof(dnode_t, dn_dirty_link[i])); 209789Sahrens list_create(&osi->os_free_dnodes[i], sizeof (dnode_t), 210789Sahrens offsetof(dnode_t, dn_dirty_link[i])); 211789Sahrens } 212789Sahrens list_create(&osi->os_dnodes, sizeof (dnode_t), 213789Sahrens offsetof(dnode_t, dn_link)); 214789Sahrens list_create(&osi->os_downgraded_dbufs, sizeof (dmu_buf_impl_t), 215789Sahrens offsetof(dmu_buf_impl_t, db_link)); 216789Sahrens 217789Sahrens osi->os_meta_dnode = dnode_special_open(osi, 218789Sahrens &osi->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT); 219789Sahrens 220789Sahrens if (ds != NULL) { 221789Sahrens winner = dsl_dataset_set_user_ptr(ds, osi, dmu_objset_evict); 222789Sahrens if (winner) { 223789Sahrens dmu_objset_evict(ds, osi); 224789Sahrens osi = winner; 225789Sahrens } 226789Sahrens } 227789Sahrens 2281544Seschrock *osip = osi; 2291544Seschrock return (0); 230789Sahrens } 231789Sahrens 232789Sahrens /* called from zpl */ 233789Sahrens int 234789Sahrens dmu_objset_open(const char *name, dmu_objset_type_t type, int mode, 235789Sahrens objset_t **osp) 236789Sahrens { 237789Sahrens dsl_dataset_t *ds; 238789Sahrens int err; 239789Sahrens objset_t *os; 240789Sahrens objset_impl_t *osi; 241789Sahrens 242789Sahrens os = kmem_alloc(sizeof (objset_t), KM_SLEEP); 243789Sahrens err = dsl_dataset_open(name, mode, os, &ds); 244789Sahrens if (err) { 245789Sahrens kmem_free(os, sizeof (objset_t)); 246789Sahrens return (err); 247789Sahrens } 248789Sahrens 249789Sahrens osi = dsl_dataset_get_user_ptr(ds); 250789Sahrens if (osi == NULL) { 251789Sahrens blkptr_t bp; 252789Sahrens 253789Sahrens dsl_dataset_get_blkptr(ds, &bp); 2541544Seschrock err = dmu_objset_open_impl(dsl_dataset_get_spa(ds), 2551544Seschrock ds, &bp, &osi); 2561544Seschrock if (err) { 2571544Seschrock dsl_dataset_close(ds, mode, os); 2581544Seschrock kmem_free(os, sizeof (objset_t)); 2591544Seschrock return (err); 2601544Seschrock } 261789Sahrens } 262789Sahrens 263789Sahrens os->os = osi; 264789Sahrens os->os_mode = mode; 265789Sahrens 266789Sahrens if (type != DMU_OST_ANY && type != os->os->os_phys->os_type) { 267789Sahrens dmu_objset_close(os); 268789Sahrens return (EINVAL); 269789Sahrens } 270789Sahrens *osp = os; 271789Sahrens return (0); 272789Sahrens } 273789Sahrens 274789Sahrens void 275789Sahrens dmu_objset_close(objset_t *os) 276789Sahrens { 277789Sahrens dsl_dataset_close(os->os->os_dsl_dataset, os->os_mode, os); 278789Sahrens kmem_free(os, sizeof (objset_t)); 279789Sahrens } 280789Sahrens 2811646Sperrin int 2821646Sperrin dmu_objset_evict_dbufs(objset_t *os, int try) 2831544Seschrock { 2841544Seschrock objset_impl_t *osi = os->os; 2851544Seschrock dnode_t *dn; 2861596Sahrens 2871596Sahrens mutex_enter(&osi->os_lock); 2881596Sahrens 2891596Sahrens /* process the mdn last, since the other dnodes have holds on it */ 2901596Sahrens list_remove(&osi->os_dnodes, osi->os_meta_dnode); 2911596Sahrens list_insert_tail(&osi->os_dnodes, osi->os_meta_dnode); 2921544Seschrock 2931544Seschrock /* 2941596Sahrens * Find the first dnode with holds. We have to do this dance 2951596Sahrens * because dnode_add_ref() only works if you already have a 2961596Sahrens * hold. If there are no holds then it has no dbufs so OK to 2971596Sahrens * skip. 2981544Seschrock */ 2991596Sahrens for (dn = list_head(&osi->os_dnodes); 3001596Sahrens dn && refcount_is_zero(&dn->dn_holds); 3011596Sahrens dn = list_next(&osi->os_dnodes, dn)) 3021596Sahrens continue; 3031596Sahrens if (dn) 3041596Sahrens dnode_add_ref(dn, FTAG); 3051596Sahrens 3061596Sahrens while (dn) { 3071596Sahrens dnode_t *next_dn = dn; 3081596Sahrens 3091596Sahrens do { 3101596Sahrens next_dn = list_next(&osi->os_dnodes, next_dn); 3111596Sahrens } while (next_dn && refcount_is_zero(&next_dn->dn_holds)); 3121596Sahrens if (next_dn) 3131596Sahrens dnode_add_ref(next_dn, FTAG); 3141596Sahrens 3151596Sahrens mutex_exit(&osi->os_lock); 3161646Sperrin if (dnode_evict_dbufs(dn, try)) { 3171646Sperrin dnode_rele(dn, FTAG); 3181646Sperrin if (next_dn) 3191646Sperrin dnode_rele(next_dn, FTAG); 3201646Sperrin return (1); 3211646Sperrin } 3221596Sahrens dnode_rele(dn, FTAG); 3231596Sahrens mutex_enter(&osi->os_lock); 3241596Sahrens dn = next_dn; 3251544Seschrock } 3261544Seschrock mutex_exit(&osi->os_lock); 3271646Sperrin return (0); 3281544Seschrock } 3291544Seschrock 3301544Seschrock void 331789Sahrens dmu_objset_evict(dsl_dataset_t *ds, void *arg) 332789Sahrens { 333789Sahrens objset_impl_t *osi = arg; 3341544Seschrock objset_t os; 3352082Seschrock int i; 336789Sahrens 337789Sahrens for (i = 0; i < TXG_SIZE; i++) { 338789Sahrens ASSERT(list_head(&osi->os_dirty_dnodes[i]) == NULL); 339789Sahrens ASSERT(list_head(&osi->os_free_dnodes[i]) == NULL); 340789Sahrens } 341789Sahrens 3422082Seschrock if (ds && ds->ds_phys->ds_num_children == 0) { 3432082Seschrock VERIFY(0 == dsl_prop_unregister(ds, "checksum", 3442082Seschrock checksum_changed_cb, osi)); 3452082Seschrock VERIFY(0 == dsl_prop_unregister(ds, "compression", 3462082Seschrock compression_changed_cb, osi)); 347789Sahrens } 348789Sahrens 3491544Seschrock /* 3501544Seschrock * We should need only a single pass over the dnode list, since 3511544Seschrock * nothing can be added to the list at this point. 3521544Seschrock */ 3531544Seschrock os.os = osi; 3541646Sperrin (void) dmu_objset_evict_dbufs(&os, 0); 3551544Seschrock 356789Sahrens ASSERT3P(list_head(&osi->os_dnodes), ==, osi->os_meta_dnode); 357789Sahrens ASSERT3P(list_tail(&osi->os_dnodes), ==, osi->os_meta_dnode); 358789Sahrens ASSERT3P(list_head(&osi->os_meta_dnode->dn_dbufs), ==, NULL); 359789Sahrens 360789Sahrens dnode_special_close(osi->os_meta_dnode); 361789Sahrens zil_free(osi->os_zil); 362789Sahrens 363789Sahrens zio_buf_free(osi->os_phys, sizeof (objset_phys_t)); 364789Sahrens kmem_free(osi, sizeof (objset_impl_t)); 365789Sahrens } 366789Sahrens 367789Sahrens /* called from dsl for meta-objset */ 368789Sahrens objset_impl_t * 369789Sahrens dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, dmu_objset_type_t type, 370789Sahrens dmu_tx_t *tx) 371789Sahrens { 372789Sahrens objset_impl_t *osi; 373789Sahrens dnode_t *mdn; 374789Sahrens 375789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 3761544Seschrock VERIFY(0 == dmu_objset_open_impl(spa, ds, NULL, &osi)); 377789Sahrens mdn = osi->os_meta_dnode; 378789Sahrens 379789Sahrens dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT, 380789Sahrens DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx); 381789Sahrens 382789Sahrens /* 383789Sahrens * We don't want to have to increase the meta-dnode's nlevels 384789Sahrens * later, because then we could do it in quescing context while 385789Sahrens * we are also accessing it in open context. 386789Sahrens * 387789Sahrens * This precaution is not necessary for the MOS (ds == NULL), 388789Sahrens * because the MOS is only updated in syncing context. 389789Sahrens * This is most fortunate: the MOS is the only objset that 390789Sahrens * needs to be synced multiple times as spa_sync() iterates 391789Sahrens * to convergence, so minimizing its dn_nlevels matters. 392789Sahrens */ 3931544Seschrock if (ds != NULL) { 3941544Seschrock int levels = 1; 3951544Seschrock 3961544Seschrock /* 3971544Seschrock * Determine the number of levels necessary for the meta-dnode 3981544Seschrock * to contain DN_MAX_OBJECT dnodes. 3991544Seschrock */ 4001544Seschrock while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift + 4011544Seschrock (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) < 4021544Seschrock DN_MAX_OBJECT * sizeof (dnode_phys_t)) 4031544Seschrock levels++; 4041544Seschrock 405789Sahrens mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] = 4061544Seschrock mdn->dn_nlevels = levels; 4071544Seschrock } 408789Sahrens 409789Sahrens ASSERT(type != DMU_OST_NONE); 410789Sahrens ASSERT(type != DMU_OST_ANY); 411789Sahrens ASSERT(type < DMU_OST_NUMTYPES); 412789Sahrens osi->os_phys->os_type = type; 413789Sahrens 414789Sahrens dsl_dataset_dirty(ds, tx); 415789Sahrens 416789Sahrens return (osi); 417789Sahrens } 418789Sahrens 419789Sahrens struct oscarg { 420789Sahrens void (*userfunc)(objset_t *os, void *arg, dmu_tx_t *tx); 421789Sahrens void *userarg; 422789Sahrens dsl_dataset_t *clone_parent; 423789Sahrens const char *lastname; 424789Sahrens dmu_objset_type_t type; 425789Sahrens }; 426789Sahrens 4272199Sahrens /* ARGSUSED */ 428789Sahrens static int 4292199Sahrens dmu_objset_create_check(void *arg1, void *arg2, dmu_tx_t *tx) 430789Sahrens { 4312199Sahrens dsl_dir_t *dd = arg1; 4322199Sahrens struct oscarg *oa = arg2; 4332199Sahrens objset_t *mos = dd->dd_pool->dp_meta_objset; 4342199Sahrens int err; 4352199Sahrens uint64_t ddobj; 4362199Sahrens 4372199Sahrens err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj, 4382199Sahrens oa->lastname, sizeof (uint64_t), 1, &ddobj); 4392199Sahrens if (err != ENOENT) 4402199Sahrens return (err ? err : EEXIST); 4412199Sahrens 4422199Sahrens if (oa->clone_parent != NULL) { 4432199Sahrens /* 4442199Sahrens * You can't clone across pools. 4452199Sahrens */ 4462199Sahrens if (oa->clone_parent->ds_dir->dd_pool != dd->dd_pool) 4472199Sahrens return (EXDEV); 4482199Sahrens 4492199Sahrens /* 4502199Sahrens * You can only clone snapshots, not the head datasets. 4512199Sahrens */ 4522199Sahrens if (oa->clone_parent->ds_phys->ds_num_children == 0) 4532199Sahrens return (EINVAL); 4542199Sahrens } 4552199Sahrens return (0); 4562199Sahrens } 4572199Sahrens 4582199Sahrens static void 4592199Sahrens dmu_objset_create_sync(void *arg1, void *arg2, dmu_tx_t *tx) 4602199Sahrens { 4612199Sahrens dsl_dir_t *dd = arg1; 4622199Sahrens struct oscarg *oa = arg2; 463789Sahrens dsl_dataset_t *ds; 464789Sahrens blkptr_t bp; 4652199Sahrens uint64_t dsobj; 466789Sahrens 467789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 468789Sahrens 4692199Sahrens dsobj = dsl_dataset_create_sync(dd, oa->lastname, 470789Sahrens oa->clone_parent, tx); 471789Sahrens 4722199Sahrens VERIFY(0 == dsl_dataset_open_obj(dd->dd_pool, dsobj, NULL, 4731544Seschrock DS_MODE_STANDARD | DS_MODE_READONLY, FTAG, &ds)); 474789Sahrens dsl_dataset_get_blkptr(ds, &bp); 475789Sahrens if (BP_IS_HOLE(&bp)) { 476789Sahrens objset_impl_t *osi; 477789Sahrens 478789Sahrens /* This is an empty dmu_objset; not a clone. */ 479789Sahrens osi = dmu_objset_create_impl(dsl_dataset_get_spa(ds), 480789Sahrens ds, oa->type, tx); 481789Sahrens 482789Sahrens if (oa->userfunc) 483789Sahrens oa->userfunc(&osi->os, oa->userarg, tx); 484789Sahrens } 485789Sahrens dsl_dataset_close(ds, DS_MODE_STANDARD | DS_MODE_READONLY, FTAG); 486789Sahrens } 487789Sahrens 488789Sahrens int 489789Sahrens dmu_objset_create(const char *name, dmu_objset_type_t type, 490789Sahrens objset_t *clone_parent, 491789Sahrens void (*func)(objset_t *os, void *arg, dmu_tx_t *tx), void *arg) 492789Sahrens { 4932199Sahrens dsl_dir_t *pdd; 494789Sahrens const char *tail; 495789Sahrens int err = 0; 4962199Sahrens struct oscarg oa = { 0 }; 497789Sahrens 4982199Sahrens ASSERT(strchr(name, '@') == NULL); 4992199Sahrens err = dsl_dir_open(name, FTAG, &pdd, &tail); 5001544Seschrock if (err) 5011544Seschrock return (err); 502789Sahrens if (tail == NULL) { 5032199Sahrens dsl_dir_close(pdd, FTAG); 504789Sahrens return (EEXIST); 505789Sahrens } 506789Sahrens 507789Sahrens dprintf("name=%s\n", name); 508789Sahrens 5092199Sahrens oa.userfunc = func; 5102199Sahrens oa.userarg = arg; 5112199Sahrens oa.lastname = tail; 5122199Sahrens oa.type = type; 5132199Sahrens if (clone_parent != NULL) { 514789Sahrens /* 5152199Sahrens * You can't clone to a different type. 516789Sahrens */ 5172199Sahrens if (clone_parent->os->os_phys->os_type != type) { 5182199Sahrens dsl_dir_close(pdd, FTAG); 5192199Sahrens return (EINVAL); 520789Sahrens } 5212199Sahrens oa.clone_parent = clone_parent->os->os_dsl_dataset; 522789Sahrens } 5232199Sahrens err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check, 5242199Sahrens dmu_objset_create_sync, pdd, &oa, 5); 5252199Sahrens dsl_dir_close(pdd, FTAG); 526789Sahrens return (err); 527789Sahrens } 528789Sahrens 529789Sahrens int 530789Sahrens dmu_objset_destroy(const char *name) 531789Sahrens { 532789Sahrens objset_t *os; 533789Sahrens int error; 534789Sahrens 535789Sahrens /* 536789Sahrens * If it looks like we'll be able to destroy it, and there's 537789Sahrens * an unplayed replay log sitting around, destroy the log. 538789Sahrens * It would be nicer to do this in dsl_dataset_destroy_sync(), 539789Sahrens * but the replay log objset is modified in open context. 540789Sahrens */ 541789Sahrens error = dmu_objset_open(name, DMU_OST_ANY, DS_MODE_EXCLUSIVE, &os); 542789Sahrens if (error == 0) { 5431807Sbonwick zil_destroy(dmu_objset_zil(os), B_FALSE); 544789Sahrens dmu_objset_close(os); 545789Sahrens } 546789Sahrens 547789Sahrens return (dsl_dataset_destroy(name)); 548789Sahrens } 549789Sahrens 550789Sahrens int 551789Sahrens dmu_objset_rollback(const char *name) 552789Sahrens { 553789Sahrens int err; 554789Sahrens objset_t *os; 555789Sahrens 5562199Sahrens err = dmu_objset_open(name, DMU_OST_ANY, 5572199Sahrens DS_MODE_EXCLUSIVE | DS_MODE_INCONSISTENT, &os); 558789Sahrens if (err == 0) { 559789Sahrens err = zil_suspend(dmu_objset_zil(os)); 560789Sahrens if (err == 0) 561789Sahrens zil_resume(dmu_objset_zil(os)); 562789Sahrens if (err == 0) { 563789Sahrens /* XXX uncache everything? */ 5642199Sahrens err = dsl_dataset_rollback(os->os->os_dsl_dataset); 565789Sahrens } 5662199Sahrens dmu_objset_close(os); 567789Sahrens } 568789Sahrens return (err); 569789Sahrens } 570789Sahrens 5712199Sahrens struct snaparg { 5722199Sahrens dsl_sync_task_group_t *dstg; 5732199Sahrens char *snapname; 5742199Sahrens char failed[MAXPATHLEN]; 5752199Sahrens }; 5762199Sahrens 5772199Sahrens static int 5782199Sahrens dmu_objset_snapshot_one(char *name, void *arg) 5792199Sahrens { 5802199Sahrens struct snaparg *sn = arg; 5812199Sahrens objset_t *os; 5822199Sahrens int err; 5832199Sahrens 5842199Sahrens (void) strcpy(sn->failed, name); 5852199Sahrens 5862199Sahrens err = dmu_objset_open(name, DMU_OST_ANY, DS_MODE_STANDARD, &os); 5872199Sahrens if (err != 0) 5882199Sahrens return (err); 5892199Sahrens 5902199Sahrens /* 5912199Sahrens * NB: we need to wait for all in-flight changes to get to disk, 5922199Sahrens * so that we snapshot those changes. zil_suspend does this as 5932199Sahrens * a side effect. 5942199Sahrens */ 5952199Sahrens err = zil_suspend(dmu_objset_zil(os)); 5962199Sahrens if (err == 0) { 5972199Sahrens dsl_sync_task_create(sn->dstg, dsl_dataset_snapshot_check, 5982199Sahrens dsl_dataset_snapshot_sync, os, sn->snapname, 3); 5992199Sahrens } 6002199Sahrens return (err); 6012199Sahrens } 6022199Sahrens 6032199Sahrens int 6042199Sahrens dmu_objset_snapshot(char *fsname, char *snapname, boolean_t recursive) 6052199Sahrens { 6062199Sahrens dsl_sync_task_t *dst; 6072199Sahrens struct snaparg sn = { 0 }; 6082199Sahrens char *cp; 6092199Sahrens spa_t *spa; 6102199Sahrens int err; 6112199Sahrens 6122199Sahrens (void) strcpy(sn.failed, fsname); 6132199Sahrens 6142199Sahrens cp = strchr(fsname, '/'); 6152199Sahrens if (cp) { 6162199Sahrens *cp = '\0'; 6172199Sahrens err = spa_open(fsname, &spa, FTAG); 6182199Sahrens *cp = '/'; 6192199Sahrens } else { 6202199Sahrens err = spa_open(fsname, &spa, FTAG); 6212199Sahrens } 6222199Sahrens if (err) 6232199Sahrens return (err); 6242199Sahrens 6252199Sahrens sn.dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 6262199Sahrens sn.snapname = snapname; 6272199Sahrens 628*2417Sahrens if (recursive) { 629*2417Sahrens err = dmu_objset_find(fsname, 630*2417Sahrens dmu_objset_snapshot_one, &sn, DS_FIND_CHILDREN); 631*2417Sahrens } else { 6322199Sahrens err = dmu_objset_snapshot_one(fsname, &sn); 633*2417Sahrens } 6342199Sahrens 6352199Sahrens if (err) 6362199Sahrens goto out; 6372199Sahrens 6382199Sahrens err = dsl_sync_task_group_wait(sn.dstg); 6392199Sahrens 6402199Sahrens for (dst = list_head(&sn.dstg->dstg_tasks); dst; 6412199Sahrens dst = list_next(&sn.dstg->dstg_tasks, dst)) { 6422199Sahrens objset_t *os = dst->dst_arg1; 6432199Sahrens if (dst->dst_err) 6442199Sahrens dmu_objset_name(os, sn.failed); 6452199Sahrens zil_resume(dmu_objset_zil(os)); 6462199Sahrens dmu_objset_close(os); 6472199Sahrens } 6482199Sahrens out: 6492199Sahrens if (err) 6502199Sahrens (void) strcpy(fsname, sn.failed); 6512199Sahrens dsl_sync_task_group_destroy(sn.dstg); 6522199Sahrens spa_close(spa, FTAG); 6532199Sahrens return (err); 6542199Sahrens } 6552199Sahrens 656789Sahrens static void 657789Sahrens dmu_objset_sync_dnodes(objset_impl_t *os, list_t *list, dmu_tx_t *tx) 658789Sahrens { 659789Sahrens dnode_t *dn = list_head(list); 660789Sahrens int level, err; 661789Sahrens 662789Sahrens for (level = 0; dn = list_head(list); level++) { 663789Sahrens zio_t *zio; 664789Sahrens zio = zio_root(os->os_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 665789Sahrens 666789Sahrens ASSERT3U(level, <=, DN_MAX_LEVELS); 667789Sahrens 668789Sahrens while (dn) { 669789Sahrens dnode_t *next = list_next(list, dn); 670789Sahrens 671789Sahrens list_remove(list, dn); 672789Sahrens if (dnode_sync(dn, level, zio, tx) == 0) { 673789Sahrens /* 674789Sahrens * This dnode requires syncing at higher 675789Sahrens * levels; put it back onto the list. 676789Sahrens */ 677789Sahrens if (next) 678789Sahrens list_insert_before(list, next, dn); 679789Sahrens else 680789Sahrens list_insert_tail(list, dn); 681789Sahrens } 682789Sahrens dn = next; 683789Sahrens } 684789Sahrens err = zio_wait(zio); 685789Sahrens ASSERT(err == 0); 686789Sahrens } 687789Sahrens } 688789Sahrens 689789Sahrens /* ARGSUSED */ 690789Sahrens static void 691789Sahrens killer(zio_t *zio, arc_buf_t *abuf, void *arg) 692789Sahrens { 693789Sahrens objset_impl_t *os = arg; 694789Sahrens objset_phys_t *osphys = zio->io_data; 695789Sahrens dnode_phys_t *dnp = &osphys->os_meta_dnode; 696789Sahrens int i; 697789Sahrens 698789Sahrens ASSERT3U(zio->io_error, ==, 0); 699789Sahrens 700789Sahrens /* 701789Sahrens * Update rootbp fill count. 702789Sahrens */ 703789Sahrens os->os_rootbp.blk_fill = 1; /* count the meta-dnode */ 704789Sahrens for (i = 0; i < dnp->dn_nblkptr; i++) 705789Sahrens os->os_rootbp.blk_fill += dnp->dn_blkptr[i].blk_fill; 706789Sahrens 707789Sahrens BP_SET_TYPE(zio->io_bp, DMU_OT_OBJSET); 708789Sahrens BP_SET_LEVEL(zio->io_bp, 0); 709789Sahrens 710789Sahrens if (!DVA_EQUAL(BP_IDENTITY(zio->io_bp), 711789Sahrens BP_IDENTITY(&zio->io_bp_orig))) { 712789Sahrens dsl_dataset_block_kill(os->os_dsl_dataset, &zio->io_bp_orig, 713789Sahrens os->os_synctx); 714789Sahrens dsl_dataset_block_born(os->os_dsl_dataset, zio->io_bp, 715789Sahrens os->os_synctx); 716789Sahrens } 717789Sahrens } 718789Sahrens 719789Sahrens 720789Sahrens /* called from dsl */ 721789Sahrens void 722789Sahrens dmu_objset_sync(objset_impl_t *os, dmu_tx_t *tx) 723789Sahrens { 724789Sahrens extern taskq_t *dbuf_tq; 725789Sahrens int txgoff; 726789Sahrens list_t *dirty_list; 727789Sahrens int err; 7281544Seschrock zbookmark_t zb; 729789Sahrens arc_buf_t *abuf = 730789Sahrens arc_buf_alloc(os->os_spa, sizeof (objset_phys_t), FTAG); 731789Sahrens 732789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 733789Sahrens ASSERT(os->os_synctx == NULL); 734789Sahrens /* XXX the write_done callback should really give us the tx... */ 735789Sahrens os->os_synctx = tx; 736789Sahrens 737789Sahrens dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg); 738789Sahrens 739789Sahrens txgoff = tx->tx_txg & TXG_MASK; 740789Sahrens 741789Sahrens dmu_objset_sync_dnodes(os, &os->os_free_dnodes[txgoff], tx); 742789Sahrens dmu_objset_sync_dnodes(os, &os->os_dirty_dnodes[txgoff], tx); 743789Sahrens 744789Sahrens /* 745789Sahrens * Free intent log blocks up to this tx. 746789Sahrens */ 747789Sahrens zil_sync(os->os_zil, tx); 748789Sahrens 749789Sahrens /* 750789Sahrens * Sync meta-dnode 751789Sahrens */ 752789Sahrens dirty_list = &os->os_dirty_dnodes[txgoff]; 753789Sahrens ASSERT(list_head(dirty_list) == NULL); 754789Sahrens list_insert_tail(dirty_list, os->os_meta_dnode); 755789Sahrens dmu_objset_sync_dnodes(os, dirty_list, tx); 756789Sahrens 757789Sahrens /* 758789Sahrens * Sync the root block. 759789Sahrens */ 760789Sahrens bcopy(os->os_phys, abuf->b_data, sizeof (objset_phys_t)); 7611544Seschrock zb.zb_objset = os->os_dsl_dataset ? os->os_dsl_dataset->ds_object : 0; 7621544Seschrock zb.zb_object = 0; 7631544Seschrock zb.zb_level = -1; 7641544Seschrock zb.zb_blkid = 0; 765789Sahrens err = arc_write(NULL, os->os_spa, os->os_md_checksum, 7661775Sbillm os->os_md_compress, 7671775Sbillm dmu_get_replication_level(os->os_spa, &zb, DMU_OT_OBJSET), 7681775Sbillm tx->tx_txg, &os->os_rootbp, abuf, killer, os, 7691544Seschrock ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, ARC_WAIT, &zb); 770789Sahrens ASSERT(err == 0); 7711544Seschrock VERIFY(arc_buf_remove_ref(abuf, FTAG) == 1); 772789Sahrens 773789Sahrens dsl_dataset_set_blkptr(os->os_dsl_dataset, &os->os_rootbp, tx); 774789Sahrens 775789Sahrens ASSERT3P(os->os_synctx, ==, tx); 776789Sahrens taskq_wait(dbuf_tq); 777789Sahrens os->os_synctx = NULL; 778789Sahrens } 779789Sahrens 780789Sahrens void 781789Sahrens dmu_objset_stats(objset_t *os, dmu_objset_stats_t *dds) 782789Sahrens { 783789Sahrens if (os->os->os_dsl_dataset != NULL) { 784789Sahrens dsl_dataset_stats(os->os->os_dsl_dataset, dds); 785789Sahrens } else { 786789Sahrens ASSERT(os->os->os_phys->os_type == DMU_OST_META); 787789Sahrens bzero(dds, sizeof (*dds)); 788789Sahrens } 789789Sahrens dds->dds_type = os->os->os_phys->os_type; 790789Sahrens } 791789Sahrens 792789Sahrens int 793789Sahrens dmu_objset_is_snapshot(objset_t *os) 794789Sahrens { 795789Sahrens if (os->os->os_dsl_dataset != NULL) 796789Sahrens return (dsl_dataset_is_snapshot(os->os->os_dsl_dataset)); 797789Sahrens else 798789Sahrens return (B_FALSE); 799789Sahrens } 800789Sahrens 801789Sahrens int 802789Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name, 803885Sahrens uint64_t *idp, uint64_t *offp) 804789Sahrens { 805789Sahrens dsl_dataset_t *ds = os->os->os_dsl_dataset; 806789Sahrens zap_cursor_t cursor; 807789Sahrens zap_attribute_t attr; 808789Sahrens 809789Sahrens if (ds->ds_phys->ds_snapnames_zapobj == 0) 810789Sahrens return (ENOENT); 811789Sahrens 812789Sahrens zap_cursor_init_serialized(&cursor, 813789Sahrens ds->ds_dir->dd_pool->dp_meta_objset, 814789Sahrens ds->ds_phys->ds_snapnames_zapobj, *offp); 815789Sahrens 816885Sahrens if (zap_cursor_retrieve(&cursor, &attr) != 0) { 817885Sahrens zap_cursor_fini(&cursor); 818885Sahrens return (ENOENT); 819885Sahrens } 820885Sahrens 821885Sahrens if (strlen(attr.za_name) + 1 > namelen) { 822885Sahrens zap_cursor_fini(&cursor); 823885Sahrens return (ENAMETOOLONG); 824885Sahrens } 825885Sahrens 826885Sahrens (void) strcpy(name, attr.za_name); 827885Sahrens if (idp) 828885Sahrens *idp = attr.za_first_integer; 829885Sahrens zap_cursor_advance(&cursor); 830885Sahrens *offp = zap_cursor_serialize(&cursor); 831885Sahrens zap_cursor_fini(&cursor); 832885Sahrens 833885Sahrens return (0); 834885Sahrens } 835885Sahrens 836885Sahrens int 837885Sahrens dmu_dir_list_next(objset_t *os, int namelen, char *name, 838885Sahrens uint64_t *idp, uint64_t *offp) 839885Sahrens { 840885Sahrens dsl_dir_t *dd = os->os->os_dsl_dataset->ds_dir; 841885Sahrens zap_cursor_t cursor; 842885Sahrens zap_attribute_t attr; 843885Sahrens 844885Sahrens /* there is no next dir on a snapshot! */ 845885Sahrens if (os->os->os_dsl_dataset->ds_object != 846885Sahrens dd->dd_phys->dd_head_dataset_obj) 847885Sahrens return (ENOENT); 848885Sahrens 849885Sahrens zap_cursor_init_serialized(&cursor, 850885Sahrens dd->dd_pool->dp_meta_objset, 851885Sahrens dd->dd_phys->dd_child_dir_zapobj, *offp); 852885Sahrens 853885Sahrens if (zap_cursor_retrieve(&cursor, &attr) != 0) { 854885Sahrens zap_cursor_fini(&cursor); 855885Sahrens return (ENOENT); 856885Sahrens } 857885Sahrens 858885Sahrens if (strlen(attr.za_name) + 1 > namelen) { 859885Sahrens zap_cursor_fini(&cursor); 860789Sahrens return (ENAMETOOLONG); 861885Sahrens } 862789Sahrens 863789Sahrens (void) strcpy(name, attr.za_name); 864885Sahrens if (idp) 865885Sahrens *idp = attr.za_first_integer; 866789Sahrens zap_cursor_advance(&cursor); 867789Sahrens *offp = zap_cursor_serialize(&cursor); 868885Sahrens zap_cursor_fini(&cursor); 869789Sahrens 870789Sahrens return (0); 871789Sahrens } 872789Sahrens 873789Sahrens /* 874789Sahrens * Find all objsets under name, and for each, call 'func(child_name, arg)'. 875789Sahrens */ 8762199Sahrens int 8772199Sahrens dmu_objset_find(char *name, int func(char *, void *), void *arg, int flags) 878789Sahrens { 879789Sahrens dsl_dir_t *dd; 880789Sahrens objset_t *os; 881789Sahrens uint64_t snapobj; 882789Sahrens zap_cursor_t zc; 883789Sahrens zap_attribute_t attr; 884789Sahrens char *child; 8851544Seschrock int do_self, err; 886789Sahrens 8871544Seschrock err = dsl_dir_open(name, FTAG, &dd, NULL); 8881544Seschrock if (err) 8892199Sahrens return (err); 890789Sahrens 8912199Sahrens /* NB: the $MOS dir doesn't have a head dataset */ 892789Sahrens do_self = (dd->dd_phys->dd_head_dataset_obj != 0); 893789Sahrens 894789Sahrens /* 895789Sahrens * Iterate over all children. 896789Sahrens */ 897*2417Sahrens if (flags & DS_FIND_CHILDREN) { 898*2417Sahrens for (zap_cursor_init(&zc, dd->dd_pool->dp_meta_objset, 899*2417Sahrens dd->dd_phys->dd_child_dir_zapobj); 900*2417Sahrens zap_cursor_retrieve(&zc, &attr) == 0; 901*2417Sahrens (void) zap_cursor_advance(&zc)) { 902*2417Sahrens ASSERT(attr.za_integer_length == sizeof (uint64_t)); 903*2417Sahrens ASSERT(attr.za_num_integers == 1); 904789Sahrens 905*2417Sahrens /* 906*2417Sahrens * No separating '/' because parent's name ends in /. 907*2417Sahrens */ 908*2417Sahrens child = kmem_alloc(MAXPATHLEN, KM_SLEEP); 909*2417Sahrens /* XXX could probably just use name here */ 910*2417Sahrens dsl_dir_name(dd, child); 911*2417Sahrens (void) strcat(child, "/"); 912*2417Sahrens (void) strcat(child, attr.za_name); 913*2417Sahrens err = dmu_objset_find(child, func, arg, flags); 914*2417Sahrens kmem_free(child, MAXPATHLEN); 915*2417Sahrens if (err) 916*2417Sahrens break; 917*2417Sahrens } 918*2417Sahrens zap_cursor_fini(&zc); 9192199Sahrens 920*2417Sahrens if (err) { 921*2417Sahrens dsl_dir_close(dd, FTAG); 922*2417Sahrens return (err); 923*2417Sahrens } 924789Sahrens } 925789Sahrens 926789Sahrens /* 927789Sahrens * Iterate over all snapshots. 928789Sahrens */ 929789Sahrens if ((flags & DS_FIND_SNAPSHOTS) && 930789Sahrens dmu_objset_open(name, DMU_OST_ANY, 931789Sahrens DS_MODE_STANDARD | DS_MODE_READONLY, &os) == 0) { 932789Sahrens 933789Sahrens snapobj = os->os->os_dsl_dataset->ds_phys->ds_snapnames_zapobj; 934789Sahrens dmu_objset_close(os); 935789Sahrens 936789Sahrens for (zap_cursor_init(&zc, dd->dd_pool->dp_meta_objset, snapobj); 937789Sahrens zap_cursor_retrieve(&zc, &attr) == 0; 938789Sahrens (void) zap_cursor_advance(&zc)) { 939789Sahrens ASSERT(attr.za_integer_length == sizeof (uint64_t)); 940789Sahrens ASSERT(attr.za_num_integers == 1); 941789Sahrens 942789Sahrens child = kmem_alloc(MAXPATHLEN, KM_SLEEP); 943789Sahrens /* XXX could probably just use name here */ 944789Sahrens dsl_dir_name(dd, child); 945789Sahrens (void) strcat(child, "@"); 946789Sahrens (void) strcat(child, attr.za_name); 9472199Sahrens err = func(child, arg); 948789Sahrens kmem_free(child, MAXPATHLEN); 9492199Sahrens if (err) 9502199Sahrens break; 951789Sahrens } 952885Sahrens zap_cursor_fini(&zc); 953789Sahrens } 954789Sahrens 955789Sahrens dsl_dir_close(dd, FTAG); 956789Sahrens 9572199Sahrens if (err) 9582199Sahrens return (err); 9592199Sahrens 960789Sahrens /* 961789Sahrens * Apply to self if appropriate. 962789Sahrens */ 963789Sahrens if (do_self) 9642199Sahrens err = func(name, arg); 9652199Sahrens return (err); 966789Sahrens } 967