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>
199 #include "zfs_namecheck.h"
200 #include "zfs_prop.h"
201 #include "zfs_deleg.h"
202 #include "zfs_comutil.h"
203 #include "zfs_ioctl_compat.h"
208 static struct cdev *zfsdev;
210 extern void zfs_init(void);
211 extern void zfs_fini(void);
213 uint_t zfs_fsyncer_key;
214 extern uint_t rrw_tsd_key;
215 static uint_t zfs_allow_log_key;
216 extern uint_t zfs_geom_probe_vdev_key;
218 typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *);
219 typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *);
220 typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *);
226 } zfs_ioc_namecheck_t;
229 POOL_CHECK_NONE = 1 << 0,
230 POOL_CHECK_SUSPENDED = 1 << 1,
231 POOL_CHECK_READONLY = 1 << 2,
232 } zfs_ioc_poolcheck_t;
234 typedef struct zfs_ioc_vec {
235 zfs_ioc_legacy_func_t *zvec_legacy_func;
236 zfs_ioc_func_t *zvec_func;
237 zfs_secpolicy_func_t *zvec_secpolicy;
238 zfs_ioc_namecheck_t zvec_namecheck;
239 boolean_t zvec_allow_log;
240 zfs_ioc_poolcheck_t zvec_pool_check;
241 boolean_t zvec_smush_outnvlist;
242 const char *zvec_name;
245 /* This array is indexed by zfs_userquota_prop_t */
246 static const char *userquota_perms[] = {
247 ZFS_DELEG_PERM_USERUSED,
248 ZFS_DELEG_PERM_USERQUOTA,
249 ZFS_DELEG_PERM_GROUPUSED,
250 ZFS_DELEG_PERM_GROUPQUOTA,
253 static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
254 static int zfs_check_settable(const char *name, nvpair_t *property,
256 static int zfs_check_clearable(char *dataset, nvlist_t *props,
258 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
260 int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
261 static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
263 static void zfsdev_close(void *data);
265 static int zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature);
267 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
269 __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
276 * Get rid of annoying "../common/" prefix to filename.
278 newfile = strrchr(file, '/');
279 if (newfile != NULL) {
280 newfile = newfile + 1; /* Get rid of leading / */
286 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
290 * To get this data, use the zfs-dprintf probe as so:
291 * dtrace -q -n 'zfs-dprintf \
292 * /stringof(arg0) == "dbuf.c"/ \
293 * {printf("%s: %s", stringof(arg1), stringof(arg3))}'
295 * arg1 = function name
299 DTRACE_PROBE4(zfs__dprintf,
300 char *, newfile, char *, func, int, line, char *, buf);
304 history_str_free(char *buf)
306 kmem_free(buf, HIS_MAX_RECORD_LEN);
310 history_str_get(zfs_cmd_t *zc)
314 if (zc->zc_history == 0)
317 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
318 if (copyinstr((void *)(uintptr_t)zc->zc_history,
319 buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
320 history_str_free(buf);
324 buf[HIS_MAX_RECORD_LEN -1] = '\0';
330 * Check to see if the named dataset is currently defined as bootable
333 zfs_is_bootfs(const char *name)
337 if (dmu_objset_hold(name, FTAG, &os) == 0) {
339 ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
340 dmu_objset_rele(os, FTAG);
347 * Return non-zero if the spa version is less than requested version.
350 zfs_earlier_version(const char *name, int version)
354 if (spa_open(name, &spa, FTAG) == 0) {
355 if (spa_version(spa) < version) {
356 spa_close(spa, FTAG);
359 spa_close(spa, FTAG);
365 * Return TRUE if the ZPL version is less than requested version.
368 zpl_earlier_version(const char *name, int version)
371 boolean_t rc = B_TRUE;
373 if (dmu_objset_hold(name, FTAG, &os) == 0) {
376 if (dmu_objset_type(os) != DMU_OST_ZFS) {
377 dmu_objset_rele(os, FTAG);
380 /* XXX reading from non-owned objset */
381 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
382 rc = zplversion < version;
383 dmu_objset_rele(os, FTAG);
389 zfs_log_history(zfs_cmd_t *zc)
394 if ((buf = history_str_get(zc)) == NULL)
397 if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
398 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
399 (void) spa_history_log(spa, buf);
400 spa_close(spa, FTAG);
402 history_str_free(buf);
406 * Policy for top-level read operations (list pools). Requires no privileges,
407 * and can be used in the local zone, as there is no associated dataset.
411 zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
417 * Policy for dataset read operations (list children, get statistics). Requires
418 * no privileges, but must be visible in the local zone.
422 zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
424 if (INGLOBALZONE(curthread) ||
425 zone_dataset_visible(zc->zc_name, NULL))
428 return (SET_ERROR(ENOENT));
432 zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
437 * The dataset must be visible by this zone -- check this first
438 * so they don't see EPERM on something they shouldn't know about.
440 if (!INGLOBALZONE(curthread) &&
441 !zone_dataset_visible(dataset, &writable))
442 return (SET_ERROR(ENOENT));
444 if (INGLOBALZONE(curthread)) {
446 * If the fs is zoned, only root can access it from the
449 if (secpolicy_zfs(cr) && zoned)
450 return (SET_ERROR(EPERM));
453 * If we are in a local zone, the 'zoned' property must be set.
456 return (SET_ERROR(EPERM));
458 /* must be writable by this zone */
460 return (SET_ERROR(EPERM));
466 zfs_dozonecheck(const char *dataset, cred_t *cr)
470 if (dsl_prop_get_integer(dataset, "jailed", &zoned, NULL))
471 return (SET_ERROR(ENOENT));
473 return (zfs_dozonecheck_impl(dataset, zoned, cr));
477 zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
481 if (dsl_prop_get_int_ds(ds, "jailed", &zoned))
482 return (SET_ERROR(ENOENT));
484 return (zfs_dozonecheck_impl(dataset, zoned, cr));
488 zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
489 const char *perm, cred_t *cr)
493 error = zfs_dozonecheck_ds(name, ds, cr);
495 error = secpolicy_zfs(cr);
497 error = dsl_deleg_access_impl(ds, perm, cr);
503 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
510 * First do a quick check for root in the global zone, which
511 * is allowed to do all write_perms. This ensures that zfs_ioc_*
512 * will get to handle nonexistent datasets.
514 if (INGLOBALZONE(curthread) && secpolicy_zfs(cr) == 0)
517 error = dsl_pool_hold(name, FTAG, &dp);
521 error = dsl_dataset_hold(dp, name, FTAG, &ds);
523 dsl_pool_rele(dp, FTAG);
527 error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
529 dsl_dataset_rele(ds, FTAG);
530 dsl_pool_rele(dp, FTAG);
536 * Policy for setting the security label property.
538 * Returns 0 for success, non-zero for access and other errors.
541 zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
543 char ds_hexsl[MAXNAMELEN];
544 bslabel_t ds_sl, new_sl;
545 boolean_t new_default = FALSE;
547 int needed_priv = -1;
550 /* First get the existing dataset label. */
551 error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
552 1, sizeof (ds_hexsl), &ds_hexsl, NULL);
554 return (SET_ERROR(EPERM));
556 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
559 /* The label must be translatable */
560 if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
561 return (SET_ERROR(EINVAL));
564 * In a non-global zone, disallow attempts to set a label that
565 * doesn't match that of the zone; otherwise no other checks
568 if (!INGLOBALZONE(curproc)) {
569 if (new_default || !blequal(&new_sl, CR_SL(CRED())))
570 return (SET_ERROR(EPERM));
575 * For global-zone datasets (i.e., those whose zoned property is
576 * "off", verify that the specified new label is valid for the
579 if (dsl_prop_get_integer(name,
580 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
581 return (SET_ERROR(EPERM));
583 if (zfs_check_global_label(name, strval) != 0)
584 return (SET_ERROR(EPERM));
588 * If the existing dataset label is nondefault, check if the
589 * dataset is mounted (label cannot be changed while mounted).
590 * Get the zfsvfs; if there isn't one, then the dataset isn't
591 * mounted (or isn't a dataset, doesn't exist, ...).
593 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
595 static char *setsl_tag = "setsl_tag";
598 * Try to own the dataset; abort if there is any error,
599 * (e.g., already mounted, in use, or other error).
601 error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE,
604 return (SET_ERROR(EPERM));
606 dmu_objset_disown(os, setsl_tag);
609 needed_priv = PRIV_FILE_DOWNGRADE_SL;
613 if (hexstr_to_label(strval, &new_sl) != 0)
614 return (SET_ERROR(EPERM));
616 if (blstrictdom(&ds_sl, &new_sl))
617 needed_priv = PRIV_FILE_DOWNGRADE_SL;
618 else if (blstrictdom(&new_sl, &ds_sl))
619 needed_priv = PRIV_FILE_UPGRADE_SL;
621 /* dataset currently has a default label */
623 needed_priv = PRIV_FILE_UPGRADE_SL;
627 if (needed_priv != -1)
628 return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
631 #endif /* SECLABEL */
634 zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
640 * Check permissions for special properties.
645 * Disallow setting of 'zoned' from within a local zone.
647 if (!INGLOBALZONE(curthread))
648 return (SET_ERROR(EPERM));
652 case ZFS_PROP_FILESYSTEM_LIMIT:
653 case ZFS_PROP_SNAPSHOT_LIMIT:
654 if (!INGLOBALZONE(curthread)) {
656 char setpoint[ZFS_MAX_DATASET_NAME_LEN];
658 * Unprivileged users are allowed to modify the
659 * limit on things *under* (ie. contained by)
660 * the thing they own.
662 if (dsl_prop_get_integer(dsname, "jailed", &zoned,
664 return (SET_ERROR(EPERM));
665 if (!zoned || strlen(dsname) <= strlen(setpoint))
666 return (SET_ERROR(EPERM));
670 case ZFS_PROP_MLSLABEL:
672 if (!is_system_labeled())
673 return (SET_ERROR(EPERM));
675 if (nvpair_value_string(propval, &strval) == 0) {
678 err = zfs_set_slabel_policy(dsname, strval, CRED());
688 return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
693 zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
697 error = zfs_dozonecheck(zc->zc_name, cr);
702 * permission to set permissions will be evaluated later in
703 * dsl_deleg_can_allow()
710 zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
712 return (zfs_secpolicy_write_perms(zc->zc_name,
713 ZFS_DELEG_PERM_ROLLBACK, cr));
718 zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
726 * Generate the current snapshot name from the given objsetid, then
727 * use that name for the secpolicy/zone checks.
729 cp = strchr(zc->zc_name, '@');
731 return (SET_ERROR(EINVAL));
732 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
736 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
738 dsl_pool_rele(dp, FTAG);
742 dsl_dataset_name(ds, zc->zc_name);
744 error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
745 ZFS_DELEG_PERM_SEND, cr);
746 dsl_dataset_rele(ds, FTAG);
747 dsl_pool_rele(dp, FTAG);
754 zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
756 return (zfs_secpolicy_write_perms(zc->zc_name,
757 ZFS_DELEG_PERM_SEND, cr));
762 zfs_secpolicy_deleg_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
767 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
768 NO_FOLLOW, NULL, &vp)) != 0)
771 /* Now make sure mntpnt and dataset are ZFS */
773 if (strcmp(vp->v_vfsp->mnt_stat.f_fstypename, "zfs") != 0 ||
774 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
775 zc->zc_name) != 0)) {
777 return (SET_ERROR(EPERM));
781 return (dsl_deleg_access(zc->zc_name,
782 ZFS_DELEG_PERM_SHARE, cr));
786 zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
788 if (!INGLOBALZONE(curthread))
789 return (SET_ERROR(EPERM));
791 if (secpolicy_nfs(cr) == 0) {
794 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
799 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
801 if (!INGLOBALZONE(curthread))
802 return (SET_ERROR(EPERM));
804 if (secpolicy_smb(cr) == 0) {
807 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
812 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
817 * Remove the @bla or /bla from the end of the name to get the parent.
819 (void) strncpy(parent, datasetname, parentsize);
820 cp = strrchr(parent, '@');
824 cp = strrchr(parent, '/');
826 return (SET_ERROR(ENOENT));
834 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
838 if ((error = zfs_secpolicy_write_perms(name,
839 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
842 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
847 zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
849 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
853 * Destroying snapshots with delegated permissions requires
854 * descendant mount and destroy permissions.
858 zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
861 nvpair_t *pair, *nextpair;
864 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
865 return (SET_ERROR(EINVAL));
866 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
868 nextpair = nvlist_next_nvpair(snaps, pair);
869 error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr);
870 if (error == ENOENT) {
872 * Ignore any snapshots that don't exist (we consider
873 * them "already destroyed"). Remove the name from the
874 * nvl here in case the snapshot is created between
875 * now and when we try to destroy it (in which case
876 * we don't want to destroy it since we haven't
877 * checked for permission).
879 fnvlist_remove_nvpair(snaps, pair);
890 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
892 char parentname[ZFS_MAX_DATASET_NAME_LEN];
895 if ((error = zfs_secpolicy_write_perms(from,
896 ZFS_DELEG_PERM_RENAME, cr)) != 0)
899 if ((error = zfs_secpolicy_write_perms(from,
900 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
903 if ((error = zfs_get_parent(to, parentname,
904 sizeof (parentname))) != 0)
907 if ((error = zfs_secpolicy_write_perms(parentname,
908 ZFS_DELEG_PERM_CREATE, cr)) != 0)
911 if ((error = zfs_secpolicy_write_perms(parentname,
912 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
920 zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
925 if ((zc->zc_cookie & 1) != 0) {
927 * This is recursive rename, so the starting snapshot might
928 * not exist. Check file system or volume permission instead.
930 at = strchr(zc->zc_name, '@');
936 error = zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr);
946 zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
949 dsl_dataset_t *clone;
952 error = zfs_secpolicy_write_perms(zc->zc_name,
953 ZFS_DELEG_PERM_PROMOTE, cr);
957 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
961 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
964 char parentname[ZFS_MAX_DATASET_NAME_LEN];
965 dsl_dataset_t *origin = NULL;
969 error = dsl_dataset_hold_obj(dd->dd_pool,
970 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin);
972 dsl_dataset_rele(clone, FTAG);
973 dsl_pool_rele(dp, FTAG);
977 error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
978 ZFS_DELEG_PERM_MOUNT, cr);
980 dsl_dataset_name(origin, parentname);
982 error = zfs_secpolicy_write_perms_ds(parentname, origin,
983 ZFS_DELEG_PERM_PROMOTE, cr);
985 dsl_dataset_rele(clone, FTAG);
986 dsl_dataset_rele(origin, FTAG);
988 dsl_pool_rele(dp, FTAG);
994 zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
998 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
999 ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
1002 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1003 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
1006 return (zfs_secpolicy_write_perms(zc->zc_name,
1007 ZFS_DELEG_PERM_CREATE, cr));
1011 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
1013 return (zfs_secpolicy_write_perms(name,
1014 ZFS_DELEG_PERM_SNAPSHOT, cr));
1018 * Check for permission to create each snapshot in the nvlist.
1022 zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1028 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
1029 return (SET_ERROR(EINVAL));
1030 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
1031 pair = nvlist_next_nvpair(snaps, pair)) {
1032 char *name = nvpair_name(pair);
1033 char *atp = strchr(name, '@');
1036 error = SET_ERROR(EINVAL);
1040 error = zfs_secpolicy_snapshot_perms(name, cr);
1049 * Check for permission to create each snapshot in the nvlist.
1053 zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1057 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
1058 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
1059 char *name = nvpair_name(pair);
1060 char *hashp = strchr(name, '#');
1062 if (hashp == NULL) {
1063 error = SET_ERROR(EINVAL);
1067 error = zfs_secpolicy_write_perms(name,
1068 ZFS_DELEG_PERM_BOOKMARK, cr);
1078 zfs_secpolicy_remap(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1080 return (zfs_secpolicy_write_perms(zc->zc_name,
1081 ZFS_DELEG_PERM_REMAP, cr));
1086 zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1088 nvpair_t *pair, *nextpair;
1091 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1093 char *name = nvpair_name(pair);
1094 char *hashp = strchr(name, '#');
1095 nextpair = nvlist_next_nvpair(innvl, pair);
1097 if (hashp == NULL) {
1098 error = SET_ERROR(EINVAL);
1103 error = zfs_secpolicy_write_perms(name,
1104 ZFS_DELEG_PERM_DESTROY, cr);
1106 if (error == ENOENT) {
1108 * Ignore any filesystems that don't exist (we consider
1109 * their bookmarks "already destroyed"). Remove
1110 * the name from the nvl here in case the filesystem
1111 * is created between now and when we try to destroy
1112 * the bookmark (in which case we don't want to
1113 * destroy it since we haven't checked for permission).
1115 fnvlist_remove_nvpair(innvl, pair);
1127 zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1130 * Even root must have a proper TSD so that we know what pool
1133 if (tsd_get(zfs_allow_log_key) == NULL)
1134 return (SET_ERROR(EPERM));
1139 zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1141 char parentname[ZFS_MAX_DATASET_NAME_LEN];
1145 if ((error = zfs_get_parent(zc->zc_name, parentname,
1146 sizeof (parentname))) != 0)
1149 if (nvlist_lookup_string(innvl, "origin", &origin) == 0 &&
1150 (error = zfs_secpolicy_write_perms(origin,
1151 ZFS_DELEG_PERM_CLONE, cr)) != 0)
1154 if ((error = zfs_secpolicy_write_perms(parentname,
1155 ZFS_DELEG_PERM_CREATE, cr)) != 0)
1158 return (zfs_secpolicy_write_perms(parentname,
1159 ZFS_DELEG_PERM_MOUNT, cr));
1163 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
1164 * SYS_CONFIG privilege, which is not available in a local zone.
1168 zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1170 if (secpolicy_sys_config(cr, B_FALSE) != 0)
1171 return (SET_ERROR(EPERM));
1177 * Policy for object to name lookups.
1181 zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1185 if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
1188 error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
1193 * Policy for fault injection. Requires all privileges.
1197 zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1199 return (secpolicy_zinject(cr));
1204 zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1206 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
1208 if (prop == ZPROP_INVAL) {
1209 if (!zfs_prop_user(zc->zc_value))
1210 return (SET_ERROR(EINVAL));
1211 return (zfs_secpolicy_write_perms(zc->zc_name,
1212 ZFS_DELEG_PERM_USERPROP, cr));
1214 return (zfs_secpolicy_setprop(zc->zc_name, prop,
1220 zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1222 int err = zfs_secpolicy_read(zc, innvl, cr);
1226 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1227 return (SET_ERROR(EINVAL));
1229 if (zc->zc_value[0] == 0) {
1231 * They are asking about a posix uid/gid. If it's
1232 * themself, allow it.
1234 if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
1235 zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
1236 if (zc->zc_guid == crgetuid(cr))
1239 if (groupmember(zc->zc_guid, cr))
1244 return (zfs_secpolicy_write_perms(zc->zc_name,
1245 userquota_perms[zc->zc_objset_type], cr));
1249 zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1251 int err = zfs_secpolicy_read(zc, innvl, cr);
1255 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1256 return (SET_ERROR(EINVAL));
1258 return (zfs_secpolicy_write_perms(zc->zc_name,
1259 userquota_perms[zc->zc_objset_type], cr));
1264 zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1266 return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
1272 zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1278 error = nvlist_lookup_nvlist(innvl, "holds", &holds);
1280 return (SET_ERROR(EINVAL));
1282 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
1283 pair = nvlist_next_nvpair(holds, pair)) {
1284 char fsname[ZFS_MAX_DATASET_NAME_LEN];
1285 error = dmu_fsname(nvpair_name(pair), fsname);
1288 error = zfs_secpolicy_write_perms(fsname,
1289 ZFS_DELEG_PERM_HOLD, cr);
1298 zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1303 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1304 pair = nvlist_next_nvpair(innvl, pair)) {
1305 char fsname[ZFS_MAX_DATASET_NAME_LEN];
1306 error = dmu_fsname(nvpair_name(pair), fsname);
1309 error = zfs_secpolicy_write_perms(fsname,
1310 ZFS_DELEG_PERM_RELEASE, cr);
1318 * Policy for allowing temporary snapshots to be taken or released
1321 zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1324 * A temporary snapshot is the same as a snapshot,
1325 * hold, destroy and release all rolled into one.
1326 * Delegated diff alone is sufficient that we allow this.
1330 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1331 ZFS_DELEG_PERM_DIFF, cr)) == 0)
1334 error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
1336 error = zfs_secpolicy_hold(zc, innvl, cr);
1338 error = zfs_secpolicy_release(zc, innvl, cr);
1340 error = zfs_secpolicy_destroy(zc, innvl, cr);
1345 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1348 get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1352 nvlist_t *list = NULL;
1355 * Read in and unpack the user-supplied nvlist.
1358 return (SET_ERROR(EINVAL));
1360 packed = kmem_alloc(size, KM_SLEEP);
1362 if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1364 kmem_free(packed, size);
1365 return (SET_ERROR(EFAULT));
1368 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1369 kmem_free(packed, size);
1373 kmem_free(packed, size);
1380 * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
1381 * Entries will be removed from the end of the nvlist, and one int32 entry
1382 * named "N_MORE_ERRORS" will be added indicating how many entries were
1386 nvlist_smush(nvlist_t *errors, size_t max)
1390 size = fnvlist_size(errors);
1393 nvpair_t *more_errors;
1397 return (SET_ERROR(ENOMEM));
1399 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
1400 more_errors = nvlist_prev_nvpair(errors, NULL);
1403 nvpair_t *pair = nvlist_prev_nvpair(errors,
1405 fnvlist_remove_nvpair(errors, pair);
1407 size = fnvlist_size(errors);
1408 } while (size > max);
1410 fnvlist_remove_nvpair(errors, more_errors);
1411 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
1412 ASSERT3U(fnvlist_size(errors), <=, max);
1419 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1421 char *packed = NULL;
1425 size = fnvlist_size(nvl);
1427 if (size > zc->zc_nvlist_dst_size) {
1429 * Solaris returns ENOMEM here, because even if an error is
1430 * returned from an ioctl(2), new zc_nvlist_dst_size will be
1431 * passed to the userland. This is not the case for FreeBSD.
1432 * We need to return 0, so the kernel will copy the
1433 * zc_nvlist_dst_size back and the userland can discover that a
1434 * bigger buffer is needed.
1438 packed = fnvlist_pack(nvl, &size);
1439 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1440 size, zc->zc_iflags) != 0)
1441 error = SET_ERROR(EFAULT);
1442 fnvlist_pack_free(packed, size);
1445 zc->zc_nvlist_dst_size = size;
1446 zc->zc_nvlist_dst_filled = B_TRUE;
1451 getzfsvfs_impl(objset_t *os, vfs_t **vfsp)
1456 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1457 return (SET_ERROR(EINVAL));
1460 mutex_enter(&os->os_user_ptr_lock);
1461 zfvp = dmu_objset_get_user(os);
1463 *vfsp = zfvp->z_vfs;
1464 vfs_ref(zfvp->z_vfs);
1466 error = SET_ERROR(ESRCH);
1468 mutex_exit(&os->os_user_ptr_lock);
1473 getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
1479 error = dmu_objset_hold(dsname, FTAG, &os);
1482 error = getzfsvfs_impl(os, &vfsp);
1483 dmu_objset_rele(os, FTAG);
1487 error = vfs_busy(vfsp, 0);
1491 error = SET_ERROR(ESRCH);
1493 *zfvp = vfsp->vfs_data;
1499 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
1500 * case its z_vfs will be NULL, and it will be opened as the owner.
1501 * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1502 * which prevents all vnode ops from running.
1505 zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
1509 if (getzfsvfs(name, zfvp) != 0)
1510 error = zfsvfs_create(name, zfvp);
1512 rrm_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
1515 if ((*zfvp)->z_unmounted) {
1517 * XXX we could probably try again, since the unmounting
1518 * thread should be just about to disassociate the
1519 * objset from the zfsvfs.
1521 rrm_exit(&(*zfvp)->z_teardown_lock, tag);
1522 return (SET_ERROR(EBUSY));
1526 * vfs_busy() ensures that the filesystem is not and
1527 * can not be unmounted.
1529 ASSERT(!(*zfvp)->z_unmounted);
1536 zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
1538 rrm_exit(&zfsvfs->z_teardown_lock, tag);
1540 if (zfsvfs->z_vfs) {
1542 VFS_RELE(zfsvfs->z_vfs);
1544 vfs_unbusy(zfsvfs->z_vfs);
1547 dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1548 zfsvfs_free(zfsvfs);
1553 zfs_ioc_pool_create(zfs_cmd_t *zc)
1556 nvlist_t *config, *props = NULL;
1557 nvlist_t *rootprops = NULL;
1558 nvlist_t *zplprops = NULL;
1560 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1561 zc->zc_iflags, &config))
1564 if (zc->zc_nvlist_src_size != 0 && (error =
1565 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1566 zc->zc_iflags, &props))) {
1567 nvlist_free(config);
1572 nvlist_t *nvl = NULL;
1573 uint64_t version = SPA_VERSION;
1575 (void) nvlist_lookup_uint64(props,
1576 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1577 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1578 error = SET_ERROR(EINVAL);
1579 goto pool_props_bad;
1581 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
1583 error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
1585 nvlist_free(config);
1589 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1591 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1592 error = zfs_fill_zplprops_root(version, rootprops,
1595 goto pool_props_bad;
1598 error = spa_create(zc->zc_name, config, props, zplprops);
1601 * Set the remaining root properties
1603 if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
1604 ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
1605 (void) spa_destroy(zc->zc_name);
1608 nvlist_free(rootprops);
1609 nvlist_free(zplprops);
1610 nvlist_free(config);
1617 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1620 zfs_log_history(zc);
1621 error = spa_destroy(zc->zc_name);
1623 zvol_remove_minors(zc->zc_name);
1628 zfs_ioc_pool_import(zfs_cmd_t *zc)
1630 nvlist_t *config, *props = NULL;
1634 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1635 zc->zc_iflags, &config)) != 0)
1638 if (zc->zc_nvlist_src_size != 0 && (error =
1639 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1640 zc->zc_iflags, &props))) {
1641 nvlist_free(config);
1645 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1646 guid != zc->zc_guid)
1647 error = SET_ERROR(EINVAL);
1649 error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1651 if (zc->zc_nvlist_dst != 0) {
1654 if ((err = put_nvlist(zc, config)) != 0)
1658 nvlist_free(config);
1666 zfs_ioc_pool_export(zfs_cmd_t *zc)
1669 boolean_t force = (boolean_t)zc->zc_cookie;
1670 boolean_t hardforce = (boolean_t)zc->zc_guid;
1672 zfs_log_history(zc);
1673 error = spa_export(zc->zc_name, NULL, force, hardforce);
1675 zvol_remove_minors(zc->zc_name);
1680 zfs_ioc_pool_configs(zfs_cmd_t *zc)
1685 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1686 return (SET_ERROR(EEXIST));
1688 error = put_nvlist(zc, configs);
1690 nvlist_free(configs);
1697 * zc_name name of the pool
1700 * zc_cookie real errno
1701 * zc_nvlist_dst config nvlist
1702 * zc_nvlist_dst_size size of config nvlist
1705 zfs_ioc_pool_stats(zfs_cmd_t *zc)
1711 error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1712 sizeof (zc->zc_value));
1714 if (config != NULL) {
1715 ret = put_nvlist(zc, config);
1716 nvlist_free(config);
1719 * The config may be present even if 'error' is non-zero.
1720 * In this case we return success, and preserve the real errno
1723 zc->zc_cookie = error;
1732 * Try to import the given pool, returning pool stats as appropriate so that
1733 * user land knows which devices are available and overall pool health.
1736 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1738 nvlist_t *tryconfig, *config;
1741 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1742 zc->zc_iflags, &tryconfig)) != 0)
1745 config = spa_tryimport(tryconfig);
1747 nvlist_free(tryconfig);
1750 return (SET_ERROR(EINVAL));
1752 error = put_nvlist(zc, config);
1753 nvlist_free(config);
1760 * zc_name name of the pool
1761 * zc_cookie scan func (pool_scan_func_t)
1762 * zc_flags scrub pause/resume flag (pool_scrub_cmd_t)
1765 zfs_ioc_pool_scan(zfs_cmd_t *zc)
1770 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1773 if (zc->zc_flags >= POOL_SCRUB_FLAGS_END)
1774 return (SET_ERROR(EINVAL));
1776 if (zc->zc_flags == POOL_SCRUB_PAUSE)
1777 error = spa_scrub_pause_resume(spa, POOL_SCRUB_PAUSE);
1778 else if (zc->zc_cookie == POOL_SCAN_NONE)
1779 error = spa_scan_stop(spa);
1781 error = spa_scan(spa, zc->zc_cookie);
1783 spa_close(spa, FTAG);
1789 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1794 error = spa_open(zc->zc_name, &spa, FTAG);
1797 spa_close(spa, FTAG);
1803 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1808 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1811 if (zc->zc_cookie < spa_version(spa) ||
1812 !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1813 spa_close(spa, FTAG);
1814 return (SET_ERROR(EINVAL));
1817 spa_upgrade(spa, zc->zc_cookie);
1818 spa_close(spa, FTAG);
1824 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1831 if ((size = zc->zc_history_len) == 0)
1832 return (SET_ERROR(EINVAL));
1834 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1837 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1838 spa_close(spa, FTAG);
1839 return (SET_ERROR(ENOTSUP));
1842 hist_buf = kmem_alloc(size, KM_SLEEP);
1843 if ((error = spa_history_get(spa, &zc->zc_history_offset,
1844 &zc->zc_history_len, hist_buf)) == 0) {
1845 error = ddi_copyout(hist_buf,
1846 (void *)(uintptr_t)zc->zc_history,
1847 zc->zc_history_len, zc->zc_iflags);
1850 spa_close(spa, FTAG);
1851 kmem_free(hist_buf, size);
1856 zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1861 error = spa_open(zc->zc_name, &spa, FTAG);
1863 error = spa_change_guid(spa);
1864 spa_close(spa, FTAG);
1870 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1872 return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
1877 * zc_name name of filesystem
1878 * zc_obj object to find
1881 * zc_value name of object
1884 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1889 /* XXX reading from objset not owned */
1890 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1892 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1893 dmu_objset_rele(os, FTAG);
1894 return (SET_ERROR(EINVAL));
1896 error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
1897 sizeof (zc->zc_value));
1898 dmu_objset_rele(os, FTAG);
1905 * zc_name name of filesystem
1906 * zc_obj object to find
1909 * zc_stat stats on object
1910 * zc_value path to object
1913 zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1918 /* XXX reading from objset not owned */
1919 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1921 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1922 dmu_objset_rele(os, FTAG);
1923 return (SET_ERROR(EINVAL));
1925 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1926 sizeof (zc->zc_value));
1927 dmu_objset_rele(os, FTAG);
1933 zfs_ioc_vdev_add(zfs_cmd_t *zc)
1937 nvlist_t *config, **l2cache, **spares;
1938 uint_t nl2cache = 0, nspares = 0;
1940 error = spa_open(zc->zc_name, &spa, FTAG);
1944 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1945 zc->zc_iflags, &config);
1946 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1947 &l2cache, &nl2cache);
1949 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1954 * A root pool with concatenated devices is not supported.
1955 * Thus, can not add a device to a root pool.
1957 * Intent log device can not be added to a rootpool because
1958 * during mountroot, zil is replayed, a seperated log device
1959 * can not be accessed during the mountroot time.
1961 * l2cache and spare devices are ok to be added to a rootpool.
1963 if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
1964 nvlist_free(config);
1965 spa_close(spa, FTAG);
1966 return (SET_ERROR(EDOM));
1968 #endif /* illumos */
1971 error = spa_vdev_add(spa, config);
1972 nvlist_free(config);
1974 spa_close(spa, FTAG);
1980 * zc_name name of the pool
1981 * zc_guid guid of vdev to remove
1982 * zc_cookie cancel removal
1985 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1990 error = spa_open(zc->zc_name, &spa, FTAG);
1993 if (zc->zc_cookie != 0) {
1994 error = spa_vdev_remove_cancel(spa);
1996 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1998 spa_close(spa, FTAG);
2003 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
2007 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
2009 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2011 switch (zc->zc_cookie) {
2012 case VDEV_STATE_ONLINE:
2013 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
2016 case VDEV_STATE_OFFLINE:
2017 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
2020 case VDEV_STATE_FAULTED:
2021 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
2022 zc->zc_obj != VDEV_AUX_EXTERNAL)
2023 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
2025 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
2028 case VDEV_STATE_DEGRADED:
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_degrade(spa, zc->zc_guid, zc->zc_obj);
2037 error = SET_ERROR(EINVAL);
2039 zc->zc_cookie = newstate;
2040 spa_close(spa, FTAG);
2045 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
2048 int replacing = zc->zc_cookie;
2052 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2055 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
2056 zc->zc_iflags, &config)) == 0) {
2057 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
2058 nvlist_free(config);
2061 spa_close(spa, FTAG);
2066 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
2071 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2074 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
2076 spa_close(spa, FTAG);
2081 zfs_ioc_vdev_split(zfs_cmd_t *zc)
2084 nvlist_t *config, *props = NULL;
2086 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
2088 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2091 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
2092 zc->zc_iflags, &config)) {
2093 spa_close(spa, FTAG);
2097 if (zc->zc_nvlist_src_size != 0 && (error =
2098 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2099 zc->zc_iflags, &props))) {
2100 spa_close(spa, FTAG);
2101 nvlist_free(config);
2105 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
2107 spa_close(spa, FTAG);
2109 nvlist_free(config);
2116 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
2119 char *path = zc->zc_value;
2120 uint64_t guid = zc->zc_guid;
2123 error = spa_open(zc->zc_name, &spa, FTAG);
2127 error = spa_vdev_setpath(spa, guid, path);
2128 spa_close(spa, FTAG);
2133 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
2136 char *fru = zc->zc_value;
2137 uint64_t guid = zc->zc_guid;
2140 error = spa_open(zc->zc_name, &spa, FTAG);
2144 error = spa_vdev_setfru(spa, guid, fru);
2145 spa_close(spa, FTAG);
2150 zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
2155 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2157 if (zc->zc_nvlist_dst != 0 &&
2158 (error = dsl_prop_get_all(os, &nv)) == 0) {
2159 dmu_objset_stats(os, nv);
2161 * NB: zvol_get_stats() will read the objset contents,
2162 * which we aren't supposed to do with a
2163 * DS_MODE_USER hold, because it could be
2164 * inconsistent. So this is a bit of a workaround...
2165 * XXX reading with out owning
2167 if (!zc->zc_objset_stats.dds_inconsistent &&
2168 dmu_objset_type(os) == DMU_OST_ZVOL) {
2169 error = zvol_get_stats(os, nv);
2174 error = put_nvlist(zc, nv);
2183 * zc_name name of filesystem
2184 * zc_nvlist_dst_size size of buffer for property nvlist
2187 * zc_objset_stats stats
2188 * zc_nvlist_dst property nvlist
2189 * zc_nvlist_dst_size size of property nvlist
2192 zfs_ioc_objset_stats(zfs_cmd_t *zc)
2197 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2199 error = zfs_ioc_objset_stats_impl(zc, os);
2200 dmu_objset_rele(os, FTAG);
2203 if (error == ENOMEM)
2210 * zc_name name of filesystem
2211 * zc_nvlist_dst_size size of buffer for property nvlist
2214 * zc_nvlist_dst received property nvlist
2215 * zc_nvlist_dst_size size of received property nvlist
2217 * Gets received properties (distinct from local properties on or after
2218 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
2219 * local property values.
2222 zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
2228 * Without this check, we would return local property values if the
2229 * caller has not already received properties on or after
2230 * SPA_VERSION_RECVD_PROPS.
2232 if (!dsl_prop_get_hasrecvd(zc->zc_name))
2233 return (SET_ERROR(ENOTSUP));
2235 if (zc->zc_nvlist_dst != 0 &&
2236 (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
2237 error = put_nvlist(zc, nv);
2245 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2251 * zfs_get_zplprop() will either find a value or give us
2252 * the default value (if there is one).
2254 if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2256 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2262 * zc_name name of filesystem
2263 * zc_nvlist_dst_size size of buffer for zpl property nvlist
2266 * zc_nvlist_dst zpl property nvlist
2267 * zc_nvlist_dst_size size of zpl property nvlist
2270 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2275 /* XXX reading without owning */
2276 if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
2279 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2282 * NB: nvl_add_zplprop() will read the objset contents,
2283 * which we aren't supposed to do with a DS_MODE_USER
2284 * hold, because it could be inconsistent.
2286 if (zc->zc_nvlist_dst != 0 &&
2287 !zc->zc_objset_stats.dds_inconsistent &&
2288 dmu_objset_type(os) == DMU_OST_ZFS) {
2291 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2292 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2293 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2294 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2295 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2296 err = put_nvlist(zc, nv);
2299 err = SET_ERROR(ENOENT);
2301 dmu_objset_rele(os, FTAG);
2306 dataset_name_hidden(const char *name)
2309 * Skip over datasets that are not visible in this zone,
2310 * internal datasets (which have a $ in their name), and
2311 * temporary datasets (which have a % in their name).
2313 if (strchr(name, '$') != NULL)
2315 if (strchr(name, '%') != NULL)
2317 if (!INGLOBALZONE(curthread) && !zone_dataset_visible(name, NULL))
2324 * zc_name name of filesystem
2325 * zc_cookie zap cursor
2326 * zc_nvlist_dst_size size of buffer for property nvlist
2329 * zc_name name of next filesystem
2330 * zc_cookie zap cursor
2331 * zc_objset_stats stats
2332 * zc_nvlist_dst property nvlist
2333 * zc_nvlist_dst_size size of property nvlist
2336 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2341 size_t orig_len = strlen(zc->zc_name);
2344 if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
2345 if (error == ENOENT)
2346 error = SET_ERROR(ESRCH);
2350 p = strrchr(zc->zc_name, '/');
2351 if (p == NULL || p[1] != '\0')
2352 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2353 p = zc->zc_name + strlen(zc->zc_name);
2356 error = dmu_dir_list_next(os,
2357 sizeof (zc->zc_name) - (p - zc->zc_name), p,
2358 NULL, &zc->zc_cookie);
2359 if (error == ENOENT)
2360 error = SET_ERROR(ESRCH);
2361 } while (error == 0 && dataset_name_hidden(zc->zc_name));
2362 dmu_objset_rele(os, FTAG);
2365 * If it's an internal dataset (ie. with a '$' in its name),
2366 * don't try to get stats for it, otherwise we'll return ENOENT.
2368 if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
2369 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2370 if (error == ENOENT) {
2371 /* We lost a race with destroy, get the next one. */
2372 zc->zc_name[orig_len] = '\0';
2381 * zc_name name of filesystem
2382 * zc_cookie zap cursor
2383 * zc_nvlist_dst_size size of buffer for property nvlist
2384 * zc_simple when set, only name is requested
2387 * zc_name name of next snapshot
2388 * zc_objset_stats stats
2389 * zc_nvlist_dst property nvlist
2390 * zc_nvlist_dst_size size of property nvlist
2393 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2398 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2400 return (error == ENOENT ? ESRCH : error);
2404 * A dataset name of maximum length cannot have any snapshots,
2405 * so exit immediately.
2407 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >=
2408 ZFS_MAX_DATASET_NAME_LEN) {
2409 dmu_objset_rele(os, FTAG);
2410 return (SET_ERROR(ESRCH));
2413 error = dmu_snapshot_list_next(os,
2414 sizeof (zc->zc_name) - strlen(zc->zc_name),
2415 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2418 if (error == 0 && !zc->zc_simple) {
2420 dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2422 error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
2426 error = dmu_objset_from_ds(ds, &ossnap);
2428 error = zfs_ioc_objset_stats_impl(zc, ossnap);
2429 dsl_dataset_rele(ds, FTAG);
2431 } else if (error == ENOENT) {
2432 error = SET_ERROR(ESRCH);
2435 dmu_objset_rele(os, FTAG);
2436 /* if we failed, undo the @ that we tacked on to zc_name */
2438 *strchr(zc->zc_name, '@') = '\0';
2443 zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2445 const char *propname = nvpair_name(pair);
2447 unsigned int vallen;
2450 zfs_userquota_prop_t type;
2456 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2458 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2459 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2461 return (SET_ERROR(EINVAL));
2465 * A correctly constructed propname is encoded as
2466 * userquota@<rid>-<domain>.
2468 if ((dash = strchr(propname, '-')) == NULL ||
2469 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2471 return (SET_ERROR(EINVAL));
2478 err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
2480 err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
2481 zfsvfs_rele(zfsvfs, FTAG);
2488 * If the named property is one that has a special function to set its value,
2489 * return 0 on success and a positive error code on failure; otherwise if it is
2490 * not one of the special properties handled by this function, return -1.
2492 * XXX: It would be better for callers of the property interface if we handled
2493 * these special cases in dsl_prop.c (in the dsl layer).
2496 zfs_prop_set_special(const char *dsname, zprop_source_t source,
2499 const char *propname = nvpair_name(pair);
2500 zfs_prop_t prop = zfs_name_to_prop(propname);
2504 if (prop == ZPROP_INVAL) {
2505 if (zfs_prop_userquota(propname))
2506 return (zfs_prop_set_userquota(dsname, pair));
2510 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2512 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2513 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2517 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
2520 VERIFY(0 == nvpair_value_uint64(pair, &intval));
2523 case ZFS_PROP_QUOTA:
2524 err = dsl_dir_set_quota(dsname, source, intval);
2526 case ZFS_PROP_REFQUOTA:
2527 err = dsl_dataset_set_refquota(dsname, source, intval);
2529 case ZFS_PROP_FILESYSTEM_LIMIT:
2530 case ZFS_PROP_SNAPSHOT_LIMIT:
2531 if (intval == UINT64_MAX) {
2532 /* clearing the limit, just do it */
2535 err = dsl_dir_activate_fs_ss_limit(dsname);
2538 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2539 * default path to set the value in the nvlist.
2544 case ZFS_PROP_RESERVATION:
2545 err = dsl_dir_set_reservation(dsname, source, intval);
2547 case ZFS_PROP_REFRESERVATION:
2548 err = dsl_dataset_set_refreservation(dsname, source, intval);
2550 case ZFS_PROP_VOLSIZE:
2551 err = zvol_set_volsize(dsname, intval);
2553 case ZFS_PROP_VERSION:
2557 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2560 err = zfs_set_version(zfsvfs, intval);
2561 zfsvfs_rele(zfsvfs, FTAG);
2563 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2566 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2567 (void) strcpy(zc->zc_name, dsname);
2568 (void) zfs_ioc_userspace_upgrade(zc);
2569 kmem_free(zc, sizeof (zfs_cmd_t));
2581 * This function is best effort. If it fails to set any of the given properties,
2582 * it continues to set as many as it can and returns the last error
2583 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2584 * with the list of names of all the properties that failed along with the
2585 * corresponding error numbers.
2587 * If every property is set successfully, zero is returned and errlist is not
2591 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2599 nvlist_t *genericnvl = fnvlist_alloc();
2600 nvlist_t *retrynvl = fnvlist_alloc();
2604 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2605 const char *propname = nvpair_name(pair);
2606 zfs_prop_t prop = zfs_name_to_prop(propname);
2609 /* decode the property value */
2611 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2613 attrs = fnvpair_value_nvlist(pair);
2614 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2616 err = SET_ERROR(EINVAL);
2619 /* Validate value type */
2620 if (err == 0 && prop == ZPROP_INVAL) {
2621 if (zfs_prop_user(propname)) {
2622 if (nvpair_type(propval) != DATA_TYPE_STRING)
2623 err = SET_ERROR(EINVAL);
2624 } else if (zfs_prop_userquota(propname)) {
2625 if (nvpair_type(propval) !=
2626 DATA_TYPE_UINT64_ARRAY)
2627 err = SET_ERROR(EINVAL);
2629 err = SET_ERROR(EINVAL);
2631 } else if (err == 0) {
2632 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2633 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2634 err = SET_ERROR(EINVAL);
2635 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2638 intval = fnvpair_value_uint64(propval);
2640 switch (zfs_prop_get_type(prop)) {
2641 case PROP_TYPE_NUMBER:
2643 case PROP_TYPE_STRING:
2644 err = SET_ERROR(EINVAL);
2646 case PROP_TYPE_INDEX:
2647 if (zfs_prop_index_to_string(prop,
2648 intval, &unused) != 0)
2649 err = SET_ERROR(EINVAL);
2653 "unknown property type");
2656 err = SET_ERROR(EINVAL);
2660 /* Validate permissions */
2662 err = zfs_check_settable(dsname, pair, CRED());
2665 err = zfs_prop_set_special(dsname, source, pair);
2668 * For better performance we build up a list of
2669 * properties to set in a single transaction.
2671 err = nvlist_add_nvpair(genericnvl, pair);
2672 } else if (err != 0 && nvl != retrynvl) {
2674 * This may be a spurious error caused by
2675 * receiving quota and reservation out of order.
2676 * Try again in a second pass.
2678 err = nvlist_add_nvpair(retrynvl, pair);
2683 if (errlist != NULL)
2684 fnvlist_add_int32(errlist, propname, err);
2689 if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2694 if (!nvlist_empty(genericnvl) &&
2695 dsl_props_set(dsname, source, genericnvl) != 0) {
2697 * If this fails, we still want to set as many properties as we
2698 * can, so try setting them individually.
2701 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2702 const char *propname = nvpair_name(pair);
2706 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2708 attrs = fnvpair_value_nvlist(pair);
2709 propval = fnvlist_lookup_nvpair(attrs,
2713 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2714 strval = fnvpair_value_string(propval);
2715 err = dsl_prop_set_string(dsname, propname,
2718 intval = fnvpair_value_uint64(propval);
2719 err = dsl_prop_set_int(dsname, propname, source,
2724 if (errlist != NULL) {
2725 fnvlist_add_int32(errlist, propname,
2732 nvlist_free(genericnvl);
2733 nvlist_free(retrynvl);
2739 * Check that all the properties are valid user properties.
2742 zfs_check_userprops(const char *fsname, nvlist_t *nvl)
2744 nvpair_t *pair = NULL;
2747 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2748 const char *propname = nvpair_name(pair);
2750 if (!zfs_prop_user(propname) ||
2751 nvpair_type(pair) != DATA_TYPE_STRING)
2752 return (SET_ERROR(EINVAL));
2754 if (error = zfs_secpolicy_write_perms(fsname,
2755 ZFS_DELEG_PERM_USERPROP, CRED()))
2758 if (strlen(propname) >= ZAP_MAXNAMELEN)
2759 return (SET_ERROR(ENAMETOOLONG));
2761 if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN)
2768 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2772 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2775 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2776 if (nvlist_exists(skipped, nvpair_name(pair)))
2779 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2784 clear_received_props(const char *dsname, nvlist_t *props,
2788 nvlist_t *cleared_props = NULL;
2789 props_skip(props, skipped, &cleared_props);
2790 if (!nvlist_empty(cleared_props)) {
2792 * Acts on local properties until the dataset has received
2793 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2795 zprop_source_t flags = (ZPROP_SRC_NONE |
2796 (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
2797 err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
2799 nvlist_free(cleared_props);
2805 * zc_name name of filesystem
2806 * zc_value name of property to set
2807 * zc_nvlist_src{_size} nvlist of properties to apply
2808 * zc_cookie received properties flag
2811 * zc_nvlist_dst{_size} error for each unapplied received property
2814 zfs_ioc_set_prop(zfs_cmd_t *zc)
2817 boolean_t received = zc->zc_cookie;
2818 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2823 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2824 zc->zc_iflags, &nvl)) != 0)
2828 nvlist_t *origprops;
2830 if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
2831 (void) clear_received_props(zc->zc_name,
2833 nvlist_free(origprops);
2836 error = dsl_prop_set_hasrecvd(zc->zc_name);
2839 errors = fnvlist_alloc();
2841 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
2843 if (zc->zc_nvlist_dst != 0 && errors != NULL) {
2844 (void) put_nvlist(zc, errors);
2847 nvlist_free(errors);
2854 * zc_name name of filesystem
2855 * zc_value name of property to inherit
2856 * zc_cookie revert to received value if TRUE
2861 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2863 const char *propname = zc->zc_value;
2864 zfs_prop_t prop = zfs_name_to_prop(propname);
2865 boolean_t received = zc->zc_cookie;
2866 zprop_source_t source = (received
2867 ? ZPROP_SRC_NONE /* revert to received value, if any */
2868 : ZPROP_SRC_INHERITED); /* explicitly inherit */
2877 * zfs_prop_set_special() expects properties in the form of an
2878 * nvpair with type info.
2880 if (prop == ZPROP_INVAL) {
2881 if (!zfs_prop_user(propname))
2882 return (SET_ERROR(EINVAL));
2884 type = PROP_TYPE_STRING;
2885 } else if (prop == ZFS_PROP_VOLSIZE ||
2886 prop == ZFS_PROP_VERSION) {
2887 return (SET_ERROR(EINVAL));
2889 type = zfs_prop_get_type(prop);
2892 VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2895 case PROP_TYPE_STRING:
2896 VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2898 case PROP_TYPE_NUMBER:
2899 case PROP_TYPE_INDEX:
2900 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2904 return (SET_ERROR(EINVAL));
2907 pair = nvlist_next_nvpair(dummy, NULL);
2908 err = zfs_prop_set_special(zc->zc_name, source, pair);
2911 return (err); /* special property already handled */
2914 * Only check this in the non-received case. We want to allow
2915 * 'inherit -S' to revert non-inheritable properties like quota
2916 * and reservation to the received or default values even though
2917 * they are not considered inheritable.
2919 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2920 return (SET_ERROR(EINVAL));
2923 /* property name has been validated by zfs_secpolicy_inherit_prop() */
2924 return (dsl_prop_inherit(zc->zc_name, zc->zc_value, source));
2928 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2935 if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2936 zc->zc_iflags, &props))
2940 * If the only property is the configfile, then just do a spa_lookup()
2941 * to handle the faulted case.
2943 pair = nvlist_next_nvpair(props, NULL);
2944 if (pair != NULL && strcmp(nvpair_name(pair),
2945 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2946 nvlist_next_nvpair(props, pair) == NULL) {
2947 mutex_enter(&spa_namespace_lock);
2948 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2949 spa_configfile_set(spa, props, B_FALSE);
2950 spa_write_cachefile(spa, B_FALSE, B_TRUE);
2952 mutex_exit(&spa_namespace_lock);
2959 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2964 error = spa_prop_set(spa, props);
2967 spa_close(spa, FTAG);
2973 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2977 nvlist_t *nvp = NULL;
2979 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2981 * If the pool is faulted, there may be properties we can still
2982 * get (such as altroot and cachefile), so attempt to get them
2985 mutex_enter(&spa_namespace_lock);
2986 if ((spa = spa_lookup(zc->zc_name)) != NULL)
2987 error = spa_prop_get(spa, &nvp);
2988 mutex_exit(&spa_namespace_lock);
2990 error = spa_prop_get(spa, &nvp);
2991 spa_close(spa, FTAG);
2994 if (error == 0 && zc->zc_nvlist_dst != 0)
2995 error = put_nvlist(zc, nvp);
2997 error = SET_ERROR(EFAULT);
3005 * zc_name name of filesystem
3006 * zc_nvlist_src{_size} nvlist of delegated permissions
3007 * zc_perm_action allow/unallow flag
3012 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
3015 nvlist_t *fsaclnv = NULL;
3017 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3018 zc->zc_iflags, &fsaclnv)) != 0)
3022 * Verify nvlist is constructed correctly
3024 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
3025 nvlist_free(fsaclnv);
3026 return (SET_ERROR(EINVAL));
3030 * If we don't have PRIV_SYS_MOUNT, then validate
3031 * that user is allowed to hand out each permission in
3035 error = secpolicy_zfs(CRED());
3037 if (zc->zc_perm_action == B_FALSE) {
3038 error = dsl_deleg_can_allow(zc->zc_name,
3041 error = dsl_deleg_can_unallow(zc->zc_name,
3047 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
3049 nvlist_free(fsaclnv);
3055 * zc_name name of filesystem
3058 * zc_nvlist_src{_size} nvlist of delegated permissions
3061 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
3066 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
3067 error = put_nvlist(zc, nvp);
3076 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3078 zfs_creat_t *zct = arg;
3080 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
3083 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
3087 * os parent objset pointer (NULL if root fs)
3088 * fuids_ok fuids allowed in this version of the spa?
3089 * sa_ok SAs allowed in this version of the spa?
3090 * createprops list of properties requested by creator
3093 * zplprops values for the zplprops we attach to the master node object
3094 * is_ci true if requested file system will be purely case-insensitive
3096 * Determine the settings for utf8only, normalization and
3097 * casesensitivity. Specific values may have been requested by the
3098 * creator and/or we can inherit values from the parent dataset. If
3099 * the file system is of too early a vintage, a creator can not
3100 * request settings for these properties, even if the requested
3101 * setting is the default value. We don't actually want to create dsl
3102 * properties for these, so remove them from the source nvlist after
3106 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
3107 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
3108 nvlist_t *zplprops, boolean_t *is_ci)
3110 uint64_t sense = ZFS_PROP_UNDEFINED;
3111 uint64_t norm = ZFS_PROP_UNDEFINED;
3112 uint64_t u8 = ZFS_PROP_UNDEFINED;
3114 ASSERT(zplprops != NULL);
3116 if (os != NULL && os->os_phys->os_type != DMU_OST_ZFS)
3117 return (SET_ERROR(EINVAL));
3120 * Pull out creator prop choices, if any.
3123 (void) nvlist_lookup_uint64(createprops,
3124 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
3125 (void) nvlist_lookup_uint64(createprops,
3126 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
3127 (void) nvlist_remove_all(createprops,
3128 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
3129 (void) nvlist_lookup_uint64(createprops,
3130 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
3131 (void) nvlist_remove_all(createprops,
3132 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
3133 (void) nvlist_lookup_uint64(createprops,
3134 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
3135 (void) nvlist_remove_all(createprops,
3136 zfs_prop_to_name(ZFS_PROP_CASE));
3140 * If the zpl version requested is whacky or the file system
3141 * or pool is version is too "young" to support normalization
3142 * and the creator tried to set a value for one of the props,
3145 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
3146 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
3147 (zplver >= ZPL_VERSION_SA && !sa_ok) ||
3148 (zplver < ZPL_VERSION_NORMALIZATION &&
3149 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
3150 sense != ZFS_PROP_UNDEFINED)))
3151 return (SET_ERROR(ENOTSUP));
3154 * Put the version in the zplprops
3156 VERIFY(nvlist_add_uint64(zplprops,
3157 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3159 if (norm == ZFS_PROP_UNDEFINED)
3160 VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
3161 VERIFY(nvlist_add_uint64(zplprops,
3162 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3165 * If we're normalizing, names must always be valid UTF-8 strings.
3169 if (u8 == ZFS_PROP_UNDEFINED)
3170 VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
3171 VERIFY(nvlist_add_uint64(zplprops,
3172 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3174 if (sense == ZFS_PROP_UNDEFINED)
3175 VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
3176 VERIFY(nvlist_add_uint64(zplprops,
3177 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3180 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
3186 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
3187 nvlist_t *zplprops, boolean_t *is_ci)
3189 boolean_t fuids_ok, sa_ok;
3190 uint64_t zplver = ZPL_VERSION;
3191 objset_t *os = NULL;
3192 char parentname[ZFS_MAX_DATASET_NAME_LEN];
3198 (void) strlcpy(parentname, dataset, sizeof (parentname));
3199 cp = strrchr(parentname, '/');
3203 if ((error = spa_open(dataset, &spa, FTAG)) != 0)
3206 spa_vers = spa_version(spa);
3207 spa_close(spa, FTAG);
3209 zplver = zfs_zpl_version_map(spa_vers);
3210 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3211 sa_ok = (zplver >= ZPL_VERSION_SA);
3214 * Open parent object set so we can inherit zplprop values.
3216 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
3219 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
3221 dmu_objset_rele(os, FTAG);
3226 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
3227 nvlist_t *zplprops, boolean_t *is_ci)
3231 uint64_t zplver = ZPL_VERSION;
3234 zplver = zfs_zpl_version_map(spa_vers);
3235 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3236 sa_ok = (zplver >= ZPL_VERSION_SA);
3238 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
3239 createprops, zplprops, is_ci);
3245 * "type" -> dmu_objset_type_t (int32)
3246 * (optional) "props" -> { prop -> value }
3249 * outnvl: propname -> error code (int32)
3252 zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3255 zfs_creat_t zct = { 0 };
3256 nvlist_t *nvprops = NULL;
3257 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
3259 dmu_objset_type_t type;
3260 boolean_t is_insensitive = B_FALSE;
3262 if (nvlist_lookup_int32(innvl, "type", &type32) != 0)
3263 return (SET_ERROR(EINVAL));
3265 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3269 cbfunc = zfs_create_cb;
3273 cbfunc = zvol_create_cb;
3280 if (strchr(fsname, '@') ||
3281 strchr(fsname, '%'))
3282 return (SET_ERROR(EINVAL));
3284 zct.zct_props = nvprops;
3287 return (SET_ERROR(EINVAL));
3289 if (type == DMU_OST_ZVOL) {
3290 uint64_t volsize, volblocksize;
3292 if (nvprops == NULL)
3293 return (SET_ERROR(EINVAL));
3294 if (nvlist_lookup_uint64(nvprops,
3295 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3296 return (SET_ERROR(EINVAL));
3298 if ((error = nvlist_lookup_uint64(nvprops,
3299 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3300 &volblocksize)) != 0 && error != ENOENT)
3301 return (SET_ERROR(EINVAL));
3304 volblocksize = zfs_prop_default_numeric(
3305 ZFS_PROP_VOLBLOCKSIZE);
3307 if ((error = zvol_check_volblocksize(
3308 volblocksize)) != 0 ||
3309 (error = zvol_check_volsize(volsize,
3310 volblocksize)) != 0)
3312 } else if (type == DMU_OST_ZFS) {
3316 * We have to have normalization and
3317 * case-folding flags correct when we do the
3318 * file system creation, so go figure them out
3321 VERIFY(nvlist_alloc(&zct.zct_zplprops,
3322 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3323 error = zfs_fill_zplprops(fsname, nvprops,
3324 zct.zct_zplprops, &is_insensitive);
3326 nvlist_free(zct.zct_zplprops);
3331 error = dmu_objset_create(fsname, type,
3332 is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
3333 nvlist_free(zct.zct_zplprops);
3336 * It would be nice to do this atomically.
3339 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3342 (void) dsl_destroy_head(fsname);
3345 if (error == 0 && type == DMU_OST_ZVOL)
3346 zvol_create_minors(fsname);
3353 * "origin" -> name of origin snapshot
3354 * (optional) "props" -> { prop -> value }
3357 * outnvl: propname -> error code (int32)
3360 zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3363 nvlist_t *nvprops = NULL;
3366 if (nvlist_lookup_string(innvl, "origin", &origin_name) != 0)
3367 return (SET_ERROR(EINVAL));
3368 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3370 if (strchr(fsname, '@') ||
3371 strchr(fsname, '%'))
3372 return (SET_ERROR(EINVAL));
3374 if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3375 return (SET_ERROR(EINVAL));
3376 error = dmu_objset_clone(fsname, origin_name);
3381 * It would be nice to do this atomically.
3384 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3387 (void) dsl_destroy_head(fsname);
3391 zvol_create_minors(fsname);
3398 zfs_ioc_remap(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3400 if (strchr(fsname, '@') ||
3401 strchr(fsname, '%'))
3402 return (SET_ERROR(EINVAL));
3404 return (dmu_objset_remap_indirects(fsname));
3409 * "snaps" -> { snapshot1, snapshot2 }
3410 * (optional) "props" -> { prop -> value (string) }
3413 * outnvl: snapshot -> error code (int32)
3416 zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3419 nvlist_t *props = NULL;
3423 (void) nvlist_lookup_nvlist(innvl, "props", &props);
3424 if ((error = zfs_check_userprops(poolname, props)) != 0)
3427 if (!nvlist_empty(props) &&
3428 zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3429 return (SET_ERROR(ENOTSUP));
3431 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3432 return (SET_ERROR(EINVAL));
3433 poollen = strlen(poolname);
3434 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3435 pair = nvlist_next_nvpair(snaps, pair)) {
3436 const char *name = nvpair_name(pair);
3437 const char *cp = strchr(name, '@');
3440 * The snap name must contain an @, and the part after it must
3441 * contain only valid characters.
3444 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3445 return (SET_ERROR(EINVAL));
3448 * The snap must be in the specified pool.
3450 if (strncmp(name, poolname, poollen) != 0 ||
3451 (name[poollen] != '/' && name[poollen] != '@'))
3452 return (SET_ERROR(EXDEV));
3454 /* This must be the only snap of this fs. */
3455 for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
3456 pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
3457 if (strncmp(name, nvpair_name(pair2), cp - name + 1)
3459 return (SET_ERROR(EXDEV));
3464 error = dsl_dataset_snapshot(snaps, props, outnvl);
3469 * innvl: "message" -> string
3473 zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
3481 * The poolname in the ioctl is not set, we get it from the TSD,
3482 * which was set at the end of the last successful ioctl that allows
3483 * logging. The secpolicy func already checked that it is set.
3484 * Only one log ioctl is allowed after each successful ioctl, so
3485 * we clear the TSD here.
3487 poolname = tsd_get(zfs_allow_log_key);
3488 (void) tsd_set(zfs_allow_log_key, NULL);
3489 error = spa_open(poolname, &spa, FTAG);
3494 if (nvlist_lookup_string(innvl, "message", &message) != 0) {
3495 spa_close(spa, FTAG);
3496 return (SET_ERROR(EINVAL));
3499 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
3500 spa_close(spa, FTAG);
3501 return (SET_ERROR(ENOTSUP));
3504 error = spa_history_log(spa, message);
3505 spa_close(spa, FTAG);
3511 zfs_ioc_nextboot(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
3513 char name[MAXNAMELEN];
3521 if (nvlist_lookup_uint64(innvl,
3522 ZPOOL_CONFIG_POOL_GUID, &pool_guid) != 0)
3524 if (nvlist_lookup_uint64(innvl,
3525 ZPOOL_CONFIG_GUID, &vdev_guid) != 0)
3527 if (nvlist_lookup_string(innvl,
3528 "command", &command) != 0)
3531 mutex_enter(&spa_namespace_lock);
3532 spa = spa_by_guid(pool_guid, vdev_guid);
3534 strcpy(name, spa_name(spa));
3535 mutex_exit(&spa_namespace_lock);
3539 if ((error = spa_open(name, &spa, FTAG)) != 0)
3541 spa_vdev_state_enter(spa, SCL_ALL);
3542 vd = spa_lookup_by_guid(spa, vdev_guid, B_TRUE);
3544 (void) spa_vdev_state_exit(spa, NULL, ENXIO);
3545 spa_close(spa, FTAG);
3548 error = vdev_label_write_pad2(vd, command, strlen(command));
3549 (void) spa_vdev_state_exit(spa, NULL, 0);
3550 txg_wait_synced(spa->spa_dsl_pool, 0);
3551 spa_close(spa, FTAG);
3557 * The dp_config_rwlock must not be held when calling this, because the
3558 * unmount may need to write out data.
3560 * This function is best-effort. Callers must deal gracefully if it
3561 * remains mounted (or is remounted after this call).
3563 * Returns 0 if the argument is not a snapshot, or it is not currently a
3564 * filesystem, or we were able to unmount it. Returns error code otherwise.
3567 zfs_unmount_snap(const char *snapname)
3570 zfsvfs_t *zfsvfs = NULL;
3572 if (strchr(snapname, '@') == NULL)
3575 int err = getzfsvfs(snapname, &zfsvfs);
3577 ASSERT3P(zfsvfs, ==, NULL);
3580 vfsp = zfsvfs->z_vfs;
3582 ASSERT(!dsl_pool_config_held(dmu_objset_pool(zfsvfs->z_os)));
3585 err = vn_vfswlock(vfsp->vfs_vnodecovered);
3592 * Always force the unmount for snapshots.
3595 (void) dounmount(vfsp, MS_FORCE, kcred);
3599 (void) dounmount(vfsp, MS_FORCE, curthread);
3605 zfs_unmount_snap_cb(const char *snapname, void *arg)
3607 zfs_unmount_snap(snapname);
3612 * When a clone is destroyed, its origin may also need to be destroyed,
3613 * in which case it must be unmounted. This routine will do that unmount
3617 zfs_destroy_unmount_origin(const char *fsname)
3623 error = dmu_objset_hold(fsname, FTAG, &os);
3626 ds = dmu_objset_ds(os);
3627 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
3628 char originname[ZFS_MAX_DATASET_NAME_LEN];
3629 dsl_dataset_name(ds->ds_prev, originname);
3630 dmu_objset_rele(os, FTAG);
3631 zfs_unmount_snap(originname);
3633 dmu_objset_rele(os, FTAG);
3639 * "snaps" -> { snapshot1, snapshot2 }
3640 * (optional boolean) "defer"
3643 * outnvl: snapshot -> error code (int32)
3648 zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3655 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3656 return (SET_ERROR(EINVAL));
3657 defer = nvlist_exists(innvl, "defer");
3659 poollen = strlen(poolname);
3660 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3661 pair = nvlist_next_nvpair(snaps, pair)) {
3662 const char *name = nvpair_name(pair);
3665 * The snap must be in the specified pool to prevent the
3666 * invalid removal of zvol minors below.
3668 if (strncmp(name, poolname, poollen) != 0 ||
3669 (name[poollen] != '/' && name[poollen] != '@'))
3670 return (SET_ERROR(EXDEV));
3672 zfs_unmount_snap(nvpair_name(pair));
3673 #if defined(__FreeBSD__)
3674 zvol_remove_minors(name);
3678 return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
3682 * Create bookmarks. Bookmark names are of the form <fs>#<bmark>.
3683 * All bookmarks must be in the same pool.
3686 * bookmark1 -> snapshot1, bookmark2 -> snapshot2
3689 * outnvl: bookmark -> error code (int32)
3694 zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3696 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3697 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3701 * Verify the snapshot argument.
3703 if (nvpair_value_string(pair, &snap_name) != 0)
3704 return (SET_ERROR(EINVAL));
3707 /* Verify that the keys (bookmarks) are unique */
3708 for (nvpair_t *pair2 = nvlist_next_nvpair(innvl, pair);
3709 pair2 != NULL; pair2 = nvlist_next_nvpair(innvl, pair2)) {
3710 if (strcmp(nvpair_name(pair), nvpair_name(pair2)) == 0)
3711 return (SET_ERROR(EINVAL));
3715 return (dsl_bookmark_create(innvl, outnvl));
3720 * property 1, property 2, ...
3724 * bookmark name 1 -> { property 1, property 2, ... },
3725 * bookmark name 2 -> { property 1, property 2, ... }
3730 zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3732 return (dsl_get_bookmarks(fsname, innvl, outnvl));
3737 * bookmark name 1, bookmark name 2
3740 * outnvl: bookmark -> error code (int32)
3744 zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl,
3749 poollen = strlen(poolname);
3750 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3751 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3752 const char *name = nvpair_name(pair);
3753 const char *cp = strchr(name, '#');
3756 * The bookmark name must contain an #, and the part after it
3757 * must contain only valid characters.
3760 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3761 return (SET_ERROR(EINVAL));
3764 * The bookmark must be in the specified pool.
3766 if (strncmp(name, poolname, poollen) != 0 ||
3767 (name[poollen] != '/' && name[poollen] != '#'))
3768 return (SET_ERROR(EXDEV));
3771 error = dsl_bookmark_destroy(innvl, outnvl);
3776 zfs_ioc_channel_program(const char *poolname, nvlist_t *innvl,
3780 uint64_t instrlimit, memlimit;
3781 boolean_t sync_flag;
3782 nvpair_t *nvarg = NULL;
3784 if (0 != nvlist_lookup_string(innvl, ZCP_ARG_PROGRAM, &program)) {
3787 if (0 != nvlist_lookup_boolean_value(innvl, ZCP_ARG_SYNC, &sync_flag)) {
3790 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_INSTRLIMIT, &instrlimit)) {
3791 instrlimit = ZCP_DEFAULT_INSTRLIMIT;
3793 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_MEMLIMIT, &memlimit)) {
3794 memlimit = ZCP_DEFAULT_MEMLIMIT;
3796 if (0 != nvlist_lookup_nvpair(innvl, ZCP_ARG_ARGLIST, &nvarg)) {
3800 if (instrlimit == 0 || instrlimit > zfs_lua_max_instrlimit)
3802 if (memlimit == 0 || memlimit > zfs_lua_max_memlimit)
3805 return (zcp_eval(poolname, program, sync_flag, instrlimit, memlimit,
3815 zfs_ioc_pool_checkpoint(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3817 return (spa_checkpoint(poolname));
3826 zfs_ioc_pool_discard_checkpoint(const char *poolname, nvlist_t *innvl,
3829 return (spa_checkpoint_discard(poolname));
3834 * zc_name name of dataset to destroy
3835 * zc_objset_type type of objset
3836 * zc_defer_destroy mark for deferred destroy
3841 zfs_ioc_destroy(zfs_cmd_t *zc)
3845 if (zc->zc_objset_type == DMU_OST_ZFS)
3846 zfs_unmount_snap(zc->zc_name);
3848 if (strchr(zc->zc_name, '@'))
3849 err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
3851 err = dsl_destroy_head(zc->zc_name);
3852 if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
3854 zvol_remove_minors(zc->zc_name);
3856 (void) zvol_remove_minor(zc->zc_name);
3862 * fsname is name of dataset to rollback (to most recent snapshot)
3864 * innvl may contain name of expected target snapshot
3866 * outnvl: "target" -> name of most recent snapshot
3871 zfs_ioc_rollback(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3874 char *target = NULL;
3877 (void) nvlist_lookup_string(innvl, "target", &target);
3878 if (target != NULL) {
3879 const char *cp = strchr(target, '@');
3882 * The snap name must contain an @, and the part after it must
3883 * contain only valid characters.
3886 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3887 return (SET_ERROR(EINVAL));
3890 if (getzfsvfs(fsname, &zfsvfs) == 0) {
3893 ds = dmu_objset_ds(zfsvfs->z_os);
3894 error = zfs_suspend_fs(zfsvfs);
3898 error = dsl_dataset_rollback(fsname, target, zfsvfs,
3900 resume_err = zfs_resume_fs(zfsvfs, ds);
3901 error = error ? error : resume_err;
3904 VFS_RELE(zfsvfs->z_vfs);
3906 vfs_unbusy(zfsvfs->z_vfs);
3909 error = dsl_dataset_rollback(fsname, target, NULL, outnvl);
3915 recursive_unmount(const char *fsname, void *arg)
3917 const char *snapname = arg;
3918 char fullname[ZFS_MAX_DATASET_NAME_LEN];
3920 (void) snprintf(fullname, sizeof (fullname), "%s@%s", fsname, snapname);
3921 zfs_unmount_snap(fullname);
3928 * zc_name old name of dataset
3929 * zc_value new name of dataset
3930 * zc_cookie recursive flag (only valid for snapshots)
3935 zfs_ioc_rename(zfs_cmd_t *zc)
3937 boolean_t recursive = zc->zc_cookie & 1;
3939 boolean_t allow_mounted = B_TRUE;
3942 allow_mounted = (zc->zc_cookie & 2) != 0;
3945 /* "zfs rename" from and to ...%recv datasets should both fail */
3946 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
3947 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3948 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
3949 dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3950 strchr(zc->zc_name, '%') || strchr(zc->zc_value, '%'))
3951 return (SET_ERROR(EINVAL));
3953 at = strchr(zc->zc_name, '@');
3955 /* snaps must be in same fs */
3958 if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
3959 return (SET_ERROR(EXDEV));
3961 if (zc->zc_objset_type == DMU_OST_ZFS && !allow_mounted) {
3962 error = dmu_objset_find(zc->zc_name,
3963 recursive_unmount, at + 1,
3964 recursive ? DS_FIND_CHILDREN : 0);
3970 error = dsl_dataset_rename_snapshot(zc->zc_name,
3971 at + 1, strchr(zc->zc_value, '@') + 1, recursive);
3977 if (zc->zc_objset_type == DMU_OST_ZVOL)
3978 (void) zvol_remove_minor(zc->zc_name);
3980 return (dsl_dir_rename(zc->zc_name, zc->zc_value));
3985 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
3987 const char *propname = nvpair_name(pair);
3988 boolean_t issnap = (strchr(dsname, '@') != NULL);
3989 zfs_prop_t prop = zfs_name_to_prop(propname);
3993 if (prop == ZPROP_INVAL) {
3994 if (zfs_prop_user(propname)) {
3995 if (err = zfs_secpolicy_write_perms(dsname,
3996 ZFS_DELEG_PERM_USERPROP, cr))
4001 if (!issnap && zfs_prop_userquota(propname)) {
4002 const char *perm = NULL;
4003 const char *uq_prefix =
4004 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
4005 const char *gq_prefix =
4006 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
4008 if (strncmp(propname, uq_prefix,
4009 strlen(uq_prefix)) == 0) {
4010 perm = ZFS_DELEG_PERM_USERQUOTA;
4011 } else if (strncmp(propname, gq_prefix,
4012 strlen(gq_prefix)) == 0) {
4013 perm = ZFS_DELEG_PERM_GROUPQUOTA;
4015 /* USERUSED and GROUPUSED are read-only */
4016 return (SET_ERROR(EINVAL));
4019 if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
4024 return (SET_ERROR(EINVAL));
4028 return (SET_ERROR(EINVAL));
4030 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
4032 * dsl_prop_get_all_impl() returns properties in this
4036 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
4037 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4042 * Check that this value is valid for this pool version
4045 case ZFS_PROP_COMPRESSION:
4047 * If the user specified gzip compression, make sure
4048 * the SPA supports it. We ignore any errors here since
4049 * we'll catch them later.
4051 if (nvpair_value_uint64(pair, &intval) == 0) {
4052 if (intval >= ZIO_COMPRESS_GZIP_1 &&
4053 intval <= ZIO_COMPRESS_GZIP_9 &&
4054 zfs_earlier_version(dsname,
4055 SPA_VERSION_GZIP_COMPRESSION)) {
4056 return (SET_ERROR(ENOTSUP));
4059 if (intval == ZIO_COMPRESS_ZLE &&
4060 zfs_earlier_version(dsname,
4061 SPA_VERSION_ZLE_COMPRESSION))
4062 return (SET_ERROR(ENOTSUP));
4064 if (intval == ZIO_COMPRESS_LZ4) {
4067 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4070 if (!spa_feature_is_enabled(spa,
4071 SPA_FEATURE_LZ4_COMPRESS)) {
4072 spa_close(spa, FTAG);
4073 return (SET_ERROR(ENOTSUP));
4075 spa_close(spa, FTAG);
4079 * If this is a bootable dataset then
4080 * verify that the compression algorithm
4081 * is supported for booting. We must return
4082 * something other than ENOTSUP since it
4083 * implies a downrev pool version.
4085 if (zfs_is_bootfs(dsname) &&
4086 !BOOTFS_COMPRESS_VALID(intval)) {
4087 return (SET_ERROR(ERANGE));
4092 case ZFS_PROP_COPIES:
4093 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
4094 return (SET_ERROR(ENOTSUP));
4097 case ZFS_PROP_RECORDSIZE:
4098 /* Record sizes above 128k need the feature to be enabled */
4099 if (nvpair_value_uint64(pair, &intval) == 0 &&
4100 intval > SPA_OLD_MAXBLOCKSIZE) {
4104 * We don't allow setting the property above 1MB,
4105 * unless the tunable has been changed.
4107 if (intval > zfs_max_recordsize ||
4108 intval > SPA_MAXBLOCKSIZE)
4109 return (SET_ERROR(ERANGE));
4111 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4114 if (!spa_feature_is_enabled(spa,
4115 SPA_FEATURE_LARGE_BLOCKS)) {
4116 spa_close(spa, FTAG);
4117 return (SET_ERROR(ENOTSUP));
4119 spa_close(spa, FTAG);
4123 case ZFS_PROP_SHARESMB:
4124 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
4125 return (SET_ERROR(ENOTSUP));
4128 case ZFS_PROP_ACLINHERIT:
4129 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
4130 nvpair_value_uint64(pair, &intval) == 0) {
4131 if (intval == ZFS_ACL_PASSTHROUGH_X &&
4132 zfs_earlier_version(dsname,
4133 SPA_VERSION_PASSTHROUGH_X))
4134 return (SET_ERROR(ENOTSUP));
4138 case ZFS_PROP_CHECKSUM:
4139 case ZFS_PROP_DEDUP:
4141 spa_feature_t feature;
4144 /* dedup feature version checks */
4145 if (prop == ZFS_PROP_DEDUP &&
4146 zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
4147 return (SET_ERROR(ENOTSUP));
4149 if (nvpair_value_uint64(pair, &intval) != 0)
4150 return (SET_ERROR(EINVAL));
4152 /* check prop value is enabled in features */
4153 feature = zio_checksum_to_feature(intval & ZIO_CHECKSUM_MASK);
4154 if (feature == SPA_FEATURE_NONE)
4157 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4160 * Salted checksums are not supported on root pools.
4162 if (spa_bootfs(spa) != 0 &&
4163 intval < ZIO_CHECKSUM_FUNCTIONS &&
4164 (zio_checksum_table[intval].ci_flags &
4165 ZCHECKSUM_FLAG_SALTED)) {
4166 spa_close(spa, FTAG);
4167 return (SET_ERROR(ERANGE));
4169 if (!spa_feature_is_enabled(spa, feature)) {
4170 spa_close(spa, FTAG);
4171 return (SET_ERROR(ENOTSUP));
4173 spa_close(spa, FTAG);
4178 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
4182 * Checks for a race condition to make sure we don't increment a feature flag
4186 zfs_prop_activate_feature_check(void *arg, dmu_tx_t *tx)
4188 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
4189 spa_feature_t *featurep = arg;
4191 if (!spa_feature_is_active(spa, *featurep))
4194 return (SET_ERROR(EBUSY));
4198 * The callback invoked on feature activation in the sync task caused by
4199 * zfs_prop_activate_feature.
4202 zfs_prop_activate_feature_sync(void *arg, dmu_tx_t *tx)
4204 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
4205 spa_feature_t *featurep = arg;
4207 spa_feature_incr(spa, *featurep, tx);
4211 * Activates a feature on a pool in response to a property setting. This
4212 * creates a new sync task which modifies the pool to reflect the feature
4216 zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature)
4220 /* EBUSY here indicates that the feature is already active */
4221 err = dsl_sync_task(spa_name(spa),
4222 zfs_prop_activate_feature_check, zfs_prop_activate_feature_sync,
4223 &feature, 2, ZFS_SPACE_CHECK_RESERVED);
4225 if (err != 0 && err != EBUSY)
4232 * Removes properties from the given props list that fail permission checks
4233 * needed to clear them and to restore them in case of a receive error. For each
4234 * property, make sure we have both set and inherit permissions.
4236 * Returns the first error encountered if any permission checks fail. If the
4237 * caller provides a non-NULL errlist, it also gives the complete list of names
4238 * of all the properties that failed a permission check along with the
4239 * corresponding error numbers. The caller is responsible for freeing the
4242 * If every property checks out successfully, zero is returned and the list
4243 * pointed at by errlist is NULL.
4246 zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
4249 nvpair_t *pair, *next_pair;
4256 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4258 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
4259 (void) strcpy(zc->zc_name, dataset);
4260 pair = nvlist_next_nvpair(props, NULL);
4261 while (pair != NULL) {
4262 next_pair = nvlist_next_nvpair(props, pair);
4264 (void) strcpy(zc->zc_value, nvpair_name(pair));
4265 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
4266 (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
4267 VERIFY(nvlist_remove_nvpair(props, pair) == 0);
4268 VERIFY(nvlist_add_int32(errors,
4269 zc->zc_value, err) == 0);
4273 kmem_free(zc, sizeof (zfs_cmd_t));
4275 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
4276 nvlist_free(errors);
4279 VERIFY(nvpair_value_int32(pair, &rv) == 0);
4282 if (errlist == NULL)
4283 nvlist_free(errors);
4291 propval_equals(nvpair_t *p1, nvpair_t *p2)
4293 if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
4294 /* dsl_prop_get_all_impl() format */
4296 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
4297 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4301 if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
4303 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
4304 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4308 if (nvpair_type(p1) != nvpair_type(p2))
4311 if (nvpair_type(p1) == DATA_TYPE_STRING) {
4312 char *valstr1, *valstr2;
4314 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
4315 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
4316 return (strcmp(valstr1, valstr2) == 0);
4318 uint64_t intval1, intval2;
4320 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
4321 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
4322 return (intval1 == intval2);
4327 * Remove properties from props if they are not going to change (as determined
4328 * by comparison with origprops). Remove them from origprops as well, since we
4329 * do not need to clear or restore properties that won't change.
4332 props_reduce(nvlist_t *props, nvlist_t *origprops)
4334 nvpair_t *pair, *next_pair;
4336 if (origprops == NULL)
4337 return; /* all props need to be received */
4339 pair = nvlist_next_nvpair(props, NULL);
4340 while (pair != NULL) {
4341 const char *propname = nvpair_name(pair);
4344 next_pair = nvlist_next_nvpair(props, pair);
4346 if ((nvlist_lookup_nvpair(origprops, propname,
4347 &match) != 0) || !propval_equals(pair, match))
4348 goto next; /* need to set received value */
4350 /* don't clear the existing received value */
4351 (void) nvlist_remove_nvpair(origprops, match);
4352 /* don't bother receiving the property */
4353 (void) nvlist_remove_nvpair(props, pair);
4360 * Extract properties that cannot be set PRIOR to the receipt of a dataset.
4361 * For example, refquota cannot be set until after the receipt of a dataset,
4362 * because in replication streams, an older/earlier snapshot may exceed the
4363 * refquota. We want to receive the older/earlier snapshot, but setting
4364 * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent
4365 * the older/earlier snapshot from being received (with EDQUOT).
4367 * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario.
4369 * libzfs will need to be judicious handling errors encountered by props
4370 * extracted by this function.
4373 extract_delay_props(nvlist_t *props)
4375 nvlist_t *delayprops;
4376 nvpair_t *nvp, *tmp;
4377 static const zfs_prop_t delayable[] = { ZFS_PROP_REFQUOTA, 0 };
4380 VERIFY(nvlist_alloc(&delayprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4382 for (nvp = nvlist_next_nvpair(props, NULL); nvp != NULL;
4383 nvp = nvlist_next_nvpair(props, nvp)) {
4385 * strcmp() is safe because zfs_prop_to_name() always returns
4388 for (i = 0; delayable[i] != 0; i++) {
4389 if (strcmp(zfs_prop_to_name(delayable[i]),
4390 nvpair_name(nvp)) == 0) {
4394 if (delayable[i] != 0) {
4395 tmp = nvlist_prev_nvpair(props, nvp);
4396 VERIFY(nvlist_add_nvpair(delayprops, nvp) == 0);
4397 VERIFY(nvlist_remove_nvpair(props, nvp) == 0);
4402 if (nvlist_empty(delayprops)) {
4403 nvlist_free(delayprops);
4406 return (delayprops);
4410 static boolean_t zfs_ioc_recv_inject_err;
4415 * zc_name name of containing filesystem
4416 * zc_nvlist_src{_size} nvlist of properties to apply
4417 * zc_value name of snapshot to create
4418 * zc_string name of clone origin (if DRR_FLAG_CLONE)
4419 * zc_cookie file descriptor to recv from
4420 * zc_begin_record the BEGIN record of the stream (not byteswapped)
4421 * zc_guid force flag
4422 * zc_cleanup_fd cleanup-on-exit file descriptor
4423 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
4424 * zc_resumable if data is incomplete assume sender will resume
4427 * zc_cookie number of bytes read
4428 * zc_nvlist_dst{_size} error for each unapplied received property
4429 * zc_obj zprop_errflags_t
4430 * zc_action_handle handle for this guid/ds mapping
4433 zfs_ioc_recv(zfs_cmd_t *zc)
4436 dmu_recv_cookie_t drc;
4437 boolean_t force = (boolean_t)zc->zc_guid;
4440 int props_error = 0;
4443 nvlist_t *props = NULL; /* sent properties */
4444 nvlist_t *origprops = NULL; /* existing properties */
4445 nvlist_t *delayprops = NULL; /* sent properties applied post-receive */
4446 char *origin = NULL;
4448 char tofs[ZFS_MAX_DATASET_NAME_LEN];
4449 cap_rights_t rights;
4450 boolean_t first_recvd_props = B_FALSE;
4452 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4453 strchr(zc->zc_value, '@') == NULL ||
4454 strchr(zc->zc_value, '%'))
4455 return (SET_ERROR(EINVAL));
4457 (void) strcpy(tofs, zc->zc_value);
4458 tosnap = strchr(tofs, '@');
4461 if (zc->zc_nvlist_src != 0 &&
4462 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
4463 zc->zc_iflags, &props)) != 0)
4470 fget_read(curthread, fd, cap_rights_init(&rights, CAP_PREAD), &fp);
4474 return (SET_ERROR(EBADF));
4477 errors = fnvlist_alloc();
4479 if (zc->zc_string[0])
4480 origin = zc->zc_string;
4482 error = dmu_recv_begin(tofs, tosnap,
4483 &zc->zc_begin_record, force, zc->zc_resumable, origin, &drc);
4488 * Set properties before we receive the stream so that they are applied
4489 * to the new data. Note that we must call dmu_recv_stream() if
4490 * dmu_recv_begin() succeeds.
4492 if (props != NULL && !drc.drc_newfs) {
4493 if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
4494 SPA_VERSION_RECVD_PROPS &&
4495 !dsl_prop_get_hasrecvd(tofs))
4496 first_recvd_props = B_TRUE;
4499 * If new received properties are supplied, they are to
4500 * completely replace the existing received properties, so stash
4501 * away the existing ones.
4503 if (dsl_prop_get_received(tofs, &origprops) == 0) {
4504 nvlist_t *errlist = NULL;
4506 * Don't bother writing a property if its value won't
4507 * change (and avoid the unnecessary security checks).
4509 * The first receive after SPA_VERSION_RECVD_PROPS is a
4510 * special case where we blow away all local properties
4513 if (!first_recvd_props)
4514 props_reduce(props, origprops);
4515 if (zfs_check_clearable(tofs, origprops, &errlist) != 0)
4516 (void) nvlist_merge(errors, errlist, 0);
4517 nvlist_free(errlist);
4519 if (clear_received_props(tofs, origprops,
4520 first_recvd_props ? NULL : props) != 0)
4521 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4523 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4527 if (props != NULL) {
4528 props_error = dsl_prop_set_hasrecvd(tofs);
4530 if (props_error == 0) {
4531 delayprops = extract_delay_props(props);
4532 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4538 error = dmu_recv_stream(&drc, fp, &off, zc->zc_cleanup_fd,
4539 &zc->zc_action_handle);
4542 zfsvfs_t *zfsvfs = NULL;
4544 if (getzfsvfs(tofs, &zfsvfs) == 0) {
4549 ds = dmu_objset_ds(zfsvfs->z_os);
4550 error = zfs_suspend_fs(zfsvfs);
4552 * If the suspend fails, then the recv_end will
4553 * likely also fail, and clean up after itself.
4555 end_err = dmu_recv_end(&drc, zfsvfs);
4557 error = zfs_resume_fs(zfsvfs, ds);
4558 error = error ? error : end_err;
4560 VFS_RELE(zfsvfs->z_vfs);
4562 vfs_unbusy(zfsvfs->z_vfs);
4565 error = dmu_recv_end(&drc, NULL);
4568 /* Set delayed properties now, after we're done receiving. */
4569 if (delayprops != NULL && error == 0) {
4570 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4571 delayprops, errors);
4575 if (delayprops != NULL) {
4577 * Merge delayed props back in with initial props, in case
4578 * we're DEBUG and zfs_ioc_recv_inject_err is set (which means
4579 * we have to make sure clear_received_props() includes
4580 * the delayed properties).
4582 * Since zfs_ioc_recv_inject_err is only in DEBUG kernels,
4583 * using ASSERT() will be just like a VERIFY.
4585 ASSERT(nvlist_merge(props, delayprops, 0) == 0);
4586 nvlist_free(delayprops);
4590 * Now that all props, initial and delayed, are set, report the prop
4591 * errors to the caller.
4593 if (zc->zc_nvlist_dst_size != 0 &&
4594 (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
4595 put_nvlist(zc, errors) != 0)) {
4597 * Caller made zc->zc_nvlist_dst less than the minimum expected
4598 * size or supplied an invalid address.
4600 props_error = SET_ERROR(EINVAL);
4603 zc->zc_cookie = off - fp->f_offset;
4604 if (off >= 0 && off <= MAXOFFSET_T)
4608 if (zfs_ioc_recv_inject_err) {
4609 zfs_ioc_recv_inject_err = B_FALSE;
4616 zvol_create_minors(tofs);
4620 * On error, restore the original props.
4622 if (error != 0 && props != NULL && !drc.drc_newfs) {
4623 if (clear_received_props(tofs, props, NULL) != 0) {
4625 * We failed to clear the received properties.
4626 * Since we may have left a $recvd value on the
4627 * system, we can't clear the $hasrecvd flag.
4629 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4630 } else if (first_recvd_props) {
4631 dsl_prop_unset_hasrecvd(tofs);
4634 if (origprops == NULL && !drc.drc_newfs) {
4635 /* We failed to stash the original properties. */
4636 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4640 * dsl_props_set() will not convert RECEIVED to LOCAL on or
4641 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
4642 * explictly if we're restoring local properties cleared in the
4643 * first new-style receive.
4645 if (origprops != NULL &&
4646 zfs_set_prop_nvlist(tofs, (first_recvd_props ?
4647 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
4648 origprops, NULL) != 0) {
4650 * We stashed the original properties but failed to
4653 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4658 nvlist_free(origprops);
4659 nvlist_free(errors);
4663 error = props_error;
4670 * zc_name name of snapshot to send
4671 * zc_cookie file descriptor to send stream to
4672 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
4673 * zc_sendobj objsetid of snapshot to send
4674 * zc_fromobj objsetid of incremental fromsnap (may be zero)
4675 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
4676 * output size in zc_objset_type.
4677 * zc_flags lzc_send_flags
4680 * zc_objset_type estimated size, if zc_guid is set
4683 zfs_ioc_send(zfs_cmd_t *zc)
4687 boolean_t estimate = (zc->zc_guid != 0);
4688 boolean_t embedok = (zc->zc_flags & 0x1);
4689 boolean_t large_block_ok = (zc->zc_flags & 0x2);
4690 boolean_t compressok = (zc->zc_flags & 0x4);
4692 if (zc->zc_obj != 0) {
4694 dsl_dataset_t *tosnap;
4696 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4700 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
4702 dsl_pool_rele(dp, FTAG);
4706 if (dsl_dir_is_clone(tosnap->ds_dir))
4708 dsl_dir_phys(tosnap->ds_dir)->dd_origin_obj;
4709 dsl_dataset_rele(tosnap, FTAG);
4710 dsl_pool_rele(dp, FTAG);
4715 dsl_dataset_t *tosnap;
4716 dsl_dataset_t *fromsnap = NULL;
4718 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4722 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
4724 dsl_pool_rele(dp, FTAG);
4728 if (zc->zc_fromobj != 0) {
4729 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
4732 dsl_dataset_rele(tosnap, FTAG);
4733 dsl_pool_rele(dp, FTAG);
4738 error = dmu_send_estimate(tosnap, fromsnap, compressok,
4739 &zc->zc_objset_type);
4741 if (fromsnap != NULL)
4742 dsl_dataset_rele(fromsnap, FTAG);
4743 dsl_dataset_rele(tosnap, FTAG);
4744 dsl_pool_rele(dp, FTAG);
4747 cap_rights_t rights;
4750 fp = getf(zc->zc_cookie);
4752 fget_write(curthread, zc->zc_cookie,
4753 cap_rights_init(&rights, CAP_WRITE), &fp);
4756 return (SET_ERROR(EBADF));
4759 error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
4760 zc->zc_fromobj, embedok, large_block_ok, compressok,
4762 zc->zc_cookie, fp->f_vnode, &off);
4764 zc->zc_cookie, fp, &off);
4767 if (off >= 0 && off <= MAXOFFSET_T)
4769 releasef(zc->zc_cookie);
4776 * zc_name name of snapshot on which to report progress
4777 * zc_cookie file descriptor of send stream
4780 * zc_cookie number of bytes written in send stream thus far
4783 zfs_ioc_send_progress(zfs_cmd_t *zc)
4787 dmu_sendarg_t *dsp = NULL;
4790 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4794 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
4796 dsl_pool_rele(dp, FTAG);
4800 mutex_enter(&ds->ds_sendstream_lock);
4803 * Iterate over all the send streams currently active on this dataset.
4804 * If there's one which matches the specified file descriptor _and_ the
4805 * stream was started by the current process, return the progress of
4808 for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
4809 dsp = list_next(&ds->ds_sendstreams, dsp)) {
4810 if (dsp->dsa_outfd == zc->zc_cookie &&
4811 dsp->dsa_proc == curproc)
4816 zc->zc_cookie = *(dsp->dsa_off);
4818 error = SET_ERROR(ENOENT);
4820 mutex_exit(&ds->ds_sendstream_lock);
4821 dsl_dataset_rele(ds, FTAG);
4822 dsl_pool_rele(dp, FTAG);
4827 zfs_ioc_inject_fault(zfs_cmd_t *zc)
4831 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
4832 &zc->zc_inject_record);
4835 zc->zc_guid = (uint64_t)id;
4841 zfs_ioc_clear_fault(zfs_cmd_t *zc)
4843 return (zio_clear_fault((int)zc->zc_guid));
4847 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
4849 int id = (int)zc->zc_guid;
4852 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
4853 &zc->zc_inject_record);
4861 zfs_ioc_error_log(zfs_cmd_t *zc)
4865 size_t count = (size_t)zc->zc_nvlist_dst_size;
4867 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
4870 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
4873 zc->zc_nvlist_dst_size = count;
4875 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
4877 spa_close(spa, FTAG);
4883 zfs_ioc_clear(zfs_cmd_t *zc)
4890 * On zpool clear we also fix up missing slogs
4892 mutex_enter(&spa_namespace_lock);
4893 spa = spa_lookup(zc->zc_name);
4895 mutex_exit(&spa_namespace_lock);
4896 return (SET_ERROR(EIO));
4898 if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
4899 /* we need to let spa_open/spa_load clear the chains */
4900 spa_set_log_state(spa, SPA_LOG_CLEAR);
4902 spa->spa_last_open_failed = 0;
4903 mutex_exit(&spa_namespace_lock);
4905 if (zc->zc_cookie & ZPOOL_NO_REWIND) {
4906 error = spa_open(zc->zc_name, &spa, FTAG);
4909 nvlist_t *config = NULL;
4911 if (zc->zc_nvlist_src == 0)
4912 return (SET_ERROR(EINVAL));
4914 if ((error = get_nvlist(zc->zc_nvlist_src,
4915 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
4916 error = spa_open_rewind(zc->zc_name, &spa, FTAG,
4918 if (config != NULL) {
4921 if ((err = put_nvlist(zc, config)) != 0)
4923 nvlist_free(config);
4925 nvlist_free(policy);
4932 spa_vdev_state_enter(spa, SCL_NONE);
4934 if (zc->zc_guid == 0) {
4937 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
4939 (void) spa_vdev_state_exit(spa, NULL, ENODEV);
4940 spa_close(spa, FTAG);
4941 return (SET_ERROR(ENODEV));
4945 vdev_clear(spa, vd);
4947 (void) spa_vdev_state_exit(spa, NULL, 0);
4950 * Resume any suspended I/Os.
4952 if (zio_resume(spa) != 0)
4953 error = SET_ERROR(EIO);
4955 spa_close(spa, FTAG);
4961 zfs_ioc_pool_reopen(zfs_cmd_t *zc)
4966 error = spa_open(zc->zc_name, &spa, FTAG);
4970 spa_vdev_state_enter(spa, SCL_NONE);
4973 * If a resilver is already in progress then set the
4974 * spa_scrub_reopen flag to B_TRUE so that we don't restart
4975 * the scan as a side effect of the reopen. Otherwise, let
4976 * vdev_open() decided if a resilver is required.
4978 spa->spa_scrub_reopen = dsl_scan_resilvering(spa->spa_dsl_pool);
4979 vdev_reopen(spa->spa_root_vdev);
4980 spa->spa_scrub_reopen = B_FALSE;
4982 (void) spa_vdev_state_exit(spa, NULL, 0);
4983 spa_close(spa, FTAG);
4988 * zc_name name of filesystem
4991 * zc_string name of conflicting snapshot, if there is one
4994 zfs_ioc_promote(zfs_cmd_t *zc)
4997 dsl_dataset_t *ds, *ods;
4998 char origin[ZFS_MAX_DATASET_NAME_LEN];
5002 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
5003 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
5004 strchr(zc->zc_name, '%'))
5005 return (SET_ERROR(EINVAL));
5007 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5011 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
5013 dsl_pool_rele(dp, FTAG);
5017 if (!dsl_dir_is_clone(ds->ds_dir)) {
5018 dsl_dataset_rele(ds, FTAG);
5019 dsl_pool_rele(dp, FTAG);
5020 return (SET_ERROR(EINVAL));
5023 error = dsl_dataset_hold_obj(dp,
5024 dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &ods);
5026 dsl_dataset_rele(ds, FTAG);
5027 dsl_pool_rele(dp, FTAG);
5031 dsl_dataset_name(ods, origin);
5032 dsl_dataset_rele(ods, FTAG);
5033 dsl_dataset_rele(ds, FTAG);
5034 dsl_pool_rele(dp, FTAG);
5037 * We don't need to unmount *all* the origin fs's snapshots, but
5040 cp = strchr(origin, '@');
5043 (void) dmu_objset_find(origin,
5044 zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
5045 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
5049 * Retrieve a single {user|group}{used|quota}@... property.
5052 * zc_name name of filesystem
5053 * zc_objset_type zfs_userquota_prop_t
5054 * zc_value domain name (eg. "S-1-234-567-89")
5055 * zc_guid RID/UID/GID
5058 * zc_cookie property value
5061 zfs_ioc_userspace_one(zfs_cmd_t *zc)
5066 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
5067 return (SET_ERROR(EINVAL));
5069 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
5073 error = zfs_userspace_one(zfsvfs,
5074 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
5075 zfsvfs_rele(zfsvfs, FTAG);
5082 * zc_name name of filesystem
5083 * zc_cookie zap cursor
5084 * zc_objset_type zfs_userquota_prop_t
5085 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
5088 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
5089 * zc_cookie zap cursor
5092 zfs_ioc_userspace_many(zfs_cmd_t *zc)
5095 int bufsize = zc->zc_nvlist_dst_size;
5098 return (SET_ERROR(ENOMEM));
5100 int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
5104 void *buf = kmem_alloc(bufsize, KM_SLEEP);
5106 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
5107 buf, &zc->zc_nvlist_dst_size);
5110 error = ddi_copyout(buf,
5111 (void *)(uintptr_t)zc->zc_nvlist_dst,
5112 zc->zc_nvlist_dst_size, zc->zc_iflags);
5114 kmem_free(buf, bufsize);
5115 zfsvfs_rele(zfsvfs, FTAG);
5122 * zc_name name of filesystem
5128 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
5134 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
5135 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
5137 * If userused is not enabled, it may be because the
5138 * objset needs to be closed & reopened (to grow the
5139 * objset_phys_t). Suspend/resume the fs will do that.
5141 dsl_dataset_t *ds, *newds;
5143 ds = dmu_objset_ds(zfsvfs->z_os);
5144 error = zfs_suspend_fs(zfsvfs);
5146 dmu_objset_refresh_ownership(ds, &newds,
5148 error = zfs_resume_fs(zfsvfs, newds);
5152 error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
5154 VFS_RELE(zfsvfs->z_vfs);
5156 vfs_unbusy(zfsvfs->z_vfs);
5159 /* XXX kind of reading contents without owning */
5160 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
5164 error = dmu_objset_userspace_upgrade(os);
5165 dmu_objset_rele(os, FTAG);
5173 * We don't want to have a hard dependency
5174 * against some special symbols in sharefs
5175 * nfs, and smbsrv. Determine them if needed when
5176 * the first file system is shared.
5177 * Neither sharefs, nfs or smbsrv are unloadable modules.
5179 int (*znfsexport_fs)(void *arg);
5180 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
5181 int (*zsmbexport_fs)(void *arg, boolean_t add_share);
5183 int zfs_nfsshare_inited;
5184 int zfs_smbshare_inited;
5186 ddi_modhandle_t nfs_mod;
5187 ddi_modhandle_t sharefs_mod;
5188 ddi_modhandle_t smbsrv_mod;
5189 #endif /* illumos */
5190 kmutex_t zfs_share_lock;
5198 ASSERT(MUTEX_HELD(&zfs_share_lock));
5199 /* Both NFS and SMB shares also require sharetab support. */
5200 if (sharefs_mod == NULL && ((sharefs_mod =
5201 ddi_modopen("fs/sharefs",
5202 KRTLD_MODE_FIRST, &error)) == NULL)) {
5203 return (SET_ERROR(ENOSYS));
5205 if (zshare_fs == NULL && ((zshare_fs =
5206 (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
5207 ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
5208 return (SET_ERROR(ENOSYS));
5212 #endif /* illumos */
5215 zfs_ioc_share(zfs_cmd_t *zc)
5221 switch (zc->zc_share.z_sharetype) {
5223 case ZFS_UNSHARE_NFS:
5224 if (zfs_nfsshare_inited == 0) {
5225 mutex_enter(&zfs_share_lock);
5226 if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
5227 KRTLD_MODE_FIRST, &error)) == NULL)) {
5228 mutex_exit(&zfs_share_lock);
5229 return (SET_ERROR(ENOSYS));
5231 if (znfsexport_fs == NULL &&
5232 ((znfsexport_fs = (int (*)(void *))
5234 "nfs_export", &error)) == NULL)) {
5235 mutex_exit(&zfs_share_lock);
5236 return (SET_ERROR(ENOSYS));
5238 error = zfs_init_sharefs();
5240 mutex_exit(&zfs_share_lock);
5241 return (SET_ERROR(ENOSYS));
5243 zfs_nfsshare_inited = 1;
5244 mutex_exit(&zfs_share_lock);
5248 case ZFS_UNSHARE_SMB:
5249 if (zfs_smbshare_inited == 0) {
5250 mutex_enter(&zfs_share_lock);
5251 if (smbsrv_mod == NULL && ((smbsrv_mod =
5252 ddi_modopen("drv/smbsrv",
5253 KRTLD_MODE_FIRST, &error)) == NULL)) {
5254 mutex_exit(&zfs_share_lock);
5255 return (SET_ERROR(ENOSYS));
5257 if (zsmbexport_fs == NULL && ((zsmbexport_fs =
5258 (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
5259 "smb_server_share", &error)) == NULL)) {
5260 mutex_exit(&zfs_share_lock);
5261 return (SET_ERROR(ENOSYS));
5263 error = zfs_init_sharefs();
5265 mutex_exit(&zfs_share_lock);
5266 return (SET_ERROR(ENOSYS));
5268 zfs_smbshare_inited = 1;
5269 mutex_exit(&zfs_share_lock);
5273 return (SET_ERROR(EINVAL));
5276 switch (zc->zc_share.z_sharetype) {
5278 case ZFS_UNSHARE_NFS:
5280 znfsexport_fs((void *)
5281 (uintptr_t)zc->zc_share.z_exportdata))
5285 case ZFS_UNSHARE_SMB:
5286 if (error = zsmbexport_fs((void *)
5287 (uintptr_t)zc->zc_share.z_exportdata,
5288 zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
5295 opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
5296 zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
5297 SHAREFS_ADD : SHAREFS_REMOVE;
5300 * Add or remove share from sharetab
5302 error = zshare_fs(opcode,
5303 (void *)(uintptr_t)zc->zc_share.z_sharedata,
5304 zc->zc_share.z_sharemax);
5308 #else /* !illumos */
5310 #endif /* illumos */
5313 ace_t full_access[] = {
5314 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
5319 * zc_name name of containing filesystem
5320 * zc_obj object # beyond which we want next in-use object #
5323 * zc_obj next in-use object #
5326 zfs_ioc_next_obj(zfs_cmd_t *zc)
5328 objset_t *os = NULL;
5331 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
5335 error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
5336 dsl_dataset_phys(os->os_dsl_dataset)->ds_prev_snap_txg);
5338 dmu_objset_rele(os, FTAG);
5344 * zc_name name of filesystem
5345 * zc_value prefix name for snapshot
5346 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
5349 * zc_value short name of new snapshot
5352 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
5359 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
5363 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
5364 (u_longlong_t)ddi_get_lbolt64());
5365 hold_name = kmem_asprintf("%%%s", zc->zc_value);
5367 error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
5370 (void) strcpy(zc->zc_value, snap_name);
5373 zfs_onexit_fd_rele(zc->zc_cleanup_fd);
5379 * zc_name name of "to" snapshot
5380 * zc_value name of "from" snapshot
5381 * zc_cookie file descriptor to write diff data on
5384 * dmu_diff_record_t's to the file descriptor
5387 zfs_ioc_diff(zfs_cmd_t *zc)
5390 cap_rights_t rights;
5395 fp = getf(zc->zc_cookie);
5397 fget_write(curthread, zc->zc_cookie,
5398 cap_rights_init(&rights, CAP_WRITE), &fp);
5401 return (SET_ERROR(EBADF));
5406 error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off);
5408 error = dmu_diff(zc->zc_name, zc->zc_value, fp, &off);
5411 if (off >= 0 && off <= MAXOFFSET_T)
5413 releasef(zc->zc_cookie);
5420 * Remove all ACL files in shares dir
5423 zfs_smb_acl_purge(znode_t *dzp)
5426 zap_attribute_t zap;
5427 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
5430 for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
5431 (error = zap_cursor_retrieve(&zc, &zap)) == 0;
5432 zap_cursor_advance(&zc)) {
5433 if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
5437 zap_cursor_fini(&zc);
5440 #endif /* illumos */
5443 zfs_ioc_smb_acl(zfs_cmd_t *zc)
5448 vnode_t *resourcevp = NULL;
5457 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
5458 NO_FOLLOW, NULL, &vp)) != 0)
5461 /* Now make sure mntpnt and dataset are ZFS */
5463 if (strcmp(vp->v_vfsp->mnt_stat.f_fstypename, "zfs") != 0 ||
5464 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
5465 zc->zc_name) != 0)) {
5467 return (SET_ERROR(EINVAL));
5471 zfsvfs = dzp->z_zfsvfs;
5475 * Create share dir if its missing.
5477 mutex_enter(&zfsvfs->z_lock);
5478 if (zfsvfs->z_shares_dir == 0) {
5481 tx = dmu_tx_create(zfsvfs->z_os);
5482 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
5484 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
5485 error = dmu_tx_assign(tx, TXG_WAIT);
5489 error = zfs_create_share_dir(zfsvfs, tx);
5493 mutex_exit(&zfsvfs->z_lock);
5499 mutex_exit(&zfsvfs->z_lock);
5501 ASSERT(zfsvfs->z_shares_dir);
5502 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
5508 switch (zc->zc_cookie) {
5509 case ZFS_SMB_ACL_ADD:
5510 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
5511 vattr.va_type = VREG;
5512 vattr.va_mode = S_IFREG|0777;
5516 vsec.vsa_mask = VSA_ACE;
5517 vsec.vsa_aclentp = &full_access;
5518 vsec.vsa_aclentsz = sizeof (full_access);
5519 vsec.vsa_aclcnt = 1;
5521 error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
5522 &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
5524 VN_RELE(resourcevp);
5527 case ZFS_SMB_ACL_REMOVE:
5528 error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
5532 case ZFS_SMB_ACL_RENAME:
5533 if ((error = get_nvlist(zc->zc_nvlist_src,
5534 zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
5536 VN_RELE(ZTOV(sharedir));
5540 if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
5541 nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
5544 VN_RELE(ZTOV(sharedir));
5546 nvlist_free(nvlist);
5549 error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
5551 nvlist_free(nvlist);
5554 case ZFS_SMB_ACL_PURGE:
5555 error = zfs_smb_acl_purge(sharedir);
5559 error = SET_ERROR(EINVAL);
5564 VN_RELE(ZTOV(sharedir));
5569 #else /* !illumos */
5570 return (EOPNOTSUPP);
5571 #endif /* illumos */
5576 * "holds" -> { snapname -> holdname (string), ... }
5577 * (optional) "cleanup_fd" -> fd (int32)
5581 * snapname -> error value (int32)
5587 zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
5591 int cleanup_fd = -1;
5595 error = nvlist_lookup_nvlist(args, "holds", &holds);
5597 return (SET_ERROR(EINVAL));
5599 /* make sure the user didn't pass us any invalid (empty) tags */
5600 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
5601 pair = nvlist_next_nvpair(holds, pair)) {
5604 error = nvpair_value_string(pair, &htag);
5606 return (SET_ERROR(error));
5608 if (strlen(htag) == 0)
5609 return (SET_ERROR(EINVAL));
5612 if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
5613 error = zfs_onexit_fd_hold(cleanup_fd, &minor);
5618 error = dsl_dataset_user_hold(holds, minor, errlist);
5620 zfs_onexit_fd_rele(cleanup_fd);
5625 * innvl is not used.
5628 * holdname -> time added (uint64 seconds since epoch)
5634 zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
5636 return (dsl_dataset_get_holds(snapname, outnvl));
5641 * snapname -> { holdname, ... }
5646 * snapname -> error value (int32)
5652 zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
5654 return (dsl_dataset_user_release(holds, errlist));
5659 * zc_name name of new filesystem or snapshot
5660 * zc_value full name of old snapshot
5663 * zc_cookie space in bytes
5664 * zc_objset_type compressed space in bytes
5665 * zc_perm_action uncompressed space in bytes
5668 zfs_ioc_space_written(zfs_cmd_t *zc)
5672 dsl_dataset_t *new, *old;
5674 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5677 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
5679 dsl_pool_rele(dp, FTAG);
5682 error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
5684 dsl_dataset_rele(new, FTAG);
5685 dsl_pool_rele(dp, FTAG);
5689 error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
5690 &zc->zc_objset_type, &zc->zc_perm_action);
5691 dsl_dataset_rele(old, FTAG);
5692 dsl_dataset_rele(new, FTAG);
5693 dsl_pool_rele(dp, FTAG);
5699 * "firstsnap" -> snapshot name
5703 * "used" -> space in bytes
5704 * "compressed" -> compressed space in bytes
5705 * "uncompressed" -> uncompressed space in bytes
5709 zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
5713 dsl_dataset_t *new, *old;
5715 uint64_t used, comp, uncomp;
5717 if (nvlist_lookup_string(innvl, "firstsnap", &firstsnap) != 0)
5718 return (SET_ERROR(EINVAL));
5720 error = dsl_pool_hold(lastsnap, FTAG, &dp);
5724 error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
5725 if (error == 0 && !new->ds_is_snapshot) {
5726 dsl_dataset_rele(new, FTAG);
5727 error = SET_ERROR(EINVAL);
5730 dsl_pool_rele(dp, FTAG);
5733 error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
5734 if (error == 0 && !old->ds_is_snapshot) {
5735 dsl_dataset_rele(old, FTAG);
5736 error = SET_ERROR(EINVAL);
5739 dsl_dataset_rele(new, FTAG);
5740 dsl_pool_rele(dp, FTAG);
5744 error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
5745 dsl_dataset_rele(old, FTAG);
5746 dsl_dataset_rele(new, FTAG);
5747 dsl_pool_rele(dp, FTAG);
5748 fnvlist_add_uint64(outnvl, "used", used);
5749 fnvlist_add_uint64(outnvl, "compressed", comp);
5750 fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
5755 zfs_ioc_jail(zfs_cmd_t *zc)
5758 return (zone_dataset_attach(curthread->td_ucred, zc->zc_name,
5759 (int)zc->zc_jailid));
5763 zfs_ioc_unjail(zfs_cmd_t *zc)
5766 return (zone_dataset_detach(curthread->td_ucred, zc->zc_name,
5767 (int)zc->zc_jailid));
5772 * "fd" -> file descriptor to write stream to (int32)
5773 * (optional) "fromsnap" -> full snap name to send an incremental from
5774 * (optional) "largeblockok" -> (value ignored)
5775 * indicates that blocks > 128KB are permitted
5776 * (optional) "embedok" -> (value ignored)
5777 * presence indicates DRR_WRITE_EMBEDDED records are permitted
5778 * (optional) "compressok" -> (value ignored)
5779 * presence indicates compressed DRR_WRITE records are permitted
5780 * (optional) "resume_object" and "resume_offset" -> (uint64)
5781 * if present, resume send stream from specified object and offset.
5788 zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5790 cap_rights_t rights;
5794 char *fromname = NULL;
5796 boolean_t largeblockok;
5798 boolean_t compressok;
5799 uint64_t resumeobj = 0;
5800 uint64_t resumeoff = 0;
5802 error = nvlist_lookup_int32(innvl, "fd", &fd);
5804 return (SET_ERROR(EINVAL));
5806 (void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
5808 largeblockok = nvlist_exists(innvl, "largeblockok");
5809 embedok = nvlist_exists(innvl, "embedok");
5810 compressok = nvlist_exists(innvl, "compressok");
5812 (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
5813 (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
5816 file_t *fp = getf(fd);
5818 fget_write(curthread, fd, cap_rights_init(&rights, CAP_WRITE), &fp);
5821 return (SET_ERROR(EBADF));
5824 error = dmu_send(snapname, fromname, embedok, largeblockok, compressok,
5826 fd, resumeobj, resumeoff, fp->f_vnode, &off);
5828 fd, resumeobj, resumeoff, fp, &off);
5832 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5843 * Determine approximately how large a zfs send stream will be -- the number
5844 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
5847 * (optional) "from" -> full snap or bookmark name to send an incremental
5849 * (optional) "largeblockok" -> (value ignored)
5850 * indicates that blocks > 128KB are permitted
5851 * (optional) "embedok" -> (value ignored)
5852 * presence indicates DRR_WRITE_EMBEDDED records are permitted
5853 * (optional) "compressok" -> (value ignored)
5854 * presence indicates compressed DRR_WRITE records are permitted
5858 * "space" -> bytes of space (uint64)
5862 zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5865 dsl_dataset_t *tosnap;
5868 boolean_t compressok;
5871 error = dsl_pool_hold(snapname, FTAG, &dp);
5875 error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
5877 dsl_pool_rele(dp, FTAG);
5881 compressok = nvlist_exists(innvl, "compressok");
5883 error = nvlist_lookup_string(innvl, "from", &fromname);
5885 if (strchr(fromname, '@') != NULL) {
5887 * If from is a snapshot, hold it and use the more
5888 * efficient dmu_send_estimate to estimate send space
5889 * size using deadlists.
5891 dsl_dataset_t *fromsnap;
5892 error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
5895 error = dmu_send_estimate(tosnap, fromsnap, compressok,
5897 dsl_dataset_rele(fromsnap, FTAG);
5898 } else if (strchr(fromname, '#') != NULL) {
5900 * If from is a bookmark, fetch the creation TXG of the
5901 * snapshot it was created from and use that to find
5902 * blocks that were born after it.
5904 zfs_bookmark_phys_t frombm;
5906 error = dsl_bookmark_lookup(dp, fromname, tosnap,
5910 error = dmu_send_estimate_from_txg(tosnap,
5911 frombm.zbm_creation_txg, compressok, &space);
5914 * from is not properly formatted as a snapshot or
5917 error = SET_ERROR(EINVAL);
5922 * If estimating the size of a full send, use dmu_send_estimate.
5924 error = dmu_send_estimate(tosnap, NULL, compressok, &space);
5927 fnvlist_add_uint64(outnvl, "space", space);
5930 dsl_dataset_rele(tosnap, FTAG);
5931 dsl_pool_rele(dp, FTAG);
5935 static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
5938 zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5939 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
5940 boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
5942 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
5944 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
5945 ASSERT3U(ioc, <, ZFS_IOC_LAST);
5946 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
5947 ASSERT3P(vec->zvec_func, ==, NULL);
5949 vec->zvec_legacy_func = func;
5950 vec->zvec_secpolicy = secpolicy;
5951 vec->zvec_namecheck = namecheck;
5952 vec->zvec_allow_log = log_history;
5953 vec->zvec_pool_check = pool_check;
5957 * See the block comment at the beginning of this file for details on
5958 * each argument to this function.
5961 zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
5962 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
5963 zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
5964 boolean_t allow_log)
5966 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
5968 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
5969 ASSERT3U(ioc, <, ZFS_IOC_LAST);
5970 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
5971 ASSERT3P(vec->zvec_func, ==, NULL);
5973 /* if we are logging, the name must be valid */
5974 ASSERT(!allow_log || namecheck != NO_NAME);
5976 vec->zvec_name = name;
5977 vec->zvec_func = func;
5978 vec->zvec_secpolicy = secpolicy;
5979 vec->zvec_namecheck = namecheck;
5980 vec->zvec_pool_check = pool_check;
5981 vec->zvec_smush_outnvlist = smush_outnvlist;
5982 vec->zvec_allow_log = allow_log;
5986 zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5987 zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
5988 zfs_ioc_poolcheck_t pool_check)
5990 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5991 POOL_NAME, log_history, pool_check);
5995 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5996 zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
5998 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5999 DATASET_NAME, B_FALSE, pool_check);
6003 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
6005 zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
6006 POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6010 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6011 zfs_secpolicy_func_t *secpolicy)
6013 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6014 NO_NAME, B_FALSE, POOL_CHECK_NONE);
6018 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
6019 zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
6021 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6022 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
6026 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
6028 zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
6029 zfs_secpolicy_read);
6033 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6034 zfs_secpolicy_func_t *secpolicy)
6036 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6037 DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6041 zfs_ioctl_init(void)
6043 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
6044 zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
6045 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6047 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
6048 zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
6049 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE);
6051 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
6052 zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
6053 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
6055 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
6056 zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
6057 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
6059 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
6060 zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
6061 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
6063 zfs_ioctl_register("create", ZFS_IOC_CREATE,
6064 zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
6065 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6067 zfs_ioctl_register("clone", ZFS_IOC_CLONE,
6068 zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
6069 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6071 zfs_ioctl_register("remap", ZFS_IOC_REMAP,
6072 zfs_ioc_remap, zfs_secpolicy_remap, DATASET_NAME,
6073 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
6075 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
6076 zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
6077 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6079 zfs_ioctl_register("hold", ZFS_IOC_HOLD,
6080 zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
6081 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6082 zfs_ioctl_register("release", ZFS_IOC_RELEASE,
6083 zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
6084 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6086 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
6087 zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
6088 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
6090 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
6091 zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
6092 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
6094 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK,
6095 zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME,
6096 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6098 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS,
6099 zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME,
6100 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
6102 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS,
6103 zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks,
6105 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6107 zfs_ioctl_register("channel_program", ZFS_IOC_CHANNEL_PROGRAM,
6108 zfs_ioc_channel_program, zfs_secpolicy_config,
6109 POOL_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE,
6112 zfs_ioctl_register("zpool_checkpoint", ZFS_IOC_POOL_CHECKPOINT,
6113 zfs_ioc_pool_checkpoint, zfs_secpolicy_config, POOL_NAME,
6114 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6116 zfs_ioctl_register("zpool_discard_checkpoint",
6117 ZFS_IOC_POOL_DISCARD_CHECKPOINT, zfs_ioc_pool_discard_checkpoint,
6118 zfs_secpolicy_config, POOL_NAME,
6119 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6121 /* IOCTLS that use the legacy function signature */
6123 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
6124 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
6126 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
6127 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
6128 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
6130 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
6131 zfs_ioc_pool_upgrade);
6132 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
6134 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
6135 zfs_ioc_vdev_remove);
6136 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
6137 zfs_ioc_vdev_set_state);
6138 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
6139 zfs_ioc_vdev_attach);
6140 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
6141 zfs_ioc_vdev_detach);
6142 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
6143 zfs_ioc_vdev_setpath);
6144 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
6145 zfs_ioc_vdev_setfru);
6146 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
6147 zfs_ioc_pool_set_props);
6148 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
6149 zfs_ioc_vdev_split);
6150 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
6151 zfs_ioc_pool_reguid);
6153 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
6154 zfs_ioc_pool_configs, zfs_secpolicy_none);
6155 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
6156 zfs_ioc_pool_tryimport, zfs_secpolicy_config);
6157 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
6158 zfs_ioc_inject_fault, zfs_secpolicy_inject);
6159 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
6160 zfs_ioc_clear_fault, zfs_secpolicy_inject);
6161 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
6162 zfs_ioc_inject_list_next, zfs_secpolicy_inject);
6165 * pool destroy, and export don't log the history as part of
6166 * zfsdev_ioctl, but rather zfs_ioc_pool_export
6167 * does the logging of those commands.
6169 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
6170 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
6171 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
6172 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
6174 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
6175 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
6176 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
6177 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
6179 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
6180 zfs_secpolicy_inject, B_FALSE, POOL_CHECK_NONE);
6181 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
6182 zfs_ioc_dsobj_to_dsname,
6183 zfs_secpolicy_diff, B_FALSE, POOL_CHECK_NONE);
6184 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
6185 zfs_ioc_pool_get_history,
6186 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
6188 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
6189 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
6191 zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
6192 zfs_secpolicy_config, B_TRUE, POOL_CHECK_READONLY);
6193 zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
6194 zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
6196 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
6197 zfs_ioc_space_written);
6198 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
6199 zfs_ioc_objset_recvd_props);
6200 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
6202 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
6204 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
6205 zfs_ioc_objset_stats);
6206 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
6207 zfs_ioc_objset_zplprops);
6208 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
6209 zfs_ioc_dataset_list_next);
6210 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
6211 zfs_ioc_snapshot_list_next);
6212 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
6213 zfs_ioc_send_progress);
6215 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
6216 zfs_ioc_diff, zfs_secpolicy_diff);
6217 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
6218 zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
6219 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
6220 zfs_ioc_obj_to_path, zfs_secpolicy_diff);
6221 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
6222 zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
6223 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
6224 zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
6225 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
6226 zfs_ioc_send, zfs_secpolicy_send);
6228 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
6229 zfs_secpolicy_none);
6230 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
6231 zfs_secpolicy_destroy);
6232 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
6233 zfs_secpolicy_rename);
6234 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
6235 zfs_secpolicy_recv);
6236 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
6237 zfs_secpolicy_promote);
6238 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
6239 zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
6240 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
6241 zfs_secpolicy_set_fsacl);
6243 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
6244 zfs_secpolicy_share, POOL_CHECK_NONE);
6245 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
6246 zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
6247 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
6248 zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
6249 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6250 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
6251 zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
6252 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6255 zfs_ioctl_register_dataset_nolog(ZFS_IOC_JAIL, zfs_ioc_jail,
6256 zfs_secpolicy_config, POOL_CHECK_NONE);
6257 zfs_ioctl_register_dataset_nolog(ZFS_IOC_UNJAIL, zfs_ioc_unjail,
6258 zfs_secpolicy_config, POOL_CHECK_NONE);
6259 zfs_ioctl_register("fbsd_nextboot", ZFS_IOC_NEXTBOOT,
6260 zfs_ioc_nextboot, zfs_secpolicy_config, NO_NAME,
6261 POOL_CHECK_NONE, B_FALSE, B_FALSE);
6266 pool_status_check(const char *name, zfs_ioc_namecheck_t type,
6267 zfs_ioc_poolcheck_t check)
6272 ASSERT(type == POOL_NAME || type == DATASET_NAME);
6274 if (check & POOL_CHECK_NONE)
6277 error = spa_open(name, &spa, FTAG);
6279 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
6280 error = SET_ERROR(EAGAIN);
6281 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
6282 error = SET_ERROR(EROFS);
6283 spa_close(spa, FTAG);
6289 * Find a free minor number.
6292 zfsdev_minor_alloc(void)
6294 static minor_t last_minor;
6297 ASSERT(MUTEX_HELD(&spa_namespace_lock));
6299 for (m = last_minor + 1; m != last_minor; m++) {
6300 if (m > ZFSDEV_MAX_MINOR)
6302 if (ddi_get_soft_state(zfsdev_state, m) == NULL) {
6312 zfs_ctldev_init(struct cdev *devp)
6315 zfs_soft_state_t *zs;
6317 ASSERT(MUTEX_HELD(&spa_namespace_lock));
6319 minor = zfsdev_minor_alloc();
6321 return (SET_ERROR(ENXIO));
6323 if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS)
6324 return (SET_ERROR(EAGAIN));
6326 devfs_set_cdevpriv((void *)(uintptr_t)minor, zfsdev_close);
6328 zs = ddi_get_soft_state(zfsdev_state, minor);
6329 zs->zss_type = ZSST_CTLDEV;
6330 zfs_onexit_init((zfs_onexit_t **)&zs->zss_data);
6336 zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor)
6338 ASSERT(MUTEX_HELD(&spa_namespace_lock));
6340 zfs_onexit_destroy(zo);
6341 ddi_soft_state_free(zfsdev_state, minor);
6345 zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which)
6347 zfs_soft_state_t *zp;
6349 zp = ddi_get_soft_state(zfsdev_state, minor);
6350 if (zp == NULL || zp->zss_type != which)
6353 return (zp->zss_data);
6357 zfsdev_open(struct cdev *devp, int flag, int mode, struct thread *td)
6362 if (getminor(*devp) != 0)
6363 return (zvol_open(devp, flag, otyp, cr));
6366 /* This is the control device. Allocate a new minor if requested. */
6368 mutex_enter(&spa_namespace_lock);
6369 error = zfs_ctldev_init(devp);
6370 mutex_exit(&spa_namespace_lock);
6377 zfsdev_close(void *data)
6380 minor_t minor = (minor_t)(uintptr_t)data;
6385 mutex_enter(&spa_namespace_lock);
6386 zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV);
6388 mutex_exit(&spa_namespace_lock);
6391 zfs_ctldev_destroy(zo, minor);
6392 mutex_exit(&spa_namespace_lock);
6396 zfsdev_ioctl(struct cdev *dev, u_long zcmd, caddr_t arg, int flag,
6403 minor_t minor = getminor(dev);
6405 zfs_iocparm_t *zc_iocparm;
6406 int cflag, cmd, oldvecnum;
6407 boolean_t newioc, compat;
6408 void *compat_zc = NULL;
6409 cred_t *cr = td->td_ucred;
6411 const zfs_ioc_vec_t *vec;
6412 char *saved_poolname = NULL;
6413 nvlist_t *innvl = NULL;
6415 cflag = ZFS_CMD_COMPAT_NONE;
6417 newioc = B_TRUE; /* "new" style (zfs_iocparm_t) ioctl */
6419 len = IOCPARM_LEN(zcmd);
6420 vecnum = cmd = zcmd & 0xff;
6423 * Check if we are talking to supported older binaries
6424 * and translate zfs_cmd if necessary
6426 if (len != sizeof(zfs_iocparm_t)) {
6433 case sizeof(zfs_cmd_zcmd_t):
6434 cflag = ZFS_CMD_COMPAT_LZC;
6436 case sizeof(zfs_cmd_deadman_t):
6437 cflag = ZFS_CMD_COMPAT_DEADMAN;
6439 case sizeof(zfs_cmd_v28_t):
6440 cflag = ZFS_CMD_COMPAT_V28;
6442 case sizeof(zfs_cmd_v15_t):
6443 cflag = ZFS_CMD_COMPAT_V15;
6444 vecnum = zfs_ioctl_v15_to_v28[cmd];
6447 * Return without further handling
6448 * if the command is blacklisted.
6450 if (vecnum == ZFS_IOC_COMPAT_PASS)
6452 else if (vecnum == ZFS_IOC_COMPAT_FAIL)
6461 vecnum = cmd - ZFS_IOC_FIRST;
6462 ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
6465 if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
6466 return (SET_ERROR(EINVAL));
6467 vec = &zfs_ioc_vec[vecnum];
6469 zc = kmem_zalloc(sizeof(zfs_cmd_t), KM_SLEEP);
6472 error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
6474 error = SET_ERROR(EFAULT);
6477 #else /* !illumos */
6478 bzero(zc, sizeof(zfs_cmd_t));
6481 zc_iocparm = (void *)arg;
6483 switch (zc_iocparm->zfs_ioctl_version) {
6484 case ZFS_IOCVER_CURRENT:
6485 if (zc_iocparm->zfs_cmd_size != sizeof(zfs_cmd_t)) {
6486 error = SET_ERROR(EINVAL);
6490 case ZFS_IOCVER_INLANES:
6491 if (zc_iocparm->zfs_cmd_size != sizeof(zfs_cmd_inlanes_t)) {
6492 error = SET_ERROR(EFAULT);
6496 cflag = ZFS_CMD_COMPAT_INLANES;
6498 case ZFS_IOCVER_RESUME:
6499 if (zc_iocparm->zfs_cmd_size != sizeof(zfs_cmd_resume_t)) {
6500 error = SET_ERROR(EFAULT);
6504 cflag = ZFS_CMD_COMPAT_RESUME;
6506 case ZFS_IOCVER_EDBP:
6507 if (zc_iocparm->zfs_cmd_size != sizeof(zfs_cmd_edbp_t)) {
6508 error = SET_ERROR(EFAULT);
6512 cflag = ZFS_CMD_COMPAT_EDBP;
6514 case ZFS_IOCVER_ZCMD:
6515 if (zc_iocparm->zfs_cmd_size > sizeof(zfs_cmd_t) ||
6516 zc_iocparm->zfs_cmd_size < sizeof(zfs_cmd_zcmd_t)) {
6517 error = SET_ERROR(EFAULT);
6521 cflag = ZFS_CMD_COMPAT_ZCMD;
6524 error = SET_ERROR(EINVAL);
6530 ASSERT(sizeof(zfs_cmd_t) >= zc_iocparm->zfs_cmd_size);
6531 compat_zc = kmem_zalloc(sizeof(zfs_cmd_t), KM_SLEEP);
6532 bzero(compat_zc, sizeof(zfs_cmd_t));
6534 error = ddi_copyin((void *)(uintptr_t)zc_iocparm->zfs_cmd,
6535 compat_zc, zc_iocparm->zfs_cmd_size, flag);
6537 error = SET_ERROR(EFAULT);
6541 error = ddi_copyin((void *)(uintptr_t)zc_iocparm->zfs_cmd,
6542 zc, zc_iocparm->zfs_cmd_size, flag);
6544 error = SET_ERROR(EFAULT);
6552 ASSERT(compat_zc != NULL);
6553 zfs_cmd_compat_get(zc, compat_zc, cflag);
6555 ASSERT(compat_zc == NULL);
6556 zfs_cmd_compat_get(zc, arg, cflag);
6559 error = zfs_ioctl_compat_pre(zc, &vecnum, cflag);
6562 if (oldvecnum != vecnum)
6563 vec = &zfs_ioc_vec[vecnum];
6565 #endif /* !illumos */
6567 zc->zc_iflags = flag & FKIOCTL;
6568 if (zc->zc_nvlist_src_size != 0) {
6569 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
6570 zc->zc_iflags, &innvl);
6575 /* rewrite innvl for backwards compatibility */
6577 innvl = zfs_ioctl_compat_innvl(zc, innvl, vecnum, cflag);
6580 * Ensure that all pool/dataset names are valid before we pass down to
6583 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
6584 switch (vec->zvec_namecheck) {
6586 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
6587 error = SET_ERROR(EINVAL);
6589 error = pool_status_check(zc->zc_name,
6590 vec->zvec_namecheck, vec->zvec_pool_check);
6594 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
6595 error = SET_ERROR(EINVAL);
6597 error = pool_status_check(zc->zc_name,
6598 vec->zvec_namecheck, vec->zvec_pool_check);
6606 error = vec->zvec_secpolicy(zc, innvl, cr);
6611 /* legacy ioctls can modify zc_name */
6612 len = strcspn(zc->zc_name, "/@#") + 1;
6613 saved_poolname = kmem_alloc(len, KM_SLEEP);
6614 (void) strlcpy(saved_poolname, zc->zc_name, len);
6616 if (vec->zvec_func != NULL) {
6620 nvlist_t *lognv = NULL;
6622 ASSERT(vec->zvec_legacy_func == NULL);
6625 * Add the innvl to the lognv before calling the func,
6626 * in case the func changes the innvl.
6628 if (vec->zvec_allow_log) {
6629 lognv = fnvlist_alloc();
6630 fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
6632 if (!nvlist_empty(innvl)) {
6633 fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
6638 outnvl = fnvlist_alloc();
6639 error = vec->zvec_func(zc->zc_name, innvl, outnvl);
6642 * Some commands can partially execute, modfiy state, and still
6643 * return an error. In these cases, attempt to record what
6647 (cmd == ZFS_IOC_CHANNEL_PROGRAM && error != EINVAL)) &&
6648 vec->zvec_allow_log &&
6649 spa_open(zc->zc_name, &spa, FTAG) == 0) {
6650 if (!nvlist_empty(outnvl)) {
6651 fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL,
6655 fnvlist_add_int64(lognv, ZPOOL_HIST_ERRNO,
6658 (void) spa_history_log_nvl(spa, lognv);
6659 spa_close(spa, FTAG);
6661 fnvlist_free(lognv);
6663 /* rewrite outnvl for backwards compatibility */
6665 outnvl = zfs_ioctl_compat_outnvl(zc, outnvl, vecnum,
6668 if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
6670 if (vec->zvec_smush_outnvlist) {
6671 smusherror = nvlist_smush(outnvl,
6672 zc->zc_nvlist_dst_size);
6674 if (smusherror == 0)
6675 puterror = put_nvlist(zc, outnvl);
6681 nvlist_free(outnvl);
6683 error = vec->zvec_legacy_func(zc);
6690 rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
6691 if (error == 0 && rc != 0)
6692 error = SET_ERROR(EFAULT);
6695 zfs_ioctl_compat_post(zc, cmd, cflag);
6697 ASSERT(compat_zc != NULL);
6698 ASSERT(sizeof(zfs_cmd_t) >= zc_iocparm->zfs_cmd_size);
6700 zfs_cmd_compat_put(zc, compat_zc, vecnum, cflag);
6701 rc = ddi_copyout(compat_zc,
6702 (void *)(uintptr_t)zc_iocparm->zfs_cmd,
6703 zc_iocparm->zfs_cmd_size, flag);
6704 if (error == 0 && rc != 0)
6705 error = SET_ERROR(EFAULT);
6706 kmem_free(compat_zc, sizeof (zfs_cmd_t));
6708 zfs_cmd_compat_put(zc, arg, vecnum, cflag);
6713 rc = ddi_copyout(zc, (void *)(uintptr_t)zc_iocparm->zfs_cmd,
6714 sizeof (zfs_cmd_t), flag);
6715 if (error == 0 && rc != 0)
6716 error = SET_ERROR(EFAULT);
6719 if (error == 0 && vec->zvec_allow_log) {
6720 char *s = tsd_get(zfs_allow_log_key);
6723 (void) tsd_set(zfs_allow_log_key, saved_poolname);
6725 if (saved_poolname != NULL)
6726 strfree(saved_poolname);
6729 kmem_free(zc, sizeof (zfs_cmd_t));
6735 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
6737 if (cmd != DDI_ATTACH)
6738 return (DDI_FAILURE);
6740 if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
6741 DDI_PSEUDO, 0) == DDI_FAILURE)
6742 return (DDI_FAILURE);
6746 ddi_report_dev(dip);
6748 return (DDI_SUCCESS);
6752 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
6754 if (spa_busy() || zfs_busy() || zvol_busy())
6755 return (DDI_FAILURE);
6757 if (cmd != DDI_DETACH)
6758 return (DDI_FAILURE);
6762 ddi_prop_remove_all(dip);
6763 ddi_remove_minor_node(dip, NULL);
6765 return (DDI_SUCCESS);
6770 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
6773 case DDI_INFO_DEVT2DEVINFO:
6775 return (DDI_SUCCESS);
6777 case DDI_INFO_DEVT2INSTANCE:
6778 *result = (void *)0;
6779 return (DDI_SUCCESS);
6782 return (DDI_FAILURE);
6784 #endif /* illumos */
6787 * OK, so this is a little weird.
6789 * /dev/zfs is the control node, i.e. minor 0.
6790 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
6792 * /dev/zfs has basically nothing to do except serve up ioctls,
6793 * so most of the standard driver entry points are in zvol.c.
6796 static struct cb_ops zfs_cb_ops = {
6797 zfsdev_open, /* open */
6798 zfsdev_close, /* close */
6799 zvol_strategy, /* strategy */
6801 zvol_dump, /* dump */
6802 zvol_read, /* read */
6803 zvol_write, /* write */
6804 zfsdev_ioctl, /* ioctl */
6808 nochpoll, /* poll */
6809 ddi_prop_op, /* prop_op */
6810 NULL, /* streamtab */
6811 D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */
6812 CB_REV, /* version */
6813 nodev, /* async read */
6814 nodev, /* async write */
6817 static struct dev_ops zfs_dev_ops = {
6818 DEVO_REV, /* version */
6820 zfs_info, /* info */
6821 nulldev, /* identify */
6822 nulldev, /* probe */
6823 zfs_attach, /* attach */
6824 zfs_detach, /* detach */
6826 &zfs_cb_ops, /* driver operations */
6827 NULL, /* no bus operations */
6829 ddi_quiesce_not_needed, /* quiesce */
6832 static struct modldrv zfs_modldrv = {
6838 static struct modlinkage modlinkage = {
6840 (void *)&zfs_modlfs,
6841 (void *)&zfs_modldrv,
6844 #endif /* illumos */
6846 static struct cdevsw zfs_cdevsw = {
6847 .d_version = D_VERSION,
6848 .d_open = zfsdev_open,
6849 .d_ioctl = zfsdev_ioctl,
6850 .d_name = ZFS_DEV_NAME
6854 zfs_allow_log_destroy(void *arg)
6856 char *poolname = arg;
6863 zfsdev = make_dev(&zfs_cdevsw, 0x0, UID_ROOT, GID_OPERATOR, 0666,
6871 destroy_dev(zfsdev);
6874 static struct root_hold_token *zfs_root_token;
6875 struct proc *zfsproc;
6883 spa_init(FREAD | FWRITE);
6888 if ((error = mod_install(&modlinkage)) != 0) {
6895 tsd_create(&zfs_fsyncer_key, NULL);
6896 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
6897 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
6899 error = ldi_ident_from_mod(&modlinkage, &zfs_li);
6901 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
6911 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
6912 return (SET_ERROR(EBUSY));
6914 if ((error = mod_remove(&modlinkage)) != 0)
6920 if (zfs_nfsshare_inited)
6921 (void) ddi_modclose(nfs_mod);
6922 if (zfs_smbshare_inited)
6923 (void) ddi_modclose(smbsrv_mod);
6924 if (zfs_nfsshare_inited || zfs_smbshare_inited)
6925 (void) ddi_modclose(sharefs_mod);
6927 tsd_destroy(&zfs_fsyncer_key);
6928 ldi_ident_release(zfs_li);
6930 mutex_destroy(&zfs_share_lock);
6936 _info(struct modinfo *modinfop)
6938 return (mod_info(&modlinkage, modinfop));
6940 #endif /* illumos */
6942 static int zfs__init(void);
6943 static int zfs__fini(void);
6944 static void zfs_shutdown(void *, int);
6946 static eventhandler_tag zfs_shutdown_event_tag;
6949 #define ZFS_MIN_KSTACK_PAGES 4
6957 #if KSTACK_PAGES < ZFS_MIN_KSTACK_PAGES
6958 printf("ZFS NOTICE: KSTACK_PAGES is %d which could result in stack "
6959 "overflow panic!\nPlease consider adding "
6960 "'options KSTACK_PAGES=%d' to your kernel config\n", KSTACK_PAGES,
6961 ZFS_MIN_KSTACK_PAGES);
6964 zfs_root_token = root_mount_hold("ZFS");
6966 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
6968 spa_init(FREAD | FWRITE);
6973 tsd_create(&zfs_fsyncer_key, NULL);
6974 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
6975 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
6976 tsd_create(&zfs_geom_probe_vdev_key, NULL);
6978 printf("ZFS storage pool version: features support (" SPA_VERSION_STRING ")\n");
6979 root_mount_rel(zfs_root_token);
6989 if (spa_busy() || zfs_busy() || zvol_busy() ||
6990 zio_injection_enabled) {
6999 tsd_destroy(&zfs_fsyncer_key);
7000 tsd_destroy(&rrw_tsd_key);
7001 tsd_destroy(&zfs_allow_log_key);
7003 mutex_destroy(&zfs_share_lock);
7009 zfs_shutdown(void *arg __unused, int howto __unused)
7013 * ZFS fini routines can not properly work in a panic-ed system.
7015 if (panicstr == NULL)
7021 zfs_modevent(module_t mod, int type, void *unused __unused)
7029 zfs_shutdown_event_tag = EVENTHANDLER_REGISTER(
7030 shutdown_post_sync, zfs_shutdown, NULL,
7031 SHUTDOWN_PRI_FIRST);
7035 if (err == 0 && zfs_shutdown_event_tag != NULL)
7036 EVENTHANDLER_DEREGISTER(shutdown_post_sync,
7037 zfs_shutdown_event_tag);
7044 return (EOPNOTSUPP);
7047 static moduledata_t zfs_mod = {
7052 DECLARE_MODULE(zfsctrl, zfs_mod, SI_SUB_VFS, SI_ORDER_ANY);
7053 MODULE_VERSION(zfsctrl, 1);
7054 MODULE_DEPEND(zfsctrl, opensolaris, 1, 1, 1);
7055 MODULE_DEPEND(zfsctrl, krpc, 1, 1, 1);
7056 MODULE_DEPEND(zfsctrl, acl_nfs4, 1, 1, 1);