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 * Portions Copyright 2011 Martin Matuska
25 * Portions Copyright 2012 Pawel Jakub Dawidek <pawel@dawidek.net>
26 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
27 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
28 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
29 * Copyright (c) 201i3 by Delphix. All rights reserved.
30 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
31 * Copyright (c) 2013 Steven Hartland. All rights reserved.
37 * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
38 * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
40 * There are two ways that we handle ioctls: the legacy way where almost
41 * all of the logic is in the ioctl callback, and the new way where most
42 * of the marshalling is handled in the common entry point, zfsdev_ioctl().
44 * Non-legacy ioctls should be registered by calling
45 * zfs_ioctl_register() from zfs_ioctl_init(). The ioctl is invoked
46 * from userland by lzc_ioctl().
48 * The registration arguments are as follows:
51 * The name of the ioctl. This is used for history logging. If the
52 * ioctl returns successfully (the callback returns 0), and allow_log
53 * is true, then a history log entry will be recorded with the input &
54 * output nvlists. The log entry can be printed with "zpool history -i".
57 * The ioctl request number, which userland will pass to ioctl(2).
58 * The ioctl numbers can change from release to release, because
59 * the caller (libzfs) must be matched to the kernel.
61 * zfs_secpolicy_func_t *secpolicy
62 * This function will be called before the zfs_ioc_func_t, to
63 * determine if this operation is permitted. It should return EPERM
64 * on failure, and 0 on success. Checks include determining if the
65 * dataset is visible in this zone, and if the user has either all
66 * zfs privileges in the zone (SYS_MOUNT), or has been granted permission
67 * to do this operation on this dataset with "zfs allow".
69 * zfs_ioc_namecheck_t namecheck
70 * This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
71 * name, a dataset name, or nothing. If the name is not well-formed,
72 * the ioctl will fail and the callback will not be called.
73 * Therefore, the callback can assume that the name is well-formed
74 * (e.g. is null-terminated, doesn't have more than one '@' character,
75 * doesn't have invalid characters).
77 * zfs_ioc_poolcheck_t pool_check
78 * This specifies requirements on the pool state. If the pool does
79 * not meet them (is suspended or is readonly), the ioctl will fail
80 * and the callback will not be called. If any checks are specified
81 * (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
82 * Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
83 * POOL_CHECK_READONLY).
85 * boolean_t smush_outnvlist
86 * If smush_outnvlist is true, then the output is presumed to be a
87 * list of errors, and it will be "smushed" down to fit into the
88 * caller's buffer, by removing some entries and replacing them with a
89 * single "N_MORE_ERRORS" entry indicating how many were removed. See
90 * nvlist_smush() for details. If smush_outnvlist is false, and the
91 * outnvlist does not fit into the userland-provided buffer, then the
92 * ioctl will fail with ENOMEM.
94 * zfs_ioc_func_t *func
95 * The callback function that will perform the operation.
97 * The callback should return 0 on success, or an error number on
98 * failure. If the function fails, the userland ioctl will return -1,
99 * and errno will be set to the callback's return value. The callback
100 * will be called with the following arguments:
103 * The name of the pool or dataset to operate on, from
104 * zfs_cmd_t:zc_name. The 'namecheck' argument specifies the
105 * expected type (pool, dataset, or none).
108 * The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src. Or
109 * NULL if no input nvlist was provided. Changes to this nvlist are
110 * ignored. If the input nvlist could not be deserialized, the
111 * ioctl will fail and the callback will not be called.
114 * The output nvlist, initially empty. The callback can fill it in,
115 * and it will be returned to userland by serializing it into
116 * zfs_cmd_t:zc_nvlist_dst. If it is non-empty, and serialization
117 * fails (e.g. because the caller didn't supply a large enough
118 * buffer), then the overall ioctl will fail. See the
119 * 'smush_nvlist' argument above for additional behaviors.
121 * There are two typical uses of the output nvlist:
122 * - To return state, e.g. property values. In this case,
123 * smush_outnvlist should be false. If the buffer was not large
124 * enough, the caller will reallocate a larger buffer and try
127 * - To return multiple errors from an ioctl which makes on-disk
128 * changes. In this case, smush_outnvlist should be true.
129 * Ioctls which make on-disk modifications should generally not
130 * use the outnvl if they succeed, because the caller can not
131 * distinguish between the operation failing, and
132 * deserialization failing.
135 #include <sys/types.h>
136 #include <sys/param.h>
137 #include <sys/errno.h>
140 #include <sys/modctl.h>
141 #include <sys/open.h>
142 #include <sys/file.h>
143 #include <sys/kmem.h>
144 #include <sys/conf.h>
145 #include <sys/cmn_err.h>
146 #include <sys/stat.h>
147 #include <sys/zfs_ioctl.h>
148 #include <sys/zfs_vfsops.h>
149 #include <sys/zfs_znode.h>
152 #include <sys/spa_impl.h>
153 #include <sys/vdev.h>
154 #include <sys/priv_impl.h>
156 #include <sys/dsl_dir.h>
157 #include <sys/dsl_dataset.h>
158 #include <sys/dsl_prop.h>
159 #include <sys/dsl_deleg.h>
160 #include <sys/dmu_objset.h>
161 #include <sys/dmu_impl.h>
162 #include <sys/dmu_tx.h>
164 #include <sys/sunddi.h>
165 #include <sys/sunldi.h>
166 #include <sys/policy.h>
167 #include <sys/zone.h>
168 #include <sys/nvpair.h>
169 #include <sys/pathname.h>
170 #include <sys/mount.h>
172 #include <sys/fs/zfs.h>
173 #include <sys/zfs_ctldir.h>
174 #include <sys/zfs_dir.h>
175 #include <sys/zfs_onexit.h>
176 #include <sys/zvol.h>
177 #include <sys/dsl_scan.h>
178 #include <sharefs/share.h>
179 #include <sys/fm/util.h>
181 #include <sys/dmu_send.h>
182 #include <sys/dsl_destroy.h>
183 #include <sys/dsl_userhold.h>
184 #include <sys/zfeature.h>
186 #include <linux/miscdevice.h>
188 #include "zfs_namecheck.h"
189 #include "zfs_prop.h"
190 #include "zfs_deleg.h"
191 #include "zfs_comutil.h"
193 kmutex_t zfsdev_state_lock
;
194 list_t zfsdev_state_list
;
196 extern void zfs_init(void);
197 extern void zfs_fini(void);
199 uint_t zfs_fsyncer_key
;
200 extern uint_t rrw_tsd_key
;
201 static uint_t zfs_allow_log_key
;
203 typedef int zfs_ioc_legacy_func_t(zfs_cmd_t
*);
204 typedef int zfs_ioc_func_t(const char *, nvlist_t
*, nvlist_t
*);
205 typedef int zfs_secpolicy_func_t(zfs_cmd_t
*, nvlist_t
*, cred_t
*);
211 } zfs_ioc_namecheck_t
;
214 POOL_CHECK_NONE
= 1 << 0,
215 POOL_CHECK_SUSPENDED
= 1 << 1,
216 POOL_CHECK_READONLY
= 1 << 2,
217 } zfs_ioc_poolcheck_t
;
219 typedef struct zfs_ioc_vec
{
220 zfs_ioc_legacy_func_t
*zvec_legacy_func
;
221 zfs_ioc_func_t
*zvec_func
;
222 zfs_secpolicy_func_t
*zvec_secpolicy
;
223 zfs_ioc_namecheck_t zvec_namecheck
;
224 boolean_t zvec_allow_log
;
225 zfs_ioc_poolcheck_t zvec_pool_check
;
226 boolean_t zvec_smush_outnvlist
;
227 const char *zvec_name
;
230 /* This array is indexed by zfs_userquota_prop_t */
231 static const char *userquota_perms
[] = {
232 ZFS_DELEG_PERM_USERUSED
,
233 ZFS_DELEG_PERM_USERQUOTA
,
234 ZFS_DELEG_PERM_GROUPUSED
,
235 ZFS_DELEG_PERM_GROUPQUOTA
,
238 static int zfs_ioc_userspace_upgrade(zfs_cmd_t
*zc
);
239 static int zfs_check_settable(const char *name
, nvpair_t
*property
,
241 static int zfs_check_clearable(char *dataset
, nvlist_t
*props
,
243 static int zfs_fill_zplprops_root(uint64_t, nvlist_t
*, nvlist_t
*,
245 int zfs_set_prop_nvlist(const char *, zprop_source_t
, nvlist_t
*, nvlist_t
*);
246 static int get_nvlist(uint64_t nvl
, uint64_t size
, int iflag
, nvlist_t
**nvp
);
248 static int zfs_prop_activate_feature(spa_t
*spa
, zfeature_info_t
*feature
);
251 history_str_free(char *buf
)
253 kmem_free(buf
, HIS_MAX_RECORD_LEN
);
257 history_str_get(zfs_cmd_t
*zc
)
261 if (zc
->zc_history
== 0)
264 buf
= kmem_alloc(HIS_MAX_RECORD_LEN
, KM_SLEEP
| KM_NODEBUG
);
265 if (copyinstr((void *)(uintptr_t)zc
->zc_history
,
266 buf
, HIS_MAX_RECORD_LEN
, NULL
) != 0) {
267 history_str_free(buf
);
271 buf
[HIS_MAX_RECORD_LEN
-1] = '\0';
277 * Check to see if the named dataset is currently defined as bootable
280 zfs_is_bootfs(const char *name
)
284 if (dmu_objset_hold(name
, FTAG
, &os
) == 0) {
286 ret
= (dmu_objset_id(os
) == spa_bootfs(dmu_objset_spa(os
)));
287 dmu_objset_rele(os
, FTAG
);
294 * Return non-zero if the spa version is less than requested version.
297 zfs_earlier_version(const char *name
, int version
)
301 if (spa_open(name
, &spa
, FTAG
) == 0) {
302 if (spa_version(spa
) < version
) {
303 spa_close(spa
, FTAG
);
306 spa_close(spa
, FTAG
);
312 * Return TRUE if the ZPL version is less than requested version.
315 zpl_earlier_version(const char *name
, int version
)
318 boolean_t rc
= B_TRUE
;
320 if (dmu_objset_hold(name
, FTAG
, &os
) == 0) {
323 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
324 dmu_objset_rele(os
, FTAG
);
327 /* XXX reading from non-owned objset */
328 if (zfs_get_zplprop(os
, ZFS_PROP_VERSION
, &zplversion
) == 0)
329 rc
= zplversion
< version
;
330 dmu_objset_rele(os
, FTAG
);
336 zfs_log_history(zfs_cmd_t
*zc
)
341 if ((buf
= history_str_get(zc
)) == NULL
)
344 if (spa_open(zc
->zc_name
, &spa
, FTAG
) == 0) {
345 if (spa_version(spa
) >= SPA_VERSION_ZPOOL_HISTORY
)
346 (void) spa_history_log(spa
, buf
);
347 spa_close(spa
, FTAG
);
349 history_str_free(buf
);
353 * Policy for top-level read operations (list pools). Requires no privileges,
354 * and can be used in the local zone, as there is no associated dataset.
358 zfs_secpolicy_none(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
364 * Policy for dataset read operations (list children, get statistics). Requires
365 * no privileges, but must be visible in the local zone.
369 zfs_secpolicy_read(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
371 if (INGLOBALZONE(curproc
) ||
372 zone_dataset_visible(zc
->zc_name
, NULL
))
375 return (SET_ERROR(ENOENT
));
379 zfs_dozonecheck_impl(const char *dataset
, uint64_t zoned
, cred_t
*cr
)
384 * The dataset must be visible by this zone -- check this first
385 * so they don't see EPERM on something they shouldn't know about.
387 if (!INGLOBALZONE(curproc
) &&
388 !zone_dataset_visible(dataset
, &writable
))
389 return (SET_ERROR(ENOENT
));
391 if (INGLOBALZONE(curproc
)) {
393 * If the fs is zoned, only root can access it from the
396 if (secpolicy_zfs(cr
) && zoned
)
397 return (SET_ERROR(EPERM
));
400 * If we are in a local zone, the 'zoned' property must be set.
403 return (SET_ERROR(EPERM
));
405 /* must be writable by this zone */
407 return (SET_ERROR(EPERM
));
413 zfs_dozonecheck(const char *dataset
, cred_t
*cr
)
417 if (dsl_prop_get_integer(dataset
, "zoned", &zoned
, NULL
))
418 return (SET_ERROR(ENOENT
));
420 return (zfs_dozonecheck_impl(dataset
, zoned
, cr
));
424 zfs_dozonecheck_ds(const char *dataset
, dsl_dataset_t
*ds
, cred_t
*cr
)
428 if (dsl_prop_get_int_ds(ds
, "zoned", &zoned
))
429 return (SET_ERROR(ENOENT
));
431 return (zfs_dozonecheck_impl(dataset
, zoned
, cr
));
435 zfs_secpolicy_write_perms_ds(const char *name
, dsl_dataset_t
*ds
,
436 const char *perm
, cred_t
*cr
)
440 error
= zfs_dozonecheck_ds(name
, ds
, cr
);
442 error
= secpolicy_zfs(cr
);
444 error
= dsl_deleg_access_impl(ds
, perm
, cr
);
450 zfs_secpolicy_write_perms(const char *name
, const char *perm
, cred_t
*cr
)
456 error
= dsl_pool_hold(name
, FTAG
, &dp
);
460 error
= dsl_dataset_hold(dp
, name
, FTAG
, &ds
);
462 dsl_pool_rele(dp
, FTAG
);
466 error
= zfs_secpolicy_write_perms_ds(name
, ds
, perm
, cr
);
468 dsl_dataset_rele(ds
, FTAG
);
469 dsl_pool_rele(dp
, FTAG
);
474 * Policy for setting the security label property.
476 * Returns 0 for success, non-zero for access and other errors.
479 zfs_set_slabel_policy(const char *name
, char *strval
, cred_t
*cr
)
482 char ds_hexsl
[MAXNAMELEN
];
483 bslabel_t ds_sl
, new_sl
;
484 boolean_t new_default
= FALSE
;
486 int needed_priv
= -1;
489 /* First get the existing dataset label. */
490 error
= dsl_prop_get(name
, zfs_prop_to_name(ZFS_PROP_MLSLABEL
),
491 1, sizeof (ds_hexsl
), &ds_hexsl
, NULL
);
493 return (SET_ERROR(EPERM
));
495 if (strcasecmp(strval
, ZFS_MLSLABEL_DEFAULT
) == 0)
498 /* The label must be translatable */
499 if (!new_default
&& (hexstr_to_label(strval
, &new_sl
) != 0))
500 return (SET_ERROR(EINVAL
));
503 * In a non-global zone, disallow attempts to set a label that
504 * doesn't match that of the zone; otherwise no other checks
507 if (!INGLOBALZONE(curproc
)) {
508 if (new_default
|| !blequal(&new_sl
, CR_SL(CRED())))
509 return (SET_ERROR(EPERM
));
514 * For global-zone datasets (i.e., those whose zoned property is
515 * "off", verify that the specified new label is valid for the
518 if (dsl_prop_get_integer(name
,
519 zfs_prop_to_name(ZFS_PROP_ZONED
), &zoned
, NULL
))
520 return (SET_ERROR(EPERM
));
522 if (zfs_check_global_label(name
, strval
) != 0)
523 return (SET_ERROR(EPERM
));
527 * If the existing dataset label is nondefault, check if the
528 * dataset is mounted (label cannot be changed while mounted).
529 * Get the zfs_sb_t; if there isn't one, then the dataset isn't
530 * mounted (or isn't a dataset, doesn't exist, ...).
532 if (strcasecmp(ds_hexsl
, ZFS_MLSLABEL_DEFAULT
) != 0) {
534 static char *setsl_tag
= "setsl_tag";
537 * Try to own the dataset; abort if there is any error,
538 * (e.g., already mounted, in use, or other error).
540 error
= dmu_objset_own(name
, DMU_OST_ZFS
, B_TRUE
,
543 return (SET_ERROR(EPERM
));
545 dmu_objset_disown(os
, setsl_tag
);
548 needed_priv
= PRIV_FILE_DOWNGRADE_SL
;
552 if (hexstr_to_label(strval
, &new_sl
) != 0)
553 return (SET_ERROR(EPERM
));
555 if (blstrictdom(&ds_sl
, &new_sl
))
556 needed_priv
= PRIV_FILE_DOWNGRADE_SL
;
557 else if (blstrictdom(&new_sl
, &ds_sl
))
558 needed_priv
= PRIV_FILE_UPGRADE_SL
;
560 /* dataset currently has a default label */
562 needed_priv
= PRIV_FILE_UPGRADE_SL
;
566 if (needed_priv
!= -1)
567 return (PRIV_POLICY(cr
, needed_priv
, B_FALSE
, EPERM
, NULL
));
571 #endif /* HAVE_MLSLABEL */
575 zfs_secpolicy_setprop(const char *dsname
, zfs_prop_t prop
, nvpair_t
*propval
,
581 * Check permissions for special properties.
588 * Disallow setting of 'zoned' from within a local zone.
590 if (!INGLOBALZONE(curproc
))
591 return (SET_ERROR(EPERM
));
595 if (!INGLOBALZONE(curproc
)) {
597 char setpoint
[MAXNAMELEN
];
599 * Unprivileged users are allowed to modify the
600 * quota on things *under* (ie. contained by)
601 * the thing they own.
603 if (dsl_prop_get_integer(dsname
, "zoned", &zoned
,
605 return (SET_ERROR(EPERM
));
606 if (!zoned
|| strlen(dsname
) <= strlen(setpoint
))
607 return (SET_ERROR(EPERM
));
611 case ZFS_PROP_MLSLABEL
:
612 if (!is_system_labeled())
613 return (SET_ERROR(EPERM
));
615 if (nvpair_value_string(propval
, &strval
) == 0) {
618 err
= zfs_set_slabel_policy(dsname
, strval
, CRED());
625 return (zfs_secpolicy_write_perms(dsname
, zfs_prop_to_name(prop
), cr
));
630 zfs_secpolicy_set_fsacl(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
634 error
= zfs_dozonecheck(zc
->zc_name
, cr
);
639 * permission to set permissions will be evaluated later in
640 * dsl_deleg_can_allow()
647 zfs_secpolicy_rollback(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
649 return (zfs_secpolicy_write_perms(zc
->zc_name
,
650 ZFS_DELEG_PERM_ROLLBACK
, cr
));
655 zfs_secpolicy_send(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
663 * Generate the current snapshot name from the given objsetid, then
664 * use that name for the secpolicy/zone checks.
666 cp
= strchr(zc
->zc_name
, '@');
668 return (SET_ERROR(EINVAL
));
669 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
673 error
= dsl_dataset_hold_obj(dp
, zc
->zc_sendobj
, FTAG
, &ds
);
675 dsl_pool_rele(dp
, FTAG
);
679 dsl_dataset_name(ds
, zc
->zc_name
);
681 error
= zfs_secpolicy_write_perms_ds(zc
->zc_name
, ds
,
682 ZFS_DELEG_PERM_SEND
, cr
);
683 dsl_dataset_rele(ds
, FTAG
);
684 dsl_pool_rele(dp
, FTAG
);
691 zfs_secpolicy_send_new(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
693 return (zfs_secpolicy_write_perms(zc
->zc_name
,
694 ZFS_DELEG_PERM_SEND
, cr
));
697 #ifdef HAVE_SMB_SHARE
700 zfs_secpolicy_deleg_share(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
705 if ((error
= lookupname(zc
->zc_value
, UIO_SYSSPACE
,
706 NO_FOLLOW
, NULL
, &vp
)) != 0)
709 /* Now make sure mntpnt and dataset are ZFS */
711 if (vp
->v_vfsp
->vfs_fstype
!= zfsfstype
||
712 (strcmp((char *)refstr_value(vp
->v_vfsp
->vfs_resource
),
713 zc
->zc_name
) != 0)) {
715 return (SET_ERROR(EPERM
));
719 return (dsl_deleg_access(zc
->zc_name
,
720 ZFS_DELEG_PERM_SHARE
, cr
));
722 #endif /* HAVE_SMB_SHARE */
725 zfs_secpolicy_share(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
727 #ifdef HAVE_SMB_SHARE
728 if (!INGLOBALZONE(curproc
))
729 return (SET_ERROR(EPERM
));
731 if (secpolicy_nfs(cr
) == 0) {
734 return (zfs_secpolicy_deleg_share(zc
, innvl
, cr
));
737 return (SET_ERROR(ENOTSUP
));
738 #endif /* HAVE_SMB_SHARE */
742 zfs_secpolicy_smb_acl(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
744 #ifdef HAVE_SMB_SHARE
745 if (!INGLOBALZONE(curproc
))
746 return (SET_ERROR(EPERM
));
748 if (secpolicy_smb(cr
) == 0) {
751 return (zfs_secpolicy_deleg_share(zc
, innvl
, cr
));
754 return (SET_ERROR(ENOTSUP
));
755 #endif /* HAVE_SMB_SHARE */
759 zfs_get_parent(const char *datasetname
, char *parent
, int parentsize
)
764 * Remove the @bla or /bla from the end of the name to get the parent.
766 (void) strncpy(parent
, datasetname
, parentsize
);
767 cp
= strrchr(parent
, '@');
771 cp
= strrchr(parent
, '/');
773 return (SET_ERROR(ENOENT
));
781 zfs_secpolicy_destroy_perms(const char *name
, cred_t
*cr
)
785 if ((error
= zfs_secpolicy_write_perms(name
,
786 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
789 return (zfs_secpolicy_write_perms(name
, ZFS_DELEG_PERM_DESTROY
, cr
));
794 zfs_secpolicy_destroy(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
796 return (zfs_secpolicy_destroy_perms(zc
->zc_name
, cr
));
800 * Destroying snapshots with delegated permissions requires
801 * descendant mount and destroy permissions.
805 zfs_secpolicy_destroy_snaps(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
808 nvpair_t
*pair
, *nextpair
;
811 if (nvlist_lookup_nvlist(innvl
, "snaps", &snaps
) != 0)
812 return (SET_ERROR(EINVAL
));
813 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
818 error
= dsl_pool_hold(nvpair_name(pair
), FTAG
, &dp
);
821 nextpair
= nvlist_next_nvpair(snaps
, pair
);
822 error
= dsl_dataset_hold(dp
, nvpair_name(pair
), FTAG
, &ds
);
824 dsl_dataset_rele(ds
, FTAG
);
825 dsl_pool_rele(dp
, FTAG
);
828 error
= zfs_secpolicy_destroy_perms(nvpair_name(pair
),
830 } else if (error
== ENOENT
) {
832 * Ignore any snapshots that don't exist (we consider
833 * them "already destroyed"). Remove the name from the
834 * nvl here in case the snapshot is created between
835 * now and when we try to destroy it (in which case
836 * we don't want to destroy it since we haven't
837 * checked for permission).
839 fnvlist_remove_nvpair(snaps
, pair
);
850 zfs_secpolicy_rename_perms(const char *from
, const char *to
, cred_t
*cr
)
852 char parentname
[MAXNAMELEN
];
855 if ((error
= zfs_secpolicy_write_perms(from
,
856 ZFS_DELEG_PERM_RENAME
, cr
)) != 0)
859 if ((error
= zfs_secpolicy_write_perms(from
,
860 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
863 if ((error
= zfs_get_parent(to
, parentname
,
864 sizeof (parentname
))) != 0)
867 if ((error
= zfs_secpolicy_write_perms(parentname
,
868 ZFS_DELEG_PERM_CREATE
, cr
)) != 0)
871 if ((error
= zfs_secpolicy_write_perms(parentname
,
872 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
880 zfs_secpolicy_rename(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
882 return (zfs_secpolicy_rename_perms(zc
->zc_name
, zc
->zc_value
, cr
));
887 zfs_secpolicy_promote(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
890 dsl_dataset_t
*clone
;
893 error
= zfs_secpolicy_write_perms(zc
->zc_name
,
894 ZFS_DELEG_PERM_PROMOTE
, cr
);
898 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
902 error
= dsl_dataset_hold(dp
, zc
->zc_name
, FTAG
, &clone
);
905 char parentname
[MAXNAMELEN
];
906 dsl_dataset_t
*origin
= NULL
;
910 error
= dsl_dataset_hold_obj(dd
->dd_pool
,
911 dd
->dd_phys
->dd_origin_obj
, FTAG
, &origin
);
913 dsl_dataset_rele(clone
, FTAG
);
914 dsl_pool_rele(dp
, FTAG
);
918 error
= zfs_secpolicy_write_perms_ds(zc
->zc_name
, clone
,
919 ZFS_DELEG_PERM_MOUNT
, cr
);
921 dsl_dataset_name(origin
, parentname
);
923 error
= zfs_secpolicy_write_perms_ds(parentname
, origin
,
924 ZFS_DELEG_PERM_PROMOTE
, cr
);
926 dsl_dataset_rele(clone
, FTAG
);
927 dsl_dataset_rele(origin
, FTAG
);
929 dsl_pool_rele(dp
, FTAG
);
935 zfs_secpolicy_recv(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
939 if ((error
= zfs_secpolicy_write_perms(zc
->zc_name
,
940 ZFS_DELEG_PERM_RECEIVE
, cr
)) != 0)
943 if ((error
= zfs_secpolicy_write_perms(zc
->zc_name
,
944 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
947 return (zfs_secpolicy_write_perms(zc
->zc_name
,
948 ZFS_DELEG_PERM_CREATE
, cr
));
952 zfs_secpolicy_snapshot_perms(const char *name
, cred_t
*cr
)
954 return (zfs_secpolicy_write_perms(name
,
955 ZFS_DELEG_PERM_SNAPSHOT
, cr
));
959 * Check for permission to create each snapshot in the nvlist.
963 zfs_secpolicy_snapshot(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
969 if (nvlist_lookup_nvlist(innvl
, "snaps", &snaps
) != 0)
970 return (SET_ERROR(EINVAL
));
971 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
972 pair
= nvlist_next_nvpair(snaps
, pair
)) {
973 char *name
= nvpair_name(pair
);
974 char *atp
= strchr(name
, '@');
977 error
= SET_ERROR(EINVAL
);
981 error
= zfs_secpolicy_snapshot_perms(name
, cr
);
991 zfs_secpolicy_log_history(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
994 * Even root must have a proper TSD so that we know what pool
997 if (tsd_get(zfs_allow_log_key
) == NULL
)
998 return (SET_ERROR(EPERM
));
1003 zfs_secpolicy_create_clone(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1005 char parentname
[MAXNAMELEN
];
1009 if ((error
= zfs_get_parent(zc
->zc_name
, parentname
,
1010 sizeof (parentname
))) != 0)
1013 if (nvlist_lookup_string(innvl
, "origin", &origin
) == 0 &&
1014 (error
= zfs_secpolicy_write_perms(origin
,
1015 ZFS_DELEG_PERM_CLONE
, cr
)) != 0)
1018 if ((error
= zfs_secpolicy_write_perms(parentname
,
1019 ZFS_DELEG_PERM_CREATE
, cr
)) != 0)
1022 return (zfs_secpolicy_write_perms(parentname
,
1023 ZFS_DELEG_PERM_MOUNT
, cr
));
1027 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
1028 * SYS_CONFIG privilege, which is not available in a local zone.
1032 zfs_secpolicy_config(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1034 if (secpolicy_sys_config(cr
, B_FALSE
) != 0)
1035 return (SET_ERROR(EPERM
));
1041 * Policy for object to name lookups.
1045 zfs_secpolicy_diff(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1049 if ((error
= secpolicy_sys_config(cr
, B_FALSE
)) == 0)
1052 error
= zfs_secpolicy_write_perms(zc
->zc_name
, ZFS_DELEG_PERM_DIFF
, cr
);
1057 * Policy for fault injection. Requires all privileges.
1061 zfs_secpolicy_inject(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1063 return (secpolicy_zinject(cr
));
1068 zfs_secpolicy_inherit_prop(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1070 zfs_prop_t prop
= zfs_name_to_prop(zc
->zc_value
);
1072 if (prop
== ZPROP_INVAL
) {
1073 if (!zfs_prop_user(zc
->zc_value
))
1074 return (SET_ERROR(EINVAL
));
1075 return (zfs_secpolicy_write_perms(zc
->zc_name
,
1076 ZFS_DELEG_PERM_USERPROP
, cr
));
1078 return (zfs_secpolicy_setprop(zc
->zc_name
, prop
,
1084 zfs_secpolicy_userspace_one(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1086 int err
= zfs_secpolicy_read(zc
, innvl
, cr
);
1090 if (zc
->zc_objset_type
>= ZFS_NUM_USERQUOTA_PROPS
)
1091 return (SET_ERROR(EINVAL
));
1093 if (zc
->zc_value
[0] == 0) {
1095 * They are asking about a posix uid/gid. If it's
1096 * themself, allow it.
1098 if (zc
->zc_objset_type
== ZFS_PROP_USERUSED
||
1099 zc
->zc_objset_type
== ZFS_PROP_USERQUOTA
) {
1100 if (zc
->zc_guid
== crgetuid(cr
))
1103 if (groupmember(zc
->zc_guid
, cr
))
1108 return (zfs_secpolicy_write_perms(zc
->zc_name
,
1109 userquota_perms
[zc
->zc_objset_type
], cr
));
1113 zfs_secpolicy_userspace_many(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1115 int err
= zfs_secpolicy_read(zc
, innvl
, cr
);
1119 if (zc
->zc_objset_type
>= ZFS_NUM_USERQUOTA_PROPS
)
1120 return (SET_ERROR(EINVAL
));
1122 return (zfs_secpolicy_write_perms(zc
->zc_name
,
1123 userquota_perms
[zc
->zc_objset_type
], cr
));
1128 zfs_secpolicy_userspace_upgrade(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1130 return (zfs_secpolicy_setprop(zc
->zc_name
, ZFS_PROP_VERSION
,
1136 zfs_secpolicy_hold(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1142 error
= nvlist_lookup_nvlist(innvl
, "holds", &holds
);
1144 return (SET_ERROR(EINVAL
));
1146 for (pair
= nvlist_next_nvpair(holds
, NULL
); pair
!= NULL
;
1147 pair
= nvlist_next_nvpair(holds
, pair
)) {
1148 char fsname
[MAXNAMELEN
];
1149 error
= dmu_fsname(nvpair_name(pair
), fsname
);
1152 error
= zfs_secpolicy_write_perms(fsname
,
1153 ZFS_DELEG_PERM_HOLD
, cr
);
1162 zfs_secpolicy_release(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1167 for (pair
= nvlist_next_nvpair(innvl
, NULL
); pair
!= NULL
;
1168 pair
= nvlist_next_nvpair(innvl
, pair
)) {
1169 char fsname
[MAXNAMELEN
];
1170 error
= dmu_fsname(nvpair_name(pair
), fsname
);
1173 error
= zfs_secpolicy_write_perms(fsname
,
1174 ZFS_DELEG_PERM_RELEASE
, cr
);
1182 * Policy for allowing temporary snapshots to be taken or released
1185 zfs_secpolicy_tmp_snapshot(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1188 * A temporary snapshot is the same as a snapshot,
1189 * hold, destroy and release all rolled into one.
1190 * Delegated diff alone is sufficient that we allow this.
1194 if ((error
= zfs_secpolicy_write_perms(zc
->zc_name
,
1195 ZFS_DELEG_PERM_DIFF
, cr
)) == 0)
1198 error
= zfs_secpolicy_snapshot_perms(zc
->zc_name
, cr
);
1200 error
= zfs_secpolicy_hold(zc
, innvl
, cr
);
1202 error
= zfs_secpolicy_release(zc
, innvl
, cr
);
1204 error
= zfs_secpolicy_destroy(zc
, innvl
, cr
);
1209 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1212 get_nvlist(uint64_t nvl
, uint64_t size
, int iflag
, nvlist_t
**nvp
)
1216 nvlist_t
*list
= NULL
;
1219 * Read in and unpack the user-supplied nvlist.
1222 return (SET_ERROR(EINVAL
));
1224 packed
= kmem_alloc(size
, KM_SLEEP
| KM_NODEBUG
);
1226 if ((error
= ddi_copyin((void *)(uintptr_t)nvl
, packed
, size
,
1228 kmem_free(packed
, size
);
1232 if ((error
= nvlist_unpack(packed
, size
, &list
, 0)) != 0) {
1233 kmem_free(packed
, size
);
1237 kmem_free(packed
, size
);
1244 * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
1245 * Entries will be removed from the end of the nvlist, and one int32 entry
1246 * named "N_MORE_ERRORS" will be added indicating how many entries were
1250 nvlist_smush(nvlist_t
*errors
, size_t max
)
1254 size
= fnvlist_size(errors
);
1257 nvpair_t
*more_errors
;
1261 return (SET_ERROR(ENOMEM
));
1263 fnvlist_add_int32(errors
, ZPROP_N_MORE_ERRORS
, 0);
1264 more_errors
= nvlist_prev_nvpair(errors
, NULL
);
1267 nvpair_t
*pair
= nvlist_prev_nvpair(errors
,
1269 fnvlist_remove_nvpair(errors
, pair
);
1271 size
= fnvlist_size(errors
);
1272 } while (size
> max
);
1274 fnvlist_remove_nvpair(errors
, more_errors
);
1275 fnvlist_add_int32(errors
, ZPROP_N_MORE_ERRORS
, n
);
1276 ASSERT3U(fnvlist_size(errors
), <=, max
);
1283 put_nvlist(zfs_cmd_t
*zc
, nvlist_t
*nvl
)
1285 char *packed
= NULL
;
1289 size
= fnvlist_size(nvl
);
1291 if (size
> zc
->zc_nvlist_dst_size
) {
1292 error
= SET_ERROR(ENOMEM
);
1294 packed
= fnvlist_pack(nvl
, &size
);
1295 if (ddi_copyout(packed
, (void *)(uintptr_t)zc
->zc_nvlist_dst
,
1296 size
, zc
->zc_iflags
) != 0)
1297 error
= SET_ERROR(EFAULT
);
1298 fnvlist_pack_free(packed
, size
);
1301 zc
->zc_nvlist_dst_size
= size
;
1302 zc
->zc_nvlist_dst_filled
= B_TRUE
;
1307 get_zfs_sb(const char *dsname
, zfs_sb_t
**zsbp
)
1312 error
= dmu_objset_hold(dsname
, FTAG
, &os
);
1315 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
1316 dmu_objset_rele(os
, FTAG
);
1317 return (SET_ERROR(EINVAL
));
1320 mutex_enter(&os
->os_user_ptr_lock
);
1321 *zsbp
= dmu_objset_get_user(os
);
1322 if (*zsbp
&& (*zsbp
)->z_sb
) {
1323 atomic_inc(&((*zsbp
)->z_sb
->s_active
));
1325 error
= SET_ERROR(ESRCH
);
1327 mutex_exit(&os
->os_user_ptr_lock
);
1328 dmu_objset_rele(os
, FTAG
);
1333 * Find a zfs_sb_t for a mounted filesystem, or create our own, in which
1334 * case its z_sb will be NULL, and it will be opened as the owner.
1335 * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1336 * which prevents all inode ops from running.
1339 zfs_sb_hold(const char *name
, void *tag
, zfs_sb_t
**zsbp
, boolean_t writer
)
1343 if (get_zfs_sb(name
, zsbp
) != 0)
1344 error
= zfs_sb_create(name
, zsbp
);
1346 rrw_enter(&(*zsbp
)->z_teardown_lock
, (writer
) ? RW_WRITER
:
1348 if ((*zsbp
)->z_unmounted
) {
1350 * XXX we could probably try again, since the unmounting
1351 * thread should be just about to disassociate the
1352 * objset from the zfsvfs.
1354 rrw_exit(&(*zsbp
)->z_teardown_lock
, tag
);
1355 return (SET_ERROR(EBUSY
));
1362 zfs_sb_rele(zfs_sb_t
*zsb
, void *tag
)
1364 rrw_exit(&zsb
->z_teardown_lock
, tag
);
1367 deactivate_super(zsb
->z_sb
);
1369 dmu_objset_disown(zsb
->z_os
, zsb
);
1375 zfs_ioc_pool_create(zfs_cmd_t
*zc
)
1378 nvlist_t
*config
, *props
= NULL
;
1379 nvlist_t
*rootprops
= NULL
;
1380 nvlist_t
*zplprops
= NULL
;
1382 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1383 zc
->zc_iflags
, &config
)))
1386 if (zc
->zc_nvlist_src_size
!= 0 && (error
=
1387 get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1388 zc
->zc_iflags
, &props
))) {
1389 nvlist_free(config
);
1394 nvlist_t
*nvl
= NULL
;
1395 uint64_t version
= SPA_VERSION
;
1397 (void) nvlist_lookup_uint64(props
,
1398 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
);
1399 if (!SPA_VERSION_IS_SUPPORTED(version
)) {
1400 error
= SET_ERROR(EINVAL
);
1401 goto pool_props_bad
;
1403 (void) nvlist_lookup_nvlist(props
, ZPOOL_ROOTFS_PROPS
, &nvl
);
1405 error
= nvlist_dup(nvl
, &rootprops
, KM_SLEEP
);
1407 nvlist_free(config
);
1411 (void) nvlist_remove_all(props
, ZPOOL_ROOTFS_PROPS
);
1413 VERIFY(nvlist_alloc(&zplprops
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1414 error
= zfs_fill_zplprops_root(version
, rootprops
,
1417 goto pool_props_bad
;
1420 error
= spa_create(zc
->zc_name
, config
, props
, zplprops
);
1423 * Set the remaining root properties
1425 if (!error
&& (error
= zfs_set_prop_nvlist(zc
->zc_name
,
1426 ZPROP_SRC_LOCAL
, rootprops
, NULL
)) != 0)
1427 (void) spa_destroy(zc
->zc_name
);
1430 nvlist_free(rootprops
);
1431 nvlist_free(zplprops
);
1432 nvlist_free(config
);
1439 zfs_ioc_pool_destroy(zfs_cmd_t
*zc
)
1442 zfs_log_history(zc
);
1443 error
= spa_destroy(zc
->zc_name
);
1445 zvol_remove_minors(zc
->zc_name
);
1450 zfs_ioc_pool_import(zfs_cmd_t
*zc
)
1452 nvlist_t
*config
, *props
= NULL
;
1456 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1457 zc
->zc_iflags
, &config
)) != 0)
1460 if (zc
->zc_nvlist_src_size
!= 0 && (error
=
1461 get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1462 zc
->zc_iflags
, &props
))) {
1463 nvlist_free(config
);
1467 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &guid
) != 0 ||
1468 guid
!= zc
->zc_guid
)
1469 error
= SET_ERROR(EINVAL
);
1471 error
= spa_import(zc
->zc_name
, config
, props
, zc
->zc_cookie
);
1473 if (zc
->zc_nvlist_dst
!= 0) {
1476 if ((err
= put_nvlist(zc
, config
)) != 0)
1480 nvlist_free(config
);
1489 zfs_ioc_pool_export(zfs_cmd_t
*zc
)
1492 boolean_t force
= (boolean_t
)zc
->zc_cookie
;
1493 boolean_t hardforce
= (boolean_t
)zc
->zc_guid
;
1495 zfs_log_history(zc
);
1496 error
= spa_export(zc
->zc_name
, NULL
, force
, hardforce
);
1498 zvol_remove_minors(zc
->zc_name
);
1503 zfs_ioc_pool_configs(zfs_cmd_t
*zc
)
1508 if ((configs
= spa_all_configs(&zc
->zc_cookie
)) == NULL
)
1509 return (SET_ERROR(EEXIST
));
1511 error
= put_nvlist(zc
, configs
);
1513 nvlist_free(configs
);
1520 * zc_name name of the pool
1523 * zc_cookie real errno
1524 * zc_nvlist_dst config nvlist
1525 * zc_nvlist_dst_size size of config nvlist
1528 zfs_ioc_pool_stats(zfs_cmd_t
*zc
)
1534 error
= spa_get_stats(zc
->zc_name
, &config
, zc
->zc_value
,
1535 sizeof (zc
->zc_value
));
1537 if (config
!= NULL
) {
1538 ret
= put_nvlist(zc
, config
);
1539 nvlist_free(config
);
1542 * The config may be present even if 'error' is non-zero.
1543 * In this case we return success, and preserve the real errno
1546 zc
->zc_cookie
= error
;
1555 * Try to import the given pool, returning pool stats as appropriate so that
1556 * user land knows which devices are available and overall pool health.
1559 zfs_ioc_pool_tryimport(zfs_cmd_t
*zc
)
1561 nvlist_t
*tryconfig
, *config
;
1564 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1565 zc
->zc_iflags
, &tryconfig
)) != 0)
1568 config
= spa_tryimport(tryconfig
);
1570 nvlist_free(tryconfig
);
1573 return (SET_ERROR(EINVAL
));
1575 error
= put_nvlist(zc
, config
);
1576 nvlist_free(config
);
1583 * zc_name name of the pool
1584 * zc_cookie scan func (pool_scan_func_t)
1587 zfs_ioc_pool_scan(zfs_cmd_t
*zc
)
1592 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1595 if (zc
->zc_cookie
== POOL_SCAN_NONE
)
1596 error
= spa_scan_stop(spa
);
1598 error
= spa_scan(spa
, zc
->zc_cookie
);
1600 spa_close(spa
, FTAG
);
1606 zfs_ioc_pool_freeze(zfs_cmd_t
*zc
)
1611 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1614 spa_close(spa
, FTAG
);
1620 zfs_ioc_pool_upgrade(zfs_cmd_t
*zc
)
1625 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1628 if (zc
->zc_cookie
< spa_version(spa
) ||
1629 !SPA_VERSION_IS_SUPPORTED(zc
->zc_cookie
)) {
1630 spa_close(spa
, FTAG
);
1631 return (SET_ERROR(EINVAL
));
1634 spa_upgrade(spa
, zc
->zc_cookie
);
1635 spa_close(spa
, FTAG
);
1641 zfs_ioc_pool_get_history(zfs_cmd_t
*zc
)
1648 if ((size
= zc
->zc_history_len
) == 0)
1649 return (SET_ERROR(EINVAL
));
1651 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1654 if (spa_version(spa
) < SPA_VERSION_ZPOOL_HISTORY
) {
1655 spa_close(spa
, FTAG
);
1656 return (SET_ERROR(ENOTSUP
));
1659 hist_buf
= vmem_alloc(size
, KM_SLEEP
);
1660 if ((error
= spa_history_get(spa
, &zc
->zc_history_offset
,
1661 &zc
->zc_history_len
, hist_buf
)) == 0) {
1662 error
= ddi_copyout(hist_buf
,
1663 (void *)(uintptr_t)zc
->zc_history
,
1664 zc
->zc_history_len
, zc
->zc_iflags
);
1667 spa_close(spa
, FTAG
);
1668 vmem_free(hist_buf
, size
);
1673 zfs_ioc_pool_reguid(zfs_cmd_t
*zc
)
1678 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1680 error
= spa_change_guid(spa
);
1681 spa_close(spa
, FTAG
);
1687 zfs_ioc_dsobj_to_dsname(zfs_cmd_t
*zc
)
1689 return (dsl_dsobj_to_dsname(zc
->zc_name
, zc
->zc_obj
, zc
->zc_value
));
1694 * zc_name name of filesystem
1695 * zc_obj object to find
1698 * zc_value name of object
1701 zfs_ioc_obj_to_path(zfs_cmd_t
*zc
)
1706 /* XXX reading from objset not owned */
1707 if ((error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
)) != 0)
1709 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
1710 dmu_objset_rele(os
, FTAG
);
1711 return (SET_ERROR(EINVAL
));
1713 error
= zfs_obj_to_path(os
, zc
->zc_obj
, zc
->zc_value
,
1714 sizeof (zc
->zc_value
));
1715 dmu_objset_rele(os
, FTAG
);
1722 * zc_name name of filesystem
1723 * zc_obj object to find
1726 * zc_stat stats on object
1727 * zc_value path to object
1730 zfs_ioc_obj_to_stats(zfs_cmd_t
*zc
)
1735 /* XXX reading from objset not owned */
1736 if ((error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
)) != 0)
1738 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
1739 dmu_objset_rele(os
, FTAG
);
1740 return (SET_ERROR(EINVAL
));
1742 error
= zfs_obj_to_stats(os
, zc
->zc_obj
, &zc
->zc_stat
, zc
->zc_value
,
1743 sizeof (zc
->zc_value
));
1744 dmu_objset_rele(os
, FTAG
);
1750 zfs_ioc_vdev_add(zfs_cmd_t
*zc
)
1754 nvlist_t
*config
, **l2cache
, **spares
;
1755 uint_t nl2cache
= 0, nspares
= 0;
1757 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1761 error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1762 zc
->zc_iflags
, &config
);
1763 (void) nvlist_lookup_nvlist_array(config
, ZPOOL_CONFIG_L2CACHE
,
1764 &l2cache
, &nl2cache
);
1766 (void) nvlist_lookup_nvlist_array(config
, ZPOOL_CONFIG_SPARES
,
1770 * A root pool with concatenated devices is not supported.
1771 * Thus, can not add a device to a root pool.
1773 * Intent log device can not be added to a rootpool because
1774 * during mountroot, zil is replayed, a seperated log device
1775 * can not be accessed during the mountroot time.
1777 * l2cache and spare devices are ok to be added to a rootpool.
1779 if (spa_bootfs(spa
) != 0 && nl2cache
== 0 && nspares
== 0) {
1780 nvlist_free(config
);
1781 spa_close(spa
, FTAG
);
1782 return (SET_ERROR(EDOM
));
1786 error
= spa_vdev_add(spa
, config
);
1787 nvlist_free(config
);
1789 spa_close(spa
, FTAG
);
1795 * zc_name name of the pool
1796 * zc_nvlist_conf nvlist of devices to remove
1797 * zc_cookie to stop the remove?
1800 zfs_ioc_vdev_remove(zfs_cmd_t
*zc
)
1805 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1808 error
= spa_vdev_remove(spa
, zc
->zc_guid
, B_FALSE
);
1809 spa_close(spa
, FTAG
);
1814 zfs_ioc_vdev_set_state(zfs_cmd_t
*zc
)
1818 vdev_state_t newstate
= VDEV_STATE_UNKNOWN
;
1820 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1822 switch (zc
->zc_cookie
) {
1823 case VDEV_STATE_ONLINE
:
1824 error
= vdev_online(spa
, zc
->zc_guid
, zc
->zc_obj
, &newstate
);
1827 case VDEV_STATE_OFFLINE
:
1828 error
= vdev_offline(spa
, zc
->zc_guid
, zc
->zc_obj
);
1831 case VDEV_STATE_FAULTED
:
1832 if (zc
->zc_obj
!= VDEV_AUX_ERR_EXCEEDED
&&
1833 zc
->zc_obj
!= VDEV_AUX_EXTERNAL
)
1834 zc
->zc_obj
= VDEV_AUX_ERR_EXCEEDED
;
1836 error
= vdev_fault(spa
, zc
->zc_guid
, zc
->zc_obj
);
1839 case VDEV_STATE_DEGRADED
:
1840 if (zc
->zc_obj
!= VDEV_AUX_ERR_EXCEEDED
&&
1841 zc
->zc_obj
!= VDEV_AUX_EXTERNAL
)
1842 zc
->zc_obj
= VDEV_AUX_ERR_EXCEEDED
;
1844 error
= vdev_degrade(spa
, zc
->zc_guid
, zc
->zc_obj
);
1848 error
= SET_ERROR(EINVAL
);
1850 zc
->zc_cookie
= newstate
;
1851 spa_close(spa
, FTAG
);
1856 zfs_ioc_vdev_attach(zfs_cmd_t
*zc
)
1859 int replacing
= zc
->zc_cookie
;
1863 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1866 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1867 zc
->zc_iflags
, &config
)) == 0) {
1868 error
= spa_vdev_attach(spa
, zc
->zc_guid
, config
, replacing
);
1869 nvlist_free(config
);
1872 spa_close(spa
, FTAG
);
1877 zfs_ioc_vdev_detach(zfs_cmd_t
*zc
)
1882 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1885 error
= spa_vdev_detach(spa
, zc
->zc_guid
, 0, B_FALSE
);
1887 spa_close(spa
, FTAG
);
1892 zfs_ioc_vdev_split(zfs_cmd_t
*zc
)
1895 nvlist_t
*config
, *props
= NULL
;
1897 boolean_t exp
= !!(zc
->zc_cookie
& ZPOOL_EXPORT_AFTER_SPLIT
);
1899 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1902 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1903 zc
->zc_iflags
, &config
))) {
1904 spa_close(spa
, FTAG
);
1908 if (zc
->zc_nvlist_src_size
!= 0 && (error
=
1909 get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1910 zc
->zc_iflags
, &props
))) {
1911 spa_close(spa
, FTAG
);
1912 nvlist_free(config
);
1916 error
= spa_vdev_split_mirror(spa
, zc
->zc_string
, config
, props
, exp
);
1918 spa_close(spa
, FTAG
);
1920 nvlist_free(config
);
1927 zfs_ioc_vdev_setpath(zfs_cmd_t
*zc
)
1930 char *path
= zc
->zc_value
;
1931 uint64_t guid
= zc
->zc_guid
;
1934 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1938 error
= spa_vdev_setpath(spa
, guid
, path
);
1939 spa_close(spa
, FTAG
);
1944 zfs_ioc_vdev_setfru(zfs_cmd_t
*zc
)
1947 char *fru
= zc
->zc_value
;
1948 uint64_t guid
= zc
->zc_guid
;
1951 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1955 error
= spa_vdev_setfru(spa
, guid
, fru
);
1956 spa_close(spa
, FTAG
);
1961 zfs_ioc_objset_stats_impl(zfs_cmd_t
*zc
, objset_t
*os
)
1966 dmu_objset_fast_stat(os
, &zc
->zc_objset_stats
);
1968 if (zc
->zc_nvlist_dst
!= 0 &&
1969 (error
= dsl_prop_get_all(os
, &nv
)) == 0) {
1970 dmu_objset_stats(os
, nv
);
1972 * NB: zvol_get_stats() will read the objset contents,
1973 * which we aren't supposed to do with a
1974 * DS_MODE_USER hold, because it could be
1975 * inconsistent. So this is a bit of a workaround...
1976 * XXX reading with out owning
1978 if (!zc
->zc_objset_stats
.dds_inconsistent
&&
1979 dmu_objset_type(os
) == DMU_OST_ZVOL
) {
1980 error
= zvol_get_stats(os
, nv
);
1986 error
= put_nvlist(zc
, nv
);
1995 * zc_name name of filesystem
1996 * zc_nvlist_dst_size size of buffer for property nvlist
1999 * zc_objset_stats stats
2000 * zc_nvlist_dst property nvlist
2001 * zc_nvlist_dst_size size of property nvlist
2004 zfs_ioc_objset_stats(zfs_cmd_t
*zc
)
2009 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
2011 error
= zfs_ioc_objset_stats_impl(zc
, os
);
2012 dmu_objset_rele(os
, FTAG
);
2020 * zc_name name of filesystem
2021 * zc_nvlist_dst_size size of buffer for property nvlist
2024 * zc_nvlist_dst received property nvlist
2025 * zc_nvlist_dst_size size of received property nvlist
2027 * Gets received properties (distinct from local properties on or after
2028 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
2029 * local property values.
2032 zfs_ioc_objset_recvd_props(zfs_cmd_t
*zc
)
2038 * Without this check, we would return local property values if the
2039 * caller has not already received properties on or after
2040 * SPA_VERSION_RECVD_PROPS.
2042 if (!dsl_prop_get_hasrecvd(zc
->zc_name
))
2043 return (SET_ERROR(ENOTSUP
));
2045 if (zc
->zc_nvlist_dst
!= 0 &&
2046 (error
= dsl_prop_get_received(zc
->zc_name
, &nv
)) == 0) {
2047 error
= put_nvlist(zc
, nv
);
2055 nvl_add_zplprop(objset_t
*os
, nvlist_t
*props
, zfs_prop_t prop
)
2061 * zfs_get_zplprop() will either find a value or give us
2062 * the default value (if there is one).
2064 if ((error
= zfs_get_zplprop(os
, prop
, &value
)) != 0)
2066 VERIFY(nvlist_add_uint64(props
, zfs_prop_to_name(prop
), value
) == 0);
2072 * zc_name name of filesystem
2073 * zc_nvlist_dst_size size of buffer for zpl property nvlist
2076 * zc_nvlist_dst zpl property nvlist
2077 * zc_nvlist_dst_size size of zpl property nvlist
2080 zfs_ioc_objset_zplprops(zfs_cmd_t
*zc
)
2085 /* XXX reading without owning */
2086 if ((err
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
)))
2089 dmu_objset_fast_stat(os
, &zc
->zc_objset_stats
);
2092 * NB: nvl_add_zplprop() will read the objset contents,
2093 * which we aren't supposed to do with a DS_MODE_USER
2094 * hold, because it could be inconsistent.
2096 if (zc
->zc_nvlist_dst
!= 0 &&
2097 !zc
->zc_objset_stats
.dds_inconsistent
&&
2098 dmu_objset_type(os
) == DMU_OST_ZFS
) {
2101 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2102 if ((err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_VERSION
)) == 0 &&
2103 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_NORMALIZE
)) == 0 &&
2104 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_UTF8ONLY
)) == 0 &&
2105 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_CASE
)) == 0)
2106 err
= put_nvlist(zc
, nv
);
2109 err
= SET_ERROR(ENOENT
);
2111 dmu_objset_rele(os
, FTAG
);
2116 dataset_name_hidden(const char *name
)
2119 * Skip over datasets that are not visible in this zone,
2120 * internal datasets (which have a $ in their name), and
2121 * temporary datasets (which have a % in their name).
2123 if (strchr(name
, '$') != NULL
)
2125 if (strchr(name
, '%') != NULL
)
2127 if (!INGLOBALZONE(curproc
) && !zone_dataset_visible(name
, NULL
))
2134 * zc_name name of filesystem
2135 * zc_cookie zap cursor
2136 * zc_nvlist_dst_size size of buffer for property nvlist
2139 * zc_name name of next filesystem
2140 * zc_cookie zap cursor
2141 * zc_objset_stats stats
2142 * zc_nvlist_dst property nvlist
2143 * zc_nvlist_dst_size size of property nvlist
2146 zfs_ioc_dataset_list_next(zfs_cmd_t
*zc
)
2151 size_t orig_len
= strlen(zc
->zc_name
);
2154 if ((error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
))) {
2155 if (error
== ENOENT
)
2156 error
= SET_ERROR(ESRCH
);
2160 p
= strrchr(zc
->zc_name
, '/');
2161 if (p
== NULL
|| p
[1] != '\0')
2162 (void) strlcat(zc
->zc_name
, "/", sizeof (zc
->zc_name
));
2163 p
= zc
->zc_name
+ strlen(zc
->zc_name
);
2166 error
= dmu_dir_list_next(os
,
2167 sizeof (zc
->zc_name
) - (p
- zc
->zc_name
), p
,
2168 NULL
, &zc
->zc_cookie
);
2169 if (error
== ENOENT
)
2170 error
= SET_ERROR(ESRCH
);
2171 } while (error
== 0 && dataset_name_hidden(zc
->zc_name
));
2172 dmu_objset_rele(os
, FTAG
);
2175 * If it's an internal dataset (ie. with a '$' in its name),
2176 * don't try to get stats for it, otherwise we'll return ENOENT.
2178 if (error
== 0 && strchr(zc
->zc_name
, '$') == NULL
) {
2179 error
= zfs_ioc_objset_stats(zc
); /* fill in the stats */
2180 if (error
== ENOENT
) {
2181 /* We lost a race with destroy, get the next one. */
2182 zc
->zc_name
[orig_len
] = '\0';
2191 * zc_name name of filesystem
2192 * zc_cookie zap cursor
2193 * zc_nvlist_dst_size size of buffer for property nvlist
2196 * zc_name name of next snapshot
2197 * zc_objset_stats stats
2198 * zc_nvlist_dst property nvlist
2199 * zc_nvlist_dst_size size of property nvlist
2202 zfs_ioc_snapshot_list_next(zfs_cmd_t
*zc
)
2207 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
2209 return (error
== ENOENT
? ESRCH
: error
);
2213 * A dataset name of maximum length cannot have any snapshots,
2214 * so exit immediately.
2216 if (strlcat(zc
->zc_name
, "@", sizeof (zc
->zc_name
)) >= MAXNAMELEN
) {
2217 dmu_objset_rele(os
, FTAG
);
2218 return (SET_ERROR(ESRCH
));
2221 error
= dmu_snapshot_list_next(os
,
2222 sizeof (zc
->zc_name
) - strlen(zc
->zc_name
),
2223 zc
->zc_name
+ strlen(zc
->zc_name
), &zc
->zc_obj
, &zc
->zc_cookie
,
2226 if (error
== 0 && !zc
->zc_simple
) {
2228 dsl_pool_t
*dp
= os
->os_dsl_dataset
->ds_dir
->dd_pool
;
2230 error
= dsl_dataset_hold_obj(dp
, zc
->zc_obj
, FTAG
, &ds
);
2234 error
= dmu_objset_from_ds(ds
, &ossnap
);
2236 error
= zfs_ioc_objset_stats_impl(zc
, ossnap
);
2237 dsl_dataset_rele(ds
, FTAG
);
2239 } else if (error
== ENOENT
) {
2240 error
= SET_ERROR(ESRCH
);
2243 dmu_objset_rele(os
, FTAG
);
2244 /* if we failed, undo the @ that we tacked on to zc_name */
2246 *strchr(zc
->zc_name
, '@') = '\0';
2251 zfs_prop_set_userquota(const char *dsname
, nvpair_t
*pair
)
2253 const char *propname
= nvpair_name(pair
);
2255 unsigned int vallen
;
2258 zfs_userquota_prop_t type
;
2264 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2266 VERIFY(nvpair_value_nvlist(pair
, &attrs
) == 0);
2267 if (nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
2269 return (SET_ERROR(EINVAL
));
2273 * A correctly constructed propname is encoded as
2274 * userquota@<rid>-<domain>.
2276 if ((dash
= strchr(propname
, '-')) == NULL
||
2277 nvpair_value_uint64_array(pair
, &valary
, &vallen
) != 0 ||
2279 return (SET_ERROR(EINVAL
));
2286 err
= zfs_sb_hold(dsname
, FTAG
, &zsb
, B_FALSE
);
2288 err
= zfs_set_userquota(zsb
, type
, domain
, rid
, quota
);
2289 zfs_sb_rele(zsb
, FTAG
);
2296 * If the named property is one that has a special function to set its value,
2297 * return 0 on success and a positive error code on failure; otherwise if it is
2298 * not one of the special properties handled by this function, return -1.
2300 * XXX: It would be better for callers of the property interface if we handled
2301 * these special cases in dsl_prop.c (in the dsl layer).
2304 zfs_prop_set_special(const char *dsname
, zprop_source_t source
,
2307 const char *propname
= nvpair_name(pair
);
2308 zfs_prop_t prop
= zfs_name_to_prop(propname
);
2312 if (prop
== ZPROP_INVAL
) {
2313 if (zfs_prop_userquota(propname
))
2314 return (zfs_prop_set_userquota(dsname
, pair
));
2318 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2320 VERIFY(nvpair_value_nvlist(pair
, &attrs
) == 0);
2321 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
2325 if (zfs_prop_get_type(prop
) == PROP_TYPE_STRING
)
2328 VERIFY(0 == nvpair_value_uint64(pair
, &intval
));
2331 case ZFS_PROP_QUOTA
:
2332 err
= dsl_dir_set_quota(dsname
, source
, intval
);
2334 case ZFS_PROP_REFQUOTA
:
2335 err
= dsl_dataset_set_refquota(dsname
, source
, intval
);
2337 case ZFS_PROP_RESERVATION
:
2338 err
= dsl_dir_set_reservation(dsname
, source
, intval
);
2340 case ZFS_PROP_REFRESERVATION
:
2341 err
= dsl_dataset_set_refreservation(dsname
, source
, intval
);
2343 case ZFS_PROP_VOLSIZE
:
2344 err
= zvol_set_volsize(dsname
, intval
);
2346 case ZFS_PROP_SNAPDEV
:
2347 err
= zvol_set_snapdev(dsname
, intval
);
2349 case ZFS_PROP_VERSION
:
2353 if ((err
= zfs_sb_hold(dsname
, FTAG
, &zsb
, B_TRUE
)) != 0)
2356 err
= zfs_set_version(zsb
, intval
);
2357 zfs_sb_rele(zsb
, FTAG
);
2359 if (err
== 0 && intval
>= ZPL_VERSION_USERSPACE
) {
2362 zc
= kmem_zalloc(sizeof (zfs_cmd_t
),
2363 KM_SLEEP
| KM_NODEBUG
);
2364 (void) strcpy(zc
->zc_name
, dsname
);
2365 (void) zfs_ioc_userspace_upgrade(zc
);
2366 kmem_free(zc
, sizeof (zfs_cmd_t
));
2370 case ZFS_PROP_COMPRESSION
:
2372 if (intval
== ZIO_COMPRESS_LZ4
) {
2373 zfeature_info_t
*feature
=
2374 &spa_feature_table
[SPA_FEATURE_LZ4_COMPRESS
];
2377 if ((err
= spa_open(dsname
, &spa
, FTAG
)) != 0)
2381 * Setting the LZ4 compression algorithm activates
2384 if (!spa_feature_is_active(spa
, feature
)) {
2385 if ((err
= zfs_prop_activate_feature(spa
,
2387 spa_close(spa
, FTAG
);
2392 spa_close(spa
, FTAG
);
2395 * We still want the default set action to be performed in the
2396 * caller, we only performed zfeature settings here.
2410 * This function is best effort. If it fails to set any of the given properties,
2411 * it continues to set as many as it can and returns the last error
2412 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2413 * with the list of names of all the properties that failed along with the
2414 * corresponding error numbers.
2416 * If every property is set successfully, zero is returned and errlist is not
2420 zfs_set_prop_nvlist(const char *dsname
, zprop_source_t source
, nvlist_t
*nvl
,
2429 nvlist_t
*genericnvl
= fnvlist_alloc();
2430 nvlist_t
*retrynvl
= fnvlist_alloc();
2433 while ((pair
= nvlist_next_nvpair(nvl
, pair
)) != NULL
) {
2434 const char *propname
= nvpair_name(pair
);
2435 zfs_prop_t prop
= zfs_name_to_prop(propname
);
2438 /* decode the property value */
2440 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2442 attrs
= fnvpair_value_nvlist(pair
);
2443 if (nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
2445 err
= SET_ERROR(EINVAL
);
2448 /* Validate value type */
2449 if (err
== 0 && prop
== ZPROP_INVAL
) {
2450 if (zfs_prop_user(propname
)) {
2451 if (nvpair_type(propval
) != DATA_TYPE_STRING
)
2452 err
= SET_ERROR(EINVAL
);
2453 } else if (zfs_prop_userquota(propname
)) {
2454 if (nvpair_type(propval
) !=
2455 DATA_TYPE_UINT64_ARRAY
)
2456 err
= SET_ERROR(EINVAL
);
2458 err
= SET_ERROR(EINVAL
);
2460 } else if (err
== 0) {
2461 if (nvpair_type(propval
) == DATA_TYPE_STRING
) {
2462 if (zfs_prop_get_type(prop
) != PROP_TYPE_STRING
)
2463 err
= SET_ERROR(EINVAL
);
2464 } else if (nvpair_type(propval
) == DATA_TYPE_UINT64
) {
2467 intval
= fnvpair_value_uint64(propval
);
2469 switch (zfs_prop_get_type(prop
)) {
2470 case PROP_TYPE_NUMBER
:
2472 case PROP_TYPE_STRING
:
2473 err
= SET_ERROR(EINVAL
);
2475 case PROP_TYPE_INDEX
:
2476 if (zfs_prop_index_to_string(prop
,
2477 intval
, &unused
) != 0)
2478 err
= SET_ERROR(EINVAL
);
2482 "unknown property type");
2485 err
= SET_ERROR(EINVAL
);
2489 /* Validate permissions */
2491 err
= zfs_check_settable(dsname
, pair
, CRED());
2494 err
= zfs_prop_set_special(dsname
, source
, pair
);
2497 * For better performance we build up a list of
2498 * properties to set in a single transaction.
2500 err
= nvlist_add_nvpair(genericnvl
, pair
);
2501 } else if (err
!= 0 && nvl
!= retrynvl
) {
2503 * This may be a spurious error caused by
2504 * receiving quota and reservation out of order.
2505 * Try again in a second pass.
2507 err
= nvlist_add_nvpair(retrynvl
, pair
);
2512 if (errlist
!= NULL
)
2513 fnvlist_add_int32(errlist
, propname
, err
);
2518 if (nvl
!= retrynvl
&& !nvlist_empty(retrynvl
)) {
2523 if (!nvlist_empty(genericnvl
) &&
2524 dsl_props_set(dsname
, source
, genericnvl
) != 0) {
2526 * If this fails, we still want to set as many properties as we
2527 * can, so try setting them individually.
2530 while ((pair
= nvlist_next_nvpair(genericnvl
, pair
)) != NULL
) {
2531 const char *propname
= nvpair_name(pair
);
2535 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2537 attrs
= fnvpair_value_nvlist(pair
);
2538 propval
= fnvlist_lookup_nvpair(attrs
,
2542 if (nvpair_type(propval
) == DATA_TYPE_STRING
) {
2543 strval
= fnvpair_value_string(propval
);
2544 err
= dsl_prop_set_string(dsname
, propname
,
2547 intval
= fnvpair_value_uint64(propval
);
2548 err
= dsl_prop_set_int(dsname
, propname
, source
,
2553 if (errlist
!= NULL
) {
2554 fnvlist_add_int32(errlist
, propname
,
2561 nvlist_free(genericnvl
);
2562 nvlist_free(retrynvl
);
2568 * Check that all the properties are valid user properties.
2571 zfs_check_userprops(const char *fsname
, nvlist_t
*nvl
)
2573 nvpair_t
*pair
= NULL
;
2576 while ((pair
= nvlist_next_nvpair(nvl
, pair
)) != NULL
) {
2577 const char *propname
= nvpair_name(pair
);
2580 if (!zfs_prop_user(propname
) ||
2581 nvpair_type(pair
) != DATA_TYPE_STRING
)
2582 return (SET_ERROR(EINVAL
));
2584 if ((error
= zfs_secpolicy_write_perms(fsname
,
2585 ZFS_DELEG_PERM_USERPROP
, CRED())))
2588 if (strlen(propname
) >= ZAP_MAXNAMELEN
)
2589 return (SET_ERROR(ENAMETOOLONG
));
2591 VERIFY(nvpair_value_string(pair
, &valstr
) == 0);
2592 if (strlen(valstr
) >= ZAP_MAXVALUELEN
)
2593 return (SET_ERROR(E2BIG
));
2599 props_skip(nvlist_t
*props
, nvlist_t
*skipped
, nvlist_t
**newprops
)
2603 VERIFY(nvlist_alloc(newprops
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2606 while ((pair
= nvlist_next_nvpair(props
, pair
)) != NULL
) {
2607 if (nvlist_exists(skipped
, nvpair_name(pair
)))
2610 VERIFY(nvlist_add_nvpair(*newprops
, pair
) == 0);
2615 clear_received_props(const char *dsname
, nvlist_t
*props
,
2619 nvlist_t
*cleared_props
= NULL
;
2620 props_skip(props
, skipped
, &cleared_props
);
2621 if (!nvlist_empty(cleared_props
)) {
2623 * Acts on local properties until the dataset has received
2624 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2626 zprop_source_t flags
= (ZPROP_SRC_NONE
|
2627 (dsl_prop_get_hasrecvd(dsname
) ? ZPROP_SRC_RECEIVED
: 0));
2628 err
= zfs_set_prop_nvlist(dsname
, flags
, cleared_props
, NULL
);
2630 nvlist_free(cleared_props
);
2636 * zc_name name of filesystem
2637 * zc_value name of property to set
2638 * zc_nvlist_src{_size} nvlist of properties to apply
2639 * zc_cookie received properties flag
2642 * zc_nvlist_dst{_size} error for each unapplied received property
2645 zfs_ioc_set_prop(zfs_cmd_t
*zc
)
2648 boolean_t received
= zc
->zc_cookie
;
2649 zprop_source_t source
= (received
? ZPROP_SRC_RECEIVED
:
2654 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2655 zc
->zc_iflags
, &nvl
)) != 0)
2659 nvlist_t
*origprops
;
2661 if (dsl_prop_get_received(zc
->zc_name
, &origprops
) == 0) {
2662 (void) clear_received_props(zc
->zc_name
,
2664 nvlist_free(origprops
);
2667 error
= dsl_prop_set_hasrecvd(zc
->zc_name
);
2670 errors
= fnvlist_alloc();
2672 error
= zfs_set_prop_nvlist(zc
->zc_name
, source
, nvl
, errors
);
2674 if (zc
->zc_nvlist_dst
!= 0 && errors
!= NULL
) {
2675 (void) put_nvlist(zc
, errors
);
2678 nvlist_free(errors
);
2685 * zc_name name of filesystem
2686 * zc_value name of property to inherit
2687 * zc_cookie revert to received value if TRUE
2692 zfs_ioc_inherit_prop(zfs_cmd_t
*zc
)
2694 const char *propname
= zc
->zc_value
;
2695 zfs_prop_t prop
= zfs_name_to_prop(propname
);
2696 boolean_t received
= zc
->zc_cookie
;
2697 zprop_source_t source
= (received
2698 ? ZPROP_SRC_NONE
/* revert to received value, if any */
2699 : ZPROP_SRC_INHERITED
); /* explicitly inherit */
2708 * zfs_prop_set_special() expects properties in the form of an
2709 * nvpair with type info.
2711 if (prop
== ZPROP_INVAL
) {
2712 if (!zfs_prop_user(propname
))
2713 return (SET_ERROR(EINVAL
));
2715 type
= PROP_TYPE_STRING
;
2716 } else if (prop
== ZFS_PROP_VOLSIZE
||
2717 prop
== ZFS_PROP_VERSION
) {
2718 return (SET_ERROR(EINVAL
));
2720 type
= zfs_prop_get_type(prop
);
2723 VERIFY(nvlist_alloc(&dummy
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2726 case PROP_TYPE_STRING
:
2727 VERIFY(0 == nvlist_add_string(dummy
, propname
, ""));
2729 case PROP_TYPE_NUMBER
:
2730 case PROP_TYPE_INDEX
:
2731 VERIFY(0 == nvlist_add_uint64(dummy
, propname
, 0));
2735 return (SET_ERROR(EINVAL
));
2738 pair
= nvlist_next_nvpair(dummy
, NULL
);
2739 err
= zfs_prop_set_special(zc
->zc_name
, source
, pair
);
2742 return (err
); /* special property already handled */
2745 * Only check this in the non-received case. We want to allow
2746 * 'inherit -S' to revert non-inheritable properties like quota
2747 * and reservation to the received or default values even though
2748 * they are not considered inheritable.
2750 if (prop
!= ZPROP_INVAL
&& !zfs_prop_inheritable(prop
))
2751 return (SET_ERROR(EINVAL
));
2754 /* property name has been validated by zfs_secpolicy_inherit_prop() */
2755 return (dsl_prop_inherit(zc
->zc_name
, zc
->zc_value
, source
));
2759 zfs_ioc_pool_set_props(zfs_cmd_t
*zc
)
2766 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2767 zc
->zc_iflags
, &props
)))
2771 * If the only property is the configfile, then just do a spa_lookup()
2772 * to handle the faulted case.
2774 pair
= nvlist_next_nvpair(props
, NULL
);
2775 if (pair
!= NULL
&& strcmp(nvpair_name(pair
),
2776 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
)) == 0 &&
2777 nvlist_next_nvpair(props
, pair
) == NULL
) {
2778 mutex_enter(&spa_namespace_lock
);
2779 if ((spa
= spa_lookup(zc
->zc_name
)) != NULL
) {
2780 spa_configfile_set(spa
, props
, B_FALSE
);
2781 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
2783 mutex_exit(&spa_namespace_lock
);
2790 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0) {
2795 error
= spa_prop_set(spa
, props
);
2798 spa_close(spa
, FTAG
);
2804 zfs_ioc_pool_get_props(zfs_cmd_t
*zc
)
2808 nvlist_t
*nvp
= NULL
;
2810 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0) {
2812 * If the pool is faulted, there may be properties we can still
2813 * get (such as altroot and cachefile), so attempt to get them
2816 mutex_enter(&spa_namespace_lock
);
2817 if ((spa
= spa_lookup(zc
->zc_name
)) != NULL
)
2818 error
= spa_prop_get(spa
, &nvp
);
2819 mutex_exit(&spa_namespace_lock
);
2821 error
= spa_prop_get(spa
, &nvp
);
2822 spa_close(spa
, FTAG
);
2825 if (error
== 0 && zc
->zc_nvlist_dst
!= 0)
2826 error
= put_nvlist(zc
, nvp
);
2828 error
= SET_ERROR(EFAULT
);
2836 * zc_name name of volume
2841 zfs_ioc_create_minor(zfs_cmd_t
*zc
)
2843 return (zvol_create_minor(zc
->zc_name
));
2848 * zc_name name of volume
2853 zfs_ioc_remove_minor(zfs_cmd_t
*zc
)
2855 return (zvol_remove_minor(zc
->zc_name
));
2860 * zc_name name of filesystem
2861 * zc_nvlist_src{_size} nvlist of delegated permissions
2862 * zc_perm_action allow/unallow flag
2867 zfs_ioc_set_fsacl(zfs_cmd_t
*zc
)
2870 nvlist_t
*fsaclnv
= NULL
;
2872 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2873 zc
->zc_iflags
, &fsaclnv
)) != 0)
2877 * Verify nvlist is constructed correctly
2879 if ((error
= zfs_deleg_verify_nvlist(fsaclnv
)) != 0) {
2880 nvlist_free(fsaclnv
);
2881 return (SET_ERROR(EINVAL
));
2885 * If we don't have PRIV_SYS_MOUNT, then validate
2886 * that user is allowed to hand out each permission in
2890 error
= secpolicy_zfs(CRED());
2892 if (zc
->zc_perm_action
== B_FALSE
) {
2893 error
= dsl_deleg_can_allow(zc
->zc_name
,
2896 error
= dsl_deleg_can_unallow(zc
->zc_name
,
2902 error
= dsl_deleg_set(zc
->zc_name
, fsaclnv
, zc
->zc_perm_action
);
2904 nvlist_free(fsaclnv
);
2910 * zc_name name of filesystem
2913 * zc_nvlist_src{_size} nvlist of delegated permissions
2916 zfs_ioc_get_fsacl(zfs_cmd_t
*zc
)
2921 if ((error
= dsl_deleg_get(zc
->zc_name
, &nvp
)) == 0) {
2922 error
= put_nvlist(zc
, nvp
);
2931 zfs_create_cb(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
)
2933 zfs_creat_t
*zct
= arg
;
2935 zfs_create_fs(os
, cr
, zct
->zct_zplprops
, tx
);
2938 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
2942 * os parent objset pointer (NULL if root fs)
2943 * fuids_ok fuids allowed in this version of the spa?
2944 * sa_ok SAs allowed in this version of the spa?
2945 * createprops list of properties requested by creator
2948 * zplprops values for the zplprops we attach to the master node object
2949 * is_ci true if requested file system will be purely case-insensitive
2951 * Determine the settings for utf8only, normalization and
2952 * casesensitivity. Specific values may have been requested by the
2953 * creator and/or we can inherit values from the parent dataset. If
2954 * the file system is of too early a vintage, a creator can not
2955 * request settings for these properties, even if the requested
2956 * setting is the default value. We don't actually want to create dsl
2957 * properties for these, so remove them from the source nvlist after
2961 zfs_fill_zplprops_impl(objset_t
*os
, uint64_t zplver
,
2962 boolean_t fuids_ok
, boolean_t sa_ok
, nvlist_t
*createprops
,
2963 nvlist_t
*zplprops
, boolean_t
*is_ci
)
2965 uint64_t sense
= ZFS_PROP_UNDEFINED
;
2966 uint64_t norm
= ZFS_PROP_UNDEFINED
;
2967 uint64_t u8
= ZFS_PROP_UNDEFINED
;
2970 ASSERT(zplprops
!= NULL
);
2973 * Pull out creator prop choices, if any.
2976 (void) nvlist_lookup_uint64(createprops
,
2977 zfs_prop_to_name(ZFS_PROP_VERSION
), &zplver
);
2978 (void) nvlist_lookup_uint64(createprops
,
2979 zfs_prop_to_name(ZFS_PROP_NORMALIZE
), &norm
);
2980 (void) nvlist_remove_all(createprops
,
2981 zfs_prop_to_name(ZFS_PROP_NORMALIZE
));
2982 (void) nvlist_lookup_uint64(createprops
,
2983 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), &u8
);
2984 (void) nvlist_remove_all(createprops
,
2985 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
));
2986 (void) nvlist_lookup_uint64(createprops
,
2987 zfs_prop_to_name(ZFS_PROP_CASE
), &sense
);
2988 (void) nvlist_remove_all(createprops
,
2989 zfs_prop_to_name(ZFS_PROP_CASE
));
2993 * If the zpl version requested is whacky or the file system
2994 * or pool is version is too "young" to support normalization
2995 * and the creator tried to set a value for one of the props,
2998 if ((zplver
< ZPL_VERSION_INITIAL
|| zplver
> ZPL_VERSION
) ||
2999 (zplver
>= ZPL_VERSION_FUID
&& !fuids_ok
) ||
3000 (zplver
>= ZPL_VERSION_SA
&& !sa_ok
) ||
3001 (zplver
< ZPL_VERSION_NORMALIZATION
&&
3002 (norm
!= ZFS_PROP_UNDEFINED
|| u8
!= ZFS_PROP_UNDEFINED
||
3003 sense
!= ZFS_PROP_UNDEFINED
)))
3004 return (SET_ERROR(ENOTSUP
));
3007 * Put the version in the zplprops
3009 VERIFY(nvlist_add_uint64(zplprops
,
3010 zfs_prop_to_name(ZFS_PROP_VERSION
), zplver
) == 0);
3012 if (norm
== ZFS_PROP_UNDEFINED
&&
3013 (error
= zfs_get_zplprop(os
, ZFS_PROP_NORMALIZE
, &norm
)) != 0)
3015 VERIFY(nvlist_add_uint64(zplprops
,
3016 zfs_prop_to_name(ZFS_PROP_NORMALIZE
), norm
) == 0);
3019 * If we're normalizing, names must always be valid UTF-8 strings.
3023 if (u8
== ZFS_PROP_UNDEFINED
&&
3024 (error
= zfs_get_zplprop(os
, ZFS_PROP_UTF8ONLY
, &u8
)) != 0)
3026 VERIFY(nvlist_add_uint64(zplprops
,
3027 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), u8
) == 0);
3029 if (sense
== ZFS_PROP_UNDEFINED
&&
3030 (error
= zfs_get_zplprop(os
, ZFS_PROP_CASE
, &sense
)) != 0)
3032 VERIFY(nvlist_add_uint64(zplprops
,
3033 zfs_prop_to_name(ZFS_PROP_CASE
), sense
) == 0);
3036 *is_ci
= (sense
== ZFS_CASE_INSENSITIVE
);
3042 zfs_fill_zplprops(const char *dataset
, nvlist_t
*createprops
,
3043 nvlist_t
*zplprops
, boolean_t
*is_ci
)
3045 boolean_t fuids_ok
, sa_ok
;
3046 uint64_t zplver
= ZPL_VERSION
;
3047 objset_t
*os
= NULL
;
3048 char parentname
[MAXNAMELEN
];
3054 (void) strlcpy(parentname
, dataset
, sizeof (parentname
));
3055 cp
= strrchr(parentname
, '/');
3059 if ((error
= spa_open(dataset
, &spa
, FTAG
)) != 0)
3062 spa_vers
= spa_version(spa
);
3063 spa_close(spa
, FTAG
);
3065 zplver
= zfs_zpl_version_map(spa_vers
);
3066 fuids_ok
= (zplver
>= ZPL_VERSION_FUID
);
3067 sa_ok
= (zplver
>= ZPL_VERSION_SA
);
3070 * Open parent object set so we can inherit zplprop values.
3072 if ((error
= dmu_objset_hold(parentname
, FTAG
, &os
)) != 0)
3075 error
= zfs_fill_zplprops_impl(os
, zplver
, fuids_ok
, sa_ok
, createprops
,
3077 dmu_objset_rele(os
, FTAG
);
3082 zfs_fill_zplprops_root(uint64_t spa_vers
, nvlist_t
*createprops
,
3083 nvlist_t
*zplprops
, boolean_t
*is_ci
)
3087 uint64_t zplver
= ZPL_VERSION
;
3090 zplver
= zfs_zpl_version_map(spa_vers
);
3091 fuids_ok
= (zplver
>= ZPL_VERSION_FUID
);
3092 sa_ok
= (zplver
>= ZPL_VERSION_SA
);
3094 error
= zfs_fill_zplprops_impl(NULL
, zplver
, fuids_ok
, sa_ok
,
3095 createprops
, zplprops
, is_ci
);
3101 * "type" -> dmu_objset_type_t (int32)
3102 * (optional) "props" -> { prop -> value }
3105 * outnvl: propname -> error code (int32)
3108 zfs_ioc_create(const char *fsname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3111 zfs_creat_t zct
= { 0 };
3112 nvlist_t
*nvprops
= NULL
;
3113 void (*cbfunc
)(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
);
3115 dmu_objset_type_t type
;
3116 boolean_t is_insensitive
= B_FALSE
;
3118 if (nvlist_lookup_int32(innvl
, "type", &type32
) != 0)
3119 return (SET_ERROR(EINVAL
));
3121 (void) nvlist_lookup_nvlist(innvl
, "props", &nvprops
);
3125 cbfunc
= zfs_create_cb
;
3129 cbfunc
= zvol_create_cb
;
3136 if (strchr(fsname
, '@') ||
3137 strchr(fsname
, '%'))
3138 return (SET_ERROR(EINVAL
));
3140 zct
.zct_props
= nvprops
;
3143 return (SET_ERROR(EINVAL
));
3145 if (type
== DMU_OST_ZVOL
) {
3146 uint64_t volsize
, volblocksize
;
3148 if (nvprops
== NULL
)
3149 return (SET_ERROR(EINVAL
));
3150 if (nvlist_lookup_uint64(nvprops
,
3151 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &volsize
) != 0)
3152 return (SET_ERROR(EINVAL
));
3154 if ((error
= nvlist_lookup_uint64(nvprops
,
3155 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
3156 &volblocksize
)) != 0 && error
!= ENOENT
)
3157 return (SET_ERROR(EINVAL
));
3160 volblocksize
= zfs_prop_default_numeric(
3161 ZFS_PROP_VOLBLOCKSIZE
);
3163 if ((error
= zvol_check_volblocksize(
3164 volblocksize
)) != 0 ||
3165 (error
= zvol_check_volsize(volsize
,
3166 volblocksize
)) != 0)
3168 } else if (type
== DMU_OST_ZFS
) {
3172 * We have to have normalization and
3173 * case-folding flags correct when we do the
3174 * file system creation, so go figure them out
3177 VERIFY(nvlist_alloc(&zct
.zct_zplprops
,
3178 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3179 error
= zfs_fill_zplprops(fsname
, nvprops
,
3180 zct
.zct_zplprops
, &is_insensitive
);
3182 nvlist_free(zct
.zct_zplprops
);
3187 error
= dmu_objset_create(fsname
, type
,
3188 is_insensitive
? DS_FLAG_CI_DATASET
: 0, cbfunc
, &zct
);
3189 nvlist_free(zct
.zct_zplprops
);
3192 * It would be nice to do this atomically.
3195 error
= zfs_set_prop_nvlist(fsname
, ZPROP_SRC_LOCAL
,
3198 (void) dsl_destroy_head(fsname
);
3205 * "origin" -> name of origin snapshot
3206 * (optional) "props" -> { prop -> value }
3210 * outnvl: propname -> error code (int32)
3213 zfs_ioc_clone(const char *fsname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3216 nvlist_t
*nvprops
= NULL
;
3219 if (nvlist_lookup_string(innvl
, "origin", &origin_name
) != 0)
3220 return (SET_ERROR(EINVAL
));
3221 (void) nvlist_lookup_nvlist(innvl
, "props", &nvprops
);
3223 if (strchr(fsname
, '@') ||
3224 strchr(fsname
, '%'))
3225 return (SET_ERROR(EINVAL
));
3227 if (dataset_namecheck(origin_name
, NULL
, NULL
) != 0)
3228 return (SET_ERROR(EINVAL
));
3229 error
= dmu_objset_clone(fsname
, origin_name
);
3234 * It would be nice to do this atomically.
3237 error
= zfs_set_prop_nvlist(fsname
, ZPROP_SRC_LOCAL
,
3240 (void) dsl_destroy_head(fsname
);
3247 * "snaps" -> { snapshot1, snapshot2 }
3248 * (optional) "props" -> { prop -> value (string) }
3251 * outnvl: snapshot -> error code (int32)
3254 zfs_ioc_snapshot(const char *poolname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3257 nvlist_t
*props
= NULL
;
3259 nvpair_t
*pair
, *pair2
;
3261 (void) nvlist_lookup_nvlist(innvl
, "props", &props
);
3262 if ((error
= zfs_check_userprops(poolname
, props
)) != 0)
3265 if (!nvlist_empty(props
) &&
3266 zfs_earlier_version(poolname
, SPA_VERSION_SNAP_PROPS
))
3267 return (SET_ERROR(ENOTSUP
));
3269 if (nvlist_lookup_nvlist(innvl
, "snaps", &snaps
) != 0)
3270 return (SET_ERROR(EINVAL
));
3271 poollen
= strlen(poolname
);
3272 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
3273 pair
= nvlist_next_nvpair(snaps
, pair
)) {
3274 const char *name
= nvpair_name(pair
);
3275 const char *cp
= strchr(name
, '@');
3278 * The snap name must contain an @, and the part after it must
3279 * contain only valid characters.
3281 if (cp
== NULL
|| snapshot_namecheck(cp
+ 1, NULL
, NULL
) != 0)
3282 return (SET_ERROR(EINVAL
));
3285 * The snap must be in the specified pool.
3287 if (strncmp(name
, poolname
, poollen
) != 0 ||
3288 (name
[poollen
] != '/' && name
[poollen
] != '@'))
3289 return (SET_ERROR(EXDEV
));
3291 /* This must be the only snap of this fs. */
3292 for (pair2
= nvlist_next_nvpair(snaps
, pair
);
3293 pair2
!= NULL
; pair2
= nvlist_next_nvpair(snaps
, pair2
)) {
3294 if (strncmp(name
, nvpair_name(pair2
), cp
- name
+ 1)
3296 return (SET_ERROR(EXDEV
));
3301 error
= dsl_dataset_snapshot(snaps
, props
, outnvl
);
3306 * innvl: "message" -> string
3310 zfs_ioc_log_history(const char *unused
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3318 * The poolname in the ioctl is not set, we get it from the TSD,
3319 * which was set at the end of the last successful ioctl that allows
3320 * logging. The secpolicy func already checked that it is set.
3321 * Only one log ioctl is allowed after each successful ioctl, so
3322 * we clear the TSD here.
3324 poolname
= tsd_get(zfs_allow_log_key
);
3325 (void) tsd_set(zfs_allow_log_key
, NULL
);
3326 error
= spa_open(poolname
, &spa
, FTAG
);
3331 if (nvlist_lookup_string(innvl
, "message", &message
) != 0) {
3332 spa_close(spa
, FTAG
);
3333 return (SET_ERROR(EINVAL
));
3336 if (spa_version(spa
) < SPA_VERSION_ZPOOL_HISTORY
) {
3337 spa_close(spa
, FTAG
);
3338 return (SET_ERROR(ENOTSUP
));
3341 error
= spa_history_log(spa
, message
);
3342 spa_close(spa
, FTAG
);
3347 * The dp_config_rwlock must not be held when calling this, because the
3348 * unmount may need to write out data.
3350 * This function is best-effort. Callers must deal gracefully if it
3351 * remains mounted (or is remounted after this call).
3353 * XXX: This function should detect a failure to unmount a snapdir of a dataset
3354 * and return the appropriate error code when it is mounted. Its Illumos and
3355 * FreeBSD counterparts do this. We do not do this on Linux because there is no
3356 * clear way to access the mount information that FreeBSD and Illumos use to
3357 * distinguish between things with mounted snapshot directories, and things
3358 * without mounted snapshot directories, which include zvols. Returning a
3359 * failure for the latter causes `zfs destroy` to fail on zvol snapshots.
3362 zfs_unmount_snap(const char *snapname
)
3364 zfs_sb_t
*zsb
= NULL
;
3369 if ((ptr
= strchr(snapname
, '@')) == NULL
)
3372 dsname
= kmem_alloc(ptr
- snapname
+ 1, KM_SLEEP
);
3373 strlcpy(dsname
, snapname
, ptr
- snapname
+ 1);
3374 fullname
= strdup(snapname
);
3376 if (zfs_sb_hold(dsname
, FTAG
, &zsb
, B_FALSE
) == 0) {
3377 ASSERT(!dsl_pool_config_held(dmu_objset_pool(zsb
->z_os
)));
3378 (void) zfsctl_unmount_snapshot(zsb
, fullname
, MNT_FORCE
);
3379 zfs_sb_rele(zsb
, FTAG
);
3382 kmem_free(dsname
, ptr
- snapname
+ 1);
3390 zfs_unmount_snap_cb(const char *snapname
, void *arg
)
3392 return (zfs_unmount_snap(snapname
));
3396 * When a clone is destroyed, its origin may also need to be destroyed,
3397 * in which case it must be unmounted. This routine will do that unmount
3401 zfs_destroy_unmount_origin(const char *fsname
)
3407 error
= dmu_objset_hold(fsname
, FTAG
, &os
);
3410 ds
= dmu_objset_ds(os
);
3411 if (dsl_dir_is_clone(ds
->ds_dir
) && DS_IS_DEFER_DESTROY(ds
->ds_prev
)) {
3412 char originname
[MAXNAMELEN
];
3413 dsl_dataset_name(ds
->ds_prev
, originname
);
3414 dmu_objset_rele(os
, FTAG
);
3415 (void) zfs_unmount_snap(originname
);
3417 dmu_objset_rele(os
, FTAG
);
3423 * "snaps" -> { snapshot1, snapshot2 }
3424 * (optional boolean) "defer"
3427 * outnvl: snapshot -> error code (int32)
3430 zfs_ioc_destroy_snaps(const char *poolname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3437 if (nvlist_lookup_nvlist(innvl
, "snaps", &snaps
) != 0)
3438 return (SET_ERROR(EINVAL
));
3439 defer
= nvlist_exists(innvl
, "defer");
3441 poollen
= strlen(poolname
);
3442 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
3443 pair
= nvlist_next_nvpair(snaps
, pair
)) {
3444 const char *name
= nvpair_name(pair
);
3447 * The snap must be in the specified pool.
3449 if (strncmp(name
, poolname
, poollen
) != 0 ||
3450 (name
[poollen
] != '/' && name
[poollen
] != '@'))
3451 return (SET_ERROR(EXDEV
));
3453 (void) zvol_remove_minor(name
);
3454 error
= zfs_unmount_snap(name
);
3459 return (dsl_destroy_snapshots_nvl(snaps
, defer
, outnvl
));
3464 * zc_name name of dataset to destroy
3465 * zc_objset_type type of objset
3466 * zc_defer_destroy mark for deferred destroy
3471 zfs_ioc_destroy(zfs_cmd_t
*zc
)
3475 if (zc
->zc_objset_type
== DMU_OST_ZFS
) {
3476 err
= zfs_unmount_snap(zc
->zc_name
);
3481 if (strchr(zc
->zc_name
, '@'))
3482 err
= dsl_destroy_snapshot(zc
->zc_name
, zc
->zc_defer_destroy
);
3484 err
= dsl_destroy_head(zc
->zc_name
);
3485 if (zc
->zc_objset_type
== DMU_OST_ZVOL
&& err
== 0)
3486 (void) zvol_remove_minor(zc
->zc_name
);
3492 * zc_name name of dataset to rollback (to most recent snapshot)
3497 zfs_ioc_rollback(zfs_cmd_t
*zc
)
3502 if (get_zfs_sb(zc
->zc_name
, &zsb
) == 0) {
3503 error
= zfs_suspend_fs(zsb
);
3507 error
= dsl_dataset_rollback(zc
->zc_name
);
3508 resume_err
= zfs_resume_fs(zsb
, zc
->zc_name
);
3509 error
= error
? error
: resume_err
;
3511 deactivate_super(zsb
->z_sb
);
3513 error
= dsl_dataset_rollback(zc
->zc_name
);
3519 recursive_unmount(const char *fsname
, void *arg
)
3521 const char *snapname
= arg
;
3524 fullname
= kmem_asprintf("%s@%s", fsname
, snapname
);
3525 zfs_unmount_snap(fullname
);
3527 return (zfs_unmount_snap(fullname
));
3532 * zc_name old name of dataset
3533 * zc_value new name of dataset
3534 * zc_cookie recursive flag (only valid for snapshots)
3539 zfs_ioc_rename(zfs_cmd_t
*zc
)
3541 boolean_t recursive
= zc
->zc_cookie
& 1;
3545 zc
->zc_value
[sizeof (zc
->zc_value
) - 1] = '\0';
3546 if (dataset_namecheck(zc
->zc_value
, NULL
, NULL
) != 0 ||
3547 strchr(zc
->zc_value
, '%'))
3548 return (SET_ERROR(EINVAL
));
3550 at
= strchr(zc
->zc_name
, '@');
3552 /* snaps must be in same fs */
3553 if (strncmp(zc
->zc_name
, zc
->zc_value
, at
- zc
->zc_name
+ 1))
3554 return (SET_ERROR(EXDEV
));
3556 if (zc
->zc_objset_type
== DMU_OST_ZFS
) {
3557 int error
= dmu_objset_find(zc
->zc_name
,
3558 recursive_unmount
, at
+ 1,
3559 recursive
? DS_FIND_CHILDREN
: 0);
3563 return (dsl_dataset_rename_snapshot(zc
->zc_name
,
3564 at
+ 1, strchr(zc
->zc_value
, '@') + 1, recursive
));
3566 err
= dsl_dir_rename(zc
->zc_name
, zc
->zc_value
);
3567 if (!err
&& zc
->zc_objset_type
== DMU_OST_ZVOL
) {
3568 (void) zvol_remove_minor(zc
->zc_name
);
3569 (void) zvol_create_minor(zc
->zc_value
);
3576 zfs_check_settable(const char *dsname
, nvpair_t
*pair
, cred_t
*cr
)
3578 const char *propname
= nvpair_name(pair
);
3579 boolean_t issnap
= (strchr(dsname
, '@') != NULL
);
3580 zfs_prop_t prop
= zfs_name_to_prop(propname
);
3584 if (prop
== ZPROP_INVAL
) {
3585 if (zfs_prop_user(propname
)) {
3586 if ((err
= zfs_secpolicy_write_perms(dsname
,
3587 ZFS_DELEG_PERM_USERPROP
, cr
)))
3592 if (!issnap
&& zfs_prop_userquota(propname
)) {
3593 const char *perm
= NULL
;
3594 const char *uq_prefix
=
3595 zfs_userquota_prop_prefixes
[ZFS_PROP_USERQUOTA
];
3596 const char *gq_prefix
=
3597 zfs_userquota_prop_prefixes
[ZFS_PROP_GROUPQUOTA
];
3599 if (strncmp(propname
, uq_prefix
,
3600 strlen(uq_prefix
)) == 0) {
3601 perm
= ZFS_DELEG_PERM_USERQUOTA
;
3602 } else if (strncmp(propname
, gq_prefix
,
3603 strlen(gq_prefix
)) == 0) {
3604 perm
= ZFS_DELEG_PERM_GROUPQUOTA
;
3606 /* USERUSED and GROUPUSED are read-only */
3607 return (SET_ERROR(EINVAL
));
3610 if ((err
= zfs_secpolicy_write_perms(dsname
, perm
, cr
)))
3615 return (SET_ERROR(EINVAL
));
3619 return (SET_ERROR(EINVAL
));
3621 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
3623 * dsl_prop_get_all_impl() returns properties in this
3627 VERIFY(nvpair_value_nvlist(pair
, &attrs
) == 0);
3628 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
3633 * Check that this value is valid for this pool version
3636 case ZFS_PROP_COMPRESSION
:
3638 * If the user specified gzip compression, make sure
3639 * the SPA supports it. We ignore any errors here since
3640 * we'll catch them later.
3642 if (nvpair_type(pair
) == DATA_TYPE_UINT64
&&
3643 nvpair_value_uint64(pair
, &intval
) == 0) {
3644 if (intval
>= ZIO_COMPRESS_GZIP_1
&&
3645 intval
<= ZIO_COMPRESS_GZIP_9
&&
3646 zfs_earlier_version(dsname
,
3647 SPA_VERSION_GZIP_COMPRESSION
)) {
3648 return (SET_ERROR(ENOTSUP
));
3651 if (intval
== ZIO_COMPRESS_ZLE
&&
3652 zfs_earlier_version(dsname
,
3653 SPA_VERSION_ZLE_COMPRESSION
))
3654 return (SET_ERROR(ENOTSUP
));
3656 if (intval
== ZIO_COMPRESS_LZ4
) {
3657 zfeature_info_t
*feature
=
3659 SPA_FEATURE_LZ4_COMPRESS
];
3662 if ((err
= spa_open(dsname
, &spa
, FTAG
)) != 0)
3665 if (!spa_feature_is_enabled(spa
, feature
)) {
3666 spa_close(spa
, FTAG
);
3667 return (SET_ERROR(ENOTSUP
));
3669 spa_close(spa
, FTAG
);
3673 * If this is a bootable dataset then
3674 * verify that the compression algorithm
3675 * is supported for booting. We must return
3676 * something other than ENOTSUP since it
3677 * implies a downrev pool version.
3679 if (zfs_is_bootfs(dsname
) &&
3680 !BOOTFS_COMPRESS_VALID(intval
)) {
3681 return (SET_ERROR(ERANGE
));
3686 case ZFS_PROP_COPIES
:
3687 if (zfs_earlier_version(dsname
, SPA_VERSION_DITTO_BLOCKS
))
3688 return (SET_ERROR(ENOTSUP
));
3691 case ZFS_PROP_DEDUP
:
3692 if (zfs_earlier_version(dsname
, SPA_VERSION_DEDUP
))
3693 return (SET_ERROR(ENOTSUP
));
3696 case ZFS_PROP_SHARESMB
:
3697 if (zpl_earlier_version(dsname
, ZPL_VERSION_FUID
))
3698 return (SET_ERROR(ENOTSUP
));
3701 case ZFS_PROP_ACLINHERIT
:
3702 if (nvpair_type(pair
) == DATA_TYPE_UINT64
&&
3703 nvpair_value_uint64(pair
, &intval
) == 0) {
3704 if (intval
== ZFS_ACL_PASSTHROUGH_X
&&
3705 zfs_earlier_version(dsname
,
3706 SPA_VERSION_PASSTHROUGH_X
))
3707 return (SET_ERROR(ENOTSUP
));
3714 return (zfs_secpolicy_setprop(dsname
, prop
, pair
, CRED()));
3718 * Checks for a race condition to make sure we don't increment a feature flag
3722 zfs_prop_activate_feature_check(void *arg
, dmu_tx_t
*tx
)
3724 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
3725 zfeature_info_t
*feature
= arg
;
3727 if (!spa_feature_is_active(spa
, feature
))
3730 return (SET_ERROR(EBUSY
));
3734 * The callback invoked on feature activation in the sync task caused by
3735 * zfs_prop_activate_feature.
3738 zfs_prop_activate_feature_sync(void *arg
, dmu_tx_t
*tx
)
3740 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
3741 zfeature_info_t
*feature
= arg
;
3743 spa_feature_incr(spa
, feature
, tx
);
3747 * Activates a feature on a pool in response to a property setting. This
3748 * creates a new sync task which modifies the pool to reflect the feature
3752 zfs_prop_activate_feature(spa_t
*spa
, zfeature_info_t
*feature
)
3756 /* EBUSY here indicates that the feature is already active */
3757 err
= dsl_sync_task(spa_name(spa
),
3758 zfs_prop_activate_feature_check
, zfs_prop_activate_feature_sync
,
3761 if (err
!= 0 && err
!= EBUSY
)
3768 * Removes properties from the given props list that fail permission checks
3769 * needed to clear them and to restore them in case of a receive error. For each
3770 * property, make sure we have both set and inherit permissions.
3772 * Returns the first error encountered if any permission checks fail. If the
3773 * caller provides a non-NULL errlist, it also gives the complete list of names
3774 * of all the properties that failed a permission check along with the
3775 * corresponding error numbers. The caller is responsible for freeing the
3778 * If every property checks out successfully, zero is returned and the list
3779 * pointed at by errlist is NULL.
3782 zfs_check_clearable(char *dataset
, nvlist_t
*props
, nvlist_t
**errlist
)
3785 nvpair_t
*pair
, *next_pair
;
3792 VERIFY(nvlist_alloc(&errors
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3794 zc
= kmem_alloc(sizeof (zfs_cmd_t
), KM_SLEEP
| KM_NODEBUG
);
3795 (void) strcpy(zc
->zc_name
, dataset
);
3796 pair
= nvlist_next_nvpair(props
, NULL
);
3797 while (pair
!= NULL
) {
3798 next_pair
= nvlist_next_nvpair(props
, pair
);
3800 (void) strcpy(zc
->zc_value
, nvpair_name(pair
));
3801 if ((err
= zfs_check_settable(dataset
, pair
, CRED())) != 0 ||
3802 (err
= zfs_secpolicy_inherit_prop(zc
, NULL
, CRED())) != 0) {
3803 VERIFY(nvlist_remove_nvpair(props
, pair
) == 0);
3804 VERIFY(nvlist_add_int32(errors
,
3805 zc
->zc_value
, err
) == 0);
3809 kmem_free(zc
, sizeof (zfs_cmd_t
));
3811 if ((pair
= nvlist_next_nvpair(errors
, NULL
)) == NULL
) {
3812 nvlist_free(errors
);
3815 VERIFY(nvpair_value_int32(pair
, &rv
) == 0);
3818 if (errlist
== NULL
)
3819 nvlist_free(errors
);
3827 propval_equals(nvpair_t
*p1
, nvpair_t
*p2
)
3829 if (nvpair_type(p1
) == DATA_TYPE_NVLIST
) {
3830 /* dsl_prop_get_all_impl() format */
3832 VERIFY(nvpair_value_nvlist(p1
, &attrs
) == 0);
3833 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
3837 if (nvpair_type(p2
) == DATA_TYPE_NVLIST
) {
3839 VERIFY(nvpair_value_nvlist(p2
, &attrs
) == 0);
3840 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
3844 if (nvpair_type(p1
) != nvpair_type(p2
))
3847 if (nvpair_type(p1
) == DATA_TYPE_STRING
) {
3848 char *valstr1
, *valstr2
;
3850 VERIFY(nvpair_value_string(p1
, (char **)&valstr1
) == 0);
3851 VERIFY(nvpair_value_string(p2
, (char **)&valstr2
) == 0);
3852 return (strcmp(valstr1
, valstr2
) == 0);
3854 uint64_t intval1
, intval2
;
3856 VERIFY(nvpair_value_uint64(p1
, &intval1
) == 0);
3857 VERIFY(nvpair_value_uint64(p2
, &intval2
) == 0);
3858 return (intval1
== intval2
);
3863 * Remove properties from props if they are not going to change (as determined
3864 * by comparison with origprops). Remove them from origprops as well, since we
3865 * do not need to clear or restore properties that won't change.
3868 props_reduce(nvlist_t
*props
, nvlist_t
*origprops
)
3870 nvpair_t
*pair
, *next_pair
;
3872 if (origprops
== NULL
)
3873 return; /* all props need to be received */
3875 pair
= nvlist_next_nvpair(props
, NULL
);
3876 while (pair
!= NULL
) {
3877 const char *propname
= nvpair_name(pair
);
3880 next_pair
= nvlist_next_nvpair(props
, pair
);
3882 if ((nvlist_lookup_nvpair(origprops
, propname
,
3883 &match
) != 0) || !propval_equals(pair
, match
))
3884 goto next
; /* need to set received value */
3886 /* don't clear the existing received value */
3887 (void) nvlist_remove_nvpair(origprops
, match
);
3888 /* don't bother receiving the property */
3889 (void) nvlist_remove_nvpair(props
, pair
);
3896 static boolean_t zfs_ioc_recv_inject_err
;
3901 * zc_name name of containing filesystem
3902 * zc_nvlist_src{_size} nvlist of properties to apply
3903 * zc_value name of snapshot to create
3904 * zc_string name of clone origin (if DRR_FLAG_CLONE)
3905 * zc_cookie file descriptor to recv from
3906 * zc_begin_record the BEGIN record of the stream (not byteswapped)
3907 * zc_guid force flag
3908 * zc_cleanup_fd cleanup-on-exit file descriptor
3909 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
3912 * zc_cookie number of bytes read
3913 * zc_nvlist_dst{_size} error for each unapplied received property
3914 * zc_obj zprop_errflags_t
3915 * zc_action_handle handle for this guid/ds mapping
3918 zfs_ioc_recv(zfs_cmd_t
*zc
)
3921 dmu_recv_cookie_t drc
;
3922 boolean_t force
= (boolean_t
)zc
->zc_guid
;
3925 int props_error
= 0;
3928 nvlist_t
*props
= NULL
; /* sent properties */
3929 nvlist_t
*origprops
= NULL
; /* existing properties */
3930 char *origin
= NULL
;
3932 char tofs
[ZFS_MAXNAMELEN
];
3933 boolean_t first_recvd_props
= B_FALSE
;
3935 if (dataset_namecheck(zc
->zc_value
, NULL
, NULL
) != 0 ||
3936 strchr(zc
->zc_value
, '@') == NULL
||
3937 strchr(zc
->zc_value
, '%'))
3938 return (SET_ERROR(EINVAL
));
3940 (void) strcpy(tofs
, zc
->zc_value
);
3941 tosnap
= strchr(tofs
, '@');
3944 if (zc
->zc_nvlist_src
!= 0 &&
3945 (error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
3946 zc
->zc_iflags
, &props
)) != 0)
3953 return (SET_ERROR(EBADF
));
3956 VERIFY(nvlist_alloc(&errors
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3958 if (zc
->zc_string
[0])
3959 origin
= zc
->zc_string
;
3961 error
= dmu_recv_begin(tofs
, tosnap
,
3962 &zc
->zc_begin_record
, force
, origin
, &drc
);
3967 * Set properties before we receive the stream so that they are applied
3968 * to the new data. Note that we must call dmu_recv_stream() if
3969 * dmu_recv_begin() succeeds.
3971 if (props
!= NULL
&& !drc
.drc_newfs
) {
3972 if (spa_version(dsl_dataset_get_spa(drc
.drc_ds
)) >=
3973 SPA_VERSION_RECVD_PROPS
&&
3974 !dsl_prop_get_hasrecvd(tofs
))
3975 first_recvd_props
= B_TRUE
;
3978 * If new received properties are supplied, they are to
3979 * completely replace the existing received properties, so stash
3980 * away the existing ones.
3982 if (dsl_prop_get_received(tofs
, &origprops
) == 0) {
3983 nvlist_t
*errlist
= NULL
;
3985 * Don't bother writing a property if its value won't
3986 * change (and avoid the unnecessary security checks).
3988 * The first receive after SPA_VERSION_RECVD_PROPS is a
3989 * special case where we blow away all local properties
3992 if (!first_recvd_props
)
3993 props_reduce(props
, origprops
);
3994 if (zfs_check_clearable(tofs
, origprops
, &errlist
) != 0)
3995 (void) nvlist_merge(errors
, errlist
, 0);
3996 nvlist_free(errlist
);
3998 if (clear_received_props(tofs
, origprops
,
3999 first_recvd_props
? NULL
: props
) != 0)
4000 zc
->zc_obj
|= ZPROP_ERR_NOCLEAR
;
4002 zc
->zc_obj
|= ZPROP_ERR_NOCLEAR
;
4006 if (props
!= NULL
) {
4007 props_error
= dsl_prop_set_hasrecvd(tofs
);
4009 if (props_error
== 0) {
4010 (void) zfs_set_prop_nvlist(tofs
, ZPROP_SRC_RECEIVED
,
4015 if (zc
->zc_nvlist_dst_size
!= 0 &&
4016 (nvlist_smush(errors
, zc
->zc_nvlist_dst_size
) != 0 ||
4017 put_nvlist(zc
, errors
) != 0)) {
4019 * Caller made zc->zc_nvlist_dst less than the minimum expected
4020 * size or supplied an invalid address.
4022 props_error
= SET_ERROR(EINVAL
);
4026 error
= dmu_recv_stream(&drc
, fp
->f_vnode
, &off
, zc
->zc_cleanup_fd
,
4027 &zc
->zc_action_handle
);
4030 zfs_sb_t
*zsb
= NULL
;
4032 if (get_zfs_sb(tofs
, &zsb
) == 0) {
4036 error
= zfs_suspend_fs(zsb
);
4038 * If the suspend fails, then the recv_end will
4039 * likely also fail, and clean up after itself.
4041 end_err
= dmu_recv_end(&drc
);
4043 error
= zfs_resume_fs(zsb
, tofs
);
4044 error
= error
? error
: end_err
;
4045 deactivate_super(zsb
->z_sb
);
4047 error
= dmu_recv_end(&drc
);
4051 zc
->zc_cookie
= off
- fp
->f_offset
;
4052 if (VOP_SEEK(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
4056 if (zfs_ioc_recv_inject_err
) {
4057 zfs_ioc_recv_inject_err
= B_FALSE
;
4062 * On error, restore the original props.
4064 if (error
!= 0 && props
!= NULL
&& !drc
.drc_newfs
) {
4065 if (clear_received_props(tofs
, props
, NULL
) != 0) {
4067 * We failed to clear the received properties.
4068 * Since we may have left a $recvd value on the
4069 * system, we can't clear the $hasrecvd flag.
4071 zc
->zc_obj
|= ZPROP_ERR_NORESTORE
;
4072 } else if (first_recvd_props
) {
4073 dsl_prop_unset_hasrecvd(tofs
);
4076 if (origprops
== NULL
&& !drc
.drc_newfs
) {
4077 /* We failed to stash the original properties. */
4078 zc
->zc_obj
|= ZPROP_ERR_NORESTORE
;
4082 * dsl_props_set() will not convert RECEIVED to LOCAL on or
4083 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
4084 * explictly if we're restoring local properties cleared in the
4085 * first new-style receive.
4087 if (origprops
!= NULL
&&
4088 zfs_set_prop_nvlist(tofs
, (first_recvd_props
?
4089 ZPROP_SRC_LOCAL
: ZPROP_SRC_RECEIVED
),
4090 origprops
, NULL
) != 0) {
4092 * We stashed the original properties but failed to
4095 zc
->zc_obj
|= ZPROP_ERR_NORESTORE
;
4100 nvlist_free(origprops
);
4101 nvlist_free(errors
);
4105 error
= props_error
;
4112 * zc_name name of snapshot to send
4113 * zc_cookie file descriptor to send stream to
4114 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
4115 * zc_sendobj objsetid of snapshot to send
4116 * zc_fromobj objsetid of incremental fromsnap (may be zero)
4117 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
4118 * output size in zc_objset_type.
4123 zfs_ioc_send(zfs_cmd_t
*zc
)
4127 boolean_t estimate
= (zc
->zc_guid
!= 0);
4129 if (zc
->zc_obj
!= 0) {
4131 dsl_dataset_t
*tosnap
;
4133 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
4137 error
= dsl_dataset_hold_obj(dp
, zc
->zc_sendobj
, FTAG
, &tosnap
);
4139 dsl_pool_rele(dp
, FTAG
);
4143 if (dsl_dir_is_clone(tosnap
->ds_dir
))
4144 zc
->zc_fromobj
= tosnap
->ds_dir
->dd_phys
->dd_origin_obj
;
4145 dsl_dataset_rele(tosnap
, FTAG
);
4146 dsl_pool_rele(dp
, FTAG
);
4151 dsl_dataset_t
*tosnap
;
4152 dsl_dataset_t
*fromsnap
= NULL
;
4154 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
4158 error
= dsl_dataset_hold_obj(dp
, zc
->zc_sendobj
, FTAG
, &tosnap
);
4160 dsl_pool_rele(dp
, FTAG
);
4164 if (zc
->zc_fromobj
!= 0) {
4165 error
= dsl_dataset_hold_obj(dp
, zc
->zc_fromobj
,
4168 dsl_dataset_rele(tosnap
, FTAG
);
4169 dsl_pool_rele(dp
, FTAG
);
4174 error
= dmu_send_estimate(tosnap
, fromsnap
,
4175 &zc
->zc_objset_type
);
4177 if (fromsnap
!= NULL
)
4178 dsl_dataset_rele(fromsnap
, FTAG
);
4179 dsl_dataset_rele(tosnap
, FTAG
);
4180 dsl_pool_rele(dp
, FTAG
);
4182 file_t
*fp
= getf(zc
->zc_cookie
);
4184 return (SET_ERROR(EBADF
));
4187 error
= dmu_send_obj(zc
->zc_name
, zc
->zc_sendobj
,
4188 zc
->zc_fromobj
, zc
->zc_cookie
, fp
->f_vnode
, &off
);
4190 if (VOP_SEEK(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
4192 releasef(zc
->zc_cookie
);
4199 * zc_name name of snapshot on which to report progress
4200 * zc_cookie file descriptor of send stream
4203 * zc_cookie number of bytes written in send stream thus far
4206 zfs_ioc_send_progress(zfs_cmd_t
*zc
)
4210 dmu_sendarg_t
*dsp
= NULL
;
4213 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
4217 error
= dsl_dataset_hold(dp
, zc
->zc_name
, FTAG
, &ds
);
4219 dsl_pool_rele(dp
, FTAG
);
4223 mutex_enter(&ds
->ds_sendstream_lock
);
4226 * Iterate over all the send streams currently active on this dataset.
4227 * If there's one which matches the specified file descriptor _and_ the
4228 * stream was started by the current process, return the progress of
4232 for (dsp
= list_head(&ds
->ds_sendstreams
); dsp
!= NULL
;
4233 dsp
= list_next(&ds
->ds_sendstreams
, dsp
)) {
4234 if (dsp
->dsa_outfd
== zc
->zc_cookie
&&
4235 dsp
->dsa_proc
->group_leader
== curproc
->group_leader
)
4240 zc
->zc_cookie
= *(dsp
->dsa_off
);
4242 error
= SET_ERROR(ENOENT
);
4244 mutex_exit(&ds
->ds_sendstream_lock
);
4245 dsl_dataset_rele(ds
, FTAG
);
4246 dsl_pool_rele(dp
, FTAG
);
4251 zfs_ioc_inject_fault(zfs_cmd_t
*zc
)
4255 error
= zio_inject_fault(zc
->zc_name
, (int)zc
->zc_guid
, &id
,
4256 &zc
->zc_inject_record
);
4259 zc
->zc_guid
= (uint64_t)id
;
4265 zfs_ioc_clear_fault(zfs_cmd_t
*zc
)
4267 return (zio_clear_fault((int)zc
->zc_guid
));
4271 zfs_ioc_inject_list_next(zfs_cmd_t
*zc
)
4273 int id
= (int)zc
->zc_guid
;
4276 error
= zio_inject_list_next(&id
, zc
->zc_name
, sizeof (zc
->zc_name
),
4277 &zc
->zc_inject_record
);
4285 zfs_ioc_error_log(zfs_cmd_t
*zc
)
4289 size_t count
= (size_t)zc
->zc_nvlist_dst_size
;
4291 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
4294 error
= spa_get_errlog(spa
, (void *)(uintptr_t)zc
->zc_nvlist_dst
,
4297 zc
->zc_nvlist_dst_size
= count
;
4299 zc
->zc_nvlist_dst_size
= spa_get_errlog_size(spa
);
4301 spa_close(spa
, FTAG
);
4307 zfs_ioc_clear(zfs_cmd_t
*zc
)
4314 * On zpool clear we also fix up missing slogs
4316 mutex_enter(&spa_namespace_lock
);
4317 spa
= spa_lookup(zc
->zc_name
);
4319 mutex_exit(&spa_namespace_lock
);
4320 return (SET_ERROR(EIO
));
4322 if (spa_get_log_state(spa
) == SPA_LOG_MISSING
) {
4323 /* we need to let spa_open/spa_load clear the chains */
4324 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
4326 spa
->spa_last_open_failed
= 0;
4327 mutex_exit(&spa_namespace_lock
);
4329 if (zc
->zc_cookie
& ZPOOL_NO_REWIND
) {
4330 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
4333 nvlist_t
*config
= NULL
;
4335 if (zc
->zc_nvlist_src
== 0)
4336 return (SET_ERROR(EINVAL
));
4338 if ((error
= get_nvlist(zc
->zc_nvlist_src
,
4339 zc
->zc_nvlist_src_size
, zc
->zc_iflags
, &policy
)) == 0) {
4340 error
= spa_open_rewind(zc
->zc_name
, &spa
, FTAG
,
4342 if (config
!= NULL
) {
4345 if ((err
= put_nvlist(zc
, config
)) != 0)
4347 nvlist_free(config
);
4349 nvlist_free(policy
);
4356 spa_vdev_state_enter(spa
, SCL_NONE
);
4358 if (zc
->zc_guid
== 0) {
4361 vd
= spa_lookup_by_guid(spa
, zc
->zc_guid
, B_TRUE
);
4363 (void) spa_vdev_state_exit(spa
, NULL
, ENODEV
);
4364 spa_close(spa
, FTAG
);
4365 return (SET_ERROR(ENODEV
));
4369 vdev_clear(spa
, vd
);
4371 (void) spa_vdev_state_exit(spa
, NULL
, 0);
4374 * Resume any suspended I/Os.
4376 if (zio_resume(spa
) != 0)
4377 error
= SET_ERROR(EIO
);
4379 spa_close(spa
, FTAG
);
4385 zfs_ioc_pool_reopen(zfs_cmd_t
*zc
)
4390 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
4394 spa_vdev_state_enter(spa
, SCL_NONE
);
4397 * If a resilver is already in progress then set the
4398 * spa_scrub_reopen flag to B_TRUE so that we don't restart
4399 * the scan as a side effect of the reopen. Otherwise, let
4400 * vdev_open() decided if a resilver is required.
4402 spa
->spa_scrub_reopen
= dsl_scan_resilvering(spa
->spa_dsl_pool
);
4403 vdev_reopen(spa
->spa_root_vdev
);
4404 spa
->spa_scrub_reopen
= B_FALSE
;
4406 (void) spa_vdev_state_exit(spa
, NULL
, 0);
4407 spa_close(spa
, FTAG
);
4412 * zc_name name of filesystem
4413 * zc_value name of origin snapshot
4416 * zc_string name of conflicting snapshot, if there is one
4419 zfs_ioc_promote(zfs_cmd_t
*zc
)
4424 * We don't need to unmount *all* the origin fs's snapshots, but
4427 cp
= strchr(zc
->zc_value
, '@');
4430 (void) dmu_objset_find(zc
->zc_value
,
4431 zfs_unmount_snap_cb
, NULL
, DS_FIND_SNAPSHOTS
);
4432 return (dsl_dataset_promote(zc
->zc_name
, zc
->zc_string
));
4436 * Retrieve a single {user|group}{used|quota}@... property.
4439 * zc_name name of filesystem
4440 * zc_objset_type zfs_userquota_prop_t
4441 * zc_value domain name (eg. "S-1-234-567-89")
4442 * zc_guid RID/UID/GID
4445 * zc_cookie property value
4448 zfs_ioc_userspace_one(zfs_cmd_t
*zc
)
4453 if (zc
->zc_objset_type
>= ZFS_NUM_USERQUOTA_PROPS
)
4454 return (SET_ERROR(EINVAL
));
4456 error
= zfs_sb_hold(zc
->zc_name
, FTAG
, &zsb
, B_FALSE
);
4460 error
= zfs_userspace_one(zsb
,
4461 zc
->zc_objset_type
, zc
->zc_value
, zc
->zc_guid
, &zc
->zc_cookie
);
4462 zfs_sb_rele(zsb
, FTAG
);
4469 * zc_name name of filesystem
4470 * zc_cookie zap cursor
4471 * zc_objset_type zfs_userquota_prop_t
4472 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
4475 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
4476 * zc_cookie zap cursor
4479 zfs_ioc_userspace_many(zfs_cmd_t
*zc
)
4482 int bufsize
= zc
->zc_nvlist_dst_size
;
4487 return (SET_ERROR(ENOMEM
));
4489 error
= zfs_sb_hold(zc
->zc_name
, FTAG
, &zsb
, B_FALSE
);
4493 buf
= vmem_alloc(bufsize
, KM_SLEEP
);
4495 error
= zfs_userspace_many(zsb
, zc
->zc_objset_type
, &zc
->zc_cookie
,
4496 buf
, &zc
->zc_nvlist_dst_size
);
4499 error
= xcopyout(buf
,
4500 (void *)(uintptr_t)zc
->zc_nvlist_dst
,
4501 zc
->zc_nvlist_dst_size
);
4503 vmem_free(buf
, bufsize
);
4504 zfs_sb_rele(zsb
, FTAG
);
4511 * zc_name name of filesystem
4517 zfs_ioc_userspace_upgrade(zfs_cmd_t
*zc
)
4523 if (get_zfs_sb(zc
->zc_name
, &zsb
) == 0) {
4524 if (!dmu_objset_userused_enabled(zsb
->z_os
)) {
4526 * If userused is not enabled, it may be because the
4527 * objset needs to be closed & reopened (to grow the
4528 * objset_phys_t). Suspend/resume the fs will do that.
4530 error
= zfs_suspend_fs(zsb
);
4532 error
= zfs_resume_fs(zsb
, zc
->zc_name
);
4535 error
= dmu_objset_userspace_upgrade(zsb
->z_os
);
4536 deactivate_super(zsb
->z_sb
);
4538 /* XXX kind of reading contents without owning */
4539 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
4543 error
= dmu_objset_userspace_upgrade(os
);
4544 dmu_objset_rele(os
, FTAG
);
4551 zfs_ioc_share(zfs_cmd_t
*zc
)
4553 return (SET_ERROR(ENOSYS
));
4556 ace_t full_access
[] = {
4557 {(uid_t
)-1, ACE_ALL_PERMS
, ACE_EVERYONE
, 0}
4562 * zc_name name of containing filesystem
4563 * zc_obj object # beyond which we want next in-use object #
4566 * zc_obj next in-use object #
4569 zfs_ioc_next_obj(zfs_cmd_t
*zc
)
4571 objset_t
*os
= NULL
;
4574 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
4578 error
= dmu_object_next(os
, &zc
->zc_obj
, B_FALSE
,
4579 os
->os_dsl_dataset
->ds_phys
->ds_prev_snap_txg
);
4581 dmu_objset_rele(os
, FTAG
);
4587 * zc_name name of filesystem
4588 * zc_value prefix name for snapshot
4589 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
4592 * zc_value short name of new snapshot
4595 zfs_ioc_tmp_snapshot(zfs_cmd_t
*zc
)
4602 error
= zfs_onexit_fd_hold(zc
->zc_cleanup_fd
, &minor
);
4606 snap_name
= kmem_asprintf("%s-%016llx", zc
->zc_value
,
4607 (u_longlong_t
)ddi_get_lbolt64());
4608 hold_name
= kmem_asprintf("%%%s", zc
->zc_value
);
4610 error
= dsl_dataset_snapshot_tmp(zc
->zc_name
, snap_name
, minor
,
4613 (void) strcpy(zc
->zc_value
, snap_name
);
4616 zfs_onexit_fd_rele(zc
->zc_cleanup_fd
);
4622 * zc_name name of "to" snapshot
4623 * zc_value name of "from" snapshot
4624 * zc_cookie file descriptor to write diff data on
4627 * dmu_diff_record_t's to the file descriptor
4630 zfs_ioc_diff(zfs_cmd_t
*zc
)
4636 fp
= getf(zc
->zc_cookie
);
4638 return (SET_ERROR(EBADF
));
4642 error
= dmu_diff(zc
->zc_name
, zc
->zc_value
, fp
->f_vnode
, &off
);
4644 if (VOP_SEEK(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
4646 releasef(zc
->zc_cookie
);
4652 * Remove all ACL files in shares dir
4654 #ifdef HAVE_SMB_SHARE
4656 zfs_smb_acl_purge(znode_t
*dzp
)
4659 zap_attribute_t zap
;
4660 zfs_sb_t
*zsb
= ZTOZSB(dzp
);
4663 for (zap_cursor_init(&zc
, zsb
->z_os
, dzp
->z_id
);
4664 (error
= zap_cursor_retrieve(&zc
, &zap
)) == 0;
4665 zap_cursor_advance(&zc
)) {
4666 if ((error
= VOP_REMOVE(ZTOV(dzp
), zap
.za_name
, kcred
,
4670 zap_cursor_fini(&zc
);
4673 #endif /* HAVE_SMB_SHARE */
4676 zfs_ioc_smb_acl(zfs_cmd_t
*zc
)
4678 #ifdef HAVE_SMB_SHARE
4681 vnode_t
*resourcevp
= NULL
;
4690 if ((error
= lookupname(zc
->zc_value
, UIO_SYSSPACE
,
4691 NO_FOLLOW
, NULL
, &vp
)) != 0)
4694 /* Now make sure mntpnt and dataset are ZFS */
4696 if (vp
->v_vfsp
->vfs_fstype
!= zfsfstype
||
4697 (strcmp((char *)refstr_value(vp
->v_vfsp
->vfs_resource
),
4698 zc
->zc_name
) != 0)) {
4700 return (SET_ERROR(EINVAL
));
4708 * Create share dir if its missing.
4710 mutex_enter(&zsb
->z_lock
);
4711 if (zsb
->z_shares_dir
== 0) {
4714 tx
= dmu_tx_create(zsb
->z_os
);
4715 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
, TRUE
,
4717 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, FALSE
, NULL
);
4718 error
= dmu_tx_assign(tx
, TXG_WAIT
);
4722 error
= zfs_create_share_dir(zsb
, tx
);
4726 mutex_exit(&zsb
->z_lock
);
4732 mutex_exit(&zsb
->z_lock
);
4734 ASSERT(zsb
->z_shares_dir
);
4735 if ((error
= zfs_zget(zsb
, zsb
->z_shares_dir
, &sharedir
)) != 0) {
4741 switch (zc
->zc_cookie
) {
4742 case ZFS_SMB_ACL_ADD
:
4743 vattr
.va_mask
= AT_MODE
|AT_UID
|AT_GID
|AT_TYPE
;
4744 vattr
.va_mode
= S_IFREG
|0777;
4748 vsec
.vsa_mask
= VSA_ACE
;
4749 vsec
.vsa_aclentp
= &full_access
;
4750 vsec
.vsa_aclentsz
= sizeof (full_access
);
4751 vsec
.vsa_aclcnt
= 1;
4753 error
= VOP_CREATE(ZTOV(sharedir
), zc
->zc_string
,
4754 &vattr
, EXCL
, 0, &resourcevp
, kcred
, 0, NULL
, &vsec
);
4756 VN_RELE(resourcevp
);
4759 case ZFS_SMB_ACL_REMOVE
:
4760 error
= VOP_REMOVE(ZTOV(sharedir
), zc
->zc_string
, kcred
,
4764 case ZFS_SMB_ACL_RENAME
:
4765 if ((error
= get_nvlist(zc
->zc_nvlist_src
,
4766 zc
->zc_nvlist_src_size
, zc
->zc_iflags
, &nvlist
)) != 0) {
4771 if (nvlist_lookup_string(nvlist
, ZFS_SMB_ACL_SRC
, &src
) ||
4772 nvlist_lookup_string(nvlist
, ZFS_SMB_ACL_TARGET
,
4775 VN_RELE(ZTOV(sharedir
));
4777 nvlist_free(nvlist
);
4780 error
= VOP_RENAME(ZTOV(sharedir
), src
, ZTOV(sharedir
), target
,
4782 nvlist_free(nvlist
);
4785 case ZFS_SMB_ACL_PURGE
:
4786 error
= zfs_smb_acl_purge(sharedir
);
4790 error
= SET_ERROR(EINVAL
);
4795 VN_RELE(ZTOV(sharedir
));
4801 return (SET_ERROR(ENOTSUP
));
4802 #endif /* HAVE_SMB_SHARE */
4807 * "holds" -> { snapname -> holdname (string), ... }
4808 * (optional) "cleanup_fd" -> fd (int32)
4812 * snapname -> error value (int32)
4818 zfs_ioc_hold(const char *pool
, nvlist_t
*args
, nvlist_t
*errlist
)
4821 int cleanup_fd
= -1;
4825 error
= nvlist_lookup_nvlist(args
, "holds", &holds
);
4827 return (SET_ERROR(EINVAL
));
4829 if (nvlist_lookup_int32(args
, "cleanup_fd", &cleanup_fd
) == 0) {
4830 error
= zfs_onexit_fd_hold(cleanup_fd
, &minor
);
4835 error
= dsl_dataset_user_hold(holds
, minor
, errlist
);
4837 zfs_onexit_fd_rele(cleanup_fd
);
4842 * innvl is not used.
4845 * holdname -> time added (uint64 seconds since epoch)
4851 zfs_ioc_get_holds(const char *snapname
, nvlist_t
*args
, nvlist_t
*outnvl
)
4853 return (dsl_dataset_get_holds(snapname
, outnvl
));
4858 * snapname -> { holdname, ... }
4863 * snapname -> error value (int32)
4869 zfs_ioc_release(const char *pool
, nvlist_t
*holds
, nvlist_t
*errlist
)
4871 return (dsl_dataset_user_release(holds
, errlist
));
4876 * zc_guid flags (ZEVENT_NONBLOCK)
4879 * zc_nvlist_dst next nvlist event
4880 * zc_cookie dropped events since last get
4881 * zc_cleanup_fd cleanup-on-exit file descriptor
4884 zfs_ioc_events_next(zfs_cmd_t
*zc
)
4887 nvlist_t
*event
= NULL
;
4889 uint64_t dropped
= 0;
4892 error
= zfs_zevent_fd_hold(zc
->zc_cleanup_fd
, &minor
, &ze
);
4897 error
= zfs_zevent_next(ze
, &event
,
4898 &zc
->zc_nvlist_dst_size
, &dropped
);
4899 if (event
!= NULL
) {
4900 zc
->zc_cookie
= dropped
;
4901 error
= put_nvlist(zc
, event
);
4905 if (zc
->zc_guid
& ZEVENT_NONBLOCK
)
4908 if ((error
== 0) || (error
!= ENOENT
))
4911 error
= zfs_zevent_wait(ze
);
4916 zfs_zevent_fd_rele(zc
->zc_cleanup_fd
);
4923 * zc_cookie cleared events count
4926 zfs_ioc_events_clear(zfs_cmd_t
*zc
)
4930 zfs_zevent_drain_all(&count
);
4931 zc
->zc_cookie
= count
;
4938 * zc_name name of new filesystem or snapshot
4939 * zc_value full name of old snapshot
4942 * zc_cookie space in bytes
4943 * zc_objset_type compressed space in bytes
4944 * zc_perm_action uncompressed space in bytes
4947 zfs_ioc_space_written(zfs_cmd_t
*zc
)
4951 dsl_dataset_t
*new, *old
;
4953 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
4956 error
= dsl_dataset_hold(dp
, zc
->zc_name
, FTAG
, &new);
4958 dsl_pool_rele(dp
, FTAG
);
4961 error
= dsl_dataset_hold(dp
, zc
->zc_value
, FTAG
, &old
);
4963 dsl_dataset_rele(new, FTAG
);
4964 dsl_pool_rele(dp
, FTAG
);
4968 error
= dsl_dataset_space_written(old
, new, &zc
->zc_cookie
,
4969 &zc
->zc_objset_type
, &zc
->zc_perm_action
);
4970 dsl_dataset_rele(old
, FTAG
);
4971 dsl_dataset_rele(new, FTAG
);
4972 dsl_pool_rele(dp
, FTAG
);
4978 * "firstsnap" -> snapshot name
4982 * "used" -> space in bytes
4983 * "compressed" -> compressed space in bytes
4984 * "uncompressed" -> uncompressed space in bytes
4988 zfs_ioc_space_snaps(const char *lastsnap
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
4992 dsl_dataset_t
*new, *old
;
4994 uint64_t used
, comp
, uncomp
;
4996 if (nvlist_lookup_string(innvl
, "firstsnap", &firstsnap
) != 0)
4997 return (SET_ERROR(EINVAL
));
4999 error
= dsl_pool_hold(lastsnap
, FTAG
, &dp
);
5003 error
= dsl_dataset_hold(dp
, lastsnap
, FTAG
, &new);
5005 dsl_pool_rele(dp
, FTAG
);
5008 error
= dsl_dataset_hold(dp
, firstsnap
, FTAG
, &old
);
5010 dsl_dataset_rele(new, FTAG
);
5011 dsl_pool_rele(dp
, FTAG
);
5015 error
= dsl_dataset_space_wouldfree(old
, new, &used
, &comp
, &uncomp
);
5016 dsl_dataset_rele(old
, FTAG
);
5017 dsl_dataset_rele(new, FTAG
);
5018 dsl_pool_rele(dp
, FTAG
);
5019 fnvlist_add_uint64(outnvl
, "used", used
);
5020 fnvlist_add_uint64(outnvl
, "compressed", comp
);
5021 fnvlist_add_uint64(outnvl
, "uncompressed", uncomp
);
5027 * "fd" -> file descriptor to write stream to (int32)
5028 * (optional) "fromsnap" -> full snap name to send an incremental from
5035 zfs_ioc_send_new(const char *snapname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
5039 char *fromname
= NULL
;
5043 error
= nvlist_lookup_int32(innvl
, "fd", &fd
);
5045 return (SET_ERROR(EINVAL
));
5047 (void) nvlist_lookup_string(innvl
, "fromsnap", &fromname
);
5049 if ((fp
= getf(fd
)) == NULL
)
5050 return (SET_ERROR(EBADF
));
5053 error
= dmu_send(snapname
, fromname
, fd
, fp
->f_vnode
, &off
);
5055 if (VOP_SEEK(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
5063 * Determine approximately how large a zfs send stream will be -- the number
5064 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
5067 * (optional) "fromsnap" -> full snap name to send an incremental from
5071 * "space" -> bytes of space (uint64)
5075 zfs_ioc_send_space(const char *snapname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
5078 dsl_dataset_t
*fromsnap
= NULL
;
5079 dsl_dataset_t
*tosnap
;
5084 error
= dsl_pool_hold(snapname
, FTAG
, &dp
);
5088 error
= dsl_dataset_hold(dp
, snapname
, FTAG
, &tosnap
);
5090 dsl_pool_rele(dp
, FTAG
);
5094 error
= nvlist_lookup_string(innvl
, "fromsnap", &fromname
);
5096 error
= dsl_dataset_hold(dp
, fromname
, FTAG
, &fromsnap
);
5098 dsl_dataset_rele(tosnap
, FTAG
);
5099 dsl_pool_rele(dp
, FTAG
);
5104 error
= dmu_send_estimate(tosnap
, fromsnap
, &space
);
5105 fnvlist_add_uint64(outnvl
, "space", space
);
5107 if (fromsnap
!= NULL
)
5108 dsl_dataset_rele(fromsnap
, FTAG
);
5109 dsl_dataset_rele(tosnap
, FTAG
);
5110 dsl_pool_rele(dp
, FTAG
);
5115 static zfs_ioc_vec_t zfs_ioc_vec
[ZFS_IOC_LAST
- ZFS_IOC_FIRST
];
5118 zfs_ioctl_register_legacy(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5119 zfs_secpolicy_func_t
*secpolicy
, zfs_ioc_namecheck_t namecheck
,
5120 boolean_t log_history
, zfs_ioc_poolcheck_t pool_check
)
5122 zfs_ioc_vec_t
*vec
= &zfs_ioc_vec
[ioc
- ZFS_IOC_FIRST
];
5124 ASSERT3U(ioc
, >=, ZFS_IOC_FIRST
);
5125 ASSERT3U(ioc
, <, ZFS_IOC_LAST
);
5126 ASSERT3P(vec
->zvec_legacy_func
, ==, NULL
);
5127 ASSERT3P(vec
->zvec_func
, ==, NULL
);
5129 vec
->zvec_legacy_func
= func
;
5130 vec
->zvec_secpolicy
= secpolicy
;
5131 vec
->zvec_namecheck
= namecheck
;
5132 vec
->zvec_allow_log
= log_history
;
5133 vec
->zvec_pool_check
= pool_check
;
5137 * See the block comment at the beginning of this file for details on
5138 * each argument to this function.
5141 zfs_ioctl_register(const char *name
, zfs_ioc_t ioc
, zfs_ioc_func_t
*func
,
5142 zfs_secpolicy_func_t
*secpolicy
, zfs_ioc_namecheck_t namecheck
,
5143 zfs_ioc_poolcheck_t pool_check
, boolean_t smush_outnvlist
,
5144 boolean_t allow_log
)
5146 zfs_ioc_vec_t
*vec
= &zfs_ioc_vec
[ioc
- ZFS_IOC_FIRST
];
5148 ASSERT3U(ioc
, >=, ZFS_IOC_FIRST
);
5149 ASSERT3U(ioc
, <, ZFS_IOC_LAST
);
5150 ASSERT3P(vec
->zvec_legacy_func
, ==, NULL
);
5151 ASSERT3P(vec
->zvec_func
, ==, NULL
);
5153 /* if we are logging, the name must be valid */
5154 ASSERT(!allow_log
|| namecheck
!= NO_NAME
);
5156 vec
->zvec_name
= name
;
5157 vec
->zvec_func
= func
;
5158 vec
->zvec_secpolicy
= secpolicy
;
5159 vec
->zvec_namecheck
= namecheck
;
5160 vec
->zvec_pool_check
= pool_check
;
5161 vec
->zvec_smush_outnvlist
= smush_outnvlist
;
5162 vec
->zvec_allow_log
= allow_log
;
5166 zfs_ioctl_register_pool(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5167 zfs_secpolicy_func_t
*secpolicy
, boolean_t log_history
,
5168 zfs_ioc_poolcheck_t pool_check
)
5170 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5171 POOL_NAME
, log_history
, pool_check
);
5175 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5176 zfs_secpolicy_func_t
*secpolicy
, zfs_ioc_poolcheck_t pool_check
)
5178 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5179 DATASET_NAME
, B_FALSE
, pool_check
);
5183 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
)
5185 zfs_ioctl_register_legacy(ioc
, func
, zfs_secpolicy_config
,
5186 POOL_NAME
, B_TRUE
, POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
5190 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5191 zfs_secpolicy_func_t
*secpolicy
)
5193 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5194 NO_NAME
, B_FALSE
, POOL_CHECK_NONE
);
5198 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc
,
5199 zfs_ioc_legacy_func_t
*func
, zfs_secpolicy_func_t
*secpolicy
)
5201 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5202 DATASET_NAME
, B_FALSE
, POOL_CHECK_SUSPENDED
);
5206 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
)
5208 zfs_ioctl_register_dataset_read_secpolicy(ioc
, func
,
5209 zfs_secpolicy_read
);
5213 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5214 zfs_secpolicy_func_t
*secpolicy
)
5216 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5217 DATASET_NAME
, B_TRUE
, POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
5221 zfs_ioctl_init(void)
5223 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT
,
5224 zfs_ioc_snapshot
, zfs_secpolicy_snapshot
, POOL_NAME
,
5225 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5227 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY
,
5228 zfs_ioc_log_history
, zfs_secpolicy_log_history
, NO_NAME
,
5229 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_FALSE
, B_FALSE
);
5231 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS
,
5232 zfs_ioc_space_snaps
, zfs_secpolicy_read
, DATASET_NAME
,
5233 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
5235 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW
,
5236 zfs_ioc_send_new
, zfs_secpolicy_send_new
, DATASET_NAME
,
5237 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
5239 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE
,
5240 zfs_ioc_send_space
, zfs_secpolicy_read
, DATASET_NAME
,
5241 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
5243 zfs_ioctl_register("create", ZFS_IOC_CREATE
,
5244 zfs_ioc_create
, zfs_secpolicy_create_clone
, DATASET_NAME
,
5245 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5247 zfs_ioctl_register("clone", ZFS_IOC_CLONE
,
5248 zfs_ioc_clone
, zfs_secpolicy_create_clone
, DATASET_NAME
,
5249 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5251 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS
,
5252 zfs_ioc_destroy_snaps
, zfs_secpolicy_destroy_snaps
, POOL_NAME
,
5253 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5255 zfs_ioctl_register("hold", ZFS_IOC_HOLD
,
5256 zfs_ioc_hold
, zfs_secpolicy_hold
, POOL_NAME
,
5257 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5258 zfs_ioctl_register("release", ZFS_IOC_RELEASE
,
5259 zfs_ioc_release
, zfs_secpolicy_release
, POOL_NAME
,
5260 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5262 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS
,
5263 zfs_ioc_get_holds
, zfs_secpolicy_read
, DATASET_NAME
,
5264 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
5266 /* IOCTLS that use the legacy function signature */
5268 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE
, zfs_ioc_pool_freeze
,
5269 zfs_secpolicy_config
, NO_NAME
, B_FALSE
, POOL_CHECK_READONLY
);
5271 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE
, zfs_ioc_pool_create
,
5272 zfs_secpolicy_config
, B_TRUE
, POOL_CHECK_NONE
);
5273 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN
,
5275 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE
,
5276 zfs_ioc_pool_upgrade
);
5277 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD
,
5279 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE
,
5280 zfs_ioc_vdev_remove
);
5281 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE
,
5282 zfs_ioc_vdev_set_state
);
5283 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH
,
5284 zfs_ioc_vdev_attach
);
5285 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH
,
5286 zfs_ioc_vdev_detach
);
5287 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH
,
5288 zfs_ioc_vdev_setpath
);
5289 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU
,
5290 zfs_ioc_vdev_setfru
);
5291 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS
,
5292 zfs_ioc_pool_set_props
);
5293 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT
,
5294 zfs_ioc_vdev_split
);
5295 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID
,
5296 zfs_ioc_pool_reguid
);
5298 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS
,
5299 zfs_ioc_pool_configs
, zfs_secpolicy_none
);
5300 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT
,
5301 zfs_ioc_pool_tryimport
, zfs_secpolicy_config
);
5302 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT
,
5303 zfs_ioc_inject_fault
, zfs_secpolicy_inject
);
5304 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT
,
5305 zfs_ioc_clear_fault
, zfs_secpolicy_inject
);
5306 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT
,
5307 zfs_ioc_inject_list_next
, zfs_secpolicy_inject
);
5310 * pool destroy, and export don't log the history as part of
5311 * zfsdev_ioctl, but rather zfs_ioc_pool_export
5312 * does the logging of those commands.
5314 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY
, zfs_ioc_pool_destroy
,
5315 zfs_secpolicy_config
, B_FALSE
, POOL_CHECK_NONE
);
5316 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT
, zfs_ioc_pool_export
,
5317 zfs_secpolicy_config
, B_FALSE
, POOL_CHECK_NONE
);
5319 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS
, zfs_ioc_pool_stats
,
5320 zfs_secpolicy_read
, B_FALSE
, POOL_CHECK_NONE
);
5321 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS
, zfs_ioc_pool_get_props
,
5322 zfs_secpolicy_read
, B_FALSE
, POOL_CHECK_NONE
);
5324 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG
, zfs_ioc_error_log
,
5325 zfs_secpolicy_inject
, B_FALSE
, POOL_CHECK_SUSPENDED
);
5326 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME
,
5327 zfs_ioc_dsobj_to_dsname
,
5328 zfs_secpolicy_diff
, B_FALSE
, POOL_CHECK_SUSPENDED
);
5329 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY
,
5330 zfs_ioc_pool_get_history
,
5331 zfs_secpolicy_config
, B_FALSE
, POOL_CHECK_SUSPENDED
);
5333 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT
, zfs_ioc_pool_import
,
5334 zfs_secpolicy_config
, B_TRUE
, POOL_CHECK_NONE
);
5336 zfs_ioctl_register_pool(ZFS_IOC_CLEAR
, zfs_ioc_clear
,
5337 zfs_secpolicy_config
, B_TRUE
, POOL_CHECK_SUSPENDED
);
5338 zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN
, zfs_ioc_pool_reopen
,
5339 zfs_secpolicy_config
, B_TRUE
, POOL_CHECK_SUSPENDED
);
5341 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN
,
5342 zfs_ioc_space_written
);
5343 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS
,
5344 zfs_ioc_objset_recvd_props
);
5345 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ
,
5347 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL
,
5349 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS
,
5350 zfs_ioc_objset_stats
);
5351 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS
,
5352 zfs_ioc_objset_zplprops
);
5353 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT
,
5354 zfs_ioc_dataset_list_next
);
5355 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT
,
5356 zfs_ioc_snapshot_list_next
);
5357 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS
,
5358 zfs_ioc_send_progress
);
5360 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF
,
5361 zfs_ioc_diff
, zfs_secpolicy_diff
);
5362 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS
,
5363 zfs_ioc_obj_to_stats
, zfs_secpolicy_diff
);
5364 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH
,
5365 zfs_ioc_obj_to_path
, zfs_secpolicy_diff
);
5366 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE
,
5367 zfs_ioc_userspace_one
, zfs_secpolicy_userspace_one
);
5368 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY
,
5369 zfs_ioc_userspace_many
, zfs_secpolicy_userspace_many
);
5370 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND
,
5371 zfs_ioc_send
, zfs_secpolicy_send
);
5373 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP
, zfs_ioc_set_prop
,
5374 zfs_secpolicy_none
);
5375 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY
, zfs_ioc_destroy
,
5376 zfs_secpolicy_destroy
);
5377 zfs_ioctl_register_dataset_modify(ZFS_IOC_ROLLBACK
, zfs_ioc_rollback
,
5378 zfs_secpolicy_rollback
);
5379 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME
, zfs_ioc_rename
,
5380 zfs_secpolicy_rename
);
5381 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV
, zfs_ioc_recv
,
5382 zfs_secpolicy_recv
);
5383 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE
, zfs_ioc_promote
,
5384 zfs_secpolicy_promote
);
5385 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP
,
5386 zfs_ioc_inherit_prop
, zfs_secpolicy_inherit_prop
);
5387 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL
, zfs_ioc_set_fsacl
,
5388 zfs_secpolicy_set_fsacl
);
5390 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE
, zfs_ioc_share
,
5391 zfs_secpolicy_share
, POOL_CHECK_NONE
);
5392 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL
, zfs_ioc_smb_acl
,
5393 zfs_secpolicy_smb_acl
, POOL_CHECK_NONE
);
5394 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE
,
5395 zfs_ioc_userspace_upgrade
, zfs_secpolicy_userspace_upgrade
,
5396 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
5397 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT
,
5398 zfs_ioc_tmp_snapshot
, zfs_secpolicy_tmp_snapshot
,
5399 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
5404 zfs_ioctl_register_legacy(ZFS_IOC_CREATE_MINOR
, zfs_ioc_create_minor
,
5405 zfs_secpolicy_config
, DATASET_NAME
, B_FALSE
, POOL_CHECK_NONE
);
5406 zfs_ioctl_register_legacy(ZFS_IOC_REMOVE_MINOR
, zfs_ioc_remove_minor
,
5407 zfs_secpolicy_config
, DATASET_NAME
, B_FALSE
, POOL_CHECK_NONE
);
5408 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_NEXT
, zfs_ioc_events_next
,
5409 zfs_secpolicy_config
, NO_NAME
, B_FALSE
, POOL_CHECK_NONE
);
5410 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_CLEAR
, zfs_ioc_events_clear
,
5411 zfs_secpolicy_config
, NO_NAME
, B_FALSE
, POOL_CHECK_NONE
);
5415 pool_status_check(const char *name
, zfs_ioc_namecheck_t type
,
5416 zfs_ioc_poolcheck_t check
)
5421 ASSERT(type
== POOL_NAME
|| type
== DATASET_NAME
);
5423 if (check
& POOL_CHECK_NONE
)
5426 error
= spa_open(name
, &spa
, FTAG
);
5428 if ((check
& POOL_CHECK_SUSPENDED
) && spa_suspended(spa
))
5429 error
= SET_ERROR(EAGAIN
);
5430 else if ((check
& POOL_CHECK_READONLY
) && !spa_writeable(spa
))
5431 error
= SET_ERROR(EROFS
);
5432 spa_close(spa
, FTAG
);
5438 zfsdev_get_state_impl(minor_t minor
, enum zfsdev_state_type which
)
5442 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
5444 for (zs
= list_head(&zfsdev_state_list
); zs
!= NULL
;
5445 zs
= list_next(&zfsdev_state_list
, zs
)) {
5446 if (zs
->zs_minor
== minor
) {
5448 case ZST_ONEXIT
: return (zs
->zs_onexit
);
5449 case ZST_ZEVENT
: return (zs
->zs_zevent
);
5450 case ZST_ALL
: return (zs
);
5459 zfsdev_get_state(minor_t minor
, enum zfsdev_state_type which
)
5463 mutex_enter(&zfsdev_state_lock
);
5464 ptr
= zfsdev_get_state_impl(minor
, which
);
5465 mutex_exit(&zfsdev_state_lock
);
5471 zfsdev_getminor(struct file
*filp
)
5473 ASSERT(filp
!= NULL
);
5474 ASSERT(filp
->private_data
!= NULL
);
5476 return (((zfsdev_state_t
*)filp
->private_data
)->zs_minor
);
5480 * Find a free minor number. The zfsdev_state_list is expected to
5481 * be short since it is only a list of currently open file handles.
5484 zfsdev_minor_alloc(void)
5486 static minor_t last_minor
= 0;
5489 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
5491 for (m
= last_minor
+ 1; m
!= last_minor
; m
++) {
5492 if (m
> ZFSDEV_MAX_MINOR
)
5494 if (zfsdev_get_state_impl(m
, ZST_ALL
) == NULL
) {
5504 zfsdev_state_init(struct file
*filp
)
5509 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
5511 minor
= zfsdev_minor_alloc();
5513 return (SET_ERROR(ENXIO
));
5515 zs
= kmem_zalloc( sizeof(zfsdev_state_t
), KM_SLEEP
);
5518 zs
->zs_minor
= minor
;
5519 filp
->private_data
= zs
;
5521 zfs_onexit_init((zfs_onexit_t
**)&zs
->zs_onexit
);
5522 zfs_zevent_init((zfs_zevent_t
**)&zs
->zs_zevent
);
5524 list_insert_tail(&zfsdev_state_list
, zs
);
5530 zfsdev_state_destroy(struct file
*filp
)
5534 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
5535 ASSERT(filp
->private_data
!= NULL
);
5537 zs
= filp
->private_data
;
5538 zfs_onexit_destroy(zs
->zs_onexit
);
5539 zfs_zevent_destroy(zs
->zs_zevent
);
5541 list_remove(&zfsdev_state_list
, zs
);
5542 kmem_free(zs
, sizeof(zfsdev_state_t
));
5548 zfsdev_open(struct inode
*ino
, struct file
*filp
)
5552 mutex_enter(&zfsdev_state_lock
);
5553 error
= zfsdev_state_init(filp
);
5554 mutex_exit(&zfsdev_state_lock
);
5560 zfsdev_release(struct inode
*ino
, struct file
*filp
)
5564 mutex_enter(&zfsdev_state_lock
);
5565 error
= zfsdev_state_destroy(filp
);
5566 mutex_exit(&zfsdev_state_lock
);
5572 zfsdev_ioctl(struct file
*filp
, unsigned cmd
, unsigned long arg
)
5576 int error
, rc
, len
, flag
= 0;
5577 const zfs_ioc_vec_t
*vec
;
5578 char saved_poolname
[MAXNAMELEN
];
5579 nvlist_t
*innvl
= NULL
;
5581 vecnum
= cmd
- ZFS_IOC_FIRST
;
5582 if (vecnum
>= sizeof (zfs_ioc_vec
) / sizeof (zfs_ioc_vec
[0]))
5583 return (-SET_ERROR(EINVAL
));
5584 vec
= &zfs_ioc_vec
[vecnum
];
5586 zc
= kmem_zalloc(sizeof (zfs_cmd_t
), KM_SLEEP
| KM_NODEBUG
);
5588 error
= ddi_copyin((void *)arg
, zc
, sizeof (zfs_cmd_t
), flag
);
5590 error
= SET_ERROR(EFAULT
);
5594 zc
->zc_iflags
= flag
& FKIOCTL
;
5595 if (zc
->zc_nvlist_src_size
!= 0) {
5596 error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
5597 zc
->zc_iflags
, &innvl
);
5603 * Ensure that all pool/dataset names are valid before we pass down to
5606 zc
->zc_name
[sizeof (zc
->zc_name
) - 1] = '\0';
5607 switch (vec
->zvec_namecheck
) {
5609 if (pool_namecheck(zc
->zc_name
, NULL
, NULL
) != 0)
5610 error
= SET_ERROR(EINVAL
);
5612 error
= pool_status_check(zc
->zc_name
,
5613 vec
->zvec_namecheck
, vec
->zvec_pool_check
);
5617 if (dataset_namecheck(zc
->zc_name
, NULL
, NULL
) != 0)
5618 error
= SET_ERROR(EINVAL
);
5620 error
= pool_status_check(zc
->zc_name
,
5621 vec
->zvec_namecheck
, vec
->zvec_pool_check
);
5629 if (error
== 0 && !(flag
& FKIOCTL
))
5630 error
= vec
->zvec_secpolicy(zc
, innvl
, CRED());
5635 /* legacy ioctls can modify zc_name */
5636 (void) strlcpy(saved_poolname
, zc
->zc_name
, sizeof(saved_poolname
));
5637 len
= strcspn(saved_poolname
, "/@") + 1;
5638 saved_poolname
[len
] = '\0';
5640 if (vec
->zvec_func
!= NULL
) {
5644 nvlist_t
*lognv
= NULL
;
5646 ASSERT(vec
->zvec_legacy_func
== NULL
);
5649 * Add the innvl to the lognv before calling the func,
5650 * in case the func changes the innvl.
5652 if (vec
->zvec_allow_log
) {
5653 lognv
= fnvlist_alloc();
5654 fnvlist_add_string(lognv
, ZPOOL_HIST_IOCTL
,
5656 if (!nvlist_empty(innvl
)) {
5657 fnvlist_add_nvlist(lognv
, ZPOOL_HIST_INPUT_NVL
,
5662 VERIFY0(nvlist_alloc(&outnvl
, NV_UNIQUE_NAME
, KM_PUSHPAGE
));
5663 error
= vec
->zvec_func(zc
->zc_name
, innvl
, outnvl
);
5665 if (error
== 0 && vec
->zvec_allow_log
&&
5666 spa_open(zc
->zc_name
, &spa
, FTAG
) == 0) {
5667 if (!nvlist_empty(outnvl
)) {
5668 fnvlist_add_nvlist(lognv
, ZPOOL_HIST_OUTPUT_NVL
,
5671 (void) spa_history_log_nvl(spa
, lognv
);
5672 spa_close(spa
, FTAG
);
5674 fnvlist_free(lognv
);
5676 if (!nvlist_empty(outnvl
) || zc
->zc_nvlist_dst_size
!= 0) {
5678 if (vec
->zvec_smush_outnvlist
) {
5679 smusherror
= nvlist_smush(outnvl
,
5680 zc
->zc_nvlist_dst_size
);
5682 if (smusherror
== 0)
5683 puterror
= put_nvlist(zc
, outnvl
);
5689 nvlist_free(outnvl
);
5691 error
= vec
->zvec_legacy_func(zc
);
5696 rc
= ddi_copyout(zc
, (void *)arg
, sizeof (zfs_cmd_t
), flag
);
5697 if (error
== 0 && rc
!= 0)
5698 error
= SET_ERROR(EFAULT
);
5699 if (error
== 0 && vec
->zvec_allow_log
) {
5700 char *s
= tsd_get(zfs_allow_log_key
);
5703 (void) tsd_set(zfs_allow_log_key
, strdup(saved_poolname
));
5706 kmem_free(zc
, sizeof (zfs_cmd_t
));
5710 #ifdef CONFIG_COMPAT
5712 zfsdev_compat_ioctl(struct file
*filp
, unsigned cmd
, unsigned long arg
)
5714 return zfsdev_ioctl(filp
, cmd
, arg
);
5717 #define zfsdev_compat_ioctl NULL
5720 static const struct file_operations zfsdev_fops
= {
5721 .open
= zfsdev_open
,
5722 .release
= zfsdev_release
,
5723 .unlocked_ioctl
= zfsdev_ioctl
,
5724 .compat_ioctl
= zfsdev_compat_ioctl
,
5725 .owner
= THIS_MODULE
,
5728 static struct miscdevice zfs_misc
= {
5729 .minor
= MISC_DYNAMIC_MINOR
,
5731 .fops
= &zfsdev_fops
,
5739 mutex_init(&zfsdev_state_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
5740 list_create(&zfsdev_state_list
, sizeof (zfsdev_state_t
),
5741 offsetof(zfsdev_state_t
, zs_next
));
5743 error
= misc_register(&zfs_misc
);
5745 printk(KERN_INFO
"ZFS: misc_register() failed %d\n", error
);
5757 error
= misc_deregister(&zfs_misc
);
5759 printk(KERN_INFO
"ZFS: misc_deregister() failed %d\n", error
);
5761 mutex_destroy(&zfsdev_state_lock
);
5762 list_destroy(&zfsdev_state_list
);
5766 zfs_allow_log_destroy(void *arg
)
5768 char *poolname
= arg
;
5773 #define ZFS_DEBUG_STR " (DEBUG mode)"
5775 #define ZFS_DEBUG_STR ""
5783 spa_init(FREAD
| FWRITE
);
5786 if ((error
= zvol_init()) != 0)
5791 if ((error
= zfs_attach()) != 0)
5794 tsd_create(&zfs_fsyncer_key
, NULL
);
5795 tsd_create(&rrw_tsd_key
, rrw_tsd_destroy
);
5796 tsd_create(&zfs_allow_log_key
, zfs_allow_log_destroy
);
5798 printk(KERN_NOTICE
"ZFS: Loaded module v%s-%s%s, "
5799 "ZFS pool version %s, ZFS filesystem version %s\n",
5800 ZFS_META_VERSION
, ZFS_META_RELEASE
, ZFS_DEBUG_STR
,
5801 SPA_VERSION_STRING
, ZPL_VERSION_STRING
);
5810 printk(KERN_NOTICE
"ZFS: Failed to Load ZFS Filesystem v%s-%s%s"
5811 ", rc = %d\n", ZFS_META_VERSION
, ZFS_META_RELEASE
,
5812 ZFS_DEBUG_STR
, error
);
5825 tsd_destroy(&zfs_fsyncer_key
);
5826 tsd_destroy(&rrw_tsd_key
);
5827 tsd_destroy(&zfs_allow_log_key
);
5829 printk(KERN_NOTICE
"ZFS: Unloaded module v%s-%s%s\n",
5830 ZFS_META_VERSION
, ZFS_META_RELEASE
, ZFS_DEBUG_STR
);
5836 spl_module_init(_init
);
5837 spl_module_exit(_fini
);
5839 MODULE_DESCRIPTION("ZFS");
5840 MODULE_AUTHOR(ZFS_META_AUTHOR
);
5841 MODULE_LICENSE(ZFS_META_LICENSE
);
5842 #endif /* HAVE_SPL */