xref: /onnv-gate/usr/src/uts/common/fs/zfs/dsl_pool.c (revision 6245:1a2a7cfb9f26)
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 /*
22*6245Smaybee  * Copyright 2008 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 
40*6245Smaybee int zfs_no_write_throttle = 0;
41*6245Smaybee uint64_t zfs_write_limit_override = 0;
42*6245Smaybee 
431544Seschrock static int
441544Seschrock dsl_pool_open_mos_dir(dsl_pool_t *dp, dsl_dir_t **ddp)
45789Sahrens {
46789Sahrens 	uint64_t obj;
47789Sahrens 	int err;
48789Sahrens 
49789Sahrens 	err = zap_lookup(dp->dp_meta_objset,
50789Sahrens 	    dp->dp_root_dir->dd_phys->dd_child_dir_zapobj,
51789Sahrens 	    MOS_DIR_NAME, sizeof (obj), 1, &obj);
521544Seschrock 	if (err)
531544Seschrock 		return (err);
54789Sahrens 
551544Seschrock 	return (dsl_dir_open_obj(dp, obj, MOS_DIR_NAME, dp, ddp));
56789Sahrens }
57789Sahrens 
58789Sahrens static dsl_pool_t *
59789Sahrens dsl_pool_open_impl(spa_t *spa, uint64_t txg)
60789Sahrens {
61789Sahrens 	dsl_pool_t *dp;
62789Sahrens 	blkptr_t *bp = spa_get_rootblkptr(spa);
63*6245Smaybee 	extern uint64_t zfs_write_limit_min;
64789Sahrens 
65789Sahrens 	dp = kmem_zalloc(sizeof (dsl_pool_t), KM_SLEEP);
66789Sahrens 	dp->dp_spa = spa;
67789Sahrens 	dp->dp_meta_rootbp = *bp;
682856Snd150628 	rw_init(&dp->dp_config_rwlock, NULL, RW_DEFAULT, NULL);
69*6245Smaybee 	dp->dp_write_limit = zfs_write_limit_min;
70789Sahrens 	txg_init(dp, txg);
71789Sahrens 
72789Sahrens 	txg_list_create(&dp->dp_dirty_datasets,
73789Sahrens 	    offsetof(dsl_dataset_t, ds_dirty_link));
74789Sahrens 	txg_list_create(&dp->dp_dirty_dirs,
75789Sahrens 	    offsetof(dsl_dir_t, dd_dirty_link));
762199Sahrens 	txg_list_create(&dp->dp_sync_tasks,
772199Sahrens 	    offsetof(dsl_sync_task_group_t, dstg_node));
785367Sahrens 	list_create(&dp->dp_synced_datasets, sizeof (dsl_dataset_t),
79789Sahrens 	    offsetof(dsl_dataset_t, ds_synced_link));
80789Sahrens 
81*6245Smaybee 	mutex_init(&dp->dp_lock, NULL, MUTEX_DEFAULT, NULL);
82*6245Smaybee 
83789Sahrens 	return (dp);
84789Sahrens }
85789Sahrens 
861544Seschrock int
871544Seschrock dsl_pool_open(spa_t *spa, uint64_t txg, dsl_pool_t **dpp)
88789Sahrens {
89789Sahrens 	int err;
90789Sahrens 	dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
911544Seschrock 	objset_impl_t *osi;
92789Sahrens 
93789Sahrens 	rw_enter(&dp->dp_config_rwlock, RW_READER);
941544Seschrock 	err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp, &osi);
951544Seschrock 	if (err)
961544Seschrock 		goto out;
971544Seschrock 	dp->dp_meta_objset = &osi->os;
981544Seschrock 
99789Sahrens 	err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
100789Sahrens 	    DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1,
101789Sahrens 	    &dp->dp_root_dir_obj);
1021544Seschrock 	if (err)
1031544Seschrock 		goto out;
1041544Seschrock 
1051544Seschrock 	err = dsl_dir_open_obj(dp, dp->dp_root_dir_obj,
1061544Seschrock 	    NULL, dp, &dp->dp_root_dir);
1071544Seschrock 	if (err)
1081544Seschrock 		goto out;
109789Sahrens 
1101544Seschrock 	err = dsl_pool_open_mos_dir(dp, &dp->dp_mos_dir);
1111544Seschrock 	if (err)
1121544Seschrock 		goto out;
1131544Seschrock 
1141544Seschrock out:
115789Sahrens 	rw_exit(&dp->dp_config_rwlock);
1161544Seschrock 	if (err)
1171544Seschrock 		dsl_pool_close(dp);
1181544Seschrock 	else
1191544Seschrock 		*dpp = dp;
120789Sahrens 
1211544Seschrock 	return (err);
122789Sahrens }
123789Sahrens 
124789Sahrens void
125789Sahrens dsl_pool_close(dsl_pool_t *dp)
126789Sahrens {
127789Sahrens 	/* drop our reference from dsl_pool_open() */
1281544Seschrock 	if (dp->dp_mos_dir)
1291544Seschrock 		dsl_dir_close(dp->dp_mos_dir, dp);
1301544Seschrock 	if (dp->dp_root_dir)
1311544Seschrock 		dsl_dir_close(dp->dp_root_dir, dp);
132789Sahrens 
133789Sahrens 	/* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */
1341544Seschrock 	if (dp->dp_meta_objset)
1351544Seschrock 		dmu_objset_evict(NULL, dp->dp_meta_objset->os);
136789Sahrens 
137789Sahrens 	txg_list_destroy(&dp->dp_dirty_datasets);
138789Sahrens 	txg_list_destroy(&dp->dp_dirty_dirs);
1395367Sahrens 	list_destroy(&dp->dp_synced_datasets);
140789Sahrens 
1415642Smaybee 	arc_flush(dp->dp_spa);
142789Sahrens 	txg_fini(dp);
1432856Snd150628 	rw_destroy(&dp->dp_config_rwlock);
144*6245Smaybee 	mutex_destroy(&dp->dp_lock);
145789Sahrens 	kmem_free(dp, sizeof (dsl_pool_t));
146789Sahrens }
147789Sahrens 
148789Sahrens dsl_pool_t *
149789Sahrens dsl_pool_create(spa_t *spa, uint64_t txg)
150789Sahrens {
151789Sahrens 	int err;
152789Sahrens 	dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
153789Sahrens 	dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg);
154789Sahrens 	dp->dp_meta_objset = &dmu_objset_create_impl(spa,
1553547Smaybee 	    NULL, &dp->dp_meta_rootbp, DMU_OST_META, tx)->os;
156789Sahrens 
157789Sahrens 	/* create the pool directory */
158789Sahrens 	err = zap_create_claim(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
159789Sahrens 	    DMU_OT_OBJECT_DIRECTORY, DMU_OT_NONE, 0, tx);
160789Sahrens 	ASSERT3U(err, ==, 0);
161789Sahrens 
162789Sahrens 	/* create and open the root dir */
163789Sahrens 	dsl_dataset_create_root(dp, &dp->dp_root_dir_obj, tx);
1641544Seschrock 	VERIFY(0 == dsl_dir_open_obj(dp, dp->dp_root_dir_obj,
1651544Seschrock 	    NULL, dp, &dp->dp_root_dir));
166789Sahrens 
167789Sahrens 	/* create and open the meta-objset dir */
1682199Sahrens 	(void) dsl_dir_create_sync(dp->dp_root_dir, MOS_DIR_NAME, tx);
1691544Seschrock 	VERIFY(0 == dsl_pool_open_mos_dir(dp, &dp->dp_mos_dir));
170789Sahrens 
171789Sahrens 	dmu_tx_commit(tx);
172789Sahrens 
173789Sahrens 	return (dp);
174789Sahrens }
175789Sahrens 
176789Sahrens void
177789Sahrens dsl_pool_sync(dsl_pool_t *dp, uint64_t txg)
178789Sahrens {
1793547Smaybee 	zio_t *zio;
180789Sahrens 	dmu_tx_t *tx;
1813547Smaybee 	dsl_dir_t *dd;
1823547Smaybee 	dsl_dataset_t *ds;
1833547Smaybee 	dsl_sync_task_group_t *dstg;
184789Sahrens 	objset_impl_t *mosi = dp->dp_meta_objset->os;
1853547Smaybee 	int err;
186789Sahrens 
187789Sahrens 	tx = dmu_tx_create_assigned(dp, txg);
188789Sahrens 
1893547Smaybee 	zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
1903547Smaybee 	while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) {
1913547Smaybee 		if (!list_link_active(&ds->ds_synced_link))
1925367Sahrens 			list_insert_tail(&dp->dp_synced_datasets, ds);
1933897Smaybee 		else
1943897Smaybee 			dmu_buf_rele(ds->ds_dbuf, ds);
1953547Smaybee 		dsl_dataset_sync(ds, zio, tx);
1963547Smaybee 	}
1973547Smaybee 	err = zio_wait(zio);
1983547Smaybee 	ASSERT(err == 0);
199789Sahrens 
2003547Smaybee 	while (dstg = txg_list_remove(&dp->dp_sync_tasks, txg))
2013547Smaybee 		dsl_sync_task_group_sync(dstg, tx);
2023547Smaybee 	while (dd = txg_list_remove(&dp->dp_dirty_dirs, txg))
2033547Smaybee 		dsl_dir_sync(dd, tx);
204789Sahrens 
205789Sahrens 	if (list_head(&mosi->os_dirty_dnodes[txg & TXG_MASK]) != NULL ||
206789Sahrens 	    list_head(&mosi->os_free_dnodes[txg & TXG_MASK]) != NULL) {
2073547Smaybee 		zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
2083547Smaybee 		dmu_objset_sync(mosi, zio, tx);
2093547Smaybee 		err = zio_wait(zio);
2103547Smaybee 		ASSERT(err == 0);
211789Sahrens 		dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", "");
212789Sahrens 		spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
213789Sahrens 	}
214789Sahrens 
215789Sahrens 	dmu_tx_commit(tx);
216789Sahrens }
217789Sahrens 
218789Sahrens void
219789Sahrens dsl_pool_zil_clean(dsl_pool_t *dp)
220789Sahrens {
221789Sahrens 	dsl_dataset_t *ds;
222789Sahrens 
2235367Sahrens 	while (ds = list_head(&dp->dp_synced_datasets)) {
2245367Sahrens 		list_remove(&dp->dp_synced_datasets, ds);
225789Sahrens 		ASSERT(ds->ds_user_ptr != NULL);
226789Sahrens 		zil_clean(((objset_impl_t *)ds->ds_user_ptr)->os_zil);
2273897Smaybee 		dmu_buf_rele(ds->ds_dbuf, ds);
228789Sahrens 	}
229789Sahrens }
230789Sahrens 
2313547Smaybee /*
2323547Smaybee  * TRUE if the current thread is the tx_sync_thread or if we
2333547Smaybee  * are being called from SPA context during pool initialization.
2343547Smaybee  */
235789Sahrens int
236789Sahrens dsl_pool_sync_context(dsl_pool_t *dp)
237789Sahrens {
238789Sahrens 	return (curthread == dp->dp_tx.tx_sync_thread ||
2393547Smaybee 	    spa_get_dsl(dp->dp_spa) == NULL);
240789Sahrens }
241789Sahrens 
242789Sahrens uint64_t
243789Sahrens dsl_pool_adjustedsize(dsl_pool_t *dp, boolean_t netfree)
244789Sahrens {
245789Sahrens 	uint64_t space, resv;
246789Sahrens 
247789Sahrens 	/*
2481775Sbillm 	 * Reserve about 1.6% (1/64), or at least 32MB, for allocation
249789Sahrens 	 * efficiency.
250789Sahrens 	 * XXX The intent log is not accounted for, so it must fit
251789Sahrens 	 * within this slop.
252789Sahrens 	 *
253789Sahrens 	 * If we're trying to assess whether it's OK to do a free,
254789Sahrens 	 * cut the reservation in half to allow forward progress
255789Sahrens 	 * (e.g. make it possible to rm(1) files from a full pool).
256789Sahrens 	 */
2572082Seschrock 	space = spa_get_dspace(dp->dp_spa);
2581775Sbillm 	resv = MAX(space >> 6, SPA_MINDEVSIZE >> 1);
259789Sahrens 	if (netfree)
260789Sahrens 		resv >>= 1;
261789Sahrens 
262789Sahrens 	return (space - resv);
263789Sahrens }
264*6245Smaybee 
265*6245Smaybee int
266*6245Smaybee dsl_pool_tempreserve_space(dsl_pool_t *dp, uint64_t space, dmu_tx_t *tx)
267*6245Smaybee {
268*6245Smaybee 	uint64_t reserved = 0;
269*6245Smaybee 	uint64_t write_limit = (zfs_write_limit_override ?
270*6245Smaybee 	    zfs_write_limit_override : dp->dp_write_limit);
271*6245Smaybee 
272*6245Smaybee 	if (zfs_no_write_throttle) {
273*6245Smaybee 		dp->dp_tempreserved[tx->tx_txg & TXG_MASK] += space;
274*6245Smaybee 		return (0);
275*6245Smaybee 	}
276*6245Smaybee 
277*6245Smaybee 	/*
278*6245Smaybee 	 * Check to see if we have exceeded the maximum allowed IO for
279*6245Smaybee 	 * this transaction group.  We can do this without locks since
280*6245Smaybee 	 * a little slop here is ok.  Note that we do the reserved check
281*6245Smaybee 	 * with only half the requested reserve: this is because the
282*6245Smaybee 	 * reserve requests are worst-case, and we really don't want to
283*6245Smaybee 	 * throttle based off of worst-case estimates.
284*6245Smaybee 	 */
285*6245Smaybee 	if (write_limit > 0) {
286*6245Smaybee 		reserved = dp->dp_space_towrite[tx->tx_txg & TXG_MASK]
287*6245Smaybee 		    + dp->dp_tempreserved[tx->tx_txg & TXG_MASK] / 2;
288*6245Smaybee 
289*6245Smaybee 		if (reserved && reserved > write_limit)
290*6245Smaybee 			return (ERESTART);
291*6245Smaybee 	}
292*6245Smaybee 
293*6245Smaybee 	atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], space);
294*6245Smaybee 
295*6245Smaybee 	/*
296*6245Smaybee 	 * If this transaction group is over 7/8ths capacity, delay
297*6245Smaybee 	 * the caller 1 clock tick.  This will slow down the "fill"
298*6245Smaybee 	 * rate until the sync process can catch up with us.
299*6245Smaybee 	 */
300*6245Smaybee 	if (reserved && reserved > (write_limit - write_limit << 3))
301*6245Smaybee 		txg_delay(dp, tx->tx_txg, 1);
302*6245Smaybee 
303*6245Smaybee 	return (0);
304*6245Smaybee }
305*6245Smaybee 
306*6245Smaybee void
307*6245Smaybee dsl_pool_tempreserve_clear(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
308*6245Smaybee {
309*6245Smaybee 	ASSERT(dp->dp_tempreserved[tx->tx_txg & TXG_MASK] >= space);
310*6245Smaybee 	atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], -space);
311*6245Smaybee }
312*6245Smaybee 
313*6245Smaybee void
314*6245Smaybee dsl_pool_memory_pressure(dsl_pool_t *dp)
315*6245Smaybee {
316*6245Smaybee 	extern uint64_t zfs_write_limit_min;
317*6245Smaybee 	uint64_t space_inuse = 0;
318*6245Smaybee 	int i;
319*6245Smaybee 
320*6245Smaybee 	if (dp->dp_write_limit == zfs_write_limit_min)
321*6245Smaybee 		return;
322*6245Smaybee 
323*6245Smaybee 	for (i = 0; i < TXG_SIZE; i++) {
324*6245Smaybee 		space_inuse += dp->dp_space_towrite[i];
325*6245Smaybee 		space_inuse += dp->dp_tempreserved[i];
326*6245Smaybee 	}
327*6245Smaybee 	dp->dp_write_limit = MAX(zfs_write_limit_min,
328*6245Smaybee 	    MIN(dp->dp_write_limit, space_inuse / 4));
329*6245Smaybee }
330*6245Smaybee 
331*6245Smaybee void
332*6245Smaybee dsl_pool_willuse_space(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
333*6245Smaybee {
334*6245Smaybee 	if (space > 0) {
335*6245Smaybee 		mutex_enter(&dp->dp_lock);
336*6245Smaybee 		dp->dp_space_towrite[tx->tx_txg & TXG_MASK] += space;
337*6245Smaybee 		mutex_exit(&dp->dp_lock);
338*6245Smaybee 	}
339*6245Smaybee }
340