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