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
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.
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.
22 #include <sys/lua/lua.h>
23 #include <sys/lua/lauxlib.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>
39 #define DST_AVG_BLKSHIFT 14
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;
47 typedef int (zcp_synctask_func_t)(lua_State *, boolean_t, nvlist_t *);
48 typedef struct zcp_synctask_info {
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;
55 } zcp_synctask_info_t;
58 * Generic synctask interface for channel program syncfuncs.
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).
65 * If 'sync' is false, executes a dry run and returns the error code.
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
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.
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)
80 zcp_run_info_t *ri = zcp_run_info(state);
82 err = checkfunc(arg, ri->zri_tx);
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)"));
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'",
101 return (luaL_error(state,
102 "I/O error while accessing dataset."));
110 static int zcp_synctask_destroy(lua_State *, boolean_t, nvlist_t *);
111 static zcp_synctask_info_t zcp_synctask_destroy_info = {
113 .func = zcp_synctask_destroy,
115 {.za_name = "filesystem | snapshot", .za_lua_type = LUA_TSTRING},
119 {.za_name = "defer", .za_lua_type = LUA_TBOOLEAN},
122 .space_check = ZFS_SPACE_CHECK_DESTROY,
128 zcp_synctask_destroy(lua_State *state, boolean_t sync, nvlist_t *err_details)
131 const char *dsname = lua_tostring(state, 1);
133 boolean_t issnap = (strchr(dsname, '@') != NULL);
135 if (!issnap && !lua_isnil(state, 2)) {
136 return (luaL_error(state,
137 "'deferred' kwarg only supported for snapshots: %s",
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);
147 ddsa.ddsa_defer = B_FALSE;
150 err = zcp_sync_task(state, dsl_destroy_snapshot_check,
151 dsl_destroy_snapshot_sync, &ddsa, sync, dsname);
153 dsl_destroy_head_arg_t ddha = { 0 };
154 ddha.ddha_name = dsname;
156 err = zcp_sync_task(state, dsl_destroy_head_check,
157 dsl_destroy_head_sync, &ddha, sync, dsname);
163 static int zcp_synctask_promote(lua_State *, boolean_t, nvlist_t *);
164 static zcp_synctask_info_t zcp_synctask_promote_info = {
166 .func = zcp_synctask_promote,
168 {.za_name = "clone", .za_lua_type = LUA_TSTRING},
174 .space_check = ZFS_SPACE_CHECK_RESERVED,
179 zcp_synctask_promote(lua_State *state, boolean_t sync, nvlist_t *err_details)
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);
186 ddpa.ddpa_clonename = dsname;
187 ddpa.err_ds = err_details;
188 ddpa.cr = ri->zri_cred;
189 ddpa.proc = ri->zri_proc;
192 * If there was a snapshot name conflict, then err_ds will be filled
193 * with a list of conflicting snapshot names.
195 err = zcp_sync_task(state, dsl_dataset_promote_check,
196 dsl_dataset_promote_sync, &ddpa, sync, dsname);
201 static int zcp_synctask_rollback(lua_State *, boolean_t, nvlist_t *err_details);
202 static zcp_synctask_info_t zcp_synctask_rollback_info = {
204 .func = zcp_synctask_rollback,
205 .space_check = ZFS_SPACE_CHECK_RESERVED,
206 .blocks_modified = 1,
208 {.za_name = "filesystem", .za_lua_type = LUA_TSTRING},
217 zcp_synctask_rollback(lua_State *state, boolean_t sync, nvlist_t *err_details)
220 const char *dsname = lua_tostring(state, 1);
221 dsl_dataset_rollback_arg_t ddra = { 0 };
223 ddra.ddra_fsname = dsname;
224 ddra.ddra_result = err_details;
226 err = zcp_sync_task(state, dsl_dataset_rollback_check,
227 dsl_dataset_rollback_sync, &ddra, sync, dsname);
232 static int zcp_synctask_snapshot(lua_State *, boolean_t, nvlist_t *);
233 static zcp_synctask_info_t zcp_synctask_snapshot_info = {
235 .func = zcp_synctask_snapshot,
237 {.za_name = "filesystem@snapname | volume@snapname",
238 .za_lua_type = LUA_TSTRING},
244 .space_check = ZFS_SPACE_CHECK_NORMAL,
250 zcp_synctask_snapshot(lua_State *state, boolean_t sync, nvlist_t *err_details)
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);
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
262 if (spa_version(ri->zri_pool->dp_spa) < SPA_VERSION_FAST_SNAP) {
263 return (SET_ERROR(ENOTSUP));
267 * We only allow for a single snapshot rather than a list, so the
268 * error list output is unnecessary.
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);
277 zcp_cleanup_handler_t *zch = zcp_register_cleanup(state,
278 (zcp_cleanup_t *)&fnvlist_free, ddsa.ddsa_snaps);
280 err = zcp_sync_task(state, dsl_dataset_snapshot_check,
281 dsl_dataset_snapshot_sync, &ddsa, sync, dsname);
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
290 fnvlist_add_boolean(ri->zri_new_zvols, dsname);
293 zcp_deregister_cleanup(state, zch);
294 fnvlist_free(ddsa.ddsa_snaps);
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 = {
303 .func = zcp_synctask_inherit_prop,
304 .space_check = ZFS_SPACE_CHECK_RESERVED,
305 .blocks_modified = 2, /* 2 * numprops */
307 { .za_name = "dataset", .za_lua_type = LUA_TSTRING },
308 { .za_name = "property", .za_lua_type = LUA_TSTRING },
317 zcp_synctask_inherit_prop_check(void *arg, dmu_tx_t *tx)
319 zcp_inherit_prop_arg_t *args = arg;
320 zfs_prop_t prop = zfs_name_to_prop(args->zipa_prop);
322 if (prop == ZPROP_INVAL) {
323 if (zfs_prop_user(args->zipa_prop))
329 if (zfs_prop_readonly(prop))
332 if (!zfs_prop_inheritable(prop))
335 return (dsl_props_set_check(&args->zipa_dpsa, tx));
339 zcp_synctask_inherit_prop_sync(void *arg, dmu_tx_t *tx)
341 zcp_inherit_prop_arg_t *args = arg;
342 dsl_props_set_arg_t *dpsa = &args->zipa_dpsa;
344 dsl_props_set_sync(dpsa, tx);
348 zcp_synctask_inherit_prop(lua_State *state, boolean_t sync,
349 nvlist_t *err_details)
352 zcp_inherit_prop_arg_t zipa = { 0 };
353 dsl_props_set_arg_t *dpsa = &zipa.zipa_dpsa;
355 const char *dsname = lua_tostring(state, 1);
356 const char *prop = lua_tostring(state, 2);
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);
365 zcp_cleanup_handler_t *zch = zcp_register_cleanup(state,
366 (zcp_cleanup_t *)&fnvlist_free, dpsa->dpsa_props);
368 err = zcp_sync_task(state, zcp_synctask_inherit_prop_check,
369 zcp_synctask_inherit_prop_sync, &zipa, sync, dsname);
371 zcp_deregister_cleanup(state, zch);
372 fnvlist_free(dpsa->dpsa_props);
377 static int zcp_synctask_bookmark(lua_State *, boolean_t, nvlist_t *);
378 static zcp_synctask_info_t zcp_synctask_bookmark_info = {
380 .func = zcp_synctask_bookmark,
382 {.za_name = "snapshot | bookmark", .za_lua_type = LUA_TSTRING},
383 {.za_name = "bookmark", .za_lua_type = LUA_TSTRING},
389 .space_check = ZFS_SPACE_CHECK_NORMAL,
390 .blocks_modified = 1,
395 zcp_synctask_bookmark(lua_State *state, boolean_t sync, nvlist_t *err_details)
398 const char *source = lua_tostring(state, 1);
399 const char *new = lua_tostring(state, 2);
401 nvlist_t *bmarks = fnvlist_alloc();
402 fnvlist_add_string(bmarks, new, source);
404 zcp_cleanup_handler_t *zch = zcp_register_cleanup(state,
405 (zcp_cleanup_t *)&fnvlist_free, bmarks);
407 dsl_bookmark_create_arg_t dbca = {
408 .dbca_bmarks = bmarks,
411 err = zcp_sync_task(state, dsl_bookmark_create_check,
412 dsl_bookmark_create_sync, &dbca, sync, source);
414 zcp_deregister_cleanup(state, zch);
415 fnvlist_free(bmarks);
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 = {
423 .func = zcp_synctask_set_prop,
424 .space_check = ZFS_SPACE_CHECK_RESERVED,
425 .blocks_modified = 2,
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},
438 zcp_synctask_set_prop(lua_State *state, boolean_t sync, nvlist_t *err_details)
441 zcp_set_prop_arg_t args = { 0 };
443 const char *dsname = lua_tostring(state, 1);
444 const char *prop = lua_tostring(state, 2);
445 const char *val = lua_tostring(state, 3);
448 args.dsname = dsname;
452 err = zcp_sync_task(state, zcp_set_prop_check, zcp_set_prop_sync,
453 &args, sync, dsname);
459 zcp_synctask_wrapper(lua_State *state)
462 zcp_cleanup_handler_t *zch;
464 nvlist_t *err_details = fnvlist_alloc();
467 * Make sure err_details is properly freed, even if a fatal error is
468 * thrown during the synctask.
470 zch = zcp_register_cleanup(state,
471 (zcp_cleanup_t *)&fnvlist_free, err_details);
473 zcp_synctask_info_t *info = lua_touserdata(state, lua_upvalueindex(1));
474 boolean_t sync = lua_toboolean(state, lua_upvalueindex(2));
476 zcp_run_info_t *ri = zcp_run_info(state);
477 dsl_pool_t *dp = ri->zri_pool;
479 /* MOS space is triple-dittoed, so we multiply by 3. */
481 ((uint64_t)info->blocks_modified << DST_AVG_BLKSHIFT) * 3;
483 zcp_parse_args(state, info->name, info->pargs, info->kwargs);
486 if (info->space_check != ZFS_SPACE_CHECK_NONE) {
487 uint64_t quota = dsl_pool_unreserved_space(dp,
489 uint64_t used = dsl_dir_phys(dp->dp_root_dir)->dd_used_bytes +
492 if (used + funcspace > quota) {
493 err = SET_ERROR(ENOSPC);
498 err = info->func(state, sync, err_details);
502 ri->zri_space_used += funcspace;
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);
511 zcp_deregister_cleanup(state, zch);
512 fnvlist_free(err_details);
518 zcp_load_synctask_lib(lua_State *state, boolean_t sync)
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,
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);