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