]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/zcp_synctask.c
async zvol minor node creation interferes with receive
[mirror_zfs.git] / module / zfs / zcp_synctask.c
CommitLineData
d99a0153
CW
1/*
2 * CDDL HEADER START
3 *
4 * This file and its contents are supplied under the terms of the
5 * Common Development and Distribution License ("CDDL"), version 1.0.
6 * You may only use this file in accordance with the terms of version
7 * 1.0 of the CDDL.
8 *
9 * A full copy of the text of the CDDL should have accompanied this
10 * source. A copy of the CDDL is also available via the Internet at
11 * http://www.illumos.org/license/CDDL.
12 *
13 * CDDL HEADER END
14 */
15
16/*
af073689 17 * Copyright (c) 2016, 2017 by Delphix. All rights reserved.
e2ef1cbf 18 * Copyright 2020 Joyent, Inc.
d99a0153
CW
19 */
20
21#include <sys/lua/lua.h>
22#include <sys/lua/lauxlib.h>
23
24#include <sys/zcp.h>
25#include <sys/dsl_dir.h>
26#include <sys/dsl_pool.h>
27#include <sys/dsl_prop.h>
28#include <sys/dsl_synctask.h>
29#include <sys/dsl_dataset.h>
30#include <sys/dsl_bookmark.h>
31#include <sys/dsl_destroy.h>
32#include <sys/dmu_objset.h>
33#include <sys/zfs_znode.h>
34#include <sys/zfeature.h>
35#include <sys/metaslab.h>
36
37#define DST_AVG_BLKSHIFT 14
38
e2ef1cbf
JK
39typedef struct zcp_inherit_prop_arg {
40 lua_State *zipa_state;
41 const char *zipa_prop;
42 dsl_props_set_arg_t zipa_dpsa;
43} zcp_inherit_prop_arg_t;
44
d99a0153
CW
45typedef int (zcp_synctask_func_t)(lua_State *, boolean_t, nvlist_t *);
46typedef struct zcp_synctask_info {
47 const char *name;
48 zcp_synctask_func_t *func;
d99a0153
CW
49 const zcp_arg_t pargs[4];
50 const zcp_arg_t kwargs[2];
234c91c5
CW
51 zfs_space_check_t space_check;
52 int blocks_modified;
d99a0153
CW
53} zcp_synctask_info_t;
54
55/*
56 * Generic synctask interface for channel program syncfuncs.
57 *
58 * To perform some action in syncing context, we'd generally call
59 * dsl_sync_task(), but since the Lua script is already running inside a
60 * synctask we need to leave out some actions (such as acquiring the config
61 * rwlock and performing space checks).
62 *
63 * If 'sync' is false, executes a dry run and returns the error code.
64 *
5b72a38d
SD
65 * If we are not running in syncing context and we are not doing a dry run
66 * (meaning we are running a zfs.sync function in open-context) then we
67 * return a Lua error.
68 *
d99a0153
CW
69 * This function also handles common fatal error cases for channel program
70 * library functions. If a fatal error occurs, err_dsname will be the dataset
71 * name reported in error messages, if supplied.
72 */
73static int
74zcp_sync_task(lua_State *state, dsl_checkfunc_t *checkfunc,
75 dsl_syncfunc_t *syncfunc, void *arg, boolean_t sync, const char *err_dsname)
76{
77 int err;
78 zcp_run_info_t *ri = zcp_run_info(state);
79
80 err = checkfunc(arg, ri->zri_tx);
81 if (!sync)
82 return (err);
83
5b72a38d
SD
84 if (!ri->zri_sync) {
85 return (luaL_error(state, "running functions from the zfs.sync "
86 "submodule requires passing sync=TRUE to "
87 "lzc_channel_program() (i.e. do not specify the \"-n\" "
88 "command line argument)"));
89 }
90
d99a0153
CW
91 if (err == 0) {
92 syncfunc(arg, ri->zri_tx);
93 } else if (err == EIO) {
94 if (err_dsname != NULL) {
95 return (luaL_error(state,
96 "I/O error while accessing dataset '%s'",
97 err_dsname));
98 } else {
99 return (luaL_error(state,
100 "I/O error while accessing dataset."));
101 }
102 }
103
104 return (err);
105}
106
107
108static int zcp_synctask_destroy(lua_State *, boolean_t, nvlist_t *);
109static zcp_synctask_info_t zcp_synctask_destroy_info = {
110 .name = "destroy",
111 .func = zcp_synctask_destroy,
d99a0153
CW
112 .pargs = {
113 {.za_name = "filesystem | snapshot", .za_lua_type = LUA_TSTRING},
114 {NULL, 0}
115 },
116 .kwargs = {
117 {.za_name = "defer", .za_lua_type = LUA_TBOOLEAN},
118 {NULL, 0}
234c91c5 119 },
d2734cce 120 .space_check = ZFS_SPACE_CHECK_DESTROY,
234c91c5 121 .blocks_modified = 0
d99a0153
CW
122};
123
124/* ARGSUSED */
125static int
126zcp_synctask_destroy(lua_State *state, boolean_t sync, nvlist_t *err_details)
127{
128 int err;
129 const char *dsname = lua_tostring(state, 1);
130
131 boolean_t issnap = (strchr(dsname, '@') != NULL);
132
133 if (!issnap && !lua_isnil(state, 2)) {
134 return (luaL_error(state,
135 "'deferred' kwarg only supported for snapshots: %s",
136 dsname));
137 }
138
139 if (issnap) {
140 dsl_destroy_snapshot_arg_t ddsa = { 0 };
141 ddsa.ddsa_name = dsname;
142 if (!lua_isnil(state, 2)) {
143 ddsa.ddsa_defer = lua_toboolean(state, 2);
144 } else {
145 ddsa.ddsa_defer = B_FALSE;
146 }
147
148 err = zcp_sync_task(state, dsl_destroy_snapshot_check,
149 dsl_destroy_snapshot_sync, &ddsa, sync, dsname);
150 } else {
151 dsl_destroy_head_arg_t ddha = { 0 };
152 ddha.ddha_name = dsname;
153
154 err = zcp_sync_task(state, dsl_destroy_head_check,
155 dsl_destroy_head_sync, &ddha, sync, dsname);
156 }
157
158 return (err);
159}
160
234c91c5 161static int zcp_synctask_promote(lua_State *, boolean_t, nvlist_t *);
d99a0153
CW
162static zcp_synctask_info_t zcp_synctask_promote_info = {
163 .name = "promote",
164 .func = zcp_synctask_promote,
d99a0153
CW
165 .pargs = {
166 {.za_name = "clone", .za_lua_type = LUA_TSTRING},
167 {NULL, 0}
168 },
169 .kwargs = {
170 {NULL, 0}
234c91c5
CW
171 },
172 .space_check = ZFS_SPACE_CHECK_RESERVED,
173 .blocks_modified = 3
d99a0153
CW
174};
175
176static int
177zcp_synctask_promote(lua_State *state, boolean_t sync, nvlist_t *err_details)
178{
179 int err;
180 dsl_dataset_promote_arg_t ddpa = { 0 };
181 const char *dsname = lua_tostring(state, 1);
182 zcp_run_info_t *ri = zcp_run_info(state);
183
184 ddpa.ddpa_clonename = dsname;
185 ddpa.err_ds = err_details;
186 ddpa.cr = ri->zri_cred;
187
188 /*
189 * If there was a snapshot name conflict, then err_ds will be filled
190 * with a list of conflicting snapshot names.
191 */
192 err = zcp_sync_task(state, dsl_dataset_promote_check,
193 dsl_dataset_promote_sync, &ddpa, sync, dsname);
194
195 return (err);
196}
197
af073689
BL
198static int zcp_synctask_rollback(lua_State *, boolean_t, nvlist_t *err_details);
199static zcp_synctask_info_t zcp_synctask_rollback_info = {
200 .name = "rollback",
201 .func = zcp_synctask_rollback,
202 .space_check = ZFS_SPACE_CHECK_RESERVED,
203 .blocks_modified = 1,
204 .pargs = {
205 {.za_name = "filesystem", .za_lua_type = LUA_TSTRING},
206 {0, 0}
207 },
208 .kwargs = {
209 {0, 0}
210 }
211};
212
213static int
214zcp_synctask_rollback(lua_State *state, boolean_t sync, nvlist_t *err_details)
215{
216 int err;
217 const char *dsname = lua_tostring(state, 1);
218 dsl_dataset_rollback_arg_t ddra = { 0 };
219
220 ddra.ddra_fsname = dsname;
221 ddra.ddra_result = err_details;
222
223 err = zcp_sync_task(state, dsl_dataset_rollback_check,
224 dsl_dataset_rollback_sync, &ddra, sync, dsname);
225
226 return (err);
227}
228
234c91c5
CW
229static int zcp_synctask_snapshot(lua_State *, boolean_t, nvlist_t *);
230static zcp_synctask_info_t zcp_synctask_snapshot_info = {
231 .name = "snapshot",
232 .func = zcp_synctask_snapshot,
233 .pargs = {
234 {.za_name = "filesystem@snapname | volume@snapname",
235 .za_lua_type = LUA_TSTRING},
236 {NULL, 0}
237 },
238 .kwargs = {
239 {NULL, 0}
240 },
241 .space_check = ZFS_SPACE_CHECK_NORMAL,
242 .blocks_modified = 3
243};
244
245/* ARGSUSED */
246static int
247zcp_synctask_snapshot(lua_State *state, boolean_t sync, nvlist_t *err_details)
248{
249 int err;
250 dsl_dataset_snapshot_arg_t ddsa = { 0 };
251 const char *dsname = lua_tostring(state, 1);
252 zcp_run_info_t *ri = zcp_run_info(state);
253
5b72a38d
SD
254 /*
255 * On old pools, the ZIL must not be active when a snapshot is created,
256 * but we can't suspend the ZIL because we're already in syncing
257 * context.
258 */
259 if (spa_version(ri->zri_pool->dp_spa) < SPA_VERSION_FAST_SNAP) {
260 return (ENOTSUP);
261 }
262
234c91c5
CW
263 /*
264 * We only allow for a single snapshot rather than a list, so the
265 * error list output is unnecessary.
266 */
267 ddsa.ddsa_errors = NULL;
268 ddsa.ddsa_props = NULL;
269 ddsa.ddsa_cr = ri->zri_cred;
270 ddsa.ddsa_snaps = fnvlist_alloc();
271 fnvlist_add_boolean(ddsa.ddsa_snaps, dsname);
272
5b72a38d
SD
273 zcp_cleanup_handler_t *zch = zcp_register_cleanup(state,
274 (zcp_cleanup_t *)&fnvlist_free, ddsa.ddsa_snaps);
234c91c5
CW
275
276 err = zcp_sync_task(state, dsl_dataset_snapshot_check,
277 dsl_dataset_snapshot_sync, &ddsa, sync, dsname);
278
ec213971
MA
279 if (err == 0) {
280 /*
281 * We may need to create a new device minor node for this
282 * dataset (if it is a zvol and the "snapdev" property is set).
283 * Save it in the nvlist so that it can be processed in open
284 * context.
285 */
286 fnvlist_add_boolean(ri->zri_new_zvols, dsname);
287 }
288
5b72a38d 289 zcp_deregister_cleanup(state, zch);
234c91c5
CW
290 fnvlist_free(ddsa.ddsa_snaps);
291
292 return (err);
293}
294
e2ef1cbf
JK
295static int zcp_synctask_inherit_prop(lua_State *, boolean_t,
296 nvlist_t *err_details);
297static zcp_synctask_info_t zcp_synctask_inherit_prop_info = {
298 .name = "inherit",
299 .func = zcp_synctask_inherit_prop,
300 .space_check = ZFS_SPACE_CHECK_RESERVED,
301 .blocks_modified = 2, /* 2 * numprops */
302 .pargs = {
303 { .za_name = "dataset", .za_lua_type = LUA_TSTRING },
304 { .za_name = "property", .za_lua_type = LUA_TSTRING },
305 { NULL, 0 }
306 },
307 .kwargs = {
308 { NULL, 0 }
309 },
310};
311
312static int
313zcp_synctask_inherit_prop_check(void *arg, dmu_tx_t *tx)
314{
315 zcp_inherit_prop_arg_t *args = arg;
316 zfs_prop_t prop = zfs_name_to_prop(args->zipa_prop);
317
318 if (prop == ZPROP_INVAL) {
319 if (zfs_prop_user(args->zipa_prop))
320 return (0);
321
322 return (EINVAL);
323 }
324
325 if (zfs_prop_readonly(prop))
326 return (EINVAL);
327
328 if (!zfs_prop_inheritable(prop))
329 return (EINVAL);
330
331 return (dsl_props_set_check(&args->zipa_dpsa, tx));
332}
333
334static void
335zcp_synctask_inherit_prop_sync(void *arg, dmu_tx_t *tx)
336{
337 zcp_inherit_prop_arg_t *args = arg;
338 dsl_props_set_arg_t *dpsa = &args->zipa_dpsa;
339
340 dsl_props_set_sync(dpsa, tx);
341}
342
343static int
344zcp_synctask_inherit_prop(lua_State *state, boolean_t sync,
345 nvlist_t *err_details)
346{
347 int err;
348 zcp_inherit_prop_arg_t zipa = { 0 };
349 dsl_props_set_arg_t *dpsa = &zipa.zipa_dpsa;
350
351 const char *dsname = lua_tostring(state, 1);
352 const char *prop = lua_tostring(state, 2);
353
354 zipa.zipa_state = state;
355 zipa.zipa_prop = prop;
356 dpsa->dpsa_dsname = dsname;
357 dpsa->dpsa_source = ZPROP_SRC_INHERITED;
358 dpsa->dpsa_props = fnvlist_alloc();
359 fnvlist_add_boolean(dpsa->dpsa_props, prop);
360
361 zcp_cleanup_handler_t *zch = zcp_register_cleanup(state,
362 (zcp_cleanup_t *)&fnvlist_free, dpsa->dpsa_props);
363
364 err = zcp_sync_task(state, zcp_synctask_inherit_prop_check,
365 zcp_synctask_inherit_prop_sync, &zipa, sync, dsname);
366
367 zcp_deregister_cleanup(state, zch);
368 fnvlist_free(dpsa->dpsa_props);
369
370 return (err);
371}
372
d99a0153
CW
373static int
374zcp_synctask_wrapper(lua_State *state)
375{
376 int err;
5b72a38d 377 zcp_cleanup_handler_t *zch;
d99a0153
CW
378 int num_ret = 1;
379 nvlist_t *err_details = fnvlist_alloc();
380
381 /*
382 * Make sure err_details is properly freed, even if a fatal error is
383 * thrown during the synctask.
384 */
5b72a38d
SD
385 zch = zcp_register_cleanup(state,
386 (zcp_cleanup_t *)&fnvlist_free, err_details);
d99a0153
CW
387
388 zcp_synctask_info_t *info = lua_touserdata(state, lua_upvalueindex(1));
389 boolean_t sync = lua_toboolean(state, lua_upvalueindex(2));
390
391 zcp_run_info_t *ri = zcp_run_info(state);
392 dsl_pool_t *dp = ri->zri_pool;
393
394 /* MOS space is triple-dittoed, so we multiply by 3. */
cbce5813
DB
395 uint64_t funcspace =
396 ((uint64_t)info->blocks_modified << DST_AVG_BLKSHIFT) * 3;
d99a0153
CW
397
398 zcp_parse_args(state, info->name, info->pargs, info->kwargs);
399
400 err = 0;
d2734cce
SD
401 if (info->space_check != ZFS_SPACE_CHECK_NONE) {
402 uint64_t quota = dsl_pool_unreserved_space(dp,
403 info->space_check);
d99a0153
CW
404 uint64_t used = dsl_dir_phys(dp->dp_root_dir)->dd_used_bytes +
405 ri->zri_space_used;
406
407 if (used + funcspace > quota) {
408 err = SET_ERROR(ENOSPC);
409 }
410 }
411
412 if (err == 0) {
413 err = info->func(state, sync, err_details);
414 }
415
416 if (err == 0) {
417 ri->zri_space_used += funcspace;
418 }
419
420 lua_pushnumber(state, (lua_Number)err);
421 if (fnvlist_num_pairs(err_details) > 0) {
422 (void) zcp_nvlist_to_lua(state, err_details, NULL, 0);
423 num_ret++;
424 }
425
5b72a38d 426 zcp_deregister_cleanup(state, zch);
d99a0153
CW
427 fnvlist_free(err_details);
428
429 return (num_ret);
430}
431
432int
433zcp_load_synctask_lib(lua_State *state, boolean_t sync)
434{
435 int i;
436 zcp_synctask_info_t *zcp_synctask_funcs[] = {
437 &zcp_synctask_destroy_info,
438 &zcp_synctask_promote_info,
af073689 439 &zcp_synctask_rollback_info,
234c91c5 440 &zcp_synctask_snapshot_info,
e2ef1cbf 441 &zcp_synctask_inherit_prop_info,
d99a0153
CW
442 NULL
443 };
444
445 lua_newtable(state);
446
447 for (i = 0; zcp_synctask_funcs[i] != NULL; i++) {
448 zcp_synctask_info_t *info = zcp_synctask_funcs[i];
449 lua_pushlightuserdata(state, info);
450 lua_pushboolean(state, sync);
451 lua_pushcclosure(state, &zcp_synctask_wrapper, 2);
452 lua_setfield(state, -2, info->name);
453 info++;
454 }
455
456 return (1);
457}