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.
36 * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
37 * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
39 * There are two ways that we handle ioctls: the legacy way where almost
40 * all of the logic is in the ioctl callback, and the new way where most
41 * of the marshalling is handled in the common entry point, zfsdev_ioctl().
43 * Non-legacy ioctls should be registered by calling
44 * zfs_ioctl_register() from zfs_ioctl_init(). The ioctl is invoked
45 * from userland by lzc_ioctl().
47 * The registration arguments are as follows:
50 * The name of the ioctl. This is used for history logging. If the
51 * ioctl returns successfully (the callback returns 0), and allow_log
52 * is true, then a history log entry will be recorded with the input &
53 * output nvlists. The log entry can be printed with "zpool history -i".
56 * The ioctl request number, which userland will pass to ioctl(2).
57 * The ioctl numbers can change from release to release, because
58 * the caller (libzfs) must be matched to the kernel.
60 * zfs_secpolicy_func_t *secpolicy
61 * This function will be called before the zfs_ioc_func_t, to
62 * determine if this operation is permitted. It should return EPERM
63 * on failure, and 0 on success. Checks include determining if the
64 * dataset is visible in this zone, and if the user has either all
65 * zfs privileges in the zone (SYS_MOUNT), or has been granted permission
66 * to do this operation on this dataset with "zfs allow".
68 * zfs_ioc_namecheck_t namecheck
69 * This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
70 * name, a dataset name, or nothing. If the name is not well-formed,
71 * the ioctl will fail and the callback will not be called.
72 * Therefore, the callback can assume that the name is well-formed
73 * (e.g. is null-terminated, doesn't have more than one '@' character,
74 * doesn't have invalid characters).
76 * zfs_ioc_poolcheck_t pool_check
77 * This specifies requirements on the pool state. If the pool does
78 * not meet them (is suspended or is readonly), the ioctl will fail
79 * and the callback will not be called. If any checks are specified
80 * (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
81 * Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
82 * POOL_CHECK_READONLY).
84 * boolean_t smush_outnvlist
85 * If smush_outnvlist is true, then the output is presumed to be a
86 * list of errors, and it will be "smushed" down to fit into the
87 * caller's buffer, by removing some entries and replacing them with a
88 * single "N_MORE_ERRORS" entry indicating how many were removed. See
89 * nvlist_smush() for details. If smush_outnvlist is false, and the
90 * outnvlist does not fit into the userland-provided buffer, then the
91 * ioctl will fail with ENOMEM.
93 * zfs_ioc_func_t *func
94 * The callback function that will perform the operation.
96 * The callback should return 0 on success, or an error number on
97 * failure. If the function fails, the userland ioctl will return -1,
98 * and errno will be set to the callback's return value. The callback
99 * will be called with the following arguments:
102 * The name of the pool or dataset to operate on, from
103 * zfs_cmd_t:zc_name. The 'namecheck' argument specifies the
104 * expected type (pool, dataset, or none).
107 * The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src. Or
108 * NULL if no input nvlist was provided. Changes to this nvlist are
109 * ignored. If the input nvlist could not be deserialized, the
110 * ioctl will fail and the callback will not be called.
113 * The output nvlist, initially empty. The callback can fill it in,
114 * and it will be returned to userland by serializing it into
115 * zfs_cmd_t:zc_nvlist_dst. If it is non-empty, and serialization
116 * fails (e.g. because the caller didn't supply a large enough
117 * buffer), then the overall ioctl will fail. See the
118 * 'smush_nvlist' argument above for additional behaviors.
120 * There are two typical uses of the output nvlist:
121 * - To return state, e.g. property values. In this case,
122 * smush_outnvlist should be false. If the buffer was not large
123 * enough, the caller will reallocate a larger buffer and try
126 * - To return multiple errors from an ioctl which makes on-disk
127 * changes. In this case, smush_outnvlist should be true.
128 * Ioctls which make on-disk modifications should generally not
129 * use the outnvl if they succeed, because the caller can not
130 * distinguish between the operation failing, and
131 * deserialization failing.
134 #include <sys/types.h>
135 #include <sys/param.h>
136 #include <sys/systm.h>
137 #include <sys/conf.h>
138 #include <sys/kernel.h>
139 #include <sys/lock.h>
140 #include <sys/malloc.h>
141 #include <sys/mutex.h>
142 #include <sys/proc.h>
143 #include <sys/errno.h>
146 #include <sys/file.h>
147 #include <sys/kmem.h>
148 #include <sys/conf.h>
149 #include <sys/cmn_err.h>
150 #include <sys/stat.h>
151 #include <sys/zfs_ioctl.h>
152 #include <sys/zfs_vfsops.h>
153 #include <sys/zfs_znode.h>
156 #include <sys/spa_impl.h>
157 #include <sys/vdev.h>
159 #include <sys/dsl_dir.h>
160 #include <sys/dsl_dataset.h>
161 #include <sys/dsl_prop.h>
162 #include <sys/dsl_deleg.h>
163 #include <sys/dmu_objset.h>
164 #include <sys/dmu_impl.h>
165 #include <sys/dmu_tx.h>
166 #include <sys/sunddi.h>
167 #include <sys/policy.h>
168 #include <sys/zone.h>
169 #include <sys/nvpair.h>
170 #include <sys/mount.h>
171 #include <sys/taskqueue.h>
173 #include <sys/varargs.h>
174 #include <sys/fs/zfs.h>
175 #include <sys/zfs_ctldir.h>
176 #include <sys/zfs_dir.h>
177 #include <sys/zfs_onexit.h>
178 #include <sys/zvol.h>
179 #include <sys/dsl_scan.h>
180 #include <sys/dmu_objset.h>
181 #include <sys/dmu_send.h>
182 #include <sys/dsl_destroy.h>
183 #include <sys/dsl_userhold.h>
184 #include <sys/zfeature.h>
186 #include "zfs_namecheck.h"
187 #include "zfs_prop.h"
188 #include "zfs_deleg.h"
189 #include "zfs_comutil.h"
190 #include "zfs_ioctl_compat.h"
192 CTASSERT(sizeof(zfs_cmd_t) < IOCPARM_MAX);
194 static int snapshot_list_prefetch;
195 SYSCTL_DECL(_vfs_zfs);
196 TUNABLE_INT("vfs.zfs.snapshot_list_prefetch", &snapshot_list_prefetch);
197 SYSCTL_INT(_vfs_zfs, OID_AUTO, snapshot_list_prefetch, CTLFLAG_RW,
198 &snapshot_list_prefetch, 0, "Prefetch data when listing snapshots");
200 static struct cdev *zfsdev;
202 extern void zfs_init(void);
203 extern void zfs_fini(void);
205 uint_t zfs_fsyncer_key;
206 extern uint_t rrw_tsd_key;
207 static uint_t zfs_allow_log_key;
209 typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *);
210 typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *);
211 typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *);
217 } zfs_ioc_namecheck_t;
220 POOL_CHECK_NONE = 1 << 0,
221 POOL_CHECK_SUSPENDED = 1 << 1,
222 POOL_CHECK_READONLY = 1 << 2,
223 } zfs_ioc_poolcheck_t;
225 typedef struct zfs_ioc_vec {
226 zfs_ioc_legacy_func_t *zvec_legacy_func;
227 zfs_ioc_func_t *zvec_func;
228 zfs_secpolicy_func_t *zvec_secpolicy;
229 zfs_ioc_namecheck_t zvec_namecheck;
230 boolean_t zvec_allow_log;
231 zfs_ioc_poolcheck_t zvec_pool_check;
232 boolean_t zvec_smush_outnvlist;
233 const char *zvec_name;
236 /* This array is indexed by zfs_userquota_prop_t */
237 static const char *userquota_perms[] = {
238 ZFS_DELEG_PERM_USERUSED,
239 ZFS_DELEG_PERM_USERQUOTA,
240 ZFS_DELEG_PERM_GROUPUSED,
241 ZFS_DELEG_PERM_GROUPQUOTA,
244 static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
245 static int zfs_check_settable(const char *name, nvpair_t *property,
247 static int zfs_check_clearable(char *dataset, nvlist_t *props,
249 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
251 int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
252 static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
254 static void zfsdev_close(void *data);
256 static int zfs_prop_activate_feature(spa_t *spa, zfeature_info_t *feature);
258 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
260 __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
267 * Get rid of annoying "../common/" prefix to filename.
269 newfile = strrchr(file, '/');
270 if (newfile != NULL) {
271 newfile = newfile + 1; /* Get rid of leading / */
277 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
281 * To get this data, use the zfs-dprintf probe as so:
282 * dtrace -q -n 'zfs-dprintf \
283 * /stringof(arg0) == "dbuf.c"/ \
284 * {printf("%s: %s", stringof(arg1), stringof(arg3))}'
286 * arg1 = function name
290 DTRACE_PROBE4(zfs__dprintf,
291 char *, newfile, char *, func, int, line, char *, buf);
295 history_str_free(char *buf)
297 kmem_free(buf, HIS_MAX_RECORD_LEN);
301 history_str_get(zfs_cmd_t *zc)
305 if (zc->zc_history == 0)
308 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
309 if (copyinstr((void *)(uintptr_t)zc->zc_history,
310 buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
311 history_str_free(buf);
315 buf[HIS_MAX_RECORD_LEN -1] = '\0';
321 * Check to see if the named dataset is currently defined as bootable
324 zfs_is_bootfs(const char *name)
328 if (dmu_objset_hold(name, FTAG, &os) == 0) {
330 ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
331 dmu_objset_rele(os, FTAG);
338 * zfs_earlier_version
340 * Return non-zero if the spa version is less than requested version.
343 zfs_earlier_version(const char *name, int version)
347 if (spa_open(name, &spa, FTAG) == 0) {
348 if (spa_version(spa) < version) {
349 spa_close(spa, FTAG);
352 spa_close(spa, FTAG);
358 * zpl_earlier_version
360 * Return TRUE if the ZPL version is less than requested version.
363 zpl_earlier_version(const char *name, int version)
366 boolean_t rc = B_TRUE;
368 if (dmu_objset_hold(name, FTAG, &os) == 0) {
371 if (dmu_objset_type(os) != DMU_OST_ZFS) {
372 dmu_objset_rele(os, FTAG);
375 /* XXX reading from non-owned objset */
376 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
377 rc = zplversion < version;
378 dmu_objset_rele(os, FTAG);
384 zfs_log_history(zfs_cmd_t *zc)
389 if ((buf = history_str_get(zc)) == NULL)
392 if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
393 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
394 (void) spa_history_log(spa, buf);
395 spa_close(spa, FTAG);
397 history_str_free(buf);
401 * Policy for top-level read operations (list pools). Requires no privileges,
402 * and can be used in the local zone, as there is no associated dataset.
406 zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
412 * Policy for dataset read operations (list children, get statistics). Requires
413 * no privileges, but must be visible in the local zone.
417 zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
419 if (INGLOBALZONE(curthread) ||
420 zone_dataset_visible(zc->zc_name, NULL))
423 return (SET_ERROR(ENOENT));
427 zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
432 * The dataset must be visible by this zone -- check this first
433 * so they don't see EPERM on something they shouldn't know about.
435 if (!INGLOBALZONE(curthread) &&
436 !zone_dataset_visible(dataset, &writable))
437 return (SET_ERROR(ENOENT));
439 if (INGLOBALZONE(curthread)) {
441 * If the fs is zoned, only root can access it from the
444 if (secpolicy_zfs(cr) && zoned)
445 return (SET_ERROR(EPERM));
448 * If we are in a local zone, the 'zoned' property must be set.
451 return (SET_ERROR(EPERM));
453 /* must be writable by this zone */
455 return (SET_ERROR(EPERM));
461 zfs_dozonecheck(const char *dataset, cred_t *cr)
465 if (dsl_prop_get_integer(dataset, "jailed", &zoned, NULL))
466 return (SET_ERROR(ENOENT));
468 return (zfs_dozonecheck_impl(dataset, zoned, cr));
472 zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
476 if (dsl_prop_get_int_ds(ds, "jailed", &zoned))
477 return (SET_ERROR(ENOENT));
479 return (zfs_dozonecheck_impl(dataset, zoned, cr));
483 zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
484 const char *perm, cred_t *cr)
488 error = zfs_dozonecheck_ds(name, ds, cr);
490 error = secpolicy_zfs(cr);
492 error = dsl_deleg_access_impl(ds, perm, cr);
498 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
504 error = dsl_pool_hold(name, FTAG, &dp);
508 error = dsl_dataset_hold(dp, name, FTAG, &ds);
510 dsl_pool_rele(dp, FTAG);
514 error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
516 dsl_dataset_rele(ds, FTAG);
517 dsl_pool_rele(dp, FTAG);
523 * Policy for setting the security label property.
525 * Returns 0 for success, non-zero for access and other errors.
528 zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
530 char ds_hexsl[MAXNAMELEN];
531 bslabel_t ds_sl, new_sl;
532 boolean_t new_default = FALSE;
534 int needed_priv = -1;
537 /* First get the existing dataset label. */
538 error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
539 1, sizeof (ds_hexsl), &ds_hexsl, NULL);
541 return (SET_ERROR(EPERM));
543 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
546 /* The label must be translatable */
547 if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
548 return (SET_ERROR(EINVAL));
551 * In a non-global zone, disallow attempts to set a label that
552 * doesn't match that of the zone; otherwise no other checks
555 if (!INGLOBALZONE(curproc)) {
556 if (new_default || !blequal(&new_sl, CR_SL(CRED())))
557 return (SET_ERROR(EPERM));
562 * For global-zone datasets (i.e., those whose zoned property is
563 * "off", verify that the specified new label is valid for the
566 if (dsl_prop_get_integer(name,
567 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
568 return (SET_ERROR(EPERM));
570 if (zfs_check_global_label(name, strval) != 0)
571 return (SET_ERROR(EPERM));
575 * If the existing dataset label is nondefault, check if the
576 * dataset is mounted (label cannot be changed while mounted).
577 * Get the zfsvfs; if there isn't one, then the dataset isn't
578 * mounted (or isn't a dataset, doesn't exist, ...).
580 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
582 static char *setsl_tag = "setsl_tag";
585 * Try to own the dataset; abort if there is any error,
586 * (e.g., already mounted, in use, or other error).
588 error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE,
591 return (SET_ERROR(EPERM));
593 dmu_objset_disown(os, setsl_tag);
596 needed_priv = PRIV_FILE_DOWNGRADE_SL;
600 if (hexstr_to_label(strval, &new_sl) != 0)
601 return (SET_ERROR(EPERM));
603 if (blstrictdom(&ds_sl, &new_sl))
604 needed_priv = PRIV_FILE_DOWNGRADE_SL;
605 else if (blstrictdom(&new_sl, &ds_sl))
606 needed_priv = PRIV_FILE_UPGRADE_SL;
608 /* dataset currently has a default label */
610 needed_priv = PRIV_FILE_UPGRADE_SL;
614 if (needed_priv != -1)
615 return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
618 #endif /* SECLABEL */
621 zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
627 * Check permissions for special properties.
632 * Disallow setting of 'zoned' from within a local zone.
634 if (!INGLOBALZONE(curthread))
635 return (SET_ERROR(EPERM));
639 if (!INGLOBALZONE(curthread)) {
641 char setpoint[MAXNAMELEN];
643 * Unprivileged users are allowed to modify the
644 * quota on things *under* (ie. contained by)
645 * the thing they own.
647 if (dsl_prop_get_integer(dsname, "jailed", &zoned,
649 return (SET_ERROR(EPERM));
650 if (!zoned || strlen(dsname) <= strlen(setpoint))
651 return (SET_ERROR(EPERM));
655 case ZFS_PROP_MLSLABEL:
657 if (!is_system_labeled())
658 return (SET_ERROR(EPERM));
660 if (nvpair_value_string(propval, &strval) == 0) {
663 err = zfs_set_slabel_policy(dsname, strval, CRED());
673 return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
678 zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
682 error = zfs_dozonecheck(zc->zc_name, cr);
687 * permission to set permissions will be evaluated later in
688 * dsl_deleg_can_allow()
695 zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
697 return (zfs_secpolicy_write_perms(zc->zc_name,
698 ZFS_DELEG_PERM_ROLLBACK, cr));
703 zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
711 * Generate the current snapshot name from the given objsetid, then
712 * use that name for the secpolicy/zone checks.
714 cp = strchr(zc->zc_name, '@');
716 return (SET_ERROR(EINVAL));
717 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
721 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
723 dsl_pool_rele(dp, FTAG);
727 dsl_dataset_name(ds, zc->zc_name);
729 error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
730 ZFS_DELEG_PERM_SEND, cr);
731 dsl_dataset_rele(ds, FTAG);
732 dsl_pool_rele(dp, FTAG);
739 zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
741 return (zfs_secpolicy_write_perms(zc->zc_name,
742 ZFS_DELEG_PERM_SEND, cr));
747 zfs_secpolicy_deleg_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
752 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
753 NO_FOLLOW, NULL, &vp)) != 0)
756 /* Now make sure mntpnt and dataset are ZFS */
758 if (strcmp(vp->v_vfsp->mnt_stat.f_fstypename, "zfs") != 0 ||
759 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
760 zc->zc_name) != 0)) {
762 return (SET_ERROR(EPERM));
766 return (dsl_deleg_access(zc->zc_name,
767 ZFS_DELEG_PERM_SHARE, cr));
771 zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
773 if (!INGLOBALZONE(curthread))
774 return (SET_ERROR(EPERM));
776 if (secpolicy_nfs(cr) == 0) {
779 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
784 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
786 if (!INGLOBALZONE(curthread))
787 return (SET_ERROR(EPERM));
789 if (secpolicy_smb(cr) == 0) {
792 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
797 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
802 * Remove the @bla or /bla from the end of the name to get the parent.
804 (void) strncpy(parent, datasetname, parentsize);
805 cp = strrchr(parent, '@');
809 cp = strrchr(parent, '/');
811 return (SET_ERROR(ENOENT));
819 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
823 if ((error = zfs_secpolicy_write_perms(name,
824 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
827 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
832 zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
834 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
838 * Destroying snapshots with delegated permissions requires
839 * descendant mount and destroy permissions.
843 zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
846 nvpair_t *pair, *nextpair;
849 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
850 return (SET_ERROR(EINVAL));
851 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
856 error = dsl_pool_hold(nvpair_name(pair), FTAG, &dp);
859 nextpair = nvlist_next_nvpair(snaps, pair);
860 error = dsl_dataset_hold(dp, nvpair_name(pair), FTAG, &ds);
862 dsl_dataset_rele(ds, FTAG);
863 dsl_pool_rele(dp, FTAG);
866 error = zfs_secpolicy_destroy_perms(nvpair_name(pair),
868 } else if (error == ENOENT) {
870 * Ignore any snapshots that don't exist (we consider
871 * them "already destroyed"). Remove the name from the
872 * nvl here in case the snapshot is created between
873 * now and when we try to destroy it (in which case
874 * we don't want to destroy it since we haven't
875 * checked for permission).
877 fnvlist_remove_nvpair(snaps, pair);
888 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
890 char parentname[MAXNAMELEN];
893 if ((error = zfs_secpolicy_write_perms(from,
894 ZFS_DELEG_PERM_RENAME, cr)) != 0)
897 if ((error = zfs_secpolicy_write_perms(from,
898 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
901 if ((error = zfs_get_parent(to, parentname,
902 sizeof (parentname))) != 0)
905 if ((error = zfs_secpolicy_write_perms(parentname,
906 ZFS_DELEG_PERM_CREATE, cr)) != 0)
909 if ((error = zfs_secpolicy_write_perms(parentname,
910 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
918 zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
923 if ((zc->zc_cookie & 1) != 0) {
925 * This is recursive rename, so the starting snapshot might
926 * not exist. Check file system or volume permission instead.
928 at = strchr(zc->zc_name, '@');
934 error = zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr);
944 zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
947 dsl_dataset_t *clone;
950 error = zfs_secpolicy_write_perms(zc->zc_name,
951 ZFS_DELEG_PERM_PROMOTE, cr);
955 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
959 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
962 char parentname[MAXNAMELEN];
963 dsl_dataset_t *origin = NULL;
967 error = dsl_dataset_hold_obj(dd->dd_pool,
968 dd->dd_phys->dd_origin_obj, FTAG, &origin);
970 dsl_dataset_rele(clone, FTAG);
971 dsl_pool_rele(dp, FTAG);
975 error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
976 ZFS_DELEG_PERM_MOUNT, cr);
978 dsl_dataset_name(origin, parentname);
980 error = zfs_secpolicy_write_perms_ds(parentname, origin,
981 ZFS_DELEG_PERM_PROMOTE, cr);
983 dsl_dataset_rele(clone, FTAG);
984 dsl_dataset_rele(origin, FTAG);
986 dsl_pool_rele(dp, FTAG);
992 zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
996 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
997 ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
1000 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1001 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
1004 return (zfs_secpolicy_write_perms(zc->zc_name,
1005 ZFS_DELEG_PERM_CREATE, cr));
1009 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
1011 return (zfs_secpolicy_write_perms(name,
1012 ZFS_DELEG_PERM_SNAPSHOT, cr));
1016 * Check for permission to create each snapshot in the nvlist.
1020 zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1026 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
1027 return (SET_ERROR(EINVAL));
1028 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
1029 pair = nvlist_next_nvpair(snaps, pair)) {
1030 char *name = nvpair_name(pair);
1031 char *atp = strchr(name, '@');
1034 error = SET_ERROR(EINVAL);
1038 error = zfs_secpolicy_snapshot_perms(name, cr);
1048 zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1051 * Even root must have a proper TSD so that we know what pool
1054 if (tsd_get(zfs_allow_log_key) == NULL)
1055 return (SET_ERROR(EPERM));
1060 zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1062 char parentname[MAXNAMELEN];
1066 if ((error = zfs_get_parent(zc->zc_name, parentname,
1067 sizeof (parentname))) != 0)
1070 if (nvlist_lookup_string(innvl, "origin", &origin) == 0 &&
1071 (error = zfs_secpolicy_write_perms(origin,
1072 ZFS_DELEG_PERM_CLONE, cr)) != 0)
1075 if ((error = zfs_secpolicy_write_perms(parentname,
1076 ZFS_DELEG_PERM_CREATE, cr)) != 0)
1079 return (zfs_secpolicy_write_perms(parentname,
1080 ZFS_DELEG_PERM_MOUNT, cr));
1084 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
1085 * SYS_CONFIG privilege, which is not available in a local zone.
1089 zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1091 if (secpolicy_sys_config(cr, B_FALSE) != 0)
1092 return (SET_ERROR(EPERM));
1098 * Policy for object to name lookups.
1102 zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1106 if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
1109 error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
1114 * Policy for fault injection. Requires all privileges.
1118 zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1120 return (secpolicy_zinject(cr));
1125 zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1127 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
1129 if (prop == ZPROP_INVAL) {
1130 if (!zfs_prop_user(zc->zc_value))
1131 return (SET_ERROR(EINVAL));
1132 return (zfs_secpolicy_write_perms(zc->zc_name,
1133 ZFS_DELEG_PERM_USERPROP, cr));
1135 return (zfs_secpolicy_setprop(zc->zc_name, prop,
1141 zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1143 int err = zfs_secpolicy_read(zc, innvl, cr);
1147 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1148 return (SET_ERROR(EINVAL));
1150 if (zc->zc_value[0] == 0) {
1152 * They are asking about a posix uid/gid. If it's
1153 * themself, allow it.
1155 if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
1156 zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
1157 if (zc->zc_guid == crgetuid(cr))
1160 if (groupmember(zc->zc_guid, cr))
1165 return (zfs_secpolicy_write_perms(zc->zc_name,
1166 userquota_perms[zc->zc_objset_type], cr));
1170 zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1172 int err = zfs_secpolicy_read(zc, innvl, cr);
1176 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1177 return (SET_ERROR(EINVAL));
1179 return (zfs_secpolicy_write_perms(zc->zc_name,
1180 userquota_perms[zc->zc_objset_type], cr));
1185 zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1187 return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
1193 zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1199 error = nvlist_lookup_nvlist(innvl, "holds", &holds);
1201 return (SET_ERROR(EINVAL));
1203 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
1204 pair = nvlist_next_nvpair(holds, pair)) {
1205 char fsname[MAXNAMELEN];
1206 error = dmu_fsname(nvpair_name(pair), fsname);
1209 error = zfs_secpolicy_write_perms(fsname,
1210 ZFS_DELEG_PERM_HOLD, cr);
1219 zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1224 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1225 pair = nvlist_next_nvpair(innvl, pair)) {
1226 char fsname[MAXNAMELEN];
1227 error = dmu_fsname(nvpair_name(pair), fsname);
1230 error = zfs_secpolicy_write_perms(fsname,
1231 ZFS_DELEG_PERM_RELEASE, cr);
1239 * Policy for allowing temporary snapshots to be taken or released
1242 zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1245 * A temporary snapshot is the same as a snapshot,
1246 * hold, destroy and release all rolled into one.
1247 * Delegated diff alone is sufficient that we allow this.
1251 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1252 ZFS_DELEG_PERM_DIFF, cr)) == 0)
1255 error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
1257 error = zfs_secpolicy_hold(zc, innvl, cr);
1259 error = zfs_secpolicy_release(zc, innvl, cr);
1261 error = zfs_secpolicy_destroy(zc, innvl, cr);
1266 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1269 get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1273 nvlist_t *list = NULL;
1276 * Read in and unpack the user-supplied nvlist.
1279 return (SET_ERROR(EINVAL));
1281 packed = kmem_alloc(size, KM_SLEEP);
1283 if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1285 kmem_free(packed, size);
1289 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1290 kmem_free(packed, size);
1294 kmem_free(packed, size);
1301 * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
1302 * Entries will be removed from the end of the nvlist, and one int32 entry
1303 * named "N_MORE_ERRORS" will be added indicating how many entries were
1307 nvlist_smush(nvlist_t *errors, size_t max)
1311 size = fnvlist_size(errors);
1314 nvpair_t *more_errors;
1318 return (SET_ERROR(ENOMEM));
1320 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
1321 more_errors = nvlist_prev_nvpair(errors, NULL);
1324 nvpair_t *pair = nvlist_prev_nvpair(errors,
1326 fnvlist_remove_nvpair(errors, pair);
1328 size = fnvlist_size(errors);
1329 } while (size > max);
1331 fnvlist_remove_nvpair(errors, more_errors);
1332 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
1333 ASSERT3U(fnvlist_size(errors), <=, max);
1340 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1342 char *packed = NULL;
1346 size = fnvlist_size(nvl);
1348 if (size > zc->zc_nvlist_dst_size) {
1350 * Solaris returns ENOMEM here, because even if an error is
1351 * returned from an ioctl(2), new zc_nvlist_dst_size will be
1352 * passed to the userland. This is not the case for FreeBSD.
1353 * We need to return 0, so the kernel will copy the
1354 * zc_nvlist_dst_size back and the userland can discover that a
1355 * bigger buffer is needed.
1359 packed = fnvlist_pack(nvl, &size);
1360 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1361 size, zc->zc_iflags) != 0)
1362 error = SET_ERROR(EFAULT);
1363 fnvlist_pack_free(packed, size);
1366 zc->zc_nvlist_dst_size = size;
1367 zc->zc_nvlist_dst_filled = B_TRUE;
1372 getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
1377 error = dmu_objset_hold(dsname, FTAG, &os);
1380 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1381 dmu_objset_rele(os, FTAG);
1382 return (SET_ERROR(EINVAL));
1385 mutex_enter(&os->os_user_ptr_lock);
1386 *zfvp = dmu_objset_get_user(os);
1388 VFS_HOLD((*zfvp)->z_vfs);
1390 error = SET_ERROR(ESRCH);
1392 mutex_exit(&os->os_user_ptr_lock);
1393 dmu_objset_rele(os, FTAG);
1398 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
1399 * case its z_vfs will be NULL, and it will be opened as the owner.
1400 * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1401 * which prevents all vnode ops from running.
1404 zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
1408 if (getzfsvfs(name, zfvp) != 0)
1409 error = zfsvfs_create(name, zfvp);
1411 rrw_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
1413 if ((*zfvp)->z_unmounted) {
1415 * XXX we could probably try again, since the unmounting
1416 * thread should be just about to disassociate the
1417 * objset from the zfsvfs.
1419 rrw_exit(&(*zfvp)->z_teardown_lock, tag);
1420 return (SET_ERROR(EBUSY));
1427 zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
1429 rrw_exit(&zfsvfs->z_teardown_lock, tag);
1431 if (zfsvfs->z_vfs) {
1432 VFS_RELE(zfsvfs->z_vfs);
1434 dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1435 zfsvfs_free(zfsvfs);
1440 zfs_ioc_pool_create(zfs_cmd_t *zc)
1443 nvlist_t *config, *props = NULL;
1444 nvlist_t *rootprops = NULL;
1445 nvlist_t *zplprops = NULL;
1447 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1448 zc->zc_iflags, &config))
1451 if (zc->zc_nvlist_src_size != 0 && (error =
1452 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1453 zc->zc_iflags, &props))) {
1454 nvlist_free(config);
1459 nvlist_t *nvl = NULL;
1460 uint64_t version = SPA_VERSION;
1462 (void) nvlist_lookup_uint64(props,
1463 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1464 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1465 error = SET_ERROR(EINVAL);
1466 goto pool_props_bad;
1468 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
1470 error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
1472 nvlist_free(config);
1476 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1478 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1479 error = zfs_fill_zplprops_root(version, rootprops,
1482 goto pool_props_bad;
1485 error = spa_create(zc->zc_name, config, props, zplprops);
1488 * Set the remaining root properties
1490 if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
1491 ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
1492 (void) spa_destroy(zc->zc_name);
1495 nvlist_free(rootprops);
1496 nvlist_free(zplprops);
1497 nvlist_free(config);
1504 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1507 zfs_log_history(zc);
1508 error = spa_destroy(zc->zc_name);
1510 zvol_remove_minors(zc->zc_name);
1515 zfs_ioc_pool_import(zfs_cmd_t *zc)
1517 nvlist_t *config, *props = NULL;
1521 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1522 zc->zc_iflags, &config)) != 0)
1525 if (zc->zc_nvlist_src_size != 0 && (error =
1526 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1527 zc->zc_iflags, &props))) {
1528 nvlist_free(config);
1532 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1533 guid != zc->zc_guid)
1534 error = SET_ERROR(EINVAL);
1536 error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1538 if (zc->zc_nvlist_dst != 0) {
1541 if ((err = put_nvlist(zc, config)) != 0)
1545 nvlist_free(config);
1554 zfs_ioc_pool_export(zfs_cmd_t *zc)
1557 boolean_t force = (boolean_t)zc->zc_cookie;
1558 boolean_t hardforce = (boolean_t)zc->zc_guid;
1560 zfs_log_history(zc);
1561 error = spa_export(zc->zc_name, NULL, force, hardforce);
1563 zvol_remove_minors(zc->zc_name);
1568 zfs_ioc_pool_configs(zfs_cmd_t *zc)
1573 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1574 return (SET_ERROR(EEXIST));
1576 error = put_nvlist(zc, configs);
1578 nvlist_free(configs);
1585 * zc_name name of the pool
1588 * zc_cookie real errno
1589 * zc_nvlist_dst config nvlist
1590 * zc_nvlist_dst_size size of config nvlist
1593 zfs_ioc_pool_stats(zfs_cmd_t *zc)
1599 error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1600 sizeof (zc->zc_value));
1602 if (config != NULL) {
1603 ret = put_nvlist(zc, config);
1604 nvlist_free(config);
1607 * The config may be present even if 'error' is non-zero.
1608 * In this case we return success, and preserve the real errno
1611 zc->zc_cookie = error;
1620 * Try to import the given pool, returning pool stats as appropriate so that
1621 * user land knows which devices are available and overall pool health.
1624 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1626 nvlist_t *tryconfig, *config;
1629 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1630 zc->zc_iflags, &tryconfig)) != 0)
1633 config = spa_tryimport(tryconfig);
1635 nvlist_free(tryconfig);
1638 return (SET_ERROR(EINVAL));
1640 error = put_nvlist(zc, config);
1641 nvlist_free(config);
1648 * zc_name name of the pool
1649 * zc_cookie scan func (pool_scan_func_t)
1652 zfs_ioc_pool_scan(zfs_cmd_t *zc)
1657 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1660 if (zc->zc_cookie == POOL_SCAN_NONE)
1661 error = spa_scan_stop(spa);
1663 error = spa_scan(spa, zc->zc_cookie);
1665 spa_close(spa, FTAG);
1671 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1676 error = spa_open(zc->zc_name, &spa, FTAG);
1679 spa_close(spa, FTAG);
1685 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1690 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1693 if (zc->zc_cookie < spa_version(spa) ||
1694 !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1695 spa_close(spa, FTAG);
1696 return (SET_ERROR(EINVAL));
1699 spa_upgrade(spa, zc->zc_cookie);
1700 spa_close(spa, FTAG);
1706 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1713 if ((size = zc->zc_history_len) == 0)
1714 return (SET_ERROR(EINVAL));
1716 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1719 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1720 spa_close(spa, FTAG);
1721 return (SET_ERROR(ENOTSUP));
1724 hist_buf = kmem_alloc(size, KM_SLEEP);
1725 if ((error = spa_history_get(spa, &zc->zc_history_offset,
1726 &zc->zc_history_len, hist_buf)) == 0) {
1727 error = ddi_copyout(hist_buf,
1728 (void *)(uintptr_t)zc->zc_history,
1729 zc->zc_history_len, zc->zc_iflags);
1732 spa_close(spa, FTAG);
1733 kmem_free(hist_buf, size);
1738 zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1743 error = spa_open(zc->zc_name, &spa, FTAG);
1745 error = spa_change_guid(spa);
1746 spa_close(spa, FTAG);
1752 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1754 return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
1759 * zc_name name of filesystem
1760 * zc_obj object to find
1763 * zc_value name of object
1766 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1771 /* XXX reading from objset not owned */
1772 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1774 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1775 dmu_objset_rele(os, FTAG);
1776 return (SET_ERROR(EINVAL));
1778 error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
1779 sizeof (zc->zc_value));
1780 dmu_objset_rele(os, FTAG);
1787 * zc_name name of filesystem
1788 * zc_obj object to find
1791 * zc_stat stats on object
1792 * zc_value path to object
1795 zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1800 /* XXX reading from objset not owned */
1801 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1803 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1804 dmu_objset_rele(os, FTAG);
1805 return (SET_ERROR(EINVAL));
1807 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1808 sizeof (zc->zc_value));
1809 dmu_objset_rele(os, FTAG);
1815 zfs_ioc_vdev_add(zfs_cmd_t *zc)
1819 nvlist_t *config, **l2cache, **spares;
1820 uint_t nl2cache = 0, nspares = 0;
1822 error = spa_open(zc->zc_name, &spa, FTAG);
1826 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1827 zc->zc_iflags, &config);
1828 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1829 &l2cache, &nl2cache);
1831 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1835 * A root pool with concatenated devices is not supported.
1836 * Thus, can not add a device to a root pool.
1838 * Intent log device can not be added to a rootpool because
1839 * during mountroot, zil is replayed, a seperated log device
1840 * can not be accessed during the mountroot time.
1842 * l2cache and spare devices are ok to be added to a rootpool.
1844 if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
1845 nvlist_free(config);
1846 spa_close(spa, FTAG);
1847 return (SET_ERROR(EDOM));
1851 error = spa_vdev_add(spa, config);
1852 nvlist_free(config);
1854 spa_close(spa, FTAG);
1860 * zc_name name of the pool
1861 * zc_nvlist_conf nvlist of devices to remove
1862 * zc_cookie to stop the remove?
1865 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1870 error = spa_open(zc->zc_name, &spa, FTAG);
1873 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1874 spa_close(spa, FTAG);
1879 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1883 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1885 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1887 switch (zc->zc_cookie) {
1888 case VDEV_STATE_ONLINE:
1889 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1892 case VDEV_STATE_OFFLINE:
1893 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1896 case VDEV_STATE_FAULTED:
1897 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1898 zc->zc_obj != VDEV_AUX_EXTERNAL)
1899 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1901 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
1904 case VDEV_STATE_DEGRADED:
1905 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1906 zc->zc_obj != VDEV_AUX_EXTERNAL)
1907 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1909 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
1913 error = SET_ERROR(EINVAL);
1915 zc->zc_cookie = newstate;
1916 spa_close(spa, FTAG);
1921 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1924 int replacing = zc->zc_cookie;
1928 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1931 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1932 zc->zc_iflags, &config)) == 0) {
1933 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1934 nvlist_free(config);
1937 spa_close(spa, FTAG);
1942 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1947 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1950 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1952 spa_close(spa, FTAG);
1957 zfs_ioc_vdev_split(zfs_cmd_t *zc)
1960 nvlist_t *config, *props = NULL;
1962 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
1964 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1967 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1968 zc->zc_iflags, &config)) {
1969 spa_close(spa, FTAG);
1973 if (zc->zc_nvlist_src_size != 0 && (error =
1974 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1975 zc->zc_iflags, &props))) {
1976 spa_close(spa, FTAG);
1977 nvlist_free(config);
1981 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
1983 spa_close(spa, FTAG);
1985 nvlist_free(config);
1992 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
1995 char *path = zc->zc_value;
1996 uint64_t guid = zc->zc_guid;
1999 error = spa_open(zc->zc_name, &spa, FTAG);
2003 error = spa_vdev_setpath(spa, guid, path);
2004 spa_close(spa, FTAG);
2009 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
2012 char *fru = zc->zc_value;
2013 uint64_t guid = zc->zc_guid;
2016 error = spa_open(zc->zc_name, &spa, FTAG);
2020 error = spa_vdev_setfru(spa, guid, fru);
2021 spa_close(spa, FTAG);
2026 zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
2031 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2033 if (zc->zc_nvlist_dst != 0 &&
2034 (error = dsl_prop_get_all(os, &nv)) == 0) {
2035 dmu_objset_stats(os, nv);
2037 * NB: zvol_get_stats() will read the objset contents,
2038 * which we aren't supposed to do with a
2039 * DS_MODE_USER hold, because it could be
2040 * inconsistent. So this is a bit of a workaround...
2041 * XXX reading with out owning
2043 if (!zc->zc_objset_stats.dds_inconsistent &&
2044 dmu_objset_type(os) == DMU_OST_ZVOL) {
2045 error = zvol_get_stats(os, nv);
2050 error = put_nvlist(zc, nv);
2059 * zc_name name of filesystem
2060 * zc_nvlist_dst_size size of buffer for property nvlist
2063 * zc_objset_stats stats
2064 * zc_nvlist_dst property nvlist
2065 * zc_nvlist_dst_size size of property nvlist
2068 zfs_ioc_objset_stats(zfs_cmd_t *zc)
2073 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2075 error = zfs_ioc_objset_stats_impl(zc, os);
2076 dmu_objset_rele(os, FTAG);
2079 if (error == ENOMEM)
2086 * zc_name name of filesystem
2087 * zc_nvlist_dst_size size of buffer for property nvlist
2090 * zc_nvlist_dst received property nvlist
2091 * zc_nvlist_dst_size size of received property nvlist
2093 * Gets received properties (distinct from local properties on or after
2094 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
2095 * local property values.
2098 zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
2104 * Without this check, we would return local property values if the
2105 * caller has not already received properties on or after
2106 * SPA_VERSION_RECVD_PROPS.
2108 if (!dsl_prop_get_hasrecvd(zc->zc_name))
2109 return (SET_ERROR(ENOTSUP));
2111 if (zc->zc_nvlist_dst != 0 &&
2112 (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
2113 error = put_nvlist(zc, nv);
2121 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2127 * zfs_get_zplprop() will either find a value or give us
2128 * the default value (if there is one).
2130 if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2132 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2138 * zc_name name of filesystem
2139 * zc_nvlist_dst_size size of buffer for zpl property nvlist
2142 * zc_nvlist_dst zpl property nvlist
2143 * zc_nvlist_dst_size size of zpl property nvlist
2146 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2151 /* XXX reading without owning */
2152 if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
2155 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2158 * NB: nvl_add_zplprop() will read the objset contents,
2159 * which we aren't supposed to do with a DS_MODE_USER
2160 * hold, because it could be inconsistent.
2162 if (zc->zc_nvlist_dst != 0 &&
2163 !zc->zc_objset_stats.dds_inconsistent &&
2164 dmu_objset_type(os) == DMU_OST_ZFS) {
2167 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2168 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2169 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2170 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2171 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2172 err = put_nvlist(zc, nv);
2175 err = SET_ERROR(ENOENT);
2177 dmu_objset_rele(os, FTAG);
2182 dataset_name_hidden(const char *name)
2185 * Skip over datasets that are not visible in this zone,
2186 * internal datasets (which have a $ in their name), and
2187 * temporary datasets (which have a % in their name).
2189 if (strchr(name, '$') != NULL)
2191 if (strchr(name, '%') != NULL)
2193 if (!INGLOBALZONE(curthread) && !zone_dataset_visible(name, NULL))
2200 * zc_name name of filesystem
2201 * zc_cookie zap cursor
2202 * zc_nvlist_dst_size size of buffer for property nvlist
2205 * zc_name name of next filesystem
2206 * zc_cookie zap cursor
2207 * zc_objset_stats stats
2208 * zc_nvlist_dst property nvlist
2209 * zc_nvlist_dst_size size of property nvlist
2212 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2217 size_t orig_len = strlen(zc->zc_name);
2220 if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
2221 if (error == ENOENT)
2222 error = SET_ERROR(ESRCH);
2226 p = strrchr(zc->zc_name, '/');
2227 if (p == NULL || p[1] != '\0')
2228 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2229 p = zc->zc_name + strlen(zc->zc_name);
2232 error = dmu_dir_list_next(os,
2233 sizeof (zc->zc_name) - (p - zc->zc_name), p,
2234 NULL, &zc->zc_cookie);
2235 if (error == ENOENT)
2236 error = SET_ERROR(ESRCH);
2237 } while (error == 0 && dataset_name_hidden(zc->zc_name));
2238 dmu_objset_rele(os, FTAG);
2241 * If it's an internal dataset (ie. with a '$' in its name),
2242 * don't try to get stats for it, otherwise we'll return ENOENT.
2244 if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
2245 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2246 if (error == ENOENT) {
2247 /* We lost a race with destroy, get the next one. */
2248 zc->zc_name[orig_len] = '\0';
2257 * zc_name name of filesystem
2258 * zc_cookie zap cursor
2259 * zc_nvlist_dst_size size of buffer for property nvlist
2260 * zc_simple when set, only name is requested
2263 * zc_name name of next snapshot
2264 * zc_objset_stats stats
2265 * zc_nvlist_dst property nvlist
2266 * zc_nvlist_dst_size size of property nvlist
2269 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2274 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2276 return (error == ENOENT ? ESRCH : error);
2280 * A dataset name of maximum length cannot have any snapshots,
2281 * so exit immediately.
2283 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
2284 dmu_objset_rele(os, FTAG);
2285 return (SET_ERROR(ESRCH));
2288 error = dmu_snapshot_list_next(os,
2289 sizeof (zc->zc_name) - strlen(zc->zc_name),
2290 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2293 if (error == 0 && !zc->zc_simple) {
2295 dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2297 error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
2301 error = dmu_objset_from_ds(ds, &ossnap);
2303 error = zfs_ioc_objset_stats_impl(zc, ossnap);
2304 dsl_dataset_rele(ds, FTAG);
2306 } else if (error == ENOENT) {
2307 error = SET_ERROR(ESRCH);
2310 dmu_objset_rele(os, FTAG);
2311 /* if we failed, undo the @ that we tacked on to zc_name */
2313 *strchr(zc->zc_name, '@') = '\0';
2318 zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2320 const char *propname = nvpair_name(pair);
2322 unsigned int vallen;
2325 zfs_userquota_prop_t type;
2331 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2333 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2334 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2336 return (SET_ERROR(EINVAL));
2340 * A correctly constructed propname is encoded as
2341 * userquota@<rid>-<domain>.
2343 if ((dash = strchr(propname, '-')) == NULL ||
2344 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2346 return (SET_ERROR(EINVAL));
2353 err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
2355 err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
2356 zfsvfs_rele(zfsvfs, FTAG);
2363 * If the named property is one that has a special function to set its value,
2364 * return 0 on success and a positive error code on failure; otherwise if it is
2365 * not one of the special properties handled by this function, return -1.
2367 * XXX: It would be better for callers of the property interface if we handled
2368 * these special cases in dsl_prop.c (in the dsl layer).
2371 zfs_prop_set_special(const char *dsname, zprop_source_t source,
2374 const char *propname = nvpair_name(pair);
2375 zfs_prop_t prop = zfs_name_to_prop(propname);
2379 if (prop == ZPROP_INVAL) {
2380 if (zfs_prop_userquota(propname))
2381 return (zfs_prop_set_userquota(dsname, pair));
2385 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2387 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2388 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2392 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
2395 VERIFY(0 == nvpair_value_uint64(pair, &intval));
2398 case ZFS_PROP_QUOTA:
2399 err = dsl_dir_set_quota(dsname, source, intval);
2401 case ZFS_PROP_REFQUOTA:
2402 err = dsl_dataset_set_refquota(dsname, source, intval);
2404 case ZFS_PROP_RESERVATION:
2405 err = dsl_dir_set_reservation(dsname, source, intval);
2407 case ZFS_PROP_REFRESERVATION:
2408 err = dsl_dataset_set_refreservation(dsname, source, intval);
2410 case ZFS_PROP_VOLSIZE:
2411 err = zvol_set_volsize(dsname, ddi_driver_major(zfs_dip),
2414 case ZFS_PROP_VERSION:
2418 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2421 err = zfs_set_version(zfsvfs, intval);
2422 zfsvfs_rele(zfsvfs, FTAG);
2424 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2427 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2428 (void) strcpy(zc->zc_name, dsname);
2429 (void) zfs_ioc_userspace_upgrade(zc);
2430 kmem_free(zc, sizeof (zfs_cmd_t));
2434 case ZFS_PROP_COMPRESSION:
2436 if (intval == ZIO_COMPRESS_LZ4) {
2437 zfeature_info_t *feature =
2438 &spa_feature_table[SPA_FEATURE_LZ4_COMPRESS];
2441 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
2445 * Setting the LZ4 compression algorithm activates
2448 if (!spa_feature_is_active(spa, feature)) {
2449 if ((err = zfs_prop_activate_feature(spa,
2451 spa_close(spa, FTAG);
2456 spa_close(spa, FTAG);
2459 * We still want the default set action to be performed in the
2460 * caller, we only performed zfeature settings here.
2474 * This function is best effort. If it fails to set any of the given properties,
2475 * it continues to set as many as it can and returns the last error
2476 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2477 * with the list of names of all the properties that failed along with the
2478 * corresponding error numbers.
2480 * If every property is set successfully, zero is returned and errlist is not
2484 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2492 nvlist_t *genericnvl = fnvlist_alloc();
2493 nvlist_t *retrynvl = fnvlist_alloc();
2497 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2498 const char *propname = nvpair_name(pair);
2499 zfs_prop_t prop = zfs_name_to_prop(propname);
2502 /* decode the property value */
2504 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2506 attrs = fnvpair_value_nvlist(pair);
2507 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2509 err = SET_ERROR(EINVAL);
2512 /* Validate value type */
2513 if (err == 0 && prop == ZPROP_INVAL) {
2514 if (zfs_prop_user(propname)) {
2515 if (nvpair_type(propval) != DATA_TYPE_STRING)
2516 err = SET_ERROR(EINVAL);
2517 } else if (zfs_prop_userquota(propname)) {
2518 if (nvpair_type(propval) !=
2519 DATA_TYPE_UINT64_ARRAY)
2520 err = SET_ERROR(EINVAL);
2522 err = SET_ERROR(EINVAL);
2524 } else if (err == 0) {
2525 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2526 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2527 err = SET_ERROR(EINVAL);
2528 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2531 intval = fnvpair_value_uint64(propval);
2533 switch (zfs_prop_get_type(prop)) {
2534 case PROP_TYPE_NUMBER:
2536 case PROP_TYPE_STRING:
2537 err = SET_ERROR(EINVAL);
2539 case PROP_TYPE_INDEX:
2540 if (zfs_prop_index_to_string(prop,
2541 intval, &unused) != 0)
2542 err = SET_ERROR(EINVAL);
2546 "unknown property type");
2549 err = SET_ERROR(EINVAL);
2553 /* Validate permissions */
2555 err = zfs_check_settable(dsname, pair, CRED());
2558 err = zfs_prop_set_special(dsname, source, pair);
2561 * For better performance we build up a list of
2562 * properties to set in a single transaction.
2564 err = nvlist_add_nvpair(genericnvl, pair);
2565 } else if (err != 0 && nvl != retrynvl) {
2567 * This may be a spurious error caused by
2568 * receiving quota and reservation out of order.
2569 * Try again in a second pass.
2571 err = nvlist_add_nvpair(retrynvl, pair);
2576 if (errlist != NULL)
2577 fnvlist_add_int32(errlist, propname, err);
2582 if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2587 if (!nvlist_empty(genericnvl) &&
2588 dsl_props_set(dsname, source, genericnvl) != 0) {
2590 * If this fails, we still want to set as many properties as we
2591 * can, so try setting them individually.
2594 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2595 const char *propname = nvpair_name(pair);
2599 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2601 attrs = fnvpair_value_nvlist(pair);
2602 propval = fnvlist_lookup_nvpair(attrs,
2606 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2607 strval = fnvpair_value_string(propval);
2608 err = dsl_prop_set_string(dsname, propname,
2611 intval = fnvpair_value_uint64(propval);
2612 err = dsl_prop_set_int(dsname, propname, source,
2617 if (errlist != NULL) {
2618 fnvlist_add_int32(errlist, propname,
2625 nvlist_free(genericnvl);
2626 nvlist_free(retrynvl);
2632 * Check that all the properties are valid user properties.
2635 zfs_check_userprops(const char *fsname, nvlist_t *nvl)
2637 nvpair_t *pair = NULL;
2640 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2641 const char *propname = nvpair_name(pair);
2644 if (!zfs_prop_user(propname) ||
2645 nvpair_type(pair) != DATA_TYPE_STRING)
2646 return (SET_ERROR(EINVAL));
2648 if (error = zfs_secpolicy_write_perms(fsname,
2649 ZFS_DELEG_PERM_USERPROP, CRED()))
2652 if (strlen(propname) >= ZAP_MAXNAMELEN)
2653 return (SET_ERROR(ENAMETOOLONG));
2655 VERIFY(nvpair_value_string(pair, &valstr) == 0);
2656 if (strlen(valstr) >= ZAP_MAXVALUELEN)
2663 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2667 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2670 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2671 if (nvlist_exists(skipped, nvpair_name(pair)))
2674 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2679 clear_received_props(const char *dsname, nvlist_t *props,
2683 nvlist_t *cleared_props = NULL;
2684 props_skip(props, skipped, &cleared_props);
2685 if (!nvlist_empty(cleared_props)) {
2687 * Acts on local properties until the dataset has received
2688 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2690 zprop_source_t flags = (ZPROP_SRC_NONE |
2691 (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
2692 err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
2694 nvlist_free(cleared_props);
2700 * zc_name name of filesystem
2701 * zc_value name of property to set
2702 * zc_nvlist_src{_size} nvlist of properties to apply
2703 * zc_cookie received properties flag
2706 * zc_nvlist_dst{_size} error for each unapplied received property
2709 zfs_ioc_set_prop(zfs_cmd_t *zc)
2712 boolean_t received = zc->zc_cookie;
2713 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2718 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2719 zc->zc_iflags, &nvl)) != 0)
2723 nvlist_t *origprops;
2725 if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
2726 (void) clear_received_props(zc->zc_name,
2728 nvlist_free(origprops);
2731 error = dsl_prop_set_hasrecvd(zc->zc_name);
2734 errors = fnvlist_alloc();
2736 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
2738 if (zc->zc_nvlist_dst != 0 && errors != NULL) {
2739 (void) put_nvlist(zc, errors);
2742 nvlist_free(errors);
2749 * zc_name name of filesystem
2750 * zc_value name of property to inherit
2751 * zc_cookie revert to received value if TRUE
2756 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2758 const char *propname = zc->zc_value;
2759 zfs_prop_t prop = zfs_name_to_prop(propname);
2760 boolean_t received = zc->zc_cookie;
2761 zprop_source_t source = (received
2762 ? ZPROP_SRC_NONE /* revert to received value, if any */
2763 : ZPROP_SRC_INHERITED); /* explicitly inherit */
2772 * zfs_prop_set_special() expects properties in the form of an
2773 * nvpair with type info.
2775 if (prop == ZPROP_INVAL) {
2776 if (!zfs_prop_user(propname))
2777 return (SET_ERROR(EINVAL));
2779 type = PROP_TYPE_STRING;
2780 } else if (prop == ZFS_PROP_VOLSIZE ||
2781 prop == ZFS_PROP_VERSION) {
2782 return (SET_ERROR(EINVAL));
2784 type = zfs_prop_get_type(prop);
2787 VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2790 case PROP_TYPE_STRING:
2791 VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2793 case PROP_TYPE_NUMBER:
2794 case PROP_TYPE_INDEX:
2795 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2799 return (SET_ERROR(EINVAL));
2802 pair = nvlist_next_nvpair(dummy, NULL);
2803 err = zfs_prop_set_special(zc->zc_name, source, pair);
2806 return (err); /* special property already handled */
2809 * Only check this in the non-received case. We want to allow
2810 * 'inherit -S' to revert non-inheritable properties like quota
2811 * and reservation to the received or default values even though
2812 * they are not considered inheritable.
2814 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2815 return (SET_ERROR(EINVAL));
2818 /* property name has been validated by zfs_secpolicy_inherit_prop() */
2819 return (dsl_prop_inherit(zc->zc_name, zc->zc_value, source));
2823 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2830 if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2831 zc->zc_iflags, &props))
2835 * If the only property is the configfile, then just do a spa_lookup()
2836 * to handle the faulted case.
2838 pair = nvlist_next_nvpair(props, NULL);
2839 if (pair != NULL && strcmp(nvpair_name(pair),
2840 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2841 nvlist_next_nvpair(props, pair) == NULL) {
2842 mutex_enter(&spa_namespace_lock);
2843 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2844 spa_configfile_set(spa, props, B_FALSE);
2845 spa_config_sync(spa, B_FALSE, B_TRUE);
2847 mutex_exit(&spa_namespace_lock);
2854 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2859 error = spa_prop_set(spa, props);
2862 spa_close(spa, FTAG);
2868 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2872 nvlist_t *nvp = NULL;
2874 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2876 * If the pool is faulted, there may be properties we can still
2877 * get (such as altroot and cachefile), so attempt to get them
2880 mutex_enter(&spa_namespace_lock);
2881 if ((spa = spa_lookup(zc->zc_name)) != NULL)
2882 error = spa_prop_get(spa, &nvp);
2883 mutex_exit(&spa_namespace_lock);
2885 error = spa_prop_get(spa, &nvp);
2886 spa_close(spa, FTAG);
2889 if (error == 0 && zc->zc_nvlist_dst != 0)
2890 error = put_nvlist(zc, nvp);
2892 error = SET_ERROR(EFAULT);
2900 * zc_name name of filesystem
2901 * zc_nvlist_src{_size} nvlist of delegated permissions
2902 * zc_perm_action allow/unallow flag
2907 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2910 nvlist_t *fsaclnv = NULL;
2912 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2913 zc->zc_iflags, &fsaclnv)) != 0)
2917 * Verify nvlist is constructed correctly
2919 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2920 nvlist_free(fsaclnv);
2921 return (SET_ERROR(EINVAL));
2925 * If we don't have PRIV_SYS_MOUNT, then validate
2926 * that user is allowed to hand out each permission in
2930 error = secpolicy_zfs(CRED());
2932 if (zc->zc_perm_action == B_FALSE) {
2933 error = dsl_deleg_can_allow(zc->zc_name,
2936 error = dsl_deleg_can_unallow(zc->zc_name,
2942 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2944 nvlist_free(fsaclnv);
2950 * zc_name name of filesystem
2953 * zc_nvlist_src{_size} nvlist of delegated permissions
2956 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
2961 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
2962 error = put_nvlist(zc, nvp);
2970 * Search the vfs list for a specified resource. Returns a pointer to it
2971 * or NULL if no suitable entry is found. The caller of this routine
2972 * is responsible for releasing the returned vfs pointer.
2975 zfs_get_vfs(const char *resource)
2979 mtx_lock(&mountlist_mtx);
2980 TAILQ_FOREACH(vfsp, &mountlist, mnt_list) {
2981 if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
2986 mtx_unlock(&mountlist_mtx);
2992 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2994 zfs_creat_t *zct = arg;
2996 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
2999 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
3003 * createprops list of properties requested by creator
3004 * default_zplver zpl version to use if unspecified in createprops
3005 * fuids_ok fuids allowed in this version of the spa?
3006 * os parent objset pointer (NULL if root fs)
3009 * zplprops values for the zplprops we attach to the master node object
3010 * is_ci true if requested file system will be purely case-insensitive
3012 * Determine the settings for utf8only, normalization and
3013 * casesensitivity. Specific values may have been requested by the
3014 * creator and/or we can inherit values from the parent dataset. If
3015 * the file system is of too early a vintage, a creator can not
3016 * request settings for these properties, even if the requested
3017 * setting is the default value. We don't actually want to create dsl
3018 * properties for these, so remove them from the source nvlist after
3022 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
3023 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
3024 nvlist_t *zplprops, boolean_t *is_ci)
3026 uint64_t sense = ZFS_PROP_UNDEFINED;
3027 uint64_t norm = ZFS_PROP_UNDEFINED;
3028 uint64_t u8 = ZFS_PROP_UNDEFINED;
3030 ASSERT(zplprops != NULL);
3033 * Pull out creator prop choices, if any.
3036 (void) nvlist_lookup_uint64(createprops,
3037 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
3038 (void) nvlist_lookup_uint64(createprops,
3039 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
3040 (void) nvlist_remove_all(createprops,
3041 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
3042 (void) nvlist_lookup_uint64(createprops,
3043 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
3044 (void) nvlist_remove_all(createprops,
3045 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
3046 (void) nvlist_lookup_uint64(createprops,
3047 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
3048 (void) nvlist_remove_all(createprops,
3049 zfs_prop_to_name(ZFS_PROP_CASE));
3053 * If the zpl version requested is whacky or the file system
3054 * or pool is version is too "young" to support normalization
3055 * and the creator tried to set a value for one of the props,
3058 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
3059 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
3060 (zplver >= ZPL_VERSION_SA && !sa_ok) ||
3061 (zplver < ZPL_VERSION_NORMALIZATION &&
3062 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
3063 sense != ZFS_PROP_UNDEFINED)))
3064 return (SET_ERROR(ENOTSUP));
3067 * Put the version in the zplprops
3069 VERIFY(nvlist_add_uint64(zplprops,
3070 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3072 if (norm == ZFS_PROP_UNDEFINED)
3073 VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
3074 VERIFY(nvlist_add_uint64(zplprops,
3075 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3078 * If we're normalizing, names must always be valid UTF-8 strings.
3082 if (u8 == ZFS_PROP_UNDEFINED)
3083 VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
3084 VERIFY(nvlist_add_uint64(zplprops,
3085 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3087 if (sense == ZFS_PROP_UNDEFINED)
3088 VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
3089 VERIFY(nvlist_add_uint64(zplprops,
3090 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3093 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
3099 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
3100 nvlist_t *zplprops, boolean_t *is_ci)
3102 boolean_t fuids_ok, sa_ok;
3103 uint64_t zplver = ZPL_VERSION;
3104 objset_t *os = NULL;
3105 char parentname[MAXNAMELEN];
3111 (void) strlcpy(parentname, dataset, sizeof (parentname));
3112 cp = strrchr(parentname, '/');
3116 if ((error = spa_open(dataset, &spa, FTAG)) != 0)
3119 spa_vers = spa_version(spa);
3120 spa_close(spa, FTAG);
3122 zplver = zfs_zpl_version_map(spa_vers);
3123 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3124 sa_ok = (zplver >= ZPL_VERSION_SA);
3127 * Open parent object set so we can inherit zplprop values.
3129 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
3132 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
3134 dmu_objset_rele(os, FTAG);
3139 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
3140 nvlist_t *zplprops, boolean_t *is_ci)
3144 uint64_t zplver = ZPL_VERSION;
3147 zplver = zfs_zpl_version_map(spa_vers);
3148 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3149 sa_ok = (zplver >= ZPL_VERSION_SA);
3151 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
3152 createprops, zplprops, is_ci);
3158 * "type" -> dmu_objset_type_t (int32)
3159 * (optional) "props" -> { prop -> value }
3162 * outnvl: propname -> error code (int32)
3165 zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3168 zfs_creat_t zct = { 0 };
3169 nvlist_t *nvprops = NULL;
3170 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
3172 dmu_objset_type_t type;
3173 boolean_t is_insensitive = B_FALSE;
3175 if (nvlist_lookup_int32(innvl, "type", &type32) != 0)
3176 return (SET_ERROR(EINVAL));
3178 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3182 cbfunc = zfs_create_cb;
3186 cbfunc = zvol_create_cb;
3193 if (strchr(fsname, '@') ||
3194 strchr(fsname, '%'))
3195 return (SET_ERROR(EINVAL));
3197 zct.zct_props = nvprops;
3200 return (SET_ERROR(EINVAL));
3202 if (type == DMU_OST_ZVOL) {
3203 uint64_t volsize, volblocksize;
3205 if (nvprops == NULL)
3206 return (SET_ERROR(EINVAL));
3207 if (nvlist_lookup_uint64(nvprops,
3208 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3209 return (SET_ERROR(EINVAL));
3211 if ((error = nvlist_lookup_uint64(nvprops,
3212 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3213 &volblocksize)) != 0 && error != ENOENT)
3214 return (SET_ERROR(EINVAL));
3217 volblocksize = zfs_prop_default_numeric(
3218 ZFS_PROP_VOLBLOCKSIZE);
3220 if ((error = zvol_check_volblocksize(
3221 volblocksize)) != 0 ||
3222 (error = zvol_check_volsize(volsize,
3223 volblocksize)) != 0)
3225 } else if (type == DMU_OST_ZFS) {
3229 * We have to have normalization and
3230 * case-folding flags correct when we do the
3231 * file system creation, so go figure them out
3234 VERIFY(nvlist_alloc(&zct.zct_zplprops,
3235 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3236 error = zfs_fill_zplprops(fsname, nvprops,
3237 zct.zct_zplprops, &is_insensitive);
3239 nvlist_free(zct.zct_zplprops);
3244 error = dmu_objset_create(fsname, type,
3245 is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
3246 nvlist_free(zct.zct_zplprops);
3249 * It would be nice to do this atomically.
3252 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3255 (void) dsl_destroy_head(fsname);
3258 if (error == 0 && type == DMU_OST_ZVOL)
3259 zvol_create_minors(fsname);
3266 * "origin" -> name of origin snapshot
3267 * (optional) "props" -> { prop -> value }
3270 * outnvl: propname -> error code (int32)
3273 zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3276 nvlist_t *nvprops = NULL;
3279 if (nvlist_lookup_string(innvl, "origin", &origin_name) != 0)
3280 return (SET_ERROR(EINVAL));
3281 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3283 if (strchr(fsname, '@') ||
3284 strchr(fsname, '%'))
3285 return (SET_ERROR(EINVAL));
3287 if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3288 return (SET_ERROR(EINVAL));
3289 error = dmu_objset_clone(fsname, origin_name);
3294 * It would be nice to do this atomically.
3297 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3300 (void) dsl_destroy_head(fsname);
3307 * "snaps" -> { snapshot1, snapshot2 }
3308 * (optional) "props" -> { prop -> value (string) }
3311 * outnvl: snapshot -> error code (int32)
3314 zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3317 nvlist_t *props = NULL;
3321 (void) nvlist_lookup_nvlist(innvl, "props", &props);
3322 if ((error = zfs_check_userprops(poolname, props)) != 0)
3325 if (!nvlist_empty(props) &&
3326 zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3327 return (SET_ERROR(ENOTSUP));
3329 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3330 return (SET_ERROR(EINVAL));
3331 poollen = strlen(poolname);
3332 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3333 pair = nvlist_next_nvpair(snaps, pair)) {
3334 const char *name = nvpair_name(pair);
3335 const char *cp = strchr(name, '@');
3338 * The snap name must contain an @, and the part after it must
3339 * contain only valid characters.
3341 if (cp == NULL || snapshot_namecheck(cp + 1, NULL, NULL) != 0)
3342 return (SET_ERROR(EINVAL));
3345 * The snap must be in the specified pool.
3347 if (strncmp(name, poolname, poollen) != 0 ||
3348 (name[poollen] != '/' && name[poollen] != '@'))
3349 return (SET_ERROR(EXDEV));
3351 /* This must be the only snap of this fs. */
3352 for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
3353 pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
3354 if (strncmp(name, nvpair_name(pair2), cp - name + 1)
3356 return (SET_ERROR(EXDEV));
3361 error = dsl_dataset_snapshot(snaps, props, outnvl);
3366 * innvl: "message" -> string
3370 zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
3378 * The poolname in the ioctl is not set, we get it from the TSD,
3379 * which was set at the end of the last successful ioctl that allows
3380 * logging. The secpolicy func already checked that it is set.
3381 * Only one log ioctl is allowed after each successful ioctl, so
3382 * we clear the TSD here.
3384 poolname = tsd_get(zfs_allow_log_key);
3385 (void) tsd_set(zfs_allow_log_key, NULL);
3386 error = spa_open(poolname, &spa, FTAG);
3391 if (nvlist_lookup_string(innvl, "message", &message) != 0) {
3392 spa_close(spa, FTAG);
3393 return (SET_ERROR(EINVAL));
3396 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
3397 spa_close(spa, FTAG);
3398 return (SET_ERROR(ENOTSUP));
3401 error = spa_history_log(spa, message);
3402 spa_close(spa, FTAG);
3407 * The dp_config_rwlock must not be held when calling this, because the
3408 * unmount may need to write out data.
3410 * This function is best-effort. Callers must deal gracefully if it
3411 * remains mounted (or is remounted after this call).
3414 zfs_unmount_snap(const char *snapname)
3419 if (strchr(snapname, '@') == NULL)
3422 vfsp = zfs_get_vfs(snapname);
3426 zfsvfs = vfsp->vfs_data;
3427 ASSERT(!dsl_pool_config_held(dmu_objset_pool(zfsvfs->z_os)));
3429 if (vn_vfswlock(vfsp->vfs_vnodecovered) != 0) {
3436 * Always force the unmount for snapshots.
3440 (void) dounmount(vfsp, MS_FORCE, kcred);
3442 mtx_lock(&Giant); /* dounmount() */
3443 dounmount(vfsp, MS_FORCE, curthread);
3444 mtx_unlock(&Giant); /* dounmount() */
3450 zfs_unmount_snap_cb(const char *snapname, void *arg)
3452 zfs_unmount_snap(snapname);
3457 * When a clone is destroyed, its origin may also need to be destroyed,
3458 * in which case it must be unmounted. This routine will do that unmount
3462 zfs_destroy_unmount_origin(const char *fsname)
3468 error = dmu_objset_hold(fsname, FTAG, &os);
3471 ds = dmu_objset_ds(os);
3472 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
3473 char originname[MAXNAMELEN];
3474 dsl_dataset_name(ds->ds_prev, originname);
3475 dmu_objset_rele(os, FTAG);
3476 zfs_unmount_snap(originname);
3478 dmu_objset_rele(os, FTAG);
3484 * "snaps" -> { snapshot1, snapshot2 }
3485 * (optional boolean) "defer"
3488 * outnvl: snapshot -> error code (int32)
3492 zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3499 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3500 return (SET_ERROR(EINVAL));
3501 defer = nvlist_exists(innvl, "defer");
3503 poollen = strlen(poolname);
3504 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3505 pair = nvlist_next_nvpair(snaps, pair)) {
3506 const char *name = nvpair_name(pair);
3509 * The snap must be in the specified pool.
3511 if (strncmp(name, poolname, poollen) != 0 ||
3512 (name[poollen] != '/' && name[poollen] != '@'))
3513 return (SET_ERROR(EXDEV));
3515 zfs_unmount_snap(name);
3516 (void) zvol_remove_minor(name);
3519 return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
3524 * zc_name name of dataset to destroy
3525 * zc_objset_type type of objset
3526 * zc_defer_destroy mark for deferred destroy
3531 zfs_ioc_destroy(zfs_cmd_t *zc)
3534 if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS)
3535 zfs_unmount_snap(zc->zc_name);
3537 if (strchr(zc->zc_name, '@'))
3538 err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
3540 err = dsl_destroy_head(zc->zc_name);
3541 if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
3542 (void) zvol_remove_minor(zc->zc_name);
3548 * zc_name name of dataset to rollback (to most recent snapshot)
3553 zfs_ioc_rollback(zfs_cmd_t *zc)
3558 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
3559 error = zfs_suspend_fs(zfsvfs);
3563 error = dsl_dataset_rollback(zc->zc_name);
3564 resume_err = zfs_resume_fs(zfsvfs, zc->zc_name);
3565 error = error ? error : resume_err;
3567 VFS_RELE(zfsvfs->z_vfs);
3569 error = dsl_dataset_rollback(zc->zc_name);
3575 recursive_unmount(const char *fsname, void *arg)
3577 const char *snapname = arg;
3578 char fullname[MAXNAMELEN];
3580 (void) snprintf(fullname, sizeof (fullname), "%s@%s", fsname, snapname);
3581 zfs_unmount_snap(fullname);
3587 * zc_name old name of dataset
3588 * zc_value new name of dataset
3589 * zc_cookie recursive flag (only valid for snapshots)
3594 zfs_ioc_rename(zfs_cmd_t *zc)
3596 boolean_t recursive = zc->zc_cookie & 1;
3598 boolean_t allow_mounted = zc->zc_cookie & 2;
3602 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3603 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3604 strchr(zc->zc_value, '%'))
3605 return (SET_ERROR(EINVAL));
3607 at = strchr(zc->zc_name, '@');
3609 /* snaps must be in same fs */
3610 if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
3611 return (SET_ERROR(EXDEV));
3614 if (zc->zc_objset_type == DMU_OST_ZFS) {
3616 if (zc->zc_objset_type == DMU_OST_ZFS && allow_mounted) {
3618 int error = dmu_objset_find(zc->zc_name,
3619 recursive_unmount, at + 1,
3620 recursive ? DS_FIND_CHILDREN : 0);
3624 return (dsl_dataset_rename_snapshot(zc->zc_name,
3625 at + 1, strchr(zc->zc_value, '@') + 1, recursive));
3628 if (zc->zc_objset_type == DMU_OST_ZVOL)
3629 (void) zvol_remove_minor(zc->zc_name);
3631 return (dsl_dir_rename(zc->zc_name, zc->zc_value));
3636 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
3638 const char *propname = nvpair_name(pair);
3639 boolean_t issnap = (strchr(dsname, '@') != NULL);
3640 zfs_prop_t prop = zfs_name_to_prop(propname);
3644 if (prop == ZPROP_INVAL) {
3645 if (zfs_prop_user(propname)) {
3646 if (err = zfs_secpolicy_write_perms(dsname,
3647 ZFS_DELEG_PERM_USERPROP, cr))
3652 if (!issnap && zfs_prop_userquota(propname)) {
3653 const char *perm = NULL;
3654 const char *uq_prefix =
3655 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
3656 const char *gq_prefix =
3657 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
3659 if (strncmp(propname, uq_prefix,
3660 strlen(uq_prefix)) == 0) {
3661 perm = ZFS_DELEG_PERM_USERQUOTA;
3662 } else if (strncmp(propname, gq_prefix,
3663 strlen(gq_prefix)) == 0) {
3664 perm = ZFS_DELEG_PERM_GROUPQUOTA;
3666 /* USERUSED and GROUPUSED are read-only */
3667 return (SET_ERROR(EINVAL));
3670 if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
3675 return (SET_ERROR(EINVAL));
3679 return (SET_ERROR(EINVAL));
3681 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
3683 * dsl_prop_get_all_impl() returns properties in this
3687 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
3688 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3693 * Check that this value is valid for this pool version
3696 case ZFS_PROP_COMPRESSION:
3698 * If the user specified gzip compression, make sure
3699 * the SPA supports it. We ignore any errors here since
3700 * we'll catch them later.
3702 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3703 nvpair_value_uint64(pair, &intval) == 0) {
3704 if (intval >= ZIO_COMPRESS_GZIP_1 &&
3705 intval <= ZIO_COMPRESS_GZIP_9 &&
3706 zfs_earlier_version(dsname,
3707 SPA_VERSION_GZIP_COMPRESSION)) {
3708 return (SET_ERROR(ENOTSUP));
3711 if (intval == ZIO_COMPRESS_ZLE &&
3712 zfs_earlier_version(dsname,
3713 SPA_VERSION_ZLE_COMPRESSION))
3714 return (SET_ERROR(ENOTSUP));
3716 if (intval == ZIO_COMPRESS_LZ4) {
3717 zfeature_info_t *feature =
3719 SPA_FEATURE_LZ4_COMPRESS];
3722 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3725 if (!spa_feature_is_enabled(spa, feature)) {
3726 spa_close(spa, FTAG);
3727 return (SET_ERROR(ENOTSUP));
3729 spa_close(spa, FTAG);
3733 * If this is a bootable dataset then
3734 * verify that the compression algorithm
3735 * is supported for booting. We must return
3736 * something other than ENOTSUP since it
3737 * implies a downrev pool version.
3739 if (zfs_is_bootfs(dsname) &&
3740 !BOOTFS_COMPRESS_VALID(intval)) {
3741 return (SET_ERROR(ERANGE));
3746 case ZFS_PROP_COPIES:
3747 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
3748 return (SET_ERROR(ENOTSUP));
3751 case ZFS_PROP_DEDUP:
3752 if (zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
3753 return (SET_ERROR(ENOTSUP));
3756 case ZFS_PROP_SHARESMB:
3757 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
3758 return (SET_ERROR(ENOTSUP));
3761 case ZFS_PROP_ACLINHERIT:
3762 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3763 nvpair_value_uint64(pair, &intval) == 0) {
3764 if (intval == ZFS_ACL_PASSTHROUGH_X &&
3765 zfs_earlier_version(dsname,
3766 SPA_VERSION_PASSTHROUGH_X))
3767 return (SET_ERROR(ENOTSUP));
3772 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
3776 * Checks for a race condition to make sure we don't increment a feature flag
3780 zfs_prop_activate_feature_check(void *arg, dmu_tx_t *tx)
3782 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
3783 zfeature_info_t *feature = arg;
3785 if (!spa_feature_is_active(spa, feature))
3788 return (SET_ERROR(EBUSY));
3792 * The callback invoked on feature activation in the sync task caused by
3793 * zfs_prop_activate_feature.
3796 zfs_prop_activate_feature_sync(void *arg, dmu_tx_t *tx)
3798 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
3799 zfeature_info_t *feature = arg;
3801 spa_feature_incr(spa, feature, tx);
3805 * Activates a feature on a pool in response to a property setting. This
3806 * creates a new sync task which modifies the pool to reflect the feature
3810 zfs_prop_activate_feature(spa_t *spa, zfeature_info_t *feature)
3814 /* EBUSY here indicates that the feature is already active */
3815 err = dsl_sync_task(spa_name(spa),
3816 zfs_prop_activate_feature_check, zfs_prop_activate_feature_sync,
3819 if (err != 0 && err != EBUSY)
3826 * Removes properties from the given props list that fail permission checks
3827 * needed to clear them and to restore them in case of a receive error. For each
3828 * property, make sure we have both set and inherit permissions.
3830 * Returns the first error encountered if any permission checks fail. If the
3831 * caller provides a non-NULL errlist, it also gives the complete list of names
3832 * of all the properties that failed a permission check along with the
3833 * corresponding error numbers. The caller is responsible for freeing the
3836 * If every property checks out successfully, zero is returned and the list
3837 * pointed at by errlist is NULL.
3840 zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
3843 nvpair_t *pair, *next_pair;
3850 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3852 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
3853 (void) strcpy(zc->zc_name, dataset);
3854 pair = nvlist_next_nvpair(props, NULL);
3855 while (pair != NULL) {
3856 next_pair = nvlist_next_nvpair(props, pair);
3858 (void) strcpy(zc->zc_value, nvpair_name(pair));
3859 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
3860 (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
3861 VERIFY(nvlist_remove_nvpair(props, pair) == 0);
3862 VERIFY(nvlist_add_int32(errors,
3863 zc->zc_value, err) == 0);
3867 kmem_free(zc, sizeof (zfs_cmd_t));
3869 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
3870 nvlist_free(errors);
3873 VERIFY(nvpair_value_int32(pair, &rv) == 0);
3876 if (errlist == NULL)
3877 nvlist_free(errors);
3885 propval_equals(nvpair_t *p1, nvpair_t *p2)
3887 if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
3888 /* dsl_prop_get_all_impl() format */
3890 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
3891 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3895 if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
3897 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
3898 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3902 if (nvpair_type(p1) != nvpair_type(p2))
3905 if (nvpair_type(p1) == DATA_TYPE_STRING) {
3906 char *valstr1, *valstr2;
3908 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
3909 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
3910 return (strcmp(valstr1, valstr2) == 0);
3912 uint64_t intval1, intval2;
3914 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
3915 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
3916 return (intval1 == intval2);
3921 * Remove properties from props if they are not going to change (as determined
3922 * by comparison with origprops). Remove them from origprops as well, since we
3923 * do not need to clear or restore properties that won't change.
3926 props_reduce(nvlist_t *props, nvlist_t *origprops)
3928 nvpair_t *pair, *next_pair;
3930 if (origprops == NULL)
3931 return; /* all props need to be received */
3933 pair = nvlist_next_nvpair(props, NULL);
3934 while (pair != NULL) {
3935 const char *propname = nvpair_name(pair);
3938 next_pair = nvlist_next_nvpair(props, pair);
3940 if ((nvlist_lookup_nvpair(origprops, propname,
3941 &match) != 0) || !propval_equals(pair, match))
3942 goto next; /* need to set received value */
3944 /* don't clear the existing received value */
3945 (void) nvlist_remove_nvpair(origprops, match);
3946 /* don't bother receiving the property */
3947 (void) nvlist_remove_nvpair(props, pair);
3954 static boolean_t zfs_ioc_recv_inject_err;
3959 * zc_name name of containing filesystem
3960 * zc_nvlist_src{_size} nvlist of properties to apply
3961 * zc_value name of snapshot to create
3962 * zc_string name of clone origin (if DRR_FLAG_CLONE)
3963 * zc_cookie file descriptor to recv from
3964 * zc_begin_record the BEGIN record of the stream (not byteswapped)
3965 * zc_guid force flag
3966 * zc_cleanup_fd cleanup-on-exit file descriptor
3967 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
3970 * zc_cookie number of bytes read
3971 * zc_nvlist_dst{_size} error for each unapplied received property
3972 * zc_obj zprop_errflags_t
3973 * zc_action_handle handle for this guid/ds mapping
3976 zfs_ioc_recv(zfs_cmd_t *zc)
3979 dmu_recv_cookie_t drc;
3980 boolean_t force = (boolean_t)zc->zc_guid;
3983 int props_error = 0;
3986 nvlist_t *props = NULL; /* sent properties */
3987 nvlist_t *origprops = NULL; /* existing properties */
3988 char *origin = NULL;
3990 char tofs[ZFS_MAXNAMELEN];
3991 boolean_t first_recvd_props = B_FALSE;
3993 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3994 strchr(zc->zc_value, '@') == NULL ||
3995 strchr(zc->zc_value, '%'))
3996 return (SET_ERROR(EINVAL));
3998 (void) strcpy(tofs, zc->zc_value);
3999 tosnap = strchr(tofs, '@');
4002 if (zc->zc_nvlist_src != 0 &&
4003 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
4004 zc->zc_iflags, &props)) != 0)
4011 return (SET_ERROR(EBADF));
4014 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4016 if (zc->zc_string[0])
4017 origin = zc->zc_string;
4019 error = dmu_recv_begin(tofs, tosnap,
4020 &zc->zc_begin_record, force, origin, &drc);
4025 * Set properties before we receive the stream so that they are applied
4026 * to the new data. Note that we must call dmu_recv_stream() if
4027 * dmu_recv_begin() succeeds.
4029 if (props != NULL && !drc.drc_newfs) {
4030 if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
4031 SPA_VERSION_RECVD_PROPS &&
4032 !dsl_prop_get_hasrecvd(tofs))
4033 first_recvd_props = B_TRUE;
4036 * If new received properties are supplied, they are to
4037 * completely replace the existing received properties, so stash
4038 * away the existing ones.
4040 if (dsl_prop_get_received(tofs, &origprops) == 0) {
4041 nvlist_t *errlist = NULL;
4043 * Don't bother writing a property if its value won't
4044 * change (and avoid the unnecessary security checks).
4046 * The first receive after SPA_VERSION_RECVD_PROPS is a
4047 * special case where we blow away all local properties
4050 if (!first_recvd_props)
4051 props_reduce(props, origprops);
4052 if (zfs_check_clearable(tofs, origprops, &errlist) != 0)
4053 (void) nvlist_merge(errors, errlist, 0);
4054 nvlist_free(errlist);
4056 if (clear_received_props(tofs, origprops,
4057 first_recvd_props ? NULL : props) != 0)
4058 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4060 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4064 if (props != NULL) {
4065 props_error = dsl_prop_set_hasrecvd(tofs);
4067 if (props_error == 0) {
4068 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4073 if (zc->zc_nvlist_dst_size != 0 &&
4074 (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
4075 put_nvlist(zc, errors) != 0)) {
4077 * Caller made zc->zc_nvlist_dst less than the minimum expected
4078 * size or supplied an invalid address.
4080 props_error = SET_ERROR(EINVAL);
4084 error = dmu_recv_stream(&drc, fp, &off, zc->zc_cleanup_fd,
4085 &zc->zc_action_handle);
4088 zfsvfs_t *zfsvfs = NULL;
4090 if (getzfsvfs(tofs, &zfsvfs) == 0) {
4094 error = zfs_suspend_fs(zfsvfs);
4096 * If the suspend fails, then the recv_end will
4097 * likely also fail, and clean up after itself.
4099 end_err = dmu_recv_end(&drc);
4101 error = zfs_resume_fs(zfsvfs, tofs);
4102 error = error ? error : end_err;
4103 VFS_RELE(zfsvfs->z_vfs);
4105 error = dmu_recv_end(&drc);
4109 zc->zc_cookie = off - fp->f_offset;
4110 if (off >= 0 && off <= MAXOFFSET_T)
4114 if (zfs_ioc_recv_inject_err) {
4115 zfs_ioc_recv_inject_err = B_FALSE;
4122 zvol_create_minors(tofs);
4126 * On error, restore the original props.
4128 if (error != 0 && props != NULL && !drc.drc_newfs) {
4129 if (clear_received_props(tofs, props, NULL) != 0) {
4131 * We failed to clear the received properties.
4132 * Since we may have left a $recvd value on the
4133 * system, we can't clear the $hasrecvd flag.
4135 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4136 } else if (first_recvd_props) {
4137 dsl_prop_unset_hasrecvd(tofs);
4140 if (origprops == NULL && !drc.drc_newfs) {
4141 /* We failed to stash the original properties. */
4142 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4146 * dsl_props_set() will not convert RECEIVED to LOCAL on or
4147 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
4148 * explictly if we're restoring local properties cleared in the
4149 * first new-style receive.
4151 if (origprops != NULL &&
4152 zfs_set_prop_nvlist(tofs, (first_recvd_props ?
4153 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
4154 origprops, NULL) != 0) {
4156 * We stashed the original properties but failed to
4159 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4164 nvlist_free(origprops);
4165 nvlist_free(errors);
4169 error = props_error;
4176 * zc_name name of snapshot to send
4177 * zc_cookie file descriptor to send stream to
4178 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
4179 * zc_sendobj objsetid of snapshot to send
4180 * zc_fromobj objsetid of incremental fromsnap (may be zero)
4181 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
4182 * output size in zc_objset_type.
4187 zfs_ioc_send(zfs_cmd_t *zc)
4191 boolean_t estimate = (zc->zc_guid != 0);
4193 if (zc->zc_obj != 0) {
4195 dsl_dataset_t *tosnap;
4197 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4201 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
4203 dsl_pool_rele(dp, FTAG);
4207 if (dsl_dir_is_clone(tosnap->ds_dir))
4208 zc->zc_fromobj = tosnap->ds_dir->dd_phys->dd_origin_obj;
4209 dsl_dataset_rele(tosnap, FTAG);
4210 dsl_pool_rele(dp, FTAG);
4215 dsl_dataset_t *tosnap;
4216 dsl_dataset_t *fromsnap = NULL;
4218 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4222 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
4224 dsl_pool_rele(dp, FTAG);
4228 if (zc->zc_fromobj != 0) {
4229 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
4232 dsl_dataset_rele(tosnap, FTAG);
4233 dsl_pool_rele(dp, FTAG);
4238 error = dmu_send_estimate(tosnap, fromsnap,
4239 &zc->zc_objset_type);
4241 if (fromsnap != NULL)
4242 dsl_dataset_rele(fromsnap, FTAG);
4243 dsl_dataset_rele(tosnap, FTAG);
4244 dsl_pool_rele(dp, FTAG);
4246 file_t *fp = getf(zc->zc_cookie);
4248 return (SET_ERROR(EBADF));
4251 error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
4253 zc->zc_fromobj, zc->zc_cookie, fp->f_vnode, &off);
4255 zc->zc_fromobj, zc->zc_cookie, fp, &off);
4258 if (off >= 0 && off <= MAXOFFSET_T)
4260 releasef(zc->zc_cookie);
4267 * zc_name name of snapshot on which to report progress
4268 * zc_cookie file descriptor of send stream
4271 * zc_cookie number of bytes written in send stream thus far
4274 zfs_ioc_send_progress(zfs_cmd_t *zc)
4278 dmu_sendarg_t *dsp = NULL;
4281 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4285 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
4287 dsl_pool_rele(dp, FTAG);
4291 mutex_enter(&ds->ds_sendstream_lock);
4294 * Iterate over all the send streams currently active on this dataset.
4295 * If there's one which matches the specified file descriptor _and_ the
4296 * stream was started by the current process, return the progress of
4299 for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
4300 dsp = list_next(&ds->ds_sendstreams, dsp)) {
4301 if (dsp->dsa_outfd == zc->zc_cookie &&
4302 dsp->dsa_proc == curproc)
4307 zc->zc_cookie = *(dsp->dsa_off);
4309 error = SET_ERROR(ENOENT);
4311 mutex_exit(&ds->ds_sendstream_lock);
4312 dsl_dataset_rele(ds, FTAG);
4313 dsl_pool_rele(dp, FTAG);
4318 zfs_ioc_inject_fault(zfs_cmd_t *zc)
4322 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
4323 &zc->zc_inject_record);
4326 zc->zc_guid = (uint64_t)id;
4332 zfs_ioc_clear_fault(zfs_cmd_t *zc)
4334 return (zio_clear_fault((int)zc->zc_guid));
4338 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
4340 int id = (int)zc->zc_guid;
4343 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
4344 &zc->zc_inject_record);
4352 zfs_ioc_error_log(zfs_cmd_t *zc)
4356 size_t count = (size_t)zc->zc_nvlist_dst_size;
4358 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
4361 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
4364 zc->zc_nvlist_dst_size = count;
4366 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
4368 spa_close(spa, FTAG);
4374 zfs_ioc_clear(zfs_cmd_t *zc)
4381 * On zpool clear we also fix up missing slogs
4383 mutex_enter(&spa_namespace_lock);
4384 spa = spa_lookup(zc->zc_name);
4386 mutex_exit(&spa_namespace_lock);
4387 return (SET_ERROR(EIO));
4389 if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
4390 /* we need to let spa_open/spa_load clear the chains */
4391 spa_set_log_state(spa, SPA_LOG_CLEAR);
4393 spa->spa_last_open_failed = 0;
4394 mutex_exit(&spa_namespace_lock);
4396 if (zc->zc_cookie & ZPOOL_NO_REWIND) {
4397 error = spa_open(zc->zc_name, &spa, FTAG);
4400 nvlist_t *config = NULL;
4402 if (zc->zc_nvlist_src == 0)
4403 return (SET_ERROR(EINVAL));
4405 if ((error = get_nvlist(zc->zc_nvlist_src,
4406 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
4407 error = spa_open_rewind(zc->zc_name, &spa, FTAG,
4409 if (config != NULL) {
4412 if ((err = put_nvlist(zc, config)) != 0)
4414 nvlist_free(config);
4416 nvlist_free(policy);
4423 spa_vdev_state_enter(spa, SCL_NONE);
4425 if (zc->zc_guid == 0) {
4428 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
4430 (void) spa_vdev_state_exit(spa, NULL, ENODEV);
4431 spa_close(spa, FTAG);
4432 return (SET_ERROR(ENODEV));
4436 vdev_clear(spa, vd);
4438 (void) spa_vdev_state_exit(spa, NULL, 0);
4441 * Resume any suspended I/Os.
4443 if (zio_resume(spa) != 0)
4444 error = SET_ERROR(EIO);
4446 spa_close(spa, FTAG);
4452 zfs_ioc_pool_reopen(zfs_cmd_t *zc)
4457 error = spa_open(zc->zc_name, &spa, FTAG);
4461 spa_vdev_state_enter(spa, SCL_NONE);
4464 * If a resilver is already in progress then set the
4465 * spa_scrub_reopen flag to B_TRUE so that we don't restart
4466 * the scan as a side effect of the reopen. Otherwise, let
4467 * vdev_open() decided if a resilver is required.
4469 spa->spa_scrub_reopen = dsl_scan_resilvering(spa->spa_dsl_pool);
4470 vdev_reopen(spa->spa_root_vdev);
4471 spa->spa_scrub_reopen = B_FALSE;
4473 (void) spa_vdev_state_exit(spa, NULL, 0);
4474 spa_close(spa, FTAG);
4479 * zc_name name of filesystem
4480 * zc_value name of origin snapshot
4483 * zc_string name of conflicting snapshot, if there is one
4486 zfs_ioc_promote(zfs_cmd_t *zc)
4491 * We don't need to unmount *all* the origin fs's snapshots, but
4494 cp = strchr(zc->zc_value, '@');
4497 (void) dmu_objset_find(zc->zc_value,
4498 zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
4499 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
4503 * Retrieve a single {user|group}{used|quota}@... property.
4506 * zc_name name of filesystem
4507 * zc_objset_type zfs_userquota_prop_t
4508 * zc_value domain name (eg. "S-1-234-567-89")
4509 * zc_guid RID/UID/GID
4512 * zc_cookie property value
4515 zfs_ioc_userspace_one(zfs_cmd_t *zc)
4520 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
4521 return (SET_ERROR(EINVAL));
4523 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4527 error = zfs_userspace_one(zfsvfs,
4528 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
4529 zfsvfs_rele(zfsvfs, FTAG);
4536 * zc_name name of filesystem
4537 * zc_cookie zap cursor
4538 * zc_objset_type zfs_userquota_prop_t
4539 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
4542 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
4543 * zc_cookie zap cursor
4546 zfs_ioc_userspace_many(zfs_cmd_t *zc)
4549 int bufsize = zc->zc_nvlist_dst_size;
4552 return (SET_ERROR(ENOMEM));
4554 int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4558 void *buf = kmem_alloc(bufsize, KM_SLEEP);
4560 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
4561 buf, &zc->zc_nvlist_dst_size);
4564 error = ddi_copyout(buf,
4565 (void *)(uintptr_t)zc->zc_nvlist_dst,
4566 zc->zc_nvlist_dst_size, zc->zc_iflags);
4568 kmem_free(buf, bufsize);
4569 zfsvfs_rele(zfsvfs, FTAG);
4576 * zc_name name of filesystem
4582 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
4588 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
4589 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
4591 * If userused is not enabled, it may be because the
4592 * objset needs to be closed & reopened (to grow the
4593 * objset_phys_t). Suspend/resume the fs will do that.
4595 error = zfs_suspend_fs(zfsvfs);
4597 error = zfs_resume_fs(zfsvfs, zc->zc_name);
4600 error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
4601 VFS_RELE(zfsvfs->z_vfs);
4603 /* XXX kind of reading contents without owning */
4604 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4608 error = dmu_objset_userspace_upgrade(os);
4609 dmu_objset_rele(os, FTAG);
4617 * We don't want to have a hard dependency
4618 * against some special symbols in sharefs
4619 * nfs, and smbsrv. Determine them if needed when
4620 * the first file system is shared.
4621 * Neither sharefs, nfs or smbsrv are unloadable modules.
4623 int (*znfsexport_fs)(void *arg);
4624 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
4625 int (*zsmbexport_fs)(void *arg, boolean_t add_share);
4627 int zfs_nfsshare_inited;
4628 int zfs_smbshare_inited;
4630 ddi_modhandle_t nfs_mod;
4631 ddi_modhandle_t sharefs_mod;
4632 ddi_modhandle_t smbsrv_mod;
4634 kmutex_t zfs_share_lock;
4642 ASSERT(MUTEX_HELD(&zfs_share_lock));
4643 /* Both NFS and SMB shares also require sharetab support. */
4644 if (sharefs_mod == NULL && ((sharefs_mod =
4645 ddi_modopen("fs/sharefs",
4646 KRTLD_MODE_FIRST, &error)) == NULL)) {
4647 return (SET_ERROR(ENOSYS));
4649 if (zshare_fs == NULL && ((zshare_fs =
4650 (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
4651 ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
4652 return (SET_ERROR(ENOSYS));
4659 zfs_ioc_share(zfs_cmd_t *zc)
4665 switch (zc->zc_share.z_sharetype) {
4667 case ZFS_UNSHARE_NFS:
4668 if (zfs_nfsshare_inited == 0) {
4669 mutex_enter(&zfs_share_lock);
4670 if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
4671 KRTLD_MODE_FIRST, &error)) == NULL)) {
4672 mutex_exit(&zfs_share_lock);
4673 return (SET_ERROR(ENOSYS));
4675 if (znfsexport_fs == NULL &&
4676 ((znfsexport_fs = (int (*)(void *))
4678 "nfs_export", &error)) == NULL)) {
4679 mutex_exit(&zfs_share_lock);
4680 return (SET_ERROR(ENOSYS));
4682 error = zfs_init_sharefs();
4684 mutex_exit(&zfs_share_lock);
4685 return (SET_ERROR(ENOSYS));
4687 zfs_nfsshare_inited = 1;
4688 mutex_exit(&zfs_share_lock);
4692 case ZFS_UNSHARE_SMB:
4693 if (zfs_smbshare_inited == 0) {
4694 mutex_enter(&zfs_share_lock);
4695 if (smbsrv_mod == NULL && ((smbsrv_mod =
4696 ddi_modopen("drv/smbsrv",
4697 KRTLD_MODE_FIRST, &error)) == NULL)) {
4698 mutex_exit(&zfs_share_lock);
4699 return (SET_ERROR(ENOSYS));
4701 if (zsmbexport_fs == NULL && ((zsmbexport_fs =
4702 (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
4703 "smb_server_share", &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_smbshare_inited = 1;
4713 mutex_exit(&zfs_share_lock);
4717 return (SET_ERROR(EINVAL));
4720 switch (zc->zc_share.z_sharetype) {
4722 case ZFS_UNSHARE_NFS:
4724 znfsexport_fs((void *)
4725 (uintptr_t)zc->zc_share.z_exportdata))
4729 case ZFS_UNSHARE_SMB:
4730 if (error = zsmbexport_fs((void *)
4731 (uintptr_t)zc->zc_share.z_exportdata,
4732 zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
4739 opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
4740 zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
4741 SHAREFS_ADD : SHAREFS_REMOVE;
4744 * Add or remove share from sharetab
4746 error = zshare_fs(opcode,
4747 (void *)(uintptr_t)zc->zc_share.z_sharedata,
4748 zc->zc_share.z_sharemax);
4757 ace_t full_access[] = {
4758 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
4763 * zc_name name of containing filesystem
4764 * zc_obj object # beyond which we want next in-use object #
4767 * zc_obj next in-use object #
4770 zfs_ioc_next_obj(zfs_cmd_t *zc)
4772 objset_t *os = NULL;
4775 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4779 error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
4780 os->os_dsl_dataset->ds_phys->ds_prev_snap_txg);
4782 dmu_objset_rele(os, FTAG);
4788 * zc_name name of filesystem
4789 * zc_value prefix name for snapshot
4790 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
4793 * zc_value short name of new snapshot
4796 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
4803 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
4807 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
4808 (u_longlong_t)ddi_get_lbolt64());
4809 hold_name = kmem_asprintf("%%%s", zc->zc_value);
4811 error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
4814 (void) strcpy(zc->zc_value, snap_name);
4817 zfs_onexit_fd_rele(zc->zc_cleanup_fd);
4823 * zc_name name of "to" snapshot
4824 * zc_value name of "from" snapshot
4825 * zc_cookie file descriptor to write diff data on
4828 * dmu_diff_record_t's to the file descriptor
4831 zfs_ioc_diff(zfs_cmd_t *zc)
4837 fp = getf(zc->zc_cookie);
4839 return (SET_ERROR(EBADF));
4844 error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off);
4846 error = dmu_diff(zc->zc_name, zc->zc_value, fp, &off);
4849 if (off >= 0 && off <= MAXOFFSET_T)
4851 releasef(zc->zc_cookie);
4858 * Remove all ACL files in shares dir
4861 zfs_smb_acl_purge(znode_t *dzp)
4864 zap_attribute_t zap;
4865 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4868 for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
4869 (error = zap_cursor_retrieve(&zc, &zap)) == 0;
4870 zap_cursor_advance(&zc)) {
4871 if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
4875 zap_cursor_fini(&zc);
4881 zfs_ioc_smb_acl(zfs_cmd_t *zc)
4886 vnode_t *resourcevp = NULL;
4895 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
4896 NO_FOLLOW, NULL, &vp)) != 0)
4899 /* Now make sure mntpnt and dataset are ZFS */
4901 if (strcmp(vp->v_vfsp->mnt_stat.f_fstypename, "zfs") != 0 ||
4902 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
4903 zc->zc_name) != 0)) {
4905 return (SET_ERROR(EINVAL));
4909 zfsvfs = dzp->z_zfsvfs;
4913 * Create share dir if its missing.
4915 mutex_enter(&zfsvfs->z_lock);
4916 if (zfsvfs->z_shares_dir == 0) {
4919 tx = dmu_tx_create(zfsvfs->z_os);
4920 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
4922 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
4923 error = dmu_tx_assign(tx, TXG_WAIT);
4927 error = zfs_create_share_dir(zfsvfs, tx);
4931 mutex_exit(&zfsvfs->z_lock);
4937 mutex_exit(&zfsvfs->z_lock);
4939 ASSERT(zfsvfs->z_shares_dir);
4940 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
4946 switch (zc->zc_cookie) {
4947 case ZFS_SMB_ACL_ADD:
4948 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
4949 vattr.va_type = VREG;
4950 vattr.va_mode = S_IFREG|0777;
4954 vsec.vsa_mask = VSA_ACE;
4955 vsec.vsa_aclentp = &full_access;
4956 vsec.vsa_aclentsz = sizeof (full_access);
4957 vsec.vsa_aclcnt = 1;
4959 error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
4960 &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
4962 VN_RELE(resourcevp);
4965 case ZFS_SMB_ACL_REMOVE:
4966 error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
4970 case ZFS_SMB_ACL_RENAME:
4971 if ((error = get_nvlist(zc->zc_nvlist_src,
4972 zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
4977 if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
4978 nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
4981 VN_RELE(ZTOV(sharedir));
4983 nvlist_free(nvlist);
4986 error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
4988 nvlist_free(nvlist);
4991 case ZFS_SMB_ACL_PURGE:
4992 error = zfs_smb_acl_purge(sharedir);
4996 error = SET_ERROR(EINVAL);
5001 VN_RELE(ZTOV(sharedir));
5007 return (EOPNOTSUPP);
5013 * "holds" -> { snapname -> holdname (string), ... }
5014 * (optional) "cleanup_fd" -> fd (int32)
5018 * snapname -> error value (int32)
5024 zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
5027 int cleanup_fd = -1;
5031 error = nvlist_lookup_nvlist(args, "holds", &holds);
5033 return (SET_ERROR(EINVAL));
5035 if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
5036 error = zfs_onexit_fd_hold(cleanup_fd, &minor);
5041 error = dsl_dataset_user_hold(holds, minor, errlist);
5043 zfs_onexit_fd_rele(cleanup_fd);
5048 * innvl is not used.
5051 * holdname -> time added (uint64 seconds since epoch)
5057 zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
5059 return (dsl_dataset_get_holds(snapname, outnvl));
5064 * snapname -> { holdname, ... }
5069 * snapname -> error value (int32)
5075 zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
5080 * The release may cause the snapshot to be destroyed; make sure it
5083 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
5084 pair = nvlist_next_nvpair(holds, pair))
5085 zfs_unmount_snap(nvpair_name(pair));
5087 return (dsl_dataset_user_release(holds, errlist));
5092 * zc_name name of new filesystem or snapshot
5093 * zc_value full name of old snapshot
5096 * zc_cookie space in bytes
5097 * zc_objset_type compressed space in bytes
5098 * zc_perm_action uncompressed space in bytes
5101 zfs_ioc_space_written(zfs_cmd_t *zc)
5105 dsl_dataset_t *new, *old;
5107 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5110 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
5112 dsl_pool_rele(dp, FTAG);
5115 error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
5117 dsl_dataset_rele(new, FTAG);
5118 dsl_pool_rele(dp, FTAG);
5122 error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
5123 &zc->zc_objset_type, &zc->zc_perm_action);
5124 dsl_dataset_rele(old, FTAG);
5125 dsl_dataset_rele(new, FTAG);
5126 dsl_pool_rele(dp, FTAG);
5132 * "firstsnap" -> snapshot name
5136 * "used" -> space in bytes
5137 * "compressed" -> compressed space in bytes
5138 * "uncompressed" -> uncompressed space in bytes
5142 zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
5146 dsl_dataset_t *new, *old;
5148 uint64_t used, comp, uncomp;
5150 if (nvlist_lookup_string(innvl, "firstsnap", &firstsnap) != 0)
5151 return (SET_ERROR(EINVAL));
5153 error = dsl_pool_hold(lastsnap, FTAG, &dp);
5157 error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
5159 dsl_pool_rele(dp, FTAG);
5162 error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
5164 dsl_dataset_rele(new, FTAG);
5165 dsl_pool_rele(dp, FTAG);
5169 error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
5170 dsl_dataset_rele(old, FTAG);
5171 dsl_dataset_rele(new, FTAG);
5172 dsl_pool_rele(dp, FTAG);
5173 fnvlist_add_uint64(outnvl, "used", used);
5174 fnvlist_add_uint64(outnvl, "compressed", comp);
5175 fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
5180 zfs_ioc_jail(zfs_cmd_t *zc)
5183 return (zone_dataset_attach(curthread->td_ucred, zc->zc_name,
5184 (int)zc->zc_jailid));
5188 zfs_ioc_unjail(zfs_cmd_t *zc)
5191 return (zone_dataset_detach(curthread->td_ucred, zc->zc_name,
5192 (int)zc->zc_jailid));
5197 * "fd" -> file descriptor to write stream to (int32)
5198 * (optional) "fromsnap" -> full snap name to send an incremental from
5205 zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5209 char *fromname = NULL;
5212 error = nvlist_lookup_int32(innvl, "fd", &fd);
5214 return (SET_ERROR(EINVAL));
5216 (void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
5218 file_t *fp = getf(fd);
5220 return (SET_ERROR(EBADF));
5224 error = dmu_send(snapname, fromname, fd, fp->f_vnode, &off);
5226 error = dmu_send(snapname, fromname, fd, fp, &off);
5230 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5241 * Determine approximately how large a zfs send stream will be -- the number
5242 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
5245 * (optional) "fromsnap" -> full snap name to send an incremental from
5249 * "space" -> bytes of space (uint64)
5253 zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5256 dsl_dataset_t *fromsnap = NULL;
5257 dsl_dataset_t *tosnap;
5262 error = dsl_pool_hold(snapname, FTAG, &dp);
5266 error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
5268 dsl_pool_rele(dp, FTAG);
5272 error = nvlist_lookup_string(innvl, "fromsnap", &fromname);
5274 error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
5276 dsl_dataset_rele(tosnap, FTAG);
5277 dsl_pool_rele(dp, FTAG);
5282 error = dmu_send_estimate(tosnap, fromsnap, &space);
5283 fnvlist_add_uint64(outnvl, "space", space);
5285 if (fromsnap != NULL)
5286 dsl_dataset_rele(fromsnap, FTAG);
5287 dsl_dataset_rele(tosnap, FTAG);
5288 dsl_pool_rele(dp, FTAG);
5293 static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
5296 zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5297 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
5298 boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
5300 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
5302 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
5303 ASSERT3U(ioc, <, ZFS_IOC_LAST);
5304 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
5305 ASSERT3P(vec->zvec_func, ==, NULL);
5307 vec->zvec_legacy_func = func;
5308 vec->zvec_secpolicy = secpolicy;
5309 vec->zvec_namecheck = namecheck;
5310 vec->zvec_allow_log = log_history;
5311 vec->zvec_pool_check = pool_check;
5315 * See the block comment at the beginning of this file for details on
5316 * each argument to this function.
5319 zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
5320 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
5321 zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
5322 boolean_t allow_log)
5324 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
5326 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
5327 ASSERT3U(ioc, <, ZFS_IOC_LAST);
5328 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
5329 ASSERT3P(vec->zvec_func, ==, NULL);
5331 /* if we are logging, the name must be valid */
5332 ASSERT(!allow_log || namecheck != NO_NAME);
5334 vec->zvec_name = name;
5335 vec->zvec_func = func;
5336 vec->zvec_secpolicy = secpolicy;
5337 vec->zvec_namecheck = namecheck;
5338 vec->zvec_pool_check = pool_check;
5339 vec->zvec_smush_outnvlist = smush_outnvlist;
5340 vec->zvec_allow_log = allow_log;
5344 zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5345 zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
5346 zfs_ioc_poolcheck_t pool_check)
5348 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5349 POOL_NAME, log_history, pool_check);
5353 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5354 zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
5356 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5357 DATASET_NAME, B_FALSE, pool_check);
5361 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
5363 zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
5364 POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5368 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5369 zfs_secpolicy_func_t *secpolicy)
5371 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5372 NO_NAME, B_FALSE, POOL_CHECK_NONE);
5376 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
5377 zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
5379 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5380 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
5384 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
5386 zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
5387 zfs_secpolicy_read);
5391 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5392 zfs_secpolicy_func_t *secpolicy)
5394 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5395 DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5399 zfs_ioctl_init(void)
5401 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
5402 zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
5403 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5405 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
5406 zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
5407 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE);
5409 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
5410 zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
5411 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5413 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
5414 zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
5415 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5417 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
5418 zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
5419 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5421 zfs_ioctl_register("create", ZFS_IOC_CREATE,
5422 zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
5423 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5425 zfs_ioctl_register("clone", ZFS_IOC_CLONE,
5426 zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
5427 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5429 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
5430 zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
5431 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5433 zfs_ioctl_register("hold", ZFS_IOC_HOLD,
5434 zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
5435 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5436 zfs_ioctl_register("release", ZFS_IOC_RELEASE,
5437 zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
5438 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5440 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
5441 zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
5442 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5444 /* IOCTLS that use the legacy function signature */
5446 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
5447 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
5449 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
5450 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5451 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
5453 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
5454 zfs_ioc_pool_upgrade);
5455 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
5457 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
5458 zfs_ioc_vdev_remove);
5459 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
5460 zfs_ioc_vdev_set_state);
5461 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
5462 zfs_ioc_vdev_attach);
5463 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
5464 zfs_ioc_vdev_detach);
5465 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
5466 zfs_ioc_vdev_setpath);
5467 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
5468 zfs_ioc_vdev_setfru);
5469 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
5470 zfs_ioc_pool_set_props);
5471 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
5472 zfs_ioc_vdev_split);
5473 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
5474 zfs_ioc_pool_reguid);
5476 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
5477 zfs_ioc_pool_configs, zfs_secpolicy_none);
5478 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
5479 zfs_ioc_pool_tryimport, zfs_secpolicy_config);
5480 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
5481 zfs_ioc_inject_fault, zfs_secpolicy_inject);
5482 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
5483 zfs_ioc_clear_fault, zfs_secpolicy_inject);
5484 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
5485 zfs_ioc_inject_list_next, zfs_secpolicy_inject);
5488 * pool destroy, and export don't log the history as part of
5489 * zfsdev_ioctl, but rather zfs_ioc_pool_export
5490 * does the logging of those commands.
5492 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
5493 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
5494 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
5495 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
5497 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
5498 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
5499 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
5500 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
5502 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
5503 zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED);
5504 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
5505 zfs_ioc_dsobj_to_dsname,
5506 zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED);
5507 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
5508 zfs_ioc_pool_get_history,
5509 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
5511 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
5512 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5514 zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
5515 zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
5516 zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
5517 zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
5519 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
5520 zfs_ioc_space_written);
5521 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
5522 zfs_ioc_objset_recvd_props);
5523 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
5525 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
5527 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
5528 zfs_ioc_objset_stats);
5529 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
5530 zfs_ioc_objset_zplprops);
5531 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
5532 zfs_ioc_dataset_list_next);
5533 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
5534 zfs_ioc_snapshot_list_next);
5535 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
5536 zfs_ioc_send_progress);
5538 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
5539 zfs_ioc_diff, zfs_secpolicy_diff);
5540 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
5541 zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
5542 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
5543 zfs_ioc_obj_to_path, zfs_secpolicy_diff);
5544 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
5545 zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
5546 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
5547 zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
5548 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
5549 zfs_ioc_send, zfs_secpolicy_send);
5551 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
5552 zfs_secpolicy_none);
5553 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
5554 zfs_secpolicy_destroy);
5555 zfs_ioctl_register_dataset_modify(ZFS_IOC_ROLLBACK, zfs_ioc_rollback,
5556 zfs_secpolicy_rollback);
5557 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
5558 zfs_secpolicy_rename);
5559 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
5560 zfs_secpolicy_recv);
5561 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
5562 zfs_secpolicy_promote);
5563 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
5564 zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
5565 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
5566 zfs_secpolicy_set_fsacl);
5568 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
5569 zfs_secpolicy_share, POOL_CHECK_NONE);
5570 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
5571 zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
5572 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
5573 zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
5574 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5575 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
5576 zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
5577 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5580 zfs_ioctl_register_dataset_nolog(ZFS_IOC_JAIL, zfs_ioc_jail,
5581 zfs_secpolicy_config, POOL_CHECK_NONE);
5582 zfs_ioctl_register_dataset_nolog(ZFS_IOC_UNJAIL, zfs_ioc_unjail,
5583 zfs_secpolicy_config, POOL_CHECK_NONE);
5588 pool_status_check(const char *name, zfs_ioc_namecheck_t type,
5589 zfs_ioc_poolcheck_t check)
5594 ASSERT(type == POOL_NAME || type == DATASET_NAME);
5596 if (check & POOL_CHECK_NONE)
5599 error = spa_open(name, &spa, FTAG);
5601 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
5602 error = SET_ERROR(EAGAIN);
5603 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
5604 error = SET_ERROR(EROFS);
5605 spa_close(spa, FTAG);
5611 * Find a free minor number.
5614 zfsdev_minor_alloc(void)
5616 static minor_t last_minor;
5619 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5621 for (m = last_minor + 1; m != last_minor; m++) {
5622 if (m > ZFSDEV_MAX_MINOR)
5624 if (ddi_get_soft_state(zfsdev_state, m) == NULL) {
5634 zfs_ctldev_init(struct cdev *devp)
5637 zfs_soft_state_t *zs;
5639 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5641 minor = zfsdev_minor_alloc();
5643 return (SET_ERROR(ENXIO));
5645 if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS)
5646 return (SET_ERROR(EAGAIN));
5648 devfs_set_cdevpriv((void *)(uintptr_t)minor, zfsdev_close);
5650 zs = ddi_get_soft_state(zfsdev_state, minor);
5651 zs->zss_type = ZSST_CTLDEV;
5652 zfs_onexit_init((zfs_onexit_t **)&zs->zss_data);
5658 zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor)
5660 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5662 zfs_onexit_destroy(zo);
5663 ddi_soft_state_free(zfsdev_state, minor);
5667 zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which)
5669 zfs_soft_state_t *zp;
5671 zp = ddi_get_soft_state(zfsdev_state, minor);
5672 if (zp == NULL || zp->zss_type != which)
5675 return (zp->zss_data);
5679 zfsdev_open(struct cdev *devp, int flag, int mode, struct thread *td)
5684 if (getminor(*devp) != 0)
5685 return (zvol_open(devp, flag, otyp, cr));
5688 /* This is the control device. Allocate a new minor if requested. */
5690 mutex_enter(&spa_namespace_lock);
5691 error = zfs_ctldev_init(devp);
5692 mutex_exit(&spa_namespace_lock);
5699 zfsdev_close(void *data)
5702 minor_t minor = (minor_t)(uintptr_t)data;
5707 mutex_enter(&spa_namespace_lock);
5708 zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV);
5710 mutex_exit(&spa_namespace_lock);
5713 zfs_ctldev_destroy(zo, minor);
5714 mutex_exit(&spa_namespace_lock);
5718 zfsdev_ioctl(struct cdev *dev, u_long zcmd, caddr_t arg, int flag,
5725 minor_t minor = getminor(dev);
5727 zfs_iocparm_t *zc_iocparm;
5728 int cflag, cmd, oldvecnum;
5729 boolean_t newioc, compat;
5730 cred_t *cr = td->td_ucred;
5732 const zfs_ioc_vec_t *vec;
5733 char *saved_poolname = NULL;
5734 nvlist_t *innvl = NULL;
5736 cflag = ZFS_CMD_COMPAT_NONE;
5740 len = IOCPARM_LEN(zcmd);
5744 * Check if we are talking to supported older binaries
5745 * and translate zfs_cmd if necessary
5747 if (len != sizeof(zfs_iocparm_t)) {
5749 if (len == sizeof(zfs_cmd_t)) {
5750 cflag = ZFS_CMD_COMPAT_LZC;
5752 } else if (len == sizeof(zfs_cmd_deadman_t)) {
5753 cflag = ZFS_CMD_COMPAT_DEADMAN;
5756 } else if (len == sizeof(zfs_cmd_v28_t)) {
5757 cflag = ZFS_CMD_COMPAT_V28;
5760 } else if (len == sizeof(zfs_cmd_v15_t)) {
5761 cflag = ZFS_CMD_COMPAT_V15;
5763 vecnum = zfs_ioctl_v15_to_v28[cmd];
5770 vecnum = cmd - ZFS_IOC_FIRST;
5771 ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
5775 if (vecnum == ZFS_IOC_COMPAT_PASS)
5777 else if (vecnum == ZFS_IOC_COMPAT_FAIL)
5782 * Check if we have sufficient kernel memory allocated
5783 * for the zfs_cmd_t request. Bail out if not so we
5784 * will not access undefined memory region.
5786 if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
5787 return (SET_ERROR(EINVAL));
5788 vec = &zfs_ioc_vec[vecnum];
5791 zc = kmem_zalloc(sizeof(zfs_cmd_t), KM_SLEEP);
5792 bzero(zc, sizeof(zfs_cmd_t));
5794 error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
5796 error = SET_ERROR(EFAULT);
5799 #else /* !illumos */
5801 * We don't alloc/free zc only if talking to library ioctl version 2
5803 if (cflag != ZFS_CMD_COMPAT_LZC) {
5804 zc = kmem_zalloc(sizeof(zfs_cmd_t), KM_SLEEP);
5805 bzero(zc, sizeof(zfs_cmd_t));
5812 zc_iocparm = (void *)arg;
5813 if (zc_iocparm->zfs_cmd_size != sizeof(zfs_cmd_t)) {
5814 error = SET_ERROR(EFAULT);
5817 error = ddi_copyin((void *)(uintptr_t)zc_iocparm->zfs_cmd, zc,
5818 sizeof(zfs_cmd_t), flag);
5820 error = SET_ERROR(EFAULT);
5826 zfs_cmd_compat_get(zc, arg, cflag);
5828 error = zfs_ioctl_compat_pre(zc, &vecnum, cflag);
5831 if (oldvecnum != vecnum)
5832 vec = &zfs_ioc_vec[vecnum];
5834 #endif /* !illumos */
5836 zc->zc_iflags = flag & FKIOCTL;
5837 if (zc->zc_nvlist_src_size != 0) {
5838 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
5839 zc->zc_iflags, &innvl);
5844 /* rewrite innvl for backwards compatibility */
5846 innvl = zfs_ioctl_compat_innvl(zc, innvl, vecnum, cflag);
5849 * Ensure that all pool/dataset names are valid before we pass down to
5852 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
5853 switch (vec->zvec_namecheck) {
5855 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
5856 error = SET_ERROR(EINVAL);
5858 error = pool_status_check(zc->zc_name,
5859 vec->zvec_namecheck, vec->zvec_pool_check);
5863 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
5864 error = SET_ERROR(EINVAL);
5866 error = pool_status_check(zc->zc_name,
5867 vec->zvec_namecheck, vec->zvec_pool_check);
5874 if (error == 0 && !(flag & FKIOCTL))
5875 error = vec->zvec_secpolicy(zc, innvl, cr);
5880 /* legacy ioctls can modify zc_name */
5881 len = strcspn(zc->zc_name, "/@") + 1;
5882 saved_poolname = kmem_alloc(len, KM_SLEEP);
5883 (void) strlcpy(saved_poolname, zc->zc_name, len);
5885 if (vec->zvec_func != NULL) {
5889 nvlist_t *lognv = NULL;
5891 ASSERT(vec->zvec_legacy_func == NULL);
5894 * Add the innvl to the lognv before calling the func,
5895 * in case the func changes the innvl.
5897 if (vec->zvec_allow_log) {
5898 lognv = fnvlist_alloc();
5899 fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
5901 if (!nvlist_empty(innvl)) {
5902 fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
5907 outnvl = fnvlist_alloc();
5908 error = vec->zvec_func(zc->zc_name, innvl, outnvl);
5910 if (error == 0 && vec->zvec_allow_log &&
5911 spa_open(zc->zc_name, &spa, FTAG) == 0) {
5912 if (!nvlist_empty(outnvl)) {
5913 fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL,
5916 (void) spa_history_log_nvl(spa, lognv);
5917 spa_close(spa, FTAG);
5919 fnvlist_free(lognv);
5921 /* rewrite outnvl for backwards compatibility */
5922 if (cflag != ZFS_CMD_COMPAT_NONE && cflag != ZFS_CMD_COMPAT_LZC)
5923 outnvl = zfs_ioctl_compat_outnvl(zc, outnvl, vecnum,
5926 if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
5928 if (vec->zvec_smush_outnvlist) {
5929 smusherror = nvlist_smush(outnvl,
5930 zc->zc_nvlist_dst_size);
5932 if (smusherror == 0)
5933 puterror = put_nvlist(zc, outnvl);
5939 nvlist_free(outnvl);
5941 error = vec->zvec_legacy_func(zc);
5948 zfs_ioctl_compat_post(zc, cmd, cflag);
5949 zfs_cmd_compat_put(zc, arg, vecnum, cflag);
5953 rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
5954 if (error == 0 && rc != 0)
5955 error = SET_ERROR(EFAULT);
5958 rc = ddi_copyout(zc, (void *)(uintptr_t)zc_iocparm->zfs_cmd,
5959 sizeof (zfs_cmd_t), flag);
5960 if (error == 0 && rc != 0)
5961 error = SET_ERROR(EFAULT);
5964 if (error == 0 && vec->zvec_allow_log) {
5965 char *s = tsd_get(zfs_allow_log_key);
5968 (void) tsd_set(zfs_allow_log_key, saved_poolname);
5970 if (saved_poolname != NULL)
5971 strfree(saved_poolname);
5975 kmem_free(zc, sizeof (zfs_cmd_t));
5978 * We don't alloc/free zc only if talking to library ioctl version 2
5980 if (cflag != ZFS_CMD_COMPAT_LZC)
5981 kmem_free(zc, sizeof (zfs_cmd_t));
5988 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
5990 if (cmd != DDI_ATTACH)
5991 return (DDI_FAILURE);
5993 if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
5994 DDI_PSEUDO, 0) == DDI_FAILURE)
5995 return (DDI_FAILURE);
5999 ddi_report_dev(dip);
6001 return (DDI_SUCCESS);
6005 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
6007 if (spa_busy() || zfs_busy() || zvol_busy())
6008 return (DDI_FAILURE);
6010 if (cmd != DDI_DETACH)
6011 return (DDI_FAILURE);
6015 ddi_prop_remove_all(dip);
6016 ddi_remove_minor_node(dip, NULL);
6018 return (DDI_SUCCESS);
6023 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
6026 case DDI_INFO_DEVT2DEVINFO:
6028 return (DDI_SUCCESS);
6030 case DDI_INFO_DEVT2INSTANCE:
6031 *result = (void *)0;
6032 return (DDI_SUCCESS);
6035 return (DDI_FAILURE);
6040 * OK, so this is a little weird.
6042 * /dev/zfs is the control node, i.e. minor 0.
6043 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
6045 * /dev/zfs has basically nothing to do except serve up ioctls,
6046 * so most of the standard driver entry points are in zvol.c.
6049 static struct cb_ops zfs_cb_ops = {
6050 zfsdev_open, /* open */
6051 zfsdev_close, /* close */
6052 zvol_strategy, /* strategy */
6054 zvol_dump, /* dump */
6055 zvol_read, /* read */
6056 zvol_write, /* write */
6057 zfsdev_ioctl, /* ioctl */
6061 nochpoll, /* poll */
6062 ddi_prop_op, /* prop_op */
6063 NULL, /* streamtab */
6064 D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */
6065 CB_REV, /* version */
6066 nodev, /* async read */
6067 nodev, /* async write */
6070 static struct dev_ops zfs_dev_ops = {
6071 DEVO_REV, /* version */
6073 zfs_info, /* info */
6074 nulldev, /* identify */
6075 nulldev, /* probe */
6076 zfs_attach, /* attach */
6077 zfs_detach, /* detach */
6079 &zfs_cb_ops, /* driver operations */
6080 NULL, /* no bus operations */
6082 ddi_quiesce_not_needed, /* quiesce */
6085 static struct modldrv zfs_modldrv = {
6091 static struct modlinkage modlinkage = {
6093 (void *)&zfs_modlfs,
6094 (void *)&zfs_modldrv,
6099 static struct cdevsw zfs_cdevsw = {
6100 .d_version = D_VERSION,
6101 .d_open = zfsdev_open,
6102 .d_ioctl = zfsdev_ioctl,
6103 .d_name = ZFS_DEV_NAME
6107 zfs_allow_log_destroy(void *arg)
6109 char *poolname = arg;
6116 zfsdev = make_dev(&zfs_cdevsw, 0x0, UID_ROOT, GID_OPERATOR, 0666,
6124 destroy_dev(zfsdev);
6127 static struct root_hold_token *zfs_root_token;
6128 struct proc *zfsproc;
6136 spa_init(FREAD | FWRITE);
6141 if ((error = mod_install(&modlinkage)) != 0) {
6148 tsd_create(&zfs_fsyncer_key, NULL);
6149 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
6150 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
6152 error = ldi_ident_from_mod(&modlinkage, &zfs_li);
6154 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
6164 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
6165 return (SET_ERROR(EBUSY));
6167 if ((error = mod_remove(&modlinkage)) != 0)
6173 if (zfs_nfsshare_inited)
6174 (void) ddi_modclose(nfs_mod);
6175 if (zfs_smbshare_inited)
6176 (void) ddi_modclose(smbsrv_mod);
6177 if (zfs_nfsshare_inited || zfs_smbshare_inited)
6178 (void) ddi_modclose(sharefs_mod);
6180 tsd_destroy(&zfs_fsyncer_key);
6181 ldi_ident_release(zfs_li);
6183 mutex_destroy(&zfs_share_lock);
6189 _info(struct modinfo *modinfop)
6191 return (mod_info(&modlinkage, modinfop));
6196 zfs_modevent(module_t mod, int type, void *unused __unused)
6202 zfs_root_token = root_mount_hold("ZFS");
6204 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
6206 spa_init(FREAD | FWRITE);
6211 tsd_create(&zfs_fsyncer_key, NULL);
6212 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
6213 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
6215 printf("ZFS storage pool version: features support (" SPA_VERSION_STRING ")\n");
6216 root_mount_rel(zfs_root_token);
6221 if (spa_busy() || zfs_busy() || zvol_busy() ||
6222 zio_injection_enabled) {
6232 tsd_destroy(&zfs_fsyncer_key);
6233 tsd_destroy(&rrw_tsd_key);
6234 tsd_destroy(&zfs_allow_log_key);
6236 mutex_destroy(&zfs_share_lock);
6245 static moduledata_t zfs_mod = {
6250 DECLARE_MODULE(zfsctrl, zfs_mod, SI_SUB_VFS, SI_ORDER_ANY);
6251 MODULE_VERSION(zfsctrl, 1);
6252 MODULE_DEPEND(zfsctrl, opensolaris, 1, 1, 1);
6253 MODULE_DEPEND(zfsctrl, krpc, 1, 1, 1);