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> 45*99d5e173STim Haley #include <sys/zfs_onexit.h> 46fa9e4066Sahrens 47744947dcSTom Erickson /* 48744947dcSTom Erickson * Needed to close a window in dnode_move() that allows the objset to be freed 49744947dcSTom Erickson * before it can be safely accessed. 50744947dcSTom Erickson */ 51744947dcSTom Erickson krwlock_t os_lock; 52744947dcSTom Erickson 53744947dcSTom Erickson void 54744947dcSTom Erickson dmu_objset_init(void) 55744947dcSTom Erickson { 56744947dcSTom Erickson rw_init(&os_lock, NULL, RW_DEFAULT, NULL); 57744947dcSTom Erickson } 58744947dcSTom Erickson 59744947dcSTom Erickson void 60744947dcSTom Erickson dmu_objset_fini(void) 61744947dcSTom Erickson { 62744947dcSTom Erickson rw_destroy(&os_lock); 63744947dcSTom Erickson } 64744947dcSTom Erickson 65fa9e4066Sahrens spa_t * 66fa9e4066Sahrens dmu_objset_spa(objset_t *os) 67fa9e4066Sahrens { 68503ad85cSMatthew Ahrens return (os->os_spa); 69fa9e4066Sahrens } 70fa9e4066Sahrens 71fa9e4066Sahrens zilog_t * 72fa9e4066Sahrens dmu_objset_zil(objset_t *os) 73fa9e4066Sahrens { 74503ad85cSMatthew Ahrens return (os->os_zil); 75fa9e4066Sahrens } 76fa9e4066Sahrens 77fa9e4066Sahrens dsl_pool_t * 78fa9e4066Sahrens dmu_objset_pool(objset_t *os) 79fa9e4066Sahrens { 80fa9e4066Sahrens dsl_dataset_t *ds; 81fa9e4066Sahrens 82503ad85cSMatthew Ahrens if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir) 83fa9e4066Sahrens return (ds->ds_dir->dd_pool); 84fa9e4066Sahrens else 85503ad85cSMatthew Ahrens return (spa_get_dsl(os->os_spa)); 86fa9e4066Sahrens } 87fa9e4066Sahrens 88fa9e4066Sahrens dsl_dataset_t * 89fa9e4066Sahrens dmu_objset_ds(objset_t *os) 90fa9e4066Sahrens { 91503ad85cSMatthew Ahrens return (os->os_dsl_dataset); 92fa9e4066Sahrens } 93fa9e4066Sahrens 94fa9e4066Sahrens dmu_objset_type_t 95fa9e4066Sahrens dmu_objset_type(objset_t *os) 96fa9e4066Sahrens { 97503ad85cSMatthew Ahrens return (os->os_phys->os_type); 98fa9e4066Sahrens } 99fa9e4066Sahrens 100fa9e4066Sahrens void 101fa9e4066Sahrens dmu_objset_name(objset_t *os, char *buf) 102fa9e4066Sahrens { 103503ad85cSMatthew Ahrens dsl_dataset_name(os->os_dsl_dataset, buf); 104fa9e4066Sahrens } 105fa9e4066Sahrens 106fa9e4066Sahrens uint64_t 107fa9e4066Sahrens dmu_objset_id(objset_t *os) 108fa9e4066Sahrens { 109503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 110fa9e4066Sahrens 111fa9e4066Sahrens return (ds ? ds->ds_object : 0); 112fa9e4066Sahrens } 113fa9e4066Sahrens 11455da60b9SMark J Musante uint64_t 11555da60b9SMark J Musante dmu_objset_syncprop(objset_t *os) 11655da60b9SMark J Musante { 11755da60b9SMark J Musante return (os->os_sync); 11855da60b9SMark J Musante } 11955da60b9SMark J Musante 120e09fa4daSNeil Perrin uint64_t 121e09fa4daSNeil Perrin dmu_objset_logbias(objset_t *os) 122e09fa4daSNeil Perrin { 123e09fa4daSNeil Perrin return (os->os_logbias); 124e09fa4daSNeil Perrin } 125e09fa4daSNeil Perrin 126fa9e4066Sahrens static void 127fa9e4066Sahrens checksum_changed_cb(void *arg, uint64_t newval) 128fa9e4066Sahrens { 129503ad85cSMatthew Ahrens objset_t *os = arg; 130fa9e4066Sahrens 131fa9e4066Sahrens /* 132fa9e4066Sahrens * Inheritance should have been done by now. 133fa9e4066Sahrens */ 134fa9e4066Sahrens ASSERT(newval != ZIO_CHECKSUM_INHERIT); 135fa9e4066Sahrens 136503ad85cSMatthew Ahrens os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE); 137fa9e4066Sahrens } 138fa9e4066Sahrens 139fa9e4066Sahrens static void 140fa9e4066Sahrens compression_changed_cb(void *arg, uint64_t newval) 141fa9e4066Sahrens { 142503ad85cSMatthew Ahrens objset_t *os = arg; 143fa9e4066Sahrens 144fa9e4066Sahrens /* 145fa9e4066Sahrens * Inheritance and range checking should have been done by now. 146fa9e4066Sahrens */ 147fa9e4066Sahrens ASSERT(newval != ZIO_COMPRESS_INHERIT); 148fa9e4066Sahrens 149503ad85cSMatthew Ahrens os->os_compress = zio_compress_select(newval, ZIO_COMPRESS_ON_VALUE); 150fa9e4066Sahrens } 151fa9e4066Sahrens 152d0ad202dSahrens static void 153d0ad202dSahrens copies_changed_cb(void *arg, uint64_t newval) 154d0ad202dSahrens { 155503ad85cSMatthew Ahrens objset_t *os = arg; 156d0ad202dSahrens 157d0ad202dSahrens /* 158d0ad202dSahrens * Inheritance and range checking should have been done by now. 159d0ad202dSahrens */ 160d0ad202dSahrens ASSERT(newval > 0); 161503ad85cSMatthew Ahrens ASSERT(newval <= spa_max_replication(os->os_spa)); 162d0ad202dSahrens 163503ad85cSMatthew Ahrens os->os_copies = newval; 164d0ad202dSahrens } 165d0ad202dSahrens 166b24ab676SJeff Bonwick static void 167b24ab676SJeff Bonwick dedup_changed_cb(void *arg, uint64_t newval) 168b24ab676SJeff Bonwick { 169b24ab676SJeff Bonwick objset_t *os = arg; 170b24ab676SJeff Bonwick spa_t *spa = os->os_spa; 171b24ab676SJeff Bonwick enum zio_checksum checksum; 172b24ab676SJeff Bonwick 173b24ab676SJeff Bonwick /* 174b24ab676SJeff Bonwick * Inheritance should have been done by now. 175b24ab676SJeff Bonwick */ 176b24ab676SJeff Bonwick ASSERT(newval != ZIO_CHECKSUM_INHERIT); 177b24ab676SJeff Bonwick 178b24ab676SJeff Bonwick checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF); 179b24ab676SJeff Bonwick 180b24ab676SJeff Bonwick os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK; 181b24ab676SJeff Bonwick os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY); 182b24ab676SJeff Bonwick } 183b24ab676SJeff Bonwick 1843baa08fcSek static void 1853baa08fcSek primary_cache_changed_cb(void *arg, uint64_t newval) 1863baa08fcSek { 187503ad85cSMatthew Ahrens objset_t *os = arg; 1883baa08fcSek 1893baa08fcSek /* 1903baa08fcSek * Inheritance and range checking should have been done by now. 1913baa08fcSek */ 1923baa08fcSek ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE || 1933baa08fcSek newval == ZFS_CACHE_METADATA); 1943baa08fcSek 195503ad85cSMatthew Ahrens os->os_primary_cache = newval; 1963baa08fcSek } 1973baa08fcSek 1983baa08fcSek static void 1993baa08fcSek secondary_cache_changed_cb(void *arg, uint64_t newval) 2003baa08fcSek { 201503ad85cSMatthew Ahrens objset_t *os = arg; 2023baa08fcSek 2033baa08fcSek /* 2043baa08fcSek * Inheritance and range checking should have been done by now. 2053baa08fcSek */ 2063baa08fcSek ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE || 2073baa08fcSek newval == ZFS_CACHE_METADATA); 2083baa08fcSek 209503ad85cSMatthew Ahrens os->os_secondary_cache = newval; 2103baa08fcSek } 2113baa08fcSek 21255da60b9SMark J Musante static void 21355da60b9SMark J Musante sync_changed_cb(void *arg, uint64_t newval) 21455da60b9SMark J Musante { 21555da60b9SMark J Musante objset_t *os = arg; 21655da60b9SMark J Musante 21755da60b9SMark J Musante /* 21855da60b9SMark J Musante * Inheritance and range checking should have been done by now. 21955da60b9SMark J Musante */ 22055da60b9SMark J Musante ASSERT(newval == ZFS_SYNC_STANDARD || newval == ZFS_SYNC_ALWAYS || 22155da60b9SMark J Musante newval == ZFS_SYNC_DISABLED); 22255da60b9SMark J Musante 22355da60b9SMark J Musante os->os_sync = newval; 22455da60b9SMark J Musante if (os->os_zil) 22555da60b9SMark J Musante zil_set_sync(os->os_zil, newval); 22655da60b9SMark J Musante } 22755da60b9SMark J Musante 228e09fa4daSNeil Perrin static void 229e09fa4daSNeil Perrin logbias_changed_cb(void *arg, uint64_t newval) 230e09fa4daSNeil Perrin { 231e09fa4daSNeil Perrin objset_t *os = arg; 232e09fa4daSNeil Perrin 233e09fa4daSNeil Perrin ASSERT(newval == ZFS_LOGBIAS_LATENCY || 234e09fa4daSNeil Perrin newval == ZFS_LOGBIAS_THROUGHPUT); 235e09fa4daSNeil Perrin os->os_logbias = newval; 236e09fa4daSNeil Perrin if (os->os_zil) 237e09fa4daSNeil Perrin zil_set_logbias(os->os_zil, newval); 238e09fa4daSNeil Perrin } 239e09fa4daSNeil Perrin 240fa9e4066Sahrens void 241fa9e4066Sahrens dmu_objset_byteswap(void *buf, size_t size) 242fa9e4066Sahrens { 243fa9e4066Sahrens objset_phys_t *osp = buf; 244fa9e4066Sahrens 24514843421SMatthew Ahrens ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t)); 246fa9e4066Sahrens dnode_byteswap(&osp->os_meta_dnode); 247fa9e4066Sahrens byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t)); 248fa9e4066Sahrens osp->os_type = BSWAP_64(osp->os_type); 24914843421SMatthew Ahrens osp->os_flags = BSWAP_64(osp->os_flags); 25014843421SMatthew Ahrens if (size == sizeof (objset_phys_t)) { 25114843421SMatthew Ahrens dnode_byteswap(&osp->os_userused_dnode); 25214843421SMatthew Ahrens dnode_byteswap(&osp->os_groupused_dnode); 25314843421SMatthew Ahrens } 254fa9e4066Sahrens } 255fa9e4066Sahrens 256ea8dc4b6Seschrock int 257ea8dc4b6Seschrock dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, 258503ad85cSMatthew Ahrens objset_t **osp) 259fa9e4066Sahrens { 260503ad85cSMatthew Ahrens objset_t *os; 261088f3894Sahrens int i, err; 262fa9e4066Sahrens 26391ebeef5Sahrens ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock)); 26491ebeef5Sahrens 265503ad85cSMatthew Ahrens os = kmem_zalloc(sizeof (objset_t), KM_SLEEP); 266503ad85cSMatthew Ahrens os->os_dsl_dataset = ds; 267503ad85cSMatthew Ahrens os->os_spa = spa; 268503ad85cSMatthew Ahrens os->os_rootbp = bp; 269503ad85cSMatthew Ahrens if (!BP_IS_HOLE(os->os_rootbp)) { 27013506d1eSmaybee uint32_t aflags = ARC_WAIT; 271ea8dc4b6Seschrock zbookmark_t zb; 272b24ab676SJeff Bonwick SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET, 273b24ab676SJeff Bonwick ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 274b24ab676SJeff Bonwick 275503ad85cSMatthew Ahrens if (DMU_OS_IS_L2CACHEABLE(os)) 2763baa08fcSek aflags |= ARC_L2CACHE; 277ea8dc4b6Seschrock 278503ad85cSMatthew Ahrens dprintf_bp(os->os_rootbp, "reading %s", ""); 279088f3894Sahrens /* 2803f9d6ad7SLin Ling * XXX when bprewrite scrub can change the bp, 281088f3894Sahrens * and this is called from dmu_objset_open_ds_os, the bp 282088f3894Sahrens * could change, and we'll need a lock. 283088f3894Sahrens */ 2843f9d6ad7SLin Ling err = dsl_read_nolock(NULL, spa, os->os_rootbp, 285503ad85cSMatthew Ahrens arc_getbuf_func, &os->os_phys_buf, 28613506d1eSmaybee ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb); 287ea8dc4b6Seschrock if (err) { 288503ad85cSMatthew Ahrens kmem_free(os, sizeof (objset_t)); 289b87f3af3Sperrin /* convert checksum errors into IO errors */ 290b87f3af3Sperrin if (err == ECKSUM) 291b87f3af3Sperrin err = EIO; 292ea8dc4b6Seschrock return (err); 293ea8dc4b6Seschrock } 29414843421SMatthew Ahrens 29514843421SMatthew Ahrens /* Increase the blocksize if we are permitted. */ 29614843421SMatthew Ahrens if (spa_version(spa) >= SPA_VERSION_USERSPACE && 297503ad85cSMatthew Ahrens arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) { 29814843421SMatthew Ahrens arc_buf_t *buf = arc_buf_alloc(spa, 299503ad85cSMatthew Ahrens sizeof (objset_phys_t), &os->os_phys_buf, 30014843421SMatthew Ahrens ARC_BUFC_METADATA); 30114843421SMatthew Ahrens bzero(buf->b_data, sizeof (objset_phys_t)); 302503ad85cSMatthew Ahrens bcopy(os->os_phys_buf->b_data, buf->b_data, 303503ad85cSMatthew Ahrens arc_buf_size(os->os_phys_buf)); 304503ad85cSMatthew Ahrens (void) arc_buf_remove_ref(os->os_phys_buf, 305503ad85cSMatthew Ahrens &os->os_phys_buf); 306503ad85cSMatthew Ahrens os->os_phys_buf = buf; 30714843421SMatthew Ahrens } 30814843421SMatthew Ahrens 309503ad85cSMatthew Ahrens os->os_phys = os->os_phys_buf->b_data; 310503ad85cSMatthew Ahrens os->os_flags = os->os_phys->os_flags; 311fa9e4066Sahrens } else { 31214843421SMatthew Ahrens int size = spa_version(spa) >= SPA_VERSION_USERSPACE ? 31314843421SMatthew Ahrens sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE; 314503ad85cSMatthew Ahrens os->os_phys_buf = arc_buf_alloc(spa, size, 315503ad85cSMatthew Ahrens &os->os_phys_buf, ARC_BUFC_METADATA); 316503ad85cSMatthew Ahrens os->os_phys = os->os_phys_buf->b_data; 317503ad85cSMatthew Ahrens bzero(os->os_phys, size); 318fa9e4066Sahrens } 319fa9e4066Sahrens 320fa9e4066Sahrens /* 321fa9e4066Sahrens * Note: the changed_cb will be called once before the register 322fa9e4066Sahrens * func returns, thus changing the checksum/compression from the 3233baa08fcSek * default (fletcher2/off). Snapshots don't need to know about 3243baa08fcSek * checksum/compression/copies. 325fa9e4066Sahrens */ 3263baa08fcSek if (ds) { 3273baa08fcSek err = dsl_prop_register(ds, "primarycache", 328503ad85cSMatthew Ahrens primary_cache_changed_cb, os); 329d0ad202dSahrens if (err == 0) 3303baa08fcSek err = dsl_prop_register(ds, "secondarycache", 331503ad85cSMatthew Ahrens secondary_cache_changed_cb, os); 3323baa08fcSek if (!dsl_dataset_is_snapshot(ds)) { 3333baa08fcSek if (err == 0) 3343baa08fcSek err = dsl_prop_register(ds, "checksum", 335503ad85cSMatthew Ahrens checksum_changed_cb, os); 3363baa08fcSek if (err == 0) 3373baa08fcSek err = dsl_prop_register(ds, "compression", 338503ad85cSMatthew Ahrens compression_changed_cb, os); 3393baa08fcSek if (err == 0) 3403baa08fcSek err = dsl_prop_register(ds, "copies", 341503ad85cSMatthew Ahrens copies_changed_cb, os); 342b24ab676SJeff Bonwick if (err == 0) 343b24ab676SJeff Bonwick err = dsl_prop_register(ds, "dedup", 344b24ab676SJeff Bonwick dedup_changed_cb, os); 345e09fa4daSNeil Perrin if (err == 0) 346e09fa4daSNeil Perrin err = dsl_prop_register(ds, "logbias", 347e09fa4daSNeil Perrin logbias_changed_cb, os); 34855da60b9SMark J Musante if (err == 0) 34955da60b9SMark J Musante err = dsl_prop_register(ds, "sync", 35055da60b9SMark J Musante sync_changed_cb, os); 3513baa08fcSek } 352ea8dc4b6Seschrock if (err) { 353503ad85cSMatthew Ahrens VERIFY(arc_buf_remove_ref(os->os_phys_buf, 354503ad85cSMatthew Ahrens &os->os_phys_buf) == 1); 355503ad85cSMatthew Ahrens kmem_free(os, sizeof (objset_t)); 356ea8dc4b6Seschrock return (err); 357ea8dc4b6Seschrock } 35899653d4eSeschrock } else if (ds == NULL) { 359fa9e4066Sahrens /* It's the meta-objset. */ 360503ad85cSMatthew Ahrens os->os_checksum = ZIO_CHECKSUM_FLETCHER_4; 361503ad85cSMatthew Ahrens os->os_compress = ZIO_COMPRESS_LZJB; 362503ad85cSMatthew Ahrens os->os_copies = spa_max_replication(spa); 363b24ab676SJeff Bonwick os->os_dedup_checksum = ZIO_CHECKSUM_OFF; 364b24ab676SJeff Bonwick os->os_dedup_verify = 0; 365b24ab676SJeff Bonwick os->os_logbias = 0; 36655da60b9SMark J Musante os->os_sync = 0; 367503ad85cSMatthew Ahrens os->os_primary_cache = ZFS_CACHE_ALL; 368503ad85cSMatthew Ahrens os->os_secondary_cache = ZFS_CACHE_ALL; 369fa9e4066Sahrens } 370fa9e4066Sahrens 3716e0cbcaaSMatthew Ahrens if (ds == NULL || !dsl_dataset_is_snapshot(ds)) 3726e0cbcaaSMatthew Ahrens os->os_zil_header = os->os_phys->os_zil_header; 373503ad85cSMatthew Ahrens os->os_zil = zil_alloc(os, &os->os_zil_header); 374fa9e4066Sahrens 375fa9e4066Sahrens for (i = 0; i < TXG_SIZE; i++) { 376503ad85cSMatthew Ahrens list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t), 377fa9e4066Sahrens offsetof(dnode_t, dn_dirty_link[i])); 378503ad85cSMatthew Ahrens list_create(&os->os_free_dnodes[i], sizeof (dnode_t), 379fa9e4066Sahrens offsetof(dnode_t, dn_dirty_link[i])); 380fa9e4066Sahrens } 381503ad85cSMatthew Ahrens list_create(&os->os_dnodes, sizeof (dnode_t), 382fa9e4066Sahrens offsetof(dnode_t, dn_link)); 383503ad85cSMatthew Ahrens list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t), 384fa9e4066Sahrens offsetof(dmu_buf_impl_t, db_link)); 385fa9e4066Sahrens 386503ad85cSMatthew Ahrens mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL); 387503ad85cSMatthew Ahrens mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL); 388503ad85cSMatthew Ahrens mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL); 389503ad85cSMatthew Ahrens 390744947dcSTom Erickson DMU_META_DNODE(os) = dnode_special_open(os, 391744947dcSTom Erickson &os->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT, 392744947dcSTom Erickson &os->os_meta_dnode); 393503ad85cSMatthew Ahrens if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) { 394744947dcSTom Erickson DMU_USERUSED_DNODE(os) = dnode_special_open(os, 395744947dcSTom Erickson &os->os_phys->os_userused_dnode, DMU_USERUSED_OBJECT, 396744947dcSTom Erickson &os->os_userused_dnode); 397744947dcSTom Erickson DMU_GROUPUSED_DNODE(os) = dnode_special_open(os, 398744947dcSTom Erickson &os->os_phys->os_groupused_dnode, DMU_GROUPUSED_OBJECT, 399744947dcSTom Erickson &os->os_groupused_dnode); 40014843421SMatthew Ahrens } 401fa9e4066Sahrens 40291ebeef5Sahrens /* 40391ebeef5Sahrens * We should be the only thread trying to do this because we 40491ebeef5Sahrens * have ds_opening_lock 40591ebeef5Sahrens */ 40691ebeef5Sahrens if (ds) { 407503ad85cSMatthew Ahrens mutex_enter(&ds->ds_lock); 408503ad85cSMatthew Ahrens ASSERT(ds->ds_objset == NULL); 409503ad85cSMatthew Ahrens ds->ds_objset = os; 410503ad85cSMatthew Ahrens mutex_exit(&ds->ds_lock); 411fa9e4066Sahrens } 412fa9e4066Sahrens 413503ad85cSMatthew Ahrens *osp = os; 414ea8dc4b6Seschrock return (0); 415fa9e4066Sahrens } 416fa9e4066Sahrens 417503ad85cSMatthew Ahrens int 418503ad85cSMatthew Ahrens dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp) 4193cb34c60Sahrens { 420503ad85cSMatthew Ahrens int err = 0; 4213cb34c60Sahrens 4223cb34c60Sahrens mutex_enter(&ds->ds_opening_lock); 423503ad85cSMatthew Ahrens *osp = ds->ds_objset; 424503ad85cSMatthew Ahrens if (*osp == NULL) { 4253cb34c60Sahrens err = dmu_objset_open_impl(dsl_dataset_get_spa(ds), 426feaa74e4SMark Maybee ds, dsl_dataset_get_blkptr(ds), osp); 4273cb34c60Sahrens } 4283cb34c60Sahrens mutex_exit(&ds->ds_opening_lock); 429503ad85cSMatthew Ahrens return (err); 4303cb34c60Sahrens } 4313cb34c60Sahrens 432503ad85cSMatthew Ahrens /* called from zpl */ 4333cb34c60Sahrens int 434503ad85cSMatthew Ahrens dmu_objset_hold(const char *name, void *tag, objset_t **osp) 4353cb34c60Sahrens { 436503ad85cSMatthew Ahrens dsl_dataset_t *ds; 4373cb34c60Sahrens int err; 4383cb34c60Sahrens 439503ad85cSMatthew Ahrens err = dsl_dataset_hold(name, tag, &ds); 4403cb34c60Sahrens if (err) 441503ad85cSMatthew Ahrens return (err); 442503ad85cSMatthew Ahrens 443503ad85cSMatthew Ahrens err = dmu_objset_from_ds(ds, osp); 444503ad85cSMatthew Ahrens if (err) 445503ad85cSMatthew Ahrens dsl_dataset_rele(ds, tag); 446503ad85cSMatthew Ahrens 4473cb34c60Sahrens return (err); 4483cb34c60Sahrens } 4493cb34c60Sahrens 450fa9e4066Sahrens /* called from zpl */ 451fa9e4066Sahrens int 452503ad85cSMatthew Ahrens dmu_objset_own(const char *name, dmu_objset_type_t type, 453503ad85cSMatthew Ahrens boolean_t readonly, void *tag, objset_t **osp) 454fa9e4066Sahrens { 455f18faf3fSek dsl_dataset_t *ds; 456f18faf3fSek int err; 457fa9e4066Sahrens 458503ad85cSMatthew Ahrens err = dsl_dataset_own(name, B_FALSE, tag, &ds); 459503ad85cSMatthew Ahrens if (err) 460fa9e4066Sahrens return (err); 461fa9e4066Sahrens 462503ad85cSMatthew Ahrens err = dmu_objset_from_ds(ds, osp); 4633cb34c60Sahrens if (err) { 464503ad85cSMatthew Ahrens dsl_dataset_disown(ds, tag); 465681d9761SEric Taylor } else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) { 466503ad85cSMatthew Ahrens dmu_objset_disown(*osp, tag); 467503ad85cSMatthew Ahrens return (EINVAL); 468681d9761SEric Taylor } else if (!readonly && dsl_dataset_is_snapshot(ds)) { 469681d9761SEric Taylor dmu_objset_disown(*osp, tag); 470681d9761SEric Taylor return (EROFS); 471fa9e4066Sahrens } 4723cb34c60Sahrens return (err); 473fa9e4066Sahrens } 474fa9e4066Sahrens 475fa9e4066Sahrens void 476503ad85cSMatthew Ahrens dmu_objset_rele(objset_t *os, void *tag) 477fa9e4066Sahrens { 478503ad85cSMatthew Ahrens dsl_dataset_rele(os->os_dsl_dataset, tag); 479503ad85cSMatthew Ahrens } 480503ad85cSMatthew Ahrens 481503ad85cSMatthew Ahrens void 482503ad85cSMatthew Ahrens dmu_objset_disown(objset_t *os, void *tag) 483503ad85cSMatthew Ahrens { 484503ad85cSMatthew Ahrens dsl_dataset_disown(os->os_dsl_dataset, tag); 485fa9e4066Sahrens } 486fa9e4066Sahrens 487436b2950Sperrin int 4881934e92fSmaybee dmu_objset_evict_dbufs(objset_t *os) 489ea8dc4b6Seschrock { 490ea8dc4b6Seschrock dnode_t *dn; 491c543ec06Sahrens 492503ad85cSMatthew Ahrens mutex_enter(&os->os_lock); 493c543ec06Sahrens 494c543ec06Sahrens /* process the mdn last, since the other dnodes have holds on it */ 495744947dcSTom Erickson list_remove(&os->os_dnodes, DMU_META_DNODE(os)); 496744947dcSTom Erickson list_insert_tail(&os->os_dnodes, DMU_META_DNODE(os)); 497ea8dc4b6Seschrock 498ea8dc4b6Seschrock /* 499c543ec06Sahrens * Find the first dnode with holds. We have to do this dance 500c543ec06Sahrens * because dnode_add_ref() only works if you already have a 501c543ec06Sahrens * hold. If there are no holds then it has no dbufs so OK to 502c543ec06Sahrens * skip. 503ea8dc4b6Seschrock */ 504503ad85cSMatthew Ahrens for (dn = list_head(&os->os_dnodes); 5051934e92fSmaybee dn && !dnode_add_ref(dn, FTAG); 506503ad85cSMatthew Ahrens dn = list_next(&os->os_dnodes, dn)) 507c543ec06Sahrens continue; 508c543ec06Sahrens 509c543ec06Sahrens while (dn) { 510c543ec06Sahrens dnode_t *next_dn = dn; 511c543ec06Sahrens 512c543ec06Sahrens do { 513503ad85cSMatthew Ahrens next_dn = list_next(&os->os_dnodes, next_dn); 5141934e92fSmaybee } while (next_dn && !dnode_add_ref(next_dn, FTAG)); 515c543ec06Sahrens 516503ad85cSMatthew Ahrens mutex_exit(&os->os_lock); 5171934e92fSmaybee dnode_evict_dbufs(dn); 518c543ec06Sahrens dnode_rele(dn, FTAG); 519503ad85cSMatthew Ahrens mutex_enter(&os->os_lock); 520c543ec06Sahrens dn = next_dn; 521ea8dc4b6Seschrock } 522744947dcSTom Erickson dn = list_head(&os->os_dnodes); 523503ad85cSMatthew Ahrens mutex_exit(&os->os_lock); 524744947dcSTom Erickson return (dn != DMU_META_DNODE(os)); 525ea8dc4b6Seschrock } 526ea8dc4b6Seschrock 527fa9e4066Sahrens void 528503ad85cSMatthew Ahrens dmu_objset_evict(objset_t *os) 529fa9e4066Sahrens { 530503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 531fa9e4066Sahrens 532b24ab676SJeff Bonwick for (int t = 0; t < TXG_SIZE; t++) 533b24ab676SJeff Bonwick ASSERT(!dmu_objset_is_dirty(os, t)); 534fa9e4066Sahrens 5353baa08fcSek if (ds) { 5363baa08fcSek if (!dsl_dataset_is_snapshot(ds)) { 5373baa08fcSek VERIFY(0 == dsl_prop_unregister(ds, "checksum", 538503ad85cSMatthew Ahrens checksum_changed_cb, os)); 5393baa08fcSek VERIFY(0 == dsl_prop_unregister(ds, "compression", 540503ad85cSMatthew Ahrens compression_changed_cb, os)); 5413baa08fcSek VERIFY(0 == dsl_prop_unregister(ds, "copies", 542503ad85cSMatthew Ahrens copies_changed_cb, os)); 543b24ab676SJeff Bonwick VERIFY(0 == dsl_prop_unregister(ds, "dedup", 544b24ab676SJeff Bonwick dedup_changed_cb, os)); 545e09fa4daSNeil Perrin VERIFY(0 == dsl_prop_unregister(ds, "logbias", 546e09fa4daSNeil Perrin logbias_changed_cb, os)); 54755da60b9SMark J Musante VERIFY(0 == dsl_prop_unregister(ds, "sync", 54855da60b9SMark J Musante sync_changed_cb, os)); 5493baa08fcSek } 5503baa08fcSek VERIFY(0 == dsl_prop_unregister(ds, "primarycache", 551503ad85cSMatthew Ahrens primary_cache_changed_cb, os)); 5523baa08fcSek VERIFY(0 == dsl_prop_unregister(ds, "secondarycache", 553503ad85cSMatthew Ahrens secondary_cache_changed_cb, os)); 554fa9e4066Sahrens } 555fa9e4066Sahrens 5560a586ceaSMark Shellenbaum if (os->os_sa) 5570a586ceaSMark Shellenbaum sa_tear_down(os); 5580a586ceaSMark Shellenbaum 559ea8dc4b6Seschrock /* 560ea8dc4b6Seschrock * We should need only a single pass over the dnode list, since 561ea8dc4b6Seschrock * nothing can be added to the list at this point. 562ea8dc4b6Seschrock */ 563503ad85cSMatthew Ahrens (void) dmu_objset_evict_dbufs(os); 564ea8dc4b6Seschrock 565744947dcSTom Erickson dnode_special_close(&os->os_meta_dnode); 566744947dcSTom Erickson if (DMU_USERUSED_DNODE(os)) { 567744947dcSTom Erickson dnode_special_close(&os->os_userused_dnode); 568744947dcSTom Erickson dnode_special_close(&os->os_groupused_dnode); 56914843421SMatthew Ahrens } 570503ad85cSMatthew Ahrens zil_free(os->os_zil); 571fa9e4066Sahrens 572503ad85cSMatthew Ahrens ASSERT3P(list_head(&os->os_dnodes), ==, NULL); 57314843421SMatthew Ahrens 574503ad85cSMatthew Ahrens VERIFY(arc_buf_remove_ref(os->os_phys_buf, &os->os_phys_buf) == 1); 575744947dcSTom Erickson 576744947dcSTom Erickson /* 577744947dcSTom Erickson * This is a barrier to prevent the objset from going away in 578744947dcSTom Erickson * dnode_move() until we can safely ensure that the objset is still in 579744947dcSTom Erickson * use. We consider the objset valid before the barrier and invalid 580744947dcSTom Erickson * after the barrier. 581744947dcSTom Erickson */ 582744947dcSTom Erickson rw_enter(&os_lock, RW_READER); 583744947dcSTom Erickson rw_exit(&os_lock); 584744947dcSTom Erickson 585503ad85cSMatthew Ahrens mutex_destroy(&os->os_lock); 586503ad85cSMatthew Ahrens mutex_destroy(&os->os_obj_lock); 587503ad85cSMatthew Ahrens mutex_destroy(&os->os_user_ptr_lock); 588503ad85cSMatthew Ahrens kmem_free(os, sizeof (objset_t)); 589fa9e4066Sahrens } 590fa9e4066Sahrens 59171eb0538SChris Kirby timestruc_t 59271eb0538SChris Kirby dmu_objset_snap_cmtime(objset_t *os) 59371eb0538SChris Kirby { 59471eb0538SChris Kirby return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir)); 59571eb0538SChris Kirby } 59671eb0538SChris Kirby 597fa9e4066Sahrens /* called from dsl for meta-objset */ 598503ad85cSMatthew Ahrens objset_t * 599c717a561Smaybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, 600c717a561Smaybee dmu_objset_type_t type, dmu_tx_t *tx) 601fa9e4066Sahrens { 602503ad85cSMatthew Ahrens objset_t *os; 603fa9e4066Sahrens dnode_t *mdn; 604fa9e4066Sahrens 605fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 606feaa74e4SMark Maybee if (ds != NULL) 607feaa74e4SMark Maybee VERIFY(0 == dmu_objset_from_ds(ds, &os)); 608feaa74e4SMark Maybee else 609feaa74e4SMark Maybee VERIFY(0 == dmu_objset_open_impl(spa, NULL, bp, &os)); 610feaa74e4SMark Maybee 611744947dcSTom Erickson mdn = DMU_META_DNODE(os); 612fa9e4066Sahrens 613fa9e4066Sahrens dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT, 614fa9e4066Sahrens DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx); 615fa9e4066Sahrens 616fa9e4066Sahrens /* 617fa9e4066Sahrens * We don't want to have to increase the meta-dnode's nlevels 618fa9e4066Sahrens * later, because then we could do it in quescing context while 619fa9e4066Sahrens * we are also accessing it in open context. 620fa9e4066Sahrens * 621fa9e4066Sahrens * This precaution is not necessary for the MOS (ds == NULL), 622fa9e4066Sahrens * because the MOS is only updated in syncing context. 623fa9e4066Sahrens * This is most fortunate: the MOS is the only objset that 624fa9e4066Sahrens * needs to be synced multiple times as spa_sync() iterates 625fa9e4066Sahrens * to convergence, so minimizing its dn_nlevels matters. 626fa9e4066Sahrens */ 627ea8dc4b6Seschrock if (ds != NULL) { 628ea8dc4b6Seschrock int levels = 1; 629ea8dc4b6Seschrock 630ea8dc4b6Seschrock /* 631ea8dc4b6Seschrock * Determine the number of levels necessary for the meta-dnode 632ea8dc4b6Seschrock * to contain DN_MAX_OBJECT dnodes. 633ea8dc4b6Seschrock */ 634ea8dc4b6Seschrock while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift + 635ea8dc4b6Seschrock (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) < 636ea8dc4b6Seschrock DN_MAX_OBJECT * sizeof (dnode_phys_t)) 637ea8dc4b6Seschrock levels++; 638ea8dc4b6Seschrock 639fa9e4066Sahrens mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] = 640ea8dc4b6Seschrock mdn->dn_nlevels = levels; 641ea8dc4b6Seschrock } 642fa9e4066Sahrens 643fa9e4066Sahrens ASSERT(type != DMU_OST_NONE); 644fa9e4066Sahrens ASSERT(type != DMU_OST_ANY); 645fa9e4066Sahrens ASSERT(type < DMU_OST_NUMTYPES); 646503ad85cSMatthew Ahrens os->os_phys->os_type = type; 647503ad85cSMatthew Ahrens if (dmu_objset_userused_enabled(os)) { 648503ad85cSMatthew Ahrens os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE; 649503ad85cSMatthew Ahrens os->os_flags = os->os_phys->os_flags; 65014843421SMatthew Ahrens } 651fa9e4066Sahrens 652fa9e4066Sahrens dsl_dataset_dirty(ds, tx); 653fa9e4066Sahrens 654503ad85cSMatthew Ahrens return (os); 655fa9e4066Sahrens } 656fa9e4066Sahrens 657fa9e4066Sahrens struct oscarg { 658ecd6cf80Smarks void (*userfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx); 659fa9e4066Sahrens void *userarg; 660ae46e4c7SMatthew Ahrens dsl_dataset_t *clone_origin; 661fa9e4066Sahrens const char *lastname; 662fa9e4066Sahrens dmu_objset_type_t type; 663ab04eb8eStimh uint64_t flags; 6643f9d6ad7SLin Ling cred_t *cr; 665fa9e4066Sahrens }; 666fa9e4066Sahrens 667ecd6cf80Smarks /*ARGSUSED*/ 668fa9e4066Sahrens static int 6691d452cf5Sahrens dmu_objset_create_check(void *arg1, void *arg2, dmu_tx_t *tx) 670fa9e4066Sahrens { 6711d452cf5Sahrens dsl_dir_t *dd = arg1; 6721d452cf5Sahrens struct oscarg *oa = arg2; 6731d452cf5Sahrens objset_t *mos = dd->dd_pool->dp_meta_objset; 674fa9e4066Sahrens int err; 6751d452cf5Sahrens uint64_t ddobj; 6761d452cf5Sahrens 6771d452cf5Sahrens err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj, 6781d452cf5Sahrens oa->lastname, sizeof (uint64_t), 1, &ddobj); 6791d452cf5Sahrens if (err != ENOENT) 6801d452cf5Sahrens return (err ? err : EEXIST); 6811d452cf5Sahrens 682ae46e4c7SMatthew Ahrens if (oa->clone_origin != NULL) { 683ae46e4c7SMatthew Ahrens /* You can't clone across pools. */ 684ae46e4c7SMatthew Ahrens if (oa->clone_origin->ds_dir->dd_pool != dd->dd_pool) 6851d452cf5Sahrens return (EXDEV); 6861d452cf5Sahrens 687ae46e4c7SMatthew Ahrens /* You can only clone snapshots, not the head datasets. */ 688ae46e4c7SMatthew Ahrens if (!dsl_dataset_is_snapshot(oa->clone_origin)) 6891d452cf5Sahrens return (EINVAL); 6901d452cf5Sahrens } 691ecd6cf80Smarks 6921d452cf5Sahrens return (0); 6931d452cf5Sahrens } 6941d452cf5Sahrens 6951d452cf5Sahrens static void 6963f9d6ad7SLin Ling dmu_objset_create_sync(void *arg1, void *arg2, dmu_tx_t *tx) 6971d452cf5Sahrens { 6981d452cf5Sahrens dsl_dir_t *dd = arg1; 699feaa74e4SMark Maybee spa_t *spa = dd->dd_pool->dp_spa; 7001d452cf5Sahrens struct oscarg *oa = arg2; 701feaa74e4SMark Maybee uint64_t obj; 702fa9e4066Sahrens 703fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 704fa9e4066Sahrens 705feaa74e4SMark Maybee obj = dsl_dataset_create_sync(dd, oa->lastname, 7063f9d6ad7SLin Ling oa->clone_origin, oa->flags, oa->cr, tx); 707fa9e4066Sahrens 708ae46e4c7SMatthew Ahrens if (oa->clone_origin == NULL) { 709feaa74e4SMark Maybee dsl_pool_t *dp = dd->dd_pool; 710feaa74e4SMark Maybee dsl_dataset_t *ds; 711ae46e4c7SMatthew Ahrens blkptr_t *bp; 712feaa74e4SMark Maybee objset_t *os; 713fa9e4066Sahrens 714feaa74e4SMark Maybee VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, obj, FTAG, &ds)); 715ae46e4c7SMatthew Ahrens bp = dsl_dataset_get_blkptr(ds); 716ae46e4c7SMatthew Ahrens ASSERT(BP_IS_HOLE(bp)); 717ae46e4c7SMatthew Ahrens 718feaa74e4SMark Maybee os = dmu_objset_create_impl(spa, ds, bp, oa->type, tx); 719fa9e4066Sahrens 720fa9e4066Sahrens if (oa->userfunc) 7213f9d6ad7SLin Ling oa->userfunc(os, oa->userarg, oa->cr, tx); 722feaa74e4SMark Maybee dsl_dataset_rele(ds, FTAG); 723fa9e4066Sahrens } 724ecd6cf80Smarks 725feaa74e4SMark Maybee spa_history_log_internal(LOG_DS_CREATE, spa, tx, "dataset = %llu", obj); 726fa9e4066Sahrens } 727fa9e4066Sahrens 728fa9e4066Sahrens int 729ae46e4c7SMatthew Ahrens dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags, 730ecd6cf80Smarks void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg) 731fa9e4066Sahrens { 7321d452cf5Sahrens dsl_dir_t *pdd; 733fa9e4066Sahrens const char *tail; 734fa9e4066Sahrens int err = 0; 7351d452cf5Sahrens struct oscarg oa = { 0 }; 736fa9e4066Sahrens 7371d452cf5Sahrens ASSERT(strchr(name, '@') == NULL); 7381d452cf5Sahrens err = dsl_dir_open(name, FTAG, &pdd, &tail); 739ea8dc4b6Seschrock if (err) 740ea8dc4b6Seschrock return (err); 741fa9e4066Sahrens if (tail == NULL) { 7421d452cf5Sahrens dsl_dir_close(pdd, FTAG); 743fa9e4066Sahrens return (EEXIST); 744fa9e4066Sahrens } 745fa9e4066Sahrens 7461d452cf5Sahrens oa.userfunc = func; 7471d452cf5Sahrens oa.userarg = arg; 7481d452cf5Sahrens oa.lastname = tail; 7491d452cf5Sahrens oa.type = type; 750ab04eb8eStimh oa.flags = flags; 7513f9d6ad7SLin Ling oa.cr = CRED(); 752ecd6cf80Smarks 753ae46e4c7SMatthew Ahrens err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check, 754ae46e4c7SMatthew Ahrens dmu_objset_create_sync, pdd, &oa, 5); 755ae46e4c7SMatthew Ahrens dsl_dir_close(pdd, FTAG); 756ae46e4c7SMatthew Ahrens return (err); 757ae46e4c7SMatthew Ahrens } 758ae46e4c7SMatthew Ahrens 759ae46e4c7SMatthew Ahrens int 760ae46e4c7SMatthew Ahrens dmu_objset_clone(const char *name, dsl_dataset_t *clone_origin, uint64_t flags) 761ae46e4c7SMatthew Ahrens { 762ae46e4c7SMatthew Ahrens dsl_dir_t *pdd; 763ae46e4c7SMatthew Ahrens const char *tail; 764ae46e4c7SMatthew Ahrens int err = 0; 765ae46e4c7SMatthew Ahrens struct oscarg oa = { 0 }; 766ae46e4c7SMatthew Ahrens 767ae46e4c7SMatthew Ahrens ASSERT(strchr(name, '@') == NULL); 768ae46e4c7SMatthew Ahrens err = dsl_dir_open(name, FTAG, &pdd, &tail); 769ae46e4c7SMatthew Ahrens if (err) 770ae46e4c7SMatthew Ahrens return (err); 771ae46e4c7SMatthew Ahrens if (tail == NULL) { 772ae46e4c7SMatthew Ahrens dsl_dir_close(pdd, FTAG); 773ae46e4c7SMatthew Ahrens return (EEXIST); 774fa9e4066Sahrens } 775ae46e4c7SMatthew Ahrens 776ae46e4c7SMatthew Ahrens oa.lastname = tail; 777ae46e4c7SMatthew Ahrens oa.clone_origin = clone_origin; 778ae46e4c7SMatthew Ahrens oa.flags = flags; 7793f9d6ad7SLin Ling oa.cr = CRED(); 780ae46e4c7SMatthew Ahrens 7811d452cf5Sahrens err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check, 7821d452cf5Sahrens dmu_objset_create_sync, pdd, &oa, 5); 7831d452cf5Sahrens dsl_dir_close(pdd, FTAG); 784fa9e4066Sahrens return (err); 785fa9e4066Sahrens } 786fa9e4066Sahrens 787fa9e4066Sahrens int 788842727c2SChris Kirby dmu_objset_destroy(const char *name, boolean_t defer) 789fa9e4066Sahrens { 790503ad85cSMatthew Ahrens dsl_dataset_t *ds; 791fa9e4066Sahrens int error; 792fa9e4066Sahrens 793503ad85cSMatthew Ahrens error = dsl_dataset_own(name, B_TRUE, FTAG, &ds); 794fa9e4066Sahrens if (error == 0) { 795503ad85cSMatthew Ahrens error = dsl_dataset_destroy(ds, FTAG, defer); 796ae46e4c7SMatthew Ahrens /* dsl_dataset_destroy() closes the ds. */ 797fa9e4066Sahrens } 798fa9e4066Sahrens 7993cb34c60Sahrens return (error); 800fa9e4066Sahrens } 801fa9e4066Sahrens 8021d452cf5Sahrens struct snaparg { 8031d452cf5Sahrens dsl_sync_task_group_t *dstg; 8041d452cf5Sahrens char *snapname; 805*99d5e173STim Haley char *htag; 8061d452cf5Sahrens char failed[MAXPATHLEN]; 807d8d780fbSMatthew Ahrens boolean_t recursive; 8086e0cbcaaSMatthew Ahrens boolean_t needsuspend; 809*99d5e173STim Haley boolean_t temporary; 810ea2f5b9eSMatthew Ahrens nvlist_t *props; 811*99d5e173STim Haley struct dsl_ds_holdarg *ha; /* only needed in the temporary case */ 812*99d5e173STim Haley dsl_dataset_t *newds; 8133cb34c60Sahrens }; 8143cb34c60Sahrens 815ea2f5b9eSMatthew Ahrens static int 816ea2f5b9eSMatthew Ahrens snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx) 817ea2f5b9eSMatthew Ahrens { 818ea2f5b9eSMatthew Ahrens objset_t *os = arg1; 819ea2f5b9eSMatthew Ahrens struct snaparg *sn = arg2; 820*99d5e173STim Haley int error; 821ea2f5b9eSMatthew Ahrens 822ea2f5b9eSMatthew Ahrens /* The props have already been checked by zfs_check_userprops(). */ 823ea2f5b9eSMatthew Ahrens 824*99d5e173STim Haley error = dsl_dataset_snapshot_check(os->os_dsl_dataset, 825*99d5e173STim Haley sn->snapname, tx); 826*99d5e173STim Haley if (error) 827*99d5e173STim Haley return (error); 828*99d5e173STim Haley 829*99d5e173STim Haley if (sn->temporary) { 830*99d5e173STim Haley /* 831*99d5e173STim Haley * Ideally we would just call 832*99d5e173STim Haley * dsl_dataset_user_hold_check() and 833*99d5e173STim Haley * dsl_dataset_destroy_check() here. However the 834*99d5e173STim Haley * dataset we want to hold and destroy is the snapshot 835*99d5e173STim Haley * that we just confirmed we can create, but it won't 836*99d5e173STim Haley * exist until after these checks are run. Do any 837*99d5e173STim Haley * checks we can here and if more checks are added to 838*99d5e173STim Haley * those routines in the future, similar checks may be 839*99d5e173STim Haley * necessary here. 840*99d5e173STim Haley */ 841*99d5e173STim Haley if (spa_version(os->os_spa) < SPA_VERSION_USERREFS) 842*99d5e173STim Haley return (ENOTSUP); 843*99d5e173STim Haley /* 844*99d5e173STim Haley * Not checking number of tags because the tag will be 845*99d5e173STim Haley * unique, as it will be the only tag. 846*99d5e173STim Haley */ 847*99d5e173STim Haley if (strlen(sn->htag) + MAX_TAG_PREFIX_LEN >= MAXNAMELEN) 848*99d5e173STim Haley return (E2BIG); 849*99d5e173STim Haley 850*99d5e173STim Haley sn->ha = kmem_alloc(sizeof (struct dsl_ds_holdarg), KM_SLEEP); 851*99d5e173STim Haley sn->ha->temphold = B_TRUE; 852*99d5e173STim Haley sn->ha->htag = sn->htag; 853*99d5e173STim Haley } 854*99d5e173STim Haley return (error); 855ea2f5b9eSMatthew Ahrens } 856ea2f5b9eSMatthew Ahrens 857ea2f5b9eSMatthew Ahrens static void 8583f9d6ad7SLin Ling snapshot_sync(void *arg1, void *arg2, dmu_tx_t *tx) 859ea2f5b9eSMatthew Ahrens { 860ea2f5b9eSMatthew Ahrens objset_t *os = arg1; 861503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 862ea2f5b9eSMatthew Ahrens struct snaparg *sn = arg2; 863ea2f5b9eSMatthew Ahrens 8643f9d6ad7SLin Ling dsl_dataset_snapshot_sync(ds, sn->snapname, tx); 865ea2f5b9eSMatthew Ahrens 86692241e0bSTom Erickson if (sn->props) { 86792241e0bSTom Erickson dsl_props_arg_t pa; 86892241e0bSTom Erickson pa.pa_props = sn->props; 86992241e0bSTom Erickson pa.pa_source = ZPROP_SRC_LOCAL; 8703f9d6ad7SLin Ling dsl_props_set_sync(ds->ds_prev, &pa, tx); 87192241e0bSTom Erickson } 872*99d5e173STim Haley 873*99d5e173STim Haley if (sn->temporary) { 874*99d5e173STim Haley struct dsl_ds_destroyarg da; 875*99d5e173STim Haley 876*99d5e173STim Haley dsl_dataset_user_hold_sync(ds->ds_prev, sn->ha, tx); 877*99d5e173STim Haley kmem_free(sn->ha, sizeof (struct dsl_ds_holdarg)); 878*99d5e173STim Haley sn->ha = NULL; 879*99d5e173STim Haley sn->newds = ds->ds_prev; 880*99d5e173STim Haley 881*99d5e173STim Haley da.ds = ds->ds_prev; 882*99d5e173STim Haley da.defer = B_TRUE; 883*99d5e173STim Haley dsl_dataset_destroy_sync(&da, FTAG, tx); 884*99d5e173STim Haley } 885ea2f5b9eSMatthew Ahrens } 8861d452cf5Sahrens 8871d452cf5Sahrens static int 888fd136879SMatthew Ahrens dmu_objset_snapshot_one(const char *name, void *arg) 8891d452cf5Sahrens { 8901d452cf5Sahrens struct snaparg *sn = arg; 8911d452cf5Sahrens objset_t *os; 8921d452cf5Sahrens int err; 893d8d780fbSMatthew Ahrens char *cp; 894d8d780fbSMatthew Ahrens 895d8d780fbSMatthew Ahrens /* 896d8d780fbSMatthew Ahrens * If the objset starts with a '%', then ignore it unless it was 897d8d780fbSMatthew Ahrens * explicitly named (ie, not recursive). These hidden datasets 898d8d780fbSMatthew Ahrens * are always inconsistent, and by not opening them here, we can 899d8d780fbSMatthew Ahrens * avoid a race with dsl_dir_destroy_check(). 900d8d780fbSMatthew Ahrens */ 901d8d780fbSMatthew Ahrens cp = strrchr(name, '/'); 902d8d780fbSMatthew Ahrens if (cp && cp[1] == '%' && sn->recursive) 903d8d780fbSMatthew Ahrens return (0); 9041d452cf5Sahrens 9051d452cf5Sahrens (void) strcpy(sn->failed, name); 9061d452cf5Sahrens 907ecd6cf80Smarks /* 908d8d780fbSMatthew Ahrens * Check permissions if we are doing a recursive snapshot. The 909d8d780fbSMatthew Ahrens * permission checks for the starting dataset have already been 910d8d780fbSMatthew Ahrens * performed in zfs_secpolicy_snapshot() 911ecd6cf80Smarks */ 912d8d780fbSMatthew Ahrens if (sn->recursive && (err = zfs_secpolicy_snapshot_perms(name, CRED()))) 913ecd6cf80Smarks return (err); 914ecd6cf80Smarks 915503ad85cSMatthew Ahrens err = dmu_objset_hold(name, sn, &os); 9161d452cf5Sahrens if (err != 0) 9171d452cf5Sahrens return (err); 9181d452cf5Sahrens 919d8d780fbSMatthew Ahrens /* 920d8d780fbSMatthew Ahrens * If the objset is in an inconsistent state (eg, in the process 921d8d780fbSMatthew Ahrens * of being destroyed), don't snapshot it. As with %hidden 922d8d780fbSMatthew Ahrens * datasets, we return EBUSY if this name was explicitly 923d8d780fbSMatthew Ahrens * requested (ie, not recursive), and otherwise ignore it. 924d8d780fbSMatthew Ahrens */ 925503ad85cSMatthew Ahrens if (os->os_dsl_dataset->ds_phys->ds_flags & DS_FLAG_INCONSISTENT) { 926503ad85cSMatthew Ahrens dmu_objset_rele(os, sn); 927d8d780fbSMatthew Ahrens return (sn->recursive ? 0 : EBUSY); 928f2e10be3Srm } 929f2e10be3Srm 9306e0cbcaaSMatthew Ahrens if (sn->needsuspend) { 9316e0cbcaaSMatthew Ahrens err = zil_suspend(dmu_objset_zil(os)); 9326e0cbcaaSMatthew Ahrens if (err) { 9336e0cbcaaSMatthew Ahrens dmu_objset_rele(os, sn); 9346e0cbcaaSMatthew Ahrens return (err); 9356e0cbcaaSMatthew Ahrens } 9361d452cf5Sahrens } 9376e0cbcaaSMatthew Ahrens dsl_sync_task_create(sn->dstg, snapshot_check, snapshot_sync, 9386e0cbcaaSMatthew Ahrens os, sn, 3); 939f2e10be3Srm 9406e0cbcaaSMatthew Ahrens return (0); 9411d452cf5Sahrens } 9421d452cf5Sahrens 9431d452cf5Sahrens int 944*99d5e173STim Haley dmu_objset_snapshot(char *fsname, char *snapname, char *tag, 945*99d5e173STim Haley nvlist_t *props, boolean_t recursive, boolean_t temporary, int cleanup_fd) 9461d452cf5Sahrens { 9471d452cf5Sahrens dsl_sync_task_t *dst; 948ea2f5b9eSMatthew Ahrens struct snaparg sn; 9491d452cf5Sahrens spa_t *spa; 950*99d5e173STim Haley minor_t minor; 9511d452cf5Sahrens int err; 9521d452cf5Sahrens 9531d452cf5Sahrens (void) strcpy(sn.failed, fsname); 9541d452cf5Sahrens 95540feaa91Sahrens err = spa_open(fsname, &spa, FTAG); 9561d452cf5Sahrens if (err) 9571d452cf5Sahrens return (err); 9581d452cf5Sahrens 959*99d5e173STim Haley if (temporary) { 960*99d5e173STim Haley if (cleanup_fd < 0) { 961*99d5e173STim Haley spa_close(spa, FTAG); 962*99d5e173STim Haley return (EINVAL); 963*99d5e173STim Haley } 964*99d5e173STim Haley if ((err = zfs_onexit_fd_hold(cleanup_fd, &minor)) != 0) { 965*99d5e173STim Haley spa_close(spa, FTAG); 966*99d5e173STim Haley return (err); 967*99d5e173STim Haley } 968*99d5e173STim Haley } 969*99d5e173STim Haley 9701d452cf5Sahrens sn.dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 9711d452cf5Sahrens sn.snapname = snapname; 972*99d5e173STim Haley sn.htag = tag; 973ea2f5b9eSMatthew Ahrens sn.props = props; 974d8d780fbSMatthew Ahrens sn.recursive = recursive; 9756e0cbcaaSMatthew Ahrens sn.needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP); 976*99d5e173STim Haley sn.temporary = temporary; 977*99d5e173STim Haley sn.ha = NULL; 978*99d5e173STim Haley sn.newds = NULL; 9791d452cf5Sahrens 9800b69c2f0Sahrens if (recursive) { 9810b69c2f0Sahrens err = dmu_objset_find(fsname, 9820b69c2f0Sahrens dmu_objset_snapshot_one, &sn, DS_FIND_CHILDREN); 9830b69c2f0Sahrens } else { 9841d452cf5Sahrens err = dmu_objset_snapshot_one(fsname, &sn); 9850b69c2f0Sahrens } 9861d452cf5Sahrens 987ea2f5b9eSMatthew Ahrens if (err == 0) 988ea2f5b9eSMatthew Ahrens err = dsl_sync_task_group_wait(sn.dstg); 9891d452cf5Sahrens 9901d452cf5Sahrens for (dst = list_head(&sn.dstg->dstg_tasks); dst; 9911d452cf5Sahrens dst = list_next(&sn.dstg->dstg_tasks, dst)) { 992ea2f5b9eSMatthew Ahrens objset_t *os = dst->dst_arg1; 993503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 994*99d5e173STim Haley if (dst->dst_err) { 9953cb34c60Sahrens dsl_dataset_name(ds, sn.failed); 996*99d5e173STim Haley } else if (temporary) { 997*99d5e173STim Haley dsl_register_onexit_hold_cleanup(sn.newds, tag, minor); 998*99d5e173STim Haley } 9996e0cbcaaSMatthew Ahrens if (sn.needsuspend) 10006e0cbcaaSMatthew Ahrens zil_resume(dmu_objset_zil(os)); 1001503ad85cSMatthew Ahrens dmu_objset_rele(os, &sn); 10023cb34c60Sahrens } 10033cb34c60Sahrens 10041d452cf5Sahrens if (err) 10051d452cf5Sahrens (void) strcpy(fsname, sn.failed); 1006*99d5e173STim Haley if (temporary) 1007*99d5e173STim Haley zfs_onexit_fd_rele(cleanup_fd); 10081d452cf5Sahrens dsl_sync_task_group_destroy(sn.dstg); 10091d452cf5Sahrens spa_close(spa, FTAG); 10101d452cf5Sahrens return (err); 10111d452cf5Sahrens } 10121d452cf5Sahrens 1013fa9e4066Sahrens static void 101414843421SMatthew Ahrens dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx) 1015fa9e4066Sahrens { 1016c717a561Smaybee dnode_t *dn; 1017faafa6e3Sahrens 1018c717a561Smaybee while (dn = list_head(list)) { 1019c717a561Smaybee ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT); 1020c717a561Smaybee ASSERT(dn->dn_dbuf->db_data_pending); 1021c717a561Smaybee /* 102214843421SMatthew Ahrens * Initialize dn_zio outside dnode_sync() because the 102314843421SMatthew Ahrens * meta-dnode needs to set it ouside dnode_sync(). 1024c717a561Smaybee */ 1025c717a561Smaybee dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio; 1026c717a561Smaybee ASSERT(dn->dn_zio); 1027faafa6e3Sahrens 1028c717a561Smaybee ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS); 1029c717a561Smaybee list_remove(list, dn); 103014843421SMatthew Ahrens 103114843421SMatthew Ahrens if (newlist) { 103214843421SMatthew Ahrens (void) dnode_add_ref(dn, newlist); 103314843421SMatthew Ahrens list_insert_tail(newlist, dn); 103414843421SMatthew Ahrens } 103514843421SMatthew Ahrens 1036c717a561Smaybee dnode_sync(dn, tx); 1037fa9e4066Sahrens } 1038fa9e4066Sahrens } 1039fa9e4066Sahrens 1040fa9e4066Sahrens /* ARGSUSED */ 1041fa9e4066Sahrens static void 1042b24ab676SJeff Bonwick dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg) 1043fa9e4066Sahrens { 1044e14bb325SJeff Bonwick blkptr_t *bp = zio->io_bp; 1045503ad85cSMatthew Ahrens objset_t *os = arg; 1046c717a561Smaybee dnode_phys_t *dnp = &os->os_phys->os_meta_dnode; 1047fa9e4066Sahrens 1048e14bb325SJeff Bonwick ASSERT(bp == os->os_rootbp); 1049e14bb325SJeff Bonwick ASSERT(BP_GET_TYPE(bp) == DMU_OT_OBJSET); 1050e14bb325SJeff Bonwick ASSERT(BP_GET_LEVEL(bp) == 0); 10510a4e9518Sgw 1052fa9e4066Sahrens /* 105314843421SMatthew Ahrens * Update rootbp fill count: it should be the number of objects 105414843421SMatthew Ahrens * allocated in the object set (not counting the "special" 105514843421SMatthew Ahrens * objects that are stored in the objset_phys_t -- the meta 105614843421SMatthew Ahrens * dnode and user/group accounting objects). 1057fa9e4066Sahrens */ 105814843421SMatthew Ahrens bp->blk_fill = 0; 1059e14bb325SJeff Bonwick for (int i = 0; i < dnp->dn_nblkptr; i++) 1060c717a561Smaybee bp->blk_fill += dnp->dn_blkptr[i].blk_fill; 1061b24ab676SJeff Bonwick } 1062b24ab676SJeff Bonwick 1063b24ab676SJeff Bonwick /* ARGSUSED */ 1064b24ab676SJeff Bonwick static void 1065b24ab676SJeff Bonwick dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg) 1066b24ab676SJeff Bonwick { 1067b24ab676SJeff Bonwick blkptr_t *bp = zio->io_bp; 1068b24ab676SJeff Bonwick blkptr_t *bp_orig = &zio->io_bp_orig; 1069b24ab676SJeff Bonwick objset_t *os = arg; 10700a4e9518Sgw 1071e14bb325SJeff Bonwick if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { 1072b24ab676SJeff Bonwick ASSERT(BP_EQUAL(bp, bp_orig)); 1073e14bb325SJeff Bonwick } else { 1074b24ab676SJeff Bonwick dsl_dataset_t *ds = os->os_dsl_dataset; 1075b24ab676SJeff Bonwick dmu_tx_t *tx = os->os_synctx; 1076b24ab676SJeff Bonwick 1077b24ab676SJeff Bonwick (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE); 1078b24ab676SJeff Bonwick dsl_dataset_block_born(ds, bp, tx); 10790a4e9518Sgw } 1080c717a561Smaybee } 1081c717a561Smaybee 1082fa9e4066Sahrens /* called from dsl */ 1083fa9e4066Sahrens void 1084503ad85cSMatthew Ahrens dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx) 1085fa9e4066Sahrens { 1086fa9e4066Sahrens int txgoff; 1087ea8dc4b6Seschrock zbookmark_t zb; 1088b24ab676SJeff Bonwick zio_prop_t zp; 1089c717a561Smaybee zio_t *zio; 1090c717a561Smaybee list_t *list; 109114843421SMatthew Ahrens list_t *newlist = NULL; 1092c717a561Smaybee dbuf_dirty_record_t *dr; 1093c717a561Smaybee 1094c717a561Smaybee dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg); 1095fa9e4066Sahrens 1096fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 1097fa9e4066Sahrens /* XXX the write_done callback should really give us the tx... */ 1098fa9e4066Sahrens os->os_synctx = tx; 1099fa9e4066Sahrens 110087bd5c1eSahrens if (os->os_dsl_dataset == NULL) { 110187bd5c1eSahrens /* 110287bd5c1eSahrens * This is the MOS. If we have upgraded, 110387bd5c1eSahrens * spa_max_replication() could change, so reset 110487bd5c1eSahrens * os_copies here. 110587bd5c1eSahrens */ 110687bd5c1eSahrens os->os_copies = spa_max_replication(os->os_spa); 110787bd5c1eSahrens } 110887bd5c1eSahrens 1109fa9e4066Sahrens /* 1110c717a561Smaybee * Create the root block IO 1111fa9e4066Sahrens */ 1112b24ab676SJeff Bonwick SET_BOOKMARK(&zb, os->os_dsl_dataset ? 1113b24ab676SJeff Bonwick os->os_dsl_dataset->ds_object : DMU_META_OBJSET, 1114b24ab676SJeff Bonwick ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 11153f9d6ad7SLin Ling VERIFY3U(0, ==, arc_release_bp(os->os_phys_buf, &os->os_phys_buf, 11163f9d6ad7SLin Ling os->os_rootbp, os->os_spa, &zb)); 1117b24ab676SJeff Bonwick 1118b24ab676SJeff Bonwick dmu_write_policy(os, NULL, 0, 0, &zp); 1119b24ab676SJeff Bonwick 1120b24ab676SJeff Bonwick zio = arc_write(pio, os->os_spa, tx->tx_txg, 1121b24ab676SJeff Bonwick os->os_rootbp, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os), &zp, 1122b24ab676SJeff Bonwick dmu_objset_write_ready, dmu_objset_write_done, os, 1123e14bb325SJeff Bonwick ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb); 1124c717a561Smaybee 1125c717a561Smaybee /* 112614843421SMatthew Ahrens * Sync special dnodes - the parent IO for the sync is the root block 1127c717a561Smaybee */ 1128744947dcSTom Erickson DMU_META_DNODE(os)->dn_zio = zio; 1129744947dcSTom Erickson dnode_sync(DMU_META_DNODE(os), tx); 1130c717a561Smaybee 113114843421SMatthew Ahrens os->os_phys->os_flags = os->os_flags; 113214843421SMatthew Ahrens 1133744947dcSTom Erickson if (DMU_USERUSED_DNODE(os) && 1134744947dcSTom Erickson DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) { 1135744947dcSTom Erickson DMU_USERUSED_DNODE(os)->dn_zio = zio; 1136744947dcSTom Erickson dnode_sync(DMU_USERUSED_DNODE(os), tx); 1137744947dcSTom Erickson DMU_GROUPUSED_DNODE(os)->dn_zio = zio; 1138744947dcSTom Erickson dnode_sync(DMU_GROUPUSED_DNODE(os), tx); 113914843421SMatthew Ahrens } 114014843421SMatthew Ahrens 1141c717a561Smaybee txgoff = tx->tx_txg & TXG_MASK; 1142fa9e4066Sahrens 114314843421SMatthew Ahrens if (dmu_objset_userused_enabled(os)) { 114414843421SMatthew Ahrens newlist = &os->os_synced_dnodes; 114514843421SMatthew Ahrens /* 114614843421SMatthew Ahrens * We must create the list here because it uses the 114714843421SMatthew Ahrens * dn_dirty_link[] of this txg. 114814843421SMatthew Ahrens */ 114914843421SMatthew Ahrens list_create(newlist, sizeof (dnode_t), 115014843421SMatthew Ahrens offsetof(dnode_t, dn_dirty_link[txgoff])); 115114843421SMatthew Ahrens } 115214843421SMatthew Ahrens 115314843421SMatthew Ahrens dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx); 115414843421SMatthew Ahrens dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx); 1155fa9e4066Sahrens 1156744947dcSTom Erickson list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff]; 1157c717a561Smaybee while (dr = list_head(list)) { 1158c717a561Smaybee ASSERT(dr->dr_dbuf->db_level == 0); 1159c717a561Smaybee list_remove(list, dr); 1160c717a561Smaybee if (dr->dr_zio) 1161c717a561Smaybee zio_nowait(dr->dr_zio); 1162c717a561Smaybee } 1163c717a561Smaybee /* 1164c717a561Smaybee * Free intent log blocks up to this tx. 1165c717a561Smaybee */ 1166c717a561Smaybee zil_sync(os->os_zil, tx); 1167088f3894Sahrens os->os_phys->os_zil_header = os->os_zil_header; 1168c717a561Smaybee zio_nowait(zio); 1169fa9e4066Sahrens } 1170fa9e4066Sahrens 1171b24ab676SJeff Bonwick boolean_t 1172b24ab676SJeff Bonwick dmu_objset_is_dirty(objset_t *os, uint64_t txg) 1173b24ab676SJeff Bonwick { 1174b24ab676SJeff Bonwick return (!list_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]) || 1175b24ab676SJeff Bonwick !list_is_empty(&os->os_free_dnodes[txg & TXG_MASK])); 1176b24ab676SJeff Bonwick } 1177b24ab676SJeff Bonwick 11785aaeed5cSSanjeev Bagewadi boolean_t 11795aaeed5cSSanjeev Bagewadi dmu_objset_is_dirty_anywhere(objset_t *os) 11805aaeed5cSSanjeev Bagewadi { 11815aaeed5cSSanjeev Bagewadi for (int t = 0; t < TXG_SIZE; t++) 11825aaeed5cSSanjeev Bagewadi if (dmu_objset_is_dirty(os, t)) 11835aaeed5cSSanjeev Bagewadi return (B_TRUE); 11845aaeed5cSSanjeev Bagewadi return (B_FALSE); 11855aaeed5cSSanjeev Bagewadi } 11865aaeed5cSSanjeev Bagewadi 1187744947dcSTom Erickson static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES]; 118814843421SMatthew Ahrens 118914843421SMatthew Ahrens void 119014843421SMatthew Ahrens dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb) 119114843421SMatthew Ahrens { 119214843421SMatthew Ahrens used_cbs[ost] = cb; 119314843421SMatthew Ahrens } 119414843421SMatthew Ahrens 119514843421SMatthew Ahrens boolean_t 1196503ad85cSMatthew Ahrens dmu_objset_userused_enabled(objset_t *os) 119714843421SMatthew Ahrens { 119814843421SMatthew Ahrens return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE && 1199744947dcSTom Erickson used_cbs[os->os_phys->os_type] != NULL && 1200744947dcSTom Erickson DMU_USERUSED_DNODE(os) != NULL); 120114843421SMatthew Ahrens } 120214843421SMatthew Ahrens 12039966ca11SMatthew Ahrens static void 12040a586ceaSMark Shellenbaum do_userquota_update(objset_t *os, uint64_t used, uint64_t flags, 12050a586ceaSMark Shellenbaum uint64_t user, uint64_t group, boolean_t subtract, dmu_tx_t *tx) 12069966ca11SMatthew Ahrens { 12070a586ceaSMark Shellenbaum if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) { 12080a586ceaSMark Shellenbaum int64_t delta = DNODE_SIZE + used; 12099966ca11SMatthew Ahrens if (subtract) 12109966ca11SMatthew Ahrens delta = -delta; 1211c33e334fSMatthew Ahrens VERIFY3U(0, ==, zap_increment_int(os, DMU_USERUSED_OBJECT, 12129966ca11SMatthew Ahrens user, delta, tx)); 1213c33e334fSMatthew Ahrens VERIFY3U(0, ==, zap_increment_int(os, DMU_GROUPUSED_OBJECT, 12149966ca11SMatthew Ahrens group, delta, tx)); 12159966ca11SMatthew Ahrens } 12169966ca11SMatthew Ahrens } 12179966ca11SMatthew Ahrens 121814843421SMatthew Ahrens void 12190a586ceaSMark Shellenbaum dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx) 122014843421SMatthew Ahrens { 122114843421SMatthew Ahrens dnode_t *dn; 122214843421SMatthew Ahrens list_t *list = &os->os_synced_dnodes; 122314843421SMatthew Ahrens 122414843421SMatthew Ahrens ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os)); 122514843421SMatthew Ahrens 122614843421SMatthew Ahrens while (dn = list_head(list)) { 12271d8ccc7bSMark Shellenbaum int flags; 122814843421SMatthew Ahrens ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object)); 122914843421SMatthew Ahrens ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE || 123014843421SMatthew Ahrens dn->dn_phys->dn_flags & 123114843421SMatthew Ahrens DNODE_FLAG_USERUSED_ACCOUNTED); 123214843421SMatthew Ahrens 123314843421SMatthew Ahrens /* Allocate the user/groupused objects if necessary. */ 1234744947dcSTom Erickson if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) { 1235503ad85cSMatthew Ahrens VERIFY(0 == zap_create_claim(os, 123614843421SMatthew Ahrens DMU_USERUSED_OBJECT, 123714843421SMatthew Ahrens DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx)); 1238503ad85cSMatthew Ahrens VERIFY(0 == zap_create_claim(os, 123914843421SMatthew Ahrens DMU_GROUPUSED_OBJECT, 124014843421SMatthew Ahrens DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx)); 124114843421SMatthew Ahrens } 124214843421SMatthew Ahrens 124314843421SMatthew Ahrens /* 12449966ca11SMatthew Ahrens * We intentionally modify the zap object even if the 12450a586ceaSMark Shellenbaum * net delta is zero. Otherwise 12469966ca11SMatthew Ahrens * the block of the zap obj could be shared between 12479966ca11SMatthew Ahrens * datasets but need to be different between them after 12489966ca11SMatthew Ahrens * a bprewrite. 124914843421SMatthew Ahrens */ 125014843421SMatthew Ahrens 12511d8ccc7bSMark Shellenbaum flags = dn->dn_id_flags; 12521d8ccc7bSMark Shellenbaum ASSERT(flags); 12531d8ccc7bSMark Shellenbaum if (flags & DN_ID_OLD_EXIST) { 12540a586ceaSMark Shellenbaum do_userquota_update(os, dn->dn_oldused, dn->dn_oldflags, 12550a586ceaSMark Shellenbaum dn->dn_olduid, dn->dn_oldgid, B_TRUE, tx); 12560a586ceaSMark Shellenbaum } 12571d8ccc7bSMark Shellenbaum if (flags & DN_ID_NEW_EXIST) { 12580a586ceaSMark Shellenbaum do_userquota_update(os, DN_USED_BYTES(dn->dn_phys), 12590a586ceaSMark Shellenbaum dn->dn_phys->dn_flags, dn->dn_newuid, 12600a586ceaSMark Shellenbaum dn->dn_newgid, B_FALSE, tx); 12610a586ceaSMark Shellenbaum } 12620a586ceaSMark Shellenbaum 12631d8ccc7bSMark Shellenbaum mutex_enter(&dn->dn_mtx); 12640a586ceaSMark Shellenbaum dn->dn_oldused = 0; 12650a586ceaSMark Shellenbaum dn->dn_oldflags = 0; 12660a586ceaSMark Shellenbaum if (dn->dn_id_flags & DN_ID_NEW_EXIST) { 12670a586ceaSMark Shellenbaum dn->dn_olduid = dn->dn_newuid; 12680a586ceaSMark Shellenbaum dn->dn_oldgid = dn->dn_newgid; 12690a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_OLD_EXIST; 12700a586ceaSMark Shellenbaum if (dn->dn_bonuslen == 0) 12710a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_SPILL; 12720a586ceaSMark Shellenbaum else 12730a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_BONUS; 12740a586ceaSMark Shellenbaum } 127528d97a71SMark Shellenbaum dn->dn_id_flags &= ~(DN_ID_NEW_EXIST); 127614843421SMatthew Ahrens mutex_exit(&dn->dn_mtx); 127714843421SMatthew Ahrens 127814843421SMatthew Ahrens list_remove(list, dn); 127914843421SMatthew Ahrens dnode_rele(dn, list); 128014843421SMatthew Ahrens } 128114843421SMatthew Ahrens } 128214843421SMatthew Ahrens 128306e0070dSMark Shellenbaum /* 128406e0070dSMark Shellenbaum * Returns a pointer to data to find uid/gid from 128506e0070dSMark Shellenbaum * 128606e0070dSMark Shellenbaum * If a dirty record for transaction group that is syncing can't 128706e0070dSMark Shellenbaum * be found then NULL is returned. In the NULL case it is assumed 128806e0070dSMark Shellenbaum * the uid/gid aren't changing. 128906e0070dSMark Shellenbaum */ 129006e0070dSMark Shellenbaum static void * 129106e0070dSMark Shellenbaum dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx) 129206e0070dSMark Shellenbaum { 129306e0070dSMark Shellenbaum dbuf_dirty_record_t *dr, **drp; 129406e0070dSMark Shellenbaum void *data; 129506e0070dSMark Shellenbaum 129606e0070dSMark Shellenbaum if (db->db_dirtycnt == 0) 129706e0070dSMark Shellenbaum return (db->db.db_data); /* Nothing is changing */ 129806e0070dSMark Shellenbaum 129906e0070dSMark Shellenbaum for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next) 130006e0070dSMark Shellenbaum if (dr->dr_txg == tx->tx_txg) 130106e0070dSMark Shellenbaum break; 130206e0070dSMark Shellenbaum 1303744947dcSTom Erickson if (dr == NULL) { 130406e0070dSMark Shellenbaum data = NULL; 1305744947dcSTom Erickson } else { 1306744947dcSTom Erickson dnode_t *dn; 1307744947dcSTom Erickson 1308744947dcSTom Erickson DB_DNODE_ENTER(dr->dr_dbuf); 1309744947dcSTom Erickson dn = DB_DNODE(dr->dr_dbuf); 1310744947dcSTom Erickson 1311744947dcSTom Erickson if (dn->dn_bonuslen == 0 && 1312744947dcSTom Erickson dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID) 1313744947dcSTom Erickson data = dr->dt.dl.dr_data->b_data; 1314744947dcSTom Erickson else 1315744947dcSTom Erickson data = dr->dt.dl.dr_data; 1316744947dcSTom Erickson 1317744947dcSTom Erickson DB_DNODE_EXIT(dr->dr_dbuf); 1318744947dcSTom Erickson } 1319744947dcSTom Erickson 132006e0070dSMark Shellenbaum return (data); 132106e0070dSMark Shellenbaum } 132206e0070dSMark Shellenbaum 13230a586ceaSMark Shellenbaum void 132406e0070dSMark Shellenbaum dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx) 13250a586ceaSMark Shellenbaum { 13260a586ceaSMark Shellenbaum objset_t *os = dn->dn_objset; 13270a586ceaSMark Shellenbaum void *data = NULL; 132806e0070dSMark Shellenbaum dmu_buf_impl_t *db = NULL; 13290a586ceaSMark Shellenbaum uint64_t *user, *group; 13300a586ceaSMark Shellenbaum int flags = dn->dn_id_flags; 13310a586ceaSMark Shellenbaum int error; 133206e0070dSMark Shellenbaum boolean_t have_spill = B_FALSE; 13330a586ceaSMark Shellenbaum 13340a586ceaSMark Shellenbaum if (!dmu_objset_userused_enabled(dn->dn_objset)) 13350a586ceaSMark Shellenbaum return; 13360a586ceaSMark Shellenbaum 13370a586ceaSMark Shellenbaum if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST| 13380a586ceaSMark Shellenbaum DN_ID_CHKED_SPILL))) 13390a586ceaSMark Shellenbaum return; 13400a586ceaSMark Shellenbaum 13410a586ceaSMark Shellenbaum if (before && dn->dn_bonuslen != 0) 13420a586ceaSMark Shellenbaum data = DN_BONUS(dn->dn_phys); 134306e0070dSMark Shellenbaum else if (!before && dn->dn_bonuslen != 0) { 134406e0070dSMark Shellenbaum if (dn->dn_bonus) { 134506e0070dSMark Shellenbaum db = dn->dn_bonus; 134606e0070dSMark Shellenbaum mutex_enter(&db->db_mtx); 134706e0070dSMark Shellenbaum data = dmu_objset_userquota_find_data(db, tx); 134806e0070dSMark Shellenbaum } else { 134906e0070dSMark Shellenbaum data = DN_BONUS(dn->dn_phys); 135006e0070dSMark Shellenbaum } 135106e0070dSMark Shellenbaum } else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) { 13520a586ceaSMark Shellenbaum int rf = 0; 13530a586ceaSMark Shellenbaum 13540a586ceaSMark Shellenbaum if (RW_WRITE_HELD(&dn->dn_struct_rwlock)) 13550a586ceaSMark Shellenbaum rf |= DB_RF_HAVESTRUCT; 13561d8ccc7bSMark Shellenbaum error = dmu_spill_hold_by_dnode(dn, 13571d8ccc7bSMark Shellenbaum rf | DB_RF_MUST_SUCCEED, 135806e0070dSMark Shellenbaum FTAG, (dmu_buf_t **)&db); 13590a586ceaSMark Shellenbaum ASSERT(error == 0); 136006e0070dSMark Shellenbaum mutex_enter(&db->db_mtx); 136106e0070dSMark Shellenbaum data = (before) ? db->db.db_data : 136206e0070dSMark Shellenbaum dmu_objset_userquota_find_data(db, tx); 136306e0070dSMark Shellenbaum have_spill = B_TRUE; 13640a586ceaSMark Shellenbaum } else { 13650a586ceaSMark Shellenbaum mutex_enter(&dn->dn_mtx); 13660a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_BONUS; 13670a586ceaSMark Shellenbaum mutex_exit(&dn->dn_mtx); 13680a586ceaSMark Shellenbaum return; 13690a586ceaSMark Shellenbaum } 13700a586ceaSMark Shellenbaum 13710a586ceaSMark Shellenbaum if (before) { 137206e0070dSMark Shellenbaum ASSERT(data); 13730a586ceaSMark Shellenbaum user = &dn->dn_olduid; 13740a586ceaSMark Shellenbaum group = &dn->dn_oldgid; 137506e0070dSMark Shellenbaum } else if (data) { 13760a586ceaSMark Shellenbaum user = &dn->dn_newuid; 13770a586ceaSMark Shellenbaum group = &dn->dn_newgid; 13780a586ceaSMark Shellenbaum } 13790a586ceaSMark Shellenbaum 138006e0070dSMark Shellenbaum /* 138106e0070dSMark Shellenbaum * Must always call the callback in case the object 138206e0070dSMark Shellenbaum * type has changed and that type isn't an object type to track 138306e0070dSMark Shellenbaum */ 13840a586ceaSMark Shellenbaum error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data, 13850a586ceaSMark Shellenbaum user, group); 13860a586ceaSMark Shellenbaum 138706e0070dSMark Shellenbaum /* 138806e0070dSMark Shellenbaum * Preserve existing uid/gid when the callback can't determine 138906e0070dSMark Shellenbaum * what the new uid/gid are and the callback returned EEXIST. 139006e0070dSMark Shellenbaum * The EEXIST error tells us to just use the existing uid/gid. 139106e0070dSMark Shellenbaum * If we don't know what the old values are then just assign 139206e0070dSMark Shellenbaum * them to 0, since that is a new file being created. 139306e0070dSMark Shellenbaum */ 139406e0070dSMark Shellenbaum if (!before && data == NULL && error == EEXIST) { 139506e0070dSMark Shellenbaum if (flags & DN_ID_OLD_EXIST) { 139606e0070dSMark Shellenbaum dn->dn_newuid = dn->dn_olduid; 139706e0070dSMark Shellenbaum dn->dn_newgid = dn->dn_oldgid; 139806e0070dSMark Shellenbaum } else { 139906e0070dSMark Shellenbaum dn->dn_newuid = 0; 140006e0070dSMark Shellenbaum dn->dn_newgid = 0; 140106e0070dSMark Shellenbaum } 140206e0070dSMark Shellenbaum error = 0; 140306e0070dSMark Shellenbaum } 140406e0070dSMark Shellenbaum 140506e0070dSMark Shellenbaum if (db) 140606e0070dSMark Shellenbaum mutex_exit(&db->db_mtx); 140706e0070dSMark Shellenbaum 14080a586ceaSMark Shellenbaum mutex_enter(&dn->dn_mtx); 14090a586ceaSMark Shellenbaum if (error == 0 && before) 14100a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_OLD_EXIST; 14110a586ceaSMark Shellenbaum if (error == 0 && !before) 14120a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_NEW_EXIST; 14130a586ceaSMark Shellenbaum 141406e0070dSMark Shellenbaum if (have_spill) { 14150a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_SPILL; 14160a586ceaSMark Shellenbaum } else { 14170a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_BONUS; 14180a586ceaSMark Shellenbaum } 14190a586ceaSMark Shellenbaum mutex_exit(&dn->dn_mtx); 142006e0070dSMark Shellenbaum if (have_spill) 142106e0070dSMark Shellenbaum dmu_buf_rele((dmu_buf_t *)db, FTAG); 14220a586ceaSMark Shellenbaum } 14230a586ceaSMark Shellenbaum 142414843421SMatthew Ahrens boolean_t 142514843421SMatthew Ahrens dmu_objset_userspace_present(objset_t *os) 142614843421SMatthew Ahrens { 1427503ad85cSMatthew Ahrens return (os->os_phys->os_flags & 142814843421SMatthew Ahrens OBJSET_FLAG_USERACCOUNTING_COMPLETE); 142914843421SMatthew Ahrens } 143014843421SMatthew Ahrens 143114843421SMatthew Ahrens int 143214843421SMatthew Ahrens dmu_objset_userspace_upgrade(objset_t *os) 143314843421SMatthew Ahrens { 143414843421SMatthew Ahrens uint64_t obj; 143514843421SMatthew Ahrens int err = 0; 143614843421SMatthew Ahrens 143714843421SMatthew Ahrens if (dmu_objset_userspace_present(os)) 143814843421SMatthew Ahrens return (0); 1439503ad85cSMatthew Ahrens if (!dmu_objset_userused_enabled(os)) 144014843421SMatthew Ahrens return (ENOTSUP); 144114843421SMatthew Ahrens if (dmu_objset_is_snapshot(os)) 144214843421SMatthew Ahrens return (EINVAL); 144314843421SMatthew Ahrens 144414843421SMatthew Ahrens /* 144514843421SMatthew Ahrens * We simply need to mark every object dirty, so that it will be 144614843421SMatthew Ahrens * synced out and now accounted. If this is called 144714843421SMatthew Ahrens * concurrently, or if we already did some work before crashing, 144814843421SMatthew Ahrens * that's fine, since we track each object's accounted state 144914843421SMatthew Ahrens * independently. 145014843421SMatthew Ahrens */ 145114843421SMatthew Ahrens 145214843421SMatthew Ahrens for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) { 145368857716SLin Ling dmu_tx_t *tx; 145414843421SMatthew Ahrens dmu_buf_t *db; 145514843421SMatthew Ahrens int objerr; 145614843421SMatthew Ahrens 145714843421SMatthew Ahrens if (issig(JUSTLOOKING) && issig(FORREAL)) 145814843421SMatthew Ahrens return (EINTR); 145914843421SMatthew Ahrens 146014843421SMatthew Ahrens objerr = dmu_bonus_hold(os, obj, FTAG, &db); 146114843421SMatthew Ahrens if (objerr) 146214843421SMatthew Ahrens continue; 146368857716SLin Ling tx = dmu_tx_create(os); 146414843421SMatthew Ahrens dmu_tx_hold_bonus(tx, obj); 146514843421SMatthew Ahrens objerr = dmu_tx_assign(tx, TXG_WAIT); 146614843421SMatthew Ahrens if (objerr) { 146714843421SMatthew Ahrens dmu_tx_abort(tx); 146814843421SMatthew Ahrens continue; 146914843421SMatthew Ahrens } 147014843421SMatthew Ahrens dmu_buf_will_dirty(db, tx); 147114843421SMatthew Ahrens dmu_buf_rele(db, FTAG); 147214843421SMatthew Ahrens dmu_tx_commit(tx); 147314843421SMatthew Ahrens } 147414843421SMatthew Ahrens 1475503ad85cSMatthew Ahrens os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE; 147614843421SMatthew Ahrens txg_wait_synced(dmu_objset_pool(os), 0); 147714843421SMatthew Ahrens return (0); 147814843421SMatthew Ahrens } 147914843421SMatthew Ahrens 1480fa9e4066Sahrens void 1481a2eea2e1Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp, 1482a2eea2e1Sahrens uint64_t *usedobjsp, uint64_t *availobjsp) 1483fa9e4066Sahrens { 1484503ad85cSMatthew Ahrens dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp, 1485a2eea2e1Sahrens usedobjsp, availobjsp); 1486a2eea2e1Sahrens } 1487a2eea2e1Sahrens 1488a2eea2e1Sahrens uint64_t 1489a2eea2e1Sahrens dmu_objset_fsid_guid(objset_t *os) 1490a2eea2e1Sahrens { 1491503ad85cSMatthew Ahrens return (dsl_dataset_fsid_guid(os->os_dsl_dataset)); 1492a2eea2e1Sahrens } 1493a2eea2e1Sahrens 1494a2eea2e1Sahrens void 1495a2eea2e1Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat) 1496a2eea2e1Sahrens { 1497503ad85cSMatthew Ahrens stat->dds_type = os->os_phys->os_type; 1498503ad85cSMatthew Ahrens if (os->os_dsl_dataset) 1499503ad85cSMatthew Ahrens dsl_dataset_fast_stat(os->os_dsl_dataset, stat); 1500a2eea2e1Sahrens } 1501a2eea2e1Sahrens 1502a2eea2e1Sahrens void 1503a2eea2e1Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv) 1504a2eea2e1Sahrens { 1505503ad85cSMatthew Ahrens ASSERT(os->os_dsl_dataset || 1506503ad85cSMatthew Ahrens os->os_phys->os_type == DMU_OST_META); 1507a2eea2e1Sahrens 1508503ad85cSMatthew Ahrens if (os->os_dsl_dataset != NULL) 1509503ad85cSMatthew Ahrens dsl_dataset_stats(os->os_dsl_dataset, nv); 1510a2eea2e1Sahrens 1511a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE, 1512503ad85cSMatthew Ahrens os->os_phys->os_type); 151314843421SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING, 151414843421SMatthew Ahrens dmu_objset_userspace_present(os)); 1515fa9e4066Sahrens } 1516fa9e4066Sahrens 1517fa9e4066Sahrens int 1518fa9e4066Sahrens dmu_objset_is_snapshot(objset_t *os) 1519fa9e4066Sahrens { 1520503ad85cSMatthew Ahrens if (os->os_dsl_dataset != NULL) 1521503ad85cSMatthew Ahrens return (dsl_dataset_is_snapshot(os->os_dsl_dataset)); 1522fa9e4066Sahrens else 1523fa9e4066Sahrens return (B_FALSE); 1524fa9e4066Sahrens } 1525fa9e4066Sahrens 1526ab04eb8eStimh int 1527ab04eb8eStimh dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen, 1528ab04eb8eStimh boolean_t *conflict) 1529ab04eb8eStimh { 1530503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 1531ab04eb8eStimh uint64_t ignored; 1532ab04eb8eStimh 1533ab04eb8eStimh if (ds->ds_phys->ds_snapnames_zapobj == 0) 1534ab04eb8eStimh return (ENOENT); 1535ab04eb8eStimh 1536ab04eb8eStimh return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset, 1537ab04eb8eStimh ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST, 1538ab04eb8eStimh real, maxlen, conflict)); 1539ab04eb8eStimh } 1540ab04eb8eStimh 1541fa9e4066Sahrens int 1542fa9e4066Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name, 1543b38f0970Sck uint64_t *idp, uint64_t *offp, boolean_t *case_conflict) 1544fa9e4066Sahrens { 1545503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 1546fa9e4066Sahrens zap_cursor_t cursor; 1547fa9e4066Sahrens zap_attribute_t attr; 1548fa9e4066Sahrens 1549fa9e4066Sahrens if (ds->ds_phys->ds_snapnames_zapobj == 0) 1550fa9e4066Sahrens return (ENOENT); 1551fa9e4066Sahrens 1552fa9e4066Sahrens zap_cursor_init_serialized(&cursor, 1553fa9e4066Sahrens ds->ds_dir->dd_pool->dp_meta_objset, 1554fa9e4066Sahrens ds->ds_phys->ds_snapnames_zapobj, *offp); 1555fa9e4066Sahrens 155687e5029aSahrens if (zap_cursor_retrieve(&cursor, &attr) != 0) { 155787e5029aSahrens zap_cursor_fini(&cursor); 155887e5029aSahrens return (ENOENT); 155987e5029aSahrens } 156087e5029aSahrens 156187e5029aSahrens if (strlen(attr.za_name) + 1 > namelen) { 156287e5029aSahrens zap_cursor_fini(&cursor); 156387e5029aSahrens return (ENAMETOOLONG); 156487e5029aSahrens } 156587e5029aSahrens 156687e5029aSahrens (void) strcpy(name, attr.za_name); 156787e5029aSahrens if (idp) 156887e5029aSahrens *idp = attr.za_first_integer; 1569b38f0970Sck if (case_conflict) 1570b38f0970Sck *case_conflict = attr.za_normalization_conflict; 157187e5029aSahrens zap_cursor_advance(&cursor); 157287e5029aSahrens *offp = zap_cursor_serialize(&cursor); 157387e5029aSahrens zap_cursor_fini(&cursor); 157487e5029aSahrens 157587e5029aSahrens return (0); 157687e5029aSahrens } 157787e5029aSahrens 157887e5029aSahrens int 157987e5029aSahrens dmu_dir_list_next(objset_t *os, int namelen, char *name, 158087e5029aSahrens uint64_t *idp, uint64_t *offp) 158187e5029aSahrens { 1582503ad85cSMatthew Ahrens dsl_dir_t *dd = os->os_dsl_dataset->ds_dir; 158387e5029aSahrens zap_cursor_t cursor; 158487e5029aSahrens zap_attribute_t attr; 158587e5029aSahrens 158687e5029aSahrens /* there is no next dir on a snapshot! */ 1587503ad85cSMatthew Ahrens if (os->os_dsl_dataset->ds_object != 158887e5029aSahrens dd->dd_phys->dd_head_dataset_obj) 1589fa9e4066Sahrens return (ENOENT); 1590fa9e4066Sahrens 159187e5029aSahrens zap_cursor_init_serialized(&cursor, 159287e5029aSahrens dd->dd_pool->dp_meta_objset, 159387e5029aSahrens dd->dd_phys->dd_child_dir_zapobj, *offp); 159487e5029aSahrens 159587e5029aSahrens if (zap_cursor_retrieve(&cursor, &attr) != 0) { 159687e5029aSahrens zap_cursor_fini(&cursor); 159787e5029aSahrens return (ENOENT); 159887e5029aSahrens } 159987e5029aSahrens 160087e5029aSahrens if (strlen(attr.za_name) + 1 > namelen) { 160187e5029aSahrens zap_cursor_fini(&cursor); 1602fa9e4066Sahrens return (ENAMETOOLONG); 160387e5029aSahrens } 1604fa9e4066Sahrens 1605fa9e4066Sahrens (void) strcpy(name, attr.za_name); 160687e5029aSahrens if (idp) 160787e5029aSahrens *idp = attr.za_first_integer; 1608fa9e4066Sahrens zap_cursor_advance(&cursor); 1609fa9e4066Sahrens *offp = zap_cursor_serialize(&cursor); 161087e5029aSahrens zap_cursor_fini(&cursor); 1611fa9e4066Sahrens 1612fa9e4066Sahrens return (0); 1613fa9e4066Sahrens } 1614fa9e4066Sahrens 1615088f3894Sahrens struct findarg { 1616fd136879SMatthew Ahrens int (*func)(const char *, void *); 1617088f3894Sahrens void *arg; 1618088f3894Sahrens }; 1619088f3894Sahrens 1620088f3894Sahrens /* ARGSUSED */ 1621088f3894Sahrens static int 1622088f3894Sahrens findfunc(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg) 1623088f3894Sahrens { 1624088f3894Sahrens struct findarg *fa = arg; 1625fd136879SMatthew Ahrens return (fa->func(dsname, fa->arg)); 1626088f3894Sahrens } 1627088f3894Sahrens 1628fa9e4066Sahrens /* 1629fa9e4066Sahrens * Find all objsets under name, and for each, call 'func(child_name, arg)'. 1630088f3894Sahrens * Perhaps change all callers to use dmu_objset_find_spa()? 1631fa9e4066Sahrens */ 16321d452cf5Sahrens int 1633fd136879SMatthew Ahrens dmu_objset_find(char *name, int func(const char *, void *), void *arg, 1634fd136879SMatthew Ahrens int flags) 1635088f3894Sahrens { 1636088f3894Sahrens struct findarg fa; 1637088f3894Sahrens fa.func = func; 1638088f3894Sahrens fa.arg = arg; 1639088f3894Sahrens return (dmu_objset_find_spa(NULL, name, findfunc, &fa, flags)); 1640088f3894Sahrens } 1641088f3894Sahrens 1642088f3894Sahrens /* 1643088f3894Sahrens * Find all objsets under name, call func on each 1644088f3894Sahrens */ 1645088f3894Sahrens int 1646088f3894Sahrens dmu_objset_find_spa(spa_t *spa, const char *name, 1647088f3894Sahrens int func(spa_t *, uint64_t, const char *, void *), void *arg, int flags) 1648fa9e4066Sahrens { 1649fa9e4066Sahrens dsl_dir_t *dd; 1650088f3894Sahrens dsl_pool_t *dp; 1651088f3894Sahrens dsl_dataset_t *ds; 1652fa9e4066Sahrens zap_cursor_t zc; 1653b7661cccSmmusante zap_attribute_t *attr; 1654fa9e4066Sahrens char *child; 1655088f3894Sahrens uint64_t thisobj; 1656088f3894Sahrens int err; 1657fa9e4066Sahrens 1658088f3894Sahrens if (name == NULL) 1659088f3894Sahrens name = spa_name(spa); 1660088f3894Sahrens err = dsl_dir_open_spa(spa, name, FTAG, &dd, NULL); 1661ea8dc4b6Seschrock if (err) 16621d452cf5Sahrens return (err); 1663fa9e4066Sahrens 1664088f3894Sahrens /* Don't visit hidden ($MOS & $ORIGIN) objsets. */ 1665088f3894Sahrens if (dd->dd_myname[0] == '$') { 1666088f3894Sahrens dsl_dir_close(dd, FTAG); 1667088f3894Sahrens return (0); 1668088f3894Sahrens } 1669088f3894Sahrens 1670088f3894Sahrens thisobj = dd->dd_phys->dd_head_dataset_obj; 1671b7661cccSmmusante attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); 1672088f3894Sahrens dp = dd->dd_pool; 1673fa9e4066Sahrens 1674fa9e4066Sahrens /* 1675fa9e4066Sahrens * Iterate over all children. 1676fa9e4066Sahrens */ 16770b69c2f0Sahrens if (flags & DS_FIND_CHILDREN) { 1678088f3894Sahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, 16790b69c2f0Sahrens dd->dd_phys->dd_child_dir_zapobj); 1680b7661cccSmmusante zap_cursor_retrieve(&zc, attr) == 0; 16810b69c2f0Sahrens (void) zap_cursor_advance(&zc)) { 1682b7661cccSmmusante ASSERT(attr->za_integer_length == sizeof (uint64_t)); 1683b7661cccSmmusante ASSERT(attr->za_num_integers == 1); 1684fa9e4066Sahrens 1685486ae710SMatthew Ahrens child = kmem_asprintf("%s/%s", name, attr->za_name); 1686088f3894Sahrens err = dmu_objset_find_spa(spa, child, func, arg, flags); 1687486ae710SMatthew Ahrens strfree(child); 16880b69c2f0Sahrens if (err) 16890b69c2f0Sahrens break; 16900b69c2f0Sahrens } 16910b69c2f0Sahrens zap_cursor_fini(&zc); 16921d452cf5Sahrens 16930b69c2f0Sahrens if (err) { 16940b69c2f0Sahrens dsl_dir_close(dd, FTAG); 1695b7661cccSmmusante kmem_free(attr, sizeof (zap_attribute_t)); 16960b69c2f0Sahrens return (err); 16970b69c2f0Sahrens } 1698fa9e4066Sahrens } 1699fa9e4066Sahrens 1700fa9e4066Sahrens /* 1701fa9e4066Sahrens * Iterate over all snapshots. 1702fa9e4066Sahrens */ 1703088f3894Sahrens if (flags & DS_FIND_SNAPSHOTS) { 1704088f3894Sahrens if (!dsl_pool_sync_context(dp)) 1705088f3894Sahrens rw_enter(&dp->dp_config_rwlock, RW_READER); 1706088f3894Sahrens err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds); 1707088f3894Sahrens if (!dsl_pool_sync_context(dp)) 1708088f3894Sahrens rw_exit(&dp->dp_config_rwlock); 1709088f3894Sahrens 1710088f3894Sahrens if (err == 0) { 1711088f3894Sahrens uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj; 1712088f3894Sahrens dsl_dataset_rele(ds, FTAG); 1713088f3894Sahrens 1714088f3894Sahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj); 1715088f3894Sahrens zap_cursor_retrieve(&zc, attr) == 0; 1716088f3894Sahrens (void) zap_cursor_advance(&zc)) { 1717088f3894Sahrens ASSERT(attr->za_integer_length == 1718088f3894Sahrens sizeof (uint64_t)); 1719088f3894Sahrens ASSERT(attr->za_num_integers == 1); 1720088f3894Sahrens 1721486ae710SMatthew Ahrens child = kmem_asprintf("%s@%s", 1722486ae710SMatthew Ahrens name, attr->za_name); 1723088f3894Sahrens err = func(spa, attr->za_first_integer, 1724088f3894Sahrens child, arg); 1725486ae710SMatthew Ahrens strfree(child); 1726088f3894Sahrens if (err) 1727088f3894Sahrens break; 1728088f3894Sahrens } 1729088f3894Sahrens zap_cursor_fini(&zc); 1730fa9e4066Sahrens } 1731fa9e4066Sahrens } 1732fa9e4066Sahrens 1733fa9e4066Sahrens dsl_dir_close(dd, FTAG); 1734b7661cccSmmusante kmem_free(attr, sizeof (zap_attribute_t)); 1735fa9e4066Sahrens 17361d452cf5Sahrens if (err) 17371d452cf5Sahrens return (err); 17381d452cf5Sahrens 1739fa9e4066Sahrens /* 1740fa9e4066Sahrens * Apply to self if appropriate. 1741fa9e4066Sahrens */ 1742088f3894Sahrens err = func(spa, thisobj, name, arg); 17431d452cf5Sahrens return (err); 1744fa9e4066Sahrens } 1745f18faf3fSek 17467f73c863SRich Morris /* ARGSUSED */ 17477f73c863SRich Morris int 1748fd136879SMatthew Ahrens dmu_objset_prefetch(const char *name, void *arg) 17497f73c863SRich Morris { 17507f73c863SRich Morris dsl_dataset_t *ds; 17517f73c863SRich Morris 17527f73c863SRich Morris if (dsl_dataset_hold(name, FTAG, &ds)) 17537f73c863SRich Morris return (0); 17547f73c863SRich Morris 17557f73c863SRich Morris if (!BP_IS_HOLE(&ds->ds_phys->ds_bp)) { 17567f73c863SRich Morris mutex_enter(&ds->ds_opening_lock); 1757503ad85cSMatthew Ahrens if (ds->ds_objset == NULL) { 17587f73c863SRich Morris uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH; 17597f73c863SRich Morris zbookmark_t zb; 17607f73c863SRich Morris 1761b24ab676SJeff Bonwick SET_BOOKMARK(&zb, ds->ds_object, ZB_ROOT_OBJECT, 1762b24ab676SJeff Bonwick ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 17637f73c863SRich Morris 17643f9d6ad7SLin Ling (void) dsl_read_nolock(NULL, dsl_dataset_get_spa(ds), 17657f73c863SRich Morris &ds->ds_phys->ds_bp, NULL, NULL, 17667f73c863SRich Morris ZIO_PRIORITY_ASYNC_READ, 17677f73c863SRich Morris ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, 17687f73c863SRich Morris &aflags, &zb); 17697f73c863SRich Morris } 17707f73c863SRich Morris mutex_exit(&ds->ds_opening_lock); 17717f73c863SRich Morris } 17727f73c863SRich Morris 17737f73c863SRich Morris dsl_dataset_rele(ds, FTAG); 17747f73c863SRich Morris return (0); 17757f73c863SRich Morris } 17767f73c863SRich Morris 1777f18faf3fSek void 1778f18faf3fSek dmu_objset_set_user(objset_t *os, void *user_ptr) 1779f18faf3fSek { 1780503ad85cSMatthew Ahrens ASSERT(MUTEX_HELD(&os->os_user_ptr_lock)); 1781503ad85cSMatthew Ahrens os->os_user_ptr = user_ptr; 1782f18faf3fSek } 1783f18faf3fSek 1784f18faf3fSek void * 1785f18faf3fSek dmu_objset_get_user(objset_t *os) 1786f18faf3fSek { 1787503ad85cSMatthew Ahrens ASSERT(MUTEX_HELD(&os->os_user_ptr_lock)); 1788503ad85cSMatthew Ahrens return (os->os_user_ptr); 1789f18faf3fSek } 1790