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 * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
26 * Portions Copyright 2012 Pawel Jakub Dawidek <pawel@dawidek.net>
27 * Copyright (c) 2014, 2016 Joyent, Inc. All rights reserved.
28 * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
29 * Copyright (c) 2014, Joyent, Inc. All rights reserved.
30 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
31 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
32 * Copyright (c) 2013 Steven Hartland. All rights reserved.
33 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
39 * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
40 * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
42 * There are two ways that we handle ioctls: the legacy way where almost
43 * all of the logic is in the ioctl callback, and the new way where most
44 * of the marshalling is handled in the common entry point, zfsdev_ioctl().
46 * Non-legacy ioctls should be registered by calling
47 * zfs_ioctl_register() from zfs_ioctl_init(). The ioctl is invoked
48 * from userland by lzc_ioctl().
50 * The registration arguments are as follows:
53 * The name of the ioctl. This is used for history logging. If the
54 * ioctl returns successfully (the callback returns 0), and allow_log
55 * is true, then a history log entry will be recorded with the input &
56 * output nvlists. The log entry can be printed with "zpool history -i".
59 * The ioctl request number, which userland will pass to ioctl(2).
60 * The ioctl numbers can change from release to release, because
61 * the caller (libzfs) must be matched to the kernel.
63 * zfs_secpolicy_func_t *secpolicy
64 * This function will be called before the zfs_ioc_func_t, to
65 * determine if this operation is permitted. It should return EPERM
66 * on failure, and 0 on success. Checks include determining if the
67 * dataset is visible in this zone, and if the user has either all
68 * zfs privileges in the zone (SYS_MOUNT), or has been granted permission
69 * to do this operation on this dataset with "zfs allow".
71 * zfs_ioc_namecheck_t namecheck
72 * This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
73 * name, a dataset name, or nothing. If the name is not well-formed,
74 * the ioctl will fail and the callback will not be called.
75 * Therefore, the callback can assume that the name is well-formed
76 * (e.g. is null-terminated, doesn't have more than one '@' character,
77 * doesn't have invalid characters).
79 * zfs_ioc_poolcheck_t pool_check
80 * This specifies requirements on the pool state. If the pool does
81 * not meet them (is suspended or is readonly), the ioctl will fail
82 * and the callback will not be called. If any checks are specified
83 * (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
84 * Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
85 * POOL_CHECK_READONLY).
87 * boolean_t smush_outnvlist
88 * If smush_outnvlist is true, then the output is presumed to be a
89 * list of errors, and it will be "smushed" down to fit into the
90 * caller's buffer, by removing some entries and replacing them with a
91 * single "N_MORE_ERRORS" entry indicating how many were removed. See
92 * nvlist_smush() for details. If smush_outnvlist is false, and the
93 * outnvlist does not fit into the userland-provided buffer, then the
94 * ioctl will fail with ENOMEM.
96 * zfs_ioc_func_t *func
97 * The callback function that will perform the operation.
99 * The callback should return 0 on success, or an error number on
100 * failure. If the function fails, the userland ioctl will return -1,
101 * and errno will be set to the callback's return value. The callback
102 * will be called with the following arguments:
105 * The name of the pool or dataset to operate on, from
106 * zfs_cmd_t:zc_name. The 'namecheck' argument specifies the
107 * expected type (pool, dataset, or none).
110 * The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src. Or
111 * NULL if no input nvlist was provided. Changes to this nvlist are
112 * ignored. If the input nvlist could not be deserialized, the
113 * ioctl will fail and the callback will not be called.
116 * The output nvlist, initially empty. The callback can fill it in,
117 * and it will be returned to userland by serializing it into
118 * zfs_cmd_t:zc_nvlist_dst. If it is non-empty, and serialization
119 * fails (e.g. because the caller didn't supply a large enough
120 * buffer), then the overall ioctl will fail. See the
121 * 'smush_nvlist' argument above for additional behaviors.
123 * There are two typical uses of the output nvlist:
124 * - To return state, e.g. property values. In this case,
125 * smush_outnvlist should be false. If the buffer was not large
126 * enough, the caller will reallocate a larger buffer and try
129 * - To return multiple errors from an ioctl which makes on-disk
130 * changes. In this case, smush_outnvlist should be true.
131 * Ioctls which make on-disk modifications should generally not
132 * use the outnvl if they succeed, because the caller can not
133 * distinguish between the operation failing, and
134 * deserialization failing.
137 #include <sys/types.h>
138 #include <sys/param.h>
139 #include <sys/errno.h>
142 #include <sys/modctl.h>
143 #include <sys/open.h>
144 #include <sys/file.h>
145 #include <sys/kmem.h>
146 #include <sys/conf.h>
147 #include <sys/cmn_err.h>
148 #include <sys/stat.h>
149 #include <sys/zfs_ioctl.h>
150 #include <sys/zfs_vfsops.h>
151 #include <sys/zfs_znode.h>
154 #include <sys/spa_impl.h>
155 #include <sys/vdev.h>
156 #include <sys/priv_impl.h>
158 #include <sys/dsl_dir.h>
159 #include <sys/dsl_dataset.h>
160 #include <sys/dsl_prop.h>
161 #include <sys/dsl_deleg.h>
162 #include <sys/dmu_objset.h>
163 #include <sys/dmu_impl.h>
164 #include <sys/dmu_tx.h>
166 #include <sys/sunddi.h>
167 #include <sys/sunldi.h>
168 #include <sys/policy.h>
169 #include <sys/zone.h>
170 #include <sys/nvpair.h>
171 #include <sys/pathname.h>
172 #include <sys/mount.h>
174 #include <sys/fs/zfs.h>
175 #include <sys/zfs_ctldir.h>
176 #include <sys/zfs_dir.h>
177 #include <sys/zfs_onexit.h>
178 #include <sys/zvol.h>
179 #include <sys/dsl_scan.h>
180 #include <sharefs/share.h>
181 #include <sys/fm/util.h>
183 #include <sys/dmu_send.h>
184 #include <sys/dsl_destroy.h>
185 #include <sys/dsl_bookmark.h>
186 #include <sys/dsl_userhold.h>
187 #include <sys/zfeature.h>
188 #include <sys/zio_checksum.h>
190 #include <linux/miscdevice.h>
191 #include <linux/slab.h>
193 #include "zfs_namecheck.h"
194 #include "zfs_prop.h"
195 #include "zfs_deleg.h"
196 #include "zfs_comutil.h"
199 * Limit maximum nvlist size. We don't want users passing in insane values
200 * for zc->zc_nvlist_src_size, since we will need to allocate that much memory.
202 #define MAX_NVLIST_SRC_SIZE KMALLOC_MAX_SIZE
204 kmutex_t zfsdev_state_lock
;
205 zfsdev_state_t
*zfsdev_state_list
;
207 extern void zfs_init(void);
208 extern void zfs_fini(void);
210 uint_t zfs_fsyncer_key
;
211 extern uint_t rrw_tsd_key
;
212 static uint_t zfs_allow_log_key
;
214 typedef int zfs_ioc_legacy_func_t(zfs_cmd_t
*);
215 typedef int zfs_ioc_func_t(const char *, nvlist_t
*, nvlist_t
*);
216 typedef int zfs_secpolicy_func_t(zfs_cmd_t
*, nvlist_t
*, cred_t
*);
222 } zfs_ioc_namecheck_t
;
225 POOL_CHECK_NONE
= 1 << 0,
226 POOL_CHECK_SUSPENDED
= 1 << 1,
227 POOL_CHECK_READONLY
= 1 << 2,
228 } zfs_ioc_poolcheck_t
;
230 typedef struct zfs_ioc_vec
{
231 zfs_ioc_legacy_func_t
*zvec_legacy_func
;
232 zfs_ioc_func_t
*zvec_func
;
233 zfs_secpolicy_func_t
*zvec_secpolicy
;
234 zfs_ioc_namecheck_t zvec_namecheck
;
235 boolean_t zvec_allow_log
;
236 zfs_ioc_poolcheck_t zvec_pool_check
;
237 boolean_t zvec_smush_outnvlist
;
238 const char *zvec_name
;
241 /* This array is indexed by zfs_userquota_prop_t */
242 static const char *userquota_perms
[] = {
243 ZFS_DELEG_PERM_USERUSED
,
244 ZFS_DELEG_PERM_USERQUOTA
,
245 ZFS_DELEG_PERM_GROUPUSED
,
246 ZFS_DELEG_PERM_GROUPQUOTA
,
247 ZFS_DELEG_PERM_USEROBJUSED
,
248 ZFS_DELEG_PERM_USEROBJQUOTA
,
249 ZFS_DELEG_PERM_GROUPOBJUSED
,
250 ZFS_DELEG_PERM_GROUPOBJQUOTA
,
253 static int zfs_ioc_userspace_upgrade(zfs_cmd_t
*zc
);
254 static int zfs_ioc_userobjspace_upgrade(zfs_cmd_t
*zc
);
255 static int zfs_check_settable(const char *name
, nvpair_t
*property
,
257 static int zfs_check_clearable(char *dataset
, nvlist_t
*props
,
259 static int zfs_fill_zplprops_root(uint64_t, nvlist_t
*, nvlist_t
*,
261 int zfs_set_prop_nvlist(const char *, zprop_source_t
, nvlist_t
*, nvlist_t
*);
262 static int get_nvlist(uint64_t nvl
, uint64_t size
, int iflag
, nvlist_t
**nvp
);
265 history_str_free(char *buf
)
267 kmem_free(buf
, HIS_MAX_RECORD_LEN
);
271 history_str_get(zfs_cmd_t
*zc
)
275 if (zc
->zc_history
== 0)
278 buf
= kmem_alloc(HIS_MAX_RECORD_LEN
, KM_SLEEP
);
279 if (copyinstr((void *)(uintptr_t)zc
->zc_history
,
280 buf
, HIS_MAX_RECORD_LEN
, NULL
) != 0) {
281 history_str_free(buf
);
285 buf
[HIS_MAX_RECORD_LEN
-1] = '\0';
291 * Check to see if the named dataset is currently defined as bootable
294 zfs_is_bootfs(const char *name
)
298 if (dmu_objset_hold(name
, FTAG
, &os
) == 0) {
300 ret
= (dmu_objset_id(os
) == spa_bootfs(dmu_objset_spa(os
)));
301 dmu_objset_rele(os
, FTAG
);
308 * Return non-zero if the spa version is less than requested version.
311 zfs_earlier_version(const char *name
, int version
)
315 if (spa_open(name
, &spa
, FTAG
) == 0) {
316 if (spa_version(spa
) < version
) {
317 spa_close(spa
, FTAG
);
320 spa_close(spa
, FTAG
);
326 * Return TRUE if the ZPL version is less than requested version.
329 zpl_earlier_version(const char *name
, int version
)
332 boolean_t rc
= B_TRUE
;
334 if (dmu_objset_hold(name
, FTAG
, &os
) == 0) {
337 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
338 dmu_objset_rele(os
, FTAG
);
341 /* XXX reading from non-owned objset */
342 if (zfs_get_zplprop(os
, ZFS_PROP_VERSION
, &zplversion
) == 0)
343 rc
= zplversion
< version
;
344 dmu_objset_rele(os
, FTAG
);
350 zfs_log_history(zfs_cmd_t
*zc
)
355 if ((buf
= history_str_get(zc
)) == NULL
)
358 if (spa_open(zc
->zc_name
, &spa
, FTAG
) == 0) {
359 if (spa_version(spa
) >= SPA_VERSION_ZPOOL_HISTORY
)
360 (void) spa_history_log(spa
, buf
);
361 spa_close(spa
, FTAG
);
363 history_str_free(buf
);
367 * Policy for top-level read operations (list pools). Requires no privileges,
368 * and can be used in the local zone, as there is no associated dataset.
372 zfs_secpolicy_none(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
378 * Policy for dataset read operations (list children, get statistics). Requires
379 * no privileges, but must be visible in the local zone.
383 zfs_secpolicy_read(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
385 if (INGLOBALZONE(curproc
) ||
386 zone_dataset_visible(zc
->zc_name
, NULL
))
389 return (SET_ERROR(ENOENT
));
393 zfs_dozonecheck_impl(const char *dataset
, uint64_t zoned
, cred_t
*cr
)
398 * The dataset must be visible by this zone -- check this first
399 * so they don't see EPERM on something they shouldn't know about.
401 if (!INGLOBALZONE(curproc
) &&
402 !zone_dataset_visible(dataset
, &writable
))
403 return (SET_ERROR(ENOENT
));
405 if (INGLOBALZONE(curproc
)) {
407 * If the fs is zoned, only root can access it from the
410 if (secpolicy_zfs(cr
) && zoned
)
411 return (SET_ERROR(EPERM
));
414 * If we are in a local zone, the 'zoned' property must be set.
417 return (SET_ERROR(EPERM
));
419 /* must be writable by this zone */
421 return (SET_ERROR(EPERM
));
427 zfs_dozonecheck(const char *dataset
, cred_t
*cr
)
431 if (dsl_prop_get_integer(dataset
, "zoned", &zoned
, NULL
))
432 return (SET_ERROR(ENOENT
));
434 return (zfs_dozonecheck_impl(dataset
, zoned
, cr
));
438 zfs_dozonecheck_ds(const char *dataset
, dsl_dataset_t
*ds
, cred_t
*cr
)
442 if (dsl_prop_get_int_ds(ds
, "zoned", &zoned
))
443 return (SET_ERROR(ENOENT
));
445 return (zfs_dozonecheck_impl(dataset
, zoned
, cr
));
449 zfs_secpolicy_write_perms_ds(const char *name
, dsl_dataset_t
*ds
,
450 const char *perm
, cred_t
*cr
)
454 error
= zfs_dozonecheck_ds(name
, ds
, cr
);
456 error
= secpolicy_zfs(cr
);
458 error
= dsl_deleg_access_impl(ds
, perm
, cr
);
464 zfs_secpolicy_write_perms(const char *name
, const char *perm
, cred_t
*cr
)
471 * First do a quick check for root in the global zone, which
472 * is allowed to do all write_perms. This ensures that zfs_ioc_*
473 * will get to handle nonexistent datasets.
475 if (INGLOBALZONE(curproc
) && secpolicy_zfs(cr
) == 0)
478 error
= dsl_pool_hold(name
, FTAG
, &dp
);
482 error
= dsl_dataset_hold(dp
, name
, FTAG
, &ds
);
484 dsl_pool_rele(dp
, FTAG
);
488 error
= zfs_secpolicy_write_perms_ds(name
, ds
, perm
, cr
);
490 dsl_dataset_rele(ds
, FTAG
);
491 dsl_pool_rele(dp
, FTAG
);
496 * Policy for setting the security label property.
498 * Returns 0 for success, non-zero for access and other errors.
501 zfs_set_slabel_policy(const char *name
, char *strval
, cred_t
*cr
)
504 char ds_hexsl
[MAXNAMELEN
];
505 bslabel_t ds_sl
, new_sl
;
506 boolean_t new_default
= FALSE
;
508 int needed_priv
= -1;
511 /* First get the existing dataset label. */
512 error
= dsl_prop_get(name
, zfs_prop_to_name(ZFS_PROP_MLSLABEL
),
513 1, sizeof (ds_hexsl
), &ds_hexsl
, NULL
);
515 return (SET_ERROR(EPERM
));
517 if (strcasecmp(strval
, ZFS_MLSLABEL_DEFAULT
) == 0)
520 /* The label must be translatable */
521 if (!new_default
&& (hexstr_to_label(strval
, &new_sl
) != 0))
522 return (SET_ERROR(EINVAL
));
525 * In a non-global zone, disallow attempts to set a label that
526 * doesn't match that of the zone; otherwise no other checks
529 if (!INGLOBALZONE(curproc
)) {
530 if (new_default
|| !blequal(&new_sl
, CR_SL(CRED())))
531 return (SET_ERROR(EPERM
));
536 * For global-zone datasets (i.e., those whose zoned property is
537 * "off", verify that the specified new label is valid for the
540 if (dsl_prop_get_integer(name
,
541 zfs_prop_to_name(ZFS_PROP_ZONED
), &zoned
, NULL
))
542 return (SET_ERROR(EPERM
));
544 if (zfs_check_global_label(name
, strval
) != 0)
545 return (SET_ERROR(EPERM
));
549 * If the existing dataset label is nondefault, check if the
550 * dataset is mounted (label cannot be changed while mounted).
551 * Get the zfs_sb_t; if there isn't one, then the dataset isn't
552 * mounted (or isn't a dataset, doesn't exist, ...).
554 if (strcasecmp(ds_hexsl
, ZFS_MLSLABEL_DEFAULT
) != 0) {
556 static char *setsl_tag
= "setsl_tag";
559 * Try to own the dataset; abort if there is any error,
560 * (e.g., already mounted, in use, or other error).
562 error
= dmu_objset_own(name
, DMU_OST_ZFS
, B_TRUE
,
565 return (SET_ERROR(EPERM
));
567 dmu_objset_disown(os
, setsl_tag
);
570 needed_priv
= PRIV_FILE_DOWNGRADE_SL
;
574 if (hexstr_to_label(strval
, &new_sl
) != 0)
575 return (SET_ERROR(EPERM
));
577 if (blstrictdom(&ds_sl
, &new_sl
))
578 needed_priv
= PRIV_FILE_DOWNGRADE_SL
;
579 else if (blstrictdom(&new_sl
, &ds_sl
))
580 needed_priv
= PRIV_FILE_UPGRADE_SL
;
582 /* dataset currently has a default label */
584 needed_priv
= PRIV_FILE_UPGRADE_SL
;
588 if (needed_priv
!= -1)
589 return (PRIV_POLICY(cr
, needed_priv
, B_FALSE
, EPERM
, NULL
));
593 #endif /* HAVE_MLSLABEL */
597 zfs_secpolicy_setprop(const char *dsname
, zfs_prop_t prop
, nvpair_t
*propval
,
603 * Check permissions for special properties.
610 * Disallow setting of 'zoned' from within a local zone.
612 if (!INGLOBALZONE(curproc
))
613 return (SET_ERROR(EPERM
));
617 case ZFS_PROP_FILESYSTEM_LIMIT
:
618 case ZFS_PROP_SNAPSHOT_LIMIT
:
619 if (!INGLOBALZONE(curproc
)) {
621 char setpoint
[ZFS_MAX_DATASET_NAME_LEN
];
623 * Unprivileged users are allowed to modify the
624 * limit on things *under* (ie. contained by)
625 * the thing they own.
627 if (dsl_prop_get_integer(dsname
, "zoned", &zoned
,
629 return (SET_ERROR(EPERM
));
630 if (!zoned
|| strlen(dsname
) <= strlen(setpoint
))
631 return (SET_ERROR(EPERM
));
635 case ZFS_PROP_MLSLABEL
:
636 if (!is_system_labeled())
637 return (SET_ERROR(EPERM
));
639 if (nvpair_value_string(propval
, &strval
) == 0) {
642 err
= zfs_set_slabel_policy(dsname
, strval
, CRED());
649 return (zfs_secpolicy_write_perms(dsname
, zfs_prop_to_name(prop
), cr
));
654 zfs_secpolicy_set_fsacl(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
658 error
= zfs_dozonecheck(zc
->zc_name
, cr
);
663 * permission to set permissions will be evaluated later in
664 * dsl_deleg_can_allow()
671 zfs_secpolicy_rollback(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
673 return (zfs_secpolicy_write_perms(zc
->zc_name
,
674 ZFS_DELEG_PERM_ROLLBACK
, cr
));
679 zfs_secpolicy_send(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
687 * Generate the current snapshot name from the given objsetid, then
688 * use that name for the secpolicy/zone checks.
690 cp
= strchr(zc
->zc_name
, '@');
692 return (SET_ERROR(EINVAL
));
693 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
697 error
= dsl_dataset_hold_obj(dp
, zc
->zc_sendobj
, FTAG
, &ds
);
699 dsl_pool_rele(dp
, FTAG
);
703 dsl_dataset_name(ds
, zc
->zc_name
);
705 error
= zfs_secpolicy_write_perms_ds(zc
->zc_name
, ds
,
706 ZFS_DELEG_PERM_SEND
, cr
);
707 dsl_dataset_rele(ds
, FTAG
);
708 dsl_pool_rele(dp
, FTAG
);
715 zfs_secpolicy_send_new(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
717 return (zfs_secpolicy_write_perms(zc
->zc_name
,
718 ZFS_DELEG_PERM_SEND
, cr
));
721 #ifdef HAVE_SMB_SHARE
724 zfs_secpolicy_deleg_share(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
729 if ((error
= lookupname(zc
->zc_value
, UIO_SYSSPACE
,
730 NO_FOLLOW
, NULL
, &vp
)) != 0)
733 /* Now make sure mntpnt and dataset are ZFS */
735 if (vp
->v_vfsp
->vfs_fstype
!= zfsfstype
||
736 (strcmp((char *)refstr_value(vp
->v_vfsp
->vfs_resource
),
737 zc
->zc_name
) != 0)) {
739 return (SET_ERROR(EPERM
));
743 return (dsl_deleg_access(zc
->zc_name
,
744 ZFS_DELEG_PERM_SHARE
, cr
));
746 #endif /* HAVE_SMB_SHARE */
749 zfs_secpolicy_share(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
751 #ifdef HAVE_SMB_SHARE
752 if (!INGLOBALZONE(curproc
))
753 return (SET_ERROR(EPERM
));
755 if (secpolicy_nfs(cr
) == 0) {
758 return (zfs_secpolicy_deleg_share(zc
, innvl
, cr
));
761 return (SET_ERROR(ENOTSUP
));
762 #endif /* HAVE_SMB_SHARE */
766 zfs_secpolicy_smb_acl(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
768 #ifdef HAVE_SMB_SHARE
769 if (!INGLOBALZONE(curproc
))
770 return (SET_ERROR(EPERM
));
772 if (secpolicy_smb(cr
) == 0) {
775 return (zfs_secpolicy_deleg_share(zc
, innvl
, cr
));
778 return (SET_ERROR(ENOTSUP
));
779 #endif /* HAVE_SMB_SHARE */
783 zfs_get_parent(const char *datasetname
, char *parent
, int parentsize
)
788 * Remove the @bla or /bla from the end of the name to get the parent.
790 (void) strncpy(parent
, datasetname
, parentsize
);
791 cp
= strrchr(parent
, '@');
795 cp
= strrchr(parent
, '/');
797 return (SET_ERROR(ENOENT
));
805 zfs_secpolicy_destroy_perms(const char *name
, cred_t
*cr
)
809 if ((error
= zfs_secpolicy_write_perms(name
,
810 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
813 return (zfs_secpolicy_write_perms(name
, ZFS_DELEG_PERM_DESTROY
, cr
));
818 zfs_secpolicy_destroy(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
820 return (zfs_secpolicy_destroy_perms(zc
->zc_name
, cr
));
824 * Destroying snapshots with delegated permissions requires
825 * descendant mount and destroy permissions.
829 zfs_secpolicy_destroy_snaps(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
832 nvpair_t
*pair
, *nextpair
;
835 if (nvlist_lookup_nvlist(innvl
, "snaps", &snaps
) != 0)
836 return (SET_ERROR(EINVAL
));
837 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
839 nextpair
= nvlist_next_nvpair(snaps
, pair
);
840 error
= zfs_secpolicy_destroy_perms(nvpair_name(pair
), cr
);
841 if (error
== ENOENT
) {
843 * Ignore any snapshots that don't exist (we consider
844 * them "already destroyed"). Remove the name from the
845 * nvl here in case the snapshot is created between
846 * now and when we try to destroy it (in which case
847 * we don't want to destroy it since we haven't
848 * checked for permission).
850 fnvlist_remove_nvpair(snaps
, pair
);
861 zfs_secpolicy_rename_perms(const char *from
, const char *to
, cred_t
*cr
)
863 char parentname
[ZFS_MAX_DATASET_NAME_LEN
];
866 if ((error
= zfs_secpolicy_write_perms(from
,
867 ZFS_DELEG_PERM_RENAME
, cr
)) != 0)
870 if ((error
= zfs_secpolicy_write_perms(from
,
871 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
874 if ((error
= zfs_get_parent(to
, parentname
,
875 sizeof (parentname
))) != 0)
878 if ((error
= zfs_secpolicy_write_perms(parentname
,
879 ZFS_DELEG_PERM_CREATE
, cr
)) != 0)
882 if ((error
= zfs_secpolicy_write_perms(parentname
,
883 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
891 zfs_secpolicy_rename(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
893 return (zfs_secpolicy_rename_perms(zc
->zc_name
, zc
->zc_value
, cr
));
898 zfs_secpolicy_promote(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
901 dsl_dataset_t
*clone
;
904 error
= zfs_secpolicy_write_perms(zc
->zc_name
,
905 ZFS_DELEG_PERM_PROMOTE
, cr
);
909 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
913 error
= dsl_dataset_hold(dp
, zc
->zc_name
, FTAG
, &clone
);
916 char parentname
[ZFS_MAX_DATASET_NAME_LEN
];
917 dsl_dataset_t
*origin
= NULL
;
921 error
= dsl_dataset_hold_obj(dd
->dd_pool
,
922 dsl_dir_phys(dd
)->dd_origin_obj
, FTAG
, &origin
);
924 dsl_dataset_rele(clone
, FTAG
);
925 dsl_pool_rele(dp
, FTAG
);
929 error
= zfs_secpolicy_write_perms_ds(zc
->zc_name
, clone
,
930 ZFS_DELEG_PERM_MOUNT
, cr
);
932 dsl_dataset_name(origin
, parentname
);
934 error
= zfs_secpolicy_write_perms_ds(parentname
, origin
,
935 ZFS_DELEG_PERM_PROMOTE
, cr
);
937 dsl_dataset_rele(clone
, FTAG
);
938 dsl_dataset_rele(origin
, FTAG
);
940 dsl_pool_rele(dp
, FTAG
);
946 zfs_secpolicy_recv(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
950 if ((error
= zfs_secpolicy_write_perms(zc
->zc_name
,
951 ZFS_DELEG_PERM_RECEIVE
, cr
)) != 0)
954 if ((error
= zfs_secpolicy_write_perms(zc
->zc_name
,
955 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
958 return (zfs_secpolicy_write_perms(zc
->zc_name
,
959 ZFS_DELEG_PERM_CREATE
, cr
));
964 zfs_secpolicy_recv_new(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
966 return (zfs_secpolicy_recv(zc
, innvl
, cr
));
970 zfs_secpolicy_snapshot_perms(const char *name
, cred_t
*cr
)
972 return (zfs_secpolicy_write_perms(name
,
973 ZFS_DELEG_PERM_SNAPSHOT
, cr
));
977 * Check for permission to create each snapshot in the nvlist.
981 zfs_secpolicy_snapshot(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
987 if (nvlist_lookup_nvlist(innvl
, "snaps", &snaps
) != 0)
988 return (SET_ERROR(EINVAL
));
989 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
990 pair
= nvlist_next_nvpair(snaps
, pair
)) {
991 char *name
= nvpair_name(pair
);
992 char *atp
= strchr(name
, '@');
995 error
= SET_ERROR(EINVAL
);
999 error
= zfs_secpolicy_snapshot_perms(name
, cr
);
1008 * Check for permission to create each snapshot in the nvlist.
1012 zfs_secpolicy_bookmark(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1017 for (pair
= nvlist_next_nvpair(innvl
, NULL
);
1018 pair
!= NULL
; pair
= nvlist_next_nvpair(innvl
, pair
)) {
1019 char *name
= nvpair_name(pair
);
1020 char *hashp
= strchr(name
, '#');
1022 if (hashp
== NULL
) {
1023 error
= SET_ERROR(EINVAL
);
1027 error
= zfs_secpolicy_write_perms(name
,
1028 ZFS_DELEG_PERM_BOOKMARK
, cr
);
1038 zfs_secpolicy_destroy_bookmarks(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1040 nvpair_t
*pair
, *nextpair
;
1043 for (pair
= nvlist_next_nvpair(innvl
, NULL
); pair
!= NULL
;
1045 char *name
= nvpair_name(pair
);
1046 char *hashp
= strchr(name
, '#');
1047 nextpair
= nvlist_next_nvpair(innvl
, pair
);
1049 if (hashp
== NULL
) {
1050 error
= SET_ERROR(EINVAL
);
1055 error
= zfs_secpolicy_write_perms(name
,
1056 ZFS_DELEG_PERM_DESTROY
, cr
);
1058 if (error
== ENOENT
) {
1060 * Ignore any filesystems that don't exist (we consider
1061 * their bookmarks "already destroyed"). Remove
1062 * the name from the nvl here in case the filesystem
1063 * is created between now and when we try to destroy
1064 * the bookmark (in which case we don't want to
1065 * destroy it since we haven't checked for permission).
1067 fnvlist_remove_nvpair(innvl
, pair
);
1079 zfs_secpolicy_log_history(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1082 * Even root must have a proper TSD so that we know what pool
1085 if (tsd_get(zfs_allow_log_key
) == NULL
)
1086 return (SET_ERROR(EPERM
));
1091 zfs_secpolicy_create_clone(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1093 char parentname
[ZFS_MAX_DATASET_NAME_LEN
];
1097 if ((error
= zfs_get_parent(zc
->zc_name
, parentname
,
1098 sizeof (parentname
))) != 0)
1101 if (nvlist_lookup_string(innvl
, "origin", &origin
) == 0 &&
1102 (error
= zfs_secpolicy_write_perms(origin
,
1103 ZFS_DELEG_PERM_CLONE
, cr
)) != 0)
1106 if ((error
= zfs_secpolicy_write_perms(parentname
,
1107 ZFS_DELEG_PERM_CREATE
, cr
)) != 0)
1110 return (zfs_secpolicy_write_perms(parentname
,
1111 ZFS_DELEG_PERM_MOUNT
, cr
));
1115 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
1116 * SYS_CONFIG privilege, which is not available in a local zone.
1120 zfs_secpolicy_config(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1122 if (secpolicy_sys_config(cr
, B_FALSE
) != 0)
1123 return (SET_ERROR(EPERM
));
1129 * Policy for object to name lookups.
1133 zfs_secpolicy_diff(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1137 if ((error
= secpolicy_sys_config(cr
, B_FALSE
)) == 0)
1140 error
= zfs_secpolicy_write_perms(zc
->zc_name
, ZFS_DELEG_PERM_DIFF
, cr
);
1145 * Policy for fault injection. Requires all privileges.
1149 zfs_secpolicy_inject(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1151 return (secpolicy_zinject(cr
));
1156 zfs_secpolicy_inherit_prop(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1158 zfs_prop_t prop
= zfs_name_to_prop(zc
->zc_value
);
1160 if (prop
== ZPROP_INVAL
) {
1161 if (!zfs_prop_user(zc
->zc_value
))
1162 return (SET_ERROR(EINVAL
));
1163 return (zfs_secpolicy_write_perms(zc
->zc_name
,
1164 ZFS_DELEG_PERM_USERPROP
, cr
));
1166 return (zfs_secpolicy_setprop(zc
->zc_name
, prop
,
1172 zfs_secpolicy_userspace_one(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1174 int err
= zfs_secpolicy_read(zc
, innvl
, cr
);
1178 if (zc
->zc_objset_type
>= ZFS_NUM_USERQUOTA_PROPS
)
1179 return (SET_ERROR(EINVAL
));
1181 if (zc
->zc_value
[0] == 0) {
1183 * They are asking about a posix uid/gid. If it's
1184 * themself, allow it.
1186 if (zc
->zc_objset_type
== ZFS_PROP_USERUSED
||
1187 zc
->zc_objset_type
== ZFS_PROP_USERQUOTA
||
1188 zc
->zc_objset_type
== ZFS_PROP_USEROBJUSED
||
1189 zc
->zc_objset_type
== ZFS_PROP_USEROBJQUOTA
) {
1190 if (zc
->zc_guid
== crgetuid(cr
))
1193 if (groupmember(zc
->zc_guid
, cr
))
1198 return (zfs_secpolicy_write_perms(zc
->zc_name
,
1199 userquota_perms
[zc
->zc_objset_type
], cr
));
1203 zfs_secpolicy_userspace_many(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1205 int err
= zfs_secpolicy_read(zc
, innvl
, cr
);
1209 if (zc
->zc_objset_type
>= ZFS_NUM_USERQUOTA_PROPS
)
1210 return (SET_ERROR(EINVAL
));
1212 return (zfs_secpolicy_write_perms(zc
->zc_name
,
1213 userquota_perms
[zc
->zc_objset_type
], cr
));
1218 zfs_secpolicy_userspace_upgrade(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1220 return (zfs_secpolicy_setprop(zc
->zc_name
, ZFS_PROP_VERSION
,
1226 zfs_secpolicy_hold(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1232 error
= nvlist_lookup_nvlist(innvl
, "holds", &holds
);
1234 return (SET_ERROR(EINVAL
));
1236 for (pair
= nvlist_next_nvpair(holds
, NULL
); pair
!= NULL
;
1237 pair
= nvlist_next_nvpair(holds
, pair
)) {
1238 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
1239 error
= dmu_fsname(nvpair_name(pair
), fsname
);
1242 error
= zfs_secpolicy_write_perms(fsname
,
1243 ZFS_DELEG_PERM_HOLD
, cr
);
1252 zfs_secpolicy_release(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1257 for (pair
= nvlist_next_nvpair(innvl
, NULL
); pair
!= NULL
;
1258 pair
= nvlist_next_nvpair(innvl
, pair
)) {
1259 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
1260 error
= dmu_fsname(nvpair_name(pair
), fsname
);
1263 error
= zfs_secpolicy_write_perms(fsname
,
1264 ZFS_DELEG_PERM_RELEASE
, cr
);
1272 * Policy for allowing temporary snapshots to be taken or released
1275 zfs_secpolicy_tmp_snapshot(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1278 * A temporary snapshot is the same as a snapshot,
1279 * hold, destroy and release all rolled into one.
1280 * Delegated diff alone is sufficient that we allow this.
1284 if ((error
= zfs_secpolicy_write_perms(zc
->zc_name
,
1285 ZFS_DELEG_PERM_DIFF
, cr
)) == 0)
1288 error
= zfs_secpolicy_snapshot_perms(zc
->zc_name
, cr
);
1290 error
= zfs_secpolicy_hold(zc
, innvl
, cr
);
1292 error
= zfs_secpolicy_release(zc
, innvl
, cr
);
1294 error
= zfs_secpolicy_destroy(zc
, innvl
, cr
);
1299 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1302 get_nvlist(uint64_t nvl
, uint64_t size
, int iflag
, nvlist_t
**nvp
)
1306 nvlist_t
*list
= NULL
;
1309 * Read in and unpack the user-supplied nvlist.
1312 return (SET_ERROR(EINVAL
));
1314 packed
= vmem_alloc(size
, KM_SLEEP
);
1316 if ((error
= ddi_copyin((void *)(uintptr_t)nvl
, packed
, size
,
1318 vmem_free(packed
, size
);
1319 return (SET_ERROR(EFAULT
));
1322 if ((error
= nvlist_unpack(packed
, size
, &list
, 0)) != 0) {
1323 vmem_free(packed
, size
);
1327 vmem_free(packed
, size
);
1334 * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
1335 * Entries will be removed from the end of the nvlist, and one int32 entry
1336 * named "N_MORE_ERRORS" will be added indicating how many entries were
1340 nvlist_smush(nvlist_t
*errors
, size_t max
)
1344 size
= fnvlist_size(errors
);
1347 nvpair_t
*more_errors
;
1351 return (SET_ERROR(ENOMEM
));
1353 fnvlist_add_int32(errors
, ZPROP_N_MORE_ERRORS
, 0);
1354 more_errors
= nvlist_prev_nvpair(errors
, NULL
);
1357 nvpair_t
*pair
= nvlist_prev_nvpair(errors
,
1359 fnvlist_remove_nvpair(errors
, pair
);
1361 size
= fnvlist_size(errors
);
1362 } while (size
> max
);
1364 fnvlist_remove_nvpair(errors
, more_errors
);
1365 fnvlist_add_int32(errors
, ZPROP_N_MORE_ERRORS
, n
);
1366 ASSERT3U(fnvlist_size(errors
), <=, max
);
1373 put_nvlist(zfs_cmd_t
*zc
, nvlist_t
*nvl
)
1375 char *packed
= NULL
;
1379 size
= fnvlist_size(nvl
);
1381 if (size
> zc
->zc_nvlist_dst_size
) {
1382 error
= SET_ERROR(ENOMEM
);
1384 packed
= fnvlist_pack(nvl
, &size
);
1385 if (ddi_copyout(packed
, (void *)(uintptr_t)zc
->zc_nvlist_dst
,
1386 size
, zc
->zc_iflags
) != 0)
1387 error
= SET_ERROR(EFAULT
);
1388 fnvlist_pack_free(packed
, size
);
1391 zc
->zc_nvlist_dst_size
= size
;
1392 zc
->zc_nvlist_dst_filled
= B_TRUE
;
1397 get_zfs_sb(const char *dsname
, zfs_sb_t
**zsbp
)
1402 error
= dmu_objset_hold(dsname
, FTAG
, &os
);
1405 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
1406 dmu_objset_rele(os
, FTAG
);
1407 return (SET_ERROR(EINVAL
));
1410 mutex_enter(&os
->os_user_ptr_lock
);
1411 *zsbp
= dmu_objset_get_user(os
);
1412 /* bump s_active only when non-zero to prevent umount race */
1413 if (*zsbp
== NULL
|| (*zsbp
)->z_sb
== NULL
||
1414 !atomic_inc_not_zero(&((*zsbp
)->z_sb
->s_active
))) {
1415 error
= SET_ERROR(ESRCH
);
1417 mutex_exit(&os
->os_user_ptr_lock
);
1418 dmu_objset_rele(os
, FTAG
);
1423 * Find a zfs_sb_t for a mounted filesystem, or create our own, in which
1424 * case its z_sb will be NULL, and it will be opened as the owner.
1425 * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1426 * which prevents all inode ops from running.
1429 zfs_sb_hold(const char *name
, void *tag
, zfs_sb_t
**zsbp
, boolean_t writer
)
1433 if (get_zfs_sb(name
, zsbp
) != 0)
1434 error
= zfs_sb_create(name
, NULL
, zsbp
);
1436 rrm_enter(&(*zsbp
)->z_teardown_lock
, (writer
) ? RW_WRITER
:
1438 if ((*zsbp
)->z_unmounted
) {
1440 * XXX we could probably try again, since the unmounting
1441 * thread should be just about to disassociate the
1442 * objset from the zsb.
1444 rrm_exit(&(*zsbp
)->z_teardown_lock
, tag
);
1445 return (SET_ERROR(EBUSY
));
1452 zfs_sb_rele(zfs_sb_t
*zsb
, void *tag
)
1454 rrm_exit(&zsb
->z_teardown_lock
, tag
);
1457 deactivate_super(zsb
->z_sb
);
1459 dmu_objset_disown(zsb
->z_os
, zsb
);
1465 zfs_ioc_pool_create(zfs_cmd_t
*zc
)
1468 nvlist_t
*config
, *props
= NULL
;
1469 nvlist_t
*rootprops
= NULL
;
1470 nvlist_t
*zplprops
= NULL
;
1472 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1473 zc
->zc_iflags
, &config
)))
1476 if (zc
->zc_nvlist_src_size
!= 0 && (error
=
1477 get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1478 zc
->zc_iflags
, &props
))) {
1479 nvlist_free(config
);
1484 nvlist_t
*nvl
= NULL
;
1485 uint64_t version
= SPA_VERSION
;
1487 (void) nvlist_lookup_uint64(props
,
1488 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
);
1489 if (!SPA_VERSION_IS_SUPPORTED(version
)) {
1490 error
= SET_ERROR(EINVAL
);
1491 goto pool_props_bad
;
1493 (void) nvlist_lookup_nvlist(props
, ZPOOL_ROOTFS_PROPS
, &nvl
);
1495 error
= nvlist_dup(nvl
, &rootprops
, KM_SLEEP
);
1497 nvlist_free(config
);
1501 (void) nvlist_remove_all(props
, ZPOOL_ROOTFS_PROPS
);
1503 VERIFY(nvlist_alloc(&zplprops
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1504 error
= zfs_fill_zplprops_root(version
, rootprops
,
1507 goto pool_props_bad
;
1510 error
= spa_create(zc
->zc_name
, config
, props
, zplprops
);
1513 * Set the remaining root properties
1515 if (!error
&& (error
= zfs_set_prop_nvlist(zc
->zc_name
,
1516 ZPROP_SRC_LOCAL
, rootprops
, NULL
)) != 0)
1517 (void) spa_destroy(zc
->zc_name
);
1520 nvlist_free(rootprops
);
1521 nvlist_free(zplprops
);
1522 nvlist_free(config
);
1529 zfs_ioc_pool_destroy(zfs_cmd_t
*zc
)
1532 zfs_log_history(zc
);
1533 error
= spa_destroy(zc
->zc_name
);
1539 zfs_ioc_pool_import(zfs_cmd_t
*zc
)
1541 nvlist_t
*config
, *props
= NULL
;
1545 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1546 zc
->zc_iflags
, &config
)) != 0)
1549 if (zc
->zc_nvlist_src_size
!= 0 && (error
=
1550 get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1551 zc
->zc_iflags
, &props
))) {
1552 nvlist_free(config
);
1556 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &guid
) != 0 ||
1557 guid
!= zc
->zc_guid
)
1558 error
= SET_ERROR(EINVAL
);
1560 error
= spa_import(zc
->zc_name
, config
, props
, zc
->zc_cookie
);
1562 if (zc
->zc_nvlist_dst
!= 0) {
1565 if ((err
= put_nvlist(zc
, config
)) != 0)
1569 nvlist_free(config
);
1576 zfs_ioc_pool_export(zfs_cmd_t
*zc
)
1579 boolean_t force
= (boolean_t
)zc
->zc_cookie
;
1580 boolean_t hardforce
= (boolean_t
)zc
->zc_guid
;
1582 zfs_log_history(zc
);
1583 error
= spa_export(zc
->zc_name
, NULL
, force
, hardforce
);
1589 zfs_ioc_pool_configs(zfs_cmd_t
*zc
)
1594 if ((configs
= spa_all_configs(&zc
->zc_cookie
)) == NULL
)
1595 return (SET_ERROR(EEXIST
));
1597 error
= put_nvlist(zc
, configs
);
1599 nvlist_free(configs
);
1606 * zc_name name of the pool
1609 * zc_cookie real errno
1610 * zc_nvlist_dst config nvlist
1611 * zc_nvlist_dst_size size of config nvlist
1614 zfs_ioc_pool_stats(zfs_cmd_t
*zc
)
1620 error
= spa_get_stats(zc
->zc_name
, &config
, zc
->zc_value
,
1621 sizeof (zc
->zc_value
));
1623 if (config
!= NULL
) {
1624 ret
= put_nvlist(zc
, config
);
1625 nvlist_free(config
);
1628 * The config may be present even if 'error' is non-zero.
1629 * In this case we return success, and preserve the real errno
1632 zc
->zc_cookie
= error
;
1641 * Try to import the given pool, returning pool stats as appropriate so that
1642 * user land knows which devices are available and overall pool health.
1645 zfs_ioc_pool_tryimport(zfs_cmd_t
*zc
)
1647 nvlist_t
*tryconfig
, *config
;
1650 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1651 zc
->zc_iflags
, &tryconfig
)) != 0)
1654 config
= spa_tryimport(tryconfig
);
1656 nvlist_free(tryconfig
);
1659 return (SET_ERROR(EINVAL
));
1661 error
= put_nvlist(zc
, config
);
1662 nvlist_free(config
);
1669 * zc_name name of the pool
1670 * zc_cookie scan func (pool_scan_func_t)
1673 zfs_ioc_pool_scan(zfs_cmd_t
*zc
)
1678 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1681 if (zc
->zc_cookie
== POOL_SCAN_NONE
)
1682 error
= spa_scan_stop(spa
);
1684 error
= spa_scan(spa
, zc
->zc_cookie
);
1686 spa_close(spa
, FTAG
);
1692 zfs_ioc_pool_freeze(zfs_cmd_t
*zc
)
1697 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1700 spa_close(spa
, FTAG
);
1706 zfs_ioc_pool_upgrade(zfs_cmd_t
*zc
)
1711 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1714 if (zc
->zc_cookie
< spa_version(spa
) ||
1715 !SPA_VERSION_IS_SUPPORTED(zc
->zc_cookie
)) {
1716 spa_close(spa
, FTAG
);
1717 return (SET_ERROR(EINVAL
));
1720 spa_upgrade(spa
, zc
->zc_cookie
);
1721 spa_close(spa
, FTAG
);
1727 zfs_ioc_pool_get_history(zfs_cmd_t
*zc
)
1734 if ((size
= zc
->zc_history_len
) == 0)
1735 return (SET_ERROR(EINVAL
));
1737 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1740 if (spa_version(spa
) < SPA_VERSION_ZPOOL_HISTORY
) {
1741 spa_close(spa
, FTAG
);
1742 return (SET_ERROR(ENOTSUP
));
1745 hist_buf
= vmem_alloc(size
, KM_SLEEP
);
1746 if ((error
= spa_history_get(spa
, &zc
->zc_history_offset
,
1747 &zc
->zc_history_len
, hist_buf
)) == 0) {
1748 error
= ddi_copyout(hist_buf
,
1749 (void *)(uintptr_t)zc
->zc_history
,
1750 zc
->zc_history_len
, zc
->zc_iflags
);
1753 spa_close(spa
, FTAG
);
1754 vmem_free(hist_buf
, size
);
1759 zfs_ioc_pool_reguid(zfs_cmd_t
*zc
)
1764 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1766 error
= spa_change_guid(spa
);
1767 spa_close(spa
, FTAG
);
1773 zfs_ioc_dsobj_to_dsname(zfs_cmd_t
*zc
)
1775 return (dsl_dsobj_to_dsname(zc
->zc_name
, zc
->zc_obj
, zc
->zc_value
));
1780 * zc_name name of filesystem
1781 * zc_obj object to find
1784 * zc_value name of object
1787 zfs_ioc_obj_to_path(zfs_cmd_t
*zc
)
1792 /* XXX reading from objset not owned */
1793 if ((error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
)) != 0)
1795 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
1796 dmu_objset_rele(os
, FTAG
);
1797 return (SET_ERROR(EINVAL
));
1799 error
= zfs_obj_to_path(os
, zc
->zc_obj
, zc
->zc_value
,
1800 sizeof (zc
->zc_value
));
1801 dmu_objset_rele(os
, FTAG
);
1808 * zc_name name of filesystem
1809 * zc_obj object to find
1812 * zc_stat stats on object
1813 * zc_value path to object
1816 zfs_ioc_obj_to_stats(zfs_cmd_t
*zc
)
1821 /* XXX reading from objset not owned */
1822 if ((error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
)) != 0)
1824 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
1825 dmu_objset_rele(os
, FTAG
);
1826 return (SET_ERROR(EINVAL
));
1828 error
= zfs_obj_to_stats(os
, zc
->zc_obj
, &zc
->zc_stat
, zc
->zc_value
,
1829 sizeof (zc
->zc_value
));
1830 dmu_objset_rele(os
, FTAG
);
1836 zfs_ioc_vdev_add(zfs_cmd_t
*zc
)
1842 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1846 error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1847 zc
->zc_iflags
, &config
);
1849 error
= spa_vdev_add(spa
, config
);
1850 nvlist_free(config
);
1852 spa_close(spa
, FTAG
);
1858 * zc_name name of the pool
1859 * zc_nvlist_conf nvlist of devices to remove
1860 * zc_cookie to stop the remove?
1863 zfs_ioc_vdev_remove(zfs_cmd_t
*zc
)
1868 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1871 error
= spa_vdev_remove(spa
, zc
->zc_guid
, B_FALSE
);
1872 spa_close(spa
, FTAG
);
1877 zfs_ioc_vdev_set_state(zfs_cmd_t
*zc
)
1881 vdev_state_t newstate
= VDEV_STATE_UNKNOWN
;
1883 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1885 switch (zc
->zc_cookie
) {
1886 case VDEV_STATE_ONLINE
:
1887 error
= vdev_online(spa
, zc
->zc_guid
, zc
->zc_obj
, &newstate
);
1890 case VDEV_STATE_OFFLINE
:
1891 error
= vdev_offline(spa
, zc
->zc_guid
, zc
->zc_obj
);
1894 case VDEV_STATE_FAULTED
:
1895 if (zc
->zc_obj
!= VDEV_AUX_ERR_EXCEEDED
&&
1896 zc
->zc_obj
!= VDEV_AUX_EXTERNAL
)
1897 zc
->zc_obj
= VDEV_AUX_ERR_EXCEEDED
;
1899 error
= vdev_fault(spa
, zc
->zc_guid
, zc
->zc_obj
);
1902 case VDEV_STATE_DEGRADED
:
1903 if (zc
->zc_obj
!= VDEV_AUX_ERR_EXCEEDED
&&
1904 zc
->zc_obj
!= VDEV_AUX_EXTERNAL
)
1905 zc
->zc_obj
= VDEV_AUX_ERR_EXCEEDED
;
1907 error
= vdev_degrade(spa
, zc
->zc_guid
, zc
->zc_obj
);
1911 error
= SET_ERROR(EINVAL
);
1913 zc
->zc_cookie
= newstate
;
1914 spa_close(spa
, FTAG
);
1919 zfs_ioc_vdev_attach(zfs_cmd_t
*zc
)
1922 int replacing
= zc
->zc_cookie
;
1926 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1929 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1930 zc
->zc_iflags
, &config
)) == 0) {
1931 error
= spa_vdev_attach(spa
, zc
->zc_guid
, config
, replacing
);
1932 nvlist_free(config
);
1935 spa_close(spa
, FTAG
);
1940 zfs_ioc_vdev_detach(zfs_cmd_t
*zc
)
1945 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1948 error
= spa_vdev_detach(spa
, zc
->zc_guid
, 0, B_FALSE
);
1950 spa_close(spa
, FTAG
);
1955 zfs_ioc_vdev_split(zfs_cmd_t
*zc
)
1958 nvlist_t
*config
, *props
= NULL
;
1960 boolean_t exp
= !!(zc
->zc_cookie
& ZPOOL_EXPORT_AFTER_SPLIT
);
1962 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1965 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1966 zc
->zc_iflags
, &config
))) {
1967 spa_close(spa
, FTAG
);
1971 if (zc
->zc_nvlist_src_size
!= 0 && (error
=
1972 get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1973 zc
->zc_iflags
, &props
))) {
1974 spa_close(spa
, FTAG
);
1975 nvlist_free(config
);
1979 error
= spa_vdev_split_mirror(spa
, zc
->zc_string
, config
, props
, exp
);
1981 spa_close(spa
, FTAG
);
1983 nvlist_free(config
);
1990 zfs_ioc_vdev_setpath(zfs_cmd_t
*zc
)
1993 char *path
= zc
->zc_value
;
1994 uint64_t guid
= zc
->zc_guid
;
1997 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
2001 error
= spa_vdev_setpath(spa
, guid
, path
);
2002 spa_close(spa
, FTAG
);
2007 zfs_ioc_vdev_setfru(zfs_cmd_t
*zc
)
2010 char *fru
= zc
->zc_value
;
2011 uint64_t guid
= zc
->zc_guid
;
2014 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
2018 error
= spa_vdev_setfru(spa
, guid
, fru
);
2019 spa_close(spa
, FTAG
);
2024 zfs_ioc_objset_stats_impl(zfs_cmd_t
*zc
, objset_t
*os
)
2029 dmu_objset_fast_stat(os
, &zc
->zc_objset_stats
);
2031 if (zc
->zc_nvlist_dst
!= 0 &&
2032 (error
= dsl_prop_get_all(os
, &nv
)) == 0) {
2033 dmu_objset_stats(os
, nv
);
2035 * NB: zvol_get_stats() will read the objset contents,
2036 * which we aren't supposed to do with a
2037 * DS_MODE_USER hold, because it could be
2038 * inconsistent. So this is a bit of a workaround...
2039 * XXX reading with out owning
2041 if (!zc
->zc_objset_stats
.dds_inconsistent
&&
2042 dmu_objset_type(os
) == DMU_OST_ZVOL
) {
2043 error
= zvol_get_stats(os
, nv
);
2051 error
= put_nvlist(zc
, nv
);
2060 * zc_name name of filesystem
2061 * zc_nvlist_dst_size size of buffer for property nvlist
2064 * zc_objset_stats stats
2065 * zc_nvlist_dst property nvlist
2066 * zc_nvlist_dst_size size of property nvlist
2069 zfs_ioc_objset_stats(zfs_cmd_t
*zc
)
2074 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
2076 error
= zfs_ioc_objset_stats_impl(zc
, os
);
2077 dmu_objset_rele(os
, FTAG
);
2085 * zc_name name of filesystem
2086 * zc_nvlist_dst_size size of buffer for property nvlist
2089 * zc_nvlist_dst received property nvlist
2090 * zc_nvlist_dst_size size of received property nvlist
2092 * Gets received properties (distinct from local properties on or after
2093 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
2094 * local property values.
2097 zfs_ioc_objset_recvd_props(zfs_cmd_t
*zc
)
2103 * Without this check, we would return local property values if the
2104 * caller has not already received properties on or after
2105 * SPA_VERSION_RECVD_PROPS.
2107 if (!dsl_prop_get_hasrecvd(zc
->zc_name
))
2108 return (SET_ERROR(ENOTSUP
));
2110 if (zc
->zc_nvlist_dst
!= 0 &&
2111 (error
= dsl_prop_get_received(zc
->zc_name
, &nv
)) == 0) {
2112 error
= put_nvlist(zc
, nv
);
2120 nvl_add_zplprop(objset_t
*os
, nvlist_t
*props
, zfs_prop_t prop
)
2126 * zfs_get_zplprop() will either find a value or give us
2127 * the default value (if there is one).
2129 if ((error
= zfs_get_zplprop(os
, prop
, &value
)) != 0)
2131 VERIFY(nvlist_add_uint64(props
, zfs_prop_to_name(prop
), value
) == 0);
2137 * zc_name name of filesystem
2138 * zc_nvlist_dst_size size of buffer for zpl property nvlist
2141 * zc_nvlist_dst zpl property nvlist
2142 * zc_nvlist_dst_size size of zpl property nvlist
2145 zfs_ioc_objset_zplprops(zfs_cmd_t
*zc
)
2150 /* XXX reading without owning */
2151 if ((err
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
)))
2154 dmu_objset_fast_stat(os
, &zc
->zc_objset_stats
);
2157 * NB: nvl_add_zplprop() will read the objset contents,
2158 * which we aren't supposed to do with a DS_MODE_USER
2159 * hold, because it could be inconsistent.
2161 if (zc
->zc_nvlist_dst
!= 0 &&
2162 !zc
->zc_objset_stats
.dds_inconsistent
&&
2163 dmu_objset_type(os
) == DMU_OST_ZFS
) {
2166 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2167 if ((err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_VERSION
)) == 0 &&
2168 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_NORMALIZE
)) == 0 &&
2169 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_UTF8ONLY
)) == 0 &&
2170 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_CASE
)) == 0)
2171 err
= put_nvlist(zc
, nv
);
2174 err
= SET_ERROR(ENOENT
);
2176 dmu_objset_rele(os
, FTAG
);
2181 dataset_name_hidden(const char *name
)
2184 * Skip over datasets that are not visible in this zone,
2185 * internal datasets (which have a $ in their name), and
2186 * temporary datasets (which have a % in their name).
2188 if (strchr(name
, '$') != NULL
)
2190 if (strchr(name
, '%') != NULL
)
2192 if (!INGLOBALZONE(curproc
) && !zone_dataset_visible(name
, NULL
))
2199 * zc_name name of filesystem
2200 * zc_cookie zap cursor
2201 * zc_nvlist_dst_size size of buffer for property nvlist
2204 * zc_name name of next filesystem
2205 * zc_cookie zap cursor
2206 * zc_objset_stats stats
2207 * zc_nvlist_dst property nvlist
2208 * zc_nvlist_dst_size size of property nvlist
2211 zfs_ioc_dataset_list_next(zfs_cmd_t
*zc
)
2216 size_t orig_len
= strlen(zc
->zc_name
);
2219 if ((error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
))) {
2220 if (error
== ENOENT
)
2221 error
= SET_ERROR(ESRCH
);
2225 p
= strrchr(zc
->zc_name
, '/');
2226 if (p
== NULL
|| p
[1] != '\0')
2227 (void) strlcat(zc
->zc_name
, "/", sizeof (zc
->zc_name
));
2228 p
= zc
->zc_name
+ strlen(zc
->zc_name
);
2231 error
= dmu_dir_list_next(os
,
2232 sizeof (zc
->zc_name
) - (p
- zc
->zc_name
), p
,
2233 NULL
, &zc
->zc_cookie
);
2234 if (error
== ENOENT
)
2235 error
= SET_ERROR(ESRCH
);
2236 } while (error
== 0 && dataset_name_hidden(zc
->zc_name
));
2237 dmu_objset_rele(os
, FTAG
);
2240 * If it's an internal dataset (ie. with a '$' in its name),
2241 * don't try to get stats for it, otherwise we'll return ENOENT.
2243 if (error
== 0 && strchr(zc
->zc_name
, '$') == NULL
) {
2244 error
= zfs_ioc_objset_stats(zc
); /* fill in the stats */
2245 if (error
== ENOENT
) {
2246 /* We lost a race with destroy, get the next one. */
2247 zc
->zc_name
[orig_len
] = '\0';
2256 * zc_name name of filesystem
2257 * zc_cookie zap cursor
2258 * zc_nvlist_dst_size size of buffer for property nvlist
2261 * zc_name name of next snapshot
2262 * zc_objset_stats stats
2263 * zc_nvlist_dst property nvlist
2264 * zc_nvlist_dst_size size of property nvlist
2267 zfs_ioc_snapshot_list_next(zfs_cmd_t
*zc
)
2272 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
2274 return (error
== ENOENT
? ESRCH
: error
);
2278 * A dataset name of maximum length cannot have any snapshots,
2279 * so exit immediately.
2281 if (strlcat(zc
->zc_name
, "@", sizeof (zc
->zc_name
)) >=
2282 ZFS_MAX_DATASET_NAME_LEN
) {
2283 dmu_objset_rele(os
, FTAG
);
2284 return (SET_ERROR(ESRCH
));
2287 error
= dmu_snapshot_list_next(os
,
2288 sizeof (zc
->zc_name
) - strlen(zc
->zc_name
),
2289 zc
->zc_name
+ strlen(zc
->zc_name
), &zc
->zc_obj
, &zc
->zc_cookie
,
2292 if (error
== 0 && !zc
->zc_simple
) {
2294 dsl_pool_t
*dp
= os
->os_dsl_dataset
->ds_dir
->dd_pool
;
2296 error
= dsl_dataset_hold_obj(dp
, zc
->zc_obj
, FTAG
, &ds
);
2300 error
= dmu_objset_from_ds(ds
, &ossnap
);
2302 error
= zfs_ioc_objset_stats_impl(zc
, ossnap
);
2303 dsl_dataset_rele(ds
, FTAG
);
2305 } else if (error
== ENOENT
) {
2306 error
= SET_ERROR(ESRCH
);
2309 dmu_objset_rele(os
, FTAG
);
2310 /* if we failed, undo the @ that we tacked on to zc_name */
2312 *strchr(zc
->zc_name
, '@') = '\0';
2317 zfs_prop_set_userquota(const char *dsname
, nvpair_t
*pair
)
2319 const char *propname
= nvpair_name(pair
);
2321 unsigned int vallen
;
2324 zfs_userquota_prop_t type
;
2330 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2332 VERIFY(nvpair_value_nvlist(pair
, &attrs
) == 0);
2333 if (nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
2335 return (SET_ERROR(EINVAL
));
2339 * A correctly constructed propname is encoded as
2340 * userquota@<rid>-<domain>.
2342 if ((dash
= strchr(propname
, '-')) == NULL
||
2343 nvpair_value_uint64_array(pair
, &valary
, &vallen
) != 0 ||
2345 return (SET_ERROR(EINVAL
));
2352 err
= zfs_sb_hold(dsname
, FTAG
, &zsb
, B_FALSE
);
2354 err
= zfs_set_userquota(zsb
, type
, domain
, rid
, quota
);
2355 zfs_sb_rele(zsb
, FTAG
);
2362 * If the named property is one that has a special function to set its value,
2363 * return 0 on success and a positive error code on failure; otherwise if it is
2364 * not one of the special properties handled by this function, return -1.
2366 * XXX: It would be better for callers of the property interface if we handled
2367 * these special cases in dsl_prop.c (in the dsl layer).
2370 zfs_prop_set_special(const char *dsname
, zprop_source_t source
,
2373 const char *propname
= nvpair_name(pair
);
2374 zfs_prop_t prop
= zfs_name_to_prop(propname
);
2378 if (prop
== ZPROP_INVAL
) {
2379 if (zfs_prop_userquota(propname
))
2380 return (zfs_prop_set_userquota(dsname
, pair
));
2384 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2386 VERIFY(nvpair_value_nvlist(pair
, &attrs
) == 0);
2387 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
2391 if (zfs_prop_get_type(prop
) == PROP_TYPE_STRING
)
2394 VERIFY(0 == nvpair_value_uint64(pair
, &intval
));
2397 case ZFS_PROP_QUOTA
:
2398 err
= dsl_dir_set_quota(dsname
, source
, intval
);
2400 case ZFS_PROP_REFQUOTA
:
2401 err
= dsl_dataset_set_refquota(dsname
, source
, intval
);
2403 case ZFS_PROP_FILESYSTEM_LIMIT
:
2404 case ZFS_PROP_SNAPSHOT_LIMIT
:
2405 if (intval
== UINT64_MAX
) {
2406 /* clearing the limit, just do it */
2409 err
= dsl_dir_activate_fs_ss_limit(dsname
);
2412 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2413 * default path to set the value in the nvlist.
2418 case ZFS_PROP_RESERVATION
:
2419 err
= dsl_dir_set_reservation(dsname
, source
, intval
);
2421 case ZFS_PROP_REFRESERVATION
:
2422 err
= dsl_dataset_set_refreservation(dsname
, source
, intval
);
2424 case ZFS_PROP_VOLSIZE
:
2425 err
= zvol_set_volsize(dsname
, intval
);
2427 case ZFS_PROP_SNAPDEV
:
2428 err
= zvol_set_snapdev(dsname
, source
, intval
);
2430 case ZFS_PROP_VERSION
:
2434 if ((err
= zfs_sb_hold(dsname
, FTAG
, &zsb
, B_TRUE
)) != 0)
2437 err
= zfs_set_version(zsb
, intval
);
2438 zfs_sb_rele(zsb
, FTAG
);
2440 if (err
== 0 && intval
>= ZPL_VERSION_USERSPACE
) {
2443 zc
= kmem_zalloc(sizeof (zfs_cmd_t
), KM_SLEEP
);
2444 (void) strcpy(zc
->zc_name
, dsname
);
2445 (void) zfs_ioc_userspace_upgrade(zc
);
2446 (void) zfs_ioc_userobjspace_upgrade(zc
);
2447 kmem_free(zc
, sizeof (zfs_cmd_t
));
2459 * This function is best effort. If it fails to set any of the given properties,
2460 * it continues to set as many as it can and returns the last error
2461 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2462 * with the list of names of all the properties that failed along with the
2463 * corresponding error numbers.
2465 * If every property is set successfully, zero is returned and errlist is not
2469 zfs_set_prop_nvlist(const char *dsname
, zprop_source_t source
, nvlist_t
*nvl
,
2478 nvlist_t
*genericnvl
= fnvlist_alloc();
2479 nvlist_t
*retrynvl
= fnvlist_alloc();
2482 while ((pair
= nvlist_next_nvpair(nvl
, pair
)) != NULL
) {
2483 const char *propname
= nvpair_name(pair
);
2484 zfs_prop_t prop
= zfs_name_to_prop(propname
);
2487 /* decode the property value */
2489 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2491 attrs
= fnvpair_value_nvlist(pair
);
2492 if (nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
2494 err
= SET_ERROR(EINVAL
);
2497 /* Validate value type */
2498 if (err
== 0 && prop
== ZPROP_INVAL
) {
2499 if (zfs_prop_user(propname
)) {
2500 if (nvpair_type(propval
) != DATA_TYPE_STRING
)
2501 err
= SET_ERROR(EINVAL
);
2502 } else if (zfs_prop_userquota(propname
)) {
2503 if (nvpair_type(propval
) !=
2504 DATA_TYPE_UINT64_ARRAY
)
2505 err
= SET_ERROR(EINVAL
);
2507 err
= SET_ERROR(EINVAL
);
2509 } else if (err
== 0) {
2510 if (nvpair_type(propval
) == DATA_TYPE_STRING
) {
2511 if (zfs_prop_get_type(prop
) != PROP_TYPE_STRING
)
2512 err
= SET_ERROR(EINVAL
);
2513 } else if (nvpair_type(propval
) == DATA_TYPE_UINT64
) {
2516 intval
= fnvpair_value_uint64(propval
);
2518 switch (zfs_prop_get_type(prop
)) {
2519 case PROP_TYPE_NUMBER
:
2521 case PROP_TYPE_STRING
:
2522 err
= SET_ERROR(EINVAL
);
2524 case PROP_TYPE_INDEX
:
2525 if (zfs_prop_index_to_string(prop
,
2526 intval
, &unused
) != 0)
2527 err
= SET_ERROR(EINVAL
);
2531 "unknown property type");
2534 err
= SET_ERROR(EINVAL
);
2538 /* Validate permissions */
2540 err
= zfs_check_settable(dsname
, pair
, CRED());
2543 err
= zfs_prop_set_special(dsname
, source
, pair
);
2546 * For better performance we build up a list of
2547 * properties to set in a single transaction.
2549 err
= nvlist_add_nvpair(genericnvl
, pair
);
2550 } else if (err
!= 0 && nvl
!= retrynvl
) {
2552 * This may be a spurious error caused by
2553 * receiving quota and reservation out of order.
2554 * Try again in a second pass.
2556 err
= nvlist_add_nvpair(retrynvl
, pair
);
2561 if (errlist
!= NULL
)
2562 fnvlist_add_int32(errlist
, propname
, err
);
2567 if (nvl
!= retrynvl
&& !nvlist_empty(retrynvl
)) {
2572 if (!nvlist_empty(genericnvl
) &&
2573 dsl_props_set(dsname
, source
, genericnvl
) != 0) {
2575 * If this fails, we still want to set as many properties as we
2576 * can, so try setting them individually.
2579 while ((pair
= nvlist_next_nvpair(genericnvl
, pair
)) != NULL
) {
2580 const char *propname
= nvpair_name(pair
);
2584 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2586 attrs
= fnvpair_value_nvlist(pair
);
2587 propval
= fnvlist_lookup_nvpair(attrs
,
2591 if (nvpair_type(propval
) == DATA_TYPE_STRING
) {
2592 strval
= fnvpair_value_string(propval
);
2593 err
= dsl_prop_set_string(dsname
, propname
,
2596 intval
= fnvpair_value_uint64(propval
);
2597 err
= dsl_prop_set_int(dsname
, propname
, source
,
2602 if (errlist
!= NULL
) {
2603 fnvlist_add_int32(errlist
, propname
,
2610 nvlist_free(genericnvl
);
2611 nvlist_free(retrynvl
);
2617 * Check that all the properties are valid user properties.
2620 zfs_check_userprops(const char *fsname
, nvlist_t
*nvl
)
2622 nvpair_t
*pair
= NULL
;
2625 while ((pair
= nvlist_next_nvpair(nvl
, pair
)) != NULL
) {
2626 const char *propname
= nvpair_name(pair
);
2628 if (!zfs_prop_user(propname
) ||
2629 nvpair_type(pair
) != DATA_TYPE_STRING
)
2630 return (SET_ERROR(EINVAL
));
2632 if ((error
= zfs_secpolicy_write_perms(fsname
,
2633 ZFS_DELEG_PERM_USERPROP
, CRED())))
2636 if (strlen(propname
) >= ZAP_MAXNAMELEN
)
2637 return (SET_ERROR(ENAMETOOLONG
));
2639 if (strlen(fnvpair_value_string(pair
)) >= ZAP_MAXVALUELEN
)
2640 return (SET_ERROR(E2BIG
));
2646 props_skip(nvlist_t
*props
, nvlist_t
*skipped
, nvlist_t
**newprops
)
2650 VERIFY(nvlist_alloc(newprops
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2653 while ((pair
= nvlist_next_nvpair(props
, pair
)) != NULL
) {
2654 if (nvlist_exists(skipped
, nvpair_name(pair
)))
2657 VERIFY(nvlist_add_nvpair(*newprops
, pair
) == 0);
2662 clear_received_props(const char *dsname
, nvlist_t
*props
,
2666 nvlist_t
*cleared_props
= NULL
;
2667 props_skip(props
, skipped
, &cleared_props
);
2668 if (!nvlist_empty(cleared_props
)) {
2670 * Acts on local properties until the dataset has received
2671 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2673 zprop_source_t flags
= (ZPROP_SRC_NONE
|
2674 (dsl_prop_get_hasrecvd(dsname
) ? ZPROP_SRC_RECEIVED
: 0));
2675 err
= zfs_set_prop_nvlist(dsname
, flags
, cleared_props
, NULL
);
2677 nvlist_free(cleared_props
);
2683 * zc_name name of filesystem
2684 * zc_value name of property to set
2685 * zc_nvlist_src{_size} nvlist of properties to apply
2686 * zc_cookie received properties flag
2689 * zc_nvlist_dst{_size} error for each unapplied received property
2692 zfs_ioc_set_prop(zfs_cmd_t
*zc
)
2695 boolean_t received
= zc
->zc_cookie
;
2696 zprop_source_t source
= (received
? ZPROP_SRC_RECEIVED
:
2701 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2702 zc
->zc_iflags
, &nvl
)) != 0)
2706 nvlist_t
*origprops
;
2708 if (dsl_prop_get_received(zc
->zc_name
, &origprops
) == 0) {
2709 (void) clear_received_props(zc
->zc_name
,
2711 nvlist_free(origprops
);
2714 error
= dsl_prop_set_hasrecvd(zc
->zc_name
);
2717 errors
= fnvlist_alloc();
2719 error
= zfs_set_prop_nvlist(zc
->zc_name
, source
, nvl
, errors
);
2721 if (zc
->zc_nvlist_dst
!= 0 && errors
!= NULL
) {
2722 (void) put_nvlist(zc
, errors
);
2725 nvlist_free(errors
);
2732 * zc_name name of filesystem
2733 * zc_value name of property to inherit
2734 * zc_cookie revert to received value if TRUE
2739 zfs_ioc_inherit_prop(zfs_cmd_t
*zc
)
2741 const char *propname
= zc
->zc_value
;
2742 zfs_prop_t prop
= zfs_name_to_prop(propname
);
2743 boolean_t received
= zc
->zc_cookie
;
2744 zprop_source_t source
= (received
2745 ? ZPROP_SRC_NONE
/* revert to received value, if any */
2746 : ZPROP_SRC_INHERITED
); /* explicitly inherit */
2755 * zfs_prop_set_special() expects properties in the form of an
2756 * nvpair with type info.
2758 if (prop
== ZPROP_INVAL
) {
2759 if (!zfs_prop_user(propname
))
2760 return (SET_ERROR(EINVAL
));
2762 type
= PROP_TYPE_STRING
;
2763 } else if (prop
== ZFS_PROP_VOLSIZE
||
2764 prop
== ZFS_PROP_VERSION
) {
2765 return (SET_ERROR(EINVAL
));
2767 type
= zfs_prop_get_type(prop
);
2770 VERIFY(nvlist_alloc(&dummy
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2773 case PROP_TYPE_STRING
:
2774 VERIFY(0 == nvlist_add_string(dummy
, propname
, ""));
2776 case PROP_TYPE_NUMBER
:
2777 case PROP_TYPE_INDEX
:
2778 VERIFY(0 == nvlist_add_uint64(dummy
, propname
, 0));
2782 return (SET_ERROR(EINVAL
));
2785 pair
= nvlist_next_nvpair(dummy
, NULL
);
2788 return (SET_ERROR(EINVAL
));
2790 err
= zfs_prop_set_special(zc
->zc_name
, source
, pair
);
2793 return (err
); /* special property already handled */
2796 * Only check this in the non-received case. We want to allow
2797 * 'inherit -S' to revert non-inheritable properties like quota
2798 * and reservation to the received or default values even though
2799 * they are not considered inheritable.
2801 if (prop
!= ZPROP_INVAL
&& !zfs_prop_inheritable(prop
))
2802 return (SET_ERROR(EINVAL
));
2805 /* property name has been validated by zfs_secpolicy_inherit_prop() */
2806 return (dsl_prop_inherit(zc
->zc_name
, zc
->zc_value
, source
));
2810 zfs_ioc_pool_set_props(zfs_cmd_t
*zc
)
2817 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2818 zc
->zc_iflags
, &props
)))
2822 * If the only property is the configfile, then just do a spa_lookup()
2823 * to handle the faulted case.
2825 pair
= nvlist_next_nvpair(props
, NULL
);
2826 if (pair
!= NULL
&& strcmp(nvpair_name(pair
),
2827 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
)) == 0 &&
2828 nvlist_next_nvpair(props
, pair
) == NULL
) {
2829 mutex_enter(&spa_namespace_lock
);
2830 if ((spa
= spa_lookup(zc
->zc_name
)) != NULL
) {
2831 spa_configfile_set(spa
, props
, B_FALSE
);
2832 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
2834 mutex_exit(&spa_namespace_lock
);
2841 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0) {
2846 error
= spa_prop_set(spa
, props
);
2849 spa_close(spa
, FTAG
);
2855 zfs_ioc_pool_get_props(zfs_cmd_t
*zc
)
2859 nvlist_t
*nvp
= NULL
;
2861 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0) {
2863 * If the pool is faulted, there may be properties we can still
2864 * get (such as altroot and cachefile), so attempt to get them
2867 mutex_enter(&spa_namespace_lock
);
2868 if ((spa
= spa_lookup(zc
->zc_name
)) != NULL
)
2869 error
= spa_prop_get(spa
, &nvp
);
2870 mutex_exit(&spa_namespace_lock
);
2872 error
= spa_prop_get(spa
, &nvp
);
2873 spa_close(spa
, FTAG
);
2876 if (error
== 0 && zc
->zc_nvlist_dst
!= 0)
2877 error
= put_nvlist(zc
, nvp
);
2879 error
= SET_ERROR(EFAULT
);
2887 * zc_name name of filesystem
2888 * zc_nvlist_src{_size} nvlist of delegated permissions
2889 * zc_perm_action allow/unallow flag
2894 zfs_ioc_set_fsacl(zfs_cmd_t
*zc
)
2897 nvlist_t
*fsaclnv
= NULL
;
2899 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2900 zc
->zc_iflags
, &fsaclnv
)) != 0)
2904 * Verify nvlist is constructed correctly
2906 if ((error
= zfs_deleg_verify_nvlist(fsaclnv
)) != 0) {
2907 nvlist_free(fsaclnv
);
2908 return (SET_ERROR(EINVAL
));
2912 * If we don't have PRIV_SYS_MOUNT, then validate
2913 * that user is allowed to hand out each permission in
2917 error
= secpolicy_zfs(CRED());
2919 if (zc
->zc_perm_action
== B_FALSE
) {
2920 error
= dsl_deleg_can_allow(zc
->zc_name
,
2923 error
= dsl_deleg_can_unallow(zc
->zc_name
,
2929 error
= dsl_deleg_set(zc
->zc_name
, fsaclnv
, zc
->zc_perm_action
);
2931 nvlist_free(fsaclnv
);
2937 * zc_name name of filesystem
2940 * zc_nvlist_src{_size} nvlist of delegated permissions
2943 zfs_ioc_get_fsacl(zfs_cmd_t
*zc
)
2948 if ((error
= dsl_deleg_get(zc
->zc_name
, &nvp
)) == 0) {
2949 error
= put_nvlist(zc
, nvp
);
2958 zfs_create_cb(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
)
2960 zfs_creat_t
*zct
= arg
;
2962 zfs_create_fs(os
, cr
, zct
->zct_zplprops
, tx
);
2965 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
2969 * os parent objset pointer (NULL if root fs)
2970 * fuids_ok fuids allowed in this version of the spa?
2971 * sa_ok SAs allowed in this version of the spa?
2972 * createprops list of properties requested by creator
2975 * zplprops values for the zplprops we attach to the master node object
2976 * is_ci true if requested file system will be purely case-insensitive
2978 * Determine the settings for utf8only, normalization and
2979 * casesensitivity. Specific values may have been requested by the
2980 * creator and/or we can inherit values from the parent dataset. If
2981 * the file system is of too early a vintage, a creator can not
2982 * request settings for these properties, even if the requested
2983 * setting is the default value. We don't actually want to create dsl
2984 * properties for these, so remove them from the source nvlist after
2988 zfs_fill_zplprops_impl(objset_t
*os
, uint64_t zplver
,
2989 boolean_t fuids_ok
, boolean_t sa_ok
, nvlist_t
*createprops
,
2990 nvlist_t
*zplprops
, boolean_t
*is_ci
)
2992 uint64_t sense
= ZFS_PROP_UNDEFINED
;
2993 uint64_t norm
= ZFS_PROP_UNDEFINED
;
2994 uint64_t u8
= ZFS_PROP_UNDEFINED
;
2997 ASSERT(zplprops
!= NULL
);
3000 * Pull out creator prop choices, if any.
3003 (void) nvlist_lookup_uint64(createprops
,
3004 zfs_prop_to_name(ZFS_PROP_VERSION
), &zplver
);
3005 (void) nvlist_lookup_uint64(createprops
,
3006 zfs_prop_to_name(ZFS_PROP_NORMALIZE
), &norm
);
3007 (void) nvlist_remove_all(createprops
,
3008 zfs_prop_to_name(ZFS_PROP_NORMALIZE
));
3009 (void) nvlist_lookup_uint64(createprops
,
3010 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), &u8
);
3011 (void) nvlist_remove_all(createprops
,
3012 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
));
3013 (void) nvlist_lookup_uint64(createprops
,
3014 zfs_prop_to_name(ZFS_PROP_CASE
), &sense
);
3015 (void) nvlist_remove_all(createprops
,
3016 zfs_prop_to_name(ZFS_PROP_CASE
));
3020 * If the zpl version requested is whacky or the file system
3021 * or pool is version is too "young" to support normalization
3022 * and the creator tried to set a value for one of the props,
3025 if ((zplver
< ZPL_VERSION_INITIAL
|| zplver
> ZPL_VERSION
) ||
3026 (zplver
>= ZPL_VERSION_FUID
&& !fuids_ok
) ||
3027 (zplver
>= ZPL_VERSION_SA
&& !sa_ok
) ||
3028 (zplver
< ZPL_VERSION_NORMALIZATION
&&
3029 (norm
!= ZFS_PROP_UNDEFINED
|| u8
!= ZFS_PROP_UNDEFINED
||
3030 sense
!= ZFS_PROP_UNDEFINED
)))
3031 return (SET_ERROR(ENOTSUP
));
3034 * Put the version in the zplprops
3036 VERIFY(nvlist_add_uint64(zplprops
,
3037 zfs_prop_to_name(ZFS_PROP_VERSION
), zplver
) == 0);
3039 if (norm
== ZFS_PROP_UNDEFINED
&&
3040 (error
= zfs_get_zplprop(os
, ZFS_PROP_NORMALIZE
, &norm
)) != 0)
3042 VERIFY(nvlist_add_uint64(zplprops
,
3043 zfs_prop_to_name(ZFS_PROP_NORMALIZE
), norm
) == 0);
3046 * If we're normalizing, names must always be valid UTF-8 strings.
3050 if (u8
== ZFS_PROP_UNDEFINED
&&
3051 (error
= zfs_get_zplprop(os
, ZFS_PROP_UTF8ONLY
, &u8
)) != 0)
3053 VERIFY(nvlist_add_uint64(zplprops
,
3054 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), u8
) == 0);
3056 if (sense
== ZFS_PROP_UNDEFINED
&&
3057 (error
= zfs_get_zplprop(os
, ZFS_PROP_CASE
, &sense
)) != 0)
3059 VERIFY(nvlist_add_uint64(zplprops
,
3060 zfs_prop_to_name(ZFS_PROP_CASE
), sense
) == 0);
3063 *is_ci
= (sense
== ZFS_CASE_INSENSITIVE
);
3069 zfs_fill_zplprops(const char *dataset
, nvlist_t
*createprops
,
3070 nvlist_t
*zplprops
, boolean_t
*is_ci
)
3072 boolean_t fuids_ok
, sa_ok
;
3073 uint64_t zplver
= ZPL_VERSION
;
3074 objset_t
*os
= NULL
;
3075 char parentname
[ZFS_MAX_DATASET_NAME_LEN
];
3081 (void) strlcpy(parentname
, dataset
, sizeof (parentname
));
3082 cp
= strrchr(parentname
, '/');
3086 if ((error
= spa_open(dataset
, &spa
, FTAG
)) != 0)
3089 spa_vers
= spa_version(spa
);
3090 spa_close(spa
, FTAG
);
3092 zplver
= zfs_zpl_version_map(spa_vers
);
3093 fuids_ok
= (zplver
>= ZPL_VERSION_FUID
);
3094 sa_ok
= (zplver
>= ZPL_VERSION_SA
);
3097 * Open parent object set so we can inherit zplprop values.
3099 if ((error
= dmu_objset_hold(parentname
, FTAG
, &os
)) != 0)
3102 error
= zfs_fill_zplprops_impl(os
, zplver
, fuids_ok
, sa_ok
, createprops
,
3104 dmu_objset_rele(os
, FTAG
);
3109 zfs_fill_zplprops_root(uint64_t spa_vers
, nvlist_t
*createprops
,
3110 nvlist_t
*zplprops
, boolean_t
*is_ci
)
3114 uint64_t zplver
= ZPL_VERSION
;
3117 zplver
= zfs_zpl_version_map(spa_vers
);
3118 fuids_ok
= (zplver
>= ZPL_VERSION_FUID
);
3119 sa_ok
= (zplver
>= ZPL_VERSION_SA
);
3121 error
= zfs_fill_zplprops_impl(NULL
, zplver
, fuids_ok
, sa_ok
,
3122 createprops
, zplprops
, is_ci
);
3128 * "type" -> dmu_objset_type_t (int32)
3129 * (optional) "props" -> { prop -> value }
3132 * outnvl: propname -> error code (int32)
3135 zfs_ioc_create(const char *fsname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3138 zfs_creat_t zct
= { 0 };
3139 nvlist_t
*nvprops
= NULL
;
3140 void (*cbfunc
)(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
);
3142 dmu_objset_type_t type
;
3143 boolean_t is_insensitive
= B_FALSE
;
3145 if (nvlist_lookup_int32(innvl
, "type", &type32
) != 0)
3146 return (SET_ERROR(EINVAL
));
3148 (void) nvlist_lookup_nvlist(innvl
, "props", &nvprops
);
3152 cbfunc
= zfs_create_cb
;
3156 cbfunc
= zvol_create_cb
;
3163 if (strchr(fsname
, '@') ||
3164 strchr(fsname
, '%'))
3165 return (SET_ERROR(EINVAL
));
3167 zct
.zct_props
= nvprops
;
3170 return (SET_ERROR(EINVAL
));
3172 if (type
== DMU_OST_ZVOL
) {
3173 uint64_t volsize
, volblocksize
;
3175 if (nvprops
== NULL
)
3176 return (SET_ERROR(EINVAL
));
3177 if (nvlist_lookup_uint64(nvprops
,
3178 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &volsize
) != 0)
3179 return (SET_ERROR(EINVAL
));
3181 if ((error
= nvlist_lookup_uint64(nvprops
,
3182 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
3183 &volblocksize
)) != 0 && error
!= ENOENT
)
3184 return (SET_ERROR(EINVAL
));
3187 volblocksize
= zfs_prop_default_numeric(
3188 ZFS_PROP_VOLBLOCKSIZE
);
3190 if ((error
= zvol_check_volblocksize(fsname
,
3191 volblocksize
)) != 0 ||
3192 (error
= zvol_check_volsize(volsize
,
3193 volblocksize
)) != 0)
3195 } else if (type
== DMU_OST_ZFS
) {
3199 * We have to have normalization and
3200 * case-folding flags correct when we do the
3201 * file system creation, so go figure them out
3204 VERIFY(nvlist_alloc(&zct
.zct_zplprops
,
3205 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3206 error
= zfs_fill_zplprops(fsname
, nvprops
,
3207 zct
.zct_zplprops
, &is_insensitive
);
3209 nvlist_free(zct
.zct_zplprops
);
3214 error
= dmu_objset_create(fsname
, type
,
3215 is_insensitive
? DS_FLAG_CI_DATASET
: 0, cbfunc
, &zct
);
3216 nvlist_free(zct
.zct_zplprops
);
3219 * It would be nice to do this atomically.
3222 error
= zfs_set_prop_nvlist(fsname
, ZPROP_SRC_LOCAL
,
3229 * Volumes will return EBUSY and cannot be destroyed
3230 * until all asynchronous minor handling has completed.
3231 * Wait for the spa_zvol_taskq to drain then retry.
3233 error2
= dsl_destroy_head(fsname
);
3234 while ((error2
== EBUSY
) && (type
== DMU_OST_ZVOL
)) {
3235 error2
= spa_open(fsname
, &spa
, FTAG
);
3237 taskq_wait(spa
->spa_zvol_taskq
);
3238 spa_close(spa
, FTAG
);
3240 error2
= dsl_destroy_head(fsname
);
3249 * "origin" -> name of origin snapshot
3250 * (optional) "props" -> { prop -> value }
3254 * outnvl: propname -> error code (int32)
3257 zfs_ioc_clone(const char *fsname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3260 nvlist_t
*nvprops
= NULL
;
3263 if (nvlist_lookup_string(innvl
, "origin", &origin_name
) != 0)
3264 return (SET_ERROR(EINVAL
));
3265 (void) nvlist_lookup_nvlist(innvl
, "props", &nvprops
);
3267 if (strchr(fsname
, '@') ||
3268 strchr(fsname
, '%'))
3269 return (SET_ERROR(EINVAL
));
3271 if (dataset_namecheck(origin_name
, NULL
, NULL
) != 0)
3272 return (SET_ERROR(EINVAL
));
3273 error
= dmu_objset_clone(fsname
, origin_name
);
3278 * It would be nice to do this atomically.
3281 error
= zfs_set_prop_nvlist(fsname
, ZPROP_SRC_LOCAL
,
3284 (void) dsl_destroy_head(fsname
);
3291 * "snaps" -> { snapshot1, snapshot2 }
3292 * (optional) "props" -> { prop -> value (string) }
3295 * outnvl: snapshot -> error code (int32)
3298 zfs_ioc_snapshot(const char *poolname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3301 nvlist_t
*props
= NULL
;
3303 nvpair_t
*pair
, *pair2
;
3305 (void) nvlist_lookup_nvlist(innvl
, "props", &props
);
3306 if ((error
= zfs_check_userprops(poolname
, props
)) != 0)
3309 if (!nvlist_empty(props
) &&
3310 zfs_earlier_version(poolname
, SPA_VERSION_SNAP_PROPS
))
3311 return (SET_ERROR(ENOTSUP
));
3313 if (nvlist_lookup_nvlist(innvl
, "snaps", &snaps
) != 0)
3314 return (SET_ERROR(EINVAL
));
3315 poollen
= strlen(poolname
);
3316 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
3317 pair
= nvlist_next_nvpair(snaps
, pair
)) {
3318 const char *name
= nvpair_name(pair
);
3319 const char *cp
= strchr(name
, '@');
3322 * The snap name must contain an @, and the part after it must
3323 * contain only valid characters.
3326 zfs_component_namecheck(cp
+ 1, NULL
, NULL
) != 0)
3327 return (SET_ERROR(EINVAL
));
3330 * The snap must be in the specified pool.
3332 if (strncmp(name
, poolname
, poollen
) != 0 ||
3333 (name
[poollen
] != '/' && name
[poollen
] != '@'))
3334 return (SET_ERROR(EXDEV
));
3336 /* This must be the only snap of this fs. */
3337 for (pair2
= nvlist_next_nvpair(snaps
, pair
);
3338 pair2
!= NULL
; pair2
= nvlist_next_nvpair(snaps
, pair2
)) {
3339 if (strncmp(name
, nvpair_name(pair2
), cp
- name
+ 1)
3341 return (SET_ERROR(EXDEV
));
3346 error
= dsl_dataset_snapshot(snaps
, props
, outnvl
);
3352 * innvl: "message" -> string
3356 zfs_ioc_log_history(const char *unused
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3364 * The poolname in the ioctl is not set, we get it from the TSD,
3365 * which was set at the end of the last successful ioctl that allows
3366 * logging. The secpolicy func already checked that it is set.
3367 * Only one log ioctl is allowed after each successful ioctl, so
3368 * we clear the TSD here.
3370 poolname
= tsd_get(zfs_allow_log_key
);
3371 if (poolname
== NULL
)
3372 return (SET_ERROR(EINVAL
));
3373 (void) tsd_set(zfs_allow_log_key
, NULL
);
3374 error
= spa_open(poolname
, &spa
, FTAG
);
3379 if (nvlist_lookup_string(innvl
, "message", &message
) != 0) {
3380 spa_close(spa
, FTAG
);
3381 return (SET_ERROR(EINVAL
));
3384 if (spa_version(spa
) < SPA_VERSION_ZPOOL_HISTORY
) {
3385 spa_close(spa
, FTAG
);
3386 return (SET_ERROR(ENOTSUP
));
3389 error
= spa_history_log(spa
, message
);
3390 spa_close(spa
, FTAG
);
3395 * The dp_config_rwlock must not be held when calling this, because the
3396 * unmount may need to write out data.
3398 * This function is best-effort. Callers must deal gracefully if it
3399 * remains mounted (or is remounted after this call).
3401 * Returns 0 if the argument is not a snapshot, or it is not currently a
3402 * filesystem, or we were able to unmount it. Returns error code otherwise.
3405 zfs_unmount_snap(const char *snapname
)
3409 if (strchr(snapname
, '@') == NULL
)
3412 err
= zfsctl_snapshot_unmount((char *)snapname
, MNT_FORCE
);
3413 if (err
!= 0 && err
!= ENOENT
)
3414 return (SET_ERROR(err
));
3421 zfs_unmount_snap_cb(const char *snapname
, void *arg
)
3423 return (zfs_unmount_snap(snapname
));
3427 * When a clone is destroyed, its origin may also need to be destroyed,
3428 * in which case it must be unmounted. This routine will do that unmount
3432 zfs_destroy_unmount_origin(const char *fsname
)
3438 error
= dmu_objset_hold(fsname
, FTAG
, &os
);
3441 ds
= dmu_objset_ds(os
);
3442 if (dsl_dir_is_clone(ds
->ds_dir
) && DS_IS_DEFER_DESTROY(ds
->ds_prev
)) {
3443 char originname
[ZFS_MAX_DATASET_NAME_LEN
];
3444 dsl_dataset_name(ds
->ds_prev
, originname
);
3445 dmu_objset_rele(os
, FTAG
);
3446 (void) zfs_unmount_snap(originname
);
3448 dmu_objset_rele(os
, FTAG
);
3454 * "snaps" -> { snapshot1, snapshot2 }
3455 * (optional boolean) "defer"
3458 * outnvl: snapshot -> error code (int32)
3462 zfs_ioc_destroy_snaps(const char *poolname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3468 if (nvlist_lookup_nvlist(innvl
, "snaps", &snaps
) != 0)
3469 return (SET_ERROR(EINVAL
));
3470 defer
= nvlist_exists(innvl
, "defer");
3472 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
3473 pair
= nvlist_next_nvpair(snaps
, pair
)) {
3474 (void) zfs_unmount_snap(nvpair_name(pair
));
3477 return (dsl_destroy_snapshots_nvl(snaps
, defer
, outnvl
));
3481 * Create bookmarks. Bookmark names are of the form <fs>#<bmark>.
3482 * All bookmarks must be in the same pool.
3485 * bookmark1 -> snapshot1, bookmark2 -> snapshot2
3488 * outnvl: bookmark -> error code (int32)
3493 zfs_ioc_bookmark(const char *poolname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3495 nvpair_t
*pair
, *pair2
;
3497 for (pair
= nvlist_next_nvpair(innvl
, NULL
);
3498 pair
!= NULL
; pair
= nvlist_next_nvpair(innvl
, pair
)) {
3502 * Verify the snapshot argument.
3504 if (nvpair_value_string(pair
, &snap_name
) != 0)
3505 return (SET_ERROR(EINVAL
));
3508 /* Verify that the keys (bookmarks) are unique */
3509 for (pair2
= nvlist_next_nvpair(innvl
, pair
);
3510 pair2
!= NULL
; pair2
= nvlist_next_nvpair(innvl
, pair2
)) {
3511 if (strcmp(nvpair_name(pair
), nvpair_name(pair2
)) == 0)
3512 return (SET_ERROR(EINVAL
));
3516 return (dsl_bookmark_create(innvl
, outnvl
));
3521 * property 1, property 2, ...
3525 * bookmark name 1 -> { property 1, property 2, ... },
3526 * bookmark name 2 -> { property 1, property 2, ... }
3531 zfs_ioc_get_bookmarks(const char *fsname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3533 return (dsl_get_bookmarks(fsname
, innvl
, outnvl
));
3538 * bookmark name 1, bookmark name 2
3541 * outnvl: bookmark -> error code (int32)
3545 zfs_ioc_destroy_bookmarks(const char *poolname
, nvlist_t
*innvl
,
3551 poollen
= strlen(poolname
);
3552 for (pair
= nvlist_next_nvpair(innvl
, NULL
);
3553 pair
!= NULL
; pair
= nvlist_next_nvpair(innvl
, pair
)) {
3554 const char *name
= nvpair_name(pair
);
3555 const char *cp
= strchr(name
, '#');
3558 * The bookmark name must contain an #, and the part after it
3559 * must contain only valid characters.
3562 zfs_component_namecheck(cp
+ 1, NULL
, NULL
) != 0)
3563 return (SET_ERROR(EINVAL
));
3566 * The bookmark must be in the specified pool.
3568 if (strncmp(name
, poolname
, poollen
) != 0 ||
3569 (name
[poollen
] != '/' && name
[poollen
] != '#'))
3570 return (SET_ERROR(EXDEV
));
3573 error
= dsl_bookmark_destroy(innvl
, outnvl
);
3579 * zc_name name of dataset to destroy
3580 * zc_objset_type type of objset
3581 * zc_defer_destroy mark for deferred destroy
3586 zfs_ioc_destroy(zfs_cmd_t
*zc
)
3590 if (zc
->zc_objset_type
== DMU_OST_ZFS
) {
3591 err
= zfs_unmount_snap(zc
->zc_name
);
3596 if (strchr(zc
->zc_name
, '@')) {
3597 err
= dsl_destroy_snapshot(zc
->zc_name
, zc
->zc_defer_destroy
);
3599 err
= dsl_destroy_head(zc
->zc_name
);
3600 if (err
== EEXIST
) {
3602 * It is possible that the given DS may have
3603 * hidden child (%recv) datasets - "leftovers"
3604 * resulting from the previously interrupted
3607 * 6 extra bytes for /%recv
3609 char namebuf
[ZFS_MAX_DATASET_NAME_LEN
+ 6];
3611 (void) snprintf(namebuf
, sizeof (namebuf
),
3612 "%s/%s", zc
->zc_name
, recv_clone_name
);
3615 * Try to remove the hidden child (%recv) and after
3616 * that try to remove the target dataset.
3617 * If the hidden child (%recv) does not exist
3618 * the original error (EEXIST) will be returned
3620 err
= dsl_destroy_head(namebuf
);
3622 err
= dsl_destroy_head(zc
->zc_name
);
3623 else if (err
== ENOENT
)
3632 * fsname is name of dataset to rollback (to most recent snapshot)
3634 * innvl is not used.
3636 * outnvl: "target" -> name of most recent snapshot
3641 zfs_ioc_rollback(const char *fsname
, nvlist_t
*args
, nvlist_t
*outnvl
)
3647 if (get_zfs_sb(fsname
, &zsb
) == 0) {
3650 ds
= dmu_objset_ds(zsb
->z_os
);
3651 error
= zfs_suspend_fs(zsb
);
3655 error
= dsl_dataset_rollback(fsname
, zsb
, outnvl
);
3656 resume_err
= zfs_resume_fs(zsb
, ds
);
3657 error
= error
? error
: resume_err
;
3659 deactivate_super(zsb
->z_sb
);
3660 } else if ((zv
= zvol_suspend(fsname
)) != NULL
) {
3661 error
= dsl_dataset_rollback(fsname
, zvol_tag(zv
), outnvl
);
3664 error
= dsl_dataset_rollback(fsname
, NULL
, outnvl
);
3670 recursive_unmount(const char *fsname
, void *arg
)
3672 const char *snapname
= arg
;
3676 fullname
= kmem_asprintf("%s@%s", fsname
, snapname
);
3677 error
= zfs_unmount_snap(fullname
);
3685 * zc_name old name of dataset
3686 * zc_value new name of dataset
3687 * zc_cookie recursive flag (only valid for snapshots)
3692 zfs_ioc_rename(zfs_cmd_t
*zc
)
3694 boolean_t recursive
= zc
->zc_cookie
& 1;
3697 zc
->zc_value
[sizeof (zc
->zc_value
) - 1] = '\0';
3698 if (dataset_namecheck(zc
->zc_value
, NULL
, NULL
) != 0 ||
3699 strchr(zc
->zc_value
, '%'))
3700 return (SET_ERROR(EINVAL
));
3702 at
= strchr(zc
->zc_name
, '@');
3704 /* snaps must be in same fs */
3707 if (strncmp(zc
->zc_name
, zc
->zc_value
, at
- zc
->zc_name
+ 1))
3708 return (SET_ERROR(EXDEV
));
3710 if (zc
->zc_objset_type
== DMU_OST_ZFS
) {
3711 error
= dmu_objset_find(zc
->zc_name
,
3712 recursive_unmount
, at
+ 1,
3713 recursive
? DS_FIND_CHILDREN
: 0);
3719 error
= dsl_dataset_rename_snapshot(zc
->zc_name
,
3720 at
+ 1, strchr(zc
->zc_value
, '@') + 1, recursive
);
3725 return (dsl_dir_rename(zc
->zc_name
, zc
->zc_value
));
3730 zfs_check_settable(const char *dsname
, nvpair_t
*pair
, cred_t
*cr
)
3732 const char *propname
= nvpair_name(pair
);
3733 boolean_t issnap
= (strchr(dsname
, '@') != NULL
);
3734 zfs_prop_t prop
= zfs_name_to_prop(propname
);
3738 if (prop
== ZPROP_INVAL
) {
3739 if (zfs_prop_user(propname
)) {
3740 if ((err
= zfs_secpolicy_write_perms(dsname
,
3741 ZFS_DELEG_PERM_USERPROP
, cr
)))
3746 if (!issnap
&& zfs_prop_userquota(propname
)) {
3747 const char *perm
= NULL
;
3748 const char *uq_prefix
=
3749 zfs_userquota_prop_prefixes
[ZFS_PROP_USERQUOTA
];
3750 const char *gq_prefix
=
3751 zfs_userquota_prop_prefixes
[ZFS_PROP_GROUPQUOTA
];
3752 const char *uiq_prefix
=
3753 zfs_userquota_prop_prefixes
[ZFS_PROP_USEROBJQUOTA
];
3754 const char *giq_prefix
=
3755 zfs_userquota_prop_prefixes
[ZFS_PROP_GROUPOBJQUOTA
];
3757 if (strncmp(propname
, uq_prefix
,
3758 strlen(uq_prefix
)) == 0) {
3759 perm
= ZFS_DELEG_PERM_USERQUOTA
;
3760 } else if (strncmp(propname
, uiq_prefix
,
3761 strlen(uiq_prefix
)) == 0) {
3762 perm
= ZFS_DELEG_PERM_USEROBJQUOTA
;
3763 } else if (strncmp(propname
, gq_prefix
,
3764 strlen(gq_prefix
)) == 0) {
3765 perm
= ZFS_DELEG_PERM_GROUPQUOTA
;
3766 } else if (strncmp(propname
, giq_prefix
,
3767 strlen(giq_prefix
)) == 0) {
3768 perm
= ZFS_DELEG_PERM_GROUPOBJQUOTA
;
3770 /* USERUSED and GROUPUSED are read-only */
3771 return (SET_ERROR(EINVAL
));
3774 if ((err
= zfs_secpolicy_write_perms(dsname
, perm
, cr
)))
3779 return (SET_ERROR(EINVAL
));
3783 return (SET_ERROR(EINVAL
));
3785 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
3787 * dsl_prop_get_all_impl() returns properties in this
3791 VERIFY(nvpair_value_nvlist(pair
, &attrs
) == 0);
3792 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
3797 * Check that this value is valid for this pool version
3800 case ZFS_PROP_COMPRESSION
:
3802 * If the user specified gzip compression, make sure
3803 * the SPA supports it. We ignore any errors here since
3804 * we'll catch them later.
3806 if (nvpair_value_uint64(pair
, &intval
) == 0) {
3807 if (intval
>= ZIO_COMPRESS_GZIP_1
&&
3808 intval
<= ZIO_COMPRESS_GZIP_9
&&
3809 zfs_earlier_version(dsname
,
3810 SPA_VERSION_GZIP_COMPRESSION
)) {
3811 return (SET_ERROR(ENOTSUP
));
3814 if (intval
== ZIO_COMPRESS_ZLE
&&
3815 zfs_earlier_version(dsname
,
3816 SPA_VERSION_ZLE_COMPRESSION
))
3817 return (SET_ERROR(ENOTSUP
));
3819 if (intval
== ZIO_COMPRESS_LZ4
) {
3822 if ((err
= spa_open(dsname
, &spa
, FTAG
)) != 0)
3825 if (!spa_feature_is_enabled(spa
,
3826 SPA_FEATURE_LZ4_COMPRESS
)) {
3827 spa_close(spa
, FTAG
);
3828 return (SET_ERROR(ENOTSUP
));
3830 spa_close(spa
, FTAG
);
3834 * If this is a bootable dataset then
3835 * verify that the compression algorithm
3836 * is supported for booting. We must return
3837 * something other than ENOTSUP since it
3838 * implies a downrev pool version.
3840 if (zfs_is_bootfs(dsname
) &&
3841 !BOOTFS_COMPRESS_VALID(intval
)) {
3842 return (SET_ERROR(ERANGE
));
3847 case ZFS_PROP_COPIES
:
3848 if (zfs_earlier_version(dsname
, SPA_VERSION_DITTO_BLOCKS
))
3849 return (SET_ERROR(ENOTSUP
));
3852 case ZFS_PROP_VOLBLOCKSIZE
:
3853 case ZFS_PROP_RECORDSIZE
:
3854 /* Record sizes above 128k need the feature to be enabled */
3855 if (nvpair_value_uint64(pair
, &intval
) == 0 &&
3856 intval
> SPA_OLD_MAXBLOCKSIZE
) {
3860 * If this is a bootable dataset then
3861 * we don't allow large (>128K) blocks,
3862 * because GRUB doesn't support them.
3864 if (zfs_is_bootfs(dsname
) &&
3865 intval
> SPA_OLD_MAXBLOCKSIZE
) {
3866 return (SET_ERROR(ERANGE
));
3870 * We don't allow setting the property above 1MB,
3871 * unless the tunable has been changed.
3873 if (intval
> zfs_max_recordsize
||
3874 intval
> SPA_MAXBLOCKSIZE
)
3875 return (SET_ERROR(ERANGE
));
3877 if ((err
= spa_open(dsname
, &spa
, FTAG
)) != 0)
3880 if (!spa_feature_is_enabled(spa
,
3881 SPA_FEATURE_LARGE_BLOCKS
)) {
3882 spa_close(spa
, FTAG
);
3883 return (SET_ERROR(ENOTSUP
));
3885 spa_close(spa
, FTAG
);
3889 case ZFS_PROP_DNODESIZE
:
3890 /* Dnode sizes above 512 need the feature to be enabled */
3891 if (nvpair_value_uint64(pair
, &intval
) == 0 &&
3892 intval
!= ZFS_DNSIZE_LEGACY
) {
3896 * If this is a bootable dataset then
3897 * we don't allow large (>512B) dnodes,
3898 * because GRUB doesn't support them.
3900 if (zfs_is_bootfs(dsname
) &&
3901 intval
!= ZFS_DNSIZE_LEGACY
) {
3902 return (SET_ERROR(EDOM
));
3905 if ((err
= spa_open(dsname
, &spa
, FTAG
)) != 0)
3908 if (!spa_feature_is_enabled(spa
,
3909 SPA_FEATURE_LARGE_DNODE
)) {
3910 spa_close(spa
, FTAG
);
3911 return (SET_ERROR(ENOTSUP
));
3913 spa_close(spa
, FTAG
);
3917 case ZFS_PROP_SHARESMB
:
3918 if (zpl_earlier_version(dsname
, ZPL_VERSION_FUID
))
3919 return (SET_ERROR(ENOTSUP
));
3922 case ZFS_PROP_ACLINHERIT
:
3923 if (nvpair_type(pair
) == DATA_TYPE_UINT64
&&
3924 nvpair_value_uint64(pair
, &intval
) == 0) {
3925 if (intval
== ZFS_ACL_PASSTHROUGH_X
&&
3926 zfs_earlier_version(dsname
,
3927 SPA_VERSION_PASSTHROUGH_X
))
3928 return (SET_ERROR(ENOTSUP
));
3931 case ZFS_PROP_CHECKSUM
:
3932 case ZFS_PROP_DEDUP
:
3934 spa_feature_t feature
;
3939 /* dedup feature version checks */
3940 if (prop
== ZFS_PROP_DEDUP
&&
3941 zfs_earlier_version(dsname
, SPA_VERSION_DEDUP
))
3942 return (SET_ERROR(ENOTSUP
));
3944 if (nvpair_value_uint64(pair
, &intval
) != 0)
3945 return (SET_ERROR(EINVAL
));
3947 /* check prop value is enabled in features */
3948 feature
= zio_checksum_to_feature(intval
& ZIO_CHECKSUM_MASK
);
3949 if (feature
== SPA_FEATURE_NONE
)
3952 if ((err
= spa_open(dsname
, &spa
, FTAG
)) != 0)
3955 * Salted checksums are not supported on root pools.
3957 if (spa_bootfs(spa
) != 0 &&
3958 intval
< ZIO_CHECKSUM_FUNCTIONS
&&
3959 (zio_checksum_table
[intval
].ci_flags
&
3960 ZCHECKSUM_FLAG_SALTED
)) {
3961 spa_close(spa
, FTAG
);
3962 return (SET_ERROR(ERANGE
));
3964 if (!spa_feature_is_enabled(spa
, feature
)) {
3965 spa_close(spa
, FTAG
);
3966 return (SET_ERROR(ENOTSUP
));
3968 spa_close(spa
, FTAG
);
3976 return (zfs_secpolicy_setprop(dsname
, prop
, pair
, CRED()));
3980 * Removes properties from the given props list that fail permission checks
3981 * needed to clear them and to restore them in case of a receive error. For each
3982 * property, make sure we have both set and inherit permissions.
3984 * Returns the first error encountered if any permission checks fail. If the
3985 * caller provides a non-NULL errlist, it also gives the complete list of names
3986 * of all the properties that failed a permission check along with the
3987 * corresponding error numbers. The caller is responsible for freeing the
3990 * If every property checks out successfully, zero is returned and the list
3991 * pointed at by errlist is NULL.
3994 zfs_check_clearable(char *dataset
, nvlist_t
*props
, nvlist_t
**errlist
)
3997 nvpair_t
*pair
, *next_pair
;
4004 VERIFY(nvlist_alloc(&errors
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4006 zc
= kmem_alloc(sizeof (zfs_cmd_t
), KM_SLEEP
);
4007 (void) strlcpy(zc
->zc_name
, dataset
, sizeof (zc
->zc_name
));
4008 pair
= nvlist_next_nvpair(props
, NULL
);
4009 while (pair
!= NULL
) {
4010 next_pair
= nvlist_next_nvpair(props
, pair
);
4012 (void) strlcpy(zc
->zc_value
, nvpair_name(pair
),
4013 sizeof (zc
->zc_value
));
4014 if ((err
= zfs_check_settable(dataset
, pair
, CRED())) != 0 ||
4015 (err
= zfs_secpolicy_inherit_prop(zc
, NULL
, CRED())) != 0) {
4016 VERIFY(nvlist_remove_nvpair(props
, pair
) == 0);
4017 VERIFY(nvlist_add_int32(errors
,
4018 zc
->zc_value
, err
) == 0);
4022 kmem_free(zc
, sizeof (zfs_cmd_t
));
4024 if ((pair
= nvlist_next_nvpair(errors
, NULL
)) == NULL
) {
4025 nvlist_free(errors
);
4028 VERIFY(nvpair_value_int32(pair
, &rv
) == 0);
4031 if (errlist
== NULL
)
4032 nvlist_free(errors
);
4040 propval_equals(nvpair_t
*p1
, nvpair_t
*p2
)
4042 if (nvpair_type(p1
) == DATA_TYPE_NVLIST
) {
4043 /* dsl_prop_get_all_impl() format */
4045 VERIFY(nvpair_value_nvlist(p1
, &attrs
) == 0);
4046 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
4050 if (nvpair_type(p2
) == DATA_TYPE_NVLIST
) {
4052 VERIFY(nvpair_value_nvlist(p2
, &attrs
) == 0);
4053 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
4057 if (nvpair_type(p1
) != nvpair_type(p2
))
4060 if (nvpair_type(p1
) == DATA_TYPE_STRING
) {
4061 char *valstr1
, *valstr2
;
4063 VERIFY(nvpair_value_string(p1
, (char **)&valstr1
) == 0);
4064 VERIFY(nvpair_value_string(p2
, (char **)&valstr2
) == 0);
4065 return (strcmp(valstr1
, valstr2
) == 0);
4067 uint64_t intval1
, intval2
;
4069 VERIFY(nvpair_value_uint64(p1
, &intval1
) == 0);
4070 VERIFY(nvpair_value_uint64(p2
, &intval2
) == 0);
4071 return (intval1
== intval2
);
4076 * Remove properties from props if they are not going to change (as determined
4077 * by comparison with origprops). Remove them from origprops as well, since we
4078 * do not need to clear or restore properties that won't change.
4081 props_reduce(nvlist_t
*props
, nvlist_t
*origprops
)
4083 nvpair_t
*pair
, *next_pair
;
4085 if (origprops
== NULL
)
4086 return; /* all props need to be received */
4088 pair
= nvlist_next_nvpair(props
, NULL
);
4089 while (pair
!= NULL
) {
4090 const char *propname
= nvpair_name(pair
);
4093 next_pair
= nvlist_next_nvpair(props
, pair
);
4095 if ((nvlist_lookup_nvpair(origprops
, propname
,
4096 &match
) != 0) || !propval_equals(pair
, match
))
4097 goto next
; /* need to set received value */
4099 /* don't clear the existing received value */
4100 (void) nvlist_remove_nvpair(origprops
, match
);
4101 /* don't bother receiving the property */
4102 (void) nvlist_remove_nvpair(props
, pair
);
4109 * Extract properties that cannot be set PRIOR to the receipt of a dataset.
4110 * For example, refquota cannot be set until after the receipt of a dataset,
4111 * because in replication streams, an older/earlier snapshot may exceed the
4112 * refquota. We want to receive the older/earlier snapshot, but setting
4113 * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent
4114 * the older/earlier snapshot from being received (with EDQUOT).
4116 * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario.
4118 * libzfs will need to be judicious handling errors encountered by props
4119 * extracted by this function.
4122 extract_delay_props(nvlist_t
*props
)
4124 nvlist_t
*delayprops
;
4125 nvpair_t
*nvp
, *tmp
;
4126 static const zfs_prop_t delayable
[] = { ZFS_PROP_REFQUOTA
, 0 };
4129 VERIFY(nvlist_alloc(&delayprops
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4131 for (nvp
= nvlist_next_nvpair(props
, NULL
); nvp
!= NULL
;
4132 nvp
= nvlist_next_nvpair(props
, nvp
)) {
4134 * strcmp() is safe because zfs_prop_to_name() always returns
4137 for (i
= 0; delayable
[i
] != 0; i
++) {
4138 if (strcmp(zfs_prop_to_name(delayable
[i
]),
4139 nvpair_name(nvp
)) == 0) {
4143 if (delayable
[i
] != 0) {
4144 tmp
= nvlist_prev_nvpair(props
, nvp
);
4145 VERIFY(nvlist_add_nvpair(delayprops
, nvp
) == 0);
4146 VERIFY(nvlist_remove_nvpair(props
, nvp
) == 0);
4151 if (nvlist_empty(delayprops
)) {
4152 nvlist_free(delayprops
);
4155 return (delayprops
);
4159 static boolean_t zfs_ioc_recv_inject_err
;
4163 * nvlist 'errors' is always allocated. It will contain descriptions of
4164 * encountered errors, if any. It's the callers responsibility to free.
4167 zfs_ioc_recv_impl(char *tofs
, char *tosnap
, char *origin
,
4168 nvlist_t
*props
, boolean_t force
, boolean_t resumable
, int input_fd
,
4169 dmu_replay_record_t
*begin_record
, int cleanup_fd
, uint64_t *read_bytes
,
4170 uint64_t *errflags
, uint64_t *action_handle
, nvlist_t
**errors
)
4172 dmu_recv_cookie_t drc
;
4174 int props_error
= 0;
4176 nvlist_t
*delayprops
= NULL
; /* sent properties applied post-receive */
4177 nvlist_t
*origprops
= NULL
; /* existing properties */
4178 boolean_t first_recvd_props
= B_FALSE
;
4183 *errors
= fnvlist_alloc();
4185 input_fp
= getf(input_fd
);
4186 if (input_fp
== NULL
)
4187 return (SET_ERROR(EBADF
));
4189 error
= dmu_recv_begin(tofs
, tosnap
,
4190 begin_record
, force
, resumable
, origin
, &drc
);
4195 * Set properties before we receive the stream so that they are applied
4196 * to the new data. Note that we must call dmu_recv_stream() if
4197 * dmu_recv_begin() succeeds.
4199 if (props
!= NULL
&& !drc
.drc_newfs
) {
4200 if (spa_version(dsl_dataset_get_spa(drc
.drc_ds
)) >=
4201 SPA_VERSION_RECVD_PROPS
&&
4202 !dsl_prop_get_hasrecvd(tofs
))
4203 first_recvd_props
= B_TRUE
;
4206 * If new received properties are supplied, they are to
4207 * completely replace the existing received properties, so stash
4208 * away the existing ones.
4210 if (dsl_prop_get_received(tofs
, &origprops
) == 0) {
4211 nvlist_t
*errlist
= NULL
;
4213 * Don't bother writing a property if its value won't
4214 * change (and avoid the unnecessary security checks).
4216 * The first receive after SPA_VERSION_RECVD_PROPS is a
4217 * special case where we blow away all local properties
4220 if (!first_recvd_props
)
4221 props_reduce(props
, origprops
);
4222 if (zfs_check_clearable(tofs
, origprops
, &errlist
) != 0)
4223 (void) nvlist_merge(*errors
, errlist
, 0);
4224 nvlist_free(errlist
);
4226 if (clear_received_props(tofs
, origprops
,
4227 first_recvd_props
? NULL
: props
) != 0)
4228 *errflags
|= ZPROP_ERR_NOCLEAR
;
4230 *errflags
|= ZPROP_ERR_NOCLEAR
;
4234 if (props
!= NULL
) {
4235 props_error
= dsl_prop_set_hasrecvd(tofs
);
4237 if (props_error
== 0) {
4238 delayprops
= extract_delay_props(props
);
4239 (void) zfs_set_prop_nvlist(tofs
, ZPROP_SRC_RECEIVED
,
4244 off
= input_fp
->f_offset
;
4245 error
= dmu_recv_stream(&drc
, input_fp
->f_vnode
, &off
, cleanup_fd
,
4249 zfs_sb_t
*zsb
= NULL
;
4250 zvol_state_t
*zv
= NULL
;
4252 if (get_zfs_sb(tofs
, &zsb
) == 0) {
4257 ds
= dmu_objset_ds(zsb
->z_os
);
4258 error
= zfs_suspend_fs(zsb
);
4260 * If the suspend fails, then the recv_end will
4261 * likely also fail, and clean up after itself.
4263 end_err
= dmu_recv_end(&drc
, zsb
);
4265 error
= zfs_resume_fs(zsb
, ds
);
4266 error
= error
? error
: end_err
;
4267 deactivate_super(zsb
->z_sb
);
4268 } else if ((zv
= zvol_suspend(tofs
)) != NULL
) {
4269 error
= dmu_recv_end(&drc
, zvol_tag(zv
));
4272 error
= dmu_recv_end(&drc
, NULL
);
4275 /* Set delayed properties now, after we're done receiving. */
4276 if (delayprops
!= NULL
&& error
== 0) {
4277 (void) zfs_set_prop_nvlist(tofs
, ZPROP_SRC_RECEIVED
,
4278 delayprops
, *errors
);
4282 if (delayprops
!= NULL
) {
4284 * Merge delayed props back in with initial props, in case
4285 * we're DEBUG and zfs_ioc_recv_inject_err is set (which means
4286 * we have to make sure clear_received_props() includes
4287 * the delayed properties).
4289 * Since zfs_ioc_recv_inject_err is only in DEBUG kernels,
4290 * using ASSERT() will be just like a VERIFY.
4292 ASSERT(nvlist_merge(props
, delayprops
, 0) == 0);
4293 nvlist_free(delayprops
);
4297 *read_bytes
= off
- input_fp
->f_offset
;
4298 if (VOP_SEEK(input_fp
->f_vnode
, input_fp
->f_offset
, &off
, NULL
) == 0)
4299 input_fp
->f_offset
= off
;
4302 if (zfs_ioc_recv_inject_err
) {
4303 zfs_ioc_recv_inject_err
= B_FALSE
;
4309 * On error, restore the original props.
4311 if (error
!= 0 && props
!= NULL
&& !drc
.drc_newfs
) {
4312 if (clear_received_props(tofs
, props
, NULL
) != 0) {
4314 * We failed to clear the received properties.
4315 * Since we may have left a $recvd value on the
4316 * system, we can't clear the $hasrecvd flag.
4318 *errflags
|= ZPROP_ERR_NORESTORE
;
4319 } else if (first_recvd_props
) {
4320 dsl_prop_unset_hasrecvd(tofs
);
4323 if (origprops
== NULL
&& !drc
.drc_newfs
) {
4324 /* We failed to stash the original properties. */
4325 *errflags
|= ZPROP_ERR_NORESTORE
;
4329 * dsl_props_set() will not convert RECEIVED to LOCAL on or
4330 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
4331 * explicitly if we're restoring local properties cleared in the
4332 * first new-style receive.
4334 if (origprops
!= NULL
&&
4335 zfs_set_prop_nvlist(tofs
, (first_recvd_props
?
4336 ZPROP_SRC_LOCAL
: ZPROP_SRC_RECEIVED
),
4337 origprops
, NULL
) != 0) {
4339 * We stashed the original properties but failed to
4342 *errflags
|= ZPROP_ERR_NORESTORE
;
4347 nvlist_free(origprops
);
4350 error
= props_error
;
4357 * zc_name name of containing filesystem (unused)
4358 * zc_nvlist_src{_size} nvlist of properties to apply
4359 * zc_value name of snapshot to create
4360 * zc_string name of clone origin (if DRR_FLAG_CLONE)
4361 * zc_cookie file descriptor to recv from
4362 * zc_begin_record the BEGIN record of the stream (not byteswapped)
4363 * zc_guid force flag
4364 * zc_cleanup_fd cleanup-on-exit file descriptor
4365 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
4368 * zc_cookie number of bytes read
4369 * zc_obj zprop_errflags_t
4370 * zc_action_handle handle for this guid/ds mapping
4371 * zc_nvlist_dst{_size} error for each unapplied received property
4374 zfs_ioc_recv(zfs_cmd_t
*zc
)
4376 dmu_replay_record_t begin_record
;
4377 nvlist_t
*errors
= NULL
;
4378 nvlist_t
*props
= NULL
;
4379 char *origin
= NULL
;
4381 char tofs
[ZFS_MAX_DATASET_NAME_LEN
];
4384 if (dataset_namecheck(zc
->zc_value
, NULL
, NULL
) != 0 ||
4385 strchr(zc
->zc_value
, '@') == NULL
||
4386 strchr(zc
->zc_value
, '%'))
4387 return (SET_ERROR(EINVAL
));
4389 (void) strlcpy(tofs
, zc
->zc_value
, sizeof (tofs
));
4390 tosnap
= strchr(tofs
, '@');
4393 if (zc
->zc_nvlist_src
!= 0 &&
4394 (error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
4395 zc
->zc_iflags
, &props
)) != 0)
4398 if (zc
->zc_string
[0])
4399 origin
= zc
->zc_string
;
4401 begin_record
.drr_type
= DRR_BEGIN
;
4402 begin_record
.drr_payloadlen
= 0;
4403 begin_record
.drr_u
.drr_begin
= zc
->zc_begin_record
;
4405 error
= zfs_ioc_recv_impl(tofs
, tosnap
, origin
, props
, zc
->zc_guid
,
4406 B_FALSE
, zc
->zc_cookie
, &begin_record
, zc
->zc_cleanup_fd
,
4407 &zc
->zc_cookie
, &zc
->zc_obj
, &zc
->zc_action_handle
, &errors
);
4411 * Now that all props, initial and delayed, are set, report the prop
4412 * errors to the caller.
4414 if (zc
->zc_nvlist_dst_size
!= 0 && errors
!= NULL
&&
4415 (nvlist_smush(errors
, zc
->zc_nvlist_dst_size
) != 0 ||
4416 put_nvlist(zc
, errors
) != 0)) {
4418 * Caller made zc->zc_nvlist_dst less than the minimum expected
4419 * size or supplied an invalid address.
4421 error
= SET_ERROR(EINVAL
);
4424 nvlist_free(errors
);
4431 * "snapname" -> full name of the snapshot to create
4432 * (optional) "props" -> properties to set (nvlist)
4433 * (optional) "origin" -> name of clone origin (DRR_FLAG_CLONE)
4434 * "begin_record" -> non-byteswapped dmu_replay_record_t
4435 * "input_fd" -> file descriptor to read stream from (int32)
4436 * (optional) "force" -> force flag (value ignored)
4437 * (optional) "resumable" -> resumable flag (value ignored)
4438 * (optional) "cleanup_fd" -> cleanup-on-exit file descriptor
4439 * (optional) "action_handle" -> handle for this guid/ds mapping
4443 * "read_bytes" -> number of bytes read
4444 * "error_flags" -> zprop_errflags_t
4445 * "action_handle" -> handle for this guid/ds mapping
4446 * "errors" -> error for each unapplied received property (nvlist)
4450 zfs_ioc_recv_new(const char *fsname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
4452 dmu_replay_record_t
*begin_record
;
4453 uint_t begin_record_size
;
4454 nvlist_t
*errors
= NULL
;
4455 nvlist_t
*props
= NULL
;
4456 char *snapname
= NULL
;
4457 char *origin
= NULL
;
4459 char tofs
[ZFS_MAX_DATASET_NAME_LEN
];
4461 boolean_t resumable
;
4462 uint64_t action_handle
= 0;
4463 uint64_t read_bytes
= 0;
4464 uint64_t errflags
= 0;
4466 int cleanup_fd
= -1;
4469 error
= nvlist_lookup_string(innvl
, "snapname", &snapname
);
4471 return (SET_ERROR(EINVAL
));
4473 if (dataset_namecheck(snapname
, NULL
, NULL
) != 0 ||
4474 strchr(snapname
, '@') == NULL
||
4475 strchr(snapname
, '%'))
4476 return (SET_ERROR(EINVAL
));
4478 (void) strcpy(tofs
, snapname
);
4479 tosnap
= strchr(tofs
, '@');
4482 error
= nvlist_lookup_string(innvl
, "origin", &origin
);
4483 if (error
&& error
!= ENOENT
)
4486 error
= nvlist_lookup_byte_array(innvl
, "begin_record",
4487 (uchar_t
**)&begin_record
, &begin_record_size
);
4488 if (error
!= 0 || begin_record_size
!= sizeof (*begin_record
))
4489 return (SET_ERROR(EINVAL
));
4491 error
= nvlist_lookup_int32(innvl
, "input_fd", &input_fd
);
4493 return (SET_ERROR(EINVAL
));
4495 force
= nvlist_exists(innvl
, "force");
4496 resumable
= nvlist_exists(innvl
, "resumable");
4498 error
= nvlist_lookup_int32(innvl
, "cleanup_fd", &cleanup_fd
);
4499 if (error
&& error
!= ENOENT
)
4502 error
= nvlist_lookup_uint64(innvl
, "action_handle", &action_handle
);
4503 if (error
&& error
!= ENOENT
)
4506 error
= nvlist_lookup_nvlist(innvl
, "props", &props
);
4507 if (error
&& error
!= ENOENT
)
4510 error
= zfs_ioc_recv_impl(tofs
, tosnap
, origin
, props
, force
,
4511 resumable
, input_fd
, begin_record
, cleanup_fd
, &read_bytes
,
4512 &errflags
, &action_handle
, &errors
);
4514 fnvlist_add_uint64(outnvl
, "read_bytes", read_bytes
);
4515 fnvlist_add_uint64(outnvl
, "error_flags", errflags
);
4516 fnvlist_add_uint64(outnvl
, "action_handle", action_handle
);
4517 fnvlist_add_nvlist(outnvl
, "errors", errors
);
4519 nvlist_free(errors
);
4527 * zc_name name of snapshot to send
4528 * zc_cookie file descriptor to send stream to
4529 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
4530 * zc_sendobj objsetid of snapshot to send
4531 * zc_fromobj objsetid of incremental fromsnap (may be zero)
4532 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
4533 * output size in zc_objset_type.
4534 * zc_flags lzc_send_flags
4537 * zc_objset_type estimated size, if zc_guid is set
4540 zfs_ioc_send(zfs_cmd_t
*zc
)
4544 boolean_t estimate
= (zc
->zc_guid
!= 0);
4545 boolean_t embedok
= (zc
->zc_flags
& 0x1);
4546 boolean_t large_block_ok
= (zc
->zc_flags
& 0x2);
4547 boolean_t compressok
= (zc
->zc_flags
& 0x4);
4549 if (zc
->zc_obj
!= 0) {
4551 dsl_dataset_t
*tosnap
;
4553 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
4557 error
= dsl_dataset_hold_obj(dp
, zc
->zc_sendobj
, FTAG
, &tosnap
);
4559 dsl_pool_rele(dp
, FTAG
);
4563 if (dsl_dir_is_clone(tosnap
->ds_dir
))
4565 dsl_dir_phys(tosnap
->ds_dir
)->dd_origin_obj
;
4566 dsl_dataset_rele(tosnap
, FTAG
);
4567 dsl_pool_rele(dp
, FTAG
);
4572 dsl_dataset_t
*tosnap
;
4573 dsl_dataset_t
*fromsnap
= NULL
;
4575 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
4579 error
= dsl_dataset_hold_obj(dp
, zc
->zc_sendobj
, FTAG
, &tosnap
);
4581 dsl_pool_rele(dp
, FTAG
);
4585 if (zc
->zc_fromobj
!= 0) {
4586 error
= dsl_dataset_hold_obj(dp
, zc
->zc_fromobj
,
4589 dsl_dataset_rele(tosnap
, FTAG
);
4590 dsl_pool_rele(dp
, FTAG
);
4595 error
= dmu_send_estimate(tosnap
, fromsnap
, compressok
,
4596 &zc
->zc_objset_type
);
4598 if (fromsnap
!= NULL
)
4599 dsl_dataset_rele(fromsnap
, FTAG
);
4600 dsl_dataset_rele(tosnap
, FTAG
);
4601 dsl_pool_rele(dp
, FTAG
);
4603 file_t
*fp
= getf(zc
->zc_cookie
);
4605 return (SET_ERROR(EBADF
));
4608 error
= dmu_send_obj(zc
->zc_name
, zc
->zc_sendobj
,
4609 zc
->zc_fromobj
, embedok
, large_block_ok
, compressok
,
4610 zc
->zc_cookie
, fp
->f_vnode
, &off
);
4612 if (VOP_SEEK(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
4614 releasef(zc
->zc_cookie
);
4621 * zc_name name of snapshot on which to report progress
4622 * zc_cookie file descriptor of send stream
4625 * zc_cookie number of bytes written in send stream thus far
4628 zfs_ioc_send_progress(zfs_cmd_t
*zc
)
4632 dmu_sendarg_t
*dsp
= NULL
;
4635 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
4639 error
= dsl_dataset_hold(dp
, zc
->zc_name
, FTAG
, &ds
);
4641 dsl_pool_rele(dp
, FTAG
);
4645 mutex_enter(&ds
->ds_sendstream_lock
);
4648 * Iterate over all the send streams currently active on this dataset.
4649 * If there's one which matches the specified file descriptor _and_ the
4650 * stream was started by the current process, return the progress of
4654 for (dsp
= list_head(&ds
->ds_sendstreams
); dsp
!= NULL
;
4655 dsp
= list_next(&ds
->ds_sendstreams
, dsp
)) {
4656 if (dsp
->dsa_outfd
== zc
->zc_cookie
&&
4657 dsp
->dsa_proc
->group_leader
== curproc
->group_leader
)
4662 zc
->zc_cookie
= *(dsp
->dsa_off
);
4664 error
= SET_ERROR(ENOENT
);
4666 mutex_exit(&ds
->ds_sendstream_lock
);
4667 dsl_dataset_rele(ds
, FTAG
);
4668 dsl_pool_rele(dp
, FTAG
);
4673 zfs_ioc_inject_fault(zfs_cmd_t
*zc
)
4677 error
= zio_inject_fault(zc
->zc_name
, (int)zc
->zc_guid
, &id
,
4678 &zc
->zc_inject_record
);
4681 zc
->zc_guid
= (uint64_t)id
;
4687 zfs_ioc_clear_fault(zfs_cmd_t
*zc
)
4689 return (zio_clear_fault((int)zc
->zc_guid
));
4693 zfs_ioc_inject_list_next(zfs_cmd_t
*zc
)
4695 int id
= (int)zc
->zc_guid
;
4698 error
= zio_inject_list_next(&id
, zc
->zc_name
, sizeof (zc
->zc_name
),
4699 &zc
->zc_inject_record
);
4707 zfs_ioc_error_log(zfs_cmd_t
*zc
)
4711 size_t count
= (size_t)zc
->zc_nvlist_dst_size
;
4713 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
4716 error
= spa_get_errlog(spa
, (void *)(uintptr_t)zc
->zc_nvlist_dst
,
4719 zc
->zc_nvlist_dst_size
= count
;
4721 zc
->zc_nvlist_dst_size
= spa_get_errlog_size(spa
);
4723 spa_close(spa
, FTAG
);
4729 zfs_ioc_clear(zfs_cmd_t
*zc
)
4736 * On zpool clear we also fix up missing slogs
4738 mutex_enter(&spa_namespace_lock
);
4739 spa
= spa_lookup(zc
->zc_name
);
4741 mutex_exit(&spa_namespace_lock
);
4742 return (SET_ERROR(EIO
));
4744 if (spa_get_log_state(spa
) == SPA_LOG_MISSING
) {
4745 /* we need to let spa_open/spa_load clear the chains */
4746 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
4748 spa
->spa_last_open_failed
= 0;
4749 mutex_exit(&spa_namespace_lock
);
4751 if (zc
->zc_cookie
& ZPOOL_NO_REWIND
) {
4752 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
4755 nvlist_t
*config
= NULL
;
4757 if (zc
->zc_nvlist_src
== 0)
4758 return (SET_ERROR(EINVAL
));
4760 if ((error
= get_nvlist(zc
->zc_nvlist_src
,
4761 zc
->zc_nvlist_src_size
, zc
->zc_iflags
, &policy
)) == 0) {
4762 error
= spa_open_rewind(zc
->zc_name
, &spa
, FTAG
,
4764 if (config
!= NULL
) {
4767 if ((err
= put_nvlist(zc
, config
)) != 0)
4769 nvlist_free(config
);
4771 nvlist_free(policy
);
4778 spa_vdev_state_enter(spa
, SCL_NONE
);
4780 if (zc
->zc_guid
== 0) {
4783 vd
= spa_lookup_by_guid(spa
, zc
->zc_guid
, B_TRUE
);
4785 (void) spa_vdev_state_exit(spa
, NULL
, ENODEV
);
4786 spa_close(spa
, FTAG
);
4787 return (SET_ERROR(ENODEV
));
4791 vdev_clear(spa
, vd
);
4793 (void) spa_vdev_state_exit(spa
, NULL
, 0);
4796 * Resume any suspended I/Os.
4798 if (zio_resume(spa
) != 0)
4799 error
= SET_ERROR(EIO
);
4801 spa_close(spa
, FTAG
);
4807 zfs_ioc_pool_reopen(zfs_cmd_t
*zc
)
4812 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
4816 spa_vdev_state_enter(spa
, SCL_NONE
);
4819 * If a resilver is already in progress then set the
4820 * spa_scrub_reopen flag to B_TRUE so that we don't restart
4821 * the scan as a side effect of the reopen. Otherwise, let
4822 * vdev_open() decided if a resilver is required.
4824 spa
->spa_scrub_reopen
= dsl_scan_resilvering(spa
->spa_dsl_pool
);
4825 vdev_reopen(spa
->spa_root_vdev
);
4826 spa
->spa_scrub_reopen
= B_FALSE
;
4828 (void) spa_vdev_state_exit(spa
, NULL
, 0);
4829 spa_close(spa
, FTAG
);
4834 * zc_name name of filesystem
4835 * zc_value name of origin snapshot
4838 * zc_string name of conflicting snapshot, if there is one
4841 zfs_ioc_promote(zfs_cmd_t
*zc
)
4846 * We don't need to unmount *all* the origin fs's snapshots, but
4849 cp
= strchr(zc
->zc_value
, '@');
4852 (void) dmu_objset_find(zc
->zc_value
,
4853 zfs_unmount_snap_cb
, NULL
, DS_FIND_SNAPSHOTS
);
4854 return (dsl_dataset_promote(zc
->zc_name
, zc
->zc_string
));
4858 * Retrieve a single {user|group}{used|quota}@... property.
4861 * zc_name name of filesystem
4862 * zc_objset_type zfs_userquota_prop_t
4863 * zc_value domain name (eg. "S-1-234-567-89")
4864 * zc_guid RID/UID/GID
4867 * zc_cookie property value
4870 zfs_ioc_userspace_one(zfs_cmd_t
*zc
)
4875 if (zc
->zc_objset_type
>= ZFS_NUM_USERQUOTA_PROPS
)
4876 return (SET_ERROR(EINVAL
));
4878 error
= zfs_sb_hold(zc
->zc_name
, FTAG
, &zsb
, B_FALSE
);
4882 error
= zfs_userspace_one(zsb
,
4883 zc
->zc_objset_type
, zc
->zc_value
, zc
->zc_guid
, &zc
->zc_cookie
);
4884 zfs_sb_rele(zsb
, FTAG
);
4891 * zc_name name of filesystem
4892 * zc_cookie zap cursor
4893 * zc_objset_type zfs_userquota_prop_t
4894 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
4897 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
4898 * zc_cookie zap cursor
4901 zfs_ioc_userspace_many(zfs_cmd_t
*zc
)
4904 int bufsize
= zc
->zc_nvlist_dst_size
;
4909 return (SET_ERROR(ENOMEM
));
4911 error
= zfs_sb_hold(zc
->zc_name
, FTAG
, &zsb
, B_FALSE
);
4915 buf
= vmem_alloc(bufsize
, KM_SLEEP
);
4917 error
= zfs_userspace_many(zsb
, zc
->zc_objset_type
, &zc
->zc_cookie
,
4918 buf
, &zc
->zc_nvlist_dst_size
);
4921 error
= xcopyout(buf
,
4922 (void *)(uintptr_t)zc
->zc_nvlist_dst
,
4923 zc
->zc_nvlist_dst_size
);
4925 vmem_free(buf
, bufsize
);
4926 zfs_sb_rele(zsb
, FTAG
);
4933 * zc_name name of filesystem
4939 zfs_ioc_userspace_upgrade(zfs_cmd_t
*zc
)
4945 if (get_zfs_sb(zc
->zc_name
, &zsb
) == 0) {
4946 if (!dmu_objset_userused_enabled(zsb
->z_os
)) {
4948 * If userused is not enabled, it may be because the
4949 * objset needs to be closed & reopened (to grow the
4950 * objset_phys_t). Suspend/resume the fs will do that.
4954 ds
= dmu_objset_ds(zsb
->z_os
);
4955 error
= zfs_suspend_fs(zsb
);
4957 dmu_objset_refresh_ownership(zsb
->z_os
,
4959 error
= zfs_resume_fs(zsb
, ds
);
4963 error
= dmu_objset_userspace_upgrade(zsb
->z_os
);
4964 deactivate_super(zsb
->z_sb
);
4966 /* XXX kind of reading contents without owning */
4967 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
4971 error
= dmu_objset_userspace_upgrade(os
);
4972 dmu_objset_rele(os
, FTAG
);
4980 * zc_name name of filesystem
4986 zfs_ioc_userobjspace_upgrade(zfs_cmd_t
*zc
)
4991 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
4995 dsl_dataset_long_hold(dmu_objset_ds(os
), FTAG
);
4996 dsl_pool_rele(dmu_objset_pool(os
), FTAG
);
4998 if (dmu_objset_userobjspace_upgradable(os
)) {
4999 mutex_enter(&os
->os_upgrade_lock
);
5000 if (os
->os_upgrade_id
== 0) {
5001 /* clear potential error code and retry */
5002 os
->os_upgrade_status
= 0;
5003 mutex_exit(&os
->os_upgrade_lock
);
5005 dmu_objset_userobjspace_upgrade(os
);
5007 mutex_exit(&os
->os_upgrade_lock
);
5010 taskq_wait_id(os
->os_spa
->spa_upgrade_taskq
, os
->os_upgrade_id
);
5011 error
= os
->os_upgrade_status
;
5014 dsl_dataset_long_rele(dmu_objset_ds(os
), FTAG
);
5015 dsl_dataset_rele(dmu_objset_ds(os
), FTAG
);
5021 zfs_ioc_share(zfs_cmd_t
*zc
)
5023 return (SET_ERROR(ENOSYS
));
5026 ace_t full_access
[] = {
5027 {(uid_t
)-1, ACE_ALL_PERMS
, ACE_EVERYONE
, 0}
5032 * zc_name name of containing filesystem
5033 * zc_obj object # beyond which we want next in-use object #
5036 * zc_obj next in-use object #
5039 zfs_ioc_next_obj(zfs_cmd_t
*zc
)
5041 objset_t
*os
= NULL
;
5044 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
5048 error
= dmu_object_next(os
, &zc
->zc_obj
, B_FALSE
, 0);
5050 dmu_objset_rele(os
, FTAG
);
5056 * zc_name name of filesystem
5057 * zc_value prefix name for snapshot
5058 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
5061 * zc_value short name of new snapshot
5064 zfs_ioc_tmp_snapshot(zfs_cmd_t
*zc
)
5071 error
= zfs_onexit_fd_hold(zc
->zc_cleanup_fd
, &minor
);
5075 snap_name
= kmem_asprintf("%s-%016llx", zc
->zc_value
,
5076 (u_longlong_t
)ddi_get_lbolt64());
5077 hold_name
= kmem_asprintf("%%%s", zc
->zc_value
);
5079 error
= dsl_dataset_snapshot_tmp(zc
->zc_name
, snap_name
, minor
,
5082 (void) strlcpy(zc
->zc_value
, snap_name
,
5083 sizeof (zc
->zc_value
));
5086 zfs_onexit_fd_rele(zc
->zc_cleanup_fd
);
5092 * zc_name name of "to" snapshot
5093 * zc_value name of "from" snapshot
5094 * zc_cookie file descriptor to write diff data on
5097 * dmu_diff_record_t's to the file descriptor
5100 zfs_ioc_diff(zfs_cmd_t
*zc
)
5106 fp
= getf(zc
->zc_cookie
);
5108 return (SET_ERROR(EBADF
));
5112 error
= dmu_diff(zc
->zc_name
, zc
->zc_value
, fp
->f_vnode
, &off
);
5114 if (VOP_SEEK(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
5116 releasef(zc
->zc_cookie
);
5122 * Remove all ACL files in shares dir
5124 #ifdef HAVE_SMB_SHARE
5126 zfs_smb_acl_purge(znode_t
*dzp
)
5129 zap_attribute_t zap
;
5130 zfs_sb_t
*zsb
= ZTOZSB(dzp
);
5133 for (zap_cursor_init(&zc
, zsb
->z_os
, dzp
->z_id
);
5134 (error
= zap_cursor_retrieve(&zc
, &zap
)) == 0;
5135 zap_cursor_advance(&zc
)) {
5136 if ((error
= VOP_REMOVE(ZTOV(dzp
), zap
.za_name
, kcred
,
5140 zap_cursor_fini(&zc
);
5143 #endif /* HAVE_SMB_SHARE */
5146 zfs_ioc_smb_acl(zfs_cmd_t
*zc
)
5148 #ifdef HAVE_SMB_SHARE
5151 vnode_t
*resourcevp
= NULL
;
5160 if ((error
= lookupname(zc
->zc_value
, UIO_SYSSPACE
,
5161 NO_FOLLOW
, NULL
, &vp
)) != 0)
5164 /* Now make sure mntpnt and dataset are ZFS */
5166 if (vp
->v_vfsp
->vfs_fstype
!= zfsfstype
||
5167 (strcmp((char *)refstr_value(vp
->v_vfsp
->vfs_resource
),
5168 zc
->zc_name
) != 0)) {
5170 return (SET_ERROR(EINVAL
));
5178 * Create share dir if its missing.
5180 mutex_enter(&zsb
->z_lock
);
5181 if (zsb
->z_shares_dir
== 0) {
5184 tx
= dmu_tx_create(zsb
->z_os
);
5185 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
, TRUE
,
5187 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, FALSE
, NULL
);
5188 error
= dmu_tx_assign(tx
, TXG_WAIT
);
5192 error
= zfs_create_share_dir(zsb
, tx
);
5196 mutex_exit(&zsb
->z_lock
);
5202 mutex_exit(&zsb
->z_lock
);
5204 ASSERT(zsb
->z_shares_dir
);
5205 if ((error
= zfs_zget(zsb
, zsb
->z_shares_dir
, &sharedir
)) != 0) {
5211 switch (zc
->zc_cookie
) {
5212 case ZFS_SMB_ACL_ADD
:
5213 vattr
.va_mask
= AT_MODE
|AT_UID
|AT_GID
|AT_TYPE
;
5214 vattr
.va_mode
= S_IFREG
|0777;
5218 vsec
.vsa_mask
= VSA_ACE
;
5219 vsec
.vsa_aclentp
= &full_access
;
5220 vsec
.vsa_aclentsz
= sizeof (full_access
);
5221 vsec
.vsa_aclcnt
= 1;
5223 error
= VOP_CREATE(ZTOV(sharedir
), zc
->zc_string
,
5224 &vattr
, EXCL
, 0, &resourcevp
, kcred
, 0, NULL
, &vsec
);
5226 VN_RELE(resourcevp
);
5229 case ZFS_SMB_ACL_REMOVE
:
5230 error
= VOP_REMOVE(ZTOV(sharedir
), zc
->zc_string
, kcred
,
5234 case ZFS_SMB_ACL_RENAME
:
5235 if ((error
= get_nvlist(zc
->zc_nvlist_src
,
5236 zc
->zc_nvlist_src_size
, zc
->zc_iflags
, &nvlist
)) != 0) {
5238 VN_RELE(ZTOV(sharedir
));
5242 if (nvlist_lookup_string(nvlist
, ZFS_SMB_ACL_SRC
, &src
) ||
5243 nvlist_lookup_string(nvlist
, ZFS_SMB_ACL_TARGET
,
5246 VN_RELE(ZTOV(sharedir
));
5248 nvlist_free(nvlist
);
5251 error
= VOP_RENAME(ZTOV(sharedir
), src
, ZTOV(sharedir
), target
,
5253 nvlist_free(nvlist
);
5256 case ZFS_SMB_ACL_PURGE
:
5257 error
= zfs_smb_acl_purge(sharedir
);
5261 error
= SET_ERROR(EINVAL
);
5266 VN_RELE(ZTOV(sharedir
));
5272 return (SET_ERROR(ENOTSUP
));
5273 #endif /* HAVE_SMB_SHARE */
5278 * "holds" -> { snapname -> holdname (string), ... }
5279 * (optional) "cleanup_fd" -> fd (int32)
5283 * snapname -> error value (int32)
5289 zfs_ioc_hold(const char *pool
, nvlist_t
*args
, nvlist_t
*errlist
)
5293 int cleanup_fd
= -1;
5297 error
= nvlist_lookup_nvlist(args
, "holds", &holds
);
5299 return (SET_ERROR(EINVAL
));
5301 /* make sure the user didn't pass us any invalid (empty) tags */
5302 for (pair
= nvlist_next_nvpair(holds
, NULL
); pair
!= NULL
;
5303 pair
= nvlist_next_nvpair(holds
, pair
)) {
5306 error
= nvpair_value_string(pair
, &htag
);
5308 return (SET_ERROR(error
));
5310 if (strlen(htag
) == 0)
5311 return (SET_ERROR(EINVAL
));
5314 if (nvlist_lookup_int32(args
, "cleanup_fd", &cleanup_fd
) == 0) {
5315 error
= zfs_onexit_fd_hold(cleanup_fd
, &minor
);
5320 error
= dsl_dataset_user_hold(holds
, minor
, errlist
);
5322 zfs_onexit_fd_rele(cleanup_fd
);
5327 * innvl is not used.
5330 * holdname -> time added (uint64 seconds since epoch)
5336 zfs_ioc_get_holds(const char *snapname
, nvlist_t
*args
, nvlist_t
*outnvl
)
5338 return (dsl_dataset_get_holds(snapname
, outnvl
));
5343 * snapname -> { holdname, ... }
5348 * snapname -> error value (int32)
5354 zfs_ioc_release(const char *pool
, nvlist_t
*holds
, nvlist_t
*errlist
)
5356 return (dsl_dataset_user_release(holds
, errlist
));
5361 * zc_guid flags (ZEVENT_NONBLOCK)
5362 * zc_cleanup_fd zevent file descriptor
5365 * zc_nvlist_dst next nvlist event
5366 * zc_cookie dropped events since last get
5369 zfs_ioc_events_next(zfs_cmd_t
*zc
)
5372 nvlist_t
*event
= NULL
;
5374 uint64_t dropped
= 0;
5377 error
= zfs_zevent_fd_hold(zc
->zc_cleanup_fd
, &minor
, &ze
);
5382 error
= zfs_zevent_next(ze
, &event
,
5383 &zc
->zc_nvlist_dst_size
, &dropped
);
5384 if (event
!= NULL
) {
5385 zc
->zc_cookie
= dropped
;
5386 error
= put_nvlist(zc
, event
);
5390 if (zc
->zc_guid
& ZEVENT_NONBLOCK
)
5393 if ((error
== 0) || (error
!= ENOENT
))
5396 error
= zfs_zevent_wait(ze
);
5401 zfs_zevent_fd_rele(zc
->zc_cleanup_fd
);
5408 * zc_cookie cleared events count
5411 zfs_ioc_events_clear(zfs_cmd_t
*zc
)
5415 zfs_zevent_drain_all(&count
);
5416 zc
->zc_cookie
= count
;
5423 * zc_guid eid | ZEVENT_SEEK_START | ZEVENT_SEEK_END
5424 * zc_cleanup zevent file descriptor
5427 zfs_ioc_events_seek(zfs_cmd_t
*zc
)
5433 error
= zfs_zevent_fd_hold(zc
->zc_cleanup_fd
, &minor
, &ze
);
5437 error
= zfs_zevent_seek(ze
, zc
->zc_guid
);
5438 zfs_zevent_fd_rele(zc
->zc_cleanup_fd
);
5445 * zc_name name of new filesystem or snapshot
5446 * zc_value full name of old snapshot
5449 * zc_cookie space in bytes
5450 * zc_objset_type compressed space in bytes
5451 * zc_perm_action uncompressed space in bytes
5454 zfs_ioc_space_written(zfs_cmd_t
*zc
)
5458 dsl_dataset_t
*new, *old
;
5460 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
5463 error
= dsl_dataset_hold(dp
, zc
->zc_name
, FTAG
, &new);
5465 dsl_pool_rele(dp
, FTAG
);
5468 error
= dsl_dataset_hold(dp
, zc
->zc_value
, FTAG
, &old
);
5470 dsl_dataset_rele(new, FTAG
);
5471 dsl_pool_rele(dp
, FTAG
);
5475 error
= dsl_dataset_space_written(old
, new, &zc
->zc_cookie
,
5476 &zc
->zc_objset_type
, &zc
->zc_perm_action
);
5477 dsl_dataset_rele(old
, FTAG
);
5478 dsl_dataset_rele(new, FTAG
);
5479 dsl_pool_rele(dp
, FTAG
);
5485 * "firstsnap" -> snapshot name
5489 * "used" -> space in bytes
5490 * "compressed" -> compressed space in bytes
5491 * "uncompressed" -> uncompressed space in bytes
5495 zfs_ioc_space_snaps(const char *lastsnap
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
5499 dsl_dataset_t
*new, *old
;
5501 uint64_t used
, comp
, uncomp
;
5503 if (nvlist_lookup_string(innvl
, "firstsnap", &firstsnap
) != 0)
5504 return (SET_ERROR(EINVAL
));
5506 error
= dsl_pool_hold(lastsnap
, FTAG
, &dp
);
5510 error
= dsl_dataset_hold(dp
, lastsnap
, FTAG
, &new);
5511 if (error
== 0 && !new->ds_is_snapshot
) {
5512 dsl_dataset_rele(new, FTAG
);
5513 error
= SET_ERROR(EINVAL
);
5516 dsl_pool_rele(dp
, FTAG
);
5519 error
= dsl_dataset_hold(dp
, firstsnap
, FTAG
, &old
);
5520 if (error
== 0 && !old
->ds_is_snapshot
) {
5521 dsl_dataset_rele(old
, FTAG
);
5522 error
= SET_ERROR(EINVAL
);
5525 dsl_dataset_rele(new, FTAG
);
5526 dsl_pool_rele(dp
, FTAG
);
5530 error
= dsl_dataset_space_wouldfree(old
, new, &used
, &comp
, &uncomp
);
5531 dsl_dataset_rele(old
, FTAG
);
5532 dsl_dataset_rele(new, FTAG
);
5533 dsl_pool_rele(dp
, FTAG
);
5534 fnvlist_add_uint64(outnvl
, "used", used
);
5535 fnvlist_add_uint64(outnvl
, "compressed", comp
);
5536 fnvlist_add_uint64(outnvl
, "uncompressed", uncomp
);
5542 * "fd" -> file descriptor to write stream to (int32)
5543 * (optional) "fromsnap" -> full snap name to send an incremental from
5544 * (optional) "largeblockok" -> (value ignored)
5545 * indicates that blocks > 128KB are permitted
5546 * (optional) "embedok" -> (value ignored)
5547 * presence indicates DRR_WRITE_EMBEDDED records are permitted
5548 * (optional) "compressok" -> (value ignored)
5549 * presence indicates compressed DRR_WRITE records are permitted
5550 * (optional) "resume_object" and "resume_offset" -> (uint64)
5551 * if present, resume send stream from specified object and offset.
5558 zfs_ioc_send_new(const char *snapname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
5562 char *fromname
= NULL
;
5565 boolean_t largeblockok
;
5567 boolean_t compressok
;
5568 uint64_t resumeobj
= 0;
5569 uint64_t resumeoff
= 0;
5571 error
= nvlist_lookup_int32(innvl
, "fd", &fd
);
5573 return (SET_ERROR(EINVAL
));
5575 (void) nvlist_lookup_string(innvl
, "fromsnap", &fromname
);
5577 largeblockok
= nvlist_exists(innvl
, "largeblockok");
5578 embedok
= nvlist_exists(innvl
, "embedok");
5579 compressok
= nvlist_exists(innvl
, "compressok");
5581 (void) nvlist_lookup_uint64(innvl
, "resume_object", &resumeobj
);
5582 (void) nvlist_lookup_uint64(innvl
, "resume_offset", &resumeoff
);
5584 if ((fp
= getf(fd
)) == NULL
)
5585 return (SET_ERROR(EBADF
));
5588 error
= dmu_send(snapname
, fromname
, embedok
, largeblockok
, compressok
,
5589 fd
, resumeobj
, resumeoff
, fp
->f_vnode
, &off
);
5591 if (VOP_SEEK(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
5599 * Determine approximately how large a zfs send stream will be -- the number
5600 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
5603 * (optional) "from" -> full snap or bookmark name to send an incremental
5605 * (optional) "largeblockok" -> (value ignored)
5606 * indicates that blocks > 128KB are permitted
5607 * (optional) "embedok" -> (value ignored)
5608 * presence indicates DRR_WRITE_EMBEDDED records are permitted
5609 * (optional) "compressok" -> (value ignored)
5610 * presence indicates compressed DRR_WRITE records are permitted
5614 * "space" -> bytes of space (uint64)
5618 zfs_ioc_send_space(const char *snapname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
5621 dsl_dataset_t
*tosnap
;
5624 /* LINTED E_FUNC_SET_NOT_USED */
5625 boolean_t largeblockok
;
5626 /* LINTED E_FUNC_SET_NOT_USED */
5628 boolean_t compressok
;
5631 error
= dsl_pool_hold(snapname
, FTAG
, &dp
);
5635 error
= dsl_dataset_hold(dp
, snapname
, FTAG
, &tosnap
);
5637 dsl_pool_rele(dp
, FTAG
);
5641 largeblockok
= nvlist_exists(innvl
, "largeblockok");
5642 embedok
= nvlist_exists(innvl
, "embedok");
5643 compressok
= nvlist_exists(innvl
, "compressok");
5645 error
= nvlist_lookup_string(innvl
, "from", &fromname
);
5647 if (strchr(fromname
, '@') != NULL
) {
5649 * If from is a snapshot, hold it and use the more
5650 * efficient dmu_send_estimate to estimate send space
5651 * size using deadlists.
5653 dsl_dataset_t
*fromsnap
;
5654 error
= dsl_dataset_hold(dp
, fromname
, FTAG
, &fromsnap
);
5657 error
= dmu_send_estimate(tosnap
, fromsnap
, compressok
,
5659 dsl_dataset_rele(fromsnap
, FTAG
);
5660 } else if (strchr(fromname
, '#') != NULL
) {
5662 * If from is a bookmark, fetch the creation TXG of the
5663 * snapshot it was created from and use that to find
5664 * blocks that were born after it.
5666 zfs_bookmark_phys_t frombm
;
5668 error
= dsl_bookmark_lookup(dp
, fromname
, tosnap
,
5672 error
= dmu_send_estimate_from_txg(tosnap
,
5673 frombm
.zbm_creation_txg
, compressok
, &space
);
5676 * from is not properly formatted as a snapshot or
5679 error
= SET_ERROR(EINVAL
);
5683 // If estimating the size of a full send, use dmu_send_estimate
5684 error
= dmu_send_estimate(tosnap
, NULL
, compressok
, &space
);
5687 fnvlist_add_uint64(outnvl
, "space", space
);
5690 dsl_dataset_rele(tosnap
, FTAG
);
5691 dsl_pool_rele(dp
, FTAG
);
5695 static zfs_ioc_vec_t zfs_ioc_vec
[ZFS_IOC_LAST
- ZFS_IOC_FIRST
];
5698 zfs_ioctl_register_legacy(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5699 zfs_secpolicy_func_t
*secpolicy
, zfs_ioc_namecheck_t namecheck
,
5700 boolean_t log_history
, zfs_ioc_poolcheck_t pool_check
)
5702 zfs_ioc_vec_t
*vec
= &zfs_ioc_vec
[ioc
- ZFS_IOC_FIRST
];
5704 ASSERT3U(ioc
, >=, ZFS_IOC_FIRST
);
5705 ASSERT3U(ioc
, <, ZFS_IOC_LAST
);
5706 ASSERT3P(vec
->zvec_legacy_func
, ==, NULL
);
5707 ASSERT3P(vec
->zvec_func
, ==, NULL
);
5709 vec
->zvec_legacy_func
= func
;
5710 vec
->zvec_secpolicy
= secpolicy
;
5711 vec
->zvec_namecheck
= namecheck
;
5712 vec
->zvec_allow_log
= log_history
;
5713 vec
->zvec_pool_check
= pool_check
;
5717 * See the block comment at the beginning of this file for details on
5718 * each argument to this function.
5721 zfs_ioctl_register(const char *name
, zfs_ioc_t ioc
, zfs_ioc_func_t
*func
,
5722 zfs_secpolicy_func_t
*secpolicy
, zfs_ioc_namecheck_t namecheck
,
5723 zfs_ioc_poolcheck_t pool_check
, boolean_t smush_outnvlist
,
5724 boolean_t allow_log
)
5726 zfs_ioc_vec_t
*vec
= &zfs_ioc_vec
[ioc
- ZFS_IOC_FIRST
];
5728 ASSERT3U(ioc
, >=, ZFS_IOC_FIRST
);
5729 ASSERT3U(ioc
, <, ZFS_IOC_LAST
);
5730 ASSERT3P(vec
->zvec_legacy_func
, ==, NULL
);
5731 ASSERT3P(vec
->zvec_func
, ==, NULL
);
5733 /* if we are logging, the name must be valid */
5734 ASSERT(!allow_log
|| namecheck
!= NO_NAME
);
5736 vec
->zvec_name
= name
;
5737 vec
->zvec_func
= func
;
5738 vec
->zvec_secpolicy
= secpolicy
;
5739 vec
->zvec_namecheck
= namecheck
;
5740 vec
->zvec_pool_check
= pool_check
;
5741 vec
->zvec_smush_outnvlist
= smush_outnvlist
;
5742 vec
->zvec_allow_log
= allow_log
;
5746 zfs_ioctl_register_pool(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5747 zfs_secpolicy_func_t
*secpolicy
, boolean_t log_history
,
5748 zfs_ioc_poolcheck_t pool_check
)
5750 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5751 POOL_NAME
, log_history
, pool_check
);
5755 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5756 zfs_secpolicy_func_t
*secpolicy
, zfs_ioc_poolcheck_t pool_check
)
5758 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5759 DATASET_NAME
, B_FALSE
, pool_check
);
5763 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
)
5765 zfs_ioctl_register_legacy(ioc
, func
, zfs_secpolicy_config
,
5766 POOL_NAME
, B_TRUE
, POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
5770 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5771 zfs_secpolicy_func_t
*secpolicy
)
5773 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5774 NO_NAME
, B_FALSE
, POOL_CHECK_NONE
);
5778 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc
,
5779 zfs_ioc_legacy_func_t
*func
, zfs_secpolicy_func_t
*secpolicy
)
5781 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5782 DATASET_NAME
, B_FALSE
, POOL_CHECK_SUSPENDED
);
5786 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
)
5788 zfs_ioctl_register_dataset_read_secpolicy(ioc
, func
,
5789 zfs_secpolicy_read
);
5793 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5794 zfs_secpolicy_func_t
*secpolicy
)
5796 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5797 DATASET_NAME
, B_TRUE
, POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
5801 zfs_ioctl_init(void)
5803 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT
,
5804 zfs_ioc_snapshot
, zfs_secpolicy_snapshot
, POOL_NAME
,
5805 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5807 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY
,
5808 zfs_ioc_log_history
, zfs_secpolicy_log_history
, NO_NAME
,
5809 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_FALSE
, B_FALSE
);
5811 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS
,
5812 zfs_ioc_space_snaps
, zfs_secpolicy_read
, DATASET_NAME
,
5813 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
5815 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW
,
5816 zfs_ioc_send_new
, zfs_secpolicy_send_new
, DATASET_NAME
,
5817 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
5819 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE
,
5820 zfs_ioc_send_space
, zfs_secpolicy_read
, DATASET_NAME
,
5821 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
5823 zfs_ioctl_register("create", ZFS_IOC_CREATE
,
5824 zfs_ioc_create
, zfs_secpolicy_create_clone
, DATASET_NAME
,
5825 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5827 zfs_ioctl_register("clone", ZFS_IOC_CLONE
,
5828 zfs_ioc_clone
, zfs_secpolicy_create_clone
, DATASET_NAME
,
5829 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5831 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS
,
5832 zfs_ioc_destroy_snaps
, zfs_secpolicy_destroy_snaps
, POOL_NAME
,
5833 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5835 zfs_ioctl_register("hold", ZFS_IOC_HOLD
,
5836 zfs_ioc_hold
, zfs_secpolicy_hold
, POOL_NAME
,
5837 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5838 zfs_ioctl_register("release", ZFS_IOC_RELEASE
,
5839 zfs_ioc_release
, zfs_secpolicy_release
, POOL_NAME
,
5840 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5842 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS
,
5843 zfs_ioc_get_holds
, zfs_secpolicy_read
, DATASET_NAME
,
5844 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
5846 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK
,
5847 zfs_ioc_rollback
, zfs_secpolicy_rollback
, DATASET_NAME
,
5848 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_FALSE
, B_TRUE
);
5850 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK
,
5851 zfs_ioc_bookmark
, zfs_secpolicy_bookmark
, POOL_NAME
,
5852 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5854 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS
,
5855 zfs_ioc_get_bookmarks
, zfs_secpolicy_read
, DATASET_NAME
,
5856 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
5858 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS
,
5859 zfs_ioc_destroy_bookmarks
, zfs_secpolicy_destroy_bookmarks
,
5861 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5863 zfs_ioctl_register("receive", ZFS_IOC_RECV_NEW
,
5864 zfs_ioc_recv_new
, zfs_secpolicy_recv_new
, DATASET_NAME
,
5865 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5867 /* IOCTLS that use the legacy function signature */
5869 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE
, zfs_ioc_pool_freeze
,
5870 zfs_secpolicy_config
, NO_NAME
, B_FALSE
, POOL_CHECK_READONLY
);
5872 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE
, zfs_ioc_pool_create
,
5873 zfs_secpolicy_config
, B_TRUE
, POOL_CHECK_NONE
);
5874 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN
,
5876 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE
,
5877 zfs_ioc_pool_upgrade
);
5878 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD
,
5880 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE
,
5881 zfs_ioc_vdev_remove
);
5882 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE
,
5883 zfs_ioc_vdev_set_state
);
5884 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH
,
5885 zfs_ioc_vdev_attach
);
5886 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH
,
5887 zfs_ioc_vdev_detach
);
5888 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH
,
5889 zfs_ioc_vdev_setpath
);
5890 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU
,
5891 zfs_ioc_vdev_setfru
);
5892 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS
,
5893 zfs_ioc_pool_set_props
);
5894 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT
,
5895 zfs_ioc_vdev_split
);
5896 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID
,
5897 zfs_ioc_pool_reguid
);
5899 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS
,
5900 zfs_ioc_pool_configs
, zfs_secpolicy_none
);
5901 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT
,
5902 zfs_ioc_pool_tryimport
, zfs_secpolicy_config
);
5903 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT
,
5904 zfs_ioc_inject_fault
, zfs_secpolicy_inject
);
5905 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT
,
5906 zfs_ioc_clear_fault
, zfs_secpolicy_inject
);
5907 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT
,
5908 zfs_ioc_inject_list_next
, zfs_secpolicy_inject
);
5911 * pool destroy, and export don't log the history as part of
5912 * zfsdev_ioctl, but rather zfs_ioc_pool_export
5913 * does the logging of those commands.
5915 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY
, zfs_ioc_pool_destroy
,
5916 zfs_secpolicy_config
, B_FALSE
, POOL_CHECK_SUSPENDED
);
5917 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT
, zfs_ioc_pool_export
,
5918 zfs_secpolicy_config
, B_FALSE
, POOL_CHECK_SUSPENDED
);
5920 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS
, zfs_ioc_pool_stats
,
5921 zfs_secpolicy_read
, B_FALSE
, POOL_CHECK_NONE
);
5922 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS
, zfs_ioc_pool_get_props
,
5923 zfs_secpolicy_read
, B_FALSE
, POOL_CHECK_NONE
);
5925 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG
, zfs_ioc_error_log
,
5926 zfs_secpolicy_inject
, B_FALSE
, POOL_CHECK_SUSPENDED
);
5927 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME
,
5928 zfs_ioc_dsobj_to_dsname
,
5929 zfs_secpolicy_diff
, B_FALSE
, POOL_CHECK_SUSPENDED
);
5930 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY
,
5931 zfs_ioc_pool_get_history
,
5932 zfs_secpolicy_config
, B_FALSE
, POOL_CHECK_SUSPENDED
);
5934 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT
, zfs_ioc_pool_import
,
5935 zfs_secpolicy_config
, B_TRUE
, POOL_CHECK_NONE
);
5937 zfs_ioctl_register_pool(ZFS_IOC_CLEAR
, zfs_ioc_clear
,
5938 zfs_secpolicy_config
, B_TRUE
, POOL_CHECK_NONE
);
5939 zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN
, zfs_ioc_pool_reopen
,
5940 zfs_secpolicy_config
, B_TRUE
, POOL_CHECK_SUSPENDED
);
5942 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN
,
5943 zfs_ioc_space_written
);
5944 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS
,
5945 zfs_ioc_objset_recvd_props
);
5946 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ
,
5948 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL
,
5950 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS
,
5951 zfs_ioc_objset_stats
);
5952 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS
,
5953 zfs_ioc_objset_zplprops
);
5954 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT
,
5955 zfs_ioc_dataset_list_next
);
5956 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT
,
5957 zfs_ioc_snapshot_list_next
);
5958 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS
,
5959 zfs_ioc_send_progress
);
5961 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF
,
5962 zfs_ioc_diff
, zfs_secpolicy_diff
);
5963 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS
,
5964 zfs_ioc_obj_to_stats
, zfs_secpolicy_diff
);
5965 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH
,
5966 zfs_ioc_obj_to_path
, zfs_secpolicy_diff
);
5967 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE
,
5968 zfs_ioc_userspace_one
, zfs_secpolicy_userspace_one
);
5969 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY
,
5970 zfs_ioc_userspace_many
, zfs_secpolicy_userspace_many
);
5971 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND
,
5972 zfs_ioc_send
, zfs_secpolicy_send
);
5974 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP
, zfs_ioc_set_prop
,
5975 zfs_secpolicy_none
);
5976 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY
, zfs_ioc_destroy
,
5977 zfs_secpolicy_destroy
);
5978 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME
, zfs_ioc_rename
,
5979 zfs_secpolicy_rename
);
5980 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV
, zfs_ioc_recv
,
5981 zfs_secpolicy_recv
);
5982 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE
, zfs_ioc_promote
,
5983 zfs_secpolicy_promote
);
5984 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP
,
5985 zfs_ioc_inherit_prop
, zfs_secpolicy_inherit_prop
);
5986 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL
, zfs_ioc_set_fsacl
,
5987 zfs_secpolicy_set_fsacl
);
5989 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE
, zfs_ioc_share
,
5990 zfs_secpolicy_share
, POOL_CHECK_NONE
);
5991 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL
, zfs_ioc_smb_acl
,
5992 zfs_secpolicy_smb_acl
, POOL_CHECK_NONE
);
5993 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE
,
5994 zfs_ioc_userspace_upgrade
, zfs_secpolicy_userspace_upgrade
,
5995 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
5996 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT
,
5997 zfs_ioc_tmp_snapshot
, zfs_secpolicy_tmp_snapshot
,
5998 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
6003 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_NEXT
, zfs_ioc_events_next
,
6004 zfs_secpolicy_config
, NO_NAME
, B_FALSE
, POOL_CHECK_NONE
);
6005 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_CLEAR
, zfs_ioc_events_clear
,
6006 zfs_secpolicy_config
, NO_NAME
, B_FALSE
, POOL_CHECK_NONE
);
6007 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_SEEK
, zfs_ioc_events_seek
,
6008 zfs_secpolicy_config
, NO_NAME
, B_FALSE
, POOL_CHECK_NONE
);
6012 pool_status_check(const char *name
, zfs_ioc_namecheck_t type
,
6013 zfs_ioc_poolcheck_t check
)
6018 ASSERT(type
== POOL_NAME
|| type
== DATASET_NAME
);
6020 if (check
& POOL_CHECK_NONE
)
6023 error
= spa_open(name
, &spa
, FTAG
);
6025 if ((check
& POOL_CHECK_SUSPENDED
) && spa_suspended(spa
))
6026 error
= SET_ERROR(EAGAIN
);
6027 else if ((check
& POOL_CHECK_READONLY
) && !spa_writeable(spa
))
6028 error
= SET_ERROR(EROFS
);
6029 spa_close(spa
, FTAG
);
6035 zfsdev_get_state_impl(minor_t minor
, enum zfsdev_state_type which
)
6039 for (zs
= zfsdev_state_list
; zs
!= NULL
; zs
= zs
->zs_next
) {
6040 if (zs
->zs_minor
== minor
) {
6044 return (zs
->zs_onexit
);
6046 return (zs
->zs_zevent
);
6057 zfsdev_get_state(minor_t minor
, enum zfsdev_state_type which
)
6061 ptr
= zfsdev_get_state_impl(minor
, which
);
6067 zfsdev_getminor(struct file
*filp
, minor_t
*minorp
)
6069 zfsdev_state_t
*zs
, *fpd
;
6071 ASSERT(filp
!= NULL
);
6072 ASSERT(!MUTEX_HELD(&zfsdev_state_lock
));
6074 fpd
= filp
->private_data
;
6078 mutex_enter(&zfsdev_state_lock
);
6080 for (zs
= zfsdev_state_list
; zs
!= NULL
; zs
= zs
->zs_next
) {
6082 if (zs
->zs_minor
== -1)
6086 *minorp
= fpd
->zs_minor
;
6087 mutex_exit(&zfsdev_state_lock
);
6092 mutex_exit(&zfsdev_state_lock
);
6098 * Find a free minor number. The zfsdev_state_list is expected to
6099 * be short since it is only a list of currently open file handles.
6102 zfsdev_minor_alloc(void)
6104 static minor_t last_minor
= 0;
6107 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
6109 for (m
= last_minor
+ 1; m
!= last_minor
; m
++) {
6110 if (m
> ZFSDEV_MAX_MINOR
)
6112 if (zfsdev_get_state_impl(m
, ZST_ALL
) == NULL
) {
6122 zfsdev_state_init(struct file
*filp
)
6124 zfsdev_state_t
*zs
, *zsprev
= NULL
;
6126 boolean_t newzs
= B_FALSE
;
6128 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
6130 minor
= zfsdev_minor_alloc();
6132 return (SET_ERROR(ENXIO
));
6134 for (zs
= zfsdev_state_list
; zs
!= NULL
; zs
= zs
->zs_next
) {
6135 if (zs
->zs_minor
== -1)
6141 zs
= kmem_zalloc(sizeof (zfsdev_state_t
), KM_SLEEP
);
6146 filp
->private_data
= zs
;
6148 zfs_onexit_init((zfs_onexit_t
**)&zs
->zs_onexit
);
6149 zfs_zevent_init((zfs_zevent_t
**)&zs
->zs_zevent
);
6153 * In order to provide for lock-free concurrent read access
6154 * to the minor list in zfsdev_get_state_impl(), new entries
6155 * must be completely written before linking them into the
6156 * list whereas existing entries are already linked; the last
6157 * operation must be updating zs_minor (from -1 to the new
6161 zs
->zs_minor
= minor
;
6163 zsprev
->zs_next
= zs
;
6166 zs
->zs_minor
= minor
;
6173 zfsdev_state_destroy(struct file
*filp
)
6177 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
6178 ASSERT(filp
->private_data
!= NULL
);
6180 zs
= filp
->private_data
;
6182 zfs_onexit_destroy(zs
->zs_onexit
);
6183 zfs_zevent_destroy(zs
->zs_zevent
);
6189 zfsdev_open(struct inode
*ino
, struct file
*filp
)
6193 mutex_enter(&zfsdev_state_lock
);
6194 error
= zfsdev_state_init(filp
);
6195 mutex_exit(&zfsdev_state_lock
);
6201 zfsdev_release(struct inode
*ino
, struct file
*filp
)
6205 mutex_enter(&zfsdev_state_lock
);
6206 error
= zfsdev_state_destroy(filp
);
6207 mutex_exit(&zfsdev_state_lock
);
6213 zfsdev_ioctl(struct file
*filp
, unsigned cmd
, unsigned long arg
)
6217 int error
, rc
, flag
= 0;
6218 const zfs_ioc_vec_t
*vec
;
6219 char *saved_poolname
= NULL
;
6220 nvlist_t
*innvl
= NULL
;
6221 fstrans_cookie_t cookie
;
6223 vecnum
= cmd
- ZFS_IOC_FIRST
;
6224 if (vecnum
>= sizeof (zfs_ioc_vec
) / sizeof (zfs_ioc_vec
[0]))
6225 return (-SET_ERROR(EINVAL
));
6226 vec
= &zfs_ioc_vec
[vecnum
];
6229 * The registered ioctl list may be sparse, verify that either
6230 * a normal or legacy handler are registered.
6232 if (vec
->zvec_func
== NULL
&& vec
->zvec_legacy_func
== NULL
)
6233 return (-SET_ERROR(EINVAL
));
6235 zc
= kmem_zalloc(sizeof (zfs_cmd_t
), KM_SLEEP
);
6237 error
= ddi_copyin((void *)arg
, zc
, sizeof (zfs_cmd_t
), flag
);
6239 error
= SET_ERROR(EFAULT
);
6243 zc
->zc_iflags
= flag
& FKIOCTL
;
6244 if (zc
->zc_nvlist_src_size
> MAX_NVLIST_SRC_SIZE
) {
6246 * Make sure the user doesn't pass in an insane value for
6247 * zc_nvlist_src_size. We have to check, since we will end
6248 * up allocating that much memory inside of get_nvlist(). This
6249 * prevents a nefarious user from allocating tons of kernel
6252 * Also, we return EINVAL instead of ENOMEM here. The reason
6253 * being that returning ENOMEM from an ioctl() has a special
6254 * connotation; that the user's size value is too small and
6255 * needs to be expanded to hold the nvlist. See
6256 * zcmd_expand_dst_nvlist() for details.
6258 error
= SET_ERROR(EINVAL
); /* User's size too big */
6260 } else if (zc
->zc_nvlist_src_size
!= 0) {
6261 error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
6262 zc
->zc_iflags
, &innvl
);
6268 * Ensure that all pool/dataset names are valid before we pass down to
6271 zc
->zc_name
[sizeof (zc
->zc_name
) - 1] = '\0';
6272 switch (vec
->zvec_namecheck
) {
6274 if (pool_namecheck(zc
->zc_name
, NULL
, NULL
) != 0)
6275 error
= SET_ERROR(EINVAL
);
6277 error
= pool_status_check(zc
->zc_name
,
6278 vec
->zvec_namecheck
, vec
->zvec_pool_check
);
6282 if (dataset_namecheck(zc
->zc_name
, NULL
, NULL
) != 0)
6283 error
= SET_ERROR(EINVAL
);
6285 error
= pool_status_check(zc
->zc_name
,
6286 vec
->zvec_namecheck
, vec
->zvec_pool_check
);
6295 cookie
= spl_fstrans_mark();
6296 error
= vec
->zvec_secpolicy(zc
, innvl
, CRED());
6297 spl_fstrans_unmark(cookie
);
6303 /* legacy ioctls can modify zc_name */
6304 saved_poolname
= strdup(zc
->zc_name
);
6305 if (saved_poolname
== NULL
) {
6306 error
= SET_ERROR(ENOMEM
);
6309 saved_poolname
[strcspn(saved_poolname
, "/@#")] = '\0';
6312 if (vec
->zvec_func
!= NULL
) {
6316 nvlist_t
*lognv
= NULL
;
6318 ASSERT(vec
->zvec_legacy_func
== NULL
);
6321 * Add the innvl to the lognv before calling the func,
6322 * in case the func changes the innvl.
6324 if (vec
->zvec_allow_log
) {
6325 lognv
= fnvlist_alloc();
6326 fnvlist_add_string(lognv
, ZPOOL_HIST_IOCTL
,
6328 if (!nvlist_empty(innvl
)) {
6329 fnvlist_add_nvlist(lognv
, ZPOOL_HIST_INPUT_NVL
,
6334 outnvl
= fnvlist_alloc();
6335 cookie
= spl_fstrans_mark();
6336 error
= vec
->zvec_func(zc
->zc_name
, innvl
, outnvl
);
6337 spl_fstrans_unmark(cookie
);
6339 if (error
== 0 && vec
->zvec_allow_log
&&
6340 spa_open(zc
->zc_name
, &spa
, FTAG
) == 0) {
6341 if (!nvlist_empty(outnvl
)) {
6342 fnvlist_add_nvlist(lognv
, ZPOOL_HIST_OUTPUT_NVL
,
6345 (void) spa_history_log_nvl(spa
, lognv
);
6346 spa_close(spa
, FTAG
);
6348 fnvlist_free(lognv
);
6350 if (!nvlist_empty(outnvl
) || zc
->zc_nvlist_dst_size
!= 0) {
6352 if (vec
->zvec_smush_outnvlist
) {
6353 smusherror
= nvlist_smush(outnvl
,
6354 zc
->zc_nvlist_dst_size
);
6356 if (smusherror
== 0)
6357 puterror
= put_nvlist(zc
, outnvl
);
6363 nvlist_free(outnvl
);
6365 cookie
= spl_fstrans_mark();
6366 error
= vec
->zvec_legacy_func(zc
);
6367 spl_fstrans_unmark(cookie
);
6372 rc
= ddi_copyout(zc
, (void *)arg
, sizeof (zfs_cmd_t
), flag
);
6373 if (error
== 0 && rc
!= 0)
6374 error
= SET_ERROR(EFAULT
);
6375 if (error
== 0 && vec
->zvec_allow_log
) {
6376 char *s
= tsd_get(zfs_allow_log_key
);
6379 (void) tsd_set(zfs_allow_log_key
, saved_poolname
);
6381 if (saved_poolname
!= NULL
)
6382 strfree(saved_poolname
);
6385 kmem_free(zc
, sizeof (zfs_cmd_t
));
6389 #ifdef CONFIG_COMPAT
6391 zfsdev_compat_ioctl(struct file
*filp
, unsigned cmd
, unsigned long arg
)
6393 return (zfsdev_ioctl(filp
, cmd
, arg
));
6396 #define zfsdev_compat_ioctl NULL
6399 static const struct file_operations zfsdev_fops
= {
6400 .open
= zfsdev_open
,
6401 .release
= zfsdev_release
,
6402 .unlocked_ioctl
= zfsdev_ioctl
,
6403 .compat_ioctl
= zfsdev_compat_ioctl
,
6404 .owner
= THIS_MODULE
,
6407 static struct miscdevice zfs_misc
= {
6408 .minor
= MISC_DYNAMIC_MINOR
,
6410 .fops
= &zfsdev_fops
,
6418 mutex_init(&zfsdev_state_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
6419 zfsdev_state_list
= kmem_zalloc(sizeof (zfsdev_state_t
), KM_SLEEP
);
6420 zfsdev_state_list
->zs_minor
= -1;
6422 error
= misc_register(&zfs_misc
);
6424 printk(KERN_INFO
"ZFS: misc_register() failed %d\n", error
);
6434 zfsdev_state_t
*zs
, *zsprev
= NULL
;
6436 misc_deregister(&zfs_misc
);
6437 mutex_destroy(&zfsdev_state_lock
);
6439 for (zs
= zfsdev_state_list
; zs
!= NULL
; zs
= zs
->zs_next
) {
6441 kmem_free(zsprev
, sizeof (zfsdev_state_t
));
6445 kmem_free(zsprev
, sizeof (zfsdev_state_t
));
6449 zfs_allow_log_destroy(void *arg
)
6451 char *poolname
= arg
;
6453 if (poolname
!= NULL
)
6458 #define ZFS_DEBUG_STR " (DEBUG mode)"
6460 #define ZFS_DEBUG_STR ""
6468 error
= -vn_set_pwd("/");
6471 "ZFS: Warning unable to set pwd to '/': %d\n", error
);
6475 if ((error
= -zvol_init()) != 0)
6478 spa_init(FREAD
| FWRITE
);
6483 if ((error
= zfs_attach()) != 0)
6486 tsd_create(&zfs_fsyncer_key
, NULL
);
6487 tsd_create(&rrw_tsd_key
, rrw_tsd_destroy
);
6488 tsd_create(&zfs_allow_log_key
, zfs_allow_log_destroy
);
6490 printk(KERN_NOTICE
"ZFS: Loaded module v%s-%s%s, "
6491 "ZFS pool version %s, ZFS filesystem version %s\n",
6492 ZFS_META_VERSION
, ZFS_META_RELEASE
, ZFS_DEBUG_STR
,
6493 SPA_VERSION_STRING
, ZPL_VERSION_STRING
);
6494 #ifndef CONFIG_FS_POSIX_ACL
6495 printk(KERN_NOTICE
"ZFS: Posix ACLs disabled by kernel\n");
6496 #endif /* CONFIG_FS_POSIX_ACL */
6504 printk(KERN_NOTICE
"ZFS: Failed to Load ZFS Filesystem v%s-%s%s"
6505 ", rc = %d\n", ZFS_META_VERSION
, ZFS_META_RELEASE
,
6506 ZFS_DEBUG_STR
, error
);
6519 tsd_destroy(&zfs_fsyncer_key
);
6520 tsd_destroy(&rrw_tsd_key
);
6521 tsd_destroy(&zfs_allow_log_key
);
6523 printk(KERN_NOTICE
"ZFS: Unloaded module v%s-%s%s\n",
6524 ZFS_META_VERSION
, ZFS_META_RELEASE
, ZFS_DEBUG_STR
);
6531 MODULE_DESCRIPTION("ZFS");
6532 MODULE_AUTHOR(ZFS_META_AUTHOR
);
6533 MODULE_LICENSE(ZFS_META_LICENSE
);
6534 MODULE_VERSION(ZFS_META_VERSION
"-" ZFS_META_RELEASE
);
6535 #endif /* HAVE_SPL */