]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/zcp_synctask.c
Disable "-fipa-sra" when using "--enable-debuginfo"
[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.
e2ef1cbf 18 * Copyright 2020 Joyent, Inc.
d99a0153
CW
19 */
20
21#include <sys/lua/lua.h>
22#include <sys/lua/lauxlib.h>
23
24#include <sys/zcp.h>
25#include <sys/dsl_dir.h>
26#include <sys/dsl_pool.h>
27#include <sys/dsl_prop.h>
28#include <sys/dsl_synctask.h>
29#include <sys/dsl_dataset.h>
30#include <sys/dsl_bookmark.h>
31#include <sys/dsl_destroy.h>
32#include <sys/dmu_objset.h>
33#include <sys/zfs_znode.h>
34#include <sys/zfeature.h>
35#include <sys/metaslab.h>
36
37#define DST_AVG_BLKSHIFT 14
38
e2ef1cbf
JK
39typedef struct zcp_inherit_prop_arg {
40 lua_State *zipa_state;
41 const char *zipa_prop;
42 dsl_props_set_arg_t zipa_dpsa;
43} zcp_inherit_prop_arg_t;
44
d99a0153
CW
45typedef int (zcp_synctask_func_t)(lua_State *, boolean_t, nvlist_t *);
46typedef struct zcp_synctask_info {
47 const char *name;
48 zcp_synctask_func_t *func;
d99a0153
CW
49 const zcp_arg_t pargs[4];
50 const zcp_arg_t kwargs[2];
234c91c5
CW
51 zfs_space_check_t space_check;
52 int blocks_modified;
d99a0153
CW
53} zcp_synctask_info_t;
54
55/*
56 * Generic synctask interface for channel program syncfuncs.
57 *
58 * To perform some action in syncing context, we'd generally call
59 * dsl_sync_task(), but since the Lua script is already running inside a
60 * synctask we need to leave out some actions (such as acquiring the config
61 * rwlock and performing space checks).
62 *
63 * If 'sync' is false, executes a dry run and returns the error code.
64 *
5b72a38d
SD
65 * If we are not running in syncing context and we are not doing a dry run
66 * (meaning we are running a zfs.sync function in open-context) then we
67 * return a Lua error.
68 *
d99a0153
CW
69 * This function also handles common fatal error cases for channel program
70 * library functions. If a fatal error occurs, err_dsname will be the dataset
71 * name reported in error messages, if supplied.
72 */
73static int
74zcp_sync_task(lua_State *state, dsl_checkfunc_t *checkfunc,
75 dsl_syncfunc_t *syncfunc, void *arg, boolean_t sync, const char *err_dsname)
76{
77 int err;
78 zcp_run_info_t *ri = zcp_run_info(state);
79
80 err = checkfunc(arg, ri->zri_tx);
81 if (!sync)
82 return (err);
83
5b72a38d
SD
84 if (!ri->zri_sync) {
85 return (luaL_error(state, "running functions from the zfs.sync "
86 "submodule requires passing sync=TRUE to "
87 "lzc_channel_program() (i.e. do not specify the \"-n\" "
88 "command line argument)"));
89 }
90
d99a0153
CW
91 if (err == 0) {
92 syncfunc(arg, ri->zri_tx);
93 } else if (err == EIO) {
94 if (err_dsname != NULL) {
95 return (luaL_error(state,
96 "I/O error while accessing dataset '%s'",
97 err_dsname));
98 } else {
99 return (luaL_error(state,
100 "I/O error while accessing dataset."));
101 }
102 }
103
104 return (err);
105}
106
107
108static int zcp_synctask_destroy(lua_State *, boolean_t, nvlist_t *);
109static zcp_synctask_info_t zcp_synctask_destroy_info = {
110 .name = "destroy",
111 .func = zcp_synctask_destroy,
d99a0153
CW
112 .pargs = {
113 {.za_name = "filesystem | snapshot", .za_lua_type = LUA_TSTRING},
114 {NULL, 0}
115 },
116 .kwargs = {
117 {.za_name = "defer", .za_lua_type = LUA_TBOOLEAN},
118 {NULL, 0}
234c91c5 119 },
d2734cce 120 .space_check = ZFS_SPACE_CHECK_DESTROY,
234c91c5 121 .blocks_modified = 0
d99a0153
CW
122};
123
124/* ARGSUSED */
125static int
126zcp_synctask_destroy(lua_State *state, boolean_t sync, nvlist_t *err_details)
127{
128 int err;
129 const char *dsname = lua_tostring(state, 1);
130
131 boolean_t issnap = (strchr(dsname, '@') != NULL);
132
133 if (!issnap && !lua_isnil(state, 2)) {
134 return (luaL_error(state,
135 "'deferred' kwarg only supported for snapshots: %s",
136 dsname));
137 }
138
139 if (issnap) {
140 dsl_destroy_snapshot_arg_t ddsa = { 0 };
141 ddsa.ddsa_name = dsname;
142 if (!lua_isnil(state, 2)) {
143 ddsa.ddsa_defer = lua_toboolean(state, 2);
144 } else {
145 ddsa.ddsa_defer = B_FALSE;
146 }
147
148 err = zcp_sync_task(state, dsl_destroy_snapshot_check,
149 dsl_destroy_snapshot_sync, &ddsa, sync, dsname);
150 } else {
151 dsl_destroy_head_arg_t ddha = { 0 };
152 ddha.ddha_name = dsname;
153
154 err = zcp_sync_task(state, dsl_destroy_head_check,
155 dsl_destroy_head_sync, &ddha, sync, dsname);
156 }
157
158 return (err);
159}
160
234c91c5 161static int zcp_synctask_promote(lua_State *, boolean_t, nvlist_t *);
d99a0153
CW
162static zcp_synctask_info_t zcp_synctask_promote_info = {
163 .name = "promote",
164 .func = zcp_synctask_promote,
d99a0153
CW
165 .pargs = {
166 {.za_name = "clone", .za_lua_type = LUA_TSTRING},
167 {NULL, 0}
168 },
169 .kwargs = {
170 {NULL, 0}
234c91c5
CW
171 },
172 .space_check = ZFS_SPACE_CHECK_RESERVED,
173 .blocks_modified = 3
d99a0153
CW
174};
175
176static int
177zcp_synctask_promote(lua_State *state, boolean_t sync, nvlist_t *err_details)
178{
179 int err;
180 dsl_dataset_promote_arg_t ddpa = { 0 };
181 const char *dsname = lua_tostring(state, 1);
182 zcp_run_info_t *ri = zcp_run_info(state);
183
184 ddpa.ddpa_clonename = dsname;
185 ddpa.err_ds = err_details;
186 ddpa.cr = ri->zri_cred;
187
188 /*
189 * If there was a snapshot name conflict, then err_ds will be filled
190 * with a list of conflicting snapshot names.
191 */
192 err = zcp_sync_task(state, dsl_dataset_promote_check,
193 dsl_dataset_promote_sync, &ddpa, sync, dsname);
194
195 return (err);
196}
197
af073689
BL
198static int zcp_synctask_rollback(lua_State *, boolean_t, nvlist_t *err_details);
199static zcp_synctask_info_t zcp_synctask_rollback_info = {
200 .name = "rollback",
201 .func = zcp_synctask_rollback,
202 .space_check = ZFS_SPACE_CHECK_RESERVED,
203 .blocks_modified = 1,
204 .pargs = {
205 {.za_name = "filesystem", .za_lua_type = LUA_TSTRING},
206 {0, 0}
207 },
208 .kwargs = {
209 {0, 0}
210 }
211};
212
213static int
214zcp_synctask_rollback(lua_State *state, boolean_t sync, nvlist_t *err_details)
215{
216 int err;
217 const char *dsname = lua_tostring(state, 1);
218 dsl_dataset_rollback_arg_t ddra = { 0 };
219
220 ddra.ddra_fsname = dsname;
221 ddra.ddra_result = err_details;
222
223 err = zcp_sync_task(state, dsl_dataset_rollback_check,
224 dsl_dataset_rollback_sync, &ddra, sync, dsname);
225
226 return (err);
227}
228
234c91c5
CW
229static int zcp_synctask_snapshot(lua_State *, boolean_t, nvlist_t *);
230static zcp_synctask_info_t zcp_synctask_snapshot_info = {
231 .name = "snapshot",
232 .func = zcp_synctask_snapshot,
233 .pargs = {
234 {.za_name = "filesystem@snapname | volume@snapname",
235 .za_lua_type = LUA_TSTRING},
236 {NULL, 0}
237 },
238 .kwargs = {
239 {NULL, 0}
240 },
241 .space_check = ZFS_SPACE_CHECK_NORMAL,
242 .blocks_modified = 3
243};
244
245/* ARGSUSED */
246static int
247zcp_synctask_snapshot(lua_State *state, boolean_t sync, nvlist_t *err_details)
248{
249 int err;
250 dsl_dataset_snapshot_arg_t ddsa = { 0 };
251 const char *dsname = lua_tostring(state, 1);
252 zcp_run_info_t *ri = zcp_run_info(state);
253
5b72a38d
SD
254 /*
255 * On old pools, the ZIL must not be active when a snapshot is created,
256 * but we can't suspend the ZIL because we're already in syncing
257 * context.
258 */
259 if (spa_version(ri->zri_pool->dp_spa) < SPA_VERSION_FAST_SNAP) {
260 return (ENOTSUP);
261 }
262
234c91c5
CW
263 /*
264 * We only allow for a single snapshot rather than a list, so the
265 * error list output is unnecessary.
266 */
267 ddsa.ddsa_errors = NULL;
268 ddsa.ddsa_props = NULL;
269 ddsa.ddsa_cr = ri->zri_cred;
270 ddsa.ddsa_snaps = fnvlist_alloc();
271 fnvlist_add_boolean(ddsa.ddsa_snaps, dsname);
272
5b72a38d
SD
273 zcp_cleanup_handler_t *zch = zcp_register_cleanup(state,
274 (zcp_cleanup_t *)&fnvlist_free, ddsa.ddsa_snaps);
234c91c5
CW
275
276 err = zcp_sync_task(state, dsl_dataset_snapshot_check,
277 dsl_dataset_snapshot_sync, &ddsa, sync, dsname);
278
5b72a38d 279 zcp_deregister_cleanup(state, zch);
234c91c5
CW
280 fnvlist_free(ddsa.ddsa_snaps);
281
282 return (err);
283}
284
e2ef1cbf
JK
285static int zcp_synctask_inherit_prop(lua_State *, boolean_t,
286 nvlist_t *err_details);
287static zcp_synctask_info_t zcp_synctask_inherit_prop_info = {
288 .name = "inherit",
289 .func = zcp_synctask_inherit_prop,
290 .space_check = ZFS_SPACE_CHECK_RESERVED,
291 .blocks_modified = 2, /* 2 * numprops */
292 .pargs = {
293 { .za_name = "dataset", .za_lua_type = LUA_TSTRING },
294 { .za_name = "property", .za_lua_type = LUA_TSTRING },
295 { NULL, 0 }
296 },
297 .kwargs = {
298 { NULL, 0 }
299 },
300};
301
302static int
303zcp_synctask_inherit_prop_check(void *arg, dmu_tx_t *tx)
304{
305 zcp_inherit_prop_arg_t *args = arg;
306 zfs_prop_t prop = zfs_name_to_prop(args->zipa_prop);
307
308 if (prop == ZPROP_INVAL) {
309 if (zfs_prop_user(args->zipa_prop))
310 return (0);
311
312 return (EINVAL);
313 }
314
315 if (zfs_prop_readonly(prop))
316 return (EINVAL);
317
318 if (!zfs_prop_inheritable(prop))
319 return (EINVAL);
320
321 return (dsl_props_set_check(&args->zipa_dpsa, tx));
322}
323
324static void
325zcp_synctask_inherit_prop_sync(void *arg, dmu_tx_t *tx)
326{
327 zcp_inherit_prop_arg_t *args = arg;
328 dsl_props_set_arg_t *dpsa = &args->zipa_dpsa;
329
330 dsl_props_set_sync(dpsa, tx);
331}
332
333static int
334zcp_synctask_inherit_prop(lua_State *state, boolean_t sync,
335 nvlist_t *err_details)
336{
337 int err;
338 zcp_inherit_prop_arg_t zipa = { 0 };
339 dsl_props_set_arg_t *dpsa = &zipa.zipa_dpsa;
340
341 const char *dsname = lua_tostring(state, 1);
342 const char *prop = lua_tostring(state, 2);
343
344 zipa.zipa_state = state;
345 zipa.zipa_prop = prop;
346 dpsa->dpsa_dsname = dsname;
347 dpsa->dpsa_source = ZPROP_SRC_INHERITED;
348 dpsa->dpsa_props = fnvlist_alloc();
349 fnvlist_add_boolean(dpsa->dpsa_props, prop);
350
351 zcp_cleanup_handler_t *zch = zcp_register_cleanup(state,
352 (zcp_cleanup_t *)&fnvlist_free, dpsa->dpsa_props);
353
354 err = zcp_sync_task(state, zcp_synctask_inherit_prop_check,
355 zcp_synctask_inherit_prop_sync, &zipa, sync, dsname);
356
357 zcp_deregister_cleanup(state, zch);
358 fnvlist_free(dpsa->dpsa_props);
359
360 return (err);
361}
362
d99a0153
CW
363static int
364zcp_synctask_wrapper(lua_State *state)
365{
366 int err;
5b72a38d 367 zcp_cleanup_handler_t *zch;
d99a0153
CW
368 int num_ret = 1;
369 nvlist_t *err_details = fnvlist_alloc();
370
371 /*
372 * Make sure err_details is properly freed, even if a fatal error is
373 * thrown during the synctask.
374 */
5b72a38d
SD
375 zch = zcp_register_cleanup(state,
376 (zcp_cleanup_t *)&fnvlist_free, err_details);
d99a0153
CW
377
378 zcp_synctask_info_t *info = lua_touserdata(state, lua_upvalueindex(1));
379 boolean_t sync = lua_toboolean(state, lua_upvalueindex(2));
380
381 zcp_run_info_t *ri = zcp_run_info(state);
382 dsl_pool_t *dp = ri->zri_pool;
383
384 /* MOS space is triple-dittoed, so we multiply by 3. */
cbce5813
DB
385 uint64_t funcspace =
386 ((uint64_t)info->blocks_modified << DST_AVG_BLKSHIFT) * 3;
d99a0153
CW
387
388 zcp_parse_args(state, info->name, info->pargs, info->kwargs);
389
390 err = 0;
d2734cce
SD
391 if (info->space_check != ZFS_SPACE_CHECK_NONE) {
392 uint64_t quota = dsl_pool_unreserved_space(dp,
393 info->space_check);
d99a0153
CW
394 uint64_t used = dsl_dir_phys(dp->dp_root_dir)->dd_used_bytes +
395 ri->zri_space_used;
396
397 if (used + funcspace > quota) {
398 err = SET_ERROR(ENOSPC);
399 }
400 }
401
402 if (err == 0) {
403 err = info->func(state, sync, err_details);
404 }
405
406 if (err == 0) {
407 ri->zri_space_used += funcspace;
408 }
409
410 lua_pushnumber(state, (lua_Number)err);
411 if (fnvlist_num_pairs(err_details) > 0) {
412 (void) zcp_nvlist_to_lua(state, err_details, NULL, 0);
413 num_ret++;
414 }
415
5b72a38d 416 zcp_deregister_cleanup(state, zch);
d99a0153
CW
417 fnvlist_free(err_details);
418
419 return (num_ret);
420}
421
422int
423zcp_load_synctask_lib(lua_State *state, boolean_t sync)
424{
425 int i;
426 zcp_synctask_info_t *zcp_synctask_funcs[] = {
427 &zcp_synctask_destroy_info,
428 &zcp_synctask_promote_info,
af073689 429 &zcp_synctask_rollback_info,
234c91c5 430 &zcp_synctask_snapshot_info,
e2ef1cbf 431 &zcp_synctask_inherit_prop_info,
d99a0153
CW
432 NULL
433 };
434
435 lua_newtable(state);
436
437 for (i = 0; zcp_synctask_funcs[i] != NULL; i++) {
438 zcp_synctask_info_t *info = zcp_synctask_funcs[i];
439 lua_pushlightuserdata(state, info);
440 lua_pushboolean(state, sync);
441 lua_pushcclosure(state, &zcp_synctask_wrapper, 2);
442 lua_setfield(state, -2, info->name);
443 info++;
444 }
445
446 return (1);
447}