]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/zcp_synctask.c
Update ena-com HAL to v1.1.4.3 and update driver accordingly
[FreeBSD/FreeBSD.git] / sys / cddl / contrib / opensolaris / uts / common / fs / zfs / zcp_synctask.c
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  */
19
20 #include "lua.h"
21 #include "lauxlib.h"
22
23 #include <sys/zcp.h>
24 #include <sys/dsl_dir.h>
25 #include <sys/dsl_pool.h>
26 #include <sys/dsl_prop.h>
27 #include <sys/dsl_synctask.h>
28 #include <sys/dsl_dataset.h>
29 #include <sys/dsl_bookmark.h>
30 #include <sys/dsl_destroy.h>
31 #include <sys/dmu_objset.h>
32 #include <sys/zfs_znode.h>
33 #include <sys/zfeature.h>
34 #include <sys/metaslab.h>
35
36 #define DST_AVG_BLKSHIFT 14
37
38 typedef int (zcp_synctask_func_t)(lua_State *, boolean_t, nvlist_t *);
39 typedef struct zcp_synctask_info {
40         const char *name;
41         zcp_synctask_func_t *func;
42         const zcp_arg_t pargs[4];
43         const zcp_arg_t kwargs[2];
44         zfs_space_check_t space_check;
45         int blocks_modified;
46 } zcp_synctask_info_t;
47
48 /*
49  * Generic synctask interface for channel program syncfuncs.
50  *
51  * To perform some action in syncing context, we'd generally call
52  * dsl_sync_task(), but since the Lua script is already running inside a
53  * synctask we need to leave out some actions (such as acquiring the config
54  * rwlock and performing space checks).
55  *
56  * If 'sync' is false, executes a dry run and returns the error code.
57  *
58  * This function also handles common fatal error cases for channel program
59  * library functions. If a fatal error occurs, err_dsname will be the dataset
60  * name reported in error messages, if supplied.
61  */
62 static int
63 zcp_sync_task(lua_State *state, dsl_checkfunc_t *checkfunc,
64     dsl_syncfunc_t *syncfunc, void *arg, boolean_t sync, const char *err_dsname)
65 {
66         int err;
67         zcp_run_info_t *ri = zcp_run_info(state);
68
69         err = checkfunc(arg, ri->zri_tx);
70         if (!sync)
71                 return (err);
72
73         if (err == 0) {
74                 syncfunc(arg, ri->zri_tx);
75         } else if (err == EIO) {
76                 if (err_dsname != NULL) {
77                         return (luaL_error(state,
78                             "I/O error while accessing dataset '%s'",
79                             err_dsname));
80                 } else {
81                         return (luaL_error(state,
82                             "I/O error while accessing dataset."));
83                 }
84         }
85
86         return (err);
87 }
88
89
90 static int zcp_synctask_destroy(lua_State *, boolean_t, nvlist_t *);
91 static zcp_synctask_info_t zcp_synctask_destroy_info = {
92         .name = "destroy",
93         .func = zcp_synctask_destroy,
94         .pargs = {
95             {.za_name = "filesystem | snapshot", .za_lua_type = LUA_TSTRING},
96             {NULL, 0}
97         },
98         .kwargs = {
99             {.za_name = "defer", .za_lua_type = LUA_TBOOLEAN},
100             {NULL, 0}
101         },
102         .space_check = ZFS_SPACE_CHECK_NONE,
103         .blocks_modified = 0
104 };
105
106 /* ARGSUSED */
107 static int
108 zcp_synctask_destroy(lua_State *state, boolean_t sync, nvlist_t *err_details)
109 {
110         int err;
111         const char *dsname = lua_tostring(state, 1);
112
113         boolean_t issnap = (strchr(dsname, '@') != NULL);
114
115         if (!issnap && !lua_isnil(state, 2)) {
116                 return (luaL_error(state,
117                     "'deferred' kwarg only supported for snapshots: %s",
118                     dsname));
119         }
120
121         if (issnap) {
122                 dsl_destroy_snapshot_arg_t ddsa = { 0 };
123                 ddsa.ddsa_name = dsname;
124                 if (!lua_isnil(state, 2)) {
125                         ddsa.ddsa_defer = lua_toboolean(state, 2);
126                 } else {
127                         ddsa.ddsa_defer = B_FALSE;
128                 }
129
130                 err = zcp_sync_task(state, dsl_destroy_snapshot_check,
131                     dsl_destroy_snapshot_sync, &ddsa, sync, dsname);
132         } else {
133                 dsl_destroy_head_arg_t ddha = { 0 };
134                 ddha.ddha_name = dsname;
135
136                 err = zcp_sync_task(state, dsl_destroy_head_check,
137                     dsl_destroy_head_sync, &ddha, sync, dsname);
138         }
139
140         return (err);
141 }
142
143 static int zcp_synctask_promote(lua_State *, boolean_t, nvlist_t *);
144 static zcp_synctask_info_t zcp_synctask_promote_info = {
145         .name = "promote",
146         .func = zcp_synctask_promote,
147         .pargs = {
148             {.za_name = "clone", .za_lua_type = LUA_TSTRING},
149             {NULL, 0}
150         },
151         .kwargs = {
152             {NULL, 0}
153         },
154         .space_check = ZFS_SPACE_CHECK_RESERVED,
155         .blocks_modified = 3
156 };
157
158 static int
159 zcp_synctask_promote(lua_State *state, boolean_t sync, nvlist_t *err_details)
160 {
161         int err;
162         dsl_dataset_promote_arg_t ddpa = { 0 };
163         const char *dsname = lua_tostring(state, 1);
164         zcp_run_info_t *ri = zcp_run_info(state);
165
166         ddpa.ddpa_clonename = dsname;
167         ddpa.err_ds = err_details;
168         ddpa.cr = ri->zri_cred;
169
170         /*
171          * If there was a snapshot name conflict, then err_ds will be filled
172          * with a list of conflicting snapshot names.
173          */
174         err = zcp_sync_task(state, dsl_dataset_promote_check,
175             dsl_dataset_promote_sync, &ddpa, sync, dsname);
176
177         return (err);
178 }
179
180 static int zcp_synctask_rollback(lua_State *, boolean_t, nvlist_t *err_details);
181 static zcp_synctask_info_t zcp_synctask_rollback_info = {
182         .name = "rollback",
183         .func = zcp_synctask_rollback,
184         .space_check = ZFS_SPACE_CHECK_RESERVED,
185         .blocks_modified = 1,
186         .pargs = {
187             {.za_name = "filesystem", .za_lua_type = LUA_TSTRING},
188             {NULL, 0}
189         },
190         .kwargs = {
191             {NULL, 0}
192         }
193 };
194
195 static int
196 zcp_synctask_rollback(lua_State *state, boolean_t sync, nvlist_t *err_details)
197 {
198         int err;
199         const char *dsname = lua_tostring(state, 1);
200         dsl_dataset_rollback_arg_t ddra = { 0 };
201
202         ddra.ddra_fsname = dsname;
203         ddra.ddra_result = err_details;
204
205         err = zcp_sync_task(state, dsl_dataset_rollback_check,
206             dsl_dataset_rollback_sync, &ddra, sync, dsname);
207
208         return (err);
209 }
210
211 static int zcp_synctask_snapshot(lua_State *, boolean_t, nvlist_t *);
212 static zcp_synctask_info_t zcp_synctask_snapshot_info = {
213         .name = "snapshot",
214         .func = zcp_synctask_snapshot,
215         .pargs = {
216             {.za_name = "filesystem@snapname | volume@snapname",
217             .za_lua_type = LUA_TSTRING},
218             {NULL, 0}
219         },
220         .kwargs = {
221             {NULL, 0}
222         },
223         .space_check = ZFS_SPACE_CHECK_NORMAL,
224         .blocks_modified = 3
225 };
226
227 /* ARGSUSED */
228 static int
229 zcp_synctask_snapshot(lua_State *state, boolean_t sync, nvlist_t *err_details)
230 {
231         int err;
232         dsl_dataset_snapshot_arg_t ddsa = { 0 };
233         const char *dsname = lua_tostring(state, 1);
234         zcp_run_info_t *ri = zcp_run_info(state);
235
236         /*
237          * We only allow for a single snapshot rather than a list, so the
238          * error list output is unnecessary.
239          */
240         ddsa.ddsa_errors = NULL;
241         ddsa.ddsa_props = NULL;
242         ddsa.ddsa_cr = ri->zri_cred;
243         ddsa.ddsa_snaps = fnvlist_alloc();
244         fnvlist_add_boolean(ddsa.ddsa_snaps, dsname);
245
246         /*
247          * On old pools, the ZIL must not be active when a snapshot is created,
248          * but we can't suspend the ZIL because we're already in syncing
249          * context.
250          */
251         if (spa_version(ri->zri_pool->dp_spa) < SPA_VERSION_FAST_SNAP) {
252                 return (ENOTSUP);
253         }
254
255         err = zcp_sync_task(state, dsl_dataset_snapshot_check,
256             dsl_dataset_snapshot_sync, &ddsa, sync, dsname);
257
258         fnvlist_free(ddsa.ddsa_snaps);
259
260         return (err);
261 }
262
263 void
264 zcp_synctask_wrapper_cleanup(void *arg)
265 {
266         fnvlist_free(arg);
267 }
268
269 static int
270 zcp_synctask_wrapper(lua_State *state)
271 {
272         int err;
273         int num_ret = 1;
274         nvlist_t *err_details = fnvlist_alloc();
275
276         /*
277          * Make sure err_details is properly freed, even if a fatal error is
278          * thrown during the synctask.
279          */
280         zcp_register_cleanup(state, &zcp_synctask_wrapper_cleanup, err_details);
281
282         zcp_synctask_info_t *info = lua_touserdata(state, lua_upvalueindex(1));
283         boolean_t sync = lua_toboolean(state, lua_upvalueindex(2));
284
285         zcp_run_info_t *ri = zcp_run_info(state);
286         dsl_pool_t *dp = ri->zri_pool;
287
288         /* MOS space is triple-dittoed, so we multiply by 3. */
289         uint64_t funcspace = (info->blocks_modified << DST_AVG_BLKSHIFT) * 3;
290
291         zcp_parse_args(state, info->name, info->pargs, info->kwargs);
292
293         err = 0;
294         if (info->space_check != ZFS_SPACE_CHECK_NONE && funcspace > 0) {
295                 uint64_t quota = dsl_pool_adjustedsize(dp,
296                     info->space_check == ZFS_SPACE_CHECK_RESERVED) -
297                     metaslab_class_get_deferred(spa_normal_class(dp->dp_spa));
298                 uint64_t used = dsl_dir_phys(dp->dp_root_dir)->dd_used_bytes +
299                     ri->zri_space_used;
300
301                 if (used + funcspace > quota) {
302                         err = SET_ERROR(ENOSPC);
303                 }
304         }
305
306         if (err == 0) {
307                 err = info->func(state, sync, err_details);
308         }
309
310         if (err == 0) {
311                 ri->zri_space_used += funcspace;
312         }
313
314         lua_pushnumber(state, (lua_Number)err);
315         if (fnvlist_num_pairs(err_details) > 0) {
316                 (void) zcp_nvlist_to_lua(state, err_details, NULL, 0);
317                 num_ret++;
318         }
319
320         zcp_clear_cleanup(state);
321         fnvlist_free(err_details);
322
323         return (num_ret);
324 }
325
326 int
327 zcp_load_synctask_lib(lua_State *state, boolean_t sync)
328 {
329         int i;
330         zcp_synctask_info_t *zcp_synctask_funcs[] = {
331                 &zcp_synctask_destroy_info,
332                 &zcp_synctask_promote_info,
333                 &zcp_synctask_rollback_info,
334                 &zcp_synctask_snapshot_info,
335                 NULL
336         };
337
338         lua_newtable(state);
339
340         for (i = 0; zcp_synctask_funcs[i] != NULL; i++) {
341                 zcp_synctask_info_t *info = zcp_synctask_funcs[i];
342                 lua_pushlightuserdata(state, info);
343                 lua_pushboolean(state, sync);
344                 lua_pushcclosure(state, &zcp_synctask_wrapper, 2);
345                 lua_setfield(state, -2, info->name);
346                 info++;
347         }
348
349         return (1);
350 }