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