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