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