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