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 /* 221544Seschrock * Copyright 2006 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> 36789Sahrens #include <sys/zfs_context.h> 37789Sahrens #include <sys/fs/zfs.h> 38789Sahrens 391544Seschrock static int 401544Seschrock dsl_pool_open_mos_dir(dsl_pool_t *dp, dsl_dir_t **ddp) 41789Sahrens { 42789Sahrens uint64_t obj; 43789Sahrens int err; 44789Sahrens 45789Sahrens err = zap_lookup(dp->dp_meta_objset, 46789Sahrens dp->dp_root_dir->dd_phys->dd_child_dir_zapobj, 47789Sahrens MOS_DIR_NAME, sizeof (obj), 1, &obj); 481544Seschrock if (err) 491544Seschrock return (err); 50789Sahrens 511544Seschrock return (dsl_dir_open_obj(dp, obj, MOS_DIR_NAME, dp, ddp)); 52789Sahrens } 53789Sahrens 54789Sahrens static dsl_pool_t * 55789Sahrens dsl_pool_open_impl(spa_t *spa, uint64_t txg) 56789Sahrens { 57789Sahrens dsl_pool_t *dp; 58789Sahrens blkptr_t *bp = spa_get_rootblkptr(spa); 59789Sahrens 60789Sahrens dp = kmem_zalloc(sizeof (dsl_pool_t), KM_SLEEP); 61789Sahrens dp->dp_spa = spa; 62789Sahrens dp->dp_meta_rootbp = *bp; 63*2856Snd150628 rw_init(&dp->dp_config_rwlock, NULL, RW_DEFAULT, NULL); 64789Sahrens txg_init(dp, txg); 65789Sahrens 66789Sahrens txg_list_create(&dp->dp_dirty_datasets, 67789Sahrens offsetof(dsl_dataset_t, ds_dirty_link)); 68789Sahrens txg_list_create(&dp->dp_dirty_dirs, 69789Sahrens offsetof(dsl_dir_t, dd_dirty_link)); 702199Sahrens txg_list_create(&dp->dp_sync_tasks, 712199Sahrens offsetof(dsl_sync_task_group_t, dstg_node)); 72789Sahrens list_create(&dp->dp_synced_objsets, sizeof (dsl_dataset_t), 73789Sahrens offsetof(dsl_dataset_t, ds_synced_link)); 74789Sahrens 75789Sahrens return (dp); 76789Sahrens } 77789Sahrens 781544Seschrock int 791544Seschrock dsl_pool_open(spa_t *spa, uint64_t txg, dsl_pool_t **dpp) 80789Sahrens { 81789Sahrens int err; 82789Sahrens dsl_pool_t *dp = dsl_pool_open_impl(spa, txg); 831544Seschrock objset_impl_t *osi; 84789Sahrens 85789Sahrens rw_enter(&dp->dp_config_rwlock, RW_READER); 861544Seschrock err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp, &osi); 871544Seschrock if (err) 881544Seschrock goto out; 891544Seschrock dp->dp_meta_objset = &osi->os; 901544Seschrock 91789Sahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 92789Sahrens DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, 93789Sahrens &dp->dp_root_dir_obj); 941544Seschrock if (err) 951544Seschrock goto out; 961544Seschrock 971544Seschrock err = dsl_dir_open_obj(dp, dp->dp_root_dir_obj, 981544Seschrock NULL, dp, &dp->dp_root_dir); 991544Seschrock if (err) 1001544Seschrock goto out; 101789Sahrens 1021544Seschrock err = dsl_pool_open_mos_dir(dp, &dp->dp_mos_dir); 1031544Seschrock if (err) 1041544Seschrock goto out; 1051544Seschrock 1061544Seschrock out: 107789Sahrens rw_exit(&dp->dp_config_rwlock); 1081544Seschrock if (err) 1091544Seschrock dsl_pool_close(dp); 1101544Seschrock else 1111544Seschrock *dpp = dp; 112789Sahrens 1131544Seschrock return (err); 114789Sahrens } 115789Sahrens 116789Sahrens void 117789Sahrens dsl_pool_close(dsl_pool_t *dp) 118789Sahrens { 119789Sahrens /* drop our reference from dsl_pool_open() */ 1201544Seschrock if (dp->dp_mos_dir) 1211544Seschrock dsl_dir_close(dp->dp_mos_dir, dp); 1221544Seschrock if (dp->dp_root_dir) 1231544Seschrock dsl_dir_close(dp->dp_root_dir, dp); 124789Sahrens 125789Sahrens /* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */ 1261544Seschrock if (dp->dp_meta_objset) 1271544Seschrock dmu_objset_evict(NULL, dp->dp_meta_objset->os); 128789Sahrens 129789Sahrens txg_list_destroy(&dp->dp_dirty_datasets); 130789Sahrens txg_list_destroy(&dp->dp_dirty_dirs); 131789Sahrens list_destroy(&dp->dp_synced_objsets); 132789Sahrens 133789Sahrens arc_flush(); 134789Sahrens txg_fini(dp); 135*2856Snd150628 rw_destroy(&dp->dp_config_rwlock); 136789Sahrens kmem_free(dp, sizeof (dsl_pool_t)); 137789Sahrens } 138789Sahrens 139789Sahrens dsl_pool_t * 140789Sahrens dsl_pool_create(spa_t *spa, uint64_t txg) 141789Sahrens { 142789Sahrens int err; 143789Sahrens dsl_pool_t *dp = dsl_pool_open_impl(spa, txg); 144789Sahrens dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg); 145789Sahrens dp->dp_meta_objset = &dmu_objset_create_impl(spa, 146789Sahrens NULL, DMU_OST_META, tx)->os; 147789Sahrens 148789Sahrens /* create the pool directory */ 149789Sahrens err = zap_create_claim(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 150789Sahrens DMU_OT_OBJECT_DIRECTORY, DMU_OT_NONE, 0, tx); 151789Sahrens ASSERT3U(err, ==, 0); 152789Sahrens 153789Sahrens /* create and open the root dir */ 154789Sahrens dsl_dataset_create_root(dp, &dp->dp_root_dir_obj, tx); 1551544Seschrock VERIFY(0 == dsl_dir_open_obj(dp, dp->dp_root_dir_obj, 1561544Seschrock NULL, dp, &dp->dp_root_dir)); 157789Sahrens 158789Sahrens /* create and open the meta-objset dir */ 1592199Sahrens (void) dsl_dir_create_sync(dp->dp_root_dir, MOS_DIR_NAME, tx); 1601544Seschrock VERIFY(0 == dsl_pool_open_mos_dir(dp, &dp->dp_mos_dir)); 161789Sahrens 162789Sahrens dmu_tx_commit(tx); 163789Sahrens 164789Sahrens return (dp); 165789Sahrens } 166789Sahrens 167789Sahrens void 168789Sahrens dsl_pool_sync(dsl_pool_t *dp, uint64_t txg) 169789Sahrens { 170789Sahrens dmu_tx_t *tx; 171789Sahrens objset_impl_t *mosi = dp->dp_meta_objset->os; 172789Sahrens 173789Sahrens tx = dmu_tx_create_assigned(dp, txg); 174789Sahrens 175789Sahrens do { 176789Sahrens dsl_dir_t *dd; 177789Sahrens dsl_dataset_t *ds; 1782199Sahrens dsl_sync_task_group_t *dstg; 179789Sahrens 180789Sahrens while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) { 181789Sahrens if (!list_link_active(&ds->ds_synced_link)) 182789Sahrens list_insert_tail(&dp->dp_synced_objsets, ds); 183789Sahrens dsl_dataset_sync(ds, tx); 184789Sahrens } 1852199Sahrens while (dstg = txg_list_remove(&dp->dp_sync_tasks, txg)) 1862199Sahrens dsl_sync_task_group_sync(dstg, tx); 187789Sahrens while (dd = txg_list_remove(&dp->dp_dirty_dirs, txg)) 188789Sahrens dsl_dir_sync(dd, tx); 189789Sahrens /* 1902199Sahrens * We need to loop since dsl_sync_task_group_sync() 1912199Sahrens * could create a new (dirty) objset. 192789Sahrens * XXX - isn't this taken care of by the spa's sync to 193789Sahrens * convergence loop? 194789Sahrens */ 195789Sahrens } while (!txg_list_empty(&dp->dp_dirty_datasets, txg)); 196789Sahrens 197789Sahrens if (list_head(&mosi->os_dirty_dnodes[txg & TXG_MASK]) != NULL || 198789Sahrens list_head(&mosi->os_free_dnodes[txg & TXG_MASK]) != NULL) { 199789Sahrens dmu_objset_sync(mosi, tx); 200789Sahrens dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", ""); 201789Sahrens spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp); 202789Sahrens } 203789Sahrens 204789Sahrens dmu_tx_commit(tx); 205789Sahrens } 206789Sahrens 207789Sahrens void 208789Sahrens dsl_pool_zil_clean(dsl_pool_t *dp) 209789Sahrens { 210789Sahrens dsl_dataset_t *ds; 211789Sahrens 212789Sahrens while (ds = list_head(&dp->dp_synced_objsets)) { 213789Sahrens list_remove(&dp->dp_synced_objsets, ds); 214789Sahrens ASSERT(ds->ds_user_ptr != NULL); 215789Sahrens zil_clean(((objset_impl_t *)ds->ds_user_ptr)->os_zil); 216789Sahrens } 217789Sahrens } 218789Sahrens 219789Sahrens int 220789Sahrens dsl_pool_sync_context(dsl_pool_t *dp) 221789Sahrens { 222789Sahrens /* 223789Sahrens * Yeah, this is cheesy. But the SPA needs some way to let 224789Sahrens * the sync threads invoke spa_open() and spa_close() while 225789Sahrens * it holds the namespace lock. I'm certainly open to better 226789Sahrens * ideas for how to determine whether the current thread is 227789Sahrens * operating on behalf of spa_sync(). This works for now. 228789Sahrens */ 229789Sahrens return (curthread == dp->dp_tx.tx_sync_thread || 230789Sahrens BP_IS_HOLE(&dp->dp_meta_rootbp)); 231789Sahrens } 232789Sahrens 233789Sahrens uint64_t 234789Sahrens dsl_pool_adjustedsize(dsl_pool_t *dp, boolean_t netfree) 235789Sahrens { 236789Sahrens uint64_t space, resv; 237789Sahrens 238789Sahrens /* 2391775Sbillm * Reserve about 1.6% (1/64), or at least 32MB, for allocation 240789Sahrens * efficiency. 241789Sahrens * XXX The intent log is not accounted for, so it must fit 242789Sahrens * within this slop. 243789Sahrens * 244789Sahrens * If we're trying to assess whether it's OK to do a free, 245789Sahrens * cut the reservation in half to allow forward progress 246789Sahrens * (e.g. make it possible to rm(1) files from a full pool). 247789Sahrens */ 2482082Seschrock space = spa_get_dspace(dp->dp_spa); 2491775Sbillm resv = MAX(space >> 6, SPA_MINDEVSIZE >> 1); 250789Sahrens if (netfree) 251789Sahrens resv >>= 1; 252789Sahrens 253789Sahrens return (space - resv); 254789Sahrens } 255