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