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