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