161145dc2SMartin Matuska // SPDX-License-Identifier: CDDL-1.0
2eda14cbcSMatt Macy /*
3eda14cbcSMatt Macy * CDDL HEADER START
4eda14cbcSMatt Macy *
5eda14cbcSMatt Macy * The contents of this file are subject to the terms of the
6eda14cbcSMatt Macy * Common Development and Distribution License (the "License").
7eda14cbcSMatt Macy * You may not use this file except in compliance with the License.
8eda14cbcSMatt Macy *
9eda14cbcSMatt Macy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10271171e0SMartin Matuska * or https://opensource.org/licenses/CDDL-1.0.
11eda14cbcSMatt Macy * See the License for the specific language governing permissions
12eda14cbcSMatt Macy * and limitations under the License.
13eda14cbcSMatt Macy *
14eda14cbcSMatt Macy * When distributing Covered Code, include this CDDL HEADER in each
15eda14cbcSMatt Macy * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16eda14cbcSMatt Macy * If applicable, add the following below this CDDL HEADER, with the
17eda14cbcSMatt Macy * fields enclosed by brackets "[]" replaced with your own identifying
18eda14cbcSMatt Macy * information: Portions Copyright [yyyy] [name of copyright owner]
19eda14cbcSMatt Macy *
20eda14cbcSMatt Macy * CDDL HEADER END
21eda14cbcSMatt Macy */
22eda14cbcSMatt Macy /*
23eda14cbcSMatt Macy * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24eda14cbcSMatt Macy * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
25eda14cbcSMatt Macy */
26eda14cbcSMatt Macy
27eda14cbcSMatt Macy #include <sys/dmu.h>
28eda14cbcSMatt Macy #include <sys/dmu_tx.h>
29eda14cbcSMatt Macy #include <sys/dsl_pool.h>
30eda14cbcSMatt Macy #include <sys/dsl_dir.h>
31eda14cbcSMatt Macy #include <sys/dsl_synctask.h>
32eda14cbcSMatt Macy #include <sys/metaslab.h>
33eda14cbcSMatt Macy
34eda14cbcSMatt Macy #define DST_AVG_BLKSHIFT 14
35eda14cbcSMatt Macy
36eda14cbcSMatt Macy static int
dsl_null_checkfunc(void * arg,dmu_tx_t * tx)37eda14cbcSMatt Macy dsl_null_checkfunc(void *arg, dmu_tx_t *tx)
38eda14cbcSMatt Macy {
39e92ffd9bSMartin Matuska (void) arg, (void) tx;
40eda14cbcSMatt Macy return (0);
41eda14cbcSMatt Macy }
42eda14cbcSMatt Macy
43eda14cbcSMatt Macy 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)44eda14cbcSMatt Macy dsl_sync_task_common(const char *pool, dsl_checkfunc_t *checkfunc,
45eda14cbcSMatt Macy dsl_syncfunc_t *syncfunc, dsl_sigfunc_t *sigfunc, void *arg,
46eda14cbcSMatt Macy int blocks_modified, zfs_space_check_t space_check, boolean_t early)
47eda14cbcSMatt Macy {
48eda14cbcSMatt Macy spa_t *spa;
49eda14cbcSMatt Macy dmu_tx_t *tx;
50eda14cbcSMatt Macy int err;
51eda14cbcSMatt Macy dsl_sync_task_t dst = { { { NULL } } };
52eda14cbcSMatt Macy dsl_pool_t *dp;
53eda14cbcSMatt Macy
54eda14cbcSMatt Macy err = spa_open(pool, &spa, FTAG);
55eda14cbcSMatt Macy if (err != 0)
56eda14cbcSMatt Macy return (err);
57eda14cbcSMatt Macy dp = spa_get_dsl(spa);
58eda14cbcSMatt Macy
59eda14cbcSMatt Macy top:
60eda14cbcSMatt Macy tx = dmu_tx_create_dd(dp->dp_mos_dir);
61*b1c1ee44SMartin Matuska VERIFY0(dmu_tx_assign(tx, DMU_TX_WAIT | DMU_TX_SUSPEND));
62eda14cbcSMatt Macy
63eda14cbcSMatt Macy dst.dst_pool = dp;
64eda14cbcSMatt Macy dst.dst_txg = dmu_tx_get_txg(tx);
65eda14cbcSMatt Macy dst.dst_space = blocks_modified << DST_AVG_BLKSHIFT;
66eda14cbcSMatt Macy dst.dst_space_check = space_check;
67eda14cbcSMatt Macy dst.dst_checkfunc = checkfunc != NULL ? checkfunc : dsl_null_checkfunc;
68eda14cbcSMatt Macy dst.dst_syncfunc = syncfunc;
69eda14cbcSMatt Macy dst.dst_arg = arg;
70eda14cbcSMatt Macy dst.dst_error = 0;
71eda14cbcSMatt Macy dst.dst_nowaiter = B_FALSE;
72eda14cbcSMatt Macy
73eda14cbcSMatt Macy dsl_pool_config_enter(dp, FTAG);
74eda14cbcSMatt Macy err = dst.dst_checkfunc(arg, tx);
75eda14cbcSMatt Macy dsl_pool_config_exit(dp, FTAG);
76eda14cbcSMatt Macy
77eda14cbcSMatt Macy if (err != 0) {
78eda14cbcSMatt Macy dmu_tx_commit(tx);
79eda14cbcSMatt Macy spa_close(spa, FTAG);
80eda14cbcSMatt Macy return (err);
81eda14cbcSMatt Macy }
82eda14cbcSMatt Macy
83eda14cbcSMatt Macy txg_list_t *task_list = (early) ?
84eda14cbcSMatt Macy &dp->dp_early_sync_tasks : &dp->dp_sync_tasks;
85eda14cbcSMatt Macy VERIFY(txg_list_add_tail(task_list, &dst, dst.dst_txg));
86eda14cbcSMatt Macy
87eda14cbcSMatt Macy dmu_tx_commit(tx);
88eda14cbcSMatt Macy
89071ab5a1SMartin Matuska if (sigfunc != NULL) {
90071ab5a1SMartin Matuska err = txg_wait_synced_flags(dp, dst.dst_txg, TXG_WAIT_SIGNAL);
91071ab5a1SMartin Matuska if (err != 0) {
92071ab5a1SMartin Matuska VERIFY3U(err, ==, EINTR);
93eda14cbcSMatt Macy /* current contract is to call func once */
94eda14cbcSMatt Macy sigfunc(arg, tx);
95071ab5a1SMartin Matuska /* in case we're performing an EAGAIN retry */
96071ab5a1SMartin Matuska sigfunc = NULL;
97071ab5a1SMartin Matuska
98071ab5a1SMartin Matuska txg_wait_synced(dp, dst.dst_txg);
99eda14cbcSMatt Macy }
100071ab5a1SMartin Matuska } else
101eda14cbcSMatt Macy txg_wait_synced(dp, dst.dst_txg);
102eda14cbcSMatt Macy
103eda14cbcSMatt Macy if (dst.dst_error == EAGAIN) {
104eda14cbcSMatt Macy txg_wait_synced(dp, dst.dst_txg + TXG_DEFER_SIZE);
105eda14cbcSMatt Macy goto top;
106eda14cbcSMatt Macy }
107eda14cbcSMatt Macy
108eda14cbcSMatt Macy spa_close(spa, FTAG);
109eda14cbcSMatt Macy return (dst.dst_error);
110eda14cbcSMatt Macy }
111eda14cbcSMatt Macy
112eda14cbcSMatt Macy /*
113eda14cbcSMatt Macy * Called from open context to perform a callback in syncing context. Waits
114eda14cbcSMatt Macy * for the operation to complete.
115eda14cbcSMatt Macy *
116eda14cbcSMatt Macy * The checkfunc will be called from open context as a preliminary check
117eda14cbcSMatt Macy * which can quickly fail. If it succeeds, it will be called again from
118eda14cbcSMatt Macy * syncing context. The checkfunc should generally be designed to work
119eda14cbcSMatt Macy * properly in either context, but if necessary it can check
120eda14cbcSMatt Macy * dmu_tx_is_syncing(tx).
121eda14cbcSMatt Macy *
122eda14cbcSMatt Macy * The synctask infrastructure enforces proper locking strategy with respect
123eda14cbcSMatt Macy * to the dp_config_rwlock -- the lock will always be held when the callbacks
124eda14cbcSMatt Macy * are called. It will be held for read during the open-context (preliminary)
125eda14cbcSMatt Macy * call to the checkfunc, and then held for write from syncing context during
126eda14cbcSMatt Macy * the calls to the check and sync funcs.
127eda14cbcSMatt Macy *
128eda14cbcSMatt Macy * A dataset or pool name can be passed as the first argument. Typically,
129eda14cbcSMatt Macy * the check func will hold, check the return value of the hold, and then
130eda14cbcSMatt Macy * release the dataset. The sync func will VERIFYO(hold()) the dataset.
131eda14cbcSMatt Macy * This is safe because no changes can be made between the check and sync funcs,
132eda14cbcSMatt Macy * and the sync func will only be called if the check func successfully opened
133eda14cbcSMatt Macy * the dataset.
134eda14cbcSMatt Macy */
135eda14cbcSMatt Macy 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)136eda14cbcSMatt Macy dsl_sync_task(const char *pool, dsl_checkfunc_t *checkfunc,
137eda14cbcSMatt Macy dsl_syncfunc_t *syncfunc, void *arg,
138eda14cbcSMatt Macy int blocks_modified, zfs_space_check_t space_check)
139eda14cbcSMatt Macy {
140eda14cbcSMatt Macy return (dsl_sync_task_common(pool, checkfunc, syncfunc, NULL, arg,
141eda14cbcSMatt Macy blocks_modified, space_check, B_FALSE));
142eda14cbcSMatt Macy }
143eda14cbcSMatt Macy
144eda14cbcSMatt Macy /*
145eda14cbcSMatt Macy * An early synctask works exactly as a standard synctask with one important
146eda14cbcSMatt Macy * difference on the way it is handled during syncing context. Standard
147eda14cbcSMatt Macy * synctasks run after we've written out all the dirty blocks of dirty
148eda14cbcSMatt Macy * datasets. Early synctasks are executed before writing out any dirty data,
149eda14cbcSMatt Macy * and thus before standard synctasks.
150eda14cbcSMatt Macy *
151eda14cbcSMatt Macy * For that reason, early synctasks can affect the process of writing dirty
152eda14cbcSMatt Macy * changes to disk for the txg that they run and should be used with caution.
153eda14cbcSMatt Macy * In addition, early synctasks should not dirty any metaslabs as this would
154eda14cbcSMatt Macy * invalidate the precondition/invariant for subsequent early synctasks.
155eda14cbcSMatt Macy * [see dsl_pool_sync() and dsl_early_sync_task_verify()]
156eda14cbcSMatt Macy */
157eda14cbcSMatt Macy 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)158eda14cbcSMatt Macy dsl_early_sync_task(const char *pool, dsl_checkfunc_t *checkfunc,
159eda14cbcSMatt Macy dsl_syncfunc_t *syncfunc, void *arg,
160eda14cbcSMatt Macy int blocks_modified, zfs_space_check_t space_check)
161eda14cbcSMatt Macy {
162eda14cbcSMatt Macy return (dsl_sync_task_common(pool, checkfunc, syncfunc, NULL, arg,
163eda14cbcSMatt Macy blocks_modified, space_check, B_TRUE));
164eda14cbcSMatt Macy }
165eda14cbcSMatt Macy
166eda14cbcSMatt Macy /*
167eda14cbcSMatt Macy * A standard synctask that can be interrupted from a signal. The sigfunc
168eda14cbcSMatt Macy * is called once if a signal occurred while waiting for the task to sync.
169eda14cbcSMatt Macy */
170eda14cbcSMatt Macy 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)171eda14cbcSMatt Macy dsl_sync_task_sig(const char *pool, dsl_checkfunc_t *checkfunc,
172eda14cbcSMatt Macy dsl_syncfunc_t *syncfunc, dsl_sigfunc_t *sigfunc, void *arg,
173eda14cbcSMatt Macy int blocks_modified, zfs_space_check_t space_check)
174eda14cbcSMatt Macy {
175eda14cbcSMatt Macy return (dsl_sync_task_common(pool, checkfunc, syncfunc, sigfunc, arg,
176eda14cbcSMatt Macy blocks_modified, space_check, B_FALSE));
177eda14cbcSMatt Macy }
178eda14cbcSMatt Macy
179eda14cbcSMatt Macy static void
dsl_sync_task_nowait_common(dsl_pool_t * dp,dsl_syncfunc_t * syncfunc,void * arg,dmu_tx_t * tx,boolean_t early)180eda14cbcSMatt Macy dsl_sync_task_nowait_common(dsl_pool_t *dp, dsl_syncfunc_t *syncfunc, void *arg,
1812c48331dSMatt Macy dmu_tx_t *tx, boolean_t early)
182eda14cbcSMatt Macy {
183eda14cbcSMatt Macy dsl_sync_task_t *dst = kmem_zalloc(sizeof (*dst), KM_SLEEP);
184eda14cbcSMatt Macy
185eda14cbcSMatt Macy dst->dst_pool = dp;
186eda14cbcSMatt Macy dst->dst_txg = dmu_tx_get_txg(tx);
1872c48331dSMatt Macy dst->dst_space_check = ZFS_SPACE_CHECK_NONE;
188eda14cbcSMatt Macy dst->dst_checkfunc = dsl_null_checkfunc;
189eda14cbcSMatt Macy dst->dst_syncfunc = syncfunc;
190eda14cbcSMatt Macy dst->dst_arg = arg;
191eda14cbcSMatt Macy dst->dst_error = 0;
192eda14cbcSMatt Macy dst->dst_nowaiter = B_TRUE;
193eda14cbcSMatt Macy
194eda14cbcSMatt Macy txg_list_t *task_list = (early) ?
195eda14cbcSMatt Macy &dp->dp_early_sync_tasks : &dp->dp_sync_tasks;
196eda14cbcSMatt Macy VERIFY(txg_list_add_tail(task_list, dst, dst->dst_txg));
197eda14cbcSMatt Macy }
198eda14cbcSMatt Macy
199eda14cbcSMatt Macy void
dsl_sync_task_nowait(dsl_pool_t * dp,dsl_syncfunc_t * syncfunc,void * arg,dmu_tx_t * tx)200eda14cbcSMatt Macy dsl_sync_task_nowait(dsl_pool_t *dp, dsl_syncfunc_t *syncfunc, void *arg,
2012c48331dSMatt Macy dmu_tx_t *tx)
202eda14cbcSMatt Macy {
2032c48331dSMatt Macy dsl_sync_task_nowait_common(dp, syncfunc, arg, tx, B_FALSE);
204eda14cbcSMatt Macy }
205eda14cbcSMatt Macy
206eda14cbcSMatt Macy void
dsl_early_sync_task_nowait(dsl_pool_t * dp,dsl_syncfunc_t * syncfunc,void * arg,dmu_tx_t * tx)207eda14cbcSMatt Macy dsl_early_sync_task_nowait(dsl_pool_t *dp, dsl_syncfunc_t *syncfunc, void *arg,
2082c48331dSMatt Macy dmu_tx_t *tx)
209eda14cbcSMatt Macy {
2102c48331dSMatt Macy dsl_sync_task_nowait_common(dp, syncfunc, arg, tx, B_TRUE);
211eda14cbcSMatt Macy }
212eda14cbcSMatt Macy
213eda14cbcSMatt Macy /*
214eda14cbcSMatt Macy * Called in syncing context to execute the synctask.
215eda14cbcSMatt Macy */
216eda14cbcSMatt Macy void
dsl_sync_task_sync(dsl_sync_task_t * dst,dmu_tx_t * tx)217eda14cbcSMatt Macy dsl_sync_task_sync(dsl_sync_task_t *dst, dmu_tx_t *tx)
218eda14cbcSMatt Macy {
219eda14cbcSMatt Macy dsl_pool_t *dp = dst->dst_pool;
220eda14cbcSMatt Macy
221eda14cbcSMatt Macy ASSERT0(dst->dst_error);
222eda14cbcSMatt Macy
223eda14cbcSMatt Macy /*
224eda14cbcSMatt Macy * Check for sufficient space.
225eda14cbcSMatt Macy *
226eda14cbcSMatt Macy * When the sync task was created, the caller specified the
227eda14cbcSMatt Macy * type of space checking required. See the comment in
228eda14cbcSMatt Macy * zfs_space_check_t for details on the semantics of each
229eda14cbcSMatt Macy * type of space checking.
230eda14cbcSMatt Macy *
231eda14cbcSMatt Macy * We just check against what's on-disk; we don't want any
232eda14cbcSMatt Macy * in-flight accounting to get in our way, because open context
233eda14cbcSMatt Macy * may have already used up various in-core limits
234eda14cbcSMatt Macy * (arc_tempreserve, dsl_pool_tempreserve).
235eda14cbcSMatt Macy */
236eda14cbcSMatt Macy if (dst->dst_space_check != ZFS_SPACE_CHECK_NONE) {
237eda14cbcSMatt Macy uint64_t quota = dsl_pool_unreserved_space(dp,
238eda14cbcSMatt Macy dst->dst_space_check);
239eda14cbcSMatt Macy uint64_t used = dsl_dir_phys(dp->dp_root_dir)->dd_used_bytes;
240eda14cbcSMatt Macy
241eda14cbcSMatt Macy /* MOS space is triple-dittoed, so we multiply by 3. */
242eda14cbcSMatt Macy if (used + dst->dst_space * 3 > quota) {
243eda14cbcSMatt Macy dst->dst_error = SET_ERROR(ENOSPC);
244eda14cbcSMatt Macy if (dst->dst_nowaiter)
245eda14cbcSMatt Macy kmem_free(dst, sizeof (*dst));
246eda14cbcSMatt Macy return;
247eda14cbcSMatt Macy }
248eda14cbcSMatt Macy }
249eda14cbcSMatt Macy
250eda14cbcSMatt Macy /*
251eda14cbcSMatt Macy * Check for errors by calling checkfunc.
252eda14cbcSMatt Macy */
253eda14cbcSMatt Macy rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
254eda14cbcSMatt Macy dst->dst_error = dst->dst_checkfunc(dst->dst_arg, tx);
255eda14cbcSMatt Macy if (dst->dst_error == 0)
256eda14cbcSMatt Macy dst->dst_syncfunc(dst->dst_arg, tx);
257eda14cbcSMatt Macy rrw_exit(&dp->dp_config_rwlock, FTAG);
258eda14cbcSMatt Macy if (dst->dst_nowaiter)
259eda14cbcSMatt Macy kmem_free(dst, sizeof (*dst));
260eda14cbcSMatt Macy }
261eda14cbcSMatt Macy
262eda14cbcSMatt Macy #if defined(_KERNEL)
263eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_sync_task);
264eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_sync_task_nowait);
265eda14cbcSMatt Macy #endif
266