xref: /onnv-gate/usr/src/uts/common/fs/zfs/dsl_pool.c (revision 7046:361307ae060d)
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>
39*7046Sahrens #include <sys/zfs_znode.h>
40*7046Sahrens #include <sys/spa_impl.h>
41789Sahrens 
426245Smaybee int zfs_no_write_throttle = 0;
436245Smaybee uint64_t zfs_write_limit_override = 0;
446245Smaybee 
45*7046Sahrens 
461544Seschrock static int
47*7046Sahrens 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,
54*7046Sahrens 	    name, sizeof (obj), 1, &obj);
551544Seschrock 	if (err)
561544Seschrock 		return (err);
57789Sahrens 
58*7046Sahrens 	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);
85*7046Sahrens 	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);
95*7046Sahrens 	dsl_dir_t *dd;
96*7046Sahrens 	dsl_dataset_t *ds;
971544Seschrock 	objset_impl_t *osi;
98789Sahrens 
99*7046Sahrens 	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 
116*7046Sahrens 	err = dsl_pool_open_special_dir(dp, MOS_DIR_NAME, &dp->dp_mos_dir);
1171544Seschrock 	if (err)
1181544Seschrock 		goto out;
1191544Seschrock 
120*7046Sahrens 	if (spa_version(spa) >= SPA_VERSION_ORIGIN) {
121*7046Sahrens 		err = dsl_pool_open_special_dir(dp, ORIGIN_DIR_NAME, &dd);
122*7046Sahrens 		if (err)
123*7046Sahrens 			goto out;
124*7046Sahrens 		err = dsl_dataset_hold_obj(dp, dd->dd_phys->dd_head_dataset_obj,
125*7046Sahrens 		    FTAG, &ds);
126*7046Sahrens 		if (err)
127*7046Sahrens 			goto out;
128*7046Sahrens 		err = dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
129*7046Sahrens 		    dp, &dp->dp_origin_snap);
130*7046Sahrens 		if (err)
131*7046Sahrens 			goto out;
132*7046Sahrens 		dsl_dataset_rele(ds, FTAG);
133*7046Sahrens 		dsl_dir_close(dd, dp);
134*7046Sahrens 	}
135*7046Sahrens 
136*7046Sahrens 	/* get scrub status */
137*7046Sahrens 	err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
138*7046Sahrens 	    DMU_POOL_SCRUB_FUNC, sizeof (uint32_t), 1,
139*7046Sahrens 	    &dp->dp_scrub_func);
140*7046Sahrens 	if (err == 0) {
141*7046Sahrens 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
142*7046Sahrens 		    DMU_POOL_SCRUB_QUEUE, sizeof (uint64_t), 1,
143*7046Sahrens 		    &dp->dp_scrub_queue_obj);
144*7046Sahrens 		if (err)
145*7046Sahrens 			goto out;
146*7046Sahrens 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
147*7046Sahrens 		    DMU_POOL_SCRUB_MIN_TXG, sizeof (uint64_t), 1,
148*7046Sahrens 		    &dp->dp_scrub_min_txg);
149*7046Sahrens 		if (err)
150*7046Sahrens 			goto out;
151*7046Sahrens 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
152*7046Sahrens 		    DMU_POOL_SCRUB_MAX_TXG, sizeof (uint64_t), 1,
153*7046Sahrens 		    &dp->dp_scrub_max_txg);
154*7046Sahrens 		if (err)
155*7046Sahrens 			goto out;
156*7046Sahrens 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
157*7046Sahrens 		    DMU_POOL_SCRUB_BOOKMARK, sizeof (uint64_t), 4,
158*7046Sahrens 		    &dp->dp_scrub_bookmark);
159*7046Sahrens 		if (err)
160*7046Sahrens 			goto out;
161*7046Sahrens 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
162*7046Sahrens 		    DMU_POOL_SCRUB_ERRORS, sizeof (uint64_t), 1,
163*7046Sahrens 		    &spa->spa_scrub_errors);
164*7046Sahrens 		if (err)
165*7046Sahrens 			goto out;
166*7046Sahrens 		if (spa_version(spa) < SPA_VERSION_DSL_SCRUB) {
167*7046Sahrens 			/*
168*7046Sahrens 			 * A new-type scrub was in progress on an old
169*7046Sahrens 			 * pool.  Restart from the beginning, since the
170*7046Sahrens 			 * old software may have changed the pool in the
171*7046Sahrens 			 * meantime.
172*7046Sahrens 			 */
173*7046Sahrens 			dsl_pool_scrub_restart(dp);
174*7046Sahrens 		}
175*7046Sahrens 	} else {
176*7046Sahrens 		/*
177*7046Sahrens 		 * It's OK if there is no scrub in progress (and if
178*7046Sahrens 		 * there was an I/O error, ignore it).
179*7046Sahrens 		 */
180*7046Sahrens 		err = 0;
181*7046Sahrens 	}
182*7046Sahrens 
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 {
196*7046Sahrens 	/* drop our references from dsl_pool_open() */
197*7046Sahrens 
198*7046Sahrens 	/*
199*7046Sahrens 	 * Since we held the origin_snap from "syncing" context (which
200*7046Sahrens 	 * includes pool-opening context), it actually only got a "ref"
201*7046Sahrens 	 * and not a hold, so just drop that here.
202*7046Sahrens 	 */
203*7046Sahrens 	if (dp->dp_origin_snap)
204*7046Sahrens 		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);
222*7046Sahrens 	mutex_destroy(&dp->dp_scrub_cancel_lock);
223789Sahrens 	kmem_free(dp, sizeof (dsl_pool_t));
224789Sahrens }
225789Sahrens 
226789Sahrens dsl_pool_t *
227789Sahrens dsl_pool_create(spa_t *spa, 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);
232*7046Sahrens 	objset_impl_t *osip;
233*7046Sahrens 	dsl_dataset_t *ds;
234*7046Sahrens 	uint64_t dsobj;
235*7046Sahrens 
236*7046Sahrens 	/* 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 */
246*7046Sahrens 	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 */
251*7046Sahrens 	(void) dsl_dir_create_sync(dp, dp->dp_root_dir, MOS_DIR_NAME, tx);
252*7046Sahrens 	VERIFY(0 == dsl_pool_open_special_dir(dp,
253*7046Sahrens 	    MOS_DIR_NAME, &dp->dp_mos_dir));
254*7046Sahrens 
255*7046Sahrens 	if (spa_version(spa) >= SPA_VERSION_DSL_SCRUB)
256*7046Sahrens 		dsl_pool_create_origin(dp, tx);
257*7046Sahrens 
258*7046Sahrens 	/* create the root dataset */
259*7046Sahrens 	dsobj = dsl_dataset_create_sync_dd(dp->dp_root_dir, NULL, 0, tx);
260*7046Sahrens 
261*7046Sahrens 	/* create the root objset */
262*7046Sahrens 	VERIFY(0 == dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
263*7046Sahrens 	osip = dmu_objset_create_impl(dp->dp_spa, ds,
264*7046Sahrens 	    dsl_dataset_get_blkptr(ds), DMU_OST_ZFS, tx);
265*7046Sahrens #ifdef _KERNEL
266*7046Sahrens 	zfs_create_fs(&osip->os, kcred, NULL, tx);
267*7046Sahrens #endif
268*7046Sahrens 	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 
304*7046Sahrens 	if (spa_sync_pass(dp->dp_spa) == 1)
305*7046Sahrens 		dsl_pool_scrub_sync(dp, tx);
306*7046Sahrens 
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 }
443*7046Sahrens 
444*7046Sahrens /* ARGSUSED */
445*7046Sahrens static int
446*7046Sahrens upgrade_clones_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
447*7046Sahrens {
448*7046Sahrens 	dmu_tx_t *tx = arg;
449*7046Sahrens 	dsl_dataset_t *ds, *prev = NULL;
450*7046Sahrens 	int err;
451*7046Sahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
452*7046Sahrens 
453*7046Sahrens 	err = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds);
454*7046Sahrens 	if (err)
455*7046Sahrens 		return (err);
456*7046Sahrens 
457*7046Sahrens 	while (ds->ds_phys->ds_prev_snap_obj != 0) {
458*7046Sahrens 		err = dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
459*7046Sahrens 		    FTAG, &prev);
460*7046Sahrens 		if (err) {
461*7046Sahrens 			dsl_dataset_rele(ds, FTAG);
462*7046Sahrens 			return (err);
463*7046Sahrens 		}
464*7046Sahrens 
465*7046Sahrens 		if (prev->ds_phys->ds_next_snap_obj != ds->ds_object)
466*7046Sahrens 			break;
467*7046Sahrens 		dsl_dataset_rele(ds, FTAG);
468*7046Sahrens 		ds = prev;
469*7046Sahrens 		prev = NULL;
470*7046Sahrens 	}
471*7046Sahrens 
472*7046Sahrens 	if (prev == NULL) {
473*7046Sahrens 		prev = dp->dp_origin_snap;
474*7046Sahrens 
475*7046Sahrens 		/*
476*7046Sahrens 		 * The $ORIGIN can't have any data, or the accounting
477*7046Sahrens 		 * will be wrong.
478*7046Sahrens 		 */
479*7046Sahrens 		ASSERT(prev->ds_phys->ds_bp.blk_birth == 0);
480*7046Sahrens 
481*7046Sahrens 		/* The origin doesn't get attached to itself */
482*7046Sahrens 		if (ds->ds_object == prev->ds_object) {
483*7046Sahrens 			dsl_dataset_rele(ds, FTAG);
484*7046Sahrens 			return (0);
485*7046Sahrens 		}
486*7046Sahrens 
487*7046Sahrens 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
488*7046Sahrens 		ds->ds_phys->ds_prev_snap_obj = prev->ds_object;
489*7046Sahrens 		ds->ds_phys->ds_prev_snap_txg = prev->ds_phys->ds_creation_txg;
490*7046Sahrens 
491*7046Sahrens 		dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
492*7046Sahrens 		ds->ds_dir->dd_phys->dd_origin_obj = prev->ds_object;
493*7046Sahrens 
494*7046Sahrens 		dmu_buf_will_dirty(prev->ds_dbuf, tx);
495*7046Sahrens 		prev->ds_phys->ds_num_children++;
496*7046Sahrens 
497*7046Sahrens 		if (ds->ds_phys->ds_next_snap_obj == 0) {
498*7046Sahrens 			ASSERT(ds->ds_prev == NULL);
499*7046Sahrens 			VERIFY(0 == dsl_dataset_hold_obj(dp,
500*7046Sahrens 			    ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
501*7046Sahrens 		}
502*7046Sahrens 	}
503*7046Sahrens 
504*7046Sahrens 	ASSERT(ds->ds_dir->dd_phys->dd_origin_obj == prev->ds_object);
505*7046Sahrens 	ASSERT(ds->ds_phys->ds_prev_snap_obj == prev->ds_object);
506*7046Sahrens 
507*7046Sahrens 	if (prev->ds_phys->ds_next_clones_obj == 0) {
508*7046Sahrens 		prev->ds_phys->ds_next_clones_obj =
509*7046Sahrens 		    zap_create(dp->dp_meta_objset,
510*7046Sahrens 		    DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
511*7046Sahrens 	}
512*7046Sahrens 	VERIFY(0 == zap_add_int(dp->dp_meta_objset,
513*7046Sahrens 	    prev->ds_phys->ds_next_clones_obj, ds->ds_object, tx));
514*7046Sahrens 
515*7046Sahrens 	dsl_dataset_rele(ds, FTAG);
516*7046Sahrens 	if (prev != dp->dp_origin_snap)
517*7046Sahrens 		dsl_dataset_rele(prev, FTAG);
518*7046Sahrens 	return (0);
519*7046Sahrens }
520*7046Sahrens 
521*7046Sahrens void
522*7046Sahrens dsl_pool_upgrade_clones(dsl_pool_t *dp, dmu_tx_t *tx)
523*7046Sahrens {
524*7046Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
525*7046Sahrens 	ASSERT(dp->dp_origin_snap != NULL);
526*7046Sahrens 
527*7046Sahrens 	(void) dmu_objset_find_spa(dp->dp_spa, NULL, upgrade_clones_cb,
528*7046Sahrens 	    tx, DS_FIND_CHILDREN);
529*7046Sahrens }
530*7046Sahrens 
531*7046Sahrens void
532*7046Sahrens dsl_pool_create_origin(dsl_pool_t *dp, dmu_tx_t *tx)
533*7046Sahrens {
534*7046Sahrens 	uint64_t dsobj;
535*7046Sahrens 	dsl_dataset_t *ds;
536*7046Sahrens 
537*7046Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
538*7046Sahrens 	ASSERT(dp->dp_origin_snap == NULL);
539*7046Sahrens 
540*7046Sahrens 	/* create the origin dir, ds, & snap-ds */
541*7046Sahrens 	rw_enter(&dp->dp_config_rwlock, RW_WRITER);
542*7046Sahrens 	dsobj = dsl_dataset_create_sync(dp->dp_root_dir, ORIGIN_DIR_NAME,
543*7046Sahrens 	    NULL, 0, kcred, tx);
544*7046Sahrens 	VERIFY(0 == dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
545*7046Sahrens 	dsl_dataset_snapshot_sync(ds, ORIGIN_DIR_NAME, kcred, tx);
546*7046Sahrens 	VERIFY(0 == dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
547*7046Sahrens 	    dp, &dp->dp_origin_snap));
548*7046Sahrens 	dsl_dataset_rele(ds, FTAG);
549*7046Sahrens 	rw_exit(&dp->dp_config_rwlock);
550*7046Sahrens }
551