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