xref: /onnv-gate/usr/src/uts/common/fs/zfs/dsl_pool.c (revision 7184:9508660f9c27)
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>
397046Sahrens #include <sys/zfs_znode.h>
407046Sahrens #include <sys/spa_impl.h>
41789Sahrens 
426245Smaybee int zfs_no_write_throttle = 0;
436245Smaybee uint64_t zfs_write_limit_override = 0;
446245Smaybee 
457046Sahrens 
461544Seschrock static int
477046Sahrens dsl_pool_open_special_dir(dsl_pool_t *dp, const char *name, dsl_dir_t **ddp)
48789Sahrens {
49789Sahrens 	uint64_t obj;
50789Sahrens 	int err;
51789Sahrens 
52789Sahrens 	err = zap_lookup(dp->dp_meta_objset,
53789Sahrens 	    dp->dp_root_dir->dd_phys->dd_child_dir_zapobj,
547046Sahrens 	    name, sizeof (obj), 1, &obj);
551544Seschrock 	if (err)
561544Seschrock 		return (err);
57789Sahrens 
587046Sahrens 	return (dsl_dir_open_obj(dp, obj, name, dp, ddp));
59789Sahrens }
60789Sahrens 
61789Sahrens static dsl_pool_t *
62789Sahrens dsl_pool_open_impl(spa_t *spa, uint64_t txg)
63789Sahrens {
64789Sahrens 	dsl_pool_t *dp;
65789Sahrens 	blkptr_t *bp = spa_get_rootblkptr(spa);
666245Smaybee 	extern uint64_t zfs_write_limit_min;
67789Sahrens 
68789Sahrens 	dp = kmem_zalloc(sizeof (dsl_pool_t), KM_SLEEP);
69789Sahrens 	dp->dp_spa = spa;
70789Sahrens 	dp->dp_meta_rootbp = *bp;
712856Snd150628 	rw_init(&dp->dp_config_rwlock, NULL, RW_DEFAULT, NULL);
726245Smaybee 	dp->dp_write_limit = zfs_write_limit_min;
73789Sahrens 	txg_init(dp, txg);
74789Sahrens 
75789Sahrens 	txg_list_create(&dp->dp_dirty_datasets,
76789Sahrens 	    offsetof(dsl_dataset_t, ds_dirty_link));
77789Sahrens 	txg_list_create(&dp->dp_dirty_dirs,
78789Sahrens 	    offsetof(dsl_dir_t, dd_dirty_link));
792199Sahrens 	txg_list_create(&dp->dp_sync_tasks,
802199Sahrens 	    offsetof(dsl_sync_task_group_t, dstg_node));
815367Sahrens 	list_create(&dp->dp_synced_datasets, sizeof (dsl_dataset_t),
82789Sahrens 	    offsetof(dsl_dataset_t, ds_synced_link));
83789Sahrens 
846245Smaybee 	mutex_init(&dp->dp_lock, NULL, MUTEX_DEFAULT, NULL);
857046Sahrens 	mutex_init(&dp->dp_scrub_cancel_lock, NULL, MUTEX_DEFAULT, NULL);
866245Smaybee 
87789Sahrens 	return (dp);
88789Sahrens }
89789Sahrens 
901544Seschrock int
911544Seschrock dsl_pool_open(spa_t *spa, uint64_t txg, dsl_pool_t **dpp)
92789Sahrens {
93789Sahrens 	int err;
94789Sahrens 	dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
957046Sahrens 	dsl_dir_t *dd;
967046Sahrens 	dsl_dataset_t *ds;
971544Seschrock 	objset_impl_t *osi;
98789Sahrens 
997046Sahrens 	rw_enter(&dp->dp_config_rwlock, RW_WRITER);
1001544Seschrock 	err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp, &osi);
1011544Seschrock 	if (err)
1021544Seschrock 		goto out;
1031544Seschrock 	dp->dp_meta_objset = &osi->os;
1041544Seschrock 
105789Sahrens 	err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
106789Sahrens 	    DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1,
107789Sahrens 	    &dp->dp_root_dir_obj);
1081544Seschrock 	if (err)
1091544Seschrock 		goto out;
1101544Seschrock 
1111544Seschrock 	err = dsl_dir_open_obj(dp, dp->dp_root_dir_obj,
1121544Seschrock 	    NULL, dp, &dp->dp_root_dir);
1131544Seschrock 	if (err)
1141544Seschrock 		goto out;
115789Sahrens 
1167046Sahrens 	err = dsl_pool_open_special_dir(dp, MOS_DIR_NAME, &dp->dp_mos_dir);
1171544Seschrock 	if (err)
1181544Seschrock 		goto out;
1191544Seschrock 
1207046Sahrens 	if (spa_version(spa) >= SPA_VERSION_ORIGIN) {
1217046Sahrens 		err = dsl_pool_open_special_dir(dp, ORIGIN_DIR_NAME, &dd);
1227046Sahrens 		if (err)
1237046Sahrens 			goto out;
1247046Sahrens 		err = dsl_dataset_hold_obj(dp, dd->dd_phys->dd_head_dataset_obj,
1257046Sahrens 		    FTAG, &ds);
1267046Sahrens 		if (err)
1277046Sahrens 			goto out;
1287046Sahrens 		err = dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
1297046Sahrens 		    dp, &dp->dp_origin_snap);
1307046Sahrens 		if (err)
1317046Sahrens 			goto out;
1327046Sahrens 		dsl_dataset_rele(ds, FTAG);
1337046Sahrens 		dsl_dir_close(dd, dp);
1347046Sahrens 	}
1357046Sahrens 
1367046Sahrens 	/* get scrub status */
1377046Sahrens 	err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1387046Sahrens 	    DMU_POOL_SCRUB_FUNC, sizeof (uint32_t), 1,
1397046Sahrens 	    &dp->dp_scrub_func);
1407046Sahrens 	if (err == 0) {
1417046Sahrens 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1427046Sahrens 		    DMU_POOL_SCRUB_QUEUE, sizeof (uint64_t), 1,
1437046Sahrens 		    &dp->dp_scrub_queue_obj);
1447046Sahrens 		if (err)
1457046Sahrens 			goto out;
1467046Sahrens 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1477046Sahrens 		    DMU_POOL_SCRUB_MIN_TXG, sizeof (uint64_t), 1,
1487046Sahrens 		    &dp->dp_scrub_min_txg);
1497046Sahrens 		if (err)
1507046Sahrens 			goto out;
1517046Sahrens 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1527046Sahrens 		    DMU_POOL_SCRUB_MAX_TXG, sizeof (uint64_t), 1,
1537046Sahrens 		    &dp->dp_scrub_max_txg);
1547046Sahrens 		if (err)
1557046Sahrens 			goto out;
1567046Sahrens 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1577046Sahrens 		    DMU_POOL_SCRUB_BOOKMARK, sizeof (uint64_t), 4,
1587046Sahrens 		    &dp->dp_scrub_bookmark);
1597046Sahrens 		if (err)
1607046Sahrens 			goto out;
1617046Sahrens 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1627046Sahrens 		    DMU_POOL_SCRUB_ERRORS, sizeof (uint64_t), 1,
1637046Sahrens 		    &spa->spa_scrub_errors);
1647046Sahrens 		if (err)
1657046Sahrens 			goto out;
1667046Sahrens 		if (spa_version(spa) < SPA_VERSION_DSL_SCRUB) {
1677046Sahrens 			/*
1687046Sahrens 			 * A new-type scrub was in progress on an old
1697046Sahrens 			 * pool.  Restart from the beginning, since the
1707046Sahrens 			 * old software may have changed the pool in the
1717046Sahrens 			 * meantime.
1727046Sahrens 			 */
1737046Sahrens 			dsl_pool_scrub_restart(dp);
1747046Sahrens 		}
1757046Sahrens 	} else {
1767046Sahrens 		/*
1777046Sahrens 		 * It's OK if there is no scrub in progress (and if
1787046Sahrens 		 * there was an I/O error, ignore it).
1797046Sahrens 		 */
1807046Sahrens 		err = 0;
1817046Sahrens 	}
1827046Sahrens 
1831544Seschrock out:
184789Sahrens 	rw_exit(&dp->dp_config_rwlock);
1851544Seschrock 	if (err)
1861544Seschrock 		dsl_pool_close(dp);
1871544Seschrock 	else
1881544Seschrock 		*dpp = dp;
189789Sahrens 
1901544Seschrock 	return (err);
191789Sahrens }
192789Sahrens 
193789Sahrens void
194789Sahrens dsl_pool_close(dsl_pool_t *dp)
195789Sahrens {
1967046Sahrens 	/* drop our references from dsl_pool_open() */
1977046Sahrens 
1987046Sahrens 	/*
1997046Sahrens 	 * Since we held the origin_snap from "syncing" context (which
2007046Sahrens 	 * includes pool-opening context), it actually only got a "ref"
2017046Sahrens 	 * and not a hold, so just drop that here.
2027046Sahrens 	 */
2037046Sahrens 	if (dp->dp_origin_snap)
2047046Sahrens 		dsl_dataset_drop_ref(dp->dp_origin_snap, dp);
2051544Seschrock 	if (dp->dp_mos_dir)
2061544Seschrock 		dsl_dir_close(dp->dp_mos_dir, dp);
2071544Seschrock 	if (dp->dp_root_dir)
2081544Seschrock 		dsl_dir_close(dp->dp_root_dir, dp);
209789Sahrens 
210789Sahrens 	/* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */
2111544Seschrock 	if (dp->dp_meta_objset)
2121544Seschrock 		dmu_objset_evict(NULL, dp->dp_meta_objset->os);
213789Sahrens 
214789Sahrens 	txg_list_destroy(&dp->dp_dirty_datasets);
215789Sahrens 	txg_list_destroy(&dp->dp_dirty_dirs);
2165367Sahrens 	list_destroy(&dp->dp_synced_datasets);
217789Sahrens 
2185642Smaybee 	arc_flush(dp->dp_spa);
219789Sahrens 	txg_fini(dp);
2202856Snd150628 	rw_destroy(&dp->dp_config_rwlock);
2216245Smaybee 	mutex_destroy(&dp->dp_lock);
2227046Sahrens 	mutex_destroy(&dp->dp_scrub_cancel_lock);
223789Sahrens 	kmem_free(dp, sizeof (dsl_pool_t));
224789Sahrens }
225789Sahrens 
226789Sahrens dsl_pool_t *
227*7184Stimh dsl_pool_create(spa_t *spa, nvlist_t *zplprops, uint64_t txg)
228789Sahrens {
229789Sahrens 	int err;
230789Sahrens 	dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
231789Sahrens 	dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg);
2327046Sahrens 	objset_impl_t *osip;
2337046Sahrens 	dsl_dataset_t *ds;
2347046Sahrens 	uint64_t dsobj;
2357046Sahrens 
2367046Sahrens 	/* create and open the MOS (meta-objset) */
237789Sahrens 	dp->dp_meta_objset = &dmu_objset_create_impl(spa,
2383547Smaybee 	    NULL, &dp->dp_meta_rootbp, DMU_OST_META, tx)->os;
239789Sahrens 
240789Sahrens 	/* create the pool directory */
241789Sahrens 	err = zap_create_claim(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
242789Sahrens 	    DMU_OT_OBJECT_DIRECTORY, DMU_OT_NONE, 0, tx);
243789Sahrens 	ASSERT3U(err, ==, 0);
244789Sahrens 
245789Sahrens 	/* create and open the root dir */
2467046Sahrens 	dp->dp_root_dir_obj = dsl_dir_create_sync(dp, NULL, NULL, tx);
2471544Seschrock 	VERIFY(0 == dsl_dir_open_obj(dp, dp->dp_root_dir_obj,
2481544Seschrock 	    NULL, dp, &dp->dp_root_dir));
249789Sahrens 
250789Sahrens 	/* create and open the meta-objset dir */
2517046Sahrens 	(void) dsl_dir_create_sync(dp, dp->dp_root_dir, MOS_DIR_NAME, tx);
2527046Sahrens 	VERIFY(0 == dsl_pool_open_special_dir(dp,
2537046Sahrens 	    MOS_DIR_NAME, &dp->dp_mos_dir));
2547046Sahrens 
2557046Sahrens 	if (spa_version(spa) >= SPA_VERSION_DSL_SCRUB)
2567046Sahrens 		dsl_pool_create_origin(dp, tx);
2577046Sahrens 
2587046Sahrens 	/* create the root dataset */
2597046Sahrens 	dsobj = dsl_dataset_create_sync_dd(dp->dp_root_dir, NULL, 0, tx);
2607046Sahrens 
2617046Sahrens 	/* create the root objset */
2627046Sahrens 	VERIFY(0 == dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
2637046Sahrens 	osip = dmu_objset_create_impl(dp->dp_spa, ds,
2647046Sahrens 	    dsl_dataset_get_blkptr(ds), DMU_OST_ZFS, tx);
2657046Sahrens #ifdef _KERNEL
266*7184Stimh 	zfs_create_fs(&osip->os, kcred, zplprops, tx);
2677046Sahrens #endif
2687046Sahrens 	dsl_dataset_rele(ds, FTAG);
269789Sahrens 
270789Sahrens 	dmu_tx_commit(tx);
271789Sahrens 
272789Sahrens 	return (dp);
273789Sahrens }
274789Sahrens 
275789Sahrens void
276789Sahrens dsl_pool_sync(dsl_pool_t *dp, uint64_t txg)
277789Sahrens {
2783547Smaybee 	zio_t *zio;
279789Sahrens 	dmu_tx_t *tx;
2803547Smaybee 	dsl_dir_t *dd;
2813547Smaybee 	dsl_dataset_t *ds;
2823547Smaybee 	dsl_sync_task_group_t *dstg;
283789Sahrens 	objset_impl_t *mosi = dp->dp_meta_objset->os;
2843547Smaybee 	int err;
285789Sahrens 
286789Sahrens 	tx = dmu_tx_create_assigned(dp, txg);
287789Sahrens 
2883547Smaybee 	zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
2893547Smaybee 	while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) {
2903547Smaybee 		if (!list_link_active(&ds->ds_synced_link))
2915367Sahrens 			list_insert_tail(&dp->dp_synced_datasets, ds);
2923897Smaybee 		else
2933897Smaybee 			dmu_buf_rele(ds->ds_dbuf, ds);
2943547Smaybee 		dsl_dataset_sync(ds, zio, tx);
2953547Smaybee 	}
2963547Smaybee 	err = zio_wait(zio);
2973547Smaybee 	ASSERT(err == 0);
298789Sahrens 
2993547Smaybee 	while (dstg = txg_list_remove(&dp->dp_sync_tasks, txg))
3003547Smaybee 		dsl_sync_task_group_sync(dstg, tx);
3013547Smaybee 	while (dd = txg_list_remove(&dp->dp_dirty_dirs, txg))
3023547Smaybee 		dsl_dir_sync(dd, tx);
303789Sahrens 
3047046Sahrens 	if (spa_sync_pass(dp->dp_spa) == 1)
3057046Sahrens 		dsl_pool_scrub_sync(dp, tx);
3067046Sahrens 
307789Sahrens 	if (list_head(&mosi->os_dirty_dnodes[txg & TXG_MASK]) != NULL ||
308789Sahrens 	    list_head(&mosi->os_free_dnodes[txg & TXG_MASK]) != NULL) {
3093547Smaybee 		zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
3103547Smaybee 		dmu_objset_sync(mosi, zio, tx);
3113547Smaybee 		err = zio_wait(zio);
3123547Smaybee 		ASSERT(err == 0);
313789Sahrens 		dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", "");
314789Sahrens 		spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
315789Sahrens 	}
316789Sahrens 
317789Sahrens 	dmu_tx_commit(tx);
318789Sahrens }
319789Sahrens 
320789Sahrens void
321789Sahrens dsl_pool_zil_clean(dsl_pool_t *dp)
322789Sahrens {
323789Sahrens 	dsl_dataset_t *ds;
324789Sahrens 
3255367Sahrens 	while (ds = list_head(&dp->dp_synced_datasets)) {
3265367Sahrens 		list_remove(&dp->dp_synced_datasets, ds);
327789Sahrens 		ASSERT(ds->ds_user_ptr != NULL);
328789Sahrens 		zil_clean(((objset_impl_t *)ds->ds_user_ptr)->os_zil);
3293897Smaybee 		dmu_buf_rele(ds->ds_dbuf, ds);
330789Sahrens 	}
331789Sahrens }
332789Sahrens 
3333547Smaybee /*
3343547Smaybee  * TRUE if the current thread is the tx_sync_thread or if we
3353547Smaybee  * are being called from SPA context during pool initialization.
3363547Smaybee  */
337789Sahrens int
338789Sahrens dsl_pool_sync_context(dsl_pool_t *dp)
339789Sahrens {
340789Sahrens 	return (curthread == dp->dp_tx.tx_sync_thread ||
3413547Smaybee 	    spa_get_dsl(dp->dp_spa) == NULL);
342789Sahrens }
343789Sahrens 
344789Sahrens uint64_t
345789Sahrens dsl_pool_adjustedsize(dsl_pool_t *dp, boolean_t netfree)
346789Sahrens {
347789Sahrens 	uint64_t space, resv;
348789Sahrens 
349789Sahrens 	/*
3501775Sbillm 	 * Reserve about 1.6% (1/64), or at least 32MB, for allocation
351789Sahrens 	 * efficiency.
352789Sahrens 	 * XXX The intent log is not accounted for, so it must fit
353789Sahrens 	 * within this slop.
354789Sahrens 	 *
355789Sahrens 	 * If we're trying to assess whether it's OK to do a free,
356789Sahrens 	 * cut the reservation in half to allow forward progress
357789Sahrens 	 * (e.g. make it possible to rm(1) files from a full pool).
358789Sahrens 	 */
3592082Seschrock 	space = spa_get_dspace(dp->dp_spa);
3601775Sbillm 	resv = MAX(space >> 6, SPA_MINDEVSIZE >> 1);
361789Sahrens 	if (netfree)
362789Sahrens 		resv >>= 1;
363789Sahrens 
364789Sahrens 	return (space - resv);
365789Sahrens }
3666245Smaybee 
3676245Smaybee int
3686245Smaybee dsl_pool_tempreserve_space(dsl_pool_t *dp, uint64_t space, dmu_tx_t *tx)
3696245Smaybee {
3706245Smaybee 	uint64_t reserved = 0;
3716245Smaybee 	uint64_t write_limit = (zfs_write_limit_override ?
3726245Smaybee 	    zfs_write_limit_override : dp->dp_write_limit);
3736245Smaybee 
3746245Smaybee 	if (zfs_no_write_throttle) {
3756643Seschrock 		atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK],
3766643Seschrock 		    space);
3776245Smaybee 		return (0);
3786245Smaybee 	}
3796245Smaybee 
3806245Smaybee 	/*
3816245Smaybee 	 * Check to see if we have exceeded the maximum allowed IO for
3826245Smaybee 	 * this transaction group.  We can do this without locks since
3836245Smaybee 	 * a little slop here is ok.  Note that we do the reserved check
3846245Smaybee 	 * with only half the requested reserve: this is because the
3856245Smaybee 	 * reserve requests are worst-case, and we really don't want to
3866245Smaybee 	 * throttle based off of worst-case estimates.
3876245Smaybee 	 */
3886245Smaybee 	if (write_limit > 0) {
3896245Smaybee 		reserved = dp->dp_space_towrite[tx->tx_txg & TXG_MASK]
3906245Smaybee 		    + dp->dp_tempreserved[tx->tx_txg & TXG_MASK] / 2;
3916245Smaybee 
3926245Smaybee 		if (reserved && reserved > write_limit)
3936245Smaybee 			return (ERESTART);
3946245Smaybee 	}
3956245Smaybee 
3966245Smaybee 	atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], space);
3976245Smaybee 
3986245Smaybee 	/*
3996245Smaybee 	 * If this transaction group is over 7/8ths capacity, delay
4006245Smaybee 	 * the caller 1 clock tick.  This will slow down the "fill"
4016245Smaybee 	 * rate until the sync process can catch up with us.
4026245Smaybee 	 */
4036740Sgw25295 	if (reserved && reserved > (write_limit - (write_limit >> 3)))
4046245Smaybee 		txg_delay(dp, tx->tx_txg, 1);
4056245Smaybee 
4066245Smaybee 	return (0);
4076245Smaybee }
4086245Smaybee 
4096245Smaybee void
4106245Smaybee dsl_pool_tempreserve_clear(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
4116245Smaybee {
4126245Smaybee 	ASSERT(dp->dp_tempreserved[tx->tx_txg & TXG_MASK] >= space);
4136245Smaybee 	atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], -space);
4146245Smaybee }
4156245Smaybee 
4166245Smaybee void
4176245Smaybee dsl_pool_memory_pressure(dsl_pool_t *dp)
4186245Smaybee {
4196245Smaybee 	extern uint64_t zfs_write_limit_min;
4206245Smaybee 	uint64_t space_inuse = 0;
4216245Smaybee 	int i;
4226245Smaybee 
4236245Smaybee 	if (dp->dp_write_limit == zfs_write_limit_min)
4246245Smaybee 		return;
4256245Smaybee 
4266245Smaybee 	for (i = 0; i < TXG_SIZE; i++) {
4276245Smaybee 		space_inuse += dp->dp_space_towrite[i];
4286245Smaybee 		space_inuse += dp->dp_tempreserved[i];
4296245Smaybee 	}
4306245Smaybee 	dp->dp_write_limit = MAX(zfs_write_limit_min,
4316245Smaybee 	    MIN(dp->dp_write_limit, space_inuse / 4));
4326245Smaybee }
4336245Smaybee 
4346245Smaybee void
4356245Smaybee dsl_pool_willuse_space(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
4366245Smaybee {
4376245Smaybee 	if (space > 0) {
4386245Smaybee 		mutex_enter(&dp->dp_lock);
4396245Smaybee 		dp->dp_space_towrite[tx->tx_txg & TXG_MASK] += space;
4406245Smaybee 		mutex_exit(&dp->dp_lock);
4416245Smaybee 	}
4426245Smaybee }
4437046Sahrens 
4447046Sahrens /* ARGSUSED */
4457046Sahrens static int
4467046Sahrens upgrade_clones_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
4477046Sahrens {
4487046Sahrens 	dmu_tx_t *tx = arg;
4497046Sahrens 	dsl_dataset_t *ds, *prev = NULL;
4507046Sahrens 	int err;
4517046Sahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
4527046Sahrens 
4537046Sahrens 	err = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds);
4547046Sahrens 	if (err)
4557046Sahrens 		return (err);
4567046Sahrens 
4577046Sahrens 	while (ds->ds_phys->ds_prev_snap_obj != 0) {
4587046Sahrens 		err = dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
4597046Sahrens 		    FTAG, &prev);
4607046Sahrens 		if (err) {
4617046Sahrens 			dsl_dataset_rele(ds, FTAG);
4627046Sahrens 			return (err);
4637046Sahrens 		}
4647046Sahrens 
4657046Sahrens 		if (prev->ds_phys->ds_next_snap_obj != ds->ds_object)
4667046Sahrens 			break;
4677046Sahrens 		dsl_dataset_rele(ds, FTAG);
4687046Sahrens 		ds = prev;
4697046Sahrens 		prev = NULL;
4707046Sahrens 	}
4717046Sahrens 
4727046Sahrens 	if (prev == NULL) {
4737046Sahrens 		prev = dp->dp_origin_snap;
4747046Sahrens 
4757046Sahrens 		/*
4767046Sahrens 		 * The $ORIGIN can't have any data, or the accounting
4777046Sahrens 		 * will be wrong.
4787046Sahrens 		 */
4797046Sahrens 		ASSERT(prev->ds_phys->ds_bp.blk_birth == 0);
4807046Sahrens 
4817046Sahrens 		/* The origin doesn't get attached to itself */
4827046Sahrens 		if (ds->ds_object == prev->ds_object) {
4837046Sahrens 			dsl_dataset_rele(ds, FTAG);
4847046Sahrens 			return (0);
4857046Sahrens 		}
4867046Sahrens 
4877046Sahrens 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
4887046Sahrens 		ds->ds_phys->ds_prev_snap_obj = prev->ds_object;
4897046Sahrens 		ds->ds_phys->ds_prev_snap_txg = prev->ds_phys->ds_creation_txg;
4907046Sahrens 
4917046Sahrens 		dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
4927046Sahrens 		ds->ds_dir->dd_phys->dd_origin_obj = prev->ds_object;
4937046Sahrens 
4947046Sahrens 		dmu_buf_will_dirty(prev->ds_dbuf, tx);
4957046Sahrens 		prev->ds_phys->ds_num_children++;
4967046Sahrens 
4977046Sahrens 		if (ds->ds_phys->ds_next_snap_obj == 0) {
4987046Sahrens 			ASSERT(ds->ds_prev == NULL);
4997046Sahrens 			VERIFY(0 == dsl_dataset_hold_obj(dp,
5007046Sahrens 			    ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
5017046Sahrens 		}
5027046Sahrens 	}
5037046Sahrens 
5047046Sahrens 	ASSERT(ds->ds_dir->dd_phys->dd_origin_obj == prev->ds_object);
5057046Sahrens 	ASSERT(ds->ds_phys->ds_prev_snap_obj == prev->ds_object);
5067046Sahrens 
5077046Sahrens 	if (prev->ds_phys->ds_next_clones_obj == 0) {
5087046Sahrens 		prev->ds_phys->ds_next_clones_obj =
5097046Sahrens 		    zap_create(dp->dp_meta_objset,
5107046Sahrens 		    DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
5117046Sahrens 	}
5127046Sahrens 	VERIFY(0 == zap_add_int(dp->dp_meta_objset,
5137046Sahrens 	    prev->ds_phys->ds_next_clones_obj, ds->ds_object, tx));
5147046Sahrens 
5157046Sahrens 	dsl_dataset_rele(ds, FTAG);
5167046Sahrens 	if (prev != dp->dp_origin_snap)
5177046Sahrens 		dsl_dataset_rele(prev, FTAG);
5187046Sahrens 	return (0);
5197046Sahrens }
5207046Sahrens 
5217046Sahrens void
5227046Sahrens dsl_pool_upgrade_clones(dsl_pool_t *dp, dmu_tx_t *tx)
5237046Sahrens {
5247046Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
5257046Sahrens 	ASSERT(dp->dp_origin_snap != NULL);
5267046Sahrens 
5277046Sahrens 	(void) dmu_objset_find_spa(dp->dp_spa, NULL, upgrade_clones_cb,
5287046Sahrens 	    tx, DS_FIND_CHILDREN);
5297046Sahrens }
5307046Sahrens 
5317046Sahrens void
5327046Sahrens dsl_pool_create_origin(dsl_pool_t *dp, dmu_tx_t *tx)
5337046Sahrens {
5347046Sahrens 	uint64_t dsobj;
5357046Sahrens 	dsl_dataset_t *ds;
5367046Sahrens 
5377046Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
5387046Sahrens 	ASSERT(dp->dp_origin_snap == NULL);
5397046Sahrens 
5407046Sahrens 	/* create the origin dir, ds, & snap-ds */
5417046Sahrens 	rw_enter(&dp->dp_config_rwlock, RW_WRITER);
5427046Sahrens 	dsobj = dsl_dataset_create_sync(dp->dp_root_dir, ORIGIN_DIR_NAME,
5437046Sahrens 	    NULL, 0, kcred, tx);
5447046Sahrens 	VERIFY(0 == dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
5457046Sahrens 	dsl_dataset_snapshot_sync(ds, ORIGIN_DIR_NAME, kcred, tx);
5467046Sahrens 	VERIFY(0 == dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
5477046Sahrens 	    dp, &dp->dp_origin_snap));
5487046Sahrens 	dsl_dataset_rele(ds, FTAG);
5497046Sahrens 	rw_exit(&dp->dp_config_rwlock);
5507046Sahrens }
551