11d452cf5Sahrens /*
21d452cf5Sahrens  * CDDL HEADER START
31d452cf5Sahrens  *
41d452cf5Sahrens  * The contents of this file are subject to the terms of the
51d452cf5Sahrens  * Common Development and Distribution License (the "License").
61d452cf5Sahrens  * You may not use this file except in compliance with the License.
71d452cf5Sahrens  *
81d452cf5Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91d452cf5Sahrens  * or http://www.opensolaris.org/os/licensing.
101d452cf5Sahrens  * See the License for the specific language governing permissions
111d452cf5Sahrens  * and limitations under the License.
121d452cf5Sahrens  *
131d452cf5Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
141d452cf5Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151d452cf5Sahrens  * If applicable, add the following below this CDDL HEADER, with the
161d452cf5Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
171d452cf5Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
181d452cf5Sahrens  *
191d452cf5Sahrens  * CDDL HEADER END
201d452cf5Sahrens  */
211d452cf5Sahrens /*
223f9d6ad7SLin Ling  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
2386714001SSerapheim Dimitropoulos  * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
241d452cf5Sahrens  */
251d452cf5Sahrens 
261d452cf5Sahrens #include <sys/dmu.h>
271d452cf5Sahrens #include <sys/dmu_tx.h>
281d452cf5Sahrens #include <sys/dsl_pool.h>
291d452cf5Sahrens #include <sys/dsl_dir.h>
301d452cf5Sahrens #include <sys/dsl_synctask.h>
3118b4d8beSMatthew Ahrens #include <sys/metaslab.h>
321d452cf5Sahrens 
331d452cf5Sahrens #define	DST_AVG_BLKSHIFT 14
341d452cf5Sahrens 
351d452cf5Sahrens /* ARGSUSED */
361d452cf5Sahrens static int
dsl_null_checkfunc(void * arg,dmu_tx_t * tx)373b2aab18SMatthew Ahrens dsl_null_checkfunc(void *arg, dmu_tx_t *tx)
381d452cf5Sahrens {
391d452cf5Sahrens 	return (0);
401d452cf5Sahrens }
411d452cf5Sahrens 
4286714001SSerapheim Dimitropoulos static int
dsl_sync_task_common(const char * pool,dsl_checkfunc_t * checkfunc,dsl_syncfunc_t * syncfunc,dsl_sigfunc_t * sigfunc,void * arg,int blocks_modified,zfs_space_check_t space_check,boolean_t early)4386714001SSerapheim Dimitropoulos dsl_sync_task_common(const char *pool, dsl_checkfunc_t *checkfunc,
44*d0cb1fb9SDon Brady     dsl_syncfunc_t *syncfunc, dsl_sigfunc_t *sigfunc, void *arg,
4586714001SSerapheim Dimitropoulos     int blocks_modified, zfs_space_check_t space_check, boolean_t early)
461d452cf5Sahrens {
473b2aab18SMatthew Ahrens 	spa_t *spa;
481d452cf5Sahrens 	dmu_tx_t *tx;
493b2aab18SMatthew Ahrens 	int err;
503b2aab18SMatthew Ahrens 	dsl_sync_task_t dst = { 0 };
513b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
521d452cf5Sahrens 
533b2aab18SMatthew Ahrens 	err = spa_open(pool, &spa, FTAG);
543b2aab18SMatthew Ahrens 	if (err != 0)
553b2aab18SMatthew Ahrens 		return (err);
563b2aab18SMatthew Ahrens 	dp = spa_get_dsl(spa);
571d452cf5Sahrens 
583b2aab18SMatthew Ahrens top:
593b2aab18SMatthew Ahrens 	tx = dmu_tx_create_dd(dp->dp_mos_dir);
603b2aab18SMatthew Ahrens 	VERIFY0(dmu_tx_assign(tx, TXG_WAIT));
613b2aab18SMatthew Ahrens 
623b2aab18SMatthew Ahrens 	dst.dst_pool = dp;
633b2aab18SMatthew Ahrens 	dst.dst_txg = dmu_tx_get_txg(tx);
643b2aab18SMatthew Ahrens 	dst.dst_space = blocks_modified << DST_AVG_BLKSHIFT;
657d46dc6cSMatthew Ahrens 	dst.dst_space_check = space_check;
663b2aab18SMatthew Ahrens 	dst.dst_checkfunc = checkfunc != NULL ? checkfunc : dsl_null_checkfunc;
673b2aab18SMatthew Ahrens 	dst.dst_syncfunc = syncfunc;
683b2aab18SMatthew Ahrens 	dst.dst_arg = arg;
693b2aab18SMatthew Ahrens 	dst.dst_error = 0;
703b2aab18SMatthew Ahrens 	dst.dst_nowaiter = B_FALSE;
713b2aab18SMatthew Ahrens 
723b2aab18SMatthew Ahrens 	dsl_pool_config_enter(dp, FTAG);
733b2aab18SMatthew Ahrens 	err = dst.dst_checkfunc(arg, tx);
743b2aab18SMatthew Ahrens 	dsl_pool_config_exit(dp, FTAG);
753b2aab18SMatthew Ahrens 
763b2aab18SMatthew Ahrens 	if (err != 0) {
771d452cf5Sahrens 		dmu_tx_commit(tx);
783b2aab18SMatthew Ahrens 		spa_close(spa, FTAG);
793b2aab18SMatthew Ahrens 		return (err);
801d452cf5Sahrens 	}
811d452cf5Sahrens 
8286714001SSerapheim Dimitropoulos 	txg_list_t *task_list = (early) ?
8386714001SSerapheim Dimitropoulos 	    &dp->dp_early_sync_tasks : &dp->dp_sync_tasks;
8486714001SSerapheim Dimitropoulos 	VERIFY(txg_list_add_tail(task_list, &dst, dst.dst_txg));
851d452cf5Sahrens 
861d452cf5Sahrens 	dmu_tx_commit(tx);
871d452cf5Sahrens 
88*d0cb1fb9SDon Brady 	if (sigfunc != NULL && txg_wait_synced_sig(dp, dst.dst_txg)) {
89*d0cb1fb9SDon Brady 		/* current contract is to call func once */
90*d0cb1fb9SDon Brady 		sigfunc(arg, tx);
91*d0cb1fb9SDon Brady 		sigfunc = NULL;	/* in case of an EAGAIN retry */
92*d0cb1fb9SDon Brady 	}
933b2aab18SMatthew Ahrens 	txg_wait_synced(dp, dst.dst_txg);
941d452cf5Sahrens 
953b2aab18SMatthew Ahrens 	if (dst.dst_error == EAGAIN) {
963b2aab18SMatthew Ahrens 		txg_wait_synced(dp, dst.dst_txg + TXG_DEFER_SIZE);
971d452cf5Sahrens 		goto top;
98b24ab676SJeff Bonwick 	}
991d452cf5Sahrens 
1003b2aab18SMatthew Ahrens 	spa_close(spa, FTAG);
1013b2aab18SMatthew Ahrens 	return (dst.dst_error);
1021d452cf5Sahrens }
1031d452cf5Sahrens 
10486714001SSerapheim Dimitropoulos /*
10586714001SSerapheim Dimitropoulos  * Called from open context to perform a callback in syncing context.  Waits
10686714001SSerapheim Dimitropoulos  * for the operation to complete.
10786714001SSerapheim Dimitropoulos  *
10886714001SSerapheim Dimitropoulos  * The checkfunc will be called from open context as a preliminary check
10986714001SSerapheim Dimitropoulos  * which can quickly fail.  If it succeeds, it will be called again from
11086714001SSerapheim Dimitropoulos  * syncing context.  The checkfunc should generally be designed to work
11186714001SSerapheim Dimitropoulos  * properly in either context, but if necessary it can check
11286714001SSerapheim Dimitropoulos  * dmu_tx_is_syncing(tx).
11386714001SSerapheim Dimitropoulos  *
11486714001SSerapheim Dimitropoulos  * The synctask infrastructure enforces proper locking strategy with respect
11586714001SSerapheim Dimitropoulos  * to the dp_config_rwlock -- the lock will always be held when the callbacks
11686714001SSerapheim Dimitropoulos  * are called.  It will be held for read during the open-context (preliminary)
11786714001SSerapheim Dimitropoulos  * call to the checkfunc, and then held for write from syncing context during
11886714001SSerapheim Dimitropoulos  * the calls to the check and sync funcs.
11986714001SSerapheim Dimitropoulos  *
12086714001SSerapheim Dimitropoulos  * A dataset or pool name can be passed as the first argument.  Typically,
12186714001SSerapheim Dimitropoulos  * the check func will hold, check the return value of the hold, and then
12286714001SSerapheim Dimitropoulos  * release the dataset.  The sync func will VERIFYO(hold()) the dataset.
12386714001SSerapheim Dimitropoulos  * This is safe because no changes can be made between the check and sync funcs,
12486714001SSerapheim Dimitropoulos  * and the sync func will only be called if the check func successfully opened
12586714001SSerapheim Dimitropoulos  * the dataset.
12686714001SSerapheim Dimitropoulos  */
12786714001SSerapheim Dimitropoulos int
dsl_sync_task(const char * pool,dsl_checkfunc_t * checkfunc,dsl_syncfunc_t * syncfunc,void * arg,int blocks_modified,zfs_space_check_t space_check)12886714001SSerapheim Dimitropoulos dsl_sync_task(const char *pool, dsl_checkfunc_t *checkfunc,
12986714001SSerapheim Dimitropoulos     dsl_syncfunc_t *syncfunc, void *arg,
13086714001SSerapheim Dimitropoulos     int blocks_modified, zfs_space_check_t space_check)
13186714001SSerapheim Dimitropoulos {
132*d0cb1fb9SDon Brady 	return (dsl_sync_task_common(pool, checkfunc, syncfunc, NULL, arg,
13386714001SSerapheim Dimitropoulos 	    blocks_modified, space_check, B_FALSE));
13486714001SSerapheim Dimitropoulos }
13586714001SSerapheim Dimitropoulos 
13686714001SSerapheim Dimitropoulos /*
13786714001SSerapheim Dimitropoulos  * An early synctask works exactly as a standard synctask with one important
13886714001SSerapheim Dimitropoulos  * difference on the way it is handled during syncing context. Standard
13986714001SSerapheim Dimitropoulos  * synctasks run after we've written out all the dirty blocks of dirty
14086714001SSerapheim Dimitropoulos  * datasets. Early synctasks are executed before writing out any dirty data,
14186714001SSerapheim Dimitropoulos  * and thus before standard synctasks.
14286714001SSerapheim Dimitropoulos  *
14386714001SSerapheim Dimitropoulos  * For that reason, early synctasks can affect the process of writing dirty
14486714001SSerapheim Dimitropoulos  * changes to disk for the txg that they run and should be used with caution.
14586714001SSerapheim Dimitropoulos  * In addition, early synctasks should not dirty any metaslabs as this would
14686714001SSerapheim Dimitropoulos  * invalidate the precodition/invariant for subsequent early synctasks.
14786714001SSerapheim Dimitropoulos  * [see dsl_pool_sync() and dsl_early_sync_task_verify()]
14886714001SSerapheim Dimitropoulos  */
14986714001SSerapheim Dimitropoulos int
dsl_early_sync_task(const char * pool,dsl_checkfunc_t * checkfunc,dsl_syncfunc_t * syncfunc,void * arg,int blocks_modified,zfs_space_check_t space_check)15086714001SSerapheim Dimitropoulos dsl_early_sync_task(const char *pool, dsl_checkfunc_t *checkfunc,
15186714001SSerapheim Dimitropoulos     dsl_syncfunc_t *syncfunc, void *arg,
15286714001SSerapheim Dimitropoulos     int blocks_modified, zfs_space_check_t space_check)
15386714001SSerapheim Dimitropoulos {
154*d0cb1fb9SDon Brady 	return (dsl_sync_task_common(pool, checkfunc, syncfunc, NULL, arg,
15586714001SSerapheim Dimitropoulos 	    blocks_modified, space_check, B_TRUE));
15686714001SSerapheim Dimitropoulos }
15786714001SSerapheim Dimitropoulos 
158*d0cb1fb9SDon Brady /*
159*d0cb1fb9SDon Brady  * A standard synctask that can be interrupted from a signal. The sigfunc
160*d0cb1fb9SDon Brady  * is called once if a signal occurred while waiting for the task to sync.
161*d0cb1fb9SDon Brady  */
162*d0cb1fb9SDon Brady int
dsl_sync_task_sig(const char * pool,dsl_checkfunc_t * checkfunc,dsl_syncfunc_t * syncfunc,dsl_sigfunc_t * sigfunc,void * arg,int blocks_modified,zfs_space_check_t space_check)163*d0cb1fb9SDon Brady dsl_sync_task_sig(const char *pool, dsl_checkfunc_t *checkfunc,
164*d0cb1fb9SDon Brady     dsl_syncfunc_t *syncfunc, dsl_sigfunc_t *sigfunc, void *arg,
165*d0cb1fb9SDon Brady     int blocks_modified, zfs_space_check_t space_check)
166*d0cb1fb9SDon Brady {
167*d0cb1fb9SDon Brady 	return (dsl_sync_task_common(pool, checkfunc, syncfunc, sigfunc, arg,
168*d0cb1fb9SDon Brady 	    blocks_modified, space_check, B_FALSE));
169*d0cb1fb9SDon Brady }
170*d0cb1fb9SDon Brady 
17186714001SSerapheim Dimitropoulos static void
dsl_sync_task_nowait_common(dsl_pool_t * dp,dsl_syncfunc_t * syncfunc,void * arg,int blocks_modified,zfs_space_check_t space_check,dmu_tx_t * tx,boolean_t early)17286714001SSerapheim Dimitropoulos dsl_sync_task_nowait_common(dsl_pool_t *dp, dsl_syncfunc_t *syncfunc, void *arg,
17386714001SSerapheim Dimitropoulos     int blocks_modified, zfs_space_check_t space_check, dmu_tx_t *tx,
17486714001SSerapheim Dimitropoulos     boolean_t early)
175e7437265Sahrens {
1763b2aab18SMatthew Ahrens 	dsl_sync_task_t *dst = kmem_zalloc(sizeof (*dst), KM_SLEEP);
177e7437265Sahrens 
1783b2aab18SMatthew Ahrens 	dst->dst_pool = dp;
1793b2aab18SMatthew Ahrens 	dst->dst_txg = dmu_tx_get_txg(tx);
1803b2aab18SMatthew Ahrens 	dst->dst_space = blocks_modified << DST_AVG_BLKSHIFT;
1817d46dc6cSMatthew Ahrens 	dst->dst_space_check = space_check;
1823b2aab18SMatthew Ahrens 	dst->dst_checkfunc = dsl_null_checkfunc;
1833b2aab18SMatthew Ahrens 	dst->dst_syncfunc = syncfunc;
1843b2aab18SMatthew Ahrens 	dst->dst_arg = arg;
1853b2aab18SMatthew Ahrens 	dst->dst_error = 0;
1863b2aab18SMatthew Ahrens 	dst->dst_nowaiter = B_TRUE;
1871d452cf5Sahrens 
18886714001SSerapheim Dimitropoulos 	txg_list_t *task_list = (early) ?
18986714001SSerapheim Dimitropoulos 	    &dp->dp_early_sync_tasks : &dp->dp_sync_tasks;
19086714001SSerapheim Dimitropoulos 	VERIFY(txg_list_add_tail(task_list, dst, dst->dst_txg));
19186714001SSerapheim Dimitropoulos }
19286714001SSerapheim Dimitropoulos 
19386714001SSerapheim Dimitropoulos void
dsl_sync_task_nowait(dsl_pool_t * dp,dsl_syncfunc_t * syncfunc,void * arg,int blocks_modified,zfs_space_check_t space_check,dmu_tx_t * tx)19486714001SSerapheim Dimitropoulos dsl_sync_task_nowait(dsl_pool_t *dp, dsl_syncfunc_t *syncfunc, void *arg,
19586714001SSerapheim Dimitropoulos     int blocks_modified, zfs_space_check_t space_check, dmu_tx_t *tx)
19686714001SSerapheim Dimitropoulos {
19786714001SSerapheim Dimitropoulos 	dsl_sync_task_nowait_common(dp, syncfunc, arg,
19886714001SSerapheim Dimitropoulos 	    blocks_modified, space_check, tx, B_FALSE);
19986714001SSerapheim Dimitropoulos }
20086714001SSerapheim Dimitropoulos 
20186714001SSerapheim Dimitropoulos void
dsl_early_sync_task_nowait(dsl_pool_t * dp,dsl_syncfunc_t * syncfunc,void * arg,int blocks_modified,zfs_space_check_t space_check,dmu_tx_t * tx)20286714001SSerapheim Dimitropoulos dsl_early_sync_task_nowait(dsl_pool_t *dp, dsl_syncfunc_t *syncfunc, void *arg,
20386714001SSerapheim Dimitropoulos     int blocks_modified, zfs_space_check_t space_check, dmu_tx_t *tx)
20486714001SSerapheim Dimitropoulos {
20586714001SSerapheim Dimitropoulos 	dsl_sync_task_nowait_common(dp, syncfunc, arg,
20686714001SSerapheim Dimitropoulos 	    blocks_modified, space_check, tx, B_TRUE);
2071d452cf5Sahrens }
2081d452cf5Sahrens 
2093b2aab18SMatthew Ahrens /*
2103b2aab18SMatthew Ahrens  * Called in syncing context to execute the synctask.
2113b2aab18SMatthew Ahrens  */
2121d452cf5Sahrens void
dsl_sync_task_sync(dsl_sync_task_t * dst,dmu_tx_t * tx)2133b2aab18SMatthew Ahrens dsl_sync_task_sync(dsl_sync_task_t *dst, dmu_tx_t *tx)
2141d452cf5Sahrens {
2153b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dst->dst_pool;
2161d452cf5Sahrens 
2173b2aab18SMatthew Ahrens 	ASSERT0(dst->dst_error);
2181d452cf5Sahrens 
2191d452cf5Sahrens 	/*
2207d46dc6cSMatthew Ahrens 	 * Check for sufficient space.
2217d46dc6cSMatthew Ahrens 	 *
2227d46dc6cSMatthew Ahrens 	 * When the sync task was created, the caller specified the
2237d46dc6cSMatthew Ahrens 	 * type of space checking required.  See the comment in
2247d46dc6cSMatthew Ahrens 	 * zfs_space_check_t for details on the semantics of each
2257d46dc6cSMatthew Ahrens 	 * type of space checking.
2267d46dc6cSMatthew Ahrens 	 *
2277d46dc6cSMatthew Ahrens 	 * We just check against what's on-disk; we don't want any
2287d46dc6cSMatthew Ahrens 	 * in-flight accounting to get in our way, because open context
2297d46dc6cSMatthew Ahrens 	 * may have already used up various in-core limits
2307d46dc6cSMatthew Ahrens 	 * (arc_tempreserve, dsl_pool_tempreserve).
2311d452cf5Sahrens 	 */
2327d46dc6cSMatthew Ahrens 	if (dst->dst_space_check != ZFS_SPACE_CHECK_NONE) {
23386714001SSerapheim Dimitropoulos 		uint64_t quota = dsl_pool_unreserved_space(dp,
23486714001SSerapheim Dimitropoulos 		    dst->dst_space_check);
235c1379625SJustin T. Gibbs 		uint64_t used = dsl_dir_phys(dp->dp_root_dir)->dd_used_bytes;
23686714001SSerapheim Dimitropoulos 
2377d46dc6cSMatthew Ahrens 		/* MOS space is triple-dittoed, so we multiply by 3. */
23886714001SSerapheim Dimitropoulos 		if (used + dst->dst_space * 3 > quota) {
2397d46dc6cSMatthew Ahrens 			dst->dst_error = SET_ERROR(ENOSPC);
2407d46dc6cSMatthew Ahrens 			if (dst->dst_nowaiter)
2417d46dc6cSMatthew Ahrens 				kmem_free(dst, sizeof (*dst));
2427d46dc6cSMatthew Ahrens 			return;
2437d46dc6cSMatthew Ahrens 		}
24418b4d8beSMatthew Ahrens 	}
2451d452cf5Sahrens 
2461d452cf5Sahrens 	/*
2473b2aab18SMatthew Ahrens 	 * Check for errors by calling checkfunc.
2481d452cf5Sahrens 	 */
2493b2aab18SMatthew Ahrens 	rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
2503b2aab18SMatthew Ahrens 	dst->dst_error = dst->dst_checkfunc(dst->dst_arg, tx);
2513b2aab18SMatthew Ahrens 	if (dst->dst_error == 0)
2523b2aab18SMatthew Ahrens 		dst->dst_syncfunc(dst->dst_arg, tx);
2533b2aab18SMatthew Ahrens 	rrw_exit(&dp->dp_config_rwlock, FTAG);
2543b2aab18SMatthew Ahrens 	if (dst->dst_nowaiter)
2553b2aab18SMatthew Ahrens 		kmem_free(dst, sizeof (*dst));
256e7437265Sahrens }
257