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