1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or https://opensource.org/licenses/CDDL-1.0. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24 * Portions Copyright 2011 Martin Matuska 25 * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved. 26 * Copyright (c) 2012 Pawel Jakub Dawidek 27 * Copyright (c) 2014, 2016 Joyent, Inc. All rights reserved. 28 * Copyright 2016 Nexenta Systems, Inc. All rights reserved. 29 * Copyright (c) 2014, Joyent, Inc. All rights reserved. 30 * Copyright (c) 2011, 2024 by Delphix. All rights reserved. 31 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved. 32 * Copyright (c) 2013 Steven Hartland. All rights reserved. 33 * Copyright (c) 2014 Integros [integros.com] 34 * Copyright 2016 Toomas Soome <tsoome@me.com> 35 * Copyright (c) 2016 Actifio, Inc. All rights reserved. 36 * Copyright (c) 2018, loli10K <ezomori.nozomu@gmail.com>. All rights reserved. 37 * Copyright 2017 RackTop Systems. 38 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved. 39 * Copyright (c) 2019 Datto Inc. 40 * Copyright (c) 2019, 2020 by Christian Schwarz. All rights reserved. 41 * Copyright (c) 2019, 2021, 2023, 2024, Klara Inc. 42 * Copyright (c) 2019, Allan Jude 43 * Copyright 2024 Oxide Computer Company 44 */ 45 46 /* 47 * ZFS ioctls. 48 * 49 * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage 50 * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool. 51 * 52 * There are two ways that we handle ioctls: the legacy way where almost 53 * all of the logic is in the ioctl callback, and the new way where most 54 * of the marshalling is handled in the common entry point, zfsdev_ioctl(). 55 * 56 * Non-legacy ioctls should be registered by calling 57 * zfs_ioctl_register() from zfs_ioctl_init(). The ioctl is invoked 58 * from userland by lzc_ioctl(). 59 * 60 * The registration arguments are as follows: 61 * 62 * const char *name 63 * The name of the ioctl. This is used for history logging. If the 64 * ioctl returns successfully (the callback returns 0), and allow_log 65 * is true, then a history log entry will be recorded with the input & 66 * output nvlists. The log entry can be printed with "zpool history -i". 67 * 68 * zfs_ioc_t ioc 69 * The ioctl request number, which userland will pass to ioctl(2). 70 * We want newer versions of libzfs and libzfs_core to run against 71 * existing zfs kernel modules (i.e. a deferred reboot after an update). 72 * Therefore the ioctl numbers cannot change from release to release. 73 * 74 * zfs_secpolicy_func_t *secpolicy 75 * This function will be called before the zfs_ioc_func_t, to 76 * determine if this operation is permitted. It should return EPERM 77 * on failure, and 0 on success. Checks include determining if the 78 * dataset is visible in this zone, and if the user has either all 79 * zfs privileges in the zone (SYS_MOUNT), or has been granted permission 80 * to do this operation on this dataset with "zfs allow". 81 * 82 * zfs_ioc_namecheck_t namecheck 83 * This specifies what to expect in the zfs_cmd_t:zc_name -- a pool 84 * name, a dataset name, or nothing. If the name is not well-formed, 85 * the ioctl will fail and the callback will not be called. 86 * Therefore, the callback can assume that the name is well-formed 87 * (e.g. is null-terminated, doesn't have more than one '@' character, 88 * doesn't have invalid characters). 89 * 90 * zfs_ioc_poolcheck_t pool_check 91 * This specifies requirements on the pool state. If the pool does 92 * not meet them (is suspended or is readonly), the ioctl will fail 93 * and the callback will not be called. If any checks are specified 94 * (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME. 95 * Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED | 96 * POOL_CHECK_READONLY). 97 * 98 * zfs_ioc_key_t *nvl_keys 99 * The list of expected/allowable innvl input keys. This list is used 100 * to validate the nvlist input to the ioctl. 101 * 102 * boolean_t smush_outnvlist 103 * If smush_outnvlist is true, then the output is presumed to be a 104 * list of errors, and it will be "smushed" down to fit into the 105 * caller's buffer, by removing some entries and replacing them with a 106 * single "N_MORE_ERRORS" entry indicating how many were removed. See 107 * nvlist_smush() for details. If smush_outnvlist is false, and the 108 * outnvlist does not fit into the userland-provided buffer, then the 109 * ioctl will fail with ENOMEM. 110 * 111 * zfs_ioc_func_t *func 112 * The callback function that will perform the operation. 113 * 114 * The callback should return 0 on success, or an error number on 115 * failure. If the function fails, the userland ioctl will return -1, 116 * and errno will be set to the callback's return value. The callback 117 * will be called with the following arguments: 118 * 119 * const char *name 120 * The name of the pool or dataset to operate on, from 121 * zfs_cmd_t:zc_name. The 'namecheck' argument specifies the 122 * expected type (pool, dataset, or none). 123 * 124 * nvlist_t *innvl 125 * The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src. Or 126 * NULL if no input nvlist was provided. Changes to this nvlist are 127 * ignored. If the input nvlist could not be deserialized, the 128 * ioctl will fail and the callback will not be called. 129 * 130 * nvlist_t *outnvl 131 * The output nvlist, initially empty. The callback can fill it in, 132 * and it will be returned to userland by serializing it into 133 * zfs_cmd_t:zc_nvlist_dst. If it is non-empty, and serialization 134 * fails (e.g. because the caller didn't supply a large enough 135 * buffer), then the overall ioctl will fail. See the 136 * 'smush_nvlist' argument above for additional behaviors. 137 * 138 * There are two typical uses of the output nvlist: 139 * - To return state, e.g. property values. In this case, 140 * smush_outnvlist should be false. If the buffer was not large 141 * enough, the caller will reallocate a larger buffer and try 142 * the ioctl again. 143 * 144 * - To return multiple errors from an ioctl which makes on-disk 145 * changes. In this case, smush_outnvlist should be true. 146 * Ioctls which make on-disk modifications should generally not 147 * use the outnvl if they succeed, because the caller can not 148 * distinguish between the operation failing, and 149 * deserialization failing. 150 * 151 * IOCTL Interface Errors 152 * 153 * The following ioctl input errors can be returned: 154 * ZFS_ERR_IOC_CMD_UNAVAIL the ioctl number is not supported by kernel 155 * ZFS_ERR_IOC_ARG_UNAVAIL an input argument is not supported by kernel 156 * ZFS_ERR_IOC_ARG_REQUIRED a required input argument is missing 157 * ZFS_ERR_IOC_ARG_BADTYPE an input argument has an invalid type 158 */ 159 160 #include <sys/types.h> 161 #include <sys/param.h> 162 #include <sys/errno.h> 163 #include <sys/uio_impl.h> 164 #include <sys/file.h> 165 #include <sys/kmem.h> 166 #include <sys/cmn_err.h> 167 #include <sys/stat.h> 168 #include <sys/zfs_ioctl.h> 169 #include <sys/zfs_quota.h> 170 #include <sys/zfs_vfsops.h> 171 #include <sys/zfs_znode.h> 172 #include <sys/zap.h> 173 #include <sys/spa.h> 174 #include <sys/spa_impl.h> 175 #include <sys/vdev.h> 176 #include <sys/vdev_impl.h> 177 #include <sys/dmu.h> 178 #include <sys/dsl_dir.h> 179 #include <sys/dsl_dataset.h> 180 #include <sys/dsl_prop.h> 181 #include <sys/dsl_deleg.h> 182 #include <sys/dmu_objset.h> 183 #include <sys/dmu_impl.h> 184 #include <sys/dmu_redact.h> 185 #include <sys/dmu_tx.h> 186 #include <sys/sunddi.h> 187 #include <sys/policy.h> 188 #include <sys/zone.h> 189 #include <sys/nvpair.h> 190 #include <sys/pathname.h> 191 #include <sys/fs/zfs.h> 192 #include <sys/zfs_ctldir.h> 193 #include <sys/zfs_dir.h> 194 #include <sys/zfs_onexit.h> 195 #include <sys/zvol.h> 196 #include <sys/dsl_scan.h> 197 #include <sys/fm/util.h> 198 #include <sys/dsl_crypt.h> 199 #include <sys/rrwlock.h> 200 #include <sys/zfs_file.h> 201 202 #include <sys/dmu_recv.h> 203 #include <sys/dmu_send.h> 204 #include <sys/dmu_recv.h> 205 #include <sys/dsl_destroy.h> 206 #include <sys/dsl_bookmark.h> 207 #include <sys/dsl_userhold.h> 208 #include <sys/zfeature.h> 209 #include <sys/zcp.h> 210 #include <sys/zio_checksum.h> 211 #include <sys/vdev_removal.h> 212 #include <sys/vdev_impl.h> 213 #include <sys/vdev_initialize.h> 214 #include <sys/vdev_trim.h> 215 216 #include "zfs_namecheck.h" 217 #include "zfs_prop.h" 218 #include "zfs_deleg.h" 219 #include "zfs_comutil.h" 220 221 #include <sys/lua/lua.h> 222 #include <sys/lua/lauxlib.h> 223 #include <sys/zfs_ioctl_impl.h> 224 225 kmutex_t zfsdev_state_lock; 226 static zfsdev_state_t zfsdev_state_listhead; 227 228 /* 229 * Limit maximum nvlist size. We don't want users passing in insane values 230 * for zc->zc_nvlist_src_size, since we will need to allocate that much memory. 231 * Defaults to 0=auto which is handled by platform code. 232 */ 233 uint64_t zfs_max_nvlist_src_size = 0; 234 235 /* 236 * When logging the output nvlist of an ioctl in the on-disk history, limit 237 * the logged size to this many bytes. This must be less than DMU_MAX_ACCESS. 238 * This applies primarily to zfs_ioc_channel_program(). 239 */ 240 static uint64_t zfs_history_output_max = 1024 * 1024; 241 242 uint_t zfs_allow_log_key; 243 244 /* DATA_TYPE_ANY is used when zkey_type can vary. */ 245 #define DATA_TYPE_ANY DATA_TYPE_UNKNOWN 246 247 typedef struct zfs_ioc_vec { 248 zfs_ioc_legacy_func_t *zvec_legacy_func; 249 zfs_ioc_func_t *zvec_func; 250 zfs_secpolicy_func_t *zvec_secpolicy; 251 zfs_ioc_namecheck_t zvec_namecheck; 252 boolean_t zvec_allow_log; 253 zfs_ioc_poolcheck_t zvec_pool_check; 254 boolean_t zvec_smush_outnvlist; 255 const char *zvec_name; 256 const zfs_ioc_key_t *zvec_nvl_keys; 257 size_t zvec_nvl_key_count; 258 } zfs_ioc_vec_t; 259 260 /* This array is indexed by zfs_userquota_prop_t */ 261 static const char *userquota_perms[] = { 262 ZFS_DELEG_PERM_USERUSED, 263 ZFS_DELEG_PERM_USERQUOTA, 264 ZFS_DELEG_PERM_GROUPUSED, 265 ZFS_DELEG_PERM_GROUPQUOTA, 266 ZFS_DELEG_PERM_USEROBJUSED, 267 ZFS_DELEG_PERM_USEROBJQUOTA, 268 ZFS_DELEG_PERM_GROUPOBJUSED, 269 ZFS_DELEG_PERM_GROUPOBJQUOTA, 270 ZFS_DELEG_PERM_PROJECTUSED, 271 ZFS_DELEG_PERM_PROJECTQUOTA, 272 ZFS_DELEG_PERM_PROJECTOBJUSED, 273 ZFS_DELEG_PERM_PROJECTOBJQUOTA, 274 }; 275 276 static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc); 277 static int zfs_ioc_id_quota_upgrade(zfs_cmd_t *zc); 278 static int zfs_check_settable(const char *name, nvpair_t *property, 279 cred_t *cr); 280 static int zfs_check_clearable(const char *dataset, nvlist_t *props, 281 nvlist_t **errors); 282 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *, 283 boolean_t *); 284 int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *); 285 static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp); 286 287 static void 288 history_str_free(char *buf) 289 { 290 kmem_free(buf, HIS_MAX_RECORD_LEN); 291 } 292 293 static char * 294 history_str_get(zfs_cmd_t *zc) 295 { 296 char *buf; 297 298 if (zc->zc_history == 0) 299 return (NULL); 300 301 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP); 302 if (copyinstr((void *)(uintptr_t)zc->zc_history, 303 buf, HIS_MAX_RECORD_LEN, NULL) != 0) { 304 history_str_free(buf); 305 return (NULL); 306 } 307 308 buf[HIS_MAX_RECORD_LEN -1] = '\0'; 309 310 return (buf); 311 } 312 313 /* 314 * Return non-zero if the spa version is less than requested version. 315 */ 316 static int 317 zfs_earlier_version(const char *name, int version) 318 { 319 spa_t *spa; 320 321 if (spa_open(name, &spa, FTAG) == 0) { 322 if (spa_version(spa) < version) { 323 spa_close(spa, FTAG); 324 return (1); 325 } 326 spa_close(spa, FTAG); 327 } 328 return (0); 329 } 330 331 /* 332 * Return TRUE if the ZPL version is less than requested version. 333 */ 334 static boolean_t 335 zpl_earlier_version(const char *name, int version) 336 { 337 objset_t *os; 338 boolean_t rc = B_TRUE; 339 340 if (dmu_objset_hold(name, FTAG, &os) == 0) { 341 uint64_t zplversion; 342 343 if (dmu_objset_type(os) != DMU_OST_ZFS) { 344 dmu_objset_rele(os, FTAG); 345 return (B_TRUE); 346 } 347 /* XXX reading from non-owned objset */ 348 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0) 349 rc = zplversion < version; 350 dmu_objset_rele(os, FTAG); 351 } 352 return (rc); 353 } 354 355 static void 356 zfs_log_history(zfs_cmd_t *zc) 357 { 358 spa_t *spa; 359 char *buf; 360 361 if ((buf = history_str_get(zc)) == NULL) 362 return; 363 364 if (spa_open(zc->zc_name, &spa, FTAG) == 0) { 365 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY) 366 (void) spa_history_log(spa, buf); 367 spa_close(spa, FTAG); 368 } 369 history_str_free(buf); 370 } 371 372 /* 373 * Policy for top-level read operations (list pools). Requires no privileges, 374 * and can be used in the local zone, as there is no associated dataset. 375 */ 376 static int 377 zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 378 { 379 (void) zc, (void) innvl, (void) cr; 380 return (0); 381 } 382 383 /* 384 * Policy for dataset read operations (list children, get statistics). Requires 385 * no privileges, but must be visible in the local zone. 386 */ 387 static int 388 zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 389 { 390 (void) innvl, (void) cr; 391 if (INGLOBALZONE(curproc) || 392 zone_dataset_visible(zc->zc_name, NULL)) 393 return (0); 394 395 return (SET_ERROR(ENOENT)); 396 } 397 398 static int 399 zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr) 400 { 401 int writable = 1; 402 403 /* 404 * The dataset must be visible by this zone -- check this first 405 * so they don't see EPERM on something they shouldn't know about. 406 */ 407 if (!INGLOBALZONE(curproc) && 408 !zone_dataset_visible(dataset, &writable)) 409 return (SET_ERROR(ENOENT)); 410 411 if (INGLOBALZONE(curproc)) { 412 /* 413 * If the fs is zoned, only root can access it from the 414 * global zone. 415 */ 416 if (secpolicy_zfs(cr) && zoned) 417 return (SET_ERROR(EPERM)); 418 } else { 419 /* 420 * If we are in a local zone, the 'zoned' property must be set. 421 */ 422 if (!zoned) 423 return (SET_ERROR(EPERM)); 424 425 /* must be writable by this zone */ 426 if (!writable) 427 return (SET_ERROR(EPERM)); 428 } 429 return (0); 430 } 431 432 static int 433 zfs_dozonecheck(const char *dataset, cred_t *cr) 434 { 435 uint64_t zoned; 436 437 if (dsl_prop_get_integer(dataset, zfs_prop_to_name(ZFS_PROP_ZONED), 438 &zoned, NULL)) 439 return (SET_ERROR(ENOENT)); 440 441 return (zfs_dozonecheck_impl(dataset, zoned, cr)); 442 } 443 444 static int 445 zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr) 446 { 447 uint64_t zoned; 448 449 if (dsl_prop_get_int_ds(ds, zfs_prop_to_name(ZFS_PROP_ZONED), &zoned)) 450 return (SET_ERROR(ENOENT)); 451 452 return (zfs_dozonecheck_impl(dataset, zoned, cr)); 453 } 454 455 static int 456 zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds, 457 const char *perm, cred_t *cr) 458 { 459 int error; 460 461 error = zfs_dozonecheck_ds(name, ds, cr); 462 if (error == 0) { 463 error = secpolicy_zfs(cr); 464 if (error != 0) 465 error = dsl_deleg_access_impl(ds, perm, cr); 466 } 467 return (error); 468 } 469 470 static int 471 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr) 472 { 473 int error; 474 dsl_dataset_t *ds; 475 dsl_pool_t *dp; 476 477 /* 478 * First do a quick check for root in the global zone, which 479 * is allowed to do all write_perms. This ensures that zfs_ioc_* 480 * will get to handle nonexistent datasets. 481 */ 482 if (INGLOBALZONE(curproc) && secpolicy_zfs(cr) == 0) 483 return (0); 484 485 error = dsl_pool_hold(name, FTAG, &dp); 486 if (error != 0) 487 return (error); 488 489 error = dsl_dataset_hold(dp, name, FTAG, &ds); 490 if (error != 0) { 491 dsl_pool_rele(dp, FTAG); 492 return (error); 493 } 494 495 error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr); 496 497 dsl_dataset_rele(ds, FTAG); 498 dsl_pool_rele(dp, FTAG); 499 return (error); 500 } 501 502 /* 503 * Policy for setting the security label property. 504 * 505 * Returns 0 for success, non-zero for access and other errors. 506 */ 507 static int 508 zfs_set_slabel_policy(const char *name, const char *strval, cred_t *cr) 509 { 510 #ifdef HAVE_MLSLABEL 511 char ds_hexsl[MAXNAMELEN]; 512 bslabel_t ds_sl, new_sl; 513 boolean_t new_default = FALSE; 514 uint64_t zoned; 515 int needed_priv = -1; 516 int error; 517 518 /* First get the existing dataset label. */ 519 error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL), 520 1, sizeof (ds_hexsl), &ds_hexsl, NULL); 521 if (error != 0) 522 return (SET_ERROR(EPERM)); 523 524 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0) 525 new_default = TRUE; 526 527 /* The label must be translatable */ 528 if (!new_default && (hexstr_to_label(strval, &new_sl) != 0)) 529 return (SET_ERROR(EINVAL)); 530 531 /* 532 * In a non-global zone, disallow attempts to set a label that 533 * doesn't match that of the zone; otherwise no other checks 534 * are needed. 535 */ 536 if (!INGLOBALZONE(curproc)) { 537 if (new_default || !blequal(&new_sl, CR_SL(CRED()))) 538 return (SET_ERROR(EPERM)); 539 return (0); 540 } 541 542 /* 543 * For global-zone datasets (i.e., those whose zoned property is 544 * "off", verify that the specified new label is valid for the 545 * global zone. 546 */ 547 if (dsl_prop_get_integer(name, 548 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL)) 549 return (SET_ERROR(EPERM)); 550 if (!zoned) { 551 if (zfs_check_global_label(name, strval) != 0) 552 return (SET_ERROR(EPERM)); 553 } 554 555 /* 556 * If the existing dataset label is nondefault, check if the 557 * dataset is mounted (label cannot be changed while mounted). 558 * Get the zfsvfs_t; if there isn't one, then the dataset isn't 559 * mounted (or isn't a dataset, doesn't exist, ...). 560 */ 561 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) { 562 objset_t *os; 563 static const char *setsl_tag = "setsl_tag"; 564 565 /* 566 * Try to own the dataset; abort if there is any error, 567 * (e.g., already mounted, in use, or other error). 568 */ 569 error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE, B_TRUE, 570 setsl_tag, &os); 571 if (error != 0) 572 return (SET_ERROR(EPERM)); 573 574 dmu_objset_disown(os, B_TRUE, setsl_tag); 575 576 if (new_default) { 577 needed_priv = PRIV_FILE_DOWNGRADE_SL; 578 goto out_check; 579 } 580 581 if (hexstr_to_label(strval, &new_sl) != 0) 582 return (SET_ERROR(EPERM)); 583 584 if (blstrictdom(&ds_sl, &new_sl)) 585 needed_priv = PRIV_FILE_DOWNGRADE_SL; 586 else if (blstrictdom(&new_sl, &ds_sl)) 587 needed_priv = PRIV_FILE_UPGRADE_SL; 588 } else { 589 /* dataset currently has a default label */ 590 if (!new_default) 591 needed_priv = PRIV_FILE_UPGRADE_SL; 592 } 593 594 out_check: 595 if (needed_priv != -1) 596 return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL)); 597 return (0); 598 #else 599 return (SET_ERROR(ENOTSUP)); 600 #endif /* HAVE_MLSLABEL */ 601 } 602 603 static int 604 zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval, 605 cred_t *cr) 606 { 607 const char *strval; 608 609 /* 610 * Check permissions for special properties. 611 */ 612 switch (prop) { 613 default: 614 break; 615 case ZFS_PROP_ZONED: 616 /* 617 * Disallow setting of 'zoned' from within a local zone. 618 */ 619 if (!INGLOBALZONE(curproc)) 620 return (SET_ERROR(EPERM)); 621 break; 622 623 case ZFS_PROP_QUOTA: 624 case ZFS_PROP_FILESYSTEM_LIMIT: 625 case ZFS_PROP_SNAPSHOT_LIMIT: 626 if (!INGLOBALZONE(curproc)) { 627 uint64_t zoned; 628 char setpoint[ZFS_MAX_DATASET_NAME_LEN]; 629 /* 630 * Unprivileged users are allowed to modify the 631 * limit on things *under* (ie. contained by) 632 * the thing they own. 633 */ 634 if (dsl_prop_get_integer(dsname, 635 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, setpoint)) 636 return (SET_ERROR(EPERM)); 637 if (!zoned || strlen(dsname) <= strlen(setpoint)) 638 return (SET_ERROR(EPERM)); 639 } 640 break; 641 642 case ZFS_PROP_MLSLABEL: 643 if (!is_system_labeled()) 644 return (SET_ERROR(EPERM)); 645 646 if (nvpair_value_string(propval, &strval) == 0) { 647 int err; 648 649 err = zfs_set_slabel_policy(dsname, strval, CRED()); 650 if (err != 0) 651 return (err); 652 } 653 break; 654 } 655 656 return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr)); 657 } 658 659 static int 660 zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 661 { 662 /* 663 * permission to set permissions will be evaluated later in 664 * dsl_deleg_can_allow() 665 */ 666 (void) innvl; 667 return (zfs_dozonecheck(zc->zc_name, cr)); 668 } 669 670 static int 671 zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 672 { 673 (void) innvl; 674 return (zfs_secpolicy_write_perms(zc->zc_name, 675 ZFS_DELEG_PERM_ROLLBACK, cr)); 676 } 677 678 static int 679 zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 680 { 681 (void) innvl; 682 dsl_pool_t *dp; 683 dsl_dataset_t *ds; 684 const char *cp; 685 int error; 686 687 /* 688 * Generate the current snapshot name from the given objsetid, then 689 * use that name for the secpolicy/zone checks. 690 */ 691 cp = strchr(zc->zc_name, '@'); 692 if (cp == NULL) 693 return (SET_ERROR(EINVAL)); 694 error = dsl_pool_hold(zc->zc_name, FTAG, &dp); 695 if (error != 0) 696 return (error); 697 698 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds); 699 if (error != 0) { 700 dsl_pool_rele(dp, FTAG); 701 return (error); 702 } 703 704 dsl_dataset_name(ds, zc->zc_name); 705 706 error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds, 707 ZFS_DELEG_PERM_SEND, cr); 708 dsl_dataset_rele(ds, FTAG); 709 dsl_pool_rele(dp, FTAG); 710 711 return (error); 712 } 713 714 static int 715 zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 716 { 717 (void) innvl; 718 return (zfs_secpolicy_write_perms(zc->zc_name, 719 ZFS_DELEG_PERM_SEND, cr)); 720 } 721 722 static int 723 zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 724 { 725 (void) zc, (void) innvl, (void) cr; 726 return (SET_ERROR(ENOTSUP)); 727 } 728 729 static int 730 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 731 { 732 (void) zc, (void) innvl, (void) cr; 733 return (SET_ERROR(ENOTSUP)); 734 } 735 736 static int 737 zfs_get_parent(const char *datasetname, char *parent, int parentsize) 738 { 739 char *cp; 740 741 /* 742 * Remove the @bla or /bla from the end of the name to get the parent. 743 */ 744 (void) strlcpy(parent, datasetname, parentsize); 745 cp = strrchr(parent, '@'); 746 if (cp != NULL) { 747 cp[0] = '\0'; 748 } else { 749 cp = strrchr(parent, '/'); 750 if (cp == NULL) 751 return (SET_ERROR(ENOENT)); 752 cp[0] = '\0'; 753 } 754 755 return (0); 756 } 757 758 int 759 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr) 760 { 761 int error; 762 763 if ((error = zfs_secpolicy_write_perms(name, 764 ZFS_DELEG_PERM_MOUNT, cr)) != 0) 765 return (error); 766 767 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr)); 768 } 769 770 static int 771 zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 772 { 773 (void) innvl; 774 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr)); 775 } 776 777 /* 778 * Destroying snapshots with delegated permissions requires 779 * descendant mount and destroy permissions. 780 */ 781 static int 782 zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 783 { 784 (void) zc; 785 nvlist_t *snaps; 786 nvpair_t *pair, *nextpair; 787 int error = 0; 788 789 snaps = fnvlist_lookup_nvlist(innvl, "snaps"); 790 791 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL; 792 pair = nextpair) { 793 nextpair = nvlist_next_nvpair(snaps, pair); 794 error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr); 795 if (error == ENOENT) { 796 /* 797 * Ignore any snapshots that don't exist (we consider 798 * them "already destroyed"). Remove the name from the 799 * nvl here in case the snapshot is created between 800 * now and when we try to destroy it (in which case 801 * we don't want to destroy it since we haven't 802 * checked for permission). 803 */ 804 fnvlist_remove_nvpair(snaps, pair); 805 error = 0; 806 } 807 if (error != 0) 808 break; 809 } 810 811 return (error); 812 } 813 814 int 815 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr) 816 { 817 char parentname[ZFS_MAX_DATASET_NAME_LEN]; 818 int error; 819 820 if ((error = zfs_secpolicy_write_perms(from, 821 ZFS_DELEG_PERM_RENAME, cr)) != 0) 822 return (error); 823 824 if ((error = zfs_secpolicy_write_perms(from, 825 ZFS_DELEG_PERM_MOUNT, cr)) != 0) 826 return (error); 827 828 if ((error = zfs_get_parent(to, parentname, 829 sizeof (parentname))) != 0) 830 return (error); 831 832 if ((error = zfs_secpolicy_write_perms(parentname, 833 ZFS_DELEG_PERM_CREATE, cr)) != 0) 834 return (error); 835 836 if ((error = zfs_secpolicy_write_perms(parentname, 837 ZFS_DELEG_PERM_MOUNT, cr)) != 0) 838 return (error); 839 840 return (error); 841 } 842 843 static int 844 zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 845 { 846 (void) innvl; 847 return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr)); 848 } 849 850 static int 851 zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 852 { 853 (void) innvl; 854 dsl_pool_t *dp; 855 dsl_dataset_t *clone; 856 int error; 857 858 error = zfs_secpolicy_write_perms(zc->zc_name, 859 ZFS_DELEG_PERM_PROMOTE, cr); 860 if (error != 0) 861 return (error); 862 863 error = dsl_pool_hold(zc->zc_name, FTAG, &dp); 864 if (error != 0) 865 return (error); 866 867 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone); 868 869 if (error == 0) { 870 char parentname[ZFS_MAX_DATASET_NAME_LEN]; 871 dsl_dataset_t *origin = NULL; 872 dsl_dir_t *dd; 873 dd = clone->ds_dir; 874 875 error = dsl_dataset_hold_obj(dd->dd_pool, 876 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin); 877 if (error != 0) { 878 dsl_dataset_rele(clone, FTAG); 879 dsl_pool_rele(dp, FTAG); 880 return (error); 881 } 882 883 error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone, 884 ZFS_DELEG_PERM_MOUNT, cr); 885 886 dsl_dataset_name(origin, parentname); 887 if (error == 0) { 888 error = zfs_secpolicy_write_perms_ds(parentname, origin, 889 ZFS_DELEG_PERM_PROMOTE, cr); 890 } 891 dsl_dataset_rele(clone, FTAG); 892 dsl_dataset_rele(origin, FTAG); 893 } 894 dsl_pool_rele(dp, FTAG); 895 return (error); 896 } 897 898 static int 899 zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 900 { 901 (void) innvl; 902 int error; 903 904 if ((error = zfs_secpolicy_write_perms(zc->zc_name, 905 ZFS_DELEG_PERM_RECEIVE, cr)) != 0) 906 return (error); 907 908 if ((error = zfs_secpolicy_write_perms(zc->zc_name, 909 ZFS_DELEG_PERM_MOUNT, cr)) != 0) 910 return (error); 911 912 return (zfs_secpolicy_write_perms(zc->zc_name, 913 ZFS_DELEG_PERM_CREATE, cr)); 914 } 915 916 int 917 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr) 918 { 919 return (zfs_secpolicy_write_perms(name, 920 ZFS_DELEG_PERM_SNAPSHOT, cr)); 921 } 922 923 /* 924 * Check for permission to create each snapshot in the nvlist. 925 */ 926 static int 927 zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 928 { 929 (void) zc; 930 nvlist_t *snaps; 931 int error = 0; 932 nvpair_t *pair; 933 934 snaps = fnvlist_lookup_nvlist(innvl, "snaps"); 935 936 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL; 937 pair = nvlist_next_nvpair(snaps, pair)) { 938 char *name = (char *)nvpair_name(pair); 939 char *atp = strchr(name, '@'); 940 941 if (atp == NULL) { 942 error = SET_ERROR(EINVAL); 943 break; 944 } 945 *atp = '\0'; 946 error = zfs_secpolicy_snapshot_perms(name, cr); 947 *atp = '@'; 948 if (error != 0) 949 break; 950 } 951 return (error); 952 } 953 954 /* 955 * Check for permission to create each bookmark in the nvlist. 956 */ 957 static int 958 zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 959 { 960 (void) zc; 961 int error = 0; 962 963 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL); 964 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) { 965 char *name = (char *)nvpair_name(pair); 966 char *hashp = strchr(name, '#'); 967 968 if (hashp == NULL) { 969 error = SET_ERROR(EINVAL); 970 break; 971 } 972 *hashp = '\0'; 973 error = zfs_secpolicy_write_perms(name, 974 ZFS_DELEG_PERM_BOOKMARK, cr); 975 *hashp = '#'; 976 if (error != 0) 977 break; 978 } 979 return (error); 980 } 981 982 static int 983 zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 984 { 985 (void) zc; 986 nvpair_t *pair, *nextpair; 987 int error = 0; 988 989 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL; 990 pair = nextpair) { 991 char *name = (char *)nvpair_name(pair); 992 char *hashp = strchr(name, '#'); 993 nextpair = nvlist_next_nvpair(innvl, pair); 994 995 if (hashp == NULL) { 996 error = SET_ERROR(EINVAL); 997 break; 998 } 999 1000 *hashp = '\0'; 1001 error = zfs_secpolicy_write_perms(name, 1002 ZFS_DELEG_PERM_DESTROY, cr); 1003 *hashp = '#'; 1004 if (error == ENOENT) { 1005 /* 1006 * Ignore any filesystems that don't exist (we consider 1007 * their bookmarks "already destroyed"). Remove 1008 * the name from the nvl here in case the filesystem 1009 * is created between now and when we try to destroy 1010 * the bookmark (in which case we don't want to 1011 * destroy it since we haven't checked for permission). 1012 */ 1013 fnvlist_remove_nvpair(innvl, pair); 1014 error = 0; 1015 } 1016 if (error != 0) 1017 break; 1018 } 1019 1020 return (error); 1021 } 1022 1023 static int 1024 zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1025 { 1026 (void) zc, (void) innvl, (void) cr; 1027 /* 1028 * Even root must have a proper TSD so that we know what pool 1029 * to log to. 1030 */ 1031 if (tsd_get(zfs_allow_log_key) == NULL) 1032 return (SET_ERROR(EPERM)); 1033 return (0); 1034 } 1035 1036 static int 1037 zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1038 { 1039 char parentname[ZFS_MAX_DATASET_NAME_LEN]; 1040 int error; 1041 const char *origin; 1042 1043 if ((error = zfs_get_parent(zc->zc_name, parentname, 1044 sizeof (parentname))) != 0) 1045 return (error); 1046 1047 if (nvlist_lookup_string(innvl, "origin", &origin) == 0 && 1048 (error = zfs_secpolicy_write_perms(origin, 1049 ZFS_DELEG_PERM_CLONE, cr)) != 0) 1050 return (error); 1051 1052 if ((error = zfs_secpolicy_write_perms(parentname, 1053 ZFS_DELEG_PERM_CREATE, cr)) != 0) 1054 return (error); 1055 1056 return (zfs_secpolicy_write_perms(parentname, 1057 ZFS_DELEG_PERM_MOUNT, cr)); 1058 } 1059 1060 /* 1061 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires 1062 * SYS_CONFIG privilege, which is not available in a local zone. 1063 */ 1064 int 1065 zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1066 { 1067 (void) zc, (void) innvl; 1068 1069 if (secpolicy_sys_config(cr, B_FALSE) != 0) 1070 return (SET_ERROR(EPERM)); 1071 1072 return (0); 1073 } 1074 1075 /* 1076 * Policy for object to name lookups. 1077 */ 1078 static int 1079 zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1080 { 1081 (void) innvl; 1082 int error; 1083 1084 if (secpolicy_sys_config(cr, B_FALSE) == 0) 1085 return (0); 1086 1087 error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr); 1088 return (error); 1089 } 1090 1091 /* 1092 * Policy for fault injection. Requires all privileges. 1093 */ 1094 static int 1095 zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1096 { 1097 (void) zc, (void) innvl; 1098 return (secpolicy_zinject(cr)); 1099 } 1100 1101 static int 1102 zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1103 { 1104 (void) innvl; 1105 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value); 1106 1107 if (prop == ZPROP_USERPROP) { 1108 if (!zfs_prop_user(zc->zc_value)) 1109 return (SET_ERROR(EINVAL)); 1110 return (zfs_secpolicy_write_perms(zc->zc_name, 1111 ZFS_DELEG_PERM_USERPROP, cr)); 1112 } else { 1113 return (zfs_secpolicy_setprop(zc->zc_name, prop, 1114 NULL, cr)); 1115 } 1116 } 1117 1118 static int 1119 zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1120 { 1121 int err = zfs_secpolicy_read(zc, innvl, cr); 1122 if (err) 1123 return (err); 1124 1125 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) 1126 return (SET_ERROR(EINVAL)); 1127 1128 if (zc->zc_value[0] == 0) { 1129 /* 1130 * They are asking about a posix uid/gid. If it's 1131 * themself, allow it. 1132 */ 1133 if (zc->zc_objset_type == ZFS_PROP_USERUSED || 1134 zc->zc_objset_type == ZFS_PROP_USERQUOTA || 1135 zc->zc_objset_type == ZFS_PROP_USEROBJUSED || 1136 zc->zc_objset_type == ZFS_PROP_USEROBJQUOTA) { 1137 if (zc->zc_guid == crgetuid(cr)) 1138 return (0); 1139 } else if (zc->zc_objset_type == ZFS_PROP_GROUPUSED || 1140 zc->zc_objset_type == ZFS_PROP_GROUPQUOTA || 1141 zc->zc_objset_type == ZFS_PROP_GROUPOBJUSED || 1142 zc->zc_objset_type == ZFS_PROP_GROUPOBJQUOTA) { 1143 if (groupmember(zc->zc_guid, cr)) 1144 return (0); 1145 } 1146 /* else is for project quota/used */ 1147 } 1148 1149 return (zfs_secpolicy_write_perms(zc->zc_name, 1150 userquota_perms[zc->zc_objset_type], cr)); 1151 } 1152 1153 static int 1154 zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1155 { 1156 int err = zfs_secpolicy_read(zc, innvl, cr); 1157 if (err) 1158 return (err); 1159 1160 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) 1161 return (SET_ERROR(EINVAL)); 1162 1163 return (zfs_secpolicy_write_perms(zc->zc_name, 1164 userquota_perms[zc->zc_objset_type], cr)); 1165 } 1166 1167 static int 1168 zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1169 { 1170 (void) innvl; 1171 return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION, 1172 NULL, cr)); 1173 } 1174 1175 static int 1176 zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1177 { 1178 (void) zc; 1179 nvpair_t *pair; 1180 nvlist_t *holds; 1181 int error; 1182 1183 holds = fnvlist_lookup_nvlist(innvl, "holds"); 1184 1185 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL; 1186 pair = nvlist_next_nvpair(holds, pair)) { 1187 char fsname[ZFS_MAX_DATASET_NAME_LEN]; 1188 error = dmu_fsname(nvpair_name(pair), fsname); 1189 if (error != 0) 1190 return (error); 1191 error = zfs_secpolicy_write_perms(fsname, 1192 ZFS_DELEG_PERM_HOLD, cr); 1193 if (error != 0) 1194 return (error); 1195 } 1196 return (0); 1197 } 1198 1199 static int 1200 zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1201 { 1202 (void) zc; 1203 nvpair_t *pair; 1204 int error; 1205 1206 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL; 1207 pair = nvlist_next_nvpair(innvl, pair)) { 1208 char fsname[ZFS_MAX_DATASET_NAME_LEN]; 1209 error = dmu_fsname(nvpair_name(pair), fsname); 1210 if (error != 0) 1211 return (error); 1212 error = zfs_secpolicy_write_perms(fsname, 1213 ZFS_DELEG_PERM_RELEASE, cr); 1214 if (error != 0) 1215 return (error); 1216 } 1217 return (0); 1218 } 1219 1220 /* 1221 * Policy for allowing temporary snapshots to be taken or released 1222 */ 1223 static int 1224 zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1225 { 1226 /* 1227 * A temporary snapshot is the same as a snapshot, 1228 * hold, destroy and release all rolled into one. 1229 * Delegated diff alone is sufficient that we allow this. 1230 */ 1231 int error; 1232 1233 if (zfs_secpolicy_write_perms(zc->zc_name, 1234 ZFS_DELEG_PERM_DIFF, cr) == 0) 1235 return (0); 1236 1237 error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr); 1238 1239 if (innvl != NULL) { 1240 if (error == 0) 1241 error = zfs_secpolicy_hold(zc, innvl, cr); 1242 if (error == 0) 1243 error = zfs_secpolicy_release(zc, innvl, cr); 1244 if (error == 0) 1245 error = zfs_secpolicy_destroy(zc, innvl, cr); 1246 } 1247 return (error); 1248 } 1249 1250 static int 1251 zfs_secpolicy_load_key(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1252 { 1253 return (zfs_secpolicy_write_perms(zc->zc_name, 1254 ZFS_DELEG_PERM_LOAD_KEY, cr)); 1255 } 1256 1257 static int 1258 zfs_secpolicy_change_key(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) 1259 { 1260 return (zfs_secpolicy_write_perms(zc->zc_name, 1261 ZFS_DELEG_PERM_CHANGE_KEY, cr)); 1262 } 1263 1264 /* 1265 * Returns the nvlist as specified by the user in the zfs_cmd_t. 1266 */ 1267 static int 1268 get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp) 1269 { 1270 char *packed; 1271 int error; 1272 nvlist_t *list = NULL; 1273 1274 /* 1275 * Read in and unpack the user-supplied nvlist. 1276 */ 1277 if (size == 0) 1278 return (SET_ERROR(EINVAL)); 1279 1280 packed = vmem_alloc(size, KM_SLEEP); 1281 1282 if (ddi_copyin((void *)(uintptr_t)nvl, packed, size, iflag) != 0) { 1283 vmem_free(packed, size); 1284 return (SET_ERROR(EFAULT)); 1285 } 1286 1287 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) { 1288 vmem_free(packed, size); 1289 return (error); 1290 } 1291 1292 vmem_free(packed, size); 1293 1294 *nvp = list; 1295 return (0); 1296 } 1297 1298 /* 1299 * Reduce the size of this nvlist until it can be serialized in 'max' bytes. 1300 * Entries will be removed from the end of the nvlist, and one int32 entry 1301 * named "N_MORE_ERRORS" will be added indicating how many entries were 1302 * removed. 1303 */ 1304 static int 1305 nvlist_smush(nvlist_t *errors, size_t max) 1306 { 1307 size_t size; 1308 1309 size = fnvlist_size(errors); 1310 1311 if (size > max) { 1312 nvpair_t *more_errors; 1313 int n = 0; 1314 1315 if (max < 1024) 1316 return (SET_ERROR(ENOMEM)); 1317 1318 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0); 1319 more_errors = nvlist_prev_nvpair(errors, NULL); 1320 1321 do { 1322 nvpair_t *pair = nvlist_prev_nvpair(errors, 1323 more_errors); 1324 fnvlist_remove_nvpair(errors, pair); 1325 n++; 1326 size = fnvlist_size(errors); 1327 } while (size > max); 1328 1329 fnvlist_remove_nvpair(errors, more_errors); 1330 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n); 1331 ASSERT3U(fnvlist_size(errors), <=, max); 1332 } 1333 1334 return (0); 1335 } 1336 1337 static int 1338 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl) 1339 { 1340 char *packed = NULL; 1341 int error = 0; 1342 size_t size; 1343 1344 size = fnvlist_size(nvl); 1345 1346 if (size > zc->zc_nvlist_dst_size) { 1347 error = SET_ERROR(ENOMEM); 1348 } else { 1349 packed = fnvlist_pack(nvl, &size); 1350 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst, 1351 size, zc->zc_iflags) != 0) 1352 error = SET_ERROR(EFAULT); 1353 fnvlist_pack_free(packed, size); 1354 } 1355 1356 zc->zc_nvlist_dst_size = size; 1357 zc->zc_nvlist_dst_filled = B_TRUE; 1358 return (error); 1359 } 1360 1361 int 1362 getzfsvfs_impl(objset_t *os, zfsvfs_t **zfvp) 1363 { 1364 int error = 0; 1365 if (dmu_objset_type(os) != DMU_OST_ZFS) { 1366 return (SET_ERROR(EINVAL)); 1367 } 1368 1369 mutex_enter(&os->os_user_ptr_lock); 1370 *zfvp = dmu_objset_get_user(os); 1371 /* bump s_active only when non-zero to prevent umount race */ 1372 error = zfs_vfs_ref(zfvp); 1373 mutex_exit(&os->os_user_ptr_lock); 1374 return (error); 1375 } 1376 1377 int 1378 getzfsvfs(const char *dsname, zfsvfs_t **zfvp) 1379 { 1380 objset_t *os; 1381 int error; 1382 1383 error = dmu_objset_hold(dsname, FTAG, &os); 1384 if (error != 0) 1385 return (error); 1386 1387 error = getzfsvfs_impl(os, zfvp); 1388 dmu_objset_rele(os, FTAG); 1389 return (error); 1390 } 1391 1392 /* 1393 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which 1394 * case its z_sb will be NULL, and it will be opened as the owner. 1395 * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER, 1396 * which prevents all inode ops from running. 1397 */ 1398 static int 1399 zfsvfs_hold(const char *name, const void *tag, zfsvfs_t **zfvp, 1400 boolean_t writer) 1401 { 1402 int error = 0; 1403 1404 if (getzfsvfs(name, zfvp) != 0) 1405 error = zfsvfs_create(name, B_FALSE, zfvp); 1406 if (error == 0) { 1407 if (writer) 1408 ZFS_TEARDOWN_ENTER_WRITE(*zfvp, tag); 1409 else 1410 ZFS_TEARDOWN_ENTER_READ(*zfvp, tag); 1411 if ((*zfvp)->z_unmounted) { 1412 /* 1413 * XXX we could probably try again, since the unmounting 1414 * thread should be just about to disassociate the 1415 * objset from the zfsvfs. 1416 */ 1417 ZFS_TEARDOWN_EXIT(*zfvp, tag); 1418 return (SET_ERROR(EBUSY)); 1419 } 1420 } 1421 return (error); 1422 } 1423 1424 static void 1425 zfsvfs_rele(zfsvfs_t *zfsvfs, const void *tag) 1426 { 1427 ZFS_TEARDOWN_EXIT(zfsvfs, tag); 1428 1429 if (zfs_vfs_held(zfsvfs)) { 1430 zfs_vfs_rele(zfsvfs); 1431 } else { 1432 dmu_objset_disown(zfsvfs->z_os, B_TRUE, zfsvfs); 1433 zfsvfs_free(zfsvfs); 1434 } 1435 } 1436 1437 static int 1438 zfs_ioc_pool_create(zfs_cmd_t *zc) 1439 { 1440 int error; 1441 nvlist_t *config, *props = NULL; 1442 nvlist_t *rootprops = NULL; 1443 nvlist_t *zplprops = NULL; 1444 dsl_crypto_params_t *dcp = NULL; 1445 const char *spa_name = zc->zc_name; 1446 boolean_t unload_wkey = B_TRUE; 1447 1448 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 1449 zc->zc_iflags, &config))) 1450 return (error); 1451 1452 if (zc->zc_nvlist_src_size != 0 && (error = 1453 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 1454 zc->zc_iflags, &props))) { 1455 nvlist_free(config); 1456 return (error); 1457 } 1458 1459 if (props) { 1460 nvlist_t *nvl = NULL; 1461 nvlist_t *hidden_args = NULL; 1462 uint64_t version = SPA_VERSION; 1463 const char *tname; 1464 1465 (void) nvlist_lookup_uint64(props, 1466 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version); 1467 if (!SPA_VERSION_IS_SUPPORTED(version)) { 1468 error = SET_ERROR(EINVAL); 1469 goto pool_props_bad; 1470 } 1471 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl); 1472 if (nvl) { 1473 error = nvlist_dup(nvl, &rootprops, KM_SLEEP); 1474 if (error != 0) 1475 goto pool_props_bad; 1476 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS); 1477 } 1478 1479 (void) nvlist_lookup_nvlist(props, ZPOOL_HIDDEN_ARGS, 1480 &hidden_args); 1481 error = dsl_crypto_params_create_nvlist(DCP_CMD_NONE, 1482 rootprops, hidden_args, &dcp); 1483 if (error != 0) 1484 goto pool_props_bad; 1485 (void) nvlist_remove_all(props, ZPOOL_HIDDEN_ARGS); 1486 1487 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1488 error = zfs_fill_zplprops_root(version, rootprops, 1489 zplprops, NULL); 1490 if (error != 0) 1491 goto pool_props_bad; 1492 1493 if (nvlist_lookup_string(props, 1494 zpool_prop_to_name(ZPOOL_PROP_TNAME), &tname) == 0) 1495 spa_name = tname; 1496 } 1497 1498 error = spa_create(zc->zc_name, config, props, zplprops, dcp); 1499 1500 /* 1501 * Set the remaining root properties 1502 */ 1503 if (!error && (error = zfs_set_prop_nvlist(spa_name, 1504 ZPROP_SRC_LOCAL, rootprops, NULL)) != 0) { 1505 (void) spa_destroy(spa_name); 1506 unload_wkey = B_FALSE; /* spa_destroy() unloads wrapping keys */ 1507 } 1508 1509 pool_props_bad: 1510 nvlist_free(rootprops); 1511 nvlist_free(zplprops); 1512 nvlist_free(config); 1513 nvlist_free(props); 1514 dsl_crypto_params_free(dcp, unload_wkey && !!error); 1515 1516 return (error); 1517 } 1518 1519 static int 1520 zfs_ioc_pool_destroy(zfs_cmd_t *zc) 1521 { 1522 int error; 1523 zfs_log_history(zc); 1524 error = spa_destroy(zc->zc_name); 1525 1526 return (error); 1527 } 1528 1529 static int 1530 zfs_ioc_pool_import(zfs_cmd_t *zc) 1531 { 1532 nvlist_t *config, *props = NULL; 1533 uint64_t guid; 1534 int error; 1535 1536 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 1537 zc->zc_iflags, &config)) != 0) 1538 return (error); 1539 1540 if (zc->zc_nvlist_src_size != 0 && (error = 1541 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 1542 zc->zc_iflags, &props))) { 1543 nvlist_free(config); 1544 return (error); 1545 } 1546 1547 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 || 1548 guid != zc->zc_guid) 1549 error = SET_ERROR(EINVAL); 1550 else 1551 error = spa_import(zc->zc_name, config, props, zc->zc_cookie); 1552 1553 if (zc->zc_nvlist_dst != 0) { 1554 int err; 1555 1556 if ((err = put_nvlist(zc, config)) != 0) 1557 error = err; 1558 } 1559 1560 nvlist_free(config); 1561 nvlist_free(props); 1562 1563 return (error); 1564 } 1565 1566 static int 1567 zfs_ioc_pool_export(zfs_cmd_t *zc) 1568 { 1569 int error; 1570 boolean_t force = (boolean_t)zc->zc_cookie; 1571 boolean_t hardforce = (boolean_t)zc->zc_guid; 1572 1573 zfs_log_history(zc); 1574 error = spa_export(zc->zc_name, NULL, force, hardforce); 1575 1576 return (error); 1577 } 1578 1579 static int 1580 zfs_ioc_pool_configs(zfs_cmd_t *zc) 1581 { 1582 nvlist_t *configs; 1583 int error; 1584 1585 error = spa_all_configs(&zc->zc_cookie, &configs); 1586 if (error) 1587 return (error); 1588 1589 error = put_nvlist(zc, configs); 1590 1591 nvlist_free(configs); 1592 1593 return (error); 1594 } 1595 1596 /* 1597 * inputs: 1598 * zc_name name of the pool 1599 * 1600 * outputs: 1601 * zc_cookie real errno 1602 * zc_nvlist_dst config nvlist 1603 * zc_nvlist_dst_size size of config nvlist 1604 */ 1605 static int 1606 zfs_ioc_pool_stats(zfs_cmd_t *zc) 1607 { 1608 nvlist_t *config; 1609 int error; 1610 int ret = 0; 1611 1612 error = spa_get_stats(zc->zc_name, &config, zc->zc_value, 1613 sizeof (zc->zc_value)); 1614 1615 if (config != NULL) { 1616 ret = put_nvlist(zc, config); 1617 nvlist_free(config); 1618 1619 /* 1620 * The config may be present even if 'error' is non-zero. 1621 * In this case we return success, and preserve the real errno 1622 * in 'zc_cookie'. 1623 */ 1624 zc->zc_cookie = error; 1625 } else { 1626 ret = error; 1627 } 1628 1629 return (ret); 1630 } 1631 1632 /* 1633 * Try to import the given pool, returning pool stats as appropriate so that 1634 * user land knows which devices are available and overall pool health. 1635 */ 1636 static int 1637 zfs_ioc_pool_tryimport(zfs_cmd_t *zc) 1638 { 1639 nvlist_t *tryconfig, *config = NULL; 1640 int error; 1641 1642 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 1643 zc->zc_iflags, &tryconfig)) != 0) 1644 return (error); 1645 1646 config = spa_tryimport(tryconfig); 1647 1648 nvlist_free(tryconfig); 1649 1650 if (config == NULL) 1651 return (SET_ERROR(EINVAL)); 1652 1653 error = put_nvlist(zc, config); 1654 nvlist_free(config); 1655 1656 return (error); 1657 } 1658 1659 /* 1660 * inputs: 1661 * zc_name name of the pool 1662 * zc_cookie scan func (pool_scan_func_t) 1663 * zc_flags scrub pause/resume flag (pool_scrub_cmd_t) 1664 */ 1665 static int 1666 zfs_ioc_pool_scan(zfs_cmd_t *zc) 1667 { 1668 spa_t *spa; 1669 int error; 1670 1671 if (zc->zc_flags >= POOL_SCRUB_FLAGS_END) 1672 return (SET_ERROR(EINVAL)); 1673 1674 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1675 return (error); 1676 1677 if (zc->zc_flags == POOL_SCRUB_PAUSE) 1678 error = spa_scrub_pause_resume(spa, POOL_SCRUB_PAUSE); 1679 else if (zc->zc_cookie == POOL_SCAN_NONE) 1680 error = spa_scan_stop(spa); 1681 else 1682 error = spa_scan(spa, zc->zc_cookie); 1683 1684 spa_close(spa, FTAG); 1685 1686 return (error); 1687 } 1688 1689 /* 1690 * inputs: 1691 * poolname name of the pool 1692 * scan_type scan func (pool_scan_func_t) 1693 * scan_command scrub pause/resume flag (pool_scrub_cmd_t) 1694 */ 1695 static const zfs_ioc_key_t zfs_keys_pool_scrub[] = { 1696 {"scan_type", DATA_TYPE_UINT64, 0}, 1697 {"scan_command", DATA_TYPE_UINT64, 0}, 1698 }; 1699 1700 static int 1701 zfs_ioc_pool_scrub(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl) 1702 { 1703 spa_t *spa; 1704 int error; 1705 uint64_t scan_type, scan_cmd; 1706 1707 if (nvlist_lookup_uint64(innvl, "scan_type", &scan_type) != 0) 1708 return (SET_ERROR(EINVAL)); 1709 if (nvlist_lookup_uint64(innvl, "scan_command", &scan_cmd) != 0) 1710 return (SET_ERROR(EINVAL)); 1711 1712 if (scan_cmd >= POOL_SCRUB_FLAGS_END) 1713 return (SET_ERROR(EINVAL)); 1714 1715 if ((error = spa_open(poolname, &spa, FTAG)) != 0) 1716 return (error); 1717 1718 if (scan_cmd == POOL_SCRUB_PAUSE) { 1719 error = spa_scrub_pause_resume(spa, POOL_SCRUB_PAUSE); 1720 } else if (scan_type == POOL_SCAN_NONE) { 1721 error = spa_scan_stop(spa); 1722 } else { 1723 error = spa_scan(spa, scan_type); 1724 } 1725 1726 spa_close(spa, FTAG); 1727 return (error); 1728 } 1729 1730 static int 1731 zfs_ioc_pool_freeze(zfs_cmd_t *zc) 1732 { 1733 spa_t *spa; 1734 int error; 1735 1736 error = spa_open(zc->zc_name, &spa, FTAG); 1737 if (error == 0) { 1738 spa_freeze(spa); 1739 spa_close(spa, FTAG); 1740 } 1741 return (error); 1742 } 1743 1744 static int 1745 zfs_ioc_pool_upgrade(zfs_cmd_t *zc) 1746 { 1747 spa_t *spa; 1748 int error; 1749 1750 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1751 return (error); 1752 1753 if (zc->zc_cookie < spa_version(spa) || 1754 !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) { 1755 spa_close(spa, FTAG); 1756 return (SET_ERROR(EINVAL)); 1757 } 1758 1759 spa_upgrade(spa, zc->zc_cookie); 1760 spa_close(spa, FTAG); 1761 1762 return (error); 1763 } 1764 1765 static int 1766 zfs_ioc_pool_get_history(zfs_cmd_t *zc) 1767 { 1768 spa_t *spa; 1769 char *hist_buf; 1770 uint64_t size; 1771 int error; 1772 1773 if ((size = zc->zc_history_len) == 0) 1774 return (SET_ERROR(EINVAL)); 1775 1776 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1777 return (error); 1778 1779 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) { 1780 spa_close(spa, FTAG); 1781 return (SET_ERROR(ENOTSUP)); 1782 } 1783 1784 hist_buf = vmem_alloc(size, KM_SLEEP); 1785 if ((error = spa_history_get(spa, &zc->zc_history_offset, 1786 &zc->zc_history_len, hist_buf)) == 0) { 1787 error = ddi_copyout(hist_buf, 1788 (void *)(uintptr_t)zc->zc_history, 1789 zc->zc_history_len, zc->zc_iflags); 1790 } 1791 1792 spa_close(spa, FTAG); 1793 vmem_free(hist_buf, size); 1794 return (error); 1795 } 1796 1797 /* 1798 * inputs: 1799 * zc_nvlist_src nvlist optionally containing ZPOOL_REGUID_GUID 1800 * zc_nvlist_src_size size of the nvlist 1801 */ 1802 static int 1803 zfs_ioc_pool_reguid(zfs_cmd_t *zc) 1804 { 1805 uint64_t *guidp = NULL; 1806 nvlist_t *props = NULL; 1807 spa_t *spa; 1808 uint64_t guid; 1809 int error; 1810 1811 if (zc->zc_nvlist_src_size != 0) { 1812 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 1813 zc->zc_iflags, &props); 1814 if (error != 0) 1815 return (error); 1816 1817 error = nvlist_lookup_uint64(props, ZPOOL_REGUID_GUID, &guid); 1818 if (error == 0) 1819 guidp = &guid; 1820 else if (error == ENOENT) 1821 guidp = NULL; 1822 else 1823 goto out; 1824 } 1825 1826 error = spa_open(zc->zc_name, &spa, FTAG); 1827 if (error == 0) { 1828 error = spa_change_guid(spa, guidp); 1829 spa_close(spa, FTAG); 1830 } 1831 1832 out: 1833 if (props != NULL) 1834 nvlist_free(props); 1835 1836 return (error); 1837 } 1838 1839 static int 1840 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc) 1841 { 1842 return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value)); 1843 } 1844 1845 /* 1846 * inputs: 1847 * zc_name name of filesystem 1848 * zc_obj object to find 1849 * 1850 * outputs: 1851 * zc_value name of object 1852 */ 1853 static int 1854 zfs_ioc_obj_to_path(zfs_cmd_t *zc) 1855 { 1856 objset_t *os; 1857 int error; 1858 1859 /* XXX reading from objset not owned */ 1860 if ((error = dmu_objset_hold_flags(zc->zc_name, B_TRUE, 1861 FTAG, &os)) != 0) 1862 return (error); 1863 if (dmu_objset_type(os) != DMU_OST_ZFS) { 1864 dmu_objset_rele_flags(os, B_TRUE, FTAG); 1865 return (SET_ERROR(EINVAL)); 1866 } 1867 error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value, 1868 sizeof (zc->zc_value)); 1869 dmu_objset_rele_flags(os, B_TRUE, FTAG); 1870 1871 return (error); 1872 } 1873 1874 /* 1875 * inputs: 1876 * zc_name name of filesystem 1877 * zc_obj object to find 1878 * 1879 * outputs: 1880 * zc_stat stats on object 1881 * zc_value path to object 1882 */ 1883 static int 1884 zfs_ioc_obj_to_stats(zfs_cmd_t *zc) 1885 { 1886 objset_t *os; 1887 int error; 1888 1889 /* XXX reading from objset not owned */ 1890 if ((error = dmu_objset_hold_flags(zc->zc_name, B_TRUE, 1891 FTAG, &os)) != 0) 1892 return (error); 1893 if (dmu_objset_type(os) != DMU_OST_ZFS) { 1894 dmu_objset_rele_flags(os, B_TRUE, FTAG); 1895 return (SET_ERROR(EINVAL)); 1896 } 1897 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value, 1898 sizeof (zc->zc_value)); 1899 dmu_objset_rele_flags(os, B_TRUE, FTAG); 1900 1901 return (error); 1902 } 1903 1904 static int 1905 zfs_ioc_vdev_add(zfs_cmd_t *zc) 1906 { 1907 spa_t *spa; 1908 int error; 1909 nvlist_t *config; 1910 1911 error = spa_open(zc->zc_name, &spa, FTAG); 1912 if (error != 0) 1913 return (error); 1914 1915 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 1916 zc->zc_iflags, &config); 1917 if (error == 0) { 1918 error = spa_vdev_add(spa, config, zc->zc_flags); 1919 nvlist_free(config); 1920 } 1921 spa_close(spa, FTAG); 1922 return (error); 1923 } 1924 1925 /* 1926 * inputs: 1927 * zc_name name of the pool 1928 * zc_guid guid of vdev to remove 1929 * zc_cookie cancel removal 1930 */ 1931 static int 1932 zfs_ioc_vdev_remove(zfs_cmd_t *zc) 1933 { 1934 spa_t *spa; 1935 int error; 1936 1937 error = spa_open(zc->zc_name, &spa, FTAG); 1938 if (error != 0) 1939 return (error); 1940 if (zc->zc_cookie != 0) { 1941 error = spa_vdev_remove_cancel(spa); 1942 } else { 1943 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE); 1944 } 1945 spa_close(spa, FTAG); 1946 return (error); 1947 } 1948 1949 static int 1950 zfs_ioc_vdev_set_state(zfs_cmd_t *zc) 1951 { 1952 spa_t *spa; 1953 int error; 1954 vdev_state_t newstate = VDEV_STATE_UNKNOWN; 1955 1956 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1957 return (error); 1958 switch (zc->zc_cookie) { 1959 case VDEV_STATE_ONLINE: 1960 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate); 1961 break; 1962 1963 case VDEV_STATE_OFFLINE: 1964 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj); 1965 break; 1966 1967 case VDEV_STATE_FAULTED: 1968 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED && 1969 zc->zc_obj != VDEV_AUX_EXTERNAL && 1970 zc->zc_obj != VDEV_AUX_EXTERNAL_PERSIST) 1971 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED; 1972 1973 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj); 1974 break; 1975 1976 case VDEV_STATE_DEGRADED: 1977 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED && 1978 zc->zc_obj != VDEV_AUX_EXTERNAL) 1979 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED; 1980 1981 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj); 1982 break; 1983 1984 case VDEV_STATE_REMOVED: 1985 error = vdev_remove_wanted(spa, zc->zc_guid); 1986 break; 1987 1988 default: 1989 error = SET_ERROR(EINVAL); 1990 } 1991 zc->zc_cookie = newstate; 1992 spa_close(spa, FTAG); 1993 return (error); 1994 } 1995 1996 static int 1997 zfs_ioc_vdev_attach(zfs_cmd_t *zc) 1998 { 1999 spa_t *spa; 2000 nvlist_t *config; 2001 int replacing = zc->zc_cookie; 2002 int rebuild = zc->zc_simple; 2003 int error; 2004 2005 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 2006 return (error); 2007 2008 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 2009 zc->zc_iflags, &config)) == 0) { 2010 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing, 2011 rebuild); 2012 nvlist_free(config); 2013 } 2014 2015 spa_close(spa, FTAG); 2016 return (error); 2017 } 2018 2019 static int 2020 zfs_ioc_vdev_detach(zfs_cmd_t *zc) 2021 { 2022 spa_t *spa; 2023 int error; 2024 2025 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 2026 return (error); 2027 2028 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE); 2029 2030 spa_close(spa, FTAG); 2031 return (error); 2032 } 2033 2034 static int 2035 zfs_ioc_vdev_split(zfs_cmd_t *zc) 2036 { 2037 spa_t *spa; 2038 nvlist_t *config, *props = NULL; 2039 int error; 2040 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT); 2041 2042 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 2043 return (error); 2044 2045 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 2046 zc->zc_iflags, &config))) { 2047 spa_close(spa, FTAG); 2048 return (error); 2049 } 2050 2051 if (zc->zc_nvlist_src_size != 0 && (error = 2052 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 2053 zc->zc_iflags, &props))) { 2054 spa_close(spa, FTAG); 2055 nvlist_free(config); 2056 return (error); 2057 } 2058 2059 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp); 2060 2061 spa_close(spa, FTAG); 2062 2063 nvlist_free(config); 2064 nvlist_free(props); 2065 2066 return (error); 2067 } 2068 2069 static int 2070 zfs_ioc_vdev_setpath(zfs_cmd_t *zc) 2071 { 2072 spa_t *spa; 2073 const char *path = zc->zc_value; 2074 uint64_t guid = zc->zc_guid; 2075 int error; 2076 2077 error = spa_open(zc->zc_name, &spa, FTAG); 2078 if (error != 0) 2079 return (error); 2080 2081 error = spa_vdev_setpath(spa, guid, path); 2082 spa_close(spa, FTAG); 2083 return (error); 2084 } 2085 2086 static int 2087 zfs_ioc_vdev_setfru(zfs_cmd_t *zc) 2088 { 2089 spa_t *spa; 2090 const char *fru = zc->zc_value; 2091 uint64_t guid = zc->zc_guid; 2092 int error; 2093 2094 error = spa_open(zc->zc_name, &spa, FTAG); 2095 if (error != 0) 2096 return (error); 2097 2098 error = spa_vdev_setfru(spa, guid, fru); 2099 spa_close(spa, FTAG); 2100 return (error); 2101 } 2102 2103 static int 2104 zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os) 2105 { 2106 int error = 0; 2107 nvlist_t *nv; 2108 2109 dmu_objset_fast_stat(os, &zc->zc_objset_stats); 2110 2111 if (!zc->zc_simple && zc->zc_nvlist_dst != 0 && 2112 (error = dsl_prop_get_all(os, &nv)) == 0) { 2113 dmu_objset_stats(os, nv); 2114 /* 2115 * NB: zvol_get_stats() will read the objset contents, 2116 * which we aren't supposed to do with a 2117 * DS_MODE_USER hold, because it could be 2118 * inconsistent. So this is a bit of a workaround... 2119 * XXX reading without owning 2120 */ 2121 if (!zc->zc_objset_stats.dds_inconsistent && 2122 dmu_objset_type(os) == DMU_OST_ZVOL) { 2123 error = zvol_get_stats(os, nv); 2124 if (error == EIO) { 2125 nvlist_free(nv); 2126 return (error); 2127 } 2128 VERIFY0(error); 2129 } 2130 if (error == 0) 2131 error = put_nvlist(zc, nv); 2132 nvlist_free(nv); 2133 } 2134 2135 return (error); 2136 } 2137 2138 /* 2139 * inputs: 2140 * zc_name name of filesystem 2141 * zc_nvlist_dst_size size of buffer for property nvlist 2142 * 2143 * outputs: 2144 * zc_objset_stats stats 2145 * zc_nvlist_dst property nvlist 2146 * zc_nvlist_dst_size size of property nvlist 2147 */ 2148 static int 2149 zfs_ioc_objset_stats(zfs_cmd_t *zc) 2150 { 2151 objset_t *os; 2152 int error; 2153 2154 error = dmu_objset_hold(zc->zc_name, FTAG, &os); 2155 if (error == 0) { 2156 error = zfs_ioc_objset_stats_impl(zc, os); 2157 dmu_objset_rele(os, FTAG); 2158 } 2159 2160 return (error); 2161 } 2162 2163 /* 2164 * inputs: 2165 * zc_name name of filesystem 2166 * zc_nvlist_dst_size size of buffer for property nvlist 2167 * 2168 * outputs: 2169 * zc_nvlist_dst received property nvlist 2170 * zc_nvlist_dst_size size of received property nvlist 2171 * 2172 * Gets received properties (distinct from local properties on or after 2173 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from 2174 * local property values. 2175 */ 2176 static int 2177 zfs_ioc_objset_recvd_props(zfs_cmd_t *zc) 2178 { 2179 int error = 0; 2180 nvlist_t *nv; 2181 2182 /* 2183 * Without this check, we would return local property values if the 2184 * caller has not already received properties on or after 2185 * SPA_VERSION_RECVD_PROPS. 2186 */ 2187 if (!dsl_prop_get_hasrecvd(zc->zc_name)) 2188 return (SET_ERROR(ENOTSUP)); 2189 2190 if (zc->zc_nvlist_dst != 0 && 2191 (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) { 2192 error = put_nvlist(zc, nv); 2193 nvlist_free(nv); 2194 } 2195 2196 return (error); 2197 } 2198 2199 static int 2200 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop) 2201 { 2202 uint64_t value; 2203 int error; 2204 2205 /* 2206 * zfs_get_zplprop() will either find a value or give us 2207 * the default value (if there is one). 2208 */ 2209 if ((error = zfs_get_zplprop(os, prop, &value)) != 0) 2210 return (error); 2211 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0); 2212 return (0); 2213 } 2214 2215 /* 2216 * inputs: 2217 * zc_name name of filesystem 2218 * zc_nvlist_dst_size size of buffer for zpl property nvlist 2219 * 2220 * outputs: 2221 * zc_nvlist_dst zpl property nvlist 2222 * zc_nvlist_dst_size size of zpl property nvlist 2223 */ 2224 static int 2225 zfs_ioc_objset_zplprops(zfs_cmd_t *zc) 2226 { 2227 objset_t *os; 2228 int err; 2229 2230 /* XXX reading without owning */ 2231 if ((err = dmu_objset_hold(zc->zc_name, FTAG, &os))) 2232 return (err); 2233 2234 dmu_objset_fast_stat(os, &zc->zc_objset_stats); 2235 2236 /* 2237 * NB: nvl_add_zplprop() will read the objset contents, 2238 * which we aren't supposed to do with a DS_MODE_USER 2239 * hold, because it could be inconsistent. 2240 */ 2241 if (zc->zc_nvlist_dst != 0 && 2242 !zc->zc_objset_stats.dds_inconsistent && 2243 dmu_objset_type(os) == DMU_OST_ZFS) { 2244 nvlist_t *nv; 2245 2246 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2247 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 && 2248 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 && 2249 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 && 2250 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0) 2251 err = put_nvlist(zc, nv); 2252 nvlist_free(nv); 2253 } else { 2254 err = SET_ERROR(ENOENT); 2255 } 2256 dmu_objset_rele(os, FTAG); 2257 return (err); 2258 } 2259 2260 /* 2261 * inputs: 2262 * zc_name name of filesystem 2263 * zc_cookie zap cursor 2264 * zc_nvlist_dst_size size of buffer for property nvlist 2265 * 2266 * outputs: 2267 * zc_name name of next filesystem 2268 * zc_cookie zap cursor 2269 * zc_objset_stats stats 2270 * zc_nvlist_dst property nvlist 2271 * zc_nvlist_dst_size size of property nvlist 2272 */ 2273 static int 2274 zfs_ioc_dataset_list_next(zfs_cmd_t *zc) 2275 { 2276 objset_t *os; 2277 int error; 2278 char *p; 2279 size_t orig_len = strlen(zc->zc_name); 2280 2281 top: 2282 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os))) { 2283 if (error == ENOENT) 2284 error = SET_ERROR(ESRCH); 2285 return (error); 2286 } 2287 2288 p = strrchr(zc->zc_name, '/'); 2289 if (p == NULL || p[1] != '\0') 2290 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name)); 2291 p = zc->zc_name + strlen(zc->zc_name); 2292 2293 do { 2294 error = dmu_dir_list_next(os, 2295 sizeof (zc->zc_name) - (p - zc->zc_name), p, 2296 NULL, &zc->zc_cookie); 2297 if (error == ENOENT) 2298 error = SET_ERROR(ESRCH); 2299 } while (error == 0 && zfs_dataset_name_hidden(zc->zc_name)); 2300 dmu_objset_rele(os, FTAG); 2301 2302 /* 2303 * If it's an internal dataset (ie. with a '$' in its name), 2304 * don't try to get stats for it, otherwise we'll return ENOENT. 2305 */ 2306 if (error == 0 && strchr(zc->zc_name, '$') == NULL) { 2307 error = zfs_ioc_objset_stats(zc); /* fill in the stats */ 2308 if (error == ENOENT) { 2309 /* We lost a race with destroy, get the next one. */ 2310 zc->zc_name[orig_len] = '\0'; 2311 goto top; 2312 } 2313 } 2314 return (error); 2315 } 2316 2317 /* 2318 * inputs: 2319 * zc_name name of filesystem 2320 * zc_cookie zap cursor 2321 * zc_nvlist_src iteration range nvlist 2322 * zc_nvlist_src_size size of iteration range nvlist 2323 * 2324 * outputs: 2325 * zc_name name of next snapshot 2326 * zc_objset_stats stats 2327 * zc_nvlist_dst property nvlist 2328 * zc_nvlist_dst_size size of property nvlist 2329 */ 2330 static int 2331 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc) 2332 { 2333 int error; 2334 objset_t *os, *ossnap; 2335 dsl_dataset_t *ds; 2336 uint64_t min_txg = 0, max_txg = 0; 2337 2338 if (zc->zc_nvlist_src_size != 0) { 2339 nvlist_t *props = NULL; 2340 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 2341 zc->zc_iflags, &props); 2342 if (error != 0) 2343 return (error); 2344 (void) nvlist_lookup_uint64(props, SNAP_ITER_MIN_TXG, 2345 &min_txg); 2346 (void) nvlist_lookup_uint64(props, SNAP_ITER_MAX_TXG, 2347 &max_txg); 2348 nvlist_free(props); 2349 } 2350 2351 error = dmu_objset_hold(zc->zc_name, FTAG, &os); 2352 if (error != 0) { 2353 return (error == ENOENT ? SET_ERROR(ESRCH) : error); 2354 } 2355 2356 /* 2357 * A dataset name of maximum length cannot have any snapshots, 2358 * so exit immediately. 2359 */ 2360 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= 2361 ZFS_MAX_DATASET_NAME_LEN) { 2362 dmu_objset_rele(os, FTAG); 2363 return (SET_ERROR(ESRCH)); 2364 } 2365 2366 while (error == 0) { 2367 if (issig()) { 2368 error = SET_ERROR(EINTR); 2369 break; 2370 } 2371 2372 error = dmu_snapshot_list_next(os, 2373 sizeof (zc->zc_name) - strlen(zc->zc_name), 2374 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, 2375 &zc->zc_cookie, NULL); 2376 if (error == ENOENT) { 2377 error = SET_ERROR(ESRCH); 2378 break; 2379 } else if (error != 0) { 2380 break; 2381 } 2382 2383 error = dsl_dataset_hold_obj(dmu_objset_pool(os), zc->zc_obj, 2384 FTAG, &ds); 2385 if (error != 0) 2386 break; 2387 2388 if ((min_txg != 0 && dsl_get_creationtxg(ds) < min_txg) || 2389 (max_txg != 0 && dsl_get_creationtxg(ds) > max_txg)) { 2390 dsl_dataset_rele(ds, FTAG); 2391 /* undo snapshot name append */ 2392 *(strchr(zc->zc_name, '@') + 1) = '\0'; 2393 /* skip snapshot */ 2394 continue; 2395 } 2396 2397 if (zc->zc_simple) { 2398 dsl_dataset_fast_stat(ds, &zc->zc_objset_stats); 2399 dsl_dataset_rele(ds, FTAG); 2400 break; 2401 } 2402 2403 if ((error = dmu_objset_from_ds(ds, &ossnap)) != 0) { 2404 dsl_dataset_rele(ds, FTAG); 2405 break; 2406 } 2407 if ((error = zfs_ioc_objset_stats_impl(zc, ossnap)) != 0) { 2408 dsl_dataset_rele(ds, FTAG); 2409 break; 2410 } 2411 dsl_dataset_rele(ds, FTAG); 2412 break; 2413 } 2414 2415 dmu_objset_rele(os, FTAG); 2416 /* if we failed, undo the @ that we tacked on to zc_name */ 2417 if (error != 0) 2418 *strchr(zc->zc_name, '@') = '\0'; 2419 return (error); 2420 } 2421 2422 static int 2423 zfs_prop_set_userquota(const char *dsname, nvpair_t *pair) 2424 { 2425 const char *propname = nvpair_name(pair); 2426 uint64_t *valary; 2427 unsigned int vallen; 2428 const char *dash, *domain; 2429 zfs_userquota_prop_t type; 2430 uint64_t rid; 2431 uint64_t quota; 2432 zfsvfs_t *zfsvfs; 2433 int err; 2434 2435 if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 2436 nvlist_t *attrs; 2437 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 2438 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 2439 &pair) != 0) 2440 return (SET_ERROR(EINVAL)); 2441 } 2442 2443 /* 2444 * A correctly constructed propname is encoded as 2445 * userquota@<rid>-<domain>. 2446 */ 2447 if ((dash = strchr(propname, '-')) == NULL || 2448 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 || 2449 vallen != 3) 2450 return (SET_ERROR(EINVAL)); 2451 2452 domain = dash + 1; 2453 type = valary[0]; 2454 rid = valary[1]; 2455 quota = valary[2]; 2456 2457 err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE); 2458 if (err == 0) { 2459 err = zfs_set_userquota(zfsvfs, type, domain, rid, quota); 2460 zfsvfs_rele(zfsvfs, FTAG); 2461 } 2462 2463 return (err); 2464 } 2465 2466 /* 2467 * If the named property is one that has a special function to set its value, 2468 * return 0 on success and a positive error code on failure; otherwise if it is 2469 * not one of the special properties handled by this function, return -1. 2470 * 2471 * XXX: It would be better for callers of the property interface if we handled 2472 * these special cases in dsl_prop.c (in the dsl layer). 2473 */ 2474 static int 2475 zfs_prop_set_special(const char *dsname, zprop_source_t source, 2476 nvpair_t *pair) 2477 { 2478 const char *propname = nvpair_name(pair); 2479 zfs_prop_t prop = zfs_name_to_prop(propname); 2480 uint64_t intval = 0; 2481 const char *strval = NULL; 2482 int err = -1; 2483 2484 if (prop == ZPROP_USERPROP) { 2485 if (zfs_prop_userquota(propname)) 2486 return (zfs_prop_set_userquota(dsname, pair)); 2487 return (-1); 2488 } 2489 2490 if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 2491 nvlist_t *attrs; 2492 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 2493 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 2494 &pair) == 0); 2495 } 2496 2497 /* all special properties are numeric except for keylocation */ 2498 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) { 2499 strval = fnvpair_value_string(pair); 2500 } else { 2501 intval = fnvpair_value_uint64(pair); 2502 } 2503 2504 switch (prop) { 2505 case ZFS_PROP_QUOTA: 2506 err = dsl_dir_set_quota(dsname, source, intval); 2507 break; 2508 case ZFS_PROP_REFQUOTA: 2509 err = dsl_dataset_set_refquota(dsname, source, intval); 2510 break; 2511 case ZFS_PROP_FILESYSTEM_LIMIT: 2512 case ZFS_PROP_SNAPSHOT_LIMIT: 2513 if (intval == UINT64_MAX) { 2514 /* clearing the limit, just do it */ 2515 err = 0; 2516 } else { 2517 err = dsl_dir_activate_fs_ss_limit(dsname); 2518 } 2519 /* 2520 * Set err to -1 to force the zfs_set_prop_nvlist code down the 2521 * default path to set the value in the nvlist. 2522 */ 2523 if (err == 0) 2524 err = -1; 2525 break; 2526 case ZFS_PROP_KEYLOCATION: 2527 err = dsl_crypto_can_set_keylocation(dsname, strval); 2528 2529 /* 2530 * Set err to -1 to force the zfs_set_prop_nvlist code down the 2531 * default path to set the value in the nvlist. 2532 */ 2533 if (err == 0) 2534 err = -1; 2535 break; 2536 case ZFS_PROP_RESERVATION: 2537 err = dsl_dir_set_reservation(dsname, source, intval); 2538 break; 2539 case ZFS_PROP_REFRESERVATION: 2540 err = dsl_dataset_set_refreservation(dsname, source, intval); 2541 break; 2542 case ZFS_PROP_COMPRESSION: 2543 err = dsl_dataset_set_compression(dsname, source, intval); 2544 /* 2545 * Set err to -1 to force the zfs_set_prop_nvlist code down the 2546 * default path to set the value in the nvlist. 2547 */ 2548 if (err == 0) 2549 err = -1; 2550 break; 2551 case ZFS_PROP_VOLSIZE: 2552 err = zvol_set_volsize(dsname, intval); 2553 break; 2554 case ZFS_PROP_VOLTHREADING: 2555 err = zvol_set_volthreading(dsname, intval); 2556 /* 2557 * Set err to -1 to force the zfs_set_prop_nvlist code down the 2558 * default path to set the value in the nvlist. 2559 */ 2560 if (err == 0) 2561 err = -1; 2562 break; 2563 case ZFS_PROP_SNAPDEV: 2564 case ZFS_PROP_VOLMODE: 2565 err = zvol_set_common(dsname, prop, source, intval); 2566 break; 2567 case ZFS_PROP_READONLY: 2568 err = zvol_set_ro(dsname, intval); 2569 /* 2570 * Set err to -1 to force the zfs_set_prop_nvlist code down the 2571 * default path to set the value in the nvlist. 2572 */ 2573 if (err == 0) 2574 err = -1; 2575 break; 2576 case ZFS_PROP_VERSION: 2577 { 2578 zfsvfs_t *zfsvfs; 2579 2580 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0) 2581 break; 2582 2583 err = zfs_set_version(zfsvfs, intval); 2584 zfsvfs_rele(zfsvfs, FTAG); 2585 2586 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) { 2587 zfs_cmd_t *zc; 2588 2589 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP); 2590 (void) strlcpy(zc->zc_name, dsname, 2591 sizeof (zc->zc_name)); 2592 (void) zfs_ioc_userspace_upgrade(zc); 2593 (void) zfs_ioc_id_quota_upgrade(zc); 2594 kmem_free(zc, sizeof (zfs_cmd_t)); 2595 } 2596 break; 2597 } 2598 default: 2599 err = -1; 2600 } 2601 2602 return (err); 2603 } 2604 2605 static boolean_t 2606 zfs_is_namespace_prop(zfs_prop_t prop) 2607 { 2608 switch (prop) { 2609 2610 case ZFS_PROP_ATIME: 2611 case ZFS_PROP_RELATIME: 2612 case ZFS_PROP_DEVICES: 2613 case ZFS_PROP_EXEC: 2614 case ZFS_PROP_SETUID: 2615 case ZFS_PROP_READONLY: 2616 case ZFS_PROP_XATTR: 2617 case ZFS_PROP_NBMAND: 2618 return (B_TRUE); 2619 2620 default: 2621 return (B_FALSE); 2622 } 2623 } 2624 2625 /* 2626 * This function is best effort. If it fails to set any of the given properties, 2627 * it continues to set as many as it can and returns the last error 2628 * encountered. If the caller provides a non-NULL errlist, it will be filled in 2629 * with the list of names of all the properties that failed along with the 2630 * corresponding error numbers. 2631 * 2632 * If every property is set successfully, zero is returned and errlist is not 2633 * modified. 2634 */ 2635 int 2636 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl, 2637 nvlist_t *errlist) 2638 { 2639 nvpair_t *pair; 2640 nvpair_t *propval; 2641 int rv = 0; 2642 int err; 2643 uint64_t intval; 2644 const char *strval; 2645 boolean_t should_update_mount_cache = B_FALSE; 2646 2647 nvlist_t *genericnvl = fnvlist_alloc(); 2648 nvlist_t *retrynvl = fnvlist_alloc(); 2649 retry: 2650 pair = NULL; 2651 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) { 2652 const char *propname = nvpair_name(pair); 2653 zfs_prop_t prop = zfs_name_to_prop(propname); 2654 err = 0; 2655 2656 /* decode the property value */ 2657 propval = pair; 2658 if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 2659 nvlist_t *attrs; 2660 attrs = fnvpair_value_nvlist(pair); 2661 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 2662 &propval) != 0) 2663 err = SET_ERROR(EINVAL); 2664 } 2665 2666 /* Validate value type */ 2667 if (err == 0 && source == ZPROP_SRC_INHERITED) { 2668 /* inherited properties are expected to be booleans */ 2669 if (nvpair_type(propval) != DATA_TYPE_BOOLEAN) 2670 err = SET_ERROR(EINVAL); 2671 } else if (err == 0 && prop == ZPROP_USERPROP) { 2672 if (zfs_prop_user(propname)) { 2673 if (nvpair_type(propval) != DATA_TYPE_STRING) 2674 err = SET_ERROR(EINVAL); 2675 } else if (zfs_prop_userquota(propname)) { 2676 if (nvpair_type(propval) != 2677 DATA_TYPE_UINT64_ARRAY) 2678 err = SET_ERROR(EINVAL); 2679 } else { 2680 err = SET_ERROR(EINVAL); 2681 } 2682 } else if (err == 0) { 2683 if (nvpair_type(propval) == DATA_TYPE_STRING) { 2684 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING) 2685 err = SET_ERROR(EINVAL); 2686 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) { 2687 const char *unused; 2688 2689 intval = fnvpair_value_uint64(propval); 2690 2691 switch (zfs_prop_get_type(prop)) { 2692 case PROP_TYPE_NUMBER: 2693 break; 2694 case PROP_TYPE_STRING: 2695 err = SET_ERROR(EINVAL); 2696 break; 2697 case PROP_TYPE_INDEX: 2698 if (zfs_prop_index_to_string(prop, 2699 intval, &unused) != 0) 2700 err = 2701 SET_ERROR(ZFS_ERR_BADPROP); 2702 break; 2703 default: 2704 cmn_err(CE_PANIC, 2705 "unknown property type"); 2706 } 2707 } else { 2708 err = SET_ERROR(EINVAL); 2709 } 2710 } 2711 2712 /* Validate permissions */ 2713 if (err == 0) 2714 err = zfs_check_settable(dsname, pair, CRED()); 2715 2716 if (err == 0) { 2717 if (source == ZPROP_SRC_INHERITED) 2718 err = -1; /* does not need special handling */ 2719 else 2720 err = zfs_prop_set_special(dsname, source, 2721 pair); 2722 if (err == -1) { 2723 /* 2724 * For better performance we build up a list of 2725 * properties to set in a single transaction. 2726 */ 2727 err = nvlist_add_nvpair(genericnvl, pair); 2728 } else if (err != 0 && nvl != retrynvl) { 2729 /* 2730 * This may be a spurious error caused by 2731 * receiving quota and reservation out of order. 2732 * Try again in a second pass. 2733 */ 2734 err = nvlist_add_nvpair(retrynvl, pair); 2735 } 2736 } 2737 2738 if (err != 0) { 2739 if (errlist != NULL) 2740 fnvlist_add_int32(errlist, propname, err); 2741 rv = err; 2742 } 2743 2744 if (zfs_is_namespace_prop(prop)) 2745 should_update_mount_cache = B_TRUE; 2746 } 2747 2748 if (nvl != retrynvl && !nvlist_empty(retrynvl)) { 2749 nvl = retrynvl; 2750 goto retry; 2751 } 2752 2753 if (nvlist_empty(genericnvl)) 2754 goto out; 2755 2756 /* 2757 * Try to set them all in one batch. 2758 */ 2759 err = dsl_props_set(dsname, source, genericnvl); 2760 if (err == 0) 2761 goto out; 2762 2763 /* 2764 * If batching fails, we still want to set as many properties as we 2765 * can, so try setting them individually. 2766 */ 2767 pair = NULL; 2768 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) { 2769 const char *propname = nvpair_name(pair); 2770 2771 propval = pair; 2772 if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 2773 nvlist_t *attrs; 2774 attrs = fnvpair_value_nvlist(pair); 2775 propval = fnvlist_lookup_nvpair(attrs, ZPROP_VALUE); 2776 } 2777 2778 if (nvpair_type(propval) == DATA_TYPE_STRING) { 2779 strval = fnvpair_value_string(propval); 2780 err = dsl_prop_set_string(dsname, propname, 2781 source, strval); 2782 } else if (nvpair_type(propval) == DATA_TYPE_BOOLEAN) { 2783 err = dsl_prop_inherit(dsname, propname, source); 2784 } else { 2785 intval = fnvpair_value_uint64(propval); 2786 err = dsl_prop_set_int(dsname, propname, source, 2787 intval); 2788 } 2789 2790 if (err != 0) { 2791 if (errlist != NULL) { 2792 fnvlist_add_int32(errlist, propname, err); 2793 } 2794 rv = err; 2795 } 2796 } 2797 2798 out: 2799 if (should_update_mount_cache) 2800 zfs_ioctl_update_mount_cache(dsname); 2801 2802 nvlist_free(genericnvl); 2803 nvlist_free(retrynvl); 2804 2805 return (rv); 2806 } 2807 2808 /* 2809 * Check that all the properties are valid user properties. 2810 */ 2811 static int 2812 zfs_check_userprops(nvlist_t *nvl) 2813 { 2814 nvpair_t *pair = NULL; 2815 2816 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) { 2817 const char *propname = nvpair_name(pair); 2818 2819 if (!zfs_prop_user(propname) || 2820 nvpair_type(pair) != DATA_TYPE_STRING) 2821 return (SET_ERROR(EINVAL)); 2822 2823 if (strlen(propname) >= ZAP_MAXNAMELEN) 2824 return (SET_ERROR(ENAMETOOLONG)); 2825 2826 if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN) 2827 return (SET_ERROR(E2BIG)); 2828 } 2829 return (0); 2830 } 2831 2832 static void 2833 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops) 2834 { 2835 nvpair_t *pair; 2836 2837 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2838 2839 pair = NULL; 2840 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) { 2841 if (nvlist_exists(skipped, nvpair_name(pair))) 2842 continue; 2843 2844 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0); 2845 } 2846 } 2847 2848 static int 2849 clear_received_props(const char *dsname, nvlist_t *props, 2850 nvlist_t *skipped) 2851 { 2852 int err = 0; 2853 nvlist_t *cleared_props = NULL; 2854 props_skip(props, skipped, &cleared_props); 2855 if (!nvlist_empty(cleared_props)) { 2856 /* 2857 * Acts on local properties until the dataset has received 2858 * properties at least once on or after SPA_VERSION_RECVD_PROPS. 2859 */ 2860 zprop_source_t flags = (ZPROP_SRC_NONE | 2861 (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0)); 2862 err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL); 2863 } 2864 nvlist_free(cleared_props); 2865 return (err); 2866 } 2867 2868 /* 2869 * inputs: 2870 * zc_name name of filesystem 2871 * zc_value name of property to set 2872 * zc_nvlist_src{_size} nvlist of properties to apply 2873 * zc_cookie received properties flag 2874 * 2875 * outputs: 2876 * zc_nvlist_dst{_size} error for each unapplied received property 2877 */ 2878 static int 2879 zfs_ioc_set_prop(zfs_cmd_t *zc) 2880 { 2881 nvlist_t *nvl; 2882 boolean_t received = zc->zc_cookie; 2883 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED : 2884 ZPROP_SRC_LOCAL); 2885 nvlist_t *errors; 2886 int error; 2887 2888 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 2889 zc->zc_iflags, &nvl)) != 0) 2890 return (error); 2891 2892 if (received) { 2893 nvlist_t *origprops; 2894 2895 if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) { 2896 (void) clear_received_props(zc->zc_name, 2897 origprops, nvl); 2898 nvlist_free(origprops); 2899 } 2900 2901 error = dsl_prop_set_hasrecvd(zc->zc_name); 2902 } 2903 2904 errors = fnvlist_alloc(); 2905 if (error == 0) 2906 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors); 2907 2908 if (zc->zc_nvlist_dst != 0 && errors != NULL) { 2909 (void) put_nvlist(zc, errors); 2910 } 2911 2912 nvlist_free(errors); 2913 nvlist_free(nvl); 2914 return (error); 2915 } 2916 2917 /* 2918 * inputs: 2919 * zc_name name of filesystem 2920 * zc_value name of property to inherit 2921 * zc_cookie revert to received value if TRUE 2922 * 2923 * outputs: none 2924 */ 2925 static int 2926 zfs_ioc_inherit_prop(zfs_cmd_t *zc) 2927 { 2928 const char *propname = zc->zc_value; 2929 zfs_prop_t prop = zfs_name_to_prop(propname); 2930 boolean_t received = zc->zc_cookie; 2931 zprop_source_t source = (received 2932 ? ZPROP_SRC_NONE /* revert to received value, if any */ 2933 : ZPROP_SRC_INHERITED); /* explicitly inherit */ 2934 nvlist_t *dummy; 2935 nvpair_t *pair; 2936 zprop_type_t type; 2937 int err; 2938 2939 if (!received) { 2940 /* 2941 * Only check this in the non-received case. We want to allow 2942 * 'inherit -S' to revert non-inheritable properties like quota 2943 * and reservation to the received or default values even though 2944 * they are not considered inheritable. 2945 */ 2946 if (prop != ZPROP_USERPROP && !zfs_prop_inheritable(prop)) 2947 return (SET_ERROR(EINVAL)); 2948 } 2949 2950 if (prop == ZPROP_USERPROP) { 2951 if (!zfs_prop_user(propname)) 2952 return (SET_ERROR(EINVAL)); 2953 2954 type = PROP_TYPE_STRING; 2955 } else if (prop == ZFS_PROP_VOLSIZE || prop == ZFS_PROP_VERSION) { 2956 return (SET_ERROR(EINVAL)); 2957 } else { 2958 type = zfs_prop_get_type(prop); 2959 } 2960 2961 /* 2962 * zfs_prop_set_special() expects properties in the form of an 2963 * nvpair with type info. 2964 */ 2965 dummy = fnvlist_alloc(); 2966 2967 switch (type) { 2968 case PROP_TYPE_STRING: 2969 VERIFY(0 == nvlist_add_string(dummy, propname, "")); 2970 break; 2971 case PROP_TYPE_NUMBER: 2972 case PROP_TYPE_INDEX: 2973 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0)); 2974 break; 2975 default: 2976 err = SET_ERROR(EINVAL); 2977 goto errout; 2978 } 2979 2980 pair = nvlist_next_nvpair(dummy, NULL); 2981 if (pair == NULL) { 2982 err = SET_ERROR(EINVAL); 2983 } else { 2984 err = zfs_prop_set_special(zc->zc_name, source, pair); 2985 if (err == -1) /* property is not "special", needs handling */ 2986 err = dsl_prop_inherit(zc->zc_name, zc->zc_value, 2987 source); 2988 } 2989 2990 errout: 2991 nvlist_free(dummy); 2992 return (err); 2993 } 2994 2995 static int 2996 zfs_ioc_pool_set_props(zfs_cmd_t *zc) 2997 { 2998 nvlist_t *props; 2999 spa_t *spa; 3000 int error; 3001 nvpair_t *pair; 3002 3003 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 3004 zc->zc_iflags, &props))) 3005 return (error); 3006 3007 /* 3008 * If the only property is the configfile, then just do a spa_lookup() 3009 * to handle the faulted case. 3010 */ 3011 pair = nvlist_next_nvpair(props, NULL); 3012 if (pair != NULL && strcmp(nvpair_name(pair), 3013 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 && 3014 nvlist_next_nvpair(props, pair) == NULL) { 3015 mutex_enter(&spa_namespace_lock); 3016 if ((spa = spa_lookup(zc->zc_name)) != NULL) { 3017 spa_configfile_set(spa, props, B_FALSE); 3018 spa_write_cachefile(spa, B_FALSE, B_TRUE, B_FALSE); 3019 } 3020 mutex_exit(&spa_namespace_lock); 3021 if (spa != NULL) { 3022 nvlist_free(props); 3023 return (0); 3024 } 3025 } 3026 3027 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) { 3028 nvlist_free(props); 3029 return (error); 3030 } 3031 3032 error = spa_prop_set(spa, props); 3033 3034 nvlist_free(props); 3035 spa_close(spa, FTAG); 3036 3037 return (error); 3038 } 3039 3040 /* 3041 * innvl: { 3042 * "get_props_names": [ "prop1", "prop2", ..., "propN" ] 3043 * } 3044 */ 3045 3046 static const zfs_ioc_key_t zfs_keys_get_props[] = { 3047 { ZPOOL_GET_PROPS_NAMES, DATA_TYPE_STRING_ARRAY, ZK_OPTIONAL }, 3048 }; 3049 3050 static int 3051 zfs_ioc_pool_get_props(const char *pool, nvlist_t *innvl, nvlist_t *outnvl) 3052 { 3053 spa_t *spa; 3054 char **props = NULL; 3055 unsigned int n_props = 0; 3056 int error; 3057 3058 if (nvlist_lookup_string_array(innvl, ZPOOL_GET_PROPS_NAMES, 3059 &props, &n_props) != 0) { 3060 props = NULL; 3061 } 3062 3063 if ((error = spa_open(pool, &spa, FTAG)) != 0) { 3064 /* 3065 * If the pool is faulted, there may be properties we can still 3066 * get (such as altroot and cachefile), so attempt to get them 3067 * anyway. 3068 */ 3069 mutex_enter(&spa_namespace_lock); 3070 if ((spa = spa_lookup(pool)) != NULL) { 3071 error = spa_prop_get(spa, outnvl); 3072 if (error == 0 && props != NULL) 3073 error = spa_prop_get_nvlist(spa, props, n_props, 3074 outnvl); 3075 } 3076 mutex_exit(&spa_namespace_lock); 3077 } else { 3078 error = spa_prop_get(spa, outnvl); 3079 if (error == 0 && props != NULL) 3080 error = spa_prop_get_nvlist(spa, props, n_props, 3081 outnvl); 3082 spa_close(spa, FTAG); 3083 } 3084 3085 return (error); 3086 } 3087 3088 /* 3089 * innvl: { 3090 * "vdevprops_set_vdev" -> guid 3091 * "vdevprops_set_props" -> { prop -> value } 3092 * } 3093 * 3094 * outnvl: propname -> error code (int32) 3095 */ 3096 static const zfs_ioc_key_t zfs_keys_vdev_set_props[] = { 3097 {ZPOOL_VDEV_PROPS_SET_VDEV, DATA_TYPE_UINT64, 0}, 3098 {ZPOOL_VDEV_PROPS_SET_PROPS, DATA_TYPE_NVLIST, 0} 3099 }; 3100 3101 static int 3102 zfs_ioc_vdev_set_props(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl) 3103 { 3104 spa_t *spa; 3105 int error; 3106 vdev_t *vd; 3107 uint64_t vdev_guid; 3108 3109 /* Early validation */ 3110 if (nvlist_lookup_uint64(innvl, ZPOOL_VDEV_PROPS_SET_VDEV, 3111 &vdev_guid) != 0) 3112 return (SET_ERROR(EINVAL)); 3113 3114 if (outnvl == NULL) 3115 return (SET_ERROR(EINVAL)); 3116 3117 if ((error = spa_open(poolname, &spa, FTAG)) != 0) 3118 return (error); 3119 3120 ASSERT(spa_writeable(spa)); 3121 3122 if ((vd = spa_lookup_by_guid(spa, vdev_guid, B_TRUE)) == NULL) { 3123 spa_close(spa, FTAG); 3124 return (SET_ERROR(ENOENT)); 3125 } 3126 3127 error = vdev_prop_set(vd, innvl, outnvl); 3128 3129 spa_close(spa, FTAG); 3130 3131 return (error); 3132 } 3133 3134 /* 3135 * innvl: { 3136 * "vdevprops_get_vdev" -> guid 3137 * (optional) "vdevprops_get_props" -> { propname -> propid } 3138 * } 3139 * 3140 * outnvl: propname -> value 3141 */ 3142 static const zfs_ioc_key_t zfs_keys_vdev_get_props[] = { 3143 {ZPOOL_VDEV_PROPS_GET_VDEV, DATA_TYPE_UINT64, 0}, 3144 {ZPOOL_VDEV_PROPS_GET_PROPS, DATA_TYPE_NVLIST, ZK_OPTIONAL} 3145 }; 3146 3147 static int 3148 zfs_ioc_vdev_get_props(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl) 3149 { 3150 spa_t *spa; 3151 int error; 3152 vdev_t *vd; 3153 uint64_t vdev_guid; 3154 3155 /* Early validation */ 3156 if (nvlist_lookup_uint64(innvl, ZPOOL_VDEV_PROPS_GET_VDEV, 3157 &vdev_guid) != 0) 3158 return (SET_ERROR(EINVAL)); 3159 3160 if (outnvl == NULL) 3161 return (SET_ERROR(EINVAL)); 3162 3163 if ((error = spa_open(poolname, &spa, FTAG)) != 0) 3164 return (error); 3165 3166 if ((vd = spa_lookup_by_guid(spa, vdev_guid, B_TRUE)) == NULL) { 3167 spa_close(spa, FTAG); 3168 return (SET_ERROR(ENOENT)); 3169 } 3170 3171 error = vdev_prop_get(vd, innvl, outnvl); 3172 3173 spa_close(spa, FTAG); 3174 3175 return (error); 3176 } 3177 3178 /* 3179 * inputs: 3180 * zc_name name of filesystem 3181 * zc_nvlist_src{_size} nvlist of delegated permissions 3182 * zc_perm_action allow/unallow flag 3183 * 3184 * outputs: none 3185 */ 3186 static int 3187 zfs_ioc_set_fsacl(zfs_cmd_t *zc) 3188 { 3189 int error; 3190 nvlist_t *fsaclnv = NULL; 3191 3192 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 3193 zc->zc_iflags, &fsaclnv)) != 0) 3194 return (error); 3195 3196 /* 3197 * Verify nvlist is constructed correctly 3198 */ 3199 if (zfs_deleg_verify_nvlist(fsaclnv) != 0) { 3200 nvlist_free(fsaclnv); 3201 return (SET_ERROR(EINVAL)); 3202 } 3203 3204 /* 3205 * If we don't have PRIV_SYS_MOUNT, then validate 3206 * that user is allowed to hand out each permission in 3207 * the nvlist(s) 3208 */ 3209 3210 error = secpolicy_zfs(CRED()); 3211 if (error != 0) { 3212 if (zc->zc_perm_action == B_FALSE) { 3213 error = dsl_deleg_can_allow(zc->zc_name, 3214 fsaclnv, CRED()); 3215 } else { 3216 error = dsl_deleg_can_unallow(zc->zc_name, 3217 fsaclnv, CRED()); 3218 } 3219 } 3220 3221 if (error == 0) 3222 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action); 3223 3224 nvlist_free(fsaclnv); 3225 return (error); 3226 } 3227 3228 /* 3229 * inputs: 3230 * zc_name name of filesystem 3231 * 3232 * outputs: 3233 * zc_nvlist_src{_size} nvlist of delegated permissions 3234 */ 3235 static int 3236 zfs_ioc_get_fsacl(zfs_cmd_t *zc) 3237 { 3238 nvlist_t *nvp; 3239 int error; 3240 3241 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) { 3242 error = put_nvlist(zc, nvp); 3243 nvlist_free(nvp); 3244 } 3245 3246 return (error); 3247 } 3248 3249 static void 3250 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 3251 { 3252 zfs_creat_t *zct = arg; 3253 3254 zfs_create_fs(os, cr, zct->zct_zplprops, tx); 3255 } 3256 3257 #define ZFS_PROP_UNDEFINED ((uint64_t)-1) 3258 3259 /* 3260 * inputs: 3261 * os parent objset pointer (NULL if root fs) 3262 * fuids_ok fuids allowed in this version of the spa? 3263 * sa_ok SAs allowed in this version of the spa? 3264 * createprops list of properties requested by creator 3265 * 3266 * outputs: 3267 * zplprops values for the zplprops we attach to the master node object 3268 * is_ci true if requested file system will be purely case-insensitive 3269 * 3270 * Determine the settings for utf8only, normalization and 3271 * casesensitivity. Specific values may have been requested by the 3272 * creator and/or we can inherit values from the parent dataset. If 3273 * the file system is of too early a vintage, a creator can not 3274 * request settings for these properties, even if the requested 3275 * setting is the default value. We don't actually want to create dsl 3276 * properties for these, so remove them from the source nvlist after 3277 * processing. 3278 */ 3279 static int 3280 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver, 3281 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops, 3282 nvlist_t *zplprops, boolean_t *is_ci) 3283 { 3284 uint64_t sense = ZFS_PROP_UNDEFINED; 3285 uint64_t norm = ZFS_PROP_UNDEFINED; 3286 uint64_t u8 = ZFS_PROP_UNDEFINED; 3287 int error; 3288 3289 ASSERT(zplprops != NULL); 3290 3291 /* parent dataset must be a filesystem */ 3292 if (os != NULL && os->os_phys->os_type != DMU_OST_ZFS) 3293 return (SET_ERROR(ZFS_ERR_WRONG_PARENT)); 3294 3295 /* 3296 * Pull out creator prop choices, if any. 3297 */ 3298 if (createprops) { 3299 (void) nvlist_lookup_uint64(createprops, 3300 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver); 3301 (void) nvlist_lookup_uint64(createprops, 3302 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm); 3303 (void) nvlist_remove_all(createprops, 3304 zfs_prop_to_name(ZFS_PROP_NORMALIZE)); 3305 (void) nvlist_lookup_uint64(createprops, 3306 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8); 3307 (void) nvlist_remove_all(createprops, 3308 zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 3309 (void) nvlist_lookup_uint64(createprops, 3310 zfs_prop_to_name(ZFS_PROP_CASE), &sense); 3311 (void) nvlist_remove_all(createprops, 3312 zfs_prop_to_name(ZFS_PROP_CASE)); 3313 } 3314 3315 /* 3316 * If the zpl version requested is whacky or the file system 3317 * or pool is version is too "young" to support normalization 3318 * and the creator tried to set a value for one of the props, 3319 * error out. 3320 */ 3321 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) || 3322 (zplver >= ZPL_VERSION_FUID && !fuids_ok) || 3323 (zplver >= ZPL_VERSION_SA && !sa_ok) || 3324 (zplver < ZPL_VERSION_NORMALIZATION && 3325 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED || 3326 sense != ZFS_PROP_UNDEFINED))) 3327 return (SET_ERROR(ENOTSUP)); 3328 3329 /* 3330 * Put the version in the zplprops 3331 */ 3332 VERIFY(nvlist_add_uint64(zplprops, 3333 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0); 3334 3335 if (norm == ZFS_PROP_UNDEFINED && 3336 (error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm)) != 0) 3337 return (error); 3338 VERIFY(nvlist_add_uint64(zplprops, 3339 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0); 3340 3341 /* 3342 * If we're normalizing, names must always be valid UTF-8 strings. 3343 */ 3344 if (norm) 3345 u8 = 1; 3346 if (u8 == ZFS_PROP_UNDEFINED && 3347 (error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8)) != 0) 3348 return (error); 3349 VERIFY(nvlist_add_uint64(zplprops, 3350 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0); 3351 3352 if (sense == ZFS_PROP_UNDEFINED && 3353 (error = zfs_get_zplprop(os, ZFS_PROP_CASE, &sense)) != 0) 3354 return (error); 3355 VERIFY(nvlist_add_uint64(zplprops, 3356 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0); 3357 3358 if (is_ci) 3359 *is_ci = (sense == ZFS_CASE_INSENSITIVE); 3360 3361 return (0); 3362 } 3363 3364 static int 3365 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops, 3366 nvlist_t *zplprops, boolean_t *is_ci) 3367 { 3368 boolean_t fuids_ok, sa_ok; 3369 uint64_t zplver = ZPL_VERSION; 3370 objset_t *os = NULL; 3371 char parentname[ZFS_MAX_DATASET_NAME_LEN]; 3372 spa_t *spa; 3373 uint64_t spa_vers; 3374 int error; 3375 3376 zfs_get_parent(dataset, parentname, sizeof (parentname)); 3377 3378 if ((error = spa_open(dataset, &spa, FTAG)) != 0) 3379 return (error); 3380 3381 spa_vers = spa_version(spa); 3382 spa_close(spa, FTAG); 3383 3384 zplver = zfs_zpl_version_map(spa_vers); 3385 fuids_ok = (zplver >= ZPL_VERSION_FUID); 3386 sa_ok = (zplver >= ZPL_VERSION_SA); 3387 3388 /* 3389 * Open parent object set so we can inherit zplprop values. 3390 */ 3391 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0) 3392 return (error); 3393 3394 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops, 3395 zplprops, is_ci); 3396 dmu_objset_rele(os, FTAG); 3397 return (error); 3398 } 3399 3400 static int 3401 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops, 3402 nvlist_t *zplprops, boolean_t *is_ci) 3403 { 3404 boolean_t fuids_ok; 3405 boolean_t sa_ok; 3406 uint64_t zplver = ZPL_VERSION; 3407 int error; 3408 3409 zplver = zfs_zpl_version_map(spa_vers); 3410 fuids_ok = (zplver >= ZPL_VERSION_FUID); 3411 sa_ok = (zplver >= ZPL_VERSION_SA); 3412 3413 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok, 3414 createprops, zplprops, is_ci); 3415 return (error); 3416 } 3417 3418 /* 3419 * innvl: { 3420 * "type" -> dmu_objset_type_t (int32) 3421 * (optional) "props" -> { prop -> value } 3422 * (optional) "hidden_args" -> { "wkeydata" -> value } 3423 * raw uint8_t array of encryption wrapping key data (32 bytes) 3424 * } 3425 * 3426 * outnvl: propname -> error code (int32) 3427 */ 3428 3429 static const zfs_ioc_key_t zfs_keys_create[] = { 3430 {"type", DATA_TYPE_INT32, 0}, 3431 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL}, 3432 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL}, 3433 }; 3434 3435 static int 3436 zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl) 3437 { 3438 int error = 0; 3439 zfs_creat_t zct = { 0 }; 3440 nvlist_t *nvprops = NULL; 3441 nvlist_t *hidden_args = NULL; 3442 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx); 3443 dmu_objset_type_t type; 3444 boolean_t is_insensitive = B_FALSE; 3445 dsl_crypto_params_t *dcp = NULL; 3446 3447 type = (dmu_objset_type_t)fnvlist_lookup_int32(innvl, "type"); 3448 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops); 3449 (void) nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args); 3450 3451 switch (type) { 3452 case DMU_OST_ZFS: 3453 cbfunc = zfs_create_cb; 3454 break; 3455 3456 case DMU_OST_ZVOL: 3457 cbfunc = zvol_create_cb; 3458 break; 3459 3460 default: 3461 cbfunc = NULL; 3462 break; 3463 } 3464 if (strchr(fsname, '@') || 3465 strchr(fsname, '%')) 3466 return (SET_ERROR(EINVAL)); 3467 3468 zct.zct_props = nvprops; 3469 3470 if (cbfunc == NULL) 3471 return (SET_ERROR(EINVAL)); 3472 3473 if (type == DMU_OST_ZVOL) { 3474 uint64_t volsize, volblocksize; 3475 3476 if (nvprops == NULL) 3477 return (SET_ERROR(EINVAL)); 3478 if (nvlist_lookup_uint64(nvprops, 3479 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0) 3480 return (SET_ERROR(EINVAL)); 3481 3482 if ((error = nvlist_lookup_uint64(nvprops, 3483 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 3484 &volblocksize)) != 0 && error != ENOENT) 3485 return (SET_ERROR(EINVAL)); 3486 3487 if (error != 0) 3488 volblocksize = zfs_prop_default_numeric( 3489 ZFS_PROP_VOLBLOCKSIZE); 3490 3491 if ((error = zvol_check_volblocksize(fsname, 3492 volblocksize)) != 0 || 3493 (error = zvol_check_volsize(volsize, 3494 volblocksize)) != 0) 3495 return (error); 3496 } else if (type == DMU_OST_ZFS) { 3497 int error; 3498 3499 /* 3500 * We have to have normalization and 3501 * case-folding flags correct when we do the 3502 * file system creation, so go figure them out 3503 * now. 3504 */ 3505 VERIFY(nvlist_alloc(&zct.zct_zplprops, 3506 NV_UNIQUE_NAME, KM_SLEEP) == 0); 3507 error = zfs_fill_zplprops(fsname, nvprops, 3508 zct.zct_zplprops, &is_insensitive); 3509 if (error != 0) { 3510 nvlist_free(zct.zct_zplprops); 3511 return (error); 3512 } 3513 } 3514 3515 error = dsl_crypto_params_create_nvlist(DCP_CMD_NONE, nvprops, 3516 hidden_args, &dcp); 3517 if (error != 0) { 3518 nvlist_free(zct.zct_zplprops); 3519 return (error); 3520 } 3521 3522 error = dmu_objset_create(fsname, type, 3523 is_insensitive ? DS_FLAG_CI_DATASET : 0, dcp, cbfunc, &zct); 3524 3525 nvlist_free(zct.zct_zplprops); 3526 dsl_crypto_params_free(dcp, !!error); 3527 3528 /* 3529 * It would be nice to do this atomically. 3530 */ 3531 if (error == 0) { 3532 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL, 3533 nvprops, outnvl); 3534 if (error != 0) { 3535 spa_t *spa; 3536 int error2; 3537 3538 /* 3539 * Volumes will return EBUSY and cannot be destroyed 3540 * until all asynchronous minor handling (e.g. from 3541 * setting the volmode property) has completed. Wait for 3542 * the spa_zvol_taskq to drain then retry. 3543 */ 3544 error2 = dsl_destroy_head(fsname); 3545 while ((error2 == EBUSY) && (type == DMU_OST_ZVOL)) { 3546 error2 = spa_open(fsname, &spa, FTAG); 3547 if (error2 == 0) { 3548 taskq_wait(spa->spa_zvol_taskq); 3549 spa_close(spa, FTAG); 3550 } 3551 error2 = dsl_destroy_head(fsname); 3552 } 3553 } 3554 } 3555 return (error); 3556 } 3557 3558 /* 3559 * innvl: { 3560 * "origin" -> name of origin snapshot 3561 * (optional) "props" -> { prop -> value } 3562 * (optional) "hidden_args" -> { "wkeydata" -> value } 3563 * raw uint8_t array of encryption wrapping key data (32 bytes) 3564 * } 3565 * 3566 * outputs: 3567 * outnvl: propname -> error code (int32) 3568 */ 3569 static const zfs_ioc_key_t zfs_keys_clone[] = { 3570 {"origin", DATA_TYPE_STRING, 0}, 3571 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL}, 3572 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL}, 3573 }; 3574 3575 static int 3576 zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl) 3577 { 3578 int error = 0; 3579 nvlist_t *nvprops = NULL; 3580 const char *origin_name; 3581 3582 origin_name = fnvlist_lookup_string(innvl, "origin"); 3583 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops); 3584 3585 if (strchr(fsname, '@') || 3586 strchr(fsname, '%')) 3587 return (SET_ERROR(EINVAL)); 3588 3589 if (dataset_namecheck(origin_name, NULL, NULL) != 0) 3590 return (SET_ERROR(EINVAL)); 3591 3592 error = dmu_objset_clone(fsname, origin_name); 3593 3594 /* 3595 * It would be nice to do this atomically. 3596 */ 3597 if (error == 0) { 3598 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL, 3599 nvprops, outnvl); 3600 if (error != 0) 3601 (void) dsl_destroy_head(fsname); 3602 } 3603 return (error); 3604 } 3605 3606 static const zfs_ioc_key_t zfs_keys_remap[] = { 3607 /* no nvl keys */ 3608 }; 3609 3610 static int 3611 zfs_ioc_remap(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl) 3612 { 3613 /* This IOCTL is no longer supported. */ 3614 (void) fsname, (void) innvl, (void) outnvl; 3615 return (0); 3616 } 3617 3618 /* 3619 * innvl: { 3620 * "snaps" -> { snapshot1, snapshot2 } 3621 * (optional) "props" -> { prop -> value (string) } 3622 * } 3623 * 3624 * outnvl: snapshot -> error code (int32) 3625 */ 3626 static const zfs_ioc_key_t zfs_keys_snapshot[] = { 3627 {"snaps", DATA_TYPE_NVLIST, 0}, 3628 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL}, 3629 }; 3630 3631 static int 3632 zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl) 3633 { 3634 nvlist_t *snaps; 3635 nvlist_t *props = NULL; 3636 int error, poollen; 3637 nvpair_t *pair; 3638 3639 (void) nvlist_lookup_nvlist(innvl, "props", &props); 3640 if (!nvlist_empty(props) && 3641 zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS)) 3642 return (SET_ERROR(ENOTSUP)); 3643 if ((error = zfs_check_userprops(props)) != 0) 3644 return (error); 3645 3646 snaps = fnvlist_lookup_nvlist(innvl, "snaps"); 3647 poollen = strlen(poolname); 3648 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL; 3649 pair = nvlist_next_nvpair(snaps, pair)) { 3650 const char *name = nvpair_name(pair); 3651 char *cp = strchr(name, '@'); 3652 3653 /* 3654 * The snap name must contain an @, and the part after it must 3655 * contain only valid characters. 3656 */ 3657 if (cp == NULL || 3658 zfs_component_namecheck(cp + 1, NULL, NULL) != 0) 3659 return (SET_ERROR(EINVAL)); 3660 3661 /* 3662 * The snap must be in the specified pool. 3663 */ 3664 if (strncmp(name, poolname, poollen) != 0 || 3665 (name[poollen] != '/' && name[poollen] != '@')) 3666 return (SET_ERROR(EXDEV)); 3667 3668 /* 3669 * Check for permission to set the properties on the fs. 3670 */ 3671 if (!nvlist_empty(props)) { 3672 *cp = '\0'; 3673 error = zfs_secpolicy_write_perms(name, 3674 ZFS_DELEG_PERM_USERPROP, CRED()); 3675 *cp = '@'; 3676 if (error != 0) 3677 return (error); 3678 } 3679 3680 /* This must be the only snap of this fs. */ 3681 for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair); 3682 pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) { 3683 if (strncmp(name, nvpair_name(pair2), cp - name + 1) 3684 == 0) { 3685 return (SET_ERROR(EXDEV)); 3686 } 3687 } 3688 } 3689 3690 error = dsl_dataset_snapshot(snaps, props, outnvl); 3691 3692 return (error); 3693 } 3694 3695 /* 3696 * innvl: "message" -> string 3697 */ 3698 static const zfs_ioc_key_t zfs_keys_log_history[] = { 3699 {"message", DATA_TYPE_STRING, 0}, 3700 }; 3701 3702 static int 3703 zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl) 3704 { 3705 (void) unused, (void) outnvl; 3706 const char *message; 3707 char *poolname; 3708 spa_t *spa; 3709 int error; 3710 3711 /* 3712 * The poolname in the ioctl is not set, we get it from the TSD, 3713 * which was set at the end of the last successful ioctl that allows 3714 * logging. The secpolicy func already checked that it is set. 3715 * Only one log ioctl is allowed after each successful ioctl, so 3716 * we clear the TSD here. 3717 */ 3718 poolname = tsd_get(zfs_allow_log_key); 3719 if (poolname == NULL) 3720 return (SET_ERROR(EINVAL)); 3721 (void) tsd_set(zfs_allow_log_key, NULL); 3722 error = spa_open(poolname, &spa, FTAG); 3723 kmem_strfree(poolname); 3724 if (error != 0) 3725 return (error); 3726 3727 message = fnvlist_lookup_string(innvl, "message"); 3728 3729 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) { 3730 spa_close(spa, FTAG); 3731 return (SET_ERROR(ENOTSUP)); 3732 } 3733 3734 error = spa_history_log(spa, message); 3735 spa_close(spa, FTAG); 3736 return (error); 3737 } 3738 3739 /* 3740 * This ioctl is used to set the bootenv configuration on the current 3741 * pool. This configuration is stored in the second padding area of the label, 3742 * and it is used by the bootloader(s) to store the bootloader and/or system 3743 * specific data. 3744 * The data is stored as nvlist data stream, and is protected by 3745 * an embedded checksum. 3746 * The version can have two possible values: 3747 * VB_RAW: nvlist should have key GRUB_ENVMAP, value DATA_TYPE_STRING. 3748 * VB_NVLIST: nvlist with arbitrary <key, value> pairs. 3749 */ 3750 static const zfs_ioc_key_t zfs_keys_set_bootenv[] = { 3751 {"version", DATA_TYPE_UINT64, 0}, 3752 {"<keys>", DATA_TYPE_ANY, ZK_OPTIONAL | ZK_WILDCARDLIST}, 3753 }; 3754 3755 static int 3756 zfs_ioc_set_bootenv(const char *name, nvlist_t *innvl, nvlist_t *outnvl) 3757 { 3758 int error; 3759 spa_t *spa; 3760 3761 if ((error = spa_open(name, &spa, FTAG)) != 0) 3762 return (error); 3763 spa_vdev_state_enter(spa, SCL_ALL); 3764 error = vdev_label_write_bootenv(spa->spa_root_vdev, innvl); 3765 (void) spa_vdev_state_exit(spa, NULL, 0); 3766 spa_close(spa, FTAG); 3767 return (error); 3768 } 3769 3770 static const zfs_ioc_key_t zfs_keys_get_bootenv[] = { 3771 /* no nvl keys */ 3772 }; 3773 3774 static int 3775 zfs_ioc_get_bootenv(const char *name, nvlist_t *innvl, nvlist_t *outnvl) 3776 { 3777 spa_t *spa; 3778 int error; 3779 3780 if ((error = spa_open(name, &spa, FTAG)) != 0) 3781 return (error); 3782 spa_vdev_state_enter(spa, SCL_ALL); 3783 error = vdev_label_read_bootenv(spa->spa_root_vdev, outnvl); 3784 (void) spa_vdev_state_exit(spa, NULL, 0); 3785 spa_close(spa, FTAG); 3786 return (error); 3787 } 3788 3789 /* 3790 * The dp_config_rwlock must not be held when calling this, because the 3791 * unmount may need to write out data. 3792 * 3793 * This function is best-effort. Callers must deal gracefully if it 3794 * remains mounted (or is remounted after this call). 3795 * 3796 * Returns 0 if the argument is not a snapshot, or it is not currently a 3797 * filesystem, or we were able to unmount it. Returns error code otherwise. 3798 */ 3799 void 3800 zfs_unmount_snap(const char *snapname) 3801 { 3802 if (strchr(snapname, '@') == NULL) 3803 return; 3804 3805 (void) zfsctl_snapshot_unmount(snapname, MNT_FORCE); 3806 } 3807 3808 static int 3809 zfs_unmount_snap_cb(const char *snapname, void *arg) 3810 { 3811 (void) arg; 3812 zfs_unmount_snap(snapname); 3813 return (0); 3814 } 3815 3816 /* 3817 * When a clone is destroyed, its origin may also need to be destroyed, 3818 * in which case it must be unmounted. This routine will do that unmount 3819 * if necessary. 3820 */ 3821 void 3822 zfs_destroy_unmount_origin(const char *fsname) 3823 { 3824 int error; 3825 objset_t *os; 3826 dsl_dataset_t *ds; 3827 3828 error = dmu_objset_hold(fsname, FTAG, &os); 3829 if (error != 0) 3830 return; 3831 ds = dmu_objset_ds(os); 3832 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) { 3833 char originname[ZFS_MAX_DATASET_NAME_LEN]; 3834 dsl_dataset_name(ds->ds_prev, originname); 3835 dmu_objset_rele(os, FTAG); 3836 zfs_unmount_snap(originname); 3837 } else { 3838 dmu_objset_rele(os, FTAG); 3839 } 3840 } 3841 3842 /* 3843 * innvl: { 3844 * "snaps" -> { snapshot1, snapshot2 } 3845 * (optional boolean) "defer" 3846 * } 3847 * 3848 * outnvl: snapshot -> error code (int32) 3849 */ 3850 static const zfs_ioc_key_t zfs_keys_destroy_snaps[] = { 3851 {"snaps", DATA_TYPE_NVLIST, 0}, 3852 {"defer", DATA_TYPE_BOOLEAN, ZK_OPTIONAL}, 3853 }; 3854 3855 static int 3856 zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl) 3857 { 3858 int poollen; 3859 nvlist_t *snaps; 3860 nvpair_t *pair; 3861 boolean_t defer; 3862 spa_t *spa; 3863 3864 snaps = fnvlist_lookup_nvlist(innvl, "snaps"); 3865 defer = nvlist_exists(innvl, "defer"); 3866 3867 poollen = strlen(poolname); 3868 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL; 3869 pair = nvlist_next_nvpair(snaps, pair)) { 3870 const char *name = nvpair_name(pair); 3871 3872 /* 3873 * The snap must be in the specified pool to prevent the 3874 * invalid removal of zvol minors below. 3875 */ 3876 if (strncmp(name, poolname, poollen) != 0 || 3877 (name[poollen] != '/' && name[poollen] != '@')) 3878 return (SET_ERROR(EXDEV)); 3879 3880 zfs_unmount_snap(nvpair_name(pair)); 3881 if (spa_open(name, &spa, FTAG) == 0) { 3882 zvol_remove_minors(spa, name, B_TRUE); 3883 spa_close(spa, FTAG); 3884 } 3885 } 3886 3887 return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl)); 3888 } 3889 3890 /* 3891 * Create bookmarks. The bookmark names are of the form <fs>#<bmark>. 3892 * All bookmarks and snapshots must be in the same pool. 3893 * dsl_bookmark_create_nvl_validate describes the nvlist schema in more detail. 3894 * 3895 * innvl: { 3896 * new_bookmark1 -> existing_snapshot, 3897 * new_bookmark2 -> existing_bookmark, 3898 * } 3899 * 3900 * outnvl: bookmark -> error code (int32) 3901 * 3902 */ 3903 static const zfs_ioc_key_t zfs_keys_bookmark[] = { 3904 {"<bookmark>...", DATA_TYPE_STRING, ZK_WILDCARDLIST}, 3905 }; 3906 3907 static int 3908 zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl) 3909 { 3910 (void) poolname; 3911 return (dsl_bookmark_create(innvl, outnvl)); 3912 } 3913 3914 /* 3915 * innvl: { 3916 * property 1, property 2, ... 3917 * } 3918 * 3919 * outnvl: { 3920 * bookmark name 1 -> { property 1, property 2, ... }, 3921 * bookmark name 2 -> { property 1, property 2, ... } 3922 * } 3923 * 3924 */ 3925 static const zfs_ioc_key_t zfs_keys_get_bookmarks[] = { 3926 {"<property>...", DATA_TYPE_BOOLEAN, ZK_WILDCARDLIST | ZK_OPTIONAL}, 3927 }; 3928 3929 static int 3930 zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl) 3931 { 3932 return (dsl_get_bookmarks(fsname, innvl, outnvl)); 3933 } 3934 3935 /* 3936 * innvl is not used. 3937 * 3938 * outnvl: { 3939 * property 1, property 2, ... 3940 * } 3941 * 3942 */ 3943 static const zfs_ioc_key_t zfs_keys_get_bookmark_props[] = { 3944 /* no nvl keys */ 3945 }; 3946 3947 static int 3948 zfs_ioc_get_bookmark_props(const char *bookmark, nvlist_t *innvl, 3949 nvlist_t *outnvl) 3950 { 3951 (void) innvl; 3952 char fsname[ZFS_MAX_DATASET_NAME_LEN]; 3953 char *bmname; 3954 3955 bmname = strchr(bookmark, '#'); 3956 if (bmname == NULL) 3957 return (SET_ERROR(EINVAL)); 3958 bmname++; 3959 3960 (void) strlcpy(fsname, bookmark, sizeof (fsname)); 3961 *(strchr(fsname, '#')) = '\0'; 3962 3963 return (dsl_get_bookmark_props(fsname, bmname, outnvl)); 3964 } 3965 3966 /* 3967 * innvl: { 3968 * bookmark name 1, bookmark name 2 3969 * } 3970 * 3971 * outnvl: bookmark -> error code (int32) 3972 * 3973 */ 3974 static const zfs_ioc_key_t zfs_keys_destroy_bookmarks[] = { 3975 {"<bookmark>...", DATA_TYPE_BOOLEAN, ZK_WILDCARDLIST}, 3976 }; 3977 3978 static int 3979 zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl, 3980 nvlist_t *outnvl) 3981 { 3982 int error, poollen; 3983 3984 poollen = strlen(poolname); 3985 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL); 3986 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) { 3987 const char *name = nvpair_name(pair); 3988 const char *cp = strchr(name, '#'); 3989 3990 /* 3991 * The bookmark name must contain an #, and the part after it 3992 * must contain only valid characters. 3993 */ 3994 if (cp == NULL || 3995 zfs_component_namecheck(cp + 1, NULL, NULL) != 0) 3996 return (SET_ERROR(EINVAL)); 3997 3998 /* 3999 * The bookmark must be in the specified pool. 4000 */ 4001 if (strncmp(name, poolname, poollen) != 0 || 4002 (name[poollen] != '/' && name[poollen] != '#')) 4003 return (SET_ERROR(EXDEV)); 4004 } 4005 4006 error = dsl_bookmark_destroy(innvl, outnvl); 4007 return (error); 4008 } 4009 4010 static const zfs_ioc_key_t zfs_keys_channel_program[] = { 4011 {"program", DATA_TYPE_STRING, 0}, 4012 {"arg", DATA_TYPE_ANY, 0}, 4013 {"sync", DATA_TYPE_BOOLEAN_VALUE, ZK_OPTIONAL}, 4014 {"instrlimit", DATA_TYPE_UINT64, ZK_OPTIONAL}, 4015 {"memlimit", DATA_TYPE_UINT64, ZK_OPTIONAL}, 4016 }; 4017 4018 static int 4019 zfs_ioc_channel_program(const char *poolname, nvlist_t *innvl, 4020 nvlist_t *outnvl) 4021 { 4022 const char *program; 4023 uint64_t instrlimit, memlimit; 4024 boolean_t sync_flag; 4025 nvpair_t *nvarg = NULL; 4026 4027 program = fnvlist_lookup_string(innvl, ZCP_ARG_PROGRAM); 4028 if (0 != nvlist_lookup_boolean_value(innvl, ZCP_ARG_SYNC, &sync_flag)) { 4029 sync_flag = B_TRUE; 4030 } 4031 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_INSTRLIMIT, &instrlimit)) { 4032 instrlimit = ZCP_DEFAULT_INSTRLIMIT; 4033 } 4034 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_MEMLIMIT, &memlimit)) { 4035 memlimit = ZCP_DEFAULT_MEMLIMIT; 4036 } 4037 nvarg = fnvlist_lookup_nvpair(innvl, ZCP_ARG_ARGLIST); 4038 4039 if (instrlimit == 0 || instrlimit > zfs_lua_max_instrlimit) 4040 return (SET_ERROR(EINVAL)); 4041 if (memlimit == 0 || memlimit > zfs_lua_max_memlimit) 4042 return (SET_ERROR(EINVAL)); 4043 4044 return (zcp_eval(poolname, program, sync_flag, instrlimit, memlimit, 4045 nvarg, outnvl)); 4046 } 4047 4048 /* 4049 * innvl: unused 4050 * outnvl: empty 4051 */ 4052 static const zfs_ioc_key_t zfs_keys_pool_checkpoint[] = { 4053 /* no nvl keys */ 4054 }; 4055 4056 static int 4057 zfs_ioc_pool_checkpoint(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl) 4058 { 4059 (void) innvl, (void) outnvl; 4060 return (spa_checkpoint(poolname)); 4061 } 4062 4063 /* 4064 * innvl: unused 4065 * outnvl: empty 4066 */ 4067 static const zfs_ioc_key_t zfs_keys_pool_discard_checkpoint[] = { 4068 /* no nvl keys */ 4069 }; 4070 4071 static int 4072 zfs_ioc_pool_discard_checkpoint(const char *poolname, nvlist_t *innvl, 4073 nvlist_t *outnvl) 4074 { 4075 (void) innvl, (void) outnvl; 4076 return (spa_checkpoint_discard(poolname)); 4077 } 4078 4079 /* 4080 * Loads specific types of data for the given pool 4081 * 4082 * innvl: { 4083 * "prefetch_type" -> int32_t 4084 * } 4085 * 4086 * outnvl: empty 4087 */ 4088 static const zfs_ioc_key_t zfs_keys_pool_prefetch[] = { 4089 {ZPOOL_PREFETCH_TYPE, DATA_TYPE_INT32, 0}, 4090 }; 4091 4092 static int 4093 zfs_ioc_pool_prefetch(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl) 4094 { 4095 (void) outnvl; 4096 4097 int error; 4098 spa_t *spa; 4099 int32_t type; 4100 4101 /* 4102 * Currently, only ZPOOL_PREFETCH_DDT is supported 4103 */ 4104 if (nvlist_lookup_int32(innvl, ZPOOL_PREFETCH_TYPE, &type) != 0 || 4105 type != ZPOOL_PREFETCH_DDT) { 4106 return (EINVAL); 4107 } 4108 4109 error = spa_open(poolname, &spa, FTAG); 4110 if (error != 0) 4111 return (error); 4112 4113 hrtime_t start_time = gethrtime(); 4114 4115 ddt_prefetch_all(spa); 4116 4117 zfs_dbgmsg("pool '%s': loaded ddt into ARC in %llu ms", spa->spa_name, 4118 (u_longlong_t)NSEC2MSEC(gethrtime() - start_time)); 4119 4120 spa_close(spa, FTAG); 4121 4122 return (error); 4123 } 4124 4125 /* 4126 * inputs: 4127 * zc_name name of dataset to destroy 4128 * zc_defer_destroy mark for deferred destroy 4129 * 4130 * outputs: none 4131 */ 4132 static int 4133 zfs_ioc_destroy(zfs_cmd_t *zc) 4134 { 4135 objset_t *os; 4136 dmu_objset_type_t ost; 4137 int err; 4138 4139 err = dmu_objset_hold(zc->zc_name, FTAG, &os); 4140 if (err != 0) 4141 return (err); 4142 ost = dmu_objset_type(os); 4143 dmu_objset_rele(os, FTAG); 4144 4145 if (ost == DMU_OST_ZFS) 4146 zfs_unmount_snap(zc->zc_name); 4147 4148 if (strchr(zc->zc_name, '@')) { 4149 err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy); 4150 } else { 4151 err = dsl_destroy_head(zc->zc_name); 4152 if (err == EEXIST) { 4153 /* 4154 * It is possible that the given DS may have 4155 * hidden child (%recv) datasets - "leftovers" 4156 * resulting from the previously interrupted 4157 * 'zfs receive'. 4158 * 4159 * 6 extra bytes for /%recv 4160 */ 4161 char namebuf[ZFS_MAX_DATASET_NAME_LEN + 6]; 4162 4163 if (snprintf(namebuf, sizeof (namebuf), "%s/%s", 4164 zc->zc_name, recv_clone_name) >= 4165 sizeof (namebuf)) 4166 return (SET_ERROR(EINVAL)); 4167 4168 /* 4169 * Try to remove the hidden child (%recv) and after 4170 * that try to remove the target dataset. 4171 * If the hidden child (%recv) does not exist 4172 * the original error (EEXIST) will be returned 4173 */ 4174 err = dsl_destroy_head(namebuf); 4175 if (err == 0) 4176 err = dsl_destroy_head(zc->zc_name); 4177 else if (err == ENOENT) 4178 err = SET_ERROR(EEXIST); 4179 } 4180 } 4181 4182 return (err); 4183 } 4184 4185 /* 4186 * innvl: { 4187 * "initialize_command" -> POOL_INITIALIZE_{CANCEL|START|SUSPEND} (uint64) 4188 * "initialize_vdevs": { -> guids to initialize (nvlist) 4189 * "vdev_path_1": vdev_guid_1, (uint64), 4190 * "vdev_path_2": vdev_guid_2, (uint64), 4191 * ... 4192 * }, 4193 * } 4194 * 4195 * outnvl: { 4196 * "initialize_vdevs": { -> initialization errors (nvlist) 4197 * "vdev_path_1": errno, see function body for possible errnos (uint64) 4198 * "vdev_path_2": errno, ... (uint64) 4199 * ... 4200 * } 4201 * } 4202 * 4203 * EINVAL is returned for an unknown commands or if any of the provided vdev 4204 * guids have be specified with a type other than uint64. 4205 */ 4206 static const zfs_ioc_key_t zfs_keys_pool_initialize[] = { 4207 {ZPOOL_INITIALIZE_COMMAND, DATA_TYPE_UINT64, 0}, 4208 {ZPOOL_INITIALIZE_VDEVS, DATA_TYPE_NVLIST, 0} 4209 }; 4210 4211 static int 4212 zfs_ioc_pool_initialize(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl) 4213 { 4214 uint64_t cmd_type; 4215 if (nvlist_lookup_uint64(innvl, ZPOOL_INITIALIZE_COMMAND, 4216 &cmd_type) != 0) { 4217 return (SET_ERROR(EINVAL)); 4218 } 4219 4220 if (!(cmd_type == POOL_INITIALIZE_CANCEL || 4221 cmd_type == POOL_INITIALIZE_START || 4222 cmd_type == POOL_INITIALIZE_SUSPEND || 4223 cmd_type == POOL_INITIALIZE_UNINIT)) { 4224 return (SET_ERROR(EINVAL)); 4225 } 4226 4227 nvlist_t *vdev_guids; 4228 if (nvlist_lookup_nvlist(innvl, ZPOOL_INITIALIZE_VDEVS, 4229 &vdev_guids) != 0) { 4230 return (SET_ERROR(EINVAL)); 4231 } 4232 4233 for (nvpair_t *pair = nvlist_next_nvpair(vdev_guids, NULL); 4234 pair != NULL; pair = nvlist_next_nvpair(vdev_guids, pair)) { 4235 uint64_t vdev_guid; 4236 if (nvpair_value_uint64(pair, &vdev_guid) != 0) { 4237 return (SET_ERROR(EINVAL)); 4238 } 4239 } 4240 4241 spa_t *spa; 4242 int error = spa_open(poolname, &spa, FTAG); 4243 if (error != 0) 4244 return (error); 4245 4246 nvlist_t *vdev_errlist = fnvlist_alloc(); 4247 int total_errors = spa_vdev_initialize(spa, vdev_guids, cmd_type, 4248 vdev_errlist); 4249 4250 if (fnvlist_size(vdev_errlist) > 0) { 4251 fnvlist_add_nvlist(outnvl, ZPOOL_INITIALIZE_VDEVS, 4252 vdev_errlist); 4253 } 4254 fnvlist_free(vdev_errlist); 4255 4256 spa_close(spa, FTAG); 4257 return (total_errors > 0 ? SET_ERROR(EINVAL) : 0); 4258 } 4259 4260 /* 4261 * innvl: { 4262 * "trim_command" -> POOL_TRIM_{CANCEL|START|SUSPEND} (uint64) 4263 * "trim_vdevs": { -> guids to TRIM (nvlist) 4264 * "vdev_path_1": vdev_guid_1, (uint64), 4265 * "vdev_path_2": vdev_guid_2, (uint64), 4266 * ... 4267 * }, 4268 * "trim_rate" -> Target TRIM rate in bytes/sec. 4269 * "trim_secure" -> Set to request a secure TRIM. 4270 * } 4271 * 4272 * outnvl: { 4273 * "trim_vdevs": { -> TRIM errors (nvlist) 4274 * "vdev_path_1": errno, see function body for possible errnos (uint64) 4275 * "vdev_path_2": errno, ... (uint64) 4276 * ... 4277 * } 4278 * } 4279 * 4280 * EINVAL is returned for an unknown commands or if any of the provided vdev 4281 * guids have be specified with a type other than uint64. 4282 */ 4283 static const zfs_ioc_key_t zfs_keys_pool_trim[] = { 4284 {ZPOOL_TRIM_COMMAND, DATA_TYPE_UINT64, 0}, 4285 {ZPOOL_TRIM_VDEVS, DATA_TYPE_NVLIST, 0}, 4286 {ZPOOL_TRIM_RATE, DATA_TYPE_UINT64, ZK_OPTIONAL}, 4287 {ZPOOL_TRIM_SECURE, DATA_TYPE_BOOLEAN_VALUE, ZK_OPTIONAL}, 4288 }; 4289 4290 static int 4291 zfs_ioc_pool_trim(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl) 4292 { 4293 uint64_t cmd_type; 4294 if (nvlist_lookup_uint64(innvl, ZPOOL_TRIM_COMMAND, &cmd_type) != 0) 4295 return (SET_ERROR(EINVAL)); 4296 4297 if (!(cmd_type == POOL_TRIM_CANCEL || 4298 cmd_type == POOL_TRIM_START || 4299 cmd_type == POOL_TRIM_SUSPEND)) { 4300 return (SET_ERROR(EINVAL)); 4301 } 4302 4303 nvlist_t *vdev_guids; 4304 if (nvlist_lookup_nvlist(innvl, ZPOOL_TRIM_VDEVS, &vdev_guids) != 0) 4305 return (SET_ERROR(EINVAL)); 4306 4307 for (nvpair_t *pair = nvlist_next_nvpair(vdev_guids, NULL); 4308 pair != NULL; pair = nvlist_next_nvpair(vdev_guids, pair)) { 4309 uint64_t vdev_guid; 4310 if (nvpair_value_uint64(pair, &vdev_guid) != 0) { 4311 return (SET_ERROR(EINVAL)); 4312 } 4313 } 4314 4315 /* Optional, defaults to maximum rate when not provided */ 4316 uint64_t rate; 4317 if (nvlist_lookup_uint64(innvl, ZPOOL_TRIM_RATE, &rate) != 0) 4318 rate = 0; 4319 4320 /* Optional, defaults to standard TRIM when not provided */ 4321 boolean_t secure; 4322 if (nvlist_lookup_boolean_value(innvl, ZPOOL_TRIM_SECURE, 4323 &secure) != 0) { 4324 secure = B_FALSE; 4325 } 4326 4327 spa_t *spa; 4328 int error = spa_open(poolname, &spa, FTAG); 4329 if (error != 0) 4330 return (error); 4331 4332 nvlist_t *vdev_errlist = fnvlist_alloc(); 4333 int total_errors = spa_vdev_trim(spa, vdev_guids, cmd_type, 4334 rate, !!zfs_trim_metaslab_skip, secure, vdev_errlist); 4335 4336 if (fnvlist_size(vdev_errlist) > 0) 4337 fnvlist_add_nvlist(outnvl, ZPOOL_TRIM_VDEVS, vdev_errlist); 4338 4339 fnvlist_free(vdev_errlist); 4340 4341 spa_close(spa, FTAG); 4342 return (total_errors > 0 ? SET_ERROR(EINVAL) : 0); 4343 } 4344 4345 #define DDT_PRUNE_UNIT "ddt_prune_unit" 4346 #define DDT_PRUNE_AMOUNT "ddt_prune_amount" 4347 4348 /* 4349 * innvl: { 4350 * "ddt_prune_unit" -> uint32_t 4351 * "ddt_prune_amount" -> uint64_t 4352 * } 4353 * 4354 * outnvl: "waited" -> boolean_t 4355 */ 4356 static const zfs_ioc_key_t zfs_keys_ddt_prune[] = { 4357 {DDT_PRUNE_UNIT, DATA_TYPE_INT32, 0}, 4358 {DDT_PRUNE_AMOUNT, DATA_TYPE_UINT64, 0}, 4359 }; 4360 4361 static int 4362 zfs_ioc_ddt_prune(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl) 4363 { 4364 int32_t unit; 4365 uint64_t amount; 4366 4367 if (nvlist_lookup_int32(innvl, DDT_PRUNE_UNIT, &unit) != 0 || 4368 nvlist_lookup_uint64(innvl, DDT_PRUNE_AMOUNT, &amount) != 0) { 4369 return (EINVAL); 4370 } 4371 4372 spa_t *spa; 4373 int error = spa_open(poolname, &spa, FTAG); 4374 if (error != 0) 4375 return (error); 4376 4377 if (!spa_feature_is_enabled(spa, SPA_FEATURE_FAST_DEDUP)) { 4378 spa_close(spa, FTAG); 4379 return (SET_ERROR(ENOTSUP)); 4380 } 4381 4382 error = ddt_prune_unique_entries(spa, (zpool_ddt_prune_unit_t)unit, 4383 amount); 4384 4385 spa_close(spa, FTAG); 4386 4387 return (error); 4388 } 4389 4390 /* 4391 * This ioctl waits for activity of a particular type to complete. If there is 4392 * no activity of that type in progress, it returns immediately, and the 4393 * returned value "waited" is false. If there is activity in progress, and no 4394 * tag is passed in, the ioctl blocks until all activity of that type is 4395 * complete, and then returns with "waited" set to true. 4396 * 4397 * If a tag is provided, it identifies a particular instance of an activity to 4398 * wait for. Currently, this is only valid for use with 'initialize', because 4399 * that is the only activity for which there can be multiple instances running 4400 * concurrently. In the case of 'initialize', the tag corresponds to the guid of 4401 * the vdev on which to wait. 4402 * 4403 * If a thread waiting in the ioctl receives a signal, the call will return 4404 * immediately, and the return value will be EINTR. 4405 * 4406 * innvl: { 4407 * "wait_activity" -> int32_t 4408 * (optional) "wait_tag" -> uint64_t 4409 * } 4410 * 4411 * outnvl: "waited" -> boolean_t 4412 */ 4413 static const zfs_ioc_key_t zfs_keys_pool_wait[] = { 4414 {ZPOOL_WAIT_ACTIVITY, DATA_TYPE_INT32, 0}, 4415 {ZPOOL_WAIT_TAG, DATA_TYPE_UINT64, ZK_OPTIONAL}, 4416 }; 4417 4418 static int 4419 zfs_ioc_wait(const char *name, nvlist_t *innvl, nvlist_t *outnvl) 4420 { 4421 int32_t activity; 4422 uint64_t tag; 4423 boolean_t waited; 4424 int error; 4425 4426 if (nvlist_lookup_int32(innvl, ZPOOL_WAIT_ACTIVITY, &activity) != 0) 4427 return (EINVAL); 4428 4429 if (nvlist_lookup_uint64(innvl, ZPOOL_WAIT_TAG, &tag) == 0) 4430 error = spa_wait_tag(name, activity, tag, &waited); 4431 else 4432 error = spa_wait(name, activity, &waited); 4433 4434 if (error == 0) 4435 fnvlist_add_boolean_value(outnvl, ZPOOL_WAIT_WAITED, waited); 4436 4437 return (error); 4438 } 4439 4440 /* 4441 * This ioctl waits for activity of a particular type to complete. If there is 4442 * no activity of that type in progress, it returns immediately, and the 4443 * returned value "waited" is false. If there is activity in progress, and no 4444 * tag is passed in, the ioctl blocks until all activity of that type is 4445 * complete, and then returns with "waited" set to true. 4446 * 4447 * If a thread waiting in the ioctl receives a signal, the call will return 4448 * immediately, and the return value will be EINTR. 4449 * 4450 * innvl: { 4451 * "wait_activity" -> int32_t 4452 * } 4453 * 4454 * outnvl: "waited" -> boolean_t 4455 */ 4456 static const zfs_ioc_key_t zfs_keys_fs_wait[] = { 4457 {ZFS_WAIT_ACTIVITY, DATA_TYPE_INT32, 0}, 4458 }; 4459 4460 static int 4461 zfs_ioc_wait_fs(const char *name, nvlist_t *innvl, nvlist_t *outnvl) 4462 { 4463 int32_t activity; 4464 boolean_t waited = B_FALSE; 4465 int error; 4466 dsl_pool_t *dp; 4467 dsl_dir_t *dd; 4468 dsl_dataset_t *ds; 4469 4470 if (nvlist_lookup_int32(innvl, ZFS_WAIT_ACTIVITY, &activity) != 0) 4471 return (SET_ERROR(EINVAL)); 4472 4473 if (activity >= ZFS_WAIT_NUM_ACTIVITIES || activity < 0) 4474 return (SET_ERROR(EINVAL)); 4475 4476 if ((error = dsl_pool_hold(name, FTAG, &dp)) != 0) 4477 return (error); 4478 4479 if ((error = dsl_dataset_hold(dp, name, FTAG, &ds)) != 0) { 4480 dsl_pool_rele(dp, FTAG); 4481 return (error); 4482 } 4483 4484 dd = ds->ds_dir; 4485 mutex_enter(&dd->dd_activity_lock); 4486 dd->dd_activity_waiters++; 4487 4488 /* 4489 * We get a long-hold here so that the dsl_dataset_t and dsl_dir_t 4490 * aren't evicted while we're waiting. Normally this is prevented by 4491 * holding the pool, but we can't do that while we're waiting since 4492 * that would prevent TXGs from syncing out. Some of the functionality 4493 * of long-holds (e.g. preventing deletion) is unnecessary for this 4494 * case, since we would cancel the waiters before proceeding with a 4495 * deletion. An alternative mechanism for keeping the dataset around 4496 * could be developed but this is simpler. 4497 */ 4498 dsl_dataset_long_hold(ds, FTAG); 4499 dsl_pool_rele(dp, FTAG); 4500 4501 error = dsl_dir_wait(dd, ds, activity, &waited); 4502 4503 dsl_dataset_long_rele(ds, FTAG); 4504 dd->dd_activity_waiters--; 4505 if (dd->dd_activity_waiters == 0) 4506 cv_signal(&dd->dd_activity_cv); 4507 mutex_exit(&dd->dd_activity_lock); 4508 4509 dsl_dataset_rele(ds, FTAG); 4510 4511 if (error == 0) 4512 fnvlist_add_boolean_value(outnvl, ZFS_WAIT_WAITED, waited); 4513 4514 return (error); 4515 } 4516 4517 /* 4518 * fsname is name of dataset to rollback (to most recent snapshot) 4519 * 4520 * innvl may contain name of expected target snapshot 4521 * 4522 * outnvl: "target" -> name of most recent snapshot 4523 * } 4524 */ 4525 static const zfs_ioc_key_t zfs_keys_rollback[] = { 4526 {"target", DATA_TYPE_STRING, ZK_OPTIONAL}, 4527 }; 4528 4529 static int 4530 zfs_ioc_rollback(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl) 4531 { 4532 zfsvfs_t *zfsvfs; 4533 zvol_state_handle_t *zv; 4534 const char *target = NULL; 4535 int error; 4536 4537 (void) nvlist_lookup_string(innvl, "target", &target); 4538 if (target != NULL) { 4539 const char *cp = strchr(target, '@'); 4540 4541 /* 4542 * The snap name must contain an @, and the part after it must 4543 * contain only valid characters. 4544 */ 4545 if (cp == NULL || 4546 zfs_component_namecheck(cp + 1, NULL, NULL) != 0) 4547 return (SET_ERROR(EINVAL)); 4548 } 4549 4550 if (getzfsvfs(fsname, &zfsvfs) == 0) { 4551 dsl_dataset_t *ds; 4552 4553 ds = dmu_objset_ds(zfsvfs->z_os); 4554 error = zfs_suspend_fs(zfsvfs); 4555 if (error == 0) { 4556 int resume_err; 4557 4558 error = dsl_dataset_rollback(fsname, target, zfsvfs, 4559 outnvl); 4560 resume_err = zfs_resume_fs(zfsvfs, ds); 4561 error = error ? error : resume_err; 4562 } 4563 zfs_vfs_rele(zfsvfs); 4564 } else if ((zv = zvol_suspend(fsname)) != NULL) { 4565 error = dsl_dataset_rollback(fsname, target, zvol_tag(zv), 4566 outnvl); 4567 zvol_resume(zv); 4568 } else { 4569 error = dsl_dataset_rollback(fsname, target, NULL, outnvl); 4570 } 4571 return (error); 4572 } 4573 4574 static int 4575 recursive_unmount(const char *fsname, void *arg) 4576 { 4577 const char *snapname = arg; 4578 char *fullname; 4579 4580 fullname = kmem_asprintf("%s@%s", fsname, snapname); 4581 zfs_unmount_snap(fullname); 4582 kmem_strfree(fullname); 4583 4584 return (0); 4585 } 4586 4587 /* 4588 * 4589 * snapname is the snapshot to redact. 4590 * innvl: { 4591 * "bookname" -> (string) 4592 * shortname of the redaction bookmark to generate 4593 * "snapnv" -> (nvlist, values ignored) 4594 * snapshots to redact snapname with respect to 4595 * } 4596 * 4597 * outnvl is unused 4598 */ 4599 4600 static const zfs_ioc_key_t zfs_keys_redact[] = { 4601 {"bookname", DATA_TYPE_STRING, 0}, 4602 {"snapnv", DATA_TYPE_NVLIST, 0}, 4603 }; 4604 4605 static int 4606 zfs_ioc_redact(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl) 4607 { 4608 (void) outnvl; 4609 nvlist_t *redactnvl = NULL; 4610 const char *redactbook = NULL; 4611 4612 if (nvlist_lookup_nvlist(innvl, "snapnv", &redactnvl) != 0) 4613 return (SET_ERROR(EINVAL)); 4614 if (fnvlist_num_pairs(redactnvl) == 0) 4615 return (SET_ERROR(ENXIO)); 4616 if (nvlist_lookup_string(innvl, "bookname", &redactbook) != 0) 4617 return (SET_ERROR(EINVAL)); 4618 4619 return (dmu_redact_snap(snapname, redactnvl, redactbook)); 4620 } 4621 4622 /* 4623 * inputs: 4624 * zc_name old name of dataset 4625 * zc_value new name of dataset 4626 * zc_cookie recursive flag (only valid for snapshots) 4627 * 4628 * outputs: none 4629 */ 4630 static int 4631 zfs_ioc_rename(zfs_cmd_t *zc) 4632 { 4633 objset_t *os; 4634 dmu_objset_type_t ost; 4635 boolean_t recursive = zc->zc_cookie & 1; 4636 boolean_t nounmount = !!(zc->zc_cookie & 2); 4637 char *at; 4638 int err; 4639 4640 /* "zfs rename" from and to ...%recv datasets should both fail */ 4641 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0'; 4642 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0'; 4643 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 || 4644 dataset_namecheck(zc->zc_value, NULL, NULL) != 0 || 4645 strchr(zc->zc_name, '%') || strchr(zc->zc_value, '%')) 4646 return (SET_ERROR(EINVAL)); 4647 4648 err = dmu_objset_hold(zc->zc_name, FTAG, &os); 4649 if (err != 0) 4650 return (err); 4651 ost = dmu_objset_type(os); 4652 dmu_objset_rele(os, FTAG); 4653 4654 at = strchr(zc->zc_name, '@'); 4655 if (at != NULL) { 4656 /* snaps must be in same fs */ 4657 int error; 4658 4659 if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1)) 4660 return (SET_ERROR(EXDEV)); 4661 *at = '\0'; 4662 if (ost == DMU_OST_ZFS && !nounmount) { 4663 error = dmu_objset_find(zc->zc_name, 4664 recursive_unmount, at + 1, 4665 recursive ? DS_FIND_CHILDREN : 0); 4666 if (error != 0) { 4667 *at = '@'; 4668 return (error); 4669 } 4670 } 4671 error = dsl_dataset_rename_snapshot(zc->zc_name, 4672 at + 1, strchr(zc->zc_value, '@') + 1, recursive); 4673 *at = '@'; 4674 4675 return (error); 4676 } else { 4677 return (dsl_dir_rename(zc->zc_name, zc->zc_value)); 4678 } 4679 } 4680 4681 static int 4682 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr) 4683 { 4684 const char *propname = nvpair_name(pair); 4685 boolean_t issnap = (strchr(dsname, '@') != NULL); 4686 zfs_prop_t prop = zfs_name_to_prop(propname); 4687 uint64_t intval, compval; 4688 int err; 4689 4690 if (prop == ZPROP_USERPROP) { 4691 if (zfs_prop_user(propname)) { 4692 if ((err = zfs_secpolicy_write_perms(dsname, 4693 ZFS_DELEG_PERM_USERPROP, cr))) 4694 return (err); 4695 return (0); 4696 } 4697 4698 if (!issnap && zfs_prop_userquota(propname)) { 4699 const char *perm = NULL; 4700 const char *uq_prefix = 4701 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA]; 4702 const char *gq_prefix = 4703 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA]; 4704 const char *uiq_prefix = 4705 zfs_userquota_prop_prefixes[ZFS_PROP_USEROBJQUOTA]; 4706 const char *giq_prefix = 4707 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPOBJQUOTA]; 4708 const char *pq_prefix = 4709 zfs_userquota_prop_prefixes[ZFS_PROP_PROJECTQUOTA]; 4710 const char *piq_prefix = zfs_userquota_prop_prefixes[\ 4711 ZFS_PROP_PROJECTOBJQUOTA]; 4712 4713 if (strncmp(propname, uq_prefix, 4714 strlen(uq_prefix)) == 0) { 4715 perm = ZFS_DELEG_PERM_USERQUOTA; 4716 } else if (strncmp(propname, uiq_prefix, 4717 strlen(uiq_prefix)) == 0) { 4718 perm = ZFS_DELEG_PERM_USEROBJQUOTA; 4719 } else if (strncmp(propname, gq_prefix, 4720 strlen(gq_prefix)) == 0) { 4721 perm = ZFS_DELEG_PERM_GROUPQUOTA; 4722 } else if (strncmp(propname, giq_prefix, 4723 strlen(giq_prefix)) == 0) { 4724 perm = ZFS_DELEG_PERM_GROUPOBJQUOTA; 4725 } else if (strncmp(propname, pq_prefix, 4726 strlen(pq_prefix)) == 0) { 4727 perm = ZFS_DELEG_PERM_PROJECTQUOTA; 4728 } else if (strncmp(propname, piq_prefix, 4729 strlen(piq_prefix)) == 0) { 4730 perm = ZFS_DELEG_PERM_PROJECTOBJQUOTA; 4731 } else { 4732 /* {USER|GROUP|PROJECT}USED are read-only */ 4733 return (SET_ERROR(EINVAL)); 4734 } 4735 4736 if ((err = zfs_secpolicy_write_perms(dsname, perm, cr))) 4737 return (err); 4738 return (0); 4739 } 4740 4741 return (SET_ERROR(EINVAL)); 4742 } 4743 4744 if (issnap) 4745 return (SET_ERROR(EINVAL)); 4746 4747 if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 4748 /* 4749 * dsl_prop_get_all_impl() returns properties in this 4750 * format. 4751 */ 4752 nvlist_t *attrs; 4753 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 4754 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 4755 &pair) == 0); 4756 } 4757 4758 /* 4759 * Check that this value is valid for this pool version 4760 */ 4761 switch (prop) { 4762 case ZFS_PROP_COMPRESSION: 4763 /* 4764 * If the user specified gzip compression, make sure 4765 * the SPA supports it. We ignore any errors here since 4766 * we'll catch them later. 4767 */ 4768 if (nvpair_value_uint64(pair, &intval) == 0) { 4769 compval = ZIO_COMPRESS_ALGO(intval); 4770 if (compval >= ZIO_COMPRESS_GZIP_1 && 4771 compval <= ZIO_COMPRESS_GZIP_9 && 4772 zfs_earlier_version(dsname, 4773 SPA_VERSION_GZIP_COMPRESSION)) { 4774 return (SET_ERROR(ENOTSUP)); 4775 } 4776 4777 if (compval == ZIO_COMPRESS_ZLE && 4778 zfs_earlier_version(dsname, 4779 SPA_VERSION_ZLE_COMPRESSION)) 4780 return (SET_ERROR(ENOTSUP)); 4781 4782 if (compval == ZIO_COMPRESS_LZ4) { 4783 spa_t *spa; 4784 4785 if ((err = spa_open(dsname, &spa, FTAG)) != 0) 4786 return (err); 4787 4788 if (!spa_feature_is_enabled(spa, 4789 SPA_FEATURE_LZ4_COMPRESS)) { 4790 spa_close(spa, FTAG); 4791 return (SET_ERROR(ENOTSUP)); 4792 } 4793 spa_close(spa, FTAG); 4794 } 4795 4796 if (compval == ZIO_COMPRESS_ZSTD) { 4797 spa_t *spa; 4798 4799 if ((err = spa_open(dsname, &spa, FTAG)) != 0) 4800 return (err); 4801 4802 if (!spa_feature_is_enabled(spa, 4803 SPA_FEATURE_ZSTD_COMPRESS)) { 4804 spa_close(spa, FTAG); 4805 return (SET_ERROR(ENOTSUP)); 4806 } 4807 spa_close(spa, FTAG); 4808 } 4809 } 4810 break; 4811 4812 case ZFS_PROP_COPIES: 4813 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS)) 4814 return (SET_ERROR(ENOTSUP)); 4815 break; 4816 4817 case ZFS_PROP_VOLBLOCKSIZE: 4818 case ZFS_PROP_RECORDSIZE: 4819 /* Record sizes above 128k need the feature to be enabled */ 4820 if (nvpair_value_uint64(pair, &intval) == 0 && 4821 intval > SPA_OLD_MAXBLOCKSIZE) { 4822 spa_t *spa; 4823 4824 /* 4825 * We don't allow setting the property above 1MB, 4826 * unless the tunable has been changed. 4827 */ 4828 if (intval > zfs_max_recordsize || 4829 intval > SPA_MAXBLOCKSIZE) 4830 return (SET_ERROR(ERANGE)); 4831 4832 if ((err = spa_open(dsname, &spa, FTAG)) != 0) 4833 return (err); 4834 4835 if (!spa_feature_is_enabled(spa, 4836 SPA_FEATURE_LARGE_BLOCKS)) { 4837 spa_close(spa, FTAG); 4838 return (SET_ERROR(ENOTSUP)); 4839 } 4840 spa_close(spa, FTAG); 4841 } 4842 break; 4843 4844 case ZFS_PROP_DNODESIZE: 4845 /* Dnode sizes above 512 need the feature to be enabled */ 4846 if (nvpair_value_uint64(pair, &intval) == 0 && 4847 intval != ZFS_DNSIZE_LEGACY) { 4848 spa_t *spa; 4849 4850 if ((err = spa_open(dsname, &spa, FTAG)) != 0) 4851 return (err); 4852 4853 if (!spa_feature_is_enabled(spa, 4854 SPA_FEATURE_LARGE_DNODE)) { 4855 spa_close(spa, FTAG); 4856 return (SET_ERROR(ENOTSUP)); 4857 } 4858 spa_close(spa, FTAG); 4859 } 4860 break; 4861 4862 case ZFS_PROP_SPECIAL_SMALL_BLOCKS: 4863 /* 4864 * This property could require the allocation classes 4865 * feature to be active for setting, however we allow 4866 * it so that tests of settable properties succeed. 4867 * The CLI will issue a warning in this case. 4868 */ 4869 break; 4870 4871 case ZFS_PROP_SHARESMB: 4872 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID)) 4873 return (SET_ERROR(ENOTSUP)); 4874 break; 4875 4876 case ZFS_PROP_ACLINHERIT: 4877 if (nvpair_type(pair) == DATA_TYPE_UINT64 && 4878 nvpair_value_uint64(pair, &intval) == 0) { 4879 if (intval == ZFS_ACL_PASSTHROUGH_X && 4880 zfs_earlier_version(dsname, 4881 SPA_VERSION_PASSTHROUGH_X)) 4882 return (SET_ERROR(ENOTSUP)); 4883 } 4884 break; 4885 case ZFS_PROP_CHECKSUM: 4886 case ZFS_PROP_DEDUP: 4887 { 4888 spa_feature_t feature; 4889 spa_t *spa; 4890 int err; 4891 4892 /* dedup feature version checks */ 4893 if (prop == ZFS_PROP_DEDUP && 4894 zfs_earlier_version(dsname, SPA_VERSION_DEDUP)) 4895 return (SET_ERROR(ENOTSUP)); 4896 4897 if (nvpair_type(pair) == DATA_TYPE_UINT64 && 4898 nvpair_value_uint64(pair, &intval) == 0) { 4899 /* check prop value is enabled in features */ 4900 feature = zio_checksum_to_feature( 4901 intval & ZIO_CHECKSUM_MASK); 4902 if (feature == SPA_FEATURE_NONE) 4903 break; 4904 4905 if ((err = spa_open(dsname, &spa, FTAG)) != 0) 4906 return (err); 4907 4908 if (!spa_feature_is_enabled(spa, feature)) { 4909 spa_close(spa, FTAG); 4910 return (SET_ERROR(ENOTSUP)); 4911 } 4912 spa_close(spa, FTAG); 4913 } 4914 break; 4915 } 4916 4917 default: 4918 break; 4919 } 4920 4921 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED())); 4922 } 4923 4924 /* 4925 * Removes properties from the given props list that fail permission checks 4926 * needed to clear them and to restore them in case of a receive error. For each 4927 * property, make sure we have both set and inherit permissions. 4928 * 4929 * Returns the first error encountered if any permission checks fail. If the 4930 * caller provides a non-NULL errlist, it also gives the complete list of names 4931 * of all the properties that failed a permission check along with the 4932 * corresponding error numbers. The caller is responsible for freeing the 4933 * returned errlist. 4934 * 4935 * If every property checks out successfully, zero is returned and the list 4936 * pointed at by errlist is NULL. 4937 */ 4938 static int 4939 zfs_check_clearable(const char *dataset, nvlist_t *props, nvlist_t **errlist) 4940 { 4941 zfs_cmd_t *zc; 4942 nvpair_t *pair, *next_pair; 4943 nvlist_t *errors; 4944 int err, rv = 0; 4945 4946 if (props == NULL) 4947 return (0); 4948 4949 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0); 4950 4951 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP); 4952 (void) strlcpy(zc->zc_name, dataset, sizeof (zc->zc_name)); 4953 pair = nvlist_next_nvpair(props, NULL); 4954 while (pair != NULL) { 4955 next_pair = nvlist_next_nvpair(props, pair); 4956 4957 (void) strlcpy(zc->zc_value, nvpair_name(pair), 4958 sizeof (zc->zc_value)); 4959 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 || 4960 (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) { 4961 VERIFY(nvlist_remove_nvpair(props, pair) == 0); 4962 VERIFY(nvlist_add_int32(errors, 4963 zc->zc_value, err) == 0); 4964 } 4965 pair = next_pair; 4966 } 4967 kmem_free(zc, sizeof (zfs_cmd_t)); 4968 4969 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) { 4970 nvlist_free(errors); 4971 errors = NULL; 4972 } else { 4973 VERIFY(nvpair_value_int32(pair, &rv) == 0); 4974 } 4975 4976 if (errlist == NULL) 4977 nvlist_free(errors); 4978 else 4979 *errlist = errors; 4980 4981 return (rv); 4982 } 4983 4984 static boolean_t 4985 propval_equals(nvpair_t *p1, nvpair_t *p2) 4986 { 4987 if (nvpair_type(p1) == DATA_TYPE_NVLIST) { 4988 /* dsl_prop_get_all_impl() format */ 4989 nvlist_t *attrs; 4990 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0); 4991 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 4992 &p1) == 0); 4993 } 4994 4995 if (nvpair_type(p2) == DATA_TYPE_NVLIST) { 4996 nvlist_t *attrs; 4997 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0); 4998 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 4999 &p2) == 0); 5000 } 5001 5002 if (nvpair_type(p1) != nvpair_type(p2)) 5003 return (B_FALSE); 5004 5005 if (nvpair_type(p1) == DATA_TYPE_STRING) { 5006 const char *valstr1, *valstr2; 5007 5008 VERIFY(nvpair_value_string(p1, &valstr1) == 0); 5009 VERIFY(nvpair_value_string(p2, &valstr2) == 0); 5010 return (strcmp(valstr1, valstr2) == 0); 5011 } else { 5012 uint64_t intval1, intval2; 5013 5014 VERIFY(nvpair_value_uint64(p1, &intval1) == 0); 5015 VERIFY(nvpair_value_uint64(p2, &intval2) == 0); 5016 return (intval1 == intval2); 5017 } 5018 } 5019 5020 /* 5021 * Remove properties from props if they are not going to change (as determined 5022 * by comparison with origprops). Remove them from origprops as well, since we 5023 * do not need to clear or restore properties that won't change. 5024 */ 5025 static void 5026 props_reduce(nvlist_t *props, nvlist_t *origprops) 5027 { 5028 nvpair_t *pair, *next_pair; 5029 5030 if (origprops == NULL) 5031 return; /* all props need to be received */ 5032 5033 pair = nvlist_next_nvpair(props, NULL); 5034 while (pair != NULL) { 5035 const char *propname = nvpair_name(pair); 5036 nvpair_t *match; 5037 5038 next_pair = nvlist_next_nvpair(props, pair); 5039 5040 if ((nvlist_lookup_nvpair(origprops, propname, 5041 &match) != 0) || !propval_equals(pair, match)) 5042 goto next; /* need to set received value */ 5043 5044 /* don't clear the existing received value */ 5045 (void) nvlist_remove_nvpair(origprops, match); 5046 /* don't bother receiving the property */ 5047 (void) nvlist_remove_nvpair(props, pair); 5048 next: 5049 pair = next_pair; 5050 } 5051 } 5052 5053 /* 5054 * Extract properties that cannot be set PRIOR to the receipt of a dataset. 5055 * For example, refquota cannot be set until after the receipt of a dataset, 5056 * because in replication streams, an older/earlier snapshot may exceed the 5057 * refquota. We want to receive the older/earlier snapshot, but setting 5058 * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent 5059 * the older/earlier snapshot from being received (with EDQUOT). 5060 * 5061 * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario. 5062 * 5063 * libzfs will need to be judicious handling errors encountered by props 5064 * extracted by this function. 5065 */ 5066 static nvlist_t * 5067 extract_delay_props(nvlist_t *props) 5068 { 5069 nvlist_t *delayprops; 5070 nvpair_t *nvp, *tmp; 5071 static const zfs_prop_t delayable[] = { 5072 ZFS_PROP_REFQUOTA, 5073 ZFS_PROP_KEYLOCATION, 5074 /* 5075 * Setting ZFS_PROP_SHARESMB requires the objset type to be 5076 * known, which is not possible prior to receipt of raw sends. 5077 */ 5078 ZFS_PROP_SHARESMB, 5079 0 5080 }; 5081 int i; 5082 5083 VERIFY(nvlist_alloc(&delayprops, NV_UNIQUE_NAME, KM_SLEEP) == 0); 5084 5085 for (nvp = nvlist_next_nvpair(props, NULL); nvp != NULL; 5086 nvp = nvlist_next_nvpair(props, nvp)) { 5087 /* 5088 * strcmp() is safe because zfs_prop_to_name() always returns 5089 * a bounded string. 5090 */ 5091 for (i = 0; delayable[i] != 0; i++) { 5092 if (strcmp(zfs_prop_to_name(delayable[i]), 5093 nvpair_name(nvp)) == 0) { 5094 break; 5095 } 5096 } 5097 if (delayable[i] != 0) { 5098 tmp = nvlist_prev_nvpair(props, nvp); 5099 VERIFY(nvlist_add_nvpair(delayprops, nvp) == 0); 5100 VERIFY(nvlist_remove_nvpair(props, nvp) == 0); 5101 nvp = tmp; 5102 } 5103 } 5104 5105 if (nvlist_empty(delayprops)) { 5106 nvlist_free(delayprops); 5107 delayprops = NULL; 5108 } 5109 return (delayprops); 5110 } 5111 5112 static void 5113 zfs_allow_log_destroy(void *arg) 5114 { 5115 char *poolname = arg; 5116 5117 if (poolname != NULL) 5118 kmem_strfree(poolname); 5119 } 5120 5121 #ifdef ZFS_DEBUG 5122 static boolean_t zfs_ioc_recv_inject_err; 5123 #endif 5124 5125 /* 5126 * nvlist 'errors' is always allocated. It will contain descriptions of 5127 * encountered errors, if any. It's the callers responsibility to free. 5128 */ 5129 static int 5130 zfs_ioc_recv_impl(char *tofs, char *tosnap, const char *origin, 5131 nvlist_t *recvprops, nvlist_t *localprops, nvlist_t *hidden_args, 5132 boolean_t force, boolean_t heal, boolean_t resumable, int input_fd, 5133 dmu_replay_record_t *begin_record, uint64_t *read_bytes, 5134 uint64_t *errflags, nvlist_t **errors) 5135 { 5136 dmu_recv_cookie_t drc; 5137 int error = 0; 5138 int props_error = 0; 5139 offset_t off, noff; 5140 nvlist_t *local_delayprops = NULL; 5141 nvlist_t *recv_delayprops = NULL; 5142 nvlist_t *inherited_delayprops = NULL; 5143 nvlist_t *origprops = NULL; /* existing properties */ 5144 nvlist_t *origrecvd = NULL; /* existing received properties */ 5145 boolean_t first_recvd_props = B_FALSE; 5146 boolean_t tofs_was_redacted; 5147 zfs_file_t *input_fp; 5148 5149 *read_bytes = 0; 5150 *errflags = 0; 5151 *errors = fnvlist_alloc(); 5152 off = 0; 5153 5154 if ((input_fp = zfs_file_get(input_fd)) == NULL) 5155 return (SET_ERROR(EBADF)); 5156 5157 noff = off = zfs_file_off(input_fp); 5158 error = dmu_recv_begin(tofs, tosnap, begin_record, force, heal, 5159 resumable, localprops, hidden_args, origin, &drc, input_fp, 5160 &off); 5161 if (error != 0) 5162 goto out; 5163 tofs_was_redacted = dsl_get_redacted(drc.drc_ds); 5164 5165 /* 5166 * Set properties before we receive the stream so that they are applied 5167 * to the new data. Note that we must call dmu_recv_stream() if 5168 * dmu_recv_begin() succeeds. 5169 */ 5170 if (recvprops != NULL && !drc.drc_newfs) { 5171 if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >= 5172 SPA_VERSION_RECVD_PROPS && 5173 !dsl_prop_get_hasrecvd(tofs)) 5174 first_recvd_props = B_TRUE; 5175 5176 /* 5177 * If new received properties are supplied, they are to 5178 * completely replace the existing received properties, 5179 * so stash away the existing ones. 5180 */ 5181 if (dsl_prop_get_received(tofs, &origrecvd) == 0) { 5182 nvlist_t *errlist = NULL; 5183 /* 5184 * Don't bother writing a property if its value won't 5185 * change (and avoid the unnecessary security checks). 5186 * 5187 * The first receive after SPA_VERSION_RECVD_PROPS is a 5188 * special case where we blow away all local properties 5189 * regardless. 5190 */ 5191 if (!first_recvd_props) 5192 props_reduce(recvprops, origrecvd); 5193 if (zfs_check_clearable(tofs, origrecvd, &errlist) != 0) 5194 (void) nvlist_merge(*errors, errlist, 0); 5195 nvlist_free(errlist); 5196 5197 if (clear_received_props(tofs, origrecvd, 5198 first_recvd_props ? NULL : recvprops) != 0) 5199 *errflags |= ZPROP_ERR_NOCLEAR; 5200 } else { 5201 *errflags |= ZPROP_ERR_NOCLEAR; 5202 } 5203 } 5204 5205 /* 5206 * Stash away existing properties so we can restore them on error unless 5207 * we're doing the first receive after SPA_VERSION_RECVD_PROPS, in which 5208 * case "origrecvd" will take care of that. 5209 */ 5210 if (localprops != NULL && !drc.drc_newfs && !first_recvd_props) { 5211 objset_t *os; 5212 if (dmu_objset_hold(tofs, FTAG, &os) == 0) { 5213 if (dsl_prop_get_all(os, &origprops) != 0) { 5214 *errflags |= ZPROP_ERR_NOCLEAR; 5215 } 5216 dmu_objset_rele(os, FTAG); 5217 } else { 5218 *errflags |= ZPROP_ERR_NOCLEAR; 5219 } 5220 } 5221 5222 if (recvprops != NULL) { 5223 props_error = dsl_prop_set_hasrecvd(tofs); 5224 5225 if (props_error == 0) { 5226 recv_delayprops = extract_delay_props(recvprops); 5227 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED, 5228 recvprops, *errors); 5229 } 5230 } 5231 5232 if (localprops != NULL) { 5233 nvlist_t *oprops = fnvlist_alloc(); 5234 nvlist_t *xprops = fnvlist_alloc(); 5235 nvpair_t *nvp = NULL; 5236 5237 while ((nvp = nvlist_next_nvpair(localprops, nvp)) != NULL) { 5238 if (nvpair_type(nvp) == DATA_TYPE_BOOLEAN) { 5239 /* -x property */ 5240 const char *name = nvpair_name(nvp); 5241 zfs_prop_t prop = zfs_name_to_prop(name); 5242 if (prop != ZPROP_USERPROP) { 5243 if (!zfs_prop_inheritable(prop)) 5244 continue; 5245 } else if (!zfs_prop_user(name)) 5246 continue; 5247 fnvlist_add_boolean(xprops, name); 5248 } else { 5249 /* -o property=value */ 5250 fnvlist_add_nvpair(oprops, nvp); 5251 } 5252 } 5253 5254 local_delayprops = extract_delay_props(oprops); 5255 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL, 5256 oprops, *errors); 5257 inherited_delayprops = extract_delay_props(xprops); 5258 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED, 5259 xprops, *errors); 5260 5261 nvlist_free(oprops); 5262 nvlist_free(xprops); 5263 } 5264 5265 error = dmu_recv_stream(&drc, &off); 5266 5267 if (error == 0) { 5268 zfsvfs_t *zfsvfs = NULL; 5269 zvol_state_handle_t *zv = NULL; 5270 5271 if (getzfsvfs(tofs, &zfsvfs) == 0) { 5272 /* online recv */ 5273 dsl_dataset_t *ds; 5274 int end_err; 5275 boolean_t stream_is_redacted = DMU_GET_FEATUREFLAGS( 5276 begin_record->drr_u.drr_begin. 5277 drr_versioninfo) & DMU_BACKUP_FEATURE_REDACTED; 5278 5279 ds = dmu_objset_ds(zfsvfs->z_os); 5280 error = zfs_suspend_fs(zfsvfs); 5281 /* 5282 * If the suspend fails, then the recv_end will 5283 * likely also fail, and clean up after itself. 5284 */ 5285 end_err = dmu_recv_end(&drc, zfsvfs); 5286 /* 5287 * If the dataset was not redacted, but we received a 5288 * redacted stream onto it, we need to unmount the 5289 * dataset. Otherwise, resume the filesystem. 5290 */ 5291 if (error == 0 && !drc.drc_newfs && 5292 stream_is_redacted && !tofs_was_redacted) { 5293 error = zfs_end_fs(zfsvfs, ds); 5294 } else if (error == 0) { 5295 error = zfs_resume_fs(zfsvfs, ds); 5296 } 5297 error = error ? error : end_err; 5298 zfs_vfs_rele(zfsvfs); 5299 } else if ((zv = zvol_suspend(tofs)) != NULL) { 5300 error = dmu_recv_end(&drc, zvol_tag(zv)); 5301 zvol_resume(zv); 5302 } else { 5303 error = dmu_recv_end(&drc, NULL); 5304 } 5305 5306 /* Set delayed properties now, after we're done receiving. */ 5307 if (recv_delayprops != NULL && error == 0) { 5308 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED, 5309 recv_delayprops, *errors); 5310 } 5311 if (local_delayprops != NULL && error == 0) { 5312 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL, 5313 local_delayprops, *errors); 5314 } 5315 if (inherited_delayprops != NULL && error == 0) { 5316 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED, 5317 inherited_delayprops, *errors); 5318 } 5319 } 5320 5321 /* 5322 * Merge delayed props back in with initial props, in case 5323 * we're DEBUG and zfs_ioc_recv_inject_err is set (which means 5324 * we have to make sure clear_received_props() includes 5325 * the delayed properties). 5326 * 5327 * Since zfs_ioc_recv_inject_err is only in DEBUG kernels, 5328 * using ASSERT() will be just like a VERIFY. 5329 */ 5330 if (recv_delayprops != NULL) { 5331 ASSERT(nvlist_merge(recvprops, recv_delayprops, 0) == 0); 5332 nvlist_free(recv_delayprops); 5333 } 5334 if (local_delayprops != NULL) { 5335 ASSERT(nvlist_merge(localprops, local_delayprops, 0) == 0); 5336 nvlist_free(local_delayprops); 5337 } 5338 if (inherited_delayprops != NULL) { 5339 ASSERT(nvlist_merge(localprops, inherited_delayprops, 0) == 0); 5340 nvlist_free(inherited_delayprops); 5341 } 5342 *read_bytes = off - noff; 5343 5344 #ifdef ZFS_DEBUG 5345 if (zfs_ioc_recv_inject_err) { 5346 zfs_ioc_recv_inject_err = B_FALSE; 5347 error = 1; 5348 } 5349 #endif 5350 5351 /* 5352 * On error, restore the original props. 5353 */ 5354 if (error != 0 && recvprops != NULL && !drc.drc_newfs) { 5355 if (clear_received_props(tofs, recvprops, NULL) != 0) { 5356 /* 5357 * We failed to clear the received properties. 5358 * Since we may have left a $recvd value on the 5359 * system, we can't clear the $hasrecvd flag. 5360 */ 5361 *errflags |= ZPROP_ERR_NORESTORE; 5362 } else if (first_recvd_props) { 5363 dsl_prop_unset_hasrecvd(tofs); 5364 } 5365 5366 if (origrecvd == NULL && !drc.drc_newfs) { 5367 /* We failed to stash the original properties. */ 5368 *errflags |= ZPROP_ERR_NORESTORE; 5369 } 5370 5371 /* 5372 * dsl_props_set() will not convert RECEIVED to LOCAL on or 5373 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL 5374 * explicitly if we're restoring local properties cleared in the 5375 * first new-style receive. 5376 */ 5377 if (origrecvd != NULL && 5378 zfs_set_prop_nvlist(tofs, (first_recvd_props ? 5379 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED), 5380 origrecvd, NULL) != 0) { 5381 /* 5382 * We stashed the original properties but failed to 5383 * restore them. 5384 */ 5385 *errflags |= ZPROP_ERR_NORESTORE; 5386 } 5387 } 5388 if (error != 0 && localprops != NULL && !drc.drc_newfs && 5389 !first_recvd_props) { 5390 nvlist_t *setprops; 5391 nvlist_t *inheritprops; 5392 nvpair_t *nvp; 5393 5394 if (origprops == NULL) { 5395 /* We failed to stash the original properties. */ 5396 *errflags |= ZPROP_ERR_NORESTORE; 5397 goto out; 5398 } 5399 5400 /* Restore original props */ 5401 setprops = fnvlist_alloc(); 5402 inheritprops = fnvlist_alloc(); 5403 nvp = NULL; 5404 while ((nvp = nvlist_next_nvpair(localprops, nvp)) != NULL) { 5405 const char *name = nvpair_name(nvp); 5406 const char *source; 5407 nvlist_t *attrs; 5408 5409 if (!nvlist_exists(origprops, name)) { 5410 /* 5411 * Property was not present or was explicitly 5412 * inherited before the receive, restore this. 5413 */ 5414 fnvlist_add_boolean(inheritprops, name); 5415 continue; 5416 } 5417 attrs = fnvlist_lookup_nvlist(origprops, name); 5418 source = fnvlist_lookup_string(attrs, ZPROP_SOURCE); 5419 5420 /* Skip received properties */ 5421 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) 5422 continue; 5423 5424 if (strcmp(source, tofs) == 0) { 5425 /* Property was locally set */ 5426 fnvlist_add_nvlist(setprops, name, attrs); 5427 } else { 5428 /* Property was implicitly inherited */ 5429 fnvlist_add_boolean(inheritprops, name); 5430 } 5431 } 5432 5433 if (zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL, setprops, 5434 NULL) != 0) 5435 *errflags |= ZPROP_ERR_NORESTORE; 5436 if (zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED, inheritprops, 5437 NULL) != 0) 5438 *errflags |= ZPROP_ERR_NORESTORE; 5439 5440 nvlist_free(setprops); 5441 nvlist_free(inheritprops); 5442 } 5443 out: 5444 zfs_file_put(input_fp); 5445 nvlist_free(origrecvd); 5446 nvlist_free(origprops); 5447 5448 if (error == 0) 5449 error = props_error; 5450 5451 return (error); 5452 } 5453 5454 /* 5455 * inputs: 5456 * zc_name name of containing filesystem (unused) 5457 * zc_nvlist_src{_size} nvlist of properties to apply 5458 * zc_nvlist_conf{_size} nvlist of properties to exclude 5459 * (DATA_TYPE_BOOLEAN) and override (everything else) 5460 * zc_value name of snapshot to create 5461 * zc_string name of clone origin (if DRR_FLAG_CLONE) 5462 * zc_cookie file descriptor to recv from 5463 * zc_begin_record the BEGIN record of the stream (not byteswapped) 5464 * zc_guid force flag 5465 * 5466 * outputs: 5467 * zc_cookie number of bytes read 5468 * zc_obj zprop_errflags_t 5469 * zc_nvlist_dst{_size} error for each unapplied received property 5470 */ 5471 static int 5472 zfs_ioc_recv(zfs_cmd_t *zc) 5473 { 5474 dmu_replay_record_t begin_record; 5475 nvlist_t *errors = NULL; 5476 nvlist_t *recvdprops = NULL; 5477 nvlist_t *localprops = NULL; 5478 const char *origin = NULL; 5479 char *tosnap; 5480 char tofs[ZFS_MAX_DATASET_NAME_LEN]; 5481 int error = 0; 5482 5483 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 || 5484 strchr(zc->zc_value, '@') == NULL || 5485 strchr(zc->zc_value, '%') != NULL) { 5486 return (SET_ERROR(EINVAL)); 5487 } 5488 5489 (void) strlcpy(tofs, zc->zc_value, sizeof (tofs)); 5490 tosnap = strchr(tofs, '@'); 5491 *tosnap++ = '\0'; 5492 5493 if (zc->zc_nvlist_src != 0 && 5494 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 5495 zc->zc_iflags, &recvdprops)) != 0) { 5496 goto out; 5497 } 5498 5499 if (zc->zc_nvlist_conf != 0 && 5500 (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 5501 zc->zc_iflags, &localprops)) != 0) { 5502 goto out; 5503 } 5504 5505 if (zc->zc_string[0]) 5506 origin = zc->zc_string; 5507 5508 begin_record.drr_type = DRR_BEGIN; 5509 begin_record.drr_payloadlen = 0; 5510 begin_record.drr_u.drr_begin = zc->zc_begin_record; 5511 5512 error = zfs_ioc_recv_impl(tofs, tosnap, origin, recvdprops, localprops, 5513 NULL, zc->zc_guid, B_FALSE, B_FALSE, zc->zc_cookie, &begin_record, 5514 &zc->zc_cookie, &zc->zc_obj, &errors); 5515 5516 /* 5517 * Now that all props, initial and delayed, are set, report the prop 5518 * errors to the caller. 5519 */ 5520 if (zc->zc_nvlist_dst_size != 0 && errors != NULL && 5521 (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 || 5522 put_nvlist(zc, errors) != 0)) { 5523 /* 5524 * Caller made zc->zc_nvlist_dst less than the minimum expected 5525 * size or supplied an invalid address. 5526 */ 5527 error = SET_ERROR(EINVAL); 5528 } 5529 5530 out: 5531 nvlist_free(errors); 5532 nvlist_free(recvdprops); 5533 nvlist_free(localprops); 5534 5535 return (error); 5536 } 5537 5538 /* 5539 * innvl: { 5540 * "snapname" -> full name of the snapshot to create 5541 * (optional) "props" -> received properties to set (nvlist) 5542 * (optional) "localprops" -> override and exclude properties (nvlist) 5543 * (optional) "origin" -> name of clone origin (DRR_FLAG_CLONE) 5544 * "begin_record" -> non-byteswapped dmu_replay_record_t 5545 * "input_fd" -> file descriptor to read stream from (int32) 5546 * (optional) "force" -> force flag (value ignored) 5547 * (optional) "heal" -> use send stream to heal data corruption 5548 * (optional) "resumable" -> resumable flag (value ignored) 5549 * (optional) "cleanup_fd" -> unused 5550 * (optional) "action_handle" -> unused 5551 * (optional) "hidden_args" -> { "wkeydata" -> value } 5552 * } 5553 * 5554 * outnvl: { 5555 * "read_bytes" -> number of bytes read 5556 * "error_flags" -> zprop_errflags_t 5557 * "errors" -> error for each unapplied received property (nvlist) 5558 * } 5559 */ 5560 static const zfs_ioc_key_t zfs_keys_recv_new[] = { 5561 {"snapname", DATA_TYPE_STRING, 0}, 5562 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL}, 5563 {"localprops", DATA_TYPE_NVLIST, ZK_OPTIONAL}, 5564 {"origin", DATA_TYPE_STRING, ZK_OPTIONAL}, 5565 {"begin_record", DATA_TYPE_BYTE_ARRAY, 0}, 5566 {"input_fd", DATA_TYPE_INT32, 0}, 5567 {"force", DATA_TYPE_BOOLEAN, ZK_OPTIONAL}, 5568 {"heal", DATA_TYPE_BOOLEAN, ZK_OPTIONAL}, 5569 {"resumable", DATA_TYPE_BOOLEAN, ZK_OPTIONAL}, 5570 {"cleanup_fd", DATA_TYPE_INT32, ZK_OPTIONAL}, 5571 {"action_handle", DATA_TYPE_UINT64, ZK_OPTIONAL}, 5572 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL}, 5573 }; 5574 5575 static int 5576 zfs_ioc_recv_new(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl) 5577 { 5578 dmu_replay_record_t *begin_record; 5579 uint_t begin_record_size; 5580 nvlist_t *errors = NULL; 5581 nvlist_t *recvprops = NULL; 5582 nvlist_t *localprops = NULL; 5583 nvlist_t *hidden_args = NULL; 5584 const char *snapname; 5585 const char *origin = NULL; 5586 char *tosnap; 5587 char tofs[ZFS_MAX_DATASET_NAME_LEN]; 5588 boolean_t force; 5589 boolean_t heal; 5590 boolean_t resumable; 5591 uint64_t read_bytes = 0; 5592 uint64_t errflags = 0; 5593 int input_fd = -1; 5594 int error; 5595 5596 snapname = fnvlist_lookup_string(innvl, "snapname"); 5597 5598 if (dataset_namecheck(snapname, NULL, NULL) != 0 || 5599 strchr(snapname, '@') == NULL || 5600 strchr(snapname, '%') != NULL) { 5601 return (SET_ERROR(EINVAL)); 5602 } 5603 5604 (void) strlcpy(tofs, snapname, sizeof (tofs)); 5605 tosnap = strchr(tofs, '@'); 5606 *tosnap++ = '\0'; 5607 5608 error = nvlist_lookup_string(innvl, "origin", &origin); 5609 if (error && error != ENOENT) 5610 return (error); 5611 5612 error = nvlist_lookup_byte_array(innvl, "begin_record", 5613 (uchar_t **)&begin_record, &begin_record_size); 5614 if (error != 0 || begin_record_size != sizeof (*begin_record)) 5615 return (SET_ERROR(EINVAL)); 5616 5617 input_fd = fnvlist_lookup_int32(innvl, "input_fd"); 5618 5619 force = nvlist_exists(innvl, "force"); 5620 heal = nvlist_exists(innvl, "heal"); 5621 resumable = nvlist_exists(innvl, "resumable"); 5622 5623 /* we still use "props" here for backwards compatibility */ 5624 error = nvlist_lookup_nvlist(innvl, "props", &recvprops); 5625 if (error && error != ENOENT) 5626 goto out; 5627 5628 error = nvlist_lookup_nvlist(innvl, "localprops", &localprops); 5629 if (error && error != ENOENT) 5630 goto out; 5631 5632 error = nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args); 5633 if (error && error != ENOENT) 5634 goto out; 5635 5636 error = zfs_ioc_recv_impl(tofs, tosnap, origin, recvprops, localprops, 5637 hidden_args, force, heal, resumable, input_fd, begin_record, 5638 &read_bytes, &errflags, &errors); 5639 5640 fnvlist_add_uint64(outnvl, "read_bytes", read_bytes); 5641 fnvlist_add_uint64(outnvl, "error_flags", errflags); 5642 fnvlist_add_nvlist(outnvl, "errors", errors); 5643 5644 out: 5645 nvlist_free(errors); 5646 nvlist_free(recvprops); 5647 nvlist_free(localprops); 5648 nvlist_free(hidden_args); 5649 5650 return (error); 5651 } 5652 5653 /* 5654 * When stack space is limited, we write replication stream data to the target 5655 * on a separate taskq thread, to make sure there's enough stack space. 5656 */ 5657 #ifndef HAVE_LARGE_STACKS 5658 #define USE_SEND_TASKQ 1 5659 #endif 5660 5661 typedef struct dump_bytes_io { 5662 zfs_file_t *dbi_fp; 5663 caddr_t dbi_buf; 5664 int dbi_len; 5665 int dbi_err; 5666 } dump_bytes_io_t; 5667 5668 static void 5669 dump_bytes_cb(void *arg) 5670 { 5671 dump_bytes_io_t *dbi = (dump_bytes_io_t *)arg; 5672 zfs_file_t *fp; 5673 caddr_t buf; 5674 5675 fp = dbi->dbi_fp; 5676 buf = dbi->dbi_buf; 5677 5678 dbi->dbi_err = zfs_file_write(fp, buf, dbi->dbi_len, NULL); 5679 } 5680 5681 typedef struct dump_bytes_arg { 5682 zfs_file_t *dba_fp; 5683 #ifdef USE_SEND_TASKQ 5684 taskq_t *dba_tq; 5685 taskq_ent_t dba_tqent; 5686 #endif 5687 } dump_bytes_arg_t; 5688 5689 static int 5690 dump_bytes(objset_t *os, void *buf, int len, void *arg) 5691 { 5692 dump_bytes_arg_t *dba = (dump_bytes_arg_t *)arg; 5693 dump_bytes_io_t dbi; 5694 5695 dbi.dbi_fp = dba->dba_fp; 5696 dbi.dbi_buf = buf; 5697 dbi.dbi_len = len; 5698 5699 #ifdef USE_SEND_TASKQ 5700 taskq_dispatch_ent(dba->dba_tq, dump_bytes_cb, &dbi, TQ_SLEEP, 5701 &dba->dba_tqent); 5702 taskq_wait(dba->dba_tq); 5703 #else 5704 dump_bytes_cb(&dbi); 5705 #endif 5706 5707 return (dbi.dbi_err); 5708 } 5709 5710 static int 5711 dump_bytes_init(dump_bytes_arg_t *dba, int fd, dmu_send_outparams_t *out) 5712 { 5713 zfs_file_t *fp = zfs_file_get(fd); 5714 if (fp == NULL) 5715 return (SET_ERROR(EBADF)); 5716 5717 dba->dba_fp = fp; 5718 #ifdef USE_SEND_TASKQ 5719 dba->dba_tq = taskq_create("z_send", 1, defclsyspri, 0, 0, 0); 5720 taskq_init_ent(&dba->dba_tqent); 5721 #endif 5722 5723 memset(out, 0, sizeof (dmu_send_outparams_t)); 5724 out->dso_outfunc = dump_bytes; 5725 out->dso_arg = dba; 5726 out->dso_dryrun = B_FALSE; 5727 5728 return (0); 5729 } 5730 5731 static void 5732 dump_bytes_fini(dump_bytes_arg_t *dba) 5733 { 5734 zfs_file_put(dba->dba_fp); 5735 #ifdef USE_SEND_TASKQ 5736 taskq_destroy(dba->dba_tq); 5737 #endif 5738 } 5739 5740 /* 5741 * inputs: 5742 * zc_name name of snapshot to send 5743 * zc_cookie file descriptor to send stream to 5744 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj) 5745 * zc_sendobj objsetid of snapshot to send 5746 * zc_fromobj objsetid of incremental fromsnap (may be zero) 5747 * zc_guid if set, estimate size of stream only. zc_cookie is ignored. 5748 * output size in zc_objset_type. 5749 * zc_flags lzc_send_flags 5750 * 5751 * outputs: 5752 * zc_objset_type estimated size, if zc_guid is set 5753 * 5754 * NOTE: This is no longer the preferred interface, any new functionality 5755 * should be added to zfs_ioc_send_new() instead. 5756 */ 5757 static int 5758 zfs_ioc_send(zfs_cmd_t *zc) 5759 { 5760 int error; 5761 offset_t off; 5762 boolean_t estimate = (zc->zc_guid != 0); 5763 boolean_t embedok = (zc->zc_flags & 0x1); 5764 boolean_t large_block_ok = (zc->zc_flags & 0x2); 5765 boolean_t compressok = (zc->zc_flags & 0x4); 5766 boolean_t rawok = (zc->zc_flags & 0x8); 5767 boolean_t savedok = (zc->zc_flags & 0x10); 5768 5769 if (zc->zc_obj != 0) { 5770 dsl_pool_t *dp; 5771 dsl_dataset_t *tosnap; 5772 5773 error = dsl_pool_hold(zc->zc_name, FTAG, &dp); 5774 if (error != 0) 5775 return (error); 5776 5777 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap); 5778 if (error != 0) { 5779 dsl_pool_rele(dp, FTAG); 5780 return (error); 5781 } 5782 5783 if (dsl_dir_is_clone(tosnap->ds_dir)) 5784 zc->zc_fromobj = 5785 dsl_dir_phys(tosnap->ds_dir)->dd_origin_obj; 5786 dsl_dataset_rele(tosnap, FTAG); 5787 dsl_pool_rele(dp, FTAG); 5788 } 5789 5790 if (estimate) { 5791 dsl_pool_t *dp; 5792 dsl_dataset_t *tosnap; 5793 dsl_dataset_t *fromsnap = NULL; 5794 5795 error = dsl_pool_hold(zc->zc_name, FTAG, &dp); 5796 if (error != 0) 5797 return (error); 5798 5799 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, 5800 FTAG, &tosnap); 5801 if (error != 0) { 5802 dsl_pool_rele(dp, FTAG); 5803 return (error); 5804 } 5805 5806 if (zc->zc_fromobj != 0) { 5807 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj, 5808 FTAG, &fromsnap); 5809 if (error != 0) { 5810 dsl_dataset_rele(tosnap, FTAG); 5811 dsl_pool_rele(dp, FTAG); 5812 return (error); 5813 } 5814 } 5815 5816 error = dmu_send_estimate_fast(tosnap, fromsnap, NULL, 5817 compressok || rawok, savedok, &zc->zc_objset_type); 5818 5819 if (fromsnap != NULL) 5820 dsl_dataset_rele(fromsnap, FTAG); 5821 dsl_dataset_rele(tosnap, FTAG); 5822 dsl_pool_rele(dp, FTAG); 5823 } else { 5824 dump_bytes_arg_t dba; 5825 dmu_send_outparams_t out; 5826 error = dump_bytes_init(&dba, zc->zc_cookie, &out); 5827 if (error) 5828 return (error); 5829 5830 off = zfs_file_off(dba.dba_fp); 5831 error = dmu_send_obj(zc->zc_name, zc->zc_sendobj, 5832 zc->zc_fromobj, embedok, large_block_ok, compressok, 5833 rawok, savedok, zc->zc_cookie, &off, &out); 5834 5835 dump_bytes_fini(&dba); 5836 } 5837 return (error); 5838 } 5839 5840 /* 5841 * inputs: 5842 * zc_name name of snapshot on which to report progress 5843 * zc_cookie file descriptor of send stream 5844 * 5845 * outputs: 5846 * zc_cookie number of bytes written in send stream thus far 5847 * zc_objset_type logical size of data traversed by send thus far 5848 */ 5849 static int 5850 zfs_ioc_send_progress(zfs_cmd_t *zc) 5851 { 5852 dsl_pool_t *dp; 5853 dsl_dataset_t *ds; 5854 dmu_sendstatus_t *dsp = NULL; 5855 int error; 5856 5857 error = dsl_pool_hold(zc->zc_name, FTAG, &dp); 5858 if (error != 0) 5859 return (error); 5860 5861 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds); 5862 if (error != 0) { 5863 dsl_pool_rele(dp, FTAG); 5864 return (error); 5865 } 5866 5867 mutex_enter(&ds->ds_sendstream_lock); 5868 5869 /* 5870 * Iterate over all the send streams currently active on this dataset. 5871 * If there's one which matches the specified file descriptor _and_ the 5872 * stream was started by the current process, return the progress of 5873 * that stream. 5874 */ 5875 5876 for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL; 5877 dsp = list_next(&ds->ds_sendstreams, dsp)) { 5878 if (dsp->dss_outfd == zc->zc_cookie && 5879 zfs_proc_is_caller(dsp->dss_proc)) 5880 break; 5881 } 5882 5883 if (dsp != NULL) { 5884 zc->zc_cookie = atomic_cas_64((volatile uint64_t *)dsp->dss_off, 5885 0, 0); 5886 /* This is the closest thing we have to atomic_read_64. */ 5887 zc->zc_objset_type = atomic_cas_64(&dsp->dss_blocks, 0, 0); 5888 } else { 5889 error = SET_ERROR(ENOENT); 5890 } 5891 5892 mutex_exit(&ds->ds_sendstream_lock); 5893 dsl_dataset_rele(ds, FTAG); 5894 dsl_pool_rele(dp, FTAG); 5895 return (error); 5896 } 5897 5898 static int 5899 zfs_ioc_inject_fault(zfs_cmd_t *zc) 5900 { 5901 int id, error; 5902 5903 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id, 5904 &zc->zc_inject_record); 5905 5906 if (error == 0) 5907 zc->zc_guid = (uint64_t)id; 5908 5909 return (error); 5910 } 5911 5912 static int 5913 zfs_ioc_clear_fault(zfs_cmd_t *zc) 5914 { 5915 return (zio_clear_fault((int)zc->zc_guid)); 5916 } 5917 5918 static int 5919 zfs_ioc_inject_list_next(zfs_cmd_t *zc) 5920 { 5921 int id = (int)zc->zc_guid; 5922 int error; 5923 5924 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name), 5925 &zc->zc_inject_record); 5926 5927 zc->zc_guid = id; 5928 5929 return (error); 5930 } 5931 5932 static int 5933 zfs_ioc_error_log(zfs_cmd_t *zc) 5934 { 5935 spa_t *spa; 5936 int error; 5937 5938 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 5939 return (error); 5940 5941 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst, 5942 &zc->zc_nvlist_dst_size); 5943 5944 spa_close(spa, FTAG); 5945 5946 return (error); 5947 } 5948 5949 static int 5950 zfs_ioc_clear(zfs_cmd_t *zc) 5951 { 5952 spa_t *spa; 5953 vdev_t *vd; 5954 int error; 5955 5956 /* 5957 * On zpool clear we also fix up missing slogs 5958 */ 5959 mutex_enter(&spa_namespace_lock); 5960 spa = spa_lookup(zc->zc_name); 5961 if (spa == NULL) { 5962 mutex_exit(&spa_namespace_lock); 5963 return (SET_ERROR(EIO)); 5964 } 5965 if (spa_get_log_state(spa) == SPA_LOG_MISSING) { 5966 /* we need to let spa_open/spa_load clear the chains */ 5967 spa_set_log_state(spa, SPA_LOG_CLEAR); 5968 } 5969 spa->spa_last_open_failed = 0; 5970 mutex_exit(&spa_namespace_lock); 5971 5972 if (zc->zc_cookie & ZPOOL_NO_REWIND) { 5973 error = spa_open(zc->zc_name, &spa, FTAG); 5974 } else { 5975 nvlist_t *policy; 5976 nvlist_t *config = NULL; 5977 5978 if (zc->zc_nvlist_src == 0) 5979 return (SET_ERROR(EINVAL)); 5980 5981 if ((error = get_nvlist(zc->zc_nvlist_src, 5982 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) { 5983 error = spa_open_rewind(zc->zc_name, &spa, FTAG, 5984 policy, &config); 5985 if (config != NULL) { 5986 int err; 5987 5988 if ((err = put_nvlist(zc, config)) != 0) 5989 error = err; 5990 nvlist_free(config); 5991 } 5992 nvlist_free(policy); 5993 } 5994 } 5995 5996 if (error != 0) 5997 return (error); 5998 5999 /* 6000 * If multihost is enabled, resuming I/O is unsafe as another 6001 * host may have imported the pool. Check for remote activity. 6002 */ 6003 if (spa_multihost(spa) && spa_suspended(spa) && 6004 spa_mmp_remote_host_activity(spa)) { 6005 spa_close(spa, FTAG); 6006 return (SET_ERROR(EREMOTEIO)); 6007 } 6008 6009 spa_vdev_state_enter(spa, SCL_NONE); 6010 6011 if (zc->zc_guid == 0) { 6012 vd = NULL; 6013 } else { 6014 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE); 6015 if (vd == NULL) { 6016 error = SET_ERROR(ENODEV); 6017 (void) spa_vdev_state_exit(spa, NULL, error); 6018 spa_close(spa, FTAG); 6019 return (error); 6020 } 6021 } 6022 6023 vdev_clear(spa, vd); 6024 6025 (void) spa_vdev_state_exit(spa, spa_suspended(spa) ? 6026 NULL : spa->spa_root_vdev, 0); 6027 6028 /* 6029 * Resume any suspended I/Os. 6030 */ 6031 if (zio_resume(spa) != 0) 6032 error = SET_ERROR(EIO); 6033 6034 spa_close(spa, FTAG); 6035 6036 return (error); 6037 } 6038 6039 /* 6040 * Reopen all the vdevs associated with the pool. 6041 * 6042 * innvl: { 6043 * "scrub_restart" -> when true and scrub is running, allow to restart 6044 * scrub as the side effect of the reopen (boolean). 6045 * } 6046 * 6047 * outnvl is unused 6048 */ 6049 static const zfs_ioc_key_t zfs_keys_pool_reopen[] = { 6050 {"scrub_restart", DATA_TYPE_BOOLEAN_VALUE, ZK_OPTIONAL}, 6051 }; 6052 6053 static int 6054 zfs_ioc_pool_reopen(const char *pool, nvlist_t *innvl, nvlist_t *outnvl) 6055 { 6056 (void) outnvl; 6057 spa_t *spa; 6058 int error; 6059 boolean_t rc, scrub_restart = B_TRUE; 6060 6061 if (innvl) { 6062 error = nvlist_lookup_boolean_value(innvl, 6063 "scrub_restart", &rc); 6064 if (error == 0) 6065 scrub_restart = rc; 6066 } 6067 6068 error = spa_open(pool, &spa, FTAG); 6069 if (error != 0) 6070 return (error); 6071 6072 spa_vdev_state_enter(spa, SCL_NONE); 6073 6074 /* 6075 * If the scrub_restart flag is B_FALSE and a scrub is already 6076 * in progress then set spa_scrub_reopen flag to B_TRUE so that 6077 * we don't restart the scrub as a side effect of the reopen. 6078 * Otherwise, let vdev_open() decided if a resilver is required. 6079 */ 6080 6081 spa->spa_scrub_reopen = (!scrub_restart && 6082 dsl_scan_scrubbing(spa->spa_dsl_pool)); 6083 vdev_reopen(spa->spa_root_vdev); 6084 spa->spa_scrub_reopen = B_FALSE; 6085 6086 (void) spa_vdev_state_exit(spa, NULL, 0); 6087 spa_close(spa, FTAG); 6088 return (0); 6089 } 6090 6091 /* 6092 * inputs: 6093 * zc_name name of filesystem 6094 * 6095 * outputs: 6096 * zc_string name of conflicting snapshot, if there is one 6097 */ 6098 static int 6099 zfs_ioc_promote(zfs_cmd_t *zc) 6100 { 6101 dsl_pool_t *dp; 6102 dsl_dataset_t *ds, *ods; 6103 char origin[ZFS_MAX_DATASET_NAME_LEN]; 6104 char *cp; 6105 int error; 6106 6107 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0'; 6108 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 || 6109 strchr(zc->zc_name, '%')) 6110 return (SET_ERROR(EINVAL)); 6111 6112 error = dsl_pool_hold(zc->zc_name, FTAG, &dp); 6113 if (error != 0) 6114 return (error); 6115 6116 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds); 6117 if (error != 0) { 6118 dsl_pool_rele(dp, FTAG); 6119 return (error); 6120 } 6121 6122 if (!dsl_dir_is_clone(ds->ds_dir)) { 6123 dsl_dataset_rele(ds, FTAG); 6124 dsl_pool_rele(dp, FTAG); 6125 return (SET_ERROR(EINVAL)); 6126 } 6127 6128 error = dsl_dataset_hold_obj(dp, 6129 dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &ods); 6130 if (error != 0) { 6131 dsl_dataset_rele(ds, FTAG); 6132 dsl_pool_rele(dp, FTAG); 6133 return (error); 6134 } 6135 6136 dsl_dataset_name(ods, origin); 6137 dsl_dataset_rele(ods, FTAG); 6138 dsl_dataset_rele(ds, FTAG); 6139 dsl_pool_rele(dp, FTAG); 6140 6141 /* 6142 * We don't need to unmount *all* the origin fs's snapshots, but 6143 * it's easier. 6144 */ 6145 cp = strchr(origin, '@'); 6146 if (cp) 6147 *cp = '\0'; 6148 (void) dmu_objset_find(origin, 6149 zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS); 6150 return (dsl_dataset_promote(zc->zc_name, zc->zc_string)); 6151 } 6152 6153 /* 6154 * Retrieve a single {user|group|project}{used|quota}@... property. 6155 * 6156 * inputs: 6157 * zc_name name of filesystem 6158 * zc_objset_type zfs_userquota_prop_t 6159 * zc_value domain name (eg. "S-1-234-567-89") 6160 * zc_guid RID/UID/GID 6161 * 6162 * outputs: 6163 * zc_cookie property value 6164 */ 6165 static int 6166 zfs_ioc_userspace_one(zfs_cmd_t *zc) 6167 { 6168 zfsvfs_t *zfsvfs; 6169 int error; 6170 6171 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) 6172 return (SET_ERROR(EINVAL)); 6173 6174 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE); 6175 if (error != 0) 6176 return (error); 6177 6178 error = zfs_userspace_one(zfsvfs, 6179 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie); 6180 zfsvfs_rele(zfsvfs, FTAG); 6181 6182 return (error); 6183 } 6184 6185 /* 6186 * inputs: 6187 * zc_name name of filesystem 6188 * zc_cookie zap cursor 6189 * zc_objset_type zfs_userquota_prop_t 6190 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist) 6191 * 6192 * outputs: 6193 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t) 6194 * zc_cookie zap cursor 6195 */ 6196 static int 6197 zfs_ioc_userspace_many(zfs_cmd_t *zc) 6198 { 6199 zfsvfs_t *zfsvfs; 6200 int bufsize = zc->zc_nvlist_dst_size; 6201 6202 if (bufsize <= 0) 6203 return (SET_ERROR(ENOMEM)); 6204 6205 int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE); 6206 if (error != 0) 6207 return (error); 6208 6209 void *buf = vmem_alloc(bufsize, KM_SLEEP); 6210 6211 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie, 6212 buf, &zc->zc_nvlist_dst_size); 6213 6214 if (error == 0) { 6215 error = xcopyout(buf, 6216 (void *)(uintptr_t)zc->zc_nvlist_dst, 6217 zc->zc_nvlist_dst_size); 6218 } 6219 vmem_free(buf, bufsize); 6220 zfsvfs_rele(zfsvfs, FTAG); 6221 6222 return (error); 6223 } 6224 6225 /* 6226 * inputs: 6227 * zc_name name of filesystem 6228 * 6229 * outputs: 6230 * none 6231 */ 6232 static int 6233 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc) 6234 { 6235 int error = 0; 6236 zfsvfs_t *zfsvfs; 6237 6238 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) { 6239 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) { 6240 /* 6241 * If userused is not enabled, it may be because the 6242 * objset needs to be closed & reopened (to grow the 6243 * objset_phys_t). Suspend/resume the fs will do that. 6244 */ 6245 dsl_dataset_t *ds, *newds; 6246 6247 ds = dmu_objset_ds(zfsvfs->z_os); 6248 error = zfs_suspend_fs(zfsvfs); 6249 if (error == 0) { 6250 dmu_objset_refresh_ownership(ds, &newds, 6251 B_TRUE, zfsvfs); 6252 error = zfs_resume_fs(zfsvfs, newds); 6253 } 6254 } 6255 if (error == 0) { 6256 mutex_enter(&zfsvfs->z_os->os_upgrade_lock); 6257 if (zfsvfs->z_os->os_upgrade_id == 0) { 6258 /* clear potential error code and retry */ 6259 zfsvfs->z_os->os_upgrade_status = 0; 6260 mutex_exit(&zfsvfs->z_os->os_upgrade_lock); 6261 6262 dsl_pool_config_enter( 6263 dmu_objset_pool(zfsvfs->z_os), FTAG); 6264 dmu_objset_userspace_upgrade(zfsvfs->z_os); 6265 dsl_pool_config_exit( 6266 dmu_objset_pool(zfsvfs->z_os), FTAG); 6267 } else { 6268 mutex_exit(&zfsvfs->z_os->os_upgrade_lock); 6269 } 6270 6271 taskq_wait_id(zfsvfs->z_os->os_spa->spa_upgrade_taskq, 6272 zfsvfs->z_os->os_upgrade_id); 6273 error = zfsvfs->z_os->os_upgrade_status; 6274 } 6275 zfs_vfs_rele(zfsvfs); 6276 } else { 6277 objset_t *os; 6278 6279 /* XXX kind of reading contents without owning */ 6280 error = dmu_objset_hold_flags(zc->zc_name, B_TRUE, FTAG, &os); 6281 if (error != 0) 6282 return (error); 6283 6284 mutex_enter(&os->os_upgrade_lock); 6285 if (os->os_upgrade_id == 0) { 6286 /* clear potential error code and retry */ 6287 os->os_upgrade_status = 0; 6288 mutex_exit(&os->os_upgrade_lock); 6289 6290 dmu_objset_userspace_upgrade(os); 6291 } else { 6292 mutex_exit(&os->os_upgrade_lock); 6293 } 6294 6295 dsl_pool_rele(dmu_objset_pool(os), FTAG); 6296 6297 taskq_wait_id(os->os_spa->spa_upgrade_taskq, os->os_upgrade_id); 6298 error = os->os_upgrade_status; 6299 6300 dsl_dataset_rele_flags(dmu_objset_ds(os), DS_HOLD_FLAG_DECRYPT, 6301 FTAG); 6302 } 6303 return (error); 6304 } 6305 6306 /* 6307 * inputs: 6308 * zc_name name of filesystem 6309 * 6310 * outputs: 6311 * none 6312 */ 6313 static int 6314 zfs_ioc_id_quota_upgrade(zfs_cmd_t *zc) 6315 { 6316 objset_t *os; 6317 int error; 6318 6319 error = dmu_objset_hold_flags(zc->zc_name, B_TRUE, FTAG, &os); 6320 if (error != 0) 6321 return (error); 6322 6323 if (dmu_objset_userobjspace_upgradable(os) || 6324 dmu_objset_projectquota_upgradable(os)) { 6325 mutex_enter(&os->os_upgrade_lock); 6326 if (os->os_upgrade_id == 0) { 6327 /* clear potential error code and retry */ 6328 os->os_upgrade_status = 0; 6329 mutex_exit(&os->os_upgrade_lock); 6330 6331 dmu_objset_id_quota_upgrade(os); 6332 } else { 6333 mutex_exit(&os->os_upgrade_lock); 6334 } 6335 6336 dsl_pool_rele(dmu_objset_pool(os), FTAG); 6337 6338 taskq_wait_id(os->os_spa->spa_upgrade_taskq, os->os_upgrade_id); 6339 error = os->os_upgrade_status; 6340 } else { 6341 dsl_pool_rele(dmu_objset_pool(os), FTAG); 6342 } 6343 6344 dsl_dataset_rele_flags(dmu_objset_ds(os), DS_HOLD_FLAG_DECRYPT, FTAG); 6345 6346 return (error); 6347 } 6348 6349 static int 6350 zfs_ioc_share(zfs_cmd_t *zc) 6351 { 6352 return (SET_ERROR(ENOSYS)); 6353 } 6354 6355 /* 6356 * inputs: 6357 * zc_name name of containing filesystem 6358 * zc_obj object # beyond which we want next in-use object # 6359 * 6360 * outputs: 6361 * zc_obj next in-use object # 6362 */ 6363 static int 6364 zfs_ioc_next_obj(zfs_cmd_t *zc) 6365 { 6366 objset_t *os = NULL; 6367 int error; 6368 6369 error = dmu_objset_hold(zc->zc_name, FTAG, &os); 6370 if (error != 0) 6371 return (error); 6372 6373 error = dmu_object_next(os, &zc->zc_obj, B_FALSE, 0); 6374 6375 dmu_objset_rele(os, FTAG); 6376 return (error); 6377 } 6378 6379 /* 6380 * inputs: 6381 * zc_name name of filesystem 6382 * zc_value prefix name for snapshot 6383 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process 6384 * 6385 * outputs: 6386 * zc_value short name of new snapshot 6387 */ 6388 static int 6389 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc) 6390 { 6391 char *snap_name; 6392 char *hold_name; 6393 minor_t minor; 6394 6395 zfs_file_t *fp = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor); 6396 if (fp == NULL) 6397 return (SET_ERROR(EBADF)); 6398 6399 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value, 6400 (u_longlong_t)ddi_get_lbolt64()); 6401 hold_name = kmem_asprintf("%%%s", zc->zc_value); 6402 6403 int error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor, 6404 hold_name); 6405 if (error == 0) 6406 (void) strlcpy(zc->zc_value, snap_name, 6407 sizeof (zc->zc_value)); 6408 kmem_strfree(snap_name); 6409 kmem_strfree(hold_name); 6410 zfs_onexit_fd_rele(fp); 6411 return (error); 6412 } 6413 6414 /* 6415 * inputs: 6416 * zc_name name of "to" snapshot 6417 * zc_value name of "from" snapshot 6418 * zc_cookie file descriptor to write diff data on 6419 * 6420 * outputs: 6421 * dmu_diff_record_t's to the file descriptor 6422 */ 6423 static int 6424 zfs_ioc_diff(zfs_cmd_t *zc) 6425 { 6426 zfs_file_t *fp; 6427 offset_t off; 6428 int error; 6429 6430 if ((fp = zfs_file_get(zc->zc_cookie)) == NULL) 6431 return (SET_ERROR(EBADF)); 6432 6433 off = zfs_file_off(fp); 6434 error = dmu_diff(zc->zc_name, zc->zc_value, fp, &off); 6435 6436 zfs_file_put(fp); 6437 6438 return (error); 6439 } 6440 6441 static int 6442 zfs_ioc_smb_acl(zfs_cmd_t *zc) 6443 { 6444 return (SET_ERROR(ENOTSUP)); 6445 } 6446 6447 /* 6448 * innvl: { 6449 * "holds" -> { snapname -> holdname (string), ... } 6450 * (optional) "cleanup_fd" -> fd (int32) 6451 * } 6452 * 6453 * outnvl: { 6454 * snapname -> error value (int32) 6455 * ... 6456 * } 6457 */ 6458 static const zfs_ioc_key_t zfs_keys_hold[] = { 6459 {"holds", DATA_TYPE_NVLIST, 0}, 6460 {"cleanup_fd", DATA_TYPE_INT32, ZK_OPTIONAL}, 6461 }; 6462 6463 static int 6464 zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist) 6465 { 6466 (void) pool; 6467 nvpair_t *pair; 6468 nvlist_t *holds; 6469 int cleanup_fd = -1; 6470 int error; 6471 minor_t minor = 0; 6472 zfs_file_t *fp = NULL; 6473 6474 holds = fnvlist_lookup_nvlist(args, "holds"); 6475 6476 /* make sure the user didn't pass us any invalid (empty) tags */ 6477 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL; 6478 pair = nvlist_next_nvpair(holds, pair)) { 6479 const char *htag; 6480 6481 error = nvpair_value_string(pair, &htag); 6482 if (error != 0) 6483 return (SET_ERROR(error)); 6484 6485 if (strlen(htag) == 0) 6486 return (SET_ERROR(EINVAL)); 6487 } 6488 6489 if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) { 6490 fp = zfs_onexit_fd_hold(cleanup_fd, &minor); 6491 if (fp == NULL) 6492 return (SET_ERROR(EBADF)); 6493 } 6494 6495 error = dsl_dataset_user_hold(holds, minor, errlist); 6496 if (fp != NULL) { 6497 ASSERT3U(minor, !=, 0); 6498 zfs_onexit_fd_rele(fp); 6499 } 6500 return (SET_ERROR(error)); 6501 } 6502 6503 /* 6504 * innvl is not used. 6505 * 6506 * outnvl: { 6507 * holdname -> time added (uint64 seconds since epoch) 6508 * ... 6509 * } 6510 */ 6511 static const zfs_ioc_key_t zfs_keys_get_holds[] = { 6512 /* no nvl keys */ 6513 }; 6514 6515 static int 6516 zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl) 6517 { 6518 (void) args; 6519 return (dsl_dataset_get_holds(snapname, outnvl)); 6520 } 6521 6522 /* 6523 * innvl: { 6524 * snapname -> { holdname, ... } 6525 * ... 6526 * } 6527 * 6528 * outnvl: { 6529 * snapname -> error value (int32) 6530 * ... 6531 * } 6532 */ 6533 static const zfs_ioc_key_t zfs_keys_release[] = { 6534 {"<snapname>...", DATA_TYPE_NVLIST, ZK_WILDCARDLIST}, 6535 }; 6536 6537 static int 6538 zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist) 6539 { 6540 (void) pool; 6541 return (dsl_dataset_user_release(holds, errlist)); 6542 } 6543 6544 /* 6545 * inputs: 6546 * zc_guid flags (ZEVENT_NONBLOCK) 6547 * zc_cleanup_fd zevent file descriptor 6548 * 6549 * outputs: 6550 * zc_nvlist_dst next nvlist event 6551 * zc_cookie dropped events since last get 6552 */ 6553 static int 6554 zfs_ioc_events_next(zfs_cmd_t *zc) 6555 { 6556 zfs_zevent_t *ze; 6557 nvlist_t *event = NULL; 6558 minor_t minor; 6559 uint64_t dropped = 0; 6560 int error; 6561 6562 zfs_file_t *fp = zfs_zevent_fd_hold(zc->zc_cleanup_fd, &minor, &ze); 6563 if (fp == NULL) 6564 return (SET_ERROR(EBADF)); 6565 6566 do { 6567 error = zfs_zevent_next(ze, &event, 6568 &zc->zc_nvlist_dst_size, &dropped); 6569 if (event != NULL) { 6570 zc->zc_cookie = dropped; 6571 error = put_nvlist(zc, event); 6572 nvlist_free(event); 6573 } 6574 6575 if (zc->zc_guid & ZEVENT_NONBLOCK) 6576 break; 6577 6578 if ((error == 0) || (error != ENOENT)) 6579 break; 6580 6581 error = zfs_zevent_wait(ze); 6582 if (error != 0) 6583 break; 6584 } while (1); 6585 6586 zfs_zevent_fd_rele(fp); 6587 6588 return (error); 6589 } 6590 6591 /* 6592 * outputs: 6593 * zc_cookie cleared events count 6594 */ 6595 static int 6596 zfs_ioc_events_clear(zfs_cmd_t *zc) 6597 { 6598 uint_t count; 6599 6600 zfs_zevent_drain_all(&count); 6601 zc->zc_cookie = count; 6602 6603 return (0); 6604 } 6605 6606 /* 6607 * inputs: 6608 * zc_guid eid | ZEVENT_SEEK_START | ZEVENT_SEEK_END 6609 * zc_cleanup zevent file descriptor 6610 */ 6611 static int 6612 zfs_ioc_events_seek(zfs_cmd_t *zc) 6613 { 6614 zfs_zevent_t *ze; 6615 minor_t minor; 6616 int error; 6617 6618 zfs_file_t *fp = zfs_zevent_fd_hold(zc->zc_cleanup_fd, &minor, &ze); 6619 if (fp == NULL) 6620 return (SET_ERROR(EBADF)); 6621 6622 error = zfs_zevent_seek(ze, zc->zc_guid); 6623 zfs_zevent_fd_rele(fp); 6624 6625 return (error); 6626 } 6627 6628 /* 6629 * inputs: 6630 * zc_name name of later filesystem or snapshot 6631 * zc_value full name of old snapshot or bookmark 6632 * 6633 * outputs: 6634 * zc_cookie space in bytes 6635 * zc_objset_type compressed space in bytes 6636 * zc_perm_action uncompressed space in bytes 6637 */ 6638 static int 6639 zfs_ioc_space_written(zfs_cmd_t *zc) 6640 { 6641 int error; 6642 dsl_pool_t *dp; 6643 dsl_dataset_t *new; 6644 6645 error = dsl_pool_hold(zc->zc_name, FTAG, &dp); 6646 if (error != 0) 6647 return (error); 6648 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new); 6649 if (error != 0) { 6650 dsl_pool_rele(dp, FTAG); 6651 return (error); 6652 } 6653 if (strchr(zc->zc_value, '#') != NULL) { 6654 zfs_bookmark_phys_t bmp; 6655 error = dsl_bookmark_lookup(dp, zc->zc_value, 6656 new, &bmp); 6657 if (error == 0) { 6658 error = dsl_dataset_space_written_bookmark(&bmp, new, 6659 &zc->zc_cookie, 6660 &zc->zc_objset_type, &zc->zc_perm_action); 6661 } 6662 } else { 6663 dsl_dataset_t *old; 6664 error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old); 6665 6666 if (error == 0) { 6667 error = dsl_dataset_space_written(old, new, 6668 &zc->zc_cookie, 6669 &zc->zc_objset_type, &zc->zc_perm_action); 6670 dsl_dataset_rele(old, FTAG); 6671 } 6672 } 6673 dsl_dataset_rele(new, FTAG); 6674 dsl_pool_rele(dp, FTAG); 6675 return (error); 6676 } 6677 6678 /* 6679 * innvl: { 6680 * "firstsnap" -> snapshot name 6681 * } 6682 * 6683 * outnvl: { 6684 * "used" -> space in bytes 6685 * "compressed" -> compressed space in bytes 6686 * "uncompressed" -> uncompressed space in bytes 6687 * } 6688 */ 6689 static const zfs_ioc_key_t zfs_keys_space_snaps[] = { 6690 {"firstsnap", DATA_TYPE_STRING, 0}, 6691 }; 6692 6693 static int 6694 zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl) 6695 { 6696 int error; 6697 dsl_pool_t *dp; 6698 dsl_dataset_t *new, *old; 6699 const char *firstsnap; 6700 uint64_t used, comp, uncomp; 6701 6702 firstsnap = fnvlist_lookup_string(innvl, "firstsnap"); 6703 6704 error = dsl_pool_hold(lastsnap, FTAG, &dp); 6705 if (error != 0) 6706 return (error); 6707 6708 error = dsl_dataset_hold(dp, lastsnap, FTAG, &new); 6709 if (error == 0 && !new->ds_is_snapshot) { 6710 dsl_dataset_rele(new, FTAG); 6711 error = SET_ERROR(EINVAL); 6712 } 6713 if (error != 0) { 6714 dsl_pool_rele(dp, FTAG); 6715 return (error); 6716 } 6717 error = dsl_dataset_hold(dp, firstsnap, FTAG, &old); 6718 if (error == 0 && !old->ds_is_snapshot) { 6719 dsl_dataset_rele(old, FTAG); 6720 error = SET_ERROR(EINVAL); 6721 } 6722 if (error != 0) { 6723 dsl_dataset_rele(new, FTAG); 6724 dsl_pool_rele(dp, FTAG); 6725 return (error); 6726 } 6727 6728 error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp); 6729 dsl_dataset_rele(old, FTAG); 6730 dsl_dataset_rele(new, FTAG); 6731 dsl_pool_rele(dp, FTAG); 6732 fnvlist_add_uint64(outnvl, "used", used); 6733 fnvlist_add_uint64(outnvl, "compressed", comp); 6734 fnvlist_add_uint64(outnvl, "uncompressed", uncomp); 6735 return (error); 6736 } 6737 6738 /* 6739 * innvl: { 6740 * "fd" -> file descriptor to write stream to (int32) 6741 * (optional) "fromsnap" -> full snap name to send an incremental from 6742 * (optional) "largeblockok" -> (value ignored) 6743 * indicates that blocks > 128KB are permitted 6744 * (optional) "embedok" -> (value ignored) 6745 * presence indicates DRR_WRITE_EMBEDDED records are permitted 6746 * (optional) "compressok" -> (value ignored) 6747 * presence indicates compressed DRR_WRITE records are permitted 6748 * (optional) "rawok" -> (value ignored) 6749 * presence indicates raw encrypted records should be used. 6750 * (optional) "savedok" -> (value ignored) 6751 * presence indicates we should send a partially received snapshot 6752 * (optional) "resume_object" and "resume_offset" -> (uint64) 6753 * if present, resume send stream from specified object and offset. 6754 * (optional) "redactbook" -> (string) 6755 * if present, use this bookmark's redaction list to generate a redacted 6756 * send stream 6757 * } 6758 * 6759 * outnvl is unused 6760 */ 6761 static const zfs_ioc_key_t zfs_keys_send_new[] = { 6762 {"fd", DATA_TYPE_INT32, 0}, 6763 {"fromsnap", DATA_TYPE_STRING, ZK_OPTIONAL}, 6764 {"largeblockok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL}, 6765 {"embedok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL}, 6766 {"compressok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL}, 6767 {"rawok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL}, 6768 {"savedok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL}, 6769 {"resume_object", DATA_TYPE_UINT64, ZK_OPTIONAL}, 6770 {"resume_offset", DATA_TYPE_UINT64, ZK_OPTIONAL}, 6771 {"redactbook", DATA_TYPE_STRING, ZK_OPTIONAL}, 6772 }; 6773 6774 static int 6775 zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl) 6776 { 6777 (void) outnvl; 6778 int error; 6779 offset_t off; 6780 const char *fromname = NULL; 6781 int fd; 6782 boolean_t largeblockok; 6783 boolean_t embedok; 6784 boolean_t compressok; 6785 boolean_t rawok; 6786 boolean_t savedok; 6787 uint64_t resumeobj = 0; 6788 uint64_t resumeoff = 0; 6789 const char *redactbook = NULL; 6790 6791 fd = fnvlist_lookup_int32(innvl, "fd"); 6792 6793 (void) nvlist_lookup_string(innvl, "fromsnap", &fromname); 6794 6795 largeblockok = nvlist_exists(innvl, "largeblockok"); 6796 embedok = nvlist_exists(innvl, "embedok"); 6797 compressok = nvlist_exists(innvl, "compressok"); 6798 rawok = nvlist_exists(innvl, "rawok"); 6799 savedok = nvlist_exists(innvl, "savedok"); 6800 6801 (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj); 6802 (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff); 6803 6804 (void) nvlist_lookup_string(innvl, "redactbook", &redactbook); 6805 6806 dump_bytes_arg_t dba; 6807 dmu_send_outparams_t out; 6808 error = dump_bytes_init(&dba, fd, &out); 6809 if (error) 6810 return (error); 6811 6812 off = zfs_file_off(dba.dba_fp); 6813 error = dmu_send(snapname, fromname, embedok, largeblockok, 6814 compressok, rawok, savedok, resumeobj, resumeoff, 6815 redactbook, fd, &off, &out); 6816 6817 dump_bytes_fini(&dba); 6818 6819 return (error); 6820 } 6821 6822 static int 6823 send_space_sum(objset_t *os, void *buf, int len, void *arg) 6824 { 6825 (void) os, (void) buf; 6826 uint64_t *size = arg; 6827 6828 *size += len; 6829 return (0); 6830 } 6831 6832 /* 6833 * Determine approximately how large a zfs send stream will be -- the number 6834 * of bytes that will be written to the fd supplied to zfs_ioc_send_new(). 6835 * 6836 * innvl: { 6837 * (optional) "from" -> full snap or bookmark name to send an incremental 6838 * from 6839 * (optional) "largeblockok" -> (value ignored) 6840 * indicates that blocks > 128KB are permitted 6841 * (optional) "embedok" -> (value ignored) 6842 * presence indicates DRR_WRITE_EMBEDDED records are permitted 6843 * (optional) "compressok" -> (value ignored) 6844 * presence indicates compressed DRR_WRITE records are permitted 6845 * (optional) "rawok" -> (value ignored) 6846 * presence indicates raw encrypted records should be used. 6847 * (optional) "resume_object" and "resume_offset" -> (uint64) 6848 * if present, resume send stream from specified object and offset. 6849 * (optional) "fd" -> file descriptor to use as a cookie for progress 6850 * tracking (int32) 6851 * } 6852 * 6853 * outnvl: { 6854 * "space" -> bytes of space (uint64) 6855 * } 6856 */ 6857 static const zfs_ioc_key_t zfs_keys_send_space[] = { 6858 {"from", DATA_TYPE_STRING, ZK_OPTIONAL}, 6859 {"fromsnap", DATA_TYPE_STRING, ZK_OPTIONAL}, 6860 {"largeblockok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL}, 6861 {"embedok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL}, 6862 {"compressok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL}, 6863 {"rawok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL}, 6864 {"fd", DATA_TYPE_INT32, ZK_OPTIONAL}, 6865 {"redactbook", DATA_TYPE_STRING, ZK_OPTIONAL}, 6866 {"resume_object", DATA_TYPE_UINT64, ZK_OPTIONAL}, 6867 {"resume_offset", DATA_TYPE_UINT64, ZK_OPTIONAL}, 6868 {"bytes", DATA_TYPE_UINT64, ZK_OPTIONAL}, 6869 }; 6870 6871 static int 6872 zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl) 6873 { 6874 dsl_pool_t *dp; 6875 dsl_dataset_t *tosnap; 6876 dsl_dataset_t *fromsnap = NULL; 6877 int error; 6878 const char *fromname = NULL; 6879 const char *redactlist_book = NULL; 6880 boolean_t largeblockok; 6881 boolean_t embedok; 6882 boolean_t compressok; 6883 boolean_t rawok; 6884 boolean_t savedok; 6885 uint64_t space = 0; 6886 boolean_t full_estimate = B_FALSE; 6887 uint64_t resumeobj = 0; 6888 uint64_t resumeoff = 0; 6889 uint64_t resume_bytes = 0; 6890 int32_t fd = -1; 6891 zfs_bookmark_phys_t zbm = {0}; 6892 6893 error = dsl_pool_hold(snapname, FTAG, &dp); 6894 if (error != 0) 6895 return (error); 6896 6897 error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap); 6898 if (error != 0) { 6899 dsl_pool_rele(dp, FTAG); 6900 return (error); 6901 } 6902 (void) nvlist_lookup_int32(innvl, "fd", &fd); 6903 6904 largeblockok = nvlist_exists(innvl, "largeblockok"); 6905 embedok = nvlist_exists(innvl, "embedok"); 6906 compressok = nvlist_exists(innvl, "compressok"); 6907 rawok = nvlist_exists(innvl, "rawok"); 6908 savedok = nvlist_exists(innvl, "savedok"); 6909 boolean_t from = (nvlist_lookup_string(innvl, "from", &fromname) == 0); 6910 boolean_t altbook = (nvlist_lookup_string(innvl, "redactbook", 6911 &redactlist_book) == 0); 6912 6913 (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj); 6914 (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff); 6915 (void) nvlist_lookup_uint64(innvl, "bytes", &resume_bytes); 6916 6917 if (altbook) { 6918 full_estimate = B_TRUE; 6919 } else if (from) { 6920 if (strchr(fromname, '#')) { 6921 error = dsl_bookmark_lookup(dp, fromname, tosnap, &zbm); 6922 6923 /* 6924 * dsl_bookmark_lookup() will fail with EXDEV if 6925 * the from-bookmark and tosnap are at the same txg. 6926 * However, it's valid to do a send (and therefore, 6927 * a send estimate) from and to the same time point, 6928 * if the bookmark is redacted (the incremental send 6929 * can change what's redacted on the target). In 6930 * this case, dsl_bookmark_lookup() fills in zbm 6931 * but returns EXDEV. Ignore this error. 6932 */ 6933 if (error == EXDEV && zbm.zbm_redaction_obj != 0 && 6934 zbm.zbm_guid == 6935 dsl_dataset_phys(tosnap)->ds_guid) 6936 error = 0; 6937 6938 if (error != 0) { 6939 dsl_dataset_rele(tosnap, FTAG); 6940 dsl_pool_rele(dp, FTAG); 6941 return (error); 6942 } 6943 if (zbm.zbm_redaction_obj != 0 || !(zbm.zbm_flags & 6944 ZBM_FLAG_HAS_FBN)) { 6945 full_estimate = B_TRUE; 6946 } 6947 } else if (strchr(fromname, '@')) { 6948 error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap); 6949 if (error != 0) { 6950 dsl_dataset_rele(tosnap, FTAG); 6951 dsl_pool_rele(dp, FTAG); 6952 return (error); 6953 } 6954 6955 if (!dsl_dataset_is_before(tosnap, fromsnap, 0)) { 6956 full_estimate = B_TRUE; 6957 dsl_dataset_rele(fromsnap, FTAG); 6958 } 6959 } else { 6960 /* 6961 * from is not properly formatted as a snapshot or 6962 * bookmark 6963 */ 6964 dsl_dataset_rele(tosnap, FTAG); 6965 dsl_pool_rele(dp, FTAG); 6966 return (SET_ERROR(EINVAL)); 6967 } 6968 } 6969 6970 if (full_estimate) { 6971 dmu_send_outparams_t out = {0}; 6972 offset_t off = 0; 6973 out.dso_outfunc = send_space_sum; 6974 out.dso_arg = &space; 6975 out.dso_dryrun = B_TRUE; 6976 /* 6977 * We have to release these holds so dmu_send can take them. It 6978 * will do all the error checking we need. 6979 */ 6980 dsl_dataset_rele(tosnap, FTAG); 6981 dsl_pool_rele(dp, FTAG); 6982 error = dmu_send(snapname, fromname, embedok, largeblockok, 6983 compressok, rawok, savedok, resumeobj, resumeoff, 6984 redactlist_book, fd, &off, &out); 6985 } else { 6986 error = dmu_send_estimate_fast(tosnap, fromsnap, 6987 (from && strchr(fromname, '#') != NULL ? &zbm : NULL), 6988 compressok || rawok, savedok, &space); 6989 space -= resume_bytes; 6990 if (fromsnap != NULL) 6991 dsl_dataset_rele(fromsnap, FTAG); 6992 dsl_dataset_rele(tosnap, FTAG); 6993 dsl_pool_rele(dp, FTAG); 6994 } 6995 6996 fnvlist_add_uint64(outnvl, "space", space); 6997 6998 return (error); 6999 } 7000 7001 /* 7002 * Sync the currently open TXG to disk for the specified pool. 7003 * This is somewhat similar to 'zfs_sync()'. 7004 * For cases that do not result in error this ioctl will wait for 7005 * the currently open TXG to commit before returning back to the caller. 7006 * 7007 * innvl: { 7008 * "force" -> when true, force uberblock update even if there is no dirty data. 7009 * In addition this will cause the vdev configuration to be written 7010 * out including updating the zpool cache file. (boolean_t) 7011 * } 7012 * 7013 * onvl is unused 7014 */ 7015 static const zfs_ioc_key_t zfs_keys_pool_sync[] = { 7016 {"force", DATA_TYPE_BOOLEAN_VALUE, 0}, 7017 }; 7018 7019 static int 7020 zfs_ioc_pool_sync(const char *pool, nvlist_t *innvl, nvlist_t *onvl) 7021 { 7022 (void) onvl; 7023 int err; 7024 boolean_t rc, force = B_FALSE; 7025 spa_t *spa; 7026 7027 if ((err = spa_open(pool, &spa, FTAG)) != 0) 7028 return (err); 7029 7030 if (innvl) { 7031 err = nvlist_lookup_boolean_value(innvl, "force", &rc); 7032 if (err == 0) 7033 force = rc; 7034 } 7035 7036 if (force) { 7037 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_WRITER); 7038 vdev_config_dirty(spa->spa_root_vdev); 7039 spa_config_exit(spa, SCL_CONFIG, FTAG); 7040 } 7041 txg_wait_synced(spa_get_dsl(spa), 0); 7042 7043 spa_close(spa, FTAG); 7044 7045 return (0); 7046 } 7047 7048 /* 7049 * Load a user's wrapping key into the kernel. 7050 * innvl: { 7051 * "hidden_args" -> { "wkeydata" -> value } 7052 * raw uint8_t array of encryption wrapping key data (32 bytes) 7053 * (optional) "noop" -> (value ignored) 7054 * presence indicated key should only be verified, not loaded 7055 * } 7056 */ 7057 static const zfs_ioc_key_t zfs_keys_load_key[] = { 7058 {"hidden_args", DATA_TYPE_NVLIST, 0}, 7059 {"noop", DATA_TYPE_BOOLEAN, ZK_OPTIONAL}, 7060 }; 7061 7062 static int 7063 zfs_ioc_load_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl) 7064 { 7065 (void) outnvl; 7066 int ret; 7067 dsl_crypto_params_t *dcp = NULL; 7068 nvlist_t *hidden_args; 7069 boolean_t noop = nvlist_exists(innvl, "noop"); 7070 7071 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) { 7072 ret = SET_ERROR(EINVAL); 7073 goto error; 7074 } 7075 7076 hidden_args = fnvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS); 7077 7078 ret = dsl_crypto_params_create_nvlist(DCP_CMD_NONE, NULL, 7079 hidden_args, &dcp); 7080 if (ret != 0) 7081 goto error; 7082 7083 ret = spa_keystore_load_wkey(dsname, dcp, noop); 7084 if (ret != 0) 7085 goto error; 7086 7087 dsl_crypto_params_free(dcp, noop); 7088 7089 return (0); 7090 7091 error: 7092 dsl_crypto_params_free(dcp, B_TRUE); 7093 return (ret); 7094 } 7095 7096 /* 7097 * Unload a user's wrapping key from the kernel. 7098 * Both innvl and outnvl are unused. 7099 */ 7100 static const zfs_ioc_key_t zfs_keys_unload_key[] = { 7101 /* no nvl keys */ 7102 }; 7103 7104 static int 7105 zfs_ioc_unload_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl) 7106 { 7107 (void) innvl, (void) outnvl; 7108 int ret = 0; 7109 7110 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) { 7111 ret = (SET_ERROR(EINVAL)); 7112 goto out; 7113 } 7114 7115 ret = spa_keystore_unload_wkey(dsname); 7116 if (ret != 0) 7117 goto out; 7118 7119 out: 7120 return (ret); 7121 } 7122 7123 /* 7124 * Changes a user's wrapping key used to decrypt a dataset. The keyformat, 7125 * keylocation, pbkdf2salt, and pbkdf2iters properties can also be specified 7126 * here to change how the key is derived in userspace. 7127 * 7128 * innvl: { 7129 * "hidden_args" (optional) -> { "wkeydata" -> value } 7130 * raw uint8_t array of new encryption wrapping key data (32 bytes) 7131 * "props" (optional) -> { prop -> value } 7132 * } 7133 * 7134 * outnvl is unused 7135 */ 7136 static const zfs_ioc_key_t zfs_keys_change_key[] = { 7137 {"crypt_cmd", DATA_TYPE_UINT64, ZK_OPTIONAL}, 7138 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL}, 7139 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL}, 7140 }; 7141 7142 static int 7143 zfs_ioc_change_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl) 7144 { 7145 (void) outnvl; 7146 int ret; 7147 uint64_t cmd = DCP_CMD_NONE; 7148 dsl_crypto_params_t *dcp = NULL; 7149 nvlist_t *args = NULL, *hidden_args = NULL; 7150 7151 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) { 7152 ret = (SET_ERROR(EINVAL)); 7153 goto error; 7154 } 7155 7156 (void) nvlist_lookup_uint64(innvl, "crypt_cmd", &cmd); 7157 (void) nvlist_lookup_nvlist(innvl, "props", &args); 7158 (void) nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args); 7159 7160 ret = dsl_crypto_params_create_nvlist(cmd, args, hidden_args, &dcp); 7161 if (ret != 0) 7162 goto error; 7163 7164 ret = spa_keystore_change_key(dsname, dcp); 7165 if (ret != 0) 7166 goto error; 7167 7168 dsl_crypto_params_free(dcp, B_FALSE); 7169 7170 return (0); 7171 7172 error: 7173 dsl_crypto_params_free(dcp, B_TRUE); 7174 return (ret); 7175 } 7176 7177 static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST]; 7178 7179 static void 7180 zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func, 7181 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck, 7182 boolean_t log_history, zfs_ioc_poolcheck_t pool_check) 7183 { 7184 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST]; 7185 7186 ASSERT3U(ioc, >=, ZFS_IOC_FIRST); 7187 ASSERT3U(ioc, <, ZFS_IOC_LAST); 7188 ASSERT3P(vec->zvec_legacy_func, ==, NULL); 7189 ASSERT3P(vec->zvec_func, ==, NULL); 7190 7191 vec->zvec_legacy_func = func; 7192 vec->zvec_secpolicy = secpolicy; 7193 vec->zvec_namecheck = namecheck; 7194 vec->zvec_allow_log = log_history; 7195 vec->zvec_pool_check = pool_check; 7196 } 7197 7198 /* 7199 * See the block comment at the beginning of this file for details on 7200 * each argument to this function. 7201 */ 7202 void 7203 zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func, 7204 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck, 7205 zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist, 7206 boolean_t allow_log, const zfs_ioc_key_t *nvl_keys, size_t num_keys) 7207 { 7208 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST]; 7209 7210 ASSERT3U(ioc, >=, ZFS_IOC_FIRST); 7211 ASSERT3U(ioc, <, ZFS_IOC_LAST); 7212 ASSERT3P(vec->zvec_legacy_func, ==, NULL); 7213 ASSERT3P(vec->zvec_func, ==, NULL); 7214 7215 /* if we are logging, the name must be valid */ 7216 ASSERT(!allow_log || namecheck != NO_NAME); 7217 7218 vec->zvec_name = name; 7219 vec->zvec_func = func; 7220 vec->zvec_secpolicy = secpolicy; 7221 vec->zvec_namecheck = namecheck; 7222 vec->zvec_pool_check = pool_check; 7223 vec->zvec_smush_outnvlist = smush_outnvlist; 7224 vec->zvec_allow_log = allow_log; 7225 vec->zvec_nvl_keys = nvl_keys; 7226 vec->zvec_nvl_key_count = num_keys; 7227 } 7228 7229 static void 7230 zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func, 7231 zfs_secpolicy_func_t *secpolicy, boolean_t log_history, 7232 zfs_ioc_poolcheck_t pool_check) 7233 { 7234 zfs_ioctl_register_legacy(ioc, func, secpolicy, 7235 POOL_NAME, log_history, pool_check); 7236 } 7237 7238 void 7239 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func, 7240 zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check) 7241 { 7242 zfs_ioctl_register_legacy(ioc, func, secpolicy, 7243 DATASET_NAME, B_FALSE, pool_check); 7244 } 7245 7246 static void 7247 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func) 7248 { 7249 zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config, 7250 POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY); 7251 } 7252 7253 static void 7254 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func, 7255 zfs_secpolicy_func_t *secpolicy) 7256 { 7257 zfs_ioctl_register_legacy(ioc, func, secpolicy, 7258 NO_NAME, B_FALSE, POOL_CHECK_NONE); 7259 } 7260 7261 static void 7262 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc, 7263 zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy) 7264 { 7265 zfs_ioctl_register_legacy(ioc, func, secpolicy, 7266 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED); 7267 } 7268 7269 static void 7270 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func) 7271 { 7272 zfs_ioctl_register_dataset_read_secpolicy(ioc, func, 7273 zfs_secpolicy_read); 7274 } 7275 7276 static void 7277 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func, 7278 zfs_secpolicy_func_t *secpolicy) 7279 { 7280 zfs_ioctl_register_legacy(ioc, func, secpolicy, 7281 DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY); 7282 } 7283 7284 static void 7285 zfs_ioctl_init(void) 7286 { 7287 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT, 7288 zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME, 7289 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE, 7290 zfs_keys_snapshot, ARRAY_SIZE(zfs_keys_snapshot)); 7291 7292 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY, 7293 zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME, 7294 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE, 7295 zfs_keys_log_history, ARRAY_SIZE(zfs_keys_log_history)); 7296 7297 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS, 7298 zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME, 7299 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE, 7300 zfs_keys_space_snaps, ARRAY_SIZE(zfs_keys_space_snaps)); 7301 7302 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW, 7303 zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME, 7304 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE, 7305 zfs_keys_send_new, ARRAY_SIZE(zfs_keys_send_new)); 7306 7307 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE, 7308 zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME, 7309 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE, 7310 zfs_keys_send_space, ARRAY_SIZE(zfs_keys_send_space)); 7311 7312 zfs_ioctl_register("create", ZFS_IOC_CREATE, 7313 zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME, 7314 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE, 7315 zfs_keys_create, ARRAY_SIZE(zfs_keys_create)); 7316 7317 zfs_ioctl_register("clone", ZFS_IOC_CLONE, 7318 zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME, 7319 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE, 7320 zfs_keys_clone, ARRAY_SIZE(zfs_keys_clone)); 7321 7322 zfs_ioctl_register("remap", ZFS_IOC_REMAP, 7323 zfs_ioc_remap, zfs_secpolicy_none, DATASET_NAME, 7324 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE, 7325 zfs_keys_remap, ARRAY_SIZE(zfs_keys_remap)); 7326 7327 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS, 7328 zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME, 7329 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE, 7330 zfs_keys_destroy_snaps, ARRAY_SIZE(zfs_keys_destroy_snaps)); 7331 7332 zfs_ioctl_register("hold", ZFS_IOC_HOLD, 7333 zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME, 7334 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE, 7335 zfs_keys_hold, ARRAY_SIZE(zfs_keys_hold)); 7336 zfs_ioctl_register("release", ZFS_IOC_RELEASE, 7337 zfs_ioc_release, zfs_secpolicy_release, POOL_NAME, 7338 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE, 7339 zfs_keys_release, ARRAY_SIZE(zfs_keys_release)); 7340 7341 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS, 7342 zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, 7343 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE, 7344 zfs_keys_get_holds, ARRAY_SIZE(zfs_keys_get_holds)); 7345 7346 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK, 7347 zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, 7348 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE, 7349 zfs_keys_rollback, ARRAY_SIZE(zfs_keys_rollback)); 7350 7351 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK, 7352 zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME, 7353 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE, 7354 zfs_keys_bookmark, ARRAY_SIZE(zfs_keys_bookmark)); 7355 7356 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS, 7357 zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME, 7358 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE, 7359 zfs_keys_get_bookmarks, ARRAY_SIZE(zfs_keys_get_bookmarks)); 7360 7361 zfs_ioctl_register("get_bookmark_props", ZFS_IOC_GET_BOOKMARK_PROPS, 7362 zfs_ioc_get_bookmark_props, zfs_secpolicy_read, ENTITY_NAME, 7363 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE, zfs_keys_get_bookmark_props, 7364 ARRAY_SIZE(zfs_keys_get_bookmark_props)); 7365 7366 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS, 7367 zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks, 7368 POOL_NAME, 7369 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE, 7370 zfs_keys_destroy_bookmarks, 7371 ARRAY_SIZE(zfs_keys_destroy_bookmarks)); 7372 7373 zfs_ioctl_register("receive", ZFS_IOC_RECV_NEW, 7374 zfs_ioc_recv_new, zfs_secpolicy_recv, DATASET_NAME, 7375 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE, 7376 zfs_keys_recv_new, ARRAY_SIZE(zfs_keys_recv_new)); 7377 zfs_ioctl_register("load-key", ZFS_IOC_LOAD_KEY, 7378 zfs_ioc_load_key, zfs_secpolicy_load_key, 7379 DATASET_NAME, POOL_CHECK_SUSPENDED, B_TRUE, B_TRUE, 7380 zfs_keys_load_key, ARRAY_SIZE(zfs_keys_load_key)); 7381 zfs_ioctl_register("unload-key", ZFS_IOC_UNLOAD_KEY, 7382 zfs_ioc_unload_key, zfs_secpolicy_load_key, 7383 DATASET_NAME, POOL_CHECK_SUSPENDED, B_TRUE, B_TRUE, 7384 zfs_keys_unload_key, ARRAY_SIZE(zfs_keys_unload_key)); 7385 zfs_ioctl_register("change-key", ZFS_IOC_CHANGE_KEY, 7386 zfs_ioc_change_key, zfs_secpolicy_change_key, 7387 DATASET_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, 7388 B_TRUE, B_TRUE, zfs_keys_change_key, 7389 ARRAY_SIZE(zfs_keys_change_key)); 7390 7391 zfs_ioctl_register("sync", ZFS_IOC_POOL_SYNC, 7392 zfs_ioc_pool_sync, zfs_secpolicy_none, POOL_NAME, 7393 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE, 7394 zfs_keys_pool_sync, ARRAY_SIZE(zfs_keys_pool_sync)); 7395 zfs_ioctl_register("reopen", ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen, 7396 zfs_secpolicy_config, POOL_NAME, POOL_CHECK_SUSPENDED, B_TRUE, 7397 B_TRUE, zfs_keys_pool_reopen, ARRAY_SIZE(zfs_keys_pool_reopen)); 7398 7399 zfs_ioctl_register("channel_program", ZFS_IOC_CHANNEL_PROGRAM, 7400 zfs_ioc_channel_program, zfs_secpolicy_config, 7401 POOL_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, 7402 B_TRUE, zfs_keys_channel_program, 7403 ARRAY_SIZE(zfs_keys_channel_program)); 7404 7405 zfs_ioctl_register("redact", ZFS_IOC_REDACT, 7406 zfs_ioc_redact, zfs_secpolicy_config, DATASET_NAME, 7407 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE, 7408 zfs_keys_redact, ARRAY_SIZE(zfs_keys_redact)); 7409 7410 zfs_ioctl_register("zpool_checkpoint", ZFS_IOC_POOL_CHECKPOINT, 7411 zfs_ioc_pool_checkpoint, zfs_secpolicy_config, POOL_NAME, 7412 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE, 7413 zfs_keys_pool_checkpoint, ARRAY_SIZE(zfs_keys_pool_checkpoint)); 7414 7415 zfs_ioctl_register("zpool_discard_checkpoint", 7416 ZFS_IOC_POOL_DISCARD_CHECKPOINT, zfs_ioc_pool_discard_checkpoint, 7417 zfs_secpolicy_config, POOL_NAME, 7418 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE, 7419 zfs_keys_pool_discard_checkpoint, 7420 ARRAY_SIZE(zfs_keys_pool_discard_checkpoint)); 7421 7422 zfs_ioctl_register("zpool_prefetch", 7423 ZFS_IOC_POOL_PREFETCH, zfs_ioc_pool_prefetch, 7424 zfs_secpolicy_config, POOL_NAME, 7425 POOL_CHECK_SUSPENDED, B_TRUE, B_TRUE, 7426 zfs_keys_pool_prefetch, ARRAY_SIZE(zfs_keys_pool_prefetch)); 7427 7428 zfs_ioctl_register("initialize", ZFS_IOC_POOL_INITIALIZE, 7429 zfs_ioc_pool_initialize, zfs_secpolicy_config, POOL_NAME, 7430 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE, 7431 zfs_keys_pool_initialize, ARRAY_SIZE(zfs_keys_pool_initialize)); 7432 7433 zfs_ioctl_register("trim", ZFS_IOC_POOL_TRIM, 7434 zfs_ioc_pool_trim, zfs_secpolicy_config, POOL_NAME, 7435 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE, 7436 zfs_keys_pool_trim, ARRAY_SIZE(zfs_keys_pool_trim)); 7437 7438 zfs_ioctl_register("wait", ZFS_IOC_WAIT, 7439 zfs_ioc_wait, zfs_secpolicy_none, POOL_NAME, 7440 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE, 7441 zfs_keys_pool_wait, ARRAY_SIZE(zfs_keys_pool_wait)); 7442 7443 zfs_ioctl_register("wait_fs", ZFS_IOC_WAIT_FS, 7444 zfs_ioc_wait_fs, zfs_secpolicy_none, DATASET_NAME, 7445 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE, 7446 zfs_keys_fs_wait, ARRAY_SIZE(zfs_keys_fs_wait)); 7447 7448 zfs_ioctl_register("set_bootenv", ZFS_IOC_SET_BOOTENV, 7449 zfs_ioc_set_bootenv, zfs_secpolicy_config, POOL_NAME, 7450 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE, 7451 zfs_keys_set_bootenv, ARRAY_SIZE(zfs_keys_set_bootenv)); 7452 7453 zfs_ioctl_register("get_bootenv", ZFS_IOC_GET_BOOTENV, 7454 zfs_ioc_get_bootenv, zfs_secpolicy_none, POOL_NAME, 7455 POOL_CHECK_SUSPENDED, B_FALSE, B_TRUE, 7456 zfs_keys_get_bootenv, ARRAY_SIZE(zfs_keys_get_bootenv)); 7457 7458 zfs_ioctl_register("zpool_vdev_get_props", ZFS_IOC_VDEV_GET_PROPS, 7459 zfs_ioc_vdev_get_props, zfs_secpolicy_read, POOL_NAME, 7460 POOL_CHECK_NONE, B_FALSE, B_FALSE, zfs_keys_vdev_get_props, 7461 ARRAY_SIZE(zfs_keys_vdev_get_props)); 7462 7463 zfs_ioctl_register("zpool_vdev_set_props", ZFS_IOC_VDEV_SET_PROPS, 7464 zfs_ioc_vdev_set_props, zfs_secpolicy_config, POOL_NAME, 7465 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE, 7466 zfs_keys_vdev_set_props, ARRAY_SIZE(zfs_keys_vdev_set_props)); 7467 7468 zfs_ioctl_register("scrub", ZFS_IOC_POOL_SCRUB, 7469 zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, 7470 POOL_CHECK_NONE, B_TRUE, B_TRUE, 7471 zfs_keys_pool_scrub, ARRAY_SIZE(zfs_keys_pool_scrub)); 7472 7473 zfs_ioctl_register("get_props", ZFS_IOC_POOL_GET_PROPS, 7474 zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, 7475 POOL_CHECK_NONE, B_FALSE, B_FALSE, 7476 zfs_keys_get_props, ARRAY_SIZE(zfs_keys_get_props)); 7477 7478 zfs_ioctl_register("zpool_ddt_prune", ZFS_IOC_DDT_PRUNE, 7479 zfs_ioc_ddt_prune, zfs_secpolicy_config, POOL_NAME, 7480 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE, 7481 zfs_keys_ddt_prune, ARRAY_SIZE(zfs_keys_ddt_prune)); 7482 7483 /* IOCTLS that use the legacy function signature */ 7484 7485 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze, 7486 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY); 7487 7488 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create, 7489 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE); 7490 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN, 7491 zfs_ioc_pool_scan); 7492 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE, 7493 zfs_ioc_pool_upgrade); 7494 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD, 7495 zfs_ioc_vdev_add); 7496 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE, 7497 zfs_ioc_vdev_remove); 7498 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE, 7499 zfs_ioc_vdev_set_state); 7500 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH, 7501 zfs_ioc_vdev_attach); 7502 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH, 7503 zfs_ioc_vdev_detach); 7504 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH, 7505 zfs_ioc_vdev_setpath); 7506 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU, 7507 zfs_ioc_vdev_setfru); 7508 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS, 7509 zfs_ioc_pool_set_props); 7510 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT, 7511 zfs_ioc_vdev_split); 7512 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID, 7513 zfs_ioc_pool_reguid); 7514 7515 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS, 7516 zfs_ioc_pool_configs, zfs_secpolicy_none); 7517 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT, 7518 zfs_ioc_pool_tryimport, zfs_secpolicy_config); 7519 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT, 7520 zfs_ioc_inject_fault, zfs_secpolicy_inject); 7521 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT, 7522 zfs_ioc_clear_fault, zfs_secpolicy_inject); 7523 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT, 7524 zfs_ioc_inject_list_next, zfs_secpolicy_inject); 7525 7526 /* 7527 * pool destroy, and export don't log the history as part of 7528 * zfsdev_ioctl, but rather zfs_ioc_pool_export 7529 * does the logging of those commands. 7530 */ 7531 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy, 7532 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED); 7533 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export, 7534 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED); 7535 7536 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats, 7537 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE); 7538 7539 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log, 7540 zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED); 7541 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME, 7542 zfs_ioc_dsobj_to_dsname, 7543 zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED); 7544 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY, 7545 zfs_ioc_pool_get_history, 7546 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED); 7547 7548 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import, 7549 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE); 7550 7551 zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear, 7552 zfs_secpolicy_config, B_TRUE, POOL_CHECK_READONLY); 7553 7554 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN, 7555 zfs_ioc_space_written); 7556 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS, 7557 zfs_ioc_objset_recvd_props); 7558 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ, 7559 zfs_ioc_next_obj); 7560 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL, 7561 zfs_ioc_get_fsacl); 7562 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS, 7563 zfs_ioc_objset_stats); 7564 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS, 7565 zfs_ioc_objset_zplprops); 7566 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT, 7567 zfs_ioc_dataset_list_next); 7568 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT, 7569 zfs_ioc_snapshot_list_next); 7570 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS, 7571 zfs_ioc_send_progress); 7572 7573 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF, 7574 zfs_ioc_diff, zfs_secpolicy_diff); 7575 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS, 7576 zfs_ioc_obj_to_stats, zfs_secpolicy_diff); 7577 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH, 7578 zfs_ioc_obj_to_path, zfs_secpolicy_diff); 7579 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE, 7580 zfs_ioc_userspace_one, zfs_secpolicy_userspace_one); 7581 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY, 7582 zfs_ioc_userspace_many, zfs_secpolicy_userspace_many); 7583 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND, 7584 zfs_ioc_send, zfs_secpolicy_send); 7585 7586 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop, 7587 zfs_secpolicy_none); 7588 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy, 7589 zfs_secpolicy_destroy); 7590 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename, 7591 zfs_secpolicy_rename); 7592 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv, 7593 zfs_secpolicy_recv); 7594 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote, 7595 zfs_secpolicy_promote); 7596 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP, 7597 zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop); 7598 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl, 7599 zfs_secpolicy_set_fsacl); 7600 7601 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share, 7602 zfs_secpolicy_share, POOL_CHECK_NONE); 7603 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl, 7604 zfs_secpolicy_smb_acl, POOL_CHECK_NONE); 7605 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE, 7606 zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade, 7607 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY); 7608 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT, 7609 zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot, 7610 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY); 7611 7612 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_NEXT, zfs_ioc_events_next, 7613 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE); 7614 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_CLEAR, zfs_ioc_events_clear, 7615 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE); 7616 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_SEEK, zfs_ioc_events_seek, 7617 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE); 7618 7619 zfs_ioctl_init_os(); 7620 } 7621 7622 /* 7623 * Verify that for non-legacy ioctls the input nvlist 7624 * pairs match against the expected input. 7625 * 7626 * Possible errors are: 7627 * ZFS_ERR_IOC_ARG_UNAVAIL An unrecognized nvpair was encountered 7628 * ZFS_ERR_IOC_ARG_REQUIRED A required nvpair is missing 7629 * ZFS_ERR_IOC_ARG_BADTYPE Invalid type for nvpair 7630 */ 7631 static int 7632 zfs_check_input_nvpairs(nvlist_t *innvl, const zfs_ioc_vec_t *vec) 7633 { 7634 const zfs_ioc_key_t *nvl_keys = vec->zvec_nvl_keys; 7635 boolean_t required_keys_found = B_FALSE; 7636 7637 /* 7638 * examine each input pair 7639 */ 7640 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL); 7641 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) { 7642 const char *name = nvpair_name(pair); 7643 data_type_t type = nvpair_type(pair); 7644 boolean_t identified = B_FALSE; 7645 7646 /* 7647 * check pair against the documented names and type 7648 */ 7649 for (int k = 0; k < vec->zvec_nvl_key_count; k++) { 7650 /* if not a wild card name, check for an exact match */ 7651 if ((nvl_keys[k].zkey_flags & ZK_WILDCARDLIST) == 0 && 7652 strcmp(nvl_keys[k].zkey_name, name) != 0) 7653 continue; 7654 7655 identified = B_TRUE; 7656 7657 if (nvl_keys[k].zkey_type != DATA_TYPE_ANY && 7658 nvl_keys[k].zkey_type != type) { 7659 return (SET_ERROR(ZFS_ERR_IOC_ARG_BADTYPE)); 7660 } 7661 7662 if (nvl_keys[k].zkey_flags & ZK_OPTIONAL) 7663 continue; 7664 7665 required_keys_found = B_TRUE; 7666 break; 7667 } 7668 7669 /* allow an 'optional' key, everything else is invalid */ 7670 if (!identified && 7671 (strcmp(name, "optional") != 0 || 7672 type != DATA_TYPE_NVLIST)) { 7673 return (SET_ERROR(ZFS_ERR_IOC_ARG_UNAVAIL)); 7674 } 7675 } 7676 7677 /* verify that all required keys were found */ 7678 for (int k = 0; k < vec->zvec_nvl_key_count; k++) { 7679 if (nvl_keys[k].zkey_flags & ZK_OPTIONAL) 7680 continue; 7681 7682 if (nvl_keys[k].zkey_flags & ZK_WILDCARDLIST) { 7683 /* at least one non-optional key is expected here */ 7684 if (!required_keys_found) 7685 return (SET_ERROR(ZFS_ERR_IOC_ARG_REQUIRED)); 7686 continue; 7687 } 7688 7689 if (!nvlist_exists(innvl, nvl_keys[k].zkey_name)) 7690 return (SET_ERROR(ZFS_ERR_IOC_ARG_REQUIRED)); 7691 } 7692 7693 return (0); 7694 } 7695 7696 static int 7697 pool_status_check(const char *name, zfs_ioc_namecheck_t type, 7698 zfs_ioc_poolcheck_t check) 7699 { 7700 spa_t *spa; 7701 int error; 7702 7703 ASSERT(type == POOL_NAME || type == DATASET_NAME || 7704 type == ENTITY_NAME); 7705 7706 if (check & POOL_CHECK_NONE) 7707 return (0); 7708 7709 error = spa_open(name, &spa, FTAG); 7710 if (error == 0) { 7711 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa)) 7712 error = SET_ERROR(EAGAIN); 7713 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa)) 7714 error = SET_ERROR(EROFS); 7715 spa_close(spa, FTAG); 7716 } 7717 return (error); 7718 } 7719 7720 int 7721 zfsdev_getminor(zfs_file_t *fp, minor_t *minorp) 7722 { 7723 zfsdev_state_t *zs, *fpd; 7724 7725 ASSERT(!MUTEX_HELD(&zfsdev_state_lock)); 7726 7727 fpd = zfs_file_private(fp); 7728 if (fpd == NULL) 7729 return (SET_ERROR(EBADF)); 7730 7731 mutex_enter(&zfsdev_state_lock); 7732 7733 for (zs = &zfsdev_state_listhead; zs != NULL; zs = zs->zs_next) { 7734 7735 if (zs->zs_minor == -1) 7736 continue; 7737 7738 if (fpd == zs) { 7739 *minorp = fpd->zs_minor; 7740 mutex_exit(&zfsdev_state_lock); 7741 return (0); 7742 } 7743 } 7744 7745 mutex_exit(&zfsdev_state_lock); 7746 7747 return (SET_ERROR(EBADF)); 7748 } 7749 7750 void * 7751 zfsdev_get_state(minor_t minor, enum zfsdev_state_type which) 7752 { 7753 zfsdev_state_t *zs; 7754 7755 for (zs = &zfsdev_state_listhead; zs != NULL; zs = zs->zs_next) { 7756 if (zs->zs_minor == minor) { 7757 membar_consumer(); 7758 switch (which) { 7759 case ZST_ONEXIT: 7760 return (zs->zs_onexit); 7761 case ZST_ZEVENT: 7762 return (zs->zs_zevent); 7763 case ZST_ALL: 7764 return (zs); 7765 } 7766 } 7767 } 7768 7769 return (NULL); 7770 } 7771 7772 /* 7773 * Find a free minor number. The zfsdev_state_list is expected to 7774 * be short since it is only a list of currently open file handles. 7775 */ 7776 static minor_t 7777 zfsdev_minor_alloc(void) 7778 { 7779 static minor_t last_minor = 0; 7780 minor_t m; 7781 7782 ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 7783 7784 for (m = last_minor + 1; m != last_minor; m++) { 7785 if (m > ZFSDEV_MAX_MINOR) 7786 m = 1; 7787 if (zfsdev_get_state(m, ZST_ALL) == NULL) { 7788 last_minor = m; 7789 return (m); 7790 } 7791 } 7792 7793 return (0); 7794 } 7795 7796 int 7797 zfsdev_state_init(void *priv) 7798 { 7799 zfsdev_state_t *zs, *zsprev = NULL; 7800 minor_t minor; 7801 boolean_t newzs = B_FALSE; 7802 7803 ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 7804 7805 minor = zfsdev_minor_alloc(); 7806 if (minor == 0) 7807 return (SET_ERROR(ENXIO)); 7808 7809 for (zs = &zfsdev_state_listhead; zs != NULL; zs = zs->zs_next) { 7810 if (zs->zs_minor == -1) 7811 break; 7812 zsprev = zs; 7813 } 7814 7815 if (!zs) { 7816 zs = kmem_zalloc(sizeof (zfsdev_state_t), KM_SLEEP); 7817 newzs = B_TRUE; 7818 } 7819 7820 zfsdev_private_set_state(priv, zs); 7821 7822 zfs_onexit_init((zfs_onexit_t **)&zs->zs_onexit); 7823 zfs_zevent_init((zfs_zevent_t **)&zs->zs_zevent); 7824 7825 /* 7826 * In order to provide for lock-free concurrent read access 7827 * to the minor list in zfsdev_get_state(), new entries 7828 * must be completely written before linking them into the 7829 * list whereas existing entries are already linked; the last 7830 * operation must be updating zs_minor (from -1 to the new 7831 * value). 7832 */ 7833 if (newzs) { 7834 zs->zs_minor = minor; 7835 membar_producer(); 7836 zsprev->zs_next = zs; 7837 } else { 7838 membar_producer(); 7839 zs->zs_minor = minor; 7840 } 7841 7842 return (0); 7843 } 7844 7845 void 7846 zfsdev_state_destroy(void *priv) 7847 { 7848 zfsdev_state_t *zs = zfsdev_private_get_state(priv); 7849 7850 ASSERT(zs != NULL); 7851 ASSERT3S(zs->zs_minor, >, 0); 7852 7853 /* 7854 * The last reference to this zfsdev file descriptor is being dropped. 7855 * We don't have to worry about lookup grabbing this state object, and 7856 * zfsdev_state_init() will not try to reuse this object until it is 7857 * invalidated by setting zs_minor to -1. Invalidation must be done 7858 * last, with a memory barrier to ensure ordering. This lets us avoid 7859 * taking the global zfsdev state lock around destruction. 7860 */ 7861 zfs_onexit_destroy(zs->zs_onexit); 7862 zfs_zevent_destroy(zs->zs_zevent); 7863 zs->zs_onexit = NULL; 7864 zs->zs_zevent = NULL; 7865 membar_producer(); 7866 zs->zs_minor = -1; 7867 } 7868 7869 long 7870 zfsdev_ioctl_common(uint_t vecnum, zfs_cmd_t *zc, int flag) 7871 { 7872 int error, cmd; 7873 const zfs_ioc_vec_t *vec; 7874 char *saved_poolname = NULL; 7875 uint64_t max_nvlist_src_size; 7876 size_t saved_poolname_len = 0; 7877 nvlist_t *innvl = NULL; 7878 fstrans_cookie_t cookie; 7879 hrtime_t start_time = gethrtime(); 7880 7881 cmd = vecnum; 7882 error = 0; 7883 if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0])) 7884 return (SET_ERROR(ZFS_ERR_IOC_CMD_UNAVAIL)); 7885 7886 vec = &zfs_ioc_vec[vecnum]; 7887 7888 /* 7889 * The registered ioctl list may be sparse, verify that either 7890 * a normal or legacy handler are registered. 7891 */ 7892 if (vec->zvec_func == NULL && vec->zvec_legacy_func == NULL) 7893 return (SET_ERROR(ZFS_ERR_IOC_CMD_UNAVAIL)); 7894 7895 zc->zc_iflags = flag & FKIOCTL; 7896 max_nvlist_src_size = zfs_max_nvlist_src_size_os(); 7897 if (zc->zc_nvlist_src_size > max_nvlist_src_size) { 7898 /* 7899 * Make sure the user doesn't pass in an insane value for 7900 * zc_nvlist_src_size. We have to check, since we will end 7901 * up allocating that much memory inside of get_nvlist(). This 7902 * prevents a nefarious user from allocating tons of kernel 7903 * memory. 7904 * 7905 * Also, we return EINVAL instead of ENOMEM here. The reason 7906 * being that returning ENOMEM from an ioctl() has a special 7907 * connotation; that the user's size value is too small and 7908 * needs to be expanded to hold the nvlist. See 7909 * zcmd_expand_dst_nvlist() for details. 7910 */ 7911 error = SET_ERROR(EINVAL); /* User's size too big */ 7912 7913 } else if (zc->zc_nvlist_src_size != 0) { 7914 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 7915 zc->zc_iflags, &innvl); 7916 if (error != 0) 7917 goto out; 7918 } 7919 7920 /* 7921 * Ensure that all pool/dataset names are valid before we pass down to 7922 * the lower layers. 7923 */ 7924 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0'; 7925 switch (vec->zvec_namecheck) { 7926 case POOL_NAME: 7927 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0) 7928 error = SET_ERROR(EINVAL); 7929 else 7930 error = pool_status_check(zc->zc_name, 7931 vec->zvec_namecheck, vec->zvec_pool_check); 7932 break; 7933 7934 case DATASET_NAME: 7935 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0) 7936 error = SET_ERROR(EINVAL); 7937 else 7938 error = pool_status_check(zc->zc_name, 7939 vec->zvec_namecheck, vec->zvec_pool_check); 7940 break; 7941 7942 case ENTITY_NAME: 7943 if (entity_namecheck(zc->zc_name, NULL, NULL) != 0) { 7944 error = SET_ERROR(EINVAL); 7945 } else { 7946 error = pool_status_check(zc->zc_name, 7947 vec->zvec_namecheck, vec->zvec_pool_check); 7948 } 7949 break; 7950 7951 case NO_NAME: 7952 break; 7953 } 7954 /* 7955 * Ensure that all input pairs are valid before we pass them down 7956 * to the lower layers. 7957 * 7958 * The vectored functions can use fnvlist_lookup_{type} for any 7959 * required pairs since zfs_check_input_nvpairs() confirmed that 7960 * they exist and are of the correct type. 7961 */ 7962 if (error == 0 && vec->zvec_func != NULL) { 7963 error = zfs_check_input_nvpairs(innvl, vec); 7964 if (error != 0) 7965 goto out; 7966 } 7967 7968 if (error == 0) { 7969 cookie = spl_fstrans_mark(); 7970 error = vec->zvec_secpolicy(zc, innvl, CRED()); 7971 spl_fstrans_unmark(cookie); 7972 } 7973 7974 if (error != 0) 7975 goto out; 7976 7977 /* legacy ioctls can modify zc_name */ 7978 /* 7979 * Can't use kmem_strdup() as we might truncate the string and 7980 * kmem_strfree() would then free with incorrect size. 7981 */ 7982 saved_poolname_len = strlen(zc->zc_name) + 1; 7983 saved_poolname = kmem_alloc(saved_poolname_len, KM_SLEEP); 7984 7985 strlcpy(saved_poolname, zc->zc_name, saved_poolname_len); 7986 saved_poolname[strcspn(saved_poolname, "/@#")] = '\0'; 7987 7988 if (vec->zvec_func != NULL) { 7989 nvlist_t *outnvl; 7990 int puterror = 0; 7991 spa_t *spa; 7992 nvlist_t *lognv = NULL; 7993 7994 ASSERT(vec->zvec_legacy_func == NULL); 7995 7996 /* 7997 * Add the innvl to the lognv before calling the func, 7998 * in case the func changes the innvl. 7999 */ 8000 if (vec->zvec_allow_log) { 8001 lognv = fnvlist_alloc(); 8002 fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL, 8003 vec->zvec_name); 8004 if (!nvlist_empty(innvl)) { 8005 fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL, 8006 innvl); 8007 } 8008 } 8009 8010 outnvl = fnvlist_alloc(); 8011 cookie = spl_fstrans_mark(); 8012 error = vec->zvec_func(zc->zc_name, innvl, outnvl); 8013 spl_fstrans_unmark(cookie); 8014 8015 /* 8016 * Some commands can partially execute, modify state, and still 8017 * return an error. In these cases, attempt to record what 8018 * was modified. 8019 */ 8020 if ((error == 0 || 8021 (cmd == ZFS_IOC_CHANNEL_PROGRAM && error != EINVAL)) && 8022 vec->zvec_allow_log && 8023 spa_open(zc->zc_name, &spa, FTAG) == 0) { 8024 if (!nvlist_empty(outnvl)) { 8025 size_t out_size = fnvlist_size(outnvl); 8026 if (out_size > zfs_history_output_max) { 8027 fnvlist_add_int64(lognv, 8028 ZPOOL_HIST_OUTPUT_SIZE, out_size); 8029 } else { 8030 fnvlist_add_nvlist(lognv, 8031 ZPOOL_HIST_OUTPUT_NVL, outnvl); 8032 } 8033 } 8034 if (error != 0) { 8035 fnvlist_add_int64(lognv, ZPOOL_HIST_ERRNO, 8036 error); 8037 } 8038 fnvlist_add_int64(lognv, ZPOOL_HIST_ELAPSED_NS, 8039 gethrtime() - start_time); 8040 (void) spa_history_log_nvl(spa, lognv); 8041 spa_close(spa, FTAG); 8042 } 8043 fnvlist_free(lognv); 8044 8045 if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) { 8046 int smusherror = 0; 8047 if (vec->zvec_smush_outnvlist) { 8048 smusherror = nvlist_smush(outnvl, 8049 zc->zc_nvlist_dst_size); 8050 } 8051 if (smusherror == 0) 8052 puterror = put_nvlist(zc, outnvl); 8053 } 8054 8055 if (puterror != 0) 8056 error = puterror; 8057 8058 nvlist_free(outnvl); 8059 } else { 8060 cookie = spl_fstrans_mark(); 8061 error = vec->zvec_legacy_func(zc); 8062 spl_fstrans_unmark(cookie); 8063 } 8064 8065 out: 8066 nvlist_free(innvl); 8067 if (error == 0 && vec->zvec_allow_log) { 8068 char *s = tsd_get(zfs_allow_log_key); 8069 if (s != NULL) 8070 kmem_strfree(s); 8071 (void) tsd_set(zfs_allow_log_key, kmem_strdup(saved_poolname)); 8072 } 8073 if (saved_poolname != NULL) 8074 kmem_free(saved_poolname, saved_poolname_len); 8075 8076 return (error); 8077 } 8078 8079 int 8080 zfs_kmod_init(void) 8081 { 8082 int error; 8083 8084 if ((error = zvol_init()) != 0) 8085 return (error); 8086 8087 spa_init(SPA_MODE_READ | SPA_MODE_WRITE); 8088 zfs_init(); 8089 8090 zfs_ioctl_init(); 8091 8092 mutex_init(&zfsdev_state_lock, NULL, MUTEX_DEFAULT, NULL); 8093 zfsdev_state_listhead.zs_minor = -1; 8094 8095 if ((error = zfsdev_attach()) != 0) 8096 goto out; 8097 8098 tsd_create(&rrw_tsd_key, rrw_tsd_destroy); 8099 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy); 8100 8101 return (0); 8102 out: 8103 zfs_fini(); 8104 spa_fini(); 8105 zvol_fini(); 8106 8107 return (error); 8108 } 8109 8110 void 8111 zfs_kmod_fini(void) 8112 { 8113 zfsdev_state_t *zs, *zsnext = NULL; 8114 8115 zfsdev_detach(); 8116 8117 mutex_destroy(&zfsdev_state_lock); 8118 8119 for (zs = &zfsdev_state_listhead; zs != NULL; zs = zsnext) { 8120 zsnext = zs->zs_next; 8121 if (zs->zs_onexit) 8122 zfs_onexit_destroy(zs->zs_onexit); 8123 if (zs->zs_zevent) 8124 zfs_zevent_destroy(zs->zs_zevent); 8125 if (zs != &zfsdev_state_listhead) 8126 kmem_free(zs, sizeof (zfsdev_state_t)); 8127 } 8128 8129 zfs_ereport_taskq_fini(); /* run before zfs_fini() on Linux */ 8130 zfs_fini(); 8131 spa_fini(); 8132 zvol_fini(); 8133 8134 tsd_destroy(&rrw_tsd_key); 8135 tsd_destroy(&zfs_allow_log_key); 8136 } 8137 8138 ZFS_MODULE_PARAM(zfs, zfs_, max_nvlist_src_size, U64, ZMOD_RW, 8139 "Maximum size in bytes allowed for src nvlist passed with ZFS ioctls"); 8140 8141 ZFS_MODULE_PARAM(zfs, zfs_, history_output_max, U64, ZMOD_RW, 8142 "Maximum size in bytes of ZFS ioctl output that will be logged"); 8143