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 { 48fa9e4066Sahrens return (os->os->os_spa); 49fa9e4066Sahrens } 50fa9e4066Sahrens 51fa9e4066Sahrens zilog_t * 52fa9e4066Sahrens dmu_objset_zil(objset_t *os) 53fa9e4066Sahrens { 54fa9e4066Sahrens return (os->os->os_zil); 55fa9e4066Sahrens } 56fa9e4066Sahrens 57fa9e4066Sahrens dsl_pool_t * 58fa9e4066Sahrens dmu_objset_pool(objset_t *os) 59fa9e4066Sahrens { 60fa9e4066Sahrens dsl_dataset_t *ds; 61fa9e4066Sahrens 62fa9e4066Sahrens if ((ds = os->os->os_dsl_dataset) != NULL && ds->ds_dir) 63fa9e4066Sahrens return (ds->ds_dir->dd_pool); 64fa9e4066Sahrens else 65fa9e4066Sahrens return (spa_get_dsl(os->os->os_spa)); 66fa9e4066Sahrens } 67fa9e4066Sahrens 68fa9e4066Sahrens dsl_dataset_t * 69fa9e4066Sahrens dmu_objset_ds(objset_t *os) 70fa9e4066Sahrens { 71fa9e4066Sahrens return (os->os->os_dsl_dataset); 72fa9e4066Sahrens } 73fa9e4066Sahrens 74fa9e4066Sahrens dmu_objset_type_t 75fa9e4066Sahrens dmu_objset_type(objset_t *os) 76fa9e4066Sahrens { 77fa9e4066Sahrens return (os->os->os_phys->os_type); 78fa9e4066Sahrens } 79fa9e4066Sahrens 80fa9e4066Sahrens void 81fa9e4066Sahrens dmu_objset_name(objset_t *os, char *buf) 82fa9e4066Sahrens { 83fa9e4066Sahrens dsl_dataset_name(os->os->os_dsl_dataset, buf); 84fa9e4066Sahrens } 85fa9e4066Sahrens 86fa9e4066Sahrens uint64_t 87fa9e4066Sahrens dmu_objset_id(objset_t *os) 88fa9e4066Sahrens { 89fa9e4066Sahrens dsl_dataset_t *ds = os->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 { 97fa9e4066Sahrens objset_impl_t *osi = arg; 98fa9e4066Sahrens 99fa9e4066Sahrens /* 100fa9e4066Sahrens * Inheritance should have been done by now. 101fa9e4066Sahrens */ 102fa9e4066Sahrens ASSERT(newval != ZIO_CHECKSUM_INHERIT); 103fa9e4066Sahrens 104fa9e4066Sahrens osi->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 { 110fa9e4066Sahrens objset_impl_t *osi = arg; 111fa9e4066Sahrens 112fa9e4066Sahrens /* 113fa9e4066Sahrens * Inheritance and range checking should have been done by now. 114fa9e4066Sahrens */ 115fa9e4066Sahrens ASSERT(newval != ZIO_COMPRESS_INHERIT); 116fa9e4066Sahrens 117fa9e4066Sahrens osi->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 { 123d0ad202dSahrens objset_impl_t *osi = arg; 124d0ad202dSahrens 125d0ad202dSahrens /* 126d0ad202dSahrens * Inheritance and range checking should have been done by now. 127d0ad202dSahrens */ 128d0ad202dSahrens ASSERT(newval > 0); 129d0ad202dSahrens ASSERT(newval <= spa_max_replication(osi->os_spa)); 130d0ad202dSahrens 131d0ad202dSahrens osi->os_copies = newval; 132d0ad202dSahrens } 133d0ad202dSahrens 1343baa08fcSek static void 1353baa08fcSek primary_cache_changed_cb(void *arg, uint64_t newval) 1363baa08fcSek { 1373baa08fcSek objset_impl_t *osi = 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 1453baa08fcSek osi->os_primary_cache = newval; 1463baa08fcSek } 1473baa08fcSek 1483baa08fcSek static void 1493baa08fcSek secondary_cache_changed_cb(void *arg, uint64_t newval) 1503baa08fcSek { 1513baa08fcSek objset_impl_t *osi = 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 1593baa08fcSek osi->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, 180ea8dc4b6Seschrock objset_impl_t **osip) 181fa9e4066Sahrens { 18291ebeef5Sahrens objset_impl_t *osi; 183088f3894Sahrens int i, err; 184fa9e4066Sahrens 18591ebeef5Sahrens ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock)); 18691ebeef5Sahrens 187fa9e4066Sahrens osi = kmem_zalloc(sizeof (objset_impl_t), KM_SLEEP); 188fa9e4066Sahrens osi->os.os = osi; 189fa9e4066Sahrens osi->os_dsl_dataset = ds; 190fa9e4066Sahrens osi->os_spa = spa; 191c717a561Smaybee osi->os_rootbp = bp; 192c717a561Smaybee if (!BP_IS_HOLE(osi->os_rootbp)) { 19313506d1eSmaybee uint32_t aflags = ARC_WAIT; 194ea8dc4b6Seschrock zbookmark_t zb; 195ea8dc4b6Seschrock zb.zb_objset = ds ? ds->ds_object : 0; 196ea8dc4b6Seschrock zb.zb_object = 0; 197ea8dc4b6Seschrock zb.zb_level = -1; 198ea8dc4b6Seschrock zb.zb_blkid = 0; 1993baa08fcSek if (DMU_OS_IS_L2CACHEABLE(osi)) 2003baa08fcSek aflags |= ARC_L2CACHE; 201ea8dc4b6Seschrock 202c717a561Smaybee dprintf_bp(osi->os_rootbp, "reading %s", ""); 203088f3894Sahrens /* 204088f3894Sahrens * NB: when bprewrite scrub can change the bp, 205088f3894Sahrens * and this is called from dmu_objset_open_ds_os, the bp 206088f3894Sahrens * could change, and we'll need a lock. 207088f3894Sahrens */ 208088f3894Sahrens err = arc_read_nolock(NULL, spa, osi->os_rootbp, 209c717a561Smaybee arc_getbuf_func, &osi->os_phys_buf, 21013506d1eSmaybee ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb); 211ea8dc4b6Seschrock if (err) { 212ea8dc4b6Seschrock kmem_free(osi, sizeof (objset_impl_t)); 213b87f3af3Sperrin /* convert checksum errors into IO errors */ 214b87f3af3Sperrin if (err == ECKSUM) 215b87f3af3Sperrin err = EIO; 216ea8dc4b6Seschrock return (err); 217ea8dc4b6Seschrock } 21814843421SMatthew Ahrens 21914843421SMatthew Ahrens /* Increase the blocksize if we are permitted. */ 22014843421SMatthew Ahrens if (spa_version(spa) >= SPA_VERSION_USERSPACE && 22114843421SMatthew Ahrens arc_buf_size(osi->os_phys_buf) < sizeof (objset_phys_t)) { 22214843421SMatthew Ahrens arc_buf_t *buf = arc_buf_alloc(spa, 22314843421SMatthew Ahrens sizeof (objset_phys_t), &osi->os_phys_buf, 22414843421SMatthew Ahrens ARC_BUFC_METADATA); 22514843421SMatthew Ahrens bzero(buf->b_data, sizeof (objset_phys_t)); 22614843421SMatthew Ahrens bcopy(osi->os_phys_buf->b_data, buf->b_data, 22714843421SMatthew Ahrens arc_buf_size(osi->os_phys_buf)); 2280aea4b19SMatthew Ahrens (void) arc_buf_remove_ref(osi->os_phys_buf, 2290aea4b19SMatthew Ahrens &osi->os_phys_buf); 23014843421SMatthew Ahrens osi->os_phys_buf = buf; 23114843421SMatthew Ahrens } 23214843421SMatthew Ahrens 233c717a561Smaybee osi->os_phys = osi->os_phys_buf->b_data; 23414843421SMatthew Ahrens osi->os_flags = osi->os_phys->os_flags; 235fa9e4066Sahrens } else { 23614843421SMatthew Ahrens int size = spa_version(spa) >= SPA_VERSION_USERSPACE ? 23714843421SMatthew Ahrens sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE; 23814843421SMatthew Ahrens osi->os_phys_buf = arc_buf_alloc(spa, size, 239c717a561Smaybee &osi->os_phys_buf, ARC_BUFC_METADATA); 240c717a561Smaybee osi->os_phys = osi->os_phys_buf->b_data; 24114843421SMatthew Ahrens bzero(osi->os_phys, size); 242fa9e4066Sahrens } 243fa9e4066Sahrens 244fa9e4066Sahrens /* 245fa9e4066Sahrens * Note: the changed_cb will be called once before the register 246fa9e4066Sahrens * func returns, thus changing the checksum/compression from the 2473baa08fcSek * default (fletcher2/off). Snapshots don't need to know about 2483baa08fcSek * checksum/compression/copies. 249fa9e4066Sahrens */ 2503baa08fcSek if (ds) { 2513baa08fcSek err = dsl_prop_register(ds, "primarycache", 2523baa08fcSek primary_cache_changed_cb, osi); 253d0ad202dSahrens if (err == 0) 2543baa08fcSek err = dsl_prop_register(ds, "secondarycache", 2553baa08fcSek secondary_cache_changed_cb, osi); 2563baa08fcSek if (!dsl_dataset_is_snapshot(ds)) { 2573baa08fcSek if (err == 0) 2583baa08fcSek err = dsl_prop_register(ds, "checksum", 2593baa08fcSek checksum_changed_cb, osi); 2603baa08fcSek if (err == 0) 2613baa08fcSek err = dsl_prop_register(ds, "compression", 2623baa08fcSek compression_changed_cb, osi); 2633baa08fcSek if (err == 0) 2643baa08fcSek err = dsl_prop_register(ds, "copies", 2653baa08fcSek copies_changed_cb, osi); 2663baa08fcSek } 267ea8dc4b6Seschrock if (err) { 268c717a561Smaybee VERIFY(arc_buf_remove_ref(osi->os_phys_buf, 269c717a561Smaybee &osi->os_phys_buf) == 1); 270ea8dc4b6Seschrock kmem_free(osi, sizeof (objset_impl_t)); 271ea8dc4b6Seschrock return (err); 272ea8dc4b6Seschrock } 27399653d4eSeschrock } else if (ds == NULL) { 274fa9e4066Sahrens /* It's the meta-objset. */ 275fa9e4066Sahrens osi->os_checksum = ZIO_CHECKSUM_FLETCHER_4; 276ea8dc4b6Seschrock osi->os_compress = ZIO_COMPRESS_LZJB; 277d0ad202dSahrens osi->os_copies = spa_max_replication(spa); 2783baa08fcSek osi->os_primary_cache = ZFS_CACHE_ALL; 2793baa08fcSek osi->os_secondary_cache = ZFS_CACHE_ALL; 280fa9e4066Sahrens } 281fa9e4066Sahrens 282088f3894Sahrens osi->os_zil_header = osi->os_phys->os_zil_header; 283088f3894Sahrens osi->os_zil = zil_alloc(&osi->os, &osi->os_zil_header); 284fa9e4066Sahrens 285fa9e4066Sahrens for (i = 0; i < TXG_SIZE; i++) { 286fa9e4066Sahrens list_create(&osi->os_dirty_dnodes[i], sizeof (dnode_t), 287fa9e4066Sahrens offsetof(dnode_t, dn_dirty_link[i])); 288fa9e4066Sahrens list_create(&osi->os_free_dnodes[i], sizeof (dnode_t), 289fa9e4066Sahrens offsetof(dnode_t, dn_dirty_link[i])); 290fa9e4066Sahrens } 291fa9e4066Sahrens list_create(&osi->os_dnodes, sizeof (dnode_t), 292fa9e4066Sahrens offsetof(dnode_t, dn_link)); 293fa9e4066Sahrens list_create(&osi->os_downgraded_dbufs, sizeof (dmu_buf_impl_t), 294fa9e4066Sahrens offsetof(dmu_buf_impl_t, db_link)); 295fa9e4066Sahrens 2965ad82045Snd mutex_init(&osi->os_lock, NULL, MUTEX_DEFAULT, NULL); 2975ad82045Snd mutex_init(&osi->os_obj_lock, NULL, MUTEX_DEFAULT, NULL); 298f18faf3fSek mutex_init(&osi->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL); 2995ad82045Snd 300fa9e4066Sahrens osi->os_meta_dnode = dnode_special_open(osi, 301fa9e4066Sahrens &osi->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT); 30214843421SMatthew Ahrens if (arc_buf_size(osi->os_phys_buf) >= sizeof (objset_phys_t)) { 30314843421SMatthew Ahrens osi->os_userused_dnode = dnode_special_open(osi, 30414843421SMatthew Ahrens &osi->os_phys->os_userused_dnode, DMU_USERUSED_OBJECT); 30514843421SMatthew Ahrens osi->os_groupused_dnode = dnode_special_open(osi, 30614843421SMatthew Ahrens &osi->os_phys->os_groupused_dnode, DMU_GROUPUSED_OBJECT); 30714843421SMatthew Ahrens } 308fa9e4066Sahrens 30991ebeef5Sahrens /* 31091ebeef5Sahrens * We should be the only thread trying to do this because we 31191ebeef5Sahrens * have ds_opening_lock 31291ebeef5Sahrens */ 31391ebeef5Sahrens if (ds) { 31491ebeef5Sahrens VERIFY(NULL == dsl_dataset_set_user_ptr(ds, osi, 31591ebeef5Sahrens dmu_objset_evict)); 316fa9e4066Sahrens } 317fa9e4066Sahrens 318ea8dc4b6Seschrock *osip = osi; 319ea8dc4b6Seschrock return (0); 320fa9e4066Sahrens } 321fa9e4066Sahrens 3223cb34c60Sahrens static int 3233cb34c60Sahrens dmu_objset_open_ds_os(dsl_dataset_t *ds, objset_t *os, dmu_objset_type_t type) 3243cb34c60Sahrens { 3253cb34c60Sahrens objset_impl_t *osi; 3263cb34c60Sahrens 3273cb34c60Sahrens mutex_enter(&ds->ds_opening_lock); 3283cb34c60Sahrens osi = dsl_dataset_get_user_ptr(ds); 3293cb34c60Sahrens if (osi == NULL) { 330745cd3c5Smaybee int err; 331745cd3c5Smaybee 3323cb34c60Sahrens err = dmu_objset_open_impl(dsl_dataset_get_spa(ds), 3333cb34c60Sahrens ds, &ds->ds_phys->ds_bp, &osi); 334745cd3c5Smaybee if (err) { 335745cd3c5Smaybee mutex_exit(&ds->ds_opening_lock); 3363cb34c60Sahrens return (err); 337745cd3c5Smaybee } 3383cb34c60Sahrens } 3393cb34c60Sahrens mutex_exit(&ds->ds_opening_lock); 3403cb34c60Sahrens 3413cb34c60Sahrens os->os = osi; 342745cd3c5Smaybee os->os_mode = DS_MODE_NOHOLD; 3433cb34c60Sahrens 3443cb34c60Sahrens if (type != DMU_OST_ANY && type != os->os->os_phys->os_type) 3453cb34c60Sahrens return (EINVAL); 3463cb34c60Sahrens return (0); 3473cb34c60Sahrens } 3483cb34c60Sahrens 3493cb34c60Sahrens int 3503cb34c60Sahrens dmu_objset_open_ds(dsl_dataset_t *ds, dmu_objset_type_t type, objset_t **osp) 3513cb34c60Sahrens { 3523cb34c60Sahrens objset_t *os; 3533cb34c60Sahrens int err; 3543cb34c60Sahrens 3553cb34c60Sahrens os = kmem_alloc(sizeof (objset_t), KM_SLEEP); 3563cb34c60Sahrens err = dmu_objset_open_ds_os(ds, os, type); 3573cb34c60Sahrens if (err) 3583cb34c60Sahrens kmem_free(os, sizeof (objset_t)); 3593cb34c60Sahrens else 3603cb34c60Sahrens *osp = os; 3613cb34c60Sahrens return (err); 3623cb34c60Sahrens } 3633cb34c60Sahrens 364fa9e4066Sahrens /* called from zpl */ 365fa9e4066Sahrens int 366fa9e4066Sahrens dmu_objset_open(const char *name, dmu_objset_type_t type, int mode, 367fa9e4066Sahrens objset_t **osp) 368fa9e4066Sahrens { 369fa9e4066Sahrens objset_t *os; 370f18faf3fSek dsl_dataset_t *ds; 371f18faf3fSek int err; 372fa9e4066Sahrens 373745cd3c5Smaybee ASSERT(DS_MODE_TYPE(mode) == DS_MODE_USER || 374745cd3c5Smaybee DS_MODE_TYPE(mode) == DS_MODE_OWNER); 3753cb34c60Sahrens 376fa9e4066Sahrens os = kmem_alloc(sizeof (objset_t), KM_SLEEP); 377745cd3c5Smaybee if (DS_MODE_TYPE(mode) == DS_MODE_USER) 378745cd3c5Smaybee err = dsl_dataset_hold(name, os, &ds); 379745cd3c5Smaybee else 380745cd3c5Smaybee err = dsl_dataset_own(name, mode, os, &ds); 381fa9e4066Sahrens if (err) { 382fa9e4066Sahrens kmem_free(os, sizeof (objset_t)); 383fa9e4066Sahrens return (err); 384fa9e4066Sahrens } 385fa9e4066Sahrens 3863cb34c60Sahrens err = dmu_objset_open_ds_os(ds, os, type); 3873cb34c60Sahrens if (err) { 388745cd3c5Smaybee if (DS_MODE_TYPE(mode) == DS_MODE_USER) 389745cd3c5Smaybee dsl_dataset_rele(ds, os); 390745cd3c5Smaybee else 391745cd3c5Smaybee dsl_dataset_disown(ds, os); 3923cb34c60Sahrens kmem_free(os, sizeof (objset_t)); 3933cb34c60Sahrens } else { 394745cd3c5Smaybee os->os_mode = mode; 3953cb34c60Sahrens *osp = os; 396fa9e4066Sahrens } 3973cb34c60Sahrens return (err); 398fa9e4066Sahrens } 399fa9e4066Sahrens 400fa9e4066Sahrens void 401fa9e4066Sahrens dmu_objset_close(objset_t *os) 402fa9e4066Sahrens { 403745cd3c5Smaybee ASSERT(DS_MODE_TYPE(os->os_mode) == DS_MODE_USER || 404745cd3c5Smaybee DS_MODE_TYPE(os->os_mode) == DS_MODE_OWNER || 405745cd3c5Smaybee DS_MODE_TYPE(os->os_mode) == DS_MODE_NOHOLD); 406745cd3c5Smaybee 407745cd3c5Smaybee if (DS_MODE_TYPE(os->os_mode) == DS_MODE_USER) 408745cd3c5Smaybee dsl_dataset_rele(os->os->os_dsl_dataset, os); 409745cd3c5Smaybee else if (DS_MODE_TYPE(os->os_mode) == DS_MODE_OWNER) 410745cd3c5Smaybee dsl_dataset_disown(os->os->os_dsl_dataset, os); 411fa9e4066Sahrens kmem_free(os, sizeof (objset_t)); 412fa9e4066Sahrens } 413fa9e4066Sahrens 414436b2950Sperrin int 4151934e92fSmaybee dmu_objset_evict_dbufs(objset_t *os) 416ea8dc4b6Seschrock { 417ea8dc4b6Seschrock objset_impl_t *osi = os->os; 418ea8dc4b6Seschrock dnode_t *dn; 419c543ec06Sahrens 420c543ec06Sahrens mutex_enter(&osi->os_lock); 421c543ec06Sahrens 422c543ec06Sahrens /* process the mdn last, since the other dnodes have holds on it */ 423c543ec06Sahrens list_remove(&osi->os_dnodes, osi->os_meta_dnode); 424c543ec06Sahrens list_insert_tail(&osi->os_dnodes, osi->os_meta_dnode); 425ea8dc4b6Seschrock 426ea8dc4b6Seschrock /* 427c543ec06Sahrens * Find the first dnode with holds. We have to do this dance 428c543ec06Sahrens * because dnode_add_ref() only works if you already have a 429c543ec06Sahrens * hold. If there are no holds then it has no dbufs so OK to 430c543ec06Sahrens * skip. 431ea8dc4b6Seschrock */ 432c543ec06Sahrens for (dn = list_head(&osi->os_dnodes); 4331934e92fSmaybee dn && !dnode_add_ref(dn, FTAG); 434c543ec06Sahrens dn = list_next(&osi->os_dnodes, dn)) 435c543ec06Sahrens continue; 436c543ec06Sahrens 437c543ec06Sahrens while (dn) { 438c543ec06Sahrens dnode_t *next_dn = dn; 439c543ec06Sahrens 440c543ec06Sahrens do { 441c543ec06Sahrens next_dn = list_next(&osi->os_dnodes, next_dn); 4421934e92fSmaybee } while (next_dn && !dnode_add_ref(next_dn, FTAG)); 443c543ec06Sahrens 444c543ec06Sahrens mutex_exit(&osi->os_lock); 4451934e92fSmaybee dnode_evict_dbufs(dn); 446c543ec06Sahrens dnode_rele(dn, FTAG); 447c543ec06Sahrens mutex_enter(&osi->os_lock); 448c543ec06Sahrens dn = next_dn; 449ea8dc4b6Seschrock } 450ea8dc4b6Seschrock mutex_exit(&osi->os_lock); 4511934e92fSmaybee return (list_head(&osi->os_dnodes) != osi->os_meta_dnode); 452ea8dc4b6Seschrock } 453ea8dc4b6Seschrock 454fa9e4066Sahrens void 455fa9e4066Sahrens dmu_objset_evict(dsl_dataset_t *ds, void *arg) 456fa9e4066Sahrens { 457fa9e4066Sahrens objset_impl_t *osi = arg; 458ea8dc4b6Seschrock objset_t os; 45999653d4eSeschrock int i; 460fa9e4066Sahrens 461fa9e4066Sahrens for (i = 0; i < TXG_SIZE; i++) { 462fa9e4066Sahrens ASSERT(list_head(&osi->os_dirty_dnodes[i]) == NULL); 463fa9e4066Sahrens ASSERT(list_head(&osi->os_free_dnodes[i]) == NULL); 464fa9e4066Sahrens } 465fa9e4066Sahrens 4663baa08fcSek if (ds) { 4673baa08fcSek if (!dsl_dataset_is_snapshot(ds)) { 4683baa08fcSek VERIFY(0 == dsl_prop_unregister(ds, "checksum", 4693baa08fcSek checksum_changed_cb, osi)); 4703baa08fcSek VERIFY(0 == dsl_prop_unregister(ds, "compression", 4713baa08fcSek compression_changed_cb, osi)); 4723baa08fcSek VERIFY(0 == dsl_prop_unregister(ds, "copies", 4733baa08fcSek copies_changed_cb, osi)); 4743baa08fcSek } 4753baa08fcSek VERIFY(0 == dsl_prop_unregister(ds, "primarycache", 4763baa08fcSek primary_cache_changed_cb, osi)); 4773baa08fcSek VERIFY(0 == dsl_prop_unregister(ds, "secondarycache", 4783baa08fcSek secondary_cache_changed_cb, osi)); 479fa9e4066Sahrens } 480fa9e4066Sahrens 481ea8dc4b6Seschrock /* 482ea8dc4b6Seschrock * We should need only a single pass over the dnode list, since 483ea8dc4b6Seschrock * nothing can be added to the list at this point. 484ea8dc4b6Seschrock */ 485ea8dc4b6Seschrock os.os = osi; 4861934e92fSmaybee (void) dmu_objset_evict_dbufs(&os); 487ea8dc4b6Seschrock 488fa9e4066Sahrens dnode_special_close(osi->os_meta_dnode); 48914843421SMatthew Ahrens if (osi->os_userused_dnode) { 49014843421SMatthew Ahrens dnode_special_close(osi->os_userused_dnode); 49114843421SMatthew Ahrens dnode_special_close(osi->os_groupused_dnode); 49214843421SMatthew Ahrens } 493fa9e4066Sahrens zil_free(osi->os_zil); 494fa9e4066Sahrens 49514843421SMatthew Ahrens ASSERT3P(list_head(&osi->os_dnodes), ==, NULL); 49614843421SMatthew Ahrens 497c717a561Smaybee VERIFY(arc_buf_remove_ref(osi->os_phys_buf, &osi->os_phys_buf) == 1); 4985ad82045Snd mutex_destroy(&osi->os_lock); 4995ad82045Snd mutex_destroy(&osi->os_obj_lock); 500f18faf3fSek mutex_destroy(&osi->os_user_ptr_lock); 501fa9e4066Sahrens kmem_free(osi, sizeof (objset_impl_t)); 502fa9e4066Sahrens } 503fa9e4066Sahrens 504fa9e4066Sahrens /* called from dsl for meta-objset */ 505fa9e4066Sahrens objset_impl_t * 506c717a561Smaybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, 507c717a561Smaybee dmu_objset_type_t type, dmu_tx_t *tx) 508fa9e4066Sahrens { 509fa9e4066Sahrens objset_impl_t *osi; 510fa9e4066Sahrens dnode_t *mdn; 511fa9e4066Sahrens 512fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 51391ebeef5Sahrens if (ds) 51491ebeef5Sahrens mutex_enter(&ds->ds_opening_lock); 515c717a561Smaybee VERIFY(0 == dmu_objset_open_impl(spa, ds, bp, &osi)); 51691ebeef5Sahrens if (ds) 51791ebeef5Sahrens mutex_exit(&ds->ds_opening_lock); 518fa9e4066Sahrens mdn = osi->os_meta_dnode; 519fa9e4066Sahrens 520fa9e4066Sahrens dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT, 521fa9e4066Sahrens DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx); 522fa9e4066Sahrens 523fa9e4066Sahrens /* 524fa9e4066Sahrens * We don't want to have to increase the meta-dnode's nlevels 525fa9e4066Sahrens * later, because then we could do it in quescing context while 526fa9e4066Sahrens * we are also accessing it in open context. 527fa9e4066Sahrens * 528fa9e4066Sahrens * This precaution is not necessary for the MOS (ds == NULL), 529fa9e4066Sahrens * because the MOS is only updated in syncing context. 530fa9e4066Sahrens * This is most fortunate: the MOS is the only objset that 531fa9e4066Sahrens * needs to be synced multiple times as spa_sync() iterates 532fa9e4066Sahrens * to convergence, so minimizing its dn_nlevels matters. 533fa9e4066Sahrens */ 534ea8dc4b6Seschrock if (ds != NULL) { 535ea8dc4b6Seschrock int levels = 1; 536ea8dc4b6Seschrock 537ea8dc4b6Seschrock /* 538ea8dc4b6Seschrock * Determine the number of levels necessary for the meta-dnode 539ea8dc4b6Seschrock * to contain DN_MAX_OBJECT dnodes. 540ea8dc4b6Seschrock */ 541ea8dc4b6Seschrock while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift + 542ea8dc4b6Seschrock (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) < 543ea8dc4b6Seschrock DN_MAX_OBJECT * sizeof (dnode_phys_t)) 544ea8dc4b6Seschrock levels++; 545ea8dc4b6Seschrock 546fa9e4066Sahrens mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] = 547ea8dc4b6Seschrock mdn->dn_nlevels = levels; 548ea8dc4b6Seschrock } 549fa9e4066Sahrens 550fa9e4066Sahrens ASSERT(type != DMU_OST_NONE); 551fa9e4066Sahrens ASSERT(type != DMU_OST_ANY); 552fa9e4066Sahrens ASSERT(type < DMU_OST_NUMTYPES); 553fa9e4066Sahrens osi->os_phys->os_type = type; 55414843421SMatthew Ahrens if (dmu_objset_userused_enabled(osi)) { 55514843421SMatthew Ahrens osi->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE; 55614843421SMatthew Ahrens osi->os_flags = osi->os_phys->os_flags; 55714843421SMatthew Ahrens } 558fa9e4066Sahrens 559fa9e4066Sahrens dsl_dataset_dirty(ds, tx); 560fa9e4066Sahrens 561fa9e4066Sahrens return (osi); 562fa9e4066Sahrens } 563fa9e4066Sahrens 564fa9e4066Sahrens struct oscarg { 565ecd6cf80Smarks void (*userfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx); 566fa9e4066Sahrens void *userarg; 567*ae46e4c7SMatthew Ahrens dsl_dataset_t *clone_origin; 568fa9e4066Sahrens const char *lastname; 569fa9e4066Sahrens dmu_objset_type_t type; 570ab04eb8eStimh uint64_t flags; 571fa9e4066Sahrens }; 572fa9e4066Sahrens 573ecd6cf80Smarks /*ARGSUSED*/ 574fa9e4066Sahrens static int 5751d452cf5Sahrens dmu_objset_create_check(void *arg1, void *arg2, dmu_tx_t *tx) 576fa9e4066Sahrens { 5771d452cf5Sahrens dsl_dir_t *dd = arg1; 5781d452cf5Sahrens struct oscarg *oa = arg2; 5791d452cf5Sahrens objset_t *mos = dd->dd_pool->dp_meta_objset; 580fa9e4066Sahrens int err; 5811d452cf5Sahrens uint64_t ddobj; 5821d452cf5Sahrens 5831d452cf5Sahrens err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj, 5841d452cf5Sahrens oa->lastname, sizeof (uint64_t), 1, &ddobj); 5851d452cf5Sahrens if (err != ENOENT) 5861d452cf5Sahrens return (err ? err : EEXIST); 5871d452cf5Sahrens 588*ae46e4c7SMatthew Ahrens if (oa->clone_origin != NULL) { 589*ae46e4c7SMatthew Ahrens /* You can't clone across pools. */ 590*ae46e4c7SMatthew Ahrens if (oa->clone_origin->ds_dir->dd_pool != dd->dd_pool) 5911d452cf5Sahrens return (EXDEV); 5921d452cf5Sahrens 593*ae46e4c7SMatthew Ahrens /* You can only clone snapshots, not the head datasets. */ 594*ae46e4c7SMatthew Ahrens if (!dsl_dataset_is_snapshot(oa->clone_origin)) 5951d452cf5Sahrens return (EINVAL); 5961d452cf5Sahrens } 597ecd6cf80Smarks 5981d452cf5Sahrens return (0); 5991d452cf5Sahrens } 6001d452cf5Sahrens 6011d452cf5Sahrens static void 602ecd6cf80Smarks dmu_objset_create_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 6031d452cf5Sahrens { 6041d452cf5Sahrens dsl_dir_t *dd = arg1; 6051d452cf5Sahrens struct oscarg *oa = arg2; 6061d452cf5Sahrens uint64_t dsobj; 607fa9e4066Sahrens 608fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 609fa9e4066Sahrens 6101d452cf5Sahrens dsobj = dsl_dataset_create_sync(dd, oa->lastname, 611*ae46e4c7SMatthew Ahrens oa->clone_origin, oa->flags, cr, tx); 612fa9e4066Sahrens 613*ae46e4c7SMatthew Ahrens if (oa->clone_origin == NULL) { 614*ae46e4c7SMatthew Ahrens dsl_dataset_t *ds; 615*ae46e4c7SMatthew Ahrens blkptr_t *bp; 616fa9e4066Sahrens objset_impl_t *osi; 617fa9e4066Sahrens 618*ae46e4c7SMatthew Ahrens VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, dsobj, 619*ae46e4c7SMatthew Ahrens FTAG, &ds)); 620*ae46e4c7SMatthew Ahrens bp = dsl_dataset_get_blkptr(ds); 621*ae46e4c7SMatthew Ahrens ASSERT(BP_IS_HOLE(bp)); 622*ae46e4c7SMatthew Ahrens 623fa9e4066Sahrens osi = dmu_objset_create_impl(dsl_dataset_get_spa(ds), 624c717a561Smaybee ds, bp, oa->type, tx); 625fa9e4066Sahrens 626fa9e4066Sahrens if (oa->userfunc) 627ecd6cf80Smarks oa->userfunc(&osi->os, oa->userarg, cr, tx); 628*ae46e4c7SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 629fa9e4066Sahrens } 630ecd6cf80Smarks 631ecd6cf80Smarks spa_history_internal_log(LOG_DS_CREATE, dd->dd_pool->dp_spa, 632ecd6cf80Smarks tx, cr, "dataset = %llu", dsobj); 633fa9e4066Sahrens } 634fa9e4066Sahrens 635fa9e4066Sahrens int 636*ae46e4c7SMatthew Ahrens dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags, 637ecd6cf80Smarks void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg) 638fa9e4066Sahrens { 6391d452cf5Sahrens dsl_dir_t *pdd; 640fa9e4066Sahrens const char *tail; 641fa9e4066Sahrens int err = 0; 6421d452cf5Sahrens struct oscarg oa = { 0 }; 643fa9e4066Sahrens 6441d452cf5Sahrens ASSERT(strchr(name, '@') == NULL); 6451d452cf5Sahrens err = dsl_dir_open(name, FTAG, &pdd, &tail); 646ea8dc4b6Seschrock if (err) 647ea8dc4b6Seschrock return (err); 648fa9e4066Sahrens if (tail == NULL) { 6491d452cf5Sahrens dsl_dir_close(pdd, FTAG); 650fa9e4066Sahrens return (EEXIST); 651fa9e4066Sahrens } 652fa9e4066Sahrens 6531d452cf5Sahrens oa.userfunc = func; 6541d452cf5Sahrens oa.userarg = arg; 6551d452cf5Sahrens oa.lastname = tail; 6561d452cf5Sahrens oa.type = type; 657ab04eb8eStimh oa.flags = flags; 658ecd6cf80Smarks 659*ae46e4c7SMatthew Ahrens err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check, 660*ae46e4c7SMatthew Ahrens dmu_objset_create_sync, pdd, &oa, 5); 661*ae46e4c7SMatthew Ahrens dsl_dir_close(pdd, FTAG); 662*ae46e4c7SMatthew Ahrens return (err); 663*ae46e4c7SMatthew Ahrens } 664*ae46e4c7SMatthew Ahrens 665*ae46e4c7SMatthew Ahrens int 666*ae46e4c7SMatthew Ahrens dmu_objset_clone(const char *name, dsl_dataset_t *clone_origin, uint64_t flags) 667*ae46e4c7SMatthew Ahrens { 668*ae46e4c7SMatthew Ahrens dsl_dir_t *pdd; 669*ae46e4c7SMatthew Ahrens const char *tail; 670*ae46e4c7SMatthew Ahrens int err = 0; 671*ae46e4c7SMatthew Ahrens struct oscarg oa = { 0 }; 672*ae46e4c7SMatthew Ahrens 673*ae46e4c7SMatthew Ahrens ASSERT(strchr(name, '@') == NULL); 674*ae46e4c7SMatthew Ahrens err = dsl_dir_open(name, FTAG, &pdd, &tail); 675*ae46e4c7SMatthew Ahrens if (err) 676*ae46e4c7SMatthew Ahrens return (err); 677*ae46e4c7SMatthew Ahrens if (tail == NULL) { 678*ae46e4c7SMatthew Ahrens dsl_dir_close(pdd, FTAG); 679*ae46e4c7SMatthew Ahrens return (EEXIST); 680fa9e4066Sahrens } 681*ae46e4c7SMatthew Ahrens 682*ae46e4c7SMatthew Ahrens oa.lastname = tail; 683*ae46e4c7SMatthew Ahrens oa.clone_origin = clone_origin; 684*ae46e4c7SMatthew Ahrens oa.flags = flags; 685*ae46e4c7SMatthew Ahrens 6861d452cf5Sahrens err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check, 6871d452cf5Sahrens dmu_objset_create_sync, pdd, &oa, 5); 6881d452cf5Sahrens dsl_dir_close(pdd, FTAG); 689fa9e4066Sahrens return (err); 690fa9e4066Sahrens } 691fa9e4066Sahrens 692fa9e4066Sahrens int 693842727c2SChris Kirby dmu_objset_destroy(const char *name, boolean_t defer) 694fa9e4066Sahrens { 695fa9e4066Sahrens objset_t *os; 696fa9e4066Sahrens int error; 697fa9e4066Sahrens 698fa9e4066Sahrens /* 699*ae46e4c7SMatthew Ahrens * dsl_dataset_destroy() can free any claimed-but-unplayed 700*ae46e4c7SMatthew Ahrens * intent log, but if there is an active log, it has blocks that 701*ae46e4c7SMatthew Ahrens * are allocated, but may not yet be reflected in the on-disk 702*ae46e4c7SMatthew Ahrens * structure. Only the ZIL knows how to free them, so we have 703*ae46e4c7SMatthew Ahrens * to call into it here. 704fa9e4066Sahrens */ 7053cb34c60Sahrens error = dmu_objset_open(name, DMU_OST_ANY, 706745cd3c5Smaybee DS_MODE_OWNER|DS_MODE_READONLY|DS_MODE_INCONSISTENT, &os); 707fa9e4066Sahrens if (error == 0) { 7083cb34c60Sahrens dsl_dataset_t *ds = os->os->os_dsl_dataset; 709d80c45e0Sbonwick zil_destroy(dmu_objset_zil(os), B_FALSE); 7103cb34c60Sahrens 711842727c2SChris Kirby error = dsl_dataset_destroy(ds, os, defer); 712*ae46e4c7SMatthew Ahrens /* dsl_dataset_destroy() closes the ds. */ 7133cb34c60Sahrens kmem_free(os, sizeof (objset_t)); 714fa9e4066Sahrens } 715fa9e4066Sahrens 7163cb34c60Sahrens return (error); 717fa9e4066Sahrens } 718fa9e4066Sahrens 7191d452cf5Sahrens struct snaparg { 7201d452cf5Sahrens dsl_sync_task_group_t *dstg; 7211d452cf5Sahrens char *snapname; 7221d452cf5Sahrens char failed[MAXPATHLEN]; 723ecd6cf80Smarks boolean_t checkperms; 724ea2f5b9eSMatthew Ahrens nvlist_t *props; 7253cb34c60Sahrens }; 7263cb34c60Sahrens 727ea2f5b9eSMatthew Ahrens static int 728ea2f5b9eSMatthew Ahrens snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx) 729ea2f5b9eSMatthew Ahrens { 730ea2f5b9eSMatthew Ahrens objset_t *os = arg1; 731ea2f5b9eSMatthew Ahrens struct snaparg *sn = arg2; 732ea2f5b9eSMatthew Ahrens 733ea2f5b9eSMatthew Ahrens /* The props have already been checked by zfs_check_userprops(). */ 734ea2f5b9eSMatthew Ahrens 735ea2f5b9eSMatthew Ahrens return (dsl_dataset_snapshot_check(os->os->os_dsl_dataset, 736ea2f5b9eSMatthew Ahrens sn->snapname, tx)); 737ea2f5b9eSMatthew Ahrens } 738ea2f5b9eSMatthew Ahrens 739ea2f5b9eSMatthew Ahrens static void 740ea2f5b9eSMatthew Ahrens snapshot_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 741ea2f5b9eSMatthew Ahrens { 742ea2f5b9eSMatthew Ahrens objset_t *os = arg1; 743ea2f5b9eSMatthew Ahrens dsl_dataset_t *ds = os->os->os_dsl_dataset; 744ea2f5b9eSMatthew Ahrens struct snaparg *sn = arg2; 745ea2f5b9eSMatthew Ahrens 746ea2f5b9eSMatthew Ahrens dsl_dataset_snapshot_sync(ds, sn->snapname, cr, tx); 747ea2f5b9eSMatthew Ahrens 748ea2f5b9eSMatthew Ahrens if (sn->props) 749ea2f5b9eSMatthew Ahrens dsl_props_set_sync(ds->ds_prev, sn->props, cr, tx); 750ea2f5b9eSMatthew Ahrens } 7511d452cf5Sahrens 7521d452cf5Sahrens static int 7531d452cf5Sahrens dmu_objset_snapshot_one(char *name, void *arg) 7541d452cf5Sahrens { 7551d452cf5Sahrens struct snaparg *sn = arg; 7561d452cf5Sahrens objset_t *os; 7571d452cf5Sahrens int err; 7581d452cf5Sahrens 7591d452cf5Sahrens (void) strcpy(sn->failed, name); 7601d452cf5Sahrens 761ecd6cf80Smarks /* 762ecd6cf80Smarks * Check permissions only when requested. This only applies when 763ecd6cf80Smarks * doing a recursive snapshot. The permission checks for the starting 764ecd6cf80Smarks * dataset have already been performed in zfs_secpolicy_snapshot() 765ecd6cf80Smarks */ 766ecd6cf80Smarks if (sn->checkperms == B_TRUE && 767ecd6cf80Smarks (err = zfs_secpolicy_snapshot_perms(name, CRED()))) 768ecd6cf80Smarks return (err); 769ecd6cf80Smarks 770745cd3c5Smaybee err = dmu_objset_open(name, DMU_OST_ANY, DS_MODE_USER, &os); 7711d452cf5Sahrens if (err != 0) 7721d452cf5Sahrens return (err); 7731d452cf5Sahrens 774745cd3c5Smaybee /* If the objset is in an inconsistent state, return busy */ 775745cd3c5Smaybee if (os->os->os_dsl_dataset->ds_phys->ds_flags & DS_FLAG_INCONSISTENT) { 776f2e10be3Srm dmu_objset_close(os); 777f2e10be3Srm return (EBUSY); 778f2e10be3Srm } 779f2e10be3Srm 7801d452cf5Sahrens /* 7811d452cf5Sahrens * NB: we need to wait for all in-flight changes to get to disk, 7821d452cf5Sahrens * so that we snapshot those changes. zil_suspend does this as 7831d452cf5Sahrens * a side effect. 7841d452cf5Sahrens */ 7851d452cf5Sahrens err = zil_suspend(dmu_objset_zil(os)); 7861d452cf5Sahrens if (err == 0) { 787ea2f5b9eSMatthew Ahrens dsl_sync_task_create(sn->dstg, snapshot_check, 788ea2f5b9eSMatthew Ahrens snapshot_sync, os, sn, 3); 789f2e10be3Srm } else { 790f2e10be3Srm dmu_objset_close(os); 7911d452cf5Sahrens } 792f2e10be3Srm 7931d452cf5Sahrens return (err); 7941d452cf5Sahrens } 7951d452cf5Sahrens 7961d452cf5Sahrens int 797ea2f5b9eSMatthew Ahrens dmu_objset_snapshot(char *fsname, char *snapname, 798ea2f5b9eSMatthew Ahrens nvlist_t *props, boolean_t recursive) 7991d452cf5Sahrens { 8001d452cf5Sahrens dsl_sync_task_t *dst; 801ea2f5b9eSMatthew Ahrens struct snaparg sn; 8021d452cf5Sahrens spa_t *spa; 8031d452cf5Sahrens int err; 8041d452cf5Sahrens 8051d452cf5Sahrens (void) strcpy(sn.failed, fsname); 8061d452cf5Sahrens 80740feaa91Sahrens err = spa_open(fsname, &spa, FTAG); 8081d452cf5Sahrens if (err) 8091d452cf5Sahrens return (err); 8101d452cf5Sahrens 8111d452cf5Sahrens sn.dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 8121d452cf5Sahrens sn.snapname = snapname; 813ea2f5b9eSMatthew Ahrens sn.props = props; 8141d452cf5Sahrens 8150b69c2f0Sahrens if (recursive) { 816ecd6cf80Smarks sn.checkperms = B_TRUE; 8170b69c2f0Sahrens err = dmu_objset_find(fsname, 8180b69c2f0Sahrens dmu_objset_snapshot_one, &sn, DS_FIND_CHILDREN); 8190b69c2f0Sahrens } else { 820ecd6cf80Smarks sn.checkperms = B_FALSE; 8211d452cf5Sahrens err = dmu_objset_snapshot_one(fsname, &sn); 8220b69c2f0Sahrens } 8231d452cf5Sahrens 824ea2f5b9eSMatthew Ahrens if (err == 0) 825ea2f5b9eSMatthew Ahrens err = dsl_sync_task_group_wait(sn.dstg); 8261d452cf5Sahrens 8271d452cf5Sahrens for (dst = list_head(&sn.dstg->dstg_tasks); dst; 8281d452cf5Sahrens dst = list_next(&sn.dstg->dstg_tasks, dst)) { 829ea2f5b9eSMatthew Ahrens objset_t *os = dst->dst_arg1; 830ea2f5b9eSMatthew Ahrens dsl_dataset_t *ds = os->os->os_dsl_dataset; 8311d452cf5Sahrens if (dst->dst_err) 8323cb34c60Sahrens dsl_dataset_name(ds, sn.failed); 833ea2f5b9eSMatthew Ahrens zil_resume(dmu_objset_zil(os)); 834ea2f5b9eSMatthew Ahrens dmu_objset_close(os); 8353cb34c60Sahrens } 8363cb34c60Sahrens 8371d452cf5Sahrens if (err) 8381d452cf5Sahrens (void) strcpy(fsname, sn.failed); 8391d452cf5Sahrens dsl_sync_task_group_destroy(sn.dstg); 8401d452cf5Sahrens spa_close(spa, FTAG); 8411d452cf5Sahrens return (err); 8421d452cf5Sahrens } 8431d452cf5Sahrens 844fa9e4066Sahrens static void 84514843421SMatthew Ahrens dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx) 846fa9e4066Sahrens { 847c717a561Smaybee dnode_t *dn; 848faafa6e3Sahrens 849c717a561Smaybee while (dn = list_head(list)) { 850c717a561Smaybee ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT); 851c717a561Smaybee ASSERT(dn->dn_dbuf->db_data_pending); 852c717a561Smaybee /* 85314843421SMatthew Ahrens * Initialize dn_zio outside dnode_sync() because the 85414843421SMatthew Ahrens * meta-dnode needs to set it ouside dnode_sync(). 855c717a561Smaybee */ 856c717a561Smaybee dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio; 857c717a561Smaybee ASSERT(dn->dn_zio); 858faafa6e3Sahrens 859c717a561Smaybee ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS); 860c717a561Smaybee list_remove(list, dn); 86114843421SMatthew Ahrens 86214843421SMatthew Ahrens if (newlist) { 86314843421SMatthew Ahrens (void) dnode_add_ref(dn, newlist); 86414843421SMatthew Ahrens list_insert_tail(newlist, dn); 86514843421SMatthew Ahrens } 86614843421SMatthew Ahrens 867c717a561Smaybee dnode_sync(dn, tx); 868fa9e4066Sahrens } 869fa9e4066Sahrens } 870fa9e4066Sahrens 871fa9e4066Sahrens /* ARGSUSED */ 872fa9e4066Sahrens static void 873c717a561Smaybee ready(zio_t *zio, arc_buf_t *abuf, void *arg) 874fa9e4066Sahrens { 875e14bb325SJeff Bonwick blkptr_t *bp = zio->io_bp; 876e14bb325SJeff Bonwick blkptr_t *bp_orig = &zio->io_bp_orig; 877fa9e4066Sahrens objset_impl_t *os = arg; 878c717a561Smaybee dnode_phys_t *dnp = &os->os_phys->os_meta_dnode; 879fa9e4066Sahrens 880e14bb325SJeff Bonwick ASSERT(bp == os->os_rootbp); 881e14bb325SJeff Bonwick ASSERT(BP_GET_TYPE(bp) == DMU_OT_OBJSET); 882e14bb325SJeff Bonwick ASSERT(BP_GET_LEVEL(bp) == 0); 8830a4e9518Sgw 884fa9e4066Sahrens /* 88514843421SMatthew Ahrens * Update rootbp fill count: it should be the number of objects 88614843421SMatthew Ahrens * allocated in the object set (not counting the "special" 88714843421SMatthew Ahrens * objects that are stored in the objset_phys_t -- the meta 88814843421SMatthew Ahrens * dnode and user/group accounting objects). 889fa9e4066Sahrens */ 89014843421SMatthew Ahrens bp->blk_fill = 0; 891e14bb325SJeff Bonwick for (int i = 0; i < dnp->dn_nblkptr; i++) 892c717a561Smaybee bp->blk_fill += dnp->dn_blkptr[i].blk_fill; 8930a4e9518Sgw 894e14bb325SJeff Bonwick if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { 895e14bb325SJeff Bonwick ASSERT(DVA_EQUAL(BP_IDENTITY(bp), BP_IDENTITY(bp_orig))); 896e14bb325SJeff Bonwick } else { 8970a4e9518Sgw if (zio->io_bp_orig.blk_birth == os->os_synctx->tx_txg) 898cdb0ab79Smaybee (void) dsl_dataset_block_kill(os->os_dsl_dataset, 899e14bb325SJeff Bonwick &zio->io_bp_orig, zio, os->os_synctx); 9000a4e9518Sgw dsl_dataset_block_born(os->os_dsl_dataset, bp, os->os_synctx); 9010a4e9518Sgw } 902c717a561Smaybee } 903c717a561Smaybee 904fa9e4066Sahrens /* called from dsl */ 905fa9e4066Sahrens void 906c717a561Smaybee dmu_objset_sync(objset_impl_t *os, zio_t *pio, dmu_tx_t *tx) 907fa9e4066Sahrens { 908fa9e4066Sahrens int txgoff; 909ea8dc4b6Seschrock zbookmark_t zb; 910088f3894Sahrens writeprops_t wp = { 0 }; 911c717a561Smaybee zio_t *zio; 912c717a561Smaybee list_t *list; 91314843421SMatthew Ahrens list_t *newlist = NULL; 914c717a561Smaybee dbuf_dirty_record_t *dr; 915c717a561Smaybee 916c717a561Smaybee dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg); 917fa9e4066Sahrens 918fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 919fa9e4066Sahrens /* XXX the write_done callback should really give us the tx... */ 920fa9e4066Sahrens os->os_synctx = tx; 921fa9e4066Sahrens 92287bd5c1eSahrens if (os->os_dsl_dataset == NULL) { 92387bd5c1eSahrens /* 92487bd5c1eSahrens * This is the MOS. If we have upgraded, 92587bd5c1eSahrens * spa_max_replication() could change, so reset 92687bd5c1eSahrens * os_copies here. 92787bd5c1eSahrens */ 92887bd5c1eSahrens os->os_copies = spa_max_replication(os->os_spa); 92987bd5c1eSahrens } 93087bd5c1eSahrens 931fa9e4066Sahrens /* 932c717a561Smaybee * Create the root block IO 933fa9e4066Sahrens */ 934ea8dc4b6Seschrock zb.zb_objset = os->os_dsl_dataset ? os->os_dsl_dataset->ds_object : 0; 935ea8dc4b6Seschrock zb.zb_object = 0; 936e14bb325SJeff Bonwick zb.zb_level = -1; /* for block ordering; it's level 0 on disk */ 937ea8dc4b6Seschrock zb.zb_blkid = 0; 938e14bb325SJeff Bonwick 939088f3894Sahrens wp.wp_type = DMU_OT_OBJSET; 940e14bb325SJeff Bonwick wp.wp_level = 0; /* on-disk BP level; see above */ 941088f3894Sahrens wp.wp_copies = os->os_copies; 942088f3894Sahrens wp.wp_oschecksum = os->os_checksum; 943088f3894Sahrens wp.wp_oscompress = os->os_compress; 944e14bb325SJeff Bonwick 945e14bb325SJeff Bonwick if (BP_IS_OLDER(os->os_rootbp, tx->tx_txg)) { 946e14bb325SJeff Bonwick (void) dsl_dataset_block_kill(os->os_dsl_dataset, 947e14bb325SJeff Bonwick os->os_rootbp, pio, tx); 948e14bb325SJeff Bonwick } 949e14bb325SJeff Bonwick 950088f3894Sahrens arc_release(os->os_phys_buf, &os->os_phys_buf); 95114843421SMatthew Ahrens 952e14bb325SJeff Bonwick zio = arc_write(pio, os->os_spa, &wp, DMU_OS_IS_L2CACHEABLE(os), 953e14bb325SJeff Bonwick tx->tx_txg, os->os_rootbp, os->os_phys_buf, ready, NULL, os, 954e14bb325SJeff Bonwick ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb); 955c717a561Smaybee 956c717a561Smaybee /* 95714843421SMatthew Ahrens * Sync special dnodes - the parent IO for the sync is the root block 958c717a561Smaybee */ 959c717a561Smaybee os->os_meta_dnode->dn_zio = zio; 960c717a561Smaybee dnode_sync(os->os_meta_dnode, tx); 961c717a561Smaybee 96214843421SMatthew Ahrens os->os_phys->os_flags = os->os_flags; 96314843421SMatthew Ahrens 96414843421SMatthew Ahrens if (os->os_userused_dnode && 96514843421SMatthew Ahrens os->os_userused_dnode->dn_type != DMU_OT_NONE) { 96614843421SMatthew Ahrens os->os_userused_dnode->dn_zio = zio; 96714843421SMatthew Ahrens dnode_sync(os->os_userused_dnode, tx); 96814843421SMatthew Ahrens os->os_groupused_dnode->dn_zio = zio; 96914843421SMatthew Ahrens dnode_sync(os->os_groupused_dnode, tx); 97014843421SMatthew Ahrens } 97114843421SMatthew Ahrens 972c717a561Smaybee txgoff = tx->tx_txg & TXG_MASK; 973fa9e4066Sahrens 97414843421SMatthew Ahrens if (dmu_objset_userused_enabled(os)) { 97514843421SMatthew Ahrens newlist = &os->os_synced_dnodes; 97614843421SMatthew Ahrens /* 97714843421SMatthew Ahrens * We must create the list here because it uses the 97814843421SMatthew Ahrens * dn_dirty_link[] of this txg. 97914843421SMatthew Ahrens */ 98014843421SMatthew Ahrens list_create(newlist, sizeof (dnode_t), 98114843421SMatthew Ahrens offsetof(dnode_t, dn_dirty_link[txgoff])); 98214843421SMatthew Ahrens } 98314843421SMatthew Ahrens 98414843421SMatthew Ahrens dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx); 98514843421SMatthew Ahrens dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx); 986fa9e4066Sahrens 987c717a561Smaybee list = &os->os_meta_dnode->dn_dirty_records[txgoff]; 988c717a561Smaybee while (dr = list_head(list)) { 989c717a561Smaybee ASSERT(dr->dr_dbuf->db_level == 0); 990c717a561Smaybee list_remove(list, dr); 991c717a561Smaybee if (dr->dr_zio) 992c717a561Smaybee zio_nowait(dr->dr_zio); 993c717a561Smaybee } 994c717a561Smaybee /* 995c717a561Smaybee * Free intent log blocks up to this tx. 996c717a561Smaybee */ 997c717a561Smaybee zil_sync(os->os_zil, tx); 998088f3894Sahrens os->os_phys->os_zil_header = os->os_zil_header; 999c717a561Smaybee zio_nowait(zio); 1000fa9e4066Sahrens } 1001fa9e4066Sahrens 100214843421SMatthew Ahrens static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES]; 100314843421SMatthew Ahrens 100414843421SMatthew Ahrens void 100514843421SMatthew Ahrens dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb) 100614843421SMatthew Ahrens { 100714843421SMatthew Ahrens used_cbs[ost] = cb; 100814843421SMatthew Ahrens } 100914843421SMatthew Ahrens 101014843421SMatthew Ahrens boolean_t 101114843421SMatthew Ahrens dmu_objset_userused_enabled(objset_impl_t *os) 101214843421SMatthew Ahrens { 101314843421SMatthew Ahrens return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE && 101414843421SMatthew Ahrens used_cbs[os->os_phys->os_type] && 101514843421SMatthew Ahrens os->os_userused_dnode); 101614843421SMatthew Ahrens } 101714843421SMatthew Ahrens 101814843421SMatthew Ahrens void 101914843421SMatthew Ahrens dmu_objset_do_userquota_callbacks(objset_impl_t *os, dmu_tx_t *tx) 102014843421SMatthew Ahrens { 102114843421SMatthew Ahrens dnode_t *dn; 102214843421SMatthew Ahrens list_t *list = &os->os_synced_dnodes; 102314843421SMatthew Ahrens static const char zerobuf[DN_MAX_BONUSLEN] = {0}; 102414843421SMatthew Ahrens 102514843421SMatthew Ahrens ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os)); 102614843421SMatthew Ahrens 102714843421SMatthew Ahrens while (dn = list_head(list)) { 102814843421SMatthew Ahrens dmu_object_type_t bonustype; 102914843421SMatthew Ahrens 103014843421SMatthew Ahrens ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object)); 103114843421SMatthew Ahrens ASSERT(dn->dn_oldphys); 103214843421SMatthew Ahrens ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE || 103314843421SMatthew Ahrens dn->dn_phys->dn_flags & 103414843421SMatthew Ahrens DNODE_FLAG_USERUSED_ACCOUNTED); 103514843421SMatthew Ahrens 103614843421SMatthew Ahrens /* Allocate the user/groupused objects if necessary. */ 103714843421SMatthew Ahrens if (os->os_userused_dnode->dn_type == DMU_OT_NONE) { 103814843421SMatthew Ahrens VERIFY(0 == zap_create_claim(&os->os, 103914843421SMatthew Ahrens DMU_USERUSED_OBJECT, 104014843421SMatthew Ahrens DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx)); 104114843421SMatthew Ahrens VERIFY(0 == zap_create_claim(&os->os, 104214843421SMatthew Ahrens DMU_GROUPUSED_OBJECT, 104314843421SMatthew Ahrens DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx)); 104414843421SMatthew Ahrens } 104514843421SMatthew Ahrens 104614843421SMatthew Ahrens /* 104714843421SMatthew Ahrens * If the object was not previously 104814843421SMatthew Ahrens * accounted, pretend that it was free. 104914843421SMatthew Ahrens */ 105014843421SMatthew Ahrens if (!(dn->dn_oldphys->dn_flags & 105114843421SMatthew Ahrens DNODE_FLAG_USERUSED_ACCOUNTED)) { 105214843421SMatthew Ahrens bzero(dn->dn_oldphys, sizeof (dnode_phys_t)); 105314843421SMatthew Ahrens } 105414843421SMatthew Ahrens 105514843421SMatthew Ahrens /* 105614843421SMatthew Ahrens * If the object was freed, use the previous bonustype. 105714843421SMatthew Ahrens */ 105814843421SMatthew Ahrens bonustype = dn->dn_phys->dn_bonustype ? 105914843421SMatthew Ahrens dn->dn_phys->dn_bonustype : dn->dn_oldphys->dn_bonustype; 106014843421SMatthew Ahrens ASSERT(dn->dn_phys->dn_type != 0 || 106114843421SMatthew Ahrens (bcmp(DN_BONUS(dn->dn_phys), zerobuf, 106214843421SMatthew Ahrens DN_MAX_BONUSLEN) == 0 && 106314843421SMatthew Ahrens DN_USED_BYTES(dn->dn_phys) == 0)); 106414843421SMatthew Ahrens ASSERT(dn->dn_oldphys->dn_type != 0 || 106514843421SMatthew Ahrens (bcmp(DN_BONUS(dn->dn_oldphys), zerobuf, 106614843421SMatthew Ahrens DN_MAX_BONUSLEN) == 0 && 106714843421SMatthew Ahrens DN_USED_BYTES(dn->dn_oldphys) == 0)); 106814843421SMatthew Ahrens used_cbs[os->os_phys->os_type](&os->os, bonustype, 106914843421SMatthew Ahrens DN_BONUS(dn->dn_oldphys), DN_BONUS(dn->dn_phys), 107014843421SMatthew Ahrens DN_USED_BYTES(dn->dn_oldphys), 107114843421SMatthew Ahrens DN_USED_BYTES(dn->dn_phys), tx); 107214843421SMatthew Ahrens 107314843421SMatthew Ahrens /* 107414843421SMatthew Ahrens * The mutex is needed here for interlock with dnode_allocate. 107514843421SMatthew Ahrens */ 107614843421SMatthew Ahrens mutex_enter(&dn->dn_mtx); 107714843421SMatthew Ahrens zio_buf_free(dn->dn_oldphys, sizeof (dnode_phys_t)); 107814843421SMatthew Ahrens dn->dn_oldphys = NULL; 107914843421SMatthew Ahrens mutex_exit(&dn->dn_mtx); 108014843421SMatthew Ahrens 108114843421SMatthew Ahrens list_remove(list, dn); 108214843421SMatthew Ahrens dnode_rele(dn, list); 108314843421SMatthew Ahrens } 108414843421SMatthew Ahrens } 108514843421SMatthew Ahrens 108614843421SMatthew Ahrens boolean_t 108714843421SMatthew Ahrens dmu_objset_userspace_present(objset_t *os) 108814843421SMatthew Ahrens { 108914843421SMatthew Ahrens return (os->os->os_phys->os_flags & 109014843421SMatthew Ahrens OBJSET_FLAG_USERACCOUNTING_COMPLETE); 109114843421SMatthew Ahrens } 109214843421SMatthew Ahrens 109314843421SMatthew Ahrens int 109414843421SMatthew Ahrens dmu_objset_userspace_upgrade(objset_t *os) 109514843421SMatthew Ahrens { 109614843421SMatthew Ahrens uint64_t obj; 109714843421SMatthew Ahrens int err = 0; 109814843421SMatthew Ahrens 109914843421SMatthew Ahrens if (dmu_objset_userspace_present(os)) 110014843421SMatthew Ahrens return (0); 110114843421SMatthew Ahrens if (!dmu_objset_userused_enabled(os->os)) 110214843421SMatthew Ahrens return (ENOTSUP); 110314843421SMatthew Ahrens if (dmu_objset_is_snapshot(os)) 110414843421SMatthew Ahrens return (EINVAL); 110514843421SMatthew Ahrens 110614843421SMatthew Ahrens /* 110714843421SMatthew Ahrens * We simply need to mark every object dirty, so that it will be 110814843421SMatthew Ahrens * synced out and now accounted. If this is called 110914843421SMatthew Ahrens * concurrently, or if we already did some work before crashing, 111014843421SMatthew Ahrens * that's fine, since we track each object's accounted state 111114843421SMatthew Ahrens * independently. 111214843421SMatthew Ahrens */ 111314843421SMatthew Ahrens 111414843421SMatthew Ahrens for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) { 111568857716SLin Ling dmu_tx_t *tx; 111614843421SMatthew Ahrens dmu_buf_t *db; 111714843421SMatthew Ahrens int objerr; 111814843421SMatthew Ahrens 111914843421SMatthew Ahrens if (issig(JUSTLOOKING) && issig(FORREAL)) 112014843421SMatthew Ahrens return (EINTR); 112114843421SMatthew Ahrens 112214843421SMatthew Ahrens objerr = dmu_bonus_hold(os, obj, FTAG, &db); 112314843421SMatthew Ahrens if (objerr) 112414843421SMatthew Ahrens continue; 112568857716SLin Ling tx = dmu_tx_create(os); 112614843421SMatthew Ahrens dmu_tx_hold_bonus(tx, obj); 112714843421SMatthew Ahrens objerr = dmu_tx_assign(tx, TXG_WAIT); 112814843421SMatthew Ahrens if (objerr) { 112914843421SMatthew Ahrens dmu_tx_abort(tx); 113014843421SMatthew Ahrens continue; 113114843421SMatthew Ahrens } 113214843421SMatthew Ahrens dmu_buf_will_dirty(db, tx); 113314843421SMatthew Ahrens dmu_buf_rele(db, FTAG); 113414843421SMatthew Ahrens dmu_tx_commit(tx); 113514843421SMatthew Ahrens } 113614843421SMatthew Ahrens 113714843421SMatthew Ahrens os->os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE; 113814843421SMatthew Ahrens txg_wait_synced(dmu_objset_pool(os), 0); 113914843421SMatthew Ahrens return (0); 114014843421SMatthew Ahrens } 114114843421SMatthew Ahrens 1142fa9e4066Sahrens void 1143a2eea2e1Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp, 1144a2eea2e1Sahrens uint64_t *usedobjsp, uint64_t *availobjsp) 1145fa9e4066Sahrens { 1146a2eea2e1Sahrens dsl_dataset_space(os->os->os_dsl_dataset, refdbytesp, availbytesp, 1147a2eea2e1Sahrens usedobjsp, availobjsp); 1148a2eea2e1Sahrens } 1149a2eea2e1Sahrens 1150a2eea2e1Sahrens uint64_t 1151a2eea2e1Sahrens dmu_objset_fsid_guid(objset_t *os) 1152a2eea2e1Sahrens { 1153a2eea2e1Sahrens return (dsl_dataset_fsid_guid(os->os->os_dsl_dataset)); 1154a2eea2e1Sahrens } 1155a2eea2e1Sahrens 1156a2eea2e1Sahrens void 1157a2eea2e1Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat) 1158a2eea2e1Sahrens { 1159a2eea2e1Sahrens stat->dds_type = os->os->os_phys->os_type; 1160a2eea2e1Sahrens if (os->os->os_dsl_dataset) 1161a2eea2e1Sahrens dsl_dataset_fast_stat(os->os->os_dsl_dataset, stat); 1162a2eea2e1Sahrens } 1163a2eea2e1Sahrens 1164a2eea2e1Sahrens void 1165a2eea2e1Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv) 1166a2eea2e1Sahrens { 1167a2eea2e1Sahrens ASSERT(os->os->os_dsl_dataset || 1168a2eea2e1Sahrens os->os->os_phys->os_type == DMU_OST_META); 1169a2eea2e1Sahrens 1170a2eea2e1Sahrens if (os->os->os_dsl_dataset != NULL) 1171a2eea2e1Sahrens dsl_dataset_stats(os->os->os_dsl_dataset, nv); 1172a2eea2e1Sahrens 1173a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE, 1174a2eea2e1Sahrens os->os->os_phys->os_type); 117514843421SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING, 117614843421SMatthew Ahrens dmu_objset_userspace_present(os)); 1177fa9e4066Sahrens } 1178fa9e4066Sahrens 1179fa9e4066Sahrens int 1180fa9e4066Sahrens dmu_objset_is_snapshot(objset_t *os) 1181fa9e4066Sahrens { 1182fa9e4066Sahrens if (os->os->os_dsl_dataset != NULL) 1183fa9e4066Sahrens return (dsl_dataset_is_snapshot(os->os->os_dsl_dataset)); 1184fa9e4066Sahrens else 1185fa9e4066Sahrens return (B_FALSE); 1186fa9e4066Sahrens } 1187fa9e4066Sahrens 1188ab04eb8eStimh int 1189ab04eb8eStimh dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen, 1190ab04eb8eStimh boolean_t *conflict) 1191ab04eb8eStimh { 1192ab04eb8eStimh dsl_dataset_t *ds = os->os->os_dsl_dataset; 1193ab04eb8eStimh uint64_t ignored; 1194ab04eb8eStimh 1195ab04eb8eStimh if (ds->ds_phys->ds_snapnames_zapobj == 0) 1196ab04eb8eStimh return (ENOENT); 1197ab04eb8eStimh 1198ab04eb8eStimh return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset, 1199ab04eb8eStimh ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST, 1200ab04eb8eStimh real, maxlen, conflict)); 1201ab04eb8eStimh } 1202ab04eb8eStimh 1203fa9e4066Sahrens int 1204fa9e4066Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name, 1205b38f0970Sck uint64_t *idp, uint64_t *offp, boolean_t *case_conflict) 1206fa9e4066Sahrens { 1207fa9e4066Sahrens dsl_dataset_t *ds = os->os->os_dsl_dataset; 1208fa9e4066Sahrens zap_cursor_t cursor; 1209fa9e4066Sahrens zap_attribute_t attr; 1210fa9e4066Sahrens 1211fa9e4066Sahrens if (ds->ds_phys->ds_snapnames_zapobj == 0) 1212fa9e4066Sahrens return (ENOENT); 1213fa9e4066Sahrens 1214fa9e4066Sahrens zap_cursor_init_serialized(&cursor, 1215fa9e4066Sahrens ds->ds_dir->dd_pool->dp_meta_objset, 1216fa9e4066Sahrens ds->ds_phys->ds_snapnames_zapobj, *offp); 1217fa9e4066Sahrens 121887e5029aSahrens if (zap_cursor_retrieve(&cursor, &attr) != 0) { 121987e5029aSahrens zap_cursor_fini(&cursor); 122087e5029aSahrens return (ENOENT); 122187e5029aSahrens } 122287e5029aSahrens 122387e5029aSahrens if (strlen(attr.za_name) + 1 > namelen) { 122487e5029aSahrens zap_cursor_fini(&cursor); 122587e5029aSahrens return (ENAMETOOLONG); 122687e5029aSahrens } 122787e5029aSahrens 122887e5029aSahrens (void) strcpy(name, attr.za_name); 122987e5029aSahrens if (idp) 123087e5029aSahrens *idp = attr.za_first_integer; 1231b38f0970Sck if (case_conflict) 1232b38f0970Sck *case_conflict = attr.za_normalization_conflict; 123387e5029aSahrens zap_cursor_advance(&cursor); 123487e5029aSahrens *offp = zap_cursor_serialize(&cursor); 123587e5029aSahrens zap_cursor_fini(&cursor); 123687e5029aSahrens 123787e5029aSahrens return (0); 123887e5029aSahrens } 123987e5029aSahrens 124087e5029aSahrens int 124187e5029aSahrens dmu_dir_list_next(objset_t *os, int namelen, char *name, 124287e5029aSahrens uint64_t *idp, uint64_t *offp) 124387e5029aSahrens { 124487e5029aSahrens dsl_dir_t *dd = os->os->os_dsl_dataset->ds_dir; 124587e5029aSahrens zap_cursor_t cursor; 124687e5029aSahrens zap_attribute_t attr; 124787e5029aSahrens 124887e5029aSahrens /* there is no next dir on a snapshot! */ 124987e5029aSahrens if (os->os->os_dsl_dataset->ds_object != 125087e5029aSahrens dd->dd_phys->dd_head_dataset_obj) 1251fa9e4066Sahrens return (ENOENT); 1252fa9e4066Sahrens 125387e5029aSahrens zap_cursor_init_serialized(&cursor, 125487e5029aSahrens dd->dd_pool->dp_meta_objset, 125587e5029aSahrens dd->dd_phys->dd_child_dir_zapobj, *offp); 125687e5029aSahrens 125787e5029aSahrens if (zap_cursor_retrieve(&cursor, &attr) != 0) { 125887e5029aSahrens zap_cursor_fini(&cursor); 125987e5029aSahrens return (ENOENT); 126087e5029aSahrens } 126187e5029aSahrens 126287e5029aSahrens if (strlen(attr.za_name) + 1 > namelen) { 126387e5029aSahrens zap_cursor_fini(&cursor); 1264fa9e4066Sahrens return (ENAMETOOLONG); 126587e5029aSahrens } 1266fa9e4066Sahrens 1267fa9e4066Sahrens (void) strcpy(name, attr.za_name); 126887e5029aSahrens if (idp) 126987e5029aSahrens *idp = attr.za_first_integer; 1270fa9e4066Sahrens zap_cursor_advance(&cursor); 1271fa9e4066Sahrens *offp = zap_cursor_serialize(&cursor); 127287e5029aSahrens zap_cursor_fini(&cursor); 1273fa9e4066Sahrens 1274fa9e4066Sahrens return (0); 1275fa9e4066Sahrens } 1276fa9e4066Sahrens 1277088f3894Sahrens struct findarg { 1278088f3894Sahrens int (*func)(char *, void *); 1279088f3894Sahrens void *arg; 1280088f3894Sahrens }; 1281088f3894Sahrens 1282088f3894Sahrens /* ARGSUSED */ 1283088f3894Sahrens static int 1284088f3894Sahrens findfunc(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg) 1285088f3894Sahrens { 1286088f3894Sahrens struct findarg *fa = arg; 1287088f3894Sahrens return (fa->func((char *)dsname, fa->arg)); 1288088f3894Sahrens } 1289088f3894Sahrens 1290fa9e4066Sahrens /* 1291fa9e4066Sahrens * Find all objsets under name, and for each, call 'func(child_name, arg)'. 1292088f3894Sahrens * Perhaps change all callers to use dmu_objset_find_spa()? 1293fa9e4066Sahrens */ 12941d452cf5Sahrens int 12951d452cf5Sahrens dmu_objset_find(char *name, int func(char *, void *), void *arg, int flags) 1296088f3894Sahrens { 1297088f3894Sahrens struct findarg fa; 1298088f3894Sahrens fa.func = func; 1299088f3894Sahrens fa.arg = arg; 1300088f3894Sahrens return (dmu_objset_find_spa(NULL, name, findfunc, &fa, flags)); 1301088f3894Sahrens } 1302088f3894Sahrens 1303088f3894Sahrens /* 1304088f3894Sahrens * Find all objsets under name, call func on each 1305088f3894Sahrens */ 1306088f3894Sahrens int 1307088f3894Sahrens dmu_objset_find_spa(spa_t *spa, const char *name, 1308088f3894Sahrens int func(spa_t *, uint64_t, const char *, void *), void *arg, int flags) 1309fa9e4066Sahrens { 1310fa9e4066Sahrens dsl_dir_t *dd; 1311088f3894Sahrens dsl_pool_t *dp; 1312088f3894Sahrens dsl_dataset_t *ds; 1313fa9e4066Sahrens zap_cursor_t zc; 1314b7661cccSmmusante zap_attribute_t *attr; 1315fa9e4066Sahrens char *child; 1316088f3894Sahrens uint64_t thisobj; 1317088f3894Sahrens int err; 1318fa9e4066Sahrens 1319088f3894Sahrens if (name == NULL) 1320088f3894Sahrens name = spa_name(spa); 1321088f3894Sahrens err = dsl_dir_open_spa(spa, name, FTAG, &dd, NULL); 1322ea8dc4b6Seschrock if (err) 13231d452cf5Sahrens return (err); 1324fa9e4066Sahrens 1325088f3894Sahrens /* Don't visit hidden ($MOS & $ORIGIN) objsets. */ 1326088f3894Sahrens if (dd->dd_myname[0] == '$') { 1327088f3894Sahrens dsl_dir_close(dd, FTAG); 1328088f3894Sahrens return (0); 1329088f3894Sahrens } 1330088f3894Sahrens 1331088f3894Sahrens thisobj = dd->dd_phys->dd_head_dataset_obj; 1332b7661cccSmmusante attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); 1333088f3894Sahrens dp = dd->dd_pool; 1334fa9e4066Sahrens 1335fa9e4066Sahrens /* 1336fa9e4066Sahrens * Iterate over all children. 1337fa9e4066Sahrens */ 13380b69c2f0Sahrens if (flags & DS_FIND_CHILDREN) { 1339088f3894Sahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, 13400b69c2f0Sahrens dd->dd_phys->dd_child_dir_zapobj); 1341b7661cccSmmusante zap_cursor_retrieve(&zc, attr) == 0; 13420b69c2f0Sahrens (void) zap_cursor_advance(&zc)) { 1343b7661cccSmmusante ASSERT(attr->za_integer_length == sizeof (uint64_t)); 1344b7661cccSmmusante ASSERT(attr->za_num_integers == 1); 1345fa9e4066Sahrens 13460b69c2f0Sahrens child = kmem_alloc(MAXPATHLEN, KM_SLEEP); 1347088f3894Sahrens (void) strcpy(child, name); 13480b69c2f0Sahrens (void) strcat(child, "/"); 1349b7661cccSmmusante (void) strcat(child, attr->za_name); 1350088f3894Sahrens err = dmu_objset_find_spa(spa, child, func, arg, flags); 13510b69c2f0Sahrens kmem_free(child, MAXPATHLEN); 13520b69c2f0Sahrens if (err) 13530b69c2f0Sahrens break; 13540b69c2f0Sahrens } 13550b69c2f0Sahrens zap_cursor_fini(&zc); 13561d452cf5Sahrens 13570b69c2f0Sahrens if (err) { 13580b69c2f0Sahrens dsl_dir_close(dd, FTAG); 1359b7661cccSmmusante kmem_free(attr, sizeof (zap_attribute_t)); 13600b69c2f0Sahrens return (err); 13610b69c2f0Sahrens } 1362fa9e4066Sahrens } 1363fa9e4066Sahrens 1364fa9e4066Sahrens /* 1365fa9e4066Sahrens * Iterate over all snapshots. 1366fa9e4066Sahrens */ 1367088f3894Sahrens if (flags & DS_FIND_SNAPSHOTS) { 1368088f3894Sahrens if (!dsl_pool_sync_context(dp)) 1369088f3894Sahrens rw_enter(&dp->dp_config_rwlock, RW_READER); 1370088f3894Sahrens err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds); 1371088f3894Sahrens if (!dsl_pool_sync_context(dp)) 1372088f3894Sahrens rw_exit(&dp->dp_config_rwlock); 1373088f3894Sahrens 1374088f3894Sahrens if (err == 0) { 1375088f3894Sahrens uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj; 1376088f3894Sahrens dsl_dataset_rele(ds, FTAG); 1377088f3894Sahrens 1378088f3894Sahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj); 1379088f3894Sahrens zap_cursor_retrieve(&zc, attr) == 0; 1380088f3894Sahrens (void) zap_cursor_advance(&zc)) { 1381088f3894Sahrens ASSERT(attr->za_integer_length == 1382088f3894Sahrens sizeof (uint64_t)); 1383088f3894Sahrens ASSERT(attr->za_num_integers == 1); 1384088f3894Sahrens 1385088f3894Sahrens child = kmem_alloc(MAXPATHLEN, KM_SLEEP); 1386088f3894Sahrens (void) strcpy(child, name); 1387088f3894Sahrens (void) strcat(child, "@"); 1388088f3894Sahrens (void) strcat(child, attr->za_name); 1389088f3894Sahrens err = func(spa, attr->za_first_integer, 1390088f3894Sahrens child, arg); 1391088f3894Sahrens kmem_free(child, MAXPATHLEN); 1392088f3894Sahrens if (err) 1393088f3894Sahrens break; 1394088f3894Sahrens } 1395088f3894Sahrens zap_cursor_fini(&zc); 1396fa9e4066Sahrens } 1397fa9e4066Sahrens } 1398fa9e4066Sahrens 1399fa9e4066Sahrens dsl_dir_close(dd, FTAG); 1400b7661cccSmmusante kmem_free(attr, sizeof (zap_attribute_t)); 1401fa9e4066Sahrens 14021d452cf5Sahrens if (err) 14031d452cf5Sahrens return (err); 14041d452cf5Sahrens 1405fa9e4066Sahrens /* 1406fa9e4066Sahrens * Apply to self if appropriate. 1407fa9e4066Sahrens */ 1408088f3894Sahrens err = func(spa, thisobj, name, arg); 14091d452cf5Sahrens return (err); 1410fa9e4066Sahrens } 1411f18faf3fSek 14127f73c863SRich Morris /* ARGSUSED */ 14137f73c863SRich Morris int 14147f73c863SRich Morris dmu_objset_prefetch(char *name, void *arg) 14157f73c863SRich Morris { 14167f73c863SRich Morris dsl_dataset_t *ds; 14177f73c863SRich Morris 14187f73c863SRich Morris if (dsl_dataset_hold(name, FTAG, &ds)) 14197f73c863SRich Morris return (0); 14207f73c863SRich Morris 14217f73c863SRich Morris if (!BP_IS_HOLE(&ds->ds_phys->ds_bp)) { 14227f73c863SRich Morris mutex_enter(&ds->ds_opening_lock); 14237f73c863SRich Morris if (!dsl_dataset_get_user_ptr(ds)) { 14247f73c863SRich Morris uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH; 14257f73c863SRich Morris zbookmark_t zb; 14267f73c863SRich Morris 14277f73c863SRich Morris zb.zb_objset = ds->ds_object; 14287f73c863SRich Morris zb.zb_object = 0; 14297f73c863SRich Morris zb.zb_level = -1; 14307f73c863SRich Morris zb.zb_blkid = 0; 14317f73c863SRich Morris 14327f73c863SRich Morris (void) arc_read_nolock(NULL, dsl_dataset_get_spa(ds), 14337f73c863SRich Morris &ds->ds_phys->ds_bp, NULL, NULL, 14347f73c863SRich Morris ZIO_PRIORITY_ASYNC_READ, 14357f73c863SRich Morris ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, 14367f73c863SRich Morris &aflags, &zb); 14377f73c863SRich Morris } 14387f73c863SRich Morris mutex_exit(&ds->ds_opening_lock); 14397f73c863SRich Morris } 14407f73c863SRich Morris 14417f73c863SRich Morris dsl_dataset_rele(ds, FTAG); 14427f73c863SRich Morris return (0); 14437f73c863SRich Morris } 14447f73c863SRich Morris 1445f18faf3fSek void 1446f18faf3fSek dmu_objset_set_user(objset_t *os, void *user_ptr) 1447f18faf3fSek { 1448f18faf3fSek ASSERT(MUTEX_HELD(&os->os->os_user_ptr_lock)); 1449f18faf3fSek os->os->os_user_ptr = user_ptr; 1450f18faf3fSek } 1451f18faf3fSek 1452f18faf3fSek void * 1453f18faf3fSek dmu_objset_get_user(objset_t *os) 1454f18faf3fSek { 1455f18faf3fSek ASSERT(MUTEX_HELD(&os->os->os_user_ptr_lock)); 1456f18faf3fSek return (os->os->os_user_ptr); 1457f18faf3fSek } 1458