4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011-2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
25 * All rights reserved.
26 * Copyright 2013 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
27 * Copyright 2014 Xin Li <delphij@FreeBSD.org>. All rights reserved.
28 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
29 * Copyright (c) 2014, Joyent, Inc. All rights reserved.
30 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
31 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
32 * Copyright (c) 2013 Steven Hartland. All rights reserved.
38 * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
39 * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
41 * There are two ways that we handle ioctls: the legacy way where almost
42 * all of the logic is in the ioctl callback, and the new way where most
43 * of the marshalling is handled in the common entry point, zfsdev_ioctl().
45 * Non-legacy ioctls should be registered by calling
46 * zfs_ioctl_register() from zfs_ioctl_init(). The ioctl is invoked
47 * from userland by lzc_ioctl().
49 * The registration arguments are as follows:
52 * The name of the ioctl. This is used for history logging. If the
53 * ioctl returns successfully (the callback returns 0), and allow_log
54 * is true, then a history log entry will be recorded with the input &
55 * output nvlists. The log entry can be printed with "zpool history -i".
58 * The ioctl request number, which userland will pass to ioctl(2).
59 * The ioctl numbers can change from release to release, because
60 * the caller (libzfs) must be matched to the kernel.
62 * zfs_secpolicy_func_t *secpolicy
63 * This function will be called before the zfs_ioc_func_t, to
64 * determine if this operation is permitted. It should return EPERM
65 * on failure, and 0 on success. Checks include determining if the
66 * dataset is visible in this zone, and if the user has either all
67 * zfs privileges in the zone (SYS_MOUNT), or has been granted permission
68 * to do this operation on this dataset with "zfs allow".
70 * zfs_ioc_namecheck_t namecheck
71 * This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
72 * name, a dataset name, or nothing. If the name is not well-formed,
73 * the ioctl will fail and the callback will not be called.
74 * Therefore, the callback can assume that the name is well-formed
75 * (e.g. is null-terminated, doesn't have more than one '@' character,
76 * doesn't have invalid characters).
78 * zfs_ioc_poolcheck_t pool_check
79 * This specifies requirements on the pool state. If the pool does
80 * not meet them (is suspended or is readonly), the ioctl will fail
81 * and the callback will not be called. If any checks are specified
82 * (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
83 * Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
84 * POOL_CHECK_READONLY).
86 * boolean_t smush_outnvlist
87 * If smush_outnvlist is true, then the output is presumed to be a
88 * list of errors, and it will be "smushed" down to fit into the
89 * caller's buffer, by removing some entries and replacing them with a
90 * single "N_MORE_ERRORS" entry indicating how many were removed. See
91 * nvlist_smush() for details. If smush_outnvlist is false, and the
92 * outnvlist does not fit into the userland-provided buffer, then the
93 * ioctl will fail with ENOMEM.
95 * zfs_ioc_func_t *func
96 * The callback function that will perform the operation.
98 * The callback should return 0 on success, or an error number on
99 * failure. If the function fails, the userland ioctl will return -1,
100 * and errno will be set to the callback's return value. The callback
101 * will be called with the following arguments:
104 * The name of the pool or dataset to operate on, from
105 * zfs_cmd_t:zc_name. The 'namecheck' argument specifies the
106 * expected type (pool, dataset, or none).
109 * The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src. Or
110 * NULL if no input nvlist was provided. Changes to this nvlist are
111 * ignored. If the input nvlist could not be deserialized, the
112 * ioctl will fail and the callback will not be called.
115 * The output nvlist, initially empty. The callback can fill it in,
116 * and it will be returned to userland by serializing it into
117 * zfs_cmd_t:zc_nvlist_dst. If it is non-empty, and serialization
118 * fails (e.g. because the caller didn't supply a large enough
119 * buffer), then the overall ioctl will fail. See the
120 * 'smush_nvlist' argument above for additional behaviors.
122 * There are two typical uses of the output nvlist:
123 * - To return state, e.g. property values. In this case,
124 * smush_outnvlist should be false. If the buffer was not large
125 * enough, the caller will reallocate a larger buffer and try
128 * - To return multiple errors from an ioctl which makes on-disk
129 * changes. In this case, smush_outnvlist should be true.
130 * Ioctls which make on-disk modifications should generally not
131 * use the outnvl if they succeed, because the caller can not
132 * distinguish between the operation failing, and
133 * deserialization failing.
136 #include <sys/types.h>
137 #include <sys/param.h>
138 #include <sys/systm.h>
139 #include <sys/conf.h>
140 #include <sys/kernel.h>
141 #include <sys/lock.h>
142 #include <sys/malloc.h>
143 #include <sys/mutex.h>
144 #include <sys/proc.h>
145 #include <sys/errno.h>
148 #include <sys/file.h>
149 #include <sys/kmem.h>
150 #include <sys/conf.h>
151 #include <sys/cmn_err.h>
152 #include <sys/stat.h>
153 #include <sys/zfs_ioctl.h>
154 #include <sys/zfs_vfsops.h>
155 #include <sys/zfs_znode.h>
158 #include <sys/spa_impl.h>
159 #include <sys/vdev.h>
161 #include <sys/dsl_dir.h>
162 #include <sys/dsl_dataset.h>
163 #include <sys/dsl_prop.h>
164 #include <sys/dsl_deleg.h>
165 #include <sys/dmu_objset.h>
166 #include <sys/dmu_impl.h>
167 #include <sys/dmu_tx.h>
168 #include <sys/sunddi.h>
169 #include <sys/policy.h>
170 #include <sys/zone.h>
171 #include <sys/nvpair.h>
172 #include <sys/mount.h>
173 #include <sys/taskqueue.h>
175 #include <sys/varargs.h>
176 #include <sys/fs/zfs.h>
177 #include <sys/zfs_ctldir.h>
178 #include <sys/zfs_dir.h>
179 #include <sys/zfs_onexit.h>
180 #include <sys/zvol.h>
181 #include <sys/dsl_scan.h>
182 #include <sys/dmu_objset.h>
183 #include <sys/dmu_send.h>
184 #include <sys/dsl_destroy.h>
185 #include <sys/dsl_bookmark.h>
186 #include <sys/dsl_userhold.h>
187 #include <sys/zfeature.h>
188 #include <sys/zio_checksum.h>
190 #include "zfs_namecheck.h"
191 #include "zfs_prop.h"
192 #include "zfs_deleg.h"
193 #include "zfs_comutil.h"
194 #include "zfs_ioctl_compat.h"
196 CTASSERT(sizeof(zfs_cmd_t) < IOCPARM_MAX);
198 static struct cdev *zfsdev;
200 extern void zfs_init(void);
201 extern void zfs_fini(void);
203 uint_t zfs_fsyncer_key;
204 extern uint_t rrw_tsd_key;
205 static uint_t zfs_allow_log_key;
207 typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *);
208 typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *);
209 typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *);
215 } zfs_ioc_namecheck_t;
218 POOL_CHECK_NONE = 1 << 0,
219 POOL_CHECK_SUSPENDED = 1 << 1,
220 POOL_CHECK_READONLY = 1 << 2,
221 } zfs_ioc_poolcheck_t;
223 typedef struct zfs_ioc_vec {
224 zfs_ioc_legacy_func_t *zvec_legacy_func;
225 zfs_ioc_func_t *zvec_func;
226 zfs_secpolicy_func_t *zvec_secpolicy;
227 zfs_ioc_namecheck_t zvec_namecheck;
228 boolean_t zvec_allow_log;
229 zfs_ioc_poolcheck_t zvec_pool_check;
230 boolean_t zvec_smush_outnvlist;
231 const char *zvec_name;
234 /* This array is indexed by zfs_userquota_prop_t */
235 static const char *userquota_perms[] = {
236 ZFS_DELEG_PERM_USERUSED,
237 ZFS_DELEG_PERM_USERQUOTA,
238 ZFS_DELEG_PERM_GROUPUSED,
239 ZFS_DELEG_PERM_GROUPQUOTA,
242 static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
243 static int zfs_check_settable(const char *name, nvpair_t *property,
245 static int zfs_check_clearable(char *dataset, nvlist_t *props,
247 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
249 int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
250 static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
252 static void zfsdev_close(void *data);
254 static int zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature);
256 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
258 __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
265 * Get rid of annoying "../common/" prefix to filename.
267 newfile = strrchr(file, '/');
268 if (newfile != NULL) {
269 newfile = newfile + 1; /* Get rid of leading / */
275 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
279 * To get this data, use the zfs-dprintf probe as so:
280 * dtrace -q -n 'zfs-dprintf \
281 * /stringof(arg0) == "dbuf.c"/ \
282 * {printf("%s: %s", stringof(arg1), stringof(arg3))}'
284 * arg1 = function name
288 DTRACE_PROBE4(zfs__dprintf,
289 char *, newfile, char *, func, int, line, char *, buf);
293 history_str_free(char *buf)
295 kmem_free(buf, HIS_MAX_RECORD_LEN);
299 history_str_get(zfs_cmd_t *zc)
303 if (zc->zc_history == 0)
306 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
307 if (copyinstr((void *)(uintptr_t)zc->zc_history,
308 buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
309 history_str_free(buf);
313 buf[HIS_MAX_RECORD_LEN -1] = '\0';
319 * Check to see if the named dataset is currently defined as bootable
322 zfs_is_bootfs(const char *name)
326 if (dmu_objset_hold(name, FTAG, &os) == 0) {
328 ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
329 dmu_objset_rele(os, FTAG);
336 * Return non-zero if the spa version is less than requested version.
339 zfs_earlier_version(const char *name, int version)
343 if (spa_open(name, &spa, FTAG) == 0) {
344 if (spa_version(spa) < version) {
345 spa_close(spa, FTAG);
348 spa_close(spa, FTAG);
354 * Return TRUE if the ZPL version is less than requested version.
357 zpl_earlier_version(const char *name, int version)
360 boolean_t rc = B_TRUE;
362 if (dmu_objset_hold(name, FTAG, &os) == 0) {
365 if (dmu_objset_type(os) != DMU_OST_ZFS) {
366 dmu_objset_rele(os, FTAG);
369 /* XXX reading from non-owned objset */
370 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
371 rc = zplversion < version;
372 dmu_objset_rele(os, FTAG);
378 zfs_log_history(zfs_cmd_t *zc)
383 if ((buf = history_str_get(zc)) == NULL)
386 if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
387 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
388 (void) spa_history_log(spa, buf);
389 spa_close(spa, FTAG);
391 history_str_free(buf);
395 * Policy for top-level read operations (list pools). Requires no privileges,
396 * and can be used in the local zone, as there is no associated dataset.
400 zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
406 * Policy for dataset read operations (list children, get statistics). Requires
407 * no privileges, but must be visible in the local zone.
411 zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
413 if (INGLOBALZONE(curthread) ||
414 zone_dataset_visible(zc->zc_name, NULL))
417 return (SET_ERROR(ENOENT));
421 zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
426 * The dataset must be visible by this zone -- check this first
427 * so they don't see EPERM on something they shouldn't know about.
429 if (!INGLOBALZONE(curthread) &&
430 !zone_dataset_visible(dataset, &writable))
431 return (SET_ERROR(ENOENT));
433 if (INGLOBALZONE(curthread)) {
435 * If the fs is zoned, only root can access it from the
438 if (secpolicy_zfs(cr) && zoned)
439 return (SET_ERROR(EPERM));
442 * If we are in a local zone, the 'zoned' property must be set.
445 return (SET_ERROR(EPERM));
447 /* must be writable by this zone */
449 return (SET_ERROR(EPERM));
455 zfs_dozonecheck(const char *dataset, cred_t *cr)
459 if (dsl_prop_get_integer(dataset, "jailed", &zoned, NULL))
460 return (SET_ERROR(ENOENT));
462 return (zfs_dozonecheck_impl(dataset, zoned, cr));
466 zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
470 if (dsl_prop_get_int_ds(ds, "jailed", &zoned))
471 return (SET_ERROR(ENOENT));
473 return (zfs_dozonecheck_impl(dataset, zoned, cr));
477 zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
478 const char *perm, cred_t *cr)
482 error = zfs_dozonecheck_ds(name, ds, cr);
484 error = secpolicy_zfs(cr);
486 error = dsl_deleg_access_impl(ds, perm, cr);
492 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
498 error = dsl_pool_hold(name, FTAG, &dp);
502 error = dsl_dataset_hold(dp, name, FTAG, &ds);
504 dsl_pool_rele(dp, FTAG);
508 error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
510 dsl_dataset_rele(ds, FTAG);
511 dsl_pool_rele(dp, FTAG);
517 * Policy for setting the security label property.
519 * Returns 0 for success, non-zero for access and other errors.
522 zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
524 char ds_hexsl[MAXNAMELEN];
525 bslabel_t ds_sl, new_sl;
526 boolean_t new_default = FALSE;
528 int needed_priv = -1;
531 /* First get the existing dataset label. */
532 error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
533 1, sizeof (ds_hexsl), &ds_hexsl, NULL);
535 return (SET_ERROR(EPERM));
537 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
540 /* The label must be translatable */
541 if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
542 return (SET_ERROR(EINVAL));
545 * In a non-global zone, disallow attempts to set a label that
546 * doesn't match that of the zone; otherwise no other checks
549 if (!INGLOBALZONE(curproc)) {
550 if (new_default || !blequal(&new_sl, CR_SL(CRED())))
551 return (SET_ERROR(EPERM));
556 * For global-zone datasets (i.e., those whose zoned property is
557 * "off", verify that the specified new label is valid for the
560 if (dsl_prop_get_integer(name,
561 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
562 return (SET_ERROR(EPERM));
564 if (zfs_check_global_label(name, strval) != 0)
565 return (SET_ERROR(EPERM));
569 * If the existing dataset label is nondefault, check if the
570 * dataset is mounted (label cannot be changed while mounted).
571 * Get the zfsvfs; if there isn't one, then the dataset isn't
572 * mounted (or isn't a dataset, doesn't exist, ...).
574 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
576 static char *setsl_tag = "setsl_tag";
579 * Try to own the dataset; abort if there is any error,
580 * (e.g., already mounted, in use, or other error).
582 error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE,
585 return (SET_ERROR(EPERM));
587 dmu_objset_disown(os, setsl_tag);
590 needed_priv = PRIV_FILE_DOWNGRADE_SL;
594 if (hexstr_to_label(strval, &new_sl) != 0)
595 return (SET_ERROR(EPERM));
597 if (blstrictdom(&ds_sl, &new_sl))
598 needed_priv = PRIV_FILE_DOWNGRADE_SL;
599 else if (blstrictdom(&new_sl, &ds_sl))
600 needed_priv = PRIV_FILE_UPGRADE_SL;
602 /* dataset currently has a default label */
604 needed_priv = PRIV_FILE_UPGRADE_SL;
608 if (needed_priv != -1)
609 return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
612 #endif /* SECLABEL */
615 zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
621 * Check permissions for special properties.
626 * Disallow setting of 'zoned' from within a local zone.
628 if (!INGLOBALZONE(curthread))
629 return (SET_ERROR(EPERM));
633 case ZFS_PROP_FILESYSTEM_LIMIT:
634 case ZFS_PROP_SNAPSHOT_LIMIT:
635 if (!INGLOBALZONE(curthread)) {
637 char setpoint[MAXNAMELEN];
639 * Unprivileged users are allowed to modify the
640 * limit on things *under* (ie. contained by)
641 * the thing they own.
643 if (dsl_prop_get_integer(dsname, "jailed", &zoned,
645 return (SET_ERROR(EPERM));
646 if (!zoned || strlen(dsname) <= strlen(setpoint))
647 return (SET_ERROR(EPERM));
651 case ZFS_PROP_MLSLABEL:
653 if (!is_system_labeled())
654 return (SET_ERROR(EPERM));
656 if (nvpair_value_string(propval, &strval) == 0) {
659 err = zfs_set_slabel_policy(dsname, strval, CRED());
669 return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
674 zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
678 error = zfs_dozonecheck(zc->zc_name, cr);
683 * permission to set permissions will be evaluated later in
684 * dsl_deleg_can_allow()
691 zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
693 return (zfs_secpolicy_write_perms(zc->zc_name,
694 ZFS_DELEG_PERM_ROLLBACK, cr));
699 zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
707 * Generate the current snapshot name from the given objsetid, then
708 * use that name for the secpolicy/zone checks.
710 cp = strchr(zc->zc_name, '@');
712 return (SET_ERROR(EINVAL));
713 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
717 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
719 dsl_pool_rele(dp, FTAG);
723 dsl_dataset_name(ds, zc->zc_name);
725 error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
726 ZFS_DELEG_PERM_SEND, cr);
727 dsl_dataset_rele(ds, FTAG);
728 dsl_pool_rele(dp, FTAG);
735 zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
737 return (zfs_secpolicy_write_perms(zc->zc_name,
738 ZFS_DELEG_PERM_SEND, cr));
743 zfs_secpolicy_deleg_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
748 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
749 NO_FOLLOW, NULL, &vp)) != 0)
752 /* Now make sure mntpnt and dataset are ZFS */
754 if (strcmp(vp->v_vfsp->mnt_stat.f_fstypename, "zfs") != 0 ||
755 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
756 zc->zc_name) != 0)) {
758 return (SET_ERROR(EPERM));
762 return (dsl_deleg_access(zc->zc_name,
763 ZFS_DELEG_PERM_SHARE, cr));
767 zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
769 if (!INGLOBALZONE(curthread))
770 return (SET_ERROR(EPERM));
772 if (secpolicy_nfs(cr) == 0) {
775 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
780 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
782 if (!INGLOBALZONE(curthread))
783 return (SET_ERROR(EPERM));
785 if (secpolicy_smb(cr) == 0) {
788 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
793 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
798 * Remove the @bla or /bla from the end of the name to get the parent.
800 (void) strncpy(parent, datasetname, parentsize);
801 cp = strrchr(parent, '@');
805 cp = strrchr(parent, '/');
807 return (SET_ERROR(ENOENT));
815 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
819 if ((error = zfs_secpolicy_write_perms(name,
820 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
823 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
828 zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
830 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
834 * Destroying snapshots with delegated permissions requires
835 * descendant mount and destroy permissions.
839 zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
842 nvpair_t *pair, *nextpair;
845 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
846 return (SET_ERROR(EINVAL));
847 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
849 nextpair = nvlist_next_nvpair(snaps, pair);
850 error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr);
851 if (error == ENOENT) {
853 * Ignore any snapshots that don't exist (we consider
854 * them "already destroyed"). Remove the name from the
855 * nvl here in case the snapshot is created between
856 * now and when we try to destroy it (in which case
857 * we don't want to destroy it since we haven't
858 * checked for permission).
860 fnvlist_remove_nvpair(snaps, pair);
871 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
873 char parentname[MAXNAMELEN];
876 if ((error = zfs_secpolicy_write_perms(from,
877 ZFS_DELEG_PERM_RENAME, cr)) != 0)
880 if ((error = zfs_secpolicy_write_perms(from,
881 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
884 if ((error = zfs_get_parent(to, parentname,
885 sizeof (parentname))) != 0)
888 if ((error = zfs_secpolicy_write_perms(parentname,
889 ZFS_DELEG_PERM_CREATE, cr)) != 0)
892 if ((error = zfs_secpolicy_write_perms(parentname,
893 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
901 zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
906 if ((zc->zc_cookie & 1) != 0) {
908 * This is recursive rename, so the starting snapshot might
909 * not exist. Check file system or volume permission instead.
911 at = strchr(zc->zc_name, '@');
917 error = zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr);
927 zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
930 dsl_dataset_t *clone;
933 error = zfs_secpolicy_write_perms(zc->zc_name,
934 ZFS_DELEG_PERM_PROMOTE, cr);
938 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
942 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
945 char parentname[MAXNAMELEN];
946 dsl_dataset_t *origin = NULL;
950 error = dsl_dataset_hold_obj(dd->dd_pool,
951 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin);
953 dsl_dataset_rele(clone, FTAG);
954 dsl_pool_rele(dp, FTAG);
958 error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
959 ZFS_DELEG_PERM_MOUNT, cr);
961 dsl_dataset_name(origin, parentname);
963 error = zfs_secpolicy_write_perms_ds(parentname, origin,
964 ZFS_DELEG_PERM_PROMOTE, cr);
966 dsl_dataset_rele(clone, FTAG);
967 dsl_dataset_rele(origin, FTAG);
969 dsl_pool_rele(dp, FTAG);
975 zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
979 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
980 ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
983 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
984 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
987 return (zfs_secpolicy_write_perms(zc->zc_name,
988 ZFS_DELEG_PERM_CREATE, cr));
992 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
994 return (zfs_secpolicy_write_perms(name,
995 ZFS_DELEG_PERM_SNAPSHOT, cr));
999 * Check for permission to create each snapshot in the nvlist.
1003 zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1009 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
1010 return (SET_ERROR(EINVAL));
1011 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
1012 pair = nvlist_next_nvpair(snaps, pair)) {
1013 char *name = nvpair_name(pair);
1014 char *atp = strchr(name, '@');
1017 error = SET_ERROR(EINVAL);
1021 error = zfs_secpolicy_snapshot_perms(name, cr);
1030 * Check for permission to create each snapshot in the nvlist.
1034 zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1038 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
1039 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
1040 char *name = nvpair_name(pair);
1041 char *hashp = strchr(name, '#');
1043 if (hashp == NULL) {
1044 error = SET_ERROR(EINVAL);
1048 error = zfs_secpolicy_write_perms(name,
1049 ZFS_DELEG_PERM_BOOKMARK, cr);
1059 zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1061 nvpair_t *pair, *nextpair;
1064 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1066 char *name = nvpair_name(pair);
1067 char *hashp = strchr(name, '#');
1068 nextpair = nvlist_next_nvpair(innvl, pair);
1070 if (hashp == NULL) {
1071 error = SET_ERROR(EINVAL);
1076 error = zfs_secpolicy_write_perms(name,
1077 ZFS_DELEG_PERM_DESTROY, cr);
1079 if (error == ENOENT) {
1081 * Ignore any filesystems that don't exist (we consider
1082 * their bookmarks "already destroyed"). Remove
1083 * the name from the nvl here in case the filesystem
1084 * is created between now and when we try to destroy
1085 * the bookmark (in which case we don't want to
1086 * destroy it since we haven't checked for permission).
1088 fnvlist_remove_nvpair(innvl, pair);
1100 zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1103 * Even root must have a proper TSD so that we know what pool
1106 if (tsd_get(zfs_allow_log_key) == NULL)
1107 return (SET_ERROR(EPERM));
1112 zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1114 char parentname[MAXNAMELEN];
1118 if ((error = zfs_get_parent(zc->zc_name, parentname,
1119 sizeof (parentname))) != 0)
1122 if (nvlist_lookup_string(innvl, "origin", &origin) == 0 &&
1123 (error = zfs_secpolicy_write_perms(origin,
1124 ZFS_DELEG_PERM_CLONE, cr)) != 0)
1127 if ((error = zfs_secpolicy_write_perms(parentname,
1128 ZFS_DELEG_PERM_CREATE, cr)) != 0)
1131 return (zfs_secpolicy_write_perms(parentname,
1132 ZFS_DELEG_PERM_MOUNT, cr));
1136 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
1137 * SYS_CONFIG privilege, which is not available in a local zone.
1141 zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1143 if (secpolicy_sys_config(cr, B_FALSE) != 0)
1144 return (SET_ERROR(EPERM));
1150 * Policy for object to name lookups.
1154 zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1158 if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
1161 error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
1166 * Policy for fault injection. Requires all privileges.
1170 zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1172 return (secpolicy_zinject(cr));
1177 zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1179 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
1181 if (prop == ZPROP_INVAL) {
1182 if (!zfs_prop_user(zc->zc_value))
1183 return (SET_ERROR(EINVAL));
1184 return (zfs_secpolicy_write_perms(zc->zc_name,
1185 ZFS_DELEG_PERM_USERPROP, cr));
1187 return (zfs_secpolicy_setprop(zc->zc_name, prop,
1193 zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1195 int err = zfs_secpolicy_read(zc, innvl, cr);
1199 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1200 return (SET_ERROR(EINVAL));
1202 if (zc->zc_value[0] == 0) {
1204 * They are asking about a posix uid/gid. If it's
1205 * themself, allow it.
1207 if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
1208 zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
1209 if (zc->zc_guid == crgetuid(cr))
1212 if (groupmember(zc->zc_guid, cr))
1217 return (zfs_secpolicy_write_perms(zc->zc_name,
1218 userquota_perms[zc->zc_objset_type], cr));
1222 zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1224 int err = zfs_secpolicy_read(zc, innvl, cr);
1228 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1229 return (SET_ERROR(EINVAL));
1231 return (zfs_secpolicy_write_perms(zc->zc_name,
1232 userquota_perms[zc->zc_objset_type], cr));
1237 zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1239 return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
1245 zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1251 error = nvlist_lookup_nvlist(innvl, "holds", &holds);
1253 return (SET_ERROR(EINVAL));
1255 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
1256 pair = nvlist_next_nvpair(holds, pair)) {
1257 char fsname[MAXNAMELEN];
1258 error = dmu_fsname(nvpair_name(pair), fsname);
1261 error = zfs_secpolicy_write_perms(fsname,
1262 ZFS_DELEG_PERM_HOLD, cr);
1271 zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1276 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1277 pair = nvlist_next_nvpair(innvl, pair)) {
1278 char fsname[MAXNAMELEN];
1279 error = dmu_fsname(nvpair_name(pair), fsname);
1282 error = zfs_secpolicy_write_perms(fsname,
1283 ZFS_DELEG_PERM_RELEASE, cr);
1291 * Policy for allowing temporary snapshots to be taken or released
1294 zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1297 * A temporary snapshot is the same as a snapshot,
1298 * hold, destroy and release all rolled into one.
1299 * Delegated diff alone is sufficient that we allow this.
1303 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1304 ZFS_DELEG_PERM_DIFF, cr)) == 0)
1307 error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
1309 error = zfs_secpolicy_hold(zc, innvl, cr);
1311 error = zfs_secpolicy_release(zc, innvl, cr);
1313 error = zfs_secpolicy_destroy(zc, innvl, cr);
1318 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1321 get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1325 nvlist_t *list = NULL;
1328 * Read in and unpack the user-supplied nvlist.
1331 return (SET_ERROR(EINVAL));
1333 packed = kmem_alloc(size, KM_SLEEP);
1335 if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1337 kmem_free(packed, size);
1341 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1342 kmem_free(packed, size);
1346 kmem_free(packed, size);
1353 * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
1354 * Entries will be removed from the end of the nvlist, and one int32 entry
1355 * named "N_MORE_ERRORS" will be added indicating how many entries were
1359 nvlist_smush(nvlist_t *errors, size_t max)
1363 size = fnvlist_size(errors);
1366 nvpair_t *more_errors;
1370 return (SET_ERROR(ENOMEM));
1372 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
1373 more_errors = nvlist_prev_nvpair(errors, NULL);
1376 nvpair_t *pair = nvlist_prev_nvpair(errors,
1378 fnvlist_remove_nvpair(errors, pair);
1380 size = fnvlist_size(errors);
1381 } while (size > max);
1383 fnvlist_remove_nvpair(errors, more_errors);
1384 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
1385 ASSERT3U(fnvlist_size(errors), <=, max);
1392 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1394 char *packed = NULL;
1398 size = fnvlist_size(nvl);
1400 if (size > zc->zc_nvlist_dst_size) {
1402 * Solaris returns ENOMEM here, because even if an error is
1403 * returned from an ioctl(2), new zc_nvlist_dst_size will be
1404 * passed to the userland. This is not the case for FreeBSD.
1405 * We need to return 0, so the kernel will copy the
1406 * zc_nvlist_dst_size back and the userland can discover that a
1407 * bigger buffer is needed.
1411 packed = fnvlist_pack(nvl, &size);
1412 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1413 size, zc->zc_iflags) != 0)
1414 error = SET_ERROR(EFAULT);
1415 fnvlist_pack_free(packed, size);
1418 zc->zc_nvlist_dst_size = size;
1419 zc->zc_nvlist_dst_filled = B_TRUE;
1424 getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
1429 error = dmu_objset_hold(dsname, FTAG, &os);
1432 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1433 dmu_objset_rele(os, FTAG);
1434 return (SET_ERROR(EINVAL));
1437 mutex_enter(&os->os_user_ptr_lock);
1438 *zfvp = dmu_objset_get_user(os);
1440 VFS_HOLD((*zfvp)->z_vfs);
1442 error = SET_ERROR(ESRCH);
1444 mutex_exit(&os->os_user_ptr_lock);
1445 dmu_objset_rele(os, FTAG);
1450 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
1451 * case its z_vfs will be NULL, and it will be opened as the owner.
1452 * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1453 * which prevents all vnode ops from running.
1456 zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
1460 if (getzfsvfs(name, zfvp) != 0)
1461 error = zfsvfs_create(name, zfvp);
1463 rrm_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
1465 if ((*zfvp)->z_unmounted) {
1467 * XXX we could probably try again, since the unmounting
1468 * thread should be just about to disassociate the
1469 * objset from the zfsvfs.
1471 rrm_exit(&(*zfvp)->z_teardown_lock, tag);
1472 return (SET_ERROR(EBUSY));
1479 zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
1481 rrm_exit(&zfsvfs->z_teardown_lock, tag);
1483 if (zfsvfs->z_vfs) {
1484 VFS_RELE(zfsvfs->z_vfs);
1486 dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1487 zfsvfs_free(zfsvfs);
1492 zfs_ioc_pool_create(zfs_cmd_t *zc)
1495 nvlist_t *config, *props = NULL;
1496 nvlist_t *rootprops = NULL;
1497 nvlist_t *zplprops = NULL;
1499 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1500 zc->zc_iflags, &config))
1503 if (zc->zc_nvlist_src_size != 0 && (error =
1504 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1505 zc->zc_iflags, &props))) {
1506 nvlist_free(config);
1511 nvlist_t *nvl = NULL;
1512 uint64_t version = SPA_VERSION;
1514 (void) nvlist_lookup_uint64(props,
1515 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1516 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1517 error = SET_ERROR(EINVAL);
1518 goto pool_props_bad;
1520 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
1522 error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
1524 nvlist_free(config);
1528 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1530 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1531 error = zfs_fill_zplprops_root(version, rootprops,
1534 goto pool_props_bad;
1537 error = spa_create(zc->zc_name, config, props, zplprops);
1540 * Set the remaining root properties
1542 if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
1543 ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
1544 (void) spa_destroy(zc->zc_name);
1547 nvlist_free(rootprops);
1548 nvlist_free(zplprops);
1549 nvlist_free(config);
1556 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1559 zfs_log_history(zc);
1560 error = spa_destroy(zc->zc_name);
1562 zvol_remove_minors(zc->zc_name);
1567 zfs_ioc_pool_import(zfs_cmd_t *zc)
1569 nvlist_t *config, *props = NULL;
1573 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1574 zc->zc_iflags, &config)) != 0)
1577 if (zc->zc_nvlist_src_size != 0 && (error =
1578 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1579 zc->zc_iflags, &props))) {
1580 nvlist_free(config);
1584 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1585 guid != zc->zc_guid)
1586 error = SET_ERROR(EINVAL);
1588 error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1590 if (zc->zc_nvlist_dst != 0) {
1593 if ((err = put_nvlist(zc, config)) != 0)
1597 nvlist_free(config);
1606 zfs_ioc_pool_export(zfs_cmd_t *zc)
1609 boolean_t force = (boolean_t)zc->zc_cookie;
1610 boolean_t hardforce = (boolean_t)zc->zc_guid;
1612 zfs_log_history(zc);
1613 error = spa_export(zc->zc_name, NULL, force, hardforce);
1615 zvol_remove_minors(zc->zc_name);
1620 zfs_ioc_pool_configs(zfs_cmd_t *zc)
1625 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1626 return (SET_ERROR(EEXIST));
1628 error = put_nvlist(zc, configs);
1630 nvlist_free(configs);
1637 * zc_name name of the pool
1640 * zc_cookie real errno
1641 * zc_nvlist_dst config nvlist
1642 * zc_nvlist_dst_size size of config nvlist
1645 zfs_ioc_pool_stats(zfs_cmd_t *zc)
1651 error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1652 sizeof (zc->zc_value));
1654 if (config != NULL) {
1655 ret = put_nvlist(zc, config);
1656 nvlist_free(config);
1659 * The config may be present even if 'error' is non-zero.
1660 * In this case we return success, and preserve the real errno
1663 zc->zc_cookie = error;
1672 * Try to import the given pool, returning pool stats as appropriate so that
1673 * user land knows which devices are available and overall pool health.
1676 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1678 nvlist_t *tryconfig, *config;
1681 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1682 zc->zc_iflags, &tryconfig)) != 0)
1685 config = spa_tryimport(tryconfig);
1687 nvlist_free(tryconfig);
1690 return (SET_ERROR(EINVAL));
1692 error = put_nvlist(zc, config);
1693 nvlist_free(config);
1700 * zc_name name of the pool
1701 * zc_cookie scan func (pool_scan_func_t)
1704 zfs_ioc_pool_scan(zfs_cmd_t *zc)
1709 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1712 if (zc->zc_cookie == POOL_SCAN_NONE)
1713 error = spa_scan_stop(spa);
1715 error = spa_scan(spa, zc->zc_cookie);
1717 spa_close(spa, FTAG);
1723 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1728 error = spa_open(zc->zc_name, &spa, FTAG);
1731 spa_close(spa, FTAG);
1737 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1742 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1745 if (zc->zc_cookie < spa_version(spa) ||
1746 !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1747 spa_close(spa, FTAG);
1748 return (SET_ERROR(EINVAL));
1751 spa_upgrade(spa, zc->zc_cookie);
1752 spa_close(spa, FTAG);
1758 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1765 if ((size = zc->zc_history_len) == 0)
1766 return (SET_ERROR(EINVAL));
1768 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1771 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1772 spa_close(spa, FTAG);
1773 return (SET_ERROR(ENOTSUP));
1776 hist_buf = kmem_alloc(size, KM_SLEEP);
1777 if ((error = spa_history_get(spa, &zc->zc_history_offset,
1778 &zc->zc_history_len, hist_buf)) == 0) {
1779 error = ddi_copyout(hist_buf,
1780 (void *)(uintptr_t)zc->zc_history,
1781 zc->zc_history_len, zc->zc_iflags);
1784 spa_close(spa, FTAG);
1785 kmem_free(hist_buf, size);
1790 zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1795 error = spa_open(zc->zc_name, &spa, FTAG);
1797 error = spa_change_guid(spa);
1798 spa_close(spa, FTAG);
1804 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1806 return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
1811 * zc_name name of filesystem
1812 * zc_obj object to find
1815 * zc_value name of object
1818 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1823 /* XXX reading from objset not owned */
1824 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1826 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1827 dmu_objset_rele(os, FTAG);
1828 return (SET_ERROR(EINVAL));
1830 error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
1831 sizeof (zc->zc_value));
1832 dmu_objset_rele(os, FTAG);
1839 * zc_name name of filesystem
1840 * zc_obj object to find
1843 * zc_stat stats on object
1844 * zc_value path to object
1847 zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1852 /* XXX reading from objset not owned */
1853 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1855 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1856 dmu_objset_rele(os, FTAG);
1857 return (SET_ERROR(EINVAL));
1859 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1860 sizeof (zc->zc_value));
1861 dmu_objset_rele(os, FTAG);
1867 zfs_ioc_vdev_add(zfs_cmd_t *zc)
1871 nvlist_t *config, **l2cache, **spares;
1872 uint_t nl2cache = 0, nspares = 0;
1874 error = spa_open(zc->zc_name, &spa, FTAG);
1878 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1879 zc->zc_iflags, &config);
1880 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1881 &l2cache, &nl2cache);
1883 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1888 * A root pool with concatenated devices is not supported.
1889 * Thus, can not add a device to a root pool.
1891 * Intent log device can not be added to a rootpool because
1892 * during mountroot, zil is replayed, a seperated log device
1893 * can not be accessed during the mountroot time.
1895 * l2cache and spare devices are ok to be added to a rootpool.
1897 if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
1898 nvlist_free(config);
1899 spa_close(spa, FTAG);
1900 return (SET_ERROR(EDOM));
1902 #endif /* illumos */
1905 error = spa_vdev_add(spa, config);
1906 nvlist_free(config);
1908 spa_close(spa, FTAG);
1914 * zc_name name of the pool
1915 * zc_nvlist_conf nvlist of devices to remove
1916 * zc_cookie to stop the remove?
1919 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1924 error = spa_open(zc->zc_name, &spa, FTAG);
1927 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1928 spa_close(spa, FTAG);
1933 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1937 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1939 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1941 switch (zc->zc_cookie) {
1942 case VDEV_STATE_ONLINE:
1943 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1946 case VDEV_STATE_OFFLINE:
1947 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1950 case VDEV_STATE_FAULTED:
1951 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1952 zc->zc_obj != VDEV_AUX_EXTERNAL)
1953 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1955 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
1958 case VDEV_STATE_DEGRADED:
1959 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1960 zc->zc_obj != VDEV_AUX_EXTERNAL)
1961 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1963 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
1967 error = SET_ERROR(EINVAL);
1969 zc->zc_cookie = newstate;
1970 spa_close(spa, FTAG);
1975 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1978 int replacing = zc->zc_cookie;
1982 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1985 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1986 zc->zc_iflags, &config)) == 0) {
1987 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1988 nvlist_free(config);
1991 spa_close(spa, FTAG);
1996 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
2001 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2004 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
2006 spa_close(spa, FTAG);
2011 zfs_ioc_vdev_split(zfs_cmd_t *zc)
2014 nvlist_t *config, *props = NULL;
2016 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
2018 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2021 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
2022 zc->zc_iflags, &config)) {
2023 spa_close(spa, FTAG);
2027 if (zc->zc_nvlist_src_size != 0 && (error =
2028 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2029 zc->zc_iflags, &props))) {
2030 spa_close(spa, FTAG);
2031 nvlist_free(config);
2035 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
2037 spa_close(spa, FTAG);
2039 nvlist_free(config);
2046 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
2049 char *path = zc->zc_value;
2050 uint64_t guid = zc->zc_guid;
2053 error = spa_open(zc->zc_name, &spa, FTAG);
2057 error = spa_vdev_setpath(spa, guid, path);
2058 spa_close(spa, FTAG);
2063 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
2066 char *fru = zc->zc_value;
2067 uint64_t guid = zc->zc_guid;
2070 error = spa_open(zc->zc_name, &spa, FTAG);
2074 error = spa_vdev_setfru(spa, guid, fru);
2075 spa_close(spa, FTAG);
2080 zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
2085 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2087 if (zc->zc_nvlist_dst != 0 &&
2088 (error = dsl_prop_get_all(os, &nv)) == 0) {
2089 dmu_objset_stats(os, nv);
2091 * NB: zvol_get_stats() will read the objset contents,
2092 * which we aren't supposed to do with a
2093 * DS_MODE_USER hold, because it could be
2094 * inconsistent. So this is a bit of a workaround...
2095 * XXX reading with out owning
2097 if (!zc->zc_objset_stats.dds_inconsistent &&
2098 dmu_objset_type(os) == DMU_OST_ZVOL) {
2099 error = zvol_get_stats(os, nv);
2104 error = put_nvlist(zc, nv);
2113 * zc_name name of filesystem
2114 * zc_nvlist_dst_size size of buffer for property nvlist
2117 * zc_objset_stats stats
2118 * zc_nvlist_dst property nvlist
2119 * zc_nvlist_dst_size size of property nvlist
2122 zfs_ioc_objset_stats(zfs_cmd_t *zc)
2127 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2129 error = zfs_ioc_objset_stats_impl(zc, os);
2130 dmu_objset_rele(os, FTAG);
2133 if (error == ENOMEM)
2140 * zc_name name of filesystem
2141 * zc_nvlist_dst_size size of buffer for property nvlist
2144 * zc_nvlist_dst received property nvlist
2145 * zc_nvlist_dst_size size of received property nvlist
2147 * Gets received properties (distinct from local properties on or after
2148 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
2149 * local property values.
2152 zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
2158 * Without this check, we would return local property values if the
2159 * caller has not already received properties on or after
2160 * SPA_VERSION_RECVD_PROPS.
2162 if (!dsl_prop_get_hasrecvd(zc->zc_name))
2163 return (SET_ERROR(ENOTSUP));
2165 if (zc->zc_nvlist_dst != 0 &&
2166 (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
2167 error = put_nvlist(zc, nv);
2175 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2181 * zfs_get_zplprop() will either find a value or give us
2182 * the default value (if there is one).
2184 if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2186 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2192 * zc_name name of filesystem
2193 * zc_nvlist_dst_size size of buffer for zpl property nvlist
2196 * zc_nvlist_dst zpl property nvlist
2197 * zc_nvlist_dst_size size of zpl property nvlist
2200 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2205 /* XXX reading without owning */
2206 if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
2209 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2212 * NB: nvl_add_zplprop() will read the objset contents,
2213 * which we aren't supposed to do with a DS_MODE_USER
2214 * hold, because it could be inconsistent.
2216 if (zc->zc_nvlist_dst != 0 &&
2217 !zc->zc_objset_stats.dds_inconsistent &&
2218 dmu_objset_type(os) == DMU_OST_ZFS) {
2221 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2222 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2223 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2224 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2225 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2226 err = put_nvlist(zc, nv);
2229 err = SET_ERROR(ENOENT);
2231 dmu_objset_rele(os, FTAG);
2236 dataset_name_hidden(const char *name)
2239 * Skip over datasets that are not visible in this zone,
2240 * internal datasets (which have a $ in their name), and
2241 * temporary datasets (which have a % in their name).
2243 if (strchr(name, '$') != NULL)
2245 if (strchr(name, '%') != NULL)
2247 if (!INGLOBALZONE(curthread) && !zone_dataset_visible(name, NULL))
2254 * zc_name name of filesystem
2255 * zc_cookie zap cursor
2256 * zc_nvlist_dst_size size of buffer for property nvlist
2259 * zc_name name of next filesystem
2260 * zc_cookie zap cursor
2261 * zc_objset_stats stats
2262 * zc_nvlist_dst property nvlist
2263 * zc_nvlist_dst_size size of property nvlist
2266 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2271 size_t orig_len = strlen(zc->zc_name);
2274 if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
2275 if (error == ENOENT)
2276 error = SET_ERROR(ESRCH);
2280 p = strrchr(zc->zc_name, '/');
2281 if (p == NULL || p[1] != '\0')
2282 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2283 p = zc->zc_name + strlen(zc->zc_name);
2286 error = dmu_dir_list_next(os,
2287 sizeof (zc->zc_name) - (p - zc->zc_name), p,
2288 NULL, &zc->zc_cookie);
2289 if (error == ENOENT)
2290 error = SET_ERROR(ESRCH);
2291 } while (error == 0 && dataset_name_hidden(zc->zc_name));
2292 dmu_objset_rele(os, FTAG);
2295 * If it's an internal dataset (ie. with a '$' in its name),
2296 * don't try to get stats for it, otherwise we'll return ENOENT.
2298 if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
2299 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2300 if (error == ENOENT) {
2301 /* We lost a race with destroy, get the next one. */
2302 zc->zc_name[orig_len] = '\0';
2311 * zc_name name of filesystem
2312 * zc_cookie zap cursor
2313 * zc_nvlist_dst_size size of buffer for property nvlist
2314 * zc_simple when set, only name is requested
2317 * zc_name name of next snapshot
2318 * zc_objset_stats stats
2319 * zc_nvlist_dst property nvlist
2320 * zc_nvlist_dst_size size of property nvlist
2323 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2328 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2330 return (error == ENOENT ? ESRCH : error);
2334 * A dataset name of maximum length cannot have any snapshots,
2335 * so exit immediately.
2337 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
2338 dmu_objset_rele(os, FTAG);
2339 return (SET_ERROR(ESRCH));
2342 error = dmu_snapshot_list_next(os,
2343 sizeof (zc->zc_name) - strlen(zc->zc_name),
2344 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2347 if (error == 0 && !zc->zc_simple) {
2349 dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2351 error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
2355 error = dmu_objset_from_ds(ds, &ossnap);
2357 error = zfs_ioc_objset_stats_impl(zc, ossnap);
2358 dsl_dataset_rele(ds, FTAG);
2360 } else if (error == ENOENT) {
2361 error = SET_ERROR(ESRCH);
2364 dmu_objset_rele(os, FTAG);
2365 /* if we failed, undo the @ that we tacked on to zc_name */
2367 *strchr(zc->zc_name, '@') = '\0';
2372 zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2374 const char *propname = nvpair_name(pair);
2376 unsigned int vallen;
2379 zfs_userquota_prop_t type;
2385 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2387 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2388 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2390 return (SET_ERROR(EINVAL));
2394 * A correctly constructed propname is encoded as
2395 * userquota@<rid>-<domain>.
2397 if ((dash = strchr(propname, '-')) == NULL ||
2398 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2400 return (SET_ERROR(EINVAL));
2407 err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
2409 err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
2410 zfsvfs_rele(zfsvfs, FTAG);
2417 * If the named property is one that has a special function to set its value,
2418 * return 0 on success and a positive error code on failure; otherwise if it is
2419 * not one of the special properties handled by this function, return -1.
2421 * XXX: It would be better for callers of the property interface if we handled
2422 * these special cases in dsl_prop.c (in the dsl layer).
2425 zfs_prop_set_special(const char *dsname, zprop_source_t source,
2428 const char *propname = nvpair_name(pair);
2429 zfs_prop_t prop = zfs_name_to_prop(propname);
2433 if (prop == ZPROP_INVAL) {
2434 if (zfs_prop_userquota(propname))
2435 return (zfs_prop_set_userquota(dsname, pair));
2439 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2441 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2442 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2446 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
2449 VERIFY(0 == nvpair_value_uint64(pair, &intval));
2452 case ZFS_PROP_QUOTA:
2453 err = dsl_dir_set_quota(dsname, source, intval);
2455 case ZFS_PROP_REFQUOTA:
2456 err = dsl_dataset_set_refquota(dsname, source, intval);
2458 case ZFS_PROP_FILESYSTEM_LIMIT:
2459 case ZFS_PROP_SNAPSHOT_LIMIT:
2460 if (intval == UINT64_MAX) {
2461 /* clearing the limit, just do it */
2464 err = dsl_dir_activate_fs_ss_limit(dsname);
2467 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2468 * default path to set the value in the nvlist.
2473 case ZFS_PROP_RESERVATION:
2474 err = dsl_dir_set_reservation(dsname, source, intval);
2476 case ZFS_PROP_REFRESERVATION:
2477 err = dsl_dataset_set_refreservation(dsname, source, intval);
2479 case ZFS_PROP_VOLSIZE:
2480 err = zvol_set_volsize(dsname, intval);
2482 case ZFS_PROP_VERSION:
2486 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2489 err = zfs_set_version(zfsvfs, intval);
2490 zfsvfs_rele(zfsvfs, FTAG);
2492 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2495 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2496 (void) strcpy(zc->zc_name, dsname);
2497 (void) zfs_ioc_userspace_upgrade(zc);
2498 kmem_free(zc, sizeof (zfs_cmd_t));
2510 * This function is best effort. If it fails to set any of the given properties,
2511 * it continues to set as many as it can and returns the last error
2512 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2513 * with the list of names of all the properties that failed along with the
2514 * corresponding error numbers.
2516 * If every property is set successfully, zero is returned and errlist is not
2520 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2528 nvlist_t *genericnvl = fnvlist_alloc();
2529 nvlist_t *retrynvl = fnvlist_alloc();
2533 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2534 const char *propname = nvpair_name(pair);
2535 zfs_prop_t prop = zfs_name_to_prop(propname);
2538 /* decode the property value */
2540 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2542 attrs = fnvpair_value_nvlist(pair);
2543 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2545 err = SET_ERROR(EINVAL);
2548 /* Validate value type */
2549 if (err == 0 && prop == ZPROP_INVAL) {
2550 if (zfs_prop_user(propname)) {
2551 if (nvpair_type(propval) != DATA_TYPE_STRING)
2552 err = SET_ERROR(EINVAL);
2553 } else if (zfs_prop_userquota(propname)) {
2554 if (nvpair_type(propval) !=
2555 DATA_TYPE_UINT64_ARRAY)
2556 err = SET_ERROR(EINVAL);
2558 err = SET_ERROR(EINVAL);
2560 } else if (err == 0) {
2561 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2562 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2563 err = SET_ERROR(EINVAL);
2564 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2567 intval = fnvpair_value_uint64(propval);
2569 switch (zfs_prop_get_type(prop)) {
2570 case PROP_TYPE_NUMBER:
2572 case PROP_TYPE_STRING:
2573 err = SET_ERROR(EINVAL);
2575 case PROP_TYPE_INDEX:
2576 if (zfs_prop_index_to_string(prop,
2577 intval, &unused) != 0)
2578 err = SET_ERROR(EINVAL);
2582 "unknown property type");
2585 err = SET_ERROR(EINVAL);
2589 /* Validate permissions */
2591 err = zfs_check_settable(dsname, pair, CRED());
2594 err = zfs_prop_set_special(dsname, source, pair);
2597 * For better performance we build up a list of
2598 * properties to set in a single transaction.
2600 err = nvlist_add_nvpair(genericnvl, pair);
2601 } else if (err != 0 && nvl != retrynvl) {
2603 * This may be a spurious error caused by
2604 * receiving quota and reservation out of order.
2605 * Try again in a second pass.
2607 err = nvlist_add_nvpair(retrynvl, pair);
2612 if (errlist != NULL)
2613 fnvlist_add_int32(errlist, propname, err);
2618 if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2623 if (!nvlist_empty(genericnvl) &&
2624 dsl_props_set(dsname, source, genericnvl) != 0) {
2626 * If this fails, we still want to set as many properties as we
2627 * can, so try setting them individually.
2630 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2631 const char *propname = nvpair_name(pair);
2635 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2637 attrs = fnvpair_value_nvlist(pair);
2638 propval = fnvlist_lookup_nvpair(attrs,
2642 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2643 strval = fnvpair_value_string(propval);
2644 err = dsl_prop_set_string(dsname, propname,
2647 intval = fnvpair_value_uint64(propval);
2648 err = dsl_prop_set_int(dsname, propname, source,
2653 if (errlist != NULL) {
2654 fnvlist_add_int32(errlist, propname,
2661 nvlist_free(genericnvl);
2662 nvlist_free(retrynvl);
2668 * Check that all the properties are valid user properties.
2671 zfs_check_userprops(const char *fsname, nvlist_t *nvl)
2673 nvpair_t *pair = NULL;
2676 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2677 const char *propname = nvpair_name(pair);
2679 if (!zfs_prop_user(propname) ||
2680 nvpair_type(pair) != DATA_TYPE_STRING)
2681 return (SET_ERROR(EINVAL));
2683 if (error = zfs_secpolicy_write_perms(fsname,
2684 ZFS_DELEG_PERM_USERPROP, CRED()))
2687 if (strlen(propname) >= ZAP_MAXNAMELEN)
2688 return (SET_ERROR(ENAMETOOLONG));
2690 if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN)
2697 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2701 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2704 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2705 if (nvlist_exists(skipped, nvpair_name(pair)))
2708 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2713 clear_received_props(const char *dsname, nvlist_t *props,
2717 nvlist_t *cleared_props = NULL;
2718 props_skip(props, skipped, &cleared_props);
2719 if (!nvlist_empty(cleared_props)) {
2721 * Acts on local properties until the dataset has received
2722 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2724 zprop_source_t flags = (ZPROP_SRC_NONE |
2725 (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
2726 err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
2728 nvlist_free(cleared_props);
2734 * zc_name name of filesystem
2735 * zc_value name of property to set
2736 * zc_nvlist_src{_size} nvlist of properties to apply
2737 * zc_cookie received properties flag
2740 * zc_nvlist_dst{_size} error for each unapplied received property
2743 zfs_ioc_set_prop(zfs_cmd_t *zc)
2746 boolean_t received = zc->zc_cookie;
2747 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2752 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2753 zc->zc_iflags, &nvl)) != 0)
2757 nvlist_t *origprops;
2759 if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
2760 (void) clear_received_props(zc->zc_name,
2762 nvlist_free(origprops);
2765 error = dsl_prop_set_hasrecvd(zc->zc_name);
2768 errors = fnvlist_alloc();
2770 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
2772 if (zc->zc_nvlist_dst != 0 && errors != NULL) {
2773 (void) put_nvlist(zc, errors);
2776 nvlist_free(errors);
2783 * zc_name name of filesystem
2784 * zc_value name of property to inherit
2785 * zc_cookie revert to received value if TRUE
2790 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2792 const char *propname = zc->zc_value;
2793 zfs_prop_t prop = zfs_name_to_prop(propname);
2794 boolean_t received = zc->zc_cookie;
2795 zprop_source_t source = (received
2796 ? ZPROP_SRC_NONE /* revert to received value, if any */
2797 : ZPROP_SRC_INHERITED); /* explicitly inherit */
2806 * zfs_prop_set_special() expects properties in the form of an
2807 * nvpair with type info.
2809 if (prop == ZPROP_INVAL) {
2810 if (!zfs_prop_user(propname))
2811 return (SET_ERROR(EINVAL));
2813 type = PROP_TYPE_STRING;
2814 } else if (prop == ZFS_PROP_VOLSIZE ||
2815 prop == ZFS_PROP_VERSION) {
2816 return (SET_ERROR(EINVAL));
2818 type = zfs_prop_get_type(prop);
2821 VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2824 case PROP_TYPE_STRING:
2825 VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2827 case PROP_TYPE_NUMBER:
2828 case PROP_TYPE_INDEX:
2829 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2833 return (SET_ERROR(EINVAL));
2836 pair = nvlist_next_nvpair(dummy, NULL);
2837 err = zfs_prop_set_special(zc->zc_name, source, pair);
2840 return (err); /* special property already handled */
2843 * Only check this in the non-received case. We want to allow
2844 * 'inherit -S' to revert non-inheritable properties like quota
2845 * and reservation to the received or default values even though
2846 * they are not considered inheritable.
2848 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2849 return (SET_ERROR(EINVAL));
2852 /* property name has been validated by zfs_secpolicy_inherit_prop() */
2853 return (dsl_prop_inherit(zc->zc_name, zc->zc_value, source));
2857 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2864 if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2865 zc->zc_iflags, &props))
2869 * If the only property is the configfile, then just do a spa_lookup()
2870 * to handle the faulted case.
2872 pair = nvlist_next_nvpair(props, NULL);
2873 if (pair != NULL && strcmp(nvpair_name(pair),
2874 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2875 nvlist_next_nvpair(props, pair) == NULL) {
2876 mutex_enter(&spa_namespace_lock);
2877 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2878 spa_configfile_set(spa, props, B_FALSE);
2879 spa_config_sync(spa, B_FALSE, B_TRUE);
2881 mutex_exit(&spa_namespace_lock);
2888 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2893 error = spa_prop_set(spa, props);
2896 spa_close(spa, FTAG);
2902 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2906 nvlist_t *nvp = NULL;
2908 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2910 * If the pool is faulted, there may be properties we can still
2911 * get (such as altroot and cachefile), so attempt to get them
2914 mutex_enter(&spa_namespace_lock);
2915 if ((spa = spa_lookup(zc->zc_name)) != NULL)
2916 error = spa_prop_get(spa, &nvp);
2917 mutex_exit(&spa_namespace_lock);
2919 error = spa_prop_get(spa, &nvp);
2920 spa_close(spa, FTAG);
2923 if (error == 0 && zc->zc_nvlist_dst != 0)
2924 error = put_nvlist(zc, nvp);
2926 error = SET_ERROR(EFAULT);
2934 * zc_name name of filesystem
2935 * zc_nvlist_src{_size} nvlist of delegated permissions
2936 * zc_perm_action allow/unallow flag
2941 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2944 nvlist_t *fsaclnv = NULL;
2946 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2947 zc->zc_iflags, &fsaclnv)) != 0)
2951 * Verify nvlist is constructed correctly
2953 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2954 nvlist_free(fsaclnv);
2955 return (SET_ERROR(EINVAL));
2959 * If we don't have PRIV_SYS_MOUNT, then validate
2960 * that user is allowed to hand out each permission in
2964 error = secpolicy_zfs(CRED());
2966 if (zc->zc_perm_action == B_FALSE) {
2967 error = dsl_deleg_can_allow(zc->zc_name,
2970 error = dsl_deleg_can_unallow(zc->zc_name,
2976 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2978 nvlist_free(fsaclnv);
2984 * zc_name name of filesystem
2987 * zc_nvlist_src{_size} nvlist of delegated permissions
2990 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
2995 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
2996 error = put_nvlist(zc, nvp);
3004 * Search the vfs list for a specified resource. Returns a pointer to it
3005 * or NULL if no suitable entry is found. The caller of this routine
3006 * is responsible for releasing the returned vfs pointer.
3009 zfs_get_vfs(const char *resource)
3013 mtx_lock(&mountlist_mtx);
3014 TAILQ_FOREACH(vfsp, &mountlist, mnt_list) {
3015 if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
3020 mtx_unlock(&mountlist_mtx);
3026 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3028 zfs_creat_t *zct = arg;
3030 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
3033 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
3037 * os parent objset pointer (NULL if root fs)
3038 * fuids_ok fuids allowed in this version of the spa?
3039 * sa_ok SAs allowed in this version of the spa?
3040 * createprops list of properties requested by creator
3043 * zplprops values for the zplprops we attach to the master node object
3044 * is_ci true if requested file system will be purely case-insensitive
3046 * Determine the settings for utf8only, normalization and
3047 * casesensitivity. Specific values may have been requested by the
3048 * creator and/or we can inherit values from the parent dataset. If
3049 * the file system is of too early a vintage, a creator can not
3050 * request settings for these properties, even if the requested
3051 * setting is the default value. We don't actually want to create dsl
3052 * properties for these, so remove them from the source nvlist after
3056 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
3057 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
3058 nvlist_t *zplprops, boolean_t *is_ci)
3060 uint64_t sense = ZFS_PROP_UNDEFINED;
3061 uint64_t norm = ZFS_PROP_UNDEFINED;
3062 uint64_t u8 = ZFS_PROP_UNDEFINED;
3064 ASSERT(zplprops != NULL);
3067 * Pull out creator prop choices, if any.
3070 (void) nvlist_lookup_uint64(createprops,
3071 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
3072 (void) nvlist_lookup_uint64(createprops,
3073 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
3074 (void) nvlist_remove_all(createprops,
3075 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
3076 (void) nvlist_lookup_uint64(createprops,
3077 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
3078 (void) nvlist_remove_all(createprops,
3079 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
3080 (void) nvlist_lookup_uint64(createprops,
3081 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
3082 (void) nvlist_remove_all(createprops,
3083 zfs_prop_to_name(ZFS_PROP_CASE));
3087 * If the zpl version requested is whacky or the file system
3088 * or pool is version is too "young" to support normalization
3089 * and the creator tried to set a value for one of the props,
3092 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
3093 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
3094 (zplver >= ZPL_VERSION_SA && !sa_ok) ||
3095 (zplver < ZPL_VERSION_NORMALIZATION &&
3096 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
3097 sense != ZFS_PROP_UNDEFINED)))
3098 return (SET_ERROR(ENOTSUP));
3101 * Put the version in the zplprops
3103 VERIFY(nvlist_add_uint64(zplprops,
3104 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3106 if (norm == ZFS_PROP_UNDEFINED)
3107 VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
3108 VERIFY(nvlist_add_uint64(zplprops,
3109 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3112 * If we're normalizing, names must always be valid UTF-8 strings.
3116 if (u8 == ZFS_PROP_UNDEFINED)
3117 VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
3118 VERIFY(nvlist_add_uint64(zplprops,
3119 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3121 if (sense == ZFS_PROP_UNDEFINED)
3122 VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
3123 VERIFY(nvlist_add_uint64(zplprops,
3124 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3127 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
3133 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
3134 nvlist_t *zplprops, boolean_t *is_ci)
3136 boolean_t fuids_ok, sa_ok;
3137 uint64_t zplver = ZPL_VERSION;
3138 objset_t *os = NULL;
3139 char parentname[MAXNAMELEN];
3145 (void) strlcpy(parentname, dataset, sizeof (parentname));
3146 cp = strrchr(parentname, '/');
3150 if ((error = spa_open(dataset, &spa, FTAG)) != 0)
3153 spa_vers = spa_version(spa);
3154 spa_close(spa, FTAG);
3156 zplver = zfs_zpl_version_map(spa_vers);
3157 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3158 sa_ok = (zplver >= ZPL_VERSION_SA);
3161 * Open parent object set so we can inherit zplprop values.
3163 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
3166 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
3168 dmu_objset_rele(os, FTAG);
3173 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
3174 nvlist_t *zplprops, boolean_t *is_ci)
3178 uint64_t zplver = ZPL_VERSION;
3181 zplver = zfs_zpl_version_map(spa_vers);
3182 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3183 sa_ok = (zplver >= ZPL_VERSION_SA);
3185 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
3186 createprops, zplprops, is_ci);
3192 * "type" -> dmu_objset_type_t (int32)
3193 * (optional) "props" -> { prop -> value }
3196 * outnvl: propname -> error code (int32)
3199 zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3202 zfs_creat_t zct = { 0 };
3203 nvlist_t *nvprops = NULL;
3204 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
3206 dmu_objset_type_t type;
3207 boolean_t is_insensitive = B_FALSE;
3209 if (nvlist_lookup_int32(innvl, "type", &type32) != 0)
3210 return (SET_ERROR(EINVAL));
3212 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3216 cbfunc = zfs_create_cb;
3220 cbfunc = zvol_create_cb;
3227 if (strchr(fsname, '@') ||
3228 strchr(fsname, '%'))
3229 return (SET_ERROR(EINVAL));
3231 zct.zct_props = nvprops;
3234 return (SET_ERROR(EINVAL));
3236 if (type == DMU_OST_ZVOL) {
3237 uint64_t volsize, volblocksize;
3239 if (nvprops == NULL)
3240 return (SET_ERROR(EINVAL));
3241 if (nvlist_lookup_uint64(nvprops,
3242 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3243 return (SET_ERROR(EINVAL));
3245 if ((error = nvlist_lookup_uint64(nvprops,
3246 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3247 &volblocksize)) != 0 && error != ENOENT)
3248 return (SET_ERROR(EINVAL));
3251 volblocksize = zfs_prop_default_numeric(
3252 ZFS_PROP_VOLBLOCKSIZE);
3254 if ((error = zvol_check_volblocksize(
3255 volblocksize)) != 0 ||
3256 (error = zvol_check_volsize(volsize,
3257 volblocksize)) != 0)
3259 } else if (type == DMU_OST_ZFS) {
3263 * We have to have normalization and
3264 * case-folding flags correct when we do the
3265 * file system creation, so go figure them out
3268 VERIFY(nvlist_alloc(&zct.zct_zplprops,
3269 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3270 error = zfs_fill_zplprops(fsname, nvprops,
3271 zct.zct_zplprops, &is_insensitive);
3273 nvlist_free(zct.zct_zplprops);
3278 error = dmu_objset_create(fsname, type,
3279 is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
3280 nvlist_free(zct.zct_zplprops);
3283 * It would be nice to do this atomically.
3286 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3289 (void) dsl_destroy_head(fsname);
3292 if (error == 0 && type == DMU_OST_ZVOL)
3293 zvol_create_minors(fsname);
3300 * "origin" -> name of origin snapshot
3301 * (optional) "props" -> { prop -> value }
3304 * outnvl: propname -> error code (int32)
3307 zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3310 nvlist_t *nvprops = NULL;
3313 if (nvlist_lookup_string(innvl, "origin", &origin_name) != 0)
3314 return (SET_ERROR(EINVAL));
3315 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3317 if (strchr(fsname, '@') ||
3318 strchr(fsname, '%'))
3319 return (SET_ERROR(EINVAL));
3321 if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3322 return (SET_ERROR(EINVAL));
3323 error = dmu_objset_clone(fsname, origin_name);
3328 * It would be nice to do this atomically.
3331 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3334 (void) dsl_destroy_head(fsname);
3338 zvol_create_minors(fsname);
3345 * "snaps" -> { snapshot1, snapshot2 }
3346 * (optional) "props" -> { prop -> value (string) }
3349 * outnvl: snapshot -> error code (int32)
3352 zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3355 nvlist_t *props = NULL;
3359 (void) nvlist_lookup_nvlist(innvl, "props", &props);
3360 if ((error = zfs_check_userprops(poolname, props)) != 0)
3363 if (!nvlist_empty(props) &&
3364 zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3365 return (SET_ERROR(ENOTSUP));
3367 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3368 return (SET_ERROR(EINVAL));
3369 poollen = strlen(poolname);
3370 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3371 pair = nvlist_next_nvpair(snaps, pair)) {
3372 const char *name = nvpair_name(pair);
3373 const char *cp = strchr(name, '@');
3376 * The snap name must contain an @, and the part after it must
3377 * contain only valid characters.
3380 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3381 return (SET_ERROR(EINVAL));
3384 * The snap must be in the specified pool.
3386 if (strncmp(name, poolname, poollen) != 0 ||
3387 (name[poollen] != '/' && name[poollen] != '@'))
3388 return (SET_ERROR(EXDEV));
3390 /* This must be the only snap of this fs. */
3391 for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
3392 pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
3393 if (strncmp(name, nvpair_name(pair2), cp - name + 1)
3395 return (SET_ERROR(EXDEV));
3400 error = dsl_dataset_snapshot(snaps, props, outnvl);
3405 * innvl: "message" -> string
3409 zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
3417 * The poolname in the ioctl is not set, we get it from the TSD,
3418 * which was set at the end of the last successful ioctl that allows
3419 * logging. The secpolicy func already checked that it is set.
3420 * Only one log ioctl is allowed after each successful ioctl, so
3421 * we clear the TSD here.
3423 poolname = tsd_get(zfs_allow_log_key);
3424 (void) tsd_set(zfs_allow_log_key, NULL);
3425 error = spa_open(poolname, &spa, FTAG);
3430 if (nvlist_lookup_string(innvl, "message", &message) != 0) {
3431 spa_close(spa, FTAG);
3432 return (SET_ERROR(EINVAL));
3435 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
3436 spa_close(spa, FTAG);
3437 return (SET_ERROR(ENOTSUP));
3440 error = spa_history_log(spa, message);
3441 spa_close(spa, FTAG);
3446 * The dp_config_rwlock must not be held when calling this, because the
3447 * unmount may need to write out data.
3449 * This function is best-effort. Callers must deal gracefully if it
3450 * remains mounted (or is remounted after this call).
3452 * Returns 0 if the argument is not a snapshot, or it is not currently a
3453 * filesystem, or we were able to unmount it. Returns error code otherwise.
3456 zfs_unmount_snap(const char *snapname)
3462 if (strchr(snapname, '@') == NULL)
3465 vfsp = zfs_get_vfs(snapname);
3469 zfsvfs = vfsp->vfs_data;
3470 ASSERT(!dsl_pool_config_held(dmu_objset_pool(zfsvfs->z_os)));
3472 err = vn_vfswlock(vfsp->vfs_vnodecovered);
3475 return (SET_ERROR(err));
3478 * Always force the unmount for snapshots.
3482 (void) dounmount(vfsp, MS_FORCE, kcred);
3485 (void) dounmount(vfsp, MS_FORCE, curthread);
3492 zfs_unmount_snap_cb(const char *snapname, void *arg)
3494 return (zfs_unmount_snap(snapname));
3498 * When a clone is destroyed, its origin may also need to be destroyed,
3499 * in which case it must be unmounted. This routine will do that unmount
3503 zfs_destroy_unmount_origin(const char *fsname)
3509 error = dmu_objset_hold(fsname, FTAG, &os);
3512 ds = dmu_objset_ds(os);
3513 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
3514 char originname[MAXNAMELEN];
3515 dsl_dataset_name(ds->ds_prev, originname);
3516 dmu_objset_rele(os, FTAG);
3517 (void) zfs_unmount_snap(originname);
3519 dmu_objset_rele(os, FTAG);
3525 * "snaps" -> { snapshot1, snapshot2 }
3526 * (optional boolean) "defer"
3529 * outnvl: snapshot -> error code (int32)
3534 zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3541 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3542 return (SET_ERROR(EINVAL));
3543 defer = nvlist_exists(innvl, "defer");
3545 poollen = strlen(poolname);
3546 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3547 pair = nvlist_next_nvpair(snaps, pair)) {
3548 const char *name = nvpair_name(pair);
3551 * The snap must be in the specified pool to prevent the
3552 * invalid removal of zvol minors below.
3554 if (strncmp(name, poolname, poollen) != 0 ||
3555 (name[poollen] != '/' && name[poollen] != '@'))
3556 return (SET_ERROR(EXDEV));
3558 error = zfs_unmount_snap(name);
3561 #if defined(__FreeBSD__)
3562 zvol_remove_minors(name);
3566 return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
3570 * Create bookmarks. Bookmark names are of the form <fs>#<bmark>.
3571 * All bookmarks must be in the same pool.
3574 * bookmark1 -> snapshot1, bookmark2 -> snapshot2
3577 * outnvl: bookmark -> error code (int32)
3582 zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3584 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3585 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3589 * Verify the snapshot argument.
3591 if (nvpair_value_string(pair, &snap_name) != 0)
3592 return (SET_ERROR(EINVAL));
3595 /* Verify that the keys (bookmarks) are unique */
3596 for (nvpair_t *pair2 = nvlist_next_nvpair(innvl, pair);
3597 pair2 != NULL; pair2 = nvlist_next_nvpair(innvl, pair2)) {
3598 if (strcmp(nvpair_name(pair), nvpair_name(pair2)) == 0)
3599 return (SET_ERROR(EINVAL));
3603 return (dsl_bookmark_create(innvl, outnvl));
3608 * property 1, property 2, ...
3612 * bookmark name 1 -> { property 1, property 2, ... },
3613 * bookmark name 2 -> { property 1, property 2, ... }
3618 zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3620 return (dsl_get_bookmarks(fsname, innvl, outnvl));
3625 * bookmark name 1, bookmark name 2
3628 * outnvl: bookmark -> error code (int32)
3632 zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl,
3637 poollen = strlen(poolname);
3638 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3639 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3640 const char *name = nvpair_name(pair);
3641 const char *cp = strchr(name, '#');
3644 * The bookmark name must contain an #, and the part after it
3645 * must contain only valid characters.
3648 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3649 return (SET_ERROR(EINVAL));
3652 * The bookmark must be in the specified pool.
3654 if (strncmp(name, poolname, poollen) != 0 ||
3655 (name[poollen] != '/' && name[poollen] != '#'))
3656 return (SET_ERROR(EXDEV));
3659 error = dsl_bookmark_destroy(innvl, outnvl);
3665 * zc_name name of dataset to destroy
3666 * zc_objset_type type of objset
3667 * zc_defer_destroy mark for deferred destroy
3672 zfs_ioc_destroy(zfs_cmd_t *zc)
3676 if (zc->zc_objset_type == DMU_OST_ZFS) {
3677 err = zfs_unmount_snap(zc->zc_name);
3682 if (strchr(zc->zc_name, '@'))
3683 err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
3685 err = dsl_destroy_head(zc->zc_name);
3686 if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
3688 zvol_remove_minors(zc->zc_name);
3690 (void) zvol_remove_minor(zc->zc_name);
3696 * fsname is name of dataset to rollback (to most recent snapshot)
3698 * innvl is not used.
3700 * outnvl: "target" -> name of most recent snapshot
3705 zfs_ioc_rollback(const char *fsname, nvlist_t *args, nvlist_t *outnvl)
3710 if (getzfsvfs(fsname, &zfsvfs) == 0) {
3711 error = zfs_suspend_fs(zfsvfs);
3715 error = dsl_dataset_rollback(fsname, zfsvfs, outnvl);
3716 resume_err = zfs_resume_fs(zfsvfs, fsname);
3717 error = error ? error : resume_err;
3719 VFS_RELE(zfsvfs->z_vfs);
3721 error = dsl_dataset_rollback(fsname, NULL, outnvl);
3727 recursive_unmount(const char *fsname, void *arg)
3729 const char *snapname = arg;
3730 char fullname[MAXNAMELEN];
3732 (void) snprintf(fullname, sizeof (fullname), "%s@%s", fsname, snapname);
3733 return (zfs_unmount_snap(fullname));
3738 * zc_name old name of dataset
3739 * zc_value new name of dataset
3740 * zc_cookie recursive flag (only valid for snapshots)
3745 zfs_ioc_rename(zfs_cmd_t *zc)
3747 boolean_t recursive = zc->zc_cookie & 1;
3749 boolean_t allow_mounted = zc->zc_cookie & 2;
3753 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3754 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3755 strchr(zc->zc_value, '%'))
3756 return (SET_ERROR(EINVAL));
3758 at = strchr(zc->zc_name, '@');
3760 /* snaps must be in same fs */
3763 if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
3764 return (SET_ERROR(EXDEV));
3767 if (zc->zc_objset_type == DMU_OST_ZFS) {
3769 if (zc->zc_objset_type == DMU_OST_ZFS && allow_mounted) {
3771 error = dmu_objset_find(zc->zc_name,
3772 recursive_unmount, at + 1,
3773 recursive ? DS_FIND_CHILDREN : 0);
3779 error = dsl_dataset_rename_snapshot(zc->zc_name,
3780 at + 1, strchr(zc->zc_value, '@') + 1, recursive);
3786 if (zc->zc_objset_type == DMU_OST_ZVOL)
3787 (void) zvol_remove_minor(zc->zc_name);
3789 return (dsl_dir_rename(zc->zc_name, zc->zc_value));
3794 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
3796 const char *propname = nvpair_name(pair);
3797 boolean_t issnap = (strchr(dsname, '@') != NULL);
3798 zfs_prop_t prop = zfs_name_to_prop(propname);
3802 if (prop == ZPROP_INVAL) {
3803 if (zfs_prop_user(propname)) {
3804 if (err = zfs_secpolicy_write_perms(dsname,
3805 ZFS_DELEG_PERM_USERPROP, cr))
3810 if (!issnap && zfs_prop_userquota(propname)) {
3811 const char *perm = NULL;
3812 const char *uq_prefix =
3813 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
3814 const char *gq_prefix =
3815 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
3817 if (strncmp(propname, uq_prefix,
3818 strlen(uq_prefix)) == 0) {
3819 perm = ZFS_DELEG_PERM_USERQUOTA;
3820 } else if (strncmp(propname, gq_prefix,
3821 strlen(gq_prefix)) == 0) {
3822 perm = ZFS_DELEG_PERM_GROUPQUOTA;
3824 /* USERUSED and GROUPUSED are read-only */
3825 return (SET_ERROR(EINVAL));
3828 if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
3833 return (SET_ERROR(EINVAL));
3837 return (SET_ERROR(EINVAL));
3839 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
3841 * dsl_prop_get_all_impl() returns properties in this
3845 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
3846 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3851 * Check that this value is valid for this pool version
3854 case ZFS_PROP_COMPRESSION:
3856 * If the user specified gzip compression, make sure
3857 * the SPA supports it. We ignore any errors here since
3858 * we'll catch them later.
3860 if (nvpair_value_uint64(pair, &intval) == 0) {
3861 if (intval >= ZIO_COMPRESS_GZIP_1 &&
3862 intval <= ZIO_COMPRESS_GZIP_9 &&
3863 zfs_earlier_version(dsname,
3864 SPA_VERSION_GZIP_COMPRESSION)) {
3865 return (SET_ERROR(ENOTSUP));
3868 if (intval == ZIO_COMPRESS_ZLE &&
3869 zfs_earlier_version(dsname,
3870 SPA_VERSION_ZLE_COMPRESSION))
3871 return (SET_ERROR(ENOTSUP));
3873 if (intval == ZIO_COMPRESS_LZ4) {
3876 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3879 if (!spa_feature_is_enabled(spa,
3880 SPA_FEATURE_LZ4_COMPRESS)) {
3881 spa_close(spa, FTAG);
3882 return (SET_ERROR(ENOTSUP));
3884 spa_close(spa, FTAG);
3888 * If this is a bootable dataset then
3889 * verify that the compression algorithm
3890 * is supported for booting. We must return
3891 * something other than ENOTSUP since it
3892 * implies a downrev pool version.
3894 if (zfs_is_bootfs(dsname) &&
3895 !BOOTFS_COMPRESS_VALID(intval)) {
3896 return (SET_ERROR(ERANGE));
3901 case ZFS_PROP_COPIES:
3902 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
3903 return (SET_ERROR(ENOTSUP));
3906 case ZFS_PROP_RECORDSIZE:
3907 /* Record sizes above 128k need the feature to be enabled */
3908 if (nvpair_value_uint64(pair, &intval) == 0 &&
3909 intval > SPA_OLD_MAXBLOCKSIZE) {
3913 * If this is a bootable dataset then
3914 * the we don't allow large (>128K) blocks,
3915 * because GRUB doesn't support them.
3917 if (zfs_is_bootfs(dsname) &&
3918 intval > SPA_OLD_MAXBLOCKSIZE) {
3919 return (SET_ERROR(ERANGE));
3923 * We don't allow setting the property above 1MB,
3924 * unless the tunable has been changed.
3926 if (intval > zfs_max_recordsize ||
3927 intval > SPA_MAXBLOCKSIZE)
3928 return (SET_ERROR(ERANGE));
3930 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3933 if (!spa_feature_is_enabled(spa,
3934 SPA_FEATURE_LARGE_BLOCKS)) {
3935 spa_close(spa, FTAG);
3936 return (SET_ERROR(ENOTSUP));
3938 spa_close(spa, FTAG);
3942 case ZFS_PROP_SHARESMB:
3943 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
3944 return (SET_ERROR(ENOTSUP));
3947 case ZFS_PROP_ACLINHERIT:
3948 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3949 nvpair_value_uint64(pair, &intval) == 0) {
3950 if (intval == ZFS_ACL_PASSTHROUGH_X &&
3951 zfs_earlier_version(dsname,
3952 SPA_VERSION_PASSTHROUGH_X))
3953 return (SET_ERROR(ENOTSUP));
3957 case ZFS_PROP_CHECKSUM:
3958 case ZFS_PROP_DEDUP:
3960 spa_feature_t feature;
3963 /* dedup feature version checks */
3964 if (prop == ZFS_PROP_DEDUP &&
3965 zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
3966 return (SET_ERROR(ENOTSUP));
3968 if (nvpair_value_uint64(pair, &intval) != 0)
3969 return (SET_ERROR(EINVAL));
3971 /* check prop value is enabled in features */
3972 feature = zio_checksum_to_feature(intval);
3973 if (feature == SPA_FEATURE_NONE)
3976 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3979 * Salted checksums are not supported on root pools.
3981 if (spa_bootfs(spa) != 0 &&
3982 intval < ZIO_CHECKSUM_FUNCTIONS &&
3983 (zio_checksum_table[intval].ci_flags &
3984 ZCHECKSUM_FLAG_SALTED)) {
3985 spa_close(spa, FTAG);
3986 return (SET_ERROR(ERANGE));
3988 if (!spa_feature_is_enabled(spa, feature)) {
3989 spa_close(spa, FTAG);
3990 return (SET_ERROR(ENOTSUP));
3992 spa_close(spa, FTAG);
3997 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
4001 * Checks for a race condition to make sure we don't increment a feature flag
4005 zfs_prop_activate_feature_check(void *arg, dmu_tx_t *tx)
4007 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
4008 spa_feature_t *featurep = arg;
4010 if (!spa_feature_is_active(spa, *featurep))
4013 return (SET_ERROR(EBUSY));
4017 * The callback invoked on feature activation in the sync task caused by
4018 * zfs_prop_activate_feature.
4021 zfs_prop_activate_feature_sync(void *arg, dmu_tx_t *tx)
4023 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
4024 spa_feature_t *featurep = arg;
4026 spa_feature_incr(spa, *featurep, tx);
4030 * Activates a feature on a pool in response to a property setting. This
4031 * creates a new sync task which modifies the pool to reflect the feature
4035 zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature)
4039 /* EBUSY here indicates that the feature is already active */
4040 err = dsl_sync_task(spa_name(spa),
4041 zfs_prop_activate_feature_check, zfs_prop_activate_feature_sync,
4042 &feature, 2, ZFS_SPACE_CHECK_RESERVED);
4044 if (err != 0 && err != EBUSY)
4051 * Removes properties from the given props list that fail permission checks
4052 * needed to clear them and to restore them in case of a receive error. For each
4053 * property, make sure we have both set and inherit permissions.
4055 * Returns the first error encountered if any permission checks fail. If the
4056 * caller provides a non-NULL errlist, it also gives the complete list of names
4057 * of all the properties that failed a permission check along with the
4058 * corresponding error numbers. The caller is responsible for freeing the
4061 * If every property checks out successfully, zero is returned and the list
4062 * pointed at by errlist is NULL.
4065 zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
4068 nvpair_t *pair, *next_pair;
4075 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4077 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
4078 (void) strcpy(zc->zc_name, dataset);
4079 pair = nvlist_next_nvpair(props, NULL);
4080 while (pair != NULL) {
4081 next_pair = nvlist_next_nvpair(props, pair);
4083 (void) strcpy(zc->zc_value, nvpair_name(pair));
4084 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
4085 (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
4086 VERIFY(nvlist_remove_nvpair(props, pair) == 0);
4087 VERIFY(nvlist_add_int32(errors,
4088 zc->zc_value, err) == 0);
4092 kmem_free(zc, sizeof (zfs_cmd_t));
4094 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
4095 nvlist_free(errors);
4098 VERIFY(nvpair_value_int32(pair, &rv) == 0);
4101 if (errlist == NULL)
4102 nvlist_free(errors);
4110 propval_equals(nvpair_t *p1, nvpair_t *p2)
4112 if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
4113 /* dsl_prop_get_all_impl() format */
4115 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
4116 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4120 if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
4122 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
4123 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4127 if (nvpair_type(p1) != nvpair_type(p2))
4130 if (nvpair_type(p1) == DATA_TYPE_STRING) {
4131 char *valstr1, *valstr2;
4133 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
4134 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
4135 return (strcmp(valstr1, valstr2) == 0);
4137 uint64_t intval1, intval2;
4139 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
4140 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
4141 return (intval1 == intval2);
4146 * Remove properties from props if they are not going to change (as determined
4147 * by comparison with origprops). Remove them from origprops as well, since we
4148 * do not need to clear or restore properties that won't change.
4151 props_reduce(nvlist_t *props, nvlist_t *origprops)
4153 nvpair_t *pair, *next_pair;
4155 if (origprops == NULL)
4156 return; /* all props need to be received */
4158 pair = nvlist_next_nvpair(props, NULL);
4159 while (pair != NULL) {
4160 const char *propname = nvpair_name(pair);
4163 next_pair = nvlist_next_nvpair(props, pair);
4165 if ((nvlist_lookup_nvpair(origprops, propname,
4166 &match) != 0) || !propval_equals(pair, match))
4167 goto next; /* need to set received value */
4169 /* don't clear the existing received value */
4170 (void) nvlist_remove_nvpair(origprops, match);
4171 /* don't bother receiving the property */
4172 (void) nvlist_remove_nvpair(props, pair);
4179 static boolean_t zfs_ioc_recv_inject_err;
4184 * zc_name name of containing filesystem
4185 * zc_nvlist_src{_size} nvlist of properties to apply
4186 * zc_value name of snapshot to create
4187 * zc_string name of clone origin (if DRR_FLAG_CLONE)
4188 * zc_cookie file descriptor to recv from
4189 * zc_begin_record the BEGIN record of the stream (not byteswapped)
4190 * zc_guid force flag
4191 * zc_cleanup_fd cleanup-on-exit file descriptor
4192 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
4193 * zc_resumable if data is incomplete assume sender will resume
4196 * zc_cookie number of bytes read
4197 * zc_nvlist_dst{_size} error for each unapplied received property
4198 * zc_obj zprop_errflags_t
4199 * zc_action_handle handle for this guid/ds mapping
4202 zfs_ioc_recv(zfs_cmd_t *zc)
4205 dmu_recv_cookie_t drc;
4206 boolean_t force = (boolean_t)zc->zc_guid;
4209 int props_error = 0;
4212 nvlist_t *props = NULL; /* sent properties */
4213 nvlist_t *origprops = NULL; /* existing properties */
4214 char *origin = NULL;
4216 char tofs[ZFS_MAXNAMELEN];
4217 cap_rights_t rights;
4218 boolean_t first_recvd_props = B_FALSE;
4220 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4221 strchr(zc->zc_value, '@') == NULL ||
4222 strchr(zc->zc_value, '%'))
4223 return (SET_ERROR(EINVAL));
4225 (void) strcpy(tofs, zc->zc_value);
4226 tosnap = strchr(tofs, '@');
4229 if (zc->zc_nvlist_src != 0 &&
4230 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
4231 zc->zc_iflags, &props)) != 0)
4238 fget_read(curthread, fd, cap_rights_init(&rights, CAP_PREAD), &fp);
4242 return (SET_ERROR(EBADF));
4245 errors = fnvlist_alloc();
4247 if (zc->zc_string[0])
4248 origin = zc->zc_string;
4250 error = dmu_recv_begin(tofs, tosnap,
4251 &zc->zc_begin_record, force, zc->zc_resumable, origin, &drc);
4256 * Set properties before we receive the stream so that they are applied
4257 * to the new data. Note that we must call dmu_recv_stream() if
4258 * dmu_recv_begin() succeeds.
4260 if (props != NULL && !drc.drc_newfs) {
4261 if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
4262 SPA_VERSION_RECVD_PROPS &&
4263 !dsl_prop_get_hasrecvd(tofs))
4264 first_recvd_props = B_TRUE;
4267 * If new received properties are supplied, they are to
4268 * completely replace the existing received properties, so stash
4269 * away the existing ones.
4271 if (dsl_prop_get_received(tofs, &origprops) == 0) {
4272 nvlist_t *errlist = NULL;
4274 * Don't bother writing a property if its value won't
4275 * change (and avoid the unnecessary security checks).
4277 * The first receive after SPA_VERSION_RECVD_PROPS is a
4278 * special case where we blow away all local properties
4281 if (!first_recvd_props)
4282 props_reduce(props, origprops);
4283 if (zfs_check_clearable(tofs, origprops, &errlist) != 0)
4284 (void) nvlist_merge(errors, errlist, 0);
4285 nvlist_free(errlist);
4287 if (clear_received_props(tofs, origprops,
4288 first_recvd_props ? NULL : props) != 0)
4289 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4291 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4295 if (props != NULL) {
4296 props_error = dsl_prop_set_hasrecvd(tofs);
4298 if (props_error == 0) {
4299 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4304 if (zc->zc_nvlist_dst_size != 0 &&
4305 (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
4306 put_nvlist(zc, errors) != 0)) {
4308 * Caller made zc->zc_nvlist_dst less than the minimum expected
4309 * size or supplied an invalid address.
4311 props_error = SET_ERROR(EINVAL);
4315 error = dmu_recv_stream(&drc, fp, &off, zc->zc_cleanup_fd,
4316 &zc->zc_action_handle);
4319 zfsvfs_t *zfsvfs = NULL;
4321 if (getzfsvfs(tofs, &zfsvfs) == 0) {
4325 error = zfs_suspend_fs(zfsvfs);
4327 * If the suspend fails, then the recv_end will
4328 * likely also fail, and clean up after itself.
4330 end_err = dmu_recv_end(&drc, zfsvfs);
4332 error = zfs_resume_fs(zfsvfs, tofs);
4333 error = error ? error : end_err;
4334 VFS_RELE(zfsvfs->z_vfs);
4336 error = dmu_recv_end(&drc, NULL);
4340 zc->zc_cookie = off - fp->f_offset;
4341 if (off >= 0 && off <= MAXOFFSET_T)
4345 if (zfs_ioc_recv_inject_err) {
4346 zfs_ioc_recv_inject_err = B_FALSE;
4353 zvol_create_minors(tofs);
4357 * On error, restore the original props.
4359 if (error != 0 && props != NULL && !drc.drc_newfs) {
4360 if (clear_received_props(tofs, props, NULL) != 0) {
4362 * We failed to clear the received properties.
4363 * Since we may have left a $recvd value on the
4364 * system, we can't clear the $hasrecvd flag.
4366 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4367 } else if (first_recvd_props) {
4368 dsl_prop_unset_hasrecvd(tofs);
4371 if (origprops == NULL && !drc.drc_newfs) {
4372 /* We failed to stash the original properties. */
4373 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4377 * dsl_props_set() will not convert RECEIVED to LOCAL on or
4378 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
4379 * explictly if we're restoring local properties cleared in the
4380 * first new-style receive.
4382 if (origprops != NULL &&
4383 zfs_set_prop_nvlist(tofs, (first_recvd_props ?
4384 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
4385 origprops, NULL) != 0) {
4387 * We stashed the original properties but failed to
4390 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4395 nvlist_free(origprops);
4396 nvlist_free(errors);
4400 error = props_error;
4407 * zc_name name of snapshot to send
4408 * zc_cookie file descriptor to send stream to
4409 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
4410 * zc_sendobj objsetid of snapshot to send
4411 * zc_fromobj objsetid of incremental fromsnap (may be zero)
4412 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
4413 * output size in zc_objset_type.
4414 * zc_flags lzc_send_flags
4417 * zc_objset_type estimated size, if zc_guid is set
4420 zfs_ioc_send(zfs_cmd_t *zc)
4424 boolean_t estimate = (zc->zc_guid != 0);
4425 boolean_t embedok = (zc->zc_flags & 0x1);
4426 boolean_t large_block_ok = (zc->zc_flags & 0x2);
4428 if (zc->zc_obj != 0) {
4430 dsl_dataset_t *tosnap;
4432 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4436 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
4438 dsl_pool_rele(dp, FTAG);
4442 if (dsl_dir_is_clone(tosnap->ds_dir))
4444 dsl_dir_phys(tosnap->ds_dir)->dd_origin_obj;
4445 dsl_dataset_rele(tosnap, FTAG);
4446 dsl_pool_rele(dp, FTAG);
4451 dsl_dataset_t *tosnap;
4452 dsl_dataset_t *fromsnap = NULL;
4454 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4458 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
4460 dsl_pool_rele(dp, FTAG);
4464 if (zc->zc_fromobj != 0) {
4465 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
4468 dsl_dataset_rele(tosnap, FTAG);
4469 dsl_pool_rele(dp, FTAG);
4474 error = dmu_send_estimate(tosnap, fromsnap,
4475 &zc->zc_objset_type);
4477 if (fromsnap != NULL)
4478 dsl_dataset_rele(fromsnap, FTAG);
4479 dsl_dataset_rele(tosnap, FTAG);
4480 dsl_pool_rele(dp, FTAG);
4483 cap_rights_t rights;
4486 fp = getf(zc->zc_cookie);
4488 fget_write(curthread, zc->zc_cookie,
4489 cap_rights_init(&rights, CAP_WRITE), &fp);
4492 return (SET_ERROR(EBADF));
4495 error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
4496 zc->zc_fromobj, embedok, large_block_ok,
4498 zc->zc_cookie, fp->f_vnode, &off);
4500 zc->zc_cookie, fp, &off);
4503 if (off >= 0 && off <= MAXOFFSET_T)
4505 releasef(zc->zc_cookie);
4512 * zc_name name of snapshot on which to report progress
4513 * zc_cookie file descriptor of send stream
4516 * zc_cookie number of bytes written in send stream thus far
4519 zfs_ioc_send_progress(zfs_cmd_t *zc)
4523 dmu_sendarg_t *dsp = NULL;
4526 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4530 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
4532 dsl_pool_rele(dp, FTAG);
4536 mutex_enter(&ds->ds_sendstream_lock);
4539 * Iterate over all the send streams currently active on this dataset.
4540 * If there's one which matches the specified file descriptor _and_ the
4541 * stream was started by the current process, return the progress of
4544 for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
4545 dsp = list_next(&ds->ds_sendstreams, dsp)) {
4546 if (dsp->dsa_outfd == zc->zc_cookie &&
4547 dsp->dsa_proc == curproc)
4552 zc->zc_cookie = *(dsp->dsa_off);
4554 error = SET_ERROR(ENOENT);
4556 mutex_exit(&ds->ds_sendstream_lock);
4557 dsl_dataset_rele(ds, FTAG);
4558 dsl_pool_rele(dp, FTAG);
4563 zfs_ioc_inject_fault(zfs_cmd_t *zc)
4567 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
4568 &zc->zc_inject_record);
4571 zc->zc_guid = (uint64_t)id;
4577 zfs_ioc_clear_fault(zfs_cmd_t *zc)
4579 return (zio_clear_fault((int)zc->zc_guid));
4583 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
4585 int id = (int)zc->zc_guid;
4588 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
4589 &zc->zc_inject_record);
4597 zfs_ioc_error_log(zfs_cmd_t *zc)
4601 size_t count = (size_t)zc->zc_nvlist_dst_size;
4603 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
4606 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
4609 zc->zc_nvlist_dst_size = count;
4611 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
4613 spa_close(spa, FTAG);
4619 zfs_ioc_clear(zfs_cmd_t *zc)
4626 * On zpool clear we also fix up missing slogs
4628 mutex_enter(&spa_namespace_lock);
4629 spa = spa_lookup(zc->zc_name);
4631 mutex_exit(&spa_namespace_lock);
4632 return (SET_ERROR(EIO));
4634 if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
4635 /* we need to let spa_open/spa_load clear the chains */
4636 spa_set_log_state(spa, SPA_LOG_CLEAR);
4638 spa->spa_last_open_failed = 0;
4639 mutex_exit(&spa_namespace_lock);
4641 if (zc->zc_cookie & ZPOOL_NO_REWIND) {
4642 error = spa_open(zc->zc_name, &spa, FTAG);
4645 nvlist_t *config = NULL;
4647 if (zc->zc_nvlist_src == 0)
4648 return (SET_ERROR(EINVAL));
4650 if ((error = get_nvlist(zc->zc_nvlist_src,
4651 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
4652 error = spa_open_rewind(zc->zc_name, &spa, FTAG,
4654 if (config != NULL) {
4657 if ((err = put_nvlist(zc, config)) != 0)
4659 nvlist_free(config);
4661 nvlist_free(policy);
4668 spa_vdev_state_enter(spa, SCL_NONE);
4670 if (zc->zc_guid == 0) {
4673 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
4675 (void) spa_vdev_state_exit(spa, NULL, ENODEV);
4676 spa_close(spa, FTAG);
4677 return (SET_ERROR(ENODEV));
4681 vdev_clear(spa, vd);
4683 (void) spa_vdev_state_exit(spa, NULL, 0);
4686 * Resume any suspended I/Os.
4688 if (zio_resume(spa) != 0)
4689 error = SET_ERROR(EIO);
4691 spa_close(spa, FTAG);
4697 zfs_ioc_pool_reopen(zfs_cmd_t *zc)
4702 error = spa_open(zc->zc_name, &spa, FTAG);
4706 spa_vdev_state_enter(spa, SCL_NONE);
4709 * If a resilver is already in progress then set the
4710 * spa_scrub_reopen flag to B_TRUE so that we don't restart
4711 * the scan as a side effect of the reopen. Otherwise, let
4712 * vdev_open() decided if a resilver is required.
4714 spa->spa_scrub_reopen = dsl_scan_resilvering(spa->spa_dsl_pool);
4715 vdev_reopen(spa->spa_root_vdev);
4716 spa->spa_scrub_reopen = B_FALSE;
4718 (void) spa_vdev_state_exit(spa, NULL, 0);
4719 spa_close(spa, FTAG);
4724 * zc_name name of filesystem
4725 * zc_value name of origin snapshot
4728 * zc_string name of conflicting snapshot, if there is one
4731 zfs_ioc_promote(zfs_cmd_t *zc)
4736 * We don't need to unmount *all* the origin fs's snapshots, but
4739 cp = strchr(zc->zc_value, '@');
4742 (void) dmu_objset_find(zc->zc_value,
4743 zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
4744 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
4748 * Retrieve a single {user|group}{used|quota}@... property.
4751 * zc_name name of filesystem
4752 * zc_objset_type zfs_userquota_prop_t
4753 * zc_value domain name (eg. "S-1-234-567-89")
4754 * zc_guid RID/UID/GID
4757 * zc_cookie property value
4760 zfs_ioc_userspace_one(zfs_cmd_t *zc)
4765 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
4766 return (SET_ERROR(EINVAL));
4768 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4772 error = zfs_userspace_one(zfsvfs,
4773 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
4774 zfsvfs_rele(zfsvfs, FTAG);
4781 * zc_name name of filesystem
4782 * zc_cookie zap cursor
4783 * zc_objset_type zfs_userquota_prop_t
4784 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
4787 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
4788 * zc_cookie zap cursor
4791 zfs_ioc_userspace_many(zfs_cmd_t *zc)
4794 int bufsize = zc->zc_nvlist_dst_size;
4797 return (SET_ERROR(ENOMEM));
4799 int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4803 void *buf = kmem_alloc(bufsize, KM_SLEEP);
4805 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
4806 buf, &zc->zc_nvlist_dst_size);
4809 error = ddi_copyout(buf,
4810 (void *)(uintptr_t)zc->zc_nvlist_dst,
4811 zc->zc_nvlist_dst_size, zc->zc_iflags);
4813 kmem_free(buf, bufsize);
4814 zfsvfs_rele(zfsvfs, FTAG);
4821 * zc_name name of filesystem
4827 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
4833 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
4834 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
4836 * If userused is not enabled, it may be because the
4837 * objset needs to be closed & reopened (to grow the
4838 * objset_phys_t). Suspend/resume the fs will do that.
4840 error = zfs_suspend_fs(zfsvfs);
4842 dmu_objset_refresh_ownership(zfsvfs->z_os,
4844 error = zfs_resume_fs(zfsvfs, zc->zc_name);
4848 error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
4849 VFS_RELE(zfsvfs->z_vfs);
4851 /* XXX kind of reading contents without owning */
4852 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4856 error = dmu_objset_userspace_upgrade(os);
4857 dmu_objset_rele(os, FTAG);
4865 * We don't want to have a hard dependency
4866 * against some special symbols in sharefs
4867 * nfs, and smbsrv. Determine them if needed when
4868 * the first file system is shared.
4869 * Neither sharefs, nfs or smbsrv are unloadable modules.
4871 int (*znfsexport_fs)(void *arg);
4872 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
4873 int (*zsmbexport_fs)(void *arg, boolean_t add_share);
4875 int zfs_nfsshare_inited;
4876 int zfs_smbshare_inited;
4878 ddi_modhandle_t nfs_mod;
4879 ddi_modhandle_t sharefs_mod;
4880 ddi_modhandle_t smbsrv_mod;
4882 kmutex_t zfs_share_lock;
4890 ASSERT(MUTEX_HELD(&zfs_share_lock));
4891 /* Both NFS and SMB shares also require sharetab support. */
4892 if (sharefs_mod == NULL && ((sharefs_mod =
4893 ddi_modopen("fs/sharefs",
4894 KRTLD_MODE_FIRST, &error)) == NULL)) {
4895 return (SET_ERROR(ENOSYS));
4897 if (zshare_fs == NULL && ((zshare_fs =
4898 (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
4899 ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
4900 return (SET_ERROR(ENOSYS));
4907 zfs_ioc_share(zfs_cmd_t *zc)
4913 switch (zc->zc_share.z_sharetype) {
4915 case ZFS_UNSHARE_NFS:
4916 if (zfs_nfsshare_inited == 0) {
4917 mutex_enter(&zfs_share_lock);
4918 if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
4919 KRTLD_MODE_FIRST, &error)) == NULL)) {
4920 mutex_exit(&zfs_share_lock);
4921 return (SET_ERROR(ENOSYS));
4923 if (znfsexport_fs == NULL &&
4924 ((znfsexport_fs = (int (*)(void *))
4926 "nfs_export", &error)) == NULL)) {
4927 mutex_exit(&zfs_share_lock);
4928 return (SET_ERROR(ENOSYS));
4930 error = zfs_init_sharefs();
4932 mutex_exit(&zfs_share_lock);
4933 return (SET_ERROR(ENOSYS));
4935 zfs_nfsshare_inited = 1;
4936 mutex_exit(&zfs_share_lock);
4940 case ZFS_UNSHARE_SMB:
4941 if (zfs_smbshare_inited == 0) {
4942 mutex_enter(&zfs_share_lock);
4943 if (smbsrv_mod == NULL && ((smbsrv_mod =
4944 ddi_modopen("drv/smbsrv",
4945 KRTLD_MODE_FIRST, &error)) == NULL)) {
4946 mutex_exit(&zfs_share_lock);
4947 return (SET_ERROR(ENOSYS));
4949 if (zsmbexport_fs == NULL && ((zsmbexport_fs =
4950 (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
4951 "smb_server_share", &error)) == NULL)) {
4952 mutex_exit(&zfs_share_lock);
4953 return (SET_ERROR(ENOSYS));
4955 error = zfs_init_sharefs();
4957 mutex_exit(&zfs_share_lock);
4958 return (SET_ERROR(ENOSYS));
4960 zfs_smbshare_inited = 1;
4961 mutex_exit(&zfs_share_lock);
4965 return (SET_ERROR(EINVAL));
4968 switch (zc->zc_share.z_sharetype) {
4970 case ZFS_UNSHARE_NFS:
4972 znfsexport_fs((void *)
4973 (uintptr_t)zc->zc_share.z_exportdata))
4977 case ZFS_UNSHARE_SMB:
4978 if (error = zsmbexport_fs((void *)
4979 (uintptr_t)zc->zc_share.z_exportdata,
4980 zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
4987 opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
4988 zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
4989 SHAREFS_ADD : SHAREFS_REMOVE;
4992 * Add or remove share from sharetab
4994 error = zshare_fs(opcode,
4995 (void *)(uintptr_t)zc->zc_share.z_sharedata,
4996 zc->zc_share.z_sharemax);
5005 ace_t full_access[] = {
5006 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
5011 * zc_name name of containing filesystem
5012 * zc_obj object # beyond which we want next in-use object #
5015 * zc_obj next in-use object #
5018 zfs_ioc_next_obj(zfs_cmd_t *zc)
5020 objset_t *os = NULL;
5023 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
5027 error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
5028 dsl_dataset_phys(os->os_dsl_dataset)->ds_prev_snap_txg);
5030 dmu_objset_rele(os, FTAG);
5036 * zc_name name of filesystem
5037 * zc_value prefix name for snapshot
5038 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
5041 * zc_value short name of new snapshot
5044 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
5051 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
5055 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
5056 (u_longlong_t)ddi_get_lbolt64());
5057 hold_name = kmem_asprintf("%%%s", zc->zc_value);
5059 error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
5062 (void) strcpy(zc->zc_value, snap_name);
5065 zfs_onexit_fd_rele(zc->zc_cleanup_fd);
5071 * zc_name name of "to" snapshot
5072 * zc_value name of "from" snapshot
5073 * zc_cookie file descriptor to write diff data on
5076 * dmu_diff_record_t's to the file descriptor
5079 zfs_ioc_diff(zfs_cmd_t *zc)
5082 cap_rights_t rights;
5087 fp = getf(zc->zc_cookie);
5089 fget_write(curthread, zc->zc_cookie,
5090 cap_rights_init(&rights, CAP_WRITE), &fp);
5093 return (SET_ERROR(EBADF));
5098 error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off);
5100 error = dmu_diff(zc->zc_name, zc->zc_value, fp, &off);
5103 if (off >= 0 && off <= MAXOFFSET_T)
5105 releasef(zc->zc_cookie);
5112 * Remove all ACL files in shares dir
5115 zfs_smb_acl_purge(znode_t *dzp)
5118 zap_attribute_t zap;
5119 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
5122 for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
5123 (error = zap_cursor_retrieve(&zc, &zap)) == 0;
5124 zap_cursor_advance(&zc)) {
5125 if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
5129 zap_cursor_fini(&zc);
5135 zfs_ioc_smb_acl(zfs_cmd_t *zc)
5140 vnode_t *resourcevp = NULL;
5149 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
5150 NO_FOLLOW, NULL, &vp)) != 0)
5153 /* Now make sure mntpnt and dataset are ZFS */
5155 if (strcmp(vp->v_vfsp->mnt_stat.f_fstypename, "zfs") != 0 ||
5156 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
5157 zc->zc_name) != 0)) {
5159 return (SET_ERROR(EINVAL));
5163 zfsvfs = dzp->z_zfsvfs;
5167 * Create share dir if its missing.
5169 mutex_enter(&zfsvfs->z_lock);
5170 if (zfsvfs->z_shares_dir == 0) {
5173 tx = dmu_tx_create(zfsvfs->z_os);
5174 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
5176 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
5177 error = dmu_tx_assign(tx, TXG_WAIT);
5181 error = zfs_create_share_dir(zfsvfs, tx);
5185 mutex_exit(&zfsvfs->z_lock);
5191 mutex_exit(&zfsvfs->z_lock);
5193 ASSERT(zfsvfs->z_shares_dir);
5194 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
5200 switch (zc->zc_cookie) {
5201 case ZFS_SMB_ACL_ADD:
5202 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
5203 vattr.va_type = VREG;
5204 vattr.va_mode = S_IFREG|0777;
5208 vsec.vsa_mask = VSA_ACE;
5209 vsec.vsa_aclentp = &full_access;
5210 vsec.vsa_aclentsz = sizeof (full_access);
5211 vsec.vsa_aclcnt = 1;
5213 error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
5214 &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
5216 VN_RELE(resourcevp);
5219 case ZFS_SMB_ACL_REMOVE:
5220 error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
5224 case ZFS_SMB_ACL_RENAME:
5225 if ((error = get_nvlist(zc->zc_nvlist_src,
5226 zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
5228 VN_RELE(ZTOV(sharedir));
5232 if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
5233 nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
5236 VN_RELE(ZTOV(sharedir));
5238 nvlist_free(nvlist);
5241 error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
5243 nvlist_free(nvlist);
5246 case ZFS_SMB_ACL_PURGE:
5247 error = zfs_smb_acl_purge(sharedir);
5251 error = SET_ERROR(EINVAL);
5256 VN_RELE(ZTOV(sharedir));
5262 return (EOPNOTSUPP);
5268 * "holds" -> { snapname -> holdname (string), ... }
5269 * (optional) "cleanup_fd" -> fd (int32)
5273 * snapname -> error value (int32)
5279 zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
5283 int cleanup_fd = -1;
5287 error = nvlist_lookup_nvlist(args, "holds", &holds);
5289 return (SET_ERROR(EINVAL));
5291 /* make sure the user didn't pass us any invalid (empty) tags */
5292 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
5293 pair = nvlist_next_nvpair(holds, pair)) {
5296 error = nvpair_value_string(pair, &htag);
5298 return (SET_ERROR(error));
5300 if (strlen(htag) == 0)
5301 return (SET_ERROR(EINVAL));
5304 if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
5305 error = zfs_onexit_fd_hold(cleanup_fd, &minor);
5310 error = dsl_dataset_user_hold(holds, minor, errlist);
5312 zfs_onexit_fd_rele(cleanup_fd);
5317 * innvl is not used.
5320 * holdname -> time added (uint64 seconds since epoch)
5326 zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
5328 return (dsl_dataset_get_holds(snapname, outnvl));
5333 * snapname -> { holdname, ... }
5338 * snapname -> error value (int32)
5344 zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
5346 return (dsl_dataset_user_release(holds, errlist));
5351 * zc_name name of new filesystem or snapshot
5352 * zc_value full name of old snapshot
5355 * zc_cookie space in bytes
5356 * zc_objset_type compressed space in bytes
5357 * zc_perm_action uncompressed space in bytes
5360 zfs_ioc_space_written(zfs_cmd_t *zc)
5364 dsl_dataset_t *new, *old;
5366 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5369 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
5371 dsl_pool_rele(dp, FTAG);
5374 error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
5376 dsl_dataset_rele(new, FTAG);
5377 dsl_pool_rele(dp, FTAG);
5381 error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
5382 &zc->zc_objset_type, &zc->zc_perm_action);
5383 dsl_dataset_rele(old, FTAG);
5384 dsl_dataset_rele(new, FTAG);
5385 dsl_pool_rele(dp, FTAG);
5391 * "firstsnap" -> snapshot name
5395 * "used" -> space in bytes
5396 * "compressed" -> compressed space in bytes
5397 * "uncompressed" -> uncompressed space in bytes
5401 zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
5405 dsl_dataset_t *new, *old;
5407 uint64_t used, comp, uncomp;
5409 if (nvlist_lookup_string(innvl, "firstsnap", &firstsnap) != 0)
5410 return (SET_ERROR(EINVAL));
5412 error = dsl_pool_hold(lastsnap, FTAG, &dp);
5416 error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
5417 if (error == 0 && !new->ds_is_snapshot) {
5418 dsl_dataset_rele(new, FTAG);
5419 error = SET_ERROR(EINVAL);
5422 dsl_pool_rele(dp, FTAG);
5425 error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
5426 if (error == 0 && !old->ds_is_snapshot) {
5427 dsl_dataset_rele(old, FTAG);
5428 error = SET_ERROR(EINVAL);
5431 dsl_dataset_rele(new, FTAG);
5432 dsl_pool_rele(dp, FTAG);
5436 error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
5437 dsl_dataset_rele(old, FTAG);
5438 dsl_dataset_rele(new, FTAG);
5439 dsl_pool_rele(dp, FTAG);
5440 fnvlist_add_uint64(outnvl, "used", used);
5441 fnvlist_add_uint64(outnvl, "compressed", comp);
5442 fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
5447 zfs_ioc_jail(zfs_cmd_t *zc)
5450 return (zone_dataset_attach(curthread->td_ucred, zc->zc_name,
5451 (int)zc->zc_jailid));
5455 zfs_ioc_unjail(zfs_cmd_t *zc)
5458 return (zone_dataset_detach(curthread->td_ucred, zc->zc_name,
5459 (int)zc->zc_jailid));
5464 * "fd" -> file descriptor to write stream to (int32)
5465 * (optional) "fromsnap" -> full snap name to send an incremental from
5466 * (optional) "largeblockok" -> (value ignored)
5467 * indicates that blocks > 128KB are permitted
5468 * (optional) "embedok" -> (value ignored)
5469 * presence indicates DRR_WRITE_EMBEDDED records are permitted
5470 * (optional) "resume_object" and "resume_offset" -> (uint64)
5471 * if present, resume send stream from specified object and offset.
5478 zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5480 cap_rights_t rights;
5484 char *fromname = NULL;
5486 boolean_t largeblockok;
5488 uint64_t resumeobj = 0;
5489 uint64_t resumeoff = 0;
5491 error = nvlist_lookup_int32(innvl, "fd", &fd);
5493 return (SET_ERROR(EINVAL));
5495 (void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
5497 largeblockok = nvlist_exists(innvl, "largeblockok");
5498 embedok = nvlist_exists(innvl, "embedok");
5500 (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
5501 (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
5504 file_t *fp = getf(fd);
5506 fget_write(curthread, fd, cap_rights_init(&rights, CAP_WRITE), &fp);
5509 return (SET_ERROR(EBADF));
5512 error = dmu_send(snapname, fromname, embedok, largeblockok, fd,
5514 resumeobj, resumeoff, fp->f_vnode, &off);
5516 resumeobj, resumeoff, fp, &off);
5520 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5531 * Determine approximately how large a zfs send stream will be -- the number
5532 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
5535 * (optional) "from" -> full snap or bookmark name to send an incremental
5540 * "space" -> bytes of space (uint64)
5544 zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5547 dsl_dataset_t *tosnap;
5552 error = dsl_pool_hold(snapname, FTAG, &dp);
5556 error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
5558 dsl_pool_rele(dp, FTAG);
5562 error = nvlist_lookup_string(innvl, "from", &fromname);
5564 if (strchr(fromname, '@') != NULL) {
5566 * If from is a snapshot, hold it and use the more
5567 * efficient dmu_send_estimate to estimate send space
5568 * size using deadlists.
5570 dsl_dataset_t *fromsnap;
5571 error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
5574 error = dmu_send_estimate(tosnap, fromsnap, &space);
5575 dsl_dataset_rele(fromsnap, FTAG);
5576 } else if (strchr(fromname, '#') != NULL) {
5578 * If from is a bookmark, fetch the creation TXG of the
5579 * snapshot it was created from and use that to find
5580 * blocks that were born after it.
5582 zfs_bookmark_phys_t frombm;
5584 error = dsl_bookmark_lookup(dp, fromname, tosnap,
5588 error = dmu_send_estimate_from_txg(tosnap,
5589 frombm.zbm_creation_txg, &space);
5592 * from is not properly formatted as a snapshot or
5595 error = SET_ERROR(EINVAL);
5599 // If estimating the size of a full send, use dmu_send_estimate
5600 error = dmu_send_estimate(tosnap, NULL, &space);
5603 fnvlist_add_uint64(outnvl, "space", space);
5606 dsl_dataset_rele(tosnap, FTAG);
5607 dsl_pool_rele(dp, FTAG);
5611 static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
5614 zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5615 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
5616 boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
5618 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
5620 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
5621 ASSERT3U(ioc, <, ZFS_IOC_LAST);
5622 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
5623 ASSERT3P(vec->zvec_func, ==, NULL);
5625 vec->zvec_legacy_func = func;
5626 vec->zvec_secpolicy = secpolicy;
5627 vec->zvec_namecheck = namecheck;
5628 vec->zvec_allow_log = log_history;
5629 vec->zvec_pool_check = pool_check;
5633 * See the block comment at the beginning of this file for details on
5634 * each argument to this function.
5637 zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
5638 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
5639 zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
5640 boolean_t allow_log)
5642 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
5644 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
5645 ASSERT3U(ioc, <, ZFS_IOC_LAST);
5646 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
5647 ASSERT3P(vec->zvec_func, ==, NULL);
5649 /* if we are logging, the name must be valid */
5650 ASSERT(!allow_log || namecheck != NO_NAME);
5652 vec->zvec_name = name;
5653 vec->zvec_func = func;
5654 vec->zvec_secpolicy = secpolicy;
5655 vec->zvec_namecheck = namecheck;
5656 vec->zvec_pool_check = pool_check;
5657 vec->zvec_smush_outnvlist = smush_outnvlist;
5658 vec->zvec_allow_log = allow_log;
5662 zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5663 zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
5664 zfs_ioc_poolcheck_t pool_check)
5666 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5667 POOL_NAME, log_history, pool_check);
5671 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5672 zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
5674 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5675 DATASET_NAME, B_FALSE, pool_check);
5679 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
5681 zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
5682 POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5686 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5687 zfs_secpolicy_func_t *secpolicy)
5689 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5690 NO_NAME, B_FALSE, POOL_CHECK_NONE);
5694 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
5695 zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
5697 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5698 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
5702 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
5704 zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
5705 zfs_secpolicy_read);
5709 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5710 zfs_secpolicy_func_t *secpolicy)
5712 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5713 DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5717 zfs_ioctl_init(void)
5719 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
5720 zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
5721 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5723 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
5724 zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
5725 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE);
5727 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
5728 zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
5729 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5731 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
5732 zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
5733 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5735 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
5736 zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
5737 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5739 zfs_ioctl_register("create", ZFS_IOC_CREATE,
5740 zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
5741 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5743 zfs_ioctl_register("clone", ZFS_IOC_CLONE,
5744 zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
5745 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5747 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
5748 zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
5749 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5751 zfs_ioctl_register("hold", ZFS_IOC_HOLD,
5752 zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
5753 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5754 zfs_ioctl_register("release", ZFS_IOC_RELEASE,
5755 zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
5756 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5758 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
5759 zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
5760 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5762 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
5763 zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
5764 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
5766 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK,
5767 zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME,
5768 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5770 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS,
5771 zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME,
5772 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5774 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS,
5775 zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks,
5777 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5779 /* IOCTLS that use the legacy function signature */
5781 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
5782 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
5784 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
5785 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5786 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
5788 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
5789 zfs_ioc_pool_upgrade);
5790 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
5792 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
5793 zfs_ioc_vdev_remove);
5794 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
5795 zfs_ioc_vdev_set_state);
5796 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
5797 zfs_ioc_vdev_attach);
5798 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
5799 zfs_ioc_vdev_detach);
5800 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
5801 zfs_ioc_vdev_setpath);
5802 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
5803 zfs_ioc_vdev_setfru);
5804 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
5805 zfs_ioc_pool_set_props);
5806 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
5807 zfs_ioc_vdev_split);
5808 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
5809 zfs_ioc_pool_reguid);
5811 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
5812 zfs_ioc_pool_configs, zfs_secpolicy_none);
5813 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
5814 zfs_ioc_pool_tryimport, zfs_secpolicy_config);
5815 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
5816 zfs_ioc_inject_fault, zfs_secpolicy_inject);
5817 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
5818 zfs_ioc_clear_fault, zfs_secpolicy_inject);
5819 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
5820 zfs_ioc_inject_list_next, zfs_secpolicy_inject);
5823 * pool destroy, and export don't log the history as part of
5824 * zfsdev_ioctl, but rather zfs_ioc_pool_export
5825 * does the logging of those commands.
5827 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
5828 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
5829 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
5830 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
5832 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
5833 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
5834 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
5835 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
5837 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
5838 zfs_secpolicy_inject, B_FALSE, POOL_CHECK_NONE);
5839 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
5840 zfs_ioc_dsobj_to_dsname,
5841 zfs_secpolicy_diff, B_FALSE, POOL_CHECK_NONE);
5842 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
5843 zfs_ioc_pool_get_history,
5844 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
5846 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
5847 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5849 zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
5850 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5851 zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
5852 zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
5854 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
5855 zfs_ioc_space_written);
5856 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
5857 zfs_ioc_objset_recvd_props);
5858 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
5860 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
5862 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
5863 zfs_ioc_objset_stats);
5864 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
5865 zfs_ioc_objset_zplprops);
5866 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
5867 zfs_ioc_dataset_list_next);
5868 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
5869 zfs_ioc_snapshot_list_next);
5870 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
5871 zfs_ioc_send_progress);
5873 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
5874 zfs_ioc_diff, zfs_secpolicy_diff);
5875 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
5876 zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
5877 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
5878 zfs_ioc_obj_to_path, zfs_secpolicy_diff);
5879 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
5880 zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
5881 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
5882 zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
5883 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
5884 zfs_ioc_send, zfs_secpolicy_send);
5886 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
5887 zfs_secpolicy_none);
5888 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
5889 zfs_secpolicy_destroy);
5890 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
5891 zfs_secpolicy_rename);
5892 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
5893 zfs_secpolicy_recv);
5894 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
5895 zfs_secpolicy_promote);
5896 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
5897 zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
5898 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
5899 zfs_secpolicy_set_fsacl);
5901 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
5902 zfs_secpolicy_share, POOL_CHECK_NONE);
5903 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
5904 zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
5905 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
5906 zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
5907 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5908 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
5909 zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
5910 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5913 zfs_ioctl_register_dataset_nolog(ZFS_IOC_JAIL, zfs_ioc_jail,
5914 zfs_secpolicy_config, POOL_CHECK_NONE);
5915 zfs_ioctl_register_dataset_nolog(ZFS_IOC_UNJAIL, zfs_ioc_unjail,
5916 zfs_secpolicy_config, POOL_CHECK_NONE);
5921 pool_status_check(const char *name, zfs_ioc_namecheck_t type,
5922 zfs_ioc_poolcheck_t check)
5927 ASSERT(type == POOL_NAME || type == DATASET_NAME);
5929 if (check & POOL_CHECK_NONE)
5932 error = spa_open(name, &spa, FTAG);
5934 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
5935 error = SET_ERROR(EAGAIN);
5936 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
5937 error = SET_ERROR(EROFS);
5938 spa_close(spa, FTAG);
5944 * Find a free minor number.
5947 zfsdev_minor_alloc(void)
5949 static minor_t last_minor;
5952 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5954 for (m = last_minor + 1; m != last_minor; m++) {
5955 if (m > ZFSDEV_MAX_MINOR)
5957 if (ddi_get_soft_state(zfsdev_state, m) == NULL) {
5967 zfs_ctldev_init(struct cdev *devp)
5970 zfs_soft_state_t *zs;
5972 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5974 minor = zfsdev_minor_alloc();
5976 return (SET_ERROR(ENXIO));
5978 if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS)
5979 return (SET_ERROR(EAGAIN));
5981 devfs_set_cdevpriv((void *)(uintptr_t)minor, zfsdev_close);
5983 zs = ddi_get_soft_state(zfsdev_state, minor);
5984 zs->zss_type = ZSST_CTLDEV;
5985 zfs_onexit_init((zfs_onexit_t **)&zs->zss_data);
5991 zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor)
5993 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5995 zfs_onexit_destroy(zo);
5996 ddi_soft_state_free(zfsdev_state, minor);
6000 zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which)
6002 zfs_soft_state_t *zp;
6004 zp = ddi_get_soft_state(zfsdev_state, minor);
6005 if (zp == NULL || zp->zss_type != which)
6008 return (zp->zss_data);
6012 zfsdev_open(struct cdev *devp, int flag, int mode, struct thread *td)
6017 if (getminor(*devp) != 0)
6018 return (zvol_open(devp, flag, otyp, cr));
6021 /* This is the control device. Allocate a new minor if requested. */
6023 mutex_enter(&spa_namespace_lock);
6024 error = zfs_ctldev_init(devp);
6025 mutex_exit(&spa_namespace_lock);
6032 zfsdev_close(void *data)
6035 minor_t minor = (minor_t)(uintptr_t)data;
6040 mutex_enter(&spa_namespace_lock);
6041 zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV);
6043 mutex_exit(&spa_namespace_lock);
6046 zfs_ctldev_destroy(zo, minor);
6047 mutex_exit(&spa_namespace_lock);
6051 zfsdev_ioctl(struct cdev *dev, u_long zcmd, caddr_t arg, int flag,
6058 minor_t minor = getminor(dev);
6060 zfs_iocparm_t *zc_iocparm;
6061 int cflag, cmd, oldvecnum;
6062 boolean_t newioc, compat;
6063 void *compat_zc = NULL;
6064 cred_t *cr = td->td_ucred;
6066 const zfs_ioc_vec_t *vec;
6067 char *saved_poolname = NULL;
6068 nvlist_t *innvl = NULL;
6070 cflag = ZFS_CMD_COMPAT_NONE;
6072 newioc = B_TRUE; /* "new" style (zfs_iocparm_t) ioctl */
6074 len = IOCPARM_LEN(zcmd);
6075 vecnum = cmd = zcmd & 0xff;
6078 * Check if we are talking to supported older binaries
6079 * and translate zfs_cmd if necessary
6081 if (len != sizeof(zfs_iocparm_t)) {
6088 case sizeof(zfs_cmd_zcmd_t):
6089 cflag = ZFS_CMD_COMPAT_LZC;
6091 case sizeof(zfs_cmd_deadman_t):
6092 cflag = ZFS_CMD_COMPAT_DEADMAN;
6094 case sizeof(zfs_cmd_v28_t):
6095 cflag = ZFS_CMD_COMPAT_V28;
6097 case sizeof(zfs_cmd_v15_t):
6098 cflag = ZFS_CMD_COMPAT_V15;
6099 vecnum = zfs_ioctl_v15_to_v28[cmd];
6102 * Return without further handling
6103 * if the command is blacklisted.
6105 if (vecnum == ZFS_IOC_COMPAT_PASS)
6107 else if (vecnum == ZFS_IOC_COMPAT_FAIL)
6116 vecnum = cmd - ZFS_IOC_FIRST;
6117 ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
6120 if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
6121 return (SET_ERROR(EINVAL));
6122 vec = &zfs_ioc_vec[vecnum];
6124 zc = kmem_zalloc(sizeof(zfs_cmd_t), KM_SLEEP);
6127 error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
6129 error = SET_ERROR(EFAULT);
6132 #else /* !illumos */
6133 bzero(zc, sizeof(zfs_cmd_t));
6136 zc_iocparm = (void *)arg;
6138 switch (zc_iocparm->zfs_ioctl_version) {
6139 case ZFS_IOCVER_CURRENT:
6140 if (zc_iocparm->zfs_cmd_size != sizeof(zfs_cmd_t)) {
6141 error = SET_ERROR(EINVAL);
6145 case ZFS_IOCVER_EDBP:
6146 if (zc_iocparm->zfs_cmd_size != sizeof(zfs_cmd_edbp_t)) {
6147 error = SET_ERROR(EFAULT);
6151 cflag = ZFS_CMD_COMPAT_EDBP;
6153 case ZFS_IOCVER_ZCMD:
6154 if (zc_iocparm->zfs_cmd_size > sizeof(zfs_cmd_t) ||
6155 zc_iocparm->zfs_cmd_size < sizeof(zfs_cmd_zcmd_t)) {
6156 error = SET_ERROR(EFAULT);
6160 cflag = ZFS_CMD_COMPAT_ZCMD;
6163 error = SET_ERROR(EINVAL);
6169 ASSERT(sizeof(zfs_cmd_t) >= zc_iocparm->zfs_cmd_size);
6170 compat_zc = kmem_zalloc(sizeof(zfs_cmd_t), KM_SLEEP);
6171 bzero(compat_zc, sizeof(zfs_cmd_t));
6173 error = ddi_copyin((void *)(uintptr_t)zc_iocparm->zfs_cmd,
6174 compat_zc, zc_iocparm->zfs_cmd_size, flag);
6176 error = SET_ERROR(EFAULT);
6180 error = ddi_copyin((void *)(uintptr_t)zc_iocparm->zfs_cmd,
6181 zc, zc_iocparm->zfs_cmd_size, flag);
6183 error = SET_ERROR(EFAULT);
6191 ASSERT(compat_zc != NULL);
6192 zfs_cmd_compat_get(zc, compat_zc, cflag);
6194 ASSERT(compat_zc == NULL);
6195 zfs_cmd_compat_get(zc, arg, cflag);
6198 error = zfs_ioctl_compat_pre(zc, &vecnum, cflag);
6201 if (oldvecnum != vecnum)
6202 vec = &zfs_ioc_vec[vecnum];
6204 #endif /* !illumos */
6206 zc->zc_iflags = flag & FKIOCTL;
6207 if (zc->zc_nvlist_src_size != 0) {
6208 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
6209 zc->zc_iflags, &innvl);
6214 /* rewrite innvl for backwards compatibility */
6216 innvl = zfs_ioctl_compat_innvl(zc, innvl, vecnum, cflag);
6219 * Ensure that all pool/dataset names are valid before we pass down to
6222 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
6223 switch (vec->zvec_namecheck) {
6225 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
6226 error = SET_ERROR(EINVAL);
6228 error = pool_status_check(zc->zc_name,
6229 vec->zvec_namecheck, vec->zvec_pool_check);
6233 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
6234 error = SET_ERROR(EINVAL);
6236 error = pool_status_check(zc->zc_name,
6237 vec->zvec_namecheck, vec->zvec_pool_check);
6244 if (error == 0 && !(flag & FKIOCTL))
6245 error = vec->zvec_secpolicy(zc, innvl, cr);
6250 /* legacy ioctls can modify zc_name */
6251 len = strcspn(zc->zc_name, "/@#") + 1;
6252 saved_poolname = kmem_alloc(len, KM_SLEEP);
6253 (void) strlcpy(saved_poolname, zc->zc_name, len);
6255 if (vec->zvec_func != NULL) {
6259 nvlist_t *lognv = NULL;
6261 ASSERT(vec->zvec_legacy_func == NULL);
6264 * Add the innvl to the lognv before calling the func,
6265 * in case the func changes the innvl.
6267 if (vec->zvec_allow_log) {
6268 lognv = fnvlist_alloc();
6269 fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
6271 if (!nvlist_empty(innvl)) {
6272 fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
6277 outnvl = fnvlist_alloc();
6278 error = vec->zvec_func(zc->zc_name, innvl, outnvl);
6280 if (error == 0 && vec->zvec_allow_log &&
6281 spa_open(zc->zc_name, &spa, FTAG) == 0) {
6282 if (!nvlist_empty(outnvl)) {
6283 fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL,
6286 (void) spa_history_log_nvl(spa, lognv);
6287 spa_close(spa, FTAG);
6289 fnvlist_free(lognv);
6291 /* rewrite outnvl for backwards compatibility */
6293 outnvl = zfs_ioctl_compat_outnvl(zc, outnvl, vecnum,
6296 if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
6298 if (vec->zvec_smush_outnvlist) {
6299 smusherror = nvlist_smush(outnvl,
6300 zc->zc_nvlist_dst_size);
6302 if (smusherror == 0)
6303 puterror = put_nvlist(zc, outnvl);
6309 nvlist_free(outnvl);
6311 error = vec->zvec_legacy_func(zc);
6318 rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
6319 if (error == 0 && rc != 0)
6320 error = SET_ERROR(EFAULT);
6323 zfs_ioctl_compat_post(zc, cmd, cflag);
6325 ASSERT(compat_zc != NULL);
6326 ASSERT(sizeof(zfs_cmd_t) >= zc_iocparm->zfs_cmd_size);
6328 zfs_cmd_compat_put(zc, compat_zc, vecnum, cflag);
6329 rc = ddi_copyout(compat_zc,
6330 (void *)(uintptr_t)zc_iocparm->zfs_cmd,
6331 zc_iocparm->zfs_cmd_size, flag);
6332 if (error == 0 && rc != 0)
6333 error = SET_ERROR(EFAULT);
6334 kmem_free(compat_zc, sizeof (zfs_cmd_t));
6336 zfs_cmd_compat_put(zc, arg, vecnum, cflag);
6341 rc = ddi_copyout(zc, (void *)(uintptr_t)zc_iocparm->zfs_cmd,
6342 sizeof (zfs_cmd_t), flag);
6343 if (error == 0 && rc != 0)
6344 error = SET_ERROR(EFAULT);
6347 if (error == 0 && vec->zvec_allow_log) {
6348 char *s = tsd_get(zfs_allow_log_key);
6351 (void) tsd_set(zfs_allow_log_key, saved_poolname);
6353 if (saved_poolname != NULL)
6354 strfree(saved_poolname);
6357 kmem_free(zc, sizeof (zfs_cmd_t));
6363 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
6365 if (cmd != DDI_ATTACH)
6366 return (DDI_FAILURE);
6368 if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
6369 DDI_PSEUDO, 0) == DDI_FAILURE)
6370 return (DDI_FAILURE);
6374 ddi_report_dev(dip);
6376 return (DDI_SUCCESS);
6380 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
6382 if (spa_busy() || zfs_busy() || zvol_busy())
6383 return (DDI_FAILURE);
6385 if (cmd != DDI_DETACH)
6386 return (DDI_FAILURE);
6390 ddi_prop_remove_all(dip);
6391 ddi_remove_minor_node(dip, NULL);
6393 return (DDI_SUCCESS);
6398 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
6401 case DDI_INFO_DEVT2DEVINFO:
6403 return (DDI_SUCCESS);
6405 case DDI_INFO_DEVT2INSTANCE:
6406 *result = (void *)0;
6407 return (DDI_SUCCESS);
6410 return (DDI_FAILURE);
6415 * OK, so this is a little weird.
6417 * /dev/zfs is the control node, i.e. minor 0.
6418 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
6420 * /dev/zfs has basically nothing to do except serve up ioctls,
6421 * so most of the standard driver entry points are in zvol.c.
6424 static struct cb_ops zfs_cb_ops = {
6425 zfsdev_open, /* open */
6426 zfsdev_close, /* close */
6427 zvol_strategy, /* strategy */
6429 zvol_dump, /* dump */
6430 zvol_read, /* read */
6431 zvol_write, /* write */
6432 zfsdev_ioctl, /* ioctl */
6436 nochpoll, /* poll */
6437 ddi_prop_op, /* prop_op */
6438 NULL, /* streamtab */
6439 D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */
6440 CB_REV, /* version */
6441 nodev, /* async read */
6442 nodev, /* async write */
6445 static struct dev_ops zfs_dev_ops = {
6446 DEVO_REV, /* version */
6448 zfs_info, /* info */
6449 nulldev, /* identify */
6450 nulldev, /* probe */
6451 zfs_attach, /* attach */
6452 zfs_detach, /* detach */
6454 &zfs_cb_ops, /* driver operations */
6455 NULL, /* no bus operations */
6457 ddi_quiesce_not_needed, /* quiesce */
6460 static struct modldrv zfs_modldrv = {
6466 static struct modlinkage modlinkage = {
6468 (void *)&zfs_modlfs,
6469 (void *)&zfs_modldrv,
6474 static struct cdevsw zfs_cdevsw = {
6475 .d_version = D_VERSION,
6476 .d_open = zfsdev_open,
6477 .d_ioctl = zfsdev_ioctl,
6478 .d_name = ZFS_DEV_NAME
6482 zfs_allow_log_destroy(void *arg)
6484 char *poolname = arg;
6491 zfsdev = make_dev(&zfs_cdevsw, 0x0, UID_ROOT, GID_OPERATOR, 0666,
6499 destroy_dev(zfsdev);
6502 static struct root_hold_token *zfs_root_token;
6503 struct proc *zfsproc;
6511 spa_init(FREAD | FWRITE);
6516 if ((error = mod_install(&modlinkage)) != 0) {
6523 tsd_create(&zfs_fsyncer_key, NULL);
6524 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
6525 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
6527 error = ldi_ident_from_mod(&modlinkage, &zfs_li);
6529 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
6539 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
6540 return (SET_ERROR(EBUSY));
6542 if ((error = mod_remove(&modlinkage)) != 0)
6548 if (zfs_nfsshare_inited)
6549 (void) ddi_modclose(nfs_mod);
6550 if (zfs_smbshare_inited)
6551 (void) ddi_modclose(smbsrv_mod);
6552 if (zfs_nfsshare_inited || zfs_smbshare_inited)
6553 (void) ddi_modclose(sharefs_mod);
6555 tsd_destroy(&zfs_fsyncer_key);
6556 ldi_ident_release(zfs_li);
6558 mutex_destroy(&zfs_share_lock);
6564 _info(struct modinfo *modinfop)
6566 return (mod_info(&modlinkage, modinfop));
6570 static int zfs__init(void);
6571 static int zfs__fini(void);
6572 static void zfs_shutdown(void *, int);
6574 static eventhandler_tag zfs_shutdown_event_tag;
6576 #define ZFS_MIN_KSTACK_PAGES 4
6582 #if KSTACK_PAGES < ZFS_MIN_KSTACK_PAGES
6583 printf("ZFS NOTICE: KSTACK_PAGES is %d which could result in stack "
6584 "overflow panic!\nPlease consider adding "
6585 "'options KSTACK_PAGES=%d' to your kernel config\n", KSTACK_PAGES,
6586 ZFS_MIN_KSTACK_PAGES);
6588 zfs_root_token = root_mount_hold("ZFS");
6590 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
6592 spa_init(FREAD | FWRITE);
6597 tsd_create(&zfs_fsyncer_key, NULL);
6598 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
6599 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
6601 printf("ZFS storage pool version: features support (" SPA_VERSION_STRING ")\n");
6602 root_mount_rel(zfs_root_token);
6612 if (spa_busy() || zfs_busy() || zvol_busy() ||
6613 zio_injection_enabled) {
6622 tsd_destroy(&zfs_fsyncer_key);
6623 tsd_destroy(&rrw_tsd_key);
6624 tsd_destroy(&zfs_allow_log_key);
6626 mutex_destroy(&zfs_share_lock);
6632 zfs_shutdown(void *arg __unused, int howto __unused)
6636 * ZFS fini routines can not properly work in a panic-ed system.
6638 if (panicstr == NULL)
6644 zfs_modevent(module_t mod, int type, void *unused __unused)
6652 zfs_shutdown_event_tag = EVENTHANDLER_REGISTER(
6653 shutdown_post_sync, zfs_shutdown, NULL,
6654 SHUTDOWN_PRI_FIRST);
6658 if (err == 0 && zfs_shutdown_event_tag != NULL)
6659 EVENTHANDLER_DEREGISTER(shutdown_post_sync,
6660 zfs_shutdown_event_tag);
6667 return (EOPNOTSUPP);
6670 static moduledata_t zfs_mod = {
6675 DECLARE_MODULE(zfsctrl, zfs_mod, SI_SUB_VFS, SI_ORDER_ANY);
6676 MODULE_VERSION(zfsctrl, 1);
6677 MODULE_DEPEND(zfsctrl, opensolaris, 1, 1, 1);
6678 MODULE_DEPEND(zfsctrl, krpc, 1, 1, 1);
6679 MODULE_DEPEND(zfsctrl, acl_nfs4, 1, 1, 1);