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 2020 Joyent, Inc. 19 */ 20 21 #include "lua.h" 22 #include "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 39 typedef 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 45 typedef int (zcp_synctask_func_t)(lua_State *, boolean_t, nvlist_t *); 46 typedef struct zcp_synctask_info { 47 const char *name; 48 zcp_synctask_func_t *func; 49 const zcp_arg_t pargs[4]; 50 const zcp_arg_t kwargs[2]; 51 zfs_space_check_t space_check; 52 int blocks_modified; 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 * 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 * 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 */ 73 static int 74 zcp_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 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 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 108 static int zcp_synctask_destroy(lua_State *, boolean_t, nvlist_t *); 109 static zcp_synctask_info_t zcp_synctask_destroy_info = { 110 .name = "destroy", 111 .func = zcp_synctask_destroy, 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} 119 }, 120 .space_check = ZFS_SPACE_CHECK_DESTROY, 121 .blocks_modified = 0 122 }; 123 124 /* ARGSUSED */ 125 static int 126 zcp_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 161 static int zcp_synctask_promote(lua_State *, boolean_t, nvlist_t *); 162 static zcp_synctask_info_t zcp_synctask_promote_info = { 163 .name = "promote", 164 .func = zcp_synctask_promote, 165 .pargs = { 166 {.za_name = "clone", .za_lua_type = LUA_TSTRING}, 167 {NULL, 0} 168 }, 169 .kwargs = { 170 {NULL, 0} 171 }, 172 .space_check = ZFS_SPACE_CHECK_RESERVED, 173 .blocks_modified = 3 174 }; 175 176 static int 177 zcp_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 198 static int zcp_synctask_rollback(lua_State *, boolean_t, nvlist_t *err_details); 199 static 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 {NULL, 0} 207 }, 208 .kwargs = { 209 {NULL, 0} 210 } 211 }; 212 213 static int 214 zcp_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 229 static int zcp_synctask_snapshot(lua_State *, boolean_t, nvlist_t *); 230 static 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 */ 246 static int 247 zcp_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 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 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 273 zcp_cleanup_handler_t *zch = zcp_register_cleanup(state, 274 (zcp_cleanup_t *)&fnvlist_free, ddsa.ddsa_snaps); 275 276 err = zcp_sync_task(state, dsl_dataset_snapshot_check, 277 dsl_dataset_snapshot_sync, &ddsa, sync, dsname); 278 279 zcp_deregister_cleanup(state, zch); 280 fnvlist_free(ddsa.ddsa_snaps); 281 282 return (err); 283 } 284 285 static int zcp_synctask_inherit_prop(lua_State *, boolean_t, 286 nvlist_t *err_details); 287 static 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 302 static int 303 zcp_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 324 static void 325 zcp_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 333 static int 334 zcp_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 363 static int 364 zcp_synctask_wrapper(lua_State *state) 365 { 366 int err; 367 zcp_cleanup_handler_t *zch; 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 */ 375 zch = zcp_register_cleanup(state, 376 (zcp_cleanup_t *)&fnvlist_free, err_details); 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. */ 385 uint64_t funcspace = (info->blocks_modified << DST_AVG_BLKSHIFT) * 3; 386 387 zcp_parse_args(state, info->name, info->pargs, info->kwargs); 388 389 err = 0; 390 if (info->space_check != ZFS_SPACE_CHECK_NONE) { 391 uint64_t quota = dsl_pool_unreserved_space(dp, 392 info->space_check); 393 uint64_t used = dsl_dir_phys(dp->dp_root_dir)->dd_used_bytes + 394 ri->zri_space_used; 395 396 if (used + funcspace > quota) { 397 err = SET_ERROR(ENOSPC); 398 } 399 } 400 401 if (err == 0) { 402 err = info->func(state, sync, err_details); 403 } 404 405 if (err == 0) { 406 ri->zri_space_used += funcspace; 407 } 408 409 lua_pushnumber(state, (lua_Number)err); 410 if (fnvlist_num_pairs(err_details) > 0) { 411 (void) zcp_nvlist_to_lua(state, err_details, NULL, 0); 412 num_ret++; 413 } 414 415 zcp_deregister_cleanup(state, zch); 416 fnvlist_free(err_details); 417 418 return (num_ret); 419 } 420 421 int 422 zcp_load_synctask_lib(lua_State *state, boolean_t sync) 423 { 424 int i; 425 zcp_synctask_info_t *zcp_synctask_funcs[] = { 426 &zcp_synctask_destroy_info, 427 &zcp_synctask_promote_info, 428 &zcp_synctask_rollback_info, 429 &zcp_synctask_snapshot_info, 430 &zcp_synctask_inherit_prop_info, 431 NULL 432 }; 433 434 lua_newtable(state); 435 436 for (i = 0; zcp_synctask_funcs[i] != NULL; i++) { 437 zcp_synctask_info_t *info = zcp_synctask_funcs[i]; 438 lua_pushlightuserdata(state, info); 439 lua_pushboolean(state, sync); 440 lua_pushcclosure(state, &zcp_synctask_wrapper, 2); 441 lua_setfield(state, -2, info->name); 442 info++; 443 } 444 445 return (1); 446 } 447