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 264543Smarks #include <sys/cred.h> 27789Sahrens #include <sys/zfs_context.h> 28789Sahrens #include <sys/dmu_objset.h> 29789Sahrens #include <sys/dsl_dir.h> 30789Sahrens #include <sys/dsl_dataset.h> 31789Sahrens #include <sys/dsl_prop.h> 32789Sahrens #include <sys/dsl_pool.h> 332199Sahrens #include <sys/dsl_synctask.h> 344543Smarks #include <sys/dsl_deleg.h> 35789Sahrens #include <sys/dnode.h> 36789Sahrens #include <sys/dbuf.h> 372885Sahrens #include <sys/zvol.h> 38789Sahrens #include <sys/dmu_tx.h> 39789Sahrens #include <sys/zio_checksum.h> 40789Sahrens #include <sys/zap.h> 41789Sahrens #include <sys/zil.h> 42789Sahrens #include <sys/dmu_impl.h> 434543Smarks #include <sys/zfs_ioctl.h> 44789Sahrens 45789Sahrens spa_t * 46789Sahrens dmu_objset_spa(objset_t *os) 47789Sahrens { 48789Sahrens return (os->os->os_spa); 49789Sahrens } 50789Sahrens 51789Sahrens zilog_t * 52789Sahrens dmu_objset_zil(objset_t *os) 53789Sahrens { 54789Sahrens return (os->os->os_zil); 55789Sahrens } 56789Sahrens 57789Sahrens dsl_pool_t * 58789Sahrens dmu_objset_pool(objset_t *os) 59789Sahrens { 60789Sahrens dsl_dataset_t *ds; 61789Sahrens 62789Sahrens if ((ds = os->os->os_dsl_dataset) != NULL && ds->ds_dir) 63789Sahrens return (ds->ds_dir->dd_pool); 64789Sahrens else 65789Sahrens return (spa_get_dsl(os->os->os_spa)); 66789Sahrens } 67789Sahrens 68789Sahrens dsl_dataset_t * 69789Sahrens dmu_objset_ds(objset_t *os) 70789Sahrens { 71789Sahrens return (os->os->os_dsl_dataset); 72789Sahrens } 73789Sahrens 74789Sahrens dmu_objset_type_t 75789Sahrens dmu_objset_type(objset_t *os) 76789Sahrens { 77789Sahrens return (os->os->os_phys->os_type); 78789Sahrens } 79789Sahrens 80789Sahrens void 81789Sahrens dmu_objset_name(objset_t *os, char *buf) 82789Sahrens { 83789Sahrens dsl_dataset_name(os->os->os_dsl_dataset, buf); 84789Sahrens } 85789Sahrens 86789Sahrens uint64_t 87789Sahrens dmu_objset_id(objset_t *os) 88789Sahrens { 89789Sahrens dsl_dataset_t *ds = os->os->os_dsl_dataset; 90789Sahrens 91789Sahrens return (ds ? ds->ds_object : 0); 92789Sahrens } 93789Sahrens 94789Sahrens static void 95789Sahrens checksum_changed_cb(void *arg, uint64_t newval) 96789Sahrens { 97789Sahrens objset_impl_t *osi = arg; 98789Sahrens 99789Sahrens /* 100789Sahrens * Inheritance should have been done by now. 101789Sahrens */ 102789Sahrens ASSERT(newval != ZIO_CHECKSUM_INHERIT); 103789Sahrens 104789Sahrens osi->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE); 105789Sahrens } 106789Sahrens 107789Sahrens static void 108789Sahrens compression_changed_cb(void *arg, uint64_t newval) 109789Sahrens { 110789Sahrens objset_impl_t *osi = arg; 111789Sahrens 112789Sahrens /* 113789Sahrens * Inheritance and range checking should have been done by now. 114789Sahrens */ 115789Sahrens ASSERT(newval != ZIO_COMPRESS_INHERIT); 116789Sahrens 117789Sahrens osi->os_compress = zio_compress_select(newval, ZIO_COMPRESS_ON_VALUE); 118789Sahrens } 119789Sahrens 1203835Sahrens static void 1213835Sahrens copies_changed_cb(void *arg, uint64_t newval) 1223835Sahrens { 1233835Sahrens objset_impl_t *osi = arg; 1243835Sahrens 1253835Sahrens /* 1263835Sahrens * Inheritance and range checking should have been done by now. 1273835Sahrens */ 1283835Sahrens ASSERT(newval > 0); 1293835Sahrens ASSERT(newval <= spa_max_replication(osi->os_spa)); 1303835Sahrens 1313835Sahrens osi->os_copies = newval; 1323835Sahrens } 1333835Sahrens 1347237Sek110237 static void 1357237Sek110237 primary_cache_changed_cb(void *arg, uint64_t newval) 1367237Sek110237 { 1377237Sek110237 objset_impl_t *osi = arg; 1387237Sek110237 1397237Sek110237 /* 1407237Sek110237 * Inheritance and range checking should have been done by now. 1417237Sek110237 */ 1427237Sek110237 ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE || 1437237Sek110237 newval == ZFS_CACHE_METADATA); 1447237Sek110237 1457237Sek110237 osi->os_primary_cache = newval; 1467237Sek110237 } 1477237Sek110237 1487237Sek110237 static void 1497237Sek110237 secondary_cache_changed_cb(void *arg, uint64_t newval) 1507237Sek110237 { 1517237Sek110237 objset_impl_t *osi = arg; 1527237Sek110237 1537237Sek110237 /* 1547237Sek110237 * Inheritance and range checking should have been done by now. 1557237Sek110237 */ 1567237Sek110237 ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE || 1577237Sek110237 newval == ZFS_CACHE_METADATA); 1587237Sek110237 1597237Sek110237 osi->os_secondary_cache = newval; 1607237Sek110237 } 1617237Sek110237 162789Sahrens void 163789Sahrens dmu_objset_byteswap(void *buf, size_t size) 164789Sahrens { 165789Sahrens objset_phys_t *osp = buf; 166789Sahrens 167789Sahrens ASSERT(size == sizeof (objset_phys_t)); 168789Sahrens dnode_byteswap(&osp->os_meta_dnode); 169789Sahrens byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t)); 170789Sahrens osp->os_type = BSWAP_64(osp->os_type); 171789Sahrens } 172789Sahrens 1731544Seschrock int 1741544Seschrock dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, 1751544Seschrock objset_impl_t **osip) 176789Sahrens { 1774787Sahrens objset_impl_t *osi; 1787046Sahrens int i, err; 179789Sahrens 1804787Sahrens ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock)); 1814787Sahrens 182789Sahrens osi = kmem_zalloc(sizeof (objset_impl_t), KM_SLEEP); 183789Sahrens osi->os.os = osi; 184789Sahrens osi->os_dsl_dataset = ds; 185789Sahrens osi->os_spa = spa; 1863547Smaybee osi->os_rootbp = bp; 1873547Smaybee if (!BP_IS_HOLE(osi->os_rootbp)) { 1882391Smaybee uint32_t aflags = ARC_WAIT; 1891544Seschrock zbookmark_t zb; 1901544Seschrock zb.zb_objset = ds ? ds->ds_object : 0; 1911544Seschrock zb.zb_object = 0; 1921544Seschrock zb.zb_level = -1; 1931544Seschrock zb.zb_blkid = 0; 1947237Sek110237 if (DMU_OS_IS_L2CACHEABLE(osi)) 1957237Sek110237 aflags |= ARC_L2CACHE; 1961544Seschrock 1973547Smaybee dprintf_bp(osi->os_rootbp, "reading %s", ""); 1987046Sahrens /* 1997046Sahrens * NB: when bprewrite scrub can change the bp, 2007046Sahrens * and this is called from dmu_objset_open_ds_os, the bp 2017046Sahrens * could change, and we'll need a lock. 2027046Sahrens */ 2037046Sahrens err = arc_read_nolock(NULL, spa, osi->os_rootbp, 2043547Smaybee arc_getbuf_func, &osi->os_phys_buf, 2052391Smaybee ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb); 2061544Seschrock if (err) { 2071544Seschrock kmem_free(osi, sizeof (objset_impl_t)); 2087294Sperrin /* convert checksum errors into IO errors */ 2097294Sperrin if (err == ECKSUM) 2107294Sperrin err = EIO; 2111544Seschrock return (err); 2121544Seschrock } 2133547Smaybee osi->os_phys = osi->os_phys_buf->b_data; 214789Sahrens } else { 2153547Smaybee osi->os_phys_buf = arc_buf_alloc(spa, sizeof (objset_phys_t), 2163547Smaybee &osi->os_phys_buf, ARC_BUFC_METADATA); 2173547Smaybee osi->os_phys = osi->os_phys_buf->b_data; 218789Sahrens bzero(osi->os_phys, sizeof (objset_phys_t)); 219789Sahrens } 220789Sahrens 221789Sahrens /* 222789Sahrens * Note: the changed_cb will be called once before the register 223789Sahrens * func returns, thus changing the checksum/compression from the 2247237Sek110237 * default (fletcher2/off). Snapshots don't need to know about 2257237Sek110237 * checksum/compression/copies. 226789Sahrens */ 2277237Sek110237 if (ds) { 2287237Sek110237 err = dsl_prop_register(ds, "primarycache", 2297237Sek110237 primary_cache_changed_cb, osi); 2301544Seschrock if (err == 0) 2317237Sek110237 err = dsl_prop_register(ds, "secondarycache", 2327237Sek110237 secondary_cache_changed_cb, osi); 2337237Sek110237 if (!dsl_dataset_is_snapshot(ds)) { 2347237Sek110237 if (err == 0) 2357237Sek110237 err = dsl_prop_register(ds, "checksum", 2367237Sek110237 checksum_changed_cb, osi); 2377237Sek110237 if (err == 0) 2387237Sek110237 err = dsl_prop_register(ds, "compression", 2397237Sek110237 compression_changed_cb, osi); 2407237Sek110237 if (err == 0) 2417237Sek110237 err = dsl_prop_register(ds, "copies", 2427237Sek110237 copies_changed_cb, osi); 2437237Sek110237 } 2441544Seschrock if (err) { 2453547Smaybee VERIFY(arc_buf_remove_ref(osi->os_phys_buf, 2463547Smaybee &osi->os_phys_buf) == 1); 2471544Seschrock kmem_free(osi, sizeof (objset_impl_t)); 2481544Seschrock return (err); 2491544Seschrock } 2502082Seschrock } else if (ds == NULL) { 251789Sahrens /* It's the meta-objset. */ 252789Sahrens osi->os_checksum = ZIO_CHECKSUM_FLETCHER_4; 2531544Seschrock osi->os_compress = ZIO_COMPRESS_LZJB; 2543835Sahrens osi->os_copies = spa_max_replication(spa); 2557237Sek110237 osi->os_primary_cache = ZFS_CACHE_ALL; 2567237Sek110237 osi->os_secondary_cache = ZFS_CACHE_ALL; 257789Sahrens } 258789Sahrens 2597046Sahrens osi->os_zil_header = osi->os_phys->os_zil_header; 2607046Sahrens osi->os_zil = zil_alloc(&osi->os, &osi->os_zil_header); 261789Sahrens 262789Sahrens for (i = 0; i < TXG_SIZE; i++) { 263789Sahrens list_create(&osi->os_dirty_dnodes[i], sizeof (dnode_t), 264789Sahrens offsetof(dnode_t, dn_dirty_link[i])); 265789Sahrens list_create(&osi->os_free_dnodes[i], sizeof (dnode_t), 266789Sahrens offsetof(dnode_t, dn_dirty_link[i])); 267789Sahrens } 268789Sahrens list_create(&osi->os_dnodes, sizeof (dnode_t), 269789Sahrens offsetof(dnode_t, dn_link)); 270789Sahrens list_create(&osi->os_downgraded_dbufs, sizeof (dmu_buf_impl_t), 271789Sahrens offsetof(dmu_buf_impl_t, db_link)); 272789Sahrens 2732856Snd150628 mutex_init(&osi->os_lock, NULL, MUTEX_DEFAULT, NULL); 2742856Snd150628 mutex_init(&osi->os_obj_lock, NULL, MUTEX_DEFAULT, NULL); 2755326Sek110237 mutex_init(&osi->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL); 2762856Snd150628 277789Sahrens osi->os_meta_dnode = dnode_special_open(osi, 278789Sahrens &osi->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT); 279789Sahrens 2804787Sahrens /* 2814787Sahrens * We should be the only thread trying to do this because we 2824787Sahrens * have ds_opening_lock 2834787Sahrens */ 2844787Sahrens if (ds) { 2854787Sahrens VERIFY(NULL == dsl_dataset_set_user_ptr(ds, osi, 2864787Sahrens dmu_objset_evict)); 287789Sahrens } 288789Sahrens 2891544Seschrock *osip = osi; 2901544Seschrock return (0); 291789Sahrens } 292789Sahrens 2935367Sahrens static int 2945367Sahrens dmu_objset_open_ds_os(dsl_dataset_t *ds, objset_t *os, dmu_objset_type_t type) 2955367Sahrens { 2965367Sahrens objset_impl_t *osi; 2975367Sahrens 2985367Sahrens mutex_enter(&ds->ds_opening_lock); 2995367Sahrens osi = dsl_dataset_get_user_ptr(ds); 3005367Sahrens if (osi == NULL) { 3016689Smaybee int err; 3026689Smaybee 3035367Sahrens err = dmu_objset_open_impl(dsl_dataset_get_spa(ds), 3045367Sahrens ds, &ds->ds_phys->ds_bp, &osi); 3056689Smaybee if (err) { 3066689Smaybee mutex_exit(&ds->ds_opening_lock); 3075367Sahrens return (err); 3086689Smaybee } 3095367Sahrens } 3105367Sahrens mutex_exit(&ds->ds_opening_lock); 3115367Sahrens 3125367Sahrens os->os = osi; 3136689Smaybee os->os_mode = DS_MODE_NOHOLD; 3145367Sahrens 3155367Sahrens if (type != DMU_OST_ANY && type != os->os->os_phys->os_type) 3165367Sahrens return (EINVAL); 3175367Sahrens return (0); 3185367Sahrens } 3195367Sahrens 3205367Sahrens int 3215367Sahrens dmu_objset_open_ds(dsl_dataset_t *ds, dmu_objset_type_t type, objset_t **osp) 3225367Sahrens { 3235367Sahrens objset_t *os; 3245367Sahrens int err; 3255367Sahrens 3265367Sahrens os = kmem_alloc(sizeof (objset_t), KM_SLEEP); 3275367Sahrens err = dmu_objset_open_ds_os(ds, os, type); 3285367Sahrens if (err) 3295367Sahrens kmem_free(os, sizeof (objset_t)); 3305367Sahrens else 3315367Sahrens *osp = os; 3325367Sahrens return (err); 3335367Sahrens } 3345367Sahrens 335789Sahrens /* called from zpl */ 336789Sahrens int 337789Sahrens dmu_objset_open(const char *name, dmu_objset_type_t type, int mode, 338789Sahrens objset_t **osp) 339789Sahrens { 3405326Sek110237 objset_t *os; 341789Sahrens dsl_dataset_t *ds; 342789Sahrens int err; 343789Sahrens 3446689Smaybee ASSERT(DS_MODE_TYPE(mode) == DS_MODE_USER || 3456689Smaybee DS_MODE_TYPE(mode) == DS_MODE_OWNER); 3465367Sahrens 347789Sahrens os = kmem_alloc(sizeof (objset_t), KM_SLEEP); 3486689Smaybee if (DS_MODE_TYPE(mode) == DS_MODE_USER) 3496689Smaybee err = dsl_dataset_hold(name, os, &ds); 3506689Smaybee else 3516689Smaybee err = dsl_dataset_own(name, mode, os, &ds); 352789Sahrens if (err) { 353789Sahrens kmem_free(os, sizeof (objset_t)); 354789Sahrens return (err); 355789Sahrens } 356789Sahrens 3575367Sahrens err = dmu_objset_open_ds_os(ds, os, type); 3585367Sahrens if (err) { 3596689Smaybee if (DS_MODE_TYPE(mode) == DS_MODE_USER) 3606689Smaybee dsl_dataset_rele(ds, os); 3616689Smaybee else 3626689Smaybee dsl_dataset_disown(ds, os); 3635367Sahrens kmem_free(os, sizeof (objset_t)); 3645367Sahrens } else { 3656689Smaybee os->os_mode = mode; 3665367Sahrens *osp = os; 367789Sahrens } 3685367Sahrens return (err); 369789Sahrens } 370789Sahrens 371789Sahrens void 372789Sahrens dmu_objset_close(objset_t *os) 373789Sahrens { 3746689Smaybee ASSERT(DS_MODE_TYPE(os->os_mode) == DS_MODE_USER || 3756689Smaybee DS_MODE_TYPE(os->os_mode) == DS_MODE_OWNER || 3766689Smaybee DS_MODE_TYPE(os->os_mode) == DS_MODE_NOHOLD); 3776689Smaybee 3786689Smaybee if (DS_MODE_TYPE(os->os_mode) == DS_MODE_USER) 3796689Smaybee dsl_dataset_rele(os->os->os_dsl_dataset, os); 3806689Smaybee else if (DS_MODE_TYPE(os->os_mode) == DS_MODE_OWNER) 3816689Smaybee dsl_dataset_disown(os->os->os_dsl_dataset, os); 382789Sahrens kmem_free(os, sizeof (objset_t)); 383789Sahrens } 384789Sahrens 3851646Sperrin int 3864944Smaybee dmu_objset_evict_dbufs(objset_t *os) 3871544Seschrock { 3881544Seschrock objset_impl_t *osi = os->os; 3891544Seschrock dnode_t *dn; 3901596Sahrens 3911596Sahrens mutex_enter(&osi->os_lock); 3921596Sahrens 3931596Sahrens /* process the mdn last, since the other dnodes have holds on it */ 3941596Sahrens list_remove(&osi->os_dnodes, osi->os_meta_dnode); 3951596Sahrens list_insert_tail(&osi->os_dnodes, osi->os_meta_dnode); 3961544Seschrock 3971544Seschrock /* 3981596Sahrens * Find the first dnode with holds. We have to do this dance 3991596Sahrens * because dnode_add_ref() only works if you already have a 4001596Sahrens * hold. If there are no holds then it has no dbufs so OK to 4011596Sahrens * skip. 4021544Seschrock */ 4031596Sahrens for (dn = list_head(&osi->os_dnodes); 4044944Smaybee dn && !dnode_add_ref(dn, FTAG); 4051596Sahrens dn = list_next(&osi->os_dnodes, dn)) 4061596Sahrens continue; 4071596Sahrens 4081596Sahrens while (dn) { 4091596Sahrens dnode_t *next_dn = dn; 4101596Sahrens 4111596Sahrens do { 4121596Sahrens next_dn = list_next(&osi->os_dnodes, next_dn); 4134944Smaybee } while (next_dn && !dnode_add_ref(next_dn, FTAG)); 4141596Sahrens 4151596Sahrens mutex_exit(&osi->os_lock); 4164944Smaybee dnode_evict_dbufs(dn); 4171596Sahrens dnode_rele(dn, FTAG); 4181596Sahrens mutex_enter(&osi->os_lock); 4191596Sahrens dn = next_dn; 4201544Seschrock } 4211544Seschrock mutex_exit(&osi->os_lock); 4224944Smaybee return (list_head(&osi->os_dnodes) != osi->os_meta_dnode); 4231544Seschrock } 4241544Seschrock 4251544Seschrock void 426789Sahrens dmu_objset_evict(dsl_dataset_t *ds, void *arg) 427789Sahrens { 428789Sahrens objset_impl_t *osi = arg; 4291544Seschrock objset_t os; 4302082Seschrock int i; 431789Sahrens 432789Sahrens for (i = 0; i < TXG_SIZE; i++) { 433789Sahrens ASSERT(list_head(&osi->os_dirty_dnodes[i]) == NULL); 434789Sahrens ASSERT(list_head(&osi->os_free_dnodes[i]) == NULL); 435789Sahrens } 436789Sahrens 4377237Sek110237 if (ds) { 4387237Sek110237 if (!dsl_dataset_is_snapshot(ds)) { 4397237Sek110237 VERIFY(0 == dsl_prop_unregister(ds, "checksum", 4407237Sek110237 checksum_changed_cb, osi)); 4417237Sek110237 VERIFY(0 == dsl_prop_unregister(ds, "compression", 4427237Sek110237 compression_changed_cb, osi)); 4437237Sek110237 VERIFY(0 == dsl_prop_unregister(ds, "copies", 4447237Sek110237 copies_changed_cb, osi)); 4457237Sek110237 } 4467237Sek110237 VERIFY(0 == dsl_prop_unregister(ds, "primarycache", 4477237Sek110237 primary_cache_changed_cb, osi)); 4487237Sek110237 VERIFY(0 == dsl_prop_unregister(ds, "secondarycache", 4497237Sek110237 secondary_cache_changed_cb, osi)); 450789Sahrens } 451789Sahrens 4521544Seschrock /* 4531544Seschrock * We should need only a single pass over the dnode list, since 4541544Seschrock * nothing can be added to the list at this point. 4551544Seschrock */ 4561544Seschrock os.os = osi; 4574944Smaybee (void) dmu_objset_evict_dbufs(&os); 4581544Seschrock 459789Sahrens ASSERT3P(list_head(&osi->os_dnodes), ==, osi->os_meta_dnode); 460789Sahrens ASSERT3P(list_tail(&osi->os_dnodes), ==, osi->os_meta_dnode); 461789Sahrens ASSERT3P(list_head(&osi->os_meta_dnode->dn_dbufs), ==, NULL); 462789Sahrens 463789Sahrens dnode_special_close(osi->os_meta_dnode); 464789Sahrens zil_free(osi->os_zil); 465789Sahrens 4663547Smaybee VERIFY(arc_buf_remove_ref(osi->os_phys_buf, &osi->os_phys_buf) == 1); 4672856Snd150628 mutex_destroy(&osi->os_lock); 4682856Snd150628 mutex_destroy(&osi->os_obj_lock); 4695326Sek110237 mutex_destroy(&osi->os_user_ptr_lock); 470789Sahrens kmem_free(osi, sizeof (objset_impl_t)); 471789Sahrens } 472789Sahrens 473789Sahrens /* called from dsl for meta-objset */ 474789Sahrens objset_impl_t * 4753547Smaybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, 4763547Smaybee dmu_objset_type_t type, dmu_tx_t *tx) 477789Sahrens { 478789Sahrens objset_impl_t *osi; 479789Sahrens dnode_t *mdn; 480789Sahrens 481789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 4824787Sahrens if (ds) 4834787Sahrens mutex_enter(&ds->ds_opening_lock); 4843547Smaybee VERIFY(0 == dmu_objset_open_impl(spa, ds, bp, &osi)); 4854787Sahrens if (ds) 4864787Sahrens mutex_exit(&ds->ds_opening_lock); 487789Sahrens mdn = osi->os_meta_dnode; 488789Sahrens 489789Sahrens dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT, 490789Sahrens DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx); 491789Sahrens 492789Sahrens /* 493789Sahrens * We don't want to have to increase the meta-dnode's nlevels 494789Sahrens * later, because then we could do it in quescing context while 495789Sahrens * we are also accessing it in open context. 496789Sahrens * 497789Sahrens * This precaution is not necessary for the MOS (ds == NULL), 498789Sahrens * because the MOS is only updated in syncing context. 499789Sahrens * This is most fortunate: the MOS is the only objset that 500789Sahrens * needs to be synced multiple times as spa_sync() iterates 501789Sahrens * to convergence, so minimizing its dn_nlevels matters. 502789Sahrens */ 5031544Seschrock if (ds != NULL) { 5041544Seschrock int levels = 1; 5051544Seschrock 5061544Seschrock /* 5071544Seschrock * Determine the number of levels necessary for the meta-dnode 5081544Seschrock * to contain DN_MAX_OBJECT dnodes. 5091544Seschrock */ 5101544Seschrock while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift + 5111544Seschrock (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) < 5121544Seschrock DN_MAX_OBJECT * sizeof (dnode_phys_t)) 5131544Seschrock levels++; 5141544Seschrock 515789Sahrens mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] = 5161544Seschrock mdn->dn_nlevels = levels; 5171544Seschrock } 518789Sahrens 519789Sahrens ASSERT(type != DMU_OST_NONE); 520789Sahrens ASSERT(type != DMU_OST_ANY); 521789Sahrens ASSERT(type < DMU_OST_NUMTYPES); 522789Sahrens osi->os_phys->os_type = type; 523789Sahrens 524789Sahrens dsl_dataset_dirty(ds, tx); 525789Sahrens 526789Sahrens return (osi); 527789Sahrens } 528789Sahrens 529789Sahrens struct oscarg { 5304543Smarks void (*userfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx); 531789Sahrens void *userarg; 532789Sahrens dsl_dataset_t *clone_parent; 533789Sahrens const char *lastname; 534789Sahrens dmu_objset_type_t type; 5356492Stimh uint64_t flags; 536789Sahrens }; 537789Sahrens 5384543Smarks /*ARGSUSED*/ 539789Sahrens static int 5402199Sahrens dmu_objset_create_check(void *arg1, void *arg2, dmu_tx_t *tx) 541789Sahrens { 5422199Sahrens dsl_dir_t *dd = arg1; 5432199Sahrens struct oscarg *oa = arg2; 5442199Sahrens objset_t *mos = dd->dd_pool->dp_meta_objset; 5452199Sahrens int err; 5462199Sahrens uint64_t ddobj; 5472199Sahrens 5482199Sahrens err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj, 5492199Sahrens oa->lastname, sizeof (uint64_t), 1, &ddobj); 5502199Sahrens if (err != ENOENT) 5512199Sahrens return (err ? err : EEXIST); 5522199Sahrens 5532199Sahrens if (oa->clone_parent != NULL) { 5542199Sahrens /* 5552199Sahrens * You can't clone across pools. 5562199Sahrens */ 5572199Sahrens if (oa->clone_parent->ds_dir->dd_pool != dd->dd_pool) 5582199Sahrens return (EXDEV); 5592199Sahrens 5602199Sahrens /* 5612199Sahrens * You can only clone snapshots, not the head datasets. 5622199Sahrens */ 5632199Sahrens if (oa->clone_parent->ds_phys->ds_num_children == 0) 5642199Sahrens return (EINVAL); 5652199Sahrens } 5664543Smarks 5672199Sahrens return (0); 5682199Sahrens } 5692199Sahrens 5702199Sahrens static void 5714543Smarks dmu_objset_create_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 5722199Sahrens { 5732199Sahrens dsl_dir_t *dd = arg1; 5742199Sahrens struct oscarg *oa = arg2; 575789Sahrens dsl_dataset_t *ds; 5763547Smaybee blkptr_t *bp; 5772199Sahrens uint64_t dsobj; 578789Sahrens 579789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 580789Sahrens 5812199Sahrens dsobj = dsl_dataset_create_sync(dd, oa->lastname, 5826492Stimh oa->clone_parent, oa->flags, cr, tx); 583789Sahrens 5846689Smaybee VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, dsobj, FTAG, &ds)); 5853547Smaybee bp = dsl_dataset_get_blkptr(ds); 5863547Smaybee if (BP_IS_HOLE(bp)) { 587789Sahrens objset_impl_t *osi; 588789Sahrens 589789Sahrens /* This is an empty dmu_objset; not a clone. */ 590789Sahrens osi = dmu_objset_create_impl(dsl_dataset_get_spa(ds), 5913547Smaybee ds, bp, oa->type, tx); 592789Sahrens 593789Sahrens if (oa->userfunc) 5944543Smarks oa->userfunc(&osi->os, oa->userarg, cr, tx); 595789Sahrens } 5964543Smarks 5974543Smarks spa_history_internal_log(LOG_DS_CREATE, dd->dd_pool->dp_spa, 5984543Smarks tx, cr, "dataset = %llu", dsobj); 5994543Smarks 6006689Smaybee dsl_dataset_rele(ds, FTAG); 601789Sahrens } 602789Sahrens 603789Sahrens int 604789Sahrens dmu_objset_create(const char *name, dmu_objset_type_t type, 6056492Stimh objset_t *clone_parent, uint64_t flags, 6064543Smarks void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg) 607789Sahrens { 6082199Sahrens dsl_dir_t *pdd; 609789Sahrens const char *tail; 610789Sahrens int err = 0; 6112199Sahrens struct oscarg oa = { 0 }; 612789Sahrens 6132199Sahrens ASSERT(strchr(name, '@') == NULL); 6142199Sahrens err = dsl_dir_open(name, FTAG, &pdd, &tail); 6151544Seschrock if (err) 6161544Seschrock return (err); 617789Sahrens if (tail == NULL) { 6182199Sahrens dsl_dir_close(pdd, FTAG); 619789Sahrens return (EEXIST); 620789Sahrens } 621789Sahrens 622789Sahrens dprintf("name=%s\n", name); 623789Sahrens 6242199Sahrens oa.userfunc = func; 6252199Sahrens oa.userarg = arg; 6262199Sahrens oa.lastname = tail; 6272199Sahrens oa.type = type; 6286492Stimh oa.flags = flags; 6294543Smarks 6302199Sahrens if (clone_parent != NULL) { 631789Sahrens /* 6322199Sahrens * You can't clone to a different type. 633789Sahrens */ 6342199Sahrens if (clone_parent->os->os_phys->os_type != type) { 6352199Sahrens dsl_dir_close(pdd, FTAG); 6362199Sahrens return (EINVAL); 637789Sahrens } 6382199Sahrens oa.clone_parent = clone_parent->os->os_dsl_dataset; 639789Sahrens } 6402199Sahrens err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check, 6412199Sahrens dmu_objset_create_sync, pdd, &oa, 5); 6422199Sahrens dsl_dir_close(pdd, FTAG); 643789Sahrens return (err); 644789Sahrens } 645789Sahrens 646789Sahrens int 647789Sahrens dmu_objset_destroy(const char *name) 648789Sahrens { 649789Sahrens objset_t *os; 650789Sahrens int error; 651789Sahrens 652789Sahrens /* 653789Sahrens * If it looks like we'll be able to destroy it, and there's 654789Sahrens * an unplayed replay log sitting around, destroy the log. 655789Sahrens * It would be nicer to do this in dsl_dataset_destroy_sync(), 656789Sahrens * but the replay log objset is modified in open context. 657789Sahrens */ 6585367Sahrens error = dmu_objset_open(name, DMU_OST_ANY, 6596689Smaybee DS_MODE_OWNER|DS_MODE_READONLY|DS_MODE_INCONSISTENT, &os); 660789Sahrens if (error == 0) { 6615367Sahrens dsl_dataset_t *ds = os->os->os_dsl_dataset; 6621807Sbonwick zil_destroy(dmu_objset_zil(os), B_FALSE); 6635367Sahrens 6646689Smaybee error = dsl_dataset_destroy(ds, os); 6655367Sahrens /* 6665367Sahrens * dsl_dataset_destroy() closes the ds. 6675367Sahrens */ 6685367Sahrens kmem_free(os, sizeof (objset_t)); 669789Sahrens } 670789Sahrens 6715367Sahrens return (error); 672789Sahrens } 673789Sahrens 6745446Sahrens /* 6755446Sahrens * This will close the objset. 6765446Sahrens */ 677789Sahrens int 6785446Sahrens dmu_objset_rollback(objset_t *os) 679789Sahrens { 680789Sahrens int err; 6815367Sahrens dsl_dataset_t *ds; 682789Sahrens 6835446Sahrens ds = os->os->os_dsl_dataset; 6844935Sperrin 6856689Smaybee if (!dsl_dataset_tryown(ds, TRUE, os)) { 6865446Sahrens dmu_objset_close(os); 6875446Sahrens return (EBUSY); 6885446Sahrens } 6895446Sahrens 6905367Sahrens err = dsl_dataset_rollback(ds, os->os->os_phys->os_type); 6914935Sperrin 6925367Sahrens /* 6935367Sahrens * NB: we close the objset manually because the rollback 6945367Sahrens * actually implicitly called dmu_objset_evict(), thus freeing 6955367Sahrens * the objset_impl_t. 6965367Sahrens */ 6976689Smaybee dsl_dataset_disown(ds, os); 6985367Sahrens kmem_free(os, sizeof (objset_t)); 699789Sahrens return (err); 700789Sahrens } 701789Sahrens 7022199Sahrens struct snaparg { 7032199Sahrens dsl_sync_task_group_t *dstg; 7042199Sahrens char *snapname; 7052199Sahrens char failed[MAXPATHLEN]; 7064543Smarks boolean_t checkperms; 7075367Sahrens list_t objsets; 7085367Sahrens }; 7095367Sahrens 7105367Sahrens struct osnode { 7115367Sahrens list_node_t node; 7125367Sahrens objset_t *os; 7132199Sahrens }; 7142199Sahrens 7152199Sahrens static int 7162199Sahrens dmu_objset_snapshot_one(char *name, void *arg) 7172199Sahrens { 7182199Sahrens struct snaparg *sn = arg; 7192199Sahrens objset_t *os; 7202199Sahrens int err; 7212199Sahrens 7222199Sahrens (void) strcpy(sn->failed, name); 7232199Sahrens 7244543Smarks /* 7254543Smarks * Check permissions only when requested. This only applies when 7264543Smarks * doing a recursive snapshot. The permission checks for the starting 7274543Smarks * dataset have already been performed in zfs_secpolicy_snapshot() 7284543Smarks */ 7294543Smarks if (sn->checkperms == B_TRUE && 7304543Smarks (err = zfs_secpolicy_snapshot_perms(name, CRED()))) 7314543Smarks return (err); 7324543Smarks 7336689Smaybee err = dmu_objset_open(name, DMU_OST_ANY, DS_MODE_USER, &os); 7342199Sahrens if (err != 0) 7352199Sahrens return (err); 7362199Sahrens 7376689Smaybee /* If the objset is in an inconsistent state, return busy */ 7386689Smaybee if (os->os->os_dsl_dataset->ds_phys->ds_flags & DS_FLAG_INCONSISTENT) { 7393637Srm160521 dmu_objset_close(os); 7403637Srm160521 return (EBUSY); 7413637Srm160521 } 7423637Srm160521 7433637Srm160521 /* 7442199Sahrens * NB: we need to wait for all in-flight changes to get to disk, 7452199Sahrens * so that we snapshot those changes. zil_suspend does this as 7462199Sahrens * a side effect. 7472199Sahrens */ 7482199Sahrens err = zil_suspend(dmu_objset_zil(os)); 7492199Sahrens if (err == 0) { 7505367Sahrens struct osnode *osn; 7512199Sahrens dsl_sync_task_create(sn->dstg, dsl_dataset_snapshot_check, 7525367Sahrens dsl_dataset_snapshot_sync, os->os->os_dsl_dataset, 7535367Sahrens sn->snapname, 3); 7545367Sahrens osn = kmem_alloc(sizeof (struct osnode), KM_SLEEP); 7555367Sahrens osn->os = os; 7565367Sahrens list_insert_tail(&sn->objsets, osn); 7573637Srm160521 } else { 7583637Srm160521 dmu_objset_close(os); 7592199Sahrens } 7603637Srm160521 7612199Sahrens return (err); 7622199Sahrens } 7632199Sahrens 7642199Sahrens int 7652199Sahrens dmu_objset_snapshot(char *fsname, char *snapname, boolean_t recursive) 7662199Sahrens { 7672199Sahrens dsl_sync_task_t *dst; 7685367Sahrens struct osnode *osn; 7692199Sahrens struct snaparg sn = { 0 }; 7702199Sahrens spa_t *spa; 7712199Sahrens int err; 7722199Sahrens 7732199Sahrens (void) strcpy(sn.failed, fsname); 7742199Sahrens 7754603Sahrens err = spa_open(fsname, &spa, FTAG); 7762199Sahrens if (err) 7772199Sahrens return (err); 7782199Sahrens 7792199Sahrens sn.dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 7802199Sahrens sn.snapname = snapname; 7815367Sahrens list_create(&sn.objsets, sizeof (struct osnode), 7825367Sahrens offsetof(struct osnode, node)); 7832199Sahrens 7842417Sahrens if (recursive) { 7854543Smarks sn.checkperms = B_TRUE; 7862417Sahrens err = dmu_objset_find(fsname, 7872417Sahrens dmu_objset_snapshot_one, &sn, DS_FIND_CHILDREN); 7882417Sahrens } else { 7894543Smarks sn.checkperms = B_FALSE; 7902199Sahrens err = dmu_objset_snapshot_one(fsname, &sn); 7912417Sahrens } 7922199Sahrens 7932199Sahrens if (err) 7942199Sahrens goto out; 7952199Sahrens 7962199Sahrens err = dsl_sync_task_group_wait(sn.dstg); 7972199Sahrens 7982199Sahrens for (dst = list_head(&sn.dstg->dstg_tasks); dst; 7992199Sahrens dst = list_next(&sn.dstg->dstg_tasks, dst)) { 8005367Sahrens dsl_dataset_t *ds = dst->dst_arg1; 8012199Sahrens if (dst->dst_err) 8025367Sahrens dsl_dataset_name(ds, sn.failed); 8032199Sahrens } 8045367Sahrens 8056174Sahrens out: 8065367Sahrens while (osn = list_head(&sn.objsets)) { 8075367Sahrens list_remove(&sn.objsets, osn); 8085367Sahrens zil_resume(dmu_objset_zil(osn->os)); 8095367Sahrens dmu_objset_close(osn->os); 8105367Sahrens kmem_free(osn, sizeof (struct osnode)); 8115367Sahrens } 8125367Sahrens list_destroy(&sn.objsets); 8136174Sahrens 8142199Sahrens if (err) 8152199Sahrens (void) strcpy(fsname, sn.failed); 8162199Sahrens dsl_sync_task_group_destroy(sn.dstg); 8172199Sahrens spa_close(spa, FTAG); 8182199Sahrens return (err); 8192199Sahrens } 8202199Sahrens 821789Sahrens static void 8223547Smaybee dmu_objset_sync_dnodes(list_t *list, dmu_tx_t *tx) 823789Sahrens { 8243547Smaybee dnode_t *dn; 825789Sahrens 8263547Smaybee while (dn = list_head(list)) { 8273547Smaybee ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT); 8283547Smaybee ASSERT(dn->dn_dbuf->db_data_pending); 8293547Smaybee /* 8303547Smaybee * Initialize dn_zio outside dnode_sync() 8313547Smaybee * to accomodate meta-dnode 8323547Smaybee */ 8333547Smaybee dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio; 8343547Smaybee ASSERT(dn->dn_zio); 835789Sahrens 8363547Smaybee ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS); 8373547Smaybee list_remove(list, dn); 8383547Smaybee dnode_sync(dn, tx); 8393547Smaybee } 8403547Smaybee } 8412981Sahrens 8423547Smaybee /* ARGSUSED */ 8433547Smaybee static void 8443547Smaybee ready(zio_t *zio, arc_buf_t *abuf, void *arg) 8453547Smaybee { 846*7754SJeff.Bonwick@Sun.COM blkptr_t *bp = zio->io_bp; 847*7754SJeff.Bonwick@Sun.COM blkptr_t *bp_orig = &zio->io_bp_orig; 8483547Smaybee objset_impl_t *os = arg; 8493547Smaybee dnode_phys_t *dnp = &os->os_phys->os_meta_dnode; 8502981Sahrens 851*7754SJeff.Bonwick@Sun.COM ASSERT(bp == os->os_rootbp); 852*7754SJeff.Bonwick@Sun.COM ASSERT(BP_GET_TYPE(bp) == DMU_OT_OBJSET); 853*7754SJeff.Bonwick@Sun.COM ASSERT(BP_GET_LEVEL(bp) == 0); 8545329Sgw25295 8553547Smaybee /* 8563547Smaybee * Update rootbp fill count. 8573547Smaybee */ 8583547Smaybee bp->blk_fill = 1; /* count the meta-dnode */ 859*7754SJeff.Bonwick@Sun.COM for (int i = 0; i < dnp->dn_nblkptr; i++) 8603547Smaybee bp->blk_fill += dnp->dn_blkptr[i].blk_fill; 8615329Sgw25295 862*7754SJeff.Bonwick@Sun.COM if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { 863*7754SJeff.Bonwick@Sun.COM ASSERT(DVA_EQUAL(BP_IDENTITY(bp), BP_IDENTITY(bp_orig))); 864*7754SJeff.Bonwick@Sun.COM } else { 8655329Sgw25295 if (zio->io_bp_orig.blk_birth == os->os_synctx->tx_txg) 8666992Smaybee (void) dsl_dataset_block_kill(os->os_dsl_dataset, 867*7754SJeff.Bonwick@Sun.COM &zio->io_bp_orig, zio, os->os_synctx); 8685329Sgw25295 dsl_dataset_block_born(os->os_dsl_dataset, bp, os->os_synctx); 8695329Sgw25295 } 870789Sahrens } 871789Sahrens 872789Sahrens /* called from dsl */ 873789Sahrens void 8743547Smaybee dmu_objset_sync(objset_impl_t *os, zio_t *pio, dmu_tx_t *tx) 875789Sahrens { 876789Sahrens int txgoff; 8771544Seschrock zbookmark_t zb; 8787046Sahrens writeprops_t wp = { 0 }; 8793547Smaybee zio_t *zio; 8803547Smaybee list_t *list; 8813547Smaybee dbuf_dirty_record_t *dr; 8823547Smaybee 8833547Smaybee dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg); 884789Sahrens 885789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 886789Sahrens /* XXX the write_done callback should really give us the tx... */ 887789Sahrens os->os_synctx = tx; 888789Sahrens 8893882Sahrens if (os->os_dsl_dataset == NULL) { 8903882Sahrens /* 8913882Sahrens * This is the MOS. If we have upgraded, 8923882Sahrens * spa_max_replication() could change, so reset 8933882Sahrens * os_copies here. 8943882Sahrens */ 8953882Sahrens os->os_copies = spa_max_replication(os->os_spa); 8963882Sahrens } 8973882Sahrens 8983547Smaybee /* 8993547Smaybee * Create the root block IO 9003547Smaybee */ 9013547Smaybee zb.zb_objset = os->os_dsl_dataset ? os->os_dsl_dataset->ds_object : 0; 9023547Smaybee zb.zb_object = 0; 903*7754SJeff.Bonwick@Sun.COM zb.zb_level = -1; /* for block ordering; it's level 0 on disk */ 9043547Smaybee zb.zb_blkid = 0; 905*7754SJeff.Bonwick@Sun.COM 906*7754SJeff.Bonwick@Sun.COM wp.wp_type = DMU_OT_OBJSET; 907*7754SJeff.Bonwick@Sun.COM wp.wp_level = 0; /* on-disk BP level; see above */ 908*7754SJeff.Bonwick@Sun.COM wp.wp_copies = os->os_copies; 909*7754SJeff.Bonwick@Sun.COM wp.wp_oschecksum = os->os_checksum; 910*7754SJeff.Bonwick@Sun.COM wp.wp_oscompress = os->os_compress; 911*7754SJeff.Bonwick@Sun.COM 9124787Sahrens if (BP_IS_OLDER(os->os_rootbp, tx->tx_txg)) { 9136992Smaybee (void) dsl_dataset_block_kill(os->os_dsl_dataset, 9143547Smaybee os->os_rootbp, pio, tx); 9154787Sahrens } 916*7754SJeff.Bonwick@Sun.COM 9177046Sahrens arc_release(os->os_phys_buf, &os->os_phys_buf); 918*7754SJeff.Bonwick@Sun.COM zio = arc_write(pio, os->os_spa, &wp, DMU_OS_IS_L2CACHEABLE(os), 919*7754SJeff.Bonwick@Sun.COM tx->tx_txg, os->os_rootbp, os->os_phys_buf, ready, NULL, os, 920*7754SJeff.Bonwick@Sun.COM ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb); 9213547Smaybee 9223547Smaybee /* 9233547Smaybee * Sync meta-dnode - the parent IO for the sync is the root block 9243547Smaybee */ 9253547Smaybee os->os_meta_dnode->dn_zio = zio; 9263547Smaybee dnode_sync(os->os_meta_dnode, tx); 927789Sahrens 928789Sahrens txgoff = tx->tx_txg & TXG_MASK; 929789Sahrens 9303547Smaybee dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], tx); 9313547Smaybee dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], tx); 932789Sahrens 9333547Smaybee list = &os->os_meta_dnode->dn_dirty_records[txgoff]; 9343547Smaybee while (dr = list_head(list)) { 9353547Smaybee ASSERT(dr->dr_dbuf->db_level == 0); 9363547Smaybee list_remove(list, dr); 9373547Smaybee if (dr->dr_zio) 9383547Smaybee zio_nowait(dr->dr_zio); 9393547Smaybee } 940789Sahrens /* 941789Sahrens * Free intent log blocks up to this tx. 942789Sahrens */ 943789Sahrens zil_sync(os->os_zil, tx); 9447046Sahrens os->os_phys->os_zil_header = os->os_zil_header; 9453547Smaybee zio_nowait(zio); 946789Sahrens } 947789Sahrens 948789Sahrens void 9492885Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp, 9502885Sahrens uint64_t *usedobjsp, uint64_t *availobjsp) 9512885Sahrens { 9522885Sahrens dsl_dataset_space(os->os->os_dsl_dataset, refdbytesp, availbytesp, 9532885Sahrens usedobjsp, availobjsp); 9542885Sahrens } 9552885Sahrens 9562885Sahrens uint64_t 9572885Sahrens dmu_objset_fsid_guid(objset_t *os) 9582885Sahrens { 9592885Sahrens return (dsl_dataset_fsid_guid(os->os->os_dsl_dataset)); 9602885Sahrens } 9612885Sahrens 9622885Sahrens void 9632885Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat) 964789Sahrens { 9652885Sahrens stat->dds_type = os->os->os_phys->os_type; 9662885Sahrens if (os->os->os_dsl_dataset) 9672885Sahrens dsl_dataset_fast_stat(os->os->os_dsl_dataset, stat); 9682885Sahrens } 9692885Sahrens 9702885Sahrens void 9712885Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv) 9722885Sahrens { 9732885Sahrens ASSERT(os->os->os_dsl_dataset || 9742885Sahrens os->os->os_phys->os_type == DMU_OST_META); 9752885Sahrens 9762885Sahrens if (os->os->os_dsl_dataset != NULL) 9772885Sahrens dsl_dataset_stats(os->os->os_dsl_dataset, nv); 9782885Sahrens 9792885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE, 9802885Sahrens os->os->os_phys->os_type); 981789Sahrens } 982789Sahrens 983789Sahrens int 984789Sahrens dmu_objset_is_snapshot(objset_t *os) 985789Sahrens { 986789Sahrens if (os->os->os_dsl_dataset != NULL) 987789Sahrens return (dsl_dataset_is_snapshot(os->os->os_dsl_dataset)); 988789Sahrens else 989789Sahrens return (B_FALSE); 990789Sahrens } 991789Sahrens 992789Sahrens int 9936492Stimh dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen, 9946492Stimh boolean_t *conflict) 9956492Stimh { 9966492Stimh dsl_dataset_t *ds = os->os->os_dsl_dataset; 9976492Stimh uint64_t ignored; 9986492Stimh 9996492Stimh if (ds->ds_phys->ds_snapnames_zapobj == 0) 10006492Stimh return (ENOENT); 10016492Stimh 10026492Stimh return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset, 10036492Stimh ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST, 10046492Stimh real, maxlen, conflict)); 10056492Stimh } 10066492Stimh 10076492Stimh int 1008789Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name, 10095663Sck153898 uint64_t *idp, uint64_t *offp, boolean_t *case_conflict) 1010789Sahrens { 1011789Sahrens dsl_dataset_t *ds = os->os->os_dsl_dataset; 1012789Sahrens zap_cursor_t cursor; 1013789Sahrens zap_attribute_t attr; 1014789Sahrens 1015789Sahrens if (ds->ds_phys->ds_snapnames_zapobj == 0) 1016789Sahrens return (ENOENT); 1017789Sahrens 1018789Sahrens zap_cursor_init_serialized(&cursor, 1019789Sahrens ds->ds_dir->dd_pool->dp_meta_objset, 1020789Sahrens ds->ds_phys->ds_snapnames_zapobj, *offp); 1021789Sahrens 1022885Sahrens if (zap_cursor_retrieve(&cursor, &attr) != 0) { 1023885Sahrens zap_cursor_fini(&cursor); 1024885Sahrens return (ENOENT); 1025885Sahrens } 1026885Sahrens 1027885Sahrens if (strlen(attr.za_name) + 1 > namelen) { 1028885Sahrens zap_cursor_fini(&cursor); 1029885Sahrens return (ENAMETOOLONG); 1030885Sahrens } 1031885Sahrens 1032885Sahrens (void) strcpy(name, attr.za_name); 1033885Sahrens if (idp) 1034885Sahrens *idp = attr.za_first_integer; 10355663Sck153898 if (case_conflict) 10365663Sck153898 *case_conflict = attr.za_normalization_conflict; 1037885Sahrens zap_cursor_advance(&cursor); 1038885Sahrens *offp = zap_cursor_serialize(&cursor); 1039885Sahrens zap_cursor_fini(&cursor); 1040885Sahrens 1041885Sahrens return (0); 1042885Sahrens } 1043885Sahrens 1044885Sahrens int 1045885Sahrens dmu_dir_list_next(objset_t *os, int namelen, char *name, 1046885Sahrens uint64_t *idp, uint64_t *offp) 1047885Sahrens { 1048885Sahrens dsl_dir_t *dd = os->os->os_dsl_dataset->ds_dir; 1049885Sahrens zap_cursor_t cursor; 1050885Sahrens zap_attribute_t attr; 1051885Sahrens 1052885Sahrens /* there is no next dir on a snapshot! */ 1053885Sahrens if (os->os->os_dsl_dataset->ds_object != 1054885Sahrens dd->dd_phys->dd_head_dataset_obj) 1055885Sahrens return (ENOENT); 1056885Sahrens 1057885Sahrens zap_cursor_init_serialized(&cursor, 1058885Sahrens dd->dd_pool->dp_meta_objset, 1059885Sahrens dd->dd_phys->dd_child_dir_zapobj, *offp); 1060885Sahrens 1061885Sahrens if (zap_cursor_retrieve(&cursor, &attr) != 0) { 1062885Sahrens zap_cursor_fini(&cursor); 1063885Sahrens return (ENOENT); 1064885Sahrens } 1065885Sahrens 1066885Sahrens if (strlen(attr.za_name) + 1 > namelen) { 1067885Sahrens zap_cursor_fini(&cursor); 1068789Sahrens return (ENAMETOOLONG); 1069885Sahrens } 1070789Sahrens 1071789Sahrens (void) strcpy(name, attr.za_name); 1072885Sahrens if (idp) 1073885Sahrens *idp = attr.za_first_integer; 1074789Sahrens zap_cursor_advance(&cursor); 1075789Sahrens *offp = zap_cursor_serialize(&cursor); 1076885Sahrens zap_cursor_fini(&cursor); 1077789Sahrens 1078789Sahrens return (0); 1079789Sahrens } 1080789Sahrens 10817046Sahrens struct findarg { 10827046Sahrens int (*func)(char *, void *); 10837046Sahrens void *arg; 10847046Sahrens }; 10857046Sahrens 10867046Sahrens /* ARGSUSED */ 10877046Sahrens static int 10887046Sahrens findfunc(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg) 10897046Sahrens { 10907046Sahrens struct findarg *fa = arg; 10917046Sahrens return (fa->func((char *)dsname, fa->arg)); 10927046Sahrens } 10937046Sahrens 1094789Sahrens /* 1095789Sahrens * Find all objsets under name, and for each, call 'func(child_name, arg)'. 10967046Sahrens * Perhaps change all callers to use dmu_objset_find_spa()? 1097789Sahrens */ 10982199Sahrens int 10992199Sahrens dmu_objset_find(char *name, int func(char *, void *), void *arg, int flags) 1100789Sahrens { 11017046Sahrens struct findarg fa; 11027046Sahrens fa.func = func; 11037046Sahrens fa.arg = arg; 11047046Sahrens return (dmu_objset_find_spa(NULL, name, findfunc, &fa, flags)); 11057046Sahrens } 11067046Sahrens 11077046Sahrens /* 11087046Sahrens * Find all objsets under name, call func on each 11097046Sahrens */ 11107046Sahrens int 11117046Sahrens dmu_objset_find_spa(spa_t *spa, const char *name, 11127046Sahrens int func(spa_t *, uint64_t, const char *, void *), void *arg, int flags) 11137046Sahrens { 1114789Sahrens dsl_dir_t *dd; 11157046Sahrens dsl_pool_t *dp; 11167046Sahrens dsl_dataset_t *ds; 1117789Sahrens zap_cursor_t zc; 11183978Smmusante zap_attribute_t *attr; 1119789Sahrens char *child; 11207046Sahrens uint64_t thisobj; 11217046Sahrens int err; 1122789Sahrens 11237046Sahrens if (name == NULL) 11247046Sahrens name = spa_name(spa); 11257046Sahrens err = dsl_dir_open_spa(spa, name, FTAG, &dd, NULL); 11261544Seschrock if (err) 11272199Sahrens return (err); 1128789Sahrens 11297046Sahrens /* Don't visit hidden ($MOS & $ORIGIN) objsets. */ 11307046Sahrens if (dd->dd_myname[0] == '$') { 11317046Sahrens dsl_dir_close(dd, FTAG); 11327046Sahrens return (0); 11337046Sahrens } 11347046Sahrens 11357046Sahrens thisobj = dd->dd_phys->dd_head_dataset_obj; 11363978Smmusante attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); 11377046Sahrens dp = dd->dd_pool; 1138789Sahrens 1139789Sahrens /* 1140789Sahrens * Iterate over all children. 1141789Sahrens */ 11422417Sahrens if (flags & DS_FIND_CHILDREN) { 11437046Sahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, 11442417Sahrens dd->dd_phys->dd_child_dir_zapobj); 11453978Smmusante zap_cursor_retrieve(&zc, attr) == 0; 11462417Sahrens (void) zap_cursor_advance(&zc)) { 11473978Smmusante ASSERT(attr->za_integer_length == sizeof (uint64_t)); 11483978Smmusante ASSERT(attr->za_num_integers == 1); 1149789Sahrens 11502417Sahrens child = kmem_alloc(MAXPATHLEN, KM_SLEEP); 11517046Sahrens (void) strcpy(child, name); 11522417Sahrens (void) strcat(child, "/"); 11533978Smmusante (void) strcat(child, attr->za_name); 11547046Sahrens err = dmu_objset_find_spa(spa, child, func, arg, flags); 11552417Sahrens kmem_free(child, MAXPATHLEN); 11562417Sahrens if (err) 11572417Sahrens break; 11582417Sahrens } 11592417Sahrens zap_cursor_fini(&zc); 11602199Sahrens 11612417Sahrens if (err) { 11622417Sahrens dsl_dir_close(dd, FTAG); 11633978Smmusante kmem_free(attr, sizeof (zap_attribute_t)); 11642417Sahrens return (err); 11652417Sahrens } 1166789Sahrens } 1167789Sahrens 1168789Sahrens /* 1169789Sahrens * Iterate over all snapshots. 1170789Sahrens */ 11717046Sahrens if (flags & DS_FIND_SNAPSHOTS) { 11727046Sahrens if (!dsl_pool_sync_context(dp)) 11737046Sahrens rw_enter(&dp->dp_config_rwlock, RW_READER); 11747046Sahrens err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds); 11757046Sahrens if (!dsl_pool_sync_context(dp)) 11767046Sahrens rw_exit(&dp->dp_config_rwlock); 1177789Sahrens 11787046Sahrens if (err == 0) { 11797046Sahrens uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj; 11807046Sahrens dsl_dataset_rele(ds, FTAG); 1181789Sahrens 11827046Sahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj); 11837046Sahrens zap_cursor_retrieve(&zc, attr) == 0; 11847046Sahrens (void) zap_cursor_advance(&zc)) { 11857046Sahrens ASSERT(attr->za_integer_length == 11867046Sahrens sizeof (uint64_t)); 11877046Sahrens ASSERT(attr->za_num_integers == 1); 1188789Sahrens 11897046Sahrens child = kmem_alloc(MAXPATHLEN, KM_SLEEP); 11907046Sahrens (void) strcpy(child, name); 11917046Sahrens (void) strcat(child, "@"); 11927046Sahrens (void) strcat(child, attr->za_name); 11937046Sahrens err = func(spa, attr->za_first_integer, 11947046Sahrens child, arg); 11957046Sahrens kmem_free(child, MAXPATHLEN); 11967046Sahrens if (err) 11977046Sahrens break; 11987046Sahrens } 11997046Sahrens zap_cursor_fini(&zc); 1200789Sahrens } 1201789Sahrens } 1202789Sahrens 1203789Sahrens dsl_dir_close(dd, FTAG); 12043978Smmusante kmem_free(attr, sizeof (zap_attribute_t)); 1205789Sahrens 12062199Sahrens if (err) 12072199Sahrens return (err); 12082199Sahrens 1209789Sahrens /* 1210789Sahrens * Apply to self if appropriate. 1211789Sahrens */ 12127046Sahrens err = func(spa, thisobj, name, arg); 12132199Sahrens return (err); 1214789Sahrens } 12155326Sek110237 12165326Sek110237 void 12175326Sek110237 dmu_objset_set_user(objset_t *os, void *user_ptr) 12185326Sek110237 { 12195326Sek110237 ASSERT(MUTEX_HELD(&os->os->os_user_ptr_lock)); 12205326Sek110237 os->os->os_user_ptr = user_ptr; 12215326Sek110237 } 12225326Sek110237 12235326Sek110237 void * 12245326Sek110237 dmu_objset_get_user(objset_t *os) 12255326Sek110237 { 12265326Sek110237 ASSERT(MUTEX_HELD(&os->os->os_user_ptr_lock)); 12275326Sek110237 return (os->os->os_user_ptr); 12285326Sek110237 } 1229