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