4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
28 #include <sys/dsl_pool.h>
29 #include <sys/dsl_dataset.h>
30 #include <sys/dsl_dir.h>
31 #include <sys/dsl_synctask.h>
32 #include <sys/dmu_tx.h>
33 #include <sys/dmu_objset.h>
37 #include <sys/zfs_context.h>
38 #include <sys/fs/zfs.h>
41 dsl_pool_open_mos_dir(dsl_pool_t *dp, dsl_dir_t **ddp)
46 err = zap_lookup(dp->dp_meta_objset,
47 dp->dp_root_dir->dd_phys->dd_child_dir_zapobj,
48 MOS_DIR_NAME, sizeof (obj), 1, &obj);
52 return (dsl_dir_open_obj(dp, obj, MOS_DIR_NAME, dp, ddp));
56 dsl_pool_open_impl(spa_t *spa, uint64_t txg)
59 blkptr_t *bp = spa_get_rootblkptr(spa);
61 dp = kmem_zalloc(sizeof (dsl_pool_t), KM_SLEEP);
63 dp->dp_meta_rootbp = *bp;
64 rw_init(&dp->dp_config_rwlock, NULL, RW_DEFAULT, NULL);
67 txg_list_create(&dp->dp_dirty_datasets,
68 offsetof(dsl_dataset_t, ds_dirty_link));
69 txg_list_create(&dp->dp_dirty_dirs,
70 offsetof(dsl_dir_t, dd_dirty_link));
71 txg_list_create(&dp->dp_sync_tasks,
72 offsetof(dsl_sync_task_group_t, dstg_node));
73 list_create(&dp->dp_synced_objsets, sizeof (dsl_dataset_t),
74 offsetof(dsl_dataset_t, ds_synced_link));
80 dsl_pool_open(spa_t *spa, uint64_t txg, dsl_pool_t **dpp)
83 dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
86 rw_enter(&dp->dp_config_rwlock, RW_READER);
87 err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp, &osi);
90 dp->dp_meta_objset = &osi->os;
92 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
93 DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1,
94 &dp->dp_root_dir_obj);
98 err = dsl_dir_open_obj(dp, dp->dp_root_dir_obj,
99 NULL, dp, &dp->dp_root_dir);
103 err = dsl_pool_open_mos_dir(dp, &dp->dp_mos_dir);
108 rw_exit(&dp->dp_config_rwlock);
118 dsl_pool_close(dsl_pool_t *dp)
120 /* drop our reference from dsl_pool_open() */
122 dsl_dir_close(dp->dp_mos_dir, dp);
124 dsl_dir_close(dp->dp_root_dir, dp);
126 /* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */
127 if (dp->dp_meta_objset)
128 dmu_objset_evict(NULL, dp->dp_meta_objset->os);
130 txg_list_destroy(&dp->dp_dirty_datasets);
131 txg_list_destroy(&dp->dp_dirty_dirs);
132 txg_list_destroy(&dp->dp_sync_tasks);
133 list_destroy(&dp->dp_synced_objsets);
137 rw_destroy(&dp->dp_config_rwlock);
138 kmem_free(dp, sizeof (dsl_pool_t));
142 dsl_pool_create(spa_t *spa, uint64_t txg)
145 dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
146 dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg);
147 dp->dp_meta_objset = &dmu_objset_create_impl(spa,
148 NULL, &dp->dp_meta_rootbp, DMU_OST_META, tx)->os;
150 /* create the pool directory */
151 err = zap_create_claim(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
152 DMU_OT_OBJECT_DIRECTORY, DMU_OT_NONE, 0, tx);
153 ASSERT3U(err, ==, 0);
155 /* create and open the root dir */
156 dsl_dataset_create_root(dp, &dp->dp_root_dir_obj, tx);
157 VERIFY(0 == dsl_dir_open_obj(dp, dp->dp_root_dir_obj,
158 NULL, dp, &dp->dp_root_dir));
160 /* create and open the meta-objset dir */
161 (void) dsl_dir_create_sync(dp->dp_root_dir, MOS_DIR_NAME, tx);
162 VERIFY(0 == dsl_pool_open_mos_dir(dp, &dp->dp_mos_dir));
170 dsl_pool_sync(dsl_pool_t *dp, uint64_t txg)
176 dsl_sync_task_group_t *dstg;
177 objset_impl_t *mosi = dp->dp_meta_objset->os;
180 tx = dmu_tx_create_assigned(dp, txg);
182 zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
183 while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) {
184 if (!list_link_active(&ds->ds_synced_link))
185 list_insert_tail(&dp->dp_synced_objsets, ds);
187 dmu_buf_rele(ds->ds_dbuf, ds);
188 dsl_dataset_sync(ds, zio, tx);
193 while (dstg = txg_list_remove(&dp->dp_sync_tasks, txg))
194 dsl_sync_task_group_sync(dstg, tx);
195 while (dd = txg_list_remove(&dp->dp_dirty_dirs, txg))
196 dsl_dir_sync(dd, tx);
198 if (list_head(&mosi->os_dirty_dnodes[txg & TXG_MASK]) != NULL ||
199 list_head(&mosi->os_free_dnodes[txg & TXG_MASK]) != NULL) {
200 zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
201 dmu_objset_sync(mosi, zio, tx);
204 dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", "");
205 spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
212 dsl_pool_zil_clean(dsl_pool_t *dp)
216 while (ds = list_head(&dp->dp_synced_objsets)) {
217 list_remove(&dp->dp_synced_objsets, ds);
218 ASSERT(ds->ds_user_ptr != NULL);
219 zil_clean(((objset_impl_t *)ds->ds_user_ptr)->os_zil);
220 dmu_buf_rele(ds->ds_dbuf, ds);
225 * TRUE if the current thread is the tx_sync_thread or if we
226 * are being called from SPA context during pool initialization.
229 dsl_pool_sync_context(dsl_pool_t *dp)
231 return (curthread == dp->dp_tx.tx_sync_thread ||
232 spa_get_dsl(dp->dp_spa) == NULL);
236 dsl_pool_adjustedsize(dsl_pool_t *dp, boolean_t netfree)
238 uint64_t space, resv;
241 * Reserve about 1.6% (1/64), or at least 32MB, for allocation
243 * XXX The intent log is not accounted for, so it must fit
246 * If we're trying to assess whether it's OK to do a free,
247 * cut the reservation in half to allow forward progress
248 * (e.g. make it possible to rm(1) files from a full pool).
250 space = spa_get_dspace(dp->dp_spa);
251 resv = MAX(space >> 6, SPA_MINDEVSIZE >> 1);
255 return (space - resv);