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 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #include <sys/types.h> 27 #include <sys/param.h> 28 #include <sys/errno.h> 29 #include <sys/uio.h> 30 #include <sys/buf.h> 31 #include <sys/modctl.h> 32 #include <sys/open.h> 33 #include <sys/file.h> 34 #include <sys/kmem.h> 35 #include <sys/conf.h> 36 #include <sys/cmn_err.h> 37 #include <sys/stat.h> 38 #include <sys/zfs_ioctl.h> 39 #include <sys/zfs_znode.h> 40 #include <sys/zap.h> 41 #include <sys/spa.h> 42 #include <sys/spa_impl.h> 43 #include <sys/vdev.h> 44 #include <sys/vdev_impl.h> 45 #include <sys/dmu.h> 46 #include <sys/dsl_dir.h> 47 #include <sys/dsl_dataset.h> 48 #include <sys/dsl_prop.h> 49 #include <sys/dsl_deleg.h> 50 #include <sys/dmu_objset.h> 51 #include <sys/ddi.h> 52 #include <sys/sunddi.h> 53 #include <sys/sunldi.h> 54 #include <sys/policy.h> 55 #include <sys/zone.h> 56 #include <sys/nvpair.h> 57 #include <sys/pathname.h> 58 #include <sys/mount.h> 59 #include <sys/sdt.h> 60 #include <sys/fs/zfs.h> 61 #include <sys/zfs_ctldir.h> 62 #include <sys/zfs_dir.h> 63 #include <sys/zvol.h> 64 #include <sharefs/share.h> 65 #include <sys/dmu_objset.h> 66 67 #include "zfs_namecheck.h" 68 #include "zfs_prop.h" 69 #include "zfs_deleg.h" 70 71 extern struct modlfs zfs_modlfs; 72 73 extern void zfs_init(void); 74 extern void zfs_fini(void); 75 76 ldi_ident_t zfs_li = NULL; 77 dev_info_t *zfs_dip; 78 79 typedef int zfs_ioc_func_t(zfs_cmd_t *); 80 typedef int zfs_secpolicy_func_t(zfs_cmd_t *, cred_t *); 81 82 typedef enum { 83 NO_NAME, 84 POOL_NAME, 85 DATASET_NAME 86 } zfs_ioc_namecheck_t; 87 88 typedef struct zfs_ioc_vec { 89 zfs_ioc_func_t *zvec_func; 90 zfs_secpolicy_func_t *zvec_secpolicy; 91 zfs_ioc_namecheck_t zvec_namecheck; 92 boolean_t zvec_his_log; 93 boolean_t zvec_pool_check; 94 } zfs_ioc_vec_t; 95 96 /* This array is indexed by zfs_userquota_prop_t */ 97 static const char *userquota_perms[] = { 98 ZFS_DELEG_PERM_USERUSED, 99 ZFS_DELEG_PERM_USERQUOTA, 100 ZFS_DELEG_PERM_GROUPUSED, 101 ZFS_DELEG_PERM_GROUPQUOTA, 102 }; 103 104 static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc); 105 static void clear_props(char *dataset, nvlist_t *props, nvlist_t *newprops); 106 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *, 107 boolean_t *); 108 int zfs_set_prop_nvlist(const char *, nvlist_t *); 109 110 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */ 111 void 112 __dprintf(const char *file, const char *func, int line, const char *fmt, ...) 113 { 114 const char *newfile; 115 char buf[256]; 116 va_list adx; 117 118 /* 119 * Get rid of annoying "../common/" prefix to filename. 120 */ 121 newfile = strrchr(file, '/'); 122 if (newfile != NULL) { 123 newfile = newfile + 1; /* Get rid of leading / */ 124 } else { 125 newfile = file; 126 } 127 128 va_start(adx, fmt); 129 (void) vsnprintf(buf, sizeof (buf), fmt, adx); 130 va_end(adx); 131 132 /* 133 * To get this data, use the zfs-dprintf probe as so: 134 * dtrace -q -n 'zfs-dprintf \ 135 * /stringof(arg0) == "dbuf.c"/ \ 136 * {printf("%s: %s", stringof(arg1), stringof(arg3))}' 137 * arg0 = file name 138 * arg1 = function name 139 * arg2 = line number 140 * arg3 = message 141 */ 142 DTRACE_PROBE4(zfs__dprintf, 143 char *, newfile, char *, func, int, line, char *, buf); 144 } 145 146 static void 147 history_str_free(char *buf) 148 { 149 kmem_free(buf, HIS_MAX_RECORD_LEN); 150 } 151 152 static char * 153 history_str_get(zfs_cmd_t *zc) 154 { 155 char *buf; 156 157 if (zc->zc_history == NULL) 158 return (NULL); 159 160 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP); 161 if (copyinstr((void *)(uintptr_t)zc->zc_history, 162 buf, HIS_MAX_RECORD_LEN, NULL) != 0) { 163 history_str_free(buf); 164 return (NULL); 165 } 166 167 buf[HIS_MAX_RECORD_LEN -1] = '\0'; 168 169 return (buf); 170 } 171 172 /* 173 * Check to see if the named dataset is currently defined as bootable 174 */ 175 static boolean_t 176 zfs_is_bootfs(const char *name) 177 { 178 spa_t *spa; 179 boolean_t ret = B_FALSE; 180 181 if (spa_open(name, &spa, FTAG) == 0) { 182 if (spa->spa_bootfs) { 183 objset_t *os; 184 185 if (dmu_objset_open(name, DMU_OST_ZFS, 186 DS_MODE_USER | DS_MODE_READONLY, &os) == 0) { 187 ret = (dmu_objset_id(os) == spa->spa_bootfs); 188 dmu_objset_close(os); 189 } 190 } 191 spa_close(spa, FTAG); 192 } 193 return (ret); 194 } 195 196 /* 197 * zfs_earlier_version 198 * 199 * Return non-zero if the spa version is less than requested version. 200 */ 201 static int 202 zfs_earlier_version(const char *name, int version) 203 { 204 spa_t *spa; 205 206 if (spa_open(name, &spa, FTAG) == 0) { 207 if (spa_version(spa) < version) { 208 spa_close(spa, FTAG); 209 return (1); 210 } 211 spa_close(spa, FTAG); 212 } 213 return (0); 214 } 215 216 /* 217 * zpl_earlier_version 218 * 219 * Return TRUE if the ZPL version is less than requested version. 220 */ 221 static boolean_t 222 zpl_earlier_version(const char *name, int version) 223 { 224 objset_t *os; 225 boolean_t rc = B_TRUE; 226 227 if (dmu_objset_open(name, DMU_OST_ANY, 228 DS_MODE_USER | DS_MODE_READONLY, &os) == 0) { 229 uint64_t zplversion; 230 231 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0) 232 rc = zplversion < version; 233 dmu_objset_close(os); 234 } 235 return (rc); 236 } 237 238 static void 239 zfs_log_history(zfs_cmd_t *zc) 240 { 241 spa_t *spa; 242 char *buf; 243 244 if ((buf = history_str_get(zc)) == NULL) 245 return; 246 247 if (spa_open(zc->zc_name, &spa, FTAG) == 0) { 248 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY) 249 (void) spa_history_log(spa, buf, LOG_CMD_NORMAL); 250 spa_close(spa, FTAG); 251 } 252 history_str_free(buf); 253 } 254 255 /* 256 * Policy for top-level read operations (list pools). Requires no privileges, 257 * and can be used in the local zone, as there is no associated dataset. 258 */ 259 /* ARGSUSED */ 260 static int 261 zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr) 262 { 263 return (0); 264 } 265 266 /* 267 * Policy for dataset read operations (list children, get statistics). Requires 268 * no privileges, but must be visible in the local zone. 269 */ 270 /* ARGSUSED */ 271 static int 272 zfs_secpolicy_read(zfs_cmd_t *zc, cred_t *cr) 273 { 274 if (INGLOBALZONE(curproc) || 275 zone_dataset_visible(zc->zc_name, NULL)) 276 return (0); 277 278 return (ENOENT); 279 } 280 281 static int 282 zfs_dozonecheck(const char *dataset, cred_t *cr) 283 { 284 uint64_t zoned; 285 int writable = 1; 286 287 /* 288 * The dataset must be visible by this zone -- check this first 289 * so they don't see EPERM on something they shouldn't know about. 290 */ 291 if (!INGLOBALZONE(curproc) && 292 !zone_dataset_visible(dataset, &writable)) 293 return (ENOENT); 294 295 if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL)) 296 return (ENOENT); 297 298 if (INGLOBALZONE(curproc)) { 299 /* 300 * If the fs is zoned, only root can access it from the 301 * global zone. 302 */ 303 if (secpolicy_zfs(cr) && zoned) 304 return (EPERM); 305 } else { 306 /* 307 * If we are in a local zone, the 'zoned' property must be set. 308 */ 309 if (!zoned) 310 return (EPERM); 311 312 /* must be writable by this zone */ 313 if (!writable) 314 return (EPERM); 315 } 316 return (0); 317 } 318 319 int 320 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr) 321 { 322 int error; 323 324 error = zfs_dozonecheck(name, cr); 325 if (error == 0) { 326 error = secpolicy_zfs(cr); 327 if (error) 328 error = dsl_deleg_access(name, perm, cr); 329 } 330 return (error); 331 } 332 333 static int 334 zfs_secpolicy_setprop(const char *name, zfs_prop_t prop, cred_t *cr) 335 { 336 /* 337 * Check permissions for special properties. 338 */ 339 switch (prop) { 340 case ZFS_PROP_ZONED: 341 /* 342 * Disallow setting of 'zoned' from within a local zone. 343 */ 344 if (!INGLOBALZONE(curproc)) 345 return (EPERM); 346 break; 347 348 case ZFS_PROP_QUOTA: 349 if (!INGLOBALZONE(curproc)) { 350 uint64_t zoned; 351 char setpoint[MAXNAMELEN]; 352 /* 353 * Unprivileged users are allowed to modify the 354 * quota on things *under* (ie. contained by) 355 * the thing they own. 356 */ 357 if (dsl_prop_get_integer(name, "zoned", &zoned, 358 setpoint)) 359 return (EPERM); 360 if (!zoned || strlen(name) <= strlen(setpoint)) 361 return (EPERM); 362 } 363 break; 364 } 365 366 return (zfs_secpolicy_write_perms(name, zfs_prop_to_name(prop), cr)); 367 } 368 369 int 370 zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr) 371 { 372 int error; 373 374 error = zfs_dozonecheck(zc->zc_name, cr); 375 if (error) 376 return (error); 377 378 /* 379 * permission to set permissions will be evaluated later in 380 * dsl_deleg_can_allow() 381 */ 382 return (0); 383 } 384 385 int 386 zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr) 387 { 388 int error; 389 error = zfs_secpolicy_write_perms(zc->zc_name, 390 ZFS_DELEG_PERM_ROLLBACK, cr); 391 if (error == 0) 392 error = zfs_secpolicy_write_perms(zc->zc_name, 393 ZFS_DELEG_PERM_MOUNT, cr); 394 return (error); 395 } 396 397 int 398 zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr) 399 { 400 return (zfs_secpolicy_write_perms(zc->zc_name, 401 ZFS_DELEG_PERM_SEND, cr)); 402 } 403 404 static int 405 zfs_secpolicy_deleg_share(zfs_cmd_t *zc, cred_t *cr) 406 { 407 vnode_t *vp; 408 int error; 409 410 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE, 411 NO_FOLLOW, NULL, &vp)) != 0) 412 return (error); 413 414 /* Now make sure mntpnt and dataset are ZFS */ 415 416 if (vp->v_vfsp->vfs_fstype != zfsfstype || 417 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource), 418 zc->zc_name) != 0)) { 419 VN_RELE(vp); 420 return (EPERM); 421 } 422 423 VN_RELE(vp); 424 return (dsl_deleg_access(zc->zc_name, 425 ZFS_DELEG_PERM_SHARE, cr)); 426 } 427 428 int 429 zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr) 430 { 431 if (!INGLOBALZONE(curproc)) 432 return (EPERM); 433 434 if (secpolicy_nfs(cr) == 0) { 435 return (0); 436 } else { 437 return (zfs_secpolicy_deleg_share(zc, cr)); 438 } 439 } 440 441 int 442 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, cred_t *cr) 443 { 444 if (!INGLOBALZONE(curproc)) 445 return (EPERM); 446 447 if (secpolicy_smb(cr) == 0) { 448 return (0); 449 } else { 450 return (zfs_secpolicy_deleg_share(zc, cr)); 451 } 452 } 453 454 static int 455 zfs_get_parent(const char *datasetname, char *parent, int parentsize) 456 { 457 char *cp; 458 459 /* 460 * Remove the @bla or /bla from the end of the name to get the parent. 461 */ 462 (void) strncpy(parent, datasetname, parentsize); 463 cp = strrchr(parent, '@'); 464 if (cp != NULL) { 465 cp[0] = '\0'; 466 } else { 467 cp = strrchr(parent, '/'); 468 if (cp == NULL) 469 return (ENOENT); 470 cp[0] = '\0'; 471 } 472 473 return (0); 474 } 475 476 int 477 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr) 478 { 479 int error; 480 481 if ((error = zfs_secpolicy_write_perms(name, 482 ZFS_DELEG_PERM_MOUNT, cr)) != 0) 483 return (error); 484 485 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr)); 486 } 487 488 static int 489 zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr) 490 { 491 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr)); 492 } 493 494 /* 495 * Must have sys_config privilege to check the iscsi permission 496 */ 497 /* ARGSUSED */ 498 static int 499 zfs_secpolicy_iscsi(zfs_cmd_t *zc, cred_t *cr) 500 { 501 return (secpolicy_zfs(cr)); 502 } 503 504 int 505 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr) 506 { 507 char parentname[MAXNAMELEN]; 508 int error; 509 510 if ((error = zfs_secpolicy_write_perms(from, 511 ZFS_DELEG_PERM_RENAME, cr)) != 0) 512 return (error); 513 514 if ((error = zfs_secpolicy_write_perms(from, 515 ZFS_DELEG_PERM_MOUNT, cr)) != 0) 516 return (error); 517 518 if ((error = zfs_get_parent(to, parentname, 519 sizeof (parentname))) != 0) 520 return (error); 521 522 if ((error = zfs_secpolicy_write_perms(parentname, 523 ZFS_DELEG_PERM_CREATE, cr)) != 0) 524 return (error); 525 526 if ((error = zfs_secpolicy_write_perms(parentname, 527 ZFS_DELEG_PERM_MOUNT, cr)) != 0) 528 return (error); 529 530 return (error); 531 } 532 533 static int 534 zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr) 535 { 536 return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr)); 537 } 538 539 static int 540 zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr) 541 { 542 char parentname[MAXNAMELEN]; 543 objset_t *clone; 544 int error; 545 546 error = zfs_secpolicy_write_perms(zc->zc_name, 547 ZFS_DELEG_PERM_PROMOTE, cr); 548 if (error) 549 return (error); 550 551 error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, 552 DS_MODE_USER | DS_MODE_READONLY, &clone); 553 554 if (error == 0) { 555 dsl_dataset_t *pclone = NULL; 556 dsl_dir_t *dd; 557 dd = clone->os->os_dsl_dataset->ds_dir; 558 559 rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER); 560 error = dsl_dataset_hold_obj(dd->dd_pool, 561 dd->dd_phys->dd_origin_obj, FTAG, &pclone); 562 rw_exit(&dd->dd_pool->dp_config_rwlock); 563 if (error) { 564 dmu_objset_close(clone); 565 return (error); 566 } 567 568 error = zfs_secpolicy_write_perms(zc->zc_name, 569 ZFS_DELEG_PERM_MOUNT, cr); 570 571 dsl_dataset_name(pclone, parentname); 572 dmu_objset_close(clone); 573 dsl_dataset_rele(pclone, FTAG); 574 if (error == 0) 575 error = zfs_secpolicy_write_perms(parentname, 576 ZFS_DELEG_PERM_PROMOTE, cr); 577 } 578 return (error); 579 } 580 581 static int 582 zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr) 583 { 584 int error; 585 586 if ((error = zfs_secpolicy_write_perms(zc->zc_name, 587 ZFS_DELEG_PERM_RECEIVE, cr)) != 0) 588 return (error); 589 590 if ((error = zfs_secpolicy_write_perms(zc->zc_name, 591 ZFS_DELEG_PERM_MOUNT, cr)) != 0) 592 return (error); 593 594 return (zfs_secpolicy_write_perms(zc->zc_name, 595 ZFS_DELEG_PERM_CREATE, cr)); 596 } 597 598 int 599 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr) 600 { 601 int error; 602 603 if ((error = zfs_secpolicy_write_perms(name, 604 ZFS_DELEG_PERM_SNAPSHOT, cr)) != 0) 605 return (error); 606 607 error = zfs_secpolicy_write_perms(name, 608 ZFS_DELEG_PERM_MOUNT, cr); 609 610 return (error); 611 } 612 613 static int 614 zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr) 615 { 616 617 return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr)); 618 } 619 620 static int 621 zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr) 622 { 623 char parentname[MAXNAMELEN]; 624 int error; 625 626 if ((error = zfs_get_parent(zc->zc_name, parentname, 627 sizeof (parentname))) != 0) 628 return (error); 629 630 if (zc->zc_value[0] != '\0') { 631 if ((error = zfs_secpolicy_write_perms(zc->zc_value, 632 ZFS_DELEG_PERM_CLONE, cr)) != 0) 633 return (error); 634 } 635 636 if ((error = zfs_secpolicy_write_perms(parentname, 637 ZFS_DELEG_PERM_CREATE, cr)) != 0) 638 return (error); 639 640 error = zfs_secpolicy_write_perms(parentname, 641 ZFS_DELEG_PERM_MOUNT, cr); 642 643 return (error); 644 } 645 646 static int 647 zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr) 648 { 649 int error; 650 651 error = secpolicy_fs_unmount(cr, NULL); 652 if (error) { 653 error = dsl_deleg_access(zc->zc_name, ZFS_DELEG_PERM_MOUNT, cr); 654 } 655 return (error); 656 } 657 658 /* 659 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires 660 * SYS_CONFIG privilege, which is not available in a local zone. 661 */ 662 /* ARGSUSED */ 663 static int 664 zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr) 665 { 666 if (secpolicy_sys_config(cr, B_FALSE) != 0) 667 return (EPERM); 668 669 return (0); 670 } 671 672 /* 673 * Just like zfs_secpolicy_config, except that we will check for 674 * mount permission on the dataset for permission to create/remove 675 * the minor nodes. 676 */ 677 static int 678 zfs_secpolicy_minor(zfs_cmd_t *zc, cred_t *cr) 679 { 680 if (secpolicy_sys_config(cr, B_FALSE) != 0) { 681 return (dsl_deleg_access(zc->zc_name, 682 ZFS_DELEG_PERM_MOUNT, cr)); 683 } 684 685 return (0); 686 } 687 688 /* 689 * Policy for fault injection. Requires all privileges. 690 */ 691 /* ARGSUSED */ 692 static int 693 zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr) 694 { 695 return (secpolicy_zinject(cr)); 696 } 697 698 static int 699 zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr) 700 { 701 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value); 702 703 if (prop == ZPROP_INVAL) { 704 if (!zfs_prop_user(zc->zc_value)) 705 return (EINVAL); 706 return (zfs_secpolicy_write_perms(zc->zc_name, 707 ZFS_DELEG_PERM_USERPROP, cr)); 708 } else { 709 if (!zfs_prop_inheritable(prop)) 710 return (EINVAL); 711 return (zfs_secpolicy_setprop(zc->zc_name, prop, cr)); 712 } 713 } 714 715 static int 716 zfs_secpolicy_userspace_one(zfs_cmd_t *zc, cred_t *cr) 717 { 718 int err = zfs_secpolicy_read(zc, cr); 719 if (err) 720 return (err); 721 722 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) 723 return (EINVAL); 724 725 if (zc->zc_value[0] == 0) { 726 /* 727 * They are asking about a posix uid/gid. If it's 728 * themself, allow it. 729 */ 730 if (zc->zc_objset_type == ZFS_PROP_USERUSED || 731 zc->zc_objset_type == ZFS_PROP_USERQUOTA) { 732 if (zc->zc_guid == crgetuid(cr)) 733 return (0); 734 } else { 735 if (groupmember(zc->zc_guid, cr)) 736 return (0); 737 } 738 } 739 740 return (zfs_secpolicy_write_perms(zc->zc_name, 741 userquota_perms[zc->zc_objset_type], cr)); 742 } 743 744 static int 745 zfs_secpolicy_userspace_many(zfs_cmd_t *zc, cred_t *cr) 746 { 747 int err = zfs_secpolicy_read(zc, cr); 748 if (err) 749 return (err); 750 751 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) 752 return (EINVAL); 753 754 return (zfs_secpolicy_write_perms(zc->zc_name, 755 userquota_perms[zc->zc_objset_type], cr)); 756 } 757 758 static int 759 zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, cred_t *cr) 760 { 761 return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION, cr)); 762 } 763 764 /* 765 * Returns the nvlist as specified by the user in the zfs_cmd_t. 766 */ 767 static int 768 get_nvlist(uint64_t nvl, uint64_t size, nvlist_t **nvp) 769 { 770 char *packed; 771 int error; 772 nvlist_t *list = NULL; 773 774 /* 775 * Read in and unpack the user-supplied nvlist. 776 */ 777 if (size == 0) 778 return (EINVAL); 779 780 packed = kmem_alloc(size, KM_SLEEP); 781 782 if ((error = xcopyin((void *)(uintptr_t)nvl, packed, size)) != 0) { 783 kmem_free(packed, size); 784 return (error); 785 } 786 787 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) { 788 kmem_free(packed, size); 789 return (error); 790 } 791 792 kmem_free(packed, size); 793 794 *nvp = list; 795 return (0); 796 } 797 798 static int 799 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl) 800 { 801 char *packed = NULL; 802 size_t size; 803 int error; 804 805 VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0); 806 807 if (size > zc->zc_nvlist_dst_size) { 808 error = ENOMEM; 809 } else { 810 packed = kmem_alloc(size, KM_SLEEP); 811 VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE, 812 KM_SLEEP) == 0); 813 error = xcopyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst, 814 size); 815 kmem_free(packed, size); 816 } 817 818 zc->zc_nvlist_dst_size = size; 819 return (error); 820 } 821 822 static int 823 getzfsvfs(const char *dsname, zfsvfs_t **zvp) 824 { 825 objset_t *os; 826 int error; 827 828 error = dmu_objset_open(dsname, DMU_OST_ZFS, 829 DS_MODE_USER | DS_MODE_READONLY, &os); 830 if (error) 831 return (error); 832 833 mutex_enter(&os->os->os_user_ptr_lock); 834 *zvp = dmu_objset_get_user(os); 835 if (*zvp) { 836 VFS_HOLD((*zvp)->z_vfs); 837 } else { 838 error = ESRCH; 839 } 840 mutex_exit(&os->os->os_user_ptr_lock); 841 dmu_objset_close(os); 842 return (error); 843 } 844 845 /* 846 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which 847 * case its z_vfs will be NULL, and it will be opened as the owner. 848 */ 849 static int 850 zfsvfs_hold(const char *name, boolean_t readonly, void *tag, zfsvfs_t **zvp) 851 { 852 int error = 0; 853 int mode = DS_MODE_OWNER | (readonly ? DS_MODE_READONLY : 0); 854 855 if (getzfsvfs(name, zvp) != 0) 856 error = zfsvfs_create(name, mode, zvp); 857 if (error == 0) { 858 rrw_enter(&(*zvp)->z_teardown_lock, RW_READER, tag); 859 if ((*zvp)->z_unmounted) { 860 /* 861 * XXX we could probably try again, since the unmounting 862 * thread should be just about to disassociate the 863 * objset from the zfsvfs. 864 */ 865 rrw_exit(&(*zvp)->z_teardown_lock, tag); 866 return (EBUSY); 867 } 868 } 869 return (error); 870 } 871 872 static void 873 zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag) 874 { 875 rrw_exit(&zfsvfs->z_teardown_lock, tag); 876 877 if (zfsvfs->z_vfs) { 878 VFS_RELE(zfsvfs->z_vfs); 879 } else { 880 dmu_objset_close(zfsvfs->z_os); 881 zfsvfs_free(zfsvfs); 882 } 883 } 884 885 static int 886 zfs_ioc_pool_create(zfs_cmd_t *zc) 887 { 888 int error; 889 nvlist_t *config, *props = NULL; 890 nvlist_t *rootprops = NULL; 891 nvlist_t *zplprops = NULL; 892 char *buf; 893 894 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 895 &config)) 896 return (error); 897 898 if (zc->zc_nvlist_src_size != 0 && (error = 899 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, &props))) { 900 nvlist_free(config); 901 return (error); 902 } 903 904 if (props) { 905 nvlist_t *nvl = NULL; 906 uint64_t version = SPA_VERSION; 907 908 (void) nvlist_lookup_uint64(props, 909 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version); 910 if (version < SPA_VERSION_INITIAL || version > SPA_VERSION) { 911 error = EINVAL; 912 goto pool_props_bad; 913 } 914 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl); 915 if (nvl) { 916 error = nvlist_dup(nvl, &rootprops, KM_SLEEP); 917 if (error != 0) { 918 nvlist_free(config); 919 nvlist_free(props); 920 return (error); 921 } 922 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS); 923 } 924 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0); 925 error = zfs_fill_zplprops_root(version, rootprops, 926 zplprops, NULL); 927 if (error) 928 goto pool_props_bad; 929 } 930 931 buf = history_str_get(zc); 932 933 error = spa_create(zc->zc_name, config, props, buf, zplprops); 934 935 /* 936 * Set the remaining root properties 937 */ 938 if (!error && 939 (error = zfs_set_prop_nvlist(zc->zc_name, rootprops)) != 0) 940 (void) spa_destroy(zc->zc_name); 941 942 if (buf != NULL) 943 history_str_free(buf); 944 945 pool_props_bad: 946 nvlist_free(rootprops); 947 nvlist_free(zplprops); 948 nvlist_free(config); 949 nvlist_free(props); 950 951 return (error); 952 } 953 954 static int 955 zfs_ioc_pool_destroy(zfs_cmd_t *zc) 956 { 957 int error; 958 zfs_log_history(zc); 959 error = spa_destroy(zc->zc_name); 960 return (error); 961 } 962 963 static int 964 zfs_ioc_pool_import(zfs_cmd_t *zc) 965 { 966 int error; 967 nvlist_t *config, *props = NULL; 968 uint64_t guid; 969 970 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 971 &config)) != 0) 972 return (error); 973 974 if (zc->zc_nvlist_src_size != 0 && (error = 975 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, &props))) { 976 nvlist_free(config); 977 return (error); 978 } 979 980 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 || 981 guid != zc->zc_guid) 982 error = EINVAL; 983 else if (zc->zc_cookie) 984 error = spa_import_faulted(zc->zc_name, config, 985 props); 986 else 987 error = spa_import(zc->zc_name, config, props); 988 989 nvlist_free(config); 990 991 if (props) 992 nvlist_free(props); 993 994 return (error); 995 } 996 997 static int 998 zfs_ioc_pool_export(zfs_cmd_t *zc) 999 { 1000 int error; 1001 boolean_t force = (boolean_t)zc->zc_cookie; 1002 boolean_t hardforce = (boolean_t)zc->zc_guid; 1003 1004 zfs_log_history(zc); 1005 error = spa_export(zc->zc_name, NULL, force, hardforce); 1006 return (error); 1007 } 1008 1009 static int 1010 zfs_ioc_pool_configs(zfs_cmd_t *zc) 1011 { 1012 nvlist_t *configs; 1013 int error; 1014 1015 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL) 1016 return (EEXIST); 1017 1018 error = put_nvlist(zc, configs); 1019 1020 nvlist_free(configs); 1021 1022 return (error); 1023 } 1024 1025 static int 1026 zfs_ioc_pool_stats(zfs_cmd_t *zc) 1027 { 1028 nvlist_t *config; 1029 int error; 1030 int ret = 0; 1031 1032 error = spa_get_stats(zc->zc_name, &config, zc->zc_value, 1033 sizeof (zc->zc_value)); 1034 1035 if (config != NULL) { 1036 ret = put_nvlist(zc, config); 1037 nvlist_free(config); 1038 1039 /* 1040 * The config may be present even if 'error' is non-zero. 1041 * In this case we return success, and preserve the real errno 1042 * in 'zc_cookie'. 1043 */ 1044 zc->zc_cookie = error; 1045 } else { 1046 ret = error; 1047 } 1048 1049 return (ret); 1050 } 1051 1052 /* 1053 * Try to import the given pool, returning pool stats as appropriate so that 1054 * user land knows which devices are available and overall pool health. 1055 */ 1056 static int 1057 zfs_ioc_pool_tryimport(zfs_cmd_t *zc) 1058 { 1059 nvlist_t *tryconfig, *config; 1060 int error; 1061 1062 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 1063 &tryconfig)) != 0) 1064 return (error); 1065 1066 config = spa_tryimport(tryconfig); 1067 1068 nvlist_free(tryconfig); 1069 1070 if (config == NULL) 1071 return (EINVAL); 1072 1073 error = put_nvlist(zc, config); 1074 nvlist_free(config); 1075 1076 return (error); 1077 } 1078 1079 static int 1080 zfs_ioc_pool_scrub(zfs_cmd_t *zc) 1081 { 1082 spa_t *spa; 1083 int error; 1084 1085 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1086 return (error); 1087 1088 error = spa_scrub(spa, zc->zc_cookie); 1089 1090 spa_close(spa, FTAG); 1091 1092 return (error); 1093 } 1094 1095 static int 1096 zfs_ioc_pool_freeze(zfs_cmd_t *zc) 1097 { 1098 spa_t *spa; 1099 int error; 1100 1101 error = spa_open(zc->zc_name, &spa, FTAG); 1102 if (error == 0) { 1103 spa_freeze(spa); 1104 spa_close(spa, FTAG); 1105 } 1106 return (error); 1107 } 1108 1109 static int 1110 zfs_ioc_pool_upgrade(zfs_cmd_t *zc) 1111 { 1112 spa_t *spa; 1113 int error; 1114 1115 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1116 return (error); 1117 1118 if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) { 1119 spa_close(spa, FTAG); 1120 return (EINVAL); 1121 } 1122 1123 spa_upgrade(spa, zc->zc_cookie); 1124 spa_close(spa, FTAG); 1125 1126 return (error); 1127 } 1128 1129 static int 1130 zfs_ioc_pool_get_history(zfs_cmd_t *zc) 1131 { 1132 spa_t *spa; 1133 char *hist_buf; 1134 uint64_t size; 1135 int error; 1136 1137 if ((size = zc->zc_history_len) == 0) 1138 return (EINVAL); 1139 1140 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1141 return (error); 1142 1143 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) { 1144 spa_close(spa, FTAG); 1145 return (ENOTSUP); 1146 } 1147 1148 hist_buf = kmem_alloc(size, KM_SLEEP); 1149 if ((error = spa_history_get(spa, &zc->zc_history_offset, 1150 &zc->zc_history_len, hist_buf)) == 0) { 1151 error = xcopyout(hist_buf, 1152 (char *)(uintptr_t)zc->zc_history, 1153 zc->zc_history_len); 1154 } 1155 1156 spa_close(spa, FTAG); 1157 kmem_free(hist_buf, size); 1158 return (error); 1159 } 1160 1161 static int 1162 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc) 1163 { 1164 int error; 1165 1166 if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value)) 1167 return (error); 1168 1169 return (0); 1170 } 1171 1172 static int 1173 zfs_ioc_obj_to_path(zfs_cmd_t *zc) 1174 { 1175 objset_t *osp; 1176 int error; 1177 1178 if ((error = dmu_objset_open(zc->zc_name, DMU_OST_ZFS, 1179 DS_MODE_USER | DS_MODE_READONLY, &osp)) != 0) 1180 return (error); 1181 error = zfs_obj_to_path(osp, zc->zc_obj, zc->zc_value, 1182 sizeof (zc->zc_value)); 1183 dmu_objset_close(osp); 1184 1185 return (error); 1186 } 1187 1188 static int 1189 zfs_ioc_vdev_add(zfs_cmd_t *zc) 1190 { 1191 spa_t *spa; 1192 int error; 1193 nvlist_t *config, **l2cache, **spares; 1194 uint_t nl2cache = 0, nspares = 0; 1195 1196 error = spa_open(zc->zc_name, &spa, FTAG); 1197 if (error != 0) 1198 return (error); 1199 1200 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 1201 &config); 1202 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE, 1203 &l2cache, &nl2cache); 1204 1205 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES, 1206 &spares, &nspares); 1207 1208 /* 1209 * A root pool with concatenated devices is not supported. 1210 * Thus, can not add a device to a root pool. 1211 * 1212 * Intent log device can not be added to a rootpool because 1213 * during mountroot, zil is replayed, a seperated log device 1214 * can not be accessed during the mountroot time. 1215 * 1216 * l2cache and spare devices are ok to be added to a rootpool. 1217 */ 1218 if (spa->spa_bootfs != 0 && nl2cache == 0 && nspares == 0) { 1219 spa_close(spa, FTAG); 1220 return (EDOM); 1221 } 1222 1223 if (error == 0) { 1224 error = spa_vdev_add(spa, config); 1225 nvlist_free(config); 1226 } 1227 spa_close(spa, FTAG); 1228 return (error); 1229 } 1230 1231 static int 1232 zfs_ioc_vdev_remove(zfs_cmd_t *zc) 1233 { 1234 spa_t *spa; 1235 int error; 1236 1237 error = spa_open(zc->zc_name, &spa, FTAG); 1238 if (error != 0) 1239 return (error); 1240 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE); 1241 spa_close(spa, FTAG); 1242 return (error); 1243 } 1244 1245 static int 1246 zfs_ioc_vdev_set_state(zfs_cmd_t *zc) 1247 { 1248 spa_t *spa; 1249 int error; 1250 vdev_state_t newstate = VDEV_STATE_UNKNOWN; 1251 1252 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1253 return (error); 1254 switch (zc->zc_cookie) { 1255 case VDEV_STATE_ONLINE: 1256 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate); 1257 break; 1258 1259 case VDEV_STATE_OFFLINE: 1260 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj); 1261 break; 1262 1263 case VDEV_STATE_FAULTED: 1264 error = vdev_fault(spa, zc->zc_guid); 1265 break; 1266 1267 case VDEV_STATE_DEGRADED: 1268 error = vdev_degrade(spa, zc->zc_guid); 1269 break; 1270 1271 default: 1272 error = EINVAL; 1273 } 1274 zc->zc_cookie = newstate; 1275 spa_close(spa, FTAG); 1276 return (error); 1277 } 1278 1279 static int 1280 zfs_ioc_vdev_attach(zfs_cmd_t *zc) 1281 { 1282 spa_t *spa; 1283 int replacing = zc->zc_cookie; 1284 nvlist_t *config; 1285 int error; 1286 1287 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1288 return (error); 1289 1290 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 1291 &config)) == 0) { 1292 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing); 1293 nvlist_free(config); 1294 } 1295 1296 spa_close(spa, FTAG); 1297 return (error); 1298 } 1299 1300 static int 1301 zfs_ioc_vdev_detach(zfs_cmd_t *zc) 1302 { 1303 spa_t *spa; 1304 int error; 1305 1306 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1307 return (error); 1308 1309 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE); 1310 1311 spa_close(spa, FTAG); 1312 return (error); 1313 } 1314 1315 static int 1316 zfs_ioc_vdev_setpath(zfs_cmd_t *zc) 1317 { 1318 spa_t *spa; 1319 char *path = zc->zc_value; 1320 uint64_t guid = zc->zc_guid; 1321 int error; 1322 1323 error = spa_open(zc->zc_name, &spa, FTAG); 1324 if (error != 0) 1325 return (error); 1326 1327 error = spa_vdev_setpath(spa, guid, path); 1328 spa_close(spa, FTAG); 1329 return (error); 1330 } 1331 1332 /* 1333 * inputs: 1334 * zc_name name of filesystem 1335 * zc_nvlist_dst_size size of buffer for property nvlist 1336 * 1337 * outputs: 1338 * zc_objset_stats stats 1339 * zc_nvlist_dst property nvlist 1340 * zc_nvlist_dst_size size of property nvlist 1341 */ 1342 static int 1343 zfs_ioc_objset_stats(zfs_cmd_t *zc) 1344 { 1345 objset_t *os = NULL; 1346 int error; 1347 nvlist_t *nv; 1348 1349 if (error = dmu_objset_open(zc->zc_name, 1350 DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os)) 1351 return (error); 1352 1353 dmu_objset_fast_stat(os, &zc->zc_objset_stats); 1354 1355 if (zc->zc_nvlist_dst != 0 && 1356 (error = dsl_prop_get_all(os, &nv, FALSE)) == 0) { 1357 dmu_objset_stats(os, nv); 1358 /* 1359 * NB: zvol_get_stats() will read the objset contents, 1360 * which we aren't supposed to do with a 1361 * DS_MODE_USER hold, because it could be 1362 * inconsistent. So this is a bit of a workaround... 1363 */ 1364 if (!zc->zc_objset_stats.dds_inconsistent) { 1365 if (dmu_objset_type(os) == DMU_OST_ZVOL) 1366 VERIFY(zvol_get_stats(os, nv) == 0); 1367 } 1368 error = put_nvlist(zc, nv); 1369 nvlist_free(nv); 1370 } 1371 1372 dmu_objset_close(os); 1373 return (error); 1374 } 1375 1376 static int 1377 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop) 1378 { 1379 uint64_t value; 1380 int error; 1381 1382 /* 1383 * zfs_get_zplprop() will either find a value or give us 1384 * the default value (if there is one). 1385 */ 1386 if ((error = zfs_get_zplprop(os, prop, &value)) != 0) 1387 return (error); 1388 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0); 1389 return (0); 1390 } 1391 1392 /* 1393 * inputs: 1394 * zc_name name of filesystem 1395 * zc_nvlist_dst_size size of buffer for zpl property nvlist 1396 * 1397 * outputs: 1398 * zc_nvlist_dst zpl property nvlist 1399 * zc_nvlist_dst_size size of zpl property nvlist 1400 */ 1401 static int 1402 zfs_ioc_objset_zplprops(zfs_cmd_t *zc) 1403 { 1404 objset_t *os; 1405 int err; 1406 1407 if (err = dmu_objset_open(zc->zc_name, 1408 DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os)) 1409 return (err); 1410 1411 dmu_objset_fast_stat(os, &zc->zc_objset_stats); 1412 1413 /* 1414 * NB: nvl_add_zplprop() will read the objset contents, 1415 * which we aren't supposed to do with a DS_MODE_USER 1416 * hold, because it could be inconsistent. 1417 */ 1418 if (zc->zc_nvlist_dst != NULL && 1419 !zc->zc_objset_stats.dds_inconsistent && 1420 dmu_objset_type(os) == DMU_OST_ZFS) { 1421 nvlist_t *nv; 1422 1423 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1424 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 && 1425 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 && 1426 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 && 1427 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0) 1428 err = put_nvlist(zc, nv); 1429 nvlist_free(nv); 1430 } else { 1431 err = ENOENT; 1432 } 1433 dmu_objset_close(os); 1434 return (err); 1435 } 1436 1437 static boolean_t 1438 dataset_name_hidden(const char *name) 1439 { 1440 /* 1441 * Skip over datasets that are not visible in this zone, 1442 * internal datasets (which have a $ in their name), and 1443 * temporary datasets (which have a % in their name). 1444 */ 1445 if (strchr(name, '$') != NULL) 1446 return (B_TRUE); 1447 if (strchr(name, '%') != NULL) 1448 return (B_TRUE); 1449 if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL)) 1450 return (B_TRUE); 1451 return (B_FALSE); 1452 } 1453 1454 /* 1455 * inputs: 1456 * zc_name name of filesystem 1457 * zc_cookie zap cursor 1458 * zc_nvlist_dst_size size of buffer for property nvlist 1459 * 1460 * outputs: 1461 * zc_name name of next filesystem 1462 * zc_cookie zap cursor 1463 * zc_objset_stats stats 1464 * zc_nvlist_dst property nvlist 1465 * zc_nvlist_dst_size size of property nvlist 1466 */ 1467 static int 1468 zfs_ioc_dataset_list_next(zfs_cmd_t *zc) 1469 { 1470 objset_t *os; 1471 int error; 1472 char *p; 1473 1474 if (error = dmu_objset_open(zc->zc_name, 1475 DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os)) { 1476 if (error == ENOENT) 1477 error = ESRCH; 1478 return (error); 1479 } 1480 1481 p = strrchr(zc->zc_name, '/'); 1482 if (p == NULL || p[1] != '\0') 1483 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name)); 1484 p = zc->zc_name + strlen(zc->zc_name); 1485 1486 /* 1487 * Pre-fetch the datasets. dmu_objset_prefetch() always returns 0 1488 * but is not declared void because its called by dmu_objset_find(). 1489 */ 1490 if (zc->zc_cookie == 0) { 1491 uint64_t cookie = 0; 1492 int len = sizeof (zc->zc_name) - (p - zc->zc_name); 1493 1494 while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0) 1495 (void) dmu_objset_prefetch(p, NULL); 1496 } 1497 1498 do { 1499 error = dmu_dir_list_next(os, 1500 sizeof (zc->zc_name) - (p - zc->zc_name), p, 1501 NULL, &zc->zc_cookie); 1502 if (error == ENOENT) 1503 error = ESRCH; 1504 } while (error == 0 && dataset_name_hidden(zc->zc_name)); 1505 dmu_objset_close(os); 1506 1507 if (error == 0) 1508 error = zfs_ioc_objset_stats(zc); /* fill in the stats */ 1509 1510 return (error); 1511 } 1512 1513 /* 1514 * inputs: 1515 * zc_name name of filesystem 1516 * zc_cookie zap cursor 1517 * zc_nvlist_dst_size size of buffer for property nvlist 1518 * 1519 * outputs: 1520 * zc_name name of next snapshot 1521 * zc_objset_stats stats 1522 * zc_nvlist_dst property nvlist 1523 * zc_nvlist_dst_size size of property nvlist 1524 */ 1525 static int 1526 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc) 1527 { 1528 objset_t *os; 1529 int error; 1530 1531 error = dmu_objset_open(zc->zc_name, 1532 DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os); 1533 if (error) 1534 return (error == ENOENT ? ESRCH : error); 1535 1536 if (zc->zc_cookie == 0) { 1537 (void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch, 1538 NULL, DS_FIND_SNAPSHOTS); 1539 } 1540 /* 1541 * A dataset name of maximum length cannot have any snapshots, 1542 * so exit immediately. 1543 */ 1544 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) { 1545 dmu_objset_close(os); 1546 return (ESRCH); 1547 } 1548 1549 error = dmu_snapshot_list_next(os, 1550 sizeof (zc->zc_name) - strlen(zc->zc_name), 1551 zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie, NULL); 1552 dmu_objset_close(os); 1553 if (error == 0) 1554 error = zfs_ioc_objset_stats(zc); /* fill in the stats */ 1555 else if (error == ENOENT) 1556 error = ESRCH; 1557 1558 /* if we failed, undo the @ that we tacked on to zc_name */ 1559 if (error) 1560 *strchr(zc->zc_name, '@') = '\0'; 1561 return (error); 1562 } 1563 1564 int 1565 zfs_set_prop_nvlist(const char *name, nvlist_t *nvl) 1566 { 1567 nvpair_t *elem; 1568 int error = 0; 1569 uint64_t intval; 1570 char *strval; 1571 nvlist_t *genericnvl; 1572 boolean_t issnap = (strchr(name, '@') != NULL); 1573 1574 /* 1575 * First validate permission to set all of the properties 1576 */ 1577 elem = NULL; 1578 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 1579 const char *propname = nvpair_name(elem); 1580 zfs_prop_t prop = zfs_name_to_prop(propname); 1581 1582 if (prop == ZPROP_INVAL) { 1583 /* 1584 * If this is a user-defined property, it must be a 1585 * string, and there is no further validation to do. 1586 */ 1587 if (zfs_prop_user(propname) && 1588 nvpair_type(elem) == DATA_TYPE_STRING) { 1589 if (error = zfs_secpolicy_write_perms(name, 1590 ZFS_DELEG_PERM_USERPROP, CRED())) 1591 return (error); 1592 continue; 1593 } 1594 1595 if (!issnap && zfs_prop_userquota(propname) && 1596 nvpair_type(elem) == DATA_TYPE_UINT64_ARRAY) { 1597 const char *perm; 1598 const char *up = zfs_userquota_prop_prefixes 1599 [ZFS_PROP_USERQUOTA]; 1600 if (strncmp(propname, up, strlen(up)) == 0) 1601 perm = ZFS_DELEG_PERM_USERQUOTA; 1602 else 1603 perm = ZFS_DELEG_PERM_GROUPQUOTA; 1604 if (error = zfs_secpolicy_write_perms(name, 1605 perm, CRED())) 1606 return (error); 1607 continue; 1608 } 1609 1610 return (EINVAL); 1611 } 1612 1613 if (issnap) 1614 return (EINVAL); 1615 1616 if ((error = zfs_secpolicy_setprop(name, prop, CRED())) != 0) 1617 return (error); 1618 1619 /* 1620 * Check that this value is valid for this pool version 1621 */ 1622 switch (prop) { 1623 case ZFS_PROP_COMPRESSION: 1624 /* 1625 * If the user specified gzip compression, make sure 1626 * the SPA supports it. We ignore any errors here since 1627 * we'll catch them later. 1628 */ 1629 if (nvpair_type(elem) == DATA_TYPE_UINT64 && 1630 nvpair_value_uint64(elem, &intval) == 0) { 1631 if (intval >= ZIO_COMPRESS_GZIP_1 && 1632 intval <= ZIO_COMPRESS_GZIP_9 && 1633 zfs_earlier_version(name, 1634 SPA_VERSION_GZIP_COMPRESSION)) 1635 return (ENOTSUP); 1636 1637 /* 1638 * If this is a bootable dataset then 1639 * verify that the compression algorithm 1640 * is supported for booting. We must return 1641 * something other than ENOTSUP since it 1642 * implies a downrev pool version. 1643 */ 1644 if (zfs_is_bootfs(name) && 1645 !BOOTFS_COMPRESS_VALID(intval)) 1646 return (ERANGE); 1647 } 1648 break; 1649 1650 case ZFS_PROP_COPIES: 1651 if (zfs_earlier_version(name, SPA_VERSION_DITTO_BLOCKS)) 1652 return (ENOTSUP); 1653 break; 1654 1655 case ZFS_PROP_SHARESMB: 1656 if (zpl_earlier_version(name, ZPL_VERSION_FUID)) 1657 return (ENOTSUP); 1658 break; 1659 1660 case ZFS_PROP_ACLINHERIT: 1661 if (nvpair_type(elem) == DATA_TYPE_UINT64 && 1662 nvpair_value_uint64(elem, &intval) == 0) 1663 if (intval == ZFS_ACL_PASSTHROUGH_X && 1664 zfs_earlier_version(name, 1665 SPA_VERSION_PASSTHROUGH_X)) 1666 return (ENOTSUP); 1667 } 1668 } 1669 1670 VERIFY(nvlist_alloc(&genericnvl, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1671 elem = NULL; 1672 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 1673 const char *propname = nvpair_name(elem); 1674 zfs_prop_t prop = zfs_name_to_prop(propname); 1675 1676 if (prop == ZPROP_INVAL) { 1677 if (zfs_prop_userquota(propname)) { 1678 uint64_t *valary; 1679 unsigned int vallen; 1680 const char *domain; 1681 zfs_userquota_prop_t type; 1682 uint64_t rid; 1683 uint64_t quota; 1684 zfsvfs_t *zfsvfs; 1685 1686 VERIFY(nvpair_value_uint64_array(elem, 1687 &valary, &vallen) == 0); 1688 VERIFY(vallen == 3); 1689 type = valary[0]; 1690 rid = valary[1]; 1691 quota = valary[2]; 1692 domain = propname + 1693 strlen(zfs_userquota_prop_prefixes[type]); 1694 1695 error = zfsvfs_hold(name, B_FALSE, FTAG, 1696 &zfsvfs); 1697 if (error == 0) { 1698 error = zfs_set_userquota(zfsvfs, 1699 type, domain, rid, quota); 1700 zfsvfs_rele(zfsvfs, FTAG); 1701 } 1702 if (error == 0) 1703 continue; 1704 else 1705 goto out; 1706 } else if (zfs_prop_user(propname)) { 1707 VERIFY(nvpair_value_string(elem, &strval) == 0); 1708 error = dsl_prop_set(name, propname, 1, 1709 strlen(strval) + 1, strval); 1710 if (error == 0) 1711 continue; 1712 else 1713 goto out; 1714 } 1715 } 1716 1717 switch (prop) { 1718 case ZFS_PROP_QUOTA: 1719 if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 1720 (error = dsl_dir_set_quota(name, intval)) != 0) 1721 goto out; 1722 break; 1723 1724 case ZFS_PROP_REFQUOTA: 1725 if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 1726 (error = dsl_dataset_set_quota(name, intval)) != 0) 1727 goto out; 1728 break; 1729 1730 case ZFS_PROP_RESERVATION: 1731 if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 1732 (error = dsl_dir_set_reservation(name, 1733 intval)) != 0) 1734 goto out; 1735 break; 1736 1737 case ZFS_PROP_REFRESERVATION: 1738 if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 1739 (error = dsl_dataset_set_reservation(name, 1740 intval)) != 0) 1741 goto out; 1742 break; 1743 1744 case ZFS_PROP_VOLSIZE: 1745 if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 1746 (error = zvol_set_volsize(name, 1747 ddi_driver_major(zfs_dip), intval)) != 0) 1748 goto out; 1749 break; 1750 1751 case ZFS_PROP_VOLBLOCKSIZE: 1752 if ((error = nvpair_value_uint64(elem, &intval)) != 0 || 1753 (error = zvol_set_volblocksize(name, intval)) != 0) 1754 goto out; 1755 break; 1756 1757 case ZFS_PROP_VERSION: 1758 { 1759 zfsvfs_t *zfsvfs; 1760 1761 if ((error = nvpair_value_uint64(elem, &intval)) != 0) 1762 goto out; 1763 if ((error = zfsvfs_hold(name, B_FALSE, FTAG, 1764 &zfsvfs)) != 0) 1765 goto out; 1766 error = zfs_set_version(zfsvfs, intval); 1767 zfsvfs_rele(zfsvfs, FTAG); 1768 1769 if (error == 0 && intval >= ZPL_VERSION_USERSPACE) { 1770 zfs_cmd_t zc = { 0 }; 1771 (void) strcpy(zc.zc_name, name); 1772 (void) zfs_ioc_userspace_upgrade(&zc); 1773 } 1774 if (error) 1775 goto out; 1776 break; 1777 } 1778 1779 default: 1780 if (nvpair_type(elem) == DATA_TYPE_STRING) { 1781 if (zfs_prop_get_type(prop) != 1782 PROP_TYPE_STRING) { 1783 error = EINVAL; 1784 goto out; 1785 } 1786 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 1787 const char *unused; 1788 1789 VERIFY(nvpair_value_uint64(elem, &intval) == 0); 1790 1791 switch (zfs_prop_get_type(prop)) { 1792 case PROP_TYPE_NUMBER: 1793 break; 1794 case PROP_TYPE_STRING: 1795 error = EINVAL; 1796 goto out; 1797 case PROP_TYPE_INDEX: 1798 if (zfs_prop_index_to_string(prop, 1799 intval, &unused) != 0) { 1800 error = EINVAL; 1801 goto out; 1802 } 1803 break; 1804 default: 1805 cmn_err(CE_PANIC, 1806 "unknown property type"); 1807 break; 1808 } 1809 } else { 1810 error = EINVAL; 1811 goto out; 1812 } 1813 if ((error = nvlist_add_nvpair(genericnvl, elem)) != 0) 1814 goto out; 1815 } 1816 } 1817 1818 if (nvlist_next_nvpair(genericnvl, NULL) != NULL) { 1819 error = dsl_props_set(name, genericnvl); 1820 } 1821 out: 1822 nvlist_free(genericnvl); 1823 return (error); 1824 } 1825 1826 /* 1827 * Check that all the properties are valid user properties. 1828 */ 1829 static int 1830 zfs_check_userprops(char *fsname, nvlist_t *nvl) 1831 { 1832 nvpair_t *elem = NULL; 1833 int error = 0; 1834 1835 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 1836 const char *propname = nvpair_name(elem); 1837 char *valstr; 1838 1839 if (!zfs_prop_user(propname) || 1840 nvpair_type(elem) != DATA_TYPE_STRING) 1841 return (EINVAL); 1842 1843 if (error = zfs_secpolicy_write_perms(fsname, 1844 ZFS_DELEG_PERM_USERPROP, CRED())) 1845 return (error); 1846 1847 if (strlen(propname) >= ZAP_MAXNAMELEN) 1848 return (ENAMETOOLONG); 1849 1850 VERIFY(nvpair_value_string(elem, &valstr) == 0); 1851 if (strlen(valstr) >= ZAP_MAXVALUELEN) 1852 return (E2BIG); 1853 } 1854 return (0); 1855 } 1856 1857 /* 1858 * inputs: 1859 * zc_name name of filesystem 1860 * zc_value name of property to set 1861 * zc_nvlist_src{_size} nvlist of properties to apply 1862 * zc_cookie clear existing local props? 1863 * 1864 * outputs: none 1865 */ 1866 static int 1867 zfs_ioc_set_prop(zfs_cmd_t *zc) 1868 { 1869 nvlist_t *nvl; 1870 int error; 1871 1872 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 1873 &nvl)) != 0) 1874 return (error); 1875 1876 if (zc->zc_cookie) { 1877 nvlist_t *origprops; 1878 objset_t *os; 1879 1880 if (dmu_objset_open(zc->zc_name, DMU_OST_ANY, 1881 DS_MODE_USER | DS_MODE_READONLY, &os) == 0) { 1882 if (dsl_prop_get_all(os, &origprops, TRUE) == 0) { 1883 clear_props(zc->zc_name, origprops, nvl); 1884 nvlist_free(origprops); 1885 } 1886 dmu_objset_close(os); 1887 } 1888 1889 } 1890 1891 error = zfs_set_prop_nvlist(zc->zc_name, nvl); 1892 1893 nvlist_free(nvl); 1894 return (error); 1895 } 1896 1897 /* 1898 * inputs: 1899 * zc_name name of filesystem 1900 * zc_value name of property to inherit 1901 * 1902 * outputs: none 1903 */ 1904 static int 1905 zfs_ioc_inherit_prop(zfs_cmd_t *zc) 1906 { 1907 /* the property name has been validated by zfs_secpolicy_inherit() */ 1908 return (dsl_prop_set(zc->zc_name, zc->zc_value, 0, 0, NULL)); 1909 } 1910 1911 static int 1912 zfs_ioc_pool_set_props(zfs_cmd_t *zc) 1913 { 1914 nvlist_t *props; 1915 spa_t *spa; 1916 int error; 1917 nvpair_t *elem; 1918 1919 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 1920 &props))) 1921 return (error); 1922 1923 /* 1924 * If the only property is the configfile, then just do a spa_lookup() 1925 * to handle the faulted case. 1926 */ 1927 elem = nvlist_next_nvpair(props, NULL); 1928 if (elem != NULL && strcmp(nvpair_name(elem), 1929 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 && 1930 nvlist_next_nvpair(props, elem) == NULL) { 1931 mutex_enter(&spa_namespace_lock); 1932 if ((spa = spa_lookup(zc->zc_name)) != NULL) { 1933 spa_configfile_set(spa, props, B_FALSE); 1934 spa_config_sync(spa, B_FALSE, B_TRUE); 1935 } 1936 mutex_exit(&spa_namespace_lock); 1937 if (spa != NULL) 1938 return (0); 1939 } 1940 1941 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) { 1942 nvlist_free(props); 1943 return (error); 1944 } 1945 1946 error = spa_prop_set(spa, props); 1947 1948 nvlist_free(props); 1949 spa_close(spa, FTAG); 1950 1951 return (error); 1952 } 1953 1954 static int 1955 zfs_ioc_pool_get_props(zfs_cmd_t *zc) 1956 { 1957 spa_t *spa; 1958 int error; 1959 nvlist_t *nvp = NULL; 1960 1961 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) { 1962 /* 1963 * If the pool is faulted, there may be properties we can still 1964 * get (such as altroot and cachefile), so attempt to get them 1965 * anyway. 1966 */ 1967 mutex_enter(&spa_namespace_lock); 1968 if ((spa = spa_lookup(zc->zc_name)) != NULL) 1969 error = spa_prop_get(spa, &nvp); 1970 mutex_exit(&spa_namespace_lock); 1971 } else { 1972 error = spa_prop_get(spa, &nvp); 1973 spa_close(spa, FTAG); 1974 } 1975 1976 if (error == 0 && zc->zc_nvlist_dst != NULL) 1977 error = put_nvlist(zc, nvp); 1978 else 1979 error = EFAULT; 1980 1981 nvlist_free(nvp); 1982 return (error); 1983 } 1984 1985 static int 1986 zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc) 1987 { 1988 nvlist_t *nvp; 1989 int error; 1990 uint32_t uid; 1991 uint32_t gid; 1992 uint32_t *groups; 1993 uint_t group_cnt; 1994 cred_t *usercred; 1995 1996 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 1997 &nvp)) != 0) { 1998 return (error); 1999 } 2000 2001 if ((error = nvlist_lookup_uint32(nvp, 2002 ZFS_DELEG_PERM_UID, &uid)) != 0) { 2003 nvlist_free(nvp); 2004 return (EPERM); 2005 } 2006 2007 if ((error = nvlist_lookup_uint32(nvp, 2008 ZFS_DELEG_PERM_GID, &gid)) != 0) { 2009 nvlist_free(nvp); 2010 return (EPERM); 2011 } 2012 2013 if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS, 2014 &groups, &group_cnt)) != 0) { 2015 nvlist_free(nvp); 2016 return (EPERM); 2017 } 2018 usercred = cralloc(); 2019 if ((crsetugid(usercred, uid, gid) != 0) || 2020 (crsetgroups(usercred, group_cnt, (gid_t *)groups) != 0)) { 2021 nvlist_free(nvp); 2022 crfree(usercred); 2023 return (EPERM); 2024 } 2025 nvlist_free(nvp); 2026 error = dsl_deleg_access(zc->zc_name, 2027 zfs_prop_to_name(ZFS_PROP_SHAREISCSI), usercred); 2028 crfree(usercred); 2029 return (error); 2030 } 2031 2032 /* 2033 * inputs: 2034 * zc_name name of filesystem 2035 * zc_nvlist_src{_size} nvlist of delegated permissions 2036 * zc_perm_action allow/unallow flag 2037 * 2038 * outputs: none 2039 */ 2040 static int 2041 zfs_ioc_set_fsacl(zfs_cmd_t *zc) 2042 { 2043 int error; 2044 nvlist_t *fsaclnv = NULL; 2045 2046 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 2047 &fsaclnv)) != 0) 2048 return (error); 2049 2050 /* 2051 * Verify nvlist is constructed correctly 2052 */ 2053 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) { 2054 nvlist_free(fsaclnv); 2055 return (EINVAL); 2056 } 2057 2058 /* 2059 * If we don't have PRIV_SYS_MOUNT, then validate 2060 * that user is allowed to hand out each permission in 2061 * the nvlist(s) 2062 */ 2063 2064 error = secpolicy_zfs(CRED()); 2065 if (error) { 2066 if (zc->zc_perm_action == B_FALSE) { 2067 error = dsl_deleg_can_allow(zc->zc_name, 2068 fsaclnv, CRED()); 2069 } else { 2070 error = dsl_deleg_can_unallow(zc->zc_name, 2071 fsaclnv, CRED()); 2072 } 2073 } 2074 2075 if (error == 0) 2076 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action); 2077 2078 nvlist_free(fsaclnv); 2079 return (error); 2080 } 2081 2082 /* 2083 * inputs: 2084 * zc_name name of filesystem 2085 * 2086 * outputs: 2087 * zc_nvlist_src{_size} nvlist of delegated permissions 2088 */ 2089 static int 2090 zfs_ioc_get_fsacl(zfs_cmd_t *zc) 2091 { 2092 nvlist_t *nvp; 2093 int error; 2094 2095 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) { 2096 error = put_nvlist(zc, nvp); 2097 nvlist_free(nvp); 2098 } 2099 2100 return (error); 2101 } 2102 2103 /* 2104 * inputs: 2105 * zc_name name of volume 2106 * 2107 * outputs: none 2108 */ 2109 static int 2110 zfs_ioc_create_minor(zfs_cmd_t *zc) 2111 { 2112 return (zvol_create_minor(zc->zc_name, ddi_driver_major(zfs_dip))); 2113 } 2114 2115 /* 2116 * inputs: 2117 * zc_name name of volume 2118 * 2119 * outputs: none 2120 */ 2121 static int 2122 zfs_ioc_remove_minor(zfs_cmd_t *zc) 2123 { 2124 return (zvol_remove_minor(zc->zc_name)); 2125 } 2126 2127 /* 2128 * Search the vfs list for a specified resource. Returns a pointer to it 2129 * or NULL if no suitable entry is found. The caller of this routine 2130 * is responsible for releasing the returned vfs pointer. 2131 */ 2132 static vfs_t * 2133 zfs_get_vfs(const char *resource) 2134 { 2135 struct vfs *vfsp; 2136 struct vfs *vfs_found = NULL; 2137 2138 vfs_list_read_lock(); 2139 vfsp = rootvfs; 2140 do { 2141 if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) { 2142 VFS_HOLD(vfsp); 2143 vfs_found = vfsp; 2144 break; 2145 } 2146 vfsp = vfsp->vfs_next; 2147 } while (vfsp != rootvfs); 2148 vfs_list_unlock(); 2149 return (vfs_found); 2150 } 2151 2152 /* ARGSUSED */ 2153 static void 2154 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 2155 { 2156 zfs_creat_t *zct = arg; 2157 2158 zfs_create_fs(os, cr, zct->zct_zplprops, tx); 2159 } 2160 2161 #define ZFS_PROP_UNDEFINED ((uint64_t)-1) 2162 2163 /* 2164 * inputs: 2165 * createprops list of properties requested by creator 2166 * default_zplver zpl version to use if unspecified in createprops 2167 * fuids_ok fuids allowed in this version of the spa? 2168 * os parent objset pointer (NULL if root fs) 2169 * 2170 * outputs: 2171 * zplprops values for the zplprops we attach to the master node object 2172 * is_ci true if requested file system will be purely case-insensitive 2173 * 2174 * Determine the settings for utf8only, normalization and 2175 * casesensitivity. Specific values may have been requested by the 2176 * creator and/or we can inherit values from the parent dataset. If 2177 * the file system is of too early a vintage, a creator can not 2178 * request settings for these properties, even if the requested 2179 * setting is the default value. We don't actually want to create dsl 2180 * properties for these, so remove them from the source nvlist after 2181 * processing. 2182 */ 2183 static int 2184 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver, 2185 boolean_t fuids_ok, nvlist_t *createprops, nvlist_t *zplprops, 2186 boolean_t *is_ci) 2187 { 2188 uint64_t sense = ZFS_PROP_UNDEFINED; 2189 uint64_t norm = ZFS_PROP_UNDEFINED; 2190 uint64_t u8 = ZFS_PROP_UNDEFINED; 2191 2192 ASSERT(zplprops != NULL); 2193 2194 /* 2195 * Pull out creator prop choices, if any. 2196 */ 2197 if (createprops) { 2198 (void) nvlist_lookup_uint64(createprops, 2199 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver); 2200 (void) nvlist_lookup_uint64(createprops, 2201 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm); 2202 (void) nvlist_remove_all(createprops, 2203 zfs_prop_to_name(ZFS_PROP_NORMALIZE)); 2204 (void) nvlist_lookup_uint64(createprops, 2205 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8); 2206 (void) nvlist_remove_all(createprops, 2207 zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 2208 (void) nvlist_lookup_uint64(createprops, 2209 zfs_prop_to_name(ZFS_PROP_CASE), &sense); 2210 (void) nvlist_remove_all(createprops, 2211 zfs_prop_to_name(ZFS_PROP_CASE)); 2212 } 2213 2214 /* 2215 * If the zpl version requested is whacky or the file system 2216 * or pool is version is too "young" to support normalization 2217 * and the creator tried to set a value for one of the props, 2218 * error out. 2219 */ 2220 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) || 2221 (zplver >= ZPL_VERSION_FUID && !fuids_ok) || 2222 (zplver < ZPL_VERSION_NORMALIZATION && 2223 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED || 2224 sense != ZFS_PROP_UNDEFINED))) 2225 return (ENOTSUP); 2226 2227 /* 2228 * Put the version in the zplprops 2229 */ 2230 VERIFY(nvlist_add_uint64(zplprops, 2231 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0); 2232 2233 if (norm == ZFS_PROP_UNDEFINED) 2234 VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0); 2235 VERIFY(nvlist_add_uint64(zplprops, 2236 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0); 2237 2238 /* 2239 * If we're normalizing, names must always be valid UTF-8 strings. 2240 */ 2241 if (norm) 2242 u8 = 1; 2243 if (u8 == ZFS_PROP_UNDEFINED) 2244 VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0); 2245 VERIFY(nvlist_add_uint64(zplprops, 2246 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0); 2247 2248 if (sense == ZFS_PROP_UNDEFINED) 2249 VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0); 2250 VERIFY(nvlist_add_uint64(zplprops, 2251 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0); 2252 2253 if (is_ci) 2254 *is_ci = (sense == ZFS_CASE_INSENSITIVE); 2255 2256 return (0); 2257 } 2258 2259 static int 2260 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops, 2261 nvlist_t *zplprops, boolean_t *is_ci) 2262 { 2263 boolean_t fuids_ok = B_TRUE; 2264 uint64_t zplver = ZPL_VERSION; 2265 objset_t *os = NULL; 2266 char parentname[MAXNAMELEN]; 2267 char *cp; 2268 int error; 2269 2270 (void) strlcpy(parentname, dataset, sizeof (parentname)); 2271 cp = strrchr(parentname, '/'); 2272 ASSERT(cp != NULL); 2273 cp[0] = '\0'; 2274 2275 if (zfs_earlier_version(dataset, SPA_VERSION_USERSPACE)) 2276 zplver = ZPL_VERSION_USERSPACE - 1; 2277 if (zfs_earlier_version(dataset, SPA_VERSION_FUID)) { 2278 zplver = ZPL_VERSION_FUID - 1; 2279 fuids_ok = B_FALSE; 2280 } 2281 2282 /* 2283 * Open parent object set so we can inherit zplprop values. 2284 */ 2285 if ((error = dmu_objset_open(parentname, DMU_OST_ANY, 2286 DS_MODE_USER | DS_MODE_READONLY, &os)) != 0) 2287 return (error); 2288 2289 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, createprops, 2290 zplprops, is_ci); 2291 dmu_objset_close(os); 2292 return (error); 2293 } 2294 2295 static int 2296 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops, 2297 nvlist_t *zplprops, boolean_t *is_ci) 2298 { 2299 boolean_t fuids_ok = B_TRUE; 2300 uint64_t zplver = ZPL_VERSION; 2301 int error; 2302 2303 if (spa_vers < SPA_VERSION_FUID) { 2304 zplver = ZPL_VERSION_FUID - 1; 2305 fuids_ok = B_FALSE; 2306 } 2307 2308 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, createprops, 2309 zplprops, is_ci); 2310 return (error); 2311 } 2312 2313 /* 2314 * inputs: 2315 * zc_objset_type type of objset to create (fs vs zvol) 2316 * zc_name name of new objset 2317 * zc_value name of snapshot to clone from (may be empty) 2318 * zc_nvlist_src{_size} nvlist of properties to apply 2319 * 2320 * outputs: none 2321 */ 2322 static int 2323 zfs_ioc_create(zfs_cmd_t *zc) 2324 { 2325 objset_t *clone; 2326 int error = 0; 2327 zfs_creat_t zct; 2328 nvlist_t *nvprops = NULL; 2329 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx); 2330 dmu_objset_type_t type = zc->zc_objset_type; 2331 2332 switch (type) { 2333 2334 case DMU_OST_ZFS: 2335 cbfunc = zfs_create_cb; 2336 break; 2337 2338 case DMU_OST_ZVOL: 2339 cbfunc = zvol_create_cb; 2340 break; 2341 2342 default: 2343 cbfunc = NULL; 2344 break; 2345 } 2346 if (strchr(zc->zc_name, '@') || 2347 strchr(zc->zc_name, '%')) 2348 return (EINVAL); 2349 2350 if (zc->zc_nvlist_src != NULL && 2351 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 2352 &nvprops)) != 0) 2353 return (error); 2354 2355 zct.zct_zplprops = NULL; 2356 zct.zct_props = nvprops; 2357 2358 if (zc->zc_value[0] != '\0') { 2359 /* 2360 * We're creating a clone of an existing snapshot. 2361 */ 2362 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0'; 2363 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) { 2364 nvlist_free(nvprops); 2365 return (EINVAL); 2366 } 2367 2368 error = dmu_objset_open(zc->zc_value, type, 2369 DS_MODE_USER | DS_MODE_READONLY, &clone); 2370 if (error) { 2371 nvlist_free(nvprops); 2372 return (error); 2373 } 2374 2375 error = dmu_objset_create(zc->zc_name, type, clone, 0, 2376 NULL, NULL); 2377 if (error) { 2378 dmu_objset_close(clone); 2379 nvlist_free(nvprops); 2380 return (error); 2381 } 2382 dmu_objset_close(clone); 2383 } else { 2384 boolean_t is_insensitive = B_FALSE; 2385 2386 if (cbfunc == NULL) { 2387 nvlist_free(nvprops); 2388 return (EINVAL); 2389 } 2390 2391 if (type == DMU_OST_ZVOL) { 2392 uint64_t volsize, volblocksize; 2393 2394 if (nvprops == NULL || 2395 nvlist_lookup_uint64(nvprops, 2396 zfs_prop_to_name(ZFS_PROP_VOLSIZE), 2397 &volsize) != 0) { 2398 nvlist_free(nvprops); 2399 return (EINVAL); 2400 } 2401 2402 if ((error = nvlist_lookup_uint64(nvprops, 2403 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 2404 &volblocksize)) != 0 && error != ENOENT) { 2405 nvlist_free(nvprops); 2406 return (EINVAL); 2407 } 2408 2409 if (error != 0) 2410 volblocksize = zfs_prop_default_numeric( 2411 ZFS_PROP_VOLBLOCKSIZE); 2412 2413 if ((error = zvol_check_volblocksize( 2414 volblocksize)) != 0 || 2415 (error = zvol_check_volsize(volsize, 2416 volblocksize)) != 0) { 2417 nvlist_free(nvprops); 2418 return (error); 2419 } 2420 } else if (type == DMU_OST_ZFS) { 2421 int error; 2422 2423 /* 2424 * We have to have normalization and 2425 * case-folding flags correct when we do the 2426 * file system creation, so go figure them out 2427 * now. 2428 */ 2429 VERIFY(nvlist_alloc(&zct.zct_zplprops, 2430 NV_UNIQUE_NAME, KM_SLEEP) == 0); 2431 error = zfs_fill_zplprops(zc->zc_name, nvprops, 2432 zct.zct_zplprops, &is_insensitive); 2433 if (error != 0) { 2434 nvlist_free(nvprops); 2435 nvlist_free(zct.zct_zplprops); 2436 return (error); 2437 } 2438 } 2439 error = dmu_objset_create(zc->zc_name, type, NULL, 2440 is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct); 2441 nvlist_free(zct.zct_zplprops); 2442 } 2443 2444 /* 2445 * It would be nice to do this atomically. 2446 */ 2447 if (error == 0) { 2448 if ((error = zfs_set_prop_nvlist(zc->zc_name, nvprops)) != 0) 2449 (void) dmu_objset_destroy(zc->zc_name); 2450 } 2451 nvlist_free(nvprops); 2452 return (error); 2453 } 2454 2455 /* 2456 * inputs: 2457 * zc_name name of filesystem 2458 * zc_value short name of snapshot 2459 * zc_cookie recursive flag 2460 * zc_nvlist_src[_size] property list 2461 * 2462 * outputs: none 2463 */ 2464 static int 2465 zfs_ioc_snapshot(zfs_cmd_t *zc) 2466 { 2467 nvlist_t *nvprops = NULL; 2468 int error; 2469 boolean_t recursive = zc->zc_cookie; 2470 2471 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 2472 return (EINVAL); 2473 2474 if (zc->zc_nvlist_src != NULL && 2475 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 2476 &nvprops)) != 0) 2477 return (error); 2478 2479 error = zfs_check_userprops(zc->zc_name, nvprops); 2480 if (error) 2481 goto out; 2482 2483 if (nvprops != NULL && nvlist_next_nvpair(nvprops, NULL) != NULL && 2484 zfs_earlier_version(zc->zc_name, SPA_VERSION_SNAP_PROPS)) { 2485 error = ENOTSUP; 2486 goto out; 2487 } 2488 2489 error = dmu_objset_snapshot(zc->zc_name, zc->zc_value, 2490 nvprops, recursive); 2491 2492 out: 2493 nvlist_free(nvprops); 2494 return (error); 2495 } 2496 2497 int 2498 zfs_unmount_snap(char *name, void *arg) 2499 { 2500 vfs_t *vfsp = NULL; 2501 2502 if (arg) { 2503 char *snapname = arg; 2504 int len = strlen(name) + strlen(snapname) + 2; 2505 char *buf = kmem_alloc(len, KM_SLEEP); 2506 2507 (void) strcpy(buf, name); 2508 (void) strcat(buf, "@"); 2509 (void) strcat(buf, snapname); 2510 vfsp = zfs_get_vfs(buf); 2511 kmem_free(buf, len); 2512 } else if (strchr(name, '@')) { 2513 vfsp = zfs_get_vfs(name); 2514 } 2515 2516 if (vfsp) { 2517 /* 2518 * Always force the unmount for snapshots. 2519 */ 2520 int flag = MS_FORCE; 2521 int err; 2522 2523 if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) { 2524 VFS_RELE(vfsp); 2525 return (err); 2526 } 2527 VFS_RELE(vfsp); 2528 if ((err = dounmount(vfsp, flag, kcred)) != 0) 2529 return (err); 2530 } 2531 return (0); 2532 } 2533 2534 /* 2535 * inputs: 2536 * zc_name name of filesystem 2537 * zc_value short name of snapshot 2538 * 2539 * outputs: none 2540 */ 2541 static int 2542 zfs_ioc_destroy_snaps(zfs_cmd_t *zc) 2543 { 2544 int err; 2545 2546 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 2547 return (EINVAL); 2548 err = dmu_objset_find(zc->zc_name, 2549 zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN); 2550 if (err) 2551 return (err); 2552 return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value)); 2553 } 2554 2555 /* 2556 * inputs: 2557 * zc_name name of dataset to destroy 2558 * zc_objset_type type of objset 2559 * 2560 * outputs: none 2561 */ 2562 static int 2563 zfs_ioc_destroy(zfs_cmd_t *zc) 2564 { 2565 if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) { 2566 int err = zfs_unmount_snap(zc->zc_name, NULL); 2567 if (err) 2568 return (err); 2569 } 2570 2571 return (dmu_objset_destroy(zc->zc_name)); 2572 } 2573 2574 /* 2575 * inputs: 2576 * zc_name name of dataset to rollback (to most recent snapshot) 2577 * 2578 * outputs: none 2579 */ 2580 static int 2581 zfs_ioc_rollback(zfs_cmd_t *zc) 2582 { 2583 objset_t *os; 2584 int error; 2585 zfsvfs_t *zfsvfs = NULL; 2586 2587 /* 2588 * Get the zfsvfs for the receiving objset. There 2589 * won't be one if we're operating on a zvol, if the 2590 * objset doesn't exist yet, or is not mounted. 2591 */ 2592 error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, DS_MODE_USER, &os); 2593 if (error) 2594 return (error); 2595 2596 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) { 2597 int mode; 2598 2599 error = zfs_suspend_fs(zfsvfs, NULL, &mode); 2600 if (error == 0) { 2601 int resume_err; 2602 2603 error = dmu_objset_rollback(os); 2604 resume_err = zfs_resume_fs(zfsvfs, zc->zc_name, mode); 2605 error = error ? error : resume_err; 2606 } else { 2607 dmu_objset_close(os); 2608 } 2609 VFS_RELE(zfsvfs->z_vfs); 2610 } else { 2611 error = dmu_objset_rollback(os); 2612 } 2613 /* Note, the dmu_objset_rollback() releases the objset for us. */ 2614 2615 return (error); 2616 } 2617 2618 /* 2619 * inputs: 2620 * zc_name old name of dataset 2621 * zc_value new name of dataset 2622 * zc_cookie recursive flag (only valid for snapshots) 2623 * 2624 * outputs: none 2625 */ 2626 static int 2627 zfs_ioc_rename(zfs_cmd_t *zc) 2628 { 2629 boolean_t recursive = zc->zc_cookie & 1; 2630 2631 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0'; 2632 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 || 2633 strchr(zc->zc_value, '%')) 2634 return (EINVAL); 2635 2636 /* 2637 * Unmount snapshot unless we're doing a recursive rename, 2638 * in which case the dataset code figures out which snapshots 2639 * to unmount. 2640 */ 2641 if (!recursive && strchr(zc->zc_name, '@') != NULL && 2642 zc->zc_objset_type == DMU_OST_ZFS) { 2643 int err = zfs_unmount_snap(zc->zc_name, NULL); 2644 if (err) 2645 return (err); 2646 } 2647 return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive)); 2648 } 2649 2650 static void 2651 clear_props(char *dataset, nvlist_t *props, nvlist_t *newprops) 2652 { 2653 zfs_cmd_t *zc; 2654 nvpair_t *prop; 2655 2656 if (props == NULL) 2657 return; 2658 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP); 2659 (void) strcpy(zc->zc_name, dataset); 2660 for (prop = nvlist_next_nvpair(props, NULL); prop; 2661 prop = nvlist_next_nvpair(props, prop)) { 2662 if (newprops != NULL && 2663 nvlist_exists(newprops, nvpair_name(prop))) 2664 continue; 2665 (void) strcpy(zc->zc_value, nvpair_name(prop)); 2666 if (zfs_secpolicy_inherit(zc, CRED()) == 0) 2667 (void) zfs_ioc_inherit_prop(zc); 2668 } 2669 kmem_free(zc, sizeof (zfs_cmd_t)); 2670 } 2671 2672 /* 2673 * inputs: 2674 * zc_name name of containing filesystem 2675 * zc_nvlist_src{_size} nvlist of properties to apply 2676 * zc_value name of snapshot to create 2677 * zc_string name of clone origin (if DRR_FLAG_CLONE) 2678 * zc_cookie file descriptor to recv from 2679 * zc_begin_record the BEGIN record of the stream (not byteswapped) 2680 * zc_guid force flag 2681 * 2682 * outputs: 2683 * zc_cookie number of bytes read 2684 */ 2685 static int 2686 zfs_ioc_recv(zfs_cmd_t *zc) 2687 { 2688 file_t *fp; 2689 objset_t *os; 2690 dmu_recv_cookie_t drc; 2691 zfsvfs_t *zfsvfs = NULL; 2692 boolean_t force = (boolean_t)zc->zc_guid; 2693 int error, fd; 2694 offset_t off; 2695 nvlist_t *props = NULL; 2696 nvlist_t *origprops = NULL; 2697 objset_t *origin = NULL; 2698 char *tosnap; 2699 char tofs[ZFS_MAXNAMELEN]; 2700 2701 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 || 2702 strchr(zc->zc_value, '@') == NULL || 2703 strchr(zc->zc_value, '%')) 2704 return (EINVAL); 2705 2706 (void) strcpy(tofs, zc->zc_value); 2707 tosnap = strchr(tofs, '@'); 2708 *tosnap = '\0'; 2709 tosnap++; 2710 2711 if (zc->zc_nvlist_src != NULL && 2712 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 2713 &props)) != 0) 2714 return (error); 2715 2716 fd = zc->zc_cookie; 2717 fp = getf(fd); 2718 if (fp == NULL) { 2719 nvlist_free(props); 2720 return (EBADF); 2721 } 2722 2723 if (getzfsvfs(tofs, &zfsvfs) == 0) { 2724 if (!mutex_tryenter(&zfsvfs->z_online_recv_lock)) { 2725 VFS_RELE(zfsvfs->z_vfs); 2726 zfsvfs = NULL; 2727 error = EBUSY; 2728 goto out; 2729 } 2730 /* 2731 * If new properties are supplied, they are to completely 2732 * replace the existing ones, so stash away the existing ones. 2733 */ 2734 if (props) 2735 (void) dsl_prop_get_all(zfsvfs->z_os, &origprops, TRUE); 2736 } else if (props && dmu_objset_open(tofs, DMU_OST_ANY, 2737 DS_MODE_USER | DS_MODE_READONLY, &os) == 0) { 2738 /* 2739 * Get the props even if there was no zfsvfs (zvol or 2740 * unmounted zpl). 2741 */ 2742 (void) dsl_prop_get_all(os, &origprops, TRUE); 2743 2744 dmu_objset_close(os); 2745 } 2746 2747 if (zc->zc_string[0]) { 2748 error = dmu_objset_open(zc->zc_string, DMU_OST_ANY, 2749 DS_MODE_USER | DS_MODE_READONLY, &origin); 2750 if (error) 2751 goto out; 2752 } 2753 2754 error = dmu_recv_begin(tofs, tosnap, &zc->zc_begin_record, 2755 force, origin, zfsvfs != NULL, &drc); 2756 if (origin) 2757 dmu_objset_close(origin); 2758 if (error) 2759 goto out; 2760 2761 /* 2762 * Reset properties. We do this before we receive the stream 2763 * so that the properties are applied to the new data. 2764 */ 2765 if (props) { 2766 clear_props(tofs, origprops, props); 2767 /* 2768 * XXX - Note, this is all-or-nothing; should be best-effort. 2769 */ 2770 (void) zfs_set_prop_nvlist(tofs, props); 2771 } 2772 2773 off = fp->f_offset; 2774 error = dmu_recv_stream(&drc, fp->f_vnode, &off); 2775 2776 if (error == 0 && zfsvfs) { 2777 char *osname; 2778 int mode; 2779 2780 /* online recv */ 2781 osname = kmem_alloc(MAXNAMELEN, KM_SLEEP); 2782 error = zfs_suspend_fs(zfsvfs, osname, &mode); 2783 if (error == 0) { 2784 int resume_err; 2785 2786 error = dmu_recv_end(&drc); 2787 resume_err = zfs_resume_fs(zfsvfs, osname, mode); 2788 error = error ? error : resume_err; 2789 } else { 2790 dmu_recv_abort_cleanup(&drc); 2791 } 2792 kmem_free(osname, MAXNAMELEN); 2793 } else if (error == 0) { 2794 error = dmu_recv_end(&drc); 2795 } 2796 2797 zc->zc_cookie = off - fp->f_offset; 2798 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0) 2799 fp->f_offset = off; 2800 2801 /* 2802 * On error, restore the original props. 2803 */ 2804 if (error && props) { 2805 clear_props(tofs, props, NULL); 2806 (void) zfs_set_prop_nvlist(tofs, origprops); 2807 } 2808 out: 2809 if (zfsvfs) { 2810 mutex_exit(&zfsvfs->z_online_recv_lock); 2811 VFS_RELE(zfsvfs->z_vfs); 2812 } 2813 nvlist_free(props); 2814 nvlist_free(origprops); 2815 releasef(fd); 2816 return (error); 2817 } 2818 2819 /* 2820 * inputs: 2821 * zc_name name of snapshot to send 2822 * zc_value short name of incremental fromsnap (may be empty) 2823 * zc_cookie file descriptor to send stream to 2824 * zc_obj fromorigin flag (mutually exclusive with zc_value) 2825 * 2826 * outputs: none 2827 */ 2828 static int 2829 zfs_ioc_send(zfs_cmd_t *zc) 2830 { 2831 objset_t *fromsnap = NULL; 2832 objset_t *tosnap; 2833 file_t *fp; 2834 int error; 2835 offset_t off; 2836 2837 error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, 2838 DS_MODE_USER | DS_MODE_READONLY, &tosnap); 2839 if (error) 2840 return (error); 2841 2842 if (zc->zc_value[0] != '\0') { 2843 char *buf; 2844 char *cp; 2845 2846 buf = kmem_alloc(MAXPATHLEN, KM_SLEEP); 2847 (void) strncpy(buf, zc->zc_name, MAXPATHLEN); 2848 cp = strchr(buf, '@'); 2849 if (cp) 2850 *(cp+1) = 0; 2851 (void) strncat(buf, zc->zc_value, MAXPATHLEN); 2852 error = dmu_objset_open(buf, DMU_OST_ANY, 2853 DS_MODE_USER | DS_MODE_READONLY, &fromsnap); 2854 kmem_free(buf, MAXPATHLEN); 2855 if (error) { 2856 dmu_objset_close(tosnap); 2857 return (error); 2858 } 2859 } 2860 2861 fp = getf(zc->zc_cookie); 2862 if (fp == NULL) { 2863 dmu_objset_close(tosnap); 2864 if (fromsnap) 2865 dmu_objset_close(fromsnap); 2866 return (EBADF); 2867 } 2868 2869 off = fp->f_offset; 2870 error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off); 2871 2872 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0) 2873 fp->f_offset = off; 2874 releasef(zc->zc_cookie); 2875 if (fromsnap) 2876 dmu_objset_close(fromsnap); 2877 dmu_objset_close(tosnap); 2878 return (error); 2879 } 2880 2881 static int 2882 zfs_ioc_inject_fault(zfs_cmd_t *zc) 2883 { 2884 int id, error; 2885 2886 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id, 2887 &zc->zc_inject_record); 2888 2889 if (error == 0) 2890 zc->zc_guid = (uint64_t)id; 2891 2892 return (error); 2893 } 2894 2895 static int 2896 zfs_ioc_clear_fault(zfs_cmd_t *zc) 2897 { 2898 return (zio_clear_fault((int)zc->zc_guid)); 2899 } 2900 2901 static int 2902 zfs_ioc_inject_list_next(zfs_cmd_t *zc) 2903 { 2904 int id = (int)zc->zc_guid; 2905 int error; 2906 2907 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name), 2908 &zc->zc_inject_record); 2909 2910 zc->zc_guid = id; 2911 2912 return (error); 2913 } 2914 2915 static int 2916 zfs_ioc_error_log(zfs_cmd_t *zc) 2917 { 2918 spa_t *spa; 2919 int error; 2920 size_t count = (size_t)zc->zc_nvlist_dst_size; 2921 2922 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 2923 return (error); 2924 2925 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst, 2926 &count); 2927 if (error == 0) 2928 zc->zc_nvlist_dst_size = count; 2929 else 2930 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa); 2931 2932 spa_close(spa, FTAG); 2933 2934 return (error); 2935 } 2936 2937 static int 2938 zfs_ioc_clear(zfs_cmd_t *zc) 2939 { 2940 spa_t *spa; 2941 vdev_t *vd; 2942 int error; 2943 2944 /* 2945 * On zpool clear we also fix up missing slogs 2946 */ 2947 mutex_enter(&spa_namespace_lock); 2948 spa = spa_lookup(zc->zc_name); 2949 if (spa == NULL) { 2950 mutex_exit(&spa_namespace_lock); 2951 return (EIO); 2952 } 2953 if (spa->spa_log_state == SPA_LOG_MISSING) { 2954 /* we need to let spa_open/spa_load clear the chains */ 2955 spa->spa_log_state = SPA_LOG_CLEAR; 2956 } 2957 mutex_exit(&spa_namespace_lock); 2958 2959 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 2960 return (error); 2961 2962 spa_vdev_state_enter(spa); 2963 2964 if (zc->zc_guid == 0) { 2965 vd = NULL; 2966 } else { 2967 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE); 2968 if (vd == NULL) { 2969 (void) spa_vdev_state_exit(spa, NULL, ENODEV); 2970 spa_close(spa, FTAG); 2971 return (ENODEV); 2972 } 2973 } 2974 2975 vdev_clear(spa, vd); 2976 2977 (void) spa_vdev_state_exit(spa, NULL, 0); 2978 2979 /* 2980 * Resume any suspended I/Os. 2981 */ 2982 if (zio_resume(spa) != 0) 2983 error = EIO; 2984 2985 spa_close(spa, FTAG); 2986 2987 return (error); 2988 } 2989 2990 /* 2991 * inputs: 2992 * zc_name name of filesystem 2993 * zc_value name of origin snapshot 2994 * 2995 * outputs: none 2996 */ 2997 static int 2998 zfs_ioc_promote(zfs_cmd_t *zc) 2999 { 3000 char *cp; 3001 3002 /* 3003 * We don't need to unmount *all* the origin fs's snapshots, but 3004 * it's easier. 3005 */ 3006 cp = strchr(zc->zc_value, '@'); 3007 if (cp) 3008 *cp = '\0'; 3009 (void) dmu_objset_find(zc->zc_value, 3010 zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS); 3011 return (dsl_dataset_promote(zc->zc_name)); 3012 } 3013 3014 /* 3015 * Retrieve a single {user|group}{used|quota}@... property. 3016 * 3017 * inputs: 3018 * zc_name name of filesystem 3019 * zc_objset_type zfs_userquota_prop_t 3020 * zc_value domain name (eg. "S-1-234-567-89") 3021 * zc_guid RID/UID/GID 3022 * 3023 * outputs: 3024 * zc_cookie property value 3025 */ 3026 static int 3027 zfs_ioc_userspace_one(zfs_cmd_t *zc) 3028 { 3029 zfsvfs_t *zfsvfs; 3030 int error; 3031 3032 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) 3033 return (EINVAL); 3034 3035 error = zfsvfs_hold(zc->zc_name, B_TRUE, FTAG, &zfsvfs); 3036 if (error) 3037 return (error); 3038 3039 error = zfs_userspace_one(zfsvfs, 3040 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie); 3041 zfsvfs_rele(zfsvfs, FTAG); 3042 3043 return (error); 3044 } 3045 3046 /* 3047 * inputs: 3048 * zc_name name of filesystem 3049 * zc_cookie zap cursor 3050 * zc_objset_type zfs_userquota_prop_t 3051 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist) 3052 * 3053 * outputs: 3054 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t) 3055 * zc_cookie zap cursor 3056 */ 3057 static int 3058 zfs_ioc_userspace_many(zfs_cmd_t *zc) 3059 { 3060 zfsvfs_t *zfsvfs; 3061 int error; 3062 3063 error = zfsvfs_hold(zc->zc_name, B_TRUE, FTAG, &zfsvfs); 3064 if (error) 3065 return (error); 3066 3067 int bufsize = zc->zc_nvlist_dst_size; 3068 void *buf = kmem_alloc(bufsize, KM_SLEEP); 3069 3070 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie, 3071 buf, &zc->zc_nvlist_dst_size); 3072 3073 if (error == 0) { 3074 error = xcopyout(buf, 3075 (void *)(uintptr_t)zc->zc_nvlist_dst, 3076 zc->zc_nvlist_dst_size); 3077 } 3078 kmem_free(buf, bufsize); 3079 zfsvfs_rele(zfsvfs, FTAG); 3080 3081 return (error); 3082 } 3083 3084 /* 3085 * inputs: 3086 * zc_name name of filesystem 3087 * 3088 * outputs: 3089 * none 3090 */ 3091 static int 3092 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc) 3093 { 3094 objset_t *os; 3095 int error; 3096 zfsvfs_t *zfsvfs; 3097 3098 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) { 3099 if (!dmu_objset_userused_enabled(zfsvfs->z_os->os)) { 3100 /* 3101 * If userused is not enabled, it may be because the 3102 * objset needs to be closed & reopened (to grow the 3103 * objset_phys_t). Suspend/resume the fs will do that. 3104 */ 3105 int mode; 3106 error = zfs_suspend_fs(zfsvfs, NULL, &mode); 3107 if (error == 0) { 3108 error = zfs_resume_fs(zfsvfs, 3109 zc->zc_name, mode); 3110 } 3111 } 3112 if (error == 0) 3113 error = dmu_objset_userspace_upgrade(zfsvfs->z_os); 3114 VFS_RELE(zfsvfs->z_vfs); 3115 } else { 3116 error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, 3117 DS_MODE_USER, &os); 3118 if (error) 3119 return (error); 3120 3121 error = dmu_objset_userspace_upgrade(os); 3122 dmu_objset_close(os); 3123 } 3124 3125 return (error); 3126 } 3127 3128 /* 3129 * We don't want to have a hard dependency 3130 * against some special symbols in sharefs 3131 * nfs, and smbsrv. Determine them if needed when 3132 * the first file system is shared. 3133 * Neither sharefs, nfs or smbsrv are unloadable modules. 3134 */ 3135 int (*znfsexport_fs)(void *arg); 3136 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t); 3137 int (*zsmbexport_fs)(void *arg, boolean_t add_share); 3138 3139 int zfs_nfsshare_inited; 3140 int zfs_smbshare_inited; 3141 3142 ddi_modhandle_t nfs_mod; 3143 ddi_modhandle_t sharefs_mod; 3144 ddi_modhandle_t smbsrv_mod; 3145 kmutex_t zfs_share_lock; 3146 3147 static int 3148 zfs_init_sharefs() 3149 { 3150 int error; 3151 3152 ASSERT(MUTEX_HELD(&zfs_share_lock)); 3153 /* Both NFS and SMB shares also require sharetab support. */ 3154 if (sharefs_mod == NULL && ((sharefs_mod = 3155 ddi_modopen("fs/sharefs", 3156 KRTLD_MODE_FIRST, &error)) == NULL)) { 3157 return (ENOSYS); 3158 } 3159 if (zshare_fs == NULL && ((zshare_fs = 3160 (int (*)(enum sharefs_sys_op, share_t *, uint32_t)) 3161 ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) { 3162 return (ENOSYS); 3163 } 3164 return (0); 3165 } 3166 3167 static int 3168 zfs_ioc_share(zfs_cmd_t *zc) 3169 { 3170 int error; 3171 int opcode; 3172 3173 switch (zc->zc_share.z_sharetype) { 3174 case ZFS_SHARE_NFS: 3175 case ZFS_UNSHARE_NFS: 3176 if (zfs_nfsshare_inited == 0) { 3177 mutex_enter(&zfs_share_lock); 3178 if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs", 3179 KRTLD_MODE_FIRST, &error)) == NULL)) { 3180 mutex_exit(&zfs_share_lock); 3181 return (ENOSYS); 3182 } 3183 if (znfsexport_fs == NULL && 3184 ((znfsexport_fs = (int (*)(void *)) 3185 ddi_modsym(nfs_mod, 3186 "nfs_export", &error)) == NULL)) { 3187 mutex_exit(&zfs_share_lock); 3188 return (ENOSYS); 3189 } 3190 error = zfs_init_sharefs(); 3191 if (error) { 3192 mutex_exit(&zfs_share_lock); 3193 return (ENOSYS); 3194 } 3195 zfs_nfsshare_inited = 1; 3196 mutex_exit(&zfs_share_lock); 3197 } 3198 break; 3199 case ZFS_SHARE_SMB: 3200 case ZFS_UNSHARE_SMB: 3201 if (zfs_smbshare_inited == 0) { 3202 mutex_enter(&zfs_share_lock); 3203 if (smbsrv_mod == NULL && ((smbsrv_mod = 3204 ddi_modopen("drv/smbsrv", 3205 KRTLD_MODE_FIRST, &error)) == NULL)) { 3206 mutex_exit(&zfs_share_lock); 3207 return (ENOSYS); 3208 } 3209 if (zsmbexport_fs == NULL && ((zsmbexport_fs = 3210 (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod, 3211 "smb_server_share", &error)) == NULL)) { 3212 mutex_exit(&zfs_share_lock); 3213 return (ENOSYS); 3214 } 3215 error = zfs_init_sharefs(); 3216 if (error) { 3217 mutex_exit(&zfs_share_lock); 3218 return (ENOSYS); 3219 } 3220 zfs_smbshare_inited = 1; 3221 mutex_exit(&zfs_share_lock); 3222 } 3223 break; 3224 default: 3225 return (EINVAL); 3226 } 3227 3228 switch (zc->zc_share.z_sharetype) { 3229 case ZFS_SHARE_NFS: 3230 case ZFS_UNSHARE_NFS: 3231 if (error = 3232 znfsexport_fs((void *) 3233 (uintptr_t)zc->zc_share.z_exportdata)) 3234 return (error); 3235 break; 3236 case ZFS_SHARE_SMB: 3237 case ZFS_UNSHARE_SMB: 3238 if (error = zsmbexport_fs((void *) 3239 (uintptr_t)zc->zc_share.z_exportdata, 3240 zc->zc_share.z_sharetype == ZFS_SHARE_SMB ? 3241 B_TRUE: B_FALSE)) { 3242 return (error); 3243 } 3244 break; 3245 } 3246 3247 opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS || 3248 zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ? 3249 SHAREFS_ADD : SHAREFS_REMOVE; 3250 3251 /* 3252 * Add or remove share from sharetab 3253 */ 3254 error = zshare_fs(opcode, 3255 (void *)(uintptr_t)zc->zc_share.z_sharedata, 3256 zc->zc_share.z_sharemax); 3257 3258 return (error); 3259 3260 } 3261 3262 ace_t full_access[] = { 3263 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0} 3264 }; 3265 3266 /* 3267 * Remove all ACL files in shares dir 3268 */ 3269 static int 3270 zfs_smb_acl_purge(znode_t *dzp) 3271 { 3272 zap_cursor_t zc; 3273 zap_attribute_t zap; 3274 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 3275 int error; 3276 3277 for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id); 3278 (error = zap_cursor_retrieve(&zc, &zap)) == 0; 3279 zap_cursor_advance(&zc)) { 3280 if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred, 3281 NULL, 0)) != 0) 3282 break; 3283 } 3284 zap_cursor_fini(&zc); 3285 return (error); 3286 } 3287 3288 static int 3289 zfs_ioc_smb_acl(zfs_cmd_t *zc) 3290 { 3291 vnode_t *vp; 3292 znode_t *dzp; 3293 vnode_t *resourcevp = NULL; 3294 znode_t *sharedir; 3295 zfsvfs_t *zfsvfs; 3296 nvlist_t *nvlist; 3297 char *src, *target; 3298 vattr_t vattr; 3299 vsecattr_t vsec; 3300 int error = 0; 3301 3302 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE, 3303 NO_FOLLOW, NULL, &vp)) != 0) 3304 return (error); 3305 3306 /* Now make sure mntpnt and dataset are ZFS */ 3307 3308 if (vp->v_vfsp->vfs_fstype != zfsfstype || 3309 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource), 3310 zc->zc_name) != 0)) { 3311 VN_RELE(vp); 3312 return (EINVAL); 3313 } 3314 3315 dzp = VTOZ(vp); 3316 zfsvfs = dzp->z_zfsvfs; 3317 ZFS_ENTER(zfsvfs); 3318 3319 /* 3320 * Create share dir if its missing. 3321 */ 3322 mutex_enter(&zfsvfs->z_lock); 3323 if (zfsvfs->z_shares_dir == 0) { 3324 dmu_tx_t *tx; 3325 3326 tx = dmu_tx_create(zfsvfs->z_os); 3327 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE, 3328 ZFS_SHARES_DIR); 3329 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 3330 error = dmu_tx_assign(tx, TXG_WAIT); 3331 if (error) { 3332 dmu_tx_abort(tx); 3333 } else { 3334 error = zfs_create_share_dir(zfsvfs, tx); 3335 dmu_tx_commit(tx); 3336 } 3337 if (error) { 3338 mutex_exit(&zfsvfs->z_lock); 3339 VN_RELE(vp); 3340 ZFS_EXIT(zfsvfs); 3341 return (error); 3342 } 3343 } 3344 mutex_exit(&zfsvfs->z_lock); 3345 3346 ASSERT(zfsvfs->z_shares_dir); 3347 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) { 3348 VN_RELE(vp); 3349 ZFS_EXIT(zfsvfs); 3350 return (error); 3351 } 3352 3353 switch (zc->zc_cookie) { 3354 case ZFS_SMB_ACL_ADD: 3355 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE; 3356 vattr.va_type = VREG; 3357 vattr.va_mode = S_IFREG|0777; 3358 vattr.va_uid = 0; 3359 vattr.va_gid = 0; 3360 3361 vsec.vsa_mask = VSA_ACE; 3362 vsec.vsa_aclentp = &full_access; 3363 vsec.vsa_aclentsz = sizeof (full_access); 3364 vsec.vsa_aclcnt = 1; 3365 3366 error = VOP_CREATE(ZTOV(sharedir), zc->zc_string, 3367 &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec); 3368 if (resourcevp) 3369 VN_RELE(resourcevp); 3370 break; 3371 3372 case ZFS_SMB_ACL_REMOVE: 3373 error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred, 3374 NULL, 0); 3375 break; 3376 3377 case ZFS_SMB_ACL_RENAME: 3378 if ((error = get_nvlist(zc->zc_nvlist_src, 3379 zc->zc_nvlist_src_size, &nvlist)) != 0) { 3380 VN_RELE(vp); 3381 ZFS_EXIT(zfsvfs); 3382 return (error); 3383 } 3384 if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) || 3385 nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET, 3386 &target)) { 3387 VN_RELE(vp); 3388 VN_RELE(ZTOV(sharedir)); 3389 ZFS_EXIT(zfsvfs); 3390 return (error); 3391 } 3392 error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target, 3393 kcred, NULL, 0); 3394 nvlist_free(nvlist); 3395 break; 3396 3397 case ZFS_SMB_ACL_PURGE: 3398 error = zfs_smb_acl_purge(sharedir); 3399 break; 3400 3401 default: 3402 error = EINVAL; 3403 break; 3404 } 3405 3406 VN_RELE(vp); 3407 VN_RELE(ZTOV(sharedir)); 3408 3409 ZFS_EXIT(zfsvfs); 3410 3411 return (error); 3412 } 3413 3414 /* 3415 * pool create, destroy, and export don't log the history as part of 3416 * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export 3417 * do the logging of those commands. 3418 */ 3419 static zfs_ioc_vec_t zfs_ioc_vec[] = { 3420 { zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE, 3421 B_FALSE }, 3422 { zfs_ioc_pool_destroy, zfs_secpolicy_config, POOL_NAME, B_FALSE, 3423 B_FALSE }, 3424 { zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE, 3425 B_FALSE }, 3426 { zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE, 3427 B_FALSE }, 3428 { zfs_ioc_pool_configs, zfs_secpolicy_none, NO_NAME, B_FALSE, 3429 B_FALSE }, 3430 { zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE, 3431 B_FALSE }, 3432 { zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE, 3433 B_FALSE }, 3434 { zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE, 3435 B_TRUE }, 3436 { zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE, 3437 B_FALSE }, 3438 { zfs_ioc_pool_upgrade, zfs_secpolicy_config, POOL_NAME, B_TRUE, 3439 B_TRUE }, 3440 { zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE, 3441 B_FALSE }, 3442 { zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE, 3443 B_TRUE }, 3444 { zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE, 3445 B_TRUE }, 3446 { zfs_ioc_vdev_set_state, zfs_secpolicy_config, POOL_NAME, B_TRUE, 3447 B_FALSE }, 3448 { zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE, 3449 B_TRUE }, 3450 { zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE, 3451 B_TRUE }, 3452 { zfs_ioc_vdev_setpath, zfs_secpolicy_config, POOL_NAME, B_FALSE, 3453 B_TRUE }, 3454 { zfs_ioc_objset_stats, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 3455 B_FALSE }, 3456 { zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 3457 B_FALSE }, 3458 { zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 3459 B_FALSE }, 3460 { zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 3461 B_FALSE }, 3462 { zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE, B_TRUE }, 3463 { zfs_ioc_create_minor, zfs_secpolicy_minor, DATASET_NAME, B_FALSE, 3464 B_FALSE }, 3465 { zfs_ioc_remove_minor, zfs_secpolicy_minor, DATASET_NAME, B_FALSE, 3466 B_FALSE }, 3467 { zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE, B_TRUE }, 3468 { zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE, 3469 B_TRUE}, 3470 { zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE, 3471 B_TRUE }, 3472 { zfs_ioc_rename, zfs_secpolicy_rename, DATASET_NAME, B_TRUE, B_TRUE }, 3473 { zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE, B_TRUE }, 3474 { zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE, B_FALSE }, 3475 { zfs_ioc_inject_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE, 3476 B_FALSE }, 3477 { zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE, 3478 B_FALSE }, 3479 { zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE, 3480 B_FALSE }, 3481 { zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE, 3482 B_FALSE }, 3483 { zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE, B_FALSE }, 3484 { zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE, 3485 B_TRUE }, 3486 { zfs_ioc_destroy_snaps, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE, 3487 B_TRUE }, 3488 { zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE, 3489 B_TRUE }, 3490 { zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE, 3491 B_FALSE }, 3492 { zfs_ioc_obj_to_path, zfs_secpolicy_config, NO_NAME, B_FALSE, 3493 B_FALSE }, 3494 { zfs_ioc_pool_set_props, zfs_secpolicy_config, POOL_NAME, B_TRUE, 3495 B_TRUE }, 3496 { zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE, 3497 B_FALSE }, 3498 { zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE, 3499 B_TRUE }, 3500 { zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 3501 B_FALSE }, 3502 { zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi, DATASET_NAME, B_FALSE, 3503 B_FALSE }, 3504 { zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE, B_FALSE }, 3505 { zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE, 3506 B_TRUE }, 3507 { zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE, 3508 B_FALSE }, 3509 { zfs_ioc_userspace_one, zfs_secpolicy_userspace_one, 3510 DATASET_NAME, B_FALSE, B_FALSE }, 3511 { zfs_ioc_userspace_many, zfs_secpolicy_userspace_many, 3512 DATASET_NAME, B_FALSE, B_FALSE }, 3513 { zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade, 3514 DATASET_NAME, B_FALSE, B_TRUE }, 3515 }; 3516 3517 int 3518 pool_status_check(const char *name, zfs_ioc_namecheck_t type) 3519 { 3520 spa_t *spa; 3521 int error; 3522 3523 ASSERT(type == POOL_NAME || type == DATASET_NAME); 3524 3525 error = spa_open(name, &spa, FTAG); 3526 if (error == 0) { 3527 if (spa_suspended(spa)) 3528 error = EAGAIN; 3529 spa_close(spa, FTAG); 3530 } 3531 return (error); 3532 } 3533 3534 static int 3535 zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 3536 { 3537 zfs_cmd_t *zc; 3538 uint_t vec; 3539 int error, rc; 3540 3541 if (getminor(dev) != 0) 3542 return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp)); 3543 3544 vec = cmd - ZFS_IOC; 3545 ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip)); 3546 3547 if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0])) 3548 return (EINVAL); 3549 3550 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP); 3551 3552 error = xcopyin((void *)arg, zc, sizeof (zfs_cmd_t)); 3553 3554 if (error == 0) 3555 error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr); 3556 3557 /* 3558 * Ensure that all pool/dataset names are valid before we pass down to 3559 * the lower layers. 3560 */ 3561 if (error == 0) { 3562 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0'; 3563 switch (zfs_ioc_vec[vec].zvec_namecheck) { 3564 case POOL_NAME: 3565 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0) 3566 error = EINVAL; 3567 if (zfs_ioc_vec[vec].zvec_pool_check) 3568 error = pool_status_check(zc->zc_name, 3569 zfs_ioc_vec[vec].zvec_namecheck); 3570 break; 3571 3572 case DATASET_NAME: 3573 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0) 3574 error = EINVAL; 3575 if (zfs_ioc_vec[vec].zvec_pool_check) 3576 error = pool_status_check(zc->zc_name, 3577 zfs_ioc_vec[vec].zvec_namecheck); 3578 break; 3579 3580 case NO_NAME: 3581 break; 3582 } 3583 } 3584 3585 if (error == 0) 3586 error = zfs_ioc_vec[vec].zvec_func(zc); 3587 3588 rc = xcopyout(zc, (void *)arg, sizeof (zfs_cmd_t)); 3589 if (error == 0) { 3590 error = rc; 3591 if (zfs_ioc_vec[vec].zvec_his_log) 3592 zfs_log_history(zc); 3593 } 3594 3595 kmem_free(zc, sizeof (zfs_cmd_t)); 3596 return (error); 3597 } 3598 3599 static int 3600 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 3601 { 3602 if (cmd != DDI_ATTACH) 3603 return (DDI_FAILURE); 3604 3605 if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0, 3606 DDI_PSEUDO, 0) == DDI_FAILURE) 3607 return (DDI_FAILURE); 3608 3609 zfs_dip = dip; 3610 3611 ddi_report_dev(dip); 3612 3613 return (DDI_SUCCESS); 3614 } 3615 3616 static int 3617 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 3618 { 3619 if (spa_busy() || zfs_busy() || zvol_busy()) 3620 return (DDI_FAILURE); 3621 3622 if (cmd != DDI_DETACH) 3623 return (DDI_FAILURE); 3624 3625 zfs_dip = NULL; 3626 3627 ddi_prop_remove_all(dip); 3628 ddi_remove_minor_node(dip, NULL); 3629 3630 return (DDI_SUCCESS); 3631 } 3632 3633 /*ARGSUSED*/ 3634 static int 3635 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 3636 { 3637 switch (infocmd) { 3638 case DDI_INFO_DEVT2DEVINFO: 3639 *result = zfs_dip; 3640 return (DDI_SUCCESS); 3641 3642 case DDI_INFO_DEVT2INSTANCE: 3643 *result = (void *)0; 3644 return (DDI_SUCCESS); 3645 } 3646 3647 return (DDI_FAILURE); 3648 } 3649 3650 /* 3651 * OK, so this is a little weird. 3652 * 3653 * /dev/zfs is the control node, i.e. minor 0. 3654 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0. 3655 * 3656 * /dev/zfs has basically nothing to do except serve up ioctls, 3657 * so most of the standard driver entry points are in zvol.c. 3658 */ 3659 static struct cb_ops zfs_cb_ops = { 3660 zvol_open, /* open */ 3661 zvol_close, /* close */ 3662 zvol_strategy, /* strategy */ 3663 nodev, /* print */ 3664 zvol_dump, /* dump */ 3665 zvol_read, /* read */ 3666 zvol_write, /* write */ 3667 zfsdev_ioctl, /* ioctl */ 3668 nodev, /* devmap */ 3669 nodev, /* mmap */ 3670 nodev, /* segmap */ 3671 nochpoll, /* poll */ 3672 ddi_prop_op, /* prop_op */ 3673 NULL, /* streamtab */ 3674 D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */ 3675 CB_REV, /* version */ 3676 nodev, /* async read */ 3677 nodev, /* async write */ 3678 }; 3679 3680 static struct dev_ops zfs_dev_ops = { 3681 DEVO_REV, /* version */ 3682 0, /* refcnt */ 3683 zfs_info, /* info */ 3684 nulldev, /* identify */ 3685 nulldev, /* probe */ 3686 zfs_attach, /* attach */ 3687 zfs_detach, /* detach */ 3688 nodev, /* reset */ 3689 &zfs_cb_ops, /* driver operations */ 3690 NULL, /* no bus operations */ 3691 NULL, /* power */ 3692 ddi_quiesce_not_needed, /* quiesce */ 3693 }; 3694 3695 static struct modldrv zfs_modldrv = { 3696 &mod_driverops, 3697 "ZFS storage pool", 3698 &zfs_dev_ops 3699 }; 3700 3701 static struct modlinkage modlinkage = { 3702 MODREV_1, 3703 (void *)&zfs_modlfs, 3704 (void *)&zfs_modldrv, 3705 NULL 3706 }; 3707 3708 3709 uint_t zfs_fsyncer_key; 3710 extern uint_t rrw_tsd_key; 3711 3712 int 3713 _init(void) 3714 { 3715 int error; 3716 3717 spa_init(FREAD | FWRITE); 3718 zfs_init(); 3719 zvol_init(); 3720 3721 if ((error = mod_install(&modlinkage)) != 0) { 3722 zvol_fini(); 3723 zfs_fini(); 3724 spa_fini(); 3725 return (error); 3726 } 3727 3728 tsd_create(&zfs_fsyncer_key, NULL); 3729 tsd_create(&rrw_tsd_key, NULL); 3730 3731 error = ldi_ident_from_mod(&modlinkage, &zfs_li); 3732 ASSERT(error == 0); 3733 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL); 3734 3735 return (0); 3736 } 3737 3738 int 3739 _fini(void) 3740 { 3741 int error; 3742 3743 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled) 3744 return (EBUSY); 3745 3746 if ((error = mod_remove(&modlinkage)) != 0) 3747 return (error); 3748 3749 zvol_fini(); 3750 zfs_fini(); 3751 spa_fini(); 3752 if (zfs_nfsshare_inited) 3753 (void) ddi_modclose(nfs_mod); 3754 if (zfs_smbshare_inited) 3755 (void) ddi_modclose(smbsrv_mod); 3756 if (zfs_nfsshare_inited || zfs_smbshare_inited) 3757 (void) ddi_modclose(sharefs_mod); 3758 3759 tsd_destroy(&zfs_fsyncer_key); 3760 ldi_ident_release(zfs_li); 3761 zfs_li = NULL; 3762 mutex_destroy(&zfs_share_lock); 3763 3764 return (error); 3765 } 3766 3767 int 3768 _info(struct modinfo *modinfop) 3769 { 3770 return (mod_info(&modlinkage, modinfop)); 3771 } 3772