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. All rights reserved.
25 * Copyright 2013 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
26 * Copyright 2014 Xin Li <delphij@FreeBSD.org>. All rights reserved.
27 * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
28 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
29 * Copyright (c) 2014, 2016 Joyent, Inc. All rights reserved.
30 * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
31 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
32 * Copyright (c) 2013 Steven Hartland. All rights reserved.
33 * Copyright (c) 2014 Integros [integros.com]
34 * Copyright 2016 Toomas Soome <tsoome@me.com>
35 * Copyright 2017 RackTop Systems.
36 * Copyright (c) 2017 Datto Inc.
42 * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
43 * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
45 * There are two ways that we handle ioctls: the legacy way where almost
46 * all of the logic is in the ioctl callback, and the new way where most
47 * of the marshalling is handled in the common entry point, zfsdev_ioctl().
49 * Non-legacy ioctls should be registered by calling
50 * zfs_ioctl_register() from zfs_ioctl_init(). The ioctl is invoked
51 * from userland by lzc_ioctl().
53 * The registration arguments are as follows:
56 * The name of the ioctl. This is used for history logging. If the
57 * ioctl returns successfully (the callback returns 0), and allow_log
58 * is true, then a history log entry will be recorded with the input &
59 * output nvlists. The log entry can be printed with "zpool history -i".
62 * The ioctl request number, which userland will pass to ioctl(2).
63 * The ioctl numbers can change from release to release, because
64 * the caller (libzfs) must be matched to the kernel.
66 * zfs_secpolicy_func_t *secpolicy
67 * This function will be called before the zfs_ioc_func_t, to
68 * determine if this operation is permitted. It should return EPERM
69 * on failure, and 0 on success. Checks include determining if the
70 * dataset is visible in this zone, and if the user has either all
71 * zfs privileges in the zone (SYS_MOUNT), or has been granted permission
72 * to do this operation on this dataset with "zfs allow".
74 * zfs_ioc_namecheck_t namecheck
75 * This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
76 * name, a dataset name, or nothing. If the name is not well-formed,
77 * the ioctl will fail and the callback will not be called.
78 * Therefore, the callback can assume that the name is well-formed
79 * (e.g. is null-terminated, doesn't have more than one '@' character,
80 * doesn't have invalid characters).
82 * zfs_ioc_poolcheck_t pool_check
83 * This specifies requirements on the pool state. If the pool does
84 * not meet them (is suspended or is readonly), the ioctl will fail
85 * and the callback will not be called. If any checks are specified
86 * (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
87 * Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
88 * POOL_CHECK_READONLY).
90 * boolean_t smush_outnvlist
91 * If smush_outnvlist is true, then the output is presumed to be a
92 * list of errors, and it will be "smushed" down to fit into the
93 * caller's buffer, by removing some entries and replacing them with a
94 * single "N_MORE_ERRORS" entry indicating how many were removed. See
95 * nvlist_smush() for details. If smush_outnvlist is false, and the
96 * outnvlist does not fit into the userland-provided buffer, then the
97 * ioctl will fail with ENOMEM.
99 * zfs_ioc_func_t *func
100 * The callback function that will perform the operation.
102 * The callback should return 0 on success, or an error number on
103 * failure. If the function fails, the userland ioctl will return -1,
104 * and errno will be set to the callback's return value. The callback
105 * will be called with the following arguments:
108 * The name of the pool or dataset to operate on, from
109 * zfs_cmd_t:zc_name. The 'namecheck' argument specifies the
110 * expected type (pool, dataset, or none).
113 * The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src. Or
114 * NULL if no input nvlist was provided. Changes to this nvlist are
115 * ignored. If the input nvlist could not be deserialized, the
116 * ioctl will fail and the callback will not be called.
119 * The output nvlist, initially empty. The callback can fill it in,
120 * and it will be returned to userland by serializing it into
121 * zfs_cmd_t:zc_nvlist_dst. If it is non-empty, and serialization
122 * fails (e.g. because the caller didn't supply a large enough
123 * buffer), then the overall ioctl will fail. See the
124 * 'smush_nvlist' argument above for additional behaviors.
126 * There are two typical uses of the output nvlist:
127 * - To return state, e.g. property values. In this case,
128 * smush_outnvlist should be false. If the buffer was not large
129 * enough, the caller will reallocate a larger buffer and try
132 * - To return multiple errors from an ioctl which makes on-disk
133 * changes. In this case, smush_outnvlist should be true.
134 * Ioctls which make on-disk modifications should generally not
135 * use the outnvl if they succeed, because the caller can not
136 * distinguish between the operation failing, and
137 * deserialization failing.
140 #include "opt_kstack_pages.h"
143 #include <sys/types.h>
144 #include <sys/param.h>
145 #include <sys/systm.h>
146 #include <sys/conf.h>
147 #include <sys/kernel.h>
148 #include <sys/lock.h>
149 #include <sys/malloc.h>
150 #include <sys/mutex.h>
151 #include <sys/proc.h>
152 #include <sys/errno.h>
155 #include <sys/file.h>
156 #include <sys/kmem.h>
157 #include <sys/conf.h>
158 #include <sys/cmn_err.h>
159 #include <sys/stat.h>
160 #include <sys/zfs_ioctl.h>
161 #include <sys/zfs_vfsops.h>
162 #include <sys/zfs_znode.h>
165 #include <sys/spa_impl.h>
166 #include <sys/vdev.h>
168 #include <sys/dsl_dir.h>
169 #include <sys/dsl_dataset.h>
170 #include <sys/dsl_prop.h>
171 #include <sys/dsl_deleg.h>
172 #include <sys/dmu_objset.h>
173 #include <sys/dmu_impl.h>
174 #include <sys/dmu_tx.h>
175 #include <sys/sunddi.h>
176 #include <sys/policy.h>
177 #include <sys/zone.h>
178 #include <sys/nvpair.h>
179 #include <sys/mount.h>
180 #include <sys/taskqueue.h>
182 #include <sys/varargs.h>
183 #include <sys/fs/zfs.h>
184 #include <sys/zfs_ctldir.h>
185 #include <sys/zfs_dir.h>
186 #include <sys/zfs_onexit.h>
187 #include <sys/zvol.h>
188 #include <sys/dsl_scan.h>
189 #include <sys/dmu_objset.h>
190 #include <sys/dmu_send.h>
191 #include <sys/dsl_destroy.h>
192 #include <sys/dsl_bookmark.h>
193 #include <sys/dsl_userhold.h>
194 #include <sys/zfeature.h>
196 #include <sys/zio_checksum.h>
197 #include <sys/vdev_removal.h>
198 #include <sys/vdev_impl.h>
199 #include <sys/vdev_initialize.h>
201 #include "zfs_namecheck.h"
202 #include "zfs_prop.h"
203 #include "zfs_deleg.h"
204 #include "zfs_comutil.h"
205 #include "zfs_ioctl_compat.h"
210 static struct cdev *zfsdev;
212 extern void zfs_init(void);
213 extern void zfs_fini(void);
215 uint_t zfs_fsyncer_key;
216 extern uint_t rrw_tsd_key;
217 static uint_t zfs_allow_log_key;
218 extern uint_t zfs_geom_probe_vdev_key;
220 typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *);
221 typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *);
222 typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *);
228 } zfs_ioc_namecheck_t;
231 POOL_CHECK_NONE = 1 << 0,
232 POOL_CHECK_SUSPENDED = 1 << 1,
233 POOL_CHECK_READONLY = 1 << 2,
234 } zfs_ioc_poolcheck_t;
236 typedef struct zfs_ioc_vec {
237 zfs_ioc_legacy_func_t *zvec_legacy_func;
238 zfs_ioc_func_t *zvec_func;
239 zfs_secpolicy_func_t *zvec_secpolicy;
240 zfs_ioc_namecheck_t zvec_namecheck;
241 boolean_t zvec_allow_log;
242 zfs_ioc_poolcheck_t zvec_pool_check;
243 boolean_t zvec_smush_outnvlist;
244 const char *zvec_name;
247 /* This array is indexed by zfs_userquota_prop_t */
248 static const char *userquota_perms[] = {
249 ZFS_DELEG_PERM_USERUSED,
250 ZFS_DELEG_PERM_USERQUOTA,
251 ZFS_DELEG_PERM_GROUPUSED,
252 ZFS_DELEG_PERM_GROUPQUOTA,
255 static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
256 static int zfs_check_settable(const char *name, nvpair_t *property,
258 static int zfs_check_clearable(char *dataset, nvlist_t *props,
260 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
262 int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
263 static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
265 static void zfsdev_close(void *data);
267 static int zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature);
269 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
271 __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
278 * Get rid of annoying "../common/" prefix to filename.
280 newfile = strrchr(file, '/');
281 if (newfile != NULL) {
282 newfile = newfile + 1; /* Get rid of leading / */
288 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
292 * To get this data, use the zfs-dprintf probe as so:
293 * dtrace -q -n 'zfs-dprintf \
294 * /stringof(arg0) == "dbuf.c"/ \
295 * {printf("%s: %s", stringof(arg1), stringof(arg3))}'
297 * arg1 = function name
301 DTRACE_PROBE4(zfs__dprintf,
302 char *, newfile, char *, func, int, line, char *, buf);
306 history_str_free(char *buf)
308 kmem_free(buf, HIS_MAX_RECORD_LEN);
312 history_str_get(zfs_cmd_t *zc)
316 if (zc->zc_history == 0)
319 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
320 if (copyinstr((void *)(uintptr_t)zc->zc_history,
321 buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
322 history_str_free(buf);
326 buf[HIS_MAX_RECORD_LEN -1] = '\0';
332 * Check to see if the named dataset is currently defined as bootable
335 zfs_is_bootfs(const char *name)
339 if (dmu_objset_hold(name, FTAG, &os) == 0) {
341 ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
342 dmu_objset_rele(os, FTAG);
349 * Return non-zero if the spa version is less than requested version.
352 zfs_earlier_version(const char *name, int version)
356 if (spa_open(name, &spa, FTAG) == 0) {
357 if (spa_version(spa) < version) {
358 spa_close(spa, FTAG);
361 spa_close(spa, FTAG);
367 * Return TRUE if the ZPL version is less than requested version.
370 zpl_earlier_version(const char *name, int version)
373 boolean_t rc = B_TRUE;
375 if (dmu_objset_hold(name, FTAG, &os) == 0) {
378 if (dmu_objset_type(os) != DMU_OST_ZFS) {
379 dmu_objset_rele(os, FTAG);
382 /* XXX reading from non-owned objset */
383 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
384 rc = zplversion < version;
385 dmu_objset_rele(os, FTAG);
391 zfs_log_history(zfs_cmd_t *zc)
396 if ((buf = history_str_get(zc)) == NULL)
399 if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
400 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
401 (void) spa_history_log(spa, buf);
402 spa_close(spa, FTAG);
404 history_str_free(buf);
408 * Policy for top-level read operations (list pools). Requires no privileges,
409 * and can be used in the local zone, as there is no associated dataset.
413 zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
419 * Policy for dataset read operations (list children, get statistics). Requires
420 * no privileges, but must be visible in the local zone.
424 zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
426 if (INGLOBALZONE(curthread) ||
427 zone_dataset_visible(zc->zc_name, NULL))
430 return (SET_ERROR(ENOENT));
434 zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
439 * The dataset must be visible by this zone -- check this first
440 * so they don't see EPERM on something they shouldn't know about.
442 if (!INGLOBALZONE(curthread) &&
443 !zone_dataset_visible(dataset, &writable))
444 return (SET_ERROR(ENOENT));
446 if (INGLOBALZONE(curthread)) {
448 * If the fs is zoned, only root can access it from the
451 if (secpolicy_zfs(cr) && zoned)
452 return (SET_ERROR(EPERM));
455 * If we are in a local zone, the 'zoned' property must be set.
458 return (SET_ERROR(EPERM));
460 /* must be writable by this zone */
462 return (SET_ERROR(EPERM));
468 zfs_dozonecheck(const char *dataset, cred_t *cr)
472 if (dsl_prop_get_integer(dataset, "jailed", &zoned, NULL))
473 return (SET_ERROR(ENOENT));
475 return (zfs_dozonecheck_impl(dataset, zoned, cr));
479 zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
483 if (dsl_prop_get_int_ds(ds, "jailed", &zoned))
484 return (SET_ERROR(ENOENT));
486 return (zfs_dozonecheck_impl(dataset, zoned, cr));
490 zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
491 const char *perm, cred_t *cr)
495 error = zfs_dozonecheck_ds(name, ds, cr);
497 error = secpolicy_zfs(cr);
499 error = dsl_deleg_access_impl(ds, perm, cr);
505 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
512 * First do a quick check for root in the global zone, which
513 * is allowed to do all write_perms. This ensures that zfs_ioc_*
514 * will get to handle nonexistent datasets.
516 if (INGLOBALZONE(curthread) && secpolicy_zfs(cr) == 0)
519 error = dsl_pool_hold(name, FTAG, &dp);
523 error = dsl_dataset_hold(dp, name, FTAG, &ds);
525 dsl_pool_rele(dp, FTAG);
529 error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
531 dsl_dataset_rele(ds, FTAG);
532 dsl_pool_rele(dp, FTAG);
538 * Policy for setting the security label property.
540 * Returns 0 for success, non-zero for access and other errors.
543 zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
545 char ds_hexsl[MAXNAMELEN];
546 bslabel_t ds_sl, new_sl;
547 boolean_t new_default = FALSE;
549 int needed_priv = -1;
552 /* First get the existing dataset label. */
553 error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
554 1, sizeof (ds_hexsl), &ds_hexsl, NULL);
556 return (SET_ERROR(EPERM));
558 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
561 /* The label must be translatable */
562 if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
563 return (SET_ERROR(EINVAL));
566 * In a non-global zone, disallow attempts to set a label that
567 * doesn't match that of the zone; otherwise no other checks
570 if (!INGLOBALZONE(curproc)) {
571 if (new_default || !blequal(&new_sl, CR_SL(CRED())))
572 return (SET_ERROR(EPERM));
577 * For global-zone datasets (i.e., those whose zoned property is
578 * "off", verify that the specified new label is valid for the
581 if (dsl_prop_get_integer(name,
582 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
583 return (SET_ERROR(EPERM));
585 if (zfs_check_global_label(name, strval) != 0)
586 return (SET_ERROR(EPERM));
590 * If the existing dataset label is nondefault, check if the
591 * dataset is mounted (label cannot be changed while mounted).
592 * Get the zfsvfs; if there isn't one, then the dataset isn't
593 * mounted (or isn't a dataset, doesn't exist, ...).
595 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
597 static char *setsl_tag = "setsl_tag";
600 * Try to own the dataset; abort if there is any error,
601 * (e.g., already mounted, in use, or other error).
603 error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE,
606 return (SET_ERROR(EPERM));
608 dmu_objset_disown(os, setsl_tag);
611 needed_priv = PRIV_FILE_DOWNGRADE_SL;
615 if (hexstr_to_label(strval, &new_sl) != 0)
616 return (SET_ERROR(EPERM));
618 if (blstrictdom(&ds_sl, &new_sl))
619 needed_priv = PRIV_FILE_DOWNGRADE_SL;
620 else if (blstrictdom(&new_sl, &ds_sl))
621 needed_priv = PRIV_FILE_UPGRADE_SL;
623 /* dataset currently has a default label */
625 needed_priv = PRIV_FILE_UPGRADE_SL;
629 if (needed_priv != -1)
630 return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
633 #endif /* SECLABEL */
636 zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
642 * Check permissions for special properties.
647 * Disallow setting of 'zoned' from within a local zone.
649 if (!INGLOBALZONE(curthread))
650 return (SET_ERROR(EPERM));
654 case ZFS_PROP_FILESYSTEM_LIMIT:
655 case ZFS_PROP_SNAPSHOT_LIMIT:
656 if (!INGLOBALZONE(curthread)) {
658 char setpoint[ZFS_MAX_DATASET_NAME_LEN];
660 * Unprivileged users are allowed to modify the
661 * limit on things *under* (ie. contained by)
662 * the thing they own.
664 if (dsl_prop_get_integer(dsname, "jailed", &zoned,
666 return (SET_ERROR(EPERM));
667 if (!zoned || strlen(dsname) <= strlen(setpoint))
668 return (SET_ERROR(EPERM));
672 case ZFS_PROP_MLSLABEL:
674 if (!is_system_labeled())
675 return (SET_ERROR(EPERM));
677 if (nvpair_value_string(propval, &strval) == 0) {
680 err = zfs_set_slabel_policy(dsname, strval, CRED());
690 return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
695 zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
699 error = zfs_dozonecheck(zc->zc_name, cr);
704 * permission to set permissions will be evaluated later in
705 * dsl_deleg_can_allow()
712 zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
714 return (zfs_secpolicy_write_perms(zc->zc_name,
715 ZFS_DELEG_PERM_ROLLBACK, cr));
720 zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
728 * Generate the current snapshot name from the given objsetid, then
729 * use that name for the secpolicy/zone checks.
731 cp = strchr(zc->zc_name, '@');
733 return (SET_ERROR(EINVAL));
734 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
738 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
740 dsl_pool_rele(dp, FTAG);
744 dsl_dataset_name(ds, zc->zc_name);
746 error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
747 ZFS_DELEG_PERM_SEND, cr);
748 dsl_dataset_rele(ds, FTAG);
749 dsl_pool_rele(dp, FTAG);
756 zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
758 return (zfs_secpolicy_write_perms(zc->zc_name,
759 ZFS_DELEG_PERM_SEND, cr));
764 zfs_secpolicy_deleg_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
769 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
770 NO_FOLLOW, NULL, &vp)) != 0)
773 /* Now make sure mntpnt and dataset are ZFS */
775 if (strcmp(vp->v_vfsp->mnt_stat.f_fstypename, "zfs") != 0 ||
776 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
777 zc->zc_name) != 0)) {
779 return (SET_ERROR(EPERM));
783 return (dsl_deleg_access(zc->zc_name,
784 ZFS_DELEG_PERM_SHARE, cr));
788 zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
790 if (!INGLOBALZONE(curthread))
791 return (SET_ERROR(EPERM));
793 if (secpolicy_nfs(cr) == 0) {
796 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
801 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
803 if (!INGLOBALZONE(curthread))
804 return (SET_ERROR(EPERM));
806 if (secpolicy_smb(cr) == 0) {
809 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
814 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
819 * Remove the @bla or /bla from the end of the name to get the parent.
821 (void) strncpy(parent, datasetname, parentsize);
822 cp = strrchr(parent, '@');
826 cp = strrchr(parent, '/');
828 return (SET_ERROR(ENOENT));
836 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
840 if ((error = zfs_secpolicy_write_perms(name,
841 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
844 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
849 zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
851 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
855 * Destroying snapshots with delegated permissions requires
856 * descendant mount and destroy permissions.
860 zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
863 nvpair_t *pair, *nextpair;
866 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
867 return (SET_ERROR(EINVAL));
868 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
870 nextpair = nvlist_next_nvpair(snaps, pair);
871 error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr);
872 if (error == ENOENT) {
874 * Ignore any snapshots that don't exist (we consider
875 * them "already destroyed"). Remove the name from the
876 * nvl here in case the snapshot is created between
877 * now and when we try to destroy it (in which case
878 * we don't want to destroy it since we haven't
879 * checked for permission).
881 fnvlist_remove_nvpair(snaps, pair);
892 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
894 char parentname[ZFS_MAX_DATASET_NAME_LEN];
897 if ((error = zfs_secpolicy_write_perms(from,
898 ZFS_DELEG_PERM_RENAME, cr)) != 0)
901 if ((error = zfs_secpolicy_write_perms(from,
902 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
905 if ((error = zfs_get_parent(to, parentname,
906 sizeof (parentname))) != 0)
909 if ((error = zfs_secpolicy_write_perms(parentname,
910 ZFS_DELEG_PERM_CREATE, cr)) != 0)
913 if ((error = zfs_secpolicy_write_perms(parentname,
914 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
922 zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
927 if ((zc->zc_cookie & 1) != 0) {
929 * This is recursive rename, so the starting snapshot might
930 * not exist. Check file system or volume permission instead.
932 at = strchr(zc->zc_name, '@');
938 error = zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr);
948 zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
951 dsl_dataset_t *clone;
954 error = zfs_secpolicy_write_perms(zc->zc_name,
955 ZFS_DELEG_PERM_PROMOTE, cr);
959 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
963 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
966 char parentname[ZFS_MAX_DATASET_NAME_LEN];
967 dsl_dataset_t *origin = NULL;
971 error = dsl_dataset_hold_obj(dd->dd_pool,
972 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin);
974 dsl_dataset_rele(clone, FTAG);
975 dsl_pool_rele(dp, FTAG);
979 error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
980 ZFS_DELEG_PERM_MOUNT, cr);
982 dsl_dataset_name(origin, parentname);
984 error = zfs_secpolicy_write_perms_ds(parentname, origin,
985 ZFS_DELEG_PERM_PROMOTE, cr);
987 dsl_dataset_rele(clone, FTAG);
988 dsl_dataset_rele(origin, FTAG);
990 dsl_pool_rele(dp, FTAG);
996 zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1000 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1001 ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
1004 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1005 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
1008 return (zfs_secpolicy_write_perms(zc->zc_name,
1009 ZFS_DELEG_PERM_CREATE, cr));
1013 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
1015 return (zfs_secpolicy_write_perms(name,
1016 ZFS_DELEG_PERM_SNAPSHOT, cr));
1020 * Check for permission to create each snapshot in the nvlist.
1024 zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1030 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
1031 return (SET_ERROR(EINVAL));
1032 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
1033 pair = nvlist_next_nvpair(snaps, pair)) {
1034 char *name = nvpair_name(pair);
1035 char *atp = strchr(name, '@');
1038 error = SET_ERROR(EINVAL);
1042 error = zfs_secpolicy_snapshot_perms(name, cr);
1051 * Check for permission to create each snapshot in the nvlist.
1055 zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1059 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
1060 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
1061 char *name = nvpair_name(pair);
1062 char *hashp = strchr(name, '#');
1064 if (hashp == NULL) {
1065 error = SET_ERROR(EINVAL);
1069 error = zfs_secpolicy_write_perms(name,
1070 ZFS_DELEG_PERM_BOOKMARK, cr);
1080 zfs_secpolicy_remap(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1082 return (zfs_secpolicy_write_perms(zc->zc_name,
1083 ZFS_DELEG_PERM_REMAP, cr));
1088 zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1090 nvpair_t *pair, *nextpair;
1093 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1095 char *name = nvpair_name(pair);
1096 char *hashp = strchr(name, '#');
1097 nextpair = nvlist_next_nvpair(innvl, pair);
1099 if (hashp == NULL) {
1100 error = SET_ERROR(EINVAL);
1105 error = zfs_secpolicy_write_perms(name,
1106 ZFS_DELEG_PERM_DESTROY, cr);
1108 if (error == ENOENT) {
1110 * Ignore any filesystems that don't exist (we consider
1111 * their bookmarks "already destroyed"). Remove
1112 * the name from the nvl here in case the filesystem
1113 * is created between now and when we try to destroy
1114 * the bookmark (in which case we don't want to
1115 * destroy it since we haven't checked for permission).
1117 fnvlist_remove_nvpair(innvl, pair);
1129 zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1132 * Even root must have a proper TSD so that we know what pool
1135 if (tsd_get(zfs_allow_log_key) == NULL)
1136 return (SET_ERROR(EPERM));
1141 zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1143 char parentname[ZFS_MAX_DATASET_NAME_LEN];
1147 if ((error = zfs_get_parent(zc->zc_name, parentname,
1148 sizeof (parentname))) != 0)
1151 if (nvlist_lookup_string(innvl, "origin", &origin) == 0 &&
1152 (error = zfs_secpolicy_write_perms(origin,
1153 ZFS_DELEG_PERM_CLONE, cr)) != 0)
1156 if ((error = zfs_secpolicy_write_perms(parentname,
1157 ZFS_DELEG_PERM_CREATE, cr)) != 0)
1160 return (zfs_secpolicy_write_perms(parentname,
1161 ZFS_DELEG_PERM_MOUNT, cr));
1165 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
1166 * SYS_CONFIG privilege, which is not available in a local zone.
1170 zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1172 if (secpolicy_sys_config(cr, B_FALSE) != 0)
1173 return (SET_ERROR(EPERM));
1179 * Policy for object to name lookups.
1183 zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1187 if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
1190 error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
1195 * Policy for fault injection. Requires all privileges.
1199 zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1201 return (secpolicy_zinject(cr));
1206 zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1208 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
1210 if (prop == ZPROP_INVAL) {
1211 if (!zfs_prop_user(zc->zc_value))
1212 return (SET_ERROR(EINVAL));
1213 return (zfs_secpolicy_write_perms(zc->zc_name,
1214 ZFS_DELEG_PERM_USERPROP, cr));
1216 return (zfs_secpolicy_setprop(zc->zc_name, prop,
1222 zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1224 int err = zfs_secpolicy_read(zc, innvl, cr);
1228 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1229 return (SET_ERROR(EINVAL));
1231 if (zc->zc_value[0] == 0) {
1233 * They are asking about a posix uid/gid. If it's
1234 * themself, allow it.
1236 if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
1237 zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
1238 if (zc->zc_guid == crgetuid(cr))
1241 if (groupmember(zc->zc_guid, cr))
1246 return (zfs_secpolicy_write_perms(zc->zc_name,
1247 userquota_perms[zc->zc_objset_type], cr));
1251 zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1253 int err = zfs_secpolicy_read(zc, innvl, cr);
1257 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1258 return (SET_ERROR(EINVAL));
1260 return (zfs_secpolicy_write_perms(zc->zc_name,
1261 userquota_perms[zc->zc_objset_type], cr));
1266 zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1268 return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
1274 zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1280 error = nvlist_lookup_nvlist(innvl, "holds", &holds);
1282 return (SET_ERROR(EINVAL));
1284 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
1285 pair = nvlist_next_nvpair(holds, pair)) {
1286 char fsname[ZFS_MAX_DATASET_NAME_LEN];
1287 error = dmu_fsname(nvpair_name(pair), fsname);
1290 error = zfs_secpolicy_write_perms(fsname,
1291 ZFS_DELEG_PERM_HOLD, cr);
1300 zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1305 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1306 pair = nvlist_next_nvpair(innvl, pair)) {
1307 char fsname[ZFS_MAX_DATASET_NAME_LEN];
1308 error = dmu_fsname(nvpair_name(pair), fsname);
1311 error = zfs_secpolicy_write_perms(fsname,
1312 ZFS_DELEG_PERM_RELEASE, cr);
1320 * Policy for allowing temporary snapshots to be taken or released
1323 zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1326 * A temporary snapshot is the same as a snapshot,
1327 * hold, destroy and release all rolled into one.
1328 * Delegated diff alone is sufficient that we allow this.
1332 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1333 ZFS_DELEG_PERM_DIFF, cr)) == 0)
1336 error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
1338 error = zfs_secpolicy_hold(zc, innvl, cr);
1340 error = zfs_secpolicy_release(zc, innvl, cr);
1342 error = zfs_secpolicy_destroy(zc, innvl, cr);
1347 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1350 get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1354 nvlist_t *list = NULL;
1357 * Read in and unpack the user-supplied nvlist.
1360 return (SET_ERROR(EINVAL));
1362 packed = kmem_alloc(size, KM_SLEEP);
1364 if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1366 kmem_free(packed, size);
1367 return (SET_ERROR(EFAULT));
1370 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1371 kmem_free(packed, size);
1375 kmem_free(packed, size);
1382 * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
1383 * Entries will be removed from the end of the nvlist, and one int32 entry
1384 * named "N_MORE_ERRORS" will be added indicating how many entries were
1388 nvlist_smush(nvlist_t *errors, size_t max)
1392 size = fnvlist_size(errors);
1395 nvpair_t *more_errors;
1399 return (SET_ERROR(ENOMEM));
1401 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
1402 more_errors = nvlist_prev_nvpair(errors, NULL);
1405 nvpair_t *pair = nvlist_prev_nvpair(errors,
1407 fnvlist_remove_nvpair(errors, pair);
1409 size = fnvlist_size(errors);
1410 } while (size > max);
1412 fnvlist_remove_nvpair(errors, more_errors);
1413 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
1414 ASSERT3U(fnvlist_size(errors), <=, max);
1421 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1423 char *packed = NULL;
1427 size = fnvlist_size(nvl);
1429 if (size > zc->zc_nvlist_dst_size) {
1431 * Solaris returns ENOMEM here, because even if an error is
1432 * returned from an ioctl(2), new zc_nvlist_dst_size will be
1433 * passed to the userland. This is not the case for FreeBSD.
1434 * We need to return 0, so the kernel will copy the
1435 * zc_nvlist_dst_size back and the userland can discover that a
1436 * bigger buffer is needed.
1440 packed = fnvlist_pack(nvl, &size);
1441 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1442 size, zc->zc_iflags) != 0)
1443 error = SET_ERROR(EFAULT);
1444 fnvlist_pack_free(packed, size);
1447 zc->zc_nvlist_dst_size = size;
1448 zc->zc_nvlist_dst_filled = B_TRUE;
1453 getzfsvfs_impl(objset_t *os, vfs_t **vfsp)
1458 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1459 return (SET_ERROR(EINVAL));
1462 mutex_enter(&os->os_user_ptr_lock);
1463 zfvp = dmu_objset_get_user(os);
1465 *vfsp = zfvp->z_vfs;
1466 vfs_ref(zfvp->z_vfs);
1468 error = SET_ERROR(ESRCH);
1470 mutex_exit(&os->os_user_ptr_lock);
1475 getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
1481 error = dmu_objset_hold(dsname, FTAG, &os);
1484 error = getzfsvfs_impl(os, &vfsp);
1485 dmu_objset_rele(os, FTAG);
1489 error = vfs_busy(vfsp, 0);
1493 error = SET_ERROR(ESRCH);
1495 *zfvp = vfsp->vfs_data;
1501 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
1502 * case its z_vfs will be NULL, and it will be opened as the owner.
1503 * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1504 * which prevents all vnode ops from running.
1507 zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
1511 if (getzfsvfs(name, zfvp) != 0)
1512 error = zfsvfs_create(name, zfvp);
1514 rrm_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
1517 if ((*zfvp)->z_unmounted) {
1519 * XXX we could probably try again, since the unmounting
1520 * thread should be just about to disassociate the
1521 * objset from the zfsvfs.
1523 rrm_exit(&(*zfvp)->z_teardown_lock, tag);
1524 return (SET_ERROR(EBUSY));
1528 * vfs_busy() ensures that the filesystem is not and
1529 * can not be unmounted.
1531 ASSERT(!(*zfvp)->z_unmounted);
1538 zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
1540 rrm_exit(&zfsvfs->z_teardown_lock, tag);
1542 if (zfsvfs->z_vfs) {
1544 VFS_RELE(zfsvfs->z_vfs);
1546 vfs_unbusy(zfsvfs->z_vfs);
1549 dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1550 zfsvfs_free(zfsvfs);
1555 zfs_ioc_pool_create(zfs_cmd_t *zc)
1558 nvlist_t *config, *props = NULL;
1559 nvlist_t *rootprops = NULL;
1560 nvlist_t *zplprops = NULL;
1561 char *spa_name = zc->zc_name;
1563 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1564 zc->zc_iflags, &config))
1567 if (zc->zc_nvlist_src_size != 0 && (error =
1568 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1569 zc->zc_iflags, &props))) {
1570 nvlist_free(config);
1575 nvlist_t *nvl = NULL;
1576 uint64_t version = SPA_VERSION;
1579 (void) nvlist_lookup_uint64(props,
1580 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1581 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1582 error = SET_ERROR(EINVAL);
1583 goto pool_props_bad;
1585 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
1587 error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
1589 nvlist_free(config);
1593 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1595 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1596 error = zfs_fill_zplprops_root(version, rootprops,
1599 goto pool_props_bad;
1601 if (nvlist_lookup_string(props,
1602 zpool_prop_to_name(ZPOOL_PROP_TNAME), &tname) == 0)
1606 error = spa_create(zc->zc_name, config, props, zplprops);
1609 * Set the remaining root properties
1611 if (!error && (error = zfs_set_prop_nvlist(spa_name,
1612 ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
1613 (void) spa_destroy(spa_name);
1616 nvlist_free(rootprops);
1617 nvlist_free(zplprops);
1618 nvlist_free(config);
1625 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1628 zfs_log_history(zc);
1629 error = spa_destroy(zc->zc_name);
1631 zvol_remove_minors(zc->zc_name);
1636 zfs_ioc_pool_import(zfs_cmd_t *zc)
1638 nvlist_t *config, *props = NULL;
1642 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1643 zc->zc_iflags, &config)) != 0)
1646 if (zc->zc_nvlist_src_size != 0 && (error =
1647 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1648 zc->zc_iflags, &props))) {
1649 nvlist_free(config);
1653 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1654 guid != zc->zc_guid)
1655 error = SET_ERROR(EINVAL);
1657 error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1659 if (zc->zc_nvlist_dst != 0) {
1662 if ((err = put_nvlist(zc, config)) != 0)
1666 nvlist_free(config);
1674 zfs_ioc_pool_export(zfs_cmd_t *zc)
1677 boolean_t force = (boolean_t)zc->zc_cookie;
1678 boolean_t hardforce = (boolean_t)zc->zc_guid;
1680 zfs_log_history(zc);
1681 error = spa_export(zc->zc_name, NULL, force, hardforce);
1683 zvol_remove_minors(zc->zc_name);
1688 zfs_ioc_pool_configs(zfs_cmd_t *zc)
1693 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1694 return (SET_ERROR(EEXIST));
1696 error = put_nvlist(zc, configs);
1698 nvlist_free(configs);
1705 * zc_name name of the pool
1708 * zc_cookie real errno
1709 * zc_nvlist_dst config nvlist
1710 * zc_nvlist_dst_size size of config nvlist
1713 zfs_ioc_pool_stats(zfs_cmd_t *zc)
1719 error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1720 sizeof (zc->zc_value));
1722 if (config != NULL) {
1723 ret = put_nvlist(zc, config);
1724 nvlist_free(config);
1727 * The config may be present even if 'error' is non-zero.
1728 * In this case we return success, and preserve the real errno
1731 zc->zc_cookie = error;
1740 * Try to import the given pool, returning pool stats as appropriate so that
1741 * user land knows which devices are available and overall pool health.
1744 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1746 nvlist_t *tryconfig, *config;
1749 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1750 zc->zc_iflags, &tryconfig)) != 0)
1753 config = spa_tryimport(tryconfig);
1755 nvlist_free(tryconfig);
1758 return (SET_ERROR(EINVAL));
1760 error = put_nvlist(zc, config);
1761 nvlist_free(config);
1768 * zc_name name of the pool
1769 * zc_cookie scan func (pool_scan_func_t)
1770 * zc_flags scrub pause/resume flag (pool_scrub_cmd_t)
1773 zfs_ioc_pool_scan(zfs_cmd_t *zc)
1778 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1781 if (zc->zc_flags >= POOL_SCRUB_FLAGS_END)
1782 return (SET_ERROR(EINVAL));
1784 if (zc->zc_flags == POOL_SCRUB_PAUSE)
1785 error = spa_scrub_pause_resume(spa, POOL_SCRUB_PAUSE);
1786 else if (zc->zc_cookie == POOL_SCAN_NONE)
1787 error = spa_scan_stop(spa);
1789 error = spa_scan(spa, zc->zc_cookie);
1791 spa_close(spa, FTAG);
1797 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1802 error = spa_open(zc->zc_name, &spa, FTAG);
1805 spa_close(spa, FTAG);
1811 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1816 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1819 if (zc->zc_cookie < spa_version(spa) ||
1820 !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1821 spa_close(spa, FTAG);
1822 return (SET_ERROR(EINVAL));
1825 spa_upgrade(spa, zc->zc_cookie);
1826 spa_close(spa, FTAG);
1832 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1839 if ((size = zc->zc_history_len) == 0)
1840 return (SET_ERROR(EINVAL));
1842 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1845 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1846 spa_close(spa, FTAG);
1847 return (SET_ERROR(ENOTSUP));
1850 hist_buf = kmem_alloc(size, KM_SLEEP);
1851 if ((error = spa_history_get(spa, &zc->zc_history_offset,
1852 &zc->zc_history_len, hist_buf)) == 0) {
1853 error = ddi_copyout(hist_buf,
1854 (void *)(uintptr_t)zc->zc_history,
1855 zc->zc_history_len, zc->zc_iflags);
1858 spa_close(spa, FTAG);
1859 kmem_free(hist_buf, size);
1864 zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1869 error = spa_open(zc->zc_name, &spa, FTAG);
1871 error = spa_change_guid(spa);
1872 spa_close(spa, FTAG);
1878 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1880 return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
1885 * zc_name name of filesystem
1886 * zc_obj object to find
1889 * zc_value name of object
1892 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1897 /* XXX reading from objset not owned */
1898 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1900 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1901 dmu_objset_rele(os, FTAG);
1902 return (SET_ERROR(EINVAL));
1904 error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
1905 sizeof (zc->zc_value));
1906 dmu_objset_rele(os, FTAG);
1913 * zc_name name of filesystem
1914 * zc_obj object to find
1917 * zc_stat stats on object
1918 * zc_value path to object
1921 zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1926 /* XXX reading from objset not owned */
1927 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1929 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1930 dmu_objset_rele(os, FTAG);
1931 return (SET_ERROR(EINVAL));
1933 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1934 sizeof (zc->zc_value));
1935 dmu_objset_rele(os, FTAG);
1941 zfs_ioc_vdev_add(zfs_cmd_t *zc)
1945 nvlist_t *config, **l2cache, **spares;
1946 uint_t nl2cache = 0, nspares = 0;
1948 error = spa_open(zc->zc_name, &spa, FTAG);
1952 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1953 zc->zc_iflags, &config);
1954 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1955 &l2cache, &nl2cache);
1957 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1962 * A root pool with concatenated devices is not supported.
1963 * Thus, can not add a device to a root pool.
1965 * Intent log device can not be added to a rootpool because
1966 * during mountroot, zil is replayed, a seperated log device
1967 * can not be accessed during the mountroot time.
1969 * l2cache and spare devices are ok to be added to a rootpool.
1971 if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
1972 nvlist_free(config);
1973 spa_close(spa, FTAG);
1974 return (SET_ERROR(EDOM));
1976 #endif /* illumos */
1979 error = spa_vdev_add(spa, config);
1980 nvlist_free(config);
1982 spa_close(spa, FTAG);
1988 * zc_name name of the pool
1989 * zc_guid guid of vdev to remove
1990 * zc_cookie cancel removal
1993 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1998 error = spa_open(zc->zc_name, &spa, FTAG);
2001 if (zc->zc_cookie != 0) {
2002 error = spa_vdev_remove_cancel(spa);
2004 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
2006 spa_close(spa, FTAG);
2011 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
2015 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
2017 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2019 switch (zc->zc_cookie) {
2020 case VDEV_STATE_ONLINE:
2021 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
2024 case VDEV_STATE_OFFLINE:
2025 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
2028 case VDEV_STATE_FAULTED:
2029 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
2030 zc->zc_obj != VDEV_AUX_EXTERNAL)
2031 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
2033 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
2036 case VDEV_STATE_DEGRADED:
2037 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
2038 zc->zc_obj != VDEV_AUX_EXTERNAL)
2039 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
2041 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
2045 error = SET_ERROR(EINVAL);
2047 zc->zc_cookie = newstate;
2048 spa_close(spa, FTAG);
2053 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
2056 int replacing = zc->zc_cookie;
2060 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2063 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
2064 zc->zc_iflags, &config)) == 0) {
2065 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
2066 nvlist_free(config);
2069 spa_close(spa, FTAG);
2074 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
2079 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2082 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
2084 spa_close(spa, FTAG);
2089 zfs_ioc_vdev_split(zfs_cmd_t *zc)
2092 nvlist_t *config, *props = NULL;
2094 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
2096 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2099 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
2100 zc->zc_iflags, &config)) {
2101 spa_close(spa, FTAG);
2105 if (zc->zc_nvlist_src_size != 0 && (error =
2106 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2107 zc->zc_iflags, &props))) {
2108 spa_close(spa, FTAG);
2109 nvlist_free(config);
2113 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
2115 spa_close(spa, FTAG);
2117 nvlist_free(config);
2124 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
2127 char *path = zc->zc_value;
2128 uint64_t guid = zc->zc_guid;
2131 error = spa_open(zc->zc_name, &spa, FTAG);
2135 error = spa_vdev_setpath(spa, guid, path);
2136 spa_close(spa, FTAG);
2141 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
2144 char *fru = zc->zc_value;
2145 uint64_t guid = zc->zc_guid;
2148 error = spa_open(zc->zc_name, &spa, FTAG);
2152 error = spa_vdev_setfru(spa, guid, fru);
2153 spa_close(spa, FTAG);
2158 zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
2163 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2165 if (zc->zc_nvlist_dst != 0 &&
2166 (error = dsl_prop_get_all(os, &nv)) == 0) {
2167 dmu_objset_stats(os, nv);
2169 * NB: zvol_get_stats() will read the objset contents,
2170 * which we aren't supposed to do with a
2171 * DS_MODE_USER hold, because it could be
2172 * inconsistent. So this is a bit of a workaround...
2173 * XXX reading with out owning
2175 if (!zc->zc_objset_stats.dds_inconsistent &&
2176 dmu_objset_type(os) == DMU_OST_ZVOL) {
2177 error = zvol_get_stats(os, nv);
2182 error = put_nvlist(zc, nv);
2191 * zc_name name of filesystem
2192 * zc_nvlist_dst_size size of buffer for property nvlist
2195 * zc_objset_stats stats
2196 * zc_nvlist_dst property nvlist
2197 * zc_nvlist_dst_size size of property nvlist
2200 zfs_ioc_objset_stats(zfs_cmd_t *zc)
2205 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2207 error = zfs_ioc_objset_stats_impl(zc, os);
2208 dmu_objset_rele(os, FTAG);
2211 if (error == ENOMEM)
2218 * zc_name name of filesystem
2219 * zc_nvlist_dst_size size of buffer for property nvlist
2222 * zc_nvlist_dst received property nvlist
2223 * zc_nvlist_dst_size size of received property nvlist
2225 * Gets received properties (distinct from local properties on or after
2226 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
2227 * local property values.
2230 zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
2236 * Without this check, we would return local property values if the
2237 * caller has not already received properties on or after
2238 * SPA_VERSION_RECVD_PROPS.
2240 if (!dsl_prop_get_hasrecvd(zc->zc_name))
2241 return (SET_ERROR(ENOTSUP));
2243 if (zc->zc_nvlist_dst != 0 &&
2244 (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
2245 error = put_nvlist(zc, nv);
2253 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2259 * zfs_get_zplprop() will either find a value or give us
2260 * the default value (if there is one).
2262 if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2264 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2270 * zc_name name of filesystem
2271 * zc_nvlist_dst_size size of buffer for zpl property nvlist
2274 * zc_nvlist_dst zpl property nvlist
2275 * zc_nvlist_dst_size size of zpl property nvlist
2278 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2283 /* XXX reading without owning */
2284 if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
2287 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2290 * NB: nvl_add_zplprop() will read the objset contents,
2291 * which we aren't supposed to do with a DS_MODE_USER
2292 * hold, because it could be inconsistent.
2294 if (zc->zc_nvlist_dst != 0 &&
2295 !zc->zc_objset_stats.dds_inconsistent &&
2296 dmu_objset_type(os) == DMU_OST_ZFS) {
2299 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2300 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2301 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2302 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2303 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2304 err = put_nvlist(zc, nv);
2307 err = SET_ERROR(ENOENT);
2309 dmu_objset_rele(os, FTAG);
2314 dataset_name_hidden(const char *name)
2317 * Skip over datasets that are not visible in this zone,
2318 * internal datasets (which have a $ in their name), and
2319 * temporary datasets (which have a % in their name).
2321 if (strchr(name, '$') != NULL)
2323 if (strchr(name, '%') != NULL)
2325 if (!INGLOBALZONE(curthread) && !zone_dataset_visible(name, NULL))
2332 * zc_name name of filesystem
2333 * zc_cookie zap cursor
2334 * zc_nvlist_dst_size size of buffer for property nvlist
2337 * zc_name name of next filesystem
2338 * zc_cookie zap cursor
2339 * zc_objset_stats stats
2340 * zc_nvlist_dst property nvlist
2341 * zc_nvlist_dst_size size of property nvlist
2344 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2349 size_t orig_len = strlen(zc->zc_name);
2352 if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
2353 if (error == ENOENT)
2354 error = SET_ERROR(ESRCH);
2358 p = strrchr(zc->zc_name, '/');
2359 if (p == NULL || p[1] != '\0')
2360 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2361 p = zc->zc_name + strlen(zc->zc_name);
2364 error = dmu_dir_list_next(os,
2365 sizeof (zc->zc_name) - (p - zc->zc_name), p,
2366 NULL, &zc->zc_cookie);
2367 if (error == ENOENT)
2368 error = SET_ERROR(ESRCH);
2369 } while (error == 0 && dataset_name_hidden(zc->zc_name));
2370 dmu_objset_rele(os, FTAG);
2373 * If it's an internal dataset (ie. with a '$' in its name),
2374 * don't try to get stats for it, otherwise we'll return ENOENT.
2376 if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
2377 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2378 if (error == ENOENT) {
2379 /* We lost a race with destroy, get the next one. */
2380 zc->zc_name[orig_len] = '\0';
2389 * zc_name name of filesystem
2390 * zc_cookie zap cursor
2391 * zc_nvlist_dst_size size of buffer for property nvlist
2392 * zc_simple when set, only name is requested
2395 * zc_name name of next snapshot
2396 * zc_objset_stats stats
2397 * zc_nvlist_dst property nvlist
2398 * zc_nvlist_dst_size size of property nvlist
2401 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2406 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2408 return (error == ENOENT ? ESRCH : error);
2412 * A dataset name of maximum length cannot have any snapshots,
2413 * so exit immediately.
2415 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >=
2416 ZFS_MAX_DATASET_NAME_LEN) {
2417 dmu_objset_rele(os, FTAG);
2418 return (SET_ERROR(ESRCH));
2421 error = dmu_snapshot_list_next(os,
2422 sizeof (zc->zc_name) - strlen(zc->zc_name),
2423 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2426 if (error == 0 && !zc->zc_simple) {
2428 dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2430 error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
2434 error = dmu_objset_from_ds(ds, &ossnap);
2436 error = zfs_ioc_objset_stats_impl(zc, ossnap);
2437 dsl_dataset_rele(ds, FTAG);
2439 } else if (error == ENOENT) {
2440 error = SET_ERROR(ESRCH);
2443 dmu_objset_rele(os, FTAG);
2444 /* if we failed, undo the @ that we tacked on to zc_name */
2446 *strchr(zc->zc_name, '@') = '\0';
2451 zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2453 const char *propname = nvpair_name(pair);
2455 unsigned int vallen;
2458 zfs_userquota_prop_t type;
2464 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2466 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2467 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2469 return (SET_ERROR(EINVAL));
2473 * A correctly constructed propname is encoded as
2474 * userquota@<rid>-<domain>.
2476 if ((dash = strchr(propname, '-')) == NULL ||
2477 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2479 return (SET_ERROR(EINVAL));
2486 err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
2488 err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
2489 zfsvfs_rele(zfsvfs, FTAG);
2496 * If the named property is one that has a special function to set its value,
2497 * return 0 on success and a positive error code on failure; otherwise if it is
2498 * not one of the special properties handled by this function, return -1.
2500 * XXX: It would be better for callers of the property interface if we handled
2501 * these special cases in dsl_prop.c (in the dsl layer).
2504 zfs_prop_set_special(const char *dsname, zprop_source_t source,
2507 const char *propname = nvpair_name(pair);
2508 zfs_prop_t prop = zfs_name_to_prop(propname);
2512 if (prop == ZPROP_INVAL) {
2513 if (zfs_prop_userquota(propname))
2514 return (zfs_prop_set_userquota(dsname, pair));
2518 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2520 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2521 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2525 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
2528 VERIFY(0 == nvpair_value_uint64(pair, &intval));
2531 case ZFS_PROP_QUOTA:
2532 err = dsl_dir_set_quota(dsname, source, intval);
2534 case ZFS_PROP_REFQUOTA:
2535 err = dsl_dataset_set_refquota(dsname, source, intval);
2537 case ZFS_PROP_FILESYSTEM_LIMIT:
2538 case ZFS_PROP_SNAPSHOT_LIMIT:
2539 if (intval == UINT64_MAX) {
2540 /* clearing the limit, just do it */
2543 err = dsl_dir_activate_fs_ss_limit(dsname);
2546 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2547 * default path to set the value in the nvlist.
2552 case ZFS_PROP_RESERVATION:
2553 err = dsl_dir_set_reservation(dsname, source, intval);
2555 case ZFS_PROP_REFRESERVATION:
2556 err = dsl_dataset_set_refreservation(dsname, source, intval);
2558 case ZFS_PROP_VOLSIZE:
2559 err = zvol_set_volsize(dsname, intval);
2561 case ZFS_PROP_VERSION:
2565 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2568 err = zfs_set_version(zfsvfs, intval);
2569 zfsvfs_rele(zfsvfs, FTAG);
2571 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2574 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2575 (void) strcpy(zc->zc_name, dsname);
2576 (void) zfs_ioc_userspace_upgrade(zc);
2577 kmem_free(zc, sizeof (zfs_cmd_t));
2589 * This function is best effort. If it fails to set any of the given properties,
2590 * it continues to set as many as it can and returns the last error
2591 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2592 * with the list of names of all the properties that failed along with the
2593 * corresponding error numbers.
2595 * If every property is set successfully, zero is returned and errlist is not
2599 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2607 nvlist_t *genericnvl = fnvlist_alloc();
2608 nvlist_t *retrynvl = fnvlist_alloc();
2612 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2613 const char *propname = nvpair_name(pair);
2614 zfs_prop_t prop = zfs_name_to_prop(propname);
2617 /* decode the property value */
2619 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2621 attrs = fnvpair_value_nvlist(pair);
2622 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2624 err = SET_ERROR(EINVAL);
2627 /* Validate value type */
2628 if (err == 0 && prop == ZPROP_INVAL) {
2629 if (zfs_prop_user(propname)) {
2630 if (nvpair_type(propval) != DATA_TYPE_STRING)
2631 err = SET_ERROR(EINVAL);
2632 } else if (zfs_prop_userquota(propname)) {
2633 if (nvpair_type(propval) !=
2634 DATA_TYPE_UINT64_ARRAY)
2635 err = SET_ERROR(EINVAL);
2637 err = SET_ERROR(EINVAL);
2639 } else if (err == 0) {
2640 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2641 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2642 err = SET_ERROR(EINVAL);
2643 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2646 intval = fnvpair_value_uint64(propval);
2648 switch (zfs_prop_get_type(prop)) {
2649 case PROP_TYPE_NUMBER:
2651 case PROP_TYPE_STRING:
2652 err = SET_ERROR(EINVAL);
2654 case PROP_TYPE_INDEX:
2655 if (zfs_prop_index_to_string(prop,
2656 intval, &unused) != 0)
2657 err = SET_ERROR(EINVAL);
2661 "unknown property type");
2664 err = SET_ERROR(EINVAL);
2668 /* Validate permissions */
2670 err = zfs_check_settable(dsname, pair, CRED());
2673 err = zfs_prop_set_special(dsname, source, pair);
2676 * For better performance we build up a list of
2677 * properties to set in a single transaction.
2679 err = nvlist_add_nvpair(genericnvl, pair);
2680 } else if (err != 0 && nvl != retrynvl) {
2682 * This may be a spurious error caused by
2683 * receiving quota and reservation out of order.
2684 * Try again in a second pass.
2686 err = nvlist_add_nvpair(retrynvl, pair);
2691 if (errlist != NULL)
2692 fnvlist_add_int32(errlist, propname, err);
2697 if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2702 if (!nvlist_empty(genericnvl) &&
2703 dsl_props_set(dsname, source, genericnvl) != 0) {
2705 * If this fails, we still want to set as many properties as we
2706 * can, so try setting them individually.
2709 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2710 const char *propname = nvpair_name(pair);
2714 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2716 attrs = fnvpair_value_nvlist(pair);
2717 propval = fnvlist_lookup_nvpair(attrs,
2721 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2722 strval = fnvpair_value_string(propval);
2723 err = dsl_prop_set_string(dsname, propname,
2726 intval = fnvpair_value_uint64(propval);
2727 err = dsl_prop_set_int(dsname, propname, source,
2732 if (errlist != NULL) {
2733 fnvlist_add_int32(errlist, propname,
2740 nvlist_free(genericnvl);
2741 nvlist_free(retrynvl);
2747 * Check that all the properties are valid user properties.
2750 zfs_check_userprops(const char *fsname, nvlist_t *nvl)
2752 nvpair_t *pair = NULL;
2755 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2756 const char *propname = nvpair_name(pair);
2758 if (!zfs_prop_user(propname) ||
2759 nvpair_type(pair) != DATA_TYPE_STRING)
2760 return (SET_ERROR(EINVAL));
2762 if (error = zfs_secpolicy_write_perms(fsname,
2763 ZFS_DELEG_PERM_USERPROP, CRED()))
2766 if (strlen(propname) >= ZAP_MAXNAMELEN)
2767 return (SET_ERROR(ENAMETOOLONG));
2769 if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN)
2776 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2780 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2783 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2784 if (nvlist_exists(skipped, nvpair_name(pair)))
2787 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2792 clear_received_props(const char *dsname, nvlist_t *props,
2796 nvlist_t *cleared_props = NULL;
2797 props_skip(props, skipped, &cleared_props);
2798 if (!nvlist_empty(cleared_props)) {
2800 * Acts on local properties until the dataset has received
2801 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2803 zprop_source_t flags = (ZPROP_SRC_NONE |
2804 (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
2805 err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
2807 nvlist_free(cleared_props);
2813 * zc_name name of filesystem
2814 * zc_value name of property to set
2815 * zc_nvlist_src{_size} nvlist of properties to apply
2816 * zc_cookie received properties flag
2819 * zc_nvlist_dst{_size} error for each unapplied received property
2822 zfs_ioc_set_prop(zfs_cmd_t *zc)
2825 boolean_t received = zc->zc_cookie;
2826 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2831 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2832 zc->zc_iflags, &nvl)) != 0)
2836 nvlist_t *origprops;
2838 if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
2839 (void) clear_received_props(zc->zc_name,
2841 nvlist_free(origprops);
2844 error = dsl_prop_set_hasrecvd(zc->zc_name);
2847 errors = fnvlist_alloc();
2849 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
2851 if (zc->zc_nvlist_dst != 0 && errors != NULL) {
2852 (void) put_nvlist(zc, errors);
2855 nvlist_free(errors);
2862 * zc_name name of filesystem
2863 * zc_value name of property to inherit
2864 * zc_cookie revert to received value if TRUE
2869 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2871 const char *propname = zc->zc_value;
2872 zfs_prop_t prop = zfs_name_to_prop(propname);
2873 boolean_t received = zc->zc_cookie;
2874 zprop_source_t source = (received
2875 ? ZPROP_SRC_NONE /* revert to received value, if any */
2876 : ZPROP_SRC_INHERITED); /* explicitly inherit */
2885 * zfs_prop_set_special() expects properties in the form of an
2886 * nvpair with type info.
2888 if (prop == ZPROP_INVAL) {
2889 if (!zfs_prop_user(propname))
2890 return (SET_ERROR(EINVAL));
2892 type = PROP_TYPE_STRING;
2893 } else if (prop == ZFS_PROP_VOLSIZE ||
2894 prop == ZFS_PROP_VERSION) {
2895 return (SET_ERROR(EINVAL));
2897 type = zfs_prop_get_type(prop);
2900 VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2903 case PROP_TYPE_STRING:
2904 VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2906 case PROP_TYPE_NUMBER:
2907 case PROP_TYPE_INDEX:
2908 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2912 return (SET_ERROR(EINVAL));
2915 pair = nvlist_next_nvpair(dummy, NULL);
2916 err = zfs_prop_set_special(zc->zc_name, source, pair);
2919 return (err); /* special property already handled */
2922 * Only check this in the non-received case. We want to allow
2923 * 'inherit -S' to revert non-inheritable properties like quota
2924 * and reservation to the received or default values even though
2925 * they are not considered inheritable.
2927 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2928 return (SET_ERROR(EINVAL));
2931 /* property name has been validated by zfs_secpolicy_inherit_prop() */
2932 return (dsl_prop_inherit(zc->zc_name, zc->zc_value, source));
2936 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2943 if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2944 zc->zc_iflags, &props))
2948 * If the only property is the configfile, then just do a spa_lookup()
2949 * to handle the faulted case.
2951 pair = nvlist_next_nvpair(props, NULL);
2952 if (pair != NULL && strcmp(nvpair_name(pair),
2953 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2954 nvlist_next_nvpair(props, pair) == NULL) {
2955 mutex_enter(&spa_namespace_lock);
2956 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2957 spa_configfile_set(spa, props, B_FALSE);
2958 spa_write_cachefile(spa, B_FALSE, B_TRUE);
2960 mutex_exit(&spa_namespace_lock);
2967 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2972 error = spa_prop_set(spa, props);
2975 spa_close(spa, FTAG);
2981 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2985 nvlist_t *nvp = NULL;
2987 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2989 * If the pool is faulted, there may be properties we can still
2990 * get (such as altroot and cachefile), so attempt to get them
2993 mutex_enter(&spa_namespace_lock);
2994 if ((spa = spa_lookup(zc->zc_name)) != NULL)
2995 error = spa_prop_get(spa, &nvp);
2996 mutex_exit(&spa_namespace_lock);
2998 error = spa_prop_get(spa, &nvp);
2999 spa_close(spa, FTAG);
3002 if (error == 0 && zc->zc_nvlist_dst != 0)
3003 error = put_nvlist(zc, nvp);
3005 error = SET_ERROR(EFAULT);
3013 * zc_name name of filesystem
3014 * zc_nvlist_src{_size} nvlist of delegated permissions
3015 * zc_perm_action allow/unallow flag
3020 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
3023 nvlist_t *fsaclnv = NULL;
3025 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3026 zc->zc_iflags, &fsaclnv)) != 0)
3030 * Verify nvlist is constructed correctly
3032 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
3033 nvlist_free(fsaclnv);
3034 return (SET_ERROR(EINVAL));
3038 * If we don't have PRIV_SYS_MOUNT, then validate
3039 * that user is allowed to hand out each permission in
3043 error = secpolicy_zfs(CRED());
3045 if (zc->zc_perm_action == B_FALSE) {
3046 error = dsl_deleg_can_allow(zc->zc_name,
3049 error = dsl_deleg_can_unallow(zc->zc_name,
3055 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
3057 nvlist_free(fsaclnv);
3063 * zc_name name of filesystem
3066 * zc_nvlist_src{_size} nvlist of delegated permissions
3069 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
3074 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
3075 error = put_nvlist(zc, nvp);
3084 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3086 zfs_creat_t *zct = arg;
3088 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
3091 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
3095 * os parent objset pointer (NULL if root fs)
3096 * fuids_ok fuids allowed in this version of the spa?
3097 * sa_ok SAs allowed in this version of the spa?
3098 * createprops list of properties requested by creator
3101 * zplprops values for the zplprops we attach to the master node object
3102 * is_ci true if requested file system will be purely case-insensitive
3104 * Determine the settings for utf8only, normalization and
3105 * casesensitivity. Specific values may have been requested by the
3106 * creator and/or we can inherit values from the parent dataset. If
3107 * the file system is of too early a vintage, a creator can not
3108 * request settings for these properties, even if the requested
3109 * setting is the default value. We don't actually want to create dsl
3110 * properties for these, so remove them from the source nvlist after
3114 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
3115 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
3116 nvlist_t *zplprops, boolean_t *is_ci)
3118 uint64_t sense = ZFS_PROP_UNDEFINED;
3119 uint64_t norm = ZFS_PROP_UNDEFINED;
3120 uint64_t u8 = ZFS_PROP_UNDEFINED;
3122 ASSERT(zplprops != NULL);
3124 if (os != NULL && os->os_phys->os_type != DMU_OST_ZFS)
3125 return (SET_ERROR(EINVAL));
3128 * Pull out creator prop choices, if any.
3131 (void) nvlist_lookup_uint64(createprops,
3132 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
3133 (void) nvlist_lookup_uint64(createprops,
3134 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
3135 (void) nvlist_remove_all(createprops,
3136 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
3137 (void) nvlist_lookup_uint64(createprops,
3138 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
3139 (void) nvlist_remove_all(createprops,
3140 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
3141 (void) nvlist_lookup_uint64(createprops,
3142 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
3143 (void) nvlist_remove_all(createprops,
3144 zfs_prop_to_name(ZFS_PROP_CASE));
3148 * If the zpl version requested is whacky or the file system
3149 * or pool is version is too "young" to support normalization
3150 * and the creator tried to set a value for one of the props,
3153 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
3154 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
3155 (zplver >= ZPL_VERSION_SA && !sa_ok) ||
3156 (zplver < ZPL_VERSION_NORMALIZATION &&
3157 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
3158 sense != ZFS_PROP_UNDEFINED)))
3159 return (SET_ERROR(ENOTSUP));
3162 * Put the version in the zplprops
3164 VERIFY(nvlist_add_uint64(zplprops,
3165 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3167 if (norm == ZFS_PROP_UNDEFINED)
3168 VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
3169 VERIFY(nvlist_add_uint64(zplprops,
3170 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3173 * If we're normalizing, names must always be valid UTF-8 strings.
3177 if (u8 == ZFS_PROP_UNDEFINED)
3178 VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
3179 VERIFY(nvlist_add_uint64(zplprops,
3180 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3182 if (sense == ZFS_PROP_UNDEFINED)
3183 VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
3184 VERIFY(nvlist_add_uint64(zplprops,
3185 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3188 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
3194 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
3195 nvlist_t *zplprops, boolean_t *is_ci)
3197 boolean_t fuids_ok, sa_ok;
3198 uint64_t zplver = ZPL_VERSION;
3199 objset_t *os = NULL;
3200 char parentname[ZFS_MAX_DATASET_NAME_LEN];
3206 (void) strlcpy(parentname, dataset, sizeof (parentname));
3207 cp = strrchr(parentname, '/');
3211 if ((error = spa_open(dataset, &spa, FTAG)) != 0)
3214 spa_vers = spa_version(spa);
3215 spa_close(spa, FTAG);
3217 zplver = zfs_zpl_version_map(spa_vers);
3218 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3219 sa_ok = (zplver >= ZPL_VERSION_SA);
3222 * Open parent object set so we can inherit zplprop values.
3224 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
3227 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
3229 dmu_objset_rele(os, FTAG);
3234 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
3235 nvlist_t *zplprops, boolean_t *is_ci)
3239 uint64_t zplver = ZPL_VERSION;
3242 zplver = zfs_zpl_version_map(spa_vers);
3243 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3244 sa_ok = (zplver >= ZPL_VERSION_SA);
3246 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
3247 createprops, zplprops, is_ci);
3253 * "type" -> dmu_objset_type_t (int32)
3254 * (optional) "props" -> { prop -> value }
3257 * outnvl: propname -> error code (int32)
3260 zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3263 zfs_creat_t zct = { 0 };
3264 nvlist_t *nvprops = NULL;
3265 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
3267 dmu_objset_type_t type;
3268 boolean_t is_insensitive = B_FALSE;
3270 if (nvlist_lookup_int32(innvl, "type", &type32) != 0)
3271 return (SET_ERROR(EINVAL));
3273 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3277 cbfunc = zfs_create_cb;
3281 cbfunc = zvol_create_cb;
3288 if (strchr(fsname, '@') ||
3289 strchr(fsname, '%'))
3290 return (SET_ERROR(EINVAL));
3292 zct.zct_props = nvprops;
3295 return (SET_ERROR(EINVAL));
3297 if (type == DMU_OST_ZVOL) {
3298 uint64_t volsize, volblocksize;
3300 if (nvprops == NULL)
3301 return (SET_ERROR(EINVAL));
3302 if (nvlist_lookup_uint64(nvprops,
3303 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3304 return (SET_ERROR(EINVAL));
3306 if ((error = nvlist_lookup_uint64(nvprops,
3307 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3308 &volblocksize)) != 0 && error != ENOENT)
3309 return (SET_ERROR(EINVAL));
3312 volblocksize = zfs_prop_default_numeric(
3313 ZFS_PROP_VOLBLOCKSIZE);
3315 if ((error = zvol_check_volblocksize(
3316 volblocksize)) != 0 ||
3317 (error = zvol_check_volsize(volsize,
3318 volblocksize)) != 0)
3320 } else if (type == DMU_OST_ZFS) {
3324 * We have to have normalization and
3325 * case-folding flags correct when we do the
3326 * file system creation, so go figure them out
3329 VERIFY(nvlist_alloc(&zct.zct_zplprops,
3330 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3331 error = zfs_fill_zplprops(fsname, nvprops,
3332 zct.zct_zplprops, &is_insensitive);
3334 nvlist_free(zct.zct_zplprops);
3339 error = dmu_objset_create(fsname, type,
3340 is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
3341 nvlist_free(zct.zct_zplprops);
3344 * It would be nice to do this atomically.
3347 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3350 (void) dsl_destroy_head(fsname);
3353 if (error == 0 && type == DMU_OST_ZVOL)
3354 zvol_create_minors(fsname);
3361 * "origin" -> name of origin snapshot
3362 * (optional) "props" -> { prop -> value }
3365 * outnvl: propname -> error code (int32)
3368 zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3371 nvlist_t *nvprops = NULL;
3374 if (nvlist_lookup_string(innvl, "origin", &origin_name) != 0)
3375 return (SET_ERROR(EINVAL));
3376 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3378 if (strchr(fsname, '@') ||
3379 strchr(fsname, '%'))
3380 return (SET_ERROR(EINVAL));
3382 if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3383 return (SET_ERROR(EINVAL));
3384 error = dmu_objset_clone(fsname, origin_name);
3389 * It would be nice to do this atomically.
3392 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3395 (void) dsl_destroy_head(fsname);
3399 zvol_create_minors(fsname);
3406 zfs_ioc_remap(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3408 if (strchr(fsname, '@') ||
3409 strchr(fsname, '%'))
3410 return (SET_ERROR(EINVAL));
3412 return (dmu_objset_remap_indirects(fsname));
3417 * "snaps" -> { snapshot1, snapshot2 }
3418 * (optional) "props" -> { prop -> value (string) }
3421 * outnvl: snapshot -> error code (int32)
3424 zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3427 nvlist_t *props = NULL;
3431 (void) nvlist_lookup_nvlist(innvl, "props", &props);
3432 if ((error = zfs_check_userprops(poolname, props)) != 0)
3435 if (!nvlist_empty(props) &&
3436 zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3437 return (SET_ERROR(ENOTSUP));
3439 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3440 return (SET_ERROR(EINVAL));
3441 poollen = strlen(poolname);
3442 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3443 pair = nvlist_next_nvpair(snaps, pair)) {
3444 const char *name = nvpair_name(pair);
3445 const char *cp = strchr(name, '@');
3448 * The snap name must contain an @, and the part after it must
3449 * contain only valid characters.
3452 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3453 return (SET_ERROR(EINVAL));
3456 * The snap must be in the specified pool.
3458 if (strncmp(name, poolname, poollen) != 0 ||
3459 (name[poollen] != '/' && name[poollen] != '@'))
3460 return (SET_ERROR(EXDEV));
3462 /* This must be the only snap of this fs. */
3463 for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
3464 pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
3465 if (strncmp(name, nvpair_name(pair2), cp - name + 1)
3467 return (SET_ERROR(EXDEV));
3472 error = dsl_dataset_snapshot(snaps, props, outnvl);
3477 * innvl: "message" -> string
3481 zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
3489 * The poolname in the ioctl is not set, we get it from the TSD,
3490 * which was set at the end of the last successful ioctl that allows
3491 * logging. The secpolicy func already checked that it is set.
3492 * Only one log ioctl is allowed after each successful ioctl, so
3493 * we clear the TSD here.
3495 poolname = tsd_get(zfs_allow_log_key);
3496 (void) tsd_set(zfs_allow_log_key, NULL);
3497 error = spa_open(poolname, &spa, FTAG);
3502 if (nvlist_lookup_string(innvl, "message", &message) != 0) {
3503 spa_close(spa, FTAG);
3504 return (SET_ERROR(EINVAL));
3507 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
3508 spa_close(spa, FTAG);
3509 return (SET_ERROR(ENOTSUP));
3512 error = spa_history_log(spa, message);
3513 spa_close(spa, FTAG);
3519 zfs_ioc_nextboot(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
3521 char name[MAXNAMELEN];
3529 if (nvlist_lookup_uint64(innvl,
3530 ZPOOL_CONFIG_POOL_GUID, &pool_guid) != 0)
3532 if (nvlist_lookup_uint64(innvl,
3533 ZPOOL_CONFIG_GUID, &vdev_guid) != 0)
3535 if (nvlist_lookup_string(innvl,
3536 "command", &command) != 0)
3539 mutex_enter(&spa_namespace_lock);
3540 spa = spa_by_guid(pool_guid, vdev_guid);
3542 strcpy(name, spa_name(spa));
3543 mutex_exit(&spa_namespace_lock);
3547 if ((error = spa_open(name, &spa, FTAG)) != 0)
3549 spa_vdev_state_enter(spa, SCL_ALL);
3550 vd = spa_lookup_by_guid(spa, vdev_guid, B_TRUE);
3552 (void) spa_vdev_state_exit(spa, NULL, ENXIO);
3553 spa_close(spa, FTAG);
3556 error = vdev_label_write_pad2(vd, command, strlen(command));
3557 (void) spa_vdev_state_exit(spa, NULL, 0);
3558 txg_wait_synced(spa->spa_dsl_pool, 0);
3559 spa_close(spa, FTAG);
3565 * The dp_config_rwlock must not be held when calling this, because the
3566 * unmount may need to write out data.
3568 * This function is best-effort. Callers must deal gracefully if it
3569 * remains mounted (or is remounted after this call).
3571 * Returns 0 if the argument is not a snapshot, or it is not currently a
3572 * filesystem, or we were able to unmount it. Returns error code otherwise.
3575 zfs_unmount_snap(const char *snapname)
3578 zfsvfs_t *zfsvfs = NULL;
3580 if (strchr(snapname, '@') == NULL)
3583 int err = getzfsvfs(snapname, &zfsvfs);
3585 ASSERT3P(zfsvfs, ==, NULL);
3588 vfsp = zfsvfs->z_vfs;
3590 ASSERT(!dsl_pool_config_held(dmu_objset_pool(zfsvfs->z_os)));
3593 err = vn_vfswlock(vfsp->vfs_vnodecovered);
3600 * Always force the unmount for snapshots.
3603 (void) dounmount(vfsp, MS_FORCE, kcred);
3607 (void) dounmount(vfsp, MS_FORCE, curthread);
3613 zfs_unmount_snap_cb(const char *snapname, void *arg)
3615 zfs_unmount_snap(snapname);
3620 * When a clone is destroyed, its origin may also need to be destroyed,
3621 * in which case it must be unmounted. This routine will do that unmount
3625 zfs_destroy_unmount_origin(const char *fsname)
3631 error = dmu_objset_hold(fsname, FTAG, &os);
3634 ds = dmu_objset_ds(os);
3635 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
3636 char originname[ZFS_MAX_DATASET_NAME_LEN];
3637 dsl_dataset_name(ds->ds_prev, originname);
3638 dmu_objset_rele(os, FTAG);
3639 zfs_unmount_snap(originname);
3641 dmu_objset_rele(os, FTAG);
3647 * "snaps" -> { snapshot1, snapshot2 }
3648 * (optional boolean) "defer"
3651 * outnvl: snapshot -> error code (int32)
3656 zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3663 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3664 return (SET_ERROR(EINVAL));
3665 defer = nvlist_exists(innvl, "defer");
3667 poollen = strlen(poolname);
3668 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3669 pair = nvlist_next_nvpair(snaps, pair)) {
3670 const char *name = nvpair_name(pair);
3673 * The snap must be in the specified pool to prevent the
3674 * invalid removal of zvol minors below.
3676 if (strncmp(name, poolname, poollen) != 0 ||
3677 (name[poollen] != '/' && name[poollen] != '@'))
3678 return (SET_ERROR(EXDEV));
3680 zfs_unmount_snap(nvpair_name(pair));
3681 #if defined(__FreeBSD__)
3682 zvol_remove_minors(name);
3686 return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
3690 * Create bookmarks. Bookmark names are of the form <fs>#<bmark>.
3691 * All bookmarks must be in the same pool.
3694 * bookmark1 -> snapshot1, bookmark2 -> snapshot2
3697 * outnvl: bookmark -> error code (int32)
3702 zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3704 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3705 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3709 * Verify the snapshot argument.
3711 if (nvpair_value_string(pair, &snap_name) != 0)
3712 return (SET_ERROR(EINVAL));
3715 /* Verify that the keys (bookmarks) are unique */
3716 for (nvpair_t *pair2 = nvlist_next_nvpair(innvl, pair);
3717 pair2 != NULL; pair2 = nvlist_next_nvpair(innvl, pair2)) {
3718 if (strcmp(nvpair_name(pair), nvpair_name(pair2)) == 0)
3719 return (SET_ERROR(EINVAL));
3723 return (dsl_bookmark_create(innvl, outnvl));
3728 * property 1, property 2, ...
3732 * bookmark name 1 -> { property 1, property 2, ... },
3733 * bookmark name 2 -> { property 1, property 2, ... }
3738 zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3740 return (dsl_get_bookmarks(fsname, innvl, outnvl));
3745 * bookmark name 1, bookmark name 2
3748 * outnvl: bookmark -> error code (int32)
3752 zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl,
3757 poollen = strlen(poolname);
3758 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3759 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3760 const char *name = nvpair_name(pair);
3761 const char *cp = strchr(name, '#');
3764 * The bookmark name must contain an #, and the part after it
3765 * must contain only valid characters.
3768 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3769 return (SET_ERROR(EINVAL));
3772 * The bookmark must be in the specified pool.
3774 if (strncmp(name, poolname, poollen) != 0 ||
3775 (name[poollen] != '/' && name[poollen] != '#'))
3776 return (SET_ERROR(EXDEV));
3779 error = dsl_bookmark_destroy(innvl, outnvl);
3784 zfs_ioc_channel_program(const char *poolname, nvlist_t *innvl,
3788 uint64_t instrlimit, memlimit;
3789 boolean_t sync_flag;
3790 nvpair_t *nvarg = NULL;
3792 if (0 != nvlist_lookup_string(innvl, ZCP_ARG_PROGRAM, &program)) {
3795 if (0 != nvlist_lookup_boolean_value(innvl, ZCP_ARG_SYNC, &sync_flag)) {
3798 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_INSTRLIMIT, &instrlimit)) {
3799 instrlimit = ZCP_DEFAULT_INSTRLIMIT;
3801 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_MEMLIMIT, &memlimit)) {
3802 memlimit = ZCP_DEFAULT_MEMLIMIT;
3804 if (0 != nvlist_lookup_nvpair(innvl, ZCP_ARG_ARGLIST, &nvarg)) {
3808 if (instrlimit == 0 || instrlimit > zfs_lua_max_instrlimit)
3810 if (memlimit == 0 || memlimit > zfs_lua_max_memlimit)
3813 return (zcp_eval(poolname, program, sync_flag, instrlimit, memlimit,
3823 zfs_ioc_pool_checkpoint(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3825 return (spa_checkpoint(poolname));
3834 zfs_ioc_pool_discard_checkpoint(const char *poolname, nvlist_t *innvl,
3837 return (spa_checkpoint_discard(poolname));
3842 * zc_name name of dataset to destroy
3843 * zc_defer_destroy mark for deferred destroy
3848 zfs_ioc_destroy(zfs_cmd_t *zc)
3851 dmu_objset_type_t ost;
3854 err = dmu_objset_hold(zc->zc_name, FTAG, &os);
3857 ost = dmu_objset_type(os);
3858 dmu_objset_rele(os, FTAG);
3860 if (ost == DMU_OST_ZFS)
3861 zfs_unmount_snap(zc->zc_name);
3863 if (strchr(zc->zc_name, '@'))
3864 err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
3866 err = dsl_destroy_head(zc->zc_name);
3867 if (ost == DMU_OST_ZVOL && err == 0)
3869 zvol_remove_minors(zc->zc_name);
3871 (void) zvol_remove_minor(zc->zc_name);
3879 * guid 1, guid 2, ...
3881 * func: POOL_INITIALIZE_{CANCEL|DO|SUSPEND}
3885 * [func: EINVAL (if provided command type didn't make sense)],
3887 * guid1: errno, (see function body for possible errnos)
3894 zfs_ioc_pool_initialize(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3899 error = spa_open(poolname, &spa, FTAG);
3904 if (nvlist_lookup_uint64(innvl, ZPOOL_INITIALIZE_COMMAND,
3906 spa_close(spa, FTAG);
3907 return (SET_ERROR(EINVAL));
3909 if (!(cmd_type == POOL_INITIALIZE_CANCEL ||
3910 cmd_type == POOL_INITIALIZE_DO ||
3911 cmd_type == POOL_INITIALIZE_SUSPEND)) {
3912 spa_close(spa, FTAG);
3913 return (SET_ERROR(EINVAL));
3916 nvlist_t *vdev_guids;
3917 if (nvlist_lookup_nvlist(innvl, ZPOOL_INITIALIZE_VDEVS,
3918 &vdev_guids) != 0) {
3919 spa_close(spa, FTAG);
3920 return (SET_ERROR(EINVAL));
3923 nvlist_t *vdev_errlist = fnvlist_alloc();
3924 int total_errors = 0;
3926 for (nvpair_t *pair = nvlist_next_nvpair(vdev_guids, NULL);
3927 pair != NULL; pair = nvlist_next_nvpair(vdev_guids, pair)) {
3928 uint64_t vdev_guid = fnvpair_value_uint64(pair);
3930 error = spa_vdev_initialize(spa, vdev_guid, cmd_type);
3932 char guid_as_str[MAXNAMELEN];
3934 (void) snprintf(guid_as_str, sizeof (guid_as_str),
3935 "%llu", (unsigned long long)vdev_guid);
3936 fnvlist_add_int64(vdev_errlist, guid_as_str, error);
3940 if (fnvlist_size(vdev_errlist) > 0) {
3941 fnvlist_add_nvlist(outnvl, ZPOOL_INITIALIZE_VDEVS,
3944 fnvlist_free(vdev_errlist);
3946 spa_close(spa, FTAG);
3947 return (total_errors > 0 ? EINVAL : 0);
3951 * fsname is name of dataset to rollback (to most recent snapshot)
3953 * innvl may contain name of expected target snapshot
3955 * outnvl: "target" -> name of most recent snapshot
3960 zfs_ioc_rollback(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3963 char *target = NULL;
3966 (void) nvlist_lookup_string(innvl, "target", &target);
3967 if (target != NULL) {
3968 const char *cp = strchr(target, '@');
3971 * The snap name must contain an @, and the part after it must
3972 * contain only valid characters.
3975 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3976 return (SET_ERROR(EINVAL));
3979 if (getzfsvfs(fsname, &zfsvfs) == 0) {
3982 ds = dmu_objset_ds(zfsvfs->z_os);
3983 error = zfs_suspend_fs(zfsvfs);
3987 error = dsl_dataset_rollback(fsname, target, zfsvfs,
3989 resume_err = zfs_resume_fs(zfsvfs, ds);
3990 error = error ? error : resume_err;
3993 VFS_RELE(zfsvfs->z_vfs);
3995 vfs_unbusy(zfsvfs->z_vfs);
3998 error = dsl_dataset_rollback(fsname, target, NULL, outnvl);
4004 recursive_unmount(const char *fsname, void *arg)
4006 const char *snapname = arg;
4007 char fullname[ZFS_MAX_DATASET_NAME_LEN];
4009 (void) snprintf(fullname, sizeof (fullname), "%s@%s", fsname, snapname);
4010 zfs_unmount_snap(fullname);
4017 * zc_name old name of dataset
4018 * zc_value new name of dataset
4019 * zc_cookie recursive flag (only valid for snapshots)
4024 zfs_ioc_rename(zfs_cmd_t *zc)
4027 dmu_objset_type_t ost;
4028 boolean_t recursive = zc->zc_cookie & 1;
4030 boolean_t allow_mounted = B_TRUE;
4034 allow_mounted = (zc->zc_cookie & 2) != 0;
4037 /* "zfs rename" from and to ...%recv datasets should both fail */
4038 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
4039 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
4040 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
4041 dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4042 strchr(zc->zc_name, '%') || strchr(zc->zc_value, '%'))
4043 return (SET_ERROR(EINVAL));
4045 err = dmu_objset_hold(zc->zc_name, FTAG, &os);
4048 ost = dmu_objset_type(os);
4049 dmu_objset_rele(os, FTAG);
4051 at = strchr(zc->zc_name, '@');
4053 /* snaps must be in same fs */
4056 if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
4057 return (SET_ERROR(EXDEV));
4059 if (ost == DMU_OST_ZFS && !allow_mounted) {
4060 error = dmu_objset_find(zc->zc_name,
4061 recursive_unmount, at + 1,
4062 recursive ? DS_FIND_CHILDREN : 0);
4068 error = dsl_dataset_rename_snapshot(zc->zc_name,
4069 at + 1, strchr(zc->zc_value, '@') + 1, recursive);
4075 if (ost == DMU_OST_ZVOL)
4076 (void) zvol_remove_minor(zc->zc_name);
4078 return (dsl_dir_rename(zc->zc_name, zc->zc_value));
4083 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
4085 const char *propname = nvpair_name(pair);
4086 boolean_t issnap = (strchr(dsname, '@') != NULL);
4087 zfs_prop_t prop = zfs_name_to_prop(propname);
4091 if (prop == ZPROP_INVAL) {
4092 if (zfs_prop_user(propname)) {
4093 if (err = zfs_secpolicy_write_perms(dsname,
4094 ZFS_DELEG_PERM_USERPROP, cr))
4099 if (!issnap && zfs_prop_userquota(propname)) {
4100 const char *perm = NULL;
4101 const char *uq_prefix =
4102 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
4103 const char *gq_prefix =
4104 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
4106 if (strncmp(propname, uq_prefix,
4107 strlen(uq_prefix)) == 0) {
4108 perm = ZFS_DELEG_PERM_USERQUOTA;
4109 } else if (strncmp(propname, gq_prefix,
4110 strlen(gq_prefix)) == 0) {
4111 perm = ZFS_DELEG_PERM_GROUPQUOTA;
4113 /* USERUSED and GROUPUSED are read-only */
4114 return (SET_ERROR(EINVAL));
4117 if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
4122 return (SET_ERROR(EINVAL));
4126 return (SET_ERROR(EINVAL));
4128 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
4130 * dsl_prop_get_all_impl() returns properties in this
4134 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
4135 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4140 * Check that this value is valid for this pool version
4143 case ZFS_PROP_COMPRESSION:
4145 * If the user specified gzip compression, make sure
4146 * the SPA supports it. We ignore any errors here since
4147 * we'll catch them later.
4149 if (nvpair_value_uint64(pair, &intval) == 0) {
4150 if (intval >= ZIO_COMPRESS_GZIP_1 &&
4151 intval <= ZIO_COMPRESS_GZIP_9 &&
4152 zfs_earlier_version(dsname,
4153 SPA_VERSION_GZIP_COMPRESSION)) {
4154 return (SET_ERROR(ENOTSUP));
4157 if (intval == ZIO_COMPRESS_ZLE &&
4158 zfs_earlier_version(dsname,
4159 SPA_VERSION_ZLE_COMPRESSION))
4160 return (SET_ERROR(ENOTSUP));
4162 if (intval == ZIO_COMPRESS_LZ4) {
4165 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4168 if (!spa_feature_is_enabled(spa,
4169 SPA_FEATURE_LZ4_COMPRESS)) {
4170 spa_close(spa, FTAG);
4171 return (SET_ERROR(ENOTSUP));
4173 spa_close(spa, FTAG);
4177 * If this is a bootable dataset then
4178 * verify that the compression algorithm
4179 * is supported for booting. We must return
4180 * something other than ENOTSUP since it
4181 * implies a downrev pool version.
4183 if (zfs_is_bootfs(dsname) &&
4184 !BOOTFS_COMPRESS_VALID(intval)) {
4185 return (SET_ERROR(ERANGE));
4190 case ZFS_PROP_COPIES:
4191 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
4192 return (SET_ERROR(ENOTSUP));
4195 case ZFS_PROP_RECORDSIZE:
4196 /* Record sizes above 128k need the feature to be enabled */
4197 if (nvpair_value_uint64(pair, &intval) == 0 &&
4198 intval > SPA_OLD_MAXBLOCKSIZE) {
4202 * We don't allow setting the property above 1MB,
4203 * unless the tunable has been changed.
4205 if (intval > zfs_max_recordsize ||
4206 intval > SPA_MAXBLOCKSIZE)
4207 return (SET_ERROR(ERANGE));
4209 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4212 if (!spa_feature_is_enabled(spa,
4213 SPA_FEATURE_LARGE_BLOCKS)) {
4214 spa_close(spa, FTAG);
4215 return (SET_ERROR(ENOTSUP));
4217 spa_close(spa, FTAG);
4221 case ZFS_PROP_DNODESIZE:
4222 /* Dnode sizes above 512 need the feature to be enabled */
4223 if (nvpair_value_uint64(pair, &intval) == 0 &&
4224 intval != ZFS_DNSIZE_LEGACY) {
4227 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4230 if (!spa_feature_is_enabled(spa,
4231 SPA_FEATURE_LARGE_DNODE)) {
4232 spa_close(spa, FTAG);
4233 return (SET_ERROR(ENOTSUP));
4235 spa_close(spa, FTAG);
4239 case ZFS_PROP_SHARESMB:
4240 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
4241 return (SET_ERROR(ENOTSUP));
4244 case ZFS_PROP_ACLINHERIT:
4245 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
4246 nvpair_value_uint64(pair, &intval) == 0) {
4247 if (intval == ZFS_ACL_PASSTHROUGH_X &&
4248 zfs_earlier_version(dsname,
4249 SPA_VERSION_PASSTHROUGH_X))
4250 return (SET_ERROR(ENOTSUP));
4254 case ZFS_PROP_CHECKSUM:
4255 case ZFS_PROP_DEDUP:
4257 spa_feature_t feature;
4260 /* dedup feature version checks */
4261 if (prop == ZFS_PROP_DEDUP &&
4262 zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
4263 return (SET_ERROR(ENOTSUP));
4265 if (nvpair_value_uint64(pair, &intval) != 0)
4266 return (SET_ERROR(EINVAL));
4268 /* check prop value is enabled in features */
4269 feature = zio_checksum_to_feature(intval & ZIO_CHECKSUM_MASK);
4270 if (feature == SPA_FEATURE_NONE)
4273 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4276 * Salted checksums are not supported on root pools.
4278 if (spa_bootfs(spa) != 0 &&
4279 intval < ZIO_CHECKSUM_FUNCTIONS &&
4280 (zio_checksum_table[intval].ci_flags &
4281 ZCHECKSUM_FLAG_SALTED)) {
4282 spa_close(spa, FTAG);
4283 return (SET_ERROR(ERANGE));
4285 if (!spa_feature_is_enabled(spa, feature)) {
4286 spa_close(spa, FTAG);
4287 return (SET_ERROR(ENOTSUP));
4289 spa_close(spa, FTAG);
4294 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
4298 * Checks for a race condition to make sure we don't increment a feature flag
4302 zfs_prop_activate_feature_check(void *arg, dmu_tx_t *tx)
4304 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
4305 spa_feature_t *featurep = arg;
4307 if (!spa_feature_is_active(spa, *featurep))
4310 return (SET_ERROR(EBUSY));
4314 * The callback invoked on feature activation in the sync task caused by
4315 * zfs_prop_activate_feature.
4318 zfs_prop_activate_feature_sync(void *arg, dmu_tx_t *tx)
4320 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
4321 spa_feature_t *featurep = arg;
4323 spa_feature_incr(spa, *featurep, tx);
4327 * Activates a feature on a pool in response to a property setting. This
4328 * creates a new sync task which modifies the pool to reflect the feature
4332 zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature)
4336 /* EBUSY here indicates that the feature is already active */
4337 err = dsl_sync_task(spa_name(spa),
4338 zfs_prop_activate_feature_check, zfs_prop_activate_feature_sync,
4339 &feature, 2, ZFS_SPACE_CHECK_RESERVED);
4341 if (err != 0 && err != EBUSY)
4348 * Removes properties from the given props list that fail permission checks
4349 * needed to clear them and to restore them in case of a receive error. For each
4350 * property, make sure we have both set and inherit permissions.
4352 * Returns the first error encountered if any permission checks fail. If the
4353 * caller provides a non-NULL errlist, it also gives the complete list of names
4354 * of all the properties that failed a permission check along with the
4355 * corresponding error numbers. The caller is responsible for freeing the
4358 * If every property checks out successfully, zero is returned and the list
4359 * pointed at by errlist is NULL.
4362 zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
4365 nvpair_t *pair, *next_pair;
4372 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4374 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
4375 (void) strcpy(zc->zc_name, dataset);
4376 pair = nvlist_next_nvpair(props, NULL);
4377 while (pair != NULL) {
4378 next_pair = nvlist_next_nvpair(props, pair);
4380 (void) strcpy(zc->zc_value, nvpair_name(pair));
4381 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
4382 (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
4383 VERIFY(nvlist_remove_nvpair(props, pair) == 0);
4384 VERIFY(nvlist_add_int32(errors,
4385 zc->zc_value, err) == 0);
4389 kmem_free(zc, sizeof (zfs_cmd_t));
4391 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
4392 nvlist_free(errors);
4395 VERIFY(nvpair_value_int32(pair, &rv) == 0);
4398 if (errlist == NULL)
4399 nvlist_free(errors);
4407 propval_equals(nvpair_t *p1, nvpair_t *p2)
4409 if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
4410 /* dsl_prop_get_all_impl() format */
4412 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
4413 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4417 if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
4419 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
4420 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4424 if (nvpair_type(p1) != nvpair_type(p2))
4427 if (nvpair_type(p1) == DATA_TYPE_STRING) {
4428 char *valstr1, *valstr2;
4430 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
4431 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
4432 return (strcmp(valstr1, valstr2) == 0);
4434 uint64_t intval1, intval2;
4436 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
4437 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
4438 return (intval1 == intval2);
4443 * Remove properties from props if they are not going to change (as determined
4444 * by comparison with origprops). Remove them from origprops as well, since we
4445 * do not need to clear or restore properties that won't change.
4448 props_reduce(nvlist_t *props, nvlist_t *origprops)
4450 nvpair_t *pair, *next_pair;
4452 if (origprops == NULL)
4453 return; /* all props need to be received */
4455 pair = nvlist_next_nvpair(props, NULL);
4456 while (pair != NULL) {
4457 const char *propname = nvpair_name(pair);
4460 next_pair = nvlist_next_nvpair(props, pair);
4462 if ((nvlist_lookup_nvpair(origprops, propname,
4463 &match) != 0) || !propval_equals(pair, match))
4464 goto next; /* need to set received value */
4466 /* don't clear the existing received value */
4467 (void) nvlist_remove_nvpair(origprops, match);
4468 /* don't bother receiving the property */
4469 (void) nvlist_remove_nvpair(props, pair);
4476 * Extract properties that cannot be set PRIOR to the receipt of a dataset.
4477 * For example, refquota cannot be set until after the receipt of a dataset,
4478 * because in replication streams, an older/earlier snapshot may exceed the
4479 * refquota. We want to receive the older/earlier snapshot, but setting
4480 * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent
4481 * the older/earlier snapshot from being received (with EDQUOT).
4483 * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario.
4485 * libzfs will need to be judicious handling errors encountered by props
4486 * extracted by this function.
4489 extract_delay_props(nvlist_t *props)
4491 nvlist_t *delayprops;
4492 nvpair_t *nvp, *tmp;
4493 static const zfs_prop_t delayable[] = { ZFS_PROP_REFQUOTA, 0 };
4496 VERIFY(nvlist_alloc(&delayprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4498 for (nvp = nvlist_next_nvpair(props, NULL); nvp != NULL;
4499 nvp = nvlist_next_nvpair(props, nvp)) {
4501 * strcmp() is safe because zfs_prop_to_name() always returns
4504 for (i = 0; delayable[i] != 0; i++) {
4505 if (strcmp(zfs_prop_to_name(delayable[i]),
4506 nvpair_name(nvp)) == 0) {
4510 if (delayable[i] != 0) {
4511 tmp = nvlist_prev_nvpair(props, nvp);
4512 VERIFY(nvlist_add_nvpair(delayprops, nvp) == 0);
4513 VERIFY(nvlist_remove_nvpair(props, nvp) == 0);
4518 if (nvlist_empty(delayprops)) {
4519 nvlist_free(delayprops);
4522 return (delayprops);
4526 static boolean_t zfs_ioc_recv_inject_err;
4531 * zc_name name of containing filesystem
4532 * zc_nvlist_src{_size} nvlist of properties to apply
4533 * zc_value name of snapshot to create
4534 * zc_string name of clone origin (if DRR_FLAG_CLONE)
4535 * zc_cookie file descriptor to recv from
4536 * zc_begin_record the BEGIN record of the stream (not byteswapped)
4537 * zc_guid force flag
4538 * zc_cleanup_fd cleanup-on-exit file descriptor
4539 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
4540 * zc_resumable if data is incomplete assume sender will resume
4543 * zc_cookie number of bytes read
4544 * zc_nvlist_dst{_size} error for each unapplied received property
4545 * zc_obj zprop_errflags_t
4546 * zc_action_handle handle for this guid/ds mapping
4549 zfs_ioc_recv(zfs_cmd_t *zc)
4552 dmu_recv_cookie_t drc;
4553 boolean_t force = (boolean_t)zc->zc_guid;
4556 int props_error = 0;
4559 nvlist_t *props = NULL; /* sent properties */
4560 nvlist_t *origprops = NULL; /* existing properties */
4561 nvlist_t *delayprops = NULL; /* sent properties applied post-receive */
4562 char *origin = NULL;
4564 char tofs[ZFS_MAX_DATASET_NAME_LEN];
4565 boolean_t first_recvd_props = B_FALSE;
4567 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4568 strchr(zc->zc_value, '@') == NULL ||
4569 strchr(zc->zc_value, '%'))
4570 return (SET_ERROR(EINVAL));
4572 (void) strcpy(tofs, zc->zc_value);
4573 tosnap = strchr(tofs, '@');
4576 if (zc->zc_nvlist_src != 0 &&
4577 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
4578 zc->zc_iflags, &props)) != 0)
4585 fget_read(curthread, fd, &cap_pread_rights, &fp);
4589 return (SET_ERROR(EBADF));
4592 errors = fnvlist_alloc();
4594 if (zc->zc_string[0])
4595 origin = zc->zc_string;
4597 error = dmu_recv_begin(tofs, tosnap,
4598 &zc->zc_begin_record, force, zc->zc_resumable, origin, &drc);
4603 * Set properties before we receive the stream so that they are applied
4604 * to the new data. Note that we must call dmu_recv_stream() if
4605 * dmu_recv_begin() succeeds.
4607 if (props != NULL && !drc.drc_newfs) {
4608 if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
4609 SPA_VERSION_RECVD_PROPS &&
4610 !dsl_prop_get_hasrecvd(tofs))
4611 first_recvd_props = B_TRUE;
4614 * If new received properties are supplied, they are to
4615 * completely replace the existing received properties, so stash
4616 * away the existing ones.
4618 if (dsl_prop_get_received(tofs, &origprops) == 0) {
4619 nvlist_t *errlist = NULL;
4621 * Don't bother writing a property if its value won't
4622 * change (and avoid the unnecessary security checks).
4624 * The first receive after SPA_VERSION_RECVD_PROPS is a
4625 * special case where we blow away all local properties
4628 if (!first_recvd_props)
4629 props_reduce(props, origprops);
4630 if (zfs_check_clearable(tofs, origprops, &errlist) != 0)
4631 (void) nvlist_merge(errors, errlist, 0);
4632 nvlist_free(errlist);
4634 if (clear_received_props(tofs, origprops,
4635 first_recvd_props ? NULL : props) != 0)
4636 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4638 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4642 if (props != NULL) {
4643 props_error = dsl_prop_set_hasrecvd(tofs);
4645 if (props_error == 0) {
4646 delayprops = extract_delay_props(props);
4647 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4653 error = dmu_recv_stream(&drc, fp, &off, zc->zc_cleanup_fd,
4654 &zc->zc_action_handle);
4657 zfsvfs_t *zfsvfs = NULL;
4659 if (getzfsvfs(tofs, &zfsvfs) == 0) {
4664 ds = dmu_objset_ds(zfsvfs->z_os);
4665 error = zfs_suspend_fs(zfsvfs);
4667 * If the suspend fails, then the recv_end will
4668 * likely also fail, and clean up after itself.
4670 end_err = dmu_recv_end(&drc, zfsvfs);
4672 error = zfs_resume_fs(zfsvfs, ds);
4673 error = error ? error : end_err;
4675 VFS_RELE(zfsvfs->z_vfs);
4677 vfs_unbusy(zfsvfs->z_vfs);
4680 error = dmu_recv_end(&drc, NULL);
4683 /* Set delayed properties now, after we're done receiving. */
4684 if (delayprops != NULL && error == 0) {
4685 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4686 delayprops, errors);
4690 if (delayprops != NULL) {
4692 * Merge delayed props back in with initial props, in case
4693 * we're DEBUG and zfs_ioc_recv_inject_err is set (which means
4694 * we have to make sure clear_received_props() includes
4695 * the delayed properties).
4697 * Since zfs_ioc_recv_inject_err is only in DEBUG kernels,
4698 * using ASSERT() will be just like a VERIFY.
4700 ASSERT(nvlist_merge(props, delayprops, 0) == 0);
4701 nvlist_free(delayprops);
4705 * Now that all props, initial and delayed, are set, report the prop
4706 * errors to the caller.
4708 if (zc->zc_nvlist_dst_size != 0 &&
4709 (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
4710 put_nvlist(zc, errors) != 0)) {
4712 * Caller made zc->zc_nvlist_dst less than the minimum expected
4713 * size or supplied an invalid address.
4715 props_error = SET_ERROR(EINVAL);
4718 zc->zc_cookie = off - fp->f_offset;
4719 if (off >= 0 && off <= MAXOFFSET_T)
4723 if (zfs_ioc_recv_inject_err) {
4724 zfs_ioc_recv_inject_err = B_FALSE;
4731 zvol_create_minors(tofs);
4735 * On error, restore the original props.
4737 if (error != 0 && props != NULL && !drc.drc_newfs) {
4738 if (clear_received_props(tofs, props, NULL) != 0) {
4740 * We failed to clear the received properties.
4741 * Since we may have left a $recvd value on the
4742 * system, we can't clear the $hasrecvd flag.
4744 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4745 } else if (first_recvd_props) {
4746 dsl_prop_unset_hasrecvd(tofs);
4749 if (origprops == NULL && !drc.drc_newfs) {
4750 /* We failed to stash the original properties. */
4751 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4755 * dsl_props_set() will not convert RECEIVED to LOCAL on or
4756 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
4757 * explictly if we're restoring local properties cleared in the
4758 * first new-style receive.
4760 if (origprops != NULL &&
4761 zfs_set_prop_nvlist(tofs, (first_recvd_props ?
4762 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
4763 origprops, NULL) != 0) {
4765 * We stashed the original properties but failed to
4768 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4773 nvlist_free(origprops);
4774 nvlist_free(errors);
4778 error = props_error;
4785 * zc_name name of snapshot to send
4786 * zc_cookie file descriptor to send stream to
4787 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
4788 * zc_sendobj objsetid of snapshot to send
4789 * zc_fromobj objsetid of incremental fromsnap (may be zero)
4790 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
4791 * output size in zc_objset_type.
4792 * zc_flags lzc_send_flags
4795 * zc_objset_type estimated size, if zc_guid is set
4798 zfs_ioc_send(zfs_cmd_t *zc)
4802 boolean_t estimate = (zc->zc_guid != 0);
4803 boolean_t embedok = (zc->zc_flags & 0x1);
4804 boolean_t large_block_ok = (zc->zc_flags & 0x2);
4805 boolean_t compressok = (zc->zc_flags & 0x4);
4807 if (zc->zc_obj != 0) {
4809 dsl_dataset_t *tosnap;
4811 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4815 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
4817 dsl_pool_rele(dp, FTAG);
4821 if (dsl_dir_is_clone(tosnap->ds_dir))
4823 dsl_dir_phys(tosnap->ds_dir)->dd_origin_obj;
4824 dsl_dataset_rele(tosnap, FTAG);
4825 dsl_pool_rele(dp, FTAG);
4830 dsl_dataset_t *tosnap;
4831 dsl_dataset_t *fromsnap = NULL;
4833 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4837 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
4839 dsl_pool_rele(dp, FTAG);
4843 if (zc->zc_fromobj != 0) {
4844 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
4847 dsl_dataset_rele(tosnap, FTAG);
4848 dsl_pool_rele(dp, FTAG);
4853 error = dmu_send_estimate(tosnap, fromsnap, compressok,
4854 &zc->zc_objset_type);
4856 if (fromsnap != NULL)
4857 dsl_dataset_rele(fromsnap, FTAG);
4858 dsl_dataset_rele(tosnap, FTAG);
4859 dsl_pool_rele(dp, FTAG);
4864 fp = getf(zc->zc_cookie);
4866 fget_write(curthread, zc->zc_cookie, &cap_write_rights, &fp);
4869 return (SET_ERROR(EBADF));
4872 error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
4873 zc->zc_fromobj, embedok, large_block_ok, compressok,
4875 zc->zc_cookie, fp->f_vnode, &off);
4877 zc->zc_cookie, fp, &off);
4880 if (off >= 0 && off <= MAXOFFSET_T)
4882 releasef(zc->zc_cookie);
4889 * zc_name name of snapshot on which to report progress
4890 * zc_cookie file descriptor of send stream
4893 * zc_cookie number of bytes written in send stream thus far
4896 zfs_ioc_send_progress(zfs_cmd_t *zc)
4900 dmu_sendarg_t *dsp = NULL;
4903 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4907 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
4909 dsl_pool_rele(dp, FTAG);
4913 mutex_enter(&ds->ds_sendstream_lock);
4916 * Iterate over all the send streams currently active on this dataset.
4917 * If there's one which matches the specified file descriptor _and_ the
4918 * stream was started by the current process, return the progress of
4921 for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
4922 dsp = list_next(&ds->ds_sendstreams, dsp)) {
4923 if (dsp->dsa_outfd == zc->zc_cookie &&
4924 dsp->dsa_proc == curproc)
4929 zc->zc_cookie = *(dsp->dsa_off);
4931 error = SET_ERROR(ENOENT);
4933 mutex_exit(&ds->ds_sendstream_lock);
4934 dsl_dataset_rele(ds, FTAG);
4935 dsl_pool_rele(dp, FTAG);
4940 zfs_ioc_inject_fault(zfs_cmd_t *zc)
4944 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
4945 &zc->zc_inject_record);
4948 zc->zc_guid = (uint64_t)id;
4954 zfs_ioc_clear_fault(zfs_cmd_t *zc)
4956 return (zio_clear_fault((int)zc->zc_guid));
4960 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
4962 int id = (int)zc->zc_guid;
4965 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
4966 &zc->zc_inject_record);
4974 zfs_ioc_error_log(zfs_cmd_t *zc)
4978 size_t count = (size_t)zc->zc_nvlist_dst_size;
4980 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
4983 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
4986 zc->zc_nvlist_dst_size = count;
4988 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
4990 spa_close(spa, FTAG);
4996 zfs_ioc_clear(zfs_cmd_t *zc)
5003 * On zpool clear we also fix up missing slogs
5005 mutex_enter(&spa_namespace_lock);
5006 spa = spa_lookup(zc->zc_name);
5008 mutex_exit(&spa_namespace_lock);
5009 return (SET_ERROR(EIO));
5011 if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
5012 /* we need to let spa_open/spa_load clear the chains */
5013 spa_set_log_state(spa, SPA_LOG_CLEAR);
5015 spa->spa_last_open_failed = 0;
5016 mutex_exit(&spa_namespace_lock);
5018 if (zc->zc_cookie & ZPOOL_NO_REWIND) {
5019 error = spa_open(zc->zc_name, &spa, FTAG);
5022 nvlist_t *config = NULL;
5024 if (zc->zc_nvlist_src == 0)
5025 return (SET_ERROR(EINVAL));
5027 if ((error = get_nvlist(zc->zc_nvlist_src,
5028 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
5029 error = spa_open_rewind(zc->zc_name, &spa, FTAG,
5031 if (config != NULL) {
5034 if ((err = put_nvlist(zc, config)) != 0)
5036 nvlist_free(config);
5038 nvlist_free(policy);
5045 spa_vdev_state_enter(spa, SCL_NONE);
5047 if (zc->zc_guid == 0) {
5050 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
5052 (void) spa_vdev_state_exit(spa, NULL, ENODEV);
5053 spa_close(spa, FTAG);
5054 return (SET_ERROR(ENODEV));
5058 vdev_clear(spa, vd);
5060 (void) spa_vdev_state_exit(spa, NULL, 0);
5063 * Resume any suspended I/Os.
5065 if (zio_resume(spa) != 0)
5066 error = SET_ERROR(EIO);
5068 spa_close(spa, FTAG);
5074 zfs_ioc_pool_reopen(zfs_cmd_t *zc)
5079 error = spa_open(zc->zc_name, &spa, FTAG);
5083 spa_vdev_state_enter(spa, SCL_NONE);
5086 * If a resilver is already in progress then set the
5087 * spa_scrub_reopen flag to B_TRUE so that we don't restart
5088 * the scan as a side effect of the reopen. Otherwise, let
5089 * vdev_open() decided if a resilver is required.
5091 spa->spa_scrub_reopen = dsl_scan_resilvering(spa->spa_dsl_pool);
5092 vdev_reopen(spa->spa_root_vdev);
5093 spa->spa_scrub_reopen = B_FALSE;
5095 (void) spa_vdev_state_exit(spa, NULL, 0);
5096 spa_close(spa, FTAG);
5101 * zc_name name of filesystem
5104 * zc_string name of conflicting snapshot, if there is one
5107 zfs_ioc_promote(zfs_cmd_t *zc)
5110 dsl_dataset_t *ds, *ods;
5111 char origin[ZFS_MAX_DATASET_NAME_LEN];
5115 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
5116 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
5117 strchr(zc->zc_name, '%'))
5118 return (SET_ERROR(EINVAL));
5120 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5124 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
5126 dsl_pool_rele(dp, FTAG);
5130 if (!dsl_dir_is_clone(ds->ds_dir)) {
5131 dsl_dataset_rele(ds, FTAG);
5132 dsl_pool_rele(dp, FTAG);
5133 return (SET_ERROR(EINVAL));
5136 error = dsl_dataset_hold_obj(dp,
5137 dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &ods);
5139 dsl_dataset_rele(ds, FTAG);
5140 dsl_pool_rele(dp, FTAG);
5144 dsl_dataset_name(ods, origin);
5145 dsl_dataset_rele(ods, FTAG);
5146 dsl_dataset_rele(ds, FTAG);
5147 dsl_pool_rele(dp, FTAG);
5150 * We don't need to unmount *all* the origin fs's snapshots, but
5153 cp = strchr(origin, '@');
5156 (void) dmu_objset_find(origin,
5157 zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
5158 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
5162 * Retrieve a single {user|group}{used|quota}@... property.
5165 * zc_name name of filesystem
5166 * zc_objset_type zfs_userquota_prop_t
5167 * zc_value domain name (eg. "S-1-234-567-89")
5168 * zc_guid RID/UID/GID
5171 * zc_cookie property value
5174 zfs_ioc_userspace_one(zfs_cmd_t *zc)
5179 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
5180 return (SET_ERROR(EINVAL));
5182 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
5186 error = zfs_userspace_one(zfsvfs,
5187 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
5188 zfsvfs_rele(zfsvfs, FTAG);
5195 * zc_name name of filesystem
5196 * zc_cookie zap cursor
5197 * zc_objset_type zfs_userquota_prop_t
5198 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
5201 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
5202 * zc_cookie zap cursor
5205 zfs_ioc_userspace_many(zfs_cmd_t *zc)
5208 int bufsize = zc->zc_nvlist_dst_size;
5211 return (SET_ERROR(ENOMEM));
5213 int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
5217 void *buf = kmem_alloc(bufsize, KM_SLEEP);
5219 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
5220 buf, &zc->zc_nvlist_dst_size);
5223 error = ddi_copyout(buf,
5224 (void *)(uintptr_t)zc->zc_nvlist_dst,
5225 zc->zc_nvlist_dst_size, zc->zc_iflags);
5227 kmem_free(buf, bufsize);
5228 zfsvfs_rele(zfsvfs, FTAG);
5235 * zc_name name of filesystem
5241 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
5247 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
5248 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
5250 * If userused is not enabled, it may be because the
5251 * objset needs to be closed & reopened (to grow the
5252 * objset_phys_t). Suspend/resume the fs will do that.
5254 dsl_dataset_t *ds, *newds;
5256 ds = dmu_objset_ds(zfsvfs->z_os);
5257 error = zfs_suspend_fs(zfsvfs);
5259 dmu_objset_refresh_ownership(ds, &newds,
5261 error = zfs_resume_fs(zfsvfs, newds);
5265 error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
5267 VFS_RELE(zfsvfs->z_vfs);
5269 vfs_unbusy(zfsvfs->z_vfs);
5272 /* XXX kind of reading contents without owning */
5273 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
5277 error = dmu_objset_userspace_upgrade(os);
5278 dmu_objset_rele(os, FTAG);
5286 * We don't want to have a hard dependency
5287 * against some special symbols in sharefs
5288 * nfs, and smbsrv. Determine them if needed when
5289 * the first file system is shared.
5290 * Neither sharefs, nfs or smbsrv are unloadable modules.
5292 int (*znfsexport_fs)(void *arg);
5293 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
5294 int (*zsmbexport_fs)(void *arg, boolean_t add_share);
5296 int zfs_nfsshare_inited;
5297 int zfs_smbshare_inited;
5299 ddi_modhandle_t nfs_mod;
5300 ddi_modhandle_t sharefs_mod;
5301 ddi_modhandle_t smbsrv_mod;
5302 #endif /* illumos */
5303 kmutex_t zfs_share_lock;
5311 ASSERT(MUTEX_HELD(&zfs_share_lock));
5312 /* Both NFS and SMB shares also require sharetab support. */
5313 if (sharefs_mod == NULL && ((sharefs_mod =
5314 ddi_modopen("fs/sharefs",
5315 KRTLD_MODE_FIRST, &error)) == NULL)) {
5316 return (SET_ERROR(ENOSYS));
5318 if (zshare_fs == NULL && ((zshare_fs =
5319 (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
5320 ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
5321 return (SET_ERROR(ENOSYS));
5325 #endif /* illumos */
5328 zfs_ioc_share(zfs_cmd_t *zc)
5334 switch (zc->zc_share.z_sharetype) {
5336 case ZFS_UNSHARE_NFS:
5337 if (zfs_nfsshare_inited == 0) {
5338 mutex_enter(&zfs_share_lock);
5339 if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
5340 KRTLD_MODE_FIRST, &error)) == NULL)) {
5341 mutex_exit(&zfs_share_lock);
5342 return (SET_ERROR(ENOSYS));
5344 if (znfsexport_fs == NULL &&
5345 ((znfsexport_fs = (int (*)(void *))
5347 "nfs_export", &error)) == NULL)) {
5348 mutex_exit(&zfs_share_lock);
5349 return (SET_ERROR(ENOSYS));
5351 error = zfs_init_sharefs();
5353 mutex_exit(&zfs_share_lock);
5354 return (SET_ERROR(ENOSYS));
5356 zfs_nfsshare_inited = 1;
5357 mutex_exit(&zfs_share_lock);
5361 case ZFS_UNSHARE_SMB:
5362 if (zfs_smbshare_inited == 0) {
5363 mutex_enter(&zfs_share_lock);
5364 if (smbsrv_mod == NULL && ((smbsrv_mod =
5365 ddi_modopen("drv/smbsrv",
5366 KRTLD_MODE_FIRST, &error)) == NULL)) {
5367 mutex_exit(&zfs_share_lock);
5368 return (SET_ERROR(ENOSYS));
5370 if (zsmbexport_fs == NULL && ((zsmbexport_fs =
5371 (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
5372 "smb_server_share", &error)) == NULL)) {
5373 mutex_exit(&zfs_share_lock);
5374 return (SET_ERROR(ENOSYS));
5376 error = zfs_init_sharefs();
5378 mutex_exit(&zfs_share_lock);
5379 return (SET_ERROR(ENOSYS));
5381 zfs_smbshare_inited = 1;
5382 mutex_exit(&zfs_share_lock);
5386 return (SET_ERROR(EINVAL));
5389 switch (zc->zc_share.z_sharetype) {
5391 case ZFS_UNSHARE_NFS:
5393 znfsexport_fs((void *)
5394 (uintptr_t)zc->zc_share.z_exportdata))
5398 case ZFS_UNSHARE_SMB:
5399 if (error = zsmbexport_fs((void *)
5400 (uintptr_t)zc->zc_share.z_exportdata,
5401 zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
5408 opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
5409 zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
5410 SHAREFS_ADD : SHAREFS_REMOVE;
5413 * Add or remove share from sharetab
5415 error = zshare_fs(opcode,
5416 (void *)(uintptr_t)zc->zc_share.z_sharedata,
5417 zc->zc_share.z_sharemax);
5421 #else /* !illumos */
5423 #endif /* illumos */
5426 ace_t full_access[] = {
5427 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
5432 * zc_name name of containing filesystem
5433 * zc_obj object # beyond which we want next in-use object #
5436 * zc_obj next in-use object #
5439 zfs_ioc_next_obj(zfs_cmd_t *zc)
5441 objset_t *os = NULL;
5444 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
5448 error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
5449 dsl_dataset_phys(os->os_dsl_dataset)->ds_prev_snap_txg);
5451 dmu_objset_rele(os, FTAG);
5457 * zc_name name of filesystem
5458 * zc_value prefix name for snapshot
5459 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
5462 * zc_value short name of new snapshot
5465 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
5472 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
5476 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
5477 (u_longlong_t)ddi_get_lbolt64());
5478 hold_name = kmem_asprintf("%%%s", zc->zc_value);
5480 error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
5483 (void) strcpy(zc->zc_value, snap_name);
5486 zfs_onexit_fd_rele(zc->zc_cleanup_fd);
5492 * zc_name name of "to" snapshot
5493 * zc_value name of "from" snapshot
5494 * zc_cookie file descriptor to write diff data on
5497 * dmu_diff_record_t's to the file descriptor
5500 zfs_ioc_diff(zfs_cmd_t *zc)
5507 fp = getf(zc->zc_cookie);
5509 fget_write(curthread, zc->zc_cookie, &cap_write_rights, &fp);
5512 return (SET_ERROR(EBADF));
5517 error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off);
5519 error = dmu_diff(zc->zc_name, zc->zc_value, fp, &off);
5522 if (off >= 0 && off <= MAXOFFSET_T)
5524 releasef(zc->zc_cookie);
5531 * Remove all ACL files in shares dir
5534 zfs_smb_acl_purge(znode_t *dzp)
5537 zap_attribute_t zap;
5538 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
5541 for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
5542 (error = zap_cursor_retrieve(&zc, &zap)) == 0;
5543 zap_cursor_advance(&zc)) {
5544 if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
5548 zap_cursor_fini(&zc);
5551 #endif /* illumos */
5554 zfs_ioc_smb_acl(zfs_cmd_t *zc)
5559 vnode_t *resourcevp = NULL;
5568 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
5569 NO_FOLLOW, NULL, &vp)) != 0)
5572 /* Now make sure mntpnt and dataset are ZFS */
5574 if (strcmp(vp->v_vfsp->mnt_stat.f_fstypename, "zfs") != 0 ||
5575 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
5576 zc->zc_name) != 0)) {
5578 return (SET_ERROR(EINVAL));
5582 zfsvfs = dzp->z_zfsvfs;
5586 * Create share dir if its missing.
5588 mutex_enter(&zfsvfs->z_lock);
5589 if (zfsvfs->z_shares_dir == 0) {
5592 tx = dmu_tx_create(zfsvfs->z_os);
5593 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
5595 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
5596 error = dmu_tx_assign(tx, TXG_WAIT);
5600 error = zfs_create_share_dir(zfsvfs, tx);
5604 mutex_exit(&zfsvfs->z_lock);
5610 mutex_exit(&zfsvfs->z_lock);
5612 ASSERT(zfsvfs->z_shares_dir);
5613 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
5619 switch (zc->zc_cookie) {
5620 case ZFS_SMB_ACL_ADD:
5621 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
5622 vattr.va_type = VREG;
5623 vattr.va_mode = S_IFREG|0777;
5627 vsec.vsa_mask = VSA_ACE;
5628 vsec.vsa_aclentp = &full_access;
5629 vsec.vsa_aclentsz = sizeof (full_access);
5630 vsec.vsa_aclcnt = 1;
5632 error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
5633 &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
5635 VN_RELE(resourcevp);
5638 case ZFS_SMB_ACL_REMOVE:
5639 error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
5643 case ZFS_SMB_ACL_RENAME:
5644 if ((error = get_nvlist(zc->zc_nvlist_src,
5645 zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
5647 VN_RELE(ZTOV(sharedir));
5651 if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
5652 nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
5655 VN_RELE(ZTOV(sharedir));
5657 nvlist_free(nvlist);
5660 error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
5662 nvlist_free(nvlist);
5665 case ZFS_SMB_ACL_PURGE:
5666 error = zfs_smb_acl_purge(sharedir);
5670 error = SET_ERROR(EINVAL);
5675 VN_RELE(ZTOV(sharedir));
5680 #else /* !illumos */
5681 return (EOPNOTSUPP);
5682 #endif /* illumos */
5687 * "holds" -> { snapname -> holdname (string), ... }
5688 * (optional) "cleanup_fd" -> fd (int32)
5692 * snapname -> error value (int32)
5698 zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
5702 int cleanup_fd = -1;
5706 error = nvlist_lookup_nvlist(args, "holds", &holds);
5708 return (SET_ERROR(EINVAL));
5710 /* make sure the user didn't pass us any invalid (empty) tags */
5711 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
5712 pair = nvlist_next_nvpair(holds, pair)) {
5715 error = nvpair_value_string(pair, &htag);
5717 return (SET_ERROR(error));
5719 if (strlen(htag) == 0)
5720 return (SET_ERROR(EINVAL));
5723 if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
5724 error = zfs_onexit_fd_hold(cleanup_fd, &minor);
5729 error = dsl_dataset_user_hold(holds, minor, errlist);
5731 zfs_onexit_fd_rele(cleanup_fd);
5736 * innvl is not used.
5739 * holdname -> time added (uint64 seconds since epoch)
5745 zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
5747 return (dsl_dataset_get_holds(snapname, outnvl));
5752 * snapname -> { holdname, ... }
5757 * snapname -> error value (int32)
5763 zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
5765 return (dsl_dataset_user_release(holds, errlist));
5770 * zc_name name of new filesystem or snapshot
5771 * zc_value full name of old snapshot
5774 * zc_cookie space in bytes
5775 * zc_objset_type compressed space in bytes
5776 * zc_perm_action uncompressed space in bytes
5779 zfs_ioc_space_written(zfs_cmd_t *zc)
5783 dsl_dataset_t *new, *old;
5785 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5788 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
5790 dsl_pool_rele(dp, FTAG);
5793 error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
5795 dsl_dataset_rele(new, FTAG);
5796 dsl_pool_rele(dp, FTAG);
5800 error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
5801 &zc->zc_objset_type, &zc->zc_perm_action);
5802 dsl_dataset_rele(old, FTAG);
5803 dsl_dataset_rele(new, FTAG);
5804 dsl_pool_rele(dp, FTAG);
5810 * "firstsnap" -> snapshot name
5814 * "used" -> space in bytes
5815 * "compressed" -> compressed space in bytes
5816 * "uncompressed" -> uncompressed space in bytes
5820 zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
5824 dsl_dataset_t *new, *old;
5826 uint64_t used, comp, uncomp;
5828 if (nvlist_lookup_string(innvl, "firstsnap", &firstsnap) != 0)
5829 return (SET_ERROR(EINVAL));
5831 error = dsl_pool_hold(lastsnap, FTAG, &dp);
5835 error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
5836 if (error == 0 && !new->ds_is_snapshot) {
5837 dsl_dataset_rele(new, FTAG);
5838 error = SET_ERROR(EINVAL);
5841 dsl_pool_rele(dp, FTAG);
5844 error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
5845 if (error == 0 && !old->ds_is_snapshot) {
5846 dsl_dataset_rele(old, FTAG);
5847 error = SET_ERROR(EINVAL);
5850 dsl_dataset_rele(new, FTAG);
5851 dsl_pool_rele(dp, FTAG);
5855 error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
5856 dsl_dataset_rele(old, FTAG);
5857 dsl_dataset_rele(new, FTAG);
5858 dsl_pool_rele(dp, FTAG);
5859 fnvlist_add_uint64(outnvl, "used", used);
5860 fnvlist_add_uint64(outnvl, "compressed", comp);
5861 fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
5866 zfs_ioc_jail(zfs_cmd_t *zc)
5869 return (zone_dataset_attach(curthread->td_ucred, zc->zc_name,
5870 (int)zc->zc_jailid));
5874 zfs_ioc_unjail(zfs_cmd_t *zc)
5877 return (zone_dataset_detach(curthread->td_ucred, zc->zc_name,
5878 (int)zc->zc_jailid));
5883 * "fd" -> file descriptor to write stream to (int32)
5884 * (optional) "fromsnap" -> full snap name to send an incremental from
5885 * (optional) "largeblockok" -> (value ignored)
5886 * indicates that blocks > 128KB are permitted
5887 * (optional) "embedok" -> (value ignored)
5888 * presence indicates DRR_WRITE_EMBEDDED records are permitted
5889 * (optional) "compressok" -> (value ignored)
5890 * presence indicates compressed DRR_WRITE records are permitted
5891 * (optional) "resume_object" and "resume_offset" -> (uint64)
5892 * if present, resume send stream from specified object and offset.
5899 zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5904 char *fromname = NULL;
5906 boolean_t largeblockok;
5908 boolean_t compressok;
5909 uint64_t resumeobj = 0;
5910 uint64_t resumeoff = 0;
5912 error = nvlist_lookup_int32(innvl, "fd", &fd);
5914 return (SET_ERROR(EINVAL));
5916 (void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
5918 largeblockok = nvlist_exists(innvl, "largeblockok");
5919 embedok = nvlist_exists(innvl, "embedok");
5920 compressok = nvlist_exists(innvl, "compressok");
5922 (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
5923 (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
5926 file_t *fp = getf(fd);
5928 fget_write(curthread, fd, &cap_write_rights, &fp);
5931 return (SET_ERROR(EBADF));
5934 error = dmu_send(snapname, fromname, embedok, largeblockok, compressok,
5936 fd, resumeobj, resumeoff, fp->f_vnode, &off);
5938 fd, resumeobj, resumeoff, fp, &off);
5942 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5953 * Determine approximately how large a zfs send stream will be -- the number
5954 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
5957 * (optional) "from" -> full snap or bookmark name to send an incremental
5959 * (optional) "largeblockok" -> (value ignored)
5960 * indicates that blocks > 128KB are permitted
5961 * (optional) "embedok" -> (value ignored)
5962 * presence indicates DRR_WRITE_EMBEDDED records are permitted
5963 * (optional) "compressok" -> (value ignored)
5964 * presence indicates compressed DRR_WRITE records are permitted
5968 * "space" -> bytes of space (uint64)
5972 zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5975 dsl_dataset_t *tosnap;
5978 boolean_t compressok;
5981 error = dsl_pool_hold(snapname, FTAG, &dp);
5985 error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
5987 dsl_pool_rele(dp, FTAG);
5991 compressok = nvlist_exists(innvl, "compressok");
5993 error = nvlist_lookup_string(innvl, "from", &fromname);
5995 if (strchr(fromname, '@') != NULL) {
5997 * If from is a snapshot, hold it and use the more
5998 * efficient dmu_send_estimate to estimate send space
5999 * size using deadlists.
6001 dsl_dataset_t *fromsnap;
6002 error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
6005 error = dmu_send_estimate(tosnap, fromsnap, compressok,
6007 dsl_dataset_rele(fromsnap, FTAG);
6008 } else if (strchr(fromname, '#') != NULL) {
6010 * If from is a bookmark, fetch the creation TXG of the
6011 * snapshot it was created from and use that to find
6012 * blocks that were born after it.
6014 zfs_bookmark_phys_t frombm;
6016 error = dsl_bookmark_lookup(dp, fromname, tosnap,
6020 error = dmu_send_estimate_from_txg(tosnap,
6021 frombm.zbm_creation_txg, compressok, &space);
6024 * from is not properly formatted as a snapshot or
6027 error = SET_ERROR(EINVAL);
6032 * If estimating the size of a full send, use dmu_send_estimate.
6034 error = dmu_send_estimate(tosnap, NULL, compressok, &space);
6037 fnvlist_add_uint64(outnvl, "space", space);
6040 dsl_dataset_rele(tosnap, FTAG);
6041 dsl_pool_rele(dp, FTAG);
6045 static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
6048 zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6049 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
6050 boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
6052 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
6054 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
6055 ASSERT3U(ioc, <, ZFS_IOC_LAST);
6056 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
6057 ASSERT3P(vec->zvec_func, ==, NULL);
6059 vec->zvec_legacy_func = func;
6060 vec->zvec_secpolicy = secpolicy;
6061 vec->zvec_namecheck = namecheck;
6062 vec->zvec_allow_log = log_history;
6063 vec->zvec_pool_check = pool_check;
6067 * See the block comment at the beginning of this file for details on
6068 * each argument to this function.
6071 zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
6072 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
6073 zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
6074 boolean_t allow_log)
6076 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
6078 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
6079 ASSERT3U(ioc, <, ZFS_IOC_LAST);
6080 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
6081 ASSERT3P(vec->zvec_func, ==, NULL);
6083 /* if we are logging, the name must be valid */
6084 ASSERT(!allow_log || namecheck != NO_NAME);
6086 vec->zvec_name = name;
6087 vec->zvec_func = func;
6088 vec->zvec_secpolicy = secpolicy;
6089 vec->zvec_namecheck = namecheck;
6090 vec->zvec_pool_check = pool_check;
6091 vec->zvec_smush_outnvlist = smush_outnvlist;
6092 vec->zvec_allow_log = allow_log;
6096 zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6097 zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
6098 zfs_ioc_poolcheck_t pool_check)
6100 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6101 POOL_NAME, log_history, pool_check);
6105 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6106 zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
6108 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6109 DATASET_NAME, B_FALSE, pool_check);
6113 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
6115 zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
6116 POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6120 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6121 zfs_secpolicy_func_t *secpolicy)
6123 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6124 NO_NAME, B_FALSE, POOL_CHECK_NONE);
6128 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
6129 zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
6131 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6132 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
6136 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
6138 zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
6139 zfs_secpolicy_read);
6143 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6144 zfs_secpolicy_func_t *secpolicy)
6146 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6147 DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6151 zfs_ioctl_init(void)
6153 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
6154 zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
6155 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6157 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
6158 zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
6159 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE);
6161 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
6162 zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
6163 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
6165 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
6166 zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
6167 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
6169 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
6170 zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
6171 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
6173 zfs_ioctl_register("create", ZFS_IOC_CREATE,
6174 zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
6175 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6177 zfs_ioctl_register("clone", ZFS_IOC_CLONE,
6178 zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
6179 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6181 zfs_ioctl_register("remap", ZFS_IOC_REMAP,
6182 zfs_ioc_remap, zfs_secpolicy_remap, DATASET_NAME,
6183 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
6185 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
6186 zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
6187 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6189 zfs_ioctl_register("hold", ZFS_IOC_HOLD,
6190 zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
6191 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6192 zfs_ioctl_register("release", ZFS_IOC_RELEASE,
6193 zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
6194 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6196 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
6197 zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
6198 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
6200 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
6201 zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
6202 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
6204 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK,
6205 zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME,
6206 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6208 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS,
6209 zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME,
6210 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
6212 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS,
6213 zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks,
6215 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6217 zfs_ioctl_register("channel_program", ZFS_IOC_CHANNEL_PROGRAM,
6218 zfs_ioc_channel_program, zfs_secpolicy_config,
6219 POOL_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE,
6222 zfs_ioctl_register("zpool_checkpoint", ZFS_IOC_POOL_CHECKPOINT,
6223 zfs_ioc_pool_checkpoint, zfs_secpolicy_config, POOL_NAME,
6224 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6226 zfs_ioctl_register("zpool_discard_checkpoint",
6227 ZFS_IOC_POOL_DISCARD_CHECKPOINT, zfs_ioc_pool_discard_checkpoint,
6228 zfs_secpolicy_config, POOL_NAME,
6229 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6231 zfs_ioctl_register("initialize", ZFS_IOC_POOL_INITIALIZE,
6232 zfs_ioc_pool_initialize, zfs_secpolicy_config, POOL_NAME,
6233 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6235 /* IOCTLS that use the legacy function signature */
6237 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
6238 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
6240 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
6241 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
6242 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
6244 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
6245 zfs_ioc_pool_upgrade);
6246 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
6248 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
6249 zfs_ioc_vdev_remove);
6250 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
6251 zfs_ioc_vdev_set_state);
6252 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
6253 zfs_ioc_vdev_attach);
6254 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
6255 zfs_ioc_vdev_detach);
6256 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
6257 zfs_ioc_vdev_setpath);
6258 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
6259 zfs_ioc_vdev_setfru);
6260 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
6261 zfs_ioc_pool_set_props);
6262 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
6263 zfs_ioc_vdev_split);
6264 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
6265 zfs_ioc_pool_reguid);
6267 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
6268 zfs_ioc_pool_configs, zfs_secpolicy_none);
6269 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
6270 zfs_ioc_pool_tryimport, zfs_secpolicy_config);
6271 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
6272 zfs_ioc_inject_fault, zfs_secpolicy_inject);
6273 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
6274 zfs_ioc_clear_fault, zfs_secpolicy_inject);
6275 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
6276 zfs_ioc_inject_list_next, zfs_secpolicy_inject);
6279 * pool destroy, and export don't log the history as part of
6280 * zfsdev_ioctl, but rather zfs_ioc_pool_export
6281 * does the logging of those commands.
6283 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
6284 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
6285 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
6286 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
6288 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
6289 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
6290 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
6291 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
6293 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
6294 zfs_secpolicy_inject, B_FALSE, POOL_CHECK_NONE);
6295 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
6296 zfs_ioc_dsobj_to_dsname,
6297 zfs_secpolicy_diff, B_FALSE, POOL_CHECK_NONE);
6298 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
6299 zfs_ioc_pool_get_history,
6300 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
6302 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
6303 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
6305 zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
6306 zfs_secpolicy_config, B_TRUE, POOL_CHECK_READONLY);
6307 zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
6308 zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
6310 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
6311 zfs_ioc_space_written);
6312 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
6313 zfs_ioc_objset_recvd_props);
6314 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
6316 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
6318 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
6319 zfs_ioc_objset_stats);
6320 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
6321 zfs_ioc_objset_zplprops);
6322 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
6323 zfs_ioc_dataset_list_next);
6324 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
6325 zfs_ioc_snapshot_list_next);
6326 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
6327 zfs_ioc_send_progress);
6329 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
6330 zfs_ioc_diff, zfs_secpolicy_diff);
6331 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
6332 zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
6333 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
6334 zfs_ioc_obj_to_path, zfs_secpolicy_diff);
6335 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
6336 zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
6337 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
6338 zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
6339 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
6340 zfs_ioc_send, zfs_secpolicy_send);
6342 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
6343 zfs_secpolicy_none);
6344 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
6345 zfs_secpolicy_destroy);
6346 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
6347 zfs_secpolicy_rename);
6348 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
6349 zfs_secpolicy_recv);
6350 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
6351 zfs_secpolicy_promote);
6352 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
6353 zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
6354 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
6355 zfs_secpolicy_set_fsacl);
6357 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
6358 zfs_secpolicy_share, POOL_CHECK_NONE);
6359 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
6360 zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
6361 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
6362 zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
6363 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6364 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
6365 zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
6366 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6369 zfs_ioctl_register_dataset_nolog(ZFS_IOC_JAIL, zfs_ioc_jail,
6370 zfs_secpolicy_config, POOL_CHECK_NONE);
6371 zfs_ioctl_register_dataset_nolog(ZFS_IOC_UNJAIL, zfs_ioc_unjail,
6372 zfs_secpolicy_config, POOL_CHECK_NONE);
6373 zfs_ioctl_register("fbsd_nextboot", ZFS_IOC_NEXTBOOT,
6374 zfs_ioc_nextboot, zfs_secpolicy_config, NO_NAME,
6375 POOL_CHECK_NONE, B_FALSE, B_FALSE);
6380 pool_status_check(const char *name, zfs_ioc_namecheck_t type,
6381 zfs_ioc_poolcheck_t check)
6386 ASSERT(type == POOL_NAME || type == DATASET_NAME);
6388 if (check & POOL_CHECK_NONE)
6391 error = spa_open(name, &spa, FTAG);
6393 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
6394 error = SET_ERROR(EAGAIN);
6395 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
6396 error = SET_ERROR(EROFS);
6397 spa_close(spa, FTAG);
6403 * Find a free minor number.
6406 zfsdev_minor_alloc(void)
6408 static minor_t last_minor;
6411 ASSERT(MUTEX_HELD(&spa_namespace_lock));
6413 for (m = last_minor + 1; m != last_minor; m++) {
6414 if (m > ZFSDEV_MAX_MINOR)
6416 if (ddi_get_soft_state(zfsdev_state, m) == NULL) {
6426 zfs_ctldev_init(struct cdev *devp)
6429 zfs_soft_state_t *zs;
6431 ASSERT(MUTEX_HELD(&spa_namespace_lock));
6433 minor = zfsdev_minor_alloc();
6435 return (SET_ERROR(ENXIO));
6437 if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS)
6438 return (SET_ERROR(EAGAIN));
6440 devfs_set_cdevpriv((void *)(uintptr_t)minor, zfsdev_close);
6442 zs = ddi_get_soft_state(zfsdev_state, minor);
6443 zs->zss_type = ZSST_CTLDEV;
6444 zfs_onexit_init((zfs_onexit_t **)&zs->zss_data);
6450 zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor)
6452 ASSERT(MUTEX_HELD(&spa_namespace_lock));
6454 zfs_onexit_destroy(zo);
6455 ddi_soft_state_free(zfsdev_state, minor);
6459 zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which)
6461 zfs_soft_state_t *zp;
6463 zp = ddi_get_soft_state(zfsdev_state, minor);
6464 if (zp == NULL || zp->zss_type != which)
6467 return (zp->zss_data);
6471 zfsdev_open(struct cdev *devp, int flag, int mode, struct thread *td)
6476 if (getminor(*devp) != 0)
6477 return (zvol_open(devp, flag, otyp, cr));
6480 /* This is the control device. Allocate a new minor if requested. */
6482 mutex_enter(&spa_namespace_lock);
6483 error = zfs_ctldev_init(devp);
6484 mutex_exit(&spa_namespace_lock);
6491 zfsdev_close(void *data)
6494 minor_t minor = (minor_t)(uintptr_t)data;
6499 mutex_enter(&spa_namespace_lock);
6500 zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV);
6502 mutex_exit(&spa_namespace_lock);
6505 zfs_ctldev_destroy(zo, minor);
6506 mutex_exit(&spa_namespace_lock);
6510 zfsdev_ioctl(struct cdev *dev, u_long zcmd, caddr_t arg, int flag,
6517 minor_t minor = getminor(dev);
6519 zfs_iocparm_t *zc_iocparm;
6520 int cflag, cmd, oldvecnum;
6521 boolean_t newioc, compat;
6522 void *compat_zc = NULL;
6523 cred_t *cr = td->td_ucred;
6525 const zfs_ioc_vec_t *vec;
6526 char *saved_poolname = NULL;
6527 nvlist_t *innvl = NULL;
6529 cflag = ZFS_CMD_COMPAT_NONE;
6531 newioc = B_TRUE; /* "new" style (zfs_iocparm_t) ioctl */
6533 len = IOCPARM_LEN(zcmd);
6534 vecnum = cmd = zcmd & 0xff;
6537 * Check if we are talking to supported older binaries
6538 * and translate zfs_cmd if necessary
6540 if (len != sizeof(zfs_iocparm_t)) {
6547 case sizeof(zfs_cmd_zcmd_t):
6548 cflag = ZFS_CMD_COMPAT_LZC;
6550 case sizeof(zfs_cmd_deadman_t):
6551 cflag = ZFS_CMD_COMPAT_DEADMAN;
6553 case sizeof(zfs_cmd_v28_t):
6554 cflag = ZFS_CMD_COMPAT_V28;
6556 case sizeof(zfs_cmd_v15_t):
6557 if (cmd >= sizeof(zfs_ioctl_v15_to_v28) /
6558 sizeof(zfs_ioctl_v15_to_v28[0]))
6561 cflag = ZFS_CMD_COMPAT_V15;
6562 vecnum = zfs_ioctl_v15_to_v28[cmd];
6565 * Return without further handling
6566 * if the command is blacklisted.
6568 if (vecnum == ZFS_IOC_COMPAT_PASS)
6570 else if (vecnum == ZFS_IOC_COMPAT_FAIL)
6579 vecnum = cmd - ZFS_IOC_FIRST;
6580 ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
6583 if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
6584 return (SET_ERROR(EINVAL));
6585 vec = &zfs_ioc_vec[vecnum];
6587 zc = kmem_zalloc(sizeof(zfs_cmd_t), KM_SLEEP);
6590 error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
6592 error = SET_ERROR(EFAULT);
6595 #else /* !illumos */
6596 bzero(zc, sizeof(zfs_cmd_t));
6599 zc_iocparm = (void *)arg;
6601 switch (zc_iocparm->zfs_ioctl_version) {
6602 case ZFS_IOCVER_CURRENT:
6603 if (zc_iocparm->zfs_cmd_size != sizeof(zfs_cmd_t)) {
6604 error = SET_ERROR(EINVAL);
6608 case ZFS_IOCVER_INLANES:
6609 if (zc_iocparm->zfs_cmd_size != sizeof(zfs_cmd_inlanes_t)) {
6610 error = SET_ERROR(EFAULT);
6614 cflag = ZFS_CMD_COMPAT_INLANES;
6616 case ZFS_IOCVER_RESUME:
6617 if (zc_iocparm->zfs_cmd_size != sizeof(zfs_cmd_resume_t)) {
6618 error = SET_ERROR(EFAULT);
6622 cflag = ZFS_CMD_COMPAT_RESUME;
6624 case ZFS_IOCVER_EDBP:
6625 if (zc_iocparm->zfs_cmd_size != sizeof(zfs_cmd_edbp_t)) {
6626 error = SET_ERROR(EFAULT);
6630 cflag = ZFS_CMD_COMPAT_EDBP;
6632 case ZFS_IOCVER_ZCMD:
6633 if (zc_iocparm->zfs_cmd_size > sizeof(zfs_cmd_t) ||
6634 zc_iocparm->zfs_cmd_size < sizeof(zfs_cmd_zcmd_t)) {
6635 error = SET_ERROR(EFAULT);
6639 cflag = ZFS_CMD_COMPAT_ZCMD;
6642 error = SET_ERROR(EINVAL);
6648 ASSERT(sizeof(zfs_cmd_t) >= zc_iocparm->zfs_cmd_size);
6649 compat_zc = kmem_zalloc(sizeof(zfs_cmd_t), KM_SLEEP);
6650 bzero(compat_zc, sizeof(zfs_cmd_t));
6652 error = ddi_copyin((void *)(uintptr_t)zc_iocparm->zfs_cmd,
6653 compat_zc, zc_iocparm->zfs_cmd_size, flag);
6655 error = SET_ERROR(EFAULT);
6659 error = ddi_copyin((void *)(uintptr_t)zc_iocparm->zfs_cmd,
6660 zc, zc_iocparm->zfs_cmd_size, flag);
6662 error = SET_ERROR(EFAULT);
6670 ASSERT(compat_zc != NULL);
6671 zfs_cmd_compat_get(zc, compat_zc, cflag);
6673 ASSERT(compat_zc == NULL);
6674 zfs_cmd_compat_get(zc, arg, cflag);
6677 error = zfs_ioctl_compat_pre(zc, &vecnum, cflag);
6680 if (oldvecnum != vecnum)
6681 vec = &zfs_ioc_vec[vecnum];
6683 #endif /* !illumos */
6685 zc->zc_iflags = flag & FKIOCTL;
6686 if (zc->zc_nvlist_src_size != 0) {
6687 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
6688 zc->zc_iflags, &innvl);
6693 /* rewrite innvl for backwards compatibility */
6695 innvl = zfs_ioctl_compat_innvl(zc, innvl, vecnum, cflag);
6698 * Ensure that all pool/dataset names are valid before we pass down to
6701 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
6702 switch (vec->zvec_namecheck) {
6704 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
6705 error = SET_ERROR(EINVAL);
6707 error = pool_status_check(zc->zc_name,
6708 vec->zvec_namecheck, vec->zvec_pool_check);
6712 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
6713 error = SET_ERROR(EINVAL);
6715 error = pool_status_check(zc->zc_name,
6716 vec->zvec_namecheck, vec->zvec_pool_check);
6724 error = vec->zvec_secpolicy(zc, innvl, cr);
6729 /* legacy ioctls can modify zc_name */
6730 len = strcspn(zc->zc_name, "/@#") + 1;
6731 saved_poolname = kmem_alloc(len, KM_SLEEP);
6732 (void) strlcpy(saved_poolname, zc->zc_name, len);
6734 if (vec->zvec_func != NULL) {
6738 nvlist_t *lognv = NULL;
6740 ASSERT(vec->zvec_legacy_func == NULL);
6743 * Add the innvl to the lognv before calling the func,
6744 * in case the func changes the innvl.
6746 if (vec->zvec_allow_log) {
6747 lognv = fnvlist_alloc();
6748 fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
6750 if (!nvlist_empty(innvl)) {
6751 fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
6756 outnvl = fnvlist_alloc();
6757 error = vec->zvec_func(zc->zc_name, innvl, outnvl);
6760 * Some commands can partially execute, modfiy state, and still
6761 * return an error. In these cases, attempt to record what
6765 (cmd == ZFS_IOC_CHANNEL_PROGRAM && error != EINVAL)) &&
6766 vec->zvec_allow_log &&
6767 spa_open(zc->zc_name, &spa, FTAG) == 0) {
6768 if (!nvlist_empty(outnvl)) {
6769 fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL,
6773 fnvlist_add_int64(lognv, ZPOOL_HIST_ERRNO,
6776 (void) spa_history_log_nvl(spa, lognv);
6777 spa_close(spa, FTAG);
6779 fnvlist_free(lognv);
6781 /* rewrite outnvl for backwards compatibility */
6783 outnvl = zfs_ioctl_compat_outnvl(zc, outnvl, vecnum,
6786 if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
6788 if (vec->zvec_smush_outnvlist) {
6789 smusherror = nvlist_smush(outnvl,
6790 zc->zc_nvlist_dst_size);
6792 if (smusherror == 0)
6793 puterror = put_nvlist(zc, outnvl);
6799 nvlist_free(outnvl);
6801 error = vec->zvec_legacy_func(zc);
6808 rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
6809 if (error == 0 && rc != 0)
6810 error = SET_ERROR(EFAULT);
6813 zfs_ioctl_compat_post(zc, cmd, cflag);
6815 ASSERT(compat_zc != NULL);
6816 ASSERT(sizeof(zfs_cmd_t) >= zc_iocparm->zfs_cmd_size);
6818 zfs_cmd_compat_put(zc, compat_zc, vecnum, cflag);
6819 rc = ddi_copyout(compat_zc,
6820 (void *)(uintptr_t)zc_iocparm->zfs_cmd,
6821 zc_iocparm->zfs_cmd_size, flag);
6822 if (error == 0 && rc != 0)
6823 error = SET_ERROR(EFAULT);
6824 kmem_free(compat_zc, sizeof (zfs_cmd_t));
6826 zfs_cmd_compat_put(zc, arg, vecnum, cflag);
6831 rc = ddi_copyout(zc, (void *)(uintptr_t)zc_iocparm->zfs_cmd,
6832 sizeof (zfs_cmd_t), flag);
6833 if (error == 0 && rc != 0)
6834 error = SET_ERROR(EFAULT);
6837 if (error == 0 && vec->zvec_allow_log) {
6838 char *s = tsd_get(zfs_allow_log_key);
6841 (void) tsd_set(zfs_allow_log_key, saved_poolname);
6843 if (saved_poolname != NULL)
6844 strfree(saved_poolname);
6847 kmem_free(zc, sizeof (zfs_cmd_t));
6853 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
6855 if (cmd != DDI_ATTACH)
6856 return (DDI_FAILURE);
6858 if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
6859 DDI_PSEUDO, 0) == DDI_FAILURE)
6860 return (DDI_FAILURE);
6864 ddi_report_dev(dip);
6866 return (DDI_SUCCESS);
6870 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
6872 if (spa_busy() || zfs_busy() || zvol_busy())
6873 return (DDI_FAILURE);
6875 if (cmd != DDI_DETACH)
6876 return (DDI_FAILURE);
6880 ddi_prop_remove_all(dip);
6881 ddi_remove_minor_node(dip, NULL);
6883 return (DDI_SUCCESS);
6888 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
6891 case DDI_INFO_DEVT2DEVINFO:
6893 return (DDI_SUCCESS);
6895 case DDI_INFO_DEVT2INSTANCE:
6896 *result = (void *)0;
6897 return (DDI_SUCCESS);
6900 return (DDI_FAILURE);
6902 #endif /* illumos */
6905 * OK, so this is a little weird.
6907 * /dev/zfs is the control node, i.e. minor 0.
6908 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
6910 * /dev/zfs has basically nothing to do except serve up ioctls,
6911 * so most of the standard driver entry points are in zvol.c.
6914 static struct cb_ops zfs_cb_ops = {
6915 zfsdev_open, /* open */
6916 zfsdev_close, /* close */
6917 zvol_strategy, /* strategy */
6919 zvol_dump, /* dump */
6920 zvol_read, /* read */
6921 zvol_write, /* write */
6922 zfsdev_ioctl, /* ioctl */
6926 nochpoll, /* poll */
6927 ddi_prop_op, /* prop_op */
6928 NULL, /* streamtab */
6929 D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */
6930 CB_REV, /* version */
6931 nodev, /* async read */
6932 nodev, /* async write */
6935 static struct dev_ops zfs_dev_ops = {
6936 DEVO_REV, /* version */
6938 zfs_info, /* info */
6939 nulldev, /* identify */
6940 nulldev, /* probe */
6941 zfs_attach, /* attach */
6942 zfs_detach, /* detach */
6944 &zfs_cb_ops, /* driver operations */
6945 NULL, /* no bus operations */
6947 ddi_quiesce_not_needed, /* quiesce */
6950 static struct modldrv zfs_modldrv = {
6956 static struct modlinkage modlinkage = {
6958 (void *)&zfs_modlfs,
6959 (void *)&zfs_modldrv,
6962 #endif /* illumos */
6964 static struct cdevsw zfs_cdevsw = {
6965 .d_version = D_VERSION,
6966 .d_open = zfsdev_open,
6967 .d_ioctl = zfsdev_ioctl,
6968 .d_name = ZFS_DEV_NAME
6972 zfs_allow_log_destroy(void *arg)
6974 char *poolname = arg;
6981 zfsdev = make_dev(&zfs_cdevsw, 0x0, UID_ROOT, GID_OPERATOR, 0666,
6989 destroy_dev(zfsdev);
6992 static struct root_hold_token *zfs_root_token;
6993 struct proc *zfsproc;
7001 spa_init(FREAD | FWRITE);
7006 if ((error = mod_install(&modlinkage)) != 0) {
7013 tsd_create(&zfs_fsyncer_key, NULL);
7014 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
7015 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
7017 error = ldi_ident_from_mod(&modlinkage, &zfs_li);
7019 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
7029 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
7030 return (SET_ERROR(EBUSY));
7032 if ((error = mod_remove(&modlinkage)) != 0)
7038 if (zfs_nfsshare_inited)
7039 (void) ddi_modclose(nfs_mod);
7040 if (zfs_smbshare_inited)
7041 (void) ddi_modclose(smbsrv_mod);
7042 if (zfs_nfsshare_inited || zfs_smbshare_inited)
7043 (void) ddi_modclose(sharefs_mod);
7045 tsd_destroy(&zfs_fsyncer_key);
7046 ldi_ident_release(zfs_li);
7048 mutex_destroy(&zfs_share_lock);
7054 _info(struct modinfo *modinfop)
7056 return (mod_info(&modlinkage, modinfop));
7058 #endif /* illumos */
7060 static int zfs__init(void);
7061 static int zfs__fini(void);
7062 static void zfs_shutdown(void *, int);
7064 static eventhandler_tag zfs_shutdown_event_tag;
7067 #define ZFS_MIN_KSTACK_PAGES 4
7075 #if KSTACK_PAGES < ZFS_MIN_KSTACK_PAGES
7076 printf("ZFS NOTICE: KSTACK_PAGES is %d which could result in stack "
7077 "overflow panic!\nPlease consider adding "
7078 "'options KSTACK_PAGES=%d' to your kernel config\n", KSTACK_PAGES,
7079 ZFS_MIN_KSTACK_PAGES);
7082 zfs_root_token = root_mount_hold("ZFS");
7084 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
7086 spa_init(FREAD | FWRITE);
7091 tsd_create(&zfs_fsyncer_key, NULL);
7092 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
7093 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
7094 tsd_create(&zfs_geom_probe_vdev_key, NULL);
7096 printf("ZFS storage pool version: features support (" SPA_VERSION_STRING ")\n");
7097 root_mount_rel(zfs_root_token);
7107 if (spa_busy() || zfs_busy() || zvol_busy() ||
7108 zio_injection_enabled) {
7117 tsd_destroy(&zfs_fsyncer_key);
7118 tsd_destroy(&rrw_tsd_key);
7119 tsd_destroy(&zfs_allow_log_key);
7121 mutex_destroy(&zfs_share_lock);
7127 zfs_shutdown(void *arg __unused, int howto __unused)
7131 * ZFS fini routines can not properly work in a panic-ed system.
7133 if (panicstr == NULL)
7139 zfs_modevent(module_t mod, int type, void *unused __unused)
7147 zfs_shutdown_event_tag = EVENTHANDLER_REGISTER(
7148 shutdown_post_sync, zfs_shutdown, NULL,
7149 SHUTDOWN_PRI_FIRST);
7153 if (err == 0 && zfs_shutdown_event_tag != NULL)
7154 EVENTHANDLER_DEREGISTER(shutdown_post_sync,
7155 zfs_shutdown_event_tag);
7162 return (EOPNOTSUPP);
7165 static moduledata_t zfs_mod = {
7170 DECLARE_MODULE(zfsctrl, zfs_mod, SI_SUB_VFS, SI_ORDER_ANY);
7171 MODULE_VERSION(zfsctrl, 1);
7172 MODULE_DEPEND(zfsctrl, opensolaris, 1, 1, 1);
7173 MODULE_DEPEND(zfsctrl, krpc, 1, 1, 1);
7174 MODULE_DEPEND(zfsctrl, acl_nfs4, 1, 1, 1);