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 2006 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/vdev.h> 44 #include <sys/dmu.h> 45 #include <sys/dsl_dir.h> 46 #include <sys/dsl_dataset.h> 47 #include <sys/dsl_prop.h> 48 #include <sys/ddi.h> 49 #include <sys/sunddi.h> 50 #include <sys/sunldi.h> 51 #include <sys/policy.h> 52 #include <sys/zone.h> 53 #include <sys/nvpair.h> 54 #include <sys/pathname.h> 55 #include <sys/mount.h> 56 #include <sys/sdt.h> 57 #include <sys/fs/zfs.h> 58 #include <sys/zfs_ctldir.h> 59 60 #include "zfs_namecheck.h" 61 62 extern struct modlfs zfs_modlfs; 63 64 extern void zfs_init(void); 65 extern void zfs_fini(void); 66 67 ldi_ident_t zfs_li = NULL; 68 dev_info_t *zfs_dip; 69 70 typedef int zfs_ioc_func_t(zfs_cmd_t *); 71 typedef int zfs_secpolicy_func_t(const char *, const char *, cred_t *); 72 73 typedef struct zfs_ioc_vec { 74 zfs_ioc_func_t *zvec_func; 75 zfs_secpolicy_func_t *zvec_secpolicy; 76 enum { 77 no_name, 78 pool_name, 79 dataset_name 80 } zvec_namecheck; 81 } zfs_ioc_vec_t; 82 83 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */ 84 void 85 __dprintf(const char *file, const char *func, int line, const char *fmt, ...) 86 { 87 const char *newfile; 88 char buf[256]; 89 va_list adx; 90 91 /* 92 * Get rid of annoying "../common/" prefix to filename. 93 */ 94 newfile = strrchr(file, '/'); 95 if (newfile != NULL) { 96 newfile = newfile + 1; /* Get rid of leading / */ 97 } else { 98 newfile = file; 99 } 100 101 va_start(adx, fmt); 102 (void) vsnprintf(buf, sizeof (buf), fmt, adx); 103 va_end(adx); 104 105 /* 106 * To get this data, use the zfs-dprintf probe as so: 107 * dtrace -q -n 'zfs-dprintf \ 108 * /stringof(arg0) == "dbuf.c"/ \ 109 * {printf("%s: %s", stringof(arg1), stringof(arg3))}' 110 * arg0 = file name 111 * arg1 = function name 112 * arg2 = line number 113 * arg3 = message 114 */ 115 DTRACE_PROBE4(zfs__dprintf, 116 char *, newfile, char *, func, int, line, char *, buf); 117 } 118 119 /* 120 * Policy for top-level read operations (list pools). Requires no privileges, 121 * and can be used in the local zone, as there is no associated dataset. 122 */ 123 /* ARGSUSED */ 124 static int 125 zfs_secpolicy_none(const char *unused1, const char *unused2, cred_t *cr) 126 { 127 return (0); 128 } 129 130 /* 131 * Policy for dataset read operations (list children, get statistics). Requires 132 * no privileges, but must be visible in the local zone. 133 */ 134 /* ARGSUSED */ 135 static int 136 zfs_secpolicy_read(const char *dataset, const char *unused, cred_t *cr) 137 { 138 if (INGLOBALZONE(curproc) || 139 zone_dataset_visible(dataset, NULL)) 140 return (0); 141 142 return (ENOENT); 143 } 144 145 static int 146 zfs_dozonecheck(const char *dataset, cred_t *cr) 147 { 148 uint64_t zoned; 149 int writable = 1; 150 151 /* 152 * The dataset must be visible by this zone -- check this first 153 * so they don't see EPERM on something they shouldn't know about. 154 */ 155 if (!INGLOBALZONE(curproc) && 156 !zone_dataset_visible(dataset, &writable)) 157 return (ENOENT); 158 159 if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL)) 160 return (ENOENT); 161 162 if (INGLOBALZONE(curproc)) { 163 /* 164 * If the fs is zoned, only root can access it from the 165 * global zone. 166 */ 167 if (secpolicy_zfs(cr) && zoned) 168 return (EPERM); 169 } else { 170 /* 171 * If we are in a local zone, the 'zoned' property must be set. 172 */ 173 if (!zoned) 174 return (EPERM); 175 176 /* must be writable by this zone */ 177 if (!writable) 178 return (EPERM); 179 } 180 return (0); 181 } 182 183 /* 184 * Policy for dataset write operations (create children, set properties, etc). 185 * Requires SYS_MOUNT privilege, and must be writable in the local zone. 186 */ 187 /* ARGSUSED */ 188 int 189 zfs_secpolicy_write(const char *dataset, const char *unused, cred_t *cr) 190 { 191 int error; 192 193 if (error = zfs_dozonecheck(dataset, cr)) 194 return (error); 195 196 return (secpolicy_zfs(cr)); 197 } 198 199 /* 200 * Policy for operations that want to write a dataset's parent: 201 * create, destroy, snapshot, clone, restore. 202 */ 203 static int 204 zfs_secpolicy_parent(const char *dataset, const char *unused, cred_t *cr) 205 { 206 char parentname[MAXNAMELEN]; 207 char *cp; 208 209 /* 210 * Remove the @bla or /bla from the end of the name to get the parent. 211 */ 212 (void) strncpy(parentname, dataset, sizeof (parentname)); 213 cp = strrchr(parentname, '@'); 214 if (cp != NULL) { 215 cp[0] = '\0'; 216 } else { 217 cp = strrchr(parentname, '/'); 218 if (cp == NULL) 219 return (ENOENT); 220 cp[0] = '\0'; 221 222 } 223 224 return (zfs_secpolicy_write(parentname, unused, cr)); 225 } 226 227 /* 228 * Policy for dataset write operations (create children, set properties, etc). 229 * Requires SYS_MOUNT privilege, and must be writable in the local zone. 230 */ 231 static int 232 zfs_secpolicy_setprop(const char *dataset, const char *prop, cred_t *cr) 233 { 234 int error; 235 236 if (error = zfs_dozonecheck(dataset, cr)) 237 return (error); 238 239 if (strcmp(prop, "zoned") == 0) { 240 /* 241 * Disallow setting of 'zoned' from within a local zone. 242 */ 243 if (!INGLOBALZONE(curproc)) 244 return (EPERM); 245 } 246 247 return (secpolicy_zfs(cr)); 248 } 249 250 /* 251 * Security policy for setting the quota. This is the same as 252 * zfs_secpolicy_write, except that the local zone may not change the quota at 253 * the zone-property setpoint. 254 */ 255 /* ARGSUSED */ 256 static int 257 zfs_secpolicy_quota(const char *dataset, const char *unused, cred_t *cr) 258 { 259 int error; 260 261 if (error = zfs_dozonecheck(dataset, cr)) 262 return (error); 263 264 if (!INGLOBALZONE(curproc)) { 265 uint64_t zoned; 266 char setpoint[MAXNAMELEN]; 267 int dslen; 268 /* 269 * Unprivileged users are allowed to modify the quota 270 * on things *under* (ie. contained by) the thing they 271 * own. 272 */ 273 if (dsl_prop_get_integer(dataset, "zoned", &zoned, setpoint)) 274 return (EPERM); 275 if (!zoned) /* this shouldn't happen */ 276 return (EPERM); 277 dslen = strlen(dataset); 278 if (dslen <= strlen(setpoint)) 279 return (EPERM); 280 } 281 282 return (secpolicy_zfs(cr)); 283 } 284 285 /* 286 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires 287 * SYS_CONFIG privilege, which is not available in a local zone. 288 */ 289 /* ARGSUSED */ 290 static int 291 zfs_secpolicy_config(const char *unused, const char *unused2, cred_t *cr) 292 { 293 if (secpolicy_sys_config(cr, B_FALSE) != 0) 294 return (EPERM); 295 296 return (0); 297 } 298 299 /* 300 * Policy for fault injection. Requires all privileges. 301 */ 302 /* ARGSUSED */ 303 static int 304 zfs_secpolicy_inject(const char *unused, const char *unused2, cred_t *cr) 305 { 306 return (secpolicy_zinject(cr)); 307 } 308 309 /* 310 * Returns the nvlist as specified by the user in the zfs_cmd_t. 311 */ 312 static int 313 get_config(zfs_cmd_t *zc, nvlist_t **nvp) 314 { 315 char *packed; 316 size_t size; 317 int error; 318 nvlist_t *config = NULL; 319 320 /* 321 * Read in and unpack the user-supplied nvlist. By this point, we know 322 * that the user has the SYS_CONFIG privilege, so allocating arbitrary 323 * sized regions of memory should not be a problem. 324 */ 325 if ((size = zc->zc_config_src_size) == 0) 326 return (EINVAL); 327 328 packed = kmem_alloc(size, KM_SLEEP); 329 330 if ((error = xcopyin((void *)(uintptr_t)zc->zc_config_src, packed, 331 size)) != 0) { 332 kmem_free(packed, size); 333 return (error); 334 } 335 336 if ((error = nvlist_unpack(packed, size, &config, 0)) != 0) { 337 kmem_free(packed, size); 338 return (error); 339 } 340 341 kmem_free(packed, size); 342 343 *nvp = config; 344 return (0); 345 } 346 347 static int 348 zfs_ioc_pool_create(zfs_cmd_t *zc) 349 { 350 int error; 351 nvlist_t *config; 352 353 if ((error = get_config(zc, &config)) != 0) 354 return (error); 355 356 error = spa_create(zc->zc_name, config, zc->zc_root[0] == '\0' ? 357 NULL : zc->zc_root); 358 359 nvlist_free(config); 360 361 return (error); 362 } 363 364 static int 365 zfs_ioc_pool_destroy(zfs_cmd_t *zc) 366 { 367 return (spa_destroy(zc->zc_name)); 368 } 369 370 static int 371 zfs_ioc_pool_import(zfs_cmd_t *zc) 372 { 373 int error; 374 nvlist_t *config; 375 uint64_t guid; 376 377 if ((error = get_config(zc, &config)) != 0) 378 return (error); 379 380 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 || 381 guid != zc->zc_guid) 382 error = EINVAL; 383 else 384 error = spa_import(zc->zc_name, config, 385 zc->zc_root[0] == '\0' ? NULL : zc->zc_root); 386 387 nvlist_free(config); 388 389 return (error); 390 } 391 392 static int 393 zfs_ioc_pool_export(zfs_cmd_t *zc) 394 { 395 return (spa_export(zc->zc_name)); 396 } 397 398 static int 399 zfs_ioc_pool_configs(zfs_cmd_t *zc) 400 { 401 nvlist_t *configs; 402 char *packed = NULL; 403 size_t size = 0; 404 int error; 405 406 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL) 407 return (EEXIST); 408 409 VERIFY(nvlist_pack(configs, &packed, &size, NV_ENCODE_NATIVE, 410 KM_SLEEP) == 0); 411 412 if (size > zc->zc_config_dst_size) 413 error = ENOMEM; 414 else 415 error = xcopyout(packed, (void *)(uintptr_t)zc->zc_config_dst, 416 size); 417 418 zc->zc_config_dst_size = size; 419 420 kmem_free(packed, size); 421 nvlist_free(configs); 422 423 return (error); 424 } 425 426 static int 427 zfs_ioc_pool_guid(zfs_cmd_t *zc) 428 { 429 spa_t *spa; 430 int error; 431 432 error = spa_open(zc->zc_name, &spa, FTAG); 433 if (error == 0) { 434 zc->zc_guid = spa_guid(spa); 435 spa_close(spa, FTAG); 436 } 437 return (error); 438 } 439 440 static int 441 zfs_ioc_pool_stats(zfs_cmd_t *zc) 442 { 443 nvlist_t *config; 444 char *packed = NULL; 445 size_t size = 0; 446 int error; 447 int ret = 0; 448 449 error = spa_get_stats(zc->zc_name, &config, zc->zc_root, 450 sizeof (zc->zc_root)); 451 452 if (config != NULL) { 453 VERIFY(nvlist_pack(config, &packed, &size, 454 NV_ENCODE_NATIVE, KM_SLEEP) == 0); 455 456 if (size > zc->zc_config_dst_size) 457 ret = ENOMEM; 458 else if (xcopyout(packed, (void *)(uintptr_t)zc->zc_config_dst, 459 size)) 460 ret = EFAULT; 461 462 zc->zc_config_dst_size = size; 463 464 kmem_free(packed, size); 465 nvlist_free(config); 466 467 /* 468 * The config may be present even if 'error' is non-zero. 469 * In this case we return success, and preserve the real errno 470 * in 'zc_cookie'. 471 */ 472 zc->zc_cookie = error; 473 } else { 474 ret = error; 475 } 476 477 return (ret); 478 } 479 480 /* 481 * Try to import the given pool, returning pool stats as appropriate so that 482 * user land knows which devices are available and overall pool health. 483 */ 484 static int 485 zfs_ioc_pool_tryimport(zfs_cmd_t *zc) 486 { 487 nvlist_t *tryconfig, *config; 488 char *packed = NULL; 489 size_t size = 0; 490 int error; 491 492 if ((error = get_config(zc, &tryconfig)) != 0) 493 return (error); 494 495 config = spa_tryimport(tryconfig); 496 497 nvlist_free(tryconfig); 498 499 if (config == NULL) 500 return (EINVAL); 501 502 VERIFY(nvlist_pack(config, &packed, &size, NV_ENCODE_NATIVE, 503 KM_SLEEP) == 0); 504 505 if (size > zc->zc_config_dst_size) 506 error = ENOMEM; 507 else 508 error = xcopyout(packed, (void *)(uintptr_t)zc->zc_config_dst, 509 size); 510 511 zc->zc_config_dst_size = size; 512 513 kmem_free(packed, size); 514 nvlist_free(config); 515 516 return (error); 517 } 518 519 static int 520 zfs_ioc_pool_scrub(zfs_cmd_t *zc) 521 { 522 spa_t *spa; 523 int error; 524 525 error = spa_open(zc->zc_name, &spa, FTAG); 526 if (error == 0) { 527 error = spa_scrub(spa, zc->zc_cookie, B_FALSE); 528 spa_close(spa, FTAG); 529 } 530 return (error); 531 } 532 533 static int 534 zfs_ioc_pool_freeze(zfs_cmd_t *zc) 535 { 536 spa_t *spa; 537 int error; 538 539 error = spa_open(zc->zc_name, &spa, FTAG); 540 if (error == 0) { 541 spa_freeze(spa); 542 spa_close(spa, FTAG); 543 } 544 return (error); 545 } 546 547 static int 548 zfs_ioc_vdev_add(zfs_cmd_t *zc) 549 { 550 spa_t *spa; 551 int error; 552 nvlist_t *config; 553 554 error = spa_open(zc->zc_name, &spa, FTAG); 555 if (error != 0) 556 return (error); 557 558 if ((error = get_config(zc, &config)) == 0) { 559 error = spa_vdev_add(spa, config); 560 nvlist_free(config); 561 } 562 563 spa_close(spa, FTAG); 564 return (error); 565 } 566 567 /* ARGSUSED */ 568 static int 569 zfs_ioc_vdev_remove(zfs_cmd_t *zc) 570 { 571 return (ENOTSUP); 572 } 573 574 static int 575 zfs_ioc_vdev_online(zfs_cmd_t *zc) 576 { 577 spa_t *spa; 578 int error; 579 580 error = spa_open(zc->zc_name, &spa, FTAG); 581 if (error != 0) 582 return (error); 583 error = vdev_online(spa, zc->zc_guid); 584 spa_close(spa, FTAG); 585 return (error); 586 } 587 588 static int 589 zfs_ioc_vdev_offline(zfs_cmd_t *zc) 590 { 591 spa_t *spa; 592 int istmp = zc->zc_cookie; 593 int error; 594 595 error = spa_open(zc->zc_name, &spa, FTAG); 596 if (error != 0) 597 return (error); 598 error = vdev_offline(spa, zc->zc_guid, istmp); 599 spa_close(spa, FTAG); 600 return (error); 601 } 602 603 static int 604 zfs_ioc_vdev_attach(zfs_cmd_t *zc) 605 { 606 spa_t *spa; 607 int replacing = zc->zc_cookie; 608 nvlist_t *config; 609 int error; 610 611 error = spa_open(zc->zc_name, &spa, FTAG); 612 if (error != 0) 613 return (error); 614 615 if ((error = get_config(zc, &config)) == 0) { 616 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing); 617 nvlist_free(config); 618 } 619 620 spa_close(spa, FTAG); 621 return (error); 622 } 623 624 static int 625 zfs_ioc_vdev_detach(zfs_cmd_t *zc) 626 { 627 spa_t *spa; 628 int error; 629 630 error = spa_open(zc->zc_name, &spa, FTAG); 631 if (error != 0) 632 return (error); 633 634 error = spa_vdev_detach(spa, zc->zc_guid, B_FALSE); 635 636 spa_close(spa, FTAG); 637 return (error); 638 } 639 640 static int 641 zfs_ioc_vdev_setpath(zfs_cmd_t *zc) 642 { 643 spa_t *spa; 644 char *path = zc->zc_prop_value; 645 uint64_t guid = zc->zc_guid; 646 int error; 647 648 error = spa_open(zc->zc_name, &spa, FTAG); 649 if (error != 0) 650 return (error); 651 652 error = spa_vdev_setpath(spa, guid, path); 653 654 spa_close(spa, FTAG); 655 return (error); 656 } 657 658 659 static int 660 zfs_ioc_objset_stats(zfs_cmd_t *zc) 661 { 662 objset_t *os = NULL; 663 int error; 664 nvlist_t *nv; 665 size_t sz; 666 char *buf; 667 668 retry: 669 error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, 670 DS_MODE_STANDARD | DS_MODE_READONLY, &os); 671 if (error != 0) { 672 /* 673 * This is ugly: dmu_objset_open() can return EBUSY if 674 * the objset is held exclusively. Fortunately this hold is 675 * only for a short while, so we retry here. 676 * This avoids user code having to handle EBUSY, 677 * for example for a "zfs list". 678 */ 679 if (error == EBUSY) { 680 delay(1); 681 goto retry; 682 } 683 return (error); 684 } 685 686 dmu_objset_stats(os, &zc->zc_objset_stats); 687 688 if (zc->zc_config_src != NULL && 689 (error = dsl_prop_get_all(os, &nv)) == 0) { 690 VERIFY(nvlist_size(nv, &sz, NV_ENCODE_NATIVE) == 0); 691 if (sz > zc->zc_config_src_size) { 692 zc->zc_config_src_size = sz; 693 error = ENOMEM; 694 } else { 695 buf = kmem_alloc(sz, KM_SLEEP); 696 VERIFY(nvlist_pack(nv, &buf, &sz, 697 NV_ENCODE_NATIVE, 0) == 0); 698 error = xcopyout(buf, 699 (void *)(uintptr_t)zc->zc_config_src, sz); 700 kmem_free(buf, sz); 701 } 702 nvlist_free(nv); 703 } 704 705 if (!error && zc->zc_objset_stats.dds_type == DMU_OST_ZVOL) 706 error = zvol_get_stats(zc, os); 707 708 spa_altroot(dmu_objset_spa(os), zc->zc_root, sizeof (zc->zc_root)); 709 710 dmu_objset_close(os); 711 return (error); 712 } 713 714 static int 715 zfs_ioc_dataset_list_next(zfs_cmd_t *zc) 716 { 717 objset_t *os; 718 int error; 719 char *p; 720 721 retry: 722 error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, 723 DS_MODE_STANDARD | DS_MODE_READONLY, &os); 724 if (error != 0) { 725 /* 726 * This is ugly: dmu_objset_open() can return EBUSY if 727 * the objset is held exclusively. Fortunately this hold is 728 * only for a short while, so we retry here. 729 * This avoids user code having to handle EBUSY, 730 * for example for a "zfs list". 731 */ 732 if (error == EBUSY) { 733 delay(1); 734 goto retry; 735 } 736 if (error == ENOENT) 737 error = ESRCH; 738 return (error); 739 } 740 741 p = strrchr(zc->zc_name, '/'); 742 if (p == NULL || p[1] != '\0') 743 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name)); 744 p = zc->zc_name + strlen(zc->zc_name); 745 746 do { 747 error = dmu_dir_list_next(os, 748 sizeof (zc->zc_name) - (p - zc->zc_name), p, 749 NULL, &zc->zc_cookie); 750 if (error == ENOENT) 751 error = ESRCH; 752 } while (error == 0 && !INGLOBALZONE(curproc) && 753 !zone_dataset_visible(zc->zc_name, NULL)); 754 755 /* 756 * If it's a hidden dataset (ie. with a '$' in its name), don't 757 * try to get stats for it. Userland will skip over it. 758 */ 759 if (error == 0 && strchr(zc->zc_name, '$') == NULL) 760 error = zfs_ioc_objset_stats(zc); /* fill in the stats */ 761 762 dmu_objset_close(os); 763 return (error); 764 } 765 766 static int 767 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc) 768 { 769 objset_t *os; 770 int error; 771 772 retry: 773 error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, 774 DS_MODE_STANDARD | DS_MODE_READONLY, &os); 775 if (error != 0) { 776 /* 777 * This is ugly: dmu_objset_open() can return EBUSY if 778 * the objset is held exclusively. Fortunately this hold is 779 * only for a short while, so we retry here. 780 * This avoids user code having to handle EBUSY, 781 * for example for a "zfs list". 782 */ 783 if (error == EBUSY) { 784 delay(1); 785 goto retry; 786 } 787 if (error == ENOENT) 788 error = ESRCH; 789 return (error); 790 } 791 792 /* 793 * A dataset name of maximum length cannot have any snapshots, 794 * so exit immediately. 795 */ 796 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) { 797 dmu_objset_close(os); 798 return (ESRCH); 799 } 800 801 error = dmu_snapshot_list_next(os, 802 sizeof (zc->zc_name) - strlen(zc->zc_name), 803 zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie); 804 if (error == ENOENT) 805 error = ESRCH; 806 807 if (error == 0) 808 error = zfs_ioc_objset_stats(zc); /* fill in the stats */ 809 810 dmu_objset_close(os); 811 return (error); 812 } 813 814 static int 815 zfs_ioc_set_prop(zfs_cmd_t *zc) 816 { 817 return (dsl_prop_set(zc->zc_name, zc->zc_prop_name, 818 zc->zc_intsz, zc->zc_numints, zc->zc_prop_value)); 819 } 820 821 static int 822 zfs_ioc_set_quota(zfs_cmd_t *zc) 823 { 824 return (dsl_dir_set_quota(zc->zc_name, zc->zc_cookie)); 825 } 826 827 static int 828 zfs_ioc_set_reservation(zfs_cmd_t *zc) 829 { 830 return (dsl_dir_set_reservation(zc->zc_name, zc->zc_cookie)); 831 } 832 833 static int 834 zfs_ioc_set_volsize(zfs_cmd_t *zc) 835 { 836 return (zvol_set_volsize(zc)); 837 } 838 839 static int 840 zfs_ioc_set_volblocksize(zfs_cmd_t *zc) 841 { 842 return (zvol_set_volblocksize(zc)); 843 } 844 845 static int 846 zfs_ioc_create_minor(zfs_cmd_t *zc) 847 { 848 return (zvol_create_minor(zc)); 849 } 850 851 static int 852 zfs_ioc_remove_minor(zfs_cmd_t *zc) 853 { 854 return (zvol_remove_minor(zc)); 855 } 856 857 /* 858 * Search the vfs list for a specified resource. Returns a pointer to it 859 * or NULL if no suitable entry is found. The caller of this routine 860 * is responsible for releasing the returned vfs pointer. 861 */ 862 static vfs_t * 863 zfs_get_vfs(const char *resource) 864 { 865 struct vfs *vfsp; 866 struct vfs *vfs_found = NULL; 867 868 vfs_list_read_lock(); 869 vfsp = rootvfs; 870 do { 871 if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) { 872 VFS_HOLD(vfsp); 873 vfs_found = vfsp; 874 break; 875 } 876 vfsp = vfsp->vfs_next; 877 } while (vfsp != rootvfs); 878 vfs_list_unlock(); 879 return (vfs_found); 880 } 881 882 static void 883 zfs_create_cb(objset_t *os, void *arg, dmu_tx_t *tx) 884 { 885 zfs_cmd_t *zc = arg; 886 zfs_create_fs(os, (cred_t *)(uintptr_t)zc->zc_cred, tx); 887 } 888 889 static int 890 zfs_ioc_create(zfs_cmd_t *zc) 891 { 892 objset_t *clone; 893 int error = 0; 894 void (*cbfunc)(objset_t *os, void *arg, dmu_tx_t *tx); 895 dmu_objset_type_t type = zc->zc_objset_type; 896 897 switch (type) { 898 899 case DMU_OST_ZFS: 900 cbfunc = zfs_create_cb; 901 break; 902 903 case DMU_OST_ZVOL: 904 cbfunc = zvol_create_cb; 905 break; 906 907 default: 908 return (EINVAL); 909 } 910 911 if (zc->zc_filename[0] != '\0') { 912 /* 913 * We're creating a clone of an existing snapshot. 914 */ 915 zc->zc_filename[sizeof (zc->zc_filename) - 1] = '\0'; 916 if (dataset_namecheck(zc->zc_filename, NULL, NULL) != 0) 917 return (EINVAL); 918 919 error = dmu_objset_open(zc->zc_filename, type, 920 DS_MODE_STANDARD | DS_MODE_READONLY, &clone); 921 if (error) 922 return (error); 923 error = dmu_objset_create(zc->zc_name, type, clone, NULL, NULL); 924 dmu_objset_close(clone); 925 } else if (strchr(zc->zc_name, '@') != 0) { 926 /* 927 * We're taking a snapshot of an existing dataset. 928 */ 929 error = dmu_objset_create(zc->zc_name, type, NULL, NULL, NULL); 930 } else { 931 /* 932 * We're creating a new dataset. 933 */ 934 if (type == DMU_OST_ZVOL) { 935 936 if ((error = zvol_check_volblocksize(zc)) != 0) 937 return (error); 938 939 if ((error = zvol_check_volsize(zc, 940 zc->zc_volblocksize)) != 0) 941 return (error); 942 } 943 error = dmu_objset_create(zc->zc_name, type, NULL, cbfunc, zc); 944 } 945 return (error); 946 } 947 948 static int 949 zfs_ioc_destroy(zfs_cmd_t *zc) 950 { 951 if (strchr(zc->zc_name, '@') != NULL && 952 zc->zc_objset_type == DMU_OST_ZFS) { 953 vfs_t *vfsp; 954 int err; 955 956 /* 957 * Snapshots under .zfs control must be unmounted 958 * before they can be destroyed. 959 */ 960 if ((vfsp = zfs_get_vfs(zc->zc_name)) != NULL) { 961 /* 962 * Always force the unmount for snapshots. 963 */ 964 int flag = MS_FORCE; 965 966 if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) { 967 VFS_RELE(vfsp); 968 return (err); 969 } 970 VFS_RELE(vfsp); 971 if ((err = dounmount(vfsp, flag, kcred)) != 0) 972 return (err); 973 } 974 } 975 976 return (dmu_objset_destroy(zc->zc_name)); 977 } 978 979 static int 980 zfs_ioc_rollback(zfs_cmd_t *zc) 981 { 982 return (dmu_objset_rollback(zc->zc_name)); 983 } 984 985 static int 986 zfs_ioc_rename(zfs_cmd_t *zc) 987 { 988 zc->zc_prop_value[sizeof (zc->zc_prop_value) - 1] = '\0'; 989 if (dataset_namecheck(zc->zc_prop_value, NULL, NULL) != 0) 990 return (EINVAL); 991 992 if (strchr(zc->zc_name, '@') != NULL && 993 zc->zc_objset_type == DMU_OST_ZFS) { 994 vfs_t *vfsp; 995 int err; 996 997 /* 998 * Snapshots under .zfs control must be unmounted 999 * before they can be renamed. 1000 */ 1001 if ((vfsp = zfs_get_vfs(zc->zc_name)) != NULL) { 1002 /* 1003 * Always force the unmount for snapshots. 1004 */ 1005 int flag = MS_FORCE; 1006 1007 if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) { 1008 VFS_RELE(vfsp); 1009 return (err); 1010 } 1011 VFS_RELE(vfsp); 1012 if ((err = dounmount(vfsp, flag, kcred)) != 0) 1013 return (err); 1014 } 1015 } 1016 1017 return (dmu_objset_rename(zc->zc_name, zc->zc_prop_value)); 1018 } 1019 1020 static int 1021 zfs_ioc_recvbackup(zfs_cmd_t *zc) 1022 { 1023 file_t *fp; 1024 int error, fd; 1025 1026 fd = zc->zc_cookie; 1027 fp = getf(fd); 1028 if (fp == NULL) 1029 return (EBADF); 1030 error = dmu_recvbackup(zc->zc_filename, &zc->zc_begin_record, 1031 &zc->zc_cookie, fp->f_vnode, fp->f_offset); 1032 releasef(fd); 1033 return (error); 1034 } 1035 1036 static int 1037 zfs_ioc_sendbackup(zfs_cmd_t *zc) 1038 { 1039 objset_t *fromsnap = NULL; 1040 objset_t *tosnap; 1041 file_t *fp; 1042 int error; 1043 1044 error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, 1045 DS_MODE_STANDARD | DS_MODE_READONLY, &tosnap); 1046 if (error) 1047 return (error); 1048 1049 if (zc->zc_prop_value[0] != '\0') { 1050 error = dmu_objset_open(zc->zc_prop_value, DMU_OST_ANY, 1051 DS_MODE_STANDARD | DS_MODE_READONLY, &fromsnap); 1052 if (error) { 1053 dmu_objset_close(tosnap); 1054 return (error); 1055 } 1056 } 1057 1058 fp = getf(zc->zc_cookie); 1059 if (fp == NULL) { 1060 dmu_objset_close(tosnap); 1061 if (fromsnap) 1062 dmu_objset_close(fromsnap); 1063 return (EBADF); 1064 } 1065 1066 error = dmu_sendbackup(tosnap, fromsnap, fp->f_vnode); 1067 1068 releasef(zc->zc_cookie); 1069 if (fromsnap) 1070 dmu_objset_close(fromsnap); 1071 dmu_objset_close(tosnap); 1072 return (error); 1073 } 1074 1075 static int 1076 zfs_ioc_inject_fault(zfs_cmd_t *zc) 1077 { 1078 int id, error; 1079 1080 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id, 1081 &zc->zc_inject_record); 1082 1083 if (error == 0) 1084 zc->zc_guid = (uint64_t)id; 1085 1086 return (error); 1087 } 1088 1089 static int 1090 zfs_ioc_clear_fault(zfs_cmd_t *zc) 1091 { 1092 return (zio_clear_fault((int)zc->zc_guid)); 1093 } 1094 1095 static int 1096 zfs_ioc_inject_list_next(zfs_cmd_t *zc) 1097 { 1098 int id = (int)zc->zc_guid; 1099 int error; 1100 1101 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name), 1102 &zc->zc_inject_record); 1103 1104 zc->zc_guid = id; 1105 1106 return (error); 1107 } 1108 1109 static int 1110 zfs_ioc_error_log(zfs_cmd_t *zc) 1111 { 1112 spa_t *spa; 1113 int error; 1114 size_t count = (size_t)zc->zc_config_dst_size; 1115 1116 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1117 return (error); 1118 1119 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_config_dst, 1120 &count); 1121 if (error == 0) 1122 zc->zc_config_dst_size = count; 1123 else 1124 zc->zc_config_dst_size = spa_get_errlog_size(spa); 1125 1126 spa_close(spa, FTAG); 1127 1128 return (error); 1129 } 1130 1131 static int 1132 zfs_ioc_clear(zfs_cmd_t *zc) 1133 { 1134 spa_t *spa; 1135 vdev_t *vd; 1136 int error; 1137 1138 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1139 return (error); 1140 1141 spa_config_enter(spa, RW_WRITER, FTAG); 1142 1143 if (zc->zc_prop_value[0] == '\0') 1144 vd = NULL; 1145 else if ((vd = spa_lookup_by_guid(spa, zc->zc_guid)) == NULL) { 1146 spa_config_exit(spa, FTAG); 1147 spa_close(spa, FTAG); 1148 return (ENODEV); 1149 } 1150 1151 vdev_clear(spa, vd); 1152 1153 spa_config_exit(spa, FTAG); 1154 1155 spa_close(spa, FTAG); 1156 1157 return (0); 1158 } 1159 1160 static int 1161 zfs_ioc_bookmark_name(zfs_cmd_t *zc) 1162 { 1163 spa_t *spa; 1164 int error; 1165 1166 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1167 return (error); 1168 1169 error = spa_bookmark_name(spa, &zc->zc_bookmark, 1170 zc->zc_prop_name, sizeof (zc->zc_prop_name), zc->zc_prop_value, 1171 sizeof (zc->zc_prop_value), zc->zc_filename, 1172 sizeof (zc->zc_filename)); 1173 1174 spa_close(spa, FTAG); 1175 1176 return (error); 1177 } 1178 1179 static zfs_ioc_vec_t zfs_ioc_vec[] = { 1180 { zfs_ioc_pool_create, zfs_secpolicy_config, pool_name }, 1181 { zfs_ioc_pool_destroy, zfs_secpolicy_config, pool_name }, 1182 { zfs_ioc_pool_import, zfs_secpolicy_config, pool_name }, 1183 { zfs_ioc_pool_export, zfs_secpolicy_config, pool_name }, 1184 { zfs_ioc_pool_configs, zfs_secpolicy_none, no_name }, 1185 { zfs_ioc_pool_guid, zfs_secpolicy_read, pool_name }, 1186 { zfs_ioc_pool_stats, zfs_secpolicy_read, pool_name }, 1187 { zfs_ioc_pool_tryimport, zfs_secpolicy_config, no_name }, 1188 { zfs_ioc_pool_scrub, zfs_secpolicy_config, pool_name }, 1189 { zfs_ioc_pool_freeze, zfs_secpolicy_config, no_name }, 1190 { zfs_ioc_vdev_add, zfs_secpolicy_config, pool_name }, 1191 { zfs_ioc_vdev_remove, zfs_secpolicy_config, pool_name }, 1192 { zfs_ioc_vdev_online, zfs_secpolicy_config, pool_name }, 1193 { zfs_ioc_vdev_offline, zfs_secpolicy_config, pool_name }, 1194 { zfs_ioc_vdev_attach, zfs_secpolicy_config, pool_name }, 1195 { zfs_ioc_vdev_detach, zfs_secpolicy_config, pool_name }, 1196 { zfs_ioc_vdev_setpath, zfs_secpolicy_config, pool_name }, 1197 { zfs_ioc_objset_stats, zfs_secpolicy_read, dataset_name }, 1198 { zfs_ioc_dataset_list_next, zfs_secpolicy_read, dataset_name }, 1199 { zfs_ioc_snapshot_list_next, zfs_secpolicy_read, dataset_name }, 1200 { zfs_ioc_set_prop, zfs_secpolicy_setprop, dataset_name }, 1201 { zfs_ioc_set_quota, zfs_secpolicy_quota, dataset_name }, 1202 { zfs_ioc_set_reservation, zfs_secpolicy_write, dataset_name }, 1203 { zfs_ioc_set_volsize, zfs_secpolicy_config, dataset_name }, 1204 { zfs_ioc_set_volblocksize, zfs_secpolicy_config, dataset_name }, 1205 { zfs_ioc_create_minor, zfs_secpolicy_config, dataset_name }, 1206 { zfs_ioc_remove_minor, zfs_secpolicy_config, dataset_name }, 1207 { zfs_ioc_create, zfs_secpolicy_parent, dataset_name }, 1208 { zfs_ioc_destroy, zfs_secpolicy_parent, dataset_name }, 1209 { zfs_ioc_rollback, zfs_secpolicy_write, dataset_name }, 1210 { zfs_ioc_rename, zfs_secpolicy_write, dataset_name }, 1211 { zfs_ioc_recvbackup, zfs_secpolicy_write, dataset_name }, 1212 { zfs_ioc_sendbackup, zfs_secpolicy_write, dataset_name }, 1213 { zfs_ioc_inject_fault, zfs_secpolicy_inject, no_name }, 1214 { zfs_ioc_clear_fault, zfs_secpolicy_inject, no_name }, 1215 { zfs_ioc_inject_list_next, zfs_secpolicy_inject, no_name }, 1216 { zfs_ioc_error_log, zfs_secpolicy_inject, pool_name }, 1217 { zfs_ioc_clear, zfs_secpolicy_config, pool_name }, 1218 { zfs_ioc_bookmark_name, zfs_secpolicy_inject, pool_name } 1219 }; 1220 1221 static int 1222 zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 1223 { 1224 zfs_cmd_t *zc; 1225 uint_t vec; 1226 int error; 1227 1228 if (getminor(dev) != 0) 1229 return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp)); 1230 1231 vec = cmd - ZFS_IOC; 1232 1233 if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0])) 1234 return (EINVAL); 1235 1236 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP); 1237 1238 error = xcopyin((void *)arg, zc, sizeof (zfs_cmd_t)); 1239 1240 if (error == 0) { 1241 zc->zc_cred = (uintptr_t)cr; 1242 zc->zc_dev = dev; 1243 error = zfs_ioc_vec[vec].zvec_secpolicy(zc->zc_name, 1244 zc->zc_prop_name, cr); 1245 } 1246 1247 /* 1248 * Ensure that all pool/dataset names are valid before we pass down to 1249 * the lower layers. 1250 */ 1251 if (error == 0) { 1252 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0'; 1253 switch (zfs_ioc_vec[vec].zvec_namecheck) { 1254 case pool_name: 1255 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0) 1256 error = EINVAL; 1257 break; 1258 1259 case dataset_name: 1260 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0) 1261 error = EINVAL; 1262 break; 1263 } 1264 } 1265 1266 if (error == 0) 1267 error = zfs_ioc_vec[vec].zvec_func(zc); 1268 1269 if (error == 0 || error == ENOMEM) { 1270 int rc = xcopyout(zc, (void *)arg, sizeof (zfs_cmd_t)); 1271 if (error == 0) 1272 error = rc; 1273 } 1274 1275 kmem_free(zc, sizeof (zfs_cmd_t)); 1276 return (error); 1277 } 1278 1279 static int 1280 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 1281 { 1282 if (cmd != DDI_ATTACH) 1283 return (DDI_FAILURE); 1284 1285 if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0, 1286 DDI_PSEUDO, 0) == DDI_FAILURE) 1287 return (DDI_FAILURE); 1288 1289 zfs_dip = dip; 1290 1291 ddi_report_dev(dip); 1292 1293 return (DDI_SUCCESS); 1294 } 1295 1296 static int 1297 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 1298 { 1299 if (spa_busy() || zfs_busy() || zvol_busy()) 1300 return (DDI_FAILURE); 1301 1302 if (cmd != DDI_DETACH) 1303 return (DDI_FAILURE); 1304 1305 zfs_dip = NULL; 1306 1307 ddi_prop_remove_all(dip); 1308 ddi_remove_minor_node(dip, NULL); 1309 1310 return (DDI_SUCCESS); 1311 } 1312 1313 /*ARGSUSED*/ 1314 static int 1315 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 1316 { 1317 switch (infocmd) { 1318 case DDI_INFO_DEVT2DEVINFO: 1319 *result = zfs_dip; 1320 return (DDI_SUCCESS); 1321 1322 case DDI_INFO_DEVT2INSTANCE: 1323 *result = (void *)0; 1324 return (DDI_SUCCESS); 1325 } 1326 1327 return (DDI_FAILURE); 1328 } 1329 1330 /* 1331 * OK, so this is a little weird. 1332 * 1333 * /dev/zfs is the control node, i.e. minor 0. 1334 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0. 1335 * 1336 * /dev/zfs has basically nothing to do except serve up ioctls, 1337 * so most of the standard driver entry points are in zvol.c. 1338 */ 1339 static struct cb_ops zfs_cb_ops = { 1340 zvol_open, /* open */ 1341 zvol_close, /* close */ 1342 zvol_strategy, /* strategy */ 1343 nodev, /* print */ 1344 nodev, /* dump */ 1345 zvol_read, /* read */ 1346 zvol_write, /* write */ 1347 zfsdev_ioctl, /* ioctl */ 1348 nodev, /* devmap */ 1349 nodev, /* mmap */ 1350 nodev, /* segmap */ 1351 nochpoll, /* poll */ 1352 ddi_prop_op, /* prop_op */ 1353 NULL, /* streamtab */ 1354 D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */ 1355 CB_REV, /* version */ 1356 zvol_aread, /* async read */ 1357 zvol_awrite, /* async write */ 1358 }; 1359 1360 static struct dev_ops zfs_dev_ops = { 1361 DEVO_REV, /* version */ 1362 0, /* refcnt */ 1363 zfs_info, /* info */ 1364 nulldev, /* identify */ 1365 nulldev, /* probe */ 1366 zfs_attach, /* attach */ 1367 zfs_detach, /* detach */ 1368 nodev, /* reset */ 1369 &zfs_cb_ops, /* driver operations */ 1370 NULL /* no bus operations */ 1371 }; 1372 1373 static struct modldrv zfs_modldrv = { 1374 &mod_driverops, "ZFS storage pool version 1", &zfs_dev_ops 1375 }; 1376 1377 static struct modlinkage modlinkage = { 1378 MODREV_1, 1379 (void *)&zfs_modlfs, 1380 (void *)&zfs_modldrv, 1381 NULL 1382 }; 1383 1384 int 1385 _init(void) 1386 { 1387 int error; 1388 1389 spa_init(FREAD | FWRITE); 1390 zfs_init(); 1391 zvol_init(); 1392 1393 if ((error = mod_install(&modlinkage)) != 0) { 1394 zvol_fini(); 1395 zfs_fini(); 1396 spa_fini(); 1397 return (error); 1398 } 1399 1400 error = ldi_ident_from_mod(&modlinkage, &zfs_li); 1401 ASSERT(error == 0); 1402 1403 return (0); 1404 } 1405 1406 int 1407 _fini(void) 1408 { 1409 int error; 1410 1411 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled) 1412 return (EBUSY); 1413 1414 if ((error = mod_remove(&modlinkage)) != 0) 1415 return (error); 1416 1417 zvol_fini(); 1418 zfs_fini(); 1419 spa_fini(); 1420 1421 ldi_ident_release(zfs_li); 1422 zfs_li = NULL; 1423 1424 return (error); 1425 } 1426 1427 int 1428 _info(struct modinfo *modinfop) 1429 { 1430 return (mod_info(&modlinkage, modinfop)); 1431 } 1432