zfs_ioctl.c revision 226707
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 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23 * Copyright (c) 2011 Pawel Jakub Dawidek <pawel@dawidek.net>. 24 * All rights reserved. 25 */ 26 27#include <sys/types.h> 28#include <sys/param.h> 29#include <sys/systm.h> 30#include <sys/conf.h> 31#include <sys/kernel.h> 32#include <sys/lock.h> 33#include <sys/malloc.h> 34#include <sys/mutex.h> 35#include <sys/proc.h> 36#include <sys/errno.h> 37#include <sys/uio.h> 38#include <sys/buf.h> 39#include <sys/file.h> 40#include <sys/kmem.h> 41#include <sys/conf.h> 42#include <sys/cmn_err.h> 43#include <sys/stat.h> 44#include <sys/zfs_ioctl.h> 45#include <sys/zfs_vfsops.h> 46#include <sys/zfs_znode.h> 47#include <sys/zap.h> 48#include <sys/spa.h> 49#include <sys/spa_impl.h> 50#include <sys/vdev.h> 51#include <sys/dmu.h> 52#include <sys/dsl_dir.h> 53#include <sys/dsl_dataset.h> 54#include <sys/dsl_prop.h> 55#include <sys/dsl_deleg.h> 56#include <sys/dmu_objset.h> 57#include <sys/sunddi.h> 58#include <sys/policy.h> 59#include <sys/zone.h> 60#include <sys/nvpair.h> 61#include <sys/mount.h> 62#include <sys/taskqueue.h> 63#include <sys/sdt.h> 64#include <sys/varargs.h> 65#include <sys/fs/zfs.h> 66#include <sys/zfs_ctldir.h> 67#include <sys/zfs_dir.h> 68#include <sys/zfs_onexit.h> 69#include <sys/zvol.h> 70#include <sys/dsl_scan.h> 71#include <sys/dmu_objset.h> 72 73#include "zfs_namecheck.h" 74#include "zfs_prop.h" 75#include "zfs_deleg.h" 76#include "zfs_comutil.h" 77#include "zfs_ioctl_compat.h" 78 79CTASSERT(sizeof(zfs_cmd_t) < IOCPARM_MAX); 80 81static struct cdev *zfsdev; 82 83extern void zfs_init(void); 84extern void zfs_fini(void); 85 86typedef int zfs_ioc_func_t(zfs_cmd_t *); 87typedef int zfs_secpolicy_func_t(zfs_cmd_t *, cred_t *); 88 89typedef enum { 90 NO_NAME, 91 POOL_NAME, 92 DATASET_NAME 93} zfs_ioc_namecheck_t; 94 95typedef struct zfs_ioc_vec { 96 zfs_ioc_func_t *zvec_func; 97 zfs_secpolicy_func_t *zvec_secpolicy; 98 zfs_ioc_namecheck_t zvec_namecheck; 99 boolean_t zvec_his_log; 100 boolean_t zvec_pool_check; 101} zfs_ioc_vec_t; 102 103/* This array is indexed by zfs_userquota_prop_t */ 104static const char *userquota_perms[] = { 105 ZFS_DELEG_PERM_USERUSED, 106 ZFS_DELEG_PERM_USERQUOTA, 107 ZFS_DELEG_PERM_GROUPUSED, 108 ZFS_DELEG_PERM_GROUPQUOTA, 109}; 110 111static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc); 112static int zfs_check_settable(const char *name, nvpair_t *property, 113 cred_t *cr); 114static int zfs_check_clearable(char *dataset, nvlist_t *props, 115 nvlist_t **errors); 116static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *, 117 boolean_t *); 118int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t **); 119 120static void zfsdev_close(void *data); 121 122/* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */ 123void 124__dprintf(const char *file, const char *func, int line, const char *fmt, ...) 125{ 126 const char *newfile; 127 char buf[512]; 128 va_list adx; 129 130 /* 131 * Get rid of annoying "../common/" prefix to filename. 132 */ 133 newfile = strrchr(file, '/'); 134 if (newfile != NULL) { 135 newfile = newfile + 1; /* Get rid of leading / */ 136 } else { 137 newfile = file; 138 } 139 140 va_start(adx, fmt); 141 (void) vsnprintf(buf, sizeof (buf), fmt, adx); 142 va_end(adx); 143 144 /* 145 * To get this data, use the zfs-dprintf probe as so: 146 * dtrace -q -n 'zfs-dprintf \ 147 * /stringof(arg0) == "dbuf.c"/ \ 148 * {printf("%s: %s", stringof(arg1), stringof(arg3))}' 149 * arg0 = file name 150 * arg1 = function name 151 * arg2 = line number 152 * arg3 = message 153 */ 154 DTRACE_PROBE4(zfs__dprintf, 155 char *, newfile, char *, func, int, line, char *, buf); 156} 157 158static void 159history_str_free(char *buf) 160{ 161 kmem_free(buf, HIS_MAX_RECORD_LEN); 162} 163 164static char * 165history_str_get(zfs_cmd_t *zc) 166{ 167 char *buf; 168 169 if (zc->zc_history == 0) 170 return (NULL); 171 172 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP); 173 if (copyinstr((void *)(uintptr_t)zc->zc_history, 174 buf, HIS_MAX_RECORD_LEN, NULL) != 0) { 175 history_str_free(buf); 176 return (NULL); 177 } 178 179 buf[HIS_MAX_RECORD_LEN -1] = '\0'; 180 181 return (buf); 182} 183 184/* 185 * Check to see if the named dataset is currently defined as bootable 186 */ 187static boolean_t 188zfs_is_bootfs(const char *name) 189{ 190 objset_t *os; 191 192 if (dmu_objset_hold(name, FTAG, &os) == 0) { 193 boolean_t ret; 194 ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os))); 195 dmu_objset_rele(os, FTAG); 196 return (ret); 197 } 198 return (B_FALSE); 199} 200 201/* 202 * zfs_earlier_version 203 * 204 * Return non-zero if the spa version is less than requested version. 205 */ 206static int 207zfs_earlier_version(const char *name, int version) 208{ 209 spa_t *spa; 210 211 if (spa_open(name, &spa, FTAG) == 0) { 212 if (spa_version(spa) < version) { 213 spa_close(spa, FTAG); 214 return (1); 215 } 216 spa_close(spa, FTAG); 217 } 218 return (0); 219} 220 221/* 222 * zpl_earlier_version 223 * 224 * Return TRUE if the ZPL version is less than requested version. 225 */ 226static boolean_t 227zpl_earlier_version(const char *name, int version) 228{ 229 objset_t *os; 230 boolean_t rc = B_TRUE; 231 232 if (dmu_objset_hold(name, FTAG, &os) == 0) { 233 uint64_t zplversion; 234 235 if (dmu_objset_type(os) != DMU_OST_ZFS) { 236 dmu_objset_rele(os, FTAG); 237 return (B_TRUE); 238 } 239 /* XXX reading from non-owned objset */ 240 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0) 241 rc = zplversion < version; 242 dmu_objset_rele(os, FTAG); 243 } 244 return (rc); 245} 246 247static void 248zfs_log_history(zfs_cmd_t *zc) 249{ 250 spa_t *spa; 251 char *buf; 252 253 if ((buf = history_str_get(zc)) == NULL) 254 return; 255 256 if (spa_open(zc->zc_name, &spa, FTAG) == 0) { 257 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY) 258 (void) spa_history_log(spa, buf, LOG_CMD_NORMAL); 259 spa_close(spa, FTAG); 260 } 261 history_str_free(buf); 262} 263 264/* 265 * Policy for top-level read operations (list pools). Requires no privileges, 266 * and can be used in the local zone, as there is no associated dataset. 267 */ 268/* ARGSUSED */ 269static int 270zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr) 271{ 272 return (0); 273} 274 275/* 276 * Policy for dataset read operations (list children, get statistics). Requires 277 * no privileges, but must be visible in the local zone. 278 */ 279/* ARGSUSED */ 280static int 281zfs_secpolicy_read(zfs_cmd_t *zc, cred_t *cr) 282{ 283 if (INGLOBALZONE(curthread) || 284 zone_dataset_visible(zc->zc_name, NULL)) 285 return (0); 286 287 return (ENOENT); 288} 289 290static int 291zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr) 292{ 293 int writable = 1; 294 295 /* 296 * The dataset must be visible by this zone -- check this first 297 * so they don't see EPERM on something they shouldn't know about. 298 */ 299 if (!INGLOBALZONE(curthread) && 300 !zone_dataset_visible(dataset, &writable)) 301 return (ENOENT); 302 303 if (INGLOBALZONE(curthread)) { 304 /* 305 * If the fs is zoned, only root can access it from the 306 * global zone. 307 */ 308 if (secpolicy_zfs(cr) && zoned) 309 return (EPERM); 310 } else { 311 /* 312 * If we are in a local zone, the 'zoned' property must be set. 313 */ 314 if (!zoned) 315 return (EPERM); 316 317 /* must be writable by this zone */ 318 if (!writable) 319 return (EPERM); 320 } 321 return (0); 322} 323 324static int 325zfs_dozonecheck(const char *dataset, cred_t *cr) 326{ 327 uint64_t zoned; 328 329 if (dsl_prop_get_integer(dataset, "jailed", &zoned, NULL)) 330 return (ENOENT); 331 332 return (zfs_dozonecheck_impl(dataset, zoned, cr)); 333} 334 335static int 336zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr) 337{ 338 uint64_t zoned; 339 340 rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER); 341 if (dsl_prop_get_ds(ds, "jailed", 8, 1, &zoned, NULL)) { 342 rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock); 343 return (ENOENT); 344 } 345 rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock); 346 347 return (zfs_dozonecheck_impl(dataset, zoned, cr)); 348} 349 350int 351zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr) 352{ 353 int error; 354 355 error = zfs_dozonecheck(name, cr); 356 if (error == 0) { 357 error = secpolicy_zfs(cr); 358 if (error) 359 error = dsl_deleg_access(name, perm, cr); 360 } 361 return (error); 362} 363 364int 365zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds, 366 const char *perm, cred_t *cr) 367{ 368 int error; 369 370 error = zfs_dozonecheck_ds(name, ds, cr); 371 if (error == 0) { 372 error = secpolicy_zfs(cr); 373 if (error) 374 error = dsl_deleg_access_impl(ds, perm, cr); 375 } 376 return (error); 377} 378 379#ifdef SECLABEL 380/* 381 * Policy for setting the security label property. 382 * 383 * Returns 0 for success, non-zero for access and other errors. 384 */ 385static int 386zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr) 387{ 388 char ds_hexsl[MAXNAMELEN]; 389 bslabel_t ds_sl, new_sl; 390 boolean_t new_default = FALSE; 391 uint64_t zoned; 392 int needed_priv = -1; 393 int error; 394 395 /* First get the existing dataset label. */ 396 error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL), 397 1, sizeof (ds_hexsl), &ds_hexsl, NULL); 398 if (error) 399 return (EPERM); 400 401 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0) 402 new_default = TRUE; 403 404 /* The label must be translatable */ 405 if (!new_default && (hexstr_to_label(strval, &new_sl) != 0)) 406 return (EINVAL); 407 408 /* 409 * In a non-global zone, disallow attempts to set a label that 410 * doesn't match that of the zone; otherwise no other checks 411 * are needed. 412 */ 413 if (!INGLOBALZONE(curproc)) { 414 if (new_default || !blequal(&new_sl, CR_SL(CRED()))) 415 return (EPERM); 416 return (0); 417 } 418 419 /* 420 * For global-zone datasets (i.e., those whose zoned property is 421 * "off", verify that the specified new label is valid for the 422 * global zone. 423 */ 424 if (dsl_prop_get_integer(name, 425 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL)) 426 return (EPERM); 427 if (!zoned) { 428 if (zfs_check_global_label(name, strval) != 0) 429 return (EPERM); 430 } 431 432 /* 433 * If the existing dataset label is nondefault, check if the 434 * dataset is mounted (label cannot be changed while mounted). 435 * Get the zfsvfs; if there isn't one, then the dataset isn't 436 * mounted (or isn't a dataset, doesn't exist, ...). 437 */ 438 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) { 439 objset_t *os; 440 static char *setsl_tag = "setsl_tag"; 441 442 /* 443 * Try to own the dataset; abort if there is any error, 444 * (e.g., already mounted, in use, or other error). 445 */ 446 error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE, 447 setsl_tag, &os); 448 if (error) 449 return (EPERM); 450 451 dmu_objset_disown(os, setsl_tag); 452 453 if (new_default) { 454 needed_priv = PRIV_FILE_DOWNGRADE_SL; 455 goto out_check; 456 } 457 458 if (hexstr_to_label(strval, &new_sl) != 0) 459 return (EPERM); 460 461 if (blstrictdom(&ds_sl, &new_sl)) 462 needed_priv = PRIV_FILE_DOWNGRADE_SL; 463 else if (blstrictdom(&new_sl, &ds_sl)) 464 needed_priv = PRIV_FILE_UPGRADE_SL; 465 } else { 466 /* dataset currently has a default label */ 467 if (!new_default) 468 needed_priv = PRIV_FILE_UPGRADE_SL; 469 } 470 471out_check: 472 if (needed_priv != -1) 473 return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL)); 474 return (0); 475} 476#endif /* SECLABEL */ 477 478static int 479zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval, 480 cred_t *cr) 481{ 482 char *strval; 483 484 /* 485 * Check permissions for special properties. 486 */ 487 switch (prop) { 488 case ZFS_PROP_ZONED: 489 /* 490 * Disallow setting of 'zoned' from within a local zone. 491 */ 492 if (!INGLOBALZONE(curthread)) 493 return (EPERM); 494 break; 495 496 case ZFS_PROP_QUOTA: 497 if (!INGLOBALZONE(curthread)) { 498 uint64_t zoned; 499 char setpoint[MAXNAMELEN]; 500 /* 501 * Unprivileged users are allowed to modify the 502 * quota on things *under* (ie. contained by) 503 * the thing they own. 504 */ 505 if (dsl_prop_get_integer(dsname, "jailed", &zoned, 506 setpoint)) 507 return (EPERM); 508 if (!zoned || strlen(dsname) <= strlen(setpoint)) 509 return (EPERM); 510 } 511 break; 512 513 case ZFS_PROP_MLSLABEL: 514#ifdef SECLABEL 515 if (!is_system_labeled()) 516 return (EPERM); 517 518 if (nvpair_value_string(propval, &strval) == 0) { 519 int err; 520 521 err = zfs_set_slabel_policy(dsname, strval, CRED()); 522 if (err != 0) 523 return (err); 524 } 525#else 526 return (EOPNOTSUPP); 527#endif 528 break; 529 } 530 531 return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr)); 532} 533 534int 535zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr) 536{ 537 int error; 538 539 error = zfs_dozonecheck(zc->zc_name, cr); 540 if (error) 541 return (error); 542 543 /* 544 * permission to set permissions will be evaluated later in 545 * dsl_deleg_can_allow() 546 */ 547 return (0); 548} 549 550int 551zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr) 552{ 553 return (zfs_secpolicy_write_perms(zc->zc_name, 554 ZFS_DELEG_PERM_ROLLBACK, cr)); 555} 556 557int 558zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr) 559{ 560 spa_t *spa; 561 dsl_pool_t *dp; 562 dsl_dataset_t *ds; 563 char *cp; 564 int error; 565 566 /* 567 * Generate the current snapshot name from the given objsetid, then 568 * use that name for the secpolicy/zone checks. 569 */ 570 cp = strchr(zc->zc_name, '@'); 571 if (cp == NULL) 572 return (EINVAL); 573 error = spa_open(zc->zc_name, &spa, FTAG); 574 if (error) 575 return (error); 576 577 dp = spa_get_dsl(spa); 578 rw_enter(&dp->dp_config_rwlock, RW_READER); 579 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds); 580 rw_exit(&dp->dp_config_rwlock); 581 spa_close(spa, FTAG); 582 if (error) 583 return (error); 584 585 dsl_dataset_name(ds, zc->zc_name); 586 587 error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds, 588 ZFS_DELEG_PERM_SEND, cr); 589 dsl_dataset_rele(ds, FTAG); 590 591 return (error); 592} 593 594static int 595zfs_secpolicy_deleg_share(zfs_cmd_t *zc, cred_t *cr) 596{ 597 vnode_t *vp; 598 int error; 599 600 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE, 601 NO_FOLLOW, NULL, &vp)) != 0) 602 return (error); 603 604 /* Now make sure mntpnt and dataset are ZFS */ 605 606 if (strcmp(vp->v_vfsp->mnt_stat.f_fstypename, "zfs") != 0 || 607 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource), 608 zc->zc_name) != 0)) { 609 VN_RELE(vp); 610 return (EPERM); 611 } 612 613 VN_RELE(vp); 614 return (dsl_deleg_access(zc->zc_name, 615 ZFS_DELEG_PERM_SHARE, cr)); 616} 617 618int 619zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr) 620{ 621 if (!INGLOBALZONE(curthread)) 622 return (EPERM); 623 624 if (secpolicy_nfs(cr) == 0) { 625 return (0); 626 } else { 627 return (zfs_secpolicy_deleg_share(zc, cr)); 628 } 629} 630 631int 632zfs_secpolicy_smb_acl(zfs_cmd_t *zc, cred_t *cr) 633{ 634 if (!INGLOBALZONE(curthread)) 635 return (EPERM); 636 637 if (secpolicy_smb(cr) == 0) { 638 return (0); 639 } else { 640 return (zfs_secpolicy_deleg_share(zc, cr)); 641 } 642} 643 644static int 645zfs_get_parent(const char *datasetname, char *parent, int parentsize) 646{ 647 char *cp; 648 649 /* 650 * Remove the @bla or /bla from the end of the name to get the parent. 651 */ 652 (void) strncpy(parent, datasetname, parentsize); 653 cp = strrchr(parent, '@'); 654 if (cp != NULL) { 655 cp[0] = '\0'; 656 } else { 657 cp = strrchr(parent, '/'); 658 if (cp == NULL) 659 return (ENOENT); 660 cp[0] = '\0'; 661 } 662 663 return (0); 664} 665 666int 667zfs_secpolicy_destroy_perms(const char *name, cred_t *cr) 668{ 669 int error; 670 671 if ((error = zfs_secpolicy_write_perms(name, 672 ZFS_DELEG_PERM_MOUNT, cr)) != 0) 673 return (error); 674 675 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr)); 676} 677 678static int 679zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr) 680{ 681 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr)); 682} 683 684/* 685 * Destroying snapshots with delegated permissions requires 686 * descendent mount and destroy permissions. 687 * Reassemble the full filesystem@snap name so dsl_deleg_access() 688 * can do the correct permission check. 689 * 690 * Since this routine is used when doing a recursive destroy of snapshots 691 * and destroying snapshots requires descendent permissions, a successfull 692 * check of the top level snapshot applies to snapshots of all descendent 693 * datasets as well. 694 * 695 * The top level snapshot may not exist when doing a recursive destroy. 696 * In this case fallback to permissions of the parent dataset. 697 */ 698static int 699zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, cred_t *cr) 700{ 701 int error; 702 char *dsname; 703 704 dsname = kmem_asprintf("%s@%s", zc->zc_name, zc->zc_value); 705 706 error = zfs_secpolicy_destroy_perms(dsname, cr); 707 708 if (error == ENOENT) 709 error = zfs_secpolicy_destroy_perms(zc->zc_name, cr); 710 711 strfree(dsname); 712 return (error); 713} 714 715int 716zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr) 717{ 718 char parentname[MAXNAMELEN]; 719 int error; 720 721 if ((error = zfs_secpolicy_write_perms(from, 722 ZFS_DELEG_PERM_RENAME, cr)) != 0) 723 return (error); 724 725 if ((error = zfs_secpolicy_write_perms(from, 726 ZFS_DELEG_PERM_MOUNT, cr)) != 0) 727 return (error); 728 729 if ((error = zfs_get_parent(to, parentname, 730 sizeof (parentname))) != 0) 731 return (error); 732 733 if ((error = zfs_secpolicy_write_perms(parentname, 734 ZFS_DELEG_PERM_CREATE, cr)) != 0) 735 return (error); 736 737 if ((error = zfs_secpolicy_write_perms(parentname, 738 ZFS_DELEG_PERM_MOUNT, cr)) != 0) 739 return (error); 740 741 return (error); 742} 743 744static int 745zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr) 746{ 747 return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr)); 748} 749 750static int 751zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr) 752{ 753 char parentname[MAXNAMELEN]; 754 objset_t *clone; 755 int error; 756 757 error = zfs_secpolicy_write_perms(zc->zc_name, 758 ZFS_DELEG_PERM_PROMOTE, cr); 759 if (error) 760 return (error); 761 762 error = dmu_objset_hold(zc->zc_name, FTAG, &clone); 763 764 if (error == 0) { 765 dsl_dataset_t *pclone = NULL; 766 dsl_dir_t *dd; 767 dd = clone->os_dsl_dataset->ds_dir; 768 769 rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER); 770 error = dsl_dataset_hold_obj(dd->dd_pool, 771 dd->dd_phys->dd_origin_obj, FTAG, &pclone); 772 rw_exit(&dd->dd_pool->dp_config_rwlock); 773 if (error) { 774 dmu_objset_rele(clone, FTAG); 775 return (error); 776 } 777 778 error = zfs_secpolicy_write_perms(zc->zc_name, 779 ZFS_DELEG_PERM_MOUNT, cr); 780 781 dsl_dataset_name(pclone, parentname); 782 dmu_objset_rele(clone, FTAG); 783 dsl_dataset_rele(pclone, FTAG); 784 if (error == 0) 785 error = zfs_secpolicy_write_perms(parentname, 786 ZFS_DELEG_PERM_PROMOTE, cr); 787 } 788 return (error); 789} 790 791static int 792zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr) 793{ 794 int error; 795 796 if ((error = zfs_secpolicy_write_perms(zc->zc_name, 797 ZFS_DELEG_PERM_RECEIVE, cr)) != 0) 798 return (error); 799 800 if ((error = zfs_secpolicy_write_perms(zc->zc_name, 801 ZFS_DELEG_PERM_MOUNT, cr)) != 0) 802 return (error); 803 804 return (zfs_secpolicy_write_perms(zc->zc_name, 805 ZFS_DELEG_PERM_CREATE, cr)); 806} 807 808int 809zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr) 810{ 811 return (zfs_secpolicy_write_perms(name, 812 ZFS_DELEG_PERM_SNAPSHOT, cr)); 813} 814 815static int 816zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr) 817{ 818 819 return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr)); 820} 821 822static int 823zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr) 824{ 825 char parentname[MAXNAMELEN]; 826 int error; 827 828 if ((error = zfs_get_parent(zc->zc_name, parentname, 829 sizeof (parentname))) != 0) 830 return (error); 831 832 if (zc->zc_value[0] != '\0') { 833 if ((error = zfs_secpolicy_write_perms(zc->zc_value, 834 ZFS_DELEG_PERM_CLONE, cr)) != 0) 835 return (error); 836 } 837 838 if ((error = zfs_secpolicy_write_perms(parentname, 839 ZFS_DELEG_PERM_CREATE, cr)) != 0) 840 return (error); 841 842 error = zfs_secpolicy_write_perms(parentname, 843 ZFS_DELEG_PERM_MOUNT, cr); 844 845 return (error); 846} 847 848static int 849zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr) 850{ 851 int error; 852 853 error = secpolicy_fs_unmount(cr, NULL); 854 if (error) { 855 error = dsl_deleg_access(zc->zc_name, ZFS_DELEG_PERM_MOUNT, cr); 856 } 857 return (error); 858} 859 860/* 861 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires 862 * SYS_CONFIG privilege, which is not available in a local zone. 863 */ 864/* ARGSUSED */ 865static int 866zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr) 867{ 868 if (secpolicy_sys_config(cr, B_FALSE) != 0) 869 return (EPERM); 870 871 return (0); 872} 873 874/* 875 * Policy for object to name lookups. 876 */ 877/* ARGSUSED */ 878static int 879zfs_secpolicy_diff(zfs_cmd_t *zc, cred_t *cr) 880{ 881 int error; 882 883 if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0) 884 return (0); 885 886 error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr); 887 return (error); 888} 889 890/* 891 * Policy for fault injection. Requires all privileges. 892 */ 893/* ARGSUSED */ 894static int 895zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr) 896{ 897 return (secpolicy_zinject(cr)); 898} 899 900static int 901zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr) 902{ 903 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value); 904 905 if (prop == ZPROP_INVAL) { 906 if (!zfs_prop_user(zc->zc_value)) 907 return (EINVAL); 908 return (zfs_secpolicy_write_perms(zc->zc_name, 909 ZFS_DELEG_PERM_USERPROP, cr)); 910 } else { 911 return (zfs_secpolicy_setprop(zc->zc_name, prop, 912 NULL, cr)); 913 } 914} 915 916static int 917zfs_secpolicy_userspace_one(zfs_cmd_t *zc, cred_t *cr) 918{ 919 int err = zfs_secpolicy_read(zc, cr); 920 if (err) 921 return (err); 922 923 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) 924 return (EINVAL); 925 926 if (zc->zc_value[0] == 0) { 927 /* 928 * They are asking about a posix uid/gid. If it's 929 * themself, allow it. 930 */ 931 if (zc->zc_objset_type == ZFS_PROP_USERUSED || 932 zc->zc_objset_type == ZFS_PROP_USERQUOTA) { 933 if (zc->zc_guid == crgetuid(cr)) 934 return (0); 935 } else { 936 if (groupmember(zc->zc_guid, cr)) 937 return (0); 938 } 939 } 940 941 return (zfs_secpolicy_write_perms(zc->zc_name, 942 userquota_perms[zc->zc_objset_type], cr)); 943} 944 945static int 946zfs_secpolicy_userspace_many(zfs_cmd_t *zc, cred_t *cr) 947{ 948 int err = zfs_secpolicy_read(zc, cr); 949 if (err) 950 return (err); 951 952 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) 953 return (EINVAL); 954 955 return (zfs_secpolicy_write_perms(zc->zc_name, 956 userquota_perms[zc->zc_objset_type], cr)); 957} 958 959static int 960zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, cred_t *cr) 961{ 962 return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION, 963 NULL, cr)); 964} 965 966static int 967zfs_secpolicy_hold(zfs_cmd_t *zc, cred_t *cr) 968{ 969 return (zfs_secpolicy_write_perms(zc->zc_name, 970 ZFS_DELEG_PERM_HOLD, cr)); 971} 972 973static int 974zfs_secpolicy_release(zfs_cmd_t *zc, cred_t *cr) 975{ 976 return (zfs_secpolicy_write_perms(zc->zc_name, 977 ZFS_DELEG_PERM_RELEASE, cr)); 978} 979 980/* 981 * Policy for allowing temporary snapshots to be taken or released 982 */ 983static int 984zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, cred_t *cr) 985{ 986 /* 987 * A temporary snapshot is the same as a snapshot, 988 * hold, destroy and release all rolled into one. 989 * Delegated diff alone is sufficient that we allow this. 990 */ 991 int error; 992 993 if ((error = zfs_secpolicy_write_perms(zc->zc_name, 994 ZFS_DELEG_PERM_DIFF, cr)) == 0) 995 return (0); 996 997 error = zfs_secpolicy_snapshot(zc, cr); 998 if (!error) 999 error = zfs_secpolicy_hold(zc, cr); 1000 if (!error) 1001 error = zfs_secpolicy_release(zc, cr); 1002 if (!error) 1003 error = zfs_secpolicy_destroy(zc, cr); 1004 return (error); 1005} 1006 1007/* 1008 * Returns the nvlist as specified by the user in the zfs_cmd_t. 1009 */ 1010static int 1011get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp) 1012{ 1013 char *packed; 1014 int error; 1015 nvlist_t *list = NULL; 1016 1017 /* 1018 * Read in and unpack the user-supplied nvlist. 1019 */ 1020 if (size == 0) 1021 return (EINVAL); 1022 1023 packed = kmem_alloc(size, KM_SLEEP); 1024 1025 if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size, 1026 iflag)) != 0) { 1027 kmem_free(packed, size); 1028 return (error); 1029 } 1030 1031 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) { 1032 kmem_free(packed, size); 1033 return (error); 1034 } 1035 1036 kmem_free(packed, size); 1037 1038 *nvp = list; 1039 return (0); 1040} 1041 1042static int 1043fit_error_list(zfs_cmd_t *zc, nvlist_t **errors) 1044{ 1045 size_t size; 1046 1047 VERIFY(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0); 1048 1049 if (size > zc->zc_nvlist_dst_size) { 1050 nvpair_t *more_errors; 1051 int n = 0; 1052 1053 if (zc->zc_nvlist_dst_size < 1024) 1054 return (ENOMEM); 1055 1056 VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, 0) == 0); 1057 more_errors = nvlist_prev_nvpair(*errors, NULL); 1058 1059 do { 1060 nvpair_t *pair = nvlist_prev_nvpair(*errors, 1061 more_errors); 1062 VERIFY(nvlist_remove_nvpair(*errors, pair) == 0); 1063 n++; 1064 VERIFY(nvlist_size(*errors, &size, 1065 NV_ENCODE_NATIVE) == 0); 1066 } while (size > zc->zc_nvlist_dst_size); 1067 1068 VERIFY(nvlist_remove_nvpair(*errors, more_errors) == 0); 1069 VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, n) == 0); 1070 ASSERT(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0); 1071 ASSERT(size <= zc->zc_nvlist_dst_size); 1072 } 1073 1074 return (0); 1075} 1076 1077static int 1078put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl) 1079{ 1080 char *packed = NULL; 1081 int error = 0; 1082 size_t size; 1083 1084 VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0); 1085 1086 if (size > zc->zc_nvlist_dst_size) { 1087 /* 1088 * Solaris returns ENOMEM here, because even if an error is 1089 * returned from an ioctl(2), new zc_nvlist_dst_size will be 1090 * passed to the userland. This is not the case for FreeBSD. 1091 * We need to return 0, so the kernel will copy the 1092 * zc_nvlist_dst_size back and the userland can discover that a 1093 * bigger buffer is needed. 1094 */ 1095 error = 0; 1096 } else { 1097 packed = kmem_alloc(size, KM_SLEEP); 1098 VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE, 1099 KM_SLEEP) == 0); 1100 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst, 1101 size, zc->zc_iflags) != 0) 1102 error = EFAULT; 1103 kmem_free(packed, size); 1104 } 1105 1106 zc->zc_nvlist_dst_size = size; 1107 return (error); 1108} 1109 1110static int 1111getzfsvfs(const char *dsname, zfsvfs_t **zfvp) 1112{ 1113 objset_t *os; 1114 int error; 1115 1116 error = dmu_objset_hold(dsname, FTAG, &os); 1117 if (error) 1118 return (error); 1119 if (dmu_objset_type(os) != DMU_OST_ZFS) { 1120 dmu_objset_rele(os, FTAG); 1121 return (EINVAL); 1122 } 1123 1124 mutex_enter(&os->os_user_ptr_lock); 1125 *zfvp = dmu_objset_get_user(os); 1126 if (*zfvp) { 1127 VFS_HOLD((*zfvp)->z_vfs); 1128 } else { 1129 error = ESRCH; 1130 } 1131 mutex_exit(&os->os_user_ptr_lock); 1132 dmu_objset_rele(os, FTAG); 1133 return (error); 1134} 1135 1136/* 1137 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which 1138 * case its z_vfs will be NULL, and it will be opened as the owner. 1139 */ 1140static int 1141zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer) 1142{ 1143 int error = 0; 1144 1145 if (getzfsvfs(name, zfvp) != 0) 1146 error = zfsvfs_create(name, zfvp); 1147 if (error == 0) { 1148 rrw_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER : 1149 RW_READER, tag); 1150 if ((*zfvp)->z_unmounted) { 1151 /* 1152 * XXX we could probably try again, since the unmounting 1153 * thread should be just about to disassociate the 1154 * objset from the zfsvfs. 1155 */ 1156 rrw_exit(&(*zfvp)->z_teardown_lock, tag); 1157 return (EBUSY); 1158 } 1159 } 1160 return (error); 1161} 1162 1163static void 1164zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag) 1165{ 1166 rrw_exit(&zfsvfs->z_teardown_lock, tag); 1167 1168 if (zfsvfs->z_vfs) { 1169 VFS_RELE(zfsvfs->z_vfs); 1170 } else { 1171 dmu_objset_disown(zfsvfs->z_os, zfsvfs); 1172 zfsvfs_free(zfsvfs); 1173 } 1174} 1175 1176static int 1177zfs_ioc_pool_create(zfs_cmd_t *zc) 1178{ 1179 int error; 1180 nvlist_t *config, *props = NULL; 1181 nvlist_t *rootprops = NULL; 1182 nvlist_t *zplprops = NULL; 1183 char *buf; 1184 1185 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 1186 zc->zc_iflags, &config)) 1187 return (error); 1188 1189 if (zc->zc_nvlist_src_size != 0 && (error = 1190 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 1191 zc->zc_iflags, &props))) { 1192 nvlist_free(config); 1193 return (error); 1194 } 1195 1196 if (props) { 1197 nvlist_t *nvl = NULL; 1198 uint64_t version = SPA_VERSION; 1199 1200 (void) nvlist_lookup_uint64(props, 1201 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version); 1202 if (version < SPA_VERSION_INITIAL || version > SPA_VERSION) { 1203 error = EINVAL; 1204 goto pool_props_bad; 1205 } 1206 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl); 1207 if (nvl) { 1208 error = nvlist_dup(nvl, &rootprops, KM_SLEEP); 1209 if (error != 0) { 1210 nvlist_free(config); 1211 nvlist_free(props); 1212 return (error); 1213 } 1214 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS); 1215 } 1216 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1217 error = zfs_fill_zplprops_root(version, rootprops, 1218 zplprops, NULL); 1219 if (error) 1220 goto pool_props_bad; 1221 } 1222 1223 buf = history_str_get(zc); 1224 1225 error = spa_create(zc->zc_name, config, props, buf, zplprops); 1226 1227 /* 1228 * Set the remaining root properties 1229 */ 1230 if (!error && (error = zfs_set_prop_nvlist(zc->zc_name, 1231 ZPROP_SRC_LOCAL, rootprops, NULL)) != 0) 1232 (void) spa_destroy(zc->zc_name); 1233 1234 if (buf != NULL) 1235 history_str_free(buf); 1236 1237pool_props_bad: 1238 nvlist_free(rootprops); 1239 nvlist_free(zplprops); 1240 nvlist_free(config); 1241 nvlist_free(props); 1242 1243 return (error); 1244} 1245 1246static int 1247zfs_ioc_pool_destroy(zfs_cmd_t *zc) 1248{ 1249 int error; 1250 zfs_log_history(zc); 1251 error = spa_destroy(zc->zc_name); 1252 if (error == 0) 1253 zvol_remove_minors(zc->zc_name); 1254 return (error); 1255} 1256 1257static int 1258zfs_ioc_pool_import(zfs_cmd_t *zc) 1259{ 1260 nvlist_t *config, *props = NULL; 1261 uint64_t guid; 1262 int error; 1263 1264 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 1265 zc->zc_iflags, &config)) != 0) 1266 return (error); 1267 1268 if (zc->zc_nvlist_src_size != 0 && (error = 1269 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 1270 zc->zc_iflags, &props))) { 1271 nvlist_free(config); 1272 return (error); 1273 } 1274 1275 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 || 1276 guid != zc->zc_guid) 1277 error = EINVAL; 1278 else 1279 error = spa_import(zc->zc_name, config, props, zc->zc_cookie); 1280 1281 if (zc->zc_nvlist_dst != 0) { 1282 int err; 1283 1284 if ((err = put_nvlist(zc, config)) != 0) 1285 error = err; 1286 } 1287 1288 nvlist_free(config); 1289 1290 if (props) 1291 nvlist_free(props); 1292 1293 return (error); 1294} 1295 1296static int 1297zfs_ioc_pool_export(zfs_cmd_t *zc) 1298{ 1299 int error; 1300 boolean_t force = (boolean_t)zc->zc_cookie; 1301 boolean_t hardforce = (boolean_t)zc->zc_guid; 1302 1303 zfs_log_history(zc); 1304 error = spa_export(zc->zc_name, NULL, force, hardforce); 1305 if (error == 0) 1306 zvol_remove_minors(zc->zc_name); 1307 return (error); 1308} 1309 1310static int 1311zfs_ioc_pool_configs(zfs_cmd_t *zc) 1312{ 1313 nvlist_t *configs; 1314 int error; 1315 1316 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL) 1317 return (EEXIST); 1318 1319 error = put_nvlist(zc, configs); 1320 1321 nvlist_free(configs); 1322 1323 return (error); 1324} 1325 1326static int 1327zfs_ioc_pool_stats(zfs_cmd_t *zc) 1328{ 1329 nvlist_t *config; 1330 int error; 1331 int ret = 0; 1332 1333 error = spa_get_stats(zc->zc_name, &config, zc->zc_value, 1334 sizeof (zc->zc_value)); 1335 1336 if (config != NULL) { 1337 ret = put_nvlist(zc, config); 1338 nvlist_free(config); 1339 1340 /* 1341 * The config may be present even if 'error' is non-zero. 1342 * In this case we return success, and preserve the real errno 1343 * in 'zc_cookie'. 1344 */ 1345 zc->zc_cookie = error; 1346 } else { 1347 ret = error; 1348 } 1349 1350 return (ret); 1351} 1352 1353/* 1354 * Try to import the given pool, returning pool stats as appropriate so that 1355 * user land knows which devices are available and overall pool health. 1356 */ 1357static int 1358zfs_ioc_pool_tryimport(zfs_cmd_t *zc) 1359{ 1360 nvlist_t *tryconfig, *config; 1361 int error; 1362 1363 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 1364 zc->zc_iflags, &tryconfig)) != 0) 1365 return (error); 1366 1367 config = spa_tryimport(tryconfig); 1368 1369 nvlist_free(tryconfig); 1370 1371 if (config == NULL) 1372 return (EINVAL); 1373 1374 error = put_nvlist(zc, config); 1375 nvlist_free(config); 1376 1377 return (error); 1378} 1379 1380/* 1381 * inputs: 1382 * zc_name name of the pool 1383 * zc_cookie scan func (pool_scan_func_t) 1384 */ 1385static int 1386zfs_ioc_pool_scan(zfs_cmd_t *zc) 1387{ 1388 spa_t *spa; 1389 int error; 1390 1391 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1392 return (error); 1393 1394 if (zc->zc_cookie == POOL_SCAN_NONE) 1395 error = spa_scan_stop(spa); 1396 else 1397 error = spa_scan(spa, zc->zc_cookie); 1398 1399 spa_close(spa, FTAG); 1400 1401 return (error); 1402} 1403 1404static int 1405zfs_ioc_pool_freeze(zfs_cmd_t *zc) 1406{ 1407 spa_t *spa; 1408 int error; 1409 1410 error = spa_open(zc->zc_name, &spa, FTAG); 1411 if (error == 0) { 1412 spa_freeze(spa); 1413 spa_close(spa, FTAG); 1414 } 1415 return (error); 1416} 1417 1418static int 1419zfs_ioc_pool_upgrade(zfs_cmd_t *zc) 1420{ 1421 spa_t *spa; 1422 int error; 1423 1424 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1425 return (error); 1426 1427 if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) { 1428 spa_close(spa, FTAG); 1429 return (EINVAL); 1430 } 1431 1432 spa_upgrade(spa, zc->zc_cookie); 1433 spa_close(spa, FTAG); 1434 1435 return (error); 1436} 1437 1438static int 1439zfs_ioc_pool_get_history(zfs_cmd_t *zc) 1440{ 1441 spa_t *spa; 1442 char *hist_buf; 1443 uint64_t size; 1444 int error; 1445 1446 if ((size = zc->zc_history_len) == 0) 1447 return (EINVAL); 1448 1449 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1450 return (error); 1451 1452 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) { 1453 spa_close(spa, FTAG); 1454 return (ENOTSUP); 1455 } 1456 1457 hist_buf = kmem_alloc(size, KM_SLEEP); 1458 if ((error = spa_history_get(spa, &zc->zc_history_offset, 1459 &zc->zc_history_len, hist_buf)) == 0) { 1460 error = ddi_copyout(hist_buf, 1461 (void *)(uintptr_t)zc->zc_history, 1462 zc->zc_history_len, zc->zc_iflags); 1463 } 1464 1465 spa_close(spa, FTAG); 1466 kmem_free(hist_buf, size); 1467 return (error); 1468} 1469 1470static int 1471zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc) 1472{ 1473 int error; 1474 1475 if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value)) 1476 return (error); 1477 1478 return (0); 1479} 1480 1481/* 1482 * inputs: 1483 * zc_name name of filesystem 1484 * zc_obj object to find 1485 * 1486 * outputs: 1487 * zc_value name of object 1488 */ 1489static int 1490zfs_ioc_obj_to_path(zfs_cmd_t *zc) 1491{ 1492 objset_t *os; 1493 int error; 1494 1495 /* XXX reading from objset not owned */ 1496 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0) 1497 return (error); 1498 if (dmu_objset_type(os) != DMU_OST_ZFS) { 1499 dmu_objset_rele(os, FTAG); 1500 return (EINVAL); 1501 } 1502 error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value, 1503 sizeof (zc->zc_value)); 1504 dmu_objset_rele(os, FTAG); 1505 1506 return (error); 1507} 1508 1509/* 1510 * inputs: 1511 * zc_name name of filesystem 1512 * zc_obj object to find 1513 * 1514 * outputs: 1515 * zc_stat stats on object 1516 * zc_value path to object 1517 */ 1518static int 1519zfs_ioc_obj_to_stats(zfs_cmd_t *zc) 1520{ 1521 objset_t *os; 1522 int error; 1523 1524 /* XXX reading from objset not owned */ 1525 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0) 1526 return (error); 1527 if (dmu_objset_type(os) != DMU_OST_ZFS) { 1528 dmu_objset_rele(os, FTAG); 1529 return (EINVAL); 1530 } 1531 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value, 1532 sizeof (zc->zc_value)); 1533 dmu_objset_rele(os, FTAG); 1534 1535 return (error); 1536} 1537 1538static int 1539zfs_ioc_vdev_add(zfs_cmd_t *zc) 1540{ 1541 spa_t *spa; 1542 int error; 1543 nvlist_t *config, **l2cache, **spares; 1544 uint_t nl2cache = 0, nspares = 0; 1545 1546 error = spa_open(zc->zc_name, &spa, FTAG); 1547 if (error != 0) 1548 return (error); 1549 1550 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 1551 zc->zc_iflags, &config); 1552 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE, 1553 &l2cache, &nl2cache); 1554 1555 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES, 1556 &spares, &nspares); 1557 1558 /* 1559 * A root pool with concatenated devices is not supported. 1560 * Thus, can not add a device to a root pool. 1561 * 1562 * Intent log device can not be added to a rootpool because 1563 * during mountroot, zil is replayed, a seperated log device 1564 * can not be accessed during the mountroot time. 1565 * 1566 * l2cache and spare devices are ok to be added to a rootpool. 1567 */ 1568 if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) { 1569 nvlist_free(config); 1570 spa_close(spa, FTAG); 1571 return (EDOM); 1572 } 1573 1574 if (error == 0) { 1575 error = spa_vdev_add(spa, config); 1576 nvlist_free(config); 1577 } 1578 spa_close(spa, FTAG); 1579 return (error); 1580} 1581 1582/* 1583 * inputs: 1584 * zc_name name of the pool 1585 * zc_nvlist_conf nvlist of devices to remove 1586 * zc_cookie to stop the remove? 1587 */ 1588static int 1589zfs_ioc_vdev_remove(zfs_cmd_t *zc) 1590{ 1591 spa_t *spa; 1592 int error; 1593 1594 error = spa_open(zc->zc_name, &spa, FTAG); 1595 if (error != 0) 1596 return (error); 1597 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE); 1598 spa_close(spa, FTAG); 1599 return (error); 1600} 1601 1602static int 1603zfs_ioc_vdev_set_state(zfs_cmd_t *zc) 1604{ 1605 spa_t *spa; 1606 int error; 1607 vdev_state_t newstate = VDEV_STATE_UNKNOWN; 1608 1609 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1610 return (error); 1611 switch (zc->zc_cookie) { 1612 case VDEV_STATE_ONLINE: 1613 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate); 1614 break; 1615 1616 case VDEV_STATE_OFFLINE: 1617 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj); 1618 break; 1619 1620 case VDEV_STATE_FAULTED: 1621 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED && 1622 zc->zc_obj != VDEV_AUX_EXTERNAL) 1623 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED; 1624 1625 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj); 1626 break; 1627 1628 case VDEV_STATE_DEGRADED: 1629 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED && 1630 zc->zc_obj != VDEV_AUX_EXTERNAL) 1631 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED; 1632 1633 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj); 1634 break; 1635 1636 default: 1637 error = EINVAL; 1638 } 1639 zc->zc_cookie = newstate; 1640 spa_close(spa, FTAG); 1641 return (error); 1642} 1643 1644static int 1645zfs_ioc_vdev_attach(zfs_cmd_t *zc) 1646{ 1647 spa_t *spa; 1648 int replacing = zc->zc_cookie; 1649 nvlist_t *config; 1650 int error; 1651 1652 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1653 return (error); 1654 1655 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 1656 zc->zc_iflags, &config)) == 0) { 1657 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing); 1658 nvlist_free(config); 1659 } 1660 1661 spa_close(spa, FTAG); 1662 return (error); 1663} 1664 1665static int 1666zfs_ioc_vdev_detach(zfs_cmd_t *zc) 1667{ 1668 spa_t *spa; 1669 int error; 1670 1671 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1672 return (error); 1673 1674 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE); 1675 1676 spa_close(spa, FTAG); 1677 return (error); 1678} 1679 1680static int 1681zfs_ioc_vdev_split(zfs_cmd_t *zc) 1682{ 1683 spa_t *spa; 1684 nvlist_t *config, *props = NULL; 1685 int error; 1686 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT); 1687 1688 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1689 return (error); 1690 1691 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 1692 zc->zc_iflags, &config)) { 1693 spa_close(spa, FTAG); 1694 return (error); 1695 } 1696 1697 if (zc->zc_nvlist_src_size != 0 && (error = 1698 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 1699 zc->zc_iflags, &props))) { 1700 spa_close(spa, FTAG); 1701 nvlist_free(config); 1702 return (error); 1703 } 1704 1705 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp); 1706 1707 spa_close(spa, FTAG); 1708 1709 nvlist_free(config); 1710 nvlist_free(props); 1711 1712 return (error); 1713} 1714 1715static int 1716zfs_ioc_vdev_setpath(zfs_cmd_t *zc) 1717{ 1718 spa_t *spa; 1719 char *path = zc->zc_value; 1720 uint64_t guid = zc->zc_guid; 1721 int error; 1722 1723 error = spa_open(zc->zc_name, &spa, FTAG); 1724 if (error != 0) 1725 return (error); 1726 1727 error = spa_vdev_setpath(spa, guid, path); 1728 spa_close(spa, FTAG); 1729 return (error); 1730} 1731 1732static int 1733zfs_ioc_vdev_setfru(zfs_cmd_t *zc) 1734{ 1735 spa_t *spa; 1736 char *fru = zc->zc_value; 1737 uint64_t guid = zc->zc_guid; 1738 int error; 1739 1740 error = spa_open(zc->zc_name, &spa, FTAG); 1741 if (error != 0) 1742 return (error); 1743 1744 error = spa_vdev_setfru(spa, guid, fru); 1745 spa_close(spa, FTAG); 1746 return (error); 1747} 1748 1749static int 1750zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os) 1751{ 1752 int error = 0; 1753 nvlist_t *nv; 1754 1755 dmu_objset_fast_stat(os, &zc->zc_objset_stats); 1756 1757 if (zc->zc_nvlist_dst != 0 && 1758 (error = dsl_prop_get_all(os, &nv)) == 0) { 1759 dmu_objset_stats(os, nv); 1760 /* 1761 * NB: zvol_get_stats() will read the objset contents, 1762 * which we aren't supposed to do with a 1763 * DS_MODE_USER hold, because it could be 1764 * inconsistent. So this is a bit of a workaround... 1765 * XXX reading with out owning 1766 */ 1767 if (!zc->zc_objset_stats.dds_inconsistent) { 1768 if (dmu_objset_type(os) == DMU_OST_ZVOL) 1769 VERIFY(zvol_get_stats(os, nv) == 0); 1770 } 1771 error = put_nvlist(zc, nv); 1772 nvlist_free(nv); 1773 } 1774 1775 return (error); 1776} 1777 1778/* 1779 * inputs: 1780 * zc_name name of filesystem 1781 * zc_nvlist_dst_size size of buffer for property nvlist 1782 * 1783 * outputs: 1784 * zc_objset_stats stats 1785 * zc_nvlist_dst property nvlist 1786 * zc_nvlist_dst_size size of property nvlist 1787 */ 1788static int 1789zfs_ioc_objset_stats(zfs_cmd_t *zc) 1790{ 1791 objset_t *os = NULL; 1792 int error; 1793 1794 if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) 1795 return (error); 1796 1797 error = zfs_ioc_objset_stats_impl(zc, os); 1798 1799 dmu_objset_rele(os, FTAG); 1800 1801 if (error == ENOMEM) 1802 error = 0; 1803 return (error); 1804} 1805 1806/* 1807 * inputs: 1808 * zc_name name of filesystem 1809 * zc_nvlist_dst_size size of buffer for property nvlist 1810 * 1811 * outputs: 1812 * zc_nvlist_dst received property nvlist 1813 * zc_nvlist_dst_size size of received property nvlist 1814 * 1815 * Gets received properties (distinct from local properties on or after 1816 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from 1817 * local property values. 1818 */ 1819static int 1820zfs_ioc_objset_recvd_props(zfs_cmd_t *zc) 1821{ 1822 objset_t *os = NULL; 1823 int error; 1824 nvlist_t *nv; 1825 1826 if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) 1827 return (error); 1828 1829 /* 1830 * Without this check, we would return local property values if the 1831 * caller has not already received properties on or after 1832 * SPA_VERSION_RECVD_PROPS. 1833 */ 1834 if (!dsl_prop_get_hasrecvd(os)) { 1835 dmu_objset_rele(os, FTAG); 1836 return (ENOTSUP); 1837 } 1838 1839 if (zc->zc_nvlist_dst != 0 && 1840 (error = dsl_prop_get_received(os, &nv)) == 0) { 1841 error = put_nvlist(zc, nv); 1842 nvlist_free(nv); 1843 } 1844 1845 dmu_objset_rele(os, FTAG); 1846 return (error); 1847} 1848 1849static int 1850nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop) 1851{ 1852 uint64_t value; 1853 int error; 1854 1855 /* 1856 * zfs_get_zplprop() will either find a value or give us 1857 * the default value (if there is one). 1858 */ 1859 if ((error = zfs_get_zplprop(os, prop, &value)) != 0) 1860 return (error); 1861 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0); 1862 return (0); 1863} 1864 1865/* 1866 * inputs: 1867 * zc_name name of filesystem 1868 * zc_nvlist_dst_size size of buffer for zpl property nvlist 1869 * 1870 * outputs: 1871 * zc_nvlist_dst zpl property nvlist 1872 * zc_nvlist_dst_size size of zpl property nvlist 1873 */ 1874static int 1875zfs_ioc_objset_zplprops(zfs_cmd_t *zc) 1876{ 1877 objset_t *os; 1878 int err; 1879 1880 /* XXX reading without owning */ 1881 if (err = dmu_objset_hold(zc->zc_name, FTAG, &os)) 1882 return (err); 1883 1884 dmu_objset_fast_stat(os, &zc->zc_objset_stats); 1885 1886 /* 1887 * NB: nvl_add_zplprop() will read the objset contents, 1888 * which we aren't supposed to do with a DS_MODE_USER 1889 * hold, because it could be inconsistent. 1890 */ 1891 if (zc->zc_nvlist_dst != 0 && 1892 !zc->zc_objset_stats.dds_inconsistent && 1893 dmu_objset_type(os) == DMU_OST_ZFS) { 1894 nvlist_t *nv; 1895 1896 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1897 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 && 1898 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 && 1899 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 && 1900 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0) 1901 err = put_nvlist(zc, nv); 1902 nvlist_free(nv); 1903 } else { 1904 err = ENOENT; 1905 } 1906 dmu_objset_rele(os, FTAG); 1907 return (err); 1908} 1909 1910boolean_t 1911dataset_name_hidden(const char *name) 1912{ 1913 /* 1914 * Skip over datasets that are not visible in this zone, 1915 * internal datasets (which have a $ in their name), and 1916 * temporary datasets (which have a % in their name). 1917 */ 1918 if (strchr(name, '$') != NULL) 1919 return (B_TRUE); 1920 if (strchr(name, '%') != NULL) 1921 return (B_TRUE); 1922 if (!INGLOBALZONE(curthread) && !zone_dataset_visible(name, NULL)) 1923 return (B_TRUE); 1924 return (B_FALSE); 1925} 1926 1927/* 1928 * inputs: 1929 * zc_name name of filesystem 1930 * zc_cookie zap cursor 1931 * zc_nvlist_dst_size size of buffer for property nvlist 1932 * 1933 * outputs: 1934 * zc_name name of next filesystem 1935 * zc_cookie zap cursor 1936 * zc_objset_stats stats 1937 * zc_nvlist_dst property nvlist 1938 * zc_nvlist_dst_size size of property nvlist 1939 */ 1940static int 1941zfs_ioc_dataset_list_next(zfs_cmd_t *zc) 1942{ 1943 objset_t *os; 1944 int error; 1945 char *p; 1946 size_t orig_len = strlen(zc->zc_name); 1947 1948top: 1949 if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) { 1950 if (error == ENOENT) 1951 error = ESRCH; 1952 return (error); 1953 } 1954 1955 p = strrchr(zc->zc_name, '/'); 1956 if (p == NULL || p[1] != '\0') 1957 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name)); 1958 p = zc->zc_name + strlen(zc->zc_name); 1959 1960 /* 1961 * Pre-fetch the datasets. dmu_objset_prefetch() always returns 0 1962 * but is not declared void because its called by dmu_objset_find(). 1963 */ 1964 if (zc->zc_cookie == 0) { 1965 uint64_t cookie = 0; 1966 int len = sizeof (zc->zc_name) - (p - zc->zc_name); 1967 1968 while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0) { 1969 if (!dataset_name_hidden(zc->zc_name)) 1970 (void) dmu_objset_prefetch(zc->zc_name, NULL); 1971 } 1972 } 1973 1974 do { 1975 error = dmu_dir_list_next(os, 1976 sizeof (zc->zc_name) - (p - zc->zc_name), p, 1977 NULL, &zc->zc_cookie); 1978 if (error == ENOENT) 1979 error = ESRCH; 1980 } while (error == 0 && dataset_name_hidden(zc->zc_name) && 1981 !(zc->zc_iflags & FKIOCTL)); 1982 dmu_objset_rele(os, FTAG); 1983 1984 /* 1985 * If it's an internal dataset (ie. with a '$' in its name), 1986 * don't try to get stats for it, otherwise we'll return ENOENT. 1987 */ 1988 if (error == 0 && strchr(zc->zc_name, '$') == NULL) { 1989 error = zfs_ioc_objset_stats(zc); /* fill in the stats */ 1990 if (error == ENOENT) { 1991 /* We lost a race with destroy, get the next one. */ 1992 zc->zc_name[orig_len] = '\0'; 1993 goto top; 1994 } 1995 } 1996 return (error); 1997} 1998 1999/* 2000 * inputs: 2001 * zc_name name of filesystem 2002 * zc_cookie zap cursor 2003 * zc_nvlist_dst_size size of buffer for property nvlist 2004 * 2005 * outputs: 2006 * zc_name name of next snapshot 2007 * zc_objset_stats stats 2008 * zc_nvlist_dst property nvlist 2009 * zc_nvlist_dst_size size of property nvlist 2010 */ 2011static int 2012zfs_ioc_snapshot_list_next(zfs_cmd_t *zc) 2013{ 2014 objset_t *os; 2015 int error; 2016 2017top: 2018 if (zc->zc_cookie == 0) 2019 (void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch, 2020 NULL, DS_FIND_SNAPSHOTS); 2021 2022 error = dmu_objset_hold(zc->zc_name, FTAG, &os); 2023 if (error) 2024 return (error == ENOENT ? ESRCH : error); 2025 2026 /* 2027 * A dataset name of maximum length cannot have any snapshots, 2028 * so exit immediately. 2029 */ 2030 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) { 2031 dmu_objset_rele(os, FTAG); 2032 return (ESRCH); 2033 } 2034 2035 error = dmu_snapshot_list_next(os, 2036 sizeof (zc->zc_name) - strlen(zc->zc_name), 2037 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie, 2038 NULL); 2039 2040 if (error == 0) { 2041 dsl_dataset_t *ds; 2042 dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool; 2043 2044 /* 2045 * Since we probably don't have a hold on this snapshot, 2046 * it's possible that the objsetid could have been destroyed 2047 * and reused for a new objset. It's OK if this happens during 2048 * a zfs send operation, since the new createtxg will be 2049 * beyond the range we're interested in. 2050 */ 2051 rw_enter(&dp->dp_config_rwlock, RW_READER); 2052 error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds); 2053 rw_exit(&dp->dp_config_rwlock); 2054 if (error) { 2055 if (error == ENOENT) { 2056 /* Racing with destroy, get the next one. */ 2057 *strchr(zc->zc_name, '@') = '\0'; 2058 dmu_objset_rele(os, FTAG); 2059 goto top; 2060 } 2061 } else { 2062 objset_t *ossnap; 2063 2064 error = dmu_objset_from_ds(ds, &ossnap); 2065 if (error == 0) 2066 error = zfs_ioc_objset_stats_impl(zc, ossnap); 2067 dsl_dataset_rele(ds, FTAG); 2068 } 2069 } else if (error == ENOENT) { 2070 error = ESRCH; 2071 } 2072 2073 dmu_objset_rele(os, FTAG); 2074 /* if we failed, undo the @ that we tacked on to zc_name */ 2075 if (error) 2076 *strchr(zc->zc_name, '@') = '\0'; 2077 return (error); 2078} 2079 2080static int 2081zfs_prop_set_userquota(const char *dsname, nvpair_t *pair) 2082{ 2083 const char *propname = nvpair_name(pair); 2084 uint64_t *valary; 2085 unsigned int vallen; 2086 const char *domain; 2087 char *dash; 2088 zfs_userquota_prop_t type; 2089 uint64_t rid; 2090 uint64_t quota; 2091 zfsvfs_t *zfsvfs; 2092 int err; 2093 2094 if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 2095 nvlist_t *attrs; 2096 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 2097 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 2098 &pair) != 0) 2099 return (EINVAL); 2100 } 2101 2102 /* 2103 * A correctly constructed propname is encoded as 2104 * userquota@<rid>-<domain>. 2105 */ 2106 if ((dash = strchr(propname, '-')) == NULL || 2107 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 || 2108 vallen != 3) 2109 return (EINVAL); 2110 2111 domain = dash + 1; 2112 type = valary[0]; 2113 rid = valary[1]; 2114 quota = valary[2]; 2115 2116 err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE); 2117 if (err == 0) { 2118 err = zfs_set_userquota(zfsvfs, type, domain, rid, quota); 2119 zfsvfs_rele(zfsvfs, FTAG); 2120 } 2121 2122 return (err); 2123} 2124 2125/* 2126 * If the named property is one that has a special function to set its value, 2127 * return 0 on success and a positive error code on failure; otherwise if it is 2128 * not one of the special properties handled by this function, return -1. 2129 * 2130 * XXX: It would be better for callers of the property interface if we handled 2131 * these special cases in dsl_prop.c (in the dsl layer). 2132 */ 2133static int 2134zfs_prop_set_special(const char *dsname, zprop_source_t source, 2135 nvpair_t *pair) 2136{ 2137 const char *propname = nvpair_name(pair); 2138 zfs_prop_t prop = zfs_name_to_prop(propname); 2139 uint64_t intval; 2140 int err; 2141 2142 if (prop == ZPROP_INVAL) { 2143 if (zfs_prop_userquota(propname)) 2144 return (zfs_prop_set_userquota(dsname, pair)); 2145 return (-1); 2146 } 2147 2148 if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 2149 nvlist_t *attrs; 2150 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 2151 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 2152 &pair) == 0); 2153 } 2154 2155 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) 2156 return (-1); 2157 2158 VERIFY(0 == nvpair_value_uint64(pair, &intval)); 2159 2160 switch (prop) { 2161 case ZFS_PROP_QUOTA: 2162 err = dsl_dir_set_quota(dsname, source, intval); 2163 break; 2164 case ZFS_PROP_REFQUOTA: 2165 err = dsl_dataset_set_quota(dsname, source, intval); 2166 break; 2167 case ZFS_PROP_RESERVATION: 2168 err = dsl_dir_set_reservation(dsname, source, intval); 2169 break; 2170 case ZFS_PROP_REFRESERVATION: 2171 err = dsl_dataset_set_reservation(dsname, source, intval); 2172 break; 2173 case ZFS_PROP_VOLSIZE: 2174 err = zvol_set_volsize(dsname, ddi_driver_major(zfs_dip), 2175 intval); 2176 break; 2177 case ZFS_PROP_VERSION: 2178 { 2179 zfsvfs_t *zfsvfs; 2180 2181 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0) 2182 break; 2183 2184 err = zfs_set_version(zfsvfs, intval); 2185 zfsvfs_rele(zfsvfs, FTAG); 2186 2187 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) { 2188 zfs_cmd_t *zc; 2189 2190 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP); 2191 (void) strcpy(zc->zc_name, dsname); 2192 (void) zfs_ioc_userspace_upgrade(zc); 2193 kmem_free(zc, sizeof (zfs_cmd_t)); 2194 } 2195 break; 2196 } 2197 2198 default: 2199 err = -1; 2200 } 2201 2202 return (err); 2203} 2204 2205/* 2206 * This function is best effort. If it fails to set any of the given properties, 2207 * it continues to set as many as it can and returns the first error 2208 * encountered. If the caller provides a non-NULL errlist, it also gives the 2209 * complete list of names of all the properties it failed to set along with the 2210 * corresponding error numbers. The caller is responsible for freeing the 2211 * returned errlist. 2212 * 2213 * If every property is set successfully, zero is returned and the list pointed 2214 * at by errlist is NULL. 2215 */ 2216int 2217zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl, 2218 nvlist_t **errlist) 2219{ 2220 nvpair_t *pair; 2221 nvpair_t *propval; 2222 int rv = 0; 2223 uint64_t intval; 2224 char *strval; 2225 nvlist_t *genericnvl; 2226 nvlist_t *errors; 2227 nvlist_t *retrynvl; 2228 2229 VERIFY(nvlist_alloc(&genericnvl, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2230 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2231 VERIFY(nvlist_alloc(&retrynvl, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2232 2233retry: 2234 pair = NULL; 2235 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) { 2236 const char *propname = nvpair_name(pair); 2237 zfs_prop_t prop = zfs_name_to_prop(propname); 2238 int err = 0; 2239 2240 /* decode the property value */ 2241 propval = pair; 2242 if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 2243 nvlist_t *attrs; 2244 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 2245 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 2246 &propval) != 0) 2247 err = EINVAL; 2248 } 2249 2250 /* Validate value type */ 2251 if (err == 0 && prop == ZPROP_INVAL) { 2252 if (zfs_prop_user(propname)) { 2253 if (nvpair_type(propval) != DATA_TYPE_STRING) 2254 err = EINVAL; 2255 } else if (zfs_prop_userquota(propname)) { 2256 if (nvpair_type(propval) != 2257 DATA_TYPE_UINT64_ARRAY) 2258 err = EINVAL; 2259 } 2260 } else if (err == 0) { 2261 if (nvpair_type(propval) == DATA_TYPE_STRING) { 2262 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING) 2263 err = EINVAL; 2264 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) { 2265 const char *unused; 2266 2267 VERIFY(nvpair_value_uint64(propval, 2268 &intval) == 0); 2269 2270 switch (zfs_prop_get_type(prop)) { 2271 case PROP_TYPE_NUMBER: 2272 break; 2273 case PROP_TYPE_STRING: 2274 err = EINVAL; 2275 break; 2276 case PROP_TYPE_INDEX: 2277 if (zfs_prop_index_to_string(prop, 2278 intval, &unused) != 0) 2279 err = EINVAL; 2280 break; 2281 default: 2282 cmn_err(CE_PANIC, 2283 "unknown property type"); 2284 } 2285 } else { 2286 err = EINVAL; 2287 } 2288 } 2289 2290 /* Validate permissions */ 2291 if (err == 0) 2292 err = zfs_check_settable(dsname, pair, CRED()); 2293 2294 if (err == 0) { 2295 err = zfs_prop_set_special(dsname, source, pair); 2296 if (err == -1) { 2297 /* 2298 * For better performance we build up a list of 2299 * properties to set in a single transaction. 2300 */ 2301 err = nvlist_add_nvpair(genericnvl, pair); 2302 } else if (err != 0 && nvl != retrynvl) { 2303 /* 2304 * This may be a spurious error caused by 2305 * receiving quota and reservation out of order. 2306 * Try again in a second pass. 2307 */ 2308 err = nvlist_add_nvpair(retrynvl, pair); 2309 } 2310 } 2311 2312 if (err != 0) 2313 VERIFY(nvlist_add_int32(errors, propname, err) == 0); 2314 } 2315 2316 if (nvl != retrynvl && !nvlist_empty(retrynvl)) { 2317 nvl = retrynvl; 2318 goto retry; 2319 } 2320 2321 if (!nvlist_empty(genericnvl) && 2322 dsl_props_set(dsname, source, genericnvl) != 0) { 2323 /* 2324 * If this fails, we still want to set as many properties as we 2325 * can, so try setting them individually. 2326 */ 2327 pair = NULL; 2328 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) { 2329 const char *propname = nvpair_name(pair); 2330 int err = 0; 2331 2332 propval = pair; 2333 if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 2334 nvlist_t *attrs; 2335 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 2336 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 2337 &propval) == 0); 2338 } 2339 2340 if (nvpair_type(propval) == DATA_TYPE_STRING) { 2341 VERIFY(nvpair_value_string(propval, 2342 &strval) == 0); 2343 err = dsl_prop_set(dsname, propname, source, 1, 2344 strlen(strval) + 1, strval); 2345 } else { 2346 VERIFY(nvpair_value_uint64(propval, 2347 &intval) == 0); 2348 err = dsl_prop_set(dsname, propname, source, 8, 2349 1, &intval); 2350 } 2351 2352 if (err != 0) { 2353 VERIFY(nvlist_add_int32(errors, propname, 2354 err) == 0); 2355 } 2356 } 2357 } 2358 nvlist_free(genericnvl); 2359 nvlist_free(retrynvl); 2360 2361 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) { 2362 nvlist_free(errors); 2363 errors = NULL; 2364 } else { 2365 VERIFY(nvpair_value_int32(pair, &rv) == 0); 2366 } 2367 2368 if (errlist == NULL) 2369 nvlist_free(errors); 2370 else 2371 *errlist = errors; 2372 2373 return (rv); 2374} 2375 2376/* 2377 * Check that all the properties are valid user properties. 2378 */ 2379static int 2380zfs_check_userprops(char *fsname, nvlist_t *nvl) 2381{ 2382 nvpair_t *pair = NULL; 2383 int error = 0; 2384 2385 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) { 2386 const char *propname = nvpair_name(pair); 2387 char *valstr; 2388 2389 if (!zfs_prop_user(propname) || 2390 nvpair_type(pair) != DATA_TYPE_STRING) 2391 return (EINVAL); 2392 2393 if (error = zfs_secpolicy_write_perms(fsname, 2394 ZFS_DELEG_PERM_USERPROP, CRED())) 2395 return (error); 2396 2397 if (strlen(propname) >= ZAP_MAXNAMELEN) 2398 return (ENAMETOOLONG); 2399 2400 VERIFY(nvpair_value_string(pair, &valstr) == 0); 2401 if (strlen(valstr) >= ZAP_MAXVALUELEN) 2402 return (E2BIG); 2403 } 2404 return (0); 2405} 2406 2407static void 2408props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops) 2409{ 2410 nvpair_t *pair; 2411 2412 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2413 2414 pair = NULL; 2415 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) { 2416 if (nvlist_exists(skipped, nvpair_name(pair))) 2417 continue; 2418 2419 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0); 2420 } 2421} 2422 2423static int 2424clear_received_props(objset_t *os, const char *fs, nvlist_t *props, 2425 nvlist_t *skipped) 2426{ 2427 int err = 0; 2428 nvlist_t *cleared_props = NULL; 2429 props_skip(props, skipped, &cleared_props); 2430 if (!nvlist_empty(cleared_props)) { 2431 /* 2432 * Acts on local properties until the dataset has received 2433 * properties at least once on or after SPA_VERSION_RECVD_PROPS. 2434 */ 2435 zprop_source_t flags = (ZPROP_SRC_NONE | 2436 (dsl_prop_get_hasrecvd(os) ? ZPROP_SRC_RECEIVED : 0)); 2437 err = zfs_set_prop_nvlist(fs, flags, cleared_props, NULL); 2438 } 2439 nvlist_free(cleared_props); 2440 return (err); 2441} 2442 2443/* 2444 * inputs: 2445 * zc_name name of filesystem 2446 * zc_value name of property to set 2447 * zc_nvlist_src{_size} nvlist of properties to apply 2448 * zc_cookie received properties flag 2449 * 2450 * outputs: 2451 * zc_nvlist_dst{_size} error for each unapplied received property 2452 */ 2453static int 2454zfs_ioc_set_prop(zfs_cmd_t *zc) 2455{ 2456 nvlist_t *nvl; 2457 boolean_t received = zc->zc_cookie; 2458 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED : 2459 ZPROP_SRC_LOCAL); 2460 nvlist_t *errors = NULL; 2461 int error; 2462 2463 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 2464 zc->zc_iflags, &nvl)) != 0) 2465 return (error); 2466 2467 if (received) { 2468 nvlist_t *origprops; 2469 objset_t *os; 2470 2471 if (dmu_objset_hold(zc->zc_name, FTAG, &os) == 0) { 2472 if (dsl_prop_get_received(os, &origprops) == 0) { 2473 (void) clear_received_props(os, 2474 zc->zc_name, origprops, nvl); 2475 nvlist_free(origprops); 2476 } 2477 2478 dsl_prop_set_hasrecvd(os); 2479 dmu_objset_rele(os, FTAG); 2480 } 2481 } 2482 2483 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, &errors); 2484 2485 if (zc->zc_nvlist_dst != 0 && errors != NULL) { 2486 (void) put_nvlist(zc, errors); 2487 } 2488 2489 nvlist_free(errors); 2490 nvlist_free(nvl); 2491 return (error); 2492} 2493 2494/* 2495 * inputs: 2496 * zc_name name of filesystem 2497 * zc_value name of property to inherit 2498 * zc_cookie revert to received value if TRUE 2499 * 2500 * outputs: none 2501 */ 2502static int 2503zfs_ioc_inherit_prop(zfs_cmd_t *zc) 2504{ 2505 const char *propname = zc->zc_value; 2506 zfs_prop_t prop = zfs_name_to_prop(propname); 2507 boolean_t received = zc->zc_cookie; 2508 zprop_source_t source = (received 2509 ? ZPROP_SRC_NONE /* revert to received value, if any */ 2510 : ZPROP_SRC_INHERITED); /* explicitly inherit */ 2511 2512 if (received) { 2513 nvlist_t *dummy; 2514 nvpair_t *pair; 2515 zprop_type_t type; 2516 int err; 2517 2518 /* 2519 * zfs_prop_set_special() expects properties in the form of an 2520 * nvpair with type info. 2521 */ 2522 if (prop == ZPROP_INVAL) { 2523 if (!zfs_prop_user(propname)) 2524 return (EINVAL); 2525 2526 type = PROP_TYPE_STRING; 2527 } else if (prop == ZFS_PROP_VOLSIZE || 2528 prop == ZFS_PROP_VERSION) { 2529 return (EINVAL); 2530 } else { 2531 type = zfs_prop_get_type(prop); 2532 } 2533 2534 VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2535 2536 switch (type) { 2537 case PROP_TYPE_STRING: 2538 VERIFY(0 == nvlist_add_string(dummy, propname, "")); 2539 break; 2540 case PROP_TYPE_NUMBER: 2541 case PROP_TYPE_INDEX: 2542 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0)); 2543 break; 2544 default: 2545 nvlist_free(dummy); 2546 return (EINVAL); 2547 } 2548 2549 pair = nvlist_next_nvpair(dummy, NULL); 2550 err = zfs_prop_set_special(zc->zc_name, source, pair); 2551 nvlist_free(dummy); 2552 if (err != -1) 2553 return (err); /* special property already handled */ 2554 } else { 2555 /* 2556 * Only check this in the non-received case. We want to allow 2557 * 'inherit -S' to revert non-inheritable properties like quota 2558 * and reservation to the received or default values even though 2559 * they are not considered inheritable. 2560 */ 2561 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop)) 2562 return (EINVAL); 2563 } 2564 2565 /* the property name has been validated by zfs_secpolicy_inherit() */ 2566 return (dsl_prop_set(zc->zc_name, zc->zc_value, source, 0, 0, NULL)); 2567} 2568 2569static int 2570zfs_ioc_pool_set_props(zfs_cmd_t *zc) 2571{ 2572 nvlist_t *props; 2573 spa_t *spa; 2574 int error; 2575 nvpair_t *pair; 2576 2577 if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 2578 zc->zc_iflags, &props)) 2579 return (error); 2580 2581 /* 2582 * If the only property is the configfile, then just do a spa_lookup() 2583 * to handle the faulted case. 2584 */ 2585 pair = nvlist_next_nvpair(props, NULL); 2586 if (pair != NULL && strcmp(nvpair_name(pair), 2587 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 && 2588 nvlist_next_nvpair(props, pair) == NULL) { 2589 mutex_enter(&spa_namespace_lock); 2590 if ((spa = spa_lookup(zc->zc_name)) != NULL) { 2591 spa_configfile_set(spa, props, B_FALSE); 2592 spa_config_sync(spa, B_FALSE, B_TRUE); 2593 } 2594 mutex_exit(&spa_namespace_lock); 2595 if (spa != NULL) { 2596 nvlist_free(props); 2597 return (0); 2598 } 2599 } 2600 2601 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) { 2602 nvlist_free(props); 2603 return (error); 2604 } 2605 2606 error = spa_prop_set(spa, props); 2607 2608 nvlist_free(props); 2609 spa_close(spa, FTAG); 2610 2611 return (error); 2612} 2613 2614static int 2615zfs_ioc_pool_get_props(zfs_cmd_t *zc) 2616{ 2617 spa_t *spa; 2618 int error; 2619 nvlist_t *nvp = NULL; 2620 2621 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) { 2622 /* 2623 * If the pool is faulted, there may be properties we can still 2624 * get (such as altroot and cachefile), so attempt to get them 2625 * anyway. 2626 */ 2627 mutex_enter(&spa_namespace_lock); 2628 if ((spa = spa_lookup(zc->zc_name)) != NULL) 2629 error = spa_prop_get(spa, &nvp); 2630 mutex_exit(&spa_namespace_lock); 2631 } else { 2632 error = spa_prop_get(spa, &nvp); 2633 spa_close(spa, FTAG); 2634 } 2635 2636 if (error == 0 && zc->zc_nvlist_dst != 0) 2637 error = put_nvlist(zc, nvp); 2638 else 2639 error = EFAULT; 2640 2641 nvlist_free(nvp); 2642 return (error); 2643} 2644 2645/* 2646 * inputs: 2647 * zc_name name of filesystem 2648 * zc_nvlist_src{_size} nvlist of delegated permissions 2649 * zc_perm_action allow/unallow flag 2650 * 2651 * outputs: none 2652 */ 2653static int 2654zfs_ioc_set_fsacl(zfs_cmd_t *zc) 2655{ 2656 int error; 2657 nvlist_t *fsaclnv = NULL; 2658 2659 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 2660 zc->zc_iflags, &fsaclnv)) != 0) 2661 return (error); 2662 2663 /* 2664 * Verify nvlist is constructed correctly 2665 */ 2666 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) { 2667 nvlist_free(fsaclnv); 2668 return (EINVAL); 2669 } 2670 2671 /* 2672 * If we don't have PRIV_SYS_MOUNT, then validate 2673 * that user is allowed to hand out each permission in 2674 * the nvlist(s) 2675 */ 2676 2677 error = secpolicy_zfs(CRED()); 2678 if (error) { 2679 if (zc->zc_perm_action == B_FALSE) { 2680 error = dsl_deleg_can_allow(zc->zc_name, 2681 fsaclnv, CRED()); 2682 } else { 2683 error = dsl_deleg_can_unallow(zc->zc_name, 2684 fsaclnv, CRED()); 2685 } 2686 } 2687 2688 if (error == 0) 2689 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action); 2690 2691 nvlist_free(fsaclnv); 2692 return (error); 2693} 2694 2695/* 2696 * inputs: 2697 * zc_name name of filesystem 2698 * 2699 * outputs: 2700 * zc_nvlist_src{_size} nvlist of delegated permissions 2701 */ 2702static int 2703zfs_ioc_get_fsacl(zfs_cmd_t *zc) 2704{ 2705 nvlist_t *nvp; 2706 int error; 2707 2708 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) { 2709 error = put_nvlist(zc, nvp); 2710 nvlist_free(nvp); 2711 } 2712 2713 return (error); 2714} 2715 2716/* 2717 * Search the vfs list for a specified resource. Returns a pointer to it 2718 * or NULL if no suitable entry is found. The caller of this routine 2719 * is responsible for releasing the returned vfs pointer. 2720 */ 2721static vfs_t * 2722zfs_get_vfs(const char *resource) 2723{ 2724 vfs_t *vfsp; 2725 2726 mtx_lock(&mountlist_mtx); 2727 TAILQ_FOREACH(vfsp, &mountlist, mnt_list) { 2728 if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) { 2729 VFS_HOLD(vfsp); 2730 break; 2731 } 2732 } 2733 mtx_unlock(&mountlist_mtx); 2734 return (vfsp); 2735} 2736 2737/* ARGSUSED */ 2738static void 2739zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 2740{ 2741 zfs_creat_t *zct = arg; 2742 2743 zfs_create_fs(os, cr, zct->zct_zplprops, tx); 2744} 2745 2746#define ZFS_PROP_UNDEFINED ((uint64_t)-1) 2747 2748/* 2749 * inputs: 2750 * createprops list of properties requested by creator 2751 * default_zplver zpl version to use if unspecified in createprops 2752 * fuids_ok fuids allowed in this version of the spa? 2753 * os parent objset pointer (NULL if root fs) 2754 * 2755 * outputs: 2756 * zplprops values for the zplprops we attach to the master node object 2757 * is_ci true if requested file system will be purely case-insensitive 2758 * 2759 * Determine the settings for utf8only, normalization and 2760 * casesensitivity. Specific values may have been requested by the 2761 * creator and/or we can inherit values from the parent dataset. If 2762 * the file system is of too early a vintage, a creator can not 2763 * request settings for these properties, even if the requested 2764 * setting is the default value. We don't actually want to create dsl 2765 * properties for these, so remove them from the source nvlist after 2766 * processing. 2767 */ 2768static int 2769zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver, 2770 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops, 2771 nvlist_t *zplprops, boolean_t *is_ci) 2772{ 2773 uint64_t sense = ZFS_PROP_UNDEFINED; 2774 uint64_t norm = ZFS_PROP_UNDEFINED; 2775 uint64_t u8 = ZFS_PROP_UNDEFINED; 2776 2777 ASSERT(zplprops != NULL); 2778 2779 /* 2780 * Pull out creator prop choices, if any. 2781 */ 2782 if (createprops) { 2783 (void) nvlist_lookup_uint64(createprops, 2784 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver); 2785 (void) nvlist_lookup_uint64(createprops, 2786 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm); 2787 (void) nvlist_remove_all(createprops, 2788 zfs_prop_to_name(ZFS_PROP_NORMALIZE)); 2789 (void) nvlist_lookup_uint64(createprops, 2790 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8); 2791 (void) nvlist_remove_all(createprops, 2792 zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 2793 (void) nvlist_lookup_uint64(createprops, 2794 zfs_prop_to_name(ZFS_PROP_CASE), &sense); 2795 (void) nvlist_remove_all(createprops, 2796 zfs_prop_to_name(ZFS_PROP_CASE)); 2797 } 2798 2799 /* 2800 * If the zpl version requested is whacky or the file system 2801 * or pool is version is too "young" to support normalization 2802 * and the creator tried to set a value for one of the props, 2803 * error out. 2804 */ 2805 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) || 2806 (zplver >= ZPL_VERSION_FUID && !fuids_ok) || 2807 (zplver >= ZPL_VERSION_SA && !sa_ok) || 2808 (zplver < ZPL_VERSION_NORMALIZATION && 2809 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED || 2810 sense != ZFS_PROP_UNDEFINED))) 2811 return (ENOTSUP); 2812 2813 /* 2814 * Put the version in the zplprops 2815 */ 2816 VERIFY(nvlist_add_uint64(zplprops, 2817 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0); 2818 2819 if (norm == ZFS_PROP_UNDEFINED) 2820 VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0); 2821 VERIFY(nvlist_add_uint64(zplprops, 2822 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0); 2823 2824 /* 2825 * If we're normalizing, names must always be valid UTF-8 strings. 2826 */ 2827 if (norm) 2828 u8 = 1; 2829 if (u8 == ZFS_PROP_UNDEFINED) 2830 VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0); 2831 VERIFY(nvlist_add_uint64(zplprops, 2832 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0); 2833 2834 if (sense == ZFS_PROP_UNDEFINED) 2835 VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0); 2836 VERIFY(nvlist_add_uint64(zplprops, 2837 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0); 2838 2839 if (is_ci) 2840 *is_ci = (sense == ZFS_CASE_INSENSITIVE); 2841 2842 return (0); 2843} 2844 2845static int 2846zfs_fill_zplprops(const char *dataset, nvlist_t *createprops, 2847 nvlist_t *zplprops, boolean_t *is_ci) 2848{ 2849 boolean_t fuids_ok, sa_ok; 2850 uint64_t zplver = ZPL_VERSION; 2851 objset_t *os = NULL; 2852 char parentname[MAXNAMELEN]; 2853 char *cp; 2854 spa_t *spa; 2855 uint64_t spa_vers; 2856 int error; 2857 2858 (void) strlcpy(parentname, dataset, sizeof (parentname)); 2859 cp = strrchr(parentname, '/'); 2860 ASSERT(cp != NULL); 2861 cp[0] = '\0'; 2862 2863 if ((error = spa_open(dataset, &spa, FTAG)) != 0) 2864 return (error); 2865 2866 spa_vers = spa_version(spa); 2867 spa_close(spa, FTAG); 2868 2869 zplver = zfs_zpl_version_map(spa_vers); 2870 fuids_ok = (zplver >= ZPL_VERSION_FUID); 2871 sa_ok = (zplver >= ZPL_VERSION_SA); 2872 2873 /* 2874 * Open parent object set so we can inherit zplprop values. 2875 */ 2876 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0) 2877 return (error); 2878 2879 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops, 2880 zplprops, is_ci); 2881 dmu_objset_rele(os, FTAG); 2882 return (error); 2883} 2884 2885static int 2886zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops, 2887 nvlist_t *zplprops, boolean_t *is_ci) 2888{ 2889 boolean_t fuids_ok; 2890 boolean_t sa_ok; 2891 uint64_t zplver = ZPL_VERSION; 2892 int error; 2893 2894 zplver = zfs_zpl_version_map(spa_vers); 2895 fuids_ok = (zplver >= ZPL_VERSION_FUID); 2896 sa_ok = (zplver >= ZPL_VERSION_SA); 2897 2898 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok, 2899 createprops, zplprops, is_ci); 2900 return (error); 2901} 2902 2903/* 2904 * inputs: 2905 * zc_objset_type type of objset to create (fs vs zvol) 2906 * zc_name name of new objset 2907 * zc_value name of snapshot to clone from (may be empty) 2908 * zc_nvlist_src{_size} nvlist of properties to apply 2909 * 2910 * outputs: none 2911 */ 2912static int 2913zfs_ioc_create(zfs_cmd_t *zc) 2914{ 2915 objset_t *clone; 2916 int error = 0; 2917 zfs_creat_t zct; 2918 nvlist_t *nvprops = NULL; 2919 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx); 2920 dmu_objset_type_t type = zc->zc_objset_type; 2921 2922 switch (type) { 2923 2924 case DMU_OST_ZFS: 2925 cbfunc = zfs_create_cb; 2926 break; 2927 2928 case DMU_OST_ZVOL: 2929 cbfunc = zvol_create_cb; 2930 break; 2931 2932 default: 2933 cbfunc = NULL; 2934 break; 2935 } 2936 if (strchr(zc->zc_name, '@') || 2937 strchr(zc->zc_name, '%')) 2938 return (EINVAL); 2939 2940 if (zc->zc_nvlist_src != 0 && 2941 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 2942 zc->zc_iflags, &nvprops)) != 0) 2943 return (error); 2944 2945 zct.zct_zplprops = NULL; 2946 zct.zct_props = nvprops; 2947 2948 if (zc->zc_value[0] != '\0') { 2949 /* 2950 * We're creating a clone of an existing snapshot. 2951 */ 2952 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0'; 2953 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) { 2954 nvlist_free(nvprops); 2955 return (EINVAL); 2956 } 2957 2958 error = dmu_objset_hold(zc->zc_value, FTAG, &clone); 2959 if (error) { 2960 nvlist_free(nvprops); 2961 return (error); 2962 } 2963 2964 error = dmu_objset_clone(zc->zc_name, dmu_objset_ds(clone), 0); 2965 dmu_objset_rele(clone, FTAG); 2966 if (error) { 2967 nvlist_free(nvprops); 2968 return (error); 2969 } 2970 } else { 2971 boolean_t is_insensitive = B_FALSE; 2972 2973 if (cbfunc == NULL) { 2974 nvlist_free(nvprops); 2975 return (EINVAL); 2976 } 2977 2978 if (type == DMU_OST_ZVOL) { 2979 uint64_t volsize, volblocksize; 2980 2981 if (nvprops == NULL || 2982 nvlist_lookup_uint64(nvprops, 2983 zfs_prop_to_name(ZFS_PROP_VOLSIZE), 2984 &volsize) != 0) { 2985 nvlist_free(nvprops); 2986 return (EINVAL); 2987 } 2988 2989 if ((error = nvlist_lookup_uint64(nvprops, 2990 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 2991 &volblocksize)) != 0 && error != ENOENT) { 2992 nvlist_free(nvprops); 2993 return (EINVAL); 2994 } 2995 2996 if (error != 0) 2997 volblocksize = zfs_prop_default_numeric( 2998 ZFS_PROP_VOLBLOCKSIZE); 2999 3000 if ((error = zvol_check_volblocksize( 3001 volblocksize)) != 0 || 3002 (error = zvol_check_volsize(volsize, 3003 volblocksize)) != 0) { 3004 nvlist_free(nvprops); 3005 return (error); 3006 } 3007 } else if (type == DMU_OST_ZFS) { 3008 int error; 3009 3010 /* 3011 * We have to have normalization and 3012 * case-folding flags correct when we do the 3013 * file system creation, so go figure them out 3014 * now. 3015 */ 3016 VERIFY(nvlist_alloc(&zct.zct_zplprops, 3017 NV_UNIQUE_NAME, KM_SLEEP) == 0); 3018 error = zfs_fill_zplprops(zc->zc_name, nvprops, 3019 zct.zct_zplprops, &is_insensitive); 3020 if (error != 0) { 3021 nvlist_free(nvprops); 3022 nvlist_free(zct.zct_zplprops); 3023 return (error); 3024 } 3025 } 3026 error = dmu_objset_create(zc->zc_name, type, 3027 is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct); 3028 nvlist_free(zct.zct_zplprops); 3029 } 3030 3031 /* 3032 * It would be nice to do this atomically. 3033 */ 3034 if (error == 0) { 3035 error = zfs_set_prop_nvlist(zc->zc_name, ZPROP_SRC_LOCAL, 3036 nvprops, NULL); 3037 if (error != 0) 3038 (void) dmu_objset_destroy(zc->zc_name, B_FALSE); 3039 } 3040 nvlist_free(nvprops); 3041#ifdef __FreeBSD__ 3042 if (error == 0 && type == DMU_OST_ZVOL) 3043 zvol_create_minors(zc->zc_name); 3044#endif 3045 return (error); 3046} 3047 3048/* 3049 * inputs: 3050 * zc_name name of filesystem 3051 * zc_value short name of snapshot 3052 * zc_cookie recursive flag 3053 * zc_nvlist_src[_size] property list 3054 * 3055 * outputs: 3056 * zc_value short snapname (i.e. part after the '@') 3057 */ 3058static int 3059zfs_ioc_snapshot(zfs_cmd_t *zc) 3060{ 3061 nvlist_t *nvprops = NULL; 3062 int error; 3063 boolean_t recursive = zc->zc_cookie; 3064 3065 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 3066 return (EINVAL); 3067 3068 if (zc->zc_nvlist_src != 0 && 3069 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 3070 zc->zc_iflags, &nvprops)) != 0) 3071 return (error); 3072 3073 error = zfs_check_userprops(zc->zc_name, nvprops); 3074 if (error) 3075 goto out; 3076 3077 if (!nvlist_empty(nvprops) && 3078 zfs_earlier_version(zc->zc_name, SPA_VERSION_SNAP_PROPS)) { 3079 error = ENOTSUP; 3080 goto out; 3081 } 3082 3083 error = dmu_objset_snapshot(zc->zc_name, zc->zc_value, NULL, 3084 nvprops, recursive, B_FALSE, -1); 3085 3086out: 3087 nvlist_free(nvprops); 3088 return (error); 3089} 3090 3091int 3092zfs_unmount_snap(const char *name, void *arg) 3093{ 3094 vfs_t *vfsp = NULL; 3095 3096 if (arg) { 3097 char *snapname = arg; 3098 char *fullname = kmem_asprintf("%s@%s", name, snapname); 3099 vfsp = zfs_get_vfs(fullname); 3100 strfree(fullname); 3101 } else if (strchr(name, '@')) { 3102 vfsp = zfs_get_vfs(name); 3103 } 3104 3105 if (vfsp) { 3106 /* 3107 * Always force the unmount for snapshots. 3108 */ 3109 int flag = MS_FORCE; 3110 int err; 3111 3112 if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) { 3113 VFS_RELE(vfsp); 3114 return (err); 3115 } 3116 VFS_RELE(vfsp); 3117 mtx_lock(&Giant); /* dounmount() */ 3118 dounmount(vfsp, flag, curthread); 3119 mtx_unlock(&Giant); /* dounmount() */ 3120 } 3121 return (0); 3122} 3123 3124/* 3125 * inputs: 3126 * zc_name name of filesystem 3127 * zc_value short name of snapshot 3128 * zc_defer_destroy mark for deferred destroy 3129 * 3130 * outputs: none 3131 */ 3132static int 3133zfs_ioc_destroy_snaps(zfs_cmd_t *zc) 3134{ 3135 int err; 3136 3137 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 3138 return (EINVAL); 3139 err = dmu_objset_find(zc->zc_name, 3140 zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN); 3141 if (err) 3142 return (err); 3143 return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value, 3144 zc->zc_defer_destroy)); 3145} 3146 3147/* 3148 * inputs: 3149 * zc_name name of dataset to destroy 3150 * zc_objset_type type of objset 3151 * zc_defer_destroy mark for deferred destroy 3152 * 3153 * outputs: none 3154 */ 3155static int 3156zfs_ioc_destroy(zfs_cmd_t *zc) 3157{ 3158 int err; 3159 if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) { 3160 err = zfs_unmount_snap(zc->zc_name, NULL); 3161 if (err) 3162 return (err); 3163 } 3164 3165 err = dmu_objset_destroy(zc->zc_name, zc->zc_defer_destroy); 3166 if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0) 3167 (void) zvol_remove_minor(zc->zc_name); 3168 return (err); 3169} 3170 3171/* 3172 * inputs: 3173 * zc_name name of dataset to rollback (to most recent snapshot) 3174 * 3175 * outputs: none 3176 */ 3177static int 3178zfs_ioc_rollback(zfs_cmd_t *zc) 3179{ 3180 dsl_dataset_t *ds, *clone; 3181 int error; 3182 zfsvfs_t *zfsvfs; 3183 char *clone_name; 3184 3185 error = dsl_dataset_hold(zc->zc_name, FTAG, &ds); 3186 if (error) 3187 return (error); 3188 3189 /* must not be a snapshot */ 3190 if (dsl_dataset_is_snapshot(ds)) { 3191 dsl_dataset_rele(ds, FTAG); 3192 return (EINVAL); 3193 } 3194 3195 /* must have a most recent snapshot */ 3196 if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) { 3197 dsl_dataset_rele(ds, FTAG); 3198 return (EINVAL); 3199 } 3200 3201 /* 3202 * Create clone of most recent snapshot. 3203 */ 3204 clone_name = kmem_asprintf("%s/%%rollback", zc->zc_name); 3205 error = dmu_objset_clone(clone_name, ds->ds_prev, DS_FLAG_INCONSISTENT); 3206 if (error) 3207 goto out; 3208 3209 error = dsl_dataset_own(clone_name, B_TRUE, FTAG, &clone); 3210 if (error) 3211 goto out; 3212 3213 /* 3214 * Do clone swap. 3215 */ 3216 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) { 3217 error = zfs_suspend_fs(zfsvfs); 3218 if (error == 0) { 3219 int resume_err; 3220 3221 if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) { 3222 error = dsl_dataset_clone_swap(clone, ds, 3223 B_TRUE); 3224 dsl_dataset_disown(ds, FTAG); 3225 ds = NULL; 3226 } else { 3227 error = EBUSY; 3228 } 3229 resume_err = zfs_resume_fs(zfsvfs, zc->zc_name); 3230 error = error ? error : resume_err; 3231 } 3232 VFS_RELE(zfsvfs->z_vfs); 3233 } else { 3234 if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) { 3235 error = dsl_dataset_clone_swap(clone, ds, B_TRUE); 3236 dsl_dataset_disown(ds, FTAG); 3237 ds = NULL; 3238 } else { 3239 error = EBUSY; 3240 } 3241 } 3242 3243 /* 3244 * Destroy clone (which also closes it). 3245 */ 3246 (void) dsl_dataset_destroy(clone, FTAG, B_FALSE); 3247 3248out: 3249 strfree(clone_name); 3250 if (ds) 3251 dsl_dataset_rele(ds, FTAG); 3252 return (error); 3253} 3254 3255/* 3256 * inputs: 3257 * zc_name old name of dataset 3258 * zc_value new name of dataset 3259 * zc_cookie recursive flag (only valid for snapshots) 3260 * 3261 * outputs: none 3262 */ 3263static int 3264zfs_ioc_rename(zfs_cmd_t *zc) 3265{ 3266 int flags = 0; 3267 3268 if (zc->zc_cookie & 1) 3269 flags |= ZFS_RENAME_RECURSIVE; 3270 if (zc->zc_cookie & 2) 3271 flags |= ZFS_RENAME_ALLOW_MOUNTED; 3272 3273 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0'; 3274 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 || 3275 strchr(zc->zc_value, '%')) 3276 return (EINVAL); 3277 3278 /* 3279 * Unmount snapshot unless we're doing a recursive rename, 3280 * in which case the dataset code figures out which snapshots 3281 * to unmount. 3282 */ 3283 if (!(flags & ZFS_RENAME_RECURSIVE) && 3284 strchr(zc->zc_name, '@') != NULL && 3285 zc->zc_objset_type == DMU_OST_ZFS) { 3286 int err = zfs_unmount_snap(zc->zc_name, NULL); 3287 if (err) 3288 return (err); 3289 } 3290 return (dmu_objset_rename(zc->zc_name, zc->zc_value, flags)); 3291} 3292 3293static int 3294zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr) 3295{ 3296 const char *propname = nvpair_name(pair); 3297 boolean_t issnap = (strchr(dsname, '@') != NULL); 3298 zfs_prop_t prop = zfs_name_to_prop(propname); 3299 uint64_t intval; 3300 int err; 3301 3302 if (prop == ZPROP_INVAL) { 3303 if (zfs_prop_user(propname)) { 3304 if (err = zfs_secpolicy_write_perms(dsname, 3305 ZFS_DELEG_PERM_USERPROP, cr)) 3306 return (err); 3307 return (0); 3308 } 3309 3310 if (!issnap && zfs_prop_userquota(propname)) { 3311 const char *perm = NULL; 3312 const char *uq_prefix = 3313 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA]; 3314 const char *gq_prefix = 3315 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA]; 3316 3317 if (strncmp(propname, uq_prefix, 3318 strlen(uq_prefix)) == 0) { 3319 perm = ZFS_DELEG_PERM_USERQUOTA; 3320 } else if (strncmp(propname, gq_prefix, 3321 strlen(gq_prefix)) == 0) { 3322 perm = ZFS_DELEG_PERM_GROUPQUOTA; 3323 } else { 3324 /* USERUSED and GROUPUSED are read-only */ 3325 return (EINVAL); 3326 } 3327 3328 if (err = zfs_secpolicy_write_perms(dsname, perm, cr)) 3329 return (err); 3330 return (0); 3331 } 3332 3333 return (EINVAL); 3334 } 3335 3336 if (issnap) 3337 return (EINVAL); 3338 3339 if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 3340 /* 3341 * dsl_prop_get_all_impl() returns properties in this 3342 * format. 3343 */ 3344 nvlist_t *attrs; 3345 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 3346 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 3347 &pair) == 0); 3348 } 3349 3350 /* 3351 * Check that this value is valid for this pool version 3352 */ 3353 switch (prop) { 3354 case ZFS_PROP_COMPRESSION: 3355 /* 3356 * If the user specified gzip compression, make sure 3357 * the SPA supports it. We ignore any errors here since 3358 * we'll catch them later. 3359 */ 3360 if (nvpair_type(pair) == DATA_TYPE_UINT64 && 3361 nvpair_value_uint64(pair, &intval) == 0) { 3362 if (intval >= ZIO_COMPRESS_GZIP_1 && 3363 intval <= ZIO_COMPRESS_GZIP_9 && 3364 zfs_earlier_version(dsname, 3365 SPA_VERSION_GZIP_COMPRESSION)) { 3366 return (ENOTSUP); 3367 } 3368 3369 if (intval == ZIO_COMPRESS_ZLE && 3370 zfs_earlier_version(dsname, 3371 SPA_VERSION_ZLE_COMPRESSION)) 3372 return (ENOTSUP); 3373 3374 /* 3375 * If this is a bootable dataset then 3376 * verify that the compression algorithm 3377 * is supported for booting. We must return 3378 * something other than ENOTSUP since it 3379 * implies a downrev pool version. 3380 */ 3381 if (zfs_is_bootfs(dsname) && 3382 !BOOTFS_COMPRESS_VALID(intval)) { 3383 return (ERANGE); 3384 } 3385 } 3386 break; 3387 3388 case ZFS_PROP_COPIES: 3389 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS)) 3390 return (ENOTSUP); 3391 break; 3392 3393 case ZFS_PROP_DEDUP: 3394 if (zfs_earlier_version(dsname, SPA_VERSION_DEDUP)) 3395 return (ENOTSUP); 3396 break; 3397 3398 case ZFS_PROP_SHARESMB: 3399 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID)) 3400 return (ENOTSUP); 3401 break; 3402 3403 case ZFS_PROP_ACLINHERIT: 3404 if (nvpair_type(pair) == DATA_TYPE_UINT64 && 3405 nvpair_value_uint64(pair, &intval) == 0) { 3406 if (intval == ZFS_ACL_PASSTHROUGH_X && 3407 zfs_earlier_version(dsname, 3408 SPA_VERSION_PASSTHROUGH_X)) 3409 return (ENOTSUP); 3410 } 3411 break; 3412 } 3413 3414 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED())); 3415} 3416 3417/* 3418 * Removes properties from the given props list that fail permission checks 3419 * needed to clear them and to restore them in case of a receive error. For each 3420 * property, make sure we have both set and inherit permissions. 3421 * 3422 * Returns the first error encountered if any permission checks fail. If the 3423 * caller provides a non-NULL errlist, it also gives the complete list of names 3424 * of all the properties that failed a permission check along with the 3425 * corresponding error numbers. The caller is responsible for freeing the 3426 * returned errlist. 3427 * 3428 * If every property checks out successfully, zero is returned and the list 3429 * pointed at by errlist is NULL. 3430 */ 3431static int 3432zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist) 3433{ 3434 zfs_cmd_t *zc; 3435 nvpair_t *pair, *next_pair; 3436 nvlist_t *errors; 3437 int err, rv = 0; 3438 3439 if (props == NULL) 3440 return (0); 3441 3442 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0); 3443 3444 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP); 3445 (void) strcpy(zc->zc_name, dataset); 3446 pair = nvlist_next_nvpair(props, NULL); 3447 while (pair != NULL) { 3448 next_pair = nvlist_next_nvpair(props, pair); 3449 3450 (void) strcpy(zc->zc_value, nvpair_name(pair)); 3451 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 || 3452 (err = zfs_secpolicy_inherit(zc, CRED())) != 0) { 3453 VERIFY(nvlist_remove_nvpair(props, pair) == 0); 3454 VERIFY(nvlist_add_int32(errors, 3455 zc->zc_value, err) == 0); 3456 } 3457 pair = next_pair; 3458 } 3459 kmem_free(zc, sizeof (zfs_cmd_t)); 3460 3461 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) { 3462 nvlist_free(errors); 3463 errors = NULL; 3464 } else { 3465 VERIFY(nvpair_value_int32(pair, &rv) == 0); 3466 } 3467 3468 if (errlist == NULL) 3469 nvlist_free(errors); 3470 else 3471 *errlist = errors; 3472 3473 return (rv); 3474} 3475 3476static boolean_t 3477propval_equals(nvpair_t *p1, nvpair_t *p2) 3478{ 3479 if (nvpair_type(p1) == DATA_TYPE_NVLIST) { 3480 /* dsl_prop_get_all_impl() format */ 3481 nvlist_t *attrs; 3482 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0); 3483 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 3484 &p1) == 0); 3485 } 3486 3487 if (nvpair_type(p2) == DATA_TYPE_NVLIST) { 3488 nvlist_t *attrs; 3489 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0); 3490 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 3491 &p2) == 0); 3492 } 3493 3494 if (nvpair_type(p1) != nvpair_type(p2)) 3495 return (B_FALSE); 3496 3497 if (nvpair_type(p1) == DATA_TYPE_STRING) { 3498 char *valstr1, *valstr2; 3499 3500 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0); 3501 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0); 3502 return (strcmp(valstr1, valstr2) == 0); 3503 } else { 3504 uint64_t intval1, intval2; 3505 3506 VERIFY(nvpair_value_uint64(p1, &intval1) == 0); 3507 VERIFY(nvpair_value_uint64(p2, &intval2) == 0); 3508 return (intval1 == intval2); 3509 } 3510} 3511 3512/* 3513 * Remove properties from props if they are not going to change (as determined 3514 * by comparison with origprops). Remove them from origprops as well, since we 3515 * do not need to clear or restore properties that won't change. 3516 */ 3517static void 3518props_reduce(nvlist_t *props, nvlist_t *origprops) 3519{ 3520 nvpair_t *pair, *next_pair; 3521 3522 if (origprops == NULL) 3523 return; /* all props need to be received */ 3524 3525 pair = nvlist_next_nvpair(props, NULL); 3526 while (pair != NULL) { 3527 const char *propname = nvpair_name(pair); 3528 nvpair_t *match; 3529 3530 next_pair = nvlist_next_nvpair(props, pair); 3531 3532 if ((nvlist_lookup_nvpair(origprops, propname, 3533 &match) != 0) || !propval_equals(pair, match)) 3534 goto next; /* need to set received value */ 3535 3536 /* don't clear the existing received value */ 3537 (void) nvlist_remove_nvpair(origprops, match); 3538 /* don't bother receiving the property */ 3539 (void) nvlist_remove_nvpair(props, pair); 3540next: 3541 pair = next_pair; 3542 } 3543} 3544 3545#ifdef DEBUG 3546static boolean_t zfs_ioc_recv_inject_err; 3547#endif 3548 3549/* 3550 * inputs: 3551 * zc_name name of containing filesystem 3552 * zc_nvlist_src{_size} nvlist of properties to apply 3553 * zc_value name of snapshot to create 3554 * zc_string name of clone origin (if DRR_FLAG_CLONE) 3555 * zc_cookie file descriptor to recv from 3556 * zc_begin_record the BEGIN record of the stream (not byteswapped) 3557 * zc_guid force flag 3558 * zc_cleanup_fd cleanup-on-exit file descriptor 3559 * zc_action_handle handle for this guid/ds mapping (or zero on first call) 3560 * 3561 * outputs: 3562 * zc_cookie number of bytes read 3563 * zc_nvlist_dst{_size} error for each unapplied received property 3564 * zc_obj zprop_errflags_t 3565 * zc_action_handle handle for this guid/ds mapping 3566 */ 3567static int 3568zfs_ioc_recv(zfs_cmd_t *zc) 3569{ 3570 file_t *fp; 3571 objset_t *os; 3572 dmu_recv_cookie_t drc; 3573 boolean_t force = (boolean_t)zc->zc_guid; 3574 int fd; 3575 int error = 0; 3576 int props_error = 0; 3577 nvlist_t *errors; 3578 offset_t off; 3579 nvlist_t *props = NULL; /* sent properties */ 3580 nvlist_t *origprops = NULL; /* existing properties */ 3581 objset_t *origin = NULL; 3582 char *tosnap; 3583 char tofs[ZFS_MAXNAMELEN]; 3584 boolean_t first_recvd_props = B_FALSE; 3585 3586 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 || 3587 strchr(zc->zc_value, '@') == NULL || 3588 strchr(zc->zc_value, '%')) 3589 return (EINVAL); 3590 3591 (void) strcpy(tofs, zc->zc_value); 3592 tosnap = strchr(tofs, '@'); 3593 *tosnap++ = '\0'; 3594 3595 if (zc->zc_nvlist_src != 0 && 3596 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 3597 zc->zc_iflags, &props)) != 0) 3598 return (error); 3599 3600 fd = zc->zc_cookie; 3601 fp = getf(fd); 3602 if (fp == NULL) { 3603 nvlist_free(props); 3604 return (EBADF); 3605 } 3606 3607 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0); 3608 3609 if (props && dmu_objset_hold(tofs, FTAG, &os) == 0) { 3610 if ((spa_version(os->os_spa) >= SPA_VERSION_RECVD_PROPS) && 3611 !dsl_prop_get_hasrecvd(os)) { 3612 first_recvd_props = B_TRUE; 3613 } 3614 3615 /* 3616 * If new received properties are supplied, they are to 3617 * completely replace the existing received properties, so stash 3618 * away the existing ones. 3619 */ 3620 if (dsl_prop_get_received(os, &origprops) == 0) { 3621 nvlist_t *errlist = NULL; 3622 /* 3623 * Don't bother writing a property if its value won't 3624 * change (and avoid the unnecessary security checks). 3625 * 3626 * The first receive after SPA_VERSION_RECVD_PROPS is a 3627 * special case where we blow away all local properties 3628 * regardless. 3629 */ 3630 if (!first_recvd_props) 3631 props_reduce(props, origprops); 3632 if (zfs_check_clearable(tofs, origprops, 3633 &errlist) != 0) 3634 (void) nvlist_merge(errors, errlist, 0); 3635 nvlist_free(errlist); 3636 } 3637 3638 dmu_objset_rele(os, FTAG); 3639 } 3640 3641 if (zc->zc_string[0]) { 3642 error = dmu_objset_hold(zc->zc_string, FTAG, &origin); 3643 if (error) 3644 goto out; 3645 } 3646 3647 error = dmu_recv_begin(tofs, tosnap, zc->zc_top_ds, 3648 &zc->zc_begin_record, force, origin, &drc); 3649 if (origin) 3650 dmu_objset_rele(origin, FTAG); 3651 if (error) 3652 goto out; 3653 3654 /* 3655 * Set properties before we receive the stream so that they are applied 3656 * to the new data. Note that we must call dmu_recv_stream() if 3657 * dmu_recv_begin() succeeds. 3658 */ 3659 if (props) { 3660 nvlist_t *errlist; 3661 3662 if (dmu_objset_from_ds(drc.drc_logical_ds, &os) == 0) { 3663 if (drc.drc_newfs) { 3664 if (spa_version(os->os_spa) >= 3665 SPA_VERSION_RECVD_PROPS) 3666 first_recvd_props = B_TRUE; 3667 } else if (origprops != NULL) { 3668 if (clear_received_props(os, tofs, origprops, 3669 first_recvd_props ? NULL : props) != 0) 3670 zc->zc_obj |= ZPROP_ERR_NOCLEAR; 3671 } else { 3672 zc->zc_obj |= ZPROP_ERR_NOCLEAR; 3673 } 3674 dsl_prop_set_hasrecvd(os); 3675 } else if (!drc.drc_newfs) { 3676 zc->zc_obj |= ZPROP_ERR_NOCLEAR; 3677 } 3678 3679 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED, 3680 props, &errlist); 3681 (void) nvlist_merge(errors, errlist, 0); 3682 nvlist_free(errlist); 3683 } 3684 3685 if (fit_error_list(zc, &errors) != 0 || put_nvlist(zc, errors) != 0) { 3686 /* 3687 * Caller made zc->zc_nvlist_dst less than the minimum expected 3688 * size or supplied an invalid address. 3689 */ 3690 props_error = EINVAL; 3691 } 3692 3693 off = fp->f_offset; 3694 error = dmu_recv_stream(&drc, fp, &off, zc->zc_cleanup_fd, 3695 &zc->zc_action_handle); 3696 3697 if (error == 0) { 3698 zfsvfs_t *zfsvfs = NULL; 3699 3700 if (getzfsvfs(tofs, &zfsvfs) == 0) { 3701 /* online recv */ 3702 int end_err; 3703 3704 error = zfs_suspend_fs(zfsvfs); 3705 /* 3706 * If the suspend fails, then the recv_end will 3707 * likely also fail, and clean up after itself. 3708 */ 3709 end_err = dmu_recv_end(&drc); 3710 if (error == 0) 3711 error = zfs_resume_fs(zfsvfs, tofs); 3712 error = error ? error : end_err; 3713 VFS_RELE(zfsvfs->z_vfs); 3714 } else { 3715 error = dmu_recv_end(&drc); 3716 } 3717 } 3718 3719 zc->zc_cookie = off - fp->f_offset; 3720 if (off >= 0 && off <= MAXOFFSET_T) 3721 fp->f_offset = off; 3722 3723#ifdef DEBUG 3724 if (zfs_ioc_recv_inject_err) { 3725 zfs_ioc_recv_inject_err = B_FALSE; 3726 error = 1; 3727 } 3728#endif 3729 /* 3730 * On error, restore the original props. 3731 */ 3732 if (error && props) { 3733 if (dmu_objset_hold(tofs, FTAG, &os) == 0) { 3734 if (clear_received_props(os, tofs, props, NULL) != 0) { 3735 /* 3736 * We failed to clear the received properties. 3737 * Since we may have left a $recvd value on the 3738 * system, we can't clear the $hasrecvd flag. 3739 */ 3740 zc->zc_obj |= ZPROP_ERR_NORESTORE; 3741 } else if (first_recvd_props) { 3742 dsl_prop_unset_hasrecvd(os); 3743 } 3744 dmu_objset_rele(os, FTAG); 3745 } else if (!drc.drc_newfs) { 3746 /* We failed to clear the received properties. */ 3747 zc->zc_obj |= ZPROP_ERR_NORESTORE; 3748 } 3749 3750 if (origprops == NULL && !drc.drc_newfs) { 3751 /* We failed to stash the original properties. */ 3752 zc->zc_obj |= ZPROP_ERR_NORESTORE; 3753 } 3754 3755 /* 3756 * dsl_props_set() will not convert RECEIVED to LOCAL on or 3757 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL 3758 * explictly if we're restoring local properties cleared in the 3759 * first new-style receive. 3760 */ 3761 if (origprops != NULL && 3762 zfs_set_prop_nvlist(tofs, (first_recvd_props ? 3763 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED), 3764 origprops, NULL) != 0) { 3765 /* 3766 * We stashed the original properties but failed to 3767 * restore them. 3768 */ 3769 zc->zc_obj |= ZPROP_ERR_NORESTORE; 3770 } 3771 } 3772out: 3773 nvlist_free(props); 3774 nvlist_free(origprops); 3775 nvlist_free(errors); 3776 releasef(fd); 3777 3778 if (error == 0) 3779 error = props_error; 3780 3781 return (error); 3782} 3783 3784/* 3785 * inputs: 3786 * zc_name name of snapshot to send 3787 * zc_cookie file descriptor to send stream to 3788 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj) 3789 * zc_sendobj objsetid of snapshot to send 3790 * zc_fromobj objsetid of incremental fromsnap (may be zero) 3791 * 3792 * outputs: none 3793 */ 3794static int 3795zfs_ioc_send(zfs_cmd_t *zc) 3796{ 3797 objset_t *fromsnap = NULL; 3798 objset_t *tosnap; 3799 file_t *fp; 3800 int error; 3801 offset_t off; 3802 dsl_dataset_t *ds; 3803 dsl_dataset_t *dsfrom = NULL; 3804 spa_t *spa; 3805 dsl_pool_t *dp; 3806 3807 error = spa_open(zc->zc_name, &spa, FTAG); 3808 if (error) 3809 return (error); 3810 3811 dp = spa_get_dsl(spa); 3812 rw_enter(&dp->dp_config_rwlock, RW_READER); 3813 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds); 3814 rw_exit(&dp->dp_config_rwlock); 3815 if (error) { 3816 spa_close(spa, FTAG); 3817 return (error); 3818 } 3819 3820 error = dmu_objset_from_ds(ds, &tosnap); 3821 if (error) { 3822 dsl_dataset_rele(ds, FTAG); 3823 spa_close(spa, FTAG); 3824 return (error); 3825 } 3826 3827 if (zc->zc_fromobj != 0) { 3828 rw_enter(&dp->dp_config_rwlock, RW_READER); 3829 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj, FTAG, &dsfrom); 3830 rw_exit(&dp->dp_config_rwlock); 3831 spa_close(spa, FTAG); 3832 if (error) { 3833 dsl_dataset_rele(ds, FTAG); 3834 return (error); 3835 } 3836 error = dmu_objset_from_ds(dsfrom, &fromsnap); 3837 if (error) { 3838 dsl_dataset_rele(dsfrom, FTAG); 3839 dsl_dataset_rele(ds, FTAG); 3840 return (error); 3841 } 3842 } else { 3843 spa_close(spa, FTAG); 3844 } 3845 3846 fp = getf(zc->zc_cookie); 3847 if (fp == NULL) { 3848 dsl_dataset_rele(ds, FTAG); 3849 if (dsfrom) 3850 dsl_dataset_rele(dsfrom, FTAG); 3851 return (EBADF); 3852 } 3853 3854 off = fp->f_offset; 3855 error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp, &off); 3856 3857 if (off >= 0 && off <= MAXOFFSET_T) 3858 fp->f_offset = off; 3859 releasef(zc->zc_cookie); 3860 if (dsfrom) 3861 dsl_dataset_rele(dsfrom, FTAG); 3862 dsl_dataset_rele(ds, FTAG); 3863 return (error); 3864} 3865 3866static int 3867zfs_ioc_inject_fault(zfs_cmd_t *zc) 3868{ 3869 int id, error; 3870 3871 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id, 3872 &zc->zc_inject_record); 3873 3874 if (error == 0) 3875 zc->zc_guid = (uint64_t)id; 3876 3877 return (error); 3878} 3879 3880static int 3881zfs_ioc_clear_fault(zfs_cmd_t *zc) 3882{ 3883 return (zio_clear_fault((int)zc->zc_guid)); 3884} 3885 3886static int 3887zfs_ioc_inject_list_next(zfs_cmd_t *zc) 3888{ 3889 int id = (int)zc->zc_guid; 3890 int error; 3891 3892 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name), 3893 &zc->zc_inject_record); 3894 3895 zc->zc_guid = id; 3896 3897 return (error); 3898} 3899 3900static int 3901zfs_ioc_error_log(zfs_cmd_t *zc) 3902{ 3903 spa_t *spa; 3904 int error; 3905 size_t count = (size_t)zc->zc_nvlist_dst_size; 3906 3907 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 3908 return (error); 3909 3910 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst, 3911 &count); 3912 if (error == 0) 3913 zc->zc_nvlist_dst_size = count; 3914 else 3915 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa); 3916 3917 spa_close(spa, FTAG); 3918 3919 return (error); 3920} 3921 3922static int 3923zfs_ioc_clear(zfs_cmd_t *zc) 3924{ 3925 spa_t *spa; 3926 vdev_t *vd; 3927 int error; 3928 3929 /* 3930 * On zpool clear we also fix up missing slogs 3931 */ 3932 mutex_enter(&spa_namespace_lock); 3933 spa = spa_lookup(zc->zc_name); 3934 if (spa == NULL) { 3935 mutex_exit(&spa_namespace_lock); 3936 return (EIO); 3937 } 3938 if (spa_get_log_state(spa) == SPA_LOG_MISSING) { 3939 /* we need to let spa_open/spa_load clear the chains */ 3940 spa_set_log_state(spa, SPA_LOG_CLEAR); 3941 } 3942 spa->spa_last_open_failed = 0; 3943 mutex_exit(&spa_namespace_lock); 3944 3945 if (zc->zc_cookie & ZPOOL_NO_REWIND) { 3946 error = spa_open(zc->zc_name, &spa, FTAG); 3947 } else { 3948 nvlist_t *policy; 3949 nvlist_t *config = NULL; 3950 3951 if (zc->zc_nvlist_src == 0) 3952 return (EINVAL); 3953 3954 if ((error = get_nvlist(zc->zc_nvlist_src, 3955 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) { 3956 error = spa_open_rewind(zc->zc_name, &spa, FTAG, 3957 policy, &config); 3958 if (config != NULL) { 3959 int err; 3960 3961 if ((err = put_nvlist(zc, config)) != 0) 3962 error = err; 3963 nvlist_free(config); 3964 } 3965 nvlist_free(policy); 3966 } 3967 } 3968 3969 if (error) 3970 return (error); 3971 3972 spa_vdev_state_enter(spa, SCL_NONE); 3973 3974 if (zc->zc_guid == 0) { 3975 vd = NULL; 3976 } else { 3977 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE); 3978 if (vd == NULL) { 3979 (void) spa_vdev_state_exit(spa, NULL, ENODEV); 3980 spa_close(spa, FTAG); 3981 return (ENODEV); 3982 } 3983 } 3984 3985 vdev_clear(spa, vd); 3986 3987 (void) spa_vdev_state_exit(spa, NULL, 0); 3988 3989 /* 3990 * Resume any suspended I/Os. 3991 */ 3992 if (zio_resume(spa) != 0) 3993 error = EIO; 3994 3995 spa_close(spa, FTAG); 3996 3997 return (error); 3998} 3999 4000/* 4001 * inputs: 4002 * zc_name name of filesystem 4003 * zc_value name of origin snapshot 4004 * 4005 * outputs: 4006 * zc_string name of conflicting snapshot, if there is one 4007 */ 4008static int 4009zfs_ioc_promote(zfs_cmd_t *zc) 4010{ 4011 char *cp; 4012 4013 /* 4014 * We don't need to unmount *all* the origin fs's snapshots, but 4015 * it's easier. 4016 */ 4017 cp = strchr(zc->zc_value, '@'); 4018 if (cp) 4019 *cp = '\0'; 4020 (void) dmu_objset_find(zc->zc_value, 4021 zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS); 4022 return (dsl_dataset_promote(zc->zc_name, zc->zc_string)); 4023} 4024 4025/* 4026 * Retrieve a single {user|group}{used|quota}@... property. 4027 * 4028 * inputs: 4029 * zc_name name of filesystem 4030 * zc_objset_type zfs_userquota_prop_t 4031 * zc_value domain name (eg. "S-1-234-567-89") 4032 * zc_guid RID/UID/GID 4033 * 4034 * outputs: 4035 * zc_cookie property value 4036 */ 4037static int 4038zfs_ioc_userspace_one(zfs_cmd_t *zc) 4039{ 4040 zfsvfs_t *zfsvfs; 4041 int error; 4042 4043 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) 4044 return (EINVAL); 4045 4046 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE); 4047 if (error) 4048 return (error); 4049 4050 error = zfs_userspace_one(zfsvfs, 4051 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie); 4052 zfsvfs_rele(zfsvfs, FTAG); 4053 4054 return (error); 4055} 4056 4057/* 4058 * inputs: 4059 * zc_name name of filesystem 4060 * zc_cookie zap cursor 4061 * zc_objset_type zfs_userquota_prop_t 4062 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist) 4063 * 4064 * outputs: 4065 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t) 4066 * zc_cookie zap cursor 4067 */ 4068static int 4069zfs_ioc_userspace_many(zfs_cmd_t *zc) 4070{ 4071 zfsvfs_t *zfsvfs; 4072 int bufsize = zc->zc_nvlist_dst_size; 4073 4074 if (bufsize <= 0) 4075 return (ENOMEM); 4076 4077 int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE); 4078 if (error) 4079 return (error); 4080 4081 void *buf = kmem_alloc(bufsize, KM_SLEEP); 4082 4083 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie, 4084 buf, &zc->zc_nvlist_dst_size); 4085 4086 if (error == 0) { 4087 error = ddi_copyout(buf, 4088 (void *)(uintptr_t)zc->zc_nvlist_dst, 4089 zc->zc_nvlist_dst_size, zc->zc_iflags); 4090 } 4091 kmem_free(buf, bufsize); 4092 zfsvfs_rele(zfsvfs, FTAG); 4093 4094 return (error); 4095} 4096 4097/* 4098 * inputs: 4099 * zc_name name of filesystem 4100 * 4101 * outputs: 4102 * none 4103 */ 4104static int 4105zfs_ioc_userspace_upgrade(zfs_cmd_t *zc) 4106{ 4107 objset_t *os; 4108 int error = 0; 4109 zfsvfs_t *zfsvfs; 4110 4111 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) { 4112 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) { 4113 /* 4114 * If userused is not enabled, it may be because the 4115 * objset needs to be closed & reopened (to grow the 4116 * objset_phys_t). Suspend/resume the fs will do that. 4117 */ 4118 error = zfs_suspend_fs(zfsvfs); 4119 if (error == 0) 4120 error = zfs_resume_fs(zfsvfs, zc->zc_name); 4121 } 4122 if (error == 0) 4123 error = dmu_objset_userspace_upgrade(zfsvfs->z_os); 4124 VFS_RELE(zfsvfs->z_vfs); 4125 } else { 4126 /* XXX kind of reading contents without owning */ 4127 error = dmu_objset_hold(zc->zc_name, FTAG, &os); 4128 if (error) 4129 return (error); 4130 4131 error = dmu_objset_userspace_upgrade(os); 4132 dmu_objset_rele(os, FTAG); 4133 } 4134 4135 return (error); 4136} 4137 4138#ifdef sun 4139/* 4140 * We don't want to have a hard dependency 4141 * against some special symbols in sharefs 4142 * nfs, and smbsrv. Determine them if needed when 4143 * the first file system is shared. 4144 * Neither sharefs, nfs or smbsrv are unloadable modules. 4145 */ 4146int (*znfsexport_fs)(void *arg); 4147int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t); 4148int (*zsmbexport_fs)(void *arg, boolean_t add_share); 4149 4150int zfs_nfsshare_inited; 4151int zfs_smbshare_inited; 4152 4153ddi_modhandle_t nfs_mod; 4154ddi_modhandle_t sharefs_mod; 4155ddi_modhandle_t smbsrv_mod; 4156#endif /* sun */ 4157kmutex_t zfs_share_lock; 4158 4159#ifdef sun 4160static int 4161zfs_init_sharefs() 4162{ 4163 int error; 4164 4165 ASSERT(MUTEX_HELD(&zfs_share_lock)); 4166 /* Both NFS and SMB shares also require sharetab support. */ 4167 if (sharefs_mod == NULL && ((sharefs_mod = 4168 ddi_modopen("fs/sharefs", 4169 KRTLD_MODE_FIRST, &error)) == NULL)) { 4170 return (ENOSYS); 4171 } 4172 if (zshare_fs == NULL && ((zshare_fs = 4173 (int (*)(enum sharefs_sys_op, share_t *, uint32_t)) 4174 ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) { 4175 return (ENOSYS); 4176 } 4177 return (0); 4178} 4179#endif /* sun */ 4180 4181static int 4182zfs_ioc_share(zfs_cmd_t *zc) 4183{ 4184#ifdef sun 4185 int error; 4186 int opcode; 4187 4188 switch (zc->zc_share.z_sharetype) { 4189 case ZFS_SHARE_NFS: 4190 case ZFS_UNSHARE_NFS: 4191 if (zfs_nfsshare_inited == 0) { 4192 mutex_enter(&zfs_share_lock); 4193 if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs", 4194 KRTLD_MODE_FIRST, &error)) == NULL)) { 4195 mutex_exit(&zfs_share_lock); 4196 return (ENOSYS); 4197 } 4198 if (znfsexport_fs == NULL && 4199 ((znfsexport_fs = (int (*)(void *)) 4200 ddi_modsym(nfs_mod, 4201 "nfs_export", &error)) == NULL)) { 4202 mutex_exit(&zfs_share_lock); 4203 return (ENOSYS); 4204 } 4205 error = zfs_init_sharefs(); 4206 if (error) { 4207 mutex_exit(&zfs_share_lock); 4208 return (ENOSYS); 4209 } 4210 zfs_nfsshare_inited = 1; 4211 mutex_exit(&zfs_share_lock); 4212 } 4213 break; 4214 case ZFS_SHARE_SMB: 4215 case ZFS_UNSHARE_SMB: 4216 if (zfs_smbshare_inited == 0) { 4217 mutex_enter(&zfs_share_lock); 4218 if (smbsrv_mod == NULL && ((smbsrv_mod = 4219 ddi_modopen("drv/smbsrv", 4220 KRTLD_MODE_FIRST, &error)) == NULL)) { 4221 mutex_exit(&zfs_share_lock); 4222 return (ENOSYS); 4223 } 4224 if (zsmbexport_fs == NULL && ((zsmbexport_fs = 4225 (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod, 4226 "smb_server_share", &error)) == NULL)) { 4227 mutex_exit(&zfs_share_lock); 4228 return (ENOSYS); 4229 } 4230 error = zfs_init_sharefs(); 4231 if (error) { 4232 mutex_exit(&zfs_share_lock); 4233 return (ENOSYS); 4234 } 4235 zfs_smbshare_inited = 1; 4236 mutex_exit(&zfs_share_lock); 4237 } 4238 break; 4239 default: 4240 return (EINVAL); 4241 } 4242 4243 switch (zc->zc_share.z_sharetype) { 4244 case ZFS_SHARE_NFS: 4245 case ZFS_UNSHARE_NFS: 4246 if (error = 4247 znfsexport_fs((void *) 4248 (uintptr_t)zc->zc_share.z_exportdata)) 4249 return (error); 4250 break; 4251 case ZFS_SHARE_SMB: 4252 case ZFS_UNSHARE_SMB: 4253 if (error = zsmbexport_fs((void *) 4254 (uintptr_t)zc->zc_share.z_exportdata, 4255 zc->zc_share.z_sharetype == ZFS_SHARE_SMB ? 4256 B_TRUE: B_FALSE)) { 4257 return (error); 4258 } 4259 break; 4260 } 4261 4262 opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS || 4263 zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ? 4264 SHAREFS_ADD : SHAREFS_REMOVE; 4265 4266 /* 4267 * Add or remove share from sharetab 4268 */ 4269 error = zshare_fs(opcode, 4270 (void *)(uintptr_t)zc->zc_share.z_sharedata, 4271 zc->zc_share.z_sharemax); 4272 4273 return (error); 4274 4275#else /* !sun */ 4276 return (ENOSYS); 4277#endif /* !sun */ 4278} 4279 4280ace_t full_access[] = { 4281 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0} 4282}; 4283 4284/* 4285 * inputs: 4286 * zc_name name of containing filesystem 4287 * zc_obj object # beyond which we want next in-use object # 4288 * 4289 * outputs: 4290 * zc_obj next in-use object # 4291 */ 4292static int 4293zfs_ioc_next_obj(zfs_cmd_t *zc) 4294{ 4295 objset_t *os = NULL; 4296 int error; 4297 4298 error = dmu_objset_hold(zc->zc_name, FTAG, &os); 4299 if (error) 4300 return (error); 4301 4302 error = dmu_object_next(os, &zc->zc_obj, B_FALSE, 4303 os->os_dsl_dataset->ds_phys->ds_prev_snap_txg); 4304 4305 dmu_objset_rele(os, FTAG); 4306 return (error); 4307} 4308 4309/* 4310 * inputs: 4311 * zc_name name of filesystem 4312 * zc_value prefix name for snapshot 4313 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process 4314 * 4315 * outputs: 4316 */ 4317static int 4318zfs_ioc_tmp_snapshot(zfs_cmd_t *zc) 4319{ 4320 char *snap_name; 4321 int error; 4322 4323 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value, 4324 (u_longlong_t)ddi_get_lbolt64()); 4325 4326 if (strlen(snap_name) >= MAXNAMELEN) { 4327 strfree(snap_name); 4328 return (E2BIG); 4329 } 4330 4331 error = dmu_objset_snapshot(zc->zc_name, snap_name, snap_name, 4332 NULL, B_FALSE, B_TRUE, zc->zc_cleanup_fd); 4333 if (error != 0) { 4334 strfree(snap_name); 4335 return (error); 4336 } 4337 4338 (void) strcpy(zc->zc_value, snap_name); 4339 strfree(snap_name); 4340 return (0); 4341} 4342 4343/* 4344 * inputs: 4345 * zc_name name of "to" snapshot 4346 * zc_value name of "from" snapshot 4347 * zc_cookie file descriptor to write diff data on 4348 * 4349 * outputs: 4350 * dmu_diff_record_t's to the file descriptor 4351 */ 4352static int 4353zfs_ioc_diff(zfs_cmd_t *zc) 4354{ 4355 objset_t *fromsnap; 4356 objset_t *tosnap; 4357 file_t *fp; 4358 offset_t off; 4359 int error; 4360 4361 error = dmu_objset_hold(zc->zc_name, FTAG, &tosnap); 4362 if (error) 4363 return (error); 4364 4365 error = dmu_objset_hold(zc->zc_value, FTAG, &fromsnap); 4366 if (error) { 4367 dmu_objset_rele(tosnap, FTAG); 4368 return (error); 4369 } 4370 4371 fp = getf(zc->zc_cookie); 4372 if (fp == NULL) { 4373 dmu_objset_rele(fromsnap, FTAG); 4374 dmu_objset_rele(tosnap, FTAG); 4375 return (EBADF); 4376 } 4377 4378 off = fp->f_offset; 4379 4380 error = dmu_diff(tosnap, fromsnap, fp, &off); 4381 4382 if (off >= 0 && off <= MAXOFFSET_T) 4383 fp->f_offset = off; 4384 releasef(zc->zc_cookie); 4385 4386 dmu_objset_rele(fromsnap, FTAG); 4387 dmu_objset_rele(tosnap, FTAG); 4388 return (error); 4389} 4390 4391#ifdef sun 4392/* 4393 * Remove all ACL files in shares dir 4394 */ 4395static int 4396zfs_smb_acl_purge(znode_t *dzp) 4397{ 4398 zap_cursor_t zc; 4399 zap_attribute_t zap; 4400 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 4401 int error; 4402 4403 for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id); 4404 (error = zap_cursor_retrieve(&zc, &zap)) == 0; 4405 zap_cursor_advance(&zc)) { 4406 if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred, 4407 NULL, 0)) != 0) 4408 break; 4409 } 4410 zap_cursor_fini(&zc); 4411 return (error); 4412} 4413#endif /* sun */ 4414 4415static int 4416zfs_ioc_smb_acl(zfs_cmd_t *zc) 4417{ 4418#ifdef sun 4419 vnode_t *vp; 4420 znode_t *dzp; 4421 vnode_t *resourcevp = NULL; 4422 znode_t *sharedir; 4423 zfsvfs_t *zfsvfs; 4424 nvlist_t *nvlist; 4425 char *src, *target; 4426 vattr_t vattr; 4427 vsecattr_t vsec; 4428 int error = 0; 4429 4430 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE, 4431 NO_FOLLOW, NULL, &vp)) != 0) 4432 return (error); 4433 4434 /* Now make sure mntpnt and dataset are ZFS */ 4435 4436 if (strcmp(vp->v_vfsp->mnt_stat.f_fstypename, "zfs") != 0 || 4437 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource), 4438 zc->zc_name) != 0)) { 4439 VN_RELE(vp); 4440 return (EINVAL); 4441 } 4442 4443 dzp = VTOZ(vp); 4444 zfsvfs = dzp->z_zfsvfs; 4445 ZFS_ENTER(zfsvfs); 4446 4447 /* 4448 * Create share dir if its missing. 4449 */ 4450 mutex_enter(&zfsvfs->z_lock); 4451 if (zfsvfs->z_shares_dir == 0) { 4452 dmu_tx_t *tx; 4453 4454 tx = dmu_tx_create(zfsvfs->z_os); 4455 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE, 4456 ZFS_SHARES_DIR); 4457 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 4458 error = dmu_tx_assign(tx, TXG_WAIT); 4459 if (error) { 4460 dmu_tx_abort(tx); 4461 } else { 4462 error = zfs_create_share_dir(zfsvfs, tx); 4463 dmu_tx_commit(tx); 4464 } 4465 if (error) { 4466 mutex_exit(&zfsvfs->z_lock); 4467 VN_RELE(vp); 4468 ZFS_EXIT(zfsvfs); 4469 return (error); 4470 } 4471 } 4472 mutex_exit(&zfsvfs->z_lock); 4473 4474 ASSERT(zfsvfs->z_shares_dir); 4475 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) { 4476 VN_RELE(vp); 4477 ZFS_EXIT(zfsvfs); 4478 return (error); 4479 } 4480 4481 switch (zc->zc_cookie) { 4482 case ZFS_SMB_ACL_ADD: 4483 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE; 4484 vattr.va_type = VREG; 4485 vattr.va_mode = S_IFREG|0777; 4486 vattr.va_uid = 0; 4487 vattr.va_gid = 0; 4488 4489 vsec.vsa_mask = VSA_ACE; 4490 vsec.vsa_aclentp = &full_access; 4491 vsec.vsa_aclentsz = sizeof (full_access); 4492 vsec.vsa_aclcnt = 1; 4493 4494 error = VOP_CREATE(ZTOV(sharedir), zc->zc_string, 4495 &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec); 4496 if (resourcevp) 4497 VN_RELE(resourcevp); 4498 break; 4499 4500 case ZFS_SMB_ACL_REMOVE: 4501 error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred, 4502 NULL, 0); 4503 break; 4504 4505 case ZFS_SMB_ACL_RENAME: 4506 if ((error = get_nvlist(zc->zc_nvlist_src, 4507 zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) { 4508 VN_RELE(vp); 4509 ZFS_EXIT(zfsvfs); 4510 return (error); 4511 } 4512 if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) || 4513 nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET, 4514 &target)) { 4515 VN_RELE(vp); 4516 VN_RELE(ZTOV(sharedir)); 4517 ZFS_EXIT(zfsvfs); 4518 nvlist_free(nvlist); 4519 return (error); 4520 } 4521 error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target, 4522 kcred, NULL, 0); 4523 nvlist_free(nvlist); 4524 break; 4525 4526 case ZFS_SMB_ACL_PURGE: 4527 error = zfs_smb_acl_purge(sharedir); 4528 break; 4529 4530 default: 4531 error = EINVAL; 4532 break; 4533 } 4534 4535 VN_RELE(vp); 4536 VN_RELE(ZTOV(sharedir)); 4537 4538 ZFS_EXIT(zfsvfs); 4539 4540 return (error); 4541#else /* !sun */ 4542 return (EOPNOTSUPP); 4543#endif /* !sun */ 4544} 4545 4546/* 4547 * inputs: 4548 * zc_name name of filesystem 4549 * zc_value short name of snap 4550 * zc_string user-supplied tag for this hold 4551 * zc_cookie recursive flag 4552 * zc_temphold set if hold is temporary 4553 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process 4554 * zc_sendobj if non-zero, the objid for zc_name@zc_value 4555 * zc_createtxg if zc_sendobj is non-zero, snap must have zc_createtxg 4556 * 4557 * outputs: none 4558 */ 4559static int 4560zfs_ioc_hold(zfs_cmd_t *zc) 4561{ 4562 boolean_t recursive = zc->zc_cookie; 4563 spa_t *spa; 4564 dsl_pool_t *dp; 4565 dsl_dataset_t *ds; 4566 int error; 4567 minor_t minor = 0; 4568 4569 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 4570 return (EINVAL); 4571 4572 if (zc->zc_sendobj == 0) { 4573 return (dsl_dataset_user_hold(zc->zc_name, zc->zc_value, 4574 zc->zc_string, recursive, zc->zc_temphold, 4575 zc->zc_cleanup_fd)); 4576 } 4577 4578 if (recursive) 4579 return (EINVAL); 4580 4581 error = spa_open(zc->zc_name, &spa, FTAG); 4582 if (error) 4583 return (error); 4584 4585 dp = spa_get_dsl(spa); 4586 rw_enter(&dp->dp_config_rwlock, RW_READER); 4587 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds); 4588 rw_exit(&dp->dp_config_rwlock); 4589 spa_close(spa, FTAG); 4590 if (error) 4591 return (error); 4592 4593 /* 4594 * Until we have a hold on this snapshot, it's possible that 4595 * zc_sendobj could've been destroyed and reused as part 4596 * of a later txg. Make sure we're looking at the right object. 4597 */ 4598 if (zc->zc_createtxg != ds->ds_phys->ds_creation_txg) { 4599 dsl_dataset_rele(ds, FTAG); 4600 return (ENOENT); 4601 } 4602 4603 if (zc->zc_cleanup_fd != -1 && zc->zc_temphold) { 4604 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor); 4605 if (error) { 4606 dsl_dataset_rele(ds, FTAG); 4607 return (error); 4608 } 4609 } 4610 4611 error = dsl_dataset_user_hold_for_send(ds, zc->zc_string, 4612 zc->zc_temphold); 4613 if (minor != 0) { 4614 if (error == 0) { 4615 dsl_register_onexit_hold_cleanup(ds, zc->zc_string, 4616 minor); 4617 } 4618 zfs_onexit_fd_rele(zc->zc_cleanup_fd); 4619 } 4620 dsl_dataset_rele(ds, FTAG); 4621 4622 return (error); 4623} 4624 4625/* 4626 * inputs: 4627 * zc_name name of dataset from which we're releasing a user hold 4628 * zc_value short name of snap 4629 * zc_string user-supplied tag for this hold 4630 * zc_cookie recursive flag 4631 * 4632 * outputs: none 4633 */ 4634static int 4635zfs_ioc_release(zfs_cmd_t *zc) 4636{ 4637 boolean_t recursive = zc->zc_cookie; 4638 4639 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 4640 return (EINVAL); 4641 4642 return (dsl_dataset_user_release(zc->zc_name, zc->zc_value, 4643 zc->zc_string, recursive)); 4644} 4645 4646/* 4647 * inputs: 4648 * zc_name name of filesystem 4649 * 4650 * outputs: 4651 * zc_nvlist_src{_size} nvlist of snapshot holds 4652 */ 4653static int 4654zfs_ioc_get_holds(zfs_cmd_t *zc) 4655{ 4656 nvlist_t *nvp; 4657 int error; 4658 4659 if ((error = dsl_dataset_get_holds(zc->zc_name, &nvp)) == 0) { 4660 error = put_nvlist(zc, nvp); 4661 nvlist_free(nvp); 4662 } 4663 4664 return (error); 4665} 4666 4667/* 4668 * pool create, destroy, and export don't log the history as part of 4669 * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export 4670 * do the logging of those commands. 4671 */ 4672static int 4673zfs_ioc_jail(zfs_cmd_t *zc) 4674{ 4675 4676 return (zone_dataset_attach(curthread->td_ucred, zc->zc_name, 4677 (int)zc->zc_jailid)); 4678} 4679 4680static int 4681zfs_ioc_unjail(zfs_cmd_t *zc) 4682{ 4683 4684 return (zone_dataset_detach(curthread->td_ucred, zc->zc_name, 4685 (int)zc->zc_jailid)); 4686} 4687 4688static zfs_ioc_vec_t zfs_ioc_vec[] = { 4689 { zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE, 4690 B_FALSE }, 4691 { zfs_ioc_pool_destroy, zfs_secpolicy_config, POOL_NAME, B_FALSE, 4692 B_FALSE }, 4693 { zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE, 4694 B_FALSE }, 4695 { zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE, 4696 B_FALSE }, 4697 { zfs_ioc_pool_configs, zfs_secpolicy_none, NO_NAME, B_FALSE, 4698 B_FALSE }, 4699 { zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE, 4700 B_FALSE }, 4701 { zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE, 4702 B_FALSE }, 4703 { zfs_ioc_pool_scan, zfs_secpolicy_config, POOL_NAME, B_TRUE, 4704 B_TRUE }, 4705 { zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE, 4706 B_FALSE }, 4707 { zfs_ioc_pool_upgrade, zfs_secpolicy_config, POOL_NAME, B_TRUE, 4708 B_TRUE }, 4709 { zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE, 4710 B_FALSE }, 4711 { zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE, 4712 B_TRUE }, 4713 { zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE, 4714 B_TRUE }, 4715 { zfs_ioc_vdev_set_state, zfs_secpolicy_config, POOL_NAME, B_TRUE, 4716 B_FALSE }, 4717 { zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE, 4718 B_TRUE }, 4719 { zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE, 4720 B_TRUE }, 4721 { zfs_ioc_vdev_setpath, zfs_secpolicy_config, POOL_NAME, B_FALSE, 4722 B_TRUE }, 4723 { zfs_ioc_vdev_setfru, zfs_secpolicy_config, POOL_NAME, B_FALSE, 4724 B_TRUE }, 4725 { zfs_ioc_objset_stats, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 4726 B_TRUE }, 4727 { zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 4728 B_FALSE }, 4729 { zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 4730 B_TRUE }, 4731 { zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 4732 B_TRUE }, 4733 { zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE, B_TRUE }, 4734 { zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE, B_TRUE }, 4735 { zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE, 4736 B_TRUE}, 4737 { zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE, 4738 B_TRUE }, 4739 { zfs_ioc_rename, zfs_secpolicy_rename, DATASET_NAME, B_TRUE, B_TRUE }, 4740 { zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE, B_TRUE }, 4741 { zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE, B_FALSE }, 4742 { zfs_ioc_inject_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE, 4743 B_FALSE }, 4744 { zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE, 4745 B_FALSE }, 4746 { zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE, 4747 B_FALSE }, 4748 { zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE, 4749 B_FALSE }, 4750 { zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE, B_FALSE }, 4751 { zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE, 4752 B_TRUE }, 4753 { zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, DATASET_NAME, 4754 B_TRUE, B_TRUE }, 4755 { zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE, 4756 B_TRUE }, 4757 { zfs_ioc_dsobj_to_dsname, zfs_secpolicy_diff, POOL_NAME, B_FALSE, 4758 B_FALSE }, 4759 { zfs_ioc_obj_to_path, zfs_secpolicy_diff, DATASET_NAME, B_FALSE, 4760 B_TRUE }, 4761 { zfs_ioc_pool_set_props, zfs_secpolicy_config, POOL_NAME, B_TRUE, 4762 B_TRUE }, 4763 { zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE, 4764 B_FALSE }, 4765 { zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE, 4766 B_TRUE }, 4767 { zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 4768 B_FALSE }, 4769 { zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE, B_FALSE }, 4770 { zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE, 4771 B_TRUE }, 4772 { zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE, 4773 B_FALSE }, 4774 { zfs_ioc_userspace_one, zfs_secpolicy_userspace_one, 4775 DATASET_NAME, B_FALSE, B_FALSE }, 4776 { zfs_ioc_userspace_many, zfs_secpolicy_userspace_many, 4777 DATASET_NAME, B_FALSE, B_FALSE }, 4778 { zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade, 4779 DATASET_NAME, B_FALSE, B_TRUE }, 4780 { zfs_ioc_hold, zfs_secpolicy_hold, DATASET_NAME, B_TRUE, B_TRUE }, 4781 { zfs_ioc_release, zfs_secpolicy_release, DATASET_NAME, B_TRUE, 4782 B_TRUE }, 4783 { zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 4784 B_TRUE }, 4785 { zfs_ioc_objset_recvd_props, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 4786 B_FALSE }, 4787 { zfs_ioc_vdev_split, zfs_secpolicy_config, POOL_NAME, B_TRUE, 4788 B_TRUE }, 4789 { zfs_ioc_next_obj, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 4790 B_FALSE }, 4791 { zfs_ioc_diff, zfs_secpolicy_diff, DATASET_NAME, B_FALSE, B_FALSE }, 4792 { zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot, DATASET_NAME, 4793 B_FALSE, B_FALSE }, 4794 { zfs_ioc_obj_to_stats, zfs_secpolicy_diff, DATASET_NAME, B_FALSE, 4795 B_TRUE }, 4796 { zfs_ioc_jail, zfs_secpolicy_config, DATASET_NAME, B_TRUE, B_FALSE }, 4797 { zfs_ioc_unjail, zfs_secpolicy_config, DATASET_NAME, B_TRUE, B_FALSE } 4798}; 4799 4800int 4801pool_status_check(const char *name, zfs_ioc_namecheck_t type) 4802{ 4803 spa_t *spa; 4804 int error; 4805 4806 ASSERT(type == POOL_NAME || type == DATASET_NAME); 4807 4808 error = spa_open(name, &spa, FTAG); 4809 if (error == 0) { 4810 if (spa_suspended(spa)) 4811 error = EAGAIN; 4812 spa_close(spa, FTAG); 4813 } 4814 return (error); 4815} 4816 4817/* 4818 * Find a free minor number. 4819 */ 4820minor_t 4821zfsdev_minor_alloc(void) 4822{ 4823 static minor_t last_minor; 4824 minor_t m; 4825 4826 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 4827 4828 for (m = last_minor + 1; m != last_minor; m++) { 4829 if (m > ZFSDEV_MAX_MINOR) 4830 m = 1; 4831 if (ddi_get_soft_state(zfsdev_state, m) == NULL) { 4832 last_minor = m; 4833 return (m); 4834 } 4835 } 4836 4837 return (0); 4838} 4839 4840static int 4841zfs_ctldev_init(struct cdev *devp) 4842{ 4843 minor_t minor; 4844 zfs_soft_state_t *zs; 4845 4846 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 4847 4848 minor = zfsdev_minor_alloc(); 4849 if (minor == 0) 4850 return (ENXIO); 4851 4852 if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS) 4853 return (EAGAIN); 4854 4855 devfs_set_cdevpriv((void *)(uintptr_t)minor, zfsdev_close); 4856 4857 zs = ddi_get_soft_state(zfsdev_state, minor); 4858 zs->zss_type = ZSST_CTLDEV; 4859 zfs_onexit_init((zfs_onexit_t **)&zs->zss_data); 4860 4861 return (0); 4862} 4863 4864static void 4865zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor) 4866{ 4867 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 4868 4869 zfs_onexit_destroy(zo); 4870 ddi_soft_state_free(zfsdev_state, minor); 4871} 4872 4873void * 4874zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which) 4875{ 4876 zfs_soft_state_t *zp; 4877 4878 zp = ddi_get_soft_state(zfsdev_state, minor); 4879 if (zp == NULL || zp->zss_type != which) 4880 return (NULL); 4881 4882 return (zp->zss_data); 4883} 4884 4885static int 4886zfsdev_open(struct cdev *devp, int flag, int mode, struct thread *td) 4887{ 4888 int error = 0; 4889 4890#ifdef sun 4891 if (getminor(*devp) != 0) 4892 return (zvol_open(devp, flag, otyp, cr)); 4893#endif 4894 4895 /* This is the control device. Allocate a new minor if requested. */ 4896 if (flag & FEXCL) { 4897 mutex_enter(&spa_namespace_lock); 4898 error = zfs_ctldev_init(devp); 4899 mutex_exit(&spa_namespace_lock); 4900 } 4901 4902 return (error); 4903} 4904 4905static void 4906zfsdev_close(void *data) 4907{ 4908 zfs_onexit_t *zo; 4909 minor_t minor = (minor_t)(uintptr_t)data; 4910 4911 if (minor == 0) 4912 return; 4913 4914 mutex_enter(&spa_namespace_lock); 4915 zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV); 4916 if (zo == NULL) { 4917 mutex_exit(&spa_namespace_lock); 4918 return; 4919 } 4920 zfs_ctldev_destroy(zo, minor); 4921 mutex_exit(&spa_namespace_lock); 4922} 4923 4924static int 4925zfsdev_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 4926 struct thread *td) 4927{ 4928 zfs_cmd_t *zc; 4929 uint_t vec; 4930 int cflag, error, len; 4931 4932 cflag = ZFS_CMD_COMPAT_NONE; 4933 len = IOCPARM_LEN(cmd); 4934 4935 /* 4936 * Check if we have sufficient kernel memory allocated 4937 * for the zfs_cmd_t request. Bail out if not so we 4938 * will not access undefined memory region. 4939 */ 4940 if (len < sizeof(zfs_cmd_t)) 4941 if (len == sizeof(zfs_cmd_v15_t)) { 4942 cflag = ZFS_CMD_COMPAT_V15; 4943 vec = zfs_ioctl_v15_to_v28[ZFS_IOC(cmd)]; 4944 } else 4945 return (EINVAL); 4946 else 4947 vec = ZFS_IOC(cmd); 4948 4949 if (cflag != ZFS_CMD_COMPAT_NONE) { 4950 if (vec == ZFS_IOC_COMPAT_PASS) 4951 return (0); 4952 else if (vec == ZFS_IOC_COMPAT_FAIL) 4953 return (ENOTSUP); 4954 } 4955 4956 if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0])) 4957 return (EINVAL); 4958 4959 if (cflag != ZFS_CMD_COMPAT_NONE) { 4960 zc = kmem_zalloc(sizeof(zfs_cmd_t), KM_SLEEP); 4961 bzero(zc, sizeof(zfs_cmd_t)); 4962 zfs_cmd_compat_get(zc, addr, cflag); 4963 zfs_ioctl_compat_pre(zc, &vec, cflag); 4964 } else { 4965 zc = (void *)addr; 4966 } 4967 4968 error = zfs_ioc_vec[vec].zvec_secpolicy(zc, td->td_ucred); 4969 4970 /* 4971 * Ensure that all pool/dataset names are valid before we pass down to 4972 * the lower layers. 4973 */ 4974 if (error == 0) { 4975 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0'; 4976 zc->zc_iflags = flag & FKIOCTL; 4977 switch (zfs_ioc_vec[vec].zvec_namecheck) { 4978 case POOL_NAME: 4979 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0) 4980 error = EINVAL; 4981 if (zfs_ioc_vec[vec].zvec_pool_check) 4982 error = pool_status_check(zc->zc_name, 4983 zfs_ioc_vec[vec].zvec_namecheck); 4984 break; 4985 4986 case DATASET_NAME: 4987 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0) 4988 error = EINVAL; 4989 if (zfs_ioc_vec[vec].zvec_pool_check) 4990 error = pool_status_check(zc->zc_name, 4991 zfs_ioc_vec[vec].zvec_namecheck); 4992 break; 4993 4994 case NO_NAME: 4995 break; 4996 } 4997 } 4998 4999 if (error == 0) 5000 error = zfs_ioc_vec[vec].zvec_func(zc); 5001 5002 if (error == 0) { 5003 if (zfs_ioc_vec[vec].zvec_his_log) 5004 zfs_log_history(zc); 5005 } 5006 5007 if (cflag != ZFS_CMD_COMPAT_NONE) { 5008 zfs_ioctl_compat_post(zc, ZFS_IOC(cmd), cflag); 5009 zfs_cmd_compat_put(zc, addr, cflag); 5010 kmem_free(zc, sizeof(zfs_cmd_t)); 5011 } 5012 5013 return (error); 5014} 5015 5016#ifdef sun 5017static int 5018zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 5019{ 5020 if (cmd != DDI_ATTACH) 5021 return (DDI_FAILURE); 5022 5023 if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0, 5024 DDI_PSEUDO, 0) == DDI_FAILURE) 5025 return (DDI_FAILURE); 5026 5027 zfs_dip = dip; 5028 5029 ddi_report_dev(dip); 5030 5031 return (DDI_SUCCESS); 5032} 5033 5034static int 5035zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 5036{ 5037 if (spa_busy() || zfs_busy() || zvol_busy()) 5038 return (DDI_FAILURE); 5039 5040 if (cmd != DDI_DETACH) 5041 return (DDI_FAILURE); 5042 5043 zfs_dip = NULL; 5044 5045 ddi_prop_remove_all(dip); 5046 ddi_remove_minor_node(dip, NULL); 5047 5048 return (DDI_SUCCESS); 5049} 5050 5051/*ARGSUSED*/ 5052static int 5053zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 5054{ 5055 switch (infocmd) { 5056 case DDI_INFO_DEVT2DEVINFO: 5057 *result = zfs_dip; 5058 return (DDI_SUCCESS); 5059 5060 case DDI_INFO_DEVT2INSTANCE: 5061 *result = (void *)0; 5062 return (DDI_SUCCESS); 5063 } 5064 5065 return (DDI_FAILURE); 5066} 5067#endif /* sun */ 5068 5069/* 5070 * OK, so this is a little weird. 5071 * 5072 * /dev/zfs is the control node, i.e. minor 0. 5073 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0. 5074 * 5075 * /dev/zfs has basically nothing to do except serve up ioctls, 5076 * so most of the standard driver entry points are in zvol.c. 5077 */ 5078#ifdef sun 5079static struct cb_ops zfs_cb_ops = { 5080 zfsdev_open, /* open */ 5081 zfsdev_close, /* close */ 5082 zvol_strategy, /* strategy */ 5083 nodev, /* print */ 5084 zvol_dump, /* dump */ 5085 zvol_read, /* read */ 5086 zvol_write, /* write */ 5087 zfsdev_ioctl, /* ioctl */ 5088 nodev, /* devmap */ 5089 nodev, /* mmap */ 5090 nodev, /* segmap */ 5091 nochpoll, /* poll */ 5092 ddi_prop_op, /* prop_op */ 5093 NULL, /* streamtab */ 5094 D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */ 5095 CB_REV, /* version */ 5096 nodev, /* async read */ 5097 nodev, /* async write */ 5098}; 5099 5100static struct dev_ops zfs_dev_ops = { 5101 DEVO_REV, /* version */ 5102 0, /* refcnt */ 5103 zfs_info, /* info */ 5104 nulldev, /* identify */ 5105 nulldev, /* probe */ 5106 zfs_attach, /* attach */ 5107 zfs_detach, /* detach */ 5108 nodev, /* reset */ 5109 &zfs_cb_ops, /* driver operations */ 5110 NULL, /* no bus operations */ 5111 NULL, /* power */ 5112 ddi_quiesce_not_needed, /* quiesce */ 5113}; 5114 5115static struct modldrv zfs_modldrv = { 5116 &mod_driverops, 5117 "ZFS storage pool", 5118 &zfs_dev_ops 5119}; 5120 5121static struct modlinkage modlinkage = { 5122 MODREV_1, 5123 (void *)&zfs_modlfs, 5124 (void *)&zfs_modldrv, 5125 NULL 5126}; 5127#endif /* sun */ 5128 5129static struct cdevsw zfs_cdevsw = { 5130 .d_version = D_VERSION, 5131 .d_open = zfsdev_open, 5132 .d_ioctl = zfsdev_ioctl, 5133 .d_name = ZFS_DEV_NAME 5134}; 5135 5136static void 5137zfsdev_init(void) 5138{ 5139 zfsdev = make_dev(&zfs_cdevsw, 0x0, UID_ROOT, GID_OPERATOR, 0666, 5140 ZFS_DEV_NAME); 5141} 5142 5143static void 5144zfsdev_fini(void) 5145{ 5146 if (zfsdev != NULL) 5147 destroy_dev(zfsdev); 5148} 5149 5150static struct root_hold_token *zfs_root_token; 5151struct proc *zfsproc; 5152 5153uint_t zfs_fsyncer_key; 5154extern uint_t rrw_tsd_key; 5155 5156#ifdef sun 5157int 5158_init(void) 5159{ 5160 int error; 5161 5162 spa_init(FREAD | FWRITE); 5163 zfs_init(); 5164 zvol_init(); 5165 5166 if ((error = mod_install(&modlinkage)) != 0) { 5167 zvol_fini(); 5168 zfs_fini(); 5169 spa_fini(); 5170 return (error); 5171 } 5172 5173 tsd_create(&zfs_fsyncer_key, NULL); 5174 tsd_create(&rrw_tsd_key, NULL); 5175 5176 error = ldi_ident_from_mod(&modlinkage, &zfs_li); 5177 ASSERT(error == 0); 5178 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL); 5179 5180 return (0); 5181} 5182 5183int 5184_fini(void) 5185{ 5186 int error; 5187 5188 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled) 5189 return (EBUSY); 5190 5191 if ((error = mod_remove(&modlinkage)) != 0) 5192 return (error); 5193 5194 zvol_fini(); 5195 zfs_fini(); 5196 spa_fini(); 5197 if (zfs_nfsshare_inited) 5198 (void) ddi_modclose(nfs_mod); 5199 if (zfs_smbshare_inited) 5200 (void) ddi_modclose(smbsrv_mod); 5201 if (zfs_nfsshare_inited || zfs_smbshare_inited) 5202 (void) ddi_modclose(sharefs_mod); 5203 5204 tsd_destroy(&zfs_fsyncer_key); 5205 ldi_ident_release(zfs_li); 5206 zfs_li = NULL; 5207 mutex_destroy(&zfs_share_lock); 5208 5209 return (error); 5210} 5211 5212int 5213_info(struct modinfo *modinfop) 5214{ 5215 return (mod_info(&modlinkage, modinfop)); 5216} 5217#endif /* sun */ 5218 5219static int 5220zfs_modevent(module_t mod, int type, void *unused __unused) 5221{ 5222 int error = 0; 5223 5224 switch (type) { 5225 case MOD_LOAD: 5226 zfs_root_token = root_mount_hold("ZFS"); 5227 5228 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL); 5229 5230 spa_init(FREAD | FWRITE); 5231 zfs_init(); 5232 zvol_init(); 5233 5234 tsd_create(&zfs_fsyncer_key, NULL); 5235 tsd_create(&rrw_tsd_key, NULL); 5236 5237 printf("ZFS storage pool version " SPA_VERSION_STRING "\n"); 5238 root_mount_rel(zfs_root_token); 5239 5240 zfsdev_init(); 5241 break; 5242 case MOD_UNLOAD: 5243 if (spa_busy() || zfs_busy() || zvol_busy() || 5244 zio_injection_enabled) { 5245 error = EBUSY; 5246 break; 5247 } 5248 5249 zfsdev_fini(); 5250 zvol_fini(); 5251 zfs_fini(); 5252 spa_fini(); 5253 5254 tsd_destroy(&zfs_fsyncer_key); 5255 tsd_destroy(&rrw_tsd_key); 5256 5257 mutex_destroy(&zfs_share_lock); 5258 break; 5259 default: 5260 error = EOPNOTSUPP; 5261 break; 5262 } 5263 return (error); 5264} 5265 5266static moduledata_t zfs_mod = { 5267 "zfsctrl", 5268 zfs_modevent, 5269 0 5270}; 5271DECLARE_MODULE(zfsctrl, zfs_mod, SI_SUB_VFS, SI_ORDER_ANY); 5272MODULE_DEPEND(zfsctrl, opensolaris, 1, 1, 1); 5273MODULE_DEPEND(zfsctrl, krpc, 1, 1, 1); 5274