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