]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/zcp_synctask.c
FreeBSD: Add zfs_link_create() error handling
[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.
948f0c44 18 * Copyright (c) 2019, 2020 by Christian Schwarz. All rights reserved.
e2ef1cbf 19 * Copyright 2020 Joyent, Inc.
d99a0153
CW
20 */
21
22#include <sys/lua/lua.h>
23#include <sys/lua/lauxlib.h>
24
25#include <sys/zcp.h>
13b5a4d5 26#include <sys/zcp_set.h>
d99a0153
CW
27#include <sys/dsl_dir.h>
28#include <sys/dsl_pool.h>
29#include <sys/dsl_prop.h>
30#include <sys/dsl_synctask.h>
31#include <sys/dsl_dataset.h>
32#include <sys/dsl_bookmark.h>
33#include <sys/dsl_destroy.h>
34#include <sys/dmu_objset.h>
35#include <sys/zfs_znode.h>
36#include <sys/zfeature.h>
37#include <sys/metaslab.h>
38
39#define DST_AVG_BLKSHIFT 14
40
e2ef1cbf
JK
41typedef struct zcp_inherit_prop_arg {
42 lua_State *zipa_state;
43 const char *zipa_prop;
44 dsl_props_set_arg_t zipa_dpsa;
45} zcp_inherit_prop_arg_t;
46
d99a0153
CW
47typedef int (zcp_synctask_func_t)(lua_State *, boolean_t, nvlist_t *);
48typedef struct zcp_synctask_info {
49 const char *name;
50 zcp_synctask_func_t *func;
d99a0153
CW
51 const zcp_arg_t pargs[4];
52 const zcp_arg_t kwargs[2];
234c91c5
CW
53 zfs_space_check_t space_check;
54 int blocks_modified;
d99a0153
CW
55} zcp_synctask_info_t;
56
23c13c7e
AL
57static void
58zcp_synctask_cleanup(void *arg)
59{
60 fnvlist_free(arg);
61}
62
d99a0153
CW
63/*
64 * Generic synctask interface for channel program syncfuncs.
65 *
66 * To perform some action in syncing context, we'd generally call
67 * dsl_sync_task(), but since the Lua script is already running inside a
68 * synctask we need to leave out some actions (such as acquiring the config
69 * rwlock and performing space checks).
70 *
71 * If 'sync' is false, executes a dry run and returns the error code.
72 *
5b72a38d
SD
73 * If we are not running in syncing context and we are not doing a dry run
74 * (meaning we are running a zfs.sync function in open-context) then we
75 * return a Lua error.
76 *
d99a0153
CW
77 * This function also handles common fatal error cases for channel program
78 * library functions. If a fatal error occurs, err_dsname will be the dataset
79 * name reported in error messages, if supplied.
80 */
81static int
82zcp_sync_task(lua_State *state, dsl_checkfunc_t *checkfunc,
83 dsl_syncfunc_t *syncfunc, void *arg, boolean_t sync, const char *err_dsname)
84{
85 int err;
86 zcp_run_info_t *ri = zcp_run_info(state);
87
88 err = checkfunc(arg, ri->zri_tx);
89 if (!sync)
90 return (err);
91
5b72a38d
SD
92 if (!ri->zri_sync) {
93 return (luaL_error(state, "running functions from the zfs.sync "
94 "submodule requires passing sync=TRUE to "
95 "lzc_channel_program() (i.e. do not specify the \"-n\" "
96 "command line argument)"));
97 }
98
d99a0153
CW
99 if (err == 0) {
100 syncfunc(arg, ri->zri_tx);
101 } else if (err == EIO) {
102 if (err_dsname != NULL) {
103 return (luaL_error(state,
104 "I/O error while accessing dataset '%s'",
105 err_dsname));
106 } else {
107 return (luaL_error(state,
108 "I/O error while accessing dataset."));
109 }
110 }
111
112 return (err);
113}
114
115
116static int zcp_synctask_destroy(lua_State *, boolean_t, nvlist_t *);
18168da7 117static const zcp_synctask_info_t zcp_synctask_destroy_info = {
d99a0153
CW
118 .name = "destroy",
119 .func = zcp_synctask_destroy,
d99a0153 120 .pargs = {
18168da7 121 {.za_name = "filesystem | snapshot", .za_lua_type = LUA_TSTRING },
d99a0153
CW
122 {NULL, 0}
123 },
124 .kwargs = {
18168da7 125 {.za_name = "defer", .za_lua_type = LUA_TBOOLEAN },
d99a0153 126 {NULL, 0}
234c91c5 127 },
d2734cce 128 .space_check = ZFS_SPACE_CHECK_DESTROY,
234c91c5 129 .blocks_modified = 0
d99a0153
CW
130};
131
d99a0153
CW
132static int
133zcp_synctask_destroy(lua_State *state, boolean_t sync, nvlist_t *err_details)
134{
14e4e3cb 135 (void) err_details;
d99a0153
CW
136 int err;
137 const char *dsname = lua_tostring(state, 1);
138
139 boolean_t issnap = (strchr(dsname, '@') != NULL);
140
141 if (!issnap && !lua_isnil(state, 2)) {
142 return (luaL_error(state,
143 "'deferred' kwarg only supported for snapshots: %s",
144 dsname));
145 }
146
147 if (issnap) {
148 dsl_destroy_snapshot_arg_t ddsa = { 0 };
149 ddsa.ddsa_name = dsname;
150 if (!lua_isnil(state, 2)) {
151 ddsa.ddsa_defer = lua_toboolean(state, 2);
152 } else {
153 ddsa.ddsa_defer = B_FALSE;
154 }
155
156 err = zcp_sync_task(state, dsl_destroy_snapshot_check,
157 dsl_destroy_snapshot_sync, &ddsa, sync, dsname);
158 } else {
159 dsl_destroy_head_arg_t ddha = { 0 };
160 ddha.ddha_name = dsname;
161
162 err = zcp_sync_task(state, dsl_destroy_head_check,
163 dsl_destroy_head_sync, &ddha, sync, dsname);
164 }
165
166 return (err);
167}
168
234c91c5 169static int zcp_synctask_promote(lua_State *, boolean_t, nvlist_t *);
18168da7 170static const zcp_synctask_info_t zcp_synctask_promote_info = {
d99a0153
CW
171 .name = "promote",
172 .func = zcp_synctask_promote,
d99a0153 173 .pargs = {
18168da7 174 {.za_name = "clone", .za_lua_type = LUA_TSTRING },
d99a0153
CW
175 {NULL, 0}
176 },
177 .kwargs = {
178 {NULL, 0}
234c91c5
CW
179 },
180 .space_check = ZFS_SPACE_CHECK_RESERVED,
181 .blocks_modified = 3
d99a0153
CW
182};
183
184static int
185zcp_synctask_promote(lua_State *state, boolean_t sync, nvlist_t *err_details)
186{
187 int err;
188 dsl_dataset_promote_arg_t ddpa = { 0 };
189 const char *dsname = lua_tostring(state, 1);
190 zcp_run_info_t *ri = zcp_run_info(state);
191
192 ddpa.ddpa_clonename = dsname;
193 ddpa.err_ds = err_details;
194 ddpa.cr = ri->zri_cred;
e59a377a 195 ddpa.proc = ri->zri_proc;
d99a0153
CW
196
197 /*
198 * If there was a snapshot name conflict, then err_ds will be filled
199 * with a list of conflicting snapshot names.
200 */
201 err = zcp_sync_task(state, dsl_dataset_promote_check,
202 dsl_dataset_promote_sync, &ddpa, sync, dsname);
203
204 return (err);
205}
206
af073689 207static int zcp_synctask_rollback(lua_State *, boolean_t, nvlist_t *err_details);
18168da7 208static const zcp_synctask_info_t zcp_synctask_rollback_info = {
af073689
BL
209 .name = "rollback",
210 .func = zcp_synctask_rollback,
211 .space_check = ZFS_SPACE_CHECK_RESERVED,
212 .blocks_modified = 1,
213 .pargs = {
18168da7 214 {.za_name = "filesystem", .za_lua_type = LUA_TSTRING },
af073689
BL
215 {0, 0}
216 },
217 .kwargs = {
218 {0, 0}
219 }
220};
221
222static int
223zcp_synctask_rollback(lua_State *state, boolean_t sync, nvlist_t *err_details)
224{
225 int err;
226 const char *dsname = lua_tostring(state, 1);
227 dsl_dataset_rollback_arg_t ddra = { 0 };
228
229 ddra.ddra_fsname = dsname;
230 ddra.ddra_result = err_details;
231
232 err = zcp_sync_task(state, dsl_dataset_rollback_check,
233 dsl_dataset_rollback_sync, &ddra, sync, dsname);
234
235 return (err);
236}
237
234c91c5 238static int zcp_synctask_snapshot(lua_State *, boolean_t, nvlist_t *);
18168da7 239static const zcp_synctask_info_t zcp_synctask_snapshot_info = {
234c91c5
CW
240 .name = "snapshot",
241 .func = zcp_synctask_snapshot,
242 .pargs = {
243 {.za_name = "filesystem@snapname | volume@snapname",
18168da7 244 .za_lua_type = LUA_TSTRING },
234c91c5
CW
245 {NULL, 0}
246 },
247 .kwargs = {
248 {NULL, 0}
249 },
250 .space_check = ZFS_SPACE_CHECK_NORMAL,
251 .blocks_modified = 3
252};
253
234c91c5
CW
254static int
255zcp_synctask_snapshot(lua_State *state, boolean_t sync, nvlist_t *err_details)
256{
14e4e3cb 257 (void) err_details;
234c91c5
CW
258 int err;
259 dsl_dataset_snapshot_arg_t ddsa = { 0 };
260 const char *dsname = lua_tostring(state, 1);
261 zcp_run_info_t *ri = zcp_run_info(state);
262
5b72a38d
SD
263 /*
264 * On old pools, the ZIL must not be active when a snapshot is created,
265 * but we can't suspend the ZIL because we're already in syncing
266 * context.
267 */
268 if (spa_version(ri->zri_pool->dp_spa) < SPA_VERSION_FAST_SNAP) {
28caa74b 269 return (SET_ERROR(ENOTSUP));
5b72a38d
SD
270 }
271
234c91c5
CW
272 /*
273 * We only allow for a single snapshot rather than a list, so the
274 * error list output is unnecessary.
275 */
276 ddsa.ddsa_errors = NULL;
277 ddsa.ddsa_props = NULL;
278 ddsa.ddsa_cr = ri->zri_cred;
e59a377a 279 ddsa.ddsa_proc = ri->zri_proc;
234c91c5
CW
280 ddsa.ddsa_snaps = fnvlist_alloc();
281 fnvlist_add_boolean(ddsa.ddsa_snaps, dsname);
282
5b72a38d 283 zcp_cleanup_handler_t *zch = zcp_register_cleanup(state,
23c13c7e 284 zcp_synctask_cleanup, ddsa.ddsa_snaps);
234c91c5
CW
285
286 err = zcp_sync_task(state, dsl_dataset_snapshot_check,
287 dsl_dataset_snapshot_sync, &ddsa, sync, dsname);
288
ec213971
MA
289 if (err == 0) {
290 /*
291 * We may need to create a new device minor node for this
292 * dataset (if it is a zvol and the "snapdev" property is set).
293 * Save it in the nvlist so that it can be processed in open
294 * context.
295 */
296 fnvlist_add_boolean(ri->zri_new_zvols, dsname);
297 }
298
5b72a38d 299 zcp_deregister_cleanup(state, zch);
234c91c5
CW
300 fnvlist_free(ddsa.ddsa_snaps);
301
302 return (err);
303}
304
ee9f3bca
AG
305static int zcp_synctask_rename_snapshot(lua_State *, boolean_t, nvlist_t *);
306static const zcp_synctask_info_t zcp_synctask_rename_snapshot_info = {
307 .name = "rename_snapshot",
308 .func = zcp_synctask_rename_snapshot,
309 .pargs = {
310 {.za_name = "filesystem | volume", .za_lua_type = LUA_TSTRING },
311 {.za_name = "oldsnapname", .za_lua_type = LUA_TSTRING },
312 {.za_name = "newsnapname", .za_lua_type = LUA_TSTRING },
313 {NULL, 0}
314 },
315 .space_check = ZFS_SPACE_CHECK_RESERVED,
316 .blocks_modified = 1
317};
318
319static int
320zcp_synctask_rename_snapshot(lua_State *state, boolean_t sync,
321 nvlist_t *err_details)
322{
323 (void) err_details;
324 int err;
325 const char *fsname = lua_tostring(state, 1);
326 const char *oldsnapname = lua_tostring(state, 2);
327 const char *newsnapname = lua_tostring(state, 3);
328
329 struct dsl_dataset_rename_snapshot_arg ddrsa = { 0 };
330 ddrsa.ddrsa_fsname = fsname;
331 ddrsa.ddrsa_oldsnapname = oldsnapname;
332 ddrsa.ddrsa_newsnapname = newsnapname;
333 ddrsa.ddrsa_recursive = B_FALSE;
334
335 err = zcp_sync_task(state, dsl_dataset_rename_snapshot_check,
336 dsl_dataset_rename_snapshot_sync, &ddrsa, sync, NULL);
337
338 return (err);
339}
340
e2ef1cbf
JK
341static int zcp_synctask_inherit_prop(lua_State *, boolean_t,
342 nvlist_t *err_details);
18168da7 343static const zcp_synctask_info_t zcp_synctask_inherit_prop_info = {
e2ef1cbf
JK
344 .name = "inherit",
345 .func = zcp_synctask_inherit_prop,
346 .space_check = ZFS_SPACE_CHECK_RESERVED,
347 .blocks_modified = 2, /* 2 * numprops */
348 .pargs = {
349 { .za_name = "dataset", .za_lua_type = LUA_TSTRING },
350 { .za_name = "property", .za_lua_type = LUA_TSTRING },
351 { NULL, 0 }
352 },
353 .kwargs = {
354 { NULL, 0 }
355 },
356};
357
358static int
359zcp_synctask_inherit_prop_check(void *arg, dmu_tx_t *tx)
360{
361 zcp_inherit_prop_arg_t *args = arg;
362 zfs_prop_t prop = zfs_name_to_prop(args->zipa_prop);
363
4ff7a8fa 364 if (prop == ZPROP_USERPROP) {
e2ef1cbf
JK
365 if (zfs_prop_user(args->zipa_prop))
366 return (0);
367
368 return (EINVAL);
369 }
370
371 if (zfs_prop_readonly(prop))
372 return (EINVAL);
373
374 if (!zfs_prop_inheritable(prop))
375 return (EINVAL);
376
377 return (dsl_props_set_check(&args->zipa_dpsa, tx));
378}
379
380static void
381zcp_synctask_inherit_prop_sync(void *arg, dmu_tx_t *tx)
382{
383 zcp_inherit_prop_arg_t *args = arg;
384 dsl_props_set_arg_t *dpsa = &args->zipa_dpsa;
385
386 dsl_props_set_sync(dpsa, tx);
387}
388
389static int
390zcp_synctask_inherit_prop(lua_State *state, boolean_t sync,
391 nvlist_t *err_details)
392{
14e4e3cb 393 (void) err_details;
e2ef1cbf
JK
394 int err;
395 zcp_inherit_prop_arg_t zipa = { 0 };
396 dsl_props_set_arg_t *dpsa = &zipa.zipa_dpsa;
397
398 const char *dsname = lua_tostring(state, 1);
399 const char *prop = lua_tostring(state, 2);
400
401 zipa.zipa_state = state;
402 zipa.zipa_prop = prop;
403 dpsa->dpsa_dsname = dsname;
404 dpsa->dpsa_source = ZPROP_SRC_INHERITED;
405 dpsa->dpsa_props = fnvlist_alloc();
406 fnvlist_add_boolean(dpsa->dpsa_props, prop);
407
408 zcp_cleanup_handler_t *zch = zcp_register_cleanup(state,
23c13c7e 409 zcp_synctask_cleanup, dpsa->dpsa_props);
e2ef1cbf
JK
410
411 err = zcp_sync_task(state, zcp_synctask_inherit_prop_check,
412 zcp_synctask_inherit_prop_sync, &zipa, sync, dsname);
413
414 zcp_deregister_cleanup(state, zch);
415 fnvlist_free(dpsa->dpsa_props);
416
417 return (err);
418}
419
948f0c44 420static int zcp_synctask_bookmark(lua_State *, boolean_t, nvlist_t *);
18168da7 421static const zcp_synctask_info_t zcp_synctask_bookmark_info = {
948f0c44
CS
422 .name = "bookmark",
423 .func = zcp_synctask_bookmark,
424 .pargs = {
18168da7
AZ
425 {.za_name = "snapshot | bookmark", .za_lua_type = LUA_TSTRING },
426 {.za_name = "bookmark", .za_lua_type = LUA_TSTRING },
948f0c44
CS
427 {NULL, 0}
428 },
429 .kwargs = {
430 {NULL, 0}
431 },
432 .space_check = ZFS_SPACE_CHECK_NORMAL,
433 .blocks_modified = 1,
434};
435
948f0c44
CS
436static int
437zcp_synctask_bookmark(lua_State *state, boolean_t sync, nvlist_t *err_details)
438{
14e4e3cb 439 (void) err_details;
948f0c44
CS
440 int err;
441 const char *source = lua_tostring(state, 1);
442 const char *new = lua_tostring(state, 2);
443
444 nvlist_t *bmarks = fnvlist_alloc();
445 fnvlist_add_string(bmarks, new, source);
446
447 zcp_cleanup_handler_t *zch = zcp_register_cleanup(state,
23c13c7e 448 zcp_synctask_cleanup, bmarks);
948f0c44
CS
449
450 dsl_bookmark_create_arg_t dbca = {
451 .dbca_bmarks = bmarks,
452 .dbca_errors = NULL,
453 };
454 err = zcp_sync_task(state, dsl_bookmark_create_check,
455 dsl_bookmark_create_sync, &dbca, sync, source);
456
457 zcp_deregister_cleanup(state, zch);
458 fnvlist_free(bmarks);
459
460 return (err);
461}
462
13b5a4d5 463static int zcp_synctask_set_prop(lua_State *, boolean_t, nvlist_t *err_details);
18168da7 464static const zcp_synctask_info_t zcp_synctask_set_prop_info = {
13b5a4d5
JK
465 .name = "set_prop",
466 .func = zcp_synctask_set_prop,
467 .space_check = ZFS_SPACE_CHECK_RESERVED,
468 .blocks_modified = 2,
469 .pargs = {
18168da7
AZ
470 { .za_name = "dataset", .za_lua_type = LUA_TSTRING },
471 { .za_name = "property", .za_lua_type = LUA_TSTRING },
472 { .za_name = "value", .za_lua_type = LUA_TSTRING },
13b5a4d5
JK
473 { NULL, 0 }
474 },
475 .kwargs = {
476 { NULL, 0 }
477 }
478};
479
480static int
481zcp_synctask_set_prop(lua_State *state, boolean_t sync, nvlist_t *err_details)
482{
14e4e3cb 483 (void) err_details;
13b5a4d5
JK
484 int err;
485 zcp_set_prop_arg_t args = { 0 };
486
487 const char *dsname = lua_tostring(state, 1);
488 const char *prop = lua_tostring(state, 2);
489 const char *val = lua_tostring(state, 3);
490
491 args.state = state;
492 args.dsname = dsname;
493 args.prop = prop;
494 args.val = val;
495
496 err = zcp_sync_task(state, zcp_set_prop_check, zcp_set_prop_sync,
497 &args, sync, dsname);
498
499 return (err);
500}
501
d99a0153
CW
502static int
503zcp_synctask_wrapper(lua_State *state)
504{
505 int err;
5b72a38d 506 zcp_cleanup_handler_t *zch;
d99a0153
CW
507 int num_ret = 1;
508 nvlist_t *err_details = fnvlist_alloc();
509
510 /*
511 * Make sure err_details is properly freed, even if a fatal error is
512 * thrown during the synctask.
513 */
23c13c7e 514 zch = zcp_register_cleanup(state, zcp_synctask_cleanup, err_details);
d99a0153
CW
515
516 zcp_synctask_info_t *info = lua_touserdata(state, lua_upvalueindex(1));
517 boolean_t sync = lua_toboolean(state, lua_upvalueindex(2));
518
519 zcp_run_info_t *ri = zcp_run_info(state);
520 dsl_pool_t *dp = ri->zri_pool;
521
522 /* MOS space is triple-dittoed, so we multiply by 3. */
cbce5813
DB
523 uint64_t funcspace =
524 ((uint64_t)info->blocks_modified << DST_AVG_BLKSHIFT) * 3;
d99a0153
CW
525
526 zcp_parse_args(state, info->name, info->pargs, info->kwargs);
527
528 err = 0;
d2734cce
SD
529 if (info->space_check != ZFS_SPACE_CHECK_NONE) {
530 uint64_t quota = dsl_pool_unreserved_space(dp,
531 info->space_check);
d99a0153
CW
532 uint64_t used = dsl_dir_phys(dp->dp_root_dir)->dd_used_bytes +
533 ri->zri_space_used;
534
535 if (used + funcspace > quota) {
536 err = SET_ERROR(ENOSPC);
537 }
538 }
539
540 if (err == 0) {
541 err = info->func(state, sync, err_details);
542 }
543
544 if (err == 0) {
545 ri->zri_space_used += funcspace;
546 }
547
548 lua_pushnumber(state, (lua_Number)err);
549 if (fnvlist_num_pairs(err_details) > 0) {
550 (void) zcp_nvlist_to_lua(state, err_details, NULL, 0);
551 num_ret++;
552 }
553
5b72a38d 554 zcp_deregister_cleanup(state, zch);
d99a0153
CW
555 fnvlist_free(err_details);
556
557 return (num_ret);
558}
559
560int
561zcp_load_synctask_lib(lua_State *state, boolean_t sync)
562{
18168da7 563 const zcp_synctask_info_t *zcp_synctask_funcs[] = {
d99a0153
CW
564 &zcp_synctask_destroy_info,
565 &zcp_synctask_promote_info,
af073689 566 &zcp_synctask_rollback_info,
234c91c5 567 &zcp_synctask_snapshot_info,
ee9f3bca 568 &zcp_synctask_rename_snapshot_info,
e2ef1cbf 569 &zcp_synctask_inherit_prop_info,
948f0c44 570 &zcp_synctask_bookmark_info,
13b5a4d5 571 &zcp_synctask_set_prop_info,
d99a0153
CW
572 NULL
573 };
574
575 lua_newtable(state);
576
18168da7
AZ
577 for (int i = 0; zcp_synctask_funcs[i] != NULL; i++) {
578 const zcp_synctask_info_t *info = zcp_synctask_funcs[i];
579 lua_pushlightuserdata(state, (void *)(uintptr_t)info);
d99a0153
CW
580 lua_pushboolean(state, sync);
581 lua_pushcclosure(state, &zcp_synctask_wrapper, 2);
582 lua_setfield(state, -2, info->name);
d99a0153
CW
583 }
584
585 return (1);
586}