1dfc1153Chris Williamson/*
2dfc1153Chris Williamson * CDDL HEADER START
3dfc1153Chris Williamson *
4dfc1153Chris Williamson * This file and its contents are supplied under the terms of the
5dfc1153Chris Williamson * Common Development and Distribution License ("CDDL"), version 1.0.
6dfc1153Chris Williamson * You may only use this file in accordance with the terms of version
7dfc1153Chris Williamson * 1.0 of the CDDL.
8dfc1153Chris Williamson *
9dfc1153Chris Williamson * A full copy of the text of the CDDL should have accompanied this
10dfc1153Chris Williamson * source.  A copy of the CDDL is also available via the Internet at
11dfc1153Chris Williamson * http://www.illumos.org/license/CDDL.
12dfc1153Chris Williamson *
13dfc1153Chris Williamson * CDDL HEADER END
14dfc1153Chris Williamson */
15dfc1153Chris Williamson
16dfc1153Chris Williamson/*
17000cce6Brad Lewis * Copyright (c) 2016, 2017 by Delphix. All rights reserved.
18dfc1153Chris Williamson */
19dfc1153Chris Williamson
20dfc1153Chris Williamson#include "lua.h"
21dfc1153Chris Williamson#include "lauxlib.h"
22dfc1153Chris Williamson
23dfc1153Chris Williamson#include <sys/zcp.h>
24dfc1153Chris Williamson#include <sys/dsl_dir.h>
25dfc1153Chris Williamson#include <sys/dsl_pool.h>
26dfc1153Chris Williamson#include <sys/dsl_prop.h>
27dfc1153Chris Williamson#include <sys/dsl_synctask.h>
28dfc1153Chris Williamson#include <sys/dsl_dataset.h>
29dfc1153Chris Williamson#include <sys/dsl_bookmark.h>
30dfc1153Chris Williamson#include <sys/dsl_destroy.h>
31dfc1153Chris Williamson#include <sys/dmu_objset.h>
32dfc1153Chris Williamson#include <sys/zfs_znode.h>
33dfc1153Chris Williamson#include <sys/zfeature.h>
34dfc1153Chris Williamson#include <sys/metaslab.h>
35dfc1153Chris Williamson
36dfc1153Chris Williamson#define	DST_AVG_BLKSHIFT 14
37dfc1153Chris Williamson
38dfc1153Chris Williamsontypedef int (zcp_synctask_func_t)(lua_State *, boolean_t, nvlist_t *);
39dfc1153Chris Williamsontypedef struct zcp_synctask_info {
40dfc1153Chris Williamson	const char *name;
41dfc1153Chris Williamson	zcp_synctask_func_t *func;
42dfc1153Chris Williamson	const zcp_arg_t pargs[4];
43dfc1153Chris Williamson	const zcp_arg_t kwargs[2];
442840dceChris Williamson	zfs_space_check_t space_check;
452840dceChris Williamson	int blocks_modified;
46dfc1153Chris Williamson} zcp_synctask_info_t;
47dfc1153Chris Williamson
48dfc1153Chris Williamson/*
49dfc1153Chris Williamson * Generic synctask interface for channel program syncfuncs.
50dfc1153Chris Williamson *
51dfc1153Chris Williamson * To perform some action in syncing context, we'd generally call
52dfc1153Chris Williamson * dsl_sync_task(), but since the Lua script is already running inside a
53dfc1153Chris Williamson * synctask we need to leave out some actions (such as acquiring the config
54dfc1153Chris Williamson * rwlock and performing space checks).
55dfc1153Chris Williamson *
56dfc1153Chris Williamson * If 'sync' is false, executes a dry run and returns the error code.
57dfc1153Chris Williamson *
58a3b2868Serapheim Dimitropoulos * If we are not running in syncing context and we are not doing a dry run
59a3b2868Serapheim Dimitropoulos * (meaning we are running a zfs.sync function in open-context) then we
60a3b2868Serapheim Dimitropoulos * return a Lua error.
61a3b2868Serapheim Dimitropoulos *
62dfc1153Chris Williamson * This function also handles common fatal error cases for channel program
63dfc1153Chris Williamson * library functions. If a fatal error occurs, err_dsname will be the dataset
64dfc1153Chris Williamson * name reported in error messages, if supplied.
65dfc1153Chris Williamson */
66dfc1153Chris Williamsonstatic int
67dfc1153Chris Williamsonzcp_sync_task(lua_State *state, dsl_checkfunc_t *checkfunc,
68dfc1153Chris Williamson    dsl_syncfunc_t *syncfunc, void *arg, boolean_t sync, const char *err_dsname)
69dfc1153Chris Williamson{
70dfc1153Chris Williamson	int err;
71dfc1153Chris Williamson	zcp_run_info_t *ri = zcp_run_info(state);
72dfc1153Chris Williamson
73dfc1153Chris Williamson	err = checkfunc(arg, ri->zri_tx);
74dfc1153Chris Williamson	if (!sync)
75dfc1153Chris Williamson		return (err);
76dfc1153Chris Williamson
77a3b2868Serapheim Dimitropoulos	if (!ri->zri_sync) {
78a3b2868Serapheim Dimitropoulos		return (luaL_error(state, "running functions from the zfs.sync "
79a3b2868Serapheim Dimitropoulos		    "submodule requires passing sync=TRUE to "
80a3b2868Serapheim Dimitropoulos		    "lzc_channel_program() (i.e. do not specify the \"-n\" "
81a3b2868Serapheim Dimitropoulos		    "command line argument)"));
82a3b2868Serapheim Dimitropoulos	}
83a3b2868Serapheim Dimitropoulos
84dfc1153Chris Williamson	if (err == 0) {
85dfc1153Chris Williamson		syncfunc(arg, ri->zri_tx);
86dfc1153Chris Williamson	} else if (err == EIO) {
87dfc1153Chris Williamson		if (err_dsname != NULL) {
88dfc1153Chris Williamson			return (luaL_error(state,
89dfc1153Chris Williamson			    "I/O error while accessing dataset '%s'",
90dfc1153Chris Williamson			    err_dsname));
91dfc1153Chris Williamson		} else {
92dfc1153Chris Williamson			return (luaL_error(state,
93dfc1153Chris Williamson			    "I/O error while accessing dataset."));
94dfc1153Chris Williamson		}
95dfc1153Chris Williamson	}
96dfc1153Chris Williamson
97dfc1153Chris Williamson	return (err);
98dfc1153Chris Williamson}
99dfc1153Chris Williamson
100dfc1153Chris Williamson
101dfc1153Chris Williamsonstatic int zcp_synctask_destroy(lua_State *, boolean_t, nvlist_t *);
102dfc1153Chris Williamsonstatic zcp_synctask_info_t zcp_synctask_destroy_info = {
103dfc1153Chris Williamson	.name = "destroy",
104dfc1153Chris Williamson	.func = zcp_synctask_destroy,
105dfc1153Chris Williamson	.pargs = {
106dfc1153Chris Williamson	    {.za_name = "filesystem | snapshot", .za_lua_type = LUA_TSTRING},
107dd328bfToomas Soome	    {NULL, 0}
108dfc1153Chris Williamson	},
109dfc1153Chris Williamson	.kwargs = {
110dfc1153Chris Williamson	    {.za_name = "defer", .za_lua_type = LUA_TBOOLEAN},
111dd328bfToomas Soome	    {NULL, 0}
1122840dceChris Williamson	},
1138671400Serapheim Dimitropoulos	.space_check = ZFS_SPACE_CHECK_DESTROY,
1142840dceChris Williamson	.blocks_modified = 0
115dfc1153Chris Williamson};
116dfc1153Chris Williamson
117dfc1153Chris Williamson/* ARGSUSED */
118dfc1153Chris Williamsonstatic int
119dfc1153Chris Williamsonzcp_synctask_destroy(lua_State *state, boolean_t sync, nvlist_t *err_details)
120dfc1153Chris Williamson{
121dfc1153Chris Williamson	int err;
122dfc1153Chris Williamson	const char *dsname = lua_tostring(state, 1);
123dfc1153Chris Williamson
124dfc1153Chris Williamson	boolean_t issnap = (strchr(dsname, '@') != NULL);
125dfc1153Chris Williamson
126dfc1153Chris Williamson	if (!issnap && !lua_isnil(state, 2)) {
127dfc1153Chris Williamson		return (luaL_error(state,
128dfc1153Chris Williamson		    "'deferred' kwarg only supported for snapshots: %s",
129dfc1153Chris Williamson		    dsname));
130dfc1153Chris Williamson	}
131dfc1153Chris Williamson
132dfc1153Chris Williamson	if (issnap) {
133dfc1153Chris Williamson		dsl_destroy_snapshot_arg_t ddsa = { 0 };
134dfc1153Chris Williamson		ddsa.ddsa_name = dsname;
135dfc1153Chris Williamson		if (!lua_isnil(state, 2)) {
136dfc1153Chris Williamson			ddsa.ddsa_defer = lua_toboolean(state, 2);
137dfc1153Chris Williamson		} else {
138dfc1153Chris Williamson			ddsa.ddsa_defer = B_FALSE;
139dfc1153Chris Williamson		}
140dfc1153Chris Williamson
141dfc1153Chris Williamson		err = zcp_sync_task(state, dsl_destroy_snapshot_check,
142dfc1153Chris Williamson		    dsl_destroy_snapshot_sync, &ddsa, sync, dsname);
143dfc1153Chris Williamson	} else {
144dfc1153Chris Williamson		dsl_destroy_head_arg_t ddha = { 0 };
145dfc1153Chris Williamson		ddha.ddha_name = dsname;
146dfc1153Chris Williamson
147dfc1153Chris Williamson		err = zcp_sync_task(state, dsl_destroy_head_check,
148dfc1153Chris Williamson		    dsl_destroy_head_sync, &ddha, sync, dsname);
149dfc1153Chris Williamson	}
150dfc1153Chris Williamson
151dfc1153Chris Williamson	return (err);
152dfc1153Chris Williamson}
153dfc1153Chris Williamson
1542840dceChris Williamsonstatic int zcp_synctask_promote(lua_State *, boolean_t, nvlist_t *);
155dfc1153Chris Williamsonstatic zcp_synctask_info_t zcp_synctask_promote_info = {
156dfc1153Chris Williamson	.name = "promote",
157dfc1153Chris Williamson	.func = zcp_synctask_promote,
158dfc1153Chris Williamson	.pargs = {
159dfc1153Chris Williamson	    {.za_name = "clone", .za_lua_type = LUA_TSTRING},
160dd328bfToomas Soome	    {NULL, 0}
161dfc1153Chris Williamson	},
162dfc1153Chris Williamson	.kwargs = {
163dd328bfToomas Soome	    {NULL, 0}
1642840dceChris Williamson	},
1652840dceChris Williamson	.space_check = ZFS_SPACE_CHECK_RESERVED,
1662840dceChris Williamson	.blocks_modified = 3
167dfc1153Chris Williamson};
168dfc1153Chris Williamson
169dfc1153Chris Williamsonstatic int
170dfc1153Chris Williamsonzcp_synctask_promote(lua_State *state, boolean_t sync, nvlist_t *err_details)
171dfc1153Chris Williamson{
172dfc1153Chris Williamson	int err;
173dfc1153Chris Williamson	dsl_dataset_promote_arg_t ddpa = { 0 };
174dfc1153Chris Williamson	const char *dsname = lua_tostring(state, 1);
175dfc1153Chris Williamson	zcp_run_info_t *ri = zcp_run_info(state);
176dfc1153Chris Williamson
177dfc1153Chris Williamson	ddpa.ddpa_clonename = dsname;
178dfc1153Chris Williamson	ddpa.err_ds = err_details;
179dfc1153Chris Williamson	ddpa.cr = ri->zri_cred;
180dfc1153Chris Williamson
181dfc1153Chris Williamson	/*
182dfc1153Chris Williamson	 * If there was a snapshot name conflict, then err_ds will be filled
183dfc1153Chris Williamson	 * with a list of conflicting snapshot names.
184dfc1153Chris Williamson	 */
185dfc1153Chris Williamson	err = zcp_sync_task(state, dsl_dataset_promote_check,
186dfc1153Chris Williamson	    dsl_dataset_promote_sync, &ddpa, sync, dsname);
187dfc1153Chris Williamson
188dfc1153Chris Williamson	return (err);
189dfc1153Chris Williamson}
190dfc1153Chris Williamson
191000cce6Brad Lewisstatic int zcp_synctask_rollback(lua_State *, boolean_t, nvlist_t *err_details);
192000cce6Brad Lewisstatic zcp_synctask_info_t zcp_synctask_rollback_info = {
193000cce6Brad Lewis	.name = "rollback",
194000cce6Brad Lewis	.func = zcp_synctask_rollback,
195000cce6Brad Lewis	.space_check = ZFS_SPACE_CHECK_RESERVED,
196000cce6Brad Lewis	.blocks_modified = 1,
197000cce6Brad Lewis	.pargs = {
198000cce6Brad Lewis	    {.za_name = "filesystem", .za_lua_type = LUA_TSTRING},
199dd328bfToomas Soome	    {NULL, 0}
200000cce6Brad Lewis	},
201000cce6Brad Lewis	.kwargs = {
202dd328bfToomas Soome	    {NULL, 0}
203000cce6Brad Lewis	}
204000cce6Brad Lewis};
205000cce6Brad Lewis
206000cce6Brad Lewisstatic int
207000cce6Brad Lewiszcp_synctask_rollback(lua_State *state, boolean_t sync, nvlist_t *err_details)
208000cce6Brad Lewis{
209000cce6Brad Lewis	int err;
210000cce6Brad Lewis	const char *dsname = lua_tostring(state, 1);
211000cce6Brad Lewis	dsl_dataset_rollback_arg_t ddra = { 0 };
212000cce6Brad Lewis
213000cce6Brad Lewis	ddra.ddra_fsname = dsname;
214000cce6Brad Lewis	ddra.ddra_result = err_details;
215000cce6Brad Lewis
216000cce6Brad Lewis	err = zcp_sync_task(state, dsl_dataset_rollback_check,
217000cce6Brad Lewis	    dsl_dataset_rollback_sync, &ddra, sync, dsname);
218000cce6Brad Lewis
219000cce6Brad Lewis	return (err);
220000cce6Brad Lewis}
221000cce6Brad Lewis
2222840dceChris Williamsonstatic int zcp_synctask_snapshot(lua_State *, boolean_t, nvlist_t *);
2232840dceChris Williamsonstatic zcp_synctask_info_t zcp_synctask_snapshot_info = {
2242840dceChris Williamson	.name = "snapshot",
2252840dceChris Williamson	.func = zcp_synctask_snapshot,
2262840dceChris Williamson	.pargs = {
2272840dceChris Williamson	    {.za_name = "filesystem@snapname | volume@snapname",
2282840dceChris Williamson	    .za_lua_type = LUA_TSTRING},
229dd328bfToomas Soome	    {NULL, 0}
2302840dceChris Williamson	},
2312840dceChris Williamson	.kwargs = {
232dd328bfToomas Soome	    {NULL, 0}
2332840dceChris Williamson	},
2342840dceChris Williamson	.space_check = ZFS_SPACE_CHECK_NORMAL,
2352840dceChris Williamson	.blocks_modified = 3
2362840dceChris Williamson};
2372840dceChris Williamson
2382840dceChris Williamson/* ARGSUSED */
2392840dceChris Williamsonstatic int
2402840dceChris Williamsonzcp_synctask_snapshot(lua_State *state, boolean_t sync, nvlist_t *err_details)
2412840dceChris Williamson{
2422840dceChris Williamson	int err;
2432840dceChris Williamson	dsl_dataset_snapshot_arg_t ddsa = { 0 };
2442840dceChris Williamson	const char *dsname = lua_tostring(state, 1);
2452840dceChris Williamson	zcp_run_info_t *ri = zcp_run_info(state);
2462840dceChris Williamson
2472840dceChris Williamson	/*
248a3b2868Serapheim Dimitropoulos	 * On old pools, the ZIL must not be active when a snapshot is created,
249a3b2868Serapheim Dimitropoulos	 * but we can't suspend the ZIL because we're already in syncing
250a3b2868Serapheim Dimitropoulos	 * context.
251a3b2868Serapheim Dimitropoulos	 */
252a3b2868Serapheim Dimitropoulos	if (spa_version(ri->zri_pool->dp_spa) < SPA_VERSION_FAST_SNAP) {
253a3b2868Serapheim Dimitropoulos		return (ENOTSUP);
254a3b2868Serapheim Dimitropoulos	}
255a3b2868Serapheim Dimitropoulos
256a3b2868Serapheim Dimitropoulos	/*
2572840dceChris Williamson	 * We only allow for a single snapshot rather than a list, so the
2582840dceChris Williamson	 * error list output is unnecessary.
2592840dceChris Williamson	 */
2602840dceChris Williamson	ddsa.ddsa_errors = NULL;
2612840dceChris Williamson	ddsa.ddsa_props = NULL;
2622840dceChris Williamson	ddsa.ddsa_cr = ri->zri_cred;
2632840dceChris Williamson	ddsa.ddsa_snaps = fnvlist_alloc();
2642840dceChris Williamson	fnvlist_add_boolean(ddsa.ddsa_snaps, dsname);
2652840dceChris Williamson
266a3b2868Serapheim Dimitropoulos	zcp_cleanup_handler_t *zch = zcp_register_cleanup(state,
267a3b2868Serapheim Dimitropoulos	    (zcp_cleanup_t *)&fnvlist_free, ddsa.ddsa_snaps);
2682840dceChris Williamson
2692840dceChris Williamson	err = zcp_sync_task(state, dsl_dataset_snapshot_check,
2702840dceChris Williamson	    dsl_dataset_snapshot_sync, &ddsa, sync, dsname);
2712840dceChris Williamson
272a3b2868Serapheim Dimitropoulos	zcp_deregister_cleanup(state, zch);
2732840dceChris Williamson	fnvlist_free(ddsa.ddsa_snaps);
2742840dceChris Williamson
2752840dceChris Williamson	return (err);
2762840dceChris Williamson}
2772840dceChris Williamson
278dfc1153Chris Williamsonstatic int
279dfc1153Chris Williamsonzcp_synctask_wrapper(lua_State *state)
280dfc1153Chris Williamson{
281dfc1153Chris Williamson	int err;
282a3b2868Serapheim Dimitropoulos	zcp_cleanup_handler_t *zch;
283dfc1153Chris Williamson	int num_ret = 1;
284dfc1153Chris Williamson	nvlist_t *err_details = fnvlist_alloc();
285dfc1153Chris Williamson
286dfc1153Chris Williamson	/*
287dfc1153Chris Williamson	 * Make sure err_details is properly freed, even if a fatal error is
288dfc1153Chris Williamson	 * thrown during the synctask.
289dfc1153Chris Williamson	 */
290a3b2868Serapheim Dimitropoulos	zch = zcp_register_cleanup(state,
291a3b2868Serapheim Dimitropoulos	    (zcp_cleanup_t *)&fnvlist_free, err_details);
292dfc1153Chris Williamson
293dfc1153Chris Williamson	zcp_synctask_info_t *info = lua_touserdata(state, lua_upvalueindex(1));
294dfc1153Chris Williamson	boolean_t sync = lua_toboolean(state, lua_upvalueindex(2));
295dfc1153Chris Williamson
296dfc1153Chris Williamson	zcp_run_info_t *ri = zcp_run_info(state);
297dfc1153Chris Williamson	dsl_pool_t *dp = ri->zri_pool;
298dfc1153Chris Williamson
299dfc1153Chris Williamson	/* MOS space is triple-dittoed, so we multiply by 3. */
300dfc1153Chris Williamson	uint64_t funcspace = (info->blocks_modified << DST_AVG_BLKSHIFT) * 3;
301dfc1153Chris Williamson
302dfc1153Chris Williamson	zcp_parse_args(state, info->name, info->pargs, info->kwargs);
303dfc1153Chris Williamson
304dfc1153Chris Williamson	err = 0;
3058671400Serapheim Dimitropoulos	if (info->space_check != ZFS_SPACE_CHECK_NONE) {
3068671400Serapheim Dimitropoulos		uint64_t quota = dsl_pool_unreserved_space(dp,
3078671400Serapheim Dimitropoulos		    info->space_check);
308dfc1153Chris Williamson		uint64_t used = dsl_dir_phys(dp->dp_root_dir)->dd_used_bytes +
309dfc1153Chris Williamson		    ri->zri_space_used;
310dfc1153Chris Williamson
311dfc1153Chris Williamson		if (used + funcspace > quota) {
312dfc1153Chris Williamson			err = SET_ERROR(ENOSPC);
313dfc1153Chris Williamson		}
314dfc1153Chris Williamson	}
315dfc1153Chris Williamson
316dfc1153Chris Williamson	if (err == 0) {
317dfc1153Chris Williamson		err = info->func(state, sync, err_details);
318dfc1153Chris Williamson	}
319dfc1153Chris Williamson
320dfc1153Chris Williamson	if (err == 0) {
321dfc1153Chris Williamson		ri->zri_space_used += funcspace;
322dfc1153Chris Williamson	}
323dfc1153Chris Williamson
324dfc1153Chris Williamson	lua_pushnumber(state, (lua_Number)err);
325dfc1153Chris Williamson	if (fnvlist_num_pairs(err_details) > 0) {
326dfc1153Chris Williamson		(void) zcp_nvlist_to_lua(state, err_details, NULL, 0);
327dfc1153Chris Williamson		num_ret++;
328dfc1153Chris Williamson	}
329dfc1153Chris Williamson
330a3b2868Serapheim Dimitropoulos	zcp_deregister_cleanup(state, zch);
331dfc1153Chris Williamson	fnvlist_free(err_details);
332dfc1153Chris Williamson
333dfc1153Chris Williamson	return (num_ret);
334dfc1153Chris Williamson}
335dfc1153Chris Williamson
336dfc1153Chris Williamsonint
337dfc1153Chris Williamsonzcp_load_synctask_lib(lua_State *state, boolean_t sync)
338dfc1153Chris Williamson{
339dfc1153Chris Williamson	int i;
340dfc1153Chris Williamson	zcp_synctask_info_t *zcp_synctask_funcs[] = {
341dfc1153Chris Williamson		&zcp_synctask_destroy_info,
342dfc1153Chris Williamson		&zcp_synctask_promote_info,
343000cce6Brad Lewis		&zcp_synctask_rollback_info,
3442840dceChris Williamson		&zcp_synctask_snapshot_info,
345dfc1153Chris Williamson		NULL
346dfc1153Chris Williamson	};
347dfc1153Chris Williamson
348dfc1153Chris Williamson	lua_newtable(state);
349dfc1153Chris Williamson
350dfc1153Chris Williamson	for (i = 0; zcp_synctask_funcs[i] != NULL; i++) {
351dfc1153Chris Williamson		zcp_synctask_info_t *info = zcp_synctask_funcs[i];
352dfc1153Chris Williamson		lua_pushlightuserdata(state, info);
353dfc1153Chris Williamson		lua_pushboolean(state, sync);
354dfc1153Chris Williamson		lua_pushcclosure(state, &zcp_synctask_wrapper, 2);
355dfc1153Chris Williamson		lua_setfield(state, -2, info->name);
356dfc1153Chris Williamson		info++;
357dfc1153Chris Williamson	}
358dfc1153Chris Williamson
359dfc1153Chris Williamson	return (1);
360dfc1153Chris Williamson}