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, 2015 by Delphix. All rights reserved.
31 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
32 * Copyright (c) 2013 Steven Hartland. All rights reserved.
33 * Copyright (c) 2014 Integros [integros.com]
34 * Copyright 2016 Toomas Soome <tsoome@me.com>
35 * Copyright 2017 RackTop Systems.
41 * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
42 * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
44 * There are two ways that we handle ioctls: the legacy way where almost
45 * all of the logic is in the ioctl callback, and the new way where most
46 * of the marshalling is handled in the common entry point, zfsdev_ioctl().
48 * Non-legacy ioctls should be registered by calling
49 * zfs_ioctl_register() from zfs_ioctl_init(). The ioctl is invoked
50 * from userland by lzc_ioctl().
52 * The registration arguments are as follows:
55 * The name of the ioctl. This is used for history logging. If the
56 * ioctl returns successfully (the callback returns 0), and allow_log
57 * is true, then a history log entry will be recorded with the input &
58 * output nvlists. The log entry can be printed with "zpool history -i".
61 * The ioctl request number, which userland will pass to ioctl(2).
62 * The ioctl numbers can change from release to release, because
63 * the caller (libzfs) must be matched to the kernel.
65 * zfs_secpolicy_func_t *secpolicy
66 * This function will be called before the zfs_ioc_func_t, to
67 * determine if this operation is permitted. It should return EPERM
68 * on failure, and 0 on success. Checks include determining if the
69 * dataset is visible in this zone, and if the user has either all
70 * zfs privileges in the zone (SYS_MOUNT), or has been granted permission
71 * to do this operation on this dataset with "zfs allow".
73 * zfs_ioc_namecheck_t namecheck
74 * This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
75 * name, a dataset name, or nothing. If the name is not well-formed,
76 * the ioctl will fail and the callback will not be called.
77 * Therefore, the callback can assume that the name is well-formed
78 * (e.g. is null-terminated, doesn't have more than one '@' character,
79 * doesn't have invalid characters).
81 * zfs_ioc_poolcheck_t pool_check
82 * This specifies requirements on the pool state. If the pool does
83 * not meet them (is suspended or is readonly), the ioctl will fail
84 * and the callback will not be called. If any checks are specified
85 * (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
86 * Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
87 * POOL_CHECK_READONLY).
89 * boolean_t smush_outnvlist
90 * If smush_outnvlist is true, then the output is presumed to be a
91 * list of errors, and it will be "smushed" down to fit into the
92 * caller's buffer, by removing some entries and replacing them with a
93 * single "N_MORE_ERRORS" entry indicating how many were removed. See
94 * nvlist_smush() for details. If smush_outnvlist is false, and the
95 * outnvlist does not fit into the userland-provided buffer, then the
96 * ioctl will fail with ENOMEM.
98 * zfs_ioc_func_t *func
99 * The callback function that will perform the operation.
101 * The callback should return 0 on success, or an error number on
102 * failure. If the function fails, the userland ioctl will return -1,
103 * and errno will be set to the callback's return value. The callback
104 * will be called with the following arguments:
107 * The name of the pool or dataset to operate on, from
108 * zfs_cmd_t:zc_name. The 'namecheck' argument specifies the
109 * expected type (pool, dataset, or none).
112 * The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src. Or
113 * NULL if no input nvlist was provided. Changes to this nvlist are
114 * ignored. If the input nvlist could not be deserialized, the
115 * ioctl will fail and the callback will not be called.
118 * The output nvlist, initially empty. The callback can fill it in,
119 * and it will be returned to userland by serializing it into
120 * zfs_cmd_t:zc_nvlist_dst. If it is non-empty, and serialization
121 * fails (e.g. because the caller didn't supply a large enough
122 * buffer), then the overall ioctl will fail. See the
123 * 'smush_nvlist' argument above for additional behaviors.
125 * There are two typical uses of the output nvlist:
126 * - To return state, e.g. property values. In this case,
127 * smush_outnvlist should be false. If the buffer was not large
128 * enough, the caller will reallocate a larger buffer and try
131 * - To return multiple errors from an ioctl which makes on-disk
132 * changes. In this case, smush_outnvlist should be true.
133 * Ioctls which make on-disk modifications should generally not
134 * use the outnvl if they succeed, because the caller can not
135 * distinguish between the operation failing, and
136 * deserialization failing.
139 #include "opt_kstack_pages.h"
142 #include <sys/types.h>
143 #include <sys/param.h>
144 #include <sys/systm.h>
145 #include <sys/conf.h>
146 #include <sys/kernel.h>
147 #include <sys/lock.h>
148 #include <sys/malloc.h>
149 #include <sys/mutex.h>
150 #include <sys/proc.h>
151 #include <sys/errno.h>
154 #include <sys/file.h>
155 #include <sys/kmem.h>
156 #include <sys/conf.h>
157 #include <sys/cmn_err.h>
158 #include <sys/stat.h>
159 #include <sys/zfs_ioctl.h>
160 #include <sys/zfs_vfsops.h>
161 #include <sys/zfs_znode.h>
164 #include <sys/spa_impl.h>
165 #include <sys/vdev.h>
167 #include <sys/dsl_dir.h>
168 #include <sys/dsl_dataset.h>
169 #include <sys/dsl_prop.h>
170 #include <sys/dsl_deleg.h>
171 #include <sys/dmu_objset.h>
172 #include <sys/dmu_impl.h>
173 #include <sys/dmu_tx.h>
174 #include <sys/sunddi.h>
175 #include <sys/policy.h>
176 #include <sys/zone.h>
177 #include <sys/nvpair.h>
178 #include <sys/mount.h>
179 #include <sys/taskqueue.h>
181 #include <sys/varargs.h>
182 #include <sys/fs/zfs.h>
183 #include <sys/zfs_ctldir.h>
184 #include <sys/zfs_dir.h>
185 #include <sys/zfs_onexit.h>
186 #include <sys/zvol.h>
187 #include <sys/dsl_scan.h>
188 #include <sys/dmu_objset.h>
189 #include <sys/dmu_send.h>
190 #include <sys/dsl_destroy.h>
191 #include <sys/dsl_bookmark.h>
192 #include <sys/dsl_userhold.h>
193 #include <sys/zfeature.h>
194 #include <sys/zio_checksum.h>
196 #include "zfs_namecheck.h"
197 #include "zfs_prop.h"
198 #include "zfs_deleg.h"
199 #include "zfs_comutil.h"
200 #include "zfs_ioctl_compat.h"
202 CTASSERT(sizeof(zfs_cmd_t) < IOCPARM_MAX);
204 static struct cdev *zfsdev;
206 extern void zfs_init(void);
207 extern void zfs_fini(void);
209 uint_t zfs_fsyncer_key;
210 extern uint_t rrw_tsd_key;
211 static uint_t zfs_allow_log_key;
212 extern uint_t zfs_geom_probe_vdev_key;
214 typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *);
215 typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *);
216 typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *);
222 } zfs_ioc_namecheck_t;
225 POOL_CHECK_NONE = 1 << 0,
226 POOL_CHECK_SUSPENDED = 1 << 1,
227 POOL_CHECK_READONLY = 1 << 2,
228 } zfs_ioc_poolcheck_t;
230 typedef struct zfs_ioc_vec {
231 zfs_ioc_legacy_func_t *zvec_legacy_func;
232 zfs_ioc_func_t *zvec_func;
233 zfs_secpolicy_func_t *zvec_secpolicy;
234 zfs_ioc_namecheck_t zvec_namecheck;
235 boolean_t zvec_allow_log;
236 zfs_ioc_poolcheck_t zvec_pool_check;
237 boolean_t zvec_smush_outnvlist;
238 const char *zvec_name;
241 /* This array is indexed by zfs_userquota_prop_t */
242 static const char *userquota_perms[] = {
243 ZFS_DELEG_PERM_USERUSED,
244 ZFS_DELEG_PERM_USERQUOTA,
245 ZFS_DELEG_PERM_GROUPUSED,
246 ZFS_DELEG_PERM_GROUPQUOTA,
249 static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
250 static int zfs_check_settable(const char *name, nvpair_t *property,
252 static int zfs_check_clearable(char *dataset, nvlist_t *props,
254 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
256 int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
257 static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
259 static void zfsdev_close(void *data);
261 static int zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature);
263 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
265 __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
272 * Get rid of annoying "../common/" prefix to filename.
274 newfile = strrchr(file, '/');
275 if (newfile != NULL) {
276 newfile = newfile + 1; /* Get rid of leading / */
282 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
286 * To get this data, use the zfs-dprintf probe as so:
287 * dtrace -q -n 'zfs-dprintf \
288 * /stringof(arg0) == "dbuf.c"/ \
289 * {printf("%s: %s", stringof(arg1), stringof(arg3))}'
291 * arg1 = function name
295 DTRACE_PROBE4(zfs__dprintf,
296 char *, newfile, char *, func, int, line, char *, buf);
300 history_str_free(char *buf)
302 kmem_free(buf, HIS_MAX_RECORD_LEN);
306 history_str_get(zfs_cmd_t *zc)
310 if (zc->zc_history == 0)
313 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
314 if (copyinstr((void *)(uintptr_t)zc->zc_history,
315 buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
316 history_str_free(buf);
320 buf[HIS_MAX_RECORD_LEN -1] = '\0';
326 * Check to see if the named dataset is currently defined as bootable
329 zfs_is_bootfs(const char *name)
333 if (dmu_objset_hold(name, FTAG, &os) == 0) {
335 ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
336 dmu_objset_rele(os, FTAG);
343 * Return non-zero if the spa version is less than requested version.
346 zfs_earlier_version(const char *name, int version)
350 if (spa_open(name, &spa, FTAG) == 0) {
351 if (spa_version(spa) < version) {
352 spa_close(spa, FTAG);
355 spa_close(spa, FTAG);
361 * Return TRUE if the ZPL version is less than requested version.
364 zpl_earlier_version(const char *name, int version)
367 boolean_t rc = B_TRUE;
369 if (dmu_objset_hold(name, FTAG, &os) == 0) {
372 if (dmu_objset_type(os) != DMU_OST_ZFS) {
373 dmu_objset_rele(os, FTAG);
376 /* XXX reading from non-owned objset */
377 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
378 rc = zplversion < version;
379 dmu_objset_rele(os, FTAG);
385 zfs_log_history(zfs_cmd_t *zc)
390 if ((buf = history_str_get(zc)) == NULL)
393 if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
394 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
395 (void) spa_history_log(spa, buf);
396 spa_close(spa, FTAG);
398 history_str_free(buf);
402 * Policy for top-level read operations (list pools). Requires no privileges,
403 * and can be used in the local zone, as there is no associated dataset.
407 zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
413 * Policy for dataset read operations (list children, get statistics). Requires
414 * no privileges, but must be visible in the local zone.
418 zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
420 if (INGLOBALZONE(curthread) ||
421 zone_dataset_visible(zc->zc_name, NULL))
424 return (SET_ERROR(ENOENT));
428 zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
433 * The dataset must be visible by this zone -- check this first
434 * so they don't see EPERM on something they shouldn't know about.
436 if (!INGLOBALZONE(curthread) &&
437 !zone_dataset_visible(dataset, &writable))
438 return (SET_ERROR(ENOENT));
440 if (INGLOBALZONE(curthread)) {
442 * If the fs is zoned, only root can access it from the
445 if (secpolicy_zfs(cr) && zoned)
446 return (SET_ERROR(EPERM));
449 * If we are in a local zone, the 'zoned' property must be set.
452 return (SET_ERROR(EPERM));
454 /* must be writable by this zone */
456 return (SET_ERROR(EPERM));
462 zfs_dozonecheck(const char *dataset, cred_t *cr)
466 if (dsl_prop_get_integer(dataset, "jailed", &zoned, NULL))
467 return (SET_ERROR(ENOENT));
469 return (zfs_dozonecheck_impl(dataset, zoned, cr));
473 zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
477 if (dsl_prop_get_int_ds(ds, "jailed", &zoned))
478 return (SET_ERROR(ENOENT));
480 return (zfs_dozonecheck_impl(dataset, zoned, cr));
484 zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
485 const char *perm, cred_t *cr)
489 error = zfs_dozonecheck_ds(name, ds, cr);
491 error = secpolicy_zfs(cr);
493 error = dsl_deleg_access_impl(ds, perm, cr);
499 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
506 * First do a quick check for root in the global zone, which
507 * is allowed to do all write_perms. This ensures that zfs_ioc_*
508 * will get to handle nonexistent datasets.
510 if (INGLOBALZONE(curthread) && secpolicy_zfs(cr) == 0)
513 error = dsl_pool_hold(name, FTAG, &dp);
517 error = dsl_dataset_hold(dp, name, FTAG, &ds);
519 dsl_pool_rele(dp, FTAG);
523 error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
525 dsl_dataset_rele(ds, FTAG);
526 dsl_pool_rele(dp, FTAG);
532 * Policy for setting the security label property.
534 * Returns 0 for success, non-zero for access and other errors.
537 zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
539 char ds_hexsl[MAXNAMELEN];
540 bslabel_t ds_sl, new_sl;
541 boolean_t new_default = FALSE;
543 int needed_priv = -1;
546 /* First get the existing dataset label. */
547 error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
548 1, sizeof (ds_hexsl), &ds_hexsl, NULL);
550 return (SET_ERROR(EPERM));
552 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
555 /* The label must be translatable */
556 if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
557 return (SET_ERROR(EINVAL));
560 * In a non-global zone, disallow attempts to set a label that
561 * doesn't match that of the zone; otherwise no other checks
564 if (!INGLOBALZONE(curproc)) {
565 if (new_default || !blequal(&new_sl, CR_SL(CRED())))
566 return (SET_ERROR(EPERM));
571 * For global-zone datasets (i.e., those whose zoned property is
572 * "off", verify that the specified new label is valid for the
575 if (dsl_prop_get_integer(name,
576 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
577 return (SET_ERROR(EPERM));
579 if (zfs_check_global_label(name, strval) != 0)
580 return (SET_ERROR(EPERM));
584 * If the existing dataset label is nondefault, check if the
585 * dataset is mounted (label cannot be changed while mounted).
586 * Get the zfsvfs; if there isn't one, then the dataset isn't
587 * mounted (or isn't a dataset, doesn't exist, ...).
589 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
591 static char *setsl_tag = "setsl_tag";
594 * Try to own the dataset; abort if there is any error,
595 * (e.g., already mounted, in use, or other error).
597 error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE,
600 return (SET_ERROR(EPERM));
602 dmu_objset_disown(os, setsl_tag);
605 needed_priv = PRIV_FILE_DOWNGRADE_SL;
609 if (hexstr_to_label(strval, &new_sl) != 0)
610 return (SET_ERROR(EPERM));
612 if (blstrictdom(&ds_sl, &new_sl))
613 needed_priv = PRIV_FILE_DOWNGRADE_SL;
614 else if (blstrictdom(&new_sl, &ds_sl))
615 needed_priv = PRIV_FILE_UPGRADE_SL;
617 /* dataset currently has a default label */
619 needed_priv = PRIV_FILE_UPGRADE_SL;
623 if (needed_priv != -1)
624 return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
627 #endif /* SECLABEL */
630 zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
636 * Check permissions for special properties.
641 * Disallow setting of 'zoned' from within a local zone.
643 if (!INGLOBALZONE(curthread))
644 return (SET_ERROR(EPERM));
648 case ZFS_PROP_FILESYSTEM_LIMIT:
649 case ZFS_PROP_SNAPSHOT_LIMIT:
650 if (!INGLOBALZONE(curthread)) {
652 char setpoint[ZFS_MAX_DATASET_NAME_LEN];
654 * Unprivileged users are allowed to modify the
655 * limit on things *under* (ie. contained by)
656 * the thing they own.
658 if (dsl_prop_get_integer(dsname, "jailed", &zoned,
660 return (SET_ERROR(EPERM));
661 if (!zoned || strlen(dsname) <= strlen(setpoint))
662 return (SET_ERROR(EPERM));
666 case ZFS_PROP_MLSLABEL:
668 if (!is_system_labeled())
669 return (SET_ERROR(EPERM));
671 if (nvpair_value_string(propval, &strval) == 0) {
674 err = zfs_set_slabel_policy(dsname, strval, CRED());
684 return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
689 zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
693 error = zfs_dozonecheck(zc->zc_name, cr);
698 * permission to set permissions will be evaluated later in
699 * dsl_deleg_can_allow()
706 zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
708 return (zfs_secpolicy_write_perms(zc->zc_name,
709 ZFS_DELEG_PERM_ROLLBACK, cr));
714 zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
722 * Generate the current snapshot name from the given objsetid, then
723 * use that name for the secpolicy/zone checks.
725 cp = strchr(zc->zc_name, '@');
727 return (SET_ERROR(EINVAL));
728 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
732 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
734 dsl_pool_rele(dp, FTAG);
738 dsl_dataset_name(ds, zc->zc_name);
740 error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
741 ZFS_DELEG_PERM_SEND, cr);
742 dsl_dataset_rele(ds, FTAG);
743 dsl_pool_rele(dp, FTAG);
750 zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
752 return (zfs_secpolicy_write_perms(zc->zc_name,
753 ZFS_DELEG_PERM_SEND, cr));
758 zfs_secpolicy_deleg_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
763 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
764 NO_FOLLOW, NULL, &vp)) != 0)
767 /* Now make sure mntpnt and dataset are ZFS */
769 if (strcmp(vp->v_vfsp->mnt_stat.f_fstypename, "zfs") != 0 ||
770 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
771 zc->zc_name) != 0)) {
773 return (SET_ERROR(EPERM));
777 return (dsl_deleg_access(zc->zc_name,
778 ZFS_DELEG_PERM_SHARE, cr));
782 zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
784 if (!INGLOBALZONE(curthread))
785 return (SET_ERROR(EPERM));
787 if (secpolicy_nfs(cr) == 0) {
790 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
795 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
797 if (!INGLOBALZONE(curthread))
798 return (SET_ERROR(EPERM));
800 if (secpolicy_smb(cr) == 0) {
803 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
808 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
813 * Remove the @bla or /bla from the end of the name to get the parent.
815 (void) strncpy(parent, datasetname, parentsize);
816 cp = strrchr(parent, '@');
820 cp = strrchr(parent, '/');
822 return (SET_ERROR(ENOENT));
830 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
834 if ((error = zfs_secpolicy_write_perms(name,
835 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
838 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
843 zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
845 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
849 * Destroying snapshots with delegated permissions requires
850 * descendant mount and destroy permissions.
854 zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
857 nvpair_t *pair, *nextpair;
860 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
861 return (SET_ERROR(EINVAL));
862 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
864 nextpair = nvlist_next_nvpair(snaps, pair);
865 error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr);
866 if (error == ENOENT) {
868 * Ignore any snapshots that don't exist (we consider
869 * them "already destroyed"). Remove the name from the
870 * nvl here in case the snapshot is created between
871 * now and when we try to destroy it (in which case
872 * we don't want to destroy it since we haven't
873 * checked for permission).
875 fnvlist_remove_nvpair(snaps, pair);
886 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
888 char parentname[ZFS_MAX_DATASET_NAME_LEN];
891 if ((error = zfs_secpolicy_write_perms(from,
892 ZFS_DELEG_PERM_RENAME, cr)) != 0)
895 if ((error = zfs_secpolicy_write_perms(from,
896 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
899 if ((error = zfs_get_parent(to, parentname,
900 sizeof (parentname))) != 0)
903 if ((error = zfs_secpolicy_write_perms(parentname,
904 ZFS_DELEG_PERM_CREATE, cr)) != 0)
907 if ((error = zfs_secpolicy_write_perms(parentname,
908 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
916 zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
921 if ((zc->zc_cookie & 1) != 0) {
923 * This is recursive rename, so the starting snapshot might
924 * not exist. Check file system or volume permission instead.
926 at = strchr(zc->zc_name, '@');
932 error = zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr);
942 zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
945 dsl_dataset_t *clone;
948 error = zfs_secpolicy_write_perms(zc->zc_name,
949 ZFS_DELEG_PERM_PROMOTE, cr);
953 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
957 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
960 char parentname[ZFS_MAX_DATASET_NAME_LEN];
961 dsl_dataset_t *origin = NULL;
965 error = dsl_dataset_hold_obj(dd->dd_pool,
966 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin);
968 dsl_dataset_rele(clone, FTAG);
969 dsl_pool_rele(dp, FTAG);
973 error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
974 ZFS_DELEG_PERM_MOUNT, cr);
976 dsl_dataset_name(origin, parentname);
978 error = zfs_secpolicy_write_perms_ds(parentname, origin,
979 ZFS_DELEG_PERM_PROMOTE, cr);
981 dsl_dataset_rele(clone, FTAG);
982 dsl_dataset_rele(origin, FTAG);
984 dsl_pool_rele(dp, FTAG);
990 zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
994 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
995 ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
998 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
999 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
1002 return (zfs_secpolicy_write_perms(zc->zc_name,
1003 ZFS_DELEG_PERM_CREATE, cr));
1007 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
1009 return (zfs_secpolicy_write_perms(name,
1010 ZFS_DELEG_PERM_SNAPSHOT, cr));
1014 * Check for permission to create each snapshot in the nvlist.
1018 zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1024 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
1025 return (SET_ERROR(EINVAL));
1026 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
1027 pair = nvlist_next_nvpair(snaps, pair)) {
1028 char *name = nvpair_name(pair);
1029 char *atp = strchr(name, '@');
1032 error = SET_ERROR(EINVAL);
1036 error = zfs_secpolicy_snapshot_perms(name, cr);
1045 * Check for permission to create each snapshot in the nvlist.
1049 zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1053 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
1054 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
1055 char *name = nvpair_name(pair);
1056 char *hashp = strchr(name, '#');
1058 if (hashp == NULL) {
1059 error = SET_ERROR(EINVAL);
1063 error = zfs_secpolicy_write_perms(name,
1064 ZFS_DELEG_PERM_BOOKMARK, cr);
1074 zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1076 nvpair_t *pair, *nextpair;
1079 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1081 char *name = nvpair_name(pair);
1082 char *hashp = strchr(name, '#');
1083 nextpair = nvlist_next_nvpair(innvl, pair);
1085 if (hashp == NULL) {
1086 error = SET_ERROR(EINVAL);
1091 error = zfs_secpolicy_write_perms(name,
1092 ZFS_DELEG_PERM_DESTROY, cr);
1094 if (error == ENOENT) {
1096 * Ignore any filesystems that don't exist (we consider
1097 * their bookmarks "already destroyed"). Remove
1098 * the name from the nvl here in case the filesystem
1099 * is created between now and when we try to destroy
1100 * the bookmark (in which case we don't want to
1101 * destroy it since we haven't checked for permission).
1103 fnvlist_remove_nvpair(innvl, pair);
1115 zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1118 * Even root must have a proper TSD so that we know what pool
1121 if (tsd_get(zfs_allow_log_key) == NULL)
1122 return (SET_ERROR(EPERM));
1127 zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1129 char parentname[ZFS_MAX_DATASET_NAME_LEN];
1133 if ((error = zfs_get_parent(zc->zc_name, parentname,
1134 sizeof (parentname))) != 0)
1137 if (nvlist_lookup_string(innvl, "origin", &origin) == 0 &&
1138 (error = zfs_secpolicy_write_perms(origin,
1139 ZFS_DELEG_PERM_CLONE, cr)) != 0)
1142 if ((error = zfs_secpolicy_write_perms(parentname,
1143 ZFS_DELEG_PERM_CREATE, cr)) != 0)
1146 return (zfs_secpolicy_write_perms(parentname,
1147 ZFS_DELEG_PERM_MOUNT, cr));
1151 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
1152 * SYS_CONFIG privilege, which is not available in a local zone.
1156 zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1158 if (secpolicy_sys_config(cr, B_FALSE) != 0)
1159 return (SET_ERROR(EPERM));
1165 * Policy for object to name lookups.
1169 zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1173 if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
1176 error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
1181 * Policy for fault injection. Requires all privileges.
1185 zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1187 return (secpolicy_zinject(cr));
1192 zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1194 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
1196 if (prop == ZPROP_INVAL) {
1197 if (!zfs_prop_user(zc->zc_value))
1198 return (SET_ERROR(EINVAL));
1199 return (zfs_secpolicy_write_perms(zc->zc_name,
1200 ZFS_DELEG_PERM_USERPROP, cr));
1202 return (zfs_secpolicy_setprop(zc->zc_name, prop,
1208 zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1210 int err = zfs_secpolicy_read(zc, innvl, cr);
1214 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1215 return (SET_ERROR(EINVAL));
1217 if (zc->zc_value[0] == 0) {
1219 * They are asking about a posix uid/gid. If it's
1220 * themself, allow it.
1222 if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
1223 zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
1224 if (zc->zc_guid == crgetuid(cr))
1227 if (groupmember(zc->zc_guid, cr))
1232 return (zfs_secpolicy_write_perms(zc->zc_name,
1233 userquota_perms[zc->zc_objset_type], cr));
1237 zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1239 int err = zfs_secpolicy_read(zc, innvl, cr);
1243 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1244 return (SET_ERROR(EINVAL));
1246 return (zfs_secpolicy_write_perms(zc->zc_name,
1247 userquota_perms[zc->zc_objset_type], cr));
1252 zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1254 return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
1260 zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1266 error = nvlist_lookup_nvlist(innvl, "holds", &holds);
1268 return (SET_ERROR(EINVAL));
1270 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
1271 pair = nvlist_next_nvpair(holds, pair)) {
1272 char fsname[ZFS_MAX_DATASET_NAME_LEN];
1273 error = dmu_fsname(nvpair_name(pair), fsname);
1276 error = zfs_secpolicy_write_perms(fsname,
1277 ZFS_DELEG_PERM_HOLD, cr);
1286 zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1291 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1292 pair = nvlist_next_nvpair(innvl, pair)) {
1293 char fsname[ZFS_MAX_DATASET_NAME_LEN];
1294 error = dmu_fsname(nvpair_name(pair), fsname);
1297 error = zfs_secpolicy_write_perms(fsname,
1298 ZFS_DELEG_PERM_RELEASE, cr);
1306 * Policy for allowing temporary snapshots to be taken or released
1309 zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1312 * A temporary snapshot is the same as a snapshot,
1313 * hold, destroy and release all rolled into one.
1314 * Delegated diff alone is sufficient that we allow this.
1318 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1319 ZFS_DELEG_PERM_DIFF, cr)) == 0)
1322 error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
1324 error = zfs_secpolicy_hold(zc, innvl, cr);
1326 error = zfs_secpolicy_release(zc, innvl, cr);
1328 error = zfs_secpolicy_destroy(zc, innvl, cr);
1333 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1336 get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1340 nvlist_t *list = NULL;
1343 * Read in and unpack the user-supplied nvlist.
1346 return (SET_ERROR(EINVAL));
1348 packed = kmem_alloc(size, KM_SLEEP);
1350 if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1352 kmem_free(packed, size);
1353 return (SET_ERROR(EFAULT));
1356 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1357 kmem_free(packed, size);
1361 kmem_free(packed, size);
1368 * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
1369 * Entries will be removed from the end of the nvlist, and one int32 entry
1370 * named "N_MORE_ERRORS" will be added indicating how many entries were
1374 nvlist_smush(nvlist_t *errors, size_t max)
1378 size = fnvlist_size(errors);
1381 nvpair_t *more_errors;
1385 return (SET_ERROR(ENOMEM));
1387 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
1388 more_errors = nvlist_prev_nvpair(errors, NULL);
1391 nvpair_t *pair = nvlist_prev_nvpair(errors,
1393 fnvlist_remove_nvpair(errors, pair);
1395 size = fnvlist_size(errors);
1396 } while (size > max);
1398 fnvlist_remove_nvpair(errors, more_errors);
1399 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
1400 ASSERT3U(fnvlist_size(errors), <=, max);
1407 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1409 char *packed = NULL;
1413 size = fnvlist_size(nvl);
1415 if (size > zc->zc_nvlist_dst_size) {
1417 * Solaris returns ENOMEM here, because even if an error is
1418 * returned from an ioctl(2), new zc_nvlist_dst_size will be
1419 * passed to the userland. This is not the case for FreeBSD.
1420 * We need to return 0, so the kernel will copy the
1421 * zc_nvlist_dst_size back and the userland can discover that a
1422 * bigger buffer is needed.
1426 packed = fnvlist_pack(nvl, &size);
1427 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1428 size, zc->zc_iflags) != 0)
1429 error = SET_ERROR(EFAULT);
1430 fnvlist_pack_free(packed, size);
1433 zc->zc_nvlist_dst_size = size;
1434 zc->zc_nvlist_dst_filled = B_TRUE;
1439 getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
1445 error = dmu_objset_hold(dsname, FTAG, &os);
1448 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1449 dmu_objset_rele(os, FTAG);
1450 return (SET_ERROR(EINVAL));
1453 mutex_enter(&os->os_user_ptr_lock);
1454 *zfvp = dmu_objset_get_user(os);
1456 vfsp = (*zfvp)->z_vfs;
1459 error = SET_ERROR(ESRCH);
1461 mutex_exit(&os->os_user_ptr_lock);
1462 dmu_objset_rele(os, FTAG);
1464 error = vfs_busy(vfsp, 0);
1468 error = SET_ERROR(ESRCH);
1475 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
1476 * case its z_vfs will be NULL, and it will be opened as the owner.
1477 * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1478 * which prevents all vnode ops from running.
1481 zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
1485 if (getzfsvfs(name, zfvp) != 0)
1486 error = zfsvfs_create(name, zfvp);
1488 rrm_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
1490 if ((*zfvp)->z_unmounted) {
1492 * XXX we could probably try again, since the unmounting
1493 * thread should be just about to disassociate the
1494 * objset from the zfsvfs.
1496 rrm_exit(&(*zfvp)->z_teardown_lock, tag);
1497 return (SET_ERROR(EBUSY));
1504 zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
1506 rrm_exit(&zfsvfs->z_teardown_lock, tag);
1508 if (zfsvfs->z_vfs) {
1510 VFS_RELE(zfsvfs->z_vfs);
1512 vfs_unbusy(zfsvfs->z_vfs);
1515 dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1516 zfsvfs_free(zfsvfs);
1521 zfs_ioc_pool_create(zfs_cmd_t *zc)
1524 nvlist_t *config, *props = NULL;
1525 nvlist_t *rootprops = NULL;
1526 nvlist_t *zplprops = NULL;
1528 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1529 zc->zc_iflags, &config))
1532 if (zc->zc_nvlist_src_size != 0 && (error =
1533 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1534 zc->zc_iflags, &props))) {
1535 nvlist_free(config);
1540 nvlist_t *nvl = NULL;
1541 uint64_t version = SPA_VERSION;
1543 (void) nvlist_lookup_uint64(props,
1544 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1545 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1546 error = SET_ERROR(EINVAL);
1547 goto pool_props_bad;
1549 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
1551 error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
1553 nvlist_free(config);
1557 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1559 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1560 error = zfs_fill_zplprops_root(version, rootprops,
1563 goto pool_props_bad;
1566 error = spa_create(zc->zc_name, config, props, zplprops);
1569 * Set the remaining root properties
1571 if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
1572 ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
1573 (void) spa_destroy(zc->zc_name);
1576 nvlist_free(rootprops);
1577 nvlist_free(zplprops);
1578 nvlist_free(config);
1585 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1588 zfs_log_history(zc);
1589 error = spa_destroy(zc->zc_name);
1591 zvol_remove_minors(zc->zc_name);
1596 zfs_ioc_pool_import(zfs_cmd_t *zc)
1598 nvlist_t *config, *props = NULL;
1602 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1603 zc->zc_iflags, &config)) != 0)
1606 if (zc->zc_nvlist_src_size != 0 && (error =
1607 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1608 zc->zc_iflags, &props))) {
1609 nvlist_free(config);
1613 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1614 guid != zc->zc_guid)
1615 error = SET_ERROR(EINVAL);
1617 error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1619 if (zc->zc_nvlist_dst != 0) {
1622 if ((err = put_nvlist(zc, config)) != 0)
1626 nvlist_free(config);
1634 zfs_ioc_pool_export(zfs_cmd_t *zc)
1637 boolean_t force = (boolean_t)zc->zc_cookie;
1638 boolean_t hardforce = (boolean_t)zc->zc_guid;
1640 zfs_log_history(zc);
1641 error = spa_export(zc->zc_name, NULL, force, hardforce);
1643 zvol_remove_minors(zc->zc_name);
1648 zfs_ioc_pool_configs(zfs_cmd_t *zc)
1653 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1654 return (SET_ERROR(EEXIST));
1656 error = put_nvlist(zc, configs);
1658 nvlist_free(configs);
1665 * zc_name name of the pool
1668 * zc_cookie real errno
1669 * zc_nvlist_dst config nvlist
1670 * zc_nvlist_dst_size size of config nvlist
1673 zfs_ioc_pool_stats(zfs_cmd_t *zc)
1679 error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1680 sizeof (zc->zc_value));
1682 if (config != NULL) {
1683 ret = put_nvlist(zc, config);
1684 nvlist_free(config);
1687 * The config may be present even if 'error' is non-zero.
1688 * In this case we return success, and preserve the real errno
1691 zc->zc_cookie = error;
1700 * Try to import the given pool, returning pool stats as appropriate so that
1701 * user land knows which devices are available and overall pool health.
1704 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1706 nvlist_t *tryconfig, *config;
1709 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1710 zc->zc_iflags, &tryconfig)) != 0)
1713 config = spa_tryimport(tryconfig);
1715 nvlist_free(tryconfig);
1718 return (SET_ERROR(EINVAL));
1720 error = put_nvlist(zc, config);
1721 nvlist_free(config);
1728 * zc_name name of the pool
1729 * zc_cookie scan func (pool_scan_func_t)
1732 zfs_ioc_pool_scan(zfs_cmd_t *zc)
1737 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1740 if (zc->zc_cookie == POOL_SCAN_NONE)
1741 error = spa_scan_stop(spa);
1743 error = spa_scan(spa, zc->zc_cookie);
1745 spa_close(spa, FTAG);
1751 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1756 error = spa_open(zc->zc_name, &spa, FTAG);
1759 spa_close(spa, FTAG);
1765 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1770 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1773 if (zc->zc_cookie < spa_version(spa) ||
1774 !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1775 spa_close(spa, FTAG);
1776 return (SET_ERROR(EINVAL));
1779 spa_upgrade(spa, zc->zc_cookie);
1780 spa_close(spa, FTAG);
1786 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1793 if ((size = zc->zc_history_len) == 0)
1794 return (SET_ERROR(EINVAL));
1796 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1799 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1800 spa_close(spa, FTAG);
1801 return (SET_ERROR(ENOTSUP));
1804 hist_buf = kmem_alloc(size, KM_SLEEP);
1805 if ((error = spa_history_get(spa, &zc->zc_history_offset,
1806 &zc->zc_history_len, hist_buf)) == 0) {
1807 error = ddi_copyout(hist_buf,
1808 (void *)(uintptr_t)zc->zc_history,
1809 zc->zc_history_len, zc->zc_iflags);
1812 spa_close(spa, FTAG);
1813 kmem_free(hist_buf, size);
1818 zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1823 error = spa_open(zc->zc_name, &spa, FTAG);
1825 error = spa_change_guid(spa);
1826 spa_close(spa, FTAG);
1832 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1834 return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
1839 * zc_name name of filesystem
1840 * zc_obj object to find
1843 * zc_value name of object
1846 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1851 /* XXX reading from objset not owned */
1852 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1854 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1855 dmu_objset_rele(os, FTAG);
1856 return (SET_ERROR(EINVAL));
1858 error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
1859 sizeof (zc->zc_value));
1860 dmu_objset_rele(os, FTAG);
1867 * zc_name name of filesystem
1868 * zc_obj object to find
1871 * zc_stat stats on object
1872 * zc_value path to object
1875 zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1880 /* XXX reading from objset not owned */
1881 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1883 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1884 dmu_objset_rele(os, FTAG);
1885 return (SET_ERROR(EINVAL));
1887 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1888 sizeof (zc->zc_value));
1889 dmu_objset_rele(os, FTAG);
1895 zfs_ioc_vdev_add(zfs_cmd_t *zc)
1899 nvlist_t *config, **l2cache, **spares;
1900 uint_t nl2cache = 0, nspares = 0;
1902 error = spa_open(zc->zc_name, &spa, FTAG);
1906 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1907 zc->zc_iflags, &config);
1908 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1909 &l2cache, &nl2cache);
1911 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1916 * A root pool with concatenated devices is not supported.
1917 * Thus, can not add a device to a root pool.
1919 * Intent log device can not be added to a rootpool because
1920 * during mountroot, zil is replayed, a seperated log device
1921 * can not be accessed during the mountroot time.
1923 * l2cache and spare devices are ok to be added to a rootpool.
1925 if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
1926 nvlist_free(config);
1927 spa_close(spa, FTAG);
1928 return (SET_ERROR(EDOM));
1930 #endif /* illumos */
1933 error = spa_vdev_add(spa, config);
1934 nvlist_free(config);
1936 spa_close(spa, FTAG);
1942 * zc_name name of the pool
1943 * zc_nvlist_conf nvlist of devices to remove
1944 * zc_cookie to stop the remove?
1947 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1952 error = spa_open(zc->zc_name, &spa, FTAG);
1955 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1956 spa_close(spa, FTAG);
1961 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1965 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1967 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1969 switch (zc->zc_cookie) {
1970 case VDEV_STATE_ONLINE:
1971 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1974 case VDEV_STATE_OFFLINE:
1975 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1978 case VDEV_STATE_FAULTED:
1979 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1980 zc->zc_obj != VDEV_AUX_EXTERNAL)
1981 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1983 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
1986 case VDEV_STATE_DEGRADED:
1987 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1988 zc->zc_obj != VDEV_AUX_EXTERNAL)
1989 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1991 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
1995 error = SET_ERROR(EINVAL);
1997 zc->zc_cookie = newstate;
1998 spa_close(spa, FTAG);
2003 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
2006 int replacing = zc->zc_cookie;
2010 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2013 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
2014 zc->zc_iflags, &config)) == 0) {
2015 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
2016 nvlist_free(config);
2019 spa_close(spa, FTAG);
2024 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
2029 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2032 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
2034 spa_close(spa, FTAG);
2039 zfs_ioc_vdev_split(zfs_cmd_t *zc)
2042 nvlist_t *config, *props = NULL;
2044 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
2046 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2049 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
2050 zc->zc_iflags, &config)) {
2051 spa_close(spa, FTAG);
2055 if (zc->zc_nvlist_src_size != 0 && (error =
2056 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2057 zc->zc_iflags, &props))) {
2058 spa_close(spa, FTAG);
2059 nvlist_free(config);
2063 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
2065 spa_close(spa, FTAG);
2067 nvlist_free(config);
2074 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
2077 char *path = zc->zc_value;
2078 uint64_t guid = zc->zc_guid;
2081 error = spa_open(zc->zc_name, &spa, FTAG);
2085 error = spa_vdev_setpath(spa, guid, path);
2086 spa_close(spa, FTAG);
2091 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
2094 char *fru = zc->zc_value;
2095 uint64_t guid = zc->zc_guid;
2098 error = spa_open(zc->zc_name, &spa, FTAG);
2102 error = spa_vdev_setfru(spa, guid, fru);
2103 spa_close(spa, FTAG);
2108 zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
2113 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2115 if (zc->zc_nvlist_dst != 0 &&
2116 (error = dsl_prop_get_all(os, &nv)) == 0) {
2117 dmu_objset_stats(os, nv);
2119 * NB: zvol_get_stats() will read the objset contents,
2120 * which we aren't supposed to do with a
2121 * DS_MODE_USER hold, because it could be
2122 * inconsistent. So this is a bit of a workaround...
2123 * XXX reading with out owning
2125 if (!zc->zc_objset_stats.dds_inconsistent &&
2126 dmu_objset_type(os) == DMU_OST_ZVOL) {
2127 error = zvol_get_stats(os, nv);
2132 error = put_nvlist(zc, nv);
2141 * zc_name name of filesystem
2142 * zc_nvlist_dst_size size of buffer for property nvlist
2145 * zc_objset_stats stats
2146 * zc_nvlist_dst property nvlist
2147 * zc_nvlist_dst_size size of property nvlist
2150 zfs_ioc_objset_stats(zfs_cmd_t *zc)
2155 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2157 error = zfs_ioc_objset_stats_impl(zc, os);
2158 dmu_objset_rele(os, FTAG);
2161 if (error == ENOMEM)
2168 * zc_name name of filesystem
2169 * zc_nvlist_dst_size size of buffer for property nvlist
2172 * zc_nvlist_dst received property nvlist
2173 * zc_nvlist_dst_size size of received property nvlist
2175 * Gets received properties (distinct from local properties on or after
2176 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
2177 * local property values.
2180 zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
2186 * Without this check, we would return local property values if the
2187 * caller has not already received properties on or after
2188 * SPA_VERSION_RECVD_PROPS.
2190 if (!dsl_prop_get_hasrecvd(zc->zc_name))
2191 return (SET_ERROR(ENOTSUP));
2193 if (zc->zc_nvlist_dst != 0 &&
2194 (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
2195 error = put_nvlist(zc, nv);
2203 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2209 * zfs_get_zplprop() will either find a value or give us
2210 * the default value (if there is one).
2212 if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2214 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2220 * zc_name name of filesystem
2221 * zc_nvlist_dst_size size of buffer for zpl property nvlist
2224 * zc_nvlist_dst zpl property nvlist
2225 * zc_nvlist_dst_size size of zpl property nvlist
2228 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2233 /* XXX reading without owning */
2234 if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
2237 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2240 * NB: nvl_add_zplprop() will read the objset contents,
2241 * which we aren't supposed to do with a DS_MODE_USER
2242 * hold, because it could be inconsistent.
2244 if (zc->zc_nvlist_dst != 0 &&
2245 !zc->zc_objset_stats.dds_inconsistent &&
2246 dmu_objset_type(os) == DMU_OST_ZFS) {
2249 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2250 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2251 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2252 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2253 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2254 err = put_nvlist(zc, nv);
2257 err = SET_ERROR(ENOENT);
2259 dmu_objset_rele(os, FTAG);
2264 dataset_name_hidden(const char *name)
2267 * Skip over datasets that are not visible in this zone,
2268 * internal datasets (which have a $ in their name), and
2269 * temporary datasets (which have a % in their name).
2271 if (strchr(name, '$') != NULL)
2273 if (strchr(name, '%') != NULL)
2275 if (!INGLOBALZONE(curthread) && !zone_dataset_visible(name, NULL))
2282 * zc_name name of filesystem
2283 * zc_cookie zap cursor
2284 * zc_nvlist_dst_size size of buffer for property nvlist
2287 * zc_name name of next filesystem
2288 * zc_cookie zap cursor
2289 * zc_objset_stats stats
2290 * zc_nvlist_dst property nvlist
2291 * zc_nvlist_dst_size size of property nvlist
2294 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2299 size_t orig_len = strlen(zc->zc_name);
2302 if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
2303 if (error == ENOENT)
2304 error = SET_ERROR(ESRCH);
2308 p = strrchr(zc->zc_name, '/');
2309 if (p == NULL || p[1] != '\0')
2310 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2311 p = zc->zc_name + strlen(zc->zc_name);
2314 error = dmu_dir_list_next(os,
2315 sizeof (zc->zc_name) - (p - zc->zc_name), p,
2316 NULL, &zc->zc_cookie);
2317 if (error == ENOENT)
2318 error = SET_ERROR(ESRCH);
2319 } while (error == 0 && dataset_name_hidden(zc->zc_name));
2320 dmu_objset_rele(os, FTAG);
2323 * If it's an internal dataset (ie. with a '$' in its name),
2324 * don't try to get stats for it, otherwise we'll return ENOENT.
2326 if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
2327 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2328 if (error == ENOENT) {
2329 /* We lost a race with destroy, get the next one. */
2330 zc->zc_name[orig_len] = '\0';
2339 * zc_name name of filesystem
2340 * zc_cookie zap cursor
2341 * zc_nvlist_dst_size size of buffer for property nvlist
2342 * zc_simple when set, only name is requested
2345 * zc_name name of next snapshot
2346 * zc_objset_stats stats
2347 * zc_nvlist_dst property nvlist
2348 * zc_nvlist_dst_size size of property nvlist
2351 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2356 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2358 return (error == ENOENT ? ESRCH : error);
2362 * A dataset name of maximum length cannot have any snapshots,
2363 * so exit immediately.
2365 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >=
2366 ZFS_MAX_DATASET_NAME_LEN) {
2367 dmu_objset_rele(os, FTAG);
2368 return (SET_ERROR(ESRCH));
2371 error = dmu_snapshot_list_next(os,
2372 sizeof (zc->zc_name) - strlen(zc->zc_name),
2373 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2376 if (error == 0 && !zc->zc_simple) {
2378 dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2380 error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
2384 error = dmu_objset_from_ds(ds, &ossnap);
2386 error = zfs_ioc_objset_stats_impl(zc, ossnap);
2387 dsl_dataset_rele(ds, FTAG);
2389 } else if (error == ENOENT) {
2390 error = SET_ERROR(ESRCH);
2393 dmu_objset_rele(os, FTAG);
2394 /* if we failed, undo the @ that we tacked on to zc_name */
2396 *strchr(zc->zc_name, '@') = '\0';
2401 zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2403 const char *propname = nvpair_name(pair);
2405 unsigned int vallen;
2408 zfs_userquota_prop_t type;
2414 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2416 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2417 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2419 return (SET_ERROR(EINVAL));
2423 * A correctly constructed propname is encoded as
2424 * userquota@<rid>-<domain>.
2426 if ((dash = strchr(propname, '-')) == NULL ||
2427 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2429 return (SET_ERROR(EINVAL));
2436 err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
2438 err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
2439 zfsvfs_rele(zfsvfs, FTAG);
2446 * If the named property is one that has a special function to set its value,
2447 * return 0 on success and a positive error code on failure; otherwise if it is
2448 * not one of the special properties handled by this function, return -1.
2450 * XXX: It would be better for callers of the property interface if we handled
2451 * these special cases in dsl_prop.c (in the dsl layer).
2454 zfs_prop_set_special(const char *dsname, zprop_source_t source,
2457 const char *propname = nvpair_name(pair);
2458 zfs_prop_t prop = zfs_name_to_prop(propname);
2462 if (prop == ZPROP_INVAL) {
2463 if (zfs_prop_userquota(propname))
2464 return (zfs_prop_set_userquota(dsname, pair));
2468 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2470 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2471 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2475 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
2478 VERIFY(0 == nvpair_value_uint64(pair, &intval));
2481 case ZFS_PROP_QUOTA:
2482 err = dsl_dir_set_quota(dsname, source, intval);
2484 case ZFS_PROP_REFQUOTA:
2485 err = dsl_dataset_set_refquota(dsname, source, intval);
2487 case ZFS_PROP_FILESYSTEM_LIMIT:
2488 case ZFS_PROP_SNAPSHOT_LIMIT:
2489 if (intval == UINT64_MAX) {
2490 /* clearing the limit, just do it */
2493 err = dsl_dir_activate_fs_ss_limit(dsname);
2496 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2497 * default path to set the value in the nvlist.
2502 case ZFS_PROP_RESERVATION:
2503 err = dsl_dir_set_reservation(dsname, source, intval);
2505 case ZFS_PROP_REFRESERVATION:
2506 err = dsl_dataset_set_refreservation(dsname, source, intval);
2508 case ZFS_PROP_VOLSIZE:
2509 err = zvol_set_volsize(dsname, intval);
2511 case ZFS_PROP_VERSION:
2515 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2518 err = zfs_set_version(zfsvfs, intval);
2519 zfsvfs_rele(zfsvfs, FTAG);
2521 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2524 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2525 (void) strcpy(zc->zc_name, dsname);
2526 (void) zfs_ioc_userspace_upgrade(zc);
2527 kmem_free(zc, sizeof (zfs_cmd_t));
2539 * This function is best effort. If it fails to set any of the given properties,
2540 * it continues to set as many as it can and returns the last error
2541 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2542 * with the list of names of all the properties that failed along with the
2543 * corresponding error numbers.
2545 * If every property is set successfully, zero is returned and errlist is not
2549 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2557 nvlist_t *genericnvl = fnvlist_alloc();
2558 nvlist_t *retrynvl = fnvlist_alloc();
2562 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2563 const char *propname = nvpair_name(pair);
2564 zfs_prop_t prop = zfs_name_to_prop(propname);
2567 /* decode the property value */
2569 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2571 attrs = fnvpair_value_nvlist(pair);
2572 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2574 err = SET_ERROR(EINVAL);
2577 /* Validate value type */
2578 if (err == 0 && prop == ZPROP_INVAL) {
2579 if (zfs_prop_user(propname)) {
2580 if (nvpair_type(propval) != DATA_TYPE_STRING)
2581 err = SET_ERROR(EINVAL);
2582 } else if (zfs_prop_userquota(propname)) {
2583 if (nvpair_type(propval) !=
2584 DATA_TYPE_UINT64_ARRAY)
2585 err = SET_ERROR(EINVAL);
2587 err = SET_ERROR(EINVAL);
2589 } else if (err == 0) {
2590 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2591 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2592 err = SET_ERROR(EINVAL);
2593 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2596 intval = fnvpair_value_uint64(propval);
2598 switch (zfs_prop_get_type(prop)) {
2599 case PROP_TYPE_NUMBER:
2601 case PROP_TYPE_STRING:
2602 err = SET_ERROR(EINVAL);
2604 case PROP_TYPE_INDEX:
2605 if (zfs_prop_index_to_string(prop,
2606 intval, &unused) != 0)
2607 err = SET_ERROR(EINVAL);
2611 "unknown property type");
2614 err = SET_ERROR(EINVAL);
2618 /* Validate permissions */
2620 err = zfs_check_settable(dsname, pair, CRED());
2623 err = zfs_prop_set_special(dsname, source, pair);
2626 * For better performance we build up a list of
2627 * properties to set in a single transaction.
2629 err = nvlist_add_nvpair(genericnvl, pair);
2630 } else if (err != 0 && nvl != retrynvl) {
2632 * This may be a spurious error caused by
2633 * receiving quota and reservation out of order.
2634 * Try again in a second pass.
2636 err = nvlist_add_nvpair(retrynvl, pair);
2641 if (errlist != NULL)
2642 fnvlist_add_int32(errlist, propname, err);
2647 if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2652 if (!nvlist_empty(genericnvl) &&
2653 dsl_props_set(dsname, source, genericnvl) != 0) {
2655 * If this fails, we still want to set as many properties as we
2656 * can, so try setting them individually.
2659 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2660 const char *propname = nvpair_name(pair);
2664 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2666 attrs = fnvpair_value_nvlist(pair);
2667 propval = fnvlist_lookup_nvpair(attrs,
2671 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2672 strval = fnvpair_value_string(propval);
2673 err = dsl_prop_set_string(dsname, propname,
2676 intval = fnvpair_value_uint64(propval);
2677 err = dsl_prop_set_int(dsname, propname, source,
2682 if (errlist != NULL) {
2683 fnvlist_add_int32(errlist, propname,
2690 nvlist_free(genericnvl);
2691 nvlist_free(retrynvl);
2697 * Check that all the properties are valid user properties.
2700 zfs_check_userprops(const char *fsname, nvlist_t *nvl)
2702 nvpair_t *pair = NULL;
2705 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2706 const char *propname = nvpair_name(pair);
2708 if (!zfs_prop_user(propname) ||
2709 nvpair_type(pair) != DATA_TYPE_STRING)
2710 return (SET_ERROR(EINVAL));
2712 if (error = zfs_secpolicy_write_perms(fsname,
2713 ZFS_DELEG_PERM_USERPROP, CRED()))
2716 if (strlen(propname) >= ZAP_MAXNAMELEN)
2717 return (SET_ERROR(ENAMETOOLONG));
2719 if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN)
2726 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2730 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2733 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2734 if (nvlist_exists(skipped, nvpair_name(pair)))
2737 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2742 clear_received_props(const char *dsname, nvlist_t *props,
2746 nvlist_t *cleared_props = NULL;
2747 props_skip(props, skipped, &cleared_props);
2748 if (!nvlist_empty(cleared_props)) {
2750 * Acts on local properties until the dataset has received
2751 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2753 zprop_source_t flags = (ZPROP_SRC_NONE |
2754 (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
2755 err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
2757 nvlist_free(cleared_props);
2763 * zc_name name of filesystem
2764 * zc_value name of property to set
2765 * zc_nvlist_src{_size} nvlist of properties to apply
2766 * zc_cookie received properties flag
2769 * zc_nvlist_dst{_size} error for each unapplied received property
2772 zfs_ioc_set_prop(zfs_cmd_t *zc)
2775 boolean_t received = zc->zc_cookie;
2776 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2781 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2782 zc->zc_iflags, &nvl)) != 0)
2786 nvlist_t *origprops;
2788 if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
2789 (void) clear_received_props(zc->zc_name,
2791 nvlist_free(origprops);
2794 error = dsl_prop_set_hasrecvd(zc->zc_name);
2797 errors = fnvlist_alloc();
2799 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
2801 if (zc->zc_nvlist_dst != 0 && errors != NULL) {
2802 (void) put_nvlist(zc, errors);
2805 nvlist_free(errors);
2812 * zc_name name of filesystem
2813 * zc_value name of property to inherit
2814 * zc_cookie revert to received value if TRUE
2819 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2821 const char *propname = zc->zc_value;
2822 zfs_prop_t prop = zfs_name_to_prop(propname);
2823 boolean_t received = zc->zc_cookie;
2824 zprop_source_t source = (received
2825 ? ZPROP_SRC_NONE /* revert to received value, if any */
2826 : ZPROP_SRC_INHERITED); /* explicitly inherit */
2835 * zfs_prop_set_special() expects properties in the form of an
2836 * nvpair with type info.
2838 if (prop == ZPROP_INVAL) {
2839 if (!zfs_prop_user(propname))
2840 return (SET_ERROR(EINVAL));
2842 type = PROP_TYPE_STRING;
2843 } else if (prop == ZFS_PROP_VOLSIZE ||
2844 prop == ZFS_PROP_VERSION) {
2845 return (SET_ERROR(EINVAL));
2847 type = zfs_prop_get_type(prop);
2850 VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2853 case PROP_TYPE_STRING:
2854 VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2856 case PROP_TYPE_NUMBER:
2857 case PROP_TYPE_INDEX:
2858 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2862 return (SET_ERROR(EINVAL));
2865 pair = nvlist_next_nvpair(dummy, NULL);
2866 err = zfs_prop_set_special(zc->zc_name, source, pair);
2869 return (err); /* special property already handled */
2872 * Only check this in the non-received case. We want to allow
2873 * 'inherit -S' to revert non-inheritable properties like quota
2874 * and reservation to the received or default values even though
2875 * they are not considered inheritable.
2877 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2878 return (SET_ERROR(EINVAL));
2881 /* property name has been validated by zfs_secpolicy_inherit_prop() */
2882 return (dsl_prop_inherit(zc->zc_name, zc->zc_value, source));
2886 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2893 if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2894 zc->zc_iflags, &props))
2898 * If the only property is the configfile, then just do a spa_lookup()
2899 * to handle the faulted case.
2901 pair = nvlist_next_nvpair(props, NULL);
2902 if (pair != NULL && strcmp(nvpair_name(pair),
2903 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2904 nvlist_next_nvpair(props, pair) == NULL) {
2905 mutex_enter(&spa_namespace_lock);
2906 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2907 spa_configfile_set(spa, props, B_FALSE);
2908 spa_config_sync(spa, B_FALSE, B_TRUE);
2910 mutex_exit(&spa_namespace_lock);
2917 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2922 error = spa_prop_set(spa, props);
2925 spa_close(spa, FTAG);
2931 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2935 nvlist_t *nvp = NULL;
2937 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2939 * If the pool is faulted, there may be properties we can still
2940 * get (such as altroot and cachefile), so attempt to get them
2943 mutex_enter(&spa_namespace_lock);
2944 if ((spa = spa_lookup(zc->zc_name)) != NULL)
2945 error = spa_prop_get(spa, &nvp);
2946 mutex_exit(&spa_namespace_lock);
2948 error = spa_prop_get(spa, &nvp);
2949 spa_close(spa, FTAG);
2952 if (error == 0 && zc->zc_nvlist_dst != 0)
2953 error = put_nvlist(zc, nvp);
2955 error = SET_ERROR(EFAULT);
2963 * zc_name name of filesystem
2964 * zc_nvlist_src{_size} nvlist of delegated permissions
2965 * zc_perm_action allow/unallow flag
2970 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2973 nvlist_t *fsaclnv = NULL;
2975 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2976 zc->zc_iflags, &fsaclnv)) != 0)
2980 * Verify nvlist is constructed correctly
2982 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2983 nvlist_free(fsaclnv);
2984 return (SET_ERROR(EINVAL));
2988 * If we don't have PRIV_SYS_MOUNT, then validate
2989 * that user is allowed to hand out each permission in
2993 error = secpolicy_zfs(CRED());
2995 if (zc->zc_perm_action == B_FALSE) {
2996 error = dsl_deleg_can_allow(zc->zc_name,
2999 error = dsl_deleg_can_unallow(zc->zc_name,
3005 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
3007 nvlist_free(fsaclnv);
3013 * zc_name name of filesystem
3016 * zc_nvlist_src{_size} nvlist of delegated permissions
3019 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
3024 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
3025 error = put_nvlist(zc, nvp);
3033 * Search the vfs list for a specified resource. Returns a pointer to it
3034 * or NULL if no suitable entry is found. The caller of this routine
3035 * is responsible for releasing the returned vfs pointer.
3038 zfs_get_vfs(const char *resource)
3042 mtx_lock(&mountlist_mtx);
3043 TAILQ_FOREACH(vfsp, &mountlist, mnt_list) {
3044 if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
3045 if (vfs_busy(vfsp, MBF_MNTLSTLOCK) != 0)
3051 mtx_unlock(&mountlist_mtx);
3057 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3059 zfs_creat_t *zct = arg;
3061 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
3064 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
3068 * os parent objset pointer (NULL if root fs)
3069 * fuids_ok fuids allowed in this version of the spa?
3070 * sa_ok SAs allowed in this version of the spa?
3071 * createprops list of properties requested by creator
3074 * zplprops values for the zplprops we attach to the master node object
3075 * is_ci true if requested file system will be purely case-insensitive
3077 * Determine the settings for utf8only, normalization and
3078 * casesensitivity. Specific values may have been requested by the
3079 * creator and/or we can inherit values from the parent dataset. If
3080 * the file system is of too early a vintage, a creator can not
3081 * request settings for these properties, even if the requested
3082 * setting is the default value. We don't actually want to create dsl
3083 * properties for these, so remove them from the source nvlist after
3087 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
3088 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
3089 nvlist_t *zplprops, boolean_t *is_ci)
3091 uint64_t sense = ZFS_PROP_UNDEFINED;
3092 uint64_t norm = ZFS_PROP_UNDEFINED;
3093 uint64_t u8 = ZFS_PROP_UNDEFINED;
3095 ASSERT(zplprops != NULL);
3097 if (os != NULL && os->os_phys->os_type != DMU_OST_ZFS)
3098 return (SET_ERROR(EINVAL));
3101 * Pull out creator prop choices, if any.
3104 (void) nvlist_lookup_uint64(createprops,
3105 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
3106 (void) nvlist_lookup_uint64(createprops,
3107 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
3108 (void) nvlist_remove_all(createprops,
3109 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
3110 (void) nvlist_lookup_uint64(createprops,
3111 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
3112 (void) nvlist_remove_all(createprops,
3113 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
3114 (void) nvlist_lookup_uint64(createprops,
3115 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
3116 (void) nvlist_remove_all(createprops,
3117 zfs_prop_to_name(ZFS_PROP_CASE));
3121 * If the zpl version requested is whacky or the file system
3122 * or pool is version is too "young" to support normalization
3123 * and the creator tried to set a value for one of the props,
3126 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
3127 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
3128 (zplver >= ZPL_VERSION_SA && !sa_ok) ||
3129 (zplver < ZPL_VERSION_NORMALIZATION &&
3130 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
3131 sense != ZFS_PROP_UNDEFINED)))
3132 return (SET_ERROR(ENOTSUP));
3135 * Put the version in the zplprops
3137 VERIFY(nvlist_add_uint64(zplprops,
3138 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3140 if (norm == ZFS_PROP_UNDEFINED)
3141 VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
3142 VERIFY(nvlist_add_uint64(zplprops,
3143 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3146 * If we're normalizing, names must always be valid UTF-8 strings.
3150 if (u8 == ZFS_PROP_UNDEFINED)
3151 VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
3152 VERIFY(nvlist_add_uint64(zplprops,
3153 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3155 if (sense == ZFS_PROP_UNDEFINED)
3156 VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
3157 VERIFY(nvlist_add_uint64(zplprops,
3158 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3161 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
3167 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
3168 nvlist_t *zplprops, boolean_t *is_ci)
3170 boolean_t fuids_ok, sa_ok;
3171 uint64_t zplver = ZPL_VERSION;
3172 objset_t *os = NULL;
3173 char parentname[ZFS_MAX_DATASET_NAME_LEN];
3179 (void) strlcpy(parentname, dataset, sizeof (parentname));
3180 cp = strrchr(parentname, '/');
3184 if ((error = spa_open(dataset, &spa, FTAG)) != 0)
3187 spa_vers = spa_version(spa);
3188 spa_close(spa, FTAG);
3190 zplver = zfs_zpl_version_map(spa_vers);
3191 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3192 sa_ok = (zplver >= ZPL_VERSION_SA);
3195 * Open parent object set so we can inherit zplprop values.
3197 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
3200 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
3202 dmu_objset_rele(os, FTAG);
3207 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
3208 nvlist_t *zplprops, boolean_t *is_ci)
3212 uint64_t zplver = ZPL_VERSION;
3215 zplver = zfs_zpl_version_map(spa_vers);
3216 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3217 sa_ok = (zplver >= ZPL_VERSION_SA);
3219 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
3220 createprops, zplprops, is_ci);
3226 * "type" -> dmu_objset_type_t (int32)
3227 * (optional) "props" -> { prop -> value }
3230 * outnvl: propname -> error code (int32)
3233 zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3236 zfs_creat_t zct = { 0 };
3237 nvlist_t *nvprops = NULL;
3238 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
3240 dmu_objset_type_t type;
3241 boolean_t is_insensitive = B_FALSE;
3243 if (nvlist_lookup_int32(innvl, "type", &type32) != 0)
3244 return (SET_ERROR(EINVAL));
3246 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3250 cbfunc = zfs_create_cb;
3254 cbfunc = zvol_create_cb;
3261 if (strchr(fsname, '@') ||
3262 strchr(fsname, '%'))
3263 return (SET_ERROR(EINVAL));
3265 zct.zct_props = nvprops;
3268 return (SET_ERROR(EINVAL));
3270 if (type == DMU_OST_ZVOL) {
3271 uint64_t volsize, volblocksize;
3273 if (nvprops == NULL)
3274 return (SET_ERROR(EINVAL));
3275 if (nvlist_lookup_uint64(nvprops,
3276 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3277 return (SET_ERROR(EINVAL));
3279 if ((error = nvlist_lookup_uint64(nvprops,
3280 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3281 &volblocksize)) != 0 && error != ENOENT)
3282 return (SET_ERROR(EINVAL));
3285 volblocksize = zfs_prop_default_numeric(
3286 ZFS_PROP_VOLBLOCKSIZE);
3288 if ((error = zvol_check_volblocksize(
3289 volblocksize)) != 0 ||
3290 (error = zvol_check_volsize(volsize,
3291 volblocksize)) != 0)
3293 } else if (type == DMU_OST_ZFS) {
3297 * We have to have normalization and
3298 * case-folding flags correct when we do the
3299 * file system creation, so go figure them out
3302 VERIFY(nvlist_alloc(&zct.zct_zplprops,
3303 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3304 error = zfs_fill_zplprops(fsname, nvprops,
3305 zct.zct_zplprops, &is_insensitive);
3307 nvlist_free(zct.zct_zplprops);
3312 error = dmu_objset_create(fsname, type,
3313 is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
3314 nvlist_free(zct.zct_zplprops);
3317 * It would be nice to do this atomically.
3320 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3323 (void) dsl_destroy_head(fsname);
3326 if (error == 0 && type == DMU_OST_ZVOL)
3327 zvol_create_minors(fsname);
3334 * "origin" -> name of origin snapshot
3335 * (optional) "props" -> { prop -> value }
3338 * outnvl: propname -> error code (int32)
3341 zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3344 nvlist_t *nvprops = NULL;
3347 if (nvlist_lookup_string(innvl, "origin", &origin_name) != 0)
3348 return (SET_ERROR(EINVAL));
3349 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3351 if (strchr(fsname, '@') ||
3352 strchr(fsname, '%'))
3353 return (SET_ERROR(EINVAL));
3355 if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3356 return (SET_ERROR(EINVAL));
3357 error = dmu_objset_clone(fsname, origin_name);
3362 * It would be nice to do this atomically.
3365 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3368 (void) dsl_destroy_head(fsname);
3372 zvol_create_minors(fsname);
3379 * "snaps" -> { snapshot1, snapshot2 }
3380 * (optional) "props" -> { prop -> value (string) }
3383 * outnvl: snapshot -> error code (int32)
3386 zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3389 nvlist_t *props = NULL;
3393 (void) nvlist_lookup_nvlist(innvl, "props", &props);
3394 if ((error = zfs_check_userprops(poolname, props)) != 0)
3397 if (!nvlist_empty(props) &&
3398 zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3399 return (SET_ERROR(ENOTSUP));
3401 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3402 return (SET_ERROR(EINVAL));
3403 poollen = strlen(poolname);
3404 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3405 pair = nvlist_next_nvpair(snaps, pair)) {
3406 const char *name = nvpair_name(pair);
3407 const char *cp = strchr(name, '@');
3410 * The snap name must contain an @, and the part after it must
3411 * contain only valid characters.
3414 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3415 return (SET_ERROR(EINVAL));
3418 * The snap must be in the specified pool.
3420 if (strncmp(name, poolname, poollen) != 0 ||
3421 (name[poollen] != '/' && name[poollen] != '@'))
3422 return (SET_ERROR(EXDEV));
3424 /* This must be the only snap of this fs. */
3425 for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
3426 pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
3427 if (strncmp(name, nvpair_name(pair2), cp - name + 1)
3429 return (SET_ERROR(EXDEV));
3434 error = dsl_dataset_snapshot(snaps, props, outnvl);
3439 * innvl: "message" -> string
3443 zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
3451 * The poolname in the ioctl is not set, we get it from the TSD,
3452 * which was set at the end of the last successful ioctl that allows
3453 * logging. The secpolicy func already checked that it is set.
3454 * Only one log ioctl is allowed after each successful ioctl, so
3455 * we clear the TSD here.
3457 poolname = tsd_get(zfs_allow_log_key);
3458 (void) tsd_set(zfs_allow_log_key, NULL);
3459 error = spa_open(poolname, &spa, FTAG);
3464 if (nvlist_lookup_string(innvl, "message", &message) != 0) {
3465 spa_close(spa, FTAG);
3466 return (SET_ERROR(EINVAL));
3469 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
3470 spa_close(spa, FTAG);
3471 return (SET_ERROR(ENOTSUP));
3474 error = spa_history_log(spa, message);
3475 spa_close(spa, FTAG);
3481 zfs_ioc_nextboot(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
3483 char name[MAXNAMELEN];
3491 if (nvlist_lookup_uint64(innvl,
3492 ZPOOL_CONFIG_POOL_GUID, &pool_guid) != 0)
3494 if (nvlist_lookup_uint64(innvl,
3495 ZPOOL_CONFIG_GUID, &vdev_guid) != 0)
3497 if (nvlist_lookup_string(innvl,
3498 "command", &command) != 0)
3501 mutex_enter(&spa_namespace_lock);
3502 spa = spa_by_guid(pool_guid, vdev_guid);
3504 strcpy(name, spa_name(spa));
3505 mutex_exit(&spa_namespace_lock);
3509 if ((error = spa_open(name, &spa, FTAG)) != 0)
3511 spa_vdev_state_enter(spa, SCL_ALL);
3512 vd = spa_lookup_by_guid(spa, vdev_guid, B_TRUE);
3514 (void) spa_vdev_state_exit(spa, NULL, ENXIO);
3515 spa_close(spa, FTAG);
3518 error = vdev_label_write_pad2(vd, command, strlen(command));
3519 (void) spa_vdev_state_exit(spa, NULL, 0);
3520 txg_wait_synced(spa->spa_dsl_pool, 0);
3521 spa_close(spa, FTAG);
3527 * The dp_config_rwlock must not be held when calling this, because the
3528 * unmount may need to write out data.
3530 * This function is best-effort. Callers must deal gracefully if it
3531 * remains mounted (or is remounted after this call).
3533 * Returns 0 if the argument is not a snapshot, or it is not currently a
3534 * filesystem, or we were able to unmount it. Returns error code otherwise.
3537 zfs_unmount_snap(const char *snapname)
3543 if (strchr(snapname, '@') == NULL)
3546 vfsp = zfs_get_vfs(snapname);
3550 zfsvfs = vfsp->vfs_data;
3551 ASSERT(!dsl_pool_config_held(dmu_objset_pool(zfsvfs->z_os)));
3553 err = vn_vfswlock(vfsp->vfs_vnodecovered);
3560 return (SET_ERROR(err));
3563 * Always force the unmount for snapshots.
3567 (void) dounmount(vfsp, MS_FORCE, kcred);
3570 (void) dounmount(vfsp, MS_FORCE, curthread);
3577 zfs_unmount_snap_cb(const char *snapname, void *arg)
3579 return (zfs_unmount_snap(snapname));
3583 * When a clone is destroyed, its origin may also need to be destroyed,
3584 * in which case it must be unmounted. This routine will do that unmount
3588 zfs_destroy_unmount_origin(const char *fsname)
3594 error = dmu_objset_hold(fsname, FTAG, &os);
3597 ds = dmu_objset_ds(os);
3598 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
3599 char originname[ZFS_MAX_DATASET_NAME_LEN];
3600 dsl_dataset_name(ds->ds_prev, originname);
3601 dmu_objset_rele(os, FTAG);
3602 (void) zfs_unmount_snap(originname);
3604 dmu_objset_rele(os, FTAG);
3610 * "snaps" -> { snapshot1, snapshot2 }
3611 * (optional boolean) "defer"
3614 * outnvl: snapshot -> error code (int32)
3619 zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3626 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3627 return (SET_ERROR(EINVAL));
3628 defer = nvlist_exists(innvl, "defer");
3630 poollen = strlen(poolname);
3631 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3632 pair = nvlist_next_nvpair(snaps, pair)) {
3633 const char *name = nvpair_name(pair);
3636 * The snap must be in the specified pool to prevent the
3637 * invalid removal of zvol minors below.
3639 if (strncmp(name, poolname, poollen) != 0 ||
3640 (name[poollen] != '/' && name[poollen] != '@'))
3641 return (SET_ERROR(EXDEV));
3643 error = zfs_unmount_snap(name);
3646 #if defined(__FreeBSD__)
3647 zvol_remove_minors(name);
3651 return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
3655 * Create bookmarks. Bookmark names are of the form <fs>#<bmark>.
3656 * All bookmarks must be in the same pool.
3659 * bookmark1 -> snapshot1, bookmark2 -> snapshot2
3662 * outnvl: bookmark -> error code (int32)
3667 zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3669 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3670 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3674 * Verify the snapshot argument.
3676 if (nvpair_value_string(pair, &snap_name) != 0)
3677 return (SET_ERROR(EINVAL));
3680 /* Verify that the keys (bookmarks) are unique */
3681 for (nvpair_t *pair2 = nvlist_next_nvpair(innvl, pair);
3682 pair2 != NULL; pair2 = nvlist_next_nvpair(innvl, pair2)) {
3683 if (strcmp(nvpair_name(pair), nvpair_name(pair2)) == 0)
3684 return (SET_ERROR(EINVAL));
3688 return (dsl_bookmark_create(innvl, outnvl));
3693 * property 1, property 2, ...
3697 * bookmark name 1 -> { property 1, property 2, ... },
3698 * bookmark name 2 -> { property 1, property 2, ... }
3703 zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3705 return (dsl_get_bookmarks(fsname, innvl, outnvl));
3710 * bookmark name 1, bookmark name 2
3713 * outnvl: bookmark -> error code (int32)
3717 zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl,
3722 poollen = strlen(poolname);
3723 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3724 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3725 const char *name = nvpair_name(pair);
3726 const char *cp = strchr(name, '#');
3729 * The bookmark name must contain an #, and the part after it
3730 * must contain only valid characters.
3733 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3734 return (SET_ERROR(EINVAL));
3737 * The bookmark must be in the specified pool.
3739 if (strncmp(name, poolname, poollen) != 0 ||
3740 (name[poollen] != '/' && name[poollen] != '#'))
3741 return (SET_ERROR(EXDEV));
3744 error = dsl_bookmark_destroy(innvl, outnvl);
3750 * zc_name name of dataset to destroy
3751 * zc_objset_type type of objset
3752 * zc_defer_destroy mark for deferred destroy
3757 zfs_ioc_destroy(zfs_cmd_t *zc)
3761 if (zc->zc_objset_type == DMU_OST_ZFS) {
3762 err = zfs_unmount_snap(zc->zc_name);
3767 if (strchr(zc->zc_name, '@'))
3768 err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
3770 err = dsl_destroy_head(zc->zc_name);
3771 if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
3773 zvol_remove_minors(zc->zc_name);
3775 (void) zvol_remove_minor(zc->zc_name);
3781 * fsname is name of dataset to rollback (to most recent snapshot)
3783 * innvl may contain name of expected target snapshot
3785 * outnvl: "target" -> name of most recent snapshot
3790 zfs_ioc_rollback(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3793 char *target = NULL;
3796 (void) nvlist_lookup_string(innvl, "target", &target);
3797 if (target != NULL) {
3798 int fslen = strlen(fsname);
3800 if (strncmp(fsname, target, fslen) != 0)
3801 return (SET_ERROR(EINVAL));
3802 if (target[fslen] != '@')
3803 return (SET_ERROR(EINVAL));
3806 if (getzfsvfs(fsname, &zfsvfs) == 0) {
3809 ds = dmu_objset_ds(zfsvfs->z_os);
3810 error = zfs_suspend_fs(zfsvfs);
3814 error = dsl_dataset_rollback(fsname, target, zfsvfs,
3816 resume_err = zfs_resume_fs(zfsvfs, ds);
3817 error = error ? error : resume_err;
3820 VFS_RELE(zfsvfs->z_vfs);
3822 vfs_unbusy(zfsvfs->z_vfs);
3825 error = dsl_dataset_rollback(fsname, target, NULL, outnvl);
3831 recursive_unmount(const char *fsname, void *arg)
3833 const char *snapname = arg;
3834 char fullname[ZFS_MAX_DATASET_NAME_LEN];
3836 (void) snprintf(fullname, sizeof (fullname), "%s@%s", fsname, snapname);
3837 return (zfs_unmount_snap(fullname));
3842 * zc_name old name of dataset
3843 * zc_value new name of dataset
3844 * zc_cookie recursive flag (only valid for snapshots)
3849 zfs_ioc_rename(zfs_cmd_t *zc)
3851 boolean_t recursive = zc->zc_cookie & 1;
3853 boolean_t allow_mounted = B_TRUE;
3856 allow_mounted = (zc->zc_cookie & 2) != 0;
3859 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3860 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3861 strchr(zc->zc_value, '%'))
3862 return (SET_ERROR(EINVAL));
3864 at = strchr(zc->zc_name, '@');
3866 /* snaps must be in same fs */
3869 if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
3870 return (SET_ERROR(EXDEV));
3872 if (zc->zc_objset_type == DMU_OST_ZFS && !allow_mounted) {
3873 error = dmu_objset_find(zc->zc_name,
3874 recursive_unmount, at + 1,
3875 recursive ? DS_FIND_CHILDREN : 0);
3881 error = dsl_dataset_rename_snapshot(zc->zc_name,
3882 at + 1, strchr(zc->zc_value, '@') + 1, recursive);
3888 if (zc->zc_objset_type == DMU_OST_ZVOL)
3889 (void) zvol_remove_minor(zc->zc_name);
3891 return (dsl_dir_rename(zc->zc_name, zc->zc_value));
3896 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
3898 const char *propname = nvpair_name(pair);
3899 boolean_t issnap = (strchr(dsname, '@') != NULL);
3900 zfs_prop_t prop = zfs_name_to_prop(propname);
3904 if (prop == ZPROP_INVAL) {
3905 if (zfs_prop_user(propname)) {
3906 if (err = zfs_secpolicy_write_perms(dsname,
3907 ZFS_DELEG_PERM_USERPROP, cr))
3912 if (!issnap && zfs_prop_userquota(propname)) {
3913 const char *perm = NULL;
3914 const char *uq_prefix =
3915 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
3916 const char *gq_prefix =
3917 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
3919 if (strncmp(propname, uq_prefix,
3920 strlen(uq_prefix)) == 0) {
3921 perm = ZFS_DELEG_PERM_USERQUOTA;
3922 } else if (strncmp(propname, gq_prefix,
3923 strlen(gq_prefix)) == 0) {
3924 perm = ZFS_DELEG_PERM_GROUPQUOTA;
3926 /* USERUSED and GROUPUSED are read-only */
3927 return (SET_ERROR(EINVAL));
3930 if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
3935 return (SET_ERROR(EINVAL));
3939 return (SET_ERROR(EINVAL));
3941 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
3943 * dsl_prop_get_all_impl() returns properties in this
3947 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
3948 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3953 * Check that this value is valid for this pool version
3956 case ZFS_PROP_COMPRESSION:
3958 * If the user specified gzip compression, make sure
3959 * the SPA supports it. We ignore any errors here since
3960 * we'll catch them later.
3962 if (nvpair_value_uint64(pair, &intval) == 0) {
3963 if (intval >= ZIO_COMPRESS_GZIP_1 &&
3964 intval <= ZIO_COMPRESS_GZIP_9 &&
3965 zfs_earlier_version(dsname,
3966 SPA_VERSION_GZIP_COMPRESSION)) {
3967 return (SET_ERROR(ENOTSUP));
3970 if (intval == ZIO_COMPRESS_ZLE &&
3971 zfs_earlier_version(dsname,
3972 SPA_VERSION_ZLE_COMPRESSION))
3973 return (SET_ERROR(ENOTSUP));
3975 if (intval == ZIO_COMPRESS_LZ4) {
3978 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3981 if (!spa_feature_is_enabled(spa,
3982 SPA_FEATURE_LZ4_COMPRESS)) {
3983 spa_close(spa, FTAG);
3984 return (SET_ERROR(ENOTSUP));
3986 spa_close(spa, FTAG);
3990 * If this is a bootable dataset then
3991 * verify that the compression algorithm
3992 * is supported for booting. We must return
3993 * something other than ENOTSUP since it
3994 * implies a downrev pool version.
3996 if (zfs_is_bootfs(dsname) &&
3997 !BOOTFS_COMPRESS_VALID(intval)) {
3998 return (SET_ERROR(ERANGE));
4003 case ZFS_PROP_COPIES:
4004 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
4005 return (SET_ERROR(ENOTSUP));
4008 case ZFS_PROP_RECORDSIZE:
4009 /* Record sizes above 128k need the feature to be enabled */
4010 if (nvpair_value_uint64(pair, &intval) == 0 &&
4011 intval > SPA_OLD_MAXBLOCKSIZE) {
4015 * We don't allow setting the property above 1MB,
4016 * unless the tunable has been changed.
4018 if (intval > zfs_max_recordsize ||
4019 intval > SPA_MAXBLOCKSIZE)
4020 return (SET_ERROR(ERANGE));
4022 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4025 if (!spa_feature_is_enabled(spa,
4026 SPA_FEATURE_LARGE_BLOCKS)) {
4027 spa_close(spa, FTAG);
4028 return (SET_ERROR(ENOTSUP));
4030 spa_close(spa, FTAG);
4034 case ZFS_PROP_SHARESMB:
4035 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
4036 return (SET_ERROR(ENOTSUP));
4039 case ZFS_PROP_ACLINHERIT:
4040 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
4041 nvpair_value_uint64(pair, &intval) == 0) {
4042 if (intval == ZFS_ACL_PASSTHROUGH_X &&
4043 zfs_earlier_version(dsname,
4044 SPA_VERSION_PASSTHROUGH_X))
4045 return (SET_ERROR(ENOTSUP));
4049 case ZFS_PROP_CHECKSUM:
4050 case ZFS_PROP_DEDUP:
4052 spa_feature_t feature;
4055 /* dedup feature version checks */
4056 if (prop == ZFS_PROP_DEDUP &&
4057 zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
4058 return (SET_ERROR(ENOTSUP));
4060 if (nvpair_value_uint64(pair, &intval) != 0)
4061 return (SET_ERROR(EINVAL));
4063 /* check prop value is enabled in features */
4064 feature = zio_checksum_to_feature(intval & ZIO_CHECKSUM_MASK);
4065 if (feature == SPA_FEATURE_NONE)
4068 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4071 * Salted checksums are not supported on root pools.
4073 if (spa_bootfs(spa) != 0 &&
4074 intval < ZIO_CHECKSUM_FUNCTIONS &&
4075 (zio_checksum_table[intval].ci_flags &
4076 ZCHECKSUM_FLAG_SALTED)) {
4077 spa_close(spa, FTAG);
4078 return (SET_ERROR(ERANGE));
4080 if (!spa_feature_is_enabled(spa, feature)) {
4081 spa_close(spa, FTAG);
4082 return (SET_ERROR(ENOTSUP));
4084 spa_close(spa, FTAG);
4089 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
4093 * Checks for a race condition to make sure we don't increment a feature flag
4097 zfs_prop_activate_feature_check(void *arg, dmu_tx_t *tx)
4099 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
4100 spa_feature_t *featurep = arg;
4102 if (!spa_feature_is_active(spa, *featurep))
4105 return (SET_ERROR(EBUSY));
4109 * The callback invoked on feature activation in the sync task caused by
4110 * zfs_prop_activate_feature.
4113 zfs_prop_activate_feature_sync(void *arg, dmu_tx_t *tx)
4115 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
4116 spa_feature_t *featurep = arg;
4118 spa_feature_incr(spa, *featurep, tx);
4122 * Activates a feature on a pool in response to a property setting. This
4123 * creates a new sync task which modifies the pool to reflect the feature
4127 zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature)
4131 /* EBUSY here indicates that the feature is already active */
4132 err = dsl_sync_task(spa_name(spa),
4133 zfs_prop_activate_feature_check, zfs_prop_activate_feature_sync,
4134 &feature, 2, ZFS_SPACE_CHECK_RESERVED);
4136 if (err != 0 && err != EBUSY)
4143 * Removes properties from the given props list that fail permission checks
4144 * needed to clear them and to restore them in case of a receive error. For each
4145 * property, make sure we have both set and inherit permissions.
4147 * Returns the first error encountered if any permission checks fail. If the
4148 * caller provides a non-NULL errlist, it also gives the complete list of names
4149 * of all the properties that failed a permission check along with the
4150 * corresponding error numbers. The caller is responsible for freeing the
4153 * If every property checks out successfully, zero is returned and the list
4154 * pointed at by errlist is NULL.
4157 zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
4160 nvpair_t *pair, *next_pair;
4167 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4169 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
4170 (void) strcpy(zc->zc_name, dataset);
4171 pair = nvlist_next_nvpair(props, NULL);
4172 while (pair != NULL) {
4173 next_pair = nvlist_next_nvpair(props, pair);
4175 (void) strcpy(zc->zc_value, nvpair_name(pair));
4176 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
4177 (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
4178 VERIFY(nvlist_remove_nvpair(props, pair) == 0);
4179 VERIFY(nvlist_add_int32(errors,
4180 zc->zc_value, err) == 0);
4184 kmem_free(zc, sizeof (zfs_cmd_t));
4186 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
4187 nvlist_free(errors);
4190 VERIFY(nvpair_value_int32(pair, &rv) == 0);
4193 if (errlist == NULL)
4194 nvlist_free(errors);
4202 propval_equals(nvpair_t *p1, nvpair_t *p2)
4204 if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
4205 /* dsl_prop_get_all_impl() format */
4207 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
4208 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4212 if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
4214 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
4215 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4219 if (nvpair_type(p1) != nvpair_type(p2))
4222 if (nvpair_type(p1) == DATA_TYPE_STRING) {
4223 char *valstr1, *valstr2;
4225 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
4226 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
4227 return (strcmp(valstr1, valstr2) == 0);
4229 uint64_t intval1, intval2;
4231 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
4232 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
4233 return (intval1 == intval2);
4238 * Remove properties from props if they are not going to change (as determined
4239 * by comparison with origprops). Remove them from origprops as well, since we
4240 * do not need to clear or restore properties that won't change.
4243 props_reduce(nvlist_t *props, nvlist_t *origprops)
4245 nvpair_t *pair, *next_pair;
4247 if (origprops == NULL)
4248 return; /* all props need to be received */
4250 pair = nvlist_next_nvpair(props, NULL);
4251 while (pair != NULL) {
4252 const char *propname = nvpair_name(pair);
4255 next_pair = nvlist_next_nvpair(props, pair);
4257 if ((nvlist_lookup_nvpair(origprops, propname,
4258 &match) != 0) || !propval_equals(pair, match))
4259 goto next; /* need to set received value */
4261 /* don't clear the existing received value */
4262 (void) nvlist_remove_nvpair(origprops, match);
4263 /* don't bother receiving the property */
4264 (void) nvlist_remove_nvpair(props, pair);
4271 * Extract properties that cannot be set PRIOR to the receipt of a dataset.
4272 * For example, refquota cannot be set until after the receipt of a dataset,
4273 * because in replication streams, an older/earlier snapshot may exceed the
4274 * refquota. We want to receive the older/earlier snapshot, but setting
4275 * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent
4276 * the older/earlier snapshot from being received (with EDQUOT).
4278 * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario.
4280 * libzfs will need to be judicious handling errors encountered by props
4281 * extracted by this function.
4284 extract_delay_props(nvlist_t *props)
4286 nvlist_t *delayprops;
4287 nvpair_t *nvp, *tmp;
4288 static const zfs_prop_t delayable[] = { ZFS_PROP_REFQUOTA, 0 };
4291 VERIFY(nvlist_alloc(&delayprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4293 for (nvp = nvlist_next_nvpair(props, NULL); nvp != NULL;
4294 nvp = nvlist_next_nvpair(props, nvp)) {
4296 * strcmp() is safe because zfs_prop_to_name() always returns
4299 for (i = 0; delayable[i] != 0; i++) {
4300 if (strcmp(zfs_prop_to_name(delayable[i]),
4301 nvpair_name(nvp)) == 0) {
4305 if (delayable[i] != 0) {
4306 tmp = nvlist_prev_nvpair(props, nvp);
4307 VERIFY(nvlist_add_nvpair(delayprops, nvp) == 0);
4308 VERIFY(nvlist_remove_nvpair(props, nvp) == 0);
4313 if (nvlist_empty(delayprops)) {
4314 nvlist_free(delayprops);
4317 return (delayprops);
4321 static boolean_t zfs_ioc_recv_inject_err;
4326 * zc_name name of containing filesystem
4327 * zc_nvlist_src{_size} nvlist of properties to apply
4328 * zc_value name of snapshot to create
4329 * zc_string name of clone origin (if DRR_FLAG_CLONE)
4330 * zc_cookie file descriptor to recv from
4331 * zc_begin_record the BEGIN record of the stream (not byteswapped)
4332 * zc_guid force flag
4333 * zc_cleanup_fd cleanup-on-exit file descriptor
4334 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
4335 * zc_resumable if data is incomplete assume sender will resume
4338 * zc_cookie number of bytes read
4339 * zc_nvlist_dst{_size} error for each unapplied received property
4340 * zc_obj zprop_errflags_t
4341 * zc_action_handle handle for this guid/ds mapping
4344 zfs_ioc_recv(zfs_cmd_t *zc)
4347 dmu_recv_cookie_t drc;
4348 boolean_t force = (boolean_t)zc->zc_guid;
4351 int props_error = 0;
4354 nvlist_t *props = NULL; /* sent properties */
4355 nvlist_t *origprops = NULL; /* existing properties */
4356 nvlist_t *delayprops = NULL; /* sent properties applied post-receive */
4357 char *origin = NULL;
4359 char tofs[ZFS_MAX_DATASET_NAME_LEN];
4360 cap_rights_t rights;
4361 boolean_t first_recvd_props = B_FALSE;
4363 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4364 strchr(zc->zc_value, '@') == NULL ||
4365 strchr(zc->zc_value, '%'))
4366 return (SET_ERROR(EINVAL));
4368 (void) strcpy(tofs, zc->zc_value);
4369 tosnap = strchr(tofs, '@');
4372 if (zc->zc_nvlist_src != 0 &&
4373 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
4374 zc->zc_iflags, &props)) != 0)
4381 fget_read(curthread, fd, cap_rights_init(&rights, CAP_PREAD), &fp);
4385 return (SET_ERROR(EBADF));
4388 errors = fnvlist_alloc();
4390 if (zc->zc_string[0])
4391 origin = zc->zc_string;
4393 error = dmu_recv_begin(tofs, tosnap,
4394 &zc->zc_begin_record, force, zc->zc_resumable, origin, &drc);
4399 * Set properties before we receive the stream so that they are applied
4400 * to the new data. Note that we must call dmu_recv_stream() if
4401 * dmu_recv_begin() succeeds.
4403 if (props != NULL && !drc.drc_newfs) {
4404 if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
4405 SPA_VERSION_RECVD_PROPS &&
4406 !dsl_prop_get_hasrecvd(tofs))
4407 first_recvd_props = B_TRUE;
4410 * If new received properties are supplied, they are to
4411 * completely replace the existing received properties, so stash
4412 * away the existing ones.
4414 if (dsl_prop_get_received(tofs, &origprops) == 0) {
4415 nvlist_t *errlist = NULL;
4417 * Don't bother writing a property if its value won't
4418 * change (and avoid the unnecessary security checks).
4420 * The first receive after SPA_VERSION_RECVD_PROPS is a
4421 * special case where we blow away all local properties
4424 if (!first_recvd_props)
4425 props_reduce(props, origprops);
4426 if (zfs_check_clearable(tofs, origprops, &errlist) != 0)
4427 (void) nvlist_merge(errors, errlist, 0);
4428 nvlist_free(errlist);
4430 if (clear_received_props(tofs, origprops,
4431 first_recvd_props ? NULL : props) != 0)
4432 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4434 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4438 if (props != NULL) {
4439 props_error = dsl_prop_set_hasrecvd(tofs);
4441 if (props_error == 0) {
4442 delayprops = extract_delay_props(props);
4443 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4449 error = dmu_recv_stream(&drc, fp, &off, zc->zc_cleanup_fd,
4450 &zc->zc_action_handle);
4453 zfsvfs_t *zfsvfs = NULL;
4455 if (getzfsvfs(tofs, &zfsvfs) == 0) {
4460 ds = dmu_objset_ds(zfsvfs->z_os);
4461 error = zfs_suspend_fs(zfsvfs);
4463 * If the suspend fails, then the recv_end will
4464 * likely also fail, and clean up after itself.
4466 end_err = dmu_recv_end(&drc, zfsvfs);
4468 error = zfs_resume_fs(zfsvfs, ds);
4469 error = error ? error : end_err;
4471 VFS_RELE(zfsvfs->z_vfs);
4473 vfs_unbusy(zfsvfs->z_vfs);
4476 error = dmu_recv_end(&drc, NULL);
4479 /* Set delayed properties now, after we're done receiving. */
4480 if (delayprops != NULL && error == 0) {
4481 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4482 delayprops, errors);
4486 if (delayprops != NULL) {
4488 * Merge delayed props back in with initial props, in case
4489 * we're DEBUG and zfs_ioc_recv_inject_err is set (which means
4490 * we have to make sure clear_received_props() includes
4491 * the delayed properties).
4493 * Since zfs_ioc_recv_inject_err is only in DEBUG kernels,
4494 * using ASSERT() will be just like a VERIFY.
4496 ASSERT(nvlist_merge(props, delayprops, 0) == 0);
4497 nvlist_free(delayprops);
4501 * Now that all props, initial and delayed, are set, report the prop
4502 * errors to the caller.
4504 if (zc->zc_nvlist_dst_size != 0 &&
4505 (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
4506 put_nvlist(zc, errors) != 0)) {
4508 * Caller made zc->zc_nvlist_dst less than the minimum expected
4509 * size or supplied an invalid address.
4511 props_error = SET_ERROR(EINVAL);
4514 zc->zc_cookie = off - fp->f_offset;
4515 if (off >= 0 && off <= MAXOFFSET_T)
4519 if (zfs_ioc_recv_inject_err) {
4520 zfs_ioc_recv_inject_err = B_FALSE;
4527 zvol_create_minors(tofs);
4531 * On error, restore the original props.
4533 if (error != 0 && props != NULL && !drc.drc_newfs) {
4534 if (clear_received_props(tofs, props, NULL) != 0) {
4536 * We failed to clear the received properties.
4537 * Since we may have left a $recvd value on the
4538 * system, we can't clear the $hasrecvd flag.
4540 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4541 } else if (first_recvd_props) {
4542 dsl_prop_unset_hasrecvd(tofs);
4545 if (origprops == NULL && !drc.drc_newfs) {
4546 /* We failed to stash the original properties. */
4547 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4551 * dsl_props_set() will not convert RECEIVED to LOCAL on or
4552 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
4553 * explictly if we're restoring local properties cleared in the
4554 * first new-style receive.
4556 if (origprops != NULL &&
4557 zfs_set_prop_nvlist(tofs, (first_recvd_props ?
4558 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
4559 origprops, NULL) != 0) {
4561 * We stashed the original properties but failed to
4564 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4569 nvlist_free(origprops);
4570 nvlist_free(errors);
4574 error = props_error;
4581 * zc_name name of snapshot to send
4582 * zc_cookie file descriptor to send stream to
4583 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
4584 * zc_sendobj objsetid of snapshot to send
4585 * zc_fromobj objsetid of incremental fromsnap (may be zero)
4586 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
4587 * output size in zc_objset_type.
4588 * zc_flags lzc_send_flags
4591 * zc_objset_type estimated size, if zc_guid is set
4594 zfs_ioc_send(zfs_cmd_t *zc)
4598 boolean_t estimate = (zc->zc_guid != 0);
4599 boolean_t embedok = (zc->zc_flags & 0x1);
4600 boolean_t large_block_ok = (zc->zc_flags & 0x2);
4601 boolean_t compressok = (zc->zc_flags & 0x4);
4603 if (zc->zc_obj != 0) {
4605 dsl_dataset_t *tosnap;
4607 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4611 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
4613 dsl_pool_rele(dp, FTAG);
4617 if (dsl_dir_is_clone(tosnap->ds_dir))
4619 dsl_dir_phys(tosnap->ds_dir)->dd_origin_obj;
4620 dsl_dataset_rele(tosnap, FTAG);
4621 dsl_pool_rele(dp, FTAG);
4626 dsl_dataset_t *tosnap;
4627 dsl_dataset_t *fromsnap = NULL;
4629 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4633 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
4635 dsl_pool_rele(dp, FTAG);
4639 if (zc->zc_fromobj != 0) {
4640 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
4643 dsl_dataset_rele(tosnap, FTAG);
4644 dsl_pool_rele(dp, FTAG);
4649 error = dmu_send_estimate(tosnap, fromsnap, compressok,
4650 &zc->zc_objset_type);
4652 if (fromsnap != NULL)
4653 dsl_dataset_rele(fromsnap, FTAG);
4654 dsl_dataset_rele(tosnap, FTAG);
4655 dsl_pool_rele(dp, FTAG);
4658 cap_rights_t rights;
4661 fp = getf(zc->zc_cookie);
4663 fget_write(curthread, zc->zc_cookie,
4664 cap_rights_init(&rights, CAP_WRITE), &fp);
4667 return (SET_ERROR(EBADF));
4670 error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
4671 zc->zc_fromobj, embedok, large_block_ok, compressok,
4673 zc->zc_cookie, fp->f_vnode, &off);
4675 zc->zc_cookie, fp, &off);
4678 if (off >= 0 && off <= MAXOFFSET_T)
4680 releasef(zc->zc_cookie);
4687 * zc_name name of snapshot on which to report progress
4688 * zc_cookie file descriptor of send stream
4691 * zc_cookie number of bytes written in send stream thus far
4694 zfs_ioc_send_progress(zfs_cmd_t *zc)
4698 dmu_sendarg_t *dsp = NULL;
4701 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4705 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
4707 dsl_pool_rele(dp, FTAG);
4711 mutex_enter(&ds->ds_sendstream_lock);
4714 * Iterate over all the send streams currently active on this dataset.
4715 * If there's one which matches the specified file descriptor _and_ the
4716 * stream was started by the current process, return the progress of
4719 for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
4720 dsp = list_next(&ds->ds_sendstreams, dsp)) {
4721 if (dsp->dsa_outfd == zc->zc_cookie &&
4722 dsp->dsa_proc == curproc)
4727 zc->zc_cookie = *(dsp->dsa_off);
4729 error = SET_ERROR(ENOENT);
4731 mutex_exit(&ds->ds_sendstream_lock);
4732 dsl_dataset_rele(ds, FTAG);
4733 dsl_pool_rele(dp, FTAG);
4738 zfs_ioc_inject_fault(zfs_cmd_t *zc)
4742 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
4743 &zc->zc_inject_record);
4746 zc->zc_guid = (uint64_t)id;
4752 zfs_ioc_clear_fault(zfs_cmd_t *zc)
4754 return (zio_clear_fault((int)zc->zc_guid));
4758 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
4760 int id = (int)zc->zc_guid;
4763 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
4764 &zc->zc_inject_record);
4772 zfs_ioc_error_log(zfs_cmd_t *zc)
4776 size_t count = (size_t)zc->zc_nvlist_dst_size;
4778 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
4781 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
4784 zc->zc_nvlist_dst_size = count;
4786 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
4788 spa_close(spa, FTAG);
4794 zfs_ioc_clear(zfs_cmd_t *zc)
4801 * On zpool clear we also fix up missing slogs
4803 mutex_enter(&spa_namespace_lock);
4804 spa = spa_lookup(zc->zc_name);
4806 mutex_exit(&spa_namespace_lock);
4807 return (SET_ERROR(EIO));
4809 if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
4810 /* we need to let spa_open/spa_load clear the chains */
4811 spa_set_log_state(spa, SPA_LOG_CLEAR);
4813 spa->spa_last_open_failed = 0;
4814 mutex_exit(&spa_namespace_lock);
4816 if (zc->zc_cookie & ZPOOL_NO_REWIND) {
4817 error = spa_open(zc->zc_name, &spa, FTAG);
4820 nvlist_t *config = NULL;
4822 if (zc->zc_nvlist_src == 0)
4823 return (SET_ERROR(EINVAL));
4825 if ((error = get_nvlist(zc->zc_nvlist_src,
4826 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
4827 error = spa_open_rewind(zc->zc_name, &spa, FTAG,
4829 if (config != NULL) {
4832 if ((err = put_nvlist(zc, config)) != 0)
4834 nvlist_free(config);
4836 nvlist_free(policy);
4843 spa_vdev_state_enter(spa, SCL_NONE);
4845 if (zc->zc_guid == 0) {
4848 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
4850 (void) spa_vdev_state_exit(spa, NULL, ENODEV);
4851 spa_close(spa, FTAG);
4852 return (SET_ERROR(ENODEV));
4856 vdev_clear(spa, vd);
4858 (void) spa_vdev_state_exit(spa, NULL, 0);
4861 * Resume any suspended I/Os.
4863 if (zio_resume(spa) != 0)
4864 error = SET_ERROR(EIO);
4866 spa_close(spa, FTAG);
4872 zfs_ioc_pool_reopen(zfs_cmd_t *zc)
4877 error = spa_open(zc->zc_name, &spa, FTAG);
4881 spa_vdev_state_enter(spa, SCL_NONE);
4884 * If a resilver is already in progress then set the
4885 * spa_scrub_reopen flag to B_TRUE so that we don't restart
4886 * the scan as a side effect of the reopen. Otherwise, let
4887 * vdev_open() decided if a resilver is required.
4889 spa->spa_scrub_reopen = dsl_scan_resilvering(spa->spa_dsl_pool);
4890 vdev_reopen(spa->spa_root_vdev);
4891 spa->spa_scrub_reopen = B_FALSE;
4893 (void) spa_vdev_state_exit(spa, NULL, 0);
4894 spa_close(spa, FTAG);
4899 * zc_name name of filesystem
4902 * zc_string name of conflicting snapshot, if there is one
4905 zfs_ioc_promote(zfs_cmd_t *zc)
4908 dsl_dataset_t *ds, *ods;
4909 char origin[ZFS_MAX_DATASET_NAME_LEN];
4913 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4917 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
4919 dsl_pool_rele(dp, FTAG);
4923 if (!dsl_dir_is_clone(ds->ds_dir)) {
4924 dsl_dataset_rele(ds, FTAG);
4925 dsl_pool_rele(dp, FTAG);
4926 return (SET_ERROR(EINVAL));
4929 error = dsl_dataset_hold_obj(dp,
4930 dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &ods);
4932 dsl_dataset_rele(ds, FTAG);
4933 dsl_pool_rele(dp, FTAG);
4937 dsl_dataset_name(ods, origin);
4938 dsl_dataset_rele(ods, FTAG);
4939 dsl_dataset_rele(ds, FTAG);
4940 dsl_pool_rele(dp, FTAG);
4943 * We don't need to unmount *all* the origin fs's snapshots, but
4946 cp = strchr(origin, '@');
4949 (void) dmu_objset_find(origin,
4950 zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
4951 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
4955 * Retrieve a single {user|group}{used|quota}@... property.
4958 * zc_name name of filesystem
4959 * zc_objset_type zfs_userquota_prop_t
4960 * zc_value domain name (eg. "S-1-234-567-89")
4961 * zc_guid RID/UID/GID
4964 * zc_cookie property value
4967 zfs_ioc_userspace_one(zfs_cmd_t *zc)
4972 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
4973 return (SET_ERROR(EINVAL));
4975 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4979 error = zfs_userspace_one(zfsvfs,
4980 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
4981 zfsvfs_rele(zfsvfs, FTAG);
4988 * zc_name name of filesystem
4989 * zc_cookie zap cursor
4990 * zc_objset_type zfs_userquota_prop_t
4991 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
4994 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
4995 * zc_cookie zap cursor
4998 zfs_ioc_userspace_many(zfs_cmd_t *zc)
5001 int bufsize = zc->zc_nvlist_dst_size;
5004 return (SET_ERROR(ENOMEM));
5006 int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
5010 void *buf = kmem_alloc(bufsize, KM_SLEEP);
5012 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
5013 buf, &zc->zc_nvlist_dst_size);
5016 error = ddi_copyout(buf,
5017 (void *)(uintptr_t)zc->zc_nvlist_dst,
5018 zc->zc_nvlist_dst_size, zc->zc_iflags);
5020 kmem_free(buf, bufsize);
5021 zfsvfs_rele(zfsvfs, FTAG);
5028 * zc_name name of filesystem
5034 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
5040 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
5041 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
5043 * If userused is not enabled, it may be because the
5044 * objset needs to be closed & reopened (to grow the
5045 * objset_phys_t). Suspend/resume the fs will do that.
5049 ds = dmu_objset_ds(zfsvfs->z_os);
5050 error = zfs_suspend_fs(zfsvfs);
5052 dmu_objset_refresh_ownership(zfsvfs->z_os,
5054 error = zfs_resume_fs(zfsvfs, ds);
5058 error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
5060 VFS_RELE(zfsvfs->z_vfs);
5062 vfs_unbusy(zfsvfs->z_vfs);
5065 /* XXX kind of reading contents without owning */
5066 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
5070 error = dmu_objset_userspace_upgrade(os);
5071 dmu_objset_rele(os, FTAG);
5079 * We don't want to have a hard dependency
5080 * against some special symbols in sharefs
5081 * nfs, and smbsrv. Determine them if needed when
5082 * the first file system is shared.
5083 * Neither sharefs, nfs or smbsrv are unloadable modules.
5085 int (*znfsexport_fs)(void *arg);
5086 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
5087 int (*zsmbexport_fs)(void *arg, boolean_t add_share);
5089 int zfs_nfsshare_inited;
5090 int zfs_smbshare_inited;
5092 ddi_modhandle_t nfs_mod;
5093 ddi_modhandle_t sharefs_mod;
5094 ddi_modhandle_t smbsrv_mod;
5095 #endif /* illumos */
5096 kmutex_t zfs_share_lock;
5104 ASSERT(MUTEX_HELD(&zfs_share_lock));
5105 /* Both NFS and SMB shares also require sharetab support. */
5106 if (sharefs_mod == NULL && ((sharefs_mod =
5107 ddi_modopen("fs/sharefs",
5108 KRTLD_MODE_FIRST, &error)) == NULL)) {
5109 return (SET_ERROR(ENOSYS));
5111 if (zshare_fs == NULL && ((zshare_fs =
5112 (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
5113 ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
5114 return (SET_ERROR(ENOSYS));
5118 #endif /* illumos */
5121 zfs_ioc_share(zfs_cmd_t *zc)
5127 switch (zc->zc_share.z_sharetype) {
5129 case ZFS_UNSHARE_NFS:
5130 if (zfs_nfsshare_inited == 0) {
5131 mutex_enter(&zfs_share_lock);
5132 if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
5133 KRTLD_MODE_FIRST, &error)) == NULL)) {
5134 mutex_exit(&zfs_share_lock);
5135 return (SET_ERROR(ENOSYS));
5137 if (znfsexport_fs == NULL &&
5138 ((znfsexport_fs = (int (*)(void *))
5140 "nfs_export", &error)) == NULL)) {
5141 mutex_exit(&zfs_share_lock);
5142 return (SET_ERROR(ENOSYS));
5144 error = zfs_init_sharefs();
5146 mutex_exit(&zfs_share_lock);
5147 return (SET_ERROR(ENOSYS));
5149 zfs_nfsshare_inited = 1;
5150 mutex_exit(&zfs_share_lock);
5154 case ZFS_UNSHARE_SMB:
5155 if (zfs_smbshare_inited == 0) {
5156 mutex_enter(&zfs_share_lock);
5157 if (smbsrv_mod == NULL && ((smbsrv_mod =
5158 ddi_modopen("drv/smbsrv",
5159 KRTLD_MODE_FIRST, &error)) == NULL)) {
5160 mutex_exit(&zfs_share_lock);
5161 return (SET_ERROR(ENOSYS));
5163 if (zsmbexport_fs == NULL && ((zsmbexport_fs =
5164 (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
5165 "smb_server_share", &error)) == NULL)) {
5166 mutex_exit(&zfs_share_lock);
5167 return (SET_ERROR(ENOSYS));
5169 error = zfs_init_sharefs();
5171 mutex_exit(&zfs_share_lock);
5172 return (SET_ERROR(ENOSYS));
5174 zfs_smbshare_inited = 1;
5175 mutex_exit(&zfs_share_lock);
5179 return (SET_ERROR(EINVAL));
5182 switch (zc->zc_share.z_sharetype) {
5184 case ZFS_UNSHARE_NFS:
5186 znfsexport_fs((void *)
5187 (uintptr_t)zc->zc_share.z_exportdata))
5191 case ZFS_UNSHARE_SMB:
5192 if (error = zsmbexport_fs((void *)
5193 (uintptr_t)zc->zc_share.z_exportdata,
5194 zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
5201 opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
5202 zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
5203 SHAREFS_ADD : SHAREFS_REMOVE;
5206 * Add or remove share from sharetab
5208 error = zshare_fs(opcode,
5209 (void *)(uintptr_t)zc->zc_share.z_sharedata,
5210 zc->zc_share.z_sharemax);
5214 #else /* !illumos */
5216 #endif /* illumos */
5219 ace_t full_access[] = {
5220 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
5225 * zc_name name of containing filesystem
5226 * zc_obj object # beyond which we want next in-use object #
5229 * zc_obj next in-use object #
5232 zfs_ioc_next_obj(zfs_cmd_t *zc)
5234 objset_t *os = NULL;
5237 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
5241 error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
5242 dsl_dataset_phys(os->os_dsl_dataset)->ds_prev_snap_txg);
5244 dmu_objset_rele(os, FTAG);
5250 * zc_name name of filesystem
5251 * zc_value prefix name for snapshot
5252 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
5255 * zc_value short name of new snapshot
5258 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
5265 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
5269 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
5270 (u_longlong_t)ddi_get_lbolt64());
5271 hold_name = kmem_asprintf("%%%s", zc->zc_value);
5273 error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
5276 (void) strcpy(zc->zc_value, snap_name);
5279 zfs_onexit_fd_rele(zc->zc_cleanup_fd);
5285 * zc_name name of "to" snapshot
5286 * zc_value name of "from" snapshot
5287 * zc_cookie file descriptor to write diff data on
5290 * dmu_diff_record_t's to the file descriptor
5293 zfs_ioc_diff(zfs_cmd_t *zc)
5296 cap_rights_t rights;
5301 fp = getf(zc->zc_cookie);
5303 fget_write(curthread, zc->zc_cookie,
5304 cap_rights_init(&rights, CAP_WRITE), &fp);
5307 return (SET_ERROR(EBADF));
5312 error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off);
5314 error = dmu_diff(zc->zc_name, zc->zc_value, fp, &off);
5317 if (off >= 0 && off <= MAXOFFSET_T)
5319 releasef(zc->zc_cookie);
5326 * Remove all ACL files in shares dir
5329 zfs_smb_acl_purge(znode_t *dzp)
5332 zap_attribute_t zap;
5333 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
5336 for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
5337 (error = zap_cursor_retrieve(&zc, &zap)) == 0;
5338 zap_cursor_advance(&zc)) {
5339 if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
5343 zap_cursor_fini(&zc);
5346 #endif /* illumos */
5349 zfs_ioc_smb_acl(zfs_cmd_t *zc)
5354 vnode_t *resourcevp = NULL;
5363 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
5364 NO_FOLLOW, NULL, &vp)) != 0)
5367 /* Now make sure mntpnt and dataset are ZFS */
5369 if (strcmp(vp->v_vfsp->mnt_stat.f_fstypename, "zfs") != 0 ||
5370 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
5371 zc->zc_name) != 0)) {
5373 return (SET_ERROR(EINVAL));
5377 zfsvfs = dzp->z_zfsvfs;
5381 * Create share dir if its missing.
5383 mutex_enter(&zfsvfs->z_lock);
5384 if (zfsvfs->z_shares_dir == 0) {
5387 tx = dmu_tx_create(zfsvfs->z_os);
5388 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
5390 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
5391 error = dmu_tx_assign(tx, TXG_WAIT);
5395 error = zfs_create_share_dir(zfsvfs, tx);
5399 mutex_exit(&zfsvfs->z_lock);
5405 mutex_exit(&zfsvfs->z_lock);
5407 ASSERT(zfsvfs->z_shares_dir);
5408 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
5414 switch (zc->zc_cookie) {
5415 case ZFS_SMB_ACL_ADD:
5416 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
5417 vattr.va_type = VREG;
5418 vattr.va_mode = S_IFREG|0777;
5422 vsec.vsa_mask = VSA_ACE;
5423 vsec.vsa_aclentp = &full_access;
5424 vsec.vsa_aclentsz = sizeof (full_access);
5425 vsec.vsa_aclcnt = 1;
5427 error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
5428 &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
5430 VN_RELE(resourcevp);
5433 case ZFS_SMB_ACL_REMOVE:
5434 error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
5438 case ZFS_SMB_ACL_RENAME:
5439 if ((error = get_nvlist(zc->zc_nvlist_src,
5440 zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
5442 VN_RELE(ZTOV(sharedir));
5446 if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
5447 nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
5450 VN_RELE(ZTOV(sharedir));
5452 nvlist_free(nvlist);
5455 error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
5457 nvlist_free(nvlist);
5460 case ZFS_SMB_ACL_PURGE:
5461 error = zfs_smb_acl_purge(sharedir);
5465 error = SET_ERROR(EINVAL);
5470 VN_RELE(ZTOV(sharedir));
5475 #else /* !illumos */
5476 return (EOPNOTSUPP);
5477 #endif /* illumos */
5482 * "holds" -> { snapname -> holdname (string), ... }
5483 * (optional) "cleanup_fd" -> fd (int32)
5487 * snapname -> error value (int32)
5493 zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
5497 int cleanup_fd = -1;
5501 error = nvlist_lookup_nvlist(args, "holds", &holds);
5503 return (SET_ERROR(EINVAL));
5505 /* make sure the user didn't pass us any invalid (empty) tags */
5506 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
5507 pair = nvlist_next_nvpair(holds, pair)) {
5510 error = nvpair_value_string(pair, &htag);
5512 return (SET_ERROR(error));
5514 if (strlen(htag) == 0)
5515 return (SET_ERROR(EINVAL));
5518 if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
5519 error = zfs_onexit_fd_hold(cleanup_fd, &minor);
5524 error = dsl_dataset_user_hold(holds, minor, errlist);
5526 zfs_onexit_fd_rele(cleanup_fd);
5531 * innvl is not used.
5534 * holdname -> time added (uint64 seconds since epoch)
5540 zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
5542 return (dsl_dataset_get_holds(snapname, outnvl));
5547 * snapname -> { holdname, ... }
5552 * snapname -> error value (int32)
5558 zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
5560 return (dsl_dataset_user_release(holds, errlist));
5565 * zc_name name of new filesystem or snapshot
5566 * zc_value full name of old snapshot
5569 * zc_cookie space in bytes
5570 * zc_objset_type compressed space in bytes
5571 * zc_perm_action uncompressed space in bytes
5574 zfs_ioc_space_written(zfs_cmd_t *zc)
5578 dsl_dataset_t *new, *old;
5580 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5583 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
5585 dsl_pool_rele(dp, FTAG);
5588 error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
5590 dsl_dataset_rele(new, FTAG);
5591 dsl_pool_rele(dp, FTAG);
5595 error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
5596 &zc->zc_objset_type, &zc->zc_perm_action);
5597 dsl_dataset_rele(old, FTAG);
5598 dsl_dataset_rele(new, FTAG);
5599 dsl_pool_rele(dp, FTAG);
5605 * "firstsnap" -> snapshot name
5609 * "used" -> space in bytes
5610 * "compressed" -> compressed space in bytes
5611 * "uncompressed" -> uncompressed space in bytes
5615 zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
5619 dsl_dataset_t *new, *old;
5621 uint64_t used, comp, uncomp;
5623 if (nvlist_lookup_string(innvl, "firstsnap", &firstsnap) != 0)
5624 return (SET_ERROR(EINVAL));
5626 error = dsl_pool_hold(lastsnap, FTAG, &dp);
5630 error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
5631 if (error == 0 && !new->ds_is_snapshot) {
5632 dsl_dataset_rele(new, FTAG);
5633 error = SET_ERROR(EINVAL);
5636 dsl_pool_rele(dp, FTAG);
5639 error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
5640 if (error == 0 && !old->ds_is_snapshot) {
5641 dsl_dataset_rele(old, FTAG);
5642 error = SET_ERROR(EINVAL);
5645 dsl_dataset_rele(new, FTAG);
5646 dsl_pool_rele(dp, FTAG);
5650 error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
5651 dsl_dataset_rele(old, FTAG);
5652 dsl_dataset_rele(new, FTAG);
5653 dsl_pool_rele(dp, FTAG);
5654 fnvlist_add_uint64(outnvl, "used", used);
5655 fnvlist_add_uint64(outnvl, "compressed", comp);
5656 fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
5661 zfs_ioc_jail(zfs_cmd_t *zc)
5664 return (zone_dataset_attach(curthread->td_ucred, zc->zc_name,
5665 (int)zc->zc_jailid));
5669 zfs_ioc_unjail(zfs_cmd_t *zc)
5672 return (zone_dataset_detach(curthread->td_ucred, zc->zc_name,
5673 (int)zc->zc_jailid));
5678 * "fd" -> file descriptor to write stream to (int32)
5679 * (optional) "fromsnap" -> full snap name to send an incremental from
5680 * (optional) "largeblockok" -> (value ignored)
5681 * indicates that blocks > 128KB are permitted
5682 * (optional) "embedok" -> (value ignored)
5683 * presence indicates DRR_WRITE_EMBEDDED records are permitted
5684 * (optional) "compressok" -> (value ignored)
5685 * presence indicates compressed DRR_WRITE records are permitted
5686 * (optional) "resume_object" and "resume_offset" -> (uint64)
5687 * if present, resume send stream from specified object and offset.
5694 zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5696 cap_rights_t rights;
5700 char *fromname = NULL;
5702 boolean_t largeblockok;
5704 boolean_t compressok;
5705 uint64_t resumeobj = 0;
5706 uint64_t resumeoff = 0;
5708 error = nvlist_lookup_int32(innvl, "fd", &fd);
5710 return (SET_ERROR(EINVAL));
5712 (void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
5714 largeblockok = nvlist_exists(innvl, "largeblockok");
5715 embedok = nvlist_exists(innvl, "embedok");
5716 compressok = nvlist_exists(innvl, "compressok");
5718 (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
5719 (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
5722 file_t *fp = getf(fd);
5724 fget_write(curthread, fd, cap_rights_init(&rights, CAP_WRITE), &fp);
5727 return (SET_ERROR(EBADF));
5730 error = dmu_send(snapname, fromname, embedok, largeblockok, compressok,
5732 fd, resumeobj, resumeoff, fp->f_vnode, &off);
5734 fd, resumeobj, resumeoff, fp, &off);
5738 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5749 * Determine approximately how large a zfs send stream will be -- the number
5750 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
5753 * (optional) "from" -> full snap or bookmark name to send an incremental
5755 * (optional) "largeblockok" -> (value ignored)
5756 * indicates that blocks > 128KB are permitted
5757 * (optional) "embedok" -> (value ignored)
5758 * presence indicates DRR_WRITE_EMBEDDED records are permitted
5759 * (optional) "compressok" -> (value ignored)
5760 * presence indicates compressed DRR_WRITE records are permitted
5764 * "space" -> bytes of space (uint64)
5768 zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5771 dsl_dataset_t *tosnap;
5774 /* LINTED E_FUNC_SET_NOT_USED */
5775 boolean_t largeblockok;
5776 /* LINTED E_FUNC_SET_NOT_USED */
5778 boolean_t compressok;
5781 error = dsl_pool_hold(snapname, FTAG, &dp);
5785 error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
5787 dsl_pool_rele(dp, FTAG);
5791 largeblockok = nvlist_exists(innvl, "largeblockok");
5792 embedok = nvlist_exists(innvl, "embedok");
5793 compressok = nvlist_exists(innvl, "compressok");
5795 error = nvlist_lookup_string(innvl, "from", &fromname);
5797 if (strchr(fromname, '@') != NULL) {
5799 * If from is a snapshot, hold it and use the more
5800 * efficient dmu_send_estimate to estimate send space
5801 * size using deadlists.
5803 dsl_dataset_t *fromsnap;
5804 error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
5807 error = dmu_send_estimate(tosnap, fromsnap, compressok,
5809 dsl_dataset_rele(fromsnap, FTAG);
5810 } else if (strchr(fromname, '#') != NULL) {
5812 * If from is a bookmark, fetch the creation TXG of the
5813 * snapshot it was created from and use that to find
5814 * blocks that were born after it.
5816 zfs_bookmark_phys_t frombm;
5818 error = dsl_bookmark_lookup(dp, fromname, tosnap,
5822 error = dmu_send_estimate_from_txg(tosnap,
5823 frombm.zbm_creation_txg, compressok, &space);
5826 * from is not properly formatted as a snapshot or
5829 error = SET_ERROR(EINVAL);
5833 // If estimating the size of a full send, use dmu_send_estimate
5834 error = dmu_send_estimate(tosnap, NULL, compressok, &space);
5837 fnvlist_add_uint64(outnvl, "space", space);
5840 dsl_dataset_rele(tosnap, FTAG);
5841 dsl_pool_rele(dp, FTAG);
5845 static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
5848 zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5849 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
5850 boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
5852 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
5854 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
5855 ASSERT3U(ioc, <, ZFS_IOC_LAST);
5856 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
5857 ASSERT3P(vec->zvec_func, ==, NULL);
5859 vec->zvec_legacy_func = func;
5860 vec->zvec_secpolicy = secpolicy;
5861 vec->zvec_namecheck = namecheck;
5862 vec->zvec_allow_log = log_history;
5863 vec->zvec_pool_check = pool_check;
5867 * See the block comment at the beginning of this file for details on
5868 * each argument to this function.
5871 zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
5872 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
5873 zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
5874 boolean_t allow_log)
5876 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
5878 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
5879 ASSERT3U(ioc, <, ZFS_IOC_LAST);
5880 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
5881 ASSERT3P(vec->zvec_func, ==, NULL);
5883 /* if we are logging, the name must be valid */
5884 ASSERT(!allow_log || namecheck != NO_NAME);
5886 vec->zvec_name = name;
5887 vec->zvec_func = func;
5888 vec->zvec_secpolicy = secpolicy;
5889 vec->zvec_namecheck = namecheck;
5890 vec->zvec_pool_check = pool_check;
5891 vec->zvec_smush_outnvlist = smush_outnvlist;
5892 vec->zvec_allow_log = allow_log;
5896 zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5897 zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
5898 zfs_ioc_poolcheck_t pool_check)
5900 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5901 POOL_NAME, log_history, pool_check);
5905 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5906 zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
5908 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5909 DATASET_NAME, B_FALSE, pool_check);
5913 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
5915 zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
5916 POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5920 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5921 zfs_secpolicy_func_t *secpolicy)
5923 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5924 NO_NAME, B_FALSE, POOL_CHECK_NONE);
5928 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
5929 zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
5931 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5932 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
5936 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
5938 zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
5939 zfs_secpolicy_read);
5943 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5944 zfs_secpolicy_func_t *secpolicy)
5946 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5947 DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5951 zfs_ioctl_init(void)
5953 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
5954 zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
5955 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5957 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
5958 zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
5959 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE);
5961 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
5962 zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
5963 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5965 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
5966 zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
5967 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5969 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
5970 zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
5971 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5973 zfs_ioctl_register("create", ZFS_IOC_CREATE,
5974 zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
5975 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5977 zfs_ioctl_register("clone", ZFS_IOC_CLONE,
5978 zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
5979 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5981 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
5982 zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
5983 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5985 zfs_ioctl_register("hold", ZFS_IOC_HOLD,
5986 zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
5987 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5988 zfs_ioctl_register("release", ZFS_IOC_RELEASE,
5989 zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
5990 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5992 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
5993 zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
5994 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5996 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
5997 zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
5998 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
6000 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK,
6001 zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME,
6002 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6004 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS,
6005 zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME,
6006 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
6008 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS,
6009 zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks,
6011 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6013 /* IOCTLS that use the legacy function signature */
6015 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
6016 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
6018 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
6019 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
6020 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
6022 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
6023 zfs_ioc_pool_upgrade);
6024 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
6026 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
6027 zfs_ioc_vdev_remove);
6028 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
6029 zfs_ioc_vdev_set_state);
6030 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
6031 zfs_ioc_vdev_attach);
6032 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
6033 zfs_ioc_vdev_detach);
6034 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
6035 zfs_ioc_vdev_setpath);
6036 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
6037 zfs_ioc_vdev_setfru);
6038 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
6039 zfs_ioc_pool_set_props);
6040 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
6041 zfs_ioc_vdev_split);
6042 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
6043 zfs_ioc_pool_reguid);
6045 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
6046 zfs_ioc_pool_configs, zfs_secpolicy_none);
6047 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
6048 zfs_ioc_pool_tryimport, zfs_secpolicy_config);
6049 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
6050 zfs_ioc_inject_fault, zfs_secpolicy_inject);
6051 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
6052 zfs_ioc_clear_fault, zfs_secpolicy_inject);
6053 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
6054 zfs_ioc_inject_list_next, zfs_secpolicy_inject);
6057 * pool destroy, and export don't log the history as part of
6058 * zfsdev_ioctl, but rather zfs_ioc_pool_export
6059 * does the logging of those commands.
6061 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
6062 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
6063 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
6064 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
6066 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
6067 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
6068 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
6069 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
6071 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
6072 zfs_secpolicy_inject, B_FALSE, POOL_CHECK_NONE);
6073 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
6074 zfs_ioc_dsobj_to_dsname,
6075 zfs_secpolicy_diff, B_FALSE, POOL_CHECK_NONE);
6076 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
6077 zfs_ioc_pool_get_history,
6078 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
6080 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
6081 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
6083 zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
6084 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
6085 zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
6086 zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
6088 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
6089 zfs_ioc_space_written);
6090 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
6091 zfs_ioc_objset_recvd_props);
6092 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
6094 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
6096 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
6097 zfs_ioc_objset_stats);
6098 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
6099 zfs_ioc_objset_zplprops);
6100 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
6101 zfs_ioc_dataset_list_next);
6102 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
6103 zfs_ioc_snapshot_list_next);
6104 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
6105 zfs_ioc_send_progress);
6107 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
6108 zfs_ioc_diff, zfs_secpolicy_diff);
6109 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
6110 zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
6111 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
6112 zfs_ioc_obj_to_path, zfs_secpolicy_diff);
6113 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
6114 zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
6115 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
6116 zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
6117 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
6118 zfs_ioc_send, zfs_secpolicy_send);
6120 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
6121 zfs_secpolicy_none);
6122 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
6123 zfs_secpolicy_destroy);
6124 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
6125 zfs_secpolicy_rename);
6126 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
6127 zfs_secpolicy_recv);
6128 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
6129 zfs_secpolicy_promote);
6130 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
6131 zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
6132 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
6133 zfs_secpolicy_set_fsacl);
6135 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
6136 zfs_secpolicy_share, POOL_CHECK_NONE);
6137 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
6138 zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
6139 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
6140 zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
6141 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6142 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
6143 zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
6144 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6147 zfs_ioctl_register_dataset_nolog(ZFS_IOC_JAIL, zfs_ioc_jail,
6148 zfs_secpolicy_config, POOL_CHECK_NONE);
6149 zfs_ioctl_register_dataset_nolog(ZFS_IOC_UNJAIL, zfs_ioc_unjail,
6150 zfs_secpolicy_config, POOL_CHECK_NONE);
6151 zfs_ioctl_register("fbsd_nextboot", ZFS_IOC_NEXTBOOT,
6152 zfs_ioc_nextboot, zfs_secpolicy_config, NO_NAME,
6153 POOL_CHECK_NONE, B_FALSE, B_FALSE);
6158 pool_status_check(const char *name, zfs_ioc_namecheck_t type,
6159 zfs_ioc_poolcheck_t check)
6164 ASSERT(type == POOL_NAME || type == DATASET_NAME);
6166 if (check & POOL_CHECK_NONE)
6169 error = spa_open(name, &spa, FTAG);
6171 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
6172 error = SET_ERROR(EAGAIN);
6173 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
6174 error = SET_ERROR(EROFS);
6175 spa_close(spa, FTAG);
6181 * Find a free minor number.
6184 zfsdev_minor_alloc(void)
6186 static minor_t last_minor;
6189 ASSERT(MUTEX_HELD(&spa_namespace_lock));
6191 for (m = last_minor + 1; m != last_minor; m++) {
6192 if (m > ZFSDEV_MAX_MINOR)
6194 if (ddi_get_soft_state(zfsdev_state, m) == NULL) {
6204 zfs_ctldev_init(struct cdev *devp)
6207 zfs_soft_state_t *zs;
6209 ASSERT(MUTEX_HELD(&spa_namespace_lock));
6211 minor = zfsdev_minor_alloc();
6213 return (SET_ERROR(ENXIO));
6215 if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS)
6216 return (SET_ERROR(EAGAIN));
6218 devfs_set_cdevpriv((void *)(uintptr_t)minor, zfsdev_close);
6220 zs = ddi_get_soft_state(zfsdev_state, minor);
6221 zs->zss_type = ZSST_CTLDEV;
6222 zfs_onexit_init((zfs_onexit_t **)&zs->zss_data);
6228 zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor)
6230 ASSERT(MUTEX_HELD(&spa_namespace_lock));
6232 zfs_onexit_destroy(zo);
6233 ddi_soft_state_free(zfsdev_state, minor);
6237 zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which)
6239 zfs_soft_state_t *zp;
6241 zp = ddi_get_soft_state(zfsdev_state, minor);
6242 if (zp == NULL || zp->zss_type != which)
6245 return (zp->zss_data);
6249 zfsdev_open(struct cdev *devp, int flag, int mode, struct thread *td)
6254 if (getminor(*devp) != 0)
6255 return (zvol_open(devp, flag, otyp, cr));
6258 /* This is the control device. Allocate a new minor if requested. */
6260 mutex_enter(&spa_namespace_lock);
6261 error = zfs_ctldev_init(devp);
6262 mutex_exit(&spa_namespace_lock);
6269 zfsdev_close(void *data)
6272 minor_t minor = (minor_t)(uintptr_t)data;
6277 mutex_enter(&spa_namespace_lock);
6278 zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV);
6280 mutex_exit(&spa_namespace_lock);
6283 zfs_ctldev_destroy(zo, minor);
6284 mutex_exit(&spa_namespace_lock);
6288 zfsdev_ioctl(struct cdev *dev, u_long zcmd, caddr_t arg, int flag,
6295 minor_t minor = getminor(dev);
6297 zfs_iocparm_t *zc_iocparm;
6298 int cflag, cmd, oldvecnum;
6299 boolean_t newioc, compat;
6300 void *compat_zc = NULL;
6301 cred_t *cr = td->td_ucred;
6303 const zfs_ioc_vec_t *vec;
6304 char *saved_poolname = NULL;
6305 nvlist_t *innvl = NULL;
6307 cflag = ZFS_CMD_COMPAT_NONE;
6309 newioc = B_TRUE; /* "new" style (zfs_iocparm_t) ioctl */
6311 len = IOCPARM_LEN(zcmd);
6312 vecnum = cmd = zcmd & 0xff;
6315 * Check if we are talking to supported older binaries
6316 * and translate zfs_cmd if necessary
6318 if (len != sizeof(zfs_iocparm_t)) {
6325 case sizeof(zfs_cmd_zcmd_t):
6326 cflag = ZFS_CMD_COMPAT_LZC;
6328 case sizeof(zfs_cmd_deadman_t):
6329 cflag = ZFS_CMD_COMPAT_DEADMAN;
6331 case sizeof(zfs_cmd_v28_t):
6332 cflag = ZFS_CMD_COMPAT_V28;
6334 case sizeof(zfs_cmd_v15_t):
6335 cflag = ZFS_CMD_COMPAT_V15;
6336 vecnum = zfs_ioctl_v15_to_v28[cmd];
6339 * Return without further handling
6340 * if the command is blacklisted.
6342 if (vecnum == ZFS_IOC_COMPAT_PASS)
6344 else if (vecnum == ZFS_IOC_COMPAT_FAIL)
6353 vecnum = cmd - ZFS_IOC_FIRST;
6354 ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
6357 if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
6358 return (SET_ERROR(EINVAL));
6359 vec = &zfs_ioc_vec[vecnum];
6361 zc = kmem_zalloc(sizeof(zfs_cmd_t), KM_SLEEP);
6364 error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
6366 error = SET_ERROR(EFAULT);
6369 #else /* !illumos */
6370 bzero(zc, sizeof(zfs_cmd_t));
6373 zc_iocparm = (void *)arg;
6375 switch (zc_iocparm->zfs_ioctl_version) {
6376 case ZFS_IOCVER_CURRENT:
6377 if (zc_iocparm->zfs_cmd_size != sizeof(zfs_cmd_t)) {
6378 error = SET_ERROR(EINVAL);
6382 case ZFS_IOCVER_INLANES:
6383 if (zc_iocparm->zfs_cmd_size != sizeof(zfs_cmd_inlanes_t)) {
6384 error = SET_ERROR(EFAULT);
6388 cflag = ZFS_CMD_COMPAT_INLANES;
6390 case ZFS_IOCVER_RESUME:
6391 if (zc_iocparm->zfs_cmd_size != sizeof(zfs_cmd_resume_t)) {
6392 error = SET_ERROR(EFAULT);
6396 cflag = ZFS_CMD_COMPAT_RESUME;
6398 case ZFS_IOCVER_EDBP:
6399 if (zc_iocparm->zfs_cmd_size != sizeof(zfs_cmd_edbp_t)) {
6400 error = SET_ERROR(EFAULT);
6404 cflag = ZFS_CMD_COMPAT_EDBP;
6406 case ZFS_IOCVER_ZCMD:
6407 if (zc_iocparm->zfs_cmd_size > sizeof(zfs_cmd_t) ||
6408 zc_iocparm->zfs_cmd_size < sizeof(zfs_cmd_zcmd_t)) {
6409 error = SET_ERROR(EFAULT);
6413 cflag = ZFS_CMD_COMPAT_ZCMD;
6416 error = SET_ERROR(EINVAL);
6422 ASSERT(sizeof(zfs_cmd_t) >= zc_iocparm->zfs_cmd_size);
6423 compat_zc = kmem_zalloc(sizeof(zfs_cmd_t), KM_SLEEP);
6424 bzero(compat_zc, sizeof(zfs_cmd_t));
6426 error = ddi_copyin((void *)(uintptr_t)zc_iocparm->zfs_cmd,
6427 compat_zc, zc_iocparm->zfs_cmd_size, flag);
6429 error = SET_ERROR(EFAULT);
6433 error = ddi_copyin((void *)(uintptr_t)zc_iocparm->zfs_cmd,
6434 zc, zc_iocparm->zfs_cmd_size, flag);
6436 error = SET_ERROR(EFAULT);
6444 ASSERT(compat_zc != NULL);
6445 zfs_cmd_compat_get(zc, compat_zc, cflag);
6447 ASSERT(compat_zc == NULL);
6448 zfs_cmd_compat_get(zc, arg, cflag);
6451 error = zfs_ioctl_compat_pre(zc, &vecnum, cflag);
6454 if (oldvecnum != vecnum)
6455 vec = &zfs_ioc_vec[vecnum];
6457 #endif /* !illumos */
6459 zc->zc_iflags = flag & FKIOCTL;
6460 if (zc->zc_nvlist_src_size != 0) {
6461 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
6462 zc->zc_iflags, &innvl);
6467 /* rewrite innvl for backwards compatibility */
6469 innvl = zfs_ioctl_compat_innvl(zc, innvl, vecnum, cflag);
6472 * Ensure that all pool/dataset names are valid before we pass down to
6475 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
6476 switch (vec->zvec_namecheck) {
6478 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
6479 error = SET_ERROR(EINVAL);
6481 error = pool_status_check(zc->zc_name,
6482 vec->zvec_namecheck, vec->zvec_pool_check);
6486 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
6487 error = SET_ERROR(EINVAL);
6489 error = pool_status_check(zc->zc_name,
6490 vec->zvec_namecheck, vec->zvec_pool_check);
6498 error = vec->zvec_secpolicy(zc, innvl, cr);
6503 /* legacy ioctls can modify zc_name */
6504 len = strcspn(zc->zc_name, "/@#") + 1;
6505 saved_poolname = kmem_alloc(len, KM_SLEEP);
6506 (void) strlcpy(saved_poolname, zc->zc_name, len);
6508 if (vec->zvec_func != NULL) {
6512 nvlist_t *lognv = NULL;
6514 ASSERT(vec->zvec_legacy_func == NULL);
6517 * Add the innvl to the lognv before calling the func,
6518 * in case the func changes the innvl.
6520 if (vec->zvec_allow_log) {
6521 lognv = fnvlist_alloc();
6522 fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
6524 if (!nvlist_empty(innvl)) {
6525 fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
6530 outnvl = fnvlist_alloc();
6531 error = vec->zvec_func(zc->zc_name, innvl, outnvl);
6533 if (error == 0 && vec->zvec_allow_log &&
6534 spa_open(zc->zc_name, &spa, FTAG) == 0) {
6535 if (!nvlist_empty(outnvl)) {
6536 fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL,
6539 (void) spa_history_log_nvl(spa, lognv);
6540 spa_close(spa, FTAG);
6542 fnvlist_free(lognv);
6544 /* rewrite outnvl for backwards compatibility */
6546 outnvl = zfs_ioctl_compat_outnvl(zc, outnvl, vecnum,
6549 if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
6551 if (vec->zvec_smush_outnvlist) {
6552 smusherror = nvlist_smush(outnvl,
6553 zc->zc_nvlist_dst_size);
6555 if (smusherror == 0)
6556 puterror = put_nvlist(zc, outnvl);
6562 nvlist_free(outnvl);
6564 error = vec->zvec_legacy_func(zc);
6571 rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
6572 if (error == 0 && rc != 0)
6573 error = SET_ERROR(EFAULT);
6576 zfs_ioctl_compat_post(zc, cmd, cflag);
6578 ASSERT(compat_zc != NULL);
6579 ASSERT(sizeof(zfs_cmd_t) >= zc_iocparm->zfs_cmd_size);
6581 zfs_cmd_compat_put(zc, compat_zc, vecnum, cflag);
6582 rc = ddi_copyout(compat_zc,
6583 (void *)(uintptr_t)zc_iocparm->zfs_cmd,
6584 zc_iocparm->zfs_cmd_size, flag);
6585 if (error == 0 && rc != 0)
6586 error = SET_ERROR(EFAULT);
6587 kmem_free(compat_zc, sizeof (zfs_cmd_t));
6589 zfs_cmd_compat_put(zc, arg, vecnum, cflag);
6594 rc = ddi_copyout(zc, (void *)(uintptr_t)zc_iocparm->zfs_cmd,
6595 sizeof (zfs_cmd_t), flag);
6596 if (error == 0 && rc != 0)
6597 error = SET_ERROR(EFAULT);
6600 if (error == 0 && vec->zvec_allow_log) {
6601 char *s = tsd_get(zfs_allow_log_key);
6604 (void) tsd_set(zfs_allow_log_key, saved_poolname);
6606 if (saved_poolname != NULL)
6607 strfree(saved_poolname);
6610 kmem_free(zc, sizeof (zfs_cmd_t));
6616 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
6618 if (cmd != DDI_ATTACH)
6619 return (DDI_FAILURE);
6621 if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
6622 DDI_PSEUDO, 0) == DDI_FAILURE)
6623 return (DDI_FAILURE);
6627 ddi_report_dev(dip);
6629 return (DDI_SUCCESS);
6633 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
6635 if (spa_busy() || zfs_busy() || zvol_busy())
6636 return (DDI_FAILURE);
6638 if (cmd != DDI_DETACH)
6639 return (DDI_FAILURE);
6643 ddi_prop_remove_all(dip);
6644 ddi_remove_minor_node(dip, NULL);
6646 return (DDI_SUCCESS);
6651 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
6654 case DDI_INFO_DEVT2DEVINFO:
6656 return (DDI_SUCCESS);
6658 case DDI_INFO_DEVT2INSTANCE:
6659 *result = (void *)0;
6660 return (DDI_SUCCESS);
6663 return (DDI_FAILURE);
6665 #endif /* illumos */
6668 * OK, so this is a little weird.
6670 * /dev/zfs is the control node, i.e. minor 0.
6671 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
6673 * /dev/zfs has basically nothing to do except serve up ioctls,
6674 * so most of the standard driver entry points are in zvol.c.
6677 static struct cb_ops zfs_cb_ops = {
6678 zfsdev_open, /* open */
6679 zfsdev_close, /* close */
6680 zvol_strategy, /* strategy */
6682 zvol_dump, /* dump */
6683 zvol_read, /* read */
6684 zvol_write, /* write */
6685 zfsdev_ioctl, /* ioctl */
6689 nochpoll, /* poll */
6690 ddi_prop_op, /* prop_op */
6691 NULL, /* streamtab */
6692 D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */
6693 CB_REV, /* version */
6694 nodev, /* async read */
6695 nodev, /* async write */
6698 static struct dev_ops zfs_dev_ops = {
6699 DEVO_REV, /* version */
6701 zfs_info, /* info */
6702 nulldev, /* identify */
6703 nulldev, /* probe */
6704 zfs_attach, /* attach */
6705 zfs_detach, /* detach */
6707 &zfs_cb_ops, /* driver operations */
6708 NULL, /* no bus operations */
6710 ddi_quiesce_not_needed, /* quiesce */
6713 static struct modldrv zfs_modldrv = {
6719 static struct modlinkage modlinkage = {
6721 (void *)&zfs_modlfs,
6722 (void *)&zfs_modldrv,
6725 #endif /* illumos */
6727 static struct cdevsw zfs_cdevsw = {
6728 .d_version = D_VERSION,
6729 .d_open = zfsdev_open,
6730 .d_ioctl = zfsdev_ioctl,
6731 .d_name = ZFS_DEV_NAME
6735 zfs_allow_log_destroy(void *arg)
6737 char *poolname = arg;
6744 zfsdev = make_dev(&zfs_cdevsw, 0x0, UID_ROOT, GID_OPERATOR, 0666,
6752 destroy_dev(zfsdev);
6755 static struct root_hold_token *zfs_root_token;
6756 struct proc *zfsproc;
6764 spa_init(FREAD | FWRITE);
6769 if ((error = mod_install(&modlinkage)) != 0) {
6776 tsd_create(&zfs_fsyncer_key, NULL);
6777 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
6778 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
6780 error = ldi_ident_from_mod(&modlinkage, &zfs_li);
6782 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
6792 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
6793 return (SET_ERROR(EBUSY));
6795 if ((error = mod_remove(&modlinkage)) != 0)
6801 if (zfs_nfsshare_inited)
6802 (void) ddi_modclose(nfs_mod);
6803 if (zfs_smbshare_inited)
6804 (void) ddi_modclose(smbsrv_mod);
6805 if (zfs_nfsshare_inited || zfs_smbshare_inited)
6806 (void) ddi_modclose(sharefs_mod);
6808 tsd_destroy(&zfs_fsyncer_key);
6809 ldi_ident_release(zfs_li);
6811 mutex_destroy(&zfs_share_lock);
6817 _info(struct modinfo *modinfop)
6819 return (mod_info(&modlinkage, modinfop));
6821 #endif /* illumos */
6823 static int zfs__init(void);
6824 static int zfs__fini(void);
6825 static void zfs_shutdown(void *, int);
6827 static eventhandler_tag zfs_shutdown_event_tag;
6830 #define ZFS_MIN_KSTACK_PAGES 4
6838 #if KSTACK_PAGES < ZFS_MIN_KSTACK_PAGES
6839 printf("ZFS NOTICE: KSTACK_PAGES is %d which could result in stack "
6840 "overflow panic!\nPlease consider adding "
6841 "'options KSTACK_PAGES=%d' to your kernel config\n", KSTACK_PAGES,
6842 ZFS_MIN_KSTACK_PAGES);
6845 zfs_root_token = root_mount_hold("ZFS");
6847 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
6849 spa_init(FREAD | FWRITE);
6854 tsd_create(&zfs_fsyncer_key, NULL);
6855 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
6856 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
6857 tsd_create(&zfs_geom_probe_vdev_key, NULL);
6859 printf("ZFS storage pool version: features support (" SPA_VERSION_STRING ")\n");
6860 root_mount_rel(zfs_root_token);
6870 if (spa_busy() || zfs_busy() || zvol_busy() ||
6871 zio_injection_enabled) {
6880 tsd_destroy(&zfs_fsyncer_key);
6881 tsd_destroy(&rrw_tsd_key);
6882 tsd_destroy(&zfs_allow_log_key);
6884 mutex_destroy(&zfs_share_lock);
6890 zfs_shutdown(void *arg __unused, int howto __unused)
6894 * ZFS fini routines can not properly work in a panic-ed system.
6896 if (panicstr == NULL)
6902 zfs_modevent(module_t mod, int type, void *unused __unused)
6910 zfs_shutdown_event_tag = EVENTHANDLER_REGISTER(
6911 shutdown_post_sync, zfs_shutdown, NULL,
6912 SHUTDOWN_PRI_FIRST);
6916 if (err == 0 && zfs_shutdown_event_tag != NULL)
6917 EVENTHANDLER_DEREGISTER(shutdown_post_sync,
6918 zfs_shutdown_event_tag);
6925 return (EOPNOTSUPP);
6928 static moduledata_t zfs_mod = {
6933 DECLARE_MODULE(zfsctrl, zfs_mod, SI_SUB_VFS, SI_ORDER_ANY);
6934 MODULE_VERSION(zfsctrl, 1);
6935 MODULE_DEPEND(zfsctrl, opensolaris, 1, 1, 1);
6936 MODULE_DEPEND(zfsctrl, krpc, 1, 1, 1);
6937 MODULE_DEPEND(zfsctrl, acl_nfs4, 1, 1, 1);