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 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 #include <sys/types.h> 29 #include <sys/param.h> 30 #include <sys/errno.h> 31 #include <sys/uio.h> 32 #include <sys/buf.h> 33 #include <sys/modctl.h> 34 #include <sys/open.h> 35 #include <sys/file.h> 36 #include <sys/kmem.h> 37 #include <sys/conf.h> 38 #include <sys/cmn_err.h> 39 #include <sys/stat.h> 40 #include <sys/zfs_ioctl.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/vdev_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/zvol.h> 64 #include <sharefs/share.h> 65 #include <sys/zfs_znode.h> 66 67 #include "zfs_namecheck.h" 68 #include "zfs_prop.h" 69 #include "zfs_deleg.h" 70 71 extern struct modlfs zfs_modlfs; 72 73 extern void zfs_init(void); 74 extern void zfs_fini(void); 75 76 ldi_ident_t zfs_li = NULL; 77 dev_info_t *zfs_dip; 78 79 typedef int zfs_ioc_func_t(zfs_cmd_t *); 80 typedef int zfs_secpolicy_func_t(zfs_cmd_t *, cred_t *); 81 82 typedef struct zfs_ioc_vec { 83 zfs_ioc_func_t *zvec_func; 84 zfs_secpolicy_func_t *zvec_secpolicy; 85 enum { 86 NO_NAME, 87 POOL_NAME, 88 DATASET_NAME 89 } zvec_namecheck; 90 boolean_t zvec_his_log; 91 } zfs_ioc_vec_t; 92 93 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */ 94 void 95 __dprintf(const char *file, const char *func, int line, const char *fmt, ...) 96 { 97 const char *newfile; 98 char buf[256]; 99 va_list adx; 100 101 /* 102 * Get rid of annoying "../common/" prefix to filename. 103 */ 104 newfile = strrchr(file, '/'); 105 if (newfile != NULL) { 106 newfile = newfile + 1; /* Get rid of leading / */ 107 } else { 108 newfile = file; 109 } 110 111 va_start(adx, fmt); 112 (void) vsnprintf(buf, sizeof (buf), fmt, adx); 113 va_end(adx); 114 115 /* 116 * To get this data, use the zfs-dprintf probe as so: 117 * dtrace -q -n 'zfs-dprintf \ 118 * /stringof(arg0) == "dbuf.c"/ \ 119 * {printf("%s: %s", stringof(arg1), stringof(arg3))}' 120 * arg0 = file name 121 * arg1 = function name 122 * arg2 = line number 123 * arg3 = message 124 */ 125 DTRACE_PROBE4(zfs__dprintf, 126 char *, newfile, char *, func, int, line, char *, buf); 127 } 128 129 static void 130 zfs_log_history(zfs_cmd_t *zc) 131 { 132 spa_t *spa; 133 char poolname[MAXNAMELEN]; 134 char *buf, *cp; 135 136 if (zc->zc_history == NULL) 137 return; 138 139 if (zc->zc_history_offset != LOG_CMD_POOL_CREATE && 140 zc->zc_history_offset != LOG_CMD_NORMAL) 141 return; 142 143 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP); 144 if (copyinstr((void *)(uintptr_t)zc->zc_history, 145 buf, HIS_MAX_RECORD_LEN, NULL) != 0) { 146 kmem_free(buf, HIS_MAX_RECORD_LEN); 147 return; 148 } 149 150 buf[HIS_MAX_RECORD_LEN -1] = '\0'; 151 152 (void) strlcpy(poolname, zc->zc_name, sizeof (poolname)); 153 cp = strpbrk(poolname, "/@"); 154 if (cp != NULL) 155 *cp = '\0'; 156 157 if (spa_open(poolname, &spa, FTAG) != 0) { 158 kmem_free(buf, HIS_MAX_RECORD_LEN); 159 return; 160 } 161 162 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY) 163 (void) spa_history_log(spa, buf, zc->zc_history_offset); 164 165 spa_close(spa, FTAG); 166 kmem_free(buf, HIS_MAX_RECORD_LEN); 167 } 168 169 /* 170 * Policy for top-level read operations (list pools). Requires no privileges, 171 * and can be used in the local zone, as there is no associated dataset. 172 */ 173 /* ARGSUSED */ 174 static int 175 zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr) 176 { 177 return (0); 178 } 179 180 /* 181 * Policy for dataset read operations (list children, get statistics). Requires 182 * no privileges, but must be visible in the local zone. 183 */ 184 /* ARGSUSED */ 185 static int 186 zfs_secpolicy_read(zfs_cmd_t *zc, cred_t *cr) 187 { 188 if (INGLOBALZONE(curproc) || 189 zone_dataset_visible(zc->zc_name, NULL)) 190 return (0); 191 192 return (ENOENT); 193 } 194 195 static int 196 zfs_dozonecheck(const char *dataset, cred_t *cr) 197 { 198 uint64_t zoned; 199 int writable = 1; 200 201 /* 202 * The dataset must be visible by this zone -- check this first 203 * so they don't see EPERM on something they shouldn't know about. 204 */ 205 if (!INGLOBALZONE(curproc) && 206 !zone_dataset_visible(dataset, &writable)) 207 return (ENOENT); 208 209 if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL)) 210 return (ENOENT); 211 212 if (INGLOBALZONE(curproc)) { 213 /* 214 * If the fs is zoned, only root can access it from the 215 * global zone. 216 */ 217 if (secpolicy_zfs(cr) && zoned) 218 return (EPERM); 219 } else { 220 /* 221 * If we are in a local zone, the 'zoned' property must be set. 222 */ 223 if (!zoned) 224 return (EPERM); 225 226 /* must be writable by this zone */ 227 if (!writable) 228 return (EPERM); 229 } 230 return (0); 231 } 232 233 int 234 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr) 235 { 236 int error; 237 238 error = zfs_dozonecheck(name, cr); 239 if (error == 0) { 240 error = secpolicy_zfs(cr); 241 if (error) { 242 error = dsl_deleg_access(name, perm, cr); 243 } 244 } 245 return (error); 246 } 247 248 static int 249 zfs_secpolicy_setprop(const char *name, zfs_prop_t prop, cred_t *cr) 250 { 251 int error = 0; 252 253 /* 254 * Check permissions for special properties. 255 */ 256 switch (prop) { 257 case ZFS_PROP_ZONED: 258 /* 259 * Disallow setting of 'zoned' from within a local zone. 260 */ 261 if (!INGLOBALZONE(curproc)) 262 return (EPERM); 263 break; 264 265 case ZFS_PROP_QUOTA: 266 if (error = 267 zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_QUOTA, cr)) 268 return (error); 269 270 if (!INGLOBALZONE(curproc)) { 271 uint64_t zoned; 272 char setpoint[MAXNAMELEN]; 273 int dslen; 274 /* 275 * Unprivileged users are allowed to modify the 276 * quota on things *under* (ie. contained by) 277 * the thing they own. 278 */ 279 if (dsl_prop_get_integer(name, "zoned", &zoned, 280 setpoint)) 281 return (EPERM); 282 if (!zoned) /* this shouldn't happen */ 283 return (EPERM); 284 dslen = strlen(name); 285 if (dslen <= strlen(setpoint)) 286 return (EPERM); 287 } 288 default: 289 error = zfs_secpolicy_write_perms(name, 290 zfs_prop_perm(prop), cr); 291 } 292 293 return (error); 294 } 295 296 int 297 zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr) 298 { 299 int error; 300 301 error = zfs_dozonecheck(zc->zc_name, cr); 302 if (error) 303 return (error); 304 305 /* 306 * permission to set permissions will be evaluated later in 307 * dsl_deleg_can_allow() 308 */ 309 return (0); 310 } 311 312 int 313 zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr) 314 { 315 int error; 316 error = zfs_secpolicy_write_perms(zc->zc_name, 317 ZFS_DELEG_PERM_ROLLBACK, cr); 318 if (error == 0) 319 error = zfs_secpolicy_write_perms(zc->zc_name, 320 ZFS_DELEG_PERM_MOUNT, cr); 321 return (error); 322 } 323 324 int 325 zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr) 326 { 327 return (zfs_secpolicy_write_perms(zc->zc_name, 328 ZFS_DELEG_PERM_SEND, cr)); 329 } 330 331 int 332 zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr) 333 { 334 if (!INGLOBALZONE(curproc)) 335 return (EPERM); 336 337 if (secpolicy_nfs(CRED()) == 0) { 338 return (0); 339 } else { 340 vnode_t *vp; 341 int error; 342 343 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE, 344 NO_FOLLOW, NULL, &vp)) != 0) 345 return (error); 346 347 /* Now make sure mntpnt and dataset are ZFS */ 348 349 if (vp->v_vfsp->vfs_fstype != zfsfstype || 350 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource), 351 zc->zc_name) != 0)) { 352 VN_RELE(vp); 353 return (EPERM); 354 } 355 356 VN_RELE(vp); 357 return (dsl_deleg_access(zc->zc_name, 358 ZFS_DELEG_PERM_SHARE, cr)); 359 } 360 } 361 362 static int 363 zfs_get_parent(const char *datasetname, char *parent, int parentsize) 364 { 365 char *cp; 366 367 /* 368 * Remove the @bla or /bla from the end of the name to get the parent. 369 */ 370 (void) strncpy(parent, datasetname, parentsize); 371 cp = strrchr(parent, '@'); 372 if (cp != NULL) { 373 cp[0] = '\0'; 374 } else { 375 cp = strrchr(parent, '/'); 376 if (cp == NULL) 377 return (ENOENT); 378 cp[0] = '\0'; 379 } 380 381 return (0); 382 } 383 384 int 385 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr) 386 { 387 int error; 388 389 if ((error = zfs_secpolicy_write_perms(name, 390 ZFS_DELEG_PERM_MOUNT, cr)) != 0) 391 return (error); 392 393 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr)); 394 } 395 396 static int 397 zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr) 398 { 399 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr)); 400 } 401 402 /* 403 * Must have sys_config privilege to check the iscsi permission 404 */ 405 /* ARGSUSED */ 406 static int 407 zfs_secpolicy_iscsi(zfs_cmd_t *zc, cred_t *cr) 408 { 409 return (secpolicy_zfs(cr)); 410 } 411 412 int 413 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr) 414 { 415 char parentname[MAXNAMELEN]; 416 int error; 417 418 if ((error = zfs_secpolicy_write_perms(from, 419 ZFS_DELEG_PERM_RENAME, cr)) != 0) 420 return (error); 421 422 if ((error = zfs_secpolicy_write_perms(from, 423 ZFS_DELEG_PERM_MOUNT, cr)) != 0) 424 return (error); 425 426 if ((error = zfs_get_parent(to, parentname, 427 sizeof (parentname))) != 0) 428 return (error); 429 430 if ((error = zfs_secpolicy_write_perms(parentname, 431 ZFS_DELEG_PERM_CREATE, cr)) != 0) 432 return (error); 433 434 if ((error = zfs_secpolicy_write_perms(parentname, 435 ZFS_DELEG_PERM_MOUNT, cr)) != 0) 436 return (error); 437 438 return (error); 439 } 440 441 static int 442 zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr) 443 { 444 return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr)); 445 } 446 447 static int 448 zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr) 449 { 450 char parentname[MAXNAMELEN]; 451 objset_t *clone; 452 int error; 453 454 error = zfs_secpolicy_write_perms(zc->zc_name, 455 ZFS_DELEG_PERM_PROMOTE, cr); 456 if (error) 457 return (error); 458 459 error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, 460 DS_MODE_STANDARD | DS_MODE_READONLY, &clone); 461 462 if (error == 0) { 463 dsl_dataset_t *pclone = NULL; 464 dsl_dir_t *dd; 465 dd = clone->os->os_dsl_dataset->ds_dir; 466 467 rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER); 468 error = dsl_dataset_open_obj(dd->dd_pool, 469 dd->dd_phys->dd_clone_parent_obj, NULL, 470 DS_MODE_NONE, FTAG, &pclone); 471 rw_exit(&dd->dd_pool->dp_config_rwlock); 472 if (error) { 473 dmu_objset_close(clone); 474 return (error); 475 } 476 477 error = zfs_secpolicy_write_perms(zc->zc_name, 478 ZFS_DELEG_PERM_MOUNT, cr); 479 480 dsl_dataset_name(pclone, parentname); 481 dmu_objset_close(clone); 482 dsl_dataset_close(pclone, DS_MODE_NONE, FTAG); 483 if (error == 0) 484 error = zfs_secpolicy_write_perms(parentname, 485 ZFS_DELEG_PERM_PROMOTE, cr); 486 } 487 return (error); 488 } 489 490 static int 491 zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr) 492 { 493 int error; 494 495 if ((error = zfs_secpolicy_write_perms(zc->zc_name, 496 ZFS_DELEG_PERM_RECEIVE, cr)) != 0) 497 return (error); 498 499 if ((error = zfs_secpolicy_write_perms(zc->zc_name, 500 ZFS_DELEG_PERM_MOUNT, cr)) != 0) 501 return (error); 502 503 return (zfs_secpolicy_write_perms(zc->zc_name, 504 ZFS_DELEG_PERM_CREATE, cr)); 505 } 506 507 int 508 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr) 509 { 510 int error; 511 512 if ((error = zfs_secpolicy_write_perms(name, 513 ZFS_DELEG_PERM_SNAPSHOT, cr)) != 0) 514 return (error); 515 516 error = zfs_secpolicy_write_perms(name, 517 ZFS_DELEG_PERM_MOUNT, cr); 518 519 return (error); 520 } 521 522 static int 523 zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr) 524 { 525 526 return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr)); 527 } 528 529 static int 530 zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr) 531 { 532 char parentname[MAXNAMELEN]; 533 int error; 534 535 if ((error = zfs_get_parent(zc->zc_name, parentname, 536 sizeof (parentname))) != 0) 537 return (error); 538 539 if (zc->zc_value[0] != '\0') { 540 if ((error = zfs_secpolicy_write_perms(zc->zc_value, 541 ZFS_DELEG_PERM_CLONE, cr)) != 0) 542 return (error); 543 } 544 545 if ((error = zfs_secpolicy_write_perms(parentname, 546 ZFS_DELEG_PERM_CREATE, cr)) != 0) 547 return (error); 548 549 error = zfs_secpolicy_write_perms(parentname, 550 ZFS_DELEG_PERM_MOUNT, cr); 551 552 return (error); 553 } 554 555 static int 556 zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr) 557 { 558 int error; 559 560 error = secpolicy_fs_unmount(cr, NULL); 561 if (error) { 562 error = dsl_deleg_access(zc->zc_name, ZFS_DELEG_PERM_MOUNT, cr); 563 } 564 return (error); 565 } 566 567 /* 568 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires 569 * SYS_CONFIG privilege, which is not available in a local zone. 570 */ 571 /* ARGSUSED */ 572 static int 573 zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr) 574 { 575 if (secpolicy_sys_config(cr, B_FALSE) != 0) 576 return (EPERM); 577 578 return (0); 579 } 580 581 /* 582 * Just like zfs_secpolicy_config, except that we will check for 583 * mount permission on the dataset for permission to create/remove 584 * the minor nodes. 585 */ 586 static int 587 zfs_secpolicy_minor(zfs_cmd_t *zc, cred_t *cr) 588 { 589 if (secpolicy_sys_config(cr, B_FALSE) != 0) { 590 return (dsl_deleg_access(zc->zc_name, 591 ZFS_DELEG_PERM_MOUNT, cr)); 592 } 593 594 return (0); 595 } 596 597 /* 598 * Policy for fault injection. Requires all privileges. 599 */ 600 /* ARGSUSED */ 601 static int 602 zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr) 603 { 604 return (secpolicy_zinject(cr)); 605 } 606 607 /* 608 * Returns the nvlist as specified by the user in the zfs_cmd_t. 609 */ 610 static int 611 get_nvlist(zfs_cmd_t *zc, nvlist_t **nvp) 612 { 613 char *packed; 614 size_t size; 615 int error; 616 nvlist_t *config = NULL; 617 618 /* 619 * Read in and unpack the user-supplied nvlist. 620 */ 621 if ((size = zc->zc_nvlist_src_size) == 0) 622 return (EINVAL); 623 624 packed = kmem_alloc(size, KM_SLEEP); 625 626 if ((error = xcopyin((void *)(uintptr_t)zc->zc_nvlist_src, packed, 627 size)) != 0) { 628 kmem_free(packed, size); 629 return (error); 630 } 631 632 if ((error = nvlist_unpack(packed, size, &config, 0)) != 0) { 633 kmem_free(packed, size); 634 return (error); 635 } 636 637 kmem_free(packed, size); 638 639 *nvp = config; 640 return (0); 641 } 642 643 static int 644 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl) 645 { 646 char *packed = NULL; 647 size_t size; 648 int error; 649 650 VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0); 651 652 if (size > zc->zc_nvlist_dst_size) { 653 error = ENOMEM; 654 } else { 655 VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE, 656 KM_SLEEP) == 0); 657 error = xcopyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst, 658 size); 659 kmem_free(packed, size); 660 } 661 662 zc->zc_nvlist_dst_size = size; 663 return (error); 664 } 665 666 static int 667 zfs_ioc_pool_create(zfs_cmd_t *zc) 668 { 669 int error; 670 nvlist_t *config; 671 672 if ((error = get_nvlist(zc, &config)) != 0) 673 return (error); 674 675 error = spa_create(zc->zc_name, config, zc->zc_value[0] == '\0' ? 676 NULL : zc->zc_value); 677 678 nvlist_free(config); 679 680 return (error); 681 } 682 683 static int 684 zfs_ioc_pool_destroy(zfs_cmd_t *zc) 685 { 686 int error; 687 zfs_log_history(zc); 688 error = spa_destroy(zc->zc_name); 689 return (error); 690 } 691 692 static int 693 zfs_ioc_pool_import(zfs_cmd_t *zc) 694 { 695 int error; 696 nvlist_t *config; 697 uint64_t guid; 698 699 if ((error = get_nvlist(zc, &config)) != 0) 700 return (error); 701 702 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 || 703 guid != zc->zc_guid) 704 error = EINVAL; 705 else 706 error = spa_import(zc->zc_name, config, 707 zc->zc_value[0] == '\0' ? NULL : zc->zc_value); 708 709 nvlist_free(config); 710 711 return (error); 712 } 713 714 static int 715 zfs_ioc_pool_export(zfs_cmd_t *zc) 716 { 717 int error; 718 zfs_log_history(zc); 719 error = spa_export(zc->zc_name, NULL); 720 return (error); 721 } 722 723 static int 724 zfs_ioc_pool_configs(zfs_cmd_t *zc) 725 { 726 nvlist_t *configs; 727 int error; 728 729 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL) 730 return (EEXIST); 731 732 error = put_nvlist(zc, configs); 733 734 nvlist_free(configs); 735 736 return (error); 737 } 738 739 static int 740 zfs_ioc_pool_stats(zfs_cmd_t *zc) 741 { 742 nvlist_t *config; 743 int error; 744 int ret = 0; 745 746 error = spa_get_stats(zc->zc_name, &config, zc->zc_value, 747 sizeof (zc->zc_value)); 748 749 if (config != NULL) { 750 ret = put_nvlist(zc, config); 751 nvlist_free(config); 752 753 /* 754 * The config may be present even if 'error' is non-zero. 755 * In this case we return success, and preserve the real errno 756 * in 'zc_cookie'. 757 */ 758 zc->zc_cookie = error; 759 } else { 760 ret = error; 761 } 762 763 return (ret); 764 } 765 766 /* 767 * Try to import the given pool, returning pool stats as appropriate so that 768 * user land knows which devices are available and overall pool health. 769 */ 770 static int 771 zfs_ioc_pool_tryimport(zfs_cmd_t *zc) 772 { 773 nvlist_t *tryconfig, *config; 774 int error; 775 776 if ((error = get_nvlist(zc, &tryconfig)) != 0) 777 return (error); 778 779 config = spa_tryimport(tryconfig); 780 781 nvlist_free(tryconfig); 782 783 if (config == NULL) 784 return (EINVAL); 785 786 error = put_nvlist(zc, config); 787 nvlist_free(config); 788 789 return (error); 790 } 791 792 static int 793 zfs_ioc_pool_scrub(zfs_cmd_t *zc) 794 { 795 spa_t *spa; 796 int error; 797 798 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 799 return (error); 800 801 spa_config_enter(spa, RW_READER, FTAG); 802 error = spa_scrub(spa, zc->zc_cookie, B_FALSE); 803 spa_config_exit(spa, FTAG); 804 805 spa_close(spa, FTAG); 806 807 return (error); 808 } 809 810 static int 811 zfs_ioc_pool_freeze(zfs_cmd_t *zc) 812 { 813 spa_t *spa; 814 int error; 815 816 error = spa_open(zc->zc_name, &spa, FTAG); 817 if (error == 0) { 818 spa_freeze(spa); 819 spa_close(spa, FTAG); 820 } 821 return (error); 822 } 823 824 static int 825 zfs_ioc_pool_upgrade(zfs_cmd_t *zc) 826 { 827 spa_t *spa; 828 int error; 829 830 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 831 return (error); 832 833 spa_upgrade(spa); 834 spa_close(spa, FTAG); 835 836 return (error); 837 } 838 839 static int 840 zfs_ioc_pool_get_history(zfs_cmd_t *zc) 841 { 842 spa_t *spa; 843 char *hist_buf; 844 uint64_t size; 845 int error; 846 847 if ((size = zc->zc_history_len) == 0) 848 return (EINVAL); 849 850 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 851 return (error); 852 853 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) { 854 spa_close(spa, FTAG); 855 return (ENOTSUP); 856 } 857 858 hist_buf = kmem_alloc(size, KM_SLEEP); 859 if ((error = spa_history_get(spa, &zc->zc_history_offset, 860 &zc->zc_history_len, hist_buf)) == 0) { 861 error = xcopyout(hist_buf, 862 (char *)(uintptr_t)zc->zc_history, 863 zc->zc_history_len); 864 } 865 866 spa_close(spa, FTAG); 867 kmem_free(hist_buf, size); 868 return (error); 869 } 870 871 static int 872 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc) 873 { 874 int error; 875 876 if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value)) 877 return (error); 878 879 return (0); 880 } 881 882 static int 883 zfs_ioc_obj_to_path(zfs_cmd_t *zc) 884 { 885 objset_t *osp; 886 int error; 887 888 if ((error = dmu_objset_open(zc->zc_name, DMU_OST_ZFS, 889 DS_MODE_NONE | DS_MODE_READONLY, &osp)) != 0) 890 return (error); 891 892 error = zfs_obj_to_path(osp, zc->zc_obj, zc->zc_value, 893 sizeof (zc->zc_value)); 894 dmu_objset_close(osp); 895 896 return (error); 897 } 898 899 static int 900 zfs_ioc_vdev_add(zfs_cmd_t *zc) 901 { 902 spa_t *spa; 903 int error; 904 nvlist_t *config; 905 906 error = spa_open(zc->zc_name, &spa, FTAG); 907 if (error != 0) 908 return (error); 909 910 /* 911 * A root pool with concatenated devices is not supported. 912 * Thus, can not add a device to a root pool with one device. 913 */ 914 if (spa->spa_root_vdev->vdev_children == 1 && spa->spa_bootfs != 0) { 915 spa_close(spa, FTAG); 916 return (EDOM); 917 } 918 919 if ((error = get_nvlist(zc, &config)) == 0) { 920 error = spa_vdev_add(spa, config); 921 nvlist_free(config); 922 } 923 spa_close(spa, FTAG); 924 return (error); 925 } 926 927 static int 928 zfs_ioc_vdev_remove(zfs_cmd_t *zc) 929 { 930 spa_t *spa; 931 int error; 932 933 error = spa_open(zc->zc_name, &spa, FTAG); 934 if (error != 0) 935 return (error); 936 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE); 937 spa_close(spa, FTAG); 938 return (error); 939 } 940 941 static int 942 zfs_ioc_vdev_set_state(zfs_cmd_t *zc) 943 { 944 spa_t *spa; 945 int error; 946 vdev_state_t newstate = VDEV_STATE_UNKNOWN; 947 948 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 949 return (error); 950 switch (zc->zc_cookie) { 951 case VDEV_STATE_ONLINE: 952 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate); 953 break; 954 955 case VDEV_STATE_OFFLINE: 956 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj); 957 break; 958 959 case VDEV_STATE_FAULTED: 960 error = vdev_fault(spa, zc->zc_guid); 961 break; 962 963 case VDEV_STATE_DEGRADED: 964 error = vdev_degrade(spa, zc->zc_guid); 965 break; 966 967 default: 968 error = EINVAL; 969 } 970 zc->zc_cookie = newstate; 971 spa_close(spa, FTAG); 972 return (error); 973 } 974 975 static int 976 zfs_ioc_vdev_attach(zfs_cmd_t *zc) 977 { 978 spa_t *spa; 979 int replacing = zc->zc_cookie; 980 nvlist_t *config; 981 int error; 982 983 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 984 return (error); 985 986 if ((error = get_nvlist(zc, &config)) == 0) { 987 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing); 988 nvlist_free(config); 989 } 990 991 spa_close(spa, FTAG); 992 return (error); 993 } 994 995 static int 996 zfs_ioc_vdev_detach(zfs_cmd_t *zc) 997 { 998 spa_t *spa; 999 int error; 1000 1001 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1002 return (error); 1003 1004 error = spa_vdev_detach(spa, zc->zc_guid, B_FALSE); 1005 1006 spa_close(spa, FTAG); 1007 return (error); 1008 } 1009 1010 static int 1011 zfs_ioc_vdev_setpath(zfs_cmd_t *zc) 1012 { 1013 spa_t *spa; 1014 char *path = zc->zc_value; 1015 uint64_t guid = zc->zc_guid; 1016 int error; 1017 1018 error = spa_open(zc->zc_name, &spa, FTAG); 1019 if (error != 0) 1020 return (error); 1021 1022 error = spa_vdev_setpath(spa, guid, path); 1023 spa_close(spa, FTAG); 1024 return (error); 1025 } 1026 1027 static int 1028 zfs_ioc_objset_stats(zfs_cmd_t *zc) 1029 { 1030 objset_t *os = NULL; 1031 int error; 1032 nvlist_t *nv; 1033 1034 retry: 1035 error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, 1036 DS_MODE_STANDARD | DS_MODE_READONLY, &os); 1037 if (error != 0) { 1038 /* 1039 * This is ugly: dmu_objset_open() can return EBUSY if 1040 * the objset is held exclusively. Fortunately this hold is 1041 * only for a short while, so we retry here. 1042 * This avoids user code having to handle EBUSY, 1043 * for example for a "zfs list". 1044 */ 1045 if (error == EBUSY) { 1046 delay(1); 1047 goto retry; 1048 } 1049 return (error); 1050 } 1051 1052 dmu_objset_fast_stat(os, &zc->zc_objset_stats); 1053 1054 if (zc->zc_nvlist_dst != 0 && 1055 (error = dsl_prop_get_all(os, &nv)) == 0) { 1056 dmu_objset_stats(os, nv); 1057 /* 1058 * NB: {zpl,zvol}_get_stats() will read the objset contents, 1059 * which we aren't supposed to do with a 1060 * DS_MODE_STANDARD open, because it could be 1061 * inconsistent. So this is a bit of a workaround... 1062 */ 1063 if (!zc->zc_objset_stats.dds_inconsistent) { 1064 if (dmu_objset_type(os) == DMU_OST_ZVOL) 1065 VERIFY(zvol_get_stats(os, nv) == 0); 1066 else if (dmu_objset_type(os) == DMU_OST_ZFS) 1067 (void) zfs_get_stats(os, nv); 1068 } 1069 error = put_nvlist(zc, nv); 1070 nvlist_free(nv); 1071 } 1072 1073 spa_altroot(dmu_objset_spa(os), zc->zc_value, sizeof (zc->zc_value)); 1074 1075 dmu_objset_close(os); 1076 return (error); 1077 } 1078 1079 static int 1080 zfs_ioc_dataset_list_next(zfs_cmd_t *zc) 1081 { 1082 objset_t *os; 1083 int error; 1084 char *p; 1085 1086 retry: 1087 error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, 1088 DS_MODE_STANDARD | DS_MODE_READONLY, &os); 1089 if (error != 0) { 1090 /* 1091 * This is ugly: dmu_objset_open() can return EBUSY if 1092 * the objset is held exclusively. Fortunately this hold is 1093 * only for a short while, so we retry here. 1094 * This avoids user code having to handle EBUSY, 1095 * for example for a "zfs list". 1096 */ 1097 if (error == EBUSY) { 1098 delay(1); 1099 goto retry; 1100 } 1101 if (error == ENOENT) 1102 error = ESRCH; 1103 return (error); 1104 } 1105 1106 p = strrchr(zc->zc_name, '/'); 1107 if (p == NULL || p[1] != '\0') 1108 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name)); 1109 p = zc->zc_name + strlen(zc->zc_name); 1110 1111 do { 1112 error = dmu_dir_list_next(os, 1113 sizeof (zc->zc_name) - (p - zc->zc_name), p, 1114 NULL, &zc->zc_cookie); 1115 if (error == ENOENT) 1116 error = ESRCH; 1117 } while (error == 0 && !INGLOBALZONE(curproc) && 1118 !zone_dataset_visible(zc->zc_name, NULL)); 1119 1120 /* 1121 * If it's a hidden dataset (ie. with a '$' in its name), don't 1122 * try to get stats for it. Userland will skip over it. 1123 */ 1124 if (error == 0 && strchr(zc->zc_name, '$') == NULL) 1125 error = zfs_ioc_objset_stats(zc); /* fill in the stats */ 1126 1127 dmu_objset_close(os); 1128 return (error); 1129 } 1130 1131 static int 1132 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc) 1133 { 1134 objset_t *os; 1135 int error; 1136 1137 retry: 1138 error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, 1139 DS_MODE_STANDARD | DS_MODE_READONLY, &os); 1140 if (error != 0) { 1141 /* 1142 * This is ugly: dmu_objset_open() can return EBUSY if 1143 * the objset is held exclusively. Fortunately this hold is 1144 * only for a short while, so we retry here. 1145 * This avoids user code having to handle EBUSY, 1146 * for example for a "zfs list". 1147 */ 1148 if (error == EBUSY) { 1149 delay(1); 1150 goto retry; 1151 } 1152 if (error == ENOENT) 1153 error = ESRCH; 1154 return (error); 1155 } 1156 1157 /* 1158 * A dataset name of maximum length cannot have any snapshots, 1159 * so exit immediately. 1160 */ 1161 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) { 1162 dmu_objset_close(os); 1163 return (ESRCH); 1164 } 1165 1166 error = dmu_snapshot_list_next(os, 1167 sizeof (zc->zc_name) - strlen(zc->zc_name), 1168 zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie); 1169 if (error == ENOENT) 1170 error = ESRCH; 1171 1172 if (error == 0) 1173 error = zfs_ioc_objset_stats(zc); /* fill in the stats */ 1174 1175 dmu_objset_close(os); 1176 return (error); 1177 } 1178 1179 static int 1180 zfs_set_prop_nvlist(const char *name, dev_t dev, cred_t *cr, nvlist_t *nvl) 1181 { 1182 nvpair_t *elem; 1183 int error; 1184 const char *propname; 1185 zfs_prop_t prop; 1186 uint64_t intval; 1187 char *strval; 1188 1189 /* 1190 * First validate permission to set all of the properties 1191 */ 1192 elem = NULL; 1193 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 1194 propname = nvpair_name(elem); 1195 1196 if ((prop = zfs_name_to_prop(propname)) == 1197 ZFS_PROP_INVAL) { 1198 /* 1199 * If this is a user-defined property, it must be a 1200 * string, and there is no further validation to do. 1201 */ 1202 if (!zfs_prop_user(propname) || 1203 nvpair_type(elem) != DATA_TYPE_STRING) 1204 return (EINVAL); 1205 1206 error = zfs_secpolicy_write_perms(name, 1207 ZFS_DELEG_PERM_USERPROP, cr); 1208 if (error) { 1209 return (EPERM); 1210 } 1211 continue; 1212 } 1213 1214 /* 1215 * Check permissions for special properties 1216 */ 1217 1218 switch (prop) { 1219 case ZFS_PROP_ZONED: 1220 /* 1221 * Disallow setting of 'zoned' from within a local zone. 1222 */ 1223 if (!INGLOBALZONE(curproc)) 1224 return (EPERM); 1225 break; 1226 1227 case ZFS_PROP_QUOTA: 1228 if (error = zfs_dozonecheck(name, cr)) 1229 return (error); 1230 1231 if (!INGLOBALZONE(curproc)) { 1232 uint64_t zoned; 1233 char setpoint[MAXNAMELEN]; 1234 int dslen; 1235 /* 1236 * Unprivileged users are allowed to modify the 1237 * quota on things *under* (ie. contained by) 1238 * the thing they own. 1239 */ 1240 if (dsl_prop_get_integer(name, "zoned", &zoned, 1241 setpoint)) 1242 return (EPERM); 1243 if (!zoned) /* this shouldn't happen */ 1244 return (EPERM); 1245 dslen = strlen(name); 1246 if (dslen <= strlen(setpoint)) 1247 return (EPERM); 1248 } 1249 break; 1250 1251 case ZFS_PROP_COMPRESSION: 1252 /* 1253 * If the user specified gzip compression, make sure 1254 * the SPA supports it. We ignore any errors here since 1255 * we'll catch them later. 1256 */ 1257 if (nvpair_type(elem) == DATA_TYPE_UINT64 && 1258 nvpair_value_uint64(elem, &intval) == 0 && 1259 intval >= ZIO_COMPRESS_GZIP_1 && 1260 intval <= ZIO_COMPRESS_GZIP_9) { 1261 char buf[MAXNAMELEN]; 1262 spa_t *spa; 1263 const char *p; 1264 1265 if ((p = strchr(name, '/')) == NULL) { 1266 p = name; 1267 } else { 1268 bcopy(name, buf, p - name); 1269 buf[p - name] = '\0'; 1270 p = buf; 1271 } 1272 1273 if (spa_open(p, &spa, FTAG) == 0) { 1274 if (spa_version(spa) < 1275 SPA_VERSION_GZIP_COMPRESSION) { 1276 spa_close(spa, FTAG); 1277 return (ENOTSUP); 1278 } 1279 1280 spa_close(spa, FTAG); 1281 } 1282 } 1283 break; 1284 } 1285 if ((error = zfs_secpolicy_setprop(name, prop, cr)) != 0) 1286 return (error); 1287 } 1288 1289 elem = NULL; 1290 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 1291 propname = nvpair_name(elem); 1292 1293 if ((prop = zfs_name_to_prop(propname)) == 1294 ZFS_PROP_INVAL) { 1295 1296 VERIFY(nvpair_value_string(elem, &strval) == 0); 1297 error = dsl_prop_set(name, propname, 1, 1298 strlen(strval) + 1, strval); 1299 if (error == 0) 1300 continue; 1301 else 1302 return (error); 1303 } 1304 1305 switch (prop) { 1306 case ZFS_PROP_QUOTA: 1307 if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 1308 (error = dsl_dir_set_quota(name, intval)) != 0) 1309 return (error); 1310 break; 1311 1312 case ZFS_PROP_RESERVATION: 1313 if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 1314 (error = dsl_dir_set_reservation(name, 1315 intval)) != 0) 1316 return (error); 1317 break; 1318 1319 case ZFS_PROP_VOLSIZE: 1320 if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 1321 (error = zvol_set_volsize(name, dev, intval)) != 0) 1322 return (error); 1323 break; 1324 1325 case ZFS_PROP_VOLBLOCKSIZE: 1326 if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 1327 (error = zvol_set_volblocksize(name, intval)) != 0) 1328 return (error); 1329 break; 1330 1331 case ZFS_PROP_VERSION: 1332 if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 1333 (error = zfs_set_version(name, intval)) != 0) 1334 return (error); 1335 break; 1336 1337 default: 1338 if (nvpair_type(elem) == DATA_TYPE_STRING) { 1339 if (zfs_prop_get_type(prop) != 1340 prop_type_string) 1341 return (EINVAL); 1342 VERIFY(nvpair_value_string(elem, &strval) == 0); 1343 if ((error = dsl_prop_set(name, 1344 nvpair_name(elem), 1, strlen(strval) + 1, 1345 strval)) != 0) 1346 return (error); 1347 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 1348 const char *unused; 1349 1350 VERIFY(nvpair_value_uint64(elem, &intval) == 0); 1351 1352 switch (zfs_prop_get_type(prop)) { 1353 case prop_type_number: 1354 break; 1355 case prop_type_boolean: 1356 if (intval > 1) 1357 return (EINVAL); 1358 break; 1359 case prop_type_string: 1360 return (EINVAL); 1361 case prop_type_index: 1362 if (zfs_prop_index_to_string(prop, 1363 intval, &unused) != 0) 1364 return (EINVAL); 1365 break; 1366 default: 1367 cmn_err(CE_PANIC, 1368 "unknown property type"); 1369 break; 1370 } 1371 1372 if ((error = dsl_prop_set(name, propname, 1373 8, 1, &intval)) != 0) 1374 return (error); 1375 } else { 1376 return (EINVAL); 1377 } 1378 break; 1379 } 1380 } 1381 1382 return (0); 1383 } 1384 1385 static int 1386 zfs_ioc_set_prop(zfs_cmd_t *zc) 1387 { 1388 nvlist_t *nvl; 1389 int error; 1390 zfs_prop_t prop; 1391 1392 /* 1393 * If zc_value is set, then this is an attempt to inherit a value. 1394 * Otherwise, zc_nvlist refers to a list of properties to set. 1395 */ 1396 if (zc->zc_value[0] != '\0') { 1397 if (!zfs_prop_user(zc->zc_value) && 1398 ((prop = zfs_name_to_prop(zc->zc_value)) == 1399 ZFS_PROP_INVAL || 1400 !zfs_prop_inheritable(prop))) 1401 return (EINVAL); 1402 1403 return (dsl_prop_set(zc->zc_name, zc->zc_value, 0, 0, NULL)); 1404 } 1405 1406 if ((error = get_nvlist(zc, &nvl)) != 0) 1407 return (error); 1408 1409 error = zfs_set_prop_nvlist(zc->zc_name, zc->zc_dev, 1410 (cred_t *)(uintptr_t)zc->zc_cred, nvl); 1411 1412 nvlist_free(nvl); 1413 return (error); 1414 } 1415 1416 static int 1417 zfs_ioc_pool_set_props(zfs_cmd_t *zc) 1418 { 1419 nvlist_t *nvl; 1420 int error, reset_bootfs = 0; 1421 uint64_t objnum; 1422 uint64_t intval; 1423 zpool_prop_t prop; 1424 nvpair_t *elem; 1425 char *propname, *strval; 1426 spa_t *spa; 1427 vdev_t *rvdev; 1428 char *vdev_type; 1429 objset_t *os; 1430 1431 if ((error = get_nvlist(zc, &nvl)) != 0) 1432 return (error); 1433 1434 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) { 1435 nvlist_free(nvl); 1436 return (error); 1437 } 1438 1439 if (spa_version(spa) < SPA_VERSION_BOOTFS) { 1440 nvlist_free(nvl); 1441 spa_close(spa, FTAG); 1442 return (ENOTSUP); 1443 } 1444 1445 elem = NULL; 1446 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 1447 1448 propname = nvpair_name(elem); 1449 1450 if ((prop = zpool_name_to_prop(propname)) == 1451 ZFS_PROP_INVAL) { 1452 nvlist_free(nvl); 1453 spa_close(spa, FTAG); 1454 return (EINVAL); 1455 } 1456 1457 switch (prop) { 1458 case ZPOOL_PROP_DELEGATION: 1459 VERIFY(nvpair_value_uint64(elem, &intval) == 0); 1460 if (intval > 1) 1461 error = EINVAL; 1462 break; 1463 case ZPOOL_PROP_BOOTFS: 1464 /* 1465 * A bootable filesystem can not be on a RAIDZ pool 1466 * nor a striped pool with more than 1 device. 1467 */ 1468 rvdev = spa->spa_root_vdev; 1469 vdev_type = 1470 rvdev->vdev_child[0]->vdev_ops->vdev_op_type; 1471 if (strcmp(vdev_type, VDEV_TYPE_RAIDZ) == 0 || 1472 (strcmp(vdev_type, VDEV_TYPE_MIRROR) != 0 && 1473 rvdev->vdev_children > 1)) { 1474 error = ENOTSUP; 1475 break; 1476 } 1477 1478 reset_bootfs = 1; 1479 1480 VERIFY(nvpair_value_string(elem, &strval) == 0); 1481 if (strval == NULL || strval[0] == '\0') { 1482 objnum = zpool_prop_default_numeric( 1483 ZPOOL_PROP_BOOTFS); 1484 break; 1485 } 1486 1487 if (error = dmu_objset_open(strval, DMU_OST_ZFS, 1488 DS_MODE_STANDARD | DS_MODE_READONLY, &os)) 1489 break; 1490 objnum = dmu_objset_id(os); 1491 dmu_objset_close(os); 1492 break; 1493 } 1494 1495 if (error) 1496 break; 1497 } 1498 if (error == 0) { 1499 if (reset_bootfs) { 1500 VERIFY(nvlist_remove(nvl, 1501 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), 1502 DATA_TYPE_STRING) == 0); 1503 VERIFY(nvlist_add_uint64(nvl, 1504 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), 1505 objnum) == 0); 1506 } 1507 error = spa_set_props(spa, nvl); 1508 } 1509 1510 nvlist_free(nvl); 1511 spa_close(spa, FTAG); 1512 1513 return (error); 1514 } 1515 1516 static int 1517 zfs_ioc_pool_get_props(zfs_cmd_t *zc) 1518 { 1519 spa_t *spa; 1520 int error; 1521 nvlist_t *nvp = NULL; 1522 1523 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1524 return (error); 1525 1526 error = spa_get_props(spa, &nvp); 1527 1528 if (error == 0 && zc->zc_nvlist_dst != NULL) 1529 error = put_nvlist(zc, nvp); 1530 else 1531 error = EFAULT; 1532 1533 spa_close(spa, FTAG); 1534 1535 if (nvp) 1536 nvlist_free(nvp); 1537 return (error); 1538 } 1539 1540 static int 1541 zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc) 1542 { 1543 nvlist_t *nvp; 1544 int error; 1545 uint32_t uid; 1546 uint32_t gid; 1547 uint32_t *groups; 1548 uint_t group_cnt; 1549 cred_t *usercred; 1550 1551 if ((error = get_nvlist(zc, &nvp)) != 0) { 1552 return (error); 1553 } 1554 1555 if ((error = nvlist_lookup_uint32(nvp, 1556 ZFS_DELEG_PERM_UID, &uid)) != 0) { 1557 nvlist_free(nvp); 1558 return (EPERM); 1559 } 1560 1561 if ((error = nvlist_lookup_uint32(nvp, 1562 ZFS_DELEG_PERM_GID, &gid)) != 0) { 1563 nvlist_free(nvp); 1564 return (EPERM); 1565 } 1566 1567 if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS, 1568 &groups, &group_cnt)) != 0) { 1569 nvlist_free(nvp); 1570 return (EPERM); 1571 } 1572 usercred = cralloc(); 1573 if ((crsetugid(usercred, uid, gid) != 0) || 1574 (crsetgroups(usercred, group_cnt, (gid_t *)groups) != 0)) { 1575 nvlist_free(nvp); 1576 crfree(usercred); 1577 return (EPERM); 1578 } 1579 nvlist_free(nvp); 1580 error = dsl_deleg_access(zc->zc_name, 1581 ZFS_DELEG_PERM_SHAREISCSI, usercred); 1582 crfree(usercred); 1583 return (error); 1584 } 1585 1586 static int 1587 zfs_ioc_set_fsacl(zfs_cmd_t *zc) 1588 { 1589 int error; 1590 nvlist_t *fsaclnv = NULL; 1591 cred_t *cr; 1592 1593 if ((error = get_nvlist(zc, &fsaclnv)) != 0) 1594 return (error); 1595 1596 /* 1597 * Verify nvlist is constructed correctly 1598 */ 1599 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) { 1600 nvlist_free(fsaclnv); 1601 return (EINVAL); 1602 } 1603 1604 /* 1605 * If we don't have PRIV_SYS_MOUNT, then validate 1606 * that user is allowed to hand out each permission in 1607 * the nvlist(s) 1608 */ 1609 1610 cr = (cred_t *)(uintptr_t)zc->zc_cred; 1611 error = secpolicy_zfs(cr); 1612 if (error) { 1613 if (zc->zc_perm_action == B_FALSE) 1614 error = dsl_deleg_can_allow(zc->zc_name, fsaclnv, cr); 1615 else 1616 error = dsl_deleg_can_unallow(zc->zc_name, fsaclnv, cr); 1617 } 1618 1619 if (error == 0) 1620 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action); 1621 1622 nvlist_free(fsaclnv); 1623 return (error); 1624 } 1625 1626 static int 1627 zfs_ioc_get_fsacl(zfs_cmd_t *zc) 1628 { 1629 nvlist_t *nvp; 1630 int error; 1631 1632 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) { 1633 error = put_nvlist(zc, nvp); 1634 nvlist_free(nvp); 1635 } 1636 1637 return (error); 1638 } 1639 1640 static int 1641 zfs_ioc_create_minor(zfs_cmd_t *zc) 1642 { 1643 return (zvol_create_minor(zc->zc_name, zc->zc_dev)); 1644 } 1645 1646 static int 1647 zfs_ioc_remove_minor(zfs_cmd_t *zc) 1648 { 1649 return (zvol_remove_minor(zc->zc_name)); 1650 } 1651 1652 /* 1653 * Search the vfs list for a specified resource. Returns a pointer to it 1654 * or NULL if no suitable entry is found. The caller of this routine 1655 * is responsible for releasing the returned vfs pointer. 1656 */ 1657 static vfs_t * 1658 zfs_get_vfs(const char *resource) 1659 { 1660 struct vfs *vfsp; 1661 struct vfs *vfs_found = NULL; 1662 1663 vfs_list_read_lock(); 1664 vfsp = rootvfs; 1665 do { 1666 if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) { 1667 VFS_HOLD(vfsp); 1668 vfs_found = vfsp; 1669 break; 1670 } 1671 vfsp = vfsp->vfs_next; 1672 } while (vfsp != rootvfs); 1673 vfs_list_unlock(); 1674 return (vfs_found); 1675 } 1676 1677 /* ARGSUSED */ 1678 static void 1679 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 1680 { 1681 nvlist_t *nvprops = arg; 1682 uint64_t version = ZPL_VERSION; 1683 1684 (void) nvlist_lookup_uint64(nvprops, 1685 zfs_prop_to_name(ZFS_PROP_VERSION), &version); 1686 1687 zfs_create_fs(os, cr, version, tx); 1688 } 1689 1690 static int 1691 zfs_ioc_create(zfs_cmd_t *zc) 1692 { 1693 objset_t *clone; 1694 int error = 0; 1695 nvlist_t *nvprops = NULL; 1696 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx); 1697 dmu_objset_type_t type = zc->zc_objset_type; 1698 1699 switch (type) { 1700 1701 case DMU_OST_ZFS: 1702 cbfunc = zfs_create_cb; 1703 break; 1704 1705 case DMU_OST_ZVOL: 1706 cbfunc = zvol_create_cb; 1707 break; 1708 1709 default: 1710 cbfunc = NULL; 1711 } 1712 if (strchr(zc->zc_name, '@')) 1713 return (EINVAL); 1714 1715 if (zc->zc_nvlist_src != NULL && 1716 (error = get_nvlist(zc, &nvprops)) != 0) 1717 return (error); 1718 1719 if (zc->zc_value[0] != '\0') { 1720 /* 1721 * We're creating a clone of an existing snapshot. 1722 */ 1723 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0'; 1724 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) { 1725 nvlist_free(nvprops); 1726 return (EINVAL); 1727 } 1728 1729 error = dmu_objset_open(zc->zc_value, type, 1730 DS_MODE_STANDARD | DS_MODE_READONLY, &clone); 1731 if (error) { 1732 nvlist_free(nvprops); 1733 return (error); 1734 } 1735 error = dmu_objset_create(zc->zc_name, type, clone, NULL, NULL); 1736 dmu_objset_close(clone); 1737 } else { 1738 if (cbfunc == NULL) { 1739 nvlist_free(nvprops); 1740 return (EINVAL); 1741 } 1742 1743 if (type == DMU_OST_ZVOL) { 1744 uint64_t volsize, volblocksize; 1745 1746 if (nvprops == NULL || 1747 nvlist_lookup_uint64(nvprops, 1748 zfs_prop_to_name(ZFS_PROP_VOLSIZE), 1749 &volsize) != 0) { 1750 nvlist_free(nvprops); 1751 return (EINVAL); 1752 } 1753 1754 if ((error = nvlist_lookup_uint64(nvprops, 1755 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 1756 &volblocksize)) != 0 && error != ENOENT) { 1757 nvlist_free(nvprops); 1758 return (EINVAL); 1759 } 1760 1761 if (error != 0) 1762 volblocksize = zfs_prop_default_numeric( 1763 ZFS_PROP_VOLBLOCKSIZE); 1764 1765 if ((error = zvol_check_volblocksize( 1766 volblocksize)) != 0 || 1767 (error = zvol_check_volsize(volsize, 1768 volblocksize)) != 0) { 1769 nvlist_free(nvprops); 1770 return (error); 1771 } 1772 } else if (type == DMU_OST_ZFS) { 1773 uint64_t version; 1774 1775 if (0 == nvlist_lookup_uint64(nvprops, 1776 zfs_prop_to_name(ZFS_PROP_VERSION), &version) && 1777 (version < ZPL_VERSION_INITIAL || 1778 version > ZPL_VERSION)) { 1779 nvlist_free(nvprops); 1780 return (EINVAL); 1781 } 1782 } 1783 1784 error = dmu_objset_create(zc->zc_name, type, NULL, cbfunc, 1785 nvprops); 1786 } 1787 1788 /* 1789 * It would be nice to do this atomically. 1790 */ 1791 if (error == 0) { 1792 if ((error = zfs_set_prop_nvlist(zc->zc_name, 1793 zc->zc_dev, (cred_t *)(uintptr_t)zc->zc_cred, 1794 nvprops)) != 0) 1795 (void) dmu_objset_destroy(zc->zc_name); 1796 } 1797 1798 nvlist_free(nvprops); 1799 return (error); 1800 } 1801 1802 static int 1803 zfs_ioc_snapshot(zfs_cmd_t *zc) 1804 { 1805 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 1806 return (EINVAL); 1807 return (dmu_objset_snapshot(zc->zc_name, 1808 zc->zc_value, zc->zc_cookie)); 1809 } 1810 1811 int 1812 zfs_unmount_snap(char *name, void *arg) 1813 { 1814 char *snapname = arg; 1815 char *cp; 1816 vfs_t *vfsp = NULL; 1817 1818 /* 1819 * Snapshots (which are under .zfs control) must be unmounted 1820 * before they can be destroyed. 1821 */ 1822 1823 if (snapname) { 1824 (void) strcat(name, "@"); 1825 (void) strcat(name, snapname); 1826 vfsp = zfs_get_vfs(name); 1827 cp = strchr(name, '@'); 1828 *cp = '\0'; 1829 } else if (strchr(name, '@')) { 1830 vfsp = zfs_get_vfs(name); 1831 } 1832 1833 if (vfsp) { 1834 /* 1835 * Always force the unmount for snapshots. 1836 */ 1837 int flag = MS_FORCE; 1838 int err; 1839 1840 if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) { 1841 VFS_RELE(vfsp); 1842 return (err); 1843 } 1844 VFS_RELE(vfsp); 1845 if ((err = dounmount(vfsp, flag, kcred)) != 0) 1846 return (err); 1847 } 1848 return (0); 1849 } 1850 1851 static int 1852 zfs_ioc_destroy_snaps(zfs_cmd_t *zc) 1853 { 1854 int err; 1855 1856 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 1857 return (EINVAL); 1858 err = dmu_objset_find(zc->zc_name, 1859 zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN); 1860 if (err) 1861 return (err); 1862 return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value)); 1863 } 1864 1865 static int 1866 zfs_ioc_destroy(zfs_cmd_t *zc) 1867 { 1868 if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) { 1869 int err = zfs_unmount_snap(zc->zc_name, NULL); 1870 if (err) 1871 return (err); 1872 } 1873 1874 return (dmu_objset_destroy(zc->zc_name)); 1875 } 1876 1877 static int 1878 zfs_ioc_rollback(zfs_cmd_t *zc) 1879 { 1880 return (dmu_objset_rollback(zc->zc_name)); 1881 } 1882 1883 static int 1884 zfs_ioc_rename(zfs_cmd_t *zc) 1885 { 1886 boolean_t recursive = zc->zc_cookie & 1; 1887 1888 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0'; 1889 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) 1890 return (EINVAL); 1891 1892 /* 1893 * Unmount snapshot unless we're doing a recursive rename, 1894 * in which case the dataset code figures out which snapshots 1895 * to unmount. 1896 */ 1897 if (!recursive && strchr(zc->zc_name, '@') != NULL && 1898 zc->zc_objset_type == DMU_OST_ZFS) { 1899 int err = zfs_unmount_snap(zc->zc_name, NULL); 1900 if (err) 1901 return (err); 1902 } 1903 1904 return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive)); 1905 } 1906 1907 static int 1908 zfs_ioc_recvbackup(zfs_cmd_t *zc) 1909 { 1910 file_t *fp; 1911 int error, fd; 1912 offset_t new_off; 1913 1914 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 || 1915 strchr(zc->zc_value, '@') == NULL) 1916 return (EINVAL); 1917 1918 fd = zc->zc_cookie; 1919 fp = getf(fd); 1920 if (fp == NULL) 1921 return (EBADF); 1922 error = dmu_recvbackup(zc->zc_value, &zc->zc_begin_record, 1923 &zc->zc_cookie, (boolean_t)zc->zc_guid, fp->f_vnode, 1924 fp->f_offset); 1925 1926 new_off = fp->f_offset + zc->zc_cookie; 1927 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &new_off) == 0) 1928 fp->f_offset = new_off; 1929 1930 releasef(fd); 1931 return (error); 1932 } 1933 1934 static int 1935 zfs_ioc_sendbackup(zfs_cmd_t *zc) 1936 { 1937 objset_t *fromsnap = NULL; 1938 objset_t *tosnap; 1939 file_t *fp; 1940 int error; 1941 1942 error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, 1943 DS_MODE_STANDARD | DS_MODE_READONLY, &tosnap); 1944 if (error) 1945 return (error); 1946 1947 if (zc->zc_value[0] != '\0') { 1948 char buf[MAXPATHLEN]; 1949 char *cp; 1950 1951 (void) strncpy(buf, zc->zc_name, sizeof (buf)); 1952 cp = strchr(buf, '@'); 1953 if (cp) 1954 *(cp+1) = 0; 1955 (void) strncat(buf, zc->zc_value, sizeof (buf)); 1956 error = dmu_objset_open(buf, DMU_OST_ANY, 1957 DS_MODE_STANDARD | DS_MODE_READONLY, &fromsnap); 1958 if (error) { 1959 dmu_objset_close(tosnap); 1960 return (error); 1961 } 1962 } 1963 1964 fp = getf(zc->zc_cookie); 1965 if (fp == NULL) { 1966 dmu_objset_close(tosnap); 1967 if (fromsnap) 1968 dmu_objset_close(fromsnap); 1969 return (EBADF); 1970 } 1971 1972 error = dmu_sendbackup(tosnap, fromsnap, fp->f_vnode); 1973 1974 releasef(zc->zc_cookie); 1975 if (fromsnap) 1976 dmu_objset_close(fromsnap); 1977 dmu_objset_close(tosnap); 1978 return (error); 1979 } 1980 1981 static int 1982 zfs_ioc_inject_fault(zfs_cmd_t *zc) 1983 { 1984 int id, error; 1985 1986 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id, 1987 &zc->zc_inject_record); 1988 1989 if (error == 0) 1990 zc->zc_guid = (uint64_t)id; 1991 1992 return (error); 1993 } 1994 1995 static int 1996 zfs_ioc_clear_fault(zfs_cmd_t *zc) 1997 { 1998 return (zio_clear_fault((int)zc->zc_guid)); 1999 } 2000 2001 static int 2002 zfs_ioc_inject_list_next(zfs_cmd_t *zc) 2003 { 2004 int id = (int)zc->zc_guid; 2005 int error; 2006 2007 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name), 2008 &zc->zc_inject_record); 2009 2010 zc->zc_guid = id; 2011 2012 return (error); 2013 } 2014 2015 static int 2016 zfs_ioc_error_log(zfs_cmd_t *zc) 2017 { 2018 spa_t *spa; 2019 int error; 2020 size_t count = (size_t)zc->zc_nvlist_dst_size; 2021 2022 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 2023 return (error); 2024 2025 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst, 2026 &count); 2027 if (error == 0) 2028 zc->zc_nvlist_dst_size = count; 2029 else 2030 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa); 2031 2032 spa_close(spa, FTAG); 2033 2034 return (error); 2035 } 2036 2037 static int 2038 zfs_ioc_clear(zfs_cmd_t *zc) 2039 { 2040 spa_t *spa; 2041 vdev_t *vd; 2042 int error; 2043 uint64_t txg; 2044 2045 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 2046 return (error); 2047 2048 txg = spa_vdev_enter(spa); 2049 2050 if (zc->zc_guid == 0) { 2051 vd = NULL; 2052 } else if ((vd = spa_lookup_by_guid(spa, zc->zc_guid)) == NULL) { 2053 (void) spa_vdev_exit(spa, NULL, txg, ENODEV); 2054 spa_close(spa, FTAG); 2055 return (ENODEV); 2056 } 2057 2058 vdev_clear(spa, vd); 2059 2060 (void) spa_vdev_exit(spa, NULL, txg, 0); 2061 2062 spa_close(spa, FTAG); 2063 2064 return (0); 2065 } 2066 2067 static int 2068 zfs_ioc_promote(zfs_cmd_t *zc) 2069 { 2070 char *cp; 2071 2072 /* 2073 * We don't need to unmount *all* the origin fs's snapshots, but 2074 * it's easier. 2075 */ 2076 cp = strchr(zc->zc_value, '@'); 2077 if (cp) 2078 *cp = '\0'; 2079 (void) dmu_objset_find(zc->zc_value, 2080 zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS); 2081 return (dsl_dataset_promote(zc->zc_name)); 2082 } 2083 2084 /* 2085 * We don't want to have a hard dependency 2086 * against some special symbols in sharefs 2087 * and nfs. Determine them if needed when 2088 * the first file system is shared. 2089 * Neither sharefs or nfs are unloadable modules. 2090 */ 2091 int (*zexport_fs)(void *arg); 2092 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t); 2093 2094 int zfs_share_inited; 2095 ddi_modhandle_t nfs_mod; 2096 ddi_modhandle_t sharefs_mod; 2097 kmutex_t zfs_share_lock; 2098 2099 static int 2100 zfs_ioc_share(zfs_cmd_t *zc) 2101 { 2102 int error; 2103 int opcode; 2104 2105 if (zfs_share_inited == 0) { 2106 mutex_enter(&zfs_share_lock); 2107 nfs_mod = ddi_modopen("fs/nfs", KRTLD_MODE_FIRST, &error); 2108 sharefs_mod = ddi_modopen("fs/sharefs", 2109 KRTLD_MODE_FIRST, &error); 2110 if (nfs_mod == NULL || sharefs_mod == NULL) { 2111 mutex_exit(&zfs_share_lock); 2112 return (ENOSYS); 2113 } 2114 if (zexport_fs == NULL && ((zexport_fs = (int (*)(void *)) 2115 ddi_modsym(nfs_mod, "nfs_export", &error)) == NULL)) { 2116 mutex_exit(&zfs_share_lock); 2117 return (ENOSYS); 2118 } 2119 2120 if (zshare_fs == NULL && ((zshare_fs = 2121 (int (*)(enum sharefs_sys_op, share_t *, uint32_t)) 2122 ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) { 2123 mutex_exit(&zfs_share_lock); 2124 return (ENOSYS); 2125 } 2126 zfs_share_inited = 1; 2127 mutex_exit(&zfs_share_lock); 2128 } 2129 2130 if (error = zexport_fs((void *)(uintptr_t)zc->zc_share.z_exportdata)) 2131 return (error); 2132 2133 opcode = (zc->zc_share.z_sharetype == B_TRUE) ? 2134 SHAREFS_ADD : SHAREFS_REMOVE; 2135 2136 error = zshare_fs(opcode, 2137 (void *)(uintptr_t)zc->zc_share.z_sharedata, 2138 zc->zc_share.z_sharemax); 2139 2140 return (error); 2141 2142 } 2143 2144 /* 2145 * pool destroy and pool export don't log the history as part of zfsdev_ioctl, 2146 * but rather zfs_ioc_pool_create, and zfs_ioc_pool_export do the loggin 2147 * of those commands. 2148 */ 2149 static zfs_ioc_vec_t zfs_ioc_vec[] = { 2150 { zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 2151 { zfs_ioc_pool_destroy, zfs_secpolicy_config, POOL_NAME, B_FALSE }, 2152 { zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 2153 { zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE }, 2154 { zfs_ioc_pool_configs, zfs_secpolicy_none, NO_NAME, B_FALSE }, 2155 { zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE }, 2156 { zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE }, 2157 { zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 2158 { zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE }, 2159 { zfs_ioc_pool_upgrade, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 2160 { zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE }, 2161 { zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 2162 { zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 2163 { zfs_ioc_vdev_set_state, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 2164 { zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 2165 { zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 2166 { zfs_ioc_vdev_setpath, zfs_secpolicy_config, POOL_NAME, B_FALSE }, 2167 { zfs_ioc_objset_stats, zfs_secpolicy_read, DATASET_NAME, B_FALSE }, 2168 { zfs_ioc_dataset_list_next, zfs_secpolicy_read, 2169 DATASET_NAME, B_FALSE }, 2170 { zfs_ioc_snapshot_list_next, zfs_secpolicy_read, 2171 DATASET_NAME, B_FALSE }, 2172 { zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE }, 2173 { zfs_ioc_create_minor, zfs_secpolicy_minor, DATASET_NAME, B_FALSE }, 2174 { zfs_ioc_remove_minor, zfs_secpolicy_minor, DATASET_NAME, B_FALSE }, 2175 { zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE }, 2176 { zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE }, 2177 { zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE }, 2178 { zfs_ioc_rename, zfs_secpolicy_rename, DATASET_NAME, B_TRUE }, 2179 { zfs_ioc_recvbackup, zfs_secpolicy_receive, DATASET_NAME, B_TRUE }, 2180 { zfs_ioc_sendbackup, zfs_secpolicy_send, DATASET_NAME, B_TRUE }, 2181 { zfs_ioc_inject_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE }, 2182 { zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE }, 2183 { zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE }, 2184 { zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE }, 2185 { zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 2186 { zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE }, 2187 { zfs_ioc_destroy_snaps, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE }, 2188 { zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE }, 2189 { zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE }, 2190 { zfs_ioc_obj_to_path, zfs_secpolicy_config, NO_NAME, B_FALSE }, 2191 { zfs_ioc_pool_set_props, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 2192 { zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE }, 2193 { zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE }, 2194 { zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE }, 2195 { zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi, 2196 DATASET_NAME, B_FALSE }, 2197 { zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE } 2198 }; 2199 2200 static int 2201 zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 2202 { 2203 zfs_cmd_t *zc; 2204 uint_t vec; 2205 int error, rc; 2206 2207 if (getminor(dev) != 0) 2208 return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp)); 2209 2210 vec = cmd - ZFS_IOC; 2211 2212 if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0])) 2213 return (EINVAL); 2214 2215 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP); 2216 2217 error = xcopyin((void *)arg, zc, sizeof (zfs_cmd_t)); 2218 2219 if (error == 0) { 2220 zc->zc_cred = (uintptr_t)cr; 2221 zc->zc_dev = dev; 2222 error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr); 2223 } 2224 2225 /* 2226 * Ensure that all pool/dataset names are valid before we pass down to 2227 * the lower layers. 2228 */ 2229 if (error == 0) { 2230 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0'; 2231 switch (zfs_ioc_vec[vec].zvec_namecheck) { 2232 case POOL_NAME: 2233 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0) 2234 error = EINVAL; 2235 break; 2236 2237 case DATASET_NAME: 2238 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0) 2239 error = EINVAL; 2240 break; 2241 2242 case NO_NAME: 2243 break; 2244 } 2245 } 2246 2247 if (error == 0) 2248 error = zfs_ioc_vec[vec].zvec_func(zc); 2249 2250 rc = xcopyout(zc, (void *)arg, sizeof (zfs_cmd_t)); 2251 if (error == 0) { 2252 error = rc; 2253 if (zfs_ioc_vec[vec].zvec_his_log == B_TRUE) 2254 zfs_log_history(zc); 2255 } 2256 2257 kmem_free(zc, sizeof (zfs_cmd_t)); 2258 return (error); 2259 } 2260 2261 static int 2262 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 2263 { 2264 if (cmd != DDI_ATTACH) 2265 return (DDI_FAILURE); 2266 2267 if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0, 2268 DDI_PSEUDO, 0) == DDI_FAILURE) 2269 return (DDI_FAILURE); 2270 2271 zfs_dip = dip; 2272 2273 ddi_report_dev(dip); 2274 2275 return (DDI_SUCCESS); 2276 } 2277 2278 static int 2279 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 2280 { 2281 if (spa_busy() || zfs_busy() || zvol_busy()) 2282 return (DDI_FAILURE); 2283 2284 if (cmd != DDI_DETACH) 2285 return (DDI_FAILURE); 2286 2287 zfs_dip = NULL; 2288 2289 ddi_prop_remove_all(dip); 2290 ddi_remove_minor_node(dip, NULL); 2291 2292 return (DDI_SUCCESS); 2293 } 2294 2295 /*ARGSUSED*/ 2296 static int 2297 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 2298 { 2299 switch (infocmd) { 2300 case DDI_INFO_DEVT2DEVINFO: 2301 *result = zfs_dip; 2302 return (DDI_SUCCESS); 2303 2304 case DDI_INFO_DEVT2INSTANCE: 2305 *result = (void *)0; 2306 return (DDI_SUCCESS); 2307 } 2308 2309 return (DDI_FAILURE); 2310 } 2311 2312 /* 2313 * OK, so this is a little weird. 2314 * 2315 * /dev/zfs is the control node, i.e. minor 0. 2316 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0. 2317 * 2318 * /dev/zfs has basically nothing to do except serve up ioctls, 2319 * so most of the standard driver entry points are in zvol.c. 2320 */ 2321 static struct cb_ops zfs_cb_ops = { 2322 zvol_open, /* open */ 2323 zvol_close, /* close */ 2324 zvol_strategy, /* strategy */ 2325 nodev, /* print */ 2326 nodev, /* dump */ 2327 zvol_read, /* read */ 2328 zvol_write, /* write */ 2329 zfsdev_ioctl, /* ioctl */ 2330 nodev, /* devmap */ 2331 nodev, /* mmap */ 2332 nodev, /* segmap */ 2333 nochpoll, /* poll */ 2334 ddi_prop_op, /* prop_op */ 2335 NULL, /* streamtab */ 2336 D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */ 2337 CB_REV, /* version */ 2338 nodev, /* async read */ 2339 nodev, /* async write */ 2340 }; 2341 2342 static struct dev_ops zfs_dev_ops = { 2343 DEVO_REV, /* version */ 2344 0, /* refcnt */ 2345 zfs_info, /* info */ 2346 nulldev, /* identify */ 2347 nulldev, /* probe */ 2348 zfs_attach, /* attach */ 2349 zfs_detach, /* detach */ 2350 nodev, /* reset */ 2351 &zfs_cb_ops, /* driver operations */ 2352 NULL /* no bus operations */ 2353 }; 2354 2355 static struct modldrv zfs_modldrv = { 2356 &mod_driverops, "ZFS storage pool version " SPA_VERSION_STRING, 2357 &zfs_dev_ops 2358 }; 2359 2360 static struct modlinkage modlinkage = { 2361 MODREV_1, 2362 (void *)&zfs_modlfs, 2363 (void *)&zfs_modldrv, 2364 NULL 2365 }; 2366 2367 int 2368 _init(void) 2369 { 2370 int error; 2371 2372 spa_init(FREAD | FWRITE); 2373 zfs_init(); 2374 zvol_init(); 2375 2376 if ((error = mod_install(&modlinkage)) != 0) { 2377 zvol_fini(); 2378 zfs_fini(); 2379 spa_fini(); 2380 return (error); 2381 } 2382 2383 error = ldi_ident_from_mod(&modlinkage, &zfs_li); 2384 ASSERT(error == 0); 2385 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL); 2386 2387 return (0); 2388 } 2389 2390 int 2391 _fini(void) 2392 { 2393 int error; 2394 2395 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled) 2396 return (EBUSY); 2397 2398 if ((error = mod_remove(&modlinkage)) != 0) 2399 return (error); 2400 2401 zvol_fini(); 2402 zfs_fini(); 2403 spa_fini(); 2404 if (zfs_share_inited) { 2405 (void) ddi_modclose(nfs_mod); 2406 (void) ddi_modclose(sharefs_mod); 2407 } 2408 2409 ldi_ident_release(zfs_li); 2410 zfs_li = NULL; 2411 mutex_destroy(&zfs_share_lock); 2412 2413 return (error); 2414 } 2415 2416 int 2417 _info(struct modinfo *modinfop) 2418 { 2419 return (mod_info(&modlinkage, modinfop)); 2420 } 2421