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 /* 2206e0070dSMark Shellenbaum * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23fa9e4066Sahrens */ 24fa9e4066Sahrens 25*55da60b9SMark J Musante /* Portions Copyright 2010 Robert Milkowski */ 26*55da60b9SMark J Musante 27ecd6cf80Smarks #include <sys/cred.h> 28fa9e4066Sahrens #include <sys/zfs_context.h> 29fa9e4066Sahrens #include <sys/dmu_objset.h> 30fa9e4066Sahrens #include <sys/dsl_dir.h> 31fa9e4066Sahrens #include <sys/dsl_dataset.h> 32fa9e4066Sahrens #include <sys/dsl_prop.h> 33fa9e4066Sahrens #include <sys/dsl_pool.h> 341d452cf5Sahrens #include <sys/dsl_synctask.h> 35ecd6cf80Smarks #include <sys/dsl_deleg.h> 36fa9e4066Sahrens #include <sys/dnode.h> 37fa9e4066Sahrens #include <sys/dbuf.h> 38a2eea2e1Sahrens #include <sys/zvol.h> 39fa9e4066Sahrens #include <sys/dmu_tx.h> 40fa9e4066Sahrens #include <sys/zap.h> 41fa9e4066Sahrens #include <sys/zil.h> 42fa9e4066Sahrens #include <sys/dmu_impl.h> 43ecd6cf80Smarks #include <sys/zfs_ioctl.h> 44486ae710SMatthew Ahrens #include <sys/sunddi.h> 450a586ceaSMark Shellenbaum #include <sys/sa.h> 46fa9e4066Sahrens 47fa9e4066Sahrens spa_t * 48fa9e4066Sahrens dmu_objset_spa(objset_t *os) 49fa9e4066Sahrens { 50503ad85cSMatthew Ahrens return (os->os_spa); 51fa9e4066Sahrens } 52fa9e4066Sahrens 53fa9e4066Sahrens zilog_t * 54fa9e4066Sahrens dmu_objset_zil(objset_t *os) 55fa9e4066Sahrens { 56503ad85cSMatthew Ahrens return (os->os_zil); 57fa9e4066Sahrens } 58fa9e4066Sahrens 59fa9e4066Sahrens dsl_pool_t * 60fa9e4066Sahrens dmu_objset_pool(objset_t *os) 61fa9e4066Sahrens { 62fa9e4066Sahrens dsl_dataset_t *ds; 63fa9e4066Sahrens 64503ad85cSMatthew Ahrens if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir) 65fa9e4066Sahrens return (ds->ds_dir->dd_pool); 66fa9e4066Sahrens else 67503ad85cSMatthew Ahrens return (spa_get_dsl(os->os_spa)); 68fa9e4066Sahrens } 69fa9e4066Sahrens 70fa9e4066Sahrens dsl_dataset_t * 71fa9e4066Sahrens dmu_objset_ds(objset_t *os) 72fa9e4066Sahrens { 73503ad85cSMatthew Ahrens return (os->os_dsl_dataset); 74fa9e4066Sahrens } 75fa9e4066Sahrens 76fa9e4066Sahrens dmu_objset_type_t 77fa9e4066Sahrens dmu_objset_type(objset_t *os) 78fa9e4066Sahrens { 79503ad85cSMatthew Ahrens return (os->os_phys->os_type); 80fa9e4066Sahrens } 81fa9e4066Sahrens 82fa9e4066Sahrens void 83fa9e4066Sahrens dmu_objset_name(objset_t *os, char *buf) 84fa9e4066Sahrens { 85503ad85cSMatthew Ahrens dsl_dataset_name(os->os_dsl_dataset, buf); 86fa9e4066Sahrens } 87fa9e4066Sahrens 88fa9e4066Sahrens uint64_t 89fa9e4066Sahrens dmu_objset_id(objset_t *os) 90fa9e4066Sahrens { 91503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 92fa9e4066Sahrens 93fa9e4066Sahrens return (ds ? ds->ds_object : 0); 94fa9e4066Sahrens } 95fa9e4066Sahrens 96*55da60b9SMark J Musante uint64_t 97*55da60b9SMark J Musante dmu_objset_syncprop(objset_t *os) 98*55da60b9SMark J Musante { 99*55da60b9SMark J Musante return (os->os_sync); 100*55da60b9SMark J Musante } 101*55da60b9SMark J Musante 102e09fa4daSNeil Perrin uint64_t 103e09fa4daSNeil Perrin dmu_objset_logbias(objset_t *os) 104e09fa4daSNeil Perrin { 105e09fa4daSNeil Perrin return (os->os_logbias); 106e09fa4daSNeil Perrin } 107e09fa4daSNeil Perrin 108fa9e4066Sahrens static void 109fa9e4066Sahrens checksum_changed_cb(void *arg, uint64_t newval) 110fa9e4066Sahrens { 111503ad85cSMatthew Ahrens objset_t *os = arg; 112fa9e4066Sahrens 113fa9e4066Sahrens /* 114fa9e4066Sahrens * Inheritance should have been done by now. 115fa9e4066Sahrens */ 116fa9e4066Sahrens ASSERT(newval != ZIO_CHECKSUM_INHERIT); 117fa9e4066Sahrens 118503ad85cSMatthew Ahrens os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE); 119fa9e4066Sahrens } 120fa9e4066Sahrens 121fa9e4066Sahrens static void 122fa9e4066Sahrens compression_changed_cb(void *arg, uint64_t newval) 123fa9e4066Sahrens { 124503ad85cSMatthew Ahrens objset_t *os = arg; 125fa9e4066Sahrens 126fa9e4066Sahrens /* 127fa9e4066Sahrens * Inheritance and range checking should have been done by now. 128fa9e4066Sahrens */ 129fa9e4066Sahrens ASSERT(newval != ZIO_COMPRESS_INHERIT); 130fa9e4066Sahrens 131503ad85cSMatthew Ahrens os->os_compress = zio_compress_select(newval, ZIO_COMPRESS_ON_VALUE); 132fa9e4066Sahrens } 133fa9e4066Sahrens 134d0ad202dSahrens static void 135d0ad202dSahrens copies_changed_cb(void *arg, uint64_t newval) 136d0ad202dSahrens { 137503ad85cSMatthew Ahrens objset_t *os = arg; 138d0ad202dSahrens 139d0ad202dSahrens /* 140d0ad202dSahrens * Inheritance and range checking should have been done by now. 141d0ad202dSahrens */ 142d0ad202dSahrens ASSERT(newval > 0); 143503ad85cSMatthew Ahrens ASSERT(newval <= spa_max_replication(os->os_spa)); 144d0ad202dSahrens 145503ad85cSMatthew Ahrens os->os_copies = newval; 146d0ad202dSahrens } 147d0ad202dSahrens 148b24ab676SJeff Bonwick static void 149b24ab676SJeff Bonwick dedup_changed_cb(void *arg, uint64_t newval) 150b24ab676SJeff Bonwick { 151b24ab676SJeff Bonwick objset_t *os = arg; 152b24ab676SJeff Bonwick spa_t *spa = os->os_spa; 153b24ab676SJeff Bonwick enum zio_checksum checksum; 154b24ab676SJeff Bonwick 155b24ab676SJeff Bonwick /* 156b24ab676SJeff Bonwick * Inheritance should have been done by now. 157b24ab676SJeff Bonwick */ 158b24ab676SJeff Bonwick ASSERT(newval != ZIO_CHECKSUM_INHERIT); 159b24ab676SJeff Bonwick 160b24ab676SJeff Bonwick checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF); 161b24ab676SJeff Bonwick 162b24ab676SJeff Bonwick os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK; 163b24ab676SJeff Bonwick os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY); 164b24ab676SJeff Bonwick } 165b24ab676SJeff Bonwick 1663baa08fcSek static void 1673baa08fcSek primary_cache_changed_cb(void *arg, uint64_t newval) 1683baa08fcSek { 169503ad85cSMatthew Ahrens objset_t *os = arg; 1703baa08fcSek 1713baa08fcSek /* 1723baa08fcSek * Inheritance and range checking should have been done by now. 1733baa08fcSek */ 1743baa08fcSek ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE || 1753baa08fcSek newval == ZFS_CACHE_METADATA); 1763baa08fcSek 177503ad85cSMatthew Ahrens os->os_primary_cache = newval; 1783baa08fcSek } 1793baa08fcSek 1803baa08fcSek static void 1813baa08fcSek secondary_cache_changed_cb(void *arg, uint64_t newval) 1823baa08fcSek { 183503ad85cSMatthew Ahrens objset_t *os = arg; 1843baa08fcSek 1853baa08fcSek /* 1863baa08fcSek * Inheritance and range checking should have been done by now. 1873baa08fcSek */ 1883baa08fcSek ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE || 1893baa08fcSek newval == ZFS_CACHE_METADATA); 1903baa08fcSek 191503ad85cSMatthew Ahrens os->os_secondary_cache = newval; 1923baa08fcSek } 1933baa08fcSek 194*55da60b9SMark J Musante static void 195*55da60b9SMark J Musante sync_changed_cb(void *arg, uint64_t newval) 196*55da60b9SMark J Musante { 197*55da60b9SMark J Musante objset_t *os = arg; 198*55da60b9SMark J Musante 199*55da60b9SMark J Musante /* 200*55da60b9SMark J Musante * Inheritance and range checking should have been done by now. 201*55da60b9SMark J Musante */ 202*55da60b9SMark J Musante ASSERT(newval == ZFS_SYNC_STANDARD || newval == ZFS_SYNC_ALWAYS || 203*55da60b9SMark J Musante newval == ZFS_SYNC_DISABLED); 204*55da60b9SMark J Musante 205*55da60b9SMark J Musante os->os_sync = newval; 206*55da60b9SMark J Musante if (os->os_zil) 207*55da60b9SMark J Musante zil_set_sync(os->os_zil, newval); 208*55da60b9SMark J Musante } 209*55da60b9SMark J Musante 210e09fa4daSNeil Perrin static void 211e09fa4daSNeil Perrin logbias_changed_cb(void *arg, uint64_t newval) 212e09fa4daSNeil Perrin { 213e09fa4daSNeil Perrin objset_t *os = arg; 214e09fa4daSNeil Perrin 215e09fa4daSNeil Perrin ASSERT(newval == ZFS_LOGBIAS_LATENCY || 216e09fa4daSNeil Perrin newval == ZFS_LOGBIAS_THROUGHPUT); 217e09fa4daSNeil Perrin os->os_logbias = newval; 218e09fa4daSNeil Perrin if (os->os_zil) 219e09fa4daSNeil Perrin zil_set_logbias(os->os_zil, newval); 220e09fa4daSNeil Perrin } 221e09fa4daSNeil Perrin 222fa9e4066Sahrens void 223fa9e4066Sahrens dmu_objset_byteswap(void *buf, size_t size) 224fa9e4066Sahrens { 225fa9e4066Sahrens objset_phys_t *osp = buf; 226fa9e4066Sahrens 22714843421SMatthew Ahrens ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t)); 228fa9e4066Sahrens dnode_byteswap(&osp->os_meta_dnode); 229fa9e4066Sahrens byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t)); 230fa9e4066Sahrens osp->os_type = BSWAP_64(osp->os_type); 23114843421SMatthew Ahrens osp->os_flags = BSWAP_64(osp->os_flags); 23214843421SMatthew Ahrens if (size == sizeof (objset_phys_t)) { 23314843421SMatthew Ahrens dnode_byteswap(&osp->os_userused_dnode); 23414843421SMatthew Ahrens dnode_byteswap(&osp->os_groupused_dnode); 23514843421SMatthew Ahrens } 236fa9e4066Sahrens } 237fa9e4066Sahrens 238ea8dc4b6Seschrock int 239ea8dc4b6Seschrock dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, 240503ad85cSMatthew Ahrens objset_t **osp) 241fa9e4066Sahrens { 242503ad85cSMatthew Ahrens objset_t *os; 243088f3894Sahrens int i, err; 244fa9e4066Sahrens 24591ebeef5Sahrens ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock)); 24691ebeef5Sahrens 247503ad85cSMatthew Ahrens os = kmem_zalloc(sizeof (objset_t), KM_SLEEP); 248503ad85cSMatthew Ahrens os->os_dsl_dataset = ds; 249503ad85cSMatthew Ahrens os->os_spa = spa; 250503ad85cSMatthew Ahrens os->os_rootbp = bp; 251503ad85cSMatthew Ahrens if (!BP_IS_HOLE(os->os_rootbp)) { 25213506d1eSmaybee uint32_t aflags = ARC_WAIT; 253ea8dc4b6Seschrock zbookmark_t zb; 254b24ab676SJeff Bonwick SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET, 255b24ab676SJeff Bonwick ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 256b24ab676SJeff Bonwick 257503ad85cSMatthew Ahrens if (DMU_OS_IS_L2CACHEABLE(os)) 2583baa08fcSek aflags |= ARC_L2CACHE; 259ea8dc4b6Seschrock 260503ad85cSMatthew Ahrens dprintf_bp(os->os_rootbp, "reading %s", ""); 261088f3894Sahrens /* 262088f3894Sahrens * NB: when bprewrite scrub can change the bp, 263088f3894Sahrens * and this is called from dmu_objset_open_ds_os, the bp 264088f3894Sahrens * could change, and we'll need a lock. 265088f3894Sahrens */ 266503ad85cSMatthew Ahrens err = arc_read_nolock(NULL, spa, os->os_rootbp, 267503ad85cSMatthew Ahrens arc_getbuf_func, &os->os_phys_buf, 26813506d1eSmaybee ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb); 269ea8dc4b6Seschrock if (err) { 270503ad85cSMatthew Ahrens kmem_free(os, sizeof (objset_t)); 271b87f3af3Sperrin /* convert checksum errors into IO errors */ 272b87f3af3Sperrin if (err == ECKSUM) 273b87f3af3Sperrin err = EIO; 274ea8dc4b6Seschrock return (err); 275ea8dc4b6Seschrock } 27614843421SMatthew Ahrens 27714843421SMatthew Ahrens /* Increase the blocksize if we are permitted. */ 27814843421SMatthew Ahrens if (spa_version(spa) >= SPA_VERSION_USERSPACE && 279503ad85cSMatthew Ahrens arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) { 28014843421SMatthew Ahrens arc_buf_t *buf = arc_buf_alloc(spa, 281503ad85cSMatthew Ahrens sizeof (objset_phys_t), &os->os_phys_buf, 28214843421SMatthew Ahrens ARC_BUFC_METADATA); 28314843421SMatthew Ahrens bzero(buf->b_data, sizeof (objset_phys_t)); 284503ad85cSMatthew Ahrens bcopy(os->os_phys_buf->b_data, buf->b_data, 285503ad85cSMatthew Ahrens arc_buf_size(os->os_phys_buf)); 286503ad85cSMatthew Ahrens (void) arc_buf_remove_ref(os->os_phys_buf, 287503ad85cSMatthew Ahrens &os->os_phys_buf); 288503ad85cSMatthew Ahrens os->os_phys_buf = buf; 28914843421SMatthew Ahrens } 29014843421SMatthew Ahrens 291503ad85cSMatthew Ahrens os->os_phys = os->os_phys_buf->b_data; 292503ad85cSMatthew Ahrens os->os_flags = os->os_phys->os_flags; 293fa9e4066Sahrens } else { 29414843421SMatthew Ahrens int size = spa_version(spa) >= SPA_VERSION_USERSPACE ? 29514843421SMatthew Ahrens sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE; 296503ad85cSMatthew Ahrens os->os_phys_buf = arc_buf_alloc(spa, size, 297503ad85cSMatthew Ahrens &os->os_phys_buf, ARC_BUFC_METADATA); 298503ad85cSMatthew Ahrens os->os_phys = os->os_phys_buf->b_data; 299503ad85cSMatthew Ahrens bzero(os->os_phys, size); 300fa9e4066Sahrens } 301fa9e4066Sahrens 302fa9e4066Sahrens /* 303fa9e4066Sahrens * Note: the changed_cb will be called once before the register 304fa9e4066Sahrens * func returns, thus changing the checksum/compression from the 3053baa08fcSek * default (fletcher2/off). Snapshots don't need to know about 3063baa08fcSek * checksum/compression/copies. 307fa9e4066Sahrens */ 3083baa08fcSek if (ds) { 3093baa08fcSek err = dsl_prop_register(ds, "primarycache", 310503ad85cSMatthew Ahrens primary_cache_changed_cb, os); 311d0ad202dSahrens if (err == 0) 3123baa08fcSek err = dsl_prop_register(ds, "secondarycache", 313503ad85cSMatthew Ahrens secondary_cache_changed_cb, os); 3143baa08fcSek if (!dsl_dataset_is_snapshot(ds)) { 3153baa08fcSek if (err == 0) 3163baa08fcSek err = dsl_prop_register(ds, "checksum", 317503ad85cSMatthew Ahrens checksum_changed_cb, os); 3183baa08fcSek if (err == 0) 3193baa08fcSek err = dsl_prop_register(ds, "compression", 320503ad85cSMatthew Ahrens compression_changed_cb, os); 3213baa08fcSek if (err == 0) 3223baa08fcSek err = dsl_prop_register(ds, "copies", 323503ad85cSMatthew Ahrens copies_changed_cb, os); 324b24ab676SJeff Bonwick if (err == 0) 325b24ab676SJeff Bonwick err = dsl_prop_register(ds, "dedup", 326b24ab676SJeff Bonwick dedup_changed_cb, os); 327e09fa4daSNeil Perrin if (err == 0) 328e09fa4daSNeil Perrin err = dsl_prop_register(ds, "logbias", 329e09fa4daSNeil Perrin logbias_changed_cb, os); 330*55da60b9SMark J Musante if (err == 0) 331*55da60b9SMark J Musante err = dsl_prop_register(ds, "sync", 332*55da60b9SMark J Musante sync_changed_cb, os); 3333baa08fcSek } 334ea8dc4b6Seschrock if (err) { 335503ad85cSMatthew Ahrens VERIFY(arc_buf_remove_ref(os->os_phys_buf, 336503ad85cSMatthew Ahrens &os->os_phys_buf) == 1); 337503ad85cSMatthew Ahrens kmem_free(os, sizeof (objset_t)); 338ea8dc4b6Seschrock return (err); 339ea8dc4b6Seschrock } 34099653d4eSeschrock } else if (ds == NULL) { 341fa9e4066Sahrens /* It's the meta-objset. */ 342503ad85cSMatthew Ahrens os->os_checksum = ZIO_CHECKSUM_FLETCHER_4; 343503ad85cSMatthew Ahrens os->os_compress = ZIO_COMPRESS_LZJB; 344503ad85cSMatthew Ahrens os->os_copies = spa_max_replication(spa); 345b24ab676SJeff Bonwick os->os_dedup_checksum = ZIO_CHECKSUM_OFF; 346b24ab676SJeff Bonwick os->os_dedup_verify = 0; 347b24ab676SJeff Bonwick os->os_logbias = 0; 348*55da60b9SMark J Musante os->os_sync = 0; 349503ad85cSMatthew Ahrens os->os_primary_cache = ZFS_CACHE_ALL; 350503ad85cSMatthew Ahrens os->os_secondary_cache = ZFS_CACHE_ALL; 351fa9e4066Sahrens } 352fa9e4066Sahrens 353503ad85cSMatthew Ahrens os->os_zil_header = os->os_phys->os_zil_header; 354503ad85cSMatthew Ahrens os->os_zil = zil_alloc(os, &os->os_zil_header); 355fa9e4066Sahrens 356fa9e4066Sahrens for (i = 0; i < TXG_SIZE; i++) { 357503ad85cSMatthew Ahrens list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t), 358fa9e4066Sahrens offsetof(dnode_t, dn_dirty_link[i])); 359503ad85cSMatthew Ahrens list_create(&os->os_free_dnodes[i], sizeof (dnode_t), 360fa9e4066Sahrens offsetof(dnode_t, dn_dirty_link[i])); 361fa9e4066Sahrens } 362503ad85cSMatthew Ahrens list_create(&os->os_dnodes, sizeof (dnode_t), 363fa9e4066Sahrens offsetof(dnode_t, dn_link)); 364503ad85cSMatthew Ahrens list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t), 365fa9e4066Sahrens offsetof(dmu_buf_impl_t, db_link)); 366fa9e4066Sahrens 367503ad85cSMatthew Ahrens mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL); 368503ad85cSMatthew Ahrens mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL); 369503ad85cSMatthew Ahrens mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL); 370503ad85cSMatthew Ahrens 371503ad85cSMatthew Ahrens os->os_meta_dnode = dnode_special_open(os, 372503ad85cSMatthew Ahrens &os->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT); 373503ad85cSMatthew Ahrens if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) { 374503ad85cSMatthew Ahrens os->os_userused_dnode = dnode_special_open(os, 375503ad85cSMatthew Ahrens &os->os_phys->os_userused_dnode, DMU_USERUSED_OBJECT); 376503ad85cSMatthew Ahrens os->os_groupused_dnode = dnode_special_open(os, 377503ad85cSMatthew Ahrens &os->os_phys->os_groupused_dnode, DMU_GROUPUSED_OBJECT); 37814843421SMatthew Ahrens } 379fa9e4066Sahrens 38091ebeef5Sahrens /* 38191ebeef5Sahrens * We should be the only thread trying to do this because we 38291ebeef5Sahrens * have ds_opening_lock 38391ebeef5Sahrens */ 38491ebeef5Sahrens if (ds) { 385503ad85cSMatthew Ahrens mutex_enter(&ds->ds_lock); 386503ad85cSMatthew Ahrens ASSERT(ds->ds_objset == NULL); 387503ad85cSMatthew Ahrens ds->ds_objset = os; 388503ad85cSMatthew Ahrens mutex_exit(&ds->ds_lock); 389fa9e4066Sahrens } 390fa9e4066Sahrens 391503ad85cSMatthew Ahrens *osp = os; 392ea8dc4b6Seschrock return (0); 393fa9e4066Sahrens } 394fa9e4066Sahrens 395503ad85cSMatthew Ahrens int 396503ad85cSMatthew Ahrens dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp) 3973cb34c60Sahrens { 398503ad85cSMatthew Ahrens int err = 0; 3993cb34c60Sahrens 4003cb34c60Sahrens mutex_enter(&ds->ds_opening_lock); 401503ad85cSMatthew Ahrens *osp = ds->ds_objset; 402503ad85cSMatthew Ahrens if (*osp == NULL) { 4033cb34c60Sahrens err = dmu_objset_open_impl(dsl_dataset_get_spa(ds), 404503ad85cSMatthew Ahrens ds, &ds->ds_phys->ds_bp, osp); 4053cb34c60Sahrens } 4063cb34c60Sahrens mutex_exit(&ds->ds_opening_lock); 407503ad85cSMatthew Ahrens return (err); 4083cb34c60Sahrens } 4093cb34c60Sahrens 410503ad85cSMatthew Ahrens /* called from zpl */ 4113cb34c60Sahrens int 412503ad85cSMatthew Ahrens dmu_objset_hold(const char *name, void *tag, objset_t **osp) 4133cb34c60Sahrens { 414503ad85cSMatthew Ahrens dsl_dataset_t *ds; 4153cb34c60Sahrens int err; 4163cb34c60Sahrens 417503ad85cSMatthew Ahrens err = dsl_dataset_hold(name, tag, &ds); 4183cb34c60Sahrens if (err) 419503ad85cSMatthew Ahrens return (err); 420503ad85cSMatthew Ahrens 421503ad85cSMatthew Ahrens err = dmu_objset_from_ds(ds, osp); 422503ad85cSMatthew Ahrens if (err) 423503ad85cSMatthew Ahrens dsl_dataset_rele(ds, tag); 424503ad85cSMatthew Ahrens 4253cb34c60Sahrens return (err); 4263cb34c60Sahrens } 4273cb34c60Sahrens 428fa9e4066Sahrens /* called from zpl */ 429fa9e4066Sahrens int 430503ad85cSMatthew Ahrens dmu_objset_own(const char *name, dmu_objset_type_t type, 431503ad85cSMatthew Ahrens boolean_t readonly, void *tag, objset_t **osp) 432fa9e4066Sahrens { 433f18faf3fSek dsl_dataset_t *ds; 434f18faf3fSek int err; 435fa9e4066Sahrens 436503ad85cSMatthew Ahrens err = dsl_dataset_own(name, B_FALSE, tag, &ds); 437503ad85cSMatthew Ahrens if (err) 438fa9e4066Sahrens return (err); 439fa9e4066Sahrens 440503ad85cSMatthew Ahrens err = dmu_objset_from_ds(ds, osp); 4413cb34c60Sahrens if (err) { 442503ad85cSMatthew Ahrens dsl_dataset_disown(ds, tag); 443681d9761SEric Taylor } else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) { 444503ad85cSMatthew Ahrens dmu_objset_disown(*osp, tag); 445503ad85cSMatthew Ahrens return (EINVAL); 446681d9761SEric Taylor } else if (!readonly && dsl_dataset_is_snapshot(ds)) { 447681d9761SEric Taylor dmu_objset_disown(*osp, tag); 448681d9761SEric Taylor return (EROFS); 449fa9e4066Sahrens } 4503cb34c60Sahrens return (err); 451fa9e4066Sahrens } 452fa9e4066Sahrens 453fa9e4066Sahrens void 454503ad85cSMatthew Ahrens dmu_objset_rele(objset_t *os, void *tag) 455fa9e4066Sahrens { 456503ad85cSMatthew Ahrens dsl_dataset_rele(os->os_dsl_dataset, tag); 457503ad85cSMatthew Ahrens } 458503ad85cSMatthew Ahrens 459503ad85cSMatthew Ahrens void 460503ad85cSMatthew Ahrens dmu_objset_disown(objset_t *os, void *tag) 461503ad85cSMatthew Ahrens { 462503ad85cSMatthew Ahrens dsl_dataset_disown(os->os_dsl_dataset, tag); 463fa9e4066Sahrens } 464fa9e4066Sahrens 465436b2950Sperrin int 4661934e92fSmaybee dmu_objset_evict_dbufs(objset_t *os) 467ea8dc4b6Seschrock { 468ea8dc4b6Seschrock dnode_t *dn; 469c543ec06Sahrens 470503ad85cSMatthew Ahrens mutex_enter(&os->os_lock); 471c543ec06Sahrens 472c543ec06Sahrens /* process the mdn last, since the other dnodes have holds on it */ 473503ad85cSMatthew Ahrens list_remove(&os->os_dnodes, os->os_meta_dnode); 474503ad85cSMatthew Ahrens list_insert_tail(&os->os_dnodes, os->os_meta_dnode); 475ea8dc4b6Seschrock 476ea8dc4b6Seschrock /* 477c543ec06Sahrens * Find the first dnode with holds. We have to do this dance 478c543ec06Sahrens * because dnode_add_ref() only works if you already have a 479c543ec06Sahrens * hold. If there are no holds then it has no dbufs so OK to 480c543ec06Sahrens * skip. 481ea8dc4b6Seschrock */ 482503ad85cSMatthew Ahrens for (dn = list_head(&os->os_dnodes); 4831934e92fSmaybee dn && !dnode_add_ref(dn, FTAG); 484503ad85cSMatthew Ahrens dn = list_next(&os->os_dnodes, dn)) 485c543ec06Sahrens continue; 486c543ec06Sahrens 487c543ec06Sahrens while (dn) { 488c543ec06Sahrens dnode_t *next_dn = dn; 489c543ec06Sahrens 490c543ec06Sahrens do { 491503ad85cSMatthew Ahrens next_dn = list_next(&os->os_dnodes, next_dn); 4921934e92fSmaybee } while (next_dn && !dnode_add_ref(next_dn, FTAG)); 493c543ec06Sahrens 494503ad85cSMatthew Ahrens mutex_exit(&os->os_lock); 4951934e92fSmaybee dnode_evict_dbufs(dn); 496c543ec06Sahrens dnode_rele(dn, FTAG); 497503ad85cSMatthew Ahrens mutex_enter(&os->os_lock); 498c543ec06Sahrens dn = next_dn; 499ea8dc4b6Seschrock } 500503ad85cSMatthew Ahrens mutex_exit(&os->os_lock); 501503ad85cSMatthew Ahrens return (list_head(&os->os_dnodes) != os->os_meta_dnode); 502ea8dc4b6Seschrock } 503ea8dc4b6Seschrock 504fa9e4066Sahrens void 505503ad85cSMatthew Ahrens dmu_objset_evict(objset_t *os) 506fa9e4066Sahrens { 507503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 508fa9e4066Sahrens 509b24ab676SJeff Bonwick for (int t = 0; t < TXG_SIZE; t++) 510b24ab676SJeff Bonwick ASSERT(!dmu_objset_is_dirty(os, t)); 511fa9e4066Sahrens 5123baa08fcSek if (ds) { 5133baa08fcSek if (!dsl_dataset_is_snapshot(ds)) { 5143baa08fcSek VERIFY(0 == dsl_prop_unregister(ds, "checksum", 515503ad85cSMatthew Ahrens checksum_changed_cb, os)); 5163baa08fcSek VERIFY(0 == dsl_prop_unregister(ds, "compression", 517503ad85cSMatthew Ahrens compression_changed_cb, os)); 5183baa08fcSek VERIFY(0 == dsl_prop_unregister(ds, "copies", 519503ad85cSMatthew Ahrens copies_changed_cb, os)); 520b24ab676SJeff Bonwick VERIFY(0 == dsl_prop_unregister(ds, "dedup", 521b24ab676SJeff Bonwick dedup_changed_cb, os)); 522e09fa4daSNeil Perrin VERIFY(0 == dsl_prop_unregister(ds, "logbias", 523e09fa4daSNeil Perrin logbias_changed_cb, os)); 524*55da60b9SMark J Musante VERIFY(0 == dsl_prop_unregister(ds, "sync", 525*55da60b9SMark J Musante sync_changed_cb, os)); 5263baa08fcSek } 5273baa08fcSek VERIFY(0 == dsl_prop_unregister(ds, "primarycache", 528503ad85cSMatthew Ahrens primary_cache_changed_cb, os)); 5293baa08fcSek VERIFY(0 == dsl_prop_unregister(ds, "secondarycache", 530503ad85cSMatthew Ahrens secondary_cache_changed_cb, os)); 531fa9e4066Sahrens } 532fa9e4066Sahrens 5330a586ceaSMark Shellenbaum if (os->os_sa) 5340a586ceaSMark Shellenbaum sa_tear_down(os); 5350a586ceaSMark Shellenbaum 536ea8dc4b6Seschrock /* 537ea8dc4b6Seschrock * We should need only a single pass over the dnode list, since 538ea8dc4b6Seschrock * nothing can be added to the list at this point. 539ea8dc4b6Seschrock */ 540503ad85cSMatthew Ahrens (void) dmu_objset_evict_dbufs(os); 541ea8dc4b6Seschrock 542503ad85cSMatthew Ahrens dnode_special_close(os->os_meta_dnode); 543503ad85cSMatthew Ahrens if (os->os_userused_dnode) { 544503ad85cSMatthew Ahrens dnode_special_close(os->os_userused_dnode); 545503ad85cSMatthew Ahrens dnode_special_close(os->os_groupused_dnode); 54614843421SMatthew Ahrens } 547503ad85cSMatthew Ahrens zil_free(os->os_zil); 548fa9e4066Sahrens 549503ad85cSMatthew Ahrens ASSERT3P(list_head(&os->os_dnodes), ==, NULL); 55014843421SMatthew Ahrens 551503ad85cSMatthew Ahrens VERIFY(arc_buf_remove_ref(os->os_phys_buf, &os->os_phys_buf) == 1); 552503ad85cSMatthew Ahrens mutex_destroy(&os->os_lock); 553503ad85cSMatthew Ahrens mutex_destroy(&os->os_obj_lock); 554503ad85cSMatthew Ahrens mutex_destroy(&os->os_user_ptr_lock); 555503ad85cSMatthew Ahrens kmem_free(os, sizeof (objset_t)); 556fa9e4066Sahrens } 557fa9e4066Sahrens 55871eb0538SChris Kirby timestruc_t 55971eb0538SChris Kirby dmu_objset_snap_cmtime(objset_t *os) 56071eb0538SChris Kirby { 56171eb0538SChris Kirby return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir)); 56271eb0538SChris Kirby } 56371eb0538SChris Kirby 564fa9e4066Sahrens /* called from dsl for meta-objset */ 565503ad85cSMatthew Ahrens objset_t * 566c717a561Smaybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, 567c717a561Smaybee dmu_objset_type_t type, dmu_tx_t *tx) 568fa9e4066Sahrens { 569503ad85cSMatthew Ahrens objset_t *os; 570fa9e4066Sahrens dnode_t *mdn; 571fa9e4066Sahrens 572fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 57391ebeef5Sahrens if (ds) 57491ebeef5Sahrens mutex_enter(&ds->ds_opening_lock); 575503ad85cSMatthew Ahrens VERIFY(0 == dmu_objset_open_impl(spa, ds, bp, &os)); 57691ebeef5Sahrens if (ds) 57791ebeef5Sahrens mutex_exit(&ds->ds_opening_lock); 578503ad85cSMatthew Ahrens mdn = os->os_meta_dnode; 579fa9e4066Sahrens 580fa9e4066Sahrens dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT, 581fa9e4066Sahrens DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx); 582fa9e4066Sahrens 583fa9e4066Sahrens /* 584fa9e4066Sahrens * We don't want to have to increase the meta-dnode's nlevels 585fa9e4066Sahrens * later, because then we could do it in quescing context while 586fa9e4066Sahrens * we are also accessing it in open context. 587fa9e4066Sahrens * 588fa9e4066Sahrens * This precaution is not necessary for the MOS (ds == NULL), 589fa9e4066Sahrens * because the MOS is only updated in syncing context. 590fa9e4066Sahrens * This is most fortunate: the MOS is the only objset that 591fa9e4066Sahrens * needs to be synced multiple times as spa_sync() iterates 592fa9e4066Sahrens * to convergence, so minimizing its dn_nlevels matters. 593fa9e4066Sahrens */ 594ea8dc4b6Seschrock if (ds != NULL) { 595ea8dc4b6Seschrock int levels = 1; 596ea8dc4b6Seschrock 597ea8dc4b6Seschrock /* 598ea8dc4b6Seschrock * Determine the number of levels necessary for the meta-dnode 599ea8dc4b6Seschrock * to contain DN_MAX_OBJECT dnodes. 600ea8dc4b6Seschrock */ 601ea8dc4b6Seschrock while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift + 602ea8dc4b6Seschrock (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) < 603ea8dc4b6Seschrock DN_MAX_OBJECT * sizeof (dnode_phys_t)) 604ea8dc4b6Seschrock levels++; 605ea8dc4b6Seschrock 606fa9e4066Sahrens mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] = 607ea8dc4b6Seschrock mdn->dn_nlevels = levels; 608ea8dc4b6Seschrock } 609fa9e4066Sahrens 610fa9e4066Sahrens ASSERT(type != DMU_OST_NONE); 611fa9e4066Sahrens ASSERT(type != DMU_OST_ANY); 612fa9e4066Sahrens ASSERT(type < DMU_OST_NUMTYPES); 613503ad85cSMatthew Ahrens os->os_phys->os_type = type; 614503ad85cSMatthew Ahrens if (dmu_objset_userused_enabled(os)) { 615503ad85cSMatthew Ahrens os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE; 616503ad85cSMatthew Ahrens os->os_flags = os->os_phys->os_flags; 61714843421SMatthew Ahrens } 618fa9e4066Sahrens 619fa9e4066Sahrens dsl_dataset_dirty(ds, tx); 620fa9e4066Sahrens 621503ad85cSMatthew Ahrens return (os); 622fa9e4066Sahrens } 623fa9e4066Sahrens 624fa9e4066Sahrens struct oscarg { 625ecd6cf80Smarks void (*userfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx); 626fa9e4066Sahrens void *userarg; 627ae46e4c7SMatthew Ahrens dsl_dataset_t *clone_origin; 628fa9e4066Sahrens const char *lastname; 629fa9e4066Sahrens dmu_objset_type_t type; 630ab04eb8eStimh uint64_t flags; 631fa9e4066Sahrens }; 632fa9e4066Sahrens 633ecd6cf80Smarks /*ARGSUSED*/ 634fa9e4066Sahrens static int 6351d452cf5Sahrens dmu_objset_create_check(void *arg1, void *arg2, dmu_tx_t *tx) 636fa9e4066Sahrens { 6371d452cf5Sahrens dsl_dir_t *dd = arg1; 6381d452cf5Sahrens struct oscarg *oa = arg2; 6391d452cf5Sahrens objset_t *mos = dd->dd_pool->dp_meta_objset; 640fa9e4066Sahrens int err; 6411d452cf5Sahrens uint64_t ddobj; 6421d452cf5Sahrens 6431d452cf5Sahrens err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj, 6441d452cf5Sahrens oa->lastname, sizeof (uint64_t), 1, &ddobj); 6451d452cf5Sahrens if (err != ENOENT) 6461d452cf5Sahrens return (err ? err : EEXIST); 6471d452cf5Sahrens 648ae46e4c7SMatthew Ahrens if (oa->clone_origin != NULL) { 649ae46e4c7SMatthew Ahrens /* You can't clone across pools. */ 650ae46e4c7SMatthew Ahrens if (oa->clone_origin->ds_dir->dd_pool != dd->dd_pool) 6511d452cf5Sahrens return (EXDEV); 6521d452cf5Sahrens 653ae46e4c7SMatthew Ahrens /* You can only clone snapshots, not the head datasets. */ 654ae46e4c7SMatthew Ahrens if (!dsl_dataset_is_snapshot(oa->clone_origin)) 6551d452cf5Sahrens return (EINVAL); 6561d452cf5Sahrens } 657ecd6cf80Smarks 6581d452cf5Sahrens return (0); 6591d452cf5Sahrens } 6601d452cf5Sahrens 6611d452cf5Sahrens static void 662ecd6cf80Smarks dmu_objset_create_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 6631d452cf5Sahrens { 6641d452cf5Sahrens dsl_dir_t *dd = arg1; 6651d452cf5Sahrens struct oscarg *oa = arg2; 6661d452cf5Sahrens uint64_t dsobj; 667fa9e4066Sahrens 668fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 669fa9e4066Sahrens 6701d452cf5Sahrens dsobj = dsl_dataset_create_sync(dd, oa->lastname, 671ae46e4c7SMatthew Ahrens oa->clone_origin, oa->flags, cr, tx); 672fa9e4066Sahrens 673ae46e4c7SMatthew Ahrens if (oa->clone_origin == NULL) { 674ae46e4c7SMatthew Ahrens dsl_dataset_t *ds; 675ae46e4c7SMatthew Ahrens blkptr_t *bp; 676503ad85cSMatthew Ahrens objset_t *os; 677fa9e4066Sahrens 678ae46e4c7SMatthew Ahrens VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, dsobj, 679ae46e4c7SMatthew Ahrens FTAG, &ds)); 680ae46e4c7SMatthew Ahrens bp = dsl_dataset_get_blkptr(ds); 681ae46e4c7SMatthew Ahrens ASSERT(BP_IS_HOLE(bp)); 682ae46e4c7SMatthew Ahrens 683503ad85cSMatthew Ahrens os = dmu_objset_create_impl(dsl_dataset_get_spa(ds), 684c717a561Smaybee ds, bp, oa->type, tx); 685fa9e4066Sahrens 686fa9e4066Sahrens if (oa->userfunc) 687503ad85cSMatthew Ahrens oa->userfunc(os, oa->userarg, cr, tx); 688ae46e4c7SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 689fa9e4066Sahrens } 690ecd6cf80Smarks 691ecd6cf80Smarks spa_history_internal_log(LOG_DS_CREATE, dd->dd_pool->dp_spa, 692ecd6cf80Smarks tx, cr, "dataset = %llu", dsobj); 693fa9e4066Sahrens } 694fa9e4066Sahrens 695fa9e4066Sahrens int 696ae46e4c7SMatthew Ahrens dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags, 697ecd6cf80Smarks void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg) 698fa9e4066Sahrens { 6991d452cf5Sahrens dsl_dir_t *pdd; 700fa9e4066Sahrens const char *tail; 701fa9e4066Sahrens int err = 0; 7021d452cf5Sahrens struct oscarg oa = { 0 }; 703fa9e4066Sahrens 7041d452cf5Sahrens ASSERT(strchr(name, '@') == NULL); 7051d452cf5Sahrens err = dsl_dir_open(name, FTAG, &pdd, &tail); 706ea8dc4b6Seschrock if (err) 707ea8dc4b6Seschrock return (err); 708fa9e4066Sahrens if (tail == NULL) { 7091d452cf5Sahrens dsl_dir_close(pdd, FTAG); 710fa9e4066Sahrens return (EEXIST); 711fa9e4066Sahrens } 712fa9e4066Sahrens 7131d452cf5Sahrens oa.userfunc = func; 7141d452cf5Sahrens oa.userarg = arg; 7151d452cf5Sahrens oa.lastname = tail; 7161d452cf5Sahrens oa.type = type; 717ab04eb8eStimh oa.flags = flags; 718ecd6cf80Smarks 719ae46e4c7SMatthew Ahrens err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check, 720ae46e4c7SMatthew Ahrens dmu_objset_create_sync, pdd, &oa, 5); 721ae46e4c7SMatthew Ahrens dsl_dir_close(pdd, FTAG); 722ae46e4c7SMatthew Ahrens return (err); 723ae46e4c7SMatthew Ahrens } 724ae46e4c7SMatthew Ahrens 725ae46e4c7SMatthew Ahrens int 726ae46e4c7SMatthew Ahrens dmu_objset_clone(const char *name, dsl_dataset_t *clone_origin, uint64_t flags) 727ae46e4c7SMatthew Ahrens { 728ae46e4c7SMatthew Ahrens dsl_dir_t *pdd; 729ae46e4c7SMatthew Ahrens const char *tail; 730ae46e4c7SMatthew Ahrens int err = 0; 731ae46e4c7SMatthew Ahrens struct oscarg oa = { 0 }; 732ae46e4c7SMatthew Ahrens 733ae46e4c7SMatthew Ahrens ASSERT(strchr(name, '@') == NULL); 734ae46e4c7SMatthew Ahrens err = dsl_dir_open(name, FTAG, &pdd, &tail); 735ae46e4c7SMatthew Ahrens if (err) 736ae46e4c7SMatthew Ahrens return (err); 737ae46e4c7SMatthew Ahrens if (tail == NULL) { 738ae46e4c7SMatthew Ahrens dsl_dir_close(pdd, FTAG); 739ae46e4c7SMatthew Ahrens return (EEXIST); 740fa9e4066Sahrens } 741ae46e4c7SMatthew Ahrens 742ae46e4c7SMatthew Ahrens oa.lastname = tail; 743ae46e4c7SMatthew Ahrens oa.clone_origin = clone_origin; 744ae46e4c7SMatthew Ahrens oa.flags = flags; 745ae46e4c7SMatthew Ahrens 7461d452cf5Sahrens err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check, 7471d452cf5Sahrens dmu_objset_create_sync, pdd, &oa, 5); 7481d452cf5Sahrens dsl_dir_close(pdd, FTAG); 749fa9e4066Sahrens return (err); 750fa9e4066Sahrens } 751fa9e4066Sahrens 752fa9e4066Sahrens int 753842727c2SChris Kirby dmu_objset_destroy(const char *name, boolean_t defer) 754fa9e4066Sahrens { 755503ad85cSMatthew Ahrens dsl_dataset_t *ds; 756fa9e4066Sahrens int error; 757fa9e4066Sahrens 758fa9e4066Sahrens /* 759ae46e4c7SMatthew Ahrens * dsl_dataset_destroy() can free any claimed-but-unplayed 760ae46e4c7SMatthew Ahrens * intent log, but if there is an active log, it has blocks that 761ae46e4c7SMatthew Ahrens * are allocated, but may not yet be reflected in the on-disk 762ae46e4c7SMatthew Ahrens * structure. Only the ZIL knows how to free them, so we have 763ae46e4c7SMatthew Ahrens * to call into it here. 764fa9e4066Sahrens */ 765503ad85cSMatthew Ahrens error = dsl_dataset_own(name, B_TRUE, FTAG, &ds); 766fa9e4066Sahrens if (error == 0) { 767503ad85cSMatthew Ahrens objset_t *os; 768503ad85cSMatthew Ahrens if (dmu_objset_from_ds(ds, &os) == 0) 769503ad85cSMatthew Ahrens zil_destroy(dmu_objset_zil(os), B_FALSE); 770503ad85cSMatthew Ahrens error = dsl_dataset_destroy(ds, FTAG, defer); 771ae46e4c7SMatthew Ahrens /* dsl_dataset_destroy() closes the ds. */ 772fa9e4066Sahrens } 773fa9e4066Sahrens 7743cb34c60Sahrens return (error); 775fa9e4066Sahrens } 776fa9e4066Sahrens 7771d452cf5Sahrens struct snaparg { 7781d452cf5Sahrens dsl_sync_task_group_t *dstg; 7791d452cf5Sahrens char *snapname; 7801d452cf5Sahrens char failed[MAXPATHLEN]; 781d8d780fbSMatthew Ahrens boolean_t recursive; 782ea2f5b9eSMatthew Ahrens nvlist_t *props; 7833cb34c60Sahrens }; 7843cb34c60Sahrens 785ea2f5b9eSMatthew Ahrens static int 786ea2f5b9eSMatthew Ahrens snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx) 787ea2f5b9eSMatthew Ahrens { 788ea2f5b9eSMatthew Ahrens objset_t *os = arg1; 789ea2f5b9eSMatthew Ahrens struct snaparg *sn = arg2; 790ea2f5b9eSMatthew Ahrens 791ea2f5b9eSMatthew Ahrens /* The props have already been checked by zfs_check_userprops(). */ 792ea2f5b9eSMatthew Ahrens 793503ad85cSMatthew Ahrens return (dsl_dataset_snapshot_check(os->os_dsl_dataset, 794ea2f5b9eSMatthew Ahrens sn->snapname, tx)); 795ea2f5b9eSMatthew Ahrens } 796ea2f5b9eSMatthew Ahrens 797ea2f5b9eSMatthew Ahrens static void 798ea2f5b9eSMatthew Ahrens snapshot_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 799ea2f5b9eSMatthew Ahrens { 800ea2f5b9eSMatthew Ahrens objset_t *os = arg1; 801503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 802ea2f5b9eSMatthew Ahrens struct snaparg *sn = arg2; 803ea2f5b9eSMatthew Ahrens 804ea2f5b9eSMatthew Ahrens dsl_dataset_snapshot_sync(ds, sn->snapname, cr, tx); 805ea2f5b9eSMatthew Ahrens 80692241e0bSTom Erickson if (sn->props) { 80792241e0bSTom Erickson dsl_props_arg_t pa; 80892241e0bSTom Erickson pa.pa_props = sn->props; 80992241e0bSTom Erickson pa.pa_source = ZPROP_SRC_LOCAL; 81092241e0bSTom Erickson dsl_props_set_sync(ds->ds_prev, &pa, cr, tx); 81192241e0bSTom Erickson } 812ea2f5b9eSMatthew Ahrens } 8131d452cf5Sahrens 8141d452cf5Sahrens static int 815fd136879SMatthew Ahrens dmu_objset_snapshot_one(const char *name, void *arg) 8161d452cf5Sahrens { 8171d452cf5Sahrens struct snaparg *sn = arg; 8181d452cf5Sahrens objset_t *os; 8191d452cf5Sahrens int err; 820d8d780fbSMatthew Ahrens char *cp; 821d8d780fbSMatthew Ahrens 822d8d780fbSMatthew Ahrens /* 823d8d780fbSMatthew Ahrens * If the objset starts with a '%', then ignore it unless it was 824d8d780fbSMatthew Ahrens * explicitly named (ie, not recursive). These hidden datasets 825d8d780fbSMatthew Ahrens * are always inconsistent, and by not opening them here, we can 826d8d780fbSMatthew Ahrens * avoid a race with dsl_dir_destroy_check(). 827d8d780fbSMatthew Ahrens */ 828d8d780fbSMatthew Ahrens cp = strrchr(name, '/'); 829d8d780fbSMatthew Ahrens if (cp && cp[1] == '%' && sn->recursive) 830d8d780fbSMatthew Ahrens return (0); 8311d452cf5Sahrens 8321d452cf5Sahrens (void) strcpy(sn->failed, name); 8331d452cf5Sahrens 834ecd6cf80Smarks /* 835d8d780fbSMatthew Ahrens * Check permissions if we are doing a recursive snapshot. The 836d8d780fbSMatthew Ahrens * permission checks for the starting dataset have already been 837d8d780fbSMatthew Ahrens * performed in zfs_secpolicy_snapshot() 838ecd6cf80Smarks */ 839d8d780fbSMatthew Ahrens if (sn->recursive && (err = zfs_secpolicy_snapshot_perms(name, CRED()))) 840ecd6cf80Smarks return (err); 841ecd6cf80Smarks 842503ad85cSMatthew Ahrens err = dmu_objset_hold(name, sn, &os); 8431d452cf5Sahrens if (err != 0) 8441d452cf5Sahrens return (err); 8451d452cf5Sahrens 846d8d780fbSMatthew Ahrens /* 847d8d780fbSMatthew Ahrens * If the objset is in an inconsistent state (eg, in the process 848d8d780fbSMatthew Ahrens * of being destroyed), don't snapshot it. As with %hidden 849d8d780fbSMatthew Ahrens * datasets, we return EBUSY if this name was explicitly 850d8d780fbSMatthew Ahrens * requested (ie, not recursive), and otherwise ignore it. 851d8d780fbSMatthew Ahrens */ 852503ad85cSMatthew Ahrens if (os->os_dsl_dataset->ds_phys->ds_flags & DS_FLAG_INCONSISTENT) { 853503ad85cSMatthew Ahrens dmu_objset_rele(os, sn); 854d8d780fbSMatthew Ahrens return (sn->recursive ? 0 : EBUSY); 855f2e10be3Srm } 856f2e10be3Srm 8571d452cf5Sahrens /* 8581d452cf5Sahrens * NB: we need to wait for all in-flight changes to get to disk, 8591d452cf5Sahrens * so that we snapshot those changes. zil_suspend does this as 8601d452cf5Sahrens * a side effect. 8611d452cf5Sahrens */ 8621d452cf5Sahrens err = zil_suspend(dmu_objset_zil(os)); 8631d452cf5Sahrens if (err == 0) { 864ea2f5b9eSMatthew Ahrens dsl_sync_task_create(sn->dstg, snapshot_check, 865ea2f5b9eSMatthew Ahrens snapshot_sync, os, sn, 3); 866f2e10be3Srm } else { 867503ad85cSMatthew Ahrens dmu_objset_rele(os, sn); 8681d452cf5Sahrens } 869f2e10be3Srm 8701d452cf5Sahrens return (err); 8711d452cf5Sahrens } 8721d452cf5Sahrens 8731d452cf5Sahrens int 874ea2f5b9eSMatthew Ahrens dmu_objset_snapshot(char *fsname, char *snapname, 875ea2f5b9eSMatthew Ahrens nvlist_t *props, boolean_t recursive) 8761d452cf5Sahrens { 8771d452cf5Sahrens dsl_sync_task_t *dst; 878ea2f5b9eSMatthew Ahrens struct snaparg sn; 8791d452cf5Sahrens spa_t *spa; 8801d452cf5Sahrens int err; 8811d452cf5Sahrens 8821d452cf5Sahrens (void) strcpy(sn.failed, fsname); 8831d452cf5Sahrens 88440feaa91Sahrens err = spa_open(fsname, &spa, FTAG); 8851d452cf5Sahrens if (err) 8861d452cf5Sahrens return (err); 8871d452cf5Sahrens 8881d452cf5Sahrens sn.dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 8891d452cf5Sahrens sn.snapname = snapname; 890ea2f5b9eSMatthew Ahrens sn.props = props; 891d8d780fbSMatthew Ahrens sn.recursive = recursive; 8921d452cf5Sahrens 8930b69c2f0Sahrens if (recursive) { 8940b69c2f0Sahrens err = dmu_objset_find(fsname, 8950b69c2f0Sahrens dmu_objset_snapshot_one, &sn, DS_FIND_CHILDREN); 8960b69c2f0Sahrens } else { 8971d452cf5Sahrens err = dmu_objset_snapshot_one(fsname, &sn); 8980b69c2f0Sahrens } 8991d452cf5Sahrens 900ea2f5b9eSMatthew Ahrens if (err == 0) 901ea2f5b9eSMatthew Ahrens err = dsl_sync_task_group_wait(sn.dstg); 9021d452cf5Sahrens 9031d452cf5Sahrens for (dst = list_head(&sn.dstg->dstg_tasks); dst; 9041d452cf5Sahrens dst = list_next(&sn.dstg->dstg_tasks, dst)) { 905ea2f5b9eSMatthew Ahrens objset_t *os = dst->dst_arg1; 906503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 9071d452cf5Sahrens if (dst->dst_err) 9083cb34c60Sahrens dsl_dataset_name(ds, sn.failed); 909ea2f5b9eSMatthew Ahrens zil_resume(dmu_objset_zil(os)); 910503ad85cSMatthew Ahrens dmu_objset_rele(os, &sn); 9113cb34c60Sahrens } 9123cb34c60Sahrens 9131d452cf5Sahrens if (err) 9141d452cf5Sahrens (void) strcpy(fsname, sn.failed); 9151d452cf5Sahrens dsl_sync_task_group_destroy(sn.dstg); 9161d452cf5Sahrens spa_close(spa, FTAG); 9171d452cf5Sahrens return (err); 9181d452cf5Sahrens } 9191d452cf5Sahrens 920fa9e4066Sahrens static void 92114843421SMatthew Ahrens dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx) 922fa9e4066Sahrens { 923c717a561Smaybee dnode_t *dn; 924faafa6e3Sahrens 925c717a561Smaybee while (dn = list_head(list)) { 926c717a561Smaybee ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT); 927c717a561Smaybee ASSERT(dn->dn_dbuf->db_data_pending); 928c717a561Smaybee /* 92914843421SMatthew Ahrens * Initialize dn_zio outside dnode_sync() because the 93014843421SMatthew Ahrens * meta-dnode needs to set it ouside dnode_sync(). 931c717a561Smaybee */ 932c717a561Smaybee dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio; 933c717a561Smaybee ASSERT(dn->dn_zio); 934faafa6e3Sahrens 935c717a561Smaybee ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS); 936c717a561Smaybee list_remove(list, dn); 93714843421SMatthew Ahrens 93814843421SMatthew Ahrens if (newlist) { 93914843421SMatthew Ahrens (void) dnode_add_ref(dn, newlist); 94014843421SMatthew Ahrens list_insert_tail(newlist, dn); 94114843421SMatthew Ahrens } 94214843421SMatthew Ahrens 943c717a561Smaybee dnode_sync(dn, tx); 944fa9e4066Sahrens } 945fa9e4066Sahrens } 946fa9e4066Sahrens 947fa9e4066Sahrens /* ARGSUSED */ 948fa9e4066Sahrens static void 949b24ab676SJeff Bonwick dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg) 950fa9e4066Sahrens { 951e14bb325SJeff Bonwick blkptr_t *bp = zio->io_bp; 952503ad85cSMatthew Ahrens objset_t *os = arg; 953c717a561Smaybee dnode_phys_t *dnp = &os->os_phys->os_meta_dnode; 954fa9e4066Sahrens 955e14bb325SJeff Bonwick ASSERT(bp == os->os_rootbp); 956e14bb325SJeff Bonwick ASSERT(BP_GET_TYPE(bp) == DMU_OT_OBJSET); 957e14bb325SJeff Bonwick ASSERT(BP_GET_LEVEL(bp) == 0); 9580a4e9518Sgw 959fa9e4066Sahrens /* 96014843421SMatthew Ahrens * Update rootbp fill count: it should be the number of objects 96114843421SMatthew Ahrens * allocated in the object set (not counting the "special" 96214843421SMatthew Ahrens * objects that are stored in the objset_phys_t -- the meta 96314843421SMatthew Ahrens * dnode and user/group accounting objects). 964fa9e4066Sahrens */ 96514843421SMatthew Ahrens bp->blk_fill = 0; 966e14bb325SJeff Bonwick for (int i = 0; i < dnp->dn_nblkptr; i++) 967c717a561Smaybee bp->blk_fill += dnp->dn_blkptr[i].blk_fill; 968b24ab676SJeff Bonwick } 969b24ab676SJeff Bonwick 970b24ab676SJeff Bonwick /* ARGSUSED */ 971b24ab676SJeff Bonwick static void 972b24ab676SJeff Bonwick dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg) 973b24ab676SJeff Bonwick { 974b24ab676SJeff Bonwick blkptr_t *bp = zio->io_bp; 975b24ab676SJeff Bonwick blkptr_t *bp_orig = &zio->io_bp_orig; 976b24ab676SJeff Bonwick objset_t *os = arg; 9770a4e9518Sgw 978e14bb325SJeff Bonwick if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { 979b24ab676SJeff Bonwick ASSERT(BP_EQUAL(bp, bp_orig)); 980e14bb325SJeff Bonwick } else { 981b24ab676SJeff Bonwick dsl_dataset_t *ds = os->os_dsl_dataset; 982b24ab676SJeff Bonwick dmu_tx_t *tx = os->os_synctx; 983b24ab676SJeff Bonwick 984b24ab676SJeff Bonwick (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE); 985b24ab676SJeff Bonwick dsl_dataset_block_born(ds, bp, tx); 9860a4e9518Sgw } 987c717a561Smaybee } 988c717a561Smaybee 989fa9e4066Sahrens /* called from dsl */ 990fa9e4066Sahrens void 991503ad85cSMatthew Ahrens dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx) 992fa9e4066Sahrens { 993fa9e4066Sahrens int txgoff; 994ea8dc4b6Seschrock zbookmark_t zb; 995b24ab676SJeff Bonwick zio_prop_t zp; 996c717a561Smaybee zio_t *zio; 997c717a561Smaybee list_t *list; 99814843421SMatthew Ahrens list_t *newlist = NULL; 999c717a561Smaybee dbuf_dirty_record_t *dr; 1000c717a561Smaybee 1001c717a561Smaybee dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg); 1002fa9e4066Sahrens 1003fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 1004fa9e4066Sahrens /* XXX the write_done callback should really give us the tx... */ 1005fa9e4066Sahrens os->os_synctx = tx; 1006fa9e4066Sahrens 100787bd5c1eSahrens if (os->os_dsl_dataset == NULL) { 100887bd5c1eSahrens /* 100987bd5c1eSahrens * This is the MOS. If we have upgraded, 101087bd5c1eSahrens * spa_max_replication() could change, so reset 101187bd5c1eSahrens * os_copies here. 101287bd5c1eSahrens */ 101387bd5c1eSahrens os->os_copies = spa_max_replication(os->os_spa); 101487bd5c1eSahrens } 101587bd5c1eSahrens 1016fa9e4066Sahrens /* 1017c717a561Smaybee * Create the root block IO 1018fa9e4066Sahrens */ 1019088f3894Sahrens arc_release(os->os_phys_buf, &os->os_phys_buf); 102014843421SMatthew Ahrens 1021b24ab676SJeff Bonwick SET_BOOKMARK(&zb, os->os_dsl_dataset ? 1022b24ab676SJeff Bonwick os->os_dsl_dataset->ds_object : DMU_META_OBJSET, 1023b24ab676SJeff Bonwick ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 1024b24ab676SJeff Bonwick 1025b24ab676SJeff Bonwick dmu_write_policy(os, NULL, 0, 0, &zp); 1026b24ab676SJeff Bonwick 1027b24ab676SJeff Bonwick zio = arc_write(pio, os->os_spa, tx->tx_txg, 1028b24ab676SJeff Bonwick os->os_rootbp, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os), &zp, 1029b24ab676SJeff Bonwick dmu_objset_write_ready, dmu_objset_write_done, os, 1030e14bb325SJeff Bonwick ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb); 1031c717a561Smaybee 1032c717a561Smaybee /* 103314843421SMatthew Ahrens * Sync special dnodes - the parent IO for the sync is the root block 1034c717a561Smaybee */ 1035c717a561Smaybee os->os_meta_dnode->dn_zio = zio; 1036c717a561Smaybee dnode_sync(os->os_meta_dnode, tx); 1037c717a561Smaybee 103814843421SMatthew Ahrens os->os_phys->os_flags = os->os_flags; 103914843421SMatthew Ahrens 104014843421SMatthew Ahrens if (os->os_userused_dnode && 104114843421SMatthew Ahrens os->os_userused_dnode->dn_type != DMU_OT_NONE) { 104214843421SMatthew Ahrens os->os_userused_dnode->dn_zio = zio; 104314843421SMatthew Ahrens dnode_sync(os->os_userused_dnode, tx); 104414843421SMatthew Ahrens os->os_groupused_dnode->dn_zio = zio; 104514843421SMatthew Ahrens dnode_sync(os->os_groupused_dnode, tx); 104614843421SMatthew Ahrens } 104714843421SMatthew Ahrens 1048c717a561Smaybee txgoff = tx->tx_txg & TXG_MASK; 1049fa9e4066Sahrens 105014843421SMatthew Ahrens if (dmu_objset_userused_enabled(os)) { 105114843421SMatthew Ahrens newlist = &os->os_synced_dnodes; 105214843421SMatthew Ahrens /* 105314843421SMatthew Ahrens * We must create the list here because it uses the 105414843421SMatthew Ahrens * dn_dirty_link[] of this txg. 105514843421SMatthew Ahrens */ 105614843421SMatthew Ahrens list_create(newlist, sizeof (dnode_t), 105714843421SMatthew Ahrens offsetof(dnode_t, dn_dirty_link[txgoff])); 105814843421SMatthew Ahrens } 105914843421SMatthew Ahrens 106014843421SMatthew Ahrens dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx); 106114843421SMatthew Ahrens dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx); 1062fa9e4066Sahrens 1063c717a561Smaybee list = &os->os_meta_dnode->dn_dirty_records[txgoff]; 1064c717a561Smaybee while (dr = list_head(list)) { 1065c717a561Smaybee ASSERT(dr->dr_dbuf->db_level == 0); 1066c717a561Smaybee list_remove(list, dr); 1067c717a561Smaybee if (dr->dr_zio) 1068c717a561Smaybee zio_nowait(dr->dr_zio); 1069c717a561Smaybee } 1070c717a561Smaybee /* 1071c717a561Smaybee * Free intent log blocks up to this tx. 1072c717a561Smaybee */ 1073c717a561Smaybee zil_sync(os->os_zil, tx); 1074088f3894Sahrens os->os_phys->os_zil_header = os->os_zil_header; 1075c717a561Smaybee zio_nowait(zio); 1076fa9e4066Sahrens } 1077fa9e4066Sahrens 1078b24ab676SJeff Bonwick boolean_t 1079b24ab676SJeff Bonwick dmu_objset_is_dirty(objset_t *os, uint64_t txg) 1080b24ab676SJeff Bonwick { 1081b24ab676SJeff Bonwick return (!list_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]) || 1082b24ab676SJeff Bonwick !list_is_empty(&os->os_free_dnodes[txg & TXG_MASK])); 1083b24ab676SJeff Bonwick } 1084b24ab676SJeff Bonwick 108514843421SMatthew Ahrens static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES]; 108614843421SMatthew Ahrens 108714843421SMatthew Ahrens void 108814843421SMatthew Ahrens dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb) 108914843421SMatthew Ahrens { 109014843421SMatthew Ahrens used_cbs[ost] = cb; 109114843421SMatthew Ahrens } 109214843421SMatthew Ahrens 109314843421SMatthew Ahrens boolean_t 1094503ad85cSMatthew Ahrens dmu_objset_userused_enabled(objset_t *os) 109514843421SMatthew Ahrens { 109614843421SMatthew Ahrens return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE && 109714843421SMatthew Ahrens used_cbs[os->os_phys->os_type] && 109814843421SMatthew Ahrens os->os_userused_dnode); 109914843421SMatthew Ahrens } 110014843421SMatthew Ahrens 11019966ca11SMatthew Ahrens static void 11020a586ceaSMark Shellenbaum do_userquota_update(objset_t *os, uint64_t used, uint64_t flags, 11030a586ceaSMark Shellenbaum uint64_t user, uint64_t group, boolean_t subtract, dmu_tx_t *tx) 11049966ca11SMatthew Ahrens { 11050a586ceaSMark Shellenbaum if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) { 11060a586ceaSMark Shellenbaum int64_t delta = DNODE_SIZE + used; 11079966ca11SMatthew Ahrens if (subtract) 11089966ca11SMatthew Ahrens delta = -delta; 1109c33e334fSMatthew Ahrens VERIFY3U(0, ==, zap_increment_int(os, DMU_USERUSED_OBJECT, 11109966ca11SMatthew Ahrens user, delta, tx)); 1111c33e334fSMatthew Ahrens VERIFY3U(0, ==, zap_increment_int(os, DMU_GROUPUSED_OBJECT, 11129966ca11SMatthew Ahrens group, delta, tx)); 11139966ca11SMatthew Ahrens } 11149966ca11SMatthew Ahrens } 11159966ca11SMatthew Ahrens 111614843421SMatthew Ahrens void 11170a586ceaSMark Shellenbaum dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx) 111814843421SMatthew Ahrens { 111914843421SMatthew Ahrens dnode_t *dn; 112014843421SMatthew Ahrens list_t *list = &os->os_synced_dnodes; 112114843421SMatthew Ahrens 112214843421SMatthew Ahrens ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os)); 112314843421SMatthew Ahrens 112414843421SMatthew Ahrens while (dn = list_head(list)) { 112514843421SMatthew Ahrens ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object)); 112614843421SMatthew Ahrens ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE || 112714843421SMatthew Ahrens dn->dn_phys->dn_flags & 112814843421SMatthew Ahrens DNODE_FLAG_USERUSED_ACCOUNTED); 112914843421SMatthew Ahrens 113014843421SMatthew Ahrens /* Allocate the user/groupused objects if necessary. */ 113114843421SMatthew Ahrens if (os->os_userused_dnode->dn_type == DMU_OT_NONE) { 1132503ad85cSMatthew Ahrens VERIFY(0 == zap_create_claim(os, 113314843421SMatthew Ahrens DMU_USERUSED_OBJECT, 113414843421SMatthew Ahrens DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx)); 1135503ad85cSMatthew Ahrens VERIFY(0 == zap_create_claim(os, 113614843421SMatthew Ahrens DMU_GROUPUSED_OBJECT, 113714843421SMatthew Ahrens DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx)); 113814843421SMatthew Ahrens } 113914843421SMatthew Ahrens 114014843421SMatthew Ahrens /* 11419966ca11SMatthew Ahrens * We intentionally modify the zap object even if the 11420a586ceaSMark Shellenbaum * net delta is zero. Otherwise 11439966ca11SMatthew Ahrens * the block of the zap obj could be shared between 11449966ca11SMatthew Ahrens * datasets but need to be different between them after 11459966ca11SMatthew Ahrens * a bprewrite. 114614843421SMatthew Ahrens */ 114714843421SMatthew Ahrens 114814843421SMatthew Ahrens /* 114914843421SMatthew Ahrens * The mutex is needed here for interlock with dnode_allocate. 115014843421SMatthew Ahrens */ 115114843421SMatthew Ahrens mutex_enter(&dn->dn_mtx); 11520a586ceaSMark Shellenbaum ASSERT(dn->dn_id_flags); 11530a586ceaSMark Shellenbaum if (dn->dn_id_flags & DN_ID_OLD_EXIST) { 11540a586ceaSMark Shellenbaum do_userquota_update(os, dn->dn_oldused, dn->dn_oldflags, 11550a586ceaSMark Shellenbaum dn->dn_olduid, dn->dn_oldgid, B_TRUE, tx); 11560a586ceaSMark Shellenbaum } 11570a586ceaSMark Shellenbaum if (dn->dn_id_flags & DN_ID_NEW_EXIST) { 11580a586ceaSMark Shellenbaum do_userquota_update(os, DN_USED_BYTES(dn->dn_phys), 11590a586ceaSMark Shellenbaum dn->dn_phys->dn_flags, dn->dn_newuid, 11600a586ceaSMark Shellenbaum dn->dn_newgid, B_FALSE, tx); 11610a586ceaSMark Shellenbaum } 11620a586ceaSMark Shellenbaum 11630a586ceaSMark Shellenbaum dn->dn_oldused = 0; 11640a586ceaSMark Shellenbaum dn->dn_oldflags = 0; 11650a586ceaSMark Shellenbaum if (dn->dn_id_flags & DN_ID_NEW_EXIST) { 11660a586ceaSMark Shellenbaum dn->dn_olduid = dn->dn_newuid; 11670a586ceaSMark Shellenbaum dn->dn_oldgid = dn->dn_newgid; 11680a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_OLD_EXIST; 11690a586ceaSMark Shellenbaum if (dn->dn_bonuslen == 0) 11700a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_SPILL; 11710a586ceaSMark Shellenbaum else 11720a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_BONUS; 11730a586ceaSMark Shellenbaum } 11740a586ceaSMark Shellenbaum dn->dn_id_flags &= ~(DN_ID_NEW_EXIST|DN_ID_SYNC); 117514843421SMatthew Ahrens mutex_exit(&dn->dn_mtx); 117614843421SMatthew Ahrens 117714843421SMatthew Ahrens list_remove(list, dn); 117814843421SMatthew Ahrens dnode_rele(dn, list); 117914843421SMatthew Ahrens } 118014843421SMatthew Ahrens } 118114843421SMatthew Ahrens 118206e0070dSMark Shellenbaum /* 118306e0070dSMark Shellenbaum * Returns a pointer to data to find uid/gid from 118406e0070dSMark Shellenbaum * 118506e0070dSMark Shellenbaum * If a dirty record for transaction group that is syncing can't 118606e0070dSMark Shellenbaum * be found then NULL is returned. In the NULL case it is assumed 118706e0070dSMark Shellenbaum * the uid/gid aren't changing. 118806e0070dSMark Shellenbaum */ 118906e0070dSMark Shellenbaum static void * 119006e0070dSMark Shellenbaum dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx) 119106e0070dSMark Shellenbaum { 119206e0070dSMark Shellenbaum dbuf_dirty_record_t *dr, **drp; 119306e0070dSMark Shellenbaum void *data; 119406e0070dSMark Shellenbaum 119506e0070dSMark Shellenbaum if (db->db_dirtycnt == 0) 119606e0070dSMark Shellenbaum return (db->db.db_data); /* Nothing is changing */ 119706e0070dSMark Shellenbaum 119806e0070dSMark Shellenbaum for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next) 119906e0070dSMark Shellenbaum if (dr->dr_txg == tx->tx_txg) 120006e0070dSMark Shellenbaum break; 120106e0070dSMark Shellenbaum 120206e0070dSMark Shellenbaum if (dr == NULL) 120306e0070dSMark Shellenbaum data = NULL; 120406e0070dSMark Shellenbaum else if (dr->dr_dbuf->db_dnode->dn_bonuslen == 0 && 120506e0070dSMark Shellenbaum dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID) 120606e0070dSMark Shellenbaum data = dr->dt.dl.dr_data->b_data; 120706e0070dSMark Shellenbaum else 120806e0070dSMark Shellenbaum data = dr->dt.dl.dr_data; 120906e0070dSMark Shellenbaum return (data); 121006e0070dSMark Shellenbaum } 121106e0070dSMark Shellenbaum 12120a586ceaSMark Shellenbaum void 121306e0070dSMark Shellenbaum dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx) 12140a586ceaSMark Shellenbaum { 12150a586ceaSMark Shellenbaum objset_t *os = dn->dn_objset; 12160a586ceaSMark Shellenbaum void *data = NULL; 121706e0070dSMark Shellenbaum dmu_buf_impl_t *db = NULL; 12180a586ceaSMark Shellenbaum uint64_t *user, *group; 12190a586ceaSMark Shellenbaum int flags = dn->dn_id_flags; 12200a586ceaSMark Shellenbaum int error; 122106e0070dSMark Shellenbaum boolean_t have_spill = B_FALSE; 12220a586ceaSMark Shellenbaum 12230a586ceaSMark Shellenbaum if (!dmu_objset_userused_enabled(dn->dn_objset)) 12240a586ceaSMark Shellenbaum return; 12250a586ceaSMark Shellenbaum 12260a586ceaSMark Shellenbaum if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST| 12270a586ceaSMark Shellenbaum DN_ID_CHKED_SPILL))) 12280a586ceaSMark Shellenbaum return; 12290a586ceaSMark Shellenbaum 12300a586ceaSMark Shellenbaum if (before && dn->dn_bonuslen != 0) 12310a586ceaSMark Shellenbaum data = DN_BONUS(dn->dn_phys); 123206e0070dSMark Shellenbaum else if (!before && dn->dn_bonuslen != 0) { 123306e0070dSMark Shellenbaum if (dn->dn_bonus) { 123406e0070dSMark Shellenbaum db = dn->dn_bonus; 123506e0070dSMark Shellenbaum mutex_enter(&db->db_mtx); 123606e0070dSMark Shellenbaum data = dmu_objset_userquota_find_data(db, tx); 123706e0070dSMark Shellenbaum } else { 123806e0070dSMark Shellenbaum data = DN_BONUS(dn->dn_phys); 123906e0070dSMark Shellenbaum } 124006e0070dSMark Shellenbaum } else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) { 12410a586ceaSMark Shellenbaum int rf = 0; 12420a586ceaSMark Shellenbaum 12430a586ceaSMark Shellenbaum if (RW_WRITE_HELD(&dn->dn_struct_rwlock)) 12440a586ceaSMark Shellenbaum rf |= DB_RF_HAVESTRUCT; 124506e0070dSMark Shellenbaum error = dmu_spill_hold_by_dnode(dn, rf, 124606e0070dSMark Shellenbaum FTAG, (dmu_buf_t **)&db); 12470a586ceaSMark Shellenbaum ASSERT(error == 0); 124806e0070dSMark Shellenbaum mutex_enter(&db->db_mtx); 124906e0070dSMark Shellenbaum data = (before) ? db->db.db_data : 125006e0070dSMark Shellenbaum dmu_objset_userquota_find_data(db, tx); 125106e0070dSMark Shellenbaum have_spill = B_TRUE; 12520a586ceaSMark Shellenbaum } else { 12530a586ceaSMark Shellenbaum mutex_enter(&dn->dn_mtx); 12540a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_BONUS; 12550a586ceaSMark Shellenbaum mutex_exit(&dn->dn_mtx); 12560a586ceaSMark Shellenbaum return; 12570a586ceaSMark Shellenbaum } 12580a586ceaSMark Shellenbaum 12590a586ceaSMark Shellenbaum if (before) { 126006e0070dSMark Shellenbaum ASSERT(data); 12610a586ceaSMark Shellenbaum user = &dn->dn_olduid; 12620a586ceaSMark Shellenbaum group = &dn->dn_oldgid; 126306e0070dSMark Shellenbaum } else if (data) { 12640a586ceaSMark Shellenbaum user = &dn->dn_newuid; 12650a586ceaSMark Shellenbaum group = &dn->dn_newgid; 12660a586ceaSMark Shellenbaum } 12670a586ceaSMark Shellenbaum 126806e0070dSMark Shellenbaum /* 126906e0070dSMark Shellenbaum * Must always call the callback in case the object 127006e0070dSMark Shellenbaum * type has changed and that type isn't an object type to track 127106e0070dSMark Shellenbaum */ 12720a586ceaSMark Shellenbaum error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data, 12730a586ceaSMark Shellenbaum user, group); 12740a586ceaSMark Shellenbaum 127506e0070dSMark Shellenbaum /* 127606e0070dSMark Shellenbaum * Preserve existing uid/gid when the callback can't determine 127706e0070dSMark Shellenbaum * what the new uid/gid are and the callback returned EEXIST. 127806e0070dSMark Shellenbaum * The EEXIST error tells us to just use the existing uid/gid. 127906e0070dSMark Shellenbaum * If we don't know what the old values are then just assign 128006e0070dSMark Shellenbaum * them to 0, since that is a new file being created. 128106e0070dSMark Shellenbaum */ 128206e0070dSMark Shellenbaum if (!before && data == NULL && error == EEXIST) { 128306e0070dSMark Shellenbaum if (flags & DN_ID_OLD_EXIST) { 128406e0070dSMark Shellenbaum dn->dn_newuid = dn->dn_olduid; 128506e0070dSMark Shellenbaum dn->dn_newgid = dn->dn_oldgid; 128606e0070dSMark Shellenbaum } else { 128706e0070dSMark Shellenbaum dn->dn_newuid = 0; 128806e0070dSMark Shellenbaum dn->dn_newgid = 0; 128906e0070dSMark Shellenbaum } 129006e0070dSMark Shellenbaum error = 0; 129106e0070dSMark Shellenbaum } 129206e0070dSMark Shellenbaum 129306e0070dSMark Shellenbaum if (db) 129406e0070dSMark Shellenbaum mutex_exit(&db->db_mtx); 129506e0070dSMark Shellenbaum 12960a586ceaSMark Shellenbaum mutex_enter(&dn->dn_mtx); 12970a586ceaSMark Shellenbaum if (error == 0 && before) 12980a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_OLD_EXIST; 12990a586ceaSMark Shellenbaum if (error == 0 && !before) 13000a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_NEW_EXIST; 13010a586ceaSMark Shellenbaum 130206e0070dSMark Shellenbaum if (have_spill) { 13030a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_SPILL; 13040a586ceaSMark Shellenbaum } else { 13050a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_BONUS; 13060a586ceaSMark Shellenbaum } 13070a586ceaSMark Shellenbaum mutex_exit(&dn->dn_mtx); 130806e0070dSMark Shellenbaum if (have_spill) 130906e0070dSMark Shellenbaum dmu_buf_rele((dmu_buf_t *)db, FTAG); 13100a586ceaSMark Shellenbaum } 13110a586ceaSMark Shellenbaum 131214843421SMatthew Ahrens boolean_t 131314843421SMatthew Ahrens dmu_objset_userspace_present(objset_t *os) 131414843421SMatthew Ahrens { 1315503ad85cSMatthew Ahrens return (os->os_phys->os_flags & 131614843421SMatthew Ahrens OBJSET_FLAG_USERACCOUNTING_COMPLETE); 131714843421SMatthew Ahrens } 131814843421SMatthew Ahrens 131914843421SMatthew Ahrens int 132014843421SMatthew Ahrens dmu_objset_userspace_upgrade(objset_t *os) 132114843421SMatthew Ahrens { 132214843421SMatthew Ahrens uint64_t obj; 132314843421SMatthew Ahrens int err = 0; 132414843421SMatthew Ahrens 132514843421SMatthew Ahrens if (dmu_objset_userspace_present(os)) 132614843421SMatthew Ahrens return (0); 1327503ad85cSMatthew Ahrens if (!dmu_objset_userused_enabled(os)) 132814843421SMatthew Ahrens return (ENOTSUP); 132914843421SMatthew Ahrens if (dmu_objset_is_snapshot(os)) 133014843421SMatthew Ahrens return (EINVAL); 133114843421SMatthew Ahrens 133214843421SMatthew Ahrens /* 133314843421SMatthew Ahrens * We simply need to mark every object dirty, so that it will be 133414843421SMatthew Ahrens * synced out and now accounted. If this is called 133514843421SMatthew Ahrens * concurrently, or if we already did some work before crashing, 133614843421SMatthew Ahrens * that's fine, since we track each object's accounted state 133714843421SMatthew Ahrens * independently. 133814843421SMatthew Ahrens */ 133914843421SMatthew Ahrens 134014843421SMatthew Ahrens for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) { 134168857716SLin Ling dmu_tx_t *tx; 134214843421SMatthew Ahrens dmu_buf_t *db; 134314843421SMatthew Ahrens int objerr; 134414843421SMatthew Ahrens 134514843421SMatthew Ahrens if (issig(JUSTLOOKING) && issig(FORREAL)) 134614843421SMatthew Ahrens return (EINTR); 134714843421SMatthew Ahrens 134814843421SMatthew Ahrens objerr = dmu_bonus_hold(os, obj, FTAG, &db); 134914843421SMatthew Ahrens if (objerr) 135014843421SMatthew Ahrens continue; 135168857716SLin Ling tx = dmu_tx_create(os); 135214843421SMatthew Ahrens dmu_tx_hold_bonus(tx, obj); 135314843421SMatthew Ahrens objerr = dmu_tx_assign(tx, TXG_WAIT); 135414843421SMatthew Ahrens if (objerr) { 135514843421SMatthew Ahrens dmu_tx_abort(tx); 135614843421SMatthew Ahrens continue; 135714843421SMatthew Ahrens } 135814843421SMatthew Ahrens dmu_buf_will_dirty(db, tx); 135914843421SMatthew Ahrens dmu_buf_rele(db, FTAG); 136014843421SMatthew Ahrens dmu_tx_commit(tx); 136114843421SMatthew Ahrens } 136214843421SMatthew Ahrens 1363503ad85cSMatthew Ahrens os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE; 136414843421SMatthew Ahrens txg_wait_synced(dmu_objset_pool(os), 0); 136514843421SMatthew Ahrens return (0); 136614843421SMatthew Ahrens } 136714843421SMatthew Ahrens 1368fa9e4066Sahrens void 1369a2eea2e1Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp, 1370a2eea2e1Sahrens uint64_t *usedobjsp, uint64_t *availobjsp) 1371fa9e4066Sahrens { 1372503ad85cSMatthew Ahrens dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp, 1373a2eea2e1Sahrens usedobjsp, availobjsp); 1374a2eea2e1Sahrens } 1375a2eea2e1Sahrens 1376a2eea2e1Sahrens uint64_t 1377a2eea2e1Sahrens dmu_objset_fsid_guid(objset_t *os) 1378a2eea2e1Sahrens { 1379503ad85cSMatthew Ahrens return (dsl_dataset_fsid_guid(os->os_dsl_dataset)); 1380a2eea2e1Sahrens } 1381a2eea2e1Sahrens 1382a2eea2e1Sahrens void 1383a2eea2e1Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat) 1384a2eea2e1Sahrens { 1385503ad85cSMatthew Ahrens stat->dds_type = os->os_phys->os_type; 1386503ad85cSMatthew Ahrens if (os->os_dsl_dataset) 1387503ad85cSMatthew Ahrens dsl_dataset_fast_stat(os->os_dsl_dataset, stat); 1388a2eea2e1Sahrens } 1389a2eea2e1Sahrens 1390a2eea2e1Sahrens void 1391a2eea2e1Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv) 1392a2eea2e1Sahrens { 1393503ad85cSMatthew Ahrens ASSERT(os->os_dsl_dataset || 1394503ad85cSMatthew Ahrens os->os_phys->os_type == DMU_OST_META); 1395a2eea2e1Sahrens 1396503ad85cSMatthew Ahrens if (os->os_dsl_dataset != NULL) 1397503ad85cSMatthew Ahrens dsl_dataset_stats(os->os_dsl_dataset, nv); 1398a2eea2e1Sahrens 1399a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE, 1400503ad85cSMatthew Ahrens os->os_phys->os_type); 140114843421SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING, 140214843421SMatthew Ahrens dmu_objset_userspace_present(os)); 1403fa9e4066Sahrens } 1404fa9e4066Sahrens 1405fa9e4066Sahrens int 1406fa9e4066Sahrens dmu_objset_is_snapshot(objset_t *os) 1407fa9e4066Sahrens { 1408503ad85cSMatthew Ahrens if (os->os_dsl_dataset != NULL) 1409503ad85cSMatthew Ahrens return (dsl_dataset_is_snapshot(os->os_dsl_dataset)); 1410fa9e4066Sahrens else 1411fa9e4066Sahrens return (B_FALSE); 1412fa9e4066Sahrens } 1413fa9e4066Sahrens 1414ab04eb8eStimh int 1415ab04eb8eStimh dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen, 1416ab04eb8eStimh boolean_t *conflict) 1417ab04eb8eStimh { 1418503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 1419ab04eb8eStimh uint64_t ignored; 1420ab04eb8eStimh 1421ab04eb8eStimh if (ds->ds_phys->ds_snapnames_zapobj == 0) 1422ab04eb8eStimh return (ENOENT); 1423ab04eb8eStimh 1424ab04eb8eStimh return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset, 1425ab04eb8eStimh ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST, 1426ab04eb8eStimh real, maxlen, conflict)); 1427ab04eb8eStimh } 1428ab04eb8eStimh 1429fa9e4066Sahrens int 1430fa9e4066Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name, 1431b38f0970Sck uint64_t *idp, uint64_t *offp, boolean_t *case_conflict) 1432fa9e4066Sahrens { 1433503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 1434fa9e4066Sahrens zap_cursor_t cursor; 1435fa9e4066Sahrens zap_attribute_t attr; 1436fa9e4066Sahrens 1437fa9e4066Sahrens if (ds->ds_phys->ds_snapnames_zapobj == 0) 1438fa9e4066Sahrens return (ENOENT); 1439fa9e4066Sahrens 1440fa9e4066Sahrens zap_cursor_init_serialized(&cursor, 1441fa9e4066Sahrens ds->ds_dir->dd_pool->dp_meta_objset, 1442fa9e4066Sahrens ds->ds_phys->ds_snapnames_zapobj, *offp); 1443fa9e4066Sahrens 144487e5029aSahrens if (zap_cursor_retrieve(&cursor, &attr) != 0) { 144587e5029aSahrens zap_cursor_fini(&cursor); 144687e5029aSahrens return (ENOENT); 144787e5029aSahrens } 144887e5029aSahrens 144987e5029aSahrens if (strlen(attr.za_name) + 1 > namelen) { 145087e5029aSahrens zap_cursor_fini(&cursor); 145187e5029aSahrens return (ENAMETOOLONG); 145287e5029aSahrens } 145387e5029aSahrens 145487e5029aSahrens (void) strcpy(name, attr.za_name); 145587e5029aSahrens if (idp) 145687e5029aSahrens *idp = attr.za_first_integer; 1457b38f0970Sck if (case_conflict) 1458b38f0970Sck *case_conflict = attr.za_normalization_conflict; 145987e5029aSahrens zap_cursor_advance(&cursor); 146087e5029aSahrens *offp = zap_cursor_serialize(&cursor); 146187e5029aSahrens zap_cursor_fini(&cursor); 146287e5029aSahrens 146387e5029aSahrens return (0); 146487e5029aSahrens } 146587e5029aSahrens 146687e5029aSahrens int 146787e5029aSahrens dmu_dir_list_next(objset_t *os, int namelen, char *name, 146887e5029aSahrens uint64_t *idp, uint64_t *offp) 146987e5029aSahrens { 1470503ad85cSMatthew Ahrens dsl_dir_t *dd = os->os_dsl_dataset->ds_dir; 147187e5029aSahrens zap_cursor_t cursor; 147287e5029aSahrens zap_attribute_t attr; 147387e5029aSahrens 147487e5029aSahrens /* there is no next dir on a snapshot! */ 1475503ad85cSMatthew Ahrens if (os->os_dsl_dataset->ds_object != 147687e5029aSahrens dd->dd_phys->dd_head_dataset_obj) 1477fa9e4066Sahrens return (ENOENT); 1478fa9e4066Sahrens 147987e5029aSahrens zap_cursor_init_serialized(&cursor, 148087e5029aSahrens dd->dd_pool->dp_meta_objset, 148187e5029aSahrens dd->dd_phys->dd_child_dir_zapobj, *offp); 148287e5029aSahrens 148387e5029aSahrens if (zap_cursor_retrieve(&cursor, &attr) != 0) { 148487e5029aSahrens zap_cursor_fini(&cursor); 148587e5029aSahrens return (ENOENT); 148687e5029aSahrens } 148787e5029aSahrens 148887e5029aSahrens if (strlen(attr.za_name) + 1 > namelen) { 148987e5029aSahrens zap_cursor_fini(&cursor); 1490fa9e4066Sahrens return (ENAMETOOLONG); 149187e5029aSahrens } 1492fa9e4066Sahrens 1493fa9e4066Sahrens (void) strcpy(name, attr.za_name); 149487e5029aSahrens if (idp) 149587e5029aSahrens *idp = attr.za_first_integer; 1496fa9e4066Sahrens zap_cursor_advance(&cursor); 1497fa9e4066Sahrens *offp = zap_cursor_serialize(&cursor); 149887e5029aSahrens zap_cursor_fini(&cursor); 1499fa9e4066Sahrens 1500fa9e4066Sahrens return (0); 1501fa9e4066Sahrens } 1502fa9e4066Sahrens 1503088f3894Sahrens struct findarg { 1504fd136879SMatthew Ahrens int (*func)(const char *, void *); 1505088f3894Sahrens void *arg; 1506088f3894Sahrens }; 1507088f3894Sahrens 1508088f3894Sahrens /* ARGSUSED */ 1509088f3894Sahrens static int 1510088f3894Sahrens findfunc(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg) 1511088f3894Sahrens { 1512088f3894Sahrens struct findarg *fa = arg; 1513fd136879SMatthew Ahrens return (fa->func(dsname, fa->arg)); 1514088f3894Sahrens } 1515088f3894Sahrens 1516fa9e4066Sahrens /* 1517fa9e4066Sahrens * Find all objsets under name, and for each, call 'func(child_name, arg)'. 1518088f3894Sahrens * Perhaps change all callers to use dmu_objset_find_spa()? 1519fa9e4066Sahrens */ 15201d452cf5Sahrens int 1521fd136879SMatthew Ahrens dmu_objset_find(char *name, int func(const char *, void *), void *arg, 1522fd136879SMatthew Ahrens int flags) 1523088f3894Sahrens { 1524088f3894Sahrens struct findarg fa; 1525088f3894Sahrens fa.func = func; 1526088f3894Sahrens fa.arg = arg; 1527088f3894Sahrens return (dmu_objset_find_spa(NULL, name, findfunc, &fa, flags)); 1528088f3894Sahrens } 1529088f3894Sahrens 1530088f3894Sahrens /* 1531088f3894Sahrens * Find all objsets under name, call func on each 1532088f3894Sahrens */ 1533088f3894Sahrens int 1534088f3894Sahrens dmu_objset_find_spa(spa_t *spa, const char *name, 1535088f3894Sahrens int func(spa_t *, uint64_t, const char *, void *), void *arg, int flags) 1536fa9e4066Sahrens { 1537fa9e4066Sahrens dsl_dir_t *dd; 1538088f3894Sahrens dsl_pool_t *dp; 1539088f3894Sahrens dsl_dataset_t *ds; 1540fa9e4066Sahrens zap_cursor_t zc; 1541b7661cccSmmusante zap_attribute_t *attr; 1542fa9e4066Sahrens char *child; 1543088f3894Sahrens uint64_t thisobj; 1544088f3894Sahrens int err; 1545fa9e4066Sahrens 1546088f3894Sahrens if (name == NULL) 1547088f3894Sahrens name = spa_name(spa); 1548088f3894Sahrens err = dsl_dir_open_spa(spa, name, FTAG, &dd, NULL); 1549ea8dc4b6Seschrock if (err) 15501d452cf5Sahrens return (err); 1551fa9e4066Sahrens 1552088f3894Sahrens /* Don't visit hidden ($MOS & $ORIGIN) objsets. */ 1553088f3894Sahrens if (dd->dd_myname[0] == '$') { 1554088f3894Sahrens dsl_dir_close(dd, FTAG); 1555088f3894Sahrens return (0); 1556088f3894Sahrens } 1557088f3894Sahrens 1558088f3894Sahrens thisobj = dd->dd_phys->dd_head_dataset_obj; 1559b7661cccSmmusante attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); 1560088f3894Sahrens dp = dd->dd_pool; 1561fa9e4066Sahrens 1562fa9e4066Sahrens /* 1563fa9e4066Sahrens * Iterate over all children. 1564fa9e4066Sahrens */ 15650b69c2f0Sahrens if (flags & DS_FIND_CHILDREN) { 1566088f3894Sahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, 15670b69c2f0Sahrens dd->dd_phys->dd_child_dir_zapobj); 1568b7661cccSmmusante zap_cursor_retrieve(&zc, attr) == 0; 15690b69c2f0Sahrens (void) zap_cursor_advance(&zc)) { 1570b7661cccSmmusante ASSERT(attr->za_integer_length == sizeof (uint64_t)); 1571b7661cccSmmusante ASSERT(attr->za_num_integers == 1); 1572fa9e4066Sahrens 1573486ae710SMatthew Ahrens child = kmem_asprintf("%s/%s", name, attr->za_name); 1574088f3894Sahrens err = dmu_objset_find_spa(spa, child, func, arg, flags); 1575486ae710SMatthew Ahrens strfree(child); 15760b69c2f0Sahrens if (err) 15770b69c2f0Sahrens break; 15780b69c2f0Sahrens } 15790b69c2f0Sahrens zap_cursor_fini(&zc); 15801d452cf5Sahrens 15810b69c2f0Sahrens if (err) { 15820b69c2f0Sahrens dsl_dir_close(dd, FTAG); 1583b7661cccSmmusante kmem_free(attr, sizeof (zap_attribute_t)); 15840b69c2f0Sahrens return (err); 15850b69c2f0Sahrens } 1586fa9e4066Sahrens } 1587fa9e4066Sahrens 1588fa9e4066Sahrens /* 1589fa9e4066Sahrens * Iterate over all snapshots. 1590fa9e4066Sahrens */ 1591088f3894Sahrens if (flags & DS_FIND_SNAPSHOTS) { 1592088f3894Sahrens if (!dsl_pool_sync_context(dp)) 1593088f3894Sahrens rw_enter(&dp->dp_config_rwlock, RW_READER); 1594088f3894Sahrens err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds); 1595088f3894Sahrens if (!dsl_pool_sync_context(dp)) 1596088f3894Sahrens rw_exit(&dp->dp_config_rwlock); 1597088f3894Sahrens 1598088f3894Sahrens if (err == 0) { 1599088f3894Sahrens uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj; 1600088f3894Sahrens dsl_dataset_rele(ds, FTAG); 1601088f3894Sahrens 1602088f3894Sahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj); 1603088f3894Sahrens zap_cursor_retrieve(&zc, attr) == 0; 1604088f3894Sahrens (void) zap_cursor_advance(&zc)) { 1605088f3894Sahrens ASSERT(attr->za_integer_length == 1606088f3894Sahrens sizeof (uint64_t)); 1607088f3894Sahrens ASSERT(attr->za_num_integers == 1); 1608088f3894Sahrens 1609486ae710SMatthew Ahrens child = kmem_asprintf("%s@%s", 1610486ae710SMatthew Ahrens name, attr->za_name); 1611088f3894Sahrens err = func(spa, attr->za_first_integer, 1612088f3894Sahrens child, arg); 1613486ae710SMatthew Ahrens strfree(child); 1614088f3894Sahrens if (err) 1615088f3894Sahrens break; 1616088f3894Sahrens } 1617088f3894Sahrens zap_cursor_fini(&zc); 1618fa9e4066Sahrens } 1619fa9e4066Sahrens } 1620fa9e4066Sahrens 1621fa9e4066Sahrens dsl_dir_close(dd, FTAG); 1622b7661cccSmmusante kmem_free(attr, sizeof (zap_attribute_t)); 1623fa9e4066Sahrens 16241d452cf5Sahrens if (err) 16251d452cf5Sahrens return (err); 16261d452cf5Sahrens 1627fa9e4066Sahrens /* 1628fa9e4066Sahrens * Apply to self if appropriate. 1629fa9e4066Sahrens */ 1630088f3894Sahrens err = func(spa, thisobj, name, arg); 16311d452cf5Sahrens return (err); 1632fa9e4066Sahrens } 1633f18faf3fSek 16347f73c863SRich Morris /* ARGSUSED */ 16357f73c863SRich Morris int 1636fd136879SMatthew Ahrens dmu_objset_prefetch(const char *name, void *arg) 16377f73c863SRich Morris { 16387f73c863SRich Morris dsl_dataset_t *ds; 16397f73c863SRich Morris 16407f73c863SRich Morris if (dsl_dataset_hold(name, FTAG, &ds)) 16417f73c863SRich Morris return (0); 16427f73c863SRich Morris 16437f73c863SRich Morris if (!BP_IS_HOLE(&ds->ds_phys->ds_bp)) { 16447f73c863SRich Morris mutex_enter(&ds->ds_opening_lock); 1645503ad85cSMatthew Ahrens if (ds->ds_objset == NULL) { 16467f73c863SRich Morris uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH; 16477f73c863SRich Morris zbookmark_t zb; 16487f73c863SRich Morris 1649b24ab676SJeff Bonwick SET_BOOKMARK(&zb, ds->ds_object, ZB_ROOT_OBJECT, 1650b24ab676SJeff Bonwick ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 16517f73c863SRich Morris 16527f73c863SRich Morris (void) arc_read_nolock(NULL, dsl_dataset_get_spa(ds), 16537f73c863SRich Morris &ds->ds_phys->ds_bp, NULL, NULL, 16547f73c863SRich Morris ZIO_PRIORITY_ASYNC_READ, 16557f73c863SRich Morris ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, 16567f73c863SRich Morris &aflags, &zb); 16577f73c863SRich Morris } 16587f73c863SRich Morris mutex_exit(&ds->ds_opening_lock); 16597f73c863SRich Morris } 16607f73c863SRich Morris 16617f73c863SRich Morris dsl_dataset_rele(ds, FTAG); 16627f73c863SRich Morris return (0); 16637f73c863SRich Morris } 16647f73c863SRich Morris 1665f18faf3fSek void 1666f18faf3fSek dmu_objset_set_user(objset_t *os, void *user_ptr) 1667f18faf3fSek { 1668503ad85cSMatthew Ahrens ASSERT(MUTEX_HELD(&os->os_user_ptr_lock)); 1669503ad85cSMatthew Ahrens os->os_user_ptr = user_ptr; 1670f18faf3fSek } 1671f18faf3fSek 1672f18faf3fSek void * 1673f18faf3fSek dmu_objset_get_user(objset_t *os) 1674f18faf3fSek { 1675503ad85cSMatthew Ahrens ASSERT(MUTEX_HELD(&os->os_user_ptr_lock)); 1676503ad85cSMatthew Ahrens return (os->os_user_ptr); 1677f18faf3fSek } 1678