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.
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.
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]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Portions Copyright 2011 Martin Matuska
25 * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
26 * Portions Copyright 2012 Pawel Jakub Dawidek <pawel@dawidek.net>
27 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
28 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
29 * Copyright (c) 2014, Joyent, Inc. All rights reserved.
30 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
31 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
32 * Copyright (c) 2013 Steven Hartland. All rights reserved.
33 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
39 * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
40 * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
42 * There are two ways that we handle ioctls: the legacy way where almost
43 * all of the logic is in the ioctl callback, and the new way where most
44 * of the marshalling is handled in the common entry point, zfsdev_ioctl().
46 * Non-legacy ioctls should be registered by calling
47 * zfs_ioctl_register() from zfs_ioctl_init(). The ioctl is invoked
48 * from userland by lzc_ioctl().
50 * The registration arguments are as follows:
53 * The name of the ioctl. This is used for history logging. If the
54 * ioctl returns successfully (the callback returns 0), and allow_log
55 * is true, then a history log entry will be recorded with the input &
56 * output nvlists. The log entry can be printed with "zpool history -i".
59 * The ioctl request number, which userland will pass to ioctl(2).
60 * The ioctl numbers can change from release to release, because
61 * the caller (libzfs) must be matched to the kernel.
63 * zfs_secpolicy_func_t *secpolicy
64 * This function will be called before the zfs_ioc_func_t, to
65 * determine if this operation is permitted. It should return EPERM
66 * on failure, and 0 on success. Checks include determining if the
67 * dataset is visible in this zone, and if the user has either all
68 * zfs privileges in the zone (SYS_MOUNT), or has been granted permission
69 * to do this operation on this dataset with "zfs allow".
71 * zfs_ioc_namecheck_t namecheck
72 * This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
73 * name, a dataset name, or nothing. If the name is not well-formed,
74 * the ioctl will fail and the callback will not be called.
75 * Therefore, the callback can assume that the name is well-formed
76 * (e.g. is null-terminated, doesn't have more than one '@' character,
77 * doesn't have invalid characters).
79 * zfs_ioc_poolcheck_t pool_check
80 * This specifies requirements on the pool state. If the pool does
81 * not meet them (is suspended or is readonly), the ioctl will fail
82 * and the callback will not be called. If any checks are specified
83 * (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
84 * Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
85 * POOL_CHECK_READONLY).
87 * boolean_t smush_outnvlist
88 * If smush_outnvlist is true, then the output is presumed to be a
89 * list of errors, and it will be "smushed" down to fit into the
90 * caller's buffer, by removing some entries and replacing them with a
91 * single "N_MORE_ERRORS" entry indicating how many were removed. See
92 * nvlist_smush() for details. If smush_outnvlist is false, and the
93 * outnvlist does not fit into the userland-provided buffer, then the
94 * ioctl will fail with ENOMEM.
96 * zfs_ioc_func_t *func
97 * The callback function that will perform the operation.
99 * The callback should return 0 on success, or an error number on
100 * failure. If the function fails, the userland ioctl will return -1,
101 * and errno will be set to the callback's return value. The callback
102 * will be called with the following arguments:
105 * The name of the pool or dataset to operate on, from
106 * zfs_cmd_t:zc_name. The 'namecheck' argument specifies the
107 * expected type (pool, dataset, or none).
110 * The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src. Or
111 * NULL if no input nvlist was provided. Changes to this nvlist are
112 * ignored. If the input nvlist could not be deserialized, the
113 * ioctl will fail and the callback will not be called.
116 * The output nvlist, initially empty. The callback can fill it in,
117 * and it will be returned to userland by serializing it into
118 * zfs_cmd_t:zc_nvlist_dst. If it is non-empty, and serialization
119 * fails (e.g. because the caller didn't supply a large enough
120 * buffer), then the overall ioctl will fail. See the
121 * 'smush_nvlist' argument above for additional behaviors.
123 * There are two typical uses of the output nvlist:
124 * - To return state, e.g. property values. In this case,
125 * smush_outnvlist should be false. If the buffer was not large
126 * enough, the caller will reallocate a larger buffer and try
129 * - To return multiple errors from an ioctl which makes on-disk
130 * changes. In this case, smush_outnvlist should be true.
131 * Ioctls which make on-disk modifications should generally not
132 * use the outnvl if they succeed, because the caller can not
133 * distinguish between the operation failing, and
134 * deserialization failing.
137 #include <sys/types.h>
138 #include <sys/param.h>
139 #include <sys/errno.h>
142 #include <sys/modctl.h>
143 #include <sys/open.h>
144 #include <sys/file.h>
145 #include <sys/kmem.h>
146 #include <sys/conf.h>
147 #include <sys/cmn_err.h>
148 #include <sys/stat.h>
149 #include <sys/zfs_ioctl.h>
150 #include <sys/zfs_vfsops.h>
151 #include <sys/zfs_znode.h>
154 #include <sys/spa_impl.h>
155 #include <sys/vdev.h>
156 #include <sys/priv_impl.h>
158 #include <sys/dsl_dir.h>
159 #include <sys/dsl_dataset.h>
160 #include <sys/dsl_prop.h>
161 #include <sys/dsl_deleg.h>
162 #include <sys/dmu_objset.h>
163 #include <sys/dmu_impl.h>
164 #include <sys/dmu_tx.h>
166 #include <sys/sunddi.h>
167 #include <sys/sunldi.h>
168 #include <sys/policy.h>
169 #include <sys/zone.h>
170 #include <sys/nvpair.h>
171 #include <sys/pathname.h>
172 #include <sys/mount.h>
174 #include <sys/fs/zfs.h>
175 #include <sys/zfs_ctldir.h>
176 #include <sys/zfs_dir.h>
177 #include <sys/zfs_onexit.h>
178 #include <sys/zvol.h>
179 #include <sys/dsl_scan.h>
180 #include <sharefs/share.h>
181 #include <sys/fm/util.h>
183 #include <sys/dmu_send.h>
184 #include <sys/dsl_destroy.h>
185 #include <sys/dsl_bookmark.h>
186 #include <sys/dsl_userhold.h>
187 #include <sys/zfeature.h>
189 #include <linux/miscdevice.h>
190 #include <linux/slab.h>
192 #include "zfs_namecheck.h"
193 #include "zfs_prop.h"
194 #include "zfs_deleg.h"
195 #include "zfs_comutil.h"
198 * Limit maximum nvlist size. We don't want users passing in insane values
199 * for zc->zc_nvlist_src_size, since we will need to allocate that much memory.
201 #define MAX_NVLIST_SRC_SIZE KMALLOC_MAX_SIZE
203 kmutex_t zfsdev_state_lock;
204 zfsdev_state_t *zfsdev_state_list;
206 extern void zfs_init(void);
207 extern void zfs_fini(void);
209 uint_t zfs_fsyncer_key;
210 extern uint_t rrw_tsd_key;
211 static uint_t zfs_allow_log_key;
213 typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *);
214 typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *);
215 typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *);
221 } zfs_ioc_namecheck_t;
224 POOL_CHECK_NONE = 1 << 0,
225 POOL_CHECK_SUSPENDED = 1 << 1,
226 POOL_CHECK_READONLY = 1 << 2,
227 } zfs_ioc_poolcheck_t;
229 typedef struct zfs_ioc_vec {
230 zfs_ioc_legacy_func_t *zvec_legacy_func;
231 zfs_ioc_func_t *zvec_func;
232 zfs_secpolicy_func_t *zvec_secpolicy;
233 zfs_ioc_namecheck_t zvec_namecheck;
234 boolean_t zvec_allow_log;
235 zfs_ioc_poolcheck_t zvec_pool_check;
236 boolean_t zvec_smush_outnvlist;
237 const char *zvec_name;
240 /* This array is indexed by zfs_userquota_prop_t */
241 static const char *userquota_perms[] = {
242 ZFS_DELEG_PERM_USERUSED,
243 ZFS_DELEG_PERM_USERQUOTA,
244 ZFS_DELEG_PERM_GROUPUSED,
245 ZFS_DELEG_PERM_GROUPQUOTA,
248 static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
249 static int zfs_check_settable(const char *name, nvpair_t *property,
251 static int zfs_check_clearable(char *dataset, nvlist_t *props,
253 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
255 int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
256 static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
259 history_str_free(char *buf)
261 kmem_free(buf, HIS_MAX_RECORD_LEN);
265 history_str_get(zfs_cmd_t *zc)
269 if (zc->zc_history == 0)
272 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
273 if (copyinstr((void *)(uintptr_t)zc->zc_history,
274 buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
275 history_str_free(buf);
279 buf[HIS_MAX_RECORD_LEN -1] = '\0';
285 * Check to see if the named dataset is currently defined as bootable
288 zfs_is_bootfs(const char *name)
292 if (dmu_objset_hold(name, FTAG, &os) == 0) {
294 ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
295 dmu_objset_rele(os, FTAG);
302 * Return non-zero if the spa version is less than requested version.
305 zfs_earlier_version(const char *name, int version)
309 if (spa_open(name, &spa, FTAG) == 0) {
310 if (spa_version(spa) < version) {
311 spa_close(spa, FTAG);
314 spa_close(spa, FTAG);
320 * Return TRUE if the ZPL version is less than requested version.
323 zpl_earlier_version(const char *name, int version)
326 boolean_t rc = B_TRUE;
328 if (dmu_objset_hold(name, FTAG, &os) == 0) {
331 if (dmu_objset_type(os) != DMU_OST_ZFS) {
332 dmu_objset_rele(os, FTAG);
335 /* XXX reading from non-owned objset */
336 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
337 rc = zplversion < version;
338 dmu_objset_rele(os, FTAG);
344 zfs_log_history(zfs_cmd_t *zc)
349 if ((buf = history_str_get(zc)) == NULL)
352 if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
353 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
354 (void) spa_history_log(spa, buf);
355 spa_close(spa, FTAG);
357 history_str_free(buf);
361 * Policy for top-level read operations (list pools). Requires no privileges,
362 * and can be used in the local zone, as there is no associated dataset.
366 zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
372 * Policy for dataset read operations (list children, get statistics). Requires
373 * no privileges, but must be visible in the local zone.
377 zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
379 if (INGLOBALZONE(curproc) ||
380 zone_dataset_visible(zc->zc_name, NULL))
383 return (SET_ERROR(ENOENT));
387 zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
392 * The dataset must be visible by this zone -- check this first
393 * so they don't see EPERM on something they shouldn't know about.
395 if (!INGLOBALZONE(curproc) &&
396 !zone_dataset_visible(dataset, &writable))
397 return (SET_ERROR(ENOENT));
399 if (INGLOBALZONE(curproc)) {
401 * If the fs is zoned, only root can access it from the
404 if (secpolicy_zfs(cr) && zoned)
405 return (SET_ERROR(EPERM));
408 * If we are in a local zone, the 'zoned' property must be set.
411 return (SET_ERROR(EPERM));
413 /* must be writable by this zone */
415 return (SET_ERROR(EPERM));
421 zfs_dozonecheck(const char *dataset, cred_t *cr)
425 if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
426 return (SET_ERROR(ENOENT));
428 return (zfs_dozonecheck_impl(dataset, zoned, cr));
432 zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
436 if (dsl_prop_get_int_ds(ds, "zoned", &zoned))
437 return (SET_ERROR(ENOENT));
439 return (zfs_dozonecheck_impl(dataset, zoned, cr));
443 zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
444 const char *perm, cred_t *cr)
448 error = zfs_dozonecheck_ds(name, ds, cr);
450 error = secpolicy_zfs(cr);
452 error = dsl_deleg_access_impl(ds, perm, cr);
458 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
464 error = dsl_pool_hold(name, FTAG, &dp);
468 error = dsl_dataset_hold(dp, name, FTAG, &ds);
470 dsl_pool_rele(dp, FTAG);
474 error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
476 dsl_dataset_rele(ds, FTAG);
477 dsl_pool_rele(dp, FTAG);
482 * Policy for setting the security label property.
484 * Returns 0 for success, non-zero for access and other errors.
487 zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
490 char ds_hexsl[MAXNAMELEN];
491 bslabel_t ds_sl, new_sl;
492 boolean_t new_default = FALSE;
494 int needed_priv = -1;
497 /* First get the existing dataset label. */
498 error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
499 1, sizeof (ds_hexsl), &ds_hexsl, NULL);
501 return (SET_ERROR(EPERM));
503 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
506 /* The label must be translatable */
507 if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
508 return (SET_ERROR(EINVAL));
511 * In a non-global zone, disallow attempts to set a label that
512 * doesn't match that of the zone; otherwise no other checks
515 if (!INGLOBALZONE(curproc)) {
516 if (new_default || !blequal(&new_sl, CR_SL(CRED())))
517 return (SET_ERROR(EPERM));
522 * For global-zone datasets (i.e., those whose zoned property is
523 * "off", verify that the specified new label is valid for the
526 if (dsl_prop_get_integer(name,
527 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
528 return (SET_ERROR(EPERM));
530 if (zfs_check_global_label(name, strval) != 0)
531 return (SET_ERROR(EPERM));
535 * If the existing dataset label is nondefault, check if the
536 * dataset is mounted (label cannot be changed while mounted).
537 * Get the zfs_sb_t; if there isn't one, then the dataset isn't
538 * mounted (or isn't a dataset, doesn't exist, ...).
540 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
542 static char *setsl_tag = "setsl_tag";
545 * Try to own the dataset; abort if there is any error,
546 * (e.g., already mounted, in use, or other error).
548 error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE,
551 return (SET_ERROR(EPERM));
553 dmu_objset_disown(os, setsl_tag);
556 needed_priv = PRIV_FILE_DOWNGRADE_SL;
560 if (hexstr_to_label(strval, &new_sl) != 0)
561 return (SET_ERROR(EPERM));
563 if (blstrictdom(&ds_sl, &new_sl))
564 needed_priv = PRIV_FILE_DOWNGRADE_SL;
565 else if (blstrictdom(&new_sl, &ds_sl))
566 needed_priv = PRIV_FILE_UPGRADE_SL;
568 /* dataset currently has a default label */
570 needed_priv = PRIV_FILE_UPGRADE_SL;
574 if (needed_priv != -1)
575 return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
579 #endif /* HAVE_MLSLABEL */
583 zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
589 * Check permissions for special properties.
596 * Disallow setting of 'zoned' from within a local zone.
598 if (!INGLOBALZONE(curproc))
599 return (SET_ERROR(EPERM));
603 case ZFS_PROP_FILESYSTEM_LIMIT:
604 case ZFS_PROP_SNAPSHOT_LIMIT:
605 if (!INGLOBALZONE(curproc)) {
607 char setpoint[ZFS_MAX_DATASET_NAME_LEN];
609 * Unprivileged users are allowed to modify the
610 * limit on things *under* (ie. contained by)
611 * the thing they own.
613 if (dsl_prop_get_integer(dsname, "zoned", &zoned,
615 return (SET_ERROR(EPERM));
616 if (!zoned || strlen(dsname) <= strlen(setpoint))
617 return (SET_ERROR(EPERM));
621 case ZFS_PROP_MLSLABEL:
622 if (!is_system_labeled())
623 return (SET_ERROR(EPERM));
625 if (nvpair_value_string(propval, &strval) == 0) {
628 err = zfs_set_slabel_policy(dsname, strval, CRED());
635 return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
640 zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
644 error = zfs_dozonecheck(zc->zc_name, cr);
649 * permission to set permissions will be evaluated later in
650 * dsl_deleg_can_allow()
657 zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
659 return (zfs_secpolicy_write_perms(zc->zc_name,
660 ZFS_DELEG_PERM_ROLLBACK, cr));
665 zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
673 * Generate the current snapshot name from the given objsetid, then
674 * use that name for the secpolicy/zone checks.
676 cp = strchr(zc->zc_name, '@');
678 return (SET_ERROR(EINVAL));
679 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
683 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
685 dsl_pool_rele(dp, FTAG);
689 dsl_dataset_name(ds, zc->zc_name);
691 error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
692 ZFS_DELEG_PERM_SEND, cr);
693 dsl_dataset_rele(ds, FTAG);
694 dsl_pool_rele(dp, FTAG);
701 zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
703 return (zfs_secpolicy_write_perms(zc->zc_name,
704 ZFS_DELEG_PERM_SEND, cr));
707 #ifdef HAVE_SMB_SHARE
710 zfs_secpolicy_deleg_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
715 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
716 NO_FOLLOW, NULL, &vp)) != 0)
719 /* Now make sure mntpnt and dataset are ZFS */
721 if (vp->v_vfsp->vfs_fstype != zfsfstype ||
722 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
723 zc->zc_name) != 0)) {
725 return (SET_ERROR(EPERM));
729 return (dsl_deleg_access(zc->zc_name,
730 ZFS_DELEG_PERM_SHARE, cr));
732 #endif /* HAVE_SMB_SHARE */
735 zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
737 #ifdef HAVE_SMB_SHARE
738 if (!INGLOBALZONE(curproc))
739 return (SET_ERROR(EPERM));
741 if (secpolicy_nfs(cr) == 0) {
744 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
747 return (SET_ERROR(ENOTSUP));
748 #endif /* HAVE_SMB_SHARE */
752 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
754 #ifdef HAVE_SMB_SHARE
755 if (!INGLOBALZONE(curproc))
756 return (SET_ERROR(EPERM));
758 if (secpolicy_smb(cr) == 0) {
761 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
764 return (SET_ERROR(ENOTSUP));
765 #endif /* HAVE_SMB_SHARE */
769 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
774 * Remove the @bla or /bla from the end of the name to get the parent.
776 (void) strncpy(parent, datasetname, parentsize);
777 cp = strrchr(parent, '@');
781 cp = strrchr(parent, '/');
783 return (SET_ERROR(ENOENT));
791 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
795 if ((error = zfs_secpolicy_write_perms(name,
796 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
799 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
804 zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
806 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
810 * Destroying snapshots with delegated permissions requires
811 * descendant mount and destroy permissions.
815 zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
818 nvpair_t *pair, *nextpair;
821 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
822 return (SET_ERROR(EINVAL));
823 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
825 nextpair = nvlist_next_nvpair(snaps, pair);
826 error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr);
827 if (error == ENOENT) {
829 * Ignore any snapshots that don't exist (we consider
830 * them "already destroyed"). Remove the name from the
831 * nvl here in case the snapshot is created between
832 * now and when we try to destroy it (in which case
833 * we don't want to destroy it since we haven't
834 * checked for permission).
836 fnvlist_remove_nvpair(snaps, pair);
847 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
849 char parentname[ZFS_MAX_DATASET_NAME_LEN];
852 if ((error = zfs_secpolicy_write_perms(from,
853 ZFS_DELEG_PERM_RENAME, cr)) != 0)
856 if ((error = zfs_secpolicy_write_perms(from,
857 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
860 if ((error = zfs_get_parent(to, parentname,
861 sizeof (parentname))) != 0)
864 if ((error = zfs_secpolicy_write_perms(parentname,
865 ZFS_DELEG_PERM_CREATE, cr)) != 0)
868 if ((error = zfs_secpolicy_write_perms(parentname,
869 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
877 zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
879 return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
884 zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
887 dsl_dataset_t *clone;
890 error = zfs_secpolicy_write_perms(zc->zc_name,
891 ZFS_DELEG_PERM_PROMOTE, cr);
895 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
899 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
902 char parentname[ZFS_MAX_DATASET_NAME_LEN];
903 dsl_dataset_t *origin = NULL;
907 error = dsl_dataset_hold_obj(dd->dd_pool,
908 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin);
910 dsl_dataset_rele(clone, FTAG);
911 dsl_pool_rele(dp, FTAG);
915 error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
916 ZFS_DELEG_PERM_MOUNT, cr);
918 dsl_dataset_name(origin, parentname);
920 error = zfs_secpolicy_write_perms_ds(parentname, origin,
921 ZFS_DELEG_PERM_PROMOTE, cr);
923 dsl_dataset_rele(clone, FTAG);
924 dsl_dataset_rele(origin, FTAG);
926 dsl_pool_rele(dp, FTAG);
932 zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
936 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
937 ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
940 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
941 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
944 return (zfs_secpolicy_write_perms(zc->zc_name,
945 ZFS_DELEG_PERM_CREATE, cr));
950 zfs_secpolicy_recv_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
952 return (zfs_secpolicy_recv(zc, innvl, cr));
956 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
958 return (zfs_secpolicy_write_perms(name,
959 ZFS_DELEG_PERM_SNAPSHOT, cr));
963 * Check for permission to create each snapshot in the nvlist.
967 zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
973 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
974 return (SET_ERROR(EINVAL));
975 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
976 pair = nvlist_next_nvpair(snaps, pair)) {
977 char *name = nvpair_name(pair);
978 char *atp = strchr(name, '@');
981 error = SET_ERROR(EINVAL);
985 error = zfs_secpolicy_snapshot_perms(name, cr);
994 * Check for permission to create each snapshot in the nvlist.
998 zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1003 for (pair = nvlist_next_nvpair(innvl, NULL);
1004 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
1005 char *name = nvpair_name(pair);
1006 char *hashp = strchr(name, '#');
1008 if (hashp == NULL) {
1009 error = SET_ERROR(EINVAL);
1013 error = zfs_secpolicy_write_perms(name,
1014 ZFS_DELEG_PERM_BOOKMARK, cr);
1024 zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1026 nvpair_t *pair, *nextpair;
1029 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1031 char *name = nvpair_name(pair);
1032 char *hashp = strchr(name, '#');
1033 nextpair = nvlist_next_nvpair(innvl, pair);
1035 if (hashp == NULL) {
1036 error = SET_ERROR(EINVAL);
1041 error = zfs_secpolicy_write_perms(name,
1042 ZFS_DELEG_PERM_DESTROY, cr);
1044 if (error == ENOENT) {
1046 * Ignore any filesystems that don't exist (we consider
1047 * their bookmarks "already destroyed"). Remove
1048 * the name from the nvl here in case the filesystem
1049 * is created between now and when we try to destroy
1050 * the bookmark (in which case we don't want to
1051 * destroy it since we haven't checked for permission).
1053 fnvlist_remove_nvpair(innvl, pair);
1065 zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1068 * Even root must have a proper TSD so that we know what pool
1071 if (tsd_get(zfs_allow_log_key) == NULL)
1072 return (SET_ERROR(EPERM));
1077 zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1079 char parentname[ZFS_MAX_DATASET_NAME_LEN];
1083 if ((error = zfs_get_parent(zc->zc_name, parentname,
1084 sizeof (parentname))) != 0)
1087 if (nvlist_lookup_string(innvl, "origin", &origin) == 0 &&
1088 (error = zfs_secpolicy_write_perms(origin,
1089 ZFS_DELEG_PERM_CLONE, cr)) != 0)
1092 if ((error = zfs_secpolicy_write_perms(parentname,
1093 ZFS_DELEG_PERM_CREATE, cr)) != 0)
1096 return (zfs_secpolicy_write_perms(parentname,
1097 ZFS_DELEG_PERM_MOUNT, cr));
1101 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
1102 * SYS_CONFIG privilege, which is not available in a local zone.
1106 zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1108 if (secpolicy_sys_config(cr, B_FALSE) != 0)
1109 return (SET_ERROR(EPERM));
1115 * Policy for object to name lookups.
1119 zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1123 if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
1126 error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
1131 * Policy for fault injection. Requires all privileges.
1135 zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1137 return (secpolicy_zinject(cr));
1142 zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1144 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
1146 if (prop == ZPROP_INVAL) {
1147 if (!zfs_prop_user(zc->zc_value))
1148 return (SET_ERROR(EINVAL));
1149 return (zfs_secpolicy_write_perms(zc->zc_name,
1150 ZFS_DELEG_PERM_USERPROP, cr));
1152 return (zfs_secpolicy_setprop(zc->zc_name, prop,
1158 zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1160 int err = zfs_secpolicy_read(zc, innvl, cr);
1164 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1165 return (SET_ERROR(EINVAL));
1167 if (zc->zc_value[0] == 0) {
1169 * They are asking about a posix uid/gid. If it's
1170 * themself, allow it.
1172 if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
1173 zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
1174 if (zc->zc_guid == crgetuid(cr))
1177 if (groupmember(zc->zc_guid, cr))
1182 return (zfs_secpolicy_write_perms(zc->zc_name,
1183 userquota_perms[zc->zc_objset_type], cr));
1187 zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1189 int err = zfs_secpolicy_read(zc, innvl, cr);
1193 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1194 return (SET_ERROR(EINVAL));
1196 return (zfs_secpolicy_write_perms(zc->zc_name,
1197 userquota_perms[zc->zc_objset_type], cr));
1202 zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1204 return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
1210 zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1216 error = nvlist_lookup_nvlist(innvl, "holds", &holds);
1218 return (SET_ERROR(EINVAL));
1220 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
1221 pair = nvlist_next_nvpair(holds, pair)) {
1222 char fsname[ZFS_MAX_DATASET_NAME_LEN];
1223 error = dmu_fsname(nvpair_name(pair), fsname);
1226 error = zfs_secpolicy_write_perms(fsname,
1227 ZFS_DELEG_PERM_HOLD, cr);
1236 zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1241 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1242 pair = nvlist_next_nvpair(innvl, pair)) {
1243 char fsname[ZFS_MAX_DATASET_NAME_LEN];
1244 error = dmu_fsname(nvpair_name(pair), fsname);
1247 error = zfs_secpolicy_write_perms(fsname,
1248 ZFS_DELEG_PERM_RELEASE, cr);
1256 * Policy for allowing temporary snapshots to be taken or released
1259 zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1262 * A temporary snapshot is the same as a snapshot,
1263 * hold, destroy and release all rolled into one.
1264 * Delegated diff alone is sufficient that we allow this.
1268 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1269 ZFS_DELEG_PERM_DIFF, cr)) == 0)
1272 error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
1274 error = zfs_secpolicy_hold(zc, innvl, cr);
1276 error = zfs_secpolicy_release(zc, innvl, cr);
1278 error = zfs_secpolicy_destroy(zc, innvl, cr);
1283 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1286 get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1290 nvlist_t *list = NULL;
1293 * Read in and unpack the user-supplied nvlist.
1296 return (SET_ERROR(EINVAL));
1298 packed = vmem_alloc(size, KM_SLEEP);
1300 if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1302 vmem_free(packed, size);
1303 return (SET_ERROR(EFAULT));
1306 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1307 vmem_free(packed, size);
1311 vmem_free(packed, size);
1318 * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
1319 * Entries will be removed from the end of the nvlist, and one int32 entry
1320 * named "N_MORE_ERRORS" will be added indicating how many entries were
1324 nvlist_smush(nvlist_t *errors, size_t max)
1328 size = fnvlist_size(errors);
1331 nvpair_t *more_errors;
1335 return (SET_ERROR(ENOMEM));
1337 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
1338 more_errors = nvlist_prev_nvpair(errors, NULL);
1341 nvpair_t *pair = nvlist_prev_nvpair(errors,
1343 fnvlist_remove_nvpair(errors, pair);
1345 size = fnvlist_size(errors);
1346 } while (size > max);
1348 fnvlist_remove_nvpair(errors, more_errors);
1349 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
1350 ASSERT3U(fnvlist_size(errors), <=, max);
1357 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1359 char *packed = NULL;
1363 size = fnvlist_size(nvl);
1365 if (size > zc->zc_nvlist_dst_size) {
1366 error = SET_ERROR(ENOMEM);
1368 packed = fnvlist_pack(nvl, &size);
1369 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1370 size, zc->zc_iflags) != 0)
1371 error = SET_ERROR(EFAULT);
1372 fnvlist_pack_free(packed, size);
1375 zc->zc_nvlist_dst_size = size;
1376 zc->zc_nvlist_dst_filled = B_TRUE;
1381 get_zfs_sb(const char *dsname, zfs_sb_t **zsbp)
1386 error = dmu_objset_hold(dsname, FTAG, &os);
1389 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1390 dmu_objset_rele(os, FTAG);
1391 return (SET_ERROR(EINVAL));
1394 mutex_enter(&os->os_user_ptr_lock);
1395 *zsbp = dmu_objset_get_user(os);
1396 /* bump s_active only when non-zero to prevent umount race */
1397 if (*zsbp == NULL || (*zsbp)->z_sb == NULL ||
1398 !atomic_inc_not_zero(&((*zsbp)->z_sb->s_active))) {
1399 error = SET_ERROR(ESRCH);
1401 mutex_exit(&os->os_user_ptr_lock);
1402 dmu_objset_rele(os, FTAG);
1407 * Find a zfs_sb_t for a mounted filesystem, or create our own, in which
1408 * case its z_sb will be NULL, and it will be opened as the owner.
1409 * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1410 * which prevents all inode ops from running.
1413 zfs_sb_hold(const char *name, void *tag, zfs_sb_t **zsbp, boolean_t writer)
1417 if (get_zfs_sb(name, zsbp) != 0)
1418 error = zfs_sb_create(name, NULL, zsbp);
1420 rrm_enter(&(*zsbp)->z_teardown_lock, (writer) ? RW_WRITER :
1422 if ((*zsbp)->z_unmounted) {
1424 * XXX we could probably try again, since the unmounting
1425 * thread should be just about to disassociate the
1426 * objset from the zsb.
1428 rrm_exit(&(*zsbp)->z_teardown_lock, tag);
1429 return (SET_ERROR(EBUSY));
1436 zfs_sb_rele(zfs_sb_t *zsb, void *tag)
1438 rrm_exit(&zsb->z_teardown_lock, tag);
1441 deactivate_super(zsb->z_sb);
1443 dmu_objset_disown(zsb->z_os, zsb);
1449 zfs_ioc_pool_create(zfs_cmd_t *zc)
1452 nvlist_t *config, *props = NULL;
1453 nvlist_t *rootprops = NULL;
1454 nvlist_t *zplprops = NULL;
1456 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1457 zc->zc_iflags, &config)))
1460 if (zc->zc_nvlist_src_size != 0 && (error =
1461 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1462 zc->zc_iflags, &props))) {
1463 nvlist_free(config);
1468 nvlist_t *nvl = NULL;
1469 uint64_t version = SPA_VERSION;
1471 (void) nvlist_lookup_uint64(props,
1472 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1473 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1474 error = SET_ERROR(EINVAL);
1475 goto pool_props_bad;
1477 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
1479 error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
1481 nvlist_free(config);
1485 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1487 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1488 error = zfs_fill_zplprops_root(version, rootprops,
1491 goto pool_props_bad;
1494 error = spa_create(zc->zc_name, config, props, zplprops);
1497 * Set the remaining root properties
1499 if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
1500 ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
1501 (void) spa_destroy(zc->zc_name);
1504 nvlist_free(rootprops);
1505 nvlist_free(zplprops);
1506 nvlist_free(config);
1513 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1516 zfs_log_history(zc);
1517 error = spa_destroy(zc->zc_name);
1523 zfs_ioc_pool_import(zfs_cmd_t *zc)
1525 nvlist_t *config, *props = NULL;
1529 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1530 zc->zc_iflags, &config)) != 0)
1533 if (zc->zc_nvlist_src_size != 0 && (error =
1534 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1535 zc->zc_iflags, &props))) {
1536 nvlist_free(config);
1540 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1541 guid != zc->zc_guid)
1542 error = SET_ERROR(EINVAL);
1544 error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1546 if (zc->zc_nvlist_dst != 0) {
1549 if ((err = put_nvlist(zc, config)) != 0)
1553 nvlist_free(config);
1560 zfs_ioc_pool_export(zfs_cmd_t *zc)
1563 boolean_t force = (boolean_t)zc->zc_cookie;
1564 boolean_t hardforce = (boolean_t)zc->zc_guid;
1566 zfs_log_history(zc);
1567 error = spa_export(zc->zc_name, NULL, force, hardforce);
1573 zfs_ioc_pool_configs(zfs_cmd_t *zc)
1578 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1579 return (SET_ERROR(EEXIST));
1581 error = put_nvlist(zc, configs);
1583 nvlist_free(configs);
1590 * zc_name name of the pool
1593 * zc_cookie real errno
1594 * zc_nvlist_dst config nvlist
1595 * zc_nvlist_dst_size size of config nvlist
1598 zfs_ioc_pool_stats(zfs_cmd_t *zc)
1604 error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1605 sizeof (zc->zc_value));
1607 if (config != NULL) {
1608 ret = put_nvlist(zc, config);
1609 nvlist_free(config);
1612 * The config may be present even if 'error' is non-zero.
1613 * In this case we return success, and preserve the real errno
1616 zc->zc_cookie = error;
1625 * Try to import the given pool, returning pool stats as appropriate so that
1626 * user land knows which devices are available and overall pool health.
1629 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1631 nvlist_t *tryconfig, *config;
1634 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1635 zc->zc_iflags, &tryconfig)) != 0)
1638 config = spa_tryimport(tryconfig);
1640 nvlist_free(tryconfig);
1643 return (SET_ERROR(EINVAL));
1645 error = put_nvlist(zc, config);
1646 nvlist_free(config);
1653 * zc_name name of the pool
1654 * zc_cookie scan func (pool_scan_func_t)
1657 zfs_ioc_pool_scan(zfs_cmd_t *zc)
1662 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1665 if (zc->zc_cookie == POOL_SCAN_NONE)
1666 error = spa_scan_stop(spa);
1668 error = spa_scan(spa, zc->zc_cookie);
1670 spa_close(spa, FTAG);
1676 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1681 error = spa_open(zc->zc_name, &spa, FTAG);
1684 spa_close(spa, FTAG);
1690 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1695 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1698 if (zc->zc_cookie < spa_version(spa) ||
1699 !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1700 spa_close(spa, FTAG);
1701 return (SET_ERROR(EINVAL));
1704 spa_upgrade(spa, zc->zc_cookie);
1705 spa_close(spa, FTAG);
1711 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1718 if ((size = zc->zc_history_len) == 0)
1719 return (SET_ERROR(EINVAL));
1721 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1724 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1725 spa_close(spa, FTAG);
1726 return (SET_ERROR(ENOTSUP));
1729 hist_buf = vmem_alloc(size, KM_SLEEP);
1730 if ((error = spa_history_get(spa, &zc->zc_history_offset,
1731 &zc->zc_history_len, hist_buf)) == 0) {
1732 error = ddi_copyout(hist_buf,
1733 (void *)(uintptr_t)zc->zc_history,
1734 zc->zc_history_len, zc->zc_iflags);
1737 spa_close(spa, FTAG);
1738 vmem_free(hist_buf, size);
1743 zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1748 error = spa_open(zc->zc_name, &spa, FTAG);
1750 error = spa_change_guid(spa);
1751 spa_close(spa, FTAG);
1757 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1759 return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
1764 * zc_name name of filesystem
1765 * zc_obj object to find
1768 * zc_value name of object
1771 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1776 /* XXX reading from objset not owned */
1777 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1779 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1780 dmu_objset_rele(os, FTAG);
1781 return (SET_ERROR(EINVAL));
1783 error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
1784 sizeof (zc->zc_value));
1785 dmu_objset_rele(os, FTAG);
1792 * zc_name name of filesystem
1793 * zc_obj object to find
1796 * zc_stat stats on object
1797 * zc_value path to object
1800 zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1805 /* XXX reading from objset not owned */
1806 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1808 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1809 dmu_objset_rele(os, FTAG);
1810 return (SET_ERROR(EINVAL));
1812 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1813 sizeof (zc->zc_value));
1814 dmu_objset_rele(os, FTAG);
1820 zfs_ioc_vdev_add(zfs_cmd_t *zc)
1826 error = spa_open(zc->zc_name, &spa, FTAG);
1830 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1831 zc->zc_iflags, &config);
1833 error = spa_vdev_add(spa, config);
1834 nvlist_free(config);
1836 spa_close(spa, FTAG);
1842 * zc_name name of the pool
1843 * zc_nvlist_conf nvlist of devices to remove
1844 * zc_cookie to stop the remove?
1847 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1852 error = spa_open(zc->zc_name, &spa, FTAG);
1855 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1856 spa_close(spa, FTAG);
1861 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1865 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1867 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1869 switch (zc->zc_cookie) {
1870 case VDEV_STATE_ONLINE:
1871 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1874 case VDEV_STATE_OFFLINE:
1875 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1878 case VDEV_STATE_FAULTED:
1879 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1880 zc->zc_obj != VDEV_AUX_EXTERNAL)
1881 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1883 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
1886 case VDEV_STATE_DEGRADED:
1887 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1888 zc->zc_obj != VDEV_AUX_EXTERNAL)
1889 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1891 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
1895 error = SET_ERROR(EINVAL);
1897 zc->zc_cookie = newstate;
1898 spa_close(spa, FTAG);
1903 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1906 int replacing = zc->zc_cookie;
1910 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1913 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1914 zc->zc_iflags, &config)) == 0) {
1915 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1916 nvlist_free(config);
1919 spa_close(spa, FTAG);
1924 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1929 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1932 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1934 spa_close(spa, FTAG);
1939 zfs_ioc_vdev_split(zfs_cmd_t *zc)
1942 nvlist_t *config, *props = NULL;
1944 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
1946 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1949 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1950 zc->zc_iflags, &config))) {
1951 spa_close(spa, FTAG);
1955 if (zc->zc_nvlist_src_size != 0 && (error =
1956 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1957 zc->zc_iflags, &props))) {
1958 spa_close(spa, FTAG);
1959 nvlist_free(config);
1963 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
1965 spa_close(spa, FTAG);
1967 nvlist_free(config);
1974 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
1977 char *path = zc->zc_value;
1978 uint64_t guid = zc->zc_guid;
1981 error = spa_open(zc->zc_name, &spa, FTAG);
1985 error = spa_vdev_setpath(spa, guid, path);
1986 spa_close(spa, FTAG);
1991 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
1994 char *fru = zc->zc_value;
1995 uint64_t guid = zc->zc_guid;
1998 error = spa_open(zc->zc_name, &spa, FTAG);
2002 error = spa_vdev_setfru(spa, guid, fru);
2003 spa_close(spa, FTAG);
2008 zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
2013 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2015 if (zc->zc_nvlist_dst != 0 &&
2016 (error = dsl_prop_get_all(os, &nv)) == 0) {
2017 dmu_objset_stats(os, nv);
2019 * NB: zvol_get_stats() will read the objset contents,
2020 * which we aren't supposed to do with a
2021 * DS_MODE_USER hold, because it could be
2022 * inconsistent. So this is a bit of a workaround...
2023 * XXX reading with out owning
2025 if (!zc->zc_objset_stats.dds_inconsistent &&
2026 dmu_objset_type(os) == DMU_OST_ZVOL) {
2027 error = zvol_get_stats(os, nv);
2033 error = put_nvlist(zc, nv);
2042 * zc_name name of filesystem
2043 * zc_nvlist_dst_size size of buffer for property nvlist
2046 * zc_objset_stats stats
2047 * zc_nvlist_dst property nvlist
2048 * zc_nvlist_dst_size size of property nvlist
2051 zfs_ioc_objset_stats(zfs_cmd_t *zc)
2056 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2058 error = zfs_ioc_objset_stats_impl(zc, os);
2059 dmu_objset_rele(os, FTAG);
2067 * zc_name name of filesystem
2068 * zc_nvlist_dst_size size of buffer for property nvlist
2071 * zc_nvlist_dst received property nvlist
2072 * zc_nvlist_dst_size size of received property nvlist
2074 * Gets received properties (distinct from local properties on or after
2075 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
2076 * local property values.
2079 zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
2085 * Without this check, we would return local property values if the
2086 * caller has not already received properties on or after
2087 * SPA_VERSION_RECVD_PROPS.
2089 if (!dsl_prop_get_hasrecvd(zc->zc_name))
2090 return (SET_ERROR(ENOTSUP));
2092 if (zc->zc_nvlist_dst != 0 &&
2093 (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
2094 error = put_nvlist(zc, nv);
2102 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2108 * zfs_get_zplprop() will either find a value or give us
2109 * the default value (if there is one).
2111 if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2113 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2119 * zc_name name of filesystem
2120 * zc_nvlist_dst_size size of buffer for zpl property nvlist
2123 * zc_nvlist_dst zpl property nvlist
2124 * zc_nvlist_dst_size size of zpl property nvlist
2127 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2132 /* XXX reading without owning */
2133 if ((err = dmu_objset_hold(zc->zc_name, FTAG, &os)))
2136 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2139 * NB: nvl_add_zplprop() will read the objset contents,
2140 * which we aren't supposed to do with a DS_MODE_USER
2141 * hold, because it could be inconsistent.
2143 if (zc->zc_nvlist_dst != 0 &&
2144 !zc->zc_objset_stats.dds_inconsistent &&
2145 dmu_objset_type(os) == DMU_OST_ZFS) {
2148 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2149 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2150 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2151 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2152 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2153 err = put_nvlist(zc, nv);
2156 err = SET_ERROR(ENOENT);
2158 dmu_objset_rele(os, FTAG);
2163 dataset_name_hidden(const char *name)
2166 * Skip over datasets that are not visible in this zone,
2167 * internal datasets (which have a $ in their name), and
2168 * temporary datasets (which have a % in their name).
2170 if (strchr(name, '$') != NULL)
2172 if (strchr(name, '%') != NULL)
2174 if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
2181 * zc_name name of filesystem
2182 * zc_cookie zap cursor
2183 * zc_nvlist_dst_size size of buffer for property nvlist
2186 * zc_name name of next filesystem
2187 * zc_cookie zap cursor
2188 * zc_objset_stats stats
2189 * zc_nvlist_dst property nvlist
2190 * zc_nvlist_dst_size size of property nvlist
2193 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2198 size_t orig_len = strlen(zc->zc_name);
2201 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os))) {
2202 if (error == ENOENT)
2203 error = SET_ERROR(ESRCH);
2207 p = strrchr(zc->zc_name, '/');
2208 if (p == NULL || p[1] != '\0')
2209 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2210 p = zc->zc_name + strlen(zc->zc_name);
2213 error = dmu_dir_list_next(os,
2214 sizeof (zc->zc_name) - (p - zc->zc_name), p,
2215 NULL, &zc->zc_cookie);
2216 if (error == ENOENT)
2217 error = SET_ERROR(ESRCH);
2218 } while (error == 0 && dataset_name_hidden(zc->zc_name));
2219 dmu_objset_rele(os, FTAG);
2222 * If it's an internal dataset (ie. with a '$' in its name),
2223 * don't try to get stats for it, otherwise we'll return ENOENT.
2225 if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
2226 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2227 if (error == ENOENT) {
2228 /* We lost a race with destroy, get the next one. */
2229 zc->zc_name[orig_len] = '\0';
2238 * zc_name name of filesystem
2239 * zc_cookie zap cursor
2240 * zc_nvlist_dst_size size of buffer for property nvlist
2243 * zc_name name of next snapshot
2244 * zc_objset_stats stats
2245 * zc_nvlist_dst property nvlist
2246 * zc_nvlist_dst_size size of property nvlist
2249 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2254 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2256 return (error == ENOENT ? ESRCH : error);
2260 * A dataset name of maximum length cannot have any snapshots,
2261 * so exit immediately.
2263 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >=
2264 ZFS_MAX_DATASET_NAME_LEN) {
2265 dmu_objset_rele(os, FTAG);
2266 return (SET_ERROR(ESRCH));
2269 error = dmu_snapshot_list_next(os,
2270 sizeof (zc->zc_name) - strlen(zc->zc_name),
2271 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2274 if (error == 0 && !zc->zc_simple) {
2276 dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2278 error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
2282 error = dmu_objset_from_ds(ds, &ossnap);
2284 error = zfs_ioc_objset_stats_impl(zc, ossnap);
2285 dsl_dataset_rele(ds, FTAG);
2287 } else if (error == ENOENT) {
2288 error = SET_ERROR(ESRCH);
2291 dmu_objset_rele(os, FTAG);
2292 /* if we failed, undo the @ that we tacked on to zc_name */
2294 *strchr(zc->zc_name, '@') = '\0';
2299 zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2301 const char *propname = nvpair_name(pair);
2303 unsigned int vallen;
2306 zfs_userquota_prop_t type;
2312 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2314 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2315 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2317 return (SET_ERROR(EINVAL));
2321 * A correctly constructed propname is encoded as
2322 * userquota@<rid>-<domain>.
2324 if ((dash = strchr(propname, '-')) == NULL ||
2325 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2327 return (SET_ERROR(EINVAL));
2334 err = zfs_sb_hold(dsname, FTAG, &zsb, B_FALSE);
2336 err = zfs_set_userquota(zsb, type, domain, rid, quota);
2337 zfs_sb_rele(zsb, FTAG);
2344 * If the named property is one that has a special function to set its value,
2345 * return 0 on success and a positive error code on failure; otherwise if it is
2346 * not one of the special properties handled by this function, return -1.
2348 * XXX: It would be better for callers of the property interface if we handled
2349 * these special cases in dsl_prop.c (in the dsl layer).
2352 zfs_prop_set_special(const char *dsname, zprop_source_t source,
2355 const char *propname = nvpair_name(pair);
2356 zfs_prop_t prop = zfs_name_to_prop(propname);
2360 if (prop == ZPROP_INVAL) {
2361 if (zfs_prop_userquota(propname))
2362 return (zfs_prop_set_userquota(dsname, pair));
2366 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2368 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2369 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2373 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
2376 VERIFY(0 == nvpair_value_uint64(pair, &intval));
2379 case ZFS_PROP_QUOTA:
2380 err = dsl_dir_set_quota(dsname, source, intval);
2382 case ZFS_PROP_REFQUOTA:
2383 err = dsl_dataset_set_refquota(dsname, source, intval);
2385 case ZFS_PROP_FILESYSTEM_LIMIT:
2386 case ZFS_PROP_SNAPSHOT_LIMIT:
2387 if (intval == UINT64_MAX) {
2388 /* clearing the limit, just do it */
2391 err = dsl_dir_activate_fs_ss_limit(dsname);
2394 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2395 * default path to set the value in the nvlist.
2400 case ZFS_PROP_RESERVATION:
2401 err = dsl_dir_set_reservation(dsname, source, intval);
2403 case ZFS_PROP_REFRESERVATION:
2404 err = dsl_dataset_set_refreservation(dsname, source, intval);
2406 case ZFS_PROP_VOLSIZE:
2407 err = zvol_set_volsize(dsname, intval);
2409 case ZFS_PROP_SNAPDEV:
2410 err = zvol_set_snapdev(dsname, source, intval);
2412 case ZFS_PROP_VERSION:
2416 if ((err = zfs_sb_hold(dsname, FTAG, &zsb, B_TRUE)) != 0)
2419 err = zfs_set_version(zsb, intval);
2420 zfs_sb_rele(zsb, FTAG);
2422 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2425 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2426 (void) strcpy(zc->zc_name, dsname);
2427 (void) zfs_ioc_userspace_upgrade(zc);
2428 kmem_free(zc, sizeof (zfs_cmd_t));
2440 * This function is best effort. If it fails to set any of the given properties,
2441 * it continues to set as many as it can and returns the last error
2442 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2443 * with the list of names of all the properties that failed along with the
2444 * corresponding error numbers.
2446 * If every property is set successfully, zero is returned and errlist is not
2450 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2459 nvlist_t *genericnvl = fnvlist_alloc();
2460 nvlist_t *retrynvl = fnvlist_alloc();
2463 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2464 const char *propname = nvpair_name(pair);
2465 zfs_prop_t prop = zfs_name_to_prop(propname);
2468 /* decode the property value */
2470 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2472 attrs = fnvpair_value_nvlist(pair);
2473 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2475 err = SET_ERROR(EINVAL);
2478 /* Validate value type */
2479 if (err == 0 && prop == ZPROP_INVAL) {
2480 if (zfs_prop_user(propname)) {
2481 if (nvpair_type(propval) != DATA_TYPE_STRING)
2482 err = SET_ERROR(EINVAL);
2483 } else if (zfs_prop_userquota(propname)) {
2484 if (nvpair_type(propval) !=
2485 DATA_TYPE_UINT64_ARRAY)
2486 err = SET_ERROR(EINVAL);
2488 err = SET_ERROR(EINVAL);
2490 } else if (err == 0) {
2491 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2492 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2493 err = SET_ERROR(EINVAL);
2494 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2497 intval = fnvpair_value_uint64(propval);
2499 switch (zfs_prop_get_type(prop)) {
2500 case PROP_TYPE_NUMBER:
2502 case PROP_TYPE_STRING:
2503 err = SET_ERROR(EINVAL);
2505 case PROP_TYPE_INDEX:
2506 if (zfs_prop_index_to_string(prop,
2507 intval, &unused) != 0)
2508 err = SET_ERROR(EINVAL);
2512 "unknown property type");
2515 err = SET_ERROR(EINVAL);
2519 /* Validate permissions */
2521 err = zfs_check_settable(dsname, pair, CRED());
2524 err = zfs_prop_set_special(dsname, source, pair);
2527 * For better performance we build up a list of
2528 * properties to set in a single transaction.
2530 err = nvlist_add_nvpair(genericnvl, pair);
2531 } else if (err != 0 && nvl != retrynvl) {
2533 * This may be a spurious error caused by
2534 * receiving quota and reservation out of order.
2535 * Try again in a second pass.
2537 err = nvlist_add_nvpair(retrynvl, pair);
2542 if (errlist != NULL)
2543 fnvlist_add_int32(errlist, propname, err);
2548 if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2553 if (!nvlist_empty(genericnvl) &&
2554 dsl_props_set(dsname, source, genericnvl) != 0) {
2556 * If this fails, we still want to set as many properties as we
2557 * can, so try setting them individually.
2560 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2561 const char *propname = nvpair_name(pair);
2565 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2567 attrs = fnvpair_value_nvlist(pair);
2568 propval = fnvlist_lookup_nvpair(attrs,
2572 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2573 strval = fnvpair_value_string(propval);
2574 err = dsl_prop_set_string(dsname, propname,
2577 intval = fnvpair_value_uint64(propval);
2578 err = dsl_prop_set_int(dsname, propname, source,
2583 if (errlist != NULL) {
2584 fnvlist_add_int32(errlist, propname,
2591 nvlist_free(genericnvl);
2592 nvlist_free(retrynvl);
2598 * Check that all the properties are valid user properties.
2601 zfs_check_userprops(const char *fsname, nvlist_t *nvl)
2603 nvpair_t *pair = NULL;
2606 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2607 const char *propname = nvpair_name(pair);
2609 if (!zfs_prop_user(propname) ||
2610 nvpair_type(pair) != DATA_TYPE_STRING)
2611 return (SET_ERROR(EINVAL));
2613 if ((error = zfs_secpolicy_write_perms(fsname,
2614 ZFS_DELEG_PERM_USERPROP, CRED())))
2617 if (strlen(propname) >= ZAP_MAXNAMELEN)
2618 return (SET_ERROR(ENAMETOOLONG));
2620 if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN)
2621 return (SET_ERROR(E2BIG));
2627 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2631 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2634 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2635 if (nvlist_exists(skipped, nvpair_name(pair)))
2638 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2643 clear_received_props(const char *dsname, nvlist_t *props,
2647 nvlist_t *cleared_props = NULL;
2648 props_skip(props, skipped, &cleared_props);
2649 if (!nvlist_empty(cleared_props)) {
2651 * Acts on local properties until the dataset has received
2652 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2654 zprop_source_t flags = (ZPROP_SRC_NONE |
2655 (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
2656 err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
2658 nvlist_free(cleared_props);
2664 * zc_name name of filesystem
2665 * zc_value name of property to set
2666 * zc_nvlist_src{_size} nvlist of properties to apply
2667 * zc_cookie received properties flag
2670 * zc_nvlist_dst{_size} error for each unapplied received property
2673 zfs_ioc_set_prop(zfs_cmd_t *zc)
2676 boolean_t received = zc->zc_cookie;
2677 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2682 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2683 zc->zc_iflags, &nvl)) != 0)
2687 nvlist_t *origprops;
2689 if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
2690 (void) clear_received_props(zc->zc_name,
2692 nvlist_free(origprops);
2695 error = dsl_prop_set_hasrecvd(zc->zc_name);
2698 errors = fnvlist_alloc();
2700 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
2702 if (zc->zc_nvlist_dst != 0 && errors != NULL) {
2703 (void) put_nvlist(zc, errors);
2706 nvlist_free(errors);
2713 * zc_name name of filesystem
2714 * zc_value name of property to inherit
2715 * zc_cookie revert to received value if TRUE
2720 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2722 const char *propname = zc->zc_value;
2723 zfs_prop_t prop = zfs_name_to_prop(propname);
2724 boolean_t received = zc->zc_cookie;
2725 zprop_source_t source = (received
2726 ? ZPROP_SRC_NONE /* revert to received value, if any */
2727 : ZPROP_SRC_INHERITED); /* explicitly inherit */
2736 * zfs_prop_set_special() expects properties in the form of an
2737 * nvpair with type info.
2739 if (prop == ZPROP_INVAL) {
2740 if (!zfs_prop_user(propname))
2741 return (SET_ERROR(EINVAL));
2743 type = PROP_TYPE_STRING;
2744 } else if (prop == ZFS_PROP_VOLSIZE ||
2745 prop == ZFS_PROP_VERSION) {
2746 return (SET_ERROR(EINVAL));
2748 type = zfs_prop_get_type(prop);
2751 VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2754 case PROP_TYPE_STRING:
2755 VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2757 case PROP_TYPE_NUMBER:
2758 case PROP_TYPE_INDEX:
2759 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2763 return (SET_ERROR(EINVAL));
2766 pair = nvlist_next_nvpair(dummy, NULL);
2767 err = zfs_prop_set_special(zc->zc_name, source, pair);
2770 return (err); /* special property already handled */
2773 * Only check this in the non-received case. We want to allow
2774 * 'inherit -S' to revert non-inheritable properties like quota
2775 * and reservation to the received or default values even though
2776 * they are not considered inheritable.
2778 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2779 return (SET_ERROR(EINVAL));
2782 /* property name has been validated by zfs_secpolicy_inherit_prop() */
2783 return (dsl_prop_inherit(zc->zc_name, zc->zc_value, source));
2787 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2794 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2795 zc->zc_iflags, &props)))
2799 * If the only property is the configfile, then just do a spa_lookup()
2800 * to handle the faulted case.
2802 pair = nvlist_next_nvpair(props, NULL);
2803 if (pair != NULL && strcmp(nvpair_name(pair),
2804 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2805 nvlist_next_nvpair(props, pair) == NULL) {
2806 mutex_enter(&spa_namespace_lock);
2807 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2808 spa_configfile_set(spa, props, B_FALSE);
2809 spa_config_sync(spa, B_FALSE, B_TRUE);
2811 mutex_exit(&spa_namespace_lock);
2818 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2823 error = spa_prop_set(spa, props);
2826 spa_close(spa, FTAG);
2832 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2836 nvlist_t *nvp = NULL;
2838 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2840 * If the pool is faulted, there may be properties we can still
2841 * get (such as altroot and cachefile), so attempt to get them
2844 mutex_enter(&spa_namespace_lock);
2845 if ((spa = spa_lookup(zc->zc_name)) != NULL)
2846 error = spa_prop_get(spa, &nvp);
2847 mutex_exit(&spa_namespace_lock);
2849 error = spa_prop_get(spa, &nvp);
2850 spa_close(spa, FTAG);
2853 if (error == 0 && zc->zc_nvlist_dst != 0)
2854 error = put_nvlist(zc, nvp);
2856 error = SET_ERROR(EFAULT);
2864 * zc_name name of filesystem
2865 * zc_nvlist_src{_size} nvlist of delegated permissions
2866 * zc_perm_action allow/unallow flag
2871 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2874 nvlist_t *fsaclnv = NULL;
2876 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2877 zc->zc_iflags, &fsaclnv)) != 0)
2881 * Verify nvlist is constructed correctly
2883 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2884 nvlist_free(fsaclnv);
2885 return (SET_ERROR(EINVAL));
2889 * If we don't have PRIV_SYS_MOUNT, then validate
2890 * that user is allowed to hand out each permission in
2894 error = secpolicy_zfs(CRED());
2896 if (zc->zc_perm_action == B_FALSE) {
2897 error = dsl_deleg_can_allow(zc->zc_name,
2900 error = dsl_deleg_can_unallow(zc->zc_name,
2906 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2908 nvlist_free(fsaclnv);
2914 * zc_name name of filesystem
2917 * zc_nvlist_src{_size} nvlist of delegated permissions
2920 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
2925 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
2926 error = put_nvlist(zc, nvp);
2935 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2937 zfs_creat_t *zct = arg;
2939 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
2942 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
2946 * os parent objset pointer (NULL if root fs)
2947 * fuids_ok fuids allowed in this version of the spa?
2948 * sa_ok SAs allowed in this version of the spa?
2949 * createprops list of properties requested by creator
2952 * zplprops values for the zplprops we attach to the master node object
2953 * is_ci true if requested file system will be purely case-insensitive
2955 * Determine the settings for utf8only, normalization and
2956 * casesensitivity. Specific values may have been requested by the
2957 * creator and/or we can inherit values from the parent dataset. If
2958 * the file system is of too early a vintage, a creator can not
2959 * request settings for these properties, even if the requested
2960 * setting is the default value. We don't actually want to create dsl
2961 * properties for these, so remove them from the source nvlist after
2965 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
2966 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
2967 nvlist_t *zplprops, boolean_t *is_ci)
2969 uint64_t sense = ZFS_PROP_UNDEFINED;
2970 uint64_t norm = ZFS_PROP_UNDEFINED;
2971 uint64_t u8 = ZFS_PROP_UNDEFINED;
2974 ASSERT(zplprops != NULL);
2977 * Pull out creator prop choices, if any.
2980 (void) nvlist_lookup_uint64(createprops,
2981 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
2982 (void) nvlist_lookup_uint64(createprops,
2983 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
2984 (void) nvlist_remove_all(createprops,
2985 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
2986 (void) nvlist_lookup_uint64(createprops,
2987 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
2988 (void) nvlist_remove_all(createprops,
2989 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
2990 (void) nvlist_lookup_uint64(createprops,
2991 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
2992 (void) nvlist_remove_all(createprops,
2993 zfs_prop_to_name(ZFS_PROP_CASE));
2997 * If the zpl version requested is whacky or the file system
2998 * or pool is version is too "young" to support normalization
2999 * and the creator tried to set a value for one of the props,
3002 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
3003 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
3004 (zplver >= ZPL_VERSION_SA && !sa_ok) ||
3005 (zplver < ZPL_VERSION_NORMALIZATION &&
3006 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
3007 sense != ZFS_PROP_UNDEFINED)))
3008 return (SET_ERROR(ENOTSUP));
3011 * Put the version in the zplprops
3013 VERIFY(nvlist_add_uint64(zplprops,
3014 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3016 if (norm == ZFS_PROP_UNDEFINED &&
3017 (error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm)) != 0)
3019 VERIFY(nvlist_add_uint64(zplprops,
3020 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3023 * If we're normalizing, names must always be valid UTF-8 strings.
3027 if (u8 == ZFS_PROP_UNDEFINED &&
3028 (error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8)) != 0)
3030 VERIFY(nvlist_add_uint64(zplprops,
3031 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3033 if (sense == ZFS_PROP_UNDEFINED &&
3034 (error = zfs_get_zplprop(os, ZFS_PROP_CASE, &sense)) != 0)
3036 VERIFY(nvlist_add_uint64(zplprops,
3037 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3040 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
3046 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
3047 nvlist_t *zplprops, boolean_t *is_ci)
3049 boolean_t fuids_ok, sa_ok;
3050 uint64_t zplver = ZPL_VERSION;
3051 objset_t *os = NULL;
3052 char parentname[ZFS_MAX_DATASET_NAME_LEN];
3058 (void) strlcpy(parentname, dataset, sizeof (parentname));
3059 cp = strrchr(parentname, '/');
3063 if ((error = spa_open(dataset, &spa, FTAG)) != 0)
3066 spa_vers = spa_version(spa);
3067 spa_close(spa, FTAG);
3069 zplver = zfs_zpl_version_map(spa_vers);
3070 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3071 sa_ok = (zplver >= ZPL_VERSION_SA);
3074 * Open parent object set so we can inherit zplprop values.
3076 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
3079 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
3081 dmu_objset_rele(os, FTAG);
3086 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
3087 nvlist_t *zplprops, boolean_t *is_ci)
3091 uint64_t zplver = ZPL_VERSION;
3094 zplver = zfs_zpl_version_map(spa_vers);
3095 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3096 sa_ok = (zplver >= ZPL_VERSION_SA);
3098 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
3099 createprops, zplprops, is_ci);
3105 * "type" -> dmu_objset_type_t (int32)
3106 * (optional) "props" -> { prop -> value }
3109 * outnvl: propname -> error code (int32)
3112 zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3115 zfs_creat_t zct = { 0 };
3116 nvlist_t *nvprops = NULL;
3117 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
3119 dmu_objset_type_t type;
3120 boolean_t is_insensitive = B_FALSE;
3122 if (nvlist_lookup_int32(innvl, "type", &type32) != 0)
3123 return (SET_ERROR(EINVAL));
3125 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3129 cbfunc = zfs_create_cb;
3133 cbfunc = zvol_create_cb;
3140 if (strchr(fsname, '@') ||
3141 strchr(fsname, '%'))
3142 return (SET_ERROR(EINVAL));
3144 zct.zct_props = nvprops;
3147 return (SET_ERROR(EINVAL));
3149 if (type == DMU_OST_ZVOL) {
3150 uint64_t volsize, volblocksize;
3152 if (nvprops == NULL)
3153 return (SET_ERROR(EINVAL));
3154 if (nvlist_lookup_uint64(nvprops,
3155 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3156 return (SET_ERROR(EINVAL));
3158 if ((error = nvlist_lookup_uint64(nvprops,
3159 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3160 &volblocksize)) != 0 && error != ENOENT)
3161 return (SET_ERROR(EINVAL));
3164 volblocksize = zfs_prop_default_numeric(
3165 ZFS_PROP_VOLBLOCKSIZE);
3167 if ((error = zvol_check_volblocksize(fsname,
3168 volblocksize)) != 0 ||
3169 (error = zvol_check_volsize(volsize,
3170 volblocksize)) != 0)
3172 } else if (type == DMU_OST_ZFS) {
3176 * We have to have normalization and
3177 * case-folding flags correct when we do the
3178 * file system creation, so go figure them out
3181 VERIFY(nvlist_alloc(&zct.zct_zplprops,
3182 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3183 error = zfs_fill_zplprops(fsname, nvprops,
3184 zct.zct_zplprops, &is_insensitive);
3186 nvlist_free(zct.zct_zplprops);
3191 error = dmu_objset_create(fsname, type,
3192 is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
3193 nvlist_free(zct.zct_zplprops);
3196 * It would be nice to do this atomically.
3199 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3206 * Volumes will return EBUSY and cannot be destroyed
3207 * until all asynchronous minor handling has completed.
3208 * Wait for the spa_zvol_taskq to drain then retry.
3210 error2 = dsl_destroy_head(fsname);
3211 while ((error2 == EBUSY) && (type == DMU_OST_ZVOL)) {
3212 error2 = spa_open(fsname, &spa, FTAG);
3214 taskq_wait(spa->spa_zvol_taskq);
3215 spa_close(spa, FTAG);
3217 error2 = dsl_destroy_head(fsname);
3226 * "origin" -> name of origin snapshot
3227 * (optional) "props" -> { prop -> value }
3231 * outnvl: propname -> error code (int32)
3234 zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3237 nvlist_t *nvprops = NULL;
3240 if (nvlist_lookup_string(innvl, "origin", &origin_name) != 0)
3241 return (SET_ERROR(EINVAL));
3242 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3244 if (strchr(fsname, '@') ||
3245 strchr(fsname, '%'))
3246 return (SET_ERROR(EINVAL));
3248 if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3249 return (SET_ERROR(EINVAL));
3250 error = dmu_objset_clone(fsname, origin_name);
3255 * It would be nice to do this atomically.
3258 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3261 (void) dsl_destroy_head(fsname);
3268 * "snaps" -> { snapshot1, snapshot2 }
3269 * (optional) "props" -> { prop -> value (string) }
3272 * outnvl: snapshot -> error code (int32)
3275 zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3278 nvlist_t *props = NULL;
3280 nvpair_t *pair, *pair2;
3282 (void) nvlist_lookup_nvlist(innvl, "props", &props);
3283 if ((error = zfs_check_userprops(poolname, props)) != 0)
3286 if (!nvlist_empty(props) &&
3287 zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3288 return (SET_ERROR(ENOTSUP));
3290 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3291 return (SET_ERROR(EINVAL));
3292 poollen = strlen(poolname);
3293 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3294 pair = nvlist_next_nvpair(snaps, pair)) {
3295 const char *name = nvpair_name(pair);
3296 const char *cp = strchr(name, '@');
3299 * The snap name must contain an @, and the part after it must
3300 * contain only valid characters.
3303 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3304 return (SET_ERROR(EINVAL));
3307 * The snap must be in the specified pool.
3309 if (strncmp(name, poolname, poollen) != 0 ||
3310 (name[poollen] != '/' && name[poollen] != '@'))
3311 return (SET_ERROR(EXDEV));
3313 /* This must be the only snap of this fs. */
3314 for (pair2 = nvlist_next_nvpair(snaps, pair);
3315 pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
3316 if (strncmp(name, nvpair_name(pair2), cp - name + 1)
3318 return (SET_ERROR(EXDEV));
3323 error = dsl_dataset_snapshot(snaps, props, outnvl);
3329 * innvl: "message" -> string
3333 zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
3341 * The poolname in the ioctl is not set, we get it from the TSD,
3342 * which was set at the end of the last successful ioctl that allows
3343 * logging. The secpolicy func already checked that it is set.
3344 * Only one log ioctl is allowed after each successful ioctl, so
3345 * we clear the TSD here.
3347 poolname = tsd_get(zfs_allow_log_key);
3348 (void) tsd_set(zfs_allow_log_key, NULL);
3349 error = spa_open(poolname, &spa, FTAG);
3354 if (nvlist_lookup_string(innvl, "message", &message) != 0) {
3355 spa_close(spa, FTAG);
3356 return (SET_ERROR(EINVAL));
3359 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
3360 spa_close(spa, FTAG);
3361 return (SET_ERROR(ENOTSUP));
3364 error = spa_history_log(spa, message);
3365 spa_close(spa, FTAG);
3370 * The dp_config_rwlock must not be held when calling this, because the
3371 * unmount may need to write out data.
3373 * This function is best-effort. Callers must deal gracefully if it
3374 * remains mounted (or is remounted after this call).
3376 * Returns 0 if the argument is not a snapshot, or it is not currently a
3377 * filesystem, or we were able to unmount it. Returns error code otherwise.
3380 zfs_unmount_snap(const char *snapname)
3384 if (strchr(snapname, '@') == NULL)
3387 err = zfsctl_snapshot_unmount((char *)snapname, MNT_FORCE);
3388 if (err != 0 && err != ENOENT)
3389 return (SET_ERROR(err));
3396 zfs_unmount_snap_cb(const char *snapname, void *arg)
3398 return (zfs_unmount_snap(snapname));
3402 * When a clone is destroyed, its origin may also need to be destroyed,
3403 * in which case it must be unmounted. This routine will do that unmount
3407 zfs_destroy_unmount_origin(const char *fsname)
3413 error = dmu_objset_hold(fsname, FTAG, &os);
3416 ds = dmu_objset_ds(os);
3417 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
3418 char originname[ZFS_MAX_DATASET_NAME_LEN];
3419 dsl_dataset_name(ds->ds_prev, originname);
3420 dmu_objset_rele(os, FTAG);
3421 (void) zfs_unmount_snap(originname);
3423 dmu_objset_rele(os, FTAG);
3429 * "snaps" -> { snapshot1, snapshot2 }
3430 * (optional boolean) "defer"
3433 * outnvl: snapshot -> error code (int32)
3437 zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3443 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3444 return (SET_ERROR(EINVAL));
3445 defer = nvlist_exists(innvl, "defer");
3447 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3448 pair = nvlist_next_nvpair(snaps, pair)) {
3449 (void) zfs_unmount_snap(nvpair_name(pair));
3452 return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
3456 * Create bookmarks. Bookmark names are of the form <fs>#<bmark>.
3457 * All bookmarks must be in the same pool.
3460 * bookmark1 -> snapshot1, bookmark2 -> snapshot2
3463 * outnvl: bookmark -> error code (int32)
3468 zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3470 nvpair_t *pair, *pair2;
3472 for (pair = nvlist_next_nvpair(innvl, NULL);
3473 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3477 * Verify the snapshot argument.
3479 if (nvpair_value_string(pair, &snap_name) != 0)
3480 return (SET_ERROR(EINVAL));
3483 /* Verify that the keys (bookmarks) are unique */
3484 for (pair2 = nvlist_next_nvpair(innvl, pair);
3485 pair2 != NULL; pair2 = nvlist_next_nvpair(innvl, pair2)) {
3486 if (strcmp(nvpair_name(pair), nvpair_name(pair2)) == 0)
3487 return (SET_ERROR(EINVAL));
3491 return (dsl_bookmark_create(innvl, outnvl));
3496 * property 1, property 2, ...
3500 * bookmark name 1 -> { property 1, property 2, ... },
3501 * bookmark name 2 -> { property 1, property 2, ... }
3506 zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3508 return (dsl_get_bookmarks(fsname, innvl, outnvl));
3513 * bookmark name 1, bookmark name 2
3516 * outnvl: bookmark -> error code (int32)
3520 zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl,
3526 poollen = strlen(poolname);
3527 for (pair = nvlist_next_nvpair(innvl, NULL);
3528 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3529 const char *name = nvpair_name(pair);
3530 const char *cp = strchr(name, '#');
3533 * The bookmark name must contain an #, and the part after it
3534 * must contain only valid characters.
3537 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3538 return (SET_ERROR(EINVAL));
3541 * The bookmark must be in the specified pool.
3543 if (strncmp(name, poolname, poollen) != 0 ||
3544 (name[poollen] != '/' && name[poollen] != '#'))
3545 return (SET_ERROR(EXDEV));
3548 error = dsl_bookmark_destroy(innvl, outnvl);
3554 * zc_name name of dataset to destroy
3555 * zc_objset_type type of objset
3556 * zc_defer_destroy mark for deferred destroy
3561 zfs_ioc_destroy(zfs_cmd_t *zc)
3565 if (zc->zc_objset_type == DMU_OST_ZFS) {
3566 err = zfs_unmount_snap(zc->zc_name);
3571 if (strchr(zc->zc_name, '@'))
3572 err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
3574 err = dsl_destroy_head(zc->zc_name);
3580 * fsname is name of dataset to rollback (to most recent snapshot)
3582 * innvl is not used.
3584 * outnvl: "target" -> name of most recent snapshot
3589 zfs_ioc_rollback(const char *fsname, nvlist_t *args, nvlist_t *outnvl)
3594 if (get_zfs_sb(fsname, &zsb) == 0) {
3595 error = zfs_suspend_fs(zsb);
3599 error = dsl_dataset_rollback(fsname, zsb, outnvl);
3600 resume_err = zfs_resume_fs(zsb, fsname);
3601 error = error ? error : resume_err;
3603 deactivate_super(zsb->z_sb);
3605 error = dsl_dataset_rollback(fsname, NULL, outnvl);
3611 recursive_unmount(const char *fsname, void *arg)
3613 const char *snapname = arg;
3617 fullname = kmem_asprintf("%s@%s", fsname, snapname);
3618 error = zfs_unmount_snap(fullname);
3626 * zc_name old name of dataset
3627 * zc_value new name of dataset
3628 * zc_cookie recursive flag (only valid for snapshots)
3633 zfs_ioc_rename(zfs_cmd_t *zc)
3635 boolean_t recursive = zc->zc_cookie & 1;
3638 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3639 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3640 strchr(zc->zc_value, '%'))
3641 return (SET_ERROR(EINVAL));
3643 at = strchr(zc->zc_name, '@');
3645 /* snaps must be in same fs */
3648 if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
3649 return (SET_ERROR(EXDEV));
3651 if (zc->zc_objset_type == DMU_OST_ZFS) {
3652 error = dmu_objset_find(zc->zc_name,
3653 recursive_unmount, at + 1,
3654 recursive ? DS_FIND_CHILDREN : 0);
3660 error = dsl_dataset_rename_snapshot(zc->zc_name,
3661 at + 1, strchr(zc->zc_value, '@') + 1, recursive);
3666 return (dsl_dir_rename(zc->zc_name, zc->zc_value));
3671 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
3673 const char *propname = nvpair_name(pair);
3674 boolean_t issnap = (strchr(dsname, '@') != NULL);
3675 zfs_prop_t prop = zfs_name_to_prop(propname);
3679 if (prop == ZPROP_INVAL) {
3680 if (zfs_prop_user(propname)) {
3681 if ((err = zfs_secpolicy_write_perms(dsname,
3682 ZFS_DELEG_PERM_USERPROP, cr)))
3687 if (!issnap && zfs_prop_userquota(propname)) {
3688 const char *perm = NULL;
3689 const char *uq_prefix =
3690 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
3691 const char *gq_prefix =
3692 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
3694 if (strncmp(propname, uq_prefix,
3695 strlen(uq_prefix)) == 0) {
3696 perm = ZFS_DELEG_PERM_USERQUOTA;
3697 } else if (strncmp(propname, gq_prefix,
3698 strlen(gq_prefix)) == 0) {
3699 perm = ZFS_DELEG_PERM_GROUPQUOTA;
3701 /* USERUSED and GROUPUSED are read-only */
3702 return (SET_ERROR(EINVAL));
3705 if ((err = zfs_secpolicy_write_perms(dsname, perm, cr)))
3710 return (SET_ERROR(EINVAL));
3714 return (SET_ERROR(EINVAL));
3716 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
3718 * dsl_prop_get_all_impl() returns properties in this
3722 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
3723 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3728 * Check that this value is valid for this pool version
3731 case ZFS_PROP_COMPRESSION:
3733 * If the user specified gzip compression, make sure
3734 * the SPA supports it. We ignore any errors here since
3735 * we'll catch them later.
3737 if (nvpair_value_uint64(pair, &intval) == 0) {
3738 if (intval >= ZIO_COMPRESS_GZIP_1 &&
3739 intval <= ZIO_COMPRESS_GZIP_9 &&
3740 zfs_earlier_version(dsname,
3741 SPA_VERSION_GZIP_COMPRESSION)) {
3742 return (SET_ERROR(ENOTSUP));
3745 if (intval == ZIO_COMPRESS_ZLE &&
3746 zfs_earlier_version(dsname,
3747 SPA_VERSION_ZLE_COMPRESSION))
3748 return (SET_ERROR(ENOTSUP));
3750 if (intval == ZIO_COMPRESS_LZ4) {
3753 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3756 if (!spa_feature_is_enabled(spa,
3757 SPA_FEATURE_LZ4_COMPRESS)) {
3758 spa_close(spa, FTAG);
3759 return (SET_ERROR(ENOTSUP));
3761 spa_close(spa, FTAG);
3765 * If this is a bootable dataset then
3766 * verify that the compression algorithm
3767 * is supported for booting. We must return
3768 * something other than ENOTSUP since it
3769 * implies a downrev pool version.
3771 if (zfs_is_bootfs(dsname) &&
3772 !BOOTFS_COMPRESS_VALID(intval)) {
3773 return (SET_ERROR(ERANGE));
3778 case ZFS_PROP_COPIES:
3779 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
3780 return (SET_ERROR(ENOTSUP));
3783 case ZFS_PROP_DEDUP:
3784 if (zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
3785 return (SET_ERROR(ENOTSUP));
3788 case ZFS_PROP_VOLBLOCKSIZE:
3789 case ZFS_PROP_RECORDSIZE:
3790 /* Record sizes above 128k need the feature to be enabled */
3791 if (nvpair_value_uint64(pair, &intval) == 0 &&
3792 intval > SPA_OLD_MAXBLOCKSIZE) {
3796 * If this is a bootable dataset then
3797 * we don't allow large (>128K) blocks,
3798 * because GRUB doesn't support them.
3800 if (zfs_is_bootfs(dsname) &&
3801 intval > SPA_OLD_MAXBLOCKSIZE) {
3802 return (SET_ERROR(ERANGE));
3806 * We don't allow setting the property above 1MB,
3807 * unless the tunable has been changed.
3809 if (intval > zfs_max_recordsize ||
3810 intval > SPA_MAXBLOCKSIZE)
3811 return (SET_ERROR(ERANGE));
3813 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3816 if (!spa_feature_is_enabled(spa,
3817 SPA_FEATURE_LARGE_BLOCKS)) {
3818 spa_close(spa, FTAG);
3819 return (SET_ERROR(ENOTSUP));
3821 spa_close(spa, FTAG);
3825 case ZFS_PROP_DNODESIZE:
3826 /* Dnode sizes above 512 need the feature to be enabled */
3827 if (nvpair_value_uint64(pair, &intval) == 0 &&
3828 intval != ZFS_DNSIZE_LEGACY) {
3832 * If this is a bootable dataset then
3833 * we don't allow large (>512B) dnodes,
3834 * because GRUB doesn't support them.
3836 if (zfs_is_bootfs(dsname) &&
3837 intval != ZFS_DNSIZE_LEGACY) {
3838 return (SET_ERROR(EDOM));
3841 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3844 if (!spa_feature_is_enabled(spa,
3845 SPA_FEATURE_LARGE_DNODE)) {
3846 spa_close(spa, FTAG);
3847 return (SET_ERROR(ENOTSUP));
3849 spa_close(spa, FTAG);
3853 case ZFS_PROP_SHARESMB:
3854 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
3855 return (SET_ERROR(ENOTSUP));
3858 case ZFS_PROP_ACLINHERIT:
3859 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3860 nvpair_value_uint64(pair, &intval) == 0) {
3861 if (intval == ZFS_ACL_PASSTHROUGH_X &&
3862 zfs_earlier_version(dsname,
3863 SPA_VERSION_PASSTHROUGH_X))
3864 return (SET_ERROR(ENOTSUP));
3871 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
3875 * Removes properties from the given props list that fail permission checks
3876 * needed to clear them and to restore them in case of a receive error. For each
3877 * property, make sure we have both set and inherit permissions.
3879 * Returns the first error encountered if any permission checks fail. If the
3880 * caller provides a non-NULL errlist, it also gives the complete list of names
3881 * of all the properties that failed a permission check along with the
3882 * corresponding error numbers. The caller is responsible for freeing the
3885 * If every property checks out successfully, zero is returned and the list
3886 * pointed at by errlist is NULL.
3889 zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
3892 nvpair_t *pair, *next_pair;
3899 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3901 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
3902 (void) strcpy(zc->zc_name, dataset);
3903 pair = nvlist_next_nvpair(props, NULL);
3904 while (pair != NULL) {
3905 next_pair = nvlist_next_nvpair(props, pair);
3907 (void) strcpy(zc->zc_value, nvpair_name(pair));
3908 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
3909 (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
3910 VERIFY(nvlist_remove_nvpair(props, pair) == 0);
3911 VERIFY(nvlist_add_int32(errors,
3912 zc->zc_value, err) == 0);
3916 kmem_free(zc, sizeof (zfs_cmd_t));
3918 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
3919 nvlist_free(errors);
3922 VERIFY(nvpair_value_int32(pair, &rv) == 0);
3925 if (errlist == NULL)
3926 nvlist_free(errors);
3934 propval_equals(nvpair_t *p1, nvpair_t *p2)
3936 if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
3937 /* dsl_prop_get_all_impl() format */
3939 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
3940 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3944 if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
3946 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
3947 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3951 if (nvpair_type(p1) != nvpair_type(p2))
3954 if (nvpair_type(p1) == DATA_TYPE_STRING) {
3955 char *valstr1, *valstr2;
3957 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
3958 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
3959 return (strcmp(valstr1, valstr2) == 0);
3961 uint64_t intval1, intval2;
3963 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
3964 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
3965 return (intval1 == intval2);
3970 * Remove properties from props if they are not going to change (as determined
3971 * by comparison with origprops). Remove them from origprops as well, since we
3972 * do not need to clear or restore properties that won't change.
3975 props_reduce(nvlist_t *props, nvlist_t *origprops)
3977 nvpair_t *pair, *next_pair;
3979 if (origprops == NULL)
3980 return; /* all props need to be received */
3982 pair = nvlist_next_nvpair(props, NULL);
3983 while (pair != NULL) {
3984 const char *propname = nvpair_name(pair);
3987 next_pair = nvlist_next_nvpair(props, pair);
3989 if ((nvlist_lookup_nvpair(origprops, propname,
3990 &match) != 0) || !propval_equals(pair, match))
3991 goto next; /* need to set received value */
3993 /* don't clear the existing received value */
3994 (void) nvlist_remove_nvpair(origprops, match);
3995 /* don't bother receiving the property */
3996 (void) nvlist_remove_nvpair(props, pair);
4003 * Extract properties that cannot be set PRIOR to the receipt of a dataset.
4004 * For example, refquota cannot be set until after the receipt of a dataset,
4005 * because in replication streams, an older/earlier snapshot may exceed the
4006 * refquota. We want to receive the older/earlier snapshot, but setting
4007 * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent
4008 * the older/earlier snapshot from being received (with EDQUOT).
4010 * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario.
4012 * libzfs will need to be judicious handling errors encountered by props
4013 * extracted by this function.
4016 extract_delay_props(nvlist_t *props)
4018 nvlist_t *delayprops;
4019 nvpair_t *nvp, *tmp;
4020 static const zfs_prop_t delayable[] = { ZFS_PROP_REFQUOTA, 0 };
4023 VERIFY(nvlist_alloc(&delayprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4025 for (nvp = nvlist_next_nvpair(props, NULL); nvp != NULL;
4026 nvp = nvlist_next_nvpair(props, nvp)) {
4028 * strcmp() is safe because zfs_prop_to_name() always returns
4031 for (i = 0; delayable[i] != 0; i++) {
4032 if (strcmp(zfs_prop_to_name(delayable[i]),
4033 nvpair_name(nvp)) == 0) {
4037 if (delayable[i] != 0) {
4038 tmp = nvlist_prev_nvpair(props, nvp);
4039 VERIFY(nvlist_add_nvpair(delayprops, nvp) == 0);
4040 VERIFY(nvlist_remove_nvpair(props, nvp) == 0);
4045 if (nvlist_empty(delayprops)) {
4046 nvlist_free(delayprops);
4049 return (delayprops);
4053 static boolean_t zfs_ioc_recv_inject_err;
4057 * On failure the 'errors' nvlist may be allocated and will contain a
4058 * descriptions of the failures. It's the callers responsibilty to free.
4061 zfs_ioc_recv_impl(char *tofs, char *tosnap, char *origin,
4062 nvlist_t *props, boolean_t force, boolean_t resumable, int input_fd,
4063 dmu_replay_record_t *begin_record, int cleanup_fd, uint64_t *read_bytes,
4064 uint64_t *errflags, uint64_t *action_handle, nvlist_t **errors)
4066 dmu_recv_cookie_t drc;
4068 int props_error = 0;
4070 nvlist_t *delayprops = NULL; /* sent properties applied post-receive */
4071 nvlist_t *origprops = NULL; /* existing properties */
4072 boolean_t first_recvd_props = B_FALSE;
4076 input_fp = getf(input_fd);
4077 if (input_fp == NULL)
4078 return (SET_ERROR(EBADF));
4080 error = dmu_recv_begin(tofs, tosnap,
4081 begin_record, force, resumable, origin, &drc);
4087 *errors = fnvlist_alloc();
4090 * Set properties before we receive the stream so that they are applied
4091 * to the new data. Note that we must call dmu_recv_stream() if
4092 * dmu_recv_begin() succeeds.
4094 if (props != NULL && !drc.drc_newfs) {
4095 if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
4096 SPA_VERSION_RECVD_PROPS &&
4097 !dsl_prop_get_hasrecvd(tofs))
4098 first_recvd_props = B_TRUE;
4101 * If new received properties are supplied, they are to
4102 * completely replace the existing received properties, so stash
4103 * away the existing ones.
4105 if (dsl_prop_get_received(tofs, &origprops) == 0) {
4106 nvlist_t *errlist = NULL;
4108 * Don't bother writing a property if its value won't
4109 * change (and avoid the unnecessary security checks).
4111 * The first receive after SPA_VERSION_RECVD_PROPS is a
4112 * special case where we blow away all local properties
4115 if (!first_recvd_props)
4116 props_reduce(props, origprops);
4117 if (zfs_check_clearable(tofs, origprops, &errlist) != 0)
4118 (void) nvlist_merge(*errors, errlist, 0);
4119 nvlist_free(errlist);
4121 if (clear_received_props(tofs, origprops,
4122 first_recvd_props ? NULL : props) != 0)
4123 *errflags |= ZPROP_ERR_NOCLEAR;
4125 *errflags |= ZPROP_ERR_NOCLEAR;
4129 if (props != NULL) {
4130 props_error = dsl_prop_set_hasrecvd(tofs);
4132 if (props_error == 0) {
4133 delayprops = extract_delay_props(props);
4134 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4139 off = input_fp->f_offset;
4140 error = dmu_recv_stream(&drc, input_fp->f_vnode, &off, cleanup_fd,
4144 zfs_sb_t *zsb = NULL;
4146 if (get_zfs_sb(tofs, &zsb) == 0) {
4150 error = zfs_suspend_fs(zsb);
4152 * If the suspend fails, then the recv_end will
4153 * likely also fail, and clean up after itself.
4155 end_err = dmu_recv_end(&drc, zsb);
4157 error = zfs_resume_fs(zsb, tofs);
4158 error = error ? error : end_err;
4159 deactivate_super(zsb->z_sb);
4161 error = dmu_recv_end(&drc, NULL);
4164 /* Set delayed properties now, after we're done receiving. */
4165 if (delayprops != NULL && error == 0) {
4166 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4167 delayprops, *errors);
4171 if (delayprops != NULL) {
4173 * Merge delayed props back in with initial props, in case
4174 * we're DEBUG and zfs_ioc_recv_inject_err is set (which means
4175 * we have to make sure clear_received_props() includes
4176 * the delayed properties).
4178 * Since zfs_ioc_recv_inject_err is only in DEBUG kernels,
4179 * using ASSERT() will be just like a VERIFY.
4181 ASSERT(nvlist_merge(props, delayprops, 0) == 0);
4182 nvlist_free(delayprops);
4186 *read_bytes = off - input_fp->f_offset;
4187 if (VOP_SEEK(input_fp->f_vnode, input_fp->f_offset, &off, NULL) == 0)
4188 input_fp->f_offset = off;
4191 if (zfs_ioc_recv_inject_err) {
4192 zfs_ioc_recv_inject_err = B_FALSE;
4198 * On error, restore the original props.
4200 if (error != 0 && props != NULL && !drc.drc_newfs) {
4201 if (clear_received_props(tofs, props, NULL) != 0) {
4203 * We failed to clear the received properties.
4204 * Since we may have left a $recvd value on the
4205 * system, we can't clear the $hasrecvd flag.
4207 *errflags |= ZPROP_ERR_NORESTORE;
4208 } else if (first_recvd_props) {
4209 dsl_prop_unset_hasrecvd(tofs);
4212 if (origprops == NULL && !drc.drc_newfs) {
4213 /* We failed to stash the original properties. */
4214 *errflags |= ZPROP_ERR_NORESTORE;
4218 * dsl_props_set() will not convert RECEIVED to LOCAL on or
4219 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
4220 * explictly if we're restoring local properties cleared in the
4221 * first new-style receive.
4223 if (origprops != NULL &&
4224 zfs_set_prop_nvlist(tofs, (first_recvd_props ?
4225 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
4226 origprops, NULL) != 0) {
4228 * We stashed the original properties but failed to
4231 *errflags |= ZPROP_ERR_NORESTORE;
4236 nvlist_free(origprops);
4239 error = props_error;
4246 * zc_name name of containing filesystem (unused)
4247 * zc_nvlist_src{_size} nvlist of properties to apply
4248 * zc_value name of snapshot to create
4249 * zc_string name of clone origin (if DRR_FLAG_CLONE)
4250 * zc_cookie file descriptor to recv from
4251 * zc_begin_record the BEGIN record of the stream (not byteswapped)
4252 * zc_guid force flag
4253 * zc_cleanup_fd cleanup-on-exit file descriptor
4254 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
4257 * zc_cookie number of bytes read
4258 * zc_obj zprop_errflags_t
4259 * zc_action_handle handle for this guid/ds mapping
4260 * zc_nvlist_dst{_size} error for each unapplied received property
4263 zfs_ioc_recv(zfs_cmd_t *zc)
4265 dmu_replay_record_t begin_record;
4266 nvlist_t *errors = NULL;
4267 nvlist_t *props = NULL;
4268 char *origin = NULL;
4270 char tofs[ZFS_MAX_DATASET_NAME_LEN];
4273 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4274 strchr(zc->zc_value, '@') == NULL ||
4275 strchr(zc->zc_value, '%'))
4276 return (SET_ERROR(EINVAL));
4278 (void) strcpy(tofs, zc->zc_value);
4279 tosnap = strchr(tofs, '@');
4282 if (zc->zc_nvlist_src != 0 &&
4283 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
4284 zc->zc_iflags, &props)) != 0)
4287 if (zc->zc_string[0])
4288 origin = zc->zc_string;
4290 begin_record.drr_type = DRR_BEGIN;
4291 begin_record.drr_payloadlen = 0;
4292 begin_record.drr_u.drr_begin = zc->zc_begin_record;
4294 error = zfs_ioc_recv_impl(tofs, tosnap, origin, props, zc->zc_guid,
4295 B_FALSE, zc->zc_cookie, &begin_record, zc->zc_cleanup_fd,
4296 &zc->zc_cookie, &zc->zc_obj, &zc->zc_action_handle, &errors);
4300 * Now that all props, initial and delayed, are set, report the prop
4301 * errors to the caller.
4303 if (zc->zc_nvlist_dst_size != 0 && errors != NULL &&
4304 (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
4305 put_nvlist(zc, errors) != 0)) {
4307 * Caller made zc->zc_nvlist_dst less than the minimum expected
4308 * size or supplied an invalid address.
4310 error = SET_ERROR(EINVAL);
4313 nvlist_free(errors);
4320 * "snapname" -> full name of the snapshot to create
4321 * (optional) "props" -> properties to set (nvlist)
4322 * (optional) "origin" -> name of clone origin (DRR_FLAG_CLONE)
4323 * "begin_record" -> non-byteswapped dmu_replay_record_t
4324 * "input_fd" -> file descriptor to read stream from (int32)
4325 * (optional) "force" -> force flag (value ignored)
4326 * (optional) "resumable" -> resumable flag (value ignored)
4327 * (optional) "cleanup_fd" -> cleanup-on-exit file descriptor
4328 * (optional) "action_handle" -> handle for this guid/ds mapping
4332 * "read_bytes" -> number of bytes read
4333 * "error_flags" -> zprop_errflags_t
4334 * "action_handle" -> handle for this guid/ds mapping
4335 * "errors" -> error for each unapplied received property (nvlist)
4339 zfs_ioc_recv_new(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
4341 dmu_replay_record_t *begin_record;
4342 uint_t begin_record_size;
4343 nvlist_t *errors = NULL;
4344 nvlist_t *props = NULL;
4345 char *snapname = NULL;
4346 char *origin = NULL;
4348 char tofs[ZFS_MAX_DATASET_NAME_LEN];
4350 boolean_t resumable;
4351 uint64_t action_handle = 0;
4352 uint64_t read_bytes = 0;
4353 uint64_t errflags = 0;
4355 int cleanup_fd = -1;
4358 error = nvlist_lookup_string(innvl, "snapname", &snapname);
4360 return (SET_ERROR(EINVAL));
4362 if (dataset_namecheck(snapname, NULL, NULL) != 0 ||
4363 strchr(snapname, '@') == NULL ||
4364 strchr(snapname, '%'))
4365 return (SET_ERROR(EINVAL));
4367 (void) strcpy(tofs, snapname);
4368 tosnap = strchr(tofs, '@');
4371 error = nvlist_lookup_string(innvl, "origin", &origin);
4372 if (error && error != ENOENT)
4375 error = nvlist_lookup_byte_array(innvl, "begin_record",
4376 (uchar_t **) &begin_record, &begin_record_size);
4377 if (error != 0 || begin_record_size != sizeof (*begin_record))
4378 return (SET_ERROR(EINVAL));
4380 error = nvlist_lookup_int32(innvl, "input_fd", &input_fd);
4382 return (SET_ERROR(EINVAL));
4384 force = nvlist_exists(innvl, "force");
4385 resumable = nvlist_exists(innvl, "resumable");
4387 error = nvlist_lookup_int32(innvl, "cleanup_fd", &cleanup_fd);
4388 if (error && error != ENOENT)
4391 error = nvlist_lookup_uint64(innvl, "action_handle", &action_handle);
4392 if (error && error != ENOENT)
4395 error = nvlist_lookup_nvlist(innvl, "props", &props);
4396 if (error && error != ENOENT)
4399 error = zfs_ioc_recv_impl(tofs, tosnap, origin, props, force,
4400 resumable, input_fd, begin_record, cleanup_fd, &read_bytes,
4401 &errflags, &action_handle, &errors);
4403 fnvlist_add_uint64(outnvl, "read_bytes", read_bytes);
4404 fnvlist_add_uint64(outnvl, "error_flags", errflags);
4405 fnvlist_add_uint64(outnvl, "action_handle", action_handle);
4406 fnvlist_add_nvlist(outnvl, "errors", errors);
4408 nvlist_free(errors);
4416 * zc_name name of snapshot to send
4417 * zc_cookie file descriptor to send stream to
4418 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
4419 * zc_sendobj objsetid of snapshot to send
4420 * zc_fromobj objsetid of incremental fromsnap (may be zero)
4421 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
4422 * output size in zc_objset_type.
4423 * zc_flags lzc_send_flags
4426 * zc_objset_type estimated size, if zc_guid is set
4429 zfs_ioc_send(zfs_cmd_t *zc)
4433 boolean_t estimate = (zc->zc_guid != 0);
4434 boolean_t embedok = (zc->zc_flags & 0x1);
4435 boolean_t large_block_ok = (zc->zc_flags & 0x2);
4437 if (zc->zc_obj != 0) {
4439 dsl_dataset_t *tosnap;
4441 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4445 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
4447 dsl_pool_rele(dp, FTAG);
4451 if (dsl_dir_is_clone(tosnap->ds_dir))
4453 dsl_dir_phys(tosnap->ds_dir)->dd_origin_obj;
4454 dsl_dataset_rele(tosnap, FTAG);
4455 dsl_pool_rele(dp, FTAG);
4460 dsl_dataset_t *tosnap;
4461 dsl_dataset_t *fromsnap = NULL;
4463 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4467 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
4469 dsl_pool_rele(dp, FTAG);
4473 if (zc->zc_fromobj != 0) {
4474 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
4477 dsl_dataset_rele(tosnap, FTAG);
4478 dsl_pool_rele(dp, FTAG);
4483 error = dmu_send_estimate(tosnap, fromsnap,
4484 &zc->zc_objset_type);
4486 if (fromsnap != NULL)
4487 dsl_dataset_rele(fromsnap, FTAG);
4488 dsl_dataset_rele(tosnap, FTAG);
4489 dsl_pool_rele(dp, FTAG);
4491 file_t *fp = getf(zc->zc_cookie);
4493 return (SET_ERROR(EBADF));
4496 error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
4497 zc->zc_fromobj, embedok, large_block_ok,
4498 zc->zc_cookie, fp->f_vnode, &off);
4500 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4502 releasef(zc->zc_cookie);
4509 * zc_name name of snapshot on which to report progress
4510 * zc_cookie file descriptor of send stream
4513 * zc_cookie number of bytes written in send stream thus far
4516 zfs_ioc_send_progress(zfs_cmd_t *zc)
4520 dmu_sendarg_t *dsp = NULL;
4523 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4527 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
4529 dsl_pool_rele(dp, FTAG);
4533 mutex_enter(&ds->ds_sendstream_lock);
4536 * Iterate over all the send streams currently active on this dataset.
4537 * If there's one which matches the specified file descriptor _and_ the
4538 * stream was started by the current process, return the progress of
4542 for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
4543 dsp = list_next(&ds->ds_sendstreams, dsp)) {
4544 if (dsp->dsa_outfd == zc->zc_cookie &&
4545 dsp->dsa_proc->group_leader == curproc->group_leader)
4550 zc->zc_cookie = *(dsp->dsa_off);
4552 error = SET_ERROR(ENOENT);
4554 mutex_exit(&ds->ds_sendstream_lock);
4555 dsl_dataset_rele(ds, FTAG);
4556 dsl_pool_rele(dp, FTAG);
4561 zfs_ioc_inject_fault(zfs_cmd_t *zc)
4565 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
4566 &zc->zc_inject_record);
4569 zc->zc_guid = (uint64_t)id;
4575 zfs_ioc_clear_fault(zfs_cmd_t *zc)
4577 return (zio_clear_fault((int)zc->zc_guid));
4581 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
4583 int id = (int)zc->zc_guid;
4586 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
4587 &zc->zc_inject_record);
4595 zfs_ioc_error_log(zfs_cmd_t *zc)
4599 size_t count = (size_t)zc->zc_nvlist_dst_size;
4601 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
4604 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
4607 zc->zc_nvlist_dst_size = count;
4609 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
4611 spa_close(spa, FTAG);
4617 zfs_ioc_clear(zfs_cmd_t *zc)
4624 * On zpool clear we also fix up missing slogs
4626 mutex_enter(&spa_namespace_lock);
4627 spa = spa_lookup(zc->zc_name);
4629 mutex_exit(&spa_namespace_lock);
4630 return (SET_ERROR(EIO));
4632 if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
4633 /* we need to let spa_open/spa_load clear the chains */
4634 spa_set_log_state(spa, SPA_LOG_CLEAR);
4636 spa->spa_last_open_failed = 0;
4637 mutex_exit(&spa_namespace_lock);
4639 if (zc->zc_cookie & ZPOOL_NO_REWIND) {
4640 error = spa_open(zc->zc_name, &spa, FTAG);
4643 nvlist_t *config = NULL;
4645 if (zc->zc_nvlist_src == 0)
4646 return (SET_ERROR(EINVAL));
4648 if ((error = get_nvlist(zc->zc_nvlist_src,
4649 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
4650 error = spa_open_rewind(zc->zc_name, &spa, FTAG,
4652 if (config != NULL) {
4655 if ((err = put_nvlist(zc, config)) != 0)
4657 nvlist_free(config);
4659 nvlist_free(policy);
4666 spa_vdev_state_enter(spa, SCL_NONE);
4668 if (zc->zc_guid == 0) {
4671 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
4673 (void) spa_vdev_state_exit(spa, NULL, ENODEV);
4674 spa_close(spa, FTAG);
4675 return (SET_ERROR(ENODEV));
4679 vdev_clear(spa, vd);
4681 (void) spa_vdev_state_exit(spa, NULL, 0);
4684 * Resume any suspended I/Os.
4686 if (zio_resume(spa) != 0)
4687 error = SET_ERROR(EIO);
4689 spa_close(spa, FTAG);
4695 zfs_ioc_pool_reopen(zfs_cmd_t *zc)
4700 error = spa_open(zc->zc_name, &spa, FTAG);
4704 spa_vdev_state_enter(spa, SCL_NONE);
4707 * If a resilver is already in progress then set the
4708 * spa_scrub_reopen flag to B_TRUE so that we don't restart
4709 * the scan as a side effect of the reopen. Otherwise, let
4710 * vdev_open() decided if a resilver is required.
4712 spa->spa_scrub_reopen = dsl_scan_resilvering(spa->spa_dsl_pool);
4713 vdev_reopen(spa->spa_root_vdev);
4714 spa->spa_scrub_reopen = B_FALSE;
4716 (void) spa_vdev_state_exit(spa, NULL, 0);
4717 spa_close(spa, FTAG);
4722 * zc_name name of filesystem
4723 * zc_value name of origin snapshot
4726 * zc_string name of conflicting snapshot, if there is one
4729 zfs_ioc_promote(zfs_cmd_t *zc)
4734 * We don't need to unmount *all* the origin fs's snapshots, but
4737 cp = strchr(zc->zc_value, '@');
4740 (void) dmu_objset_find(zc->zc_value,
4741 zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
4742 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
4746 * Retrieve a single {user|group}{used|quota}@... property.
4749 * zc_name name of filesystem
4750 * zc_objset_type zfs_userquota_prop_t
4751 * zc_value domain name (eg. "S-1-234-567-89")
4752 * zc_guid RID/UID/GID
4755 * zc_cookie property value
4758 zfs_ioc_userspace_one(zfs_cmd_t *zc)
4763 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
4764 return (SET_ERROR(EINVAL));
4766 error = zfs_sb_hold(zc->zc_name, FTAG, &zsb, B_FALSE);
4770 error = zfs_userspace_one(zsb,
4771 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
4772 zfs_sb_rele(zsb, FTAG);
4779 * zc_name name of filesystem
4780 * zc_cookie zap cursor
4781 * zc_objset_type zfs_userquota_prop_t
4782 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
4785 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
4786 * zc_cookie zap cursor
4789 zfs_ioc_userspace_many(zfs_cmd_t *zc)
4792 int bufsize = zc->zc_nvlist_dst_size;
4797 return (SET_ERROR(ENOMEM));
4799 error = zfs_sb_hold(zc->zc_name, FTAG, &zsb, B_FALSE);
4803 buf = vmem_alloc(bufsize, KM_SLEEP);
4805 error = zfs_userspace_many(zsb, zc->zc_objset_type, &zc->zc_cookie,
4806 buf, &zc->zc_nvlist_dst_size);
4809 error = xcopyout(buf,
4810 (void *)(uintptr_t)zc->zc_nvlist_dst,
4811 zc->zc_nvlist_dst_size);
4813 vmem_free(buf, bufsize);
4814 zfs_sb_rele(zsb, FTAG);
4821 * zc_name name of filesystem
4827 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
4833 if (get_zfs_sb(zc->zc_name, &zsb) == 0) {
4834 if (!dmu_objset_userused_enabled(zsb->z_os)) {
4836 * If userused is not enabled, it may be because the
4837 * objset needs to be closed & reopened (to grow the
4838 * objset_phys_t). Suspend/resume the fs will do that.
4840 error = zfs_suspend_fs(zsb);
4842 dmu_objset_refresh_ownership(zsb->z_os,
4844 error = zfs_resume_fs(zsb, zc->zc_name);
4848 error = dmu_objset_userspace_upgrade(zsb->z_os);
4849 deactivate_super(zsb->z_sb);
4851 /* XXX kind of reading contents without owning */
4852 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4856 error = dmu_objset_userspace_upgrade(os);
4857 dmu_objset_rele(os, FTAG);
4864 zfs_ioc_share(zfs_cmd_t *zc)
4866 return (SET_ERROR(ENOSYS));
4869 ace_t full_access[] = {
4870 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
4875 * zc_name name of containing filesystem
4876 * zc_obj object # beyond which we want next in-use object #
4879 * zc_obj next in-use object #
4882 zfs_ioc_next_obj(zfs_cmd_t *zc)
4884 objset_t *os = NULL;
4887 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4891 error = dmu_object_next(os, &zc->zc_obj, B_FALSE, 0);
4893 dmu_objset_rele(os, FTAG);
4899 * zc_name name of filesystem
4900 * zc_value prefix name for snapshot
4901 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
4904 * zc_value short name of new snapshot
4907 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
4914 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
4918 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
4919 (u_longlong_t)ddi_get_lbolt64());
4920 hold_name = kmem_asprintf("%%%s", zc->zc_value);
4922 error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
4925 (void) strcpy(zc->zc_value, snap_name);
4928 zfs_onexit_fd_rele(zc->zc_cleanup_fd);
4934 * zc_name name of "to" snapshot
4935 * zc_value name of "from" snapshot
4936 * zc_cookie file descriptor to write diff data on
4939 * dmu_diff_record_t's to the file descriptor
4942 zfs_ioc_diff(zfs_cmd_t *zc)
4948 fp = getf(zc->zc_cookie);
4950 return (SET_ERROR(EBADF));
4954 error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off);
4956 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4958 releasef(zc->zc_cookie);
4964 * Remove all ACL files in shares dir
4966 #ifdef HAVE_SMB_SHARE
4968 zfs_smb_acl_purge(znode_t *dzp)
4971 zap_attribute_t zap;
4972 zfs_sb_t *zsb = ZTOZSB(dzp);
4975 for (zap_cursor_init(&zc, zsb->z_os, dzp->z_id);
4976 (error = zap_cursor_retrieve(&zc, &zap)) == 0;
4977 zap_cursor_advance(&zc)) {
4978 if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
4982 zap_cursor_fini(&zc);
4985 #endif /* HAVE_SMB_SHARE */
4988 zfs_ioc_smb_acl(zfs_cmd_t *zc)
4990 #ifdef HAVE_SMB_SHARE
4993 vnode_t *resourcevp = NULL;
5002 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
5003 NO_FOLLOW, NULL, &vp)) != 0)
5006 /* Now make sure mntpnt and dataset are ZFS */
5008 if (vp->v_vfsp->vfs_fstype != zfsfstype ||
5009 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
5010 zc->zc_name) != 0)) {
5012 return (SET_ERROR(EINVAL));
5020 * Create share dir if its missing.
5022 mutex_enter(&zsb->z_lock);
5023 if (zsb->z_shares_dir == 0) {
5026 tx = dmu_tx_create(zsb->z_os);
5027 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
5029 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
5030 error = dmu_tx_assign(tx, TXG_WAIT);
5034 error = zfs_create_share_dir(zsb, tx);
5038 mutex_exit(&zsb->z_lock);
5044 mutex_exit(&zsb->z_lock);
5046 ASSERT(zsb->z_shares_dir);
5047 if ((error = zfs_zget(zsb, zsb->z_shares_dir, &sharedir)) != 0) {
5053 switch (zc->zc_cookie) {
5054 case ZFS_SMB_ACL_ADD:
5055 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
5056 vattr.va_mode = S_IFREG|0777;
5060 vsec.vsa_mask = VSA_ACE;
5061 vsec.vsa_aclentp = &full_access;
5062 vsec.vsa_aclentsz = sizeof (full_access);
5063 vsec.vsa_aclcnt = 1;
5065 error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
5066 &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
5068 VN_RELE(resourcevp);
5071 case ZFS_SMB_ACL_REMOVE:
5072 error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
5076 case ZFS_SMB_ACL_RENAME:
5077 if ((error = get_nvlist(zc->zc_nvlist_src,
5078 zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
5080 VN_RELE(ZTOV(sharedir));
5084 if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
5085 nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
5088 VN_RELE(ZTOV(sharedir));
5090 nvlist_free(nvlist);
5093 error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
5095 nvlist_free(nvlist);
5098 case ZFS_SMB_ACL_PURGE:
5099 error = zfs_smb_acl_purge(sharedir);
5103 error = SET_ERROR(EINVAL);
5108 VN_RELE(ZTOV(sharedir));
5114 return (SET_ERROR(ENOTSUP));
5115 #endif /* HAVE_SMB_SHARE */
5120 * "holds" -> { snapname -> holdname (string), ... }
5121 * (optional) "cleanup_fd" -> fd (int32)
5125 * snapname -> error value (int32)
5131 zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
5135 int cleanup_fd = -1;
5139 error = nvlist_lookup_nvlist(args, "holds", &holds);
5141 return (SET_ERROR(EINVAL));
5143 /* make sure the user didn't pass us any invalid (empty) tags */
5144 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
5145 pair = nvlist_next_nvpair(holds, pair)) {
5148 error = nvpair_value_string(pair, &htag);
5150 return (SET_ERROR(error));
5152 if (strlen(htag) == 0)
5153 return (SET_ERROR(EINVAL));
5156 if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
5157 error = zfs_onexit_fd_hold(cleanup_fd, &minor);
5162 error = dsl_dataset_user_hold(holds, minor, errlist);
5164 zfs_onexit_fd_rele(cleanup_fd);
5169 * innvl is not used.
5172 * holdname -> time added (uint64 seconds since epoch)
5178 zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
5180 return (dsl_dataset_get_holds(snapname, outnvl));
5185 * snapname -> { holdname, ... }
5190 * snapname -> error value (int32)
5196 zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
5198 return (dsl_dataset_user_release(holds, errlist));
5203 * zc_guid flags (ZEVENT_NONBLOCK)
5204 * zc_cleanup_fd zevent file descriptor
5207 * zc_nvlist_dst next nvlist event
5208 * zc_cookie dropped events since last get
5211 zfs_ioc_events_next(zfs_cmd_t *zc)
5214 nvlist_t *event = NULL;
5216 uint64_t dropped = 0;
5219 error = zfs_zevent_fd_hold(zc->zc_cleanup_fd, &minor, &ze);
5224 error = zfs_zevent_next(ze, &event,
5225 &zc->zc_nvlist_dst_size, &dropped);
5226 if (event != NULL) {
5227 zc->zc_cookie = dropped;
5228 error = put_nvlist(zc, event);
5232 if (zc->zc_guid & ZEVENT_NONBLOCK)
5235 if ((error == 0) || (error != ENOENT))
5238 error = zfs_zevent_wait(ze);
5243 zfs_zevent_fd_rele(zc->zc_cleanup_fd);
5250 * zc_cookie cleared events count
5253 zfs_ioc_events_clear(zfs_cmd_t *zc)
5257 zfs_zevent_drain_all(&count);
5258 zc->zc_cookie = count;
5265 * zc_guid eid | ZEVENT_SEEK_START | ZEVENT_SEEK_END
5266 * zc_cleanup zevent file descriptor
5269 zfs_ioc_events_seek(zfs_cmd_t *zc)
5275 error = zfs_zevent_fd_hold(zc->zc_cleanup_fd, &minor, &ze);
5279 error = zfs_zevent_seek(ze, zc->zc_guid);
5280 zfs_zevent_fd_rele(zc->zc_cleanup_fd);
5287 * zc_name name of new filesystem or snapshot
5288 * zc_value full name of old snapshot
5291 * zc_cookie space in bytes
5292 * zc_objset_type compressed space in bytes
5293 * zc_perm_action uncompressed space in bytes
5296 zfs_ioc_space_written(zfs_cmd_t *zc)
5300 dsl_dataset_t *new, *old;
5302 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5305 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
5307 dsl_pool_rele(dp, FTAG);
5310 error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
5312 dsl_dataset_rele(new, FTAG);
5313 dsl_pool_rele(dp, FTAG);
5317 error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
5318 &zc->zc_objset_type, &zc->zc_perm_action);
5319 dsl_dataset_rele(old, FTAG);
5320 dsl_dataset_rele(new, FTAG);
5321 dsl_pool_rele(dp, FTAG);
5327 * "firstsnap" -> snapshot name
5331 * "used" -> space in bytes
5332 * "compressed" -> compressed space in bytes
5333 * "uncompressed" -> uncompressed space in bytes
5337 zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
5341 dsl_dataset_t *new, *old;
5343 uint64_t used, comp, uncomp;
5345 if (nvlist_lookup_string(innvl, "firstsnap", &firstsnap) != 0)
5346 return (SET_ERROR(EINVAL));
5348 error = dsl_pool_hold(lastsnap, FTAG, &dp);
5352 error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
5353 if (error == 0 && !new->ds_is_snapshot) {
5354 dsl_dataset_rele(new, FTAG);
5355 error = SET_ERROR(EINVAL);
5358 dsl_pool_rele(dp, FTAG);
5361 error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
5362 if (error == 0 && !old->ds_is_snapshot) {
5363 dsl_dataset_rele(old, FTAG);
5364 error = SET_ERROR(EINVAL);
5367 dsl_dataset_rele(new, FTAG);
5368 dsl_pool_rele(dp, FTAG);
5372 error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
5373 dsl_dataset_rele(old, FTAG);
5374 dsl_dataset_rele(new, FTAG);
5375 dsl_pool_rele(dp, FTAG);
5376 fnvlist_add_uint64(outnvl, "used", used);
5377 fnvlist_add_uint64(outnvl, "compressed", comp);
5378 fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
5384 * "fd" -> file descriptor to write stream to (int32)
5385 * (optional) "fromsnap" -> full snap name to send an incremental from
5386 * (optional) "largeblockok" -> (value ignored)
5387 * indicates that blocks > 128KB are permitted
5388 * (optional) "embedok" -> (value ignored)
5389 * presence indicates DRR_WRITE_EMBEDDED records are permitted
5390 * (optional) "resume_object" and "resume_offset" -> (uint64)
5391 * if present, resume send stream from specified object and offset.
5398 zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5402 char *fromname = NULL;
5405 boolean_t largeblockok;
5407 uint64_t resumeobj = 0;
5408 uint64_t resumeoff = 0;
5410 error = nvlist_lookup_int32(innvl, "fd", &fd);
5412 return (SET_ERROR(EINVAL));
5414 (void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
5416 largeblockok = nvlist_exists(innvl, "largeblockok");
5417 embedok = nvlist_exists(innvl, "embedok");
5419 (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
5420 (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
5422 if ((fp = getf(fd)) == NULL)
5423 return (SET_ERROR(EBADF));
5426 error = dmu_send(snapname, fromname, embedok, largeblockok, fd,
5427 resumeobj, resumeoff, fp->f_vnode, &off);
5429 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5437 * Determine approximately how large a zfs send stream will be -- the number
5438 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
5441 * (optional) "from" -> full snap or bookmark name to send an incremental
5446 * "space" -> bytes of space (uint64)
5450 zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5453 dsl_dataset_t *tosnap;
5458 error = dsl_pool_hold(snapname, FTAG, &dp);
5462 error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
5464 dsl_pool_rele(dp, FTAG);
5468 error = nvlist_lookup_string(innvl, "from", &fromname);
5470 if (strchr(fromname, '@') != NULL) {
5472 * If from is a snapshot, hold it and use the more
5473 * efficient dmu_send_estimate to estimate send space
5474 * size using deadlists.
5476 dsl_dataset_t *fromsnap;
5477 error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
5480 error = dmu_send_estimate(tosnap, fromsnap, &space);
5481 dsl_dataset_rele(fromsnap, FTAG);
5482 } else if (strchr(fromname, '#') != NULL) {
5484 * If from is a bookmark, fetch the creation TXG of the
5485 * snapshot it was created from and use that to find
5486 * blocks that were born after it.
5488 zfs_bookmark_phys_t frombm;
5490 error = dsl_bookmark_lookup(dp, fromname, tosnap,
5494 error = dmu_send_estimate_from_txg(tosnap,
5495 frombm.zbm_creation_txg, &space);
5498 * from is not properly formatted as a snapshot or
5501 error = SET_ERROR(EINVAL);
5505 // If estimating the size of a full send, use dmu_send_estimate
5506 error = dmu_send_estimate(tosnap, NULL, &space);
5509 fnvlist_add_uint64(outnvl, "space", space);
5512 dsl_dataset_rele(tosnap, FTAG);
5513 dsl_pool_rele(dp, FTAG);
5517 static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
5520 zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5521 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
5522 boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
5524 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
5526 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
5527 ASSERT3U(ioc, <, ZFS_IOC_LAST);
5528 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
5529 ASSERT3P(vec->zvec_func, ==, NULL);
5531 vec->zvec_legacy_func = func;
5532 vec->zvec_secpolicy = secpolicy;
5533 vec->zvec_namecheck = namecheck;
5534 vec->zvec_allow_log = log_history;
5535 vec->zvec_pool_check = pool_check;
5539 * See the block comment at the beginning of this file for details on
5540 * each argument to this function.
5543 zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
5544 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
5545 zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
5546 boolean_t allow_log)
5548 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
5550 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
5551 ASSERT3U(ioc, <, ZFS_IOC_LAST);
5552 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
5553 ASSERT3P(vec->zvec_func, ==, NULL);
5555 /* if we are logging, the name must be valid */
5556 ASSERT(!allow_log || namecheck != NO_NAME);
5558 vec->zvec_name = name;
5559 vec->zvec_func = func;
5560 vec->zvec_secpolicy = secpolicy;
5561 vec->zvec_namecheck = namecheck;
5562 vec->zvec_pool_check = pool_check;
5563 vec->zvec_smush_outnvlist = smush_outnvlist;
5564 vec->zvec_allow_log = allow_log;
5568 zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5569 zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
5570 zfs_ioc_poolcheck_t pool_check)
5572 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5573 POOL_NAME, log_history, pool_check);
5577 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5578 zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
5580 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5581 DATASET_NAME, B_FALSE, pool_check);
5585 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
5587 zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
5588 POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5592 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5593 zfs_secpolicy_func_t *secpolicy)
5595 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5596 NO_NAME, B_FALSE, POOL_CHECK_NONE);
5600 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
5601 zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
5603 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5604 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
5608 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
5610 zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
5611 zfs_secpolicy_read);
5615 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5616 zfs_secpolicy_func_t *secpolicy)
5618 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5619 DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5623 zfs_ioctl_init(void)
5625 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
5626 zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
5627 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5629 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
5630 zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
5631 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE);
5633 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
5634 zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
5635 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5637 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
5638 zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
5639 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5641 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
5642 zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
5643 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5645 zfs_ioctl_register("create", ZFS_IOC_CREATE,
5646 zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
5647 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5649 zfs_ioctl_register("clone", ZFS_IOC_CLONE,
5650 zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
5651 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5653 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
5654 zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
5655 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5657 zfs_ioctl_register("hold", ZFS_IOC_HOLD,
5658 zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
5659 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5660 zfs_ioctl_register("release", ZFS_IOC_RELEASE,
5661 zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
5662 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5664 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
5665 zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
5666 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5668 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
5669 zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
5670 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
5672 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK,
5673 zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME,
5674 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5676 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS,
5677 zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME,
5678 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5680 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS,
5681 zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks,
5683 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5685 zfs_ioctl_register("receive", ZFS_IOC_RECV_NEW,
5686 zfs_ioc_recv_new, zfs_secpolicy_recv_new, DATASET_NAME,
5687 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5689 /* IOCTLS that use the legacy function signature */
5691 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
5692 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
5694 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
5695 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5696 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
5698 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
5699 zfs_ioc_pool_upgrade);
5700 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
5702 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
5703 zfs_ioc_vdev_remove);
5704 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
5705 zfs_ioc_vdev_set_state);
5706 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
5707 zfs_ioc_vdev_attach);
5708 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
5709 zfs_ioc_vdev_detach);
5710 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
5711 zfs_ioc_vdev_setpath);
5712 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
5713 zfs_ioc_vdev_setfru);
5714 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
5715 zfs_ioc_pool_set_props);
5716 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
5717 zfs_ioc_vdev_split);
5718 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
5719 zfs_ioc_pool_reguid);
5721 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
5722 zfs_ioc_pool_configs, zfs_secpolicy_none);
5723 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
5724 zfs_ioc_pool_tryimport, zfs_secpolicy_config);
5725 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
5726 zfs_ioc_inject_fault, zfs_secpolicy_inject);
5727 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
5728 zfs_ioc_clear_fault, zfs_secpolicy_inject);
5729 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
5730 zfs_ioc_inject_list_next, zfs_secpolicy_inject);
5733 * pool destroy, and export don't log the history as part of
5734 * zfsdev_ioctl, but rather zfs_ioc_pool_export
5735 * does the logging of those commands.
5737 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
5738 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
5739 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
5740 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
5742 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
5743 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
5744 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
5745 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
5747 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
5748 zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED);
5749 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
5750 zfs_ioc_dsobj_to_dsname,
5751 zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED);
5752 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
5753 zfs_ioc_pool_get_history,
5754 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
5756 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
5757 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5759 zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
5760 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5761 zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
5762 zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
5764 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
5765 zfs_ioc_space_written);
5766 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
5767 zfs_ioc_objset_recvd_props);
5768 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
5770 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
5772 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
5773 zfs_ioc_objset_stats);
5774 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
5775 zfs_ioc_objset_zplprops);
5776 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
5777 zfs_ioc_dataset_list_next);
5778 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
5779 zfs_ioc_snapshot_list_next);
5780 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
5781 zfs_ioc_send_progress);
5783 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
5784 zfs_ioc_diff, zfs_secpolicy_diff);
5785 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
5786 zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
5787 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
5788 zfs_ioc_obj_to_path, zfs_secpolicy_diff);
5789 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
5790 zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
5791 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
5792 zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
5793 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
5794 zfs_ioc_send, zfs_secpolicy_send);
5796 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
5797 zfs_secpolicy_none);
5798 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
5799 zfs_secpolicy_destroy);
5800 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
5801 zfs_secpolicy_rename);
5802 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
5803 zfs_secpolicy_recv);
5804 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
5805 zfs_secpolicy_promote);
5806 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
5807 zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
5808 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
5809 zfs_secpolicy_set_fsacl);
5811 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
5812 zfs_secpolicy_share, POOL_CHECK_NONE);
5813 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
5814 zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
5815 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
5816 zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
5817 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5818 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
5819 zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
5820 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5825 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_NEXT, zfs_ioc_events_next,
5826 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
5827 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_CLEAR, zfs_ioc_events_clear,
5828 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
5829 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_SEEK, zfs_ioc_events_seek,
5830 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
5834 pool_status_check(const char *name, zfs_ioc_namecheck_t type,
5835 zfs_ioc_poolcheck_t check)
5840 ASSERT(type == POOL_NAME || type == DATASET_NAME);
5842 if (check & POOL_CHECK_NONE)
5845 error = spa_open(name, &spa, FTAG);
5847 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
5848 error = SET_ERROR(EAGAIN);
5849 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
5850 error = SET_ERROR(EROFS);
5851 spa_close(spa, FTAG);
5857 zfsdev_get_state_impl(minor_t minor, enum zfsdev_state_type which)
5861 for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) {
5862 if (zs->zs_minor == minor) {
5866 return (zs->zs_onexit);
5868 return (zs->zs_zevent);
5879 zfsdev_get_state(minor_t minor, enum zfsdev_state_type which)
5883 ptr = zfsdev_get_state_impl(minor, which);
5889 zfsdev_getminor(struct file *filp, minor_t *minorp)
5891 zfsdev_state_t *zs, *fpd;
5893 ASSERT(filp != NULL);
5894 ASSERT(!MUTEX_HELD(&zfsdev_state_lock));
5896 fpd = filp->private_data;
5900 mutex_enter(&zfsdev_state_lock);
5902 for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) {
5904 if (zs->zs_minor == -1)
5908 *minorp = fpd->zs_minor;
5909 mutex_exit(&zfsdev_state_lock);
5914 mutex_exit(&zfsdev_state_lock);
5920 * Find a free minor number. The zfsdev_state_list is expected to
5921 * be short since it is only a list of currently open file handles.
5924 zfsdev_minor_alloc(void)
5926 static minor_t last_minor = 0;
5929 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5931 for (m = last_minor + 1; m != last_minor; m++) {
5932 if (m > ZFSDEV_MAX_MINOR)
5934 if (zfsdev_get_state_impl(m, ZST_ALL) == NULL) {
5944 zfsdev_state_init(struct file *filp)
5946 zfsdev_state_t *zs, *zsprev = NULL;
5948 boolean_t newzs = B_FALSE;
5950 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5952 minor = zfsdev_minor_alloc();
5954 return (SET_ERROR(ENXIO));
5956 for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) {
5957 if (zs->zs_minor == -1)
5963 zs = kmem_zalloc(sizeof (zfsdev_state_t), KM_SLEEP);
5968 filp->private_data = zs;
5970 zfs_onexit_init((zfs_onexit_t **)&zs->zs_onexit);
5971 zfs_zevent_init((zfs_zevent_t **)&zs->zs_zevent);
5975 * In order to provide for lock-free concurrent read access
5976 * to the minor list in zfsdev_get_state_impl(), new entries
5977 * must be completely written before linking them into the
5978 * list whereas existing entries are already linked; the last
5979 * operation must be updating zs_minor (from -1 to the new
5983 zs->zs_minor = minor;
5985 zsprev->zs_next = zs;
5988 zs->zs_minor = minor;
5995 zfsdev_state_destroy(struct file *filp)
5999 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
6000 ASSERT(filp->private_data != NULL);
6002 zs = filp->private_data;
6004 zfs_onexit_destroy(zs->zs_onexit);
6005 zfs_zevent_destroy(zs->zs_zevent);
6011 zfsdev_open(struct inode *ino, struct file *filp)
6015 mutex_enter(&zfsdev_state_lock);
6016 error = zfsdev_state_init(filp);
6017 mutex_exit(&zfsdev_state_lock);
6023 zfsdev_release(struct inode *ino, struct file *filp)
6027 mutex_enter(&zfsdev_state_lock);
6028 error = zfsdev_state_destroy(filp);
6029 mutex_exit(&zfsdev_state_lock);
6035 zfsdev_ioctl(struct file *filp, unsigned cmd, unsigned long arg)
6039 int error, rc, flag = 0;
6040 const zfs_ioc_vec_t *vec;
6041 char *saved_poolname = NULL;
6042 nvlist_t *innvl = NULL;
6043 fstrans_cookie_t cookie;
6045 vecnum = cmd - ZFS_IOC_FIRST;
6046 if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
6047 return (-SET_ERROR(EINVAL));
6048 vec = &zfs_ioc_vec[vecnum];
6051 * The registered ioctl list may be sparse, verify that either
6052 * a normal or legacy handler are registered.
6054 if (vec->zvec_func == NULL && vec->zvec_legacy_func == NULL)
6055 return (-SET_ERROR(EINVAL));
6057 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
6059 error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
6061 error = SET_ERROR(EFAULT);
6065 zc->zc_iflags = flag & FKIOCTL;
6066 if (zc->zc_nvlist_src_size > MAX_NVLIST_SRC_SIZE) {
6068 * Make sure the user doesn't pass in an insane value for
6069 * zc_nvlist_src_size. We have to check, since we will end
6070 * up allocating that much memory inside of get_nvlist(). This
6071 * prevents a nefarious user from allocating tons of kernel
6074 * Also, we return EINVAL instead of ENOMEM here. The reason
6075 * being that returning ENOMEM from an ioctl() has a special
6076 * connotation; that the user's size value is too small and
6077 * needs to be expanded to hold the nvlist. See
6078 * zcmd_expand_dst_nvlist() for details.
6080 error = SET_ERROR(EINVAL); /* User's size too big */
6082 } else if (zc->zc_nvlist_src_size != 0) {
6083 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
6084 zc->zc_iflags, &innvl);
6090 * Ensure that all pool/dataset names are valid before we pass down to
6093 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
6094 switch (vec->zvec_namecheck) {
6096 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
6097 error = SET_ERROR(EINVAL);
6099 error = pool_status_check(zc->zc_name,
6100 vec->zvec_namecheck, vec->zvec_pool_check);
6104 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
6105 error = SET_ERROR(EINVAL);
6107 error = pool_status_check(zc->zc_name,
6108 vec->zvec_namecheck, vec->zvec_pool_check);
6116 if (error == 0 && !(flag & FKIOCTL)) {
6117 cookie = spl_fstrans_mark();
6118 error = vec->zvec_secpolicy(zc, innvl, CRED());
6119 spl_fstrans_unmark(cookie);
6125 /* legacy ioctls can modify zc_name */
6126 saved_poolname = strdup(zc->zc_name);
6127 if (saved_poolname == NULL) {
6128 error = SET_ERROR(ENOMEM);
6131 saved_poolname[strcspn(saved_poolname, "/@#")] = '\0';
6134 if (vec->zvec_func != NULL) {
6138 nvlist_t *lognv = NULL;
6140 ASSERT(vec->zvec_legacy_func == NULL);
6143 * Add the innvl to the lognv before calling the func,
6144 * in case the func changes the innvl.
6146 if (vec->zvec_allow_log) {
6147 lognv = fnvlist_alloc();
6148 fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
6150 if (!nvlist_empty(innvl)) {
6151 fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
6156 outnvl = fnvlist_alloc();
6157 cookie = spl_fstrans_mark();
6158 error = vec->zvec_func(zc->zc_name, innvl, outnvl);
6159 spl_fstrans_unmark(cookie);
6161 if (error == 0 && vec->zvec_allow_log &&
6162 spa_open(zc->zc_name, &spa, FTAG) == 0) {
6163 if (!nvlist_empty(outnvl)) {
6164 fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL,
6167 (void) spa_history_log_nvl(spa, lognv);
6168 spa_close(spa, FTAG);
6170 fnvlist_free(lognv);
6172 if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
6174 if (vec->zvec_smush_outnvlist) {
6175 smusherror = nvlist_smush(outnvl,
6176 zc->zc_nvlist_dst_size);
6178 if (smusherror == 0)
6179 puterror = put_nvlist(zc, outnvl);
6185 nvlist_free(outnvl);
6187 cookie = spl_fstrans_mark();
6188 error = vec->zvec_legacy_func(zc);
6189 spl_fstrans_unmark(cookie);
6194 rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
6195 if (error == 0 && rc != 0)
6196 error = SET_ERROR(EFAULT);
6197 if (error == 0 && vec->zvec_allow_log) {
6198 char *s = tsd_get(zfs_allow_log_key);
6201 (void) tsd_set(zfs_allow_log_key, saved_poolname);
6203 if (saved_poolname != NULL)
6204 strfree(saved_poolname);
6207 kmem_free(zc, sizeof (zfs_cmd_t));
6211 #ifdef CONFIG_COMPAT
6213 zfsdev_compat_ioctl(struct file *filp, unsigned cmd, unsigned long arg)
6215 return (zfsdev_ioctl(filp, cmd, arg));
6218 #define zfsdev_compat_ioctl NULL
6221 static const struct file_operations zfsdev_fops = {
6222 .open = zfsdev_open,
6223 .release = zfsdev_release,
6224 .unlocked_ioctl = zfsdev_ioctl,
6225 .compat_ioctl = zfsdev_compat_ioctl,
6226 .owner = THIS_MODULE,
6229 static struct miscdevice zfs_misc = {
6230 .minor = MISC_DYNAMIC_MINOR,
6232 .fops = &zfsdev_fops,
6240 mutex_init(&zfsdev_state_lock, NULL, MUTEX_DEFAULT, NULL);
6241 zfsdev_state_list = kmem_zalloc(sizeof (zfsdev_state_t), KM_SLEEP);
6242 zfsdev_state_list->zs_minor = -1;
6244 error = misc_register(&zfs_misc);
6246 printk(KERN_INFO "ZFS: misc_register() failed %d\n", error);
6256 zfsdev_state_t *zs, *zsprev = NULL;
6258 misc_deregister(&zfs_misc);
6259 mutex_destroy(&zfsdev_state_lock);
6261 for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) {
6263 kmem_free(zsprev, sizeof (zfsdev_state_t));
6267 kmem_free(zsprev, sizeof (zfsdev_state_t));
6271 zfs_allow_log_destroy(void *arg)
6273 char *poolname = arg;
6278 #define ZFS_DEBUG_STR " (DEBUG mode)"
6280 #define ZFS_DEBUG_STR ""
6288 error = -vn_set_pwd("/");
6291 "ZFS: Warning unable to set pwd to '/': %d\n", error);
6295 if ((error = -zvol_init()) != 0)
6298 spa_init(FREAD | FWRITE);
6303 if ((error = zfs_attach()) != 0)
6306 tsd_create(&zfs_fsyncer_key, NULL);
6307 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
6308 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
6310 printk(KERN_NOTICE "ZFS: Loaded module v%s-%s%s, "
6311 "ZFS pool version %s, ZFS filesystem version %s\n",
6312 ZFS_META_VERSION, ZFS_META_RELEASE, ZFS_DEBUG_STR,
6313 SPA_VERSION_STRING, ZPL_VERSION_STRING);
6314 #ifndef CONFIG_FS_POSIX_ACL
6315 printk(KERN_NOTICE "ZFS: Posix ACLs disabled by kernel\n");
6316 #endif /* CONFIG_FS_POSIX_ACL */
6324 printk(KERN_NOTICE "ZFS: Failed to Load ZFS Filesystem v%s-%s%s"
6325 ", rc = %d\n", ZFS_META_VERSION, ZFS_META_RELEASE,
6326 ZFS_DEBUG_STR, error);
6339 tsd_destroy(&zfs_fsyncer_key);
6340 tsd_destroy(&rrw_tsd_key);
6341 tsd_destroy(&zfs_allow_log_key);
6343 printk(KERN_NOTICE "ZFS: Unloaded module v%s-%s%s\n",
6344 ZFS_META_VERSION, ZFS_META_RELEASE, ZFS_DEBUG_STR);
6351 MODULE_DESCRIPTION("ZFS");
6352 MODULE_AUTHOR(ZFS_META_AUTHOR);
6353 MODULE_LICENSE(ZFS_META_LICENSE);
6354 MODULE_VERSION(ZFS_META_VERSION "-" ZFS_META_RELEASE);
6355 #endif /* HAVE_SPL */