4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Portions Copyright 2011 Martin Matuska
25 * Portions Copyright 2012 Pawel Jakub Dawidek <pawel@dawidek.net>
26 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
27 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
28 * Copyright (c) 2014, Joyent, Inc. All rights reserved.
29 * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
30 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
31 * Copyright (c) 2013 Steven Hartland. All rights reserved.
32 * Copyright (c) 2014, Nexenta Systems, Inc. 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>
189 #include <linux/miscdevice.h>
191 #include "zfs_namecheck.h"
192 #include "zfs_prop.h"
193 #include "zfs_deleg.h"
194 #include "zfs_comutil.h"
196 kmutex_t zfsdev_state_lock
;
197 zfsdev_state_t
*zfsdev_state_list
;
199 extern void zfs_init(void);
200 extern void zfs_fini(void);
202 uint_t zfs_fsyncer_key
;
203 extern uint_t rrw_tsd_key
;
204 static uint_t zfs_allow_log_key
;
206 typedef int zfs_ioc_legacy_func_t(zfs_cmd_t
*);
207 typedef int zfs_ioc_func_t(const char *, nvlist_t
*, nvlist_t
*);
208 typedef int zfs_secpolicy_func_t(zfs_cmd_t
*, nvlist_t
*, cred_t
*);
214 } zfs_ioc_namecheck_t
;
217 POOL_CHECK_NONE
= 1 << 0,
218 POOL_CHECK_SUSPENDED
= 1 << 1,
219 POOL_CHECK_READONLY
= 1 << 2,
220 } zfs_ioc_poolcheck_t
;
222 typedef struct zfs_ioc_vec
{
223 zfs_ioc_legacy_func_t
*zvec_legacy_func
;
224 zfs_ioc_func_t
*zvec_func
;
225 zfs_secpolicy_func_t
*zvec_secpolicy
;
226 zfs_ioc_namecheck_t zvec_namecheck
;
227 boolean_t zvec_allow_log
;
228 zfs_ioc_poolcheck_t zvec_pool_check
;
229 boolean_t zvec_smush_outnvlist
;
230 const char *zvec_name
;
233 /* This array is indexed by zfs_userquota_prop_t */
234 static const char *userquota_perms
[] = {
235 ZFS_DELEG_PERM_USERUSED
,
236 ZFS_DELEG_PERM_USERQUOTA
,
237 ZFS_DELEG_PERM_GROUPUSED
,
238 ZFS_DELEG_PERM_GROUPQUOTA
,
241 static int zfs_ioc_userspace_upgrade(zfs_cmd_t
*zc
);
242 static int zfs_check_settable(const char *name
, nvpair_t
*property
,
244 static int zfs_check_clearable(char *dataset
, nvlist_t
*props
,
246 static int zfs_fill_zplprops_root(uint64_t, nvlist_t
*, nvlist_t
*,
248 int zfs_set_prop_nvlist(const char *, zprop_source_t
, nvlist_t
*, nvlist_t
*);
249 static int get_nvlist(uint64_t nvl
, uint64_t size
, int iflag
, nvlist_t
**nvp
);
252 history_str_free(char *buf
)
254 kmem_free(buf
, HIS_MAX_RECORD_LEN
);
258 history_str_get(zfs_cmd_t
*zc
)
262 if (zc
->zc_history
== 0)
265 buf
= kmem_alloc(HIS_MAX_RECORD_LEN
, KM_SLEEP
);
266 if (copyinstr((void *)(uintptr_t)zc
->zc_history
,
267 buf
, HIS_MAX_RECORD_LEN
, NULL
) != 0) {
268 history_str_free(buf
);
272 buf
[HIS_MAX_RECORD_LEN
-1] = '\0';
278 * Check to see if the named dataset is currently defined as bootable
281 zfs_is_bootfs(const char *name
)
285 if (dmu_objset_hold(name
, FTAG
, &os
) == 0) {
287 ret
= (dmu_objset_id(os
) == spa_bootfs(dmu_objset_spa(os
)));
288 dmu_objset_rele(os
, FTAG
);
295 * Return non-zero if the spa version is less than requested version.
298 zfs_earlier_version(const char *name
, int version
)
302 if (spa_open(name
, &spa
, FTAG
) == 0) {
303 if (spa_version(spa
) < version
) {
304 spa_close(spa
, FTAG
);
307 spa_close(spa
, FTAG
);
313 * Return TRUE if the ZPL version is less than requested version.
316 zpl_earlier_version(const char *name
, int version
)
319 boolean_t rc
= B_TRUE
;
321 if (dmu_objset_hold(name
, FTAG
, &os
) == 0) {
324 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
325 dmu_objset_rele(os
, FTAG
);
328 /* XXX reading from non-owned objset */
329 if (zfs_get_zplprop(os
, ZFS_PROP_VERSION
, &zplversion
) == 0)
330 rc
= zplversion
< version
;
331 dmu_objset_rele(os
, FTAG
);
337 zfs_log_history(zfs_cmd_t
*zc
)
342 if ((buf
= history_str_get(zc
)) == NULL
)
345 if (spa_open(zc
->zc_name
, &spa
, FTAG
) == 0) {
346 if (spa_version(spa
) >= SPA_VERSION_ZPOOL_HISTORY
)
347 (void) spa_history_log(spa
, buf
);
348 spa_close(spa
, FTAG
);
350 history_str_free(buf
);
354 * Policy for top-level read operations (list pools). Requires no privileges,
355 * and can be used in the local zone, as there is no associated dataset.
359 zfs_secpolicy_none(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
365 * Policy for dataset read operations (list children, get statistics). Requires
366 * no privileges, but must be visible in the local zone.
370 zfs_secpolicy_read(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
372 if (INGLOBALZONE(curproc
) ||
373 zone_dataset_visible(zc
->zc_name
, NULL
))
376 return (SET_ERROR(ENOENT
));
380 zfs_dozonecheck_impl(const char *dataset
, uint64_t zoned
, cred_t
*cr
)
385 * The dataset must be visible by this zone -- check this first
386 * so they don't see EPERM on something they shouldn't know about.
388 if (!INGLOBALZONE(curproc
) &&
389 !zone_dataset_visible(dataset
, &writable
))
390 return (SET_ERROR(ENOENT
));
392 if (INGLOBALZONE(curproc
)) {
394 * If the fs is zoned, only root can access it from the
397 if (secpolicy_zfs(cr
) && zoned
)
398 return (SET_ERROR(EPERM
));
401 * If we are in a local zone, the 'zoned' property must be set.
404 return (SET_ERROR(EPERM
));
406 /* must be writable by this zone */
408 return (SET_ERROR(EPERM
));
414 zfs_dozonecheck(const char *dataset
, cred_t
*cr
)
418 if (dsl_prop_get_integer(dataset
, "zoned", &zoned
, NULL
))
419 return (SET_ERROR(ENOENT
));
421 return (zfs_dozonecheck_impl(dataset
, zoned
, cr
));
425 zfs_dozonecheck_ds(const char *dataset
, dsl_dataset_t
*ds
, cred_t
*cr
)
429 if (dsl_prop_get_int_ds(ds
, "zoned", &zoned
))
430 return (SET_ERROR(ENOENT
));
432 return (zfs_dozonecheck_impl(dataset
, zoned
, cr
));
436 zfs_secpolicy_write_perms_ds(const char *name
, dsl_dataset_t
*ds
,
437 const char *perm
, cred_t
*cr
)
441 error
= zfs_dozonecheck_ds(name
, ds
, cr
);
443 error
= secpolicy_zfs(cr
);
445 error
= dsl_deleg_access_impl(ds
, perm
, cr
);
451 zfs_secpolicy_write_perms(const char *name
, const char *perm
, cred_t
*cr
)
457 error
= dsl_pool_hold(name
, FTAG
, &dp
);
461 error
= dsl_dataset_hold(dp
, name
, FTAG
, &ds
);
463 dsl_pool_rele(dp
, FTAG
);
467 error
= zfs_secpolicy_write_perms_ds(name
, ds
, perm
, cr
);
469 dsl_dataset_rele(ds
, FTAG
);
470 dsl_pool_rele(dp
, FTAG
);
475 * Policy for setting the security label property.
477 * Returns 0 for success, non-zero for access and other errors.
480 zfs_set_slabel_policy(const char *name
, char *strval
, cred_t
*cr
)
483 char ds_hexsl
[MAXNAMELEN
];
484 bslabel_t ds_sl
, new_sl
;
485 boolean_t new_default
= FALSE
;
487 int needed_priv
= -1;
490 /* First get the existing dataset label. */
491 error
= dsl_prop_get(name
, zfs_prop_to_name(ZFS_PROP_MLSLABEL
),
492 1, sizeof (ds_hexsl
), &ds_hexsl
, NULL
);
494 return (SET_ERROR(EPERM
));
496 if (strcasecmp(strval
, ZFS_MLSLABEL_DEFAULT
) == 0)
499 /* The label must be translatable */
500 if (!new_default
&& (hexstr_to_label(strval
, &new_sl
) != 0))
501 return (SET_ERROR(EINVAL
));
504 * In a non-global zone, disallow attempts to set a label that
505 * doesn't match that of the zone; otherwise no other checks
508 if (!INGLOBALZONE(curproc
)) {
509 if (new_default
|| !blequal(&new_sl
, CR_SL(CRED())))
510 return (SET_ERROR(EPERM
));
515 * For global-zone datasets (i.e., those whose zoned property is
516 * "off", verify that the specified new label is valid for the
519 if (dsl_prop_get_integer(name
,
520 zfs_prop_to_name(ZFS_PROP_ZONED
), &zoned
, NULL
))
521 return (SET_ERROR(EPERM
));
523 if (zfs_check_global_label(name
, strval
) != 0)
524 return (SET_ERROR(EPERM
));
528 * If the existing dataset label is nondefault, check if the
529 * dataset is mounted (label cannot be changed while mounted).
530 * Get the zfs_sb_t; if there isn't one, then the dataset isn't
531 * mounted (or isn't a dataset, doesn't exist, ...).
533 if (strcasecmp(ds_hexsl
, ZFS_MLSLABEL_DEFAULT
) != 0) {
535 static char *setsl_tag
= "setsl_tag";
538 * Try to own the dataset; abort if there is any error,
539 * (e.g., already mounted, in use, or other error).
541 error
= dmu_objset_own(name
, DMU_OST_ZFS
, B_TRUE
,
544 return (SET_ERROR(EPERM
));
546 dmu_objset_disown(os
, setsl_tag
);
549 needed_priv
= PRIV_FILE_DOWNGRADE_SL
;
553 if (hexstr_to_label(strval
, &new_sl
) != 0)
554 return (SET_ERROR(EPERM
));
556 if (blstrictdom(&ds_sl
, &new_sl
))
557 needed_priv
= PRIV_FILE_DOWNGRADE_SL
;
558 else if (blstrictdom(&new_sl
, &ds_sl
))
559 needed_priv
= PRIV_FILE_UPGRADE_SL
;
561 /* dataset currently has a default label */
563 needed_priv
= PRIV_FILE_UPGRADE_SL
;
567 if (needed_priv
!= -1)
568 return (PRIV_POLICY(cr
, needed_priv
, B_FALSE
, EPERM
, NULL
));
572 #endif /* HAVE_MLSLABEL */
576 zfs_secpolicy_setprop(const char *dsname
, zfs_prop_t prop
, nvpair_t
*propval
,
582 * Check permissions for special properties.
589 * Disallow setting of 'zoned' from within a local zone.
591 if (!INGLOBALZONE(curproc
))
592 return (SET_ERROR(EPERM
));
596 case ZFS_PROP_FILESYSTEM_LIMIT
:
597 case ZFS_PROP_SNAPSHOT_LIMIT
:
598 if (!INGLOBALZONE(curproc
)) {
600 char setpoint
[MAXNAMELEN
];
602 * Unprivileged users are allowed to modify the
603 * limit on things *under* (ie. contained by)
604 * the thing they own.
606 if (dsl_prop_get_integer(dsname
, "zoned", &zoned
,
608 return (SET_ERROR(EPERM
));
609 if (!zoned
|| strlen(dsname
) <= strlen(setpoint
))
610 return (SET_ERROR(EPERM
));
614 case ZFS_PROP_MLSLABEL
:
615 if (!is_system_labeled())
616 return (SET_ERROR(EPERM
));
618 if (nvpair_value_string(propval
, &strval
) == 0) {
621 err
= zfs_set_slabel_policy(dsname
, strval
, CRED());
628 return (zfs_secpolicy_write_perms(dsname
, zfs_prop_to_name(prop
), cr
));
633 zfs_secpolicy_set_fsacl(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
637 error
= zfs_dozonecheck(zc
->zc_name
, cr
);
642 * permission to set permissions will be evaluated later in
643 * dsl_deleg_can_allow()
650 zfs_secpolicy_rollback(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
652 return (zfs_secpolicy_write_perms(zc
->zc_name
,
653 ZFS_DELEG_PERM_ROLLBACK
, cr
));
658 zfs_secpolicy_send(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
666 * Generate the current snapshot name from the given objsetid, then
667 * use that name for the secpolicy/zone checks.
669 cp
= strchr(zc
->zc_name
, '@');
671 return (SET_ERROR(EINVAL
));
672 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
676 error
= dsl_dataset_hold_obj(dp
, zc
->zc_sendobj
, FTAG
, &ds
);
678 dsl_pool_rele(dp
, FTAG
);
682 dsl_dataset_name(ds
, zc
->zc_name
);
684 error
= zfs_secpolicy_write_perms_ds(zc
->zc_name
, ds
,
685 ZFS_DELEG_PERM_SEND
, cr
);
686 dsl_dataset_rele(ds
, FTAG
);
687 dsl_pool_rele(dp
, FTAG
);
694 zfs_secpolicy_send_new(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
696 return (zfs_secpolicy_write_perms(zc
->zc_name
,
697 ZFS_DELEG_PERM_SEND
, cr
));
700 #ifdef HAVE_SMB_SHARE
703 zfs_secpolicy_deleg_share(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
708 if ((error
= lookupname(zc
->zc_value
, UIO_SYSSPACE
,
709 NO_FOLLOW
, NULL
, &vp
)) != 0)
712 /* Now make sure mntpnt and dataset are ZFS */
714 if (vp
->v_vfsp
->vfs_fstype
!= zfsfstype
||
715 (strcmp((char *)refstr_value(vp
->v_vfsp
->vfs_resource
),
716 zc
->zc_name
) != 0)) {
718 return (SET_ERROR(EPERM
));
722 return (dsl_deleg_access(zc
->zc_name
,
723 ZFS_DELEG_PERM_SHARE
, cr
));
725 #endif /* HAVE_SMB_SHARE */
728 zfs_secpolicy_share(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
730 #ifdef HAVE_SMB_SHARE
731 if (!INGLOBALZONE(curproc
))
732 return (SET_ERROR(EPERM
));
734 if (secpolicy_nfs(cr
) == 0) {
737 return (zfs_secpolicy_deleg_share(zc
, innvl
, cr
));
740 return (SET_ERROR(ENOTSUP
));
741 #endif /* HAVE_SMB_SHARE */
745 zfs_secpolicy_smb_acl(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
747 #ifdef HAVE_SMB_SHARE
748 if (!INGLOBALZONE(curproc
))
749 return (SET_ERROR(EPERM
));
751 if (secpolicy_smb(cr
) == 0) {
754 return (zfs_secpolicy_deleg_share(zc
, innvl
, cr
));
757 return (SET_ERROR(ENOTSUP
));
758 #endif /* HAVE_SMB_SHARE */
762 zfs_get_parent(const char *datasetname
, char *parent
, int parentsize
)
767 * Remove the @bla or /bla from the end of the name to get the parent.
769 (void) strncpy(parent
, datasetname
, parentsize
);
770 cp
= strrchr(parent
, '@');
774 cp
= strrchr(parent
, '/');
776 return (SET_ERROR(ENOENT
));
784 zfs_secpolicy_destroy_perms(const char *name
, cred_t
*cr
)
788 if ((error
= zfs_secpolicy_write_perms(name
,
789 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
792 return (zfs_secpolicy_write_perms(name
, ZFS_DELEG_PERM_DESTROY
, cr
));
797 zfs_secpolicy_destroy(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
799 return (zfs_secpolicy_destroy_perms(zc
->zc_name
, cr
));
803 * Destroying snapshots with delegated permissions requires
804 * descendant mount and destroy permissions.
808 zfs_secpolicy_destroy_snaps(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
811 nvpair_t
*pair
, *nextpair
;
814 if (nvlist_lookup_nvlist(innvl
, "snaps", &snaps
) != 0)
815 return (SET_ERROR(EINVAL
));
816 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
818 nextpair
= nvlist_next_nvpair(snaps
, pair
);
819 error
= zfs_secpolicy_destroy_perms(nvpair_name(pair
), cr
);
820 if (error
== ENOENT
) {
822 * Ignore any snapshots that don't exist (we consider
823 * them "already destroyed"). Remove the name from the
824 * nvl here in case the snapshot is created between
825 * now and when we try to destroy it (in which case
826 * we don't want to destroy it since we haven't
827 * checked for permission).
829 fnvlist_remove_nvpair(snaps
, pair
);
840 zfs_secpolicy_rename_perms(const char *from
, const char *to
, cred_t
*cr
)
842 char parentname
[MAXNAMELEN
];
845 if ((error
= zfs_secpolicy_write_perms(from
,
846 ZFS_DELEG_PERM_RENAME
, cr
)) != 0)
849 if ((error
= zfs_secpolicy_write_perms(from
,
850 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
853 if ((error
= zfs_get_parent(to
, parentname
,
854 sizeof (parentname
))) != 0)
857 if ((error
= zfs_secpolicy_write_perms(parentname
,
858 ZFS_DELEG_PERM_CREATE
, cr
)) != 0)
861 if ((error
= zfs_secpolicy_write_perms(parentname
,
862 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
870 zfs_secpolicy_rename(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
872 return (zfs_secpolicy_rename_perms(zc
->zc_name
, zc
->zc_value
, cr
));
877 zfs_secpolicy_promote(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
880 dsl_dataset_t
*clone
;
883 error
= zfs_secpolicy_write_perms(zc
->zc_name
,
884 ZFS_DELEG_PERM_PROMOTE
, cr
);
888 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
892 error
= dsl_dataset_hold(dp
, zc
->zc_name
, FTAG
, &clone
);
895 char parentname
[MAXNAMELEN
];
896 dsl_dataset_t
*origin
= NULL
;
900 error
= dsl_dataset_hold_obj(dd
->dd_pool
,
901 dsl_dir_phys(dd
)->dd_origin_obj
, FTAG
, &origin
);
903 dsl_dataset_rele(clone
, FTAG
);
904 dsl_pool_rele(dp
, FTAG
);
908 error
= zfs_secpolicy_write_perms_ds(zc
->zc_name
, clone
,
909 ZFS_DELEG_PERM_MOUNT
, cr
);
911 dsl_dataset_name(origin
, parentname
);
913 error
= zfs_secpolicy_write_perms_ds(parentname
, origin
,
914 ZFS_DELEG_PERM_PROMOTE
, cr
);
916 dsl_dataset_rele(clone
, FTAG
);
917 dsl_dataset_rele(origin
, FTAG
);
919 dsl_pool_rele(dp
, FTAG
);
925 zfs_secpolicy_recv(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
929 if ((error
= zfs_secpolicy_write_perms(zc
->zc_name
,
930 ZFS_DELEG_PERM_RECEIVE
, cr
)) != 0)
933 if ((error
= zfs_secpolicy_write_perms(zc
->zc_name
,
934 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
937 return (zfs_secpolicy_write_perms(zc
->zc_name
,
938 ZFS_DELEG_PERM_CREATE
, cr
));
942 zfs_secpolicy_snapshot_perms(const char *name
, cred_t
*cr
)
944 return (zfs_secpolicy_write_perms(name
,
945 ZFS_DELEG_PERM_SNAPSHOT
, cr
));
949 * Check for permission to create each snapshot in the nvlist.
953 zfs_secpolicy_snapshot(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
959 if (nvlist_lookup_nvlist(innvl
, "snaps", &snaps
) != 0)
960 return (SET_ERROR(EINVAL
));
961 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
962 pair
= nvlist_next_nvpair(snaps
, pair
)) {
963 char *name
= nvpair_name(pair
);
964 char *atp
= strchr(name
, '@');
967 error
= SET_ERROR(EINVAL
);
971 error
= zfs_secpolicy_snapshot_perms(name
, cr
);
980 * Check for permission to create each snapshot in the nvlist.
984 zfs_secpolicy_bookmark(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
989 for (pair
= nvlist_next_nvpair(innvl
, NULL
);
990 pair
!= NULL
; pair
= nvlist_next_nvpair(innvl
, pair
)) {
991 char *name
= nvpair_name(pair
);
992 char *hashp
= strchr(name
, '#');
995 error
= SET_ERROR(EINVAL
);
999 error
= zfs_secpolicy_write_perms(name
,
1000 ZFS_DELEG_PERM_BOOKMARK
, cr
);
1010 zfs_secpolicy_destroy_bookmarks(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1012 nvpair_t
*pair
, *nextpair
;
1015 for (pair
= nvlist_next_nvpair(innvl
, NULL
); pair
!= NULL
;
1017 char *name
= nvpair_name(pair
);
1018 char *hashp
= strchr(name
, '#');
1019 nextpair
= nvlist_next_nvpair(innvl
, pair
);
1021 if (hashp
== NULL
) {
1022 error
= SET_ERROR(EINVAL
);
1027 error
= zfs_secpolicy_write_perms(name
,
1028 ZFS_DELEG_PERM_DESTROY
, cr
);
1030 if (error
== ENOENT
) {
1032 * Ignore any filesystems that don't exist (we consider
1033 * their bookmarks "already destroyed"). Remove
1034 * the name from the nvl here in case the filesystem
1035 * is created between now and when we try to destroy
1036 * the bookmark (in which case we don't want to
1037 * destroy it since we haven't checked for permission).
1039 fnvlist_remove_nvpair(innvl
, pair
);
1051 zfs_secpolicy_log_history(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1054 * Even root must have a proper TSD so that we know what pool
1057 if (tsd_get(zfs_allow_log_key
) == NULL
)
1058 return (SET_ERROR(EPERM
));
1063 zfs_secpolicy_create_clone(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1065 char parentname
[MAXNAMELEN
];
1069 if ((error
= zfs_get_parent(zc
->zc_name
, parentname
,
1070 sizeof (parentname
))) != 0)
1073 if (nvlist_lookup_string(innvl
, "origin", &origin
) == 0 &&
1074 (error
= zfs_secpolicy_write_perms(origin
,
1075 ZFS_DELEG_PERM_CLONE
, cr
)) != 0)
1078 if ((error
= zfs_secpolicy_write_perms(parentname
,
1079 ZFS_DELEG_PERM_CREATE
, cr
)) != 0)
1082 return (zfs_secpolicy_write_perms(parentname
,
1083 ZFS_DELEG_PERM_MOUNT
, cr
));
1087 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
1088 * SYS_CONFIG privilege, which is not available in a local zone.
1092 zfs_secpolicy_config(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1094 if (secpolicy_sys_config(cr
, B_FALSE
) != 0)
1095 return (SET_ERROR(EPERM
));
1101 * Policy for object to name lookups.
1105 zfs_secpolicy_diff(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1109 if ((error
= secpolicy_sys_config(cr
, B_FALSE
)) == 0)
1112 error
= zfs_secpolicy_write_perms(zc
->zc_name
, ZFS_DELEG_PERM_DIFF
, cr
);
1117 * Policy for fault injection. Requires all privileges.
1121 zfs_secpolicy_inject(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1123 return (secpolicy_zinject(cr
));
1128 zfs_secpolicy_inherit_prop(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1130 zfs_prop_t prop
= zfs_name_to_prop(zc
->zc_value
);
1132 if (prop
== ZPROP_INVAL
) {
1133 if (!zfs_prop_user(zc
->zc_value
))
1134 return (SET_ERROR(EINVAL
));
1135 return (zfs_secpolicy_write_perms(zc
->zc_name
,
1136 ZFS_DELEG_PERM_USERPROP
, cr
));
1138 return (zfs_secpolicy_setprop(zc
->zc_name
, prop
,
1144 zfs_secpolicy_userspace_one(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1146 int err
= zfs_secpolicy_read(zc
, innvl
, cr
);
1150 if (zc
->zc_objset_type
>= ZFS_NUM_USERQUOTA_PROPS
)
1151 return (SET_ERROR(EINVAL
));
1153 if (zc
->zc_value
[0] == 0) {
1155 * They are asking about a posix uid/gid. If it's
1156 * themself, allow it.
1158 if (zc
->zc_objset_type
== ZFS_PROP_USERUSED
||
1159 zc
->zc_objset_type
== ZFS_PROP_USERQUOTA
) {
1160 if (zc
->zc_guid
== crgetuid(cr
))
1163 if (groupmember(zc
->zc_guid
, cr
))
1168 return (zfs_secpolicy_write_perms(zc
->zc_name
,
1169 userquota_perms
[zc
->zc_objset_type
], cr
));
1173 zfs_secpolicy_userspace_many(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1175 int err
= zfs_secpolicy_read(zc
, innvl
, cr
);
1179 if (zc
->zc_objset_type
>= ZFS_NUM_USERQUOTA_PROPS
)
1180 return (SET_ERROR(EINVAL
));
1182 return (zfs_secpolicy_write_perms(zc
->zc_name
,
1183 userquota_perms
[zc
->zc_objset_type
], cr
));
1188 zfs_secpolicy_userspace_upgrade(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1190 return (zfs_secpolicy_setprop(zc
->zc_name
, ZFS_PROP_VERSION
,
1196 zfs_secpolicy_hold(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1202 error
= nvlist_lookup_nvlist(innvl
, "holds", &holds
);
1204 return (SET_ERROR(EINVAL
));
1206 for (pair
= nvlist_next_nvpair(holds
, NULL
); pair
!= NULL
;
1207 pair
= nvlist_next_nvpair(holds
, pair
)) {
1208 char fsname
[MAXNAMELEN
];
1209 error
= dmu_fsname(nvpair_name(pair
), fsname
);
1212 error
= zfs_secpolicy_write_perms(fsname
,
1213 ZFS_DELEG_PERM_HOLD
, cr
);
1222 zfs_secpolicy_release(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1227 for (pair
= nvlist_next_nvpair(innvl
, NULL
); pair
!= NULL
;
1228 pair
= nvlist_next_nvpair(innvl
, pair
)) {
1229 char fsname
[MAXNAMELEN
];
1230 error
= dmu_fsname(nvpair_name(pair
), fsname
);
1233 error
= zfs_secpolicy_write_perms(fsname
,
1234 ZFS_DELEG_PERM_RELEASE
, cr
);
1242 * Policy for allowing temporary snapshots to be taken or released
1245 zfs_secpolicy_tmp_snapshot(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1248 * A temporary snapshot is the same as a snapshot,
1249 * hold, destroy and release all rolled into one.
1250 * Delegated diff alone is sufficient that we allow this.
1254 if ((error
= zfs_secpolicy_write_perms(zc
->zc_name
,
1255 ZFS_DELEG_PERM_DIFF
, cr
)) == 0)
1258 error
= zfs_secpolicy_snapshot_perms(zc
->zc_name
, cr
);
1260 error
= zfs_secpolicy_hold(zc
, innvl
, cr
);
1262 error
= zfs_secpolicy_release(zc
, innvl
, cr
);
1264 error
= zfs_secpolicy_destroy(zc
, innvl
, cr
);
1269 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1272 get_nvlist(uint64_t nvl
, uint64_t size
, int iflag
, nvlist_t
**nvp
)
1276 nvlist_t
*list
= NULL
;
1279 * Read in and unpack the user-supplied nvlist.
1282 return (SET_ERROR(EINVAL
));
1284 packed
= vmem_alloc(size
, KM_SLEEP
);
1286 if ((error
= ddi_copyin((void *)(uintptr_t)nvl
, packed
, size
,
1288 vmem_free(packed
, size
);
1289 return (SET_ERROR(EFAULT
));
1292 if ((error
= nvlist_unpack(packed
, size
, &list
, 0)) != 0) {
1293 vmem_free(packed
, size
);
1297 vmem_free(packed
, size
);
1304 * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
1305 * Entries will be removed from the end of the nvlist, and one int32 entry
1306 * named "N_MORE_ERRORS" will be added indicating how many entries were
1310 nvlist_smush(nvlist_t
*errors
, size_t max
)
1314 size
= fnvlist_size(errors
);
1317 nvpair_t
*more_errors
;
1321 return (SET_ERROR(ENOMEM
));
1323 fnvlist_add_int32(errors
, ZPROP_N_MORE_ERRORS
, 0);
1324 more_errors
= nvlist_prev_nvpair(errors
, NULL
);
1327 nvpair_t
*pair
= nvlist_prev_nvpair(errors
,
1329 fnvlist_remove_nvpair(errors
, pair
);
1331 size
= fnvlist_size(errors
);
1332 } while (size
> max
);
1334 fnvlist_remove_nvpair(errors
, more_errors
);
1335 fnvlist_add_int32(errors
, ZPROP_N_MORE_ERRORS
, n
);
1336 ASSERT3U(fnvlist_size(errors
), <=, max
);
1343 put_nvlist(zfs_cmd_t
*zc
, nvlist_t
*nvl
)
1345 char *packed
= NULL
;
1349 size
= fnvlist_size(nvl
);
1351 if (size
> zc
->zc_nvlist_dst_size
) {
1352 error
= SET_ERROR(ENOMEM
);
1354 packed
= fnvlist_pack(nvl
, &size
);
1355 if (ddi_copyout(packed
, (void *)(uintptr_t)zc
->zc_nvlist_dst
,
1356 size
, zc
->zc_iflags
) != 0)
1357 error
= SET_ERROR(EFAULT
);
1358 fnvlist_pack_free(packed
, size
);
1361 zc
->zc_nvlist_dst_size
= size
;
1362 zc
->zc_nvlist_dst_filled
= B_TRUE
;
1367 get_zfs_sb(const char *dsname
, zfs_sb_t
**zsbp
)
1372 error
= dmu_objset_hold(dsname
, FTAG
, &os
);
1375 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
1376 dmu_objset_rele(os
, FTAG
);
1377 return (SET_ERROR(EINVAL
));
1380 mutex_enter(&os
->os_user_ptr_lock
);
1381 *zsbp
= dmu_objset_get_user(os
);
1382 if (*zsbp
&& (*zsbp
)->z_sb
) {
1383 atomic_inc(&((*zsbp
)->z_sb
->s_active
));
1385 error
= SET_ERROR(ESRCH
);
1387 mutex_exit(&os
->os_user_ptr_lock
);
1388 dmu_objset_rele(os
, FTAG
);
1393 * Find a zfs_sb_t for a mounted filesystem, or create our own, in which
1394 * case its z_sb will be NULL, and it will be opened as the owner.
1395 * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1396 * which prevents all inode ops from running.
1399 zfs_sb_hold(const char *name
, void *tag
, zfs_sb_t
**zsbp
, boolean_t writer
)
1403 if (get_zfs_sb(name
, zsbp
) != 0)
1404 error
= zfs_sb_create(name
, NULL
, zsbp
);
1406 rrm_enter(&(*zsbp
)->z_teardown_lock
, (writer
) ? RW_WRITER
:
1408 if ((*zsbp
)->z_unmounted
) {
1410 * XXX we could probably try again, since the unmounting
1411 * thread should be just about to disassociate the
1412 * objset from the zsb.
1414 rrm_exit(&(*zsbp
)->z_teardown_lock
, tag
);
1415 return (SET_ERROR(EBUSY
));
1422 zfs_sb_rele(zfs_sb_t
*zsb
, void *tag
)
1424 rrm_exit(&zsb
->z_teardown_lock
, tag
);
1427 deactivate_super(zsb
->z_sb
);
1429 dmu_objset_disown(zsb
->z_os
, zsb
);
1435 zfs_ioc_pool_create(zfs_cmd_t
*zc
)
1438 nvlist_t
*config
, *props
= NULL
;
1439 nvlist_t
*rootprops
= NULL
;
1440 nvlist_t
*zplprops
= NULL
;
1442 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1443 zc
->zc_iflags
, &config
)))
1446 if (zc
->zc_nvlist_src_size
!= 0 && (error
=
1447 get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1448 zc
->zc_iflags
, &props
))) {
1449 nvlist_free(config
);
1454 nvlist_t
*nvl
= NULL
;
1455 uint64_t version
= SPA_VERSION
;
1457 (void) nvlist_lookup_uint64(props
,
1458 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
);
1459 if (!SPA_VERSION_IS_SUPPORTED(version
)) {
1460 error
= SET_ERROR(EINVAL
);
1461 goto pool_props_bad
;
1463 (void) nvlist_lookup_nvlist(props
, ZPOOL_ROOTFS_PROPS
, &nvl
);
1465 error
= nvlist_dup(nvl
, &rootprops
, KM_SLEEP
);
1467 nvlist_free(config
);
1471 (void) nvlist_remove_all(props
, ZPOOL_ROOTFS_PROPS
);
1473 VERIFY(nvlist_alloc(&zplprops
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1474 error
= zfs_fill_zplprops_root(version
, rootprops
,
1477 goto pool_props_bad
;
1480 error
= spa_create(zc
->zc_name
, config
, props
, zplprops
);
1483 * Set the remaining root properties
1485 if (!error
&& (error
= zfs_set_prop_nvlist(zc
->zc_name
,
1486 ZPROP_SRC_LOCAL
, rootprops
, NULL
)) != 0)
1487 (void) spa_destroy(zc
->zc_name
);
1490 nvlist_free(rootprops
);
1491 nvlist_free(zplprops
);
1492 nvlist_free(config
);
1499 zfs_ioc_pool_destroy(zfs_cmd_t
*zc
)
1502 zfs_log_history(zc
);
1503 error
= spa_destroy(zc
->zc_name
);
1509 zfs_ioc_pool_import(zfs_cmd_t
*zc
)
1511 nvlist_t
*config
, *props
= NULL
;
1515 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1516 zc
->zc_iflags
, &config
)) != 0)
1519 if (zc
->zc_nvlist_src_size
!= 0 && (error
=
1520 get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1521 zc
->zc_iflags
, &props
))) {
1522 nvlist_free(config
);
1526 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &guid
) != 0 ||
1527 guid
!= zc
->zc_guid
)
1528 error
= SET_ERROR(EINVAL
);
1530 error
= spa_import(zc
->zc_name
, config
, props
, zc
->zc_cookie
);
1532 if (zc
->zc_nvlist_dst
!= 0) {
1535 if ((err
= put_nvlist(zc
, config
)) != 0)
1539 nvlist_free(config
);
1548 zfs_ioc_pool_export(zfs_cmd_t
*zc
)
1551 boolean_t force
= (boolean_t
)zc
->zc_cookie
;
1552 boolean_t hardforce
= (boolean_t
)zc
->zc_guid
;
1554 zfs_log_history(zc
);
1555 error
= spa_export(zc
->zc_name
, NULL
, force
, hardforce
);
1561 zfs_ioc_pool_configs(zfs_cmd_t
*zc
)
1566 if ((configs
= spa_all_configs(&zc
->zc_cookie
)) == NULL
)
1567 return (SET_ERROR(EEXIST
));
1569 error
= put_nvlist(zc
, configs
);
1571 nvlist_free(configs
);
1578 * zc_name name of the pool
1581 * zc_cookie real errno
1582 * zc_nvlist_dst config nvlist
1583 * zc_nvlist_dst_size size of config nvlist
1586 zfs_ioc_pool_stats(zfs_cmd_t
*zc
)
1592 error
= spa_get_stats(zc
->zc_name
, &config
, zc
->zc_value
,
1593 sizeof (zc
->zc_value
));
1595 if (config
!= NULL
) {
1596 ret
= put_nvlist(zc
, config
);
1597 nvlist_free(config
);
1600 * The config may be present even if 'error' is non-zero.
1601 * In this case we return success, and preserve the real errno
1604 zc
->zc_cookie
= error
;
1613 * Try to import the given pool, returning pool stats as appropriate so that
1614 * user land knows which devices are available and overall pool health.
1617 zfs_ioc_pool_tryimport(zfs_cmd_t
*zc
)
1619 nvlist_t
*tryconfig
, *config
;
1622 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1623 zc
->zc_iflags
, &tryconfig
)) != 0)
1626 config
= spa_tryimport(tryconfig
);
1628 nvlist_free(tryconfig
);
1631 return (SET_ERROR(EINVAL
));
1633 error
= put_nvlist(zc
, config
);
1634 nvlist_free(config
);
1641 * zc_name name of the pool
1642 * zc_cookie scan func (pool_scan_func_t)
1645 zfs_ioc_pool_scan(zfs_cmd_t
*zc
)
1650 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1653 if (zc
->zc_cookie
== POOL_SCAN_NONE
)
1654 error
= spa_scan_stop(spa
);
1656 error
= spa_scan(spa
, zc
->zc_cookie
);
1658 spa_close(spa
, FTAG
);
1664 zfs_ioc_pool_freeze(zfs_cmd_t
*zc
)
1669 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1672 spa_close(spa
, FTAG
);
1678 zfs_ioc_pool_upgrade(zfs_cmd_t
*zc
)
1683 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1686 if (zc
->zc_cookie
< spa_version(spa
) ||
1687 !SPA_VERSION_IS_SUPPORTED(zc
->zc_cookie
)) {
1688 spa_close(spa
, FTAG
);
1689 return (SET_ERROR(EINVAL
));
1692 spa_upgrade(spa
, zc
->zc_cookie
);
1693 spa_close(spa
, FTAG
);
1699 zfs_ioc_pool_get_history(zfs_cmd_t
*zc
)
1706 if ((size
= zc
->zc_history_len
) == 0)
1707 return (SET_ERROR(EINVAL
));
1709 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1712 if (spa_version(spa
) < SPA_VERSION_ZPOOL_HISTORY
) {
1713 spa_close(spa
, FTAG
);
1714 return (SET_ERROR(ENOTSUP
));
1717 hist_buf
= vmem_alloc(size
, KM_SLEEP
);
1718 if ((error
= spa_history_get(spa
, &zc
->zc_history_offset
,
1719 &zc
->zc_history_len
, hist_buf
)) == 0) {
1720 error
= ddi_copyout(hist_buf
,
1721 (void *)(uintptr_t)zc
->zc_history
,
1722 zc
->zc_history_len
, zc
->zc_iflags
);
1725 spa_close(spa
, FTAG
);
1726 vmem_free(hist_buf
, size
);
1731 zfs_ioc_pool_reguid(zfs_cmd_t
*zc
)
1736 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1738 error
= spa_change_guid(spa
);
1739 spa_close(spa
, FTAG
);
1745 zfs_ioc_dsobj_to_dsname(zfs_cmd_t
*zc
)
1747 return (dsl_dsobj_to_dsname(zc
->zc_name
, zc
->zc_obj
, zc
->zc_value
));
1752 * zc_name name of filesystem
1753 * zc_obj object to find
1756 * zc_value name of object
1759 zfs_ioc_obj_to_path(zfs_cmd_t
*zc
)
1764 /* XXX reading from objset not owned */
1765 if ((error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
)) != 0)
1767 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
1768 dmu_objset_rele(os
, FTAG
);
1769 return (SET_ERROR(EINVAL
));
1771 error
= zfs_obj_to_path(os
, zc
->zc_obj
, zc
->zc_value
,
1772 sizeof (zc
->zc_value
));
1773 dmu_objset_rele(os
, FTAG
);
1780 * zc_name name of filesystem
1781 * zc_obj object to find
1784 * zc_stat stats on object
1785 * zc_value path to object
1788 zfs_ioc_obj_to_stats(zfs_cmd_t
*zc
)
1793 /* XXX reading from objset not owned */
1794 if ((error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
)) != 0)
1796 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
1797 dmu_objset_rele(os
, FTAG
);
1798 return (SET_ERROR(EINVAL
));
1800 error
= zfs_obj_to_stats(os
, zc
->zc_obj
, &zc
->zc_stat
, zc
->zc_value
,
1801 sizeof (zc
->zc_value
));
1802 dmu_objset_rele(os
, FTAG
);
1808 zfs_ioc_vdev_add(zfs_cmd_t
*zc
)
1814 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1818 error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1819 zc
->zc_iflags
, &config
);
1821 error
= spa_vdev_add(spa
, config
);
1822 nvlist_free(config
);
1824 spa_close(spa
, FTAG
);
1830 * zc_name name of the pool
1831 * zc_nvlist_conf nvlist of devices to remove
1832 * zc_cookie to stop the remove?
1835 zfs_ioc_vdev_remove(zfs_cmd_t
*zc
)
1840 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1843 error
= spa_vdev_remove(spa
, zc
->zc_guid
, B_FALSE
);
1844 spa_close(spa
, FTAG
);
1849 zfs_ioc_vdev_set_state(zfs_cmd_t
*zc
)
1853 vdev_state_t newstate
= VDEV_STATE_UNKNOWN
;
1855 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1857 switch (zc
->zc_cookie
) {
1858 case VDEV_STATE_ONLINE
:
1859 error
= vdev_online(spa
, zc
->zc_guid
, zc
->zc_obj
, &newstate
);
1862 case VDEV_STATE_OFFLINE
:
1863 error
= vdev_offline(spa
, zc
->zc_guid
, zc
->zc_obj
);
1866 case VDEV_STATE_FAULTED
:
1867 if (zc
->zc_obj
!= VDEV_AUX_ERR_EXCEEDED
&&
1868 zc
->zc_obj
!= VDEV_AUX_EXTERNAL
)
1869 zc
->zc_obj
= VDEV_AUX_ERR_EXCEEDED
;
1871 error
= vdev_fault(spa
, zc
->zc_guid
, zc
->zc_obj
);
1874 case VDEV_STATE_DEGRADED
:
1875 if (zc
->zc_obj
!= VDEV_AUX_ERR_EXCEEDED
&&
1876 zc
->zc_obj
!= VDEV_AUX_EXTERNAL
)
1877 zc
->zc_obj
= VDEV_AUX_ERR_EXCEEDED
;
1879 error
= vdev_degrade(spa
, zc
->zc_guid
, zc
->zc_obj
);
1883 error
= SET_ERROR(EINVAL
);
1885 zc
->zc_cookie
= newstate
;
1886 spa_close(spa
, FTAG
);
1891 zfs_ioc_vdev_attach(zfs_cmd_t
*zc
)
1894 int replacing
= zc
->zc_cookie
;
1898 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1901 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1902 zc
->zc_iflags
, &config
)) == 0) {
1903 error
= spa_vdev_attach(spa
, zc
->zc_guid
, config
, replacing
);
1904 nvlist_free(config
);
1907 spa_close(spa
, FTAG
);
1912 zfs_ioc_vdev_detach(zfs_cmd_t
*zc
)
1917 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1920 error
= spa_vdev_detach(spa
, zc
->zc_guid
, 0, B_FALSE
);
1922 spa_close(spa
, FTAG
);
1927 zfs_ioc_vdev_split(zfs_cmd_t
*zc
)
1930 nvlist_t
*config
, *props
= NULL
;
1932 boolean_t exp
= !!(zc
->zc_cookie
& ZPOOL_EXPORT_AFTER_SPLIT
);
1934 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1937 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1938 zc
->zc_iflags
, &config
))) {
1939 spa_close(spa
, FTAG
);
1943 if (zc
->zc_nvlist_src_size
!= 0 && (error
=
1944 get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1945 zc
->zc_iflags
, &props
))) {
1946 spa_close(spa
, FTAG
);
1947 nvlist_free(config
);
1951 error
= spa_vdev_split_mirror(spa
, zc
->zc_string
, config
, props
, exp
);
1953 spa_close(spa
, FTAG
);
1955 nvlist_free(config
);
1962 zfs_ioc_vdev_setpath(zfs_cmd_t
*zc
)
1965 char *path
= zc
->zc_value
;
1966 uint64_t guid
= zc
->zc_guid
;
1969 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1973 error
= spa_vdev_setpath(spa
, guid
, path
);
1974 spa_close(spa
, FTAG
);
1979 zfs_ioc_vdev_setfru(zfs_cmd_t
*zc
)
1982 char *fru
= zc
->zc_value
;
1983 uint64_t guid
= zc
->zc_guid
;
1986 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1990 error
= spa_vdev_setfru(spa
, guid
, fru
);
1991 spa_close(spa
, FTAG
);
1996 zfs_ioc_objset_stats_impl(zfs_cmd_t
*zc
, objset_t
*os
)
2001 dmu_objset_fast_stat(os
, &zc
->zc_objset_stats
);
2003 if (zc
->zc_nvlist_dst
!= 0 &&
2004 (error
= dsl_prop_get_all(os
, &nv
)) == 0) {
2005 dmu_objset_stats(os
, nv
);
2007 * NB: zvol_get_stats() will read the objset contents,
2008 * which we aren't supposed to do with a
2009 * DS_MODE_USER hold, because it could be
2010 * inconsistent. So this is a bit of a workaround...
2011 * XXX reading with out owning
2013 if (!zc
->zc_objset_stats
.dds_inconsistent
&&
2014 dmu_objset_type(os
) == DMU_OST_ZVOL
) {
2015 error
= zvol_get_stats(os
, nv
);
2021 error
= put_nvlist(zc
, nv
);
2030 * zc_name name of filesystem
2031 * zc_nvlist_dst_size size of buffer for property nvlist
2034 * zc_objset_stats stats
2035 * zc_nvlist_dst property nvlist
2036 * zc_nvlist_dst_size size of property nvlist
2039 zfs_ioc_objset_stats(zfs_cmd_t
*zc
)
2044 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
2046 error
= zfs_ioc_objset_stats_impl(zc
, os
);
2047 dmu_objset_rele(os
, FTAG
);
2055 * zc_name name of filesystem
2056 * zc_nvlist_dst_size size of buffer for property nvlist
2059 * zc_nvlist_dst received property nvlist
2060 * zc_nvlist_dst_size size of received property nvlist
2062 * Gets received properties (distinct from local properties on or after
2063 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
2064 * local property values.
2067 zfs_ioc_objset_recvd_props(zfs_cmd_t
*zc
)
2073 * Without this check, we would return local property values if the
2074 * caller has not already received properties on or after
2075 * SPA_VERSION_RECVD_PROPS.
2077 if (!dsl_prop_get_hasrecvd(zc
->zc_name
))
2078 return (SET_ERROR(ENOTSUP
));
2080 if (zc
->zc_nvlist_dst
!= 0 &&
2081 (error
= dsl_prop_get_received(zc
->zc_name
, &nv
)) == 0) {
2082 error
= put_nvlist(zc
, nv
);
2090 nvl_add_zplprop(objset_t
*os
, nvlist_t
*props
, zfs_prop_t prop
)
2096 * zfs_get_zplprop() will either find a value or give us
2097 * the default value (if there is one).
2099 if ((error
= zfs_get_zplprop(os
, prop
, &value
)) != 0)
2101 VERIFY(nvlist_add_uint64(props
, zfs_prop_to_name(prop
), value
) == 0);
2107 * zc_name name of filesystem
2108 * zc_nvlist_dst_size size of buffer for zpl property nvlist
2111 * zc_nvlist_dst zpl property nvlist
2112 * zc_nvlist_dst_size size of zpl property nvlist
2115 zfs_ioc_objset_zplprops(zfs_cmd_t
*zc
)
2120 /* XXX reading without owning */
2121 if ((err
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
)))
2124 dmu_objset_fast_stat(os
, &zc
->zc_objset_stats
);
2127 * NB: nvl_add_zplprop() will read the objset contents,
2128 * which we aren't supposed to do with a DS_MODE_USER
2129 * hold, because it could be inconsistent.
2131 if (zc
->zc_nvlist_dst
!= 0 &&
2132 !zc
->zc_objset_stats
.dds_inconsistent
&&
2133 dmu_objset_type(os
) == DMU_OST_ZFS
) {
2136 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2137 if ((err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_VERSION
)) == 0 &&
2138 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_NORMALIZE
)) == 0 &&
2139 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_UTF8ONLY
)) == 0 &&
2140 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_CASE
)) == 0)
2141 err
= put_nvlist(zc
, nv
);
2144 err
= SET_ERROR(ENOENT
);
2146 dmu_objset_rele(os
, FTAG
);
2151 dataset_name_hidden(const char *name
)
2154 * Skip over datasets that are not visible in this zone,
2155 * internal datasets (which have a $ in their name), and
2156 * temporary datasets (which have a % in their name).
2158 if (strchr(name
, '$') != NULL
)
2160 if (strchr(name
, '%') != NULL
)
2162 if (!INGLOBALZONE(curproc
) && !zone_dataset_visible(name
, NULL
))
2169 * zc_name name of filesystem
2170 * zc_cookie zap cursor
2171 * zc_nvlist_dst_size size of buffer for property nvlist
2174 * zc_name name of next filesystem
2175 * zc_cookie zap cursor
2176 * zc_objset_stats stats
2177 * zc_nvlist_dst property nvlist
2178 * zc_nvlist_dst_size size of property nvlist
2181 zfs_ioc_dataset_list_next(zfs_cmd_t
*zc
)
2186 size_t orig_len
= strlen(zc
->zc_name
);
2189 if ((error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
))) {
2190 if (error
== ENOENT
)
2191 error
= SET_ERROR(ESRCH
);
2195 p
= strrchr(zc
->zc_name
, '/');
2196 if (p
== NULL
|| p
[1] != '\0')
2197 (void) strlcat(zc
->zc_name
, "/", sizeof (zc
->zc_name
));
2198 p
= zc
->zc_name
+ strlen(zc
->zc_name
);
2201 error
= dmu_dir_list_next(os
,
2202 sizeof (zc
->zc_name
) - (p
- zc
->zc_name
), p
,
2203 NULL
, &zc
->zc_cookie
);
2204 if (error
== ENOENT
)
2205 error
= SET_ERROR(ESRCH
);
2206 } while (error
== 0 && dataset_name_hidden(zc
->zc_name
));
2207 dmu_objset_rele(os
, FTAG
);
2210 * If it's an internal dataset (ie. with a '$' in its name),
2211 * don't try to get stats for it, otherwise we'll return ENOENT.
2213 if (error
== 0 && strchr(zc
->zc_name
, '$') == NULL
) {
2214 error
= zfs_ioc_objset_stats(zc
); /* fill in the stats */
2215 if (error
== ENOENT
) {
2216 /* We lost a race with destroy, get the next one. */
2217 zc
->zc_name
[orig_len
] = '\0';
2226 * zc_name name of filesystem
2227 * zc_cookie zap cursor
2228 * zc_nvlist_dst_size size of buffer for property nvlist
2231 * zc_name name of next snapshot
2232 * zc_objset_stats stats
2233 * zc_nvlist_dst property nvlist
2234 * zc_nvlist_dst_size size of property nvlist
2237 zfs_ioc_snapshot_list_next(zfs_cmd_t
*zc
)
2242 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
2244 return (error
== ENOENT
? ESRCH
: error
);
2248 * A dataset name of maximum length cannot have any snapshots,
2249 * so exit immediately.
2251 if (strlcat(zc
->zc_name
, "@", sizeof (zc
->zc_name
)) >= MAXNAMELEN
) {
2252 dmu_objset_rele(os
, FTAG
);
2253 return (SET_ERROR(ESRCH
));
2256 error
= dmu_snapshot_list_next(os
,
2257 sizeof (zc
->zc_name
) - strlen(zc
->zc_name
),
2258 zc
->zc_name
+ strlen(zc
->zc_name
), &zc
->zc_obj
, &zc
->zc_cookie
,
2261 if (error
== 0 && !zc
->zc_simple
) {
2263 dsl_pool_t
*dp
= os
->os_dsl_dataset
->ds_dir
->dd_pool
;
2265 error
= dsl_dataset_hold_obj(dp
, zc
->zc_obj
, FTAG
, &ds
);
2269 error
= dmu_objset_from_ds(ds
, &ossnap
);
2271 error
= zfs_ioc_objset_stats_impl(zc
, ossnap
);
2272 dsl_dataset_rele(ds
, FTAG
);
2274 } else if (error
== ENOENT
) {
2275 error
= SET_ERROR(ESRCH
);
2278 dmu_objset_rele(os
, FTAG
);
2279 /* if we failed, undo the @ that we tacked on to zc_name */
2281 *strchr(zc
->zc_name
, '@') = '\0';
2286 zfs_prop_set_userquota(const char *dsname
, nvpair_t
*pair
)
2288 const char *propname
= nvpair_name(pair
);
2290 unsigned int vallen
;
2293 zfs_userquota_prop_t type
;
2299 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2301 VERIFY(nvpair_value_nvlist(pair
, &attrs
) == 0);
2302 if (nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
2304 return (SET_ERROR(EINVAL
));
2308 * A correctly constructed propname is encoded as
2309 * userquota@<rid>-<domain>.
2311 if ((dash
= strchr(propname
, '-')) == NULL
||
2312 nvpair_value_uint64_array(pair
, &valary
, &vallen
) != 0 ||
2314 return (SET_ERROR(EINVAL
));
2321 err
= zfs_sb_hold(dsname
, FTAG
, &zsb
, B_FALSE
);
2323 err
= zfs_set_userquota(zsb
, type
, domain
, rid
, quota
);
2324 zfs_sb_rele(zsb
, FTAG
);
2331 * If the named property is one that has a special function to set its value,
2332 * return 0 on success and a positive error code on failure; otherwise if it is
2333 * not one of the special properties handled by this function, return -1.
2335 * XXX: It would be better for callers of the property interface if we handled
2336 * these special cases in dsl_prop.c (in the dsl layer).
2339 zfs_prop_set_special(const char *dsname
, zprop_source_t source
,
2342 const char *propname
= nvpair_name(pair
);
2343 zfs_prop_t prop
= zfs_name_to_prop(propname
);
2347 if (prop
== ZPROP_INVAL
) {
2348 if (zfs_prop_userquota(propname
))
2349 return (zfs_prop_set_userquota(dsname
, pair
));
2353 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2355 VERIFY(nvpair_value_nvlist(pair
, &attrs
) == 0);
2356 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
2360 if (zfs_prop_get_type(prop
) == PROP_TYPE_STRING
)
2363 VERIFY(0 == nvpair_value_uint64(pair
, &intval
));
2366 case ZFS_PROP_QUOTA
:
2367 err
= dsl_dir_set_quota(dsname
, source
, intval
);
2369 case ZFS_PROP_REFQUOTA
:
2370 err
= dsl_dataset_set_refquota(dsname
, source
, intval
);
2372 case ZFS_PROP_FILESYSTEM_LIMIT
:
2373 case ZFS_PROP_SNAPSHOT_LIMIT
:
2374 if (intval
== UINT64_MAX
) {
2375 /* clearing the limit, just do it */
2378 err
= dsl_dir_activate_fs_ss_limit(dsname
);
2381 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2382 * default path to set the value in the nvlist.
2387 case ZFS_PROP_RESERVATION
:
2388 err
= dsl_dir_set_reservation(dsname
, source
, intval
);
2390 case ZFS_PROP_REFRESERVATION
:
2391 err
= dsl_dataset_set_refreservation(dsname
, source
, intval
);
2393 case ZFS_PROP_VOLSIZE
:
2394 err
= zvol_set_volsize(dsname
, intval
);
2396 case ZFS_PROP_SNAPDEV
:
2397 err
= zvol_set_snapdev(dsname
, source
, intval
);
2399 case ZFS_PROP_VERSION
:
2403 if ((err
= zfs_sb_hold(dsname
, FTAG
, &zsb
, B_TRUE
)) != 0)
2406 err
= zfs_set_version(zsb
, intval
);
2407 zfs_sb_rele(zsb
, FTAG
);
2409 if (err
== 0 && intval
>= ZPL_VERSION_USERSPACE
) {
2412 zc
= kmem_zalloc(sizeof (zfs_cmd_t
), KM_SLEEP
);
2413 (void) strcpy(zc
->zc_name
, dsname
);
2414 (void) zfs_ioc_userspace_upgrade(zc
);
2415 kmem_free(zc
, sizeof (zfs_cmd_t
));
2427 * This function is best effort. If it fails to set any of the given properties,
2428 * it continues to set as many as it can and returns the last error
2429 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2430 * with the list of names of all the properties that failed along with the
2431 * corresponding error numbers.
2433 * If every property is set successfully, zero is returned and errlist is not
2437 zfs_set_prop_nvlist(const char *dsname
, zprop_source_t source
, nvlist_t
*nvl
,
2446 nvlist_t
*genericnvl
= fnvlist_alloc();
2447 nvlist_t
*retrynvl
= fnvlist_alloc();
2450 while ((pair
= nvlist_next_nvpair(nvl
, pair
)) != NULL
) {
2451 const char *propname
= nvpair_name(pair
);
2452 zfs_prop_t prop
= zfs_name_to_prop(propname
);
2455 /* decode the property value */
2457 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2459 attrs
= fnvpair_value_nvlist(pair
);
2460 if (nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
2462 err
= SET_ERROR(EINVAL
);
2465 /* Validate value type */
2466 if (err
== 0 && prop
== ZPROP_INVAL
) {
2467 if (zfs_prop_user(propname
)) {
2468 if (nvpair_type(propval
) != DATA_TYPE_STRING
)
2469 err
= SET_ERROR(EINVAL
);
2470 } else if (zfs_prop_userquota(propname
)) {
2471 if (nvpair_type(propval
) !=
2472 DATA_TYPE_UINT64_ARRAY
)
2473 err
= SET_ERROR(EINVAL
);
2475 err
= SET_ERROR(EINVAL
);
2477 } else if (err
== 0) {
2478 if (nvpair_type(propval
) == DATA_TYPE_STRING
) {
2479 if (zfs_prop_get_type(prop
) != PROP_TYPE_STRING
)
2480 err
= SET_ERROR(EINVAL
);
2481 } else if (nvpair_type(propval
) == DATA_TYPE_UINT64
) {
2484 intval
= fnvpair_value_uint64(propval
);
2486 switch (zfs_prop_get_type(prop
)) {
2487 case PROP_TYPE_NUMBER
:
2489 case PROP_TYPE_STRING
:
2490 err
= SET_ERROR(EINVAL
);
2492 case PROP_TYPE_INDEX
:
2493 if (zfs_prop_index_to_string(prop
,
2494 intval
, &unused
) != 0)
2495 err
= SET_ERROR(EINVAL
);
2499 "unknown property type");
2502 err
= SET_ERROR(EINVAL
);
2506 /* Validate permissions */
2508 err
= zfs_check_settable(dsname
, pair
, CRED());
2511 err
= zfs_prop_set_special(dsname
, source
, pair
);
2514 * For better performance we build up a list of
2515 * properties to set in a single transaction.
2517 err
= nvlist_add_nvpair(genericnvl
, pair
);
2518 } else if (err
!= 0 && nvl
!= retrynvl
) {
2520 * This may be a spurious error caused by
2521 * receiving quota and reservation out of order.
2522 * Try again in a second pass.
2524 err
= nvlist_add_nvpair(retrynvl
, pair
);
2529 if (errlist
!= NULL
)
2530 fnvlist_add_int32(errlist
, propname
, err
);
2535 if (nvl
!= retrynvl
&& !nvlist_empty(retrynvl
)) {
2540 if (!nvlist_empty(genericnvl
) &&
2541 dsl_props_set(dsname
, source
, genericnvl
) != 0) {
2543 * If this fails, we still want to set as many properties as we
2544 * can, so try setting them individually.
2547 while ((pair
= nvlist_next_nvpair(genericnvl
, pair
)) != NULL
) {
2548 const char *propname
= nvpair_name(pair
);
2552 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2554 attrs
= fnvpair_value_nvlist(pair
);
2555 propval
= fnvlist_lookup_nvpair(attrs
,
2559 if (nvpair_type(propval
) == DATA_TYPE_STRING
) {
2560 strval
= fnvpair_value_string(propval
);
2561 err
= dsl_prop_set_string(dsname
, propname
,
2564 intval
= fnvpair_value_uint64(propval
);
2565 err
= dsl_prop_set_int(dsname
, propname
, source
,
2570 if (errlist
!= NULL
) {
2571 fnvlist_add_int32(errlist
, propname
,
2578 nvlist_free(genericnvl
);
2579 nvlist_free(retrynvl
);
2585 * Check that all the properties are valid user properties.
2588 zfs_check_userprops(const char *fsname
, nvlist_t
*nvl
)
2590 nvpair_t
*pair
= NULL
;
2593 while ((pair
= nvlist_next_nvpair(nvl
, pair
)) != NULL
) {
2594 const char *propname
= nvpair_name(pair
);
2596 if (!zfs_prop_user(propname
) ||
2597 nvpair_type(pair
) != DATA_TYPE_STRING
)
2598 return (SET_ERROR(EINVAL
));
2600 if ((error
= zfs_secpolicy_write_perms(fsname
,
2601 ZFS_DELEG_PERM_USERPROP
, CRED())))
2604 if (strlen(propname
) >= ZAP_MAXNAMELEN
)
2605 return (SET_ERROR(ENAMETOOLONG
));
2607 if (strlen(fnvpair_value_string(pair
)) >= ZAP_MAXVALUELEN
)
2608 return (SET_ERROR(E2BIG
));
2614 props_skip(nvlist_t
*props
, nvlist_t
*skipped
, nvlist_t
**newprops
)
2618 VERIFY(nvlist_alloc(newprops
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2621 while ((pair
= nvlist_next_nvpair(props
, pair
)) != NULL
) {
2622 if (nvlist_exists(skipped
, nvpair_name(pair
)))
2625 VERIFY(nvlist_add_nvpair(*newprops
, pair
) == 0);
2630 clear_received_props(const char *dsname
, nvlist_t
*props
,
2634 nvlist_t
*cleared_props
= NULL
;
2635 props_skip(props
, skipped
, &cleared_props
);
2636 if (!nvlist_empty(cleared_props
)) {
2638 * Acts on local properties until the dataset has received
2639 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2641 zprop_source_t flags
= (ZPROP_SRC_NONE
|
2642 (dsl_prop_get_hasrecvd(dsname
) ? ZPROP_SRC_RECEIVED
: 0));
2643 err
= zfs_set_prop_nvlist(dsname
, flags
, cleared_props
, NULL
);
2645 nvlist_free(cleared_props
);
2651 * zc_name name of filesystem
2652 * zc_value name of property to set
2653 * zc_nvlist_src{_size} nvlist of properties to apply
2654 * zc_cookie received properties flag
2657 * zc_nvlist_dst{_size} error for each unapplied received property
2660 zfs_ioc_set_prop(zfs_cmd_t
*zc
)
2663 boolean_t received
= zc
->zc_cookie
;
2664 zprop_source_t source
= (received
? ZPROP_SRC_RECEIVED
:
2669 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2670 zc
->zc_iflags
, &nvl
)) != 0)
2674 nvlist_t
*origprops
;
2676 if (dsl_prop_get_received(zc
->zc_name
, &origprops
) == 0) {
2677 (void) clear_received_props(zc
->zc_name
,
2679 nvlist_free(origprops
);
2682 error
= dsl_prop_set_hasrecvd(zc
->zc_name
);
2685 errors
= fnvlist_alloc();
2687 error
= zfs_set_prop_nvlist(zc
->zc_name
, source
, nvl
, errors
);
2689 if (zc
->zc_nvlist_dst
!= 0 && errors
!= NULL
) {
2690 (void) put_nvlist(zc
, errors
);
2693 nvlist_free(errors
);
2700 * zc_name name of filesystem
2701 * zc_value name of property to inherit
2702 * zc_cookie revert to received value if TRUE
2707 zfs_ioc_inherit_prop(zfs_cmd_t
*zc
)
2709 const char *propname
= zc
->zc_value
;
2710 zfs_prop_t prop
= zfs_name_to_prop(propname
);
2711 boolean_t received
= zc
->zc_cookie
;
2712 zprop_source_t source
= (received
2713 ? ZPROP_SRC_NONE
/* revert to received value, if any */
2714 : ZPROP_SRC_INHERITED
); /* explicitly inherit */
2723 * zfs_prop_set_special() expects properties in the form of an
2724 * nvpair with type info.
2726 if (prop
== ZPROP_INVAL
) {
2727 if (!zfs_prop_user(propname
))
2728 return (SET_ERROR(EINVAL
));
2730 type
= PROP_TYPE_STRING
;
2731 } else if (prop
== ZFS_PROP_VOLSIZE
||
2732 prop
== ZFS_PROP_VERSION
) {
2733 return (SET_ERROR(EINVAL
));
2735 type
= zfs_prop_get_type(prop
);
2738 VERIFY(nvlist_alloc(&dummy
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2741 case PROP_TYPE_STRING
:
2742 VERIFY(0 == nvlist_add_string(dummy
, propname
, ""));
2744 case PROP_TYPE_NUMBER
:
2745 case PROP_TYPE_INDEX
:
2746 VERIFY(0 == nvlist_add_uint64(dummy
, propname
, 0));
2750 return (SET_ERROR(EINVAL
));
2753 pair
= nvlist_next_nvpair(dummy
, NULL
);
2754 err
= zfs_prop_set_special(zc
->zc_name
, source
, pair
);
2757 return (err
); /* special property already handled */
2760 * Only check this in the non-received case. We want to allow
2761 * 'inherit -S' to revert non-inheritable properties like quota
2762 * and reservation to the received or default values even though
2763 * they are not considered inheritable.
2765 if (prop
!= ZPROP_INVAL
&& !zfs_prop_inheritable(prop
))
2766 return (SET_ERROR(EINVAL
));
2769 /* property name has been validated by zfs_secpolicy_inherit_prop() */
2770 return (dsl_prop_inherit(zc
->zc_name
, zc
->zc_value
, source
));
2774 zfs_ioc_pool_set_props(zfs_cmd_t
*zc
)
2781 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2782 zc
->zc_iflags
, &props
)))
2786 * If the only property is the configfile, then just do a spa_lookup()
2787 * to handle the faulted case.
2789 pair
= nvlist_next_nvpair(props
, NULL
);
2790 if (pair
!= NULL
&& strcmp(nvpair_name(pair
),
2791 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
)) == 0 &&
2792 nvlist_next_nvpair(props
, pair
) == NULL
) {
2793 mutex_enter(&spa_namespace_lock
);
2794 if ((spa
= spa_lookup(zc
->zc_name
)) != NULL
) {
2795 spa_configfile_set(spa
, props
, B_FALSE
);
2796 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
2798 mutex_exit(&spa_namespace_lock
);
2805 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0) {
2810 error
= spa_prop_set(spa
, props
);
2813 spa_close(spa
, FTAG
);
2819 zfs_ioc_pool_get_props(zfs_cmd_t
*zc
)
2823 nvlist_t
*nvp
= NULL
;
2825 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0) {
2827 * If the pool is faulted, there may be properties we can still
2828 * get (such as altroot and cachefile), so attempt to get them
2831 mutex_enter(&spa_namespace_lock
);
2832 if ((spa
= spa_lookup(zc
->zc_name
)) != NULL
)
2833 error
= spa_prop_get(spa
, &nvp
);
2834 mutex_exit(&spa_namespace_lock
);
2836 error
= spa_prop_get(spa
, &nvp
);
2837 spa_close(spa
, FTAG
);
2840 if (error
== 0 && zc
->zc_nvlist_dst
!= 0)
2841 error
= put_nvlist(zc
, nvp
);
2843 error
= SET_ERROR(EFAULT
);
2851 * zc_name name of filesystem
2852 * zc_nvlist_src{_size} nvlist of delegated permissions
2853 * zc_perm_action allow/unallow flag
2858 zfs_ioc_set_fsacl(zfs_cmd_t
*zc
)
2861 nvlist_t
*fsaclnv
= NULL
;
2863 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2864 zc
->zc_iflags
, &fsaclnv
)) != 0)
2868 * Verify nvlist is constructed correctly
2870 if ((error
= zfs_deleg_verify_nvlist(fsaclnv
)) != 0) {
2871 nvlist_free(fsaclnv
);
2872 return (SET_ERROR(EINVAL
));
2876 * If we don't have PRIV_SYS_MOUNT, then validate
2877 * that user is allowed to hand out each permission in
2881 error
= secpolicy_zfs(CRED());
2883 if (zc
->zc_perm_action
== B_FALSE
) {
2884 error
= dsl_deleg_can_allow(zc
->zc_name
,
2887 error
= dsl_deleg_can_unallow(zc
->zc_name
,
2893 error
= dsl_deleg_set(zc
->zc_name
, fsaclnv
, zc
->zc_perm_action
);
2895 nvlist_free(fsaclnv
);
2901 * zc_name name of filesystem
2904 * zc_nvlist_src{_size} nvlist of delegated permissions
2907 zfs_ioc_get_fsacl(zfs_cmd_t
*zc
)
2912 if ((error
= dsl_deleg_get(zc
->zc_name
, &nvp
)) == 0) {
2913 error
= put_nvlist(zc
, nvp
);
2922 zfs_create_cb(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
)
2924 zfs_creat_t
*zct
= arg
;
2926 zfs_create_fs(os
, cr
, zct
->zct_zplprops
, tx
);
2929 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
2933 * os parent objset pointer (NULL if root fs)
2934 * fuids_ok fuids allowed in this version of the spa?
2935 * sa_ok SAs allowed in this version of the spa?
2936 * createprops list of properties requested by creator
2939 * zplprops values for the zplprops we attach to the master node object
2940 * is_ci true if requested file system will be purely case-insensitive
2942 * Determine the settings for utf8only, normalization and
2943 * casesensitivity. Specific values may have been requested by the
2944 * creator and/or we can inherit values from the parent dataset. If
2945 * the file system is of too early a vintage, a creator can not
2946 * request settings for these properties, even if the requested
2947 * setting is the default value. We don't actually want to create dsl
2948 * properties for these, so remove them from the source nvlist after
2952 zfs_fill_zplprops_impl(objset_t
*os
, uint64_t zplver
,
2953 boolean_t fuids_ok
, boolean_t sa_ok
, nvlist_t
*createprops
,
2954 nvlist_t
*zplprops
, boolean_t
*is_ci
)
2956 uint64_t sense
= ZFS_PROP_UNDEFINED
;
2957 uint64_t norm
= ZFS_PROP_UNDEFINED
;
2958 uint64_t u8
= ZFS_PROP_UNDEFINED
;
2961 ASSERT(zplprops
!= NULL
);
2964 * Pull out creator prop choices, if any.
2967 (void) nvlist_lookup_uint64(createprops
,
2968 zfs_prop_to_name(ZFS_PROP_VERSION
), &zplver
);
2969 (void) nvlist_lookup_uint64(createprops
,
2970 zfs_prop_to_name(ZFS_PROP_NORMALIZE
), &norm
);
2971 (void) nvlist_remove_all(createprops
,
2972 zfs_prop_to_name(ZFS_PROP_NORMALIZE
));
2973 (void) nvlist_lookup_uint64(createprops
,
2974 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), &u8
);
2975 (void) nvlist_remove_all(createprops
,
2976 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
));
2977 (void) nvlist_lookup_uint64(createprops
,
2978 zfs_prop_to_name(ZFS_PROP_CASE
), &sense
);
2979 (void) nvlist_remove_all(createprops
,
2980 zfs_prop_to_name(ZFS_PROP_CASE
));
2984 * If the zpl version requested is whacky or the file system
2985 * or pool is version is too "young" to support normalization
2986 * and the creator tried to set a value for one of the props,
2989 if ((zplver
< ZPL_VERSION_INITIAL
|| zplver
> ZPL_VERSION
) ||
2990 (zplver
>= ZPL_VERSION_FUID
&& !fuids_ok
) ||
2991 (zplver
>= ZPL_VERSION_SA
&& !sa_ok
) ||
2992 (zplver
< ZPL_VERSION_NORMALIZATION
&&
2993 (norm
!= ZFS_PROP_UNDEFINED
|| u8
!= ZFS_PROP_UNDEFINED
||
2994 sense
!= ZFS_PROP_UNDEFINED
)))
2995 return (SET_ERROR(ENOTSUP
));
2998 * Put the version in the zplprops
3000 VERIFY(nvlist_add_uint64(zplprops
,
3001 zfs_prop_to_name(ZFS_PROP_VERSION
), zplver
) == 0);
3003 if (norm
== ZFS_PROP_UNDEFINED
&&
3004 (error
= zfs_get_zplprop(os
, ZFS_PROP_NORMALIZE
, &norm
)) != 0)
3006 VERIFY(nvlist_add_uint64(zplprops
,
3007 zfs_prop_to_name(ZFS_PROP_NORMALIZE
), norm
) == 0);
3010 * If we're normalizing, names must always be valid UTF-8 strings.
3014 if (u8
== ZFS_PROP_UNDEFINED
&&
3015 (error
= zfs_get_zplprop(os
, ZFS_PROP_UTF8ONLY
, &u8
)) != 0)
3017 VERIFY(nvlist_add_uint64(zplprops
,
3018 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), u8
) == 0);
3020 if (sense
== ZFS_PROP_UNDEFINED
&&
3021 (error
= zfs_get_zplprop(os
, ZFS_PROP_CASE
, &sense
)) != 0)
3023 VERIFY(nvlist_add_uint64(zplprops
,
3024 zfs_prop_to_name(ZFS_PROP_CASE
), sense
) == 0);
3027 *is_ci
= (sense
== ZFS_CASE_INSENSITIVE
);
3033 zfs_fill_zplprops(const char *dataset
, nvlist_t
*createprops
,
3034 nvlist_t
*zplprops
, boolean_t
*is_ci
)
3036 boolean_t fuids_ok
, sa_ok
;
3037 uint64_t zplver
= ZPL_VERSION
;
3038 objset_t
*os
= NULL
;
3039 char parentname
[MAXNAMELEN
];
3045 (void) strlcpy(parentname
, dataset
, sizeof (parentname
));
3046 cp
= strrchr(parentname
, '/');
3050 if ((error
= spa_open(dataset
, &spa
, FTAG
)) != 0)
3053 spa_vers
= spa_version(spa
);
3054 spa_close(spa
, FTAG
);
3056 zplver
= zfs_zpl_version_map(spa_vers
);
3057 fuids_ok
= (zplver
>= ZPL_VERSION_FUID
);
3058 sa_ok
= (zplver
>= ZPL_VERSION_SA
);
3061 * Open parent object set so we can inherit zplprop values.
3063 if ((error
= dmu_objset_hold(parentname
, FTAG
, &os
)) != 0)
3066 error
= zfs_fill_zplprops_impl(os
, zplver
, fuids_ok
, sa_ok
, createprops
,
3068 dmu_objset_rele(os
, FTAG
);
3073 zfs_fill_zplprops_root(uint64_t spa_vers
, nvlist_t
*createprops
,
3074 nvlist_t
*zplprops
, boolean_t
*is_ci
)
3078 uint64_t zplver
= ZPL_VERSION
;
3081 zplver
= zfs_zpl_version_map(spa_vers
);
3082 fuids_ok
= (zplver
>= ZPL_VERSION_FUID
);
3083 sa_ok
= (zplver
>= ZPL_VERSION_SA
);
3085 error
= zfs_fill_zplprops_impl(NULL
, zplver
, fuids_ok
, sa_ok
,
3086 createprops
, zplprops
, is_ci
);
3092 * "type" -> dmu_objset_type_t (int32)
3093 * (optional) "props" -> { prop -> value }
3096 * outnvl: propname -> error code (int32)
3099 zfs_ioc_create(const char *fsname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3102 zfs_creat_t zct
= { 0 };
3103 nvlist_t
*nvprops
= NULL
;
3104 void (*cbfunc
)(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
);
3106 dmu_objset_type_t type
;
3107 boolean_t is_insensitive
= B_FALSE
;
3109 if (nvlist_lookup_int32(innvl
, "type", &type32
) != 0)
3110 return (SET_ERROR(EINVAL
));
3112 (void) nvlist_lookup_nvlist(innvl
, "props", &nvprops
);
3116 cbfunc
= zfs_create_cb
;
3120 cbfunc
= zvol_create_cb
;
3127 if (strchr(fsname
, '@') ||
3128 strchr(fsname
, '%'))
3129 return (SET_ERROR(EINVAL
));
3131 zct
.zct_props
= nvprops
;
3134 return (SET_ERROR(EINVAL
));
3136 if (type
== DMU_OST_ZVOL
) {
3137 uint64_t volsize
, volblocksize
;
3139 if (nvprops
== NULL
)
3140 return (SET_ERROR(EINVAL
));
3141 if (nvlist_lookup_uint64(nvprops
,
3142 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &volsize
) != 0)
3143 return (SET_ERROR(EINVAL
));
3145 if ((error
= nvlist_lookup_uint64(nvprops
,
3146 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
3147 &volblocksize
)) != 0 && error
!= ENOENT
)
3148 return (SET_ERROR(EINVAL
));
3151 volblocksize
= zfs_prop_default_numeric(
3152 ZFS_PROP_VOLBLOCKSIZE
);
3154 if ((error
= zvol_check_volblocksize(fsname
,
3155 volblocksize
)) != 0 ||
3156 (error
= zvol_check_volsize(volsize
,
3157 volblocksize
)) != 0)
3159 } else if (type
== DMU_OST_ZFS
) {
3163 * We have to have normalization and
3164 * case-folding flags correct when we do the
3165 * file system creation, so go figure them out
3168 VERIFY(nvlist_alloc(&zct
.zct_zplprops
,
3169 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3170 error
= zfs_fill_zplprops(fsname
, nvprops
,
3171 zct
.zct_zplprops
, &is_insensitive
);
3173 nvlist_free(zct
.zct_zplprops
);
3178 error
= dmu_objset_create(fsname
, type
,
3179 is_insensitive
? DS_FLAG_CI_DATASET
: 0, cbfunc
, &zct
);
3180 nvlist_free(zct
.zct_zplprops
);
3183 * It would be nice to do this atomically.
3186 error
= zfs_set_prop_nvlist(fsname
, ZPROP_SRC_LOCAL
,
3189 (void) dsl_destroy_head(fsname
);
3196 * "origin" -> name of origin snapshot
3197 * (optional) "props" -> { prop -> value }
3201 * outnvl: propname -> error code (int32)
3204 zfs_ioc_clone(const char *fsname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3207 nvlist_t
*nvprops
= NULL
;
3210 if (nvlist_lookup_string(innvl
, "origin", &origin_name
) != 0)
3211 return (SET_ERROR(EINVAL
));
3212 (void) nvlist_lookup_nvlist(innvl
, "props", &nvprops
);
3214 if (strchr(fsname
, '@') ||
3215 strchr(fsname
, '%'))
3216 return (SET_ERROR(EINVAL
));
3218 if (dataset_namecheck(origin_name
, NULL
, NULL
) != 0)
3219 return (SET_ERROR(EINVAL
));
3220 error
= dmu_objset_clone(fsname
, origin_name
);
3225 * It would be nice to do this atomically.
3228 error
= zfs_set_prop_nvlist(fsname
, ZPROP_SRC_LOCAL
,
3231 (void) dsl_destroy_head(fsname
);
3238 * "snaps" -> { snapshot1, snapshot2 }
3239 * (optional) "props" -> { prop -> value (string) }
3242 * outnvl: snapshot -> error code (int32)
3245 zfs_ioc_snapshot(const char *poolname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3248 nvlist_t
*props
= NULL
;
3250 nvpair_t
*pair
, *pair2
;
3252 (void) nvlist_lookup_nvlist(innvl
, "props", &props
);
3253 if ((error
= zfs_check_userprops(poolname
, props
)) != 0)
3256 if (!nvlist_empty(props
) &&
3257 zfs_earlier_version(poolname
, SPA_VERSION_SNAP_PROPS
))
3258 return (SET_ERROR(ENOTSUP
));
3260 if (nvlist_lookup_nvlist(innvl
, "snaps", &snaps
) != 0)
3261 return (SET_ERROR(EINVAL
));
3262 poollen
= strlen(poolname
);
3263 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
3264 pair
= nvlist_next_nvpair(snaps
, pair
)) {
3265 const char *name
= nvpair_name(pair
);
3266 const char *cp
= strchr(name
, '@');
3269 * The snap name must contain an @, and the part after it must
3270 * contain only valid characters.
3273 zfs_component_namecheck(cp
+ 1, NULL
, NULL
) != 0)
3274 return (SET_ERROR(EINVAL
));
3277 * The snap must be in the specified pool.
3279 if (strncmp(name
, poolname
, poollen
) != 0 ||
3280 (name
[poollen
] != '/' && name
[poollen
] != '@'))
3281 return (SET_ERROR(EXDEV
));
3283 /* This must be the only snap of this fs. */
3284 for (pair2
= nvlist_next_nvpair(snaps
, pair
);
3285 pair2
!= NULL
; pair2
= nvlist_next_nvpair(snaps
, pair2
)) {
3286 if (strncmp(name
, nvpair_name(pair2
), cp
- name
+ 1)
3288 return (SET_ERROR(EXDEV
));
3293 error
= dsl_dataset_snapshot(snaps
, props
, outnvl
);
3299 * innvl: "message" -> string
3303 zfs_ioc_log_history(const char *unused
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3311 * The poolname in the ioctl is not set, we get it from the TSD,
3312 * which was set at the end of the last successful ioctl that allows
3313 * logging. The secpolicy func already checked that it is set.
3314 * Only one log ioctl is allowed after each successful ioctl, so
3315 * we clear the TSD here.
3317 poolname
= tsd_get(zfs_allow_log_key
);
3318 (void) tsd_set(zfs_allow_log_key
, NULL
);
3319 error
= spa_open(poolname
, &spa
, FTAG
);
3324 if (nvlist_lookup_string(innvl
, "message", &message
) != 0) {
3325 spa_close(spa
, FTAG
);
3326 return (SET_ERROR(EINVAL
));
3329 if (spa_version(spa
) < SPA_VERSION_ZPOOL_HISTORY
) {
3330 spa_close(spa
, FTAG
);
3331 return (SET_ERROR(ENOTSUP
));
3334 error
= spa_history_log(spa
, message
);
3335 spa_close(spa
, FTAG
);
3340 * The dp_config_rwlock must not be held when calling this, because the
3341 * unmount may need to write out data.
3343 * This function is best-effort. Callers must deal gracefully if it
3344 * remains mounted (or is remounted after this call).
3346 * Returns 0 if the argument is not a snapshot, or it is not currently a
3347 * filesystem, or we were able to unmount it. Returns error code otherwise.
3350 zfs_unmount_snap(const char *snapname
)
3354 if (strchr(snapname
, '@') == NULL
)
3357 err
= zfsctl_snapshot_unmount((char *)snapname
, MNT_FORCE
);
3358 if (err
!= 0 && err
!= ENOENT
)
3359 return (SET_ERROR(err
));
3366 zfs_unmount_snap_cb(const char *snapname
, void *arg
)
3368 return (zfs_unmount_snap(snapname
));
3372 * When a clone is destroyed, its origin may also need to be destroyed,
3373 * in which case it must be unmounted. This routine will do that unmount
3377 zfs_destroy_unmount_origin(const char *fsname
)
3383 error
= dmu_objset_hold(fsname
, FTAG
, &os
);
3386 ds
= dmu_objset_ds(os
);
3387 if (dsl_dir_is_clone(ds
->ds_dir
) && DS_IS_DEFER_DESTROY(ds
->ds_prev
)) {
3388 char originname
[MAXNAMELEN
];
3389 dsl_dataset_name(ds
->ds_prev
, originname
);
3390 dmu_objset_rele(os
, FTAG
);
3391 (void) zfs_unmount_snap(originname
);
3393 dmu_objset_rele(os
, FTAG
);
3399 * "snaps" -> { snapshot1, snapshot2 }
3400 * (optional boolean) "defer"
3403 * outnvl: snapshot -> error code (int32)
3407 zfs_ioc_destroy_snaps(const char *poolname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3413 if (nvlist_lookup_nvlist(innvl
, "snaps", &snaps
) != 0)
3414 return (SET_ERROR(EINVAL
));
3415 defer
= nvlist_exists(innvl
, "defer");
3417 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
3418 pair
= nvlist_next_nvpair(snaps
, pair
)) {
3419 (void) zfs_unmount_snap(nvpair_name(pair
));
3422 return (dsl_destroy_snapshots_nvl(snaps
, defer
, outnvl
));
3426 * Create bookmarks. Bookmark names are of the form <fs>#<bmark>.
3427 * All bookmarks must be in the same pool.
3430 * bookmark1 -> snapshot1, bookmark2 -> snapshot2
3433 * outnvl: bookmark -> error code (int32)
3438 zfs_ioc_bookmark(const char *poolname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3440 nvpair_t
*pair
, *pair2
;
3442 for (pair
= nvlist_next_nvpair(innvl
, NULL
);
3443 pair
!= NULL
; pair
= nvlist_next_nvpair(innvl
, pair
)) {
3447 * Verify the snapshot argument.
3449 if (nvpair_value_string(pair
, &snap_name
) != 0)
3450 return (SET_ERROR(EINVAL
));
3453 /* Verify that the keys (bookmarks) are unique */
3454 for (pair2
= nvlist_next_nvpair(innvl
, pair
);
3455 pair2
!= NULL
; pair2
= nvlist_next_nvpair(innvl
, pair2
)) {
3456 if (strcmp(nvpair_name(pair
), nvpair_name(pair2
)) == 0)
3457 return (SET_ERROR(EINVAL
));
3461 return (dsl_bookmark_create(innvl
, outnvl
));
3466 * property 1, property 2, ...
3470 * bookmark name 1 -> { property 1, property 2, ... },
3471 * bookmark name 2 -> { property 1, property 2, ... }
3476 zfs_ioc_get_bookmarks(const char *fsname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3478 return (dsl_get_bookmarks(fsname
, innvl
, outnvl
));
3483 * bookmark name 1, bookmark name 2
3486 * outnvl: bookmark -> error code (int32)
3490 zfs_ioc_destroy_bookmarks(const char *poolname
, nvlist_t
*innvl
,
3496 poollen
= strlen(poolname
);
3497 for (pair
= nvlist_next_nvpair(innvl
, NULL
);
3498 pair
!= NULL
; pair
= nvlist_next_nvpair(innvl
, pair
)) {
3499 const char *name
= nvpair_name(pair
);
3500 const char *cp
= strchr(name
, '#');
3503 * The bookmark name must contain an #, and the part after it
3504 * must contain only valid characters.
3507 zfs_component_namecheck(cp
+ 1, NULL
, NULL
) != 0)
3508 return (SET_ERROR(EINVAL
));
3511 * The bookmark must be in the specified pool.
3513 if (strncmp(name
, poolname
, poollen
) != 0 ||
3514 (name
[poollen
] != '/' && name
[poollen
] != '#'))
3515 return (SET_ERROR(EXDEV
));
3518 error
= dsl_bookmark_destroy(innvl
, outnvl
);
3524 * zc_name name of dataset to destroy
3525 * zc_objset_type type of objset
3526 * zc_defer_destroy mark for deferred destroy
3531 zfs_ioc_destroy(zfs_cmd_t
*zc
)
3535 if (zc
->zc_objset_type
== DMU_OST_ZFS
) {
3536 err
= zfs_unmount_snap(zc
->zc_name
);
3541 if (strchr(zc
->zc_name
, '@'))
3542 err
= dsl_destroy_snapshot(zc
->zc_name
, zc
->zc_defer_destroy
);
3544 err
= dsl_destroy_head(zc
->zc_name
);
3550 * fsname is name of dataset to rollback (to most recent snapshot)
3552 * innvl is not used.
3554 * outnvl: "target" -> name of most recent snapshot
3559 zfs_ioc_rollback(const char *fsname
, nvlist_t
*args
, nvlist_t
*outnvl
)
3564 if (get_zfs_sb(fsname
, &zsb
) == 0) {
3565 error
= zfs_suspend_fs(zsb
);
3569 error
= dsl_dataset_rollback(fsname
, zsb
, outnvl
);
3570 resume_err
= zfs_resume_fs(zsb
, fsname
);
3571 error
= error
? error
: resume_err
;
3573 deactivate_super(zsb
->z_sb
);
3575 error
= dsl_dataset_rollback(fsname
, NULL
, outnvl
);
3581 recursive_unmount(const char *fsname
, void *arg
)
3583 const char *snapname
= arg
;
3587 fullname
= kmem_asprintf("%s@%s", fsname
, snapname
);
3588 error
= zfs_unmount_snap(fullname
);
3596 * zc_name old name of dataset
3597 * zc_value new name of dataset
3598 * zc_cookie recursive flag (only valid for snapshots)
3603 zfs_ioc_rename(zfs_cmd_t
*zc
)
3605 boolean_t recursive
= zc
->zc_cookie
& 1;
3608 zc
->zc_value
[sizeof (zc
->zc_value
) - 1] = '\0';
3609 if (dataset_namecheck(zc
->zc_value
, NULL
, NULL
) != 0 ||
3610 strchr(zc
->zc_value
, '%'))
3611 return (SET_ERROR(EINVAL
));
3613 at
= strchr(zc
->zc_name
, '@');
3615 /* snaps must be in same fs */
3618 if (strncmp(zc
->zc_name
, zc
->zc_value
, at
- zc
->zc_name
+ 1))
3619 return (SET_ERROR(EXDEV
));
3621 if (zc
->zc_objset_type
== DMU_OST_ZFS
) {
3622 error
= dmu_objset_find(zc
->zc_name
,
3623 recursive_unmount
, at
+ 1,
3624 recursive
? DS_FIND_CHILDREN
: 0);
3630 error
= dsl_dataset_rename_snapshot(zc
->zc_name
,
3631 at
+ 1, strchr(zc
->zc_value
, '@') + 1, recursive
);
3636 return (dsl_dir_rename(zc
->zc_name
, zc
->zc_value
));
3641 zfs_check_settable(const char *dsname
, nvpair_t
*pair
, cred_t
*cr
)
3643 const char *propname
= nvpair_name(pair
);
3644 boolean_t issnap
= (strchr(dsname
, '@') != NULL
);
3645 zfs_prop_t prop
= zfs_name_to_prop(propname
);
3649 if (prop
== ZPROP_INVAL
) {
3650 if (zfs_prop_user(propname
)) {
3651 if ((err
= zfs_secpolicy_write_perms(dsname
,
3652 ZFS_DELEG_PERM_USERPROP
, cr
)))
3657 if (!issnap
&& zfs_prop_userquota(propname
)) {
3658 const char *perm
= NULL
;
3659 const char *uq_prefix
=
3660 zfs_userquota_prop_prefixes
[ZFS_PROP_USERQUOTA
];
3661 const char *gq_prefix
=
3662 zfs_userquota_prop_prefixes
[ZFS_PROP_GROUPQUOTA
];
3664 if (strncmp(propname
, uq_prefix
,
3665 strlen(uq_prefix
)) == 0) {
3666 perm
= ZFS_DELEG_PERM_USERQUOTA
;
3667 } else if (strncmp(propname
, gq_prefix
,
3668 strlen(gq_prefix
)) == 0) {
3669 perm
= ZFS_DELEG_PERM_GROUPQUOTA
;
3671 /* USERUSED and GROUPUSED are read-only */
3672 return (SET_ERROR(EINVAL
));
3675 if ((err
= zfs_secpolicy_write_perms(dsname
, perm
, cr
)))
3680 return (SET_ERROR(EINVAL
));
3684 return (SET_ERROR(EINVAL
));
3686 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
3688 * dsl_prop_get_all_impl() returns properties in this
3692 VERIFY(nvpair_value_nvlist(pair
, &attrs
) == 0);
3693 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
3698 * Check that this value is valid for this pool version
3701 case ZFS_PROP_COMPRESSION
:
3703 * If the user specified gzip compression, make sure
3704 * the SPA supports it. We ignore any errors here since
3705 * we'll catch them later.
3707 if (nvpair_value_uint64(pair
, &intval
) == 0) {
3708 if (intval
>= ZIO_COMPRESS_GZIP_1
&&
3709 intval
<= ZIO_COMPRESS_GZIP_9
&&
3710 zfs_earlier_version(dsname
,
3711 SPA_VERSION_GZIP_COMPRESSION
)) {
3712 return (SET_ERROR(ENOTSUP
));
3715 if (intval
== ZIO_COMPRESS_ZLE
&&
3716 zfs_earlier_version(dsname
,
3717 SPA_VERSION_ZLE_COMPRESSION
))
3718 return (SET_ERROR(ENOTSUP
));
3720 if (intval
== ZIO_COMPRESS_LZ4
) {
3723 if ((err
= spa_open(dsname
, &spa
, FTAG
)) != 0)
3726 if (!spa_feature_is_enabled(spa
,
3727 SPA_FEATURE_LZ4_COMPRESS
)) {
3728 spa_close(spa
, FTAG
);
3729 return (SET_ERROR(ENOTSUP
));
3731 spa_close(spa
, FTAG
);
3735 * If this is a bootable dataset then
3736 * verify that the compression algorithm
3737 * is supported for booting. We must return
3738 * something other than ENOTSUP since it
3739 * implies a downrev pool version.
3741 if (zfs_is_bootfs(dsname
) &&
3742 !BOOTFS_COMPRESS_VALID(intval
)) {
3743 return (SET_ERROR(ERANGE
));
3748 case ZFS_PROP_COPIES
:
3749 if (zfs_earlier_version(dsname
, SPA_VERSION_DITTO_BLOCKS
))
3750 return (SET_ERROR(ENOTSUP
));
3753 case ZFS_PROP_DEDUP
:
3754 if (zfs_earlier_version(dsname
, SPA_VERSION_DEDUP
))
3755 return (SET_ERROR(ENOTSUP
));
3758 case ZFS_PROP_VOLBLOCKSIZE
:
3759 case ZFS_PROP_RECORDSIZE
:
3760 /* Record sizes above 128k need the feature to be enabled */
3761 if (nvpair_value_uint64(pair
, &intval
) == 0 &&
3762 intval
> SPA_OLD_MAXBLOCKSIZE
) {
3766 * If this is a bootable dataset then
3767 * the we don't allow large (>128K) blocks,
3768 * because GRUB doesn't support them.
3770 if (zfs_is_bootfs(dsname
) &&
3771 intval
> SPA_OLD_MAXBLOCKSIZE
) {
3772 return (SET_ERROR(EDOM
));
3776 * We don't allow setting the property above 1MB,
3777 * unless the tunable has been changed.
3779 if (intval
> zfs_max_recordsize
||
3780 intval
> SPA_MAXBLOCKSIZE
)
3781 return (SET_ERROR(EDOM
));
3783 if ((err
= spa_open(dsname
, &spa
, FTAG
)) != 0)
3786 if (!spa_feature_is_enabled(spa
,
3787 SPA_FEATURE_LARGE_BLOCKS
)) {
3788 spa_close(spa
, FTAG
);
3789 return (SET_ERROR(ENOTSUP
));
3791 spa_close(spa
, FTAG
);
3795 case ZFS_PROP_SHARESMB
:
3796 if (zpl_earlier_version(dsname
, ZPL_VERSION_FUID
))
3797 return (SET_ERROR(ENOTSUP
));
3800 case ZFS_PROP_ACLINHERIT
:
3801 if (nvpair_type(pair
) == DATA_TYPE_UINT64
&&
3802 nvpair_value_uint64(pair
, &intval
) == 0) {
3803 if (intval
== ZFS_ACL_PASSTHROUGH_X
&&
3804 zfs_earlier_version(dsname
,
3805 SPA_VERSION_PASSTHROUGH_X
))
3806 return (SET_ERROR(ENOTSUP
));
3813 return (zfs_secpolicy_setprop(dsname
, prop
, pair
, CRED()));
3817 * Removes properties from the given props list that fail permission checks
3818 * needed to clear them and to restore them in case of a receive error. For each
3819 * property, make sure we have both set and inherit permissions.
3821 * Returns the first error encountered if any permission checks fail. If the
3822 * caller provides a non-NULL errlist, it also gives the complete list of names
3823 * of all the properties that failed a permission check along with the
3824 * corresponding error numbers. The caller is responsible for freeing the
3827 * If every property checks out successfully, zero is returned and the list
3828 * pointed at by errlist is NULL.
3831 zfs_check_clearable(char *dataset
, nvlist_t
*props
, nvlist_t
**errlist
)
3834 nvpair_t
*pair
, *next_pair
;
3841 VERIFY(nvlist_alloc(&errors
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3843 zc
= kmem_alloc(sizeof (zfs_cmd_t
), KM_SLEEP
);
3844 (void) strcpy(zc
->zc_name
, dataset
);
3845 pair
= nvlist_next_nvpair(props
, NULL
);
3846 while (pair
!= NULL
) {
3847 next_pair
= nvlist_next_nvpair(props
, pair
);
3849 (void) strcpy(zc
->zc_value
, nvpair_name(pair
));
3850 if ((err
= zfs_check_settable(dataset
, pair
, CRED())) != 0 ||
3851 (err
= zfs_secpolicy_inherit_prop(zc
, NULL
, CRED())) != 0) {
3852 VERIFY(nvlist_remove_nvpair(props
, pair
) == 0);
3853 VERIFY(nvlist_add_int32(errors
,
3854 zc
->zc_value
, err
) == 0);
3858 kmem_free(zc
, sizeof (zfs_cmd_t
));
3860 if ((pair
= nvlist_next_nvpair(errors
, NULL
)) == NULL
) {
3861 nvlist_free(errors
);
3864 VERIFY(nvpair_value_int32(pair
, &rv
) == 0);
3867 if (errlist
== NULL
)
3868 nvlist_free(errors
);
3876 propval_equals(nvpair_t
*p1
, nvpair_t
*p2
)
3878 if (nvpair_type(p1
) == DATA_TYPE_NVLIST
) {
3879 /* dsl_prop_get_all_impl() format */
3881 VERIFY(nvpair_value_nvlist(p1
, &attrs
) == 0);
3882 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
3886 if (nvpair_type(p2
) == DATA_TYPE_NVLIST
) {
3888 VERIFY(nvpair_value_nvlist(p2
, &attrs
) == 0);
3889 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
3893 if (nvpair_type(p1
) != nvpair_type(p2
))
3896 if (nvpair_type(p1
) == DATA_TYPE_STRING
) {
3897 char *valstr1
, *valstr2
;
3899 VERIFY(nvpair_value_string(p1
, (char **)&valstr1
) == 0);
3900 VERIFY(nvpair_value_string(p2
, (char **)&valstr2
) == 0);
3901 return (strcmp(valstr1
, valstr2
) == 0);
3903 uint64_t intval1
, intval2
;
3905 VERIFY(nvpair_value_uint64(p1
, &intval1
) == 0);
3906 VERIFY(nvpair_value_uint64(p2
, &intval2
) == 0);
3907 return (intval1
== intval2
);
3912 * Remove properties from props if they are not going to change (as determined
3913 * by comparison with origprops). Remove them from origprops as well, since we
3914 * do not need to clear or restore properties that won't change.
3917 props_reduce(nvlist_t
*props
, nvlist_t
*origprops
)
3919 nvpair_t
*pair
, *next_pair
;
3921 if (origprops
== NULL
)
3922 return; /* all props need to be received */
3924 pair
= nvlist_next_nvpair(props
, NULL
);
3925 while (pair
!= NULL
) {
3926 const char *propname
= nvpair_name(pair
);
3929 next_pair
= nvlist_next_nvpair(props
, pair
);
3931 if ((nvlist_lookup_nvpair(origprops
, propname
,
3932 &match
) != 0) || !propval_equals(pair
, match
))
3933 goto next
; /* need to set received value */
3935 /* don't clear the existing received value */
3936 (void) nvlist_remove_nvpair(origprops
, match
);
3937 /* don't bother receiving the property */
3938 (void) nvlist_remove_nvpair(props
, pair
);
3945 static boolean_t zfs_ioc_recv_inject_err
;
3950 * zc_name name of containing filesystem
3951 * zc_nvlist_src{_size} nvlist of properties to apply
3952 * zc_value name of snapshot to create
3953 * zc_string name of clone origin (if DRR_FLAG_CLONE)
3954 * zc_cookie file descriptor to recv from
3955 * zc_begin_record the BEGIN record of the stream (not byteswapped)
3956 * zc_guid force flag
3957 * zc_cleanup_fd cleanup-on-exit file descriptor
3958 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
3961 * zc_cookie number of bytes read
3962 * zc_nvlist_dst{_size} error for each unapplied received property
3963 * zc_obj zprop_errflags_t
3964 * zc_action_handle handle for this guid/ds mapping
3967 zfs_ioc_recv(zfs_cmd_t
*zc
)
3970 dmu_recv_cookie_t drc
;
3971 boolean_t force
= (boolean_t
)zc
->zc_guid
;
3974 int props_error
= 0;
3977 nvlist_t
*props
= NULL
; /* sent properties */
3978 nvlist_t
*origprops
= NULL
; /* existing properties */
3979 char *origin
= NULL
;
3981 char tofs
[ZFS_MAXNAMELEN
];
3982 boolean_t first_recvd_props
= B_FALSE
;
3984 if (dataset_namecheck(zc
->zc_value
, NULL
, NULL
) != 0 ||
3985 strchr(zc
->zc_value
, '@') == NULL
||
3986 strchr(zc
->zc_value
, '%'))
3987 return (SET_ERROR(EINVAL
));
3989 (void) strcpy(tofs
, zc
->zc_value
);
3990 tosnap
= strchr(tofs
, '@');
3993 if (zc
->zc_nvlist_src
!= 0 &&
3994 (error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
3995 zc
->zc_iflags
, &props
)) != 0)
4002 return (SET_ERROR(EBADF
));
4005 VERIFY(nvlist_alloc(&errors
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4007 if (zc
->zc_string
[0])
4008 origin
= zc
->zc_string
;
4010 error
= dmu_recv_begin(tofs
, tosnap
,
4011 &zc
->zc_begin_record
, force
, origin
, &drc
);
4016 * Set properties before we receive the stream so that they are applied
4017 * to the new data. Note that we must call dmu_recv_stream() if
4018 * dmu_recv_begin() succeeds.
4020 if (props
!= NULL
&& !drc
.drc_newfs
) {
4021 if (spa_version(dsl_dataset_get_spa(drc
.drc_ds
)) >=
4022 SPA_VERSION_RECVD_PROPS
&&
4023 !dsl_prop_get_hasrecvd(tofs
))
4024 first_recvd_props
= B_TRUE
;
4027 * If new received properties are supplied, they are to
4028 * completely replace the existing received properties, so stash
4029 * away the existing ones.
4031 if (dsl_prop_get_received(tofs
, &origprops
) == 0) {
4032 nvlist_t
*errlist
= NULL
;
4034 * Don't bother writing a property if its value won't
4035 * change (and avoid the unnecessary security checks).
4037 * The first receive after SPA_VERSION_RECVD_PROPS is a
4038 * special case where we blow away all local properties
4041 if (!first_recvd_props
)
4042 props_reduce(props
, origprops
);
4043 if (zfs_check_clearable(tofs
, origprops
, &errlist
) != 0)
4044 (void) nvlist_merge(errors
, errlist
, 0);
4045 nvlist_free(errlist
);
4047 if (clear_received_props(tofs
, origprops
,
4048 first_recvd_props
? NULL
: props
) != 0)
4049 zc
->zc_obj
|= ZPROP_ERR_NOCLEAR
;
4051 zc
->zc_obj
|= ZPROP_ERR_NOCLEAR
;
4055 if (props
!= NULL
) {
4056 props_error
= dsl_prop_set_hasrecvd(tofs
);
4058 if (props_error
== 0) {
4059 (void) zfs_set_prop_nvlist(tofs
, ZPROP_SRC_RECEIVED
,
4064 if (zc
->zc_nvlist_dst_size
!= 0 &&
4065 (nvlist_smush(errors
, zc
->zc_nvlist_dst_size
) != 0 ||
4066 put_nvlist(zc
, errors
) != 0)) {
4068 * Caller made zc->zc_nvlist_dst less than the minimum expected
4069 * size or supplied an invalid address.
4071 props_error
= SET_ERROR(EINVAL
);
4075 error
= dmu_recv_stream(&drc
, fp
->f_vnode
, &off
, zc
->zc_cleanup_fd
,
4076 &zc
->zc_action_handle
);
4079 zfs_sb_t
*zsb
= NULL
;
4081 if (get_zfs_sb(tofs
, &zsb
) == 0) {
4085 error
= zfs_suspend_fs(zsb
);
4087 * If the suspend fails, then the recv_end will
4088 * likely also fail, and clean up after itself.
4090 end_err
= dmu_recv_end(&drc
, zsb
);
4092 error
= zfs_resume_fs(zsb
, tofs
);
4093 error
= error
? error
: end_err
;
4094 deactivate_super(zsb
->z_sb
);
4096 error
= dmu_recv_end(&drc
, NULL
);
4100 zc
->zc_cookie
= off
- fp
->f_offset
;
4101 if (VOP_SEEK(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
4105 if (zfs_ioc_recv_inject_err
) {
4106 zfs_ioc_recv_inject_err
= B_FALSE
;
4112 * On error, restore the original props.
4114 if (error
!= 0 && props
!= NULL
&& !drc
.drc_newfs
) {
4115 if (clear_received_props(tofs
, props
, NULL
) != 0) {
4117 * We failed to clear the received properties.
4118 * Since we may have left a $recvd value on the
4119 * system, we can't clear the $hasrecvd flag.
4121 zc
->zc_obj
|= ZPROP_ERR_NORESTORE
;
4122 } else if (first_recvd_props
) {
4123 dsl_prop_unset_hasrecvd(tofs
);
4126 if (origprops
== NULL
&& !drc
.drc_newfs
) {
4127 /* We failed to stash the original properties. */
4128 zc
->zc_obj
|= ZPROP_ERR_NORESTORE
;
4132 * dsl_props_set() will not convert RECEIVED to LOCAL on or
4133 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
4134 * explictly if we're restoring local properties cleared in the
4135 * first new-style receive.
4137 if (origprops
!= NULL
&&
4138 zfs_set_prop_nvlist(tofs
, (first_recvd_props
?
4139 ZPROP_SRC_LOCAL
: ZPROP_SRC_RECEIVED
),
4140 origprops
, NULL
) != 0) {
4142 * We stashed the original properties but failed to
4145 zc
->zc_obj
|= ZPROP_ERR_NORESTORE
;
4150 nvlist_free(origprops
);
4151 nvlist_free(errors
);
4155 error
= props_error
;
4162 * zc_name name of snapshot to send
4163 * zc_cookie file descriptor to send stream to
4164 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
4165 * zc_sendobj objsetid of snapshot to send
4166 * zc_fromobj objsetid of incremental fromsnap (may be zero)
4167 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
4168 * output size in zc_objset_type.
4169 * zc_flags lzc_send_flags
4172 * zc_objset_type estimated size, if zc_guid is set
4175 zfs_ioc_send(zfs_cmd_t
*zc
)
4179 boolean_t estimate
= (zc
->zc_guid
!= 0);
4180 boolean_t embedok
= (zc
->zc_flags
& 0x1);
4181 boolean_t large_block_ok
= (zc
->zc_flags
& 0x2);
4183 if (zc
->zc_obj
!= 0) {
4185 dsl_dataset_t
*tosnap
;
4187 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
4191 error
= dsl_dataset_hold_obj(dp
, zc
->zc_sendobj
, FTAG
, &tosnap
);
4193 dsl_pool_rele(dp
, FTAG
);
4197 if (dsl_dir_is_clone(tosnap
->ds_dir
))
4199 dsl_dir_phys(tosnap
->ds_dir
)->dd_origin_obj
;
4200 dsl_dataset_rele(tosnap
, FTAG
);
4201 dsl_pool_rele(dp
, FTAG
);
4206 dsl_dataset_t
*tosnap
;
4207 dsl_dataset_t
*fromsnap
= NULL
;
4209 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
4213 error
= dsl_dataset_hold_obj(dp
, zc
->zc_sendobj
, FTAG
, &tosnap
);
4215 dsl_pool_rele(dp
, FTAG
);
4219 if (zc
->zc_fromobj
!= 0) {
4220 error
= dsl_dataset_hold_obj(dp
, zc
->zc_fromobj
,
4223 dsl_dataset_rele(tosnap
, FTAG
);
4224 dsl_pool_rele(dp
, FTAG
);
4229 error
= dmu_send_estimate(tosnap
, fromsnap
,
4230 &zc
->zc_objset_type
);
4232 if (fromsnap
!= NULL
)
4233 dsl_dataset_rele(fromsnap
, FTAG
);
4234 dsl_dataset_rele(tosnap
, FTAG
);
4235 dsl_pool_rele(dp
, FTAG
);
4237 file_t
*fp
= getf(zc
->zc_cookie
);
4239 return (SET_ERROR(EBADF
));
4242 error
= dmu_send_obj(zc
->zc_name
, zc
->zc_sendobj
,
4243 zc
->zc_fromobj
, embedok
, large_block_ok
,
4244 zc
->zc_cookie
, fp
->f_vnode
, &off
);
4246 if (VOP_SEEK(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
4248 releasef(zc
->zc_cookie
);
4255 * zc_name name of snapshot on which to report progress
4256 * zc_cookie file descriptor of send stream
4259 * zc_cookie number of bytes written in send stream thus far
4262 zfs_ioc_send_progress(zfs_cmd_t
*zc
)
4266 dmu_sendarg_t
*dsp
= NULL
;
4269 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
4273 error
= dsl_dataset_hold(dp
, zc
->zc_name
, FTAG
, &ds
);
4275 dsl_pool_rele(dp
, FTAG
);
4279 mutex_enter(&ds
->ds_sendstream_lock
);
4282 * Iterate over all the send streams currently active on this dataset.
4283 * If there's one which matches the specified file descriptor _and_ the
4284 * stream was started by the current process, return the progress of
4288 for (dsp
= list_head(&ds
->ds_sendstreams
); dsp
!= NULL
;
4289 dsp
= list_next(&ds
->ds_sendstreams
, dsp
)) {
4290 if (dsp
->dsa_outfd
== zc
->zc_cookie
&&
4291 dsp
->dsa_proc
->group_leader
== curproc
->group_leader
)
4296 zc
->zc_cookie
= *(dsp
->dsa_off
);
4298 error
= SET_ERROR(ENOENT
);
4300 mutex_exit(&ds
->ds_sendstream_lock
);
4301 dsl_dataset_rele(ds
, FTAG
);
4302 dsl_pool_rele(dp
, FTAG
);
4307 zfs_ioc_inject_fault(zfs_cmd_t
*zc
)
4311 error
= zio_inject_fault(zc
->zc_name
, (int)zc
->zc_guid
, &id
,
4312 &zc
->zc_inject_record
);
4315 zc
->zc_guid
= (uint64_t)id
;
4321 zfs_ioc_clear_fault(zfs_cmd_t
*zc
)
4323 return (zio_clear_fault((int)zc
->zc_guid
));
4327 zfs_ioc_inject_list_next(zfs_cmd_t
*zc
)
4329 int id
= (int)zc
->zc_guid
;
4332 error
= zio_inject_list_next(&id
, zc
->zc_name
, sizeof (zc
->zc_name
),
4333 &zc
->zc_inject_record
);
4341 zfs_ioc_error_log(zfs_cmd_t
*zc
)
4345 size_t count
= (size_t)zc
->zc_nvlist_dst_size
;
4347 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
4350 error
= spa_get_errlog(spa
, (void *)(uintptr_t)zc
->zc_nvlist_dst
,
4353 zc
->zc_nvlist_dst_size
= count
;
4355 zc
->zc_nvlist_dst_size
= spa_get_errlog_size(spa
);
4357 spa_close(spa
, FTAG
);
4363 zfs_ioc_clear(zfs_cmd_t
*zc
)
4370 * On zpool clear we also fix up missing slogs
4372 mutex_enter(&spa_namespace_lock
);
4373 spa
= spa_lookup(zc
->zc_name
);
4375 mutex_exit(&spa_namespace_lock
);
4376 return (SET_ERROR(EIO
));
4378 if (spa_get_log_state(spa
) == SPA_LOG_MISSING
) {
4379 /* we need to let spa_open/spa_load clear the chains */
4380 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
4382 spa
->spa_last_open_failed
= 0;
4383 mutex_exit(&spa_namespace_lock
);
4385 if (zc
->zc_cookie
& ZPOOL_NO_REWIND
) {
4386 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
4389 nvlist_t
*config
= NULL
;
4391 if (zc
->zc_nvlist_src
== 0)
4392 return (SET_ERROR(EINVAL
));
4394 if ((error
= get_nvlist(zc
->zc_nvlist_src
,
4395 zc
->zc_nvlist_src_size
, zc
->zc_iflags
, &policy
)) == 0) {
4396 error
= spa_open_rewind(zc
->zc_name
, &spa
, FTAG
,
4398 if (config
!= NULL
) {
4401 if ((err
= put_nvlist(zc
, config
)) != 0)
4403 nvlist_free(config
);
4405 nvlist_free(policy
);
4412 spa_vdev_state_enter(spa
, SCL_NONE
);
4414 if (zc
->zc_guid
== 0) {
4417 vd
= spa_lookup_by_guid(spa
, zc
->zc_guid
, B_TRUE
);
4419 (void) spa_vdev_state_exit(spa
, NULL
, ENODEV
);
4420 spa_close(spa
, FTAG
);
4421 return (SET_ERROR(ENODEV
));
4425 vdev_clear(spa
, vd
);
4427 (void) spa_vdev_state_exit(spa
, NULL
, 0);
4430 * Resume any suspended I/Os.
4432 if (zio_resume(spa
) != 0)
4433 error
= SET_ERROR(EIO
);
4435 spa_close(spa
, FTAG
);
4441 zfs_ioc_pool_reopen(zfs_cmd_t
*zc
)
4446 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
4450 spa_vdev_state_enter(spa
, SCL_NONE
);
4453 * If a resilver is already in progress then set the
4454 * spa_scrub_reopen flag to B_TRUE so that we don't restart
4455 * the scan as a side effect of the reopen. Otherwise, let
4456 * vdev_open() decided if a resilver is required.
4458 spa
->spa_scrub_reopen
= dsl_scan_resilvering(spa
->spa_dsl_pool
);
4459 vdev_reopen(spa
->spa_root_vdev
);
4460 spa
->spa_scrub_reopen
= B_FALSE
;
4462 (void) spa_vdev_state_exit(spa
, NULL
, 0);
4463 spa_close(spa
, FTAG
);
4468 * zc_name name of filesystem
4469 * zc_value name of origin snapshot
4472 * zc_string name of conflicting snapshot, if there is one
4475 zfs_ioc_promote(zfs_cmd_t
*zc
)
4480 * We don't need to unmount *all* the origin fs's snapshots, but
4483 cp
= strchr(zc
->zc_value
, '@');
4486 (void) dmu_objset_find(zc
->zc_value
,
4487 zfs_unmount_snap_cb
, NULL
, DS_FIND_SNAPSHOTS
);
4488 return (dsl_dataset_promote(zc
->zc_name
, zc
->zc_string
));
4492 * Retrieve a single {user|group}{used|quota}@... property.
4495 * zc_name name of filesystem
4496 * zc_objset_type zfs_userquota_prop_t
4497 * zc_value domain name (eg. "S-1-234-567-89")
4498 * zc_guid RID/UID/GID
4501 * zc_cookie property value
4504 zfs_ioc_userspace_one(zfs_cmd_t
*zc
)
4509 if (zc
->zc_objset_type
>= ZFS_NUM_USERQUOTA_PROPS
)
4510 return (SET_ERROR(EINVAL
));
4512 error
= zfs_sb_hold(zc
->zc_name
, FTAG
, &zsb
, B_FALSE
);
4516 error
= zfs_userspace_one(zsb
,
4517 zc
->zc_objset_type
, zc
->zc_value
, zc
->zc_guid
, &zc
->zc_cookie
);
4518 zfs_sb_rele(zsb
, FTAG
);
4525 * zc_name name of filesystem
4526 * zc_cookie zap cursor
4527 * zc_objset_type zfs_userquota_prop_t
4528 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
4531 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
4532 * zc_cookie zap cursor
4535 zfs_ioc_userspace_many(zfs_cmd_t
*zc
)
4538 int bufsize
= zc
->zc_nvlist_dst_size
;
4543 return (SET_ERROR(ENOMEM
));
4545 error
= zfs_sb_hold(zc
->zc_name
, FTAG
, &zsb
, B_FALSE
);
4549 buf
= vmem_alloc(bufsize
, KM_SLEEP
);
4551 error
= zfs_userspace_many(zsb
, zc
->zc_objset_type
, &zc
->zc_cookie
,
4552 buf
, &zc
->zc_nvlist_dst_size
);
4555 error
= xcopyout(buf
,
4556 (void *)(uintptr_t)zc
->zc_nvlist_dst
,
4557 zc
->zc_nvlist_dst_size
);
4559 vmem_free(buf
, bufsize
);
4560 zfs_sb_rele(zsb
, FTAG
);
4567 * zc_name name of filesystem
4573 zfs_ioc_userspace_upgrade(zfs_cmd_t
*zc
)
4579 if (get_zfs_sb(zc
->zc_name
, &zsb
) == 0) {
4580 if (!dmu_objset_userused_enabled(zsb
->z_os
)) {
4582 * If userused is not enabled, it may be because the
4583 * objset needs to be closed & reopened (to grow the
4584 * objset_phys_t). Suspend/resume the fs will do that.
4586 error
= zfs_suspend_fs(zsb
);
4588 dmu_objset_refresh_ownership(zsb
->z_os
,
4590 error
= zfs_resume_fs(zsb
, zc
->zc_name
);
4594 error
= dmu_objset_userspace_upgrade(zsb
->z_os
);
4595 deactivate_super(zsb
->z_sb
);
4597 /* XXX kind of reading contents without owning */
4598 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
4602 error
= dmu_objset_userspace_upgrade(os
);
4603 dmu_objset_rele(os
, FTAG
);
4610 zfs_ioc_share(zfs_cmd_t
*zc
)
4612 return (SET_ERROR(ENOSYS
));
4615 ace_t full_access
[] = {
4616 {(uid_t
)-1, ACE_ALL_PERMS
, ACE_EVERYONE
, 0}
4621 * zc_name name of containing filesystem
4622 * zc_obj object # beyond which we want next in-use object #
4625 * zc_obj next in-use object #
4628 zfs_ioc_next_obj(zfs_cmd_t
*zc
)
4630 objset_t
*os
= NULL
;
4633 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
4637 error
= dmu_object_next(os
, &zc
->zc_obj
, B_FALSE
, 0);
4639 dmu_objset_rele(os
, FTAG
);
4645 * zc_name name of filesystem
4646 * zc_value prefix name for snapshot
4647 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
4650 * zc_value short name of new snapshot
4653 zfs_ioc_tmp_snapshot(zfs_cmd_t
*zc
)
4660 error
= zfs_onexit_fd_hold(zc
->zc_cleanup_fd
, &minor
);
4664 snap_name
= kmem_asprintf("%s-%016llx", zc
->zc_value
,
4665 (u_longlong_t
)ddi_get_lbolt64());
4666 hold_name
= kmem_asprintf("%%%s", zc
->zc_value
);
4668 error
= dsl_dataset_snapshot_tmp(zc
->zc_name
, snap_name
, minor
,
4671 (void) strcpy(zc
->zc_value
, snap_name
);
4674 zfs_onexit_fd_rele(zc
->zc_cleanup_fd
);
4680 * zc_name name of "to" snapshot
4681 * zc_value name of "from" snapshot
4682 * zc_cookie file descriptor to write diff data on
4685 * dmu_diff_record_t's to the file descriptor
4688 zfs_ioc_diff(zfs_cmd_t
*zc
)
4694 fp
= getf(zc
->zc_cookie
);
4696 return (SET_ERROR(EBADF
));
4700 error
= dmu_diff(zc
->zc_name
, zc
->zc_value
, fp
->f_vnode
, &off
);
4702 if (VOP_SEEK(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
4704 releasef(zc
->zc_cookie
);
4710 * Remove all ACL files in shares dir
4712 #ifdef HAVE_SMB_SHARE
4714 zfs_smb_acl_purge(znode_t
*dzp
)
4717 zap_attribute_t zap
;
4718 zfs_sb_t
*zsb
= ZTOZSB(dzp
);
4721 for (zap_cursor_init(&zc
, zsb
->z_os
, dzp
->z_id
);
4722 (error
= zap_cursor_retrieve(&zc
, &zap
)) == 0;
4723 zap_cursor_advance(&zc
)) {
4724 if ((error
= VOP_REMOVE(ZTOV(dzp
), zap
.za_name
, kcred
,
4728 zap_cursor_fini(&zc
);
4731 #endif /* HAVE_SMB_SHARE */
4734 zfs_ioc_smb_acl(zfs_cmd_t
*zc
)
4736 #ifdef HAVE_SMB_SHARE
4739 vnode_t
*resourcevp
= NULL
;
4748 if ((error
= lookupname(zc
->zc_value
, UIO_SYSSPACE
,
4749 NO_FOLLOW
, NULL
, &vp
)) != 0)
4752 /* Now make sure mntpnt and dataset are ZFS */
4754 if (vp
->v_vfsp
->vfs_fstype
!= zfsfstype
||
4755 (strcmp((char *)refstr_value(vp
->v_vfsp
->vfs_resource
),
4756 zc
->zc_name
) != 0)) {
4758 return (SET_ERROR(EINVAL
));
4766 * Create share dir if its missing.
4768 mutex_enter(&zsb
->z_lock
);
4769 if (zsb
->z_shares_dir
== 0) {
4772 tx
= dmu_tx_create(zsb
->z_os
);
4773 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
, TRUE
,
4775 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, FALSE
, NULL
);
4776 error
= dmu_tx_assign(tx
, TXG_WAIT
);
4780 error
= zfs_create_share_dir(zsb
, tx
);
4784 mutex_exit(&zsb
->z_lock
);
4790 mutex_exit(&zsb
->z_lock
);
4792 ASSERT(zsb
->z_shares_dir
);
4793 if ((error
= zfs_zget(zsb
, zsb
->z_shares_dir
, &sharedir
)) != 0) {
4799 switch (zc
->zc_cookie
) {
4800 case ZFS_SMB_ACL_ADD
:
4801 vattr
.va_mask
= AT_MODE
|AT_UID
|AT_GID
|AT_TYPE
;
4802 vattr
.va_mode
= S_IFREG
|0777;
4806 vsec
.vsa_mask
= VSA_ACE
;
4807 vsec
.vsa_aclentp
= &full_access
;
4808 vsec
.vsa_aclentsz
= sizeof (full_access
);
4809 vsec
.vsa_aclcnt
= 1;
4811 error
= VOP_CREATE(ZTOV(sharedir
), zc
->zc_string
,
4812 &vattr
, EXCL
, 0, &resourcevp
, kcred
, 0, NULL
, &vsec
);
4814 VN_RELE(resourcevp
);
4817 case ZFS_SMB_ACL_REMOVE
:
4818 error
= VOP_REMOVE(ZTOV(sharedir
), zc
->zc_string
, kcred
,
4822 case ZFS_SMB_ACL_RENAME
:
4823 if ((error
= get_nvlist(zc
->zc_nvlist_src
,
4824 zc
->zc_nvlist_src_size
, zc
->zc_iflags
, &nvlist
)) != 0) {
4829 if (nvlist_lookup_string(nvlist
, ZFS_SMB_ACL_SRC
, &src
) ||
4830 nvlist_lookup_string(nvlist
, ZFS_SMB_ACL_TARGET
,
4833 VN_RELE(ZTOV(sharedir
));
4835 nvlist_free(nvlist
);
4838 error
= VOP_RENAME(ZTOV(sharedir
), src
, ZTOV(sharedir
), target
,
4840 nvlist_free(nvlist
);
4843 case ZFS_SMB_ACL_PURGE
:
4844 error
= zfs_smb_acl_purge(sharedir
);
4848 error
= SET_ERROR(EINVAL
);
4853 VN_RELE(ZTOV(sharedir
));
4859 return (SET_ERROR(ENOTSUP
));
4860 #endif /* HAVE_SMB_SHARE */
4865 * "holds" -> { snapname -> holdname (string), ... }
4866 * (optional) "cleanup_fd" -> fd (int32)
4870 * snapname -> error value (int32)
4876 zfs_ioc_hold(const char *pool
, nvlist_t
*args
, nvlist_t
*errlist
)
4879 int cleanup_fd
= -1;
4883 error
= nvlist_lookup_nvlist(args
, "holds", &holds
);
4885 return (SET_ERROR(EINVAL
));
4887 if (nvlist_lookup_int32(args
, "cleanup_fd", &cleanup_fd
) == 0) {
4888 error
= zfs_onexit_fd_hold(cleanup_fd
, &minor
);
4893 error
= dsl_dataset_user_hold(holds
, minor
, errlist
);
4895 zfs_onexit_fd_rele(cleanup_fd
);
4900 * innvl is not used.
4903 * holdname -> time added (uint64 seconds since epoch)
4909 zfs_ioc_get_holds(const char *snapname
, nvlist_t
*args
, nvlist_t
*outnvl
)
4911 return (dsl_dataset_get_holds(snapname
, outnvl
));
4916 * snapname -> { holdname, ... }
4921 * snapname -> error value (int32)
4927 zfs_ioc_release(const char *pool
, nvlist_t
*holds
, nvlist_t
*errlist
)
4929 return (dsl_dataset_user_release(holds
, errlist
));
4934 * zc_guid flags (ZEVENT_NONBLOCK)
4935 * zc_cleanup_fd zevent file descriptor
4938 * zc_nvlist_dst next nvlist event
4939 * zc_cookie dropped events since last get
4942 zfs_ioc_events_next(zfs_cmd_t
*zc
)
4945 nvlist_t
*event
= NULL
;
4947 uint64_t dropped
= 0;
4950 error
= zfs_zevent_fd_hold(zc
->zc_cleanup_fd
, &minor
, &ze
);
4955 error
= zfs_zevent_next(ze
, &event
,
4956 &zc
->zc_nvlist_dst_size
, &dropped
);
4957 if (event
!= NULL
) {
4958 zc
->zc_cookie
= dropped
;
4959 error
= put_nvlist(zc
, event
);
4963 if (zc
->zc_guid
& ZEVENT_NONBLOCK
)
4966 if ((error
== 0) || (error
!= ENOENT
))
4969 error
= zfs_zevent_wait(ze
);
4974 zfs_zevent_fd_rele(zc
->zc_cleanup_fd
);
4981 * zc_cookie cleared events count
4984 zfs_ioc_events_clear(zfs_cmd_t
*zc
)
4988 zfs_zevent_drain_all(&count
);
4989 zc
->zc_cookie
= count
;
4996 * zc_guid eid | ZEVENT_SEEK_START | ZEVENT_SEEK_END
4997 * zc_cleanup zevent file descriptor
5000 zfs_ioc_events_seek(zfs_cmd_t
*zc
)
5006 error
= zfs_zevent_fd_hold(zc
->zc_cleanup_fd
, &minor
, &ze
);
5010 error
= zfs_zevent_seek(ze
, zc
->zc_guid
);
5011 zfs_zevent_fd_rele(zc
->zc_cleanup_fd
);
5018 * zc_name name of new filesystem or snapshot
5019 * zc_value full name of old snapshot
5022 * zc_cookie space in bytes
5023 * zc_objset_type compressed space in bytes
5024 * zc_perm_action uncompressed space in bytes
5027 zfs_ioc_space_written(zfs_cmd_t
*zc
)
5031 dsl_dataset_t
*new, *old
;
5033 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
5036 error
= dsl_dataset_hold(dp
, zc
->zc_name
, FTAG
, &new);
5038 dsl_pool_rele(dp
, FTAG
);
5041 error
= dsl_dataset_hold(dp
, zc
->zc_value
, FTAG
, &old
);
5043 dsl_dataset_rele(new, FTAG
);
5044 dsl_pool_rele(dp
, FTAG
);
5048 error
= dsl_dataset_space_written(old
, new, &zc
->zc_cookie
,
5049 &zc
->zc_objset_type
, &zc
->zc_perm_action
);
5050 dsl_dataset_rele(old
, FTAG
);
5051 dsl_dataset_rele(new, FTAG
);
5052 dsl_pool_rele(dp
, FTAG
);
5058 * "firstsnap" -> snapshot name
5062 * "used" -> space in bytes
5063 * "compressed" -> compressed space in bytes
5064 * "uncompressed" -> uncompressed space in bytes
5068 zfs_ioc_space_snaps(const char *lastsnap
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
5072 dsl_dataset_t
*new, *old
;
5074 uint64_t used
, comp
, uncomp
;
5076 if (nvlist_lookup_string(innvl
, "firstsnap", &firstsnap
) != 0)
5077 return (SET_ERROR(EINVAL
));
5079 error
= dsl_pool_hold(lastsnap
, FTAG
, &dp
);
5083 error
= dsl_dataset_hold(dp
, lastsnap
, FTAG
, &new);
5084 if (error
== 0 && !new->ds_is_snapshot
) {
5085 dsl_dataset_rele(new, FTAG
);
5086 error
= SET_ERROR(EINVAL
);
5089 dsl_pool_rele(dp
, FTAG
);
5092 error
= dsl_dataset_hold(dp
, firstsnap
, FTAG
, &old
);
5093 if (error
== 0 && !old
->ds_is_snapshot
) {
5094 dsl_dataset_rele(old
, FTAG
);
5095 error
= SET_ERROR(EINVAL
);
5098 dsl_dataset_rele(new, FTAG
);
5099 dsl_pool_rele(dp
, FTAG
);
5103 error
= dsl_dataset_space_wouldfree(old
, new, &used
, &comp
, &uncomp
);
5104 dsl_dataset_rele(old
, FTAG
);
5105 dsl_dataset_rele(new, FTAG
);
5106 dsl_pool_rele(dp
, FTAG
);
5107 fnvlist_add_uint64(outnvl
, "used", used
);
5108 fnvlist_add_uint64(outnvl
, "compressed", comp
);
5109 fnvlist_add_uint64(outnvl
, "uncompressed", uncomp
);
5115 * "fd" -> file descriptor to write stream to (int32)
5116 * (optional) "fromsnap" -> full snap name to send an incremental from
5117 * (optional) "largeblockok" -> (value ignored)
5118 * indicates that blocks > 128KB are permitted
5119 * (optional) "embedok" -> (value ignored)
5120 * presence indicates DRR_WRITE_EMBEDDED records are permitted
5127 zfs_ioc_send_new(const char *snapname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
5131 char *fromname
= NULL
;
5134 boolean_t largeblockok
;
5137 error
= nvlist_lookup_int32(innvl
, "fd", &fd
);
5139 return (SET_ERROR(EINVAL
));
5141 (void) nvlist_lookup_string(innvl
, "fromsnap", &fromname
);
5143 largeblockok
= nvlist_exists(innvl
, "largeblockok");
5144 embedok
= nvlist_exists(innvl
, "embedok");
5146 if ((fp
= getf(fd
)) == NULL
)
5147 return (SET_ERROR(EBADF
));
5150 error
= dmu_send(snapname
, fromname
, embedok
, largeblockok
,
5151 fd
, fp
->f_vnode
, &off
);
5153 if (VOP_SEEK(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
5161 * Determine approximately how large a zfs send stream will be -- the number
5162 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
5165 * (optional) "from" -> full snap or bookmark name to send an incremental
5170 * "space" -> bytes of space (uint64)
5174 zfs_ioc_send_space(const char *snapname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
5177 dsl_dataset_t
*tosnap
;
5182 error
= dsl_pool_hold(snapname
, FTAG
, &dp
);
5186 error
= dsl_dataset_hold(dp
, snapname
, FTAG
, &tosnap
);
5188 dsl_pool_rele(dp
, FTAG
);
5192 error
= nvlist_lookup_string(innvl
, "from", &fromname
);
5194 if (strchr(fromname
, '@') != NULL
) {
5196 * If from is a snapshot, hold it and use the more
5197 * efficient dmu_send_estimate to estimate send space
5198 * size using deadlists.
5200 dsl_dataset_t
*fromsnap
;
5201 error
= dsl_dataset_hold(dp
, fromname
, FTAG
, &fromsnap
);
5204 error
= dmu_send_estimate(tosnap
, fromsnap
, &space
);
5205 dsl_dataset_rele(fromsnap
, FTAG
);
5206 } else if (strchr(fromname
, '#') != NULL
) {
5208 * If from is a bookmark, fetch the creation TXG of the
5209 * snapshot it was created from and use that to find
5210 * blocks that were born after it.
5212 zfs_bookmark_phys_t frombm
;
5214 error
= dsl_bookmark_lookup(dp
, fromname
, tosnap
,
5218 error
= dmu_send_estimate_from_txg(tosnap
,
5219 frombm
.zbm_creation_txg
, &space
);
5222 * from is not properly formatted as a snapshot or
5225 error
= SET_ERROR(EINVAL
);
5229 // If estimating the size of a full send, use dmu_send_estimate
5230 error
= dmu_send_estimate(tosnap
, NULL
, &space
);
5233 fnvlist_add_uint64(outnvl
, "space", space
);
5236 dsl_dataset_rele(tosnap
, FTAG
);
5237 dsl_pool_rele(dp
, FTAG
);
5241 static zfs_ioc_vec_t zfs_ioc_vec
[ZFS_IOC_LAST
- ZFS_IOC_FIRST
];
5244 zfs_ioctl_register_legacy(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5245 zfs_secpolicy_func_t
*secpolicy
, zfs_ioc_namecheck_t namecheck
,
5246 boolean_t log_history
, zfs_ioc_poolcheck_t pool_check
)
5248 zfs_ioc_vec_t
*vec
= &zfs_ioc_vec
[ioc
- ZFS_IOC_FIRST
];
5250 ASSERT3U(ioc
, >=, ZFS_IOC_FIRST
);
5251 ASSERT3U(ioc
, <, ZFS_IOC_LAST
);
5252 ASSERT3P(vec
->zvec_legacy_func
, ==, NULL
);
5253 ASSERT3P(vec
->zvec_func
, ==, NULL
);
5255 vec
->zvec_legacy_func
= func
;
5256 vec
->zvec_secpolicy
= secpolicy
;
5257 vec
->zvec_namecheck
= namecheck
;
5258 vec
->zvec_allow_log
= log_history
;
5259 vec
->zvec_pool_check
= pool_check
;
5263 * See the block comment at the beginning of this file for details on
5264 * each argument to this function.
5267 zfs_ioctl_register(const char *name
, zfs_ioc_t ioc
, zfs_ioc_func_t
*func
,
5268 zfs_secpolicy_func_t
*secpolicy
, zfs_ioc_namecheck_t namecheck
,
5269 zfs_ioc_poolcheck_t pool_check
, boolean_t smush_outnvlist
,
5270 boolean_t allow_log
)
5272 zfs_ioc_vec_t
*vec
= &zfs_ioc_vec
[ioc
- ZFS_IOC_FIRST
];
5274 ASSERT3U(ioc
, >=, ZFS_IOC_FIRST
);
5275 ASSERT3U(ioc
, <, ZFS_IOC_LAST
);
5276 ASSERT3P(vec
->zvec_legacy_func
, ==, NULL
);
5277 ASSERT3P(vec
->zvec_func
, ==, NULL
);
5279 /* if we are logging, the name must be valid */
5280 ASSERT(!allow_log
|| namecheck
!= NO_NAME
);
5282 vec
->zvec_name
= name
;
5283 vec
->zvec_func
= func
;
5284 vec
->zvec_secpolicy
= secpolicy
;
5285 vec
->zvec_namecheck
= namecheck
;
5286 vec
->zvec_pool_check
= pool_check
;
5287 vec
->zvec_smush_outnvlist
= smush_outnvlist
;
5288 vec
->zvec_allow_log
= allow_log
;
5292 zfs_ioctl_register_pool(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5293 zfs_secpolicy_func_t
*secpolicy
, boolean_t log_history
,
5294 zfs_ioc_poolcheck_t pool_check
)
5296 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5297 POOL_NAME
, log_history
, pool_check
);
5301 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5302 zfs_secpolicy_func_t
*secpolicy
, zfs_ioc_poolcheck_t pool_check
)
5304 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5305 DATASET_NAME
, B_FALSE
, pool_check
);
5309 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
)
5311 zfs_ioctl_register_legacy(ioc
, func
, zfs_secpolicy_config
,
5312 POOL_NAME
, B_TRUE
, POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
5316 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5317 zfs_secpolicy_func_t
*secpolicy
)
5319 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5320 NO_NAME
, B_FALSE
, POOL_CHECK_NONE
);
5324 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc
,
5325 zfs_ioc_legacy_func_t
*func
, zfs_secpolicy_func_t
*secpolicy
)
5327 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5328 DATASET_NAME
, B_FALSE
, POOL_CHECK_SUSPENDED
);
5332 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
)
5334 zfs_ioctl_register_dataset_read_secpolicy(ioc
, func
,
5335 zfs_secpolicy_read
);
5339 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5340 zfs_secpolicy_func_t
*secpolicy
)
5342 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5343 DATASET_NAME
, B_TRUE
, POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
5347 zfs_ioctl_init(void)
5349 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT
,
5350 zfs_ioc_snapshot
, zfs_secpolicy_snapshot
, POOL_NAME
,
5351 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5353 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY
,
5354 zfs_ioc_log_history
, zfs_secpolicy_log_history
, NO_NAME
,
5355 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_FALSE
, B_FALSE
);
5357 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS
,
5358 zfs_ioc_space_snaps
, zfs_secpolicy_read
, DATASET_NAME
,
5359 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
5361 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW
,
5362 zfs_ioc_send_new
, zfs_secpolicy_send_new
, DATASET_NAME
,
5363 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
5365 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE
,
5366 zfs_ioc_send_space
, zfs_secpolicy_read
, DATASET_NAME
,
5367 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
5369 zfs_ioctl_register("create", ZFS_IOC_CREATE
,
5370 zfs_ioc_create
, zfs_secpolicy_create_clone
, DATASET_NAME
,
5371 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5373 zfs_ioctl_register("clone", ZFS_IOC_CLONE
,
5374 zfs_ioc_clone
, zfs_secpolicy_create_clone
, DATASET_NAME
,
5375 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5377 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS
,
5378 zfs_ioc_destroy_snaps
, zfs_secpolicy_destroy_snaps
, POOL_NAME
,
5379 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5381 zfs_ioctl_register("hold", ZFS_IOC_HOLD
,
5382 zfs_ioc_hold
, zfs_secpolicy_hold
, POOL_NAME
,
5383 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5384 zfs_ioctl_register("release", ZFS_IOC_RELEASE
,
5385 zfs_ioc_release
, zfs_secpolicy_release
, POOL_NAME
,
5386 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5388 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS
,
5389 zfs_ioc_get_holds
, zfs_secpolicy_read
, DATASET_NAME
,
5390 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
5392 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK
,
5393 zfs_ioc_rollback
, zfs_secpolicy_rollback
, DATASET_NAME
,
5394 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_FALSE
, B_TRUE
);
5396 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK
,
5397 zfs_ioc_bookmark
, zfs_secpolicy_bookmark
, POOL_NAME
,
5398 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5400 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS
,
5401 zfs_ioc_get_bookmarks
, zfs_secpolicy_read
, DATASET_NAME
,
5402 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
5404 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS
,
5405 zfs_ioc_destroy_bookmarks
, zfs_secpolicy_destroy_bookmarks
,
5407 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5409 /* IOCTLS that use the legacy function signature */
5411 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE
, zfs_ioc_pool_freeze
,
5412 zfs_secpolicy_config
, NO_NAME
, B_FALSE
, POOL_CHECK_READONLY
);
5414 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE
, zfs_ioc_pool_create
,
5415 zfs_secpolicy_config
, B_TRUE
, POOL_CHECK_NONE
);
5416 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN
,
5418 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE
,
5419 zfs_ioc_pool_upgrade
);
5420 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD
,
5422 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE
,
5423 zfs_ioc_vdev_remove
);
5424 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE
,
5425 zfs_ioc_vdev_set_state
);
5426 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH
,
5427 zfs_ioc_vdev_attach
);
5428 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH
,
5429 zfs_ioc_vdev_detach
);
5430 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH
,
5431 zfs_ioc_vdev_setpath
);
5432 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU
,
5433 zfs_ioc_vdev_setfru
);
5434 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS
,
5435 zfs_ioc_pool_set_props
);
5436 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT
,
5437 zfs_ioc_vdev_split
);
5438 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID
,
5439 zfs_ioc_pool_reguid
);
5441 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS
,
5442 zfs_ioc_pool_configs
, zfs_secpolicy_none
);
5443 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT
,
5444 zfs_ioc_pool_tryimport
, zfs_secpolicy_config
);
5445 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT
,
5446 zfs_ioc_inject_fault
, zfs_secpolicy_inject
);
5447 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT
,
5448 zfs_ioc_clear_fault
, zfs_secpolicy_inject
);
5449 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT
,
5450 zfs_ioc_inject_list_next
, zfs_secpolicy_inject
);
5453 * pool destroy, and export don't log the history as part of
5454 * zfsdev_ioctl, but rather zfs_ioc_pool_export
5455 * does the logging of those commands.
5457 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY
, zfs_ioc_pool_destroy
,
5458 zfs_secpolicy_config
, B_FALSE
, POOL_CHECK_SUSPENDED
);
5459 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT
, zfs_ioc_pool_export
,
5460 zfs_secpolicy_config
, B_FALSE
, POOL_CHECK_SUSPENDED
);
5462 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS
, zfs_ioc_pool_stats
,
5463 zfs_secpolicy_read
, B_FALSE
, POOL_CHECK_NONE
);
5464 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS
, zfs_ioc_pool_get_props
,
5465 zfs_secpolicy_read
, B_FALSE
, POOL_CHECK_NONE
);
5467 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG
, zfs_ioc_error_log
,
5468 zfs_secpolicy_inject
, B_FALSE
, POOL_CHECK_SUSPENDED
);
5469 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME
,
5470 zfs_ioc_dsobj_to_dsname
,
5471 zfs_secpolicy_diff
, B_FALSE
, POOL_CHECK_SUSPENDED
);
5472 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY
,
5473 zfs_ioc_pool_get_history
,
5474 zfs_secpolicy_config
, B_FALSE
, POOL_CHECK_SUSPENDED
);
5476 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT
, zfs_ioc_pool_import
,
5477 zfs_secpolicy_config
, B_TRUE
, POOL_CHECK_NONE
);
5479 zfs_ioctl_register_pool(ZFS_IOC_CLEAR
, zfs_ioc_clear
,
5480 zfs_secpolicy_config
, B_TRUE
, POOL_CHECK_NONE
);
5481 zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN
, zfs_ioc_pool_reopen
,
5482 zfs_secpolicy_config
, B_TRUE
, POOL_CHECK_SUSPENDED
);
5484 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN
,
5485 zfs_ioc_space_written
);
5486 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS
,
5487 zfs_ioc_objset_recvd_props
);
5488 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ
,
5490 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL
,
5492 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS
,
5493 zfs_ioc_objset_stats
);
5494 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS
,
5495 zfs_ioc_objset_zplprops
);
5496 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT
,
5497 zfs_ioc_dataset_list_next
);
5498 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT
,
5499 zfs_ioc_snapshot_list_next
);
5500 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS
,
5501 zfs_ioc_send_progress
);
5503 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF
,
5504 zfs_ioc_diff
, zfs_secpolicy_diff
);
5505 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS
,
5506 zfs_ioc_obj_to_stats
, zfs_secpolicy_diff
);
5507 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH
,
5508 zfs_ioc_obj_to_path
, zfs_secpolicy_diff
);
5509 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE
,
5510 zfs_ioc_userspace_one
, zfs_secpolicy_userspace_one
);
5511 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY
,
5512 zfs_ioc_userspace_many
, zfs_secpolicy_userspace_many
);
5513 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND
,
5514 zfs_ioc_send
, zfs_secpolicy_send
);
5516 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP
, zfs_ioc_set_prop
,
5517 zfs_secpolicy_none
);
5518 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY
, zfs_ioc_destroy
,
5519 zfs_secpolicy_destroy
);
5520 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME
, zfs_ioc_rename
,
5521 zfs_secpolicy_rename
);
5522 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV
, zfs_ioc_recv
,
5523 zfs_secpolicy_recv
);
5524 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE
, zfs_ioc_promote
,
5525 zfs_secpolicy_promote
);
5526 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP
,
5527 zfs_ioc_inherit_prop
, zfs_secpolicy_inherit_prop
);
5528 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL
, zfs_ioc_set_fsacl
,
5529 zfs_secpolicy_set_fsacl
);
5531 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE
, zfs_ioc_share
,
5532 zfs_secpolicy_share
, POOL_CHECK_NONE
);
5533 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL
, zfs_ioc_smb_acl
,
5534 zfs_secpolicy_smb_acl
, POOL_CHECK_NONE
);
5535 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE
,
5536 zfs_ioc_userspace_upgrade
, zfs_secpolicy_userspace_upgrade
,
5537 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
5538 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT
,
5539 zfs_ioc_tmp_snapshot
, zfs_secpolicy_tmp_snapshot
,
5540 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
5545 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_NEXT
, zfs_ioc_events_next
,
5546 zfs_secpolicy_config
, NO_NAME
, B_FALSE
, POOL_CHECK_NONE
);
5547 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_CLEAR
, zfs_ioc_events_clear
,
5548 zfs_secpolicy_config
, NO_NAME
, B_FALSE
, POOL_CHECK_NONE
);
5549 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_SEEK
, zfs_ioc_events_seek
,
5550 zfs_secpolicy_config
, NO_NAME
, B_FALSE
, POOL_CHECK_NONE
);
5554 pool_status_check(const char *name
, zfs_ioc_namecheck_t type
,
5555 zfs_ioc_poolcheck_t check
)
5560 ASSERT(type
== POOL_NAME
|| type
== DATASET_NAME
);
5562 if (check
& POOL_CHECK_NONE
)
5565 error
= spa_open(name
, &spa
, FTAG
);
5567 if ((check
& POOL_CHECK_SUSPENDED
) && spa_suspended(spa
))
5568 error
= SET_ERROR(EAGAIN
);
5569 else if ((check
& POOL_CHECK_READONLY
) && !spa_writeable(spa
))
5570 error
= SET_ERROR(EROFS
);
5571 spa_close(spa
, FTAG
);
5577 zfsdev_get_state_impl(minor_t minor
, enum zfsdev_state_type which
)
5581 for (zs
= zfsdev_state_list
; zs
!= NULL
; zs
= zs
->zs_next
) {
5582 if (zs
->zs_minor
== minor
) {
5586 return (zs
->zs_onexit
);
5588 return (zs
->zs_zevent
);
5599 zfsdev_get_state(minor_t minor
, enum zfsdev_state_type which
)
5603 ptr
= zfsdev_get_state_impl(minor
, which
);
5609 zfsdev_getminor(struct file
*filp
, minor_t
*minorp
)
5611 zfsdev_state_t
*zs
, *fpd
;
5613 ASSERT(filp
!= NULL
);
5614 ASSERT(!MUTEX_HELD(&zfsdev_state_lock
));
5616 fpd
= filp
->private_data
;
5620 mutex_enter(&zfsdev_state_lock
);
5622 for (zs
= zfsdev_state_list
; zs
!= NULL
; zs
= zs
->zs_next
) {
5624 if (zs
->zs_minor
== -1)
5628 *minorp
= fpd
->zs_minor
;
5629 mutex_exit(&zfsdev_state_lock
);
5634 mutex_exit(&zfsdev_state_lock
);
5640 * Find a free minor number. The zfsdev_state_list is expected to
5641 * be short since it is only a list of currently open file handles.
5644 zfsdev_minor_alloc(void)
5646 static minor_t last_minor
= 0;
5649 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
5651 for (m
= last_minor
+ 1; m
!= last_minor
; m
++) {
5652 if (m
> ZFSDEV_MAX_MINOR
)
5654 if (zfsdev_get_state_impl(m
, ZST_ALL
) == NULL
) {
5664 zfsdev_state_init(struct file
*filp
)
5666 zfsdev_state_t
*zs
, *zsprev
= NULL
;
5668 boolean_t newzs
= B_FALSE
;
5670 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
5672 minor
= zfsdev_minor_alloc();
5674 return (SET_ERROR(ENXIO
));
5676 for (zs
= zfsdev_state_list
; zs
!= NULL
; zs
= zs
->zs_next
) {
5677 if (zs
->zs_minor
== -1)
5683 zs
= kmem_zalloc(sizeof (zfsdev_state_t
), KM_SLEEP
);
5688 filp
->private_data
= zs
;
5690 zfs_onexit_init((zfs_onexit_t
**)&zs
->zs_onexit
);
5691 zfs_zevent_init((zfs_zevent_t
**)&zs
->zs_zevent
);
5695 * In order to provide for lock-free concurrent read access
5696 * to the minor list in zfsdev_get_state_impl(), new entries
5697 * must be completely written before linking them into the
5698 * list whereas existing entries are already linked; the last
5699 * operation must be updating zs_minor (from -1 to the new
5703 zs
->zs_minor
= minor
;
5705 zsprev
->zs_next
= zs
;
5708 zs
->zs_minor
= minor
;
5715 zfsdev_state_destroy(struct file
*filp
)
5719 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
5720 ASSERT(filp
->private_data
!= NULL
);
5722 zs
= filp
->private_data
;
5724 zfs_onexit_destroy(zs
->zs_onexit
);
5725 zfs_zevent_destroy(zs
->zs_zevent
);
5731 zfsdev_open(struct inode
*ino
, struct file
*filp
)
5735 mutex_enter(&zfsdev_state_lock
);
5736 error
= zfsdev_state_init(filp
);
5737 mutex_exit(&zfsdev_state_lock
);
5743 zfsdev_release(struct inode
*ino
, struct file
*filp
)
5747 mutex_enter(&zfsdev_state_lock
);
5748 error
= zfsdev_state_destroy(filp
);
5749 mutex_exit(&zfsdev_state_lock
);
5755 zfsdev_ioctl(struct file
*filp
, unsigned cmd
, unsigned long arg
)
5759 int error
, rc
, flag
= 0;
5760 const zfs_ioc_vec_t
*vec
;
5761 char *saved_poolname
= NULL
;
5762 nvlist_t
*innvl
= NULL
;
5763 fstrans_cookie_t cookie
;
5765 vecnum
= cmd
- ZFS_IOC_FIRST
;
5766 if (vecnum
>= sizeof (zfs_ioc_vec
) / sizeof (zfs_ioc_vec
[0]))
5767 return (-SET_ERROR(EINVAL
));
5768 vec
= &zfs_ioc_vec
[vecnum
];
5771 * The registered ioctl list may be sparse, verify that either
5772 * a normal or legacy handler are registered.
5774 if (vec
->zvec_func
== NULL
&& vec
->zvec_legacy_func
== NULL
)
5775 return (-SET_ERROR(EINVAL
));
5777 zc
= kmem_zalloc(sizeof (zfs_cmd_t
), KM_SLEEP
);
5779 error
= ddi_copyin((void *)arg
, zc
, sizeof (zfs_cmd_t
), flag
);
5781 error
= SET_ERROR(EFAULT
);
5785 zc
->zc_iflags
= flag
& FKIOCTL
;
5786 if (zc
->zc_nvlist_src_size
!= 0) {
5787 error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
5788 zc
->zc_iflags
, &innvl
);
5794 * Ensure that all pool/dataset names are valid before we pass down to
5797 zc
->zc_name
[sizeof (zc
->zc_name
) - 1] = '\0';
5798 switch (vec
->zvec_namecheck
) {
5800 if (pool_namecheck(zc
->zc_name
, NULL
, NULL
) != 0)
5801 error
= SET_ERROR(EINVAL
);
5803 error
= pool_status_check(zc
->zc_name
,
5804 vec
->zvec_namecheck
, vec
->zvec_pool_check
);
5808 if (dataset_namecheck(zc
->zc_name
, NULL
, NULL
) != 0)
5809 error
= SET_ERROR(EINVAL
);
5811 error
= pool_status_check(zc
->zc_name
,
5812 vec
->zvec_namecheck
, vec
->zvec_pool_check
);
5820 if (error
== 0 && !(flag
& FKIOCTL
))
5821 error
= vec
->zvec_secpolicy(zc
, innvl
, CRED());
5826 /* legacy ioctls can modify zc_name */
5827 saved_poolname
= strdup(zc
->zc_name
);
5828 if (saved_poolname
== NULL
) {
5829 error
= SET_ERROR(ENOMEM
);
5832 saved_poolname
[strcspn(saved_poolname
, "/@#")] = '\0';
5835 if (vec
->zvec_func
!= NULL
) {
5839 nvlist_t
*lognv
= NULL
;
5841 ASSERT(vec
->zvec_legacy_func
== NULL
);
5844 * Add the innvl to the lognv before calling the func,
5845 * in case the func changes the innvl.
5847 if (vec
->zvec_allow_log
) {
5848 lognv
= fnvlist_alloc();
5849 fnvlist_add_string(lognv
, ZPOOL_HIST_IOCTL
,
5851 if (!nvlist_empty(innvl
)) {
5852 fnvlist_add_nvlist(lognv
, ZPOOL_HIST_INPUT_NVL
,
5857 outnvl
= fnvlist_alloc();
5858 cookie
= spl_fstrans_mark();
5859 error
= vec
->zvec_func(zc
->zc_name
, innvl
, outnvl
);
5860 spl_fstrans_unmark(cookie
);
5862 if (error
== 0 && vec
->zvec_allow_log
&&
5863 spa_open(zc
->zc_name
, &spa
, FTAG
) == 0) {
5864 if (!nvlist_empty(outnvl
)) {
5865 fnvlist_add_nvlist(lognv
, ZPOOL_HIST_OUTPUT_NVL
,
5868 (void) spa_history_log_nvl(spa
, lognv
);
5869 spa_close(spa
, FTAG
);
5871 fnvlist_free(lognv
);
5873 if (!nvlist_empty(outnvl
) || zc
->zc_nvlist_dst_size
!= 0) {
5875 if (vec
->zvec_smush_outnvlist
) {
5876 smusherror
= nvlist_smush(outnvl
,
5877 zc
->zc_nvlist_dst_size
);
5879 if (smusherror
== 0)
5880 puterror
= put_nvlist(zc
, outnvl
);
5886 nvlist_free(outnvl
);
5888 cookie
= spl_fstrans_mark();
5889 error
= vec
->zvec_legacy_func(zc
);
5890 spl_fstrans_unmark(cookie
);
5895 rc
= ddi_copyout(zc
, (void *)arg
, sizeof (zfs_cmd_t
), flag
);
5896 if (error
== 0 && rc
!= 0)
5897 error
= SET_ERROR(EFAULT
);
5898 if (error
== 0 && vec
->zvec_allow_log
) {
5899 char *s
= tsd_get(zfs_allow_log_key
);
5902 (void) tsd_set(zfs_allow_log_key
, saved_poolname
);
5904 if (saved_poolname
!= NULL
)
5905 strfree(saved_poolname
);
5908 kmem_free(zc
, sizeof (zfs_cmd_t
));
5912 #ifdef CONFIG_COMPAT
5914 zfsdev_compat_ioctl(struct file
*filp
, unsigned cmd
, unsigned long arg
)
5916 return (zfsdev_ioctl(filp
, cmd
, arg
));
5919 #define zfsdev_compat_ioctl NULL
5922 static const struct file_operations zfsdev_fops
= {
5923 .open
= zfsdev_open
,
5924 .release
= zfsdev_release
,
5925 .unlocked_ioctl
= zfsdev_ioctl
,
5926 .compat_ioctl
= zfsdev_compat_ioctl
,
5927 .owner
= THIS_MODULE
,
5930 static struct miscdevice zfs_misc
= {
5931 .minor
= MISC_DYNAMIC_MINOR
,
5933 .fops
= &zfsdev_fops
,
5941 mutex_init(&zfsdev_state_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
5942 zfsdev_state_list
= kmem_zalloc(sizeof (zfsdev_state_t
), KM_SLEEP
);
5943 zfsdev_state_list
->zs_minor
= -1;
5945 error
= misc_register(&zfs_misc
);
5947 printk(KERN_INFO
"ZFS: misc_register() failed %d\n", error
);
5957 zfsdev_state_t
*zs
, *zsprev
= NULL
;
5959 misc_deregister(&zfs_misc
);
5960 mutex_destroy(&zfsdev_state_lock
);
5962 for (zs
= zfsdev_state_list
; zs
!= NULL
; zs
= zs
->zs_next
) {
5964 kmem_free(zsprev
, sizeof (zfsdev_state_t
));
5968 kmem_free(zsprev
, sizeof (zfsdev_state_t
));
5972 zfs_allow_log_destroy(void *arg
)
5974 char *poolname
= arg
;
5979 #define ZFS_DEBUG_STR " (DEBUG mode)"
5981 #define ZFS_DEBUG_STR ""
5989 error
= -vn_set_pwd("/");
5992 "ZFS: Warning unable to set pwd to '/': %d\n", error
);
5996 if ((error
= -zvol_init()) != 0)
5999 spa_init(FREAD
| FWRITE
);
6004 if ((error
= zfs_attach()) != 0)
6007 tsd_create(&zfs_fsyncer_key
, NULL
);
6008 tsd_create(&rrw_tsd_key
, rrw_tsd_destroy
);
6009 tsd_create(&zfs_allow_log_key
, zfs_allow_log_destroy
);
6011 printk(KERN_NOTICE
"ZFS: Loaded module v%s-%s%s, "
6012 "ZFS pool version %s, ZFS filesystem version %s\n",
6013 ZFS_META_VERSION
, ZFS_META_RELEASE
, ZFS_DEBUG_STR
,
6014 SPA_VERSION_STRING
, ZPL_VERSION_STRING
);
6015 #ifndef CONFIG_FS_POSIX_ACL
6016 printk(KERN_NOTICE
"ZFS: Posix ACLs disabled by kernel\n");
6017 #endif /* CONFIG_FS_POSIX_ACL */
6025 printk(KERN_NOTICE
"ZFS: Failed to Load ZFS Filesystem v%s-%s%s"
6026 ", rc = %d\n", ZFS_META_VERSION
, ZFS_META_RELEASE
,
6027 ZFS_DEBUG_STR
, error
);
6040 tsd_destroy(&zfs_fsyncer_key
);
6041 tsd_destroy(&rrw_tsd_key
);
6042 tsd_destroy(&zfs_allow_log_key
);
6044 printk(KERN_NOTICE
"ZFS: Unloaded module v%s-%s%s\n",
6045 ZFS_META_VERSION
, ZFS_META_RELEASE
, ZFS_DEBUG_STR
);
6052 MODULE_DESCRIPTION("ZFS");
6053 MODULE_AUTHOR(ZFS_META_AUTHOR
);
6054 MODULE_LICENSE(ZFS_META_LICENSE
);
6055 MODULE_VERSION(ZFS_META_VERSION
"-" ZFS_META_RELEASE
);
6056 #endif /* HAVE_SPL */