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, OmniTI Computer Consulting, Inc. All rights reserved.
29 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
30 * Copyright (c) 2014, Joyent, Inc. All rights reserved.
31 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
32 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
33 * Copyright (c) 2013 Steven Hartland. All rights reserved.
39 * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
40 * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
42 * There are two ways that we handle ioctls: the legacy way where almost
43 * all of the logic is in the ioctl callback, and the new way where most
44 * of the marshalling is handled in the common entry point, zfsdev_ioctl().
46 * Non-legacy ioctls should be registered by calling
47 * zfs_ioctl_register() from zfs_ioctl_init(). The ioctl is invoked
48 * from userland by lzc_ioctl().
50 * The registration arguments are as follows:
53 * The name of the ioctl. This is used for history logging. If the
54 * ioctl returns successfully (the callback returns 0), and allow_log
55 * is true, then a history log entry will be recorded with the input &
56 * output nvlists. The log entry can be printed with "zpool history -i".
59 * The ioctl request number, which userland will pass to ioctl(2).
60 * The ioctl numbers can change from release to release, because
61 * the caller (libzfs) must be matched to the kernel.
63 * zfs_secpolicy_func_t *secpolicy
64 * This function will be called before the zfs_ioc_func_t, to
65 * determine if this operation is permitted. It should return EPERM
66 * on failure, and 0 on success. Checks include determining if the
67 * dataset is visible in this zone, and if the user has either all
68 * zfs privileges in the zone (SYS_MOUNT), or has been granted permission
69 * to do this operation on this dataset with "zfs allow".
71 * zfs_ioc_namecheck_t namecheck
72 * This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
73 * name, a dataset name, or nothing. If the name is not well-formed,
74 * the ioctl will fail and the callback will not be called.
75 * Therefore, the callback can assume that the name is well-formed
76 * (e.g. is null-terminated, doesn't have more than one '@' character,
77 * doesn't have invalid characters).
79 * zfs_ioc_poolcheck_t pool_check
80 * This specifies requirements on the pool state. If the pool does
81 * not meet them (is suspended or is readonly), the ioctl will fail
82 * and the callback will not be called. If any checks are specified
83 * (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
84 * Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
85 * POOL_CHECK_READONLY).
87 * boolean_t smush_outnvlist
88 * If smush_outnvlist is true, then the output is presumed to be a
89 * list of errors, and it will be "smushed" down to fit into the
90 * caller's buffer, by removing some entries and replacing them with a
91 * single "N_MORE_ERRORS" entry indicating how many were removed. See
92 * nvlist_smush() for details. If smush_outnvlist is false, and the
93 * outnvlist does not fit into the userland-provided buffer, then the
94 * ioctl will fail with ENOMEM.
96 * zfs_ioc_func_t *func
97 * The callback function that will perform the operation.
99 * The callback should return 0 on success, or an error number on
100 * failure. If the function fails, the userland ioctl will return -1,
101 * and errno will be set to the callback's return value. The callback
102 * will be called with the following arguments:
105 * The name of the pool or dataset to operate on, from
106 * zfs_cmd_t:zc_name. The 'namecheck' argument specifies the
107 * expected type (pool, dataset, or none).
110 * The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src. Or
111 * NULL if no input nvlist was provided. Changes to this nvlist are
112 * ignored. If the input nvlist could not be deserialized, the
113 * ioctl will fail and the callback will not be called.
116 * The output nvlist, initially empty. The callback can fill it in,
117 * and it will be returned to userland by serializing it into
118 * zfs_cmd_t:zc_nvlist_dst. If it is non-empty, and serialization
119 * fails (e.g. because the caller didn't supply a large enough
120 * buffer), then the overall ioctl will fail. See the
121 * 'smush_nvlist' argument above for additional behaviors.
123 * There are two typical uses of the output nvlist:
124 * - To return state, e.g. property values. In this case,
125 * smush_outnvlist should be false. If the buffer was not large
126 * enough, the caller will reallocate a larger buffer and try
129 * - To return multiple errors from an ioctl which makes on-disk
130 * changes. In this case, smush_outnvlist should be true.
131 * Ioctls which make on-disk modifications should generally not
132 * use the outnvl if they succeed, because the caller can not
133 * distinguish between the operation failing, and
134 * deserialization failing.
137 #include "opt_kstack_pages.h"
140 #include <sys/types.h>
141 #include <sys/param.h>
142 #include <sys/systm.h>
143 #include <sys/conf.h>
144 #include <sys/kernel.h>
145 #include <sys/lock.h>
146 #include <sys/malloc.h>
147 #include <sys/mutex.h>
148 #include <sys/proc.h>
149 #include <sys/errno.h>
152 #include <sys/file.h>
153 #include <sys/kmem.h>
154 #include <sys/conf.h>
155 #include <sys/cmn_err.h>
156 #include <sys/stat.h>
157 #include <sys/zfs_ioctl.h>
158 #include <sys/zfs_vfsops.h>
159 #include <sys/zfs_znode.h>
162 #include <sys/spa_impl.h>
163 #include <sys/vdev.h>
165 #include <sys/dsl_dir.h>
166 #include <sys/dsl_dataset.h>
167 #include <sys/dsl_prop.h>
168 #include <sys/dsl_deleg.h>
169 #include <sys/dmu_objset.h>
170 #include <sys/dmu_impl.h>
171 #include <sys/dmu_tx.h>
172 #include <sys/sunddi.h>
173 #include <sys/policy.h>
174 #include <sys/zone.h>
175 #include <sys/nvpair.h>
176 #include <sys/mount.h>
177 #include <sys/taskqueue.h>
179 #include <sys/varargs.h>
180 #include <sys/fs/zfs.h>
181 #include <sys/zfs_ctldir.h>
182 #include <sys/zfs_dir.h>
183 #include <sys/zfs_onexit.h>
184 #include <sys/zvol.h>
185 #include <sys/dsl_scan.h>
186 #include <sys/dmu_objset.h>
187 #include <sys/dmu_send.h>
188 #include <sys/dsl_destroy.h>
189 #include <sys/dsl_bookmark.h>
190 #include <sys/dsl_userhold.h>
191 #include <sys/zfeature.h>
192 #include <sys/zio_checksum.h>
194 #include "zfs_namecheck.h"
195 #include "zfs_prop.h"
196 #include "zfs_deleg.h"
197 #include "zfs_comutil.h"
198 #include "zfs_ioctl_compat.h"
200 CTASSERT(sizeof(zfs_cmd_t) < IOCPARM_MAX);
202 static struct cdev *zfsdev;
204 extern void zfs_init(void);
205 extern void zfs_fini(void);
207 uint_t zfs_fsyncer_key;
208 extern uint_t rrw_tsd_key;
209 static uint_t zfs_allow_log_key;
211 typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *);
212 typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *);
213 typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *);
219 } zfs_ioc_namecheck_t;
222 POOL_CHECK_NONE = 1 << 0,
223 POOL_CHECK_SUSPENDED = 1 << 1,
224 POOL_CHECK_READONLY = 1 << 2,
225 } zfs_ioc_poolcheck_t;
227 typedef struct zfs_ioc_vec {
228 zfs_ioc_legacy_func_t *zvec_legacy_func;
229 zfs_ioc_func_t *zvec_func;
230 zfs_secpolicy_func_t *zvec_secpolicy;
231 zfs_ioc_namecheck_t zvec_namecheck;
232 boolean_t zvec_allow_log;
233 zfs_ioc_poolcheck_t zvec_pool_check;
234 boolean_t zvec_smush_outnvlist;
235 const char *zvec_name;
238 /* This array is indexed by zfs_userquota_prop_t */
239 static const char *userquota_perms[] = {
240 ZFS_DELEG_PERM_USERUSED,
241 ZFS_DELEG_PERM_USERQUOTA,
242 ZFS_DELEG_PERM_GROUPUSED,
243 ZFS_DELEG_PERM_GROUPQUOTA,
246 static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
247 static int zfs_check_settable(const char *name, nvpair_t *property,
249 static int zfs_check_clearable(char *dataset, nvlist_t *props,
251 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
253 int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
254 static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
256 static void zfsdev_close(void *data);
258 static int zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature);
260 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
262 __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
269 * Get rid of annoying "../common/" prefix to filename.
271 newfile = strrchr(file, '/');
272 if (newfile != NULL) {
273 newfile = newfile + 1; /* Get rid of leading / */
279 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
283 * To get this data, use the zfs-dprintf probe as so:
284 * dtrace -q -n 'zfs-dprintf \
285 * /stringof(arg0) == "dbuf.c"/ \
286 * {printf("%s: %s", stringof(arg1), stringof(arg3))}'
288 * arg1 = function name
292 DTRACE_PROBE4(zfs__dprintf,
293 char *, newfile, char *, func, int, line, char *, buf);
297 history_str_free(char *buf)
299 kmem_free(buf, HIS_MAX_RECORD_LEN);
303 history_str_get(zfs_cmd_t *zc)
307 if (zc->zc_history == 0)
310 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
311 if (copyinstr((void *)(uintptr_t)zc->zc_history,
312 buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
313 history_str_free(buf);
317 buf[HIS_MAX_RECORD_LEN -1] = '\0';
323 * Check to see if the named dataset is currently defined as bootable
326 zfs_is_bootfs(const char *name)
330 if (dmu_objset_hold(name, FTAG, &os) == 0) {
332 ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
333 dmu_objset_rele(os, FTAG);
340 * Return non-zero if the spa version is less than requested version.
343 zfs_earlier_version(const char *name, int version)
347 if (spa_open(name, &spa, FTAG) == 0) {
348 if (spa_version(spa) < version) {
349 spa_close(spa, FTAG);
352 spa_close(spa, FTAG);
358 * Return TRUE if the ZPL version is less than requested version.
361 zpl_earlier_version(const char *name, int version)
364 boolean_t rc = B_TRUE;
366 if (dmu_objset_hold(name, FTAG, &os) == 0) {
369 if (dmu_objset_type(os) != DMU_OST_ZFS) {
370 dmu_objset_rele(os, FTAG);
373 /* XXX reading from non-owned objset */
374 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
375 rc = zplversion < version;
376 dmu_objset_rele(os, FTAG);
382 zfs_log_history(zfs_cmd_t *zc)
387 if ((buf = history_str_get(zc)) == NULL)
390 if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
391 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
392 (void) spa_history_log(spa, buf);
393 spa_close(spa, FTAG);
395 history_str_free(buf);
399 * Policy for top-level read operations (list pools). Requires no privileges,
400 * and can be used in the local zone, as there is no associated dataset.
404 zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
410 * Policy for dataset read operations (list children, get statistics). Requires
411 * no privileges, but must be visible in the local zone.
415 zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
417 if (INGLOBALZONE(curthread) ||
418 zone_dataset_visible(zc->zc_name, NULL))
421 return (SET_ERROR(ENOENT));
425 zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
430 * The dataset must be visible by this zone -- check this first
431 * so they don't see EPERM on something they shouldn't know about.
433 if (!INGLOBALZONE(curthread) &&
434 !zone_dataset_visible(dataset, &writable))
435 return (SET_ERROR(ENOENT));
437 if (INGLOBALZONE(curthread)) {
439 * If the fs is zoned, only root can access it from the
442 if (secpolicy_zfs(cr) && zoned)
443 return (SET_ERROR(EPERM));
446 * If we are in a local zone, the 'zoned' property must be set.
449 return (SET_ERROR(EPERM));
451 /* must be writable by this zone */
453 return (SET_ERROR(EPERM));
459 zfs_dozonecheck(const char *dataset, cred_t *cr)
463 if (dsl_prop_get_integer(dataset, "jailed", &zoned, NULL))
464 return (SET_ERROR(ENOENT));
466 return (zfs_dozonecheck_impl(dataset, zoned, cr));
470 zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
474 if (dsl_prop_get_int_ds(ds, "jailed", &zoned))
475 return (SET_ERROR(ENOENT));
477 return (zfs_dozonecheck_impl(dataset, zoned, cr));
481 zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
482 const char *perm, cred_t *cr)
486 error = zfs_dozonecheck_ds(name, ds, cr);
488 error = secpolicy_zfs(cr);
490 error = dsl_deleg_access_impl(ds, perm, cr);
496 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
502 error = dsl_pool_hold(name, FTAG, &dp);
506 error = dsl_dataset_hold(dp, name, FTAG, &ds);
508 dsl_pool_rele(dp, FTAG);
512 error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
514 dsl_dataset_rele(ds, FTAG);
515 dsl_pool_rele(dp, FTAG);
521 * Policy for setting the security label property.
523 * Returns 0 for success, non-zero for access and other errors.
526 zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
528 char ds_hexsl[MAXNAMELEN];
529 bslabel_t ds_sl, new_sl;
530 boolean_t new_default = FALSE;
532 int needed_priv = -1;
535 /* First get the existing dataset label. */
536 error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
537 1, sizeof (ds_hexsl), &ds_hexsl, NULL);
539 return (SET_ERROR(EPERM));
541 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
544 /* The label must be translatable */
545 if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
546 return (SET_ERROR(EINVAL));
549 * In a non-global zone, disallow attempts to set a label that
550 * doesn't match that of the zone; otherwise no other checks
553 if (!INGLOBALZONE(curproc)) {
554 if (new_default || !blequal(&new_sl, CR_SL(CRED())))
555 return (SET_ERROR(EPERM));
560 * For global-zone datasets (i.e., those whose zoned property is
561 * "off", verify that the specified new label is valid for the
564 if (dsl_prop_get_integer(name,
565 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
566 return (SET_ERROR(EPERM));
568 if (zfs_check_global_label(name, strval) != 0)
569 return (SET_ERROR(EPERM));
573 * If the existing dataset label is nondefault, check if the
574 * dataset is mounted (label cannot be changed while mounted).
575 * Get the zfsvfs; if there isn't one, then the dataset isn't
576 * mounted (or isn't a dataset, doesn't exist, ...).
578 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
580 static char *setsl_tag = "setsl_tag";
583 * Try to own the dataset; abort if there is any error,
584 * (e.g., already mounted, in use, or other error).
586 error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE,
589 return (SET_ERROR(EPERM));
591 dmu_objset_disown(os, setsl_tag);
594 needed_priv = PRIV_FILE_DOWNGRADE_SL;
598 if (hexstr_to_label(strval, &new_sl) != 0)
599 return (SET_ERROR(EPERM));
601 if (blstrictdom(&ds_sl, &new_sl))
602 needed_priv = PRIV_FILE_DOWNGRADE_SL;
603 else if (blstrictdom(&new_sl, &ds_sl))
604 needed_priv = PRIV_FILE_UPGRADE_SL;
606 /* dataset currently has a default label */
608 needed_priv = PRIV_FILE_UPGRADE_SL;
612 if (needed_priv != -1)
613 return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
616 #endif /* SECLABEL */
619 zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
625 * Check permissions for special properties.
630 * Disallow setting of 'zoned' from within a local zone.
632 if (!INGLOBALZONE(curthread))
633 return (SET_ERROR(EPERM));
637 case ZFS_PROP_FILESYSTEM_LIMIT:
638 case ZFS_PROP_SNAPSHOT_LIMIT:
639 if (!INGLOBALZONE(curthread)) {
641 char setpoint[MAXNAMELEN];
643 * Unprivileged users are allowed to modify the
644 * limit on things *under* (ie. contained by)
645 * the thing they own.
647 if (dsl_prop_get_integer(dsname, "jailed", &zoned,
649 return (SET_ERROR(EPERM));
650 if (!zoned || strlen(dsname) <= strlen(setpoint))
651 return (SET_ERROR(EPERM));
655 case ZFS_PROP_MLSLABEL:
657 if (!is_system_labeled())
658 return (SET_ERROR(EPERM));
660 if (nvpair_value_string(propval, &strval) == 0) {
663 err = zfs_set_slabel_policy(dsname, strval, CRED());
673 return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
678 zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
682 error = zfs_dozonecheck(zc->zc_name, cr);
687 * permission to set permissions will be evaluated later in
688 * dsl_deleg_can_allow()
695 zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
697 return (zfs_secpolicy_write_perms(zc->zc_name,
698 ZFS_DELEG_PERM_ROLLBACK, cr));
703 zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
711 * Generate the current snapshot name from the given objsetid, then
712 * use that name for the secpolicy/zone checks.
714 cp = strchr(zc->zc_name, '@');
716 return (SET_ERROR(EINVAL));
717 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
721 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
723 dsl_pool_rele(dp, FTAG);
727 dsl_dataset_name(ds, zc->zc_name);
729 error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
730 ZFS_DELEG_PERM_SEND, cr);
731 dsl_dataset_rele(ds, FTAG);
732 dsl_pool_rele(dp, FTAG);
739 zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
741 return (zfs_secpolicy_write_perms(zc->zc_name,
742 ZFS_DELEG_PERM_SEND, cr));
747 zfs_secpolicy_deleg_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
752 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
753 NO_FOLLOW, NULL, &vp)) != 0)
756 /* Now make sure mntpnt and dataset are ZFS */
758 if (strcmp(vp->v_vfsp->mnt_stat.f_fstypename, "zfs") != 0 ||
759 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
760 zc->zc_name) != 0)) {
762 return (SET_ERROR(EPERM));
766 return (dsl_deleg_access(zc->zc_name,
767 ZFS_DELEG_PERM_SHARE, cr));
771 zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
773 if (!INGLOBALZONE(curthread))
774 return (SET_ERROR(EPERM));
776 if (secpolicy_nfs(cr) == 0) {
779 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
784 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
786 if (!INGLOBALZONE(curthread))
787 return (SET_ERROR(EPERM));
789 if (secpolicy_smb(cr) == 0) {
792 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
797 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
802 * Remove the @bla or /bla from the end of the name to get the parent.
804 (void) strncpy(parent, datasetname, parentsize);
805 cp = strrchr(parent, '@');
809 cp = strrchr(parent, '/');
811 return (SET_ERROR(ENOENT));
819 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
823 if ((error = zfs_secpolicy_write_perms(name,
824 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
827 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
832 zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
834 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
838 * Destroying snapshots with delegated permissions requires
839 * descendant mount and destroy permissions.
843 zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
846 nvpair_t *pair, *nextpair;
849 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
850 return (SET_ERROR(EINVAL));
851 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
853 nextpair = nvlist_next_nvpair(snaps, pair);
854 error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr);
855 if (error == ENOENT) {
857 * Ignore any snapshots that don't exist (we consider
858 * them "already destroyed"). Remove the name from the
859 * nvl here in case the snapshot is created between
860 * now and when we try to destroy it (in which case
861 * we don't want to destroy it since we haven't
862 * checked for permission).
864 fnvlist_remove_nvpair(snaps, pair);
875 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
877 char parentname[MAXNAMELEN];
880 if ((error = zfs_secpolicy_write_perms(from,
881 ZFS_DELEG_PERM_RENAME, cr)) != 0)
884 if ((error = zfs_secpolicy_write_perms(from,
885 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
888 if ((error = zfs_get_parent(to, parentname,
889 sizeof (parentname))) != 0)
892 if ((error = zfs_secpolicy_write_perms(parentname,
893 ZFS_DELEG_PERM_CREATE, cr)) != 0)
896 if ((error = zfs_secpolicy_write_perms(parentname,
897 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
905 zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
910 if ((zc->zc_cookie & 1) != 0) {
912 * This is recursive rename, so the starting snapshot might
913 * not exist. Check file system or volume permission instead.
915 at = strchr(zc->zc_name, '@');
921 error = zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr);
931 zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
934 dsl_dataset_t *clone;
937 error = zfs_secpolicy_write_perms(zc->zc_name,
938 ZFS_DELEG_PERM_PROMOTE, cr);
942 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
946 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
949 char parentname[MAXNAMELEN];
950 dsl_dataset_t *origin = NULL;
954 error = dsl_dataset_hold_obj(dd->dd_pool,
955 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin);
957 dsl_dataset_rele(clone, FTAG);
958 dsl_pool_rele(dp, FTAG);
962 error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
963 ZFS_DELEG_PERM_MOUNT, cr);
965 dsl_dataset_name(origin, parentname);
967 error = zfs_secpolicy_write_perms_ds(parentname, origin,
968 ZFS_DELEG_PERM_PROMOTE, cr);
970 dsl_dataset_rele(clone, FTAG);
971 dsl_dataset_rele(origin, FTAG);
973 dsl_pool_rele(dp, FTAG);
979 zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
983 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
984 ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
987 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
988 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
991 return (zfs_secpolicy_write_perms(zc->zc_name,
992 ZFS_DELEG_PERM_CREATE, cr));
996 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
998 return (zfs_secpolicy_write_perms(name,
999 ZFS_DELEG_PERM_SNAPSHOT, cr));
1003 * Check for permission to create each snapshot in the nvlist.
1007 zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1013 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
1014 return (SET_ERROR(EINVAL));
1015 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
1016 pair = nvlist_next_nvpair(snaps, pair)) {
1017 char *name = nvpair_name(pair);
1018 char *atp = strchr(name, '@');
1021 error = SET_ERROR(EINVAL);
1025 error = zfs_secpolicy_snapshot_perms(name, cr);
1034 * Check for permission to create each snapshot in the nvlist.
1038 zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1042 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
1043 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
1044 char *name = nvpair_name(pair);
1045 char *hashp = strchr(name, '#');
1047 if (hashp == NULL) {
1048 error = SET_ERROR(EINVAL);
1052 error = zfs_secpolicy_write_perms(name,
1053 ZFS_DELEG_PERM_BOOKMARK, cr);
1063 zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1065 nvpair_t *pair, *nextpair;
1068 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1070 char *name = nvpair_name(pair);
1071 char *hashp = strchr(name, '#');
1072 nextpair = nvlist_next_nvpair(innvl, pair);
1074 if (hashp == NULL) {
1075 error = SET_ERROR(EINVAL);
1080 error = zfs_secpolicy_write_perms(name,
1081 ZFS_DELEG_PERM_DESTROY, cr);
1083 if (error == ENOENT) {
1085 * Ignore any filesystems that don't exist (we consider
1086 * their bookmarks "already destroyed"). Remove
1087 * the name from the nvl here in case the filesystem
1088 * is created between now and when we try to destroy
1089 * the bookmark (in which case we don't want to
1090 * destroy it since we haven't checked for permission).
1092 fnvlist_remove_nvpair(innvl, pair);
1104 zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1107 * Even root must have a proper TSD so that we know what pool
1110 if (tsd_get(zfs_allow_log_key) == NULL)
1111 return (SET_ERROR(EPERM));
1116 zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1118 char parentname[MAXNAMELEN];
1122 if ((error = zfs_get_parent(zc->zc_name, parentname,
1123 sizeof (parentname))) != 0)
1126 if (nvlist_lookup_string(innvl, "origin", &origin) == 0 &&
1127 (error = zfs_secpolicy_write_perms(origin,
1128 ZFS_DELEG_PERM_CLONE, cr)) != 0)
1131 if ((error = zfs_secpolicy_write_perms(parentname,
1132 ZFS_DELEG_PERM_CREATE, cr)) != 0)
1135 return (zfs_secpolicy_write_perms(parentname,
1136 ZFS_DELEG_PERM_MOUNT, cr));
1140 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
1141 * SYS_CONFIG privilege, which is not available in a local zone.
1145 zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1147 if (secpolicy_sys_config(cr, B_FALSE) != 0)
1148 return (SET_ERROR(EPERM));
1154 * Policy for object to name lookups.
1158 zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1162 if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
1165 error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
1170 * Policy for fault injection. Requires all privileges.
1174 zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1176 return (secpolicy_zinject(cr));
1181 zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1183 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
1185 if (prop == ZPROP_INVAL) {
1186 if (!zfs_prop_user(zc->zc_value))
1187 return (SET_ERROR(EINVAL));
1188 return (zfs_secpolicy_write_perms(zc->zc_name,
1189 ZFS_DELEG_PERM_USERPROP, cr));
1191 return (zfs_secpolicy_setprop(zc->zc_name, prop,
1197 zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1199 int err = zfs_secpolicy_read(zc, innvl, cr);
1203 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1204 return (SET_ERROR(EINVAL));
1206 if (zc->zc_value[0] == 0) {
1208 * They are asking about a posix uid/gid. If it's
1209 * themself, allow it.
1211 if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
1212 zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
1213 if (zc->zc_guid == crgetuid(cr))
1216 if (groupmember(zc->zc_guid, cr))
1221 return (zfs_secpolicy_write_perms(zc->zc_name,
1222 userquota_perms[zc->zc_objset_type], cr));
1226 zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1228 int err = zfs_secpolicy_read(zc, innvl, cr);
1232 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1233 return (SET_ERROR(EINVAL));
1235 return (zfs_secpolicy_write_perms(zc->zc_name,
1236 userquota_perms[zc->zc_objset_type], cr));
1241 zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1243 return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
1249 zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1255 error = nvlist_lookup_nvlist(innvl, "holds", &holds);
1257 return (SET_ERROR(EINVAL));
1259 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
1260 pair = nvlist_next_nvpair(holds, pair)) {
1261 char fsname[MAXNAMELEN];
1262 error = dmu_fsname(nvpair_name(pair), fsname);
1265 error = zfs_secpolicy_write_perms(fsname,
1266 ZFS_DELEG_PERM_HOLD, cr);
1275 zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1280 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1281 pair = nvlist_next_nvpair(innvl, pair)) {
1282 char fsname[MAXNAMELEN];
1283 error = dmu_fsname(nvpair_name(pair), fsname);
1286 error = zfs_secpolicy_write_perms(fsname,
1287 ZFS_DELEG_PERM_RELEASE, cr);
1295 * Policy for allowing temporary snapshots to be taken or released
1298 zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1301 * A temporary snapshot is the same as a snapshot,
1302 * hold, destroy and release all rolled into one.
1303 * Delegated diff alone is sufficient that we allow this.
1307 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1308 ZFS_DELEG_PERM_DIFF, cr)) == 0)
1311 error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
1313 error = zfs_secpolicy_hold(zc, innvl, cr);
1315 error = zfs_secpolicy_release(zc, innvl, cr);
1317 error = zfs_secpolicy_destroy(zc, innvl, cr);
1322 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1325 get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1329 nvlist_t *list = NULL;
1332 * Read in and unpack the user-supplied nvlist.
1335 return (SET_ERROR(EINVAL));
1337 packed = kmem_alloc(size, KM_SLEEP);
1339 if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1341 kmem_free(packed, size);
1342 return (SET_ERROR(EFAULT));
1345 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1346 kmem_free(packed, size);
1350 kmem_free(packed, size);
1357 * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
1358 * Entries will be removed from the end of the nvlist, and one int32 entry
1359 * named "N_MORE_ERRORS" will be added indicating how many entries were
1363 nvlist_smush(nvlist_t *errors, size_t max)
1367 size = fnvlist_size(errors);
1370 nvpair_t *more_errors;
1374 return (SET_ERROR(ENOMEM));
1376 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
1377 more_errors = nvlist_prev_nvpair(errors, NULL);
1380 nvpair_t *pair = nvlist_prev_nvpair(errors,
1382 fnvlist_remove_nvpair(errors, pair);
1384 size = fnvlist_size(errors);
1385 } while (size > max);
1387 fnvlist_remove_nvpair(errors, more_errors);
1388 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
1389 ASSERT3U(fnvlist_size(errors), <=, max);
1396 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1398 char *packed = NULL;
1402 size = fnvlist_size(nvl);
1404 if (size > zc->zc_nvlist_dst_size) {
1406 * Solaris returns ENOMEM here, because even if an error is
1407 * returned from an ioctl(2), new zc_nvlist_dst_size will be
1408 * passed to the userland. This is not the case for FreeBSD.
1409 * We need to return 0, so the kernel will copy the
1410 * zc_nvlist_dst_size back and the userland can discover that a
1411 * bigger buffer is needed.
1415 packed = fnvlist_pack(nvl, &size);
1416 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1417 size, zc->zc_iflags) != 0)
1418 error = SET_ERROR(EFAULT);
1419 fnvlist_pack_free(packed, size);
1422 zc->zc_nvlist_dst_size = size;
1423 zc->zc_nvlist_dst_filled = B_TRUE;
1428 getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
1433 error = dmu_objset_hold(dsname, FTAG, &os);
1436 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1437 dmu_objset_rele(os, FTAG);
1438 return (SET_ERROR(EINVAL));
1441 mutex_enter(&os->os_user_ptr_lock);
1442 *zfvp = dmu_objset_get_user(os);
1444 VFS_HOLD((*zfvp)->z_vfs);
1446 error = SET_ERROR(ESRCH);
1448 mutex_exit(&os->os_user_ptr_lock);
1449 dmu_objset_rele(os, FTAG);
1454 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
1455 * case its z_vfs will be NULL, and it will be opened as the owner.
1456 * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1457 * which prevents all vnode ops from running.
1460 zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
1464 if (getzfsvfs(name, zfvp) != 0)
1465 error = zfsvfs_create(name, zfvp);
1467 rrm_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
1469 if ((*zfvp)->z_unmounted) {
1471 * XXX we could probably try again, since the unmounting
1472 * thread should be just about to disassociate the
1473 * objset from the zfsvfs.
1475 rrm_exit(&(*zfvp)->z_teardown_lock, tag);
1476 return (SET_ERROR(EBUSY));
1483 zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
1485 rrm_exit(&zfsvfs->z_teardown_lock, tag);
1487 if (zfsvfs->z_vfs) {
1488 VFS_RELE(zfsvfs->z_vfs);
1490 dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1491 zfsvfs_free(zfsvfs);
1496 zfs_ioc_pool_create(zfs_cmd_t *zc)
1499 nvlist_t *config, *props = NULL;
1500 nvlist_t *rootprops = NULL;
1501 nvlist_t *zplprops = NULL;
1503 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1504 zc->zc_iflags, &config))
1507 if (zc->zc_nvlist_src_size != 0 && (error =
1508 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1509 zc->zc_iflags, &props))) {
1510 nvlist_free(config);
1515 nvlist_t *nvl = NULL;
1516 uint64_t version = SPA_VERSION;
1518 (void) nvlist_lookup_uint64(props,
1519 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1520 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1521 error = SET_ERROR(EINVAL);
1522 goto pool_props_bad;
1524 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
1526 error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
1528 nvlist_free(config);
1532 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1534 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1535 error = zfs_fill_zplprops_root(version, rootprops,
1538 goto pool_props_bad;
1541 error = spa_create(zc->zc_name, config, props, zplprops);
1544 * Set the remaining root properties
1546 if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
1547 ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
1548 (void) spa_destroy(zc->zc_name);
1551 nvlist_free(rootprops);
1552 nvlist_free(zplprops);
1553 nvlist_free(config);
1560 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1563 zfs_log_history(zc);
1564 error = spa_destroy(zc->zc_name);
1566 zvol_remove_minors(zc->zc_name);
1571 zfs_ioc_pool_import(zfs_cmd_t *zc)
1573 nvlist_t *config, *props = NULL;
1577 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1578 zc->zc_iflags, &config)) != 0)
1581 if (zc->zc_nvlist_src_size != 0 && (error =
1582 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1583 zc->zc_iflags, &props))) {
1584 nvlist_free(config);
1588 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1589 guid != zc->zc_guid)
1590 error = SET_ERROR(EINVAL);
1592 error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1594 if (zc->zc_nvlist_dst != 0) {
1597 if ((err = put_nvlist(zc, config)) != 0)
1601 nvlist_free(config);
1610 zfs_ioc_pool_export(zfs_cmd_t *zc)
1613 boolean_t force = (boolean_t)zc->zc_cookie;
1614 boolean_t hardforce = (boolean_t)zc->zc_guid;
1616 zfs_log_history(zc);
1617 error = spa_export(zc->zc_name, NULL, force, hardforce);
1619 zvol_remove_minors(zc->zc_name);
1624 zfs_ioc_pool_configs(zfs_cmd_t *zc)
1629 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1630 return (SET_ERROR(EEXIST));
1632 error = put_nvlist(zc, configs);
1634 nvlist_free(configs);
1641 * zc_name name of the pool
1644 * zc_cookie real errno
1645 * zc_nvlist_dst config nvlist
1646 * zc_nvlist_dst_size size of config nvlist
1649 zfs_ioc_pool_stats(zfs_cmd_t *zc)
1655 error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1656 sizeof (zc->zc_value));
1658 if (config != NULL) {
1659 ret = put_nvlist(zc, config);
1660 nvlist_free(config);
1663 * The config may be present even if 'error' is non-zero.
1664 * In this case we return success, and preserve the real errno
1667 zc->zc_cookie = error;
1676 * Try to import the given pool, returning pool stats as appropriate so that
1677 * user land knows which devices are available and overall pool health.
1680 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1682 nvlist_t *tryconfig, *config;
1685 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1686 zc->zc_iflags, &tryconfig)) != 0)
1689 config = spa_tryimport(tryconfig);
1691 nvlist_free(tryconfig);
1694 return (SET_ERROR(EINVAL));
1696 error = put_nvlist(zc, config);
1697 nvlist_free(config);
1704 * zc_name name of the pool
1705 * zc_cookie scan func (pool_scan_func_t)
1708 zfs_ioc_pool_scan(zfs_cmd_t *zc)
1713 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1716 if (zc->zc_cookie == POOL_SCAN_NONE)
1717 error = spa_scan_stop(spa);
1719 error = spa_scan(spa, zc->zc_cookie);
1721 spa_close(spa, FTAG);
1727 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1732 error = spa_open(zc->zc_name, &spa, FTAG);
1735 spa_close(spa, FTAG);
1741 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1746 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1749 if (zc->zc_cookie < spa_version(spa) ||
1750 !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1751 spa_close(spa, FTAG);
1752 return (SET_ERROR(EINVAL));
1755 spa_upgrade(spa, zc->zc_cookie);
1756 spa_close(spa, FTAG);
1762 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1769 if ((size = zc->zc_history_len) == 0)
1770 return (SET_ERROR(EINVAL));
1772 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1775 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1776 spa_close(spa, FTAG);
1777 return (SET_ERROR(ENOTSUP));
1780 hist_buf = kmem_alloc(size, KM_SLEEP);
1781 if ((error = spa_history_get(spa, &zc->zc_history_offset,
1782 &zc->zc_history_len, hist_buf)) == 0) {
1783 error = ddi_copyout(hist_buf,
1784 (void *)(uintptr_t)zc->zc_history,
1785 zc->zc_history_len, zc->zc_iflags);
1788 spa_close(spa, FTAG);
1789 kmem_free(hist_buf, size);
1794 zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1799 error = spa_open(zc->zc_name, &spa, FTAG);
1801 error = spa_change_guid(spa);
1802 spa_close(spa, FTAG);
1808 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1810 return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
1815 * zc_name name of filesystem
1816 * zc_obj object to find
1819 * zc_value name of object
1822 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1827 /* XXX reading from objset not owned */
1828 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1830 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1831 dmu_objset_rele(os, FTAG);
1832 return (SET_ERROR(EINVAL));
1834 error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
1835 sizeof (zc->zc_value));
1836 dmu_objset_rele(os, FTAG);
1843 * zc_name name of filesystem
1844 * zc_obj object to find
1847 * zc_stat stats on object
1848 * zc_value path to object
1851 zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1856 /* XXX reading from objset not owned */
1857 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1859 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1860 dmu_objset_rele(os, FTAG);
1861 return (SET_ERROR(EINVAL));
1863 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1864 sizeof (zc->zc_value));
1865 dmu_objset_rele(os, FTAG);
1871 zfs_ioc_vdev_add(zfs_cmd_t *zc)
1875 nvlist_t *config, **l2cache, **spares;
1876 uint_t nl2cache = 0, nspares = 0;
1878 error = spa_open(zc->zc_name, &spa, FTAG);
1882 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1883 zc->zc_iflags, &config);
1884 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1885 &l2cache, &nl2cache);
1887 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1892 * A root pool with concatenated devices is not supported.
1893 * Thus, can not add a device to a root pool.
1895 * Intent log device can not be added to a rootpool because
1896 * during mountroot, zil is replayed, a seperated log device
1897 * can not be accessed during the mountroot time.
1899 * l2cache and spare devices are ok to be added to a rootpool.
1901 if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
1902 nvlist_free(config);
1903 spa_close(spa, FTAG);
1904 return (SET_ERROR(EDOM));
1906 #endif /* illumos */
1909 error = spa_vdev_add(spa, config);
1910 nvlist_free(config);
1912 spa_close(spa, FTAG);
1918 * zc_name name of the pool
1919 * zc_nvlist_conf nvlist of devices to remove
1920 * zc_cookie to stop the remove?
1923 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1928 error = spa_open(zc->zc_name, &spa, FTAG);
1931 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1932 spa_close(spa, FTAG);
1937 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1941 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1943 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1945 switch (zc->zc_cookie) {
1946 case VDEV_STATE_ONLINE:
1947 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1950 case VDEV_STATE_OFFLINE:
1951 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1954 case VDEV_STATE_FAULTED:
1955 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1956 zc->zc_obj != VDEV_AUX_EXTERNAL)
1957 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1959 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
1962 case VDEV_STATE_DEGRADED:
1963 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1964 zc->zc_obj != VDEV_AUX_EXTERNAL)
1965 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1967 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
1971 error = SET_ERROR(EINVAL);
1973 zc->zc_cookie = newstate;
1974 spa_close(spa, FTAG);
1979 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1982 int replacing = zc->zc_cookie;
1986 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1989 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1990 zc->zc_iflags, &config)) == 0) {
1991 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1992 nvlist_free(config);
1995 spa_close(spa, FTAG);
2000 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
2005 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2008 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
2010 spa_close(spa, FTAG);
2015 zfs_ioc_vdev_split(zfs_cmd_t *zc)
2018 nvlist_t *config, *props = NULL;
2020 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
2022 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2025 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
2026 zc->zc_iflags, &config)) {
2027 spa_close(spa, FTAG);
2031 if (zc->zc_nvlist_src_size != 0 && (error =
2032 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2033 zc->zc_iflags, &props))) {
2034 spa_close(spa, FTAG);
2035 nvlist_free(config);
2039 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
2041 spa_close(spa, FTAG);
2043 nvlist_free(config);
2050 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
2053 char *path = zc->zc_value;
2054 uint64_t guid = zc->zc_guid;
2057 error = spa_open(zc->zc_name, &spa, FTAG);
2061 error = spa_vdev_setpath(spa, guid, path);
2062 spa_close(spa, FTAG);
2067 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
2070 char *fru = zc->zc_value;
2071 uint64_t guid = zc->zc_guid;
2074 error = spa_open(zc->zc_name, &spa, FTAG);
2078 error = spa_vdev_setfru(spa, guid, fru);
2079 spa_close(spa, FTAG);
2084 zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
2089 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2091 if (zc->zc_nvlist_dst != 0 &&
2092 (error = dsl_prop_get_all(os, &nv)) == 0) {
2093 dmu_objset_stats(os, nv);
2095 * NB: zvol_get_stats() will read the objset contents,
2096 * which we aren't supposed to do with a
2097 * DS_MODE_USER hold, because it could be
2098 * inconsistent. So this is a bit of a workaround...
2099 * XXX reading with out owning
2101 if (!zc->zc_objset_stats.dds_inconsistent &&
2102 dmu_objset_type(os) == DMU_OST_ZVOL) {
2103 error = zvol_get_stats(os, nv);
2108 error = put_nvlist(zc, nv);
2117 * zc_name name of filesystem
2118 * zc_nvlist_dst_size size of buffer for property nvlist
2121 * zc_objset_stats stats
2122 * zc_nvlist_dst property nvlist
2123 * zc_nvlist_dst_size size of property nvlist
2126 zfs_ioc_objset_stats(zfs_cmd_t *zc)
2131 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2133 error = zfs_ioc_objset_stats_impl(zc, os);
2134 dmu_objset_rele(os, FTAG);
2137 if (error == ENOMEM)
2144 * zc_name name of filesystem
2145 * zc_nvlist_dst_size size of buffer for property nvlist
2148 * zc_nvlist_dst received property nvlist
2149 * zc_nvlist_dst_size size of received property nvlist
2151 * Gets received properties (distinct from local properties on or after
2152 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
2153 * local property values.
2156 zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
2162 * Without this check, we would return local property values if the
2163 * caller has not already received properties on or after
2164 * SPA_VERSION_RECVD_PROPS.
2166 if (!dsl_prop_get_hasrecvd(zc->zc_name))
2167 return (SET_ERROR(ENOTSUP));
2169 if (zc->zc_nvlist_dst != 0 &&
2170 (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
2171 error = put_nvlist(zc, nv);
2179 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2185 * zfs_get_zplprop() will either find a value or give us
2186 * the default value (if there is one).
2188 if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2190 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2196 * zc_name name of filesystem
2197 * zc_nvlist_dst_size size of buffer for zpl property nvlist
2200 * zc_nvlist_dst zpl property nvlist
2201 * zc_nvlist_dst_size size of zpl property nvlist
2204 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2209 /* XXX reading without owning */
2210 if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
2213 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2216 * NB: nvl_add_zplprop() will read the objset contents,
2217 * which we aren't supposed to do with a DS_MODE_USER
2218 * hold, because it could be inconsistent.
2220 if (zc->zc_nvlist_dst != 0 &&
2221 !zc->zc_objset_stats.dds_inconsistent &&
2222 dmu_objset_type(os) == DMU_OST_ZFS) {
2225 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2226 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2227 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2228 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2229 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2230 err = put_nvlist(zc, nv);
2233 err = SET_ERROR(ENOENT);
2235 dmu_objset_rele(os, FTAG);
2240 dataset_name_hidden(const char *name)
2243 * Skip over datasets that are not visible in this zone,
2244 * internal datasets (which have a $ in their name), and
2245 * temporary datasets (which have a % in their name).
2247 if (strchr(name, '$') != NULL)
2249 if (strchr(name, '%') != NULL)
2251 if (!INGLOBALZONE(curthread) && !zone_dataset_visible(name, NULL))
2258 * zc_name name of filesystem
2259 * zc_cookie zap cursor
2260 * zc_nvlist_dst_size size of buffer for property nvlist
2263 * zc_name name of next filesystem
2264 * zc_cookie zap cursor
2265 * zc_objset_stats stats
2266 * zc_nvlist_dst property nvlist
2267 * zc_nvlist_dst_size size of property nvlist
2270 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2275 size_t orig_len = strlen(zc->zc_name);
2278 if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
2279 if (error == ENOENT)
2280 error = SET_ERROR(ESRCH);
2284 p = strrchr(zc->zc_name, '/');
2285 if (p == NULL || p[1] != '\0')
2286 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2287 p = zc->zc_name + strlen(zc->zc_name);
2290 error = dmu_dir_list_next(os,
2291 sizeof (zc->zc_name) - (p - zc->zc_name), p,
2292 NULL, &zc->zc_cookie);
2293 if (error == ENOENT)
2294 error = SET_ERROR(ESRCH);
2295 } while (error == 0 && dataset_name_hidden(zc->zc_name));
2296 dmu_objset_rele(os, FTAG);
2299 * If it's an internal dataset (ie. with a '$' in its name),
2300 * don't try to get stats for it, otherwise we'll return ENOENT.
2302 if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
2303 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2304 if (error == ENOENT) {
2305 /* We lost a race with destroy, get the next one. */
2306 zc->zc_name[orig_len] = '\0';
2315 * zc_name name of filesystem
2316 * zc_cookie zap cursor
2317 * zc_nvlist_dst_size size of buffer for property nvlist
2318 * zc_simple when set, only name is requested
2321 * zc_name name of next snapshot
2322 * zc_objset_stats stats
2323 * zc_nvlist_dst property nvlist
2324 * zc_nvlist_dst_size size of property nvlist
2327 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2332 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2334 return (error == ENOENT ? ESRCH : error);
2338 * A dataset name of maximum length cannot have any snapshots,
2339 * so exit immediately.
2341 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
2342 dmu_objset_rele(os, FTAG);
2343 return (SET_ERROR(ESRCH));
2346 error = dmu_snapshot_list_next(os,
2347 sizeof (zc->zc_name) - strlen(zc->zc_name),
2348 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2351 if (error == 0 && !zc->zc_simple) {
2353 dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2355 error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
2359 error = dmu_objset_from_ds(ds, &ossnap);
2361 error = zfs_ioc_objset_stats_impl(zc, ossnap);
2362 dsl_dataset_rele(ds, FTAG);
2364 } else if (error == ENOENT) {
2365 error = SET_ERROR(ESRCH);
2368 dmu_objset_rele(os, FTAG);
2369 /* if we failed, undo the @ that we tacked on to zc_name */
2371 *strchr(zc->zc_name, '@') = '\0';
2376 zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2378 const char *propname = nvpair_name(pair);
2380 unsigned int vallen;
2383 zfs_userquota_prop_t type;
2389 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2391 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2392 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2394 return (SET_ERROR(EINVAL));
2398 * A correctly constructed propname is encoded as
2399 * userquota@<rid>-<domain>.
2401 if ((dash = strchr(propname, '-')) == NULL ||
2402 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2404 return (SET_ERROR(EINVAL));
2411 err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
2413 err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
2414 zfsvfs_rele(zfsvfs, FTAG);
2421 * If the named property is one that has a special function to set its value,
2422 * return 0 on success and a positive error code on failure; otherwise if it is
2423 * not one of the special properties handled by this function, return -1.
2425 * XXX: It would be better for callers of the property interface if we handled
2426 * these special cases in dsl_prop.c (in the dsl layer).
2429 zfs_prop_set_special(const char *dsname, zprop_source_t source,
2432 const char *propname = nvpair_name(pair);
2433 zfs_prop_t prop = zfs_name_to_prop(propname);
2437 if (prop == ZPROP_INVAL) {
2438 if (zfs_prop_userquota(propname))
2439 return (zfs_prop_set_userquota(dsname, pair));
2443 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2445 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2446 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2450 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
2453 VERIFY(0 == nvpair_value_uint64(pair, &intval));
2456 case ZFS_PROP_QUOTA:
2457 err = dsl_dir_set_quota(dsname, source, intval);
2459 case ZFS_PROP_REFQUOTA:
2460 err = dsl_dataset_set_refquota(dsname, source, intval);
2462 case ZFS_PROP_FILESYSTEM_LIMIT:
2463 case ZFS_PROP_SNAPSHOT_LIMIT:
2464 if (intval == UINT64_MAX) {
2465 /* clearing the limit, just do it */
2468 err = dsl_dir_activate_fs_ss_limit(dsname);
2471 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2472 * default path to set the value in the nvlist.
2477 case ZFS_PROP_RESERVATION:
2478 err = dsl_dir_set_reservation(dsname, source, intval);
2480 case ZFS_PROP_REFRESERVATION:
2481 err = dsl_dataset_set_refreservation(dsname, source, intval);
2483 case ZFS_PROP_VOLSIZE:
2484 err = zvol_set_volsize(dsname, intval);
2486 case ZFS_PROP_VERSION:
2490 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2493 err = zfs_set_version(zfsvfs, intval);
2494 zfsvfs_rele(zfsvfs, FTAG);
2496 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2499 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2500 (void) strcpy(zc->zc_name, dsname);
2501 (void) zfs_ioc_userspace_upgrade(zc);
2502 kmem_free(zc, sizeof (zfs_cmd_t));
2514 * This function is best effort. If it fails to set any of the given properties,
2515 * it continues to set as many as it can and returns the last error
2516 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2517 * with the list of names of all the properties that failed along with the
2518 * corresponding error numbers.
2520 * If every property is set successfully, zero is returned and errlist is not
2524 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2532 nvlist_t *genericnvl = fnvlist_alloc();
2533 nvlist_t *retrynvl = fnvlist_alloc();
2537 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2538 const char *propname = nvpair_name(pair);
2539 zfs_prop_t prop = zfs_name_to_prop(propname);
2542 /* decode the property value */
2544 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2546 attrs = fnvpair_value_nvlist(pair);
2547 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2549 err = SET_ERROR(EINVAL);
2552 /* Validate value type */
2553 if (err == 0 && prop == ZPROP_INVAL) {
2554 if (zfs_prop_user(propname)) {
2555 if (nvpair_type(propval) != DATA_TYPE_STRING)
2556 err = SET_ERROR(EINVAL);
2557 } else if (zfs_prop_userquota(propname)) {
2558 if (nvpair_type(propval) !=
2559 DATA_TYPE_UINT64_ARRAY)
2560 err = SET_ERROR(EINVAL);
2562 err = SET_ERROR(EINVAL);
2564 } else if (err == 0) {
2565 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2566 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2567 err = SET_ERROR(EINVAL);
2568 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2571 intval = fnvpair_value_uint64(propval);
2573 switch (zfs_prop_get_type(prop)) {
2574 case PROP_TYPE_NUMBER:
2576 case PROP_TYPE_STRING:
2577 err = SET_ERROR(EINVAL);
2579 case PROP_TYPE_INDEX:
2580 if (zfs_prop_index_to_string(prop,
2581 intval, &unused) != 0)
2582 err = SET_ERROR(EINVAL);
2586 "unknown property type");
2589 err = SET_ERROR(EINVAL);
2593 /* Validate permissions */
2595 err = zfs_check_settable(dsname, pair, CRED());
2598 err = zfs_prop_set_special(dsname, source, pair);
2601 * For better performance we build up a list of
2602 * properties to set in a single transaction.
2604 err = nvlist_add_nvpair(genericnvl, pair);
2605 } else if (err != 0 && nvl != retrynvl) {
2607 * This may be a spurious error caused by
2608 * receiving quota and reservation out of order.
2609 * Try again in a second pass.
2611 err = nvlist_add_nvpair(retrynvl, pair);
2616 if (errlist != NULL)
2617 fnvlist_add_int32(errlist, propname, err);
2622 if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2627 if (!nvlist_empty(genericnvl) &&
2628 dsl_props_set(dsname, source, genericnvl) != 0) {
2630 * If this fails, we still want to set as many properties as we
2631 * can, so try setting them individually.
2634 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2635 const char *propname = nvpair_name(pair);
2639 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2641 attrs = fnvpair_value_nvlist(pair);
2642 propval = fnvlist_lookup_nvpair(attrs,
2646 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2647 strval = fnvpair_value_string(propval);
2648 err = dsl_prop_set_string(dsname, propname,
2651 intval = fnvpair_value_uint64(propval);
2652 err = dsl_prop_set_int(dsname, propname, source,
2657 if (errlist != NULL) {
2658 fnvlist_add_int32(errlist, propname,
2665 nvlist_free(genericnvl);
2666 nvlist_free(retrynvl);
2672 * Check that all the properties are valid user properties.
2675 zfs_check_userprops(const char *fsname, nvlist_t *nvl)
2677 nvpair_t *pair = NULL;
2680 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2681 const char *propname = nvpair_name(pair);
2683 if (!zfs_prop_user(propname) ||
2684 nvpair_type(pair) != DATA_TYPE_STRING)
2685 return (SET_ERROR(EINVAL));
2687 if (error = zfs_secpolicy_write_perms(fsname,
2688 ZFS_DELEG_PERM_USERPROP, CRED()))
2691 if (strlen(propname) >= ZAP_MAXNAMELEN)
2692 return (SET_ERROR(ENAMETOOLONG));
2694 if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN)
2701 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2705 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2708 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2709 if (nvlist_exists(skipped, nvpair_name(pair)))
2712 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2717 clear_received_props(const char *dsname, nvlist_t *props,
2721 nvlist_t *cleared_props = NULL;
2722 props_skip(props, skipped, &cleared_props);
2723 if (!nvlist_empty(cleared_props)) {
2725 * Acts on local properties until the dataset has received
2726 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2728 zprop_source_t flags = (ZPROP_SRC_NONE |
2729 (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
2730 err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
2732 nvlist_free(cleared_props);
2738 * zc_name name of filesystem
2739 * zc_value name of property to set
2740 * zc_nvlist_src{_size} nvlist of properties to apply
2741 * zc_cookie received properties flag
2744 * zc_nvlist_dst{_size} error for each unapplied received property
2747 zfs_ioc_set_prop(zfs_cmd_t *zc)
2750 boolean_t received = zc->zc_cookie;
2751 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2756 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2757 zc->zc_iflags, &nvl)) != 0)
2761 nvlist_t *origprops;
2763 if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
2764 (void) clear_received_props(zc->zc_name,
2766 nvlist_free(origprops);
2769 error = dsl_prop_set_hasrecvd(zc->zc_name);
2772 errors = fnvlist_alloc();
2774 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
2776 if (zc->zc_nvlist_dst != 0 && errors != NULL) {
2777 (void) put_nvlist(zc, errors);
2780 nvlist_free(errors);
2787 * zc_name name of filesystem
2788 * zc_value name of property to inherit
2789 * zc_cookie revert to received value if TRUE
2794 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2796 const char *propname = zc->zc_value;
2797 zfs_prop_t prop = zfs_name_to_prop(propname);
2798 boolean_t received = zc->zc_cookie;
2799 zprop_source_t source = (received
2800 ? ZPROP_SRC_NONE /* revert to received value, if any */
2801 : ZPROP_SRC_INHERITED); /* explicitly inherit */
2810 * zfs_prop_set_special() expects properties in the form of an
2811 * nvpair with type info.
2813 if (prop == ZPROP_INVAL) {
2814 if (!zfs_prop_user(propname))
2815 return (SET_ERROR(EINVAL));
2817 type = PROP_TYPE_STRING;
2818 } else if (prop == ZFS_PROP_VOLSIZE ||
2819 prop == ZFS_PROP_VERSION) {
2820 return (SET_ERROR(EINVAL));
2822 type = zfs_prop_get_type(prop);
2825 VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2828 case PROP_TYPE_STRING:
2829 VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2831 case PROP_TYPE_NUMBER:
2832 case PROP_TYPE_INDEX:
2833 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2837 return (SET_ERROR(EINVAL));
2840 pair = nvlist_next_nvpair(dummy, NULL);
2841 err = zfs_prop_set_special(zc->zc_name, source, pair);
2844 return (err); /* special property already handled */
2847 * Only check this in the non-received case. We want to allow
2848 * 'inherit -S' to revert non-inheritable properties like quota
2849 * and reservation to the received or default values even though
2850 * they are not considered inheritable.
2852 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2853 return (SET_ERROR(EINVAL));
2856 /* property name has been validated by zfs_secpolicy_inherit_prop() */
2857 return (dsl_prop_inherit(zc->zc_name, zc->zc_value, source));
2861 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2868 if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2869 zc->zc_iflags, &props))
2873 * If the only property is the configfile, then just do a spa_lookup()
2874 * to handle the faulted case.
2876 pair = nvlist_next_nvpair(props, NULL);
2877 if (pair != NULL && strcmp(nvpair_name(pair),
2878 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2879 nvlist_next_nvpair(props, pair) == NULL) {
2880 mutex_enter(&spa_namespace_lock);
2881 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2882 spa_configfile_set(spa, props, B_FALSE);
2883 spa_config_sync(spa, B_FALSE, B_TRUE);
2885 mutex_exit(&spa_namespace_lock);
2892 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2897 error = spa_prop_set(spa, props);
2900 spa_close(spa, FTAG);
2906 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2910 nvlist_t *nvp = NULL;
2912 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2914 * If the pool is faulted, there may be properties we can still
2915 * get (such as altroot and cachefile), so attempt to get them
2918 mutex_enter(&spa_namespace_lock);
2919 if ((spa = spa_lookup(zc->zc_name)) != NULL)
2920 error = spa_prop_get(spa, &nvp);
2921 mutex_exit(&spa_namespace_lock);
2923 error = spa_prop_get(spa, &nvp);
2924 spa_close(spa, FTAG);
2927 if (error == 0 && zc->zc_nvlist_dst != 0)
2928 error = put_nvlist(zc, nvp);
2930 error = SET_ERROR(EFAULT);
2938 * zc_name name of filesystem
2939 * zc_nvlist_src{_size} nvlist of delegated permissions
2940 * zc_perm_action allow/unallow flag
2945 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2948 nvlist_t *fsaclnv = NULL;
2950 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2951 zc->zc_iflags, &fsaclnv)) != 0)
2955 * Verify nvlist is constructed correctly
2957 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2958 nvlist_free(fsaclnv);
2959 return (SET_ERROR(EINVAL));
2963 * If we don't have PRIV_SYS_MOUNT, then validate
2964 * that user is allowed to hand out each permission in
2968 error = secpolicy_zfs(CRED());
2970 if (zc->zc_perm_action == B_FALSE) {
2971 error = dsl_deleg_can_allow(zc->zc_name,
2974 error = dsl_deleg_can_unallow(zc->zc_name,
2980 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2982 nvlist_free(fsaclnv);
2988 * zc_name name of filesystem
2991 * zc_nvlist_src{_size} nvlist of delegated permissions
2994 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
2999 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
3000 error = put_nvlist(zc, nvp);
3008 * Search the vfs list for a specified resource. Returns a pointer to it
3009 * or NULL if no suitable entry is found. The caller of this routine
3010 * is responsible for releasing the returned vfs pointer.
3013 zfs_get_vfs(const char *resource)
3017 mtx_lock(&mountlist_mtx);
3018 TAILQ_FOREACH(vfsp, &mountlist, mnt_list) {
3019 if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
3024 mtx_unlock(&mountlist_mtx);
3030 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3032 zfs_creat_t *zct = arg;
3034 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
3037 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
3041 * os parent objset pointer (NULL if root fs)
3042 * fuids_ok fuids allowed in this version of the spa?
3043 * sa_ok SAs allowed in this version of the spa?
3044 * createprops list of properties requested by creator
3047 * zplprops values for the zplprops we attach to the master node object
3048 * is_ci true if requested file system will be purely case-insensitive
3050 * Determine the settings for utf8only, normalization and
3051 * casesensitivity. Specific values may have been requested by the
3052 * creator and/or we can inherit values from the parent dataset. If
3053 * the file system is of too early a vintage, a creator can not
3054 * request settings for these properties, even if the requested
3055 * setting is the default value. We don't actually want to create dsl
3056 * properties for these, so remove them from the source nvlist after
3060 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
3061 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
3062 nvlist_t *zplprops, boolean_t *is_ci)
3064 uint64_t sense = ZFS_PROP_UNDEFINED;
3065 uint64_t norm = ZFS_PROP_UNDEFINED;
3066 uint64_t u8 = ZFS_PROP_UNDEFINED;
3068 ASSERT(zplprops != NULL);
3071 * Pull out creator prop choices, if any.
3074 (void) nvlist_lookup_uint64(createprops,
3075 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
3076 (void) nvlist_lookup_uint64(createprops,
3077 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
3078 (void) nvlist_remove_all(createprops,
3079 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
3080 (void) nvlist_lookup_uint64(createprops,
3081 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
3082 (void) nvlist_remove_all(createprops,
3083 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
3084 (void) nvlist_lookup_uint64(createprops,
3085 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
3086 (void) nvlist_remove_all(createprops,
3087 zfs_prop_to_name(ZFS_PROP_CASE));
3091 * If the zpl version requested is whacky or the file system
3092 * or pool is version is too "young" to support normalization
3093 * and the creator tried to set a value for one of the props,
3096 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
3097 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
3098 (zplver >= ZPL_VERSION_SA && !sa_ok) ||
3099 (zplver < ZPL_VERSION_NORMALIZATION &&
3100 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
3101 sense != ZFS_PROP_UNDEFINED)))
3102 return (SET_ERROR(ENOTSUP));
3105 * Put the version in the zplprops
3107 VERIFY(nvlist_add_uint64(zplprops,
3108 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3110 if (norm == ZFS_PROP_UNDEFINED)
3111 VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
3112 VERIFY(nvlist_add_uint64(zplprops,
3113 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3116 * If we're normalizing, names must always be valid UTF-8 strings.
3120 if (u8 == ZFS_PROP_UNDEFINED)
3121 VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
3122 VERIFY(nvlist_add_uint64(zplprops,
3123 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3125 if (sense == ZFS_PROP_UNDEFINED)
3126 VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
3127 VERIFY(nvlist_add_uint64(zplprops,
3128 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3131 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
3137 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
3138 nvlist_t *zplprops, boolean_t *is_ci)
3140 boolean_t fuids_ok, sa_ok;
3141 uint64_t zplver = ZPL_VERSION;
3142 objset_t *os = NULL;
3143 char parentname[MAXNAMELEN];
3149 (void) strlcpy(parentname, dataset, sizeof (parentname));
3150 cp = strrchr(parentname, '/');
3154 if ((error = spa_open(dataset, &spa, FTAG)) != 0)
3157 spa_vers = spa_version(spa);
3158 spa_close(spa, FTAG);
3160 zplver = zfs_zpl_version_map(spa_vers);
3161 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3162 sa_ok = (zplver >= ZPL_VERSION_SA);
3165 * Open parent object set so we can inherit zplprop values.
3167 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
3170 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
3172 dmu_objset_rele(os, FTAG);
3177 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
3178 nvlist_t *zplprops, boolean_t *is_ci)
3182 uint64_t zplver = ZPL_VERSION;
3185 zplver = zfs_zpl_version_map(spa_vers);
3186 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3187 sa_ok = (zplver >= ZPL_VERSION_SA);
3189 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
3190 createprops, zplprops, is_ci);
3196 * "type" -> dmu_objset_type_t (int32)
3197 * (optional) "props" -> { prop -> value }
3200 * outnvl: propname -> error code (int32)
3203 zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3206 zfs_creat_t zct = { 0 };
3207 nvlist_t *nvprops = NULL;
3208 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
3210 dmu_objset_type_t type;
3211 boolean_t is_insensitive = B_FALSE;
3213 if (nvlist_lookup_int32(innvl, "type", &type32) != 0)
3214 return (SET_ERROR(EINVAL));
3216 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3220 cbfunc = zfs_create_cb;
3224 cbfunc = zvol_create_cb;
3231 if (strchr(fsname, '@') ||
3232 strchr(fsname, '%'))
3233 return (SET_ERROR(EINVAL));
3235 zct.zct_props = nvprops;
3238 return (SET_ERROR(EINVAL));
3240 if (type == DMU_OST_ZVOL) {
3241 uint64_t volsize, volblocksize;
3243 if (nvprops == NULL)
3244 return (SET_ERROR(EINVAL));
3245 if (nvlist_lookup_uint64(nvprops,
3246 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3247 return (SET_ERROR(EINVAL));
3249 if ((error = nvlist_lookup_uint64(nvprops,
3250 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3251 &volblocksize)) != 0 && error != ENOENT)
3252 return (SET_ERROR(EINVAL));
3255 volblocksize = zfs_prop_default_numeric(
3256 ZFS_PROP_VOLBLOCKSIZE);
3258 if ((error = zvol_check_volblocksize(
3259 volblocksize)) != 0 ||
3260 (error = zvol_check_volsize(volsize,
3261 volblocksize)) != 0)
3263 } else if (type == DMU_OST_ZFS) {
3267 * We have to have normalization and
3268 * case-folding flags correct when we do the
3269 * file system creation, so go figure them out
3272 VERIFY(nvlist_alloc(&zct.zct_zplprops,
3273 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3274 error = zfs_fill_zplprops(fsname, nvprops,
3275 zct.zct_zplprops, &is_insensitive);
3277 nvlist_free(zct.zct_zplprops);
3282 error = dmu_objset_create(fsname, type,
3283 is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
3284 nvlist_free(zct.zct_zplprops);
3287 * It would be nice to do this atomically.
3290 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3293 (void) dsl_destroy_head(fsname);
3296 if (error == 0 && type == DMU_OST_ZVOL)
3297 zvol_create_minors(fsname);
3304 * "origin" -> name of origin snapshot
3305 * (optional) "props" -> { prop -> value }
3308 * outnvl: propname -> error code (int32)
3311 zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3314 nvlist_t *nvprops = NULL;
3317 if (nvlist_lookup_string(innvl, "origin", &origin_name) != 0)
3318 return (SET_ERROR(EINVAL));
3319 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3321 if (strchr(fsname, '@') ||
3322 strchr(fsname, '%'))
3323 return (SET_ERROR(EINVAL));
3325 if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3326 return (SET_ERROR(EINVAL));
3327 error = dmu_objset_clone(fsname, origin_name);
3332 * It would be nice to do this atomically.
3335 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3338 (void) dsl_destroy_head(fsname);
3342 zvol_create_minors(fsname);
3349 * "snaps" -> { snapshot1, snapshot2 }
3350 * (optional) "props" -> { prop -> value (string) }
3353 * outnvl: snapshot -> error code (int32)
3356 zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3359 nvlist_t *props = NULL;
3363 (void) nvlist_lookup_nvlist(innvl, "props", &props);
3364 if ((error = zfs_check_userprops(poolname, props)) != 0)
3367 if (!nvlist_empty(props) &&
3368 zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3369 return (SET_ERROR(ENOTSUP));
3371 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3372 return (SET_ERROR(EINVAL));
3373 poollen = strlen(poolname);
3374 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3375 pair = nvlist_next_nvpair(snaps, pair)) {
3376 const char *name = nvpair_name(pair);
3377 const char *cp = strchr(name, '@');
3380 * The snap name must contain an @, and the part after it must
3381 * contain only valid characters.
3384 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3385 return (SET_ERROR(EINVAL));
3388 * The snap must be in the specified pool.
3390 if (strncmp(name, poolname, poollen) != 0 ||
3391 (name[poollen] != '/' && name[poollen] != '@'))
3392 return (SET_ERROR(EXDEV));
3394 /* This must be the only snap of this fs. */
3395 for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
3396 pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
3397 if (strncmp(name, nvpair_name(pair2), cp - name + 1)
3399 return (SET_ERROR(EXDEV));
3404 error = dsl_dataset_snapshot(snaps, props, outnvl);
3409 * innvl: "message" -> string
3413 zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
3421 * The poolname in the ioctl is not set, we get it from the TSD,
3422 * which was set at the end of the last successful ioctl that allows
3423 * logging. The secpolicy func already checked that it is set.
3424 * Only one log ioctl is allowed after each successful ioctl, so
3425 * we clear the TSD here.
3427 poolname = tsd_get(zfs_allow_log_key);
3428 (void) tsd_set(zfs_allow_log_key, NULL);
3429 error = spa_open(poolname, &spa, FTAG);
3434 if (nvlist_lookup_string(innvl, "message", &message) != 0) {
3435 spa_close(spa, FTAG);
3436 return (SET_ERROR(EINVAL));
3439 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
3440 spa_close(spa, FTAG);
3441 return (SET_ERROR(ENOTSUP));
3444 error = spa_history_log(spa, message);
3445 spa_close(spa, FTAG);
3450 * The dp_config_rwlock must not be held when calling this, because the
3451 * unmount may need to write out data.
3453 * This function is best-effort. Callers must deal gracefully if it
3454 * remains mounted (or is remounted after this call).
3456 * Returns 0 if the argument is not a snapshot, or it is not currently a
3457 * filesystem, or we were able to unmount it. Returns error code otherwise.
3460 zfs_unmount_snap(const char *snapname)
3466 if (strchr(snapname, '@') == NULL)
3469 vfsp = zfs_get_vfs(snapname);
3473 zfsvfs = vfsp->vfs_data;
3474 ASSERT(!dsl_pool_config_held(dmu_objset_pool(zfsvfs->z_os)));
3476 err = vn_vfswlock(vfsp->vfs_vnodecovered);
3479 return (SET_ERROR(err));
3482 * Always force the unmount for snapshots.
3486 (void) dounmount(vfsp, MS_FORCE, kcred);
3489 (void) dounmount(vfsp, MS_FORCE, curthread);
3496 zfs_unmount_snap_cb(const char *snapname, void *arg)
3498 return (zfs_unmount_snap(snapname));
3502 * When a clone is destroyed, its origin may also need to be destroyed,
3503 * in which case it must be unmounted. This routine will do that unmount
3507 zfs_destroy_unmount_origin(const char *fsname)
3513 error = dmu_objset_hold(fsname, FTAG, &os);
3516 ds = dmu_objset_ds(os);
3517 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
3518 char originname[MAXNAMELEN];
3519 dsl_dataset_name(ds->ds_prev, originname);
3520 dmu_objset_rele(os, FTAG);
3521 (void) zfs_unmount_snap(originname);
3523 dmu_objset_rele(os, FTAG);
3529 * "snaps" -> { snapshot1, snapshot2 }
3530 * (optional boolean) "defer"
3533 * outnvl: snapshot -> error code (int32)
3538 zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3545 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3546 return (SET_ERROR(EINVAL));
3547 defer = nvlist_exists(innvl, "defer");
3549 poollen = strlen(poolname);
3550 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3551 pair = nvlist_next_nvpair(snaps, pair)) {
3552 const char *name = nvpair_name(pair);
3555 * The snap must be in the specified pool to prevent the
3556 * invalid removal of zvol minors below.
3558 if (strncmp(name, poolname, poollen) != 0 ||
3559 (name[poollen] != '/' && name[poollen] != '@'))
3560 return (SET_ERROR(EXDEV));
3562 error = zfs_unmount_snap(name);
3565 #if defined(__FreeBSD__)
3566 zvol_remove_minors(name);
3570 return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
3574 * Create bookmarks. Bookmark names are of the form <fs>#<bmark>.
3575 * All bookmarks must be in the same pool.
3578 * bookmark1 -> snapshot1, bookmark2 -> snapshot2
3581 * outnvl: bookmark -> error code (int32)
3586 zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3588 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3589 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3593 * Verify the snapshot argument.
3595 if (nvpair_value_string(pair, &snap_name) != 0)
3596 return (SET_ERROR(EINVAL));
3599 /* Verify that the keys (bookmarks) are unique */
3600 for (nvpair_t *pair2 = nvlist_next_nvpair(innvl, pair);
3601 pair2 != NULL; pair2 = nvlist_next_nvpair(innvl, pair2)) {
3602 if (strcmp(nvpair_name(pair), nvpair_name(pair2)) == 0)
3603 return (SET_ERROR(EINVAL));
3607 return (dsl_bookmark_create(innvl, outnvl));
3612 * property 1, property 2, ...
3616 * bookmark name 1 -> { property 1, property 2, ... },
3617 * bookmark name 2 -> { property 1, property 2, ... }
3622 zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3624 return (dsl_get_bookmarks(fsname, innvl, outnvl));
3629 * bookmark name 1, bookmark name 2
3632 * outnvl: bookmark -> error code (int32)
3636 zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl,
3641 poollen = strlen(poolname);
3642 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3643 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3644 const char *name = nvpair_name(pair);
3645 const char *cp = strchr(name, '#');
3648 * The bookmark name must contain an #, and the part after it
3649 * must contain only valid characters.
3652 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3653 return (SET_ERROR(EINVAL));
3656 * The bookmark must be in the specified pool.
3658 if (strncmp(name, poolname, poollen) != 0 ||
3659 (name[poollen] != '/' && name[poollen] != '#'))
3660 return (SET_ERROR(EXDEV));
3663 error = dsl_bookmark_destroy(innvl, outnvl);
3669 * zc_name name of dataset to destroy
3670 * zc_objset_type type of objset
3671 * zc_defer_destroy mark for deferred destroy
3676 zfs_ioc_destroy(zfs_cmd_t *zc)
3680 if (zc->zc_objset_type == DMU_OST_ZFS) {
3681 err = zfs_unmount_snap(zc->zc_name);
3686 if (strchr(zc->zc_name, '@'))
3687 err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
3689 err = dsl_destroy_head(zc->zc_name);
3690 if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
3692 zvol_remove_minors(zc->zc_name);
3694 (void) zvol_remove_minor(zc->zc_name);
3700 * fsname is name of dataset to rollback (to most recent snapshot)
3702 * innvl is not used.
3704 * outnvl: "target" -> name of most recent snapshot
3709 zfs_ioc_rollback(const char *fsname, nvlist_t *args, nvlist_t *outnvl)
3714 if (getzfsvfs(fsname, &zfsvfs) == 0) {
3715 error = zfs_suspend_fs(zfsvfs);
3719 error = dsl_dataset_rollback(fsname, zfsvfs, outnvl);
3720 resume_err = zfs_resume_fs(zfsvfs, fsname);
3721 error = error ? error : resume_err;
3723 VFS_RELE(zfsvfs->z_vfs);
3725 error = dsl_dataset_rollback(fsname, NULL, outnvl);
3731 recursive_unmount(const char *fsname, void *arg)
3733 const char *snapname = arg;
3734 char fullname[MAXNAMELEN];
3736 (void) snprintf(fullname, sizeof (fullname), "%s@%s", fsname, snapname);
3737 return (zfs_unmount_snap(fullname));
3742 * zc_name old name of dataset
3743 * zc_value new name of dataset
3744 * zc_cookie recursive flag (only valid for snapshots)
3749 zfs_ioc_rename(zfs_cmd_t *zc)
3751 boolean_t recursive = zc->zc_cookie & 1;
3753 boolean_t allow_mounted = B_TRUE;
3756 allow_mounted = (zc->zc_cookie & 2) != 0;
3759 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3760 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3761 strchr(zc->zc_value, '%'))
3762 return (SET_ERROR(EINVAL));
3764 at = strchr(zc->zc_name, '@');
3766 /* snaps must be in same fs */
3769 if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
3770 return (SET_ERROR(EXDEV));
3772 if (zc->zc_objset_type == DMU_OST_ZFS && allow_mounted) {
3773 error = dmu_objset_find(zc->zc_name,
3774 recursive_unmount, at + 1,
3775 recursive ? DS_FIND_CHILDREN : 0);
3781 error = dsl_dataset_rename_snapshot(zc->zc_name,
3782 at + 1, strchr(zc->zc_value, '@') + 1, recursive);
3788 if (zc->zc_objset_type == DMU_OST_ZVOL)
3789 (void) zvol_remove_minor(zc->zc_name);
3791 return (dsl_dir_rename(zc->zc_name, zc->zc_value));
3796 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
3798 const char *propname = nvpair_name(pair);
3799 boolean_t issnap = (strchr(dsname, '@') != NULL);
3800 zfs_prop_t prop = zfs_name_to_prop(propname);
3804 if (prop == ZPROP_INVAL) {
3805 if (zfs_prop_user(propname)) {
3806 if (err = zfs_secpolicy_write_perms(dsname,
3807 ZFS_DELEG_PERM_USERPROP, cr))
3812 if (!issnap && zfs_prop_userquota(propname)) {
3813 const char *perm = NULL;
3814 const char *uq_prefix =
3815 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
3816 const char *gq_prefix =
3817 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
3819 if (strncmp(propname, uq_prefix,
3820 strlen(uq_prefix)) == 0) {
3821 perm = ZFS_DELEG_PERM_USERQUOTA;
3822 } else if (strncmp(propname, gq_prefix,
3823 strlen(gq_prefix)) == 0) {
3824 perm = ZFS_DELEG_PERM_GROUPQUOTA;
3826 /* USERUSED and GROUPUSED are read-only */
3827 return (SET_ERROR(EINVAL));
3830 if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
3835 return (SET_ERROR(EINVAL));
3839 return (SET_ERROR(EINVAL));
3841 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
3843 * dsl_prop_get_all_impl() returns properties in this
3847 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
3848 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3853 * Check that this value is valid for this pool version
3856 case ZFS_PROP_COMPRESSION:
3858 * If the user specified gzip compression, make sure
3859 * the SPA supports it. We ignore any errors here since
3860 * we'll catch them later.
3862 if (nvpair_value_uint64(pair, &intval) == 0) {
3863 if (intval >= ZIO_COMPRESS_GZIP_1 &&
3864 intval <= ZIO_COMPRESS_GZIP_9 &&
3865 zfs_earlier_version(dsname,
3866 SPA_VERSION_GZIP_COMPRESSION)) {
3867 return (SET_ERROR(ENOTSUP));
3870 if (intval == ZIO_COMPRESS_ZLE &&
3871 zfs_earlier_version(dsname,
3872 SPA_VERSION_ZLE_COMPRESSION))
3873 return (SET_ERROR(ENOTSUP));
3875 if (intval == ZIO_COMPRESS_LZ4) {
3878 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3881 if (!spa_feature_is_enabled(spa,
3882 SPA_FEATURE_LZ4_COMPRESS)) {
3883 spa_close(spa, FTAG);
3884 return (SET_ERROR(ENOTSUP));
3886 spa_close(spa, FTAG);
3890 * If this is a bootable dataset then
3891 * verify that the compression algorithm
3892 * is supported for booting. We must return
3893 * something other than ENOTSUP since it
3894 * implies a downrev pool version.
3896 if (zfs_is_bootfs(dsname) &&
3897 !BOOTFS_COMPRESS_VALID(intval)) {
3898 return (SET_ERROR(ERANGE));
3903 case ZFS_PROP_COPIES:
3904 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
3905 return (SET_ERROR(ENOTSUP));
3908 case ZFS_PROP_RECORDSIZE:
3909 /* Record sizes above 128k need the feature to be enabled */
3910 if (nvpair_value_uint64(pair, &intval) == 0 &&
3911 intval > SPA_OLD_MAXBLOCKSIZE) {
3915 * If this is a bootable dataset then
3916 * the we don't allow large (>128K) blocks,
3917 * because GRUB doesn't support them.
3919 if (zfs_is_bootfs(dsname) &&
3920 intval > SPA_OLD_MAXBLOCKSIZE) {
3921 return (SET_ERROR(ERANGE));
3925 * We don't allow setting the property above 1MB,
3926 * unless the tunable has been changed.
3928 if (intval > zfs_max_recordsize ||
3929 intval > SPA_MAXBLOCKSIZE)
3930 return (SET_ERROR(ERANGE));
3932 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3935 if (!spa_feature_is_enabled(spa,
3936 SPA_FEATURE_LARGE_BLOCKS)) {
3937 spa_close(spa, FTAG);
3938 return (SET_ERROR(ENOTSUP));
3940 spa_close(spa, FTAG);
3944 case ZFS_PROP_SHARESMB:
3945 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
3946 return (SET_ERROR(ENOTSUP));
3949 case ZFS_PROP_ACLINHERIT:
3950 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3951 nvpair_value_uint64(pair, &intval) == 0) {
3952 if (intval == ZFS_ACL_PASSTHROUGH_X &&
3953 zfs_earlier_version(dsname,
3954 SPA_VERSION_PASSTHROUGH_X))
3955 return (SET_ERROR(ENOTSUP));
3959 case ZFS_PROP_CHECKSUM:
3960 case ZFS_PROP_DEDUP:
3962 spa_feature_t feature;
3965 /* dedup feature version checks */
3966 if (prop == ZFS_PROP_DEDUP &&
3967 zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
3968 return (SET_ERROR(ENOTSUP));
3970 if (nvpair_value_uint64(pair, &intval) != 0)
3971 return (SET_ERROR(EINVAL));
3973 /* check prop value is enabled in features */
3974 feature = zio_checksum_to_feature(intval);
3975 if (feature == SPA_FEATURE_NONE)
3978 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3981 * Salted checksums are not supported on root pools.
3983 if (spa_bootfs(spa) != 0 &&
3984 intval < ZIO_CHECKSUM_FUNCTIONS &&
3985 (zio_checksum_table[intval].ci_flags &
3986 ZCHECKSUM_FLAG_SALTED)) {
3987 spa_close(spa, FTAG);
3988 return (SET_ERROR(ERANGE));
3990 if (!spa_feature_is_enabled(spa, feature)) {
3991 spa_close(spa, FTAG);
3992 return (SET_ERROR(ENOTSUP));
3994 spa_close(spa, FTAG);
3999 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
4003 * Checks for a race condition to make sure we don't increment a feature flag
4007 zfs_prop_activate_feature_check(void *arg, dmu_tx_t *tx)
4009 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
4010 spa_feature_t *featurep = arg;
4012 if (!spa_feature_is_active(spa, *featurep))
4015 return (SET_ERROR(EBUSY));
4019 * The callback invoked on feature activation in the sync task caused by
4020 * zfs_prop_activate_feature.
4023 zfs_prop_activate_feature_sync(void *arg, dmu_tx_t *tx)
4025 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
4026 spa_feature_t *featurep = arg;
4028 spa_feature_incr(spa, *featurep, tx);
4032 * Activates a feature on a pool in response to a property setting. This
4033 * creates a new sync task which modifies the pool to reflect the feature
4037 zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature)
4041 /* EBUSY here indicates that the feature is already active */
4042 err = dsl_sync_task(spa_name(spa),
4043 zfs_prop_activate_feature_check, zfs_prop_activate_feature_sync,
4044 &feature, 2, ZFS_SPACE_CHECK_RESERVED);
4046 if (err != 0 && err != EBUSY)
4053 * Removes properties from the given props list that fail permission checks
4054 * needed to clear them and to restore them in case of a receive error. For each
4055 * property, make sure we have both set and inherit permissions.
4057 * Returns the first error encountered if any permission checks fail. If the
4058 * caller provides a non-NULL errlist, it also gives the complete list of names
4059 * of all the properties that failed a permission check along with the
4060 * corresponding error numbers. The caller is responsible for freeing the
4063 * If every property checks out successfully, zero is returned and the list
4064 * pointed at by errlist is NULL.
4067 zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
4070 nvpair_t *pair, *next_pair;
4077 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4079 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
4080 (void) strcpy(zc->zc_name, dataset);
4081 pair = nvlist_next_nvpair(props, NULL);
4082 while (pair != NULL) {
4083 next_pair = nvlist_next_nvpair(props, pair);
4085 (void) strcpy(zc->zc_value, nvpair_name(pair));
4086 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
4087 (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
4088 VERIFY(nvlist_remove_nvpair(props, pair) == 0);
4089 VERIFY(nvlist_add_int32(errors,
4090 zc->zc_value, err) == 0);
4094 kmem_free(zc, sizeof (zfs_cmd_t));
4096 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
4097 nvlist_free(errors);
4100 VERIFY(nvpair_value_int32(pair, &rv) == 0);
4103 if (errlist == NULL)
4104 nvlist_free(errors);
4112 propval_equals(nvpair_t *p1, nvpair_t *p2)
4114 if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
4115 /* dsl_prop_get_all_impl() format */
4117 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
4118 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4122 if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
4124 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
4125 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4129 if (nvpair_type(p1) != nvpair_type(p2))
4132 if (nvpair_type(p1) == DATA_TYPE_STRING) {
4133 char *valstr1, *valstr2;
4135 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
4136 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
4137 return (strcmp(valstr1, valstr2) == 0);
4139 uint64_t intval1, intval2;
4141 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
4142 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
4143 return (intval1 == intval2);
4148 * Remove properties from props if they are not going to change (as determined
4149 * by comparison with origprops). Remove them from origprops as well, since we
4150 * do not need to clear or restore properties that won't change.
4153 props_reduce(nvlist_t *props, nvlist_t *origprops)
4155 nvpair_t *pair, *next_pair;
4157 if (origprops == NULL)
4158 return; /* all props need to be received */
4160 pair = nvlist_next_nvpair(props, NULL);
4161 while (pair != NULL) {
4162 const char *propname = nvpair_name(pair);
4165 next_pair = nvlist_next_nvpair(props, pair);
4167 if ((nvlist_lookup_nvpair(origprops, propname,
4168 &match) != 0) || !propval_equals(pair, match))
4169 goto next; /* need to set received value */
4171 /* don't clear the existing received value */
4172 (void) nvlist_remove_nvpair(origprops, match);
4173 /* don't bother receiving the property */
4174 (void) nvlist_remove_nvpair(props, pair);
4181 * Extract properties that cannot be set PRIOR to the receipt of a dataset.
4182 * For example, refquota cannot be set until after the receipt of a dataset,
4183 * because in replication streams, an older/earlier snapshot may exceed the
4184 * refquota. We want to receive the older/earlier snapshot, but setting
4185 * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent
4186 * the older/earlier snapshot from being received (with EDQUOT).
4188 * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario.
4190 * libzfs will need to be judicious handling errors encountered by props
4191 * extracted by this function.
4194 extract_delay_props(nvlist_t *props)
4196 nvlist_t *delayprops;
4197 nvpair_t *nvp, *tmp;
4198 static const zfs_prop_t delayable[] = { ZFS_PROP_REFQUOTA, 0 };
4201 VERIFY(nvlist_alloc(&delayprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4203 for (nvp = nvlist_next_nvpair(props, NULL); nvp != NULL;
4204 nvp = nvlist_next_nvpair(props, nvp)) {
4206 * strcmp() is safe because zfs_prop_to_name() always returns
4209 for (i = 0; delayable[i] != 0; i++) {
4210 if (strcmp(zfs_prop_to_name(delayable[i]),
4211 nvpair_name(nvp)) == 0) {
4215 if (delayable[i] != 0) {
4216 tmp = nvlist_prev_nvpair(props, nvp);
4217 VERIFY(nvlist_add_nvpair(delayprops, nvp) == 0);
4218 VERIFY(nvlist_remove_nvpair(props, nvp) == 0);
4223 if (nvlist_empty(delayprops)) {
4224 nvlist_free(delayprops);
4227 return (delayprops);
4231 static boolean_t zfs_ioc_recv_inject_err;
4236 * zc_name name of containing filesystem
4237 * zc_nvlist_src{_size} nvlist of properties to apply
4238 * zc_value name of snapshot to create
4239 * zc_string name of clone origin (if DRR_FLAG_CLONE)
4240 * zc_cookie file descriptor to recv from
4241 * zc_begin_record the BEGIN record of the stream (not byteswapped)
4242 * zc_guid force flag
4243 * zc_cleanup_fd cleanup-on-exit file descriptor
4244 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
4245 * zc_resumable if data is incomplete assume sender will resume
4248 * zc_cookie number of bytes read
4249 * zc_nvlist_dst{_size} error for each unapplied received property
4250 * zc_obj zprop_errflags_t
4251 * zc_action_handle handle for this guid/ds mapping
4254 zfs_ioc_recv(zfs_cmd_t *zc)
4257 dmu_recv_cookie_t drc;
4258 boolean_t force = (boolean_t)zc->zc_guid;
4261 int props_error = 0;
4264 nvlist_t *props = NULL; /* sent properties */
4265 nvlist_t *origprops = NULL; /* existing properties */
4266 nvlist_t *delayprops = NULL; /* sent properties applied post-receive */
4267 char *origin = NULL;
4269 char tofs[ZFS_MAXNAMELEN];
4270 cap_rights_t rights;
4271 boolean_t first_recvd_props = B_FALSE;
4273 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4274 strchr(zc->zc_value, '@') == NULL ||
4275 strchr(zc->zc_value, '%'))
4276 return (SET_ERROR(EINVAL));
4278 (void) strcpy(tofs, zc->zc_value);
4279 tosnap = strchr(tofs, '@');
4282 if (zc->zc_nvlist_src != 0 &&
4283 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
4284 zc->zc_iflags, &props)) != 0)
4291 fget_read(curthread, fd, cap_rights_init(&rights, CAP_PREAD), &fp);
4295 return (SET_ERROR(EBADF));
4298 errors = fnvlist_alloc();
4300 if (zc->zc_string[0])
4301 origin = zc->zc_string;
4303 error = dmu_recv_begin(tofs, tosnap,
4304 &zc->zc_begin_record, force, zc->zc_resumable, origin, &drc);
4309 * Set properties before we receive the stream so that they are applied
4310 * to the new data. Note that we must call dmu_recv_stream() if
4311 * dmu_recv_begin() succeeds.
4313 if (props != NULL && !drc.drc_newfs) {
4314 if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
4315 SPA_VERSION_RECVD_PROPS &&
4316 !dsl_prop_get_hasrecvd(tofs))
4317 first_recvd_props = B_TRUE;
4320 * If new received properties are supplied, they are to
4321 * completely replace the existing received properties, so stash
4322 * away the existing ones.
4324 if (dsl_prop_get_received(tofs, &origprops) == 0) {
4325 nvlist_t *errlist = NULL;
4327 * Don't bother writing a property if its value won't
4328 * change (and avoid the unnecessary security checks).
4330 * The first receive after SPA_VERSION_RECVD_PROPS is a
4331 * special case where we blow away all local properties
4334 if (!first_recvd_props)
4335 props_reduce(props, origprops);
4336 if (zfs_check_clearable(tofs, origprops, &errlist) != 0)
4337 (void) nvlist_merge(errors, errlist, 0);
4338 nvlist_free(errlist);
4340 if (clear_received_props(tofs, origprops,
4341 first_recvd_props ? NULL : props) != 0)
4342 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4344 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4348 if (props != NULL) {
4349 props_error = dsl_prop_set_hasrecvd(tofs);
4351 if (props_error == 0) {
4352 delayprops = extract_delay_props(props);
4353 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4359 error = dmu_recv_stream(&drc, fp, &off, zc->zc_cleanup_fd,
4360 &zc->zc_action_handle);
4363 zfsvfs_t *zfsvfs = NULL;
4365 if (getzfsvfs(tofs, &zfsvfs) == 0) {
4369 error = zfs_suspend_fs(zfsvfs);
4371 * If the suspend fails, then the recv_end will
4372 * likely also fail, and clean up after itself.
4374 end_err = dmu_recv_end(&drc, zfsvfs);
4376 error = zfs_resume_fs(zfsvfs, tofs);
4377 error = error ? error : end_err;
4378 VFS_RELE(zfsvfs->z_vfs);
4380 error = dmu_recv_end(&drc, NULL);
4383 /* Set delayed properties now, after we're done receiving. */
4384 if (delayprops != NULL && error == 0) {
4385 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4386 delayprops, errors);
4390 if (delayprops != NULL) {
4392 * Merge delayed props back in with initial props, in case
4393 * we're DEBUG and zfs_ioc_recv_inject_err is set (which means
4394 * we have to make sure clear_received_props() includes
4395 * the delayed properties).
4397 * Since zfs_ioc_recv_inject_err is only in DEBUG kernels,
4398 * using ASSERT() will be just like a VERIFY.
4400 ASSERT(nvlist_merge(props, delayprops, 0) == 0);
4401 nvlist_free(delayprops);
4405 * Now that all props, initial and delayed, are set, report the prop
4406 * errors to the caller.
4408 if (zc->zc_nvlist_dst_size != 0 &&
4409 (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
4410 put_nvlist(zc, errors) != 0)) {
4412 * Caller made zc->zc_nvlist_dst less than the minimum expected
4413 * size or supplied an invalid address.
4415 props_error = SET_ERROR(EINVAL);
4418 zc->zc_cookie = off - fp->f_offset;
4419 if (off >= 0 && off <= MAXOFFSET_T)
4423 if (zfs_ioc_recv_inject_err) {
4424 zfs_ioc_recv_inject_err = B_FALSE;
4431 zvol_create_minors(tofs);
4435 * On error, restore the original props.
4437 if (error != 0 && props != NULL && !drc.drc_newfs) {
4438 if (clear_received_props(tofs, props, NULL) != 0) {
4440 * We failed to clear the received properties.
4441 * Since we may have left a $recvd value on the
4442 * system, we can't clear the $hasrecvd flag.
4444 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4445 } else if (first_recvd_props) {
4446 dsl_prop_unset_hasrecvd(tofs);
4449 if (origprops == NULL && !drc.drc_newfs) {
4450 /* We failed to stash the original properties. */
4451 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4455 * dsl_props_set() will not convert RECEIVED to LOCAL on or
4456 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
4457 * explictly if we're restoring local properties cleared in the
4458 * first new-style receive.
4460 if (origprops != NULL &&
4461 zfs_set_prop_nvlist(tofs, (first_recvd_props ?
4462 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
4463 origprops, NULL) != 0) {
4465 * We stashed the original properties but failed to
4468 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4473 nvlist_free(origprops);
4474 nvlist_free(errors);
4478 error = props_error;
4485 * zc_name name of snapshot to send
4486 * zc_cookie file descriptor to send stream to
4487 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
4488 * zc_sendobj objsetid of snapshot to send
4489 * zc_fromobj objsetid of incremental fromsnap (may be zero)
4490 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
4491 * output size in zc_objset_type.
4492 * zc_flags lzc_send_flags
4495 * zc_objset_type estimated size, if zc_guid is set
4498 zfs_ioc_send(zfs_cmd_t *zc)
4502 boolean_t estimate = (zc->zc_guid != 0);
4503 boolean_t embedok = (zc->zc_flags & 0x1);
4504 boolean_t large_block_ok = (zc->zc_flags & 0x2);
4506 if (zc->zc_obj != 0) {
4508 dsl_dataset_t *tosnap;
4510 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4514 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
4516 dsl_pool_rele(dp, FTAG);
4520 if (dsl_dir_is_clone(tosnap->ds_dir))
4522 dsl_dir_phys(tosnap->ds_dir)->dd_origin_obj;
4523 dsl_dataset_rele(tosnap, FTAG);
4524 dsl_pool_rele(dp, FTAG);
4529 dsl_dataset_t *tosnap;
4530 dsl_dataset_t *fromsnap = NULL;
4532 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4536 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
4538 dsl_pool_rele(dp, FTAG);
4542 if (zc->zc_fromobj != 0) {
4543 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
4546 dsl_dataset_rele(tosnap, FTAG);
4547 dsl_pool_rele(dp, FTAG);
4552 error = dmu_send_estimate(tosnap, fromsnap,
4553 &zc->zc_objset_type);
4555 if (fromsnap != NULL)
4556 dsl_dataset_rele(fromsnap, FTAG);
4557 dsl_dataset_rele(tosnap, FTAG);
4558 dsl_pool_rele(dp, FTAG);
4561 cap_rights_t rights;
4564 fp = getf(zc->zc_cookie);
4566 fget_write(curthread, zc->zc_cookie,
4567 cap_rights_init(&rights, CAP_WRITE), &fp);
4570 return (SET_ERROR(EBADF));
4573 error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
4574 zc->zc_fromobj, embedok, large_block_ok,
4576 zc->zc_cookie, fp->f_vnode, &off);
4578 zc->zc_cookie, fp, &off);
4581 if (off >= 0 && off <= MAXOFFSET_T)
4583 releasef(zc->zc_cookie);
4590 * zc_name name of snapshot on which to report progress
4591 * zc_cookie file descriptor of send stream
4594 * zc_cookie number of bytes written in send stream thus far
4597 zfs_ioc_send_progress(zfs_cmd_t *zc)
4601 dmu_sendarg_t *dsp = NULL;
4604 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4608 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
4610 dsl_pool_rele(dp, FTAG);
4614 mutex_enter(&ds->ds_sendstream_lock);
4617 * Iterate over all the send streams currently active on this dataset.
4618 * If there's one which matches the specified file descriptor _and_ the
4619 * stream was started by the current process, return the progress of
4622 for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
4623 dsp = list_next(&ds->ds_sendstreams, dsp)) {
4624 if (dsp->dsa_outfd == zc->zc_cookie &&
4625 dsp->dsa_proc == curproc)
4630 zc->zc_cookie = *(dsp->dsa_off);
4632 error = SET_ERROR(ENOENT);
4634 mutex_exit(&ds->ds_sendstream_lock);
4635 dsl_dataset_rele(ds, FTAG);
4636 dsl_pool_rele(dp, FTAG);
4641 zfs_ioc_inject_fault(zfs_cmd_t *zc)
4645 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
4646 &zc->zc_inject_record);
4649 zc->zc_guid = (uint64_t)id;
4655 zfs_ioc_clear_fault(zfs_cmd_t *zc)
4657 return (zio_clear_fault((int)zc->zc_guid));
4661 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
4663 int id = (int)zc->zc_guid;
4666 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
4667 &zc->zc_inject_record);
4675 zfs_ioc_error_log(zfs_cmd_t *zc)
4679 size_t count = (size_t)zc->zc_nvlist_dst_size;
4681 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
4684 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
4687 zc->zc_nvlist_dst_size = count;
4689 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
4691 spa_close(spa, FTAG);
4697 zfs_ioc_clear(zfs_cmd_t *zc)
4704 * On zpool clear we also fix up missing slogs
4706 mutex_enter(&spa_namespace_lock);
4707 spa = spa_lookup(zc->zc_name);
4709 mutex_exit(&spa_namespace_lock);
4710 return (SET_ERROR(EIO));
4712 if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
4713 /* we need to let spa_open/spa_load clear the chains */
4714 spa_set_log_state(spa, SPA_LOG_CLEAR);
4716 spa->spa_last_open_failed = 0;
4717 mutex_exit(&spa_namespace_lock);
4719 if (zc->zc_cookie & ZPOOL_NO_REWIND) {
4720 error = spa_open(zc->zc_name, &spa, FTAG);
4723 nvlist_t *config = NULL;
4725 if (zc->zc_nvlist_src == 0)
4726 return (SET_ERROR(EINVAL));
4728 if ((error = get_nvlist(zc->zc_nvlist_src,
4729 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
4730 error = spa_open_rewind(zc->zc_name, &spa, FTAG,
4732 if (config != NULL) {
4735 if ((err = put_nvlist(zc, config)) != 0)
4737 nvlist_free(config);
4739 nvlist_free(policy);
4746 spa_vdev_state_enter(spa, SCL_NONE);
4748 if (zc->zc_guid == 0) {
4751 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
4753 (void) spa_vdev_state_exit(spa, NULL, ENODEV);
4754 spa_close(spa, FTAG);
4755 return (SET_ERROR(ENODEV));
4759 vdev_clear(spa, vd);
4761 (void) spa_vdev_state_exit(spa, NULL, 0);
4764 * Resume any suspended I/Os.
4766 if (zio_resume(spa) != 0)
4767 error = SET_ERROR(EIO);
4769 spa_close(spa, FTAG);
4775 zfs_ioc_pool_reopen(zfs_cmd_t *zc)
4780 error = spa_open(zc->zc_name, &spa, FTAG);
4784 spa_vdev_state_enter(spa, SCL_NONE);
4787 * If a resilver is already in progress then set the
4788 * spa_scrub_reopen flag to B_TRUE so that we don't restart
4789 * the scan as a side effect of the reopen. Otherwise, let
4790 * vdev_open() decided if a resilver is required.
4792 spa->spa_scrub_reopen = dsl_scan_resilvering(spa->spa_dsl_pool);
4793 vdev_reopen(spa->spa_root_vdev);
4794 spa->spa_scrub_reopen = B_FALSE;
4796 (void) spa_vdev_state_exit(spa, NULL, 0);
4797 spa_close(spa, FTAG);
4802 * zc_name name of filesystem
4803 * zc_value name of origin snapshot
4806 * zc_string name of conflicting snapshot, if there is one
4809 zfs_ioc_promote(zfs_cmd_t *zc)
4814 * We don't need to unmount *all* the origin fs's snapshots, but
4817 cp = strchr(zc->zc_value, '@');
4820 (void) dmu_objset_find(zc->zc_value,
4821 zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
4822 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
4826 * Retrieve a single {user|group}{used|quota}@... property.
4829 * zc_name name of filesystem
4830 * zc_objset_type zfs_userquota_prop_t
4831 * zc_value domain name (eg. "S-1-234-567-89")
4832 * zc_guid RID/UID/GID
4835 * zc_cookie property value
4838 zfs_ioc_userspace_one(zfs_cmd_t *zc)
4843 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
4844 return (SET_ERROR(EINVAL));
4846 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4850 error = zfs_userspace_one(zfsvfs,
4851 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
4852 zfsvfs_rele(zfsvfs, FTAG);
4859 * zc_name name of filesystem
4860 * zc_cookie zap cursor
4861 * zc_objset_type zfs_userquota_prop_t
4862 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
4865 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
4866 * zc_cookie zap cursor
4869 zfs_ioc_userspace_many(zfs_cmd_t *zc)
4872 int bufsize = zc->zc_nvlist_dst_size;
4875 return (SET_ERROR(ENOMEM));
4877 int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4881 void *buf = kmem_alloc(bufsize, KM_SLEEP);
4883 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
4884 buf, &zc->zc_nvlist_dst_size);
4887 error = ddi_copyout(buf,
4888 (void *)(uintptr_t)zc->zc_nvlist_dst,
4889 zc->zc_nvlist_dst_size, zc->zc_iflags);
4891 kmem_free(buf, bufsize);
4892 zfsvfs_rele(zfsvfs, FTAG);
4899 * zc_name name of filesystem
4905 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
4911 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
4912 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
4914 * If userused is not enabled, it may be because the
4915 * objset needs to be closed & reopened (to grow the
4916 * objset_phys_t). Suspend/resume the fs will do that.
4918 error = zfs_suspend_fs(zfsvfs);
4920 dmu_objset_refresh_ownership(zfsvfs->z_os,
4922 error = zfs_resume_fs(zfsvfs, zc->zc_name);
4926 error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
4927 VFS_RELE(zfsvfs->z_vfs);
4929 /* XXX kind of reading contents without owning */
4930 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4934 error = dmu_objset_userspace_upgrade(os);
4935 dmu_objset_rele(os, FTAG);
4943 * We don't want to have a hard dependency
4944 * against some special symbols in sharefs
4945 * nfs, and smbsrv. Determine them if needed when
4946 * the first file system is shared.
4947 * Neither sharefs, nfs or smbsrv are unloadable modules.
4949 int (*znfsexport_fs)(void *arg);
4950 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
4951 int (*zsmbexport_fs)(void *arg, boolean_t add_share);
4953 int zfs_nfsshare_inited;
4954 int zfs_smbshare_inited;
4956 ddi_modhandle_t nfs_mod;
4957 ddi_modhandle_t sharefs_mod;
4958 ddi_modhandle_t smbsrv_mod;
4959 #endif /* illumos */
4960 kmutex_t zfs_share_lock;
4968 ASSERT(MUTEX_HELD(&zfs_share_lock));
4969 /* Both NFS and SMB shares also require sharetab support. */
4970 if (sharefs_mod == NULL && ((sharefs_mod =
4971 ddi_modopen("fs/sharefs",
4972 KRTLD_MODE_FIRST, &error)) == NULL)) {
4973 return (SET_ERROR(ENOSYS));
4975 if (zshare_fs == NULL && ((zshare_fs =
4976 (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
4977 ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
4978 return (SET_ERROR(ENOSYS));
4982 #endif /* illumos */
4985 zfs_ioc_share(zfs_cmd_t *zc)
4991 switch (zc->zc_share.z_sharetype) {
4993 case ZFS_UNSHARE_NFS:
4994 if (zfs_nfsshare_inited == 0) {
4995 mutex_enter(&zfs_share_lock);
4996 if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
4997 KRTLD_MODE_FIRST, &error)) == NULL)) {
4998 mutex_exit(&zfs_share_lock);
4999 return (SET_ERROR(ENOSYS));
5001 if (znfsexport_fs == NULL &&
5002 ((znfsexport_fs = (int (*)(void *))
5004 "nfs_export", &error)) == NULL)) {
5005 mutex_exit(&zfs_share_lock);
5006 return (SET_ERROR(ENOSYS));
5008 error = zfs_init_sharefs();
5010 mutex_exit(&zfs_share_lock);
5011 return (SET_ERROR(ENOSYS));
5013 zfs_nfsshare_inited = 1;
5014 mutex_exit(&zfs_share_lock);
5018 case ZFS_UNSHARE_SMB:
5019 if (zfs_smbshare_inited == 0) {
5020 mutex_enter(&zfs_share_lock);
5021 if (smbsrv_mod == NULL && ((smbsrv_mod =
5022 ddi_modopen("drv/smbsrv",
5023 KRTLD_MODE_FIRST, &error)) == NULL)) {
5024 mutex_exit(&zfs_share_lock);
5025 return (SET_ERROR(ENOSYS));
5027 if (zsmbexport_fs == NULL && ((zsmbexport_fs =
5028 (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
5029 "smb_server_share", &error)) == NULL)) {
5030 mutex_exit(&zfs_share_lock);
5031 return (SET_ERROR(ENOSYS));
5033 error = zfs_init_sharefs();
5035 mutex_exit(&zfs_share_lock);
5036 return (SET_ERROR(ENOSYS));
5038 zfs_smbshare_inited = 1;
5039 mutex_exit(&zfs_share_lock);
5043 return (SET_ERROR(EINVAL));
5046 switch (zc->zc_share.z_sharetype) {
5048 case ZFS_UNSHARE_NFS:
5050 znfsexport_fs((void *)
5051 (uintptr_t)zc->zc_share.z_exportdata))
5055 case ZFS_UNSHARE_SMB:
5056 if (error = zsmbexport_fs((void *)
5057 (uintptr_t)zc->zc_share.z_exportdata,
5058 zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
5065 opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
5066 zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
5067 SHAREFS_ADD : SHAREFS_REMOVE;
5070 * Add or remove share from sharetab
5072 error = zshare_fs(opcode,
5073 (void *)(uintptr_t)zc->zc_share.z_sharedata,
5074 zc->zc_share.z_sharemax);
5078 #else /* !illumos */
5080 #endif /* illumos */
5083 ace_t full_access[] = {
5084 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
5089 * zc_name name of containing filesystem
5090 * zc_obj object # beyond which we want next in-use object #
5093 * zc_obj next in-use object #
5096 zfs_ioc_next_obj(zfs_cmd_t *zc)
5098 objset_t *os = NULL;
5101 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
5105 error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
5106 dsl_dataset_phys(os->os_dsl_dataset)->ds_prev_snap_txg);
5108 dmu_objset_rele(os, FTAG);
5114 * zc_name name of filesystem
5115 * zc_value prefix name for snapshot
5116 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
5119 * zc_value short name of new snapshot
5122 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
5129 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
5133 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
5134 (u_longlong_t)ddi_get_lbolt64());
5135 hold_name = kmem_asprintf("%%%s", zc->zc_value);
5137 error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
5140 (void) strcpy(zc->zc_value, snap_name);
5143 zfs_onexit_fd_rele(zc->zc_cleanup_fd);
5149 * zc_name name of "to" snapshot
5150 * zc_value name of "from" snapshot
5151 * zc_cookie file descriptor to write diff data on
5154 * dmu_diff_record_t's to the file descriptor
5157 zfs_ioc_diff(zfs_cmd_t *zc)
5160 cap_rights_t rights;
5165 fp = getf(zc->zc_cookie);
5167 fget_write(curthread, zc->zc_cookie,
5168 cap_rights_init(&rights, CAP_WRITE), &fp);
5171 return (SET_ERROR(EBADF));
5176 error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off);
5178 error = dmu_diff(zc->zc_name, zc->zc_value, fp, &off);
5181 if (off >= 0 && off <= MAXOFFSET_T)
5183 releasef(zc->zc_cookie);
5190 * Remove all ACL files in shares dir
5193 zfs_smb_acl_purge(znode_t *dzp)
5196 zap_attribute_t zap;
5197 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
5200 for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
5201 (error = zap_cursor_retrieve(&zc, &zap)) == 0;
5202 zap_cursor_advance(&zc)) {
5203 if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
5207 zap_cursor_fini(&zc);
5210 #endif /* illumos */
5213 zfs_ioc_smb_acl(zfs_cmd_t *zc)
5218 vnode_t *resourcevp = NULL;
5227 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
5228 NO_FOLLOW, NULL, &vp)) != 0)
5231 /* Now make sure mntpnt and dataset are ZFS */
5233 if (strcmp(vp->v_vfsp->mnt_stat.f_fstypename, "zfs") != 0 ||
5234 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
5235 zc->zc_name) != 0)) {
5237 return (SET_ERROR(EINVAL));
5241 zfsvfs = dzp->z_zfsvfs;
5245 * Create share dir if its missing.
5247 mutex_enter(&zfsvfs->z_lock);
5248 if (zfsvfs->z_shares_dir == 0) {
5251 tx = dmu_tx_create(zfsvfs->z_os);
5252 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
5254 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
5255 error = dmu_tx_assign(tx, TXG_WAIT);
5259 error = zfs_create_share_dir(zfsvfs, tx);
5263 mutex_exit(&zfsvfs->z_lock);
5269 mutex_exit(&zfsvfs->z_lock);
5271 ASSERT(zfsvfs->z_shares_dir);
5272 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
5278 switch (zc->zc_cookie) {
5279 case ZFS_SMB_ACL_ADD:
5280 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
5281 vattr.va_type = VREG;
5282 vattr.va_mode = S_IFREG|0777;
5286 vsec.vsa_mask = VSA_ACE;
5287 vsec.vsa_aclentp = &full_access;
5288 vsec.vsa_aclentsz = sizeof (full_access);
5289 vsec.vsa_aclcnt = 1;
5291 error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
5292 &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
5294 VN_RELE(resourcevp);
5297 case ZFS_SMB_ACL_REMOVE:
5298 error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
5302 case ZFS_SMB_ACL_RENAME:
5303 if ((error = get_nvlist(zc->zc_nvlist_src,
5304 zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
5306 VN_RELE(ZTOV(sharedir));
5310 if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
5311 nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
5314 VN_RELE(ZTOV(sharedir));
5316 nvlist_free(nvlist);
5319 error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
5321 nvlist_free(nvlist);
5324 case ZFS_SMB_ACL_PURGE:
5325 error = zfs_smb_acl_purge(sharedir);
5329 error = SET_ERROR(EINVAL);
5334 VN_RELE(ZTOV(sharedir));
5339 #else /* !illumos */
5340 return (EOPNOTSUPP);
5341 #endif /* illumos */
5346 * "holds" -> { snapname -> holdname (string), ... }
5347 * (optional) "cleanup_fd" -> fd (int32)
5351 * snapname -> error value (int32)
5357 zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
5361 int cleanup_fd = -1;
5365 error = nvlist_lookup_nvlist(args, "holds", &holds);
5367 return (SET_ERROR(EINVAL));
5369 /* make sure the user didn't pass us any invalid (empty) tags */
5370 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
5371 pair = nvlist_next_nvpair(holds, pair)) {
5374 error = nvpair_value_string(pair, &htag);
5376 return (SET_ERROR(error));
5378 if (strlen(htag) == 0)
5379 return (SET_ERROR(EINVAL));
5382 if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
5383 error = zfs_onexit_fd_hold(cleanup_fd, &minor);
5388 error = dsl_dataset_user_hold(holds, minor, errlist);
5390 zfs_onexit_fd_rele(cleanup_fd);
5395 * innvl is not used.
5398 * holdname -> time added (uint64 seconds since epoch)
5404 zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
5406 return (dsl_dataset_get_holds(snapname, outnvl));
5411 * snapname -> { holdname, ... }
5416 * snapname -> error value (int32)
5422 zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
5424 return (dsl_dataset_user_release(holds, errlist));
5429 * zc_name name of new filesystem or snapshot
5430 * zc_value full name of old snapshot
5433 * zc_cookie space in bytes
5434 * zc_objset_type compressed space in bytes
5435 * zc_perm_action uncompressed space in bytes
5438 zfs_ioc_space_written(zfs_cmd_t *zc)
5442 dsl_dataset_t *new, *old;
5444 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5447 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
5449 dsl_pool_rele(dp, FTAG);
5452 error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
5454 dsl_dataset_rele(new, FTAG);
5455 dsl_pool_rele(dp, FTAG);
5459 error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
5460 &zc->zc_objset_type, &zc->zc_perm_action);
5461 dsl_dataset_rele(old, FTAG);
5462 dsl_dataset_rele(new, FTAG);
5463 dsl_pool_rele(dp, FTAG);
5469 * "firstsnap" -> snapshot name
5473 * "used" -> space in bytes
5474 * "compressed" -> compressed space in bytes
5475 * "uncompressed" -> uncompressed space in bytes
5479 zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
5483 dsl_dataset_t *new, *old;
5485 uint64_t used, comp, uncomp;
5487 if (nvlist_lookup_string(innvl, "firstsnap", &firstsnap) != 0)
5488 return (SET_ERROR(EINVAL));
5490 error = dsl_pool_hold(lastsnap, FTAG, &dp);
5494 error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
5495 if (error == 0 && !new->ds_is_snapshot) {
5496 dsl_dataset_rele(new, FTAG);
5497 error = SET_ERROR(EINVAL);
5500 dsl_pool_rele(dp, FTAG);
5503 error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
5504 if (error == 0 && !old->ds_is_snapshot) {
5505 dsl_dataset_rele(old, FTAG);
5506 error = SET_ERROR(EINVAL);
5509 dsl_dataset_rele(new, FTAG);
5510 dsl_pool_rele(dp, FTAG);
5514 error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
5515 dsl_dataset_rele(old, FTAG);
5516 dsl_dataset_rele(new, FTAG);
5517 dsl_pool_rele(dp, FTAG);
5518 fnvlist_add_uint64(outnvl, "used", used);
5519 fnvlist_add_uint64(outnvl, "compressed", comp);
5520 fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
5525 zfs_ioc_jail(zfs_cmd_t *zc)
5528 return (zone_dataset_attach(curthread->td_ucred, zc->zc_name,
5529 (int)zc->zc_jailid));
5533 zfs_ioc_unjail(zfs_cmd_t *zc)
5536 return (zone_dataset_detach(curthread->td_ucred, zc->zc_name,
5537 (int)zc->zc_jailid));
5542 * "fd" -> file descriptor to write stream to (int32)
5543 * (optional) "fromsnap" -> full snap name to send an incremental from
5544 * (optional) "largeblockok" -> (value ignored)
5545 * indicates that blocks > 128KB are permitted
5546 * (optional) "embedok" -> (value ignored)
5547 * presence indicates DRR_WRITE_EMBEDDED records are permitted
5548 * (optional) "resume_object" and "resume_offset" -> (uint64)
5549 * if present, resume send stream from specified object and offset.
5556 zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5558 cap_rights_t rights;
5562 char *fromname = NULL;
5564 boolean_t largeblockok;
5566 uint64_t resumeobj = 0;
5567 uint64_t resumeoff = 0;
5569 error = nvlist_lookup_int32(innvl, "fd", &fd);
5571 return (SET_ERROR(EINVAL));
5573 (void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
5575 largeblockok = nvlist_exists(innvl, "largeblockok");
5576 embedok = nvlist_exists(innvl, "embedok");
5578 (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
5579 (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
5582 file_t *fp = getf(fd);
5584 fget_write(curthread, fd, cap_rights_init(&rights, CAP_WRITE), &fp);
5587 return (SET_ERROR(EBADF));
5590 error = dmu_send(snapname, fromname, embedok, largeblockok, fd,
5592 resumeobj, resumeoff, fp->f_vnode, &off);
5594 resumeobj, resumeoff, fp, &off);
5598 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5609 * Determine approximately how large a zfs send stream will be -- the number
5610 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
5613 * (optional) "from" -> full snap or bookmark name to send an incremental
5618 * "space" -> bytes of space (uint64)
5622 zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5625 dsl_dataset_t *tosnap;
5630 error = dsl_pool_hold(snapname, FTAG, &dp);
5634 error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
5636 dsl_pool_rele(dp, FTAG);
5640 error = nvlist_lookup_string(innvl, "from", &fromname);
5642 if (strchr(fromname, '@') != NULL) {
5644 * If from is a snapshot, hold it and use the more
5645 * efficient dmu_send_estimate to estimate send space
5646 * size using deadlists.
5648 dsl_dataset_t *fromsnap;
5649 error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
5652 error = dmu_send_estimate(tosnap, fromsnap, &space);
5653 dsl_dataset_rele(fromsnap, FTAG);
5654 } else if (strchr(fromname, '#') != NULL) {
5656 * If from is a bookmark, fetch the creation TXG of the
5657 * snapshot it was created from and use that to find
5658 * blocks that were born after it.
5660 zfs_bookmark_phys_t frombm;
5662 error = dsl_bookmark_lookup(dp, fromname, tosnap,
5666 error = dmu_send_estimate_from_txg(tosnap,
5667 frombm.zbm_creation_txg, &space);
5670 * from is not properly formatted as a snapshot or
5673 error = SET_ERROR(EINVAL);
5677 // If estimating the size of a full send, use dmu_send_estimate
5678 error = dmu_send_estimate(tosnap, NULL, &space);
5681 fnvlist_add_uint64(outnvl, "space", space);
5684 dsl_dataset_rele(tosnap, FTAG);
5685 dsl_pool_rele(dp, FTAG);
5689 static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
5692 zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5693 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
5694 boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
5696 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
5698 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
5699 ASSERT3U(ioc, <, ZFS_IOC_LAST);
5700 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
5701 ASSERT3P(vec->zvec_func, ==, NULL);
5703 vec->zvec_legacy_func = func;
5704 vec->zvec_secpolicy = secpolicy;
5705 vec->zvec_namecheck = namecheck;
5706 vec->zvec_allow_log = log_history;
5707 vec->zvec_pool_check = pool_check;
5711 * See the block comment at the beginning of this file for details on
5712 * each argument to this function.
5715 zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
5716 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
5717 zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
5718 boolean_t allow_log)
5720 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
5722 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
5723 ASSERT3U(ioc, <, ZFS_IOC_LAST);
5724 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
5725 ASSERT3P(vec->zvec_func, ==, NULL);
5727 /* if we are logging, the name must be valid */
5728 ASSERT(!allow_log || namecheck != NO_NAME);
5730 vec->zvec_name = name;
5731 vec->zvec_func = func;
5732 vec->zvec_secpolicy = secpolicy;
5733 vec->zvec_namecheck = namecheck;
5734 vec->zvec_pool_check = pool_check;
5735 vec->zvec_smush_outnvlist = smush_outnvlist;
5736 vec->zvec_allow_log = allow_log;
5740 zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5741 zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
5742 zfs_ioc_poolcheck_t pool_check)
5744 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5745 POOL_NAME, log_history, pool_check);
5749 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5750 zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
5752 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5753 DATASET_NAME, B_FALSE, pool_check);
5757 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
5759 zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
5760 POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5764 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5765 zfs_secpolicy_func_t *secpolicy)
5767 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5768 NO_NAME, B_FALSE, POOL_CHECK_NONE);
5772 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
5773 zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
5775 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5776 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
5780 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
5782 zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
5783 zfs_secpolicy_read);
5787 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5788 zfs_secpolicy_func_t *secpolicy)
5790 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5791 DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5795 zfs_ioctl_init(void)
5797 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
5798 zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
5799 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5801 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
5802 zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
5803 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE);
5805 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
5806 zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
5807 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5809 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
5810 zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
5811 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5813 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
5814 zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
5815 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5817 zfs_ioctl_register("create", ZFS_IOC_CREATE,
5818 zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
5819 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5821 zfs_ioctl_register("clone", ZFS_IOC_CLONE,
5822 zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
5823 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5825 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
5826 zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
5827 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5829 zfs_ioctl_register("hold", ZFS_IOC_HOLD,
5830 zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
5831 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5832 zfs_ioctl_register("release", ZFS_IOC_RELEASE,
5833 zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
5834 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5836 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
5837 zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
5838 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5840 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
5841 zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
5842 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
5844 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK,
5845 zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME,
5846 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5848 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS,
5849 zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME,
5850 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5852 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS,
5853 zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks,
5855 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5857 /* IOCTLS that use the legacy function signature */
5859 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
5860 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
5862 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
5863 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5864 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
5866 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
5867 zfs_ioc_pool_upgrade);
5868 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
5870 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
5871 zfs_ioc_vdev_remove);
5872 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
5873 zfs_ioc_vdev_set_state);
5874 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
5875 zfs_ioc_vdev_attach);
5876 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
5877 zfs_ioc_vdev_detach);
5878 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
5879 zfs_ioc_vdev_setpath);
5880 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
5881 zfs_ioc_vdev_setfru);
5882 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
5883 zfs_ioc_pool_set_props);
5884 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
5885 zfs_ioc_vdev_split);
5886 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
5887 zfs_ioc_pool_reguid);
5889 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
5890 zfs_ioc_pool_configs, zfs_secpolicy_none);
5891 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
5892 zfs_ioc_pool_tryimport, zfs_secpolicy_config);
5893 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
5894 zfs_ioc_inject_fault, zfs_secpolicy_inject);
5895 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
5896 zfs_ioc_clear_fault, zfs_secpolicy_inject);
5897 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
5898 zfs_ioc_inject_list_next, zfs_secpolicy_inject);
5901 * pool destroy, and export don't log the history as part of
5902 * zfsdev_ioctl, but rather zfs_ioc_pool_export
5903 * does the logging of those commands.
5905 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
5906 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
5907 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
5908 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
5910 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
5911 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
5912 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
5913 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
5915 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
5916 zfs_secpolicy_inject, B_FALSE, POOL_CHECK_NONE);
5917 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
5918 zfs_ioc_dsobj_to_dsname,
5919 zfs_secpolicy_diff, B_FALSE, POOL_CHECK_NONE);
5920 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
5921 zfs_ioc_pool_get_history,
5922 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
5924 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
5925 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5927 zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
5928 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5929 zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
5930 zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
5932 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
5933 zfs_ioc_space_written);
5934 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
5935 zfs_ioc_objset_recvd_props);
5936 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
5938 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
5940 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
5941 zfs_ioc_objset_stats);
5942 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
5943 zfs_ioc_objset_zplprops);
5944 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
5945 zfs_ioc_dataset_list_next);
5946 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
5947 zfs_ioc_snapshot_list_next);
5948 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
5949 zfs_ioc_send_progress);
5951 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
5952 zfs_ioc_diff, zfs_secpolicy_diff);
5953 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
5954 zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
5955 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
5956 zfs_ioc_obj_to_path, zfs_secpolicy_diff);
5957 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
5958 zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
5959 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
5960 zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
5961 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
5962 zfs_ioc_send, zfs_secpolicy_send);
5964 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
5965 zfs_secpolicy_none);
5966 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
5967 zfs_secpolicy_destroy);
5968 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
5969 zfs_secpolicy_rename);
5970 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
5971 zfs_secpolicy_recv);
5972 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
5973 zfs_secpolicy_promote);
5974 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
5975 zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
5976 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
5977 zfs_secpolicy_set_fsacl);
5979 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
5980 zfs_secpolicy_share, POOL_CHECK_NONE);
5981 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
5982 zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
5983 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
5984 zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
5985 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5986 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
5987 zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
5988 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5991 zfs_ioctl_register_dataset_nolog(ZFS_IOC_JAIL, zfs_ioc_jail,
5992 zfs_secpolicy_config, POOL_CHECK_NONE);
5993 zfs_ioctl_register_dataset_nolog(ZFS_IOC_UNJAIL, zfs_ioc_unjail,
5994 zfs_secpolicy_config, POOL_CHECK_NONE);
5999 pool_status_check(const char *name, zfs_ioc_namecheck_t type,
6000 zfs_ioc_poolcheck_t check)
6005 ASSERT(type == POOL_NAME || type == DATASET_NAME);
6007 if (check & POOL_CHECK_NONE)
6010 error = spa_open(name, &spa, FTAG);
6012 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
6013 error = SET_ERROR(EAGAIN);
6014 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
6015 error = SET_ERROR(EROFS);
6016 spa_close(spa, FTAG);
6022 * Find a free minor number.
6025 zfsdev_minor_alloc(void)
6027 static minor_t last_minor;
6030 ASSERT(MUTEX_HELD(&spa_namespace_lock));
6032 for (m = last_minor + 1; m != last_minor; m++) {
6033 if (m > ZFSDEV_MAX_MINOR)
6035 if (ddi_get_soft_state(zfsdev_state, m) == NULL) {
6045 zfs_ctldev_init(struct cdev *devp)
6048 zfs_soft_state_t *zs;
6050 ASSERT(MUTEX_HELD(&spa_namespace_lock));
6052 minor = zfsdev_minor_alloc();
6054 return (SET_ERROR(ENXIO));
6056 if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS)
6057 return (SET_ERROR(EAGAIN));
6059 devfs_set_cdevpriv((void *)(uintptr_t)minor, zfsdev_close);
6061 zs = ddi_get_soft_state(zfsdev_state, minor);
6062 zs->zss_type = ZSST_CTLDEV;
6063 zfs_onexit_init((zfs_onexit_t **)&zs->zss_data);
6069 zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor)
6071 ASSERT(MUTEX_HELD(&spa_namespace_lock));
6073 zfs_onexit_destroy(zo);
6074 ddi_soft_state_free(zfsdev_state, minor);
6078 zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which)
6080 zfs_soft_state_t *zp;
6082 zp = ddi_get_soft_state(zfsdev_state, minor);
6083 if (zp == NULL || zp->zss_type != which)
6086 return (zp->zss_data);
6090 zfsdev_open(struct cdev *devp, int flag, int mode, struct thread *td)
6095 if (getminor(*devp) != 0)
6096 return (zvol_open(devp, flag, otyp, cr));
6099 /* This is the control device. Allocate a new minor if requested. */
6101 mutex_enter(&spa_namespace_lock);
6102 error = zfs_ctldev_init(devp);
6103 mutex_exit(&spa_namespace_lock);
6110 zfsdev_close(void *data)
6113 minor_t minor = (minor_t)(uintptr_t)data;
6118 mutex_enter(&spa_namespace_lock);
6119 zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV);
6121 mutex_exit(&spa_namespace_lock);
6124 zfs_ctldev_destroy(zo, minor);
6125 mutex_exit(&spa_namespace_lock);
6129 zfsdev_ioctl(struct cdev *dev, u_long zcmd, caddr_t arg, int flag,
6136 minor_t minor = getminor(dev);
6138 zfs_iocparm_t *zc_iocparm;
6139 int cflag, cmd, oldvecnum;
6140 boolean_t newioc, compat;
6141 void *compat_zc = NULL;
6142 cred_t *cr = td->td_ucred;
6144 const zfs_ioc_vec_t *vec;
6145 char *saved_poolname = NULL;
6146 nvlist_t *innvl = NULL;
6148 cflag = ZFS_CMD_COMPAT_NONE;
6150 newioc = B_TRUE; /* "new" style (zfs_iocparm_t) ioctl */
6152 len = IOCPARM_LEN(zcmd);
6153 vecnum = cmd = zcmd & 0xff;
6156 * Check if we are talking to supported older binaries
6157 * and translate zfs_cmd if necessary
6159 if (len != sizeof(zfs_iocparm_t)) {
6166 case sizeof(zfs_cmd_zcmd_t):
6167 cflag = ZFS_CMD_COMPAT_LZC;
6169 case sizeof(zfs_cmd_deadman_t):
6170 cflag = ZFS_CMD_COMPAT_DEADMAN;
6172 case sizeof(zfs_cmd_v28_t):
6173 cflag = ZFS_CMD_COMPAT_V28;
6175 case sizeof(zfs_cmd_v15_t):
6176 cflag = ZFS_CMD_COMPAT_V15;
6177 vecnum = zfs_ioctl_v15_to_v28[cmd];
6180 * Return without further handling
6181 * if the command is blacklisted.
6183 if (vecnum == ZFS_IOC_COMPAT_PASS)
6185 else if (vecnum == ZFS_IOC_COMPAT_FAIL)
6194 vecnum = cmd - ZFS_IOC_FIRST;
6195 ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
6198 if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
6199 return (SET_ERROR(EINVAL));
6200 vec = &zfs_ioc_vec[vecnum];
6202 zc = kmem_zalloc(sizeof(zfs_cmd_t), KM_SLEEP);
6205 error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
6207 error = SET_ERROR(EFAULT);
6210 #else /* !illumos */
6211 bzero(zc, sizeof(zfs_cmd_t));
6214 zc_iocparm = (void *)arg;
6216 switch (zc_iocparm->zfs_ioctl_version) {
6217 case ZFS_IOCVER_CURRENT:
6218 if (zc_iocparm->zfs_cmd_size != sizeof(zfs_cmd_t)) {
6219 error = SET_ERROR(EINVAL);
6223 case ZFS_IOCVER_RESUME:
6224 if (zc_iocparm->zfs_cmd_size != sizeof(zfs_cmd_resume_t)) {
6225 error = SET_ERROR(EFAULT);
6229 cflag = ZFS_CMD_COMPAT_RESUME;
6231 case ZFS_IOCVER_EDBP:
6232 if (zc_iocparm->zfs_cmd_size != sizeof(zfs_cmd_edbp_t)) {
6233 error = SET_ERROR(EFAULT);
6237 cflag = ZFS_CMD_COMPAT_EDBP;
6239 case ZFS_IOCVER_ZCMD:
6240 if (zc_iocparm->zfs_cmd_size > sizeof(zfs_cmd_t) ||
6241 zc_iocparm->zfs_cmd_size < sizeof(zfs_cmd_zcmd_t)) {
6242 error = SET_ERROR(EFAULT);
6246 cflag = ZFS_CMD_COMPAT_ZCMD;
6249 error = SET_ERROR(EINVAL);
6255 ASSERT(sizeof(zfs_cmd_t) >= zc_iocparm->zfs_cmd_size);
6256 compat_zc = kmem_zalloc(sizeof(zfs_cmd_t), KM_SLEEP);
6257 bzero(compat_zc, sizeof(zfs_cmd_t));
6259 error = ddi_copyin((void *)(uintptr_t)zc_iocparm->zfs_cmd,
6260 compat_zc, zc_iocparm->zfs_cmd_size, flag);
6262 error = SET_ERROR(EFAULT);
6266 error = ddi_copyin((void *)(uintptr_t)zc_iocparm->zfs_cmd,
6267 zc, zc_iocparm->zfs_cmd_size, flag);
6269 error = SET_ERROR(EFAULT);
6277 ASSERT(compat_zc != NULL);
6278 zfs_cmd_compat_get(zc, compat_zc, cflag);
6280 ASSERT(compat_zc == NULL);
6281 zfs_cmd_compat_get(zc, arg, cflag);
6284 error = zfs_ioctl_compat_pre(zc, &vecnum, cflag);
6287 if (oldvecnum != vecnum)
6288 vec = &zfs_ioc_vec[vecnum];
6290 #endif /* !illumos */
6292 zc->zc_iflags = flag & FKIOCTL;
6293 if (zc->zc_nvlist_src_size != 0) {
6294 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
6295 zc->zc_iflags, &innvl);
6300 /* rewrite innvl for backwards compatibility */
6302 innvl = zfs_ioctl_compat_innvl(zc, innvl, vecnum, cflag);
6305 * Ensure that all pool/dataset names are valid before we pass down to
6308 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
6309 switch (vec->zvec_namecheck) {
6311 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
6312 error = SET_ERROR(EINVAL);
6314 error = pool_status_check(zc->zc_name,
6315 vec->zvec_namecheck, vec->zvec_pool_check);
6319 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
6320 error = SET_ERROR(EINVAL);
6322 error = pool_status_check(zc->zc_name,
6323 vec->zvec_namecheck, vec->zvec_pool_check);
6330 if (error == 0 && !(flag & FKIOCTL))
6331 error = vec->zvec_secpolicy(zc, innvl, cr);
6336 /* legacy ioctls can modify zc_name */
6337 len = strcspn(zc->zc_name, "/@#") + 1;
6338 saved_poolname = kmem_alloc(len, KM_SLEEP);
6339 (void) strlcpy(saved_poolname, zc->zc_name, len);
6341 if (vec->zvec_func != NULL) {
6345 nvlist_t *lognv = NULL;
6347 ASSERT(vec->zvec_legacy_func == NULL);
6350 * Add the innvl to the lognv before calling the func,
6351 * in case the func changes the innvl.
6353 if (vec->zvec_allow_log) {
6354 lognv = fnvlist_alloc();
6355 fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
6357 if (!nvlist_empty(innvl)) {
6358 fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
6363 outnvl = fnvlist_alloc();
6364 error = vec->zvec_func(zc->zc_name, innvl, outnvl);
6366 if (error == 0 && vec->zvec_allow_log &&
6367 spa_open(zc->zc_name, &spa, FTAG) == 0) {
6368 if (!nvlist_empty(outnvl)) {
6369 fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL,
6372 (void) spa_history_log_nvl(spa, lognv);
6373 spa_close(spa, FTAG);
6375 fnvlist_free(lognv);
6377 /* rewrite outnvl for backwards compatibility */
6379 outnvl = zfs_ioctl_compat_outnvl(zc, outnvl, vecnum,
6382 if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
6384 if (vec->zvec_smush_outnvlist) {
6385 smusherror = nvlist_smush(outnvl,
6386 zc->zc_nvlist_dst_size);
6388 if (smusherror == 0)
6389 puterror = put_nvlist(zc, outnvl);
6395 nvlist_free(outnvl);
6397 error = vec->zvec_legacy_func(zc);
6404 rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
6405 if (error == 0 && rc != 0)
6406 error = SET_ERROR(EFAULT);
6409 zfs_ioctl_compat_post(zc, cmd, cflag);
6411 ASSERT(compat_zc != NULL);
6412 ASSERT(sizeof(zfs_cmd_t) >= zc_iocparm->zfs_cmd_size);
6414 zfs_cmd_compat_put(zc, compat_zc, vecnum, cflag);
6415 rc = ddi_copyout(compat_zc,
6416 (void *)(uintptr_t)zc_iocparm->zfs_cmd,
6417 zc_iocparm->zfs_cmd_size, flag);
6418 if (error == 0 && rc != 0)
6419 error = SET_ERROR(EFAULT);
6420 kmem_free(compat_zc, sizeof (zfs_cmd_t));
6422 zfs_cmd_compat_put(zc, arg, vecnum, cflag);
6427 rc = ddi_copyout(zc, (void *)(uintptr_t)zc_iocparm->zfs_cmd,
6428 sizeof (zfs_cmd_t), flag);
6429 if (error == 0 && rc != 0)
6430 error = SET_ERROR(EFAULT);
6433 if (error == 0 && vec->zvec_allow_log) {
6434 char *s = tsd_get(zfs_allow_log_key);
6437 (void) tsd_set(zfs_allow_log_key, saved_poolname);
6439 if (saved_poolname != NULL)
6440 strfree(saved_poolname);
6443 kmem_free(zc, sizeof (zfs_cmd_t));
6449 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
6451 if (cmd != DDI_ATTACH)
6452 return (DDI_FAILURE);
6454 if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
6455 DDI_PSEUDO, 0) == DDI_FAILURE)
6456 return (DDI_FAILURE);
6460 ddi_report_dev(dip);
6462 return (DDI_SUCCESS);
6466 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
6468 if (spa_busy() || zfs_busy() || zvol_busy())
6469 return (DDI_FAILURE);
6471 if (cmd != DDI_DETACH)
6472 return (DDI_FAILURE);
6476 ddi_prop_remove_all(dip);
6477 ddi_remove_minor_node(dip, NULL);
6479 return (DDI_SUCCESS);
6484 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
6487 case DDI_INFO_DEVT2DEVINFO:
6489 return (DDI_SUCCESS);
6491 case DDI_INFO_DEVT2INSTANCE:
6492 *result = (void *)0;
6493 return (DDI_SUCCESS);
6496 return (DDI_FAILURE);
6498 #endif /* illumos */
6501 * OK, so this is a little weird.
6503 * /dev/zfs is the control node, i.e. minor 0.
6504 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
6506 * /dev/zfs has basically nothing to do except serve up ioctls,
6507 * so most of the standard driver entry points are in zvol.c.
6510 static struct cb_ops zfs_cb_ops = {
6511 zfsdev_open, /* open */
6512 zfsdev_close, /* close */
6513 zvol_strategy, /* strategy */
6515 zvol_dump, /* dump */
6516 zvol_read, /* read */
6517 zvol_write, /* write */
6518 zfsdev_ioctl, /* ioctl */
6522 nochpoll, /* poll */
6523 ddi_prop_op, /* prop_op */
6524 NULL, /* streamtab */
6525 D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */
6526 CB_REV, /* version */
6527 nodev, /* async read */
6528 nodev, /* async write */
6531 static struct dev_ops zfs_dev_ops = {
6532 DEVO_REV, /* version */
6534 zfs_info, /* info */
6535 nulldev, /* identify */
6536 nulldev, /* probe */
6537 zfs_attach, /* attach */
6538 zfs_detach, /* detach */
6540 &zfs_cb_ops, /* driver operations */
6541 NULL, /* no bus operations */
6543 ddi_quiesce_not_needed, /* quiesce */
6546 static struct modldrv zfs_modldrv = {
6552 static struct modlinkage modlinkage = {
6554 (void *)&zfs_modlfs,
6555 (void *)&zfs_modldrv,
6558 #endif /* illumos */
6560 static struct cdevsw zfs_cdevsw = {
6561 .d_version = D_VERSION,
6562 .d_open = zfsdev_open,
6563 .d_ioctl = zfsdev_ioctl,
6564 .d_name = ZFS_DEV_NAME
6568 zfs_allow_log_destroy(void *arg)
6570 char *poolname = arg;
6577 zfsdev = make_dev(&zfs_cdevsw, 0x0, UID_ROOT, GID_OPERATOR, 0666,
6585 destroy_dev(zfsdev);
6588 static struct root_hold_token *zfs_root_token;
6589 struct proc *zfsproc;
6597 spa_init(FREAD | FWRITE);
6602 if ((error = mod_install(&modlinkage)) != 0) {
6609 tsd_create(&zfs_fsyncer_key, NULL);
6610 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
6611 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
6613 error = ldi_ident_from_mod(&modlinkage, &zfs_li);
6615 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
6625 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
6626 return (SET_ERROR(EBUSY));
6628 if ((error = mod_remove(&modlinkage)) != 0)
6634 if (zfs_nfsshare_inited)
6635 (void) ddi_modclose(nfs_mod);
6636 if (zfs_smbshare_inited)
6637 (void) ddi_modclose(smbsrv_mod);
6638 if (zfs_nfsshare_inited || zfs_smbshare_inited)
6639 (void) ddi_modclose(sharefs_mod);
6641 tsd_destroy(&zfs_fsyncer_key);
6642 ldi_ident_release(zfs_li);
6644 mutex_destroy(&zfs_share_lock);
6650 _info(struct modinfo *modinfop)
6652 return (mod_info(&modlinkage, modinfop));
6654 #endif /* illumos */
6656 static int zfs__init(void);
6657 static int zfs__fini(void);
6658 static void zfs_shutdown(void *, int);
6660 static eventhandler_tag zfs_shutdown_event_tag;
6663 #define ZFS_MIN_KSTACK_PAGES 4
6671 #if KSTACK_PAGES < ZFS_MIN_KSTACK_PAGES
6672 printf("ZFS NOTICE: KSTACK_PAGES is %d which could result in stack "
6673 "overflow panic!\nPlease consider adding "
6674 "'options KSTACK_PAGES=%d' to your kernel config\n", KSTACK_PAGES,
6675 ZFS_MIN_KSTACK_PAGES);
6678 zfs_root_token = root_mount_hold("ZFS");
6680 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
6682 spa_init(FREAD | FWRITE);
6687 tsd_create(&zfs_fsyncer_key, NULL);
6688 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
6689 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
6691 printf("ZFS storage pool version: features support (" SPA_VERSION_STRING ")\n");
6692 root_mount_rel(zfs_root_token);
6702 if (spa_busy() || zfs_busy() || zvol_busy() ||
6703 zio_injection_enabled) {
6712 tsd_destroy(&zfs_fsyncer_key);
6713 tsd_destroy(&rrw_tsd_key);
6714 tsd_destroy(&zfs_allow_log_key);
6716 mutex_destroy(&zfs_share_lock);
6722 zfs_shutdown(void *arg __unused, int howto __unused)
6726 * ZFS fini routines can not properly work in a panic-ed system.
6728 if (panicstr == NULL)
6734 zfs_modevent(module_t mod, int type, void *unused __unused)
6742 zfs_shutdown_event_tag = EVENTHANDLER_REGISTER(
6743 shutdown_post_sync, zfs_shutdown, NULL,
6744 SHUTDOWN_PRI_FIRST);
6748 if (err == 0 && zfs_shutdown_event_tag != NULL)
6749 EVENTHANDLER_DEREGISTER(shutdown_post_sync,
6750 zfs_shutdown_event_tag);
6757 return (EOPNOTSUPP);
6760 static moduledata_t zfs_mod = {
6765 DECLARE_MODULE(zfsctrl, zfs_mod, SI_SUB_VFS, SI_ORDER_ANY);
6766 MODULE_VERSION(zfsctrl, 1);
6767 MODULE_DEPEND(zfsctrl, opensolaris, 1, 1, 1);
6768 MODULE_DEPEND(zfsctrl, krpc, 1, 1, 1);
6769 MODULE_DEPEND(zfsctrl, acl_nfs4, 1, 1, 1);