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