1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5ea8dc4b6Seschrock * Common Development and Distribution License (the "License"). 6ea8dc4b6Seschrock * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21fa9e4066Sahrens /* 22ea2f5b9eSMatthew Ahrens * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23fa9e4066Sahrens * Use is subject to license terms. 24fa9e4066Sahrens */ 25fa9e4066Sahrens 26ecd6cf80Smarks #include <sys/cred.h> 27fa9e4066Sahrens #include <sys/zfs_context.h> 28fa9e4066Sahrens #include <sys/dmu_objset.h> 29fa9e4066Sahrens #include <sys/dsl_dir.h> 30fa9e4066Sahrens #include <sys/dsl_dataset.h> 31fa9e4066Sahrens #include <sys/dsl_prop.h> 32fa9e4066Sahrens #include <sys/dsl_pool.h> 331d452cf5Sahrens #include <sys/dsl_synctask.h> 34ecd6cf80Smarks #include <sys/dsl_deleg.h> 35fa9e4066Sahrens #include <sys/dnode.h> 36fa9e4066Sahrens #include <sys/dbuf.h> 37a2eea2e1Sahrens #include <sys/zvol.h> 38fa9e4066Sahrens #include <sys/dmu_tx.h> 39fa9e4066Sahrens #include <sys/zio_checksum.h> 40fa9e4066Sahrens #include <sys/zap.h> 41fa9e4066Sahrens #include <sys/zil.h> 42fa9e4066Sahrens #include <sys/dmu_impl.h> 43ecd6cf80Smarks #include <sys/zfs_ioctl.h> 44fa9e4066Sahrens 45fa9e4066Sahrens spa_t * 46fa9e4066Sahrens dmu_objset_spa(objset_t *os) 47fa9e4066Sahrens { 48*503ad85cSMatthew Ahrens return (os->os_spa); 49fa9e4066Sahrens } 50fa9e4066Sahrens 51fa9e4066Sahrens zilog_t * 52fa9e4066Sahrens dmu_objset_zil(objset_t *os) 53fa9e4066Sahrens { 54*503ad85cSMatthew Ahrens return (os->os_zil); 55fa9e4066Sahrens } 56fa9e4066Sahrens 57fa9e4066Sahrens dsl_pool_t * 58fa9e4066Sahrens dmu_objset_pool(objset_t *os) 59fa9e4066Sahrens { 60fa9e4066Sahrens dsl_dataset_t *ds; 61fa9e4066Sahrens 62*503ad85cSMatthew Ahrens if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir) 63fa9e4066Sahrens return (ds->ds_dir->dd_pool); 64fa9e4066Sahrens else 65*503ad85cSMatthew Ahrens return (spa_get_dsl(os->os_spa)); 66fa9e4066Sahrens } 67fa9e4066Sahrens 68fa9e4066Sahrens dsl_dataset_t * 69fa9e4066Sahrens dmu_objset_ds(objset_t *os) 70fa9e4066Sahrens { 71*503ad85cSMatthew Ahrens return (os->os_dsl_dataset); 72fa9e4066Sahrens } 73fa9e4066Sahrens 74fa9e4066Sahrens dmu_objset_type_t 75fa9e4066Sahrens dmu_objset_type(objset_t *os) 76fa9e4066Sahrens { 77*503ad85cSMatthew Ahrens return (os->os_phys->os_type); 78fa9e4066Sahrens } 79fa9e4066Sahrens 80fa9e4066Sahrens void 81fa9e4066Sahrens dmu_objset_name(objset_t *os, char *buf) 82fa9e4066Sahrens { 83*503ad85cSMatthew Ahrens dsl_dataset_name(os->os_dsl_dataset, buf); 84fa9e4066Sahrens } 85fa9e4066Sahrens 86fa9e4066Sahrens uint64_t 87fa9e4066Sahrens dmu_objset_id(objset_t *os) 88fa9e4066Sahrens { 89*503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 90fa9e4066Sahrens 91fa9e4066Sahrens return (ds ? ds->ds_object : 0); 92fa9e4066Sahrens } 93fa9e4066Sahrens 94fa9e4066Sahrens static void 95fa9e4066Sahrens checksum_changed_cb(void *arg, uint64_t newval) 96fa9e4066Sahrens { 97*503ad85cSMatthew Ahrens objset_t *os = arg; 98fa9e4066Sahrens 99fa9e4066Sahrens /* 100fa9e4066Sahrens * Inheritance should have been done by now. 101fa9e4066Sahrens */ 102fa9e4066Sahrens ASSERT(newval != ZIO_CHECKSUM_INHERIT); 103fa9e4066Sahrens 104*503ad85cSMatthew Ahrens os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE); 105fa9e4066Sahrens } 106fa9e4066Sahrens 107fa9e4066Sahrens static void 108fa9e4066Sahrens compression_changed_cb(void *arg, uint64_t newval) 109fa9e4066Sahrens { 110*503ad85cSMatthew Ahrens objset_t *os = arg; 111fa9e4066Sahrens 112fa9e4066Sahrens /* 113fa9e4066Sahrens * Inheritance and range checking should have been done by now. 114fa9e4066Sahrens */ 115fa9e4066Sahrens ASSERT(newval != ZIO_COMPRESS_INHERIT); 116fa9e4066Sahrens 117*503ad85cSMatthew Ahrens os->os_compress = zio_compress_select(newval, ZIO_COMPRESS_ON_VALUE); 118fa9e4066Sahrens } 119fa9e4066Sahrens 120d0ad202dSahrens static void 121d0ad202dSahrens copies_changed_cb(void *arg, uint64_t newval) 122d0ad202dSahrens { 123*503ad85cSMatthew Ahrens objset_t *os = arg; 124d0ad202dSahrens 125d0ad202dSahrens /* 126d0ad202dSahrens * Inheritance and range checking should have been done by now. 127d0ad202dSahrens */ 128d0ad202dSahrens ASSERT(newval > 0); 129*503ad85cSMatthew Ahrens ASSERT(newval <= spa_max_replication(os->os_spa)); 130d0ad202dSahrens 131*503ad85cSMatthew Ahrens os->os_copies = newval; 132d0ad202dSahrens } 133d0ad202dSahrens 1343baa08fcSek static void 1353baa08fcSek primary_cache_changed_cb(void *arg, uint64_t newval) 1363baa08fcSek { 137*503ad85cSMatthew Ahrens objset_t *os = arg; 1383baa08fcSek 1393baa08fcSek /* 1403baa08fcSek * Inheritance and range checking should have been done by now. 1413baa08fcSek */ 1423baa08fcSek ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE || 1433baa08fcSek newval == ZFS_CACHE_METADATA); 1443baa08fcSek 145*503ad85cSMatthew Ahrens os->os_primary_cache = newval; 1463baa08fcSek } 1473baa08fcSek 1483baa08fcSek static void 1493baa08fcSek secondary_cache_changed_cb(void *arg, uint64_t newval) 1503baa08fcSek { 151*503ad85cSMatthew Ahrens objset_t *os = arg; 1523baa08fcSek 1533baa08fcSek /* 1543baa08fcSek * Inheritance and range checking should have been done by now. 1553baa08fcSek */ 1563baa08fcSek ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE || 1573baa08fcSek newval == ZFS_CACHE_METADATA); 1583baa08fcSek 159*503ad85cSMatthew Ahrens os->os_secondary_cache = newval; 1603baa08fcSek } 1613baa08fcSek 162fa9e4066Sahrens void 163fa9e4066Sahrens dmu_objset_byteswap(void *buf, size_t size) 164fa9e4066Sahrens { 165fa9e4066Sahrens objset_phys_t *osp = buf; 166fa9e4066Sahrens 16714843421SMatthew Ahrens ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t)); 168fa9e4066Sahrens dnode_byteswap(&osp->os_meta_dnode); 169fa9e4066Sahrens byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t)); 170fa9e4066Sahrens osp->os_type = BSWAP_64(osp->os_type); 17114843421SMatthew Ahrens osp->os_flags = BSWAP_64(osp->os_flags); 17214843421SMatthew Ahrens if (size == sizeof (objset_phys_t)) { 17314843421SMatthew Ahrens dnode_byteswap(&osp->os_userused_dnode); 17414843421SMatthew Ahrens dnode_byteswap(&osp->os_groupused_dnode); 17514843421SMatthew Ahrens } 176fa9e4066Sahrens } 177fa9e4066Sahrens 178ea8dc4b6Seschrock int 179ea8dc4b6Seschrock dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, 180*503ad85cSMatthew Ahrens objset_t **osp) 181fa9e4066Sahrens { 182*503ad85cSMatthew Ahrens objset_t *os; 183088f3894Sahrens int i, err; 184fa9e4066Sahrens 18591ebeef5Sahrens ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock)); 18691ebeef5Sahrens 187*503ad85cSMatthew Ahrens os = kmem_zalloc(sizeof (objset_t), KM_SLEEP); 188*503ad85cSMatthew Ahrens os->os_dsl_dataset = ds; 189*503ad85cSMatthew Ahrens os->os_spa = spa; 190*503ad85cSMatthew Ahrens os->os_rootbp = bp; 191*503ad85cSMatthew Ahrens if (!BP_IS_HOLE(os->os_rootbp)) { 19213506d1eSmaybee uint32_t aflags = ARC_WAIT; 193ea8dc4b6Seschrock zbookmark_t zb; 194ea8dc4b6Seschrock zb.zb_objset = ds ? ds->ds_object : 0; 195ea8dc4b6Seschrock zb.zb_object = 0; 196ea8dc4b6Seschrock zb.zb_level = -1; 197ea8dc4b6Seschrock zb.zb_blkid = 0; 198*503ad85cSMatthew Ahrens if (DMU_OS_IS_L2CACHEABLE(os)) 1993baa08fcSek aflags |= ARC_L2CACHE; 200ea8dc4b6Seschrock 201*503ad85cSMatthew Ahrens dprintf_bp(os->os_rootbp, "reading %s", ""); 202088f3894Sahrens /* 203088f3894Sahrens * NB: when bprewrite scrub can change the bp, 204088f3894Sahrens * and this is called from dmu_objset_open_ds_os, the bp 205088f3894Sahrens * could change, and we'll need a lock. 206088f3894Sahrens */ 207*503ad85cSMatthew Ahrens err = arc_read_nolock(NULL, spa, os->os_rootbp, 208*503ad85cSMatthew Ahrens arc_getbuf_func, &os->os_phys_buf, 20913506d1eSmaybee ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb); 210ea8dc4b6Seschrock if (err) { 211*503ad85cSMatthew Ahrens kmem_free(os, sizeof (objset_t)); 212b87f3af3Sperrin /* convert checksum errors into IO errors */ 213b87f3af3Sperrin if (err == ECKSUM) 214b87f3af3Sperrin err = EIO; 215ea8dc4b6Seschrock return (err); 216ea8dc4b6Seschrock } 21714843421SMatthew Ahrens 21814843421SMatthew Ahrens /* Increase the blocksize if we are permitted. */ 21914843421SMatthew Ahrens if (spa_version(spa) >= SPA_VERSION_USERSPACE && 220*503ad85cSMatthew Ahrens arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) { 22114843421SMatthew Ahrens arc_buf_t *buf = arc_buf_alloc(spa, 222*503ad85cSMatthew Ahrens sizeof (objset_phys_t), &os->os_phys_buf, 22314843421SMatthew Ahrens ARC_BUFC_METADATA); 22414843421SMatthew Ahrens bzero(buf->b_data, sizeof (objset_phys_t)); 225*503ad85cSMatthew Ahrens bcopy(os->os_phys_buf->b_data, buf->b_data, 226*503ad85cSMatthew Ahrens arc_buf_size(os->os_phys_buf)); 227*503ad85cSMatthew Ahrens (void) arc_buf_remove_ref(os->os_phys_buf, 228*503ad85cSMatthew Ahrens &os->os_phys_buf); 229*503ad85cSMatthew Ahrens os->os_phys_buf = buf; 23014843421SMatthew Ahrens } 23114843421SMatthew Ahrens 232*503ad85cSMatthew Ahrens os->os_phys = os->os_phys_buf->b_data; 233*503ad85cSMatthew Ahrens os->os_flags = os->os_phys->os_flags; 234fa9e4066Sahrens } else { 23514843421SMatthew Ahrens int size = spa_version(spa) >= SPA_VERSION_USERSPACE ? 23614843421SMatthew Ahrens sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE; 237*503ad85cSMatthew Ahrens os->os_phys_buf = arc_buf_alloc(spa, size, 238*503ad85cSMatthew Ahrens &os->os_phys_buf, ARC_BUFC_METADATA); 239*503ad85cSMatthew Ahrens os->os_phys = os->os_phys_buf->b_data; 240*503ad85cSMatthew Ahrens bzero(os->os_phys, size); 241fa9e4066Sahrens } 242fa9e4066Sahrens 243fa9e4066Sahrens /* 244fa9e4066Sahrens * Note: the changed_cb will be called once before the register 245fa9e4066Sahrens * func returns, thus changing the checksum/compression from the 2463baa08fcSek * default (fletcher2/off). Snapshots don't need to know about 2473baa08fcSek * checksum/compression/copies. 248fa9e4066Sahrens */ 2493baa08fcSek if (ds) { 2503baa08fcSek err = dsl_prop_register(ds, "primarycache", 251*503ad85cSMatthew Ahrens primary_cache_changed_cb, os); 252d0ad202dSahrens if (err == 0) 2533baa08fcSek err = dsl_prop_register(ds, "secondarycache", 254*503ad85cSMatthew Ahrens secondary_cache_changed_cb, os); 2553baa08fcSek if (!dsl_dataset_is_snapshot(ds)) { 2563baa08fcSek if (err == 0) 2573baa08fcSek err = dsl_prop_register(ds, "checksum", 258*503ad85cSMatthew Ahrens checksum_changed_cb, os); 2593baa08fcSek if (err == 0) 2603baa08fcSek err = dsl_prop_register(ds, "compression", 261*503ad85cSMatthew Ahrens compression_changed_cb, os); 2623baa08fcSek if (err == 0) 2633baa08fcSek err = dsl_prop_register(ds, "copies", 264*503ad85cSMatthew Ahrens copies_changed_cb, os); 2653baa08fcSek } 266ea8dc4b6Seschrock if (err) { 267*503ad85cSMatthew Ahrens VERIFY(arc_buf_remove_ref(os->os_phys_buf, 268*503ad85cSMatthew Ahrens &os->os_phys_buf) == 1); 269*503ad85cSMatthew Ahrens kmem_free(os, sizeof (objset_t)); 270ea8dc4b6Seschrock return (err); 271ea8dc4b6Seschrock } 27299653d4eSeschrock } else if (ds == NULL) { 273fa9e4066Sahrens /* It's the meta-objset. */ 274*503ad85cSMatthew Ahrens os->os_checksum = ZIO_CHECKSUM_FLETCHER_4; 275*503ad85cSMatthew Ahrens os->os_compress = ZIO_COMPRESS_LZJB; 276*503ad85cSMatthew Ahrens os->os_copies = spa_max_replication(spa); 277*503ad85cSMatthew Ahrens os->os_primary_cache = ZFS_CACHE_ALL; 278*503ad85cSMatthew Ahrens os->os_secondary_cache = ZFS_CACHE_ALL; 279fa9e4066Sahrens } 280fa9e4066Sahrens 281*503ad85cSMatthew Ahrens os->os_zil_header = os->os_phys->os_zil_header; 282*503ad85cSMatthew Ahrens os->os_zil = zil_alloc(os, &os->os_zil_header); 283fa9e4066Sahrens 284fa9e4066Sahrens for (i = 0; i < TXG_SIZE; i++) { 285*503ad85cSMatthew Ahrens list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t), 286fa9e4066Sahrens offsetof(dnode_t, dn_dirty_link[i])); 287*503ad85cSMatthew Ahrens list_create(&os->os_free_dnodes[i], sizeof (dnode_t), 288fa9e4066Sahrens offsetof(dnode_t, dn_dirty_link[i])); 289fa9e4066Sahrens } 290*503ad85cSMatthew Ahrens list_create(&os->os_dnodes, sizeof (dnode_t), 291fa9e4066Sahrens offsetof(dnode_t, dn_link)); 292*503ad85cSMatthew Ahrens list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t), 293fa9e4066Sahrens offsetof(dmu_buf_impl_t, db_link)); 294fa9e4066Sahrens 295*503ad85cSMatthew Ahrens mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL); 296*503ad85cSMatthew Ahrens mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL); 297*503ad85cSMatthew Ahrens mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL); 298*503ad85cSMatthew Ahrens 299*503ad85cSMatthew Ahrens os->os_meta_dnode = dnode_special_open(os, 300*503ad85cSMatthew Ahrens &os->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT); 301*503ad85cSMatthew Ahrens if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) { 302*503ad85cSMatthew Ahrens os->os_userused_dnode = dnode_special_open(os, 303*503ad85cSMatthew Ahrens &os->os_phys->os_userused_dnode, DMU_USERUSED_OBJECT); 304*503ad85cSMatthew Ahrens os->os_groupused_dnode = dnode_special_open(os, 305*503ad85cSMatthew Ahrens &os->os_phys->os_groupused_dnode, DMU_GROUPUSED_OBJECT); 30614843421SMatthew Ahrens } 307fa9e4066Sahrens 30891ebeef5Sahrens /* 30991ebeef5Sahrens * We should be the only thread trying to do this because we 31091ebeef5Sahrens * have ds_opening_lock 31191ebeef5Sahrens */ 31291ebeef5Sahrens if (ds) { 313*503ad85cSMatthew Ahrens mutex_enter(&ds->ds_lock); 314*503ad85cSMatthew Ahrens ASSERT(ds->ds_objset == NULL); 315*503ad85cSMatthew Ahrens ds->ds_objset = os; 316*503ad85cSMatthew Ahrens mutex_exit(&ds->ds_lock); 317fa9e4066Sahrens } 318fa9e4066Sahrens 319*503ad85cSMatthew Ahrens *osp = os; 320ea8dc4b6Seschrock return (0); 321fa9e4066Sahrens } 322fa9e4066Sahrens 323*503ad85cSMatthew Ahrens int 324*503ad85cSMatthew Ahrens dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp) 3253cb34c60Sahrens { 326*503ad85cSMatthew Ahrens int err = 0; 3273cb34c60Sahrens 3283cb34c60Sahrens mutex_enter(&ds->ds_opening_lock); 329*503ad85cSMatthew Ahrens *osp = ds->ds_objset; 330*503ad85cSMatthew Ahrens if (*osp == NULL) { 3313cb34c60Sahrens err = dmu_objset_open_impl(dsl_dataset_get_spa(ds), 332*503ad85cSMatthew Ahrens ds, &ds->ds_phys->ds_bp, osp); 3333cb34c60Sahrens } 3343cb34c60Sahrens mutex_exit(&ds->ds_opening_lock); 335*503ad85cSMatthew Ahrens return (err); 3363cb34c60Sahrens } 3373cb34c60Sahrens 338*503ad85cSMatthew Ahrens /* called from zpl */ 3393cb34c60Sahrens int 340*503ad85cSMatthew Ahrens dmu_objset_hold(const char *name, void *tag, objset_t **osp) 3413cb34c60Sahrens { 342*503ad85cSMatthew Ahrens dsl_dataset_t *ds; 3433cb34c60Sahrens int err; 3443cb34c60Sahrens 345*503ad85cSMatthew Ahrens err = dsl_dataset_hold(name, tag, &ds); 3463cb34c60Sahrens if (err) 347*503ad85cSMatthew Ahrens return (err); 348*503ad85cSMatthew Ahrens 349*503ad85cSMatthew Ahrens err = dmu_objset_from_ds(ds, osp); 350*503ad85cSMatthew Ahrens if (err) 351*503ad85cSMatthew Ahrens dsl_dataset_rele(ds, tag); 352*503ad85cSMatthew Ahrens 3533cb34c60Sahrens return (err); 3543cb34c60Sahrens } 3553cb34c60Sahrens 356fa9e4066Sahrens /* called from zpl */ 357fa9e4066Sahrens int 358*503ad85cSMatthew Ahrens dmu_objset_own(const char *name, dmu_objset_type_t type, 359*503ad85cSMatthew Ahrens boolean_t readonly, void *tag, objset_t **osp) 360fa9e4066Sahrens { 361f18faf3fSek dsl_dataset_t *ds; 362f18faf3fSek int err; 363fa9e4066Sahrens 364*503ad85cSMatthew Ahrens err = dsl_dataset_own(name, B_FALSE, tag, &ds); 365*503ad85cSMatthew Ahrens if (err) 366fa9e4066Sahrens return (err); 367fa9e4066Sahrens 368*503ad85cSMatthew Ahrens err = dmu_objset_from_ds(ds, osp); 3693cb34c60Sahrens if (err) { 370*503ad85cSMatthew Ahrens dsl_dataset_disown(ds, tag); 371*503ad85cSMatthew Ahrens } else if ((type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) || 372*503ad85cSMatthew Ahrens (!readonly && dsl_dataset_is_snapshot(ds))) { 373*503ad85cSMatthew Ahrens dmu_objset_disown(*osp, tag); 374*503ad85cSMatthew Ahrens return (EINVAL); 375fa9e4066Sahrens } 376*503ad85cSMatthew Ahrens 3773cb34c60Sahrens return (err); 378fa9e4066Sahrens } 379fa9e4066Sahrens 380fa9e4066Sahrens void 381*503ad85cSMatthew Ahrens dmu_objset_rele(objset_t *os, void *tag) 382fa9e4066Sahrens { 383*503ad85cSMatthew Ahrens dsl_dataset_rele(os->os_dsl_dataset, tag); 384*503ad85cSMatthew Ahrens } 385*503ad85cSMatthew Ahrens 386*503ad85cSMatthew Ahrens void 387*503ad85cSMatthew Ahrens dmu_objset_disown(objset_t *os, void *tag) 388*503ad85cSMatthew Ahrens { 389*503ad85cSMatthew Ahrens dsl_dataset_disown(os->os_dsl_dataset, tag); 390fa9e4066Sahrens } 391fa9e4066Sahrens 392436b2950Sperrin int 3931934e92fSmaybee dmu_objset_evict_dbufs(objset_t *os) 394ea8dc4b6Seschrock { 395ea8dc4b6Seschrock dnode_t *dn; 396c543ec06Sahrens 397*503ad85cSMatthew Ahrens mutex_enter(&os->os_lock); 398c543ec06Sahrens 399c543ec06Sahrens /* process the mdn last, since the other dnodes have holds on it */ 400*503ad85cSMatthew Ahrens list_remove(&os->os_dnodes, os->os_meta_dnode); 401*503ad85cSMatthew Ahrens list_insert_tail(&os->os_dnodes, os->os_meta_dnode); 402ea8dc4b6Seschrock 403ea8dc4b6Seschrock /* 404c543ec06Sahrens * Find the first dnode with holds. We have to do this dance 405c543ec06Sahrens * because dnode_add_ref() only works if you already have a 406c543ec06Sahrens * hold. If there are no holds then it has no dbufs so OK to 407c543ec06Sahrens * skip. 408ea8dc4b6Seschrock */ 409*503ad85cSMatthew Ahrens for (dn = list_head(&os->os_dnodes); 4101934e92fSmaybee dn && !dnode_add_ref(dn, FTAG); 411*503ad85cSMatthew Ahrens dn = list_next(&os->os_dnodes, dn)) 412c543ec06Sahrens continue; 413c543ec06Sahrens 414c543ec06Sahrens while (dn) { 415c543ec06Sahrens dnode_t *next_dn = dn; 416c543ec06Sahrens 417c543ec06Sahrens do { 418*503ad85cSMatthew Ahrens next_dn = list_next(&os->os_dnodes, next_dn); 4191934e92fSmaybee } while (next_dn && !dnode_add_ref(next_dn, FTAG)); 420c543ec06Sahrens 421*503ad85cSMatthew Ahrens mutex_exit(&os->os_lock); 4221934e92fSmaybee dnode_evict_dbufs(dn); 423c543ec06Sahrens dnode_rele(dn, FTAG); 424*503ad85cSMatthew Ahrens mutex_enter(&os->os_lock); 425c543ec06Sahrens dn = next_dn; 426ea8dc4b6Seschrock } 427*503ad85cSMatthew Ahrens mutex_exit(&os->os_lock); 428*503ad85cSMatthew Ahrens return (list_head(&os->os_dnodes) != os->os_meta_dnode); 429ea8dc4b6Seschrock } 430ea8dc4b6Seschrock 431fa9e4066Sahrens void 432*503ad85cSMatthew Ahrens dmu_objset_evict(objset_t *os) 433fa9e4066Sahrens { 434*503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 43599653d4eSeschrock int i; 436fa9e4066Sahrens 437fa9e4066Sahrens for (i = 0; i < TXG_SIZE; i++) { 438*503ad85cSMatthew Ahrens ASSERT(list_head(&os->os_dirty_dnodes[i]) == NULL); 439*503ad85cSMatthew Ahrens ASSERT(list_head(&os->os_free_dnodes[i]) == NULL); 440fa9e4066Sahrens } 441fa9e4066Sahrens 4423baa08fcSek if (ds) { 4433baa08fcSek if (!dsl_dataset_is_snapshot(ds)) { 4443baa08fcSek VERIFY(0 == dsl_prop_unregister(ds, "checksum", 445*503ad85cSMatthew Ahrens checksum_changed_cb, os)); 4463baa08fcSek VERIFY(0 == dsl_prop_unregister(ds, "compression", 447*503ad85cSMatthew Ahrens compression_changed_cb, os)); 4483baa08fcSek VERIFY(0 == dsl_prop_unregister(ds, "copies", 449*503ad85cSMatthew Ahrens copies_changed_cb, os)); 4503baa08fcSek } 4513baa08fcSek VERIFY(0 == dsl_prop_unregister(ds, "primarycache", 452*503ad85cSMatthew Ahrens primary_cache_changed_cb, os)); 4533baa08fcSek VERIFY(0 == dsl_prop_unregister(ds, "secondarycache", 454*503ad85cSMatthew Ahrens secondary_cache_changed_cb, os)); 455fa9e4066Sahrens } 456fa9e4066Sahrens 457ea8dc4b6Seschrock /* 458ea8dc4b6Seschrock * We should need only a single pass over the dnode list, since 459ea8dc4b6Seschrock * nothing can be added to the list at this point. 460ea8dc4b6Seschrock */ 461*503ad85cSMatthew Ahrens (void) dmu_objset_evict_dbufs(os); 462ea8dc4b6Seschrock 463*503ad85cSMatthew Ahrens dnode_special_close(os->os_meta_dnode); 464*503ad85cSMatthew Ahrens if (os->os_userused_dnode) { 465*503ad85cSMatthew Ahrens dnode_special_close(os->os_userused_dnode); 466*503ad85cSMatthew Ahrens dnode_special_close(os->os_groupused_dnode); 46714843421SMatthew Ahrens } 468*503ad85cSMatthew Ahrens zil_free(os->os_zil); 469fa9e4066Sahrens 470*503ad85cSMatthew Ahrens ASSERT3P(list_head(&os->os_dnodes), ==, NULL); 47114843421SMatthew Ahrens 472*503ad85cSMatthew Ahrens VERIFY(arc_buf_remove_ref(os->os_phys_buf, &os->os_phys_buf) == 1); 473*503ad85cSMatthew Ahrens mutex_destroy(&os->os_lock); 474*503ad85cSMatthew Ahrens mutex_destroy(&os->os_obj_lock); 475*503ad85cSMatthew Ahrens mutex_destroy(&os->os_user_ptr_lock); 476*503ad85cSMatthew Ahrens kmem_free(os, sizeof (objset_t)); 477fa9e4066Sahrens } 478fa9e4066Sahrens 479fa9e4066Sahrens /* called from dsl for meta-objset */ 480*503ad85cSMatthew Ahrens objset_t * 481c717a561Smaybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, 482c717a561Smaybee dmu_objset_type_t type, dmu_tx_t *tx) 483fa9e4066Sahrens { 484*503ad85cSMatthew Ahrens objset_t *os; 485fa9e4066Sahrens dnode_t *mdn; 486fa9e4066Sahrens 487fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 48891ebeef5Sahrens if (ds) 48991ebeef5Sahrens mutex_enter(&ds->ds_opening_lock); 490*503ad85cSMatthew Ahrens VERIFY(0 == dmu_objset_open_impl(spa, ds, bp, &os)); 49191ebeef5Sahrens if (ds) 49291ebeef5Sahrens mutex_exit(&ds->ds_opening_lock); 493*503ad85cSMatthew Ahrens mdn = os->os_meta_dnode; 494fa9e4066Sahrens 495fa9e4066Sahrens dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT, 496fa9e4066Sahrens DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx); 497fa9e4066Sahrens 498fa9e4066Sahrens /* 499fa9e4066Sahrens * We don't want to have to increase the meta-dnode's nlevels 500fa9e4066Sahrens * later, because then we could do it in quescing context while 501fa9e4066Sahrens * we are also accessing it in open context. 502fa9e4066Sahrens * 503fa9e4066Sahrens * This precaution is not necessary for the MOS (ds == NULL), 504fa9e4066Sahrens * because the MOS is only updated in syncing context. 505fa9e4066Sahrens * This is most fortunate: the MOS is the only objset that 506fa9e4066Sahrens * needs to be synced multiple times as spa_sync() iterates 507fa9e4066Sahrens * to convergence, so minimizing its dn_nlevels matters. 508fa9e4066Sahrens */ 509ea8dc4b6Seschrock if (ds != NULL) { 510ea8dc4b6Seschrock int levels = 1; 511ea8dc4b6Seschrock 512ea8dc4b6Seschrock /* 513ea8dc4b6Seschrock * Determine the number of levels necessary for the meta-dnode 514ea8dc4b6Seschrock * to contain DN_MAX_OBJECT dnodes. 515ea8dc4b6Seschrock */ 516ea8dc4b6Seschrock while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift + 517ea8dc4b6Seschrock (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) < 518ea8dc4b6Seschrock DN_MAX_OBJECT * sizeof (dnode_phys_t)) 519ea8dc4b6Seschrock levels++; 520ea8dc4b6Seschrock 521fa9e4066Sahrens mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] = 522ea8dc4b6Seschrock mdn->dn_nlevels = levels; 523ea8dc4b6Seschrock } 524fa9e4066Sahrens 525fa9e4066Sahrens ASSERT(type != DMU_OST_NONE); 526fa9e4066Sahrens ASSERT(type != DMU_OST_ANY); 527fa9e4066Sahrens ASSERT(type < DMU_OST_NUMTYPES); 528*503ad85cSMatthew Ahrens os->os_phys->os_type = type; 529*503ad85cSMatthew Ahrens if (dmu_objset_userused_enabled(os)) { 530*503ad85cSMatthew Ahrens os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE; 531*503ad85cSMatthew Ahrens os->os_flags = os->os_phys->os_flags; 53214843421SMatthew Ahrens } 533fa9e4066Sahrens 534fa9e4066Sahrens dsl_dataset_dirty(ds, tx); 535fa9e4066Sahrens 536*503ad85cSMatthew Ahrens return (os); 537fa9e4066Sahrens } 538fa9e4066Sahrens 539fa9e4066Sahrens struct oscarg { 540ecd6cf80Smarks void (*userfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx); 541fa9e4066Sahrens void *userarg; 542ae46e4c7SMatthew Ahrens dsl_dataset_t *clone_origin; 543fa9e4066Sahrens const char *lastname; 544fa9e4066Sahrens dmu_objset_type_t type; 545ab04eb8eStimh uint64_t flags; 546fa9e4066Sahrens }; 547fa9e4066Sahrens 548ecd6cf80Smarks /*ARGSUSED*/ 549fa9e4066Sahrens static int 5501d452cf5Sahrens dmu_objset_create_check(void *arg1, void *arg2, dmu_tx_t *tx) 551fa9e4066Sahrens { 5521d452cf5Sahrens dsl_dir_t *dd = arg1; 5531d452cf5Sahrens struct oscarg *oa = arg2; 5541d452cf5Sahrens objset_t *mos = dd->dd_pool->dp_meta_objset; 555fa9e4066Sahrens int err; 5561d452cf5Sahrens uint64_t ddobj; 5571d452cf5Sahrens 5581d452cf5Sahrens err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj, 5591d452cf5Sahrens oa->lastname, sizeof (uint64_t), 1, &ddobj); 5601d452cf5Sahrens if (err != ENOENT) 5611d452cf5Sahrens return (err ? err : EEXIST); 5621d452cf5Sahrens 563ae46e4c7SMatthew Ahrens if (oa->clone_origin != NULL) { 564ae46e4c7SMatthew Ahrens /* You can't clone across pools. */ 565ae46e4c7SMatthew Ahrens if (oa->clone_origin->ds_dir->dd_pool != dd->dd_pool) 5661d452cf5Sahrens return (EXDEV); 5671d452cf5Sahrens 568ae46e4c7SMatthew Ahrens /* You can only clone snapshots, not the head datasets. */ 569ae46e4c7SMatthew Ahrens if (!dsl_dataset_is_snapshot(oa->clone_origin)) 5701d452cf5Sahrens return (EINVAL); 5711d452cf5Sahrens } 572ecd6cf80Smarks 5731d452cf5Sahrens return (0); 5741d452cf5Sahrens } 5751d452cf5Sahrens 5761d452cf5Sahrens static void 577ecd6cf80Smarks dmu_objset_create_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 5781d452cf5Sahrens { 5791d452cf5Sahrens dsl_dir_t *dd = arg1; 5801d452cf5Sahrens struct oscarg *oa = arg2; 5811d452cf5Sahrens uint64_t dsobj; 582fa9e4066Sahrens 583fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 584fa9e4066Sahrens 5851d452cf5Sahrens dsobj = dsl_dataset_create_sync(dd, oa->lastname, 586ae46e4c7SMatthew Ahrens oa->clone_origin, oa->flags, cr, tx); 587fa9e4066Sahrens 588ae46e4c7SMatthew Ahrens if (oa->clone_origin == NULL) { 589ae46e4c7SMatthew Ahrens dsl_dataset_t *ds; 590ae46e4c7SMatthew Ahrens blkptr_t *bp; 591*503ad85cSMatthew Ahrens objset_t *os; 592fa9e4066Sahrens 593ae46e4c7SMatthew Ahrens VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, dsobj, 594ae46e4c7SMatthew Ahrens FTAG, &ds)); 595ae46e4c7SMatthew Ahrens bp = dsl_dataset_get_blkptr(ds); 596ae46e4c7SMatthew Ahrens ASSERT(BP_IS_HOLE(bp)); 597ae46e4c7SMatthew Ahrens 598*503ad85cSMatthew Ahrens os = dmu_objset_create_impl(dsl_dataset_get_spa(ds), 599c717a561Smaybee ds, bp, oa->type, tx); 600fa9e4066Sahrens 601fa9e4066Sahrens if (oa->userfunc) 602*503ad85cSMatthew Ahrens oa->userfunc(os, oa->userarg, cr, tx); 603ae46e4c7SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 604fa9e4066Sahrens } 605ecd6cf80Smarks 606ecd6cf80Smarks spa_history_internal_log(LOG_DS_CREATE, dd->dd_pool->dp_spa, 607ecd6cf80Smarks tx, cr, "dataset = %llu", dsobj); 608fa9e4066Sahrens } 609fa9e4066Sahrens 610fa9e4066Sahrens int 611ae46e4c7SMatthew Ahrens dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags, 612ecd6cf80Smarks void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg) 613fa9e4066Sahrens { 6141d452cf5Sahrens dsl_dir_t *pdd; 615fa9e4066Sahrens const char *tail; 616fa9e4066Sahrens int err = 0; 6171d452cf5Sahrens struct oscarg oa = { 0 }; 618fa9e4066Sahrens 6191d452cf5Sahrens ASSERT(strchr(name, '@') == NULL); 6201d452cf5Sahrens err = dsl_dir_open(name, FTAG, &pdd, &tail); 621ea8dc4b6Seschrock if (err) 622ea8dc4b6Seschrock return (err); 623fa9e4066Sahrens if (tail == NULL) { 6241d452cf5Sahrens dsl_dir_close(pdd, FTAG); 625fa9e4066Sahrens return (EEXIST); 626fa9e4066Sahrens } 627fa9e4066Sahrens 6281d452cf5Sahrens oa.userfunc = func; 6291d452cf5Sahrens oa.userarg = arg; 6301d452cf5Sahrens oa.lastname = tail; 6311d452cf5Sahrens oa.type = type; 632ab04eb8eStimh oa.flags = flags; 633ecd6cf80Smarks 634ae46e4c7SMatthew Ahrens err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check, 635ae46e4c7SMatthew Ahrens dmu_objset_create_sync, pdd, &oa, 5); 636ae46e4c7SMatthew Ahrens dsl_dir_close(pdd, FTAG); 637ae46e4c7SMatthew Ahrens return (err); 638ae46e4c7SMatthew Ahrens } 639ae46e4c7SMatthew Ahrens 640ae46e4c7SMatthew Ahrens int 641ae46e4c7SMatthew Ahrens dmu_objset_clone(const char *name, dsl_dataset_t *clone_origin, uint64_t flags) 642ae46e4c7SMatthew Ahrens { 643ae46e4c7SMatthew Ahrens dsl_dir_t *pdd; 644ae46e4c7SMatthew Ahrens const char *tail; 645ae46e4c7SMatthew Ahrens int err = 0; 646ae46e4c7SMatthew Ahrens struct oscarg oa = { 0 }; 647ae46e4c7SMatthew Ahrens 648ae46e4c7SMatthew Ahrens ASSERT(strchr(name, '@') == NULL); 649ae46e4c7SMatthew Ahrens err = dsl_dir_open(name, FTAG, &pdd, &tail); 650ae46e4c7SMatthew Ahrens if (err) 651ae46e4c7SMatthew Ahrens return (err); 652ae46e4c7SMatthew Ahrens if (tail == NULL) { 653ae46e4c7SMatthew Ahrens dsl_dir_close(pdd, FTAG); 654ae46e4c7SMatthew Ahrens return (EEXIST); 655fa9e4066Sahrens } 656ae46e4c7SMatthew Ahrens 657ae46e4c7SMatthew Ahrens oa.lastname = tail; 658ae46e4c7SMatthew Ahrens oa.clone_origin = clone_origin; 659ae46e4c7SMatthew Ahrens oa.flags = flags; 660ae46e4c7SMatthew Ahrens 6611d452cf5Sahrens err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check, 6621d452cf5Sahrens dmu_objset_create_sync, pdd, &oa, 5); 6631d452cf5Sahrens dsl_dir_close(pdd, FTAG); 664fa9e4066Sahrens return (err); 665fa9e4066Sahrens } 666fa9e4066Sahrens 667fa9e4066Sahrens int 668842727c2SChris Kirby dmu_objset_destroy(const char *name, boolean_t defer) 669fa9e4066Sahrens { 670*503ad85cSMatthew Ahrens dsl_dataset_t *ds; 671fa9e4066Sahrens int error; 672fa9e4066Sahrens 673fa9e4066Sahrens /* 674ae46e4c7SMatthew Ahrens * dsl_dataset_destroy() can free any claimed-but-unplayed 675ae46e4c7SMatthew Ahrens * intent log, but if there is an active log, it has blocks that 676ae46e4c7SMatthew Ahrens * are allocated, but may not yet be reflected in the on-disk 677ae46e4c7SMatthew Ahrens * structure. Only the ZIL knows how to free them, so we have 678ae46e4c7SMatthew Ahrens * to call into it here. 679fa9e4066Sahrens */ 680*503ad85cSMatthew Ahrens error = dsl_dataset_own(name, B_TRUE, FTAG, &ds); 681fa9e4066Sahrens if (error == 0) { 682*503ad85cSMatthew Ahrens objset_t *os; 683*503ad85cSMatthew Ahrens if (dmu_objset_from_ds(ds, &os) == 0) 684*503ad85cSMatthew Ahrens zil_destroy(dmu_objset_zil(os), B_FALSE); 685*503ad85cSMatthew Ahrens error = dsl_dataset_destroy(ds, FTAG, defer); 686ae46e4c7SMatthew Ahrens /* dsl_dataset_destroy() closes the ds. */ 687fa9e4066Sahrens } 688fa9e4066Sahrens 6893cb34c60Sahrens return (error); 690fa9e4066Sahrens } 691fa9e4066Sahrens 6921d452cf5Sahrens struct snaparg { 6931d452cf5Sahrens dsl_sync_task_group_t *dstg; 6941d452cf5Sahrens char *snapname; 6951d452cf5Sahrens char failed[MAXPATHLEN]; 696ecd6cf80Smarks boolean_t checkperms; 697ea2f5b9eSMatthew Ahrens nvlist_t *props; 6983cb34c60Sahrens }; 6993cb34c60Sahrens 700ea2f5b9eSMatthew Ahrens static int 701ea2f5b9eSMatthew Ahrens snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx) 702ea2f5b9eSMatthew Ahrens { 703ea2f5b9eSMatthew Ahrens objset_t *os = arg1; 704ea2f5b9eSMatthew Ahrens struct snaparg *sn = arg2; 705ea2f5b9eSMatthew Ahrens 706ea2f5b9eSMatthew Ahrens /* The props have already been checked by zfs_check_userprops(). */ 707ea2f5b9eSMatthew Ahrens 708*503ad85cSMatthew Ahrens return (dsl_dataset_snapshot_check(os->os_dsl_dataset, 709ea2f5b9eSMatthew Ahrens sn->snapname, tx)); 710ea2f5b9eSMatthew Ahrens } 711ea2f5b9eSMatthew Ahrens 712ea2f5b9eSMatthew Ahrens static void 713ea2f5b9eSMatthew Ahrens snapshot_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 714ea2f5b9eSMatthew Ahrens { 715ea2f5b9eSMatthew Ahrens objset_t *os = arg1; 716*503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 717ea2f5b9eSMatthew Ahrens struct snaparg *sn = arg2; 718ea2f5b9eSMatthew Ahrens 719ea2f5b9eSMatthew Ahrens dsl_dataset_snapshot_sync(ds, sn->snapname, cr, tx); 720ea2f5b9eSMatthew Ahrens 721ea2f5b9eSMatthew Ahrens if (sn->props) 722ea2f5b9eSMatthew Ahrens dsl_props_set_sync(ds->ds_prev, sn->props, cr, tx); 723ea2f5b9eSMatthew Ahrens } 7241d452cf5Sahrens 7251d452cf5Sahrens static int 7261d452cf5Sahrens dmu_objset_snapshot_one(char *name, void *arg) 7271d452cf5Sahrens { 7281d452cf5Sahrens struct snaparg *sn = arg; 7291d452cf5Sahrens objset_t *os; 7301d452cf5Sahrens int err; 7311d452cf5Sahrens 7321d452cf5Sahrens (void) strcpy(sn->failed, name); 7331d452cf5Sahrens 734ecd6cf80Smarks /* 735ecd6cf80Smarks * Check permissions only when requested. This only applies when 736ecd6cf80Smarks * doing a recursive snapshot. The permission checks for the starting 737ecd6cf80Smarks * dataset have already been performed in zfs_secpolicy_snapshot() 738ecd6cf80Smarks */ 739ecd6cf80Smarks if (sn->checkperms == B_TRUE && 740ecd6cf80Smarks (err = zfs_secpolicy_snapshot_perms(name, CRED()))) 741ecd6cf80Smarks return (err); 742ecd6cf80Smarks 743*503ad85cSMatthew Ahrens err = dmu_objset_hold(name, sn, &os); 7441d452cf5Sahrens if (err != 0) 7451d452cf5Sahrens return (err); 7461d452cf5Sahrens 747745cd3c5Smaybee /* If the objset is in an inconsistent state, return busy */ 748*503ad85cSMatthew Ahrens if (os->os_dsl_dataset->ds_phys->ds_flags & DS_FLAG_INCONSISTENT) { 749*503ad85cSMatthew Ahrens dmu_objset_rele(os, sn); 750f2e10be3Srm return (EBUSY); 751f2e10be3Srm } 752f2e10be3Srm 7531d452cf5Sahrens /* 7541d452cf5Sahrens * NB: we need to wait for all in-flight changes to get to disk, 7551d452cf5Sahrens * so that we snapshot those changes. zil_suspend does this as 7561d452cf5Sahrens * a side effect. 7571d452cf5Sahrens */ 7581d452cf5Sahrens err = zil_suspend(dmu_objset_zil(os)); 7591d452cf5Sahrens if (err == 0) { 760ea2f5b9eSMatthew Ahrens dsl_sync_task_create(sn->dstg, snapshot_check, 761ea2f5b9eSMatthew Ahrens snapshot_sync, os, sn, 3); 762f2e10be3Srm } else { 763*503ad85cSMatthew Ahrens dmu_objset_rele(os, sn); 7641d452cf5Sahrens } 765f2e10be3Srm 7661d452cf5Sahrens return (err); 7671d452cf5Sahrens } 7681d452cf5Sahrens 7691d452cf5Sahrens int 770ea2f5b9eSMatthew Ahrens dmu_objset_snapshot(char *fsname, char *snapname, 771ea2f5b9eSMatthew Ahrens nvlist_t *props, boolean_t recursive) 7721d452cf5Sahrens { 7731d452cf5Sahrens dsl_sync_task_t *dst; 774ea2f5b9eSMatthew Ahrens struct snaparg sn; 7751d452cf5Sahrens spa_t *spa; 7761d452cf5Sahrens int err; 7771d452cf5Sahrens 7781d452cf5Sahrens (void) strcpy(sn.failed, fsname); 7791d452cf5Sahrens 78040feaa91Sahrens err = spa_open(fsname, &spa, FTAG); 7811d452cf5Sahrens if (err) 7821d452cf5Sahrens return (err); 7831d452cf5Sahrens 7841d452cf5Sahrens sn.dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 7851d452cf5Sahrens sn.snapname = snapname; 786ea2f5b9eSMatthew Ahrens sn.props = props; 7871d452cf5Sahrens 7880b69c2f0Sahrens if (recursive) { 789ecd6cf80Smarks sn.checkperms = B_TRUE; 7900b69c2f0Sahrens err = dmu_objset_find(fsname, 7910b69c2f0Sahrens dmu_objset_snapshot_one, &sn, DS_FIND_CHILDREN); 7920b69c2f0Sahrens } else { 793ecd6cf80Smarks sn.checkperms = B_FALSE; 7941d452cf5Sahrens err = dmu_objset_snapshot_one(fsname, &sn); 7950b69c2f0Sahrens } 7961d452cf5Sahrens 797ea2f5b9eSMatthew Ahrens if (err == 0) 798ea2f5b9eSMatthew Ahrens err = dsl_sync_task_group_wait(sn.dstg); 7991d452cf5Sahrens 8001d452cf5Sahrens for (dst = list_head(&sn.dstg->dstg_tasks); dst; 8011d452cf5Sahrens dst = list_next(&sn.dstg->dstg_tasks, dst)) { 802ea2f5b9eSMatthew Ahrens objset_t *os = dst->dst_arg1; 803*503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 8041d452cf5Sahrens if (dst->dst_err) 8053cb34c60Sahrens dsl_dataset_name(ds, sn.failed); 806ea2f5b9eSMatthew Ahrens zil_resume(dmu_objset_zil(os)); 807*503ad85cSMatthew Ahrens dmu_objset_rele(os, &sn); 8083cb34c60Sahrens } 8093cb34c60Sahrens 8101d452cf5Sahrens if (err) 8111d452cf5Sahrens (void) strcpy(fsname, sn.failed); 8121d452cf5Sahrens dsl_sync_task_group_destroy(sn.dstg); 8131d452cf5Sahrens spa_close(spa, FTAG); 8141d452cf5Sahrens return (err); 8151d452cf5Sahrens } 8161d452cf5Sahrens 817fa9e4066Sahrens static void 81814843421SMatthew Ahrens dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx) 819fa9e4066Sahrens { 820c717a561Smaybee dnode_t *dn; 821faafa6e3Sahrens 822c717a561Smaybee while (dn = list_head(list)) { 823c717a561Smaybee ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT); 824c717a561Smaybee ASSERT(dn->dn_dbuf->db_data_pending); 825c717a561Smaybee /* 82614843421SMatthew Ahrens * Initialize dn_zio outside dnode_sync() because the 82714843421SMatthew Ahrens * meta-dnode needs to set it ouside dnode_sync(). 828c717a561Smaybee */ 829c717a561Smaybee dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio; 830c717a561Smaybee ASSERT(dn->dn_zio); 831faafa6e3Sahrens 832c717a561Smaybee ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS); 833c717a561Smaybee list_remove(list, dn); 83414843421SMatthew Ahrens 83514843421SMatthew Ahrens if (newlist) { 83614843421SMatthew Ahrens (void) dnode_add_ref(dn, newlist); 83714843421SMatthew Ahrens list_insert_tail(newlist, dn); 83814843421SMatthew Ahrens } 83914843421SMatthew Ahrens 840c717a561Smaybee dnode_sync(dn, tx); 841fa9e4066Sahrens } 842fa9e4066Sahrens } 843fa9e4066Sahrens 844fa9e4066Sahrens /* ARGSUSED */ 845fa9e4066Sahrens static void 846c717a561Smaybee ready(zio_t *zio, arc_buf_t *abuf, void *arg) 847fa9e4066Sahrens { 848e14bb325SJeff Bonwick blkptr_t *bp = zio->io_bp; 849e14bb325SJeff Bonwick blkptr_t *bp_orig = &zio->io_bp_orig; 850*503ad85cSMatthew Ahrens objset_t *os = arg; 851c717a561Smaybee dnode_phys_t *dnp = &os->os_phys->os_meta_dnode; 852fa9e4066Sahrens 853e14bb325SJeff Bonwick ASSERT(bp == os->os_rootbp); 854e14bb325SJeff Bonwick ASSERT(BP_GET_TYPE(bp) == DMU_OT_OBJSET); 855e14bb325SJeff Bonwick ASSERT(BP_GET_LEVEL(bp) == 0); 8560a4e9518Sgw 857fa9e4066Sahrens /* 85814843421SMatthew Ahrens * Update rootbp fill count: it should be the number of objects 85914843421SMatthew Ahrens * allocated in the object set (not counting the "special" 86014843421SMatthew Ahrens * objects that are stored in the objset_phys_t -- the meta 86114843421SMatthew Ahrens * dnode and user/group accounting objects). 862fa9e4066Sahrens */ 86314843421SMatthew Ahrens bp->blk_fill = 0; 864e14bb325SJeff Bonwick for (int i = 0; i < dnp->dn_nblkptr; i++) 865c717a561Smaybee bp->blk_fill += dnp->dn_blkptr[i].blk_fill; 8660a4e9518Sgw 867e14bb325SJeff Bonwick if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { 868e14bb325SJeff Bonwick ASSERT(DVA_EQUAL(BP_IDENTITY(bp), BP_IDENTITY(bp_orig))); 869e14bb325SJeff Bonwick } else { 8700a4e9518Sgw if (zio->io_bp_orig.blk_birth == os->os_synctx->tx_txg) 871cdb0ab79Smaybee (void) dsl_dataset_block_kill(os->os_dsl_dataset, 872e14bb325SJeff Bonwick &zio->io_bp_orig, zio, os->os_synctx); 8730a4e9518Sgw dsl_dataset_block_born(os->os_dsl_dataset, bp, os->os_synctx); 8740a4e9518Sgw } 875c717a561Smaybee } 876c717a561Smaybee 877fa9e4066Sahrens /* called from dsl */ 878fa9e4066Sahrens void 879*503ad85cSMatthew Ahrens dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx) 880fa9e4066Sahrens { 881fa9e4066Sahrens int txgoff; 882ea8dc4b6Seschrock zbookmark_t zb; 883088f3894Sahrens writeprops_t wp = { 0 }; 884c717a561Smaybee zio_t *zio; 885c717a561Smaybee list_t *list; 88614843421SMatthew Ahrens list_t *newlist = NULL; 887c717a561Smaybee dbuf_dirty_record_t *dr; 888c717a561Smaybee 889c717a561Smaybee dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg); 890fa9e4066Sahrens 891fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 892fa9e4066Sahrens /* XXX the write_done callback should really give us the tx... */ 893fa9e4066Sahrens os->os_synctx = tx; 894fa9e4066Sahrens 89587bd5c1eSahrens if (os->os_dsl_dataset == NULL) { 89687bd5c1eSahrens /* 89787bd5c1eSahrens * This is the MOS. If we have upgraded, 89887bd5c1eSahrens * spa_max_replication() could change, so reset 89987bd5c1eSahrens * os_copies here. 90087bd5c1eSahrens */ 90187bd5c1eSahrens os->os_copies = spa_max_replication(os->os_spa); 90287bd5c1eSahrens } 90387bd5c1eSahrens 904fa9e4066Sahrens /* 905c717a561Smaybee * Create the root block IO 906fa9e4066Sahrens */ 907ea8dc4b6Seschrock zb.zb_objset = os->os_dsl_dataset ? os->os_dsl_dataset->ds_object : 0; 908ea8dc4b6Seschrock zb.zb_object = 0; 909e14bb325SJeff Bonwick zb.zb_level = -1; /* for block ordering; it's level 0 on disk */ 910ea8dc4b6Seschrock zb.zb_blkid = 0; 911e14bb325SJeff Bonwick 912088f3894Sahrens wp.wp_type = DMU_OT_OBJSET; 913e14bb325SJeff Bonwick wp.wp_level = 0; /* on-disk BP level; see above */ 914088f3894Sahrens wp.wp_copies = os->os_copies; 915088f3894Sahrens wp.wp_oschecksum = os->os_checksum; 916088f3894Sahrens wp.wp_oscompress = os->os_compress; 917e14bb325SJeff Bonwick 918e14bb325SJeff Bonwick if (BP_IS_OLDER(os->os_rootbp, tx->tx_txg)) { 919e14bb325SJeff Bonwick (void) dsl_dataset_block_kill(os->os_dsl_dataset, 920e14bb325SJeff Bonwick os->os_rootbp, pio, tx); 921e14bb325SJeff Bonwick } 922e14bb325SJeff Bonwick 923088f3894Sahrens arc_release(os->os_phys_buf, &os->os_phys_buf); 92414843421SMatthew Ahrens 925e14bb325SJeff Bonwick zio = arc_write(pio, os->os_spa, &wp, DMU_OS_IS_L2CACHEABLE(os), 926e14bb325SJeff Bonwick tx->tx_txg, os->os_rootbp, os->os_phys_buf, ready, NULL, os, 927e14bb325SJeff Bonwick ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb); 928c717a561Smaybee 929c717a561Smaybee /* 93014843421SMatthew Ahrens * Sync special dnodes - the parent IO for the sync is the root block 931c717a561Smaybee */ 932c717a561Smaybee os->os_meta_dnode->dn_zio = zio; 933c717a561Smaybee dnode_sync(os->os_meta_dnode, tx); 934c717a561Smaybee 93514843421SMatthew Ahrens os->os_phys->os_flags = os->os_flags; 93614843421SMatthew Ahrens 93714843421SMatthew Ahrens if (os->os_userused_dnode && 93814843421SMatthew Ahrens os->os_userused_dnode->dn_type != DMU_OT_NONE) { 93914843421SMatthew Ahrens os->os_userused_dnode->dn_zio = zio; 94014843421SMatthew Ahrens dnode_sync(os->os_userused_dnode, tx); 94114843421SMatthew Ahrens os->os_groupused_dnode->dn_zio = zio; 94214843421SMatthew Ahrens dnode_sync(os->os_groupused_dnode, tx); 94314843421SMatthew Ahrens } 94414843421SMatthew Ahrens 945c717a561Smaybee txgoff = tx->tx_txg & TXG_MASK; 946fa9e4066Sahrens 94714843421SMatthew Ahrens if (dmu_objset_userused_enabled(os)) { 94814843421SMatthew Ahrens newlist = &os->os_synced_dnodes; 94914843421SMatthew Ahrens /* 95014843421SMatthew Ahrens * We must create the list here because it uses the 95114843421SMatthew Ahrens * dn_dirty_link[] of this txg. 95214843421SMatthew Ahrens */ 95314843421SMatthew Ahrens list_create(newlist, sizeof (dnode_t), 95414843421SMatthew Ahrens offsetof(dnode_t, dn_dirty_link[txgoff])); 95514843421SMatthew Ahrens } 95614843421SMatthew Ahrens 95714843421SMatthew Ahrens dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx); 95814843421SMatthew Ahrens dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx); 959fa9e4066Sahrens 960c717a561Smaybee list = &os->os_meta_dnode->dn_dirty_records[txgoff]; 961c717a561Smaybee while (dr = list_head(list)) { 962c717a561Smaybee ASSERT(dr->dr_dbuf->db_level == 0); 963c717a561Smaybee list_remove(list, dr); 964c717a561Smaybee if (dr->dr_zio) 965c717a561Smaybee zio_nowait(dr->dr_zio); 966c717a561Smaybee } 967c717a561Smaybee /* 968c717a561Smaybee * Free intent log blocks up to this tx. 969c717a561Smaybee */ 970c717a561Smaybee zil_sync(os->os_zil, tx); 971088f3894Sahrens os->os_phys->os_zil_header = os->os_zil_header; 972c717a561Smaybee zio_nowait(zio); 973fa9e4066Sahrens } 974fa9e4066Sahrens 97514843421SMatthew Ahrens static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES]; 97614843421SMatthew Ahrens 97714843421SMatthew Ahrens void 97814843421SMatthew Ahrens dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb) 97914843421SMatthew Ahrens { 98014843421SMatthew Ahrens used_cbs[ost] = cb; 98114843421SMatthew Ahrens } 98214843421SMatthew Ahrens 98314843421SMatthew Ahrens boolean_t 984*503ad85cSMatthew Ahrens dmu_objset_userused_enabled(objset_t *os) 98514843421SMatthew Ahrens { 98614843421SMatthew Ahrens return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE && 98714843421SMatthew Ahrens used_cbs[os->os_phys->os_type] && 98814843421SMatthew Ahrens os->os_userused_dnode); 98914843421SMatthew Ahrens } 99014843421SMatthew Ahrens 99114843421SMatthew Ahrens void 992*503ad85cSMatthew Ahrens dmu_objset_do_userquota_callbacks(objset_t *os, dmu_tx_t *tx) 99314843421SMatthew Ahrens { 99414843421SMatthew Ahrens dnode_t *dn; 99514843421SMatthew Ahrens list_t *list = &os->os_synced_dnodes; 99614843421SMatthew Ahrens static const char zerobuf[DN_MAX_BONUSLEN] = {0}; 99714843421SMatthew Ahrens 99814843421SMatthew Ahrens ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os)); 99914843421SMatthew Ahrens 100014843421SMatthew Ahrens while (dn = list_head(list)) { 100114843421SMatthew Ahrens dmu_object_type_t bonustype; 100214843421SMatthew Ahrens 100314843421SMatthew Ahrens ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object)); 100414843421SMatthew Ahrens ASSERT(dn->dn_oldphys); 100514843421SMatthew Ahrens ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE || 100614843421SMatthew Ahrens dn->dn_phys->dn_flags & 100714843421SMatthew Ahrens DNODE_FLAG_USERUSED_ACCOUNTED); 100814843421SMatthew Ahrens 100914843421SMatthew Ahrens /* Allocate the user/groupused objects if necessary. */ 101014843421SMatthew Ahrens if (os->os_userused_dnode->dn_type == DMU_OT_NONE) { 1011*503ad85cSMatthew Ahrens VERIFY(0 == zap_create_claim(os, 101214843421SMatthew Ahrens DMU_USERUSED_OBJECT, 101314843421SMatthew Ahrens DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx)); 1014*503ad85cSMatthew Ahrens VERIFY(0 == zap_create_claim(os, 101514843421SMatthew Ahrens DMU_GROUPUSED_OBJECT, 101614843421SMatthew Ahrens DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx)); 101714843421SMatthew Ahrens } 101814843421SMatthew Ahrens 101914843421SMatthew Ahrens /* 102014843421SMatthew Ahrens * If the object was not previously 102114843421SMatthew Ahrens * accounted, pretend that it was free. 102214843421SMatthew Ahrens */ 102314843421SMatthew Ahrens if (!(dn->dn_oldphys->dn_flags & 102414843421SMatthew Ahrens DNODE_FLAG_USERUSED_ACCOUNTED)) { 102514843421SMatthew Ahrens bzero(dn->dn_oldphys, sizeof (dnode_phys_t)); 102614843421SMatthew Ahrens } 102714843421SMatthew Ahrens 102814843421SMatthew Ahrens /* 102914843421SMatthew Ahrens * If the object was freed, use the previous bonustype. 103014843421SMatthew Ahrens */ 103114843421SMatthew Ahrens bonustype = dn->dn_phys->dn_bonustype ? 103214843421SMatthew Ahrens dn->dn_phys->dn_bonustype : dn->dn_oldphys->dn_bonustype; 103314843421SMatthew Ahrens ASSERT(dn->dn_phys->dn_type != 0 || 103414843421SMatthew Ahrens (bcmp(DN_BONUS(dn->dn_phys), zerobuf, 103514843421SMatthew Ahrens DN_MAX_BONUSLEN) == 0 && 103614843421SMatthew Ahrens DN_USED_BYTES(dn->dn_phys) == 0)); 103714843421SMatthew Ahrens ASSERT(dn->dn_oldphys->dn_type != 0 || 103814843421SMatthew Ahrens (bcmp(DN_BONUS(dn->dn_oldphys), zerobuf, 103914843421SMatthew Ahrens DN_MAX_BONUSLEN) == 0 && 104014843421SMatthew Ahrens DN_USED_BYTES(dn->dn_oldphys) == 0)); 1041*503ad85cSMatthew Ahrens used_cbs[os->os_phys->os_type](os, bonustype, 104214843421SMatthew Ahrens DN_BONUS(dn->dn_oldphys), DN_BONUS(dn->dn_phys), 104314843421SMatthew Ahrens DN_USED_BYTES(dn->dn_oldphys), 104414843421SMatthew Ahrens DN_USED_BYTES(dn->dn_phys), tx); 104514843421SMatthew Ahrens 104614843421SMatthew Ahrens /* 104714843421SMatthew Ahrens * The mutex is needed here for interlock with dnode_allocate. 104814843421SMatthew Ahrens */ 104914843421SMatthew Ahrens mutex_enter(&dn->dn_mtx); 105014843421SMatthew Ahrens zio_buf_free(dn->dn_oldphys, sizeof (dnode_phys_t)); 105114843421SMatthew Ahrens dn->dn_oldphys = NULL; 105214843421SMatthew Ahrens mutex_exit(&dn->dn_mtx); 105314843421SMatthew Ahrens 105414843421SMatthew Ahrens list_remove(list, dn); 105514843421SMatthew Ahrens dnode_rele(dn, list); 105614843421SMatthew Ahrens } 105714843421SMatthew Ahrens } 105814843421SMatthew Ahrens 105914843421SMatthew Ahrens boolean_t 106014843421SMatthew Ahrens dmu_objset_userspace_present(objset_t *os) 106114843421SMatthew Ahrens { 1062*503ad85cSMatthew Ahrens return (os->os_phys->os_flags & 106314843421SMatthew Ahrens OBJSET_FLAG_USERACCOUNTING_COMPLETE); 106414843421SMatthew Ahrens } 106514843421SMatthew Ahrens 106614843421SMatthew Ahrens int 106714843421SMatthew Ahrens dmu_objset_userspace_upgrade(objset_t *os) 106814843421SMatthew Ahrens { 106914843421SMatthew Ahrens uint64_t obj; 107014843421SMatthew Ahrens int err = 0; 107114843421SMatthew Ahrens 107214843421SMatthew Ahrens if (dmu_objset_userspace_present(os)) 107314843421SMatthew Ahrens return (0); 1074*503ad85cSMatthew Ahrens if (!dmu_objset_userused_enabled(os)) 107514843421SMatthew Ahrens return (ENOTSUP); 107614843421SMatthew Ahrens if (dmu_objset_is_snapshot(os)) 107714843421SMatthew Ahrens return (EINVAL); 107814843421SMatthew Ahrens 107914843421SMatthew Ahrens /* 108014843421SMatthew Ahrens * We simply need to mark every object dirty, so that it will be 108114843421SMatthew Ahrens * synced out and now accounted. If this is called 108214843421SMatthew Ahrens * concurrently, or if we already did some work before crashing, 108314843421SMatthew Ahrens * that's fine, since we track each object's accounted state 108414843421SMatthew Ahrens * independently. 108514843421SMatthew Ahrens */ 108614843421SMatthew Ahrens 108714843421SMatthew Ahrens for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) { 108868857716SLin Ling dmu_tx_t *tx; 108914843421SMatthew Ahrens dmu_buf_t *db; 109014843421SMatthew Ahrens int objerr; 109114843421SMatthew Ahrens 109214843421SMatthew Ahrens if (issig(JUSTLOOKING) && issig(FORREAL)) 109314843421SMatthew Ahrens return (EINTR); 109414843421SMatthew Ahrens 109514843421SMatthew Ahrens objerr = dmu_bonus_hold(os, obj, FTAG, &db); 109614843421SMatthew Ahrens if (objerr) 109714843421SMatthew Ahrens continue; 109868857716SLin Ling tx = dmu_tx_create(os); 109914843421SMatthew Ahrens dmu_tx_hold_bonus(tx, obj); 110014843421SMatthew Ahrens objerr = dmu_tx_assign(tx, TXG_WAIT); 110114843421SMatthew Ahrens if (objerr) { 110214843421SMatthew Ahrens dmu_tx_abort(tx); 110314843421SMatthew Ahrens continue; 110414843421SMatthew Ahrens } 110514843421SMatthew Ahrens dmu_buf_will_dirty(db, tx); 110614843421SMatthew Ahrens dmu_buf_rele(db, FTAG); 110714843421SMatthew Ahrens dmu_tx_commit(tx); 110814843421SMatthew Ahrens } 110914843421SMatthew Ahrens 1110*503ad85cSMatthew Ahrens os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE; 111114843421SMatthew Ahrens txg_wait_synced(dmu_objset_pool(os), 0); 111214843421SMatthew Ahrens return (0); 111314843421SMatthew Ahrens } 111414843421SMatthew Ahrens 1115fa9e4066Sahrens void 1116a2eea2e1Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp, 1117a2eea2e1Sahrens uint64_t *usedobjsp, uint64_t *availobjsp) 1118fa9e4066Sahrens { 1119*503ad85cSMatthew Ahrens dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp, 1120a2eea2e1Sahrens usedobjsp, availobjsp); 1121a2eea2e1Sahrens } 1122a2eea2e1Sahrens 1123a2eea2e1Sahrens uint64_t 1124a2eea2e1Sahrens dmu_objset_fsid_guid(objset_t *os) 1125a2eea2e1Sahrens { 1126*503ad85cSMatthew Ahrens return (dsl_dataset_fsid_guid(os->os_dsl_dataset)); 1127a2eea2e1Sahrens } 1128a2eea2e1Sahrens 1129a2eea2e1Sahrens void 1130a2eea2e1Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat) 1131a2eea2e1Sahrens { 1132*503ad85cSMatthew Ahrens stat->dds_type = os->os_phys->os_type; 1133*503ad85cSMatthew Ahrens if (os->os_dsl_dataset) 1134*503ad85cSMatthew Ahrens dsl_dataset_fast_stat(os->os_dsl_dataset, stat); 1135a2eea2e1Sahrens } 1136a2eea2e1Sahrens 1137a2eea2e1Sahrens void 1138a2eea2e1Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv) 1139a2eea2e1Sahrens { 1140*503ad85cSMatthew Ahrens ASSERT(os->os_dsl_dataset || 1141*503ad85cSMatthew Ahrens os->os_phys->os_type == DMU_OST_META); 1142a2eea2e1Sahrens 1143*503ad85cSMatthew Ahrens if (os->os_dsl_dataset != NULL) 1144*503ad85cSMatthew Ahrens dsl_dataset_stats(os->os_dsl_dataset, nv); 1145a2eea2e1Sahrens 1146a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE, 1147*503ad85cSMatthew Ahrens os->os_phys->os_type); 114814843421SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING, 114914843421SMatthew Ahrens dmu_objset_userspace_present(os)); 1150fa9e4066Sahrens } 1151fa9e4066Sahrens 1152fa9e4066Sahrens int 1153fa9e4066Sahrens dmu_objset_is_snapshot(objset_t *os) 1154fa9e4066Sahrens { 1155*503ad85cSMatthew Ahrens if (os->os_dsl_dataset != NULL) 1156*503ad85cSMatthew Ahrens return (dsl_dataset_is_snapshot(os->os_dsl_dataset)); 1157fa9e4066Sahrens else 1158fa9e4066Sahrens return (B_FALSE); 1159fa9e4066Sahrens } 1160fa9e4066Sahrens 1161ab04eb8eStimh int 1162ab04eb8eStimh dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen, 1163ab04eb8eStimh boolean_t *conflict) 1164ab04eb8eStimh { 1165*503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 1166ab04eb8eStimh uint64_t ignored; 1167ab04eb8eStimh 1168ab04eb8eStimh if (ds->ds_phys->ds_snapnames_zapobj == 0) 1169ab04eb8eStimh return (ENOENT); 1170ab04eb8eStimh 1171ab04eb8eStimh return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset, 1172ab04eb8eStimh ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST, 1173ab04eb8eStimh real, maxlen, conflict)); 1174ab04eb8eStimh } 1175ab04eb8eStimh 1176fa9e4066Sahrens int 1177fa9e4066Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name, 1178b38f0970Sck uint64_t *idp, uint64_t *offp, boolean_t *case_conflict) 1179fa9e4066Sahrens { 1180*503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 1181fa9e4066Sahrens zap_cursor_t cursor; 1182fa9e4066Sahrens zap_attribute_t attr; 1183fa9e4066Sahrens 1184fa9e4066Sahrens if (ds->ds_phys->ds_snapnames_zapobj == 0) 1185fa9e4066Sahrens return (ENOENT); 1186fa9e4066Sahrens 1187fa9e4066Sahrens zap_cursor_init_serialized(&cursor, 1188fa9e4066Sahrens ds->ds_dir->dd_pool->dp_meta_objset, 1189fa9e4066Sahrens ds->ds_phys->ds_snapnames_zapobj, *offp); 1190fa9e4066Sahrens 119187e5029aSahrens if (zap_cursor_retrieve(&cursor, &attr) != 0) { 119287e5029aSahrens zap_cursor_fini(&cursor); 119387e5029aSahrens return (ENOENT); 119487e5029aSahrens } 119587e5029aSahrens 119687e5029aSahrens if (strlen(attr.za_name) + 1 > namelen) { 119787e5029aSahrens zap_cursor_fini(&cursor); 119887e5029aSahrens return (ENAMETOOLONG); 119987e5029aSahrens } 120087e5029aSahrens 120187e5029aSahrens (void) strcpy(name, attr.za_name); 120287e5029aSahrens if (idp) 120387e5029aSahrens *idp = attr.za_first_integer; 1204b38f0970Sck if (case_conflict) 1205b38f0970Sck *case_conflict = attr.za_normalization_conflict; 120687e5029aSahrens zap_cursor_advance(&cursor); 120787e5029aSahrens *offp = zap_cursor_serialize(&cursor); 120887e5029aSahrens zap_cursor_fini(&cursor); 120987e5029aSahrens 121087e5029aSahrens return (0); 121187e5029aSahrens } 121287e5029aSahrens 121387e5029aSahrens int 121487e5029aSahrens dmu_dir_list_next(objset_t *os, int namelen, char *name, 121587e5029aSahrens uint64_t *idp, uint64_t *offp) 121687e5029aSahrens { 1217*503ad85cSMatthew Ahrens dsl_dir_t *dd = os->os_dsl_dataset->ds_dir; 121887e5029aSahrens zap_cursor_t cursor; 121987e5029aSahrens zap_attribute_t attr; 122087e5029aSahrens 122187e5029aSahrens /* there is no next dir on a snapshot! */ 1222*503ad85cSMatthew Ahrens if (os->os_dsl_dataset->ds_object != 122387e5029aSahrens dd->dd_phys->dd_head_dataset_obj) 1224fa9e4066Sahrens return (ENOENT); 1225fa9e4066Sahrens 122687e5029aSahrens zap_cursor_init_serialized(&cursor, 122787e5029aSahrens dd->dd_pool->dp_meta_objset, 122887e5029aSahrens dd->dd_phys->dd_child_dir_zapobj, *offp); 122987e5029aSahrens 123087e5029aSahrens if (zap_cursor_retrieve(&cursor, &attr) != 0) { 123187e5029aSahrens zap_cursor_fini(&cursor); 123287e5029aSahrens return (ENOENT); 123387e5029aSahrens } 123487e5029aSahrens 123587e5029aSahrens if (strlen(attr.za_name) + 1 > namelen) { 123687e5029aSahrens zap_cursor_fini(&cursor); 1237fa9e4066Sahrens return (ENAMETOOLONG); 123887e5029aSahrens } 1239fa9e4066Sahrens 1240fa9e4066Sahrens (void) strcpy(name, attr.za_name); 124187e5029aSahrens if (idp) 124287e5029aSahrens *idp = attr.za_first_integer; 1243fa9e4066Sahrens zap_cursor_advance(&cursor); 1244fa9e4066Sahrens *offp = zap_cursor_serialize(&cursor); 124587e5029aSahrens zap_cursor_fini(&cursor); 1246fa9e4066Sahrens 1247fa9e4066Sahrens return (0); 1248fa9e4066Sahrens } 1249fa9e4066Sahrens 1250088f3894Sahrens struct findarg { 1251088f3894Sahrens int (*func)(char *, void *); 1252088f3894Sahrens void *arg; 1253088f3894Sahrens }; 1254088f3894Sahrens 1255088f3894Sahrens /* ARGSUSED */ 1256088f3894Sahrens static int 1257088f3894Sahrens findfunc(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg) 1258088f3894Sahrens { 1259088f3894Sahrens struct findarg *fa = arg; 1260088f3894Sahrens return (fa->func((char *)dsname, fa->arg)); 1261088f3894Sahrens } 1262088f3894Sahrens 1263fa9e4066Sahrens /* 1264fa9e4066Sahrens * Find all objsets under name, and for each, call 'func(child_name, arg)'. 1265088f3894Sahrens * Perhaps change all callers to use dmu_objset_find_spa()? 1266fa9e4066Sahrens */ 12671d452cf5Sahrens int 12681d452cf5Sahrens dmu_objset_find(char *name, int func(char *, void *), void *arg, int flags) 1269088f3894Sahrens { 1270088f3894Sahrens struct findarg fa; 1271088f3894Sahrens fa.func = func; 1272088f3894Sahrens fa.arg = arg; 1273088f3894Sahrens return (dmu_objset_find_spa(NULL, name, findfunc, &fa, flags)); 1274088f3894Sahrens } 1275088f3894Sahrens 1276088f3894Sahrens /* 1277088f3894Sahrens * Find all objsets under name, call func on each 1278088f3894Sahrens */ 1279088f3894Sahrens int 1280088f3894Sahrens dmu_objset_find_spa(spa_t *spa, const char *name, 1281088f3894Sahrens int func(spa_t *, uint64_t, const char *, void *), void *arg, int flags) 1282fa9e4066Sahrens { 1283fa9e4066Sahrens dsl_dir_t *dd; 1284088f3894Sahrens dsl_pool_t *dp; 1285088f3894Sahrens dsl_dataset_t *ds; 1286fa9e4066Sahrens zap_cursor_t zc; 1287b7661cccSmmusante zap_attribute_t *attr; 1288fa9e4066Sahrens char *child; 1289088f3894Sahrens uint64_t thisobj; 1290088f3894Sahrens int err; 1291fa9e4066Sahrens 1292088f3894Sahrens if (name == NULL) 1293088f3894Sahrens name = spa_name(spa); 1294088f3894Sahrens err = dsl_dir_open_spa(spa, name, FTAG, &dd, NULL); 1295ea8dc4b6Seschrock if (err) 12961d452cf5Sahrens return (err); 1297fa9e4066Sahrens 1298088f3894Sahrens /* Don't visit hidden ($MOS & $ORIGIN) objsets. */ 1299088f3894Sahrens if (dd->dd_myname[0] == '$') { 1300088f3894Sahrens dsl_dir_close(dd, FTAG); 1301088f3894Sahrens return (0); 1302088f3894Sahrens } 1303088f3894Sahrens 1304088f3894Sahrens thisobj = dd->dd_phys->dd_head_dataset_obj; 1305b7661cccSmmusante attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); 1306088f3894Sahrens dp = dd->dd_pool; 1307fa9e4066Sahrens 1308fa9e4066Sahrens /* 1309fa9e4066Sahrens * Iterate over all children. 1310fa9e4066Sahrens */ 13110b69c2f0Sahrens if (flags & DS_FIND_CHILDREN) { 1312088f3894Sahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, 13130b69c2f0Sahrens dd->dd_phys->dd_child_dir_zapobj); 1314b7661cccSmmusante zap_cursor_retrieve(&zc, attr) == 0; 13150b69c2f0Sahrens (void) zap_cursor_advance(&zc)) { 1316b7661cccSmmusante ASSERT(attr->za_integer_length == sizeof (uint64_t)); 1317b7661cccSmmusante ASSERT(attr->za_num_integers == 1); 1318fa9e4066Sahrens 13190b69c2f0Sahrens child = kmem_alloc(MAXPATHLEN, KM_SLEEP); 1320088f3894Sahrens (void) strcpy(child, name); 13210b69c2f0Sahrens (void) strcat(child, "/"); 1322b7661cccSmmusante (void) strcat(child, attr->za_name); 1323088f3894Sahrens err = dmu_objset_find_spa(spa, child, func, arg, flags); 13240b69c2f0Sahrens kmem_free(child, MAXPATHLEN); 13250b69c2f0Sahrens if (err) 13260b69c2f0Sahrens break; 13270b69c2f0Sahrens } 13280b69c2f0Sahrens zap_cursor_fini(&zc); 13291d452cf5Sahrens 13300b69c2f0Sahrens if (err) { 13310b69c2f0Sahrens dsl_dir_close(dd, FTAG); 1332b7661cccSmmusante kmem_free(attr, sizeof (zap_attribute_t)); 13330b69c2f0Sahrens return (err); 13340b69c2f0Sahrens } 1335fa9e4066Sahrens } 1336fa9e4066Sahrens 1337fa9e4066Sahrens /* 1338fa9e4066Sahrens * Iterate over all snapshots. 1339fa9e4066Sahrens */ 1340088f3894Sahrens if (flags & DS_FIND_SNAPSHOTS) { 1341088f3894Sahrens if (!dsl_pool_sync_context(dp)) 1342088f3894Sahrens rw_enter(&dp->dp_config_rwlock, RW_READER); 1343088f3894Sahrens err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds); 1344088f3894Sahrens if (!dsl_pool_sync_context(dp)) 1345088f3894Sahrens rw_exit(&dp->dp_config_rwlock); 1346088f3894Sahrens 1347088f3894Sahrens if (err == 0) { 1348088f3894Sahrens uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj; 1349088f3894Sahrens dsl_dataset_rele(ds, FTAG); 1350088f3894Sahrens 1351088f3894Sahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj); 1352088f3894Sahrens zap_cursor_retrieve(&zc, attr) == 0; 1353088f3894Sahrens (void) zap_cursor_advance(&zc)) { 1354088f3894Sahrens ASSERT(attr->za_integer_length == 1355088f3894Sahrens sizeof (uint64_t)); 1356088f3894Sahrens ASSERT(attr->za_num_integers == 1); 1357088f3894Sahrens 1358088f3894Sahrens child = kmem_alloc(MAXPATHLEN, KM_SLEEP); 1359088f3894Sahrens (void) strcpy(child, name); 1360088f3894Sahrens (void) strcat(child, "@"); 1361088f3894Sahrens (void) strcat(child, attr->za_name); 1362088f3894Sahrens err = func(spa, attr->za_first_integer, 1363088f3894Sahrens child, arg); 1364088f3894Sahrens kmem_free(child, MAXPATHLEN); 1365088f3894Sahrens if (err) 1366088f3894Sahrens break; 1367088f3894Sahrens } 1368088f3894Sahrens zap_cursor_fini(&zc); 1369fa9e4066Sahrens } 1370fa9e4066Sahrens } 1371fa9e4066Sahrens 1372fa9e4066Sahrens dsl_dir_close(dd, FTAG); 1373b7661cccSmmusante kmem_free(attr, sizeof (zap_attribute_t)); 1374fa9e4066Sahrens 13751d452cf5Sahrens if (err) 13761d452cf5Sahrens return (err); 13771d452cf5Sahrens 1378fa9e4066Sahrens /* 1379fa9e4066Sahrens * Apply to self if appropriate. 1380fa9e4066Sahrens */ 1381088f3894Sahrens err = func(spa, thisobj, name, arg); 13821d452cf5Sahrens return (err); 1383fa9e4066Sahrens } 1384f18faf3fSek 13857f73c863SRich Morris /* ARGSUSED */ 13867f73c863SRich Morris int 13877f73c863SRich Morris dmu_objset_prefetch(char *name, void *arg) 13887f73c863SRich Morris { 13897f73c863SRich Morris dsl_dataset_t *ds; 13907f73c863SRich Morris 13917f73c863SRich Morris if (dsl_dataset_hold(name, FTAG, &ds)) 13927f73c863SRich Morris return (0); 13937f73c863SRich Morris 13947f73c863SRich Morris if (!BP_IS_HOLE(&ds->ds_phys->ds_bp)) { 13957f73c863SRich Morris mutex_enter(&ds->ds_opening_lock); 1396*503ad85cSMatthew Ahrens if (ds->ds_objset == NULL) { 13977f73c863SRich Morris uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH; 13987f73c863SRich Morris zbookmark_t zb; 13997f73c863SRich Morris 14007f73c863SRich Morris zb.zb_objset = ds->ds_object; 14017f73c863SRich Morris zb.zb_object = 0; 14027f73c863SRich Morris zb.zb_level = -1; 14037f73c863SRich Morris zb.zb_blkid = 0; 14047f73c863SRich Morris 14057f73c863SRich Morris (void) arc_read_nolock(NULL, dsl_dataset_get_spa(ds), 14067f73c863SRich Morris &ds->ds_phys->ds_bp, NULL, NULL, 14077f73c863SRich Morris ZIO_PRIORITY_ASYNC_READ, 14087f73c863SRich Morris ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, 14097f73c863SRich Morris &aflags, &zb); 14107f73c863SRich Morris } 14117f73c863SRich Morris mutex_exit(&ds->ds_opening_lock); 14127f73c863SRich Morris } 14137f73c863SRich Morris 14147f73c863SRich Morris dsl_dataset_rele(ds, FTAG); 14157f73c863SRich Morris return (0); 14167f73c863SRich Morris } 14177f73c863SRich Morris 1418f18faf3fSek void 1419f18faf3fSek dmu_objset_set_user(objset_t *os, void *user_ptr) 1420f18faf3fSek { 1421*503ad85cSMatthew Ahrens ASSERT(MUTEX_HELD(&os->os_user_ptr_lock)); 1422*503ad85cSMatthew Ahrens os->os_user_ptr = user_ptr; 1423f18faf3fSek } 1424f18faf3fSek 1425f18faf3fSek void * 1426f18faf3fSek dmu_objset_get_user(objset_t *os) 1427f18faf3fSek { 1428*503ad85cSMatthew Ahrens ASSERT(MUTEX_HELD(&os->os_user_ptr_lock)); 1429*503ad85cSMatthew Ahrens return (os->os_user_ptr); 1430f18faf3fSek } 1431