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 2555da60b9SMark J Musante /* Portions Copyright 2010 Robert Milkowski */ 2655da60b9SMark 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> 440a586ceaSMark Shellenbaum #include <sys/sa.h> 45fa9e4066Sahrens 46*744947dcSTom Erickson /* 47*744947dcSTom Erickson * Needed to close a window in dnode_move() that allows the objset to be freed 48*744947dcSTom Erickson * before it can be safely accessed. 49*744947dcSTom Erickson */ 50*744947dcSTom Erickson krwlock_t os_lock; 51*744947dcSTom Erickson 52*744947dcSTom Erickson void 53*744947dcSTom Erickson dmu_objset_init(void) 54*744947dcSTom Erickson { 55*744947dcSTom Erickson rw_init(&os_lock, NULL, RW_DEFAULT, NULL); 56*744947dcSTom Erickson } 57*744947dcSTom Erickson 58*744947dcSTom Erickson void 59*744947dcSTom Erickson dmu_objset_fini(void) 60*744947dcSTom Erickson { 61*744947dcSTom Erickson rw_destroy(&os_lock); 62*744947dcSTom Erickson } 63*744947dcSTom Erickson 64fa9e4066Sahrens spa_t * 65fa9e4066Sahrens dmu_objset_spa(objset_t *os) 66fa9e4066Sahrens { 67503ad85cSMatthew Ahrens return (os->os_spa); 68fa9e4066Sahrens } 69fa9e4066Sahrens 70fa9e4066Sahrens zilog_t * 71fa9e4066Sahrens dmu_objset_zil(objset_t *os) 72fa9e4066Sahrens { 73503ad85cSMatthew Ahrens return (os->os_zil); 74fa9e4066Sahrens } 75fa9e4066Sahrens 76fa9e4066Sahrens dsl_pool_t * 77fa9e4066Sahrens dmu_objset_pool(objset_t *os) 78fa9e4066Sahrens { 79fa9e4066Sahrens dsl_dataset_t *ds; 80fa9e4066Sahrens 81503ad85cSMatthew Ahrens if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir) 82fa9e4066Sahrens return (ds->ds_dir->dd_pool); 83fa9e4066Sahrens else 84503ad85cSMatthew Ahrens return (spa_get_dsl(os->os_spa)); 85fa9e4066Sahrens } 86fa9e4066Sahrens 87fa9e4066Sahrens dsl_dataset_t * 88fa9e4066Sahrens dmu_objset_ds(objset_t *os) 89fa9e4066Sahrens { 90503ad85cSMatthew Ahrens return (os->os_dsl_dataset); 91fa9e4066Sahrens } 92fa9e4066Sahrens 93fa9e4066Sahrens dmu_objset_type_t 94fa9e4066Sahrens dmu_objset_type(objset_t *os) 95fa9e4066Sahrens { 96503ad85cSMatthew Ahrens return (os->os_phys->os_type); 97fa9e4066Sahrens } 98fa9e4066Sahrens 99fa9e4066Sahrens void 100fa9e4066Sahrens dmu_objset_name(objset_t *os, char *buf) 101fa9e4066Sahrens { 102503ad85cSMatthew Ahrens dsl_dataset_name(os->os_dsl_dataset, buf); 103fa9e4066Sahrens } 104fa9e4066Sahrens 105fa9e4066Sahrens uint64_t 106fa9e4066Sahrens dmu_objset_id(objset_t *os) 107fa9e4066Sahrens { 108503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 109fa9e4066Sahrens 110fa9e4066Sahrens return (ds ? ds->ds_object : 0); 111fa9e4066Sahrens } 112fa9e4066Sahrens 11355da60b9SMark J Musante uint64_t 11455da60b9SMark J Musante dmu_objset_syncprop(objset_t *os) 11555da60b9SMark J Musante { 11655da60b9SMark J Musante return (os->os_sync); 11755da60b9SMark J Musante } 11855da60b9SMark J Musante 119e09fa4daSNeil Perrin uint64_t 120e09fa4daSNeil Perrin dmu_objset_logbias(objset_t *os) 121e09fa4daSNeil Perrin { 122e09fa4daSNeil Perrin return (os->os_logbias); 123e09fa4daSNeil Perrin } 124e09fa4daSNeil Perrin 125fa9e4066Sahrens static void 126fa9e4066Sahrens checksum_changed_cb(void *arg, uint64_t newval) 127fa9e4066Sahrens { 128503ad85cSMatthew Ahrens objset_t *os = arg; 129fa9e4066Sahrens 130fa9e4066Sahrens /* 131fa9e4066Sahrens * Inheritance should have been done by now. 132fa9e4066Sahrens */ 133fa9e4066Sahrens ASSERT(newval != ZIO_CHECKSUM_INHERIT); 134fa9e4066Sahrens 135503ad85cSMatthew Ahrens os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE); 136fa9e4066Sahrens } 137fa9e4066Sahrens 138fa9e4066Sahrens static void 139fa9e4066Sahrens compression_changed_cb(void *arg, uint64_t newval) 140fa9e4066Sahrens { 141503ad85cSMatthew Ahrens objset_t *os = arg; 142fa9e4066Sahrens 143fa9e4066Sahrens /* 144fa9e4066Sahrens * Inheritance and range checking should have been done by now. 145fa9e4066Sahrens */ 146fa9e4066Sahrens ASSERT(newval != ZIO_COMPRESS_INHERIT); 147fa9e4066Sahrens 148503ad85cSMatthew Ahrens os->os_compress = zio_compress_select(newval, ZIO_COMPRESS_ON_VALUE); 149fa9e4066Sahrens } 150fa9e4066Sahrens 151d0ad202dSahrens static void 152d0ad202dSahrens copies_changed_cb(void *arg, uint64_t newval) 153d0ad202dSahrens { 154503ad85cSMatthew Ahrens objset_t *os = arg; 155d0ad202dSahrens 156d0ad202dSahrens /* 157d0ad202dSahrens * Inheritance and range checking should have been done by now. 158d0ad202dSahrens */ 159d0ad202dSahrens ASSERT(newval > 0); 160503ad85cSMatthew Ahrens ASSERT(newval <= spa_max_replication(os->os_spa)); 161d0ad202dSahrens 162503ad85cSMatthew Ahrens os->os_copies = newval; 163d0ad202dSahrens } 164d0ad202dSahrens 165b24ab676SJeff Bonwick static void 166b24ab676SJeff Bonwick dedup_changed_cb(void *arg, uint64_t newval) 167b24ab676SJeff Bonwick { 168b24ab676SJeff Bonwick objset_t *os = arg; 169b24ab676SJeff Bonwick spa_t *spa = os->os_spa; 170b24ab676SJeff Bonwick enum zio_checksum checksum; 171b24ab676SJeff Bonwick 172b24ab676SJeff Bonwick /* 173b24ab676SJeff Bonwick * Inheritance should have been done by now. 174b24ab676SJeff Bonwick */ 175b24ab676SJeff Bonwick ASSERT(newval != ZIO_CHECKSUM_INHERIT); 176b24ab676SJeff Bonwick 177b24ab676SJeff Bonwick checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF); 178b24ab676SJeff Bonwick 179b24ab676SJeff Bonwick os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK; 180b24ab676SJeff Bonwick os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY); 181b24ab676SJeff Bonwick } 182b24ab676SJeff Bonwick 1833baa08fcSek static void 1843baa08fcSek primary_cache_changed_cb(void *arg, uint64_t newval) 1853baa08fcSek { 186503ad85cSMatthew Ahrens objset_t *os = arg; 1873baa08fcSek 1883baa08fcSek /* 1893baa08fcSek * Inheritance and range checking should have been done by now. 1903baa08fcSek */ 1913baa08fcSek ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE || 1923baa08fcSek newval == ZFS_CACHE_METADATA); 1933baa08fcSek 194503ad85cSMatthew Ahrens os->os_primary_cache = newval; 1953baa08fcSek } 1963baa08fcSek 1973baa08fcSek static void 1983baa08fcSek secondary_cache_changed_cb(void *arg, uint64_t newval) 1993baa08fcSek { 200503ad85cSMatthew Ahrens objset_t *os = arg; 2013baa08fcSek 2023baa08fcSek /* 2033baa08fcSek * Inheritance and range checking should have been done by now. 2043baa08fcSek */ 2053baa08fcSek ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE || 2063baa08fcSek newval == ZFS_CACHE_METADATA); 2073baa08fcSek 208503ad85cSMatthew Ahrens os->os_secondary_cache = newval; 2093baa08fcSek } 2103baa08fcSek 21155da60b9SMark J Musante static void 21255da60b9SMark J Musante sync_changed_cb(void *arg, uint64_t newval) 21355da60b9SMark J Musante { 21455da60b9SMark J Musante objset_t *os = arg; 21555da60b9SMark J Musante 21655da60b9SMark J Musante /* 21755da60b9SMark J Musante * Inheritance and range checking should have been done by now. 21855da60b9SMark J Musante */ 21955da60b9SMark J Musante ASSERT(newval == ZFS_SYNC_STANDARD || newval == ZFS_SYNC_ALWAYS || 22055da60b9SMark J Musante newval == ZFS_SYNC_DISABLED); 22155da60b9SMark J Musante 22255da60b9SMark J Musante os->os_sync = newval; 22355da60b9SMark J Musante if (os->os_zil) 22455da60b9SMark J Musante zil_set_sync(os->os_zil, newval); 22555da60b9SMark J Musante } 22655da60b9SMark J Musante 227e09fa4daSNeil Perrin static void 228e09fa4daSNeil Perrin logbias_changed_cb(void *arg, uint64_t newval) 229e09fa4daSNeil Perrin { 230e09fa4daSNeil Perrin objset_t *os = arg; 231e09fa4daSNeil Perrin 232e09fa4daSNeil Perrin ASSERT(newval == ZFS_LOGBIAS_LATENCY || 233e09fa4daSNeil Perrin newval == ZFS_LOGBIAS_THROUGHPUT); 234e09fa4daSNeil Perrin os->os_logbias = newval; 235e09fa4daSNeil Perrin if (os->os_zil) 236e09fa4daSNeil Perrin zil_set_logbias(os->os_zil, newval); 237e09fa4daSNeil Perrin } 238e09fa4daSNeil Perrin 239fa9e4066Sahrens void 240fa9e4066Sahrens dmu_objset_byteswap(void *buf, size_t size) 241fa9e4066Sahrens { 242fa9e4066Sahrens objset_phys_t *osp = buf; 243fa9e4066Sahrens 24414843421SMatthew Ahrens ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t)); 245fa9e4066Sahrens dnode_byteswap(&osp->os_meta_dnode); 246fa9e4066Sahrens byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t)); 247fa9e4066Sahrens osp->os_type = BSWAP_64(osp->os_type); 24814843421SMatthew Ahrens osp->os_flags = BSWAP_64(osp->os_flags); 24914843421SMatthew Ahrens if (size == sizeof (objset_phys_t)) { 25014843421SMatthew Ahrens dnode_byteswap(&osp->os_userused_dnode); 25114843421SMatthew Ahrens dnode_byteswap(&osp->os_groupused_dnode); 25214843421SMatthew Ahrens } 253fa9e4066Sahrens } 254fa9e4066Sahrens 255ea8dc4b6Seschrock int 256ea8dc4b6Seschrock dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, 257503ad85cSMatthew Ahrens objset_t **osp) 258fa9e4066Sahrens { 259503ad85cSMatthew Ahrens objset_t *os; 260088f3894Sahrens int i, err; 261fa9e4066Sahrens 26291ebeef5Sahrens ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock)); 26391ebeef5Sahrens 264503ad85cSMatthew Ahrens os = kmem_zalloc(sizeof (objset_t), KM_SLEEP); 265503ad85cSMatthew Ahrens os->os_dsl_dataset = ds; 266503ad85cSMatthew Ahrens os->os_spa = spa; 267503ad85cSMatthew Ahrens os->os_rootbp = bp; 268503ad85cSMatthew Ahrens if (!BP_IS_HOLE(os->os_rootbp)) { 26913506d1eSmaybee uint32_t aflags = ARC_WAIT; 270ea8dc4b6Seschrock zbookmark_t zb; 271b24ab676SJeff Bonwick SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET, 272b24ab676SJeff Bonwick ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 273b24ab676SJeff Bonwick 274503ad85cSMatthew Ahrens if (DMU_OS_IS_L2CACHEABLE(os)) 2753baa08fcSek aflags |= ARC_L2CACHE; 276ea8dc4b6Seschrock 277503ad85cSMatthew Ahrens dprintf_bp(os->os_rootbp, "reading %s", ""); 278088f3894Sahrens /* 2793f9d6ad7SLin Ling * XXX when bprewrite scrub can change the bp, 280088f3894Sahrens * and this is called from dmu_objset_open_ds_os, the bp 281088f3894Sahrens * could change, and we'll need a lock. 282088f3894Sahrens */ 2833f9d6ad7SLin Ling err = dsl_read_nolock(NULL, spa, os->os_rootbp, 284503ad85cSMatthew Ahrens arc_getbuf_func, &os->os_phys_buf, 28513506d1eSmaybee ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb); 286ea8dc4b6Seschrock if (err) { 287503ad85cSMatthew Ahrens kmem_free(os, sizeof (objset_t)); 288b87f3af3Sperrin /* convert checksum errors into IO errors */ 289b87f3af3Sperrin if (err == ECKSUM) 290b87f3af3Sperrin err = EIO; 291ea8dc4b6Seschrock return (err); 292ea8dc4b6Seschrock } 29314843421SMatthew Ahrens 29414843421SMatthew Ahrens /* Increase the blocksize if we are permitted. */ 29514843421SMatthew Ahrens if (spa_version(spa) >= SPA_VERSION_USERSPACE && 296503ad85cSMatthew Ahrens arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) { 29714843421SMatthew Ahrens arc_buf_t *buf = arc_buf_alloc(spa, 298503ad85cSMatthew Ahrens sizeof (objset_phys_t), &os->os_phys_buf, 29914843421SMatthew Ahrens ARC_BUFC_METADATA); 30014843421SMatthew Ahrens bzero(buf->b_data, sizeof (objset_phys_t)); 301503ad85cSMatthew Ahrens bcopy(os->os_phys_buf->b_data, buf->b_data, 302503ad85cSMatthew Ahrens arc_buf_size(os->os_phys_buf)); 303503ad85cSMatthew Ahrens (void) arc_buf_remove_ref(os->os_phys_buf, 304503ad85cSMatthew Ahrens &os->os_phys_buf); 305503ad85cSMatthew Ahrens os->os_phys_buf = buf; 30614843421SMatthew Ahrens } 30714843421SMatthew Ahrens 308503ad85cSMatthew Ahrens os->os_phys = os->os_phys_buf->b_data; 309503ad85cSMatthew Ahrens os->os_flags = os->os_phys->os_flags; 310fa9e4066Sahrens } else { 31114843421SMatthew Ahrens int size = spa_version(spa) >= SPA_VERSION_USERSPACE ? 31214843421SMatthew Ahrens sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE; 313503ad85cSMatthew Ahrens os->os_phys_buf = arc_buf_alloc(spa, size, 314503ad85cSMatthew Ahrens &os->os_phys_buf, ARC_BUFC_METADATA); 315503ad85cSMatthew Ahrens os->os_phys = os->os_phys_buf->b_data; 316503ad85cSMatthew Ahrens bzero(os->os_phys, size); 317fa9e4066Sahrens } 318fa9e4066Sahrens 319fa9e4066Sahrens /* 320fa9e4066Sahrens * Note: the changed_cb will be called once before the register 321fa9e4066Sahrens * func returns, thus changing the checksum/compression from the 3223baa08fcSek * default (fletcher2/off). Snapshots don't need to know about 3233baa08fcSek * checksum/compression/copies. 324fa9e4066Sahrens */ 3253baa08fcSek if (ds) { 3263baa08fcSek err = dsl_prop_register(ds, "primarycache", 327503ad85cSMatthew Ahrens primary_cache_changed_cb, os); 328d0ad202dSahrens if (err == 0) 3293baa08fcSek err = dsl_prop_register(ds, "secondarycache", 330503ad85cSMatthew Ahrens secondary_cache_changed_cb, os); 3313baa08fcSek if (!dsl_dataset_is_snapshot(ds)) { 3323baa08fcSek if (err == 0) 3333baa08fcSek err = dsl_prop_register(ds, "checksum", 334503ad85cSMatthew Ahrens checksum_changed_cb, os); 3353baa08fcSek if (err == 0) 3363baa08fcSek err = dsl_prop_register(ds, "compression", 337503ad85cSMatthew Ahrens compression_changed_cb, os); 3383baa08fcSek if (err == 0) 3393baa08fcSek err = dsl_prop_register(ds, "copies", 340503ad85cSMatthew Ahrens copies_changed_cb, os); 341b24ab676SJeff Bonwick if (err == 0) 342b24ab676SJeff Bonwick err = dsl_prop_register(ds, "dedup", 343b24ab676SJeff Bonwick dedup_changed_cb, os); 344e09fa4daSNeil Perrin if (err == 0) 345e09fa4daSNeil Perrin err = dsl_prop_register(ds, "logbias", 346e09fa4daSNeil Perrin logbias_changed_cb, os); 34755da60b9SMark J Musante if (err == 0) 34855da60b9SMark J Musante err = dsl_prop_register(ds, "sync", 34955da60b9SMark J Musante sync_changed_cb, os); 3503baa08fcSek } 351ea8dc4b6Seschrock if (err) { 352503ad85cSMatthew Ahrens VERIFY(arc_buf_remove_ref(os->os_phys_buf, 353503ad85cSMatthew Ahrens &os->os_phys_buf) == 1); 354503ad85cSMatthew Ahrens kmem_free(os, sizeof (objset_t)); 355ea8dc4b6Seschrock return (err); 356ea8dc4b6Seschrock } 35799653d4eSeschrock } else if (ds == NULL) { 358fa9e4066Sahrens /* It's the meta-objset. */ 359503ad85cSMatthew Ahrens os->os_checksum = ZIO_CHECKSUM_FLETCHER_4; 360503ad85cSMatthew Ahrens os->os_compress = ZIO_COMPRESS_LZJB; 361503ad85cSMatthew Ahrens os->os_copies = spa_max_replication(spa); 362b24ab676SJeff Bonwick os->os_dedup_checksum = ZIO_CHECKSUM_OFF; 363b24ab676SJeff Bonwick os->os_dedup_verify = 0; 364b24ab676SJeff Bonwick os->os_logbias = 0; 36555da60b9SMark J Musante os->os_sync = 0; 366503ad85cSMatthew Ahrens os->os_primary_cache = ZFS_CACHE_ALL; 367503ad85cSMatthew Ahrens os->os_secondary_cache = ZFS_CACHE_ALL; 368fa9e4066Sahrens } 369fa9e4066Sahrens 370503ad85cSMatthew Ahrens os->os_zil_header = os->os_phys->os_zil_header; 371503ad85cSMatthew Ahrens os->os_zil = zil_alloc(os, &os->os_zil_header); 372fa9e4066Sahrens 373fa9e4066Sahrens for (i = 0; i < TXG_SIZE; i++) { 374503ad85cSMatthew Ahrens list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t), 375fa9e4066Sahrens offsetof(dnode_t, dn_dirty_link[i])); 376503ad85cSMatthew Ahrens list_create(&os->os_free_dnodes[i], sizeof (dnode_t), 377fa9e4066Sahrens offsetof(dnode_t, dn_dirty_link[i])); 378fa9e4066Sahrens } 379503ad85cSMatthew Ahrens list_create(&os->os_dnodes, sizeof (dnode_t), 380fa9e4066Sahrens offsetof(dnode_t, dn_link)); 381503ad85cSMatthew Ahrens list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t), 382fa9e4066Sahrens offsetof(dmu_buf_impl_t, db_link)); 383fa9e4066Sahrens 384503ad85cSMatthew Ahrens mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL); 385503ad85cSMatthew Ahrens mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL); 386503ad85cSMatthew Ahrens mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL); 387503ad85cSMatthew Ahrens 388*744947dcSTom Erickson DMU_META_DNODE(os) = dnode_special_open(os, 389*744947dcSTom Erickson &os->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT, 390*744947dcSTom Erickson &os->os_meta_dnode); 391503ad85cSMatthew Ahrens if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) { 392*744947dcSTom Erickson DMU_USERUSED_DNODE(os) = dnode_special_open(os, 393*744947dcSTom Erickson &os->os_phys->os_userused_dnode, DMU_USERUSED_OBJECT, 394*744947dcSTom Erickson &os->os_userused_dnode); 395*744947dcSTom Erickson DMU_GROUPUSED_DNODE(os) = dnode_special_open(os, 396*744947dcSTom Erickson &os->os_phys->os_groupused_dnode, DMU_GROUPUSED_OBJECT, 397*744947dcSTom Erickson &os->os_groupused_dnode); 39814843421SMatthew Ahrens } 399fa9e4066Sahrens 40091ebeef5Sahrens /* 40191ebeef5Sahrens * We should be the only thread trying to do this because we 40291ebeef5Sahrens * have ds_opening_lock 40391ebeef5Sahrens */ 40491ebeef5Sahrens if (ds) { 405503ad85cSMatthew Ahrens mutex_enter(&ds->ds_lock); 406503ad85cSMatthew Ahrens ASSERT(ds->ds_objset == NULL); 407503ad85cSMatthew Ahrens ds->ds_objset = os; 408503ad85cSMatthew Ahrens mutex_exit(&ds->ds_lock); 409fa9e4066Sahrens } 410fa9e4066Sahrens 411503ad85cSMatthew Ahrens *osp = os; 412ea8dc4b6Seschrock return (0); 413fa9e4066Sahrens } 414fa9e4066Sahrens 415503ad85cSMatthew Ahrens int 416503ad85cSMatthew Ahrens dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp) 4173cb34c60Sahrens { 418503ad85cSMatthew Ahrens int err = 0; 4193cb34c60Sahrens 4203cb34c60Sahrens mutex_enter(&ds->ds_opening_lock); 421503ad85cSMatthew Ahrens *osp = ds->ds_objset; 422503ad85cSMatthew Ahrens if (*osp == NULL) { 4233cb34c60Sahrens err = dmu_objset_open_impl(dsl_dataset_get_spa(ds), 424503ad85cSMatthew Ahrens ds, &ds->ds_phys->ds_bp, osp); 4253cb34c60Sahrens } 4263cb34c60Sahrens mutex_exit(&ds->ds_opening_lock); 427503ad85cSMatthew Ahrens return (err); 4283cb34c60Sahrens } 4293cb34c60Sahrens 430503ad85cSMatthew Ahrens /* called from zpl */ 4313cb34c60Sahrens int 432503ad85cSMatthew Ahrens dmu_objset_hold(const char *name, void *tag, objset_t **osp) 4333cb34c60Sahrens { 434503ad85cSMatthew Ahrens dsl_dataset_t *ds; 4353cb34c60Sahrens int err; 4363cb34c60Sahrens 437503ad85cSMatthew Ahrens err = dsl_dataset_hold(name, tag, &ds); 4383cb34c60Sahrens if (err) 439503ad85cSMatthew Ahrens return (err); 440503ad85cSMatthew Ahrens 441503ad85cSMatthew Ahrens err = dmu_objset_from_ds(ds, osp); 442503ad85cSMatthew Ahrens if (err) 443503ad85cSMatthew Ahrens dsl_dataset_rele(ds, tag); 444503ad85cSMatthew Ahrens 4453cb34c60Sahrens return (err); 4463cb34c60Sahrens } 4473cb34c60Sahrens 448fa9e4066Sahrens /* called from zpl */ 449fa9e4066Sahrens int 450503ad85cSMatthew Ahrens dmu_objset_own(const char *name, dmu_objset_type_t type, 451503ad85cSMatthew Ahrens boolean_t readonly, void *tag, objset_t **osp) 452fa9e4066Sahrens { 453f18faf3fSek dsl_dataset_t *ds; 454f18faf3fSek int err; 455fa9e4066Sahrens 456503ad85cSMatthew Ahrens err = dsl_dataset_own(name, B_FALSE, tag, &ds); 457503ad85cSMatthew Ahrens if (err) 458fa9e4066Sahrens return (err); 459fa9e4066Sahrens 460503ad85cSMatthew Ahrens err = dmu_objset_from_ds(ds, osp); 4613cb34c60Sahrens if (err) { 462503ad85cSMatthew Ahrens dsl_dataset_disown(ds, tag); 463681d9761SEric Taylor } else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) { 464503ad85cSMatthew Ahrens dmu_objset_disown(*osp, tag); 465503ad85cSMatthew Ahrens return (EINVAL); 466681d9761SEric Taylor } else if (!readonly && dsl_dataset_is_snapshot(ds)) { 467681d9761SEric Taylor dmu_objset_disown(*osp, tag); 468681d9761SEric Taylor return (EROFS); 469fa9e4066Sahrens } 4703cb34c60Sahrens return (err); 471fa9e4066Sahrens } 472fa9e4066Sahrens 473fa9e4066Sahrens void 474503ad85cSMatthew Ahrens dmu_objset_rele(objset_t *os, void *tag) 475fa9e4066Sahrens { 476503ad85cSMatthew Ahrens dsl_dataset_rele(os->os_dsl_dataset, tag); 477503ad85cSMatthew Ahrens } 478503ad85cSMatthew Ahrens 479503ad85cSMatthew Ahrens void 480503ad85cSMatthew Ahrens dmu_objset_disown(objset_t *os, void *tag) 481503ad85cSMatthew Ahrens { 482503ad85cSMatthew Ahrens dsl_dataset_disown(os->os_dsl_dataset, tag); 483fa9e4066Sahrens } 484fa9e4066Sahrens 485436b2950Sperrin int 4861934e92fSmaybee dmu_objset_evict_dbufs(objset_t *os) 487ea8dc4b6Seschrock { 488ea8dc4b6Seschrock dnode_t *dn; 489c543ec06Sahrens 490503ad85cSMatthew Ahrens mutex_enter(&os->os_lock); 491c543ec06Sahrens 492c543ec06Sahrens /* process the mdn last, since the other dnodes have holds on it */ 493*744947dcSTom Erickson list_remove(&os->os_dnodes, DMU_META_DNODE(os)); 494*744947dcSTom Erickson list_insert_tail(&os->os_dnodes, DMU_META_DNODE(os)); 495ea8dc4b6Seschrock 496ea8dc4b6Seschrock /* 497c543ec06Sahrens * Find the first dnode with holds. We have to do this dance 498c543ec06Sahrens * because dnode_add_ref() only works if you already have a 499c543ec06Sahrens * hold. If there are no holds then it has no dbufs so OK to 500c543ec06Sahrens * skip. 501ea8dc4b6Seschrock */ 502503ad85cSMatthew Ahrens for (dn = list_head(&os->os_dnodes); 5031934e92fSmaybee dn && !dnode_add_ref(dn, FTAG); 504503ad85cSMatthew Ahrens dn = list_next(&os->os_dnodes, dn)) 505c543ec06Sahrens continue; 506c543ec06Sahrens 507c543ec06Sahrens while (dn) { 508c543ec06Sahrens dnode_t *next_dn = dn; 509c543ec06Sahrens 510c543ec06Sahrens do { 511503ad85cSMatthew Ahrens next_dn = list_next(&os->os_dnodes, next_dn); 5121934e92fSmaybee } while (next_dn && !dnode_add_ref(next_dn, FTAG)); 513c543ec06Sahrens 514503ad85cSMatthew Ahrens mutex_exit(&os->os_lock); 5151934e92fSmaybee dnode_evict_dbufs(dn); 516c543ec06Sahrens dnode_rele(dn, FTAG); 517503ad85cSMatthew Ahrens mutex_enter(&os->os_lock); 518c543ec06Sahrens dn = next_dn; 519ea8dc4b6Seschrock } 520*744947dcSTom Erickson dn = list_head(&os->os_dnodes); 521503ad85cSMatthew Ahrens mutex_exit(&os->os_lock); 522*744947dcSTom Erickson return (dn != DMU_META_DNODE(os)); 523ea8dc4b6Seschrock } 524ea8dc4b6Seschrock 525fa9e4066Sahrens void 526503ad85cSMatthew Ahrens dmu_objset_evict(objset_t *os) 527fa9e4066Sahrens { 528503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 529fa9e4066Sahrens 530b24ab676SJeff Bonwick for (int t = 0; t < TXG_SIZE; t++) 531b24ab676SJeff Bonwick ASSERT(!dmu_objset_is_dirty(os, t)); 532fa9e4066Sahrens 5333baa08fcSek if (ds) { 5343baa08fcSek if (!dsl_dataset_is_snapshot(ds)) { 5353baa08fcSek VERIFY(0 == dsl_prop_unregister(ds, "checksum", 536503ad85cSMatthew Ahrens checksum_changed_cb, os)); 5373baa08fcSek VERIFY(0 == dsl_prop_unregister(ds, "compression", 538503ad85cSMatthew Ahrens compression_changed_cb, os)); 5393baa08fcSek VERIFY(0 == dsl_prop_unregister(ds, "copies", 540503ad85cSMatthew Ahrens copies_changed_cb, os)); 541b24ab676SJeff Bonwick VERIFY(0 == dsl_prop_unregister(ds, "dedup", 542b24ab676SJeff Bonwick dedup_changed_cb, os)); 543e09fa4daSNeil Perrin VERIFY(0 == dsl_prop_unregister(ds, "logbias", 544e09fa4daSNeil Perrin logbias_changed_cb, os)); 54555da60b9SMark J Musante VERIFY(0 == dsl_prop_unregister(ds, "sync", 54655da60b9SMark J Musante sync_changed_cb, os)); 5473baa08fcSek } 5483baa08fcSek VERIFY(0 == dsl_prop_unregister(ds, "primarycache", 549503ad85cSMatthew Ahrens primary_cache_changed_cb, os)); 5503baa08fcSek VERIFY(0 == dsl_prop_unregister(ds, "secondarycache", 551503ad85cSMatthew Ahrens secondary_cache_changed_cb, os)); 552fa9e4066Sahrens } 553fa9e4066Sahrens 5540a586ceaSMark Shellenbaum if (os->os_sa) 5550a586ceaSMark Shellenbaum sa_tear_down(os); 5560a586ceaSMark Shellenbaum 557ea8dc4b6Seschrock /* 558ea8dc4b6Seschrock * We should need only a single pass over the dnode list, since 559ea8dc4b6Seschrock * nothing can be added to the list at this point. 560ea8dc4b6Seschrock */ 561503ad85cSMatthew Ahrens (void) dmu_objset_evict_dbufs(os); 562ea8dc4b6Seschrock 563*744947dcSTom Erickson dnode_special_close(&os->os_meta_dnode); 564*744947dcSTom Erickson if (DMU_USERUSED_DNODE(os)) { 565*744947dcSTom Erickson dnode_special_close(&os->os_userused_dnode); 566*744947dcSTom Erickson dnode_special_close(&os->os_groupused_dnode); 56714843421SMatthew Ahrens } 568503ad85cSMatthew Ahrens zil_free(os->os_zil); 569fa9e4066Sahrens 570503ad85cSMatthew Ahrens ASSERT3P(list_head(&os->os_dnodes), ==, NULL); 57114843421SMatthew Ahrens 572503ad85cSMatthew Ahrens VERIFY(arc_buf_remove_ref(os->os_phys_buf, &os->os_phys_buf) == 1); 573*744947dcSTom Erickson 574*744947dcSTom Erickson /* 575*744947dcSTom Erickson * This is a barrier to prevent the objset from going away in 576*744947dcSTom Erickson * dnode_move() until we can safely ensure that the objset is still in 577*744947dcSTom Erickson * use. We consider the objset valid before the barrier and invalid 578*744947dcSTom Erickson * after the barrier. 579*744947dcSTom Erickson */ 580*744947dcSTom Erickson rw_enter(&os_lock, RW_READER); 581*744947dcSTom Erickson rw_exit(&os_lock); 582*744947dcSTom Erickson 583503ad85cSMatthew Ahrens mutex_destroy(&os->os_lock); 584503ad85cSMatthew Ahrens mutex_destroy(&os->os_obj_lock); 585503ad85cSMatthew Ahrens mutex_destroy(&os->os_user_ptr_lock); 586503ad85cSMatthew Ahrens kmem_free(os, sizeof (objset_t)); 587fa9e4066Sahrens } 588fa9e4066Sahrens 58971eb0538SChris Kirby timestruc_t 59071eb0538SChris Kirby dmu_objset_snap_cmtime(objset_t *os) 59171eb0538SChris Kirby { 59271eb0538SChris Kirby return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir)); 59371eb0538SChris Kirby } 59471eb0538SChris Kirby 595fa9e4066Sahrens /* called from dsl for meta-objset */ 596503ad85cSMatthew Ahrens objset_t * 597c717a561Smaybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, 598c717a561Smaybee dmu_objset_type_t type, dmu_tx_t *tx) 599fa9e4066Sahrens { 600503ad85cSMatthew Ahrens objset_t *os; 601fa9e4066Sahrens dnode_t *mdn; 602fa9e4066Sahrens 603fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 60491ebeef5Sahrens if (ds) 60591ebeef5Sahrens mutex_enter(&ds->ds_opening_lock); 606503ad85cSMatthew Ahrens VERIFY(0 == dmu_objset_open_impl(spa, ds, bp, &os)); 60791ebeef5Sahrens if (ds) 60891ebeef5Sahrens mutex_exit(&ds->ds_opening_lock); 609*744947dcSTom Erickson mdn = DMU_META_DNODE(os); 610fa9e4066Sahrens 611fa9e4066Sahrens dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT, 612fa9e4066Sahrens DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx); 613fa9e4066Sahrens 614fa9e4066Sahrens /* 615fa9e4066Sahrens * We don't want to have to increase the meta-dnode's nlevels 616fa9e4066Sahrens * later, because then we could do it in quescing context while 617fa9e4066Sahrens * we are also accessing it in open context. 618fa9e4066Sahrens * 619fa9e4066Sahrens * This precaution is not necessary for the MOS (ds == NULL), 620fa9e4066Sahrens * because the MOS is only updated in syncing context. 621fa9e4066Sahrens * This is most fortunate: the MOS is the only objset that 622fa9e4066Sahrens * needs to be synced multiple times as spa_sync() iterates 623fa9e4066Sahrens * to convergence, so minimizing its dn_nlevels matters. 624fa9e4066Sahrens */ 625ea8dc4b6Seschrock if (ds != NULL) { 626ea8dc4b6Seschrock int levels = 1; 627ea8dc4b6Seschrock 628ea8dc4b6Seschrock /* 629ea8dc4b6Seschrock * Determine the number of levels necessary for the meta-dnode 630ea8dc4b6Seschrock * to contain DN_MAX_OBJECT dnodes. 631ea8dc4b6Seschrock */ 632ea8dc4b6Seschrock while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift + 633ea8dc4b6Seschrock (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) < 634ea8dc4b6Seschrock DN_MAX_OBJECT * sizeof (dnode_phys_t)) 635ea8dc4b6Seschrock levels++; 636ea8dc4b6Seschrock 637fa9e4066Sahrens mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] = 638ea8dc4b6Seschrock mdn->dn_nlevels = levels; 639ea8dc4b6Seschrock } 640fa9e4066Sahrens 641fa9e4066Sahrens ASSERT(type != DMU_OST_NONE); 642fa9e4066Sahrens ASSERT(type != DMU_OST_ANY); 643fa9e4066Sahrens ASSERT(type < DMU_OST_NUMTYPES); 644503ad85cSMatthew Ahrens os->os_phys->os_type = type; 645503ad85cSMatthew Ahrens if (dmu_objset_userused_enabled(os)) { 646503ad85cSMatthew Ahrens os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE; 647503ad85cSMatthew Ahrens os->os_flags = os->os_phys->os_flags; 64814843421SMatthew Ahrens } 649fa9e4066Sahrens 650fa9e4066Sahrens dsl_dataset_dirty(ds, tx); 651fa9e4066Sahrens 652503ad85cSMatthew Ahrens return (os); 653fa9e4066Sahrens } 654fa9e4066Sahrens 655fa9e4066Sahrens struct oscarg { 656ecd6cf80Smarks void (*userfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx); 657fa9e4066Sahrens void *userarg; 658ae46e4c7SMatthew Ahrens dsl_dataset_t *clone_origin; 659fa9e4066Sahrens const char *lastname; 660fa9e4066Sahrens dmu_objset_type_t type; 661ab04eb8eStimh uint64_t flags; 6623f9d6ad7SLin Ling cred_t *cr; 663fa9e4066Sahrens }; 664fa9e4066Sahrens 665ecd6cf80Smarks /*ARGSUSED*/ 666fa9e4066Sahrens static int 6671d452cf5Sahrens dmu_objset_create_check(void *arg1, void *arg2, dmu_tx_t *tx) 668fa9e4066Sahrens { 6691d452cf5Sahrens dsl_dir_t *dd = arg1; 6701d452cf5Sahrens struct oscarg *oa = arg2; 6711d452cf5Sahrens objset_t *mos = dd->dd_pool->dp_meta_objset; 672fa9e4066Sahrens int err; 6731d452cf5Sahrens uint64_t ddobj; 6741d452cf5Sahrens 6751d452cf5Sahrens err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj, 6761d452cf5Sahrens oa->lastname, sizeof (uint64_t), 1, &ddobj); 6771d452cf5Sahrens if (err != ENOENT) 6781d452cf5Sahrens return (err ? err : EEXIST); 6791d452cf5Sahrens 680ae46e4c7SMatthew Ahrens if (oa->clone_origin != NULL) { 681ae46e4c7SMatthew Ahrens /* You can't clone across pools. */ 682ae46e4c7SMatthew Ahrens if (oa->clone_origin->ds_dir->dd_pool != dd->dd_pool) 6831d452cf5Sahrens return (EXDEV); 6841d452cf5Sahrens 685ae46e4c7SMatthew Ahrens /* You can only clone snapshots, not the head datasets. */ 686ae46e4c7SMatthew Ahrens if (!dsl_dataset_is_snapshot(oa->clone_origin)) 6871d452cf5Sahrens return (EINVAL); 6881d452cf5Sahrens } 689ecd6cf80Smarks 6901d452cf5Sahrens return (0); 6911d452cf5Sahrens } 6921d452cf5Sahrens 6931d452cf5Sahrens static void 6943f9d6ad7SLin Ling dmu_objset_create_sync(void *arg1, void *arg2, dmu_tx_t *tx) 6951d452cf5Sahrens { 6961d452cf5Sahrens dsl_dir_t *dd = arg1; 6971d452cf5Sahrens struct oscarg *oa = arg2; 6981d452cf5Sahrens uint64_t dsobj; 699fa9e4066Sahrens 700fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 701fa9e4066Sahrens 7021d452cf5Sahrens dsobj = dsl_dataset_create_sync(dd, oa->lastname, 7033f9d6ad7SLin Ling oa->clone_origin, oa->flags, oa->cr, tx); 704fa9e4066Sahrens 705ae46e4c7SMatthew Ahrens if (oa->clone_origin == NULL) { 706ae46e4c7SMatthew Ahrens dsl_dataset_t *ds; 707ae46e4c7SMatthew Ahrens blkptr_t *bp; 708503ad85cSMatthew Ahrens objset_t *os; 709fa9e4066Sahrens 710ae46e4c7SMatthew Ahrens VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, dsobj, 711ae46e4c7SMatthew Ahrens FTAG, &ds)); 712ae46e4c7SMatthew Ahrens bp = dsl_dataset_get_blkptr(ds); 713ae46e4c7SMatthew Ahrens ASSERT(BP_IS_HOLE(bp)); 714ae46e4c7SMatthew Ahrens 715503ad85cSMatthew Ahrens os = dmu_objset_create_impl(dsl_dataset_get_spa(ds), 716c717a561Smaybee ds, bp, oa->type, tx); 717fa9e4066Sahrens 718fa9e4066Sahrens if (oa->userfunc) 7193f9d6ad7SLin Ling oa->userfunc(os, oa->userarg, oa->cr, tx); 720ae46e4c7SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 721fa9e4066Sahrens } 722ecd6cf80Smarks 7233f9d6ad7SLin Ling spa_history_log_internal(LOG_DS_CREATE, dd->dd_pool->dp_spa, 7243f9d6ad7SLin Ling tx, "dataset = %llu", dsobj); 725fa9e4066Sahrens } 726fa9e4066Sahrens 727fa9e4066Sahrens int 728ae46e4c7SMatthew Ahrens dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags, 729ecd6cf80Smarks void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg) 730fa9e4066Sahrens { 7311d452cf5Sahrens dsl_dir_t *pdd; 732fa9e4066Sahrens const char *tail; 733fa9e4066Sahrens int err = 0; 7341d452cf5Sahrens struct oscarg oa = { 0 }; 735fa9e4066Sahrens 7361d452cf5Sahrens ASSERT(strchr(name, '@') == NULL); 7371d452cf5Sahrens err = dsl_dir_open(name, FTAG, &pdd, &tail); 738ea8dc4b6Seschrock if (err) 739ea8dc4b6Seschrock return (err); 740fa9e4066Sahrens if (tail == NULL) { 7411d452cf5Sahrens dsl_dir_close(pdd, FTAG); 742fa9e4066Sahrens return (EEXIST); 743fa9e4066Sahrens } 744fa9e4066Sahrens 7451d452cf5Sahrens oa.userfunc = func; 7461d452cf5Sahrens oa.userarg = arg; 7471d452cf5Sahrens oa.lastname = tail; 7481d452cf5Sahrens oa.type = type; 749ab04eb8eStimh oa.flags = flags; 7503f9d6ad7SLin Ling oa.cr = CRED(); 751ecd6cf80Smarks 752ae46e4c7SMatthew Ahrens err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check, 753ae46e4c7SMatthew Ahrens dmu_objset_create_sync, pdd, &oa, 5); 754ae46e4c7SMatthew Ahrens dsl_dir_close(pdd, FTAG); 755ae46e4c7SMatthew Ahrens return (err); 756ae46e4c7SMatthew Ahrens } 757ae46e4c7SMatthew Ahrens 758ae46e4c7SMatthew Ahrens int 759ae46e4c7SMatthew Ahrens dmu_objset_clone(const char *name, dsl_dataset_t *clone_origin, uint64_t flags) 760ae46e4c7SMatthew Ahrens { 761ae46e4c7SMatthew Ahrens dsl_dir_t *pdd; 762ae46e4c7SMatthew Ahrens const char *tail; 763ae46e4c7SMatthew Ahrens int err = 0; 764ae46e4c7SMatthew Ahrens struct oscarg oa = { 0 }; 765ae46e4c7SMatthew Ahrens 766ae46e4c7SMatthew Ahrens ASSERT(strchr(name, '@') == NULL); 767ae46e4c7SMatthew Ahrens err = dsl_dir_open(name, FTAG, &pdd, &tail); 768ae46e4c7SMatthew Ahrens if (err) 769ae46e4c7SMatthew Ahrens return (err); 770ae46e4c7SMatthew Ahrens if (tail == NULL) { 771ae46e4c7SMatthew Ahrens dsl_dir_close(pdd, FTAG); 772ae46e4c7SMatthew Ahrens return (EEXIST); 773fa9e4066Sahrens } 774ae46e4c7SMatthew Ahrens 775ae46e4c7SMatthew Ahrens oa.lastname = tail; 776ae46e4c7SMatthew Ahrens oa.clone_origin = clone_origin; 777ae46e4c7SMatthew Ahrens oa.flags = flags; 7783f9d6ad7SLin Ling oa.cr = CRED(); 779ae46e4c7SMatthew Ahrens 7801d452cf5Sahrens err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check, 7811d452cf5Sahrens dmu_objset_create_sync, pdd, &oa, 5); 7821d452cf5Sahrens dsl_dir_close(pdd, FTAG); 783fa9e4066Sahrens return (err); 784fa9e4066Sahrens } 785fa9e4066Sahrens 786fa9e4066Sahrens int 787842727c2SChris Kirby dmu_objset_destroy(const char *name, boolean_t defer) 788fa9e4066Sahrens { 789503ad85cSMatthew Ahrens dsl_dataset_t *ds; 790fa9e4066Sahrens int error; 791fa9e4066Sahrens 792fa9e4066Sahrens /* 793ae46e4c7SMatthew Ahrens * dsl_dataset_destroy() can free any claimed-but-unplayed 794ae46e4c7SMatthew Ahrens * intent log, but if there is an active log, it has blocks that 795ae46e4c7SMatthew Ahrens * are allocated, but may not yet be reflected in the on-disk 796ae46e4c7SMatthew Ahrens * structure. Only the ZIL knows how to free them, so we have 797ae46e4c7SMatthew Ahrens * to call into it here. 798fa9e4066Sahrens */ 799503ad85cSMatthew Ahrens error = dsl_dataset_own(name, B_TRUE, FTAG, &ds); 800fa9e4066Sahrens if (error == 0) { 801503ad85cSMatthew Ahrens objset_t *os; 802503ad85cSMatthew Ahrens if (dmu_objset_from_ds(ds, &os) == 0) 803503ad85cSMatthew Ahrens zil_destroy(dmu_objset_zil(os), B_FALSE); 804503ad85cSMatthew Ahrens error = dsl_dataset_destroy(ds, FTAG, defer); 805ae46e4c7SMatthew Ahrens /* dsl_dataset_destroy() closes the ds. */ 806fa9e4066Sahrens } 807fa9e4066Sahrens 8083cb34c60Sahrens return (error); 809fa9e4066Sahrens } 810fa9e4066Sahrens 8111d452cf5Sahrens struct snaparg { 8121d452cf5Sahrens dsl_sync_task_group_t *dstg; 8131d452cf5Sahrens char *snapname; 8141d452cf5Sahrens char failed[MAXPATHLEN]; 815d8d780fbSMatthew Ahrens boolean_t recursive; 816ea2f5b9eSMatthew Ahrens nvlist_t *props; 8173cb34c60Sahrens }; 8183cb34c60Sahrens 819ea2f5b9eSMatthew Ahrens static int 820ea2f5b9eSMatthew Ahrens snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx) 821ea2f5b9eSMatthew Ahrens { 822ea2f5b9eSMatthew Ahrens objset_t *os = arg1; 823ea2f5b9eSMatthew Ahrens struct snaparg *sn = arg2; 824ea2f5b9eSMatthew Ahrens 825ea2f5b9eSMatthew Ahrens /* The props have already been checked by zfs_check_userprops(). */ 826ea2f5b9eSMatthew Ahrens 827503ad85cSMatthew Ahrens return (dsl_dataset_snapshot_check(os->os_dsl_dataset, 828ea2f5b9eSMatthew Ahrens sn->snapname, tx)); 829ea2f5b9eSMatthew Ahrens } 830ea2f5b9eSMatthew Ahrens 831ea2f5b9eSMatthew Ahrens static void 8323f9d6ad7SLin Ling snapshot_sync(void *arg1, void *arg2, dmu_tx_t *tx) 833ea2f5b9eSMatthew Ahrens { 834ea2f5b9eSMatthew Ahrens objset_t *os = arg1; 835503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 836ea2f5b9eSMatthew Ahrens struct snaparg *sn = arg2; 837ea2f5b9eSMatthew Ahrens 8383f9d6ad7SLin Ling dsl_dataset_snapshot_sync(ds, sn->snapname, tx); 839ea2f5b9eSMatthew Ahrens 84092241e0bSTom Erickson if (sn->props) { 84192241e0bSTom Erickson dsl_props_arg_t pa; 84292241e0bSTom Erickson pa.pa_props = sn->props; 84392241e0bSTom Erickson pa.pa_source = ZPROP_SRC_LOCAL; 8443f9d6ad7SLin Ling dsl_props_set_sync(ds->ds_prev, &pa, tx); 84592241e0bSTom Erickson } 846ea2f5b9eSMatthew Ahrens } 8471d452cf5Sahrens 8481d452cf5Sahrens static int 849fd136879SMatthew Ahrens dmu_objset_snapshot_one(const char *name, void *arg) 8501d452cf5Sahrens { 8511d452cf5Sahrens struct snaparg *sn = arg; 8521d452cf5Sahrens objset_t *os; 8531d452cf5Sahrens int err; 854d8d780fbSMatthew Ahrens char *cp; 855d8d780fbSMatthew Ahrens 856d8d780fbSMatthew Ahrens /* 857d8d780fbSMatthew Ahrens * If the objset starts with a '%', then ignore it unless it was 858d8d780fbSMatthew Ahrens * explicitly named (ie, not recursive). These hidden datasets 859d8d780fbSMatthew Ahrens * are always inconsistent, and by not opening them here, we can 860d8d780fbSMatthew Ahrens * avoid a race with dsl_dir_destroy_check(). 861d8d780fbSMatthew Ahrens */ 862d8d780fbSMatthew Ahrens cp = strrchr(name, '/'); 863d8d780fbSMatthew Ahrens if (cp && cp[1] == '%' && sn->recursive) 864d8d780fbSMatthew Ahrens return (0); 8651d452cf5Sahrens 8661d452cf5Sahrens (void) strcpy(sn->failed, name); 8671d452cf5Sahrens 868ecd6cf80Smarks /* 869d8d780fbSMatthew Ahrens * Check permissions if we are doing a recursive snapshot. The 870d8d780fbSMatthew Ahrens * permission checks for the starting dataset have already been 871d8d780fbSMatthew Ahrens * performed in zfs_secpolicy_snapshot() 872ecd6cf80Smarks */ 873d8d780fbSMatthew Ahrens if (sn->recursive && (err = zfs_secpolicy_snapshot_perms(name, CRED()))) 874ecd6cf80Smarks return (err); 875ecd6cf80Smarks 876503ad85cSMatthew Ahrens err = dmu_objset_hold(name, sn, &os); 8771d452cf5Sahrens if (err != 0) 8781d452cf5Sahrens return (err); 8791d452cf5Sahrens 880d8d780fbSMatthew Ahrens /* 881d8d780fbSMatthew Ahrens * If the objset is in an inconsistent state (eg, in the process 882d8d780fbSMatthew Ahrens * of being destroyed), don't snapshot it. As with %hidden 883d8d780fbSMatthew Ahrens * datasets, we return EBUSY if this name was explicitly 884d8d780fbSMatthew Ahrens * requested (ie, not recursive), and otherwise ignore it. 885d8d780fbSMatthew Ahrens */ 886503ad85cSMatthew Ahrens if (os->os_dsl_dataset->ds_phys->ds_flags & DS_FLAG_INCONSISTENT) { 887503ad85cSMatthew Ahrens dmu_objset_rele(os, sn); 888d8d780fbSMatthew Ahrens return (sn->recursive ? 0 : EBUSY); 889f2e10be3Srm } 890f2e10be3Srm 8911d452cf5Sahrens /* 8921d452cf5Sahrens * NB: we need to wait for all in-flight changes to get to disk, 8931d452cf5Sahrens * so that we snapshot those changes. zil_suspend does this as 8941d452cf5Sahrens * a side effect. 8951d452cf5Sahrens */ 8961d452cf5Sahrens err = zil_suspend(dmu_objset_zil(os)); 8971d452cf5Sahrens if (err == 0) { 898ea2f5b9eSMatthew Ahrens dsl_sync_task_create(sn->dstg, snapshot_check, 899ea2f5b9eSMatthew Ahrens snapshot_sync, os, sn, 3); 900f2e10be3Srm } else { 901503ad85cSMatthew Ahrens dmu_objset_rele(os, sn); 9021d452cf5Sahrens } 903f2e10be3Srm 9041d452cf5Sahrens return (err); 9051d452cf5Sahrens } 9061d452cf5Sahrens 9071d452cf5Sahrens int 908ea2f5b9eSMatthew Ahrens dmu_objset_snapshot(char *fsname, char *snapname, 909ea2f5b9eSMatthew Ahrens nvlist_t *props, boolean_t recursive) 9101d452cf5Sahrens { 9111d452cf5Sahrens dsl_sync_task_t *dst; 912ea2f5b9eSMatthew Ahrens struct snaparg sn; 9131d452cf5Sahrens spa_t *spa; 9141d452cf5Sahrens int err; 9151d452cf5Sahrens 9161d452cf5Sahrens (void) strcpy(sn.failed, fsname); 9171d452cf5Sahrens 91840feaa91Sahrens err = spa_open(fsname, &spa, FTAG); 9191d452cf5Sahrens if (err) 9201d452cf5Sahrens return (err); 9211d452cf5Sahrens 9221d452cf5Sahrens sn.dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 9231d452cf5Sahrens sn.snapname = snapname; 924ea2f5b9eSMatthew Ahrens sn.props = props; 925d8d780fbSMatthew Ahrens sn.recursive = recursive; 9261d452cf5Sahrens 9270b69c2f0Sahrens if (recursive) { 9280b69c2f0Sahrens err = dmu_objset_find(fsname, 9290b69c2f0Sahrens dmu_objset_snapshot_one, &sn, DS_FIND_CHILDREN); 9300b69c2f0Sahrens } else { 9311d452cf5Sahrens err = dmu_objset_snapshot_one(fsname, &sn); 9320b69c2f0Sahrens } 9331d452cf5Sahrens 934ea2f5b9eSMatthew Ahrens if (err == 0) 935ea2f5b9eSMatthew Ahrens err = dsl_sync_task_group_wait(sn.dstg); 9361d452cf5Sahrens 9371d452cf5Sahrens for (dst = list_head(&sn.dstg->dstg_tasks); dst; 9381d452cf5Sahrens dst = list_next(&sn.dstg->dstg_tasks, dst)) { 939ea2f5b9eSMatthew Ahrens objset_t *os = dst->dst_arg1; 940503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 9411d452cf5Sahrens if (dst->dst_err) 9423cb34c60Sahrens dsl_dataset_name(ds, sn.failed); 943ea2f5b9eSMatthew Ahrens zil_resume(dmu_objset_zil(os)); 944503ad85cSMatthew Ahrens dmu_objset_rele(os, &sn); 9453cb34c60Sahrens } 9463cb34c60Sahrens 9471d452cf5Sahrens if (err) 9481d452cf5Sahrens (void) strcpy(fsname, sn.failed); 9491d452cf5Sahrens dsl_sync_task_group_destroy(sn.dstg); 9501d452cf5Sahrens spa_close(spa, FTAG); 9511d452cf5Sahrens return (err); 9521d452cf5Sahrens } 9531d452cf5Sahrens 954fa9e4066Sahrens static void 95514843421SMatthew Ahrens dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx) 956fa9e4066Sahrens { 957c717a561Smaybee dnode_t *dn; 958faafa6e3Sahrens 959c717a561Smaybee while (dn = list_head(list)) { 960c717a561Smaybee ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT); 961c717a561Smaybee ASSERT(dn->dn_dbuf->db_data_pending); 962c717a561Smaybee /* 96314843421SMatthew Ahrens * Initialize dn_zio outside dnode_sync() because the 96414843421SMatthew Ahrens * meta-dnode needs to set it ouside dnode_sync(). 965c717a561Smaybee */ 966c717a561Smaybee dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio; 967c717a561Smaybee ASSERT(dn->dn_zio); 968faafa6e3Sahrens 969c717a561Smaybee ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS); 970c717a561Smaybee list_remove(list, dn); 97114843421SMatthew Ahrens 97214843421SMatthew Ahrens if (newlist) { 97314843421SMatthew Ahrens (void) dnode_add_ref(dn, newlist); 97414843421SMatthew Ahrens list_insert_tail(newlist, dn); 97514843421SMatthew Ahrens } 97614843421SMatthew Ahrens 977c717a561Smaybee dnode_sync(dn, tx); 978fa9e4066Sahrens } 979fa9e4066Sahrens } 980fa9e4066Sahrens 981fa9e4066Sahrens /* ARGSUSED */ 982fa9e4066Sahrens static void 983b24ab676SJeff Bonwick dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg) 984fa9e4066Sahrens { 985e14bb325SJeff Bonwick blkptr_t *bp = zio->io_bp; 986503ad85cSMatthew Ahrens objset_t *os = arg; 987c717a561Smaybee dnode_phys_t *dnp = &os->os_phys->os_meta_dnode; 988fa9e4066Sahrens 989e14bb325SJeff Bonwick ASSERT(bp == os->os_rootbp); 990e14bb325SJeff Bonwick ASSERT(BP_GET_TYPE(bp) == DMU_OT_OBJSET); 991e14bb325SJeff Bonwick ASSERT(BP_GET_LEVEL(bp) == 0); 9920a4e9518Sgw 993fa9e4066Sahrens /* 99414843421SMatthew Ahrens * Update rootbp fill count: it should be the number of objects 99514843421SMatthew Ahrens * allocated in the object set (not counting the "special" 99614843421SMatthew Ahrens * objects that are stored in the objset_phys_t -- the meta 99714843421SMatthew Ahrens * dnode and user/group accounting objects). 998fa9e4066Sahrens */ 99914843421SMatthew Ahrens bp->blk_fill = 0; 1000e14bb325SJeff Bonwick for (int i = 0; i < dnp->dn_nblkptr; i++) 1001c717a561Smaybee bp->blk_fill += dnp->dn_blkptr[i].blk_fill; 1002b24ab676SJeff Bonwick } 1003b24ab676SJeff Bonwick 1004b24ab676SJeff Bonwick /* ARGSUSED */ 1005b24ab676SJeff Bonwick static void 1006b24ab676SJeff Bonwick dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg) 1007b24ab676SJeff Bonwick { 1008b24ab676SJeff Bonwick blkptr_t *bp = zio->io_bp; 1009b24ab676SJeff Bonwick blkptr_t *bp_orig = &zio->io_bp_orig; 1010b24ab676SJeff Bonwick objset_t *os = arg; 10110a4e9518Sgw 1012e14bb325SJeff Bonwick if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { 1013b24ab676SJeff Bonwick ASSERT(BP_EQUAL(bp, bp_orig)); 1014e14bb325SJeff Bonwick } else { 1015b24ab676SJeff Bonwick dsl_dataset_t *ds = os->os_dsl_dataset; 1016b24ab676SJeff Bonwick dmu_tx_t *tx = os->os_synctx; 1017b24ab676SJeff Bonwick 1018b24ab676SJeff Bonwick (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE); 1019b24ab676SJeff Bonwick dsl_dataset_block_born(ds, bp, tx); 10200a4e9518Sgw } 1021c717a561Smaybee } 1022c717a561Smaybee 1023fa9e4066Sahrens /* called from dsl */ 1024fa9e4066Sahrens void 1025503ad85cSMatthew Ahrens dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx) 1026fa9e4066Sahrens { 1027fa9e4066Sahrens int txgoff; 1028ea8dc4b6Seschrock zbookmark_t zb; 1029b24ab676SJeff Bonwick zio_prop_t zp; 1030c717a561Smaybee zio_t *zio; 1031c717a561Smaybee list_t *list; 103214843421SMatthew Ahrens list_t *newlist = NULL; 1033c717a561Smaybee dbuf_dirty_record_t *dr; 1034c717a561Smaybee 1035c717a561Smaybee dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg); 1036fa9e4066Sahrens 1037fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 1038fa9e4066Sahrens /* XXX the write_done callback should really give us the tx... */ 1039fa9e4066Sahrens os->os_synctx = tx; 1040fa9e4066Sahrens 104187bd5c1eSahrens if (os->os_dsl_dataset == NULL) { 104287bd5c1eSahrens /* 104387bd5c1eSahrens * This is the MOS. If we have upgraded, 104487bd5c1eSahrens * spa_max_replication() could change, so reset 104587bd5c1eSahrens * os_copies here. 104687bd5c1eSahrens */ 104787bd5c1eSahrens os->os_copies = spa_max_replication(os->os_spa); 104887bd5c1eSahrens } 104987bd5c1eSahrens 1050fa9e4066Sahrens /* 1051c717a561Smaybee * Create the root block IO 1052fa9e4066Sahrens */ 1053b24ab676SJeff Bonwick SET_BOOKMARK(&zb, os->os_dsl_dataset ? 1054b24ab676SJeff Bonwick os->os_dsl_dataset->ds_object : DMU_META_OBJSET, 1055b24ab676SJeff Bonwick ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 10563f9d6ad7SLin Ling VERIFY3U(0, ==, arc_release_bp(os->os_phys_buf, &os->os_phys_buf, 10573f9d6ad7SLin Ling os->os_rootbp, os->os_spa, &zb)); 1058b24ab676SJeff Bonwick 1059b24ab676SJeff Bonwick dmu_write_policy(os, NULL, 0, 0, &zp); 1060b24ab676SJeff Bonwick 1061b24ab676SJeff Bonwick zio = arc_write(pio, os->os_spa, tx->tx_txg, 1062b24ab676SJeff Bonwick os->os_rootbp, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os), &zp, 1063b24ab676SJeff Bonwick dmu_objset_write_ready, dmu_objset_write_done, os, 1064e14bb325SJeff Bonwick ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb); 1065c717a561Smaybee 1066c717a561Smaybee /* 106714843421SMatthew Ahrens * Sync special dnodes - the parent IO for the sync is the root block 1068c717a561Smaybee */ 1069*744947dcSTom Erickson DMU_META_DNODE(os)->dn_zio = zio; 1070*744947dcSTom Erickson dnode_sync(DMU_META_DNODE(os), tx); 1071c717a561Smaybee 107214843421SMatthew Ahrens os->os_phys->os_flags = os->os_flags; 107314843421SMatthew Ahrens 1074*744947dcSTom Erickson if (DMU_USERUSED_DNODE(os) && 1075*744947dcSTom Erickson DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) { 1076*744947dcSTom Erickson DMU_USERUSED_DNODE(os)->dn_zio = zio; 1077*744947dcSTom Erickson dnode_sync(DMU_USERUSED_DNODE(os), tx); 1078*744947dcSTom Erickson DMU_GROUPUSED_DNODE(os)->dn_zio = zio; 1079*744947dcSTom Erickson dnode_sync(DMU_GROUPUSED_DNODE(os), tx); 108014843421SMatthew Ahrens } 108114843421SMatthew Ahrens 1082c717a561Smaybee txgoff = tx->tx_txg & TXG_MASK; 1083fa9e4066Sahrens 108414843421SMatthew Ahrens if (dmu_objset_userused_enabled(os)) { 108514843421SMatthew Ahrens newlist = &os->os_synced_dnodes; 108614843421SMatthew Ahrens /* 108714843421SMatthew Ahrens * We must create the list here because it uses the 108814843421SMatthew Ahrens * dn_dirty_link[] of this txg. 108914843421SMatthew Ahrens */ 109014843421SMatthew Ahrens list_create(newlist, sizeof (dnode_t), 109114843421SMatthew Ahrens offsetof(dnode_t, dn_dirty_link[txgoff])); 109214843421SMatthew Ahrens } 109314843421SMatthew Ahrens 109414843421SMatthew Ahrens dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx); 109514843421SMatthew Ahrens dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx); 1096fa9e4066Sahrens 1097*744947dcSTom Erickson list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff]; 1098c717a561Smaybee while (dr = list_head(list)) { 1099c717a561Smaybee ASSERT(dr->dr_dbuf->db_level == 0); 1100c717a561Smaybee list_remove(list, dr); 1101c717a561Smaybee if (dr->dr_zio) 1102c717a561Smaybee zio_nowait(dr->dr_zio); 1103c717a561Smaybee } 1104c717a561Smaybee /* 1105c717a561Smaybee * Free intent log blocks up to this tx. 1106c717a561Smaybee */ 1107c717a561Smaybee zil_sync(os->os_zil, tx); 1108088f3894Sahrens os->os_phys->os_zil_header = os->os_zil_header; 1109c717a561Smaybee zio_nowait(zio); 1110fa9e4066Sahrens } 1111fa9e4066Sahrens 1112b24ab676SJeff Bonwick boolean_t 1113b24ab676SJeff Bonwick dmu_objset_is_dirty(objset_t *os, uint64_t txg) 1114b24ab676SJeff Bonwick { 1115b24ab676SJeff Bonwick return (!list_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]) || 1116b24ab676SJeff Bonwick !list_is_empty(&os->os_free_dnodes[txg & TXG_MASK])); 1117b24ab676SJeff Bonwick } 1118b24ab676SJeff Bonwick 1119*744947dcSTom Erickson static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES]; 112014843421SMatthew Ahrens 112114843421SMatthew Ahrens void 112214843421SMatthew Ahrens dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb) 112314843421SMatthew Ahrens { 112414843421SMatthew Ahrens used_cbs[ost] = cb; 112514843421SMatthew Ahrens } 112614843421SMatthew Ahrens 112714843421SMatthew Ahrens boolean_t 1128503ad85cSMatthew Ahrens dmu_objset_userused_enabled(objset_t *os) 112914843421SMatthew Ahrens { 113014843421SMatthew Ahrens return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE && 1131*744947dcSTom Erickson used_cbs[os->os_phys->os_type] != NULL && 1132*744947dcSTom Erickson DMU_USERUSED_DNODE(os) != NULL); 113314843421SMatthew Ahrens } 113414843421SMatthew Ahrens 11359966ca11SMatthew Ahrens static void 11360a586ceaSMark Shellenbaum do_userquota_update(objset_t *os, uint64_t used, uint64_t flags, 11370a586ceaSMark Shellenbaum uint64_t user, uint64_t group, boolean_t subtract, dmu_tx_t *tx) 11389966ca11SMatthew Ahrens { 11390a586ceaSMark Shellenbaum if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) { 11400a586ceaSMark Shellenbaum int64_t delta = DNODE_SIZE + used; 11419966ca11SMatthew Ahrens if (subtract) 11429966ca11SMatthew Ahrens delta = -delta; 1143c33e334fSMatthew Ahrens VERIFY3U(0, ==, zap_increment_int(os, DMU_USERUSED_OBJECT, 11449966ca11SMatthew Ahrens user, delta, tx)); 1145c33e334fSMatthew Ahrens VERIFY3U(0, ==, zap_increment_int(os, DMU_GROUPUSED_OBJECT, 11469966ca11SMatthew Ahrens group, delta, tx)); 11479966ca11SMatthew Ahrens } 11489966ca11SMatthew Ahrens } 11499966ca11SMatthew Ahrens 115014843421SMatthew Ahrens void 11510a586ceaSMark Shellenbaum dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx) 115214843421SMatthew Ahrens { 115314843421SMatthew Ahrens dnode_t *dn; 115414843421SMatthew Ahrens list_t *list = &os->os_synced_dnodes; 115514843421SMatthew Ahrens 115614843421SMatthew Ahrens ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os)); 115714843421SMatthew Ahrens 115814843421SMatthew Ahrens while (dn = list_head(list)) { 11591d8ccc7bSMark Shellenbaum int flags; 116014843421SMatthew Ahrens ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object)); 116114843421SMatthew Ahrens ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE || 116214843421SMatthew Ahrens dn->dn_phys->dn_flags & 116314843421SMatthew Ahrens DNODE_FLAG_USERUSED_ACCOUNTED); 116414843421SMatthew Ahrens 116514843421SMatthew Ahrens /* Allocate the user/groupused objects if necessary. */ 1166*744947dcSTom Erickson if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) { 1167503ad85cSMatthew Ahrens VERIFY(0 == zap_create_claim(os, 116814843421SMatthew Ahrens DMU_USERUSED_OBJECT, 116914843421SMatthew Ahrens DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx)); 1170503ad85cSMatthew Ahrens VERIFY(0 == zap_create_claim(os, 117114843421SMatthew Ahrens DMU_GROUPUSED_OBJECT, 117214843421SMatthew Ahrens DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx)); 117314843421SMatthew Ahrens } 117414843421SMatthew Ahrens 117514843421SMatthew Ahrens /* 11769966ca11SMatthew Ahrens * We intentionally modify the zap object even if the 11770a586ceaSMark Shellenbaum * net delta is zero. Otherwise 11789966ca11SMatthew Ahrens * the block of the zap obj could be shared between 11799966ca11SMatthew Ahrens * datasets but need to be different between them after 11809966ca11SMatthew Ahrens * a bprewrite. 118114843421SMatthew Ahrens */ 118214843421SMatthew Ahrens 11831d8ccc7bSMark Shellenbaum flags = dn->dn_id_flags; 11841d8ccc7bSMark Shellenbaum ASSERT(flags); 11851d8ccc7bSMark Shellenbaum if (flags & DN_ID_OLD_EXIST) { 11860a586ceaSMark Shellenbaum do_userquota_update(os, dn->dn_oldused, dn->dn_oldflags, 11870a586ceaSMark Shellenbaum dn->dn_olduid, dn->dn_oldgid, B_TRUE, tx); 11880a586ceaSMark Shellenbaum } 11891d8ccc7bSMark Shellenbaum if (flags & DN_ID_NEW_EXIST) { 11900a586ceaSMark Shellenbaum do_userquota_update(os, DN_USED_BYTES(dn->dn_phys), 11910a586ceaSMark Shellenbaum dn->dn_phys->dn_flags, dn->dn_newuid, 11920a586ceaSMark Shellenbaum dn->dn_newgid, B_FALSE, tx); 11930a586ceaSMark Shellenbaum } 11940a586ceaSMark Shellenbaum 11951d8ccc7bSMark Shellenbaum mutex_enter(&dn->dn_mtx); 11960a586ceaSMark Shellenbaum dn->dn_oldused = 0; 11970a586ceaSMark Shellenbaum dn->dn_oldflags = 0; 11980a586ceaSMark Shellenbaum if (dn->dn_id_flags & DN_ID_NEW_EXIST) { 11990a586ceaSMark Shellenbaum dn->dn_olduid = dn->dn_newuid; 12000a586ceaSMark Shellenbaum dn->dn_oldgid = dn->dn_newgid; 12010a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_OLD_EXIST; 12020a586ceaSMark Shellenbaum if (dn->dn_bonuslen == 0) 12030a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_SPILL; 12040a586ceaSMark Shellenbaum else 12050a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_BONUS; 12060a586ceaSMark Shellenbaum } 120728d97a71SMark Shellenbaum dn->dn_id_flags &= ~(DN_ID_NEW_EXIST); 120814843421SMatthew Ahrens mutex_exit(&dn->dn_mtx); 120914843421SMatthew Ahrens 121014843421SMatthew Ahrens list_remove(list, dn); 121114843421SMatthew Ahrens dnode_rele(dn, list); 121214843421SMatthew Ahrens } 121314843421SMatthew Ahrens } 121414843421SMatthew Ahrens 121506e0070dSMark Shellenbaum /* 121606e0070dSMark Shellenbaum * Returns a pointer to data to find uid/gid from 121706e0070dSMark Shellenbaum * 121806e0070dSMark Shellenbaum * If a dirty record for transaction group that is syncing can't 121906e0070dSMark Shellenbaum * be found then NULL is returned. In the NULL case it is assumed 122006e0070dSMark Shellenbaum * the uid/gid aren't changing. 122106e0070dSMark Shellenbaum */ 122206e0070dSMark Shellenbaum static void * 122306e0070dSMark Shellenbaum dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx) 122406e0070dSMark Shellenbaum { 122506e0070dSMark Shellenbaum dbuf_dirty_record_t *dr, **drp; 122606e0070dSMark Shellenbaum void *data; 122706e0070dSMark Shellenbaum 122806e0070dSMark Shellenbaum if (db->db_dirtycnt == 0) 122906e0070dSMark Shellenbaum return (db->db.db_data); /* Nothing is changing */ 123006e0070dSMark Shellenbaum 123106e0070dSMark Shellenbaum for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next) 123206e0070dSMark Shellenbaum if (dr->dr_txg == tx->tx_txg) 123306e0070dSMark Shellenbaum break; 123406e0070dSMark Shellenbaum 1235*744947dcSTom Erickson if (dr == NULL) { 123606e0070dSMark Shellenbaum data = NULL; 1237*744947dcSTom Erickson } else { 1238*744947dcSTom Erickson dnode_t *dn; 1239*744947dcSTom Erickson 1240*744947dcSTom Erickson DB_DNODE_ENTER(dr->dr_dbuf); 1241*744947dcSTom Erickson dn = DB_DNODE(dr->dr_dbuf); 1242*744947dcSTom Erickson 1243*744947dcSTom Erickson if (dn->dn_bonuslen == 0 && 1244*744947dcSTom Erickson dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID) 1245*744947dcSTom Erickson data = dr->dt.dl.dr_data->b_data; 1246*744947dcSTom Erickson else 1247*744947dcSTom Erickson data = dr->dt.dl.dr_data; 1248*744947dcSTom Erickson 1249*744947dcSTom Erickson DB_DNODE_EXIT(dr->dr_dbuf); 1250*744947dcSTom Erickson } 1251*744947dcSTom Erickson 125206e0070dSMark Shellenbaum return (data); 125306e0070dSMark Shellenbaum } 125406e0070dSMark Shellenbaum 12550a586ceaSMark Shellenbaum void 125606e0070dSMark Shellenbaum dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx) 12570a586ceaSMark Shellenbaum { 12580a586ceaSMark Shellenbaum objset_t *os = dn->dn_objset; 12590a586ceaSMark Shellenbaum void *data = NULL; 126006e0070dSMark Shellenbaum dmu_buf_impl_t *db = NULL; 12610a586ceaSMark Shellenbaum uint64_t *user, *group; 12620a586ceaSMark Shellenbaum int flags = dn->dn_id_flags; 12630a586ceaSMark Shellenbaum int error; 126406e0070dSMark Shellenbaum boolean_t have_spill = B_FALSE; 12650a586ceaSMark Shellenbaum 12660a586ceaSMark Shellenbaum if (!dmu_objset_userused_enabled(dn->dn_objset)) 12670a586ceaSMark Shellenbaum return; 12680a586ceaSMark Shellenbaum 12690a586ceaSMark Shellenbaum if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST| 12700a586ceaSMark Shellenbaum DN_ID_CHKED_SPILL))) 12710a586ceaSMark Shellenbaum return; 12720a586ceaSMark Shellenbaum 12730a586ceaSMark Shellenbaum if (before && dn->dn_bonuslen != 0) 12740a586ceaSMark Shellenbaum data = DN_BONUS(dn->dn_phys); 127506e0070dSMark Shellenbaum else if (!before && dn->dn_bonuslen != 0) { 127606e0070dSMark Shellenbaum if (dn->dn_bonus) { 127706e0070dSMark Shellenbaum db = dn->dn_bonus; 127806e0070dSMark Shellenbaum mutex_enter(&db->db_mtx); 127906e0070dSMark Shellenbaum data = dmu_objset_userquota_find_data(db, tx); 128006e0070dSMark Shellenbaum } else { 128106e0070dSMark Shellenbaum data = DN_BONUS(dn->dn_phys); 128206e0070dSMark Shellenbaum } 128306e0070dSMark Shellenbaum } else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) { 12840a586ceaSMark Shellenbaum int rf = 0; 12850a586ceaSMark Shellenbaum 12860a586ceaSMark Shellenbaum if (RW_WRITE_HELD(&dn->dn_struct_rwlock)) 12870a586ceaSMark Shellenbaum rf |= DB_RF_HAVESTRUCT; 12881d8ccc7bSMark Shellenbaum error = dmu_spill_hold_by_dnode(dn, 12891d8ccc7bSMark Shellenbaum rf | DB_RF_MUST_SUCCEED, 129006e0070dSMark Shellenbaum FTAG, (dmu_buf_t **)&db); 12910a586ceaSMark Shellenbaum ASSERT(error == 0); 129206e0070dSMark Shellenbaum mutex_enter(&db->db_mtx); 129306e0070dSMark Shellenbaum data = (before) ? db->db.db_data : 129406e0070dSMark Shellenbaum dmu_objset_userquota_find_data(db, tx); 129506e0070dSMark Shellenbaum have_spill = B_TRUE; 12960a586ceaSMark Shellenbaum } else { 12970a586ceaSMark Shellenbaum mutex_enter(&dn->dn_mtx); 12980a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_BONUS; 12990a586ceaSMark Shellenbaum mutex_exit(&dn->dn_mtx); 13000a586ceaSMark Shellenbaum return; 13010a586ceaSMark Shellenbaum } 13020a586ceaSMark Shellenbaum 13030a586ceaSMark Shellenbaum if (before) { 130406e0070dSMark Shellenbaum ASSERT(data); 13050a586ceaSMark Shellenbaum user = &dn->dn_olduid; 13060a586ceaSMark Shellenbaum group = &dn->dn_oldgid; 130706e0070dSMark Shellenbaum } else if (data) { 13080a586ceaSMark Shellenbaum user = &dn->dn_newuid; 13090a586ceaSMark Shellenbaum group = &dn->dn_newgid; 13100a586ceaSMark Shellenbaum } 13110a586ceaSMark Shellenbaum 131206e0070dSMark Shellenbaum /* 131306e0070dSMark Shellenbaum * Must always call the callback in case the object 131406e0070dSMark Shellenbaum * type has changed and that type isn't an object type to track 131506e0070dSMark Shellenbaum */ 13160a586ceaSMark Shellenbaum error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data, 13170a586ceaSMark Shellenbaum user, group); 13180a586ceaSMark Shellenbaum 131906e0070dSMark Shellenbaum /* 132006e0070dSMark Shellenbaum * Preserve existing uid/gid when the callback can't determine 132106e0070dSMark Shellenbaum * what the new uid/gid are and the callback returned EEXIST. 132206e0070dSMark Shellenbaum * The EEXIST error tells us to just use the existing uid/gid. 132306e0070dSMark Shellenbaum * If we don't know what the old values are then just assign 132406e0070dSMark Shellenbaum * them to 0, since that is a new file being created. 132506e0070dSMark Shellenbaum */ 132606e0070dSMark Shellenbaum if (!before && data == NULL && error == EEXIST) { 132706e0070dSMark Shellenbaum if (flags & DN_ID_OLD_EXIST) { 132806e0070dSMark Shellenbaum dn->dn_newuid = dn->dn_olduid; 132906e0070dSMark Shellenbaum dn->dn_newgid = dn->dn_oldgid; 133006e0070dSMark Shellenbaum } else { 133106e0070dSMark Shellenbaum dn->dn_newuid = 0; 133206e0070dSMark Shellenbaum dn->dn_newgid = 0; 133306e0070dSMark Shellenbaum } 133406e0070dSMark Shellenbaum error = 0; 133506e0070dSMark Shellenbaum } 133606e0070dSMark Shellenbaum 133706e0070dSMark Shellenbaum if (db) 133806e0070dSMark Shellenbaum mutex_exit(&db->db_mtx); 133906e0070dSMark Shellenbaum 13400a586ceaSMark Shellenbaum mutex_enter(&dn->dn_mtx); 13410a586ceaSMark Shellenbaum if (error == 0 && before) 13420a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_OLD_EXIST; 13430a586ceaSMark Shellenbaum if (error == 0 && !before) 13440a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_NEW_EXIST; 13450a586ceaSMark Shellenbaum 134606e0070dSMark Shellenbaum if (have_spill) { 13470a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_SPILL; 13480a586ceaSMark Shellenbaum } else { 13490a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_BONUS; 13500a586ceaSMark Shellenbaum } 13510a586ceaSMark Shellenbaum mutex_exit(&dn->dn_mtx); 135206e0070dSMark Shellenbaum if (have_spill) 135306e0070dSMark Shellenbaum dmu_buf_rele((dmu_buf_t *)db, FTAG); 13540a586ceaSMark Shellenbaum } 13550a586ceaSMark Shellenbaum 135614843421SMatthew Ahrens boolean_t 135714843421SMatthew Ahrens dmu_objset_userspace_present(objset_t *os) 135814843421SMatthew Ahrens { 1359503ad85cSMatthew Ahrens return (os->os_phys->os_flags & 136014843421SMatthew Ahrens OBJSET_FLAG_USERACCOUNTING_COMPLETE); 136114843421SMatthew Ahrens } 136214843421SMatthew Ahrens 136314843421SMatthew Ahrens int 136414843421SMatthew Ahrens dmu_objset_userspace_upgrade(objset_t *os) 136514843421SMatthew Ahrens { 136614843421SMatthew Ahrens uint64_t obj; 136714843421SMatthew Ahrens int err = 0; 136814843421SMatthew Ahrens 136914843421SMatthew Ahrens if (dmu_objset_userspace_present(os)) 137014843421SMatthew Ahrens return (0); 1371503ad85cSMatthew Ahrens if (!dmu_objset_userused_enabled(os)) 137214843421SMatthew Ahrens return (ENOTSUP); 137314843421SMatthew Ahrens if (dmu_objset_is_snapshot(os)) 137414843421SMatthew Ahrens return (EINVAL); 137514843421SMatthew Ahrens 137614843421SMatthew Ahrens /* 137714843421SMatthew Ahrens * We simply need to mark every object dirty, so that it will be 137814843421SMatthew Ahrens * synced out and now accounted. If this is called 137914843421SMatthew Ahrens * concurrently, or if we already did some work before crashing, 138014843421SMatthew Ahrens * that's fine, since we track each object's accounted state 138114843421SMatthew Ahrens * independently. 138214843421SMatthew Ahrens */ 138314843421SMatthew Ahrens 138414843421SMatthew Ahrens for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) { 138568857716SLin Ling dmu_tx_t *tx; 138614843421SMatthew Ahrens dmu_buf_t *db; 138714843421SMatthew Ahrens int objerr; 138814843421SMatthew Ahrens 138914843421SMatthew Ahrens if (issig(JUSTLOOKING) && issig(FORREAL)) 139014843421SMatthew Ahrens return (EINTR); 139114843421SMatthew Ahrens 139214843421SMatthew Ahrens objerr = dmu_bonus_hold(os, obj, FTAG, &db); 139314843421SMatthew Ahrens if (objerr) 139414843421SMatthew Ahrens continue; 139568857716SLin Ling tx = dmu_tx_create(os); 139614843421SMatthew Ahrens dmu_tx_hold_bonus(tx, obj); 139714843421SMatthew Ahrens objerr = dmu_tx_assign(tx, TXG_WAIT); 139814843421SMatthew Ahrens if (objerr) { 139914843421SMatthew Ahrens dmu_tx_abort(tx); 140014843421SMatthew Ahrens continue; 140114843421SMatthew Ahrens } 140214843421SMatthew Ahrens dmu_buf_will_dirty(db, tx); 140314843421SMatthew Ahrens dmu_buf_rele(db, FTAG); 140414843421SMatthew Ahrens dmu_tx_commit(tx); 140514843421SMatthew Ahrens } 140614843421SMatthew Ahrens 1407503ad85cSMatthew Ahrens os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE; 140814843421SMatthew Ahrens txg_wait_synced(dmu_objset_pool(os), 0); 140914843421SMatthew Ahrens return (0); 141014843421SMatthew Ahrens } 141114843421SMatthew Ahrens 1412fa9e4066Sahrens void 1413a2eea2e1Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp, 1414a2eea2e1Sahrens uint64_t *usedobjsp, uint64_t *availobjsp) 1415fa9e4066Sahrens { 1416503ad85cSMatthew Ahrens dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp, 1417a2eea2e1Sahrens usedobjsp, availobjsp); 1418a2eea2e1Sahrens } 1419a2eea2e1Sahrens 1420a2eea2e1Sahrens uint64_t 1421a2eea2e1Sahrens dmu_objset_fsid_guid(objset_t *os) 1422a2eea2e1Sahrens { 1423503ad85cSMatthew Ahrens return (dsl_dataset_fsid_guid(os->os_dsl_dataset)); 1424a2eea2e1Sahrens } 1425a2eea2e1Sahrens 1426a2eea2e1Sahrens void 1427a2eea2e1Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat) 1428a2eea2e1Sahrens { 1429503ad85cSMatthew Ahrens stat->dds_type = os->os_phys->os_type; 1430503ad85cSMatthew Ahrens if (os->os_dsl_dataset) 1431503ad85cSMatthew Ahrens dsl_dataset_fast_stat(os->os_dsl_dataset, stat); 1432a2eea2e1Sahrens } 1433a2eea2e1Sahrens 1434a2eea2e1Sahrens void 1435a2eea2e1Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv) 1436a2eea2e1Sahrens { 1437503ad85cSMatthew Ahrens ASSERT(os->os_dsl_dataset || 1438503ad85cSMatthew Ahrens os->os_phys->os_type == DMU_OST_META); 1439a2eea2e1Sahrens 1440503ad85cSMatthew Ahrens if (os->os_dsl_dataset != NULL) 1441503ad85cSMatthew Ahrens dsl_dataset_stats(os->os_dsl_dataset, nv); 1442a2eea2e1Sahrens 1443a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE, 1444503ad85cSMatthew Ahrens os->os_phys->os_type); 144514843421SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING, 144614843421SMatthew Ahrens dmu_objset_userspace_present(os)); 1447fa9e4066Sahrens } 1448fa9e4066Sahrens 1449fa9e4066Sahrens int 1450fa9e4066Sahrens dmu_objset_is_snapshot(objset_t *os) 1451fa9e4066Sahrens { 1452503ad85cSMatthew Ahrens if (os->os_dsl_dataset != NULL) 1453503ad85cSMatthew Ahrens return (dsl_dataset_is_snapshot(os->os_dsl_dataset)); 1454fa9e4066Sahrens else 1455fa9e4066Sahrens return (B_FALSE); 1456fa9e4066Sahrens } 1457fa9e4066Sahrens 1458ab04eb8eStimh int 1459ab04eb8eStimh dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen, 1460ab04eb8eStimh boolean_t *conflict) 1461ab04eb8eStimh { 1462503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 1463ab04eb8eStimh uint64_t ignored; 1464ab04eb8eStimh 1465ab04eb8eStimh if (ds->ds_phys->ds_snapnames_zapobj == 0) 1466ab04eb8eStimh return (ENOENT); 1467ab04eb8eStimh 1468ab04eb8eStimh return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset, 1469ab04eb8eStimh ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST, 1470ab04eb8eStimh real, maxlen, conflict)); 1471ab04eb8eStimh } 1472ab04eb8eStimh 1473fa9e4066Sahrens int 1474fa9e4066Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name, 1475b38f0970Sck uint64_t *idp, uint64_t *offp, boolean_t *case_conflict) 1476fa9e4066Sahrens { 1477503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 1478fa9e4066Sahrens zap_cursor_t cursor; 1479fa9e4066Sahrens zap_attribute_t attr; 1480fa9e4066Sahrens 1481fa9e4066Sahrens if (ds->ds_phys->ds_snapnames_zapobj == 0) 1482fa9e4066Sahrens return (ENOENT); 1483fa9e4066Sahrens 1484fa9e4066Sahrens zap_cursor_init_serialized(&cursor, 1485fa9e4066Sahrens ds->ds_dir->dd_pool->dp_meta_objset, 1486fa9e4066Sahrens ds->ds_phys->ds_snapnames_zapobj, *offp); 1487fa9e4066Sahrens 148887e5029aSahrens if (zap_cursor_retrieve(&cursor, &attr) != 0) { 148987e5029aSahrens zap_cursor_fini(&cursor); 149087e5029aSahrens return (ENOENT); 149187e5029aSahrens } 149287e5029aSahrens 149387e5029aSahrens if (strlen(attr.za_name) + 1 > namelen) { 149487e5029aSahrens zap_cursor_fini(&cursor); 149587e5029aSahrens return (ENAMETOOLONG); 149687e5029aSahrens } 149787e5029aSahrens 149887e5029aSahrens (void) strcpy(name, attr.za_name); 149987e5029aSahrens if (idp) 150087e5029aSahrens *idp = attr.za_first_integer; 1501b38f0970Sck if (case_conflict) 1502b38f0970Sck *case_conflict = attr.za_normalization_conflict; 150387e5029aSahrens zap_cursor_advance(&cursor); 150487e5029aSahrens *offp = zap_cursor_serialize(&cursor); 150587e5029aSahrens zap_cursor_fini(&cursor); 150687e5029aSahrens 150787e5029aSahrens return (0); 150887e5029aSahrens } 150987e5029aSahrens 151087e5029aSahrens int 151187e5029aSahrens dmu_dir_list_next(objset_t *os, int namelen, char *name, 151287e5029aSahrens uint64_t *idp, uint64_t *offp) 151387e5029aSahrens { 1514503ad85cSMatthew Ahrens dsl_dir_t *dd = os->os_dsl_dataset->ds_dir; 151587e5029aSahrens zap_cursor_t cursor; 151687e5029aSahrens zap_attribute_t attr; 151787e5029aSahrens 151887e5029aSahrens /* there is no next dir on a snapshot! */ 1519503ad85cSMatthew Ahrens if (os->os_dsl_dataset->ds_object != 152087e5029aSahrens dd->dd_phys->dd_head_dataset_obj) 1521fa9e4066Sahrens return (ENOENT); 1522fa9e4066Sahrens 152387e5029aSahrens zap_cursor_init_serialized(&cursor, 152487e5029aSahrens dd->dd_pool->dp_meta_objset, 152587e5029aSahrens dd->dd_phys->dd_child_dir_zapobj, *offp); 152687e5029aSahrens 152787e5029aSahrens if (zap_cursor_retrieve(&cursor, &attr) != 0) { 152887e5029aSahrens zap_cursor_fini(&cursor); 152987e5029aSahrens return (ENOENT); 153087e5029aSahrens } 153187e5029aSahrens 153287e5029aSahrens if (strlen(attr.za_name) + 1 > namelen) { 153387e5029aSahrens zap_cursor_fini(&cursor); 1534fa9e4066Sahrens return (ENAMETOOLONG); 153587e5029aSahrens } 1536fa9e4066Sahrens 1537fa9e4066Sahrens (void) strcpy(name, attr.za_name); 153887e5029aSahrens if (idp) 153987e5029aSahrens *idp = attr.za_first_integer; 1540fa9e4066Sahrens zap_cursor_advance(&cursor); 1541fa9e4066Sahrens *offp = zap_cursor_serialize(&cursor); 154287e5029aSahrens zap_cursor_fini(&cursor); 1543fa9e4066Sahrens 1544fa9e4066Sahrens return (0); 1545fa9e4066Sahrens } 1546fa9e4066Sahrens 1547088f3894Sahrens struct findarg { 1548fd136879SMatthew Ahrens int (*func)(const char *, void *); 1549088f3894Sahrens void *arg; 1550088f3894Sahrens }; 1551088f3894Sahrens 1552088f3894Sahrens /* ARGSUSED */ 1553088f3894Sahrens static int 1554088f3894Sahrens findfunc(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg) 1555088f3894Sahrens { 1556088f3894Sahrens struct findarg *fa = arg; 1557fd136879SMatthew Ahrens return (fa->func(dsname, fa->arg)); 1558088f3894Sahrens } 1559088f3894Sahrens 1560fa9e4066Sahrens /* 1561fa9e4066Sahrens * Find all objsets under name, and for each, call 'func(child_name, arg)'. 1562088f3894Sahrens * Perhaps change all callers to use dmu_objset_find_spa()? 1563fa9e4066Sahrens */ 15641d452cf5Sahrens int 1565fd136879SMatthew Ahrens dmu_objset_find(char *name, int func(const char *, void *), void *arg, 1566fd136879SMatthew Ahrens int flags) 1567088f3894Sahrens { 1568088f3894Sahrens struct findarg fa; 1569088f3894Sahrens fa.func = func; 1570088f3894Sahrens fa.arg = arg; 1571088f3894Sahrens return (dmu_objset_find_spa(NULL, name, findfunc, &fa, flags)); 1572088f3894Sahrens } 1573088f3894Sahrens 1574088f3894Sahrens /* 1575088f3894Sahrens * Find all objsets under name, call func on each 1576088f3894Sahrens */ 1577088f3894Sahrens int 1578088f3894Sahrens dmu_objset_find_spa(spa_t *spa, const char *name, 1579088f3894Sahrens int func(spa_t *, uint64_t, const char *, void *), void *arg, int flags) 1580fa9e4066Sahrens { 1581fa9e4066Sahrens dsl_dir_t *dd; 1582088f3894Sahrens dsl_pool_t *dp; 1583088f3894Sahrens dsl_dataset_t *ds; 1584fa9e4066Sahrens zap_cursor_t zc; 1585b7661cccSmmusante zap_attribute_t *attr; 1586fa9e4066Sahrens char *child; 1587088f3894Sahrens uint64_t thisobj; 1588088f3894Sahrens int err; 1589fa9e4066Sahrens 1590088f3894Sahrens if (name == NULL) 1591088f3894Sahrens name = spa_name(spa); 1592088f3894Sahrens err = dsl_dir_open_spa(spa, name, FTAG, &dd, NULL); 1593ea8dc4b6Seschrock if (err) 15941d452cf5Sahrens return (err); 1595fa9e4066Sahrens 1596088f3894Sahrens /* Don't visit hidden ($MOS & $ORIGIN) objsets. */ 1597088f3894Sahrens if (dd->dd_myname[0] == '$') { 1598088f3894Sahrens dsl_dir_close(dd, FTAG); 1599088f3894Sahrens return (0); 1600088f3894Sahrens } 1601088f3894Sahrens 1602088f3894Sahrens thisobj = dd->dd_phys->dd_head_dataset_obj; 1603b7661cccSmmusante attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); 1604088f3894Sahrens dp = dd->dd_pool; 1605fa9e4066Sahrens 1606fa9e4066Sahrens /* 1607fa9e4066Sahrens * Iterate over all children. 1608fa9e4066Sahrens */ 16090b69c2f0Sahrens if (flags & DS_FIND_CHILDREN) { 1610088f3894Sahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, 16110b69c2f0Sahrens dd->dd_phys->dd_child_dir_zapobj); 1612b7661cccSmmusante zap_cursor_retrieve(&zc, attr) == 0; 16130b69c2f0Sahrens (void) zap_cursor_advance(&zc)) { 1614b7661cccSmmusante ASSERT(attr->za_integer_length == sizeof (uint64_t)); 1615b7661cccSmmusante ASSERT(attr->za_num_integers == 1); 1616fa9e4066Sahrens 1617486ae710SMatthew Ahrens child = kmem_asprintf("%s/%s", name, attr->za_name); 1618088f3894Sahrens err = dmu_objset_find_spa(spa, child, func, arg, flags); 1619486ae710SMatthew Ahrens strfree(child); 16200b69c2f0Sahrens if (err) 16210b69c2f0Sahrens break; 16220b69c2f0Sahrens } 16230b69c2f0Sahrens zap_cursor_fini(&zc); 16241d452cf5Sahrens 16250b69c2f0Sahrens if (err) { 16260b69c2f0Sahrens dsl_dir_close(dd, FTAG); 1627b7661cccSmmusante kmem_free(attr, sizeof (zap_attribute_t)); 16280b69c2f0Sahrens return (err); 16290b69c2f0Sahrens } 1630fa9e4066Sahrens } 1631fa9e4066Sahrens 1632fa9e4066Sahrens /* 1633fa9e4066Sahrens * Iterate over all snapshots. 1634fa9e4066Sahrens */ 1635088f3894Sahrens if (flags & DS_FIND_SNAPSHOTS) { 1636088f3894Sahrens if (!dsl_pool_sync_context(dp)) 1637088f3894Sahrens rw_enter(&dp->dp_config_rwlock, RW_READER); 1638088f3894Sahrens err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds); 1639088f3894Sahrens if (!dsl_pool_sync_context(dp)) 1640088f3894Sahrens rw_exit(&dp->dp_config_rwlock); 1641088f3894Sahrens 1642088f3894Sahrens if (err == 0) { 1643088f3894Sahrens uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj; 1644088f3894Sahrens dsl_dataset_rele(ds, FTAG); 1645088f3894Sahrens 1646088f3894Sahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj); 1647088f3894Sahrens zap_cursor_retrieve(&zc, attr) == 0; 1648088f3894Sahrens (void) zap_cursor_advance(&zc)) { 1649088f3894Sahrens ASSERT(attr->za_integer_length == 1650088f3894Sahrens sizeof (uint64_t)); 1651088f3894Sahrens ASSERT(attr->za_num_integers == 1); 1652088f3894Sahrens 1653486ae710SMatthew Ahrens child = kmem_asprintf("%s@%s", 1654486ae710SMatthew Ahrens name, attr->za_name); 1655088f3894Sahrens err = func(spa, attr->za_first_integer, 1656088f3894Sahrens child, arg); 1657486ae710SMatthew Ahrens strfree(child); 1658088f3894Sahrens if (err) 1659088f3894Sahrens break; 1660088f3894Sahrens } 1661088f3894Sahrens zap_cursor_fini(&zc); 1662fa9e4066Sahrens } 1663fa9e4066Sahrens } 1664fa9e4066Sahrens 1665fa9e4066Sahrens dsl_dir_close(dd, FTAG); 1666b7661cccSmmusante kmem_free(attr, sizeof (zap_attribute_t)); 1667fa9e4066Sahrens 16681d452cf5Sahrens if (err) 16691d452cf5Sahrens return (err); 16701d452cf5Sahrens 1671fa9e4066Sahrens /* 1672fa9e4066Sahrens * Apply to self if appropriate. 1673fa9e4066Sahrens */ 1674088f3894Sahrens err = func(spa, thisobj, name, arg); 16751d452cf5Sahrens return (err); 1676fa9e4066Sahrens } 1677f18faf3fSek 16787f73c863SRich Morris /* ARGSUSED */ 16797f73c863SRich Morris int 1680fd136879SMatthew Ahrens dmu_objset_prefetch(const char *name, void *arg) 16817f73c863SRich Morris { 16827f73c863SRich Morris dsl_dataset_t *ds; 16837f73c863SRich Morris 16847f73c863SRich Morris if (dsl_dataset_hold(name, FTAG, &ds)) 16857f73c863SRich Morris return (0); 16867f73c863SRich Morris 16877f73c863SRich Morris if (!BP_IS_HOLE(&ds->ds_phys->ds_bp)) { 16887f73c863SRich Morris mutex_enter(&ds->ds_opening_lock); 1689503ad85cSMatthew Ahrens if (ds->ds_objset == NULL) { 16907f73c863SRich Morris uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH; 16917f73c863SRich Morris zbookmark_t zb; 16927f73c863SRich Morris 1693b24ab676SJeff Bonwick SET_BOOKMARK(&zb, ds->ds_object, ZB_ROOT_OBJECT, 1694b24ab676SJeff Bonwick ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 16957f73c863SRich Morris 16963f9d6ad7SLin Ling (void) dsl_read_nolock(NULL, dsl_dataset_get_spa(ds), 16977f73c863SRich Morris &ds->ds_phys->ds_bp, NULL, NULL, 16987f73c863SRich Morris ZIO_PRIORITY_ASYNC_READ, 16997f73c863SRich Morris ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, 17007f73c863SRich Morris &aflags, &zb); 17017f73c863SRich Morris } 17027f73c863SRich Morris mutex_exit(&ds->ds_opening_lock); 17037f73c863SRich Morris } 17047f73c863SRich Morris 17057f73c863SRich Morris dsl_dataset_rele(ds, FTAG); 17067f73c863SRich Morris return (0); 17077f73c863SRich Morris } 17087f73c863SRich Morris 1709f18faf3fSek void 1710f18faf3fSek dmu_objset_set_user(objset_t *os, void *user_ptr) 1711f18faf3fSek { 1712503ad85cSMatthew Ahrens ASSERT(MUTEX_HELD(&os->os_user_ptr_lock)); 1713503ad85cSMatthew Ahrens os->os_user_ptr = user_ptr; 1714f18faf3fSek } 1715f18faf3fSek 1716f18faf3fSek void * 1717f18faf3fSek dmu_objset_get_user(objset_t *os) 1718f18faf3fSek { 1719503ad85cSMatthew Ahrens ASSERT(MUTEX_HELD(&os->os_user_ptr_lock)); 1720503ad85cSMatthew Ahrens return (os->os_user_ptr); 1721f18faf3fSek } 1722