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