1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51544Seschrock * Common Development and Distribution License (the "License"). 61544Seschrock * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 21789Sahrens /* 223547Smaybee * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 27789Sahrens 28789Sahrens #include <sys/dsl_pool.h> 29789Sahrens #include <sys/dsl_dataset.h> 30789Sahrens #include <sys/dsl_dir.h> 312199Sahrens #include <sys/dsl_synctask.h> 32789Sahrens #include <sys/dmu_tx.h> 33789Sahrens #include <sys/dmu_objset.h> 34789Sahrens #include <sys/arc.h> 35789Sahrens #include <sys/zap.h> 363547Smaybee #include <sys/zio.h> 37789Sahrens #include <sys/zfs_context.h> 38789Sahrens #include <sys/fs/zfs.h> 39789Sahrens 401544Seschrock static int 411544Seschrock dsl_pool_open_mos_dir(dsl_pool_t *dp, dsl_dir_t **ddp) 42789Sahrens { 43789Sahrens uint64_t obj; 44789Sahrens int err; 45789Sahrens 46789Sahrens err = zap_lookup(dp->dp_meta_objset, 47789Sahrens dp->dp_root_dir->dd_phys->dd_child_dir_zapobj, 48789Sahrens MOS_DIR_NAME, sizeof (obj), 1, &obj); 491544Seschrock if (err) 501544Seschrock return (err); 51789Sahrens 521544Seschrock return (dsl_dir_open_obj(dp, obj, MOS_DIR_NAME, dp, ddp)); 53789Sahrens } 54789Sahrens 55789Sahrens static dsl_pool_t * 56789Sahrens dsl_pool_open_impl(spa_t *spa, uint64_t txg) 57789Sahrens { 58789Sahrens dsl_pool_t *dp; 59789Sahrens blkptr_t *bp = spa_get_rootblkptr(spa); 60789Sahrens 61789Sahrens dp = kmem_zalloc(sizeof (dsl_pool_t), KM_SLEEP); 62789Sahrens dp->dp_spa = spa; 63789Sahrens dp->dp_meta_rootbp = *bp; 642856Snd150628 rw_init(&dp->dp_config_rwlock, NULL, RW_DEFAULT, NULL); 65789Sahrens txg_init(dp, txg); 66789Sahrens 67789Sahrens txg_list_create(&dp->dp_dirty_datasets, 68789Sahrens offsetof(dsl_dataset_t, ds_dirty_link)); 69789Sahrens txg_list_create(&dp->dp_dirty_dirs, 70789Sahrens offsetof(dsl_dir_t, dd_dirty_link)); 712199Sahrens txg_list_create(&dp->dp_sync_tasks, 722199Sahrens offsetof(dsl_sync_task_group_t, dstg_node)); 73789Sahrens list_create(&dp->dp_synced_objsets, sizeof (dsl_dataset_t), 74789Sahrens offsetof(dsl_dataset_t, ds_synced_link)); 75789Sahrens 76789Sahrens return (dp); 77789Sahrens } 78789Sahrens 791544Seschrock int 801544Seschrock dsl_pool_open(spa_t *spa, uint64_t txg, dsl_pool_t **dpp) 81789Sahrens { 82789Sahrens int err; 83789Sahrens dsl_pool_t *dp = dsl_pool_open_impl(spa, txg); 841544Seschrock objset_impl_t *osi; 85789Sahrens 86789Sahrens rw_enter(&dp->dp_config_rwlock, RW_READER); 871544Seschrock err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp, &osi); 881544Seschrock if (err) 891544Seschrock goto out; 901544Seschrock dp->dp_meta_objset = &osi->os; 911544Seschrock 92789Sahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 93789Sahrens DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, 94789Sahrens &dp->dp_root_dir_obj); 951544Seschrock if (err) 961544Seschrock goto out; 971544Seschrock 981544Seschrock err = dsl_dir_open_obj(dp, dp->dp_root_dir_obj, 991544Seschrock NULL, dp, &dp->dp_root_dir); 1001544Seschrock if (err) 1011544Seschrock goto out; 102789Sahrens 1031544Seschrock err = dsl_pool_open_mos_dir(dp, &dp->dp_mos_dir); 1041544Seschrock if (err) 1051544Seschrock goto out; 1061544Seschrock 1071544Seschrock out: 108789Sahrens rw_exit(&dp->dp_config_rwlock); 1091544Seschrock if (err) 1101544Seschrock dsl_pool_close(dp); 1111544Seschrock else 1121544Seschrock *dpp = dp; 113789Sahrens 1141544Seschrock return (err); 115789Sahrens } 116789Sahrens 117789Sahrens void 118789Sahrens dsl_pool_close(dsl_pool_t *dp) 119789Sahrens { 120789Sahrens /* drop our reference from dsl_pool_open() */ 1211544Seschrock if (dp->dp_mos_dir) 1221544Seschrock dsl_dir_close(dp->dp_mos_dir, dp); 1231544Seschrock if (dp->dp_root_dir) 1241544Seschrock dsl_dir_close(dp->dp_root_dir, dp); 125789Sahrens 126789Sahrens /* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */ 1271544Seschrock if (dp->dp_meta_objset) 1281544Seschrock dmu_objset_evict(NULL, dp->dp_meta_objset->os); 129789Sahrens 130789Sahrens txg_list_destroy(&dp->dp_dirty_datasets); 131789Sahrens txg_list_destroy(&dp->dp_dirty_dirs); 132789Sahrens list_destroy(&dp->dp_synced_objsets); 133789Sahrens 134789Sahrens arc_flush(); 135789Sahrens txg_fini(dp); 1362856Snd150628 rw_destroy(&dp->dp_config_rwlock); 137789Sahrens kmem_free(dp, sizeof (dsl_pool_t)); 138789Sahrens } 139789Sahrens 140789Sahrens dsl_pool_t * 141789Sahrens dsl_pool_create(spa_t *spa, uint64_t txg) 142789Sahrens { 143789Sahrens int err; 144789Sahrens dsl_pool_t *dp = dsl_pool_open_impl(spa, txg); 145789Sahrens dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg); 146789Sahrens dp->dp_meta_objset = &dmu_objset_create_impl(spa, 1473547Smaybee NULL, &dp->dp_meta_rootbp, DMU_OST_META, tx)->os; 148789Sahrens 149789Sahrens /* create the pool directory */ 150789Sahrens err = zap_create_claim(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 151789Sahrens DMU_OT_OBJECT_DIRECTORY, DMU_OT_NONE, 0, tx); 152789Sahrens ASSERT3U(err, ==, 0); 153789Sahrens 154789Sahrens /* create and open the root dir */ 155789Sahrens dsl_dataset_create_root(dp, &dp->dp_root_dir_obj, tx); 1561544Seschrock VERIFY(0 == dsl_dir_open_obj(dp, dp->dp_root_dir_obj, 1571544Seschrock NULL, dp, &dp->dp_root_dir)); 158789Sahrens 159789Sahrens /* create and open the meta-objset dir */ 1602199Sahrens (void) dsl_dir_create_sync(dp->dp_root_dir, MOS_DIR_NAME, tx); 1611544Seschrock VERIFY(0 == dsl_pool_open_mos_dir(dp, &dp->dp_mos_dir)); 162789Sahrens 163789Sahrens dmu_tx_commit(tx); 164789Sahrens 165789Sahrens return (dp); 166789Sahrens } 167789Sahrens 168789Sahrens void 169789Sahrens dsl_pool_sync(dsl_pool_t *dp, uint64_t txg) 170789Sahrens { 1713547Smaybee zio_t *zio; 172789Sahrens dmu_tx_t *tx; 1733547Smaybee dsl_dir_t *dd; 1743547Smaybee dsl_dataset_t *ds; 1753547Smaybee dsl_sync_task_group_t *dstg; 176789Sahrens objset_impl_t *mosi = dp->dp_meta_objset->os; 1773547Smaybee int err; 178789Sahrens 179789Sahrens tx = dmu_tx_create_assigned(dp, txg); 180789Sahrens 1813547Smaybee zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 1823547Smaybee while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) { 1833547Smaybee if (!list_link_active(&ds->ds_synced_link)) 1843547Smaybee list_insert_tail(&dp->dp_synced_objsets, ds); 185*3897Smaybee else 186*3897Smaybee dmu_buf_rele(ds->ds_dbuf, ds); 1873547Smaybee dsl_dataset_sync(ds, zio, tx); 1883547Smaybee } 1893547Smaybee err = zio_wait(zio); 1903547Smaybee ASSERT(err == 0); 191789Sahrens 1923547Smaybee while (dstg = txg_list_remove(&dp->dp_sync_tasks, txg)) 1933547Smaybee dsl_sync_task_group_sync(dstg, tx); 1943547Smaybee while (dd = txg_list_remove(&dp->dp_dirty_dirs, txg)) 1953547Smaybee dsl_dir_sync(dd, tx); 196789Sahrens 197789Sahrens if (list_head(&mosi->os_dirty_dnodes[txg & TXG_MASK]) != NULL || 198789Sahrens list_head(&mosi->os_free_dnodes[txg & TXG_MASK]) != NULL) { 1993547Smaybee zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 2003547Smaybee dmu_objset_sync(mosi, zio, tx); 2013547Smaybee err = zio_wait(zio); 2023547Smaybee ASSERT(err == 0); 203789Sahrens dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", ""); 204789Sahrens spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp); 205789Sahrens } 206789Sahrens 207789Sahrens dmu_tx_commit(tx); 208789Sahrens } 209789Sahrens 210789Sahrens void 211789Sahrens dsl_pool_zil_clean(dsl_pool_t *dp) 212789Sahrens { 213789Sahrens dsl_dataset_t *ds; 214789Sahrens 215789Sahrens while (ds = list_head(&dp->dp_synced_objsets)) { 216789Sahrens list_remove(&dp->dp_synced_objsets, ds); 217789Sahrens ASSERT(ds->ds_user_ptr != NULL); 218789Sahrens zil_clean(((objset_impl_t *)ds->ds_user_ptr)->os_zil); 219*3897Smaybee dmu_buf_rele(ds->ds_dbuf, ds); 220789Sahrens } 221789Sahrens } 222789Sahrens 2233547Smaybee /* 2243547Smaybee * TRUE if the current thread is the tx_sync_thread or if we 2253547Smaybee * are being called from SPA context during pool initialization. 2263547Smaybee */ 227789Sahrens int 228789Sahrens dsl_pool_sync_context(dsl_pool_t *dp) 229789Sahrens { 230789Sahrens return (curthread == dp->dp_tx.tx_sync_thread || 2313547Smaybee spa_get_dsl(dp->dp_spa) == NULL); 232789Sahrens } 233789Sahrens 234789Sahrens uint64_t 235789Sahrens dsl_pool_adjustedsize(dsl_pool_t *dp, boolean_t netfree) 236789Sahrens { 237789Sahrens uint64_t space, resv; 238789Sahrens 239789Sahrens /* 2401775Sbillm * Reserve about 1.6% (1/64), or at least 32MB, for allocation 241789Sahrens * efficiency. 242789Sahrens * XXX The intent log is not accounted for, so it must fit 243789Sahrens * within this slop. 244789Sahrens * 245789Sahrens * If we're trying to assess whether it's OK to do a free, 246789Sahrens * cut the reservation in half to allow forward progress 247789Sahrens * (e.g. make it possible to rm(1) files from a full pool). 248789Sahrens */ 2492082Seschrock space = spa_get_dspace(dp->dp_spa); 2501775Sbillm resv = MAX(space >> 6, SPA_MINDEVSIZE >> 1); 251789Sahrens if (netfree) 252789Sahrens resv >>= 1; 253789Sahrens 254789Sahrens return (space - resv); 255789Sahrens } 256