xref: /onnv-gate/usr/src/uts/common/fs/zfs/dsl_pool.c (revision 7837:001de5627df3)
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 #include <sys/dsl_pool.h>
27789Sahrens #include <sys/dsl_dataset.h>
28789Sahrens #include <sys/dsl_dir.h>
292199Sahrens #include <sys/dsl_synctask.h>
30789Sahrens #include <sys/dmu_tx.h>
31789Sahrens #include <sys/dmu_objset.h>
32789Sahrens #include <sys/arc.h>
33789Sahrens #include <sys/zap.h>
343547Smaybee #include <sys/zio.h>
35789Sahrens #include <sys/zfs_context.h>
36789Sahrens #include <sys/fs/zfs.h>
377046Sahrens #include <sys/zfs_znode.h>
387046Sahrens #include <sys/spa_impl.h>
39789Sahrens 
406245Smaybee int zfs_no_write_throttle = 0;
417468SMark.Maybee@Sun.COM int zfs_write_limit_shift = 3;			/* 1/8th of physical memory */
427468SMark.Maybee@Sun.COM int zfs_txg_synctime = 5;			/* target secs to sync a txg */
437468SMark.Maybee@Sun.COM 
447468SMark.Maybee@Sun.COM uint64_t zfs_write_limit_min = 32 << 20;	/* min write limit is 32MB */
457468SMark.Maybee@Sun.COM uint64_t zfs_write_limit_max = 0;		/* max data payload per txg */
467468SMark.Maybee@Sun.COM uint64_t zfs_write_limit_inflated = 0;
476245Smaybee uint64_t zfs_write_limit_override = 0;
486245Smaybee 
497468SMark.Maybee@Sun.COM kmutex_t zfs_write_limit_lock;
507468SMark.Maybee@Sun.COM 
517468SMark.Maybee@Sun.COM static pgcnt_t old_physmem = 0;
527046Sahrens 
531544Seschrock static int
547046Sahrens dsl_pool_open_special_dir(dsl_pool_t *dp, const char *name, dsl_dir_t **ddp)
55789Sahrens {
56789Sahrens 	uint64_t obj;
57789Sahrens 	int err;
58789Sahrens 
59789Sahrens 	err = zap_lookup(dp->dp_meta_objset,
60789Sahrens 	    dp->dp_root_dir->dd_phys->dd_child_dir_zapobj,
617046Sahrens 	    name, sizeof (obj), 1, &obj);
621544Seschrock 	if (err)
631544Seschrock 		return (err);
64789Sahrens 
657046Sahrens 	return (dsl_dir_open_obj(dp, obj, name, dp, ddp));
66789Sahrens }
67789Sahrens 
68789Sahrens static dsl_pool_t *
69789Sahrens dsl_pool_open_impl(spa_t *spa, uint64_t txg)
70789Sahrens {
71789Sahrens 	dsl_pool_t *dp;
72789Sahrens 	blkptr_t *bp = spa_get_rootblkptr(spa);
73789Sahrens 
74789Sahrens 	dp = kmem_zalloc(sizeof (dsl_pool_t), KM_SLEEP);
75789Sahrens 	dp->dp_spa = spa;
76789Sahrens 	dp->dp_meta_rootbp = *bp;
772856Snd150628 	rw_init(&dp->dp_config_rwlock, NULL, RW_DEFAULT, NULL);
786245Smaybee 	dp->dp_write_limit = zfs_write_limit_min;
79789Sahrens 	txg_init(dp, txg);
80789Sahrens 
81789Sahrens 	txg_list_create(&dp->dp_dirty_datasets,
82789Sahrens 	    offsetof(dsl_dataset_t, ds_dirty_link));
83789Sahrens 	txg_list_create(&dp->dp_dirty_dirs,
84789Sahrens 	    offsetof(dsl_dir_t, dd_dirty_link));
852199Sahrens 	txg_list_create(&dp->dp_sync_tasks,
862199Sahrens 	    offsetof(dsl_sync_task_group_t, dstg_node));
875367Sahrens 	list_create(&dp->dp_synced_datasets, sizeof (dsl_dataset_t),
88789Sahrens 	    offsetof(dsl_dataset_t, ds_synced_link));
89789Sahrens 
906245Smaybee 	mutex_init(&dp->dp_lock, NULL, MUTEX_DEFAULT, NULL);
917046Sahrens 	mutex_init(&dp->dp_scrub_cancel_lock, NULL, MUTEX_DEFAULT, NULL);
926245Smaybee 
93789Sahrens 	return (dp);
94789Sahrens }
95789Sahrens 
961544Seschrock int
971544Seschrock dsl_pool_open(spa_t *spa, uint64_t txg, dsl_pool_t **dpp)
98789Sahrens {
99789Sahrens 	int err;
100789Sahrens 	dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
1017046Sahrens 	dsl_dir_t *dd;
1027046Sahrens 	dsl_dataset_t *ds;
1031544Seschrock 	objset_impl_t *osi;
104789Sahrens 
1057046Sahrens 	rw_enter(&dp->dp_config_rwlock, RW_WRITER);
1061544Seschrock 	err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp, &osi);
1071544Seschrock 	if (err)
1081544Seschrock 		goto out;
1091544Seschrock 	dp->dp_meta_objset = &osi->os;
1101544Seschrock 
111789Sahrens 	err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
112789Sahrens 	    DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1,
113789Sahrens 	    &dp->dp_root_dir_obj);
1141544Seschrock 	if (err)
1151544Seschrock 		goto out;
1161544Seschrock 
1171544Seschrock 	err = dsl_dir_open_obj(dp, dp->dp_root_dir_obj,
1181544Seschrock 	    NULL, dp, &dp->dp_root_dir);
1191544Seschrock 	if (err)
1201544Seschrock 		goto out;
121789Sahrens 
1227046Sahrens 	err = dsl_pool_open_special_dir(dp, MOS_DIR_NAME, &dp->dp_mos_dir);
1231544Seschrock 	if (err)
1241544Seschrock 		goto out;
1251544Seschrock 
1267046Sahrens 	if (spa_version(spa) >= SPA_VERSION_ORIGIN) {
1277046Sahrens 		err = dsl_pool_open_special_dir(dp, ORIGIN_DIR_NAME, &dd);
1287046Sahrens 		if (err)
1297046Sahrens 			goto out;
1307046Sahrens 		err = dsl_dataset_hold_obj(dp, dd->dd_phys->dd_head_dataset_obj,
1317046Sahrens 		    FTAG, &ds);
1327046Sahrens 		if (err)
1337046Sahrens 			goto out;
1347046Sahrens 		err = dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
1357046Sahrens 		    dp, &dp->dp_origin_snap);
1367046Sahrens 		if (err)
1377046Sahrens 			goto out;
1387046Sahrens 		dsl_dataset_rele(ds, FTAG);
1397046Sahrens 		dsl_dir_close(dd, dp);
1407046Sahrens 	}
1417046Sahrens 
1427046Sahrens 	/* get scrub status */
1437046Sahrens 	err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1447046Sahrens 	    DMU_POOL_SCRUB_FUNC, sizeof (uint32_t), 1,
1457046Sahrens 	    &dp->dp_scrub_func);
1467046Sahrens 	if (err == 0) {
1477046Sahrens 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1487046Sahrens 		    DMU_POOL_SCRUB_QUEUE, sizeof (uint64_t), 1,
1497046Sahrens 		    &dp->dp_scrub_queue_obj);
1507046Sahrens 		if (err)
1517046Sahrens 			goto out;
1527046Sahrens 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1537046Sahrens 		    DMU_POOL_SCRUB_MIN_TXG, sizeof (uint64_t), 1,
1547046Sahrens 		    &dp->dp_scrub_min_txg);
1557046Sahrens 		if (err)
1567046Sahrens 			goto out;
1577046Sahrens 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1587046Sahrens 		    DMU_POOL_SCRUB_MAX_TXG, sizeof (uint64_t), 1,
1597046Sahrens 		    &dp->dp_scrub_max_txg);
1607046Sahrens 		if (err)
1617046Sahrens 			goto out;
1627046Sahrens 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1637046Sahrens 		    DMU_POOL_SCRUB_BOOKMARK, sizeof (uint64_t), 4,
1647046Sahrens 		    &dp->dp_scrub_bookmark);
1657046Sahrens 		if (err)
1667046Sahrens 			goto out;
1677046Sahrens 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1687046Sahrens 		    DMU_POOL_SCRUB_ERRORS, sizeof (uint64_t), 1,
1697046Sahrens 		    &spa->spa_scrub_errors);
1707046Sahrens 		if (err)
1717046Sahrens 			goto out;
1727046Sahrens 		if (spa_version(spa) < SPA_VERSION_DSL_SCRUB) {
1737046Sahrens 			/*
1747046Sahrens 			 * A new-type scrub was in progress on an old
1757046Sahrens 			 * pool.  Restart from the beginning, since the
1767046Sahrens 			 * old software may have changed the pool in the
1777046Sahrens 			 * meantime.
1787046Sahrens 			 */
1797046Sahrens 			dsl_pool_scrub_restart(dp);
1807046Sahrens 		}
1817046Sahrens 	} else {
1827046Sahrens 		/*
1837046Sahrens 		 * It's OK if there is no scrub in progress (and if
1847046Sahrens 		 * there was an I/O error, ignore it).
1857046Sahrens 		 */
1867046Sahrens 		err = 0;
1877046Sahrens 	}
1887046Sahrens 
1891544Seschrock out:
190789Sahrens 	rw_exit(&dp->dp_config_rwlock);
1911544Seschrock 	if (err)
1921544Seschrock 		dsl_pool_close(dp);
1931544Seschrock 	else
1941544Seschrock 		*dpp = dp;
195789Sahrens 
1961544Seschrock 	return (err);
197789Sahrens }
198789Sahrens 
199789Sahrens void
200789Sahrens dsl_pool_close(dsl_pool_t *dp)
201789Sahrens {
2027046Sahrens 	/* drop our references from dsl_pool_open() */
2037046Sahrens 
2047046Sahrens 	/*
2057046Sahrens 	 * Since we held the origin_snap from "syncing" context (which
2067046Sahrens 	 * includes pool-opening context), it actually only got a "ref"
2077046Sahrens 	 * and not a hold, so just drop that here.
2087046Sahrens 	 */
2097046Sahrens 	if (dp->dp_origin_snap)
2107046Sahrens 		dsl_dataset_drop_ref(dp->dp_origin_snap, dp);
2111544Seschrock 	if (dp->dp_mos_dir)
2121544Seschrock 		dsl_dir_close(dp->dp_mos_dir, dp);
2131544Seschrock 	if (dp->dp_root_dir)
2141544Seschrock 		dsl_dir_close(dp->dp_root_dir, dp);
215789Sahrens 
216789Sahrens 	/* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */
2171544Seschrock 	if (dp->dp_meta_objset)
2181544Seschrock 		dmu_objset_evict(NULL, dp->dp_meta_objset->os);
219789Sahrens 
220789Sahrens 	txg_list_destroy(&dp->dp_dirty_datasets);
221789Sahrens 	txg_list_destroy(&dp->dp_dirty_dirs);
2225367Sahrens 	list_destroy(&dp->dp_synced_datasets);
223789Sahrens 
2245642Smaybee 	arc_flush(dp->dp_spa);
225789Sahrens 	txg_fini(dp);
2262856Snd150628 	rw_destroy(&dp->dp_config_rwlock);
2276245Smaybee 	mutex_destroy(&dp->dp_lock);
2287046Sahrens 	mutex_destroy(&dp->dp_scrub_cancel_lock);
229*7837SMatthew.Ahrens@Sun.COM 	if (dp->dp_blkstats)
230*7837SMatthew.Ahrens@Sun.COM 		kmem_free(dp->dp_blkstats, sizeof (zfs_all_blkstats_t));
231789Sahrens 	kmem_free(dp, sizeof (dsl_pool_t));
232789Sahrens }
233789Sahrens 
234789Sahrens dsl_pool_t *
2357184Stimh dsl_pool_create(spa_t *spa, nvlist_t *zplprops, uint64_t txg)
236789Sahrens {
237789Sahrens 	int err;
238789Sahrens 	dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
239789Sahrens 	dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg);
2407046Sahrens 	objset_impl_t *osip;
2417046Sahrens 	dsl_dataset_t *ds;
2427046Sahrens 	uint64_t dsobj;
2437046Sahrens 
2447046Sahrens 	/* create and open the MOS (meta-objset) */
245789Sahrens 	dp->dp_meta_objset = &dmu_objset_create_impl(spa,
2463547Smaybee 	    NULL, &dp->dp_meta_rootbp, DMU_OST_META, tx)->os;
247789Sahrens 
248789Sahrens 	/* create the pool directory */
249789Sahrens 	err = zap_create_claim(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
250789Sahrens 	    DMU_OT_OBJECT_DIRECTORY, DMU_OT_NONE, 0, tx);
251789Sahrens 	ASSERT3U(err, ==, 0);
252789Sahrens 
253789Sahrens 	/* create and open the root dir */
2547046Sahrens 	dp->dp_root_dir_obj = dsl_dir_create_sync(dp, NULL, NULL, tx);
2551544Seschrock 	VERIFY(0 == dsl_dir_open_obj(dp, dp->dp_root_dir_obj,
2561544Seschrock 	    NULL, dp, &dp->dp_root_dir));
257789Sahrens 
258789Sahrens 	/* create and open the meta-objset dir */
2597046Sahrens 	(void) dsl_dir_create_sync(dp, dp->dp_root_dir, MOS_DIR_NAME, tx);
2607046Sahrens 	VERIFY(0 == dsl_pool_open_special_dir(dp,
2617046Sahrens 	    MOS_DIR_NAME, &dp->dp_mos_dir));
2627046Sahrens 
2637046Sahrens 	if (spa_version(spa) >= SPA_VERSION_DSL_SCRUB)
2647046Sahrens 		dsl_pool_create_origin(dp, tx);
2657046Sahrens 
2667046Sahrens 	/* create the root dataset */
2677046Sahrens 	dsobj = dsl_dataset_create_sync_dd(dp->dp_root_dir, NULL, 0, tx);
2687046Sahrens 
2697046Sahrens 	/* create the root objset */
2707046Sahrens 	VERIFY(0 == dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
2717046Sahrens 	osip = dmu_objset_create_impl(dp->dp_spa, ds,
2727046Sahrens 	    dsl_dataset_get_blkptr(ds), DMU_OST_ZFS, tx);
2737046Sahrens #ifdef _KERNEL
2747184Stimh 	zfs_create_fs(&osip->os, kcred, zplprops, tx);
2757046Sahrens #endif
2767046Sahrens 	dsl_dataset_rele(ds, FTAG);
277789Sahrens 
278789Sahrens 	dmu_tx_commit(tx);
279789Sahrens 
280789Sahrens 	return (dp);
281789Sahrens }
282789Sahrens 
283789Sahrens void
284789Sahrens dsl_pool_sync(dsl_pool_t *dp, uint64_t txg)
285789Sahrens {
2863547Smaybee 	zio_t *zio;
287789Sahrens 	dmu_tx_t *tx;
2883547Smaybee 	dsl_dir_t *dd;
2893547Smaybee 	dsl_dataset_t *ds;
2903547Smaybee 	dsl_sync_task_group_t *dstg;
291789Sahrens 	objset_impl_t *mosi = dp->dp_meta_objset->os;
2927468SMark.Maybee@Sun.COM 	hrtime_t start, write_time;
2937468SMark.Maybee@Sun.COM 	uint64_t data_written;
2943547Smaybee 	int err;
295789Sahrens 
296789Sahrens 	tx = dmu_tx_create_assigned(dp, txg);
297789Sahrens 
2987468SMark.Maybee@Sun.COM 	dp->dp_read_overhead = 0;
2993547Smaybee 	zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
3003547Smaybee 	while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) {
3013547Smaybee 		if (!list_link_active(&ds->ds_synced_link))
3025367Sahrens 			list_insert_tail(&dp->dp_synced_datasets, ds);
3033897Smaybee 		else
3043897Smaybee 			dmu_buf_rele(ds->ds_dbuf, ds);
3053547Smaybee 		dsl_dataset_sync(ds, zio, tx);
3063547Smaybee 	}
3077468SMark.Maybee@Sun.COM 	DTRACE_PROBE(pool_sync__1setup);
3087468SMark.Maybee@Sun.COM 
3097468SMark.Maybee@Sun.COM 	start = gethrtime();
3103547Smaybee 	err = zio_wait(zio);
3117468SMark.Maybee@Sun.COM 	write_time = gethrtime() - start;
3123547Smaybee 	ASSERT(err == 0);
3137468SMark.Maybee@Sun.COM 	DTRACE_PROBE(pool_sync__2rootzio);
314789Sahrens 
3153547Smaybee 	while (dstg = txg_list_remove(&dp->dp_sync_tasks, txg))
3163547Smaybee 		dsl_sync_task_group_sync(dstg, tx);
3177468SMark.Maybee@Sun.COM 	DTRACE_PROBE(pool_sync__3task);
3187468SMark.Maybee@Sun.COM 
3197468SMark.Maybee@Sun.COM 	start = gethrtime();
3203547Smaybee 	while (dd = txg_list_remove(&dp->dp_dirty_dirs, txg))
3213547Smaybee 		dsl_dir_sync(dd, tx);
3227468SMark.Maybee@Sun.COM 	write_time += gethrtime() - start;
323789Sahrens 
3247046Sahrens 	if (spa_sync_pass(dp->dp_spa) == 1)
3257046Sahrens 		dsl_pool_scrub_sync(dp, tx);
3267046Sahrens 
3277468SMark.Maybee@Sun.COM 	start = gethrtime();
328789Sahrens 	if (list_head(&mosi->os_dirty_dnodes[txg & TXG_MASK]) != NULL ||
329789Sahrens 	    list_head(&mosi->os_free_dnodes[txg & TXG_MASK]) != NULL) {
3303547Smaybee 		zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
3313547Smaybee 		dmu_objset_sync(mosi, zio, tx);
3323547Smaybee 		err = zio_wait(zio);
3333547Smaybee 		ASSERT(err == 0);
334789Sahrens 		dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", "");
335789Sahrens 		spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
336789Sahrens 	}
3377468SMark.Maybee@Sun.COM 	write_time += gethrtime() - start;
3387468SMark.Maybee@Sun.COM 	DTRACE_PROBE2(pool_sync__4io, hrtime_t, write_time,
3397468SMark.Maybee@Sun.COM 	    hrtime_t, dp->dp_read_overhead);
3407468SMark.Maybee@Sun.COM 	write_time -= dp->dp_read_overhead;
341789Sahrens 
342789Sahrens 	dmu_tx_commit(tx);
3437468SMark.Maybee@Sun.COM 
3447468SMark.Maybee@Sun.COM 	data_written = dp->dp_space_towrite[txg & TXG_MASK];
3457468SMark.Maybee@Sun.COM 	dp->dp_space_towrite[txg & TXG_MASK] = 0;
3467468SMark.Maybee@Sun.COM 	ASSERT(dp->dp_tempreserved[txg & TXG_MASK] == 0);
3477468SMark.Maybee@Sun.COM 
3487468SMark.Maybee@Sun.COM 	/*
3497468SMark.Maybee@Sun.COM 	 * If the write limit max has not been explicitly set, set it
3507468SMark.Maybee@Sun.COM 	 * to a fraction of available physical memory (default 1/8th).
3517468SMark.Maybee@Sun.COM 	 * Note that we must inflate the limit because the spa
3527468SMark.Maybee@Sun.COM 	 * inflates write sizes to account for data replication.
3537468SMark.Maybee@Sun.COM 	 * Check this each sync phase to catch changing memory size.
3547468SMark.Maybee@Sun.COM 	 */
3557468SMark.Maybee@Sun.COM 	if (physmem != old_physmem && zfs_write_limit_shift) {
3567468SMark.Maybee@Sun.COM 		mutex_enter(&zfs_write_limit_lock);
3577468SMark.Maybee@Sun.COM 		old_physmem = physmem;
3587468SMark.Maybee@Sun.COM 		zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift;
3597468SMark.Maybee@Sun.COM 		zfs_write_limit_inflated = MAX(zfs_write_limit_min,
3607468SMark.Maybee@Sun.COM 		    spa_get_asize(dp->dp_spa, zfs_write_limit_max));
3617468SMark.Maybee@Sun.COM 		mutex_exit(&zfs_write_limit_lock);
3627468SMark.Maybee@Sun.COM 	}
3637468SMark.Maybee@Sun.COM 
3647468SMark.Maybee@Sun.COM 	/*
3657468SMark.Maybee@Sun.COM 	 * Attempt to keep the sync time consistent by adjusting the
3667468SMark.Maybee@Sun.COM 	 * amount of write traffic allowed into each transaction group.
3677468SMark.Maybee@Sun.COM 	 * Weight the throughput calculation towards the current value:
3687468SMark.Maybee@Sun.COM 	 * 	thru = 3/4 old_thru + 1/4 new_thru
3697468SMark.Maybee@Sun.COM 	 */
3707468SMark.Maybee@Sun.COM 	ASSERT(zfs_write_limit_min > 0);
3717468SMark.Maybee@Sun.COM 	if (data_written > zfs_write_limit_min / 8 && write_time > 0) {
3727468SMark.Maybee@Sun.COM 		uint64_t throughput = (data_written * NANOSEC) / write_time;
3737468SMark.Maybee@Sun.COM 		if (dp->dp_throughput)
3747468SMark.Maybee@Sun.COM 			dp->dp_throughput = throughput / 4 +
3757468SMark.Maybee@Sun.COM 			    3 * dp->dp_throughput / 4;
3767468SMark.Maybee@Sun.COM 		else
3777468SMark.Maybee@Sun.COM 			dp->dp_throughput = throughput;
3787468SMark.Maybee@Sun.COM 		dp->dp_write_limit = MIN(zfs_write_limit_inflated,
3797468SMark.Maybee@Sun.COM 		    MAX(zfs_write_limit_min,
3807468SMark.Maybee@Sun.COM 		    dp->dp_throughput * zfs_txg_synctime));
3817468SMark.Maybee@Sun.COM 	}
382789Sahrens }
383789Sahrens 
384789Sahrens void
385789Sahrens dsl_pool_zil_clean(dsl_pool_t *dp)
386789Sahrens {
387789Sahrens 	dsl_dataset_t *ds;
388789Sahrens 
3895367Sahrens 	while (ds = list_head(&dp->dp_synced_datasets)) {
3905367Sahrens 		list_remove(&dp->dp_synced_datasets, ds);
391789Sahrens 		ASSERT(ds->ds_user_ptr != NULL);
392789Sahrens 		zil_clean(((objset_impl_t *)ds->ds_user_ptr)->os_zil);
3933897Smaybee 		dmu_buf_rele(ds->ds_dbuf, ds);
394789Sahrens 	}
395789Sahrens }
396789Sahrens 
3973547Smaybee /*
3983547Smaybee  * TRUE if the current thread is the tx_sync_thread or if we
3993547Smaybee  * are being called from SPA context during pool initialization.
4003547Smaybee  */
401789Sahrens int
402789Sahrens dsl_pool_sync_context(dsl_pool_t *dp)
403789Sahrens {
404789Sahrens 	return (curthread == dp->dp_tx.tx_sync_thread ||
4053547Smaybee 	    spa_get_dsl(dp->dp_spa) == NULL);
406789Sahrens }
407789Sahrens 
408789Sahrens uint64_t
409789Sahrens dsl_pool_adjustedsize(dsl_pool_t *dp, boolean_t netfree)
410789Sahrens {
411789Sahrens 	uint64_t space, resv;
412789Sahrens 
413789Sahrens 	/*
4141775Sbillm 	 * Reserve about 1.6% (1/64), or at least 32MB, for allocation
415789Sahrens 	 * efficiency.
416789Sahrens 	 * XXX The intent log is not accounted for, so it must fit
417789Sahrens 	 * within this slop.
418789Sahrens 	 *
419789Sahrens 	 * If we're trying to assess whether it's OK to do a free,
420789Sahrens 	 * cut the reservation in half to allow forward progress
421789Sahrens 	 * (e.g. make it possible to rm(1) files from a full pool).
422789Sahrens 	 */
4232082Seschrock 	space = spa_get_dspace(dp->dp_spa);
4241775Sbillm 	resv = MAX(space >> 6, SPA_MINDEVSIZE >> 1);
425789Sahrens 	if (netfree)
426789Sahrens 		resv >>= 1;
427789Sahrens 
428789Sahrens 	return (space - resv);
429789Sahrens }
4306245Smaybee 
4316245Smaybee int
4326245Smaybee dsl_pool_tempreserve_space(dsl_pool_t *dp, uint64_t space, dmu_tx_t *tx)
4336245Smaybee {
4346245Smaybee 	uint64_t reserved = 0;
4356245Smaybee 	uint64_t write_limit = (zfs_write_limit_override ?
4366245Smaybee 	    zfs_write_limit_override : dp->dp_write_limit);
4376245Smaybee 
4386245Smaybee 	if (zfs_no_write_throttle) {
4396643Seschrock 		atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK],
4406643Seschrock 		    space);
4416245Smaybee 		return (0);
4426245Smaybee 	}
4436245Smaybee 
4446245Smaybee 	/*
4456245Smaybee 	 * Check to see if we have exceeded the maximum allowed IO for
4466245Smaybee 	 * this transaction group.  We can do this without locks since
4476245Smaybee 	 * a little slop here is ok.  Note that we do the reserved check
4486245Smaybee 	 * with only half the requested reserve: this is because the
4496245Smaybee 	 * reserve requests are worst-case, and we really don't want to
4506245Smaybee 	 * throttle based off of worst-case estimates.
4516245Smaybee 	 */
4526245Smaybee 	if (write_limit > 0) {
4536245Smaybee 		reserved = dp->dp_space_towrite[tx->tx_txg & TXG_MASK]
4546245Smaybee 		    + dp->dp_tempreserved[tx->tx_txg & TXG_MASK] / 2;
4556245Smaybee 
4566245Smaybee 		if (reserved && reserved > write_limit)
4576245Smaybee 			return (ERESTART);
4586245Smaybee 	}
4596245Smaybee 
4606245Smaybee 	atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], space);
4616245Smaybee 
4626245Smaybee 	/*
4636245Smaybee 	 * If this transaction group is over 7/8ths capacity, delay
4646245Smaybee 	 * the caller 1 clock tick.  This will slow down the "fill"
4656245Smaybee 	 * rate until the sync process can catch up with us.
4666245Smaybee 	 */
4676740Sgw25295 	if (reserved && reserved > (write_limit - (write_limit >> 3)))
4686245Smaybee 		txg_delay(dp, tx->tx_txg, 1);
4696245Smaybee 
4706245Smaybee 	return (0);
4716245Smaybee }
4726245Smaybee 
4736245Smaybee void
4746245Smaybee dsl_pool_tempreserve_clear(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
4756245Smaybee {
4766245Smaybee 	ASSERT(dp->dp_tempreserved[tx->tx_txg & TXG_MASK] >= space);
4776245Smaybee 	atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], -space);
4786245Smaybee }
4796245Smaybee 
4806245Smaybee void
4816245Smaybee dsl_pool_memory_pressure(dsl_pool_t *dp)
4826245Smaybee {
4836245Smaybee 	uint64_t space_inuse = 0;
4846245Smaybee 	int i;
4856245Smaybee 
4866245Smaybee 	if (dp->dp_write_limit == zfs_write_limit_min)
4876245Smaybee 		return;
4886245Smaybee 
4896245Smaybee 	for (i = 0; i < TXG_SIZE; i++) {
4906245Smaybee 		space_inuse += dp->dp_space_towrite[i];
4916245Smaybee 		space_inuse += dp->dp_tempreserved[i];
4926245Smaybee 	}
4936245Smaybee 	dp->dp_write_limit = MAX(zfs_write_limit_min,
4946245Smaybee 	    MIN(dp->dp_write_limit, space_inuse / 4));
4956245Smaybee }
4966245Smaybee 
4976245Smaybee void
4986245Smaybee dsl_pool_willuse_space(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
4996245Smaybee {
5006245Smaybee 	if (space > 0) {
5016245Smaybee 		mutex_enter(&dp->dp_lock);
5026245Smaybee 		dp->dp_space_towrite[tx->tx_txg & TXG_MASK] += space;
5036245Smaybee 		mutex_exit(&dp->dp_lock);
5046245Smaybee 	}
5056245Smaybee }
5067046Sahrens 
5077046Sahrens /* ARGSUSED */
5087046Sahrens static int
5097046Sahrens upgrade_clones_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
5107046Sahrens {
5117046Sahrens 	dmu_tx_t *tx = arg;
5127046Sahrens 	dsl_dataset_t *ds, *prev = NULL;
5137046Sahrens 	int err;
5147046Sahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
5157046Sahrens 
5167046Sahrens 	err = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds);
5177046Sahrens 	if (err)
5187046Sahrens 		return (err);
5197046Sahrens 
5207046Sahrens 	while (ds->ds_phys->ds_prev_snap_obj != 0) {
5217046Sahrens 		err = dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
5227046Sahrens 		    FTAG, &prev);
5237046Sahrens 		if (err) {
5247046Sahrens 			dsl_dataset_rele(ds, FTAG);
5257046Sahrens 			return (err);
5267046Sahrens 		}
5277046Sahrens 
5287046Sahrens 		if (prev->ds_phys->ds_next_snap_obj != ds->ds_object)
5297046Sahrens 			break;
5307046Sahrens 		dsl_dataset_rele(ds, FTAG);
5317046Sahrens 		ds = prev;
5327046Sahrens 		prev = NULL;
5337046Sahrens 	}
5347046Sahrens 
5357046Sahrens 	if (prev == NULL) {
5367046Sahrens 		prev = dp->dp_origin_snap;
5377046Sahrens 
5387046Sahrens 		/*
5397046Sahrens 		 * The $ORIGIN can't have any data, or the accounting
5407046Sahrens 		 * will be wrong.
5417046Sahrens 		 */
5427046Sahrens 		ASSERT(prev->ds_phys->ds_bp.blk_birth == 0);
5437046Sahrens 
5447046Sahrens 		/* The origin doesn't get attached to itself */
5457046Sahrens 		if (ds->ds_object == prev->ds_object) {
5467046Sahrens 			dsl_dataset_rele(ds, FTAG);
5477046Sahrens 			return (0);
5487046Sahrens 		}
5497046Sahrens 
5507046Sahrens 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
5517046Sahrens 		ds->ds_phys->ds_prev_snap_obj = prev->ds_object;
5527046Sahrens 		ds->ds_phys->ds_prev_snap_txg = prev->ds_phys->ds_creation_txg;
5537046Sahrens 
5547046Sahrens 		dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
5557046Sahrens 		ds->ds_dir->dd_phys->dd_origin_obj = prev->ds_object;
5567046Sahrens 
5577046Sahrens 		dmu_buf_will_dirty(prev->ds_dbuf, tx);
5587046Sahrens 		prev->ds_phys->ds_num_children++;
5597046Sahrens 
5607046Sahrens 		if (ds->ds_phys->ds_next_snap_obj == 0) {
5617046Sahrens 			ASSERT(ds->ds_prev == NULL);
5627046Sahrens 			VERIFY(0 == dsl_dataset_hold_obj(dp,
5637046Sahrens 			    ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
5647046Sahrens 		}
5657046Sahrens 	}
5667046Sahrens 
5677046Sahrens 	ASSERT(ds->ds_dir->dd_phys->dd_origin_obj == prev->ds_object);
5687046Sahrens 	ASSERT(ds->ds_phys->ds_prev_snap_obj == prev->ds_object);
5697046Sahrens 
5707046Sahrens 	if (prev->ds_phys->ds_next_clones_obj == 0) {
5717046Sahrens 		prev->ds_phys->ds_next_clones_obj =
5727046Sahrens 		    zap_create(dp->dp_meta_objset,
5737046Sahrens 		    DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
5747046Sahrens 	}
5757046Sahrens 	VERIFY(0 == zap_add_int(dp->dp_meta_objset,
5767046Sahrens 	    prev->ds_phys->ds_next_clones_obj, ds->ds_object, tx));
5777046Sahrens 
5787046Sahrens 	dsl_dataset_rele(ds, FTAG);
5797046Sahrens 	if (prev != dp->dp_origin_snap)
5807046Sahrens 		dsl_dataset_rele(prev, FTAG);
5817046Sahrens 	return (0);
5827046Sahrens }
5837046Sahrens 
5847046Sahrens void
5857046Sahrens dsl_pool_upgrade_clones(dsl_pool_t *dp, dmu_tx_t *tx)
5867046Sahrens {
5877046Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
5887046Sahrens 	ASSERT(dp->dp_origin_snap != NULL);
5897046Sahrens 
5907046Sahrens 	(void) dmu_objset_find_spa(dp->dp_spa, NULL, upgrade_clones_cb,
5917046Sahrens 	    tx, DS_FIND_CHILDREN);
5927046Sahrens }
5937046Sahrens 
5947046Sahrens void
5957046Sahrens dsl_pool_create_origin(dsl_pool_t *dp, dmu_tx_t *tx)
5967046Sahrens {
5977046Sahrens 	uint64_t dsobj;
5987046Sahrens 	dsl_dataset_t *ds;
5997046Sahrens 
6007046Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
6017046Sahrens 	ASSERT(dp->dp_origin_snap == NULL);
6027046Sahrens 
6037046Sahrens 	/* create the origin dir, ds, & snap-ds */
6047046Sahrens 	rw_enter(&dp->dp_config_rwlock, RW_WRITER);
6057046Sahrens 	dsobj = dsl_dataset_create_sync(dp->dp_root_dir, ORIGIN_DIR_NAME,
6067046Sahrens 	    NULL, 0, kcred, tx);
6077046Sahrens 	VERIFY(0 == dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
6087046Sahrens 	dsl_dataset_snapshot_sync(ds, ORIGIN_DIR_NAME, kcred, tx);
6097046Sahrens 	VERIFY(0 == dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
6107046Sahrens 	    dp, &dp->dp_origin_snap));
6117046Sahrens 	dsl_dataset_rele(ds, FTAG);
6127046Sahrens 	rw_exit(&dp->dp_config_rwlock);
6137046Sahrens }
614