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 "opt_kstack_pages.h"
139 #include <sys/types.h>
140 #include <sys/param.h>
141 #include <sys/systm.h>
142 #include <sys/conf.h>
143 #include <sys/kernel.h>
144 #include <sys/lock.h>
145 #include <sys/malloc.h>
146 #include <sys/mutex.h>
147 #include <sys/proc.h>
148 #include <sys/errno.h>
151 #include <sys/file.h>
152 #include <sys/kmem.h>
153 #include <sys/conf.h>
154 #include <sys/cmn_err.h>
155 #include <sys/stat.h>
156 #include <sys/zfs_ioctl.h>
157 #include <sys/zfs_vfsops.h>
158 #include <sys/zfs_znode.h>
161 #include <sys/spa_impl.h>
162 #include <sys/vdev.h>
164 #include <sys/dsl_dir.h>
165 #include <sys/dsl_dataset.h>
166 #include <sys/dsl_prop.h>
167 #include <sys/dsl_deleg.h>
168 #include <sys/dmu_objset.h>
169 #include <sys/dmu_impl.h>
170 #include <sys/dmu_tx.h>
171 #include <sys/sunddi.h>
172 #include <sys/policy.h>
173 #include <sys/zone.h>
174 #include <sys/nvpair.h>
175 #include <sys/mount.h>
176 #include <sys/taskqueue.h>
178 #include <sys/varargs.h>
179 #include <sys/fs/zfs.h>
180 #include <sys/zfs_ctldir.h>
181 #include <sys/zfs_dir.h>
182 #include <sys/zfs_onexit.h>
183 #include <sys/zvol.h>
184 #include <sys/dsl_scan.h>
185 #include <sys/dmu_objset.h>
186 #include <sys/dmu_send.h>
187 #include <sys/dsl_destroy.h>
188 #include <sys/dsl_bookmark.h>
189 #include <sys/dsl_userhold.h>
190 #include <sys/zfeature.h>
191 #include <sys/zio_checksum.h>
193 #include "zfs_namecheck.h"
194 #include "zfs_prop.h"
195 #include "zfs_deleg.h"
196 #include "zfs_comutil.h"
197 #include "zfs_ioctl_compat.h"
199 CTASSERT(sizeof(zfs_cmd_t) < IOCPARM_MAX);
201 static struct cdev *zfsdev;
203 extern void zfs_init(void);
204 extern void zfs_fini(void);
206 uint_t zfs_fsyncer_key;
207 extern uint_t rrw_tsd_key;
208 static uint_t zfs_allow_log_key;
210 typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *);
211 typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *);
212 typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *);
218 } zfs_ioc_namecheck_t;
221 POOL_CHECK_NONE = 1 << 0,
222 POOL_CHECK_SUSPENDED = 1 << 1,
223 POOL_CHECK_READONLY = 1 << 2,
224 } zfs_ioc_poolcheck_t;
226 typedef struct zfs_ioc_vec {
227 zfs_ioc_legacy_func_t *zvec_legacy_func;
228 zfs_ioc_func_t *zvec_func;
229 zfs_secpolicy_func_t *zvec_secpolicy;
230 zfs_ioc_namecheck_t zvec_namecheck;
231 boolean_t zvec_allow_log;
232 zfs_ioc_poolcheck_t zvec_pool_check;
233 boolean_t zvec_smush_outnvlist;
234 const char *zvec_name;
237 /* This array is indexed by zfs_userquota_prop_t */
238 static const char *userquota_perms[] = {
239 ZFS_DELEG_PERM_USERUSED,
240 ZFS_DELEG_PERM_USERQUOTA,
241 ZFS_DELEG_PERM_GROUPUSED,
242 ZFS_DELEG_PERM_GROUPQUOTA,
245 static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
246 static int zfs_check_settable(const char *name, nvpair_t *property,
248 static int zfs_check_clearable(char *dataset, nvlist_t *props,
250 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
252 int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
253 static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
255 static void zfsdev_close(void *data);
257 static int zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature);
259 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
261 __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
268 * Get rid of annoying "../common/" prefix to filename.
270 newfile = strrchr(file, '/');
271 if (newfile != NULL) {
272 newfile = newfile + 1; /* Get rid of leading / */
278 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
282 * To get this data, use the zfs-dprintf probe as so:
283 * dtrace -q -n 'zfs-dprintf \
284 * /stringof(arg0) == "dbuf.c"/ \
285 * {printf("%s: %s", stringof(arg1), stringof(arg3))}'
287 * arg1 = function name
291 DTRACE_PROBE4(zfs__dprintf,
292 char *, newfile, char *, func, int, line, char *, buf);
296 history_str_free(char *buf)
298 kmem_free(buf, HIS_MAX_RECORD_LEN);
302 history_str_get(zfs_cmd_t *zc)
306 if (zc->zc_history == 0)
309 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
310 if (copyinstr((void *)(uintptr_t)zc->zc_history,
311 buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
312 history_str_free(buf);
316 buf[HIS_MAX_RECORD_LEN -1] = '\0';
322 * Check to see if the named dataset is currently defined as bootable
325 zfs_is_bootfs(const char *name)
329 if (dmu_objset_hold(name, FTAG, &os) == 0) {
331 ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
332 dmu_objset_rele(os, FTAG);
339 * Return non-zero if the spa version is less than requested version.
342 zfs_earlier_version(const char *name, int version)
346 if (spa_open(name, &spa, FTAG) == 0) {
347 if (spa_version(spa) < version) {
348 spa_close(spa, FTAG);
351 spa_close(spa, FTAG);
357 * Return TRUE if the ZPL version is less than requested version.
360 zpl_earlier_version(const char *name, int version)
363 boolean_t rc = B_TRUE;
365 if (dmu_objset_hold(name, FTAG, &os) == 0) {
368 if (dmu_objset_type(os) != DMU_OST_ZFS) {
369 dmu_objset_rele(os, FTAG);
372 /* XXX reading from non-owned objset */
373 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
374 rc = zplversion < version;
375 dmu_objset_rele(os, FTAG);
381 zfs_log_history(zfs_cmd_t *zc)
386 if ((buf = history_str_get(zc)) == NULL)
389 if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
390 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
391 (void) spa_history_log(spa, buf);
392 spa_close(spa, FTAG);
394 history_str_free(buf);
398 * Policy for top-level read operations (list pools). Requires no privileges,
399 * and can be used in the local zone, as there is no associated dataset.
403 zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
409 * Policy for dataset read operations (list children, get statistics). Requires
410 * no privileges, but must be visible in the local zone.
414 zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
416 if (INGLOBALZONE(curthread) ||
417 zone_dataset_visible(zc->zc_name, NULL))
420 return (SET_ERROR(ENOENT));
424 zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
429 * The dataset must be visible by this zone -- check this first
430 * so they don't see EPERM on something they shouldn't know about.
432 if (!INGLOBALZONE(curthread) &&
433 !zone_dataset_visible(dataset, &writable))
434 return (SET_ERROR(ENOENT));
436 if (INGLOBALZONE(curthread)) {
438 * If the fs is zoned, only root can access it from the
441 if (secpolicy_zfs(cr) && zoned)
442 return (SET_ERROR(EPERM));
445 * If we are in a local zone, the 'zoned' property must be set.
448 return (SET_ERROR(EPERM));
450 /* must be writable by this zone */
452 return (SET_ERROR(EPERM));
458 zfs_dozonecheck(const char *dataset, cred_t *cr)
462 if (dsl_prop_get_integer(dataset, "jailed", &zoned, NULL))
463 return (SET_ERROR(ENOENT));
465 return (zfs_dozonecheck_impl(dataset, zoned, cr));
469 zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
473 if (dsl_prop_get_int_ds(ds, "jailed", &zoned))
474 return (SET_ERROR(ENOENT));
476 return (zfs_dozonecheck_impl(dataset, zoned, cr));
480 zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
481 const char *perm, cred_t *cr)
485 error = zfs_dozonecheck_ds(name, ds, cr);
487 error = secpolicy_zfs(cr);
489 error = dsl_deleg_access_impl(ds, perm, cr);
495 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
501 error = dsl_pool_hold(name, FTAG, &dp);
505 error = dsl_dataset_hold(dp, name, FTAG, &ds);
507 dsl_pool_rele(dp, FTAG);
511 error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
513 dsl_dataset_rele(ds, FTAG);
514 dsl_pool_rele(dp, FTAG);
520 * Policy for setting the security label property.
522 * Returns 0 for success, non-zero for access and other errors.
525 zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
527 char ds_hexsl[MAXNAMELEN];
528 bslabel_t ds_sl, new_sl;
529 boolean_t new_default = FALSE;
531 int needed_priv = -1;
534 /* First get the existing dataset label. */
535 error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
536 1, sizeof (ds_hexsl), &ds_hexsl, NULL);
538 return (SET_ERROR(EPERM));
540 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
543 /* The label must be translatable */
544 if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
545 return (SET_ERROR(EINVAL));
548 * In a non-global zone, disallow attempts to set a label that
549 * doesn't match that of the zone; otherwise no other checks
552 if (!INGLOBALZONE(curproc)) {
553 if (new_default || !blequal(&new_sl, CR_SL(CRED())))
554 return (SET_ERROR(EPERM));
559 * For global-zone datasets (i.e., those whose zoned property is
560 * "off", verify that the specified new label is valid for the
563 if (dsl_prop_get_integer(name,
564 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
565 return (SET_ERROR(EPERM));
567 if (zfs_check_global_label(name, strval) != 0)
568 return (SET_ERROR(EPERM));
572 * If the existing dataset label is nondefault, check if the
573 * dataset is mounted (label cannot be changed while mounted).
574 * Get the zfsvfs; if there isn't one, then the dataset isn't
575 * mounted (or isn't a dataset, doesn't exist, ...).
577 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
579 static char *setsl_tag = "setsl_tag";
582 * Try to own the dataset; abort if there is any error,
583 * (e.g., already mounted, in use, or other error).
585 error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE,
588 return (SET_ERROR(EPERM));
590 dmu_objset_disown(os, setsl_tag);
593 needed_priv = PRIV_FILE_DOWNGRADE_SL;
597 if (hexstr_to_label(strval, &new_sl) != 0)
598 return (SET_ERROR(EPERM));
600 if (blstrictdom(&ds_sl, &new_sl))
601 needed_priv = PRIV_FILE_DOWNGRADE_SL;
602 else if (blstrictdom(&new_sl, &ds_sl))
603 needed_priv = PRIV_FILE_UPGRADE_SL;
605 /* dataset currently has a default label */
607 needed_priv = PRIV_FILE_UPGRADE_SL;
611 if (needed_priv != -1)
612 return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
615 #endif /* SECLABEL */
618 zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
624 * Check permissions for special properties.
629 * Disallow setting of 'zoned' from within a local zone.
631 if (!INGLOBALZONE(curthread))
632 return (SET_ERROR(EPERM));
636 case ZFS_PROP_FILESYSTEM_LIMIT:
637 case ZFS_PROP_SNAPSHOT_LIMIT:
638 if (!INGLOBALZONE(curthread)) {
640 char setpoint[MAXNAMELEN];
642 * Unprivileged users are allowed to modify the
643 * limit on things *under* (ie. contained by)
644 * the thing they own.
646 if (dsl_prop_get_integer(dsname, "jailed", &zoned,
648 return (SET_ERROR(EPERM));
649 if (!zoned || strlen(dsname) <= strlen(setpoint))
650 return (SET_ERROR(EPERM));
654 case ZFS_PROP_MLSLABEL:
656 if (!is_system_labeled())
657 return (SET_ERROR(EPERM));
659 if (nvpair_value_string(propval, &strval) == 0) {
662 err = zfs_set_slabel_policy(dsname, strval, CRED());
672 return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
677 zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
681 error = zfs_dozonecheck(zc->zc_name, cr);
686 * permission to set permissions will be evaluated later in
687 * dsl_deleg_can_allow()
694 zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
696 return (zfs_secpolicy_write_perms(zc->zc_name,
697 ZFS_DELEG_PERM_ROLLBACK, cr));
702 zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
710 * Generate the current snapshot name from the given objsetid, then
711 * use that name for the secpolicy/zone checks.
713 cp = strchr(zc->zc_name, '@');
715 return (SET_ERROR(EINVAL));
716 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
720 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
722 dsl_pool_rele(dp, FTAG);
726 dsl_dataset_name(ds, zc->zc_name);
728 error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
729 ZFS_DELEG_PERM_SEND, cr);
730 dsl_dataset_rele(ds, FTAG);
731 dsl_pool_rele(dp, FTAG);
738 zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
740 return (zfs_secpolicy_write_perms(zc->zc_name,
741 ZFS_DELEG_PERM_SEND, cr));
746 zfs_secpolicy_deleg_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
751 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
752 NO_FOLLOW, NULL, &vp)) != 0)
755 /* Now make sure mntpnt and dataset are ZFS */
757 if (strcmp(vp->v_vfsp->mnt_stat.f_fstypename, "zfs") != 0 ||
758 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
759 zc->zc_name) != 0)) {
761 return (SET_ERROR(EPERM));
765 return (dsl_deleg_access(zc->zc_name,
766 ZFS_DELEG_PERM_SHARE, cr));
770 zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
772 if (!INGLOBALZONE(curthread))
773 return (SET_ERROR(EPERM));
775 if (secpolicy_nfs(cr) == 0) {
778 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
783 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
785 if (!INGLOBALZONE(curthread))
786 return (SET_ERROR(EPERM));
788 if (secpolicy_smb(cr) == 0) {
791 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
796 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
801 * Remove the @bla or /bla from the end of the name to get the parent.
803 (void) strncpy(parent, datasetname, parentsize);
804 cp = strrchr(parent, '@');
808 cp = strrchr(parent, '/');
810 return (SET_ERROR(ENOENT));
818 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
822 if ((error = zfs_secpolicy_write_perms(name,
823 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
826 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
831 zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
833 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
837 * Destroying snapshots with delegated permissions requires
838 * descendant mount and destroy permissions.
842 zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
845 nvpair_t *pair, *nextpair;
848 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
849 return (SET_ERROR(EINVAL));
850 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
852 nextpair = nvlist_next_nvpair(snaps, pair);
853 error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr);
854 if (error == ENOENT) {
856 * Ignore any snapshots that don't exist (we consider
857 * them "already destroyed"). Remove the name from the
858 * nvl here in case the snapshot is created between
859 * now and when we try to destroy it (in which case
860 * we don't want to destroy it since we haven't
861 * checked for permission).
863 fnvlist_remove_nvpair(snaps, pair);
874 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
876 char parentname[MAXNAMELEN];
879 if ((error = zfs_secpolicy_write_perms(from,
880 ZFS_DELEG_PERM_RENAME, cr)) != 0)
883 if ((error = zfs_secpolicy_write_perms(from,
884 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
887 if ((error = zfs_get_parent(to, parentname,
888 sizeof (parentname))) != 0)
891 if ((error = zfs_secpolicy_write_perms(parentname,
892 ZFS_DELEG_PERM_CREATE, cr)) != 0)
895 if ((error = zfs_secpolicy_write_perms(parentname,
896 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
904 zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
909 if ((zc->zc_cookie & 1) != 0) {
911 * This is recursive rename, so the starting snapshot might
912 * not exist. Check file system or volume permission instead.
914 at = strchr(zc->zc_name, '@');
920 error = zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr);
930 zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
933 dsl_dataset_t *clone;
936 error = zfs_secpolicy_write_perms(zc->zc_name,
937 ZFS_DELEG_PERM_PROMOTE, cr);
941 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
945 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
948 char parentname[MAXNAMELEN];
949 dsl_dataset_t *origin = NULL;
953 error = dsl_dataset_hold_obj(dd->dd_pool,
954 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin);
956 dsl_dataset_rele(clone, FTAG);
957 dsl_pool_rele(dp, FTAG);
961 error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
962 ZFS_DELEG_PERM_MOUNT, cr);
964 dsl_dataset_name(origin, parentname);
966 error = zfs_secpolicy_write_perms_ds(parentname, origin,
967 ZFS_DELEG_PERM_PROMOTE, cr);
969 dsl_dataset_rele(clone, FTAG);
970 dsl_dataset_rele(origin, FTAG);
972 dsl_pool_rele(dp, FTAG);
978 zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
982 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
983 ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
986 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
987 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
990 return (zfs_secpolicy_write_perms(zc->zc_name,
991 ZFS_DELEG_PERM_CREATE, cr));
995 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
997 return (zfs_secpolicy_write_perms(name,
998 ZFS_DELEG_PERM_SNAPSHOT, cr));
1002 * Check for permission to create each snapshot in the nvlist.
1006 zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1012 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
1013 return (SET_ERROR(EINVAL));
1014 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
1015 pair = nvlist_next_nvpair(snaps, pair)) {
1016 char *name = nvpair_name(pair);
1017 char *atp = strchr(name, '@');
1020 error = SET_ERROR(EINVAL);
1024 error = zfs_secpolicy_snapshot_perms(name, cr);
1033 * Check for permission to create each snapshot in the nvlist.
1037 zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1041 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
1042 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
1043 char *name = nvpair_name(pair);
1044 char *hashp = strchr(name, '#');
1046 if (hashp == NULL) {
1047 error = SET_ERROR(EINVAL);
1051 error = zfs_secpolicy_write_perms(name,
1052 ZFS_DELEG_PERM_BOOKMARK, cr);
1062 zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1064 nvpair_t *pair, *nextpair;
1067 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1069 char *name = nvpair_name(pair);
1070 char *hashp = strchr(name, '#');
1071 nextpair = nvlist_next_nvpair(innvl, pair);
1073 if (hashp == NULL) {
1074 error = SET_ERROR(EINVAL);
1079 error = zfs_secpolicy_write_perms(name,
1080 ZFS_DELEG_PERM_DESTROY, cr);
1082 if (error == ENOENT) {
1084 * Ignore any filesystems that don't exist (we consider
1085 * their bookmarks "already destroyed"). Remove
1086 * the name from the nvl here in case the filesystem
1087 * is created between now and when we try to destroy
1088 * the bookmark (in which case we don't want to
1089 * destroy it since we haven't checked for permission).
1091 fnvlist_remove_nvpair(innvl, pair);
1103 zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1106 * Even root must have a proper TSD so that we know what pool
1109 if (tsd_get(zfs_allow_log_key) == NULL)
1110 return (SET_ERROR(EPERM));
1115 zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1117 char parentname[MAXNAMELEN];
1121 if ((error = zfs_get_parent(zc->zc_name, parentname,
1122 sizeof (parentname))) != 0)
1125 if (nvlist_lookup_string(innvl, "origin", &origin) == 0 &&
1126 (error = zfs_secpolicy_write_perms(origin,
1127 ZFS_DELEG_PERM_CLONE, cr)) != 0)
1130 if ((error = zfs_secpolicy_write_perms(parentname,
1131 ZFS_DELEG_PERM_CREATE, cr)) != 0)
1134 return (zfs_secpolicy_write_perms(parentname,
1135 ZFS_DELEG_PERM_MOUNT, cr));
1139 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
1140 * SYS_CONFIG privilege, which is not available in a local zone.
1144 zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1146 if (secpolicy_sys_config(cr, B_FALSE) != 0)
1147 return (SET_ERROR(EPERM));
1153 * Policy for object to name lookups.
1157 zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1161 if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
1164 error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
1169 * Policy for fault injection. Requires all privileges.
1173 zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1175 return (secpolicy_zinject(cr));
1180 zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1182 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
1184 if (prop == ZPROP_INVAL) {
1185 if (!zfs_prop_user(zc->zc_value))
1186 return (SET_ERROR(EINVAL));
1187 return (zfs_secpolicy_write_perms(zc->zc_name,
1188 ZFS_DELEG_PERM_USERPROP, cr));
1190 return (zfs_secpolicy_setprop(zc->zc_name, prop,
1196 zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1198 int err = zfs_secpolicy_read(zc, innvl, cr);
1202 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1203 return (SET_ERROR(EINVAL));
1205 if (zc->zc_value[0] == 0) {
1207 * They are asking about a posix uid/gid. If it's
1208 * themself, allow it.
1210 if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
1211 zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
1212 if (zc->zc_guid == crgetuid(cr))
1215 if (groupmember(zc->zc_guid, cr))
1220 return (zfs_secpolicy_write_perms(zc->zc_name,
1221 userquota_perms[zc->zc_objset_type], cr));
1225 zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1227 int err = zfs_secpolicy_read(zc, innvl, cr);
1231 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1232 return (SET_ERROR(EINVAL));
1234 return (zfs_secpolicy_write_perms(zc->zc_name,
1235 userquota_perms[zc->zc_objset_type], cr));
1240 zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1242 return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
1248 zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1254 error = nvlist_lookup_nvlist(innvl, "holds", &holds);
1256 return (SET_ERROR(EINVAL));
1258 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
1259 pair = nvlist_next_nvpair(holds, pair)) {
1260 char fsname[MAXNAMELEN];
1261 error = dmu_fsname(nvpair_name(pair), fsname);
1264 error = zfs_secpolicy_write_perms(fsname,
1265 ZFS_DELEG_PERM_HOLD, cr);
1274 zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1279 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1280 pair = nvlist_next_nvpair(innvl, pair)) {
1281 char fsname[MAXNAMELEN];
1282 error = dmu_fsname(nvpair_name(pair), fsname);
1285 error = zfs_secpolicy_write_perms(fsname,
1286 ZFS_DELEG_PERM_RELEASE, cr);
1294 * Policy for allowing temporary snapshots to be taken or released
1297 zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1300 * A temporary snapshot is the same as a snapshot,
1301 * hold, destroy and release all rolled into one.
1302 * Delegated diff alone is sufficient that we allow this.
1306 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1307 ZFS_DELEG_PERM_DIFF, cr)) == 0)
1310 error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
1312 error = zfs_secpolicy_hold(zc, innvl, cr);
1314 error = zfs_secpolicy_release(zc, innvl, cr);
1316 error = zfs_secpolicy_destroy(zc, innvl, cr);
1321 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1324 get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1328 nvlist_t *list = NULL;
1331 * Read in and unpack the user-supplied nvlist.
1334 return (SET_ERROR(EINVAL));
1336 packed = kmem_alloc(size, KM_SLEEP);
1338 if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1340 kmem_free(packed, size);
1341 return (SET_ERROR(EFAULT));
1344 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1345 kmem_free(packed, size);
1349 kmem_free(packed, size);
1356 * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
1357 * Entries will be removed from the end of the nvlist, and one int32 entry
1358 * named "N_MORE_ERRORS" will be added indicating how many entries were
1362 nvlist_smush(nvlist_t *errors, size_t max)
1366 size = fnvlist_size(errors);
1369 nvpair_t *more_errors;
1373 return (SET_ERROR(ENOMEM));
1375 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
1376 more_errors = nvlist_prev_nvpair(errors, NULL);
1379 nvpair_t *pair = nvlist_prev_nvpair(errors,
1381 fnvlist_remove_nvpair(errors, pair);
1383 size = fnvlist_size(errors);
1384 } while (size > max);
1386 fnvlist_remove_nvpair(errors, more_errors);
1387 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
1388 ASSERT3U(fnvlist_size(errors), <=, max);
1395 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1397 char *packed = NULL;
1401 size = fnvlist_size(nvl);
1403 if (size > zc->zc_nvlist_dst_size) {
1405 * Solaris returns ENOMEM here, because even if an error is
1406 * returned from an ioctl(2), new zc_nvlist_dst_size will be
1407 * passed to the userland. This is not the case for FreeBSD.
1408 * We need to return 0, so the kernel will copy the
1409 * zc_nvlist_dst_size back and the userland can discover that a
1410 * bigger buffer is needed.
1414 packed = fnvlist_pack(nvl, &size);
1415 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1416 size, zc->zc_iflags) != 0)
1417 error = SET_ERROR(EFAULT);
1418 fnvlist_pack_free(packed, size);
1421 zc->zc_nvlist_dst_size = size;
1422 zc->zc_nvlist_dst_filled = B_TRUE;
1427 getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
1432 error = dmu_objset_hold(dsname, FTAG, &os);
1435 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1436 dmu_objset_rele(os, FTAG);
1437 return (SET_ERROR(EINVAL));
1440 mutex_enter(&os->os_user_ptr_lock);
1441 *zfvp = dmu_objset_get_user(os);
1443 VFS_HOLD((*zfvp)->z_vfs);
1445 error = SET_ERROR(ESRCH);
1447 mutex_exit(&os->os_user_ptr_lock);
1448 dmu_objset_rele(os, FTAG);
1453 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
1454 * case its z_vfs will be NULL, and it will be opened as the owner.
1455 * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1456 * which prevents all vnode ops from running.
1459 zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
1463 if (getzfsvfs(name, zfvp) != 0)
1464 error = zfsvfs_create(name, zfvp);
1466 rrm_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
1468 if ((*zfvp)->z_unmounted) {
1470 * XXX we could probably try again, since the unmounting
1471 * thread should be just about to disassociate the
1472 * objset from the zfsvfs.
1474 rrm_exit(&(*zfvp)->z_teardown_lock, tag);
1475 return (SET_ERROR(EBUSY));
1482 zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
1484 rrm_exit(&zfsvfs->z_teardown_lock, tag);
1486 if (zfsvfs->z_vfs) {
1487 VFS_RELE(zfsvfs->z_vfs);
1489 dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1490 zfsvfs_free(zfsvfs);
1495 zfs_ioc_pool_create(zfs_cmd_t *zc)
1498 nvlist_t *config, *props = NULL;
1499 nvlist_t *rootprops = NULL;
1500 nvlist_t *zplprops = NULL;
1502 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1503 zc->zc_iflags, &config))
1506 if (zc->zc_nvlist_src_size != 0 && (error =
1507 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1508 zc->zc_iflags, &props))) {
1509 nvlist_free(config);
1514 nvlist_t *nvl = NULL;
1515 uint64_t version = SPA_VERSION;
1517 (void) nvlist_lookup_uint64(props,
1518 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1519 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1520 error = SET_ERROR(EINVAL);
1521 goto pool_props_bad;
1523 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
1525 error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
1527 nvlist_free(config);
1531 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1533 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1534 error = zfs_fill_zplprops_root(version, rootprops,
1537 goto pool_props_bad;
1540 error = spa_create(zc->zc_name, config, props, zplprops);
1543 * Set the remaining root properties
1545 if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
1546 ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
1547 (void) spa_destroy(zc->zc_name);
1550 nvlist_free(rootprops);
1551 nvlist_free(zplprops);
1552 nvlist_free(config);
1559 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1562 zfs_log_history(zc);
1563 error = spa_destroy(zc->zc_name);
1565 zvol_remove_minors(zc->zc_name);
1570 zfs_ioc_pool_import(zfs_cmd_t *zc)
1572 nvlist_t *config, *props = NULL;
1576 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1577 zc->zc_iflags, &config)) != 0)
1580 if (zc->zc_nvlist_src_size != 0 && (error =
1581 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1582 zc->zc_iflags, &props))) {
1583 nvlist_free(config);
1587 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1588 guid != zc->zc_guid)
1589 error = SET_ERROR(EINVAL);
1591 error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1593 if (zc->zc_nvlist_dst != 0) {
1596 if ((err = put_nvlist(zc, config)) != 0)
1600 nvlist_free(config);
1609 zfs_ioc_pool_export(zfs_cmd_t *zc)
1612 boolean_t force = (boolean_t)zc->zc_cookie;
1613 boolean_t hardforce = (boolean_t)zc->zc_guid;
1615 zfs_log_history(zc);
1616 error = spa_export(zc->zc_name, NULL, force, hardforce);
1618 zvol_remove_minors(zc->zc_name);
1623 zfs_ioc_pool_configs(zfs_cmd_t *zc)
1628 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1629 return (SET_ERROR(EEXIST));
1631 error = put_nvlist(zc, configs);
1633 nvlist_free(configs);
1640 * zc_name name of the pool
1643 * zc_cookie real errno
1644 * zc_nvlist_dst config nvlist
1645 * zc_nvlist_dst_size size of config nvlist
1648 zfs_ioc_pool_stats(zfs_cmd_t *zc)
1654 error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1655 sizeof (zc->zc_value));
1657 if (config != NULL) {
1658 ret = put_nvlist(zc, config);
1659 nvlist_free(config);
1662 * The config may be present even if 'error' is non-zero.
1663 * In this case we return success, and preserve the real errno
1666 zc->zc_cookie = error;
1675 * Try to import the given pool, returning pool stats as appropriate so that
1676 * user land knows which devices are available and overall pool health.
1679 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1681 nvlist_t *tryconfig, *config;
1684 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1685 zc->zc_iflags, &tryconfig)) != 0)
1688 config = spa_tryimport(tryconfig);
1690 nvlist_free(tryconfig);
1693 return (SET_ERROR(EINVAL));
1695 error = put_nvlist(zc, config);
1696 nvlist_free(config);
1703 * zc_name name of the pool
1704 * zc_cookie scan func (pool_scan_func_t)
1707 zfs_ioc_pool_scan(zfs_cmd_t *zc)
1712 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1715 if (zc->zc_cookie == POOL_SCAN_NONE)
1716 error = spa_scan_stop(spa);
1718 error = spa_scan(spa, zc->zc_cookie);
1720 spa_close(spa, FTAG);
1726 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1731 error = spa_open(zc->zc_name, &spa, FTAG);
1734 spa_close(spa, FTAG);
1740 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1745 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1748 if (zc->zc_cookie < spa_version(spa) ||
1749 !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1750 spa_close(spa, FTAG);
1751 return (SET_ERROR(EINVAL));
1754 spa_upgrade(spa, zc->zc_cookie);
1755 spa_close(spa, FTAG);
1761 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1768 if ((size = zc->zc_history_len) == 0)
1769 return (SET_ERROR(EINVAL));
1771 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1774 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1775 spa_close(spa, FTAG);
1776 return (SET_ERROR(ENOTSUP));
1779 hist_buf = kmem_alloc(size, KM_SLEEP);
1780 if ((error = spa_history_get(spa, &zc->zc_history_offset,
1781 &zc->zc_history_len, hist_buf)) == 0) {
1782 error = ddi_copyout(hist_buf,
1783 (void *)(uintptr_t)zc->zc_history,
1784 zc->zc_history_len, zc->zc_iflags);
1787 spa_close(spa, FTAG);
1788 kmem_free(hist_buf, size);
1793 zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1798 error = spa_open(zc->zc_name, &spa, FTAG);
1800 error = spa_change_guid(spa);
1801 spa_close(spa, FTAG);
1807 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1809 return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
1814 * zc_name name of filesystem
1815 * zc_obj object to find
1818 * zc_value name of object
1821 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1826 /* XXX reading from objset not owned */
1827 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1829 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1830 dmu_objset_rele(os, FTAG);
1831 return (SET_ERROR(EINVAL));
1833 error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
1834 sizeof (zc->zc_value));
1835 dmu_objset_rele(os, FTAG);
1842 * zc_name name of filesystem
1843 * zc_obj object to find
1846 * zc_stat stats on object
1847 * zc_value path to object
1850 zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1855 /* XXX reading from objset not owned */
1856 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1858 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1859 dmu_objset_rele(os, FTAG);
1860 return (SET_ERROR(EINVAL));
1862 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1863 sizeof (zc->zc_value));
1864 dmu_objset_rele(os, FTAG);
1870 zfs_ioc_vdev_add(zfs_cmd_t *zc)
1874 nvlist_t *config, **l2cache, **spares;
1875 uint_t nl2cache = 0, nspares = 0;
1877 error = spa_open(zc->zc_name, &spa, FTAG);
1881 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1882 zc->zc_iflags, &config);
1883 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1884 &l2cache, &nl2cache);
1886 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1891 * A root pool with concatenated devices is not supported.
1892 * Thus, can not add a device to a root pool.
1894 * Intent log device can not be added to a rootpool because
1895 * during mountroot, zil is replayed, a seperated log device
1896 * can not be accessed during the mountroot time.
1898 * l2cache and spare devices are ok to be added to a rootpool.
1900 if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
1901 nvlist_free(config);
1902 spa_close(spa, FTAG);
1903 return (SET_ERROR(EDOM));
1905 #endif /* illumos */
1908 error = spa_vdev_add(spa, config);
1909 nvlist_free(config);
1911 spa_close(spa, FTAG);
1917 * zc_name name of the pool
1918 * zc_nvlist_conf nvlist of devices to remove
1919 * zc_cookie to stop the remove?
1922 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1927 error = spa_open(zc->zc_name, &spa, FTAG);
1930 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1931 spa_close(spa, FTAG);
1936 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1940 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1942 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1944 switch (zc->zc_cookie) {
1945 case VDEV_STATE_ONLINE:
1946 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1949 case VDEV_STATE_OFFLINE:
1950 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1953 case VDEV_STATE_FAULTED:
1954 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1955 zc->zc_obj != VDEV_AUX_EXTERNAL)
1956 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1958 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
1961 case VDEV_STATE_DEGRADED:
1962 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1963 zc->zc_obj != VDEV_AUX_EXTERNAL)
1964 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1966 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
1970 error = SET_ERROR(EINVAL);
1972 zc->zc_cookie = newstate;
1973 spa_close(spa, FTAG);
1978 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1981 int replacing = zc->zc_cookie;
1985 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1988 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1989 zc->zc_iflags, &config)) == 0) {
1990 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1991 nvlist_free(config);
1994 spa_close(spa, FTAG);
1999 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
2004 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2007 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
2009 spa_close(spa, FTAG);
2014 zfs_ioc_vdev_split(zfs_cmd_t *zc)
2017 nvlist_t *config, *props = NULL;
2019 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
2021 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2024 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
2025 zc->zc_iflags, &config)) {
2026 spa_close(spa, FTAG);
2030 if (zc->zc_nvlist_src_size != 0 && (error =
2031 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2032 zc->zc_iflags, &props))) {
2033 spa_close(spa, FTAG);
2034 nvlist_free(config);
2038 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
2040 spa_close(spa, FTAG);
2042 nvlist_free(config);
2049 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
2052 char *path = zc->zc_value;
2053 uint64_t guid = zc->zc_guid;
2056 error = spa_open(zc->zc_name, &spa, FTAG);
2060 error = spa_vdev_setpath(spa, guid, path);
2061 spa_close(spa, FTAG);
2066 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
2069 char *fru = zc->zc_value;
2070 uint64_t guid = zc->zc_guid;
2073 error = spa_open(zc->zc_name, &spa, FTAG);
2077 error = spa_vdev_setfru(spa, guid, fru);
2078 spa_close(spa, FTAG);
2083 zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
2088 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2090 if (zc->zc_nvlist_dst != 0 &&
2091 (error = dsl_prop_get_all(os, &nv)) == 0) {
2092 dmu_objset_stats(os, nv);
2094 * NB: zvol_get_stats() will read the objset contents,
2095 * which we aren't supposed to do with a
2096 * DS_MODE_USER hold, because it could be
2097 * inconsistent. So this is a bit of a workaround...
2098 * XXX reading with out owning
2100 if (!zc->zc_objset_stats.dds_inconsistent &&
2101 dmu_objset_type(os) == DMU_OST_ZVOL) {
2102 error = zvol_get_stats(os, nv);
2107 error = put_nvlist(zc, nv);
2116 * zc_name name of filesystem
2117 * zc_nvlist_dst_size size of buffer for property nvlist
2120 * zc_objset_stats stats
2121 * zc_nvlist_dst property nvlist
2122 * zc_nvlist_dst_size size of property nvlist
2125 zfs_ioc_objset_stats(zfs_cmd_t *zc)
2130 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2132 error = zfs_ioc_objset_stats_impl(zc, os);
2133 dmu_objset_rele(os, FTAG);
2136 if (error == ENOMEM)
2143 * zc_name name of filesystem
2144 * zc_nvlist_dst_size size of buffer for property nvlist
2147 * zc_nvlist_dst received property nvlist
2148 * zc_nvlist_dst_size size of received property nvlist
2150 * Gets received properties (distinct from local properties on or after
2151 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
2152 * local property values.
2155 zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
2161 * Without this check, we would return local property values if the
2162 * caller has not already received properties on or after
2163 * SPA_VERSION_RECVD_PROPS.
2165 if (!dsl_prop_get_hasrecvd(zc->zc_name))
2166 return (SET_ERROR(ENOTSUP));
2168 if (zc->zc_nvlist_dst != 0 &&
2169 (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
2170 error = put_nvlist(zc, nv);
2178 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2184 * zfs_get_zplprop() will either find a value or give us
2185 * the default value (if there is one).
2187 if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2189 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2195 * zc_name name of filesystem
2196 * zc_nvlist_dst_size size of buffer for zpl property nvlist
2199 * zc_nvlist_dst zpl property nvlist
2200 * zc_nvlist_dst_size size of zpl property nvlist
2203 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2208 /* XXX reading without owning */
2209 if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
2212 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2215 * NB: nvl_add_zplprop() will read the objset contents,
2216 * which we aren't supposed to do with a DS_MODE_USER
2217 * hold, because it could be inconsistent.
2219 if (zc->zc_nvlist_dst != 0 &&
2220 !zc->zc_objset_stats.dds_inconsistent &&
2221 dmu_objset_type(os) == DMU_OST_ZFS) {
2224 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2225 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2226 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2227 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2228 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2229 err = put_nvlist(zc, nv);
2232 err = SET_ERROR(ENOENT);
2234 dmu_objset_rele(os, FTAG);
2239 dataset_name_hidden(const char *name)
2242 * Skip over datasets that are not visible in this zone,
2243 * internal datasets (which have a $ in their name), and
2244 * temporary datasets (which have a % in their name).
2246 if (strchr(name, '$') != NULL)
2248 if (strchr(name, '%') != NULL)
2250 if (!INGLOBALZONE(curthread) && !zone_dataset_visible(name, NULL))
2257 * zc_name name of filesystem
2258 * zc_cookie zap cursor
2259 * zc_nvlist_dst_size size of buffer for property nvlist
2262 * zc_name name of next filesystem
2263 * zc_cookie zap cursor
2264 * zc_objset_stats stats
2265 * zc_nvlist_dst property nvlist
2266 * zc_nvlist_dst_size size of property nvlist
2269 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2274 size_t orig_len = strlen(zc->zc_name);
2277 if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
2278 if (error == ENOENT)
2279 error = SET_ERROR(ESRCH);
2283 p = strrchr(zc->zc_name, '/');
2284 if (p == NULL || p[1] != '\0')
2285 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2286 p = zc->zc_name + strlen(zc->zc_name);
2289 error = dmu_dir_list_next(os,
2290 sizeof (zc->zc_name) - (p - zc->zc_name), p,
2291 NULL, &zc->zc_cookie);
2292 if (error == ENOENT)
2293 error = SET_ERROR(ESRCH);
2294 } while (error == 0 && dataset_name_hidden(zc->zc_name));
2295 dmu_objset_rele(os, FTAG);
2298 * If it's an internal dataset (ie. with a '$' in its name),
2299 * don't try to get stats for it, otherwise we'll return ENOENT.
2301 if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
2302 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2303 if (error == ENOENT) {
2304 /* We lost a race with destroy, get the next one. */
2305 zc->zc_name[orig_len] = '\0';
2314 * zc_name name of filesystem
2315 * zc_cookie zap cursor
2316 * zc_nvlist_dst_size size of buffer for property nvlist
2317 * zc_simple when set, only name is requested
2320 * zc_name name of next snapshot
2321 * zc_objset_stats stats
2322 * zc_nvlist_dst property nvlist
2323 * zc_nvlist_dst_size size of property nvlist
2326 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2331 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2333 return (error == ENOENT ? ESRCH : error);
2337 * A dataset name of maximum length cannot have any snapshots,
2338 * so exit immediately.
2340 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
2341 dmu_objset_rele(os, FTAG);
2342 return (SET_ERROR(ESRCH));
2345 error = dmu_snapshot_list_next(os,
2346 sizeof (zc->zc_name) - strlen(zc->zc_name),
2347 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2350 if (error == 0 && !zc->zc_simple) {
2352 dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2354 error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
2358 error = dmu_objset_from_ds(ds, &ossnap);
2360 error = zfs_ioc_objset_stats_impl(zc, ossnap);
2361 dsl_dataset_rele(ds, FTAG);
2363 } else if (error == ENOENT) {
2364 error = SET_ERROR(ESRCH);
2367 dmu_objset_rele(os, FTAG);
2368 /* if we failed, undo the @ that we tacked on to zc_name */
2370 *strchr(zc->zc_name, '@') = '\0';
2375 zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2377 const char *propname = nvpair_name(pair);
2379 unsigned int vallen;
2382 zfs_userquota_prop_t type;
2388 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2390 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2391 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2393 return (SET_ERROR(EINVAL));
2397 * A correctly constructed propname is encoded as
2398 * userquota@<rid>-<domain>.
2400 if ((dash = strchr(propname, '-')) == NULL ||
2401 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2403 return (SET_ERROR(EINVAL));
2410 err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
2412 err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
2413 zfsvfs_rele(zfsvfs, FTAG);
2420 * If the named property is one that has a special function to set its value,
2421 * return 0 on success and a positive error code on failure; otherwise if it is
2422 * not one of the special properties handled by this function, return -1.
2424 * XXX: It would be better for callers of the property interface if we handled
2425 * these special cases in dsl_prop.c (in the dsl layer).
2428 zfs_prop_set_special(const char *dsname, zprop_source_t source,
2431 const char *propname = nvpair_name(pair);
2432 zfs_prop_t prop = zfs_name_to_prop(propname);
2436 if (prop == ZPROP_INVAL) {
2437 if (zfs_prop_userquota(propname))
2438 return (zfs_prop_set_userquota(dsname, pair));
2442 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2444 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2445 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2449 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
2452 VERIFY(0 == nvpair_value_uint64(pair, &intval));
2455 case ZFS_PROP_QUOTA:
2456 err = dsl_dir_set_quota(dsname, source, intval);
2458 case ZFS_PROP_REFQUOTA:
2459 err = dsl_dataset_set_refquota(dsname, source, intval);
2461 case ZFS_PROP_FILESYSTEM_LIMIT:
2462 case ZFS_PROP_SNAPSHOT_LIMIT:
2463 if (intval == UINT64_MAX) {
2464 /* clearing the limit, just do it */
2467 err = dsl_dir_activate_fs_ss_limit(dsname);
2470 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2471 * default path to set the value in the nvlist.
2476 case ZFS_PROP_RESERVATION:
2477 err = dsl_dir_set_reservation(dsname, source, intval);
2479 case ZFS_PROP_REFRESERVATION:
2480 err = dsl_dataset_set_refreservation(dsname, source, intval);
2482 case ZFS_PROP_VOLSIZE:
2483 err = zvol_set_volsize(dsname, intval);
2485 case ZFS_PROP_VERSION:
2489 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2492 err = zfs_set_version(zfsvfs, intval);
2493 zfsvfs_rele(zfsvfs, FTAG);
2495 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2498 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2499 (void) strcpy(zc->zc_name, dsname);
2500 (void) zfs_ioc_userspace_upgrade(zc);
2501 kmem_free(zc, sizeof (zfs_cmd_t));
2513 * This function is best effort. If it fails to set any of the given properties,
2514 * it continues to set as many as it can and returns the last error
2515 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2516 * with the list of names of all the properties that failed along with the
2517 * corresponding error numbers.
2519 * If every property is set successfully, zero is returned and errlist is not
2523 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2531 nvlist_t *genericnvl = fnvlist_alloc();
2532 nvlist_t *retrynvl = fnvlist_alloc();
2536 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2537 const char *propname = nvpair_name(pair);
2538 zfs_prop_t prop = zfs_name_to_prop(propname);
2541 /* decode the property value */
2543 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2545 attrs = fnvpair_value_nvlist(pair);
2546 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2548 err = SET_ERROR(EINVAL);
2551 /* Validate value type */
2552 if (err == 0 && prop == ZPROP_INVAL) {
2553 if (zfs_prop_user(propname)) {
2554 if (nvpair_type(propval) != DATA_TYPE_STRING)
2555 err = SET_ERROR(EINVAL);
2556 } else if (zfs_prop_userquota(propname)) {
2557 if (nvpair_type(propval) !=
2558 DATA_TYPE_UINT64_ARRAY)
2559 err = SET_ERROR(EINVAL);
2561 err = SET_ERROR(EINVAL);
2563 } else if (err == 0) {
2564 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2565 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2566 err = SET_ERROR(EINVAL);
2567 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2570 intval = fnvpair_value_uint64(propval);
2572 switch (zfs_prop_get_type(prop)) {
2573 case PROP_TYPE_NUMBER:
2575 case PROP_TYPE_STRING:
2576 err = SET_ERROR(EINVAL);
2578 case PROP_TYPE_INDEX:
2579 if (zfs_prop_index_to_string(prop,
2580 intval, &unused) != 0)
2581 err = SET_ERROR(EINVAL);
2585 "unknown property type");
2588 err = SET_ERROR(EINVAL);
2592 /* Validate permissions */
2594 err = zfs_check_settable(dsname, pair, CRED());
2597 err = zfs_prop_set_special(dsname, source, pair);
2600 * For better performance we build up a list of
2601 * properties to set in a single transaction.
2603 err = nvlist_add_nvpair(genericnvl, pair);
2604 } else if (err != 0 && nvl != retrynvl) {
2606 * This may be a spurious error caused by
2607 * receiving quota and reservation out of order.
2608 * Try again in a second pass.
2610 err = nvlist_add_nvpair(retrynvl, pair);
2615 if (errlist != NULL)
2616 fnvlist_add_int32(errlist, propname, err);
2621 if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2626 if (!nvlist_empty(genericnvl) &&
2627 dsl_props_set(dsname, source, genericnvl) != 0) {
2629 * If this fails, we still want to set as many properties as we
2630 * can, so try setting them individually.
2633 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2634 const char *propname = nvpair_name(pair);
2638 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2640 attrs = fnvpair_value_nvlist(pair);
2641 propval = fnvlist_lookup_nvpair(attrs,
2645 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2646 strval = fnvpair_value_string(propval);
2647 err = dsl_prop_set_string(dsname, propname,
2650 intval = fnvpair_value_uint64(propval);
2651 err = dsl_prop_set_int(dsname, propname, source,
2656 if (errlist != NULL) {
2657 fnvlist_add_int32(errlist, propname,
2664 nvlist_free(genericnvl);
2665 nvlist_free(retrynvl);
2671 * Check that all the properties are valid user properties.
2674 zfs_check_userprops(const char *fsname, nvlist_t *nvl)
2676 nvpair_t *pair = NULL;
2679 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2680 const char *propname = nvpair_name(pair);
2682 if (!zfs_prop_user(propname) ||
2683 nvpair_type(pair) != DATA_TYPE_STRING)
2684 return (SET_ERROR(EINVAL));
2686 if (error = zfs_secpolicy_write_perms(fsname,
2687 ZFS_DELEG_PERM_USERPROP, CRED()))
2690 if (strlen(propname) >= ZAP_MAXNAMELEN)
2691 return (SET_ERROR(ENAMETOOLONG));
2693 if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN)
2700 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2704 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2707 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2708 if (nvlist_exists(skipped, nvpair_name(pair)))
2711 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2716 clear_received_props(const char *dsname, nvlist_t *props,
2720 nvlist_t *cleared_props = NULL;
2721 props_skip(props, skipped, &cleared_props);
2722 if (!nvlist_empty(cleared_props)) {
2724 * Acts on local properties until the dataset has received
2725 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2727 zprop_source_t flags = (ZPROP_SRC_NONE |
2728 (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
2729 err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
2731 nvlist_free(cleared_props);
2737 * zc_name name of filesystem
2738 * zc_value name of property to set
2739 * zc_nvlist_src{_size} nvlist of properties to apply
2740 * zc_cookie received properties flag
2743 * zc_nvlist_dst{_size} error for each unapplied received property
2746 zfs_ioc_set_prop(zfs_cmd_t *zc)
2749 boolean_t received = zc->zc_cookie;
2750 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2755 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2756 zc->zc_iflags, &nvl)) != 0)
2760 nvlist_t *origprops;
2762 if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
2763 (void) clear_received_props(zc->zc_name,
2765 nvlist_free(origprops);
2768 error = dsl_prop_set_hasrecvd(zc->zc_name);
2771 errors = fnvlist_alloc();
2773 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
2775 if (zc->zc_nvlist_dst != 0 && errors != NULL) {
2776 (void) put_nvlist(zc, errors);
2779 nvlist_free(errors);
2786 * zc_name name of filesystem
2787 * zc_value name of property to inherit
2788 * zc_cookie revert to received value if TRUE
2793 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2795 const char *propname = zc->zc_value;
2796 zfs_prop_t prop = zfs_name_to_prop(propname);
2797 boolean_t received = zc->zc_cookie;
2798 zprop_source_t source = (received
2799 ? ZPROP_SRC_NONE /* revert to received value, if any */
2800 : ZPROP_SRC_INHERITED); /* explicitly inherit */
2809 * zfs_prop_set_special() expects properties in the form of an
2810 * nvpair with type info.
2812 if (prop == ZPROP_INVAL) {
2813 if (!zfs_prop_user(propname))
2814 return (SET_ERROR(EINVAL));
2816 type = PROP_TYPE_STRING;
2817 } else if (prop == ZFS_PROP_VOLSIZE ||
2818 prop == ZFS_PROP_VERSION) {
2819 return (SET_ERROR(EINVAL));
2821 type = zfs_prop_get_type(prop);
2824 VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2827 case PROP_TYPE_STRING:
2828 VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2830 case PROP_TYPE_NUMBER:
2831 case PROP_TYPE_INDEX:
2832 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2836 return (SET_ERROR(EINVAL));
2839 pair = nvlist_next_nvpair(dummy, NULL);
2840 err = zfs_prop_set_special(zc->zc_name, source, pair);
2843 return (err); /* special property already handled */
2846 * Only check this in the non-received case. We want to allow
2847 * 'inherit -S' to revert non-inheritable properties like quota
2848 * and reservation to the received or default values even though
2849 * they are not considered inheritable.
2851 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2852 return (SET_ERROR(EINVAL));
2855 /* property name has been validated by zfs_secpolicy_inherit_prop() */
2856 return (dsl_prop_inherit(zc->zc_name, zc->zc_value, source));
2860 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2867 if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2868 zc->zc_iflags, &props))
2872 * If the only property is the configfile, then just do a spa_lookup()
2873 * to handle the faulted case.
2875 pair = nvlist_next_nvpair(props, NULL);
2876 if (pair != NULL && strcmp(nvpair_name(pair),
2877 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2878 nvlist_next_nvpair(props, pair) == NULL) {
2879 mutex_enter(&spa_namespace_lock);
2880 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2881 spa_configfile_set(spa, props, B_FALSE);
2882 spa_config_sync(spa, B_FALSE, B_TRUE);
2884 mutex_exit(&spa_namespace_lock);
2891 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2896 error = spa_prop_set(spa, props);
2899 spa_close(spa, FTAG);
2905 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2909 nvlist_t *nvp = NULL;
2911 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2913 * If the pool is faulted, there may be properties we can still
2914 * get (such as altroot and cachefile), so attempt to get them
2917 mutex_enter(&spa_namespace_lock);
2918 if ((spa = spa_lookup(zc->zc_name)) != NULL)
2919 error = spa_prop_get(spa, &nvp);
2920 mutex_exit(&spa_namespace_lock);
2922 error = spa_prop_get(spa, &nvp);
2923 spa_close(spa, FTAG);
2926 if (error == 0 && zc->zc_nvlist_dst != 0)
2927 error = put_nvlist(zc, nvp);
2929 error = SET_ERROR(EFAULT);
2937 * zc_name name of filesystem
2938 * zc_nvlist_src{_size} nvlist of delegated permissions
2939 * zc_perm_action allow/unallow flag
2944 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2947 nvlist_t *fsaclnv = NULL;
2949 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2950 zc->zc_iflags, &fsaclnv)) != 0)
2954 * Verify nvlist is constructed correctly
2956 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2957 nvlist_free(fsaclnv);
2958 return (SET_ERROR(EINVAL));
2962 * If we don't have PRIV_SYS_MOUNT, then validate
2963 * that user is allowed to hand out each permission in
2967 error = secpolicy_zfs(CRED());
2969 if (zc->zc_perm_action == B_FALSE) {
2970 error = dsl_deleg_can_allow(zc->zc_name,
2973 error = dsl_deleg_can_unallow(zc->zc_name,
2979 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2981 nvlist_free(fsaclnv);
2987 * zc_name name of filesystem
2990 * zc_nvlist_src{_size} nvlist of delegated permissions
2993 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
2998 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
2999 error = put_nvlist(zc, nvp);
3007 * Search the vfs list for a specified resource. Returns a pointer to it
3008 * or NULL if no suitable entry is found. The caller of this routine
3009 * is responsible for releasing the returned vfs pointer.
3012 zfs_get_vfs(const char *resource)
3016 mtx_lock(&mountlist_mtx);
3017 TAILQ_FOREACH(vfsp, &mountlist, mnt_list) {
3018 if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
3023 mtx_unlock(&mountlist_mtx);
3029 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3031 zfs_creat_t *zct = arg;
3033 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
3036 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
3040 * os parent objset pointer (NULL if root fs)
3041 * fuids_ok fuids allowed in this version of the spa?
3042 * sa_ok SAs allowed in this version of the spa?
3043 * createprops list of properties requested by creator
3046 * zplprops values for the zplprops we attach to the master node object
3047 * is_ci true if requested file system will be purely case-insensitive
3049 * Determine the settings for utf8only, normalization and
3050 * casesensitivity. Specific values may have been requested by the
3051 * creator and/or we can inherit values from the parent dataset. If
3052 * the file system is of too early a vintage, a creator can not
3053 * request settings for these properties, even if the requested
3054 * setting is the default value. We don't actually want to create dsl
3055 * properties for these, so remove them from the source nvlist after
3059 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
3060 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
3061 nvlist_t *zplprops, boolean_t *is_ci)
3063 uint64_t sense = ZFS_PROP_UNDEFINED;
3064 uint64_t norm = ZFS_PROP_UNDEFINED;
3065 uint64_t u8 = ZFS_PROP_UNDEFINED;
3067 ASSERT(zplprops != NULL);
3070 * Pull out creator prop choices, if any.
3073 (void) nvlist_lookup_uint64(createprops,
3074 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
3075 (void) nvlist_lookup_uint64(createprops,
3076 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
3077 (void) nvlist_remove_all(createprops,
3078 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
3079 (void) nvlist_lookup_uint64(createprops,
3080 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
3081 (void) nvlist_remove_all(createprops,
3082 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
3083 (void) nvlist_lookup_uint64(createprops,
3084 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
3085 (void) nvlist_remove_all(createprops,
3086 zfs_prop_to_name(ZFS_PROP_CASE));
3090 * If the zpl version requested is whacky or the file system
3091 * or pool is version is too "young" to support normalization
3092 * and the creator tried to set a value for one of the props,
3095 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
3096 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
3097 (zplver >= ZPL_VERSION_SA && !sa_ok) ||
3098 (zplver < ZPL_VERSION_NORMALIZATION &&
3099 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
3100 sense != ZFS_PROP_UNDEFINED)))
3101 return (SET_ERROR(ENOTSUP));
3104 * Put the version in the zplprops
3106 VERIFY(nvlist_add_uint64(zplprops,
3107 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3109 if (norm == ZFS_PROP_UNDEFINED)
3110 VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
3111 VERIFY(nvlist_add_uint64(zplprops,
3112 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3115 * If we're normalizing, names must always be valid UTF-8 strings.
3119 if (u8 == ZFS_PROP_UNDEFINED)
3120 VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
3121 VERIFY(nvlist_add_uint64(zplprops,
3122 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3124 if (sense == ZFS_PROP_UNDEFINED)
3125 VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
3126 VERIFY(nvlist_add_uint64(zplprops,
3127 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3130 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
3136 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
3137 nvlist_t *zplprops, boolean_t *is_ci)
3139 boolean_t fuids_ok, sa_ok;
3140 uint64_t zplver = ZPL_VERSION;
3141 objset_t *os = NULL;
3142 char parentname[MAXNAMELEN];
3148 (void) strlcpy(parentname, dataset, sizeof (parentname));
3149 cp = strrchr(parentname, '/');
3153 if ((error = spa_open(dataset, &spa, FTAG)) != 0)
3156 spa_vers = spa_version(spa);
3157 spa_close(spa, FTAG);
3159 zplver = zfs_zpl_version_map(spa_vers);
3160 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3161 sa_ok = (zplver >= ZPL_VERSION_SA);
3164 * Open parent object set so we can inherit zplprop values.
3166 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
3169 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
3171 dmu_objset_rele(os, FTAG);
3176 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
3177 nvlist_t *zplprops, boolean_t *is_ci)
3181 uint64_t zplver = ZPL_VERSION;
3184 zplver = zfs_zpl_version_map(spa_vers);
3185 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3186 sa_ok = (zplver >= ZPL_VERSION_SA);
3188 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
3189 createprops, zplprops, is_ci);
3195 * "type" -> dmu_objset_type_t (int32)
3196 * (optional) "props" -> { prop -> value }
3199 * outnvl: propname -> error code (int32)
3202 zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3205 zfs_creat_t zct = { 0 };
3206 nvlist_t *nvprops = NULL;
3207 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
3209 dmu_objset_type_t type;
3210 boolean_t is_insensitive = B_FALSE;
3212 if (nvlist_lookup_int32(innvl, "type", &type32) != 0)
3213 return (SET_ERROR(EINVAL));
3215 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3219 cbfunc = zfs_create_cb;
3223 cbfunc = zvol_create_cb;
3230 if (strchr(fsname, '@') ||
3231 strchr(fsname, '%'))
3232 return (SET_ERROR(EINVAL));
3234 zct.zct_props = nvprops;
3237 return (SET_ERROR(EINVAL));
3239 if (type == DMU_OST_ZVOL) {
3240 uint64_t volsize, volblocksize;
3242 if (nvprops == NULL)
3243 return (SET_ERROR(EINVAL));
3244 if (nvlist_lookup_uint64(nvprops,
3245 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3246 return (SET_ERROR(EINVAL));
3248 if ((error = nvlist_lookup_uint64(nvprops,
3249 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3250 &volblocksize)) != 0 && error != ENOENT)
3251 return (SET_ERROR(EINVAL));
3254 volblocksize = zfs_prop_default_numeric(
3255 ZFS_PROP_VOLBLOCKSIZE);
3257 if ((error = zvol_check_volblocksize(
3258 volblocksize)) != 0 ||
3259 (error = zvol_check_volsize(volsize,
3260 volblocksize)) != 0)
3262 } else if (type == DMU_OST_ZFS) {
3266 * We have to have normalization and
3267 * case-folding flags correct when we do the
3268 * file system creation, so go figure them out
3271 VERIFY(nvlist_alloc(&zct.zct_zplprops,
3272 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3273 error = zfs_fill_zplprops(fsname, nvprops,
3274 zct.zct_zplprops, &is_insensitive);
3276 nvlist_free(zct.zct_zplprops);
3281 error = dmu_objset_create(fsname, type,
3282 is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
3283 nvlist_free(zct.zct_zplprops);
3286 * It would be nice to do this atomically.
3289 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3292 (void) dsl_destroy_head(fsname);
3295 if (error == 0 && type == DMU_OST_ZVOL)
3296 zvol_create_minors(fsname);
3303 * "origin" -> name of origin snapshot
3304 * (optional) "props" -> { prop -> value }
3307 * outnvl: propname -> error code (int32)
3310 zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3313 nvlist_t *nvprops = NULL;
3316 if (nvlist_lookup_string(innvl, "origin", &origin_name) != 0)
3317 return (SET_ERROR(EINVAL));
3318 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3320 if (strchr(fsname, '@') ||
3321 strchr(fsname, '%'))
3322 return (SET_ERROR(EINVAL));
3324 if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3325 return (SET_ERROR(EINVAL));
3326 error = dmu_objset_clone(fsname, origin_name);
3331 * It would be nice to do this atomically.
3334 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3337 (void) dsl_destroy_head(fsname);
3341 zvol_create_minors(fsname);
3348 * "snaps" -> { snapshot1, snapshot2 }
3349 * (optional) "props" -> { prop -> value (string) }
3352 * outnvl: snapshot -> error code (int32)
3355 zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3358 nvlist_t *props = NULL;
3362 (void) nvlist_lookup_nvlist(innvl, "props", &props);
3363 if ((error = zfs_check_userprops(poolname, props)) != 0)
3366 if (!nvlist_empty(props) &&
3367 zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3368 return (SET_ERROR(ENOTSUP));
3370 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3371 return (SET_ERROR(EINVAL));
3372 poollen = strlen(poolname);
3373 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3374 pair = nvlist_next_nvpair(snaps, pair)) {
3375 const char *name = nvpair_name(pair);
3376 const char *cp = strchr(name, '@');
3379 * The snap name must contain an @, and the part after it must
3380 * contain only valid characters.
3383 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3384 return (SET_ERROR(EINVAL));
3387 * The snap must be in the specified pool.
3389 if (strncmp(name, poolname, poollen) != 0 ||
3390 (name[poollen] != '/' && name[poollen] != '@'))
3391 return (SET_ERROR(EXDEV));
3393 /* This must be the only snap of this fs. */
3394 for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
3395 pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
3396 if (strncmp(name, nvpair_name(pair2), cp - name + 1)
3398 return (SET_ERROR(EXDEV));
3403 error = dsl_dataset_snapshot(snaps, props, outnvl);
3408 * innvl: "message" -> string
3412 zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
3420 * The poolname in the ioctl is not set, we get it from the TSD,
3421 * which was set at the end of the last successful ioctl that allows
3422 * logging. The secpolicy func already checked that it is set.
3423 * Only one log ioctl is allowed after each successful ioctl, so
3424 * we clear the TSD here.
3426 poolname = tsd_get(zfs_allow_log_key);
3427 (void) tsd_set(zfs_allow_log_key, NULL);
3428 error = spa_open(poolname, &spa, FTAG);
3433 if (nvlist_lookup_string(innvl, "message", &message) != 0) {
3434 spa_close(spa, FTAG);
3435 return (SET_ERROR(EINVAL));
3438 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
3439 spa_close(spa, FTAG);
3440 return (SET_ERROR(ENOTSUP));
3443 error = spa_history_log(spa, message);
3444 spa_close(spa, FTAG);
3449 * The dp_config_rwlock must not be held when calling this, because the
3450 * unmount may need to write out data.
3452 * This function is best-effort. Callers must deal gracefully if it
3453 * remains mounted (or is remounted after this call).
3455 * Returns 0 if the argument is not a snapshot, or it is not currently a
3456 * filesystem, or we were able to unmount it. Returns error code otherwise.
3459 zfs_unmount_snap(const char *snapname)
3465 if (strchr(snapname, '@') == NULL)
3468 vfsp = zfs_get_vfs(snapname);
3472 zfsvfs = vfsp->vfs_data;
3473 ASSERT(!dsl_pool_config_held(dmu_objset_pool(zfsvfs->z_os)));
3475 err = vn_vfswlock(vfsp->vfs_vnodecovered);
3478 return (SET_ERROR(err));
3481 * Always force the unmount for snapshots.
3485 (void) dounmount(vfsp, MS_FORCE, kcred);
3488 (void) dounmount(vfsp, MS_FORCE, curthread);
3495 zfs_unmount_snap_cb(const char *snapname, void *arg)
3497 return (zfs_unmount_snap(snapname));
3501 * When a clone is destroyed, its origin may also need to be destroyed,
3502 * in which case it must be unmounted. This routine will do that unmount
3506 zfs_destroy_unmount_origin(const char *fsname)
3512 error = dmu_objset_hold(fsname, FTAG, &os);
3515 ds = dmu_objset_ds(os);
3516 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
3517 char originname[MAXNAMELEN];
3518 dsl_dataset_name(ds->ds_prev, originname);
3519 dmu_objset_rele(os, FTAG);
3520 (void) zfs_unmount_snap(originname);
3522 dmu_objset_rele(os, FTAG);
3528 * "snaps" -> { snapshot1, snapshot2 }
3529 * (optional boolean) "defer"
3532 * outnvl: snapshot -> error code (int32)
3537 zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3544 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3545 return (SET_ERROR(EINVAL));
3546 defer = nvlist_exists(innvl, "defer");
3548 poollen = strlen(poolname);
3549 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3550 pair = nvlist_next_nvpair(snaps, pair)) {
3551 const char *name = nvpair_name(pair);
3554 * The snap must be in the specified pool to prevent the
3555 * invalid removal of zvol minors below.
3557 if (strncmp(name, poolname, poollen) != 0 ||
3558 (name[poollen] != '/' && name[poollen] != '@'))
3559 return (SET_ERROR(EXDEV));
3561 error = zfs_unmount_snap(name);
3564 #if defined(__FreeBSD__)
3565 zvol_remove_minors(name);
3569 return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
3573 * Create bookmarks. Bookmark names are of the form <fs>#<bmark>.
3574 * All bookmarks must be in the same pool.
3577 * bookmark1 -> snapshot1, bookmark2 -> snapshot2
3580 * outnvl: bookmark -> error code (int32)
3585 zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3587 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3588 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3592 * Verify the snapshot argument.
3594 if (nvpair_value_string(pair, &snap_name) != 0)
3595 return (SET_ERROR(EINVAL));
3598 /* Verify that the keys (bookmarks) are unique */
3599 for (nvpair_t *pair2 = nvlist_next_nvpair(innvl, pair);
3600 pair2 != NULL; pair2 = nvlist_next_nvpair(innvl, pair2)) {
3601 if (strcmp(nvpair_name(pair), nvpair_name(pair2)) == 0)
3602 return (SET_ERROR(EINVAL));
3606 return (dsl_bookmark_create(innvl, outnvl));
3611 * property 1, property 2, ...
3615 * bookmark name 1 -> { property 1, property 2, ... },
3616 * bookmark name 2 -> { property 1, property 2, ... }
3621 zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3623 return (dsl_get_bookmarks(fsname, innvl, outnvl));
3628 * bookmark name 1, bookmark name 2
3631 * outnvl: bookmark -> error code (int32)
3635 zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl,
3640 poollen = strlen(poolname);
3641 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3642 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3643 const char *name = nvpair_name(pair);
3644 const char *cp = strchr(name, '#');
3647 * The bookmark name must contain an #, and the part after it
3648 * must contain only valid characters.
3651 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3652 return (SET_ERROR(EINVAL));
3655 * The bookmark must be in the specified pool.
3657 if (strncmp(name, poolname, poollen) != 0 ||
3658 (name[poollen] != '/' && name[poollen] != '#'))
3659 return (SET_ERROR(EXDEV));
3662 error = dsl_bookmark_destroy(innvl, outnvl);
3668 * zc_name name of dataset to destroy
3669 * zc_objset_type type of objset
3670 * zc_defer_destroy mark for deferred destroy
3675 zfs_ioc_destroy(zfs_cmd_t *zc)
3679 if (zc->zc_objset_type == DMU_OST_ZFS) {
3680 err = zfs_unmount_snap(zc->zc_name);
3685 if (strchr(zc->zc_name, '@'))
3686 err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
3688 err = dsl_destroy_head(zc->zc_name);
3689 if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
3691 zvol_remove_minors(zc->zc_name);
3693 (void) zvol_remove_minor(zc->zc_name);
3699 * fsname is name of dataset to rollback (to most recent snapshot)
3701 * innvl is not used.
3703 * outnvl: "target" -> name of most recent snapshot
3708 zfs_ioc_rollback(const char *fsname, nvlist_t *args, nvlist_t *outnvl)
3713 if (getzfsvfs(fsname, &zfsvfs) == 0) {
3714 error = zfs_suspend_fs(zfsvfs);
3718 error = dsl_dataset_rollback(fsname, zfsvfs, outnvl);
3719 resume_err = zfs_resume_fs(zfsvfs, fsname);
3720 error = error ? error : resume_err;
3722 VFS_RELE(zfsvfs->z_vfs);
3724 error = dsl_dataset_rollback(fsname, NULL, outnvl);
3730 recursive_unmount(const char *fsname, void *arg)
3732 const char *snapname = arg;
3733 char fullname[MAXNAMELEN];
3735 (void) snprintf(fullname, sizeof (fullname), "%s@%s", fsname, snapname);
3736 return (zfs_unmount_snap(fullname));
3741 * zc_name old name of dataset
3742 * zc_value new name of dataset
3743 * zc_cookie recursive flag (only valid for snapshots)
3748 zfs_ioc_rename(zfs_cmd_t *zc)
3750 boolean_t recursive = zc->zc_cookie & 1;
3752 boolean_t allow_mounted = B_TRUE;
3755 allow_mounted = (zc->zc_cookie & 2) != 0;
3758 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3759 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3760 strchr(zc->zc_value, '%'))
3761 return (SET_ERROR(EINVAL));
3763 at = strchr(zc->zc_name, '@');
3765 /* snaps must be in same fs */
3768 if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
3769 return (SET_ERROR(EXDEV));
3771 if (zc->zc_objset_type == DMU_OST_ZFS && allow_mounted) {
3772 error = dmu_objset_find(zc->zc_name,
3773 recursive_unmount, at + 1,
3774 recursive ? DS_FIND_CHILDREN : 0);
3780 error = dsl_dataset_rename_snapshot(zc->zc_name,
3781 at + 1, strchr(zc->zc_value, '@') + 1, recursive);
3787 if (zc->zc_objset_type == DMU_OST_ZVOL)
3788 (void) zvol_remove_minor(zc->zc_name);
3790 return (dsl_dir_rename(zc->zc_name, zc->zc_value));
3795 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
3797 const char *propname = nvpair_name(pair);
3798 boolean_t issnap = (strchr(dsname, '@') != NULL);
3799 zfs_prop_t prop = zfs_name_to_prop(propname);
3803 if (prop == ZPROP_INVAL) {
3804 if (zfs_prop_user(propname)) {
3805 if (err = zfs_secpolicy_write_perms(dsname,
3806 ZFS_DELEG_PERM_USERPROP, cr))
3811 if (!issnap && zfs_prop_userquota(propname)) {
3812 const char *perm = NULL;
3813 const char *uq_prefix =
3814 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
3815 const char *gq_prefix =
3816 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
3818 if (strncmp(propname, uq_prefix,
3819 strlen(uq_prefix)) == 0) {
3820 perm = ZFS_DELEG_PERM_USERQUOTA;
3821 } else if (strncmp(propname, gq_prefix,
3822 strlen(gq_prefix)) == 0) {
3823 perm = ZFS_DELEG_PERM_GROUPQUOTA;
3825 /* USERUSED and GROUPUSED are read-only */
3826 return (SET_ERROR(EINVAL));
3829 if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
3834 return (SET_ERROR(EINVAL));
3838 return (SET_ERROR(EINVAL));
3840 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
3842 * dsl_prop_get_all_impl() returns properties in this
3846 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
3847 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3852 * Check that this value is valid for this pool version
3855 case ZFS_PROP_COMPRESSION:
3857 * If the user specified gzip compression, make sure
3858 * the SPA supports it. We ignore any errors here since
3859 * we'll catch them later.
3861 if (nvpair_value_uint64(pair, &intval) == 0) {
3862 if (intval >= ZIO_COMPRESS_GZIP_1 &&
3863 intval <= ZIO_COMPRESS_GZIP_9 &&
3864 zfs_earlier_version(dsname,
3865 SPA_VERSION_GZIP_COMPRESSION)) {
3866 return (SET_ERROR(ENOTSUP));
3869 if (intval == ZIO_COMPRESS_ZLE &&
3870 zfs_earlier_version(dsname,
3871 SPA_VERSION_ZLE_COMPRESSION))
3872 return (SET_ERROR(ENOTSUP));
3874 if (intval == ZIO_COMPRESS_LZ4) {
3877 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3880 if (!spa_feature_is_enabled(spa,
3881 SPA_FEATURE_LZ4_COMPRESS)) {
3882 spa_close(spa, FTAG);
3883 return (SET_ERROR(ENOTSUP));
3885 spa_close(spa, FTAG);
3889 * If this is a bootable dataset then
3890 * verify that the compression algorithm
3891 * is supported for booting. We must return
3892 * something other than ENOTSUP since it
3893 * implies a downrev pool version.
3895 if (zfs_is_bootfs(dsname) &&
3896 !BOOTFS_COMPRESS_VALID(intval)) {
3897 return (SET_ERROR(ERANGE));
3902 case ZFS_PROP_COPIES:
3903 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
3904 return (SET_ERROR(ENOTSUP));
3907 case ZFS_PROP_RECORDSIZE:
3908 /* Record sizes above 128k need the feature to be enabled */
3909 if (nvpair_value_uint64(pair, &intval) == 0 &&
3910 intval > SPA_OLD_MAXBLOCKSIZE) {
3914 * If this is a bootable dataset then
3915 * the we don't allow large (>128K) blocks,
3916 * because GRUB doesn't support them.
3918 if (zfs_is_bootfs(dsname) &&
3919 intval > SPA_OLD_MAXBLOCKSIZE) {
3920 return (SET_ERROR(ERANGE));
3924 * We don't allow setting the property above 1MB,
3925 * unless the tunable has been changed.
3927 if (intval > zfs_max_recordsize ||
3928 intval > SPA_MAXBLOCKSIZE)
3929 return (SET_ERROR(ERANGE));
3931 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3934 if (!spa_feature_is_enabled(spa,
3935 SPA_FEATURE_LARGE_BLOCKS)) {
3936 spa_close(spa, FTAG);
3937 return (SET_ERROR(ENOTSUP));
3939 spa_close(spa, FTAG);
3943 case ZFS_PROP_SHARESMB:
3944 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
3945 return (SET_ERROR(ENOTSUP));
3948 case ZFS_PROP_ACLINHERIT:
3949 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3950 nvpair_value_uint64(pair, &intval) == 0) {
3951 if (intval == ZFS_ACL_PASSTHROUGH_X &&
3952 zfs_earlier_version(dsname,
3953 SPA_VERSION_PASSTHROUGH_X))
3954 return (SET_ERROR(ENOTSUP));
3958 case ZFS_PROP_CHECKSUM:
3959 case ZFS_PROP_DEDUP:
3961 spa_feature_t feature;
3964 /* dedup feature version checks */
3965 if (prop == ZFS_PROP_DEDUP &&
3966 zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
3967 return (SET_ERROR(ENOTSUP));
3969 if (nvpair_value_uint64(pair, &intval) != 0)
3970 return (SET_ERROR(EINVAL));
3972 /* check prop value is enabled in features */
3973 feature = zio_checksum_to_feature(intval);
3974 if (feature == SPA_FEATURE_NONE)
3977 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3980 * Salted checksums are not supported on root pools.
3982 if (spa_bootfs(spa) != 0 &&
3983 intval < ZIO_CHECKSUM_FUNCTIONS &&
3984 (zio_checksum_table[intval].ci_flags &
3985 ZCHECKSUM_FLAG_SALTED)) {
3986 spa_close(spa, FTAG);
3987 return (SET_ERROR(ERANGE));
3989 if (!spa_feature_is_enabled(spa, feature)) {
3990 spa_close(spa, FTAG);
3991 return (SET_ERROR(ENOTSUP));
3993 spa_close(spa, FTAG);
3998 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
4002 * Checks for a race condition to make sure we don't increment a feature flag
4006 zfs_prop_activate_feature_check(void *arg, dmu_tx_t *tx)
4008 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
4009 spa_feature_t *featurep = arg;
4011 if (!spa_feature_is_active(spa, *featurep))
4014 return (SET_ERROR(EBUSY));
4018 * The callback invoked on feature activation in the sync task caused by
4019 * zfs_prop_activate_feature.
4022 zfs_prop_activate_feature_sync(void *arg, dmu_tx_t *tx)
4024 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
4025 spa_feature_t *featurep = arg;
4027 spa_feature_incr(spa, *featurep, tx);
4031 * Activates a feature on a pool in response to a property setting. This
4032 * creates a new sync task which modifies the pool to reflect the feature
4036 zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature)
4040 /* EBUSY here indicates that the feature is already active */
4041 err = dsl_sync_task(spa_name(spa),
4042 zfs_prop_activate_feature_check, zfs_prop_activate_feature_sync,
4043 &feature, 2, ZFS_SPACE_CHECK_RESERVED);
4045 if (err != 0 && err != EBUSY)
4052 * Removes properties from the given props list that fail permission checks
4053 * needed to clear them and to restore them in case of a receive error. For each
4054 * property, make sure we have both set and inherit permissions.
4056 * Returns the first error encountered if any permission checks fail. If the
4057 * caller provides a non-NULL errlist, it also gives the complete list of names
4058 * of all the properties that failed a permission check along with the
4059 * corresponding error numbers. The caller is responsible for freeing the
4062 * If every property checks out successfully, zero is returned and the list
4063 * pointed at by errlist is NULL.
4066 zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
4069 nvpair_t *pair, *next_pair;
4076 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4078 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
4079 (void) strcpy(zc->zc_name, dataset);
4080 pair = nvlist_next_nvpair(props, NULL);
4081 while (pair != NULL) {
4082 next_pair = nvlist_next_nvpair(props, pair);
4084 (void) strcpy(zc->zc_value, nvpair_name(pair));
4085 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
4086 (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
4087 VERIFY(nvlist_remove_nvpair(props, pair) == 0);
4088 VERIFY(nvlist_add_int32(errors,
4089 zc->zc_value, err) == 0);
4093 kmem_free(zc, sizeof (zfs_cmd_t));
4095 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
4096 nvlist_free(errors);
4099 VERIFY(nvpair_value_int32(pair, &rv) == 0);
4102 if (errlist == NULL)
4103 nvlist_free(errors);
4111 propval_equals(nvpair_t *p1, nvpair_t *p2)
4113 if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
4114 /* dsl_prop_get_all_impl() format */
4116 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
4117 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4121 if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
4123 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
4124 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4128 if (nvpair_type(p1) != nvpair_type(p2))
4131 if (nvpair_type(p1) == DATA_TYPE_STRING) {
4132 char *valstr1, *valstr2;
4134 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
4135 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
4136 return (strcmp(valstr1, valstr2) == 0);
4138 uint64_t intval1, intval2;
4140 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
4141 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
4142 return (intval1 == intval2);
4147 * Remove properties from props if they are not going to change (as determined
4148 * by comparison with origprops). Remove them from origprops as well, since we
4149 * do not need to clear or restore properties that won't change.
4152 props_reduce(nvlist_t *props, nvlist_t *origprops)
4154 nvpair_t *pair, *next_pair;
4156 if (origprops == NULL)
4157 return; /* all props need to be received */
4159 pair = nvlist_next_nvpair(props, NULL);
4160 while (pair != NULL) {
4161 const char *propname = nvpair_name(pair);
4164 next_pair = nvlist_next_nvpair(props, pair);
4166 if ((nvlist_lookup_nvpair(origprops, propname,
4167 &match) != 0) || !propval_equals(pair, match))
4168 goto next; /* need to set received value */
4170 /* don't clear the existing received value */
4171 (void) nvlist_remove_nvpair(origprops, match);
4172 /* don't bother receiving the property */
4173 (void) nvlist_remove_nvpair(props, pair);
4180 static boolean_t zfs_ioc_recv_inject_err;
4185 * zc_name name of containing filesystem
4186 * zc_nvlist_src{_size} nvlist of properties to apply
4187 * zc_value name of snapshot to create
4188 * zc_string name of clone origin (if DRR_FLAG_CLONE)
4189 * zc_cookie file descriptor to recv from
4190 * zc_begin_record the BEGIN record of the stream (not byteswapped)
4191 * zc_guid force flag
4192 * zc_cleanup_fd cleanup-on-exit file descriptor
4193 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
4194 * zc_resumable if data is incomplete assume sender will resume
4197 * zc_cookie number of bytes read
4198 * zc_nvlist_dst{_size} error for each unapplied received property
4199 * zc_obj zprop_errflags_t
4200 * zc_action_handle handle for this guid/ds mapping
4203 zfs_ioc_recv(zfs_cmd_t *zc)
4206 dmu_recv_cookie_t drc;
4207 boolean_t force = (boolean_t)zc->zc_guid;
4210 int props_error = 0;
4213 nvlist_t *props = NULL; /* sent properties */
4214 nvlist_t *origprops = NULL; /* existing properties */
4215 char *origin = NULL;
4217 char tofs[ZFS_MAXNAMELEN];
4218 cap_rights_t rights;
4219 boolean_t first_recvd_props = B_FALSE;
4221 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4222 strchr(zc->zc_value, '@') == NULL ||
4223 strchr(zc->zc_value, '%'))
4224 return (SET_ERROR(EINVAL));
4226 (void) strcpy(tofs, zc->zc_value);
4227 tosnap = strchr(tofs, '@');
4230 if (zc->zc_nvlist_src != 0 &&
4231 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
4232 zc->zc_iflags, &props)) != 0)
4239 fget_read(curthread, fd, cap_rights_init(&rights, CAP_PREAD), &fp);
4243 return (SET_ERROR(EBADF));
4246 errors = fnvlist_alloc();
4248 if (zc->zc_string[0])
4249 origin = zc->zc_string;
4251 error = dmu_recv_begin(tofs, tosnap,
4252 &zc->zc_begin_record, force, zc->zc_resumable, origin, &drc);
4257 * Set properties before we receive the stream so that they are applied
4258 * to the new data. Note that we must call dmu_recv_stream() if
4259 * dmu_recv_begin() succeeds.
4261 if (props != NULL && !drc.drc_newfs) {
4262 if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
4263 SPA_VERSION_RECVD_PROPS &&
4264 !dsl_prop_get_hasrecvd(tofs))
4265 first_recvd_props = B_TRUE;
4268 * If new received properties are supplied, they are to
4269 * completely replace the existing received properties, so stash
4270 * away the existing ones.
4272 if (dsl_prop_get_received(tofs, &origprops) == 0) {
4273 nvlist_t *errlist = NULL;
4275 * Don't bother writing a property if its value won't
4276 * change (and avoid the unnecessary security checks).
4278 * The first receive after SPA_VERSION_RECVD_PROPS is a
4279 * special case where we blow away all local properties
4282 if (!first_recvd_props)
4283 props_reduce(props, origprops);
4284 if (zfs_check_clearable(tofs, origprops, &errlist) != 0)
4285 (void) nvlist_merge(errors, errlist, 0);
4286 nvlist_free(errlist);
4288 if (clear_received_props(tofs, origprops,
4289 first_recvd_props ? NULL : props) != 0)
4290 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4292 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4296 if (props != NULL) {
4297 props_error = dsl_prop_set_hasrecvd(tofs);
4299 if (props_error == 0) {
4300 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4305 if (zc->zc_nvlist_dst_size != 0 &&
4306 (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
4307 put_nvlist(zc, errors) != 0)) {
4309 * Caller made zc->zc_nvlist_dst less than the minimum expected
4310 * size or supplied an invalid address.
4312 props_error = SET_ERROR(EINVAL);
4316 error = dmu_recv_stream(&drc, fp, &off, zc->zc_cleanup_fd,
4317 &zc->zc_action_handle);
4320 zfsvfs_t *zfsvfs = NULL;
4322 if (getzfsvfs(tofs, &zfsvfs) == 0) {
4326 error = zfs_suspend_fs(zfsvfs);
4328 * If the suspend fails, then the recv_end will
4329 * likely also fail, and clean up after itself.
4331 end_err = dmu_recv_end(&drc, zfsvfs);
4333 error = zfs_resume_fs(zfsvfs, tofs);
4334 error = error ? error : end_err;
4335 VFS_RELE(zfsvfs->z_vfs);
4337 error = dmu_recv_end(&drc, NULL);
4341 zc->zc_cookie = off - fp->f_offset;
4342 if (off >= 0 && off <= MAXOFFSET_T)
4346 if (zfs_ioc_recv_inject_err) {
4347 zfs_ioc_recv_inject_err = B_FALSE;
4354 zvol_create_minors(tofs);
4358 * On error, restore the original props.
4360 if (error != 0 && props != NULL && !drc.drc_newfs) {
4361 if (clear_received_props(tofs, props, NULL) != 0) {
4363 * We failed to clear the received properties.
4364 * Since we may have left a $recvd value on the
4365 * system, we can't clear the $hasrecvd flag.
4367 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4368 } else if (first_recvd_props) {
4369 dsl_prop_unset_hasrecvd(tofs);
4372 if (origprops == NULL && !drc.drc_newfs) {
4373 /* We failed to stash the original properties. */
4374 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4378 * dsl_props_set() will not convert RECEIVED to LOCAL on or
4379 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
4380 * explictly if we're restoring local properties cleared in the
4381 * first new-style receive.
4383 if (origprops != NULL &&
4384 zfs_set_prop_nvlist(tofs, (first_recvd_props ?
4385 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
4386 origprops, NULL) != 0) {
4388 * We stashed the original properties but failed to
4391 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4396 nvlist_free(origprops);
4397 nvlist_free(errors);
4401 error = props_error;
4408 * zc_name name of snapshot to send
4409 * zc_cookie file descriptor to send stream to
4410 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
4411 * zc_sendobj objsetid of snapshot to send
4412 * zc_fromobj objsetid of incremental fromsnap (may be zero)
4413 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
4414 * output size in zc_objset_type.
4415 * zc_flags lzc_send_flags
4418 * zc_objset_type estimated size, if zc_guid is set
4421 zfs_ioc_send(zfs_cmd_t *zc)
4425 boolean_t estimate = (zc->zc_guid != 0);
4426 boolean_t embedok = (zc->zc_flags & 0x1);
4427 boolean_t large_block_ok = (zc->zc_flags & 0x2);
4429 if (zc->zc_obj != 0) {
4431 dsl_dataset_t *tosnap;
4433 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4437 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
4439 dsl_pool_rele(dp, FTAG);
4443 if (dsl_dir_is_clone(tosnap->ds_dir))
4445 dsl_dir_phys(tosnap->ds_dir)->dd_origin_obj;
4446 dsl_dataset_rele(tosnap, FTAG);
4447 dsl_pool_rele(dp, FTAG);
4452 dsl_dataset_t *tosnap;
4453 dsl_dataset_t *fromsnap = NULL;
4455 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4459 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
4461 dsl_pool_rele(dp, FTAG);
4465 if (zc->zc_fromobj != 0) {
4466 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
4469 dsl_dataset_rele(tosnap, FTAG);
4470 dsl_pool_rele(dp, FTAG);
4475 error = dmu_send_estimate(tosnap, fromsnap,
4476 &zc->zc_objset_type);
4478 if (fromsnap != NULL)
4479 dsl_dataset_rele(fromsnap, FTAG);
4480 dsl_dataset_rele(tosnap, FTAG);
4481 dsl_pool_rele(dp, FTAG);
4484 cap_rights_t rights;
4487 fp = getf(zc->zc_cookie);
4489 fget_write(curthread, zc->zc_cookie,
4490 cap_rights_init(&rights, CAP_WRITE), &fp);
4493 return (SET_ERROR(EBADF));
4496 error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
4497 zc->zc_fromobj, embedok, large_block_ok,
4499 zc->zc_cookie, fp->f_vnode, &off);
4501 zc->zc_cookie, fp, &off);
4504 if (off >= 0 && off <= MAXOFFSET_T)
4506 releasef(zc->zc_cookie);
4513 * zc_name name of snapshot on which to report progress
4514 * zc_cookie file descriptor of send stream
4517 * zc_cookie number of bytes written in send stream thus far
4520 zfs_ioc_send_progress(zfs_cmd_t *zc)
4524 dmu_sendarg_t *dsp = NULL;
4527 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4531 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
4533 dsl_pool_rele(dp, FTAG);
4537 mutex_enter(&ds->ds_sendstream_lock);
4540 * Iterate over all the send streams currently active on this dataset.
4541 * If there's one which matches the specified file descriptor _and_ the
4542 * stream was started by the current process, return the progress of
4545 for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
4546 dsp = list_next(&ds->ds_sendstreams, dsp)) {
4547 if (dsp->dsa_outfd == zc->zc_cookie &&
4548 dsp->dsa_proc == curproc)
4553 zc->zc_cookie = *(dsp->dsa_off);
4555 error = SET_ERROR(ENOENT);
4557 mutex_exit(&ds->ds_sendstream_lock);
4558 dsl_dataset_rele(ds, FTAG);
4559 dsl_pool_rele(dp, FTAG);
4564 zfs_ioc_inject_fault(zfs_cmd_t *zc)
4568 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
4569 &zc->zc_inject_record);
4572 zc->zc_guid = (uint64_t)id;
4578 zfs_ioc_clear_fault(zfs_cmd_t *zc)
4580 return (zio_clear_fault((int)zc->zc_guid));
4584 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
4586 int id = (int)zc->zc_guid;
4589 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
4590 &zc->zc_inject_record);
4598 zfs_ioc_error_log(zfs_cmd_t *zc)
4602 size_t count = (size_t)zc->zc_nvlist_dst_size;
4604 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
4607 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
4610 zc->zc_nvlist_dst_size = count;
4612 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
4614 spa_close(spa, FTAG);
4620 zfs_ioc_clear(zfs_cmd_t *zc)
4627 * On zpool clear we also fix up missing slogs
4629 mutex_enter(&spa_namespace_lock);
4630 spa = spa_lookup(zc->zc_name);
4632 mutex_exit(&spa_namespace_lock);
4633 return (SET_ERROR(EIO));
4635 if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
4636 /* we need to let spa_open/spa_load clear the chains */
4637 spa_set_log_state(spa, SPA_LOG_CLEAR);
4639 spa->spa_last_open_failed = 0;
4640 mutex_exit(&spa_namespace_lock);
4642 if (zc->zc_cookie & ZPOOL_NO_REWIND) {
4643 error = spa_open(zc->zc_name, &spa, FTAG);
4646 nvlist_t *config = NULL;
4648 if (zc->zc_nvlist_src == 0)
4649 return (SET_ERROR(EINVAL));
4651 if ((error = get_nvlist(zc->zc_nvlist_src,
4652 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
4653 error = spa_open_rewind(zc->zc_name, &spa, FTAG,
4655 if (config != NULL) {
4658 if ((err = put_nvlist(zc, config)) != 0)
4660 nvlist_free(config);
4662 nvlist_free(policy);
4669 spa_vdev_state_enter(spa, SCL_NONE);
4671 if (zc->zc_guid == 0) {
4674 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
4676 (void) spa_vdev_state_exit(spa, NULL, ENODEV);
4677 spa_close(spa, FTAG);
4678 return (SET_ERROR(ENODEV));
4682 vdev_clear(spa, vd);
4684 (void) spa_vdev_state_exit(spa, NULL, 0);
4687 * Resume any suspended I/Os.
4689 if (zio_resume(spa) != 0)
4690 error = SET_ERROR(EIO);
4692 spa_close(spa, FTAG);
4698 zfs_ioc_pool_reopen(zfs_cmd_t *zc)
4703 error = spa_open(zc->zc_name, &spa, FTAG);
4707 spa_vdev_state_enter(spa, SCL_NONE);
4710 * If a resilver is already in progress then set the
4711 * spa_scrub_reopen flag to B_TRUE so that we don't restart
4712 * the scan as a side effect of the reopen. Otherwise, let
4713 * vdev_open() decided if a resilver is required.
4715 spa->spa_scrub_reopen = dsl_scan_resilvering(spa->spa_dsl_pool);
4716 vdev_reopen(spa->spa_root_vdev);
4717 spa->spa_scrub_reopen = B_FALSE;
4719 (void) spa_vdev_state_exit(spa, NULL, 0);
4720 spa_close(spa, FTAG);
4725 * zc_name name of filesystem
4726 * zc_value name of origin snapshot
4729 * zc_string name of conflicting snapshot, if there is one
4732 zfs_ioc_promote(zfs_cmd_t *zc)
4737 * We don't need to unmount *all* the origin fs's snapshots, but
4740 cp = strchr(zc->zc_value, '@');
4743 (void) dmu_objset_find(zc->zc_value,
4744 zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
4745 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
4749 * Retrieve a single {user|group}{used|quota}@... property.
4752 * zc_name name of filesystem
4753 * zc_objset_type zfs_userquota_prop_t
4754 * zc_value domain name (eg. "S-1-234-567-89")
4755 * zc_guid RID/UID/GID
4758 * zc_cookie property value
4761 zfs_ioc_userspace_one(zfs_cmd_t *zc)
4766 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
4767 return (SET_ERROR(EINVAL));
4769 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4773 error = zfs_userspace_one(zfsvfs,
4774 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
4775 zfsvfs_rele(zfsvfs, FTAG);
4782 * zc_name name of filesystem
4783 * zc_cookie zap cursor
4784 * zc_objset_type zfs_userquota_prop_t
4785 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
4788 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
4789 * zc_cookie zap cursor
4792 zfs_ioc_userspace_many(zfs_cmd_t *zc)
4795 int bufsize = zc->zc_nvlist_dst_size;
4798 return (SET_ERROR(ENOMEM));
4800 int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4804 void *buf = kmem_alloc(bufsize, KM_SLEEP);
4806 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
4807 buf, &zc->zc_nvlist_dst_size);
4810 error = ddi_copyout(buf,
4811 (void *)(uintptr_t)zc->zc_nvlist_dst,
4812 zc->zc_nvlist_dst_size, zc->zc_iflags);
4814 kmem_free(buf, bufsize);
4815 zfsvfs_rele(zfsvfs, FTAG);
4822 * zc_name name of filesystem
4828 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
4834 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
4835 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
4837 * If userused is not enabled, it may be because the
4838 * objset needs to be closed & reopened (to grow the
4839 * objset_phys_t). Suspend/resume the fs will do that.
4841 error = zfs_suspend_fs(zfsvfs);
4843 dmu_objset_refresh_ownership(zfsvfs->z_os,
4845 error = zfs_resume_fs(zfsvfs, zc->zc_name);
4849 error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
4850 VFS_RELE(zfsvfs->z_vfs);
4852 /* XXX kind of reading contents without owning */
4853 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4857 error = dmu_objset_userspace_upgrade(os);
4858 dmu_objset_rele(os, FTAG);
4866 * We don't want to have a hard dependency
4867 * against some special symbols in sharefs
4868 * nfs, and smbsrv. Determine them if needed when
4869 * the first file system is shared.
4870 * Neither sharefs, nfs or smbsrv are unloadable modules.
4872 int (*znfsexport_fs)(void *arg);
4873 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
4874 int (*zsmbexport_fs)(void *arg, boolean_t add_share);
4876 int zfs_nfsshare_inited;
4877 int zfs_smbshare_inited;
4879 ddi_modhandle_t nfs_mod;
4880 ddi_modhandle_t sharefs_mod;
4881 ddi_modhandle_t smbsrv_mod;
4882 #endif /* illumos */
4883 kmutex_t zfs_share_lock;
4891 ASSERT(MUTEX_HELD(&zfs_share_lock));
4892 /* Both NFS and SMB shares also require sharetab support. */
4893 if (sharefs_mod == NULL && ((sharefs_mod =
4894 ddi_modopen("fs/sharefs",
4895 KRTLD_MODE_FIRST, &error)) == NULL)) {
4896 return (SET_ERROR(ENOSYS));
4898 if (zshare_fs == NULL && ((zshare_fs =
4899 (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
4900 ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
4901 return (SET_ERROR(ENOSYS));
4905 #endif /* illumos */
4908 zfs_ioc_share(zfs_cmd_t *zc)
4914 switch (zc->zc_share.z_sharetype) {
4916 case ZFS_UNSHARE_NFS:
4917 if (zfs_nfsshare_inited == 0) {
4918 mutex_enter(&zfs_share_lock);
4919 if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
4920 KRTLD_MODE_FIRST, &error)) == NULL)) {
4921 mutex_exit(&zfs_share_lock);
4922 return (SET_ERROR(ENOSYS));
4924 if (znfsexport_fs == NULL &&
4925 ((znfsexport_fs = (int (*)(void *))
4927 "nfs_export", &error)) == NULL)) {
4928 mutex_exit(&zfs_share_lock);
4929 return (SET_ERROR(ENOSYS));
4931 error = zfs_init_sharefs();
4933 mutex_exit(&zfs_share_lock);
4934 return (SET_ERROR(ENOSYS));
4936 zfs_nfsshare_inited = 1;
4937 mutex_exit(&zfs_share_lock);
4941 case ZFS_UNSHARE_SMB:
4942 if (zfs_smbshare_inited == 0) {
4943 mutex_enter(&zfs_share_lock);
4944 if (smbsrv_mod == NULL && ((smbsrv_mod =
4945 ddi_modopen("drv/smbsrv",
4946 KRTLD_MODE_FIRST, &error)) == NULL)) {
4947 mutex_exit(&zfs_share_lock);
4948 return (SET_ERROR(ENOSYS));
4950 if (zsmbexport_fs == NULL && ((zsmbexport_fs =
4951 (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
4952 "smb_server_share", &error)) == NULL)) {
4953 mutex_exit(&zfs_share_lock);
4954 return (SET_ERROR(ENOSYS));
4956 error = zfs_init_sharefs();
4958 mutex_exit(&zfs_share_lock);
4959 return (SET_ERROR(ENOSYS));
4961 zfs_smbshare_inited = 1;
4962 mutex_exit(&zfs_share_lock);
4966 return (SET_ERROR(EINVAL));
4969 switch (zc->zc_share.z_sharetype) {
4971 case ZFS_UNSHARE_NFS:
4973 znfsexport_fs((void *)
4974 (uintptr_t)zc->zc_share.z_exportdata))
4978 case ZFS_UNSHARE_SMB:
4979 if (error = zsmbexport_fs((void *)
4980 (uintptr_t)zc->zc_share.z_exportdata,
4981 zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
4988 opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
4989 zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
4990 SHAREFS_ADD : SHAREFS_REMOVE;
4993 * Add or remove share from sharetab
4995 error = zshare_fs(opcode,
4996 (void *)(uintptr_t)zc->zc_share.z_sharedata,
4997 zc->zc_share.z_sharemax);
5001 #else /* !illumos */
5003 #endif /* illumos */
5006 ace_t full_access[] = {
5007 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
5012 * zc_name name of containing filesystem
5013 * zc_obj object # beyond which we want next in-use object #
5016 * zc_obj next in-use object #
5019 zfs_ioc_next_obj(zfs_cmd_t *zc)
5021 objset_t *os = NULL;
5024 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
5028 error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
5029 dsl_dataset_phys(os->os_dsl_dataset)->ds_prev_snap_txg);
5031 dmu_objset_rele(os, FTAG);
5037 * zc_name name of filesystem
5038 * zc_value prefix name for snapshot
5039 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
5042 * zc_value short name of new snapshot
5045 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
5052 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
5056 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
5057 (u_longlong_t)ddi_get_lbolt64());
5058 hold_name = kmem_asprintf("%%%s", zc->zc_value);
5060 error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
5063 (void) strcpy(zc->zc_value, snap_name);
5066 zfs_onexit_fd_rele(zc->zc_cleanup_fd);
5072 * zc_name name of "to" snapshot
5073 * zc_value name of "from" snapshot
5074 * zc_cookie file descriptor to write diff data on
5077 * dmu_diff_record_t's to the file descriptor
5080 zfs_ioc_diff(zfs_cmd_t *zc)
5083 cap_rights_t rights;
5088 fp = getf(zc->zc_cookie);
5090 fget_write(curthread, zc->zc_cookie,
5091 cap_rights_init(&rights, CAP_WRITE), &fp);
5094 return (SET_ERROR(EBADF));
5099 error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off);
5101 error = dmu_diff(zc->zc_name, zc->zc_value, fp, &off);
5104 if (off >= 0 && off <= MAXOFFSET_T)
5106 releasef(zc->zc_cookie);
5113 * Remove all ACL files in shares dir
5116 zfs_smb_acl_purge(znode_t *dzp)
5119 zap_attribute_t zap;
5120 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
5123 for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
5124 (error = zap_cursor_retrieve(&zc, &zap)) == 0;
5125 zap_cursor_advance(&zc)) {
5126 if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
5130 zap_cursor_fini(&zc);
5133 #endif /* illumos */
5136 zfs_ioc_smb_acl(zfs_cmd_t *zc)
5141 vnode_t *resourcevp = NULL;
5150 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
5151 NO_FOLLOW, NULL, &vp)) != 0)
5154 /* Now make sure mntpnt and dataset are ZFS */
5156 if (strcmp(vp->v_vfsp->mnt_stat.f_fstypename, "zfs") != 0 ||
5157 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
5158 zc->zc_name) != 0)) {
5160 return (SET_ERROR(EINVAL));
5164 zfsvfs = dzp->z_zfsvfs;
5168 * Create share dir if its missing.
5170 mutex_enter(&zfsvfs->z_lock);
5171 if (zfsvfs->z_shares_dir == 0) {
5174 tx = dmu_tx_create(zfsvfs->z_os);
5175 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
5177 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
5178 error = dmu_tx_assign(tx, TXG_WAIT);
5182 error = zfs_create_share_dir(zfsvfs, tx);
5186 mutex_exit(&zfsvfs->z_lock);
5192 mutex_exit(&zfsvfs->z_lock);
5194 ASSERT(zfsvfs->z_shares_dir);
5195 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
5201 switch (zc->zc_cookie) {
5202 case ZFS_SMB_ACL_ADD:
5203 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
5204 vattr.va_type = VREG;
5205 vattr.va_mode = S_IFREG|0777;
5209 vsec.vsa_mask = VSA_ACE;
5210 vsec.vsa_aclentp = &full_access;
5211 vsec.vsa_aclentsz = sizeof (full_access);
5212 vsec.vsa_aclcnt = 1;
5214 error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
5215 &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
5217 VN_RELE(resourcevp);
5220 case ZFS_SMB_ACL_REMOVE:
5221 error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
5225 case ZFS_SMB_ACL_RENAME:
5226 if ((error = get_nvlist(zc->zc_nvlist_src,
5227 zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
5229 VN_RELE(ZTOV(sharedir));
5233 if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
5234 nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
5237 VN_RELE(ZTOV(sharedir));
5239 nvlist_free(nvlist);
5242 error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
5244 nvlist_free(nvlist);
5247 case ZFS_SMB_ACL_PURGE:
5248 error = zfs_smb_acl_purge(sharedir);
5252 error = SET_ERROR(EINVAL);
5257 VN_RELE(ZTOV(sharedir));
5262 #else /* !illumos */
5263 return (EOPNOTSUPP);
5264 #endif /* illumos */
5269 * "holds" -> { snapname -> holdname (string), ... }
5270 * (optional) "cleanup_fd" -> fd (int32)
5274 * snapname -> error value (int32)
5280 zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
5284 int cleanup_fd = -1;
5288 error = nvlist_lookup_nvlist(args, "holds", &holds);
5290 return (SET_ERROR(EINVAL));
5292 /* make sure the user didn't pass us any invalid (empty) tags */
5293 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
5294 pair = nvlist_next_nvpair(holds, pair)) {
5297 error = nvpair_value_string(pair, &htag);
5299 return (SET_ERROR(error));
5301 if (strlen(htag) == 0)
5302 return (SET_ERROR(EINVAL));
5305 if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
5306 error = zfs_onexit_fd_hold(cleanup_fd, &minor);
5311 error = dsl_dataset_user_hold(holds, minor, errlist);
5313 zfs_onexit_fd_rele(cleanup_fd);
5318 * innvl is not used.
5321 * holdname -> time added (uint64 seconds since epoch)
5327 zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
5329 return (dsl_dataset_get_holds(snapname, outnvl));
5334 * snapname -> { holdname, ... }
5339 * snapname -> error value (int32)
5345 zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
5347 return (dsl_dataset_user_release(holds, errlist));
5352 * zc_name name of new filesystem or snapshot
5353 * zc_value full name of old snapshot
5356 * zc_cookie space in bytes
5357 * zc_objset_type compressed space in bytes
5358 * zc_perm_action uncompressed space in bytes
5361 zfs_ioc_space_written(zfs_cmd_t *zc)
5365 dsl_dataset_t *new, *old;
5367 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5370 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
5372 dsl_pool_rele(dp, FTAG);
5375 error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
5377 dsl_dataset_rele(new, FTAG);
5378 dsl_pool_rele(dp, FTAG);
5382 error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
5383 &zc->zc_objset_type, &zc->zc_perm_action);
5384 dsl_dataset_rele(old, FTAG);
5385 dsl_dataset_rele(new, FTAG);
5386 dsl_pool_rele(dp, FTAG);
5392 * "firstsnap" -> snapshot name
5396 * "used" -> space in bytes
5397 * "compressed" -> compressed space in bytes
5398 * "uncompressed" -> uncompressed space in bytes
5402 zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
5406 dsl_dataset_t *new, *old;
5408 uint64_t used, comp, uncomp;
5410 if (nvlist_lookup_string(innvl, "firstsnap", &firstsnap) != 0)
5411 return (SET_ERROR(EINVAL));
5413 error = dsl_pool_hold(lastsnap, FTAG, &dp);
5417 error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
5418 if (error == 0 && !new->ds_is_snapshot) {
5419 dsl_dataset_rele(new, FTAG);
5420 error = SET_ERROR(EINVAL);
5423 dsl_pool_rele(dp, FTAG);
5426 error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
5427 if (error == 0 && !old->ds_is_snapshot) {
5428 dsl_dataset_rele(old, FTAG);
5429 error = SET_ERROR(EINVAL);
5432 dsl_dataset_rele(new, FTAG);
5433 dsl_pool_rele(dp, FTAG);
5437 error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
5438 dsl_dataset_rele(old, FTAG);
5439 dsl_dataset_rele(new, FTAG);
5440 dsl_pool_rele(dp, FTAG);
5441 fnvlist_add_uint64(outnvl, "used", used);
5442 fnvlist_add_uint64(outnvl, "compressed", comp);
5443 fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
5448 zfs_ioc_jail(zfs_cmd_t *zc)
5451 return (zone_dataset_attach(curthread->td_ucred, zc->zc_name,
5452 (int)zc->zc_jailid));
5456 zfs_ioc_unjail(zfs_cmd_t *zc)
5459 return (zone_dataset_detach(curthread->td_ucred, zc->zc_name,
5460 (int)zc->zc_jailid));
5465 * "fd" -> file descriptor to write stream to (int32)
5466 * (optional) "fromsnap" -> full snap name to send an incremental from
5467 * (optional) "largeblockok" -> (value ignored)
5468 * indicates that blocks > 128KB are permitted
5469 * (optional) "embedok" -> (value ignored)
5470 * presence indicates DRR_WRITE_EMBEDDED records are permitted
5471 * (optional) "resume_object" and "resume_offset" -> (uint64)
5472 * if present, resume send stream from specified object and offset.
5479 zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5481 cap_rights_t rights;
5485 char *fromname = NULL;
5487 boolean_t largeblockok;
5489 uint64_t resumeobj = 0;
5490 uint64_t resumeoff = 0;
5492 error = nvlist_lookup_int32(innvl, "fd", &fd);
5494 return (SET_ERROR(EINVAL));
5496 (void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
5498 largeblockok = nvlist_exists(innvl, "largeblockok");
5499 embedok = nvlist_exists(innvl, "embedok");
5501 (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
5502 (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
5505 file_t *fp = getf(fd);
5507 fget_write(curthread, fd, cap_rights_init(&rights, CAP_WRITE), &fp);
5510 return (SET_ERROR(EBADF));
5513 error = dmu_send(snapname, fromname, embedok, largeblockok, fd,
5515 resumeobj, resumeoff, fp->f_vnode, &off);
5517 resumeobj, resumeoff, fp, &off);
5521 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5532 * Determine approximately how large a zfs send stream will be -- the number
5533 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
5536 * (optional) "from" -> full snap or bookmark name to send an incremental
5541 * "space" -> bytes of space (uint64)
5545 zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5548 dsl_dataset_t *tosnap;
5553 error = dsl_pool_hold(snapname, FTAG, &dp);
5557 error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
5559 dsl_pool_rele(dp, FTAG);
5563 error = nvlist_lookup_string(innvl, "from", &fromname);
5565 if (strchr(fromname, '@') != NULL) {
5567 * If from is a snapshot, hold it and use the more
5568 * efficient dmu_send_estimate to estimate send space
5569 * size using deadlists.
5571 dsl_dataset_t *fromsnap;
5572 error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
5575 error = dmu_send_estimate(tosnap, fromsnap, &space);
5576 dsl_dataset_rele(fromsnap, FTAG);
5577 } else if (strchr(fromname, '#') != NULL) {
5579 * If from is a bookmark, fetch the creation TXG of the
5580 * snapshot it was created from and use that to find
5581 * blocks that were born after it.
5583 zfs_bookmark_phys_t frombm;
5585 error = dsl_bookmark_lookup(dp, fromname, tosnap,
5589 error = dmu_send_estimate_from_txg(tosnap,
5590 frombm.zbm_creation_txg, &space);
5593 * from is not properly formatted as a snapshot or
5596 error = SET_ERROR(EINVAL);
5600 // If estimating the size of a full send, use dmu_send_estimate
5601 error = dmu_send_estimate(tosnap, NULL, &space);
5604 fnvlist_add_uint64(outnvl, "space", space);
5607 dsl_dataset_rele(tosnap, FTAG);
5608 dsl_pool_rele(dp, FTAG);
5612 static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
5615 zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5616 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
5617 boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
5619 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
5621 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
5622 ASSERT3U(ioc, <, ZFS_IOC_LAST);
5623 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
5624 ASSERT3P(vec->zvec_func, ==, NULL);
5626 vec->zvec_legacy_func = func;
5627 vec->zvec_secpolicy = secpolicy;
5628 vec->zvec_namecheck = namecheck;
5629 vec->zvec_allow_log = log_history;
5630 vec->zvec_pool_check = pool_check;
5634 * See the block comment at the beginning of this file for details on
5635 * each argument to this function.
5638 zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
5639 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
5640 zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
5641 boolean_t allow_log)
5643 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
5645 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
5646 ASSERT3U(ioc, <, ZFS_IOC_LAST);
5647 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
5648 ASSERT3P(vec->zvec_func, ==, NULL);
5650 /* if we are logging, the name must be valid */
5651 ASSERT(!allow_log || namecheck != NO_NAME);
5653 vec->zvec_name = name;
5654 vec->zvec_func = func;
5655 vec->zvec_secpolicy = secpolicy;
5656 vec->zvec_namecheck = namecheck;
5657 vec->zvec_pool_check = pool_check;
5658 vec->zvec_smush_outnvlist = smush_outnvlist;
5659 vec->zvec_allow_log = allow_log;
5663 zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5664 zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
5665 zfs_ioc_poolcheck_t pool_check)
5667 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5668 POOL_NAME, log_history, pool_check);
5672 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5673 zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
5675 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5676 DATASET_NAME, B_FALSE, pool_check);
5680 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
5682 zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
5683 POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5687 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5688 zfs_secpolicy_func_t *secpolicy)
5690 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5691 NO_NAME, B_FALSE, POOL_CHECK_NONE);
5695 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
5696 zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
5698 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5699 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
5703 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
5705 zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
5706 zfs_secpolicy_read);
5710 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5711 zfs_secpolicy_func_t *secpolicy)
5713 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5714 DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5718 zfs_ioctl_init(void)
5720 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
5721 zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
5722 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5724 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
5725 zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
5726 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE);
5728 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
5729 zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
5730 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5732 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
5733 zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
5734 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5736 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
5737 zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
5738 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5740 zfs_ioctl_register("create", ZFS_IOC_CREATE,
5741 zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
5742 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5744 zfs_ioctl_register("clone", ZFS_IOC_CLONE,
5745 zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
5746 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5748 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
5749 zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
5750 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5752 zfs_ioctl_register("hold", ZFS_IOC_HOLD,
5753 zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
5754 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5755 zfs_ioctl_register("release", ZFS_IOC_RELEASE,
5756 zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
5757 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5759 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
5760 zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
5761 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5763 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
5764 zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
5765 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
5767 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK,
5768 zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME,
5769 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5771 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS,
5772 zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME,
5773 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5775 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS,
5776 zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks,
5778 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5780 /* IOCTLS that use the legacy function signature */
5782 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
5783 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
5785 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
5786 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5787 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
5789 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
5790 zfs_ioc_pool_upgrade);
5791 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
5793 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
5794 zfs_ioc_vdev_remove);
5795 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
5796 zfs_ioc_vdev_set_state);
5797 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
5798 zfs_ioc_vdev_attach);
5799 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
5800 zfs_ioc_vdev_detach);
5801 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
5802 zfs_ioc_vdev_setpath);
5803 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
5804 zfs_ioc_vdev_setfru);
5805 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
5806 zfs_ioc_pool_set_props);
5807 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
5808 zfs_ioc_vdev_split);
5809 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
5810 zfs_ioc_pool_reguid);
5812 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
5813 zfs_ioc_pool_configs, zfs_secpolicy_none);
5814 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
5815 zfs_ioc_pool_tryimport, zfs_secpolicy_config);
5816 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
5817 zfs_ioc_inject_fault, zfs_secpolicy_inject);
5818 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
5819 zfs_ioc_clear_fault, zfs_secpolicy_inject);
5820 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
5821 zfs_ioc_inject_list_next, zfs_secpolicy_inject);
5824 * pool destroy, and export don't log the history as part of
5825 * zfsdev_ioctl, but rather zfs_ioc_pool_export
5826 * does the logging of those commands.
5828 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
5829 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
5830 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
5831 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
5833 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
5834 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
5835 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
5836 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
5838 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
5839 zfs_secpolicy_inject, B_FALSE, POOL_CHECK_NONE);
5840 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
5841 zfs_ioc_dsobj_to_dsname,
5842 zfs_secpolicy_diff, B_FALSE, POOL_CHECK_NONE);
5843 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
5844 zfs_ioc_pool_get_history,
5845 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
5847 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
5848 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5850 zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
5851 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5852 zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
5853 zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
5855 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
5856 zfs_ioc_space_written);
5857 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
5858 zfs_ioc_objset_recvd_props);
5859 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
5861 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
5863 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
5864 zfs_ioc_objset_stats);
5865 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
5866 zfs_ioc_objset_zplprops);
5867 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
5868 zfs_ioc_dataset_list_next);
5869 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
5870 zfs_ioc_snapshot_list_next);
5871 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
5872 zfs_ioc_send_progress);
5874 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
5875 zfs_ioc_diff, zfs_secpolicy_diff);
5876 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
5877 zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
5878 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
5879 zfs_ioc_obj_to_path, zfs_secpolicy_diff);
5880 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
5881 zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
5882 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
5883 zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
5884 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
5885 zfs_ioc_send, zfs_secpolicy_send);
5887 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
5888 zfs_secpolicy_none);
5889 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
5890 zfs_secpolicy_destroy);
5891 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
5892 zfs_secpolicy_rename);
5893 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
5894 zfs_secpolicy_recv);
5895 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
5896 zfs_secpolicy_promote);
5897 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
5898 zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
5899 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
5900 zfs_secpolicy_set_fsacl);
5902 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
5903 zfs_secpolicy_share, POOL_CHECK_NONE);
5904 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
5905 zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
5906 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
5907 zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
5908 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5909 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
5910 zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
5911 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5914 zfs_ioctl_register_dataset_nolog(ZFS_IOC_JAIL, zfs_ioc_jail,
5915 zfs_secpolicy_config, POOL_CHECK_NONE);
5916 zfs_ioctl_register_dataset_nolog(ZFS_IOC_UNJAIL, zfs_ioc_unjail,
5917 zfs_secpolicy_config, POOL_CHECK_NONE);
5922 pool_status_check(const char *name, zfs_ioc_namecheck_t type,
5923 zfs_ioc_poolcheck_t check)
5928 ASSERT(type == POOL_NAME || type == DATASET_NAME);
5930 if (check & POOL_CHECK_NONE)
5933 error = spa_open(name, &spa, FTAG);
5935 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
5936 error = SET_ERROR(EAGAIN);
5937 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
5938 error = SET_ERROR(EROFS);
5939 spa_close(spa, FTAG);
5945 * Find a free minor number.
5948 zfsdev_minor_alloc(void)
5950 static minor_t last_minor;
5953 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5955 for (m = last_minor + 1; m != last_minor; m++) {
5956 if (m > ZFSDEV_MAX_MINOR)
5958 if (ddi_get_soft_state(zfsdev_state, m) == NULL) {
5968 zfs_ctldev_init(struct cdev *devp)
5971 zfs_soft_state_t *zs;
5973 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5975 minor = zfsdev_minor_alloc();
5977 return (SET_ERROR(ENXIO));
5979 if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS)
5980 return (SET_ERROR(EAGAIN));
5982 devfs_set_cdevpriv((void *)(uintptr_t)minor, zfsdev_close);
5984 zs = ddi_get_soft_state(zfsdev_state, minor);
5985 zs->zss_type = ZSST_CTLDEV;
5986 zfs_onexit_init((zfs_onexit_t **)&zs->zss_data);
5992 zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor)
5994 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5996 zfs_onexit_destroy(zo);
5997 ddi_soft_state_free(zfsdev_state, minor);
6001 zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which)
6003 zfs_soft_state_t *zp;
6005 zp = ddi_get_soft_state(zfsdev_state, minor);
6006 if (zp == NULL || zp->zss_type != which)
6009 return (zp->zss_data);
6013 zfsdev_open(struct cdev *devp, int flag, int mode, struct thread *td)
6018 if (getminor(*devp) != 0)
6019 return (zvol_open(devp, flag, otyp, cr));
6022 /* This is the control device. Allocate a new minor if requested. */
6024 mutex_enter(&spa_namespace_lock);
6025 error = zfs_ctldev_init(devp);
6026 mutex_exit(&spa_namespace_lock);
6033 zfsdev_close(void *data)
6036 minor_t minor = (minor_t)(uintptr_t)data;
6041 mutex_enter(&spa_namespace_lock);
6042 zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV);
6044 mutex_exit(&spa_namespace_lock);
6047 zfs_ctldev_destroy(zo, minor);
6048 mutex_exit(&spa_namespace_lock);
6052 zfsdev_ioctl(struct cdev *dev, u_long zcmd, caddr_t arg, int flag,
6059 minor_t minor = getminor(dev);
6061 zfs_iocparm_t *zc_iocparm;
6062 int cflag, cmd, oldvecnum;
6063 boolean_t newioc, compat;
6064 void *compat_zc = NULL;
6065 cred_t *cr = td->td_ucred;
6067 const zfs_ioc_vec_t *vec;
6068 char *saved_poolname = NULL;
6069 nvlist_t *innvl = NULL;
6071 cflag = ZFS_CMD_COMPAT_NONE;
6073 newioc = B_TRUE; /* "new" style (zfs_iocparm_t) ioctl */
6075 len = IOCPARM_LEN(zcmd);
6076 vecnum = cmd = zcmd & 0xff;
6079 * Check if we are talking to supported older binaries
6080 * and translate zfs_cmd if necessary
6082 if (len != sizeof(zfs_iocparm_t)) {
6089 case sizeof(zfs_cmd_zcmd_t):
6090 cflag = ZFS_CMD_COMPAT_LZC;
6092 case sizeof(zfs_cmd_deadman_t):
6093 cflag = ZFS_CMD_COMPAT_DEADMAN;
6095 case sizeof(zfs_cmd_v28_t):
6096 cflag = ZFS_CMD_COMPAT_V28;
6098 case sizeof(zfs_cmd_v15_t):
6099 cflag = ZFS_CMD_COMPAT_V15;
6100 vecnum = zfs_ioctl_v15_to_v28[cmd];
6103 * Return without further handling
6104 * if the command is blacklisted.
6106 if (vecnum == ZFS_IOC_COMPAT_PASS)
6108 else if (vecnum == ZFS_IOC_COMPAT_FAIL)
6117 vecnum = cmd - ZFS_IOC_FIRST;
6118 ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
6121 if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
6122 return (SET_ERROR(EINVAL));
6123 vec = &zfs_ioc_vec[vecnum];
6125 zc = kmem_zalloc(sizeof(zfs_cmd_t), KM_SLEEP);
6128 error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
6130 error = SET_ERROR(EFAULT);
6133 #else /* !illumos */
6134 bzero(zc, sizeof(zfs_cmd_t));
6137 zc_iocparm = (void *)arg;
6139 switch (zc_iocparm->zfs_ioctl_version) {
6140 case ZFS_IOCVER_CURRENT:
6141 if (zc_iocparm->zfs_cmd_size != sizeof(zfs_cmd_t)) {
6142 error = SET_ERROR(EINVAL);
6146 case ZFS_IOCVER_EDBP:
6147 if (zc_iocparm->zfs_cmd_size != sizeof(zfs_cmd_edbp_t)) {
6148 error = SET_ERROR(EFAULT);
6152 cflag = ZFS_CMD_COMPAT_EDBP;
6154 case ZFS_IOCVER_ZCMD:
6155 if (zc_iocparm->zfs_cmd_size > sizeof(zfs_cmd_t) ||
6156 zc_iocparm->zfs_cmd_size < sizeof(zfs_cmd_zcmd_t)) {
6157 error = SET_ERROR(EFAULT);
6161 cflag = ZFS_CMD_COMPAT_ZCMD;
6164 error = SET_ERROR(EINVAL);
6170 ASSERT(sizeof(zfs_cmd_t) >= zc_iocparm->zfs_cmd_size);
6171 compat_zc = kmem_zalloc(sizeof(zfs_cmd_t), KM_SLEEP);
6172 bzero(compat_zc, sizeof(zfs_cmd_t));
6174 error = ddi_copyin((void *)(uintptr_t)zc_iocparm->zfs_cmd,
6175 compat_zc, zc_iocparm->zfs_cmd_size, flag);
6177 error = SET_ERROR(EFAULT);
6181 error = ddi_copyin((void *)(uintptr_t)zc_iocparm->zfs_cmd,
6182 zc, zc_iocparm->zfs_cmd_size, flag);
6184 error = SET_ERROR(EFAULT);
6192 ASSERT(compat_zc != NULL);
6193 zfs_cmd_compat_get(zc, compat_zc, cflag);
6195 ASSERT(compat_zc == NULL);
6196 zfs_cmd_compat_get(zc, arg, cflag);
6199 error = zfs_ioctl_compat_pre(zc, &vecnum, cflag);
6202 if (oldvecnum != vecnum)
6203 vec = &zfs_ioc_vec[vecnum];
6205 #endif /* !illumos */
6207 zc->zc_iflags = flag & FKIOCTL;
6208 if (zc->zc_nvlist_src_size != 0) {
6209 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
6210 zc->zc_iflags, &innvl);
6215 /* rewrite innvl for backwards compatibility */
6217 innvl = zfs_ioctl_compat_innvl(zc, innvl, vecnum, cflag);
6220 * Ensure that all pool/dataset names are valid before we pass down to
6223 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
6224 switch (vec->zvec_namecheck) {
6226 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
6227 error = SET_ERROR(EINVAL);
6229 error = pool_status_check(zc->zc_name,
6230 vec->zvec_namecheck, vec->zvec_pool_check);
6234 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
6235 error = SET_ERROR(EINVAL);
6237 error = pool_status_check(zc->zc_name,
6238 vec->zvec_namecheck, vec->zvec_pool_check);
6245 if (error == 0 && !(flag & FKIOCTL))
6246 error = vec->zvec_secpolicy(zc, innvl, cr);
6251 /* legacy ioctls can modify zc_name */
6252 len = strcspn(zc->zc_name, "/@#") + 1;
6253 saved_poolname = kmem_alloc(len, KM_SLEEP);
6254 (void) strlcpy(saved_poolname, zc->zc_name, len);
6256 if (vec->zvec_func != NULL) {
6260 nvlist_t *lognv = NULL;
6262 ASSERT(vec->zvec_legacy_func == NULL);
6265 * Add the innvl to the lognv before calling the func,
6266 * in case the func changes the innvl.
6268 if (vec->zvec_allow_log) {
6269 lognv = fnvlist_alloc();
6270 fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
6272 if (!nvlist_empty(innvl)) {
6273 fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
6278 outnvl = fnvlist_alloc();
6279 error = vec->zvec_func(zc->zc_name, innvl, outnvl);
6281 if (error == 0 && vec->zvec_allow_log &&
6282 spa_open(zc->zc_name, &spa, FTAG) == 0) {
6283 if (!nvlist_empty(outnvl)) {
6284 fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL,
6287 (void) spa_history_log_nvl(spa, lognv);
6288 spa_close(spa, FTAG);
6290 fnvlist_free(lognv);
6292 /* rewrite outnvl for backwards compatibility */
6294 outnvl = zfs_ioctl_compat_outnvl(zc, outnvl, vecnum,
6297 if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
6299 if (vec->zvec_smush_outnvlist) {
6300 smusherror = nvlist_smush(outnvl,
6301 zc->zc_nvlist_dst_size);
6303 if (smusherror == 0)
6304 puterror = put_nvlist(zc, outnvl);
6310 nvlist_free(outnvl);
6312 error = vec->zvec_legacy_func(zc);
6319 rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
6320 if (error == 0 && rc != 0)
6321 error = SET_ERROR(EFAULT);
6324 zfs_ioctl_compat_post(zc, cmd, cflag);
6326 ASSERT(compat_zc != NULL);
6327 ASSERT(sizeof(zfs_cmd_t) >= zc_iocparm->zfs_cmd_size);
6329 zfs_cmd_compat_put(zc, compat_zc, vecnum, cflag);
6330 rc = ddi_copyout(compat_zc,
6331 (void *)(uintptr_t)zc_iocparm->zfs_cmd,
6332 zc_iocparm->zfs_cmd_size, flag);
6333 if (error == 0 && rc != 0)
6334 error = SET_ERROR(EFAULT);
6335 kmem_free(compat_zc, sizeof (zfs_cmd_t));
6337 zfs_cmd_compat_put(zc, arg, vecnum, cflag);
6342 rc = ddi_copyout(zc, (void *)(uintptr_t)zc_iocparm->zfs_cmd,
6343 sizeof (zfs_cmd_t), flag);
6344 if (error == 0 && rc != 0)
6345 error = SET_ERROR(EFAULT);
6348 if (error == 0 && vec->zvec_allow_log) {
6349 char *s = tsd_get(zfs_allow_log_key);
6352 (void) tsd_set(zfs_allow_log_key, saved_poolname);
6354 if (saved_poolname != NULL)
6355 strfree(saved_poolname);
6358 kmem_free(zc, sizeof (zfs_cmd_t));
6364 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
6366 if (cmd != DDI_ATTACH)
6367 return (DDI_FAILURE);
6369 if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
6370 DDI_PSEUDO, 0) == DDI_FAILURE)
6371 return (DDI_FAILURE);
6375 ddi_report_dev(dip);
6377 return (DDI_SUCCESS);
6381 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
6383 if (spa_busy() || zfs_busy() || zvol_busy())
6384 return (DDI_FAILURE);
6386 if (cmd != DDI_DETACH)
6387 return (DDI_FAILURE);
6391 ddi_prop_remove_all(dip);
6392 ddi_remove_minor_node(dip, NULL);
6394 return (DDI_SUCCESS);
6399 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
6402 case DDI_INFO_DEVT2DEVINFO:
6404 return (DDI_SUCCESS);
6406 case DDI_INFO_DEVT2INSTANCE:
6407 *result = (void *)0;
6408 return (DDI_SUCCESS);
6411 return (DDI_FAILURE);
6413 #endif /* illumos */
6416 * OK, so this is a little weird.
6418 * /dev/zfs is the control node, i.e. minor 0.
6419 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
6421 * /dev/zfs has basically nothing to do except serve up ioctls,
6422 * so most of the standard driver entry points are in zvol.c.
6425 static struct cb_ops zfs_cb_ops = {
6426 zfsdev_open, /* open */
6427 zfsdev_close, /* close */
6428 zvol_strategy, /* strategy */
6430 zvol_dump, /* dump */
6431 zvol_read, /* read */
6432 zvol_write, /* write */
6433 zfsdev_ioctl, /* ioctl */
6437 nochpoll, /* poll */
6438 ddi_prop_op, /* prop_op */
6439 NULL, /* streamtab */
6440 D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */
6441 CB_REV, /* version */
6442 nodev, /* async read */
6443 nodev, /* async write */
6446 static struct dev_ops zfs_dev_ops = {
6447 DEVO_REV, /* version */
6449 zfs_info, /* info */
6450 nulldev, /* identify */
6451 nulldev, /* probe */
6452 zfs_attach, /* attach */
6453 zfs_detach, /* detach */
6455 &zfs_cb_ops, /* driver operations */
6456 NULL, /* no bus operations */
6458 ddi_quiesce_not_needed, /* quiesce */
6461 static struct modldrv zfs_modldrv = {
6467 static struct modlinkage modlinkage = {
6469 (void *)&zfs_modlfs,
6470 (void *)&zfs_modldrv,
6473 #endif /* illumos */
6475 static struct cdevsw zfs_cdevsw = {
6476 .d_version = D_VERSION,
6477 .d_open = zfsdev_open,
6478 .d_ioctl = zfsdev_ioctl,
6479 .d_name = ZFS_DEV_NAME
6483 zfs_allow_log_destroy(void *arg)
6485 char *poolname = arg;
6492 zfsdev = make_dev(&zfs_cdevsw, 0x0, UID_ROOT, GID_OPERATOR, 0666,
6500 destroy_dev(zfsdev);
6503 static struct root_hold_token *zfs_root_token;
6504 struct proc *zfsproc;
6512 spa_init(FREAD | FWRITE);
6517 if ((error = mod_install(&modlinkage)) != 0) {
6524 tsd_create(&zfs_fsyncer_key, NULL);
6525 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
6526 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
6528 error = ldi_ident_from_mod(&modlinkage, &zfs_li);
6530 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
6540 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
6541 return (SET_ERROR(EBUSY));
6543 if ((error = mod_remove(&modlinkage)) != 0)
6549 if (zfs_nfsshare_inited)
6550 (void) ddi_modclose(nfs_mod);
6551 if (zfs_smbshare_inited)
6552 (void) ddi_modclose(smbsrv_mod);
6553 if (zfs_nfsshare_inited || zfs_smbshare_inited)
6554 (void) ddi_modclose(sharefs_mod);
6556 tsd_destroy(&zfs_fsyncer_key);
6557 ldi_ident_release(zfs_li);
6559 mutex_destroy(&zfs_share_lock);
6565 _info(struct modinfo *modinfop)
6567 return (mod_info(&modlinkage, modinfop));
6569 #endif /* illumos */
6571 static int zfs__init(void);
6572 static int zfs__fini(void);
6573 static void zfs_shutdown(void *, int);
6575 static eventhandler_tag zfs_shutdown_event_tag;
6578 #define ZFS_MIN_KSTACK_PAGES 4
6586 #if KSTACK_PAGES < ZFS_MIN_KSTACK_PAGES
6587 printf("ZFS NOTICE: KSTACK_PAGES is %d which could result in stack "
6588 "overflow panic!\nPlease consider adding "
6589 "'options KSTACK_PAGES=%d' to your kernel config\n", KSTACK_PAGES,
6590 ZFS_MIN_KSTACK_PAGES);
6593 zfs_root_token = root_mount_hold("ZFS");
6595 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
6597 spa_init(FREAD | FWRITE);
6602 tsd_create(&zfs_fsyncer_key, NULL);
6603 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
6604 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
6606 printf("ZFS storage pool version: features support (" SPA_VERSION_STRING ")\n");
6607 root_mount_rel(zfs_root_token);
6617 if (spa_busy() || zfs_busy() || zvol_busy() ||
6618 zio_injection_enabled) {
6627 tsd_destroy(&zfs_fsyncer_key);
6628 tsd_destroy(&rrw_tsd_key);
6629 tsd_destroy(&zfs_allow_log_key);
6631 mutex_destroy(&zfs_share_lock);
6637 zfs_shutdown(void *arg __unused, int howto __unused)
6641 * ZFS fini routines can not properly work in a panic-ed system.
6643 if (panicstr == NULL)
6649 zfs_modevent(module_t mod, int type, void *unused __unused)
6657 zfs_shutdown_event_tag = EVENTHANDLER_REGISTER(
6658 shutdown_post_sync, zfs_shutdown, NULL,
6659 SHUTDOWN_PRI_FIRST);
6663 if (err == 0 && zfs_shutdown_event_tag != NULL)
6664 EVENTHANDLER_DEREGISTER(shutdown_post_sync,
6665 zfs_shutdown_event_tag);
6672 return (EOPNOTSUPP);
6675 static moduledata_t zfs_mod = {
6680 DECLARE_MODULE(zfsctrl, zfs_mod, SI_SUB_VFS, SI_ORDER_ANY);
6681 MODULE_VERSION(zfsctrl, 1);
6682 MODULE_DEPEND(zfsctrl, opensolaris, 1, 1, 1);
6683 MODULE_DEPEND(zfsctrl, krpc, 1, 1, 1);
6684 MODULE_DEPEND(zfsctrl, acl_nfs4, 1, 1, 1);