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