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 /* 226174Sahrens * Copyright 2008 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); 2465326Sek110237 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 2645367Sahrens static int 2655367Sahrens dmu_objset_open_ds_os(dsl_dataset_t *ds, objset_t *os, dmu_objset_type_t type) 2665367Sahrens { 2675367Sahrens objset_impl_t *osi; 2685367Sahrens int err; 2695367Sahrens 2705367Sahrens mutex_enter(&ds->ds_opening_lock); 2715367Sahrens osi = dsl_dataset_get_user_ptr(ds); 2725367Sahrens if (osi == NULL) { 2735367Sahrens err = dmu_objset_open_impl(dsl_dataset_get_spa(ds), 2745367Sahrens ds, &ds->ds_phys->ds_bp, &osi); 2755367Sahrens if (err) 2765367Sahrens return (err); 2775367Sahrens } 2785367Sahrens mutex_exit(&ds->ds_opening_lock); 2795367Sahrens 2805367Sahrens os->os = osi; 2815367Sahrens os->os_mode = DS_MODE_NONE; 2825367Sahrens 2835367Sahrens if (type != DMU_OST_ANY && type != os->os->os_phys->os_type) 2845367Sahrens return (EINVAL); 2855367Sahrens return (0); 2865367Sahrens } 2875367Sahrens 2885367Sahrens int 2895367Sahrens dmu_objset_open_ds(dsl_dataset_t *ds, dmu_objset_type_t type, objset_t **osp) 2905367Sahrens { 2915367Sahrens objset_t *os; 2925367Sahrens int err; 2935367Sahrens 2945367Sahrens os = kmem_alloc(sizeof (objset_t), KM_SLEEP); 2955367Sahrens err = dmu_objset_open_ds_os(ds, os, type); 2965367Sahrens if (err) 2975367Sahrens kmem_free(os, sizeof (objset_t)); 2985367Sahrens else 2995367Sahrens *osp = os; 3005367Sahrens return (err); 3015367Sahrens } 3025367Sahrens 303789Sahrens /* called from zpl */ 304789Sahrens int 305789Sahrens dmu_objset_open(const char *name, dmu_objset_type_t type, int mode, 306789Sahrens objset_t **osp) 307789Sahrens { 3085326Sek110237 objset_t *os; 309789Sahrens dsl_dataset_t *ds; 310789Sahrens int err; 311789Sahrens 3125367Sahrens ASSERT(mode != DS_MODE_NONE); 3135367Sahrens 314789Sahrens os = kmem_alloc(sizeof (objset_t), KM_SLEEP); 315789Sahrens err = dsl_dataset_open(name, mode, os, &ds); 316789Sahrens if (err) { 317789Sahrens kmem_free(os, sizeof (objset_t)); 318789Sahrens return (err); 319789Sahrens } 320789Sahrens 3215367Sahrens err = dmu_objset_open_ds_os(ds, os, type); 3225367Sahrens os->os_mode = mode; 3235367Sahrens if (err) { 3245367Sahrens kmem_free(os, sizeof (objset_t)); 3255367Sahrens dsl_dataset_close(ds, mode, os); 3265367Sahrens } else { 3275367Sahrens *osp = os; 328789Sahrens } 3295367Sahrens return (err); 330789Sahrens } 331789Sahrens 332789Sahrens void 333789Sahrens dmu_objset_close(objset_t *os) 334789Sahrens { 3355367Sahrens if (os->os_mode != DS_MODE_NONE) 3365367Sahrens dsl_dataset_close(os->os->os_dsl_dataset, os->os_mode, os); 337789Sahrens kmem_free(os, sizeof (objset_t)); 338789Sahrens } 339789Sahrens 3401646Sperrin int 3414944Smaybee dmu_objset_evict_dbufs(objset_t *os) 3421544Seschrock { 3431544Seschrock objset_impl_t *osi = os->os; 3441544Seschrock dnode_t *dn; 3451596Sahrens 3461596Sahrens mutex_enter(&osi->os_lock); 3471596Sahrens 3481596Sahrens /* process the mdn last, since the other dnodes have holds on it */ 3491596Sahrens list_remove(&osi->os_dnodes, osi->os_meta_dnode); 3501596Sahrens list_insert_tail(&osi->os_dnodes, osi->os_meta_dnode); 3511544Seschrock 3521544Seschrock /* 3531596Sahrens * Find the first dnode with holds. We have to do this dance 3541596Sahrens * because dnode_add_ref() only works if you already have a 3551596Sahrens * hold. If there are no holds then it has no dbufs so OK to 3561596Sahrens * skip. 3571544Seschrock */ 3581596Sahrens for (dn = list_head(&osi->os_dnodes); 3594944Smaybee dn && !dnode_add_ref(dn, FTAG); 3601596Sahrens dn = list_next(&osi->os_dnodes, dn)) 3611596Sahrens continue; 3621596Sahrens 3631596Sahrens while (dn) { 3641596Sahrens dnode_t *next_dn = dn; 3651596Sahrens 3661596Sahrens do { 3671596Sahrens next_dn = list_next(&osi->os_dnodes, next_dn); 3684944Smaybee } while (next_dn && !dnode_add_ref(next_dn, FTAG)); 3691596Sahrens 3701596Sahrens mutex_exit(&osi->os_lock); 3714944Smaybee dnode_evict_dbufs(dn); 3721596Sahrens dnode_rele(dn, FTAG); 3731596Sahrens mutex_enter(&osi->os_lock); 3741596Sahrens dn = next_dn; 3751544Seschrock } 3761544Seschrock mutex_exit(&osi->os_lock); 3774944Smaybee return (list_head(&osi->os_dnodes) != osi->os_meta_dnode); 3781544Seschrock } 3791544Seschrock 3801544Seschrock void 381789Sahrens dmu_objset_evict(dsl_dataset_t *ds, void *arg) 382789Sahrens { 383789Sahrens objset_impl_t *osi = arg; 3841544Seschrock objset_t os; 3852082Seschrock int i; 386789Sahrens 387789Sahrens for (i = 0; i < TXG_SIZE; i++) { 388789Sahrens ASSERT(list_head(&osi->os_dirty_dnodes[i]) == NULL); 389789Sahrens ASSERT(list_head(&osi->os_free_dnodes[i]) == NULL); 390789Sahrens } 391789Sahrens 3922082Seschrock if (ds && ds->ds_phys->ds_num_children == 0) { 3932082Seschrock VERIFY(0 == dsl_prop_unregister(ds, "checksum", 3942082Seschrock checksum_changed_cb, osi)); 3952082Seschrock VERIFY(0 == dsl_prop_unregister(ds, "compression", 3962082Seschrock compression_changed_cb, osi)); 3973835Sahrens VERIFY(0 == dsl_prop_unregister(ds, "copies", 3983835Sahrens copies_changed_cb, osi)); 399789Sahrens } 400789Sahrens 4011544Seschrock /* 4021544Seschrock * We should need only a single pass over the dnode list, since 4031544Seschrock * nothing can be added to the list at this point. 4041544Seschrock */ 4051544Seschrock os.os = osi; 4064944Smaybee (void) dmu_objset_evict_dbufs(&os); 4071544Seschrock 408789Sahrens ASSERT3P(list_head(&osi->os_dnodes), ==, osi->os_meta_dnode); 409789Sahrens ASSERT3P(list_tail(&osi->os_dnodes), ==, osi->os_meta_dnode); 410789Sahrens ASSERT3P(list_head(&osi->os_meta_dnode->dn_dbufs), ==, NULL); 411789Sahrens 412789Sahrens dnode_special_close(osi->os_meta_dnode); 413789Sahrens zil_free(osi->os_zil); 414789Sahrens 4153547Smaybee VERIFY(arc_buf_remove_ref(osi->os_phys_buf, &osi->os_phys_buf) == 1); 4162856Snd150628 mutex_destroy(&osi->os_lock); 4172856Snd150628 mutex_destroy(&osi->os_obj_lock); 4185326Sek110237 mutex_destroy(&osi->os_user_ptr_lock); 419789Sahrens kmem_free(osi, sizeof (objset_impl_t)); 420789Sahrens } 421789Sahrens 422789Sahrens /* called from dsl for meta-objset */ 423789Sahrens objset_impl_t * 4243547Smaybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, 4253547Smaybee dmu_objset_type_t type, dmu_tx_t *tx) 426789Sahrens { 427789Sahrens objset_impl_t *osi; 428789Sahrens dnode_t *mdn; 429789Sahrens 430789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 4314787Sahrens if (ds) 4324787Sahrens mutex_enter(&ds->ds_opening_lock); 4333547Smaybee VERIFY(0 == dmu_objset_open_impl(spa, ds, bp, &osi)); 4344787Sahrens if (ds) 4354787Sahrens mutex_exit(&ds->ds_opening_lock); 436789Sahrens mdn = osi->os_meta_dnode; 437789Sahrens 438789Sahrens dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT, 439789Sahrens DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx); 440789Sahrens 441789Sahrens /* 442789Sahrens * We don't want to have to increase the meta-dnode's nlevels 443789Sahrens * later, because then we could do it in quescing context while 444789Sahrens * we are also accessing it in open context. 445789Sahrens * 446789Sahrens * This precaution is not necessary for the MOS (ds == NULL), 447789Sahrens * because the MOS is only updated in syncing context. 448789Sahrens * This is most fortunate: the MOS is the only objset that 449789Sahrens * needs to be synced multiple times as spa_sync() iterates 450789Sahrens * to convergence, so minimizing its dn_nlevels matters. 451789Sahrens */ 4521544Seschrock if (ds != NULL) { 4531544Seschrock int levels = 1; 4541544Seschrock 4551544Seschrock /* 4561544Seschrock * Determine the number of levels necessary for the meta-dnode 4571544Seschrock * to contain DN_MAX_OBJECT dnodes. 4581544Seschrock */ 4591544Seschrock while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift + 4601544Seschrock (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) < 4611544Seschrock DN_MAX_OBJECT * sizeof (dnode_phys_t)) 4621544Seschrock levels++; 4631544Seschrock 464789Sahrens mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] = 4651544Seschrock mdn->dn_nlevels = levels; 4661544Seschrock } 467789Sahrens 468789Sahrens ASSERT(type != DMU_OST_NONE); 469789Sahrens ASSERT(type != DMU_OST_ANY); 470789Sahrens ASSERT(type < DMU_OST_NUMTYPES); 471789Sahrens osi->os_phys->os_type = type; 472789Sahrens 473789Sahrens dsl_dataset_dirty(ds, tx); 474789Sahrens 475789Sahrens return (osi); 476789Sahrens } 477789Sahrens 478789Sahrens struct oscarg { 4794543Smarks void (*userfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx); 480789Sahrens void *userarg; 481789Sahrens dsl_dataset_t *clone_parent; 482789Sahrens const char *lastname; 483789Sahrens dmu_objset_type_t type; 484*6492Stimh uint64_t flags; 485789Sahrens }; 486789Sahrens 4874543Smarks /*ARGSUSED*/ 488789Sahrens static int 4892199Sahrens dmu_objset_create_check(void *arg1, void *arg2, dmu_tx_t *tx) 490789Sahrens { 4912199Sahrens dsl_dir_t *dd = arg1; 4922199Sahrens struct oscarg *oa = arg2; 4932199Sahrens objset_t *mos = dd->dd_pool->dp_meta_objset; 4942199Sahrens int err; 4952199Sahrens uint64_t ddobj; 4962199Sahrens 4972199Sahrens err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj, 4982199Sahrens oa->lastname, sizeof (uint64_t), 1, &ddobj); 4992199Sahrens if (err != ENOENT) 5002199Sahrens return (err ? err : EEXIST); 5012199Sahrens 5022199Sahrens if (oa->clone_parent != NULL) { 5032199Sahrens /* 5042199Sahrens * You can't clone across pools. 5052199Sahrens */ 5062199Sahrens if (oa->clone_parent->ds_dir->dd_pool != dd->dd_pool) 5072199Sahrens return (EXDEV); 5082199Sahrens 5092199Sahrens /* 5102199Sahrens * You can only clone snapshots, not the head datasets. 5112199Sahrens */ 5122199Sahrens if (oa->clone_parent->ds_phys->ds_num_children == 0) 5132199Sahrens return (EINVAL); 5142199Sahrens } 5154543Smarks 5162199Sahrens return (0); 5172199Sahrens } 5182199Sahrens 5192199Sahrens static void 5204543Smarks dmu_objset_create_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 5212199Sahrens { 5222199Sahrens dsl_dir_t *dd = arg1; 5232199Sahrens struct oscarg *oa = arg2; 524789Sahrens dsl_dataset_t *ds; 5253547Smaybee blkptr_t *bp; 5262199Sahrens uint64_t dsobj; 527789Sahrens 528789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 529789Sahrens 5302199Sahrens dsobj = dsl_dataset_create_sync(dd, oa->lastname, 531*6492Stimh oa->clone_parent, oa->flags, cr, tx); 532789Sahrens 5332199Sahrens VERIFY(0 == dsl_dataset_open_obj(dd->dd_pool, dsobj, NULL, 5341544Seschrock DS_MODE_STANDARD | DS_MODE_READONLY, FTAG, &ds)); 5353547Smaybee bp = dsl_dataset_get_blkptr(ds); 5363547Smaybee if (BP_IS_HOLE(bp)) { 537789Sahrens objset_impl_t *osi; 538789Sahrens 539789Sahrens /* This is an empty dmu_objset; not a clone. */ 540789Sahrens osi = dmu_objset_create_impl(dsl_dataset_get_spa(ds), 5413547Smaybee ds, bp, oa->type, tx); 542789Sahrens 543789Sahrens if (oa->userfunc) 5444543Smarks oa->userfunc(&osi->os, oa->userarg, cr, tx); 545789Sahrens } 5464543Smarks 5474543Smarks spa_history_internal_log(LOG_DS_CREATE, dd->dd_pool->dp_spa, 5484543Smarks tx, cr, "dataset = %llu", dsobj); 5494543Smarks 550789Sahrens dsl_dataset_close(ds, DS_MODE_STANDARD | DS_MODE_READONLY, FTAG); 551789Sahrens } 552789Sahrens 553789Sahrens int 554789Sahrens dmu_objset_create(const char *name, dmu_objset_type_t type, 555*6492Stimh objset_t *clone_parent, uint64_t flags, 5564543Smarks void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg) 557789Sahrens { 5582199Sahrens dsl_dir_t *pdd; 559789Sahrens const char *tail; 560789Sahrens int err = 0; 5612199Sahrens struct oscarg oa = { 0 }; 562789Sahrens 5632199Sahrens ASSERT(strchr(name, '@') == NULL); 5642199Sahrens err = dsl_dir_open(name, FTAG, &pdd, &tail); 5651544Seschrock if (err) 5661544Seschrock return (err); 567789Sahrens if (tail == NULL) { 5682199Sahrens dsl_dir_close(pdd, FTAG); 569789Sahrens return (EEXIST); 570789Sahrens } 571789Sahrens 572789Sahrens dprintf("name=%s\n", name); 573789Sahrens 5742199Sahrens oa.userfunc = func; 5752199Sahrens oa.userarg = arg; 5762199Sahrens oa.lastname = tail; 5772199Sahrens oa.type = type; 578*6492Stimh oa.flags = flags; 5794543Smarks 5802199Sahrens if (clone_parent != NULL) { 581789Sahrens /* 5822199Sahrens * You can't clone to a different type. 583789Sahrens */ 5842199Sahrens if (clone_parent->os->os_phys->os_type != type) { 5852199Sahrens dsl_dir_close(pdd, FTAG); 5862199Sahrens return (EINVAL); 587789Sahrens } 5882199Sahrens oa.clone_parent = clone_parent->os->os_dsl_dataset; 589789Sahrens } 5902199Sahrens err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check, 5912199Sahrens dmu_objset_create_sync, pdd, &oa, 5); 5922199Sahrens dsl_dir_close(pdd, FTAG); 593789Sahrens return (err); 594789Sahrens } 595789Sahrens 596789Sahrens int 597789Sahrens dmu_objset_destroy(const char *name) 598789Sahrens { 599789Sahrens objset_t *os; 600789Sahrens int error; 601789Sahrens 602789Sahrens /* 603789Sahrens * If it looks like we'll be able to destroy it, and there's 604789Sahrens * an unplayed replay log sitting around, destroy the log. 605789Sahrens * It would be nicer to do this in dsl_dataset_destroy_sync(), 606789Sahrens * but the replay log objset is modified in open context. 607789Sahrens */ 6085367Sahrens error = dmu_objset_open(name, DMU_OST_ANY, 6095367Sahrens DS_MODE_EXCLUSIVE|DS_MODE_READONLY, &os); 610789Sahrens if (error == 0) { 6115367Sahrens dsl_dataset_t *ds = os->os->os_dsl_dataset; 6121807Sbonwick zil_destroy(dmu_objset_zil(os), B_FALSE); 6135367Sahrens 6145367Sahrens /* 6155367Sahrens * dsl_dataset_destroy() closes the ds. 6165367Sahrens * os is just used as the tag after it's freed. 6175367Sahrens */ 6185367Sahrens kmem_free(os, sizeof (objset_t)); 6195367Sahrens error = dsl_dataset_destroy(ds, os); 620789Sahrens } 621789Sahrens 6225367Sahrens return (error); 623789Sahrens } 624789Sahrens 6255446Sahrens /* 6265446Sahrens * This will close the objset. 6275446Sahrens */ 628789Sahrens int 6295446Sahrens dmu_objset_rollback(objset_t *os) 630789Sahrens { 631789Sahrens int err; 6325367Sahrens dsl_dataset_t *ds; 633789Sahrens 6345446Sahrens ds = os->os->os_dsl_dataset; 6354935Sperrin 6365446Sahrens if (!dsl_dataset_tryupgrade(ds, DS_MODE_STANDARD, DS_MODE_EXCLUSIVE)) { 6375446Sahrens dmu_objset_close(os); 6385446Sahrens return (EBUSY); 6395446Sahrens } 6405446Sahrens 6415367Sahrens err = dsl_dataset_rollback(ds, os->os->os_phys->os_type); 6424935Sperrin 6435367Sahrens /* 6445367Sahrens * NB: we close the objset manually because the rollback 6455367Sahrens * actually implicitly called dmu_objset_evict(), thus freeing 6465367Sahrens * the objset_impl_t. 6475367Sahrens */ 6485367Sahrens dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, os); 6495367Sahrens kmem_free(os, sizeof (objset_t)); 650789Sahrens return (err); 651789Sahrens } 652789Sahrens 6532199Sahrens struct snaparg { 6542199Sahrens dsl_sync_task_group_t *dstg; 6552199Sahrens char *snapname; 6562199Sahrens char failed[MAXPATHLEN]; 6574543Smarks boolean_t checkperms; 6585367Sahrens list_t objsets; 6595367Sahrens }; 6605367Sahrens 6615367Sahrens struct osnode { 6625367Sahrens list_node_t node; 6635367Sahrens objset_t *os; 6642199Sahrens }; 6652199Sahrens 6662199Sahrens static int 6672199Sahrens dmu_objset_snapshot_one(char *name, void *arg) 6682199Sahrens { 6692199Sahrens struct snaparg *sn = arg; 6702199Sahrens objset_t *os; 6713637Srm160521 dmu_objset_stats_t stat; 6722199Sahrens int err; 6732199Sahrens 6742199Sahrens (void) strcpy(sn->failed, name); 6752199Sahrens 6764543Smarks /* 6774543Smarks * Check permissions only when requested. This only applies when 6784543Smarks * doing a recursive snapshot. The permission checks for the starting 6794543Smarks * dataset have already been performed in zfs_secpolicy_snapshot() 6804543Smarks */ 6814543Smarks if (sn->checkperms == B_TRUE && 6824543Smarks (err = zfs_secpolicy_snapshot_perms(name, CRED()))) 6834543Smarks return (err); 6844543Smarks 6852199Sahrens err = dmu_objset_open(name, DMU_OST_ANY, DS_MODE_STANDARD, &os); 6862199Sahrens if (err != 0) 6872199Sahrens return (err); 6882199Sahrens 6892199Sahrens /* 6903637Srm160521 * If the objset is in an inconsistent state, return busy. 6913637Srm160521 */ 6923637Srm160521 dmu_objset_fast_stat(os, &stat); 6933637Srm160521 if (stat.dds_inconsistent) { 6943637Srm160521 dmu_objset_close(os); 6953637Srm160521 return (EBUSY); 6963637Srm160521 } 6973637Srm160521 6983637Srm160521 /* 6992199Sahrens * NB: we need to wait for all in-flight changes to get to disk, 7002199Sahrens * so that we snapshot those changes. zil_suspend does this as 7012199Sahrens * a side effect. 7022199Sahrens */ 7032199Sahrens err = zil_suspend(dmu_objset_zil(os)); 7042199Sahrens if (err == 0) { 7055367Sahrens struct osnode *osn; 7062199Sahrens dsl_sync_task_create(sn->dstg, dsl_dataset_snapshot_check, 7075367Sahrens dsl_dataset_snapshot_sync, os->os->os_dsl_dataset, 7085367Sahrens sn->snapname, 3); 7095367Sahrens osn = kmem_alloc(sizeof (struct osnode), KM_SLEEP); 7105367Sahrens osn->os = os; 7115367Sahrens list_insert_tail(&sn->objsets, osn); 7123637Srm160521 } else { 7133637Srm160521 dmu_objset_close(os); 7142199Sahrens } 7153637Srm160521 7162199Sahrens return (err); 7172199Sahrens } 7182199Sahrens 7192199Sahrens int 7202199Sahrens dmu_objset_snapshot(char *fsname, char *snapname, boolean_t recursive) 7212199Sahrens { 7222199Sahrens dsl_sync_task_t *dst; 7235367Sahrens struct osnode *osn; 7242199Sahrens struct snaparg sn = { 0 }; 7252199Sahrens spa_t *spa; 7262199Sahrens int err; 7272199Sahrens 7282199Sahrens (void) strcpy(sn.failed, fsname); 7292199Sahrens 7304603Sahrens err = spa_open(fsname, &spa, FTAG); 7312199Sahrens if (err) 7322199Sahrens return (err); 7332199Sahrens 7342199Sahrens sn.dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 7352199Sahrens sn.snapname = snapname; 7365367Sahrens list_create(&sn.objsets, sizeof (struct osnode), 7375367Sahrens offsetof(struct osnode, node)); 7382199Sahrens 7392417Sahrens if (recursive) { 7404543Smarks sn.checkperms = B_TRUE; 7412417Sahrens err = dmu_objset_find(fsname, 7422417Sahrens dmu_objset_snapshot_one, &sn, DS_FIND_CHILDREN); 7432417Sahrens } else { 7444543Smarks sn.checkperms = B_FALSE; 7452199Sahrens err = dmu_objset_snapshot_one(fsname, &sn); 7462417Sahrens } 7472199Sahrens 7482199Sahrens if (err) 7492199Sahrens goto out; 7502199Sahrens 7512199Sahrens err = dsl_sync_task_group_wait(sn.dstg); 7522199Sahrens 7532199Sahrens for (dst = list_head(&sn.dstg->dstg_tasks); dst; 7542199Sahrens dst = list_next(&sn.dstg->dstg_tasks, dst)) { 7555367Sahrens dsl_dataset_t *ds = dst->dst_arg1; 7562199Sahrens if (dst->dst_err) 7575367Sahrens dsl_dataset_name(ds, sn.failed); 7582199Sahrens } 7595367Sahrens 7606174Sahrens out: 7615367Sahrens while (osn = list_head(&sn.objsets)) { 7625367Sahrens list_remove(&sn.objsets, osn); 7635367Sahrens zil_resume(dmu_objset_zil(osn->os)); 7645367Sahrens dmu_objset_close(osn->os); 7655367Sahrens kmem_free(osn, sizeof (struct osnode)); 7665367Sahrens } 7675367Sahrens list_destroy(&sn.objsets); 7686174Sahrens 7692199Sahrens if (err) 7702199Sahrens (void) strcpy(fsname, sn.failed); 7712199Sahrens dsl_sync_task_group_destroy(sn.dstg); 7722199Sahrens spa_close(spa, FTAG); 7732199Sahrens return (err); 7742199Sahrens } 7752199Sahrens 776789Sahrens static void 7773547Smaybee dmu_objset_sync_dnodes(list_t *list, dmu_tx_t *tx) 778789Sahrens { 7793547Smaybee dnode_t *dn; 780789Sahrens 7813547Smaybee while (dn = list_head(list)) { 7823547Smaybee ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT); 7833547Smaybee ASSERT(dn->dn_dbuf->db_data_pending); 7843547Smaybee /* 7853547Smaybee * Initialize dn_zio outside dnode_sync() 7863547Smaybee * to accomodate meta-dnode 7873547Smaybee */ 7883547Smaybee dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio; 7893547Smaybee ASSERT(dn->dn_zio); 790789Sahrens 7913547Smaybee ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS); 7923547Smaybee list_remove(list, dn); 7933547Smaybee dnode_sync(dn, tx); 7943547Smaybee } 7953547Smaybee } 7962981Sahrens 7973547Smaybee /* ARGSUSED */ 7983547Smaybee static void 7993547Smaybee ready(zio_t *zio, arc_buf_t *abuf, void *arg) 8003547Smaybee { 8013547Smaybee objset_impl_t *os = arg; 8023547Smaybee blkptr_t *bp = os->os_rootbp; 8033547Smaybee dnode_phys_t *dnp = &os->os_phys->os_meta_dnode; 8043547Smaybee int i; 8052981Sahrens 8065329Sgw25295 ASSERT(bp == zio->io_bp); 8075329Sgw25295 8083547Smaybee /* 8093547Smaybee * Update rootbp fill count. 8103547Smaybee */ 8113547Smaybee bp->blk_fill = 1; /* count the meta-dnode */ 8123547Smaybee for (i = 0; i < dnp->dn_nblkptr; i++) 8133547Smaybee bp->blk_fill += dnp->dn_blkptr[i].blk_fill; 8145329Sgw25295 8155329Sgw25295 BP_SET_TYPE(bp, DMU_OT_OBJSET); 8165329Sgw25295 BP_SET_LEVEL(bp, 0); 8175329Sgw25295 8185329Sgw25295 /* We must do this after we've set the bp's type and level */ 8195329Sgw25295 if (!DVA_EQUAL(BP_IDENTITY(bp), 8205329Sgw25295 BP_IDENTITY(&zio->io_bp_orig))) { 8215329Sgw25295 if (zio->io_bp_orig.blk_birth == os->os_synctx->tx_txg) 8225329Sgw25295 dsl_dataset_block_kill(os->os_dsl_dataset, 8235329Sgw25295 &zio->io_bp_orig, NULL, os->os_synctx); 8245329Sgw25295 dsl_dataset_block_born(os->os_dsl_dataset, bp, os->os_synctx); 8255329Sgw25295 } 826789Sahrens } 827789Sahrens 828789Sahrens /* ARGSUSED */ 829789Sahrens static void 830789Sahrens killer(zio_t *zio, arc_buf_t *abuf, void *arg) 831789Sahrens { 832789Sahrens objset_impl_t *os = arg; 833789Sahrens 834789Sahrens ASSERT3U(zio->io_error, ==, 0); 8353547Smaybee arc_release(os->os_phys_buf, &os->os_phys_buf); 836789Sahrens } 837789Sahrens 838789Sahrens /* called from dsl */ 839789Sahrens void 8403547Smaybee dmu_objset_sync(objset_impl_t *os, zio_t *pio, dmu_tx_t *tx) 841789Sahrens { 842789Sahrens int txgoff; 8431544Seschrock zbookmark_t zb; 8443547Smaybee zio_t *zio; 8453547Smaybee list_t *list; 8463547Smaybee dbuf_dirty_record_t *dr; 8473547Smaybee 8483547Smaybee dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg); 849789Sahrens 850789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 851789Sahrens /* XXX the write_done callback should really give us the tx... */ 852789Sahrens os->os_synctx = tx; 853789Sahrens 8543882Sahrens if (os->os_dsl_dataset == NULL) { 8553882Sahrens /* 8563882Sahrens * This is the MOS. If we have upgraded, 8573882Sahrens * spa_max_replication() could change, so reset 8583882Sahrens * os_copies here. 8593882Sahrens */ 8603882Sahrens os->os_copies = spa_max_replication(os->os_spa); 8613882Sahrens } 8623882Sahrens 8633547Smaybee /* 8643547Smaybee * Create the root block IO 8653547Smaybee */ 8663547Smaybee zb.zb_objset = os->os_dsl_dataset ? os->os_dsl_dataset->ds_object : 0; 8673547Smaybee zb.zb_object = 0; 8683547Smaybee zb.zb_level = -1; 8693547Smaybee zb.zb_blkid = 0; 8704787Sahrens if (BP_IS_OLDER(os->os_rootbp, tx->tx_txg)) { 8713547Smaybee dsl_dataset_block_kill(os->os_dsl_dataset, 8723547Smaybee os->os_rootbp, pio, tx); 8734787Sahrens } 8743547Smaybee zio = arc_write(pio, os->os_spa, os->os_md_checksum, 8753547Smaybee os->os_md_compress, 8763835Sahrens dmu_get_replication_level(os, &zb, DMU_OT_OBJSET), 8773547Smaybee tx->tx_txg, os->os_rootbp, os->os_phys_buf, ready, killer, os, 8784634Sek110237 ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED | ZIO_FLAG_METADATA, 8794634Sek110237 &zb); 8803547Smaybee 8813547Smaybee /* 8823547Smaybee * Sync meta-dnode - the parent IO for the sync is the root block 8833547Smaybee */ 8843547Smaybee os->os_meta_dnode->dn_zio = zio; 8853547Smaybee dnode_sync(os->os_meta_dnode, tx); 886789Sahrens 887789Sahrens txgoff = tx->tx_txg & TXG_MASK; 888789Sahrens 8893547Smaybee dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], tx); 8903547Smaybee dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], tx); 891789Sahrens 8923547Smaybee list = &os->os_meta_dnode->dn_dirty_records[txgoff]; 8933547Smaybee while (dr = list_head(list)) { 8943547Smaybee ASSERT(dr->dr_dbuf->db_level == 0); 8953547Smaybee list_remove(list, dr); 8963547Smaybee if (dr->dr_zio) 8973547Smaybee zio_nowait(dr->dr_zio); 8983547Smaybee } 899789Sahrens /* 900789Sahrens * Free intent log blocks up to this tx. 901789Sahrens */ 902789Sahrens zil_sync(os->os_zil, tx); 9033547Smaybee zio_nowait(zio); 904789Sahrens } 905789Sahrens 906789Sahrens void 9072885Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp, 9082885Sahrens uint64_t *usedobjsp, uint64_t *availobjsp) 9092885Sahrens { 9102885Sahrens dsl_dataset_space(os->os->os_dsl_dataset, refdbytesp, availbytesp, 9112885Sahrens usedobjsp, availobjsp); 9122885Sahrens } 9132885Sahrens 9142885Sahrens uint64_t 9152885Sahrens dmu_objset_fsid_guid(objset_t *os) 9162885Sahrens { 9172885Sahrens return (dsl_dataset_fsid_guid(os->os->os_dsl_dataset)); 9182885Sahrens } 9192885Sahrens 9202885Sahrens void 9212885Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat) 922789Sahrens { 9232885Sahrens stat->dds_type = os->os->os_phys->os_type; 9242885Sahrens if (os->os->os_dsl_dataset) 9252885Sahrens dsl_dataset_fast_stat(os->os->os_dsl_dataset, stat); 9262885Sahrens } 9272885Sahrens 9282885Sahrens void 9292885Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv) 9302885Sahrens { 9312885Sahrens ASSERT(os->os->os_dsl_dataset || 9322885Sahrens os->os->os_phys->os_type == DMU_OST_META); 9332885Sahrens 9342885Sahrens if (os->os->os_dsl_dataset != NULL) 9352885Sahrens dsl_dataset_stats(os->os->os_dsl_dataset, nv); 9362885Sahrens 9372885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE, 9382885Sahrens os->os->os_phys->os_type); 939789Sahrens } 940789Sahrens 941789Sahrens int 942789Sahrens dmu_objset_is_snapshot(objset_t *os) 943789Sahrens { 944789Sahrens if (os->os->os_dsl_dataset != NULL) 945789Sahrens return (dsl_dataset_is_snapshot(os->os->os_dsl_dataset)); 946789Sahrens else 947789Sahrens return (B_FALSE); 948789Sahrens } 949789Sahrens 950789Sahrens int 951*6492Stimh dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen, 952*6492Stimh boolean_t *conflict) 953*6492Stimh { 954*6492Stimh dsl_dataset_t *ds = os->os->os_dsl_dataset; 955*6492Stimh uint64_t ignored; 956*6492Stimh 957*6492Stimh if (ds->ds_phys->ds_snapnames_zapobj == 0) 958*6492Stimh return (ENOENT); 959*6492Stimh 960*6492Stimh return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset, 961*6492Stimh ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST, 962*6492Stimh real, maxlen, conflict)); 963*6492Stimh } 964*6492Stimh 965*6492Stimh int 966789Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name, 9675663Sck153898 uint64_t *idp, uint64_t *offp, boolean_t *case_conflict) 968789Sahrens { 969789Sahrens dsl_dataset_t *ds = os->os->os_dsl_dataset; 970789Sahrens zap_cursor_t cursor; 971789Sahrens zap_attribute_t attr; 972789Sahrens 973789Sahrens if (ds->ds_phys->ds_snapnames_zapobj == 0) 974789Sahrens return (ENOENT); 975789Sahrens 976789Sahrens zap_cursor_init_serialized(&cursor, 977789Sahrens ds->ds_dir->dd_pool->dp_meta_objset, 978789Sahrens ds->ds_phys->ds_snapnames_zapobj, *offp); 979789Sahrens 980885Sahrens if (zap_cursor_retrieve(&cursor, &attr) != 0) { 981885Sahrens zap_cursor_fini(&cursor); 982885Sahrens return (ENOENT); 983885Sahrens } 984885Sahrens 985885Sahrens if (strlen(attr.za_name) + 1 > namelen) { 986885Sahrens zap_cursor_fini(&cursor); 987885Sahrens return (ENAMETOOLONG); 988885Sahrens } 989885Sahrens 990885Sahrens (void) strcpy(name, attr.za_name); 991885Sahrens if (idp) 992885Sahrens *idp = attr.za_first_integer; 9935663Sck153898 if (case_conflict) 9945663Sck153898 *case_conflict = attr.za_normalization_conflict; 995885Sahrens zap_cursor_advance(&cursor); 996885Sahrens *offp = zap_cursor_serialize(&cursor); 997885Sahrens zap_cursor_fini(&cursor); 998885Sahrens 999885Sahrens return (0); 1000885Sahrens } 1001885Sahrens 1002885Sahrens int 1003885Sahrens dmu_dir_list_next(objset_t *os, int namelen, char *name, 1004885Sahrens uint64_t *idp, uint64_t *offp) 1005885Sahrens { 1006885Sahrens dsl_dir_t *dd = os->os->os_dsl_dataset->ds_dir; 1007885Sahrens zap_cursor_t cursor; 1008885Sahrens zap_attribute_t attr; 1009885Sahrens 1010885Sahrens /* there is no next dir on a snapshot! */ 1011885Sahrens if (os->os->os_dsl_dataset->ds_object != 1012885Sahrens dd->dd_phys->dd_head_dataset_obj) 1013885Sahrens return (ENOENT); 1014885Sahrens 1015885Sahrens zap_cursor_init_serialized(&cursor, 1016885Sahrens dd->dd_pool->dp_meta_objset, 1017885Sahrens dd->dd_phys->dd_child_dir_zapobj, *offp); 1018885Sahrens 1019885Sahrens if (zap_cursor_retrieve(&cursor, &attr) != 0) { 1020885Sahrens zap_cursor_fini(&cursor); 1021885Sahrens return (ENOENT); 1022885Sahrens } 1023885Sahrens 1024885Sahrens if (strlen(attr.za_name) + 1 > namelen) { 1025885Sahrens zap_cursor_fini(&cursor); 1026789Sahrens return (ENAMETOOLONG); 1027885Sahrens } 1028789Sahrens 1029789Sahrens (void) strcpy(name, attr.za_name); 1030885Sahrens if (idp) 1031885Sahrens *idp = attr.za_first_integer; 1032789Sahrens zap_cursor_advance(&cursor); 1033789Sahrens *offp = zap_cursor_serialize(&cursor); 1034885Sahrens zap_cursor_fini(&cursor); 1035789Sahrens 1036789Sahrens return (0); 1037789Sahrens } 1038789Sahrens 1039789Sahrens /* 1040789Sahrens * Find all objsets under name, and for each, call 'func(child_name, arg)'. 1041789Sahrens */ 10422199Sahrens int 10432199Sahrens dmu_objset_find(char *name, int func(char *, void *), void *arg, int flags) 1044789Sahrens { 1045789Sahrens dsl_dir_t *dd; 1046789Sahrens objset_t *os; 1047789Sahrens uint64_t snapobj; 1048789Sahrens zap_cursor_t zc; 10493978Smmusante zap_attribute_t *attr; 1050789Sahrens char *child; 10511544Seschrock int do_self, err; 1052789Sahrens 10531544Seschrock err = dsl_dir_open(name, FTAG, &dd, NULL); 10541544Seschrock if (err) 10552199Sahrens return (err); 1056789Sahrens 10572199Sahrens /* NB: the $MOS dir doesn't have a head dataset */ 1058789Sahrens do_self = (dd->dd_phys->dd_head_dataset_obj != 0); 10593978Smmusante attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); 1060789Sahrens 1061789Sahrens /* 1062789Sahrens * Iterate over all children. 1063789Sahrens */ 10642417Sahrens if (flags & DS_FIND_CHILDREN) { 10652417Sahrens for (zap_cursor_init(&zc, dd->dd_pool->dp_meta_objset, 10662417Sahrens dd->dd_phys->dd_child_dir_zapobj); 10673978Smmusante zap_cursor_retrieve(&zc, attr) == 0; 10682417Sahrens (void) zap_cursor_advance(&zc)) { 10693978Smmusante ASSERT(attr->za_integer_length == sizeof (uint64_t)); 10703978Smmusante ASSERT(attr->za_num_integers == 1); 1071789Sahrens 10722417Sahrens /* 10732417Sahrens * No separating '/' because parent's name ends in /. 10742417Sahrens */ 10752417Sahrens child = kmem_alloc(MAXPATHLEN, KM_SLEEP); 10762417Sahrens /* XXX could probably just use name here */ 10772417Sahrens dsl_dir_name(dd, child); 10782417Sahrens (void) strcat(child, "/"); 10793978Smmusante (void) strcat(child, attr->za_name); 10802417Sahrens err = dmu_objset_find(child, func, arg, flags); 10812417Sahrens kmem_free(child, MAXPATHLEN); 10822417Sahrens if (err) 10832417Sahrens break; 10842417Sahrens } 10852417Sahrens zap_cursor_fini(&zc); 10862199Sahrens 10872417Sahrens if (err) { 10882417Sahrens dsl_dir_close(dd, FTAG); 10893978Smmusante kmem_free(attr, sizeof (zap_attribute_t)); 10902417Sahrens return (err); 10912417Sahrens } 1092789Sahrens } 1093789Sahrens 1094789Sahrens /* 1095789Sahrens * Iterate over all snapshots. 1096789Sahrens */ 1097789Sahrens if ((flags & DS_FIND_SNAPSHOTS) && 1098789Sahrens dmu_objset_open(name, DMU_OST_ANY, 1099789Sahrens DS_MODE_STANDARD | DS_MODE_READONLY, &os) == 0) { 1100789Sahrens 1101789Sahrens snapobj = os->os->os_dsl_dataset->ds_phys->ds_snapnames_zapobj; 1102789Sahrens dmu_objset_close(os); 1103789Sahrens 1104789Sahrens for (zap_cursor_init(&zc, dd->dd_pool->dp_meta_objset, snapobj); 11053978Smmusante zap_cursor_retrieve(&zc, attr) == 0; 1106789Sahrens (void) zap_cursor_advance(&zc)) { 11073978Smmusante ASSERT(attr->za_integer_length == sizeof (uint64_t)); 11083978Smmusante ASSERT(attr->za_num_integers == 1); 1109789Sahrens 1110789Sahrens child = kmem_alloc(MAXPATHLEN, KM_SLEEP); 1111789Sahrens /* XXX could probably just use name here */ 1112789Sahrens dsl_dir_name(dd, child); 1113789Sahrens (void) strcat(child, "@"); 11143978Smmusante (void) strcat(child, attr->za_name); 11152199Sahrens err = func(child, arg); 1116789Sahrens kmem_free(child, MAXPATHLEN); 11172199Sahrens if (err) 11182199Sahrens break; 1119789Sahrens } 1120885Sahrens zap_cursor_fini(&zc); 1121789Sahrens } 1122789Sahrens 1123789Sahrens dsl_dir_close(dd, FTAG); 11243978Smmusante kmem_free(attr, sizeof (zap_attribute_t)); 1125789Sahrens 11262199Sahrens if (err) 11272199Sahrens return (err); 11282199Sahrens 1129789Sahrens /* 1130789Sahrens * Apply to self if appropriate. 1131789Sahrens */ 1132789Sahrens if (do_self) 11332199Sahrens err = func(name, arg); 11342199Sahrens return (err); 1135789Sahrens } 11365326Sek110237 11375326Sek110237 void 11385326Sek110237 dmu_objset_set_user(objset_t *os, void *user_ptr) 11395326Sek110237 { 11405326Sek110237 ASSERT(MUTEX_HELD(&os->os->os_user_ptr_lock)); 11415326Sek110237 os->os->os_user_ptr = user_ptr; 11425326Sek110237 } 11435326Sek110237 11445326Sek110237 void * 11455326Sek110237 dmu_objset_get_user(objset_t *os) 11465326Sek110237 { 11475326Sek110237 ASSERT(MUTEX_HELD(&os->os->os_user_ptr_lock)); 11485326Sek110237 return (os->os->os_user_ptr); 11495326Sek110237 } 1150