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 /* 22*06e0070dSMark Shellenbaum * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23fa9e4066Sahrens */ 24fa9e4066Sahrens 25ecd6cf80Smarks #include <sys/cred.h> 26fa9e4066Sahrens #include <sys/zfs_context.h> 27fa9e4066Sahrens #include <sys/dmu_objset.h> 28fa9e4066Sahrens #include <sys/dsl_dir.h> 29fa9e4066Sahrens #include <sys/dsl_dataset.h> 30fa9e4066Sahrens #include <sys/dsl_prop.h> 31fa9e4066Sahrens #include <sys/dsl_pool.h> 321d452cf5Sahrens #include <sys/dsl_synctask.h> 33ecd6cf80Smarks #include <sys/dsl_deleg.h> 34fa9e4066Sahrens #include <sys/dnode.h> 35fa9e4066Sahrens #include <sys/dbuf.h> 36a2eea2e1Sahrens #include <sys/zvol.h> 37fa9e4066Sahrens #include <sys/dmu_tx.h> 38fa9e4066Sahrens #include <sys/zap.h> 39fa9e4066Sahrens #include <sys/zil.h> 40fa9e4066Sahrens #include <sys/dmu_impl.h> 41ecd6cf80Smarks #include <sys/zfs_ioctl.h> 42486ae710SMatthew Ahrens #include <sys/sunddi.h> 430a586ceaSMark Shellenbaum #include <sys/sa.h> 44fa9e4066Sahrens 45fa9e4066Sahrens spa_t * 46fa9e4066Sahrens dmu_objset_spa(objset_t *os) 47fa9e4066Sahrens { 48503ad85cSMatthew Ahrens return (os->os_spa); 49fa9e4066Sahrens } 50fa9e4066Sahrens 51fa9e4066Sahrens zilog_t * 52fa9e4066Sahrens dmu_objset_zil(objset_t *os) 53fa9e4066Sahrens { 54503ad85cSMatthew Ahrens return (os->os_zil); 55fa9e4066Sahrens } 56fa9e4066Sahrens 57fa9e4066Sahrens dsl_pool_t * 58fa9e4066Sahrens dmu_objset_pool(objset_t *os) 59fa9e4066Sahrens { 60fa9e4066Sahrens dsl_dataset_t *ds; 61fa9e4066Sahrens 62503ad85cSMatthew Ahrens if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir) 63fa9e4066Sahrens return (ds->ds_dir->dd_pool); 64fa9e4066Sahrens else 65503ad85cSMatthew Ahrens return (spa_get_dsl(os->os_spa)); 66fa9e4066Sahrens } 67fa9e4066Sahrens 68fa9e4066Sahrens dsl_dataset_t * 69fa9e4066Sahrens dmu_objset_ds(objset_t *os) 70fa9e4066Sahrens { 71503ad85cSMatthew Ahrens return (os->os_dsl_dataset); 72fa9e4066Sahrens } 73fa9e4066Sahrens 74fa9e4066Sahrens dmu_objset_type_t 75fa9e4066Sahrens dmu_objset_type(objset_t *os) 76fa9e4066Sahrens { 77503ad85cSMatthew Ahrens return (os->os_phys->os_type); 78fa9e4066Sahrens } 79fa9e4066Sahrens 80fa9e4066Sahrens void 81fa9e4066Sahrens dmu_objset_name(objset_t *os, char *buf) 82fa9e4066Sahrens { 83503ad85cSMatthew Ahrens dsl_dataset_name(os->os_dsl_dataset, buf); 84fa9e4066Sahrens } 85fa9e4066Sahrens 86fa9e4066Sahrens uint64_t 87fa9e4066Sahrens dmu_objset_id(objset_t *os) 88fa9e4066Sahrens { 89503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 90fa9e4066Sahrens 91fa9e4066Sahrens return (ds ? ds->ds_object : 0); 92fa9e4066Sahrens } 93fa9e4066Sahrens 94e09fa4daSNeil Perrin uint64_t 95e09fa4daSNeil Perrin dmu_objset_logbias(objset_t *os) 96e09fa4daSNeil Perrin { 97e09fa4daSNeil Perrin return (os->os_logbias); 98e09fa4daSNeil Perrin } 99e09fa4daSNeil Perrin 100fa9e4066Sahrens static void 101fa9e4066Sahrens checksum_changed_cb(void *arg, uint64_t newval) 102fa9e4066Sahrens { 103503ad85cSMatthew Ahrens objset_t *os = arg; 104fa9e4066Sahrens 105fa9e4066Sahrens /* 106fa9e4066Sahrens * Inheritance should have been done by now. 107fa9e4066Sahrens */ 108fa9e4066Sahrens ASSERT(newval != ZIO_CHECKSUM_INHERIT); 109fa9e4066Sahrens 110503ad85cSMatthew Ahrens os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE); 111fa9e4066Sahrens } 112fa9e4066Sahrens 113fa9e4066Sahrens static void 114fa9e4066Sahrens compression_changed_cb(void *arg, uint64_t newval) 115fa9e4066Sahrens { 116503ad85cSMatthew Ahrens objset_t *os = arg; 117fa9e4066Sahrens 118fa9e4066Sahrens /* 119fa9e4066Sahrens * Inheritance and range checking should have been done by now. 120fa9e4066Sahrens */ 121fa9e4066Sahrens ASSERT(newval != ZIO_COMPRESS_INHERIT); 122fa9e4066Sahrens 123503ad85cSMatthew Ahrens os->os_compress = zio_compress_select(newval, ZIO_COMPRESS_ON_VALUE); 124fa9e4066Sahrens } 125fa9e4066Sahrens 126d0ad202dSahrens static void 127d0ad202dSahrens copies_changed_cb(void *arg, uint64_t newval) 128d0ad202dSahrens { 129503ad85cSMatthew Ahrens objset_t *os = arg; 130d0ad202dSahrens 131d0ad202dSahrens /* 132d0ad202dSahrens * Inheritance and range checking should have been done by now. 133d0ad202dSahrens */ 134d0ad202dSahrens ASSERT(newval > 0); 135503ad85cSMatthew Ahrens ASSERT(newval <= spa_max_replication(os->os_spa)); 136d0ad202dSahrens 137503ad85cSMatthew Ahrens os->os_copies = newval; 138d0ad202dSahrens } 139d0ad202dSahrens 140b24ab676SJeff Bonwick static void 141b24ab676SJeff Bonwick dedup_changed_cb(void *arg, uint64_t newval) 142b24ab676SJeff Bonwick { 143b24ab676SJeff Bonwick objset_t *os = arg; 144b24ab676SJeff Bonwick spa_t *spa = os->os_spa; 145b24ab676SJeff Bonwick enum zio_checksum checksum; 146b24ab676SJeff Bonwick 147b24ab676SJeff Bonwick /* 148b24ab676SJeff Bonwick * Inheritance should have been done by now. 149b24ab676SJeff Bonwick */ 150b24ab676SJeff Bonwick ASSERT(newval != ZIO_CHECKSUM_INHERIT); 151b24ab676SJeff Bonwick 152b24ab676SJeff Bonwick checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF); 153b24ab676SJeff Bonwick 154b24ab676SJeff Bonwick os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK; 155b24ab676SJeff Bonwick os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY); 156b24ab676SJeff Bonwick } 157b24ab676SJeff Bonwick 1583baa08fcSek static void 1593baa08fcSek primary_cache_changed_cb(void *arg, uint64_t newval) 1603baa08fcSek { 161503ad85cSMatthew Ahrens objset_t *os = arg; 1623baa08fcSek 1633baa08fcSek /* 1643baa08fcSek * Inheritance and range checking should have been done by now. 1653baa08fcSek */ 1663baa08fcSek ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE || 1673baa08fcSek newval == ZFS_CACHE_METADATA); 1683baa08fcSek 169503ad85cSMatthew Ahrens os->os_primary_cache = newval; 1703baa08fcSek } 1713baa08fcSek 1723baa08fcSek static void 1733baa08fcSek secondary_cache_changed_cb(void *arg, uint64_t newval) 1743baa08fcSek { 175503ad85cSMatthew Ahrens objset_t *os = arg; 1763baa08fcSek 1773baa08fcSek /* 1783baa08fcSek * Inheritance and range checking should have been done by now. 1793baa08fcSek */ 1803baa08fcSek ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE || 1813baa08fcSek newval == ZFS_CACHE_METADATA); 1823baa08fcSek 183503ad85cSMatthew Ahrens os->os_secondary_cache = newval; 1843baa08fcSek } 1853baa08fcSek 186e09fa4daSNeil Perrin static void 187e09fa4daSNeil Perrin logbias_changed_cb(void *arg, uint64_t newval) 188e09fa4daSNeil Perrin { 189e09fa4daSNeil Perrin objset_t *os = arg; 190e09fa4daSNeil Perrin 191e09fa4daSNeil Perrin ASSERT(newval == ZFS_LOGBIAS_LATENCY || 192e09fa4daSNeil Perrin newval == ZFS_LOGBIAS_THROUGHPUT); 193e09fa4daSNeil Perrin os->os_logbias = newval; 194e09fa4daSNeil Perrin if (os->os_zil) 195e09fa4daSNeil Perrin zil_set_logbias(os->os_zil, newval); 196e09fa4daSNeil Perrin } 197e09fa4daSNeil Perrin 198fa9e4066Sahrens void 199fa9e4066Sahrens dmu_objset_byteswap(void *buf, size_t size) 200fa9e4066Sahrens { 201fa9e4066Sahrens objset_phys_t *osp = buf; 202fa9e4066Sahrens 20314843421SMatthew Ahrens ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t)); 204fa9e4066Sahrens dnode_byteswap(&osp->os_meta_dnode); 205fa9e4066Sahrens byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t)); 206fa9e4066Sahrens osp->os_type = BSWAP_64(osp->os_type); 20714843421SMatthew Ahrens osp->os_flags = BSWAP_64(osp->os_flags); 20814843421SMatthew Ahrens if (size == sizeof (objset_phys_t)) { 20914843421SMatthew Ahrens dnode_byteswap(&osp->os_userused_dnode); 21014843421SMatthew Ahrens dnode_byteswap(&osp->os_groupused_dnode); 21114843421SMatthew Ahrens } 212fa9e4066Sahrens } 213fa9e4066Sahrens 214ea8dc4b6Seschrock int 215ea8dc4b6Seschrock dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, 216503ad85cSMatthew Ahrens objset_t **osp) 217fa9e4066Sahrens { 218503ad85cSMatthew Ahrens objset_t *os; 219088f3894Sahrens int i, err; 220fa9e4066Sahrens 22191ebeef5Sahrens ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock)); 22291ebeef5Sahrens 223503ad85cSMatthew Ahrens os = kmem_zalloc(sizeof (objset_t), KM_SLEEP); 224503ad85cSMatthew Ahrens os->os_dsl_dataset = ds; 225503ad85cSMatthew Ahrens os->os_spa = spa; 226503ad85cSMatthew Ahrens os->os_rootbp = bp; 227503ad85cSMatthew Ahrens if (!BP_IS_HOLE(os->os_rootbp)) { 22813506d1eSmaybee uint32_t aflags = ARC_WAIT; 229ea8dc4b6Seschrock zbookmark_t zb; 230b24ab676SJeff Bonwick SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET, 231b24ab676SJeff Bonwick ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 232b24ab676SJeff Bonwick 233503ad85cSMatthew Ahrens if (DMU_OS_IS_L2CACHEABLE(os)) 2343baa08fcSek aflags |= ARC_L2CACHE; 235ea8dc4b6Seschrock 236503ad85cSMatthew Ahrens dprintf_bp(os->os_rootbp, "reading %s", ""); 237088f3894Sahrens /* 238088f3894Sahrens * NB: when bprewrite scrub can change the bp, 239088f3894Sahrens * and this is called from dmu_objset_open_ds_os, the bp 240088f3894Sahrens * could change, and we'll need a lock. 241088f3894Sahrens */ 242503ad85cSMatthew Ahrens err = arc_read_nolock(NULL, spa, os->os_rootbp, 243503ad85cSMatthew Ahrens arc_getbuf_func, &os->os_phys_buf, 24413506d1eSmaybee ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb); 245ea8dc4b6Seschrock if (err) { 246503ad85cSMatthew Ahrens kmem_free(os, sizeof (objset_t)); 247b87f3af3Sperrin /* convert checksum errors into IO errors */ 248b87f3af3Sperrin if (err == ECKSUM) 249b87f3af3Sperrin err = EIO; 250ea8dc4b6Seschrock return (err); 251ea8dc4b6Seschrock } 25214843421SMatthew Ahrens 25314843421SMatthew Ahrens /* Increase the blocksize if we are permitted. */ 25414843421SMatthew Ahrens if (spa_version(spa) >= SPA_VERSION_USERSPACE && 255503ad85cSMatthew Ahrens arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) { 25614843421SMatthew Ahrens arc_buf_t *buf = arc_buf_alloc(spa, 257503ad85cSMatthew Ahrens sizeof (objset_phys_t), &os->os_phys_buf, 25814843421SMatthew Ahrens ARC_BUFC_METADATA); 25914843421SMatthew Ahrens bzero(buf->b_data, sizeof (objset_phys_t)); 260503ad85cSMatthew Ahrens bcopy(os->os_phys_buf->b_data, buf->b_data, 261503ad85cSMatthew Ahrens arc_buf_size(os->os_phys_buf)); 262503ad85cSMatthew Ahrens (void) arc_buf_remove_ref(os->os_phys_buf, 263503ad85cSMatthew Ahrens &os->os_phys_buf); 264503ad85cSMatthew Ahrens os->os_phys_buf = buf; 26514843421SMatthew Ahrens } 26614843421SMatthew Ahrens 267503ad85cSMatthew Ahrens os->os_phys = os->os_phys_buf->b_data; 268503ad85cSMatthew Ahrens os->os_flags = os->os_phys->os_flags; 269fa9e4066Sahrens } else { 27014843421SMatthew Ahrens int size = spa_version(spa) >= SPA_VERSION_USERSPACE ? 27114843421SMatthew Ahrens sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE; 272503ad85cSMatthew Ahrens os->os_phys_buf = arc_buf_alloc(spa, size, 273503ad85cSMatthew Ahrens &os->os_phys_buf, ARC_BUFC_METADATA); 274503ad85cSMatthew Ahrens os->os_phys = os->os_phys_buf->b_data; 275503ad85cSMatthew Ahrens bzero(os->os_phys, size); 276fa9e4066Sahrens } 277fa9e4066Sahrens 278fa9e4066Sahrens /* 279fa9e4066Sahrens * Note: the changed_cb will be called once before the register 280fa9e4066Sahrens * func returns, thus changing the checksum/compression from the 2813baa08fcSek * default (fletcher2/off). Snapshots don't need to know about 2823baa08fcSek * checksum/compression/copies. 283fa9e4066Sahrens */ 2843baa08fcSek if (ds) { 2853baa08fcSek err = dsl_prop_register(ds, "primarycache", 286503ad85cSMatthew Ahrens primary_cache_changed_cb, os); 287d0ad202dSahrens if (err == 0) 2883baa08fcSek err = dsl_prop_register(ds, "secondarycache", 289503ad85cSMatthew Ahrens secondary_cache_changed_cb, os); 2903baa08fcSek if (!dsl_dataset_is_snapshot(ds)) { 2913baa08fcSek if (err == 0) 2923baa08fcSek err = dsl_prop_register(ds, "checksum", 293503ad85cSMatthew Ahrens checksum_changed_cb, os); 2943baa08fcSek if (err == 0) 2953baa08fcSek err = dsl_prop_register(ds, "compression", 296503ad85cSMatthew Ahrens compression_changed_cb, os); 2973baa08fcSek if (err == 0) 2983baa08fcSek err = dsl_prop_register(ds, "copies", 299503ad85cSMatthew Ahrens copies_changed_cb, os); 300b24ab676SJeff Bonwick if (err == 0) 301b24ab676SJeff Bonwick err = dsl_prop_register(ds, "dedup", 302b24ab676SJeff Bonwick dedup_changed_cb, os); 303e09fa4daSNeil Perrin if (err == 0) 304e09fa4daSNeil Perrin err = dsl_prop_register(ds, "logbias", 305e09fa4daSNeil Perrin logbias_changed_cb, os); 3063baa08fcSek } 307ea8dc4b6Seschrock if (err) { 308503ad85cSMatthew Ahrens VERIFY(arc_buf_remove_ref(os->os_phys_buf, 309503ad85cSMatthew Ahrens &os->os_phys_buf) == 1); 310503ad85cSMatthew Ahrens kmem_free(os, sizeof (objset_t)); 311ea8dc4b6Seschrock return (err); 312ea8dc4b6Seschrock } 31399653d4eSeschrock } else if (ds == NULL) { 314fa9e4066Sahrens /* It's the meta-objset. */ 315503ad85cSMatthew Ahrens os->os_checksum = ZIO_CHECKSUM_FLETCHER_4; 316503ad85cSMatthew Ahrens os->os_compress = ZIO_COMPRESS_LZJB; 317503ad85cSMatthew Ahrens os->os_copies = spa_max_replication(spa); 318b24ab676SJeff Bonwick os->os_dedup_checksum = ZIO_CHECKSUM_OFF; 319b24ab676SJeff Bonwick os->os_dedup_verify = 0; 320b24ab676SJeff Bonwick os->os_logbias = 0; 321503ad85cSMatthew Ahrens os->os_primary_cache = ZFS_CACHE_ALL; 322503ad85cSMatthew Ahrens os->os_secondary_cache = ZFS_CACHE_ALL; 323fa9e4066Sahrens } 324fa9e4066Sahrens 325503ad85cSMatthew Ahrens os->os_zil_header = os->os_phys->os_zil_header; 326503ad85cSMatthew Ahrens os->os_zil = zil_alloc(os, &os->os_zil_header); 327fa9e4066Sahrens 328fa9e4066Sahrens for (i = 0; i < TXG_SIZE; i++) { 329503ad85cSMatthew Ahrens list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t), 330fa9e4066Sahrens offsetof(dnode_t, dn_dirty_link[i])); 331503ad85cSMatthew Ahrens list_create(&os->os_free_dnodes[i], sizeof (dnode_t), 332fa9e4066Sahrens offsetof(dnode_t, dn_dirty_link[i])); 333fa9e4066Sahrens } 334503ad85cSMatthew Ahrens list_create(&os->os_dnodes, sizeof (dnode_t), 335fa9e4066Sahrens offsetof(dnode_t, dn_link)); 336503ad85cSMatthew Ahrens list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t), 337fa9e4066Sahrens offsetof(dmu_buf_impl_t, db_link)); 338fa9e4066Sahrens 339503ad85cSMatthew Ahrens mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL); 340503ad85cSMatthew Ahrens mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL); 341503ad85cSMatthew Ahrens mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL); 342503ad85cSMatthew Ahrens 343503ad85cSMatthew Ahrens os->os_meta_dnode = dnode_special_open(os, 344503ad85cSMatthew Ahrens &os->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT); 345503ad85cSMatthew Ahrens if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) { 346503ad85cSMatthew Ahrens os->os_userused_dnode = dnode_special_open(os, 347503ad85cSMatthew Ahrens &os->os_phys->os_userused_dnode, DMU_USERUSED_OBJECT); 348503ad85cSMatthew Ahrens os->os_groupused_dnode = dnode_special_open(os, 349503ad85cSMatthew Ahrens &os->os_phys->os_groupused_dnode, DMU_GROUPUSED_OBJECT); 35014843421SMatthew Ahrens } 351fa9e4066Sahrens 35291ebeef5Sahrens /* 35391ebeef5Sahrens * We should be the only thread trying to do this because we 35491ebeef5Sahrens * have ds_opening_lock 35591ebeef5Sahrens */ 35691ebeef5Sahrens if (ds) { 357503ad85cSMatthew Ahrens mutex_enter(&ds->ds_lock); 358503ad85cSMatthew Ahrens ASSERT(ds->ds_objset == NULL); 359503ad85cSMatthew Ahrens ds->ds_objset = os; 360503ad85cSMatthew Ahrens mutex_exit(&ds->ds_lock); 361fa9e4066Sahrens } 362fa9e4066Sahrens 363503ad85cSMatthew Ahrens *osp = os; 364ea8dc4b6Seschrock return (0); 365fa9e4066Sahrens } 366fa9e4066Sahrens 367503ad85cSMatthew Ahrens int 368503ad85cSMatthew Ahrens dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp) 3693cb34c60Sahrens { 370503ad85cSMatthew Ahrens int err = 0; 3713cb34c60Sahrens 3723cb34c60Sahrens mutex_enter(&ds->ds_opening_lock); 373503ad85cSMatthew Ahrens *osp = ds->ds_objset; 374503ad85cSMatthew Ahrens if (*osp == NULL) { 3753cb34c60Sahrens err = dmu_objset_open_impl(dsl_dataset_get_spa(ds), 376503ad85cSMatthew Ahrens ds, &ds->ds_phys->ds_bp, osp); 3773cb34c60Sahrens } 3783cb34c60Sahrens mutex_exit(&ds->ds_opening_lock); 379503ad85cSMatthew Ahrens return (err); 3803cb34c60Sahrens } 3813cb34c60Sahrens 382503ad85cSMatthew Ahrens /* called from zpl */ 3833cb34c60Sahrens int 384503ad85cSMatthew Ahrens dmu_objset_hold(const char *name, void *tag, objset_t **osp) 3853cb34c60Sahrens { 386503ad85cSMatthew Ahrens dsl_dataset_t *ds; 3873cb34c60Sahrens int err; 3883cb34c60Sahrens 389503ad85cSMatthew Ahrens err = dsl_dataset_hold(name, tag, &ds); 3903cb34c60Sahrens if (err) 391503ad85cSMatthew Ahrens return (err); 392503ad85cSMatthew Ahrens 393503ad85cSMatthew Ahrens err = dmu_objset_from_ds(ds, osp); 394503ad85cSMatthew Ahrens if (err) 395503ad85cSMatthew Ahrens dsl_dataset_rele(ds, tag); 396503ad85cSMatthew Ahrens 3973cb34c60Sahrens return (err); 3983cb34c60Sahrens } 3993cb34c60Sahrens 400fa9e4066Sahrens /* called from zpl */ 401fa9e4066Sahrens int 402503ad85cSMatthew Ahrens dmu_objset_own(const char *name, dmu_objset_type_t type, 403503ad85cSMatthew Ahrens boolean_t readonly, void *tag, objset_t **osp) 404fa9e4066Sahrens { 405f18faf3fSek dsl_dataset_t *ds; 406f18faf3fSek int err; 407fa9e4066Sahrens 408503ad85cSMatthew Ahrens err = dsl_dataset_own(name, B_FALSE, tag, &ds); 409503ad85cSMatthew Ahrens if (err) 410fa9e4066Sahrens return (err); 411fa9e4066Sahrens 412503ad85cSMatthew Ahrens err = dmu_objset_from_ds(ds, osp); 4133cb34c60Sahrens if (err) { 414503ad85cSMatthew Ahrens dsl_dataset_disown(ds, tag); 415681d9761SEric Taylor } else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) { 416503ad85cSMatthew Ahrens dmu_objset_disown(*osp, tag); 417503ad85cSMatthew Ahrens return (EINVAL); 418681d9761SEric Taylor } else if (!readonly && dsl_dataset_is_snapshot(ds)) { 419681d9761SEric Taylor dmu_objset_disown(*osp, tag); 420681d9761SEric Taylor return (EROFS); 421fa9e4066Sahrens } 4223cb34c60Sahrens return (err); 423fa9e4066Sahrens } 424fa9e4066Sahrens 425fa9e4066Sahrens void 426503ad85cSMatthew Ahrens dmu_objset_rele(objset_t *os, void *tag) 427fa9e4066Sahrens { 428503ad85cSMatthew Ahrens dsl_dataset_rele(os->os_dsl_dataset, tag); 429503ad85cSMatthew Ahrens } 430503ad85cSMatthew Ahrens 431503ad85cSMatthew Ahrens void 432503ad85cSMatthew Ahrens dmu_objset_disown(objset_t *os, void *tag) 433503ad85cSMatthew Ahrens { 434503ad85cSMatthew Ahrens dsl_dataset_disown(os->os_dsl_dataset, tag); 435fa9e4066Sahrens } 436fa9e4066Sahrens 437436b2950Sperrin int 4381934e92fSmaybee dmu_objset_evict_dbufs(objset_t *os) 439ea8dc4b6Seschrock { 440ea8dc4b6Seschrock dnode_t *dn; 441c543ec06Sahrens 442503ad85cSMatthew Ahrens mutex_enter(&os->os_lock); 443c543ec06Sahrens 444c543ec06Sahrens /* process the mdn last, since the other dnodes have holds on it */ 445503ad85cSMatthew Ahrens list_remove(&os->os_dnodes, os->os_meta_dnode); 446503ad85cSMatthew Ahrens list_insert_tail(&os->os_dnodes, os->os_meta_dnode); 447ea8dc4b6Seschrock 448ea8dc4b6Seschrock /* 449c543ec06Sahrens * Find the first dnode with holds. We have to do this dance 450c543ec06Sahrens * because dnode_add_ref() only works if you already have a 451c543ec06Sahrens * hold. If there are no holds then it has no dbufs so OK to 452c543ec06Sahrens * skip. 453ea8dc4b6Seschrock */ 454503ad85cSMatthew Ahrens for (dn = list_head(&os->os_dnodes); 4551934e92fSmaybee dn && !dnode_add_ref(dn, FTAG); 456503ad85cSMatthew Ahrens dn = list_next(&os->os_dnodes, dn)) 457c543ec06Sahrens continue; 458c543ec06Sahrens 459c543ec06Sahrens while (dn) { 460c543ec06Sahrens dnode_t *next_dn = dn; 461c543ec06Sahrens 462c543ec06Sahrens do { 463503ad85cSMatthew Ahrens next_dn = list_next(&os->os_dnodes, next_dn); 4641934e92fSmaybee } while (next_dn && !dnode_add_ref(next_dn, FTAG)); 465c543ec06Sahrens 466503ad85cSMatthew Ahrens mutex_exit(&os->os_lock); 4671934e92fSmaybee dnode_evict_dbufs(dn); 468c543ec06Sahrens dnode_rele(dn, FTAG); 469503ad85cSMatthew Ahrens mutex_enter(&os->os_lock); 470c543ec06Sahrens dn = next_dn; 471ea8dc4b6Seschrock } 472503ad85cSMatthew Ahrens mutex_exit(&os->os_lock); 473503ad85cSMatthew Ahrens return (list_head(&os->os_dnodes) != os->os_meta_dnode); 474ea8dc4b6Seschrock } 475ea8dc4b6Seschrock 476fa9e4066Sahrens void 477503ad85cSMatthew Ahrens dmu_objset_evict(objset_t *os) 478fa9e4066Sahrens { 479503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 480fa9e4066Sahrens 481b24ab676SJeff Bonwick for (int t = 0; t < TXG_SIZE; t++) 482b24ab676SJeff Bonwick ASSERT(!dmu_objset_is_dirty(os, t)); 483fa9e4066Sahrens 4843baa08fcSek if (ds) { 4853baa08fcSek if (!dsl_dataset_is_snapshot(ds)) { 4863baa08fcSek VERIFY(0 == dsl_prop_unregister(ds, "checksum", 487503ad85cSMatthew Ahrens checksum_changed_cb, os)); 4883baa08fcSek VERIFY(0 == dsl_prop_unregister(ds, "compression", 489503ad85cSMatthew Ahrens compression_changed_cb, os)); 4903baa08fcSek VERIFY(0 == dsl_prop_unregister(ds, "copies", 491503ad85cSMatthew Ahrens copies_changed_cb, os)); 492b24ab676SJeff Bonwick VERIFY(0 == dsl_prop_unregister(ds, "dedup", 493b24ab676SJeff Bonwick dedup_changed_cb, os)); 494e09fa4daSNeil Perrin VERIFY(0 == dsl_prop_unregister(ds, "logbias", 495e09fa4daSNeil Perrin logbias_changed_cb, os)); 4963baa08fcSek } 4973baa08fcSek VERIFY(0 == dsl_prop_unregister(ds, "primarycache", 498503ad85cSMatthew Ahrens primary_cache_changed_cb, os)); 4993baa08fcSek VERIFY(0 == dsl_prop_unregister(ds, "secondarycache", 500503ad85cSMatthew Ahrens secondary_cache_changed_cb, os)); 501fa9e4066Sahrens } 502fa9e4066Sahrens 5030a586ceaSMark Shellenbaum if (os->os_sa) 5040a586ceaSMark Shellenbaum sa_tear_down(os); 5050a586ceaSMark Shellenbaum 506ea8dc4b6Seschrock /* 507ea8dc4b6Seschrock * We should need only a single pass over the dnode list, since 508ea8dc4b6Seschrock * nothing can be added to the list at this point. 509ea8dc4b6Seschrock */ 510503ad85cSMatthew Ahrens (void) dmu_objset_evict_dbufs(os); 511ea8dc4b6Seschrock 512503ad85cSMatthew Ahrens dnode_special_close(os->os_meta_dnode); 513503ad85cSMatthew Ahrens if (os->os_userused_dnode) { 514503ad85cSMatthew Ahrens dnode_special_close(os->os_userused_dnode); 515503ad85cSMatthew Ahrens dnode_special_close(os->os_groupused_dnode); 51614843421SMatthew Ahrens } 517503ad85cSMatthew Ahrens zil_free(os->os_zil); 518fa9e4066Sahrens 519503ad85cSMatthew Ahrens ASSERT3P(list_head(&os->os_dnodes), ==, NULL); 52014843421SMatthew Ahrens 521503ad85cSMatthew Ahrens VERIFY(arc_buf_remove_ref(os->os_phys_buf, &os->os_phys_buf) == 1); 522503ad85cSMatthew Ahrens mutex_destroy(&os->os_lock); 523503ad85cSMatthew Ahrens mutex_destroy(&os->os_obj_lock); 524503ad85cSMatthew Ahrens mutex_destroy(&os->os_user_ptr_lock); 525503ad85cSMatthew Ahrens kmem_free(os, sizeof (objset_t)); 526fa9e4066Sahrens } 527fa9e4066Sahrens 52871eb0538SChris Kirby timestruc_t 52971eb0538SChris Kirby dmu_objset_snap_cmtime(objset_t *os) 53071eb0538SChris Kirby { 53171eb0538SChris Kirby return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir)); 53271eb0538SChris Kirby } 53371eb0538SChris Kirby 534fa9e4066Sahrens /* called from dsl for meta-objset */ 535503ad85cSMatthew Ahrens objset_t * 536c717a561Smaybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, 537c717a561Smaybee dmu_objset_type_t type, dmu_tx_t *tx) 538fa9e4066Sahrens { 539503ad85cSMatthew Ahrens objset_t *os; 540fa9e4066Sahrens dnode_t *mdn; 541fa9e4066Sahrens 542fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 54391ebeef5Sahrens if (ds) 54491ebeef5Sahrens mutex_enter(&ds->ds_opening_lock); 545503ad85cSMatthew Ahrens VERIFY(0 == dmu_objset_open_impl(spa, ds, bp, &os)); 54691ebeef5Sahrens if (ds) 54791ebeef5Sahrens mutex_exit(&ds->ds_opening_lock); 548503ad85cSMatthew Ahrens mdn = os->os_meta_dnode; 549fa9e4066Sahrens 550fa9e4066Sahrens dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT, 551fa9e4066Sahrens DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx); 552fa9e4066Sahrens 553fa9e4066Sahrens /* 554fa9e4066Sahrens * We don't want to have to increase the meta-dnode's nlevels 555fa9e4066Sahrens * later, because then we could do it in quescing context while 556fa9e4066Sahrens * we are also accessing it in open context. 557fa9e4066Sahrens * 558fa9e4066Sahrens * This precaution is not necessary for the MOS (ds == NULL), 559fa9e4066Sahrens * because the MOS is only updated in syncing context. 560fa9e4066Sahrens * This is most fortunate: the MOS is the only objset that 561fa9e4066Sahrens * needs to be synced multiple times as spa_sync() iterates 562fa9e4066Sahrens * to convergence, so minimizing its dn_nlevels matters. 563fa9e4066Sahrens */ 564ea8dc4b6Seschrock if (ds != NULL) { 565ea8dc4b6Seschrock int levels = 1; 566ea8dc4b6Seschrock 567ea8dc4b6Seschrock /* 568ea8dc4b6Seschrock * Determine the number of levels necessary for the meta-dnode 569ea8dc4b6Seschrock * to contain DN_MAX_OBJECT dnodes. 570ea8dc4b6Seschrock */ 571ea8dc4b6Seschrock while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift + 572ea8dc4b6Seschrock (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) < 573ea8dc4b6Seschrock DN_MAX_OBJECT * sizeof (dnode_phys_t)) 574ea8dc4b6Seschrock levels++; 575ea8dc4b6Seschrock 576fa9e4066Sahrens mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] = 577ea8dc4b6Seschrock mdn->dn_nlevels = levels; 578ea8dc4b6Seschrock } 579fa9e4066Sahrens 580fa9e4066Sahrens ASSERT(type != DMU_OST_NONE); 581fa9e4066Sahrens ASSERT(type != DMU_OST_ANY); 582fa9e4066Sahrens ASSERT(type < DMU_OST_NUMTYPES); 583503ad85cSMatthew Ahrens os->os_phys->os_type = type; 584503ad85cSMatthew Ahrens if (dmu_objset_userused_enabled(os)) { 585503ad85cSMatthew Ahrens os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE; 586503ad85cSMatthew Ahrens os->os_flags = os->os_phys->os_flags; 58714843421SMatthew Ahrens } 588fa9e4066Sahrens 589fa9e4066Sahrens dsl_dataset_dirty(ds, tx); 590fa9e4066Sahrens 591503ad85cSMatthew Ahrens return (os); 592fa9e4066Sahrens } 593fa9e4066Sahrens 594fa9e4066Sahrens struct oscarg { 595ecd6cf80Smarks void (*userfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx); 596fa9e4066Sahrens void *userarg; 597ae46e4c7SMatthew Ahrens dsl_dataset_t *clone_origin; 598fa9e4066Sahrens const char *lastname; 599fa9e4066Sahrens dmu_objset_type_t type; 600ab04eb8eStimh uint64_t flags; 601fa9e4066Sahrens }; 602fa9e4066Sahrens 603ecd6cf80Smarks /*ARGSUSED*/ 604fa9e4066Sahrens static int 6051d452cf5Sahrens dmu_objset_create_check(void *arg1, void *arg2, dmu_tx_t *tx) 606fa9e4066Sahrens { 6071d452cf5Sahrens dsl_dir_t *dd = arg1; 6081d452cf5Sahrens struct oscarg *oa = arg2; 6091d452cf5Sahrens objset_t *mos = dd->dd_pool->dp_meta_objset; 610fa9e4066Sahrens int err; 6111d452cf5Sahrens uint64_t ddobj; 6121d452cf5Sahrens 6131d452cf5Sahrens err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj, 6141d452cf5Sahrens oa->lastname, sizeof (uint64_t), 1, &ddobj); 6151d452cf5Sahrens if (err != ENOENT) 6161d452cf5Sahrens return (err ? err : EEXIST); 6171d452cf5Sahrens 618ae46e4c7SMatthew Ahrens if (oa->clone_origin != NULL) { 619ae46e4c7SMatthew Ahrens /* You can't clone across pools. */ 620ae46e4c7SMatthew Ahrens if (oa->clone_origin->ds_dir->dd_pool != dd->dd_pool) 6211d452cf5Sahrens return (EXDEV); 6221d452cf5Sahrens 623ae46e4c7SMatthew Ahrens /* You can only clone snapshots, not the head datasets. */ 624ae46e4c7SMatthew Ahrens if (!dsl_dataset_is_snapshot(oa->clone_origin)) 6251d452cf5Sahrens return (EINVAL); 6261d452cf5Sahrens } 627ecd6cf80Smarks 6281d452cf5Sahrens return (0); 6291d452cf5Sahrens } 6301d452cf5Sahrens 6311d452cf5Sahrens static void 632ecd6cf80Smarks dmu_objset_create_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 6331d452cf5Sahrens { 6341d452cf5Sahrens dsl_dir_t *dd = arg1; 6351d452cf5Sahrens struct oscarg *oa = arg2; 6361d452cf5Sahrens uint64_t dsobj; 637fa9e4066Sahrens 638fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 639fa9e4066Sahrens 6401d452cf5Sahrens dsobj = dsl_dataset_create_sync(dd, oa->lastname, 641ae46e4c7SMatthew Ahrens oa->clone_origin, oa->flags, cr, tx); 642fa9e4066Sahrens 643ae46e4c7SMatthew Ahrens if (oa->clone_origin == NULL) { 644ae46e4c7SMatthew Ahrens dsl_dataset_t *ds; 645ae46e4c7SMatthew Ahrens blkptr_t *bp; 646503ad85cSMatthew Ahrens objset_t *os; 647fa9e4066Sahrens 648ae46e4c7SMatthew Ahrens VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, dsobj, 649ae46e4c7SMatthew Ahrens FTAG, &ds)); 650ae46e4c7SMatthew Ahrens bp = dsl_dataset_get_blkptr(ds); 651ae46e4c7SMatthew Ahrens ASSERT(BP_IS_HOLE(bp)); 652ae46e4c7SMatthew Ahrens 653503ad85cSMatthew Ahrens os = dmu_objset_create_impl(dsl_dataset_get_spa(ds), 654c717a561Smaybee ds, bp, oa->type, tx); 655fa9e4066Sahrens 656fa9e4066Sahrens if (oa->userfunc) 657503ad85cSMatthew Ahrens oa->userfunc(os, oa->userarg, cr, tx); 658ae46e4c7SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 659fa9e4066Sahrens } 660ecd6cf80Smarks 661ecd6cf80Smarks spa_history_internal_log(LOG_DS_CREATE, dd->dd_pool->dp_spa, 662ecd6cf80Smarks tx, cr, "dataset = %llu", dsobj); 663fa9e4066Sahrens } 664fa9e4066Sahrens 665fa9e4066Sahrens int 666ae46e4c7SMatthew Ahrens dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags, 667ecd6cf80Smarks void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg) 668fa9e4066Sahrens { 6691d452cf5Sahrens dsl_dir_t *pdd; 670fa9e4066Sahrens const char *tail; 671fa9e4066Sahrens int err = 0; 6721d452cf5Sahrens struct oscarg oa = { 0 }; 673fa9e4066Sahrens 6741d452cf5Sahrens ASSERT(strchr(name, '@') == NULL); 6751d452cf5Sahrens err = dsl_dir_open(name, FTAG, &pdd, &tail); 676ea8dc4b6Seschrock if (err) 677ea8dc4b6Seschrock return (err); 678fa9e4066Sahrens if (tail == NULL) { 6791d452cf5Sahrens dsl_dir_close(pdd, FTAG); 680fa9e4066Sahrens return (EEXIST); 681fa9e4066Sahrens } 682fa9e4066Sahrens 6831d452cf5Sahrens oa.userfunc = func; 6841d452cf5Sahrens oa.userarg = arg; 6851d452cf5Sahrens oa.lastname = tail; 6861d452cf5Sahrens oa.type = type; 687ab04eb8eStimh oa.flags = flags; 688ecd6cf80Smarks 689ae46e4c7SMatthew Ahrens err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check, 690ae46e4c7SMatthew Ahrens dmu_objset_create_sync, pdd, &oa, 5); 691ae46e4c7SMatthew Ahrens dsl_dir_close(pdd, FTAG); 692ae46e4c7SMatthew Ahrens return (err); 693ae46e4c7SMatthew Ahrens } 694ae46e4c7SMatthew Ahrens 695ae46e4c7SMatthew Ahrens int 696ae46e4c7SMatthew Ahrens dmu_objset_clone(const char *name, dsl_dataset_t *clone_origin, uint64_t flags) 697ae46e4c7SMatthew Ahrens { 698ae46e4c7SMatthew Ahrens dsl_dir_t *pdd; 699ae46e4c7SMatthew Ahrens const char *tail; 700ae46e4c7SMatthew Ahrens int err = 0; 701ae46e4c7SMatthew Ahrens struct oscarg oa = { 0 }; 702ae46e4c7SMatthew Ahrens 703ae46e4c7SMatthew Ahrens ASSERT(strchr(name, '@') == NULL); 704ae46e4c7SMatthew Ahrens err = dsl_dir_open(name, FTAG, &pdd, &tail); 705ae46e4c7SMatthew Ahrens if (err) 706ae46e4c7SMatthew Ahrens return (err); 707ae46e4c7SMatthew Ahrens if (tail == NULL) { 708ae46e4c7SMatthew Ahrens dsl_dir_close(pdd, FTAG); 709ae46e4c7SMatthew Ahrens return (EEXIST); 710fa9e4066Sahrens } 711ae46e4c7SMatthew Ahrens 712ae46e4c7SMatthew Ahrens oa.lastname = tail; 713ae46e4c7SMatthew Ahrens oa.clone_origin = clone_origin; 714ae46e4c7SMatthew Ahrens oa.flags = flags; 715ae46e4c7SMatthew Ahrens 7161d452cf5Sahrens err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check, 7171d452cf5Sahrens dmu_objset_create_sync, pdd, &oa, 5); 7181d452cf5Sahrens dsl_dir_close(pdd, FTAG); 719fa9e4066Sahrens return (err); 720fa9e4066Sahrens } 721fa9e4066Sahrens 722fa9e4066Sahrens int 723842727c2SChris Kirby dmu_objset_destroy(const char *name, boolean_t defer) 724fa9e4066Sahrens { 725503ad85cSMatthew Ahrens dsl_dataset_t *ds; 726fa9e4066Sahrens int error; 727fa9e4066Sahrens 728fa9e4066Sahrens /* 729ae46e4c7SMatthew Ahrens * dsl_dataset_destroy() can free any claimed-but-unplayed 730ae46e4c7SMatthew Ahrens * intent log, but if there is an active log, it has blocks that 731ae46e4c7SMatthew Ahrens * are allocated, but may not yet be reflected in the on-disk 732ae46e4c7SMatthew Ahrens * structure. Only the ZIL knows how to free them, so we have 733ae46e4c7SMatthew Ahrens * to call into it here. 734fa9e4066Sahrens */ 735503ad85cSMatthew Ahrens error = dsl_dataset_own(name, B_TRUE, FTAG, &ds); 736fa9e4066Sahrens if (error == 0) { 737503ad85cSMatthew Ahrens objset_t *os; 738503ad85cSMatthew Ahrens if (dmu_objset_from_ds(ds, &os) == 0) 739503ad85cSMatthew Ahrens zil_destroy(dmu_objset_zil(os), B_FALSE); 740503ad85cSMatthew Ahrens error = dsl_dataset_destroy(ds, FTAG, defer); 741ae46e4c7SMatthew Ahrens /* dsl_dataset_destroy() closes the ds. */ 742fa9e4066Sahrens } 743fa9e4066Sahrens 7443cb34c60Sahrens return (error); 745fa9e4066Sahrens } 746fa9e4066Sahrens 7471d452cf5Sahrens struct snaparg { 7481d452cf5Sahrens dsl_sync_task_group_t *dstg; 7491d452cf5Sahrens char *snapname; 7501d452cf5Sahrens char failed[MAXPATHLEN]; 751d8d780fbSMatthew Ahrens boolean_t recursive; 752ea2f5b9eSMatthew Ahrens nvlist_t *props; 7533cb34c60Sahrens }; 7543cb34c60Sahrens 755ea2f5b9eSMatthew Ahrens static int 756ea2f5b9eSMatthew Ahrens snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx) 757ea2f5b9eSMatthew Ahrens { 758ea2f5b9eSMatthew Ahrens objset_t *os = arg1; 759ea2f5b9eSMatthew Ahrens struct snaparg *sn = arg2; 760ea2f5b9eSMatthew Ahrens 761ea2f5b9eSMatthew Ahrens /* The props have already been checked by zfs_check_userprops(). */ 762ea2f5b9eSMatthew Ahrens 763503ad85cSMatthew Ahrens return (dsl_dataset_snapshot_check(os->os_dsl_dataset, 764ea2f5b9eSMatthew Ahrens sn->snapname, tx)); 765ea2f5b9eSMatthew Ahrens } 766ea2f5b9eSMatthew Ahrens 767ea2f5b9eSMatthew Ahrens static void 768ea2f5b9eSMatthew Ahrens snapshot_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 769ea2f5b9eSMatthew Ahrens { 770ea2f5b9eSMatthew Ahrens objset_t *os = arg1; 771503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 772ea2f5b9eSMatthew Ahrens struct snaparg *sn = arg2; 773ea2f5b9eSMatthew Ahrens 774ea2f5b9eSMatthew Ahrens dsl_dataset_snapshot_sync(ds, sn->snapname, cr, tx); 775ea2f5b9eSMatthew Ahrens 77692241e0bSTom Erickson if (sn->props) { 77792241e0bSTom Erickson dsl_props_arg_t pa; 77892241e0bSTom Erickson pa.pa_props = sn->props; 77992241e0bSTom Erickson pa.pa_source = ZPROP_SRC_LOCAL; 78092241e0bSTom Erickson dsl_props_set_sync(ds->ds_prev, &pa, cr, tx); 78192241e0bSTom Erickson } 782ea2f5b9eSMatthew Ahrens } 7831d452cf5Sahrens 7841d452cf5Sahrens static int 785fd136879SMatthew Ahrens dmu_objset_snapshot_one(const char *name, void *arg) 7861d452cf5Sahrens { 7871d452cf5Sahrens struct snaparg *sn = arg; 7881d452cf5Sahrens objset_t *os; 7891d452cf5Sahrens int err; 790d8d780fbSMatthew Ahrens char *cp; 791d8d780fbSMatthew Ahrens 792d8d780fbSMatthew Ahrens /* 793d8d780fbSMatthew Ahrens * If the objset starts with a '%', then ignore it unless it was 794d8d780fbSMatthew Ahrens * explicitly named (ie, not recursive). These hidden datasets 795d8d780fbSMatthew Ahrens * are always inconsistent, and by not opening them here, we can 796d8d780fbSMatthew Ahrens * avoid a race with dsl_dir_destroy_check(). 797d8d780fbSMatthew Ahrens */ 798d8d780fbSMatthew Ahrens cp = strrchr(name, '/'); 799d8d780fbSMatthew Ahrens if (cp && cp[1] == '%' && sn->recursive) 800d8d780fbSMatthew Ahrens return (0); 8011d452cf5Sahrens 8021d452cf5Sahrens (void) strcpy(sn->failed, name); 8031d452cf5Sahrens 804ecd6cf80Smarks /* 805d8d780fbSMatthew Ahrens * Check permissions if we are doing a recursive snapshot. The 806d8d780fbSMatthew Ahrens * permission checks for the starting dataset have already been 807d8d780fbSMatthew Ahrens * performed in zfs_secpolicy_snapshot() 808ecd6cf80Smarks */ 809d8d780fbSMatthew Ahrens if (sn->recursive && (err = zfs_secpolicy_snapshot_perms(name, CRED()))) 810ecd6cf80Smarks return (err); 811ecd6cf80Smarks 812503ad85cSMatthew Ahrens err = dmu_objset_hold(name, sn, &os); 8131d452cf5Sahrens if (err != 0) 8141d452cf5Sahrens return (err); 8151d452cf5Sahrens 816d8d780fbSMatthew Ahrens /* 817d8d780fbSMatthew Ahrens * If the objset is in an inconsistent state (eg, in the process 818d8d780fbSMatthew Ahrens * of being destroyed), don't snapshot it. As with %hidden 819d8d780fbSMatthew Ahrens * datasets, we return EBUSY if this name was explicitly 820d8d780fbSMatthew Ahrens * requested (ie, not recursive), and otherwise ignore it. 821d8d780fbSMatthew Ahrens */ 822503ad85cSMatthew Ahrens if (os->os_dsl_dataset->ds_phys->ds_flags & DS_FLAG_INCONSISTENT) { 823503ad85cSMatthew Ahrens dmu_objset_rele(os, sn); 824d8d780fbSMatthew Ahrens return (sn->recursive ? 0 : EBUSY); 825f2e10be3Srm } 826f2e10be3Srm 8271d452cf5Sahrens /* 8281d452cf5Sahrens * NB: we need to wait for all in-flight changes to get to disk, 8291d452cf5Sahrens * so that we snapshot those changes. zil_suspend does this as 8301d452cf5Sahrens * a side effect. 8311d452cf5Sahrens */ 8321d452cf5Sahrens err = zil_suspend(dmu_objset_zil(os)); 8331d452cf5Sahrens if (err == 0) { 834ea2f5b9eSMatthew Ahrens dsl_sync_task_create(sn->dstg, snapshot_check, 835ea2f5b9eSMatthew Ahrens snapshot_sync, os, sn, 3); 836f2e10be3Srm } else { 837503ad85cSMatthew Ahrens dmu_objset_rele(os, sn); 8381d452cf5Sahrens } 839f2e10be3Srm 8401d452cf5Sahrens return (err); 8411d452cf5Sahrens } 8421d452cf5Sahrens 8431d452cf5Sahrens int 844ea2f5b9eSMatthew Ahrens dmu_objset_snapshot(char *fsname, char *snapname, 845ea2f5b9eSMatthew Ahrens nvlist_t *props, boolean_t recursive) 8461d452cf5Sahrens { 8471d452cf5Sahrens dsl_sync_task_t *dst; 848ea2f5b9eSMatthew Ahrens struct snaparg sn; 8491d452cf5Sahrens spa_t *spa; 8501d452cf5Sahrens int err; 8511d452cf5Sahrens 8521d452cf5Sahrens (void) strcpy(sn.failed, fsname); 8531d452cf5Sahrens 85440feaa91Sahrens err = spa_open(fsname, &spa, FTAG); 8551d452cf5Sahrens if (err) 8561d452cf5Sahrens return (err); 8571d452cf5Sahrens 8581d452cf5Sahrens sn.dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 8591d452cf5Sahrens sn.snapname = snapname; 860ea2f5b9eSMatthew Ahrens sn.props = props; 861d8d780fbSMatthew Ahrens sn.recursive = recursive; 8621d452cf5Sahrens 8630b69c2f0Sahrens if (recursive) { 8640b69c2f0Sahrens err = dmu_objset_find(fsname, 8650b69c2f0Sahrens dmu_objset_snapshot_one, &sn, DS_FIND_CHILDREN); 8660b69c2f0Sahrens } else { 8671d452cf5Sahrens err = dmu_objset_snapshot_one(fsname, &sn); 8680b69c2f0Sahrens } 8691d452cf5Sahrens 870ea2f5b9eSMatthew Ahrens if (err == 0) 871ea2f5b9eSMatthew Ahrens err = dsl_sync_task_group_wait(sn.dstg); 8721d452cf5Sahrens 8731d452cf5Sahrens for (dst = list_head(&sn.dstg->dstg_tasks); dst; 8741d452cf5Sahrens dst = list_next(&sn.dstg->dstg_tasks, dst)) { 875ea2f5b9eSMatthew Ahrens objset_t *os = dst->dst_arg1; 876503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 8771d452cf5Sahrens if (dst->dst_err) 8783cb34c60Sahrens dsl_dataset_name(ds, sn.failed); 879ea2f5b9eSMatthew Ahrens zil_resume(dmu_objset_zil(os)); 880503ad85cSMatthew Ahrens dmu_objset_rele(os, &sn); 8813cb34c60Sahrens } 8823cb34c60Sahrens 8831d452cf5Sahrens if (err) 8841d452cf5Sahrens (void) strcpy(fsname, sn.failed); 8851d452cf5Sahrens dsl_sync_task_group_destroy(sn.dstg); 8861d452cf5Sahrens spa_close(spa, FTAG); 8871d452cf5Sahrens return (err); 8881d452cf5Sahrens } 8891d452cf5Sahrens 890fa9e4066Sahrens static void 89114843421SMatthew Ahrens dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx) 892fa9e4066Sahrens { 893c717a561Smaybee dnode_t *dn; 894faafa6e3Sahrens 895c717a561Smaybee while (dn = list_head(list)) { 896c717a561Smaybee ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT); 897c717a561Smaybee ASSERT(dn->dn_dbuf->db_data_pending); 898c717a561Smaybee /* 89914843421SMatthew Ahrens * Initialize dn_zio outside dnode_sync() because the 90014843421SMatthew Ahrens * meta-dnode needs to set it ouside dnode_sync(). 901c717a561Smaybee */ 902c717a561Smaybee dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio; 903c717a561Smaybee ASSERT(dn->dn_zio); 904faafa6e3Sahrens 905c717a561Smaybee ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS); 906c717a561Smaybee list_remove(list, dn); 90714843421SMatthew Ahrens 90814843421SMatthew Ahrens if (newlist) { 90914843421SMatthew Ahrens (void) dnode_add_ref(dn, newlist); 91014843421SMatthew Ahrens list_insert_tail(newlist, dn); 91114843421SMatthew Ahrens } 91214843421SMatthew Ahrens 913c717a561Smaybee dnode_sync(dn, tx); 914fa9e4066Sahrens } 915fa9e4066Sahrens } 916fa9e4066Sahrens 917fa9e4066Sahrens /* ARGSUSED */ 918fa9e4066Sahrens static void 919b24ab676SJeff Bonwick dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg) 920fa9e4066Sahrens { 921e14bb325SJeff Bonwick blkptr_t *bp = zio->io_bp; 922503ad85cSMatthew Ahrens objset_t *os = arg; 923c717a561Smaybee dnode_phys_t *dnp = &os->os_phys->os_meta_dnode; 924fa9e4066Sahrens 925e14bb325SJeff Bonwick ASSERT(bp == os->os_rootbp); 926e14bb325SJeff Bonwick ASSERT(BP_GET_TYPE(bp) == DMU_OT_OBJSET); 927e14bb325SJeff Bonwick ASSERT(BP_GET_LEVEL(bp) == 0); 9280a4e9518Sgw 929fa9e4066Sahrens /* 93014843421SMatthew Ahrens * Update rootbp fill count: it should be the number of objects 93114843421SMatthew Ahrens * allocated in the object set (not counting the "special" 93214843421SMatthew Ahrens * objects that are stored in the objset_phys_t -- the meta 93314843421SMatthew Ahrens * dnode and user/group accounting objects). 934fa9e4066Sahrens */ 93514843421SMatthew Ahrens bp->blk_fill = 0; 936e14bb325SJeff Bonwick for (int i = 0; i < dnp->dn_nblkptr; i++) 937c717a561Smaybee bp->blk_fill += dnp->dn_blkptr[i].blk_fill; 938b24ab676SJeff Bonwick } 939b24ab676SJeff Bonwick 940b24ab676SJeff Bonwick /* ARGSUSED */ 941b24ab676SJeff Bonwick static void 942b24ab676SJeff Bonwick dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg) 943b24ab676SJeff Bonwick { 944b24ab676SJeff Bonwick blkptr_t *bp = zio->io_bp; 945b24ab676SJeff Bonwick blkptr_t *bp_orig = &zio->io_bp_orig; 946b24ab676SJeff Bonwick objset_t *os = arg; 9470a4e9518Sgw 948e14bb325SJeff Bonwick if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { 949b24ab676SJeff Bonwick ASSERT(BP_EQUAL(bp, bp_orig)); 950e14bb325SJeff Bonwick } else { 951b24ab676SJeff Bonwick dsl_dataset_t *ds = os->os_dsl_dataset; 952b24ab676SJeff Bonwick dmu_tx_t *tx = os->os_synctx; 953b24ab676SJeff Bonwick 954b24ab676SJeff Bonwick (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE); 955b24ab676SJeff Bonwick dsl_dataset_block_born(ds, bp, tx); 9560a4e9518Sgw } 957c717a561Smaybee } 958c717a561Smaybee 959fa9e4066Sahrens /* called from dsl */ 960fa9e4066Sahrens void 961503ad85cSMatthew Ahrens dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx) 962fa9e4066Sahrens { 963fa9e4066Sahrens int txgoff; 964ea8dc4b6Seschrock zbookmark_t zb; 965b24ab676SJeff Bonwick zio_prop_t zp; 966c717a561Smaybee zio_t *zio; 967c717a561Smaybee list_t *list; 96814843421SMatthew Ahrens list_t *newlist = NULL; 969c717a561Smaybee dbuf_dirty_record_t *dr; 970c717a561Smaybee 971c717a561Smaybee dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg); 972fa9e4066Sahrens 973fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 974fa9e4066Sahrens /* XXX the write_done callback should really give us the tx... */ 975fa9e4066Sahrens os->os_synctx = tx; 976fa9e4066Sahrens 97787bd5c1eSahrens if (os->os_dsl_dataset == NULL) { 97887bd5c1eSahrens /* 97987bd5c1eSahrens * This is the MOS. If we have upgraded, 98087bd5c1eSahrens * spa_max_replication() could change, so reset 98187bd5c1eSahrens * os_copies here. 98287bd5c1eSahrens */ 98387bd5c1eSahrens os->os_copies = spa_max_replication(os->os_spa); 98487bd5c1eSahrens } 98587bd5c1eSahrens 986fa9e4066Sahrens /* 987c717a561Smaybee * Create the root block IO 988fa9e4066Sahrens */ 989088f3894Sahrens arc_release(os->os_phys_buf, &os->os_phys_buf); 99014843421SMatthew Ahrens 991b24ab676SJeff Bonwick SET_BOOKMARK(&zb, os->os_dsl_dataset ? 992b24ab676SJeff Bonwick os->os_dsl_dataset->ds_object : DMU_META_OBJSET, 993b24ab676SJeff Bonwick ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 994b24ab676SJeff Bonwick 995b24ab676SJeff Bonwick dmu_write_policy(os, NULL, 0, 0, &zp); 996b24ab676SJeff Bonwick 997b24ab676SJeff Bonwick zio = arc_write(pio, os->os_spa, tx->tx_txg, 998b24ab676SJeff Bonwick os->os_rootbp, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os), &zp, 999b24ab676SJeff Bonwick dmu_objset_write_ready, dmu_objset_write_done, os, 1000e14bb325SJeff Bonwick ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb); 1001c717a561Smaybee 1002c717a561Smaybee /* 100314843421SMatthew Ahrens * Sync special dnodes - the parent IO for the sync is the root block 1004c717a561Smaybee */ 1005c717a561Smaybee os->os_meta_dnode->dn_zio = zio; 1006c717a561Smaybee dnode_sync(os->os_meta_dnode, tx); 1007c717a561Smaybee 100814843421SMatthew Ahrens os->os_phys->os_flags = os->os_flags; 100914843421SMatthew Ahrens 101014843421SMatthew Ahrens if (os->os_userused_dnode && 101114843421SMatthew Ahrens os->os_userused_dnode->dn_type != DMU_OT_NONE) { 101214843421SMatthew Ahrens os->os_userused_dnode->dn_zio = zio; 101314843421SMatthew Ahrens dnode_sync(os->os_userused_dnode, tx); 101414843421SMatthew Ahrens os->os_groupused_dnode->dn_zio = zio; 101514843421SMatthew Ahrens dnode_sync(os->os_groupused_dnode, tx); 101614843421SMatthew Ahrens } 101714843421SMatthew Ahrens 1018c717a561Smaybee txgoff = tx->tx_txg & TXG_MASK; 1019fa9e4066Sahrens 102014843421SMatthew Ahrens if (dmu_objset_userused_enabled(os)) { 102114843421SMatthew Ahrens newlist = &os->os_synced_dnodes; 102214843421SMatthew Ahrens /* 102314843421SMatthew Ahrens * We must create the list here because it uses the 102414843421SMatthew Ahrens * dn_dirty_link[] of this txg. 102514843421SMatthew Ahrens */ 102614843421SMatthew Ahrens list_create(newlist, sizeof (dnode_t), 102714843421SMatthew Ahrens offsetof(dnode_t, dn_dirty_link[txgoff])); 102814843421SMatthew Ahrens } 102914843421SMatthew Ahrens 103014843421SMatthew Ahrens dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx); 103114843421SMatthew Ahrens dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx); 1032fa9e4066Sahrens 1033c717a561Smaybee list = &os->os_meta_dnode->dn_dirty_records[txgoff]; 1034c717a561Smaybee while (dr = list_head(list)) { 1035c717a561Smaybee ASSERT(dr->dr_dbuf->db_level == 0); 1036c717a561Smaybee list_remove(list, dr); 1037c717a561Smaybee if (dr->dr_zio) 1038c717a561Smaybee zio_nowait(dr->dr_zio); 1039c717a561Smaybee } 1040c717a561Smaybee /* 1041c717a561Smaybee * Free intent log blocks up to this tx. 1042c717a561Smaybee */ 1043c717a561Smaybee zil_sync(os->os_zil, tx); 1044088f3894Sahrens os->os_phys->os_zil_header = os->os_zil_header; 1045c717a561Smaybee zio_nowait(zio); 1046fa9e4066Sahrens } 1047fa9e4066Sahrens 1048b24ab676SJeff Bonwick boolean_t 1049b24ab676SJeff Bonwick dmu_objset_is_dirty(objset_t *os, uint64_t txg) 1050b24ab676SJeff Bonwick { 1051b24ab676SJeff Bonwick return (!list_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]) || 1052b24ab676SJeff Bonwick !list_is_empty(&os->os_free_dnodes[txg & TXG_MASK])); 1053b24ab676SJeff Bonwick } 1054b24ab676SJeff Bonwick 105514843421SMatthew Ahrens static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES]; 105614843421SMatthew Ahrens 105714843421SMatthew Ahrens void 105814843421SMatthew Ahrens dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb) 105914843421SMatthew Ahrens { 106014843421SMatthew Ahrens used_cbs[ost] = cb; 106114843421SMatthew Ahrens } 106214843421SMatthew Ahrens 106314843421SMatthew Ahrens boolean_t 1064503ad85cSMatthew Ahrens dmu_objset_userused_enabled(objset_t *os) 106514843421SMatthew Ahrens { 106614843421SMatthew Ahrens return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE && 106714843421SMatthew Ahrens used_cbs[os->os_phys->os_type] && 106814843421SMatthew Ahrens os->os_userused_dnode); 106914843421SMatthew Ahrens } 107014843421SMatthew Ahrens 10719966ca11SMatthew Ahrens static void 10720a586ceaSMark Shellenbaum do_userquota_update(objset_t *os, uint64_t used, uint64_t flags, 10730a586ceaSMark Shellenbaum uint64_t user, uint64_t group, boolean_t subtract, dmu_tx_t *tx) 10749966ca11SMatthew Ahrens { 10750a586ceaSMark Shellenbaum if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) { 10760a586ceaSMark Shellenbaum int64_t delta = DNODE_SIZE + used; 10779966ca11SMatthew Ahrens if (subtract) 10789966ca11SMatthew Ahrens delta = -delta; 1079c33e334fSMatthew Ahrens VERIFY3U(0, ==, zap_increment_int(os, DMU_USERUSED_OBJECT, 10809966ca11SMatthew Ahrens user, delta, tx)); 1081c33e334fSMatthew Ahrens VERIFY3U(0, ==, zap_increment_int(os, DMU_GROUPUSED_OBJECT, 10829966ca11SMatthew Ahrens group, delta, tx)); 10839966ca11SMatthew Ahrens } 10849966ca11SMatthew Ahrens } 10859966ca11SMatthew Ahrens 108614843421SMatthew Ahrens void 10870a586ceaSMark Shellenbaum dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx) 108814843421SMatthew Ahrens { 108914843421SMatthew Ahrens dnode_t *dn; 109014843421SMatthew Ahrens list_t *list = &os->os_synced_dnodes; 109114843421SMatthew Ahrens 109214843421SMatthew Ahrens ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os)); 109314843421SMatthew Ahrens 109414843421SMatthew Ahrens while (dn = list_head(list)) { 109514843421SMatthew Ahrens ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object)); 109614843421SMatthew Ahrens ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE || 109714843421SMatthew Ahrens dn->dn_phys->dn_flags & 109814843421SMatthew Ahrens DNODE_FLAG_USERUSED_ACCOUNTED); 109914843421SMatthew Ahrens 110014843421SMatthew Ahrens /* Allocate the user/groupused objects if necessary. */ 110114843421SMatthew Ahrens if (os->os_userused_dnode->dn_type == DMU_OT_NONE) { 1102503ad85cSMatthew Ahrens VERIFY(0 == zap_create_claim(os, 110314843421SMatthew Ahrens DMU_USERUSED_OBJECT, 110414843421SMatthew Ahrens DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx)); 1105503ad85cSMatthew Ahrens VERIFY(0 == zap_create_claim(os, 110614843421SMatthew Ahrens DMU_GROUPUSED_OBJECT, 110714843421SMatthew Ahrens DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx)); 110814843421SMatthew Ahrens } 110914843421SMatthew Ahrens 111014843421SMatthew Ahrens /* 11119966ca11SMatthew Ahrens * We intentionally modify the zap object even if the 11120a586ceaSMark Shellenbaum * net delta is zero. Otherwise 11139966ca11SMatthew Ahrens * the block of the zap obj could be shared between 11149966ca11SMatthew Ahrens * datasets but need to be different between them after 11159966ca11SMatthew Ahrens * a bprewrite. 111614843421SMatthew Ahrens */ 111714843421SMatthew Ahrens 111814843421SMatthew Ahrens /* 111914843421SMatthew Ahrens * The mutex is needed here for interlock with dnode_allocate. 112014843421SMatthew Ahrens */ 112114843421SMatthew Ahrens mutex_enter(&dn->dn_mtx); 11220a586ceaSMark Shellenbaum ASSERT(dn->dn_id_flags); 11230a586ceaSMark Shellenbaum if (dn->dn_id_flags & DN_ID_OLD_EXIST) { 11240a586ceaSMark Shellenbaum do_userquota_update(os, dn->dn_oldused, dn->dn_oldflags, 11250a586ceaSMark Shellenbaum dn->dn_olduid, dn->dn_oldgid, B_TRUE, tx); 11260a586ceaSMark Shellenbaum } 11270a586ceaSMark Shellenbaum if (dn->dn_id_flags & DN_ID_NEW_EXIST) { 11280a586ceaSMark Shellenbaum do_userquota_update(os, DN_USED_BYTES(dn->dn_phys), 11290a586ceaSMark Shellenbaum dn->dn_phys->dn_flags, dn->dn_newuid, 11300a586ceaSMark Shellenbaum dn->dn_newgid, B_FALSE, tx); 11310a586ceaSMark Shellenbaum } 11320a586ceaSMark Shellenbaum 11330a586ceaSMark Shellenbaum dn->dn_oldused = 0; 11340a586ceaSMark Shellenbaum dn->dn_oldflags = 0; 11350a586ceaSMark Shellenbaum if (dn->dn_id_flags & DN_ID_NEW_EXIST) { 11360a586ceaSMark Shellenbaum dn->dn_olduid = dn->dn_newuid; 11370a586ceaSMark Shellenbaum dn->dn_oldgid = dn->dn_newgid; 11380a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_OLD_EXIST; 11390a586ceaSMark Shellenbaum if (dn->dn_bonuslen == 0) 11400a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_SPILL; 11410a586ceaSMark Shellenbaum else 11420a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_BONUS; 11430a586ceaSMark Shellenbaum } 11440a586ceaSMark Shellenbaum dn->dn_id_flags &= ~(DN_ID_NEW_EXIST|DN_ID_SYNC); 114514843421SMatthew Ahrens mutex_exit(&dn->dn_mtx); 114614843421SMatthew Ahrens 114714843421SMatthew Ahrens list_remove(list, dn); 114814843421SMatthew Ahrens dnode_rele(dn, list); 114914843421SMatthew Ahrens } 115014843421SMatthew Ahrens } 115114843421SMatthew Ahrens 1152*06e0070dSMark Shellenbaum /* 1153*06e0070dSMark Shellenbaum * Returns a pointer to data to find uid/gid from 1154*06e0070dSMark Shellenbaum * 1155*06e0070dSMark Shellenbaum * If a dirty record for transaction group that is syncing can't 1156*06e0070dSMark Shellenbaum * be found then NULL is returned. In the NULL case it is assumed 1157*06e0070dSMark Shellenbaum * the uid/gid aren't changing. 1158*06e0070dSMark Shellenbaum */ 1159*06e0070dSMark Shellenbaum static void * 1160*06e0070dSMark Shellenbaum dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx) 1161*06e0070dSMark Shellenbaum { 1162*06e0070dSMark Shellenbaum dbuf_dirty_record_t *dr, **drp; 1163*06e0070dSMark Shellenbaum void *data; 1164*06e0070dSMark Shellenbaum 1165*06e0070dSMark Shellenbaum if (db->db_dirtycnt == 0) 1166*06e0070dSMark Shellenbaum return (db->db.db_data); /* Nothing is changing */ 1167*06e0070dSMark Shellenbaum 1168*06e0070dSMark Shellenbaum for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next) 1169*06e0070dSMark Shellenbaum if (dr->dr_txg == tx->tx_txg) 1170*06e0070dSMark Shellenbaum break; 1171*06e0070dSMark Shellenbaum 1172*06e0070dSMark Shellenbaum if (dr == NULL) 1173*06e0070dSMark Shellenbaum data = NULL; 1174*06e0070dSMark Shellenbaum else if (dr->dr_dbuf->db_dnode->dn_bonuslen == 0 && 1175*06e0070dSMark Shellenbaum dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID) 1176*06e0070dSMark Shellenbaum data = dr->dt.dl.dr_data->b_data; 1177*06e0070dSMark Shellenbaum else 1178*06e0070dSMark Shellenbaum data = dr->dt.dl.dr_data; 1179*06e0070dSMark Shellenbaum return (data); 1180*06e0070dSMark Shellenbaum } 1181*06e0070dSMark Shellenbaum 11820a586ceaSMark Shellenbaum void 1183*06e0070dSMark Shellenbaum dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx) 11840a586ceaSMark Shellenbaum { 11850a586ceaSMark Shellenbaum objset_t *os = dn->dn_objset; 11860a586ceaSMark Shellenbaum void *data = NULL; 1187*06e0070dSMark Shellenbaum dmu_buf_impl_t *db = NULL; 11880a586ceaSMark Shellenbaum uint64_t *user, *group; 11890a586ceaSMark Shellenbaum int flags = dn->dn_id_flags; 11900a586ceaSMark Shellenbaum int error; 1191*06e0070dSMark Shellenbaum boolean_t have_spill = B_FALSE; 11920a586ceaSMark Shellenbaum 11930a586ceaSMark Shellenbaum if (!dmu_objset_userused_enabled(dn->dn_objset)) 11940a586ceaSMark Shellenbaum return; 11950a586ceaSMark Shellenbaum 11960a586ceaSMark Shellenbaum if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST| 11970a586ceaSMark Shellenbaum DN_ID_CHKED_SPILL))) 11980a586ceaSMark Shellenbaum return; 11990a586ceaSMark Shellenbaum 12000a586ceaSMark Shellenbaum if (before && dn->dn_bonuslen != 0) 12010a586ceaSMark Shellenbaum data = DN_BONUS(dn->dn_phys); 1202*06e0070dSMark Shellenbaum else if (!before && dn->dn_bonuslen != 0) { 1203*06e0070dSMark Shellenbaum if (dn->dn_bonus) { 1204*06e0070dSMark Shellenbaum db = dn->dn_bonus; 1205*06e0070dSMark Shellenbaum mutex_enter(&db->db_mtx); 1206*06e0070dSMark Shellenbaum data = dmu_objset_userquota_find_data(db, tx); 1207*06e0070dSMark Shellenbaum } else { 1208*06e0070dSMark Shellenbaum data = DN_BONUS(dn->dn_phys); 1209*06e0070dSMark Shellenbaum } 1210*06e0070dSMark Shellenbaum } else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) { 12110a586ceaSMark Shellenbaum int rf = 0; 12120a586ceaSMark Shellenbaum 12130a586ceaSMark Shellenbaum if (RW_WRITE_HELD(&dn->dn_struct_rwlock)) 12140a586ceaSMark Shellenbaum rf |= DB_RF_HAVESTRUCT; 1215*06e0070dSMark Shellenbaum error = dmu_spill_hold_by_dnode(dn, rf, 1216*06e0070dSMark Shellenbaum FTAG, (dmu_buf_t **)&db); 12170a586ceaSMark Shellenbaum ASSERT(error == 0); 1218*06e0070dSMark Shellenbaum mutex_enter(&db->db_mtx); 1219*06e0070dSMark Shellenbaum data = (before) ? db->db.db_data : 1220*06e0070dSMark Shellenbaum dmu_objset_userquota_find_data(db, tx); 1221*06e0070dSMark Shellenbaum have_spill = B_TRUE; 12220a586ceaSMark Shellenbaum } else { 12230a586ceaSMark Shellenbaum mutex_enter(&dn->dn_mtx); 12240a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_BONUS; 12250a586ceaSMark Shellenbaum mutex_exit(&dn->dn_mtx); 12260a586ceaSMark Shellenbaum return; 12270a586ceaSMark Shellenbaum } 12280a586ceaSMark Shellenbaum 12290a586ceaSMark Shellenbaum if (before) { 1230*06e0070dSMark Shellenbaum ASSERT(data); 12310a586ceaSMark Shellenbaum user = &dn->dn_olduid; 12320a586ceaSMark Shellenbaum group = &dn->dn_oldgid; 1233*06e0070dSMark Shellenbaum } else if (data) { 12340a586ceaSMark Shellenbaum user = &dn->dn_newuid; 12350a586ceaSMark Shellenbaum group = &dn->dn_newgid; 12360a586ceaSMark Shellenbaum } 12370a586ceaSMark Shellenbaum 1238*06e0070dSMark Shellenbaum /* 1239*06e0070dSMark Shellenbaum * Must always call the callback in case the object 1240*06e0070dSMark Shellenbaum * type has changed and that type isn't an object type to track 1241*06e0070dSMark Shellenbaum */ 12420a586ceaSMark Shellenbaum error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data, 12430a586ceaSMark Shellenbaum user, group); 12440a586ceaSMark Shellenbaum 1245*06e0070dSMark Shellenbaum /* 1246*06e0070dSMark Shellenbaum * Preserve existing uid/gid when the callback can't determine 1247*06e0070dSMark Shellenbaum * what the new uid/gid are and the callback returned EEXIST. 1248*06e0070dSMark Shellenbaum * The EEXIST error tells us to just use the existing uid/gid. 1249*06e0070dSMark Shellenbaum * If we don't know what the old values are then just assign 1250*06e0070dSMark Shellenbaum * them to 0, since that is a new file being created. 1251*06e0070dSMark Shellenbaum */ 1252*06e0070dSMark Shellenbaum if (!before && data == NULL && error == EEXIST) { 1253*06e0070dSMark Shellenbaum if (flags & DN_ID_OLD_EXIST) { 1254*06e0070dSMark Shellenbaum dn->dn_newuid = dn->dn_olduid; 1255*06e0070dSMark Shellenbaum dn->dn_newgid = dn->dn_oldgid; 1256*06e0070dSMark Shellenbaum } else { 1257*06e0070dSMark Shellenbaum dn->dn_newuid = 0; 1258*06e0070dSMark Shellenbaum dn->dn_newgid = 0; 1259*06e0070dSMark Shellenbaum } 1260*06e0070dSMark Shellenbaum error = 0; 1261*06e0070dSMark Shellenbaum } 1262*06e0070dSMark Shellenbaum 1263*06e0070dSMark Shellenbaum if (db) 1264*06e0070dSMark Shellenbaum mutex_exit(&db->db_mtx); 1265*06e0070dSMark Shellenbaum 12660a586ceaSMark Shellenbaum mutex_enter(&dn->dn_mtx); 12670a586ceaSMark Shellenbaum if (error == 0 && before) 12680a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_OLD_EXIST; 12690a586ceaSMark Shellenbaum if (error == 0 && !before) 12700a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_NEW_EXIST; 12710a586ceaSMark Shellenbaum 1272*06e0070dSMark Shellenbaum if (have_spill) { 12730a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_SPILL; 12740a586ceaSMark Shellenbaum } else { 12750a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_BONUS; 12760a586ceaSMark Shellenbaum } 12770a586ceaSMark Shellenbaum mutex_exit(&dn->dn_mtx); 1278*06e0070dSMark Shellenbaum if (have_spill) 1279*06e0070dSMark Shellenbaum dmu_buf_rele((dmu_buf_t *)db, FTAG); 12800a586ceaSMark Shellenbaum } 12810a586ceaSMark Shellenbaum 128214843421SMatthew Ahrens boolean_t 128314843421SMatthew Ahrens dmu_objset_userspace_present(objset_t *os) 128414843421SMatthew Ahrens { 1285503ad85cSMatthew Ahrens return (os->os_phys->os_flags & 128614843421SMatthew Ahrens OBJSET_FLAG_USERACCOUNTING_COMPLETE); 128714843421SMatthew Ahrens } 128814843421SMatthew Ahrens 128914843421SMatthew Ahrens int 129014843421SMatthew Ahrens dmu_objset_userspace_upgrade(objset_t *os) 129114843421SMatthew Ahrens { 129214843421SMatthew Ahrens uint64_t obj; 129314843421SMatthew Ahrens int err = 0; 129414843421SMatthew Ahrens 129514843421SMatthew Ahrens if (dmu_objset_userspace_present(os)) 129614843421SMatthew Ahrens return (0); 1297503ad85cSMatthew Ahrens if (!dmu_objset_userused_enabled(os)) 129814843421SMatthew Ahrens return (ENOTSUP); 129914843421SMatthew Ahrens if (dmu_objset_is_snapshot(os)) 130014843421SMatthew Ahrens return (EINVAL); 130114843421SMatthew Ahrens 130214843421SMatthew Ahrens /* 130314843421SMatthew Ahrens * We simply need to mark every object dirty, so that it will be 130414843421SMatthew Ahrens * synced out and now accounted. If this is called 130514843421SMatthew Ahrens * concurrently, or if we already did some work before crashing, 130614843421SMatthew Ahrens * that's fine, since we track each object's accounted state 130714843421SMatthew Ahrens * independently. 130814843421SMatthew Ahrens */ 130914843421SMatthew Ahrens 131014843421SMatthew Ahrens for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) { 131168857716SLin Ling dmu_tx_t *tx; 131214843421SMatthew Ahrens dmu_buf_t *db; 131314843421SMatthew Ahrens int objerr; 131414843421SMatthew Ahrens 131514843421SMatthew Ahrens if (issig(JUSTLOOKING) && issig(FORREAL)) 131614843421SMatthew Ahrens return (EINTR); 131714843421SMatthew Ahrens 131814843421SMatthew Ahrens objerr = dmu_bonus_hold(os, obj, FTAG, &db); 131914843421SMatthew Ahrens if (objerr) 132014843421SMatthew Ahrens continue; 132168857716SLin Ling tx = dmu_tx_create(os); 132214843421SMatthew Ahrens dmu_tx_hold_bonus(tx, obj); 132314843421SMatthew Ahrens objerr = dmu_tx_assign(tx, TXG_WAIT); 132414843421SMatthew Ahrens if (objerr) { 132514843421SMatthew Ahrens dmu_tx_abort(tx); 132614843421SMatthew Ahrens continue; 132714843421SMatthew Ahrens } 132814843421SMatthew Ahrens dmu_buf_will_dirty(db, tx); 132914843421SMatthew Ahrens dmu_buf_rele(db, FTAG); 133014843421SMatthew Ahrens dmu_tx_commit(tx); 133114843421SMatthew Ahrens } 133214843421SMatthew Ahrens 1333503ad85cSMatthew Ahrens os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE; 133414843421SMatthew Ahrens txg_wait_synced(dmu_objset_pool(os), 0); 133514843421SMatthew Ahrens return (0); 133614843421SMatthew Ahrens } 133714843421SMatthew Ahrens 1338fa9e4066Sahrens void 1339a2eea2e1Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp, 1340a2eea2e1Sahrens uint64_t *usedobjsp, uint64_t *availobjsp) 1341fa9e4066Sahrens { 1342503ad85cSMatthew Ahrens dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp, 1343a2eea2e1Sahrens usedobjsp, availobjsp); 1344a2eea2e1Sahrens } 1345a2eea2e1Sahrens 1346a2eea2e1Sahrens uint64_t 1347a2eea2e1Sahrens dmu_objset_fsid_guid(objset_t *os) 1348a2eea2e1Sahrens { 1349503ad85cSMatthew Ahrens return (dsl_dataset_fsid_guid(os->os_dsl_dataset)); 1350a2eea2e1Sahrens } 1351a2eea2e1Sahrens 1352a2eea2e1Sahrens void 1353a2eea2e1Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat) 1354a2eea2e1Sahrens { 1355503ad85cSMatthew Ahrens stat->dds_type = os->os_phys->os_type; 1356503ad85cSMatthew Ahrens if (os->os_dsl_dataset) 1357503ad85cSMatthew Ahrens dsl_dataset_fast_stat(os->os_dsl_dataset, stat); 1358a2eea2e1Sahrens } 1359a2eea2e1Sahrens 1360a2eea2e1Sahrens void 1361a2eea2e1Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv) 1362a2eea2e1Sahrens { 1363503ad85cSMatthew Ahrens ASSERT(os->os_dsl_dataset || 1364503ad85cSMatthew Ahrens os->os_phys->os_type == DMU_OST_META); 1365a2eea2e1Sahrens 1366503ad85cSMatthew Ahrens if (os->os_dsl_dataset != NULL) 1367503ad85cSMatthew Ahrens dsl_dataset_stats(os->os_dsl_dataset, nv); 1368a2eea2e1Sahrens 1369a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE, 1370503ad85cSMatthew Ahrens os->os_phys->os_type); 137114843421SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING, 137214843421SMatthew Ahrens dmu_objset_userspace_present(os)); 1373fa9e4066Sahrens } 1374fa9e4066Sahrens 1375fa9e4066Sahrens int 1376fa9e4066Sahrens dmu_objset_is_snapshot(objset_t *os) 1377fa9e4066Sahrens { 1378503ad85cSMatthew Ahrens if (os->os_dsl_dataset != NULL) 1379503ad85cSMatthew Ahrens return (dsl_dataset_is_snapshot(os->os_dsl_dataset)); 1380fa9e4066Sahrens else 1381fa9e4066Sahrens return (B_FALSE); 1382fa9e4066Sahrens } 1383fa9e4066Sahrens 1384ab04eb8eStimh int 1385ab04eb8eStimh dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen, 1386ab04eb8eStimh boolean_t *conflict) 1387ab04eb8eStimh { 1388503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 1389ab04eb8eStimh uint64_t ignored; 1390ab04eb8eStimh 1391ab04eb8eStimh if (ds->ds_phys->ds_snapnames_zapobj == 0) 1392ab04eb8eStimh return (ENOENT); 1393ab04eb8eStimh 1394ab04eb8eStimh return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset, 1395ab04eb8eStimh ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST, 1396ab04eb8eStimh real, maxlen, conflict)); 1397ab04eb8eStimh } 1398ab04eb8eStimh 1399fa9e4066Sahrens int 1400fa9e4066Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name, 1401b38f0970Sck uint64_t *idp, uint64_t *offp, boolean_t *case_conflict) 1402fa9e4066Sahrens { 1403503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 1404fa9e4066Sahrens zap_cursor_t cursor; 1405fa9e4066Sahrens zap_attribute_t attr; 1406fa9e4066Sahrens 1407fa9e4066Sahrens if (ds->ds_phys->ds_snapnames_zapobj == 0) 1408fa9e4066Sahrens return (ENOENT); 1409fa9e4066Sahrens 1410fa9e4066Sahrens zap_cursor_init_serialized(&cursor, 1411fa9e4066Sahrens ds->ds_dir->dd_pool->dp_meta_objset, 1412fa9e4066Sahrens ds->ds_phys->ds_snapnames_zapobj, *offp); 1413fa9e4066Sahrens 141487e5029aSahrens if (zap_cursor_retrieve(&cursor, &attr) != 0) { 141587e5029aSahrens zap_cursor_fini(&cursor); 141687e5029aSahrens return (ENOENT); 141787e5029aSahrens } 141887e5029aSahrens 141987e5029aSahrens if (strlen(attr.za_name) + 1 > namelen) { 142087e5029aSahrens zap_cursor_fini(&cursor); 142187e5029aSahrens return (ENAMETOOLONG); 142287e5029aSahrens } 142387e5029aSahrens 142487e5029aSahrens (void) strcpy(name, attr.za_name); 142587e5029aSahrens if (idp) 142687e5029aSahrens *idp = attr.za_first_integer; 1427b38f0970Sck if (case_conflict) 1428b38f0970Sck *case_conflict = attr.za_normalization_conflict; 142987e5029aSahrens zap_cursor_advance(&cursor); 143087e5029aSahrens *offp = zap_cursor_serialize(&cursor); 143187e5029aSahrens zap_cursor_fini(&cursor); 143287e5029aSahrens 143387e5029aSahrens return (0); 143487e5029aSahrens } 143587e5029aSahrens 143687e5029aSahrens int 143787e5029aSahrens dmu_dir_list_next(objset_t *os, int namelen, char *name, 143887e5029aSahrens uint64_t *idp, uint64_t *offp) 143987e5029aSahrens { 1440503ad85cSMatthew Ahrens dsl_dir_t *dd = os->os_dsl_dataset->ds_dir; 144187e5029aSahrens zap_cursor_t cursor; 144287e5029aSahrens zap_attribute_t attr; 144387e5029aSahrens 144487e5029aSahrens /* there is no next dir on a snapshot! */ 1445503ad85cSMatthew Ahrens if (os->os_dsl_dataset->ds_object != 144687e5029aSahrens dd->dd_phys->dd_head_dataset_obj) 1447fa9e4066Sahrens return (ENOENT); 1448fa9e4066Sahrens 144987e5029aSahrens zap_cursor_init_serialized(&cursor, 145087e5029aSahrens dd->dd_pool->dp_meta_objset, 145187e5029aSahrens dd->dd_phys->dd_child_dir_zapobj, *offp); 145287e5029aSahrens 145387e5029aSahrens if (zap_cursor_retrieve(&cursor, &attr) != 0) { 145487e5029aSahrens zap_cursor_fini(&cursor); 145587e5029aSahrens return (ENOENT); 145687e5029aSahrens } 145787e5029aSahrens 145887e5029aSahrens if (strlen(attr.za_name) + 1 > namelen) { 145987e5029aSahrens zap_cursor_fini(&cursor); 1460fa9e4066Sahrens return (ENAMETOOLONG); 146187e5029aSahrens } 1462fa9e4066Sahrens 1463fa9e4066Sahrens (void) strcpy(name, attr.za_name); 146487e5029aSahrens if (idp) 146587e5029aSahrens *idp = attr.za_first_integer; 1466fa9e4066Sahrens zap_cursor_advance(&cursor); 1467fa9e4066Sahrens *offp = zap_cursor_serialize(&cursor); 146887e5029aSahrens zap_cursor_fini(&cursor); 1469fa9e4066Sahrens 1470fa9e4066Sahrens return (0); 1471fa9e4066Sahrens } 1472fa9e4066Sahrens 1473088f3894Sahrens struct findarg { 1474fd136879SMatthew Ahrens int (*func)(const char *, void *); 1475088f3894Sahrens void *arg; 1476088f3894Sahrens }; 1477088f3894Sahrens 1478088f3894Sahrens /* ARGSUSED */ 1479088f3894Sahrens static int 1480088f3894Sahrens findfunc(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg) 1481088f3894Sahrens { 1482088f3894Sahrens struct findarg *fa = arg; 1483fd136879SMatthew Ahrens return (fa->func(dsname, fa->arg)); 1484088f3894Sahrens } 1485088f3894Sahrens 1486fa9e4066Sahrens /* 1487fa9e4066Sahrens * Find all objsets under name, and for each, call 'func(child_name, arg)'. 1488088f3894Sahrens * Perhaps change all callers to use dmu_objset_find_spa()? 1489fa9e4066Sahrens */ 14901d452cf5Sahrens int 1491fd136879SMatthew Ahrens dmu_objset_find(char *name, int func(const char *, void *), void *arg, 1492fd136879SMatthew Ahrens int flags) 1493088f3894Sahrens { 1494088f3894Sahrens struct findarg fa; 1495088f3894Sahrens fa.func = func; 1496088f3894Sahrens fa.arg = arg; 1497088f3894Sahrens return (dmu_objset_find_spa(NULL, name, findfunc, &fa, flags)); 1498088f3894Sahrens } 1499088f3894Sahrens 1500088f3894Sahrens /* 1501088f3894Sahrens * Find all objsets under name, call func on each 1502088f3894Sahrens */ 1503088f3894Sahrens int 1504088f3894Sahrens dmu_objset_find_spa(spa_t *spa, const char *name, 1505088f3894Sahrens int func(spa_t *, uint64_t, const char *, void *), void *arg, int flags) 1506fa9e4066Sahrens { 1507fa9e4066Sahrens dsl_dir_t *dd; 1508088f3894Sahrens dsl_pool_t *dp; 1509088f3894Sahrens dsl_dataset_t *ds; 1510fa9e4066Sahrens zap_cursor_t zc; 1511b7661cccSmmusante zap_attribute_t *attr; 1512fa9e4066Sahrens char *child; 1513088f3894Sahrens uint64_t thisobj; 1514088f3894Sahrens int err; 1515fa9e4066Sahrens 1516088f3894Sahrens if (name == NULL) 1517088f3894Sahrens name = spa_name(spa); 1518088f3894Sahrens err = dsl_dir_open_spa(spa, name, FTAG, &dd, NULL); 1519ea8dc4b6Seschrock if (err) 15201d452cf5Sahrens return (err); 1521fa9e4066Sahrens 1522088f3894Sahrens /* Don't visit hidden ($MOS & $ORIGIN) objsets. */ 1523088f3894Sahrens if (dd->dd_myname[0] == '$') { 1524088f3894Sahrens dsl_dir_close(dd, FTAG); 1525088f3894Sahrens return (0); 1526088f3894Sahrens } 1527088f3894Sahrens 1528088f3894Sahrens thisobj = dd->dd_phys->dd_head_dataset_obj; 1529b7661cccSmmusante attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); 1530088f3894Sahrens dp = dd->dd_pool; 1531fa9e4066Sahrens 1532fa9e4066Sahrens /* 1533fa9e4066Sahrens * Iterate over all children. 1534fa9e4066Sahrens */ 15350b69c2f0Sahrens if (flags & DS_FIND_CHILDREN) { 1536088f3894Sahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, 15370b69c2f0Sahrens dd->dd_phys->dd_child_dir_zapobj); 1538b7661cccSmmusante zap_cursor_retrieve(&zc, attr) == 0; 15390b69c2f0Sahrens (void) zap_cursor_advance(&zc)) { 1540b7661cccSmmusante ASSERT(attr->za_integer_length == sizeof (uint64_t)); 1541b7661cccSmmusante ASSERT(attr->za_num_integers == 1); 1542fa9e4066Sahrens 1543486ae710SMatthew Ahrens child = kmem_asprintf("%s/%s", name, attr->za_name); 1544088f3894Sahrens err = dmu_objset_find_spa(spa, child, func, arg, flags); 1545486ae710SMatthew Ahrens strfree(child); 15460b69c2f0Sahrens if (err) 15470b69c2f0Sahrens break; 15480b69c2f0Sahrens } 15490b69c2f0Sahrens zap_cursor_fini(&zc); 15501d452cf5Sahrens 15510b69c2f0Sahrens if (err) { 15520b69c2f0Sahrens dsl_dir_close(dd, FTAG); 1553b7661cccSmmusante kmem_free(attr, sizeof (zap_attribute_t)); 15540b69c2f0Sahrens return (err); 15550b69c2f0Sahrens } 1556fa9e4066Sahrens } 1557fa9e4066Sahrens 1558fa9e4066Sahrens /* 1559fa9e4066Sahrens * Iterate over all snapshots. 1560fa9e4066Sahrens */ 1561088f3894Sahrens if (flags & DS_FIND_SNAPSHOTS) { 1562088f3894Sahrens if (!dsl_pool_sync_context(dp)) 1563088f3894Sahrens rw_enter(&dp->dp_config_rwlock, RW_READER); 1564088f3894Sahrens err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds); 1565088f3894Sahrens if (!dsl_pool_sync_context(dp)) 1566088f3894Sahrens rw_exit(&dp->dp_config_rwlock); 1567088f3894Sahrens 1568088f3894Sahrens if (err == 0) { 1569088f3894Sahrens uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj; 1570088f3894Sahrens dsl_dataset_rele(ds, FTAG); 1571088f3894Sahrens 1572088f3894Sahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj); 1573088f3894Sahrens zap_cursor_retrieve(&zc, attr) == 0; 1574088f3894Sahrens (void) zap_cursor_advance(&zc)) { 1575088f3894Sahrens ASSERT(attr->za_integer_length == 1576088f3894Sahrens sizeof (uint64_t)); 1577088f3894Sahrens ASSERT(attr->za_num_integers == 1); 1578088f3894Sahrens 1579486ae710SMatthew Ahrens child = kmem_asprintf("%s@%s", 1580486ae710SMatthew Ahrens name, attr->za_name); 1581088f3894Sahrens err = func(spa, attr->za_first_integer, 1582088f3894Sahrens child, arg); 1583486ae710SMatthew Ahrens strfree(child); 1584088f3894Sahrens if (err) 1585088f3894Sahrens break; 1586088f3894Sahrens } 1587088f3894Sahrens zap_cursor_fini(&zc); 1588fa9e4066Sahrens } 1589fa9e4066Sahrens } 1590fa9e4066Sahrens 1591fa9e4066Sahrens dsl_dir_close(dd, FTAG); 1592b7661cccSmmusante kmem_free(attr, sizeof (zap_attribute_t)); 1593fa9e4066Sahrens 15941d452cf5Sahrens if (err) 15951d452cf5Sahrens return (err); 15961d452cf5Sahrens 1597fa9e4066Sahrens /* 1598fa9e4066Sahrens * Apply to self if appropriate. 1599fa9e4066Sahrens */ 1600088f3894Sahrens err = func(spa, thisobj, name, arg); 16011d452cf5Sahrens return (err); 1602fa9e4066Sahrens } 1603f18faf3fSek 16047f73c863SRich Morris /* ARGSUSED */ 16057f73c863SRich Morris int 1606fd136879SMatthew Ahrens dmu_objset_prefetch(const char *name, void *arg) 16077f73c863SRich Morris { 16087f73c863SRich Morris dsl_dataset_t *ds; 16097f73c863SRich Morris 16107f73c863SRich Morris if (dsl_dataset_hold(name, FTAG, &ds)) 16117f73c863SRich Morris return (0); 16127f73c863SRich Morris 16137f73c863SRich Morris if (!BP_IS_HOLE(&ds->ds_phys->ds_bp)) { 16147f73c863SRich Morris mutex_enter(&ds->ds_opening_lock); 1615503ad85cSMatthew Ahrens if (ds->ds_objset == NULL) { 16167f73c863SRich Morris uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH; 16177f73c863SRich Morris zbookmark_t zb; 16187f73c863SRich Morris 1619b24ab676SJeff Bonwick SET_BOOKMARK(&zb, ds->ds_object, ZB_ROOT_OBJECT, 1620b24ab676SJeff Bonwick ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 16217f73c863SRich Morris 16227f73c863SRich Morris (void) arc_read_nolock(NULL, dsl_dataset_get_spa(ds), 16237f73c863SRich Morris &ds->ds_phys->ds_bp, NULL, NULL, 16247f73c863SRich Morris ZIO_PRIORITY_ASYNC_READ, 16257f73c863SRich Morris ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, 16267f73c863SRich Morris &aflags, &zb); 16277f73c863SRich Morris } 16287f73c863SRich Morris mutex_exit(&ds->ds_opening_lock); 16297f73c863SRich Morris } 16307f73c863SRich Morris 16317f73c863SRich Morris dsl_dataset_rele(ds, FTAG); 16327f73c863SRich Morris return (0); 16337f73c863SRich Morris } 16347f73c863SRich Morris 1635f18faf3fSek void 1636f18faf3fSek dmu_objset_set_user(objset_t *os, void *user_ptr) 1637f18faf3fSek { 1638503ad85cSMatthew Ahrens ASSERT(MUTEX_HELD(&os->os_user_ptr_lock)); 1639503ad85cSMatthew Ahrens os->os_user_ptr = user_ptr; 1640f18faf3fSek } 1641f18faf3fSek 1642f18faf3fSek void * 1643f18faf3fSek dmu_objset_get_user(objset_t *os) 1644f18faf3fSek { 1645503ad85cSMatthew Ahrens ASSERT(MUTEX_HELD(&os->os_user_ptr_lock)); 1646503ad85cSMatthew Ahrens return (os->os_user_ptr); 1647f18faf3fSek } 1648