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