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