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