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 * Copyright (c) 2011-2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
25 * All rights reserved.
26 * Copyright 2013 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
27 * Copyright 2014 Xin Li <delphij@FreeBSD.org>. 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.
38 * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
39 * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
41 * There are two ways that we handle ioctls: the legacy way where almost
42 * all of the logic is in the ioctl callback, and the new way where most
43 * of the marshalling is handled in the common entry point, zfsdev_ioctl().
45 * Non-legacy ioctls should be registered by calling
46 * zfs_ioctl_register() from zfs_ioctl_init(). The ioctl is invoked
47 * from userland by lzc_ioctl().
49 * The registration arguments are as follows:
52 * The name of the ioctl. This is used for history logging. If the
53 * ioctl returns successfully (the callback returns 0), and allow_log
54 * is true, then a history log entry will be recorded with the input &
55 * output nvlists. The log entry can be printed with "zpool history -i".
58 * The ioctl request number, which userland will pass to ioctl(2).
59 * The ioctl numbers can change from release to release, because
60 * the caller (libzfs) must be matched to the kernel.
62 * zfs_secpolicy_func_t *secpolicy
63 * This function will be called before the zfs_ioc_func_t, to
64 * determine if this operation is permitted. It should return EPERM
65 * on failure, and 0 on success. Checks include determining if the
66 * dataset is visible in this zone, and if the user has either all
67 * zfs privileges in the zone (SYS_MOUNT), or has been granted permission
68 * to do this operation on this dataset with "zfs allow".
70 * zfs_ioc_namecheck_t namecheck
71 * This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
72 * name, a dataset name, or nothing. If the name is not well-formed,
73 * the ioctl will fail and the callback will not be called.
74 * Therefore, the callback can assume that the name is well-formed
75 * (e.g. is null-terminated, doesn't have more than one '@' character,
76 * doesn't have invalid characters).
78 * zfs_ioc_poolcheck_t pool_check
79 * This specifies requirements on the pool state. If the pool does
80 * not meet them (is suspended or is readonly), the ioctl will fail
81 * and the callback will not be called. If any checks are specified
82 * (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
83 * Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
84 * POOL_CHECK_READONLY).
86 * boolean_t smush_outnvlist
87 * If smush_outnvlist is true, then the output is presumed to be a
88 * list of errors, and it will be "smushed" down to fit into the
89 * caller's buffer, by removing some entries and replacing them with a
90 * single "N_MORE_ERRORS" entry indicating how many were removed. See
91 * nvlist_smush() for details. If smush_outnvlist is false, and the
92 * outnvlist does not fit into the userland-provided buffer, then the
93 * ioctl will fail with ENOMEM.
95 * zfs_ioc_func_t *func
96 * The callback function that will perform the operation.
98 * The callback should return 0 on success, or an error number on
99 * failure. If the function fails, the userland ioctl will return -1,
100 * and errno will be set to the callback's return value. The callback
101 * will be called with the following arguments:
104 * The name of the pool or dataset to operate on, from
105 * zfs_cmd_t:zc_name. The 'namecheck' argument specifies the
106 * expected type (pool, dataset, or none).
109 * The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src. Or
110 * NULL if no input nvlist was provided. Changes to this nvlist are
111 * ignored. If the input nvlist could not be deserialized, the
112 * ioctl will fail and the callback will not be called.
115 * The output nvlist, initially empty. The callback can fill it in,
116 * and it will be returned to userland by serializing it into
117 * zfs_cmd_t:zc_nvlist_dst. If it is non-empty, and serialization
118 * fails (e.g. because the caller didn't supply a large enough
119 * buffer), then the overall ioctl will fail. See the
120 * 'smush_nvlist' argument above for additional behaviors.
122 * There are two typical uses of the output nvlist:
123 * - To return state, e.g. property values. In this case,
124 * smush_outnvlist should be false. If the buffer was not large
125 * enough, the caller will reallocate a larger buffer and try
128 * - To return multiple errors from an ioctl which makes on-disk
129 * changes. In this case, smush_outnvlist should be true.
130 * Ioctls which make on-disk modifications should generally not
131 * use the outnvl if they succeed, because the caller can not
132 * distinguish between the operation failing, and
133 * deserialization failing.
136 #include <sys/types.h>
137 #include <sys/param.h>
138 #include <sys/systm.h>
139 #include <sys/conf.h>
140 #include <sys/kernel.h>
141 #include <sys/lock.h>
142 #include <sys/malloc.h>
143 #include <sys/mutex.h>
144 #include <sys/proc.h>
145 #include <sys/errno.h>
148 #include <sys/file.h>
149 #include <sys/kmem.h>
150 #include <sys/conf.h>
151 #include <sys/cmn_err.h>
152 #include <sys/stat.h>
153 #include <sys/zfs_ioctl.h>
154 #include <sys/zfs_vfsops.h>
155 #include <sys/zfs_znode.h>
158 #include <sys/spa_impl.h>
159 #include <sys/vdev.h>
161 #include <sys/dsl_dir.h>
162 #include <sys/dsl_dataset.h>
163 #include <sys/dsl_prop.h>
164 #include <sys/dsl_deleg.h>
165 #include <sys/dmu_objset.h>
166 #include <sys/dmu_impl.h>
167 #include <sys/dmu_tx.h>
168 #include <sys/sunddi.h>
169 #include <sys/policy.h>
170 #include <sys/zone.h>
171 #include <sys/nvpair.h>
172 #include <sys/mount.h>
173 #include <sys/taskqueue.h>
175 #include <sys/varargs.h>
176 #include <sys/fs/zfs.h>
177 #include <sys/zfs_ctldir.h>
178 #include <sys/zfs_dir.h>
179 #include <sys/zfs_onexit.h>
180 #include <sys/zvol.h>
181 #include <sys/dsl_scan.h>
182 #include <sys/dmu_objset.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 "zfs_namecheck.h"
190 #include "zfs_prop.h"
191 #include "zfs_deleg.h"
192 #include "zfs_comutil.h"
193 #include "zfs_ioctl_compat.h"
195 CTASSERT(sizeof(zfs_cmd_t) < IOCPARM_MAX);
197 static struct cdev *zfsdev;
199 extern void zfs_init(void);
200 extern void zfs_fini(void);
202 uint_t zfs_fsyncer_key;
203 extern uint_t rrw_tsd_key;
204 static uint_t zfs_allow_log_key;
206 typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *);
207 typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *);
208 typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *);
214 } zfs_ioc_namecheck_t;
217 POOL_CHECK_NONE = 1 << 0,
218 POOL_CHECK_SUSPENDED = 1 << 1,
219 POOL_CHECK_READONLY = 1 << 2,
220 } zfs_ioc_poolcheck_t;
222 typedef struct zfs_ioc_vec {
223 zfs_ioc_legacy_func_t *zvec_legacy_func;
224 zfs_ioc_func_t *zvec_func;
225 zfs_secpolicy_func_t *zvec_secpolicy;
226 zfs_ioc_namecheck_t zvec_namecheck;
227 boolean_t zvec_allow_log;
228 zfs_ioc_poolcheck_t zvec_pool_check;
229 boolean_t zvec_smush_outnvlist;
230 const char *zvec_name;
233 /* This array is indexed by zfs_userquota_prop_t */
234 static const char *userquota_perms[] = {
235 ZFS_DELEG_PERM_USERUSED,
236 ZFS_DELEG_PERM_USERQUOTA,
237 ZFS_DELEG_PERM_GROUPUSED,
238 ZFS_DELEG_PERM_GROUPQUOTA,
241 static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
242 static int zfs_check_settable(const char *name, nvpair_t *property,
244 static int zfs_check_clearable(char *dataset, nvlist_t *props,
246 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
248 int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
249 static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
251 static void zfsdev_close(void *data);
253 static int zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature);
255 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
257 __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
264 * Get rid of annoying "../common/" prefix to filename.
266 newfile = strrchr(file, '/');
267 if (newfile != NULL) {
268 newfile = newfile + 1; /* Get rid of leading / */
274 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
278 * To get this data, use the zfs-dprintf probe as so:
279 * dtrace -q -n 'zfs-dprintf \
280 * /stringof(arg0) == "dbuf.c"/ \
281 * {printf("%s: %s", stringof(arg1), stringof(arg3))}'
283 * arg1 = function name
287 DTRACE_PROBE4(zfs__dprintf,
288 char *, newfile, char *, func, int, line, char *, buf);
292 history_str_free(char *buf)
294 kmem_free(buf, HIS_MAX_RECORD_LEN);
298 history_str_get(zfs_cmd_t *zc)
302 if (zc->zc_history == 0)
305 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
306 if (copyinstr((void *)(uintptr_t)zc->zc_history,
307 buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
308 history_str_free(buf);
312 buf[HIS_MAX_RECORD_LEN -1] = '\0';
318 * Check to see if the named dataset is currently defined as bootable
321 zfs_is_bootfs(const char *name)
325 if (dmu_objset_hold(name, FTAG, &os) == 0) {
327 ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
328 dmu_objset_rele(os, FTAG);
335 * Return non-zero if the spa version is less than requested version.
338 zfs_earlier_version(const char *name, int version)
342 if (spa_open(name, &spa, FTAG) == 0) {
343 if (spa_version(spa) < version) {
344 spa_close(spa, FTAG);
347 spa_close(spa, FTAG);
353 * Return TRUE if the ZPL version is less than requested version.
356 zpl_earlier_version(const char *name, int version)
359 boolean_t rc = B_TRUE;
361 if (dmu_objset_hold(name, FTAG, &os) == 0) {
364 if (dmu_objset_type(os) != DMU_OST_ZFS) {
365 dmu_objset_rele(os, FTAG);
368 /* XXX reading from non-owned objset */
369 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
370 rc = zplversion < version;
371 dmu_objset_rele(os, FTAG);
377 zfs_log_history(zfs_cmd_t *zc)
382 if ((buf = history_str_get(zc)) == NULL)
385 if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
386 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
387 (void) spa_history_log(spa, buf);
388 spa_close(spa, FTAG);
390 history_str_free(buf);
394 * Policy for top-level read operations (list pools). Requires no privileges,
395 * and can be used in the local zone, as there is no associated dataset.
399 zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
405 * Policy for dataset read operations (list children, get statistics). Requires
406 * no privileges, but must be visible in the local zone.
410 zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
412 if (INGLOBALZONE(curthread) ||
413 zone_dataset_visible(zc->zc_name, NULL))
416 return (SET_ERROR(ENOENT));
420 zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
425 * The dataset must be visible by this zone -- check this first
426 * so they don't see EPERM on something they shouldn't know about.
428 if (!INGLOBALZONE(curthread) &&
429 !zone_dataset_visible(dataset, &writable))
430 return (SET_ERROR(ENOENT));
432 if (INGLOBALZONE(curthread)) {
434 * If the fs is zoned, only root can access it from the
437 if (secpolicy_zfs(cr) && zoned)
438 return (SET_ERROR(EPERM));
441 * If we are in a local zone, the 'zoned' property must be set.
444 return (SET_ERROR(EPERM));
446 /* must be writable by this zone */
448 return (SET_ERROR(EPERM));
454 zfs_dozonecheck(const char *dataset, cred_t *cr)
458 if (dsl_prop_get_integer(dataset, "jailed", &zoned, NULL))
459 return (SET_ERROR(ENOENT));
461 return (zfs_dozonecheck_impl(dataset, zoned, cr));
465 zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
469 if (dsl_prop_get_int_ds(ds, "jailed", &zoned))
470 return (SET_ERROR(ENOENT));
472 return (zfs_dozonecheck_impl(dataset, zoned, cr));
476 zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
477 const char *perm, cred_t *cr)
481 error = zfs_dozonecheck_ds(name, ds, cr);
483 error = secpolicy_zfs(cr);
485 error = dsl_deleg_access_impl(ds, perm, cr);
491 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
497 error = dsl_pool_hold(name, FTAG, &dp);
501 error = dsl_dataset_hold(dp, name, FTAG, &ds);
503 dsl_pool_rele(dp, FTAG);
507 error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
509 dsl_dataset_rele(ds, FTAG);
510 dsl_pool_rele(dp, FTAG);
516 * Policy for setting the security label property.
518 * Returns 0 for success, non-zero for access and other errors.
521 zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
523 char ds_hexsl[MAXNAMELEN];
524 bslabel_t ds_sl, new_sl;
525 boolean_t new_default = FALSE;
527 int needed_priv = -1;
530 /* First get the existing dataset label. */
531 error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
532 1, sizeof (ds_hexsl), &ds_hexsl, NULL);
534 return (SET_ERROR(EPERM));
536 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
539 /* The label must be translatable */
540 if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
541 return (SET_ERROR(EINVAL));
544 * In a non-global zone, disallow attempts to set a label that
545 * doesn't match that of the zone; otherwise no other checks
548 if (!INGLOBALZONE(curproc)) {
549 if (new_default || !blequal(&new_sl, CR_SL(CRED())))
550 return (SET_ERROR(EPERM));
555 * For global-zone datasets (i.e., those whose zoned property is
556 * "off", verify that the specified new label is valid for the
559 if (dsl_prop_get_integer(name,
560 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
561 return (SET_ERROR(EPERM));
563 if (zfs_check_global_label(name, strval) != 0)
564 return (SET_ERROR(EPERM));
568 * If the existing dataset label is nondefault, check if the
569 * dataset is mounted (label cannot be changed while mounted).
570 * Get the zfsvfs; if there isn't one, then the dataset isn't
571 * mounted (or isn't a dataset, doesn't exist, ...).
573 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
575 static char *setsl_tag = "setsl_tag";
578 * Try to own the dataset; abort if there is any error,
579 * (e.g., already mounted, in use, or other error).
581 error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE,
584 return (SET_ERROR(EPERM));
586 dmu_objset_disown(os, setsl_tag);
589 needed_priv = PRIV_FILE_DOWNGRADE_SL;
593 if (hexstr_to_label(strval, &new_sl) != 0)
594 return (SET_ERROR(EPERM));
596 if (blstrictdom(&ds_sl, &new_sl))
597 needed_priv = PRIV_FILE_DOWNGRADE_SL;
598 else if (blstrictdom(&new_sl, &ds_sl))
599 needed_priv = PRIV_FILE_UPGRADE_SL;
601 /* dataset currently has a default label */
603 needed_priv = PRIV_FILE_UPGRADE_SL;
607 if (needed_priv != -1)
608 return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
611 #endif /* SECLABEL */
614 zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
620 * Check permissions for special properties.
625 * Disallow setting of 'zoned' from within a local zone.
627 if (!INGLOBALZONE(curthread))
628 return (SET_ERROR(EPERM));
632 case ZFS_PROP_FILESYSTEM_LIMIT:
633 case ZFS_PROP_SNAPSHOT_LIMIT:
634 if (!INGLOBALZONE(curthread)) {
636 char setpoint[MAXNAMELEN];
638 * Unprivileged users are allowed to modify the
639 * limit on things *under* (ie. contained by)
640 * the thing they own.
642 if (dsl_prop_get_integer(dsname, "jailed", &zoned,
644 return (SET_ERROR(EPERM));
645 if (!zoned || strlen(dsname) <= strlen(setpoint))
646 return (SET_ERROR(EPERM));
650 case ZFS_PROP_MLSLABEL:
652 if (!is_system_labeled())
653 return (SET_ERROR(EPERM));
655 if (nvpair_value_string(propval, &strval) == 0) {
658 err = zfs_set_slabel_policy(dsname, strval, CRED());
668 return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
673 zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
677 error = zfs_dozonecheck(zc->zc_name, cr);
682 * permission to set permissions will be evaluated later in
683 * dsl_deleg_can_allow()
690 zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
692 return (zfs_secpolicy_write_perms(zc->zc_name,
693 ZFS_DELEG_PERM_ROLLBACK, cr));
698 zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
706 * Generate the current snapshot name from the given objsetid, then
707 * use that name for the secpolicy/zone checks.
709 cp = strchr(zc->zc_name, '@');
711 return (SET_ERROR(EINVAL));
712 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
716 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
718 dsl_pool_rele(dp, FTAG);
722 dsl_dataset_name(ds, zc->zc_name);
724 error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
725 ZFS_DELEG_PERM_SEND, cr);
726 dsl_dataset_rele(ds, FTAG);
727 dsl_pool_rele(dp, FTAG);
734 zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
736 return (zfs_secpolicy_write_perms(zc->zc_name,
737 ZFS_DELEG_PERM_SEND, cr));
742 zfs_secpolicy_deleg_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
747 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
748 NO_FOLLOW, NULL, &vp)) != 0)
751 /* Now make sure mntpnt and dataset are ZFS */
753 if (strcmp(vp->v_vfsp->mnt_stat.f_fstypename, "zfs") != 0 ||
754 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
755 zc->zc_name) != 0)) {
757 return (SET_ERROR(EPERM));
761 return (dsl_deleg_access(zc->zc_name,
762 ZFS_DELEG_PERM_SHARE, cr));
766 zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
768 if (!INGLOBALZONE(curthread))
769 return (SET_ERROR(EPERM));
771 if (secpolicy_nfs(cr) == 0) {
774 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
779 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
781 if (!INGLOBALZONE(curthread))
782 return (SET_ERROR(EPERM));
784 if (secpolicy_smb(cr) == 0) {
787 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
792 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
797 * Remove the @bla or /bla from the end of the name to get the parent.
799 (void) strncpy(parent, datasetname, parentsize);
800 cp = strrchr(parent, '@');
804 cp = strrchr(parent, '/');
806 return (SET_ERROR(ENOENT));
814 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
818 if ((error = zfs_secpolicy_write_perms(name,
819 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
822 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
827 zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
829 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
833 * Destroying snapshots with delegated permissions requires
834 * descendant mount and destroy permissions.
838 zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
841 nvpair_t *pair, *nextpair;
844 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
845 return (SET_ERROR(EINVAL));
846 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
848 nextpair = nvlist_next_nvpair(snaps, pair);
849 error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr);
850 if (error == ENOENT) {
852 * Ignore any snapshots that don't exist (we consider
853 * them "already destroyed"). Remove the name from the
854 * nvl here in case the snapshot is created between
855 * now and when we try to destroy it (in which case
856 * we don't want to destroy it since we haven't
857 * checked for permission).
859 fnvlist_remove_nvpair(snaps, pair);
870 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
872 char parentname[MAXNAMELEN];
875 if ((error = zfs_secpolicy_write_perms(from,
876 ZFS_DELEG_PERM_RENAME, cr)) != 0)
879 if ((error = zfs_secpolicy_write_perms(from,
880 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
883 if ((error = zfs_get_parent(to, parentname,
884 sizeof (parentname))) != 0)
887 if ((error = zfs_secpolicy_write_perms(parentname,
888 ZFS_DELEG_PERM_CREATE, cr)) != 0)
891 if ((error = zfs_secpolicy_write_perms(parentname,
892 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
900 zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
905 if ((zc->zc_cookie & 1) != 0) {
907 * This is recursive rename, so the starting snapshot might
908 * not exist. Check file system or volume permission instead.
910 at = strchr(zc->zc_name, '@');
916 error = zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr);
926 zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
929 dsl_dataset_t *clone;
932 error = zfs_secpolicy_write_perms(zc->zc_name,
933 ZFS_DELEG_PERM_PROMOTE, cr);
937 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
941 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
944 char parentname[MAXNAMELEN];
945 dsl_dataset_t *origin = NULL;
949 error = dsl_dataset_hold_obj(dd->dd_pool,
950 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin);
952 dsl_dataset_rele(clone, FTAG);
953 dsl_pool_rele(dp, FTAG);
957 error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
958 ZFS_DELEG_PERM_MOUNT, cr);
960 dsl_dataset_name(origin, parentname);
962 error = zfs_secpolicy_write_perms_ds(parentname, origin,
963 ZFS_DELEG_PERM_PROMOTE, cr);
965 dsl_dataset_rele(clone, FTAG);
966 dsl_dataset_rele(origin, FTAG);
968 dsl_pool_rele(dp, FTAG);
974 zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
978 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
979 ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
982 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
983 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
986 return (zfs_secpolicy_write_perms(zc->zc_name,
987 ZFS_DELEG_PERM_CREATE, cr));
991 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
993 return (zfs_secpolicy_write_perms(name,
994 ZFS_DELEG_PERM_SNAPSHOT, cr));
998 * Check for permission to create each snapshot in the nvlist.
1002 zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1008 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
1009 return (SET_ERROR(EINVAL));
1010 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
1011 pair = nvlist_next_nvpair(snaps, pair)) {
1012 char *name = nvpair_name(pair);
1013 char *atp = strchr(name, '@');
1016 error = SET_ERROR(EINVAL);
1020 error = zfs_secpolicy_snapshot_perms(name, cr);
1029 * Check for permission to create each snapshot in the nvlist.
1033 zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1037 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
1038 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
1039 char *name = nvpair_name(pair);
1040 char *hashp = strchr(name, '#');
1042 if (hashp == NULL) {
1043 error = SET_ERROR(EINVAL);
1047 error = zfs_secpolicy_write_perms(name,
1048 ZFS_DELEG_PERM_BOOKMARK, cr);
1058 zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1060 nvpair_t *pair, *nextpair;
1063 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1065 char *name = nvpair_name(pair);
1066 char *hashp = strchr(name, '#');
1067 nextpair = nvlist_next_nvpair(innvl, pair);
1069 if (hashp == NULL) {
1070 error = SET_ERROR(EINVAL);
1075 error = zfs_secpolicy_write_perms(name,
1076 ZFS_DELEG_PERM_DESTROY, cr);
1078 if (error == ENOENT) {
1080 * Ignore any filesystems that don't exist (we consider
1081 * their bookmarks "already destroyed"). Remove
1082 * the name from the nvl here in case the filesystem
1083 * is created between now and when we try to destroy
1084 * the bookmark (in which case we don't want to
1085 * destroy it since we haven't checked for permission).
1087 fnvlist_remove_nvpair(innvl, pair);
1099 zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1102 * Even root must have a proper TSD so that we know what pool
1105 if (tsd_get(zfs_allow_log_key) == NULL)
1106 return (SET_ERROR(EPERM));
1111 zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1113 char parentname[MAXNAMELEN];
1117 if ((error = zfs_get_parent(zc->zc_name, parentname,
1118 sizeof (parentname))) != 0)
1121 if (nvlist_lookup_string(innvl, "origin", &origin) == 0 &&
1122 (error = zfs_secpolicy_write_perms(origin,
1123 ZFS_DELEG_PERM_CLONE, cr)) != 0)
1126 if ((error = zfs_secpolicy_write_perms(parentname,
1127 ZFS_DELEG_PERM_CREATE, cr)) != 0)
1130 return (zfs_secpolicy_write_perms(parentname,
1131 ZFS_DELEG_PERM_MOUNT, cr));
1135 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
1136 * SYS_CONFIG privilege, which is not available in a local zone.
1140 zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1142 if (secpolicy_sys_config(cr, B_FALSE) != 0)
1143 return (SET_ERROR(EPERM));
1149 * Policy for object to name lookups.
1153 zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1157 if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
1160 error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
1165 * Policy for fault injection. Requires all privileges.
1169 zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1171 return (secpolicy_zinject(cr));
1176 zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1178 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
1180 if (prop == ZPROP_INVAL) {
1181 if (!zfs_prop_user(zc->zc_value))
1182 return (SET_ERROR(EINVAL));
1183 return (zfs_secpolicy_write_perms(zc->zc_name,
1184 ZFS_DELEG_PERM_USERPROP, cr));
1186 return (zfs_secpolicy_setprop(zc->zc_name, prop,
1192 zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1194 int err = zfs_secpolicy_read(zc, innvl, cr);
1198 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1199 return (SET_ERROR(EINVAL));
1201 if (zc->zc_value[0] == 0) {
1203 * They are asking about a posix uid/gid. If it's
1204 * themself, allow it.
1206 if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
1207 zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
1208 if (zc->zc_guid == crgetuid(cr))
1211 if (groupmember(zc->zc_guid, cr))
1216 return (zfs_secpolicy_write_perms(zc->zc_name,
1217 userquota_perms[zc->zc_objset_type], cr));
1221 zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1223 int err = zfs_secpolicy_read(zc, innvl, cr);
1227 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1228 return (SET_ERROR(EINVAL));
1230 return (zfs_secpolicy_write_perms(zc->zc_name,
1231 userquota_perms[zc->zc_objset_type], cr));
1236 zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1238 return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
1244 zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1250 error = nvlist_lookup_nvlist(innvl, "holds", &holds);
1252 return (SET_ERROR(EINVAL));
1254 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
1255 pair = nvlist_next_nvpair(holds, pair)) {
1256 char fsname[MAXNAMELEN];
1257 error = dmu_fsname(nvpair_name(pair), fsname);
1260 error = zfs_secpolicy_write_perms(fsname,
1261 ZFS_DELEG_PERM_HOLD, cr);
1270 zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1275 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1276 pair = nvlist_next_nvpair(innvl, pair)) {
1277 char fsname[MAXNAMELEN];
1278 error = dmu_fsname(nvpair_name(pair), fsname);
1281 error = zfs_secpolicy_write_perms(fsname,
1282 ZFS_DELEG_PERM_RELEASE, cr);
1290 * Policy for allowing temporary snapshots to be taken or released
1293 zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1296 * A temporary snapshot is the same as a snapshot,
1297 * hold, destroy and release all rolled into one.
1298 * Delegated diff alone is sufficient that we allow this.
1302 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1303 ZFS_DELEG_PERM_DIFF, cr)) == 0)
1306 error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
1308 error = zfs_secpolicy_hold(zc, innvl, cr);
1310 error = zfs_secpolicy_release(zc, innvl, cr);
1312 error = zfs_secpolicy_destroy(zc, innvl, cr);
1317 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1320 get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1324 nvlist_t *list = NULL;
1327 * Read in and unpack the user-supplied nvlist.
1330 return (SET_ERROR(EINVAL));
1332 packed = kmem_alloc(size, KM_SLEEP);
1334 if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1336 kmem_free(packed, size);
1340 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1341 kmem_free(packed, size);
1345 kmem_free(packed, size);
1352 * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
1353 * Entries will be removed from the end of the nvlist, and one int32 entry
1354 * named "N_MORE_ERRORS" will be added indicating how many entries were
1358 nvlist_smush(nvlist_t *errors, size_t max)
1362 size = fnvlist_size(errors);
1365 nvpair_t *more_errors;
1369 return (SET_ERROR(ENOMEM));
1371 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
1372 more_errors = nvlist_prev_nvpair(errors, NULL);
1375 nvpair_t *pair = nvlist_prev_nvpair(errors,
1377 fnvlist_remove_nvpair(errors, pair);
1379 size = fnvlist_size(errors);
1380 } while (size > max);
1382 fnvlist_remove_nvpair(errors, more_errors);
1383 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
1384 ASSERT3U(fnvlist_size(errors), <=, max);
1391 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1393 char *packed = NULL;
1397 size = fnvlist_size(nvl);
1399 if (size > zc->zc_nvlist_dst_size) {
1401 * Solaris returns ENOMEM here, because even if an error is
1402 * returned from an ioctl(2), new zc_nvlist_dst_size will be
1403 * passed to the userland. This is not the case for FreeBSD.
1404 * We need to return 0, so the kernel will copy the
1405 * zc_nvlist_dst_size back and the userland can discover that a
1406 * bigger buffer is needed.
1410 packed = fnvlist_pack(nvl, &size);
1411 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1412 size, zc->zc_iflags) != 0)
1413 error = SET_ERROR(EFAULT);
1414 fnvlist_pack_free(packed, size);
1417 zc->zc_nvlist_dst_size = size;
1418 zc->zc_nvlist_dst_filled = B_TRUE;
1423 getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
1428 error = dmu_objset_hold(dsname, FTAG, &os);
1431 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1432 dmu_objset_rele(os, FTAG);
1433 return (SET_ERROR(EINVAL));
1436 mutex_enter(&os->os_user_ptr_lock);
1437 *zfvp = dmu_objset_get_user(os);
1439 VFS_HOLD((*zfvp)->z_vfs);
1441 error = SET_ERROR(ESRCH);
1443 mutex_exit(&os->os_user_ptr_lock);
1444 dmu_objset_rele(os, FTAG);
1449 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
1450 * case its z_vfs will be NULL, and it will be opened as the owner.
1451 * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1452 * which prevents all vnode ops from running.
1455 zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
1459 if (getzfsvfs(name, zfvp) != 0)
1460 error = zfsvfs_create(name, zfvp);
1462 rrm_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
1464 if ((*zfvp)->z_unmounted) {
1466 * XXX we could probably try again, since the unmounting
1467 * thread should be just about to disassociate the
1468 * objset from the zfsvfs.
1470 rrm_exit(&(*zfvp)->z_teardown_lock, tag);
1471 return (SET_ERROR(EBUSY));
1478 zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
1480 rrm_exit(&zfsvfs->z_teardown_lock, tag);
1482 if (zfsvfs->z_vfs) {
1483 VFS_RELE(zfsvfs->z_vfs);
1485 dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1486 zfsvfs_free(zfsvfs);
1491 zfs_ioc_pool_create(zfs_cmd_t *zc)
1494 nvlist_t *config, *props = NULL;
1495 nvlist_t *rootprops = NULL;
1496 nvlist_t *zplprops = NULL;
1498 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1499 zc->zc_iflags, &config))
1502 if (zc->zc_nvlist_src_size != 0 && (error =
1503 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1504 zc->zc_iflags, &props))) {
1505 nvlist_free(config);
1510 nvlist_t *nvl = NULL;
1511 uint64_t version = SPA_VERSION;
1513 (void) nvlist_lookup_uint64(props,
1514 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1515 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1516 error = SET_ERROR(EINVAL);
1517 goto pool_props_bad;
1519 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
1521 error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
1523 nvlist_free(config);
1527 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1529 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1530 error = zfs_fill_zplprops_root(version, rootprops,
1533 goto pool_props_bad;
1536 error = spa_create(zc->zc_name, config, props, zplprops);
1539 * Set the remaining root properties
1541 if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
1542 ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
1543 (void) spa_destroy(zc->zc_name);
1546 nvlist_free(rootprops);
1547 nvlist_free(zplprops);
1548 nvlist_free(config);
1555 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1558 zfs_log_history(zc);
1559 error = spa_destroy(zc->zc_name);
1561 zvol_remove_minors(zc->zc_name);
1566 zfs_ioc_pool_import(zfs_cmd_t *zc)
1568 nvlist_t *config, *props = NULL;
1572 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1573 zc->zc_iflags, &config)) != 0)
1576 if (zc->zc_nvlist_src_size != 0 && (error =
1577 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1578 zc->zc_iflags, &props))) {
1579 nvlist_free(config);
1583 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1584 guid != zc->zc_guid)
1585 error = SET_ERROR(EINVAL);
1587 error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1589 if (zc->zc_nvlist_dst != 0) {
1592 if ((err = put_nvlist(zc, config)) != 0)
1596 nvlist_free(config);
1605 zfs_ioc_pool_export(zfs_cmd_t *zc)
1608 boolean_t force = (boolean_t)zc->zc_cookie;
1609 boolean_t hardforce = (boolean_t)zc->zc_guid;
1611 zfs_log_history(zc);
1612 error = spa_export(zc->zc_name, NULL, force, hardforce);
1614 zvol_remove_minors(zc->zc_name);
1619 zfs_ioc_pool_configs(zfs_cmd_t *zc)
1624 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1625 return (SET_ERROR(EEXIST));
1627 error = put_nvlist(zc, configs);
1629 nvlist_free(configs);
1636 * zc_name name of the pool
1639 * zc_cookie real errno
1640 * zc_nvlist_dst config nvlist
1641 * zc_nvlist_dst_size size of config nvlist
1644 zfs_ioc_pool_stats(zfs_cmd_t *zc)
1650 error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1651 sizeof (zc->zc_value));
1653 if (config != NULL) {
1654 ret = put_nvlist(zc, config);
1655 nvlist_free(config);
1658 * The config may be present even if 'error' is non-zero.
1659 * In this case we return success, and preserve the real errno
1662 zc->zc_cookie = error;
1671 * Try to import the given pool, returning pool stats as appropriate so that
1672 * user land knows which devices are available and overall pool health.
1675 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1677 nvlist_t *tryconfig, *config;
1680 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1681 zc->zc_iflags, &tryconfig)) != 0)
1684 config = spa_tryimport(tryconfig);
1686 nvlist_free(tryconfig);
1689 return (SET_ERROR(EINVAL));
1691 error = put_nvlist(zc, config);
1692 nvlist_free(config);
1699 * zc_name name of the pool
1700 * zc_cookie scan func (pool_scan_func_t)
1703 zfs_ioc_pool_scan(zfs_cmd_t *zc)
1708 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1711 if (zc->zc_cookie == POOL_SCAN_NONE)
1712 error = spa_scan_stop(spa);
1714 error = spa_scan(spa, zc->zc_cookie);
1716 spa_close(spa, FTAG);
1722 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1727 error = spa_open(zc->zc_name, &spa, FTAG);
1730 spa_close(spa, FTAG);
1736 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1741 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1744 if (zc->zc_cookie < spa_version(spa) ||
1745 !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1746 spa_close(spa, FTAG);
1747 return (SET_ERROR(EINVAL));
1750 spa_upgrade(spa, zc->zc_cookie);
1751 spa_close(spa, FTAG);
1757 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1764 if ((size = zc->zc_history_len) == 0)
1765 return (SET_ERROR(EINVAL));
1767 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1770 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1771 spa_close(spa, FTAG);
1772 return (SET_ERROR(ENOTSUP));
1775 hist_buf = kmem_alloc(size, KM_SLEEP);
1776 if ((error = spa_history_get(spa, &zc->zc_history_offset,
1777 &zc->zc_history_len, hist_buf)) == 0) {
1778 error = ddi_copyout(hist_buf,
1779 (void *)(uintptr_t)zc->zc_history,
1780 zc->zc_history_len, zc->zc_iflags);
1783 spa_close(spa, FTAG);
1784 kmem_free(hist_buf, size);
1789 zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1794 error = spa_open(zc->zc_name, &spa, FTAG);
1796 error = spa_change_guid(spa);
1797 spa_close(spa, FTAG);
1803 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1805 return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
1810 * zc_name name of filesystem
1811 * zc_obj object to find
1814 * zc_value name of object
1817 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1822 /* XXX reading from objset not owned */
1823 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1825 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1826 dmu_objset_rele(os, FTAG);
1827 return (SET_ERROR(EINVAL));
1829 error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
1830 sizeof (zc->zc_value));
1831 dmu_objset_rele(os, FTAG);
1838 * zc_name name of filesystem
1839 * zc_obj object to find
1842 * zc_stat stats on object
1843 * zc_value path to object
1846 zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1851 /* XXX reading from objset not owned */
1852 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1854 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1855 dmu_objset_rele(os, FTAG);
1856 return (SET_ERROR(EINVAL));
1858 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1859 sizeof (zc->zc_value));
1860 dmu_objset_rele(os, FTAG);
1866 zfs_ioc_vdev_add(zfs_cmd_t *zc)
1870 nvlist_t *config, **l2cache, **spares;
1871 uint_t nl2cache = 0, nspares = 0;
1873 error = spa_open(zc->zc_name, &spa, FTAG);
1877 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1878 zc->zc_iflags, &config);
1879 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1880 &l2cache, &nl2cache);
1882 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1887 * A root pool with concatenated devices is not supported.
1888 * Thus, can not add a device to a root pool.
1890 * Intent log device can not be added to a rootpool because
1891 * during mountroot, zil is replayed, a seperated log device
1892 * can not be accessed during the mountroot time.
1894 * l2cache and spare devices are ok to be added to a rootpool.
1896 if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
1897 nvlist_free(config);
1898 spa_close(spa, FTAG);
1899 return (SET_ERROR(EDOM));
1901 #endif /* illumos */
1904 error = spa_vdev_add(spa, config);
1905 nvlist_free(config);
1907 spa_close(spa, FTAG);
1913 * zc_name name of the pool
1914 * zc_nvlist_conf nvlist of devices to remove
1915 * zc_cookie to stop the remove?
1918 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1923 error = spa_open(zc->zc_name, &spa, FTAG);
1926 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1927 spa_close(spa, FTAG);
1932 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1936 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1938 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1940 switch (zc->zc_cookie) {
1941 case VDEV_STATE_ONLINE:
1942 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1945 case VDEV_STATE_OFFLINE:
1946 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1949 case VDEV_STATE_FAULTED:
1950 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1951 zc->zc_obj != VDEV_AUX_EXTERNAL)
1952 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1954 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
1957 case VDEV_STATE_DEGRADED:
1958 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1959 zc->zc_obj != VDEV_AUX_EXTERNAL)
1960 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1962 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
1966 error = SET_ERROR(EINVAL);
1968 zc->zc_cookie = newstate;
1969 spa_close(spa, FTAG);
1974 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1977 int replacing = zc->zc_cookie;
1981 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1984 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1985 zc->zc_iflags, &config)) == 0) {
1986 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1987 nvlist_free(config);
1990 spa_close(spa, FTAG);
1995 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
2000 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2003 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
2005 spa_close(spa, FTAG);
2010 zfs_ioc_vdev_split(zfs_cmd_t *zc)
2013 nvlist_t *config, *props = NULL;
2015 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
2017 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2020 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
2021 zc->zc_iflags, &config)) {
2022 spa_close(spa, FTAG);
2026 if (zc->zc_nvlist_src_size != 0 && (error =
2027 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2028 zc->zc_iflags, &props))) {
2029 spa_close(spa, FTAG);
2030 nvlist_free(config);
2034 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
2036 spa_close(spa, FTAG);
2038 nvlist_free(config);
2045 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
2048 char *path = zc->zc_value;
2049 uint64_t guid = zc->zc_guid;
2052 error = spa_open(zc->zc_name, &spa, FTAG);
2056 error = spa_vdev_setpath(spa, guid, path);
2057 spa_close(spa, FTAG);
2062 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
2065 char *fru = zc->zc_value;
2066 uint64_t guid = zc->zc_guid;
2069 error = spa_open(zc->zc_name, &spa, FTAG);
2073 error = spa_vdev_setfru(spa, guid, fru);
2074 spa_close(spa, FTAG);
2079 zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
2084 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2086 if (zc->zc_nvlist_dst != 0 &&
2087 (error = dsl_prop_get_all(os, &nv)) == 0) {
2088 dmu_objset_stats(os, nv);
2090 * NB: zvol_get_stats() will read the objset contents,
2091 * which we aren't supposed to do with a
2092 * DS_MODE_USER hold, because it could be
2093 * inconsistent. So this is a bit of a workaround...
2094 * XXX reading with out owning
2096 if (!zc->zc_objset_stats.dds_inconsistent &&
2097 dmu_objset_type(os) == DMU_OST_ZVOL) {
2098 error = zvol_get_stats(os, nv);
2103 error = put_nvlist(zc, nv);
2112 * zc_name name of filesystem
2113 * zc_nvlist_dst_size size of buffer for property nvlist
2116 * zc_objset_stats stats
2117 * zc_nvlist_dst property nvlist
2118 * zc_nvlist_dst_size size of property nvlist
2121 zfs_ioc_objset_stats(zfs_cmd_t *zc)
2126 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2128 error = zfs_ioc_objset_stats_impl(zc, os);
2129 dmu_objset_rele(os, FTAG);
2132 if (error == ENOMEM)
2139 * zc_name name of filesystem
2140 * zc_nvlist_dst_size size of buffer for property nvlist
2143 * zc_nvlist_dst received property nvlist
2144 * zc_nvlist_dst_size size of received property nvlist
2146 * Gets received properties (distinct from local properties on or after
2147 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
2148 * local property values.
2151 zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
2157 * Without this check, we would return local property values if the
2158 * caller has not already received properties on or after
2159 * SPA_VERSION_RECVD_PROPS.
2161 if (!dsl_prop_get_hasrecvd(zc->zc_name))
2162 return (SET_ERROR(ENOTSUP));
2164 if (zc->zc_nvlist_dst != 0 &&
2165 (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
2166 error = put_nvlist(zc, nv);
2174 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2180 * zfs_get_zplprop() will either find a value or give us
2181 * the default value (if there is one).
2183 if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2185 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2191 * zc_name name of filesystem
2192 * zc_nvlist_dst_size size of buffer for zpl property nvlist
2195 * zc_nvlist_dst zpl property nvlist
2196 * zc_nvlist_dst_size size of zpl property nvlist
2199 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2204 /* XXX reading without owning */
2205 if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
2208 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2211 * NB: nvl_add_zplprop() will read the objset contents,
2212 * which we aren't supposed to do with a DS_MODE_USER
2213 * hold, because it could be inconsistent.
2215 if (zc->zc_nvlist_dst != 0 &&
2216 !zc->zc_objset_stats.dds_inconsistent &&
2217 dmu_objset_type(os) == DMU_OST_ZFS) {
2220 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2221 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2222 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2223 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2224 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2225 err = put_nvlist(zc, nv);
2228 err = SET_ERROR(ENOENT);
2230 dmu_objset_rele(os, FTAG);
2235 dataset_name_hidden(const char *name)
2238 * Skip over datasets that are not visible in this zone,
2239 * internal datasets (which have a $ in their name), and
2240 * temporary datasets (which have a % in their name).
2242 if (strchr(name, '$') != NULL)
2244 if (strchr(name, '%') != NULL)
2246 if (!INGLOBALZONE(curthread) && !zone_dataset_visible(name, NULL))
2253 * zc_name name of filesystem
2254 * zc_cookie zap cursor
2255 * zc_nvlist_dst_size size of buffer for property nvlist
2258 * zc_name name of next filesystem
2259 * zc_cookie zap cursor
2260 * zc_objset_stats stats
2261 * zc_nvlist_dst property nvlist
2262 * zc_nvlist_dst_size size of property nvlist
2265 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2270 size_t orig_len = strlen(zc->zc_name);
2273 if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
2274 if (error == ENOENT)
2275 error = SET_ERROR(ESRCH);
2279 p = strrchr(zc->zc_name, '/');
2280 if (p == NULL || p[1] != '\0')
2281 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2282 p = zc->zc_name + strlen(zc->zc_name);
2285 error = dmu_dir_list_next(os,
2286 sizeof (zc->zc_name) - (p - zc->zc_name), p,
2287 NULL, &zc->zc_cookie);
2288 if (error == ENOENT)
2289 error = SET_ERROR(ESRCH);
2290 } while (error == 0 && dataset_name_hidden(zc->zc_name));
2291 dmu_objset_rele(os, FTAG);
2294 * If it's an internal dataset (ie. with a '$' in its name),
2295 * don't try to get stats for it, otherwise we'll return ENOENT.
2297 if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
2298 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2299 if (error == ENOENT) {
2300 /* We lost a race with destroy, get the next one. */
2301 zc->zc_name[orig_len] = '\0';
2310 * zc_name name of filesystem
2311 * zc_cookie zap cursor
2312 * zc_nvlist_dst_size size of buffer for property nvlist
2313 * zc_simple when set, only name is requested
2316 * zc_name name of next snapshot
2317 * zc_objset_stats stats
2318 * zc_nvlist_dst property nvlist
2319 * zc_nvlist_dst_size size of property nvlist
2322 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2327 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2329 return (error == ENOENT ? ESRCH : error);
2333 * A dataset name of maximum length cannot have any snapshots,
2334 * so exit immediately.
2336 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
2337 dmu_objset_rele(os, FTAG);
2338 return (SET_ERROR(ESRCH));
2341 error = dmu_snapshot_list_next(os,
2342 sizeof (zc->zc_name) - strlen(zc->zc_name),
2343 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2346 if (error == 0 && !zc->zc_simple) {
2348 dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2350 error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
2354 error = dmu_objset_from_ds(ds, &ossnap);
2356 error = zfs_ioc_objset_stats_impl(zc, ossnap);
2357 dsl_dataset_rele(ds, FTAG);
2359 } else if (error == ENOENT) {
2360 error = SET_ERROR(ESRCH);
2363 dmu_objset_rele(os, FTAG);
2364 /* if we failed, undo the @ that we tacked on to zc_name */
2366 *strchr(zc->zc_name, '@') = '\0';
2371 zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2373 const char *propname = nvpair_name(pair);
2375 unsigned int vallen;
2378 zfs_userquota_prop_t type;
2384 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2386 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2387 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2389 return (SET_ERROR(EINVAL));
2393 * A correctly constructed propname is encoded as
2394 * userquota@<rid>-<domain>.
2396 if ((dash = strchr(propname, '-')) == NULL ||
2397 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2399 return (SET_ERROR(EINVAL));
2406 err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
2408 err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
2409 zfsvfs_rele(zfsvfs, FTAG);
2416 * If the named property is one that has a special function to set its value,
2417 * return 0 on success and a positive error code on failure; otherwise if it is
2418 * not one of the special properties handled by this function, return -1.
2420 * XXX: It would be better for callers of the property interface if we handled
2421 * these special cases in dsl_prop.c (in the dsl layer).
2424 zfs_prop_set_special(const char *dsname, zprop_source_t source,
2427 const char *propname = nvpair_name(pair);
2428 zfs_prop_t prop = zfs_name_to_prop(propname);
2432 if (prop == ZPROP_INVAL) {
2433 if (zfs_prop_userquota(propname))
2434 return (zfs_prop_set_userquota(dsname, pair));
2438 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2440 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2441 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2445 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
2448 VERIFY(0 == nvpair_value_uint64(pair, &intval));
2451 case ZFS_PROP_QUOTA:
2452 err = dsl_dir_set_quota(dsname, source, intval);
2454 case ZFS_PROP_REFQUOTA:
2455 err = dsl_dataset_set_refquota(dsname, source, intval);
2457 case ZFS_PROP_FILESYSTEM_LIMIT:
2458 case ZFS_PROP_SNAPSHOT_LIMIT:
2459 if (intval == UINT64_MAX) {
2460 /* clearing the limit, just do it */
2463 err = dsl_dir_activate_fs_ss_limit(dsname);
2466 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2467 * default path to set the value in the nvlist.
2472 case ZFS_PROP_RESERVATION:
2473 err = dsl_dir_set_reservation(dsname, source, intval);
2475 case ZFS_PROP_REFRESERVATION:
2476 err = dsl_dataset_set_refreservation(dsname, source, intval);
2478 case ZFS_PROP_VOLSIZE:
2479 err = zvol_set_volsize(dsname, intval);
2481 case ZFS_PROP_VERSION:
2485 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2488 err = zfs_set_version(zfsvfs, intval);
2489 zfsvfs_rele(zfsvfs, FTAG);
2491 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2494 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2495 (void) strcpy(zc->zc_name, dsname);
2496 (void) zfs_ioc_userspace_upgrade(zc);
2497 kmem_free(zc, sizeof (zfs_cmd_t));
2509 * This function is best effort. If it fails to set any of the given properties,
2510 * it continues to set as many as it can and returns the last error
2511 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2512 * with the list of names of all the properties that failed along with the
2513 * corresponding error numbers.
2515 * If every property is set successfully, zero is returned and errlist is not
2519 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2527 nvlist_t *genericnvl = fnvlist_alloc();
2528 nvlist_t *retrynvl = fnvlist_alloc();
2532 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2533 const char *propname = nvpair_name(pair);
2534 zfs_prop_t prop = zfs_name_to_prop(propname);
2537 /* decode the property value */
2539 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2541 attrs = fnvpair_value_nvlist(pair);
2542 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2544 err = SET_ERROR(EINVAL);
2547 /* Validate value type */
2548 if (err == 0 && prop == ZPROP_INVAL) {
2549 if (zfs_prop_user(propname)) {
2550 if (nvpair_type(propval) != DATA_TYPE_STRING)
2551 err = SET_ERROR(EINVAL);
2552 } else if (zfs_prop_userquota(propname)) {
2553 if (nvpair_type(propval) !=
2554 DATA_TYPE_UINT64_ARRAY)
2555 err = SET_ERROR(EINVAL);
2557 err = SET_ERROR(EINVAL);
2559 } else if (err == 0) {
2560 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2561 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2562 err = SET_ERROR(EINVAL);
2563 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2566 intval = fnvpair_value_uint64(propval);
2568 switch (zfs_prop_get_type(prop)) {
2569 case PROP_TYPE_NUMBER:
2571 case PROP_TYPE_STRING:
2572 err = SET_ERROR(EINVAL);
2574 case PROP_TYPE_INDEX:
2575 if (zfs_prop_index_to_string(prop,
2576 intval, &unused) != 0)
2577 err = SET_ERROR(EINVAL);
2581 "unknown property type");
2584 err = SET_ERROR(EINVAL);
2588 /* Validate permissions */
2590 err = zfs_check_settable(dsname, pair, CRED());
2593 err = zfs_prop_set_special(dsname, source, pair);
2596 * For better performance we build up a list of
2597 * properties to set in a single transaction.
2599 err = nvlist_add_nvpair(genericnvl, pair);
2600 } else if (err != 0 && nvl != retrynvl) {
2602 * This may be a spurious error caused by
2603 * receiving quota and reservation out of order.
2604 * Try again in a second pass.
2606 err = nvlist_add_nvpair(retrynvl, pair);
2611 if (errlist != NULL)
2612 fnvlist_add_int32(errlist, propname, err);
2617 if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2622 if (!nvlist_empty(genericnvl) &&
2623 dsl_props_set(dsname, source, genericnvl) != 0) {
2625 * If this fails, we still want to set as many properties as we
2626 * can, so try setting them individually.
2629 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2630 const char *propname = nvpair_name(pair);
2634 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2636 attrs = fnvpair_value_nvlist(pair);
2637 propval = fnvlist_lookup_nvpair(attrs,
2641 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2642 strval = fnvpair_value_string(propval);
2643 err = dsl_prop_set_string(dsname, propname,
2646 intval = fnvpair_value_uint64(propval);
2647 err = dsl_prop_set_int(dsname, propname, source,
2652 if (errlist != NULL) {
2653 fnvlist_add_int32(errlist, propname,
2660 nvlist_free(genericnvl);
2661 nvlist_free(retrynvl);
2667 * Check that all the properties are valid user properties.
2670 zfs_check_userprops(const char *fsname, nvlist_t *nvl)
2672 nvpair_t *pair = NULL;
2675 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2676 const char *propname = nvpair_name(pair);
2678 if (!zfs_prop_user(propname) ||
2679 nvpair_type(pair) != DATA_TYPE_STRING)
2680 return (SET_ERROR(EINVAL));
2682 if (error = zfs_secpolicy_write_perms(fsname,
2683 ZFS_DELEG_PERM_USERPROP, CRED()))
2686 if (strlen(propname) >= ZAP_MAXNAMELEN)
2687 return (SET_ERROR(ENAMETOOLONG));
2689 if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN)
2696 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2700 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2703 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2704 if (nvlist_exists(skipped, nvpair_name(pair)))
2707 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2712 clear_received_props(const char *dsname, nvlist_t *props,
2716 nvlist_t *cleared_props = NULL;
2717 props_skip(props, skipped, &cleared_props);
2718 if (!nvlist_empty(cleared_props)) {
2720 * Acts on local properties until the dataset has received
2721 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2723 zprop_source_t flags = (ZPROP_SRC_NONE |
2724 (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
2725 err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
2727 nvlist_free(cleared_props);
2733 * zc_name name of filesystem
2734 * zc_value name of property to set
2735 * zc_nvlist_src{_size} nvlist of properties to apply
2736 * zc_cookie received properties flag
2739 * zc_nvlist_dst{_size} error for each unapplied received property
2742 zfs_ioc_set_prop(zfs_cmd_t *zc)
2745 boolean_t received = zc->zc_cookie;
2746 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2751 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2752 zc->zc_iflags, &nvl)) != 0)
2756 nvlist_t *origprops;
2758 if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
2759 (void) clear_received_props(zc->zc_name,
2761 nvlist_free(origprops);
2764 error = dsl_prop_set_hasrecvd(zc->zc_name);
2767 errors = fnvlist_alloc();
2769 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
2771 if (zc->zc_nvlist_dst != 0 && errors != NULL) {
2772 (void) put_nvlist(zc, errors);
2775 nvlist_free(errors);
2782 * zc_name name of filesystem
2783 * zc_value name of property to inherit
2784 * zc_cookie revert to received value if TRUE
2789 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2791 const char *propname = zc->zc_value;
2792 zfs_prop_t prop = zfs_name_to_prop(propname);
2793 boolean_t received = zc->zc_cookie;
2794 zprop_source_t source = (received
2795 ? ZPROP_SRC_NONE /* revert to received value, if any */
2796 : ZPROP_SRC_INHERITED); /* explicitly inherit */
2805 * zfs_prop_set_special() expects properties in the form of an
2806 * nvpair with type info.
2808 if (prop == ZPROP_INVAL) {
2809 if (!zfs_prop_user(propname))
2810 return (SET_ERROR(EINVAL));
2812 type = PROP_TYPE_STRING;
2813 } else if (prop == ZFS_PROP_VOLSIZE ||
2814 prop == ZFS_PROP_VERSION) {
2815 return (SET_ERROR(EINVAL));
2817 type = zfs_prop_get_type(prop);
2820 VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2823 case PROP_TYPE_STRING:
2824 VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2826 case PROP_TYPE_NUMBER:
2827 case PROP_TYPE_INDEX:
2828 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2832 return (SET_ERROR(EINVAL));
2835 pair = nvlist_next_nvpair(dummy, NULL);
2836 err = zfs_prop_set_special(zc->zc_name, source, pair);
2839 return (err); /* special property already handled */
2842 * Only check this in the non-received case. We want to allow
2843 * 'inherit -S' to revert non-inheritable properties like quota
2844 * and reservation to the received or default values even though
2845 * they are not considered inheritable.
2847 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2848 return (SET_ERROR(EINVAL));
2851 /* property name has been validated by zfs_secpolicy_inherit_prop() */
2852 return (dsl_prop_inherit(zc->zc_name, zc->zc_value, source));
2856 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2863 if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2864 zc->zc_iflags, &props))
2868 * If the only property is the configfile, then just do a spa_lookup()
2869 * to handle the faulted case.
2871 pair = nvlist_next_nvpair(props, NULL);
2872 if (pair != NULL && strcmp(nvpair_name(pair),
2873 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2874 nvlist_next_nvpair(props, pair) == NULL) {
2875 mutex_enter(&spa_namespace_lock);
2876 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2877 spa_configfile_set(spa, props, B_FALSE);
2878 spa_config_sync(spa, B_FALSE, B_TRUE);
2880 mutex_exit(&spa_namespace_lock);
2887 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2892 error = spa_prop_set(spa, props);
2895 spa_close(spa, FTAG);
2901 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2905 nvlist_t *nvp = NULL;
2907 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2909 * If the pool is faulted, there may be properties we can still
2910 * get (such as altroot and cachefile), so attempt to get them
2913 mutex_enter(&spa_namespace_lock);
2914 if ((spa = spa_lookup(zc->zc_name)) != NULL)
2915 error = spa_prop_get(spa, &nvp);
2916 mutex_exit(&spa_namespace_lock);
2918 error = spa_prop_get(spa, &nvp);
2919 spa_close(spa, FTAG);
2922 if (error == 0 && zc->zc_nvlist_dst != 0)
2923 error = put_nvlist(zc, nvp);
2925 error = SET_ERROR(EFAULT);
2933 * zc_name name of filesystem
2934 * zc_nvlist_src{_size} nvlist of delegated permissions
2935 * zc_perm_action allow/unallow flag
2940 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2943 nvlist_t *fsaclnv = NULL;
2945 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2946 zc->zc_iflags, &fsaclnv)) != 0)
2950 * Verify nvlist is constructed correctly
2952 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2953 nvlist_free(fsaclnv);
2954 return (SET_ERROR(EINVAL));
2958 * If we don't have PRIV_SYS_MOUNT, then validate
2959 * that user is allowed to hand out each permission in
2963 error = secpolicy_zfs(CRED());
2965 if (zc->zc_perm_action == B_FALSE) {
2966 error = dsl_deleg_can_allow(zc->zc_name,
2969 error = dsl_deleg_can_unallow(zc->zc_name,
2975 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2977 nvlist_free(fsaclnv);
2983 * zc_name name of filesystem
2986 * zc_nvlist_src{_size} nvlist of delegated permissions
2989 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
2994 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
2995 error = put_nvlist(zc, nvp);
3003 * Search the vfs list for a specified resource. Returns a pointer to it
3004 * or NULL if no suitable entry is found. The caller of this routine
3005 * is responsible for releasing the returned vfs pointer.
3008 zfs_get_vfs(const char *resource)
3012 mtx_lock(&mountlist_mtx);
3013 TAILQ_FOREACH(vfsp, &mountlist, mnt_list) {
3014 if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
3019 mtx_unlock(&mountlist_mtx);
3025 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3027 zfs_creat_t *zct = arg;
3029 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
3032 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
3036 * os parent objset pointer (NULL if root fs)
3037 * fuids_ok fuids allowed in this version of the spa?
3038 * sa_ok SAs allowed in this version of the spa?
3039 * createprops list of properties requested by creator
3042 * zplprops values for the zplprops we attach to the master node object
3043 * is_ci true if requested file system will be purely case-insensitive
3045 * Determine the settings for utf8only, normalization and
3046 * casesensitivity. Specific values may have been requested by the
3047 * creator and/or we can inherit values from the parent dataset. If
3048 * the file system is of too early a vintage, a creator can not
3049 * request settings for these properties, even if the requested
3050 * setting is the default value. We don't actually want to create dsl
3051 * properties for these, so remove them from the source nvlist after
3055 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
3056 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
3057 nvlist_t *zplprops, boolean_t *is_ci)
3059 uint64_t sense = ZFS_PROP_UNDEFINED;
3060 uint64_t norm = ZFS_PROP_UNDEFINED;
3061 uint64_t u8 = ZFS_PROP_UNDEFINED;
3063 ASSERT(zplprops != NULL);
3066 * Pull out creator prop choices, if any.
3069 (void) nvlist_lookup_uint64(createprops,
3070 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
3071 (void) nvlist_lookup_uint64(createprops,
3072 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
3073 (void) nvlist_remove_all(createprops,
3074 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
3075 (void) nvlist_lookup_uint64(createprops,
3076 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
3077 (void) nvlist_remove_all(createprops,
3078 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
3079 (void) nvlist_lookup_uint64(createprops,
3080 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
3081 (void) nvlist_remove_all(createprops,
3082 zfs_prop_to_name(ZFS_PROP_CASE));
3086 * If the zpl version requested is whacky or the file system
3087 * or pool is version is too "young" to support normalization
3088 * and the creator tried to set a value for one of the props,
3091 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
3092 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
3093 (zplver >= ZPL_VERSION_SA && !sa_ok) ||
3094 (zplver < ZPL_VERSION_NORMALIZATION &&
3095 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
3096 sense != ZFS_PROP_UNDEFINED)))
3097 return (SET_ERROR(ENOTSUP));
3100 * Put the version in the zplprops
3102 VERIFY(nvlist_add_uint64(zplprops,
3103 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3105 if (norm == ZFS_PROP_UNDEFINED)
3106 VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
3107 VERIFY(nvlist_add_uint64(zplprops,
3108 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3111 * If we're normalizing, names must always be valid UTF-8 strings.
3115 if (u8 == ZFS_PROP_UNDEFINED)
3116 VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
3117 VERIFY(nvlist_add_uint64(zplprops,
3118 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3120 if (sense == ZFS_PROP_UNDEFINED)
3121 VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
3122 VERIFY(nvlist_add_uint64(zplprops,
3123 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3126 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
3132 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
3133 nvlist_t *zplprops, boolean_t *is_ci)
3135 boolean_t fuids_ok, sa_ok;
3136 uint64_t zplver = ZPL_VERSION;
3137 objset_t *os = NULL;
3138 char parentname[MAXNAMELEN];
3144 (void) strlcpy(parentname, dataset, sizeof (parentname));
3145 cp = strrchr(parentname, '/');
3149 if ((error = spa_open(dataset, &spa, FTAG)) != 0)
3152 spa_vers = spa_version(spa);
3153 spa_close(spa, FTAG);
3155 zplver = zfs_zpl_version_map(spa_vers);
3156 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3157 sa_ok = (zplver >= ZPL_VERSION_SA);
3160 * Open parent object set so we can inherit zplprop values.
3162 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
3165 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
3167 dmu_objset_rele(os, FTAG);
3172 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
3173 nvlist_t *zplprops, boolean_t *is_ci)
3177 uint64_t zplver = ZPL_VERSION;
3180 zplver = zfs_zpl_version_map(spa_vers);
3181 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3182 sa_ok = (zplver >= ZPL_VERSION_SA);
3184 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
3185 createprops, zplprops, is_ci);
3191 * "type" -> dmu_objset_type_t (int32)
3192 * (optional) "props" -> { prop -> value }
3195 * outnvl: propname -> error code (int32)
3198 zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3201 zfs_creat_t zct = { 0 };
3202 nvlist_t *nvprops = NULL;
3203 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
3205 dmu_objset_type_t type;
3206 boolean_t is_insensitive = B_FALSE;
3208 if (nvlist_lookup_int32(innvl, "type", &type32) != 0)
3209 return (SET_ERROR(EINVAL));
3211 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3215 cbfunc = zfs_create_cb;
3219 cbfunc = zvol_create_cb;
3226 if (strchr(fsname, '@') ||
3227 strchr(fsname, '%'))
3228 return (SET_ERROR(EINVAL));
3230 zct.zct_props = nvprops;
3233 return (SET_ERROR(EINVAL));
3235 if (type == DMU_OST_ZVOL) {
3236 uint64_t volsize, volblocksize;
3238 if (nvprops == NULL)
3239 return (SET_ERROR(EINVAL));
3240 if (nvlist_lookup_uint64(nvprops,
3241 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3242 return (SET_ERROR(EINVAL));
3244 if ((error = nvlist_lookup_uint64(nvprops,
3245 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3246 &volblocksize)) != 0 && error != ENOENT)
3247 return (SET_ERROR(EINVAL));
3250 volblocksize = zfs_prop_default_numeric(
3251 ZFS_PROP_VOLBLOCKSIZE);
3253 if ((error = zvol_check_volblocksize(
3254 volblocksize)) != 0 ||
3255 (error = zvol_check_volsize(volsize,
3256 volblocksize)) != 0)
3258 } else if (type == DMU_OST_ZFS) {
3262 * We have to have normalization and
3263 * case-folding flags correct when we do the
3264 * file system creation, so go figure them out
3267 VERIFY(nvlist_alloc(&zct.zct_zplprops,
3268 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3269 error = zfs_fill_zplprops(fsname, nvprops,
3270 zct.zct_zplprops, &is_insensitive);
3272 nvlist_free(zct.zct_zplprops);
3277 error = dmu_objset_create(fsname, type,
3278 is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
3279 nvlist_free(zct.zct_zplprops);
3282 * It would be nice to do this atomically.
3285 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3288 (void) dsl_destroy_head(fsname);
3291 if (error == 0 && type == DMU_OST_ZVOL)
3292 zvol_create_minors(fsname);
3299 * "origin" -> name of origin snapshot
3300 * (optional) "props" -> { prop -> value }
3303 * outnvl: propname -> error code (int32)
3306 zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3309 nvlist_t *nvprops = NULL;
3312 if (nvlist_lookup_string(innvl, "origin", &origin_name) != 0)
3313 return (SET_ERROR(EINVAL));
3314 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3316 if (strchr(fsname, '@') ||
3317 strchr(fsname, '%'))
3318 return (SET_ERROR(EINVAL));
3320 if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3321 return (SET_ERROR(EINVAL));
3322 error = dmu_objset_clone(fsname, origin_name);
3327 * It would be nice to do this atomically.
3330 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3333 (void) dsl_destroy_head(fsname);
3337 zvol_create_minors(fsname);
3344 * "snaps" -> { snapshot1, snapshot2 }
3345 * (optional) "props" -> { prop -> value (string) }
3348 * outnvl: snapshot -> error code (int32)
3351 zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3354 nvlist_t *props = NULL;
3358 (void) nvlist_lookup_nvlist(innvl, "props", &props);
3359 if ((error = zfs_check_userprops(poolname, props)) != 0)
3362 if (!nvlist_empty(props) &&
3363 zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3364 return (SET_ERROR(ENOTSUP));
3366 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3367 return (SET_ERROR(EINVAL));
3368 poollen = strlen(poolname);
3369 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3370 pair = nvlist_next_nvpair(snaps, pair)) {
3371 const char *name = nvpair_name(pair);
3372 const char *cp = strchr(name, '@');
3375 * The snap name must contain an @, and the part after it must
3376 * contain only valid characters.
3379 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3380 return (SET_ERROR(EINVAL));
3383 * The snap must be in the specified pool.
3385 if (strncmp(name, poolname, poollen) != 0 ||
3386 (name[poollen] != '/' && name[poollen] != '@'))
3387 return (SET_ERROR(EXDEV));
3389 /* This must be the only snap of this fs. */
3390 for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
3391 pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
3392 if (strncmp(name, nvpair_name(pair2), cp - name + 1)
3394 return (SET_ERROR(EXDEV));
3399 error = dsl_dataset_snapshot(snaps, props, outnvl);
3404 * innvl: "message" -> string
3408 zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
3416 * The poolname in the ioctl is not set, we get it from the TSD,
3417 * which was set at the end of the last successful ioctl that allows
3418 * logging. The secpolicy func already checked that it is set.
3419 * Only one log ioctl is allowed after each successful ioctl, so
3420 * we clear the TSD here.
3422 poolname = tsd_get(zfs_allow_log_key);
3423 (void) tsd_set(zfs_allow_log_key, NULL);
3424 error = spa_open(poolname, &spa, FTAG);
3429 if (nvlist_lookup_string(innvl, "message", &message) != 0) {
3430 spa_close(spa, FTAG);
3431 return (SET_ERROR(EINVAL));
3434 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
3435 spa_close(spa, FTAG);
3436 return (SET_ERROR(ENOTSUP));
3439 error = spa_history_log(spa, message);
3440 spa_close(spa, FTAG);
3445 * The dp_config_rwlock must not be held when calling this, because the
3446 * unmount may need to write out data.
3448 * This function is best-effort. Callers must deal gracefully if it
3449 * remains mounted (or is remounted after this call).
3451 * Returns 0 if the argument is not a snapshot, or it is not currently a
3452 * filesystem, or we were able to unmount it. Returns error code otherwise.
3455 zfs_unmount_snap(const char *snapname)
3461 if (strchr(snapname, '@') == NULL)
3464 vfsp = zfs_get_vfs(snapname);
3468 zfsvfs = vfsp->vfs_data;
3469 ASSERT(!dsl_pool_config_held(dmu_objset_pool(zfsvfs->z_os)));
3471 err = vn_vfswlock(vfsp->vfs_vnodecovered);
3474 return (SET_ERROR(err));
3477 * Always force the unmount for snapshots.
3481 (void) dounmount(vfsp, MS_FORCE, kcred);
3484 (void) dounmount(vfsp, MS_FORCE, curthread);
3491 zfs_unmount_snap_cb(const char *snapname, void *arg)
3493 return (zfs_unmount_snap(snapname));
3497 * When a clone is destroyed, its origin may also need to be destroyed,
3498 * in which case it must be unmounted. This routine will do that unmount
3502 zfs_destroy_unmount_origin(const char *fsname)
3508 error = dmu_objset_hold(fsname, FTAG, &os);
3511 ds = dmu_objset_ds(os);
3512 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
3513 char originname[MAXNAMELEN];
3514 dsl_dataset_name(ds->ds_prev, originname);
3515 dmu_objset_rele(os, FTAG);
3516 (void) zfs_unmount_snap(originname);
3518 dmu_objset_rele(os, FTAG);
3524 * "snaps" -> { snapshot1, snapshot2 }
3525 * (optional boolean) "defer"
3528 * outnvl: snapshot -> error code (int32)
3533 zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3540 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3541 return (SET_ERROR(EINVAL));
3542 defer = nvlist_exists(innvl, "defer");
3544 poollen = strlen(poolname);
3545 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3546 pair = nvlist_next_nvpair(snaps, pair)) {
3547 const char *name = nvpair_name(pair);
3550 * The snap must be in the specified pool to prevent the
3551 * invalid removal of zvol minors below.
3553 if (strncmp(name, poolname, poollen) != 0 ||
3554 (name[poollen] != '/' && name[poollen] != '@'))
3555 return (SET_ERROR(EXDEV));
3557 error = zfs_unmount_snap(name);
3560 #if defined(__FreeBSD__)
3561 zvol_remove_minors(name);
3565 return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
3569 * Create bookmarks. Bookmark names are of the form <fs>#<bmark>.
3570 * All bookmarks must be in the same pool.
3573 * bookmark1 -> snapshot1, bookmark2 -> snapshot2
3576 * outnvl: bookmark -> error code (int32)
3581 zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3583 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3584 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3588 * Verify the snapshot argument.
3590 if (nvpair_value_string(pair, &snap_name) != 0)
3591 return (SET_ERROR(EINVAL));
3594 /* Verify that the keys (bookmarks) are unique */
3595 for (nvpair_t *pair2 = nvlist_next_nvpair(innvl, pair);
3596 pair2 != NULL; pair2 = nvlist_next_nvpair(innvl, pair2)) {
3597 if (strcmp(nvpair_name(pair), nvpair_name(pair2)) == 0)
3598 return (SET_ERROR(EINVAL));
3602 return (dsl_bookmark_create(innvl, outnvl));
3607 * property 1, property 2, ...
3611 * bookmark name 1 -> { property 1, property 2, ... },
3612 * bookmark name 2 -> { property 1, property 2, ... }
3617 zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3619 return (dsl_get_bookmarks(fsname, innvl, outnvl));
3624 * bookmark name 1, bookmark name 2
3627 * outnvl: bookmark -> error code (int32)
3631 zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl,
3636 poollen = strlen(poolname);
3637 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3638 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3639 const char *name = nvpair_name(pair);
3640 const char *cp = strchr(name, '#');
3643 * The bookmark name must contain an #, and the part after it
3644 * must contain only valid characters.
3647 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3648 return (SET_ERROR(EINVAL));
3651 * The bookmark must be in the specified pool.
3653 if (strncmp(name, poolname, poollen) != 0 ||
3654 (name[poollen] != '/' && name[poollen] != '#'))
3655 return (SET_ERROR(EXDEV));
3658 error = dsl_bookmark_destroy(innvl, outnvl);
3664 * zc_name name of dataset to destroy
3665 * zc_objset_type type of objset
3666 * zc_defer_destroy mark for deferred destroy
3671 zfs_ioc_destroy(zfs_cmd_t *zc)
3675 if (zc->zc_objset_type == DMU_OST_ZFS) {
3676 err = zfs_unmount_snap(zc->zc_name);
3681 if (strchr(zc->zc_name, '@'))
3682 err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
3684 err = dsl_destroy_head(zc->zc_name);
3685 if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
3687 zvol_remove_minors(zc->zc_name);
3689 (void) zvol_remove_minor(zc->zc_name);
3695 * fsname is name of dataset to rollback (to most recent snapshot)
3697 * innvl is not used.
3699 * outnvl: "target" -> name of most recent snapshot
3704 zfs_ioc_rollback(const char *fsname, nvlist_t *args, nvlist_t *outnvl)
3709 if (getzfsvfs(fsname, &zfsvfs) == 0) {
3710 error = zfs_suspend_fs(zfsvfs);
3714 error = dsl_dataset_rollback(fsname, zfsvfs, outnvl);
3715 resume_err = zfs_resume_fs(zfsvfs, fsname);
3716 error = error ? error : resume_err;
3718 VFS_RELE(zfsvfs->z_vfs);
3720 error = dsl_dataset_rollback(fsname, NULL, outnvl);
3726 recursive_unmount(const char *fsname, void *arg)
3728 const char *snapname = arg;
3729 char fullname[MAXNAMELEN];
3731 (void) snprintf(fullname, sizeof (fullname), "%s@%s", fsname, snapname);
3732 return (zfs_unmount_snap(fullname));
3737 * zc_name old name of dataset
3738 * zc_value new name of dataset
3739 * zc_cookie recursive flag (only valid for snapshots)
3744 zfs_ioc_rename(zfs_cmd_t *zc)
3746 boolean_t recursive = zc->zc_cookie & 1;
3748 boolean_t allow_mounted = zc->zc_cookie & 2;
3752 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3753 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3754 strchr(zc->zc_value, '%'))
3755 return (SET_ERROR(EINVAL));
3757 at = strchr(zc->zc_name, '@');
3759 /* snaps must be in same fs */
3762 if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
3763 return (SET_ERROR(EXDEV));
3766 if (zc->zc_objset_type == DMU_OST_ZFS) {
3768 if (zc->zc_objset_type == DMU_OST_ZFS && allow_mounted) {
3770 error = dmu_objset_find(zc->zc_name,
3771 recursive_unmount, at + 1,
3772 recursive ? DS_FIND_CHILDREN : 0);
3778 error = dsl_dataset_rename_snapshot(zc->zc_name,
3779 at + 1, strchr(zc->zc_value, '@') + 1, recursive);
3785 if (zc->zc_objset_type == DMU_OST_ZVOL)
3786 (void) zvol_remove_minor(zc->zc_name);
3788 return (dsl_dir_rename(zc->zc_name, zc->zc_value));
3793 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
3795 const char *propname = nvpair_name(pair);
3796 boolean_t issnap = (strchr(dsname, '@') != NULL);
3797 zfs_prop_t prop = zfs_name_to_prop(propname);
3801 if (prop == ZPROP_INVAL) {
3802 if (zfs_prop_user(propname)) {
3803 if (err = zfs_secpolicy_write_perms(dsname,
3804 ZFS_DELEG_PERM_USERPROP, cr))
3809 if (!issnap && zfs_prop_userquota(propname)) {
3810 const char *perm = NULL;
3811 const char *uq_prefix =
3812 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
3813 const char *gq_prefix =
3814 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
3816 if (strncmp(propname, uq_prefix,
3817 strlen(uq_prefix)) == 0) {
3818 perm = ZFS_DELEG_PERM_USERQUOTA;
3819 } else if (strncmp(propname, gq_prefix,
3820 strlen(gq_prefix)) == 0) {
3821 perm = ZFS_DELEG_PERM_GROUPQUOTA;
3823 /* USERUSED and GROUPUSED are read-only */
3824 return (SET_ERROR(EINVAL));
3827 if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
3832 return (SET_ERROR(EINVAL));
3836 return (SET_ERROR(EINVAL));
3838 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
3840 * dsl_prop_get_all_impl() returns properties in this
3844 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
3845 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3850 * Check that this value is valid for this pool version
3853 case ZFS_PROP_COMPRESSION:
3855 * If the user specified gzip compression, make sure
3856 * the SPA supports it. We ignore any errors here since
3857 * we'll catch them later.
3859 if (nvpair_value_uint64(pair, &intval) == 0) {
3860 if (intval >= ZIO_COMPRESS_GZIP_1 &&
3861 intval <= ZIO_COMPRESS_GZIP_9 &&
3862 zfs_earlier_version(dsname,
3863 SPA_VERSION_GZIP_COMPRESSION)) {
3864 return (SET_ERROR(ENOTSUP));
3867 if (intval == ZIO_COMPRESS_ZLE &&
3868 zfs_earlier_version(dsname,
3869 SPA_VERSION_ZLE_COMPRESSION))
3870 return (SET_ERROR(ENOTSUP));
3872 if (intval == ZIO_COMPRESS_LZ4) {
3875 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3878 if (!spa_feature_is_enabled(spa,
3879 SPA_FEATURE_LZ4_COMPRESS)) {
3880 spa_close(spa, FTAG);
3881 return (SET_ERROR(ENOTSUP));
3883 spa_close(spa, FTAG);
3887 * If this is a bootable dataset then
3888 * verify that the compression algorithm
3889 * is supported for booting. We must return
3890 * something other than ENOTSUP since it
3891 * implies a downrev pool version.
3893 if (zfs_is_bootfs(dsname) &&
3894 !BOOTFS_COMPRESS_VALID(intval)) {
3895 return (SET_ERROR(ERANGE));
3900 case ZFS_PROP_COPIES:
3901 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
3902 return (SET_ERROR(ENOTSUP));
3905 case ZFS_PROP_DEDUP:
3906 if (zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
3907 return (SET_ERROR(ENOTSUP));
3910 case ZFS_PROP_RECORDSIZE:
3911 /* Record sizes above 128k need the feature to be enabled */
3912 if (nvpair_value_uint64(pair, &intval) == 0 &&
3913 intval > SPA_OLD_MAXBLOCKSIZE) {
3917 * If this is a bootable dataset then
3918 * the we don't allow large (>128K) blocks,
3919 * because GRUB doesn't support them.
3921 if (zfs_is_bootfs(dsname) &&
3922 intval > SPA_OLD_MAXBLOCKSIZE) {
3923 return (SET_ERROR(ERANGE));
3927 * We don't allow setting the property above 1MB,
3928 * unless the tunable has been changed.
3930 if (intval > zfs_max_recordsize ||
3931 intval > SPA_MAXBLOCKSIZE)
3932 return (SET_ERROR(ERANGE));
3934 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3937 if (!spa_feature_is_enabled(spa,
3938 SPA_FEATURE_LARGE_BLOCKS)) {
3939 spa_close(spa, FTAG);
3940 return (SET_ERROR(ENOTSUP));
3942 spa_close(spa, FTAG);
3946 case ZFS_PROP_SHARESMB:
3947 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
3948 return (SET_ERROR(ENOTSUP));
3951 case ZFS_PROP_ACLINHERIT:
3952 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3953 nvpair_value_uint64(pair, &intval) == 0) {
3954 if (intval == ZFS_ACL_PASSTHROUGH_X &&
3955 zfs_earlier_version(dsname,
3956 SPA_VERSION_PASSTHROUGH_X))
3957 return (SET_ERROR(ENOTSUP));
3962 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
3966 * Checks for a race condition to make sure we don't increment a feature flag
3970 zfs_prop_activate_feature_check(void *arg, dmu_tx_t *tx)
3972 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
3973 spa_feature_t *featurep = arg;
3975 if (!spa_feature_is_active(spa, *featurep))
3978 return (SET_ERROR(EBUSY));
3982 * The callback invoked on feature activation in the sync task caused by
3983 * zfs_prop_activate_feature.
3986 zfs_prop_activate_feature_sync(void *arg, dmu_tx_t *tx)
3988 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
3989 spa_feature_t *featurep = arg;
3991 spa_feature_incr(spa, *featurep, tx);
3995 * Activates a feature on a pool in response to a property setting. This
3996 * creates a new sync task which modifies the pool to reflect the feature
4000 zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature)
4004 /* EBUSY here indicates that the feature is already active */
4005 err = dsl_sync_task(spa_name(spa),
4006 zfs_prop_activate_feature_check, zfs_prop_activate_feature_sync,
4007 &feature, 2, ZFS_SPACE_CHECK_RESERVED);
4009 if (err != 0 && err != EBUSY)
4016 * Removes properties from the given props list that fail permission checks
4017 * needed to clear them and to restore them in case of a receive error. For each
4018 * property, make sure we have both set and inherit permissions.
4020 * Returns the first error encountered if any permission checks fail. If the
4021 * caller provides a non-NULL errlist, it also gives the complete list of names
4022 * of all the properties that failed a permission check along with the
4023 * corresponding error numbers. The caller is responsible for freeing the
4026 * If every property checks out successfully, zero is returned and the list
4027 * pointed at by errlist is NULL.
4030 zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
4033 nvpair_t *pair, *next_pair;
4040 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4042 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
4043 (void) strcpy(zc->zc_name, dataset);
4044 pair = nvlist_next_nvpair(props, NULL);
4045 while (pair != NULL) {
4046 next_pair = nvlist_next_nvpair(props, pair);
4048 (void) strcpy(zc->zc_value, nvpair_name(pair));
4049 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
4050 (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
4051 VERIFY(nvlist_remove_nvpair(props, pair) == 0);
4052 VERIFY(nvlist_add_int32(errors,
4053 zc->zc_value, err) == 0);
4057 kmem_free(zc, sizeof (zfs_cmd_t));
4059 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
4060 nvlist_free(errors);
4063 VERIFY(nvpair_value_int32(pair, &rv) == 0);
4066 if (errlist == NULL)
4067 nvlist_free(errors);
4075 propval_equals(nvpair_t *p1, nvpair_t *p2)
4077 if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
4078 /* dsl_prop_get_all_impl() format */
4080 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
4081 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4085 if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
4087 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
4088 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4092 if (nvpair_type(p1) != nvpair_type(p2))
4095 if (nvpair_type(p1) == DATA_TYPE_STRING) {
4096 char *valstr1, *valstr2;
4098 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
4099 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
4100 return (strcmp(valstr1, valstr2) == 0);
4102 uint64_t intval1, intval2;
4104 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
4105 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
4106 return (intval1 == intval2);
4111 * Remove properties from props if they are not going to change (as determined
4112 * by comparison with origprops). Remove them from origprops as well, since we
4113 * do not need to clear or restore properties that won't change.
4116 props_reduce(nvlist_t *props, nvlist_t *origprops)
4118 nvpair_t *pair, *next_pair;
4120 if (origprops == NULL)
4121 return; /* all props need to be received */
4123 pair = nvlist_next_nvpair(props, NULL);
4124 while (pair != NULL) {
4125 const char *propname = nvpair_name(pair);
4128 next_pair = nvlist_next_nvpair(props, pair);
4130 if ((nvlist_lookup_nvpair(origprops, propname,
4131 &match) != 0) || !propval_equals(pair, match))
4132 goto next; /* need to set received value */
4134 /* don't clear the existing received value */
4135 (void) nvlist_remove_nvpair(origprops, match);
4136 /* don't bother receiving the property */
4137 (void) nvlist_remove_nvpair(props, pair);
4144 static boolean_t zfs_ioc_recv_inject_err;
4149 * zc_name name of containing filesystem
4150 * zc_nvlist_src{_size} nvlist of properties to apply
4151 * zc_value name of snapshot to create
4152 * zc_string name of clone origin (if DRR_FLAG_CLONE)
4153 * zc_cookie file descriptor to recv from
4154 * zc_begin_record the BEGIN record of the stream (not byteswapped)
4155 * zc_guid force flag
4156 * zc_cleanup_fd cleanup-on-exit file descriptor
4157 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
4160 * zc_cookie number of bytes read
4161 * zc_nvlist_dst{_size} error for each unapplied received property
4162 * zc_obj zprop_errflags_t
4163 * zc_action_handle handle for this guid/ds mapping
4166 zfs_ioc_recv(zfs_cmd_t *zc)
4169 dmu_recv_cookie_t drc;
4170 boolean_t force = (boolean_t)zc->zc_guid;
4173 int props_error = 0;
4176 nvlist_t *props = NULL; /* sent properties */
4177 nvlist_t *origprops = NULL; /* existing properties */
4178 char *origin = NULL;
4180 char tofs[ZFS_MAXNAMELEN];
4181 cap_rights_t rights;
4182 boolean_t first_recvd_props = B_FALSE;
4184 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4185 strchr(zc->zc_value, '@') == NULL ||
4186 strchr(zc->zc_value, '%'))
4187 return (SET_ERROR(EINVAL));
4189 (void) strcpy(tofs, zc->zc_value);
4190 tosnap = strchr(tofs, '@');
4193 if (zc->zc_nvlist_src != 0 &&
4194 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
4195 zc->zc_iflags, &props)) != 0)
4202 fget_read(curthread, fd, cap_rights_init(&rights, CAP_PREAD), &fp);
4206 return (SET_ERROR(EBADF));
4209 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4211 if (zc->zc_string[0])
4212 origin = zc->zc_string;
4214 error = dmu_recv_begin(tofs, tosnap,
4215 &zc->zc_begin_record, force, origin, &drc);
4220 * Set properties before we receive the stream so that they are applied
4221 * to the new data. Note that we must call dmu_recv_stream() if
4222 * dmu_recv_begin() succeeds.
4224 if (props != NULL && !drc.drc_newfs) {
4225 if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
4226 SPA_VERSION_RECVD_PROPS &&
4227 !dsl_prop_get_hasrecvd(tofs))
4228 first_recvd_props = B_TRUE;
4231 * If new received properties are supplied, they are to
4232 * completely replace the existing received properties, so stash
4233 * away the existing ones.
4235 if (dsl_prop_get_received(tofs, &origprops) == 0) {
4236 nvlist_t *errlist = NULL;
4238 * Don't bother writing a property if its value won't
4239 * change (and avoid the unnecessary security checks).
4241 * The first receive after SPA_VERSION_RECVD_PROPS is a
4242 * special case where we blow away all local properties
4245 if (!first_recvd_props)
4246 props_reduce(props, origprops);
4247 if (zfs_check_clearable(tofs, origprops, &errlist) != 0)
4248 (void) nvlist_merge(errors, errlist, 0);
4249 nvlist_free(errlist);
4251 if (clear_received_props(tofs, origprops,
4252 first_recvd_props ? NULL : props) != 0)
4253 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4255 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4259 if (props != NULL) {
4260 props_error = dsl_prop_set_hasrecvd(tofs);
4262 if (props_error == 0) {
4263 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4268 if (zc->zc_nvlist_dst_size != 0 &&
4269 (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
4270 put_nvlist(zc, errors) != 0)) {
4272 * Caller made zc->zc_nvlist_dst less than the minimum expected
4273 * size or supplied an invalid address.
4275 props_error = SET_ERROR(EINVAL);
4279 error = dmu_recv_stream(&drc, fp, &off, zc->zc_cleanup_fd,
4280 &zc->zc_action_handle);
4283 zfsvfs_t *zfsvfs = NULL;
4285 if (getzfsvfs(tofs, &zfsvfs) == 0) {
4289 error = zfs_suspend_fs(zfsvfs);
4291 * If the suspend fails, then the recv_end will
4292 * likely also fail, and clean up after itself.
4294 end_err = dmu_recv_end(&drc, zfsvfs);
4296 error = zfs_resume_fs(zfsvfs, tofs);
4297 error = error ? error : end_err;
4298 VFS_RELE(zfsvfs->z_vfs);
4300 error = dmu_recv_end(&drc, NULL);
4304 zc->zc_cookie = off - fp->f_offset;
4305 if (off >= 0 && off <= MAXOFFSET_T)
4309 if (zfs_ioc_recv_inject_err) {
4310 zfs_ioc_recv_inject_err = B_FALSE;
4317 zvol_create_minors(tofs);
4321 * On error, restore the original props.
4323 if (error != 0 && props != NULL && !drc.drc_newfs) {
4324 if (clear_received_props(tofs, props, NULL) != 0) {
4326 * We failed to clear the received properties.
4327 * Since we may have left a $recvd value on the
4328 * system, we can't clear the $hasrecvd flag.
4330 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4331 } else if (first_recvd_props) {
4332 dsl_prop_unset_hasrecvd(tofs);
4335 if (origprops == NULL && !drc.drc_newfs) {
4336 /* We failed to stash the original properties. */
4337 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4341 * dsl_props_set() will not convert RECEIVED to LOCAL on or
4342 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
4343 * explictly if we're restoring local properties cleared in the
4344 * first new-style receive.
4346 if (origprops != NULL &&
4347 zfs_set_prop_nvlist(tofs, (first_recvd_props ?
4348 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
4349 origprops, NULL) != 0) {
4351 * We stashed the original properties but failed to
4354 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4359 nvlist_free(origprops);
4360 nvlist_free(errors);
4364 error = props_error;
4371 * zc_name name of snapshot to send
4372 * zc_cookie file descriptor to send stream to
4373 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
4374 * zc_sendobj objsetid of snapshot to send
4375 * zc_fromobj objsetid of incremental fromsnap (may be zero)
4376 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
4377 * output size in zc_objset_type.
4378 * zc_flags lzc_send_flags
4381 * zc_objset_type estimated size, if zc_guid is set
4384 zfs_ioc_send(zfs_cmd_t *zc)
4388 boolean_t estimate = (zc->zc_guid != 0);
4389 boolean_t embedok = (zc->zc_flags & 0x1);
4390 boolean_t large_block_ok = (zc->zc_flags & 0x2);
4392 if (zc->zc_obj != 0) {
4394 dsl_dataset_t *tosnap;
4396 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4400 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
4402 dsl_pool_rele(dp, FTAG);
4406 if (dsl_dir_is_clone(tosnap->ds_dir))
4408 dsl_dir_phys(tosnap->ds_dir)->dd_origin_obj;
4409 dsl_dataset_rele(tosnap, FTAG);
4410 dsl_pool_rele(dp, FTAG);
4415 dsl_dataset_t *tosnap;
4416 dsl_dataset_t *fromsnap = NULL;
4418 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4422 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
4424 dsl_pool_rele(dp, FTAG);
4428 if (zc->zc_fromobj != 0) {
4429 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
4432 dsl_dataset_rele(tosnap, FTAG);
4433 dsl_pool_rele(dp, FTAG);
4438 error = dmu_send_estimate(tosnap, fromsnap,
4439 &zc->zc_objset_type);
4441 if (fromsnap != NULL)
4442 dsl_dataset_rele(fromsnap, FTAG);
4443 dsl_dataset_rele(tosnap, FTAG);
4444 dsl_pool_rele(dp, FTAG);
4447 cap_rights_t rights;
4450 fp = getf(zc->zc_cookie);
4452 fget_write(curthread, zc->zc_cookie,
4453 cap_rights_init(&rights, CAP_WRITE), &fp);
4456 return (SET_ERROR(EBADF));
4459 error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
4460 zc->zc_fromobj, embedok, large_block_ok,
4462 zc->zc_cookie, fp->f_vnode, &off);
4464 zc->zc_cookie, fp, &off);
4467 if (off >= 0 && off <= MAXOFFSET_T)
4469 releasef(zc->zc_cookie);
4476 * zc_name name of snapshot on which to report progress
4477 * zc_cookie file descriptor of send stream
4480 * zc_cookie number of bytes written in send stream thus far
4483 zfs_ioc_send_progress(zfs_cmd_t *zc)
4487 dmu_sendarg_t *dsp = NULL;
4490 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4494 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
4496 dsl_pool_rele(dp, FTAG);
4500 mutex_enter(&ds->ds_sendstream_lock);
4503 * Iterate over all the send streams currently active on this dataset.
4504 * If there's one which matches the specified file descriptor _and_ the
4505 * stream was started by the current process, return the progress of
4508 for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
4509 dsp = list_next(&ds->ds_sendstreams, dsp)) {
4510 if (dsp->dsa_outfd == zc->zc_cookie &&
4511 dsp->dsa_proc == curproc)
4516 zc->zc_cookie = *(dsp->dsa_off);
4518 error = SET_ERROR(ENOENT);
4520 mutex_exit(&ds->ds_sendstream_lock);
4521 dsl_dataset_rele(ds, FTAG);
4522 dsl_pool_rele(dp, FTAG);
4527 zfs_ioc_inject_fault(zfs_cmd_t *zc)
4531 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
4532 &zc->zc_inject_record);
4535 zc->zc_guid = (uint64_t)id;
4541 zfs_ioc_clear_fault(zfs_cmd_t *zc)
4543 return (zio_clear_fault((int)zc->zc_guid));
4547 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
4549 int id = (int)zc->zc_guid;
4552 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
4553 &zc->zc_inject_record);
4561 zfs_ioc_error_log(zfs_cmd_t *zc)
4565 size_t count = (size_t)zc->zc_nvlist_dst_size;
4567 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
4570 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
4573 zc->zc_nvlist_dst_size = count;
4575 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
4577 spa_close(spa, FTAG);
4583 zfs_ioc_clear(zfs_cmd_t *zc)
4590 * On zpool clear we also fix up missing slogs
4592 mutex_enter(&spa_namespace_lock);
4593 spa = spa_lookup(zc->zc_name);
4595 mutex_exit(&spa_namespace_lock);
4596 return (SET_ERROR(EIO));
4598 if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
4599 /* we need to let spa_open/spa_load clear the chains */
4600 spa_set_log_state(spa, SPA_LOG_CLEAR);
4602 spa->spa_last_open_failed = 0;
4603 mutex_exit(&spa_namespace_lock);
4605 if (zc->zc_cookie & ZPOOL_NO_REWIND) {
4606 error = spa_open(zc->zc_name, &spa, FTAG);
4609 nvlist_t *config = NULL;
4611 if (zc->zc_nvlist_src == 0)
4612 return (SET_ERROR(EINVAL));
4614 if ((error = get_nvlist(zc->zc_nvlist_src,
4615 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
4616 error = spa_open_rewind(zc->zc_name, &spa, FTAG,
4618 if (config != NULL) {
4621 if ((err = put_nvlist(zc, config)) != 0)
4623 nvlist_free(config);
4625 nvlist_free(policy);
4632 spa_vdev_state_enter(spa, SCL_NONE);
4634 if (zc->zc_guid == 0) {
4637 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
4639 (void) spa_vdev_state_exit(spa, NULL, ENODEV);
4640 spa_close(spa, FTAG);
4641 return (SET_ERROR(ENODEV));
4645 vdev_clear(spa, vd);
4647 (void) spa_vdev_state_exit(spa, NULL, 0);
4650 * Resume any suspended I/Os.
4652 if (zio_resume(spa) != 0)
4653 error = SET_ERROR(EIO);
4655 spa_close(spa, FTAG);
4661 zfs_ioc_pool_reopen(zfs_cmd_t *zc)
4666 error = spa_open(zc->zc_name, &spa, FTAG);
4670 spa_vdev_state_enter(spa, SCL_NONE);
4673 * If a resilver is already in progress then set the
4674 * spa_scrub_reopen flag to B_TRUE so that we don't restart
4675 * the scan as a side effect of the reopen. Otherwise, let
4676 * vdev_open() decided if a resilver is required.
4678 spa->spa_scrub_reopen = dsl_scan_resilvering(spa->spa_dsl_pool);
4679 vdev_reopen(spa->spa_root_vdev);
4680 spa->spa_scrub_reopen = B_FALSE;
4682 (void) spa_vdev_state_exit(spa, NULL, 0);
4683 spa_close(spa, FTAG);
4688 * zc_name name of filesystem
4689 * zc_value name of origin snapshot
4692 * zc_string name of conflicting snapshot, if there is one
4695 zfs_ioc_promote(zfs_cmd_t *zc)
4700 * We don't need to unmount *all* the origin fs's snapshots, but
4703 cp = strchr(zc->zc_value, '@');
4706 (void) dmu_objset_find(zc->zc_value,
4707 zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
4708 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
4712 * Retrieve a single {user|group}{used|quota}@... property.
4715 * zc_name name of filesystem
4716 * zc_objset_type zfs_userquota_prop_t
4717 * zc_value domain name (eg. "S-1-234-567-89")
4718 * zc_guid RID/UID/GID
4721 * zc_cookie property value
4724 zfs_ioc_userspace_one(zfs_cmd_t *zc)
4729 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
4730 return (SET_ERROR(EINVAL));
4732 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4736 error = zfs_userspace_one(zfsvfs,
4737 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
4738 zfsvfs_rele(zfsvfs, FTAG);
4745 * zc_name name of filesystem
4746 * zc_cookie zap cursor
4747 * zc_objset_type zfs_userquota_prop_t
4748 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
4751 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
4752 * zc_cookie zap cursor
4755 zfs_ioc_userspace_many(zfs_cmd_t *zc)
4758 int bufsize = zc->zc_nvlist_dst_size;
4761 return (SET_ERROR(ENOMEM));
4763 int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4767 void *buf = kmem_alloc(bufsize, KM_SLEEP);
4769 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
4770 buf, &zc->zc_nvlist_dst_size);
4773 error = ddi_copyout(buf,
4774 (void *)(uintptr_t)zc->zc_nvlist_dst,
4775 zc->zc_nvlist_dst_size, zc->zc_iflags);
4777 kmem_free(buf, bufsize);
4778 zfsvfs_rele(zfsvfs, FTAG);
4785 * zc_name name of filesystem
4791 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
4797 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
4798 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
4800 * If userused is not enabled, it may be because the
4801 * objset needs to be closed & reopened (to grow the
4802 * objset_phys_t). Suspend/resume the fs will do that.
4804 error = zfs_suspend_fs(zfsvfs);
4806 dmu_objset_refresh_ownership(zfsvfs->z_os,
4808 error = zfs_resume_fs(zfsvfs, zc->zc_name);
4812 error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
4813 VFS_RELE(zfsvfs->z_vfs);
4815 /* XXX kind of reading contents without owning */
4816 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4820 error = dmu_objset_userspace_upgrade(os);
4821 dmu_objset_rele(os, FTAG);
4829 * We don't want to have a hard dependency
4830 * against some special symbols in sharefs
4831 * nfs, and smbsrv. Determine them if needed when
4832 * the first file system is shared.
4833 * Neither sharefs, nfs or smbsrv are unloadable modules.
4835 int (*znfsexport_fs)(void *arg);
4836 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
4837 int (*zsmbexport_fs)(void *arg, boolean_t add_share);
4839 int zfs_nfsshare_inited;
4840 int zfs_smbshare_inited;
4842 ddi_modhandle_t nfs_mod;
4843 ddi_modhandle_t sharefs_mod;
4844 ddi_modhandle_t smbsrv_mod;
4846 kmutex_t zfs_share_lock;
4854 ASSERT(MUTEX_HELD(&zfs_share_lock));
4855 /* Both NFS and SMB shares also require sharetab support. */
4856 if (sharefs_mod == NULL && ((sharefs_mod =
4857 ddi_modopen("fs/sharefs",
4858 KRTLD_MODE_FIRST, &error)) == NULL)) {
4859 return (SET_ERROR(ENOSYS));
4861 if (zshare_fs == NULL && ((zshare_fs =
4862 (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
4863 ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
4864 return (SET_ERROR(ENOSYS));
4871 zfs_ioc_share(zfs_cmd_t *zc)
4877 switch (zc->zc_share.z_sharetype) {
4879 case ZFS_UNSHARE_NFS:
4880 if (zfs_nfsshare_inited == 0) {
4881 mutex_enter(&zfs_share_lock);
4882 if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
4883 KRTLD_MODE_FIRST, &error)) == NULL)) {
4884 mutex_exit(&zfs_share_lock);
4885 return (SET_ERROR(ENOSYS));
4887 if (znfsexport_fs == NULL &&
4888 ((znfsexport_fs = (int (*)(void *))
4890 "nfs_export", &error)) == NULL)) {
4891 mutex_exit(&zfs_share_lock);
4892 return (SET_ERROR(ENOSYS));
4894 error = zfs_init_sharefs();
4896 mutex_exit(&zfs_share_lock);
4897 return (SET_ERROR(ENOSYS));
4899 zfs_nfsshare_inited = 1;
4900 mutex_exit(&zfs_share_lock);
4904 case ZFS_UNSHARE_SMB:
4905 if (zfs_smbshare_inited == 0) {
4906 mutex_enter(&zfs_share_lock);
4907 if (smbsrv_mod == NULL && ((smbsrv_mod =
4908 ddi_modopen("drv/smbsrv",
4909 KRTLD_MODE_FIRST, &error)) == NULL)) {
4910 mutex_exit(&zfs_share_lock);
4911 return (SET_ERROR(ENOSYS));
4913 if (zsmbexport_fs == NULL && ((zsmbexport_fs =
4914 (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
4915 "smb_server_share", &error)) == NULL)) {
4916 mutex_exit(&zfs_share_lock);
4917 return (SET_ERROR(ENOSYS));
4919 error = zfs_init_sharefs();
4921 mutex_exit(&zfs_share_lock);
4922 return (SET_ERROR(ENOSYS));
4924 zfs_smbshare_inited = 1;
4925 mutex_exit(&zfs_share_lock);
4929 return (SET_ERROR(EINVAL));
4932 switch (zc->zc_share.z_sharetype) {
4934 case ZFS_UNSHARE_NFS:
4936 znfsexport_fs((void *)
4937 (uintptr_t)zc->zc_share.z_exportdata))
4941 case ZFS_UNSHARE_SMB:
4942 if (error = zsmbexport_fs((void *)
4943 (uintptr_t)zc->zc_share.z_exportdata,
4944 zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
4951 opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
4952 zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
4953 SHAREFS_ADD : SHAREFS_REMOVE;
4956 * Add or remove share from sharetab
4958 error = zshare_fs(opcode,
4959 (void *)(uintptr_t)zc->zc_share.z_sharedata,
4960 zc->zc_share.z_sharemax);
4969 ace_t full_access[] = {
4970 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
4975 * zc_name name of containing filesystem
4976 * zc_obj object # beyond which we want next in-use object #
4979 * zc_obj next in-use object #
4982 zfs_ioc_next_obj(zfs_cmd_t *zc)
4984 objset_t *os = NULL;
4987 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4991 error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
4992 dsl_dataset_phys(os->os_dsl_dataset)->ds_prev_snap_txg);
4994 dmu_objset_rele(os, FTAG);
5000 * zc_name name of filesystem
5001 * zc_value prefix name for snapshot
5002 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
5005 * zc_value short name of new snapshot
5008 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
5015 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
5019 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
5020 (u_longlong_t)ddi_get_lbolt64());
5021 hold_name = kmem_asprintf("%%%s", zc->zc_value);
5023 error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
5026 (void) strcpy(zc->zc_value, snap_name);
5029 zfs_onexit_fd_rele(zc->zc_cleanup_fd);
5035 * zc_name name of "to" snapshot
5036 * zc_value name of "from" snapshot
5037 * zc_cookie file descriptor to write diff data on
5040 * dmu_diff_record_t's to the file descriptor
5043 zfs_ioc_diff(zfs_cmd_t *zc)
5046 cap_rights_t rights;
5051 fp = getf(zc->zc_cookie);
5053 fget_write(curthread, zc->zc_cookie,
5054 cap_rights_init(&rights, CAP_WRITE), &fp);
5057 return (SET_ERROR(EBADF));
5062 error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off);
5064 error = dmu_diff(zc->zc_name, zc->zc_value, fp, &off);
5067 if (off >= 0 && off <= MAXOFFSET_T)
5069 releasef(zc->zc_cookie);
5076 * Remove all ACL files in shares dir
5079 zfs_smb_acl_purge(znode_t *dzp)
5082 zap_attribute_t zap;
5083 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
5086 for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
5087 (error = zap_cursor_retrieve(&zc, &zap)) == 0;
5088 zap_cursor_advance(&zc)) {
5089 if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
5093 zap_cursor_fini(&zc);
5099 zfs_ioc_smb_acl(zfs_cmd_t *zc)
5104 vnode_t *resourcevp = NULL;
5113 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
5114 NO_FOLLOW, NULL, &vp)) != 0)
5117 /* Now make sure mntpnt and dataset are ZFS */
5119 if (strcmp(vp->v_vfsp->mnt_stat.f_fstypename, "zfs") != 0 ||
5120 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
5121 zc->zc_name) != 0)) {
5123 return (SET_ERROR(EINVAL));
5127 zfsvfs = dzp->z_zfsvfs;
5131 * Create share dir if its missing.
5133 mutex_enter(&zfsvfs->z_lock);
5134 if (zfsvfs->z_shares_dir == 0) {
5137 tx = dmu_tx_create(zfsvfs->z_os);
5138 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
5140 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
5141 error = dmu_tx_assign(tx, TXG_WAIT);
5145 error = zfs_create_share_dir(zfsvfs, tx);
5149 mutex_exit(&zfsvfs->z_lock);
5155 mutex_exit(&zfsvfs->z_lock);
5157 ASSERT(zfsvfs->z_shares_dir);
5158 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
5164 switch (zc->zc_cookie) {
5165 case ZFS_SMB_ACL_ADD:
5166 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
5167 vattr.va_type = VREG;
5168 vattr.va_mode = S_IFREG|0777;
5172 vsec.vsa_mask = VSA_ACE;
5173 vsec.vsa_aclentp = &full_access;
5174 vsec.vsa_aclentsz = sizeof (full_access);
5175 vsec.vsa_aclcnt = 1;
5177 error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
5178 &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
5180 VN_RELE(resourcevp);
5183 case ZFS_SMB_ACL_REMOVE:
5184 error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
5188 case ZFS_SMB_ACL_RENAME:
5189 if ((error = get_nvlist(zc->zc_nvlist_src,
5190 zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
5192 VN_RELE(ZTOV(sharedir));
5196 if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
5197 nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
5200 VN_RELE(ZTOV(sharedir));
5202 nvlist_free(nvlist);
5205 error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
5207 nvlist_free(nvlist);
5210 case ZFS_SMB_ACL_PURGE:
5211 error = zfs_smb_acl_purge(sharedir);
5215 error = SET_ERROR(EINVAL);
5220 VN_RELE(ZTOV(sharedir));
5226 return (EOPNOTSUPP);
5232 * "holds" -> { snapname -> holdname (string), ... }
5233 * (optional) "cleanup_fd" -> fd (int32)
5237 * snapname -> error value (int32)
5243 zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
5247 int cleanup_fd = -1;
5251 error = nvlist_lookup_nvlist(args, "holds", &holds);
5253 return (SET_ERROR(EINVAL));
5255 /* make sure the user didn't pass us any invalid (empty) tags */
5256 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
5257 pair = nvlist_next_nvpair(holds, pair)) {
5260 error = nvpair_value_string(pair, &htag);
5262 return (SET_ERROR(error));
5264 if (strlen(htag) == 0)
5265 return (SET_ERROR(EINVAL));
5268 if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
5269 error = zfs_onexit_fd_hold(cleanup_fd, &minor);
5274 error = dsl_dataset_user_hold(holds, minor, errlist);
5276 zfs_onexit_fd_rele(cleanup_fd);
5281 * innvl is not used.
5284 * holdname -> time added (uint64 seconds since epoch)
5290 zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
5292 return (dsl_dataset_get_holds(snapname, outnvl));
5297 * snapname -> { holdname, ... }
5302 * snapname -> error value (int32)
5308 zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
5310 return (dsl_dataset_user_release(holds, errlist));
5315 * zc_name name of new filesystem or snapshot
5316 * zc_value full name of old snapshot
5319 * zc_cookie space in bytes
5320 * zc_objset_type compressed space in bytes
5321 * zc_perm_action uncompressed space in bytes
5324 zfs_ioc_space_written(zfs_cmd_t *zc)
5328 dsl_dataset_t *new, *old;
5330 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5333 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
5335 dsl_pool_rele(dp, FTAG);
5338 error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
5340 dsl_dataset_rele(new, FTAG);
5341 dsl_pool_rele(dp, FTAG);
5345 error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
5346 &zc->zc_objset_type, &zc->zc_perm_action);
5347 dsl_dataset_rele(old, FTAG);
5348 dsl_dataset_rele(new, FTAG);
5349 dsl_pool_rele(dp, FTAG);
5355 * "firstsnap" -> snapshot name
5359 * "used" -> space in bytes
5360 * "compressed" -> compressed space in bytes
5361 * "uncompressed" -> uncompressed space in bytes
5365 zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
5369 dsl_dataset_t *new, *old;
5371 uint64_t used, comp, uncomp;
5373 if (nvlist_lookup_string(innvl, "firstsnap", &firstsnap) != 0)
5374 return (SET_ERROR(EINVAL));
5376 error = dsl_pool_hold(lastsnap, FTAG, &dp);
5380 error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
5381 if (error == 0 && !new->ds_is_snapshot) {
5382 dsl_dataset_rele(new, FTAG);
5383 error = SET_ERROR(EINVAL);
5386 dsl_pool_rele(dp, FTAG);
5389 error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
5390 if (error == 0 && !old->ds_is_snapshot) {
5391 dsl_dataset_rele(old, FTAG);
5392 error = SET_ERROR(EINVAL);
5395 dsl_dataset_rele(new, FTAG);
5396 dsl_pool_rele(dp, FTAG);
5400 error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
5401 dsl_dataset_rele(old, FTAG);
5402 dsl_dataset_rele(new, FTAG);
5403 dsl_pool_rele(dp, FTAG);
5404 fnvlist_add_uint64(outnvl, "used", used);
5405 fnvlist_add_uint64(outnvl, "compressed", comp);
5406 fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
5411 zfs_ioc_jail(zfs_cmd_t *zc)
5414 return (zone_dataset_attach(curthread->td_ucred, zc->zc_name,
5415 (int)zc->zc_jailid));
5419 zfs_ioc_unjail(zfs_cmd_t *zc)
5422 return (zone_dataset_detach(curthread->td_ucred, zc->zc_name,
5423 (int)zc->zc_jailid));
5428 * "fd" -> file descriptor to write stream to (int32)
5429 * (optional) "fromsnap" -> full snap name to send an incremental from
5430 * (optional) "largeblockok" -> (value ignored)
5431 * indicates that blocks > 128KB are permitted
5432 * (optional) "embedok" -> (value ignored)
5433 * presence indicates DRR_WRITE_EMBEDDED records are permitted
5440 zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5442 cap_rights_t rights;
5446 char *fromname = NULL;
5448 boolean_t largeblockok;
5451 error = nvlist_lookup_int32(innvl, "fd", &fd);
5453 return (SET_ERROR(EINVAL));
5455 (void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
5457 largeblockok = nvlist_exists(innvl, "largeblockok");
5458 embedok = nvlist_exists(innvl, "embedok");
5461 file_t *fp = getf(fd);
5463 fget_write(curthread, fd, cap_rights_init(&rights, CAP_WRITE), &fp);
5466 return (SET_ERROR(EBADF));
5469 error = dmu_send(snapname, fromname, embedok, largeblockok,
5471 fd, fp->f_vnode, &off);
5477 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5488 * Determine approximately how large a zfs send stream will be -- the number
5489 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
5492 * (optional) "from" -> full snap or bookmark name to send an incremental
5497 * "space" -> bytes of space (uint64)
5501 zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5504 dsl_dataset_t *tosnap;
5509 error = dsl_pool_hold(snapname, FTAG, &dp);
5513 error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
5515 dsl_pool_rele(dp, FTAG);
5519 error = nvlist_lookup_string(innvl, "from", &fromname);
5521 if (strchr(fromname, '@') != NULL) {
5523 * If from is a snapshot, hold it and use the more
5524 * efficient dmu_send_estimate to estimate send space
5525 * size using deadlists.
5527 dsl_dataset_t *fromsnap;
5528 error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
5531 error = dmu_send_estimate(tosnap, fromsnap, &space);
5532 dsl_dataset_rele(fromsnap, FTAG);
5533 } else if (strchr(fromname, '#') != NULL) {
5535 * If from is a bookmark, fetch the creation TXG of the
5536 * snapshot it was created from and use that to find
5537 * blocks that were born after it.
5539 zfs_bookmark_phys_t frombm;
5541 error = dsl_bookmark_lookup(dp, fromname, tosnap,
5545 error = dmu_send_estimate_from_txg(tosnap,
5546 frombm.zbm_creation_txg, &space);
5549 * from is not properly formatted as a snapshot or
5552 error = SET_ERROR(EINVAL);
5556 // If estimating the size of a full send, use dmu_send_estimate
5557 error = dmu_send_estimate(tosnap, NULL, &space);
5560 fnvlist_add_uint64(outnvl, "space", space);
5563 dsl_dataset_rele(tosnap, FTAG);
5564 dsl_pool_rele(dp, FTAG);
5568 static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
5571 zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5572 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
5573 boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
5575 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
5577 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
5578 ASSERT3U(ioc, <, ZFS_IOC_LAST);
5579 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
5580 ASSERT3P(vec->zvec_func, ==, NULL);
5582 vec->zvec_legacy_func = func;
5583 vec->zvec_secpolicy = secpolicy;
5584 vec->zvec_namecheck = namecheck;
5585 vec->zvec_allow_log = log_history;
5586 vec->zvec_pool_check = pool_check;
5590 * See the block comment at the beginning of this file for details on
5591 * each argument to this function.
5594 zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
5595 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
5596 zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
5597 boolean_t allow_log)
5599 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
5601 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
5602 ASSERT3U(ioc, <, ZFS_IOC_LAST);
5603 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
5604 ASSERT3P(vec->zvec_func, ==, NULL);
5606 /* if we are logging, the name must be valid */
5607 ASSERT(!allow_log || namecheck != NO_NAME);
5609 vec->zvec_name = name;
5610 vec->zvec_func = func;
5611 vec->zvec_secpolicy = secpolicy;
5612 vec->zvec_namecheck = namecheck;
5613 vec->zvec_pool_check = pool_check;
5614 vec->zvec_smush_outnvlist = smush_outnvlist;
5615 vec->zvec_allow_log = allow_log;
5619 zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5620 zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
5621 zfs_ioc_poolcheck_t pool_check)
5623 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5624 POOL_NAME, log_history, pool_check);
5628 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5629 zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
5631 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5632 DATASET_NAME, B_FALSE, pool_check);
5636 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
5638 zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
5639 POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5643 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5644 zfs_secpolicy_func_t *secpolicy)
5646 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5647 NO_NAME, B_FALSE, POOL_CHECK_NONE);
5651 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
5652 zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
5654 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5655 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
5659 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
5661 zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
5662 zfs_secpolicy_read);
5666 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5667 zfs_secpolicy_func_t *secpolicy)
5669 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5670 DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5674 zfs_ioctl_init(void)
5676 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
5677 zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
5678 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5680 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
5681 zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
5682 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE);
5684 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
5685 zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
5686 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5688 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
5689 zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
5690 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5692 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
5693 zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
5694 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5696 zfs_ioctl_register("create", ZFS_IOC_CREATE,
5697 zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
5698 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5700 zfs_ioctl_register("clone", ZFS_IOC_CLONE,
5701 zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
5702 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5704 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
5705 zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
5706 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5708 zfs_ioctl_register("hold", ZFS_IOC_HOLD,
5709 zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
5710 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5711 zfs_ioctl_register("release", ZFS_IOC_RELEASE,
5712 zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
5713 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5715 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
5716 zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
5717 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5719 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
5720 zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
5721 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
5723 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK,
5724 zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME,
5725 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5727 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS,
5728 zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME,
5729 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5731 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS,
5732 zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks,
5734 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5736 /* IOCTLS that use the legacy function signature */
5738 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
5739 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
5741 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
5742 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5743 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
5745 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
5746 zfs_ioc_pool_upgrade);
5747 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
5749 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
5750 zfs_ioc_vdev_remove);
5751 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
5752 zfs_ioc_vdev_set_state);
5753 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
5754 zfs_ioc_vdev_attach);
5755 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
5756 zfs_ioc_vdev_detach);
5757 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
5758 zfs_ioc_vdev_setpath);
5759 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
5760 zfs_ioc_vdev_setfru);
5761 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
5762 zfs_ioc_pool_set_props);
5763 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
5764 zfs_ioc_vdev_split);
5765 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
5766 zfs_ioc_pool_reguid);
5768 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
5769 zfs_ioc_pool_configs, zfs_secpolicy_none);
5770 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
5771 zfs_ioc_pool_tryimport, zfs_secpolicy_config);
5772 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
5773 zfs_ioc_inject_fault, zfs_secpolicy_inject);
5774 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
5775 zfs_ioc_clear_fault, zfs_secpolicy_inject);
5776 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
5777 zfs_ioc_inject_list_next, zfs_secpolicy_inject);
5780 * pool destroy, and export don't log the history as part of
5781 * zfsdev_ioctl, but rather zfs_ioc_pool_export
5782 * does the logging of those commands.
5784 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
5785 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
5786 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
5787 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
5789 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
5790 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
5791 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
5792 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
5794 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
5795 zfs_secpolicy_inject, B_FALSE, POOL_CHECK_NONE);
5796 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
5797 zfs_ioc_dsobj_to_dsname,
5798 zfs_secpolicy_diff, B_FALSE, POOL_CHECK_NONE);
5799 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
5800 zfs_ioc_pool_get_history,
5801 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
5803 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
5804 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5806 zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
5807 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5808 zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
5809 zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
5811 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
5812 zfs_ioc_space_written);
5813 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
5814 zfs_ioc_objset_recvd_props);
5815 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
5817 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
5819 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
5820 zfs_ioc_objset_stats);
5821 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
5822 zfs_ioc_objset_zplprops);
5823 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
5824 zfs_ioc_dataset_list_next);
5825 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
5826 zfs_ioc_snapshot_list_next);
5827 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
5828 zfs_ioc_send_progress);
5830 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
5831 zfs_ioc_diff, zfs_secpolicy_diff);
5832 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
5833 zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
5834 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
5835 zfs_ioc_obj_to_path, zfs_secpolicy_diff);
5836 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
5837 zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
5838 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
5839 zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
5840 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
5841 zfs_ioc_send, zfs_secpolicy_send);
5843 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
5844 zfs_secpolicy_none);
5845 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
5846 zfs_secpolicy_destroy);
5847 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
5848 zfs_secpolicy_rename);
5849 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
5850 zfs_secpolicy_recv);
5851 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
5852 zfs_secpolicy_promote);
5853 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
5854 zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
5855 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
5856 zfs_secpolicy_set_fsacl);
5858 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
5859 zfs_secpolicy_share, POOL_CHECK_NONE);
5860 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
5861 zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
5862 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
5863 zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
5864 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5865 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
5866 zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
5867 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5870 zfs_ioctl_register_dataset_nolog(ZFS_IOC_JAIL, zfs_ioc_jail,
5871 zfs_secpolicy_config, POOL_CHECK_NONE);
5872 zfs_ioctl_register_dataset_nolog(ZFS_IOC_UNJAIL, zfs_ioc_unjail,
5873 zfs_secpolicy_config, POOL_CHECK_NONE);
5878 pool_status_check(const char *name, zfs_ioc_namecheck_t type,
5879 zfs_ioc_poolcheck_t check)
5884 ASSERT(type == POOL_NAME || type == DATASET_NAME);
5886 if (check & POOL_CHECK_NONE)
5889 error = spa_open(name, &spa, FTAG);
5891 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
5892 error = SET_ERROR(EAGAIN);
5893 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
5894 error = SET_ERROR(EROFS);
5895 spa_close(spa, FTAG);
5901 * Find a free minor number.
5904 zfsdev_minor_alloc(void)
5906 static minor_t last_minor;
5909 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5911 for (m = last_minor + 1; m != last_minor; m++) {
5912 if (m > ZFSDEV_MAX_MINOR)
5914 if (ddi_get_soft_state(zfsdev_state, m) == NULL) {
5924 zfs_ctldev_init(struct cdev *devp)
5927 zfs_soft_state_t *zs;
5929 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5931 minor = zfsdev_minor_alloc();
5933 return (SET_ERROR(ENXIO));
5935 if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS)
5936 return (SET_ERROR(EAGAIN));
5938 devfs_set_cdevpriv((void *)(uintptr_t)minor, zfsdev_close);
5940 zs = ddi_get_soft_state(zfsdev_state, minor);
5941 zs->zss_type = ZSST_CTLDEV;
5942 zfs_onexit_init((zfs_onexit_t **)&zs->zss_data);
5948 zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor)
5950 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5952 zfs_onexit_destroy(zo);
5953 ddi_soft_state_free(zfsdev_state, minor);
5957 zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which)
5959 zfs_soft_state_t *zp;
5961 zp = ddi_get_soft_state(zfsdev_state, minor);
5962 if (zp == NULL || zp->zss_type != which)
5965 return (zp->zss_data);
5969 zfsdev_open(struct cdev *devp, int flag, int mode, struct thread *td)
5974 if (getminor(*devp) != 0)
5975 return (zvol_open(devp, flag, otyp, cr));
5978 /* This is the control device. Allocate a new minor if requested. */
5980 mutex_enter(&spa_namespace_lock);
5981 error = zfs_ctldev_init(devp);
5982 mutex_exit(&spa_namespace_lock);
5989 zfsdev_close(void *data)
5992 minor_t minor = (minor_t)(uintptr_t)data;
5997 mutex_enter(&spa_namespace_lock);
5998 zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV);
6000 mutex_exit(&spa_namespace_lock);
6003 zfs_ctldev_destroy(zo, minor);
6004 mutex_exit(&spa_namespace_lock);
6008 zfsdev_ioctl(struct cdev *dev, u_long zcmd, caddr_t arg, int flag,
6015 minor_t minor = getminor(dev);
6017 zfs_iocparm_t *zc_iocparm;
6018 int cflag, cmd, oldvecnum;
6019 boolean_t newioc, compat;
6020 void *compat_zc = NULL;
6021 cred_t *cr = td->td_ucred;
6023 const zfs_ioc_vec_t *vec;
6024 char *saved_poolname = NULL;
6025 nvlist_t *innvl = NULL;
6027 cflag = ZFS_CMD_COMPAT_NONE;
6029 newioc = B_TRUE; /* "new" style (zfs_iocparm_t) ioctl */
6031 len = IOCPARM_LEN(zcmd);
6032 vecnum = cmd = zcmd & 0xff;
6035 * Check if we are talking to supported older binaries
6036 * and translate zfs_cmd if necessary
6038 if (len != sizeof(zfs_iocparm_t)) {
6045 case sizeof(zfs_cmd_zcmd_t):
6046 cflag = ZFS_CMD_COMPAT_LZC;
6048 case sizeof(zfs_cmd_deadman_t):
6049 cflag = ZFS_CMD_COMPAT_DEADMAN;
6051 case sizeof(zfs_cmd_v28_t):
6052 cflag = ZFS_CMD_COMPAT_V28;
6054 case sizeof(zfs_cmd_v15_t):
6055 cflag = ZFS_CMD_COMPAT_V15;
6056 vecnum = zfs_ioctl_v15_to_v28[cmd];
6059 * Return without further handling
6060 * if the command is blacklisted.
6062 if (vecnum == ZFS_IOC_COMPAT_PASS)
6064 else if (vecnum == ZFS_IOC_COMPAT_FAIL)
6073 vecnum = cmd - ZFS_IOC_FIRST;
6074 ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
6077 if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
6078 return (SET_ERROR(EINVAL));
6079 vec = &zfs_ioc_vec[vecnum];
6081 zc = kmem_zalloc(sizeof(zfs_cmd_t), KM_SLEEP);
6084 error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
6086 error = SET_ERROR(EFAULT);
6089 #else /* !illumos */
6090 bzero(zc, sizeof(zfs_cmd_t));
6093 zc_iocparm = (void *)arg;
6095 switch (zc_iocparm->zfs_ioctl_version) {
6096 case ZFS_IOCVER_CURRENT:
6097 if (zc_iocparm->zfs_cmd_size != sizeof(zfs_cmd_t)) {
6098 error = SET_ERROR(EINVAL);
6102 case ZFS_IOCVER_ZCMD:
6103 if (zc_iocparm->zfs_cmd_size > sizeof(zfs_cmd_t) ||
6104 zc_iocparm->zfs_cmd_size < sizeof(zfs_cmd_zcmd_t)) {
6105 error = SET_ERROR(EFAULT);
6109 cflag = ZFS_CMD_COMPAT_ZCMD;
6112 error = SET_ERROR(EINVAL);
6118 ASSERT(sizeof(zfs_cmd_t) >= zc_iocparm->zfs_cmd_size);
6119 compat_zc = kmem_zalloc(sizeof(zfs_cmd_t), KM_SLEEP);
6120 bzero(compat_zc, sizeof(zfs_cmd_t));
6122 error = ddi_copyin((void *)(uintptr_t)zc_iocparm->zfs_cmd,
6123 compat_zc, zc_iocparm->zfs_cmd_size, flag);
6125 error = SET_ERROR(EFAULT);
6129 error = ddi_copyin((void *)(uintptr_t)zc_iocparm->zfs_cmd,
6130 zc, zc_iocparm->zfs_cmd_size, flag);
6132 error = SET_ERROR(EFAULT);
6140 ASSERT(compat_zc != NULL);
6141 zfs_cmd_compat_get(zc, compat_zc, cflag);
6143 ASSERT(compat_zc == NULL);
6144 zfs_cmd_compat_get(zc, arg, cflag);
6147 error = zfs_ioctl_compat_pre(zc, &vecnum, cflag);
6150 if (oldvecnum != vecnum)
6151 vec = &zfs_ioc_vec[vecnum];
6153 #endif /* !illumos */
6155 zc->zc_iflags = flag & FKIOCTL;
6156 if (zc->zc_nvlist_src_size != 0) {
6157 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
6158 zc->zc_iflags, &innvl);
6163 /* rewrite innvl for backwards compatibility */
6165 innvl = zfs_ioctl_compat_innvl(zc, innvl, vecnum, cflag);
6168 * Ensure that all pool/dataset names are valid before we pass down to
6171 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
6172 switch (vec->zvec_namecheck) {
6174 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
6175 error = SET_ERROR(EINVAL);
6177 error = pool_status_check(zc->zc_name,
6178 vec->zvec_namecheck, vec->zvec_pool_check);
6182 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
6183 error = SET_ERROR(EINVAL);
6185 error = pool_status_check(zc->zc_name,
6186 vec->zvec_namecheck, vec->zvec_pool_check);
6193 if (error == 0 && !(flag & FKIOCTL))
6194 error = vec->zvec_secpolicy(zc, innvl, cr);
6199 /* legacy ioctls can modify zc_name */
6200 len = strcspn(zc->zc_name, "/@#") + 1;
6201 saved_poolname = kmem_alloc(len, KM_SLEEP);
6202 (void) strlcpy(saved_poolname, zc->zc_name, len);
6204 if (vec->zvec_func != NULL) {
6208 nvlist_t *lognv = NULL;
6210 ASSERT(vec->zvec_legacy_func == NULL);
6213 * Add the innvl to the lognv before calling the func,
6214 * in case the func changes the innvl.
6216 if (vec->zvec_allow_log) {
6217 lognv = fnvlist_alloc();
6218 fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
6220 if (!nvlist_empty(innvl)) {
6221 fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
6226 outnvl = fnvlist_alloc();
6227 error = vec->zvec_func(zc->zc_name, innvl, outnvl);
6229 if (error == 0 && vec->zvec_allow_log &&
6230 spa_open(zc->zc_name, &spa, FTAG) == 0) {
6231 if (!nvlist_empty(outnvl)) {
6232 fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL,
6235 (void) spa_history_log_nvl(spa, lognv);
6236 spa_close(spa, FTAG);
6238 fnvlist_free(lognv);
6240 /* rewrite outnvl for backwards compatibility */
6242 outnvl = zfs_ioctl_compat_outnvl(zc, outnvl, vecnum,
6245 if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
6247 if (vec->zvec_smush_outnvlist) {
6248 smusherror = nvlist_smush(outnvl,
6249 zc->zc_nvlist_dst_size);
6251 if (smusherror == 0)
6252 puterror = put_nvlist(zc, outnvl);
6258 nvlist_free(outnvl);
6260 error = vec->zvec_legacy_func(zc);
6267 rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
6268 if (error == 0 && rc != 0)
6269 error = SET_ERROR(EFAULT);
6272 zfs_ioctl_compat_post(zc, cmd, cflag);
6274 ASSERT(compat_zc != NULL);
6275 ASSERT(sizeof(zfs_cmd_t) >= zc_iocparm->zfs_cmd_size);
6277 zfs_cmd_compat_put(zc, compat_zc, vecnum, cflag);
6278 rc = ddi_copyout(compat_zc,
6279 (void *)(uintptr_t)zc_iocparm->zfs_cmd,
6280 zc_iocparm->zfs_cmd_size, flag);
6281 if (error == 0 && rc != 0)
6282 error = SET_ERROR(EFAULT);
6283 kmem_free(compat_zc, sizeof (zfs_cmd_t));
6285 zfs_cmd_compat_put(zc, arg, vecnum, cflag);
6290 rc = ddi_copyout(zc, (void *)(uintptr_t)zc_iocparm->zfs_cmd,
6291 sizeof (zfs_cmd_t), flag);
6292 if (error == 0 && rc != 0)
6293 error = SET_ERROR(EFAULT);
6296 if (error == 0 && vec->zvec_allow_log) {
6297 char *s = tsd_get(zfs_allow_log_key);
6300 (void) tsd_set(zfs_allow_log_key, saved_poolname);
6302 if (saved_poolname != NULL)
6303 strfree(saved_poolname);
6306 kmem_free(zc, sizeof (zfs_cmd_t));
6312 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
6314 if (cmd != DDI_ATTACH)
6315 return (DDI_FAILURE);
6317 if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
6318 DDI_PSEUDO, 0) == DDI_FAILURE)
6319 return (DDI_FAILURE);
6323 ddi_report_dev(dip);
6325 return (DDI_SUCCESS);
6329 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
6331 if (spa_busy() || zfs_busy() || zvol_busy())
6332 return (DDI_FAILURE);
6334 if (cmd != DDI_DETACH)
6335 return (DDI_FAILURE);
6339 ddi_prop_remove_all(dip);
6340 ddi_remove_minor_node(dip, NULL);
6342 return (DDI_SUCCESS);
6347 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
6350 case DDI_INFO_DEVT2DEVINFO:
6352 return (DDI_SUCCESS);
6354 case DDI_INFO_DEVT2INSTANCE:
6355 *result = (void *)0;
6356 return (DDI_SUCCESS);
6359 return (DDI_FAILURE);
6364 * OK, so this is a little weird.
6366 * /dev/zfs is the control node, i.e. minor 0.
6367 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
6369 * /dev/zfs has basically nothing to do except serve up ioctls,
6370 * so most of the standard driver entry points are in zvol.c.
6373 static struct cb_ops zfs_cb_ops = {
6374 zfsdev_open, /* open */
6375 zfsdev_close, /* close */
6376 zvol_strategy, /* strategy */
6378 zvol_dump, /* dump */
6379 zvol_read, /* read */
6380 zvol_write, /* write */
6381 zfsdev_ioctl, /* ioctl */
6385 nochpoll, /* poll */
6386 ddi_prop_op, /* prop_op */
6387 NULL, /* streamtab */
6388 D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */
6389 CB_REV, /* version */
6390 nodev, /* async read */
6391 nodev, /* async write */
6394 static struct dev_ops zfs_dev_ops = {
6395 DEVO_REV, /* version */
6397 zfs_info, /* info */
6398 nulldev, /* identify */
6399 nulldev, /* probe */
6400 zfs_attach, /* attach */
6401 zfs_detach, /* detach */
6403 &zfs_cb_ops, /* driver operations */
6404 NULL, /* no bus operations */
6406 ddi_quiesce_not_needed, /* quiesce */
6409 static struct modldrv zfs_modldrv = {
6415 static struct modlinkage modlinkage = {
6417 (void *)&zfs_modlfs,
6418 (void *)&zfs_modldrv,
6423 static struct cdevsw zfs_cdevsw = {
6424 .d_version = D_VERSION,
6425 .d_open = zfsdev_open,
6426 .d_ioctl = zfsdev_ioctl,
6427 .d_name = ZFS_DEV_NAME
6431 zfs_allow_log_destroy(void *arg)
6433 char *poolname = arg;
6440 zfsdev = make_dev(&zfs_cdevsw, 0x0, UID_ROOT, GID_OPERATOR, 0666,
6448 destroy_dev(zfsdev);
6451 static struct root_hold_token *zfs_root_token;
6452 struct proc *zfsproc;
6460 spa_init(FREAD | FWRITE);
6465 if ((error = mod_install(&modlinkage)) != 0) {
6472 tsd_create(&zfs_fsyncer_key, NULL);
6473 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
6474 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
6476 error = ldi_ident_from_mod(&modlinkage, &zfs_li);
6478 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
6488 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
6489 return (SET_ERROR(EBUSY));
6491 if ((error = mod_remove(&modlinkage)) != 0)
6497 if (zfs_nfsshare_inited)
6498 (void) ddi_modclose(nfs_mod);
6499 if (zfs_smbshare_inited)
6500 (void) ddi_modclose(smbsrv_mod);
6501 if (zfs_nfsshare_inited || zfs_smbshare_inited)
6502 (void) ddi_modclose(sharefs_mod);
6504 tsd_destroy(&zfs_fsyncer_key);
6505 ldi_ident_release(zfs_li);
6507 mutex_destroy(&zfs_share_lock);
6513 _info(struct modinfo *modinfop)
6515 return (mod_info(&modlinkage, modinfop));
6519 static int zfs__init(void);
6520 static int zfs__fini(void);
6521 static void zfs_shutdown(void *, int);
6523 static eventhandler_tag zfs_shutdown_event_tag;
6525 #define ZFS_MIN_KSTACK_PAGES 4
6531 #if KSTACK_PAGES < ZFS_MIN_KSTACK_PAGES
6532 printf("ZFS NOTICE: KSTACK_PAGES is %d which could result in stack "
6533 "overflow panic!\nPlease consider adding "
6534 "'options KSTACK_PAGES=%d' to your kernel config\n", KSTACK_PAGES,
6535 ZFS_MIN_KSTACK_PAGES);
6537 zfs_root_token = root_mount_hold("ZFS");
6539 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
6541 spa_init(FREAD | FWRITE);
6546 tsd_create(&zfs_fsyncer_key, NULL);
6547 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
6548 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
6550 printf("ZFS storage pool version: features support (" SPA_VERSION_STRING ")\n");
6551 root_mount_rel(zfs_root_token);
6561 if (spa_busy() || zfs_busy() || zvol_busy() ||
6562 zio_injection_enabled) {
6571 tsd_destroy(&zfs_fsyncer_key);
6572 tsd_destroy(&rrw_tsd_key);
6573 tsd_destroy(&zfs_allow_log_key);
6575 mutex_destroy(&zfs_share_lock);
6581 zfs_shutdown(void *arg __unused, int howto __unused)
6585 * ZFS fini routines can not properly work in a panic-ed system.
6587 if (panicstr == NULL)
6593 zfs_modevent(module_t mod, int type, void *unused __unused)
6601 zfs_shutdown_event_tag = EVENTHANDLER_REGISTER(
6602 shutdown_post_sync, zfs_shutdown, NULL,
6603 SHUTDOWN_PRI_FIRST);
6607 if (err == 0 && zfs_shutdown_event_tag != NULL)
6608 EVENTHANDLER_DEREGISTER(shutdown_post_sync,
6609 zfs_shutdown_event_tag);
6616 return (EOPNOTSUPP);
6619 static moduledata_t zfs_mod = {
6624 DECLARE_MODULE(zfsctrl, zfs_mod, SI_SUB_VFS, SI_ORDER_ANY);
6625 MODULE_VERSION(zfsctrl, 1);
6626 MODULE_DEPEND(zfsctrl, opensolaris, 1, 1, 1);
6627 MODULE_DEPEND(zfsctrl, krpc, 1, 1, 1);
6628 MODULE_DEPEND(zfsctrl, acl_nfs4, 1, 1, 1);