xref: /illumos-gate/usr/src/uts/common/fs/zfs/zcp_synctask.c (revision dd72704bd9e794056c558153663c739e2012d721)
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 /*
17  * Copyright (c) 2016, 2017 by Delphix. All rights reserved.
18  * Copyright 2020 Joyent, Inc.
19  */
20 
21 #include "lua.h"
22 #include "lauxlib.h"
23 
24 #include <sys/zcp.h>
25 #include <sys/zcp_set.h>
26 #include <sys/zcp_change_key.h>
27 #include <sys/dsl_crypt.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 /*
59  * Generic synctask interface for channel program syncfuncs.
60  *
61  * To perform some action in syncing context, we'd generally call
62  * dsl_sync_task(), but since the Lua script is already running inside a
63  * synctask we need to leave out some actions (such as acquiring the config
64  * rwlock and performing space checks).
65  *
66  * If 'sync' is false, executes a dry run and returns the error code.
67  *
68  * If we are not running in syncing context and we are not doing a dry run
69  * (meaning we are running a zfs.sync function in open-context) then we
70  * return a Lua error.
71  *
72  * This function also handles common fatal error cases for channel program
73  * library functions. If a fatal error occurs, err_dsname will be the dataset
74  * name reported in error messages, if supplied.
75  */
76 static int
77 zcp_sync_task(lua_State *state, dsl_checkfunc_t *checkfunc,
78     dsl_syncfunc_t *syncfunc, void *arg, boolean_t sync, const char *err_dsname)
79 {
80 	int err;
81 	zcp_run_info_t *ri = zcp_run_info(state);
82 
83 	err = checkfunc(arg, ri->zri_tx);
84 	if (!sync)
85 		return (err);
86 
87 	if (!ri->zri_sync) {
88 		return (luaL_error(state, "running functions from the zfs.sync "
89 		    "submodule requires passing sync=TRUE to "
90 		    "lzc_channel_program() (i.e. do not specify the \"-n\" "
91 		    "command line argument)"));
92 	}
93 
94 	if (err == 0) {
95 		syncfunc(arg, ri->zri_tx);
96 	} else if (err == EIO) {
97 		if (err_dsname != NULL) {
98 			return (luaL_error(state,
99 			    "I/O error while accessing dataset '%s'",
100 			    err_dsname));
101 		} else {
102 			return (luaL_error(state,
103 			    "I/O error while accessing dataset."));
104 		}
105 	}
106 
107 	return (err);
108 }
109 
110 
111 static int zcp_synctask_destroy(lua_State *, boolean_t, nvlist_t *);
112 static zcp_synctask_info_t zcp_synctask_destroy_info = {
113 	.name = "destroy",
114 	.func = zcp_synctask_destroy,
115 	.pargs = {
116 	    {.za_name = "filesystem | snapshot", .za_lua_type = LUA_TSTRING},
117 	    {NULL, 0}
118 	},
119 	.kwargs = {
120 	    {.za_name = "defer", .za_lua_type = LUA_TBOOLEAN},
121 	    {NULL, 0}
122 	},
123 	.space_check = ZFS_SPACE_CHECK_DESTROY,
124 	.blocks_modified = 0
125 };
126 
127 /* ARGSUSED */
128 static int
129 zcp_synctask_destroy(lua_State *state, boolean_t sync, nvlist_t *err_details)
130 {
131 	int err;
132 	const char *dsname = lua_tostring(state, 1);
133 
134 	boolean_t issnap = (strchr(dsname, '@') != NULL);
135 
136 	if (!issnap && !lua_isnil(state, 2)) {
137 		return (luaL_error(state,
138 		    "'deferred' kwarg only supported for snapshots: %s",
139 		    dsname));
140 	}
141 
142 	if (issnap) {
143 		dsl_destroy_snapshot_arg_t ddsa = { 0 };
144 		ddsa.ddsa_name = dsname;
145 		if (!lua_isnil(state, 2)) {
146 			ddsa.ddsa_defer = lua_toboolean(state, 2);
147 		} else {
148 			ddsa.ddsa_defer = B_FALSE;
149 		}
150 
151 		err = zcp_sync_task(state, dsl_destroy_snapshot_check,
152 		    dsl_destroy_snapshot_sync, &ddsa, sync, dsname);
153 	} else {
154 		dsl_destroy_head_arg_t ddha = { 0 };
155 		ddha.ddha_name = dsname;
156 
157 		err = zcp_sync_task(state, dsl_destroy_head_check,
158 		    dsl_destroy_head_sync, &ddha, sync, dsname);
159 	}
160 
161 	return (err);
162 }
163 
164 static int zcp_synctask_promote(lua_State *, boolean_t, nvlist_t *);
165 static zcp_synctask_info_t zcp_synctask_promote_info = {
166 	.name = "promote",
167 	.func = zcp_synctask_promote,
168 	.pargs = {
169 	    {.za_name = "clone", .za_lua_type = LUA_TSTRING},
170 	    {NULL, 0}
171 	},
172 	.kwargs = {
173 	    {NULL, 0}
174 	},
175 	.space_check = ZFS_SPACE_CHECK_RESERVED,
176 	.blocks_modified = 3
177 };
178 
179 static int
180 zcp_synctask_promote(lua_State *state, boolean_t sync, nvlist_t *err_details)
181 {
182 	int err;
183 	dsl_dataset_promote_arg_t ddpa = { 0 };
184 	const char *dsname = lua_tostring(state, 1);
185 	zcp_run_info_t *ri = zcp_run_info(state);
186 
187 	ddpa.ddpa_clonename = dsname;
188 	ddpa.err_ds = err_details;
189 	ddpa.cr = ri->zri_cred;
190 
191 	/*
192 	 * If there was a snapshot name conflict, then err_ds will be filled
193 	 * with a list of conflicting snapshot names.
194 	 */
195 	err = zcp_sync_task(state, dsl_dataset_promote_check,
196 	    dsl_dataset_promote_sync, &ddpa, sync, dsname);
197 
198 	return (err);
199 }
200 
201 static int zcp_synctask_rollback(lua_State *, boolean_t, nvlist_t *err_details);
202 static zcp_synctask_info_t zcp_synctask_rollback_info = {
203 	.name = "rollback",
204 	.func = zcp_synctask_rollback,
205 	.space_check = ZFS_SPACE_CHECK_RESERVED,
206 	.blocks_modified = 1,
207 	.pargs = {
208 	    {.za_name = "filesystem", .za_lua_type = LUA_TSTRING},
209 	    {NULL, 0}
210 	},
211 	.kwargs = {
212 	    {NULL, 0}
213 	}
214 };
215 
216 static int
217 zcp_synctask_rollback(lua_State *state, boolean_t sync, nvlist_t *err_details)
218 {
219 	int err;
220 	const char *dsname = lua_tostring(state, 1);
221 	dsl_dataset_rollback_arg_t ddra = { 0 };
222 
223 	ddra.ddra_fsname = dsname;
224 	ddra.ddra_result = err_details;
225 
226 	err = zcp_sync_task(state, dsl_dataset_rollback_check,
227 	    dsl_dataset_rollback_sync, &ddra, sync, dsname);
228 
229 	return (err);
230 }
231 
232 static int zcp_synctask_snapshot(lua_State *, boolean_t, nvlist_t *);
233 static zcp_synctask_info_t zcp_synctask_snapshot_info = {
234 	.name = "snapshot",
235 	.func = zcp_synctask_snapshot,
236 	.pargs = {
237 	    {.za_name = "filesystem@snapname | volume@snapname",
238 	    .za_lua_type = LUA_TSTRING},
239 	    {NULL, 0}
240 	},
241 	.kwargs = {
242 	    {NULL, 0}
243 	},
244 	.space_check = ZFS_SPACE_CHECK_NORMAL,
245 	.blocks_modified = 3
246 };
247 
248 /* ARGSUSED */
249 static int
250 zcp_synctask_snapshot(lua_State *state, boolean_t sync, nvlist_t *err_details)
251 {
252 	int err;
253 	dsl_dataset_snapshot_arg_t ddsa = { 0 };
254 	const char *dsname = lua_tostring(state, 1);
255 	zcp_run_info_t *ri = zcp_run_info(state);
256 
257 	/*
258 	 * On old pools, the ZIL must not be active when a snapshot is created,
259 	 * but we can't suspend the ZIL because we're already in syncing
260 	 * context.
261 	 */
262 	if (spa_version(ri->zri_pool->dp_spa) < SPA_VERSION_FAST_SNAP) {
263 		return (ENOTSUP);
264 	}
265 
266 	/*
267 	 * We only allow for a single snapshot rather than a list, so the
268 	 * error list output is unnecessary.
269 	 */
270 	ddsa.ddsa_errors = NULL;
271 	ddsa.ddsa_props = NULL;
272 	ddsa.ddsa_cr = ri->zri_cred;
273 	ddsa.ddsa_snaps = fnvlist_alloc();
274 	fnvlist_add_boolean(ddsa.ddsa_snaps, dsname);
275 
276 	zcp_cleanup_handler_t *zch = zcp_register_cleanup(state,
277 	    (zcp_cleanup_t *)&fnvlist_free, ddsa.ddsa_snaps);
278 
279 	err = zcp_sync_task(state, dsl_dataset_snapshot_check,
280 	    dsl_dataset_snapshot_sync, &ddsa, sync, dsname);
281 
282 	zcp_deregister_cleanup(state, zch);
283 	fnvlist_free(ddsa.ddsa_snaps);
284 
285 	return (err);
286 }
287 
288 static int zcp_synctask_inherit_prop(lua_State *, boolean_t,
289     nvlist_t *err_details);
290 static zcp_synctask_info_t zcp_synctask_inherit_prop_info = {
291 	.name = "inherit",
292 	.func = zcp_synctask_inherit_prop,
293 	.space_check = ZFS_SPACE_CHECK_RESERVED,
294 	.blocks_modified = 2, /* 2 * numprops */
295 	.pargs = {
296 		{ .za_name = "dataset", .za_lua_type = LUA_TSTRING },
297 		{ .za_name = "property", .za_lua_type = LUA_TSTRING },
298 		{ NULL, 0 }
299 	},
300 	.kwargs = {
301 		{ NULL, 0 }
302 	},
303 };
304 
305 static int
306 zcp_synctask_inherit_prop_check(void *arg, dmu_tx_t *tx)
307 {
308 	zcp_inherit_prop_arg_t *args = arg;
309 	zfs_prop_t prop = zfs_name_to_prop(args->zipa_prop);
310 
311 	if (prop == ZPROP_INVAL) {
312 		if (zfs_prop_user(args->zipa_prop))
313 			return (0);
314 
315 		return (EINVAL);
316 	}
317 
318 	if (zfs_prop_readonly(prop))
319 		return (EINVAL);
320 
321 	if (!zfs_prop_inheritable(prop))
322 		return (EINVAL);
323 
324 	return (dsl_props_set_check(&args->zipa_dpsa, tx));
325 }
326 
327 static void
328 zcp_synctask_inherit_prop_sync(void *arg, dmu_tx_t *tx)
329 {
330 	zcp_inherit_prop_arg_t *args = arg;
331 	dsl_props_set_arg_t *dpsa = &args->zipa_dpsa;
332 
333 	dsl_props_set_sync(dpsa, tx);
334 }
335 
336 static int
337 zcp_synctask_inherit_prop(lua_State *state, boolean_t sync,
338     nvlist_t *err_details)
339 {
340 	int err;
341 	zcp_inherit_prop_arg_t zipa = { 0 };
342 	dsl_props_set_arg_t *dpsa = &zipa.zipa_dpsa;
343 
344 	const char *dsname = lua_tostring(state, 1);
345 	const char *prop = lua_tostring(state, 2);
346 
347 	zipa.zipa_state = state;
348 	zipa.zipa_prop = prop;
349 	dpsa->dpsa_dsname = dsname;
350 	dpsa->dpsa_source = ZPROP_SRC_INHERITED;
351 	dpsa->dpsa_props = fnvlist_alloc();
352 	fnvlist_add_boolean(dpsa->dpsa_props, prop);
353 
354 	zcp_cleanup_handler_t *zch = zcp_register_cleanup(state,
355 	    (zcp_cleanup_t *)&fnvlist_free, dpsa->dpsa_props);
356 
357 	err = zcp_sync_task(state, zcp_synctask_inherit_prop_check,
358 	    zcp_synctask_inherit_prop_sync, &zipa, sync, dsname);
359 
360 	zcp_deregister_cleanup(state, zch);
361 	fnvlist_free(dpsa->dpsa_props);
362 
363 	return (err);
364 }
365 
366 static int zcp_synctask_set_prop(lua_State *, boolean_t, nvlist_t *err_details);
367 static zcp_synctask_info_t zcp_synctask_set_prop_info = {
368 	.name = "set_prop",
369 	.func = zcp_synctask_set_prop,
370 	.space_check = ZFS_SPACE_CHECK_RESERVED,
371 	.blocks_modified = 2,
372 	.pargs = {
373 		{ .za_name = "dataset", .za_lua_type = LUA_TSTRING},
374 		{ .za_name = "property", .za_lua_type =  LUA_TSTRING},
375 		{ .za_name = "value", .za_lua_type =  LUA_TSTRING},
376 		{ NULL, 0 }
377 	},
378 	.kwargs = {
379 		{ NULL, 0 }
380 	}
381 };
382 
383 static int
384 zcp_synctask_set_prop(lua_State *state, boolean_t sync, nvlist_t *err_details)
385 {
386 	int err;
387 	zcp_set_prop_arg_t args = { 0 };
388 
389 	const char *dsname = lua_tostring(state, 1);
390 	const char *prop = lua_tostring(state, 2);
391 	const char *val = lua_tostring(state, 3);
392 
393 	args.state = state;
394 	args.dsname = dsname;
395 	args.prop = prop;
396 	args.val = val;
397 
398 	err = zcp_sync_task(state, zcp_set_prop_check, zcp_set_prop_sync,
399 	    &args, sync, dsname);
400 
401 	return (err);
402 }
403 
404 static int zcp_synctask_change_key(lua_State *, boolean_t, nvlist_t *);
405 static zcp_synctask_info_t zcp_synctask_change_key_info = {
406 	.name = "change_key",
407 	.func = zcp_synctask_change_key,
408 	.pargs = {
409 		{ .za_name = "dataset", .za_lua_type = LUA_TSTRING },
410 		{ .za_name = "key", .za_lua_type = LUA_TSTRING },
411 		{ .za_name = "format", .za_lua_type = LUA_TSTRING },
412 		{ NULL, 0 },
413 	},
414 	.kwargs = {
415 		{ NULL, 0 }
416 	},
417 	.space_check = ZFS_SPACE_CHECK_RESERVED,
418 	/*
419 	 * This is the same value that is used when zfs change-key is run.
420 	 * See spa_keystore_change_key() in dsl_crypt.c
421 	 */
422 	.blocks_modified = 15
423 };
424 
425 static int
426 zcp_synctask_change_key(lua_State *state, boolean_t sync, nvlist_t *err_details)
427 {
428 	int err;
429 	spa_keystore_change_key_args_t skcka = { 0 };
430 	dsl_crypto_params_t *dcp = NULL;
431 	const char *dsname;
432 	const char *key;
433 	const char *format;
434 	size_t keylen;
435 	uint64_t keyformat;
436 
437 	dsname = lua_tostring(state, 1);
438 
439 	/*
440 	 * The key may be raw key, which could contain NUL within it.
441 	 * Use lua_tolstring() instead of lua_tostring() to obtain the length.
442 	 */
443 	key = lua_tolstring(state, 2, &keylen);
444 
445 	format = lua_tostring(state, 3);
446 
447 	if (zfs_prop_string_to_index(ZFS_PROP_KEYFORMAT, format,
448 	    &keyformat) != 0)
449 		return (SET_ERROR(EINVAL));
450 
451 	err = zcp_synctask_change_key_create_params(key, keylen, keyformat,
452 	    &dcp);
453 	if (err != 0)
454 		goto done;
455 
456 	skcka.skcka_dsname = dsname;
457 	skcka.skcka_cp = dcp;
458 
459 	zcp_cleanup_handler_t *zch = zcp_register_cleanup(state,
460 	    (zcp_cleanup_t *)&zcp_synctask_change_key_cleanup, &skcka);
461 
462 	err = zcp_sync_task(state, zcp_synctask_change_key_check,
463 	    zcp_synctask_change_key_sync, &skcka, sync, dsname);
464 
465 	zcp_deregister_cleanup(state, zch);
466 
467 done:
468 	dsl_crypto_params_free(dcp, (err != 0 || !sync) ? B_TRUE : B_FALSE);
469 	return (err);
470 }
471 
472 static int
473 zcp_synctask_wrapper(lua_State *state)
474 {
475 	int err;
476 	zcp_cleanup_handler_t *zch;
477 	int num_ret = 1;
478 	nvlist_t *err_details = fnvlist_alloc();
479 
480 	/*
481 	 * Make sure err_details is properly freed, even if a fatal error is
482 	 * thrown during the synctask.
483 	 */
484 	zch = zcp_register_cleanup(state,
485 	    (zcp_cleanup_t *)&fnvlist_free, err_details);
486 
487 	zcp_synctask_info_t *info = lua_touserdata(state, lua_upvalueindex(1));
488 	boolean_t sync = lua_toboolean(state, lua_upvalueindex(2));
489 
490 	zcp_run_info_t *ri = zcp_run_info(state);
491 	dsl_pool_t *dp = ri->zri_pool;
492 
493 	/* MOS space is triple-dittoed, so we multiply by 3. */
494 	uint64_t funcspace = (info->blocks_modified << DST_AVG_BLKSHIFT) * 3;
495 
496 	zcp_parse_args(state, info->name, info->pargs, info->kwargs);
497 
498 	err = 0;
499 	if (info->space_check != ZFS_SPACE_CHECK_NONE) {
500 		uint64_t quota = dsl_pool_unreserved_space(dp,
501 		    info->space_check);
502 		uint64_t used = dsl_dir_phys(dp->dp_root_dir)->dd_used_bytes +
503 		    ri->zri_space_used;
504 
505 		if (used + funcspace > quota) {
506 			err = SET_ERROR(ENOSPC);
507 		}
508 	}
509 
510 	if (err == 0) {
511 		err = info->func(state, sync, err_details);
512 	}
513 
514 	if (err == 0) {
515 		ri->zri_space_used += funcspace;
516 	}
517 
518 	lua_pushnumber(state, (lua_Number)err);
519 	if (fnvlist_num_pairs(err_details) > 0) {
520 		(void) zcp_nvlist_to_lua(state, err_details, NULL, 0);
521 		num_ret++;
522 	}
523 
524 	zcp_deregister_cleanup(state, zch);
525 	fnvlist_free(err_details);
526 
527 	return (num_ret);
528 }
529 
530 int
531 zcp_load_synctask_lib(lua_State *state, boolean_t sync)
532 {
533 	int i;
534 	zcp_synctask_info_t *zcp_synctask_funcs[] = {
535 		&zcp_synctask_destroy_info,
536 		&zcp_synctask_promote_info,
537 		&zcp_synctask_rollback_info,
538 		&zcp_synctask_snapshot_info,
539 		&zcp_synctask_inherit_prop_info,
540 		&zcp_synctask_set_prop_info,
541 		&zcp_synctask_change_key_info,
542 		NULL
543 	};
544 
545 	lua_newtable(state);
546 
547 	for (i = 0; zcp_synctask_funcs[i] != NULL; i++) {
548 		zcp_synctask_info_t *info = zcp_synctask_funcs[i];
549 		lua_pushlightuserdata(state, info);
550 		lua_pushboolean(state, sync);
551 		lua_pushcclosure(state, &zcp_synctask_wrapper, 2);
552 		lua_setfield(state, -2, info->name);
553 		info++;
554 	}
555 
556 	return (1);
557 }
558