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.
38 * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
39 * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
41 * There are two ways that we handle ioctls: the legacy way where almost
42 * all of the logic is in the ioctl callback, and the new way where most
43 * of the marshalling is handled in the common entry point, zfsdev_ioctl().
45 * Non-legacy ioctls should be registered by calling
46 * zfs_ioctl_register() from zfs_ioctl_init(). The ioctl is invoked
47 * from userland by lzc_ioctl().
49 * The registration arguments are as follows:
52 * The name of the ioctl. This is used for history logging. If the
53 * ioctl returns successfully (the callback returns 0), and allow_log
54 * is true, then a history log entry will be recorded with the input &
55 * output nvlists. The log entry can be printed with "zpool history -i".
58 * The ioctl request number, which userland will pass to ioctl(2).
59 * The ioctl numbers can change from release to release, because
60 * the caller (libzfs) must be matched to the kernel.
62 * zfs_secpolicy_func_t *secpolicy
63 * This function will be called before the zfs_ioc_func_t, to
64 * determine if this operation is permitted. It should return EPERM
65 * on failure, and 0 on success. Checks include determining if the
66 * dataset is visible in this zone, and if the user has either all
67 * zfs privileges in the zone (SYS_MOUNT), or has been granted permission
68 * to do this operation on this dataset with "zfs allow".
70 * zfs_ioc_namecheck_t namecheck
71 * This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
72 * name, a dataset name, or nothing. If the name is not well-formed,
73 * the ioctl will fail and the callback will not be called.
74 * Therefore, the callback can assume that the name is well-formed
75 * (e.g. is null-terminated, doesn't have more than one '@' character,
76 * doesn't have invalid characters).
78 * zfs_ioc_poolcheck_t pool_check
79 * This specifies requirements on the pool state. If the pool does
80 * not meet them (is suspended or is readonly), the ioctl will fail
81 * and the callback will not be called. If any checks are specified
82 * (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
83 * Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
84 * POOL_CHECK_READONLY).
86 * boolean_t smush_outnvlist
87 * If smush_outnvlist is true, then the output is presumed to be a
88 * list of errors, and it will be "smushed" down to fit into the
89 * caller's buffer, by removing some entries and replacing them with a
90 * single "N_MORE_ERRORS" entry indicating how many were removed. See
91 * nvlist_smush() for details. If smush_outnvlist is false, and the
92 * outnvlist does not fit into the userland-provided buffer, then the
93 * ioctl will fail with ENOMEM.
95 * zfs_ioc_func_t *func
96 * The callback function that will perform the operation.
98 * The callback should return 0 on success, or an error number on
99 * failure. If the function fails, the userland ioctl will return -1,
100 * and errno will be set to the callback's return value. The callback
101 * will be called with the following arguments:
104 * The name of the pool or dataset to operate on, from
105 * zfs_cmd_t:zc_name. The 'namecheck' argument specifies the
106 * expected type (pool, dataset, or none).
109 * The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src. Or
110 * NULL if no input nvlist was provided. Changes to this nvlist are
111 * ignored. If the input nvlist could not be deserialized, the
112 * ioctl will fail and the callback will not be called.
115 * The output nvlist, initially empty. The callback can fill it in,
116 * and it will be returned to userland by serializing it into
117 * zfs_cmd_t:zc_nvlist_dst. If it is non-empty, and serialization
118 * fails (e.g. because the caller didn't supply a large enough
119 * buffer), then the overall ioctl will fail. See the
120 * 'smush_nvlist' argument above for additional behaviors.
122 * There are two typical uses of the output nvlist:
123 * - To return state, e.g. property values. In this case,
124 * smush_outnvlist should be false. If the buffer was not large
125 * enough, the caller will reallocate a larger buffer and try
128 * - To return multiple errors from an ioctl which makes on-disk
129 * changes. In this case, smush_outnvlist should be true.
130 * Ioctls which make on-disk modifications should generally not
131 * use the outnvl if they succeed, because the caller can not
132 * distinguish between the operation failing, and
133 * deserialization failing.
136 #include <sys/types.h>
137 #include <sys/param.h>
138 #include <sys/errno.h>
141 #include <sys/modctl.h>
142 #include <sys/open.h>
143 #include <sys/file.h>
144 #include <sys/kmem.h>
145 #include <sys/conf.h>
146 #include <sys/cmn_err.h>
147 #include <sys/stat.h>
148 #include <sys/zfs_ioctl.h>
149 #include <sys/zfs_vfsops.h>
150 #include <sys/zfs_znode.h>
153 #include <sys/spa_impl.h>
154 #include <sys/vdev.h>
155 #include <sys/priv_impl.h>
157 #include <sys/dsl_dir.h>
158 #include <sys/dsl_dataset.h>
159 #include <sys/dsl_prop.h>
160 #include <sys/dsl_deleg.h>
161 #include <sys/dmu_objset.h>
162 #include <sys/dmu_impl.h>
163 #include <sys/dmu_tx.h>
165 #include <sys/sunddi.h>
166 #include <sys/sunldi.h>
167 #include <sys/policy.h>
168 #include <sys/zone.h>
169 #include <sys/nvpair.h>
170 #include <sys/pathname.h>
171 #include <sys/mount.h>
173 #include <sys/fs/zfs.h>
174 #include <sys/zfs_ctldir.h>
175 #include <sys/zfs_dir.h>
176 #include <sys/zfs_onexit.h>
177 #include <sys/zvol.h>
178 #include <sys/dsl_scan.h>
179 #include <sharefs/share.h>
180 #include <sys/fm/util.h>
182 #include <sys/dmu_send.h>
183 #include <sys/dsl_destroy.h>
184 #include <sys/dsl_bookmark.h>
185 #include <sys/dsl_userhold.h>
186 #include <sys/zfeature.h>
188 #include <linux/miscdevice.h>
190 #include "zfs_namecheck.h"
191 #include "zfs_prop.h"
192 #include "zfs_deleg.h"
193 #include "zfs_comutil.h"
195 kmutex_t zfsdev_state_lock
;
196 zfsdev_state_t
*zfsdev_state_list
;
198 extern void zfs_init(void);
199 extern void zfs_fini(void);
201 uint_t zfs_fsyncer_key
;
202 extern uint_t rrw_tsd_key
;
203 static uint_t zfs_allow_log_key
;
205 typedef int zfs_ioc_legacy_func_t(zfs_cmd_t
*);
206 typedef int zfs_ioc_func_t(const char *, nvlist_t
*, nvlist_t
*);
207 typedef int zfs_secpolicy_func_t(zfs_cmd_t
*, nvlist_t
*, cred_t
*);
213 } zfs_ioc_namecheck_t
;
216 POOL_CHECK_NONE
= 1 << 0,
217 POOL_CHECK_SUSPENDED
= 1 << 1,
218 POOL_CHECK_READONLY
= 1 << 2,
219 } zfs_ioc_poolcheck_t
;
221 typedef struct zfs_ioc_vec
{
222 zfs_ioc_legacy_func_t
*zvec_legacy_func
;
223 zfs_ioc_func_t
*zvec_func
;
224 zfs_secpolicy_func_t
*zvec_secpolicy
;
225 zfs_ioc_namecheck_t zvec_namecheck
;
226 boolean_t zvec_allow_log
;
227 zfs_ioc_poolcheck_t zvec_pool_check
;
228 boolean_t zvec_smush_outnvlist
;
229 const char *zvec_name
;
232 /* This array is indexed by zfs_userquota_prop_t */
233 static const char *userquota_perms
[] = {
234 ZFS_DELEG_PERM_USERUSED
,
235 ZFS_DELEG_PERM_USERQUOTA
,
236 ZFS_DELEG_PERM_GROUPUSED
,
237 ZFS_DELEG_PERM_GROUPQUOTA
,
240 static int zfs_ioc_userspace_upgrade(zfs_cmd_t
*zc
);
241 static int zfs_check_settable(const char *name
, nvpair_t
*property
,
243 static int zfs_check_clearable(char *dataset
, nvlist_t
*props
,
245 static int zfs_fill_zplprops_root(uint64_t, nvlist_t
*, nvlist_t
*,
247 int zfs_set_prop_nvlist(const char *, zprop_source_t
, nvlist_t
*, nvlist_t
*);
248 static int get_nvlist(uint64_t nvl
, uint64_t size
, int iflag
, nvlist_t
**nvp
);
250 #if defined(HAVE_DECLARE_EVENT_CLASS)
252 __dprintf(const char *file
, const char *func
, int line
, const char *fmt
, ...)
256 char *buf
= kmem_alloc(size
, KM_SLEEP
);
261 * Get rid of annoying prefix to filename.
263 newfile
= strrchr(file
, '/');
264 if (newfile
!= NULL
) {
265 newfile
= newfile
+ 1; /* Get rid of leading / */
271 (void) vsnprintf(buf
, size
, fmt
, adx
);
275 * Get rid of trailing newline.
277 nl
= strrchr(buf
, '\n');
282 * To get this data enable the zfs__dprintf trace point as shown:
284 * # Enable zfs__dprintf tracepoint, clear the tracepoint ring buffer
285 * $ echo 1 > /sys/module/zfs/parameters/zfs_flags
286 * $ echo 1 > /sys/kernel/debug/tracing/events/zfs/enable
287 * $ echo 0 > /sys/kernel/debug/tracing/trace
289 * # Dump the ring buffer.
290 * $ cat /sys/kernel/debug/tracing/trace
292 DTRACE_PROBE4(zfs__dprintf
,
293 char *, newfile
, char *, func
, int, line
, char *, buf
);
295 kmem_free(buf
, size
);
297 #endif /* HAVE_DECLARE_EVENT_CLASS */
300 history_str_free(char *buf
)
302 kmem_free(buf
, HIS_MAX_RECORD_LEN
);
306 history_str_get(zfs_cmd_t
*zc
)
310 if (zc
->zc_history
== 0)
313 buf
= kmem_alloc(HIS_MAX_RECORD_LEN
, KM_SLEEP
);
314 if (copyinstr((void *)(uintptr_t)zc
->zc_history
,
315 buf
, HIS_MAX_RECORD_LEN
, NULL
) != 0) {
316 history_str_free(buf
);
320 buf
[HIS_MAX_RECORD_LEN
-1] = '\0';
326 * Check to see if the named dataset is currently defined as bootable
329 zfs_is_bootfs(const char *name
)
333 if (dmu_objset_hold(name
, FTAG
, &os
) == 0) {
335 ret
= (dmu_objset_id(os
) == spa_bootfs(dmu_objset_spa(os
)));
336 dmu_objset_rele(os
, FTAG
);
343 * Return non-zero if the spa version is less than requested version.
346 zfs_earlier_version(const char *name
, int version
)
350 if (spa_open(name
, &spa
, FTAG
) == 0) {
351 if (spa_version(spa
) < version
) {
352 spa_close(spa
, FTAG
);
355 spa_close(spa
, FTAG
);
361 * Return TRUE if the ZPL version is less than requested version.
364 zpl_earlier_version(const char *name
, int version
)
367 boolean_t rc
= B_TRUE
;
369 if (dmu_objset_hold(name
, FTAG
, &os
) == 0) {
372 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
373 dmu_objset_rele(os
, FTAG
);
376 /* XXX reading from non-owned objset */
377 if (zfs_get_zplprop(os
, ZFS_PROP_VERSION
, &zplversion
) == 0)
378 rc
= zplversion
< version
;
379 dmu_objset_rele(os
, FTAG
);
385 zfs_log_history(zfs_cmd_t
*zc
)
390 if ((buf
= history_str_get(zc
)) == NULL
)
393 if (spa_open(zc
->zc_name
, &spa
, FTAG
) == 0) {
394 if (spa_version(spa
) >= SPA_VERSION_ZPOOL_HISTORY
)
395 (void) spa_history_log(spa
, buf
);
396 spa_close(spa
, FTAG
);
398 history_str_free(buf
);
402 * Policy for top-level read operations (list pools). Requires no privileges,
403 * and can be used in the local zone, as there is no associated dataset.
407 zfs_secpolicy_none(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
413 * Policy for dataset read operations (list children, get statistics). Requires
414 * no privileges, but must be visible in the local zone.
418 zfs_secpolicy_read(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
420 if (INGLOBALZONE(curproc
) ||
421 zone_dataset_visible(zc
->zc_name
, NULL
))
424 return (SET_ERROR(ENOENT
));
428 zfs_dozonecheck_impl(const char *dataset
, uint64_t zoned
, cred_t
*cr
)
433 * The dataset must be visible by this zone -- check this first
434 * so they don't see EPERM on something they shouldn't know about.
436 if (!INGLOBALZONE(curproc
) &&
437 !zone_dataset_visible(dataset
, &writable
))
438 return (SET_ERROR(ENOENT
));
440 if (INGLOBALZONE(curproc
)) {
442 * If the fs is zoned, only root can access it from the
445 if (secpolicy_zfs(cr
) && zoned
)
446 return (SET_ERROR(EPERM
));
449 * If we are in a local zone, the 'zoned' property must be set.
452 return (SET_ERROR(EPERM
));
454 /* must be writable by this zone */
456 return (SET_ERROR(EPERM
));
462 zfs_dozonecheck(const char *dataset
, cred_t
*cr
)
466 if (dsl_prop_get_integer(dataset
, "zoned", &zoned
, NULL
))
467 return (SET_ERROR(ENOENT
));
469 return (zfs_dozonecheck_impl(dataset
, zoned
, cr
));
473 zfs_dozonecheck_ds(const char *dataset
, dsl_dataset_t
*ds
, cred_t
*cr
)
477 if (dsl_prop_get_int_ds(ds
, "zoned", &zoned
))
478 return (SET_ERROR(ENOENT
));
480 return (zfs_dozonecheck_impl(dataset
, zoned
, cr
));
484 zfs_secpolicy_write_perms_ds(const char *name
, dsl_dataset_t
*ds
,
485 const char *perm
, cred_t
*cr
)
489 error
= zfs_dozonecheck_ds(name
, ds
, cr
);
491 error
= secpolicy_zfs(cr
);
493 error
= dsl_deleg_access_impl(ds
, perm
, cr
);
499 zfs_secpolicy_write_perms(const char *name
, const char *perm
, cred_t
*cr
)
505 error
= dsl_pool_hold(name
, FTAG
, &dp
);
509 error
= dsl_dataset_hold(dp
, name
, FTAG
, &ds
);
511 dsl_pool_rele(dp
, FTAG
);
515 error
= zfs_secpolicy_write_perms_ds(name
, ds
, perm
, cr
);
517 dsl_dataset_rele(ds
, FTAG
);
518 dsl_pool_rele(dp
, FTAG
);
523 * Policy for setting the security label property.
525 * Returns 0 for success, non-zero for access and other errors.
528 zfs_set_slabel_policy(const char *name
, char *strval
, cred_t
*cr
)
531 char ds_hexsl
[MAXNAMELEN
];
532 bslabel_t ds_sl
, new_sl
;
533 boolean_t new_default
= FALSE
;
535 int needed_priv
= -1;
538 /* First get the existing dataset label. */
539 error
= dsl_prop_get(name
, zfs_prop_to_name(ZFS_PROP_MLSLABEL
),
540 1, sizeof (ds_hexsl
), &ds_hexsl
, NULL
);
542 return (SET_ERROR(EPERM
));
544 if (strcasecmp(strval
, ZFS_MLSLABEL_DEFAULT
) == 0)
547 /* The label must be translatable */
548 if (!new_default
&& (hexstr_to_label(strval
, &new_sl
) != 0))
549 return (SET_ERROR(EINVAL
));
552 * In a non-global zone, disallow attempts to set a label that
553 * doesn't match that of the zone; otherwise no other checks
556 if (!INGLOBALZONE(curproc
)) {
557 if (new_default
|| !blequal(&new_sl
, CR_SL(CRED())))
558 return (SET_ERROR(EPERM
));
563 * For global-zone datasets (i.e., those whose zoned property is
564 * "off", verify that the specified new label is valid for the
567 if (dsl_prop_get_integer(name
,
568 zfs_prop_to_name(ZFS_PROP_ZONED
), &zoned
, NULL
))
569 return (SET_ERROR(EPERM
));
571 if (zfs_check_global_label(name
, strval
) != 0)
572 return (SET_ERROR(EPERM
));
576 * If the existing dataset label is nondefault, check if the
577 * dataset is mounted (label cannot be changed while mounted).
578 * Get the zfs_sb_t; if there isn't one, then the dataset isn't
579 * mounted (or isn't a dataset, doesn't exist, ...).
581 if (strcasecmp(ds_hexsl
, ZFS_MLSLABEL_DEFAULT
) != 0) {
583 static char *setsl_tag
= "setsl_tag";
586 * Try to own the dataset; abort if there is any error,
587 * (e.g., already mounted, in use, or other error).
589 error
= dmu_objset_own(name
, DMU_OST_ZFS
, B_TRUE
,
592 return (SET_ERROR(EPERM
));
594 dmu_objset_disown(os
, setsl_tag
);
597 needed_priv
= PRIV_FILE_DOWNGRADE_SL
;
601 if (hexstr_to_label(strval
, &new_sl
) != 0)
602 return (SET_ERROR(EPERM
));
604 if (blstrictdom(&ds_sl
, &new_sl
))
605 needed_priv
= PRIV_FILE_DOWNGRADE_SL
;
606 else if (blstrictdom(&new_sl
, &ds_sl
))
607 needed_priv
= PRIV_FILE_UPGRADE_SL
;
609 /* dataset currently has a default label */
611 needed_priv
= PRIV_FILE_UPGRADE_SL
;
615 if (needed_priv
!= -1)
616 return (PRIV_POLICY(cr
, needed_priv
, B_FALSE
, EPERM
, NULL
));
620 #endif /* HAVE_MLSLABEL */
624 zfs_secpolicy_setprop(const char *dsname
, zfs_prop_t prop
, nvpair_t
*propval
,
630 * Check permissions for special properties.
637 * Disallow setting of 'zoned' from within a local zone.
639 if (!INGLOBALZONE(curproc
))
640 return (SET_ERROR(EPERM
));
644 case ZFS_PROP_FILESYSTEM_LIMIT
:
645 case ZFS_PROP_SNAPSHOT_LIMIT
:
646 if (!INGLOBALZONE(curproc
)) {
648 char setpoint
[MAXNAMELEN
];
650 * Unprivileged users are allowed to modify the
651 * limit on things *under* (ie. contained by)
652 * the thing they own.
654 if (dsl_prop_get_integer(dsname
, "zoned", &zoned
,
656 return (SET_ERROR(EPERM
));
657 if (!zoned
|| strlen(dsname
) <= strlen(setpoint
))
658 return (SET_ERROR(EPERM
));
662 case ZFS_PROP_MLSLABEL
:
663 if (!is_system_labeled())
664 return (SET_ERROR(EPERM
));
666 if (nvpair_value_string(propval
, &strval
) == 0) {
669 err
= zfs_set_slabel_policy(dsname
, strval
, CRED());
676 return (zfs_secpolicy_write_perms(dsname
, zfs_prop_to_name(prop
), cr
));
681 zfs_secpolicy_set_fsacl(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
685 error
= zfs_dozonecheck(zc
->zc_name
, cr
);
690 * permission to set permissions will be evaluated later in
691 * dsl_deleg_can_allow()
698 zfs_secpolicy_rollback(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
700 return (zfs_secpolicy_write_perms(zc
->zc_name
,
701 ZFS_DELEG_PERM_ROLLBACK
, cr
));
706 zfs_secpolicy_send(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
714 * Generate the current snapshot name from the given objsetid, then
715 * use that name for the secpolicy/zone checks.
717 cp
= strchr(zc
->zc_name
, '@');
719 return (SET_ERROR(EINVAL
));
720 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
724 error
= dsl_dataset_hold_obj(dp
, zc
->zc_sendobj
, FTAG
, &ds
);
726 dsl_pool_rele(dp
, FTAG
);
730 dsl_dataset_name(ds
, zc
->zc_name
);
732 error
= zfs_secpolicy_write_perms_ds(zc
->zc_name
, ds
,
733 ZFS_DELEG_PERM_SEND
, cr
);
734 dsl_dataset_rele(ds
, FTAG
);
735 dsl_pool_rele(dp
, FTAG
);
742 zfs_secpolicy_send_new(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
744 return (zfs_secpolicy_write_perms(zc
->zc_name
,
745 ZFS_DELEG_PERM_SEND
, cr
));
748 #ifdef HAVE_SMB_SHARE
751 zfs_secpolicy_deleg_share(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
756 if ((error
= lookupname(zc
->zc_value
, UIO_SYSSPACE
,
757 NO_FOLLOW
, NULL
, &vp
)) != 0)
760 /* Now make sure mntpnt and dataset are ZFS */
762 if (vp
->v_vfsp
->vfs_fstype
!= zfsfstype
||
763 (strcmp((char *)refstr_value(vp
->v_vfsp
->vfs_resource
),
764 zc
->zc_name
) != 0)) {
766 return (SET_ERROR(EPERM
));
770 return (dsl_deleg_access(zc
->zc_name
,
771 ZFS_DELEG_PERM_SHARE
, cr
));
773 #endif /* HAVE_SMB_SHARE */
776 zfs_secpolicy_share(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
778 #ifdef HAVE_SMB_SHARE
779 if (!INGLOBALZONE(curproc
))
780 return (SET_ERROR(EPERM
));
782 if (secpolicy_nfs(cr
) == 0) {
785 return (zfs_secpolicy_deleg_share(zc
, innvl
, cr
));
788 return (SET_ERROR(ENOTSUP
));
789 #endif /* HAVE_SMB_SHARE */
793 zfs_secpolicy_smb_acl(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
795 #ifdef HAVE_SMB_SHARE
796 if (!INGLOBALZONE(curproc
))
797 return (SET_ERROR(EPERM
));
799 if (secpolicy_smb(cr
) == 0) {
802 return (zfs_secpolicy_deleg_share(zc
, innvl
, cr
));
805 return (SET_ERROR(ENOTSUP
));
806 #endif /* HAVE_SMB_SHARE */
810 zfs_get_parent(const char *datasetname
, char *parent
, int parentsize
)
815 * Remove the @bla or /bla from the end of the name to get the parent.
817 (void) strncpy(parent
, datasetname
, parentsize
);
818 cp
= strrchr(parent
, '@');
822 cp
= strrchr(parent
, '/');
824 return (SET_ERROR(ENOENT
));
832 zfs_secpolicy_destroy_perms(const char *name
, cred_t
*cr
)
836 if ((error
= zfs_secpolicy_write_perms(name
,
837 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
840 return (zfs_secpolicy_write_perms(name
, ZFS_DELEG_PERM_DESTROY
, cr
));
845 zfs_secpolicy_destroy(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
847 return (zfs_secpolicy_destroy_perms(zc
->zc_name
, cr
));
851 * Destroying snapshots with delegated permissions requires
852 * descendant mount and destroy permissions.
856 zfs_secpolicy_destroy_snaps(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
859 nvpair_t
*pair
, *nextpair
;
862 if (nvlist_lookup_nvlist(innvl
, "snaps", &snaps
) != 0)
863 return (SET_ERROR(EINVAL
));
864 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
866 nextpair
= nvlist_next_nvpair(snaps
, pair
);
867 error
= zfs_secpolicy_destroy_perms(nvpair_name(pair
), cr
);
868 if (error
== ENOENT
) {
870 * Ignore any snapshots that don't exist (we consider
871 * them "already destroyed"). Remove the name from the
872 * nvl here in case the snapshot is created between
873 * now and when we try to destroy it (in which case
874 * we don't want to destroy it since we haven't
875 * checked for permission).
877 fnvlist_remove_nvpair(snaps
, pair
);
888 zfs_secpolicy_rename_perms(const char *from
, const char *to
, cred_t
*cr
)
890 char parentname
[MAXNAMELEN
];
893 if ((error
= zfs_secpolicy_write_perms(from
,
894 ZFS_DELEG_PERM_RENAME
, cr
)) != 0)
897 if ((error
= zfs_secpolicy_write_perms(from
,
898 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
901 if ((error
= zfs_get_parent(to
, parentname
,
902 sizeof (parentname
))) != 0)
905 if ((error
= zfs_secpolicy_write_perms(parentname
,
906 ZFS_DELEG_PERM_CREATE
, cr
)) != 0)
909 if ((error
= zfs_secpolicy_write_perms(parentname
,
910 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
918 zfs_secpolicy_rename(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
920 return (zfs_secpolicy_rename_perms(zc
->zc_name
, zc
->zc_value
, cr
));
925 zfs_secpolicy_promote(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
928 dsl_dataset_t
*clone
;
931 error
= zfs_secpolicy_write_perms(zc
->zc_name
,
932 ZFS_DELEG_PERM_PROMOTE
, cr
);
936 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
940 error
= dsl_dataset_hold(dp
, zc
->zc_name
, FTAG
, &clone
);
943 char parentname
[MAXNAMELEN
];
944 dsl_dataset_t
*origin
= NULL
;
948 error
= dsl_dataset_hold_obj(dd
->dd_pool
,
949 dsl_dir_phys(dd
)->dd_origin_obj
, FTAG
, &origin
);
951 dsl_dataset_rele(clone
, FTAG
);
952 dsl_pool_rele(dp
, FTAG
);
956 error
= zfs_secpolicy_write_perms_ds(zc
->zc_name
, clone
,
957 ZFS_DELEG_PERM_MOUNT
, cr
);
959 dsl_dataset_name(origin
, parentname
);
961 error
= zfs_secpolicy_write_perms_ds(parentname
, origin
,
962 ZFS_DELEG_PERM_PROMOTE
, cr
);
964 dsl_dataset_rele(clone
, FTAG
);
965 dsl_dataset_rele(origin
, FTAG
);
967 dsl_pool_rele(dp
, FTAG
);
973 zfs_secpolicy_recv(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
977 if ((error
= zfs_secpolicy_write_perms(zc
->zc_name
,
978 ZFS_DELEG_PERM_RECEIVE
, cr
)) != 0)
981 if ((error
= zfs_secpolicy_write_perms(zc
->zc_name
,
982 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
985 return (zfs_secpolicy_write_perms(zc
->zc_name
,
986 ZFS_DELEG_PERM_CREATE
, cr
));
990 zfs_secpolicy_snapshot_perms(const char *name
, cred_t
*cr
)
992 return (zfs_secpolicy_write_perms(name
,
993 ZFS_DELEG_PERM_SNAPSHOT
, cr
));
997 * Check for permission to create each snapshot in the nvlist.
1001 zfs_secpolicy_snapshot(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1007 if (nvlist_lookup_nvlist(innvl
, "snaps", &snaps
) != 0)
1008 return (SET_ERROR(EINVAL
));
1009 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
1010 pair
= nvlist_next_nvpair(snaps
, pair
)) {
1011 char *name
= nvpair_name(pair
);
1012 char *atp
= strchr(name
, '@');
1015 error
= SET_ERROR(EINVAL
);
1019 error
= zfs_secpolicy_snapshot_perms(name
, cr
);
1028 * Check for permission to create each snapshot in the nvlist.
1032 zfs_secpolicy_bookmark(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1037 for (pair
= nvlist_next_nvpair(innvl
, NULL
);
1038 pair
!= NULL
; pair
= nvlist_next_nvpair(innvl
, pair
)) {
1039 char *name
= nvpair_name(pair
);
1040 char *hashp
= strchr(name
, '#');
1042 if (hashp
== NULL
) {
1043 error
= SET_ERROR(EINVAL
);
1047 error
= zfs_secpolicy_write_perms(name
,
1048 ZFS_DELEG_PERM_BOOKMARK
, cr
);
1058 zfs_secpolicy_destroy_bookmarks(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1060 nvpair_t
*pair
, *nextpair
;
1063 for (pair
= nvlist_next_nvpair(innvl
, NULL
); pair
!= NULL
;
1065 char *name
= nvpair_name(pair
);
1066 char *hashp
= strchr(name
, '#');
1067 nextpair
= nvlist_next_nvpair(innvl
, pair
);
1069 if (hashp
== NULL
) {
1070 error
= SET_ERROR(EINVAL
);
1075 error
= zfs_secpolicy_write_perms(name
,
1076 ZFS_DELEG_PERM_DESTROY
, cr
);
1078 if (error
== ENOENT
) {
1080 * Ignore any filesystems that don't exist (we consider
1081 * their bookmarks "already destroyed"). Remove
1082 * the name from the nvl here in case the filesystem
1083 * is created between now and when we try to destroy
1084 * the bookmark (in which case we don't want to
1085 * destroy it since we haven't checked for permission).
1087 fnvlist_remove_nvpair(innvl
, pair
);
1099 zfs_secpolicy_log_history(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1102 * Even root must have a proper TSD so that we know what pool
1105 if (tsd_get(zfs_allow_log_key
) == NULL
)
1106 return (SET_ERROR(EPERM
));
1111 zfs_secpolicy_create_clone(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1113 char parentname
[MAXNAMELEN
];
1117 if ((error
= zfs_get_parent(zc
->zc_name
, parentname
,
1118 sizeof (parentname
))) != 0)
1121 if (nvlist_lookup_string(innvl
, "origin", &origin
) == 0 &&
1122 (error
= zfs_secpolicy_write_perms(origin
,
1123 ZFS_DELEG_PERM_CLONE
, cr
)) != 0)
1126 if ((error
= zfs_secpolicy_write_perms(parentname
,
1127 ZFS_DELEG_PERM_CREATE
, cr
)) != 0)
1130 return (zfs_secpolicy_write_perms(parentname
,
1131 ZFS_DELEG_PERM_MOUNT
, cr
));
1135 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
1136 * SYS_CONFIG privilege, which is not available in a local zone.
1140 zfs_secpolicy_config(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1142 if (secpolicy_sys_config(cr
, B_FALSE
) != 0)
1143 return (SET_ERROR(EPERM
));
1149 * Policy for object to name lookups.
1153 zfs_secpolicy_diff(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1157 if ((error
= secpolicy_sys_config(cr
, B_FALSE
)) == 0)
1160 error
= zfs_secpolicy_write_perms(zc
->zc_name
, ZFS_DELEG_PERM_DIFF
, cr
);
1165 * Policy for fault injection. Requires all privileges.
1169 zfs_secpolicy_inject(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1171 return (secpolicy_zinject(cr
));
1176 zfs_secpolicy_inherit_prop(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1178 zfs_prop_t prop
= zfs_name_to_prop(zc
->zc_value
);
1180 if (prop
== ZPROP_INVAL
) {
1181 if (!zfs_prop_user(zc
->zc_value
))
1182 return (SET_ERROR(EINVAL
));
1183 return (zfs_secpolicy_write_perms(zc
->zc_name
,
1184 ZFS_DELEG_PERM_USERPROP
, cr
));
1186 return (zfs_secpolicy_setprop(zc
->zc_name
, prop
,
1192 zfs_secpolicy_userspace_one(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1194 int err
= zfs_secpolicy_read(zc
, innvl
, cr
);
1198 if (zc
->zc_objset_type
>= ZFS_NUM_USERQUOTA_PROPS
)
1199 return (SET_ERROR(EINVAL
));
1201 if (zc
->zc_value
[0] == 0) {
1203 * They are asking about a posix uid/gid. If it's
1204 * themself, allow it.
1206 if (zc
->zc_objset_type
== ZFS_PROP_USERUSED
||
1207 zc
->zc_objset_type
== ZFS_PROP_USERQUOTA
) {
1208 if (zc
->zc_guid
== crgetuid(cr
))
1211 if (groupmember(zc
->zc_guid
, cr
))
1216 return (zfs_secpolicy_write_perms(zc
->zc_name
,
1217 userquota_perms
[zc
->zc_objset_type
], cr
));
1221 zfs_secpolicy_userspace_many(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1223 int err
= zfs_secpolicy_read(zc
, innvl
, cr
);
1227 if (zc
->zc_objset_type
>= ZFS_NUM_USERQUOTA_PROPS
)
1228 return (SET_ERROR(EINVAL
));
1230 return (zfs_secpolicy_write_perms(zc
->zc_name
,
1231 userquota_perms
[zc
->zc_objset_type
], cr
));
1236 zfs_secpolicy_userspace_upgrade(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1238 return (zfs_secpolicy_setprop(zc
->zc_name
, ZFS_PROP_VERSION
,
1244 zfs_secpolicy_hold(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1250 error
= nvlist_lookup_nvlist(innvl
, "holds", &holds
);
1252 return (SET_ERROR(EINVAL
));
1254 for (pair
= nvlist_next_nvpair(holds
, NULL
); pair
!= NULL
;
1255 pair
= nvlist_next_nvpair(holds
, pair
)) {
1256 char fsname
[MAXNAMELEN
];
1257 error
= dmu_fsname(nvpair_name(pair
), fsname
);
1260 error
= zfs_secpolicy_write_perms(fsname
,
1261 ZFS_DELEG_PERM_HOLD
, cr
);
1270 zfs_secpolicy_release(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1275 for (pair
= nvlist_next_nvpair(innvl
, NULL
); pair
!= NULL
;
1276 pair
= nvlist_next_nvpair(innvl
, pair
)) {
1277 char fsname
[MAXNAMELEN
];
1278 error
= dmu_fsname(nvpair_name(pair
), fsname
);
1281 error
= zfs_secpolicy_write_perms(fsname
,
1282 ZFS_DELEG_PERM_RELEASE
, cr
);
1290 * Policy for allowing temporary snapshots to be taken or released
1293 zfs_secpolicy_tmp_snapshot(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1296 * A temporary snapshot is the same as a snapshot,
1297 * hold, destroy and release all rolled into one.
1298 * Delegated diff alone is sufficient that we allow this.
1302 if ((error
= zfs_secpolicy_write_perms(zc
->zc_name
,
1303 ZFS_DELEG_PERM_DIFF
, cr
)) == 0)
1306 error
= zfs_secpolicy_snapshot_perms(zc
->zc_name
, cr
);
1308 error
= zfs_secpolicy_hold(zc
, innvl
, cr
);
1310 error
= zfs_secpolicy_release(zc
, innvl
, cr
);
1312 error
= zfs_secpolicy_destroy(zc
, innvl
, cr
);
1317 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1320 get_nvlist(uint64_t nvl
, uint64_t size
, int iflag
, nvlist_t
**nvp
)
1324 nvlist_t
*list
= NULL
;
1327 * Read in and unpack the user-supplied nvlist.
1330 return (SET_ERROR(EINVAL
));
1332 packed
= vmem_alloc(size
, KM_SLEEP
);
1334 if ((error
= ddi_copyin((void *)(uintptr_t)nvl
, packed
, size
,
1336 vmem_free(packed
, size
);
1340 if ((error
= nvlist_unpack(packed
, size
, &list
, 0)) != 0) {
1341 vmem_free(packed
, size
);
1345 vmem_free(packed
, size
);
1352 * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
1353 * Entries will be removed from the end of the nvlist, and one int32 entry
1354 * named "N_MORE_ERRORS" will be added indicating how many entries were
1358 nvlist_smush(nvlist_t
*errors
, size_t max
)
1362 size
= fnvlist_size(errors
);
1365 nvpair_t
*more_errors
;
1369 return (SET_ERROR(ENOMEM
));
1371 fnvlist_add_int32(errors
, ZPROP_N_MORE_ERRORS
, 0);
1372 more_errors
= nvlist_prev_nvpair(errors
, NULL
);
1375 nvpair_t
*pair
= nvlist_prev_nvpair(errors
,
1377 fnvlist_remove_nvpair(errors
, pair
);
1379 size
= fnvlist_size(errors
);
1380 } while (size
> max
);
1382 fnvlist_remove_nvpair(errors
, more_errors
);
1383 fnvlist_add_int32(errors
, ZPROP_N_MORE_ERRORS
, n
);
1384 ASSERT3U(fnvlist_size(errors
), <=, max
);
1391 put_nvlist(zfs_cmd_t
*zc
, nvlist_t
*nvl
)
1393 char *packed
= NULL
;
1397 size
= fnvlist_size(nvl
);
1399 if (size
> zc
->zc_nvlist_dst_size
) {
1400 error
= SET_ERROR(ENOMEM
);
1402 packed
= fnvlist_pack(nvl
, &size
);
1403 if (ddi_copyout(packed
, (void *)(uintptr_t)zc
->zc_nvlist_dst
,
1404 size
, zc
->zc_iflags
) != 0)
1405 error
= SET_ERROR(EFAULT
);
1406 fnvlist_pack_free(packed
, size
);
1409 zc
->zc_nvlist_dst_size
= size
;
1410 zc
->zc_nvlist_dst_filled
= B_TRUE
;
1415 get_zfs_sb(const char *dsname
, zfs_sb_t
**zsbp
)
1420 error
= dmu_objset_hold(dsname
, FTAG
, &os
);
1423 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
1424 dmu_objset_rele(os
, FTAG
);
1425 return (SET_ERROR(EINVAL
));
1428 mutex_enter(&os
->os_user_ptr_lock
);
1429 *zsbp
= dmu_objset_get_user(os
);
1430 if (*zsbp
&& (*zsbp
)->z_sb
) {
1431 atomic_inc(&((*zsbp
)->z_sb
->s_active
));
1433 error
= SET_ERROR(ESRCH
);
1435 mutex_exit(&os
->os_user_ptr_lock
);
1436 dmu_objset_rele(os
, FTAG
);
1441 * Find a zfs_sb_t for a mounted filesystem, or create our own, in which
1442 * case its z_sb will be NULL, and it will be opened as the owner.
1443 * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1444 * which prevents all inode ops from running.
1447 zfs_sb_hold(const char *name
, void *tag
, zfs_sb_t
**zsbp
, boolean_t writer
)
1451 if (get_zfs_sb(name
, zsbp
) != 0)
1452 error
= zfs_sb_create(name
, zsbp
);
1454 rrw_enter(&(*zsbp
)->z_teardown_lock
, (writer
) ? RW_WRITER
:
1456 if ((*zsbp
)->z_unmounted
) {
1458 * XXX we could probably try again, since the unmounting
1459 * thread should be just about to disassociate the
1460 * objset from the zsb.
1462 rrw_exit(&(*zsbp
)->z_teardown_lock
, tag
);
1463 return (SET_ERROR(EBUSY
));
1470 zfs_sb_rele(zfs_sb_t
*zsb
, void *tag
)
1472 rrw_exit(&zsb
->z_teardown_lock
, tag
);
1475 deactivate_super(zsb
->z_sb
);
1477 dmu_objset_disown(zsb
->z_os
, zsb
);
1483 zfs_ioc_pool_create(zfs_cmd_t
*zc
)
1486 nvlist_t
*config
, *props
= NULL
;
1487 nvlist_t
*rootprops
= NULL
;
1488 nvlist_t
*zplprops
= NULL
;
1490 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1491 zc
->zc_iflags
, &config
)))
1494 if (zc
->zc_nvlist_src_size
!= 0 && (error
=
1495 get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1496 zc
->zc_iflags
, &props
))) {
1497 nvlist_free(config
);
1502 nvlist_t
*nvl
= NULL
;
1503 uint64_t version
= SPA_VERSION
;
1505 (void) nvlist_lookup_uint64(props
,
1506 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
);
1507 if (!SPA_VERSION_IS_SUPPORTED(version
)) {
1508 error
= SET_ERROR(EINVAL
);
1509 goto pool_props_bad
;
1511 (void) nvlist_lookup_nvlist(props
, ZPOOL_ROOTFS_PROPS
, &nvl
);
1513 error
= nvlist_dup(nvl
, &rootprops
, KM_SLEEP
);
1515 nvlist_free(config
);
1519 (void) nvlist_remove_all(props
, ZPOOL_ROOTFS_PROPS
);
1521 VERIFY(nvlist_alloc(&zplprops
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1522 error
= zfs_fill_zplprops_root(version
, rootprops
,
1525 goto pool_props_bad
;
1528 error
= spa_create(zc
->zc_name
, config
, props
, zplprops
);
1531 * Set the remaining root properties
1533 if (!error
&& (error
= zfs_set_prop_nvlist(zc
->zc_name
,
1534 ZPROP_SRC_LOCAL
, rootprops
, NULL
)) != 0)
1535 (void) spa_destroy(zc
->zc_name
);
1538 nvlist_free(rootprops
);
1539 nvlist_free(zplprops
);
1540 nvlist_free(config
);
1547 zfs_ioc_pool_destroy(zfs_cmd_t
*zc
)
1550 zfs_log_history(zc
);
1551 error
= spa_destroy(zc
->zc_name
);
1553 zvol_remove_minors(zc
->zc_name
);
1558 zfs_ioc_pool_import(zfs_cmd_t
*zc
)
1560 nvlist_t
*config
, *props
= NULL
;
1564 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1565 zc
->zc_iflags
, &config
)) != 0)
1568 if (zc
->zc_nvlist_src_size
!= 0 && (error
=
1569 get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1570 zc
->zc_iflags
, &props
))) {
1571 nvlist_free(config
);
1575 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &guid
) != 0 ||
1576 guid
!= zc
->zc_guid
)
1577 error
= SET_ERROR(EINVAL
);
1579 error
= spa_import(zc
->zc_name
, config
, props
, zc
->zc_cookie
);
1581 if (zc
->zc_nvlist_dst
!= 0) {
1584 if ((err
= put_nvlist(zc
, config
)) != 0)
1588 nvlist_free(config
);
1597 zfs_ioc_pool_export(zfs_cmd_t
*zc
)
1600 boolean_t force
= (boolean_t
)zc
->zc_cookie
;
1601 boolean_t hardforce
= (boolean_t
)zc
->zc_guid
;
1603 zfs_log_history(zc
);
1604 error
= spa_export(zc
->zc_name
, NULL
, force
, hardforce
);
1606 zvol_remove_minors(zc
->zc_name
);
1611 zfs_ioc_pool_configs(zfs_cmd_t
*zc
)
1616 if ((configs
= spa_all_configs(&zc
->zc_cookie
)) == NULL
)
1617 return (SET_ERROR(EEXIST
));
1619 error
= put_nvlist(zc
, configs
);
1621 nvlist_free(configs
);
1628 * zc_name name of the pool
1631 * zc_cookie real errno
1632 * zc_nvlist_dst config nvlist
1633 * zc_nvlist_dst_size size of config nvlist
1636 zfs_ioc_pool_stats(zfs_cmd_t
*zc
)
1642 error
= spa_get_stats(zc
->zc_name
, &config
, zc
->zc_value
,
1643 sizeof (zc
->zc_value
));
1645 if (config
!= NULL
) {
1646 ret
= put_nvlist(zc
, config
);
1647 nvlist_free(config
);
1650 * The config may be present even if 'error' is non-zero.
1651 * In this case we return success, and preserve the real errno
1654 zc
->zc_cookie
= error
;
1663 * Try to import the given pool, returning pool stats as appropriate so that
1664 * user land knows which devices are available and overall pool health.
1667 zfs_ioc_pool_tryimport(zfs_cmd_t
*zc
)
1669 nvlist_t
*tryconfig
, *config
;
1672 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1673 zc
->zc_iflags
, &tryconfig
)) != 0)
1676 config
= spa_tryimport(tryconfig
);
1678 nvlist_free(tryconfig
);
1681 return (SET_ERROR(EINVAL
));
1683 error
= put_nvlist(zc
, config
);
1684 nvlist_free(config
);
1691 * zc_name name of the pool
1692 * zc_cookie scan func (pool_scan_func_t)
1695 zfs_ioc_pool_scan(zfs_cmd_t
*zc
)
1700 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1703 if (zc
->zc_cookie
== POOL_SCAN_NONE
)
1704 error
= spa_scan_stop(spa
);
1706 error
= spa_scan(spa
, zc
->zc_cookie
);
1708 spa_close(spa
, FTAG
);
1714 zfs_ioc_pool_freeze(zfs_cmd_t
*zc
)
1719 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1722 spa_close(spa
, FTAG
);
1728 zfs_ioc_pool_upgrade(zfs_cmd_t
*zc
)
1733 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1736 if (zc
->zc_cookie
< spa_version(spa
) ||
1737 !SPA_VERSION_IS_SUPPORTED(zc
->zc_cookie
)) {
1738 spa_close(spa
, FTAG
);
1739 return (SET_ERROR(EINVAL
));
1742 spa_upgrade(spa
, zc
->zc_cookie
);
1743 spa_close(spa
, FTAG
);
1749 zfs_ioc_pool_get_history(zfs_cmd_t
*zc
)
1756 if ((size
= zc
->zc_history_len
) == 0)
1757 return (SET_ERROR(EINVAL
));
1759 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1762 if (spa_version(spa
) < SPA_VERSION_ZPOOL_HISTORY
) {
1763 spa_close(spa
, FTAG
);
1764 return (SET_ERROR(ENOTSUP
));
1767 hist_buf
= vmem_alloc(size
, KM_SLEEP
);
1768 if ((error
= spa_history_get(spa
, &zc
->zc_history_offset
,
1769 &zc
->zc_history_len
, hist_buf
)) == 0) {
1770 error
= ddi_copyout(hist_buf
,
1771 (void *)(uintptr_t)zc
->zc_history
,
1772 zc
->zc_history_len
, zc
->zc_iflags
);
1775 spa_close(spa
, FTAG
);
1776 vmem_free(hist_buf
, size
);
1781 zfs_ioc_pool_reguid(zfs_cmd_t
*zc
)
1786 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1788 error
= spa_change_guid(spa
);
1789 spa_close(spa
, FTAG
);
1795 zfs_ioc_dsobj_to_dsname(zfs_cmd_t
*zc
)
1797 return (dsl_dsobj_to_dsname(zc
->zc_name
, zc
->zc_obj
, zc
->zc_value
));
1802 * zc_name name of filesystem
1803 * zc_obj object to find
1806 * zc_value name of object
1809 zfs_ioc_obj_to_path(zfs_cmd_t
*zc
)
1814 /* XXX reading from objset not owned */
1815 if ((error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
)) != 0)
1817 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
1818 dmu_objset_rele(os
, FTAG
);
1819 return (SET_ERROR(EINVAL
));
1821 error
= zfs_obj_to_path(os
, zc
->zc_obj
, zc
->zc_value
,
1822 sizeof (zc
->zc_value
));
1823 dmu_objset_rele(os
, FTAG
);
1830 * zc_name name of filesystem
1831 * zc_obj object to find
1834 * zc_stat stats on object
1835 * zc_value path to object
1838 zfs_ioc_obj_to_stats(zfs_cmd_t
*zc
)
1843 /* XXX reading from objset not owned */
1844 if ((error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
)) != 0)
1846 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
1847 dmu_objset_rele(os
, FTAG
);
1848 return (SET_ERROR(EINVAL
));
1850 error
= zfs_obj_to_stats(os
, zc
->zc_obj
, &zc
->zc_stat
, zc
->zc_value
,
1851 sizeof (zc
->zc_value
));
1852 dmu_objset_rele(os
, FTAG
);
1858 zfs_ioc_vdev_add(zfs_cmd_t
*zc
)
1864 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1868 error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1869 zc
->zc_iflags
, &config
);
1871 error
= spa_vdev_add(spa
, config
);
1872 nvlist_free(config
);
1874 spa_close(spa
, FTAG
);
1880 * zc_name name of the pool
1881 * zc_nvlist_conf nvlist of devices to remove
1882 * zc_cookie to stop the remove?
1885 zfs_ioc_vdev_remove(zfs_cmd_t
*zc
)
1890 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1893 error
= spa_vdev_remove(spa
, zc
->zc_guid
, B_FALSE
);
1894 spa_close(spa
, FTAG
);
1899 zfs_ioc_vdev_set_state(zfs_cmd_t
*zc
)
1903 vdev_state_t newstate
= VDEV_STATE_UNKNOWN
;
1905 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1907 switch (zc
->zc_cookie
) {
1908 case VDEV_STATE_ONLINE
:
1909 error
= vdev_online(spa
, zc
->zc_guid
, zc
->zc_obj
, &newstate
);
1912 case VDEV_STATE_OFFLINE
:
1913 error
= vdev_offline(spa
, zc
->zc_guid
, zc
->zc_obj
);
1916 case VDEV_STATE_FAULTED
:
1917 if (zc
->zc_obj
!= VDEV_AUX_ERR_EXCEEDED
&&
1918 zc
->zc_obj
!= VDEV_AUX_EXTERNAL
)
1919 zc
->zc_obj
= VDEV_AUX_ERR_EXCEEDED
;
1921 error
= vdev_fault(spa
, zc
->zc_guid
, zc
->zc_obj
);
1924 case VDEV_STATE_DEGRADED
:
1925 if (zc
->zc_obj
!= VDEV_AUX_ERR_EXCEEDED
&&
1926 zc
->zc_obj
!= VDEV_AUX_EXTERNAL
)
1927 zc
->zc_obj
= VDEV_AUX_ERR_EXCEEDED
;
1929 error
= vdev_degrade(spa
, zc
->zc_guid
, zc
->zc_obj
);
1933 error
= SET_ERROR(EINVAL
);
1935 zc
->zc_cookie
= newstate
;
1936 spa_close(spa
, FTAG
);
1941 zfs_ioc_vdev_attach(zfs_cmd_t
*zc
)
1944 int replacing
= zc
->zc_cookie
;
1948 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1951 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1952 zc
->zc_iflags
, &config
)) == 0) {
1953 error
= spa_vdev_attach(spa
, zc
->zc_guid
, config
, replacing
);
1954 nvlist_free(config
);
1957 spa_close(spa
, FTAG
);
1962 zfs_ioc_vdev_detach(zfs_cmd_t
*zc
)
1967 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1970 error
= spa_vdev_detach(spa
, zc
->zc_guid
, 0, B_FALSE
);
1972 spa_close(spa
, FTAG
);
1977 zfs_ioc_vdev_split(zfs_cmd_t
*zc
)
1980 nvlist_t
*config
, *props
= NULL
;
1982 boolean_t exp
= !!(zc
->zc_cookie
& ZPOOL_EXPORT_AFTER_SPLIT
);
1984 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1987 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1988 zc
->zc_iflags
, &config
))) {
1989 spa_close(spa
, FTAG
);
1993 if (zc
->zc_nvlist_src_size
!= 0 && (error
=
1994 get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1995 zc
->zc_iflags
, &props
))) {
1996 spa_close(spa
, FTAG
);
1997 nvlist_free(config
);
2001 error
= spa_vdev_split_mirror(spa
, zc
->zc_string
, config
, props
, exp
);
2003 spa_close(spa
, FTAG
);
2005 nvlist_free(config
);
2012 zfs_ioc_vdev_setpath(zfs_cmd_t
*zc
)
2015 char *path
= zc
->zc_value
;
2016 uint64_t guid
= zc
->zc_guid
;
2019 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
2023 error
= spa_vdev_setpath(spa
, guid
, path
);
2024 spa_close(spa
, FTAG
);
2029 zfs_ioc_vdev_setfru(zfs_cmd_t
*zc
)
2032 char *fru
= zc
->zc_value
;
2033 uint64_t guid
= zc
->zc_guid
;
2036 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
2040 error
= spa_vdev_setfru(spa
, guid
, fru
);
2041 spa_close(spa
, FTAG
);
2046 zfs_ioc_objset_stats_impl(zfs_cmd_t
*zc
, objset_t
*os
)
2051 dmu_objset_fast_stat(os
, &zc
->zc_objset_stats
);
2053 if (zc
->zc_nvlist_dst
!= 0 &&
2054 (error
= dsl_prop_get_all(os
, &nv
)) == 0) {
2055 dmu_objset_stats(os
, nv
);
2057 * NB: zvol_get_stats() will read the objset contents,
2058 * which we aren't supposed to do with a
2059 * DS_MODE_USER hold, because it could be
2060 * inconsistent. So this is a bit of a workaround...
2061 * XXX reading with out owning
2063 if (!zc
->zc_objset_stats
.dds_inconsistent
&&
2064 dmu_objset_type(os
) == DMU_OST_ZVOL
) {
2065 error
= zvol_get_stats(os
, nv
);
2071 error
= put_nvlist(zc
, nv
);
2080 * zc_name name of filesystem
2081 * zc_nvlist_dst_size size of buffer for property nvlist
2084 * zc_objset_stats stats
2085 * zc_nvlist_dst property nvlist
2086 * zc_nvlist_dst_size size of property nvlist
2089 zfs_ioc_objset_stats(zfs_cmd_t
*zc
)
2094 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
2096 error
= zfs_ioc_objset_stats_impl(zc
, os
);
2097 dmu_objset_rele(os
, FTAG
);
2105 * zc_name name of filesystem
2106 * zc_nvlist_dst_size size of buffer for property nvlist
2109 * zc_nvlist_dst received property nvlist
2110 * zc_nvlist_dst_size size of received property nvlist
2112 * Gets received properties (distinct from local properties on or after
2113 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
2114 * local property values.
2117 zfs_ioc_objset_recvd_props(zfs_cmd_t
*zc
)
2123 * Without this check, we would return local property values if the
2124 * caller has not already received properties on or after
2125 * SPA_VERSION_RECVD_PROPS.
2127 if (!dsl_prop_get_hasrecvd(zc
->zc_name
))
2128 return (SET_ERROR(ENOTSUP
));
2130 if (zc
->zc_nvlist_dst
!= 0 &&
2131 (error
= dsl_prop_get_received(zc
->zc_name
, &nv
)) == 0) {
2132 error
= put_nvlist(zc
, nv
);
2140 nvl_add_zplprop(objset_t
*os
, nvlist_t
*props
, zfs_prop_t prop
)
2146 * zfs_get_zplprop() will either find a value or give us
2147 * the default value (if there is one).
2149 if ((error
= zfs_get_zplprop(os
, prop
, &value
)) != 0)
2151 VERIFY(nvlist_add_uint64(props
, zfs_prop_to_name(prop
), value
) == 0);
2157 * zc_name name of filesystem
2158 * zc_nvlist_dst_size size of buffer for zpl property nvlist
2161 * zc_nvlist_dst zpl property nvlist
2162 * zc_nvlist_dst_size size of zpl property nvlist
2165 zfs_ioc_objset_zplprops(zfs_cmd_t
*zc
)
2170 /* XXX reading without owning */
2171 if ((err
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
)))
2174 dmu_objset_fast_stat(os
, &zc
->zc_objset_stats
);
2177 * NB: nvl_add_zplprop() will read the objset contents,
2178 * which we aren't supposed to do with a DS_MODE_USER
2179 * hold, because it could be inconsistent.
2181 if (zc
->zc_nvlist_dst
!= 0 &&
2182 !zc
->zc_objset_stats
.dds_inconsistent
&&
2183 dmu_objset_type(os
) == DMU_OST_ZFS
) {
2186 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2187 if ((err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_VERSION
)) == 0 &&
2188 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_NORMALIZE
)) == 0 &&
2189 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_UTF8ONLY
)) == 0 &&
2190 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_CASE
)) == 0)
2191 err
= put_nvlist(zc
, nv
);
2194 err
= SET_ERROR(ENOENT
);
2196 dmu_objset_rele(os
, FTAG
);
2201 dataset_name_hidden(const char *name
)
2204 * Skip over datasets that are not visible in this zone,
2205 * internal datasets (which have a $ in their name), and
2206 * temporary datasets (which have a % in their name).
2208 if (strchr(name
, '$') != NULL
)
2210 if (strchr(name
, '%') != NULL
)
2212 if (!INGLOBALZONE(curproc
) && !zone_dataset_visible(name
, NULL
))
2219 * zc_name name of filesystem
2220 * zc_cookie zap cursor
2221 * zc_nvlist_dst_size size of buffer for property nvlist
2224 * zc_name name of next filesystem
2225 * zc_cookie zap cursor
2226 * zc_objset_stats stats
2227 * zc_nvlist_dst property nvlist
2228 * zc_nvlist_dst_size size of property nvlist
2231 zfs_ioc_dataset_list_next(zfs_cmd_t
*zc
)
2236 size_t orig_len
= strlen(zc
->zc_name
);
2239 if ((error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
))) {
2240 if (error
== ENOENT
)
2241 error
= SET_ERROR(ESRCH
);
2245 p
= strrchr(zc
->zc_name
, '/');
2246 if (p
== NULL
|| p
[1] != '\0')
2247 (void) strlcat(zc
->zc_name
, "/", sizeof (zc
->zc_name
));
2248 p
= zc
->zc_name
+ strlen(zc
->zc_name
);
2251 error
= dmu_dir_list_next(os
,
2252 sizeof (zc
->zc_name
) - (p
- zc
->zc_name
), p
,
2253 NULL
, &zc
->zc_cookie
);
2254 if (error
== ENOENT
)
2255 error
= SET_ERROR(ESRCH
);
2256 } while (error
== 0 && dataset_name_hidden(zc
->zc_name
));
2257 dmu_objset_rele(os
, FTAG
);
2260 * If it's an internal dataset (ie. with a '$' in its name),
2261 * don't try to get stats for it, otherwise we'll return ENOENT.
2263 if (error
== 0 && strchr(zc
->zc_name
, '$') == NULL
) {
2264 error
= zfs_ioc_objset_stats(zc
); /* fill in the stats */
2265 if (error
== ENOENT
) {
2266 /* We lost a race with destroy, get the next one. */
2267 zc
->zc_name
[orig_len
] = '\0';
2276 * zc_name name of filesystem
2277 * zc_cookie zap cursor
2278 * zc_nvlist_dst_size size of buffer for property nvlist
2281 * zc_name name of next snapshot
2282 * zc_objset_stats stats
2283 * zc_nvlist_dst property nvlist
2284 * zc_nvlist_dst_size size of property nvlist
2287 zfs_ioc_snapshot_list_next(zfs_cmd_t
*zc
)
2292 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
2294 return (error
== ENOENT
? ESRCH
: error
);
2298 * A dataset name of maximum length cannot have any snapshots,
2299 * so exit immediately.
2301 if (strlcat(zc
->zc_name
, "@", sizeof (zc
->zc_name
)) >= MAXNAMELEN
) {
2302 dmu_objset_rele(os
, FTAG
);
2303 return (SET_ERROR(ESRCH
));
2306 error
= dmu_snapshot_list_next(os
,
2307 sizeof (zc
->zc_name
) - strlen(zc
->zc_name
),
2308 zc
->zc_name
+ strlen(zc
->zc_name
), &zc
->zc_obj
, &zc
->zc_cookie
,
2311 if (error
== 0 && !zc
->zc_simple
) {
2313 dsl_pool_t
*dp
= os
->os_dsl_dataset
->ds_dir
->dd_pool
;
2315 error
= dsl_dataset_hold_obj(dp
, zc
->zc_obj
, FTAG
, &ds
);
2319 error
= dmu_objset_from_ds(ds
, &ossnap
);
2321 error
= zfs_ioc_objset_stats_impl(zc
, ossnap
);
2322 dsl_dataset_rele(ds
, FTAG
);
2324 } else if (error
== ENOENT
) {
2325 error
= SET_ERROR(ESRCH
);
2328 dmu_objset_rele(os
, FTAG
);
2329 /* if we failed, undo the @ that we tacked on to zc_name */
2331 *strchr(zc
->zc_name
, '@') = '\0';
2336 zfs_prop_set_userquota(const char *dsname
, nvpair_t
*pair
)
2338 const char *propname
= nvpair_name(pair
);
2340 unsigned int vallen
;
2343 zfs_userquota_prop_t type
;
2349 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2351 VERIFY(nvpair_value_nvlist(pair
, &attrs
) == 0);
2352 if (nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
2354 return (SET_ERROR(EINVAL
));
2358 * A correctly constructed propname is encoded as
2359 * userquota@<rid>-<domain>.
2361 if ((dash
= strchr(propname
, '-')) == NULL
||
2362 nvpair_value_uint64_array(pair
, &valary
, &vallen
) != 0 ||
2364 return (SET_ERROR(EINVAL
));
2371 err
= zfs_sb_hold(dsname
, FTAG
, &zsb
, B_FALSE
);
2373 err
= zfs_set_userquota(zsb
, type
, domain
, rid
, quota
);
2374 zfs_sb_rele(zsb
, FTAG
);
2381 * If the named property is one that has a special function to set its value,
2382 * return 0 on success and a positive error code on failure; otherwise if it is
2383 * not one of the special properties handled by this function, return -1.
2385 * XXX: It would be better for callers of the property interface if we handled
2386 * these special cases in dsl_prop.c (in the dsl layer).
2389 zfs_prop_set_special(const char *dsname
, zprop_source_t source
,
2392 const char *propname
= nvpair_name(pair
);
2393 zfs_prop_t prop
= zfs_name_to_prop(propname
);
2397 if (prop
== ZPROP_INVAL
) {
2398 if (zfs_prop_userquota(propname
))
2399 return (zfs_prop_set_userquota(dsname
, pair
));
2403 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2405 VERIFY(nvpair_value_nvlist(pair
, &attrs
) == 0);
2406 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
2410 if (zfs_prop_get_type(prop
) == PROP_TYPE_STRING
)
2413 VERIFY(0 == nvpair_value_uint64(pair
, &intval
));
2416 case ZFS_PROP_QUOTA
:
2417 err
= dsl_dir_set_quota(dsname
, source
, intval
);
2419 case ZFS_PROP_REFQUOTA
:
2420 err
= dsl_dataset_set_refquota(dsname
, source
, intval
);
2422 case ZFS_PROP_FILESYSTEM_LIMIT
:
2423 case ZFS_PROP_SNAPSHOT_LIMIT
:
2424 if (intval
== UINT64_MAX
) {
2425 /* clearing the limit, just do it */
2428 err
= dsl_dir_activate_fs_ss_limit(dsname
);
2431 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2432 * default path to set the value in the nvlist.
2437 case ZFS_PROP_RESERVATION
:
2438 err
= dsl_dir_set_reservation(dsname
, source
, intval
);
2440 case ZFS_PROP_REFRESERVATION
:
2441 err
= dsl_dataset_set_refreservation(dsname
, source
, intval
);
2443 case ZFS_PROP_VOLSIZE
:
2444 err
= zvol_set_volsize(dsname
, intval
);
2446 case ZFS_PROP_SNAPDEV
:
2447 err
= zvol_set_snapdev(dsname
, intval
);
2449 case ZFS_PROP_VERSION
:
2453 if ((err
= zfs_sb_hold(dsname
, FTAG
, &zsb
, B_TRUE
)) != 0)
2456 err
= zfs_set_version(zsb
, intval
);
2457 zfs_sb_rele(zsb
, FTAG
);
2459 if (err
== 0 && intval
>= ZPL_VERSION_USERSPACE
) {
2462 zc
= kmem_zalloc(sizeof (zfs_cmd_t
), KM_SLEEP
);
2463 (void) strcpy(zc
->zc_name
, dsname
);
2464 (void) zfs_ioc_userspace_upgrade(zc
);
2465 kmem_free(zc
, sizeof (zfs_cmd_t
));
2477 * This function is best effort. If it fails to set any of the given properties,
2478 * it continues to set as many as it can and returns the last error
2479 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2480 * with the list of names of all the properties that failed along with the
2481 * corresponding error numbers.
2483 * If every property is set successfully, zero is returned and errlist is not
2487 zfs_set_prop_nvlist(const char *dsname
, zprop_source_t source
, nvlist_t
*nvl
,
2496 nvlist_t
*genericnvl
= fnvlist_alloc();
2497 nvlist_t
*retrynvl
= fnvlist_alloc();
2500 while ((pair
= nvlist_next_nvpair(nvl
, pair
)) != NULL
) {
2501 const char *propname
= nvpair_name(pair
);
2502 zfs_prop_t prop
= zfs_name_to_prop(propname
);
2505 /* decode the property value */
2507 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2509 attrs
= fnvpair_value_nvlist(pair
);
2510 if (nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
2512 err
= SET_ERROR(EINVAL
);
2515 /* Validate value type */
2516 if (err
== 0 && prop
== ZPROP_INVAL
) {
2517 if (zfs_prop_user(propname
)) {
2518 if (nvpair_type(propval
) != DATA_TYPE_STRING
)
2519 err
= SET_ERROR(EINVAL
);
2520 } else if (zfs_prop_userquota(propname
)) {
2521 if (nvpair_type(propval
) !=
2522 DATA_TYPE_UINT64_ARRAY
)
2523 err
= SET_ERROR(EINVAL
);
2525 err
= SET_ERROR(EINVAL
);
2527 } else if (err
== 0) {
2528 if (nvpair_type(propval
) == DATA_TYPE_STRING
) {
2529 if (zfs_prop_get_type(prop
) != PROP_TYPE_STRING
)
2530 err
= SET_ERROR(EINVAL
);
2531 } else if (nvpair_type(propval
) == DATA_TYPE_UINT64
) {
2534 intval
= fnvpair_value_uint64(propval
);
2536 switch (zfs_prop_get_type(prop
)) {
2537 case PROP_TYPE_NUMBER
:
2539 case PROP_TYPE_STRING
:
2540 err
= SET_ERROR(EINVAL
);
2542 case PROP_TYPE_INDEX
:
2543 if (zfs_prop_index_to_string(prop
,
2544 intval
, &unused
) != 0)
2545 err
= SET_ERROR(EINVAL
);
2549 "unknown property type");
2552 err
= SET_ERROR(EINVAL
);
2556 /* Validate permissions */
2558 err
= zfs_check_settable(dsname
, pair
, CRED());
2561 err
= zfs_prop_set_special(dsname
, source
, pair
);
2564 * For better performance we build up a list of
2565 * properties to set in a single transaction.
2567 err
= nvlist_add_nvpair(genericnvl
, pair
);
2568 } else if (err
!= 0 && nvl
!= retrynvl
) {
2570 * This may be a spurious error caused by
2571 * receiving quota and reservation out of order.
2572 * Try again in a second pass.
2574 err
= nvlist_add_nvpair(retrynvl
, pair
);
2579 if (errlist
!= NULL
)
2580 fnvlist_add_int32(errlist
, propname
, err
);
2585 if (nvl
!= retrynvl
&& !nvlist_empty(retrynvl
)) {
2590 if (!nvlist_empty(genericnvl
) &&
2591 dsl_props_set(dsname
, source
, genericnvl
) != 0) {
2593 * If this fails, we still want to set as many properties as we
2594 * can, so try setting them individually.
2597 while ((pair
= nvlist_next_nvpair(genericnvl
, pair
)) != NULL
) {
2598 const char *propname
= nvpair_name(pair
);
2602 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2604 attrs
= fnvpair_value_nvlist(pair
);
2605 propval
= fnvlist_lookup_nvpair(attrs
,
2609 if (nvpair_type(propval
) == DATA_TYPE_STRING
) {
2610 strval
= fnvpair_value_string(propval
);
2611 err
= dsl_prop_set_string(dsname
, propname
,
2614 intval
= fnvpair_value_uint64(propval
);
2615 err
= dsl_prop_set_int(dsname
, propname
, source
,
2620 if (errlist
!= NULL
) {
2621 fnvlist_add_int32(errlist
, propname
,
2628 nvlist_free(genericnvl
);
2629 nvlist_free(retrynvl
);
2635 * Check that all the properties are valid user properties.
2638 zfs_check_userprops(const char *fsname
, nvlist_t
*nvl
)
2640 nvpair_t
*pair
= NULL
;
2643 while ((pair
= nvlist_next_nvpair(nvl
, pair
)) != NULL
) {
2644 const char *propname
= nvpair_name(pair
);
2646 if (!zfs_prop_user(propname
) ||
2647 nvpair_type(pair
) != DATA_TYPE_STRING
)
2648 return (SET_ERROR(EINVAL
));
2650 if ((error
= zfs_secpolicy_write_perms(fsname
,
2651 ZFS_DELEG_PERM_USERPROP
, CRED())))
2654 if (strlen(propname
) >= ZAP_MAXNAMELEN
)
2655 return (SET_ERROR(ENAMETOOLONG
));
2657 if (strlen(fnvpair_value_string(pair
)) >= ZAP_MAXVALUELEN
)
2658 return (SET_ERROR(E2BIG
));
2664 props_skip(nvlist_t
*props
, nvlist_t
*skipped
, nvlist_t
**newprops
)
2668 VERIFY(nvlist_alloc(newprops
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2671 while ((pair
= nvlist_next_nvpair(props
, pair
)) != NULL
) {
2672 if (nvlist_exists(skipped
, nvpair_name(pair
)))
2675 VERIFY(nvlist_add_nvpair(*newprops
, pair
) == 0);
2680 clear_received_props(const char *dsname
, nvlist_t
*props
,
2684 nvlist_t
*cleared_props
= NULL
;
2685 props_skip(props
, skipped
, &cleared_props
);
2686 if (!nvlist_empty(cleared_props
)) {
2688 * Acts on local properties until the dataset has received
2689 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2691 zprop_source_t flags
= (ZPROP_SRC_NONE
|
2692 (dsl_prop_get_hasrecvd(dsname
) ? ZPROP_SRC_RECEIVED
: 0));
2693 err
= zfs_set_prop_nvlist(dsname
, flags
, cleared_props
, NULL
);
2695 nvlist_free(cleared_props
);
2701 * zc_name name of filesystem
2702 * zc_value name of property to set
2703 * zc_nvlist_src{_size} nvlist of properties to apply
2704 * zc_cookie received properties flag
2707 * zc_nvlist_dst{_size} error for each unapplied received property
2710 zfs_ioc_set_prop(zfs_cmd_t
*zc
)
2713 boolean_t received
= zc
->zc_cookie
;
2714 zprop_source_t source
= (received
? ZPROP_SRC_RECEIVED
:
2719 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2720 zc
->zc_iflags
, &nvl
)) != 0)
2724 nvlist_t
*origprops
;
2726 if (dsl_prop_get_received(zc
->zc_name
, &origprops
) == 0) {
2727 (void) clear_received_props(zc
->zc_name
,
2729 nvlist_free(origprops
);
2732 error
= dsl_prop_set_hasrecvd(zc
->zc_name
);
2735 errors
= fnvlist_alloc();
2737 error
= zfs_set_prop_nvlist(zc
->zc_name
, source
, nvl
, errors
);
2739 if (zc
->zc_nvlist_dst
!= 0 && errors
!= NULL
) {
2740 (void) put_nvlist(zc
, errors
);
2743 nvlist_free(errors
);
2750 * zc_name name of filesystem
2751 * zc_value name of property to inherit
2752 * zc_cookie revert to received value if TRUE
2757 zfs_ioc_inherit_prop(zfs_cmd_t
*zc
)
2759 const char *propname
= zc
->zc_value
;
2760 zfs_prop_t prop
= zfs_name_to_prop(propname
);
2761 boolean_t received
= zc
->zc_cookie
;
2762 zprop_source_t source
= (received
2763 ? ZPROP_SRC_NONE
/* revert to received value, if any */
2764 : ZPROP_SRC_INHERITED
); /* explicitly inherit */
2773 * zfs_prop_set_special() expects properties in the form of an
2774 * nvpair with type info.
2776 if (prop
== ZPROP_INVAL
) {
2777 if (!zfs_prop_user(propname
))
2778 return (SET_ERROR(EINVAL
));
2780 type
= PROP_TYPE_STRING
;
2781 } else if (prop
== ZFS_PROP_VOLSIZE
||
2782 prop
== ZFS_PROP_VERSION
) {
2783 return (SET_ERROR(EINVAL
));
2785 type
= zfs_prop_get_type(prop
);
2788 VERIFY(nvlist_alloc(&dummy
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2791 case PROP_TYPE_STRING
:
2792 VERIFY(0 == nvlist_add_string(dummy
, propname
, ""));
2794 case PROP_TYPE_NUMBER
:
2795 case PROP_TYPE_INDEX
:
2796 VERIFY(0 == nvlist_add_uint64(dummy
, propname
, 0));
2800 return (SET_ERROR(EINVAL
));
2803 pair
= nvlist_next_nvpair(dummy
, NULL
);
2804 err
= zfs_prop_set_special(zc
->zc_name
, source
, pair
);
2807 return (err
); /* special property already handled */
2810 * Only check this in the non-received case. We want to allow
2811 * 'inherit -S' to revert non-inheritable properties like quota
2812 * and reservation to the received or default values even though
2813 * they are not considered inheritable.
2815 if (prop
!= ZPROP_INVAL
&& !zfs_prop_inheritable(prop
))
2816 return (SET_ERROR(EINVAL
));
2819 /* property name has been validated by zfs_secpolicy_inherit_prop() */
2820 return (dsl_prop_inherit(zc
->zc_name
, zc
->zc_value
, source
));
2824 zfs_ioc_pool_set_props(zfs_cmd_t
*zc
)
2831 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2832 zc
->zc_iflags
, &props
)))
2836 * If the only property is the configfile, then just do a spa_lookup()
2837 * to handle the faulted case.
2839 pair
= nvlist_next_nvpair(props
, NULL
);
2840 if (pair
!= NULL
&& strcmp(nvpair_name(pair
),
2841 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
)) == 0 &&
2842 nvlist_next_nvpair(props
, pair
) == NULL
) {
2843 mutex_enter(&spa_namespace_lock
);
2844 if ((spa
= spa_lookup(zc
->zc_name
)) != NULL
) {
2845 spa_configfile_set(spa
, props
, B_FALSE
);
2846 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
2848 mutex_exit(&spa_namespace_lock
);
2855 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0) {
2860 error
= spa_prop_set(spa
, props
);
2863 spa_close(spa
, FTAG
);
2869 zfs_ioc_pool_get_props(zfs_cmd_t
*zc
)
2873 nvlist_t
*nvp
= NULL
;
2875 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0) {
2877 * If the pool is faulted, there may be properties we can still
2878 * get (such as altroot and cachefile), so attempt to get them
2881 mutex_enter(&spa_namespace_lock
);
2882 if ((spa
= spa_lookup(zc
->zc_name
)) != NULL
)
2883 error
= spa_prop_get(spa
, &nvp
);
2884 mutex_exit(&spa_namespace_lock
);
2886 error
= spa_prop_get(spa
, &nvp
);
2887 spa_close(spa
, FTAG
);
2890 if (error
== 0 && zc
->zc_nvlist_dst
!= 0)
2891 error
= put_nvlist(zc
, nvp
);
2893 error
= SET_ERROR(EFAULT
);
2901 * zc_name name of filesystem
2902 * zc_nvlist_src{_size} nvlist of delegated permissions
2903 * zc_perm_action allow/unallow flag
2908 zfs_ioc_set_fsacl(zfs_cmd_t
*zc
)
2911 nvlist_t
*fsaclnv
= NULL
;
2913 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2914 zc
->zc_iflags
, &fsaclnv
)) != 0)
2918 * Verify nvlist is constructed correctly
2920 if ((error
= zfs_deleg_verify_nvlist(fsaclnv
)) != 0) {
2921 nvlist_free(fsaclnv
);
2922 return (SET_ERROR(EINVAL
));
2926 * If we don't have PRIV_SYS_MOUNT, then validate
2927 * that user is allowed to hand out each permission in
2931 error
= secpolicy_zfs(CRED());
2933 if (zc
->zc_perm_action
== B_FALSE
) {
2934 error
= dsl_deleg_can_allow(zc
->zc_name
,
2937 error
= dsl_deleg_can_unallow(zc
->zc_name
,
2943 error
= dsl_deleg_set(zc
->zc_name
, fsaclnv
, zc
->zc_perm_action
);
2945 nvlist_free(fsaclnv
);
2951 * zc_name name of filesystem
2954 * zc_nvlist_src{_size} nvlist of delegated permissions
2957 zfs_ioc_get_fsacl(zfs_cmd_t
*zc
)
2962 if ((error
= dsl_deleg_get(zc
->zc_name
, &nvp
)) == 0) {
2963 error
= put_nvlist(zc
, nvp
);
2972 zfs_create_cb(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
)
2974 zfs_creat_t
*zct
= arg
;
2976 zfs_create_fs(os
, cr
, zct
->zct_zplprops
, tx
);
2979 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
2983 * os parent objset pointer (NULL if root fs)
2984 * fuids_ok fuids allowed in this version of the spa?
2985 * sa_ok SAs allowed in this version of the spa?
2986 * createprops list of properties requested by creator
2989 * zplprops values for the zplprops we attach to the master node object
2990 * is_ci true if requested file system will be purely case-insensitive
2992 * Determine the settings for utf8only, normalization and
2993 * casesensitivity. Specific values may have been requested by the
2994 * creator and/or we can inherit values from the parent dataset. If
2995 * the file system is of too early a vintage, a creator can not
2996 * request settings for these properties, even if the requested
2997 * setting is the default value. We don't actually want to create dsl
2998 * properties for these, so remove them from the source nvlist after
3002 zfs_fill_zplprops_impl(objset_t
*os
, uint64_t zplver
,
3003 boolean_t fuids_ok
, boolean_t sa_ok
, nvlist_t
*createprops
,
3004 nvlist_t
*zplprops
, boolean_t
*is_ci
)
3006 uint64_t sense
= ZFS_PROP_UNDEFINED
;
3007 uint64_t norm
= ZFS_PROP_UNDEFINED
;
3008 uint64_t u8
= ZFS_PROP_UNDEFINED
;
3011 ASSERT(zplprops
!= NULL
);
3014 * Pull out creator prop choices, if any.
3017 (void) nvlist_lookup_uint64(createprops
,
3018 zfs_prop_to_name(ZFS_PROP_VERSION
), &zplver
);
3019 (void) nvlist_lookup_uint64(createprops
,
3020 zfs_prop_to_name(ZFS_PROP_NORMALIZE
), &norm
);
3021 (void) nvlist_remove_all(createprops
,
3022 zfs_prop_to_name(ZFS_PROP_NORMALIZE
));
3023 (void) nvlist_lookup_uint64(createprops
,
3024 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), &u8
);
3025 (void) nvlist_remove_all(createprops
,
3026 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
));
3027 (void) nvlist_lookup_uint64(createprops
,
3028 zfs_prop_to_name(ZFS_PROP_CASE
), &sense
);
3029 (void) nvlist_remove_all(createprops
,
3030 zfs_prop_to_name(ZFS_PROP_CASE
));
3034 * If the zpl version requested is whacky or the file system
3035 * or pool is version is too "young" to support normalization
3036 * and the creator tried to set a value for one of the props,
3039 if ((zplver
< ZPL_VERSION_INITIAL
|| zplver
> ZPL_VERSION
) ||
3040 (zplver
>= ZPL_VERSION_FUID
&& !fuids_ok
) ||
3041 (zplver
>= ZPL_VERSION_SA
&& !sa_ok
) ||
3042 (zplver
< ZPL_VERSION_NORMALIZATION
&&
3043 (norm
!= ZFS_PROP_UNDEFINED
|| u8
!= ZFS_PROP_UNDEFINED
||
3044 sense
!= ZFS_PROP_UNDEFINED
)))
3045 return (SET_ERROR(ENOTSUP
));
3048 * Put the version in the zplprops
3050 VERIFY(nvlist_add_uint64(zplprops
,
3051 zfs_prop_to_name(ZFS_PROP_VERSION
), zplver
) == 0);
3053 if (norm
== ZFS_PROP_UNDEFINED
&&
3054 (error
= zfs_get_zplprop(os
, ZFS_PROP_NORMALIZE
, &norm
)) != 0)
3056 VERIFY(nvlist_add_uint64(zplprops
,
3057 zfs_prop_to_name(ZFS_PROP_NORMALIZE
), norm
) == 0);
3060 * If we're normalizing, names must always be valid UTF-8 strings.
3064 if (u8
== ZFS_PROP_UNDEFINED
&&
3065 (error
= zfs_get_zplprop(os
, ZFS_PROP_UTF8ONLY
, &u8
)) != 0)
3067 VERIFY(nvlist_add_uint64(zplprops
,
3068 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), u8
) == 0);
3070 if (sense
== ZFS_PROP_UNDEFINED
&&
3071 (error
= zfs_get_zplprop(os
, ZFS_PROP_CASE
, &sense
)) != 0)
3073 VERIFY(nvlist_add_uint64(zplprops
,
3074 zfs_prop_to_name(ZFS_PROP_CASE
), sense
) == 0);
3077 *is_ci
= (sense
== ZFS_CASE_INSENSITIVE
);
3083 zfs_fill_zplprops(const char *dataset
, nvlist_t
*createprops
,
3084 nvlist_t
*zplprops
, boolean_t
*is_ci
)
3086 boolean_t fuids_ok
, sa_ok
;
3087 uint64_t zplver
= ZPL_VERSION
;
3088 objset_t
*os
= NULL
;
3089 char parentname
[MAXNAMELEN
];
3095 (void) strlcpy(parentname
, dataset
, sizeof (parentname
));
3096 cp
= strrchr(parentname
, '/');
3100 if ((error
= spa_open(dataset
, &spa
, FTAG
)) != 0)
3103 spa_vers
= spa_version(spa
);
3104 spa_close(spa
, FTAG
);
3106 zplver
= zfs_zpl_version_map(spa_vers
);
3107 fuids_ok
= (zplver
>= ZPL_VERSION_FUID
);
3108 sa_ok
= (zplver
>= ZPL_VERSION_SA
);
3111 * Open parent object set so we can inherit zplprop values.
3113 if ((error
= dmu_objset_hold(parentname
, FTAG
, &os
)) != 0)
3116 error
= zfs_fill_zplprops_impl(os
, zplver
, fuids_ok
, sa_ok
, createprops
,
3118 dmu_objset_rele(os
, FTAG
);
3123 zfs_fill_zplprops_root(uint64_t spa_vers
, nvlist_t
*createprops
,
3124 nvlist_t
*zplprops
, boolean_t
*is_ci
)
3128 uint64_t zplver
= ZPL_VERSION
;
3131 zplver
= zfs_zpl_version_map(spa_vers
);
3132 fuids_ok
= (zplver
>= ZPL_VERSION_FUID
);
3133 sa_ok
= (zplver
>= ZPL_VERSION_SA
);
3135 error
= zfs_fill_zplprops_impl(NULL
, zplver
, fuids_ok
, sa_ok
,
3136 createprops
, zplprops
, is_ci
);
3142 * "type" -> dmu_objset_type_t (int32)
3143 * (optional) "props" -> { prop -> value }
3146 * outnvl: propname -> error code (int32)
3149 zfs_ioc_create(const char *fsname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3152 zfs_creat_t zct
= { 0 };
3153 nvlist_t
*nvprops
= NULL
;
3154 void (*cbfunc
)(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
);
3156 dmu_objset_type_t type
;
3157 boolean_t is_insensitive
= B_FALSE
;
3159 if (nvlist_lookup_int32(innvl
, "type", &type32
) != 0)
3160 return (SET_ERROR(EINVAL
));
3162 (void) nvlist_lookup_nvlist(innvl
, "props", &nvprops
);
3166 cbfunc
= zfs_create_cb
;
3170 cbfunc
= zvol_create_cb
;
3177 if (strchr(fsname
, '@') ||
3178 strchr(fsname
, '%'))
3179 return (SET_ERROR(EINVAL
));
3181 zct
.zct_props
= nvprops
;
3184 return (SET_ERROR(EINVAL
));
3186 if (type
== DMU_OST_ZVOL
) {
3187 uint64_t volsize
, volblocksize
;
3189 if (nvprops
== NULL
)
3190 return (SET_ERROR(EINVAL
));
3191 if (nvlist_lookup_uint64(nvprops
,
3192 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &volsize
) != 0)
3193 return (SET_ERROR(EINVAL
));
3195 if ((error
= nvlist_lookup_uint64(nvprops
,
3196 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
3197 &volblocksize
)) != 0 && error
!= ENOENT
)
3198 return (SET_ERROR(EINVAL
));
3201 volblocksize
= zfs_prop_default_numeric(
3202 ZFS_PROP_VOLBLOCKSIZE
);
3204 if ((error
= zvol_check_volblocksize(
3205 volblocksize
)) != 0 ||
3206 (error
= zvol_check_volsize(volsize
,
3207 volblocksize
)) != 0)
3209 } else if (type
== DMU_OST_ZFS
) {
3213 * We have to have normalization and
3214 * case-folding flags correct when we do the
3215 * file system creation, so go figure them out
3218 VERIFY(nvlist_alloc(&zct
.zct_zplprops
,
3219 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3220 error
= zfs_fill_zplprops(fsname
, nvprops
,
3221 zct
.zct_zplprops
, &is_insensitive
);
3223 nvlist_free(zct
.zct_zplprops
);
3228 error
= dmu_objset_create(fsname
, type
,
3229 is_insensitive
? DS_FLAG_CI_DATASET
: 0, cbfunc
, &zct
);
3230 nvlist_free(zct
.zct_zplprops
);
3233 * It would be nice to do this atomically.
3236 error
= zfs_set_prop_nvlist(fsname
, ZPROP_SRC_LOCAL
,
3239 (void) dsl_destroy_head(fsname
);
3243 if (error
== 0 && type
== DMU_OST_ZVOL
)
3244 zvol_create_minors(fsname
);
3252 * "origin" -> name of origin snapshot
3253 * (optional) "props" -> { prop -> value }
3257 * outnvl: propname -> error code (int32)
3260 zfs_ioc_clone(const char *fsname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3263 nvlist_t
*nvprops
= NULL
;
3266 if (nvlist_lookup_string(innvl
, "origin", &origin_name
) != 0)
3267 return (SET_ERROR(EINVAL
));
3268 (void) nvlist_lookup_nvlist(innvl
, "props", &nvprops
);
3270 if (strchr(fsname
, '@') ||
3271 strchr(fsname
, '%'))
3272 return (SET_ERROR(EINVAL
));
3274 if (dataset_namecheck(origin_name
, NULL
, NULL
) != 0)
3275 return (SET_ERROR(EINVAL
));
3276 error
= dmu_objset_clone(fsname
, origin_name
);
3281 * It would be nice to do this atomically.
3284 error
= zfs_set_prop_nvlist(fsname
, ZPROP_SRC_LOCAL
,
3287 (void) dsl_destroy_head(fsname
);
3292 zvol_create_minors(fsname
);
3300 * "snaps" -> { snapshot1, snapshot2 }
3301 * (optional) "props" -> { prop -> value (string) }
3304 * outnvl: snapshot -> error code (int32)
3307 zfs_ioc_snapshot(const char *poolname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3310 nvlist_t
*props
= NULL
;
3312 nvpair_t
*pair
, *pair2
;
3314 (void) nvlist_lookup_nvlist(innvl
, "props", &props
);
3315 if ((error
= zfs_check_userprops(poolname
, props
)) != 0)
3318 if (!nvlist_empty(props
) &&
3319 zfs_earlier_version(poolname
, SPA_VERSION_SNAP_PROPS
))
3320 return (SET_ERROR(ENOTSUP
));
3322 if (nvlist_lookup_nvlist(innvl
, "snaps", &snaps
) != 0)
3323 return (SET_ERROR(EINVAL
));
3324 poollen
= strlen(poolname
);
3325 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
3326 pair
= nvlist_next_nvpair(snaps
, pair
)) {
3327 const char *name
= nvpair_name(pair
);
3328 const char *cp
= strchr(name
, '@');
3331 * The snap name must contain an @, and the part after it must
3332 * contain only valid characters.
3335 zfs_component_namecheck(cp
+ 1, NULL
, NULL
) != 0)
3336 return (SET_ERROR(EINVAL
));
3339 * The snap must be in the specified pool.
3341 if (strncmp(name
, poolname
, poollen
) != 0 ||
3342 (name
[poollen
] != '/' && name
[poollen
] != '@'))
3343 return (SET_ERROR(EXDEV
));
3345 /* This must be the only snap of this fs. */
3346 for (pair2
= nvlist_next_nvpair(snaps
, pair
);
3347 pair2
!= NULL
; pair2
= nvlist_next_nvpair(snaps
, pair2
)) {
3348 if (strncmp(name
, nvpair_name(pair2
), cp
- name
+ 1)
3350 return (SET_ERROR(EXDEV
));
3355 error
= dsl_dataset_snapshot(snaps
, props
, outnvl
);
3359 zvol_create_minors(poolname
);
3366 * innvl: "message" -> string
3370 zfs_ioc_log_history(const char *unused
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3378 * The poolname in the ioctl is not set, we get it from the TSD,
3379 * which was set at the end of the last successful ioctl that allows
3380 * logging. The secpolicy func already checked that it is set.
3381 * Only one log ioctl is allowed after each successful ioctl, so
3382 * we clear the TSD here.
3384 poolname
= tsd_get(zfs_allow_log_key
);
3385 (void) tsd_set(zfs_allow_log_key
, NULL
);
3386 error
= spa_open(poolname
, &spa
, FTAG
);
3391 if (nvlist_lookup_string(innvl
, "message", &message
) != 0) {
3392 spa_close(spa
, FTAG
);
3393 return (SET_ERROR(EINVAL
));
3396 if (spa_version(spa
) < SPA_VERSION_ZPOOL_HISTORY
) {
3397 spa_close(spa
, FTAG
);
3398 return (SET_ERROR(ENOTSUP
));
3401 error
= spa_history_log(spa
, message
);
3402 spa_close(spa
, FTAG
);
3407 * The dp_config_rwlock must not be held when calling this, because the
3408 * unmount may need to write out data.
3410 * This function is best-effort. Callers must deal gracefully if it
3411 * remains mounted (or is remounted after this call).
3413 * XXX: This function should detect a failure to unmount a snapdir of a dataset
3414 * and return the appropriate error code when it is mounted. Its Illumos and
3415 * FreeBSD counterparts do this. We do not do this on Linux because there is no
3416 * clear way to access the mount information that FreeBSD and Illumos use to
3417 * distinguish between things with mounted snapshot directories, and things
3418 * without mounted snapshot directories, which include zvols. Returning a
3419 * failure for the latter causes `zfs destroy` to fail on zvol snapshots.
3422 zfs_unmount_snap(const char *snapname
)
3424 zfs_sb_t
*zsb
= NULL
;
3429 if ((ptr
= strchr(snapname
, '@')) == NULL
)
3432 dsname
= kmem_alloc(ptr
- snapname
+ 1, KM_SLEEP
);
3433 strlcpy(dsname
, snapname
, ptr
- snapname
+ 1);
3434 fullname
= strdup(snapname
);
3436 if (zfs_sb_hold(dsname
, FTAG
, &zsb
, B_FALSE
) == 0) {
3437 ASSERT(!dsl_pool_config_held(dmu_objset_pool(zsb
->z_os
)));
3438 (void) zfsctl_unmount_snapshot(zsb
, fullname
, MNT_FORCE
);
3439 zfs_sb_rele(zsb
, FTAG
);
3442 kmem_free(dsname
, ptr
- snapname
+ 1);
3450 zfs_unmount_snap_cb(const char *snapname
, void *arg
)
3452 return (zfs_unmount_snap(snapname
));
3456 * When a clone is destroyed, its origin may also need to be destroyed,
3457 * in which case it must be unmounted. This routine will do that unmount
3461 zfs_destroy_unmount_origin(const char *fsname
)
3467 error
= dmu_objset_hold(fsname
, FTAG
, &os
);
3470 ds
= dmu_objset_ds(os
);
3471 if (dsl_dir_is_clone(ds
->ds_dir
) && DS_IS_DEFER_DESTROY(ds
->ds_prev
)) {
3472 char originname
[MAXNAMELEN
];
3473 dsl_dataset_name(ds
->ds_prev
, originname
);
3474 dmu_objset_rele(os
, FTAG
);
3475 (void) zfs_unmount_snap(originname
);
3477 dmu_objset_rele(os
, FTAG
);
3483 * "snaps" -> { snapshot1, snapshot2 }
3484 * (optional boolean) "defer"
3487 * outnvl: snapshot -> error code (int32)
3491 zfs_ioc_destroy_snaps(const char *poolname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3497 if (nvlist_lookup_nvlist(innvl
, "snaps", &snaps
) != 0)
3498 return (SET_ERROR(EINVAL
));
3499 defer
= nvlist_exists(innvl
, "defer");
3501 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
3502 pair
= nvlist_next_nvpair(snaps
, pair
)) {
3503 (void) zfs_unmount_snap(nvpair_name(pair
));
3504 (void) zvol_remove_minor(nvpair_name(pair
));
3507 return (dsl_destroy_snapshots_nvl(snaps
, defer
, outnvl
));
3511 * Create bookmarks. Bookmark names are of the form <fs>#<bmark>.
3512 * All bookmarks must be in the same pool.
3515 * bookmark1 -> snapshot1, bookmark2 -> snapshot2
3518 * outnvl: bookmark -> error code (int32)
3523 zfs_ioc_bookmark(const char *poolname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3525 nvpair_t
*pair
, *pair2
;
3527 for (pair
= nvlist_next_nvpair(innvl
, NULL
);
3528 pair
!= NULL
; pair
= nvlist_next_nvpair(innvl
, pair
)) {
3532 * Verify the snapshot argument.
3534 if (nvpair_value_string(pair
, &snap_name
) != 0)
3535 return (SET_ERROR(EINVAL
));
3538 /* Verify that the keys (bookmarks) are unique */
3539 for (pair2
= nvlist_next_nvpair(innvl
, pair
);
3540 pair2
!= NULL
; pair2
= nvlist_next_nvpair(innvl
, pair2
)) {
3541 if (strcmp(nvpair_name(pair
), nvpair_name(pair2
)) == 0)
3542 return (SET_ERROR(EINVAL
));
3546 return (dsl_bookmark_create(innvl
, outnvl
));
3551 * property 1, property 2, ...
3555 * bookmark name 1 -> { property 1, property 2, ... },
3556 * bookmark name 2 -> { property 1, property 2, ... }
3561 zfs_ioc_get_bookmarks(const char *fsname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3563 return (dsl_get_bookmarks(fsname
, innvl
, outnvl
));
3568 * bookmark name 1, bookmark name 2
3571 * outnvl: bookmark -> error code (int32)
3575 zfs_ioc_destroy_bookmarks(const char *poolname
, nvlist_t
*innvl
,
3581 poollen
= strlen(poolname
);
3582 for (pair
= nvlist_next_nvpair(innvl
, NULL
);
3583 pair
!= NULL
; pair
= nvlist_next_nvpair(innvl
, pair
)) {
3584 const char *name
= nvpair_name(pair
);
3585 const char *cp
= strchr(name
, '#');
3588 * The bookmark name must contain an #, and the part after it
3589 * must contain only valid characters.
3592 zfs_component_namecheck(cp
+ 1, NULL
, NULL
) != 0)
3593 return (SET_ERROR(EINVAL
));
3596 * The bookmark must be in the specified pool.
3598 if (strncmp(name
, poolname
, poollen
) != 0 ||
3599 (name
[poollen
] != '/' && name
[poollen
] != '#'))
3600 return (SET_ERROR(EXDEV
));
3603 error
= dsl_bookmark_destroy(innvl
, outnvl
);
3609 * zc_name name of dataset to destroy
3610 * zc_objset_type type of objset
3611 * zc_defer_destroy mark for deferred destroy
3616 zfs_ioc_destroy(zfs_cmd_t
*zc
)
3620 if (zc
->zc_objset_type
== DMU_OST_ZFS
) {
3621 err
= zfs_unmount_snap(zc
->zc_name
);
3626 if (strchr(zc
->zc_name
, '@'))
3627 err
= dsl_destroy_snapshot(zc
->zc_name
, zc
->zc_defer_destroy
);
3629 err
= dsl_destroy_head(zc
->zc_name
);
3630 if (zc
->zc_objset_type
== DMU_OST_ZVOL
&& err
== 0)
3631 (void) zvol_remove_minor(zc
->zc_name
);
3636 * fsname is name of dataset to rollback (to most recent snapshot)
3638 * innvl is not used.
3640 * outnvl: "target" -> name of most recent snapshot
3645 zfs_ioc_rollback(const char *fsname
, nvlist_t
*args
, nvlist_t
*outnvl
)
3650 if (get_zfs_sb(fsname
, &zsb
) == 0) {
3651 error
= zfs_suspend_fs(zsb
);
3655 error
= dsl_dataset_rollback(fsname
, zsb
, outnvl
);
3656 resume_err
= zfs_resume_fs(zsb
, fsname
);
3657 error
= error
? error
: resume_err
;
3659 deactivate_super(zsb
->z_sb
);
3661 error
= dsl_dataset_rollback(fsname
, NULL
, outnvl
);
3667 recursive_unmount(const char *fsname
, void *arg
)
3669 const char *snapname
= arg
;
3673 fullname
= kmem_asprintf("%s@%s", fsname
, snapname
);
3674 error
= zfs_unmount_snap(fullname
);
3682 * zc_name old name of dataset
3683 * zc_value new name of dataset
3684 * zc_cookie recursive flag (only valid for snapshots)
3689 zfs_ioc_rename(zfs_cmd_t
*zc
)
3691 boolean_t recursive
= zc
->zc_cookie
& 1;
3694 zc
->zc_value
[sizeof (zc
->zc_value
) - 1] = '\0';
3695 if (dataset_namecheck(zc
->zc_value
, NULL
, NULL
) != 0 ||
3696 strchr(zc
->zc_value
, '%'))
3697 return (SET_ERROR(EINVAL
));
3699 at
= strchr(zc
->zc_name
, '@');
3701 /* snaps must be in same fs */
3704 if (strncmp(zc
->zc_name
, zc
->zc_value
, at
- zc
->zc_name
+ 1))
3705 return (SET_ERROR(EXDEV
));
3707 if (zc
->zc_objset_type
== DMU_OST_ZFS
) {
3708 error
= dmu_objset_find(zc
->zc_name
,
3709 recursive_unmount
, at
+ 1,
3710 recursive
? DS_FIND_CHILDREN
: 0);
3716 error
= dsl_dataset_rename_snapshot(zc
->zc_name
,
3717 at
+ 1, strchr(zc
->zc_value
, '@') + 1, recursive
);
3722 return (dsl_dir_rename(zc
->zc_name
, zc
->zc_value
));
3727 zfs_check_settable(const char *dsname
, nvpair_t
*pair
, cred_t
*cr
)
3729 const char *propname
= nvpair_name(pair
);
3730 boolean_t issnap
= (strchr(dsname
, '@') != NULL
);
3731 zfs_prop_t prop
= zfs_name_to_prop(propname
);
3735 if (prop
== ZPROP_INVAL
) {
3736 if (zfs_prop_user(propname
)) {
3737 if ((err
= zfs_secpolicy_write_perms(dsname
,
3738 ZFS_DELEG_PERM_USERPROP
, cr
)))
3743 if (!issnap
&& zfs_prop_userquota(propname
)) {
3744 const char *perm
= NULL
;
3745 const char *uq_prefix
=
3746 zfs_userquota_prop_prefixes
[ZFS_PROP_USERQUOTA
];
3747 const char *gq_prefix
=
3748 zfs_userquota_prop_prefixes
[ZFS_PROP_GROUPQUOTA
];
3750 if (strncmp(propname
, uq_prefix
,
3751 strlen(uq_prefix
)) == 0) {
3752 perm
= ZFS_DELEG_PERM_USERQUOTA
;
3753 } else if (strncmp(propname
, gq_prefix
,
3754 strlen(gq_prefix
)) == 0) {
3755 perm
= ZFS_DELEG_PERM_GROUPQUOTA
;
3757 /* USERUSED and GROUPUSED are read-only */
3758 return (SET_ERROR(EINVAL
));
3761 if ((err
= zfs_secpolicy_write_perms(dsname
, perm
, cr
)))
3766 return (SET_ERROR(EINVAL
));
3770 return (SET_ERROR(EINVAL
));
3772 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
3774 * dsl_prop_get_all_impl() returns properties in this
3778 VERIFY(nvpair_value_nvlist(pair
, &attrs
) == 0);
3779 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
3784 * Check that this value is valid for this pool version
3787 case ZFS_PROP_COMPRESSION
:
3789 * If the user specified gzip compression, make sure
3790 * the SPA supports it. We ignore any errors here since
3791 * we'll catch them later.
3793 if (nvpair_type(pair
) == DATA_TYPE_UINT64
&&
3794 nvpair_value_uint64(pair
, &intval
) == 0) {
3795 if (intval
>= ZIO_COMPRESS_GZIP_1
&&
3796 intval
<= ZIO_COMPRESS_GZIP_9
&&
3797 zfs_earlier_version(dsname
,
3798 SPA_VERSION_GZIP_COMPRESSION
)) {
3799 return (SET_ERROR(ENOTSUP
));
3802 if (intval
== ZIO_COMPRESS_ZLE
&&
3803 zfs_earlier_version(dsname
,
3804 SPA_VERSION_ZLE_COMPRESSION
))
3805 return (SET_ERROR(ENOTSUP
));
3807 if (intval
== ZIO_COMPRESS_LZ4
) {
3810 if ((err
= spa_open(dsname
, &spa
, FTAG
)) != 0)
3813 if (!spa_feature_is_enabled(spa
,
3814 SPA_FEATURE_LZ4_COMPRESS
)) {
3815 spa_close(spa
, FTAG
);
3816 return (SET_ERROR(ENOTSUP
));
3818 spa_close(spa
, FTAG
);
3822 * If this is a bootable dataset then
3823 * verify that the compression algorithm
3824 * is supported for booting. We must return
3825 * something other than ENOTSUP since it
3826 * implies a downrev pool version.
3828 if (zfs_is_bootfs(dsname
) &&
3829 !BOOTFS_COMPRESS_VALID(intval
)) {
3830 return (SET_ERROR(ERANGE
));
3835 case ZFS_PROP_COPIES
:
3836 if (zfs_earlier_version(dsname
, SPA_VERSION_DITTO_BLOCKS
))
3837 return (SET_ERROR(ENOTSUP
));
3840 case ZFS_PROP_DEDUP
:
3841 if (zfs_earlier_version(dsname
, SPA_VERSION_DEDUP
))
3842 return (SET_ERROR(ENOTSUP
));
3845 case ZFS_PROP_SHARESMB
:
3846 if (zpl_earlier_version(dsname
, ZPL_VERSION_FUID
))
3847 return (SET_ERROR(ENOTSUP
));
3850 case ZFS_PROP_ACLINHERIT
:
3851 if (nvpair_type(pair
) == DATA_TYPE_UINT64
&&
3852 nvpair_value_uint64(pair
, &intval
) == 0) {
3853 if (intval
== ZFS_ACL_PASSTHROUGH_X
&&
3854 zfs_earlier_version(dsname
,
3855 SPA_VERSION_PASSTHROUGH_X
))
3856 return (SET_ERROR(ENOTSUP
));
3863 return (zfs_secpolicy_setprop(dsname
, prop
, pair
, CRED()));
3867 * Removes properties from the given props list that fail permission checks
3868 * needed to clear them and to restore them in case of a receive error. For each
3869 * property, make sure we have both set and inherit permissions.
3871 * Returns the first error encountered if any permission checks fail. If the
3872 * caller provides a non-NULL errlist, it also gives the complete list of names
3873 * of all the properties that failed a permission check along with the
3874 * corresponding error numbers. The caller is responsible for freeing the
3877 * If every property checks out successfully, zero is returned and the list
3878 * pointed at by errlist is NULL.
3881 zfs_check_clearable(char *dataset
, nvlist_t
*props
, nvlist_t
**errlist
)
3884 nvpair_t
*pair
, *next_pair
;
3891 VERIFY(nvlist_alloc(&errors
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3893 zc
= kmem_alloc(sizeof (zfs_cmd_t
), KM_SLEEP
);
3894 (void) strcpy(zc
->zc_name
, dataset
);
3895 pair
= nvlist_next_nvpair(props
, NULL
);
3896 while (pair
!= NULL
) {
3897 next_pair
= nvlist_next_nvpair(props
, pair
);
3899 (void) strcpy(zc
->zc_value
, nvpair_name(pair
));
3900 if ((err
= zfs_check_settable(dataset
, pair
, CRED())) != 0 ||
3901 (err
= zfs_secpolicy_inherit_prop(zc
, NULL
, CRED())) != 0) {
3902 VERIFY(nvlist_remove_nvpair(props
, pair
) == 0);
3903 VERIFY(nvlist_add_int32(errors
,
3904 zc
->zc_value
, err
) == 0);
3908 kmem_free(zc
, sizeof (zfs_cmd_t
));
3910 if ((pair
= nvlist_next_nvpair(errors
, NULL
)) == NULL
) {
3911 nvlist_free(errors
);
3914 VERIFY(nvpair_value_int32(pair
, &rv
) == 0);
3917 if (errlist
== NULL
)
3918 nvlist_free(errors
);
3926 propval_equals(nvpair_t
*p1
, nvpair_t
*p2
)
3928 if (nvpair_type(p1
) == DATA_TYPE_NVLIST
) {
3929 /* dsl_prop_get_all_impl() format */
3931 VERIFY(nvpair_value_nvlist(p1
, &attrs
) == 0);
3932 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
3936 if (nvpair_type(p2
) == DATA_TYPE_NVLIST
) {
3938 VERIFY(nvpair_value_nvlist(p2
, &attrs
) == 0);
3939 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
3943 if (nvpair_type(p1
) != nvpair_type(p2
))
3946 if (nvpair_type(p1
) == DATA_TYPE_STRING
) {
3947 char *valstr1
, *valstr2
;
3949 VERIFY(nvpair_value_string(p1
, (char **)&valstr1
) == 0);
3950 VERIFY(nvpair_value_string(p2
, (char **)&valstr2
) == 0);
3951 return (strcmp(valstr1
, valstr2
) == 0);
3953 uint64_t intval1
, intval2
;
3955 VERIFY(nvpair_value_uint64(p1
, &intval1
) == 0);
3956 VERIFY(nvpair_value_uint64(p2
, &intval2
) == 0);
3957 return (intval1
== intval2
);
3962 * Remove properties from props if they are not going to change (as determined
3963 * by comparison with origprops). Remove them from origprops as well, since we
3964 * do not need to clear or restore properties that won't change.
3967 props_reduce(nvlist_t
*props
, nvlist_t
*origprops
)
3969 nvpair_t
*pair
, *next_pair
;
3971 if (origprops
== NULL
)
3972 return; /* all props need to be received */
3974 pair
= nvlist_next_nvpair(props
, NULL
);
3975 while (pair
!= NULL
) {
3976 const char *propname
= nvpair_name(pair
);
3979 next_pair
= nvlist_next_nvpair(props
, pair
);
3981 if ((nvlist_lookup_nvpair(origprops
, propname
,
3982 &match
) != 0) || !propval_equals(pair
, match
))
3983 goto next
; /* need to set received value */
3985 /* don't clear the existing received value */
3986 (void) nvlist_remove_nvpair(origprops
, match
);
3987 /* don't bother receiving the property */
3988 (void) nvlist_remove_nvpair(props
, pair
);
3995 static boolean_t zfs_ioc_recv_inject_err
;
4000 * zc_name name of containing filesystem
4001 * zc_nvlist_src{_size} nvlist of properties to apply
4002 * zc_value name of snapshot to create
4003 * zc_string name of clone origin (if DRR_FLAG_CLONE)
4004 * zc_cookie file descriptor to recv from
4005 * zc_begin_record the BEGIN record of the stream (not byteswapped)
4006 * zc_guid force flag
4007 * zc_cleanup_fd cleanup-on-exit file descriptor
4008 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
4011 * zc_cookie number of bytes read
4012 * zc_nvlist_dst{_size} error for each unapplied received property
4013 * zc_obj zprop_errflags_t
4014 * zc_action_handle handle for this guid/ds mapping
4017 zfs_ioc_recv(zfs_cmd_t
*zc
)
4020 dmu_recv_cookie_t drc
;
4021 boolean_t force
= (boolean_t
)zc
->zc_guid
;
4024 int props_error
= 0;
4027 nvlist_t
*props
= NULL
; /* sent properties */
4028 nvlist_t
*origprops
= NULL
; /* existing properties */
4029 char *origin
= NULL
;
4031 char tofs
[ZFS_MAXNAMELEN
];
4032 boolean_t first_recvd_props
= B_FALSE
;
4034 if (dataset_namecheck(zc
->zc_value
, NULL
, NULL
) != 0 ||
4035 strchr(zc
->zc_value
, '@') == NULL
||
4036 strchr(zc
->zc_value
, '%'))
4037 return (SET_ERROR(EINVAL
));
4039 (void) strcpy(tofs
, zc
->zc_value
);
4040 tosnap
= strchr(tofs
, '@');
4043 if (zc
->zc_nvlist_src
!= 0 &&
4044 (error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
4045 zc
->zc_iflags
, &props
)) != 0)
4052 return (SET_ERROR(EBADF
));
4055 VERIFY(nvlist_alloc(&errors
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4057 if (zc
->zc_string
[0])
4058 origin
= zc
->zc_string
;
4060 error
= dmu_recv_begin(tofs
, tosnap
,
4061 &zc
->zc_begin_record
, force
, origin
, &drc
);
4066 * Set properties before we receive the stream so that they are applied
4067 * to the new data. Note that we must call dmu_recv_stream() if
4068 * dmu_recv_begin() succeeds.
4070 if (props
!= NULL
&& !drc
.drc_newfs
) {
4071 if (spa_version(dsl_dataset_get_spa(drc
.drc_ds
)) >=
4072 SPA_VERSION_RECVD_PROPS
&&
4073 !dsl_prop_get_hasrecvd(tofs
))
4074 first_recvd_props
= B_TRUE
;
4077 * If new received properties are supplied, they are to
4078 * completely replace the existing received properties, so stash
4079 * away the existing ones.
4081 if (dsl_prop_get_received(tofs
, &origprops
) == 0) {
4082 nvlist_t
*errlist
= NULL
;
4084 * Don't bother writing a property if its value won't
4085 * change (and avoid the unnecessary security checks).
4087 * The first receive after SPA_VERSION_RECVD_PROPS is a
4088 * special case where we blow away all local properties
4091 if (!first_recvd_props
)
4092 props_reduce(props
, origprops
);
4093 if (zfs_check_clearable(tofs
, origprops
, &errlist
) != 0)
4094 (void) nvlist_merge(errors
, errlist
, 0);
4095 nvlist_free(errlist
);
4097 if (clear_received_props(tofs
, origprops
,
4098 first_recvd_props
? NULL
: props
) != 0)
4099 zc
->zc_obj
|= ZPROP_ERR_NOCLEAR
;
4101 zc
->zc_obj
|= ZPROP_ERR_NOCLEAR
;
4105 if (props
!= NULL
) {
4106 props_error
= dsl_prop_set_hasrecvd(tofs
);
4108 if (props_error
== 0) {
4109 (void) zfs_set_prop_nvlist(tofs
, ZPROP_SRC_RECEIVED
,
4114 if (zc
->zc_nvlist_dst_size
!= 0 &&
4115 (nvlist_smush(errors
, zc
->zc_nvlist_dst_size
) != 0 ||
4116 put_nvlist(zc
, errors
) != 0)) {
4118 * Caller made zc->zc_nvlist_dst less than the minimum expected
4119 * size or supplied an invalid address.
4121 props_error
= SET_ERROR(EINVAL
);
4125 error
= dmu_recv_stream(&drc
, fp
->f_vnode
, &off
, zc
->zc_cleanup_fd
,
4126 &zc
->zc_action_handle
);
4129 zfs_sb_t
*zsb
= NULL
;
4131 if (get_zfs_sb(tofs
, &zsb
) == 0) {
4135 error
= zfs_suspend_fs(zsb
);
4137 * If the suspend fails, then the recv_end will
4138 * likely also fail, and clean up after itself.
4140 end_err
= dmu_recv_end(&drc
, zsb
);
4142 error
= zfs_resume_fs(zsb
, tofs
);
4143 error
= error
? error
: end_err
;
4144 deactivate_super(zsb
->z_sb
);
4146 error
= dmu_recv_end(&drc
, NULL
);
4150 zc
->zc_cookie
= off
- fp
->f_offset
;
4151 if (VOP_SEEK(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
4155 if (zfs_ioc_recv_inject_err
) {
4156 zfs_ioc_recv_inject_err
= B_FALSE
;
4163 zvol_create_minors(tofs
);
4167 * On error, restore the original props.
4169 if (error
!= 0 && props
!= NULL
&& !drc
.drc_newfs
) {
4170 if (clear_received_props(tofs
, props
, NULL
) != 0) {
4172 * We failed to clear the received properties.
4173 * Since we may have left a $recvd value on the
4174 * system, we can't clear the $hasrecvd flag.
4176 zc
->zc_obj
|= ZPROP_ERR_NORESTORE
;
4177 } else if (first_recvd_props
) {
4178 dsl_prop_unset_hasrecvd(tofs
);
4181 if (origprops
== NULL
&& !drc
.drc_newfs
) {
4182 /* We failed to stash the original properties. */
4183 zc
->zc_obj
|= ZPROP_ERR_NORESTORE
;
4187 * dsl_props_set() will not convert RECEIVED to LOCAL on or
4188 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
4189 * explictly if we're restoring local properties cleared in the
4190 * first new-style receive.
4192 if (origprops
!= NULL
&&
4193 zfs_set_prop_nvlist(tofs
, (first_recvd_props
?
4194 ZPROP_SRC_LOCAL
: ZPROP_SRC_RECEIVED
),
4195 origprops
, NULL
) != 0) {
4197 * We stashed the original properties but failed to
4200 zc
->zc_obj
|= ZPROP_ERR_NORESTORE
;
4205 nvlist_free(origprops
);
4206 nvlist_free(errors
);
4210 error
= props_error
;
4217 * zc_name name of snapshot to send
4218 * zc_cookie file descriptor to send stream to
4219 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
4220 * zc_sendobj objsetid of snapshot to send
4221 * zc_fromobj objsetid of incremental fromsnap (may be zero)
4222 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
4223 * output size in zc_objset_type.
4224 * zc_flags if =1, WRITE_EMBEDDED records are permitted
4227 * zc_objset_type estimated size, if zc_guid is set
4230 zfs_ioc_send(zfs_cmd_t
*zc
)
4234 boolean_t estimate
= (zc
->zc_guid
!= 0);
4235 boolean_t embedok
= (zc
->zc_flags
& 0x1);
4237 if (zc
->zc_obj
!= 0) {
4239 dsl_dataset_t
*tosnap
;
4241 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
4245 error
= dsl_dataset_hold_obj(dp
, zc
->zc_sendobj
, FTAG
, &tosnap
);
4247 dsl_pool_rele(dp
, FTAG
);
4251 if (dsl_dir_is_clone(tosnap
->ds_dir
))
4253 dsl_dir_phys(tosnap
->ds_dir
)->dd_origin_obj
;
4254 dsl_dataset_rele(tosnap
, FTAG
);
4255 dsl_pool_rele(dp
, FTAG
);
4260 dsl_dataset_t
*tosnap
;
4261 dsl_dataset_t
*fromsnap
= NULL
;
4263 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
4267 error
= dsl_dataset_hold_obj(dp
, zc
->zc_sendobj
, FTAG
, &tosnap
);
4269 dsl_pool_rele(dp
, FTAG
);
4273 if (zc
->zc_fromobj
!= 0) {
4274 error
= dsl_dataset_hold_obj(dp
, zc
->zc_fromobj
,
4277 dsl_dataset_rele(tosnap
, FTAG
);
4278 dsl_pool_rele(dp
, FTAG
);
4283 error
= dmu_send_estimate(tosnap
, fromsnap
,
4284 &zc
->zc_objset_type
);
4286 if (fromsnap
!= NULL
)
4287 dsl_dataset_rele(fromsnap
, FTAG
);
4288 dsl_dataset_rele(tosnap
, FTAG
);
4289 dsl_pool_rele(dp
, FTAG
);
4291 file_t
*fp
= getf(zc
->zc_cookie
);
4293 return (SET_ERROR(EBADF
));
4296 error
= dmu_send_obj(zc
->zc_name
, zc
->zc_sendobj
,
4297 zc
->zc_fromobj
, embedok
, zc
->zc_cookie
, fp
->f_vnode
, &off
);
4299 if (VOP_SEEK(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
4301 releasef(zc
->zc_cookie
);
4308 * zc_name name of snapshot on which to report progress
4309 * zc_cookie file descriptor of send stream
4312 * zc_cookie number of bytes written in send stream thus far
4315 zfs_ioc_send_progress(zfs_cmd_t
*zc
)
4319 dmu_sendarg_t
*dsp
= NULL
;
4322 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
4326 error
= dsl_dataset_hold(dp
, zc
->zc_name
, FTAG
, &ds
);
4328 dsl_pool_rele(dp
, FTAG
);
4332 mutex_enter(&ds
->ds_sendstream_lock
);
4335 * Iterate over all the send streams currently active on this dataset.
4336 * If there's one which matches the specified file descriptor _and_ the
4337 * stream was started by the current process, return the progress of
4341 for (dsp
= list_head(&ds
->ds_sendstreams
); dsp
!= NULL
;
4342 dsp
= list_next(&ds
->ds_sendstreams
, dsp
)) {
4343 if (dsp
->dsa_outfd
== zc
->zc_cookie
&&
4344 dsp
->dsa_proc
->group_leader
== curproc
->group_leader
)
4349 zc
->zc_cookie
= *(dsp
->dsa_off
);
4351 error
= SET_ERROR(ENOENT
);
4353 mutex_exit(&ds
->ds_sendstream_lock
);
4354 dsl_dataset_rele(ds
, FTAG
);
4355 dsl_pool_rele(dp
, FTAG
);
4360 zfs_ioc_inject_fault(zfs_cmd_t
*zc
)
4364 error
= zio_inject_fault(zc
->zc_name
, (int)zc
->zc_guid
, &id
,
4365 &zc
->zc_inject_record
);
4368 zc
->zc_guid
= (uint64_t)id
;
4374 zfs_ioc_clear_fault(zfs_cmd_t
*zc
)
4376 return (zio_clear_fault((int)zc
->zc_guid
));
4380 zfs_ioc_inject_list_next(zfs_cmd_t
*zc
)
4382 int id
= (int)zc
->zc_guid
;
4385 error
= zio_inject_list_next(&id
, zc
->zc_name
, sizeof (zc
->zc_name
),
4386 &zc
->zc_inject_record
);
4394 zfs_ioc_error_log(zfs_cmd_t
*zc
)
4398 size_t count
= (size_t)zc
->zc_nvlist_dst_size
;
4400 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
4403 error
= spa_get_errlog(spa
, (void *)(uintptr_t)zc
->zc_nvlist_dst
,
4406 zc
->zc_nvlist_dst_size
= count
;
4408 zc
->zc_nvlist_dst_size
= spa_get_errlog_size(spa
);
4410 spa_close(spa
, FTAG
);
4416 zfs_ioc_clear(zfs_cmd_t
*zc
)
4423 * On zpool clear we also fix up missing slogs
4425 mutex_enter(&spa_namespace_lock
);
4426 spa
= spa_lookup(zc
->zc_name
);
4428 mutex_exit(&spa_namespace_lock
);
4429 return (SET_ERROR(EIO
));
4431 if (spa_get_log_state(spa
) == SPA_LOG_MISSING
) {
4432 /* we need to let spa_open/spa_load clear the chains */
4433 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
4435 spa
->spa_last_open_failed
= 0;
4436 mutex_exit(&spa_namespace_lock
);
4438 if (zc
->zc_cookie
& ZPOOL_NO_REWIND
) {
4439 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
4442 nvlist_t
*config
= NULL
;
4444 if (zc
->zc_nvlist_src
== 0)
4445 return (SET_ERROR(EINVAL
));
4447 if ((error
= get_nvlist(zc
->zc_nvlist_src
,
4448 zc
->zc_nvlist_src_size
, zc
->zc_iflags
, &policy
)) == 0) {
4449 error
= spa_open_rewind(zc
->zc_name
, &spa
, FTAG
,
4451 if (config
!= NULL
) {
4454 if ((err
= put_nvlist(zc
, config
)) != 0)
4456 nvlist_free(config
);
4458 nvlist_free(policy
);
4465 spa_vdev_state_enter(spa
, SCL_NONE
);
4467 if (zc
->zc_guid
== 0) {
4470 vd
= spa_lookup_by_guid(spa
, zc
->zc_guid
, B_TRUE
);
4472 (void) spa_vdev_state_exit(spa
, NULL
, ENODEV
);
4473 spa_close(spa
, FTAG
);
4474 return (SET_ERROR(ENODEV
));
4478 vdev_clear(spa
, vd
);
4480 (void) spa_vdev_state_exit(spa
, NULL
, 0);
4483 * Resume any suspended I/Os.
4485 if (zio_resume(spa
) != 0)
4486 error
= SET_ERROR(EIO
);
4488 spa_close(spa
, FTAG
);
4494 zfs_ioc_pool_reopen(zfs_cmd_t
*zc
)
4499 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
4503 spa_vdev_state_enter(spa
, SCL_NONE
);
4506 * If a resilver is already in progress then set the
4507 * spa_scrub_reopen flag to B_TRUE so that we don't restart
4508 * the scan as a side effect of the reopen. Otherwise, let
4509 * vdev_open() decided if a resilver is required.
4511 spa
->spa_scrub_reopen
= dsl_scan_resilvering(spa
->spa_dsl_pool
);
4512 vdev_reopen(spa
->spa_root_vdev
);
4513 spa
->spa_scrub_reopen
= B_FALSE
;
4515 (void) spa_vdev_state_exit(spa
, NULL
, 0);
4516 spa_close(spa
, FTAG
);
4521 * zc_name name of filesystem
4522 * zc_value name of origin snapshot
4525 * zc_string name of conflicting snapshot, if there is one
4528 zfs_ioc_promote(zfs_cmd_t
*zc
)
4533 * We don't need to unmount *all* the origin fs's snapshots, but
4536 cp
= strchr(zc
->zc_value
, '@');
4539 (void) dmu_objset_find(zc
->zc_value
,
4540 zfs_unmount_snap_cb
, NULL
, DS_FIND_SNAPSHOTS
);
4541 return (dsl_dataset_promote(zc
->zc_name
, zc
->zc_string
));
4545 * Retrieve a single {user|group}{used|quota}@... property.
4548 * zc_name name of filesystem
4549 * zc_objset_type zfs_userquota_prop_t
4550 * zc_value domain name (eg. "S-1-234-567-89")
4551 * zc_guid RID/UID/GID
4554 * zc_cookie property value
4557 zfs_ioc_userspace_one(zfs_cmd_t
*zc
)
4562 if (zc
->zc_objset_type
>= ZFS_NUM_USERQUOTA_PROPS
)
4563 return (SET_ERROR(EINVAL
));
4565 error
= zfs_sb_hold(zc
->zc_name
, FTAG
, &zsb
, B_FALSE
);
4569 error
= zfs_userspace_one(zsb
,
4570 zc
->zc_objset_type
, zc
->zc_value
, zc
->zc_guid
, &zc
->zc_cookie
);
4571 zfs_sb_rele(zsb
, FTAG
);
4578 * zc_name name of filesystem
4579 * zc_cookie zap cursor
4580 * zc_objset_type zfs_userquota_prop_t
4581 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
4584 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
4585 * zc_cookie zap cursor
4588 zfs_ioc_userspace_many(zfs_cmd_t
*zc
)
4591 int bufsize
= zc
->zc_nvlist_dst_size
;
4596 return (SET_ERROR(ENOMEM
));
4598 error
= zfs_sb_hold(zc
->zc_name
, FTAG
, &zsb
, B_FALSE
);
4602 buf
= vmem_alloc(bufsize
, KM_SLEEP
);
4604 error
= zfs_userspace_many(zsb
, zc
->zc_objset_type
, &zc
->zc_cookie
,
4605 buf
, &zc
->zc_nvlist_dst_size
);
4608 error
= xcopyout(buf
,
4609 (void *)(uintptr_t)zc
->zc_nvlist_dst
,
4610 zc
->zc_nvlist_dst_size
);
4612 vmem_free(buf
, bufsize
);
4613 zfs_sb_rele(zsb
, FTAG
);
4620 * zc_name name of filesystem
4626 zfs_ioc_userspace_upgrade(zfs_cmd_t
*zc
)
4632 if (get_zfs_sb(zc
->zc_name
, &zsb
) == 0) {
4633 if (!dmu_objset_userused_enabled(zsb
->z_os
)) {
4635 * If userused is not enabled, it may be because the
4636 * objset needs to be closed & reopened (to grow the
4637 * objset_phys_t). Suspend/resume the fs will do that.
4639 error
= zfs_suspend_fs(zsb
);
4641 dmu_objset_refresh_ownership(zsb
->z_os
,
4643 error
= zfs_resume_fs(zsb
, zc
->zc_name
);
4647 error
= dmu_objset_userspace_upgrade(zsb
->z_os
);
4648 deactivate_super(zsb
->z_sb
);
4650 /* XXX kind of reading contents without owning */
4651 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
4655 error
= dmu_objset_userspace_upgrade(os
);
4656 dmu_objset_rele(os
, FTAG
);
4663 zfs_ioc_share(zfs_cmd_t
*zc
)
4665 return (SET_ERROR(ENOSYS
));
4668 ace_t full_access
[] = {
4669 {(uid_t
)-1, ACE_ALL_PERMS
, ACE_EVERYONE
, 0}
4674 * zc_name name of containing filesystem
4675 * zc_obj object # beyond which we want next in-use object #
4678 * zc_obj next in-use object #
4681 zfs_ioc_next_obj(zfs_cmd_t
*zc
)
4683 objset_t
*os
= NULL
;
4686 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
4690 error
= dmu_object_next(os
, &zc
->zc_obj
, B_FALSE
,
4691 dsl_dataset_phys(os
->os_dsl_dataset
)->ds_prev_snap_txg
);
4693 dmu_objset_rele(os
, FTAG
);
4699 * zc_name name of filesystem
4700 * zc_value prefix name for snapshot
4701 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
4704 * zc_value short name of new snapshot
4707 zfs_ioc_tmp_snapshot(zfs_cmd_t
*zc
)
4714 error
= zfs_onexit_fd_hold(zc
->zc_cleanup_fd
, &minor
);
4718 snap_name
= kmem_asprintf("%s-%016llx", zc
->zc_value
,
4719 (u_longlong_t
)ddi_get_lbolt64());
4720 hold_name
= kmem_asprintf("%%%s", zc
->zc_value
);
4722 error
= dsl_dataset_snapshot_tmp(zc
->zc_name
, snap_name
, minor
,
4725 (void) strcpy(zc
->zc_value
, snap_name
);
4728 zfs_onexit_fd_rele(zc
->zc_cleanup_fd
);
4734 * zc_name name of "to" snapshot
4735 * zc_value name of "from" snapshot
4736 * zc_cookie file descriptor to write diff data on
4739 * dmu_diff_record_t's to the file descriptor
4742 zfs_ioc_diff(zfs_cmd_t
*zc
)
4748 fp
= getf(zc
->zc_cookie
);
4750 return (SET_ERROR(EBADF
));
4754 error
= dmu_diff(zc
->zc_name
, zc
->zc_value
, fp
->f_vnode
, &off
);
4756 if (VOP_SEEK(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
4758 releasef(zc
->zc_cookie
);
4764 * Remove all ACL files in shares dir
4766 #ifdef HAVE_SMB_SHARE
4768 zfs_smb_acl_purge(znode_t
*dzp
)
4771 zap_attribute_t zap
;
4772 zfs_sb_t
*zsb
= ZTOZSB(dzp
);
4775 for (zap_cursor_init(&zc
, zsb
->z_os
, dzp
->z_id
);
4776 (error
= zap_cursor_retrieve(&zc
, &zap
)) == 0;
4777 zap_cursor_advance(&zc
)) {
4778 if ((error
= VOP_REMOVE(ZTOV(dzp
), zap
.za_name
, kcred
,
4782 zap_cursor_fini(&zc
);
4785 #endif /* HAVE_SMB_SHARE */
4788 zfs_ioc_smb_acl(zfs_cmd_t
*zc
)
4790 #ifdef HAVE_SMB_SHARE
4793 vnode_t
*resourcevp
= NULL
;
4802 if ((error
= lookupname(zc
->zc_value
, UIO_SYSSPACE
,
4803 NO_FOLLOW
, NULL
, &vp
)) != 0)
4806 /* Now make sure mntpnt and dataset are ZFS */
4808 if (vp
->v_vfsp
->vfs_fstype
!= zfsfstype
||
4809 (strcmp((char *)refstr_value(vp
->v_vfsp
->vfs_resource
),
4810 zc
->zc_name
) != 0)) {
4812 return (SET_ERROR(EINVAL
));
4820 * Create share dir if its missing.
4822 mutex_enter(&zsb
->z_lock
);
4823 if (zsb
->z_shares_dir
== 0) {
4826 tx
= dmu_tx_create(zsb
->z_os
);
4827 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
, TRUE
,
4829 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, FALSE
, NULL
);
4830 error
= dmu_tx_assign(tx
, TXG_WAIT
);
4834 error
= zfs_create_share_dir(zsb
, tx
);
4838 mutex_exit(&zsb
->z_lock
);
4844 mutex_exit(&zsb
->z_lock
);
4846 ASSERT(zsb
->z_shares_dir
);
4847 if ((error
= zfs_zget(zsb
, zsb
->z_shares_dir
, &sharedir
)) != 0) {
4853 switch (zc
->zc_cookie
) {
4854 case ZFS_SMB_ACL_ADD
:
4855 vattr
.va_mask
= AT_MODE
|AT_UID
|AT_GID
|AT_TYPE
;
4856 vattr
.va_mode
= S_IFREG
|0777;
4860 vsec
.vsa_mask
= VSA_ACE
;
4861 vsec
.vsa_aclentp
= &full_access
;
4862 vsec
.vsa_aclentsz
= sizeof (full_access
);
4863 vsec
.vsa_aclcnt
= 1;
4865 error
= VOP_CREATE(ZTOV(sharedir
), zc
->zc_string
,
4866 &vattr
, EXCL
, 0, &resourcevp
, kcred
, 0, NULL
, &vsec
);
4868 VN_RELE(resourcevp
);
4871 case ZFS_SMB_ACL_REMOVE
:
4872 error
= VOP_REMOVE(ZTOV(sharedir
), zc
->zc_string
, kcred
,
4876 case ZFS_SMB_ACL_RENAME
:
4877 if ((error
= get_nvlist(zc
->zc_nvlist_src
,
4878 zc
->zc_nvlist_src_size
, zc
->zc_iflags
, &nvlist
)) != 0) {
4883 if (nvlist_lookup_string(nvlist
, ZFS_SMB_ACL_SRC
, &src
) ||
4884 nvlist_lookup_string(nvlist
, ZFS_SMB_ACL_TARGET
,
4887 VN_RELE(ZTOV(sharedir
));
4889 nvlist_free(nvlist
);
4892 error
= VOP_RENAME(ZTOV(sharedir
), src
, ZTOV(sharedir
), target
,
4894 nvlist_free(nvlist
);
4897 case ZFS_SMB_ACL_PURGE
:
4898 error
= zfs_smb_acl_purge(sharedir
);
4902 error
= SET_ERROR(EINVAL
);
4907 VN_RELE(ZTOV(sharedir
));
4913 return (SET_ERROR(ENOTSUP
));
4914 #endif /* HAVE_SMB_SHARE */
4919 * "holds" -> { snapname -> holdname (string), ... }
4920 * (optional) "cleanup_fd" -> fd (int32)
4924 * snapname -> error value (int32)
4930 zfs_ioc_hold(const char *pool
, nvlist_t
*args
, nvlist_t
*errlist
)
4933 int cleanup_fd
= -1;
4937 error
= nvlist_lookup_nvlist(args
, "holds", &holds
);
4939 return (SET_ERROR(EINVAL
));
4941 if (nvlist_lookup_int32(args
, "cleanup_fd", &cleanup_fd
) == 0) {
4942 error
= zfs_onexit_fd_hold(cleanup_fd
, &minor
);
4947 error
= dsl_dataset_user_hold(holds
, minor
, errlist
);
4949 zfs_onexit_fd_rele(cleanup_fd
);
4954 * innvl is not used.
4957 * holdname -> time added (uint64 seconds since epoch)
4963 zfs_ioc_get_holds(const char *snapname
, nvlist_t
*args
, nvlist_t
*outnvl
)
4965 return (dsl_dataset_get_holds(snapname
, outnvl
));
4970 * snapname -> { holdname, ... }
4975 * snapname -> error value (int32)
4981 zfs_ioc_release(const char *pool
, nvlist_t
*holds
, nvlist_t
*errlist
)
4983 return (dsl_dataset_user_release(holds
, errlist
));
4988 * zc_guid flags (ZEVENT_NONBLOCK)
4989 * zc_cleanup_fd zevent file descriptor
4992 * zc_nvlist_dst next nvlist event
4993 * zc_cookie dropped events since last get
4996 zfs_ioc_events_next(zfs_cmd_t
*zc
)
4999 nvlist_t
*event
= NULL
;
5001 uint64_t dropped
= 0;
5004 error
= zfs_zevent_fd_hold(zc
->zc_cleanup_fd
, &minor
, &ze
);
5009 error
= zfs_zevent_next(ze
, &event
,
5010 &zc
->zc_nvlist_dst_size
, &dropped
);
5011 if (event
!= NULL
) {
5012 zc
->zc_cookie
= dropped
;
5013 error
= put_nvlist(zc
, event
);
5017 if (zc
->zc_guid
& ZEVENT_NONBLOCK
)
5020 if ((error
== 0) || (error
!= ENOENT
))
5023 error
= zfs_zevent_wait(ze
);
5028 zfs_zevent_fd_rele(zc
->zc_cleanup_fd
);
5035 * zc_cookie cleared events count
5038 zfs_ioc_events_clear(zfs_cmd_t
*zc
)
5042 zfs_zevent_drain_all(&count
);
5043 zc
->zc_cookie
= count
;
5050 * zc_guid eid | ZEVENT_SEEK_START | ZEVENT_SEEK_END
5051 * zc_cleanup zevent file descriptor
5054 zfs_ioc_events_seek(zfs_cmd_t
*zc
)
5060 error
= zfs_zevent_fd_hold(zc
->zc_cleanup_fd
, &minor
, &ze
);
5064 error
= zfs_zevent_seek(ze
, zc
->zc_guid
);
5065 zfs_zevent_fd_rele(zc
->zc_cleanup_fd
);
5072 * zc_name name of new filesystem or snapshot
5073 * zc_value full name of old snapshot
5076 * zc_cookie space in bytes
5077 * zc_objset_type compressed space in bytes
5078 * zc_perm_action uncompressed space in bytes
5081 zfs_ioc_space_written(zfs_cmd_t
*zc
)
5085 dsl_dataset_t
*new, *old
;
5087 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
5090 error
= dsl_dataset_hold(dp
, zc
->zc_name
, FTAG
, &new);
5092 dsl_pool_rele(dp
, FTAG
);
5095 error
= dsl_dataset_hold(dp
, zc
->zc_value
, FTAG
, &old
);
5097 dsl_dataset_rele(new, FTAG
);
5098 dsl_pool_rele(dp
, FTAG
);
5102 error
= dsl_dataset_space_written(old
, new, &zc
->zc_cookie
,
5103 &zc
->zc_objset_type
, &zc
->zc_perm_action
);
5104 dsl_dataset_rele(old
, FTAG
);
5105 dsl_dataset_rele(new, FTAG
);
5106 dsl_pool_rele(dp
, FTAG
);
5112 * "firstsnap" -> snapshot name
5116 * "used" -> space in bytes
5117 * "compressed" -> compressed space in bytes
5118 * "uncompressed" -> uncompressed space in bytes
5122 zfs_ioc_space_snaps(const char *lastsnap
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
5126 dsl_dataset_t
*new, *old
;
5128 uint64_t used
, comp
, uncomp
;
5130 if (nvlist_lookup_string(innvl
, "firstsnap", &firstsnap
) != 0)
5131 return (SET_ERROR(EINVAL
));
5133 error
= dsl_pool_hold(lastsnap
, FTAG
, &dp
);
5137 error
= dsl_dataset_hold(dp
, lastsnap
, FTAG
, &new);
5139 dsl_pool_rele(dp
, FTAG
);
5142 error
= dsl_dataset_hold(dp
, firstsnap
, FTAG
, &old
);
5144 dsl_dataset_rele(new, FTAG
);
5145 dsl_pool_rele(dp
, FTAG
);
5149 error
= dsl_dataset_space_wouldfree(old
, new, &used
, &comp
, &uncomp
);
5150 dsl_dataset_rele(old
, FTAG
);
5151 dsl_dataset_rele(new, FTAG
);
5152 dsl_pool_rele(dp
, FTAG
);
5153 fnvlist_add_uint64(outnvl
, "used", used
);
5154 fnvlist_add_uint64(outnvl
, "compressed", comp
);
5155 fnvlist_add_uint64(outnvl
, "uncompressed", uncomp
);
5161 * "fd" -> file descriptor to write stream to (int32)
5162 * (optional) "fromsnap" -> full snap name to send an incremental from
5163 * (optional) "embedok" -> (value ignored)
5164 * presence indicates DRR_WRITE_EMBEDDED records are permitted
5171 zfs_ioc_send_new(const char *snapname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
5175 char *fromname
= NULL
;
5180 error
= nvlist_lookup_int32(innvl
, "fd", &fd
);
5182 return (SET_ERROR(EINVAL
));
5184 (void) nvlist_lookup_string(innvl
, "fromsnap", &fromname
);
5186 embedok
= nvlist_exists(innvl
, "embedok");
5188 if ((fp
= getf(fd
)) == NULL
)
5189 return (SET_ERROR(EBADF
));
5192 error
= dmu_send(snapname
, fromname
, embedok
, fd
, fp
->f_vnode
, &off
);
5194 if (VOP_SEEK(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
5202 * Determine approximately how large a zfs send stream will be -- the number
5203 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
5206 * (optional) "fromsnap" -> full snap name to send an incremental from
5210 * "space" -> bytes of space (uint64)
5214 zfs_ioc_send_space(const char *snapname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
5217 dsl_dataset_t
*fromsnap
= NULL
;
5218 dsl_dataset_t
*tosnap
;
5223 error
= dsl_pool_hold(snapname
, FTAG
, &dp
);
5227 error
= dsl_dataset_hold(dp
, snapname
, FTAG
, &tosnap
);
5229 dsl_pool_rele(dp
, FTAG
);
5233 error
= nvlist_lookup_string(innvl
, "fromsnap", &fromname
);
5235 error
= dsl_dataset_hold(dp
, fromname
, FTAG
, &fromsnap
);
5237 dsl_dataset_rele(tosnap
, FTAG
);
5238 dsl_pool_rele(dp
, FTAG
);
5243 error
= dmu_send_estimate(tosnap
, fromsnap
, &space
);
5244 fnvlist_add_uint64(outnvl
, "space", space
);
5246 if (fromsnap
!= NULL
)
5247 dsl_dataset_rele(fromsnap
, FTAG
);
5248 dsl_dataset_rele(tosnap
, FTAG
);
5249 dsl_pool_rele(dp
, FTAG
);
5254 static zfs_ioc_vec_t zfs_ioc_vec
[ZFS_IOC_LAST
- ZFS_IOC_FIRST
];
5257 zfs_ioctl_register_legacy(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5258 zfs_secpolicy_func_t
*secpolicy
, zfs_ioc_namecheck_t namecheck
,
5259 boolean_t log_history
, zfs_ioc_poolcheck_t pool_check
)
5261 zfs_ioc_vec_t
*vec
= &zfs_ioc_vec
[ioc
- ZFS_IOC_FIRST
];
5263 ASSERT3U(ioc
, >=, ZFS_IOC_FIRST
);
5264 ASSERT3U(ioc
, <, ZFS_IOC_LAST
);
5265 ASSERT3P(vec
->zvec_legacy_func
, ==, NULL
);
5266 ASSERT3P(vec
->zvec_func
, ==, NULL
);
5268 vec
->zvec_legacy_func
= func
;
5269 vec
->zvec_secpolicy
= secpolicy
;
5270 vec
->zvec_namecheck
= namecheck
;
5271 vec
->zvec_allow_log
= log_history
;
5272 vec
->zvec_pool_check
= pool_check
;
5276 * See the block comment at the beginning of this file for details on
5277 * each argument to this function.
5280 zfs_ioctl_register(const char *name
, zfs_ioc_t ioc
, zfs_ioc_func_t
*func
,
5281 zfs_secpolicy_func_t
*secpolicy
, zfs_ioc_namecheck_t namecheck
,
5282 zfs_ioc_poolcheck_t pool_check
, boolean_t smush_outnvlist
,
5283 boolean_t allow_log
)
5285 zfs_ioc_vec_t
*vec
= &zfs_ioc_vec
[ioc
- ZFS_IOC_FIRST
];
5287 ASSERT3U(ioc
, >=, ZFS_IOC_FIRST
);
5288 ASSERT3U(ioc
, <, ZFS_IOC_LAST
);
5289 ASSERT3P(vec
->zvec_legacy_func
, ==, NULL
);
5290 ASSERT3P(vec
->zvec_func
, ==, NULL
);
5292 /* if we are logging, the name must be valid */
5293 ASSERT(!allow_log
|| namecheck
!= NO_NAME
);
5295 vec
->zvec_name
= name
;
5296 vec
->zvec_func
= func
;
5297 vec
->zvec_secpolicy
= secpolicy
;
5298 vec
->zvec_namecheck
= namecheck
;
5299 vec
->zvec_pool_check
= pool_check
;
5300 vec
->zvec_smush_outnvlist
= smush_outnvlist
;
5301 vec
->zvec_allow_log
= allow_log
;
5305 zfs_ioctl_register_pool(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5306 zfs_secpolicy_func_t
*secpolicy
, boolean_t log_history
,
5307 zfs_ioc_poolcheck_t pool_check
)
5309 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5310 POOL_NAME
, log_history
, pool_check
);
5314 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5315 zfs_secpolicy_func_t
*secpolicy
, zfs_ioc_poolcheck_t pool_check
)
5317 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5318 DATASET_NAME
, B_FALSE
, pool_check
);
5322 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
)
5324 zfs_ioctl_register_legacy(ioc
, func
, zfs_secpolicy_config
,
5325 POOL_NAME
, B_TRUE
, POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
5329 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5330 zfs_secpolicy_func_t
*secpolicy
)
5332 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5333 NO_NAME
, B_FALSE
, POOL_CHECK_NONE
);
5337 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc
,
5338 zfs_ioc_legacy_func_t
*func
, zfs_secpolicy_func_t
*secpolicy
)
5340 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5341 DATASET_NAME
, B_FALSE
, POOL_CHECK_SUSPENDED
);
5345 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
)
5347 zfs_ioctl_register_dataset_read_secpolicy(ioc
, func
,
5348 zfs_secpolicy_read
);
5352 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5353 zfs_secpolicy_func_t
*secpolicy
)
5355 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5356 DATASET_NAME
, B_TRUE
, POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
5360 zfs_ioctl_init(void)
5362 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT
,
5363 zfs_ioc_snapshot
, zfs_secpolicy_snapshot
, POOL_NAME
,
5364 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5366 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY
,
5367 zfs_ioc_log_history
, zfs_secpolicy_log_history
, NO_NAME
,
5368 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_FALSE
, B_FALSE
);
5370 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS
,
5371 zfs_ioc_space_snaps
, zfs_secpolicy_read
, DATASET_NAME
,
5372 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
5374 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW
,
5375 zfs_ioc_send_new
, zfs_secpolicy_send_new
, DATASET_NAME
,
5376 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
5378 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE
,
5379 zfs_ioc_send_space
, zfs_secpolicy_read
, DATASET_NAME
,
5380 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
5382 zfs_ioctl_register("create", ZFS_IOC_CREATE
,
5383 zfs_ioc_create
, zfs_secpolicy_create_clone
, DATASET_NAME
,
5384 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5386 zfs_ioctl_register("clone", ZFS_IOC_CLONE
,
5387 zfs_ioc_clone
, zfs_secpolicy_create_clone
, DATASET_NAME
,
5388 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5390 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS
,
5391 zfs_ioc_destroy_snaps
, zfs_secpolicy_destroy_snaps
, POOL_NAME
,
5392 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5394 zfs_ioctl_register("hold", ZFS_IOC_HOLD
,
5395 zfs_ioc_hold
, zfs_secpolicy_hold
, POOL_NAME
,
5396 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5397 zfs_ioctl_register("release", ZFS_IOC_RELEASE
,
5398 zfs_ioc_release
, zfs_secpolicy_release
, POOL_NAME
,
5399 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5401 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS
,
5402 zfs_ioc_get_holds
, zfs_secpolicy_read
, DATASET_NAME
,
5403 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
5405 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK
,
5406 zfs_ioc_rollback
, zfs_secpolicy_rollback
, DATASET_NAME
,
5407 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_FALSE
, B_TRUE
);
5409 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK
,
5410 zfs_ioc_bookmark
, zfs_secpolicy_bookmark
, POOL_NAME
,
5411 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5413 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS
,
5414 zfs_ioc_get_bookmarks
, zfs_secpolicy_read
, DATASET_NAME
,
5415 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
5417 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS
,
5418 zfs_ioc_destroy_bookmarks
, zfs_secpolicy_destroy_bookmarks
,
5420 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5422 /* IOCTLS that use the legacy function signature */
5424 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE
, zfs_ioc_pool_freeze
,
5425 zfs_secpolicy_config
, NO_NAME
, B_FALSE
, POOL_CHECK_READONLY
);
5427 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE
, zfs_ioc_pool_create
,
5428 zfs_secpolicy_config
, B_TRUE
, POOL_CHECK_NONE
);
5429 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN
,
5431 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE
,
5432 zfs_ioc_pool_upgrade
);
5433 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD
,
5435 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE
,
5436 zfs_ioc_vdev_remove
);
5437 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE
,
5438 zfs_ioc_vdev_set_state
);
5439 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH
,
5440 zfs_ioc_vdev_attach
);
5441 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH
,
5442 zfs_ioc_vdev_detach
);
5443 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH
,
5444 zfs_ioc_vdev_setpath
);
5445 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU
,
5446 zfs_ioc_vdev_setfru
);
5447 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS
,
5448 zfs_ioc_pool_set_props
);
5449 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT
,
5450 zfs_ioc_vdev_split
);
5451 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID
,
5452 zfs_ioc_pool_reguid
);
5454 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS
,
5455 zfs_ioc_pool_configs
, zfs_secpolicy_none
);
5456 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT
,
5457 zfs_ioc_pool_tryimport
, zfs_secpolicy_config
);
5458 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT
,
5459 zfs_ioc_inject_fault
, zfs_secpolicy_inject
);
5460 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT
,
5461 zfs_ioc_clear_fault
, zfs_secpolicy_inject
);
5462 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT
,
5463 zfs_ioc_inject_list_next
, zfs_secpolicy_inject
);
5466 * pool destroy, and export don't log the history as part of
5467 * zfsdev_ioctl, but rather zfs_ioc_pool_export
5468 * does the logging of those commands.
5470 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY
, zfs_ioc_pool_destroy
,
5471 zfs_secpolicy_config
, B_FALSE
, POOL_CHECK_SUSPENDED
);
5472 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT
, zfs_ioc_pool_export
,
5473 zfs_secpolicy_config
, B_FALSE
, POOL_CHECK_SUSPENDED
);
5475 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS
, zfs_ioc_pool_stats
,
5476 zfs_secpolicy_read
, B_FALSE
, POOL_CHECK_NONE
);
5477 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS
, zfs_ioc_pool_get_props
,
5478 zfs_secpolicy_read
, B_FALSE
, POOL_CHECK_NONE
);
5480 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG
, zfs_ioc_error_log
,
5481 zfs_secpolicy_inject
, B_FALSE
, POOL_CHECK_SUSPENDED
);
5482 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME
,
5483 zfs_ioc_dsobj_to_dsname
,
5484 zfs_secpolicy_diff
, B_FALSE
, POOL_CHECK_SUSPENDED
);
5485 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY
,
5486 zfs_ioc_pool_get_history
,
5487 zfs_secpolicy_config
, B_FALSE
, POOL_CHECK_SUSPENDED
);
5489 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT
, zfs_ioc_pool_import
,
5490 zfs_secpolicy_config
, B_TRUE
, POOL_CHECK_NONE
);
5492 zfs_ioctl_register_pool(ZFS_IOC_CLEAR
, zfs_ioc_clear
,
5493 zfs_secpolicy_config
, B_TRUE
, POOL_CHECK_NONE
);
5494 zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN
, zfs_ioc_pool_reopen
,
5495 zfs_secpolicy_config
, B_TRUE
, POOL_CHECK_SUSPENDED
);
5497 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN
,
5498 zfs_ioc_space_written
);
5499 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS
,
5500 zfs_ioc_objset_recvd_props
);
5501 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ
,
5503 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL
,
5505 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS
,
5506 zfs_ioc_objset_stats
);
5507 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS
,
5508 zfs_ioc_objset_zplprops
);
5509 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT
,
5510 zfs_ioc_dataset_list_next
);
5511 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT
,
5512 zfs_ioc_snapshot_list_next
);
5513 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS
,
5514 zfs_ioc_send_progress
);
5516 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF
,
5517 zfs_ioc_diff
, zfs_secpolicy_diff
);
5518 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS
,
5519 zfs_ioc_obj_to_stats
, zfs_secpolicy_diff
);
5520 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH
,
5521 zfs_ioc_obj_to_path
, zfs_secpolicy_diff
);
5522 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE
,
5523 zfs_ioc_userspace_one
, zfs_secpolicy_userspace_one
);
5524 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY
,
5525 zfs_ioc_userspace_many
, zfs_secpolicy_userspace_many
);
5526 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND
,
5527 zfs_ioc_send
, zfs_secpolicy_send
);
5529 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP
, zfs_ioc_set_prop
,
5530 zfs_secpolicy_none
);
5531 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY
, zfs_ioc_destroy
,
5532 zfs_secpolicy_destroy
);
5533 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME
, zfs_ioc_rename
,
5534 zfs_secpolicy_rename
);
5535 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV
, zfs_ioc_recv
,
5536 zfs_secpolicy_recv
);
5537 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE
, zfs_ioc_promote
,
5538 zfs_secpolicy_promote
);
5539 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP
,
5540 zfs_ioc_inherit_prop
, zfs_secpolicy_inherit_prop
);
5541 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL
, zfs_ioc_set_fsacl
,
5542 zfs_secpolicy_set_fsacl
);
5544 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE
, zfs_ioc_share
,
5545 zfs_secpolicy_share
, POOL_CHECK_NONE
);
5546 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL
, zfs_ioc_smb_acl
,
5547 zfs_secpolicy_smb_acl
, POOL_CHECK_NONE
);
5548 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE
,
5549 zfs_ioc_userspace_upgrade
, zfs_secpolicy_userspace_upgrade
,
5550 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
5551 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT
,
5552 zfs_ioc_tmp_snapshot
, zfs_secpolicy_tmp_snapshot
,
5553 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
5558 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_NEXT
, zfs_ioc_events_next
,
5559 zfs_secpolicy_config
, NO_NAME
, B_FALSE
, POOL_CHECK_NONE
);
5560 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_CLEAR
, zfs_ioc_events_clear
,
5561 zfs_secpolicy_config
, NO_NAME
, B_FALSE
, POOL_CHECK_NONE
);
5562 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_SEEK
, zfs_ioc_events_seek
,
5563 zfs_secpolicy_config
, NO_NAME
, B_FALSE
, POOL_CHECK_NONE
);
5567 pool_status_check(const char *name
, zfs_ioc_namecheck_t type
,
5568 zfs_ioc_poolcheck_t check
)
5573 ASSERT(type
== POOL_NAME
|| type
== DATASET_NAME
);
5575 if (check
& POOL_CHECK_NONE
)
5578 error
= spa_open(name
, &spa
, FTAG
);
5580 if ((check
& POOL_CHECK_SUSPENDED
) && spa_suspended(spa
))
5581 error
= SET_ERROR(EAGAIN
);
5582 else if ((check
& POOL_CHECK_READONLY
) && !spa_writeable(spa
))
5583 error
= SET_ERROR(EROFS
);
5584 spa_close(spa
, FTAG
);
5590 zfsdev_get_state_impl(minor_t minor
, enum zfsdev_state_type which
)
5594 for (zs
= zfsdev_state_list
; zs
!= NULL
; zs
= zs
->zs_next
) {
5595 if (zs
->zs_minor
== minor
) {
5599 return (zs
->zs_onexit
);
5601 return (zs
->zs_zevent
);
5612 zfsdev_get_state(minor_t minor
, enum zfsdev_state_type which
)
5616 ptr
= zfsdev_get_state_impl(minor
, which
);
5622 zfsdev_getminor(struct file
*filp
)
5624 ASSERT(filp
!= NULL
);
5625 ASSERT(filp
->private_data
!= NULL
);
5627 return (((zfsdev_state_t
*)filp
->private_data
)->zs_minor
);
5631 * Find a free minor number. The zfsdev_state_list is expected to
5632 * be short since it is only a list of currently open file handles.
5635 zfsdev_minor_alloc(void)
5637 static minor_t last_minor
= 0;
5640 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
5642 for (m
= last_minor
+ 1; m
!= last_minor
; m
++) {
5643 if (m
> ZFSDEV_MAX_MINOR
)
5645 if (zfsdev_get_state_impl(m
, ZST_ALL
) == NULL
) {
5655 zfsdev_state_init(struct file
*filp
)
5657 zfsdev_state_t
*zs
, *zsprev
= NULL
;
5659 boolean_t newzs
= B_FALSE
;
5661 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
5663 minor
= zfsdev_minor_alloc();
5665 return (SET_ERROR(ENXIO
));
5667 for (zs
= zfsdev_state_list
; zs
!= NULL
; zs
= zs
->zs_next
) {
5668 if (zs
->zs_minor
== -1)
5674 zs
= kmem_zalloc(sizeof (zfsdev_state_t
), KM_SLEEP
);
5679 filp
->private_data
= zs
;
5681 zfs_onexit_init((zfs_onexit_t
**)&zs
->zs_onexit
);
5682 zfs_zevent_init((zfs_zevent_t
**)&zs
->zs_zevent
);
5686 * In order to provide for lock-free concurrent read access
5687 * to the minor list in zfsdev_get_state_impl(), new entries
5688 * must be completely written before linking them into the
5689 * list whereas existing entries are already linked; the last
5690 * operation must be updating zs_minor (from -1 to the new
5694 zs
->zs_minor
= minor
;
5696 zsprev
->zs_next
= zs
;
5699 zs
->zs_minor
= minor
;
5706 zfsdev_state_destroy(struct file
*filp
)
5710 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
5711 ASSERT(filp
->private_data
!= NULL
);
5713 zs
= filp
->private_data
;
5715 zfs_onexit_destroy(zs
->zs_onexit
);
5716 zfs_zevent_destroy(zs
->zs_zevent
);
5722 zfsdev_open(struct inode
*ino
, struct file
*filp
)
5726 mutex_enter(&zfsdev_state_lock
);
5727 error
= zfsdev_state_init(filp
);
5728 mutex_exit(&zfsdev_state_lock
);
5734 zfsdev_release(struct inode
*ino
, struct file
*filp
)
5738 mutex_enter(&zfsdev_state_lock
);
5739 error
= zfsdev_state_destroy(filp
);
5740 mutex_exit(&zfsdev_state_lock
);
5746 zfsdev_ioctl(struct file
*filp
, unsigned cmd
, unsigned long arg
)
5750 int error
, rc
, flag
= 0;
5751 const zfs_ioc_vec_t
*vec
;
5752 char *saved_poolname
= NULL
;
5753 nvlist_t
*innvl
= NULL
;
5754 fstrans_cookie_t cookie
;
5756 vecnum
= cmd
- ZFS_IOC_FIRST
;
5757 if (vecnum
>= sizeof (zfs_ioc_vec
) / sizeof (zfs_ioc_vec
[0]))
5758 return (-SET_ERROR(EINVAL
));
5759 vec
= &zfs_ioc_vec
[vecnum
];
5762 * The registered ioctl list may be sparse, verify that either
5763 * a normal or legacy handler are registered.
5765 if (vec
->zvec_func
== NULL
&& vec
->zvec_legacy_func
== NULL
)
5766 return (-SET_ERROR(EINVAL
));
5768 zc
= kmem_zalloc(sizeof (zfs_cmd_t
), KM_SLEEP
);
5770 error
= ddi_copyin((void *)arg
, zc
, sizeof (zfs_cmd_t
), flag
);
5772 error
= SET_ERROR(EFAULT
);
5776 zc
->zc_iflags
= flag
& FKIOCTL
;
5777 if (zc
->zc_nvlist_src_size
!= 0) {
5778 error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
5779 zc
->zc_iflags
, &innvl
);
5785 * Ensure that all pool/dataset names are valid before we pass down to
5788 zc
->zc_name
[sizeof (zc
->zc_name
) - 1] = '\0';
5789 switch (vec
->zvec_namecheck
) {
5791 if (pool_namecheck(zc
->zc_name
, NULL
, NULL
) != 0)
5792 error
= SET_ERROR(EINVAL
);
5794 error
= pool_status_check(zc
->zc_name
,
5795 vec
->zvec_namecheck
, vec
->zvec_pool_check
);
5799 if (dataset_namecheck(zc
->zc_name
, NULL
, NULL
) != 0)
5800 error
= SET_ERROR(EINVAL
);
5802 error
= pool_status_check(zc
->zc_name
,
5803 vec
->zvec_namecheck
, vec
->zvec_pool_check
);
5811 if (error
== 0 && !(flag
& FKIOCTL
))
5812 error
= vec
->zvec_secpolicy(zc
, innvl
, CRED());
5817 /* legacy ioctls can modify zc_name */
5818 saved_poolname
= strdup(zc
->zc_name
);
5819 if (saved_poolname
== NULL
) {
5820 error
= SET_ERROR(ENOMEM
);
5823 saved_poolname
[strcspn(saved_poolname
, "/@#")] = '\0';
5826 if (vec
->zvec_func
!= NULL
) {
5830 nvlist_t
*lognv
= NULL
;
5832 ASSERT(vec
->zvec_legacy_func
== NULL
);
5835 * Add the innvl to the lognv before calling the func,
5836 * in case the func changes the innvl.
5838 if (vec
->zvec_allow_log
) {
5839 lognv
= fnvlist_alloc();
5840 fnvlist_add_string(lognv
, ZPOOL_HIST_IOCTL
,
5842 if (!nvlist_empty(innvl
)) {
5843 fnvlist_add_nvlist(lognv
, ZPOOL_HIST_INPUT_NVL
,
5848 outnvl
= fnvlist_alloc();
5849 cookie
= spl_fstrans_mark();
5850 error
= vec
->zvec_func(zc
->zc_name
, innvl
, outnvl
);
5851 spl_fstrans_unmark(cookie
);
5853 if (error
== 0 && vec
->zvec_allow_log
&&
5854 spa_open(zc
->zc_name
, &spa
, FTAG
) == 0) {
5855 if (!nvlist_empty(outnvl
)) {
5856 fnvlist_add_nvlist(lognv
, ZPOOL_HIST_OUTPUT_NVL
,
5859 (void) spa_history_log_nvl(spa
, lognv
);
5860 spa_close(spa
, FTAG
);
5862 fnvlist_free(lognv
);
5864 if (!nvlist_empty(outnvl
) || zc
->zc_nvlist_dst_size
!= 0) {
5866 if (vec
->zvec_smush_outnvlist
) {
5867 smusherror
= nvlist_smush(outnvl
,
5868 zc
->zc_nvlist_dst_size
);
5870 if (smusherror
== 0)
5871 puterror
= put_nvlist(zc
, outnvl
);
5877 nvlist_free(outnvl
);
5879 cookie
= spl_fstrans_mark();
5880 error
= vec
->zvec_legacy_func(zc
);
5881 spl_fstrans_unmark(cookie
);
5886 rc
= ddi_copyout(zc
, (void *)arg
, sizeof (zfs_cmd_t
), flag
);
5887 if (error
== 0 && rc
!= 0)
5888 error
= SET_ERROR(EFAULT
);
5889 if (error
== 0 && vec
->zvec_allow_log
) {
5890 char *s
= tsd_get(zfs_allow_log_key
);
5893 (void) tsd_set(zfs_allow_log_key
, saved_poolname
);
5895 if (saved_poolname
!= NULL
)
5896 strfree(saved_poolname
);
5899 kmem_free(zc
, sizeof (zfs_cmd_t
));
5903 #ifdef CONFIG_COMPAT
5905 zfsdev_compat_ioctl(struct file
*filp
, unsigned cmd
, unsigned long arg
)
5907 return (zfsdev_ioctl(filp
, cmd
, arg
));
5910 #define zfsdev_compat_ioctl NULL
5913 static const struct file_operations zfsdev_fops
= {
5914 .open
= zfsdev_open
,
5915 .release
= zfsdev_release
,
5916 .unlocked_ioctl
= zfsdev_ioctl
,
5917 .compat_ioctl
= zfsdev_compat_ioctl
,
5918 .owner
= THIS_MODULE
,
5921 static struct miscdevice zfs_misc
= {
5922 .minor
= MISC_DYNAMIC_MINOR
,
5924 .fops
= &zfsdev_fops
,
5932 mutex_init(&zfsdev_state_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
5933 zfsdev_state_list
= kmem_zalloc(sizeof (zfsdev_state_t
), KM_SLEEP
);
5934 zfsdev_state_list
->zs_minor
= -1;
5936 error
= misc_register(&zfs_misc
);
5938 printk(KERN_INFO
"ZFS: misc_register() failed %d\n", error
);
5949 zfsdev_state_t
*zs
, *zsprev
= NULL
;
5951 error
= misc_deregister(&zfs_misc
);
5953 printk(KERN_INFO
"ZFS: misc_deregister() failed %d\n", error
);
5955 mutex_destroy(&zfsdev_state_lock
);
5957 for (zs
= zfsdev_state_list
; zs
!= NULL
; zs
= zs
->zs_next
) {
5959 kmem_free(zsprev
, sizeof (zfsdev_state_t
));
5963 kmem_free(zsprev
, sizeof (zfsdev_state_t
));
5967 zfs_allow_log_destroy(void *arg
)
5969 char *poolname
= arg
;
5974 #define ZFS_DEBUG_STR " (DEBUG mode)"
5976 #define ZFS_DEBUG_STR ""
5984 error
= vn_set_pwd("/");
5987 "ZFS: Warning unable to set pwd to '/': %d\n", error
);
5991 spa_init(FREAD
| FWRITE
);
5994 if ((error
= zvol_init()) != 0)
5999 if ((error
= zfs_attach()) != 0)
6002 tsd_create(&zfs_fsyncer_key
, NULL
);
6003 tsd_create(&rrw_tsd_key
, rrw_tsd_destroy
);
6004 tsd_create(&zfs_allow_log_key
, zfs_allow_log_destroy
);
6006 printk(KERN_NOTICE
"ZFS: Loaded module v%s-%s%s, "
6007 "ZFS pool version %s, ZFS filesystem version %s\n",
6008 ZFS_META_VERSION
, ZFS_META_RELEASE
, ZFS_DEBUG_STR
,
6009 SPA_VERSION_STRING
, ZPL_VERSION_STRING
);
6010 #ifndef CONFIG_FS_POSIX_ACL
6011 printk(KERN_NOTICE
"ZFS: Posix ACLs disabled by kernel\n");
6012 #endif /* CONFIG_FS_POSIX_ACL */
6021 printk(KERN_NOTICE
"ZFS: Failed to Load ZFS Filesystem v%s-%s%s"
6022 ", rc = %d\n", ZFS_META_VERSION
, ZFS_META_RELEASE
,
6023 ZFS_DEBUG_STR
, error
);
6036 tsd_destroy(&zfs_fsyncer_key
);
6037 tsd_destroy(&rrw_tsd_key
);
6038 tsd_destroy(&zfs_allow_log_key
);
6040 printk(KERN_NOTICE
"ZFS: Unloaded module v%s-%s%s\n",
6041 ZFS_META_VERSION
, ZFS_META_RELEASE
, ZFS_DEBUG_STR
);
6048 MODULE_DESCRIPTION("ZFS");
6049 MODULE_AUTHOR(ZFS_META_AUTHOR
);
6050 MODULE_LICENSE(ZFS_META_LICENSE
);
6051 MODULE_VERSION(ZFS_META_VERSION
"-" ZFS_META_RELEASE
);
6052 #endif /* HAVE_SPL */