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