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