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, NULL)); 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_stats(zfs_cmd_t *zc) 428 { 429 nvlist_t *config; 430 char *packed = NULL; 431 size_t size = 0; 432 int error; 433 int ret = 0; 434 435 error = spa_get_stats(zc->zc_name, &config, zc->zc_root, 436 sizeof (zc->zc_root)); 437 438 if (config != NULL) { 439 VERIFY(nvlist_pack(config, &packed, &size, 440 NV_ENCODE_NATIVE, KM_SLEEP) == 0); 441 442 if (size > zc->zc_config_dst_size) 443 ret = ENOMEM; 444 else if (xcopyout(packed, (void *)(uintptr_t)zc->zc_config_dst, 445 size)) 446 ret = EFAULT; 447 448 zc->zc_config_dst_size = size; 449 450 kmem_free(packed, size); 451 nvlist_free(config); 452 453 /* 454 * The config may be present even if 'error' is non-zero. 455 * In this case we return success, and preserve the real errno 456 * in 'zc_cookie'. 457 */ 458 zc->zc_cookie = error; 459 } else { 460 ret = error; 461 } 462 463 return (ret); 464 } 465 466 /* 467 * Try to import the given pool, returning pool stats as appropriate so that 468 * user land knows which devices are available and overall pool health. 469 */ 470 static int 471 zfs_ioc_pool_tryimport(zfs_cmd_t *zc) 472 { 473 nvlist_t *tryconfig, *config; 474 char *packed = NULL; 475 size_t size = 0; 476 int error; 477 478 if ((error = get_config(zc, &tryconfig)) != 0) 479 return (error); 480 481 config = spa_tryimport(tryconfig); 482 483 nvlist_free(tryconfig); 484 485 if (config == NULL) 486 return (EINVAL); 487 488 VERIFY(nvlist_pack(config, &packed, &size, NV_ENCODE_NATIVE, 489 KM_SLEEP) == 0); 490 491 if (size > zc->zc_config_dst_size) 492 error = ENOMEM; 493 else 494 error = xcopyout(packed, (void *)(uintptr_t)zc->zc_config_dst, 495 size); 496 497 zc->zc_config_dst_size = size; 498 499 kmem_free(packed, size); 500 nvlist_free(config); 501 502 return (error); 503 } 504 505 static int 506 zfs_ioc_pool_scrub(zfs_cmd_t *zc) 507 { 508 spa_t *spa; 509 int error; 510 511 error = spa_open(zc->zc_name, &spa, FTAG); 512 if (error == 0) { 513 error = spa_scrub(spa, zc->zc_cookie, B_FALSE); 514 spa_close(spa, FTAG); 515 } 516 return (error); 517 } 518 519 static int 520 zfs_ioc_pool_freeze(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 spa_freeze(spa); 528 spa_close(spa, FTAG); 529 } 530 return (error); 531 } 532 533 static int 534 zfs_ioc_pool_upgrade(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_upgrade(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 static int 568 zfs_ioc_vdev_remove(zfs_cmd_t *zc) 569 { 570 spa_t *spa; 571 int error; 572 573 error = spa_open(zc->zc_name, &spa, FTAG); 574 if (error != 0) 575 return (error); 576 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE); 577 spa_close(spa, FTAG); 578 return (error); 579 } 580 581 static int 582 zfs_ioc_vdev_online(zfs_cmd_t *zc) 583 { 584 spa_t *spa; 585 int error; 586 587 error = spa_open(zc->zc_name, &spa, FTAG); 588 if (error != 0) 589 return (error); 590 error = vdev_online(spa, zc->zc_guid); 591 spa_close(spa, FTAG); 592 return (error); 593 } 594 595 static int 596 zfs_ioc_vdev_offline(zfs_cmd_t *zc) 597 { 598 spa_t *spa; 599 int istmp = zc->zc_cookie; 600 int error; 601 602 error = spa_open(zc->zc_name, &spa, FTAG); 603 if (error != 0) 604 return (error); 605 error = vdev_offline(spa, zc->zc_guid, istmp); 606 spa_close(spa, FTAG); 607 return (error); 608 } 609 610 static int 611 zfs_ioc_vdev_attach(zfs_cmd_t *zc) 612 { 613 spa_t *spa; 614 int replacing = zc->zc_cookie; 615 nvlist_t *config; 616 int error; 617 618 error = spa_open(zc->zc_name, &spa, FTAG); 619 if (error != 0) 620 return (error); 621 622 if ((error = get_config(zc, &config)) == 0) { 623 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing); 624 nvlist_free(config); 625 } 626 627 spa_close(spa, FTAG); 628 return (error); 629 } 630 631 static int 632 zfs_ioc_vdev_detach(zfs_cmd_t *zc) 633 { 634 spa_t *spa; 635 int error; 636 637 error = spa_open(zc->zc_name, &spa, FTAG); 638 if (error != 0) 639 return (error); 640 641 error = spa_vdev_detach(spa, zc->zc_guid, B_FALSE); 642 643 spa_close(spa, FTAG); 644 return (error); 645 } 646 647 static int 648 zfs_ioc_vdev_setpath(zfs_cmd_t *zc) 649 { 650 spa_t *spa; 651 char *path = zc->zc_prop_value; 652 uint64_t guid = zc->zc_guid; 653 int error; 654 655 error = spa_open(zc->zc_name, &spa, FTAG); 656 if (error != 0) 657 return (error); 658 659 error = spa_vdev_setpath(spa, guid, path); 660 661 spa_close(spa, FTAG); 662 return (error); 663 } 664 665 666 static int 667 zfs_ioc_objset_stats(zfs_cmd_t *zc) 668 { 669 objset_t *os = NULL; 670 int error; 671 nvlist_t *nv; 672 size_t sz; 673 char *buf; 674 675 retry: 676 error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, 677 DS_MODE_STANDARD | DS_MODE_READONLY, &os); 678 if (error != 0) { 679 /* 680 * This is ugly: dmu_objset_open() can return EBUSY if 681 * the objset is held exclusively. Fortunately this hold is 682 * only for a short while, so we retry here. 683 * This avoids user code having to handle EBUSY, 684 * for example for a "zfs list". 685 */ 686 if (error == EBUSY) { 687 delay(1); 688 goto retry; 689 } 690 return (error); 691 } 692 693 dmu_objset_stats(os, &zc->zc_objset_stats); 694 695 if (zc->zc_config_src != NULL && 696 (error = dsl_prop_get_all(os, &nv)) == 0) { 697 VERIFY(nvlist_size(nv, &sz, NV_ENCODE_NATIVE) == 0); 698 if (sz > zc->zc_config_src_size) { 699 zc->zc_config_src_size = sz; 700 error = ENOMEM; 701 } else { 702 buf = kmem_alloc(sz, KM_SLEEP); 703 VERIFY(nvlist_pack(nv, &buf, &sz, 704 NV_ENCODE_NATIVE, 0) == 0); 705 error = xcopyout(buf, 706 (void *)(uintptr_t)zc->zc_config_src, sz); 707 kmem_free(buf, sz); 708 } 709 nvlist_free(nv); 710 } 711 712 if (!error && zc->zc_objset_stats.dds_type == DMU_OST_ZVOL) 713 error = zvol_get_stats(zc, os); 714 715 spa_altroot(dmu_objset_spa(os), zc->zc_root, sizeof (zc->zc_root)); 716 717 dmu_objset_close(os); 718 return (error); 719 } 720 721 static int 722 zfs_ioc_dataset_list_next(zfs_cmd_t *zc) 723 { 724 objset_t *os; 725 int error; 726 char *p; 727 728 retry: 729 error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, 730 DS_MODE_STANDARD | DS_MODE_READONLY, &os); 731 if (error != 0) { 732 /* 733 * This is ugly: dmu_objset_open() can return EBUSY if 734 * the objset is held exclusively. Fortunately this hold is 735 * only for a short while, so we retry here. 736 * This avoids user code having to handle EBUSY, 737 * for example for a "zfs list". 738 */ 739 if (error == EBUSY) { 740 delay(1); 741 goto retry; 742 } 743 if (error == ENOENT) 744 error = ESRCH; 745 return (error); 746 } 747 748 p = strrchr(zc->zc_name, '/'); 749 if (p == NULL || p[1] != '\0') 750 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name)); 751 p = zc->zc_name + strlen(zc->zc_name); 752 753 do { 754 error = dmu_dir_list_next(os, 755 sizeof (zc->zc_name) - (p - zc->zc_name), p, 756 NULL, &zc->zc_cookie); 757 if (error == ENOENT) 758 error = ESRCH; 759 } while (error == 0 && !INGLOBALZONE(curproc) && 760 !zone_dataset_visible(zc->zc_name, NULL)); 761 762 /* 763 * If it's a hidden dataset (ie. with a '$' in its name), don't 764 * try to get stats for it. Userland will skip over it. 765 */ 766 if (error == 0 && strchr(zc->zc_name, '$') == NULL) 767 error = zfs_ioc_objset_stats(zc); /* fill in the stats */ 768 769 dmu_objset_close(os); 770 return (error); 771 } 772 773 static int 774 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc) 775 { 776 objset_t *os; 777 int error; 778 779 retry: 780 error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, 781 DS_MODE_STANDARD | DS_MODE_READONLY, &os); 782 if (error != 0) { 783 /* 784 * This is ugly: dmu_objset_open() can return EBUSY if 785 * the objset is held exclusively. Fortunately this hold is 786 * only for a short while, so we retry here. 787 * This avoids user code having to handle EBUSY, 788 * for example for a "zfs list". 789 */ 790 if (error == EBUSY) { 791 delay(1); 792 goto retry; 793 } 794 if (error == ENOENT) 795 error = ESRCH; 796 return (error); 797 } 798 799 /* 800 * A dataset name of maximum length cannot have any snapshots, 801 * so exit immediately. 802 */ 803 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) { 804 dmu_objset_close(os); 805 return (ESRCH); 806 } 807 808 error = dmu_snapshot_list_next(os, 809 sizeof (zc->zc_name) - strlen(zc->zc_name), 810 zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie); 811 if (error == ENOENT) 812 error = ESRCH; 813 814 if (error == 0) 815 error = zfs_ioc_objset_stats(zc); /* fill in the stats */ 816 817 dmu_objset_close(os); 818 return (error); 819 } 820 821 static int 822 zfs_ioc_set_prop(zfs_cmd_t *zc) 823 { 824 return (dsl_prop_set(zc->zc_name, zc->zc_prop_name, 825 zc->zc_intsz, zc->zc_numints, zc->zc_prop_value)); 826 } 827 828 static int 829 zfs_ioc_set_quota(zfs_cmd_t *zc) 830 { 831 return (dsl_dir_set_quota(zc->zc_name, zc->zc_cookie)); 832 } 833 834 static int 835 zfs_ioc_set_reservation(zfs_cmd_t *zc) 836 { 837 return (dsl_dir_set_reservation(zc->zc_name, zc->zc_cookie)); 838 } 839 840 static int 841 zfs_ioc_set_volsize(zfs_cmd_t *zc) 842 { 843 return (zvol_set_volsize(zc)); 844 } 845 846 static int 847 zfs_ioc_set_volblocksize(zfs_cmd_t *zc) 848 { 849 return (zvol_set_volblocksize(zc)); 850 } 851 852 static int 853 zfs_ioc_create_minor(zfs_cmd_t *zc) 854 { 855 return (zvol_create_minor(zc)); 856 } 857 858 static int 859 zfs_ioc_remove_minor(zfs_cmd_t *zc) 860 { 861 return (zvol_remove_minor(zc)); 862 } 863 864 /* 865 * Search the vfs list for a specified resource. Returns a pointer to it 866 * or NULL if no suitable entry is found. The caller of this routine 867 * is responsible for releasing the returned vfs pointer. 868 */ 869 static vfs_t * 870 zfs_get_vfs(const char *resource) 871 { 872 struct vfs *vfsp; 873 struct vfs *vfs_found = NULL; 874 875 vfs_list_read_lock(); 876 vfsp = rootvfs; 877 do { 878 if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) { 879 VFS_HOLD(vfsp); 880 vfs_found = vfsp; 881 break; 882 } 883 vfsp = vfsp->vfs_next; 884 } while (vfsp != rootvfs); 885 vfs_list_unlock(); 886 return (vfs_found); 887 } 888 889 static void 890 zfs_create_cb(objset_t *os, void *arg, dmu_tx_t *tx) 891 { 892 zfs_cmd_t *zc = arg; 893 zfs_create_fs(os, (cred_t *)(uintptr_t)zc->zc_cred, tx); 894 } 895 896 static int 897 zfs_ioc_create(zfs_cmd_t *zc) 898 { 899 objset_t *clone; 900 int error = 0; 901 void (*cbfunc)(objset_t *os, void *arg, dmu_tx_t *tx); 902 dmu_objset_type_t type = zc->zc_objset_type; 903 904 switch (type) { 905 906 case DMU_OST_ZFS: 907 cbfunc = zfs_create_cb; 908 break; 909 910 case DMU_OST_ZVOL: 911 cbfunc = zvol_create_cb; 912 break; 913 914 default: 915 return (EINVAL); 916 } 917 918 if (zc->zc_filename[0] != '\0') { 919 /* 920 * We're creating a clone of an existing snapshot. 921 */ 922 zc->zc_filename[sizeof (zc->zc_filename) - 1] = '\0'; 923 if (dataset_namecheck(zc->zc_filename, NULL, NULL) != 0) 924 return (EINVAL); 925 926 error = dmu_objset_open(zc->zc_filename, type, 927 DS_MODE_STANDARD | DS_MODE_READONLY, &clone); 928 if (error) 929 return (error); 930 error = dmu_objset_create(zc->zc_name, type, clone, NULL, NULL); 931 dmu_objset_close(clone); 932 } else if (strchr(zc->zc_name, '@') != 0) { 933 /* 934 * We're taking a snapshot of an existing dataset. 935 */ 936 error = dmu_objset_create(zc->zc_name, type, NULL, NULL, NULL); 937 } else { 938 /* 939 * We're creating a new dataset. 940 */ 941 if (type == DMU_OST_ZVOL) { 942 943 if ((error = zvol_check_volblocksize(zc)) != 0) 944 return (error); 945 946 if ((error = zvol_check_volsize(zc, 947 zc->zc_volblocksize)) != 0) 948 return (error); 949 } 950 error = dmu_objset_create(zc->zc_name, type, NULL, cbfunc, zc); 951 } 952 return (error); 953 } 954 955 static int 956 zfs_ioc_destroy(zfs_cmd_t *zc) 957 { 958 if (strchr(zc->zc_name, '@') != NULL && 959 zc->zc_objset_type == DMU_OST_ZFS) { 960 vfs_t *vfsp; 961 int err; 962 963 /* 964 * Snapshots under .zfs control must be unmounted 965 * before they can be destroyed. 966 */ 967 if ((vfsp = zfs_get_vfs(zc->zc_name)) != NULL) { 968 /* 969 * Always force the unmount for snapshots. 970 */ 971 int flag = MS_FORCE; 972 973 if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) { 974 VFS_RELE(vfsp); 975 return (err); 976 } 977 VFS_RELE(vfsp); 978 if ((err = dounmount(vfsp, flag, kcred)) != 0) 979 return (err); 980 } 981 } 982 983 return (dmu_objset_destroy(zc->zc_name)); 984 } 985 986 static int 987 zfs_ioc_rollback(zfs_cmd_t *zc) 988 { 989 return (dmu_objset_rollback(zc->zc_name)); 990 } 991 992 static int 993 zfs_ioc_rename(zfs_cmd_t *zc) 994 { 995 zc->zc_prop_value[sizeof (zc->zc_prop_value) - 1] = '\0'; 996 if (dataset_namecheck(zc->zc_prop_value, NULL, NULL) != 0) 997 return (EINVAL); 998 999 if (strchr(zc->zc_name, '@') != NULL && 1000 zc->zc_objset_type == DMU_OST_ZFS) { 1001 vfs_t *vfsp; 1002 int err; 1003 1004 /* 1005 * Snapshots under .zfs control must be unmounted 1006 * before they can be renamed. 1007 */ 1008 if ((vfsp = zfs_get_vfs(zc->zc_name)) != NULL) { 1009 /* 1010 * Always force the unmount for snapshots. 1011 */ 1012 int flag = MS_FORCE; 1013 1014 if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) { 1015 VFS_RELE(vfsp); 1016 return (err); 1017 } 1018 VFS_RELE(vfsp); 1019 if ((err = dounmount(vfsp, flag, kcred)) != 0) 1020 return (err); 1021 } 1022 } 1023 1024 return (dmu_objset_rename(zc->zc_name, zc->zc_prop_value)); 1025 } 1026 1027 static int 1028 zfs_ioc_recvbackup(zfs_cmd_t *zc) 1029 { 1030 file_t *fp; 1031 int error, fd; 1032 1033 fd = zc->zc_cookie; 1034 fp = getf(fd); 1035 if (fp == NULL) 1036 return (EBADF); 1037 error = dmu_recvbackup(zc->zc_filename, &zc->zc_begin_record, 1038 &zc->zc_cookie, fp->f_vnode, fp->f_offset); 1039 releasef(fd); 1040 return (error); 1041 } 1042 1043 static int 1044 zfs_ioc_sendbackup(zfs_cmd_t *zc) 1045 { 1046 objset_t *fromsnap = NULL; 1047 objset_t *tosnap; 1048 file_t *fp; 1049 int error; 1050 1051 error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, 1052 DS_MODE_STANDARD | DS_MODE_READONLY, &tosnap); 1053 if (error) 1054 return (error); 1055 1056 if (zc->zc_prop_value[0] != '\0') { 1057 error = dmu_objset_open(zc->zc_prop_value, DMU_OST_ANY, 1058 DS_MODE_STANDARD | DS_MODE_READONLY, &fromsnap); 1059 if (error) { 1060 dmu_objset_close(tosnap); 1061 return (error); 1062 } 1063 } 1064 1065 fp = getf(zc->zc_cookie); 1066 if (fp == NULL) { 1067 dmu_objset_close(tosnap); 1068 if (fromsnap) 1069 dmu_objset_close(fromsnap); 1070 return (EBADF); 1071 } 1072 1073 error = dmu_sendbackup(tosnap, fromsnap, fp->f_vnode); 1074 1075 releasef(zc->zc_cookie); 1076 if (fromsnap) 1077 dmu_objset_close(fromsnap); 1078 dmu_objset_close(tosnap); 1079 return (error); 1080 } 1081 1082 static int 1083 zfs_ioc_inject_fault(zfs_cmd_t *zc) 1084 { 1085 int id, error; 1086 1087 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id, 1088 &zc->zc_inject_record); 1089 1090 if (error == 0) 1091 zc->zc_guid = (uint64_t)id; 1092 1093 return (error); 1094 } 1095 1096 static int 1097 zfs_ioc_clear_fault(zfs_cmd_t *zc) 1098 { 1099 return (zio_clear_fault((int)zc->zc_guid)); 1100 } 1101 1102 static int 1103 zfs_ioc_inject_list_next(zfs_cmd_t *zc) 1104 { 1105 int id = (int)zc->zc_guid; 1106 int error; 1107 1108 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name), 1109 &zc->zc_inject_record); 1110 1111 zc->zc_guid = id; 1112 1113 return (error); 1114 } 1115 1116 static int 1117 zfs_ioc_error_log(zfs_cmd_t *zc) 1118 { 1119 spa_t *spa; 1120 int error; 1121 size_t count = (size_t)zc->zc_config_dst_size; 1122 1123 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1124 return (error); 1125 1126 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_config_dst, 1127 &count); 1128 if (error == 0) 1129 zc->zc_config_dst_size = count; 1130 else 1131 zc->zc_config_dst_size = spa_get_errlog_size(spa); 1132 1133 spa_close(spa, FTAG); 1134 1135 return (error); 1136 } 1137 1138 static int 1139 zfs_ioc_clear(zfs_cmd_t *zc) 1140 { 1141 spa_t *spa; 1142 vdev_t *vd; 1143 int error; 1144 1145 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1146 return (error); 1147 1148 spa_config_enter(spa, RW_WRITER, FTAG); 1149 1150 if (zc->zc_prop_value[0] == '\0') 1151 vd = NULL; 1152 else if ((vd = spa_lookup_by_guid(spa, zc->zc_guid)) == NULL) { 1153 spa_config_exit(spa, FTAG); 1154 spa_close(spa, FTAG); 1155 return (ENODEV); 1156 } 1157 1158 vdev_clear(spa, vd); 1159 1160 spa_config_exit(spa, FTAG); 1161 1162 spa_close(spa, FTAG); 1163 1164 return (0); 1165 } 1166 1167 static int 1168 zfs_ioc_bookmark_name(zfs_cmd_t *zc) 1169 { 1170 spa_t *spa; 1171 int error; 1172 1173 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1174 return (error); 1175 1176 error = spa_bookmark_name(spa, &zc->zc_bookmark, 1177 zc->zc_prop_name, sizeof (zc->zc_prop_name), zc->zc_prop_value, 1178 sizeof (zc->zc_prop_value), zc->zc_filename, 1179 sizeof (zc->zc_filename)); 1180 1181 spa_close(spa, FTAG); 1182 1183 return (error); 1184 } 1185 1186 static int 1187 zfs_ioc_promote(zfs_cmd_t *zc) 1188 { 1189 return (dsl_dataset_promote(zc->zc_name)); 1190 } 1191 1192 static zfs_ioc_vec_t zfs_ioc_vec[] = { 1193 { zfs_ioc_pool_create, zfs_secpolicy_config, pool_name }, 1194 { zfs_ioc_pool_destroy, zfs_secpolicy_config, pool_name }, 1195 { zfs_ioc_pool_import, zfs_secpolicy_config, pool_name }, 1196 { zfs_ioc_pool_export, zfs_secpolicy_config, pool_name }, 1197 { zfs_ioc_pool_configs, zfs_secpolicy_none, no_name }, 1198 { zfs_ioc_pool_stats, zfs_secpolicy_read, pool_name }, 1199 { zfs_ioc_pool_tryimport, zfs_secpolicy_config, no_name }, 1200 { zfs_ioc_pool_scrub, zfs_secpolicy_config, pool_name }, 1201 { zfs_ioc_pool_freeze, zfs_secpolicy_config, no_name }, 1202 { zfs_ioc_pool_upgrade, zfs_secpolicy_config, pool_name }, 1203 { zfs_ioc_vdev_add, zfs_secpolicy_config, pool_name }, 1204 { zfs_ioc_vdev_remove, zfs_secpolicy_config, pool_name }, 1205 { zfs_ioc_vdev_online, zfs_secpolicy_config, pool_name }, 1206 { zfs_ioc_vdev_offline, zfs_secpolicy_config, pool_name }, 1207 { zfs_ioc_vdev_attach, zfs_secpolicy_config, pool_name }, 1208 { zfs_ioc_vdev_detach, zfs_secpolicy_config, pool_name }, 1209 { zfs_ioc_vdev_setpath, zfs_secpolicy_config, pool_name }, 1210 { zfs_ioc_objset_stats, zfs_secpolicy_read, dataset_name }, 1211 { zfs_ioc_dataset_list_next, zfs_secpolicy_read, dataset_name }, 1212 { zfs_ioc_snapshot_list_next, zfs_secpolicy_read, dataset_name }, 1213 { zfs_ioc_set_prop, zfs_secpolicy_setprop, dataset_name }, 1214 { zfs_ioc_set_quota, zfs_secpolicy_quota, dataset_name }, 1215 { zfs_ioc_set_reservation, zfs_secpolicy_write, dataset_name }, 1216 { zfs_ioc_set_volsize, zfs_secpolicy_config, dataset_name }, 1217 { zfs_ioc_set_volblocksize, zfs_secpolicy_config, dataset_name }, 1218 { zfs_ioc_create_minor, zfs_secpolicy_config, dataset_name }, 1219 { zfs_ioc_remove_minor, zfs_secpolicy_config, dataset_name }, 1220 { zfs_ioc_create, zfs_secpolicy_parent, dataset_name }, 1221 { zfs_ioc_destroy, zfs_secpolicy_parent, dataset_name }, 1222 { zfs_ioc_rollback, zfs_secpolicy_write, dataset_name }, 1223 { zfs_ioc_rename, zfs_secpolicy_write, dataset_name }, 1224 { zfs_ioc_recvbackup, zfs_secpolicy_write, dataset_name }, 1225 { zfs_ioc_sendbackup, zfs_secpolicy_write, dataset_name }, 1226 { zfs_ioc_inject_fault, zfs_secpolicy_inject, no_name }, 1227 { zfs_ioc_clear_fault, zfs_secpolicy_inject, no_name }, 1228 { zfs_ioc_inject_list_next, zfs_secpolicy_inject, no_name }, 1229 { zfs_ioc_error_log, zfs_secpolicy_inject, pool_name }, 1230 { zfs_ioc_clear, zfs_secpolicy_config, pool_name }, 1231 { zfs_ioc_bookmark_name, zfs_secpolicy_inject, pool_name }, 1232 { zfs_ioc_promote, zfs_secpolicy_write, dataset_name } 1233 }; 1234 1235 static int 1236 zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 1237 { 1238 zfs_cmd_t *zc; 1239 uint_t vec; 1240 int error; 1241 1242 if (getminor(dev) != 0) 1243 return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp)); 1244 1245 vec = cmd - ZFS_IOC; 1246 1247 if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0])) 1248 return (EINVAL); 1249 1250 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP); 1251 1252 error = xcopyin((void *)arg, zc, sizeof (zfs_cmd_t)); 1253 1254 if (error == 0) { 1255 zc->zc_cred = (uintptr_t)cr; 1256 zc->zc_dev = dev; 1257 error = zfs_ioc_vec[vec].zvec_secpolicy(zc->zc_name, 1258 zc->zc_prop_name, cr); 1259 } 1260 1261 /* 1262 * Ensure that all pool/dataset names are valid before we pass down to 1263 * the lower layers. 1264 */ 1265 if (error == 0) { 1266 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0'; 1267 switch (zfs_ioc_vec[vec].zvec_namecheck) { 1268 case pool_name: 1269 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0) 1270 error = EINVAL; 1271 break; 1272 1273 case dataset_name: 1274 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0) 1275 error = EINVAL; 1276 break; 1277 } 1278 } 1279 1280 if (error == 0) 1281 error = zfs_ioc_vec[vec].zvec_func(zc); 1282 1283 if (error == 0 || error == ENOMEM) { 1284 int rc = xcopyout(zc, (void *)arg, sizeof (zfs_cmd_t)); 1285 if (error == 0) 1286 error = rc; 1287 } 1288 1289 kmem_free(zc, sizeof (zfs_cmd_t)); 1290 return (error); 1291 } 1292 1293 static int 1294 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 1295 { 1296 if (cmd != DDI_ATTACH) 1297 return (DDI_FAILURE); 1298 1299 if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0, 1300 DDI_PSEUDO, 0) == DDI_FAILURE) 1301 return (DDI_FAILURE); 1302 1303 zfs_dip = dip; 1304 1305 ddi_report_dev(dip); 1306 1307 return (DDI_SUCCESS); 1308 } 1309 1310 static int 1311 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 1312 { 1313 if (spa_busy() || zfs_busy() || zvol_busy()) 1314 return (DDI_FAILURE); 1315 1316 if (cmd != DDI_DETACH) 1317 return (DDI_FAILURE); 1318 1319 zfs_dip = NULL; 1320 1321 ddi_prop_remove_all(dip); 1322 ddi_remove_minor_node(dip, NULL); 1323 1324 return (DDI_SUCCESS); 1325 } 1326 1327 /*ARGSUSED*/ 1328 static int 1329 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 1330 { 1331 switch (infocmd) { 1332 case DDI_INFO_DEVT2DEVINFO: 1333 *result = zfs_dip; 1334 return (DDI_SUCCESS); 1335 1336 case DDI_INFO_DEVT2INSTANCE: 1337 *result = (void *)0; 1338 return (DDI_SUCCESS); 1339 } 1340 1341 return (DDI_FAILURE); 1342 } 1343 1344 /* 1345 * OK, so this is a little weird. 1346 * 1347 * /dev/zfs is the control node, i.e. minor 0. 1348 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0. 1349 * 1350 * /dev/zfs has basically nothing to do except serve up ioctls, 1351 * so most of the standard driver entry points are in zvol.c. 1352 */ 1353 static struct cb_ops zfs_cb_ops = { 1354 zvol_open, /* open */ 1355 zvol_close, /* close */ 1356 zvol_strategy, /* strategy */ 1357 nodev, /* print */ 1358 nodev, /* dump */ 1359 zvol_read, /* read */ 1360 zvol_write, /* write */ 1361 zfsdev_ioctl, /* ioctl */ 1362 nodev, /* devmap */ 1363 nodev, /* mmap */ 1364 nodev, /* segmap */ 1365 nochpoll, /* poll */ 1366 ddi_prop_op, /* prop_op */ 1367 NULL, /* streamtab */ 1368 D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */ 1369 CB_REV, /* version */ 1370 zvol_aread, /* async read */ 1371 zvol_awrite, /* async write */ 1372 }; 1373 1374 static struct dev_ops zfs_dev_ops = { 1375 DEVO_REV, /* version */ 1376 0, /* refcnt */ 1377 zfs_info, /* info */ 1378 nulldev, /* identify */ 1379 nulldev, /* probe */ 1380 zfs_attach, /* attach */ 1381 zfs_detach, /* detach */ 1382 nodev, /* reset */ 1383 &zfs_cb_ops, /* driver operations */ 1384 NULL /* no bus operations */ 1385 }; 1386 1387 static struct modldrv zfs_modldrv = { 1388 &mod_driverops, "ZFS storage pool version 1", &zfs_dev_ops 1389 }; 1390 1391 static struct modlinkage modlinkage = { 1392 MODREV_1, 1393 (void *)&zfs_modlfs, 1394 (void *)&zfs_modldrv, 1395 NULL 1396 }; 1397 1398 int 1399 _init(void) 1400 { 1401 int error; 1402 1403 spa_init(FREAD | FWRITE); 1404 zfs_init(); 1405 zvol_init(); 1406 1407 if ((error = mod_install(&modlinkage)) != 0) { 1408 zvol_fini(); 1409 zfs_fini(); 1410 spa_fini(); 1411 return (error); 1412 } 1413 1414 error = ldi_ident_from_mod(&modlinkage, &zfs_li); 1415 ASSERT(error == 0); 1416 1417 return (0); 1418 } 1419 1420 int 1421 _fini(void) 1422 { 1423 int error; 1424 1425 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled) 1426 return (EBUSY); 1427 1428 if ((error = mod_remove(&modlinkage)) != 0) 1429 return (error); 1430 1431 zvol_fini(); 1432 zfs_fini(); 1433 spa_fini(); 1434 1435 ldi_ident_release(zfs_li); 1436 zfs_li = NULL; 1437 1438 return (error); 1439 } 1440 1441 int 1442 _info(struct modinfo *modinfop) 1443 { 1444 return (mod_info(&modlinkage, modinfop)); 1445 } 1446