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 /* 229355SMatthew.Ahrens@Sun.COM * Copyright 2009 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 1679396SMatthew.Ahrens@Sun.COM ASSERT(size == OBJSET_OLD_PHYS_SIZE || 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); 1719396SMatthew.Ahrens@Sun.COM osp->os_flags = BSWAP_64(osp->os_flags); 1729396SMatthew.Ahrens@Sun.COM if (size == sizeof (objset_phys_t)) { 1739396SMatthew.Ahrens@Sun.COM dnode_byteswap(&osp->os_userused_dnode); 1749396SMatthew.Ahrens@Sun.COM dnode_byteswap(&osp->os_groupused_dnode); 1759396SMatthew.Ahrens@Sun.COM } 176789Sahrens } 177789Sahrens 1781544Seschrock int 1791544Seschrock dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, 1801544Seschrock objset_impl_t **osip) 181789Sahrens { 1824787Sahrens objset_impl_t *osi; 1837046Sahrens int i, err; 184789Sahrens 1854787Sahrens ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock)); 1864787Sahrens 187789Sahrens osi = kmem_zalloc(sizeof (objset_impl_t), KM_SLEEP); 188789Sahrens osi->os.os = osi; 189789Sahrens osi->os_dsl_dataset = ds; 190789Sahrens osi->os_spa = spa; 1913547Smaybee osi->os_rootbp = bp; 1923547Smaybee if (!BP_IS_HOLE(osi->os_rootbp)) { 1932391Smaybee uint32_t aflags = ARC_WAIT; 1941544Seschrock zbookmark_t zb; 1951544Seschrock zb.zb_objset = ds ? ds->ds_object : 0; 1961544Seschrock zb.zb_object = 0; 1971544Seschrock zb.zb_level = -1; 1981544Seschrock zb.zb_blkid = 0; 1997237Sek110237 if (DMU_OS_IS_L2CACHEABLE(osi)) 2007237Sek110237 aflags |= ARC_L2CACHE; 2011544Seschrock 2023547Smaybee dprintf_bp(osi->os_rootbp, "reading %s", ""); 2037046Sahrens /* 2047046Sahrens * NB: when bprewrite scrub can change the bp, 2057046Sahrens * and this is called from dmu_objset_open_ds_os, the bp 2067046Sahrens * could change, and we'll need a lock. 2077046Sahrens */ 2087046Sahrens err = arc_read_nolock(NULL, spa, osi->os_rootbp, 2093547Smaybee arc_getbuf_func, &osi->os_phys_buf, 2102391Smaybee ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb); 2111544Seschrock if (err) { 2121544Seschrock kmem_free(osi, sizeof (objset_impl_t)); 2137294Sperrin /* convert checksum errors into IO errors */ 2147294Sperrin if (err == ECKSUM) 2157294Sperrin err = EIO; 2161544Seschrock return (err); 2171544Seschrock } 2189396SMatthew.Ahrens@Sun.COM 2199396SMatthew.Ahrens@Sun.COM /* Increase the blocksize if we are permitted. */ 2209396SMatthew.Ahrens@Sun.COM if (spa_version(spa) >= SPA_VERSION_USERSPACE && 2219396SMatthew.Ahrens@Sun.COM arc_buf_size(osi->os_phys_buf) < sizeof (objset_phys_t)) { 2229396SMatthew.Ahrens@Sun.COM arc_buf_t *buf = arc_buf_alloc(spa, 2239396SMatthew.Ahrens@Sun.COM sizeof (objset_phys_t), &osi->os_phys_buf, 2249396SMatthew.Ahrens@Sun.COM ARC_BUFC_METADATA); 2259396SMatthew.Ahrens@Sun.COM bzero(buf->b_data, sizeof (objset_phys_t)); 2269396SMatthew.Ahrens@Sun.COM bcopy(osi->os_phys_buf->b_data, buf->b_data, 2279396SMatthew.Ahrens@Sun.COM arc_buf_size(osi->os_phys_buf)); 2289554SMatthew.Ahrens@Sun.COM (void) arc_buf_remove_ref(osi->os_phys_buf, 2299554SMatthew.Ahrens@Sun.COM &osi->os_phys_buf); 2309396SMatthew.Ahrens@Sun.COM osi->os_phys_buf = buf; 2319396SMatthew.Ahrens@Sun.COM } 2329396SMatthew.Ahrens@Sun.COM 2333547Smaybee osi->os_phys = osi->os_phys_buf->b_data; 2349396SMatthew.Ahrens@Sun.COM osi->os_flags = osi->os_phys->os_flags; 235789Sahrens } else { 2369396SMatthew.Ahrens@Sun.COM int size = spa_version(spa) >= SPA_VERSION_USERSPACE ? 2379396SMatthew.Ahrens@Sun.COM sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE; 2389396SMatthew.Ahrens@Sun.COM osi->os_phys_buf = arc_buf_alloc(spa, size, 2393547Smaybee &osi->os_phys_buf, ARC_BUFC_METADATA); 2403547Smaybee osi->os_phys = osi->os_phys_buf->b_data; 2419396SMatthew.Ahrens@Sun.COM bzero(osi->os_phys, size); 242789Sahrens } 243789Sahrens 244789Sahrens /* 245789Sahrens * Note: the changed_cb will be called once before the register 246789Sahrens * func returns, thus changing the checksum/compression from the 2477237Sek110237 * default (fletcher2/off). Snapshots don't need to know about 2487237Sek110237 * checksum/compression/copies. 249789Sahrens */ 2507237Sek110237 if (ds) { 2517237Sek110237 err = dsl_prop_register(ds, "primarycache", 2527237Sek110237 primary_cache_changed_cb, osi); 2531544Seschrock if (err == 0) 2547237Sek110237 err = dsl_prop_register(ds, "secondarycache", 2557237Sek110237 secondary_cache_changed_cb, osi); 2567237Sek110237 if (!dsl_dataset_is_snapshot(ds)) { 2577237Sek110237 if (err == 0) 2587237Sek110237 err = dsl_prop_register(ds, "checksum", 2597237Sek110237 checksum_changed_cb, osi); 2607237Sek110237 if (err == 0) 2617237Sek110237 err = dsl_prop_register(ds, "compression", 2627237Sek110237 compression_changed_cb, osi); 2637237Sek110237 if (err == 0) 2647237Sek110237 err = dsl_prop_register(ds, "copies", 2657237Sek110237 copies_changed_cb, osi); 2667237Sek110237 } 2671544Seschrock if (err) { 2683547Smaybee VERIFY(arc_buf_remove_ref(osi->os_phys_buf, 2693547Smaybee &osi->os_phys_buf) == 1); 2701544Seschrock kmem_free(osi, sizeof (objset_impl_t)); 2711544Seschrock return (err); 2721544Seschrock } 2732082Seschrock } else if (ds == NULL) { 274789Sahrens /* It's the meta-objset. */ 275789Sahrens osi->os_checksum = ZIO_CHECKSUM_FLETCHER_4; 2761544Seschrock osi->os_compress = ZIO_COMPRESS_LZJB; 2773835Sahrens osi->os_copies = spa_max_replication(spa); 2787237Sek110237 osi->os_primary_cache = ZFS_CACHE_ALL; 2797237Sek110237 osi->os_secondary_cache = ZFS_CACHE_ALL; 280789Sahrens } 281789Sahrens 2827046Sahrens osi->os_zil_header = osi->os_phys->os_zil_header; 2837046Sahrens osi->os_zil = zil_alloc(&osi->os, &osi->os_zil_header); 284789Sahrens 285789Sahrens for (i = 0; i < TXG_SIZE; i++) { 286789Sahrens list_create(&osi->os_dirty_dnodes[i], sizeof (dnode_t), 287789Sahrens offsetof(dnode_t, dn_dirty_link[i])); 288789Sahrens list_create(&osi->os_free_dnodes[i], sizeof (dnode_t), 289789Sahrens offsetof(dnode_t, dn_dirty_link[i])); 290789Sahrens } 291789Sahrens list_create(&osi->os_dnodes, sizeof (dnode_t), 292789Sahrens offsetof(dnode_t, dn_link)); 293789Sahrens list_create(&osi->os_downgraded_dbufs, sizeof (dmu_buf_impl_t), 294789Sahrens offsetof(dmu_buf_impl_t, db_link)); 295789Sahrens 2962856Snd150628 mutex_init(&osi->os_lock, NULL, MUTEX_DEFAULT, NULL); 2972856Snd150628 mutex_init(&osi->os_obj_lock, NULL, MUTEX_DEFAULT, NULL); 2985326Sek110237 mutex_init(&osi->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL); 2992856Snd150628 300789Sahrens osi->os_meta_dnode = dnode_special_open(osi, 301789Sahrens &osi->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT); 3029396SMatthew.Ahrens@Sun.COM if (arc_buf_size(osi->os_phys_buf) >= sizeof (objset_phys_t)) { 3039396SMatthew.Ahrens@Sun.COM osi->os_userused_dnode = dnode_special_open(osi, 3049396SMatthew.Ahrens@Sun.COM &osi->os_phys->os_userused_dnode, DMU_USERUSED_OBJECT); 3059396SMatthew.Ahrens@Sun.COM osi->os_groupused_dnode = dnode_special_open(osi, 3069396SMatthew.Ahrens@Sun.COM &osi->os_phys->os_groupused_dnode, DMU_GROUPUSED_OBJECT); 3079396SMatthew.Ahrens@Sun.COM } 308789Sahrens 3094787Sahrens /* 3104787Sahrens * We should be the only thread trying to do this because we 3114787Sahrens * have ds_opening_lock 3124787Sahrens */ 3134787Sahrens if (ds) { 3144787Sahrens VERIFY(NULL == dsl_dataset_set_user_ptr(ds, osi, 3154787Sahrens dmu_objset_evict)); 316789Sahrens } 317789Sahrens 3181544Seschrock *osip = osi; 3191544Seschrock return (0); 320789Sahrens } 321789Sahrens 3225367Sahrens static int 3235367Sahrens dmu_objset_open_ds_os(dsl_dataset_t *ds, objset_t *os, dmu_objset_type_t type) 3245367Sahrens { 3255367Sahrens objset_impl_t *osi; 3265367Sahrens 3275367Sahrens mutex_enter(&ds->ds_opening_lock); 3285367Sahrens osi = dsl_dataset_get_user_ptr(ds); 3295367Sahrens if (osi == NULL) { 3306689Smaybee int err; 3316689Smaybee 3325367Sahrens err = dmu_objset_open_impl(dsl_dataset_get_spa(ds), 3335367Sahrens ds, &ds->ds_phys->ds_bp, &osi); 3346689Smaybee if (err) { 3356689Smaybee mutex_exit(&ds->ds_opening_lock); 3365367Sahrens return (err); 3376689Smaybee } 3385367Sahrens } 3395367Sahrens mutex_exit(&ds->ds_opening_lock); 3405367Sahrens 3415367Sahrens os->os = osi; 3426689Smaybee os->os_mode = DS_MODE_NOHOLD; 3435367Sahrens 3445367Sahrens if (type != DMU_OST_ANY && type != os->os->os_phys->os_type) 3455367Sahrens return (EINVAL); 3465367Sahrens return (0); 3475367Sahrens } 3485367Sahrens 3495367Sahrens int 3505367Sahrens dmu_objset_open_ds(dsl_dataset_t *ds, dmu_objset_type_t type, objset_t **osp) 3515367Sahrens { 3525367Sahrens objset_t *os; 3535367Sahrens int err; 3545367Sahrens 3555367Sahrens os = kmem_alloc(sizeof (objset_t), KM_SLEEP); 3565367Sahrens err = dmu_objset_open_ds_os(ds, os, type); 3575367Sahrens if (err) 3585367Sahrens kmem_free(os, sizeof (objset_t)); 3595367Sahrens else 3605367Sahrens *osp = os; 3615367Sahrens return (err); 3625367Sahrens } 3635367Sahrens 364789Sahrens /* called from zpl */ 365789Sahrens int 366789Sahrens dmu_objset_open(const char *name, dmu_objset_type_t type, int mode, 367789Sahrens objset_t **osp) 368789Sahrens { 3695326Sek110237 objset_t *os; 370789Sahrens dsl_dataset_t *ds; 371789Sahrens int err; 372789Sahrens 3736689Smaybee ASSERT(DS_MODE_TYPE(mode) == DS_MODE_USER || 3746689Smaybee DS_MODE_TYPE(mode) == DS_MODE_OWNER); 3755367Sahrens 376789Sahrens os = kmem_alloc(sizeof (objset_t), KM_SLEEP); 3776689Smaybee if (DS_MODE_TYPE(mode) == DS_MODE_USER) 3786689Smaybee err = dsl_dataset_hold(name, os, &ds); 3796689Smaybee else 3806689Smaybee err = dsl_dataset_own(name, mode, os, &ds); 381789Sahrens if (err) { 382789Sahrens kmem_free(os, sizeof (objset_t)); 383789Sahrens return (err); 384789Sahrens } 385789Sahrens 3865367Sahrens err = dmu_objset_open_ds_os(ds, os, type); 3875367Sahrens if (err) { 3886689Smaybee if (DS_MODE_TYPE(mode) == DS_MODE_USER) 3896689Smaybee dsl_dataset_rele(ds, os); 3906689Smaybee else 3916689Smaybee dsl_dataset_disown(ds, os); 3925367Sahrens kmem_free(os, sizeof (objset_t)); 3935367Sahrens } else { 3946689Smaybee os->os_mode = mode; 3955367Sahrens *osp = os; 396789Sahrens } 3975367Sahrens return (err); 398789Sahrens } 399789Sahrens 400789Sahrens void 401789Sahrens dmu_objset_close(objset_t *os) 402789Sahrens { 4036689Smaybee ASSERT(DS_MODE_TYPE(os->os_mode) == DS_MODE_USER || 4046689Smaybee DS_MODE_TYPE(os->os_mode) == DS_MODE_OWNER || 4056689Smaybee DS_MODE_TYPE(os->os_mode) == DS_MODE_NOHOLD); 4066689Smaybee 4076689Smaybee if (DS_MODE_TYPE(os->os_mode) == DS_MODE_USER) 4086689Smaybee dsl_dataset_rele(os->os->os_dsl_dataset, os); 4096689Smaybee else if (DS_MODE_TYPE(os->os_mode) == DS_MODE_OWNER) 4106689Smaybee dsl_dataset_disown(os->os->os_dsl_dataset, os); 411789Sahrens kmem_free(os, sizeof (objset_t)); 412789Sahrens } 413789Sahrens 4141646Sperrin int 4154944Smaybee dmu_objset_evict_dbufs(objset_t *os) 4161544Seschrock { 4171544Seschrock objset_impl_t *osi = os->os; 4181544Seschrock dnode_t *dn; 4191596Sahrens 4201596Sahrens mutex_enter(&osi->os_lock); 4211596Sahrens 4221596Sahrens /* process the mdn last, since the other dnodes have holds on it */ 4231596Sahrens list_remove(&osi->os_dnodes, osi->os_meta_dnode); 4241596Sahrens list_insert_tail(&osi->os_dnodes, osi->os_meta_dnode); 4251544Seschrock 4261544Seschrock /* 4271596Sahrens * Find the first dnode with holds. We have to do this dance 4281596Sahrens * because dnode_add_ref() only works if you already have a 4291596Sahrens * hold. If there are no holds then it has no dbufs so OK to 4301596Sahrens * skip. 4311544Seschrock */ 4321596Sahrens for (dn = list_head(&osi->os_dnodes); 4334944Smaybee dn && !dnode_add_ref(dn, FTAG); 4341596Sahrens dn = list_next(&osi->os_dnodes, dn)) 4351596Sahrens continue; 4361596Sahrens 4371596Sahrens while (dn) { 4381596Sahrens dnode_t *next_dn = dn; 4391596Sahrens 4401596Sahrens do { 4411596Sahrens next_dn = list_next(&osi->os_dnodes, next_dn); 4424944Smaybee } while (next_dn && !dnode_add_ref(next_dn, FTAG)); 4431596Sahrens 4441596Sahrens mutex_exit(&osi->os_lock); 4454944Smaybee dnode_evict_dbufs(dn); 4461596Sahrens dnode_rele(dn, FTAG); 4471596Sahrens mutex_enter(&osi->os_lock); 4481596Sahrens dn = next_dn; 4491544Seschrock } 4501544Seschrock mutex_exit(&osi->os_lock); 4514944Smaybee return (list_head(&osi->os_dnodes) != osi->os_meta_dnode); 4521544Seschrock } 4531544Seschrock 4541544Seschrock void 455789Sahrens dmu_objset_evict(dsl_dataset_t *ds, void *arg) 456789Sahrens { 457789Sahrens objset_impl_t *osi = arg; 4581544Seschrock objset_t os; 4592082Seschrock int i; 460789Sahrens 461789Sahrens for (i = 0; i < TXG_SIZE; i++) { 462789Sahrens ASSERT(list_head(&osi->os_dirty_dnodes[i]) == NULL); 463789Sahrens ASSERT(list_head(&osi->os_free_dnodes[i]) == NULL); 464789Sahrens } 465789Sahrens 4667237Sek110237 if (ds) { 4677237Sek110237 if (!dsl_dataset_is_snapshot(ds)) { 4687237Sek110237 VERIFY(0 == dsl_prop_unregister(ds, "checksum", 4697237Sek110237 checksum_changed_cb, osi)); 4707237Sek110237 VERIFY(0 == dsl_prop_unregister(ds, "compression", 4717237Sek110237 compression_changed_cb, osi)); 4727237Sek110237 VERIFY(0 == dsl_prop_unregister(ds, "copies", 4737237Sek110237 copies_changed_cb, osi)); 4747237Sek110237 } 4757237Sek110237 VERIFY(0 == dsl_prop_unregister(ds, "primarycache", 4767237Sek110237 primary_cache_changed_cb, osi)); 4777237Sek110237 VERIFY(0 == dsl_prop_unregister(ds, "secondarycache", 4787237Sek110237 secondary_cache_changed_cb, osi)); 479789Sahrens } 480789Sahrens 4811544Seschrock /* 4821544Seschrock * We should need only a single pass over the dnode list, since 4831544Seschrock * nothing can be added to the list at this point. 4841544Seschrock */ 4851544Seschrock os.os = osi; 4864944Smaybee (void) dmu_objset_evict_dbufs(&os); 4871544Seschrock 4889396SMatthew.Ahrens@Sun.COM dnode_special_close(osi->os_meta_dnode); 4899396SMatthew.Ahrens@Sun.COM if (osi->os_userused_dnode) { 4909396SMatthew.Ahrens@Sun.COM dnode_special_close(osi->os_userused_dnode); 4919396SMatthew.Ahrens@Sun.COM dnode_special_close(osi->os_groupused_dnode); 4929396SMatthew.Ahrens@Sun.COM } 4939396SMatthew.Ahrens@Sun.COM zil_free(osi->os_zil); 494789Sahrens 4959396SMatthew.Ahrens@Sun.COM ASSERT3P(list_head(&osi->os_dnodes), ==, NULL); 496789Sahrens 4973547Smaybee VERIFY(arc_buf_remove_ref(osi->os_phys_buf, &osi->os_phys_buf) == 1); 4982856Snd150628 mutex_destroy(&osi->os_lock); 4992856Snd150628 mutex_destroy(&osi->os_obj_lock); 5005326Sek110237 mutex_destroy(&osi->os_user_ptr_lock); 501789Sahrens kmem_free(osi, sizeof (objset_impl_t)); 502789Sahrens } 503789Sahrens 504789Sahrens /* called from dsl for meta-objset */ 505789Sahrens objset_impl_t * 5063547Smaybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, 5073547Smaybee dmu_objset_type_t type, dmu_tx_t *tx) 508789Sahrens { 509789Sahrens objset_impl_t *osi; 510789Sahrens dnode_t *mdn; 511789Sahrens 512789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 5134787Sahrens if (ds) 5144787Sahrens mutex_enter(&ds->ds_opening_lock); 5153547Smaybee VERIFY(0 == dmu_objset_open_impl(spa, ds, bp, &osi)); 5164787Sahrens if (ds) 5174787Sahrens mutex_exit(&ds->ds_opening_lock); 518789Sahrens mdn = osi->os_meta_dnode; 519789Sahrens 520789Sahrens dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT, 521789Sahrens DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx); 522789Sahrens 523789Sahrens /* 524789Sahrens * We don't want to have to increase the meta-dnode's nlevels 525789Sahrens * later, because then we could do it in quescing context while 526789Sahrens * we are also accessing it in open context. 527789Sahrens * 528789Sahrens * This precaution is not necessary for the MOS (ds == NULL), 529789Sahrens * because the MOS is only updated in syncing context. 530789Sahrens * This is most fortunate: the MOS is the only objset that 531789Sahrens * needs to be synced multiple times as spa_sync() iterates 532789Sahrens * to convergence, so minimizing its dn_nlevels matters. 533789Sahrens */ 5341544Seschrock if (ds != NULL) { 5351544Seschrock int levels = 1; 5361544Seschrock 5371544Seschrock /* 5381544Seschrock * Determine the number of levels necessary for the meta-dnode 5391544Seschrock * to contain DN_MAX_OBJECT dnodes. 5401544Seschrock */ 5411544Seschrock while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift + 5421544Seschrock (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) < 5431544Seschrock DN_MAX_OBJECT * sizeof (dnode_phys_t)) 5441544Seschrock levels++; 5451544Seschrock 546789Sahrens mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] = 5471544Seschrock mdn->dn_nlevels = levels; 5481544Seschrock } 549789Sahrens 550789Sahrens ASSERT(type != DMU_OST_NONE); 551789Sahrens ASSERT(type != DMU_OST_ANY); 552789Sahrens ASSERT(type < DMU_OST_NUMTYPES); 553789Sahrens osi->os_phys->os_type = type; 5549396SMatthew.Ahrens@Sun.COM if (dmu_objset_userused_enabled(osi)) { 5559396SMatthew.Ahrens@Sun.COM osi->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE; 5569396SMatthew.Ahrens@Sun.COM osi->os_flags = osi->os_phys->os_flags; 5579396SMatthew.Ahrens@Sun.COM } 558789Sahrens 559789Sahrens dsl_dataset_dirty(ds, tx); 560789Sahrens 561789Sahrens return (osi); 562789Sahrens } 563789Sahrens 564789Sahrens struct oscarg { 5654543Smarks void (*userfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx); 566789Sahrens void *userarg; 567789Sahrens dsl_dataset_t *clone_parent; 568789Sahrens const char *lastname; 569789Sahrens dmu_objset_type_t type; 5706492Stimh uint64_t flags; 571789Sahrens }; 572789Sahrens 5734543Smarks /*ARGSUSED*/ 574789Sahrens static int 5752199Sahrens dmu_objset_create_check(void *arg1, void *arg2, dmu_tx_t *tx) 576789Sahrens { 5772199Sahrens dsl_dir_t *dd = arg1; 5782199Sahrens struct oscarg *oa = arg2; 5792199Sahrens objset_t *mos = dd->dd_pool->dp_meta_objset; 5802199Sahrens int err; 5812199Sahrens uint64_t ddobj; 5822199Sahrens 5832199Sahrens err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj, 5842199Sahrens oa->lastname, sizeof (uint64_t), 1, &ddobj); 5852199Sahrens if (err != ENOENT) 5862199Sahrens return (err ? err : EEXIST); 5872199Sahrens 5882199Sahrens if (oa->clone_parent != NULL) { 5892199Sahrens /* 5902199Sahrens * You can't clone across pools. 5912199Sahrens */ 5922199Sahrens if (oa->clone_parent->ds_dir->dd_pool != dd->dd_pool) 5932199Sahrens return (EXDEV); 5942199Sahrens 5952199Sahrens /* 5962199Sahrens * You can only clone snapshots, not the head datasets. 5972199Sahrens */ 5982199Sahrens if (oa->clone_parent->ds_phys->ds_num_children == 0) 5992199Sahrens return (EINVAL); 6002199Sahrens } 6014543Smarks 6022199Sahrens return (0); 6032199Sahrens } 6042199Sahrens 6052199Sahrens static void 6064543Smarks dmu_objset_create_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 6072199Sahrens { 6082199Sahrens dsl_dir_t *dd = arg1; 6092199Sahrens struct oscarg *oa = arg2; 610789Sahrens dsl_dataset_t *ds; 6113547Smaybee blkptr_t *bp; 6122199Sahrens uint64_t dsobj; 613789Sahrens 614789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 615789Sahrens 6162199Sahrens dsobj = dsl_dataset_create_sync(dd, oa->lastname, 6176492Stimh oa->clone_parent, oa->flags, cr, tx); 618789Sahrens 6196689Smaybee VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, dsobj, FTAG, &ds)); 6203547Smaybee bp = dsl_dataset_get_blkptr(ds); 6213547Smaybee if (BP_IS_HOLE(bp)) { 622789Sahrens objset_impl_t *osi; 623789Sahrens 624789Sahrens /* This is an empty dmu_objset; not a clone. */ 625789Sahrens osi = dmu_objset_create_impl(dsl_dataset_get_spa(ds), 6263547Smaybee ds, bp, oa->type, tx); 627789Sahrens 628789Sahrens if (oa->userfunc) 6294543Smarks oa->userfunc(&osi->os, oa->userarg, cr, tx); 630789Sahrens } 6314543Smarks 6324543Smarks spa_history_internal_log(LOG_DS_CREATE, dd->dd_pool->dp_spa, 6334543Smarks tx, cr, "dataset = %llu", dsobj); 6344543Smarks 6356689Smaybee dsl_dataset_rele(ds, FTAG); 636789Sahrens } 637789Sahrens 638789Sahrens int 639789Sahrens dmu_objset_create(const char *name, dmu_objset_type_t type, 6406492Stimh objset_t *clone_parent, uint64_t flags, 6414543Smarks void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg) 642789Sahrens { 6432199Sahrens dsl_dir_t *pdd; 644789Sahrens const char *tail; 645789Sahrens int err = 0; 6462199Sahrens struct oscarg oa = { 0 }; 647789Sahrens 6482199Sahrens ASSERT(strchr(name, '@') == NULL); 6492199Sahrens err = dsl_dir_open(name, FTAG, &pdd, &tail); 6501544Seschrock if (err) 6511544Seschrock return (err); 652789Sahrens if (tail == NULL) { 6532199Sahrens dsl_dir_close(pdd, FTAG); 654789Sahrens return (EEXIST); 655789Sahrens } 656789Sahrens 657789Sahrens dprintf("name=%s\n", name); 658789Sahrens 6592199Sahrens oa.userfunc = func; 6602199Sahrens oa.userarg = arg; 6612199Sahrens oa.lastname = tail; 6622199Sahrens oa.type = type; 6636492Stimh oa.flags = flags; 6644543Smarks 6652199Sahrens if (clone_parent != NULL) { 666789Sahrens /* 6672199Sahrens * You can't clone to a different type. 668789Sahrens */ 6692199Sahrens if (clone_parent->os->os_phys->os_type != type) { 6702199Sahrens dsl_dir_close(pdd, FTAG); 6712199Sahrens return (EINVAL); 672789Sahrens } 6732199Sahrens oa.clone_parent = clone_parent->os->os_dsl_dataset; 674789Sahrens } 6752199Sahrens err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check, 6762199Sahrens dmu_objset_create_sync, pdd, &oa, 5); 6772199Sahrens dsl_dir_close(pdd, FTAG); 678789Sahrens return (err); 679789Sahrens } 680789Sahrens 681789Sahrens int 682*10242Schris.kirby@sun.com dmu_objset_destroy(const char *name, boolean_t defer) 683789Sahrens { 684789Sahrens objset_t *os; 685789Sahrens int error; 686789Sahrens 687789Sahrens /* 688789Sahrens * If it looks like we'll be able to destroy it, and there's 689789Sahrens * an unplayed replay log sitting around, destroy the log. 690789Sahrens * It would be nicer to do this in dsl_dataset_destroy_sync(), 691789Sahrens * but the replay log objset is modified in open context. 692789Sahrens */ 6935367Sahrens error = dmu_objset_open(name, DMU_OST_ANY, 6946689Smaybee DS_MODE_OWNER|DS_MODE_READONLY|DS_MODE_INCONSISTENT, &os); 695789Sahrens if (error == 0) { 6965367Sahrens dsl_dataset_t *ds = os->os->os_dsl_dataset; 6971807Sbonwick zil_destroy(dmu_objset_zil(os), B_FALSE); 6985367Sahrens 699*10242Schris.kirby@sun.com error = dsl_dataset_destroy(ds, os, defer); 7005367Sahrens /* 7015367Sahrens * dsl_dataset_destroy() closes the ds. 7025367Sahrens */ 7035367Sahrens kmem_free(os, sizeof (objset_t)); 704789Sahrens } 705789Sahrens 7065367Sahrens return (error); 707789Sahrens } 708789Sahrens 7095446Sahrens /* 7105446Sahrens * This will close the objset. 7115446Sahrens */ 712789Sahrens int 7135446Sahrens dmu_objset_rollback(objset_t *os) 714789Sahrens { 715789Sahrens int err; 7165367Sahrens dsl_dataset_t *ds; 717789Sahrens 7185446Sahrens ds = os->os->os_dsl_dataset; 7194935Sperrin 7206689Smaybee if (!dsl_dataset_tryown(ds, TRUE, os)) { 7215446Sahrens dmu_objset_close(os); 7225446Sahrens return (EBUSY); 7235446Sahrens } 7245446Sahrens 7255367Sahrens err = dsl_dataset_rollback(ds, os->os->os_phys->os_type); 7264935Sperrin 7275367Sahrens /* 7285367Sahrens * NB: we close the objset manually because the rollback 7295367Sahrens * actually implicitly called dmu_objset_evict(), thus freeing 7305367Sahrens * the objset_impl_t. 7315367Sahrens */ 7326689Smaybee dsl_dataset_disown(ds, os); 7335367Sahrens kmem_free(os, sizeof (objset_t)); 734789Sahrens return (err); 735789Sahrens } 736789Sahrens 7372199Sahrens struct snaparg { 7382199Sahrens dsl_sync_task_group_t *dstg; 7392199Sahrens char *snapname; 7402199Sahrens char failed[MAXPATHLEN]; 7414543Smarks boolean_t checkperms; 7429355SMatthew.Ahrens@Sun.COM nvlist_t *props; 7435367Sahrens }; 7445367Sahrens 7459355SMatthew.Ahrens@Sun.COM static int 7469355SMatthew.Ahrens@Sun.COM snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx) 7479355SMatthew.Ahrens@Sun.COM { 7489355SMatthew.Ahrens@Sun.COM objset_t *os = arg1; 7499355SMatthew.Ahrens@Sun.COM struct snaparg *sn = arg2; 7509355SMatthew.Ahrens@Sun.COM 7519355SMatthew.Ahrens@Sun.COM /* The props have already been checked by zfs_check_userprops(). */ 7529355SMatthew.Ahrens@Sun.COM 7539355SMatthew.Ahrens@Sun.COM return (dsl_dataset_snapshot_check(os->os->os_dsl_dataset, 7549355SMatthew.Ahrens@Sun.COM sn->snapname, tx)); 7559355SMatthew.Ahrens@Sun.COM } 7569355SMatthew.Ahrens@Sun.COM 7579355SMatthew.Ahrens@Sun.COM static void 7589355SMatthew.Ahrens@Sun.COM snapshot_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 7599355SMatthew.Ahrens@Sun.COM { 7609355SMatthew.Ahrens@Sun.COM objset_t *os = arg1; 7619355SMatthew.Ahrens@Sun.COM dsl_dataset_t *ds = os->os->os_dsl_dataset; 7629355SMatthew.Ahrens@Sun.COM struct snaparg *sn = arg2; 7639355SMatthew.Ahrens@Sun.COM 7649355SMatthew.Ahrens@Sun.COM dsl_dataset_snapshot_sync(ds, sn->snapname, cr, tx); 7659355SMatthew.Ahrens@Sun.COM 7669355SMatthew.Ahrens@Sun.COM if (sn->props) 7679355SMatthew.Ahrens@Sun.COM dsl_props_set_sync(ds->ds_prev, sn->props, cr, tx); 7689355SMatthew.Ahrens@Sun.COM } 7692199Sahrens 7702199Sahrens static int 7712199Sahrens dmu_objset_snapshot_one(char *name, void *arg) 7722199Sahrens { 7732199Sahrens struct snaparg *sn = arg; 7742199Sahrens objset_t *os; 7752199Sahrens int err; 7762199Sahrens 7772199Sahrens (void) strcpy(sn->failed, name); 7782199Sahrens 7794543Smarks /* 7804543Smarks * Check permissions only when requested. This only applies when 7814543Smarks * doing a recursive snapshot. The permission checks for the starting 7824543Smarks * dataset have already been performed in zfs_secpolicy_snapshot() 7834543Smarks */ 7844543Smarks if (sn->checkperms == B_TRUE && 7854543Smarks (err = zfs_secpolicy_snapshot_perms(name, CRED()))) 7864543Smarks return (err); 7874543Smarks 7886689Smaybee err = dmu_objset_open(name, DMU_OST_ANY, DS_MODE_USER, &os); 7892199Sahrens if (err != 0) 7902199Sahrens return (err); 7912199Sahrens 7926689Smaybee /* If the objset is in an inconsistent state, return busy */ 7936689Smaybee if (os->os->os_dsl_dataset->ds_phys->ds_flags & DS_FLAG_INCONSISTENT) { 7943637Srm160521 dmu_objset_close(os); 7953637Srm160521 return (EBUSY); 7963637Srm160521 } 7973637Srm160521 7983637Srm160521 /* 7992199Sahrens * NB: we need to wait for all in-flight changes to get to disk, 8002199Sahrens * so that we snapshot those changes. zil_suspend does this as 8012199Sahrens * a side effect. 8022199Sahrens */ 8032199Sahrens err = zil_suspend(dmu_objset_zil(os)); 8042199Sahrens if (err == 0) { 8059355SMatthew.Ahrens@Sun.COM dsl_sync_task_create(sn->dstg, snapshot_check, 8069355SMatthew.Ahrens@Sun.COM snapshot_sync, os, sn, 3); 8073637Srm160521 } else { 8083637Srm160521 dmu_objset_close(os); 8092199Sahrens } 8103637Srm160521 8112199Sahrens return (err); 8122199Sahrens } 8132199Sahrens 8142199Sahrens int 8159355SMatthew.Ahrens@Sun.COM dmu_objset_snapshot(char *fsname, char *snapname, 8169355SMatthew.Ahrens@Sun.COM nvlist_t *props, boolean_t recursive) 8172199Sahrens { 8182199Sahrens dsl_sync_task_t *dst; 8199355SMatthew.Ahrens@Sun.COM struct snaparg sn; 8202199Sahrens spa_t *spa; 8212199Sahrens int err; 8222199Sahrens 8232199Sahrens (void) strcpy(sn.failed, fsname); 8242199Sahrens 8254603Sahrens err = spa_open(fsname, &spa, FTAG); 8262199Sahrens if (err) 8272199Sahrens return (err); 8282199Sahrens 8292199Sahrens sn.dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 8302199Sahrens sn.snapname = snapname; 8319355SMatthew.Ahrens@Sun.COM sn.props = props; 8322199Sahrens 8332417Sahrens if (recursive) { 8344543Smarks sn.checkperms = B_TRUE; 8352417Sahrens err = dmu_objset_find(fsname, 8362417Sahrens dmu_objset_snapshot_one, &sn, DS_FIND_CHILDREN); 8372417Sahrens } else { 8384543Smarks sn.checkperms = B_FALSE; 8392199Sahrens err = dmu_objset_snapshot_one(fsname, &sn); 8402417Sahrens } 8412199Sahrens 8429355SMatthew.Ahrens@Sun.COM if (err == 0) 8439355SMatthew.Ahrens@Sun.COM err = dsl_sync_task_group_wait(sn.dstg); 8442199Sahrens 8452199Sahrens for (dst = list_head(&sn.dstg->dstg_tasks); dst; 8462199Sahrens dst = list_next(&sn.dstg->dstg_tasks, dst)) { 8479355SMatthew.Ahrens@Sun.COM objset_t *os = dst->dst_arg1; 8489355SMatthew.Ahrens@Sun.COM dsl_dataset_t *ds = os->os->os_dsl_dataset; 8492199Sahrens if (dst->dst_err) 8505367Sahrens dsl_dataset_name(ds, sn.failed); 8519355SMatthew.Ahrens@Sun.COM zil_resume(dmu_objset_zil(os)); 8529355SMatthew.Ahrens@Sun.COM dmu_objset_close(os); 8532199Sahrens } 8545367Sahrens 8552199Sahrens if (err) 8562199Sahrens (void) strcpy(fsname, sn.failed); 8572199Sahrens dsl_sync_task_group_destroy(sn.dstg); 8582199Sahrens spa_close(spa, FTAG); 8592199Sahrens return (err); 8602199Sahrens } 8612199Sahrens 862789Sahrens static void 8639396SMatthew.Ahrens@Sun.COM dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx) 864789Sahrens { 8653547Smaybee dnode_t *dn; 866789Sahrens 8673547Smaybee while (dn = list_head(list)) { 8683547Smaybee ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT); 8693547Smaybee ASSERT(dn->dn_dbuf->db_data_pending); 8703547Smaybee /* 8719396SMatthew.Ahrens@Sun.COM * Initialize dn_zio outside dnode_sync() because the 8729396SMatthew.Ahrens@Sun.COM * meta-dnode needs to set it ouside dnode_sync(). 8733547Smaybee */ 8743547Smaybee dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio; 8753547Smaybee ASSERT(dn->dn_zio); 876789Sahrens 8773547Smaybee ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS); 8783547Smaybee list_remove(list, dn); 8799396SMatthew.Ahrens@Sun.COM 8809396SMatthew.Ahrens@Sun.COM if (newlist) { 8819396SMatthew.Ahrens@Sun.COM (void) dnode_add_ref(dn, newlist); 8829396SMatthew.Ahrens@Sun.COM list_insert_tail(newlist, dn); 8839396SMatthew.Ahrens@Sun.COM } 8849396SMatthew.Ahrens@Sun.COM 8853547Smaybee dnode_sync(dn, tx); 8863547Smaybee } 8873547Smaybee } 8882981Sahrens 8893547Smaybee /* ARGSUSED */ 8903547Smaybee static void 8913547Smaybee ready(zio_t *zio, arc_buf_t *abuf, void *arg) 8923547Smaybee { 8937754SJeff.Bonwick@Sun.COM blkptr_t *bp = zio->io_bp; 8947754SJeff.Bonwick@Sun.COM blkptr_t *bp_orig = &zio->io_bp_orig; 8953547Smaybee objset_impl_t *os = arg; 8963547Smaybee dnode_phys_t *dnp = &os->os_phys->os_meta_dnode; 8972981Sahrens 8987754SJeff.Bonwick@Sun.COM ASSERT(bp == os->os_rootbp); 8997754SJeff.Bonwick@Sun.COM ASSERT(BP_GET_TYPE(bp) == DMU_OT_OBJSET); 9007754SJeff.Bonwick@Sun.COM ASSERT(BP_GET_LEVEL(bp) == 0); 9015329Sgw25295 9023547Smaybee /* 9039396SMatthew.Ahrens@Sun.COM * Update rootbp fill count: it should be the number of objects 9049396SMatthew.Ahrens@Sun.COM * allocated in the object set (not counting the "special" 9059396SMatthew.Ahrens@Sun.COM * objects that are stored in the objset_phys_t -- the meta 9069396SMatthew.Ahrens@Sun.COM * dnode and user/group accounting objects). 9073547Smaybee */ 9089396SMatthew.Ahrens@Sun.COM bp->blk_fill = 0; 9097754SJeff.Bonwick@Sun.COM for (int i = 0; i < dnp->dn_nblkptr; i++) 9103547Smaybee bp->blk_fill += dnp->dn_blkptr[i].blk_fill; 9115329Sgw25295 9127754SJeff.Bonwick@Sun.COM if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { 9137754SJeff.Bonwick@Sun.COM ASSERT(DVA_EQUAL(BP_IDENTITY(bp), BP_IDENTITY(bp_orig))); 9147754SJeff.Bonwick@Sun.COM } else { 9155329Sgw25295 if (zio->io_bp_orig.blk_birth == os->os_synctx->tx_txg) 9166992Smaybee (void) dsl_dataset_block_kill(os->os_dsl_dataset, 9177754SJeff.Bonwick@Sun.COM &zio->io_bp_orig, zio, os->os_synctx); 9185329Sgw25295 dsl_dataset_block_born(os->os_dsl_dataset, bp, os->os_synctx); 9195329Sgw25295 } 920789Sahrens } 921789Sahrens 922789Sahrens /* called from dsl */ 923789Sahrens void 9243547Smaybee dmu_objset_sync(objset_impl_t *os, zio_t *pio, dmu_tx_t *tx) 925789Sahrens { 926789Sahrens int txgoff; 9271544Seschrock zbookmark_t zb; 9287046Sahrens writeprops_t wp = { 0 }; 9293547Smaybee zio_t *zio; 9303547Smaybee list_t *list; 9319396SMatthew.Ahrens@Sun.COM list_t *newlist = NULL; 9323547Smaybee dbuf_dirty_record_t *dr; 9333547Smaybee 9343547Smaybee dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg); 935789Sahrens 936789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 937789Sahrens /* XXX the write_done callback should really give us the tx... */ 938789Sahrens os->os_synctx = tx; 939789Sahrens 9403882Sahrens if (os->os_dsl_dataset == NULL) { 9413882Sahrens /* 9423882Sahrens * This is the MOS. If we have upgraded, 9433882Sahrens * spa_max_replication() could change, so reset 9443882Sahrens * os_copies here. 9453882Sahrens */ 9463882Sahrens os->os_copies = spa_max_replication(os->os_spa); 9473882Sahrens } 9483882Sahrens 9493547Smaybee /* 9503547Smaybee * Create the root block IO 9513547Smaybee */ 9523547Smaybee zb.zb_objset = os->os_dsl_dataset ? os->os_dsl_dataset->ds_object : 0; 9533547Smaybee zb.zb_object = 0; 9547754SJeff.Bonwick@Sun.COM zb.zb_level = -1; /* for block ordering; it's level 0 on disk */ 9553547Smaybee zb.zb_blkid = 0; 9567754SJeff.Bonwick@Sun.COM 9577754SJeff.Bonwick@Sun.COM wp.wp_type = DMU_OT_OBJSET; 9587754SJeff.Bonwick@Sun.COM wp.wp_level = 0; /* on-disk BP level; see above */ 9597754SJeff.Bonwick@Sun.COM wp.wp_copies = os->os_copies; 9607754SJeff.Bonwick@Sun.COM wp.wp_oschecksum = os->os_checksum; 9617754SJeff.Bonwick@Sun.COM wp.wp_oscompress = os->os_compress; 9627754SJeff.Bonwick@Sun.COM 9634787Sahrens if (BP_IS_OLDER(os->os_rootbp, tx->tx_txg)) { 9646992Smaybee (void) dsl_dataset_block_kill(os->os_dsl_dataset, 9653547Smaybee os->os_rootbp, pio, tx); 9664787Sahrens } 9677754SJeff.Bonwick@Sun.COM 9687046Sahrens arc_release(os->os_phys_buf, &os->os_phys_buf); 9699396SMatthew.Ahrens@Sun.COM 9707754SJeff.Bonwick@Sun.COM zio = arc_write(pio, os->os_spa, &wp, DMU_OS_IS_L2CACHEABLE(os), 9717754SJeff.Bonwick@Sun.COM tx->tx_txg, os->os_rootbp, os->os_phys_buf, ready, NULL, os, 9727754SJeff.Bonwick@Sun.COM ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb); 9733547Smaybee 9743547Smaybee /* 9759396SMatthew.Ahrens@Sun.COM * Sync special dnodes - the parent IO for the sync is the root block 9763547Smaybee */ 9773547Smaybee os->os_meta_dnode->dn_zio = zio; 9783547Smaybee dnode_sync(os->os_meta_dnode, tx); 979789Sahrens 9809396SMatthew.Ahrens@Sun.COM os->os_phys->os_flags = os->os_flags; 9819396SMatthew.Ahrens@Sun.COM 9829396SMatthew.Ahrens@Sun.COM if (os->os_userused_dnode && 9839396SMatthew.Ahrens@Sun.COM os->os_userused_dnode->dn_type != DMU_OT_NONE) { 9849396SMatthew.Ahrens@Sun.COM os->os_userused_dnode->dn_zio = zio; 9859396SMatthew.Ahrens@Sun.COM dnode_sync(os->os_userused_dnode, tx); 9869396SMatthew.Ahrens@Sun.COM os->os_groupused_dnode->dn_zio = zio; 9879396SMatthew.Ahrens@Sun.COM dnode_sync(os->os_groupused_dnode, tx); 9889396SMatthew.Ahrens@Sun.COM } 9899396SMatthew.Ahrens@Sun.COM 990789Sahrens txgoff = tx->tx_txg & TXG_MASK; 991789Sahrens 9929396SMatthew.Ahrens@Sun.COM if (dmu_objset_userused_enabled(os)) { 9939396SMatthew.Ahrens@Sun.COM newlist = &os->os_synced_dnodes; 9949396SMatthew.Ahrens@Sun.COM /* 9959396SMatthew.Ahrens@Sun.COM * We must create the list here because it uses the 9969396SMatthew.Ahrens@Sun.COM * dn_dirty_link[] of this txg. 9979396SMatthew.Ahrens@Sun.COM */ 9989396SMatthew.Ahrens@Sun.COM list_create(newlist, sizeof (dnode_t), 9999396SMatthew.Ahrens@Sun.COM offsetof(dnode_t, dn_dirty_link[txgoff])); 10009396SMatthew.Ahrens@Sun.COM } 10019396SMatthew.Ahrens@Sun.COM 10029396SMatthew.Ahrens@Sun.COM dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx); 10039396SMatthew.Ahrens@Sun.COM dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx); 1004789Sahrens 10053547Smaybee list = &os->os_meta_dnode->dn_dirty_records[txgoff]; 10063547Smaybee while (dr = list_head(list)) { 10073547Smaybee ASSERT(dr->dr_dbuf->db_level == 0); 10083547Smaybee list_remove(list, dr); 10093547Smaybee if (dr->dr_zio) 10103547Smaybee zio_nowait(dr->dr_zio); 10113547Smaybee } 1012789Sahrens /* 1013789Sahrens * Free intent log blocks up to this tx. 1014789Sahrens */ 1015789Sahrens zil_sync(os->os_zil, tx); 10167046Sahrens os->os_phys->os_zil_header = os->os_zil_header; 10173547Smaybee zio_nowait(zio); 1018789Sahrens } 1019789Sahrens 10209396SMatthew.Ahrens@Sun.COM static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES]; 10219396SMatthew.Ahrens@Sun.COM 10229396SMatthew.Ahrens@Sun.COM void 10239396SMatthew.Ahrens@Sun.COM dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb) 10249396SMatthew.Ahrens@Sun.COM { 10259396SMatthew.Ahrens@Sun.COM used_cbs[ost] = cb; 10269396SMatthew.Ahrens@Sun.COM } 10279396SMatthew.Ahrens@Sun.COM 10289396SMatthew.Ahrens@Sun.COM boolean_t 10299396SMatthew.Ahrens@Sun.COM dmu_objset_userused_enabled(objset_impl_t *os) 10309396SMatthew.Ahrens@Sun.COM { 10319396SMatthew.Ahrens@Sun.COM return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE && 10329396SMatthew.Ahrens@Sun.COM used_cbs[os->os_phys->os_type] && 10339396SMatthew.Ahrens@Sun.COM os->os_userused_dnode); 10349396SMatthew.Ahrens@Sun.COM } 10359396SMatthew.Ahrens@Sun.COM 10369396SMatthew.Ahrens@Sun.COM void 10379396SMatthew.Ahrens@Sun.COM dmu_objset_do_userquota_callbacks(objset_impl_t *os, dmu_tx_t *tx) 10389396SMatthew.Ahrens@Sun.COM { 10399396SMatthew.Ahrens@Sun.COM dnode_t *dn; 10409396SMatthew.Ahrens@Sun.COM list_t *list = &os->os_synced_dnodes; 10419396SMatthew.Ahrens@Sun.COM static const char zerobuf[DN_MAX_BONUSLEN] = {0}; 10429396SMatthew.Ahrens@Sun.COM 10439396SMatthew.Ahrens@Sun.COM ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os)); 10449396SMatthew.Ahrens@Sun.COM 10459396SMatthew.Ahrens@Sun.COM while (dn = list_head(list)) { 10469396SMatthew.Ahrens@Sun.COM dmu_object_type_t bonustype; 10479396SMatthew.Ahrens@Sun.COM 10489396SMatthew.Ahrens@Sun.COM ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object)); 10499396SMatthew.Ahrens@Sun.COM ASSERT(dn->dn_oldphys); 10509396SMatthew.Ahrens@Sun.COM ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE || 10519396SMatthew.Ahrens@Sun.COM dn->dn_phys->dn_flags & 10529396SMatthew.Ahrens@Sun.COM DNODE_FLAG_USERUSED_ACCOUNTED); 10539396SMatthew.Ahrens@Sun.COM 10549396SMatthew.Ahrens@Sun.COM /* Allocate the user/groupused objects if necessary. */ 10559396SMatthew.Ahrens@Sun.COM if (os->os_userused_dnode->dn_type == DMU_OT_NONE) { 10569396SMatthew.Ahrens@Sun.COM VERIFY(0 == zap_create_claim(&os->os, 10579396SMatthew.Ahrens@Sun.COM DMU_USERUSED_OBJECT, 10589396SMatthew.Ahrens@Sun.COM DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx)); 10599396SMatthew.Ahrens@Sun.COM VERIFY(0 == zap_create_claim(&os->os, 10609396SMatthew.Ahrens@Sun.COM DMU_GROUPUSED_OBJECT, 10619396SMatthew.Ahrens@Sun.COM DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx)); 10629396SMatthew.Ahrens@Sun.COM } 10639396SMatthew.Ahrens@Sun.COM 10649396SMatthew.Ahrens@Sun.COM /* 10659396SMatthew.Ahrens@Sun.COM * If the object was not previously 10669396SMatthew.Ahrens@Sun.COM * accounted, pretend that it was free. 10679396SMatthew.Ahrens@Sun.COM */ 10689396SMatthew.Ahrens@Sun.COM if (!(dn->dn_oldphys->dn_flags & 10699396SMatthew.Ahrens@Sun.COM DNODE_FLAG_USERUSED_ACCOUNTED)) { 10709396SMatthew.Ahrens@Sun.COM bzero(dn->dn_oldphys, sizeof (dnode_phys_t)); 10719396SMatthew.Ahrens@Sun.COM } 10729396SMatthew.Ahrens@Sun.COM 10739396SMatthew.Ahrens@Sun.COM /* 10749396SMatthew.Ahrens@Sun.COM * If the object was freed, use the previous bonustype. 10759396SMatthew.Ahrens@Sun.COM */ 10769396SMatthew.Ahrens@Sun.COM bonustype = dn->dn_phys->dn_bonustype ? 10779396SMatthew.Ahrens@Sun.COM dn->dn_phys->dn_bonustype : dn->dn_oldphys->dn_bonustype; 10789396SMatthew.Ahrens@Sun.COM ASSERT(dn->dn_phys->dn_type != 0 || 10799396SMatthew.Ahrens@Sun.COM (bcmp(DN_BONUS(dn->dn_phys), zerobuf, 10809396SMatthew.Ahrens@Sun.COM DN_MAX_BONUSLEN) == 0 && 10819396SMatthew.Ahrens@Sun.COM DN_USED_BYTES(dn->dn_phys) == 0)); 10829396SMatthew.Ahrens@Sun.COM ASSERT(dn->dn_oldphys->dn_type != 0 || 10839396SMatthew.Ahrens@Sun.COM (bcmp(DN_BONUS(dn->dn_oldphys), zerobuf, 10849396SMatthew.Ahrens@Sun.COM DN_MAX_BONUSLEN) == 0 && 10859396SMatthew.Ahrens@Sun.COM DN_USED_BYTES(dn->dn_oldphys) == 0)); 10869396SMatthew.Ahrens@Sun.COM used_cbs[os->os_phys->os_type](&os->os, bonustype, 10879396SMatthew.Ahrens@Sun.COM DN_BONUS(dn->dn_oldphys), DN_BONUS(dn->dn_phys), 10889396SMatthew.Ahrens@Sun.COM DN_USED_BYTES(dn->dn_oldphys), 10899396SMatthew.Ahrens@Sun.COM DN_USED_BYTES(dn->dn_phys), tx); 10909396SMatthew.Ahrens@Sun.COM 10919396SMatthew.Ahrens@Sun.COM /* 10929396SMatthew.Ahrens@Sun.COM * The mutex is needed here for interlock with dnode_allocate. 10939396SMatthew.Ahrens@Sun.COM */ 10949396SMatthew.Ahrens@Sun.COM mutex_enter(&dn->dn_mtx); 10959396SMatthew.Ahrens@Sun.COM zio_buf_free(dn->dn_oldphys, sizeof (dnode_phys_t)); 10969396SMatthew.Ahrens@Sun.COM dn->dn_oldphys = NULL; 10979396SMatthew.Ahrens@Sun.COM mutex_exit(&dn->dn_mtx); 10989396SMatthew.Ahrens@Sun.COM 10999396SMatthew.Ahrens@Sun.COM list_remove(list, dn); 11009396SMatthew.Ahrens@Sun.COM dnode_rele(dn, list); 11019396SMatthew.Ahrens@Sun.COM } 11029396SMatthew.Ahrens@Sun.COM } 11039396SMatthew.Ahrens@Sun.COM 11049396SMatthew.Ahrens@Sun.COM boolean_t 11059396SMatthew.Ahrens@Sun.COM dmu_objset_userspace_present(objset_t *os) 11069396SMatthew.Ahrens@Sun.COM { 11079396SMatthew.Ahrens@Sun.COM return (os->os->os_phys->os_flags & 11089396SMatthew.Ahrens@Sun.COM OBJSET_FLAG_USERACCOUNTING_COMPLETE); 11099396SMatthew.Ahrens@Sun.COM } 11109396SMatthew.Ahrens@Sun.COM 11119396SMatthew.Ahrens@Sun.COM int 11129396SMatthew.Ahrens@Sun.COM dmu_objset_userspace_upgrade(objset_t *os) 11139396SMatthew.Ahrens@Sun.COM { 11149396SMatthew.Ahrens@Sun.COM uint64_t obj; 11159396SMatthew.Ahrens@Sun.COM int err = 0; 11169396SMatthew.Ahrens@Sun.COM 11179396SMatthew.Ahrens@Sun.COM if (dmu_objset_userspace_present(os)) 11189396SMatthew.Ahrens@Sun.COM return (0); 11199396SMatthew.Ahrens@Sun.COM if (!dmu_objset_userused_enabled(os->os)) 11209396SMatthew.Ahrens@Sun.COM return (ENOTSUP); 11219396SMatthew.Ahrens@Sun.COM if (dmu_objset_is_snapshot(os)) 11229396SMatthew.Ahrens@Sun.COM return (EINVAL); 11239396SMatthew.Ahrens@Sun.COM 11249396SMatthew.Ahrens@Sun.COM /* 11259396SMatthew.Ahrens@Sun.COM * We simply need to mark every object dirty, so that it will be 11269396SMatthew.Ahrens@Sun.COM * synced out and now accounted. If this is called 11279396SMatthew.Ahrens@Sun.COM * concurrently, or if we already did some work before crashing, 11289396SMatthew.Ahrens@Sun.COM * that's fine, since we track each object's accounted state 11299396SMatthew.Ahrens@Sun.COM * independently. 11309396SMatthew.Ahrens@Sun.COM */ 11319396SMatthew.Ahrens@Sun.COM 11329396SMatthew.Ahrens@Sun.COM for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) { 11339951SLin.Ling@Sun.COM dmu_tx_t *tx; 11349396SMatthew.Ahrens@Sun.COM dmu_buf_t *db; 11359396SMatthew.Ahrens@Sun.COM int objerr; 11369396SMatthew.Ahrens@Sun.COM 11379396SMatthew.Ahrens@Sun.COM if (issig(JUSTLOOKING) && issig(FORREAL)) 11389396SMatthew.Ahrens@Sun.COM return (EINTR); 11399396SMatthew.Ahrens@Sun.COM 11409396SMatthew.Ahrens@Sun.COM objerr = dmu_bonus_hold(os, obj, FTAG, &db); 11419396SMatthew.Ahrens@Sun.COM if (objerr) 11429396SMatthew.Ahrens@Sun.COM continue; 11439951SLin.Ling@Sun.COM tx = dmu_tx_create(os); 11449396SMatthew.Ahrens@Sun.COM dmu_tx_hold_bonus(tx, obj); 11459396SMatthew.Ahrens@Sun.COM objerr = dmu_tx_assign(tx, TXG_WAIT); 11469396SMatthew.Ahrens@Sun.COM if (objerr) { 11479396SMatthew.Ahrens@Sun.COM dmu_tx_abort(tx); 11489396SMatthew.Ahrens@Sun.COM continue; 11499396SMatthew.Ahrens@Sun.COM } 11509396SMatthew.Ahrens@Sun.COM dmu_buf_will_dirty(db, tx); 11519396SMatthew.Ahrens@Sun.COM dmu_buf_rele(db, FTAG); 11529396SMatthew.Ahrens@Sun.COM dmu_tx_commit(tx); 11539396SMatthew.Ahrens@Sun.COM } 11549396SMatthew.Ahrens@Sun.COM 11559396SMatthew.Ahrens@Sun.COM os->os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE; 11569396SMatthew.Ahrens@Sun.COM txg_wait_synced(dmu_objset_pool(os), 0); 11579396SMatthew.Ahrens@Sun.COM return (0); 11589396SMatthew.Ahrens@Sun.COM } 11599396SMatthew.Ahrens@Sun.COM 1160789Sahrens void 11612885Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp, 11622885Sahrens uint64_t *usedobjsp, uint64_t *availobjsp) 11632885Sahrens { 11642885Sahrens dsl_dataset_space(os->os->os_dsl_dataset, refdbytesp, availbytesp, 11652885Sahrens usedobjsp, availobjsp); 11662885Sahrens } 11672885Sahrens 11682885Sahrens uint64_t 11692885Sahrens dmu_objset_fsid_guid(objset_t *os) 11702885Sahrens { 11712885Sahrens return (dsl_dataset_fsid_guid(os->os->os_dsl_dataset)); 11722885Sahrens } 11732885Sahrens 11742885Sahrens void 11752885Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat) 1176789Sahrens { 11772885Sahrens stat->dds_type = os->os->os_phys->os_type; 11782885Sahrens if (os->os->os_dsl_dataset) 11792885Sahrens dsl_dataset_fast_stat(os->os->os_dsl_dataset, stat); 11802885Sahrens } 11812885Sahrens 11822885Sahrens void 11832885Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv) 11842885Sahrens { 11852885Sahrens ASSERT(os->os->os_dsl_dataset || 11862885Sahrens os->os->os_phys->os_type == DMU_OST_META); 11872885Sahrens 11882885Sahrens if (os->os->os_dsl_dataset != NULL) 11892885Sahrens dsl_dataset_stats(os->os->os_dsl_dataset, nv); 11902885Sahrens 11912885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE, 11922885Sahrens os->os->os_phys->os_type); 11939396SMatthew.Ahrens@Sun.COM dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING, 11949396SMatthew.Ahrens@Sun.COM dmu_objset_userspace_present(os)); 1195789Sahrens } 1196789Sahrens 1197789Sahrens int 1198789Sahrens dmu_objset_is_snapshot(objset_t *os) 1199789Sahrens { 1200789Sahrens if (os->os->os_dsl_dataset != NULL) 1201789Sahrens return (dsl_dataset_is_snapshot(os->os->os_dsl_dataset)); 1202789Sahrens else 1203789Sahrens return (B_FALSE); 1204789Sahrens } 1205789Sahrens 1206789Sahrens int 12076492Stimh dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen, 12086492Stimh boolean_t *conflict) 12096492Stimh { 12106492Stimh dsl_dataset_t *ds = os->os->os_dsl_dataset; 12116492Stimh uint64_t ignored; 12126492Stimh 12136492Stimh if (ds->ds_phys->ds_snapnames_zapobj == 0) 12146492Stimh return (ENOENT); 12156492Stimh 12166492Stimh return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset, 12176492Stimh ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST, 12186492Stimh real, maxlen, conflict)); 12196492Stimh } 12206492Stimh 12216492Stimh int 1222789Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name, 12235663Sck153898 uint64_t *idp, uint64_t *offp, boolean_t *case_conflict) 1224789Sahrens { 1225789Sahrens dsl_dataset_t *ds = os->os->os_dsl_dataset; 1226789Sahrens zap_cursor_t cursor; 1227789Sahrens zap_attribute_t attr; 1228789Sahrens 1229789Sahrens if (ds->ds_phys->ds_snapnames_zapobj == 0) 1230789Sahrens return (ENOENT); 1231789Sahrens 1232789Sahrens zap_cursor_init_serialized(&cursor, 1233789Sahrens ds->ds_dir->dd_pool->dp_meta_objset, 1234789Sahrens ds->ds_phys->ds_snapnames_zapobj, *offp); 1235789Sahrens 1236885Sahrens if (zap_cursor_retrieve(&cursor, &attr) != 0) { 1237885Sahrens zap_cursor_fini(&cursor); 1238885Sahrens return (ENOENT); 1239885Sahrens } 1240885Sahrens 1241885Sahrens if (strlen(attr.za_name) + 1 > namelen) { 1242885Sahrens zap_cursor_fini(&cursor); 1243885Sahrens return (ENAMETOOLONG); 1244885Sahrens } 1245885Sahrens 1246885Sahrens (void) strcpy(name, attr.za_name); 1247885Sahrens if (idp) 1248885Sahrens *idp = attr.za_first_integer; 12495663Sck153898 if (case_conflict) 12505663Sck153898 *case_conflict = attr.za_normalization_conflict; 1251885Sahrens zap_cursor_advance(&cursor); 1252885Sahrens *offp = zap_cursor_serialize(&cursor); 1253885Sahrens zap_cursor_fini(&cursor); 1254885Sahrens 1255885Sahrens return (0); 1256885Sahrens } 1257885Sahrens 1258885Sahrens int 1259885Sahrens dmu_dir_list_next(objset_t *os, int namelen, char *name, 1260885Sahrens uint64_t *idp, uint64_t *offp) 1261885Sahrens { 1262885Sahrens dsl_dir_t *dd = os->os->os_dsl_dataset->ds_dir; 1263885Sahrens zap_cursor_t cursor; 1264885Sahrens zap_attribute_t attr; 1265885Sahrens 1266885Sahrens /* there is no next dir on a snapshot! */ 1267885Sahrens if (os->os->os_dsl_dataset->ds_object != 1268885Sahrens dd->dd_phys->dd_head_dataset_obj) 1269885Sahrens return (ENOENT); 1270885Sahrens 1271885Sahrens zap_cursor_init_serialized(&cursor, 1272885Sahrens dd->dd_pool->dp_meta_objset, 1273885Sahrens dd->dd_phys->dd_child_dir_zapobj, *offp); 1274885Sahrens 1275885Sahrens if (zap_cursor_retrieve(&cursor, &attr) != 0) { 1276885Sahrens zap_cursor_fini(&cursor); 1277885Sahrens return (ENOENT); 1278885Sahrens } 1279885Sahrens 1280885Sahrens if (strlen(attr.za_name) + 1 > namelen) { 1281885Sahrens zap_cursor_fini(&cursor); 1282789Sahrens return (ENAMETOOLONG); 1283885Sahrens } 1284789Sahrens 1285789Sahrens (void) strcpy(name, attr.za_name); 1286885Sahrens if (idp) 1287885Sahrens *idp = attr.za_first_integer; 1288789Sahrens zap_cursor_advance(&cursor); 1289789Sahrens *offp = zap_cursor_serialize(&cursor); 1290885Sahrens zap_cursor_fini(&cursor); 1291789Sahrens 1292789Sahrens return (0); 1293789Sahrens } 1294789Sahrens 12957046Sahrens struct findarg { 12967046Sahrens int (*func)(char *, void *); 12977046Sahrens void *arg; 12987046Sahrens }; 12997046Sahrens 13007046Sahrens /* ARGSUSED */ 13017046Sahrens static int 13027046Sahrens findfunc(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg) 13037046Sahrens { 13047046Sahrens struct findarg *fa = arg; 13057046Sahrens return (fa->func((char *)dsname, fa->arg)); 13067046Sahrens } 13077046Sahrens 1308789Sahrens /* 1309789Sahrens * Find all objsets under name, and for each, call 'func(child_name, arg)'. 13107046Sahrens * Perhaps change all callers to use dmu_objset_find_spa()? 1311789Sahrens */ 13122199Sahrens int 13132199Sahrens dmu_objset_find(char *name, int func(char *, void *), void *arg, int flags) 1314789Sahrens { 13157046Sahrens struct findarg fa; 13167046Sahrens fa.func = func; 13177046Sahrens fa.arg = arg; 13187046Sahrens return (dmu_objset_find_spa(NULL, name, findfunc, &fa, flags)); 13197046Sahrens } 13207046Sahrens 13217046Sahrens /* 13227046Sahrens * Find all objsets under name, call func on each 13237046Sahrens */ 13247046Sahrens int 13257046Sahrens dmu_objset_find_spa(spa_t *spa, const char *name, 13267046Sahrens int func(spa_t *, uint64_t, const char *, void *), void *arg, int flags) 13277046Sahrens { 1328789Sahrens dsl_dir_t *dd; 13297046Sahrens dsl_pool_t *dp; 13307046Sahrens dsl_dataset_t *ds; 1331789Sahrens zap_cursor_t zc; 13323978Smmusante zap_attribute_t *attr; 1333789Sahrens char *child; 13347046Sahrens uint64_t thisobj; 13357046Sahrens int err; 1336789Sahrens 13377046Sahrens if (name == NULL) 13387046Sahrens name = spa_name(spa); 13397046Sahrens err = dsl_dir_open_spa(spa, name, FTAG, &dd, NULL); 13401544Seschrock if (err) 13412199Sahrens return (err); 1342789Sahrens 13437046Sahrens /* Don't visit hidden ($MOS & $ORIGIN) objsets. */ 13447046Sahrens if (dd->dd_myname[0] == '$') { 13457046Sahrens dsl_dir_close(dd, FTAG); 13467046Sahrens return (0); 13477046Sahrens } 13487046Sahrens 13497046Sahrens thisobj = dd->dd_phys->dd_head_dataset_obj; 13503978Smmusante attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); 13517046Sahrens dp = dd->dd_pool; 1352789Sahrens 1353789Sahrens /* 1354789Sahrens * Iterate over all children. 1355789Sahrens */ 13562417Sahrens if (flags & DS_FIND_CHILDREN) { 13577046Sahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, 13582417Sahrens dd->dd_phys->dd_child_dir_zapobj); 13593978Smmusante zap_cursor_retrieve(&zc, attr) == 0; 13602417Sahrens (void) zap_cursor_advance(&zc)) { 13613978Smmusante ASSERT(attr->za_integer_length == sizeof (uint64_t)); 13623978Smmusante ASSERT(attr->za_num_integers == 1); 1363789Sahrens 13642417Sahrens child = kmem_alloc(MAXPATHLEN, KM_SLEEP); 13657046Sahrens (void) strcpy(child, name); 13662417Sahrens (void) strcat(child, "/"); 13673978Smmusante (void) strcat(child, attr->za_name); 13687046Sahrens err = dmu_objset_find_spa(spa, child, func, arg, flags); 13692417Sahrens kmem_free(child, MAXPATHLEN); 13702417Sahrens if (err) 13712417Sahrens break; 13722417Sahrens } 13732417Sahrens zap_cursor_fini(&zc); 13742199Sahrens 13752417Sahrens if (err) { 13762417Sahrens dsl_dir_close(dd, FTAG); 13773978Smmusante kmem_free(attr, sizeof (zap_attribute_t)); 13782417Sahrens return (err); 13792417Sahrens } 1380789Sahrens } 1381789Sahrens 1382789Sahrens /* 1383789Sahrens * Iterate over all snapshots. 1384789Sahrens */ 13857046Sahrens if (flags & DS_FIND_SNAPSHOTS) { 13867046Sahrens if (!dsl_pool_sync_context(dp)) 13877046Sahrens rw_enter(&dp->dp_config_rwlock, RW_READER); 13887046Sahrens err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds); 13897046Sahrens if (!dsl_pool_sync_context(dp)) 13907046Sahrens rw_exit(&dp->dp_config_rwlock); 1391789Sahrens 13927046Sahrens if (err == 0) { 13937046Sahrens uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj; 13947046Sahrens dsl_dataset_rele(ds, FTAG); 1395789Sahrens 13967046Sahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj); 13977046Sahrens zap_cursor_retrieve(&zc, attr) == 0; 13987046Sahrens (void) zap_cursor_advance(&zc)) { 13997046Sahrens ASSERT(attr->za_integer_length == 14007046Sahrens sizeof (uint64_t)); 14017046Sahrens ASSERT(attr->za_num_integers == 1); 1402789Sahrens 14037046Sahrens child = kmem_alloc(MAXPATHLEN, KM_SLEEP); 14047046Sahrens (void) strcpy(child, name); 14057046Sahrens (void) strcat(child, "@"); 14067046Sahrens (void) strcat(child, attr->za_name); 14077046Sahrens err = func(spa, attr->za_first_integer, 14087046Sahrens child, arg); 14097046Sahrens kmem_free(child, MAXPATHLEN); 14107046Sahrens if (err) 14117046Sahrens break; 14127046Sahrens } 14137046Sahrens zap_cursor_fini(&zc); 1414789Sahrens } 1415789Sahrens } 1416789Sahrens 1417789Sahrens dsl_dir_close(dd, FTAG); 14183978Smmusante kmem_free(attr, sizeof (zap_attribute_t)); 1419789Sahrens 14202199Sahrens if (err) 14212199Sahrens return (err); 14222199Sahrens 1423789Sahrens /* 1424789Sahrens * Apply to self if appropriate. 1425789Sahrens */ 14267046Sahrens err = func(spa, thisobj, name, arg); 14272199Sahrens return (err); 1428789Sahrens } 14295326Sek110237 14308415SRichard.Morris@Sun.COM /* ARGSUSED */ 14318415SRichard.Morris@Sun.COM int 14328415SRichard.Morris@Sun.COM dmu_objset_prefetch(char *name, void *arg) 14338415SRichard.Morris@Sun.COM { 14348415SRichard.Morris@Sun.COM dsl_dataset_t *ds; 14358415SRichard.Morris@Sun.COM 14368415SRichard.Morris@Sun.COM if (dsl_dataset_hold(name, FTAG, &ds)) 14378415SRichard.Morris@Sun.COM return (0); 14388415SRichard.Morris@Sun.COM 14398415SRichard.Morris@Sun.COM if (!BP_IS_HOLE(&ds->ds_phys->ds_bp)) { 14408415SRichard.Morris@Sun.COM mutex_enter(&ds->ds_opening_lock); 14418415SRichard.Morris@Sun.COM if (!dsl_dataset_get_user_ptr(ds)) { 14428415SRichard.Morris@Sun.COM uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH; 14438415SRichard.Morris@Sun.COM zbookmark_t zb; 14448415SRichard.Morris@Sun.COM 14458415SRichard.Morris@Sun.COM zb.zb_objset = ds->ds_object; 14468415SRichard.Morris@Sun.COM zb.zb_object = 0; 14478415SRichard.Morris@Sun.COM zb.zb_level = -1; 14488415SRichard.Morris@Sun.COM zb.zb_blkid = 0; 14498415SRichard.Morris@Sun.COM 14508415SRichard.Morris@Sun.COM (void) arc_read_nolock(NULL, dsl_dataset_get_spa(ds), 14518415SRichard.Morris@Sun.COM &ds->ds_phys->ds_bp, NULL, NULL, 14528415SRichard.Morris@Sun.COM ZIO_PRIORITY_ASYNC_READ, 14538415SRichard.Morris@Sun.COM ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, 14548415SRichard.Morris@Sun.COM &aflags, &zb); 14558415SRichard.Morris@Sun.COM } 14568415SRichard.Morris@Sun.COM mutex_exit(&ds->ds_opening_lock); 14578415SRichard.Morris@Sun.COM } 14588415SRichard.Morris@Sun.COM 14598415SRichard.Morris@Sun.COM dsl_dataset_rele(ds, FTAG); 14608415SRichard.Morris@Sun.COM return (0); 14618415SRichard.Morris@Sun.COM } 14628415SRichard.Morris@Sun.COM 14635326Sek110237 void 14645326Sek110237 dmu_objset_set_user(objset_t *os, void *user_ptr) 14655326Sek110237 { 14665326Sek110237 ASSERT(MUTEX_HELD(&os->os->os_user_ptr_lock)); 14675326Sek110237 os->os->os_user_ptr = user_ptr; 14685326Sek110237 } 14695326Sek110237 14705326Sek110237 void * 14715326Sek110237 dmu_objset_get_user(objset_t *os) 14725326Sek110237 { 14735326Sek110237 ASSERT(MUTEX_HELD(&os->os->os_user_ptr_lock)); 14745326Sek110237 return (os->os->os_user_ptr); 14755326Sek110237 } 1476