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. 234445fffbSMatthew Ahrens * Copyright (c) 2012 by Delphix. All rights reserved. 24fa9e4066Sahrens */ 25fa9e4066Sahrens 2655da60b9SMark J Musante /* Portions Copyright 2010 Robert Milkowski */ 2755da60b9SMark J Musante 28ecd6cf80Smarks #include <sys/cred.h> 29fa9e4066Sahrens #include <sys/zfs_context.h> 30fa9e4066Sahrens #include <sys/dmu_objset.h> 31fa9e4066Sahrens #include <sys/dsl_dir.h> 32fa9e4066Sahrens #include <sys/dsl_dataset.h> 33fa9e4066Sahrens #include <sys/dsl_prop.h> 34fa9e4066Sahrens #include <sys/dsl_pool.h> 351d452cf5Sahrens #include <sys/dsl_synctask.h> 36ecd6cf80Smarks #include <sys/dsl_deleg.h> 37fa9e4066Sahrens #include <sys/dnode.h> 38fa9e4066Sahrens #include <sys/dbuf.h> 39a2eea2e1Sahrens #include <sys/zvol.h> 40fa9e4066Sahrens #include <sys/dmu_tx.h> 41fa9e4066Sahrens #include <sys/zap.h> 42fa9e4066Sahrens #include <sys/zil.h> 43fa9e4066Sahrens #include <sys/dmu_impl.h> 44ecd6cf80Smarks #include <sys/zfs_ioctl.h> 450a586ceaSMark Shellenbaum #include <sys/sa.h> 4699d5e173STim Haley #include <sys/zfs_onexit.h> 47fa9e4066Sahrens 48744947dcSTom Erickson /* 49744947dcSTom Erickson * Needed to close a window in dnode_move() that allows the objset to be freed 50744947dcSTom Erickson * before it can be safely accessed. 51744947dcSTom Erickson */ 52744947dcSTom Erickson krwlock_t os_lock; 53744947dcSTom Erickson 54744947dcSTom Erickson void 55744947dcSTom Erickson dmu_objset_init(void) 56744947dcSTom Erickson { 57744947dcSTom Erickson rw_init(&os_lock, NULL, RW_DEFAULT, NULL); 58744947dcSTom Erickson } 59744947dcSTom Erickson 60744947dcSTom Erickson void 61744947dcSTom Erickson dmu_objset_fini(void) 62744947dcSTom Erickson { 63744947dcSTom Erickson rw_destroy(&os_lock); 64744947dcSTom Erickson } 65744947dcSTom Erickson 66fa9e4066Sahrens spa_t * 67fa9e4066Sahrens dmu_objset_spa(objset_t *os) 68fa9e4066Sahrens { 69503ad85cSMatthew Ahrens return (os->os_spa); 70fa9e4066Sahrens } 71fa9e4066Sahrens 72fa9e4066Sahrens zilog_t * 73fa9e4066Sahrens dmu_objset_zil(objset_t *os) 74fa9e4066Sahrens { 75503ad85cSMatthew Ahrens return (os->os_zil); 76fa9e4066Sahrens } 77fa9e4066Sahrens 78fa9e4066Sahrens dsl_pool_t * 79fa9e4066Sahrens dmu_objset_pool(objset_t *os) 80fa9e4066Sahrens { 81fa9e4066Sahrens dsl_dataset_t *ds; 82fa9e4066Sahrens 83503ad85cSMatthew Ahrens if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir) 84fa9e4066Sahrens return (ds->ds_dir->dd_pool); 85fa9e4066Sahrens else 86503ad85cSMatthew Ahrens return (spa_get_dsl(os->os_spa)); 87fa9e4066Sahrens } 88fa9e4066Sahrens 89fa9e4066Sahrens dsl_dataset_t * 90fa9e4066Sahrens dmu_objset_ds(objset_t *os) 91fa9e4066Sahrens { 92503ad85cSMatthew Ahrens return (os->os_dsl_dataset); 93fa9e4066Sahrens } 94fa9e4066Sahrens 95fa9e4066Sahrens dmu_objset_type_t 96fa9e4066Sahrens dmu_objset_type(objset_t *os) 97fa9e4066Sahrens { 98503ad85cSMatthew Ahrens return (os->os_phys->os_type); 99fa9e4066Sahrens } 100fa9e4066Sahrens 101fa9e4066Sahrens void 102fa9e4066Sahrens dmu_objset_name(objset_t *os, char *buf) 103fa9e4066Sahrens { 104503ad85cSMatthew Ahrens dsl_dataset_name(os->os_dsl_dataset, buf); 105fa9e4066Sahrens } 106fa9e4066Sahrens 107fa9e4066Sahrens uint64_t 108fa9e4066Sahrens dmu_objset_id(objset_t *os) 109fa9e4066Sahrens { 110503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 111fa9e4066Sahrens 112fa9e4066Sahrens return (ds ? ds->ds_object : 0); 113fa9e4066Sahrens } 114fa9e4066Sahrens 11555da60b9SMark J Musante uint64_t 11655da60b9SMark J Musante dmu_objset_syncprop(objset_t *os) 11755da60b9SMark J Musante { 11855da60b9SMark J Musante return (os->os_sync); 11955da60b9SMark J Musante } 12055da60b9SMark J Musante 121e09fa4daSNeil Perrin uint64_t 122e09fa4daSNeil Perrin dmu_objset_logbias(objset_t *os) 123e09fa4daSNeil Perrin { 124e09fa4daSNeil Perrin return (os->os_logbias); 125e09fa4daSNeil Perrin } 126e09fa4daSNeil Perrin 127fa9e4066Sahrens static void 128fa9e4066Sahrens checksum_changed_cb(void *arg, uint64_t newval) 129fa9e4066Sahrens { 130503ad85cSMatthew Ahrens objset_t *os = arg; 131fa9e4066Sahrens 132fa9e4066Sahrens /* 133fa9e4066Sahrens * Inheritance should have been done by now. 134fa9e4066Sahrens */ 135fa9e4066Sahrens ASSERT(newval != ZIO_CHECKSUM_INHERIT); 136fa9e4066Sahrens 137503ad85cSMatthew Ahrens os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE); 138fa9e4066Sahrens } 139fa9e4066Sahrens 140fa9e4066Sahrens static void 141fa9e4066Sahrens compression_changed_cb(void *arg, uint64_t newval) 142fa9e4066Sahrens { 143503ad85cSMatthew Ahrens objset_t *os = arg; 144fa9e4066Sahrens 145fa9e4066Sahrens /* 146fa9e4066Sahrens * Inheritance and range checking should have been done by now. 147fa9e4066Sahrens */ 148fa9e4066Sahrens ASSERT(newval != ZIO_COMPRESS_INHERIT); 149fa9e4066Sahrens 150503ad85cSMatthew Ahrens os->os_compress = zio_compress_select(newval, ZIO_COMPRESS_ON_VALUE); 151fa9e4066Sahrens } 152fa9e4066Sahrens 153d0ad202dSahrens static void 154d0ad202dSahrens copies_changed_cb(void *arg, uint64_t newval) 155d0ad202dSahrens { 156503ad85cSMatthew Ahrens objset_t *os = arg; 157d0ad202dSahrens 158d0ad202dSahrens /* 159d0ad202dSahrens * Inheritance and range checking should have been done by now. 160d0ad202dSahrens */ 161d0ad202dSahrens ASSERT(newval > 0); 162503ad85cSMatthew Ahrens ASSERT(newval <= spa_max_replication(os->os_spa)); 163d0ad202dSahrens 164503ad85cSMatthew Ahrens os->os_copies = newval; 165d0ad202dSahrens } 166d0ad202dSahrens 167b24ab676SJeff Bonwick static void 168b24ab676SJeff Bonwick dedup_changed_cb(void *arg, uint64_t newval) 169b24ab676SJeff Bonwick { 170b24ab676SJeff Bonwick objset_t *os = arg; 171b24ab676SJeff Bonwick spa_t *spa = os->os_spa; 172b24ab676SJeff Bonwick enum zio_checksum checksum; 173b24ab676SJeff Bonwick 174b24ab676SJeff Bonwick /* 175b24ab676SJeff Bonwick * Inheritance should have been done by now. 176b24ab676SJeff Bonwick */ 177b24ab676SJeff Bonwick ASSERT(newval != ZIO_CHECKSUM_INHERIT); 178b24ab676SJeff Bonwick 179b24ab676SJeff Bonwick checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF); 180b24ab676SJeff Bonwick 181b24ab676SJeff Bonwick os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK; 182b24ab676SJeff Bonwick os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY); 183b24ab676SJeff Bonwick } 184b24ab676SJeff Bonwick 1853baa08fcSek static void 1863baa08fcSek primary_cache_changed_cb(void *arg, uint64_t newval) 1873baa08fcSek { 188503ad85cSMatthew Ahrens objset_t *os = arg; 1893baa08fcSek 1903baa08fcSek /* 1913baa08fcSek * Inheritance and range checking should have been done by now. 1923baa08fcSek */ 1933baa08fcSek ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE || 1943baa08fcSek newval == ZFS_CACHE_METADATA); 1953baa08fcSek 196503ad85cSMatthew Ahrens os->os_primary_cache = newval; 1973baa08fcSek } 1983baa08fcSek 1993baa08fcSek static void 2003baa08fcSek secondary_cache_changed_cb(void *arg, uint64_t newval) 2013baa08fcSek { 202503ad85cSMatthew Ahrens objset_t *os = arg; 2033baa08fcSek 2043baa08fcSek /* 2053baa08fcSek * Inheritance and range checking should have been done by now. 2063baa08fcSek */ 2073baa08fcSek ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE || 2083baa08fcSek newval == ZFS_CACHE_METADATA); 2093baa08fcSek 210503ad85cSMatthew Ahrens os->os_secondary_cache = newval; 2113baa08fcSek } 2123baa08fcSek 21355da60b9SMark J Musante static void 21455da60b9SMark J Musante sync_changed_cb(void *arg, uint64_t newval) 21555da60b9SMark J Musante { 21655da60b9SMark J Musante objset_t *os = arg; 21755da60b9SMark J Musante 21855da60b9SMark J Musante /* 21955da60b9SMark J Musante * Inheritance and range checking should have been done by now. 22055da60b9SMark J Musante */ 22155da60b9SMark J Musante ASSERT(newval == ZFS_SYNC_STANDARD || newval == ZFS_SYNC_ALWAYS || 22255da60b9SMark J Musante newval == ZFS_SYNC_DISABLED); 22355da60b9SMark J Musante 22455da60b9SMark J Musante os->os_sync = newval; 22555da60b9SMark J Musante if (os->os_zil) 22655da60b9SMark J Musante zil_set_sync(os->os_zil, newval); 22755da60b9SMark J Musante } 22855da60b9SMark J Musante 229e09fa4daSNeil Perrin static void 230e09fa4daSNeil Perrin logbias_changed_cb(void *arg, uint64_t newval) 231e09fa4daSNeil Perrin { 232e09fa4daSNeil Perrin objset_t *os = arg; 233e09fa4daSNeil Perrin 234e09fa4daSNeil Perrin ASSERT(newval == ZFS_LOGBIAS_LATENCY || 235e09fa4daSNeil Perrin newval == ZFS_LOGBIAS_THROUGHPUT); 236e09fa4daSNeil Perrin os->os_logbias = newval; 237e09fa4daSNeil Perrin if (os->os_zil) 238e09fa4daSNeil Perrin zil_set_logbias(os->os_zil, newval); 239e09fa4daSNeil Perrin } 240e09fa4daSNeil Perrin 241fa9e4066Sahrens void 242fa9e4066Sahrens dmu_objset_byteswap(void *buf, size_t size) 243fa9e4066Sahrens { 244fa9e4066Sahrens objset_phys_t *osp = buf; 245fa9e4066Sahrens 24614843421SMatthew Ahrens ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t)); 247fa9e4066Sahrens dnode_byteswap(&osp->os_meta_dnode); 248fa9e4066Sahrens byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t)); 249fa9e4066Sahrens osp->os_type = BSWAP_64(osp->os_type); 25014843421SMatthew Ahrens osp->os_flags = BSWAP_64(osp->os_flags); 25114843421SMatthew Ahrens if (size == sizeof (objset_phys_t)) { 25214843421SMatthew Ahrens dnode_byteswap(&osp->os_userused_dnode); 25314843421SMatthew Ahrens dnode_byteswap(&osp->os_groupused_dnode); 25414843421SMatthew Ahrens } 255fa9e4066Sahrens } 256fa9e4066Sahrens 257ea8dc4b6Seschrock int 258ea8dc4b6Seschrock dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, 259503ad85cSMatthew Ahrens objset_t **osp) 260fa9e4066Sahrens { 261503ad85cSMatthew Ahrens objset_t *os; 262088f3894Sahrens int i, err; 263fa9e4066Sahrens 26491ebeef5Sahrens ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock)); 26591ebeef5Sahrens 266503ad85cSMatthew Ahrens os = kmem_zalloc(sizeof (objset_t), KM_SLEEP); 267503ad85cSMatthew Ahrens os->os_dsl_dataset = ds; 268503ad85cSMatthew Ahrens os->os_spa = spa; 269503ad85cSMatthew Ahrens os->os_rootbp = bp; 270503ad85cSMatthew Ahrens if (!BP_IS_HOLE(os->os_rootbp)) { 27113506d1eSmaybee uint32_t aflags = ARC_WAIT; 272ea8dc4b6Seschrock zbookmark_t zb; 273b24ab676SJeff Bonwick SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET, 274b24ab676SJeff Bonwick ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 275b24ab676SJeff Bonwick 276503ad85cSMatthew Ahrens if (DMU_OS_IS_L2CACHEABLE(os)) 2773baa08fcSek aflags |= ARC_L2CACHE; 278ea8dc4b6Seschrock 279503ad85cSMatthew Ahrens dprintf_bp(os->os_rootbp, "reading %s", ""); 280*1b912ec7SGeorge Wilson err = arc_read(NULL, spa, os->os_rootbp, 281503ad85cSMatthew Ahrens arc_getbuf_func, &os->os_phys_buf, 28213506d1eSmaybee ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb); 283ea8dc4b6Seschrock if (err) { 284503ad85cSMatthew Ahrens kmem_free(os, sizeof (objset_t)); 285b87f3af3Sperrin /* convert checksum errors into IO errors */ 286b87f3af3Sperrin if (err == ECKSUM) 287b87f3af3Sperrin err = EIO; 288ea8dc4b6Seschrock return (err); 289ea8dc4b6Seschrock } 29014843421SMatthew Ahrens 29114843421SMatthew Ahrens /* Increase the blocksize if we are permitted. */ 29214843421SMatthew Ahrens if (spa_version(spa) >= SPA_VERSION_USERSPACE && 293503ad85cSMatthew Ahrens arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) { 29414843421SMatthew Ahrens arc_buf_t *buf = arc_buf_alloc(spa, 295503ad85cSMatthew Ahrens sizeof (objset_phys_t), &os->os_phys_buf, 29614843421SMatthew Ahrens ARC_BUFC_METADATA); 29714843421SMatthew Ahrens bzero(buf->b_data, sizeof (objset_phys_t)); 298503ad85cSMatthew Ahrens bcopy(os->os_phys_buf->b_data, buf->b_data, 299503ad85cSMatthew Ahrens arc_buf_size(os->os_phys_buf)); 300503ad85cSMatthew Ahrens (void) arc_buf_remove_ref(os->os_phys_buf, 301503ad85cSMatthew Ahrens &os->os_phys_buf); 302503ad85cSMatthew Ahrens os->os_phys_buf = buf; 30314843421SMatthew Ahrens } 30414843421SMatthew Ahrens 305503ad85cSMatthew Ahrens os->os_phys = os->os_phys_buf->b_data; 306503ad85cSMatthew Ahrens os->os_flags = os->os_phys->os_flags; 307fa9e4066Sahrens } else { 30814843421SMatthew Ahrens int size = spa_version(spa) >= SPA_VERSION_USERSPACE ? 30914843421SMatthew Ahrens sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE; 310503ad85cSMatthew Ahrens os->os_phys_buf = arc_buf_alloc(spa, size, 311503ad85cSMatthew Ahrens &os->os_phys_buf, ARC_BUFC_METADATA); 312503ad85cSMatthew Ahrens os->os_phys = os->os_phys_buf->b_data; 313503ad85cSMatthew Ahrens bzero(os->os_phys, size); 314fa9e4066Sahrens } 315fa9e4066Sahrens 316fa9e4066Sahrens /* 317fa9e4066Sahrens * Note: the changed_cb will be called once before the register 318fa9e4066Sahrens * func returns, thus changing the checksum/compression from the 3193baa08fcSek * default (fletcher2/off). Snapshots don't need to know about 3203baa08fcSek * checksum/compression/copies. 321fa9e4066Sahrens */ 3223baa08fcSek if (ds) { 3233baa08fcSek err = dsl_prop_register(ds, "primarycache", 324503ad85cSMatthew Ahrens primary_cache_changed_cb, os); 325d0ad202dSahrens if (err == 0) 3263baa08fcSek err = dsl_prop_register(ds, "secondarycache", 327503ad85cSMatthew Ahrens secondary_cache_changed_cb, os); 3283baa08fcSek if (!dsl_dataset_is_snapshot(ds)) { 3293baa08fcSek if (err == 0) 3303baa08fcSek err = dsl_prop_register(ds, "checksum", 331503ad85cSMatthew Ahrens checksum_changed_cb, os); 3323baa08fcSek if (err == 0) 3333baa08fcSek err = dsl_prop_register(ds, "compression", 334503ad85cSMatthew Ahrens compression_changed_cb, os); 3353baa08fcSek if (err == 0) 3363baa08fcSek err = dsl_prop_register(ds, "copies", 337503ad85cSMatthew Ahrens copies_changed_cb, os); 338b24ab676SJeff Bonwick if (err == 0) 339b24ab676SJeff Bonwick err = dsl_prop_register(ds, "dedup", 340b24ab676SJeff Bonwick dedup_changed_cb, os); 341e09fa4daSNeil Perrin if (err == 0) 342e09fa4daSNeil Perrin err = dsl_prop_register(ds, "logbias", 343e09fa4daSNeil Perrin logbias_changed_cb, os); 34455da60b9SMark J Musante if (err == 0) 34555da60b9SMark J Musante err = dsl_prop_register(ds, "sync", 34655da60b9SMark J Musante sync_changed_cb, os); 3473baa08fcSek } 348ea8dc4b6Seschrock if (err) { 349503ad85cSMatthew Ahrens VERIFY(arc_buf_remove_ref(os->os_phys_buf, 350503ad85cSMatthew Ahrens &os->os_phys_buf) == 1); 351503ad85cSMatthew Ahrens kmem_free(os, sizeof (objset_t)); 352ea8dc4b6Seschrock return (err); 353ea8dc4b6Seschrock } 35499653d4eSeschrock } else if (ds == NULL) { 355fa9e4066Sahrens /* It's the meta-objset. */ 356503ad85cSMatthew Ahrens os->os_checksum = ZIO_CHECKSUM_FLETCHER_4; 357503ad85cSMatthew Ahrens os->os_compress = ZIO_COMPRESS_LZJB; 358503ad85cSMatthew Ahrens os->os_copies = spa_max_replication(spa); 359b24ab676SJeff Bonwick os->os_dedup_checksum = ZIO_CHECKSUM_OFF; 360b24ab676SJeff Bonwick os->os_dedup_verify = 0; 361b24ab676SJeff Bonwick os->os_logbias = 0; 36255da60b9SMark J Musante os->os_sync = 0; 363503ad85cSMatthew Ahrens os->os_primary_cache = ZFS_CACHE_ALL; 364503ad85cSMatthew Ahrens os->os_secondary_cache = ZFS_CACHE_ALL; 365fa9e4066Sahrens } 366fa9e4066Sahrens 3676e0cbcaaSMatthew Ahrens if (ds == NULL || !dsl_dataset_is_snapshot(ds)) 3686e0cbcaaSMatthew Ahrens os->os_zil_header = os->os_phys->os_zil_header; 369503ad85cSMatthew Ahrens os->os_zil = zil_alloc(os, &os->os_zil_header); 370fa9e4066Sahrens 371fa9e4066Sahrens for (i = 0; i < TXG_SIZE; i++) { 372503ad85cSMatthew Ahrens list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t), 373fa9e4066Sahrens offsetof(dnode_t, dn_dirty_link[i])); 374503ad85cSMatthew Ahrens list_create(&os->os_free_dnodes[i], sizeof (dnode_t), 375fa9e4066Sahrens offsetof(dnode_t, dn_dirty_link[i])); 376fa9e4066Sahrens } 377503ad85cSMatthew Ahrens list_create(&os->os_dnodes, sizeof (dnode_t), 378fa9e4066Sahrens offsetof(dnode_t, dn_link)); 379503ad85cSMatthew Ahrens list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t), 380fa9e4066Sahrens offsetof(dmu_buf_impl_t, db_link)); 381fa9e4066Sahrens 382503ad85cSMatthew Ahrens mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL); 383503ad85cSMatthew Ahrens mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL); 384503ad85cSMatthew Ahrens mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL); 385503ad85cSMatthew Ahrens 386744947dcSTom Erickson DMU_META_DNODE(os) = dnode_special_open(os, 387744947dcSTom Erickson &os->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT, 388744947dcSTom Erickson &os->os_meta_dnode); 389503ad85cSMatthew Ahrens if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) { 390744947dcSTom Erickson DMU_USERUSED_DNODE(os) = dnode_special_open(os, 391744947dcSTom Erickson &os->os_phys->os_userused_dnode, DMU_USERUSED_OBJECT, 392744947dcSTom Erickson &os->os_userused_dnode); 393744947dcSTom Erickson DMU_GROUPUSED_DNODE(os) = dnode_special_open(os, 394744947dcSTom Erickson &os->os_phys->os_groupused_dnode, DMU_GROUPUSED_OBJECT, 395744947dcSTom Erickson &os->os_groupused_dnode); 39614843421SMatthew Ahrens } 397fa9e4066Sahrens 39891ebeef5Sahrens /* 39991ebeef5Sahrens * We should be the only thread trying to do this because we 40091ebeef5Sahrens * have ds_opening_lock 40191ebeef5Sahrens */ 40291ebeef5Sahrens if (ds) { 403503ad85cSMatthew Ahrens mutex_enter(&ds->ds_lock); 404503ad85cSMatthew Ahrens ASSERT(ds->ds_objset == NULL); 405503ad85cSMatthew Ahrens ds->ds_objset = os; 406503ad85cSMatthew Ahrens mutex_exit(&ds->ds_lock); 407fa9e4066Sahrens } 408fa9e4066Sahrens 409503ad85cSMatthew Ahrens *osp = os; 410ea8dc4b6Seschrock return (0); 411fa9e4066Sahrens } 412fa9e4066Sahrens 413503ad85cSMatthew Ahrens int 414503ad85cSMatthew Ahrens dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp) 4153cb34c60Sahrens { 416503ad85cSMatthew Ahrens int err = 0; 4173cb34c60Sahrens 4183cb34c60Sahrens mutex_enter(&ds->ds_opening_lock); 419503ad85cSMatthew Ahrens *osp = ds->ds_objset; 420503ad85cSMatthew Ahrens if (*osp == NULL) { 4213cb34c60Sahrens err = dmu_objset_open_impl(dsl_dataset_get_spa(ds), 422feaa74e4SMark Maybee ds, dsl_dataset_get_blkptr(ds), osp); 4233cb34c60Sahrens } 4243cb34c60Sahrens mutex_exit(&ds->ds_opening_lock); 425503ad85cSMatthew Ahrens return (err); 4263cb34c60Sahrens } 4273cb34c60Sahrens 428503ad85cSMatthew Ahrens /* called from zpl */ 4293cb34c60Sahrens int 430503ad85cSMatthew Ahrens dmu_objset_hold(const char *name, void *tag, objset_t **osp) 4313cb34c60Sahrens { 432503ad85cSMatthew Ahrens dsl_dataset_t *ds; 4333cb34c60Sahrens int err; 4343cb34c60Sahrens 435503ad85cSMatthew Ahrens err = dsl_dataset_hold(name, tag, &ds); 4363cb34c60Sahrens if (err) 437503ad85cSMatthew Ahrens return (err); 438503ad85cSMatthew Ahrens 439503ad85cSMatthew Ahrens err = dmu_objset_from_ds(ds, osp); 440503ad85cSMatthew Ahrens if (err) 441503ad85cSMatthew Ahrens dsl_dataset_rele(ds, tag); 442503ad85cSMatthew Ahrens 4433cb34c60Sahrens return (err); 4443cb34c60Sahrens } 4453cb34c60Sahrens 446fa9e4066Sahrens /* called from zpl */ 447fa9e4066Sahrens int 448503ad85cSMatthew Ahrens dmu_objset_own(const char *name, dmu_objset_type_t type, 449503ad85cSMatthew Ahrens boolean_t readonly, void *tag, objset_t **osp) 450fa9e4066Sahrens { 451f18faf3fSek dsl_dataset_t *ds; 452f18faf3fSek int err; 453fa9e4066Sahrens 454503ad85cSMatthew Ahrens err = dsl_dataset_own(name, B_FALSE, tag, &ds); 455503ad85cSMatthew Ahrens if (err) 456fa9e4066Sahrens return (err); 457fa9e4066Sahrens 458503ad85cSMatthew Ahrens err = dmu_objset_from_ds(ds, osp); 4593cb34c60Sahrens if (err) { 460503ad85cSMatthew Ahrens dsl_dataset_disown(ds, tag); 461681d9761SEric Taylor } else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) { 462503ad85cSMatthew Ahrens dmu_objset_disown(*osp, tag); 463503ad85cSMatthew Ahrens return (EINVAL); 464681d9761SEric Taylor } else if (!readonly && dsl_dataset_is_snapshot(ds)) { 465681d9761SEric Taylor dmu_objset_disown(*osp, tag); 466681d9761SEric Taylor return (EROFS); 467fa9e4066Sahrens } 4683cb34c60Sahrens return (err); 469fa9e4066Sahrens } 470fa9e4066Sahrens 471fa9e4066Sahrens void 472503ad85cSMatthew Ahrens dmu_objset_rele(objset_t *os, void *tag) 473fa9e4066Sahrens { 474503ad85cSMatthew Ahrens dsl_dataset_rele(os->os_dsl_dataset, tag); 475503ad85cSMatthew Ahrens } 476503ad85cSMatthew Ahrens 477503ad85cSMatthew Ahrens void 478503ad85cSMatthew Ahrens dmu_objset_disown(objset_t *os, void *tag) 479503ad85cSMatthew Ahrens { 480503ad85cSMatthew Ahrens dsl_dataset_disown(os->os_dsl_dataset, tag); 481fa9e4066Sahrens } 482fa9e4066Sahrens 483436b2950Sperrin int 4841934e92fSmaybee dmu_objset_evict_dbufs(objset_t *os) 485ea8dc4b6Seschrock { 486ea8dc4b6Seschrock dnode_t *dn; 487c543ec06Sahrens 488503ad85cSMatthew Ahrens mutex_enter(&os->os_lock); 489c543ec06Sahrens 490c543ec06Sahrens /* process the mdn last, since the other dnodes have holds on it */ 491744947dcSTom Erickson list_remove(&os->os_dnodes, DMU_META_DNODE(os)); 492744947dcSTom Erickson list_insert_tail(&os->os_dnodes, DMU_META_DNODE(os)); 493ea8dc4b6Seschrock 494ea8dc4b6Seschrock /* 495c543ec06Sahrens * Find the first dnode with holds. We have to do this dance 496c543ec06Sahrens * because dnode_add_ref() only works if you already have a 497c543ec06Sahrens * hold. If there are no holds then it has no dbufs so OK to 498c543ec06Sahrens * skip. 499ea8dc4b6Seschrock */ 500503ad85cSMatthew Ahrens for (dn = list_head(&os->os_dnodes); 5011934e92fSmaybee dn && !dnode_add_ref(dn, FTAG); 502503ad85cSMatthew Ahrens dn = list_next(&os->os_dnodes, dn)) 503c543ec06Sahrens continue; 504c543ec06Sahrens 505c543ec06Sahrens while (dn) { 506c543ec06Sahrens dnode_t *next_dn = dn; 507c543ec06Sahrens 508c543ec06Sahrens do { 509503ad85cSMatthew Ahrens next_dn = list_next(&os->os_dnodes, next_dn); 5101934e92fSmaybee } while (next_dn && !dnode_add_ref(next_dn, FTAG)); 511c543ec06Sahrens 512503ad85cSMatthew Ahrens mutex_exit(&os->os_lock); 5131934e92fSmaybee dnode_evict_dbufs(dn); 514c543ec06Sahrens dnode_rele(dn, FTAG); 515503ad85cSMatthew Ahrens mutex_enter(&os->os_lock); 516c543ec06Sahrens dn = next_dn; 517ea8dc4b6Seschrock } 518744947dcSTom Erickson dn = list_head(&os->os_dnodes); 519503ad85cSMatthew Ahrens mutex_exit(&os->os_lock); 520744947dcSTom Erickson return (dn != DMU_META_DNODE(os)); 521ea8dc4b6Seschrock } 522ea8dc4b6Seschrock 523fa9e4066Sahrens void 524503ad85cSMatthew Ahrens dmu_objset_evict(objset_t *os) 525fa9e4066Sahrens { 526503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 527fa9e4066Sahrens 528b24ab676SJeff Bonwick for (int t = 0; t < TXG_SIZE; t++) 529b24ab676SJeff Bonwick ASSERT(!dmu_objset_is_dirty(os, t)); 530fa9e4066Sahrens 5313baa08fcSek if (ds) { 5323baa08fcSek if (!dsl_dataset_is_snapshot(ds)) { 5333baa08fcSek VERIFY(0 == dsl_prop_unregister(ds, "checksum", 534503ad85cSMatthew Ahrens checksum_changed_cb, os)); 5353baa08fcSek VERIFY(0 == dsl_prop_unregister(ds, "compression", 536503ad85cSMatthew Ahrens compression_changed_cb, os)); 5373baa08fcSek VERIFY(0 == dsl_prop_unregister(ds, "copies", 538503ad85cSMatthew Ahrens copies_changed_cb, os)); 539b24ab676SJeff Bonwick VERIFY(0 == dsl_prop_unregister(ds, "dedup", 540b24ab676SJeff Bonwick dedup_changed_cb, os)); 541e09fa4daSNeil Perrin VERIFY(0 == dsl_prop_unregister(ds, "logbias", 542e09fa4daSNeil Perrin logbias_changed_cb, os)); 54355da60b9SMark J Musante VERIFY(0 == dsl_prop_unregister(ds, "sync", 54455da60b9SMark J Musante sync_changed_cb, os)); 5453baa08fcSek } 5463baa08fcSek VERIFY(0 == dsl_prop_unregister(ds, "primarycache", 547503ad85cSMatthew Ahrens primary_cache_changed_cb, os)); 5483baa08fcSek VERIFY(0 == dsl_prop_unregister(ds, "secondarycache", 549503ad85cSMatthew Ahrens secondary_cache_changed_cb, os)); 550fa9e4066Sahrens } 551fa9e4066Sahrens 5520a586ceaSMark Shellenbaum if (os->os_sa) 5530a586ceaSMark Shellenbaum sa_tear_down(os); 5540a586ceaSMark Shellenbaum 555ea8dc4b6Seschrock /* 556ea8dc4b6Seschrock * We should need only a single pass over the dnode list, since 557ea8dc4b6Seschrock * nothing can be added to the list at this point. 558ea8dc4b6Seschrock */ 559503ad85cSMatthew Ahrens (void) dmu_objset_evict_dbufs(os); 560ea8dc4b6Seschrock 561744947dcSTom Erickson dnode_special_close(&os->os_meta_dnode); 562744947dcSTom Erickson if (DMU_USERUSED_DNODE(os)) { 563744947dcSTom Erickson dnode_special_close(&os->os_userused_dnode); 564744947dcSTom Erickson dnode_special_close(&os->os_groupused_dnode); 56514843421SMatthew Ahrens } 566503ad85cSMatthew Ahrens zil_free(os->os_zil); 567fa9e4066Sahrens 568503ad85cSMatthew Ahrens ASSERT3P(list_head(&os->os_dnodes), ==, NULL); 56914843421SMatthew Ahrens 570503ad85cSMatthew Ahrens VERIFY(arc_buf_remove_ref(os->os_phys_buf, &os->os_phys_buf) == 1); 571744947dcSTom Erickson 572744947dcSTom Erickson /* 573744947dcSTom Erickson * This is a barrier to prevent the objset from going away in 574744947dcSTom Erickson * dnode_move() until we can safely ensure that the objset is still in 575744947dcSTom Erickson * use. We consider the objset valid before the barrier and invalid 576744947dcSTom Erickson * after the barrier. 577744947dcSTom Erickson */ 578744947dcSTom Erickson rw_enter(&os_lock, RW_READER); 579744947dcSTom Erickson rw_exit(&os_lock); 580744947dcSTom Erickson 581503ad85cSMatthew Ahrens mutex_destroy(&os->os_lock); 582503ad85cSMatthew Ahrens mutex_destroy(&os->os_obj_lock); 583503ad85cSMatthew Ahrens mutex_destroy(&os->os_user_ptr_lock); 584503ad85cSMatthew Ahrens kmem_free(os, sizeof (objset_t)); 585fa9e4066Sahrens } 586fa9e4066Sahrens 58771eb0538SChris Kirby timestruc_t 58871eb0538SChris Kirby dmu_objset_snap_cmtime(objset_t *os) 58971eb0538SChris Kirby { 59071eb0538SChris Kirby return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir)); 59171eb0538SChris Kirby } 59271eb0538SChris Kirby 593fa9e4066Sahrens /* called from dsl for meta-objset */ 594503ad85cSMatthew Ahrens objset_t * 595c717a561Smaybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, 596c717a561Smaybee dmu_objset_type_t type, dmu_tx_t *tx) 597fa9e4066Sahrens { 598503ad85cSMatthew Ahrens objset_t *os; 599fa9e4066Sahrens dnode_t *mdn; 600fa9e4066Sahrens 601fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 602feaa74e4SMark Maybee if (ds != NULL) 603feaa74e4SMark Maybee VERIFY(0 == dmu_objset_from_ds(ds, &os)); 604feaa74e4SMark Maybee else 605feaa74e4SMark Maybee VERIFY(0 == dmu_objset_open_impl(spa, NULL, bp, &os)); 606feaa74e4SMark Maybee 607744947dcSTom Erickson mdn = DMU_META_DNODE(os); 608fa9e4066Sahrens 609fa9e4066Sahrens dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT, 610fa9e4066Sahrens DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx); 611fa9e4066Sahrens 612fa9e4066Sahrens /* 613fa9e4066Sahrens * We don't want to have to increase the meta-dnode's nlevels 614fa9e4066Sahrens * later, because then we could do it in quescing context while 615fa9e4066Sahrens * we are also accessing it in open context. 616fa9e4066Sahrens * 617fa9e4066Sahrens * This precaution is not necessary for the MOS (ds == NULL), 618fa9e4066Sahrens * because the MOS is only updated in syncing context. 619fa9e4066Sahrens * This is most fortunate: the MOS is the only objset that 620fa9e4066Sahrens * needs to be synced multiple times as spa_sync() iterates 621fa9e4066Sahrens * to convergence, so minimizing its dn_nlevels matters. 622fa9e4066Sahrens */ 623ea8dc4b6Seschrock if (ds != NULL) { 624ea8dc4b6Seschrock int levels = 1; 625ea8dc4b6Seschrock 626ea8dc4b6Seschrock /* 627ea8dc4b6Seschrock * Determine the number of levels necessary for the meta-dnode 628ea8dc4b6Seschrock * to contain DN_MAX_OBJECT dnodes. 629ea8dc4b6Seschrock */ 630ea8dc4b6Seschrock while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift + 631ea8dc4b6Seschrock (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) < 632ea8dc4b6Seschrock DN_MAX_OBJECT * sizeof (dnode_phys_t)) 633ea8dc4b6Seschrock levels++; 634ea8dc4b6Seschrock 635fa9e4066Sahrens mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] = 636ea8dc4b6Seschrock mdn->dn_nlevels = levels; 637ea8dc4b6Seschrock } 638fa9e4066Sahrens 639fa9e4066Sahrens ASSERT(type != DMU_OST_NONE); 640fa9e4066Sahrens ASSERT(type != DMU_OST_ANY); 641fa9e4066Sahrens ASSERT(type < DMU_OST_NUMTYPES); 642503ad85cSMatthew Ahrens os->os_phys->os_type = type; 643503ad85cSMatthew Ahrens if (dmu_objset_userused_enabled(os)) { 644503ad85cSMatthew Ahrens os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE; 645503ad85cSMatthew Ahrens os->os_flags = os->os_phys->os_flags; 64614843421SMatthew Ahrens } 647fa9e4066Sahrens 648fa9e4066Sahrens dsl_dataset_dirty(ds, tx); 649fa9e4066Sahrens 650503ad85cSMatthew Ahrens return (os); 651fa9e4066Sahrens } 652fa9e4066Sahrens 653fa9e4066Sahrens struct oscarg { 654ecd6cf80Smarks void (*userfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx); 655fa9e4066Sahrens void *userarg; 656ae46e4c7SMatthew Ahrens dsl_dataset_t *clone_origin; 657fa9e4066Sahrens const char *lastname; 658fa9e4066Sahrens dmu_objset_type_t type; 659ab04eb8eStimh uint64_t flags; 6603f9d6ad7SLin Ling cred_t *cr; 661fa9e4066Sahrens }; 662fa9e4066Sahrens 663ecd6cf80Smarks /*ARGSUSED*/ 664fa9e4066Sahrens static int 6651d452cf5Sahrens dmu_objset_create_check(void *arg1, void *arg2, dmu_tx_t *tx) 666fa9e4066Sahrens { 6671d452cf5Sahrens dsl_dir_t *dd = arg1; 6681d452cf5Sahrens struct oscarg *oa = arg2; 6691d452cf5Sahrens objset_t *mos = dd->dd_pool->dp_meta_objset; 670fa9e4066Sahrens int err; 6711d452cf5Sahrens uint64_t ddobj; 6721d452cf5Sahrens 6731d452cf5Sahrens err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj, 6741d452cf5Sahrens oa->lastname, sizeof (uint64_t), 1, &ddobj); 6751d452cf5Sahrens if (err != ENOENT) 6761d452cf5Sahrens return (err ? err : EEXIST); 6771d452cf5Sahrens 678ae46e4c7SMatthew Ahrens if (oa->clone_origin != NULL) { 679ae46e4c7SMatthew Ahrens /* You can't clone across pools. */ 680ae46e4c7SMatthew Ahrens if (oa->clone_origin->ds_dir->dd_pool != dd->dd_pool) 6811d452cf5Sahrens return (EXDEV); 6821d452cf5Sahrens 683ae46e4c7SMatthew Ahrens /* You can only clone snapshots, not the head datasets. */ 684ae46e4c7SMatthew Ahrens if (!dsl_dataset_is_snapshot(oa->clone_origin)) 6851d452cf5Sahrens return (EINVAL); 6861d452cf5Sahrens } 687ecd6cf80Smarks 6881d452cf5Sahrens return (0); 6891d452cf5Sahrens } 6901d452cf5Sahrens 6911d452cf5Sahrens static void 6923f9d6ad7SLin Ling dmu_objset_create_sync(void *arg1, void *arg2, dmu_tx_t *tx) 6931d452cf5Sahrens { 6941d452cf5Sahrens dsl_dir_t *dd = arg1; 695feaa74e4SMark Maybee spa_t *spa = dd->dd_pool->dp_spa; 6961d452cf5Sahrens struct oscarg *oa = arg2; 697feaa74e4SMark Maybee uint64_t obj; 6984445fffbSMatthew Ahrens dsl_dataset_t *ds; 6994445fffbSMatthew Ahrens blkptr_t *bp; 700fa9e4066Sahrens 701fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 702fa9e4066Sahrens 703feaa74e4SMark Maybee obj = dsl_dataset_create_sync(dd, oa->lastname, 7043f9d6ad7SLin Ling oa->clone_origin, oa->flags, oa->cr, tx); 705fa9e4066Sahrens 706b420f3adSRichard Lowe VERIFY3U(0, ==, dsl_dataset_hold_obj(dd->dd_pool, obj, FTAG, &ds)); 7074445fffbSMatthew Ahrens bp = dsl_dataset_get_blkptr(ds); 7084445fffbSMatthew Ahrens if (BP_IS_HOLE(bp)) { 7094445fffbSMatthew Ahrens objset_t *os = 7104445fffbSMatthew Ahrens dmu_objset_create_impl(spa, ds, bp, oa->type, tx); 711fa9e4066Sahrens 712fa9e4066Sahrens if (oa->userfunc) 7133f9d6ad7SLin Ling oa->userfunc(os, oa->userarg, oa->cr, tx); 714fa9e4066Sahrens } 715ecd6cf80Smarks 7164445fffbSMatthew Ahrens if (oa->clone_origin == NULL) { 7174445fffbSMatthew Ahrens spa_history_log_internal_ds(ds, "create", tx, ""); 7184445fffbSMatthew Ahrens } else { 7194445fffbSMatthew Ahrens char namebuf[MAXNAMELEN]; 7204445fffbSMatthew Ahrens dsl_dataset_name(oa->clone_origin, namebuf); 7214445fffbSMatthew Ahrens spa_history_log_internal_ds(ds, "clone", tx, 7224445fffbSMatthew Ahrens "origin=%s (%llu)", namebuf, oa->clone_origin->ds_object); 7234445fffbSMatthew Ahrens } 7244445fffbSMatthew Ahrens dsl_dataset_rele(ds, FTAG); 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 792503ad85cSMatthew Ahrens error = dsl_dataset_own(name, B_TRUE, FTAG, &ds); 793fa9e4066Sahrens if (error == 0) { 794503ad85cSMatthew Ahrens error = dsl_dataset_destroy(ds, FTAG, defer); 795ae46e4c7SMatthew Ahrens /* dsl_dataset_destroy() closes the ds. */ 796fa9e4066Sahrens } 797fa9e4066Sahrens 7983cb34c60Sahrens return (error); 799fa9e4066Sahrens } 800fa9e4066Sahrens 8014445fffbSMatthew Ahrens typedef struct snapallarg { 8024445fffbSMatthew Ahrens dsl_sync_task_group_t *saa_dstg; 8034445fffbSMatthew Ahrens boolean_t saa_needsuspend; 8044445fffbSMatthew Ahrens nvlist_t *saa_props; 8054445fffbSMatthew Ahrens 8064445fffbSMatthew Ahrens /* the following are used only if 'temporary' is set: */ 8074445fffbSMatthew Ahrens boolean_t saa_temporary; 8084445fffbSMatthew Ahrens const char *saa_htag; 8094445fffbSMatthew Ahrens struct dsl_ds_holdarg *saa_ha; 8104445fffbSMatthew Ahrens dsl_dataset_t *saa_newds; 8114445fffbSMatthew Ahrens } snapallarg_t; 8124445fffbSMatthew Ahrens 8134445fffbSMatthew Ahrens typedef struct snaponearg { 8144445fffbSMatthew Ahrens const char *soa_longname; /* long snap name */ 8154445fffbSMatthew Ahrens const char *soa_snapname; /* short snap name */ 8164445fffbSMatthew Ahrens snapallarg_t *soa_saa; 8174445fffbSMatthew Ahrens } snaponearg_t; 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; 8234445fffbSMatthew Ahrens snaponearg_t *soa = arg2; 8244445fffbSMatthew Ahrens snapallarg_t *saa = soa->soa_saa; 82599d5e173STim Haley int error; 826ea2f5b9eSMatthew Ahrens 827ea2f5b9eSMatthew Ahrens /* The props have already been checked by zfs_check_userprops(). */ 828ea2f5b9eSMatthew Ahrens 82999d5e173STim Haley error = dsl_dataset_snapshot_check(os->os_dsl_dataset, 8304445fffbSMatthew Ahrens soa->soa_snapname, tx); 83199d5e173STim Haley if (error) 83299d5e173STim Haley return (error); 83399d5e173STim Haley 8344445fffbSMatthew Ahrens if (saa->saa_temporary) { 83599d5e173STim Haley /* 83699d5e173STim Haley * Ideally we would just call 83799d5e173STim Haley * dsl_dataset_user_hold_check() and 83899d5e173STim Haley * dsl_dataset_destroy_check() here. However the 83999d5e173STim Haley * dataset we want to hold and destroy is the snapshot 84099d5e173STim Haley * that we just confirmed we can create, but it won't 84199d5e173STim Haley * exist until after these checks are run. Do any 84299d5e173STim Haley * checks we can here and if more checks are added to 84399d5e173STim Haley * those routines in the future, similar checks may be 84499d5e173STim Haley * necessary here. 84599d5e173STim Haley */ 84699d5e173STim Haley if (spa_version(os->os_spa) < SPA_VERSION_USERREFS) 84799d5e173STim Haley return (ENOTSUP); 84899d5e173STim Haley /* 84999d5e173STim Haley * Not checking number of tags because the tag will be 85099d5e173STim Haley * unique, as it will be the only tag. 85199d5e173STim Haley */ 8524445fffbSMatthew Ahrens if (strlen(saa->saa_htag) + MAX_TAG_PREFIX_LEN >= MAXNAMELEN) 85399d5e173STim Haley return (E2BIG); 85499d5e173STim Haley 8554445fffbSMatthew Ahrens saa->saa_ha = kmem_alloc(sizeof (struct dsl_ds_holdarg), 8564445fffbSMatthew Ahrens KM_SLEEP); 8574445fffbSMatthew Ahrens saa->saa_ha->temphold = B_TRUE; 8584445fffbSMatthew Ahrens saa->saa_ha->htag = saa->saa_htag; 85999d5e173STim Haley } 86099d5e173STim Haley return (error); 861ea2f5b9eSMatthew Ahrens } 862ea2f5b9eSMatthew Ahrens 863ea2f5b9eSMatthew Ahrens static void 8643f9d6ad7SLin Ling snapshot_sync(void *arg1, void *arg2, dmu_tx_t *tx) 865ea2f5b9eSMatthew Ahrens { 866ea2f5b9eSMatthew Ahrens objset_t *os = arg1; 867503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 8684445fffbSMatthew Ahrens snaponearg_t *soa = arg2; 8694445fffbSMatthew Ahrens snapallarg_t *saa = soa->soa_saa; 870ea2f5b9eSMatthew Ahrens 8714445fffbSMatthew Ahrens dsl_dataset_snapshot_sync(ds, soa->soa_snapname, tx); 872ea2f5b9eSMatthew Ahrens 8734445fffbSMatthew Ahrens if (saa->saa_props != NULL) { 87492241e0bSTom Erickson dsl_props_arg_t pa; 8754445fffbSMatthew Ahrens pa.pa_props = saa->saa_props; 87692241e0bSTom Erickson pa.pa_source = ZPROP_SRC_LOCAL; 8773f9d6ad7SLin Ling dsl_props_set_sync(ds->ds_prev, &pa, tx); 87892241e0bSTom Erickson } 87999d5e173STim Haley 8804445fffbSMatthew Ahrens if (saa->saa_temporary) { 88199d5e173STim Haley struct dsl_ds_destroyarg da; 88299d5e173STim Haley 8834445fffbSMatthew Ahrens dsl_dataset_user_hold_sync(ds->ds_prev, saa->saa_ha, tx); 8844445fffbSMatthew Ahrens kmem_free(saa->saa_ha, sizeof (struct dsl_ds_holdarg)); 8854445fffbSMatthew Ahrens saa->saa_ha = NULL; 8864445fffbSMatthew Ahrens saa->saa_newds = ds->ds_prev; 88799d5e173STim Haley 88899d5e173STim Haley da.ds = ds->ds_prev; 88999d5e173STim Haley da.defer = B_TRUE; 89099d5e173STim Haley dsl_dataset_destroy_sync(&da, FTAG, tx); 89199d5e173STim Haley } 892ea2f5b9eSMatthew Ahrens } 8931d452cf5Sahrens 8941d452cf5Sahrens static int 8954445fffbSMatthew Ahrens snapshot_one_impl(const char *snapname, void *arg) 8961d452cf5Sahrens { 8974445fffbSMatthew Ahrens char fsname[MAXPATHLEN]; 8984445fffbSMatthew Ahrens snapallarg_t *saa = arg; 8994445fffbSMatthew Ahrens snaponearg_t *soa; 9001d452cf5Sahrens objset_t *os; 9011d452cf5Sahrens int err; 9021d452cf5Sahrens 9034445fffbSMatthew Ahrens (void) strlcpy(fsname, snapname, sizeof (fsname)); 9044445fffbSMatthew Ahrens strchr(fsname, '@')[0] = '\0'; 905ecd6cf80Smarks 9064445fffbSMatthew Ahrens err = dmu_objset_hold(fsname, saa, &os); 9071d452cf5Sahrens if (err != 0) 9081d452cf5Sahrens return (err); 9091d452cf5Sahrens 910d8d780fbSMatthew Ahrens /* 911d8d780fbSMatthew Ahrens * If the objset is in an inconsistent state (eg, in the process 9124445fffbSMatthew Ahrens * of being destroyed), don't snapshot it. 913d8d780fbSMatthew Ahrens */ 914503ad85cSMatthew Ahrens if (os->os_dsl_dataset->ds_phys->ds_flags & DS_FLAG_INCONSISTENT) { 9154445fffbSMatthew Ahrens dmu_objset_rele(os, saa); 9164445fffbSMatthew Ahrens return (EBUSY); 917f2e10be3Srm } 918f2e10be3Srm 9194445fffbSMatthew Ahrens if (saa->saa_needsuspend) { 9206e0cbcaaSMatthew Ahrens err = zil_suspend(dmu_objset_zil(os)); 9216e0cbcaaSMatthew Ahrens if (err) { 9224445fffbSMatthew Ahrens dmu_objset_rele(os, saa); 9236e0cbcaaSMatthew Ahrens return (err); 9246e0cbcaaSMatthew Ahrens } 9251d452cf5Sahrens } 9264445fffbSMatthew Ahrens 9274445fffbSMatthew Ahrens soa = kmem_zalloc(sizeof (*soa), KM_SLEEP); 9284445fffbSMatthew Ahrens soa->soa_saa = saa; 9294445fffbSMatthew Ahrens soa->soa_longname = snapname; 9304445fffbSMatthew Ahrens soa->soa_snapname = strchr(snapname, '@') + 1; 9314445fffbSMatthew Ahrens 9324445fffbSMatthew Ahrens dsl_sync_task_create(saa->saa_dstg, snapshot_check, snapshot_sync, 9334445fffbSMatthew Ahrens os, soa, 3); 934f2e10be3Srm 9356e0cbcaaSMatthew Ahrens return (0); 9361d452cf5Sahrens } 9371d452cf5Sahrens 9384445fffbSMatthew Ahrens /* 9394445fffbSMatthew Ahrens * The snapshots must all be in the same pool. 9404445fffbSMatthew Ahrens */ 9411d452cf5Sahrens int 9424445fffbSMatthew Ahrens dmu_objset_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t *errors) 9431d452cf5Sahrens { 9441d452cf5Sahrens dsl_sync_task_t *dst; 9454445fffbSMatthew Ahrens snapallarg_t saa = { 0 }; 9461d452cf5Sahrens spa_t *spa; 9474445fffbSMatthew Ahrens int rv = 0; 9481d452cf5Sahrens int err; 9494445fffbSMatthew Ahrens nvpair_t *pair; 9501d452cf5Sahrens 9514445fffbSMatthew Ahrens pair = nvlist_next_nvpair(snaps, NULL); 9524445fffbSMatthew Ahrens if (pair == NULL) 9534445fffbSMatthew Ahrens return (0); 9541d452cf5Sahrens 9554445fffbSMatthew Ahrens err = spa_open(nvpair_name(pair), &spa, FTAG); 9561d452cf5Sahrens if (err) 9571d452cf5Sahrens return (err); 9584445fffbSMatthew Ahrens saa.saa_dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 9594445fffbSMatthew Ahrens saa.saa_props = props; 9604445fffbSMatthew Ahrens saa.saa_needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP); 9614445fffbSMatthew Ahrens 9624445fffbSMatthew Ahrens for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL; 9634445fffbSMatthew Ahrens pair = nvlist_next_nvpair(snaps, pair)) { 9644445fffbSMatthew Ahrens err = snapshot_one_impl(nvpair_name(pair), &saa); 9654445fffbSMatthew Ahrens if (err != 0) { 9664445fffbSMatthew Ahrens if (errors != NULL) { 9674445fffbSMatthew Ahrens fnvlist_add_int32(errors, 9684445fffbSMatthew Ahrens nvpair_name(pair), err); 9694445fffbSMatthew Ahrens } 9704445fffbSMatthew Ahrens rv = err; 97199d5e173STim Haley } 9724445fffbSMatthew Ahrens } 9734445fffbSMatthew Ahrens 9744445fffbSMatthew Ahrens /* 9754445fffbSMatthew Ahrens * If any call to snapshot_one_impl() failed, don't execute the 9764445fffbSMatthew Ahrens * sync task. The error handling code below will clean up the 9774445fffbSMatthew Ahrens * snaponearg_t from any successful calls to 9784445fffbSMatthew Ahrens * snapshot_one_impl(). 9794445fffbSMatthew Ahrens */ 9804445fffbSMatthew Ahrens if (rv == 0) 9814445fffbSMatthew Ahrens err = dsl_sync_task_group_wait(saa.saa_dstg); 9824445fffbSMatthew Ahrens if (err != 0) 9834445fffbSMatthew Ahrens rv = err; 9844445fffbSMatthew Ahrens 9854445fffbSMatthew Ahrens for (dst = list_head(&saa.saa_dstg->dstg_tasks); dst; 9864445fffbSMatthew Ahrens dst = list_next(&saa.saa_dstg->dstg_tasks, dst)) { 9874445fffbSMatthew Ahrens objset_t *os = dst->dst_arg1; 9884445fffbSMatthew Ahrens snaponearg_t *soa = dst->dst_arg2; 9894445fffbSMatthew Ahrens if (dst->dst_err != 0) { 9904445fffbSMatthew Ahrens if (errors != NULL) { 9914445fffbSMatthew Ahrens fnvlist_add_int32(errors, 9924445fffbSMatthew Ahrens soa->soa_longname, dst->dst_err); 9934445fffbSMatthew Ahrens } 9944445fffbSMatthew Ahrens rv = dst->dst_err; 99599d5e173STim Haley } 9964445fffbSMatthew Ahrens 9974445fffbSMatthew Ahrens if (saa.saa_needsuspend) 9984445fffbSMatthew Ahrens zil_resume(dmu_objset_zil(os)); 9994445fffbSMatthew Ahrens dmu_objset_rele(os, &saa); 10004445fffbSMatthew Ahrens kmem_free(soa, sizeof (*soa)); 100199d5e173STim Haley } 100299d5e173STim Haley 10034445fffbSMatthew Ahrens dsl_sync_task_group_destroy(saa.saa_dstg); 10044445fffbSMatthew Ahrens spa_close(spa, FTAG); 10054445fffbSMatthew Ahrens return (rv); 10064445fffbSMatthew Ahrens } 10074445fffbSMatthew Ahrens 10084445fffbSMatthew Ahrens int 10094445fffbSMatthew Ahrens dmu_objset_snapshot_one(const char *fsname, const char *snapname) 10104445fffbSMatthew Ahrens { 10114445fffbSMatthew Ahrens int err; 10124445fffbSMatthew Ahrens char *longsnap = kmem_asprintf("%s@%s", fsname, snapname); 10134445fffbSMatthew Ahrens nvlist_t *snaps = fnvlist_alloc(); 10144445fffbSMatthew Ahrens 10154445fffbSMatthew Ahrens fnvlist_add_boolean(snaps, longsnap); 10164445fffbSMatthew Ahrens err = dmu_objset_snapshot(snaps, NULL, NULL); 10174445fffbSMatthew Ahrens fnvlist_free(snaps); 10184445fffbSMatthew Ahrens strfree(longsnap); 10194445fffbSMatthew Ahrens return (err); 10204445fffbSMatthew Ahrens } 10214445fffbSMatthew Ahrens 10224445fffbSMatthew Ahrens int 10234445fffbSMatthew Ahrens dmu_objset_snapshot_tmp(const char *snapname, const char *tag, int cleanup_fd) 10244445fffbSMatthew Ahrens { 10254445fffbSMatthew Ahrens dsl_sync_task_t *dst; 10264445fffbSMatthew Ahrens snapallarg_t saa = { 0 }; 10274445fffbSMatthew Ahrens spa_t *spa; 10284445fffbSMatthew Ahrens minor_t minor; 10294445fffbSMatthew Ahrens int err; 10304445fffbSMatthew Ahrens 10314445fffbSMatthew Ahrens err = spa_open(snapname, &spa, FTAG); 10324445fffbSMatthew Ahrens if (err) 10334445fffbSMatthew Ahrens return (err); 10344445fffbSMatthew Ahrens saa.saa_dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 10354445fffbSMatthew Ahrens saa.saa_htag = tag; 10364445fffbSMatthew Ahrens saa.saa_needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP); 10374445fffbSMatthew Ahrens saa.saa_temporary = B_TRUE; 10384445fffbSMatthew Ahrens 10394445fffbSMatthew Ahrens if (cleanup_fd < 0) { 10404445fffbSMatthew Ahrens spa_close(spa, FTAG); 10414445fffbSMatthew Ahrens return (EINVAL); 10424445fffbSMatthew Ahrens } 10434445fffbSMatthew Ahrens if ((err = zfs_onexit_fd_hold(cleanup_fd, &minor)) != 0) { 10444445fffbSMatthew Ahrens spa_close(spa, FTAG); 10454445fffbSMatthew Ahrens return (err); 10460b69c2f0Sahrens } 10471d452cf5Sahrens 10484445fffbSMatthew Ahrens err = snapshot_one_impl(snapname, &saa); 10494445fffbSMatthew Ahrens 1050ea2f5b9eSMatthew Ahrens if (err == 0) 10514445fffbSMatthew Ahrens err = dsl_sync_task_group_wait(saa.saa_dstg); 10521d452cf5Sahrens 10534445fffbSMatthew Ahrens for (dst = list_head(&saa.saa_dstg->dstg_tasks); dst; 10544445fffbSMatthew Ahrens dst = list_next(&saa.saa_dstg->dstg_tasks, dst)) { 1055ea2f5b9eSMatthew Ahrens objset_t *os = dst->dst_arg1; 10564445fffbSMatthew Ahrens dsl_register_onexit_hold_cleanup(saa.saa_newds, tag, minor); 10574445fffbSMatthew Ahrens if (saa.saa_needsuspend) 10586e0cbcaaSMatthew Ahrens zil_resume(dmu_objset_zil(os)); 10594445fffbSMatthew Ahrens dmu_objset_rele(os, &saa); 10603cb34c60Sahrens } 10613cb34c60Sahrens 10624445fffbSMatthew Ahrens zfs_onexit_fd_rele(cleanup_fd); 10634445fffbSMatthew Ahrens dsl_sync_task_group_destroy(saa.saa_dstg); 10641d452cf5Sahrens spa_close(spa, FTAG); 10651d452cf5Sahrens return (err); 10661d452cf5Sahrens } 10671d452cf5Sahrens 10684445fffbSMatthew Ahrens 1069fa9e4066Sahrens static void 107014843421SMatthew Ahrens dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx) 1071fa9e4066Sahrens { 1072c717a561Smaybee dnode_t *dn; 1073faafa6e3Sahrens 1074c717a561Smaybee while (dn = list_head(list)) { 1075c717a561Smaybee ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT); 1076c717a561Smaybee ASSERT(dn->dn_dbuf->db_data_pending); 1077c717a561Smaybee /* 107814843421SMatthew Ahrens * Initialize dn_zio outside dnode_sync() because the 107914843421SMatthew Ahrens * meta-dnode needs to set it ouside dnode_sync(). 1080c717a561Smaybee */ 1081c717a561Smaybee dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio; 1082c717a561Smaybee ASSERT(dn->dn_zio); 1083faafa6e3Sahrens 1084c717a561Smaybee ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS); 1085c717a561Smaybee list_remove(list, dn); 108614843421SMatthew Ahrens 108714843421SMatthew Ahrens if (newlist) { 108814843421SMatthew Ahrens (void) dnode_add_ref(dn, newlist); 108914843421SMatthew Ahrens list_insert_tail(newlist, dn); 109014843421SMatthew Ahrens } 109114843421SMatthew Ahrens 1092c717a561Smaybee dnode_sync(dn, tx); 1093fa9e4066Sahrens } 1094fa9e4066Sahrens } 1095fa9e4066Sahrens 1096fa9e4066Sahrens /* ARGSUSED */ 1097fa9e4066Sahrens static void 1098b24ab676SJeff Bonwick dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg) 1099fa9e4066Sahrens { 1100e14bb325SJeff Bonwick blkptr_t *bp = zio->io_bp; 1101503ad85cSMatthew Ahrens objset_t *os = arg; 1102c717a561Smaybee dnode_phys_t *dnp = &os->os_phys->os_meta_dnode; 1103fa9e4066Sahrens 1104e14bb325SJeff Bonwick ASSERT(bp == os->os_rootbp); 1105e14bb325SJeff Bonwick ASSERT(BP_GET_TYPE(bp) == DMU_OT_OBJSET); 1106e14bb325SJeff Bonwick ASSERT(BP_GET_LEVEL(bp) == 0); 11070a4e9518Sgw 1108fa9e4066Sahrens /* 110914843421SMatthew Ahrens * Update rootbp fill count: it should be the number of objects 111014843421SMatthew Ahrens * allocated in the object set (not counting the "special" 111114843421SMatthew Ahrens * objects that are stored in the objset_phys_t -- the meta 111214843421SMatthew Ahrens * dnode and user/group accounting objects). 1113fa9e4066Sahrens */ 111414843421SMatthew Ahrens bp->blk_fill = 0; 1115e14bb325SJeff Bonwick for (int i = 0; i < dnp->dn_nblkptr; i++) 1116c717a561Smaybee bp->blk_fill += dnp->dn_blkptr[i].blk_fill; 1117b24ab676SJeff Bonwick } 1118b24ab676SJeff Bonwick 1119b24ab676SJeff Bonwick /* ARGSUSED */ 1120b24ab676SJeff Bonwick static void 1121b24ab676SJeff Bonwick dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg) 1122b24ab676SJeff Bonwick { 1123b24ab676SJeff Bonwick blkptr_t *bp = zio->io_bp; 1124b24ab676SJeff Bonwick blkptr_t *bp_orig = &zio->io_bp_orig; 1125b24ab676SJeff Bonwick objset_t *os = arg; 11260a4e9518Sgw 1127e14bb325SJeff Bonwick if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { 1128b24ab676SJeff Bonwick ASSERT(BP_EQUAL(bp, bp_orig)); 1129e14bb325SJeff Bonwick } else { 1130b24ab676SJeff Bonwick dsl_dataset_t *ds = os->os_dsl_dataset; 1131b24ab676SJeff Bonwick dmu_tx_t *tx = os->os_synctx; 1132b24ab676SJeff Bonwick 1133b24ab676SJeff Bonwick (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE); 1134b24ab676SJeff Bonwick dsl_dataset_block_born(ds, bp, tx); 11350a4e9518Sgw } 1136c717a561Smaybee } 1137c717a561Smaybee 1138fa9e4066Sahrens /* called from dsl */ 1139fa9e4066Sahrens void 1140503ad85cSMatthew Ahrens dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx) 1141fa9e4066Sahrens { 1142fa9e4066Sahrens int txgoff; 1143ea8dc4b6Seschrock zbookmark_t zb; 1144b24ab676SJeff Bonwick zio_prop_t zp; 1145c717a561Smaybee zio_t *zio; 1146c717a561Smaybee list_t *list; 114714843421SMatthew Ahrens list_t *newlist = NULL; 1148c717a561Smaybee dbuf_dirty_record_t *dr; 1149c717a561Smaybee 1150c717a561Smaybee dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg); 1151fa9e4066Sahrens 1152fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 1153fa9e4066Sahrens /* XXX the write_done callback should really give us the tx... */ 1154fa9e4066Sahrens os->os_synctx = tx; 1155fa9e4066Sahrens 115687bd5c1eSahrens if (os->os_dsl_dataset == NULL) { 115787bd5c1eSahrens /* 115887bd5c1eSahrens * This is the MOS. If we have upgraded, 115987bd5c1eSahrens * spa_max_replication() could change, so reset 116087bd5c1eSahrens * os_copies here. 116187bd5c1eSahrens */ 116287bd5c1eSahrens os->os_copies = spa_max_replication(os->os_spa); 116387bd5c1eSahrens } 116487bd5c1eSahrens 1165fa9e4066Sahrens /* 1166c717a561Smaybee * Create the root block IO 1167fa9e4066Sahrens */ 1168b24ab676SJeff Bonwick SET_BOOKMARK(&zb, os->os_dsl_dataset ? 1169b24ab676SJeff Bonwick os->os_dsl_dataset->ds_object : DMU_META_OBJSET, 1170b24ab676SJeff Bonwick ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 1171*1b912ec7SGeorge Wilson arc_release(os->os_phys_buf, &os->os_phys_buf); 1172b24ab676SJeff Bonwick 1173b24ab676SJeff Bonwick dmu_write_policy(os, NULL, 0, 0, &zp); 1174b24ab676SJeff Bonwick 1175b24ab676SJeff Bonwick zio = arc_write(pio, os->os_spa, tx->tx_txg, 1176b24ab676SJeff Bonwick os->os_rootbp, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os), &zp, 1177b24ab676SJeff Bonwick dmu_objset_write_ready, dmu_objset_write_done, os, 1178e14bb325SJeff Bonwick ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb); 1179c717a561Smaybee 1180c717a561Smaybee /* 118114843421SMatthew Ahrens * Sync special dnodes - the parent IO for the sync is the root block 1182c717a561Smaybee */ 1183744947dcSTom Erickson DMU_META_DNODE(os)->dn_zio = zio; 1184744947dcSTom Erickson dnode_sync(DMU_META_DNODE(os), tx); 1185c717a561Smaybee 118614843421SMatthew Ahrens os->os_phys->os_flags = os->os_flags; 118714843421SMatthew Ahrens 1188744947dcSTom Erickson if (DMU_USERUSED_DNODE(os) && 1189744947dcSTom Erickson DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) { 1190744947dcSTom Erickson DMU_USERUSED_DNODE(os)->dn_zio = zio; 1191744947dcSTom Erickson dnode_sync(DMU_USERUSED_DNODE(os), tx); 1192744947dcSTom Erickson DMU_GROUPUSED_DNODE(os)->dn_zio = zio; 1193744947dcSTom Erickson dnode_sync(DMU_GROUPUSED_DNODE(os), tx); 119414843421SMatthew Ahrens } 119514843421SMatthew Ahrens 1196c717a561Smaybee txgoff = tx->tx_txg & TXG_MASK; 1197fa9e4066Sahrens 119814843421SMatthew Ahrens if (dmu_objset_userused_enabled(os)) { 119914843421SMatthew Ahrens newlist = &os->os_synced_dnodes; 120014843421SMatthew Ahrens /* 120114843421SMatthew Ahrens * We must create the list here because it uses the 120214843421SMatthew Ahrens * dn_dirty_link[] of this txg. 120314843421SMatthew Ahrens */ 120414843421SMatthew Ahrens list_create(newlist, sizeof (dnode_t), 120514843421SMatthew Ahrens offsetof(dnode_t, dn_dirty_link[txgoff])); 120614843421SMatthew Ahrens } 120714843421SMatthew Ahrens 120814843421SMatthew Ahrens dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx); 120914843421SMatthew Ahrens dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx); 1210fa9e4066Sahrens 1211744947dcSTom Erickson list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff]; 1212c717a561Smaybee while (dr = list_head(list)) { 1213c717a561Smaybee ASSERT(dr->dr_dbuf->db_level == 0); 1214c717a561Smaybee list_remove(list, dr); 1215c717a561Smaybee if (dr->dr_zio) 1216c717a561Smaybee zio_nowait(dr->dr_zio); 1217c717a561Smaybee } 1218c717a561Smaybee /* 1219c717a561Smaybee * Free intent log blocks up to this tx. 1220c717a561Smaybee */ 1221c717a561Smaybee zil_sync(os->os_zil, tx); 1222088f3894Sahrens os->os_phys->os_zil_header = os->os_zil_header; 1223c717a561Smaybee zio_nowait(zio); 1224fa9e4066Sahrens } 1225fa9e4066Sahrens 1226b24ab676SJeff Bonwick boolean_t 1227b24ab676SJeff Bonwick dmu_objset_is_dirty(objset_t *os, uint64_t txg) 1228b24ab676SJeff Bonwick { 1229b24ab676SJeff Bonwick return (!list_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]) || 1230b24ab676SJeff Bonwick !list_is_empty(&os->os_free_dnodes[txg & TXG_MASK])); 1231b24ab676SJeff Bonwick } 1232b24ab676SJeff Bonwick 1233744947dcSTom Erickson static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES]; 123414843421SMatthew Ahrens 123514843421SMatthew Ahrens void 123614843421SMatthew Ahrens dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb) 123714843421SMatthew Ahrens { 123814843421SMatthew Ahrens used_cbs[ost] = cb; 123914843421SMatthew Ahrens } 124014843421SMatthew Ahrens 124114843421SMatthew Ahrens boolean_t 1242503ad85cSMatthew Ahrens dmu_objset_userused_enabled(objset_t *os) 124314843421SMatthew Ahrens { 124414843421SMatthew Ahrens return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE && 1245744947dcSTom Erickson used_cbs[os->os_phys->os_type] != NULL && 1246744947dcSTom Erickson DMU_USERUSED_DNODE(os) != NULL); 124714843421SMatthew Ahrens } 124814843421SMatthew Ahrens 12499966ca11SMatthew Ahrens static void 12500a586ceaSMark Shellenbaum do_userquota_update(objset_t *os, uint64_t used, uint64_t flags, 12510a586ceaSMark Shellenbaum uint64_t user, uint64_t group, boolean_t subtract, dmu_tx_t *tx) 12529966ca11SMatthew Ahrens { 12530a586ceaSMark Shellenbaum if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) { 12540a586ceaSMark Shellenbaum int64_t delta = DNODE_SIZE + used; 12559966ca11SMatthew Ahrens if (subtract) 12569966ca11SMatthew Ahrens delta = -delta; 1257b420f3adSRichard Lowe VERIFY3U(0, ==, zap_increment_int(os, DMU_USERUSED_OBJECT, 12589966ca11SMatthew Ahrens user, delta, tx)); 1259b420f3adSRichard Lowe VERIFY3U(0, ==, zap_increment_int(os, DMU_GROUPUSED_OBJECT, 12609966ca11SMatthew Ahrens group, delta, tx)); 12619966ca11SMatthew Ahrens } 12629966ca11SMatthew Ahrens } 12639966ca11SMatthew Ahrens 126414843421SMatthew Ahrens void 12650a586ceaSMark Shellenbaum dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx) 126614843421SMatthew Ahrens { 126714843421SMatthew Ahrens dnode_t *dn; 126814843421SMatthew Ahrens list_t *list = &os->os_synced_dnodes; 126914843421SMatthew Ahrens 127014843421SMatthew Ahrens ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os)); 127114843421SMatthew Ahrens 127214843421SMatthew Ahrens while (dn = list_head(list)) { 12731d8ccc7bSMark Shellenbaum int flags; 127414843421SMatthew Ahrens ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object)); 127514843421SMatthew Ahrens ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE || 127614843421SMatthew Ahrens dn->dn_phys->dn_flags & 127714843421SMatthew Ahrens DNODE_FLAG_USERUSED_ACCOUNTED); 127814843421SMatthew Ahrens 127914843421SMatthew Ahrens /* Allocate the user/groupused objects if necessary. */ 1280744947dcSTom Erickson if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) { 1281503ad85cSMatthew Ahrens VERIFY(0 == zap_create_claim(os, 128214843421SMatthew Ahrens DMU_USERUSED_OBJECT, 128314843421SMatthew Ahrens DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx)); 1284503ad85cSMatthew Ahrens VERIFY(0 == zap_create_claim(os, 128514843421SMatthew Ahrens DMU_GROUPUSED_OBJECT, 128614843421SMatthew Ahrens DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx)); 128714843421SMatthew Ahrens } 128814843421SMatthew Ahrens 128914843421SMatthew Ahrens /* 12909966ca11SMatthew Ahrens * We intentionally modify the zap object even if the 12910a586ceaSMark Shellenbaum * net delta is zero. Otherwise 12929966ca11SMatthew Ahrens * the block of the zap obj could be shared between 12939966ca11SMatthew Ahrens * datasets but need to be different between them after 12949966ca11SMatthew Ahrens * a bprewrite. 129514843421SMatthew Ahrens */ 129614843421SMatthew Ahrens 12971d8ccc7bSMark Shellenbaum flags = dn->dn_id_flags; 12981d8ccc7bSMark Shellenbaum ASSERT(flags); 12991d8ccc7bSMark Shellenbaum if (flags & DN_ID_OLD_EXIST) { 13000a586ceaSMark Shellenbaum do_userquota_update(os, dn->dn_oldused, dn->dn_oldflags, 13010a586ceaSMark Shellenbaum dn->dn_olduid, dn->dn_oldgid, B_TRUE, tx); 13020a586ceaSMark Shellenbaum } 13031d8ccc7bSMark Shellenbaum if (flags & DN_ID_NEW_EXIST) { 13040a586ceaSMark Shellenbaum do_userquota_update(os, DN_USED_BYTES(dn->dn_phys), 13050a586ceaSMark Shellenbaum dn->dn_phys->dn_flags, dn->dn_newuid, 13060a586ceaSMark Shellenbaum dn->dn_newgid, B_FALSE, tx); 13070a586ceaSMark Shellenbaum } 13080a586ceaSMark Shellenbaum 13091d8ccc7bSMark Shellenbaum mutex_enter(&dn->dn_mtx); 13100a586ceaSMark Shellenbaum dn->dn_oldused = 0; 13110a586ceaSMark Shellenbaum dn->dn_oldflags = 0; 13120a586ceaSMark Shellenbaum if (dn->dn_id_flags & DN_ID_NEW_EXIST) { 13130a586ceaSMark Shellenbaum dn->dn_olduid = dn->dn_newuid; 13140a586ceaSMark Shellenbaum dn->dn_oldgid = dn->dn_newgid; 13150a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_OLD_EXIST; 13160a586ceaSMark Shellenbaum if (dn->dn_bonuslen == 0) 13170a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_SPILL; 13180a586ceaSMark Shellenbaum else 13190a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_BONUS; 13200a586ceaSMark Shellenbaum } 132128d97a71SMark Shellenbaum dn->dn_id_flags &= ~(DN_ID_NEW_EXIST); 132214843421SMatthew Ahrens mutex_exit(&dn->dn_mtx); 132314843421SMatthew Ahrens 132414843421SMatthew Ahrens list_remove(list, dn); 132514843421SMatthew Ahrens dnode_rele(dn, list); 132614843421SMatthew Ahrens } 132714843421SMatthew Ahrens } 132814843421SMatthew Ahrens 132906e0070dSMark Shellenbaum /* 133006e0070dSMark Shellenbaum * Returns a pointer to data to find uid/gid from 133106e0070dSMark Shellenbaum * 133206e0070dSMark Shellenbaum * If a dirty record for transaction group that is syncing can't 133306e0070dSMark Shellenbaum * be found then NULL is returned. In the NULL case it is assumed 133406e0070dSMark Shellenbaum * the uid/gid aren't changing. 133506e0070dSMark Shellenbaum */ 133606e0070dSMark Shellenbaum static void * 133706e0070dSMark Shellenbaum dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx) 133806e0070dSMark Shellenbaum { 133906e0070dSMark Shellenbaum dbuf_dirty_record_t *dr, **drp; 134006e0070dSMark Shellenbaum void *data; 134106e0070dSMark Shellenbaum 134206e0070dSMark Shellenbaum if (db->db_dirtycnt == 0) 134306e0070dSMark Shellenbaum return (db->db.db_data); /* Nothing is changing */ 134406e0070dSMark Shellenbaum 134506e0070dSMark Shellenbaum for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next) 134606e0070dSMark Shellenbaum if (dr->dr_txg == tx->tx_txg) 134706e0070dSMark Shellenbaum break; 134806e0070dSMark Shellenbaum 1349744947dcSTom Erickson if (dr == NULL) { 135006e0070dSMark Shellenbaum data = NULL; 1351744947dcSTom Erickson } else { 1352744947dcSTom Erickson dnode_t *dn; 1353744947dcSTom Erickson 1354744947dcSTom Erickson DB_DNODE_ENTER(dr->dr_dbuf); 1355744947dcSTom Erickson dn = DB_DNODE(dr->dr_dbuf); 1356744947dcSTom Erickson 1357744947dcSTom Erickson if (dn->dn_bonuslen == 0 && 1358744947dcSTom Erickson dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID) 1359744947dcSTom Erickson data = dr->dt.dl.dr_data->b_data; 1360744947dcSTom Erickson else 1361744947dcSTom Erickson data = dr->dt.dl.dr_data; 1362744947dcSTom Erickson 1363744947dcSTom Erickson DB_DNODE_EXIT(dr->dr_dbuf); 1364744947dcSTom Erickson } 1365744947dcSTom Erickson 136606e0070dSMark Shellenbaum return (data); 136706e0070dSMark Shellenbaum } 136806e0070dSMark Shellenbaum 13690a586ceaSMark Shellenbaum void 137006e0070dSMark Shellenbaum dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx) 13710a586ceaSMark Shellenbaum { 13720a586ceaSMark Shellenbaum objset_t *os = dn->dn_objset; 13730a586ceaSMark Shellenbaum void *data = NULL; 137406e0070dSMark Shellenbaum dmu_buf_impl_t *db = NULL; 13750a586ceaSMark Shellenbaum uint64_t *user, *group; 13760a586ceaSMark Shellenbaum int flags = dn->dn_id_flags; 13770a586ceaSMark Shellenbaum int error; 137806e0070dSMark Shellenbaum boolean_t have_spill = B_FALSE; 13790a586ceaSMark Shellenbaum 13800a586ceaSMark Shellenbaum if (!dmu_objset_userused_enabled(dn->dn_objset)) 13810a586ceaSMark Shellenbaum return; 13820a586ceaSMark Shellenbaum 13830a586ceaSMark Shellenbaum if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST| 13840a586ceaSMark Shellenbaum DN_ID_CHKED_SPILL))) 13850a586ceaSMark Shellenbaum return; 13860a586ceaSMark Shellenbaum 13870a586ceaSMark Shellenbaum if (before && dn->dn_bonuslen != 0) 13880a586ceaSMark Shellenbaum data = DN_BONUS(dn->dn_phys); 138906e0070dSMark Shellenbaum else if (!before && dn->dn_bonuslen != 0) { 139006e0070dSMark Shellenbaum if (dn->dn_bonus) { 139106e0070dSMark Shellenbaum db = dn->dn_bonus; 139206e0070dSMark Shellenbaum mutex_enter(&db->db_mtx); 139306e0070dSMark Shellenbaum data = dmu_objset_userquota_find_data(db, tx); 139406e0070dSMark Shellenbaum } else { 139506e0070dSMark Shellenbaum data = DN_BONUS(dn->dn_phys); 139606e0070dSMark Shellenbaum } 139706e0070dSMark Shellenbaum } else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) { 13980a586ceaSMark Shellenbaum int rf = 0; 13990a586ceaSMark Shellenbaum 14000a586ceaSMark Shellenbaum if (RW_WRITE_HELD(&dn->dn_struct_rwlock)) 14010a586ceaSMark Shellenbaum rf |= DB_RF_HAVESTRUCT; 14021d8ccc7bSMark Shellenbaum error = dmu_spill_hold_by_dnode(dn, 14031d8ccc7bSMark Shellenbaum rf | DB_RF_MUST_SUCCEED, 140406e0070dSMark Shellenbaum FTAG, (dmu_buf_t **)&db); 14050a586ceaSMark Shellenbaum ASSERT(error == 0); 140606e0070dSMark Shellenbaum mutex_enter(&db->db_mtx); 140706e0070dSMark Shellenbaum data = (before) ? db->db.db_data : 140806e0070dSMark Shellenbaum dmu_objset_userquota_find_data(db, tx); 140906e0070dSMark Shellenbaum have_spill = B_TRUE; 14100a586ceaSMark Shellenbaum } else { 14110a586ceaSMark Shellenbaum mutex_enter(&dn->dn_mtx); 14120a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_BONUS; 14130a586ceaSMark Shellenbaum mutex_exit(&dn->dn_mtx); 14140a586ceaSMark Shellenbaum return; 14150a586ceaSMark Shellenbaum } 14160a586ceaSMark Shellenbaum 14170a586ceaSMark Shellenbaum if (before) { 141806e0070dSMark Shellenbaum ASSERT(data); 14190a586ceaSMark Shellenbaum user = &dn->dn_olduid; 14200a586ceaSMark Shellenbaum group = &dn->dn_oldgid; 142106e0070dSMark Shellenbaum } else if (data) { 14220a586ceaSMark Shellenbaum user = &dn->dn_newuid; 14230a586ceaSMark Shellenbaum group = &dn->dn_newgid; 14240a586ceaSMark Shellenbaum } 14250a586ceaSMark Shellenbaum 142606e0070dSMark Shellenbaum /* 142706e0070dSMark Shellenbaum * Must always call the callback in case the object 142806e0070dSMark Shellenbaum * type has changed and that type isn't an object type to track 142906e0070dSMark Shellenbaum */ 14300a586ceaSMark Shellenbaum error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data, 14310a586ceaSMark Shellenbaum user, group); 14320a586ceaSMark Shellenbaum 143306e0070dSMark Shellenbaum /* 143406e0070dSMark Shellenbaum * Preserve existing uid/gid when the callback can't determine 143506e0070dSMark Shellenbaum * what the new uid/gid are and the callback returned EEXIST. 143606e0070dSMark Shellenbaum * The EEXIST error tells us to just use the existing uid/gid. 143706e0070dSMark Shellenbaum * If we don't know what the old values are then just assign 143806e0070dSMark Shellenbaum * them to 0, since that is a new file being created. 143906e0070dSMark Shellenbaum */ 144006e0070dSMark Shellenbaum if (!before && data == NULL && error == EEXIST) { 144106e0070dSMark Shellenbaum if (flags & DN_ID_OLD_EXIST) { 144206e0070dSMark Shellenbaum dn->dn_newuid = dn->dn_olduid; 144306e0070dSMark Shellenbaum dn->dn_newgid = dn->dn_oldgid; 144406e0070dSMark Shellenbaum } else { 144506e0070dSMark Shellenbaum dn->dn_newuid = 0; 144606e0070dSMark Shellenbaum dn->dn_newgid = 0; 144706e0070dSMark Shellenbaum } 144806e0070dSMark Shellenbaum error = 0; 144906e0070dSMark Shellenbaum } 145006e0070dSMark Shellenbaum 145106e0070dSMark Shellenbaum if (db) 145206e0070dSMark Shellenbaum mutex_exit(&db->db_mtx); 145306e0070dSMark Shellenbaum 14540a586ceaSMark Shellenbaum mutex_enter(&dn->dn_mtx); 14550a586ceaSMark Shellenbaum if (error == 0 && before) 14560a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_OLD_EXIST; 14570a586ceaSMark Shellenbaum if (error == 0 && !before) 14580a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_NEW_EXIST; 14590a586ceaSMark Shellenbaum 146006e0070dSMark Shellenbaum if (have_spill) { 14610a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_SPILL; 14620a586ceaSMark Shellenbaum } else { 14630a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_BONUS; 14640a586ceaSMark Shellenbaum } 14650a586ceaSMark Shellenbaum mutex_exit(&dn->dn_mtx); 146606e0070dSMark Shellenbaum if (have_spill) 146706e0070dSMark Shellenbaum dmu_buf_rele((dmu_buf_t *)db, FTAG); 14680a586ceaSMark Shellenbaum } 14690a586ceaSMark Shellenbaum 147014843421SMatthew Ahrens boolean_t 147114843421SMatthew Ahrens dmu_objset_userspace_present(objset_t *os) 147214843421SMatthew Ahrens { 1473503ad85cSMatthew Ahrens return (os->os_phys->os_flags & 147414843421SMatthew Ahrens OBJSET_FLAG_USERACCOUNTING_COMPLETE); 147514843421SMatthew Ahrens } 147614843421SMatthew Ahrens 147714843421SMatthew Ahrens int 147814843421SMatthew Ahrens dmu_objset_userspace_upgrade(objset_t *os) 147914843421SMatthew Ahrens { 148014843421SMatthew Ahrens uint64_t obj; 148114843421SMatthew Ahrens int err = 0; 148214843421SMatthew Ahrens 148314843421SMatthew Ahrens if (dmu_objset_userspace_present(os)) 148414843421SMatthew Ahrens return (0); 1485503ad85cSMatthew Ahrens if (!dmu_objset_userused_enabled(os)) 148614843421SMatthew Ahrens return (ENOTSUP); 148714843421SMatthew Ahrens if (dmu_objset_is_snapshot(os)) 148814843421SMatthew Ahrens return (EINVAL); 148914843421SMatthew Ahrens 149014843421SMatthew Ahrens /* 149114843421SMatthew Ahrens * We simply need to mark every object dirty, so that it will be 149214843421SMatthew Ahrens * synced out and now accounted. If this is called 149314843421SMatthew Ahrens * concurrently, or if we already did some work before crashing, 149414843421SMatthew Ahrens * that's fine, since we track each object's accounted state 149514843421SMatthew Ahrens * independently. 149614843421SMatthew Ahrens */ 149714843421SMatthew Ahrens 149814843421SMatthew Ahrens for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) { 149968857716SLin Ling dmu_tx_t *tx; 150014843421SMatthew Ahrens dmu_buf_t *db; 150114843421SMatthew Ahrens int objerr; 150214843421SMatthew Ahrens 150314843421SMatthew Ahrens if (issig(JUSTLOOKING) && issig(FORREAL)) 150414843421SMatthew Ahrens return (EINTR); 150514843421SMatthew Ahrens 150614843421SMatthew Ahrens objerr = dmu_bonus_hold(os, obj, FTAG, &db); 150714843421SMatthew Ahrens if (objerr) 150814843421SMatthew Ahrens continue; 150968857716SLin Ling tx = dmu_tx_create(os); 151014843421SMatthew Ahrens dmu_tx_hold_bonus(tx, obj); 151114843421SMatthew Ahrens objerr = dmu_tx_assign(tx, TXG_WAIT); 151214843421SMatthew Ahrens if (objerr) { 151314843421SMatthew Ahrens dmu_tx_abort(tx); 151414843421SMatthew Ahrens continue; 151514843421SMatthew Ahrens } 151614843421SMatthew Ahrens dmu_buf_will_dirty(db, tx); 151714843421SMatthew Ahrens dmu_buf_rele(db, FTAG); 151814843421SMatthew Ahrens dmu_tx_commit(tx); 151914843421SMatthew Ahrens } 152014843421SMatthew Ahrens 1521503ad85cSMatthew Ahrens os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE; 152214843421SMatthew Ahrens txg_wait_synced(dmu_objset_pool(os), 0); 152314843421SMatthew Ahrens return (0); 152414843421SMatthew Ahrens } 152514843421SMatthew Ahrens 1526fa9e4066Sahrens void 1527a2eea2e1Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp, 1528a2eea2e1Sahrens uint64_t *usedobjsp, uint64_t *availobjsp) 1529fa9e4066Sahrens { 1530503ad85cSMatthew Ahrens dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp, 1531a2eea2e1Sahrens usedobjsp, availobjsp); 1532a2eea2e1Sahrens } 1533a2eea2e1Sahrens 1534a2eea2e1Sahrens uint64_t 1535a2eea2e1Sahrens dmu_objset_fsid_guid(objset_t *os) 1536a2eea2e1Sahrens { 1537503ad85cSMatthew Ahrens return (dsl_dataset_fsid_guid(os->os_dsl_dataset)); 1538a2eea2e1Sahrens } 1539a2eea2e1Sahrens 1540a2eea2e1Sahrens void 1541a2eea2e1Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat) 1542a2eea2e1Sahrens { 1543503ad85cSMatthew Ahrens stat->dds_type = os->os_phys->os_type; 1544503ad85cSMatthew Ahrens if (os->os_dsl_dataset) 1545503ad85cSMatthew Ahrens dsl_dataset_fast_stat(os->os_dsl_dataset, stat); 1546a2eea2e1Sahrens } 1547a2eea2e1Sahrens 1548a2eea2e1Sahrens void 1549a2eea2e1Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv) 1550a2eea2e1Sahrens { 1551503ad85cSMatthew Ahrens ASSERT(os->os_dsl_dataset || 1552503ad85cSMatthew Ahrens os->os_phys->os_type == DMU_OST_META); 1553a2eea2e1Sahrens 1554503ad85cSMatthew Ahrens if (os->os_dsl_dataset != NULL) 1555503ad85cSMatthew Ahrens dsl_dataset_stats(os->os_dsl_dataset, nv); 1556a2eea2e1Sahrens 1557a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE, 1558503ad85cSMatthew Ahrens os->os_phys->os_type); 155914843421SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING, 156014843421SMatthew Ahrens dmu_objset_userspace_present(os)); 1561fa9e4066Sahrens } 1562fa9e4066Sahrens 1563fa9e4066Sahrens int 1564fa9e4066Sahrens dmu_objset_is_snapshot(objset_t *os) 1565fa9e4066Sahrens { 1566503ad85cSMatthew Ahrens if (os->os_dsl_dataset != NULL) 1567503ad85cSMatthew Ahrens return (dsl_dataset_is_snapshot(os->os_dsl_dataset)); 1568fa9e4066Sahrens else 1569fa9e4066Sahrens return (B_FALSE); 1570fa9e4066Sahrens } 1571fa9e4066Sahrens 1572ab04eb8eStimh int 1573ab04eb8eStimh dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen, 1574ab04eb8eStimh boolean_t *conflict) 1575ab04eb8eStimh { 1576503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 1577ab04eb8eStimh uint64_t ignored; 1578ab04eb8eStimh 1579ab04eb8eStimh if (ds->ds_phys->ds_snapnames_zapobj == 0) 1580ab04eb8eStimh return (ENOENT); 1581ab04eb8eStimh 1582ab04eb8eStimh return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset, 1583ab04eb8eStimh ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST, 1584ab04eb8eStimh real, maxlen, conflict)); 1585ab04eb8eStimh } 1586ab04eb8eStimh 1587fa9e4066Sahrens int 1588fa9e4066Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name, 1589b38f0970Sck uint64_t *idp, uint64_t *offp, boolean_t *case_conflict) 1590fa9e4066Sahrens { 1591503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 1592fa9e4066Sahrens zap_cursor_t cursor; 1593fa9e4066Sahrens zap_attribute_t attr; 1594fa9e4066Sahrens 1595fa9e4066Sahrens if (ds->ds_phys->ds_snapnames_zapobj == 0) 1596fa9e4066Sahrens return (ENOENT); 1597fa9e4066Sahrens 1598fa9e4066Sahrens zap_cursor_init_serialized(&cursor, 1599fa9e4066Sahrens ds->ds_dir->dd_pool->dp_meta_objset, 1600fa9e4066Sahrens ds->ds_phys->ds_snapnames_zapobj, *offp); 1601fa9e4066Sahrens 160287e5029aSahrens if (zap_cursor_retrieve(&cursor, &attr) != 0) { 160387e5029aSahrens zap_cursor_fini(&cursor); 160487e5029aSahrens return (ENOENT); 160587e5029aSahrens } 160687e5029aSahrens 160787e5029aSahrens if (strlen(attr.za_name) + 1 > namelen) { 160887e5029aSahrens zap_cursor_fini(&cursor); 160987e5029aSahrens return (ENAMETOOLONG); 161087e5029aSahrens } 161187e5029aSahrens 161287e5029aSahrens (void) strcpy(name, attr.za_name); 161387e5029aSahrens if (idp) 161487e5029aSahrens *idp = attr.za_first_integer; 1615b38f0970Sck if (case_conflict) 1616b38f0970Sck *case_conflict = attr.za_normalization_conflict; 161787e5029aSahrens zap_cursor_advance(&cursor); 161887e5029aSahrens *offp = zap_cursor_serialize(&cursor); 161987e5029aSahrens zap_cursor_fini(&cursor); 162087e5029aSahrens 162187e5029aSahrens return (0); 162287e5029aSahrens } 162387e5029aSahrens 162487e5029aSahrens int 162587e5029aSahrens dmu_dir_list_next(objset_t *os, int namelen, char *name, 162687e5029aSahrens uint64_t *idp, uint64_t *offp) 162787e5029aSahrens { 1628503ad85cSMatthew Ahrens dsl_dir_t *dd = os->os_dsl_dataset->ds_dir; 162987e5029aSahrens zap_cursor_t cursor; 163087e5029aSahrens zap_attribute_t attr; 163187e5029aSahrens 163287e5029aSahrens /* there is no next dir on a snapshot! */ 1633503ad85cSMatthew Ahrens if (os->os_dsl_dataset->ds_object != 163487e5029aSahrens dd->dd_phys->dd_head_dataset_obj) 1635fa9e4066Sahrens return (ENOENT); 1636fa9e4066Sahrens 163787e5029aSahrens zap_cursor_init_serialized(&cursor, 163887e5029aSahrens dd->dd_pool->dp_meta_objset, 163987e5029aSahrens dd->dd_phys->dd_child_dir_zapobj, *offp); 164087e5029aSahrens 164187e5029aSahrens if (zap_cursor_retrieve(&cursor, &attr) != 0) { 164287e5029aSahrens zap_cursor_fini(&cursor); 164387e5029aSahrens return (ENOENT); 164487e5029aSahrens } 164587e5029aSahrens 164687e5029aSahrens if (strlen(attr.za_name) + 1 > namelen) { 164787e5029aSahrens zap_cursor_fini(&cursor); 1648fa9e4066Sahrens return (ENAMETOOLONG); 164987e5029aSahrens } 1650fa9e4066Sahrens 1651fa9e4066Sahrens (void) strcpy(name, attr.za_name); 165287e5029aSahrens if (idp) 165387e5029aSahrens *idp = attr.za_first_integer; 1654fa9e4066Sahrens zap_cursor_advance(&cursor); 1655fa9e4066Sahrens *offp = zap_cursor_serialize(&cursor); 165687e5029aSahrens zap_cursor_fini(&cursor); 1657fa9e4066Sahrens 1658fa9e4066Sahrens return (0); 1659fa9e4066Sahrens } 1660fa9e4066Sahrens 1661088f3894Sahrens struct findarg { 1662fd136879SMatthew Ahrens int (*func)(const char *, void *); 1663088f3894Sahrens void *arg; 1664088f3894Sahrens }; 1665088f3894Sahrens 1666088f3894Sahrens /* ARGSUSED */ 1667088f3894Sahrens static int 1668088f3894Sahrens findfunc(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg) 1669088f3894Sahrens { 1670088f3894Sahrens struct findarg *fa = arg; 1671fd136879SMatthew Ahrens return (fa->func(dsname, fa->arg)); 1672088f3894Sahrens } 1673088f3894Sahrens 1674fa9e4066Sahrens /* 1675fa9e4066Sahrens * Find all objsets under name, and for each, call 'func(child_name, arg)'. 1676088f3894Sahrens * Perhaps change all callers to use dmu_objset_find_spa()? 1677fa9e4066Sahrens */ 16781d452cf5Sahrens int 1679fd136879SMatthew Ahrens dmu_objset_find(char *name, int func(const char *, void *), void *arg, 1680fd136879SMatthew Ahrens int flags) 1681088f3894Sahrens { 1682088f3894Sahrens struct findarg fa; 1683088f3894Sahrens fa.func = func; 1684088f3894Sahrens fa.arg = arg; 1685088f3894Sahrens return (dmu_objset_find_spa(NULL, name, findfunc, &fa, flags)); 1686088f3894Sahrens } 1687088f3894Sahrens 1688088f3894Sahrens /* 1689088f3894Sahrens * Find all objsets under name, call func on each 1690088f3894Sahrens */ 1691088f3894Sahrens int 1692088f3894Sahrens dmu_objset_find_spa(spa_t *spa, const char *name, 1693088f3894Sahrens int func(spa_t *, uint64_t, const char *, void *), void *arg, int flags) 1694fa9e4066Sahrens { 1695fa9e4066Sahrens dsl_dir_t *dd; 1696088f3894Sahrens dsl_pool_t *dp; 1697088f3894Sahrens dsl_dataset_t *ds; 1698fa9e4066Sahrens zap_cursor_t zc; 1699b7661cccSmmusante zap_attribute_t *attr; 1700fa9e4066Sahrens char *child; 1701088f3894Sahrens uint64_t thisobj; 1702088f3894Sahrens int err; 1703fa9e4066Sahrens 1704088f3894Sahrens if (name == NULL) 1705088f3894Sahrens name = spa_name(spa); 1706088f3894Sahrens err = dsl_dir_open_spa(spa, name, FTAG, &dd, NULL); 1707ea8dc4b6Seschrock if (err) 17081d452cf5Sahrens return (err); 1709fa9e4066Sahrens 1710088f3894Sahrens /* Don't visit hidden ($MOS & $ORIGIN) objsets. */ 1711088f3894Sahrens if (dd->dd_myname[0] == '$') { 1712088f3894Sahrens dsl_dir_close(dd, FTAG); 1713088f3894Sahrens return (0); 1714088f3894Sahrens } 1715088f3894Sahrens 1716088f3894Sahrens thisobj = dd->dd_phys->dd_head_dataset_obj; 1717b7661cccSmmusante attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); 1718088f3894Sahrens dp = dd->dd_pool; 1719fa9e4066Sahrens 1720fa9e4066Sahrens /* 1721fa9e4066Sahrens * Iterate over all children. 1722fa9e4066Sahrens */ 17230b69c2f0Sahrens if (flags & DS_FIND_CHILDREN) { 1724088f3894Sahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, 17250b69c2f0Sahrens dd->dd_phys->dd_child_dir_zapobj); 1726b7661cccSmmusante zap_cursor_retrieve(&zc, attr) == 0; 17270b69c2f0Sahrens (void) zap_cursor_advance(&zc)) { 1728b7661cccSmmusante ASSERT(attr->za_integer_length == sizeof (uint64_t)); 1729b7661cccSmmusante ASSERT(attr->za_num_integers == 1); 1730fa9e4066Sahrens 1731486ae710SMatthew Ahrens child = kmem_asprintf("%s/%s", name, attr->za_name); 1732088f3894Sahrens err = dmu_objset_find_spa(spa, child, func, arg, flags); 1733486ae710SMatthew Ahrens strfree(child); 17340b69c2f0Sahrens if (err) 17350b69c2f0Sahrens break; 17360b69c2f0Sahrens } 17370b69c2f0Sahrens zap_cursor_fini(&zc); 17381d452cf5Sahrens 17390b69c2f0Sahrens if (err) { 17400b69c2f0Sahrens dsl_dir_close(dd, FTAG); 1741b7661cccSmmusante kmem_free(attr, sizeof (zap_attribute_t)); 17420b69c2f0Sahrens return (err); 17430b69c2f0Sahrens } 1744fa9e4066Sahrens } 1745fa9e4066Sahrens 1746fa9e4066Sahrens /* 1747fa9e4066Sahrens * Iterate over all snapshots. 1748fa9e4066Sahrens */ 1749088f3894Sahrens if (flags & DS_FIND_SNAPSHOTS) { 1750088f3894Sahrens if (!dsl_pool_sync_context(dp)) 1751088f3894Sahrens rw_enter(&dp->dp_config_rwlock, RW_READER); 1752088f3894Sahrens err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds); 1753088f3894Sahrens if (!dsl_pool_sync_context(dp)) 1754088f3894Sahrens rw_exit(&dp->dp_config_rwlock); 1755088f3894Sahrens 1756088f3894Sahrens if (err == 0) { 1757088f3894Sahrens uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj; 1758088f3894Sahrens dsl_dataset_rele(ds, FTAG); 1759088f3894Sahrens 1760088f3894Sahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj); 1761088f3894Sahrens zap_cursor_retrieve(&zc, attr) == 0; 1762088f3894Sahrens (void) zap_cursor_advance(&zc)) { 1763088f3894Sahrens ASSERT(attr->za_integer_length == 1764088f3894Sahrens sizeof (uint64_t)); 1765088f3894Sahrens ASSERT(attr->za_num_integers == 1); 1766088f3894Sahrens 1767486ae710SMatthew Ahrens child = kmem_asprintf("%s@%s", 1768486ae710SMatthew Ahrens name, attr->za_name); 1769088f3894Sahrens err = func(spa, attr->za_first_integer, 1770088f3894Sahrens child, arg); 1771486ae710SMatthew Ahrens strfree(child); 1772088f3894Sahrens if (err) 1773088f3894Sahrens break; 1774088f3894Sahrens } 1775088f3894Sahrens zap_cursor_fini(&zc); 1776fa9e4066Sahrens } 1777fa9e4066Sahrens } 1778fa9e4066Sahrens 1779fa9e4066Sahrens dsl_dir_close(dd, FTAG); 1780b7661cccSmmusante kmem_free(attr, sizeof (zap_attribute_t)); 1781fa9e4066Sahrens 17821d452cf5Sahrens if (err) 17831d452cf5Sahrens return (err); 17841d452cf5Sahrens 1785fa9e4066Sahrens /* 1786fa9e4066Sahrens * Apply to self if appropriate. 1787fa9e4066Sahrens */ 1788088f3894Sahrens err = func(spa, thisobj, name, arg); 17891d452cf5Sahrens return (err); 1790fa9e4066Sahrens } 1791f18faf3fSek 17927f73c863SRich Morris /* ARGSUSED */ 17937f73c863SRich Morris int 1794fd136879SMatthew Ahrens dmu_objset_prefetch(const char *name, void *arg) 17957f73c863SRich Morris { 17967f73c863SRich Morris dsl_dataset_t *ds; 17977f73c863SRich Morris 17987f73c863SRich Morris if (dsl_dataset_hold(name, FTAG, &ds)) 17997f73c863SRich Morris return (0); 18007f73c863SRich Morris 18017f73c863SRich Morris if (!BP_IS_HOLE(&ds->ds_phys->ds_bp)) { 18027f73c863SRich Morris mutex_enter(&ds->ds_opening_lock); 1803503ad85cSMatthew Ahrens if (ds->ds_objset == NULL) { 18047f73c863SRich Morris uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH; 18057f73c863SRich Morris zbookmark_t zb; 18067f73c863SRich Morris 1807b24ab676SJeff Bonwick SET_BOOKMARK(&zb, ds->ds_object, ZB_ROOT_OBJECT, 1808b24ab676SJeff Bonwick ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 18097f73c863SRich Morris 1810*1b912ec7SGeorge Wilson (void) arc_read(NULL, dsl_dataset_get_spa(ds), 18117f73c863SRich Morris &ds->ds_phys->ds_bp, NULL, NULL, 18127f73c863SRich Morris ZIO_PRIORITY_ASYNC_READ, 18137f73c863SRich Morris ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, 18147f73c863SRich Morris &aflags, &zb); 18157f73c863SRich Morris } 18167f73c863SRich Morris mutex_exit(&ds->ds_opening_lock); 18177f73c863SRich Morris } 18187f73c863SRich Morris 18197f73c863SRich Morris dsl_dataset_rele(ds, FTAG); 18207f73c863SRich Morris return (0); 18217f73c863SRich Morris } 18227f73c863SRich Morris 1823f18faf3fSek void 1824f18faf3fSek dmu_objset_set_user(objset_t *os, void *user_ptr) 1825f18faf3fSek { 1826503ad85cSMatthew Ahrens ASSERT(MUTEX_HELD(&os->os_user_ptr_lock)); 1827503ad85cSMatthew Ahrens os->os_user_ptr = user_ptr; 1828f18faf3fSek } 1829f18faf3fSek 1830f18faf3fSek void * 1831f18faf3fSek dmu_objset_get_user(objset_t *os) 1832f18faf3fSek { 1833503ad85cSMatthew Ahrens ASSERT(MUTEX_HELD(&os->os_user_ptr_lock)); 1834503ad85cSMatthew Ahrens return (os->os_user_ptr); 1835f18faf3fSek } 1836