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