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 static 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 /* 2992 * Search the vfs list for a specified resource. Returns a pointer to it 2993 * or NULL if no suitable entry is found. The caller of this routine 2994 * is responsible for releasing the returned vfs pointer. 2995 */ 2996 static vfs_t * 2997 zfs_get_vfs(const char *resource) 2998 { 2999 struct vfs *vfsp; 3000 struct vfs *vfs_found = NULL; 3001 3002 vfs_list_read_lock(); 3003 vfsp = rootvfs; 3004 do { 3005 if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) { 3006 VFS_HOLD(vfsp); 3007 vfs_found = vfsp; 3008 break; 3009 } 3010 vfsp = vfsp->vfs_next; 3011 } while (vfsp != rootvfs); 3012 vfs_list_unlock(); 3013 return (vfs_found); 3014 } 3015 3016 /* ARGSUSED */ 3017 static void 3018 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 3019 { 3020 zfs_creat_t *zct = arg; 3021 3022 zfs_create_fs(os, cr, zct->zct_zplprops, tx); 3023 } 3024 3025 #define ZFS_PROP_UNDEFINED ((uint64_t)-1) 3026 3027 /* 3028 * inputs: 3029 * os parent objset pointer (NULL if root fs) 3030 * fuids_ok fuids allowed in this version of the spa? 3031 * sa_ok SAs allowed in this version of the spa? 3032 * createprops list of properties requested by creator 3033 * 3034 * outputs: 3035 * zplprops values for the zplprops we attach to the master node object 3036 * is_ci true if requested file system will be purely case-insensitive 3037 * 3038 * Determine the settings for utf8only, normalization and 3039 * casesensitivity. Specific values may have been requested by the 3040 * creator and/or we can inherit values from the parent dataset. If 3041 * the file system is of too early a vintage, a creator can not 3042 * request settings for these properties, even if the requested 3043 * setting is the default value. We don't actually want to create dsl 3044 * properties for these, so remove them from the source nvlist after 3045 * processing. 3046 */ 3047 static int 3048 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver, 3049 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops, 3050 nvlist_t *zplprops, boolean_t *is_ci) 3051 { 3052 uint64_t sense = ZFS_PROP_UNDEFINED; 3053 uint64_t norm = ZFS_PROP_UNDEFINED; 3054 uint64_t u8 = ZFS_PROP_UNDEFINED; 3055 3056 ASSERT(zplprops != NULL); 3057 3058 if (os != NULL && os->os_phys->os_type != DMU_OST_ZFS) 3059 return (SET_ERROR(EINVAL)); 3060 3061 /* 3062 * Pull out creator prop choices, if any. 3063 */ 3064 if (createprops) { 3065 (void) nvlist_lookup_uint64(createprops, 3066 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver); 3067 (void) nvlist_lookup_uint64(createprops, 3068 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm); 3069 (void) nvlist_remove_all(createprops, 3070 zfs_prop_to_name(ZFS_PROP_NORMALIZE)); 3071 (void) nvlist_lookup_uint64(createprops, 3072 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8); 3073 (void) nvlist_remove_all(createprops, 3074 zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 3075 (void) nvlist_lookup_uint64(createprops, 3076 zfs_prop_to_name(ZFS_PROP_CASE), &sense); 3077 (void) nvlist_remove_all(createprops, 3078 zfs_prop_to_name(ZFS_PROP_CASE)); 3079 } 3080 3081 /* 3082 * If the zpl version requested is whacky or the file system 3083 * or pool is version is too "young" to support normalization 3084 * and the creator tried to set a value for one of the props, 3085 * error out. 3086 */ 3087 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) || 3088 (zplver >= ZPL_VERSION_FUID && !fuids_ok) || 3089 (zplver >= ZPL_VERSION_SA && !sa_ok) || 3090 (zplver < ZPL_VERSION_NORMALIZATION && 3091 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED || 3092 sense != ZFS_PROP_UNDEFINED))) 3093 return (SET_ERROR(ENOTSUP)); 3094 3095 /* 3096 * Put the version in the zplprops 3097 */ 3098 VERIFY(nvlist_add_uint64(zplprops, 3099 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0); 3100 3101 if (norm == ZFS_PROP_UNDEFINED) 3102 VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0); 3103 VERIFY(nvlist_add_uint64(zplprops, 3104 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0); 3105 3106 /* 3107 * If we're normalizing, names must always be valid UTF-8 strings. 3108 */ 3109 if (norm) 3110 u8 = 1; 3111 if (u8 == ZFS_PROP_UNDEFINED) 3112 VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0); 3113 VERIFY(nvlist_add_uint64(zplprops, 3114 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0); 3115 3116 if (sense == ZFS_PROP_UNDEFINED) 3117 VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0); 3118 VERIFY(nvlist_add_uint64(zplprops, 3119 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0); 3120 3121 if (is_ci) 3122 *is_ci = (sense == ZFS_CASE_INSENSITIVE); 3123 3124 return (0); 3125 } 3126 3127 static int 3128 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops, 3129 nvlist_t *zplprops, boolean_t *is_ci) 3130 { 3131 boolean_t fuids_ok, sa_ok; 3132 uint64_t zplver = ZPL_VERSION; 3133 objset_t *os = NULL; 3134 char parentname[ZFS_MAX_DATASET_NAME_LEN]; 3135 char *cp; 3136 spa_t *spa; 3137 uint64_t spa_vers; 3138 int error; 3139 3140 (void) strlcpy(parentname, dataset, sizeof (parentname)); 3141 cp = strrchr(parentname, '/'); 3142 ASSERT(cp != NULL); 3143 cp[0] = '\0'; 3144 3145 if ((error = spa_open(dataset, &spa, FTAG)) != 0) 3146 return (error); 3147 3148 spa_vers = spa_version(spa); 3149 spa_close(spa, FTAG); 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 /* 3156 * Open parent object set so we can inherit zplprop values. 3157 */ 3158 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0) 3159 return (error); 3160 3161 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops, 3162 zplprops, is_ci); 3163 dmu_objset_rele(os, FTAG); 3164 return (error); 3165 } 3166 3167 static int 3168 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops, 3169 nvlist_t *zplprops, boolean_t *is_ci) 3170 { 3171 boolean_t fuids_ok; 3172 boolean_t sa_ok; 3173 uint64_t zplver = ZPL_VERSION; 3174 int error; 3175 3176 zplver = zfs_zpl_version_map(spa_vers); 3177 fuids_ok = (zplver >= ZPL_VERSION_FUID); 3178 sa_ok = (zplver >= ZPL_VERSION_SA); 3179 3180 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok, 3181 createprops, zplprops, is_ci); 3182 return (error); 3183 } 3184 3185 /* 3186 * innvl: { 3187 * "type" -> dmu_objset_type_t (int32) 3188 * (optional) "props" -> { prop -> value } 3189 * } 3190 * 3191 * outnvl: propname -> error code (int32) 3192 */ 3193 static int 3194 zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl) 3195 { 3196 int error = 0; 3197 zfs_creat_t zct = { 0 }; 3198 nvlist_t *nvprops = NULL; 3199 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx); 3200 int32_t type32; 3201 dmu_objset_type_t type; 3202 boolean_t is_insensitive = B_FALSE; 3203 3204 if (nvlist_lookup_int32(innvl, "type", &type32) != 0) 3205 return (SET_ERROR(EINVAL)); 3206 type = type32; 3207 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops); 3208 3209 switch (type) { 3210 case DMU_OST_ZFS: 3211 cbfunc = zfs_create_cb; 3212 break; 3213 3214 case DMU_OST_ZVOL: 3215 cbfunc = zvol_create_cb; 3216 break; 3217 3218 default: 3219 cbfunc = NULL; 3220 break; 3221 } 3222 if (strchr(fsname, '@') || 3223 strchr(fsname, '%')) 3224 return (SET_ERROR(EINVAL)); 3225 3226 zct.zct_props = nvprops; 3227 3228 if (cbfunc == NULL) 3229 return (SET_ERROR(EINVAL)); 3230 3231 if (type == DMU_OST_ZVOL) { 3232 uint64_t volsize, volblocksize; 3233 3234 if (nvprops == NULL) 3235 return (SET_ERROR(EINVAL)); 3236 if (nvlist_lookup_uint64(nvprops, 3237 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0) 3238 return (SET_ERROR(EINVAL)); 3239 3240 if ((error = nvlist_lookup_uint64(nvprops, 3241 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 3242 &volblocksize)) != 0 && error != ENOENT) 3243 return (SET_ERROR(EINVAL)); 3244 3245 if (error != 0) 3246 volblocksize = zfs_prop_default_numeric( 3247 ZFS_PROP_VOLBLOCKSIZE); 3248 3249 if ((error = zvol_check_volblocksize( 3250 volblocksize)) != 0 || 3251 (error = zvol_check_volsize(volsize, 3252 volblocksize)) != 0) 3253 return (error); 3254 } else if (type == DMU_OST_ZFS) { 3255 int error; 3256 3257 /* 3258 * We have to have normalization and 3259 * case-folding flags correct when we do the 3260 * file system creation, so go figure them out 3261 * now. 3262 */ 3263 VERIFY(nvlist_alloc(&zct.zct_zplprops, 3264 NV_UNIQUE_NAME, KM_SLEEP) == 0); 3265 error = zfs_fill_zplprops(fsname, nvprops, 3266 zct.zct_zplprops, &is_insensitive); 3267 if (error != 0) { 3268 nvlist_free(zct.zct_zplprops); 3269 return (error); 3270 } 3271 } 3272 3273 error = dmu_objset_create(fsname, type, 3274 is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct); 3275 nvlist_free(zct.zct_zplprops); 3276 3277 /* 3278 * It would be nice to do this atomically. 3279 */ 3280 if (error == 0) { 3281 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL, 3282 nvprops, outnvl); 3283 if (error != 0) 3284 (void) dsl_destroy_head(fsname); 3285 } 3286 return (error); 3287 } 3288 3289 /* 3290 * innvl: { 3291 * "origin" -> name of origin snapshot 3292 * (optional) "props" -> { prop -> value } 3293 * } 3294 * 3295 * outnvl: propname -> error code (int32) 3296 */ 3297 static int 3298 zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl) 3299 { 3300 int error = 0; 3301 nvlist_t *nvprops = NULL; 3302 char *origin_name; 3303 3304 if (nvlist_lookup_string(innvl, "origin", &origin_name) != 0) 3305 return (SET_ERROR(EINVAL)); 3306 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops); 3307 3308 if (strchr(fsname, '@') || 3309 strchr(fsname, '%')) 3310 return (SET_ERROR(EINVAL)); 3311 3312 if (dataset_namecheck(origin_name, NULL, NULL) != 0) 3313 return (SET_ERROR(EINVAL)); 3314 error = dmu_objset_clone(fsname, origin_name); 3315 if (error != 0) 3316 return (error); 3317 3318 /* 3319 * It would be nice to do this atomically. 3320 */ 3321 if (error == 0) { 3322 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL, 3323 nvprops, outnvl); 3324 if (error != 0) 3325 (void) dsl_destroy_head(fsname); 3326 } 3327 return (error); 3328 } 3329 3330 /* 3331 * innvl: { 3332 * "snaps" -> { snapshot1, snapshot2 } 3333 * (optional) "props" -> { prop -> value (string) } 3334 * } 3335 * 3336 * outnvl: snapshot -> error code (int32) 3337 */ 3338 static int 3339 zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl) 3340 { 3341 nvlist_t *snaps; 3342 nvlist_t *props = NULL; 3343 int error, poollen; 3344 nvpair_t *pair; 3345 3346 (void) nvlist_lookup_nvlist(innvl, "props", &props); 3347 if ((error = zfs_check_userprops(poolname, props)) != 0) 3348 return (error); 3349 3350 if (!nvlist_empty(props) && 3351 zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS)) 3352 return (SET_ERROR(ENOTSUP)); 3353 3354 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0) 3355 return (SET_ERROR(EINVAL)); 3356 poollen = strlen(poolname); 3357 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL; 3358 pair = nvlist_next_nvpair(snaps, pair)) { 3359 const char *name = nvpair_name(pair); 3360 const char *cp = strchr(name, '@'); 3361 3362 /* 3363 * The snap name must contain an @, and the part after it must 3364 * contain only valid characters. 3365 */ 3366 if (cp == NULL || 3367 zfs_component_namecheck(cp + 1, NULL, NULL) != 0) 3368 return (SET_ERROR(EINVAL)); 3369 3370 /* 3371 * The snap must be in the specified pool. 3372 */ 3373 if (strncmp(name, poolname, poollen) != 0 || 3374 (name[poollen] != '/' && name[poollen] != '@')) 3375 return (SET_ERROR(EXDEV)); 3376 3377 /* This must be the only snap of this fs. */ 3378 for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair); 3379 pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) { 3380 if (strncmp(name, nvpair_name(pair2), cp - name + 1) 3381 == 0) { 3382 return (SET_ERROR(EXDEV)); 3383 } 3384 } 3385 } 3386 3387 error = dsl_dataset_snapshot(snaps, props, outnvl); 3388 return (error); 3389 } 3390 3391 /* 3392 * innvl: "message" -> string 3393 */ 3394 /* ARGSUSED */ 3395 static int 3396 zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl) 3397 { 3398 char *message; 3399 spa_t *spa; 3400 int error; 3401 char *poolname; 3402 3403 /* 3404 * The poolname in the ioctl is not set, we get it from the TSD, 3405 * which was set at the end of the last successful ioctl that allows 3406 * logging. The secpolicy func already checked that it is set. 3407 * Only one log ioctl is allowed after each successful ioctl, so 3408 * we clear the TSD here. 3409 */ 3410 poolname = tsd_get(zfs_allow_log_key); 3411 (void) tsd_set(zfs_allow_log_key, NULL); 3412 error = spa_open(poolname, &spa, FTAG); 3413 strfree(poolname); 3414 if (error != 0) 3415 return (error); 3416 3417 if (nvlist_lookup_string(innvl, "message", &message) != 0) { 3418 spa_close(spa, FTAG); 3419 return (SET_ERROR(EINVAL)); 3420 } 3421 3422 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) { 3423 spa_close(spa, FTAG); 3424 return (SET_ERROR(ENOTSUP)); 3425 } 3426 3427 error = spa_history_log(spa, message); 3428 spa_close(spa, FTAG); 3429 return (error); 3430 } 3431 3432 /* 3433 * The dp_config_rwlock must not be held when calling this, because the 3434 * unmount may need to write out data. 3435 * 3436 * This function is best-effort. Callers must deal gracefully if it 3437 * remains mounted (or is remounted after this call). 3438 * 3439 * Returns 0 if the argument is not a snapshot, or it is not currently a 3440 * filesystem, or we were able to unmount it. Returns error code otherwise. 3441 */ 3442 int 3443 zfs_unmount_snap(const char *snapname) 3444 { 3445 vfs_t *vfsp; 3446 zfsvfs_t *zfsvfs; 3447 int err; 3448 3449 if (strchr(snapname, '@') == NULL) 3450 return (0); 3451 3452 vfsp = zfs_get_vfs(snapname); 3453 if (vfsp == NULL) 3454 return (0); 3455 3456 zfsvfs = vfsp->vfs_data; 3457 ASSERT(!dsl_pool_config_held(dmu_objset_pool(zfsvfs->z_os))); 3458 3459 err = vn_vfswlock(vfsp->vfs_vnodecovered); 3460 VFS_RELE(vfsp); 3461 if (err != 0) 3462 return (SET_ERROR(err)); 3463 3464 /* 3465 * Always force the unmount for snapshots. 3466 */ 3467 (void) dounmount(vfsp, MS_FORCE, kcred); 3468 return (0); 3469 } 3470 3471 /* ARGSUSED */ 3472 static int 3473 zfs_unmount_snap_cb(const char *snapname, void *arg) 3474 { 3475 return (zfs_unmount_snap(snapname)); 3476 } 3477 3478 /* 3479 * When a clone is destroyed, its origin may also need to be destroyed, 3480 * in which case it must be unmounted. This routine will do that unmount 3481 * if necessary. 3482 */ 3483 void 3484 zfs_destroy_unmount_origin(const char *fsname) 3485 { 3486 int error; 3487 objset_t *os; 3488 dsl_dataset_t *ds; 3489 3490 error = dmu_objset_hold(fsname, FTAG, &os); 3491 if (error != 0) 3492 return; 3493 ds = dmu_objset_ds(os); 3494 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) { 3495 char originname[ZFS_MAX_DATASET_NAME_LEN]; 3496 dsl_dataset_name(ds->ds_prev, originname); 3497 dmu_objset_rele(os, FTAG); 3498 (void) zfs_unmount_snap(originname); 3499 } else { 3500 dmu_objset_rele(os, FTAG); 3501 } 3502 } 3503 3504 /* 3505 * innvl: { 3506 * "snaps" -> { snapshot1, snapshot2 } 3507 * (optional boolean) "defer" 3508 * } 3509 * 3510 * outnvl: snapshot -> error code (int32) 3511 * 3512 */ 3513 /* ARGSUSED */ 3514 static int 3515 zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl) 3516 { 3517 nvlist_t *snaps; 3518 nvpair_t *pair; 3519 boolean_t defer; 3520 3521 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0) 3522 return (SET_ERROR(EINVAL)); 3523 defer = nvlist_exists(innvl, "defer"); 3524 3525 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL; 3526 pair = nvlist_next_nvpair(snaps, pair)) { 3527 (void) zfs_unmount_snap(nvpair_name(pair)); 3528 } 3529 3530 return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl)); 3531 } 3532 3533 /* 3534 * Create bookmarks. Bookmark names are of the form <fs>#<bmark>. 3535 * All bookmarks must be in the same pool. 3536 * 3537 * innvl: { 3538 * bookmark1 -> snapshot1, bookmark2 -> snapshot2 3539 * } 3540 * 3541 * outnvl: bookmark -> error code (int32) 3542 * 3543 */ 3544 /* ARGSUSED */ 3545 static int 3546 zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl) 3547 { 3548 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL); 3549 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) { 3550 char *snap_name; 3551 3552 /* 3553 * Verify the snapshot argument. 3554 */ 3555 if (nvpair_value_string(pair, &snap_name) != 0) 3556 return (SET_ERROR(EINVAL)); 3557 3558 3559 /* Verify that the keys (bookmarks) are unique */ 3560 for (nvpair_t *pair2 = nvlist_next_nvpair(innvl, pair); 3561 pair2 != NULL; pair2 = nvlist_next_nvpair(innvl, pair2)) { 3562 if (strcmp(nvpair_name(pair), nvpair_name(pair2)) == 0) 3563 return (SET_ERROR(EINVAL)); 3564 } 3565 } 3566 3567 return (dsl_bookmark_create(innvl, outnvl)); 3568 } 3569 3570 /* 3571 * innvl: { 3572 * property 1, property 2, ... 3573 * } 3574 * 3575 * outnvl: { 3576 * bookmark name 1 -> { property 1, property 2, ... }, 3577 * bookmark name 2 -> { property 1, property 2, ... } 3578 * } 3579 * 3580 */ 3581 static int 3582 zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl) 3583 { 3584 return (dsl_get_bookmarks(fsname, innvl, outnvl)); 3585 } 3586 3587 /* 3588 * innvl: { 3589 * bookmark name 1, bookmark name 2 3590 * } 3591 * 3592 * outnvl: bookmark -> error code (int32) 3593 * 3594 */ 3595 static int 3596 zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl, 3597 nvlist_t *outnvl) 3598 { 3599 int error, poollen; 3600 3601 poollen = strlen(poolname); 3602 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL); 3603 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) { 3604 const char *name = nvpair_name(pair); 3605 const char *cp = strchr(name, '#'); 3606 3607 /* 3608 * The bookmark name must contain an #, and the part after it 3609 * must contain only valid characters. 3610 */ 3611 if (cp == NULL || 3612 zfs_component_namecheck(cp + 1, NULL, NULL) != 0) 3613 return (SET_ERROR(EINVAL)); 3614 3615 /* 3616 * The bookmark must be in the specified pool. 3617 */ 3618 if (strncmp(name, poolname, poollen) != 0 || 3619 (name[poollen] != '/' && name[poollen] != '#')) 3620 return (SET_ERROR(EXDEV)); 3621 } 3622 3623 error = dsl_bookmark_destroy(innvl, outnvl); 3624 return (error); 3625 } 3626 3627 static int 3628 zfs_ioc_channel_program(const char *poolname, nvlist_t *innvl, 3629 nvlist_t *outnvl) 3630 { 3631 char *program; 3632 uint64_t instrlimit, memlimit; 3633 nvpair_t *nvarg = NULL; 3634 3635 if (0 != nvlist_lookup_string(innvl, ZCP_ARG_PROGRAM, &program)) { 3636 return (EINVAL); 3637 } 3638 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_INSTRLIMIT, &instrlimit)) { 3639 instrlimit = ZCP_DEFAULT_INSTRLIMIT; 3640 } 3641 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_MEMLIMIT, &memlimit)) { 3642 memlimit = ZCP_DEFAULT_MEMLIMIT; 3643 } 3644 if (0 != nvlist_lookup_nvpair(innvl, ZCP_ARG_ARGLIST, &nvarg)) { 3645 return (EINVAL); 3646 } 3647 3648 if (instrlimit == 0 || instrlimit > zfs_lua_max_instrlimit) 3649 return (EINVAL); 3650 if (memlimit == 0 || memlimit > ZCP_MAX_MEMLIMIT) 3651 return (EINVAL); 3652 3653 return (zcp_eval(poolname, program, instrlimit, memlimit, 3654 nvarg, outnvl)); 3655 } 3656 3657 /* 3658 * inputs: 3659 * zc_name name of dataset to destroy 3660 * zc_objset_type type of objset 3661 * zc_defer_destroy mark for deferred destroy 3662 * 3663 * outputs: none 3664 */ 3665 static int 3666 zfs_ioc_destroy(zfs_cmd_t *zc) 3667 { 3668 int err; 3669 3670 if (zc->zc_objset_type == DMU_OST_ZFS) { 3671 err = zfs_unmount_snap(zc->zc_name); 3672 if (err != 0) 3673 return (err); 3674 } 3675 3676 if (strchr(zc->zc_name, '@')) 3677 err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy); 3678 else 3679 err = dsl_destroy_head(zc->zc_name); 3680 if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0) 3681 (void) zvol_remove_minor(zc->zc_name); 3682 return (err); 3683 } 3684 3685 /* 3686 * fsname is name of dataset to rollback (to most recent snapshot) 3687 * 3688 * innvl may contain name of expected target snapshot 3689 * 3690 * outnvl: "target" -> name of most recent snapshot 3691 * } 3692 */ 3693 /* ARGSUSED */ 3694 static int 3695 zfs_ioc_rollback(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl) 3696 { 3697 zfsvfs_t *zfsvfs; 3698 char *target = NULL; 3699 int error; 3700 3701 (void) nvlist_lookup_string(innvl, "target", &target); 3702 if (target != NULL) { 3703 int fslen = strlen(fsname); 3704 3705 if (strncmp(fsname, target, fslen) != 0) 3706 return (SET_ERROR(EINVAL)); 3707 if (target[fslen] != '@') 3708 return (SET_ERROR(EINVAL)); 3709 } 3710 3711 if (getzfsvfs(fsname, &zfsvfs) == 0) { 3712 dsl_dataset_t *ds; 3713 3714 ds = dmu_objset_ds(zfsvfs->z_os); 3715 error = zfs_suspend_fs(zfsvfs); 3716 if (error == 0) { 3717 int resume_err; 3718 3719 error = dsl_dataset_rollback(fsname, target, zfsvfs, 3720 outnvl); 3721 resume_err = zfs_resume_fs(zfsvfs, ds); 3722 error = error ? error : resume_err; 3723 } 3724 VFS_RELE(zfsvfs->z_vfs); 3725 } else { 3726 error = dsl_dataset_rollback(fsname, target, NULL, outnvl); 3727 } 3728 return (error); 3729 } 3730 3731 static int 3732 recursive_unmount(const char *fsname, void *arg) 3733 { 3734 const char *snapname = arg; 3735 char fullname[ZFS_MAX_DATASET_NAME_LEN]; 3736 3737 (void) snprintf(fullname, sizeof (fullname), "%s@%s", fsname, snapname); 3738 return (zfs_unmount_snap(fullname)); 3739 } 3740 3741 /* 3742 * inputs: 3743 * zc_name old name of dataset 3744 * zc_value new name of dataset 3745 * zc_cookie recursive flag (only valid for snapshots) 3746 * 3747 * outputs: none 3748 */ 3749 static int 3750 zfs_ioc_rename(zfs_cmd_t *zc) 3751 { 3752 boolean_t recursive = zc->zc_cookie & 1; 3753 char *at; 3754 3755 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0'; 3756 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 || 3757 strchr(zc->zc_value, '%')) 3758 return (SET_ERROR(EINVAL)); 3759 3760 at = strchr(zc->zc_name, '@'); 3761 if (at != NULL) { 3762 /* snaps must be in same fs */ 3763 int error; 3764 3765 if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1)) 3766 return (SET_ERROR(EXDEV)); 3767 *at = '\0'; 3768 if (zc->zc_objset_type == DMU_OST_ZFS) { 3769 error = dmu_objset_find(zc->zc_name, 3770 recursive_unmount, at + 1, 3771 recursive ? DS_FIND_CHILDREN : 0); 3772 if (error != 0) { 3773 *at = '@'; 3774 return (error); 3775 } 3776 } 3777 error = dsl_dataset_rename_snapshot(zc->zc_name, 3778 at + 1, strchr(zc->zc_value, '@') + 1, recursive); 3779 *at = '@'; 3780 3781 return (error); 3782 } else { 3783 if (zc->zc_objset_type == DMU_OST_ZVOL) 3784 (void) zvol_remove_minor(zc->zc_name); 3785 return (dsl_dir_rename(zc->zc_name, zc->zc_value)); 3786 } 3787 } 3788 3789 static int 3790 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr) 3791 { 3792 const char *propname = nvpair_name(pair); 3793 boolean_t issnap = (strchr(dsname, '@') != NULL); 3794 zfs_prop_t prop = zfs_name_to_prop(propname); 3795 uint64_t intval; 3796 int err; 3797 3798 if (prop == ZPROP_INVAL) { 3799 if (zfs_prop_user(propname)) { 3800 if (err = zfs_secpolicy_write_perms(dsname, 3801 ZFS_DELEG_PERM_USERPROP, cr)) 3802 return (err); 3803 return (0); 3804 } 3805 3806 if (!issnap && zfs_prop_userquota(propname)) { 3807 const char *perm = NULL; 3808 const char *uq_prefix = 3809 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA]; 3810 const char *gq_prefix = 3811 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA]; 3812 3813 if (strncmp(propname, uq_prefix, 3814 strlen(uq_prefix)) == 0) { 3815 perm = ZFS_DELEG_PERM_USERQUOTA; 3816 } else if (strncmp(propname, gq_prefix, 3817 strlen(gq_prefix)) == 0) { 3818 perm = ZFS_DELEG_PERM_GROUPQUOTA; 3819 } else { 3820 /* USERUSED and GROUPUSED are read-only */ 3821 return (SET_ERROR(EINVAL)); 3822 } 3823 3824 if (err = zfs_secpolicy_write_perms(dsname, perm, cr)) 3825 return (err); 3826 return (0); 3827 } 3828 3829 return (SET_ERROR(EINVAL)); 3830 } 3831 3832 if (issnap) 3833 return (SET_ERROR(EINVAL)); 3834 3835 if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 3836 /* 3837 * dsl_prop_get_all_impl() returns properties in this 3838 * format. 3839 */ 3840 nvlist_t *attrs; 3841 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 3842 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 3843 &pair) == 0); 3844 } 3845 3846 /* 3847 * Check that this value is valid for this pool version 3848 */ 3849 switch (prop) { 3850 case ZFS_PROP_COMPRESSION: 3851 /* 3852 * If the user specified gzip compression, make sure 3853 * the SPA supports it. We ignore any errors here since 3854 * we'll catch them later. 3855 */ 3856 if (nvpair_value_uint64(pair, &intval) == 0) { 3857 if (intval >= ZIO_COMPRESS_GZIP_1 && 3858 intval <= ZIO_COMPRESS_GZIP_9 && 3859 zfs_earlier_version(dsname, 3860 SPA_VERSION_GZIP_COMPRESSION)) { 3861 return (SET_ERROR(ENOTSUP)); 3862 } 3863 3864 if (intval == ZIO_COMPRESS_ZLE && 3865 zfs_earlier_version(dsname, 3866 SPA_VERSION_ZLE_COMPRESSION)) 3867 return (SET_ERROR(ENOTSUP)); 3868 3869 if (intval == ZIO_COMPRESS_LZ4) { 3870 spa_t *spa; 3871 3872 if ((err = spa_open(dsname, &spa, FTAG)) != 0) 3873 return (err); 3874 3875 if (!spa_feature_is_enabled(spa, 3876 SPA_FEATURE_LZ4_COMPRESS)) { 3877 spa_close(spa, FTAG); 3878 return (SET_ERROR(ENOTSUP)); 3879 } 3880 spa_close(spa, FTAG); 3881 } 3882 3883 /* 3884 * If this is a bootable dataset then 3885 * verify that the compression algorithm 3886 * is supported for booting. We must return 3887 * something other than ENOTSUP since it 3888 * implies a downrev pool version. 3889 */ 3890 if (zfs_is_bootfs(dsname) && 3891 !BOOTFS_COMPRESS_VALID(intval)) { 3892 return (SET_ERROR(ERANGE)); 3893 } 3894 } 3895 break; 3896 3897 case ZFS_PROP_COPIES: 3898 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS)) 3899 return (SET_ERROR(ENOTSUP)); 3900 break; 3901 3902 case ZFS_PROP_RECORDSIZE: 3903 /* Record sizes above 128k need the feature to be enabled */ 3904 if (nvpair_value_uint64(pair, &intval) == 0 && 3905 intval > SPA_OLD_MAXBLOCKSIZE) { 3906 spa_t *spa; 3907 3908 /* 3909 * We don't allow setting the property above 1MB, 3910 * unless the tunable has been changed. 3911 */ 3912 if (intval > zfs_max_recordsize || 3913 intval > SPA_MAXBLOCKSIZE) 3914 return (SET_ERROR(ERANGE)); 3915 3916 if ((err = spa_open(dsname, &spa, FTAG)) != 0) 3917 return (err); 3918 3919 if (!spa_feature_is_enabled(spa, 3920 SPA_FEATURE_LARGE_BLOCKS)) { 3921 spa_close(spa, FTAG); 3922 return (SET_ERROR(ENOTSUP)); 3923 } 3924 spa_close(spa, FTAG); 3925 } 3926 break; 3927 3928 case ZFS_PROP_SHARESMB: 3929 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID)) 3930 return (SET_ERROR(ENOTSUP)); 3931 break; 3932 3933 case ZFS_PROP_ACLINHERIT: 3934 if (nvpair_type(pair) == DATA_TYPE_UINT64 && 3935 nvpair_value_uint64(pair, &intval) == 0) { 3936 if (intval == ZFS_ACL_PASSTHROUGH_X && 3937 zfs_earlier_version(dsname, 3938 SPA_VERSION_PASSTHROUGH_X)) 3939 return (SET_ERROR(ENOTSUP)); 3940 } 3941 break; 3942 3943 case ZFS_PROP_CHECKSUM: 3944 case ZFS_PROP_DEDUP: 3945 { 3946 spa_feature_t feature; 3947 spa_t *spa; 3948 3949 /* dedup feature version checks */ 3950 if (prop == ZFS_PROP_DEDUP && 3951 zfs_earlier_version(dsname, SPA_VERSION_DEDUP)) 3952 return (SET_ERROR(ENOTSUP)); 3953 3954 if (nvpair_value_uint64(pair, &intval) != 0) 3955 return (SET_ERROR(EINVAL)); 3956 3957 /* check prop value is enabled in features */ 3958 feature = zio_checksum_to_feature(intval & ZIO_CHECKSUM_MASK); 3959 if (feature == SPA_FEATURE_NONE) 3960 break; 3961 3962 if ((err = spa_open(dsname, &spa, FTAG)) != 0) 3963 return (err); 3964 /* 3965 * Salted checksums are not supported on root pools. 3966 */ 3967 if (spa_bootfs(spa) != 0 && 3968 intval < ZIO_CHECKSUM_FUNCTIONS && 3969 (zio_checksum_table[intval].ci_flags & 3970 ZCHECKSUM_FLAG_SALTED)) { 3971 spa_close(spa, FTAG); 3972 return (SET_ERROR(ERANGE)); 3973 } 3974 if (!spa_feature_is_enabled(spa, feature)) { 3975 spa_close(spa, FTAG); 3976 return (SET_ERROR(ENOTSUP)); 3977 } 3978 spa_close(spa, FTAG); 3979 break; 3980 } 3981 } 3982 3983 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED())); 3984 } 3985 3986 /* 3987 * Checks for a race condition to make sure we don't increment a feature flag 3988 * multiple times. 3989 */ 3990 static int 3991 zfs_prop_activate_feature_check(void *arg, dmu_tx_t *tx) 3992 { 3993 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 3994 spa_feature_t *featurep = arg; 3995 3996 if (!spa_feature_is_active(spa, *featurep)) 3997 return (0); 3998 else 3999 return (SET_ERROR(EBUSY)); 4000 } 4001 4002 /* 4003 * The callback invoked on feature activation in the sync task caused by 4004 * zfs_prop_activate_feature. 4005 */ 4006 static void 4007 zfs_prop_activate_feature_sync(void *arg, dmu_tx_t *tx) 4008 { 4009 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 4010 spa_feature_t *featurep = arg; 4011 4012 spa_feature_incr(spa, *featurep, tx); 4013 } 4014 4015 /* 4016 * Activates a feature on a pool in response to a property setting. This 4017 * creates a new sync task which modifies the pool to reflect the feature 4018 * as being active. 4019 */ 4020 static int 4021 zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature) 4022 { 4023 int err; 4024 4025 /* EBUSY here indicates that the feature is already active */ 4026 err = dsl_sync_task(spa_name(spa), 4027 zfs_prop_activate_feature_check, zfs_prop_activate_feature_sync, 4028 &feature, 2, ZFS_SPACE_CHECK_RESERVED); 4029 4030 if (err != 0 && err != EBUSY) 4031 return (err); 4032 else 4033 return (0); 4034 } 4035 4036 /* 4037 * Removes properties from the given props list that fail permission checks 4038 * needed to clear them and to restore them in case of a receive error. For each 4039 * property, make sure we have both set and inherit permissions. 4040 * 4041 * Returns the first error encountered if any permission checks fail. If the 4042 * caller provides a non-NULL errlist, it also gives the complete list of names 4043 * of all the properties that failed a permission check along with the 4044 * corresponding error numbers. The caller is responsible for freeing the 4045 * returned errlist. 4046 * 4047 * If every property checks out successfully, zero is returned and the list 4048 * pointed at by errlist is NULL. 4049 */ 4050 static int 4051 zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist) 4052 { 4053 zfs_cmd_t *zc; 4054 nvpair_t *pair, *next_pair; 4055 nvlist_t *errors; 4056 int err, rv = 0; 4057 4058 if (props == NULL) 4059 return (0); 4060 4061 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0); 4062 4063 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP); 4064 (void) strcpy(zc->zc_name, dataset); 4065 pair = nvlist_next_nvpair(props, NULL); 4066 while (pair != NULL) { 4067 next_pair = nvlist_next_nvpair(props, pair); 4068 4069 (void) strcpy(zc->zc_value, nvpair_name(pair)); 4070 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 || 4071 (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) { 4072 VERIFY(nvlist_remove_nvpair(props, pair) == 0); 4073 VERIFY(nvlist_add_int32(errors, 4074 zc->zc_value, err) == 0); 4075 } 4076 pair = next_pair; 4077 } 4078 kmem_free(zc, sizeof (zfs_cmd_t)); 4079 4080 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) { 4081 nvlist_free(errors); 4082 errors = NULL; 4083 } else { 4084 VERIFY(nvpair_value_int32(pair, &rv) == 0); 4085 } 4086 4087 if (errlist == NULL) 4088 nvlist_free(errors); 4089 else 4090 *errlist = errors; 4091 4092 return (rv); 4093 } 4094 4095 static boolean_t 4096 propval_equals(nvpair_t *p1, nvpair_t *p2) 4097 { 4098 if (nvpair_type(p1) == DATA_TYPE_NVLIST) { 4099 /* dsl_prop_get_all_impl() format */ 4100 nvlist_t *attrs; 4101 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0); 4102 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 4103 &p1) == 0); 4104 } 4105 4106 if (nvpair_type(p2) == DATA_TYPE_NVLIST) { 4107 nvlist_t *attrs; 4108 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0); 4109 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 4110 &p2) == 0); 4111 } 4112 4113 if (nvpair_type(p1) != nvpair_type(p2)) 4114 return (B_FALSE); 4115 4116 if (nvpair_type(p1) == DATA_TYPE_STRING) { 4117 char *valstr1, *valstr2; 4118 4119 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0); 4120 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0); 4121 return (strcmp(valstr1, valstr2) == 0); 4122 } else { 4123 uint64_t intval1, intval2; 4124 4125 VERIFY(nvpair_value_uint64(p1, &intval1) == 0); 4126 VERIFY(nvpair_value_uint64(p2, &intval2) == 0); 4127 return (intval1 == intval2); 4128 } 4129 } 4130 4131 /* 4132 * Remove properties from props if they are not going to change (as determined 4133 * by comparison with origprops). Remove them from origprops as well, since we 4134 * do not need to clear or restore properties that won't change. 4135 */ 4136 static void 4137 props_reduce(nvlist_t *props, nvlist_t *origprops) 4138 { 4139 nvpair_t *pair, *next_pair; 4140 4141 if (origprops == NULL) 4142 return; /* all props need to be received */ 4143 4144 pair = nvlist_next_nvpair(props, NULL); 4145 while (pair != NULL) { 4146 const char *propname = nvpair_name(pair); 4147 nvpair_t *match; 4148 4149 next_pair = nvlist_next_nvpair(props, pair); 4150 4151 if ((nvlist_lookup_nvpair(origprops, propname, 4152 &match) != 0) || !propval_equals(pair, match)) 4153 goto next; /* need to set received value */ 4154 4155 /* don't clear the existing received value */ 4156 (void) nvlist_remove_nvpair(origprops, match); 4157 /* don't bother receiving the property */ 4158 (void) nvlist_remove_nvpair(props, pair); 4159 next: 4160 pair = next_pair; 4161 } 4162 } 4163 4164 /* 4165 * Extract properties that cannot be set PRIOR to the receipt of a dataset. 4166 * For example, refquota cannot be set until after the receipt of a dataset, 4167 * because in replication streams, an older/earlier snapshot may exceed the 4168 * refquota. We want to receive the older/earlier snapshot, but setting 4169 * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent 4170 * the older/earlier snapshot from being received (with EDQUOT). 4171 * 4172 * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario. 4173 * 4174 * libzfs will need to be judicious handling errors encountered by props 4175 * extracted by this function. 4176 */ 4177 static nvlist_t * 4178 extract_delay_props(nvlist_t *props) 4179 { 4180 nvlist_t *delayprops; 4181 nvpair_t *nvp, *tmp; 4182 static const zfs_prop_t delayable[] = { ZFS_PROP_REFQUOTA, 0 }; 4183 int i; 4184 4185 VERIFY(nvlist_alloc(&delayprops, NV_UNIQUE_NAME, KM_SLEEP) == 0); 4186 4187 for (nvp = nvlist_next_nvpair(props, NULL); nvp != NULL; 4188 nvp = nvlist_next_nvpair(props, nvp)) { 4189 /* 4190 * strcmp() is safe because zfs_prop_to_name() always returns 4191 * a bounded string. 4192 */ 4193 for (i = 0; delayable[i] != 0; i++) { 4194 if (strcmp(zfs_prop_to_name(delayable[i]), 4195 nvpair_name(nvp)) == 0) { 4196 break; 4197 } 4198 } 4199 if (delayable[i] != 0) { 4200 tmp = nvlist_prev_nvpair(props, nvp); 4201 VERIFY(nvlist_add_nvpair(delayprops, nvp) == 0); 4202 VERIFY(nvlist_remove_nvpair(props, nvp) == 0); 4203 nvp = tmp; 4204 } 4205 } 4206 4207 if (nvlist_empty(delayprops)) { 4208 nvlist_free(delayprops); 4209 delayprops = NULL; 4210 } 4211 return (delayprops); 4212 } 4213 4214 #ifdef DEBUG 4215 static boolean_t zfs_ioc_recv_inject_err; 4216 #endif 4217 4218 /* 4219 * inputs: 4220 * zc_name name of containing filesystem 4221 * zc_nvlist_src{_size} nvlist of properties to apply 4222 * zc_value name of snapshot to create 4223 * zc_string name of clone origin (if DRR_FLAG_CLONE) 4224 * zc_cookie file descriptor to recv from 4225 * zc_begin_record the BEGIN record of the stream (not byteswapped) 4226 * zc_guid force flag 4227 * zc_cleanup_fd cleanup-on-exit file descriptor 4228 * zc_action_handle handle for this guid/ds mapping (or zero on first call) 4229 * zc_resumable if data is incomplete assume sender will resume 4230 * 4231 * outputs: 4232 * zc_cookie number of bytes read 4233 * zc_nvlist_dst{_size} error for each unapplied received property 4234 * zc_obj zprop_errflags_t 4235 * zc_action_handle handle for this guid/ds mapping 4236 */ 4237 static int 4238 zfs_ioc_recv(zfs_cmd_t *zc) 4239 { 4240 file_t *fp; 4241 dmu_recv_cookie_t drc; 4242 boolean_t force = (boolean_t)zc->zc_guid; 4243 int fd; 4244 int error = 0; 4245 int props_error = 0; 4246 nvlist_t *errors; 4247 offset_t off; 4248 nvlist_t *props = NULL; /* sent properties */ 4249 nvlist_t *origprops = NULL; /* existing properties */ 4250 nvlist_t *delayprops = NULL; /* sent properties applied post-receive */ 4251 char *origin = NULL; 4252 char *tosnap; 4253 char tofs[ZFS_MAX_DATASET_NAME_LEN]; 4254 boolean_t first_recvd_props = B_FALSE; 4255 4256 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 || 4257 strchr(zc->zc_value, '@') == NULL || 4258 strchr(zc->zc_value, '%')) 4259 return (SET_ERROR(EINVAL)); 4260 4261 (void) strcpy(tofs, zc->zc_value); 4262 tosnap = strchr(tofs, '@'); 4263 *tosnap++ = '\0'; 4264 4265 if (zc->zc_nvlist_src != NULL && 4266 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 4267 zc->zc_iflags, &props)) != 0) 4268 return (error); 4269 4270 fd = zc->zc_cookie; 4271 fp = getf(fd); 4272 if (fp == NULL) { 4273 nvlist_free(props); 4274 return (SET_ERROR(EBADF)); 4275 } 4276 4277 errors = fnvlist_alloc(); 4278 4279 if (zc->zc_string[0]) 4280 origin = zc->zc_string; 4281 4282 error = dmu_recv_begin(tofs, tosnap, 4283 &zc->zc_begin_record, force, zc->zc_resumable, origin, &drc); 4284 if (error != 0) 4285 goto out; 4286 4287 /* 4288 * Set properties before we receive the stream so that they are applied 4289 * to the new data. Note that we must call dmu_recv_stream() if 4290 * dmu_recv_begin() succeeds. 4291 */ 4292 if (props != NULL && !drc.drc_newfs) { 4293 if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >= 4294 SPA_VERSION_RECVD_PROPS && 4295 !dsl_prop_get_hasrecvd(tofs)) 4296 first_recvd_props = B_TRUE; 4297 4298 /* 4299 * If new received properties are supplied, they are to 4300 * completely replace the existing received properties, so stash 4301 * away the existing ones. 4302 */ 4303 if (dsl_prop_get_received(tofs, &origprops) == 0) { 4304 nvlist_t *errlist = NULL; 4305 /* 4306 * Don't bother writing a property if its value won't 4307 * change (and avoid the unnecessary security checks). 4308 * 4309 * The first receive after SPA_VERSION_RECVD_PROPS is a 4310 * special case where we blow away all local properties 4311 * regardless. 4312 */ 4313 if (!first_recvd_props) 4314 props_reduce(props, origprops); 4315 if (zfs_check_clearable(tofs, origprops, &errlist) != 0) 4316 (void) nvlist_merge(errors, errlist, 0); 4317 nvlist_free(errlist); 4318 4319 if (clear_received_props(tofs, origprops, 4320 first_recvd_props ? NULL : props) != 0) 4321 zc->zc_obj |= ZPROP_ERR_NOCLEAR; 4322 } else { 4323 zc->zc_obj |= ZPROP_ERR_NOCLEAR; 4324 } 4325 } 4326 4327 if (props != NULL) { 4328 props_error = dsl_prop_set_hasrecvd(tofs); 4329 4330 if (props_error == 0) { 4331 delayprops = extract_delay_props(props); 4332 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED, 4333 props, errors); 4334 } 4335 } 4336 4337 off = fp->f_offset; 4338 error = dmu_recv_stream(&drc, fp->f_vnode, &off, zc->zc_cleanup_fd, 4339 &zc->zc_action_handle); 4340 4341 if (error == 0) { 4342 zfsvfs_t *zfsvfs = NULL; 4343 4344 if (getzfsvfs(tofs, &zfsvfs) == 0) { 4345 /* online recv */ 4346 dsl_dataset_t *ds; 4347 int end_err; 4348 4349 ds = dmu_objset_ds(zfsvfs->z_os); 4350 error = zfs_suspend_fs(zfsvfs); 4351 /* 4352 * If the suspend fails, then the recv_end will 4353 * likely also fail, and clean up after itself. 4354 */ 4355 end_err = dmu_recv_end(&drc, zfsvfs); 4356 if (error == 0) 4357 error = zfs_resume_fs(zfsvfs, ds); 4358 error = error ? error : end_err; 4359 VFS_RELE(zfsvfs->z_vfs); 4360 } else { 4361 error = dmu_recv_end(&drc, NULL); 4362 } 4363 4364 /* Set delayed properties now, after we're done receiving. */ 4365 if (delayprops != NULL && error == 0) { 4366 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED, 4367 delayprops, errors); 4368 } 4369 } 4370 4371 if (delayprops != NULL) { 4372 /* 4373 * Merge delayed props back in with initial props, in case 4374 * we're DEBUG and zfs_ioc_recv_inject_err is set (which means 4375 * we have to make sure clear_received_props() includes 4376 * the delayed properties). 4377 * 4378 * Since zfs_ioc_recv_inject_err is only in DEBUG kernels, 4379 * using ASSERT() will be just like a VERIFY. 4380 */ 4381 ASSERT(nvlist_merge(props, delayprops, 0) == 0); 4382 nvlist_free(delayprops); 4383 } 4384 4385 /* 4386 * Now that all props, initial and delayed, are set, report the prop 4387 * errors to the caller. 4388 */ 4389 if (zc->zc_nvlist_dst_size != 0 && 4390 (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 || 4391 put_nvlist(zc, errors) != 0)) { 4392 /* 4393 * Caller made zc->zc_nvlist_dst less than the minimum expected 4394 * size or supplied an invalid address. 4395 */ 4396 props_error = SET_ERROR(EINVAL); 4397 } 4398 4399 zc->zc_cookie = off - fp->f_offset; 4400 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0) 4401 fp->f_offset = off; 4402 4403 #ifdef DEBUG 4404 if (zfs_ioc_recv_inject_err) { 4405 zfs_ioc_recv_inject_err = B_FALSE; 4406 error = 1; 4407 } 4408 #endif 4409 /* 4410 * On error, restore the original props. 4411 */ 4412 if (error != 0 && props != NULL && !drc.drc_newfs) { 4413 if (clear_received_props(tofs, props, NULL) != 0) { 4414 /* 4415 * We failed to clear the received properties. 4416 * Since we may have left a $recvd value on the 4417 * system, we can't clear the $hasrecvd flag. 4418 */ 4419 zc->zc_obj |= ZPROP_ERR_NORESTORE; 4420 } else if (first_recvd_props) { 4421 dsl_prop_unset_hasrecvd(tofs); 4422 } 4423 4424 if (origprops == NULL && !drc.drc_newfs) { 4425 /* We failed to stash the original properties. */ 4426 zc->zc_obj |= ZPROP_ERR_NORESTORE; 4427 } 4428 4429 /* 4430 * dsl_props_set() will not convert RECEIVED to LOCAL on or 4431 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL 4432 * explictly if we're restoring local properties cleared in the 4433 * first new-style receive. 4434 */ 4435 if (origprops != NULL && 4436 zfs_set_prop_nvlist(tofs, (first_recvd_props ? 4437 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED), 4438 origprops, NULL) != 0) { 4439 /* 4440 * We stashed the original properties but failed to 4441 * restore them. 4442 */ 4443 zc->zc_obj |= ZPROP_ERR_NORESTORE; 4444 } 4445 } 4446 out: 4447 nvlist_free(props); 4448 nvlist_free(origprops); 4449 nvlist_free(errors); 4450 releasef(fd); 4451 4452 if (error == 0) 4453 error = props_error; 4454 4455 return (error); 4456 } 4457 4458 /* 4459 * inputs: 4460 * zc_name name of snapshot to send 4461 * zc_cookie file descriptor to send stream to 4462 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj) 4463 * zc_sendobj objsetid of snapshot to send 4464 * zc_fromobj objsetid of incremental fromsnap (may be zero) 4465 * zc_guid if set, estimate size of stream only. zc_cookie is ignored. 4466 * output size in zc_objset_type. 4467 * zc_flags lzc_send_flags 4468 * 4469 * outputs: 4470 * zc_objset_type estimated size, if zc_guid is set 4471 */ 4472 static int 4473 zfs_ioc_send(zfs_cmd_t *zc) 4474 { 4475 int error; 4476 offset_t off; 4477 boolean_t estimate = (zc->zc_guid != 0); 4478 boolean_t embedok = (zc->zc_flags & 0x1); 4479 boolean_t large_block_ok = (zc->zc_flags & 0x2); 4480 boolean_t compressok = (zc->zc_flags & 0x4); 4481 4482 if (zc->zc_obj != 0) { 4483 dsl_pool_t *dp; 4484 dsl_dataset_t *tosnap; 4485 4486 error = dsl_pool_hold(zc->zc_name, FTAG, &dp); 4487 if (error != 0) 4488 return (error); 4489 4490 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap); 4491 if (error != 0) { 4492 dsl_pool_rele(dp, FTAG); 4493 return (error); 4494 } 4495 4496 if (dsl_dir_is_clone(tosnap->ds_dir)) 4497 zc->zc_fromobj = 4498 dsl_dir_phys(tosnap->ds_dir)->dd_origin_obj; 4499 dsl_dataset_rele(tosnap, FTAG); 4500 dsl_pool_rele(dp, FTAG); 4501 } 4502 4503 if (estimate) { 4504 dsl_pool_t *dp; 4505 dsl_dataset_t *tosnap; 4506 dsl_dataset_t *fromsnap = NULL; 4507 4508 error = dsl_pool_hold(zc->zc_name, FTAG, &dp); 4509 if (error != 0) 4510 return (error); 4511 4512 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap); 4513 if (error != 0) { 4514 dsl_pool_rele(dp, FTAG); 4515 return (error); 4516 } 4517 4518 if (zc->zc_fromobj != 0) { 4519 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj, 4520 FTAG, &fromsnap); 4521 if (error != 0) { 4522 dsl_dataset_rele(tosnap, FTAG); 4523 dsl_pool_rele(dp, FTAG); 4524 return (error); 4525 } 4526 } 4527 4528 error = dmu_send_estimate(tosnap, fromsnap, compressok, 4529 &zc->zc_objset_type); 4530 4531 if (fromsnap != NULL) 4532 dsl_dataset_rele(fromsnap, FTAG); 4533 dsl_dataset_rele(tosnap, FTAG); 4534 dsl_pool_rele(dp, FTAG); 4535 } else { 4536 file_t *fp = getf(zc->zc_cookie); 4537 if (fp == NULL) 4538 return (SET_ERROR(EBADF)); 4539 4540 off = fp->f_offset; 4541 error = dmu_send_obj(zc->zc_name, zc->zc_sendobj, 4542 zc->zc_fromobj, embedok, large_block_ok, compressok, 4543 zc->zc_cookie, fp->f_vnode, &off); 4544 4545 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0) 4546 fp->f_offset = off; 4547 releasef(zc->zc_cookie); 4548 } 4549 return (error); 4550 } 4551 4552 /* 4553 * inputs: 4554 * zc_name name of snapshot on which to report progress 4555 * zc_cookie file descriptor of send stream 4556 * 4557 * outputs: 4558 * zc_cookie number of bytes written in send stream thus far 4559 */ 4560 static int 4561 zfs_ioc_send_progress(zfs_cmd_t *zc) 4562 { 4563 dsl_pool_t *dp; 4564 dsl_dataset_t *ds; 4565 dmu_sendarg_t *dsp = NULL; 4566 int error; 4567 4568 error = dsl_pool_hold(zc->zc_name, FTAG, &dp); 4569 if (error != 0) 4570 return (error); 4571 4572 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds); 4573 if (error != 0) { 4574 dsl_pool_rele(dp, FTAG); 4575 return (error); 4576 } 4577 4578 mutex_enter(&ds->ds_sendstream_lock); 4579 4580 /* 4581 * Iterate over all the send streams currently active on this dataset. 4582 * If there's one which matches the specified file descriptor _and_ the 4583 * stream was started by the current process, return the progress of 4584 * that stream. 4585 */ 4586 for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL; 4587 dsp = list_next(&ds->ds_sendstreams, dsp)) { 4588 if (dsp->dsa_outfd == zc->zc_cookie && 4589 dsp->dsa_proc == curproc) 4590 break; 4591 } 4592 4593 if (dsp != NULL) 4594 zc->zc_cookie = *(dsp->dsa_off); 4595 else 4596 error = SET_ERROR(ENOENT); 4597 4598 mutex_exit(&ds->ds_sendstream_lock); 4599 dsl_dataset_rele(ds, FTAG); 4600 dsl_pool_rele(dp, FTAG); 4601 return (error); 4602 } 4603 4604 static int 4605 zfs_ioc_inject_fault(zfs_cmd_t *zc) 4606 { 4607 int id, error; 4608 4609 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id, 4610 &zc->zc_inject_record); 4611 4612 if (error == 0) 4613 zc->zc_guid = (uint64_t)id; 4614 4615 return (error); 4616 } 4617 4618 static int 4619 zfs_ioc_clear_fault(zfs_cmd_t *zc) 4620 { 4621 return (zio_clear_fault((int)zc->zc_guid)); 4622 } 4623 4624 static int 4625 zfs_ioc_inject_list_next(zfs_cmd_t *zc) 4626 { 4627 int id = (int)zc->zc_guid; 4628 int error; 4629 4630 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name), 4631 &zc->zc_inject_record); 4632 4633 zc->zc_guid = id; 4634 4635 return (error); 4636 } 4637 4638 static int 4639 zfs_ioc_error_log(zfs_cmd_t *zc) 4640 { 4641 spa_t *spa; 4642 int error; 4643 size_t count = (size_t)zc->zc_nvlist_dst_size; 4644 4645 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 4646 return (error); 4647 4648 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst, 4649 &count); 4650 if (error == 0) 4651 zc->zc_nvlist_dst_size = count; 4652 else 4653 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa); 4654 4655 spa_close(spa, FTAG); 4656 4657 return (error); 4658 } 4659 4660 static int 4661 zfs_ioc_clear(zfs_cmd_t *zc) 4662 { 4663 spa_t *spa; 4664 vdev_t *vd; 4665 int error; 4666 4667 /* 4668 * On zpool clear we also fix up missing slogs 4669 */ 4670 mutex_enter(&spa_namespace_lock); 4671 spa = spa_lookup(zc->zc_name); 4672 if (spa == NULL) { 4673 mutex_exit(&spa_namespace_lock); 4674 return (SET_ERROR(EIO)); 4675 } 4676 if (spa_get_log_state(spa) == SPA_LOG_MISSING) { 4677 /* we need to let spa_open/spa_load clear the chains */ 4678 spa_set_log_state(spa, SPA_LOG_CLEAR); 4679 } 4680 spa->spa_last_open_failed = 0; 4681 mutex_exit(&spa_namespace_lock); 4682 4683 if (zc->zc_cookie & ZPOOL_NO_REWIND) { 4684 error = spa_open(zc->zc_name, &spa, FTAG); 4685 } else { 4686 nvlist_t *policy; 4687 nvlist_t *config = NULL; 4688 4689 if (zc->zc_nvlist_src == NULL) 4690 return (SET_ERROR(EINVAL)); 4691 4692 if ((error = get_nvlist(zc->zc_nvlist_src, 4693 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) { 4694 error = spa_open_rewind(zc->zc_name, &spa, FTAG, 4695 policy, &config); 4696 if (config != NULL) { 4697 int err; 4698 4699 if ((err = put_nvlist(zc, config)) != 0) 4700 error = err; 4701 nvlist_free(config); 4702 } 4703 nvlist_free(policy); 4704 } 4705 } 4706 4707 if (error != 0) 4708 return (error); 4709 4710 spa_vdev_state_enter(spa, SCL_NONE); 4711 4712 if (zc->zc_guid == 0) { 4713 vd = NULL; 4714 } else { 4715 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE); 4716 if (vd == NULL) { 4717 (void) spa_vdev_state_exit(spa, NULL, ENODEV); 4718 spa_close(spa, FTAG); 4719 return (SET_ERROR(ENODEV)); 4720 } 4721 } 4722 4723 vdev_clear(spa, vd); 4724 4725 (void) spa_vdev_state_exit(spa, NULL, 0); 4726 4727 /* 4728 * Resume any suspended I/Os. 4729 */ 4730 if (zio_resume(spa) != 0) 4731 error = SET_ERROR(EIO); 4732 4733 spa_close(spa, FTAG); 4734 4735 return (error); 4736 } 4737 4738 static int 4739 zfs_ioc_pool_reopen(zfs_cmd_t *zc) 4740 { 4741 spa_t *spa; 4742 int error; 4743 4744 error = spa_open(zc->zc_name, &spa, FTAG); 4745 if (error != 0) 4746 return (error); 4747 4748 spa_vdev_state_enter(spa, SCL_NONE); 4749 4750 /* 4751 * If a resilver is already in progress then set the 4752 * spa_scrub_reopen flag to B_TRUE so that we don't restart 4753 * the scan as a side effect of the reopen. Otherwise, let 4754 * vdev_open() decided if a resilver is required. 4755 */ 4756 spa->spa_scrub_reopen = dsl_scan_resilvering(spa->spa_dsl_pool); 4757 vdev_reopen(spa->spa_root_vdev); 4758 spa->spa_scrub_reopen = B_FALSE; 4759 4760 (void) spa_vdev_state_exit(spa, NULL, 0); 4761 spa_close(spa, FTAG); 4762 return (0); 4763 } 4764 /* 4765 * inputs: 4766 * zc_name name of filesystem 4767 * 4768 * outputs: 4769 * zc_string name of conflicting snapshot, if there is one 4770 */ 4771 static int 4772 zfs_ioc_promote(zfs_cmd_t *zc) 4773 { 4774 dsl_pool_t *dp; 4775 dsl_dataset_t *ds, *ods; 4776 char origin[ZFS_MAX_DATASET_NAME_LEN]; 4777 char *cp; 4778 int error; 4779 4780 error = dsl_pool_hold(zc->zc_name, FTAG, &dp); 4781 if (error != 0) 4782 return (error); 4783 4784 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds); 4785 if (error != 0) { 4786 dsl_pool_rele(dp, FTAG); 4787 return (error); 4788 } 4789 4790 if (!dsl_dir_is_clone(ds->ds_dir)) { 4791 dsl_dataset_rele(ds, FTAG); 4792 dsl_pool_rele(dp, FTAG); 4793 return (SET_ERROR(EINVAL)); 4794 } 4795 4796 error = dsl_dataset_hold_obj(dp, 4797 dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &ods); 4798 if (error != 0) { 4799 dsl_dataset_rele(ds, FTAG); 4800 dsl_pool_rele(dp, FTAG); 4801 return (error); 4802 } 4803 4804 dsl_dataset_name(ods, origin); 4805 dsl_dataset_rele(ods, FTAG); 4806 dsl_dataset_rele(ds, FTAG); 4807 dsl_pool_rele(dp, FTAG); 4808 4809 /* 4810 * We don't need to unmount *all* the origin fs's snapshots, but 4811 * it's easier. 4812 */ 4813 cp = strchr(origin, '@'); 4814 if (cp) 4815 *cp = '\0'; 4816 (void) dmu_objset_find(origin, 4817 zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS); 4818 return (dsl_dataset_promote(zc->zc_name, zc->zc_string)); 4819 } 4820 4821 /* 4822 * Retrieve a single {user|group}{used|quota}@... property. 4823 * 4824 * inputs: 4825 * zc_name name of filesystem 4826 * zc_objset_type zfs_userquota_prop_t 4827 * zc_value domain name (eg. "S-1-234-567-89") 4828 * zc_guid RID/UID/GID 4829 * 4830 * outputs: 4831 * zc_cookie property value 4832 */ 4833 static int 4834 zfs_ioc_userspace_one(zfs_cmd_t *zc) 4835 { 4836 zfsvfs_t *zfsvfs; 4837 int error; 4838 4839 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) 4840 return (SET_ERROR(EINVAL)); 4841 4842 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE); 4843 if (error != 0) 4844 return (error); 4845 4846 error = zfs_userspace_one(zfsvfs, 4847 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie); 4848 zfsvfs_rele(zfsvfs, FTAG); 4849 4850 return (error); 4851 } 4852 4853 /* 4854 * inputs: 4855 * zc_name name of filesystem 4856 * zc_cookie zap cursor 4857 * zc_objset_type zfs_userquota_prop_t 4858 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist) 4859 * 4860 * outputs: 4861 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t) 4862 * zc_cookie zap cursor 4863 */ 4864 static int 4865 zfs_ioc_userspace_many(zfs_cmd_t *zc) 4866 { 4867 zfsvfs_t *zfsvfs; 4868 int bufsize = zc->zc_nvlist_dst_size; 4869 4870 if (bufsize <= 0) 4871 return (SET_ERROR(ENOMEM)); 4872 4873 int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE); 4874 if (error != 0) 4875 return (error); 4876 4877 void *buf = kmem_alloc(bufsize, KM_SLEEP); 4878 4879 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie, 4880 buf, &zc->zc_nvlist_dst_size); 4881 4882 if (error == 0) { 4883 error = xcopyout(buf, 4884 (void *)(uintptr_t)zc->zc_nvlist_dst, 4885 zc->zc_nvlist_dst_size); 4886 } 4887 kmem_free(buf, bufsize); 4888 zfsvfs_rele(zfsvfs, FTAG); 4889 4890 return (error); 4891 } 4892 4893 /* 4894 * inputs: 4895 * zc_name name of filesystem 4896 * 4897 * outputs: 4898 * none 4899 */ 4900 static int 4901 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc) 4902 { 4903 objset_t *os; 4904 int error = 0; 4905 zfsvfs_t *zfsvfs; 4906 4907 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) { 4908 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) { 4909 /* 4910 * If userused is not enabled, it may be because the 4911 * objset needs to be closed & reopened (to grow the 4912 * objset_phys_t). Suspend/resume the fs will do that. 4913 */ 4914 dsl_dataset_t *ds; 4915 4916 ds = dmu_objset_ds(zfsvfs->z_os); 4917 error = zfs_suspend_fs(zfsvfs); 4918 if (error == 0) { 4919 dmu_objset_refresh_ownership(zfsvfs->z_os, 4920 zfsvfs); 4921 error = zfs_resume_fs(zfsvfs, ds); 4922 } 4923 } 4924 if (error == 0) 4925 error = dmu_objset_userspace_upgrade(zfsvfs->z_os); 4926 VFS_RELE(zfsvfs->z_vfs); 4927 } else { 4928 /* XXX kind of reading contents without owning */ 4929 error = dmu_objset_hold(zc->zc_name, FTAG, &os); 4930 if (error != 0) 4931 return (error); 4932 4933 error = dmu_objset_userspace_upgrade(os); 4934 dmu_objset_rele(os, FTAG); 4935 } 4936 4937 return (error); 4938 } 4939 4940 /* 4941 * We don't want to have a hard dependency 4942 * against some special symbols in sharefs 4943 * nfs, and smbsrv. Determine them if needed when 4944 * the first file system is shared. 4945 * Neither sharefs, nfs or smbsrv are unloadable modules. 4946 */ 4947 int (*znfsexport_fs)(void *arg); 4948 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t); 4949 int (*zsmbexport_fs)(void *arg, boolean_t add_share); 4950 4951 int zfs_nfsshare_inited; 4952 int zfs_smbshare_inited; 4953 4954 ddi_modhandle_t nfs_mod; 4955 ddi_modhandle_t sharefs_mod; 4956 ddi_modhandle_t smbsrv_mod; 4957 kmutex_t zfs_share_lock; 4958 4959 static int 4960 zfs_init_sharefs() 4961 { 4962 int error; 4963 4964 ASSERT(MUTEX_HELD(&zfs_share_lock)); 4965 /* Both NFS and SMB shares also require sharetab support. */ 4966 if (sharefs_mod == NULL && ((sharefs_mod = 4967 ddi_modopen("fs/sharefs", 4968 KRTLD_MODE_FIRST, &error)) == NULL)) { 4969 return (SET_ERROR(ENOSYS)); 4970 } 4971 if (zshare_fs == NULL && ((zshare_fs = 4972 (int (*)(enum sharefs_sys_op, share_t *, uint32_t)) 4973 ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) { 4974 return (SET_ERROR(ENOSYS)); 4975 } 4976 return (0); 4977 } 4978 4979 static int 4980 zfs_ioc_share(zfs_cmd_t *zc) 4981 { 4982 int error; 4983 int opcode; 4984 4985 switch (zc->zc_share.z_sharetype) { 4986 case ZFS_SHARE_NFS: 4987 case ZFS_UNSHARE_NFS: 4988 if (zfs_nfsshare_inited == 0) { 4989 mutex_enter(&zfs_share_lock); 4990 if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs", 4991 KRTLD_MODE_FIRST, &error)) == NULL)) { 4992 mutex_exit(&zfs_share_lock); 4993 return (SET_ERROR(ENOSYS)); 4994 } 4995 if (znfsexport_fs == NULL && 4996 ((znfsexport_fs = (int (*)(void *)) 4997 ddi_modsym(nfs_mod, 4998 "nfs_export", &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_nfsshare_inited = 1; 5008 mutex_exit(&zfs_share_lock); 5009 } 5010 break; 5011 case ZFS_SHARE_SMB: 5012 case ZFS_UNSHARE_SMB: 5013 if (zfs_smbshare_inited == 0) { 5014 mutex_enter(&zfs_share_lock); 5015 if (smbsrv_mod == NULL && ((smbsrv_mod = 5016 ddi_modopen("drv/smbsrv", 5017 KRTLD_MODE_FIRST, &error)) == NULL)) { 5018 mutex_exit(&zfs_share_lock); 5019 return (SET_ERROR(ENOSYS)); 5020 } 5021 if (zsmbexport_fs == NULL && ((zsmbexport_fs = 5022 (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod, 5023 "smb_server_share", &error)) == NULL)) { 5024 mutex_exit(&zfs_share_lock); 5025 return (SET_ERROR(ENOSYS)); 5026 } 5027 error = zfs_init_sharefs(); 5028 if (error != 0) { 5029 mutex_exit(&zfs_share_lock); 5030 return (SET_ERROR(ENOSYS)); 5031 } 5032 zfs_smbshare_inited = 1; 5033 mutex_exit(&zfs_share_lock); 5034 } 5035 break; 5036 default: 5037 return (SET_ERROR(EINVAL)); 5038 } 5039 5040 switch (zc->zc_share.z_sharetype) { 5041 case ZFS_SHARE_NFS: 5042 case ZFS_UNSHARE_NFS: 5043 if (error = 5044 znfsexport_fs((void *) 5045 (uintptr_t)zc->zc_share.z_exportdata)) 5046 return (error); 5047 break; 5048 case ZFS_SHARE_SMB: 5049 case ZFS_UNSHARE_SMB: 5050 if (error = zsmbexport_fs((void *) 5051 (uintptr_t)zc->zc_share.z_exportdata, 5052 zc->zc_share.z_sharetype == ZFS_SHARE_SMB ? 5053 B_TRUE: B_FALSE)) { 5054 return (error); 5055 } 5056 break; 5057 } 5058 5059 opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS || 5060 zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ? 5061 SHAREFS_ADD : SHAREFS_REMOVE; 5062 5063 /* 5064 * Add or remove share from sharetab 5065 */ 5066 error = zshare_fs(opcode, 5067 (void *)(uintptr_t)zc->zc_share.z_sharedata, 5068 zc->zc_share.z_sharemax); 5069 5070 return (error); 5071 5072 } 5073 5074 ace_t full_access[] = { 5075 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0} 5076 }; 5077 5078 /* 5079 * inputs: 5080 * zc_name name of containing filesystem 5081 * zc_obj object # beyond which we want next in-use object # 5082 * 5083 * outputs: 5084 * zc_obj next in-use object # 5085 */ 5086 static int 5087 zfs_ioc_next_obj(zfs_cmd_t *zc) 5088 { 5089 objset_t *os = NULL; 5090 int error; 5091 5092 error = dmu_objset_hold(zc->zc_name, FTAG, &os); 5093 if (error != 0) 5094 return (error); 5095 5096 error = dmu_object_next(os, &zc->zc_obj, B_FALSE, 5097 dsl_dataset_phys(os->os_dsl_dataset)->ds_prev_snap_txg); 5098 5099 dmu_objset_rele(os, FTAG); 5100 return (error); 5101 } 5102 5103 /* 5104 * inputs: 5105 * zc_name name of filesystem 5106 * zc_value prefix name for snapshot 5107 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process 5108 * 5109 * outputs: 5110 * zc_value short name of new snapshot 5111 */ 5112 static int 5113 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc) 5114 { 5115 char *snap_name; 5116 char *hold_name; 5117 int error; 5118 minor_t minor; 5119 5120 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor); 5121 if (error != 0) 5122 return (error); 5123 5124 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value, 5125 (u_longlong_t)ddi_get_lbolt64()); 5126 hold_name = kmem_asprintf("%%%s", zc->zc_value); 5127 5128 error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor, 5129 hold_name); 5130 if (error == 0) 5131 (void) strcpy(zc->zc_value, snap_name); 5132 strfree(snap_name); 5133 strfree(hold_name); 5134 zfs_onexit_fd_rele(zc->zc_cleanup_fd); 5135 return (error); 5136 } 5137 5138 /* 5139 * inputs: 5140 * zc_name name of "to" snapshot 5141 * zc_value name of "from" snapshot 5142 * zc_cookie file descriptor to write diff data on 5143 * 5144 * outputs: 5145 * dmu_diff_record_t's to the file descriptor 5146 */ 5147 static int 5148 zfs_ioc_diff(zfs_cmd_t *zc) 5149 { 5150 file_t *fp; 5151 offset_t off; 5152 int error; 5153 5154 fp = getf(zc->zc_cookie); 5155 if (fp == NULL) 5156 return (SET_ERROR(EBADF)); 5157 5158 off = fp->f_offset; 5159 5160 error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off); 5161 5162 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0) 5163 fp->f_offset = off; 5164 releasef(zc->zc_cookie); 5165 5166 return (error); 5167 } 5168 5169 /* 5170 * Remove all ACL files in shares dir 5171 */ 5172 static int 5173 zfs_smb_acl_purge(znode_t *dzp) 5174 { 5175 zap_cursor_t zc; 5176 zap_attribute_t zap; 5177 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 5178 int error; 5179 5180 for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id); 5181 (error = zap_cursor_retrieve(&zc, &zap)) == 0; 5182 zap_cursor_advance(&zc)) { 5183 if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred, 5184 NULL, 0)) != 0) 5185 break; 5186 } 5187 zap_cursor_fini(&zc); 5188 return (error); 5189 } 5190 5191 static int 5192 zfs_ioc_smb_acl(zfs_cmd_t *zc) 5193 { 5194 vnode_t *vp; 5195 znode_t *dzp; 5196 vnode_t *resourcevp = NULL; 5197 znode_t *sharedir; 5198 zfsvfs_t *zfsvfs; 5199 nvlist_t *nvlist; 5200 char *src, *target; 5201 vattr_t vattr; 5202 vsecattr_t vsec; 5203 int error = 0; 5204 5205 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE, 5206 NO_FOLLOW, NULL, &vp)) != 0) 5207 return (error); 5208 5209 /* Now make sure mntpnt and dataset are ZFS */ 5210 5211 if (vp->v_vfsp->vfs_fstype != zfsfstype || 5212 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource), 5213 zc->zc_name) != 0)) { 5214 VN_RELE(vp); 5215 return (SET_ERROR(EINVAL)); 5216 } 5217 5218 dzp = VTOZ(vp); 5219 zfsvfs = dzp->z_zfsvfs; 5220 ZFS_ENTER(zfsvfs); 5221 5222 /* 5223 * Create share dir if its missing. 5224 */ 5225 mutex_enter(&zfsvfs->z_lock); 5226 if (zfsvfs->z_shares_dir == 0) { 5227 dmu_tx_t *tx; 5228 5229 tx = dmu_tx_create(zfsvfs->z_os); 5230 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE, 5231 ZFS_SHARES_DIR); 5232 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 5233 error = dmu_tx_assign(tx, TXG_WAIT); 5234 if (error != 0) { 5235 dmu_tx_abort(tx); 5236 } else { 5237 error = zfs_create_share_dir(zfsvfs, tx); 5238 dmu_tx_commit(tx); 5239 } 5240 if (error != 0) { 5241 mutex_exit(&zfsvfs->z_lock); 5242 VN_RELE(vp); 5243 ZFS_EXIT(zfsvfs); 5244 return (error); 5245 } 5246 } 5247 mutex_exit(&zfsvfs->z_lock); 5248 5249 ASSERT(zfsvfs->z_shares_dir); 5250 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) { 5251 VN_RELE(vp); 5252 ZFS_EXIT(zfsvfs); 5253 return (error); 5254 } 5255 5256 switch (zc->zc_cookie) { 5257 case ZFS_SMB_ACL_ADD: 5258 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE; 5259 vattr.va_type = VREG; 5260 vattr.va_mode = S_IFREG|0777; 5261 vattr.va_uid = 0; 5262 vattr.va_gid = 0; 5263 5264 vsec.vsa_mask = VSA_ACE; 5265 vsec.vsa_aclentp = &full_access; 5266 vsec.vsa_aclentsz = sizeof (full_access); 5267 vsec.vsa_aclcnt = 1; 5268 5269 error = VOP_CREATE(ZTOV(sharedir), zc->zc_string, 5270 &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec); 5271 if (resourcevp) 5272 VN_RELE(resourcevp); 5273 break; 5274 5275 case ZFS_SMB_ACL_REMOVE: 5276 error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred, 5277 NULL, 0); 5278 break; 5279 5280 case ZFS_SMB_ACL_RENAME: 5281 if ((error = get_nvlist(zc->zc_nvlist_src, 5282 zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) { 5283 VN_RELE(vp); 5284 VN_RELE(ZTOV(sharedir)); 5285 ZFS_EXIT(zfsvfs); 5286 return (error); 5287 } 5288 if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) || 5289 nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET, 5290 &target)) { 5291 VN_RELE(vp); 5292 VN_RELE(ZTOV(sharedir)); 5293 ZFS_EXIT(zfsvfs); 5294 nvlist_free(nvlist); 5295 return (error); 5296 } 5297 error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target, 5298 kcred, NULL, 0); 5299 nvlist_free(nvlist); 5300 break; 5301 5302 case ZFS_SMB_ACL_PURGE: 5303 error = zfs_smb_acl_purge(sharedir); 5304 break; 5305 5306 default: 5307 error = SET_ERROR(EINVAL); 5308 break; 5309 } 5310 5311 VN_RELE(vp); 5312 VN_RELE(ZTOV(sharedir)); 5313 5314 ZFS_EXIT(zfsvfs); 5315 5316 return (error); 5317 } 5318 5319 /* 5320 * innvl: { 5321 * "holds" -> { snapname -> holdname (string), ... } 5322 * (optional) "cleanup_fd" -> fd (int32) 5323 * } 5324 * 5325 * outnvl: { 5326 * snapname -> error value (int32) 5327 * ... 5328 * } 5329 */ 5330 /* ARGSUSED */ 5331 static int 5332 zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist) 5333 { 5334 nvpair_t *pair; 5335 nvlist_t *holds; 5336 int cleanup_fd = -1; 5337 int error; 5338 minor_t minor = 0; 5339 5340 error = nvlist_lookup_nvlist(args, "holds", &holds); 5341 if (error != 0) 5342 return (SET_ERROR(EINVAL)); 5343 5344 /* make sure the user didn't pass us any invalid (empty) tags */ 5345 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL; 5346 pair = nvlist_next_nvpair(holds, pair)) { 5347 char *htag; 5348 5349 error = nvpair_value_string(pair, &htag); 5350 if (error != 0) 5351 return (SET_ERROR(error)); 5352 5353 if (strlen(htag) == 0) 5354 return (SET_ERROR(EINVAL)); 5355 } 5356 5357 if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) { 5358 error = zfs_onexit_fd_hold(cleanup_fd, &minor); 5359 if (error != 0) 5360 return (error); 5361 } 5362 5363 error = dsl_dataset_user_hold(holds, minor, errlist); 5364 if (minor != 0) 5365 zfs_onexit_fd_rele(cleanup_fd); 5366 return (error); 5367 } 5368 5369 /* 5370 * innvl is not used. 5371 * 5372 * outnvl: { 5373 * holdname -> time added (uint64 seconds since epoch) 5374 * ... 5375 * } 5376 */ 5377 /* ARGSUSED */ 5378 static int 5379 zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl) 5380 { 5381 return (dsl_dataset_get_holds(snapname, outnvl)); 5382 } 5383 5384 /* 5385 * innvl: { 5386 * snapname -> { holdname, ... } 5387 * ... 5388 * } 5389 * 5390 * outnvl: { 5391 * snapname -> error value (int32) 5392 * ... 5393 * } 5394 */ 5395 /* ARGSUSED */ 5396 static int 5397 zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist) 5398 { 5399 return (dsl_dataset_user_release(holds, errlist)); 5400 } 5401 5402 /* 5403 * inputs: 5404 * zc_name name of new filesystem or snapshot 5405 * zc_value full name of old snapshot 5406 * 5407 * outputs: 5408 * zc_cookie space in bytes 5409 * zc_objset_type compressed space in bytes 5410 * zc_perm_action uncompressed space in bytes 5411 */ 5412 static int 5413 zfs_ioc_space_written(zfs_cmd_t *zc) 5414 { 5415 int error; 5416 dsl_pool_t *dp; 5417 dsl_dataset_t *new, *old; 5418 5419 error = dsl_pool_hold(zc->zc_name, FTAG, &dp); 5420 if (error != 0) 5421 return (error); 5422 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new); 5423 if (error != 0) { 5424 dsl_pool_rele(dp, FTAG); 5425 return (error); 5426 } 5427 error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old); 5428 if (error != 0) { 5429 dsl_dataset_rele(new, FTAG); 5430 dsl_pool_rele(dp, FTAG); 5431 return (error); 5432 } 5433 5434 error = dsl_dataset_space_written(old, new, &zc->zc_cookie, 5435 &zc->zc_objset_type, &zc->zc_perm_action); 5436 dsl_dataset_rele(old, FTAG); 5437 dsl_dataset_rele(new, FTAG); 5438 dsl_pool_rele(dp, FTAG); 5439 return (error); 5440 } 5441 5442 /* 5443 * innvl: { 5444 * "firstsnap" -> snapshot name 5445 * } 5446 * 5447 * outnvl: { 5448 * "used" -> space in bytes 5449 * "compressed" -> compressed space in bytes 5450 * "uncompressed" -> uncompressed space in bytes 5451 * } 5452 */ 5453 static int 5454 zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl) 5455 { 5456 int error; 5457 dsl_pool_t *dp; 5458 dsl_dataset_t *new, *old; 5459 char *firstsnap; 5460 uint64_t used, comp, uncomp; 5461 5462 if (nvlist_lookup_string(innvl, "firstsnap", &firstsnap) != 0) 5463 return (SET_ERROR(EINVAL)); 5464 5465 error = dsl_pool_hold(lastsnap, FTAG, &dp); 5466 if (error != 0) 5467 return (error); 5468 5469 error = dsl_dataset_hold(dp, lastsnap, FTAG, &new); 5470 if (error == 0 && !new->ds_is_snapshot) { 5471 dsl_dataset_rele(new, FTAG); 5472 error = SET_ERROR(EINVAL); 5473 } 5474 if (error != 0) { 5475 dsl_pool_rele(dp, FTAG); 5476 return (error); 5477 } 5478 error = dsl_dataset_hold(dp, firstsnap, FTAG, &old); 5479 if (error == 0 && !old->ds_is_snapshot) { 5480 dsl_dataset_rele(old, FTAG); 5481 error = SET_ERROR(EINVAL); 5482 } 5483 if (error != 0) { 5484 dsl_dataset_rele(new, FTAG); 5485 dsl_pool_rele(dp, FTAG); 5486 return (error); 5487 } 5488 5489 error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp); 5490 dsl_dataset_rele(old, FTAG); 5491 dsl_dataset_rele(new, FTAG); 5492 dsl_pool_rele(dp, FTAG); 5493 fnvlist_add_uint64(outnvl, "used", used); 5494 fnvlist_add_uint64(outnvl, "compressed", comp); 5495 fnvlist_add_uint64(outnvl, "uncompressed", uncomp); 5496 return (error); 5497 } 5498 5499 /* 5500 * innvl: { 5501 * "fd" -> file descriptor to write stream to (int32) 5502 * (optional) "fromsnap" -> full snap name to send an incremental from 5503 * (optional) "largeblockok" -> (value ignored) 5504 * indicates that blocks > 128KB are permitted 5505 * (optional) "embedok" -> (value ignored) 5506 * presence indicates DRR_WRITE_EMBEDDED records are permitted 5507 * (optional) "compressok" -> (value ignored) 5508 * presence indicates compressed DRR_WRITE records are permitted 5509 * (optional) "resume_object" and "resume_offset" -> (uint64) 5510 * if present, resume send stream from specified object and offset. 5511 * } 5512 * 5513 * outnvl is unused 5514 */ 5515 /* ARGSUSED */ 5516 static int 5517 zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl) 5518 { 5519 int error; 5520 offset_t off; 5521 char *fromname = NULL; 5522 int fd; 5523 boolean_t largeblockok; 5524 boolean_t embedok; 5525 boolean_t compressok; 5526 uint64_t resumeobj = 0; 5527 uint64_t resumeoff = 0; 5528 5529 error = nvlist_lookup_int32(innvl, "fd", &fd); 5530 if (error != 0) 5531 return (SET_ERROR(EINVAL)); 5532 5533 (void) nvlist_lookup_string(innvl, "fromsnap", &fromname); 5534 5535 largeblockok = nvlist_exists(innvl, "largeblockok"); 5536 embedok = nvlist_exists(innvl, "embedok"); 5537 compressok = nvlist_exists(innvl, "compressok"); 5538 5539 (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj); 5540 (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff); 5541 5542 file_t *fp = getf(fd); 5543 if (fp == NULL) 5544 return (SET_ERROR(EBADF)); 5545 5546 off = fp->f_offset; 5547 error = dmu_send(snapname, fromname, embedok, largeblockok, compressok, 5548 fd, resumeobj, resumeoff, fp->f_vnode, &off); 5549 5550 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0) 5551 fp->f_offset = off; 5552 releasef(fd); 5553 return (error); 5554 } 5555 5556 /* 5557 * Determine approximately how large a zfs send stream will be -- the number 5558 * of bytes that will be written to the fd supplied to zfs_ioc_send_new(). 5559 * 5560 * innvl: { 5561 * (optional) "from" -> full snap or bookmark name to send an incremental 5562 * from 5563 * (optional) "largeblockok" -> (value ignored) 5564 * indicates that blocks > 128KB are permitted 5565 * (optional) "embedok" -> (value ignored) 5566 * presence indicates DRR_WRITE_EMBEDDED records are permitted 5567 * (optional) "compressok" -> (value ignored) 5568 * presence indicates compressed DRR_WRITE records are permitted 5569 * } 5570 * 5571 * outnvl: { 5572 * "space" -> bytes of space (uint64) 5573 * } 5574 */ 5575 static int 5576 zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl) 5577 { 5578 dsl_pool_t *dp; 5579 dsl_dataset_t *tosnap; 5580 int error; 5581 char *fromname; 5582 /* LINTED E_FUNC_SET_NOT_USED */ 5583 boolean_t largeblockok; 5584 /* LINTED E_FUNC_SET_NOT_USED */ 5585 boolean_t embedok; 5586 boolean_t compressok; 5587 uint64_t space; 5588 5589 error = dsl_pool_hold(snapname, FTAG, &dp); 5590 if (error != 0) 5591 return (error); 5592 5593 error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap); 5594 if (error != 0) { 5595 dsl_pool_rele(dp, FTAG); 5596 return (error); 5597 } 5598 5599 largeblockok = nvlist_exists(innvl, "largeblockok"); 5600 embedok = nvlist_exists(innvl, "embedok"); 5601 compressok = nvlist_exists(innvl, "compressok"); 5602 5603 error = nvlist_lookup_string(innvl, "from", &fromname); 5604 if (error == 0) { 5605 if (strchr(fromname, '@') != NULL) { 5606 /* 5607 * If from is a snapshot, hold it and use the more 5608 * efficient dmu_send_estimate to estimate send space 5609 * size using deadlists. 5610 */ 5611 dsl_dataset_t *fromsnap; 5612 error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap); 5613 if (error != 0) 5614 goto out; 5615 error = dmu_send_estimate(tosnap, fromsnap, compressok, 5616 &space); 5617 dsl_dataset_rele(fromsnap, FTAG); 5618 } else if (strchr(fromname, '#') != NULL) { 5619 /* 5620 * If from is a bookmark, fetch the creation TXG of the 5621 * snapshot it was created from and use that to find 5622 * blocks that were born after it. 5623 */ 5624 zfs_bookmark_phys_t frombm; 5625 5626 error = dsl_bookmark_lookup(dp, fromname, tosnap, 5627 &frombm); 5628 if (error != 0) 5629 goto out; 5630 error = dmu_send_estimate_from_txg(tosnap, 5631 frombm.zbm_creation_txg, compressok, &space); 5632 } else { 5633 /* 5634 * from is not properly formatted as a snapshot or 5635 * bookmark 5636 */ 5637 error = SET_ERROR(EINVAL); 5638 goto out; 5639 } 5640 } else { 5641 // If estimating the size of a full send, use dmu_send_estimate 5642 error = dmu_send_estimate(tosnap, NULL, compressok, &space); 5643 } 5644 5645 fnvlist_add_uint64(outnvl, "space", space); 5646 5647 out: 5648 dsl_dataset_rele(tosnap, FTAG); 5649 dsl_pool_rele(dp, FTAG); 5650 return (error); 5651 } 5652 5653 static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST]; 5654 5655 static void 5656 zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func, 5657 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck, 5658 boolean_t log_history, zfs_ioc_poolcheck_t pool_check) 5659 { 5660 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST]; 5661 5662 ASSERT3U(ioc, >=, ZFS_IOC_FIRST); 5663 ASSERT3U(ioc, <, ZFS_IOC_LAST); 5664 ASSERT3P(vec->zvec_legacy_func, ==, NULL); 5665 ASSERT3P(vec->zvec_func, ==, NULL); 5666 5667 vec->zvec_legacy_func = func; 5668 vec->zvec_secpolicy = secpolicy; 5669 vec->zvec_namecheck = namecheck; 5670 vec->zvec_allow_log = log_history; 5671 vec->zvec_pool_check = pool_check; 5672 } 5673 5674 /* 5675 * See the block comment at the beginning of this file for details on 5676 * each argument to this function. 5677 */ 5678 static void 5679 zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func, 5680 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck, 5681 zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist, 5682 boolean_t allow_log) 5683 { 5684 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST]; 5685 5686 ASSERT3U(ioc, >=, ZFS_IOC_FIRST); 5687 ASSERT3U(ioc, <, ZFS_IOC_LAST); 5688 ASSERT3P(vec->zvec_legacy_func, ==, NULL); 5689 ASSERT3P(vec->zvec_func, ==, NULL); 5690 5691 /* if we are logging, the name must be valid */ 5692 ASSERT(!allow_log || namecheck != NO_NAME); 5693 5694 vec->zvec_name = name; 5695 vec->zvec_func = func; 5696 vec->zvec_secpolicy = secpolicy; 5697 vec->zvec_namecheck = namecheck; 5698 vec->zvec_pool_check = pool_check; 5699 vec->zvec_smush_outnvlist = smush_outnvlist; 5700 vec->zvec_allow_log = allow_log; 5701 } 5702 5703 static void 5704 zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func, 5705 zfs_secpolicy_func_t *secpolicy, boolean_t log_history, 5706 zfs_ioc_poolcheck_t pool_check) 5707 { 5708 zfs_ioctl_register_legacy(ioc, func, secpolicy, 5709 POOL_NAME, log_history, pool_check); 5710 } 5711 5712 static void 5713 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func, 5714 zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check) 5715 { 5716 zfs_ioctl_register_legacy(ioc, func, secpolicy, 5717 DATASET_NAME, B_FALSE, pool_check); 5718 } 5719 5720 static void 5721 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func) 5722 { 5723 zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config, 5724 POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY); 5725 } 5726 5727 static void 5728 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func, 5729 zfs_secpolicy_func_t *secpolicy) 5730 { 5731 zfs_ioctl_register_legacy(ioc, func, secpolicy, 5732 NO_NAME, B_FALSE, POOL_CHECK_NONE); 5733 } 5734 5735 static void 5736 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc, 5737 zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy) 5738 { 5739 zfs_ioctl_register_legacy(ioc, func, secpolicy, 5740 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED); 5741 } 5742 5743 static void 5744 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func) 5745 { 5746 zfs_ioctl_register_dataset_read_secpolicy(ioc, func, 5747 zfs_secpolicy_read); 5748 } 5749 5750 static void 5751 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func, 5752 zfs_secpolicy_func_t *secpolicy) 5753 { 5754 zfs_ioctl_register_legacy(ioc, func, secpolicy, 5755 DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY); 5756 } 5757 5758 static void 5759 zfs_ioctl_init(void) 5760 { 5761 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT, 5762 zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME, 5763 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE); 5764 5765 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY, 5766 zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME, 5767 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE); 5768 5769 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS, 5770 zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME, 5771 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE); 5772 5773 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW, 5774 zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME, 5775 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE); 5776 5777 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE, 5778 zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME, 5779 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE); 5780 5781 zfs_ioctl_register("create", ZFS_IOC_CREATE, 5782 zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME, 5783 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE); 5784 5785 zfs_ioctl_register("clone", ZFS_IOC_CLONE, 5786 zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME, 5787 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE); 5788 5789 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS, 5790 zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME, 5791 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE); 5792 5793 zfs_ioctl_register("hold", ZFS_IOC_HOLD, 5794 zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME, 5795 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE); 5796 zfs_ioctl_register("release", ZFS_IOC_RELEASE, 5797 zfs_ioc_release, zfs_secpolicy_release, POOL_NAME, 5798 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE); 5799 5800 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS, 5801 zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, 5802 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE); 5803 5804 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK, 5805 zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, 5806 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE); 5807 5808 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK, 5809 zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME, 5810 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE); 5811 5812 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS, 5813 zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME, 5814 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE); 5815 5816 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS, 5817 zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks, 5818 POOL_NAME, 5819 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE); 5820 5821 zfs_ioctl_register("channel_program", ZFS_IOC_CHANNEL_PROGRAM, 5822 zfs_ioc_channel_program, zfs_secpolicy_config, 5823 POOL_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, 5824 B_TRUE); 5825 5826 /* IOCTLS that use the legacy function signature */ 5827 5828 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze, 5829 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY); 5830 5831 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create, 5832 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE); 5833 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN, 5834 zfs_ioc_pool_scan); 5835 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE, 5836 zfs_ioc_pool_upgrade); 5837 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD, 5838 zfs_ioc_vdev_add); 5839 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE, 5840 zfs_ioc_vdev_remove); 5841 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE, 5842 zfs_ioc_vdev_set_state); 5843 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH, 5844 zfs_ioc_vdev_attach); 5845 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH, 5846 zfs_ioc_vdev_detach); 5847 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH, 5848 zfs_ioc_vdev_setpath); 5849 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU, 5850 zfs_ioc_vdev_setfru); 5851 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS, 5852 zfs_ioc_pool_set_props); 5853 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT, 5854 zfs_ioc_vdev_split); 5855 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID, 5856 zfs_ioc_pool_reguid); 5857 5858 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS, 5859 zfs_ioc_pool_configs, zfs_secpolicy_none); 5860 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT, 5861 zfs_ioc_pool_tryimport, zfs_secpolicy_config); 5862 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT, 5863 zfs_ioc_inject_fault, zfs_secpolicy_inject); 5864 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT, 5865 zfs_ioc_clear_fault, zfs_secpolicy_inject); 5866 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT, 5867 zfs_ioc_inject_list_next, zfs_secpolicy_inject); 5868 5869 /* 5870 * pool destroy, and export don't log the history as part of 5871 * zfsdev_ioctl, but rather zfs_ioc_pool_export 5872 * does the logging of those commands. 5873 */ 5874 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy, 5875 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE); 5876 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export, 5877 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE); 5878 5879 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats, 5880 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE); 5881 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props, 5882 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE); 5883 5884 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log, 5885 zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED); 5886 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME, 5887 zfs_ioc_dsobj_to_dsname, 5888 zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED); 5889 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY, 5890 zfs_ioc_pool_get_history, 5891 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED); 5892 5893 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import, 5894 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE); 5895 5896 zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear, 5897 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE); 5898 zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen, 5899 zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED); 5900 5901 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN, 5902 zfs_ioc_space_written); 5903 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS, 5904 zfs_ioc_objset_recvd_props); 5905 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ, 5906 zfs_ioc_next_obj); 5907 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL, 5908 zfs_ioc_get_fsacl); 5909 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS, 5910 zfs_ioc_objset_stats); 5911 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS, 5912 zfs_ioc_objset_zplprops); 5913 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT, 5914 zfs_ioc_dataset_list_next); 5915 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT, 5916 zfs_ioc_snapshot_list_next); 5917 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS, 5918 zfs_ioc_send_progress); 5919 5920 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF, 5921 zfs_ioc_diff, zfs_secpolicy_diff); 5922 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS, 5923 zfs_ioc_obj_to_stats, zfs_secpolicy_diff); 5924 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH, 5925 zfs_ioc_obj_to_path, zfs_secpolicy_diff); 5926 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE, 5927 zfs_ioc_userspace_one, zfs_secpolicy_userspace_one); 5928 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY, 5929 zfs_ioc_userspace_many, zfs_secpolicy_userspace_many); 5930 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND, 5931 zfs_ioc_send, zfs_secpolicy_send); 5932 5933 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop, 5934 zfs_secpolicy_none); 5935 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy, 5936 zfs_secpolicy_destroy); 5937 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename, 5938 zfs_secpolicy_rename); 5939 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv, 5940 zfs_secpolicy_recv); 5941 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote, 5942 zfs_secpolicy_promote); 5943 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP, 5944 zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop); 5945 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl, 5946 zfs_secpolicy_set_fsacl); 5947 5948 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share, 5949 zfs_secpolicy_share, POOL_CHECK_NONE); 5950 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl, 5951 zfs_secpolicy_smb_acl, POOL_CHECK_NONE); 5952 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE, 5953 zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade, 5954 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY); 5955 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT, 5956 zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot, 5957 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY); 5958 } 5959 5960 int 5961 pool_status_check(const char *name, zfs_ioc_namecheck_t type, 5962 zfs_ioc_poolcheck_t check) 5963 { 5964 spa_t *spa; 5965 int error; 5966 5967 ASSERT(type == POOL_NAME || type == DATASET_NAME); 5968 5969 if (check & POOL_CHECK_NONE) 5970 return (0); 5971 5972 error = spa_open(name, &spa, FTAG); 5973 if (error == 0) { 5974 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa)) 5975 error = SET_ERROR(EAGAIN); 5976 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa)) 5977 error = SET_ERROR(EROFS); 5978 spa_close(spa, FTAG); 5979 } 5980 return (error); 5981 } 5982 5983 /* 5984 * Find a free minor number. 5985 */ 5986 minor_t 5987 zfsdev_minor_alloc(void) 5988 { 5989 static minor_t last_minor; 5990 minor_t m; 5991 5992 ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 5993 5994 for (m = last_minor + 1; m != last_minor; m++) { 5995 if (m > ZFSDEV_MAX_MINOR) 5996 m = 1; 5997 if (ddi_get_soft_state(zfsdev_state, m) == NULL) { 5998 last_minor = m; 5999 return (m); 6000 } 6001 } 6002 6003 return (0); 6004 } 6005 6006 static int 6007 zfs_ctldev_init(dev_t *devp) 6008 { 6009 minor_t minor; 6010 zfs_soft_state_t *zs; 6011 6012 ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 6013 ASSERT(getminor(*devp) == 0); 6014 6015 minor = zfsdev_minor_alloc(); 6016 if (minor == 0) 6017 return (SET_ERROR(ENXIO)); 6018 6019 if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS) 6020 return (SET_ERROR(EAGAIN)); 6021 6022 *devp = makedevice(getemajor(*devp), minor); 6023 6024 zs = ddi_get_soft_state(zfsdev_state, minor); 6025 zs->zss_type = ZSST_CTLDEV; 6026 zfs_onexit_init((zfs_onexit_t **)&zs->zss_data); 6027 6028 return (0); 6029 } 6030 6031 static void 6032 zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor) 6033 { 6034 ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 6035 6036 zfs_onexit_destroy(zo); 6037 ddi_soft_state_free(zfsdev_state, minor); 6038 } 6039 6040 void * 6041 zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which) 6042 { 6043 zfs_soft_state_t *zp; 6044 6045 zp = ddi_get_soft_state(zfsdev_state, minor); 6046 if (zp == NULL || zp->zss_type != which) 6047 return (NULL); 6048 6049 return (zp->zss_data); 6050 } 6051 6052 static int 6053 zfsdev_open(dev_t *devp, int flag, int otyp, cred_t *cr) 6054 { 6055 int error = 0; 6056 6057 if (getminor(*devp) != 0) 6058 return (zvol_open(devp, flag, otyp, cr)); 6059 6060 /* This is the control device. Allocate a new minor if requested. */ 6061 if (flag & FEXCL) { 6062 mutex_enter(&zfsdev_state_lock); 6063 error = zfs_ctldev_init(devp); 6064 mutex_exit(&zfsdev_state_lock); 6065 } 6066 6067 return (error); 6068 } 6069 6070 static int 6071 zfsdev_close(dev_t dev, int flag, int otyp, cred_t *cr) 6072 { 6073 zfs_onexit_t *zo; 6074 minor_t minor = getminor(dev); 6075 6076 if (minor == 0) 6077 return (0); 6078 6079 mutex_enter(&zfsdev_state_lock); 6080 zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV); 6081 if (zo == NULL) { 6082 mutex_exit(&zfsdev_state_lock); 6083 return (zvol_close(dev, flag, otyp, cr)); 6084 } 6085 zfs_ctldev_destroy(zo, minor); 6086 mutex_exit(&zfsdev_state_lock); 6087 6088 return (0); 6089 } 6090 6091 static int 6092 zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 6093 { 6094 zfs_cmd_t *zc; 6095 uint_t vecnum; 6096 int error, rc, len; 6097 minor_t minor = getminor(dev); 6098 const zfs_ioc_vec_t *vec; 6099 char *saved_poolname = NULL; 6100 nvlist_t *innvl = NULL; 6101 6102 if (minor != 0 && 6103 zfsdev_get_soft_state(minor, ZSST_CTLDEV) == NULL) 6104 return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp)); 6105 6106 vecnum = cmd - ZFS_IOC_FIRST; 6107 ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip)); 6108 6109 if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0])) 6110 return (SET_ERROR(EINVAL)); 6111 vec = &zfs_ioc_vec[vecnum]; 6112 6113 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP); 6114 6115 error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag); 6116 if (error != 0) { 6117 error = SET_ERROR(EFAULT); 6118 goto out; 6119 } 6120 6121 zc->zc_iflags = flag & FKIOCTL; 6122 if (zc->zc_nvlist_src_size != 0) { 6123 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 6124 zc->zc_iflags, &innvl); 6125 if (error != 0) 6126 goto out; 6127 } 6128 6129 /* 6130 * Ensure that all pool/dataset names are valid before we pass down to 6131 * the lower layers. 6132 */ 6133 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0'; 6134 switch (vec->zvec_namecheck) { 6135 case POOL_NAME: 6136 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0) 6137 error = SET_ERROR(EINVAL); 6138 else 6139 error = pool_status_check(zc->zc_name, 6140 vec->zvec_namecheck, vec->zvec_pool_check); 6141 break; 6142 6143 case DATASET_NAME: 6144 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0) 6145 error = SET_ERROR(EINVAL); 6146 else 6147 error = pool_status_check(zc->zc_name, 6148 vec->zvec_namecheck, vec->zvec_pool_check); 6149 break; 6150 6151 case NO_NAME: 6152 break; 6153 } 6154 6155 6156 if (error == 0) 6157 error = vec->zvec_secpolicy(zc, innvl, cr); 6158 6159 if (error != 0) 6160 goto out; 6161 6162 /* legacy ioctls can modify zc_name */ 6163 len = strcspn(zc->zc_name, "/@#") + 1; 6164 saved_poolname = kmem_alloc(len, KM_SLEEP); 6165 (void) strlcpy(saved_poolname, zc->zc_name, len); 6166 6167 if (vec->zvec_func != NULL) { 6168 nvlist_t *outnvl; 6169 int puterror = 0; 6170 spa_t *spa; 6171 nvlist_t *lognv = NULL; 6172 6173 ASSERT(vec->zvec_legacy_func == NULL); 6174 6175 /* 6176 * Add the innvl to the lognv before calling the func, 6177 * in case the func changes the innvl. 6178 */ 6179 if (vec->zvec_allow_log) { 6180 lognv = fnvlist_alloc(); 6181 fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL, 6182 vec->zvec_name); 6183 if (!nvlist_empty(innvl)) { 6184 fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL, 6185 innvl); 6186 } 6187 } 6188 6189 outnvl = fnvlist_alloc(); 6190 error = vec->zvec_func(zc->zc_name, innvl, outnvl); 6191 6192 /* 6193 * Some commands can partially execute, modfiy state, and still 6194 * return an error. In these cases, attempt to record what 6195 * was modified. 6196 */ 6197 if ((error == 0 || 6198 (cmd == ZFS_IOC_CHANNEL_PROGRAM && error != EINVAL)) && 6199 vec->zvec_allow_log && 6200 spa_open(zc->zc_name, &spa, FTAG) == 0) { 6201 if (!nvlist_empty(outnvl)) { 6202 fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL, 6203 outnvl); 6204 } 6205 if (error != 0) { 6206 fnvlist_add_int64(lognv, ZPOOL_HIST_ERRNO, 6207 error); 6208 } 6209 (void) spa_history_log_nvl(spa, lognv); 6210 spa_close(spa, FTAG); 6211 } 6212 fnvlist_free(lognv); 6213 6214 if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) { 6215 int smusherror = 0; 6216 if (vec->zvec_smush_outnvlist) { 6217 smusherror = nvlist_smush(outnvl, 6218 zc->zc_nvlist_dst_size); 6219 } 6220 if (smusherror == 0) 6221 puterror = put_nvlist(zc, outnvl); 6222 } 6223 6224 if (puterror != 0) 6225 error = puterror; 6226 6227 nvlist_free(outnvl); 6228 } else { 6229 error = vec->zvec_legacy_func(zc); 6230 } 6231 6232 out: 6233 nvlist_free(innvl); 6234 rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag); 6235 if (error == 0 && rc != 0) 6236 error = SET_ERROR(EFAULT); 6237 if (error == 0 && vec->zvec_allow_log) { 6238 char *s = tsd_get(zfs_allow_log_key); 6239 if (s != NULL) 6240 strfree(s); 6241 (void) tsd_set(zfs_allow_log_key, saved_poolname); 6242 } else { 6243 if (saved_poolname != NULL) 6244 strfree(saved_poolname); 6245 } 6246 6247 kmem_free(zc, sizeof (zfs_cmd_t)); 6248 return (error); 6249 } 6250 6251 static int 6252 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 6253 { 6254 if (cmd != DDI_ATTACH) 6255 return (DDI_FAILURE); 6256 6257 if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0, 6258 DDI_PSEUDO, 0) == DDI_FAILURE) 6259 return (DDI_FAILURE); 6260 6261 zfs_dip = dip; 6262 6263 ddi_report_dev(dip); 6264 6265 return (DDI_SUCCESS); 6266 } 6267 6268 static int 6269 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 6270 { 6271 if (spa_busy() || zfs_busy() || zvol_busy()) 6272 return (DDI_FAILURE); 6273 6274 if (cmd != DDI_DETACH) 6275 return (DDI_FAILURE); 6276 6277 zfs_dip = NULL; 6278 6279 ddi_prop_remove_all(dip); 6280 ddi_remove_minor_node(dip, NULL); 6281 6282 return (DDI_SUCCESS); 6283 } 6284 6285 /*ARGSUSED*/ 6286 static int 6287 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 6288 { 6289 switch (infocmd) { 6290 case DDI_INFO_DEVT2DEVINFO: 6291 *result = zfs_dip; 6292 return (DDI_SUCCESS); 6293 6294 case DDI_INFO_DEVT2INSTANCE: 6295 *result = (void *)0; 6296 return (DDI_SUCCESS); 6297 } 6298 6299 return (DDI_FAILURE); 6300 } 6301 6302 /* 6303 * OK, so this is a little weird. 6304 * 6305 * /dev/zfs is the control node, i.e. minor 0. 6306 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0. 6307 * 6308 * /dev/zfs has basically nothing to do except serve up ioctls, 6309 * so most of the standard driver entry points are in zvol.c. 6310 */ 6311 static struct cb_ops zfs_cb_ops = { 6312 zfsdev_open, /* open */ 6313 zfsdev_close, /* close */ 6314 zvol_strategy, /* strategy */ 6315 nodev, /* print */ 6316 zvol_dump, /* dump */ 6317 zvol_read, /* read */ 6318 zvol_write, /* write */ 6319 zfsdev_ioctl, /* ioctl */ 6320 nodev, /* devmap */ 6321 nodev, /* mmap */ 6322 nodev, /* segmap */ 6323 nochpoll, /* poll */ 6324 ddi_prop_op, /* prop_op */ 6325 NULL, /* streamtab */ 6326 D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */ 6327 CB_REV, /* version */ 6328 nodev, /* async read */ 6329 nodev, /* async write */ 6330 }; 6331 6332 static struct dev_ops zfs_dev_ops = { 6333 DEVO_REV, /* version */ 6334 0, /* refcnt */ 6335 zfs_info, /* info */ 6336 nulldev, /* identify */ 6337 nulldev, /* probe */ 6338 zfs_attach, /* attach */ 6339 zfs_detach, /* detach */ 6340 nodev, /* reset */ 6341 &zfs_cb_ops, /* driver operations */ 6342 NULL, /* no bus operations */ 6343 NULL, /* power */ 6344 ddi_quiesce_not_needed, /* quiesce */ 6345 }; 6346 6347 static struct modldrv zfs_modldrv = { 6348 &mod_driverops, 6349 "ZFS storage pool", 6350 &zfs_dev_ops 6351 }; 6352 6353 static struct modlinkage modlinkage = { 6354 MODREV_1, 6355 (void *)&zfs_modlfs, 6356 (void *)&zfs_modldrv, 6357 NULL 6358 }; 6359 6360 static void 6361 zfs_allow_log_destroy(void *arg) 6362 { 6363 char *poolname = arg; 6364 strfree(poolname); 6365 } 6366 6367 int 6368 _init(void) 6369 { 6370 int error; 6371 6372 spa_init(FREAD | FWRITE); 6373 zfs_init(); 6374 zvol_init(); 6375 zfs_ioctl_init(); 6376 6377 if ((error = mod_install(&modlinkage)) != 0) { 6378 zvol_fini(); 6379 zfs_fini(); 6380 spa_fini(); 6381 return (error); 6382 } 6383 6384 tsd_create(&zfs_fsyncer_key, NULL); 6385 tsd_create(&rrw_tsd_key, rrw_tsd_destroy); 6386 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy); 6387 6388 error = ldi_ident_from_mod(&modlinkage, &zfs_li); 6389 ASSERT(error == 0); 6390 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL); 6391 6392 return (0); 6393 } 6394 6395 int 6396 _fini(void) 6397 { 6398 int error; 6399 6400 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled) 6401 return (SET_ERROR(EBUSY)); 6402 6403 if ((error = mod_remove(&modlinkage)) != 0) 6404 return (error); 6405 6406 zvol_fini(); 6407 zfs_fini(); 6408 spa_fini(); 6409 if (zfs_nfsshare_inited) 6410 (void) ddi_modclose(nfs_mod); 6411 if (zfs_smbshare_inited) 6412 (void) ddi_modclose(smbsrv_mod); 6413 if (zfs_nfsshare_inited || zfs_smbshare_inited) 6414 (void) ddi_modclose(sharefs_mod); 6415 6416 tsd_destroy(&zfs_fsyncer_key); 6417 ldi_ident_release(zfs_li); 6418 zfs_li = NULL; 6419 mutex_destroy(&zfs_share_lock); 6420 6421 return (error); 6422 } 6423 6424 int 6425 _info(struct modinfo *modinfop) 6426 { 6427 return (mod_info(&modlinkage, modinfop)); 6428 } 6429