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 2011 Nexenta Systems, Inc. All rights reserved.
28 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
29 * Copyright (c) 2013 by Delphix. All rights reserved.
30 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
31 * Copyright (c) 2013 Steven Hartland. All rights reserved.
37 * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
38 * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
40 * There are two ways that we handle ioctls: the legacy way where almost
41 * all of the logic is in the ioctl callback, and the new way where most
42 * of the marshalling is handled in the common entry point, zfsdev_ioctl().
44 * Non-legacy ioctls should be registered by calling
45 * zfs_ioctl_register() from zfs_ioctl_init(). The ioctl is invoked
46 * from userland by lzc_ioctl().
48 * The registration arguments are as follows:
51 * The name of the ioctl. This is used for history logging. If the
52 * ioctl returns successfully (the callback returns 0), and allow_log
53 * is true, then a history log entry will be recorded with the input &
54 * output nvlists. The log entry can be printed with "zpool history -i".
57 * The ioctl request number, which userland will pass to ioctl(2).
58 * The ioctl numbers can change from release to release, because
59 * the caller (libzfs) must be matched to the kernel.
61 * zfs_secpolicy_func_t *secpolicy
62 * This function will be called before the zfs_ioc_func_t, to
63 * determine if this operation is permitted. It should return EPERM
64 * on failure, and 0 on success. Checks include determining if the
65 * dataset is visible in this zone, and if the user has either all
66 * zfs privileges in the zone (SYS_MOUNT), or has been granted permission
67 * to do this operation on this dataset with "zfs allow".
69 * zfs_ioc_namecheck_t namecheck
70 * This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
71 * name, a dataset name, or nothing. If the name is not well-formed,
72 * the ioctl will fail and the callback will not be called.
73 * Therefore, the callback can assume that the name is well-formed
74 * (e.g. is null-terminated, doesn't have more than one '@' character,
75 * doesn't have invalid characters).
77 * zfs_ioc_poolcheck_t pool_check
78 * This specifies requirements on the pool state. If the pool does
79 * not meet them (is suspended or is readonly), the ioctl will fail
80 * and the callback will not be called. If any checks are specified
81 * (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
82 * Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
83 * POOL_CHECK_READONLY).
85 * boolean_t smush_outnvlist
86 * If smush_outnvlist is true, then the output is presumed to be a
87 * list of errors, and it will be "smushed" down to fit into the
88 * caller's buffer, by removing some entries and replacing them with a
89 * single "N_MORE_ERRORS" entry indicating how many were removed. See
90 * nvlist_smush() for details. If smush_outnvlist is false, and the
91 * outnvlist does not fit into the userland-provided buffer, then the
92 * ioctl will fail with ENOMEM.
94 * zfs_ioc_func_t *func
95 * The callback function that will perform the operation.
97 * The callback should return 0 on success, or an error number on
98 * failure. If the function fails, the userland ioctl will return -1,
99 * and errno will be set to the callback's return value. The callback
100 * will be called with the following arguments:
103 * The name of the pool or dataset to operate on, from
104 * zfs_cmd_t:zc_name. The 'namecheck' argument specifies the
105 * expected type (pool, dataset, or none).
108 * The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src. Or
109 * NULL if no input nvlist was provided. Changes to this nvlist are
110 * ignored. If the input nvlist could not be deserialized, the
111 * ioctl will fail and the callback will not be called.
114 * The output nvlist, initially empty. The callback can fill it in,
115 * and it will be returned to userland by serializing it into
116 * zfs_cmd_t:zc_nvlist_dst. If it is non-empty, and serialization
117 * fails (e.g. because the caller didn't supply a large enough
118 * buffer), then the overall ioctl will fail. See the
119 * 'smush_nvlist' argument above for additional behaviors.
121 * There are two typical uses of the output nvlist:
122 * - To return state, e.g. property values. In this case,
123 * smush_outnvlist should be false. If the buffer was not large
124 * enough, the caller will reallocate a larger buffer and try
127 * - To return multiple errors from an ioctl which makes on-disk
128 * changes. In this case, smush_outnvlist should be true.
129 * Ioctls which make on-disk modifications should generally not
130 * use the outnvl if they succeed, because the caller can not
131 * distinguish between the operation failing, and
132 * deserialization failing.
135 #include <sys/types.h>
136 #include <sys/param.h>
137 #include <sys/systm.h>
138 #include <sys/conf.h>
139 #include <sys/kernel.h>
140 #include <sys/lock.h>
141 #include <sys/malloc.h>
142 #include <sys/mutex.h>
143 #include <sys/proc.h>
144 #include <sys/errno.h>
147 #include <sys/file.h>
148 #include <sys/kmem.h>
149 #include <sys/conf.h>
150 #include <sys/cmn_err.h>
151 #include <sys/stat.h>
152 #include <sys/zfs_ioctl.h>
153 #include <sys/zfs_vfsops.h>
154 #include <sys/zfs_znode.h>
157 #include <sys/spa_impl.h>
158 #include <sys/vdev.h>
160 #include <sys/dsl_dir.h>
161 #include <sys/dsl_dataset.h>
162 #include <sys/dsl_prop.h>
163 #include <sys/dsl_deleg.h>
164 #include <sys/dmu_objset.h>
165 #include <sys/dmu_impl.h>
166 #include <sys/dmu_tx.h>
167 #include <sys/sunddi.h>
168 #include <sys/policy.h>
169 #include <sys/zone.h>
170 #include <sys/nvpair.h>
171 #include <sys/mount.h>
172 #include <sys/taskqueue.h>
174 #include <sys/varargs.h>
175 #include <sys/fs/zfs.h>
176 #include <sys/zfs_ctldir.h>
177 #include <sys/zfs_dir.h>
178 #include <sys/zfs_onexit.h>
179 #include <sys/zvol.h>
180 #include <sys/dsl_scan.h>
181 #include <sys/dmu_objset.h>
182 #include <sys/dmu_send.h>
183 #include <sys/dsl_destroy.h>
184 #include <sys/dsl_userhold.h>
185 #include <sys/zfeature.h>
187 #include "zfs_namecheck.h"
188 #include "zfs_prop.h"
189 #include "zfs_deleg.h"
190 #include "zfs_comutil.h"
191 #include "zfs_ioctl_compat.h"
193 CTASSERT(sizeof(zfs_cmd_t) < IOCPARM_MAX);
195 static int snapshot_list_prefetch;
196 SYSCTL_DECL(_vfs_zfs);
197 TUNABLE_INT("vfs.zfs.snapshot_list_prefetch", &snapshot_list_prefetch);
198 SYSCTL_INT(_vfs_zfs, OID_AUTO, snapshot_list_prefetch, CTLFLAG_RW,
199 &snapshot_list_prefetch, 0, "Prefetch data when listing snapshots");
201 static struct cdev *zfsdev;
203 extern void zfs_init(void);
204 extern void zfs_fini(void);
206 uint_t zfs_fsyncer_key;
207 extern uint_t rrw_tsd_key;
208 static uint_t zfs_allow_log_key;
210 typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *);
211 typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *);
212 typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *);
218 } zfs_ioc_namecheck_t;
221 POOL_CHECK_NONE = 1 << 0,
222 POOL_CHECK_SUSPENDED = 1 << 1,
223 POOL_CHECK_READONLY = 1 << 2,
224 } zfs_ioc_poolcheck_t;
226 typedef struct zfs_ioc_vec {
227 zfs_ioc_legacy_func_t *zvec_legacy_func;
228 zfs_ioc_func_t *zvec_func;
229 zfs_secpolicy_func_t *zvec_secpolicy;
230 zfs_ioc_namecheck_t zvec_namecheck;
231 boolean_t zvec_allow_log;
232 zfs_ioc_poolcheck_t zvec_pool_check;
233 boolean_t zvec_smush_outnvlist;
234 const char *zvec_name;
237 /* This array is indexed by zfs_userquota_prop_t */
238 static const char *userquota_perms[] = {
239 ZFS_DELEG_PERM_USERUSED,
240 ZFS_DELEG_PERM_USERQUOTA,
241 ZFS_DELEG_PERM_GROUPUSED,
242 ZFS_DELEG_PERM_GROUPQUOTA,
245 static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
246 static int zfs_check_settable(const char *name, nvpair_t *property,
248 static int zfs_check_clearable(char *dataset, nvlist_t *props,
250 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
252 int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
253 static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
255 static void zfsdev_close(void *data);
257 static int zfs_prop_activate_feature(spa_t *spa, zfeature_info_t *feature);
259 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
261 __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
268 * Get rid of annoying "../common/" prefix to filename.
270 newfile = strrchr(file, '/');
271 if (newfile != NULL) {
272 newfile = newfile + 1; /* Get rid of leading / */
278 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
282 * To get this data, use the zfs-dprintf probe as so:
283 * dtrace -q -n 'zfs-dprintf \
284 * /stringof(arg0) == "dbuf.c"/ \
285 * {printf("%s: %s", stringof(arg1), stringof(arg3))}'
287 * arg1 = function name
291 DTRACE_PROBE4(zfs__dprintf,
292 char *, newfile, char *, func, int, line, char *, buf);
296 history_str_free(char *buf)
298 kmem_free(buf, HIS_MAX_RECORD_LEN);
302 history_str_get(zfs_cmd_t *zc)
306 if (zc->zc_history == 0)
309 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
310 if (copyinstr((void *)(uintptr_t)zc->zc_history,
311 buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
312 history_str_free(buf);
316 buf[HIS_MAX_RECORD_LEN -1] = '\0';
322 * Check to see if the named dataset is currently defined as bootable
325 zfs_is_bootfs(const char *name)
329 if (dmu_objset_hold(name, FTAG, &os) == 0) {
331 ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
332 dmu_objset_rele(os, FTAG);
339 * Return non-zero if the spa version is less than requested version.
342 zfs_earlier_version(const char *name, int version)
346 if (spa_open(name, &spa, FTAG) == 0) {
347 if (spa_version(spa) < version) {
348 spa_close(spa, FTAG);
351 spa_close(spa, FTAG);
357 * Return TRUE if the ZPL version is less than requested version.
360 zpl_earlier_version(const char *name, int version)
363 boolean_t rc = B_TRUE;
365 if (dmu_objset_hold(name, FTAG, &os) == 0) {
368 if (dmu_objset_type(os) != DMU_OST_ZFS) {
369 dmu_objset_rele(os, FTAG);
372 /* XXX reading from non-owned objset */
373 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
374 rc = zplversion < version;
375 dmu_objset_rele(os, FTAG);
381 zfs_log_history(zfs_cmd_t *zc)
386 if ((buf = history_str_get(zc)) == NULL)
389 if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
390 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
391 (void) spa_history_log(spa, buf);
392 spa_close(spa, FTAG);
394 history_str_free(buf);
398 * Policy for top-level read operations (list pools). Requires no privileges,
399 * and can be used in the local zone, as there is no associated dataset.
403 zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
409 * Policy for dataset read operations (list children, get statistics). Requires
410 * no privileges, but must be visible in the local zone.
414 zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
416 if (INGLOBALZONE(curthread) ||
417 zone_dataset_visible(zc->zc_name, NULL))
420 return (SET_ERROR(ENOENT));
424 zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
429 * The dataset must be visible by this zone -- check this first
430 * so they don't see EPERM on something they shouldn't know about.
432 if (!INGLOBALZONE(curthread) &&
433 !zone_dataset_visible(dataset, &writable))
434 return (SET_ERROR(ENOENT));
436 if (INGLOBALZONE(curthread)) {
438 * If the fs is zoned, only root can access it from the
441 if (secpolicy_zfs(cr) && zoned)
442 return (SET_ERROR(EPERM));
445 * If we are in a local zone, the 'zoned' property must be set.
448 return (SET_ERROR(EPERM));
450 /* must be writable by this zone */
452 return (SET_ERROR(EPERM));
458 zfs_dozonecheck(const char *dataset, cred_t *cr)
462 if (dsl_prop_get_integer(dataset, "jailed", &zoned, NULL))
463 return (SET_ERROR(ENOENT));
465 return (zfs_dozonecheck_impl(dataset, zoned, cr));
469 zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
473 if (dsl_prop_get_int_ds(ds, "jailed", &zoned))
474 return (SET_ERROR(ENOENT));
476 return (zfs_dozonecheck_impl(dataset, zoned, cr));
480 zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
481 const char *perm, cred_t *cr)
485 error = zfs_dozonecheck_ds(name, ds, cr);
487 error = secpolicy_zfs(cr);
489 error = dsl_deleg_access_impl(ds, perm, cr);
495 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
501 error = dsl_pool_hold(name, FTAG, &dp);
505 error = dsl_dataset_hold(dp, name, FTAG, &ds);
507 dsl_pool_rele(dp, FTAG);
511 error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
513 dsl_dataset_rele(ds, FTAG);
514 dsl_pool_rele(dp, FTAG);
520 * Policy for setting the security label property.
522 * Returns 0 for success, non-zero for access and other errors.
525 zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
527 char ds_hexsl[MAXNAMELEN];
528 bslabel_t ds_sl, new_sl;
529 boolean_t new_default = FALSE;
531 int needed_priv = -1;
534 /* First get the existing dataset label. */
535 error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
536 1, sizeof (ds_hexsl), &ds_hexsl, NULL);
538 return (SET_ERROR(EPERM));
540 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
543 /* The label must be translatable */
544 if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
545 return (SET_ERROR(EINVAL));
548 * In a non-global zone, disallow attempts to set a label that
549 * doesn't match that of the zone; otherwise no other checks
552 if (!INGLOBALZONE(curproc)) {
553 if (new_default || !blequal(&new_sl, CR_SL(CRED())))
554 return (SET_ERROR(EPERM));
559 * For global-zone datasets (i.e., those whose zoned property is
560 * "off", verify that the specified new label is valid for the
563 if (dsl_prop_get_integer(name,
564 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
565 return (SET_ERROR(EPERM));
567 if (zfs_check_global_label(name, strval) != 0)
568 return (SET_ERROR(EPERM));
572 * If the existing dataset label is nondefault, check if the
573 * dataset is mounted (label cannot be changed while mounted).
574 * Get the zfsvfs; if there isn't one, then the dataset isn't
575 * mounted (or isn't a dataset, doesn't exist, ...).
577 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
579 static char *setsl_tag = "setsl_tag";
582 * Try to own the dataset; abort if there is any error,
583 * (e.g., already mounted, in use, or other error).
585 error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE,
588 return (SET_ERROR(EPERM));
590 dmu_objset_disown(os, setsl_tag);
593 needed_priv = PRIV_FILE_DOWNGRADE_SL;
597 if (hexstr_to_label(strval, &new_sl) != 0)
598 return (SET_ERROR(EPERM));
600 if (blstrictdom(&ds_sl, &new_sl))
601 needed_priv = PRIV_FILE_DOWNGRADE_SL;
602 else if (blstrictdom(&new_sl, &ds_sl))
603 needed_priv = PRIV_FILE_UPGRADE_SL;
605 /* dataset currently has a default label */
607 needed_priv = PRIV_FILE_UPGRADE_SL;
611 if (needed_priv != -1)
612 return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
615 #endif /* SECLABEL */
618 zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
624 * Check permissions for special properties.
629 * Disallow setting of 'zoned' from within a local zone.
631 if (!INGLOBALZONE(curthread))
632 return (SET_ERROR(EPERM));
636 if (!INGLOBALZONE(curthread)) {
638 char setpoint[MAXNAMELEN];
640 * Unprivileged users are allowed to modify the
641 * quota on things *under* (ie. contained by)
642 * the thing they own.
644 if (dsl_prop_get_integer(dsname, "jailed", &zoned,
646 return (SET_ERROR(EPERM));
647 if (!zoned || strlen(dsname) <= strlen(setpoint))
648 return (SET_ERROR(EPERM));
652 case ZFS_PROP_MLSLABEL:
654 if (!is_system_labeled())
655 return (SET_ERROR(EPERM));
657 if (nvpair_value_string(propval, &strval) == 0) {
660 err = zfs_set_slabel_policy(dsname, strval, CRED());
670 return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
675 zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
679 error = zfs_dozonecheck(zc->zc_name, cr);
684 * permission to set permissions will be evaluated later in
685 * dsl_deleg_can_allow()
692 zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
694 return (zfs_secpolicy_write_perms(zc->zc_name,
695 ZFS_DELEG_PERM_ROLLBACK, cr));
700 zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
708 * Generate the current snapshot name from the given objsetid, then
709 * use that name for the secpolicy/zone checks.
711 cp = strchr(zc->zc_name, '@');
713 return (SET_ERROR(EINVAL));
714 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
718 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
720 dsl_pool_rele(dp, FTAG);
724 dsl_dataset_name(ds, zc->zc_name);
726 error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
727 ZFS_DELEG_PERM_SEND, cr);
728 dsl_dataset_rele(ds, FTAG);
729 dsl_pool_rele(dp, FTAG);
736 zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
738 return (zfs_secpolicy_write_perms(zc->zc_name,
739 ZFS_DELEG_PERM_SEND, cr));
744 zfs_secpolicy_deleg_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
749 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
750 NO_FOLLOW, NULL, &vp)) != 0)
753 /* Now make sure mntpnt and dataset are ZFS */
755 if (strcmp(vp->v_vfsp->mnt_stat.f_fstypename, "zfs") != 0 ||
756 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
757 zc->zc_name) != 0)) {
759 return (SET_ERROR(EPERM));
763 return (dsl_deleg_access(zc->zc_name,
764 ZFS_DELEG_PERM_SHARE, cr));
768 zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
770 if (!INGLOBALZONE(curthread))
771 return (SET_ERROR(EPERM));
773 if (secpolicy_nfs(cr) == 0) {
776 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
781 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
783 if (!INGLOBALZONE(curthread))
784 return (SET_ERROR(EPERM));
786 if (secpolicy_smb(cr) == 0) {
789 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
794 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
799 * Remove the @bla or /bla from the end of the name to get the parent.
801 (void) strncpy(parent, datasetname, parentsize);
802 cp = strrchr(parent, '@');
806 cp = strrchr(parent, '/');
808 return (SET_ERROR(ENOENT));
816 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
820 if ((error = zfs_secpolicy_write_perms(name,
821 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
824 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
829 zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
831 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
835 * Destroying snapshots with delegated permissions requires
836 * descendant mount and destroy permissions.
840 zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
843 nvpair_t *pair, *nextpair;
846 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
847 return (SET_ERROR(EINVAL));
848 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
853 error = dsl_pool_hold(nvpair_name(pair), FTAG, &dp);
856 nextpair = nvlist_next_nvpair(snaps, pair);
857 error = dsl_dataset_hold(dp, nvpair_name(pair), FTAG, &ds);
859 dsl_dataset_rele(ds, FTAG);
860 dsl_pool_rele(dp, FTAG);
863 error = zfs_secpolicy_destroy_perms(nvpair_name(pair),
865 } else if (error == ENOENT) {
867 * Ignore any snapshots that don't exist (we consider
868 * them "already destroyed"). Remove the name from the
869 * nvl here in case the snapshot is created between
870 * now and when we try to destroy it (in which case
871 * we don't want to destroy it since we haven't
872 * checked for permission).
874 fnvlist_remove_nvpair(snaps, pair);
885 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
887 char parentname[MAXNAMELEN];
890 if ((error = zfs_secpolicy_write_perms(from,
891 ZFS_DELEG_PERM_RENAME, cr)) != 0)
894 if ((error = zfs_secpolicy_write_perms(from,
895 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
898 if ((error = zfs_get_parent(to, parentname,
899 sizeof (parentname))) != 0)
902 if ((error = zfs_secpolicy_write_perms(parentname,
903 ZFS_DELEG_PERM_CREATE, cr)) != 0)
906 if ((error = zfs_secpolicy_write_perms(parentname,
907 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
915 zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
920 if ((zc->zc_cookie & 1) != 0) {
922 * This is recursive rename, so the starting snapshot might
923 * not exist. Check file system or volume permission instead.
925 at = strchr(zc->zc_name, '@');
931 error = zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr);
941 zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
944 dsl_dataset_t *clone;
947 error = zfs_secpolicy_write_perms(zc->zc_name,
948 ZFS_DELEG_PERM_PROMOTE, cr);
952 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
956 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
959 char parentname[MAXNAMELEN];
960 dsl_dataset_t *origin = NULL;
964 error = dsl_dataset_hold_obj(dd->dd_pool,
965 dd->dd_phys->dd_origin_obj, FTAG, &origin);
967 dsl_dataset_rele(clone, FTAG);
968 dsl_pool_rele(dp, FTAG);
972 error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
973 ZFS_DELEG_PERM_MOUNT, cr);
975 dsl_dataset_name(origin, parentname);
977 error = zfs_secpolicy_write_perms_ds(parentname, origin,
978 ZFS_DELEG_PERM_PROMOTE, cr);
980 dsl_dataset_rele(clone, FTAG);
981 dsl_dataset_rele(origin, FTAG);
983 dsl_pool_rele(dp, FTAG);
989 zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
993 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
994 ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
997 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
998 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
1001 return (zfs_secpolicy_write_perms(zc->zc_name,
1002 ZFS_DELEG_PERM_CREATE, cr));
1006 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
1008 return (zfs_secpolicy_write_perms(name,
1009 ZFS_DELEG_PERM_SNAPSHOT, cr));
1013 * Check for permission to create each snapshot in the nvlist.
1017 zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1023 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
1024 return (SET_ERROR(EINVAL));
1025 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
1026 pair = nvlist_next_nvpair(snaps, pair)) {
1027 char *name = nvpair_name(pair);
1028 char *atp = strchr(name, '@');
1031 error = SET_ERROR(EINVAL);
1035 error = zfs_secpolicy_snapshot_perms(name, cr);
1045 zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1048 * Even root must have a proper TSD so that we know what pool
1051 if (tsd_get(zfs_allow_log_key) == NULL)
1052 return (SET_ERROR(EPERM));
1057 zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1059 char parentname[MAXNAMELEN];
1063 if ((error = zfs_get_parent(zc->zc_name, parentname,
1064 sizeof (parentname))) != 0)
1067 if (nvlist_lookup_string(innvl, "origin", &origin) == 0 &&
1068 (error = zfs_secpolicy_write_perms(origin,
1069 ZFS_DELEG_PERM_CLONE, cr)) != 0)
1072 if ((error = zfs_secpolicy_write_perms(parentname,
1073 ZFS_DELEG_PERM_CREATE, cr)) != 0)
1076 return (zfs_secpolicy_write_perms(parentname,
1077 ZFS_DELEG_PERM_MOUNT, cr));
1081 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
1082 * SYS_CONFIG privilege, which is not available in a local zone.
1086 zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1088 if (secpolicy_sys_config(cr, B_FALSE) != 0)
1089 return (SET_ERROR(EPERM));
1095 * Policy for object to name lookups.
1099 zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1103 if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
1106 error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
1111 * Policy for fault injection. Requires all privileges.
1115 zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1117 return (secpolicy_zinject(cr));
1122 zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1124 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
1126 if (prop == ZPROP_INVAL) {
1127 if (!zfs_prop_user(zc->zc_value))
1128 return (SET_ERROR(EINVAL));
1129 return (zfs_secpolicy_write_perms(zc->zc_name,
1130 ZFS_DELEG_PERM_USERPROP, cr));
1132 return (zfs_secpolicy_setprop(zc->zc_name, prop,
1138 zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1140 int err = zfs_secpolicy_read(zc, innvl, cr);
1144 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1145 return (SET_ERROR(EINVAL));
1147 if (zc->zc_value[0] == 0) {
1149 * They are asking about a posix uid/gid. If it's
1150 * themself, allow it.
1152 if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
1153 zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
1154 if (zc->zc_guid == crgetuid(cr))
1157 if (groupmember(zc->zc_guid, cr))
1162 return (zfs_secpolicy_write_perms(zc->zc_name,
1163 userquota_perms[zc->zc_objset_type], cr));
1167 zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1169 int err = zfs_secpolicy_read(zc, innvl, cr);
1173 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1174 return (SET_ERROR(EINVAL));
1176 return (zfs_secpolicy_write_perms(zc->zc_name,
1177 userquota_perms[zc->zc_objset_type], cr));
1182 zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1184 return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
1190 zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1196 error = nvlist_lookup_nvlist(innvl, "holds", &holds);
1198 return (SET_ERROR(EINVAL));
1200 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
1201 pair = nvlist_next_nvpair(holds, pair)) {
1202 char fsname[MAXNAMELEN];
1203 error = dmu_fsname(nvpair_name(pair), fsname);
1206 error = zfs_secpolicy_write_perms(fsname,
1207 ZFS_DELEG_PERM_HOLD, cr);
1216 zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1221 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1222 pair = nvlist_next_nvpair(innvl, pair)) {
1223 char fsname[MAXNAMELEN];
1224 error = dmu_fsname(nvpair_name(pair), fsname);
1227 error = zfs_secpolicy_write_perms(fsname,
1228 ZFS_DELEG_PERM_RELEASE, cr);
1236 * Policy for allowing temporary snapshots to be taken or released
1239 zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1242 * A temporary snapshot is the same as a snapshot,
1243 * hold, destroy and release all rolled into one.
1244 * Delegated diff alone is sufficient that we allow this.
1248 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1249 ZFS_DELEG_PERM_DIFF, cr)) == 0)
1252 error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
1254 error = zfs_secpolicy_hold(zc, innvl, cr);
1256 error = zfs_secpolicy_release(zc, innvl, cr);
1258 error = zfs_secpolicy_destroy(zc, innvl, cr);
1263 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1266 get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1270 nvlist_t *list = NULL;
1273 * Read in and unpack the user-supplied nvlist.
1276 return (SET_ERROR(EINVAL));
1278 packed = kmem_alloc(size, KM_SLEEP);
1280 if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1282 kmem_free(packed, size);
1286 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1287 kmem_free(packed, size);
1291 kmem_free(packed, size);
1298 * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
1299 * Entries will be removed from the end of the nvlist, and one int32 entry
1300 * named "N_MORE_ERRORS" will be added indicating how many entries were
1304 nvlist_smush(nvlist_t *errors, size_t max)
1308 size = fnvlist_size(errors);
1311 nvpair_t *more_errors;
1315 return (SET_ERROR(ENOMEM));
1317 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
1318 more_errors = nvlist_prev_nvpair(errors, NULL);
1321 nvpair_t *pair = nvlist_prev_nvpair(errors,
1323 fnvlist_remove_nvpair(errors, pair);
1325 size = fnvlist_size(errors);
1326 } while (size > max);
1328 fnvlist_remove_nvpair(errors, more_errors);
1329 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
1330 ASSERT3U(fnvlist_size(errors), <=, max);
1337 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1339 char *packed = NULL;
1343 size = fnvlist_size(nvl);
1345 if (size > zc->zc_nvlist_dst_size) {
1347 * Solaris returns ENOMEM here, because even if an error is
1348 * returned from an ioctl(2), new zc_nvlist_dst_size will be
1349 * passed to the userland. This is not the case for FreeBSD.
1350 * We need to return 0, so the kernel will copy the
1351 * zc_nvlist_dst_size back and the userland can discover that a
1352 * bigger buffer is needed.
1356 packed = fnvlist_pack(nvl, &size);
1357 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1358 size, zc->zc_iflags) != 0)
1359 error = SET_ERROR(EFAULT);
1360 fnvlist_pack_free(packed, size);
1363 zc->zc_nvlist_dst_size = size;
1364 zc->zc_nvlist_dst_filled = B_TRUE;
1369 getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
1374 error = dmu_objset_hold(dsname, FTAG, &os);
1377 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1378 dmu_objset_rele(os, FTAG);
1379 return (SET_ERROR(EINVAL));
1382 mutex_enter(&os->os_user_ptr_lock);
1383 *zfvp = dmu_objset_get_user(os);
1385 VFS_HOLD((*zfvp)->z_vfs);
1387 error = SET_ERROR(ESRCH);
1389 mutex_exit(&os->os_user_ptr_lock);
1390 dmu_objset_rele(os, FTAG);
1395 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
1396 * case its z_vfs will be NULL, and it will be opened as the owner.
1397 * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1398 * which prevents all vnode ops from running.
1401 zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
1405 if (getzfsvfs(name, zfvp) != 0)
1406 error = zfsvfs_create(name, zfvp);
1408 rrw_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
1410 if ((*zfvp)->z_unmounted) {
1412 * XXX we could probably try again, since the unmounting
1413 * thread should be just about to disassociate the
1414 * objset from the zfsvfs.
1416 rrw_exit(&(*zfvp)->z_teardown_lock, tag);
1417 return (SET_ERROR(EBUSY));
1424 zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
1426 rrw_exit(&zfsvfs->z_teardown_lock, tag);
1428 if (zfsvfs->z_vfs) {
1429 VFS_RELE(zfsvfs->z_vfs);
1431 dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1432 zfsvfs_free(zfsvfs);
1437 zfs_ioc_pool_create(zfs_cmd_t *zc)
1440 nvlist_t *config, *props = NULL;
1441 nvlist_t *rootprops = NULL;
1442 nvlist_t *zplprops = NULL;
1444 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1445 zc->zc_iflags, &config))
1448 if (zc->zc_nvlist_src_size != 0 && (error =
1449 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1450 zc->zc_iflags, &props))) {
1451 nvlist_free(config);
1456 nvlist_t *nvl = NULL;
1457 uint64_t version = SPA_VERSION;
1459 (void) nvlist_lookup_uint64(props,
1460 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1461 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1462 error = SET_ERROR(EINVAL);
1463 goto pool_props_bad;
1465 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
1467 error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
1469 nvlist_free(config);
1473 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1475 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1476 error = zfs_fill_zplprops_root(version, rootprops,
1479 goto pool_props_bad;
1482 error = spa_create(zc->zc_name, config, props, zplprops);
1485 * Set the remaining root properties
1487 if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
1488 ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
1489 (void) spa_destroy(zc->zc_name);
1492 nvlist_free(rootprops);
1493 nvlist_free(zplprops);
1494 nvlist_free(config);
1501 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1504 zfs_log_history(zc);
1505 error = spa_destroy(zc->zc_name);
1507 zvol_remove_minors(zc->zc_name);
1512 zfs_ioc_pool_import(zfs_cmd_t *zc)
1514 nvlist_t *config, *props = NULL;
1518 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1519 zc->zc_iflags, &config)) != 0)
1522 if (zc->zc_nvlist_src_size != 0 && (error =
1523 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1524 zc->zc_iflags, &props))) {
1525 nvlist_free(config);
1529 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1530 guid != zc->zc_guid)
1531 error = SET_ERROR(EINVAL);
1533 error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1535 if (zc->zc_nvlist_dst != 0) {
1538 if ((err = put_nvlist(zc, config)) != 0)
1542 nvlist_free(config);
1551 zfs_ioc_pool_export(zfs_cmd_t *zc)
1554 boolean_t force = (boolean_t)zc->zc_cookie;
1555 boolean_t hardforce = (boolean_t)zc->zc_guid;
1557 zfs_log_history(zc);
1558 error = spa_export(zc->zc_name, NULL, force, hardforce);
1560 zvol_remove_minors(zc->zc_name);
1565 zfs_ioc_pool_configs(zfs_cmd_t *zc)
1570 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1571 return (SET_ERROR(EEXIST));
1573 error = put_nvlist(zc, configs);
1575 nvlist_free(configs);
1582 * zc_name name of the pool
1585 * zc_cookie real errno
1586 * zc_nvlist_dst config nvlist
1587 * zc_nvlist_dst_size size of config nvlist
1590 zfs_ioc_pool_stats(zfs_cmd_t *zc)
1596 error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1597 sizeof (zc->zc_value));
1599 if (config != NULL) {
1600 ret = put_nvlist(zc, config);
1601 nvlist_free(config);
1604 * The config may be present even if 'error' is non-zero.
1605 * In this case we return success, and preserve the real errno
1608 zc->zc_cookie = error;
1617 * Try to import the given pool, returning pool stats as appropriate so that
1618 * user land knows which devices are available and overall pool health.
1621 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1623 nvlist_t *tryconfig, *config;
1626 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1627 zc->zc_iflags, &tryconfig)) != 0)
1630 config = spa_tryimport(tryconfig);
1632 nvlist_free(tryconfig);
1635 return (SET_ERROR(EINVAL));
1637 error = put_nvlist(zc, config);
1638 nvlist_free(config);
1645 * zc_name name of the pool
1646 * zc_cookie scan func (pool_scan_func_t)
1649 zfs_ioc_pool_scan(zfs_cmd_t *zc)
1654 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1657 if (zc->zc_cookie == POOL_SCAN_NONE)
1658 error = spa_scan_stop(spa);
1660 error = spa_scan(spa, zc->zc_cookie);
1662 spa_close(spa, FTAG);
1668 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1673 error = spa_open(zc->zc_name, &spa, FTAG);
1676 spa_close(spa, FTAG);
1682 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1687 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1690 if (zc->zc_cookie < spa_version(spa) ||
1691 !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1692 spa_close(spa, FTAG);
1693 return (SET_ERROR(EINVAL));
1696 spa_upgrade(spa, zc->zc_cookie);
1697 spa_close(spa, FTAG);
1703 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1710 if ((size = zc->zc_history_len) == 0)
1711 return (SET_ERROR(EINVAL));
1713 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1716 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1717 spa_close(spa, FTAG);
1718 return (SET_ERROR(ENOTSUP));
1721 hist_buf = kmem_alloc(size, KM_SLEEP);
1722 if ((error = spa_history_get(spa, &zc->zc_history_offset,
1723 &zc->zc_history_len, hist_buf)) == 0) {
1724 error = ddi_copyout(hist_buf,
1725 (void *)(uintptr_t)zc->zc_history,
1726 zc->zc_history_len, zc->zc_iflags);
1729 spa_close(spa, FTAG);
1730 kmem_free(hist_buf, size);
1735 zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1740 error = spa_open(zc->zc_name, &spa, FTAG);
1742 error = spa_change_guid(spa);
1743 spa_close(spa, FTAG);
1749 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1751 return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
1756 * zc_name name of filesystem
1757 * zc_obj object to find
1760 * zc_value name of object
1763 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1768 /* XXX reading from objset not owned */
1769 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1771 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1772 dmu_objset_rele(os, FTAG);
1773 return (SET_ERROR(EINVAL));
1775 error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
1776 sizeof (zc->zc_value));
1777 dmu_objset_rele(os, FTAG);
1784 * zc_name name of filesystem
1785 * zc_obj object to find
1788 * zc_stat stats on object
1789 * zc_value path to object
1792 zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1797 /* XXX reading from objset not owned */
1798 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1800 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1801 dmu_objset_rele(os, FTAG);
1802 return (SET_ERROR(EINVAL));
1804 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1805 sizeof (zc->zc_value));
1806 dmu_objset_rele(os, FTAG);
1812 zfs_ioc_vdev_add(zfs_cmd_t *zc)
1816 nvlist_t *config, **l2cache, **spares;
1817 uint_t nl2cache = 0, nspares = 0;
1819 error = spa_open(zc->zc_name, &spa, FTAG);
1823 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1824 zc->zc_iflags, &config);
1825 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1826 &l2cache, &nl2cache);
1828 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1833 * A root pool with concatenated devices is not supported.
1834 * Thus, can not add a device to a root pool.
1836 * Intent log device can not be added to a rootpool because
1837 * during mountroot, zil is replayed, a seperated log device
1838 * can not be accessed during the mountroot time.
1840 * l2cache and spare devices are ok to be added to a rootpool.
1842 if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
1843 nvlist_free(config);
1844 spa_close(spa, FTAG);
1845 return (SET_ERROR(EDOM));
1847 #endif /* illumos */
1850 error = spa_vdev_add(spa, config);
1851 nvlist_free(config);
1853 spa_close(spa, FTAG);
1859 * zc_name name of the pool
1860 * zc_nvlist_conf nvlist of devices to remove
1861 * zc_cookie to stop the remove?
1864 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1869 error = spa_open(zc->zc_name, &spa, FTAG);
1872 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1873 spa_close(spa, FTAG);
1878 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1882 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1884 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1886 switch (zc->zc_cookie) {
1887 case VDEV_STATE_ONLINE:
1888 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1891 case VDEV_STATE_OFFLINE:
1892 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1895 case VDEV_STATE_FAULTED:
1896 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1897 zc->zc_obj != VDEV_AUX_EXTERNAL)
1898 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1900 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
1903 case VDEV_STATE_DEGRADED:
1904 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1905 zc->zc_obj != VDEV_AUX_EXTERNAL)
1906 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1908 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
1912 error = SET_ERROR(EINVAL);
1914 zc->zc_cookie = newstate;
1915 spa_close(spa, FTAG);
1920 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1923 int replacing = zc->zc_cookie;
1927 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1930 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1931 zc->zc_iflags, &config)) == 0) {
1932 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1933 nvlist_free(config);
1936 spa_close(spa, FTAG);
1941 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1946 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1949 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1951 spa_close(spa, FTAG);
1956 zfs_ioc_vdev_split(zfs_cmd_t *zc)
1959 nvlist_t *config, *props = NULL;
1961 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
1963 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1966 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1967 zc->zc_iflags, &config)) {
1968 spa_close(spa, FTAG);
1972 if (zc->zc_nvlist_src_size != 0 && (error =
1973 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1974 zc->zc_iflags, &props))) {
1975 spa_close(spa, FTAG);
1976 nvlist_free(config);
1980 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
1982 spa_close(spa, FTAG);
1984 nvlist_free(config);
1991 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
1994 char *path = zc->zc_value;
1995 uint64_t guid = zc->zc_guid;
1998 error = spa_open(zc->zc_name, &spa, FTAG);
2002 error = spa_vdev_setpath(spa, guid, path);
2003 spa_close(spa, FTAG);
2008 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
2011 char *fru = zc->zc_value;
2012 uint64_t guid = zc->zc_guid;
2015 error = spa_open(zc->zc_name, &spa, FTAG);
2019 error = spa_vdev_setfru(spa, guid, fru);
2020 spa_close(spa, FTAG);
2025 zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
2030 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2032 if (zc->zc_nvlist_dst != 0 &&
2033 (error = dsl_prop_get_all(os, &nv)) == 0) {
2034 dmu_objset_stats(os, nv);
2036 * NB: zvol_get_stats() will read the objset contents,
2037 * which we aren't supposed to do with a
2038 * DS_MODE_USER hold, because it could be
2039 * inconsistent. So this is a bit of a workaround...
2040 * XXX reading with out owning
2042 if (!zc->zc_objset_stats.dds_inconsistent &&
2043 dmu_objset_type(os) == DMU_OST_ZVOL) {
2044 error = zvol_get_stats(os, nv);
2049 error = put_nvlist(zc, nv);
2058 * zc_name name of filesystem
2059 * zc_nvlist_dst_size size of buffer for property nvlist
2062 * zc_objset_stats stats
2063 * zc_nvlist_dst property nvlist
2064 * zc_nvlist_dst_size size of property nvlist
2067 zfs_ioc_objset_stats(zfs_cmd_t *zc)
2072 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2074 error = zfs_ioc_objset_stats_impl(zc, os);
2075 dmu_objset_rele(os, FTAG);
2078 if (error == ENOMEM)
2085 * zc_name name of filesystem
2086 * zc_nvlist_dst_size size of buffer for property nvlist
2089 * zc_nvlist_dst received property nvlist
2090 * zc_nvlist_dst_size size of received property nvlist
2092 * Gets received properties (distinct from local properties on or after
2093 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
2094 * local property values.
2097 zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
2103 * Without this check, we would return local property values if the
2104 * caller has not already received properties on or after
2105 * SPA_VERSION_RECVD_PROPS.
2107 if (!dsl_prop_get_hasrecvd(zc->zc_name))
2108 return (SET_ERROR(ENOTSUP));
2110 if (zc->zc_nvlist_dst != 0 &&
2111 (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
2112 error = put_nvlist(zc, nv);
2120 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2126 * zfs_get_zplprop() will either find a value or give us
2127 * the default value (if there is one).
2129 if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2131 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2137 * zc_name name of filesystem
2138 * zc_nvlist_dst_size size of buffer for zpl property nvlist
2141 * zc_nvlist_dst zpl property nvlist
2142 * zc_nvlist_dst_size size of zpl property nvlist
2145 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2150 /* XXX reading without owning */
2151 if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
2154 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2157 * NB: nvl_add_zplprop() will read the objset contents,
2158 * which we aren't supposed to do with a DS_MODE_USER
2159 * hold, because it could be inconsistent.
2161 if (zc->zc_nvlist_dst != 0 &&
2162 !zc->zc_objset_stats.dds_inconsistent &&
2163 dmu_objset_type(os) == DMU_OST_ZFS) {
2166 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2167 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2168 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2169 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2170 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2171 err = put_nvlist(zc, nv);
2174 err = SET_ERROR(ENOENT);
2176 dmu_objset_rele(os, FTAG);
2181 dataset_name_hidden(const char *name)
2184 * Skip over datasets that are not visible in this zone,
2185 * internal datasets (which have a $ in their name), and
2186 * temporary datasets (which have a % in their name).
2188 if (strchr(name, '$') != NULL)
2190 if (strchr(name, '%') != NULL)
2192 if (!INGLOBALZONE(curthread) && !zone_dataset_visible(name, NULL))
2199 * zc_name name of filesystem
2200 * zc_cookie zap cursor
2201 * zc_nvlist_dst_size size of buffer for property nvlist
2204 * zc_name name of next filesystem
2205 * zc_cookie zap cursor
2206 * zc_objset_stats stats
2207 * zc_nvlist_dst property nvlist
2208 * zc_nvlist_dst_size size of property nvlist
2211 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2216 size_t orig_len = strlen(zc->zc_name);
2219 if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
2220 if (error == ENOENT)
2221 error = SET_ERROR(ESRCH);
2225 p = strrchr(zc->zc_name, '/');
2226 if (p == NULL || p[1] != '\0')
2227 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2228 p = zc->zc_name + strlen(zc->zc_name);
2231 error = dmu_dir_list_next(os,
2232 sizeof (zc->zc_name) - (p - zc->zc_name), p,
2233 NULL, &zc->zc_cookie);
2234 if (error == ENOENT)
2235 error = SET_ERROR(ESRCH);
2236 } while (error == 0 && dataset_name_hidden(zc->zc_name));
2237 dmu_objset_rele(os, FTAG);
2240 * If it's an internal dataset (ie. with a '$' in its name),
2241 * don't try to get stats for it, otherwise we'll return ENOENT.
2243 if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
2244 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2245 if (error == ENOENT) {
2246 /* We lost a race with destroy, get the next one. */
2247 zc->zc_name[orig_len] = '\0';
2256 * zc_name name of filesystem
2257 * zc_cookie zap cursor
2258 * zc_nvlist_dst_size size of buffer for property nvlist
2259 * zc_simple when set, only name is requested
2262 * zc_name name of next snapshot
2263 * zc_objset_stats stats
2264 * zc_nvlist_dst property nvlist
2265 * zc_nvlist_dst_size size of property nvlist
2268 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2273 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2275 return (error == ENOENT ? ESRCH : error);
2279 * A dataset name of maximum length cannot have any snapshots,
2280 * so exit immediately.
2282 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
2283 dmu_objset_rele(os, FTAG);
2284 return (SET_ERROR(ESRCH));
2287 error = dmu_snapshot_list_next(os,
2288 sizeof (zc->zc_name) - strlen(zc->zc_name),
2289 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2292 if (error == 0 && !zc->zc_simple) {
2294 dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2296 error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
2300 error = dmu_objset_from_ds(ds, &ossnap);
2302 error = zfs_ioc_objset_stats_impl(zc, ossnap);
2303 dsl_dataset_rele(ds, FTAG);
2305 } else if (error == ENOENT) {
2306 error = SET_ERROR(ESRCH);
2309 dmu_objset_rele(os, FTAG);
2310 /* if we failed, undo the @ that we tacked on to zc_name */
2312 *strchr(zc->zc_name, '@') = '\0';
2317 zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2319 const char *propname = nvpair_name(pair);
2321 unsigned int vallen;
2324 zfs_userquota_prop_t type;
2330 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2332 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2333 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2335 return (SET_ERROR(EINVAL));
2339 * A correctly constructed propname is encoded as
2340 * userquota@<rid>-<domain>.
2342 if ((dash = strchr(propname, '-')) == NULL ||
2343 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2345 return (SET_ERROR(EINVAL));
2352 err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
2354 err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
2355 zfsvfs_rele(zfsvfs, FTAG);
2362 * If the named property is one that has a special function to set its value,
2363 * return 0 on success and a positive error code on failure; otherwise if it is
2364 * not one of the special properties handled by this function, return -1.
2366 * XXX: It would be better for callers of the property interface if we handled
2367 * these special cases in dsl_prop.c (in the dsl layer).
2370 zfs_prop_set_special(const char *dsname, zprop_source_t source,
2373 const char *propname = nvpair_name(pair);
2374 zfs_prop_t prop = zfs_name_to_prop(propname);
2378 if (prop == ZPROP_INVAL) {
2379 if (zfs_prop_userquota(propname))
2380 return (zfs_prop_set_userquota(dsname, pair));
2384 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2386 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2387 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2391 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
2394 VERIFY(0 == nvpair_value_uint64(pair, &intval));
2397 case ZFS_PROP_QUOTA:
2398 err = dsl_dir_set_quota(dsname, source, intval);
2400 case ZFS_PROP_REFQUOTA:
2401 err = dsl_dataset_set_refquota(dsname, source, intval);
2403 case ZFS_PROP_RESERVATION:
2404 err = dsl_dir_set_reservation(dsname, source, intval);
2406 case ZFS_PROP_REFRESERVATION:
2407 err = dsl_dataset_set_refreservation(dsname, source, intval);
2409 case ZFS_PROP_VOLSIZE:
2410 err = zvol_set_volsize(dsname, ddi_driver_major(zfs_dip),
2413 case ZFS_PROP_VERSION:
2417 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2420 err = zfs_set_version(zfsvfs, intval);
2421 zfsvfs_rele(zfsvfs, FTAG);
2423 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2426 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2427 (void) strcpy(zc->zc_name, dsname);
2428 (void) zfs_ioc_userspace_upgrade(zc);
2429 kmem_free(zc, sizeof (zfs_cmd_t));
2433 case ZFS_PROP_COMPRESSION:
2435 if (intval == ZIO_COMPRESS_LZ4) {
2436 zfeature_info_t *feature =
2437 &spa_feature_table[SPA_FEATURE_LZ4_COMPRESS];
2440 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
2444 * Setting the LZ4 compression algorithm activates
2447 if (!spa_feature_is_active(spa, feature)) {
2448 if ((err = zfs_prop_activate_feature(spa,
2450 spa_close(spa, FTAG);
2455 spa_close(spa, FTAG);
2458 * We still want the default set action to be performed in the
2459 * caller, we only performed zfeature settings here.
2473 * This function is best effort. If it fails to set any of the given properties,
2474 * it continues to set as many as it can and returns the last error
2475 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2476 * with the list of names of all the properties that failed along with the
2477 * corresponding error numbers.
2479 * If every property is set successfully, zero is returned and errlist is not
2483 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2491 nvlist_t *genericnvl = fnvlist_alloc();
2492 nvlist_t *retrynvl = fnvlist_alloc();
2496 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2497 const char *propname = nvpair_name(pair);
2498 zfs_prop_t prop = zfs_name_to_prop(propname);
2501 /* decode the property value */
2503 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2505 attrs = fnvpair_value_nvlist(pair);
2506 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2508 err = SET_ERROR(EINVAL);
2511 /* Validate value type */
2512 if (err == 0 && prop == ZPROP_INVAL) {
2513 if (zfs_prop_user(propname)) {
2514 if (nvpair_type(propval) != DATA_TYPE_STRING)
2515 err = SET_ERROR(EINVAL);
2516 } else if (zfs_prop_userquota(propname)) {
2517 if (nvpair_type(propval) !=
2518 DATA_TYPE_UINT64_ARRAY)
2519 err = SET_ERROR(EINVAL);
2521 err = SET_ERROR(EINVAL);
2523 } else if (err == 0) {
2524 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2525 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2526 err = SET_ERROR(EINVAL);
2527 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2530 intval = fnvpair_value_uint64(propval);
2532 switch (zfs_prop_get_type(prop)) {
2533 case PROP_TYPE_NUMBER:
2535 case PROP_TYPE_STRING:
2536 err = SET_ERROR(EINVAL);
2538 case PROP_TYPE_INDEX:
2539 if (zfs_prop_index_to_string(prop,
2540 intval, &unused) != 0)
2541 err = SET_ERROR(EINVAL);
2545 "unknown property type");
2548 err = SET_ERROR(EINVAL);
2552 /* Validate permissions */
2554 err = zfs_check_settable(dsname, pair, CRED());
2557 err = zfs_prop_set_special(dsname, source, pair);
2560 * For better performance we build up a list of
2561 * properties to set in a single transaction.
2563 err = nvlist_add_nvpair(genericnvl, pair);
2564 } else if (err != 0 && nvl != retrynvl) {
2566 * This may be a spurious error caused by
2567 * receiving quota and reservation out of order.
2568 * Try again in a second pass.
2570 err = nvlist_add_nvpair(retrynvl, pair);
2575 if (errlist != NULL)
2576 fnvlist_add_int32(errlist, propname, err);
2581 if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2586 if (!nvlist_empty(genericnvl) &&
2587 dsl_props_set(dsname, source, genericnvl) != 0) {
2589 * If this fails, we still want to set as many properties as we
2590 * can, so try setting them individually.
2593 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2594 const char *propname = nvpair_name(pair);
2598 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2600 attrs = fnvpair_value_nvlist(pair);
2601 propval = fnvlist_lookup_nvpair(attrs,
2605 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2606 strval = fnvpair_value_string(propval);
2607 err = dsl_prop_set_string(dsname, propname,
2610 intval = fnvpair_value_uint64(propval);
2611 err = dsl_prop_set_int(dsname, propname, source,
2616 if (errlist != NULL) {
2617 fnvlist_add_int32(errlist, propname,
2624 nvlist_free(genericnvl);
2625 nvlist_free(retrynvl);
2631 * Check that all the properties are valid user properties.
2634 zfs_check_userprops(const char *fsname, nvlist_t *nvl)
2636 nvpair_t *pair = NULL;
2639 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2640 const char *propname = nvpair_name(pair);
2643 if (!zfs_prop_user(propname) ||
2644 nvpair_type(pair) != DATA_TYPE_STRING)
2645 return (SET_ERROR(EINVAL));
2647 if (error = zfs_secpolicy_write_perms(fsname,
2648 ZFS_DELEG_PERM_USERPROP, CRED()))
2651 if (strlen(propname) >= ZAP_MAXNAMELEN)
2652 return (SET_ERROR(ENAMETOOLONG));
2654 VERIFY(nvpair_value_string(pair, &valstr) == 0);
2655 if (strlen(valstr) >= ZAP_MAXVALUELEN)
2662 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2666 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2669 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2670 if (nvlist_exists(skipped, nvpair_name(pair)))
2673 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2678 clear_received_props(const char *dsname, nvlist_t *props,
2682 nvlist_t *cleared_props = NULL;
2683 props_skip(props, skipped, &cleared_props);
2684 if (!nvlist_empty(cleared_props)) {
2686 * Acts on local properties until the dataset has received
2687 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2689 zprop_source_t flags = (ZPROP_SRC_NONE |
2690 (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
2691 err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
2693 nvlist_free(cleared_props);
2699 * zc_name name of filesystem
2700 * zc_value name of property to set
2701 * zc_nvlist_src{_size} nvlist of properties to apply
2702 * zc_cookie received properties flag
2705 * zc_nvlist_dst{_size} error for each unapplied received property
2708 zfs_ioc_set_prop(zfs_cmd_t *zc)
2711 boolean_t received = zc->zc_cookie;
2712 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2717 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2718 zc->zc_iflags, &nvl)) != 0)
2722 nvlist_t *origprops;
2724 if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
2725 (void) clear_received_props(zc->zc_name,
2727 nvlist_free(origprops);
2730 error = dsl_prop_set_hasrecvd(zc->zc_name);
2733 errors = fnvlist_alloc();
2735 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
2737 if (zc->zc_nvlist_dst != 0 && errors != NULL) {
2738 (void) put_nvlist(zc, errors);
2741 nvlist_free(errors);
2748 * zc_name name of filesystem
2749 * zc_value name of property to inherit
2750 * zc_cookie revert to received value if TRUE
2755 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2757 const char *propname = zc->zc_value;
2758 zfs_prop_t prop = zfs_name_to_prop(propname);
2759 boolean_t received = zc->zc_cookie;
2760 zprop_source_t source = (received
2761 ? ZPROP_SRC_NONE /* revert to received value, if any */
2762 : ZPROP_SRC_INHERITED); /* explicitly inherit */
2771 * zfs_prop_set_special() expects properties in the form of an
2772 * nvpair with type info.
2774 if (prop == ZPROP_INVAL) {
2775 if (!zfs_prop_user(propname))
2776 return (SET_ERROR(EINVAL));
2778 type = PROP_TYPE_STRING;
2779 } else if (prop == ZFS_PROP_VOLSIZE ||
2780 prop == ZFS_PROP_VERSION) {
2781 return (SET_ERROR(EINVAL));
2783 type = zfs_prop_get_type(prop);
2786 VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2789 case PROP_TYPE_STRING:
2790 VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2792 case PROP_TYPE_NUMBER:
2793 case PROP_TYPE_INDEX:
2794 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2798 return (SET_ERROR(EINVAL));
2801 pair = nvlist_next_nvpair(dummy, NULL);
2802 err = zfs_prop_set_special(zc->zc_name, source, pair);
2805 return (err); /* special property already handled */
2808 * Only check this in the non-received case. We want to allow
2809 * 'inherit -S' to revert non-inheritable properties like quota
2810 * and reservation to the received or default values even though
2811 * they are not considered inheritable.
2813 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2814 return (SET_ERROR(EINVAL));
2817 /* property name has been validated by zfs_secpolicy_inherit_prop() */
2818 return (dsl_prop_inherit(zc->zc_name, zc->zc_value, source));
2822 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2829 if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2830 zc->zc_iflags, &props))
2834 * If the only property is the configfile, then just do a spa_lookup()
2835 * to handle the faulted case.
2837 pair = nvlist_next_nvpair(props, NULL);
2838 if (pair != NULL && strcmp(nvpair_name(pair),
2839 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2840 nvlist_next_nvpair(props, pair) == NULL) {
2841 mutex_enter(&spa_namespace_lock);
2842 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2843 spa_configfile_set(spa, props, B_FALSE);
2844 spa_config_sync(spa, B_FALSE, B_TRUE);
2846 mutex_exit(&spa_namespace_lock);
2853 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2858 error = spa_prop_set(spa, props);
2861 spa_close(spa, FTAG);
2867 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2871 nvlist_t *nvp = NULL;
2873 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2875 * If the pool is faulted, there may be properties we can still
2876 * get (such as altroot and cachefile), so attempt to get them
2879 mutex_enter(&spa_namespace_lock);
2880 if ((spa = spa_lookup(zc->zc_name)) != NULL)
2881 error = spa_prop_get(spa, &nvp);
2882 mutex_exit(&spa_namespace_lock);
2884 error = spa_prop_get(spa, &nvp);
2885 spa_close(spa, FTAG);
2888 if (error == 0 && zc->zc_nvlist_dst != 0)
2889 error = put_nvlist(zc, nvp);
2891 error = SET_ERROR(EFAULT);
2899 * zc_name name of filesystem
2900 * zc_nvlist_src{_size} nvlist of delegated permissions
2901 * zc_perm_action allow/unallow flag
2906 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2909 nvlist_t *fsaclnv = NULL;
2911 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2912 zc->zc_iflags, &fsaclnv)) != 0)
2916 * Verify nvlist is constructed correctly
2918 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2919 nvlist_free(fsaclnv);
2920 return (SET_ERROR(EINVAL));
2924 * If we don't have PRIV_SYS_MOUNT, then validate
2925 * that user is allowed to hand out each permission in
2929 error = secpolicy_zfs(CRED());
2931 if (zc->zc_perm_action == B_FALSE) {
2932 error = dsl_deleg_can_allow(zc->zc_name,
2935 error = dsl_deleg_can_unallow(zc->zc_name,
2941 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2943 nvlist_free(fsaclnv);
2949 * zc_name name of filesystem
2952 * zc_nvlist_src{_size} nvlist of delegated permissions
2955 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
2960 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
2961 error = put_nvlist(zc, nvp);
2969 * Search the vfs list for a specified resource. Returns a pointer to it
2970 * or NULL if no suitable entry is found. The caller of this routine
2971 * is responsible for releasing the returned vfs pointer.
2974 zfs_get_vfs(const char *resource)
2978 mtx_lock(&mountlist_mtx);
2979 TAILQ_FOREACH(vfsp, &mountlist, mnt_list) {
2980 if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
2985 mtx_unlock(&mountlist_mtx);
2991 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2993 zfs_creat_t *zct = arg;
2995 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
2998 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
3002 * os parent objset pointer (NULL if root fs)
3003 * fuids_ok fuids allowed in this version of the spa?
3004 * sa_ok SAs allowed in this version of the spa?
3005 * createprops list of properties requested by creator
3008 * zplprops values for the zplprops we attach to the master node object
3009 * is_ci true if requested file system will be purely case-insensitive
3011 * Determine the settings for utf8only, normalization and
3012 * casesensitivity. Specific values may have been requested by the
3013 * creator and/or we can inherit values from the parent dataset. If
3014 * the file system is of too early a vintage, a creator can not
3015 * request settings for these properties, even if the requested
3016 * setting is the default value. We don't actually want to create dsl
3017 * properties for these, so remove them from the source nvlist after
3021 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
3022 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
3023 nvlist_t *zplprops, boolean_t *is_ci)
3025 uint64_t sense = ZFS_PROP_UNDEFINED;
3026 uint64_t norm = ZFS_PROP_UNDEFINED;
3027 uint64_t u8 = ZFS_PROP_UNDEFINED;
3029 ASSERT(zplprops != NULL);
3032 * Pull out creator prop choices, if any.
3035 (void) nvlist_lookup_uint64(createprops,
3036 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
3037 (void) nvlist_lookup_uint64(createprops,
3038 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
3039 (void) nvlist_remove_all(createprops,
3040 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
3041 (void) nvlist_lookup_uint64(createprops,
3042 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
3043 (void) nvlist_remove_all(createprops,
3044 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
3045 (void) nvlist_lookup_uint64(createprops,
3046 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
3047 (void) nvlist_remove_all(createprops,
3048 zfs_prop_to_name(ZFS_PROP_CASE));
3052 * If the zpl version requested is whacky or the file system
3053 * or pool is version is too "young" to support normalization
3054 * and the creator tried to set a value for one of the props,
3057 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
3058 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
3059 (zplver >= ZPL_VERSION_SA && !sa_ok) ||
3060 (zplver < ZPL_VERSION_NORMALIZATION &&
3061 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
3062 sense != ZFS_PROP_UNDEFINED)))
3063 return (SET_ERROR(ENOTSUP));
3066 * Put the version in the zplprops
3068 VERIFY(nvlist_add_uint64(zplprops,
3069 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3071 if (norm == ZFS_PROP_UNDEFINED)
3072 VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
3073 VERIFY(nvlist_add_uint64(zplprops,
3074 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3077 * If we're normalizing, names must always be valid UTF-8 strings.
3081 if (u8 == ZFS_PROP_UNDEFINED)
3082 VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
3083 VERIFY(nvlist_add_uint64(zplprops,
3084 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3086 if (sense == ZFS_PROP_UNDEFINED)
3087 VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
3088 VERIFY(nvlist_add_uint64(zplprops,
3089 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3092 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
3098 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
3099 nvlist_t *zplprops, boolean_t *is_ci)
3101 boolean_t fuids_ok, sa_ok;
3102 uint64_t zplver = ZPL_VERSION;
3103 objset_t *os = NULL;
3104 char parentname[MAXNAMELEN];
3110 (void) strlcpy(parentname, dataset, sizeof (parentname));
3111 cp = strrchr(parentname, '/');
3115 if ((error = spa_open(dataset, &spa, FTAG)) != 0)
3118 spa_vers = spa_version(spa);
3119 spa_close(spa, FTAG);
3121 zplver = zfs_zpl_version_map(spa_vers);
3122 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3123 sa_ok = (zplver >= ZPL_VERSION_SA);
3126 * Open parent object set so we can inherit zplprop values.
3128 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
3131 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
3133 dmu_objset_rele(os, FTAG);
3138 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
3139 nvlist_t *zplprops, boolean_t *is_ci)
3143 uint64_t zplver = ZPL_VERSION;
3146 zplver = zfs_zpl_version_map(spa_vers);
3147 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3148 sa_ok = (zplver >= ZPL_VERSION_SA);
3150 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
3151 createprops, zplprops, is_ci);
3157 * "type" -> dmu_objset_type_t (int32)
3158 * (optional) "props" -> { prop -> value }
3161 * outnvl: propname -> error code (int32)
3164 zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3167 zfs_creat_t zct = { 0 };
3168 nvlist_t *nvprops = NULL;
3169 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
3171 dmu_objset_type_t type;
3172 boolean_t is_insensitive = B_FALSE;
3174 if (nvlist_lookup_int32(innvl, "type", &type32) != 0)
3175 return (SET_ERROR(EINVAL));
3177 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3181 cbfunc = zfs_create_cb;
3185 cbfunc = zvol_create_cb;
3192 if (strchr(fsname, '@') ||
3193 strchr(fsname, '%'))
3194 return (SET_ERROR(EINVAL));
3196 zct.zct_props = nvprops;
3199 return (SET_ERROR(EINVAL));
3201 if (type == DMU_OST_ZVOL) {
3202 uint64_t volsize, volblocksize;
3204 if (nvprops == NULL)
3205 return (SET_ERROR(EINVAL));
3206 if (nvlist_lookup_uint64(nvprops,
3207 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3208 return (SET_ERROR(EINVAL));
3210 if ((error = nvlist_lookup_uint64(nvprops,
3211 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3212 &volblocksize)) != 0 && error != ENOENT)
3213 return (SET_ERROR(EINVAL));
3216 volblocksize = zfs_prop_default_numeric(
3217 ZFS_PROP_VOLBLOCKSIZE);
3219 if ((error = zvol_check_volblocksize(
3220 volblocksize)) != 0 ||
3221 (error = zvol_check_volsize(volsize,
3222 volblocksize)) != 0)
3224 } else if (type == DMU_OST_ZFS) {
3228 * We have to have normalization and
3229 * case-folding flags correct when we do the
3230 * file system creation, so go figure them out
3233 VERIFY(nvlist_alloc(&zct.zct_zplprops,
3234 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3235 error = zfs_fill_zplprops(fsname, nvprops,
3236 zct.zct_zplprops, &is_insensitive);
3238 nvlist_free(zct.zct_zplprops);
3243 error = dmu_objset_create(fsname, type,
3244 is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
3245 nvlist_free(zct.zct_zplprops);
3248 * It would be nice to do this atomically.
3251 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3254 (void) dsl_destroy_head(fsname);
3257 if (error == 0 && type == DMU_OST_ZVOL)
3258 zvol_create_minors(fsname);
3265 * "origin" -> name of origin snapshot
3266 * (optional) "props" -> { prop -> value }
3269 * outnvl: propname -> error code (int32)
3272 zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3275 nvlist_t *nvprops = NULL;
3278 if (nvlist_lookup_string(innvl, "origin", &origin_name) != 0)
3279 return (SET_ERROR(EINVAL));
3280 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3282 if (strchr(fsname, '@') ||
3283 strchr(fsname, '%'))
3284 return (SET_ERROR(EINVAL));
3286 if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3287 return (SET_ERROR(EINVAL));
3288 error = dmu_objset_clone(fsname, origin_name);
3293 * It would be nice to do this atomically.
3296 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3299 (void) dsl_destroy_head(fsname);
3306 * "snaps" -> { snapshot1, snapshot2 }
3307 * (optional) "props" -> { prop -> value (string) }
3310 * outnvl: snapshot -> error code (int32)
3313 zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3316 nvlist_t *props = NULL;
3320 (void) nvlist_lookup_nvlist(innvl, "props", &props);
3321 if ((error = zfs_check_userprops(poolname, props)) != 0)
3324 if (!nvlist_empty(props) &&
3325 zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3326 return (SET_ERROR(ENOTSUP));
3328 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3329 return (SET_ERROR(EINVAL));
3330 poollen = strlen(poolname);
3331 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3332 pair = nvlist_next_nvpair(snaps, pair)) {
3333 const char *name = nvpair_name(pair);
3334 const char *cp = strchr(name, '@');
3337 * The snap name must contain an @, and the part after it must
3338 * contain only valid characters.
3340 if (cp == NULL || snapshot_namecheck(cp + 1, NULL, NULL) != 0)
3341 return (SET_ERROR(EINVAL));
3344 * The snap must be in the specified pool.
3346 if (strncmp(name, poolname, poollen) != 0 ||
3347 (name[poollen] != '/' && name[poollen] != '@'))
3348 return (SET_ERROR(EXDEV));
3350 /* This must be the only snap of this fs. */
3351 for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
3352 pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
3353 if (strncmp(name, nvpair_name(pair2), cp - name + 1)
3355 return (SET_ERROR(EXDEV));
3360 error = dsl_dataset_snapshot(snaps, props, outnvl);
3365 * innvl: "message" -> string
3369 zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
3377 * The poolname in the ioctl is not set, we get it from the TSD,
3378 * which was set at the end of the last successful ioctl that allows
3379 * logging. The secpolicy func already checked that it is set.
3380 * Only one log ioctl is allowed after each successful ioctl, so
3381 * we clear the TSD here.
3383 poolname = tsd_get(zfs_allow_log_key);
3384 (void) tsd_set(zfs_allow_log_key, NULL);
3385 error = spa_open(poolname, &spa, FTAG);
3390 if (nvlist_lookup_string(innvl, "message", &message) != 0) {
3391 spa_close(spa, FTAG);
3392 return (SET_ERROR(EINVAL));
3395 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
3396 spa_close(spa, FTAG);
3397 return (SET_ERROR(ENOTSUP));
3400 error = spa_history_log(spa, message);
3401 spa_close(spa, FTAG);
3406 * The dp_config_rwlock must not be held when calling this, because the
3407 * unmount may need to write out data.
3409 * This function is best-effort. Callers must deal gracefully if it
3410 * remains mounted (or is remounted after this call).
3412 * Returns 0 if the argument is not a snapshot, or it is not currently a
3413 * filesystem, or we were able to unmount it. Returns error code otherwise.
3416 zfs_unmount_snap(const char *snapname)
3422 if (strchr(snapname, '@') == NULL)
3425 vfsp = zfs_get_vfs(snapname);
3429 zfsvfs = vfsp->vfs_data;
3430 ASSERT(!dsl_pool_config_held(dmu_objset_pool(zfsvfs->z_os)));
3432 err = vn_vfswlock(vfsp->vfs_vnodecovered);
3435 return (SET_ERROR(err));
3438 * Always force the unmount for snapshots.
3442 (void) dounmount(vfsp, MS_FORCE, kcred);
3444 mtx_lock(&Giant); /* dounmount() */
3445 (void) dounmount(vfsp, MS_FORCE, curthread);
3446 mtx_unlock(&Giant); /* dounmount() */
3453 zfs_unmount_snap_cb(const char *snapname, void *arg)
3455 return (zfs_unmount_snap(snapname));
3459 * When a clone is destroyed, its origin may also need to be destroyed,
3460 * in which case it must be unmounted. This routine will do that unmount
3464 zfs_destroy_unmount_origin(const char *fsname)
3470 error = dmu_objset_hold(fsname, FTAG, &os);
3473 ds = dmu_objset_ds(os);
3474 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
3475 char originname[MAXNAMELEN];
3476 dsl_dataset_name(ds->ds_prev, originname);
3477 dmu_objset_rele(os, FTAG);
3478 (void) zfs_unmount_snap(originname);
3480 dmu_objset_rele(os, FTAG);
3486 * "snaps" -> { snapshot1, snapshot2 }
3487 * (optional boolean) "defer"
3490 * outnvl: snapshot -> error code (int32)
3494 zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3501 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3502 return (SET_ERROR(EINVAL));
3503 defer = nvlist_exists(innvl, "defer");
3505 poollen = strlen(poolname);
3506 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3507 pair = nvlist_next_nvpair(snaps, pair)) {
3508 const char *name = nvpair_name(pair);
3511 * The snap must be in the specified pool.
3513 if (strncmp(name, poolname, poollen) != 0 ||
3514 (name[poollen] != '/' && name[poollen] != '@'))
3515 return (SET_ERROR(EXDEV));
3517 error = zfs_unmount_snap(name);
3520 (void) zvol_remove_minor(name);
3523 return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
3528 * zc_name name of dataset to destroy
3529 * zc_objset_type type of objset
3530 * zc_defer_destroy mark for deferred destroy
3535 zfs_ioc_destroy(zfs_cmd_t *zc)
3539 if (zc->zc_objset_type == DMU_OST_ZFS) {
3540 err = zfs_unmount_snap(zc->zc_name);
3545 if (strchr(zc->zc_name, '@'))
3546 err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
3548 err = dsl_destroy_head(zc->zc_name);
3549 if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
3550 (void) zvol_remove_minor(zc->zc_name);
3555 * fsname is name of dataset to rollback (to most recent snapshot)
3557 * innvl is not used.
3559 * outnvl: "target" -> name of most recent snapshot
3564 zfs_ioc_rollback(const char *fsname, nvlist_t *args, nvlist_t *outnvl)
3569 if (getzfsvfs(fsname, &zfsvfs) == 0) {
3570 error = zfs_suspend_fs(zfsvfs);
3574 error = dsl_dataset_rollback(fsname, zfsvfs, outnvl);
3575 resume_err = zfs_resume_fs(zfsvfs, fsname);
3576 error = error ? error : resume_err;
3578 VFS_RELE(zfsvfs->z_vfs);
3580 error = dsl_dataset_rollback(fsname, NULL, outnvl);
3586 recursive_unmount(const char *fsname, void *arg)
3588 const char *snapname = arg;
3589 char fullname[MAXNAMELEN];
3591 (void) snprintf(fullname, sizeof (fullname), "%s@%s", fsname, snapname);
3592 return (zfs_unmount_snap(fullname));
3597 * zc_name old name of dataset
3598 * zc_value new name of dataset
3599 * zc_cookie recursive flag (only valid for snapshots)
3604 zfs_ioc_rename(zfs_cmd_t *zc)
3606 boolean_t recursive = zc->zc_cookie & 1;
3608 boolean_t allow_mounted = zc->zc_cookie & 2;
3612 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3613 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3614 strchr(zc->zc_value, '%'))
3615 return (SET_ERROR(EINVAL));
3617 at = strchr(zc->zc_name, '@');
3619 /* snaps must be in same fs */
3622 if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
3623 return (SET_ERROR(EXDEV));
3626 if (zc->zc_objset_type == DMU_OST_ZFS) {
3628 if (zc->zc_objset_type == DMU_OST_ZFS && allow_mounted) {
3630 error = dmu_objset_find(zc->zc_name,
3631 recursive_unmount, at + 1,
3632 recursive ? DS_FIND_CHILDREN : 0);
3638 error = dsl_dataset_rename_snapshot(zc->zc_name,
3639 at + 1, strchr(zc->zc_value, '@') + 1, recursive);
3645 if (zc->zc_objset_type == DMU_OST_ZVOL)
3646 (void) zvol_remove_minor(zc->zc_name);
3648 return (dsl_dir_rename(zc->zc_name, zc->zc_value));
3653 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
3655 const char *propname = nvpair_name(pair);
3656 boolean_t issnap = (strchr(dsname, '@') != NULL);
3657 zfs_prop_t prop = zfs_name_to_prop(propname);
3661 if (prop == ZPROP_INVAL) {
3662 if (zfs_prop_user(propname)) {
3663 if (err = zfs_secpolicy_write_perms(dsname,
3664 ZFS_DELEG_PERM_USERPROP, cr))
3669 if (!issnap && zfs_prop_userquota(propname)) {
3670 const char *perm = NULL;
3671 const char *uq_prefix =
3672 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
3673 const char *gq_prefix =
3674 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
3676 if (strncmp(propname, uq_prefix,
3677 strlen(uq_prefix)) == 0) {
3678 perm = ZFS_DELEG_PERM_USERQUOTA;
3679 } else if (strncmp(propname, gq_prefix,
3680 strlen(gq_prefix)) == 0) {
3681 perm = ZFS_DELEG_PERM_GROUPQUOTA;
3683 /* USERUSED and GROUPUSED are read-only */
3684 return (SET_ERROR(EINVAL));
3687 if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
3692 return (SET_ERROR(EINVAL));
3696 return (SET_ERROR(EINVAL));
3698 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
3700 * dsl_prop_get_all_impl() returns properties in this
3704 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
3705 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3710 * Check that this value is valid for this pool version
3713 case ZFS_PROP_COMPRESSION:
3715 * If the user specified gzip compression, make sure
3716 * the SPA supports it. We ignore any errors here since
3717 * we'll catch them later.
3719 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3720 nvpair_value_uint64(pair, &intval) == 0) {
3721 if (intval >= ZIO_COMPRESS_GZIP_1 &&
3722 intval <= ZIO_COMPRESS_GZIP_9 &&
3723 zfs_earlier_version(dsname,
3724 SPA_VERSION_GZIP_COMPRESSION)) {
3725 return (SET_ERROR(ENOTSUP));
3728 if (intval == ZIO_COMPRESS_ZLE &&
3729 zfs_earlier_version(dsname,
3730 SPA_VERSION_ZLE_COMPRESSION))
3731 return (SET_ERROR(ENOTSUP));
3733 if (intval == ZIO_COMPRESS_LZ4) {
3734 zfeature_info_t *feature =
3736 SPA_FEATURE_LZ4_COMPRESS];
3739 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3742 if (!spa_feature_is_enabled(spa, feature)) {
3743 spa_close(spa, FTAG);
3744 return (SET_ERROR(ENOTSUP));
3746 spa_close(spa, FTAG);
3750 * If this is a bootable dataset then
3751 * verify that the compression algorithm
3752 * is supported for booting. We must return
3753 * something other than ENOTSUP since it
3754 * implies a downrev pool version.
3756 if (zfs_is_bootfs(dsname) &&
3757 !BOOTFS_COMPRESS_VALID(intval)) {
3758 return (SET_ERROR(ERANGE));
3763 case ZFS_PROP_COPIES:
3764 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
3765 return (SET_ERROR(ENOTSUP));
3768 case ZFS_PROP_DEDUP:
3769 if (zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
3770 return (SET_ERROR(ENOTSUP));
3773 case ZFS_PROP_SHARESMB:
3774 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
3775 return (SET_ERROR(ENOTSUP));
3778 case ZFS_PROP_ACLINHERIT:
3779 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3780 nvpair_value_uint64(pair, &intval) == 0) {
3781 if (intval == ZFS_ACL_PASSTHROUGH_X &&
3782 zfs_earlier_version(dsname,
3783 SPA_VERSION_PASSTHROUGH_X))
3784 return (SET_ERROR(ENOTSUP));
3789 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
3793 * Checks for a race condition to make sure we don't increment a feature flag
3797 zfs_prop_activate_feature_check(void *arg, dmu_tx_t *tx)
3799 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
3800 zfeature_info_t *feature = arg;
3802 if (!spa_feature_is_active(spa, feature))
3805 return (SET_ERROR(EBUSY));
3809 * The callback invoked on feature activation in the sync task caused by
3810 * zfs_prop_activate_feature.
3813 zfs_prop_activate_feature_sync(void *arg, dmu_tx_t *tx)
3815 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
3816 zfeature_info_t *feature = arg;
3818 spa_feature_incr(spa, feature, tx);
3822 * Activates a feature on a pool in response to a property setting. This
3823 * creates a new sync task which modifies the pool to reflect the feature
3827 zfs_prop_activate_feature(spa_t *spa, zfeature_info_t *feature)
3831 /* EBUSY here indicates that the feature is already active */
3832 err = dsl_sync_task(spa_name(spa),
3833 zfs_prop_activate_feature_check, zfs_prop_activate_feature_sync,
3836 if (err != 0 && err != EBUSY)
3843 * Removes properties from the given props list that fail permission checks
3844 * needed to clear them and to restore them in case of a receive error. For each
3845 * property, make sure we have both set and inherit permissions.
3847 * Returns the first error encountered if any permission checks fail. If the
3848 * caller provides a non-NULL errlist, it also gives the complete list of names
3849 * of all the properties that failed a permission check along with the
3850 * corresponding error numbers. The caller is responsible for freeing the
3853 * If every property checks out successfully, zero is returned and the list
3854 * pointed at by errlist is NULL.
3857 zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
3860 nvpair_t *pair, *next_pair;
3867 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3869 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
3870 (void) strcpy(zc->zc_name, dataset);
3871 pair = nvlist_next_nvpair(props, NULL);
3872 while (pair != NULL) {
3873 next_pair = nvlist_next_nvpair(props, pair);
3875 (void) strcpy(zc->zc_value, nvpair_name(pair));
3876 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
3877 (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
3878 VERIFY(nvlist_remove_nvpair(props, pair) == 0);
3879 VERIFY(nvlist_add_int32(errors,
3880 zc->zc_value, err) == 0);
3884 kmem_free(zc, sizeof (zfs_cmd_t));
3886 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
3887 nvlist_free(errors);
3890 VERIFY(nvpair_value_int32(pair, &rv) == 0);
3893 if (errlist == NULL)
3894 nvlist_free(errors);
3902 propval_equals(nvpair_t *p1, nvpair_t *p2)
3904 if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
3905 /* dsl_prop_get_all_impl() format */
3907 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
3908 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3912 if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
3914 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
3915 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3919 if (nvpair_type(p1) != nvpair_type(p2))
3922 if (nvpair_type(p1) == DATA_TYPE_STRING) {
3923 char *valstr1, *valstr2;
3925 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
3926 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
3927 return (strcmp(valstr1, valstr2) == 0);
3929 uint64_t intval1, intval2;
3931 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
3932 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
3933 return (intval1 == intval2);
3938 * Remove properties from props if they are not going to change (as determined
3939 * by comparison with origprops). Remove them from origprops as well, since we
3940 * do not need to clear or restore properties that won't change.
3943 props_reduce(nvlist_t *props, nvlist_t *origprops)
3945 nvpair_t *pair, *next_pair;
3947 if (origprops == NULL)
3948 return; /* all props need to be received */
3950 pair = nvlist_next_nvpair(props, NULL);
3951 while (pair != NULL) {
3952 const char *propname = nvpair_name(pair);
3955 next_pair = nvlist_next_nvpair(props, pair);
3957 if ((nvlist_lookup_nvpair(origprops, propname,
3958 &match) != 0) || !propval_equals(pair, match))
3959 goto next; /* need to set received value */
3961 /* don't clear the existing received value */
3962 (void) nvlist_remove_nvpair(origprops, match);
3963 /* don't bother receiving the property */
3964 (void) nvlist_remove_nvpair(props, pair);
3971 static boolean_t zfs_ioc_recv_inject_err;
3976 * zc_name name of containing filesystem
3977 * zc_nvlist_src{_size} nvlist of properties to apply
3978 * zc_value name of snapshot to create
3979 * zc_string name of clone origin (if DRR_FLAG_CLONE)
3980 * zc_cookie file descriptor to recv from
3981 * zc_begin_record the BEGIN record of the stream (not byteswapped)
3982 * zc_guid force flag
3983 * zc_cleanup_fd cleanup-on-exit file descriptor
3984 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
3987 * zc_cookie number of bytes read
3988 * zc_nvlist_dst{_size} error for each unapplied received property
3989 * zc_obj zprop_errflags_t
3990 * zc_action_handle handle for this guid/ds mapping
3993 zfs_ioc_recv(zfs_cmd_t *zc)
3996 dmu_recv_cookie_t drc;
3997 boolean_t force = (boolean_t)zc->zc_guid;
4000 int props_error = 0;
4003 nvlist_t *props = NULL; /* sent properties */
4004 nvlist_t *origprops = NULL; /* existing properties */
4005 char *origin = NULL;
4007 char tofs[ZFS_MAXNAMELEN];
4008 cap_rights_t rights;
4009 boolean_t first_recvd_props = B_FALSE;
4011 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4012 strchr(zc->zc_value, '@') == NULL ||
4013 strchr(zc->zc_value, '%'))
4014 return (SET_ERROR(EINVAL));
4016 (void) strcpy(tofs, zc->zc_value);
4017 tosnap = strchr(tofs, '@');
4020 if (zc->zc_nvlist_src != 0 &&
4021 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
4022 zc->zc_iflags, &props)) != 0)
4026 fp = getf(fd, cap_rights_init(&rights, CAP_PREAD));
4029 return (SET_ERROR(EBADF));
4032 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4034 if (zc->zc_string[0])
4035 origin = zc->zc_string;
4037 error = dmu_recv_begin(tofs, tosnap,
4038 &zc->zc_begin_record, force, origin, &drc);
4043 * Set properties before we receive the stream so that they are applied
4044 * to the new data. Note that we must call dmu_recv_stream() if
4045 * dmu_recv_begin() succeeds.
4047 if (props != NULL && !drc.drc_newfs) {
4048 if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
4049 SPA_VERSION_RECVD_PROPS &&
4050 !dsl_prop_get_hasrecvd(tofs))
4051 first_recvd_props = B_TRUE;
4054 * If new received properties are supplied, they are to
4055 * completely replace the existing received properties, so stash
4056 * away the existing ones.
4058 if (dsl_prop_get_received(tofs, &origprops) == 0) {
4059 nvlist_t *errlist = NULL;
4061 * Don't bother writing a property if its value won't
4062 * change (and avoid the unnecessary security checks).
4064 * The first receive after SPA_VERSION_RECVD_PROPS is a
4065 * special case where we blow away all local properties
4068 if (!first_recvd_props)
4069 props_reduce(props, origprops);
4070 if (zfs_check_clearable(tofs, origprops, &errlist) != 0)
4071 (void) nvlist_merge(errors, errlist, 0);
4072 nvlist_free(errlist);
4074 if (clear_received_props(tofs, origprops,
4075 first_recvd_props ? NULL : props) != 0)
4076 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4078 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4082 if (props != NULL) {
4083 props_error = dsl_prop_set_hasrecvd(tofs);
4085 if (props_error == 0) {
4086 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4091 if (zc->zc_nvlist_dst_size != 0 &&
4092 (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
4093 put_nvlist(zc, errors) != 0)) {
4095 * Caller made zc->zc_nvlist_dst less than the minimum expected
4096 * size or supplied an invalid address.
4098 props_error = SET_ERROR(EINVAL);
4102 error = dmu_recv_stream(&drc, fp, &off, zc->zc_cleanup_fd,
4103 &zc->zc_action_handle);
4106 zfsvfs_t *zfsvfs = NULL;
4108 if (getzfsvfs(tofs, &zfsvfs) == 0) {
4112 error = zfs_suspend_fs(zfsvfs);
4114 * If the suspend fails, then the recv_end will
4115 * likely also fail, and clean up after itself.
4117 end_err = dmu_recv_end(&drc, zfsvfs);
4119 error = zfs_resume_fs(zfsvfs, tofs);
4120 error = error ? error : end_err;
4121 VFS_RELE(zfsvfs->z_vfs);
4123 error = dmu_recv_end(&drc, NULL);
4127 zc->zc_cookie = off - fp->f_offset;
4128 if (off >= 0 && off <= MAXOFFSET_T)
4132 if (zfs_ioc_recv_inject_err) {
4133 zfs_ioc_recv_inject_err = B_FALSE;
4140 zvol_create_minors(tofs);
4144 * On error, restore the original props.
4146 if (error != 0 && props != NULL && !drc.drc_newfs) {
4147 if (clear_received_props(tofs, props, NULL) != 0) {
4149 * We failed to clear the received properties.
4150 * Since we may have left a $recvd value on the
4151 * system, we can't clear the $hasrecvd flag.
4153 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4154 } else if (first_recvd_props) {
4155 dsl_prop_unset_hasrecvd(tofs);
4158 if (origprops == NULL && !drc.drc_newfs) {
4159 /* We failed to stash the original properties. */
4160 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4164 * dsl_props_set() will not convert RECEIVED to LOCAL on or
4165 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
4166 * explictly if we're restoring local properties cleared in the
4167 * first new-style receive.
4169 if (origprops != NULL &&
4170 zfs_set_prop_nvlist(tofs, (first_recvd_props ?
4171 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
4172 origprops, NULL) != 0) {
4174 * We stashed the original properties but failed to
4177 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4182 nvlist_free(origprops);
4183 nvlist_free(errors);
4187 error = props_error;
4194 * zc_name name of snapshot to send
4195 * zc_cookie file descriptor to send stream to
4196 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
4197 * zc_sendobj objsetid of snapshot to send
4198 * zc_fromobj objsetid of incremental fromsnap (may be zero)
4199 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
4200 * output size in zc_objset_type.
4205 zfs_ioc_send(zfs_cmd_t *zc)
4209 boolean_t estimate = (zc->zc_guid != 0);
4211 if (zc->zc_obj != 0) {
4213 dsl_dataset_t *tosnap;
4215 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4219 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
4221 dsl_pool_rele(dp, FTAG);
4225 if (dsl_dir_is_clone(tosnap->ds_dir))
4226 zc->zc_fromobj = tosnap->ds_dir->dd_phys->dd_origin_obj;
4227 dsl_dataset_rele(tosnap, FTAG);
4228 dsl_pool_rele(dp, FTAG);
4233 dsl_dataset_t *tosnap;
4234 dsl_dataset_t *fromsnap = NULL;
4236 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4240 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
4242 dsl_pool_rele(dp, FTAG);
4246 if (zc->zc_fromobj != 0) {
4247 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
4250 dsl_dataset_rele(tosnap, FTAG);
4251 dsl_pool_rele(dp, FTAG);
4256 error = dmu_send_estimate(tosnap, fromsnap,
4257 &zc->zc_objset_type);
4259 if (fromsnap != NULL)
4260 dsl_dataset_rele(fromsnap, FTAG);
4261 dsl_dataset_rele(tosnap, FTAG);
4262 dsl_pool_rele(dp, FTAG);
4265 cap_rights_t rights;
4267 fp = getf(zc->zc_cookie,
4268 cap_rights_init(&rights, CAP_WRITE));
4270 return (SET_ERROR(EBADF));
4273 error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
4275 zc->zc_fromobj, zc->zc_cookie, fp->f_vnode, &off);
4277 zc->zc_fromobj, zc->zc_cookie, fp, &off);
4280 if (off >= 0 && off <= MAXOFFSET_T)
4282 releasef(zc->zc_cookie);
4289 * zc_name name of snapshot on which to report progress
4290 * zc_cookie file descriptor of send stream
4293 * zc_cookie number of bytes written in send stream thus far
4296 zfs_ioc_send_progress(zfs_cmd_t *zc)
4300 dmu_sendarg_t *dsp = NULL;
4303 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4307 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
4309 dsl_pool_rele(dp, FTAG);
4313 mutex_enter(&ds->ds_sendstream_lock);
4316 * Iterate over all the send streams currently active on this dataset.
4317 * If there's one which matches the specified file descriptor _and_ the
4318 * stream was started by the current process, return the progress of
4321 for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
4322 dsp = list_next(&ds->ds_sendstreams, dsp)) {
4323 if (dsp->dsa_outfd == zc->zc_cookie &&
4324 dsp->dsa_proc == curproc)
4329 zc->zc_cookie = *(dsp->dsa_off);
4331 error = SET_ERROR(ENOENT);
4333 mutex_exit(&ds->ds_sendstream_lock);
4334 dsl_dataset_rele(ds, FTAG);
4335 dsl_pool_rele(dp, FTAG);
4340 zfs_ioc_inject_fault(zfs_cmd_t *zc)
4344 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
4345 &zc->zc_inject_record);
4348 zc->zc_guid = (uint64_t)id;
4354 zfs_ioc_clear_fault(zfs_cmd_t *zc)
4356 return (zio_clear_fault((int)zc->zc_guid));
4360 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
4362 int id = (int)zc->zc_guid;
4365 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
4366 &zc->zc_inject_record);
4374 zfs_ioc_error_log(zfs_cmd_t *zc)
4378 size_t count = (size_t)zc->zc_nvlist_dst_size;
4380 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
4383 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
4386 zc->zc_nvlist_dst_size = count;
4388 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
4390 spa_close(spa, FTAG);
4396 zfs_ioc_clear(zfs_cmd_t *zc)
4403 * On zpool clear we also fix up missing slogs
4405 mutex_enter(&spa_namespace_lock);
4406 spa = spa_lookup(zc->zc_name);
4408 mutex_exit(&spa_namespace_lock);
4409 return (SET_ERROR(EIO));
4411 if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
4412 /* we need to let spa_open/spa_load clear the chains */
4413 spa_set_log_state(spa, SPA_LOG_CLEAR);
4415 spa->spa_last_open_failed = 0;
4416 mutex_exit(&spa_namespace_lock);
4418 if (zc->zc_cookie & ZPOOL_NO_REWIND) {
4419 error = spa_open(zc->zc_name, &spa, FTAG);
4422 nvlist_t *config = NULL;
4424 if (zc->zc_nvlist_src == 0)
4425 return (SET_ERROR(EINVAL));
4427 if ((error = get_nvlist(zc->zc_nvlist_src,
4428 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
4429 error = spa_open_rewind(zc->zc_name, &spa, FTAG,
4431 if (config != NULL) {
4434 if ((err = put_nvlist(zc, config)) != 0)
4436 nvlist_free(config);
4438 nvlist_free(policy);
4445 spa_vdev_state_enter(spa, SCL_NONE);
4447 if (zc->zc_guid == 0) {
4450 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
4452 (void) spa_vdev_state_exit(spa, NULL, ENODEV);
4453 spa_close(spa, FTAG);
4454 return (SET_ERROR(ENODEV));
4458 vdev_clear(spa, vd);
4460 (void) spa_vdev_state_exit(spa, NULL, 0);
4463 * Resume any suspended I/Os.
4465 if (zio_resume(spa) != 0)
4466 error = SET_ERROR(EIO);
4468 spa_close(spa, FTAG);
4474 zfs_ioc_pool_reopen(zfs_cmd_t *zc)
4479 error = spa_open(zc->zc_name, &spa, FTAG);
4483 spa_vdev_state_enter(spa, SCL_NONE);
4486 * If a resilver is already in progress then set the
4487 * spa_scrub_reopen flag to B_TRUE so that we don't restart
4488 * the scan as a side effect of the reopen. Otherwise, let
4489 * vdev_open() decided if a resilver is required.
4491 spa->spa_scrub_reopen = dsl_scan_resilvering(spa->spa_dsl_pool);
4492 vdev_reopen(spa->spa_root_vdev);
4493 spa->spa_scrub_reopen = B_FALSE;
4495 (void) spa_vdev_state_exit(spa, NULL, 0);
4496 spa_close(spa, FTAG);
4501 * zc_name name of filesystem
4502 * zc_value name of origin snapshot
4505 * zc_string name of conflicting snapshot, if there is one
4508 zfs_ioc_promote(zfs_cmd_t *zc)
4513 * We don't need to unmount *all* the origin fs's snapshots, but
4516 cp = strchr(zc->zc_value, '@');
4519 (void) dmu_objset_find(zc->zc_value,
4520 zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
4521 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
4525 * Retrieve a single {user|group}{used|quota}@... property.
4528 * zc_name name of filesystem
4529 * zc_objset_type zfs_userquota_prop_t
4530 * zc_value domain name (eg. "S-1-234-567-89")
4531 * zc_guid RID/UID/GID
4534 * zc_cookie property value
4537 zfs_ioc_userspace_one(zfs_cmd_t *zc)
4542 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
4543 return (SET_ERROR(EINVAL));
4545 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4549 error = zfs_userspace_one(zfsvfs,
4550 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
4551 zfsvfs_rele(zfsvfs, FTAG);
4558 * zc_name name of filesystem
4559 * zc_cookie zap cursor
4560 * zc_objset_type zfs_userquota_prop_t
4561 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
4564 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
4565 * zc_cookie zap cursor
4568 zfs_ioc_userspace_many(zfs_cmd_t *zc)
4571 int bufsize = zc->zc_nvlist_dst_size;
4574 return (SET_ERROR(ENOMEM));
4576 int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4580 void *buf = kmem_alloc(bufsize, KM_SLEEP);
4582 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
4583 buf, &zc->zc_nvlist_dst_size);
4586 error = ddi_copyout(buf,
4587 (void *)(uintptr_t)zc->zc_nvlist_dst,
4588 zc->zc_nvlist_dst_size, zc->zc_iflags);
4590 kmem_free(buf, bufsize);
4591 zfsvfs_rele(zfsvfs, FTAG);
4598 * zc_name name of filesystem
4604 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
4610 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
4611 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
4613 * If userused is not enabled, it may be because the
4614 * objset needs to be closed & reopened (to grow the
4615 * objset_phys_t). Suspend/resume the fs will do that.
4617 error = zfs_suspend_fs(zfsvfs);
4619 dmu_objset_refresh_ownership(zfsvfs->z_os,
4621 error = zfs_resume_fs(zfsvfs, zc->zc_name);
4625 error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
4626 VFS_RELE(zfsvfs->z_vfs);
4628 /* XXX kind of reading contents without owning */
4629 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4633 error = dmu_objset_userspace_upgrade(os);
4634 dmu_objset_rele(os, FTAG);
4642 * We don't want to have a hard dependency
4643 * against some special symbols in sharefs
4644 * nfs, and smbsrv. Determine them if needed when
4645 * the first file system is shared.
4646 * Neither sharefs, nfs or smbsrv are unloadable modules.
4648 int (*znfsexport_fs)(void *arg);
4649 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
4650 int (*zsmbexport_fs)(void *arg, boolean_t add_share);
4652 int zfs_nfsshare_inited;
4653 int zfs_smbshare_inited;
4655 ddi_modhandle_t nfs_mod;
4656 ddi_modhandle_t sharefs_mod;
4657 ddi_modhandle_t smbsrv_mod;
4659 kmutex_t zfs_share_lock;
4667 ASSERT(MUTEX_HELD(&zfs_share_lock));
4668 /* Both NFS and SMB shares also require sharetab support. */
4669 if (sharefs_mod == NULL && ((sharefs_mod =
4670 ddi_modopen("fs/sharefs",
4671 KRTLD_MODE_FIRST, &error)) == NULL)) {
4672 return (SET_ERROR(ENOSYS));
4674 if (zshare_fs == NULL && ((zshare_fs =
4675 (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
4676 ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
4677 return (SET_ERROR(ENOSYS));
4684 zfs_ioc_share(zfs_cmd_t *zc)
4690 switch (zc->zc_share.z_sharetype) {
4692 case ZFS_UNSHARE_NFS:
4693 if (zfs_nfsshare_inited == 0) {
4694 mutex_enter(&zfs_share_lock);
4695 if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
4696 KRTLD_MODE_FIRST, &error)) == NULL)) {
4697 mutex_exit(&zfs_share_lock);
4698 return (SET_ERROR(ENOSYS));
4700 if (znfsexport_fs == NULL &&
4701 ((znfsexport_fs = (int (*)(void *))
4703 "nfs_export", &error)) == NULL)) {
4704 mutex_exit(&zfs_share_lock);
4705 return (SET_ERROR(ENOSYS));
4707 error = zfs_init_sharefs();
4709 mutex_exit(&zfs_share_lock);
4710 return (SET_ERROR(ENOSYS));
4712 zfs_nfsshare_inited = 1;
4713 mutex_exit(&zfs_share_lock);
4717 case ZFS_UNSHARE_SMB:
4718 if (zfs_smbshare_inited == 0) {
4719 mutex_enter(&zfs_share_lock);
4720 if (smbsrv_mod == NULL && ((smbsrv_mod =
4721 ddi_modopen("drv/smbsrv",
4722 KRTLD_MODE_FIRST, &error)) == NULL)) {
4723 mutex_exit(&zfs_share_lock);
4724 return (SET_ERROR(ENOSYS));
4726 if (zsmbexport_fs == NULL && ((zsmbexport_fs =
4727 (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
4728 "smb_server_share", &error)) == NULL)) {
4729 mutex_exit(&zfs_share_lock);
4730 return (SET_ERROR(ENOSYS));
4732 error = zfs_init_sharefs();
4734 mutex_exit(&zfs_share_lock);
4735 return (SET_ERROR(ENOSYS));
4737 zfs_smbshare_inited = 1;
4738 mutex_exit(&zfs_share_lock);
4742 return (SET_ERROR(EINVAL));
4745 switch (zc->zc_share.z_sharetype) {
4747 case ZFS_UNSHARE_NFS:
4749 znfsexport_fs((void *)
4750 (uintptr_t)zc->zc_share.z_exportdata))
4754 case ZFS_UNSHARE_SMB:
4755 if (error = zsmbexport_fs((void *)
4756 (uintptr_t)zc->zc_share.z_exportdata,
4757 zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
4764 opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
4765 zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
4766 SHAREFS_ADD : SHAREFS_REMOVE;
4769 * Add or remove share from sharetab
4771 error = zshare_fs(opcode,
4772 (void *)(uintptr_t)zc->zc_share.z_sharedata,
4773 zc->zc_share.z_sharemax);
4782 ace_t full_access[] = {
4783 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
4788 * zc_name name of containing filesystem
4789 * zc_obj object # beyond which we want next in-use object #
4792 * zc_obj next in-use object #
4795 zfs_ioc_next_obj(zfs_cmd_t *zc)
4797 objset_t *os = NULL;
4800 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4804 error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
4805 os->os_dsl_dataset->ds_phys->ds_prev_snap_txg);
4807 dmu_objset_rele(os, FTAG);
4813 * zc_name name of filesystem
4814 * zc_value prefix name for snapshot
4815 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
4818 * zc_value short name of new snapshot
4821 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
4828 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
4832 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
4833 (u_longlong_t)ddi_get_lbolt64());
4834 hold_name = kmem_asprintf("%%%s", zc->zc_value);
4836 error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
4839 (void) strcpy(zc->zc_value, snap_name);
4842 zfs_onexit_fd_rele(zc->zc_cleanup_fd);
4848 * zc_name name of "to" snapshot
4849 * zc_value name of "from" snapshot
4850 * zc_cookie file descriptor to write diff data on
4853 * dmu_diff_record_t's to the file descriptor
4856 zfs_ioc_diff(zfs_cmd_t *zc)
4859 cap_rights_t rights;
4863 fp = getf(zc->zc_cookie, cap_rights_init(&rights, CAP_WRITE));
4865 return (SET_ERROR(EBADF));
4870 error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off);
4872 error = dmu_diff(zc->zc_name, zc->zc_value, fp, &off);
4875 if (off >= 0 && off <= MAXOFFSET_T)
4877 releasef(zc->zc_cookie);
4884 * Remove all ACL files in shares dir
4887 zfs_smb_acl_purge(znode_t *dzp)
4890 zap_attribute_t zap;
4891 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4894 for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
4895 (error = zap_cursor_retrieve(&zc, &zap)) == 0;
4896 zap_cursor_advance(&zc)) {
4897 if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
4901 zap_cursor_fini(&zc);
4907 zfs_ioc_smb_acl(zfs_cmd_t *zc)
4912 vnode_t *resourcevp = NULL;
4921 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
4922 NO_FOLLOW, NULL, &vp)) != 0)
4925 /* Now make sure mntpnt and dataset are ZFS */
4927 if (strcmp(vp->v_vfsp->mnt_stat.f_fstypename, "zfs") != 0 ||
4928 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
4929 zc->zc_name) != 0)) {
4931 return (SET_ERROR(EINVAL));
4935 zfsvfs = dzp->z_zfsvfs;
4939 * Create share dir if its missing.
4941 mutex_enter(&zfsvfs->z_lock);
4942 if (zfsvfs->z_shares_dir == 0) {
4945 tx = dmu_tx_create(zfsvfs->z_os);
4946 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
4948 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
4949 error = dmu_tx_assign(tx, TXG_WAIT);
4953 error = zfs_create_share_dir(zfsvfs, tx);
4957 mutex_exit(&zfsvfs->z_lock);
4963 mutex_exit(&zfsvfs->z_lock);
4965 ASSERT(zfsvfs->z_shares_dir);
4966 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
4972 switch (zc->zc_cookie) {
4973 case ZFS_SMB_ACL_ADD:
4974 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
4975 vattr.va_type = VREG;
4976 vattr.va_mode = S_IFREG|0777;
4980 vsec.vsa_mask = VSA_ACE;
4981 vsec.vsa_aclentp = &full_access;
4982 vsec.vsa_aclentsz = sizeof (full_access);
4983 vsec.vsa_aclcnt = 1;
4985 error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
4986 &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
4988 VN_RELE(resourcevp);
4991 case ZFS_SMB_ACL_REMOVE:
4992 error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
4996 case ZFS_SMB_ACL_RENAME:
4997 if ((error = get_nvlist(zc->zc_nvlist_src,
4998 zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
5003 if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
5004 nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
5007 VN_RELE(ZTOV(sharedir));
5009 nvlist_free(nvlist);
5012 error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
5014 nvlist_free(nvlist);
5017 case ZFS_SMB_ACL_PURGE:
5018 error = zfs_smb_acl_purge(sharedir);
5022 error = SET_ERROR(EINVAL);
5027 VN_RELE(ZTOV(sharedir));
5033 return (EOPNOTSUPP);
5039 * "holds" -> { snapname -> holdname (string), ... }
5040 * (optional) "cleanup_fd" -> fd (int32)
5044 * snapname -> error value (int32)
5050 zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
5053 int cleanup_fd = -1;
5057 error = nvlist_lookup_nvlist(args, "holds", &holds);
5059 return (SET_ERROR(EINVAL));
5061 if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
5062 error = zfs_onexit_fd_hold(cleanup_fd, &minor);
5067 error = dsl_dataset_user_hold(holds, minor, errlist);
5069 zfs_onexit_fd_rele(cleanup_fd);
5074 * innvl is not used.
5077 * holdname -> time added (uint64 seconds since epoch)
5083 zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
5085 return (dsl_dataset_get_holds(snapname, outnvl));
5090 * snapname -> { holdname, ... }
5095 * snapname -> error value (int32)
5101 zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
5103 return (dsl_dataset_user_release(holds, errlist));
5108 * zc_name name of new filesystem or snapshot
5109 * zc_value full name of old snapshot
5112 * zc_cookie space in bytes
5113 * zc_objset_type compressed space in bytes
5114 * zc_perm_action uncompressed space in bytes
5117 zfs_ioc_space_written(zfs_cmd_t *zc)
5121 dsl_dataset_t *new, *old;
5123 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5126 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
5128 dsl_pool_rele(dp, FTAG);
5131 error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
5133 dsl_dataset_rele(new, FTAG);
5134 dsl_pool_rele(dp, FTAG);
5138 error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
5139 &zc->zc_objset_type, &zc->zc_perm_action);
5140 dsl_dataset_rele(old, FTAG);
5141 dsl_dataset_rele(new, FTAG);
5142 dsl_pool_rele(dp, FTAG);
5148 * "firstsnap" -> snapshot name
5152 * "used" -> space in bytes
5153 * "compressed" -> compressed space in bytes
5154 * "uncompressed" -> uncompressed space in bytes
5158 zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
5162 dsl_dataset_t *new, *old;
5164 uint64_t used, comp, uncomp;
5166 if (nvlist_lookup_string(innvl, "firstsnap", &firstsnap) != 0)
5167 return (SET_ERROR(EINVAL));
5169 error = dsl_pool_hold(lastsnap, FTAG, &dp);
5173 error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
5175 dsl_pool_rele(dp, FTAG);
5178 error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
5180 dsl_dataset_rele(new, FTAG);
5181 dsl_pool_rele(dp, FTAG);
5185 error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
5186 dsl_dataset_rele(old, FTAG);
5187 dsl_dataset_rele(new, FTAG);
5188 dsl_pool_rele(dp, FTAG);
5189 fnvlist_add_uint64(outnvl, "used", used);
5190 fnvlist_add_uint64(outnvl, "compressed", comp);
5191 fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
5196 zfs_ioc_jail(zfs_cmd_t *zc)
5199 return (zone_dataset_attach(curthread->td_ucred, zc->zc_name,
5200 (int)zc->zc_jailid));
5204 zfs_ioc_unjail(zfs_cmd_t *zc)
5207 return (zone_dataset_detach(curthread->td_ucred, zc->zc_name,
5208 (int)zc->zc_jailid));
5213 * "fd" -> file descriptor to write stream to (int32)
5214 * (optional) "fromsnap" -> full snap name to send an incremental from
5221 zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5223 cap_rights_t rights;
5226 char *fromname = NULL;
5229 error = nvlist_lookup_int32(innvl, "fd", &fd);
5231 return (SET_ERROR(EINVAL));
5233 (void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
5235 file_t *fp = getf(fd, cap_rights_init(&rights, CAP_READ));
5237 return (SET_ERROR(EBADF));
5241 error = dmu_send(snapname, fromname, fd, fp->f_vnode, &off);
5243 error = dmu_send(snapname, fromname, fd, fp, &off);
5247 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5258 * Determine approximately how large a zfs send stream will be -- the number
5259 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
5262 * (optional) "fromsnap" -> full snap name to send an incremental from
5266 * "space" -> bytes of space (uint64)
5270 zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5273 dsl_dataset_t *fromsnap = NULL;
5274 dsl_dataset_t *tosnap;
5279 error = dsl_pool_hold(snapname, FTAG, &dp);
5283 error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
5285 dsl_pool_rele(dp, FTAG);
5289 error = nvlist_lookup_string(innvl, "fromsnap", &fromname);
5291 error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
5293 dsl_dataset_rele(tosnap, FTAG);
5294 dsl_pool_rele(dp, FTAG);
5299 error = dmu_send_estimate(tosnap, fromsnap, &space);
5300 fnvlist_add_uint64(outnvl, "space", space);
5302 if (fromsnap != NULL)
5303 dsl_dataset_rele(fromsnap, FTAG);
5304 dsl_dataset_rele(tosnap, FTAG);
5305 dsl_pool_rele(dp, FTAG);
5310 static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
5313 zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5314 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
5315 boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
5317 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
5319 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
5320 ASSERT3U(ioc, <, ZFS_IOC_LAST);
5321 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
5322 ASSERT3P(vec->zvec_func, ==, NULL);
5324 vec->zvec_legacy_func = func;
5325 vec->zvec_secpolicy = secpolicy;
5326 vec->zvec_namecheck = namecheck;
5327 vec->zvec_allow_log = log_history;
5328 vec->zvec_pool_check = pool_check;
5332 * See the block comment at the beginning of this file for details on
5333 * each argument to this function.
5336 zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
5337 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
5338 zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
5339 boolean_t allow_log)
5341 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
5343 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
5344 ASSERT3U(ioc, <, ZFS_IOC_LAST);
5345 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
5346 ASSERT3P(vec->zvec_func, ==, NULL);
5348 /* if we are logging, the name must be valid */
5349 ASSERT(!allow_log || namecheck != NO_NAME);
5351 vec->zvec_name = name;
5352 vec->zvec_func = func;
5353 vec->zvec_secpolicy = secpolicy;
5354 vec->zvec_namecheck = namecheck;
5355 vec->zvec_pool_check = pool_check;
5356 vec->zvec_smush_outnvlist = smush_outnvlist;
5357 vec->zvec_allow_log = allow_log;
5361 zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5362 zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
5363 zfs_ioc_poolcheck_t pool_check)
5365 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5366 POOL_NAME, log_history, pool_check);
5370 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5371 zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
5373 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5374 DATASET_NAME, B_FALSE, pool_check);
5378 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
5380 zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
5381 POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5385 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5386 zfs_secpolicy_func_t *secpolicy)
5388 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5389 NO_NAME, B_FALSE, POOL_CHECK_NONE);
5393 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
5394 zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
5396 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5397 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
5401 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
5403 zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
5404 zfs_secpolicy_read);
5408 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5409 zfs_secpolicy_func_t *secpolicy)
5411 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5412 DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5416 zfs_ioctl_init(void)
5418 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
5419 zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
5420 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5422 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
5423 zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
5424 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE);
5426 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
5427 zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
5428 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5430 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
5431 zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
5432 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5434 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
5435 zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
5436 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5438 zfs_ioctl_register("create", ZFS_IOC_CREATE,
5439 zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
5440 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5442 zfs_ioctl_register("clone", ZFS_IOC_CLONE,
5443 zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
5444 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5446 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
5447 zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
5448 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5450 zfs_ioctl_register("hold", ZFS_IOC_HOLD,
5451 zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
5452 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5453 zfs_ioctl_register("release", ZFS_IOC_RELEASE,
5454 zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
5455 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5457 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
5458 zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
5459 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5461 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
5462 zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
5463 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
5465 /* IOCTLS that use the legacy function signature */
5467 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
5468 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
5470 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
5471 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5472 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
5474 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
5475 zfs_ioc_pool_upgrade);
5476 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
5478 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
5479 zfs_ioc_vdev_remove);
5480 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
5481 zfs_ioc_vdev_set_state);
5482 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
5483 zfs_ioc_vdev_attach);
5484 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
5485 zfs_ioc_vdev_detach);
5486 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
5487 zfs_ioc_vdev_setpath);
5488 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
5489 zfs_ioc_vdev_setfru);
5490 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
5491 zfs_ioc_pool_set_props);
5492 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
5493 zfs_ioc_vdev_split);
5494 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
5495 zfs_ioc_pool_reguid);
5497 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
5498 zfs_ioc_pool_configs, zfs_secpolicy_none);
5499 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
5500 zfs_ioc_pool_tryimport, zfs_secpolicy_config);
5501 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
5502 zfs_ioc_inject_fault, zfs_secpolicy_inject);
5503 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
5504 zfs_ioc_clear_fault, zfs_secpolicy_inject);
5505 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
5506 zfs_ioc_inject_list_next, zfs_secpolicy_inject);
5509 * pool destroy, and export don't log the history as part of
5510 * zfsdev_ioctl, but rather zfs_ioc_pool_export
5511 * does the logging of those commands.
5513 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
5514 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
5515 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
5516 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
5518 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
5519 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
5520 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
5521 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
5523 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
5524 zfs_secpolicy_inject, B_FALSE, POOL_CHECK_NONE);
5525 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
5526 zfs_ioc_dsobj_to_dsname,
5527 zfs_secpolicy_diff, B_FALSE, POOL_CHECK_NONE);
5528 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
5529 zfs_ioc_pool_get_history,
5530 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
5532 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
5533 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5535 zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
5536 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5537 zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
5538 zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
5540 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
5541 zfs_ioc_space_written);
5542 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
5543 zfs_ioc_objset_recvd_props);
5544 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
5546 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
5548 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
5549 zfs_ioc_objset_stats);
5550 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
5551 zfs_ioc_objset_zplprops);
5552 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
5553 zfs_ioc_dataset_list_next);
5554 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
5555 zfs_ioc_snapshot_list_next);
5556 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
5557 zfs_ioc_send_progress);
5559 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
5560 zfs_ioc_diff, zfs_secpolicy_diff);
5561 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
5562 zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
5563 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
5564 zfs_ioc_obj_to_path, zfs_secpolicy_diff);
5565 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
5566 zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
5567 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
5568 zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
5569 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
5570 zfs_ioc_send, zfs_secpolicy_send);
5572 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
5573 zfs_secpolicy_none);
5574 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
5575 zfs_secpolicy_destroy);
5576 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
5577 zfs_secpolicy_rename);
5578 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
5579 zfs_secpolicy_recv);
5580 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
5581 zfs_secpolicy_promote);
5582 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
5583 zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
5584 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
5585 zfs_secpolicy_set_fsacl);
5587 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
5588 zfs_secpolicy_share, POOL_CHECK_NONE);
5589 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
5590 zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
5591 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
5592 zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
5593 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5594 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
5595 zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
5596 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5599 zfs_ioctl_register_dataset_nolog(ZFS_IOC_JAIL, zfs_ioc_jail,
5600 zfs_secpolicy_config, POOL_CHECK_NONE);
5601 zfs_ioctl_register_dataset_nolog(ZFS_IOC_UNJAIL, zfs_ioc_unjail,
5602 zfs_secpolicy_config, POOL_CHECK_NONE);
5607 pool_status_check(const char *name, zfs_ioc_namecheck_t type,
5608 zfs_ioc_poolcheck_t check)
5613 ASSERT(type == POOL_NAME || type == DATASET_NAME);
5615 if (check & POOL_CHECK_NONE)
5618 error = spa_open(name, &spa, FTAG);
5620 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
5621 error = SET_ERROR(EAGAIN);
5622 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
5623 error = SET_ERROR(EROFS);
5624 spa_close(spa, FTAG);
5630 * Find a free minor number.
5633 zfsdev_minor_alloc(void)
5635 static minor_t last_minor;
5638 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5640 for (m = last_minor + 1; m != last_minor; m++) {
5641 if (m > ZFSDEV_MAX_MINOR)
5643 if (ddi_get_soft_state(zfsdev_state, m) == NULL) {
5653 zfs_ctldev_init(struct cdev *devp)
5656 zfs_soft_state_t *zs;
5658 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5660 minor = zfsdev_minor_alloc();
5662 return (SET_ERROR(ENXIO));
5664 if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS)
5665 return (SET_ERROR(EAGAIN));
5667 devfs_set_cdevpriv((void *)(uintptr_t)minor, zfsdev_close);
5669 zs = ddi_get_soft_state(zfsdev_state, minor);
5670 zs->zss_type = ZSST_CTLDEV;
5671 zfs_onexit_init((zfs_onexit_t **)&zs->zss_data);
5677 zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor)
5679 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5681 zfs_onexit_destroy(zo);
5682 ddi_soft_state_free(zfsdev_state, minor);
5686 zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which)
5688 zfs_soft_state_t *zp;
5690 zp = ddi_get_soft_state(zfsdev_state, minor);
5691 if (zp == NULL || zp->zss_type != which)
5694 return (zp->zss_data);
5698 zfsdev_open(struct cdev *devp, int flag, int mode, struct thread *td)
5703 if (getminor(*devp) != 0)
5704 return (zvol_open(devp, flag, otyp, cr));
5707 /* This is the control device. Allocate a new minor if requested. */
5709 mutex_enter(&spa_namespace_lock);
5710 error = zfs_ctldev_init(devp);
5711 mutex_exit(&spa_namespace_lock);
5718 zfsdev_close(void *data)
5721 minor_t minor = (minor_t)(uintptr_t)data;
5726 mutex_enter(&spa_namespace_lock);
5727 zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV);
5729 mutex_exit(&spa_namespace_lock);
5732 zfs_ctldev_destroy(zo, minor);
5733 mutex_exit(&spa_namespace_lock);
5737 zfsdev_ioctl(struct cdev *dev, u_long zcmd, caddr_t arg, int flag,
5744 minor_t minor = getminor(dev);
5746 zfs_iocparm_t *zc_iocparm;
5747 int cflag, cmd, oldvecnum;
5748 boolean_t newioc, compat;
5749 cred_t *cr = td->td_ucred;
5751 const zfs_ioc_vec_t *vec;
5752 char *saved_poolname = NULL;
5753 nvlist_t *innvl = NULL;
5755 cflag = ZFS_CMD_COMPAT_NONE;
5759 len = IOCPARM_LEN(zcmd);
5763 * Check if we are talking to supported older binaries
5764 * and translate zfs_cmd if necessary
5766 if (len != sizeof(zfs_iocparm_t)) {
5768 if (len == sizeof(zfs_cmd_t)) {
5769 cflag = ZFS_CMD_COMPAT_LZC;
5771 } else if (len == sizeof(zfs_cmd_deadman_t)) {
5772 cflag = ZFS_CMD_COMPAT_DEADMAN;
5775 } else if (len == sizeof(zfs_cmd_v28_t)) {
5776 cflag = ZFS_CMD_COMPAT_V28;
5779 } else if (len == sizeof(zfs_cmd_v15_t)) {
5780 cflag = ZFS_CMD_COMPAT_V15;
5782 vecnum = zfs_ioctl_v15_to_v28[cmd];
5789 vecnum = cmd - ZFS_IOC_FIRST;
5790 ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
5794 if (vecnum == ZFS_IOC_COMPAT_PASS)
5796 else if (vecnum == ZFS_IOC_COMPAT_FAIL)
5801 * Check if we have sufficient kernel memory allocated
5802 * for the zfs_cmd_t request. Bail out if not so we
5803 * will not access undefined memory region.
5805 if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
5806 return (SET_ERROR(EINVAL));
5807 vec = &zfs_ioc_vec[vecnum];
5810 zc = kmem_zalloc(sizeof(zfs_cmd_t), KM_SLEEP);
5811 bzero(zc, sizeof(zfs_cmd_t));
5813 error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
5815 error = SET_ERROR(EFAULT);
5818 #else /* !illumos */
5820 * We don't alloc/free zc only if talking to library ioctl version 2
5822 if (cflag != ZFS_CMD_COMPAT_LZC) {
5823 zc = kmem_zalloc(sizeof(zfs_cmd_t), KM_SLEEP);
5824 bzero(zc, sizeof(zfs_cmd_t));
5831 zc_iocparm = (void *)arg;
5832 if (zc_iocparm->zfs_cmd_size != sizeof(zfs_cmd_t)) {
5833 error = SET_ERROR(EFAULT);
5836 error = ddi_copyin((void *)(uintptr_t)zc_iocparm->zfs_cmd, zc,
5837 sizeof(zfs_cmd_t), flag);
5839 error = SET_ERROR(EFAULT);
5845 zfs_cmd_compat_get(zc, arg, cflag);
5847 error = zfs_ioctl_compat_pre(zc, &vecnum, cflag);
5850 if (oldvecnum != vecnum)
5851 vec = &zfs_ioc_vec[vecnum];
5853 #endif /* !illumos */
5855 zc->zc_iflags = flag & FKIOCTL;
5856 if (zc->zc_nvlist_src_size != 0) {
5857 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
5858 zc->zc_iflags, &innvl);
5863 /* rewrite innvl for backwards compatibility */
5865 innvl = zfs_ioctl_compat_innvl(zc, innvl, vecnum, cflag);
5868 * Ensure that all pool/dataset names are valid before we pass down to
5871 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
5872 switch (vec->zvec_namecheck) {
5874 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
5875 error = SET_ERROR(EINVAL);
5877 error = pool_status_check(zc->zc_name,
5878 vec->zvec_namecheck, vec->zvec_pool_check);
5882 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
5883 error = SET_ERROR(EINVAL);
5885 error = pool_status_check(zc->zc_name,
5886 vec->zvec_namecheck, vec->zvec_pool_check);
5893 if (error == 0 && !(flag & FKIOCTL))
5894 error = vec->zvec_secpolicy(zc, innvl, cr);
5899 /* legacy ioctls can modify zc_name */
5900 len = strcspn(zc->zc_name, "/@") + 1;
5901 saved_poolname = kmem_alloc(len, KM_SLEEP);
5902 (void) strlcpy(saved_poolname, zc->zc_name, len);
5904 if (vec->zvec_func != NULL) {
5908 nvlist_t *lognv = NULL;
5910 ASSERT(vec->zvec_legacy_func == NULL);
5913 * Add the innvl to the lognv before calling the func,
5914 * in case the func changes the innvl.
5916 if (vec->zvec_allow_log) {
5917 lognv = fnvlist_alloc();
5918 fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
5920 if (!nvlist_empty(innvl)) {
5921 fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
5926 outnvl = fnvlist_alloc();
5927 error = vec->zvec_func(zc->zc_name, innvl, outnvl);
5929 if (error == 0 && vec->zvec_allow_log &&
5930 spa_open(zc->zc_name, &spa, FTAG) == 0) {
5931 if (!nvlist_empty(outnvl)) {
5932 fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL,
5935 (void) spa_history_log_nvl(spa, lognv);
5936 spa_close(spa, FTAG);
5938 fnvlist_free(lognv);
5940 /* rewrite outnvl for backwards compatibility */
5941 if (cflag != ZFS_CMD_COMPAT_NONE && cflag != ZFS_CMD_COMPAT_LZC)
5942 outnvl = zfs_ioctl_compat_outnvl(zc, outnvl, vecnum,
5945 if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
5947 if (vec->zvec_smush_outnvlist) {
5948 smusherror = nvlist_smush(outnvl,
5949 zc->zc_nvlist_dst_size);
5951 if (smusherror == 0)
5952 puterror = put_nvlist(zc, outnvl);
5958 nvlist_free(outnvl);
5960 error = vec->zvec_legacy_func(zc);
5967 zfs_ioctl_compat_post(zc, cmd, cflag);
5968 zfs_cmd_compat_put(zc, arg, vecnum, cflag);
5972 rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
5973 if (error == 0 && rc != 0)
5974 error = SET_ERROR(EFAULT);
5977 rc = ddi_copyout(zc, (void *)(uintptr_t)zc_iocparm->zfs_cmd,
5978 sizeof (zfs_cmd_t), flag);
5979 if (error == 0 && rc != 0)
5980 error = SET_ERROR(EFAULT);
5983 if (error == 0 && vec->zvec_allow_log) {
5984 char *s = tsd_get(zfs_allow_log_key);
5987 (void) tsd_set(zfs_allow_log_key, saved_poolname);
5989 if (saved_poolname != NULL)
5990 strfree(saved_poolname);
5994 kmem_free(zc, sizeof (zfs_cmd_t));
5997 * We don't alloc/free zc only if talking to library ioctl version 2
5999 if (cflag != ZFS_CMD_COMPAT_LZC)
6000 kmem_free(zc, sizeof (zfs_cmd_t));
6007 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
6009 if (cmd != DDI_ATTACH)
6010 return (DDI_FAILURE);
6012 if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
6013 DDI_PSEUDO, 0) == DDI_FAILURE)
6014 return (DDI_FAILURE);
6018 ddi_report_dev(dip);
6020 return (DDI_SUCCESS);
6024 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
6026 if (spa_busy() || zfs_busy() || zvol_busy())
6027 return (DDI_FAILURE);
6029 if (cmd != DDI_DETACH)
6030 return (DDI_FAILURE);
6034 ddi_prop_remove_all(dip);
6035 ddi_remove_minor_node(dip, NULL);
6037 return (DDI_SUCCESS);
6042 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
6045 case DDI_INFO_DEVT2DEVINFO:
6047 return (DDI_SUCCESS);
6049 case DDI_INFO_DEVT2INSTANCE:
6050 *result = (void *)0;
6051 return (DDI_SUCCESS);
6054 return (DDI_FAILURE);
6059 * OK, so this is a little weird.
6061 * /dev/zfs is the control node, i.e. minor 0.
6062 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
6064 * /dev/zfs has basically nothing to do except serve up ioctls,
6065 * so most of the standard driver entry points are in zvol.c.
6068 static struct cb_ops zfs_cb_ops = {
6069 zfsdev_open, /* open */
6070 zfsdev_close, /* close */
6071 zvol_strategy, /* strategy */
6073 zvol_dump, /* dump */
6074 zvol_read, /* read */
6075 zvol_write, /* write */
6076 zfsdev_ioctl, /* ioctl */
6080 nochpoll, /* poll */
6081 ddi_prop_op, /* prop_op */
6082 NULL, /* streamtab */
6083 D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */
6084 CB_REV, /* version */
6085 nodev, /* async read */
6086 nodev, /* async write */
6089 static struct dev_ops zfs_dev_ops = {
6090 DEVO_REV, /* version */
6092 zfs_info, /* info */
6093 nulldev, /* identify */
6094 nulldev, /* probe */
6095 zfs_attach, /* attach */
6096 zfs_detach, /* detach */
6098 &zfs_cb_ops, /* driver operations */
6099 NULL, /* no bus operations */
6101 ddi_quiesce_not_needed, /* quiesce */
6104 static struct modldrv zfs_modldrv = {
6110 static struct modlinkage modlinkage = {
6112 (void *)&zfs_modlfs,
6113 (void *)&zfs_modldrv,
6118 static struct cdevsw zfs_cdevsw = {
6119 .d_version = D_VERSION,
6120 .d_open = zfsdev_open,
6121 .d_ioctl = zfsdev_ioctl,
6122 .d_name = ZFS_DEV_NAME
6126 zfs_allow_log_destroy(void *arg)
6128 char *poolname = arg;
6135 zfsdev = make_dev(&zfs_cdevsw, 0x0, UID_ROOT, GID_OPERATOR, 0666,
6143 destroy_dev(zfsdev);
6146 static struct root_hold_token *zfs_root_token;
6147 struct proc *zfsproc;
6155 spa_init(FREAD | FWRITE);
6160 if ((error = mod_install(&modlinkage)) != 0) {
6167 tsd_create(&zfs_fsyncer_key, NULL);
6168 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
6169 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
6171 error = ldi_ident_from_mod(&modlinkage, &zfs_li);
6173 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
6183 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
6184 return (SET_ERROR(EBUSY));
6186 if ((error = mod_remove(&modlinkage)) != 0)
6192 if (zfs_nfsshare_inited)
6193 (void) ddi_modclose(nfs_mod);
6194 if (zfs_smbshare_inited)
6195 (void) ddi_modclose(smbsrv_mod);
6196 if (zfs_nfsshare_inited || zfs_smbshare_inited)
6197 (void) ddi_modclose(sharefs_mod);
6199 tsd_destroy(&zfs_fsyncer_key);
6200 ldi_ident_release(zfs_li);
6202 mutex_destroy(&zfs_share_lock);
6208 _info(struct modinfo *modinfop)
6210 return (mod_info(&modlinkage, modinfop));
6214 static int zfs__init(void);
6215 static int zfs__fini(void);
6216 static void zfs_shutdown(void *, int);
6218 static eventhandler_tag zfs_shutdown_event_tag;
6224 zfs_root_token = root_mount_hold("ZFS");
6226 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
6228 spa_init(FREAD | FWRITE);
6233 tsd_create(&zfs_fsyncer_key, NULL);
6234 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
6235 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
6237 printf("ZFS storage pool version: features support (" SPA_VERSION_STRING ")\n");
6238 root_mount_rel(zfs_root_token);
6248 if (spa_busy() || zfs_busy() || zvol_busy() ||
6249 zio_injection_enabled) {
6258 tsd_destroy(&zfs_fsyncer_key);
6259 tsd_destroy(&rrw_tsd_key);
6260 tsd_destroy(&zfs_allow_log_key);
6262 mutex_destroy(&zfs_share_lock);
6268 zfs_shutdown(void *arg __unused, int howto __unused)
6272 * ZFS fini routines can not properly work in a panic-ed system.
6274 if (panicstr == NULL)
6280 zfs_modevent(module_t mod, int type, void *unused __unused)
6288 zfs_shutdown_event_tag = EVENTHANDLER_REGISTER(
6289 shutdown_post_sync, zfs_shutdown, NULL,
6290 SHUTDOWN_PRI_FIRST);
6294 if (err == 0 && zfs_shutdown_event_tag != NULL)
6295 EVENTHANDLER_DEREGISTER(shutdown_post_sync,
6296 zfs_shutdown_event_tag);
6303 return (EOPNOTSUPP);
6306 static moduledata_t zfs_mod = {
6311 DECLARE_MODULE(zfsctrl, zfs_mod, SI_SUB_VFS, SI_ORDER_ANY);
6312 MODULE_VERSION(zfsctrl, 1);
6313 MODULE_DEPEND(zfsctrl, opensolaris, 1, 1, 1);
6314 MODULE_DEPEND(zfsctrl, krpc, 1, 1, 1);
6315 MODULE_DEPEND(zfsctrl, acl_nfs4, 1, 1, 1);