]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/dsl_pool.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / cddl / contrib / opensolaris / uts / common / fs / zfs / dsl_pool.c
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25
26 #pragma ident   "%Z%%M% %I%     %E% SMI"
27
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>
34 #include <sys/arc.h>
35 #include <sys/zap.h>
36 #include <sys/zio.h>
37 #include <sys/zfs_context.h>
38 #include <sys/fs/zfs.h>
39
40 static int
41 dsl_pool_open_mos_dir(dsl_pool_t *dp, dsl_dir_t **ddp)
42 {
43         uint64_t obj;
44         int err;
45
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);
49         if (err)
50                 return (err);
51
52         return (dsl_dir_open_obj(dp, obj, MOS_DIR_NAME, dp, ddp));
53 }
54
55 static dsl_pool_t *
56 dsl_pool_open_impl(spa_t *spa, uint64_t txg)
57 {
58         dsl_pool_t *dp;
59         blkptr_t *bp = spa_get_rootblkptr(spa);
60
61         dp = kmem_zalloc(sizeof (dsl_pool_t), KM_SLEEP);
62         dp->dp_spa = spa;
63         dp->dp_meta_rootbp = *bp;
64         rw_init(&dp->dp_config_rwlock, NULL, RW_DEFAULT, NULL);
65         txg_init(dp, txg);
66
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));
75
76         return (dp);
77 }
78
79 int
80 dsl_pool_open(spa_t *spa, uint64_t txg, dsl_pool_t **dpp)
81 {
82         int err;
83         dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
84         objset_impl_t *osi;
85
86         rw_enter(&dp->dp_config_rwlock, RW_READER);
87         err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp, &osi);
88         if (err)
89                 goto out;
90         dp->dp_meta_objset = &osi->os;
91
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);
95         if (err)
96                 goto out;
97
98         err = dsl_dir_open_obj(dp, dp->dp_root_dir_obj,
99             NULL, dp, &dp->dp_root_dir);
100         if (err)
101                 goto out;
102
103         err = dsl_pool_open_mos_dir(dp, &dp->dp_mos_dir);
104         if (err)
105                 goto out;
106
107 out:
108         rw_exit(&dp->dp_config_rwlock);
109         if (err)
110                 dsl_pool_close(dp);
111         else
112                 *dpp = dp;
113
114         return (err);
115 }
116
117 void
118 dsl_pool_close(dsl_pool_t *dp)
119 {
120         /* drop our reference from dsl_pool_open() */
121         if (dp->dp_mos_dir)
122                 dsl_dir_close(dp->dp_mos_dir, dp);
123         if (dp->dp_root_dir)
124                 dsl_dir_close(dp->dp_root_dir, dp);
125
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);
129
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);
134
135         arc_flush();
136         txg_fini(dp);
137         rw_destroy(&dp->dp_config_rwlock);
138         kmem_free(dp, sizeof (dsl_pool_t));
139 }
140
141 dsl_pool_t *
142 dsl_pool_create(spa_t *spa, uint64_t txg)
143 {
144         int err;
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;
149
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);
154
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));
159
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));
163
164         dmu_tx_commit(tx);
165
166         return (dp);
167 }
168
169 void
170 dsl_pool_sync(dsl_pool_t *dp, uint64_t txg)
171 {
172         zio_t *zio;
173         dmu_tx_t *tx;
174         dsl_dir_t *dd;
175         dsl_dataset_t *ds;
176         dsl_sync_task_group_t *dstg;
177         objset_impl_t *mosi = dp->dp_meta_objset->os;
178         int err;
179
180         tx = dmu_tx_create_assigned(dp, txg);
181
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);
186                 else
187                         dmu_buf_rele(ds->ds_dbuf, ds);
188                 dsl_dataset_sync(ds, zio, tx);
189         }
190         err = zio_wait(zio);
191         ASSERT(err == 0);
192
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);
197
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);
202                 err = zio_wait(zio);
203                 ASSERT(err == 0);
204                 dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", "");
205                 spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
206         }
207
208         dmu_tx_commit(tx);
209 }
210
211 void
212 dsl_pool_zil_clean(dsl_pool_t *dp)
213 {
214         dsl_dataset_t *ds;
215
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);
221         }
222 }
223
224 /*
225  * TRUE if the current thread is the tx_sync_thread or if we
226  * are being called from SPA context during pool initialization.
227  */
228 int
229 dsl_pool_sync_context(dsl_pool_t *dp)
230 {
231         return (curthread == dp->dp_tx.tx_sync_thread ||
232             spa_get_dsl(dp->dp_spa) == NULL);
233 }
234
235 uint64_t
236 dsl_pool_adjustedsize(dsl_pool_t *dp, boolean_t netfree)
237 {
238         uint64_t space, resv;
239
240         /*
241          * Reserve about 1.6% (1/64), or at least 32MB, for allocation
242          * efficiency.
243          * XXX The intent log is not accounted for, so it must fit
244          * within this slop.
245          *
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).
249          */
250         space = spa_get_dspace(dp->dp_spa);
251         resv = MAX(space >> 6, SPA_MINDEVSIZE >> 1);
252         if (netfree)
253                 resv >>= 1;
254
255         return (space - resv);
256 }