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 /* 22a4585abfSahrens * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 23fa9e4066Sahrens * Use is subject to license terms. 24fa9e4066Sahrens */ 25fa9e4066Sahrens 26fa9e4066Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 27fa9e4066Sahrens 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/zio_checksum.h> 42fa9e4066Sahrens #include <sys/zap.h> 43fa9e4066Sahrens #include <sys/zil.h> 44fa9e4066Sahrens #include <sys/dmu_impl.h> 45ecd6cf80Smarks #include <sys/zfs_ioctl.h> 46fa9e4066Sahrens 47fa9e4066Sahrens spa_t * 48fa9e4066Sahrens dmu_objset_spa(objset_t *os) 49fa9e4066Sahrens { 50fa9e4066Sahrens return (os->os->os_spa); 51fa9e4066Sahrens } 52fa9e4066Sahrens 53fa9e4066Sahrens zilog_t * 54fa9e4066Sahrens dmu_objset_zil(objset_t *os) 55fa9e4066Sahrens { 56fa9e4066Sahrens return (os->os->os_zil); 57fa9e4066Sahrens } 58fa9e4066Sahrens 59fa9e4066Sahrens dsl_pool_t * 60fa9e4066Sahrens dmu_objset_pool(objset_t *os) 61fa9e4066Sahrens { 62fa9e4066Sahrens dsl_dataset_t *ds; 63fa9e4066Sahrens 64fa9e4066Sahrens if ((ds = os->os->os_dsl_dataset) != NULL && ds->ds_dir) 65fa9e4066Sahrens return (ds->ds_dir->dd_pool); 66fa9e4066Sahrens else 67fa9e4066Sahrens return (spa_get_dsl(os->os->os_spa)); 68fa9e4066Sahrens } 69fa9e4066Sahrens 70fa9e4066Sahrens dsl_dataset_t * 71fa9e4066Sahrens dmu_objset_ds(objset_t *os) 72fa9e4066Sahrens { 73fa9e4066Sahrens return (os->os->os_dsl_dataset); 74fa9e4066Sahrens } 75fa9e4066Sahrens 76fa9e4066Sahrens dmu_objset_type_t 77fa9e4066Sahrens dmu_objset_type(objset_t *os) 78fa9e4066Sahrens { 79fa9e4066Sahrens return (os->os->os_phys->os_type); 80fa9e4066Sahrens } 81fa9e4066Sahrens 82fa9e4066Sahrens void 83fa9e4066Sahrens dmu_objset_name(objset_t *os, char *buf) 84fa9e4066Sahrens { 85fa9e4066Sahrens dsl_dataset_name(os->os->os_dsl_dataset, buf); 86fa9e4066Sahrens } 87fa9e4066Sahrens 88fa9e4066Sahrens uint64_t 89fa9e4066Sahrens dmu_objset_id(objset_t *os) 90fa9e4066Sahrens { 91fa9e4066Sahrens dsl_dataset_t *ds = os->os->os_dsl_dataset; 92fa9e4066Sahrens 93fa9e4066Sahrens return (ds ? ds->ds_object : 0); 94fa9e4066Sahrens } 95fa9e4066Sahrens 96fa9e4066Sahrens static void 97fa9e4066Sahrens checksum_changed_cb(void *arg, uint64_t newval) 98fa9e4066Sahrens { 99fa9e4066Sahrens objset_impl_t *osi = arg; 100fa9e4066Sahrens 101fa9e4066Sahrens /* 102fa9e4066Sahrens * Inheritance should have been done by now. 103fa9e4066Sahrens */ 104fa9e4066Sahrens ASSERT(newval != ZIO_CHECKSUM_INHERIT); 105fa9e4066Sahrens 106fa9e4066Sahrens osi->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE); 107fa9e4066Sahrens } 108fa9e4066Sahrens 109fa9e4066Sahrens static void 110fa9e4066Sahrens compression_changed_cb(void *arg, uint64_t newval) 111fa9e4066Sahrens { 112fa9e4066Sahrens objset_impl_t *osi = arg; 113fa9e4066Sahrens 114fa9e4066Sahrens /* 115fa9e4066Sahrens * Inheritance and range checking should have been done by now. 116fa9e4066Sahrens */ 117fa9e4066Sahrens ASSERT(newval != ZIO_COMPRESS_INHERIT); 118fa9e4066Sahrens 119fa9e4066Sahrens osi->os_compress = zio_compress_select(newval, ZIO_COMPRESS_ON_VALUE); 120fa9e4066Sahrens } 121fa9e4066Sahrens 122d0ad202dSahrens static void 123d0ad202dSahrens copies_changed_cb(void *arg, uint64_t newval) 124d0ad202dSahrens { 125d0ad202dSahrens objset_impl_t *osi = arg; 126d0ad202dSahrens 127d0ad202dSahrens /* 128d0ad202dSahrens * Inheritance and range checking should have been done by now. 129d0ad202dSahrens */ 130d0ad202dSahrens ASSERT(newval > 0); 131d0ad202dSahrens ASSERT(newval <= spa_max_replication(osi->os_spa)); 132d0ad202dSahrens 133d0ad202dSahrens osi->os_copies = newval; 134d0ad202dSahrens } 135d0ad202dSahrens 136fa9e4066Sahrens void 137fa9e4066Sahrens dmu_objset_byteswap(void *buf, size_t size) 138fa9e4066Sahrens { 139fa9e4066Sahrens objset_phys_t *osp = buf; 140fa9e4066Sahrens 141fa9e4066Sahrens ASSERT(size == sizeof (objset_phys_t)); 142fa9e4066Sahrens dnode_byteswap(&osp->os_meta_dnode); 143fa9e4066Sahrens byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t)); 144fa9e4066Sahrens osp->os_type = BSWAP_64(osp->os_type); 145fa9e4066Sahrens } 146fa9e4066Sahrens 147ea8dc4b6Seschrock int 148ea8dc4b6Seschrock dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, 149ea8dc4b6Seschrock objset_impl_t **osip) 150fa9e4066Sahrens { 15191ebeef5Sahrens objset_impl_t *osi; 152*088f3894Sahrens int i, err; 153fa9e4066Sahrens 15491ebeef5Sahrens ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock)); 15591ebeef5Sahrens 156fa9e4066Sahrens osi = kmem_zalloc(sizeof (objset_impl_t), KM_SLEEP); 157fa9e4066Sahrens osi->os.os = osi; 158fa9e4066Sahrens osi->os_dsl_dataset = ds; 159fa9e4066Sahrens osi->os_spa = spa; 160c717a561Smaybee osi->os_rootbp = bp; 161c717a561Smaybee if (!BP_IS_HOLE(osi->os_rootbp)) { 16213506d1eSmaybee uint32_t aflags = ARC_WAIT; 163ea8dc4b6Seschrock zbookmark_t zb; 164ea8dc4b6Seschrock zb.zb_objset = ds ? ds->ds_object : 0; 165ea8dc4b6Seschrock zb.zb_object = 0; 166ea8dc4b6Seschrock zb.zb_level = -1; 167ea8dc4b6Seschrock zb.zb_blkid = 0; 168ea8dc4b6Seschrock 169c717a561Smaybee dprintf_bp(osi->os_rootbp, "reading %s", ""); 170*088f3894Sahrens /* 171*088f3894Sahrens * NB: when bprewrite scrub can change the bp, 172*088f3894Sahrens * and this is called from dmu_objset_open_ds_os, the bp 173*088f3894Sahrens * could change, and we'll need a lock. 174*088f3894Sahrens */ 175*088f3894Sahrens err = arc_read_nolock(NULL, spa, osi->os_rootbp, 176c717a561Smaybee arc_getbuf_func, &osi->os_phys_buf, 17713506d1eSmaybee ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb); 178ea8dc4b6Seschrock if (err) { 179ea8dc4b6Seschrock kmem_free(osi, sizeof (objset_impl_t)); 180ea8dc4b6Seschrock return (err); 181ea8dc4b6Seschrock } 182c717a561Smaybee osi->os_phys = osi->os_phys_buf->b_data; 183fa9e4066Sahrens } else { 184c717a561Smaybee osi->os_phys_buf = arc_buf_alloc(spa, sizeof (objset_phys_t), 185c717a561Smaybee &osi->os_phys_buf, ARC_BUFC_METADATA); 186c717a561Smaybee osi->os_phys = osi->os_phys_buf->b_data; 187fa9e4066Sahrens bzero(osi->os_phys, sizeof (objset_phys_t)); 188fa9e4066Sahrens } 189fa9e4066Sahrens 190fa9e4066Sahrens /* 191fa9e4066Sahrens * Note: the changed_cb will be called once before the register 192fa9e4066Sahrens * func returns, thus changing the checksum/compression from the 19399653d4eSeschrock * default (fletcher2/off). Snapshots don't need to know, and 19499653d4eSeschrock * registering would complicate clone promotion. 195fa9e4066Sahrens */ 19699653d4eSeschrock if (ds && ds->ds_phys->ds_num_children == 0) { 197fa9e4066Sahrens err = dsl_prop_register(ds, "checksum", 198fa9e4066Sahrens checksum_changed_cb, osi); 199ea8dc4b6Seschrock if (err == 0) 200ea8dc4b6Seschrock err = dsl_prop_register(ds, "compression", 201ea8dc4b6Seschrock compression_changed_cb, osi); 202d0ad202dSahrens if (err == 0) 203d0ad202dSahrens err = dsl_prop_register(ds, "copies", 204d0ad202dSahrens copies_changed_cb, osi); 205ea8dc4b6Seschrock if (err) { 206c717a561Smaybee VERIFY(arc_buf_remove_ref(osi->os_phys_buf, 207c717a561Smaybee &osi->os_phys_buf) == 1); 208ea8dc4b6Seschrock kmem_free(osi, sizeof (objset_impl_t)); 209ea8dc4b6Seschrock return (err); 210ea8dc4b6Seschrock } 21199653d4eSeschrock } else if (ds == NULL) { 212fa9e4066Sahrens /* It's the meta-objset. */ 213fa9e4066Sahrens osi->os_checksum = ZIO_CHECKSUM_FLETCHER_4; 214ea8dc4b6Seschrock osi->os_compress = ZIO_COMPRESS_LZJB; 215d0ad202dSahrens osi->os_copies = spa_max_replication(spa); 216fa9e4066Sahrens } 217fa9e4066Sahrens 218*088f3894Sahrens osi->os_zil_header = osi->os_phys->os_zil_header; 219*088f3894Sahrens osi->os_zil = zil_alloc(&osi->os, &osi->os_zil_header); 220fa9e4066Sahrens 221fa9e4066Sahrens for (i = 0; i < TXG_SIZE; i++) { 222fa9e4066Sahrens list_create(&osi->os_dirty_dnodes[i], sizeof (dnode_t), 223fa9e4066Sahrens offsetof(dnode_t, dn_dirty_link[i])); 224fa9e4066Sahrens list_create(&osi->os_free_dnodes[i], sizeof (dnode_t), 225fa9e4066Sahrens offsetof(dnode_t, dn_dirty_link[i])); 226fa9e4066Sahrens } 227fa9e4066Sahrens list_create(&osi->os_dnodes, sizeof (dnode_t), 228fa9e4066Sahrens offsetof(dnode_t, dn_link)); 229fa9e4066Sahrens list_create(&osi->os_downgraded_dbufs, sizeof (dmu_buf_impl_t), 230fa9e4066Sahrens offsetof(dmu_buf_impl_t, db_link)); 231fa9e4066Sahrens 2325ad82045Snd mutex_init(&osi->os_lock, NULL, MUTEX_DEFAULT, NULL); 2335ad82045Snd mutex_init(&osi->os_obj_lock, NULL, MUTEX_DEFAULT, NULL); 234f18faf3fSek mutex_init(&osi->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL); 2355ad82045Snd 236fa9e4066Sahrens osi->os_meta_dnode = dnode_special_open(osi, 237fa9e4066Sahrens &osi->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT); 238fa9e4066Sahrens 23991ebeef5Sahrens /* 24091ebeef5Sahrens * We should be the only thread trying to do this because we 24191ebeef5Sahrens * have ds_opening_lock 24291ebeef5Sahrens */ 24391ebeef5Sahrens if (ds) { 24491ebeef5Sahrens VERIFY(NULL == dsl_dataset_set_user_ptr(ds, osi, 24591ebeef5Sahrens dmu_objset_evict)); 246fa9e4066Sahrens } 247fa9e4066Sahrens 248ea8dc4b6Seschrock *osip = osi; 249ea8dc4b6Seschrock return (0); 250fa9e4066Sahrens } 251fa9e4066Sahrens 2523cb34c60Sahrens static int 2533cb34c60Sahrens dmu_objset_open_ds_os(dsl_dataset_t *ds, objset_t *os, dmu_objset_type_t type) 2543cb34c60Sahrens { 2553cb34c60Sahrens objset_impl_t *osi; 2563cb34c60Sahrens 2573cb34c60Sahrens mutex_enter(&ds->ds_opening_lock); 2583cb34c60Sahrens osi = dsl_dataset_get_user_ptr(ds); 2593cb34c60Sahrens if (osi == NULL) { 260745cd3c5Smaybee int err; 261745cd3c5Smaybee 2623cb34c60Sahrens err = dmu_objset_open_impl(dsl_dataset_get_spa(ds), 2633cb34c60Sahrens ds, &ds->ds_phys->ds_bp, &osi); 264745cd3c5Smaybee if (err) { 265745cd3c5Smaybee mutex_exit(&ds->ds_opening_lock); 2663cb34c60Sahrens return (err); 267745cd3c5Smaybee } 2683cb34c60Sahrens } 2693cb34c60Sahrens mutex_exit(&ds->ds_opening_lock); 2703cb34c60Sahrens 2713cb34c60Sahrens os->os = osi; 272745cd3c5Smaybee os->os_mode = DS_MODE_NOHOLD; 2733cb34c60Sahrens 2743cb34c60Sahrens if (type != DMU_OST_ANY && type != os->os->os_phys->os_type) 2753cb34c60Sahrens return (EINVAL); 2763cb34c60Sahrens return (0); 2773cb34c60Sahrens } 2783cb34c60Sahrens 2793cb34c60Sahrens int 2803cb34c60Sahrens dmu_objset_open_ds(dsl_dataset_t *ds, dmu_objset_type_t type, objset_t **osp) 2813cb34c60Sahrens { 2823cb34c60Sahrens objset_t *os; 2833cb34c60Sahrens int err; 2843cb34c60Sahrens 2853cb34c60Sahrens os = kmem_alloc(sizeof (objset_t), KM_SLEEP); 2863cb34c60Sahrens err = dmu_objset_open_ds_os(ds, os, type); 2873cb34c60Sahrens if (err) 2883cb34c60Sahrens kmem_free(os, sizeof (objset_t)); 2893cb34c60Sahrens else 2903cb34c60Sahrens *osp = os; 2913cb34c60Sahrens return (err); 2923cb34c60Sahrens } 2933cb34c60Sahrens 294fa9e4066Sahrens /* called from zpl */ 295fa9e4066Sahrens int 296fa9e4066Sahrens dmu_objset_open(const char *name, dmu_objset_type_t type, int mode, 297fa9e4066Sahrens objset_t **osp) 298fa9e4066Sahrens { 299fa9e4066Sahrens objset_t *os; 300f18faf3fSek dsl_dataset_t *ds; 301f18faf3fSek int err; 302fa9e4066Sahrens 303745cd3c5Smaybee ASSERT(DS_MODE_TYPE(mode) == DS_MODE_USER || 304745cd3c5Smaybee DS_MODE_TYPE(mode) == DS_MODE_OWNER); 3053cb34c60Sahrens 306fa9e4066Sahrens os = kmem_alloc(sizeof (objset_t), KM_SLEEP); 307745cd3c5Smaybee if (DS_MODE_TYPE(mode) == DS_MODE_USER) 308745cd3c5Smaybee err = dsl_dataset_hold(name, os, &ds); 309745cd3c5Smaybee else 310745cd3c5Smaybee err = dsl_dataset_own(name, mode, os, &ds); 311fa9e4066Sahrens if (err) { 312fa9e4066Sahrens kmem_free(os, sizeof (objset_t)); 313fa9e4066Sahrens return (err); 314fa9e4066Sahrens } 315fa9e4066Sahrens 3163cb34c60Sahrens err = dmu_objset_open_ds_os(ds, os, type); 3173cb34c60Sahrens if (err) { 318745cd3c5Smaybee if (DS_MODE_TYPE(mode) == DS_MODE_USER) 319745cd3c5Smaybee dsl_dataset_rele(ds, os); 320745cd3c5Smaybee else 321745cd3c5Smaybee dsl_dataset_disown(ds, os); 3223cb34c60Sahrens kmem_free(os, sizeof (objset_t)); 3233cb34c60Sahrens } else { 324745cd3c5Smaybee os->os_mode = mode; 3253cb34c60Sahrens *osp = os; 326fa9e4066Sahrens } 3273cb34c60Sahrens return (err); 328fa9e4066Sahrens } 329fa9e4066Sahrens 330fa9e4066Sahrens void 331fa9e4066Sahrens dmu_objset_close(objset_t *os) 332fa9e4066Sahrens { 333745cd3c5Smaybee ASSERT(DS_MODE_TYPE(os->os_mode) == DS_MODE_USER || 334745cd3c5Smaybee DS_MODE_TYPE(os->os_mode) == DS_MODE_OWNER || 335745cd3c5Smaybee DS_MODE_TYPE(os->os_mode) == DS_MODE_NOHOLD); 336745cd3c5Smaybee 337745cd3c5Smaybee if (DS_MODE_TYPE(os->os_mode) == DS_MODE_USER) 338745cd3c5Smaybee dsl_dataset_rele(os->os->os_dsl_dataset, os); 339745cd3c5Smaybee else if (DS_MODE_TYPE(os->os_mode) == DS_MODE_OWNER) 340745cd3c5Smaybee dsl_dataset_disown(os->os->os_dsl_dataset, os); 341fa9e4066Sahrens kmem_free(os, sizeof (objset_t)); 342fa9e4066Sahrens } 343fa9e4066Sahrens 344436b2950Sperrin int 3451934e92fSmaybee dmu_objset_evict_dbufs(objset_t *os) 346ea8dc4b6Seschrock { 347ea8dc4b6Seschrock objset_impl_t *osi = os->os; 348ea8dc4b6Seschrock dnode_t *dn; 349c543ec06Sahrens 350c543ec06Sahrens mutex_enter(&osi->os_lock); 351c543ec06Sahrens 352c543ec06Sahrens /* process the mdn last, since the other dnodes have holds on it */ 353c543ec06Sahrens list_remove(&osi->os_dnodes, osi->os_meta_dnode); 354c543ec06Sahrens list_insert_tail(&osi->os_dnodes, osi->os_meta_dnode); 355ea8dc4b6Seschrock 356ea8dc4b6Seschrock /* 357c543ec06Sahrens * Find the first dnode with holds. We have to do this dance 358c543ec06Sahrens * because dnode_add_ref() only works if you already have a 359c543ec06Sahrens * hold. If there are no holds then it has no dbufs so OK to 360c543ec06Sahrens * skip. 361ea8dc4b6Seschrock */ 362c543ec06Sahrens for (dn = list_head(&osi->os_dnodes); 3631934e92fSmaybee dn && !dnode_add_ref(dn, FTAG); 364c543ec06Sahrens dn = list_next(&osi->os_dnodes, dn)) 365c543ec06Sahrens continue; 366c543ec06Sahrens 367c543ec06Sahrens while (dn) { 368c543ec06Sahrens dnode_t *next_dn = dn; 369c543ec06Sahrens 370c543ec06Sahrens do { 371c543ec06Sahrens next_dn = list_next(&osi->os_dnodes, next_dn); 3721934e92fSmaybee } while (next_dn && !dnode_add_ref(next_dn, FTAG)); 373c543ec06Sahrens 374c543ec06Sahrens mutex_exit(&osi->os_lock); 3751934e92fSmaybee dnode_evict_dbufs(dn); 376c543ec06Sahrens dnode_rele(dn, FTAG); 377c543ec06Sahrens mutex_enter(&osi->os_lock); 378c543ec06Sahrens dn = next_dn; 379ea8dc4b6Seschrock } 380ea8dc4b6Seschrock mutex_exit(&osi->os_lock); 3811934e92fSmaybee return (list_head(&osi->os_dnodes) != osi->os_meta_dnode); 382ea8dc4b6Seschrock } 383ea8dc4b6Seschrock 384fa9e4066Sahrens void 385fa9e4066Sahrens dmu_objset_evict(dsl_dataset_t *ds, void *arg) 386fa9e4066Sahrens { 387fa9e4066Sahrens objset_impl_t *osi = arg; 388ea8dc4b6Seschrock objset_t os; 38999653d4eSeschrock int i; 390fa9e4066Sahrens 391fa9e4066Sahrens for (i = 0; i < TXG_SIZE; i++) { 392fa9e4066Sahrens ASSERT(list_head(&osi->os_dirty_dnodes[i]) == NULL); 393fa9e4066Sahrens ASSERT(list_head(&osi->os_free_dnodes[i]) == NULL); 394fa9e4066Sahrens } 395fa9e4066Sahrens 396745cd3c5Smaybee if (ds && ds->ds_phys && ds->ds_phys->ds_num_children == 0) { 39799653d4eSeschrock VERIFY(0 == dsl_prop_unregister(ds, "checksum", 39899653d4eSeschrock checksum_changed_cb, osi)); 39999653d4eSeschrock VERIFY(0 == dsl_prop_unregister(ds, "compression", 40099653d4eSeschrock compression_changed_cb, osi)); 401d0ad202dSahrens VERIFY(0 == dsl_prop_unregister(ds, "copies", 402d0ad202dSahrens copies_changed_cb, osi)); 403fa9e4066Sahrens } 404fa9e4066Sahrens 405ea8dc4b6Seschrock /* 406ea8dc4b6Seschrock * We should need only a single pass over the dnode list, since 407ea8dc4b6Seschrock * nothing can be added to the list at this point. 408ea8dc4b6Seschrock */ 409ea8dc4b6Seschrock os.os = osi; 4101934e92fSmaybee (void) dmu_objset_evict_dbufs(&os); 411ea8dc4b6Seschrock 412fa9e4066Sahrens ASSERT3P(list_head(&osi->os_dnodes), ==, osi->os_meta_dnode); 413fa9e4066Sahrens ASSERT3P(list_tail(&osi->os_dnodes), ==, osi->os_meta_dnode); 414fa9e4066Sahrens ASSERT3P(list_head(&osi->os_meta_dnode->dn_dbufs), ==, NULL); 415fa9e4066Sahrens 416fa9e4066Sahrens dnode_special_close(osi->os_meta_dnode); 417fa9e4066Sahrens zil_free(osi->os_zil); 418fa9e4066Sahrens 419c717a561Smaybee VERIFY(arc_buf_remove_ref(osi->os_phys_buf, &osi->os_phys_buf) == 1); 4205ad82045Snd mutex_destroy(&osi->os_lock); 4215ad82045Snd mutex_destroy(&osi->os_obj_lock); 422f18faf3fSek mutex_destroy(&osi->os_user_ptr_lock); 423fa9e4066Sahrens kmem_free(osi, sizeof (objset_impl_t)); 424fa9e4066Sahrens } 425fa9e4066Sahrens 426fa9e4066Sahrens /* called from dsl for meta-objset */ 427fa9e4066Sahrens objset_impl_t * 428c717a561Smaybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, 429c717a561Smaybee dmu_objset_type_t type, dmu_tx_t *tx) 430fa9e4066Sahrens { 431fa9e4066Sahrens objset_impl_t *osi; 432fa9e4066Sahrens dnode_t *mdn; 433fa9e4066Sahrens 434fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 43591ebeef5Sahrens if (ds) 43691ebeef5Sahrens mutex_enter(&ds->ds_opening_lock); 437c717a561Smaybee VERIFY(0 == dmu_objset_open_impl(spa, ds, bp, &osi)); 43891ebeef5Sahrens if (ds) 43991ebeef5Sahrens mutex_exit(&ds->ds_opening_lock); 440fa9e4066Sahrens mdn = osi->os_meta_dnode; 441fa9e4066Sahrens 442fa9e4066Sahrens dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT, 443fa9e4066Sahrens DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx); 444fa9e4066Sahrens 445fa9e4066Sahrens /* 446fa9e4066Sahrens * We don't want to have to increase the meta-dnode's nlevels 447fa9e4066Sahrens * later, because then we could do it in quescing context while 448fa9e4066Sahrens * we are also accessing it in open context. 449fa9e4066Sahrens * 450fa9e4066Sahrens * This precaution is not necessary for the MOS (ds == NULL), 451fa9e4066Sahrens * because the MOS is only updated in syncing context. 452fa9e4066Sahrens * This is most fortunate: the MOS is the only objset that 453fa9e4066Sahrens * needs to be synced multiple times as spa_sync() iterates 454fa9e4066Sahrens * to convergence, so minimizing its dn_nlevels matters. 455fa9e4066Sahrens */ 456ea8dc4b6Seschrock if (ds != NULL) { 457ea8dc4b6Seschrock int levels = 1; 458ea8dc4b6Seschrock 459ea8dc4b6Seschrock /* 460ea8dc4b6Seschrock * Determine the number of levels necessary for the meta-dnode 461ea8dc4b6Seschrock * to contain DN_MAX_OBJECT dnodes. 462ea8dc4b6Seschrock */ 463ea8dc4b6Seschrock while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift + 464ea8dc4b6Seschrock (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) < 465ea8dc4b6Seschrock DN_MAX_OBJECT * sizeof (dnode_phys_t)) 466ea8dc4b6Seschrock levels++; 467ea8dc4b6Seschrock 468fa9e4066Sahrens mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] = 469ea8dc4b6Seschrock mdn->dn_nlevels = levels; 470ea8dc4b6Seschrock } 471fa9e4066Sahrens 472fa9e4066Sahrens ASSERT(type != DMU_OST_NONE); 473fa9e4066Sahrens ASSERT(type != DMU_OST_ANY); 474fa9e4066Sahrens ASSERT(type < DMU_OST_NUMTYPES); 475fa9e4066Sahrens osi->os_phys->os_type = type; 476fa9e4066Sahrens 477fa9e4066Sahrens dsl_dataset_dirty(ds, tx); 478fa9e4066Sahrens 479fa9e4066Sahrens return (osi); 480fa9e4066Sahrens } 481fa9e4066Sahrens 482fa9e4066Sahrens struct oscarg { 483ecd6cf80Smarks void (*userfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx); 484fa9e4066Sahrens void *userarg; 485fa9e4066Sahrens dsl_dataset_t *clone_parent; 486fa9e4066Sahrens const char *lastname; 487fa9e4066Sahrens dmu_objset_type_t type; 488ab04eb8eStimh uint64_t flags; 489fa9e4066Sahrens }; 490fa9e4066Sahrens 491ecd6cf80Smarks /*ARGSUSED*/ 492fa9e4066Sahrens static int 4931d452cf5Sahrens dmu_objset_create_check(void *arg1, void *arg2, dmu_tx_t *tx) 494fa9e4066Sahrens { 4951d452cf5Sahrens dsl_dir_t *dd = arg1; 4961d452cf5Sahrens struct oscarg *oa = arg2; 4971d452cf5Sahrens objset_t *mos = dd->dd_pool->dp_meta_objset; 498fa9e4066Sahrens int err; 4991d452cf5Sahrens uint64_t ddobj; 5001d452cf5Sahrens 5011d452cf5Sahrens err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj, 5021d452cf5Sahrens oa->lastname, sizeof (uint64_t), 1, &ddobj); 5031d452cf5Sahrens if (err != ENOENT) 5041d452cf5Sahrens return (err ? err : EEXIST); 5051d452cf5Sahrens 5061d452cf5Sahrens if (oa->clone_parent != NULL) { 5071d452cf5Sahrens /* 5081d452cf5Sahrens * You can't clone across pools. 5091d452cf5Sahrens */ 5101d452cf5Sahrens if (oa->clone_parent->ds_dir->dd_pool != dd->dd_pool) 5111d452cf5Sahrens return (EXDEV); 5121d452cf5Sahrens 5131d452cf5Sahrens /* 5141d452cf5Sahrens * You can only clone snapshots, not the head datasets. 5151d452cf5Sahrens */ 5161d452cf5Sahrens if (oa->clone_parent->ds_phys->ds_num_children == 0) 5171d452cf5Sahrens return (EINVAL); 5181d452cf5Sahrens } 519ecd6cf80Smarks 5201d452cf5Sahrens return (0); 5211d452cf5Sahrens } 5221d452cf5Sahrens 5231d452cf5Sahrens static void 524ecd6cf80Smarks dmu_objset_create_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 5251d452cf5Sahrens { 5261d452cf5Sahrens dsl_dir_t *dd = arg1; 5271d452cf5Sahrens struct oscarg *oa = arg2; 5281d452cf5Sahrens dsl_dataset_t *ds; 529c717a561Smaybee blkptr_t *bp; 5301d452cf5Sahrens uint64_t dsobj; 531fa9e4066Sahrens 532fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 533fa9e4066Sahrens 5341d452cf5Sahrens dsobj = dsl_dataset_create_sync(dd, oa->lastname, 535ab04eb8eStimh oa->clone_parent, oa->flags, cr, tx); 536fa9e4066Sahrens 537745cd3c5Smaybee VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, dsobj, FTAG, &ds)); 538c717a561Smaybee bp = dsl_dataset_get_blkptr(ds); 539c717a561Smaybee if (BP_IS_HOLE(bp)) { 540fa9e4066Sahrens objset_impl_t *osi; 541fa9e4066Sahrens 542fa9e4066Sahrens /* This is an empty dmu_objset; not a clone. */ 543fa9e4066Sahrens osi = dmu_objset_create_impl(dsl_dataset_get_spa(ds), 544c717a561Smaybee ds, bp, oa->type, tx); 545fa9e4066Sahrens 546fa9e4066Sahrens if (oa->userfunc) 547ecd6cf80Smarks oa->userfunc(&osi->os, oa->userarg, cr, tx); 548fa9e4066Sahrens } 549ecd6cf80Smarks 550ecd6cf80Smarks spa_history_internal_log(LOG_DS_CREATE, dd->dd_pool->dp_spa, 551ecd6cf80Smarks tx, cr, "dataset = %llu", dsobj); 552ecd6cf80Smarks 553745cd3c5Smaybee dsl_dataset_rele(ds, FTAG); 554fa9e4066Sahrens } 555fa9e4066Sahrens 556fa9e4066Sahrens int 557fa9e4066Sahrens dmu_objset_create(const char *name, dmu_objset_type_t type, 558ab04eb8eStimh objset_t *clone_parent, uint64_t flags, 559ecd6cf80Smarks void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg) 560fa9e4066Sahrens { 5611d452cf5Sahrens dsl_dir_t *pdd; 562fa9e4066Sahrens const char *tail; 563fa9e4066Sahrens int err = 0; 5641d452cf5Sahrens struct oscarg oa = { 0 }; 565fa9e4066Sahrens 5661d452cf5Sahrens ASSERT(strchr(name, '@') == NULL); 5671d452cf5Sahrens err = dsl_dir_open(name, FTAG, &pdd, &tail); 568ea8dc4b6Seschrock if (err) 569ea8dc4b6Seschrock return (err); 570fa9e4066Sahrens if (tail == NULL) { 5711d452cf5Sahrens dsl_dir_close(pdd, FTAG); 572fa9e4066Sahrens return (EEXIST); 573fa9e4066Sahrens } 574fa9e4066Sahrens 575fa9e4066Sahrens dprintf("name=%s\n", name); 576fa9e4066Sahrens 5771d452cf5Sahrens oa.userfunc = func; 5781d452cf5Sahrens oa.userarg = arg; 5791d452cf5Sahrens oa.lastname = tail; 5801d452cf5Sahrens oa.type = type; 581ab04eb8eStimh oa.flags = flags; 582ecd6cf80Smarks 5831d452cf5Sahrens if (clone_parent != NULL) { 584fa9e4066Sahrens /* 5851d452cf5Sahrens * You can't clone to a different type. 586fa9e4066Sahrens */ 5871d452cf5Sahrens if (clone_parent->os->os_phys->os_type != type) { 5881d452cf5Sahrens dsl_dir_close(pdd, FTAG); 5891d452cf5Sahrens return (EINVAL); 590fa9e4066Sahrens } 5911d452cf5Sahrens oa.clone_parent = clone_parent->os->os_dsl_dataset; 592fa9e4066Sahrens } 5931d452cf5Sahrens err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check, 5941d452cf5Sahrens dmu_objset_create_sync, pdd, &oa, 5); 5951d452cf5Sahrens dsl_dir_close(pdd, FTAG); 596fa9e4066Sahrens return (err); 597fa9e4066Sahrens } 598fa9e4066Sahrens 599fa9e4066Sahrens int 600fa9e4066Sahrens dmu_objset_destroy(const char *name) 601fa9e4066Sahrens { 602fa9e4066Sahrens objset_t *os; 603fa9e4066Sahrens int error; 604fa9e4066Sahrens 605fa9e4066Sahrens /* 606fa9e4066Sahrens * If it looks like we'll be able to destroy it, and there's 607fa9e4066Sahrens * an unplayed replay log sitting around, destroy the log. 608fa9e4066Sahrens * It would be nicer to do this in dsl_dataset_destroy_sync(), 609fa9e4066Sahrens * but the replay log objset is modified in open context. 610fa9e4066Sahrens */ 6113cb34c60Sahrens error = dmu_objset_open(name, DMU_OST_ANY, 612745cd3c5Smaybee DS_MODE_OWNER|DS_MODE_READONLY|DS_MODE_INCONSISTENT, &os); 613fa9e4066Sahrens if (error == 0) { 6143cb34c60Sahrens dsl_dataset_t *ds = os->os->os_dsl_dataset; 615d80c45e0Sbonwick zil_destroy(dmu_objset_zil(os), B_FALSE); 6163cb34c60Sahrens 617745cd3c5Smaybee error = dsl_dataset_destroy(ds, os); 6183cb34c60Sahrens /* 6193cb34c60Sahrens * dsl_dataset_destroy() closes the ds. 6203cb34c60Sahrens */ 6213cb34c60Sahrens kmem_free(os, sizeof (objset_t)); 622fa9e4066Sahrens } 623fa9e4066Sahrens 6243cb34c60Sahrens return (error); 625fa9e4066Sahrens } 626fa9e4066Sahrens 6274ccbb6e7Sahrens /* 6284ccbb6e7Sahrens * This will close the objset. 6294ccbb6e7Sahrens */ 630fa9e4066Sahrens int 6314ccbb6e7Sahrens dmu_objset_rollback(objset_t *os) 632fa9e4066Sahrens { 633fa9e4066Sahrens int err; 6343cb34c60Sahrens dsl_dataset_t *ds; 635fa9e4066Sahrens 6363cb34c60Sahrens ds = os->os->os_dsl_dataset; 6374ccbb6e7Sahrens 638745cd3c5Smaybee if (!dsl_dataset_tryown(ds, TRUE, os)) { 6394ccbb6e7Sahrens dmu_objset_close(os); 6404ccbb6e7Sahrens return (EBUSY); 6414ccbb6e7Sahrens } 6424ccbb6e7Sahrens 6433cb34c60Sahrens err = dsl_dataset_rollback(ds, os->os->os_phys->os_type); 6443a8a1de4Sperrin 6453cb34c60Sahrens /* 6463cb34c60Sahrens * NB: we close the objset manually because the rollback 6473cb34c60Sahrens * actually implicitly called dmu_objset_evict(), thus freeing 6483cb34c60Sahrens * the objset_impl_t. 6493cb34c60Sahrens */ 650745cd3c5Smaybee dsl_dataset_disown(ds, os); 6513cb34c60Sahrens kmem_free(os, sizeof (objset_t)); 652fa9e4066Sahrens return (err); 653fa9e4066Sahrens } 654fa9e4066Sahrens 6551d452cf5Sahrens struct snaparg { 6561d452cf5Sahrens dsl_sync_task_group_t *dstg; 6571d452cf5Sahrens char *snapname; 6581d452cf5Sahrens char failed[MAXPATHLEN]; 659ecd6cf80Smarks boolean_t checkperms; 6603cb34c60Sahrens list_t objsets; 6613cb34c60Sahrens }; 6623cb34c60Sahrens 6633cb34c60Sahrens struct osnode { 6643cb34c60Sahrens list_node_t node; 6653cb34c60Sahrens objset_t *os; 6661d452cf5Sahrens }; 6671d452cf5Sahrens 6681d452cf5Sahrens static int 6691d452cf5Sahrens dmu_objset_snapshot_one(char *name, void *arg) 6701d452cf5Sahrens { 6711d452cf5Sahrens struct snaparg *sn = arg; 6721d452cf5Sahrens objset_t *os; 6731d452cf5Sahrens int err; 6741d452cf5Sahrens 6751d452cf5Sahrens (void) strcpy(sn->failed, name); 6761d452cf5Sahrens 677ecd6cf80Smarks /* 678ecd6cf80Smarks * Check permissions only when requested. This only applies when 679ecd6cf80Smarks * doing a recursive snapshot. The permission checks for the starting 680ecd6cf80Smarks * dataset have already been performed in zfs_secpolicy_snapshot() 681ecd6cf80Smarks */ 682ecd6cf80Smarks if (sn->checkperms == B_TRUE && 683ecd6cf80Smarks (err = zfs_secpolicy_snapshot_perms(name, CRED()))) 684ecd6cf80Smarks return (err); 685ecd6cf80Smarks 686745cd3c5Smaybee err = dmu_objset_open(name, DMU_OST_ANY, DS_MODE_USER, &os); 6871d452cf5Sahrens if (err != 0) 6881d452cf5Sahrens return (err); 6891d452cf5Sahrens 690745cd3c5Smaybee /* If the objset is in an inconsistent state, return busy */ 691745cd3c5Smaybee if (os->os->os_dsl_dataset->ds_phys->ds_flags & DS_FLAG_INCONSISTENT) { 692f2e10be3Srm dmu_objset_close(os); 693f2e10be3Srm return (EBUSY); 694f2e10be3Srm } 695f2e10be3Srm 6961d452cf5Sahrens /* 6971d452cf5Sahrens * NB: we need to wait for all in-flight changes to get to disk, 6981d452cf5Sahrens * so that we snapshot those changes. zil_suspend does this as 6991d452cf5Sahrens * a side effect. 7001d452cf5Sahrens */ 7011d452cf5Sahrens err = zil_suspend(dmu_objset_zil(os)); 7021d452cf5Sahrens if (err == 0) { 7033cb34c60Sahrens struct osnode *osn; 7041d452cf5Sahrens dsl_sync_task_create(sn->dstg, dsl_dataset_snapshot_check, 7053cb34c60Sahrens dsl_dataset_snapshot_sync, os->os->os_dsl_dataset, 7063cb34c60Sahrens sn->snapname, 3); 7073cb34c60Sahrens osn = kmem_alloc(sizeof (struct osnode), KM_SLEEP); 7083cb34c60Sahrens osn->os = os; 7093cb34c60Sahrens list_insert_tail(&sn->objsets, osn); 710f2e10be3Srm } else { 711f2e10be3Srm dmu_objset_close(os); 7121d452cf5Sahrens } 713f2e10be3Srm 7141d452cf5Sahrens return (err); 7151d452cf5Sahrens } 7161d452cf5Sahrens 7171d452cf5Sahrens int 7181d452cf5Sahrens dmu_objset_snapshot(char *fsname, char *snapname, boolean_t recursive) 7191d452cf5Sahrens { 7201d452cf5Sahrens dsl_sync_task_t *dst; 7213cb34c60Sahrens struct osnode *osn; 7221d452cf5Sahrens struct snaparg sn = { 0 }; 7231d452cf5Sahrens spa_t *spa; 7241d452cf5Sahrens int err; 7251d452cf5Sahrens 7261d452cf5Sahrens (void) strcpy(sn.failed, fsname); 7271d452cf5Sahrens 72840feaa91Sahrens err = spa_open(fsname, &spa, FTAG); 7291d452cf5Sahrens if (err) 7301d452cf5Sahrens return (err); 7311d452cf5Sahrens 7321d452cf5Sahrens sn.dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 7331d452cf5Sahrens sn.snapname = snapname; 7343cb34c60Sahrens list_create(&sn.objsets, sizeof (struct osnode), 7353cb34c60Sahrens offsetof(struct osnode, node)); 7361d452cf5Sahrens 7370b69c2f0Sahrens if (recursive) { 738ecd6cf80Smarks sn.checkperms = B_TRUE; 7390b69c2f0Sahrens err = dmu_objset_find(fsname, 7400b69c2f0Sahrens dmu_objset_snapshot_one, &sn, DS_FIND_CHILDREN); 7410b69c2f0Sahrens } else { 742ecd6cf80Smarks sn.checkperms = B_FALSE; 7431d452cf5Sahrens err = dmu_objset_snapshot_one(fsname, &sn); 7440b69c2f0Sahrens } 7451d452cf5Sahrens 7461d452cf5Sahrens if (err) 7471d452cf5Sahrens goto out; 7481d452cf5Sahrens 7491d452cf5Sahrens err = dsl_sync_task_group_wait(sn.dstg); 7501d452cf5Sahrens 7511d452cf5Sahrens for (dst = list_head(&sn.dstg->dstg_tasks); dst; 7521d452cf5Sahrens dst = list_next(&sn.dstg->dstg_tasks, dst)) { 7533cb34c60Sahrens dsl_dataset_t *ds = dst->dst_arg1; 7541d452cf5Sahrens if (dst->dst_err) 7553cb34c60Sahrens dsl_dataset_name(ds, sn.failed); 7563cb34c60Sahrens } 7573cb34c60Sahrens 758a4585abfSahrens out: 7593cb34c60Sahrens while (osn = list_head(&sn.objsets)) { 7603cb34c60Sahrens list_remove(&sn.objsets, osn); 7613cb34c60Sahrens zil_resume(dmu_objset_zil(osn->os)); 7623cb34c60Sahrens dmu_objset_close(osn->os); 7633cb34c60Sahrens kmem_free(osn, sizeof (struct osnode)); 7641d452cf5Sahrens } 7653cb34c60Sahrens list_destroy(&sn.objsets); 766a4585abfSahrens 7671d452cf5Sahrens if (err) 7681d452cf5Sahrens (void) strcpy(fsname, sn.failed); 7691d452cf5Sahrens dsl_sync_task_group_destroy(sn.dstg); 7701d452cf5Sahrens spa_close(spa, FTAG); 7711d452cf5Sahrens return (err); 7721d452cf5Sahrens } 7731d452cf5Sahrens 774fa9e4066Sahrens static void 775c717a561Smaybee dmu_objset_sync_dnodes(list_t *list, dmu_tx_t *tx) 776fa9e4066Sahrens { 777c717a561Smaybee dnode_t *dn; 778faafa6e3Sahrens 779c717a561Smaybee while (dn = list_head(list)) { 780c717a561Smaybee ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT); 781c717a561Smaybee ASSERT(dn->dn_dbuf->db_data_pending); 782c717a561Smaybee /* 783c717a561Smaybee * Initialize dn_zio outside dnode_sync() 784c717a561Smaybee * to accomodate meta-dnode 785c717a561Smaybee */ 786c717a561Smaybee dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio; 787c717a561Smaybee ASSERT(dn->dn_zio); 788faafa6e3Sahrens 789c717a561Smaybee ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS); 790c717a561Smaybee list_remove(list, dn); 791c717a561Smaybee dnode_sync(dn, tx); 792fa9e4066Sahrens } 793fa9e4066Sahrens } 794fa9e4066Sahrens 795fa9e4066Sahrens /* ARGSUSED */ 796fa9e4066Sahrens static void 797c717a561Smaybee ready(zio_t *zio, arc_buf_t *abuf, void *arg) 798fa9e4066Sahrens { 799fa9e4066Sahrens objset_impl_t *os = arg; 800c717a561Smaybee blkptr_t *bp = os->os_rootbp; 801c717a561Smaybee dnode_phys_t *dnp = &os->os_phys->os_meta_dnode; 802fa9e4066Sahrens int i; 803fa9e4066Sahrens 8040a4e9518Sgw ASSERT(bp == zio->io_bp); 8050a4e9518Sgw 806fa9e4066Sahrens /* 807fa9e4066Sahrens * Update rootbp fill count. 808fa9e4066Sahrens */ 809c717a561Smaybee bp->blk_fill = 1; /* count the meta-dnode */ 810fa9e4066Sahrens for (i = 0; i < dnp->dn_nblkptr; i++) 811c717a561Smaybee bp->blk_fill += dnp->dn_blkptr[i].blk_fill; 8120a4e9518Sgw 8130a4e9518Sgw BP_SET_TYPE(bp, DMU_OT_OBJSET); 8140a4e9518Sgw BP_SET_LEVEL(bp, 0); 8150a4e9518Sgw 8160a4e9518Sgw /* We must do this after we've set the bp's type and level */ 8170a4e9518Sgw if (!DVA_EQUAL(BP_IDENTITY(bp), 8180a4e9518Sgw BP_IDENTITY(&zio->io_bp_orig))) { 8190a4e9518Sgw if (zio->io_bp_orig.blk_birth == os->os_synctx->tx_txg) 820cdb0ab79Smaybee (void) dsl_dataset_block_kill(os->os_dsl_dataset, 8210a4e9518Sgw &zio->io_bp_orig, NULL, os->os_synctx); 8220a4e9518Sgw dsl_dataset_block_born(os->os_dsl_dataset, bp, os->os_synctx); 8230a4e9518Sgw } 824c717a561Smaybee } 825c717a561Smaybee 826fa9e4066Sahrens /* called from dsl */ 827fa9e4066Sahrens void 828c717a561Smaybee dmu_objset_sync(objset_impl_t *os, zio_t *pio, dmu_tx_t *tx) 829fa9e4066Sahrens { 830fa9e4066Sahrens int txgoff; 831ea8dc4b6Seschrock zbookmark_t zb; 832*088f3894Sahrens writeprops_t wp = { 0 }; 833c717a561Smaybee zio_t *zio; 834c717a561Smaybee list_t *list; 835c717a561Smaybee dbuf_dirty_record_t *dr; 836c717a561Smaybee 837c717a561Smaybee dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg); 838fa9e4066Sahrens 839fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 840fa9e4066Sahrens /* XXX the write_done callback should really give us the tx... */ 841fa9e4066Sahrens os->os_synctx = tx; 842fa9e4066Sahrens 84387bd5c1eSahrens if (os->os_dsl_dataset == NULL) { 84487bd5c1eSahrens /* 84587bd5c1eSahrens * This is the MOS. If we have upgraded, 84687bd5c1eSahrens * spa_max_replication() could change, so reset 84787bd5c1eSahrens * os_copies here. 84887bd5c1eSahrens */ 84987bd5c1eSahrens os->os_copies = spa_max_replication(os->os_spa); 85087bd5c1eSahrens } 85187bd5c1eSahrens 852fa9e4066Sahrens /* 853c717a561Smaybee * Create the root block IO 854fa9e4066Sahrens */ 855ea8dc4b6Seschrock zb.zb_objset = os->os_dsl_dataset ? os->os_dsl_dataset->ds_object : 0; 856ea8dc4b6Seschrock zb.zb_object = 0; 857ea8dc4b6Seschrock zb.zb_level = -1; 858ea8dc4b6Seschrock zb.zb_blkid = 0; 85991ebeef5Sahrens if (BP_IS_OLDER(os->os_rootbp, tx->tx_txg)) { 860cdb0ab79Smaybee (void) dsl_dataset_block_kill(os->os_dsl_dataset, 861c717a561Smaybee os->os_rootbp, pio, tx); 86291ebeef5Sahrens } 863*088f3894Sahrens wp.wp_type = DMU_OT_OBJSET; 864*088f3894Sahrens wp.wp_copies = os->os_copies; 865*088f3894Sahrens wp.wp_level = (uint8_t)-1; 866*088f3894Sahrens wp.wp_oschecksum = os->os_checksum; 867*088f3894Sahrens wp.wp_oscompress = os->os_compress; 868*088f3894Sahrens arc_release(os->os_phys_buf, &os->os_phys_buf); 869*088f3894Sahrens zio = arc_write(pio, os->os_spa, &wp, 870*088f3894Sahrens tx->tx_txg, os->os_rootbp, os->os_phys_buf, ready, NULL, os, 871fdb2e906Sek ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED | ZIO_FLAG_METADATA, 872fdb2e906Sek &zb); 873c717a561Smaybee 874c717a561Smaybee /* 875c717a561Smaybee * Sync meta-dnode - the parent IO for the sync is the root block 876c717a561Smaybee */ 877c717a561Smaybee os->os_meta_dnode->dn_zio = zio; 878c717a561Smaybee dnode_sync(os->os_meta_dnode, tx); 879c717a561Smaybee 880c717a561Smaybee txgoff = tx->tx_txg & TXG_MASK; 881fa9e4066Sahrens 882c717a561Smaybee dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], tx); 883c717a561Smaybee dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], tx); 884fa9e4066Sahrens 885c717a561Smaybee list = &os->os_meta_dnode->dn_dirty_records[txgoff]; 886c717a561Smaybee while (dr = list_head(list)) { 887c717a561Smaybee ASSERT(dr->dr_dbuf->db_level == 0); 888c717a561Smaybee list_remove(list, dr); 889c717a561Smaybee if (dr->dr_zio) 890c717a561Smaybee zio_nowait(dr->dr_zio); 891c717a561Smaybee } 892c717a561Smaybee /* 893c717a561Smaybee * Free intent log blocks up to this tx. 894c717a561Smaybee */ 895c717a561Smaybee zil_sync(os->os_zil, tx); 896*088f3894Sahrens os->os_phys->os_zil_header = os->os_zil_header; 897c717a561Smaybee zio_nowait(zio); 898fa9e4066Sahrens } 899fa9e4066Sahrens 900fa9e4066Sahrens void 901a2eea2e1Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp, 902a2eea2e1Sahrens uint64_t *usedobjsp, uint64_t *availobjsp) 903fa9e4066Sahrens { 904a2eea2e1Sahrens dsl_dataset_space(os->os->os_dsl_dataset, refdbytesp, availbytesp, 905a2eea2e1Sahrens usedobjsp, availobjsp); 906a2eea2e1Sahrens } 907a2eea2e1Sahrens 908a2eea2e1Sahrens uint64_t 909a2eea2e1Sahrens dmu_objset_fsid_guid(objset_t *os) 910a2eea2e1Sahrens { 911a2eea2e1Sahrens return (dsl_dataset_fsid_guid(os->os->os_dsl_dataset)); 912a2eea2e1Sahrens } 913a2eea2e1Sahrens 914a2eea2e1Sahrens void 915a2eea2e1Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat) 916a2eea2e1Sahrens { 917a2eea2e1Sahrens stat->dds_type = os->os->os_phys->os_type; 918a2eea2e1Sahrens if (os->os->os_dsl_dataset) 919a2eea2e1Sahrens dsl_dataset_fast_stat(os->os->os_dsl_dataset, stat); 920a2eea2e1Sahrens } 921a2eea2e1Sahrens 922a2eea2e1Sahrens void 923a2eea2e1Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv) 924a2eea2e1Sahrens { 925a2eea2e1Sahrens ASSERT(os->os->os_dsl_dataset || 926a2eea2e1Sahrens os->os->os_phys->os_type == DMU_OST_META); 927a2eea2e1Sahrens 928a2eea2e1Sahrens if (os->os->os_dsl_dataset != NULL) 929a2eea2e1Sahrens dsl_dataset_stats(os->os->os_dsl_dataset, nv); 930a2eea2e1Sahrens 931a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE, 932a2eea2e1Sahrens os->os->os_phys->os_type); 933fa9e4066Sahrens } 934fa9e4066Sahrens 935fa9e4066Sahrens int 936fa9e4066Sahrens dmu_objset_is_snapshot(objset_t *os) 937fa9e4066Sahrens { 938fa9e4066Sahrens if (os->os->os_dsl_dataset != NULL) 939fa9e4066Sahrens return (dsl_dataset_is_snapshot(os->os->os_dsl_dataset)); 940fa9e4066Sahrens else 941fa9e4066Sahrens return (B_FALSE); 942fa9e4066Sahrens } 943fa9e4066Sahrens 944ab04eb8eStimh int 945ab04eb8eStimh dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen, 946ab04eb8eStimh boolean_t *conflict) 947ab04eb8eStimh { 948ab04eb8eStimh dsl_dataset_t *ds = os->os->os_dsl_dataset; 949ab04eb8eStimh uint64_t ignored; 950ab04eb8eStimh 951ab04eb8eStimh if (ds->ds_phys->ds_snapnames_zapobj == 0) 952ab04eb8eStimh return (ENOENT); 953ab04eb8eStimh 954ab04eb8eStimh return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset, 955ab04eb8eStimh ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST, 956ab04eb8eStimh real, maxlen, conflict)); 957ab04eb8eStimh } 958ab04eb8eStimh 959fa9e4066Sahrens int 960fa9e4066Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name, 961b38f0970Sck uint64_t *idp, uint64_t *offp, boolean_t *case_conflict) 962fa9e4066Sahrens { 963fa9e4066Sahrens dsl_dataset_t *ds = os->os->os_dsl_dataset; 964fa9e4066Sahrens zap_cursor_t cursor; 965fa9e4066Sahrens zap_attribute_t attr; 966fa9e4066Sahrens 967fa9e4066Sahrens if (ds->ds_phys->ds_snapnames_zapobj == 0) 968fa9e4066Sahrens return (ENOENT); 969fa9e4066Sahrens 970fa9e4066Sahrens zap_cursor_init_serialized(&cursor, 971fa9e4066Sahrens ds->ds_dir->dd_pool->dp_meta_objset, 972fa9e4066Sahrens ds->ds_phys->ds_snapnames_zapobj, *offp); 973fa9e4066Sahrens 97487e5029aSahrens if (zap_cursor_retrieve(&cursor, &attr) != 0) { 97587e5029aSahrens zap_cursor_fini(&cursor); 97687e5029aSahrens return (ENOENT); 97787e5029aSahrens } 97887e5029aSahrens 97987e5029aSahrens if (strlen(attr.za_name) + 1 > namelen) { 98087e5029aSahrens zap_cursor_fini(&cursor); 98187e5029aSahrens return (ENAMETOOLONG); 98287e5029aSahrens } 98387e5029aSahrens 98487e5029aSahrens (void) strcpy(name, attr.za_name); 98587e5029aSahrens if (idp) 98687e5029aSahrens *idp = attr.za_first_integer; 987b38f0970Sck if (case_conflict) 988b38f0970Sck *case_conflict = attr.za_normalization_conflict; 98987e5029aSahrens zap_cursor_advance(&cursor); 99087e5029aSahrens *offp = zap_cursor_serialize(&cursor); 99187e5029aSahrens zap_cursor_fini(&cursor); 99287e5029aSahrens 99387e5029aSahrens return (0); 99487e5029aSahrens } 99587e5029aSahrens 99687e5029aSahrens int 99787e5029aSahrens dmu_dir_list_next(objset_t *os, int namelen, char *name, 99887e5029aSahrens uint64_t *idp, uint64_t *offp) 99987e5029aSahrens { 100087e5029aSahrens dsl_dir_t *dd = os->os->os_dsl_dataset->ds_dir; 100187e5029aSahrens zap_cursor_t cursor; 100287e5029aSahrens zap_attribute_t attr; 100387e5029aSahrens 100487e5029aSahrens /* there is no next dir on a snapshot! */ 100587e5029aSahrens if (os->os->os_dsl_dataset->ds_object != 100687e5029aSahrens dd->dd_phys->dd_head_dataset_obj) 1007fa9e4066Sahrens return (ENOENT); 1008fa9e4066Sahrens 100987e5029aSahrens zap_cursor_init_serialized(&cursor, 101087e5029aSahrens dd->dd_pool->dp_meta_objset, 101187e5029aSahrens dd->dd_phys->dd_child_dir_zapobj, *offp); 101287e5029aSahrens 101387e5029aSahrens if (zap_cursor_retrieve(&cursor, &attr) != 0) { 101487e5029aSahrens zap_cursor_fini(&cursor); 101587e5029aSahrens return (ENOENT); 101687e5029aSahrens } 101787e5029aSahrens 101887e5029aSahrens if (strlen(attr.za_name) + 1 > namelen) { 101987e5029aSahrens zap_cursor_fini(&cursor); 1020fa9e4066Sahrens return (ENAMETOOLONG); 102187e5029aSahrens } 1022fa9e4066Sahrens 1023fa9e4066Sahrens (void) strcpy(name, attr.za_name); 102487e5029aSahrens if (idp) 102587e5029aSahrens *idp = attr.za_first_integer; 1026fa9e4066Sahrens zap_cursor_advance(&cursor); 1027fa9e4066Sahrens *offp = zap_cursor_serialize(&cursor); 102887e5029aSahrens zap_cursor_fini(&cursor); 1029fa9e4066Sahrens 1030fa9e4066Sahrens return (0); 1031fa9e4066Sahrens } 1032fa9e4066Sahrens 1033*088f3894Sahrens struct findarg { 1034*088f3894Sahrens int (*func)(char *, void *); 1035*088f3894Sahrens void *arg; 1036*088f3894Sahrens }; 1037*088f3894Sahrens 1038*088f3894Sahrens /* ARGSUSED */ 1039*088f3894Sahrens static int 1040*088f3894Sahrens findfunc(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg) 1041*088f3894Sahrens { 1042*088f3894Sahrens struct findarg *fa = arg; 1043*088f3894Sahrens return (fa->func((char *)dsname, fa->arg)); 1044*088f3894Sahrens } 1045*088f3894Sahrens 1046fa9e4066Sahrens /* 1047fa9e4066Sahrens * Find all objsets under name, and for each, call 'func(child_name, arg)'. 1048*088f3894Sahrens * Perhaps change all callers to use dmu_objset_find_spa()? 1049fa9e4066Sahrens */ 10501d452cf5Sahrens int 10511d452cf5Sahrens dmu_objset_find(char *name, int func(char *, void *), void *arg, int flags) 1052*088f3894Sahrens { 1053*088f3894Sahrens struct findarg fa; 1054*088f3894Sahrens fa.func = func; 1055*088f3894Sahrens fa.arg = arg; 1056*088f3894Sahrens return (dmu_objset_find_spa(NULL, name, findfunc, &fa, flags)); 1057*088f3894Sahrens } 1058*088f3894Sahrens 1059*088f3894Sahrens /* 1060*088f3894Sahrens * Find all objsets under name, call func on each 1061*088f3894Sahrens */ 1062*088f3894Sahrens int 1063*088f3894Sahrens dmu_objset_find_spa(spa_t *spa, const char *name, 1064*088f3894Sahrens int func(spa_t *, uint64_t, const char *, void *), void *arg, int flags) 1065fa9e4066Sahrens { 1066fa9e4066Sahrens dsl_dir_t *dd; 1067*088f3894Sahrens dsl_pool_t *dp; 1068*088f3894Sahrens dsl_dataset_t *ds; 1069fa9e4066Sahrens zap_cursor_t zc; 1070b7661cccSmmusante zap_attribute_t *attr; 1071fa9e4066Sahrens char *child; 1072*088f3894Sahrens uint64_t thisobj; 1073*088f3894Sahrens int err; 1074fa9e4066Sahrens 1075*088f3894Sahrens if (name == NULL) 1076*088f3894Sahrens name = spa_name(spa); 1077*088f3894Sahrens err = dsl_dir_open_spa(spa, name, FTAG, &dd, NULL); 1078ea8dc4b6Seschrock if (err) 10791d452cf5Sahrens return (err); 1080fa9e4066Sahrens 1081*088f3894Sahrens /* Don't visit hidden ($MOS & $ORIGIN) objsets. */ 1082*088f3894Sahrens if (dd->dd_myname[0] == '$') { 1083*088f3894Sahrens dsl_dir_close(dd, FTAG); 1084*088f3894Sahrens return (0); 1085*088f3894Sahrens } 1086*088f3894Sahrens 1087*088f3894Sahrens thisobj = dd->dd_phys->dd_head_dataset_obj; 1088b7661cccSmmusante attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); 1089*088f3894Sahrens dp = dd->dd_pool; 1090fa9e4066Sahrens 1091fa9e4066Sahrens /* 1092fa9e4066Sahrens * Iterate over all children. 1093fa9e4066Sahrens */ 10940b69c2f0Sahrens if (flags & DS_FIND_CHILDREN) { 1095*088f3894Sahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, 10960b69c2f0Sahrens dd->dd_phys->dd_child_dir_zapobj); 1097b7661cccSmmusante zap_cursor_retrieve(&zc, attr) == 0; 10980b69c2f0Sahrens (void) zap_cursor_advance(&zc)) { 1099b7661cccSmmusante ASSERT(attr->za_integer_length == sizeof (uint64_t)); 1100b7661cccSmmusante ASSERT(attr->za_num_integers == 1); 1101fa9e4066Sahrens 11020b69c2f0Sahrens child = kmem_alloc(MAXPATHLEN, KM_SLEEP); 1103*088f3894Sahrens (void) strcpy(child, name); 11040b69c2f0Sahrens (void) strcat(child, "/"); 1105b7661cccSmmusante (void) strcat(child, attr->za_name); 1106*088f3894Sahrens err = dmu_objset_find_spa(spa, child, func, arg, flags); 11070b69c2f0Sahrens kmem_free(child, MAXPATHLEN); 11080b69c2f0Sahrens if (err) 11090b69c2f0Sahrens break; 11100b69c2f0Sahrens } 11110b69c2f0Sahrens zap_cursor_fini(&zc); 11121d452cf5Sahrens 11130b69c2f0Sahrens if (err) { 11140b69c2f0Sahrens dsl_dir_close(dd, FTAG); 1115b7661cccSmmusante kmem_free(attr, sizeof (zap_attribute_t)); 11160b69c2f0Sahrens return (err); 11170b69c2f0Sahrens } 1118fa9e4066Sahrens } 1119fa9e4066Sahrens 1120fa9e4066Sahrens /* 1121fa9e4066Sahrens * Iterate over all snapshots. 1122fa9e4066Sahrens */ 1123*088f3894Sahrens if (flags & DS_FIND_SNAPSHOTS) { 1124*088f3894Sahrens if (!dsl_pool_sync_context(dp)) 1125*088f3894Sahrens rw_enter(&dp->dp_config_rwlock, RW_READER); 1126*088f3894Sahrens err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds); 1127*088f3894Sahrens if (!dsl_pool_sync_context(dp)) 1128*088f3894Sahrens rw_exit(&dp->dp_config_rwlock); 1129*088f3894Sahrens 1130*088f3894Sahrens if (err == 0) { 1131*088f3894Sahrens uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj; 1132*088f3894Sahrens dsl_dataset_rele(ds, FTAG); 1133*088f3894Sahrens 1134*088f3894Sahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj); 1135*088f3894Sahrens zap_cursor_retrieve(&zc, attr) == 0; 1136*088f3894Sahrens (void) zap_cursor_advance(&zc)) { 1137*088f3894Sahrens ASSERT(attr->za_integer_length == 1138*088f3894Sahrens sizeof (uint64_t)); 1139*088f3894Sahrens ASSERT(attr->za_num_integers == 1); 1140*088f3894Sahrens 1141*088f3894Sahrens child = kmem_alloc(MAXPATHLEN, KM_SLEEP); 1142*088f3894Sahrens (void) strcpy(child, name); 1143*088f3894Sahrens (void) strcat(child, "@"); 1144*088f3894Sahrens (void) strcat(child, attr->za_name); 1145*088f3894Sahrens err = func(spa, attr->za_first_integer, 1146*088f3894Sahrens child, arg); 1147*088f3894Sahrens kmem_free(child, MAXPATHLEN); 1148*088f3894Sahrens if (err) 1149*088f3894Sahrens break; 1150*088f3894Sahrens } 1151*088f3894Sahrens zap_cursor_fini(&zc); 1152fa9e4066Sahrens } 1153fa9e4066Sahrens } 1154fa9e4066Sahrens 1155fa9e4066Sahrens dsl_dir_close(dd, FTAG); 1156b7661cccSmmusante kmem_free(attr, sizeof (zap_attribute_t)); 1157fa9e4066Sahrens 11581d452cf5Sahrens if (err) 11591d452cf5Sahrens return (err); 11601d452cf5Sahrens 1161fa9e4066Sahrens /* 1162fa9e4066Sahrens * Apply to self if appropriate. 1163fa9e4066Sahrens */ 1164*088f3894Sahrens err = func(spa, thisobj, name, arg); 11651d452cf5Sahrens return (err); 1166fa9e4066Sahrens } 1167f18faf3fSek 1168f18faf3fSek void 1169f18faf3fSek dmu_objset_set_user(objset_t *os, void *user_ptr) 1170f18faf3fSek { 1171f18faf3fSek ASSERT(MUTEX_HELD(&os->os->os_user_ptr_lock)); 1172f18faf3fSek os->os->os_user_ptr = user_ptr; 1173f18faf3fSek } 1174f18faf3fSek 1175f18faf3fSek void * 1176f18faf3fSek dmu_objset_get_user(objset_t *os) 1177f18faf3fSek { 1178f18faf3fSek ASSERT(MUTEX_HELD(&os->os->os_user_ptr_lock)); 1179f18faf3fSek return (os->os->os_user_ptr); 1180f18faf3fSek } 1181