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 2008 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/zfs_znode.h> 42 #include <sys/zap.h> 43 #include <sys/spa.h> 44 #include <sys/spa_impl.h> 45 #include <sys/vdev.h> 46 #include <sys/vdev_impl.h> 47 #include <sys/dmu.h> 48 #include <sys/dsl_dir.h> 49 #include <sys/dsl_dataset.h> 50 #include <sys/dsl_prop.h> 51 #include <sys/dsl_deleg.h> 52 #include <sys/dmu_objset.h> 53 #include <sys/ddi.h> 54 #include <sys/sunddi.h> 55 #include <sys/sunldi.h> 56 #include <sys/policy.h> 57 #include <sys/zone.h> 58 #include <sys/nvpair.h> 59 #include <sys/pathname.h> 60 #include <sys/mount.h> 61 #include <sys/sdt.h> 62 #include <sys/fs/zfs.h> 63 #include <sys/zfs_ctldir.h> 64 #include <sys/zfs_dir.h> 65 #include <sys/zvol.h> 66 #include <sharefs/share.h> 67 #include <sys/dmu_objset.h> 68 69 #include "zfs_namecheck.h" 70 #include "zfs_prop.h" 71 #include "zfs_deleg.h" 72 73 extern struct modlfs zfs_modlfs; 74 75 extern void zfs_init(void); 76 extern void zfs_fini(void); 77 78 ldi_ident_t zfs_li = NULL; 79 dev_info_t *zfs_dip; 80 81 typedef int zfs_ioc_func_t(zfs_cmd_t *); 82 typedef int zfs_secpolicy_func_t(zfs_cmd_t *, cred_t *); 83 84 typedef struct zfs_ioc_vec { 85 zfs_ioc_func_t *zvec_func; 86 zfs_secpolicy_func_t *zvec_secpolicy; 87 enum { 88 NO_NAME, 89 POOL_NAME, 90 DATASET_NAME 91 } zvec_namecheck; 92 boolean_t zvec_his_log; 93 } zfs_ioc_vec_t; 94 95 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */ 96 void 97 __dprintf(const char *file, const char *func, int line, const char *fmt, ...) 98 { 99 const char *newfile; 100 char buf[256]; 101 va_list adx; 102 103 /* 104 * Get rid of annoying "../common/" prefix to filename. 105 */ 106 newfile = strrchr(file, '/'); 107 if (newfile != NULL) { 108 newfile = newfile + 1; /* Get rid of leading / */ 109 } else { 110 newfile = file; 111 } 112 113 va_start(adx, fmt); 114 (void) vsnprintf(buf, sizeof (buf), fmt, adx); 115 va_end(adx); 116 117 /* 118 * To get this data, use the zfs-dprintf probe as so: 119 * dtrace -q -n 'zfs-dprintf \ 120 * /stringof(arg0) == "dbuf.c"/ \ 121 * {printf("%s: %s", stringof(arg1), stringof(arg3))}' 122 * arg0 = file name 123 * arg1 = function name 124 * arg2 = line number 125 * arg3 = message 126 */ 127 DTRACE_PROBE4(zfs__dprintf, 128 char *, newfile, char *, func, int, line, char *, buf); 129 } 130 131 static void 132 history_str_free(char *buf) 133 { 134 kmem_free(buf, HIS_MAX_RECORD_LEN); 135 } 136 137 static char * 138 history_str_get(zfs_cmd_t *zc) 139 { 140 char *buf; 141 142 if (zc->zc_history == NULL) 143 return (NULL); 144 145 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP); 146 if (copyinstr((void *)(uintptr_t)zc->zc_history, 147 buf, HIS_MAX_RECORD_LEN, NULL) != 0) { 148 history_str_free(buf); 149 return (NULL); 150 } 151 152 buf[HIS_MAX_RECORD_LEN -1] = '\0'; 153 154 return (buf); 155 } 156 157 /* 158 * zfs_check_version 159 * 160 * Return non-zero if the spa version is less than requested version. 161 */ 162 static int 163 zfs_check_version(const char *name, int version) 164 { 165 166 spa_t *spa; 167 168 if (spa_open(name, &spa, FTAG) == 0) { 169 if (spa_version(spa) < version) { 170 spa_close(spa, FTAG); 171 return (1); 172 } 173 spa_close(spa, FTAG); 174 } 175 return (0); 176 } 177 178 /* 179 * zpl_check_version 180 * 181 * Return non-zero if the ZPL version is less than requested version. 182 */ 183 static int 184 zpl_check_version(const char *name, int version) 185 { 186 objset_t *os; 187 int rc = 1; 188 189 if (dmu_objset_open(name, DMU_OST_ANY, 190 DS_MODE_STANDARD | DS_MODE_READONLY, &os) == 0) { 191 uint64_t propversion; 192 193 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, 194 &propversion) == 0) { 195 rc = !(propversion >= version); 196 } 197 dmu_objset_close(os); 198 } 199 return (rc); 200 } 201 202 static void 203 zfs_log_history(zfs_cmd_t *zc) 204 { 205 spa_t *spa; 206 char *buf; 207 208 if ((buf = history_str_get(zc)) == NULL) 209 return; 210 211 if (spa_open(zc->zc_name, &spa, FTAG) == 0) { 212 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY) 213 (void) spa_history_log(spa, buf, LOG_CMD_NORMAL); 214 spa_close(spa, FTAG); 215 } 216 history_str_free(buf); 217 } 218 219 /* 220 * Policy for top-level read operations (list pools). Requires no privileges, 221 * and can be used in the local zone, as there is no associated dataset. 222 */ 223 /* ARGSUSED */ 224 static int 225 zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr) 226 { 227 return (0); 228 } 229 230 /* 231 * Policy for dataset read operations (list children, get statistics). Requires 232 * no privileges, but must be visible in the local zone. 233 */ 234 /* ARGSUSED */ 235 static int 236 zfs_secpolicy_read(zfs_cmd_t *zc, cred_t *cr) 237 { 238 if (INGLOBALZONE(curproc) || 239 zone_dataset_visible(zc->zc_name, NULL)) 240 return (0); 241 242 return (ENOENT); 243 } 244 245 static int 246 zfs_dozonecheck(const char *dataset, cred_t *cr) 247 { 248 uint64_t zoned; 249 int writable = 1; 250 251 /* 252 * The dataset must be visible by this zone -- check this first 253 * so they don't see EPERM on something they shouldn't know about. 254 */ 255 if (!INGLOBALZONE(curproc) && 256 !zone_dataset_visible(dataset, &writable)) 257 return (ENOENT); 258 259 if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL)) 260 return (ENOENT); 261 262 if (INGLOBALZONE(curproc)) { 263 /* 264 * If the fs is zoned, only root can access it from the 265 * global zone. 266 */ 267 if (secpolicy_zfs(cr) && zoned) 268 return (EPERM); 269 } else { 270 /* 271 * If we are in a local zone, the 'zoned' property must be set. 272 */ 273 if (!zoned) 274 return (EPERM); 275 276 /* must be writable by this zone */ 277 if (!writable) 278 return (EPERM); 279 } 280 return (0); 281 } 282 283 int 284 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr) 285 { 286 int error; 287 288 error = zfs_dozonecheck(name, cr); 289 if (error == 0) { 290 error = secpolicy_zfs(cr); 291 if (error) 292 error = dsl_deleg_access(name, perm, cr); 293 } 294 return (error); 295 } 296 297 static int 298 zfs_secpolicy_setprop(const char *name, zfs_prop_t prop, cred_t *cr) 299 { 300 /* 301 * Check permissions for special properties. 302 */ 303 switch (prop) { 304 case ZFS_PROP_ZONED: 305 /* 306 * Disallow setting of 'zoned' from within a local zone. 307 */ 308 if (!INGLOBALZONE(curproc)) 309 return (EPERM); 310 break; 311 312 case ZFS_PROP_QUOTA: 313 if (!INGLOBALZONE(curproc)) { 314 uint64_t zoned; 315 char setpoint[MAXNAMELEN]; 316 /* 317 * Unprivileged users are allowed to modify the 318 * quota on things *under* (ie. contained by) 319 * the thing they own. 320 */ 321 if (dsl_prop_get_integer(name, "zoned", &zoned, 322 setpoint)) 323 return (EPERM); 324 if (!zoned || strlen(name) <= strlen(setpoint)) 325 return (EPERM); 326 } 327 break; 328 } 329 330 return (zfs_secpolicy_write_perms(name, zfs_prop_to_name(prop), cr)); 331 } 332 333 int 334 zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr) 335 { 336 int error; 337 338 error = zfs_dozonecheck(zc->zc_name, cr); 339 if (error) 340 return (error); 341 342 /* 343 * permission to set permissions will be evaluated later in 344 * dsl_deleg_can_allow() 345 */ 346 return (0); 347 } 348 349 int 350 zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr) 351 { 352 int error; 353 error = zfs_secpolicy_write_perms(zc->zc_name, 354 ZFS_DELEG_PERM_ROLLBACK, cr); 355 if (error == 0) 356 error = zfs_secpolicy_write_perms(zc->zc_name, 357 ZFS_DELEG_PERM_MOUNT, cr); 358 return (error); 359 } 360 361 int 362 zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr) 363 { 364 return (zfs_secpolicy_write_perms(zc->zc_name, 365 ZFS_DELEG_PERM_SEND, cr)); 366 } 367 368 int 369 zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr) 370 { 371 if (!INGLOBALZONE(curproc)) 372 return (EPERM); 373 374 if (secpolicy_nfs(cr) == 0) { 375 return (0); 376 } else { 377 vnode_t *vp; 378 int error; 379 380 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE, 381 NO_FOLLOW, NULL, &vp)) != 0) 382 return (error); 383 384 /* Now make sure mntpnt and dataset are ZFS */ 385 386 if (vp->v_vfsp->vfs_fstype != zfsfstype || 387 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource), 388 zc->zc_name) != 0)) { 389 VN_RELE(vp); 390 return (EPERM); 391 } 392 393 VN_RELE(vp); 394 return (dsl_deleg_access(zc->zc_name, 395 ZFS_DELEG_PERM_SHARE, cr)); 396 } 397 } 398 399 static int 400 zfs_get_parent(const char *datasetname, char *parent, int parentsize) 401 { 402 char *cp; 403 404 /* 405 * Remove the @bla or /bla from the end of the name to get the parent. 406 */ 407 (void) strncpy(parent, datasetname, parentsize); 408 cp = strrchr(parent, '@'); 409 if (cp != NULL) { 410 cp[0] = '\0'; 411 } else { 412 cp = strrchr(parent, '/'); 413 if (cp == NULL) 414 return (ENOENT); 415 cp[0] = '\0'; 416 } 417 418 return (0); 419 } 420 421 int 422 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr) 423 { 424 int error; 425 426 if ((error = zfs_secpolicy_write_perms(name, 427 ZFS_DELEG_PERM_MOUNT, cr)) != 0) 428 return (error); 429 430 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr)); 431 } 432 433 static int 434 zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr) 435 { 436 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr)); 437 } 438 439 /* 440 * Must have sys_config privilege to check the iscsi permission 441 */ 442 /* ARGSUSED */ 443 static int 444 zfs_secpolicy_iscsi(zfs_cmd_t *zc, cred_t *cr) 445 { 446 return (secpolicy_zfs(cr)); 447 } 448 449 int 450 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr) 451 { 452 char parentname[MAXNAMELEN]; 453 int error; 454 455 if ((error = zfs_secpolicy_write_perms(from, 456 ZFS_DELEG_PERM_RENAME, cr)) != 0) 457 return (error); 458 459 if ((error = zfs_secpolicy_write_perms(from, 460 ZFS_DELEG_PERM_MOUNT, cr)) != 0) 461 return (error); 462 463 if ((error = zfs_get_parent(to, parentname, 464 sizeof (parentname))) != 0) 465 return (error); 466 467 if ((error = zfs_secpolicy_write_perms(parentname, 468 ZFS_DELEG_PERM_CREATE, cr)) != 0) 469 return (error); 470 471 if ((error = zfs_secpolicy_write_perms(parentname, 472 ZFS_DELEG_PERM_MOUNT, cr)) != 0) 473 return (error); 474 475 return (error); 476 } 477 478 static int 479 zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr) 480 { 481 return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr)); 482 } 483 484 static int 485 zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr) 486 { 487 char parentname[MAXNAMELEN]; 488 objset_t *clone; 489 int error; 490 491 error = zfs_secpolicy_write_perms(zc->zc_name, 492 ZFS_DELEG_PERM_PROMOTE, cr); 493 if (error) 494 return (error); 495 496 error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, 497 DS_MODE_STANDARD | DS_MODE_READONLY, &clone); 498 499 if (error == 0) { 500 dsl_dataset_t *pclone = NULL; 501 dsl_dir_t *dd; 502 dd = clone->os->os_dsl_dataset->ds_dir; 503 504 rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER); 505 error = dsl_dataset_open_obj(dd->dd_pool, 506 dd->dd_phys->dd_origin_obj, NULL, 507 DS_MODE_NONE, FTAG, &pclone); 508 rw_exit(&dd->dd_pool->dp_config_rwlock); 509 if (error) { 510 dmu_objset_close(clone); 511 return (error); 512 } 513 514 error = zfs_secpolicy_write_perms(zc->zc_name, 515 ZFS_DELEG_PERM_MOUNT, cr); 516 517 dsl_dataset_name(pclone, parentname); 518 dmu_objset_close(clone); 519 dsl_dataset_close(pclone, DS_MODE_NONE, FTAG); 520 if (error == 0) 521 error = zfs_secpolicy_write_perms(parentname, 522 ZFS_DELEG_PERM_PROMOTE, cr); 523 } 524 return (error); 525 } 526 527 static int 528 zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr) 529 { 530 int error; 531 532 if ((error = zfs_secpolicy_write_perms(zc->zc_name, 533 ZFS_DELEG_PERM_RECEIVE, cr)) != 0) 534 return (error); 535 536 if ((error = zfs_secpolicy_write_perms(zc->zc_name, 537 ZFS_DELEG_PERM_MOUNT, cr)) != 0) 538 return (error); 539 540 return (zfs_secpolicy_write_perms(zc->zc_name, 541 ZFS_DELEG_PERM_CREATE, cr)); 542 } 543 544 int 545 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr) 546 { 547 int error; 548 549 if ((error = zfs_secpolicy_write_perms(name, 550 ZFS_DELEG_PERM_SNAPSHOT, cr)) != 0) 551 return (error); 552 553 error = zfs_secpolicy_write_perms(name, 554 ZFS_DELEG_PERM_MOUNT, cr); 555 556 return (error); 557 } 558 559 static int 560 zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr) 561 { 562 563 return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr)); 564 } 565 566 static int 567 zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr) 568 { 569 char parentname[MAXNAMELEN]; 570 int error; 571 572 if ((error = zfs_get_parent(zc->zc_name, parentname, 573 sizeof (parentname))) != 0) 574 return (error); 575 576 if (zc->zc_value[0] != '\0') { 577 if ((error = zfs_secpolicy_write_perms(zc->zc_value, 578 ZFS_DELEG_PERM_CLONE, cr)) != 0) 579 return (error); 580 } 581 582 if ((error = zfs_secpolicy_write_perms(parentname, 583 ZFS_DELEG_PERM_CREATE, cr)) != 0) 584 return (error); 585 586 error = zfs_secpolicy_write_perms(parentname, 587 ZFS_DELEG_PERM_MOUNT, cr); 588 589 return (error); 590 } 591 592 static int 593 zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr) 594 { 595 int error; 596 597 error = secpolicy_fs_unmount(cr, NULL); 598 if (error) { 599 error = dsl_deleg_access(zc->zc_name, ZFS_DELEG_PERM_MOUNT, cr); 600 } 601 return (error); 602 } 603 604 /* 605 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires 606 * SYS_CONFIG privilege, which is not available in a local zone. 607 */ 608 /* ARGSUSED */ 609 static int 610 zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr) 611 { 612 if (secpolicy_sys_config(cr, B_FALSE) != 0) 613 return (EPERM); 614 615 return (0); 616 } 617 618 /* 619 * Just like zfs_secpolicy_config, except that we will check for 620 * mount permission on the dataset for permission to create/remove 621 * the minor nodes. 622 */ 623 static int 624 zfs_secpolicy_minor(zfs_cmd_t *zc, cred_t *cr) 625 { 626 if (secpolicy_sys_config(cr, B_FALSE) != 0) { 627 return (dsl_deleg_access(zc->zc_name, 628 ZFS_DELEG_PERM_MOUNT, cr)); 629 } 630 631 return (0); 632 } 633 634 /* 635 * Policy for fault injection. Requires all privileges. 636 */ 637 /* ARGSUSED */ 638 static int 639 zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr) 640 { 641 return (secpolicy_zinject(cr)); 642 } 643 644 static int 645 zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr) 646 { 647 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value); 648 649 if (prop == ZPROP_INVAL) { 650 if (!zfs_prop_user(zc->zc_value)) 651 return (EINVAL); 652 return (zfs_secpolicy_write_perms(zc->zc_name, 653 ZFS_DELEG_PERM_USERPROP, cr)); 654 } else { 655 if (!zfs_prop_inheritable(prop)) 656 return (EINVAL); 657 return (zfs_secpolicy_setprop(zc->zc_name, prop, cr)); 658 } 659 } 660 661 /* 662 * Returns the nvlist as specified by the user in the zfs_cmd_t. 663 */ 664 static int 665 get_nvlist(uint64_t nvl, uint64_t size, nvlist_t **nvp) 666 { 667 char *packed; 668 int error; 669 nvlist_t *list = NULL; 670 671 /* 672 * Read in and unpack the user-supplied nvlist. 673 */ 674 if (size == 0) 675 return (EINVAL); 676 677 packed = kmem_alloc(size, KM_SLEEP); 678 679 if ((error = xcopyin((void *)(uintptr_t)nvl, packed, size)) != 0) { 680 kmem_free(packed, size); 681 return (error); 682 } 683 684 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) { 685 kmem_free(packed, size); 686 return (error); 687 } 688 689 kmem_free(packed, size); 690 691 *nvp = list; 692 return (0); 693 } 694 695 static int 696 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl) 697 { 698 char *packed = NULL; 699 size_t size; 700 int error; 701 702 VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0); 703 704 if (size > zc->zc_nvlist_dst_size) { 705 error = ENOMEM; 706 } else { 707 packed = kmem_alloc(size, KM_SLEEP); 708 VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE, 709 KM_SLEEP) == 0); 710 error = xcopyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst, 711 size); 712 kmem_free(packed, size); 713 } 714 715 zc->zc_nvlist_dst_size = size; 716 return (error); 717 } 718 719 static int 720 zfs_ioc_pool_create(zfs_cmd_t *zc) 721 { 722 int error; 723 nvlist_t *config, *props = NULL; 724 char *buf; 725 726 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 727 &config)) 728 return (error); 729 730 if (zc->zc_nvlist_src_size != 0 && (error = 731 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, &props))) { 732 nvlist_free(config); 733 return (error); 734 } 735 736 buf = history_str_get(zc); 737 738 error = spa_create(zc->zc_name, config, props, buf); 739 740 if (buf != NULL) 741 history_str_free(buf); 742 743 nvlist_free(config); 744 745 if (props) 746 nvlist_free(props); 747 748 return (error); 749 } 750 751 static int 752 zfs_ioc_pool_destroy(zfs_cmd_t *zc) 753 { 754 int error; 755 zfs_log_history(zc); 756 error = spa_destroy(zc->zc_name); 757 return (error); 758 } 759 760 static int 761 zfs_ioc_pool_import(zfs_cmd_t *zc) 762 { 763 int error; 764 nvlist_t *config, *props = NULL; 765 uint64_t guid; 766 767 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 768 &config)) != 0) 769 return (error); 770 771 if (zc->zc_nvlist_src_size != 0 && (error = 772 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, &props))) { 773 nvlist_free(config); 774 return (error); 775 } 776 777 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 || 778 guid != zc->zc_guid) 779 error = EINVAL; 780 else 781 error = spa_import(zc->zc_name, config, props); 782 783 nvlist_free(config); 784 785 if (props) 786 nvlist_free(props); 787 788 return (error); 789 } 790 791 static int 792 zfs_ioc_pool_export(zfs_cmd_t *zc) 793 { 794 int error; 795 zfs_log_history(zc); 796 error = spa_export(zc->zc_name, NULL); 797 return (error); 798 } 799 800 static int 801 zfs_ioc_pool_configs(zfs_cmd_t *zc) 802 { 803 nvlist_t *configs; 804 int error; 805 806 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL) 807 return (EEXIST); 808 809 error = put_nvlist(zc, configs); 810 811 nvlist_free(configs); 812 813 return (error); 814 } 815 816 static int 817 zfs_ioc_pool_stats(zfs_cmd_t *zc) 818 { 819 nvlist_t *config; 820 int error; 821 int ret = 0; 822 823 error = spa_get_stats(zc->zc_name, &config, zc->zc_value, 824 sizeof (zc->zc_value)); 825 826 if (config != NULL) { 827 ret = put_nvlist(zc, config); 828 nvlist_free(config); 829 830 /* 831 * The config may be present even if 'error' is non-zero. 832 * In this case we return success, and preserve the real errno 833 * in 'zc_cookie'. 834 */ 835 zc->zc_cookie = error; 836 } else { 837 ret = error; 838 } 839 840 return (ret); 841 } 842 843 /* 844 * Try to import the given pool, returning pool stats as appropriate so that 845 * user land knows which devices are available and overall pool health. 846 */ 847 static int 848 zfs_ioc_pool_tryimport(zfs_cmd_t *zc) 849 { 850 nvlist_t *tryconfig, *config; 851 int error; 852 853 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 854 &tryconfig)) != 0) 855 return (error); 856 857 config = spa_tryimport(tryconfig); 858 859 nvlist_free(tryconfig); 860 861 if (config == NULL) 862 return (EINVAL); 863 864 error = put_nvlist(zc, config); 865 nvlist_free(config); 866 867 return (error); 868 } 869 870 static int 871 zfs_ioc_pool_scrub(zfs_cmd_t *zc) 872 { 873 spa_t *spa; 874 int error; 875 876 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 877 return (error); 878 879 mutex_enter(&spa_namespace_lock); 880 error = spa_scrub(spa, zc->zc_cookie, B_FALSE); 881 mutex_exit(&spa_namespace_lock); 882 883 spa_close(spa, FTAG); 884 885 return (error); 886 } 887 888 static int 889 zfs_ioc_pool_freeze(zfs_cmd_t *zc) 890 { 891 spa_t *spa; 892 int error; 893 894 error = spa_open(zc->zc_name, &spa, FTAG); 895 if (error == 0) { 896 spa_freeze(spa); 897 spa_close(spa, FTAG); 898 } 899 return (error); 900 } 901 902 static int 903 zfs_ioc_pool_upgrade(zfs_cmd_t *zc) 904 { 905 spa_t *spa; 906 int error; 907 908 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 909 return (error); 910 911 if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) { 912 spa_close(spa, FTAG); 913 return (EINVAL); 914 } 915 916 spa_upgrade(spa, zc->zc_cookie); 917 spa_close(spa, FTAG); 918 919 return (error); 920 } 921 922 static int 923 zfs_ioc_pool_get_history(zfs_cmd_t *zc) 924 { 925 spa_t *spa; 926 char *hist_buf; 927 uint64_t size; 928 int error; 929 930 if ((size = zc->zc_history_len) == 0) 931 return (EINVAL); 932 933 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 934 return (error); 935 936 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) { 937 spa_close(spa, FTAG); 938 return (ENOTSUP); 939 } 940 941 hist_buf = kmem_alloc(size, KM_SLEEP); 942 if ((error = spa_history_get(spa, &zc->zc_history_offset, 943 &zc->zc_history_len, hist_buf)) == 0) { 944 error = xcopyout(hist_buf, 945 (char *)(uintptr_t)zc->zc_history, 946 zc->zc_history_len); 947 } 948 949 spa_close(spa, FTAG); 950 kmem_free(hist_buf, size); 951 return (error); 952 } 953 954 static int 955 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc) 956 { 957 int error; 958 959 if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value)) 960 return (error); 961 962 return (0); 963 } 964 965 static int 966 zfs_ioc_obj_to_path(zfs_cmd_t *zc) 967 { 968 objset_t *osp; 969 int error; 970 971 if ((error = dmu_objset_open(zc->zc_name, DMU_OST_ZFS, 972 DS_MODE_NONE | DS_MODE_READONLY, &osp)) != 0) 973 return (error); 974 975 error = zfs_obj_to_path(osp, zc->zc_obj, zc->zc_value, 976 sizeof (zc->zc_value)); 977 dmu_objset_close(osp); 978 979 return (error); 980 } 981 982 static int 983 zfs_ioc_vdev_add(zfs_cmd_t *zc) 984 { 985 spa_t *spa; 986 int error; 987 nvlist_t *config, **l2cache; 988 uint_t nl2cache; 989 990 error = spa_open(zc->zc_name, &spa, FTAG); 991 if (error != 0) 992 return (error); 993 994 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 995 &config); 996 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE, 997 &l2cache, &nl2cache); 998 999 /* 1000 * A root pool with concatenated devices is not supported. 1001 * Thus, can not add a device to a root pool with one device. 1002 * Allow for l2cache devices to be added. 1003 */ 1004 if (spa->spa_root_vdev->vdev_children == 1 && spa->spa_bootfs != 0 && 1005 nl2cache == 0) { 1006 spa_close(spa, FTAG); 1007 return (EDOM); 1008 } 1009 1010 if (error == 0) { 1011 error = spa_vdev_add(spa, config); 1012 nvlist_free(config); 1013 } 1014 spa_close(spa, FTAG); 1015 return (error); 1016 } 1017 1018 static int 1019 zfs_ioc_vdev_remove(zfs_cmd_t *zc) 1020 { 1021 spa_t *spa; 1022 int error; 1023 1024 error = spa_open(zc->zc_name, &spa, FTAG); 1025 if (error != 0) 1026 return (error); 1027 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE); 1028 spa_close(spa, FTAG); 1029 return (error); 1030 } 1031 1032 static int 1033 zfs_ioc_vdev_set_state(zfs_cmd_t *zc) 1034 { 1035 spa_t *spa; 1036 int error; 1037 vdev_state_t newstate = VDEV_STATE_UNKNOWN; 1038 1039 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1040 return (error); 1041 switch (zc->zc_cookie) { 1042 case VDEV_STATE_ONLINE: 1043 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate); 1044 break; 1045 1046 case VDEV_STATE_OFFLINE: 1047 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj); 1048 break; 1049 1050 case VDEV_STATE_FAULTED: 1051 error = vdev_fault(spa, zc->zc_guid); 1052 break; 1053 1054 case VDEV_STATE_DEGRADED: 1055 error = vdev_degrade(spa, zc->zc_guid); 1056 break; 1057 1058 default: 1059 error = EINVAL; 1060 } 1061 zc->zc_cookie = newstate; 1062 spa_close(spa, FTAG); 1063 return (error); 1064 } 1065 1066 static int 1067 zfs_ioc_vdev_attach(zfs_cmd_t *zc) 1068 { 1069 spa_t *spa; 1070 int replacing = zc->zc_cookie; 1071 nvlist_t *config; 1072 int error; 1073 1074 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1075 return (error); 1076 1077 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 1078 &config)) == 0) { 1079 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing); 1080 nvlist_free(config); 1081 } 1082 1083 spa_close(spa, FTAG); 1084 return (error); 1085 } 1086 1087 static int 1088 zfs_ioc_vdev_detach(zfs_cmd_t *zc) 1089 { 1090 spa_t *spa; 1091 int error; 1092 1093 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1094 return (error); 1095 1096 error = spa_vdev_detach(spa, zc->zc_guid, B_FALSE); 1097 1098 spa_close(spa, FTAG); 1099 return (error); 1100 } 1101 1102 static int 1103 zfs_ioc_vdev_setpath(zfs_cmd_t *zc) 1104 { 1105 spa_t *spa; 1106 char *path = zc->zc_value; 1107 uint64_t guid = zc->zc_guid; 1108 int error; 1109 1110 error = spa_open(zc->zc_name, &spa, FTAG); 1111 if (error != 0) 1112 return (error); 1113 1114 error = spa_vdev_setpath(spa, guid, path); 1115 spa_close(spa, FTAG); 1116 return (error); 1117 } 1118 1119 static int 1120 zfs_os_open_retry(char *name, objset_t **os) 1121 { 1122 int error; 1123 1124 retry: 1125 error = dmu_objset_open(name, DMU_OST_ANY, 1126 DS_MODE_STANDARD | DS_MODE_READONLY, os); 1127 if (error != 0) { 1128 /* 1129 * This is ugly: dmu_objset_open() can return EBUSY if 1130 * the objset is held exclusively. Fortunately this hold is 1131 * only for a short while, so we retry here. 1132 * This avoids user code having to handle EBUSY, 1133 * for example for a "zfs list". 1134 */ 1135 if (error == EBUSY) { 1136 delay(1); 1137 goto retry; 1138 } 1139 } 1140 return (error); 1141 } 1142 1143 /* 1144 * inputs: 1145 * zc_name name of filesystem 1146 * zc_nvlist_dst_size size of buffer for property nvlist 1147 * 1148 * outputs: 1149 * zc_objset_stats stats 1150 * zc_nvlist_dst property nvlist 1151 * zc_nvlist_dst_size size of property nvlist 1152 * zc_value alternate root 1153 */ 1154 static int 1155 zfs_ioc_objset_stats(zfs_cmd_t *zc) 1156 { 1157 objset_t *os = NULL; 1158 int error; 1159 nvlist_t *nv; 1160 1161 if ((error = zfs_os_open_retry(zc->zc_name, &os)) != 0) 1162 return (error); 1163 1164 dmu_objset_fast_stat(os, &zc->zc_objset_stats); 1165 1166 if (zc->zc_nvlist_dst != 0 && 1167 (error = dsl_prop_get_all(os, &nv)) == 0) { 1168 dmu_objset_stats(os, nv); 1169 /* 1170 * NB: zvol_get_stats() will read the objset contents, 1171 * which we aren't supposed to do with a 1172 * DS_MODE_STANDARD open, because it could be 1173 * inconsistent. So this is a bit of a workaround... 1174 */ 1175 if (!zc->zc_objset_stats.dds_inconsistent) { 1176 if (dmu_objset_type(os) == DMU_OST_ZVOL) 1177 VERIFY(zvol_get_stats(os, nv) == 0); 1178 } 1179 error = put_nvlist(zc, nv); 1180 nvlist_free(nv); 1181 } 1182 1183 spa_altroot(dmu_objset_spa(os), zc->zc_value, sizeof (zc->zc_value)); 1184 1185 dmu_objset_close(os); 1186 return (error); 1187 } 1188 1189 static int 1190 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop) 1191 { 1192 uint64_t value; 1193 int error; 1194 1195 /* 1196 * zfs_get_zplprop() will either find a value or give us 1197 * the default value (if there is one). 1198 */ 1199 if ((error = zfs_get_zplprop(os, prop, &value)) != 0) 1200 return (error); 1201 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0); 1202 return (0); 1203 } 1204 1205 /* 1206 * inputs: 1207 * zc_name name of filesystem 1208 * zc_nvlist_dst_size size of buffer for zpl property nvlist 1209 * 1210 * outputs: 1211 * zc_nvlist_dst zpl property nvlist 1212 * zc_nvlist_dst_size size of zpl property nvlist 1213 */ 1214 static int 1215 zfs_ioc_objset_zplprops(zfs_cmd_t *zc) 1216 { 1217 objset_t *os; 1218 int err; 1219 1220 if ((err = zfs_os_open_retry(zc->zc_name, &os)) != 0) 1221 return (err); 1222 1223 dmu_objset_fast_stat(os, &zc->zc_objset_stats); 1224 1225 /* 1226 * NB: nvl_add_zplprop() will read the objset contents, 1227 * which we aren't supposed to do with a DS_MODE_STANDARD 1228 * open, because it could be inconsistent. 1229 */ 1230 if (zc->zc_nvlist_dst != NULL && 1231 !zc->zc_objset_stats.dds_inconsistent && 1232 dmu_objset_type(os) == DMU_OST_ZFS) { 1233 nvlist_t *nv; 1234 1235 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1236 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 && 1237 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 && 1238 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 && 1239 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0) 1240 err = put_nvlist(zc, nv); 1241 nvlist_free(nv); 1242 } else { 1243 err = ENOENT; 1244 } 1245 dmu_objset_close(os); 1246 return (err); 1247 } 1248 1249 /* 1250 * inputs: 1251 * zc_name name of filesystem 1252 * zc_cookie zap cursor 1253 * zc_nvlist_dst_size size of buffer for property nvlist 1254 * 1255 * outputs: 1256 * zc_name name of next filesystem 1257 * zc_objset_stats stats 1258 * zc_nvlist_dst property nvlist 1259 * zc_nvlist_dst_size size of property nvlist 1260 * zc_value alternate root 1261 */ 1262 static int 1263 zfs_ioc_dataset_list_next(zfs_cmd_t *zc) 1264 { 1265 objset_t *os; 1266 int error; 1267 char *p; 1268 1269 if ((error = zfs_os_open_retry(zc->zc_name, &os)) != 0) { 1270 if (error == ENOENT) 1271 error = ESRCH; 1272 return (error); 1273 } 1274 1275 p = strrchr(zc->zc_name, '/'); 1276 if (p == NULL || p[1] != '\0') 1277 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name)); 1278 p = zc->zc_name + strlen(zc->zc_name); 1279 1280 do { 1281 error = dmu_dir_list_next(os, 1282 sizeof (zc->zc_name) - (p - zc->zc_name), p, 1283 NULL, &zc->zc_cookie); 1284 if (error == ENOENT) 1285 error = ESRCH; 1286 } while (error == 0 && !INGLOBALZONE(curproc) && 1287 !zone_dataset_visible(zc->zc_name, NULL)); 1288 1289 /* 1290 * If it's a hidden dataset (ie. with a '$' in its name), don't 1291 * try to get stats for it. Userland will skip over it. 1292 */ 1293 if (error == 0 && strchr(zc->zc_name, '$') == NULL) 1294 error = zfs_ioc_objset_stats(zc); /* fill in the stats */ 1295 1296 dmu_objset_close(os); 1297 return (error); 1298 } 1299 1300 /* 1301 * inputs: 1302 * zc_name name of filesystem 1303 * zc_cookie zap cursor 1304 * zc_nvlist_dst_size size of buffer for property nvlist 1305 * 1306 * outputs: 1307 * zc_name name of next snapshot 1308 * zc_objset_stats stats 1309 * zc_nvlist_dst property nvlist 1310 * zc_nvlist_dst_size size of property nvlist 1311 * zc_value alternate root 1312 */ 1313 static int 1314 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc) 1315 { 1316 objset_t *os; 1317 int error; 1318 1319 if ((error = zfs_os_open_retry(zc->zc_name, &os)) != 0) { 1320 if (error == ENOENT) 1321 error = ESRCH; 1322 return (error); 1323 } 1324 1325 /* 1326 * A dataset name of maximum length cannot have any snapshots, 1327 * so exit immediately. 1328 */ 1329 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) { 1330 dmu_objset_close(os); 1331 return (ESRCH); 1332 } 1333 1334 error = dmu_snapshot_list_next(os, 1335 sizeof (zc->zc_name) - strlen(zc->zc_name), 1336 zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie, NULL); 1337 if (error == ENOENT) 1338 error = ESRCH; 1339 1340 if (error == 0) 1341 error = zfs_ioc_objset_stats(zc); /* fill in the stats */ 1342 1343 /* if we failed, undo the @ that we tacked on to zc_name */ 1344 if (error != 0) 1345 *strchr(zc->zc_name, '@') = '\0'; 1346 1347 dmu_objset_close(os); 1348 return (error); 1349 } 1350 1351 static int 1352 zfs_set_prop_nvlist(const char *name, nvlist_t *nvl) 1353 { 1354 nvpair_t *elem; 1355 int error; 1356 uint64_t intval; 1357 char *strval; 1358 1359 /* 1360 * First validate permission to set all of the properties 1361 */ 1362 elem = NULL; 1363 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 1364 const char *propname = nvpair_name(elem); 1365 zfs_prop_t prop = zfs_name_to_prop(propname); 1366 1367 if (prop == ZPROP_INVAL) { 1368 /* 1369 * If this is a user-defined property, it must be a 1370 * string, and there is no further validation to do. 1371 */ 1372 if (!zfs_prop_user(propname) || 1373 nvpair_type(elem) != DATA_TYPE_STRING) 1374 return (EINVAL); 1375 1376 if (error = zfs_secpolicy_write_perms(name, 1377 ZFS_DELEG_PERM_USERPROP, CRED())) 1378 return (error); 1379 continue; 1380 } 1381 1382 if ((error = zfs_secpolicy_setprop(name, prop, CRED())) != 0) 1383 return (error); 1384 1385 /* 1386 * Check that this value is valid for this pool version 1387 */ 1388 switch (prop) { 1389 case ZFS_PROP_COMPRESSION: 1390 /* 1391 * If the user specified gzip compression, make sure 1392 * the SPA supports it. We ignore any errors here since 1393 * we'll catch them later. 1394 */ 1395 if (nvpair_type(elem) == DATA_TYPE_UINT64 && 1396 nvpair_value_uint64(elem, &intval) == 0 && 1397 intval >= ZIO_COMPRESS_GZIP_1 && 1398 intval <= ZIO_COMPRESS_GZIP_9) { 1399 if (zfs_check_version(name, 1400 SPA_VERSION_GZIP_COMPRESSION)) 1401 return (ENOTSUP); 1402 } 1403 break; 1404 1405 case ZFS_PROP_COPIES: 1406 if (zfs_check_version(name, SPA_VERSION_DITTO_BLOCKS)) 1407 return (ENOTSUP); 1408 break; 1409 1410 case ZFS_PROP_SHARESMB: 1411 if (zpl_check_version(name, ZPL_VERSION_FUID)) 1412 return (ENOTSUP); 1413 break; 1414 } 1415 if ((error = zfs_secpolicy_setprop(name, prop, CRED())) != 0) 1416 return (error); 1417 } 1418 1419 elem = NULL; 1420 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 1421 const char *propname = nvpair_name(elem); 1422 zfs_prop_t prop = zfs_name_to_prop(propname); 1423 1424 if (prop == ZPROP_INVAL) { 1425 VERIFY(nvpair_value_string(elem, &strval) == 0); 1426 error = dsl_prop_set(name, propname, 1, 1427 strlen(strval) + 1, strval); 1428 if (error == 0) 1429 continue; 1430 else 1431 return (error); 1432 } 1433 1434 switch (prop) { 1435 case ZFS_PROP_QUOTA: 1436 if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 1437 (error = dsl_dir_set_quota(name, intval)) != 0) 1438 return (error); 1439 break; 1440 1441 case ZFS_PROP_REFQUOTA: 1442 if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 1443 (error = dsl_dataset_set_quota(name, intval)) != 0) 1444 return (error); 1445 break; 1446 1447 case ZFS_PROP_RESERVATION: 1448 if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 1449 (error = dsl_dir_set_reservation(name, 1450 intval)) != 0) 1451 return (error); 1452 break; 1453 1454 case ZFS_PROP_REFRESERVATION: 1455 if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 1456 (error = dsl_dataset_set_reservation(name, 1457 intval)) != 0) 1458 return (error); 1459 break; 1460 1461 case ZFS_PROP_VOLSIZE: 1462 if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 1463 (error = zvol_set_volsize(name, 1464 ddi_driver_major(zfs_dip), intval)) != 0) 1465 return (error); 1466 break; 1467 1468 case ZFS_PROP_VOLBLOCKSIZE: 1469 if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 1470 (error = zvol_set_volblocksize(name, intval)) != 0) 1471 return (error); 1472 break; 1473 1474 case ZFS_PROP_VERSION: 1475 if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 1476 (error = zfs_set_version(name, intval)) != 0) 1477 return (error); 1478 break; 1479 1480 default: 1481 if (nvpair_type(elem) == DATA_TYPE_STRING) { 1482 if (zfs_prop_get_type(prop) != 1483 PROP_TYPE_STRING) 1484 return (EINVAL); 1485 VERIFY(nvpair_value_string(elem, &strval) == 0); 1486 if ((error = dsl_prop_set(name, 1487 nvpair_name(elem), 1, strlen(strval) + 1, 1488 strval)) != 0) 1489 return (error); 1490 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 1491 const char *unused; 1492 1493 VERIFY(nvpair_value_uint64(elem, &intval) == 0); 1494 1495 switch (zfs_prop_get_type(prop)) { 1496 case PROP_TYPE_NUMBER: 1497 break; 1498 case PROP_TYPE_STRING: 1499 return (EINVAL); 1500 case PROP_TYPE_INDEX: 1501 if (zfs_prop_index_to_string(prop, 1502 intval, &unused) != 0) 1503 return (EINVAL); 1504 break; 1505 default: 1506 cmn_err(CE_PANIC, 1507 "unknown property type"); 1508 break; 1509 } 1510 1511 if ((error = dsl_prop_set(name, propname, 1512 8, 1, &intval)) != 0) 1513 return (error); 1514 } else { 1515 return (EINVAL); 1516 } 1517 break; 1518 } 1519 } 1520 1521 return (0); 1522 } 1523 1524 /* 1525 * inputs: 1526 * zc_name name of filesystem 1527 * zc_value name of property to inherit 1528 * zc_nvlist_src{_size} nvlist of properties to apply 1529 * 1530 * outputs: none 1531 */ 1532 static int 1533 zfs_ioc_set_prop(zfs_cmd_t *zc) 1534 { 1535 nvlist_t *nvl; 1536 int error; 1537 1538 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 1539 &nvl)) != 0) 1540 return (error); 1541 1542 error = zfs_set_prop_nvlist(zc->zc_name, nvl); 1543 1544 nvlist_free(nvl); 1545 return (error); 1546 } 1547 1548 /* 1549 * inputs: 1550 * zc_name name of filesystem 1551 * zc_value name of property to inherit 1552 * 1553 * outputs: none 1554 */ 1555 static int 1556 zfs_ioc_inherit_prop(zfs_cmd_t *zc) 1557 { 1558 /* the property name has been validated by zfs_secpolicy_inherit() */ 1559 return (dsl_prop_set(zc->zc_name, zc->zc_value, 0, 0, NULL)); 1560 } 1561 1562 static int 1563 zfs_ioc_pool_set_props(zfs_cmd_t *zc) 1564 { 1565 nvlist_t *props; 1566 spa_t *spa; 1567 int error; 1568 1569 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 1570 &props))) 1571 return (error); 1572 1573 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) { 1574 nvlist_free(props); 1575 return (error); 1576 } 1577 1578 error = spa_prop_set(spa, props); 1579 1580 nvlist_free(props); 1581 spa_close(spa, FTAG); 1582 1583 return (error); 1584 } 1585 1586 static int 1587 zfs_ioc_pool_get_props(zfs_cmd_t *zc) 1588 { 1589 spa_t *spa; 1590 int error; 1591 nvlist_t *nvp = NULL; 1592 1593 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1594 return (error); 1595 1596 error = spa_prop_get(spa, &nvp); 1597 1598 if (error == 0 && zc->zc_nvlist_dst != NULL) 1599 error = put_nvlist(zc, nvp); 1600 else 1601 error = EFAULT; 1602 1603 spa_close(spa, FTAG); 1604 1605 if (nvp) 1606 nvlist_free(nvp); 1607 return (error); 1608 } 1609 1610 static int 1611 zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc) 1612 { 1613 nvlist_t *nvp; 1614 int error; 1615 uint32_t uid; 1616 uint32_t gid; 1617 uint32_t *groups; 1618 uint_t group_cnt; 1619 cred_t *usercred; 1620 1621 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 1622 &nvp)) != 0) { 1623 return (error); 1624 } 1625 1626 if ((error = nvlist_lookup_uint32(nvp, 1627 ZFS_DELEG_PERM_UID, &uid)) != 0) { 1628 nvlist_free(nvp); 1629 return (EPERM); 1630 } 1631 1632 if ((error = nvlist_lookup_uint32(nvp, 1633 ZFS_DELEG_PERM_GID, &gid)) != 0) { 1634 nvlist_free(nvp); 1635 return (EPERM); 1636 } 1637 1638 if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS, 1639 &groups, &group_cnt)) != 0) { 1640 nvlist_free(nvp); 1641 return (EPERM); 1642 } 1643 usercred = cralloc(); 1644 if ((crsetugid(usercred, uid, gid) != 0) || 1645 (crsetgroups(usercred, group_cnt, (gid_t *)groups) != 0)) { 1646 nvlist_free(nvp); 1647 crfree(usercred); 1648 return (EPERM); 1649 } 1650 nvlist_free(nvp); 1651 error = dsl_deleg_access(zc->zc_name, 1652 zfs_prop_to_name(ZFS_PROP_SHAREISCSI), usercred); 1653 crfree(usercred); 1654 return (error); 1655 } 1656 1657 /* 1658 * inputs: 1659 * zc_name name of filesystem 1660 * zc_nvlist_src{_size} nvlist of delegated permissions 1661 * zc_perm_action allow/unallow flag 1662 * 1663 * outputs: none 1664 */ 1665 static int 1666 zfs_ioc_set_fsacl(zfs_cmd_t *zc) 1667 { 1668 int error; 1669 nvlist_t *fsaclnv = NULL; 1670 1671 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 1672 &fsaclnv)) != 0) 1673 return (error); 1674 1675 /* 1676 * Verify nvlist is constructed correctly 1677 */ 1678 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) { 1679 nvlist_free(fsaclnv); 1680 return (EINVAL); 1681 } 1682 1683 /* 1684 * If we don't have PRIV_SYS_MOUNT, then validate 1685 * that user is allowed to hand out each permission in 1686 * the nvlist(s) 1687 */ 1688 1689 error = secpolicy_zfs(CRED()); 1690 if (error) { 1691 if (zc->zc_perm_action == B_FALSE) { 1692 error = dsl_deleg_can_allow(zc->zc_name, 1693 fsaclnv, CRED()); 1694 } else { 1695 error = dsl_deleg_can_unallow(zc->zc_name, 1696 fsaclnv, CRED()); 1697 } 1698 } 1699 1700 if (error == 0) 1701 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action); 1702 1703 nvlist_free(fsaclnv); 1704 return (error); 1705 } 1706 1707 /* 1708 * inputs: 1709 * zc_name name of filesystem 1710 * 1711 * outputs: 1712 * zc_nvlist_src{_size} nvlist of delegated permissions 1713 */ 1714 static int 1715 zfs_ioc_get_fsacl(zfs_cmd_t *zc) 1716 { 1717 nvlist_t *nvp; 1718 int error; 1719 1720 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) { 1721 error = put_nvlist(zc, nvp); 1722 nvlist_free(nvp); 1723 } 1724 1725 return (error); 1726 } 1727 1728 /* 1729 * inputs: 1730 * zc_name name of volume 1731 * 1732 * outputs: none 1733 */ 1734 static int 1735 zfs_ioc_create_minor(zfs_cmd_t *zc) 1736 { 1737 return (zvol_create_minor(zc->zc_name, ddi_driver_major(zfs_dip))); 1738 } 1739 1740 /* 1741 * inputs: 1742 * zc_name name of volume 1743 * 1744 * outputs: none 1745 */ 1746 static int 1747 zfs_ioc_remove_minor(zfs_cmd_t *zc) 1748 { 1749 return (zvol_remove_minor(zc->zc_name)); 1750 } 1751 1752 /* 1753 * Search the vfs list for a specified resource. Returns a pointer to it 1754 * or NULL if no suitable entry is found. The caller of this routine 1755 * is responsible for releasing the returned vfs pointer. 1756 */ 1757 static vfs_t * 1758 zfs_get_vfs(const char *resource) 1759 { 1760 struct vfs *vfsp; 1761 struct vfs *vfs_found = NULL; 1762 1763 vfs_list_read_lock(); 1764 vfsp = rootvfs; 1765 do { 1766 if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) { 1767 VFS_HOLD(vfsp); 1768 vfs_found = vfsp; 1769 break; 1770 } 1771 vfsp = vfsp->vfs_next; 1772 } while (vfsp != rootvfs); 1773 vfs_list_unlock(); 1774 return (vfs_found); 1775 } 1776 1777 /* ARGSUSED */ 1778 static void 1779 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 1780 { 1781 zfs_creat_t *zct = arg; 1782 1783 zfs_create_fs(os, cr, zct->zct_zplprops, tx); 1784 } 1785 1786 #define ZFS_PROP_UNDEFINED ((uint64_t)-1) 1787 1788 /* 1789 * inputs: 1790 * createprops list of properties requested by creator 1791 * dataset name of dataset we are creating 1792 * 1793 * outputs: 1794 * zplprops values for the zplprops we attach to the master node object 1795 * 1796 * Determine the settings for utf8only, normalization and 1797 * casesensitivity. Specific values may have been requested by the 1798 * creator and/or we can inherit values from the parent dataset. If 1799 * the file system is of too early a vintage, a creator can not 1800 * request settings for these properties, even if the requested 1801 * setting is the default value. We don't actually want to create dsl 1802 * properties for these, so remove them from the source nvlist after 1803 * processing. 1804 */ 1805 static int 1806 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops, 1807 nvlist_t *zplprops, uint64_t zplver) 1808 { 1809 objset_t *os; 1810 char parentname[MAXNAMELEN]; 1811 char *cp; 1812 uint64_t sense = ZFS_PROP_UNDEFINED; 1813 uint64_t norm = ZFS_PROP_UNDEFINED; 1814 uint64_t u8 = ZFS_PROP_UNDEFINED; 1815 int error = 0; 1816 1817 ASSERT(zplprops != NULL); 1818 1819 (void) strlcpy(parentname, dataset, sizeof (parentname)); 1820 cp = strrchr(parentname, '/'); 1821 ASSERT(cp != NULL); 1822 cp[0] = '\0'; 1823 1824 /* 1825 * Pull out creator prop choices, if any. 1826 */ 1827 if (createprops) { 1828 (void) nvlist_lookup_uint64(createprops, 1829 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm); 1830 (void) nvlist_remove_all(createprops, 1831 zfs_prop_to_name(ZFS_PROP_NORMALIZE)); 1832 (void) nvlist_lookup_uint64(createprops, 1833 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8); 1834 (void) nvlist_remove_all(createprops, 1835 zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 1836 (void) nvlist_lookup_uint64(createprops, 1837 zfs_prop_to_name(ZFS_PROP_CASE), &sense); 1838 (void) nvlist_remove_all(createprops, 1839 zfs_prop_to_name(ZFS_PROP_CASE)); 1840 } 1841 1842 /* 1843 * If the file system or pool is version is too "young" to 1844 * support normalization and the creator tried to set a value 1845 * for one of the props, error out. We only need check the 1846 * ZPL version because we've already checked by now that the 1847 * SPA version is compatible with the selected ZPL version. 1848 */ 1849 if (zplver < ZPL_VERSION_NORMALIZATION && 1850 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED || 1851 sense != ZFS_PROP_UNDEFINED)) 1852 return (ENOTSUP); 1853 1854 /* 1855 * Put the version in the zplprops 1856 */ 1857 VERIFY(nvlist_add_uint64(zplprops, 1858 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0); 1859 1860 /* 1861 * Open parent object set so we can inherit zplprop values if 1862 * necessary. 1863 */ 1864 if ((error = zfs_os_open_retry(parentname, &os)) != 0) 1865 return (error); 1866 1867 if (norm == ZFS_PROP_UNDEFINED) 1868 VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0); 1869 VERIFY(nvlist_add_uint64(zplprops, 1870 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0); 1871 1872 /* 1873 * If we're normalizing, names must always be valid UTF-8 strings. 1874 */ 1875 if (norm) 1876 u8 = 1; 1877 if (u8 == ZFS_PROP_UNDEFINED) 1878 VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0); 1879 VERIFY(nvlist_add_uint64(zplprops, 1880 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0); 1881 1882 if (sense == ZFS_PROP_UNDEFINED) 1883 VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0); 1884 VERIFY(nvlist_add_uint64(zplprops, 1885 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0); 1886 1887 dmu_objset_close(os); 1888 return (0); 1889 } 1890 1891 /* 1892 * inputs: 1893 * zc_objset_type type of objset to create (fs vs zvol) 1894 * zc_name name of new objset 1895 * zc_value name of snapshot to clone from (may be empty) 1896 * zc_nvlist_src{_size} nvlist of properties to apply 1897 * 1898 * outputs: none 1899 */ 1900 static int 1901 zfs_ioc_create(zfs_cmd_t *zc) 1902 { 1903 objset_t *clone; 1904 int error = 0; 1905 zfs_creat_t zct; 1906 nvlist_t *nvprops = NULL; 1907 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx); 1908 dmu_objset_type_t type = zc->zc_objset_type; 1909 1910 switch (type) { 1911 1912 case DMU_OST_ZFS: 1913 cbfunc = zfs_create_cb; 1914 break; 1915 1916 case DMU_OST_ZVOL: 1917 cbfunc = zvol_create_cb; 1918 break; 1919 1920 default: 1921 cbfunc = NULL; 1922 } 1923 if (strchr(zc->zc_name, '@') || 1924 strchr(zc->zc_name, '%')) 1925 return (EINVAL); 1926 1927 if (zc->zc_nvlist_src != NULL && 1928 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 1929 &nvprops)) != 0) 1930 return (error); 1931 1932 zct.zct_zplprops = NULL; 1933 zct.zct_props = nvprops; 1934 1935 if (zc->zc_value[0] != '\0') { 1936 /* 1937 * We're creating a clone of an existing snapshot. 1938 */ 1939 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0'; 1940 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) { 1941 nvlist_free(nvprops); 1942 return (EINVAL); 1943 } 1944 1945 error = dmu_objset_open(zc->zc_value, type, 1946 DS_MODE_STANDARD | DS_MODE_READONLY, &clone); 1947 if (error) { 1948 nvlist_free(nvprops); 1949 return (error); 1950 } 1951 error = dmu_objset_create(zc->zc_name, type, clone, NULL, NULL); 1952 if (error) { 1953 dmu_objset_close(clone); 1954 nvlist_free(nvprops); 1955 return (error); 1956 } 1957 dmu_objset_close(clone); 1958 } else { 1959 if (cbfunc == NULL) { 1960 nvlist_free(nvprops); 1961 return (EINVAL); 1962 } 1963 1964 if (type == DMU_OST_ZVOL) { 1965 uint64_t volsize, volblocksize; 1966 1967 if (nvprops == NULL || 1968 nvlist_lookup_uint64(nvprops, 1969 zfs_prop_to_name(ZFS_PROP_VOLSIZE), 1970 &volsize) != 0) { 1971 nvlist_free(nvprops); 1972 return (EINVAL); 1973 } 1974 1975 if ((error = nvlist_lookup_uint64(nvprops, 1976 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 1977 &volblocksize)) != 0 && error != ENOENT) { 1978 nvlist_free(nvprops); 1979 return (EINVAL); 1980 } 1981 1982 if (error != 0) 1983 volblocksize = zfs_prop_default_numeric( 1984 ZFS_PROP_VOLBLOCKSIZE); 1985 1986 if ((error = zvol_check_volblocksize( 1987 volblocksize)) != 0 || 1988 (error = zvol_check_volsize(volsize, 1989 volblocksize)) != 0) { 1990 nvlist_free(nvprops); 1991 return (error); 1992 } 1993 } else if (type == DMU_OST_ZFS) { 1994 uint64_t version; 1995 int error; 1996 1997 /* 1998 * Default ZPL version to non-FUID capable if the 1999 * pool is not upgraded to support FUIDs. 2000 */ 2001 if (zfs_check_version(zc->zc_name, SPA_VERSION_FUID)) 2002 version = ZPL_VERSION_FUID - 1; 2003 else 2004 version = ZPL_VERSION; 2005 2006 /* 2007 * Potentially override default ZPL version based 2008 * on creator's request. 2009 */ 2010 (void) nvlist_lookup_uint64(nvprops, 2011 zfs_prop_to_name(ZFS_PROP_VERSION), &version); 2012 2013 /* 2014 * Make sure version we ended up with is kosher 2015 */ 2016 if ((version < ZPL_VERSION_INITIAL || 2017 version > ZPL_VERSION) || 2018 (version >= ZPL_VERSION_FUID && 2019 zfs_check_version(zc->zc_name, SPA_VERSION_FUID))) { 2020 nvlist_free(nvprops); 2021 return (ENOTSUP); 2022 } 2023 2024 /* 2025 * We have to have normalization and 2026 * case-folding flags correct when we do the 2027 * file system creation, so go figure them out 2028 * now. 2029 */ 2030 VERIFY(nvlist_alloc(&zct.zct_zplprops, 2031 NV_UNIQUE_NAME, KM_SLEEP) == 0); 2032 error = zfs_fill_zplprops(zc->zc_name, nvprops, 2033 zct.zct_zplprops, version); 2034 if (error != 0) { 2035 nvlist_free(nvprops); 2036 nvlist_free(zct.zct_zplprops); 2037 return (error); 2038 } 2039 } 2040 error = dmu_objset_create(zc->zc_name, type, NULL, cbfunc, 2041 &zct); 2042 nvlist_free(zct.zct_zplprops); 2043 } 2044 2045 /* 2046 * It would be nice to do this atomically. 2047 */ 2048 if (error == 0) { 2049 if ((error = zfs_set_prop_nvlist(zc->zc_name, nvprops)) != 0) 2050 (void) dmu_objset_destroy(zc->zc_name); 2051 } 2052 2053 nvlist_free(nvprops); 2054 return (error); 2055 } 2056 2057 /* 2058 * inputs: 2059 * zc_name name of filesystem 2060 * zc_value short name of snapshot 2061 * zc_cookie recursive flag 2062 * 2063 * outputs: none 2064 */ 2065 static int 2066 zfs_ioc_snapshot(zfs_cmd_t *zc) 2067 { 2068 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 2069 return (EINVAL); 2070 return (dmu_objset_snapshot(zc->zc_name, 2071 zc->zc_value, zc->zc_cookie)); 2072 } 2073 2074 int 2075 zfs_unmount_snap(char *name, void *arg) 2076 { 2077 char *snapname = arg; 2078 char *cp; 2079 vfs_t *vfsp = NULL; 2080 2081 /* 2082 * Snapshots (which are under .zfs control) must be unmounted 2083 * before they can be destroyed. 2084 */ 2085 2086 if (snapname) { 2087 (void) strcat(name, "@"); 2088 (void) strcat(name, snapname); 2089 vfsp = zfs_get_vfs(name); 2090 cp = strchr(name, '@'); 2091 *cp = '\0'; 2092 } else if (strchr(name, '@')) { 2093 vfsp = zfs_get_vfs(name); 2094 } 2095 2096 if (vfsp) { 2097 /* 2098 * Always force the unmount for snapshots. 2099 */ 2100 int flag = MS_FORCE; 2101 int err; 2102 2103 if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) { 2104 VFS_RELE(vfsp); 2105 return (err); 2106 } 2107 VFS_RELE(vfsp); 2108 if ((err = dounmount(vfsp, flag, kcred)) != 0) 2109 return (err); 2110 } 2111 return (0); 2112 } 2113 2114 /* 2115 * inputs: 2116 * zc_name name of filesystem 2117 * zc_value short name of snapshot 2118 * 2119 * outputs: none 2120 */ 2121 static int 2122 zfs_ioc_destroy_snaps(zfs_cmd_t *zc) 2123 { 2124 int err; 2125 2126 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 2127 return (EINVAL); 2128 err = dmu_objset_find(zc->zc_name, 2129 zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN); 2130 if (err) 2131 return (err); 2132 return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value)); 2133 } 2134 2135 /* 2136 * inputs: 2137 * zc_name name of dataset to destroy 2138 * zc_objset_type type of objset 2139 * 2140 * outputs: none 2141 */ 2142 static int 2143 zfs_ioc_destroy(zfs_cmd_t *zc) 2144 { 2145 if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) { 2146 int err = zfs_unmount_snap(zc->zc_name, NULL); 2147 if (err) 2148 return (err); 2149 } 2150 2151 return (dmu_objset_destroy(zc->zc_name)); 2152 } 2153 2154 /* 2155 * inputs: 2156 * zc_name name of dataset to rollback (to most recent snapshot) 2157 * 2158 * outputs: none 2159 */ 2160 static int 2161 zfs_ioc_rollback(zfs_cmd_t *zc) 2162 { 2163 objset_t *os; 2164 int error; 2165 zfsvfs_t *zfsvfs = NULL; 2166 2167 /* 2168 * Get the zfsvfs for the receiving objset. There 2169 * won't be one if we're operating on a zvol, if the 2170 * objset doesn't exist yet, or is not mounted. 2171 */ 2172 error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, 2173 DS_MODE_STANDARD, &os); 2174 if (error) 2175 return (error); 2176 2177 if (dmu_objset_type(os) == DMU_OST_ZFS) { 2178 mutex_enter(&os->os->os_user_ptr_lock); 2179 zfsvfs = dmu_objset_get_user(os); 2180 if (zfsvfs != NULL) 2181 VFS_HOLD(zfsvfs->z_vfs); 2182 mutex_exit(&os->os->os_user_ptr_lock); 2183 } 2184 2185 if (zfsvfs != NULL) { 2186 char osname[MAXNAMELEN]; 2187 int mode; 2188 2189 VERIFY3U(0, ==, zfs_suspend_fs(zfsvfs, osname, &mode)); 2190 ASSERT(strcmp(osname, zc->zc_name) == 0); 2191 error = dmu_objset_rollback(os); 2192 VERIFY3U(0, ==, zfs_resume_fs(zfsvfs, osname, mode)); 2193 2194 VFS_RELE(zfsvfs->z_vfs); 2195 } else { 2196 error = dmu_objset_rollback(os); 2197 } 2198 /* Note, the dmu_objset_rollback() closes the objset for us. */ 2199 2200 return (error); 2201 } 2202 2203 /* 2204 * inputs: 2205 * zc_name old name of dataset 2206 * zc_value new name of dataset 2207 * zc_cookie recursive flag (only valid for snapshots) 2208 * 2209 * outputs: none 2210 */ 2211 static int 2212 zfs_ioc_rename(zfs_cmd_t *zc) 2213 { 2214 boolean_t recursive = zc->zc_cookie & 1; 2215 2216 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0'; 2217 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 || 2218 strchr(zc->zc_value, '%')) 2219 return (EINVAL); 2220 2221 /* 2222 * Unmount snapshot unless we're doing a recursive rename, 2223 * in which case the dataset code figures out which snapshots 2224 * to unmount. 2225 */ 2226 if (!recursive && strchr(zc->zc_name, '@') != NULL && 2227 zc->zc_objset_type == DMU_OST_ZFS) { 2228 int err = zfs_unmount_snap(zc->zc_name, NULL); 2229 if (err) 2230 return (err); 2231 } 2232 2233 return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive)); 2234 } 2235 2236 /* 2237 * inputs: 2238 * zc_name name of containing filesystem 2239 * zc_nvlist_src{_size} nvlist of properties to apply 2240 * zc_value name of snapshot to create 2241 * zc_string name of clone origin (if DRR_FLAG_CLONE) 2242 * zc_cookie file descriptor to recv from 2243 * zc_begin_record the BEGIN record of the stream (not byteswapped) 2244 * zc_guid force flag 2245 * 2246 * outputs: 2247 * zc_cookie number of bytes read 2248 */ 2249 static int 2250 zfs_ioc_recv(zfs_cmd_t *zc) 2251 { 2252 file_t *fp; 2253 objset_t *os; 2254 dmu_recv_cookie_t drc; 2255 zfsvfs_t *zfsvfs = NULL; 2256 boolean_t force = (boolean_t)zc->zc_guid; 2257 int error, fd; 2258 offset_t off; 2259 nvlist_t *props = NULL; 2260 objset_t *origin = NULL; 2261 char *tosnap; 2262 char tofs[ZFS_MAXNAMELEN]; 2263 2264 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 || 2265 strchr(zc->zc_value, '@') == NULL || 2266 strchr(zc->zc_value, '%')) 2267 return (EINVAL); 2268 2269 (void) strcpy(tofs, zc->zc_value); 2270 tosnap = strchr(tofs, '@'); 2271 *tosnap = '\0'; 2272 tosnap++; 2273 2274 if (zc->zc_nvlist_src != NULL && 2275 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 2276 &props)) != 0) 2277 return (error); 2278 2279 fd = zc->zc_cookie; 2280 fp = getf(fd); 2281 if (fp == NULL) { 2282 nvlist_free(props); 2283 return (EBADF); 2284 } 2285 2286 /* 2287 * Get the zfsvfs for the receiving objset. There 2288 * won't be one if we're operating on a zvol, if the 2289 * objset doesn't exist yet, or is not mounted. 2290 */ 2291 2292 error = dmu_objset_open(tofs, DMU_OST_ZFS, 2293 DS_MODE_STANDARD | DS_MODE_READONLY, &os); 2294 if (!error) { 2295 mutex_enter(&os->os->os_user_ptr_lock); 2296 zfsvfs = dmu_objset_get_user(os); 2297 if (zfsvfs != NULL) 2298 VFS_HOLD(zfsvfs->z_vfs); 2299 mutex_exit(&os->os->os_user_ptr_lock); 2300 dmu_objset_close(os); 2301 } 2302 2303 if (zc->zc_string[0]) { 2304 error = dmu_objset_open(zc->zc_string, DMU_OST_ANY, 2305 DS_MODE_STANDARD | DS_MODE_READONLY, &origin); 2306 if (error) { 2307 if (zfsvfs != NULL) 2308 VFS_RELE(zfsvfs->z_vfs); 2309 nvlist_free(props); 2310 releasef(fd); 2311 return (error); 2312 } 2313 } 2314 2315 error = dmu_recv_begin(tofs, tosnap, &zc->zc_begin_record, 2316 force, origin, zfsvfs != NULL, &drc); 2317 if (origin) 2318 dmu_objset_close(origin); 2319 if (error) { 2320 if (zfsvfs != NULL) 2321 VFS_RELE(zfsvfs->z_vfs); 2322 nvlist_free(props); 2323 releasef(fd); 2324 return (error); 2325 } 2326 2327 /* 2328 * If properties are supplied, they are to completely replace 2329 * the existing ones; "inherit" any existing properties. 2330 */ 2331 if (props) { 2332 objset_t *os; 2333 nvlist_t *nv = NULL; 2334 2335 error = dmu_objset_open(tofs, DMU_OST_ANY, 2336 DS_MODE_STANDARD | DS_MODE_READONLY | DS_MODE_INCONSISTENT, 2337 &os); 2338 if (error == 0) { 2339 error = dsl_prop_get_all(os, &nv); 2340 dmu_objset_close(os); 2341 } 2342 if (error == 0) { 2343 nvpair_t *elem; 2344 zfs_cmd_t *zc2; 2345 zc2 = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP); 2346 2347 (void) strcpy(zc2->zc_name, tofs); 2348 for (elem = nvlist_next_nvpair(nv, NULL); elem; 2349 elem = nvlist_next_nvpair(nv, elem)) { 2350 (void) strcpy(zc2->zc_value, nvpair_name(elem)); 2351 if (zfs_secpolicy_inherit(zc2, CRED()) == 0) 2352 (void) zfs_ioc_inherit_prop(zc2); 2353 } 2354 kmem_free(zc2, sizeof (zfs_cmd_t)); 2355 } 2356 if (nv) 2357 nvlist_free(nv); 2358 } 2359 2360 /* 2361 * Set properties. Note, we ignore errors. Would be better to 2362 * do best-effort in zfs_set_prop_nvlist, too. 2363 */ 2364 (void) zfs_set_prop_nvlist(tofs, props); 2365 nvlist_free(props); 2366 2367 off = fp->f_offset; 2368 error = dmu_recv_stream(&drc, fp->f_vnode, &off); 2369 2370 if (error == 0) { 2371 if (zfsvfs != NULL) { 2372 char osname[MAXNAMELEN]; 2373 int mode; 2374 2375 (void) zfs_suspend_fs(zfsvfs, osname, &mode); 2376 error = dmu_recv_end(&drc); 2377 error |= zfs_resume_fs(zfsvfs, osname, mode); 2378 } else { 2379 error = dmu_recv_end(&drc); 2380 } 2381 } 2382 if (zfsvfs != NULL) 2383 VFS_RELE(zfsvfs->z_vfs); 2384 2385 zc->zc_cookie = off - fp->f_offset; 2386 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0) 2387 fp->f_offset = off; 2388 2389 releasef(fd); 2390 return (error); 2391 } 2392 2393 /* 2394 * inputs: 2395 * zc_name name of snapshot to send 2396 * zc_value short name of incremental fromsnap (may be empty) 2397 * zc_cookie file descriptor to send stream to 2398 * zc_obj fromorigin flag (mutually exclusive with zc_value) 2399 * 2400 * outputs: none 2401 */ 2402 static int 2403 zfs_ioc_send(zfs_cmd_t *zc) 2404 { 2405 objset_t *fromsnap = NULL; 2406 objset_t *tosnap; 2407 file_t *fp; 2408 int error; 2409 offset_t off; 2410 2411 error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, 2412 DS_MODE_STANDARD | DS_MODE_READONLY, &tosnap); 2413 if (error) 2414 return (error); 2415 2416 if (zc->zc_value[0] != '\0') { 2417 char buf[MAXPATHLEN]; 2418 char *cp; 2419 2420 (void) strncpy(buf, zc->zc_name, sizeof (buf)); 2421 cp = strchr(buf, '@'); 2422 if (cp) 2423 *(cp+1) = 0; 2424 (void) strncat(buf, zc->zc_value, sizeof (buf)); 2425 error = dmu_objset_open(buf, DMU_OST_ANY, 2426 DS_MODE_STANDARD | DS_MODE_READONLY, &fromsnap); 2427 if (error) { 2428 dmu_objset_close(tosnap); 2429 return (error); 2430 } 2431 } 2432 2433 fp = getf(zc->zc_cookie); 2434 if (fp == NULL) { 2435 dmu_objset_close(tosnap); 2436 if (fromsnap) 2437 dmu_objset_close(fromsnap); 2438 return (EBADF); 2439 } 2440 2441 off = fp->f_offset; 2442 error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off); 2443 2444 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0) 2445 fp->f_offset = off; 2446 releasef(zc->zc_cookie); 2447 if (fromsnap) 2448 dmu_objset_close(fromsnap); 2449 dmu_objset_close(tosnap); 2450 return (error); 2451 } 2452 2453 static int 2454 zfs_ioc_inject_fault(zfs_cmd_t *zc) 2455 { 2456 int id, error; 2457 2458 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id, 2459 &zc->zc_inject_record); 2460 2461 if (error == 0) 2462 zc->zc_guid = (uint64_t)id; 2463 2464 return (error); 2465 } 2466 2467 static int 2468 zfs_ioc_clear_fault(zfs_cmd_t *zc) 2469 { 2470 return (zio_clear_fault((int)zc->zc_guid)); 2471 } 2472 2473 static int 2474 zfs_ioc_inject_list_next(zfs_cmd_t *zc) 2475 { 2476 int id = (int)zc->zc_guid; 2477 int error; 2478 2479 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name), 2480 &zc->zc_inject_record); 2481 2482 zc->zc_guid = id; 2483 2484 return (error); 2485 } 2486 2487 static int 2488 zfs_ioc_error_log(zfs_cmd_t *zc) 2489 { 2490 spa_t *spa; 2491 int error; 2492 size_t count = (size_t)zc->zc_nvlist_dst_size; 2493 2494 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 2495 return (error); 2496 2497 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst, 2498 &count); 2499 if (error == 0) 2500 zc->zc_nvlist_dst_size = count; 2501 else 2502 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa); 2503 2504 spa_close(spa, FTAG); 2505 2506 return (error); 2507 } 2508 2509 static int 2510 zfs_ioc_clear(zfs_cmd_t *zc) 2511 { 2512 spa_t *spa; 2513 vdev_t *vd; 2514 uint64_t txg; 2515 int error; 2516 2517 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 2518 return (error); 2519 2520 /* 2521 * Try to resume any I/Os which may have been suspended 2522 * as a result of a complete pool failure. 2523 */ 2524 if (!list_is_empty(&spa->spa_zio_list)) { 2525 if (zio_vdev_resume_io(spa) != 0) { 2526 spa_close(spa, FTAG); 2527 return (EIO); 2528 } 2529 } 2530 2531 txg = spa_vdev_enter(spa); 2532 2533 if (zc->zc_guid == 0) { 2534 vd = NULL; 2535 } else if ((vd = spa_lookup_by_guid(spa, zc->zc_guid)) == NULL) { 2536 spa_aux_vdev_t *sav; 2537 int i; 2538 2539 /* 2540 * Check if this is an l2cache device. 2541 */ 2542 ASSERT(spa != NULL); 2543 sav = &spa->spa_l2cache; 2544 for (i = 0; i < sav->sav_count; i++) { 2545 if (sav->sav_vdevs[i]->vdev_guid == zc->zc_guid) { 2546 vd = sav->sav_vdevs[i]; 2547 break; 2548 } 2549 } 2550 2551 if (vd == NULL) { 2552 (void) spa_vdev_exit(spa, NULL, txg, ENODEV); 2553 spa_close(spa, FTAG); 2554 return (ENODEV); 2555 } 2556 } 2557 2558 vdev_clear(spa, vd, B_TRUE); 2559 2560 (void) spa_vdev_exit(spa, NULL, txg, 0); 2561 2562 spa_close(spa, FTAG); 2563 2564 return (0); 2565 } 2566 2567 /* 2568 * inputs: 2569 * zc_name name of filesystem 2570 * zc_value name of origin snapshot 2571 * 2572 * outputs: none 2573 */ 2574 static int 2575 zfs_ioc_promote(zfs_cmd_t *zc) 2576 { 2577 char *cp; 2578 2579 /* 2580 * We don't need to unmount *all* the origin fs's snapshots, but 2581 * it's easier. 2582 */ 2583 cp = strchr(zc->zc_value, '@'); 2584 if (cp) 2585 *cp = '\0'; 2586 (void) dmu_objset_find(zc->zc_value, 2587 zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS); 2588 return (dsl_dataset_promote(zc->zc_name)); 2589 } 2590 2591 /* 2592 * We don't want to have a hard dependency 2593 * against some special symbols in sharefs 2594 * nfs, and smbsrv. Determine them if needed when 2595 * the first file system is shared. 2596 * Neither sharefs, nfs or smbsrv are unloadable modules. 2597 */ 2598 int (*znfsexport_fs)(void *arg); 2599 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t); 2600 int (*zsmbexport_fs)(void *arg, boolean_t add_share); 2601 2602 int zfs_nfsshare_inited; 2603 int zfs_smbshare_inited; 2604 2605 ddi_modhandle_t nfs_mod; 2606 ddi_modhandle_t sharefs_mod; 2607 ddi_modhandle_t smbsrv_mod; 2608 kmutex_t zfs_share_lock; 2609 2610 static int 2611 zfs_init_sharefs() 2612 { 2613 int error; 2614 2615 ASSERT(MUTEX_HELD(&zfs_share_lock)); 2616 /* Both NFS and SMB shares also require sharetab support. */ 2617 if (sharefs_mod == NULL && ((sharefs_mod = 2618 ddi_modopen("fs/sharefs", 2619 KRTLD_MODE_FIRST, &error)) == NULL)) { 2620 return (ENOSYS); 2621 } 2622 if (zshare_fs == NULL && ((zshare_fs = 2623 (int (*)(enum sharefs_sys_op, share_t *, uint32_t)) 2624 ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) { 2625 return (ENOSYS); 2626 } 2627 return (0); 2628 } 2629 2630 static int 2631 zfs_ioc_share(zfs_cmd_t *zc) 2632 { 2633 int error; 2634 int opcode; 2635 2636 switch (zc->zc_share.z_sharetype) { 2637 case ZFS_SHARE_NFS: 2638 case ZFS_UNSHARE_NFS: 2639 if (zfs_nfsshare_inited == 0) { 2640 mutex_enter(&zfs_share_lock); 2641 if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs", 2642 KRTLD_MODE_FIRST, &error)) == NULL)) { 2643 mutex_exit(&zfs_share_lock); 2644 return (ENOSYS); 2645 } 2646 if (znfsexport_fs == NULL && 2647 ((znfsexport_fs = (int (*)(void *)) 2648 ddi_modsym(nfs_mod, 2649 "nfs_export", &error)) == NULL)) { 2650 mutex_exit(&zfs_share_lock); 2651 return (ENOSYS); 2652 } 2653 error = zfs_init_sharefs(); 2654 if (error) { 2655 mutex_exit(&zfs_share_lock); 2656 return (ENOSYS); 2657 } 2658 zfs_nfsshare_inited = 1; 2659 mutex_exit(&zfs_share_lock); 2660 } 2661 break; 2662 case ZFS_SHARE_SMB: 2663 case ZFS_UNSHARE_SMB: 2664 if (zfs_smbshare_inited == 0) { 2665 mutex_enter(&zfs_share_lock); 2666 if (smbsrv_mod == NULL && ((smbsrv_mod = 2667 ddi_modopen("drv/smbsrv", 2668 KRTLD_MODE_FIRST, &error)) == NULL)) { 2669 mutex_exit(&zfs_share_lock); 2670 return (ENOSYS); 2671 } 2672 if (zsmbexport_fs == NULL && ((zsmbexport_fs = 2673 (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod, 2674 "lmshrd_share_upcall", &error)) == NULL)) { 2675 mutex_exit(&zfs_share_lock); 2676 return (ENOSYS); 2677 } 2678 error = zfs_init_sharefs(); 2679 if (error) { 2680 mutex_exit(&zfs_share_lock); 2681 return (ENOSYS); 2682 } 2683 zfs_smbshare_inited = 1; 2684 mutex_exit(&zfs_share_lock); 2685 } 2686 break; 2687 default: 2688 return (EINVAL); 2689 } 2690 2691 switch (zc->zc_share.z_sharetype) { 2692 case ZFS_SHARE_NFS: 2693 case ZFS_UNSHARE_NFS: 2694 if (error = 2695 znfsexport_fs((void *) 2696 (uintptr_t)zc->zc_share.z_exportdata)) 2697 return (error); 2698 break; 2699 case ZFS_SHARE_SMB: 2700 case ZFS_UNSHARE_SMB: 2701 if (error = zsmbexport_fs((void *) 2702 (uintptr_t)zc->zc_share.z_exportdata, 2703 zc->zc_share.z_sharetype == ZFS_SHARE_SMB ? 2704 B_TRUE : B_FALSE)) { 2705 return (error); 2706 } 2707 break; 2708 } 2709 2710 opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS || 2711 zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ? 2712 SHAREFS_ADD : SHAREFS_REMOVE; 2713 2714 /* 2715 * Add or remove share from sharetab 2716 */ 2717 error = zshare_fs(opcode, 2718 (void *)(uintptr_t)zc->zc_share.z_sharedata, 2719 zc->zc_share.z_sharemax); 2720 2721 return (error); 2722 2723 } 2724 2725 /* 2726 * pool create, destroy, and export don't log the history as part of 2727 * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export 2728 * do the logging of those commands. 2729 */ 2730 static zfs_ioc_vec_t zfs_ioc_vec[] = { 2731 { zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE }, 2732 { zfs_ioc_pool_destroy, zfs_secpolicy_config, POOL_NAME, B_FALSE }, 2733 { zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 2734 { zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE }, 2735 { zfs_ioc_pool_configs, zfs_secpolicy_none, NO_NAME, B_FALSE }, 2736 { zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE }, 2737 { zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE }, 2738 { zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 2739 { zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE }, 2740 { zfs_ioc_pool_upgrade, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 2741 { zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE }, 2742 { zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 2743 { zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 2744 { zfs_ioc_vdev_set_state, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 2745 { zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 2746 { zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 2747 { zfs_ioc_vdev_setpath, zfs_secpolicy_config, POOL_NAME, B_FALSE }, 2748 { zfs_ioc_objset_stats, zfs_secpolicy_read, DATASET_NAME, B_FALSE }, 2749 { zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE }, 2750 { zfs_ioc_dataset_list_next, zfs_secpolicy_read, 2751 DATASET_NAME, B_FALSE }, 2752 { zfs_ioc_snapshot_list_next, zfs_secpolicy_read, 2753 DATASET_NAME, B_FALSE }, 2754 { zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE }, 2755 { zfs_ioc_create_minor, zfs_secpolicy_minor, DATASET_NAME, B_FALSE }, 2756 { zfs_ioc_remove_minor, zfs_secpolicy_minor, DATASET_NAME, B_FALSE }, 2757 { zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE }, 2758 { zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE }, 2759 { zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE }, 2760 { zfs_ioc_rename, zfs_secpolicy_rename, DATASET_NAME, B_TRUE }, 2761 { zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE }, 2762 { zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE }, 2763 { zfs_ioc_inject_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE }, 2764 { zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE }, 2765 { zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE }, 2766 { zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE }, 2767 { zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 2768 { zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE }, 2769 { zfs_ioc_destroy_snaps, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE }, 2770 { zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE }, 2771 { zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE }, 2772 { zfs_ioc_obj_to_path, zfs_secpolicy_config, NO_NAME, B_FALSE }, 2773 { zfs_ioc_pool_set_props, zfs_secpolicy_config, POOL_NAME, B_TRUE }, 2774 { zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE }, 2775 { zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE }, 2776 { zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE }, 2777 { zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi, 2778 DATASET_NAME, B_FALSE }, 2779 { zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE }, 2780 { zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE }, 2781 }; 2782 2783 static int 2784 zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 2785 { 2786 zfs_cmd_t *zc; 2787 uint_t vec; 2788 int error, rc; 2789 2790 if (getminor(dev) != 0) 2791 return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp)); 2792 2793 vec = cmd - ZFS_IOC; 2794 ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip)); 2795 2796 if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0])) 2797 return (EINVAL); 2798 2799 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP); 2800 2801 error = xcopyin((void *)arg, zc, sizeof (zfs_cmd_t)); 2802 2803 if (error == 0) 2804 error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr); 2805 2806 /* 2807 * Ensure that all pool/dataset names are valid before we pass down to 2808 * the lower layers. 2809 */ 2810 if (error == 0) { 2811 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0'; 2812 switch (zfs_ioc_vec[vec].zvec_namecheck) { 2813 case POOL_NAME: 2814 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0) 2815 error = EINVAL; 2816 break; 2817 2818 case DATASET_NAME: 2819 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0) 2820 error = EINVAL; 2821 break; 2822 2823 case NO_NAME: 2824 break; 2825 } 2826 } 2827 2828 if (error == 0) 2829 error = zfs_ioc_vec[vec].zvec_func(zc); 2830 2831 rc = xcopyout(zc, (void *)arg, sizeof (zfs_cmd_t)); 2832 if (error == 0) { 2833 error = rc; 2834 if (zfs_ioc_vec[vec].zvec_his_log == B_TRUE) 2835 zfs_log_history(zc); 2836 } 2837 2838 kmem_free(zc, sizeof (zfs_cmd_t)); 2839 return (error); 2840 } 2841 2842 static int 2843 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 2844 { 2845 if (cmd != DDI_ATTACH) 2846 return (DDI_FAILURE); 2847 2848 if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0, 2849 DDI_PSEUDO, 0) == DDI_FAILURE) 2850 return (DDI_FAILURE); 2851 2852 zfs_dip = dip; 2853 2854 ddi_report_dev(dip); 2855 2856 return (DDI_SUCCESS); 2857 } 2858 2859 static int 2860 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 2861 { 2862 if (spa_busy() || zfs_busy() || zvol_busy()) 2863 return (DDI_FAILURE); 2864 2865 if (cmd != DDI_DETACH) 2866 return (DDI_FAILURE); 2867 2868 zfs_dip = NULL; 2869 2870 ddi_prop_remove_all(dip); 2871 ddi_remove_minor_node(dip, NULL); 2872 2873 return (DDI_SUCCESS); 2874 } 2875 2876 /*ARGSUSED*/ 2877 static int 2878 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 2879 { 2880 switch (infocmd) { 2881 case DDI_INFO_DEVT2DEVINFO: 2882 *result = zfs_dip; 2883 return (DDI_SUCCESS); 2884 2885 case DDI_INFO_DEVT2INSTANCE: 2886 *result = (void *)0; 2887 return (DDI_SUCCESS); 2888 } 2889 2890 return (DDI_FAILURE); 2891 } 2892 2893 /* 2894 * OK, so this is a little weird. 2895 * 2896 * /dev/zfs is the control node, i.e. minor 0. 2897 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0. 2898 * 2899 * /dev/zfs has basically nothing to do except serve up ioctls, 2900 * so most of the standard driver entry points are in zvol.c. 2901 */ 2902 static struct cb_ops zfs_cb_ops = { 2903 zvol_open, /* open */ 2904 zvol_close, /* close */ 2905 zvol_strategy, /* strategy */ 2906 nodev, /* print */ 2907 nodev, /* dump */ 2908 zvol_read, /* read */ 2909 zvol_write, /* write */ 2910 zfsdev_ioctl, /* ioctl */ 2911 nodev, /* devmap */ 2912 nodev, /* mmap */ 2913 nodev, /* segmap */ 2914 nochpoll, /* poll */ 2915 ddi_prop_op, /* prop_op */ 2916 NULL, /* streamtab */ 2917 D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */ 2918 CB_REV, /* version */ 2919 nodev, /* async read */ 2920 nodev, /* async write */ 2921 }; 2922 2923 static struct dev_ops zfs_dev_ops = { 2924 DEVO_REV, /* version */ 2925 0, /* refcnt */ 2926 zfs_info, /* info */ 2927 nulldev, /* identify */ 2928 nulldev, /* probe */ 2929 zfs_attach, /* attach */ 2930 zfs_detach, /* detach */ 2931 nodev, /* reset */ 2932 &zfs_cb_ops, /* driver operations */ 2933 NULL /* no bus operations */ 2934 }; 2935 2936 static struct modldrv zfs_modldrv = { 2937 &mod_driverops, "ZFS storage pool version " SPA_VERSION_STRING, 2938 &zfs_dev_ops 2939 }; 2940 2941 static struct modlinkage modlinkage = { 2942 MODREV_1, 2943 (void *)&zfs_modlfs, 2944 (void *)&zfs_modldrv, 2945 NULL 2946 }; 2947 2948 2949 uint_t zfs_fsyncer_key; 2950 extern uint_t rrw_tsd_key; 2951 2952 int 2953 _init(void) 2954 { 2955 int error; 2956 2957 spa_init(FREAD | FWRITE); 2958 zfs_init(); 2959 zvol_init(); 2960 2961 if ((error = mod_install(&modlinkage)) != 0) { 2962 zvol_fini(); 2963 zfs_fini(); 2964 spa_fini(); 2965 return (error); 2966 } 2967 2968 tsd_create(&zfs_fsyncer_key, NULL); 2969 tsd_create(&rrw_tsd_key, NULL); 2970 2971 error = ldi_ident_from_mod(&modlinkage, &zfs_li); 2972 ASSERT(error == 0); 2973 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL); 2974 2975 return (0); 2976 } 2977 2978 int 2979 _fini(void) 2980 { 2981 int error; 2982 2983 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled) 2984 return (EBUSY); 2985 2986 if ((error = mod_remove(&modlinkage)) != 0) 2987 return (error); 2988 2989 zvol_fini(); 2990 zfs_fini(); 2991 spa_fini(); 2992 if (zfs_nfsshare_inited) 2993 (void) ddi_modclose(nfs_mod); 2994 if (zfs_smbshare_inited) 2995 (void) ddi_modclose(smbsrv_mod); 2996 if (zfs_nfsshare_inited || zfs_smbshare_inited) 2997 (void) ddi_modclose(sharefs_mod); 2998 2999 tsd_destroy(&zfs_fsyncer_key); 3000 ldi_ident_release(zfs_li); 3001 zfs_li = NULL; 3002 mutex_destroy(&zfs_share_lock); 3003 3004 return (error); 3005 } 3006 3007 int 3008 _info(struct modinfo *modinfop) 3009 { 3010 return (mod_info(&modlinkage, modinfop)); 3011 } 3012