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 /* 223547Smaybee * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 27789Sahrens 284543Smarks #include <sys/cred.h> 29789Sahrens #include <sys/zfs_context.h> 30789Sahrens #include <sys/dmu_objset.h> 31789Sahrens #include <sys/dsl_dir.h> 32789Sahrens #include <sys/dsl_dataset.h> 33789Sahrens #include <sys/dsl_prop.h> 34789Sahrens #include <sys/dsl_pool.h> 352199Sahrens #include <sys/dsl_synctask.h> 364543Smarks #include <sys/dsl_deleg.h> 37789Sahrens #include <sys/dnode.h> 38789Sahrens #include <sys/dbuf.h> 392885Sahrens #include <sys/zvol.h> 40789Sahrens #include <sys/dmu_tx.h> 41789Sahrens #include <sys/zio_checksum.h> 42789Sahrens #include <sys/zap.h> 43789Sahrens #include <sys/zil.h> 44789Sahrens #include <sys/dmu_impl.h> 454543Smarks #include <sys/zfs_ioctl.h> 46789Sahrens 47789Sahrens spa_t * 48789Sahrens dmu_objset_spa(objset_t *os) 49789Sahrens { 50789Sahrens return (os->os->os_spa); 51789Sahrens } 52789Sahrens 53789Sahrens zilog_t * 54789Sahrens dmu_objset_zil(objset_t *os) 55789Sahrens { 56789Sahrens return (os->os->os_zil); 57789Sahrens } 58789Sahrens 59789Sahrens dsl_pool_t * 60789Sahrens dmu_objset_pool(objset_t *os) 61789Sahrens { 62789Sahrens dsl_dataset_t *ds; 63789Sahrens 64789Sahrens if ((ds = os->os->os_dsl_dataset) != NULL && ds->ds_dir) 65789Sahrens return (ds->ds_dir->dd_pool); 66789Sahrens else 67789Sahrens return (spa_get_dsl(os->os->os_spa)); 68789Sahrens } 69789Sahrens 70789Sahrens dsl_dataset_t * 71789Sahrens dmu_objset_ds(objset_t *os) 72789Sahrens { 73789Sahrens return (os->os->os_dsl_dataset); 74789Sahrens } 75789Sahrens 76789Sahrens dmu_objset_type_t 77789Sahrens dmu_objset_type(objset_t *os) 78789Sahrens { 79789Sahrens return (os->os->os_phys->os_type); 80789Sahrens } 81789Sahrens 82789Sahrens void 83789Sahrens dmu_objset_name(objset_t *os, char *buf) 84789Sahrens { 85789Sahrens dsl_dataset_name(os->os->os_dsl_dataset, buf); 86789Sahrens } 87789Sahrens 88789Sahrens uint64_t 89789Sahrens dmu_objset_id(objset_t *os) 90789Sahrens { 91789Sahrens dsl_dataset_t *ds = os->os->os_dsl_dataset; 92789Sahrens 93789Sahrens return (ds ? ds->ds_object : 0); 94789Sahrens } 95789Sahrens 96789Sahrens static void 97789Sahrens checksum_changed_cb(void *arg, uint64_t newval) 98789Sahrens { 99789Sahrens objset_impl_t *osi = arg; 100789Sahrens 101789Sahrens /* 102789Sahrens * Inheritance should have been done by now. 103789Sahrens */ 104789Sahrens ASSERT(newval != ZIO_CHECKSUM_INHERIT); 105789Sahrens 106789Sahrens osi->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE); 107789Sahrens } 108789Sahrens 109789Sahrens static void 110789Sahrens compression_changed_cb(void *arg, uint64_t newval) 111789Sahrens { 112789Sahrens objset_impl_t *osi = arg; 113789Sahrens 114789Sahrens /* 115789Sahrens * Inheritance and range checking should have been done by now. 116789Sahrens */ 117789Sahrens ASSERT(newval != ZIO_COMPRESS_INHERIT); 118789Sahrens 119789Sahrens osi->os_compress = zio_compress_select(newval, ZIO_COMPRESS_ON_VALUE); 120789Sahrens } 121789Sahrens 1223835Sahrens static void 1233835Sahrens copies_changed_cb(void *arg, uint64_t newval) 1243835Sahrens { 1253835Sahrens objset_impl_t *osi = arg; 1263835Sahrens 1273835Sahrens /* 1283835Sahrens * Inheritance and range checking should have been done by now. 1293835Sahrens */ 1303835Sahrens ASSERT(newval > 0); 1313835Sahrens ASSERT(newval <= spa_max_replication(osi->os_spa)); 1323835Sahrens 1333835Sahrens osi->os_copies = newval; 1343835Sahrens } 1353835Sahrens 136789Sahrens void 137789Sahrens dmu_objset_byteswap(void *buf, size_t size) 138789Sahrens { 139789Sahrens objset_phys_t *osp = buf; 140789Sahrens 141789Sahrens ASSERT(size == sizeof (objset_phys_t)); 142789Sahrens dnode_byteswap(&osp->os_meta_dnode); 143789Sahrens byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t)); 144789Sahrens osp->os_type = BSWAP_64(osp->os_type); 145789Sahrens } 146789Sahrens 1471544Seschrock int 1481544Seschrock dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, 1491544Seschrock objset_impl_t **osip) 150789Sahrens { 1514787Sahrens objset_impl_t *osi; 152789Sahrens int i, err, checksum; 153789Sahrens 1544787Sahrens ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock)); 1554787Sahrens 156789Sahrens osi = kmem_zalloc(sizeof (objset_impl_t), KM_SLEEP); 157789Sahrens osi->os.os = osi; 158789Sahrens osi->os_dsl_dataset = ds; 159789Sahrens osi->os_spa = spa; 1603547Smaybee osi->os_rootbp = bp; 1613547Smaybee if (!BP_IS_HOLE(osi->os_rootbp)) { 1622391Smaybee uint32_t aflags = ARC_WAIT; 1631544Seschrock zbookmark_t zb; 1641544Seschrock zb.zb_objset = ds ? ds->ds_object : 0; 1651544Seschrock zb.zb_object = 0; 1661544Seschrock zb.zb_level = -1; 1671544Seschrock zb.zb_blkid = 0; 1681544Seschrock 1693547Smaybee dprintf_bp(osi->os_rootbp, "reading %s", ""); 1703547Smaybee err = arc_read(NULL, spa, osi->os_rootbp, 171789Sahrens dmu_ot[DMU_OT_OBJSET].ot_byteswap, 1723547Smaybee arc_getbuf_func, &osi->os_phys_buf, 1732391Smaybee ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb); 1741544Seschrock if (err) { 1751544Seschrock kmem_free(osi, sizeof (objset_impl_t)); 1761544Seschrock return (err); 1771544Seschrock } 1783547Smaybee osi->os_phys = osi->os_phys_buf->b_data; 1795147Srm160521 if (ds == NULL || dsl_dataset_is_snapshot(ds) == 0) 1805147Srm160521 arc_release(osi->os_phys_buf, &osi->os_phys_buf); 181789Sahrens } else { 1823547Smaybee osi->os_phys_buf = arc_buf_alloc(spa, sizeof (objset_phys_t), 1833547Smaybee &osi->os_phys_buf, ARC_BUFC_METADATA); 1843547Smaybee osi->os_phys = osi->os_phys_buf->b_data; 185789Sahrens bzero(osi->os_phys, sizeof (objset_phys_t)); 186789Sahrens } 187789Sahrens 188789Sahrens /* 189789Sahrens * Note: the changed_cb will be called once before the register 190789Sahrens * func returns, thus changing the checksum/compression from the 1912082Seschrock * default (fletcher2/off). Snapshots don't need to know, and 1922082Seschrock * registering would complicate clone promotion. 193789Sahrens */ 1942082Seschrock if (ds && ds->ds_phys->ds_num_children == 0) { 195789Sahrens err = dsl_prop_register(ds, "checksum", 196789Sahrens checksum_changed_cb, osi); 1971544Seschrock if (err == 0) 1981544Seschrock err = dsl_prop_register(ds, "compression", 1991544Seschrock compression_changed_cb, osi); 2003835Sahrens if (err == 0) 2013835Sahrens err = dsl_prop_register(ds, "copies", 2023835Sahrens copies_changed_cb, osi); 2031544Seschrock if (err) { 2043547Smaybee VERIFY(arc_buf_remove_ref(osi->os_phys_buf, 2053547Smaybee &osi->os_phys_buf) == 1); 2061544Seschrock kmem_free(osi, sizeof (objset_impl_t)); 2071544Seschrock return (err); 2081544Seschrock } 2092082Seschrock } else if (ds == NULL) { 210789Sahrens /* It's the meta-objset. */ 211789Sahrens osi->os_checksum = ZIO_CHECKSUM_FLETCHER_4; 2121544Seschrock osi->os_compress = ZIO_COMPRESS_LZJB; 2133835Sahrens osi->os_copies = spa_max_replication(spa); 214789Sahrens } 215789Sahrens 2161544Seschrock osi->os_zil = zil_alloc(&osi->os, &osi->os_phys->os_zil_header); 2171544Seschrock 218789Sahrens /* 219789Sahrens * Metadata always gets compressed and checksummed. 220789Sahrens * If the data checksum is multi-bit correctable, and it's not 221789Sahrens * a ZBT-style checksum, then it's suitable for metadata as well. 222789Sahrens * Otherwise, the metadata checksum defaults to fletcher4. 223789Sahrens */ 224789Sahrens checksum = osi->os_checksum; 225789Sahrens 226789Sahrens if (zio_checksum_table[checksum].ci_correctable && 227789Sahrens !zio_checksum_table[checksum].ci_zbt) 228789Sahrens osi->os_md_checksum = checksum; 229789Sahrens else 230789Sahrens osi->os_md_checksum = ZIO_CHECKSUM_FLETCHER_4; 2311544Seschrock osi->os_md_compress = ZIO_COMPRESS_LZJB; 232789Sahrens 233789Sahrens for (i = 0; i < TXG_SIZE; i++) { 234789Sahrens list_create(&osi->os_dirty_dnodes[i], sizeof (dnode_t), 235789Sahrens offsetof(dnode_t, dn_dirty_link[i])); 236789Sahrens list_create(&osi->os_free_dnodes[i], sizeof (dnode_t), 237789Sahrens offsetof(dnode_t, dn_dirty_link[i])); 238789Sahrens } 239789Sahrens list_create(&osi->os_dnodes, sizeof (dnode_t), 240789Sahrens offsetof(dnode_t, dn_link)); 241789Sahrens list_create(&osi->os_downgraded_dbufs, sizeof (dmu_buf_impl_t), 242789Sahrens offsetof(dmu_buf_impl_t, db_link)); 243789Sahrens 2442856Snd150628 mutex_init(&osi->os_lock, NULL, MUTEX_DEFAULT, NULL); 2452856Snd150628 mutex_init(&osi->os_obj_lock, NULL, MUTEX_DEFAULT, NULL); 246*5326Sek110237 mutex_init(&osi->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL); 2472856Snd150628 248789Sahrens osi->os_meta_dnode = dnode_special_open(osi, 249789Sahrens &osi->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT); 250789Sahrens 2514787Sahrens /* 2524787Sahrens * We should be the only thread trying to do this because we 2534787Sahrens * have ds_opening_lock 2544787Sahrens */ 2554787Sahrens if (ds) { 2564787Sahrens VERIFY(NULL == dsl_dataset_set_user_ptr(ds, osi, 2574787Sahrens dmu_objset_evict)); 258789Sahrens } 259789Sahrens 2601544Seschrock *osip = osi; 2611544Seschrock return (0); 262789Sahrens } 263789Sahrens 264789Sahrens /* called from zpl */ 265789Sahrens int 266789Sahrens dmu_objset_open(const char *name, dmu_objset_type_t type, int mode, 267789Sahrens objset_t **osp) 268789Sahrens { 269*5326Sek110237 objset_t *os; 270789Sahrens dsl_dataset_t *ds; 271*5326Sek110237 objset_impl_t *osi; 272789Sahrens int err; 273789Sahrens 274789Sahrens os = kmem_alloc(sizeof (objset_t), KM_SLEEP); 275789Sahrens err = dsl_dataset_open(name, mode, os, &ds); 276789Sahrens if (err) { 277789Sahrens kmem_free(os, sizeof (objset_t)); 278789Sahrens return (err); 279789Sahrens } 280789Sahrens 2814787Sahrens mutex_enter(&ds->ds_opening_lock); 282789Sahrens osi = dsl_dataset_get_user_ptr(ds); 283789Sahrens if (osi == NULL) { 2841544Seschrock err = dmu_objset_open_impl(dsl_dataset_get_spa(ds), 2853547Smaybee ds, &ds->ds_phys->ds_bp, &osi); 2861544Seschrock if (err) { 2871544Seschrock dsl_dataset_close(ds, mode, os); 2881544Seschrock kmem_free(os, sizeof (objset_t)); 2891544Seschrock return (err); 2901544Seschrock } 291789Sahrens } 2924787Sahrens mutex_exit(&ds->ds_opening_lock); 293789Sahrens 294789Sahrens os->os = osi; 295789Sahrens os->os_mode = mode; 296789Sahrens 297789Sahrens if (type != DMU_OST_ANY && type != os->os->os_phys->os_type) { 298789Sahrens dmu_objset_close(os); 299789Sahrens return (EINVAL); 300789Sahrens } 301789Sahrens *osp = os; 302789Sahrens return (0); 303789Sahrens } 304789Sahrens 305789Sahrens void 306789Sahrens dmu_objset_close(objset_t *os) 307789Sahrens { 308789Sahrens dsl_dataset_close(os->os->os_dsl_dataset, os->os_mode, os); 309789Sahrens kmem_free(os, sizeof (objset_t)); 310789Sahrens } 311789Sahrens 3121646Sperrin int 3134944Smaybee dmu_objset_evict_dbufs(objset_t *os) 3141544Seschrock { 3151544Seschrock objset_impl_t *osi = os->os; 3161544Seschrock dnode_t *dn; 3171596Sahrens 3181596Sahrens mutex_enter(&osi->os_lock); 3191596Sahrens 3201596Sahrens /* process the mdn last, since the other dnodes have holds on it */ 3211596Sahrens list_remove(&osi->os_dnodes, osi->os_meta_dnode); 3221596Sahrens list_insert_tail(&osi->os_dnodes, osi->os_meta_dnode); 3231544Seschrock 3241544Seschrock /* 3251596Sahrens * Find the first dnode with holds. We have to do this dance 3261596Sahrens * because dnode_add_ref() only works if you already have a 3271596Sahrens * hold. If there are no holds then it has no dbufs so OK to 3281596Sahrens * skip. 3291544Seschrock */ 3301596Sahrens for (dn = list_head(&osi->os_dnodes); 3314944Smaybee dn && !dnode_add_ref(dn, FTAG); 3321596Sahrens dn = list_next(&osi->os_dnodes, dn)) 3331596Sahrens continue; 3341596Sahrens 3351596Sahrens while (dn) { 3361596Sahrens dnode_t *next_dn = dn; 3371596Sahrens 3381596Sahrens do { 3391596Sahrens next_dn = list_next(&osi->os_dnodes, next_dn); 3404944Smaybee } while (next_dn && !dnode_add_ref(next_dn, FTAG)); 3411596Sahrens 3421596Sahrens mutex_exit(&osi->os_lock); 3434944Smaybee dnode_evict_dbufs(dn); 3441596Sahrens dnode_rele(dn, FTAG); 3451596Sahrens mutex_enter(&osi->os_lock); 3461596Sahrens dn = next_dn; 3471544Seschrock } 3481544Seschrock mutex_exit(&osi->os_lock); 3494944Smaybee return (list_head(&osi->os_dnodes) != osi->os_meta_dnode); 3501544Seschrock } 3511544Seschrock 3521544Seschrock void 353789Sahrens dmu_objset_evict(dsl_dataset_t *ds, void *arg) 354789Sahrens { 355789Sahrens objset_impl_t *osi = arg; 3561544Seschrock objset_t os; 3572082Seschrock int i; 358789Sahrens 359789Sahrens for (i = 0; i < TXG_SIZE; i++) { 360789Sahrens ASSERT(list_head(&osi->os_dirty_dnodes[i]) == NULL); 361789Sahrens ASSERT(list_head(&osi->os_free_dnodes[i]) == NULL); 362789Sahrens } 363789Sahrens 3642082Seschrock if (ds && ds->ds_phys->ds_num_children == 0) { 3652082Seschrock VERIFY(0 == dsl_prop_unregister(ds, "checksum", 3662082Seschrock checksum_changed_cb, osi)); 3672082Seschrock VERIFY(0 == dsl_prop_unregister(ds, "compression", 3682082Seschrock compression_changed_cb, osi)); 3693835Sahrens VERIFY(0 == dsl_prop_unregister(ds, "copies", 3703835Sahrens copies_changed_cb, osi)); 371789Sahrens } 372789Sahrens 3731544Seschrock /* 3741544Seschrock * We should need only a single pass over the dnode list, since 3751544Seschrock * nothing can be added to the list at this point. 3761544Seschrock */ 3771544Seschrock os.os = osi; 3784944Smaybee (void) dmu_objset_evict_dbufs(&os); 3791544Seschrock 380789Sahrens ASSERT3P(list_head(&osi->os_dnodes), ==, osi->os_meta_dnode); 381789Sahrens ASSERT3P(list_tail(&osi->os_dnodes), ==, osi->os_meta_dnode); 382789Sahrens ASSERT3P(list_head(&osi->os_meta_dnode->dn_dbufs), ==, NULL); 383789Sahrens 384789Sahrens dnode_special_close(osi->os_meta_dnode); 385789Sahrens zil_free(osi->os_zil); 386789Sahrens 3873547Smaybee VERIFY(arc_buf_remove_ref(osi->os_phys_buf, &osi->os_phys_buf) == 1); 3882856Snd150628 mutex_destroy(&osi->os_lock); 3892856Snd150628 mutex_destroy(&osi->os_obj_lock); 390*5326Sek110237 mutex_destroy(&osi->os_user_ptr_lock); 391789Sahrens kmem_free(osi, sizeof (objset_impl_t)); 392789Sahrens } 393789Sahrens 394789Sahrens /* called from dsl for meta-objset */ 395789Sahrens objset_impl_t * 3963547Smaybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, 3973547Smaybee dmu_objset_type_t type, dmu_tx_t *tx) 398789Sahrens { 399789Sahrens objset_impl_t *osi; 400789Sahrens dnode_t *mdn; 401789Sahrens 402789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 4034787Sahrens if (ds) 4044787Sahrens mutex_enter(&ds->ds_opening_lock); 4053547Smaybee VERIFY(0 == dmu_objset_open_impl(spa, ds, bp, &osi)); 4064787Sahrens if (ds) 4074787Sahrens mutex_exit(&ds->ds_opening_lock); 408789Sahrens mdn = osi->os_meta_dnode; 409789Sahrens 410789Sahrens dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT, 411789Sahrens DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx); 412789Sahrens 413789Sahrens /* 414789Sahrens * We don't want to have to increase the meta-dnode's nlevels 415789Sahrens * later, because then we could do it in quescing context while 416789Sahrens * we are also accessing it in open context. 417789Sahrens * 418789Sahrens * This precaution is not necessary for the MOS (ds == NULL), 419789Sahrens * because the MOS is only updated in syncing context. 420789Sahrens * This is most fortunate: the MOS is the only objset that 421789Sahrens * needs to be synced multiple times as spa_sync() iterates 422789Sahrens * to convergence, so minimizing its dn_nlevels matters. 423789Sahrens */ 4241544Seschrock if (ds != NULL) { 4251544Seschrock int levels = 1; 4261544Seschrock 4271544Seschrock /* 4281544Seschrock * Determine the number of levels necessary for the meta-dnode 4291544Seschrock * to contain DN_MAX_OBJECT dnodes. 4301544Seschrock */ 4311544Seschrock while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift + 4321544Seschrock (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) < 4331544Seschrock DN_MAX_OBJECT * sizeof (dnode_phys_t)) 4341544Seschrock levels++; 4351544Seschrock 436789Sahrens mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] = 4371544Seschrock mdn->dn_nlevels = levels; 4381544Seschrock } 439789Sahrens 440789Sahrens ASSERT(type != DMU_OST_NONE); 441789Sahrens ASSERT(type != DMU_OST_ANY); 442789Sahrens ASSERT(type < DMU_OST_NUMTYPES); 443789Sahrens osi->os_phys->os_type = type; 444789Sahrens 445789Sahrens dsl_dataset_dirty(ds, tx); 446789Sahrens 447789Sahrens return (osi); 448789Sahrens } 449789Sahrens 450789Sahrens struct oscarg { 4514543Smarks void (*userfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx); 452789Sahrens void *userarg; 453789Sahrens dsl_dataset_t *clone_parent; 454789Sahrens const char *lastname; 455789Sahrens dmu_objset_type_t type; 456789Sahrens }; 457789Sahrens 4584543Smarks /*ARGSUSED*/ 459789Sahrens static int 4602199Sahrens dmu_objset_create_check(void *arg1, void *arg2, dmu_tx_t *tx) 461789Sahrens { 4622199Sahrens dsl_dir_t *dd = arg1; 4632199Sahrens struct oscarg *oa = arg2; 4642199Sahrens objset_t *mos = dd->dd_pool->dp_meta_objset; 4652199Sahrens int err; 4662199Sahrens uint64_t ddobj; 4672199Sahrens 4682199Sahrens err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj, 4692199Sahrens oa->lastname, sizeof (uint64_t), 1, &ddobj); 4702199Sahrens if (err != ENOENT) 4712199Sahrens return (err ? err : EEXIST); 4722199Sahrens 4732199Sahrens if (oa->clone_parent != NULL) { 4742199Sahrens /* 4752199Sahrens * You can't clone across pools. 4762199Sahrens */ 4772199Sahrens if (oa->clone_parent->ds_dir->dd_pool != dd->dd_pool) 4782199Sahrens return (EXDEV); 4792199Sahrens 4802199Sahrens /* 4812199Sahrens * You can only clone snapshots, not the head datasets. 4822199Sahrens */ 4832199Sahrens if (oa->clone_parent->ds_phys->ds_num_children == 0) 4842199Sahrens return (EINVAL); 4852199Sahrens } 4864543Smarks 4872199Sahrens return (0); 4882199Sahrens } 4892199Sahrens 4902199Sahrens static void 4914543Smarks dmu_objset_create_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 4922199Sahrens { 4932199Sahrens dsl_dir_t *dd = arg1; 4942199Sahrens struct oscarg *oa = arg2; 495789Sahrens dsl_dataset_t *ds; 4963547Smaybee blkptr_t *bp; 4972199Sahrens uint64_t dsobj; 498789Sahrens 499789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 500789Sahrens 5012199Sahrens dsobj = dsl_dataset_create_sync(dd, oa->lastname, 502789Sahrens oa->clone_parent, tx); 503789Sahrens 5042199Sahrens VERIFY(0 == dsl_dataset_open_obj(dd->dd_pool, dsobj, NULL, 5051544Seschrock DS_MODE_STANDARD | DS_MODE_READONLY, FTAG, &ds)); 5063547Smaybee bp = dsl_dataset_get_blkptr(ds); 5073547Smaybee if (BP_IS_HOLE(bp)) { 508789Sahrens objset_impl_t *osi; 509789Sahrens 510789Sahrens /* This is an empty dmu_objset; not a clone. */ 511789Sahrens osi = dmu_objset_create_impl(dsl_dataset_get_spa(ds), 5123547Smaybee ds, bp, oa->type, tx); 513789Sahrens 514789Sahrens if (oa->userfunc) 5154543Smarks oa->userfunc(&osi->os, oa->userarg, cr, tx); 516789Sahrens } 5174543Smarks 5184543Smarks /* 5194543Smarks * Create create time permission if any? 5204543Smarks */ 5214543Smarks dsl_deleg_set_create_perms(ds->ds_dir, tx, cr); 5224543Smarks 5234543Smarks spa_history_internal_log(LOG_DS_CREATE, dd->dd_pool->dp_spa, 5244543Smarks tx, cr, "dataset = %llu", dsobj); 5254543Smarks 526789Sahrens dsl_dataset_close(ds, DS_MODE_STANDARD | DS_MODE_READONLY, FTAG); 527789Sahrens } 528789Sahrens 529789Sahrens int 530789Sahrens dmu_objset_create(const char *name, dmu_objset_type_t type, 531789Sahrens objset_t *clone_parent, 5324543Smarks void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg) 533789Sahrens { 5342199Sahrens dsl_dir_t *pdd; 535789Sahrens const char *tail; 536789Sahrens int err = 0; 5372199Sahrens struct oscarg oa = { 0 }; 538789Sahrens 5392199Sahrens ASSERT(strchr(name, '@') == NULL); 5402199Sahrens err = dsl_dir_open(name, FTAG, &pdd, &tail); 5411544Seschrock if (err) 5421544Seschrock return (err); 543789Sahrens if (tail == NULL) { 5442199Sahrens dsl_dir_close(pdd, FTAG); 545789Sahrens return (EEXIST); 546789Sahrens } 547789Sahrens 548789Sahrens dprintf("name=%s\n", name); 549789Sahrens 5502199Sahrens oa.userfunc = func; 5512199Sahrens oa.userarg = arg; 5522199Sahrens oa.lastname = tail; 5532199Sahrens oa.type = type; 5544543Smarks 5552199Sahrens if (clone_parent != NULL) { 556789Sahrens /* 5572199Sahrens * You can't clone to a different type. 558789Sahrens */ 5592199Sahrens if (clone_parent->os->os_phys->os_type != type) { 5602199Sahrens dsl_dir_close(pdd, FTAG); 5612199Sahrens return (EINVAL); 562789Sahrens } 5632199Sahrens oa.clone_parent = clone_parent->os->os_dsl_dataset; 564789Sahrens } 5652199Sahrens err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check, 5662199Sahrens dmu_objset_create_sync, pdd, &oa, 5); 5672199Sahrens dsl_dir_close(pdd, FTAG); 568789Sahrens return (err); 569789Sahrens } 570789Sahrens 571789Sahrens int 572789Sahrens dmu_objset_destroy(const char *name) 573789Sahrens { 574789Sahrens objset_t *os; 575789Sahrens int error; 576789Sahrens 577789Sahrens /* 578789Sahrens * If it looks like we'll be able to destroy it, and there's 579789Sahrens * an unplayed replay log sitting around, destroy the log. 580789Sahrens * It would be nicer to do this in dsl_dataset_destroy_sync(), 581789Sahrens * but the replay log objset is modified in open context. 582789Sahrens */ 583789Sahrens error = dmu_objset_open(name, DMU_OST_ANY, DS_MODE_EXCLUSIVE, &os); 584789Sahrens if (error == 0) { 5851807Sbonwick zil_destroy(dmu_objset_zil(os), B_FALSE); 586789Sahrens dmu_objset_close(os); 587789Sahrens } 588789Sahrens 589789Sahrens return (dsl_dataset_destroy(name)); 590789Sahrens } 591789Sahrens 592789Sahrens int 593789Sahrens dmu_objset_rollback(const char *name) 594789Sahrens { 595789Sahrens int err; 596789Sahrens objset_t *os; 597789Sahrens 5982199Sahrens err = dmu_objset_open(name, DMU_OST_ANY, 5992199Sahrens DS_MODE_EXCLUSIVE | DS_MODE_INCONSISTENT, &os); 6004935Sperrin if (err) 6014935Sperrin return (err); 6024935Sperrin 6034935Sperrin /* XXX uncache everything? */ 6044935Sperrin err = dsl_dataset_rollback(os->os->os_dsl_dataset); 6054935Sperrin 6064935Sperrin dmu_objset_close(os); 607789Sahrens return (err); 608789Sahrens } 609789Sahrens 6102199Sahrens struct snaparg { 6112199Sahrens dsl_sync_task_group_t *dstg; 6122199Sahrens char *snapname; 6132199Sahrens char failed[MAXPATHLEN]; 6144543Smarks boolean_t checkperms; 6152199Sahrens }; 6162199Sahrens 6172199Sahrens static int 6182199Sahrens dmu_objset_snapshot_one(char *name, void *arg) 6192199Sahrens { 6202199Sahrens struct snaparg *sn = arg; 6212199Sahrens objset_t *os; 6223637Srm160521 dmu_objset_stats_t stat; 6232199Sahrens int err; 6242199Sahrens 6252199Sahrens (void) strcpy(sn->failed, name); 6262199Sahrens 6274543Smarks /* 6284543Smarks * Check permissions only when requested. This only applies when 6294543Smarks * doing a recursive snapshot. The permission checks for the starting 6304543Smarks * dataset have already been performed in zfs_secpolicy_snapshot() 6314543Smarks */ 6324543Smarks if (sn->checkperms == B_TRUE && 6334543Smarks (err = zfs_secpolicy_snapshot_perms(name, CRED()))) 6344543Smarks return (err); 6354543Smarks 6362199Sahrens err = dmu_objset_open(name, DMU_OST_ANY, DS_MODE_STANDARD, &os); 6372199Sahrens if (err != 0) 6382199Sahrens return (err); 6392199Sahrens 6402199Sahrens /* 6413637Srm160521 * If the objset is in an inconsistent state, return busy. 6423637Srm160521 */ 6433637Srm160521 dmu_objset_fast_stat(os, &stat); 6443637Srm160521 if (stat.dds_inconsistent) { 6453637Srm160521 dmu_objset_close(os); 6463637Srm160521 return (EBUSY); 6473637Srm160521 } 6483637Srm160521 6493637Srm160521 /* 6502199Sahrens * NB: we need to wait for all in-flight changes to get to disk, 6512199Sahrens * so that we snapshot those changes. zil_suspend does this as 6522199Sahrens * a side effect. 6532199Sahrens */ 6542199Sahrens err = zil_suspend(dmu_objset_zil(os)); 6552199Sahrens if (err == 0) { 6562199Sahrens dsl_sync_task_create(sn->dstg, dsl_dataset_snapshot_check, 6572199Sahrens dsl_dataset_snapshot_sync, os, sn->snapname, 3); 6583637Srm160521 } else { 6593637Srm160521 dmu_objset_close(os); 6602199Sahrens } 6613637Srm160521 6622199Sahrens return (err); 6632199Sahrens } 6642199Sahrens 6652199Sahrens int 6662199Sahrens dmu_objset_snapshot(char *fsname, char *snapname, boolean_t recursive) 6672199Sahrens { 6682199Sahrens dsl_sync_task_t *dst; 6692199Sahrens struct snaparg sn = { 0 }; 6702199Sahrens spa_t *spa; 6712199Sahrens int err; 6722199Sahrens 6732199Sahrens (void) strcpy(sn.failed, fsname); 6742199Sahrens 6754603Sahrens err = spa_open(fsname, &spa, FTAG); 6762199Sahrens if (err) 6772199Sahrens return (err); 6782199Sahrens 6792199Sahrens sn.dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 6802199Sahrens sn.snapname = snapname; 6812199Sahrens 6822417Sahrens if (recursive) { 6834543Smarks sn.checkperms = B_TRUE; 6842417Sahrens err = dmu_objset_find(fsname, 6852417Sahrens dmu_objset_snapshot_one, &sn, DS_FIND_CHILDREN); 6862417Sahrens } else { 6874543Smarks sn.checkperms = B_FALSE; 6882199Sahrens err = dmu_objset_snapshot_one(fsname, &sn); 6892417Sahrens } 6902199Sahrens 6912199Sahrens if (err) 6922199Sahrens goto out; 6932199Sahrens 6942199Sahrens err = dsl_sync_task_group_wait(sn.dstg); 6952199Sahrens 6962199Sahrens for (dst = list_head(&sn.dstg->dstg_tasks); dst; 6972199Sahrens dst = list_next(&sn.dstg->dstg_tasks, dst)) { 6982199Sahrens objset_t *os = dst->dst_arg1; 6992199Sahrens if (dst->dst_err) 7002199Sahrens dmu_objset_name(os, sn.failed); 7012199Sahrens zil_resume(dmu_objset_zil(os)); 7022199Sahrens dmu_objset_close(os); 7032199Sahrens } 7042199Sahrens out: 7052199Sahrens if (err) 7062199Sahrens (void) strcpy(fsname, sn.failed); 7072199Sahrens dsl_sync_task_group_destroy(sn.dstg); 7082199Sahrens spa_close(spa, FTAG); 7092199Sahrens return (err); 7102199Sahrens } 7112199Sahrens 712789Sahrens static void 7133547Smaybee dmu_objset_sync_dnodes(list_t *list, dmu_tx_t *tx) 714789Sahrens { 7153547Smaybee dnode_t *dn; 716789Sahrens 7173547Smaybee while (dn = list_head(list)) { 7183547Smaybee ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT); 7193547Smaybee ASSERT(dn->dn_dbuf->db_data_pending); 7203547Smaybee /* 7213547Smaybee * Initialize dn_zio outside dnode_sync() 7223547Smaybee * to accomodate meta-dnode 7233547Smaybee */ 7243547Smaybee dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio; 7253547Smaybee ASSERT(dn->dn_zio); 726789Sahrens 7273547Smaybee ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS); 7283547Smaybee list_remove(list, dn); 7293547Smaybee dnode_sync(dn, tx); 7303547Smaybee } 7313547Smaybee } 7322981Sahrens 7333547Smaybee /* ARGSUSED */ 7343547Smaybee static void 7353547Smaybee ready(zio_t *zio, arc_buf_t *abuf, void *arg) 7363547Smaybee { 7373547Smaybee objset_impl_t *os = arg; 7383547Smaybee blkptr_t *bp = os->os_rootbp; 7393547Smaybee dnode_phys_t *dnp = &os->os_phys->os_meta_dnode; 7403547Smaybee int i; 7412981Sahrens 7423547Smaybee /* 7433547Smaybee * Update rootbp fill count. 7443547Smaybee */ 7453547Smaybee bp->blk_fill = 1; /* count the meta-dnode */ 7463547Smaybee for (i = 0; i < dnp->dn_nblkptr; i++) 7473547Smaybee bp->blk_fill += dnp->dn_blkptr[i].blk_fill; 748789Sahrens } 749789Sahrens 750789Sahrens /* ARGSUSED */ 751789Sahrens static void 752789Sahrens killer(zio_t *zio, arc_buf_t *abuf, void *arg) 753789Sahrens { 754789Sahrens objset_impl_t *os = arg; 755789Sahrens 756789Sahrens ASSERT3U(zio->io_error, ==, 0); 757789Sahrens 758789Sahrens BP_SET_TYPE(zio->io_bp, DMU_OT_OBJSET); 759789Sahrens BP_SET_LEVEL(zio->io_bp, 0); 760789Sahrens 761789Sahrens if (!DVA_EQUAL(BP_IDENTITY(zio->io_bp), 762789Sahrens BP_IDENTITY(&zio->io_bp_orig))) { 7633547Smaybee if (zio->io_bp_orig.blk_birth == os->os_synctx->tx_txg) 7643547Smaybee dsl_dataset_block_kill(os->os_dsl_dataset, 7653547Smaybee &zio->io_bp_orig, NULL, os->os_synctx); 766789Sahrens dsl_dataset_block_born(os->os_dsl_dataset, zio->io_bp, 767789Sahrens os->os_synctx); 768789Sahrens } 7693547Smaybee arc_release(os->os_phys_buf, &os->os_phys_buf); 770789Sahrens } 771789Sahrens 772789Sahrens /* called from dsl */ 773789Sahrens void 7743547Smaybee dmu_objset_sync(objset_impl_t *os, zio_t *pio, dmu_tx_t *tx) 775789Sahrens { 776789Sahrens int txgoff; 7771544Seschrock zbookmark_t zb; 7783547Smaybee zio_t *zio; 7793547Smaybee list_t *list; 7803547Smaybee dbuf_dirty_record_t *dr; 7813547Smaybee 7823547Smaybee dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg); 783789Sahrens 784789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 785789Sahrens /* XXX the write_done callback should really give us the tx... */ 786789Sahrens os->os_synctx = tx; 787789Sahrens 7883882Sahrens if (os->os_dsl_dataset == NULL) { 7893882Sahrens /* 7903882Sahrens * This is the MOS. If we have upgraded, 7913882Sahrens * spa_max_replication() could change, so reset 7923882Sahrens * os_copies here. 7933882Sahrens */ 7943882Sahrens os->os_copies = spa_max_replication(os->os_spa); 7953882Sahrens } 7963882Sahrens 7973547Smaybee /* 7983547Smaybee * Create the root block IO 7993547Smaybee */ 8003547Smaybee zb.zb_objset = os->os_dsl_dataset ? os->os_dsl_dataset->ds_object : 0; 8013547Smaybee zb.zb_object = 0; 8023547Smaybee zb.zb_level = -1; 8033547Smaybee zb.zb_blkid = 0; 8044787Sahrens if (BP_IS_OLDER(os->os_rootbp, tx->tx_txg)) { 8053547Smaybee dsl_dataset_block_kill(os->os_dsl_dataset, 8063547Smaybee os->os_rootbp, pio, tx); 8074787Sahrens } 8083547Smaybee zio = arc_write(pio, os->os_spa, os->os_md_checksum, 8093547Smaybee os->os_md_compress, 8103835Sahrens dmu_get_replication_level(os, &zb, DMU_OT_OBJSET), 8113547Smaybee tx->tx_txg, os->os_rootbp, os->os_phys_buf, ready, killer, os, 8124634Sek110237 ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED | ZIO_FLAG_METADATA, 8134634Sek110237 &zb); 8143547Smaybee 8153547Smaybee /* 8163547Smaybee * Sync meta-dnode - the parent IO for the sync is the root block 8173547Smaybee */ 8183547Smaybee os->os_meta_dnode->dn_zio = zio; 8193547Smaybee dnode_sync(os->os_meta_dnode, tx); 820789Sahrens 821789Sahrens txgoff = tx->tx_txg & TXG_MASK; 822789Sahrens 8233547Smaybee dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], tx); 8243547Smaybee dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], tx); 825789Sahrens 8263547Smaybee list = &os->os_meta_dnode->dn_dirty_records[txgoff]; 8273547Smaybee while (dr = list_head(list)) { 8283547Smaybee ASSERT(dr->dr_dbuf->db_level == 0); 8293547Smaybee list_remove(list, dr); 8303547Smaybee if (dr->dr_zio) 8313547Smaybee zio_nowait(dr->dr_zio); 8323547Smaybee } 833789Sahrens /* 834789Sahrens * Free intent log blocks up to this tx. 835789Sahrens */ 836789Sahrens zil_sync(os->os_zil, tx); 8373547Smaybee zio_nowait(zio); 838789Sahrens } 839789Sahrens 840789Sahrens void 8412885Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp, 8422885Sahrens uint64_t *usedobjsp, uint64_t *availobjsp) 8432885Sahrens { 8442885Sahrens dsl_dataset_space(os->os->os_dsl_dataset, refdbytesp, availbytesp, 8452885Sahrens usedobjsp, availobjsp); 8462885Sahrens } 8472885Sahrens 8482885Sahrens uint64_t 8492885Sahrens dmu_objset_fsid_guid(objset_t *os) 8502885Sahrens { 8512885Sahrens return (dsl_dataset_fsid_guid(os->os->os_dsl_dataset)); 8522885Sahrens } 8532885Sahrens 8542885Sahrens void 8552885Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat) 856789Sahrens { 8572885Sahrens stat->dds_type = os->os->os_phys->os_type; 8582885Sahrens if (os->os->os_dsl_dataset) 8592885Sahrens dsl_dataset_fast_stat(os->os->os_dsl_dataset, stat); 8602885Sahrens } 8612885Sahrens 8622885Sahrens void 8632885Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv) 8642885Sahrens { 8652885Sahrens ASSERT(os->os->os_dsl_dataset || 8662885Sahrens os->os->os_phys->os_type == DMU_OST_META); 8672885Sahrens 8682885Sahrens if (os->os->os_dsl_dataset != NULL) 8692885Sahrens dsl_dataset_stats(os->os->os_dsl_dataset, nv); 8702885Sahrens 8712885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE, 8722885Sahrens os->os->os_phys->os_type); 873789Sahrens } 874789Sahrens 875789Sahrens int 876789Sahrens dmu_objset_is_snapshot(objset_t *os) 877789Sahrens { 878789Sahrens if (os->os->os_dsl_dataset != NULL) 879789Sahrens return (dsl_dataset_is_snapshot(os->os->os_dsl_dataset)); 880789Sahrens else 881789Sahrens return (B_FALSE); 882789Sahrens } 883789Sahrens 884789Sahrens int 885789Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name, 886885Sahrens uint64_t *idp, uint64_t *offp) 887789Sahrens { 888789Sahrens dsl_dataset_t *ds = os->os->os_dsl_dataset; 889789Sahrens zap_cursor_t cursor; 890789Sahrens zap_attribute_t attr; 891789Sahrens 892789Sahrens if (ds->ds_phys->ds_snapnames_zapobj == 0) 893789Sahrens return (ENOENT); 894789Sahrens 895789Sahrens zap_cursor_init_serialized(&cursor, 896789Sahrens ds->ds_dir->dd_pool->dp_meta_objset, 897789Sahrens ds->ds_phys->ds_snapnames_zapobj, *offp); 898789Sahrens 899885Sahrens if (zap_cursor_retrieve(&cursor, &attr) != 0) { 900885Sahrens zap_cursor_fini(&cursor); 901885Sahrens return (ENOENT); 902885Sahrens } 903885Sahrens 904885Sahrens if (strlen(attr.za_name) + 1 > namelen) { 905885Sahrens zap_cursor_fini(&cursor); 906885Sahrens return (ENAMETOOLONG); 907885Sahrens } 908885Sahrens 909885Sahrens (void) strcpy(name, attr.za_name); 910885Sahrens if (idp) 911885Sahrens *idp = attr.za_first_integer; 912885Sahrens zap_cursor_advance(&cursor); 913885Sahrens *offp = zap_cursor_serialize(&cursor); 914885Sahrens zap_cursor_fini(&cursor); 915885Sahrens 916885Sahrens return (0); 917885Sahrens } 918885Sahrens 919885Sahrens int 920885Sahrens dmu_dir_list_next(objset_t *os, int namelen, char *name, 921885Sahrens uint64_t *idp, uint64_t *offp) 922885Sahrens { 923885Sahrens dsl_dir_t *dd = os->os->os_dsl_dataset->ds_dir; 924885Sahrens zap_cursor_t cursor; 925885Sahrens zap_attribute_t attr; 926885Sahrens 927885Sahrens /* there is no next dir on a snapshot! */ 928885Sahrens if (os->os->os_dsl_dataset->ds_object != 929885Sahrens dd->dd_phys->dd_head_dataset_obj) 930885Sahrens return (ENOENT); 931885Sahrens 932885Sahrens zap_cursor_init_serialized(&cursor, 933885Sahrens dd->dd_pool->dp_meta_objset, 934885Sahrens dd->dd_phys->dd_child_dir_zapobj, *offp); 935885Sahrens 936885Sahrens if (zap_cursor_retrieve(&cursor, &attr) != 0) { 937885Sahrens zap_cursor_fini(&cursor); 938885Sahrens return (ENOENT); 939885Sahrens } 940885Sahrens 941885Sahrens if (strlen(attr.za_name) + 1 > namelen) { 942885Sahrens zap_cursor_fini(&cursor); 943789Sahrens return (ENAMETOOLONG); 944885Sahrens } 945789Sahrens 946789Sahrens (void) strcpy(name, attr.za_name); 947885Sahrens if (idp) 948885Sahrens *idp = attr.za_first_integer; 949789Sahrens zap_cursor_advance(&cursor); 950789Sahrens *offp = zap_cursor_serialize(&cursor); 951885Sahrens zap_cursor_fini(&cursor); 952789Sahrens 953789Sahrens return (0); 954789Sahrens } 955789Sahrens 956789Sahrens /* 957789Sahrens * Find all objsets under name, and for each, call 'func(child_name, arg)'. 958789Sahrens */ 9592199Sahrens int 9602199Sahrens dmu_objset_find(char *name, int func(char *, void *), void *arg, int flags) 961789Sahrens { 962789Sahrens dsl_dir_t *dd; 963789Sahrens objset_t *os; 964789Sahrens uint64_t snapobj; 965789Sahrens zap_cursor_t zc; 9663978Smmusante zap_attribute_t *attr; 967789Sahrens char *child; 9681544Seschrock int do_self, err; 969789Sahrens 9701544Seschrock err = dsl_dir_open(name, FTAG, &dd, NULL); 9711544Seschrock if (err) 9722199Sahrens return (err); 973789Sahrens 9742199Sahrens /* NB: the $MOS dir doesn't have a head dataset */ 975789Sahrens do_self = (dd->dd_phys->dd_head_dataset_obj != 0); 9763978Smmusante attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); 977789Sahrens 978789Sahrens /* 979789Sahrens * Iterate over all children. 980789Sahrens */ 9812417Sahrens if (flags & DS_FIND_CHILDREN) { 9822417Sahrens for (zap_cursor_init(&zc, dd->dd_pool->dp_meta_objset, 9832417Sahrens dd->dd_phys->dd_child_dir_zapobj); 9843978Smmusante zap_cursor_retrieve(&zc, attr) == 0; 9852417Sahrens (void) zap_cursor_advance(&zc)) { 9863978Smmusante ASSERT(attr->za_integer_length == sizeof (uint64_t)); 9873978Smmusante ASSERT(attr->za_num_integers == 1); 988789Sahrens 9892417Sahrens /* 9902417Sahrens * No separating '/' because parent's name ends in /. 9912417Sahrens */ 9922417Sahrens child = kmem_alloc(MAXPATHLEN, KM_SLEEP); 9932417Sahrens /* XXX could probably just use name here */ 9942417Sahrens dsl_dir_name(dd, child); 9952417Sahrens (void) strcat(child, "/"); 9963978Smmusante (void) strcat(child, attr->za_name); 9972417Sahrens err = dmu_objset_find(child, func, arg, flags); 9982417Sahrens kmem_free(child, MAXPATHLEN); 9992417Sahrens if (err) 10002417Sahrens break; 10012417Sahrens } 10022417Sahrens zap_cursor_fini(&zc); 10032199Sahrens 10042417Sahrens if (err) { 10052417Sahrens dsl_dir_close(dd, FTAG); 10063978Smmusante kmem_free(attr, sizeof (zap_attribute_t)); 10072417Sahrens return (err); 10082417Sahrens } 1009789Sahrens } 1010789Sahrens 1011789Sahrens /* 1012789Sahrens * Iterate over all snapshots. 1013789Sahrens */ 1014789Sahrens if ((flags & DS_FIND_SNAPSHOTS) && 1015789Sahrens dmu_objset_open(name, DMU_OST_ANY, 1016789Sahrens DS_MODE_STANDARD | DS_MODE_READONLY, &os) == 0) { 1017789Sahrens 1018789Sahrens snapobj = os->os->os_dsl_dataset->ds_phys->ds_snapnames_zapobj; 1019789Sahrens dmu_objset_close(os); 1020789Sahrens 1021789Sahrens for (zap_cursor_init(&zc, dd->dd_pool->dp_meta_objset, snapobj); 10223978Smmusante zap_cursor_retrieve(&zc, attr) == 0; 1023789Sahrens (void) zap_cursor_advance(&zc)) { 10243978Smmusante ASSERT(attr->za_integer_length == sizeof (uint64_t)); 10253978Smmusante ASSERT(attr->za_num_integers == 1); 1026789Sahrens 1027789Sahrens child = kmem_alloc(MAXPATHLEN, KM_SLEEP); 1028789Sahrens /* XXX could probably just use name here */ 1029789Sahrens dsl_dir_name(dd, child); 1030789Sahrens (void) strcat(child, "@"); 10313978Smmusante (void) strcat(child, attr->za_name); 10322199Sahrens err = func(child, arg); 1033789Sahrens kmem_free(child, MAXPATHLEN); 10342199Sahrens if (err) 10352199Sahrens break; 1036789Sahrens } 1037885Sahrens zap_cursor_fini(&zc); 1038789Sahrens } 1039789Sahrens 1040789Sahrens dsl_dir_close(dd, FTAG); 10413978Smmusante kmem_free(attr, sizeof (zap_attribute_t)); 1042789Sahrens 10432199Sahrens if (err) 10442199Sahrens return (err); 10452199Sahrens 1046789Sahrens /* 1047789Sahrens * Apply to self if appropriate. 1048789Sahrens */ 1049789Sahrens if (do_self) 10502199Sahrens err = func(name, arg); 10512199Sahrens return (err); 1052789Sahrens } 1053*5326Sek110237 1054*5326Sek110237 void 1055*5326Sek110237 dmu_objset_set_user(objset_t *os, void *user_ptr) 1056*5326Sek110237 { 1057*5326Sek110237 ASSERT(MUTEX_HELD(&os->os->os_user_ptr_lock)); 1058*5326Sek110237 os->os->os_user_ptr = user_ptr; 1059*5326Sek110237 } 1060*5326Sek110237 1061*5326Sek110237 void * 1062*5326Sek110237 dmu_objset_get_user(objset_t *os) 1063*5326Sek110237 { 1064*5326Sek110237 ASSERT(MUTEX_HELD(&os->os->os_user_ptr_lock)); 1065*5326Sek110237 return (os->os->os_user_ptr); 1066*5326Sek110237 } 1067