xref: /onnv-gate/usr/src/uts/common/fs/zfs/dsl_pool.c (revision 6643:3a34b0dbb107)
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 /*
226245Smaybee  * 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 
406245Smaybee int zfs_no_write_throttle = 0;
416245Smaybee uint64_t zfs_write_limit_override = 0;
426245Smaybee 
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);
636245Smaybee 	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);
696245Smaybee 	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 
816245Smaybee 	mutex_init(&dp->dp_lock, NULL, MUTEX_DEFAULT, NULL);
826245Smaybee 
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);
1446245Smaybee 	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 }
2646245Smaybee 
2656245Smaybee int
2666245Smaybee dsl_pool_tempreserve_space(dsl_pool_t *dp, uint64_t space, dmu_tx_t *tx)
2676245Smaybee {
2686245Smaybee 	uint64_t reserved = 0;
2696245Smaybee 	uint64_t write_limit = (zfs_write_limit_override ?
2706245Smaybee 	    zfs_write_limit_override : dp->dp_write_limit);
2716245Smaybee 
2726245Smaybee 	if (zfs_no_write_throttle) {
273*6643Seschrock 		atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK],
274*6643Seschrock 		    space);
2756245Smaybee 		return (0);
2766245Smaybee 	}
2776245Smaybee 
2786245Smaybee 	/*
2796245Smaybee 	 * Check to see if we have exceeded the maximum allowed IO for
2806245Smaybee 	 * this transaction group.  We can do this without locks since
2816245Smaybee 	 * a little slop here is ok.  Note that we do the reserved check
2826245Smaybee 	 * with only half the requested reserve: this is because the
2836245Smaybee 	 * reserve requests are worst-case, and we really don't want to
2846245Smaybee 	 * throttle based off of worst-case estimates.
2856245Smaybee 	 */
2866245Smaybee 	if (write_limit > 0) {
2876245Smaybee 		reserved = dp->dp_space_towrite[tx->tx_txg & TXG_MASK]
2886245Smaybee 		    + dp->dp_tempreserved[tx->tx_txg & TXG_MASK] / 2;
2896245Smaybee 
2906245Smaybee 		if (reserved && reserved > write_limit)
2916245Smaybee 			return (ERESTART);
2926245Smaybee 	}
2936245Smaybee 
2946245Smaybee 	atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], space);
2956245Smaybee 
2966245Smaybee 	/*
2976245Smaybee 	 * If this transaction group is over 7/8ths capacity, delay
2986245Smaybee 	 * the caller 1 clock tick.  This will slow down the "fill"
2996245Smaybee 	 * rate until the sync process can catch up with us.
3006245Smaybee 	 */
3016245Smaybee 	if (reserved && reserved > (write_limit - write_limit << 3))
3026245Smaybee 		txg_delay(dp, tx->tx_txg, 1);
3036245Smaybee 
3046245Smaybee 	return (0);
3056245Smaybee }
3066245Smaybee 
3076245Smaybee void
3086245Smaybee dsl_pool_tempreserve_clear(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
3096245Smaybee {
3106245Smaybee 	ASSERT(dp->dp_tempreserved[tx->tx_txg & TXG_MASK] >= space);
3116245Smaybee 	atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], -space);
3126245Smaybee }
3136245Smaybee 
3146245Smaybee void
3156245Smaybee dsl_pool_memory_pressure(dsl_pool_t *dp)
3166245Smaybee {
3176245Smaybee 	extern uint64_t zfs_write_limit_min;
3186245Smaybee 	uint64_t space_inuse = 0;
3196245Smaybee 	int i;
3206245Smaybee 
3216245Smaybee 	if (dp->dp_write_limit == zfs_write_limit_min)
3226245Smaybee 		return;
3236245Smaybee 
3246245Smaybee 	for (i = 0; i < TXG_SIZE; i++) {
3256245Smaybee 		space_inuse += dp->dp_space_towrite[i];
3266245Smaybee 		space_inuse += dp->dp_tempreserved[i];
3276245Smaybee 	}
3286245Smaybee 	dp->dp_write_limit = MAX(zfs_write_limit_min,
3296245Smaybee 	    MIN(dp->dp_write_limit, space_inuse / 4));
3306245Smaybee }
3316245Smaybee 
3326245Smaybee void
3336245Smaybee dsl_pool_willuse_space(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
3346245Smaybee {
3356245Smaybee 	if (space > 0) {
3366245Smaybee 		mutex_enter(&dp->dp_lock);
3376245Smaybee 		dp->dp_space_towrite[tx->tx_txg & TXG_MASK] += space;
3386245Smaybee 		mutex_exit(&dp->dp_lock);
3396245Smaybee 	}
3406245Smaybee }
341