1 // SPDX-License-Identifier: CDDL-1.0 2 /* 3 * CDDL HEADER START 4 * 5 * This file and its contents are supplied under the terms of the 6 * Common Development and Distribution License ("CDDL"), version 1.0. 7 * You may only use this file in accordance with the terms of version 8 * 1.0 of the CDDL. 9 * 10 * A full copy of the text of the CDDL should have accompanied this 11 * source. A copy of the CDDL is also available via the Internet at 12 * http://www.illumos.org/license/CDDL. 13 * 14 * CDDL HEADER END 15 */ 16 17 /* 18 * Copyright (c) 2016, 2017 by Delphix. All rights reserved. 19 * Copyright (c) 2019, 2020 by Christian Schwarz. All rights reserved. 20 * Copyright 2020 Joyent, Inc. 21 */ 22 23 #include <sys/lua/lua.h> 24 #include <sys/lua/lauxlib.h> 25 26 #include <sys/zcp.h> 27 #include <sys/zcp_set.h> 28 #include <sys/dsl_dir.h> 29 #include <sys/dsl_pool.h> 30 #include <sys/dsl_prop.h> 31 #include <sys/dsl_synctask.h> 32 #include <sys/dsl_dataset.h> 33 #include <sys/dsl_bookmark.h> 34 #include <sys/dsl_destroy.h> 35 #include <sys/dmu_objset.h> 36 #include <sys/zfs_znode.h> 37 #include <sys/zfeature.h> 38 #include <sys/metaslab.h> 39 40 #define DST_AVG_BLKSHIFT 14 41 42 typedef struct zcp_inherit_prop_arg { 43 lua_State *zipa_state; 44 const char *zipa_prop; 45 dsl_props_set_arg_t zipa_dpsa; 46 } zcp_inherit_prop_arg_t; 47 48 typedef int (zcp_synctask_func_t)(lua_State *, boolean_t, nvlist_t *); 49 typedef struct zcp_synctask_info { 50 const char *name; 51 zcp_synctask_func_t *func; 52 const zcp_arg_t pargs[4]; 53 const zcp_arg_t kwargs[2]; 54 zfs_space_check_t space_check; 55 int blocks_modified; 56 } zcp_synctask_info_t; 57 58 static void 59 zcp_synctask_cleanup(void *arg) 60 { 61 fnvlist_free(arg); 62 } 63 64 /* 65 * Generic synctask interface for channel program syncfuncs. 66 * 67 * To perform some action in syncing context, we'd generally call 68 * dsl_sync_task(), but since the Lua script is already running inside a 69 * synctask we need to leave out some actions (such as acquiring the config 70 * rwlock and performing space checks). 71 * 72 * If 'sync' is false, executes a dry run and returns the error code. 73 * 74 * If we are not running in syncing context and we are not doing a dry run 75 * (meaning we are running a zfs.sync function in open-context) then we 76 * return a Lua error. 77 * 78 * This function also handles common fatal error cases for channel program 79 * library functions. If a fatal error occurs, err_dsname will be the dataset 80 * name reported in error messages, if supplied. 81 */ 82 static int 83 zcp_sync_task(lua_State *state, dsl_checkfunc_t *checkfunc, 84 dsl_syncfunc_t *syncfunc, void *arg, boolean_t sync, const char *err_dsname) 85 { 86 int err; 87 zcp_run_info_t *ri = zcp_run_info(state); 88 89 err = checkfunc(arg, ri->zri_tx); 90 if (!sync) 91 return (err); 92 93 if (!ri->zri_sync) { 94 return (luaL_error(state, "running functions from the zfs.sync " 95 "submodule requires passing sync=TRUE to " 96 "lzc_channel_program() (i.e. do not specify the \"-n\" " 97 "command line argument)")); 98 } 99 100 if (err == 0) { 101 syncfunc(arg, ri->zri_tx); 102 } else if (err == EIO) { 103 if (err_dsname != NULL) { 104 return (luaL_error(state, 105 "I/O error while accessing dataset '%s'", 106 err_dsname)); 107 } else { 108 return (luaL_error(state, 109 "I/O error while accessing dataset.")); 110 } 111 } 112 113 return (err); 114 } 115 116 117 static int zcp_synctask_destroy(lua_State *, boolean_t, nvlist_t *); 118 static const zcp_synctask_info_t zcp_synctask_destroy_info = { 119 .name = "destroy", 120 .func = zcp_synctask_destroy, 121 .pargs = { 122 {.za_name = "filesystem | snapshot", .za_lua_type = LUA_TSTRING }, 123 {NULL, 0} 124 }, 125 .kwargs = { 126 {.za_name = "defer", .za_lua_type = LUA_TBOOLEAN }, 127 {NULL, 0} 128 }, 129 .space_check = ZFS_SPACE_CHECK_DESTROY, 130 .blocks_modified = 0 131 }; 132 133 static int 134 zcp_synctask_destroy(lua_State *state, boolean_t sync, nvlist_t *err_details) 135 { 136 (void) err_details; 137 int err; 138 const char *dsname = lua_tostring(state, 1); 139 140 boolean_t issnap = (strchr(dsname, '@') != NULL); 141 142 if (!issnap && !lua_isnil(state, 2)) { 143 return (luaL_error(state, 144 "'deferred' kwarg only supported for snapshots: %s", 145 dsname)); 146 } 147 148 if (issnap) { 149 dsl_destroy_snapshot_arg_t ddsa = { 0 }; 150 ddsa.ddsa_name = dsname; 151 if (!lua_isnil(state, 2)) { 152 ddsa.ddsa_defer = lua_toboolean(state, 2); 153 } else { 154 ddsa.ddsa_defer = B_FALSE; 155 } 156 157 err = zcp_sync_task(state, dsl_destroy_snapshot_check, 158 dsl_destroy_snapshot_sync, &ddsa, sync, dsname); 159 } else { 160 dsl_destroy_head_arg_t ddha = { 0 }; 161 ddha.ddha_name = dsname; 162 163 err = zcp_sync_task(state, dsl_destroy_head_check, 164 dsl_destroy_head_sync, &ddha, sync, dsname); 165 } 166 167 return (err); 168 } 169 170 static int zcp_synctask_promote(lua_State *, boolean_t, nvlist_t *); 171 static const zcp_synctask_info_t zcp_synctask_promote_info = { 172 .name = "promote", 173 .func = zcp_synctask_promote, 174 .pargs = { 175 {.za_name = "clone", .za_lua_type = LUA_TSTRING }, 176 {NULL, 0} 177 }, 178 .kwargs = { 179 {NULL, 0} 180 }, 181 .space_check = ZFS_SPACE_CHECK_RESERVED, 182 .blocks_modified = 3 183 }; 184 185 static int 186 zcp_synctask_promote(lua_State *state, boolean_t sync, nvlist_t *err_details) 187 { 188 int err; 189 dsl_dataset_promote_arg_t ddpa = { 0 }; 190 const char *dsname = lua_tostring(state, 1); 191 zcp_run_info_t *ri = zcp_run_info(state); 192 193 ddpa.ddpa_clonename = dsname; 194 ddpa.err_ds = err_details; 195 ddpa.cr = ri->zri_cred; 196 ddpa.proc = ri->zri_proc; 197 198 /* 199 * If there was a snapshot name conflict, then err_ds will be filled 200 * with a list of conflicting snapshot names. 201 */ 202 err = zcp_sync_task(state, dsl_dataset_promote_check, 203 dsl_dataset_promote_sync, &ddpa, sync, dsname); 204 205 return (err); 206 } 207 208 static int zcp_synctask_rollback(lua_State *, boolean_t, nvlist_t *err_details); 209 static const zcp_synctask_info_t zcp_synctask_rollback_info = { 210 .name = "rollback", 211 .func = zcp_synctask_rollback, 212 .space_check = ZFS_SPACE_CHECK_RESERVED, 213 .blocks_modified = 1, 214 .pargs = { 215 {.za_name = "filesystem", .za_lua_type = LUA_TSTRING }, 216 {0, 0} 217 }, 218 .kwargs = { 219 {0, 0} 220 } 221 }; 222 223 static int 224 zcp_synctask_rollback(lua_State *state, boolean_t sync, nvlist_t *err_details) 225 { 226 int err; 227 const char *dsname = lua_tostring(state, 1); 228 dsl_dataset_rollback_arg_t ddra = { 0 }; 229 230 ddra.ddra_fsname = dsname; 231 ddra.ddra_result = err_details; 232 233 err = zcp_sync_task(state, dsl_dataset_rollback_check, 234 dsl_dataset_rollback_sync, &ddra, sync, dsname); 235 236 return (err); 237 } 238 239 static int zcp_synctask_snapshot(lua_State *, boolean_t, nvlist_t *); 240 static const zcp_synctask_info_t zcp_synctask_snapshot_info = { 241 .name = "snapshot", 242 .func = zcp_synctask_snapshot, 243 .pargs = { 244 {.za_name = "filesystem@snapname | volume@snapname", 245 .za_lua_type = LUA_TSTRING }, 246 {NULL, 0} 247 }, 248 .kwargs = { 249 {NULL, 0} 250 }, 251 .space_check = ZFS_SPACE_CHECK_NORMAL, 252 .blocks_modified = 3 253 }; 254 255 static int 256 zcp_synctask_snapshot(lua_State *state, boolean_t sync, nvlist_t *err_details) 257 { 258 (void) err_details; 259 int err; 260 dsl_dataset_snapshot_arg_t ddsa = { 0 }; 261 const char *dsname = lua_tostring(state, 1); 262 zcp_run_info_t *ri = zcp_run_info(state); 263 264 /* 265 * On old pools, the ZIL must not be active when a snapshot is created, 266 * but we can't suspend the ZIL because we're already in syncing 267 * context. 268 */ 269 if (spa_version(ri->zri_pool->dp_spa) < SPA_VERSION_FAST_SNAP) { 270 return (SET_ERROR(ENOTSUP)); 271 } 272 273 /* 274 * We only allow for a single snapshot rather than a list, so the 275 * error list output is unnecessary. 276 */ 277 ddsa.ddsa_errors = NULL; 278 ddsa.ddsa_props = NULL; 279 ddsa.ddsa_cr = ri->zri_cred; 280 ddsa.ddsa_proc = ri->zri_proc; 281 ddsa.ddsa_snaps = fnvlist_alloc(); 282 fnvlist_add_boolean(ddsa.ddsa_snaps, dsname); 283 284 zcp_cleanup_handler_t *zch = zcp_register_cleanup(state, 285 zcp_synctask_cleanup, ddsa.ddsa_snaps); 286 287 err = zcp_sync_task(state, dsl_dataset_snapshot_check, 288 dsl_dataset_snapshot_sync, &ddsa, sync, dsname); 289 290 if (err == 0) { 291 /* 292 * We may need to create a new device minor node for this 293 * dataset (if it is a zvol and the "snapdev" property is set). 294 * Save it in the nvlist so that it can be processed in open 295 * context. 296 */ 297 fnvlist_add_boolean(ri->zri_new_zvols, dsname); 298 } 299 300 zcp_deregister_cleanup(state, zch); 301 fnvlist_free(ddsa.ddsa_snaps); 302 303 return (err); 304 } 305 306 static int zcp_synctask_rename_snapshot(lua_State *, boolean_t, nvlist_t *); 307 static const zcp_synctask_info_t zcp_synctask_rename_snapshot_info = { 308 .name = "rename_snapshot", 309 .func = zcp_synctask_rename_snapshot, 310 .pargs = { 311 {.za_name = "filesystem | volume", .za_lua_type = LUA_TSTRING }, 312 {.za_name = "oldsnapname", .za_lua_type = LUA_TSTRING }, 313 {.za_name = "newsnapname", .za_lua_type = LUA_TSTRING }, 314 {NULL, 0} 315 }, 316 .space_check = ZFS_SPACE_CHECK_RESERVED, 317 .blocks_modified = 1 318 }; 319 320 static int 321 zcp_synctask_rename_snapshot(lua_State *state, boolean_t sync, 322 nvlist_t *err_details) 323 { 324 (void) err_details; 325 int err; 326 const char *fsname = lua_tostring(state, 1); 327 const char *oldsnapname = lua_tostring(state, 2); 328 const char *newsnapname = lua_tostring(state, 3); 329 330 struct dsl_dataset_rename_snapshot_arg ddrsa = { 0 }; 331 ddrsa.ddrsa_fsname = fsname; 332 ddrsa.ddrsa_oldsnapname = oldsnapname; 333 ddrsa.ddrsa_newsnapname = newsnapname; 334 ddrsa.ddrsa_recursive = B_FALSE; 335 336 err = zcp_sync_task(state, dsl_dataset_rename_snapshot_check, 337 dsl_dataset_rename_snapshot_sync, &ddrsa, sync, NULL); 338 339 return (err); 340 } 341 342 static int zcp_synctask_inherit_prop(lua_State *, boolean_t, 343 nvlist_t *err_details); 344 static const zcp_synctask_info_t zcp_synctask_inherit_prop_info = { 345 .name = "inherit", 346 .func = zcp_synctask_inherit_prop, 347 .space_check = ZFS_SPACE_CHECK_RESERVED, 348 .blocks_modified = 2, /* 2 * numprops */ 349 .pargs = { 350 { .za_name = "dataset", .za_lua_type = LUA_TSTRING }, 351 { .za_name = "property", .za_lua_type = LUA_TSTRING }, 352 { NULL, 0 } 353 }, 354 .kwargs = { 355 { NULL, 0 } 356 }, 357 }; 358 359 static int 360 zcp_synctask_inherit_prop_check(void *arg, dmu_tx_t *tx) 361 { 362 zcp_inherit_prop_arg_t *args = arg; 363 zfs_prop_t prop = zfs_name_to_prop(args->zipa_prop); 364 365 if (prop == ZPROP_USERPROP) { 366 if (zfs_prop_user(args->zipa_prop)) 367 return (0); 368 369 return (EINVAL); 370 } 371 372 if (zfs_prop_readonly(prop)) 373 return (EINVAL); 374 375 if (!zfs_prop_inheritable(prop)) 376 return (EINVAL); 377 378 return (dsl_props_set_check(&args->zipa_dpsa, tx)); 379 } 380 381 static void 382 zcp_synctask_inherit_prop_sync(void *arg, dmu_tx_t *tx) 383 { 384 zcp_inherit_prop_arg_t *args = arg; 385 dsl_props_set_arg_t *dpsa = &args->zipa_dpsa; 386 387 dsl_props_set_sync(dpsa, tx); 388 } 389 390 static int 391 zcp_synctask_inherit_prop(lua_State *state, boolean_t sync, 392 nvlist_t *err_details) 393 { 394 (void) err_details; 395 int err; 396 zcp_inherit_prop_arg_t zipa = { 0 }; 397 dsl_props_set_arg_t *dpsa = &zipa.zipa_dpsa; 398 399 const char *dsname = lua_tostring(state, 1); 400 const char *prop = lua_tostring(state, 2); 401 402 zipa.zipa_state = state; 403 zipa.zipa_prop = prop; 404 dpsa->dpsa_dsname = dsname; 405 dpsa->dpsa_source = ZPROP_SRC_INHERITED; 406 dpsa->dpsa_props = fnvlist_alloc(); 407 fnvlist_add_boolean(dpsa->dpsa_props, prop); 408 409 zcp_cleanup_handler_t *zch = zcp_register_cleanup(state, 410 zcp_synctask_cleanup, dpsa->dpsa_props); 411 412 err = zcp_sync_task(state, zcp_synctask_inherit_prop_check, 413 zcp_synctask_inherit_prop_sync, &zipa, sync, dsname); 414 415 zcp_deregister_cleanup(state, zch); 416 fnvlist_free(dpsa->dpsa_props); 417 418 return (err); 419 } 420 421 static int zcp_synctask_bookmark(lua_State *, boolean_t, nvlist_t *); 422 static const zcp_synctask_info_t zcp_synctask_bookmark_info = { 423 .name = "bookmark", 424 .func = zcp_synctask_bookmark, 425 .pargs = { 426 {.za_name = "snapshot | bookmark", .za_lua_type = LUA_TSTRING }, 427 {.za_name = "bookmark", .za_lua_type = LUA_TSTRING }, 428 {NULL, 0} 429 }, 430 .kwargs = { 431 {NULL, 0} 432 }, 433 .space_check = ZFS_SPACE_CHECK_NORMAL, 434 .blocks_modified = 1, 435 }; 436 437 static int 438 zcp_synctask_bookmark(lua_State *state, boolean_t sync, nvlist_t *err_details) 439 { 440 (void) err_details; 441 int err; 442 const char *source = lua_tostring(state, 1); 443 const char *new = lua_tostring(state, 2); 444 445 nvlist_t *bmarks = fnvlist_alloc(); 446 fnvlist_add_string(bmarks, new, source); 447 448 zcp_cleanup_handler_t *zch = zcp_register_cleanup(state, 449 zcp_synctask_cleanup, bmarks); 450 451 dsl_bookmark_create_arg_t dbca = { 452 .dbca_bmarks = bmarks, 453 .dbca_errors = NULL, 454 }; 455 err = zcp_sync_task(state, dsl_bookmark_create_check, 456 dsl_bookmark_create_sync, &dbca, sync, source); 457 458 zcp_deregister_cleanup(state, zch); 459 fnvlist_free(bmarks); 460 461 return (err); 462 } 463 464 static int zcp_synctask_set_prop(lua_State *, boolean_t, nvlist_t *err_details); 465 static const zcp_synctask_info_t zcp_synctask_set_prop_info = { 466 .name = "set_prop", 467 .func = zcp_synctask_set_prop, 468 .space_check = ZFS_SPACE_CHECK_RESERVED, 469 .blocks_modified = 2, 470 .pargs = { 471 { .za_name = "dataset", .za_lua_type = LUA_TSTRING }, 472 { .za_name = "property", .za_lua_type = LUA_TSTRING }, 473 { .za_name = "value", .za_lua_type = LUA_TSTRING }, 474 { NULL, 0 } 475 }, 476 .kwargs = { 477 { NULL, 0 } 478 } 479 }; 480 481 static int 482 zcp_synctask_set_prop(lua_State *state, boolean_t sync, nvlist_t *err_details) 483 { 484 (void) err_details; 485 int err; 486 zcp_set_prop_arg_t args = { 0 }; 487 488 const char *dsname = lua_tostring(state, 1); 489 const char *prop = lua_tostring(state, 2); 490 const char *val = lua_tostring(state, 3); 491 492 args.state = state; 493 args.dsname = dsname; 494 args.prop = prop; 495 args.val = val; 496 497 err = zcp_sync_task(state, zcp_set_prop_check, zcp_set_prop_sync, 498 &args, sync, dsname); 499 500 return (err); 501 } 502 503 static int 504 zcp_synctask_wrapper(lua_State *state) 505 { 506 int err; 507 zcp_cleanup_handler_t *zch; 508 int num_ret = 1; 509 nvlist_t *err_details = fnvlist_alloc(); 510 511 /* 512 * Make sure err_details is properly freed, even if a fatal error is 513 * thrown during the synctask. 514 */ 515 zch = zcp_register_cleanup(state, zcp_synctask_cleanup, err_details); 516 517 zcp_synctask_info_t *info = lua_touserdata(state, lua_upvalueindex(1)); 518 boolean_t sync = lua_toboolean(state, lua_upvalueindex(2)); 519 520 zcp_run_info_t *ri = zcp_run_info(state); 521 dsl_pool_t *dp = ri->zri_pool; 522 523 /* MOS space is triple-dittoed, so we multiply by 3. */ 524 uint64_t funcspace = 525 ((uint64_t)info->blocks_modified << DST_AVG_BLKSHIFT) * 3; 526 527 zcp_parse_args(state, info->name, info->pargs, info->kwargs); 528 529 err = 0; 530 if (info->space_check != ZFS_SPACE_CHECK_NONE) { 531 uint64_t quota = dsl_pool_unreserved_space(dp, 532 info->space_check); 533 uint64_t used = dsl_dir_phys(dp->dp_root_dir)->dd_used_bytes + 534 ri->zri_space_used; 535 536 if (used + funcspace > quota) { 537 err = SET_ERROR(ENOSPC); 538 } 539 } 540 541 if (err == 0) { 542 err = info->func(state, sync, err_details); 543 } 544 545 if (err == 0) { 546 ri->zri_space_used += funcspace; 547 } 548 549 lua_pushnumber(state, (lua_Number)err); 550 if (fnvlist_num_pairs(err_details) > 0) { 551 (void) zcp_nvlist_to_lua(state, err_details, NULL, 0); 552 num_ret++; 553 } 554 555 zcp_deregister_cleanup(state, zch); 556 fnvlist_free(err_details); 557 558 return (num_ret); 559 } 560 561 int 562 zcp_load_synctask_lib(lua_State *state, boolean_t sync) 563 { 564 const zcp_synctask_info_t *zcp_synctask_funcs[] = { 565 &zcp_synctask_destroy_info, 566 &zcp_synctask_promote_info, 567 &zcp_synctask_rollback_info, 568 &zcp_synctask_snapshot_info, 569 &zcp_synctask_rename_snapshot_info, 570 &zcp_synctask_inherit_prop_info, 571 &zcp_synctask_bookmark_info, 572 &zcp_synctask_set_prop_info, 573 NULL 574 }; 575 576 lua_newtable(state); 577 578 for (int i = 0; zcp_synctask_funcs[i] != NULL; i++) { 579 const zcp_synctask_info_t *info = zcp_synctask_funcs[i]; 580 lua_pushlightuserdata(state, (void *)(uintptr_t)info); 581 lua_pushboolean(state, sync); 582 lua_pushcclosure(state, &zcp_synctask_wrapper, 2); 583 lua_setfield(state, -2, info->name); 584 } 585 586 return (1); 587 } 588