xref: /freebsd/sys/contrib/openzfs/module/zfs/zcp_synctask.c (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
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 (c) 2019, 2020 by Christian Schwarz. All rights reserved.
19  * Copyright 2020 Joyent, Inc.
20  */
21 
22 #include <sys/lua/lua.h>
23 #include <sys/lua/lauxlib.h>
24 
25 #include <sys/zcp.h>
26 #include <sys/zcp_set.h>
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 
41 typedef 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 
47 typedef int (zcp_synctask_func_t)(lua_State *, boolean_t, nvlist_t *);
48 typedef struct zcp_synctask_info {
49 	const char *name;
50 	zcp_synctask_func_t *func;
51 	const zcp_arg_t pargs[4];
52 	const zcp_arg_t kwargs[2];
53 	zfs_space_check_t space_check;
54 	int blocks_modified;
55 } zcp_synctask_info_t;
56 
57 /*
58  * Generic synctask interface for channel program syncfuncs.
59  *
60  * To perform some action in syncing context, we'd generally call
61  * dsl_sync_task(), but since the Lua script is already running inside a
62  * synctask we need to leave out some actions (such as acquiring the config
63  * rwlock and performing space checks).
64  *
65  * If 'sync' is false, executes a dry run and returns the error code.
66  *
67  * If we are not running in syncing context and we are not doing a dry run
68  * (meaning we are running a zfs.sync function in open-context) then we
69  * return a Lua error.
70  *
71  * This function also handles common fatal error cases for channel program
72  * library functions. If a fatal error occurs, err_dsname will be the dataset
73  * name reported in error messages, if supplied.
74  */
75 static int
76 zcp_sync_task(lua_State *state, dsl_checkfunc_t *checkfunc,
77     dsl_syncfunc_t *syncfunc, void *arg, boolean_t sync, const char *err_dsname)
78 {
79 	int err;
80 	zcp_run_info_t *ri = zcp_run_info(state);
81 
82 	err = checkfunc(arg, ri->zri_tx);
83 	if (!sync)
84 		return (err);
85 
86 	if (!ri->zri_sync) {
87 		return (luaL_error(state, "running functions from the zfs.sync "
88 		    "submodule requires passing sync=TRUE to "
89 		    "lzc_channel_program() (i.e. do not specify the \"-n\" "
90 		    "command line argument)"));
91 	}
92 
93 	if (err == 0) {
94 		syncfunc(arg, ri->zri_tx);
95 	} else if (err == EIO) {
96 		if (err_dsname != NULL) {
97 			return (luaL_error(state,
98 			    "I/O error while accessing dataset '%s'",
99 			    err_dsname));
100 		} else {
101 			return (luaL_error(state,
102 			    "I/O error while accessing dataset."));
103 		}
104 	}
105 
106 	return (err);
107 }
108 
109 
110 static int zcp_synctask_destroy(lua_State *, boolean_t, nvlist_t *);
111 static zcp_synctask_info_t zcp_synctask_destroy_info = {
112 	.name = "destroy",
113 	.func = zcp_synctask_destroy,
114 	.pargs = {
115 	    {.za_name = "filesystem | snapshot", .za_lua_type = LUA_TSTRING},
116 	    {NULL, 0}
117 	},
118 	.kwargs = {
119 	    {.za_name = "defer", .za_lua_type = LUA_TBOOLEAN},
120 	    {NULL, 0}
121 	},
122 	.space_check = ZFS_SPACE_CHECK_DESTROY,
123 	.blocks_modified = 0
124 };
125 
126 /* ARGSUSED */
127 static int
128 zcp_synctask_destroy(lua_State *state, boolean_t sync, nvlist_t *err_details)
129 {
130 	int err;
131 	const char *dsname = lua_tostring(state, 1);
132 
133 	boolean_t issnap = (strchr(dsname, '@') != NULL);
134 
135 	if (!issnap && !lua_isnil(state, 2)) {
136 		return (luaL_error(state,
137 		    "'deferred' kwarg only supported for snapshots: %s",
138 		    dsname));
139 	}
140 
141 	if (issnap) {
142 		dsl_destroy_snapshot_arg_t ddsa = { 0 };
143 		ddsa.ddsa_name = dsname;
144 		if (!lua_isnil(state, 2)) {
145 			ddsa.ddsa_defer = lua_toboolean(state, 2);
146 		} else {
147 			ddsa.ddsa_defer = B_FALSE;
148 		}
149 
150 		err = zcp_sync_task(state, dsl_destroy_snapshot_check,
151 		    dsl_destroy_snapshot_sync, &ddsa, sync, dsname);
152 	} else {
153 		dsl_destroy_head_arg_t ddha = { 0 };
154 		ddha.ddha_name = dsname;
155 
156 		err = zcp_sync_task(state, dsl_destroy_head_check,
157 		    dsl_destroy_head_sync, &ddha, sync, dsname);
158 	}
159 
160 	return (err);
161 }
162 
163 static int zcp_synctask_promote(lua_State *, boolean_t, nvlist_t *);
164 static zcp_synctask_info_t zcp_synctask_promote_info = {
165 	.name = "promote",
166 	.func = zcp_synctask_promote,
167 	.pargs = {
168 	    {.za_name = "clone", .za_lua_type = LUA_TSTRING},
169 	    {NULL, 0}
170 	},
171 	.kwargs = {
172 	    {NULL, 0}
173 	},
174 	.space_check = ZFS_SPACE_CHECK_RESERVED,
175 	.blocks_modified = 3
176 };
177 
178 static int
179 zcp_synctask_promote(lua_State *state, boolean_t sync, nvlist_t *err_details)
180 {
181 	int err;
182 	dsl_dataset_promote_arg_t ddpa = { 0 };
183 	const char *dsname = lua_tostring(state, 1);
184 	zcp_run_info_t *ri = zcp_run_info(state);
185 
186 	ddpa.ddpa_clonename = dsname;
187 	ddpa.err_ds = err_details;
188 	ddpa.cr = ri->zri_cred;
189 	ddpa.proc = ri->zri_proc;
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 	    {0, 0}
210 	},
211 	.kwargs = {
212 	    {0, 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 (SET_ERROR(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_proc = ri->zri_proc;
274 	ddsa.ddsa_snaps = fnvlist_alloc();
275 	fnvlist_add_boolean(ddsa.ddsa_snaps, dsname);
276 
277 	zcp_cleanup_handler_t *zch = zcp_register_cleanup(state,
278 	    (zcp_cleanup_t *)&fnvlist_free, ddsa.ddsa_snaps);
279 
280 	err = zcp_sync_task(state, dsl_dataset_snapshot_check,
281 	    dsl_dataset_snapshot_sync, &ddsa, sync, dsname);
282 
283 	if (err == 0) {
284 		/*
285 		 * We may need to create a new device minor node for this
286 		 * dataset (if it is a zvol and the "snapdev" property is set).
287 		 * Save it in the nvlist so that it can be processed in open
288 		 * context.
289 		 */
290 		fnvlist_add_boolean(ri->zri_new_zvols, dsname);
291 	}
292 
293 	zcp_deregister_cleanup(state, zch);
294 	fnvlist_free(ddsa.ddsa_snaps);
295 
296 	return (err);
297 }
298 
299 static int zcp_synctask_inherit_prop(lua_State *, boolean_t,
300     nvlist_t *err_details);
301 static zcp_synctask_info_t zcp_synctask_inherit_prop_info = {
302 	.name = "inherit",
303 	.func = zcp_synctask_inherit_prop,
304 	.space_check = ZFS_SPACE_CHECK_RESERVED,
305 	.blocks_modified = 2, /* 2 * numprops */
306 	.pargs = {
307 		{ .za_name = "dataset", .za_lua_type = LUA_TSTRING },
308 		{ .za_name = "property", .za_lua_type = LUA_TSTRING },
309 		{ NULL, 0 }
310 	},
311 	.kwargs = {
312 		{ NULL, 0 }
313 	},
314 };
315 
316 static int
317 zcp_synctask_inherit_prop_check(void *arg, dmu_tx_t *tx)
318 {
319 	zcp_inherit_prop_arg_t *args = arg;
320 	zfs_prop_t prop = zfs_name_to_prop(args->zipa_prop);
321 
322 	if (prop == ZPROP_INVAL) {
323 		if (zfs_prop_user(args->zipa_prop))
324 			return (0);
325 
326 		return (EINVAL);
327 	}
328 
329 	if (zfs_prop_readonly(prop))
330 		return (EINVAL);
331 
332 	if (!zfs_prop_inheritable(prop))
333 		return (EINVAL);
334 
335 	return (dsl_props_set_check(&args->zipa_dpsa, tx));
336 }
337 
338 static void
339 zcp_synctask_inherit_prop_sync(void *arg, dmu_tx_t *tx)
340 {
341 	zcp_inherit_prop_arg_t *args = arg;
342 	dsl_props_set_arg_t *dpsa = &args->zipa_dpsa;
343 
344 	dsl_props_set_sync(dpsa, tx);
345 }
346 
347 static int
348 zcp_synctask_inherit_prop(lua_State *state, boolean_t sync,
349     nvlist_t *err_details)
350 {
351 	int err;
352 	zcp_inherit_prop_arg_t zipa = { 0 };
353 	dsl_props_set_arg_t *dpsa = &zipa.zipa_dpsa;
354 
355 	const char *dsname = lua_tostring(state, 1);
356 	const char *prop = lua_tostring(state, 2);
357 
358 	zipa.zipa_state = state;
359 	zipa.zipa_prop = prop;
360 	dpsa->dpsa_dsname = dsname;
361 	dpsa->dpsa_source = ZPROP_SRC_INHERITED;
362 	dpsa->dpsa_props = fnvlist_alloc();
363 	fnvlist_add_boolean(dpsa->dpsa_props, prop);
364 
365 	zcp_cleanup_handler_t *zch = zcp_register_cleanup(state,
366 	    (zcp_cleanup_t *)&fnvlist_free, dpsa->dpsa_props);
367 
368 	err = zcp_sync_task(state, zcp_synctask_inherit_prop_check,
369 	    zcp_synctask_inherit_prop_sync, &zipa, sync, dsname);
370 
371 	zcp_deregister_cleanup(state, zch);
372 	fnvlist_free(dpsa->dpsa_props);
373 
374 	return (err);
375 }
376 
377 static int zcp_synctask_bookmark(lua_State *, boolean_t, nvlist_t *);
378 static zcp_synctask_info_t zcp_synctask_bookmark_info = {
379 	.name = "bookmark",
380 	.func = zcp_synctask_bookmark,
381 	.pargs = {
382 	    {.za_name = "snapshot | bookmark", .za_lua_type = LUA_TSTRING},
383 	    {.za_name = "bookmark", .za_lua_type = LUA_TSTRING},
384 	    {NULL, 0}
385 	},
386 	.kwargs = {
387 	    {NULL, 0}
388 	},
389 	.space_check = ZFS_SPACE_CHECK_NORMAL,
390 	.blocks_modified = 1,
391 };
392 
393 /* ARGSUSED */
394 static int
395 zcp_synctask_bookmark(lua_State *state, boolean_t sync, nvlist_t *err_details)
396 {
397 	int err;
398 	const char *source = lua_tostring(state, 1);
399 	const char *new = lua_tostring(state, 2);
400 
401 	nvlist_t *bmarks = fnvlist_alloc();
402 	fnvlist_add_string(bmarks, new, source);
403 
404 	zcp_cleanup_handler_t *zch = zcp_register_cleanup(state,
405 	    (zcp_cleanup_t *)&fnvlist_free, bmarks);
406 
407 	dsl_bookmark_create_arg_t dbca = {
408 		.dbca_bmarks = bmarks,
409 		.dbca_errors = NULL,
410 	};
411 	err = zcp_sync_task(state, dsl_bookmark_create_check,
412 	    dsl_bookmark_create_sync, &dbca, sync, source);
413 
414 	zcp_deregister_cleanup(state, zch);
415 	fnvlist_free(bmarks);
416 
417 	return (err);
418 }
419 
420 static int zcp_synctask_set_prop(lua_State *, boolean_t, nvlist_t *err_details);
421 static zcp_synctask_info_t zcp_synctask_set_prop_info = {
422 	.name = "set_prop",
423 	.func = zcp_synctask_set_prop,
424 	.space_check = ZFS_SPACE_CHECK_RESERVED,
425 	.blocks_modified = 2,
426 	.pargs = {
427 		{ .za_name = "dataset", .za_lua_type = LUA_TSTRING},
428 		{ .za_name = "property", .za_lua_type =  LUA_TSTRING},
429 		{ .za_name = "value", .za_lua_type =  LUA_TSTRING},
430 		{ NULL, 0 }
431 	},
432 	.kwargs = {
433 		{ NULL, 0 }
434 	}
435 };
436 
437 static int
438 zcp_synctask_set_prop(lua_State *state, boolean_t sync, nvlist_t *err_details)
439 {
440 	int err;
441 	zcp_set_prop_arg_t args = { 0 };
442 
443 	const char *dsname = lua_tostring(state, 1);
444 	const char *prop = lua_tostring(state, 2);
445 	const char *val = lua_tostring(state, 3);
446 
447 	args.state = state;
448 	args.dsname = dsname;
449 	args.prop = prop;
450 	args.val = val;
451 
452 	err = zcp_sync_task(state, zcp_set_prop_check, zcp_set_prop_sync,
453 	    &args, sync, dsname);
454 
455 	return (err);
456 }
457 
458 static int
459 zcp_synctask_wrapper(lua_State *state)
460 {
461 	int err;
462 	zcp_cleanup_handler_t *zch;
463 	int num_ret = 1;
464 	nvlist_t *err_details = fnvlist_alloc();
465 
466 	/*
467 	 * Make sure err_details is properly freed, even if a fatal error is
468 	 * thrown during the synctask.
469 	 */
470 	zch = zcp_register_cleanup(state,
471 	    (zcp_cleanup_t *)&fnvlist_free, err_details);
472 
473 	zcp_synctask_info_t *info = lua_touserdata(state, lua_upvalueindex(1));
474 	boolean_t sync = lua_toboolean(state, lua_upvalueindex(2));
475 
476 	zcp_run_info_t *ri = zcp_run_info(state);
477 	dsl_pool_t *dp = ri->zri_pool;
478 
479 	/* MOS space is triple-dittoed, so we multiply by 3. */
480 	uint64_t funcspace =
481 	    ((uint64_t)info->blocks_modified << DST_AVG_BLKSHIFT) * 3;
482 
483 	zcp_parse_args(state, info->name, info->pargs, info->kwargs);
484 
485 	err = 0;
486 	if (info->space_check != ZFS_SPACE_CHECK_NONE) {
487 		uint64_t quota = dsl_pool_unreserved_space(dp,
488 		    info->space_check);
489 		uint64_t used = dsl_dir_phys(dp->dp_root_dir)->dd_used_bytes +
490 		    ri->zri_space_used;
491 
492 		if (used + funcspace > quota) {
493 			err = SET_ERROR(ENOSPC);
494 		}
495 	}
496 
497 	if (err == 0) {
498 		err = info->func(state, sync, err_details);
499 	}
500 
501 	if (err == 0) {
502 		ri->zri_space_used += funcspace;
503 	}
504 
505 	lua_pushnumber(state, (lua_Number)err);
506 	if (fnvlist_num_pairs(err_details) > 0) {
507 		(void) zcp_nvlist_to_lua(state, err_details, NULL, 0);
508 		num_ret++;
509 	}
510 
511 	zcp_deregister_cleanup(state, zch);
512 	fnvlist_free(err_details);
513 
514 	return (num_ret);
515 }
516 
517 int
518 zcp_load_synctask_lib(lua_State *state, boolean_t sync)
519 {
520 	int i;
521 	zcp_synctask_info_t *zcp_synctask_funcs[] = {
522 		&zcp_synctask_destroy_info,
523 		&zcp_synctask_promote_info,
524 		&zcp_synctask_rollback_info,
525 		&zcp_synctask_snapshot_info,
526 		&zcp_synctask_inherit_prop_info,
527 		&zcp_synctask_bookmark_info,
528 		&zcp_synctask_set_prop_info,
529 		NULL
530 	};
531 
532 	lua_newtable(state);
533 
534 	for (i = 0; zcp_synctask_funcs[i] != NULL; i++) {
535 		zcp_synctask_info_t *info = zcp_synctask_funcs[i];
536 		lua_pushlightuserdata(state, info);
537 		lua_pushboolean(state, sync);
538 		lua_pushcclosure(state, &zcp_synctask_wrapper, 2);
539 		lua_setfield(state, -2, info->name);
540 		info++;
541 	}
542 
543 	return (1);
544 }
545