4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Portions Copyright 2011 Martin Matuska
25 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
26 * Copyright (c) 2014, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2013 by Delphix. All rights reserved.
28 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
29 * Copyright (c) 2013 Steven Hartland. All rights reserved.
35 * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
36 * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
38 * There are two ways that we handle ioctls: the legacy way where almost
39 * all of the logic is in the ioctl callback, and the new way where most
40 * of the marshalling is handled in the common entry point, zfsdev_ioctl().
42 * Non-legacy ioctls should be registered by calling
43 * zfs_ioctl_register() from zfs_ioctl_init(). The ioctl is invoked
44 * from userland by lzc_ioctl().
46 * The registration arguments are as follows:
49 * The name of the ioctl. This is used for history logging. If the
50 * ioctl returns successfully (the callback returns 0), and allow_log
51 * is true, then a history log entry will be recorded with the input &
52 * output nvlists. The log entry can be printed with "zpool history -i".
55 * The ioctl request number, which userland will pass to ioctl(2).
56 * The ioctl numbers can change from release to release, because
57 * the caller (libzfs) must be matched to the kernel.
59 * zfs_secpolicy_func_t *secpolicy
60 * This function will be called before the zfs_ioc_func_t, to
61 * determine if this operation is permitted. It should return EPERM
62 * on failure, and 0 on success. Checks include determining if the
63 * dataset is visible in this zone, and if the user has either all
64 * zfs privileges in the zone (SYS_MOUNT), or has been granted permission
65 * to do this operation on this dataset with "zfs allow".
67 * zfs_ioc_namecheck_t namecheck
68 * This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
69 * name, a dataset name, or nothing. If the name is not well-formed,
70 * the ioctl will fail and the callback will not be called.
71 * Therefore, the callback can assume that the name is well-formed
72 * (e.g. is null-terminated, doesn't have more than one '@' character,
73 * doesn't have invalid characters).
75 * zfs_ioc_poolcheck_t pool_check
76 * This specifies requirements on the pool state. If the pool does
77 * not meet them (is suspended or is readonly), the ioctl will fail
78 * and the callback will not be called. If any checks are specified
79 * (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
80 * Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
81 * POOL_CHECK_READONLY).
83 * boolean_t smush_outnvlist
84 * If smush_outnvlist is true, then the output is presumed to be a
85 * list of errors, and it will be "smushed" down to fit into the
86 * caller's buffer, by removing some entries and replacing them with a
87 * single "N_MORE_ERRORS" entry indicating how many were removed. See
88 * nvlist_smush() for details. If smush_outnvlist is false, and the
89 * outnvlist does not fit into the userland-provided buffer, then the
90 * ioctl will fail with ENOMEM.
92 * zfs_ioc_func_t *func
93 * The callback function that will perform the operation.
95 * The callback should return 0 on success, or an error number on
96 * failure. If the function fails, the userland ioctl will return -1,
97 * and errno will be set to the callback's return value. The callback
98 * will be called with the following arguments:
101 * The name of the pool or dataset to operate on, from
102 * zfs_cmd_t:zc_name. The 'namecheck' argument specifies the
103 * expected type (pool, dataset, or none).
106 * The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src. Or
107 * NULL if no input nvlist was provided. Changes to this nvlist are
108 * ignored. If the input nvlist could not be deserialized, the
109 * ioctl will fail and the callback will not be called.
112 * The output nvlist, initially empty. The callback can fill it in,
113 * and it will be returned to userland by serializing it into
114 * zfs_cmd_t:zc_nvlist_dst. If it is non-empty, and serialization
115 * fails (e.g. because the caller didn't supply a large enough
116 * buffer), then the overall ioctl will fail. See the
117 * 'smush_nvlist' argument above for additional behaviors.
119 * There are two typical uses of the output nvlist:
120 * - To return state, e.g. property values. In this case,
121 * smush_outnvlist should be false. If the buffer was not large
122 * enough, the caller will reallocate a larger buffer and try
125 * - To return multiple errors from an ioctl which makes on-disk
126 * changes. In this case, smush_outnvlist should be true.
127 * Ioctls which make on-disk modifications should generally not
128 * use the outnvl if they succeed, because the caller can not
129 * distinguish between the operation failing, and
130 * deserialization failing.
133 #include <sys/types.h>
134 #include <sys/param.h>
135 #include <sys/errno.h>
138 #include <sys/modctl.h>
139 #include <sys/open.h>
140 #include <sys/file.h>
141 #include <sys/kmem.h>
142 #include <sys/conf.h>
143 #include <sys/cmn_err.h>
144 #include <sys/stat.h>
145 #include <sys/zfs_ioctl.h>
146 #include <sys/zfs_vfsops.h>
147 #include <sys/zfs_znode.h>
150 #include <sys/spa_impl.h>
151 #include <sys/vdev.h>
152 #include <sys/priv_impl.h>
154 #include <sys/dsl_dir.h>
155 #include <sys/dsl_dataset.h>
156 #include <sys/dsl_prop.h>
157 #include <sys/dsl_deleg.h>
158 #include <sys/dmu_objset.h>
159 #include <sys/dmu_impl.h>
160 #include <sys/dmu_tx.h>
162 #include <sys/sunddi.h>
163 #include <sys/sunldi.h>
164 #include <sys/policy.h>
165 #include <sys/zone.h>
166 #include <sys/nvpair.h>
167 #include <sys/pathname.h>
168 #include <sys/mount.h>
170 #include <sys/fs/zfs.h>
171 #include <sys/zfs_ctldir.h>
172 #include <sys/zfs_dir.h>
173 #include <sys/zfs_onexit.h>
174 #include <sys/zvol.h>
175 #include <sys/dsl_scan.h>
176 #include <sharefs/share.h>
177 #include <sys/dmu_objset.h>
178 #include <sys/dmu_send.h>
179 #include <sys/dsl_destroy.h>
180 #include <sys/dsl_bookmark.h>
181 #include <sys/dsl_userhold.h>
182 #include <sys/zfeature.h>
184 #include "zfs_namecheck.h"
185 #include "zfs_prop.h"
186 #include "zfs_deleg.h"
187 #include "zfs_comutil.h"
189 extern struct modlfs zfs_modlfs
;
191 extern void zfs_init(void);
192 extern void zfs_fini(void);
194 ldi_ident_t zfs_li
= NULL
;
197 uint_t zfs_fsyncer_key
;
198 extern uint_t rrw_tsd_key
;
199 static uint_t zfs_allow_log_key
;
201 typedef int zfs_ioc_legacy_func_t(zfs_cmd_t
*);
202 typedef int zfs_ioc_func_t(const char *, nvlist_t
*, nvlist_t
*);
203 typedef int zfs_secpolicy_func_t(zfs_cmd_t
*, nvlist_t
*, cred_t
*);
209 } zfs_ioc_namecheck_t
;
212 POOL_CHECK_NONE
= 1 << 0,
213 POOL_CHECK_SUSPENDED
= 1 << 1,
214 POOL_CHECK_READONLY
= 1 << 2,
215 } zfs_ioc_poolcheck_t
;
217 typedef struct zfs_ioc_vec
{
218 zfs_ioc_legacy_func_t
*zvec_legacy_func
;
219 zfs_ioc_func_t
*zvec_func
;
220 zfs_secpolicy_func_t
*zvec_secpolicy
;
221 zfs_ioc_namecheck_t zvec_namecheck
;
222 boolean_t zvec_allow_log
;
223 zfs_ioc_poolcheck_t zvec_pool_check
;
224 boolean_t zvec_smush_outnvlist
;
225 const char *zvec_name
;
228 /* This array is indexed by zfs_userquota_prop_t */
229 static const char *userquota_perms
[] = {
230 ZFS_DELEG_PERM_USERUSED
,
231 ZFS_DELEG_PERM_USERQUOTA
,
232 ZFS_DELEG_PERM_GROUPUSED
,
233 ZFS_DELEG_PERM_GROUPQUOTA
,
236 static int zfs_ioc_userspace_upgrade(zfs_cmd_t
*zc
);
237 static int zfs_check_settable(const char *name
, nvpair_t
*property
,
239 static int zfs_check_clearable(char *dataset
, nvlist_t
*props
,
241 static int zfs_fill_zplprops_root(uint64_t, nvlist_t
*, nvlist_t
*,
243 int zfs_set_prop_nvlist(const char *, zprop_source_t
, nvlist_t
*, nvlist_t
*);
244 static int get_nvlist(uint64_t nvl
, uint64_t size
, int iflag
, nvlist_t
**nvp
);
246 static int zfs_prop_activate_feature(spa_t
*spa
, spa_feature_t feature
);
248 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
250 __dprintf(const char *file
, const char *func
, int line
, const char *fmt
, ...)
257 * Get rid of annoying "../common/" prefix to filename.
259 newfile
= strrchr(file
, '/');
260 if (newfile
!= NULL
) {
261 newfile
= newfile
+ 1; /* Get rid of leading / */
267 (void) vsnprintf(buf
, sizeof (buf
), fmt
, adx
);
271 * To get this data, use the zfs-dprintf probe as so:
272 * dtrace -q -n 'zfs-dprintf \
273 * /stringof(arg0) == "dbuf.c"/ \
274 * {printf("%s: %s", stringof(arg1), stringof(arg3))}'
276 * arg1 = function name
280 DTRACE_PROBE4(zfs__dprintf
,
281 char *, newfile
, char *, func
, int, line
, char *, buf
);
285 history_str_free(char *buf
)
287 kmem_free(buf
, HIS_MAX_RECORD_LEN
);
291 history_str_get(zfs_cmd_t
*zc
)
295 if (zc
->zc_history
== NULL
)
298 buf
= kmem_alloc(HIS_MAX_RECORD_LEN
, KM_SLEEP
);
299 if (copyinstr((void *)(uintptr_t)zc
->zc_history
,
300 buf
, HIS_MAX_RECORD_LEN
, NULL
) != 0) {
301 history_str_free(buf
);
305 buf
[HIS_MAX_RECORD_LEN
-1] = '\0';
311 * Check to see if the named dataset is currently defined as bootable
314 zfs_is_bootfs(const char *name
)
318 if (dmu_objset_hold(name
, FTAG
, &os
) == 0) {
320 ret
= (dmu_objset_id(os
) == spa_bootfs(dmu_objset_spa(os
)));
321 dmu_objset_rele(os
, FTAG
);
328 * Return non-zero if the spa version is less than requested version.
331 zfs_earlier_version(const char *name
, int version
)
335 if (spa_open(name
, &spa
, FTAG
) == 0) {
336 if (spa_version(spa
) < version
) {
337 spa_close(spa
, FTAG
);
340 spa_close(spa
, FTAG
);
346 * Return TRUE if the ZPL version is less than requested version.
349 zpl_earlier_version(const char *name
, int version
)
352 boolean_t rc
= B_TRUE
;
354 if (dmu_objset_hold(name
, FTAG
, &os
) == 0) {
357 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
358 dmu_objset_rele(os
, FTAG
);
361 /* XXX reading from non-owned objset */
362 if (zfs_get_zplprop(os
, ZFS_PROP_VERSION
, &zplversion
) == 0)
363 rc
= zplversion
< version
;
364 dmu_objset_rele(os
, FTAG
);
370 zfs_log_history(zfs_cmd_t
*zc
)
375 if ((buf
= history_str_get(zc
)) == NULL
)
378 if (spa_open(zc
->zc_name
, &spa
, FTAG
) == 0) {
379 if (spa_version(spa
) >= SPA_VERSION_ZPOOL_HISTORY
)
380 (void) spa_history_log(spa
, buf
);
381 spa_close(spa
, FTAG
);
383 history_str_free(buf
);
387 * Policy for top-level read operations (list pools). Requires no privileges,
388 * and can be used in the local zone, as there is no associated dataset.
392 zfs_secpolicy_none(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
398 * Policy for dataset read operations (list children, get statistics). Requires
399 * no privileges, but must be visible in the local zone.
403 zfs_secpolicy_read(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
405 if (INGLOBALZONE(curproc
) ||
406 zone_dataset_visible(zc
->zc_name
, NULL
))
409 return (SET_ERROR(ENOENT
));
413 zfs_dozonecheck_impl(const char *dataset
, uint64_t zoned
, cred_t
*cr
)
418 * The dataset must be visible by this zone -- check this first
419 * so they don't see EPERM on something they shouldn't know about.
421 if (!INGLOBALZONE(curproc
) &&
422 !zone_dataset_visible(dataset
, &writable
))
423 return (SET_ERROR(ENOENT
));
425 if (INGLOBALZONE(curproc
)) {
427 * If the fs is zoned, only root can access it from the
430 if (secpolicy_zfs(cr
) && zoned
)
431 return (SET_ERROR(EPERM
));
434 * If we are in a local zone, the 'zoned' property must be set.
437 return (SET_ERROR(EPERM
));
439 /* must be writable by this zone */
441 return (SET_ERROR(EPERM
));
447 zfs_dozonecheck(const char *dataset
, cred_t
*cr
)
451 if (dsl_prop_get_integer(dataset
, "zoned", &zoned
, NULL
))
452 return (SET_ERROR(ENOENT
));
454 return (zfs_dozonecheck_impl(dataset
, zoned
, cr
));
458 zfs_dozonecheck_ds(const char *dataset
, dsl_dataset_t
*ds
, cred_t
*cr
)
462 if (dsl_prop_get_int_ds(ds
, "zoned", &zoned
))
463 return (SET_ERROR(ENOENT
));
465 return (zfs_dozonecheck_impl(dataset
, zoned
, cr
));
469 zfs_secpolicy_write_perms_ds(const char *name
, dsl_dataset_t
*ds
,
470 const char *perm
, cred_t
*cr
)
474 error
= zfs_dozonecheck_ds(name
, ds
, cr
);
476 error
= secpolicy_zfs(cr
);
478 error
= dsl_deleg_access_impl(ds
, perm
, cr
);
484 zfs_secpolicy_write_perms(const char *name
, const char *perm
, cred_t
*cr
)
490 error
= dsl_pool_hold(name
, FTAG
, &dp
);
494 error
= dsl_dataset_hold(dp
, name
, FTAG
, &ds
);
496 dsl_pool_rele(dp
, FTAG
);
500 error
= zfs_secpolicy_write_perms_ds(name
, ds
, perm
, cr
);
502 dsl_dataset_rele(ds
, FTAG
);
503 dsl_pool_rele(dp
, FTAG
);
508 * Policy for setting the security label property.
510 * Returns 0 for success, non-zero for access and other errors.
513 zfs_set_slabel_policy(const char *name
, char *strval
, cred_t
*cr
)
515 char ds_hexsl
[MAXNAMELEN
];
516 bslabel_t ds_sl
, new_sl
;
517 boolean_t new_default
= FALSE
;
519 int needed_priv
= -1;
522 /* First get the existing dataset label. */
523 error
= dsl_prop_get(name
, zfs_prop_to_name(ZFS_PROP_MLSLABEL
),
524 1, sizeof (ds_hexsl
), &ds_hexsl
, NULL
);
526 return (SET_ERROR(EPERM
));
528 if (strcasecmp(strval
, ZFS_MLSLABEL_DEFAULT
) == 0)
531 /* The label must be translatable */
532 if (!new_default
&& (hexstr_to_label(strval
, &new_sl
) != 0))
533 return (SET_ERROR(EINVAL
));
536 * In a non-global zone, disallow attempts to set a label that
537 * doesn't match that of the zone; otherwise no other checks
540 if (!INGLOBALZONE(curproc
)) {
541 if (new_default
|| !blequal(&new_sl
, CR_SL(CRED())))
542 return (SET_ERROR(EPERM
));
547 * For global-zone datasets (i.e., those whose zoned property is
548 * "off", verify that the specified new label is valid for the
551 if (dsl_prop_get_integer(name
,
552 zfs_prop_to_name(ZFS_PROP_ZONED
), &zoned
, NULL
))
553 return (SET_ERROR(EPERM
));
555 if (zfs_check_global_label(name
, strval
) != 0)
556 return (SET_ERROR(EPERM
));
560 * If the existing dataset label is nondefault, check if the
561 * dataset is mounted (label cannot be changed while mounted).
562 * Get the zfsvfs; if there isn't one, then the dataset isn't
563 * mounted (or isn't a dataset, doesn't exist, ...).
565 if (strcasecmp(ds_hexsl
, ZFS_MLSLABEL_DEFAULT
) != 0) {
567 static char *setsl_tag
= "setsl_tag";
570 * Try to own the dataset; abort if there is any error,
571 * (e.g., already mounted, in use, or other error).
573 error
= dmu_objset_own(name
, DMU_OST_ZFS
, B_TRUE
,
576 return (SET_ERROR(EPERM
));
578 dmu_objset_disown(os
, setsl_tag
);
581 needed_priv
= PRIV_FILE_DOWNGRADE_SL
;
585 if (hexstr_to_label(strval
, &new_sl
) != 0)
586 return (SET_ERROR(EPERM
));
588 if (blstrictdom(&ds_sl
, &new_sl
))
589 needed_priv
= PRIV_FILE_DOWNGRADE_SL
;
590 else if (blstrictdom(&new_sl
, &ds_sl
))
591 needed_priv
= PRIV_FILE_UPGRADE_SL
;
593 /* dataset currently has a default label */
595 needed_priv
= PRIV_FILE_UPGRADE_SL
;
599 if (needed_priv
!= -1)
600 return (PRIV_POLICY(cr
, needed_priv
, B_FALSE
, EPERM
, NULL
));
605 zfs_secpolicy_setprop(const char *dsname
, zfs_prop_t prop
, nvpair_t
*propval
,
611 * Check permissions for special properties.
616 * Disallow setting of 'zoned' from within a local zone.
618 if (!INGLOBALZONE(curproc
))
619 return (SET_ERROR(EPERM
));
623 case ZFS_PROP_FILESYSTEM_LIMIT
:
624 case ZFS_PROP_SNAPSHOT_LIMIT
:
625 if (!INGLOBALZONE(curproc
)) {
627 char setpoint
[MAXNAMELEN
];
629 * Unprivileged users are allowed to modify the
630 * limit on things *under* (ie. contained by)
631 * the thing they own.
633 if (dsl_prop_get_integer(dsname
, "zoned", &zoned
,
635 return (SET_ERROR(EPERM
));
636 if (!zoned
|| strlen(dsname
) <= strlen(setpoint
))
637 return (SET_ERROR(EPERM
));
641 case ZFS_PROP_MLSLABEL
:
642 if (!is_system_labeled())
643 return (SET_ERROR(EPERM
));
645 if (nvpair_value_string(propval
, &strval
) == 0) {
648 err
= zfs_set_slabel_policy(dsname
, strval
, CRED());
655 return (zfs_secpolicy_write_perms(dsname
, zfs_prop_to_name(prop
), cr
));
660 zfs_secpolicy_set_fsacl(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
664 error
= zfs_dozonecheck(zc
->zc_name
, cr
);
669 * permission to set permissions will be evaluated later in
670 * dsl_deleg_can_allow()
677 zfs_secpolicy_rollback(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
679 return (zfs_secpolicy_write_perms(zc
->zc_name
,
680 ZFS_DELEG_PERM_ROLLBACK
, cr
));
685 zfs_secpolicy_send(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
693 * Generate the current snapshot name from the given objsetid, then
694 * use that name for the secpolicy/zone checks.
696 cp
= strchr(zc
->zc_name
, '@');
698 return (SET_ERROR(EINVAL
));
699 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
703 error
= dsl_dataset_hold_obj(dp
, zc
->zc_sendobj
, FTAG
, &ds
);
705 dsl_pool_rele(dp
, FTAG
);
709 dsl_dataset_name(ds
, zc
->zc_name
);
711 error
= zfs_secpolicy_write_perms_ds(zc
->zc_name
, ds
,
712 ZFS_DELEG_PERM_SEND
, cr
);
713 dsl_dataset_rele(ds
, FTAG
);
714 dsl_pool_rele(dp
, FTAG
);
721 zfs_secpolicy_send_new(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
723 return (zfs_secpolicy_write_perms(zc
->zc_name
,
724 ZFS_DELEG_PERM_SEND
, cr
));
729 zfs_secpolicy_deleg_share(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
734 if ((error
= lookupname(zc
->zc_value
, UIO_SYSSPACE
,
735 NO_FOLLOW
, NULL
, &vp
)) != 0)
738 /* Now make sure mntpnt and dataset are ZFS */
740 if (vp
->v_vfsp
->vfs_fstype
!= zfsfstype
||
741 (strcmp((char *)refstr_value(vp
->v_vfsp
->vfs_resource
),
742 zc
->zc_name
) != 0)) {
744 return (SET_ERROR(EPERM
));
748 return (dsl_deleg_access(zc
->zc_name
,
749 ZFS_DELEG_PERM_SHARE
, cr
));
753 zfs_secpolicy_share(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
755 if (!INGLOBALZONE(curproc
))
756 return (SET_ERROR(EPERM
));
758 if (secpolicy_nfs(cr
) == 0) {
761 return (zfs_secpolicy_deleg_share(zc
, innvl
, cr
));
766 zfs_secpolicy_smb_acl(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
768 if (!INGLOBALZONE(curproc
))
769 return (SET_ERROR(EPERM
));
771 if (secpolicy_smb(cr
) == 0) {
774 return (zfs_secpolicy_deleg_share(zc
, innvl
, cr
));
779 zfs_get_parent(const char *datasetname
, char *parent
, int parentsize
)
784 * Remove the @bla or /bla from the end of the name to get the parent.
786 (void) strncpy(parent
, datasetname
, parentsize
);
787 cp
= strrchr(parent
, '@');
791 cp
= strrchr(parent
, '/');
793 return (SET_ERROR(ENOENT
));
801 zfs_secpolicy_destroy_perms(const char *name
, cred_t
*cr
)
805 if ((error
= zfs_secpolicy_write_perms(name
,
806 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
809 return (zfs_secpolicy_write_perms(name
, ZFS_DELEG_PERM_DESTROY
, cr
));
814 zfs_secpolicy_destroy(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
816 return (zfs_secpolicy_destroy_perms(zc
->zc_name
, cr
));
820 * Destroying snapshots with delegated permissions requires
821 * descendant mount and destroy permissions.
825 zfs_secpolicy_destroy_snaps(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
828 nvpair_t
*pair
, *nextpair
;
831 if (nvlist_lookup_nvlist(innvl
, "snaps", &snaps
) != 0)
832 return (SET_ERROR(EINVAL
));
833 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
835 nextpair
= nvlist_next_nvpair(snaps
, pair
);
836 error
= zfs_secpolicy_destroy_perms(nvpair_name(pair
), cr
);
837 if (error
== ENOENT
) {
839 * Ignore any snapshots that don't exist (we consider
840 * them "already destroyed"). Remove the name from the
841 * nvl here in case the snapshot is created between
842 * now and when we try to destroy it (in which case
843 * we don't want to destroy it since we haven't
844 * checked for permission).
846 fnvlist_remove_nvpair(snaps
, pair
);
857 zfs_secpolicy_rename_perms(const char *from
, const char *to
, cred_t
*cr
)
859 char parentname
[MAXNAMELEN
];
862 if ((error
= zfs_secpolicy_write_perms(from
,
863 ZFS_DELEG_PERM_RENAME
, cr
)) != 0)
866 if ((error
= zfs_secpolicy_write_perms(from
,
867 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
870 if ((error
= zfs_get_parent(to
, parentname
,
871 sizeof (parentname
))) != 0)
874 if ((error
= zfs_secpolicy_write_perms(parentname
,
875 ZFS_DELEG_PERM_CREATE
, cr
)) != 0)
878 if ((error
= zfs_secpolicy_write_perms(parentname
,
879 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
887 zfs_secpolicy_rename(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
889 return (zfs_secpolicy_rename_perms(zc
->zc_name
, zc
->zc_value
, cr
));
894 zfs_secpolicy_promote(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
897 dsl_dataset_t
*clone
;
900 error
= zfs_secpolicy_write_perms(zc
->zc_name
,
901 ZFS_DELEG_PERM_PROMOTE
, cr
);
905 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
909 error
= dsl_dataset_hold(dp
, zc
->zc_name
, FTAG
, &clone
);
912 char parentname
[MAXNAMELEN
];
913 dsl_dataset_t
*origin
= NULL
;
917 error
= dsl_dataset_hold_obj(dd
->dd_pool
,
918 dd
->dd_phys
->dd_origin_obj
, FTAG
, &origin
);
920 dsl_dataset_rele(clone
, FTAG
);
921 dsl_pool_rele(dp
, FTAG
);
925 error
= zfs_secpolicy_write_perms_ds(zc
->zc_name
, clone
,
926 ZFS_DELEG_PERM_MOUNT
, cr
);
928 dsl_dataset_name(origin
, parentname
);
930 error
= zfs_secpolicy_write_perms_ds(parentname
, origin
,
931 ZFS_DELEG_PERM_PROMOTE
, cr
);
933 dsl_dataset_rele(clone
, FTAG
);
934 dsl_dataset_rele(origin
, FTAG
);
936 dsl_pool_rele(dp
, FTAG
);
942 zfs_secpolicy_recv(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
946 if ((error
= zfs_secpolicy_write_perms(zc
->zc_name
,
947 ZFS_DELEG_PERM_RECEIVE
, cr
)) != 0)
950 if ((error
= zfs_secpolicy_write_perms(zc
->zc_name
,
951 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
954 return (zfs_secpolicy_write_perms(zc
->zc_name
,
955 ZFS_DELEG_PERM_CREATE
, cr
));
959 zfs_secpolicy_snapshot_perms(const char *name
, cred_t
*cr
)
961 return (zfs_secpolicy_write_perms(name
,
962 ZFS_DELEG_PERM_SNAPSHOT
, cr
));
966 * Check for permission to create each snapshot in the nvlist.
970 zfs_secpolicy_snapshot(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
976 if (nvlist_lookup_nvlist(innvl
, "snaps", &snaps
) != 0)
977 return (SET_ERROR(EINVAL
));
978 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
979 pair
= nvlist_next_nvpair(snaps
, pair
)) {
980 char *name
= nvpair_name(pair
);
981 char *atp
= strchr(name
, '@');
984 error
= SET_ERROR(EINVAL
);
988 error
= zfs_secpolicy_snapshot_perms(name
, cr
);
997 * Check for permission to create each snapshot in the nvlist.
1001 zfs_secpolicy_bookmark(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1005 for (nvpair_t
*pair
= nvlist_next_nvpair(innvl
, NULL
);
1006 pair
!= NULL
; pair
= nvlist_next_nvpair(innvl
, pair
)) {
1007 char *name
= nvpair_name(pair
);
1008 char *hashp
= strchr(name
, '#');
1010 if (hashp
== NULL
) {
1011 error
= SET_ERROR(EINVAL
);
1015 error
= zfs_secpolicy_write_perms(name
,
1016 ZFS_DELEG_PERM_BOOKMARK
, cr
);
1026 zfs_secpolicy_destroy_bookmarks(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1028 nvpair_t
*pair
, *nextpair
;
1031 for (pair
= nvlist_next_nvpair(innvl
, NULL
); pair
!= NULL
;
1033 char *name
= nvpair_name(pair
);
1034 char *hashp
= strchr(name
, '#');
1035 nextpair
= nvlist_next_nvpair(innvl
, pair
);
1037 if (hashp
== NULL
) {
1038 error
= SET_ERROR(EINVAL
);
1043 error
= zfs_secpolicy_write_perms(name
,
1044 ZFS_DELEG_PERM_DESTROY
, cr
);
1046 if (error
== ENOENT
) {
1048 * Ignore any filesystems that don't exist (we consider
1049 * their bookmarks "already destroyed"). Remove
1050 * the name from the nvl here in case the filesystem
1051 * is created between now and when we try to destroy
1052 * the bookmark (in which case we don't want to
1053 * destroy it since we haven't checked for permission).
1055 fnvlist_remove_nvpair(innvl
, pair
);
1067 zfs_secpolicy_log_history(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1070 * Even root must have a proper TSD so that we know what pool
1073 if (tsd_get(zfs_allow_log_key
) == NULL
)
1074 return (SET_ERROR(EPERM
));
1079 zfs_secpolicy_create_clone(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1081 char parentname
[MAXNAMELEN
];
1085 if ((error
= zfs_get_parent(zc
->zc_name
, parentname
,
1086 sizeof (parentname
))) != 0)
1089 if (nvlist_lookup_string(innvl
, "origin", &origin
) == 0 &&
1090 (error
= zfs_secpolicy_write_perms(origin
,
1091 ZFS_DELEG_PERM_CLONE
, cr
)) != 0)
1094 if ((error
= zfs_secpolicy_write_perms(parentname
,
1095 ZFS_DELEG_PERM_CREATE
, cr
)) != 0)
1098 return (zfs_secpolicy_write_perms(parentname
,
1099 ZFS_DELEG_PERM_MOUNT
, cr
));
1103 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
1104 * SYS_CONFIG privilege, which is not available in a local zone.
1108 zfs_secpolicy_config(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1110 if (secpolicy_sys_config(cr
, B_FALSE
) != 0)
1111 return (SET_ERROR(EPERM
));
1117 * Policy for object to name lookups.
1121 zfs_secpolicy_diff(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1125 if ((error
= secpolicy_sys_config(cr
, B_FALSE
)) == 0)
1128 error
= zfs_secpolicy_write_perms(zc
->zc_name
, ZFS_DELEG_PERM_DIFF
, cr
);
1133 * Policy for fault injection. Requires all privileges.
1137 zfs_secpolicy_inject(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1139 return (secpolicy_zinject(cr
));
1144 zfs_secpolicy_inherit_prop(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1146 zfs_prop_t prop
= zfs_name_to_prop(zc
->zc_value
);
1148 if (prop
== ZPROP_INVAL
) {
1149 if (!zfs_prop_user(zc
->zc_value
))
1150 return (SET_ERROR(EINVAL
));
1151 return (zfs_secpolicy_write_perms(zc
->zc_name
,
1152 ZFS_DELEG_PERM_USERPROP
, cr
));
1154 return (zfs_secpolicy_setprop(zc
->zc_name
, prop
,
1160 zfs_secpolicy_userspace_one(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1162 int err
= zfs_secpolicy_read(zc
, innvl
, cr
);
1166 if (zc
->zc_objset_type
>= ZFS_NUM_USERQUOTA_PROPS
)
1167 return (SET_ERROR(EINVAL
));
1169 if (zc
->zc_value
[0] == 0) {
1171 * They are asking about a posix uid/gid. If it's
1172 * themself, allow it.
1174 if (zc
->zc_objset_type
== ZFS_PROP_USERUSED
||
1175 zc
->zc_objset_type
== ZFS_PROP_USERQUOTA
) {
1176 if (zc
->zc_guid
== crgetuid(cr
))
1179 if (groupmember(zc
->zc_guid
, cr
))
1184 return (zfs_secpolicy_write_perms(zc
->zc_name
,
1185 userquota_perms
[zc
->zc_objset_type
], cr
));
1189 zfs_secpolicy_userspace_many(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1191 int err
= zfs_secpolicy_read(zc
, innvl
, cr
);
1195 if (zc
->zc_objset_type
>= ZFS_NUM_USERQUOTA_PROPS
)
1196 return (SET_ERROR(EINVAL
));
1198 return (zfs_secpolicy_write_perms(zc
->zc_name
,
1199 userquota_perms
[zc
->zc_objset_type
], cr
));
1204 zfs_secpolicy_userspace_upgrade(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1206 return (zfs_secpolicy_setprop(zc
->zc_name
, ZFS_PROP_VERSION
,
1212 zfs_secpolicy_hold(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1218 error
= nvlist_lookup_nvlist(innvl
, "holds", &holds
);
1220 return (SET_ERROR(EINVAL
));
1222 for (pair
= nvlist_next_nvpair(holds
, NULL
); pair
!= NULL
;
1223 pair
= nvlist_next_nvpair(holds
, pair
)) {
1224 char fsname
[MAXNAMELEN
];
1225 error
= dmu_fsname(nvpair_name(pair
), fsname
);
1228 error
= zfs_secpolicy_write_perms(fsname
,
1229 ZFS_DELEG_PERM_HOLD
, cr
);
1238 zfs_secpolicy_release(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1243 for (pair
= nvlist_next_nvpair(innvl
, NULL
); pair
!= NULL
;
1244 pair
= nvlist_next_nvpair(innvl
, pair
)) {
1245 char fsname
[MAXNAMELEN
];
1246 error
= dmu_fsname(nvpair_name(pair
), fsname
);
1249 error
= zfs_secpolicy_write_perms(fsname
,
1250 ZFS_DELEG_PERM_RELEASE
, cr
);
1258 * Policy for allowing temporary snapshots to be taken or released
1261 zfs_secpolicy_tmp_snapshot(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1264 * A temporary snapshot is the same as a snapshot,
1265 * hold, destroy and release all rolled into one.
1266 * Delegated diff alone is sufficient that we allow this.
1270 if ((error
= zfs_secpolicy_write_perms(zc
->zc_name
,
1271 ZFS_DELEG_PERM_DIFF
, cr
)) == 0)
1274 error
= zfs_secpolicy_snapshot_perms(zc
->zc_name
, cr
);
1276 error
= zfs_secpolicy_hold(zc
, innvl
, cr
);
1278 error
= zfs_secpolicy_release(zc
, innvl
, cr
);
1280 error
= zfs_secpolicy_destroy(zc
, innvl
, cr
);
1285 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1288 get_nvlist(uint64_t nvl
, uint64_t size
, int iflag
, nvlist_t
**nvp
)
1292 nvlist_t
*list
= NULL
;
1295 * Read in and unpack the user-supplied nvlist.
1298 return (SET_ERROR(EINVAL
));
1300 packed
= kmem_alloc(size
, KM_SLEEP
);
1302 if ((error
= ddi_copyin((void *)(uintptr_t)nvl
, packed
, size
,
1304 kmem_free(packed
, size
);
1308 if ((error
= nvlist_unpack(packed
, size
, &list
, 0)) != 0) {
1309 kmem_free(packed
, size
);
1313 kmem_free(packed
, size
);
1320 * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
1321 * Entries will be removed from the end of the nvlist, and one int32 entry
1322 * named "N_MORE_ERRORS" will be added indicating how many entries were
1326 nvlist_smush(nvlist_t
*errors
, size_t max
)
1330 size
= fnvlist_size(errors
);
1333 nvpair_t
*more_errors
;
1337 return (SET_ERROR(ENOMEM
));
1339 fnvlist_add_int32(errors
, ZPROP_N_MORE_ERRORS
, 0);
1340 more_errors
= nvlist_prev_nvpair(errors
, NULL
);
1343 nvpair_t
*pair
= nvlist_prev_nvpair(errors
,
1345 fnvlist_remove_nvpair(errors
, pair
);
1347 size
= fnvlist_size(errors
);
1348 } while (size
> max
);
1350 fnvlist_remove_nvpair(errors
, more_errors
);
1351 fnvlist_add_int32(errors
, ZPROP_N_MORE_ERRORS
, n
);
1352 ASSERT3U(fnvlist_size(errors
), <=, max
);
1359 put_nvlist(zfs_cmd_t
*zc
, nvlist_t
*nvl
)
1361 char *packed
= NULL
;
1365 size
= fnvlist_size(nvl
);
1367 if (size
> zc
->zc_nvlist_dst_size
) {
1368 error
= SET_ERROR(ENOMEM
);
1370 packed
= fnvlist_pack(nvl
, &size
);
1371 if (ddi_copyout(packed
, (void *)(uintptr_t)zc
->zc_nvlist_dst
,
1372 size
, zc
->zc_iflags
) != 0)
1373 error
= SET_ERROR(EFAULT
);
1374 fnvlist_pack_free(packed
, size
);
1377 zc
->zc_nvlist_dst_size
= size
;
1378 zc
->zc_nvlist_dst_filled
= B_TRUE
;
1383 getzfsvfs(const char *dsname
, zfsvfs_t
**zfvp
)
1388 error
= dmu_objset_hold(dsname
, FTAG
, &os
);
1391 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
1392 dmu_objset_rele(os
, FTAG
);
1393 return (SET_ERROR(EINVAL
));
1396 mutex_enter(&os
->os_user_ptr_lock
);
1397 *zfvp
= dmu_objset_get_user(os
);
1399 VFS_HOLD((*zfvp
)->z_vfs
);
1401 error
= SET_ERROR(ESRCH
);
1403 mutex_exit(&os
->os_user_ptr_lock
);
1404 dmu_objset_rele(os
, FTAG
);
1409 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
1410 * case its z_vfs will be NULL, and it will be opened as the owner.
1411 * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1412 * which prevents all vnode ops from running.
1415 zfsvfs_hold(const char *name
, void *tag
, zfsvfs_t
**zfvp
, boolean_t writer
)
1419 if (getzfsvfs(name
, zfvp
) != 0)
1420 error
= zfsvfs_create(name
, zfvp
);
1422 rrw_enter(&(*zfvp
)->z_teardown_lock
, (writer
) ? RW_WRITER
:
1424 if ((*zfvp
)->z_unmounted
) {
1426 * XXX we could probably try again, since the unmounting
1427 * thread should be just about to disassociate the
1428 * objset from the zfsvfs.
1430 rrw_exit(&(*zfvp
)->z_teardown_lock
, tag
);
1431 return (SET_ERROR(EBUSY
));
1438 zfsvfs_rele(zfsvfs_t
*zfsvfs
, void *tag
)
1440 rrw_exit(&zfsvfs
->z_teardown_lock
, tag
);
1442 if (zfsvfs
->z_vfs
) {
1443 VFS_RELE(zfsvfs
->z_vfs
);
1445 dmu_objset_disown(zfsvfs
->z_os
, zfsvfs
);
1446 zfsvfs_free(zfsvfs
);
1451 zfs_ioc_pool_create(zfs_cmd_t
*zc
)
1454 nvlist_t
*config
, *props
= NULL
;
1455 nvlist_t
*rootprops
= NULL
;
1456 nvlist_t
*zplprops
= NULL
;
1458 if (error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1459 zc
->zc_iflags
, &config
))
1462 if (zc
->zc_nvlist_src_size
!= 0 && (error
=
1463 get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1464 zc
->zc_iflags
, &props
))) {
1465 nvlist_free(config
);
1470 nvlist_t
*nvl
= NULL
;
1471 uint64_t version
= SPA_VERSION
;
1473 (void) nvlist_lookup_uint64(props
,
1474 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
);
1475 if (!SPA_VERSION_IS_SUPPORTED(version
)) {
1476 error
= SET_ERROR(EINVAL
);
1477 goto pool_props_bad
;
1479 (void) nvlist_lookup_nvlist(props
, ZPOOL_ROOTFS_PROPS
, &nvl
);
1481 error
= nvlist_dup(nvl
, &rootprops
, KM_SLEEP
);
1483 nvlist_free(config
);
1487 (void) nvlist_remove_all(props
, ZPOOL_ROOTFS_PROPS
);
1489 VERIFY(nvlist_alloc(&zplprops
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1490 error
= zfs_fill_zplprops_root(version
, rootprops
,
1493 goto pool_props_bad
;
1496 error
= spa_create(zc
->zc_name
, config
, props
, zplprops
);
1499 * Set the remaining root properties
1501 if (!error
&& (error
= zfs_set_prop_nvlist(zc
->zc_name
,
1502 ZPROP_SRC_LOCAL
, rootprops
, NULL
)) != 0)
1503 (void) spa_destroy(zc
->zc_name
);
1506 nvlist_free(rootprops
);
1507 nvlist_free(zplprops
);
1508 nvlist_free(config
);
1515 zfs_ioc_pool_destroy(zfs_cmd_t
*zc
)
1518 zfs_log_history(zc
);
1519 error
= spa_destroy(zc
->zc_name
);
1521 zvol_remove_minors(zc
->zc_name
);
1526 zfs_ioc_pool_import(zfs_cmd_t
*zc
)
1528 nvlist_t
*config
, *props
= NULL
;
1532 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1533 zc
->zc_iflags
, &config
)) != 0)
1536 if (zc
->zc_nvlist_src_size
!= 0 && (error
=
1537 get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1538 zc
->zc_iflags
, &props
))) {
1539 nvlist_free(config
);
1543 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &guid
) != 0 ||
1544 guid
!= zc
->zc_guid
)
1545 error
= SET_ERROR(EINVAL
);
1547 error
= spa_import(zc
->zc_name
, config
, props
, zc
->zc_cookie
);
1549 if (zc
->zc_nvlist_dst
!= 0) {
1552 if ((err
= put_nvlist(zc
, config
)) != 0)
1556 nvlist_free(config
);
1565 zfs_ioc_pool_export(zfs_cmd_t
*zc
)
1568 boolean_t force
= (boolean_t
)zc
->zc_cookie
;
1569 boolean_t hardforce
= (boolean_t
)zc
->zc_guid
;
1571 zfs_log_history(zc
);
1572 error
= spa_export(zc
->zc_name
, NULL
, force
, hardforce
);
1574 zvol_remove_minors(zc
->zc_name
);
1579 zfs_ioc_pool_configs(zfs_cmd_t
*zc
)
1584 if ((configs
= spa_all_configs(&zc
->zc_cookie
)) == NULL
)
1585 return (SET_ERROR(EEXIST
));
1587 error
= put_nvlist(zc
, configs
);
1589 nvlist_free(configs
);
1596 * zc_name name of the pool
1599 * zc_cookie real errno
1600 * zc_nvlist_dst config nvlist
1601 * zc_nvlist_dst_size size of config nvlist
1604 zfs_ioc_pool_stats(zfs_cmd_t
*zc
)
1610 error
= spa_get_stats(zc
->zc_name
, &config
, zc
->zc_value
,
1611 sizeof (zc
->zc_value
));
1613 if (config
!= NULL
) {
1614 ret
= put_nvlist(zc
, config
);
1615 nvlist_free(config
);
1618 * The config may be present even if 'error' is non-zero.
1619 * In this case we return success, and preserve the real errno
1622 zc
->zc_cookie
= error
;
1631 * Try to import the given pool, returning pool stats as appropriate so that
1632 * user land knows which devices are available and overall pool health.
1635 zfs_ioc_pool_tryimport(zfs_cmd_t
*zc
)
1637 nvlist_t
*tryconfig
, *config
;
1640 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1641 zc
->zc_iflags
, &tryconfig
)) != 0)
1644 config
= spa_tryimport(tryconfig
);
1646 nvlist_free(tryconfig
);
1649 return (SET_ERROR(EINVAL
));
1651 error
= put_nvlist(zc
, config
);
1652 nvlist_free(config
);
1659 * zc_name name of the pool
1660 * zc_cookie scan func (pool_scan_func_t)
1663 zfs_ioc_pool_scan(zfs_cmd_t
*zc
)
1668 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1671 if (zc
->zc_cookie
== POOL_SCAN_NONE
)
1672 error
= spa_scan_stop(spa
);
1674 error
= spa_scan(spa
, zc
->zc_cookie
);
1676 spa_close(spa
, FTAG
);
1682 zfs_ioc_pool_freeze(zfs_cmd_t
*zc
)
1687 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1690 spa_close(spa
, FTAG
);
1696 zfs_ioc_pool_upgrade(zfs_cmd_t
*zc
)
1701 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1704 if (zc
->zc_cookie
< spa_version(spa
) ||
1705 !SPA_VERSION_IS_SUPPORTED(zc
->zc_cookie
)) {
1706 spa_close(spa
, FTAG
);
1707 return (SET_ERROR(EINVAL
));
1710 spa_upgrade(spa
, zc
->zc_cookie
);
1711 spa_close(spa
, FTAG
);
1717 zfs_ioc_pool_get_history(zfs_cmd_t
*zc
)
1724 if ((size
= zc
->zc_history_len
) == 0)
1725 return (SET_ERROR(EINVAL
));
1727 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1730 if (spa_version(spa
) < SPA_VERSION_ZPOOL_HISTORY
) {
1731 spa_close(spa
, FTAG
);
1732 return (SET_ERROR(ENOTSUP
));
1735 hist_buf
= kmem_alloc(size
, KM_SLEEP
);
1736 if ((error
= spa_history_get(spa
, &zc
->zc_history_offset
,
1737 &zc
->zc_history_len
, hist_buf
)) == 0) {
1738 error
= ddi_copyout(hist_buf
,
1739 (void *)(uintptr_t)zc
->zc_history
,
1740 zc
->zc_history_len
, zc
->zc_iflags
);
1743 spa_close(spa
, FTAG
);
1744 kmem_free(hist_buf
, size
);
1749 zfs_ioc_pool_reguid(zfs_cmd_t
*zc
)
1754 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1756 error
= spa_change_guid(spa
);
1757 spa_close(spa
, FTAG
);
1763 zfs_ioc_dsobj_to_dsname(zfs_cmd_t
*zc
)
1765 return (dsl_dsobj_to_dsname(zc
->zc_name
, zc
->zc_obj
, zc
->zc_value
));
1770 * zc_name name of filesystem
1771 * zc_obj object to find
1774 * zc_value name of object
1777 zfs_ioc_obj_to_path(zfs_cmd_t
*zc
)
1782 /* XXX reading from objset not owned */
1783 if ((error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
)) != 0)
1785 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
1786 dmu_objset_rele(os
, FTAG
);
1787 return (SET_ERROR(EINVAL
));
1789 error
= zfs_obj_to_path(os
, zc
->zc_obj
, zc
->zc_value
,
1790 sizeof (zc
->zc_value
));
1791 dmu_objset_rele(os
, FTAG
);
1798 * zc_name name of filesystem
1799 * zc_obj object to find
1802 * zc_stat stats on object
1803 * zc_value path to object
1806 zfs_ioc_obj_to_stats(zfs_cmd_t
*zc
)
1811 /* XXX reading from objset not owned */
1812 if ((error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
)) != 0)
1814 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
1815 dmu_objset_rele(os
, FTAG
);
1816 return (SET_ERROR(EINVAL
));
1818 error
= zfs_obj_to_stats(os
, zc
->zc_obj
, &zc
->zc_stat
, zc
->zc_value
,
1819 sizeof (zc
->zc_value
));
1820 dmu_objset_rele(os
, FTAG
);
1826 zfs_ioc_vdev_add(zfs_cmd_t
*zc
)
1830 nvlist_t
*config
, **l2cache
, **spares
;
1831 uint_t nl2cache
= 0, nspares
= 0;
1833 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1837 error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1838 zc
->zc_iflags
, &config
);
1839 (void) nvlist_lookup_nvlist_array(config
, ZPOOL_CONFIG_L2CACHE
,
1840 &l2cache
, &nl2cache
);
1842 (void) nvlist_lookup_nvlist_array(config
, ZPOOL_CONFIG_SPARES
,
1846 * A root pool with concatenated devices is not supported.
1847 * Thus, can not add a device to a root pool.
1849 * Intent log device can not be added to a rootpool because
1850 * during mountroot, zil is replayed, a seperated log device
1851 * can not be accessed during the mountroot time.
1853 * l2cache and spare devices are ok to be added to a rootpool.
1855 if (spa_bootfs(spa
) != 0 && nl2cache
== 0 && nspares
== 0) {
1856 nvlist_free(config
);
1857 spa_close(spa
, FTAG
);
1858 return (SET_ERROR(EDOM
));
1862 error
= spa_vdev_add(spa
, config
);
1863 nvlist_free(config
);
1865 spa_close(spa
, FTAG
);
1871 * zc_name name of the pool
1872 * zc_nvlist_conf nvlist of devices to remove
1873 * zc_cookie to stop the remove?
1876 zfs_ioc_vdev_remove(zfs_cmd_t
*zc
)
1881 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1884 error
= spa_vdev_remove(spa
, zc
->zc_guid
, B_FALSE
);
1885 spa_close(spa
, FTAG
);
1890 zfs_ioc_vdev_set_state(zfs_cmd_t
*zc
)
1894 vdev_state_t newstate
= VDEV_STATE_UNKNOWN
;
1896 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1898 switch (zc
->zc_cookie
) {
1899 case VDEV_STATE_ONLINE
:
1900 error
= vdev_online(spa
, zc
->zc_guid
, zc
->zc_obj
, &newstate
);
1903 case VDEV_STATE_OFFLINE
:
1904 error
= vdev_offline(spa
, zc
->zc_guid
, zc
->zc_obj
);
1907 case VDEV_STATE_FAULTED
:
1908 if (zc
->zc_obj
!= VDEV_AUX_ERR_EXCEEDED
&&
1909 zc
->zc_obj
!= VDEV_AUX_EXTERNAL
)
1910 zc
->zc_obj
= VDEV_AUX_ERR_EXCEEDED
;
1912 error
= vdev_fault(spa
, zc
->zc_guid
, zc
->zc_obj
);
1915 case VDEV_STATE_DEGRADED
:
1916 if (zc
->zc_obj
!= VDEV_AUX_ERR_EXCEEDED
&&
1917 zc
->zc_obj
!= VDEV_AUX_EXTERNAL
)
1918 zc
->zc_obj
= VDEV_AUX_ERR_EXCEEDED
;
1920 error
= vdev_degrade(spa
, zc
->zc_guid
, zc
->zc_obj
);
1924 error
= SET_ERROR(EINVAL
);
1926 zc
->zc_cookie
= newstate
;
1927 spa_close(spa
, FTAG
);
1932 zfs_ioc_vdev_attach(zfs_cmd_t
*zc
)
1935 int replacing
= zc
->zc_cookie
;
1939 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1942 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1943 zc
->zc_iflags
, &config
)) == 0) {
1944 error
= spa_vdev_attach(spa
, zc
->zc_guid
, config
, replacing
);
1945 nvlist_free(config
);
1948 spa_close(spa
, FTAG
);
1953 zfs_ioc_vdev_detach(zfs_cmd_t
*zc
)
1958 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1961 error
= spa_vdev_detach(spa
, zc
->zc_guid
, 0, B_FALSE
);
1963 spa_close(spa
, FTAG
);
1968 zfs_ioc_vdev_split(zfs_cmd_t
*zc
)
1971 nvlist_t
*config
, *props
= NULL
;
1973 boolean_t exp
= !!(zc
->zc_cookie
& ZPOOL_EXPORT_AFTER_SPLIT
);
1975 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1978 if (error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1979 zc
->zc_iflags
, &config
)) {
1980 spa_close(spa
, FTAG
);
1984 if (zc
->zc_nvlist_src_size
!= 0 && (error
=
1985 get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1986 zc
->zc_iflags
, &props
))) {
1987 spa_close(spa
, FTAG
);
1988 nvlist_free(config
);
1992 error
= spa_vdev_split_mirror(spa
, zc
->zc_string
, config
, props
, exp
);
1994 spa_close(spa
, FTAG
);
1996 nvlist_free(config
);
2003 zfs_ioc_vdev_setpath(zfs_cmd_t
*zc
)
2006 char *path
= zc
->zc_value
;
2007 uint64_t guid
= zc
->zc_guid
;
2010 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
2014 error
= spa_vdev_setpath(spa
, guid
, path
);
2015 spa_close(spa
, FTAG
);
2020 zfs_ioc_vdev_setfru(zfs_cmd_t
*zc
)
2023 char *fru
= zc
->zc_value
;
2024 uint64_t guid
= zc
->zc_guid
;
2027 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
2031 error
= spa_vdev_setfru(spa
, guid
, fru
);
2032 spa_close(spa
, FTAG
);
2037 zfs_ioc_objset_stats_impl(zfs_cmd_t
*zc
, objset_t
*os
)
2042 dmu_objset_fast_stat(os
, &zc
->zc_objset_stats
);
2044 if (zc
->zc_nvlist_dst
!= 0 &&
2045 (error
= dsl_prop_get_all(os
, &nv
)) == 0) {
2046 dmu_objset_stats(os
, nv
);
2048 * NB: zvol_get_stats() will read the objset contents,
2049 * which we aren't supposed to do with a
2050 * DS_MODE_USER hold, because it could be
2051 * inconsistent. So this is a bit of a workaround...
2052 * XXX reading with out owning
2054 if (!zc
->zc_objset_stats
.dds_inconsistent
&&
2055 dmu_objset_type(os
) == DMU_OST_ZVOL
) {
2056 error
= zvol_get_stats(os
, nv
);
2061 error
= put_nvlist(zc
, nv
);
2070 * zc_name name of filesystem
2071 * zc_nvlist_dst_size size of buffer for property nvlist
2074 * zc_objset_stats stats
2075 * zc_nvlist_dst property nvlist
2076 * zc_nvlist_dst_size size of property nvlist
2079 zfs_ioc_objset_stats(zfs_cmd_t
*zc
)
2084 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
2086 error
= zfs_ioc_objset_stats_impl(zc
, os
);
2087 dmu_objset_rele(os
, FTAG
);
2095 * zc_name name of filesystem
2096 * zc_nvlist_dst_size size of buffer for property nvlist
2099 * zc_nvlist_dst received property nvlist
2100 * zc_nvlist_dst_size size of received property nvlist
2102 * Gets received properties (distinct from local properties on or after
2103 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
2104 * local property values.
2107 zfs_ioc_objset_recvd_props(zfs_cmd_t
*zc
)
2113 * Without this check, we would return local property values if the
2114 * caller has not already received properties on or after
2115 * SPA_VERSION_RECVD_PROPS.
2117 if (!dsl_prop_get_hasrecvd(zc
->zc_name
))
2118 return (SET_ERROR(ENOTSUP
));
2120 if (zc
->zc_nvlist_dst
!= 0 &&
2121 (error
= dsl_prop_get_received(zc
->zc_name
, &nv
)) == 0) {
2122 error
= put_nvlist(zc
, nv
);
2130 nvl_add_zplprop(objset_t
*os
, nvlist_t
*props
, zfs_prop_t prop
)
2136 * zfs_get_zplprop() will either find a value or give us
2137 * the default value (if there is one).
2139 if ((error
= zfs_get_zplprop(os
, prop
, &value
)) != 0)
2141 VERIFY(nvlist_add_uint64(props
, zfs_prop_to_name(prop
), value
) == 0);
2147 * zc_name name of filesystem
2148 * zc_nvlist_dst_size size of buffer for zpl property nvlist
2151 * zc_nvlist_dst zpl property nvlist
2152 * zc_nvlist_dst_size size of zpl property nvlist
2155 zfs_ioc_objset_zplprops(zfs_cmd_t
*zc
)
2160 /* XXX reading without owning */
2161 if (err
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
))
2164 dmu_objset_fast_stat(os
, &zc
->zc_objset_stats
);
2167 * NB: nvl_add_zplprop() will read the objset contents,
2168 * which we aren't supposed to do with a DS_MODE_USER
2169 * hold, because it could be inconsistent.
2171 if (zc
->zc_nvlist_dst
!= NULL
&&
2172 !zc
->zc_objset_stats
.dds_inconsistent
&&
2173 dmu_objset_type(os
) == DMU_OST_ZFS
) {
2176 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2177 if ((err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_VERSION
)) == 0 &&
2178 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_NORMALIZE
)) == 0 &&
2179 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_UTF8ONLY
)) == 0 &&
2180 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_CASE
)) == 0)
2181 err
= put_nvlist(zc
, nv
);
2184 err
= SET_ERROR(ENOENT
);
2186 dmu_objset_rele(os
, FTAG
);
2191 dataset_name_hidden(const char *name
)
2194 * Skip over datasets that are not visible in this zone,
2195 * internal datasets (which have a $ in their name), and
2196 * temporary datasets (which have a % in their name).
2198 if (strchr(name
, '$') != NULL
)
2200 if (strchr(name
, '%') != NULL
)
2202 if (!INGLOBALZONE(curproc
) && !zone_dataset_visible(name
, NULL
))
2209 * zc_name name of filesystem
2210 * zc_cookie zap cursor
2211 * zc_nvlist_dst_size size of buffer for property nvlist
2214 * zc_name name of next filesystem
2215 * zc_cookie zap cursor
2216 * zc_objset_stats stats
2217 * zc_nvlist_dst property nvlist
2218 * zc_nvlist_dst_size size of property nvlist
2221 zfs_ioc_dataset_list_next(zfs_cmd_t
*zc
)
2226 size_t orig_len
= strlen(zc
->zc_name
);
2229 if (error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
)) {
2230 if (error
== ENOENT
)
2231 error
= SET_ERROR(ESRCH
);
2235 p
= strrchr(zc
->zc_name
, '/');
2236 if (p
== NULL
|| p
[1] != '\0')
2237 (void) strlcat(zc
->zc_name
, "/", sizeof (zc
->zc_name
));
2238 p
= zc
->zc_name
+ strlen(zc
->zc_name
);
2241 error
= dmu_dir_list_next(os
,
2242 sizeof (zc
->zc_name
) - (p
- zc
->zc_name
), p
,
2243 NULL
, &zc
->zc_cookie
);
2244 if (error
== ENOENT
)
2245 error
= SET_ERROR(ESRCH
);
2246 } while (error
== 0 && dataset_name_hidden(zc
->zc_name
));
2247 dmu_objset_rele(os
, FTAG
);
2250 * If it's an internal dataset (ie. with a '$' in its name),
2251 * don't try to get stats for it, otherwise we'll return ENOENT.
2253 if (error
== 0 && strchr(zc
->zc_name
, '$') == NULL
) {
2254 error
= zfs_ioc_objset_stats(zc
); /* fill in the stats */
2255 if (error
== ENOENT
) {
2256 /* We lost a race with destroy, get the next one. */
2257 zc
->zc_name
[orig_len
] = '\0';
2266 * zc_name name of filesystem
2267 * zc_cookie zap cursor
2268 * zc_nvlist_dst_size size of buffer for property nvlist
2271 * zc_name name of next snapshot
2272 * zc_objset_stats stats
2273 * zc_nvlist_dst property nvlist
2274 * zc_nvlist_dst_size size of property nvlist
2277 zfs_ioc_snapshot_list_next(zfs_cmd_t
*zc
)
2282 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
2284 return (error
== ENOENT
? ESRCH
: error
);
2288 * A dataset name of maximum length cannot have any snapshots,
2289 * so exit immediately.
2291 if (strlcat(zc
->zc_name
, "@", sizeof (zc
->zc_name
)) >= MAXNAMELEN
) {
2292 dmu_objset_rele(os
, FTAG
);
2293 return (SET_ERROR(ESRCH
));
2296 error
= dmu_snapshot_list_next(os
,
2297 sizeof (zc
->zc_name
) - strlen(zc
->zc_name
),
2298 zc
->zc_name
+ strlen(zc
->zc_name
), &zc
->zc_obj
, &zc
->zc_cookie
,
2303 dsl_pool_t
*dp
= os
->os_dsl_dataset
->ds_dir
->dd_pool
;
2305 error
= dsl_dataset_hold_obj(dp
, zc
->zc_obj
, FTAG
, &ds
);
2309 error
= dmu_objset_from_ds(ds
, &ossnap
);
2311 error
= zfs_ioc_objset_stats_impl(zc
, ossnap
);
2312 dsl_dataset_rele(ds
, FTAG
);
2314 } else if (error
== ENOENT
) {
2315 error
= SET_ERROR(ESRCH
);
2318 dmu_objset_rele(os
, FTAG
);
2319 /* if we failed, undo the @ that we tacked on to zc_name */
2321 *strchr(zc
->zc_name
, '@') = '\0';
2326 zfs_prop_set_userquota(const char *dsname
, nvpair_t
*pair
)
2328 const char *propname
= nvpair_name(pair
);
2330 unsigned int vallen
;
2333 zfs_userquota_prop_t type
;
2339 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2341 VERIFY(nvpair_value_nvlist(pair
, &attrs
) == 0);
2342 if (nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
2344 return (SET_ERROR(EINVAL
));
2348 * A correctly constructed propname is encoded as
2349 * userquota@<rid>-<domain>.
2351 if ((dash
= strchr(propname
, '-')) == NULL
||
2352 nvpair_value_uint64_array(pair
, &valary
, &vallen
) != 0 ||
2354 return (SET_ERROR(EINVAL
));
2361 err
= zfsvfs_hold(dsname
, FTAG
, &zfsvfs
, B_FALSE
);
2363 err
= zfs_set_userquota(zfsvfs
, type
, domain
, rid
, quota
);
2364 zfsvfs_rele(zfsvfs
, FTAG
);
2371 * If the named property is one that has a special function to set its value,
2372 * return 0 on success and a positive error code on failure; otherwise if it is
2373 * not one of the special properties handled by this function, return -1.
2375 * XXX: It would be better for callers of the property interface if we handled
2376 * these special cases in dsl_prop.c (in the dsl layer).
2379 zfs_prop_set_special(const char *dsname
, zprop_source_t source
,
2382 const char *propname
= nvpair_name(pair
);
2383 zfs_prop_t prop
= zfs_name_to_prop(propname
);
2387 if (prop
== ZPROP_INVAL
) {
2388 if (zfs_prop_userquota(propname
))
2389 return (zfs_prop_set_userquota(dsname
, pair
));
2393 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2395 VERIFY(nvpair_value_nvlist(pair
, &attrs
) == 0);
2396 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
2400 if (zfs_prop_get_type(prop
) == PROP_TYPE_STRING
)
2403 VERIFY(0 == nvpair_value_uint64(pair
, &intval
));
2406 case ZFS_PROP_QUOTA
:
2407 err
= dsl_dir_set_quota(dsname
, source
, intval
);
2409 case ZFS_PROP_REFQUOTA
:
2410 err
= dsl_dataset_set_refquota(dsname
, source
, intval
);
2412 case ZFS_PROP_FILESYSTEM_LIMIT
:
2413 case ZFS_PROP_SNAPSHOT_LIMIT
:
2414 if (intval
== UINT64_MAX
) {
2415 /* clearing the limit, just do it */
2418 err
= dsl_dir_activate_fs_ss_limit(dsname
);
2421 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2422 * default path to set the value in the nvlist.
2427 case ZFS_PROP_RESERVATION
:
2428 err
= dsl_dir_set_reservation(dsname
, source
, intval
);
2430 case ZFS_PROP_REFRESERVATION
:
2431 err
= dsl_dataset_set_refreservation(dsname
, source
, intval
);
2433 case ZFS_PROP_VOLSIZE
:
2434 err
= zvol_set_volsize(dsname
, intval
);
2436 case ZFS_PROP_VERSION
:
2440 if ((err
= zfsvfs_hold(dsname
, FTAG
, &zfsvfs
, B_TRUE
)) != 0)
2443 err
= zfs_set_version(zfsvfs
, intval
);
2444 zfsvfs_rele(zfsvfs
, FTAG
);
2446 if (err
== 0 && intval
>= ZPL_VERSION_USERSPACE
) {
2449 zc
= kmem_zalloc(sizeof (zfs_cmd_t
), KM_SLEEP
);
2450 (void) strcpy(zc
->zc_name
, dsname
);
2451 (void) zfs_ioc_userspace_upgrade(zc
);
2452 kmem_free(zc
, sizeof (zfs_cmd_t
));
2456 case ZFS_PROP_COMPRESSION
:
2458 if (intval
== ZIO_COMPRESS_LZ4
) {
2461 if ((err
= spa_open(dsname
, &spa
, FTAG
)) != 0)
2465 * Setting the LZ4 compression algorithm activates
2468 if (!spa_feature_is_active(spa
,
2469 SPA_FEATURE_LZ4_COMPRESS
)) {
2470 if ((err
= zfs_prop_activate_feature(spa
,
2471 SPA_FEATURE_LZ4_COMPRESS
)) != 0) {
2472 spa_close(spa
, FTAG
);
2477 spa_close(spa
, FTAG
);
2480 * We still want the default set action to be performed in the
2481 * caller, we only performed zfeature settings here.
2495 * This function is best effort. If it fails to set any of the given properties,
2496 * it continues to set as many as it can and returns the last error
2497 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2498 * with the list of names of all the properties that failed along with the
2499 * corresponding error numbers.
2501 * If every property is set successfully, zero is returned and errlist is not
2505 zfs_set_prop_nvlist(const char *dsname
, zprop_source_t source
, nvlist_t
*nvl
,
2513 nvlist_t
*genericnvl
= fnvlist_alloc();
2514 nvlist_t
*retrynvl
= fnvlist_alloc();
2518 while ((pair
= nvlist_next_nvpair(nvl
, pair
)) != NULL
) {
2519 const char *propname
= nvpair_name(pair
);
2520 zfs_prop_t prop
= zfs_name_to_prop(propname
);
2523 /* decode the property value */
2525 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2527 attrs
= fnvpair_value_nvlist(pair
);
2528 if (nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
2530 err
= SET_ERROR(EINVAL
);
2533 /* Validate value type */
2534 if (err
== 0 && prop
== ZPROP_INVAL
) {
2535 if (zfs_prop_user(propname
)) {
2536 if (nvpair_type(propval
) != DATA_TYPE_STRING
)
2537 err
= SET_ERROR(EINVAL
);
2538 } else if (zfs_prop_userquota(propname
)) {
2539 if (nvpair_type(propval
) !=
2540 DATA_TYPE_UINT64_ARRAY
)
2541 err
= SET_ERROR(EINVAL
);
2543 err
= SET_ERROR(EINVAL
);
2545 } else if (err
== 0) {
2546 if (nvpair_type(propval
) == DATA_TYPE_STRING
) {
2547 if (zfs_prop_get_type(prop
) != PROP_TYPE_STRING
)
2548 err
= SET_ERROR(EINVAL
);
2549 } else if (nvpair_type(propval
) == DATA_TYPE_UINT64
) {
2552 intval
= fnvpair_value_uint64(propval
);
2554 switch (zfs_prop_get_type(prop
)) {
2555 case PROP_TYPE_NUMBER
:
2557 case PROP_TYPE_STRING
:
2558 err
= SET_ERROR(EINVAL
);
2560 case PROP_TYPE_INDEX
:
2561 if (zfs_prop_index_to_string(prop
,
2562 intval
, &unused
) != 0)
2563 err
= SET_ERROR(EINVAL
);
2567 "unknown property type");
2570 err
= SET_ERROR(EINVAL
);
2574 /* Validate permissions */
2576 err
= zfs_check_settable(dsname
, pair
, CRED());
2579 err
= zfs_prop_set_special(dsname
, source
, pair
);
2582 * For better performance we build up a list of
2583 * properties to set in a single transaction.
2585 err
= nvlist_add_nvpair(genericnvl
, pair
);
2586 } else if (err
!= 0 && nvl
!= retrynvl
) {
2588 * This may be a spurious error caused by
2589 * receiving quota and reservation out of order.
2590 * Try again in a second pass.
2592 err
= nvlist_add_nvpair(retrynvl
, pair
);
2597 if (errlist
!= NULL
)
2598 fnvlist_add_int32(errlist
, propname
, err
);
2603 if (nvl
!= retrynvl
&& !nvlist_empty(retrynvl
)) {
2608 if (!nvlist_empty(genericnvl
) &&
2609 dsl_props_set(dsname
, source
, genericnvl
) != 0) {
2611 * If this fails, we still want to set as many properties as we
2612 * can, so try setting them individually.
2615 while ((pair
= nvlist_next_nvpair(genericnvl
, pair
)) != NULL
) {
2616 const char *propname
= nvpair_name(pair
);
2620 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2622 attrs
= fnvpair_value_nvlist(pair
);
2623 propval
= fnvlist_lookup_nvpair(attrs
,
2627 if (nvpair_type(propval
) == DATA_TYPE_STRING
) {
2628 strval
= fnvpair_value_string(propval
);
2629 err
= dsl_prop_set_string(dsname
, propname
,
2632 intval
= fnvpair_value_uint64(propval
);
2633 err
= dsl_prop_set_int(dsname
, propname
, source
,
2638 if (errlist
!= NULL
) {
2639 fnvlist_add_int32(errlist
, propname
,
2646 nvlist_free(genericnvl
);
2647 nvlist_free(retrynvl
);
2653 * Check that all the properties are valid user properties.
2656 zfs_check_userprops(const char *fsname
, nvlist_t
*nvl
)
2658 nvpair_t
*pair
= NULL
;
2661 while ((pair
= nvlist_next_nvpair(nvl
, pair
)) != NULL
) {
2662 const char *propname
= nvpair_name(pair
);
2664 if (!zfs_prop_user(propname
) ||
2665 nvpair_type(pair
) != DATA_TYPE_STRING
)
2666 return (SET_ERROR(EINVAL
));
2668 if (error
= zfs_secpolicy_write_perms(fsname
,
2669 ZFS_DELEG_PERM_USERPROP
, CRED()))
2672 if (strlen(propname
) >= ZAP_MAXNAMELEN
)
2673 return (SET_ERROR(ENAMETOOLONG
));
2675 if (strlen(fnvpair_value_string(pair
)) >= ZAP_MAXVALUELEN
)
2682 props_skip(nvlist_t
*props
, nvlist_t
*skipped
, nvlist_t
**newprops
)
2686 VERIFY(nvlist_alloc(newprops
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2689 while ((pair
= nvlist_next_nvpair(props
, pair
)) != NULL
) {
2690 if (nvlist_exists(skipped
, nvpair_name(pair
)))
2693 VERIFY(nvlist_add_nvpair(*newprops
, pair
) == 0);
2698 clear_received_props(const char *dsname
, nvlist_t
*props
,
2702 nvlist_t
*cleared_props
= NULL
;
2703 props_skip(props
, skipped
, &cleared_props
);
2704 if (!nvlist_empty(cleared_props
)) {
2706 * Acts on local properties until the dataset has received
2707 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2709 zprop_source_t flags
= (ZPROP_SRC_NONE
|
2710 (dsl_prop_get_hasrecvd(dsname
) ? ZPROP_SRC_RECEIVED
: 0));
2711 err
= zfs_set_prop_nvlist(dsname
, flags
, cleared_props
, NULL
);
2713 nvlist_free(cleared_props
);
2719 * zc_name name of filesystem
2720 * zc_value name of property to set
2721 * zc_nvlist_src{_size} nvlist of properties to apply
2722 * zc_cookie received properties flag
2725 * zc_nvlist_dst{_size} error for each unapplied received property
2728 zfs_ioc_set_prop(zfs_cmd_t
*zc
)
2731 boolean_t received
= zc
->zc_cookie
;
2732 zprop_source_t source
= (received
? ZPROP_SRC_RECEIVED
:
2737 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2738 zc
->zc_iflags
, &nvl
)) != 0)
2742 nvlist_t
*origprops
;
2744 if (dsl_prop_get_received(zc
->zc_name
, &origprops
) == 0) {
2745 (void) clear_received_props(zc
->zc_name
,
2747 nvlist_free(origprops
);
2750 error
= dsl_prop_set_hasrecvd(zc
->zc_name
);
2753 errors
= fnvlist_alloc();
2755 error
= zfs_set_prop_nvlist(zc
->zc_name
, source
, nvl
, errors
);
2757 if (zc
->zc_nvlist_dst
!= NULL
&& errors
!= NULL
) {
2758 (void) put_nvlist(zc
, errors
);
2761 nvlist_free(errors
);
2768 * zc_name name of filesystem
2769 * zc_value name of property to inherit
2770 * zc_cookie revert to received value if TRUE
2775 zfs_ioc_inherit_prop(zfs_cmd_t
*zc
)
2777 const char *propname
= zc
->zc_value
;
2778 zfs_prop_t prop
= zfs_name_to_prop(propname
);
2779 boolean_t received
= zc
->zc_cookie
;
2780 zprop_source_t source
= (received
2781 ? ZPROP_SRC_NONE
/* revert to received value, if any */
2782 : ZPROP_SRC_INHERITED
); /* explicitly inherit */
2791 * zfs_prop_set_special() expects properties in the form of an
2792 * nvpair with type info.
2794 if (prop
== ZPROP_INVAL
) {
2795 if (!zfs_prop_user(propname
))
2796 return (SET_ERROR(EINVAL
));
2798 type
= PROP_TYPE_STRING
;
2799 } else if (prop
== ZFS_PROP_VOLSIZE
||
2800 prop
== ZFS_PROP_VERSION
) {
2801 return (SET_ERROR(EINVAL
));
2803 type
= zfs_prop_get_type(prop
);
2806 VERIFY(nvlist_alloc(&dummy
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2809 case PROP_TYPE_STRING
:
2810 VERIFY(0 == nvlist_add_string(dummy
, propname
, ""));
2812 case PROP_TYPE_NUMBER
:
2813 case PROP_TYPE_INDEX
:
2814 VERIFY(0 == nvlist_add_uint64(dummy
, propname
, 0));
2818 return (SET_ERROR(EINVAL
));
2821 pair
= nvlist_next_nvpair(dummy
, NULL
);
2822 err
= zfs_prop_set_special(zc
->zc_name
, source
, pair
);
2825 return (err
); /* special property already handled */
2828 * Only check this in the non-received case. We want to allow
2829 * 'inherit -S' to revert non-inheritable properties like quota
2830 * and reservation to the received or default values even though
2831 * they are not considered inheritable.
2833 if (prop
!= ZPROP_INVAL
&& !zfs_prop_inheritable(prop
))
2834 return (SET_ERROR(EINVAL
));
2837 /* property name has been validated by zfs_secpolicy_inherit_prop() */
2838 return (dsl_prop_inherit(zc
->zc_name
, zc
->zc_value
, source
));
2842 zfs_ioc_pool_set_props(zfs_cmd_t
*zc
)
2849 if (error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2850 zc
->zc_iflags
, &props
))
2854 * If the only property is the configfile, then just do a spa_lookup()
2855 * to handle the faulted case.
2857 pair
= nvlist_next_nvpair(props
, NULL
);
2858 if (pair
!= NULL
&& strcmp(nvpair_name(pair
),
2859 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
)) == 0 &&
2860 nvlist_next_nvpair(props
, pair
) == NULL
) {
2861 mutex_enter(&spa_namespace_lock
);
2862 if ((spa
= spa_lookup(zc
->zc_name
)) != NULL
) {
2863 spa_configfile_set(spa
, props
, B_FALSE
);
2864 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
2866 mutex_exit(&spa_namespace_lock
);
2873 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0) {
2878 error
= spa_prop_set(spa
, props
);
2881 spa_close(spa
, FTAG
);
2887 zfs_ioc_pool_get_props(zfs_cmd_t
*zc
)
2891 nvlist_t
*nvp
= NULL
;
2893 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0) {
2895 * If the pool is faulted, there may be properties we can still
2896 * get (such as altroot and cachefile), so attempt to get them
2899 mutex_enter(&spa_namespace_lock
);
2900 if ((spa
= spa_lookup(zc
->zc_name
)) != NULL
)
2901 error
= spa_prop_get(spa
, &nvp
);
2902 mutex_exit(&spa_namespace_lock
);
2904 error
= spa_prop_get(spa
, &nvp
);
2905 spa_close(spa
, FTAG
);
2908 if (error
== 0 && zc
->zc_nvlist_dst
!= NULL
)
2909 error
= put_nvlist(zc
, nvp
);
2911 error
= SET_ERROR(EFAULT
);
2919 * zc_name name of filesystem
2920 * zc_nvlist_src{_size} nvlist of delegated permissions
2921 * zc_perm_action allow/unallow flag
2926 zfs_ioc_set_fsacl(zfs_cmd_t
*zc
)
2929 nvlist_t
*fsaclnv
= NULL
;
2931 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2932 zc
->zc_iflags
, &fsaclnv
)) != 0)
2936 * Verify nvlist is constructed correctly
2938 if ((error
= zfs_deleg_verify_nvlist(fsaclnv
)) != 0) {
2939 nvlist_free(fsaclnv
);
2940 return (SET_ERROR(EINVAL
));
2944 * If we don't have PRIV_SYS_MOUNT, then validate
2945 * that user is allowed to hand out each permission in
2949 error
= secpolicy_zfs(CRED());
2951 if (zc
->zc_perm_action
== B_FALSE
) {
2952 error
= dsl_deleg_can_allow(zc
->zc_name
,
2955 error
= dsl_deleg_can_unallow(zc
->zc_name
,
2961 error
= dsl_deleg_set(zc
->zc_name
, fsaclnv
, zc
->zc_perm_action
);
2963 nvlist_free(fsaclnv
);
2969 * zc_name name of filesystem
2972 * zc_nvlist_src{_size} nvlist of delegated permissions
2975 zfs_ioc_get_fsacl(zfs_cmd_t
*zc
)
2980 if ((error
= dsl_deleg_get(zc
->zc_name
, &nvp
)) == 0) {
2981 error
= put_nvlist(zc
, nvp
);
2989 * Search the vfs list for a specified resource. Returns a pointer to it
2990 * or NULL if no suitable entry is found. The caller of this routine
2991 * is responsible for releasing the returned vfs pointer.
2994 zfs_get_vfs(const char *resource
)
2997 struct vfs
*vfs_found
= NULL
;
2999 vfs_list_read_lock();
3002 if (strcmp(refstr_value(vfsp
->vfs_resource
), resource
) == 0) {
3007 vfsp
= vfsp
->vfs_next
;
3008 } while (vfsp
!= rootvfs
);
3015 zfs_create_cb(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
)
3017 zfs_creat_t
*zct
= arg
;
3019 zfs_create_fs(os
, cr
, zct
->zct_zplprops
, tx
);
3022 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
3026 * os parent objset pointer (NULL if root fs)
3027 * fuids_ok fuids allowed in this version of the spa?
3028 * sa_ok SAs allowed in this version of the spa?
3029 * createprops list of properties requested by creator
3032 * zplprops values for the zplprops we attach to the master node object
3033 * is_ci true if requested file system will be purely case-insensitive
3035 * Determine the settings for utf8only, normalization and
3036 * casesensitivity. Specific values may have been requested by the
3037 * creator and/or we can inherit values from the parent dataset. If
3038 * the file system is of too early a vintage, a creator can not
3039 * request settings for these properties, even if the requested
3040 * setting is the default value. We don't actually want to create dsl
3041 * properties for these, so remove them from the source nvlist after
3045 zfs_fill_zplprops_impl(objset_t
*os
, uint64_t zplver
,
3046 boolean_t fuids_ok
, boolean_t sa_ok
, nvlist_t
*createprops
,
3047 nvlist_t
*zplprops
, boolean_t
*is_ci
)
3049 uint64_t sense
= ZFS_PROP_UNDEFINED
;
3050 uint64_t norm
= ZFS_PROP_UNDEFINED
;
3051 uint64_t u8
= ZFS_PROP_UNDEFINED
;
3053 ASSERT(zplprops
!= NULL
);
3056 * Pull out creator prop choices, if any.
3059 (void) nvlist_lookup_uint64(createprops
,
3060 zfs_prop_to_name(ZFS_PROP_VERSION
), &zplver
);
3061 (void) nvlist_lookup_uint64(createprops
,
3062 zfs_prop_to_name(ZFS_PROP_NORMALIZE
), &norm
);
3063 (void) nvlist_remove_all(createprops
,
3064 zfs_prop_to_name(ZFS_PROP_NORMALIZE
));
3065 (void) nvlist_lookup_uint64(createprops
,
3066 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), &u8
);
3067 (void) nvlist_remove_all(createprops
,
3068 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
));
3069 (void) nvlist_lookup_uint64(createprops
,
3070 zfs_prop_to_name(ZFS_PROP_CASE
), &sense
);
3071 (void) nvlist_remove_all(createprops
,
3072 zfs_prop_to_name(ZFS_PROP_CASE
));
3076 * If the zpl version requested is whacky or the file system
3077 * or pool is version is too "young" to support normalization
3078 * and the creator tried to set a value for one of the props,
3081 if ((zplver
< ZPL_VERSION_INITIAL
|| zplver
> ZPL_VERSION
) ||
3082 (zplver
>= ZPL_VERSION_FUID
&& !fuids_ok
) ||
3083 (zplver
>= ZPL_VERSION_SA
&& !sa_ok
) ||
3084 (zplver
< ZPL_VERSION_NORMALIZATION
&&
3085 (norm
!= ZFS_PROP_UNDEFINED
|| u8
!= ZFS_PROP_UNDEFINED
||
3086 sense
!= ZFS_PROP_UNDEFINED
)))
3087 return (SET_ERROR(ENOTSUP
));
3090 * Put the version in the zplprops
3092 VERIFY(nvlist_add_uint64(zplprops
,
3093 zfs_prop_to_name(ZFS_PROP_VERSION
), zplver
) == 0);
3095 if (norm
== ZFS_PROP_UNDEFINED
)
3096 VERIFY(zfs_get_zplprop(os
, ZFS_PROP_NORMALIZE
, &norm
) == 0);
3097 VERIFY(nvlist_add_uint64(zplprops
,
3098 zfs_prop_to_name(ZFS_PROP_NORMALIZE
), norm
) == 0);
3101 * If we're normalizing, names must always be valid UTF-8 strings.
3105 if (u8
== ZFS_PROP_UNDEFINED
)
3106 VERIFY(zfs_get_zplprop(os
, ZFS_PROP_UTF8ONLY
, &u8
) == 0);
3107 VERIFY(nvlist_add_uint64(zplprops
,
3108 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), u8
) == 0);
3110 if (sense
== ZFS_PROP_UNDEFINED
)
3111 VERIFY(zfs_get_zplprop(os
, ZFS_PROP_CASE
, &sense
) == 0);
3112 VERIFY(nvlist_add_uint64(zplprops
,
3113 zfs_prop_to_name(ZFS_PROP_CASE
), sense
) == 0);
3116 *is_ci
= (sense
== ZFS_CASE_INSENSITIVE
);
3122 zfs_fill_zplprops(const char *dataset
, nvlist_t
*createprops
,
3123 nvlist_t
*zplprops
, boolean_t
*is_ci
)
3125 boolean_t fuids_ok
, sa_ok
;
3126 uint64_t zplver
= ZPL_VERSION
;
3127 objset_t
*os
= NULL
;
3128 char parentname
[MAXNAMELEN
];
3134 (void) strlcpy(parentname
, dataset
, sizeof (parentname
));
3135 cp
= strrchr(parentname
, '/');
3139 if ((error
= spa_open(dataset
, &spa
, FTAG
)) != 0)
3142 spa_vers
= spa_version(spa
);
3143 spa_close(spa
, FTAG
);
3145 zplver
= zfs_zpl_version_map(spa_vers
);
3146 fuids_ok
= (zplver
>= ZPL_VERSION_FUID
);
3147 sa_ok
= (zplver
>= ZPL_VERSION_SA
);
3150 * Open parent object set so we can inherit zplprop values.
3152 if ((error
= dmu_objset_hold(parentname
, FTAG
, &os
)) != 0)
3155 error
= zfs_fill_zplprops_impl(os
, zplver
, fuids_ok
, sa_ok
, createprops
,
3157 dmu_objset_rele(os
, FTAG
);
3162 zfs_fill_zplprops_root(uint64_t spa_vers
, nvlist_t
*createprops
,
3163 nvlist_t
*zplprops
, boolean_t
*is_ci
)
3167 uint64_t zplver
= ZPL_VERSION
;
3170 zplver
= zfs_zpl_version_map(spa_vers
);
3171 fuids_ok
= (zplver
>= ZPL_VERSION_FUID
);
3172 sa_ok
= (zplver
>= ZPL_VERSION_SA
);
3174 error
= zfs_fill_zplprops_impl(NULL
, zplver
, fuids_ok
, sa_ok
,
3175 createprops
, zplprops
, is_ci
);
3181 * "type" -> dmu_objset_type_t (int32)
3182 * (optional) "props" -> { prop -> value }
3185 * outnvl: propname -> error code (int32)
3188 zfs_ioc_create(const char *fsname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3191 zfs_creat_t zct
= { 0 };
3192 nvlist_t
*nvprops
= NULL
;
3193 void (*cbfunc
)(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
);
3195 dmu_objset_type_t type
;
3196 boolean_t is_insensitive
= B_FALSE
;
3198 if (nvlist_lookup_int32(innvl
, "type", &type32
) != 0)
3199 return (SET_ERROR(EINVAL
));
3201 (void) nvlist_lookup_nvlist(innvl
, "props", &nvprops
);
3205 cbfunc
= zfs_create_cb
;
3209 cbfunc
= zvol_create_cb
;
3216 if (strchr(fsname
, '@') ||
3217 strchr(fsname
, '%'))
3218 return (SET_ERROR(EINVAL
));
3220 zct
.zct_props
= nvprops
;
3223 return (SET_ERROR(EINVAL
));
3225 if (type
== DMU_OST_ZVOL
) {
3226 uint64_t volsize
, volblocksize
;
3228 if (nvprops
== NULL
)
3229 return (SET_ERROR(EINVAL
));
3230 if (nvlist_lookup_uint64(nvprops
,
3231 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &volsize
) != 0)
3232 return (SET_ERROR(EINVAL
));
3234 if ((error
= nvlist_lookup_uint64(nvprops
,
3235 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
3236 &volblocksize
)) != 0 && error
!= ENOENT
)
3237 return (SET_ERROR(EINVAL
));
3240 volblocksize
= zfs_prop_default_numeric(
3241 ZFS_PROP_VOLBLOCKSIZE
);
3243 if ((error
= zvol_check_volblocksize(
3244 volblocksize
)) != 0 ||
3245 (error
= zvol_check_volsize(volsize
,
3246 volblocksize
)) != 0)
3248 } else if (type
== DMU_OST_ZFS
) {
3252 * We have to have normalization and
3253 * case-folding flags correct when we do the
3254 * file system creation, so go figure them out
3257 VERIFY(nvlist_alloc(&zct
.zct_zplprops
,
3258 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3259 error
= zfs_fill_zplprops(fsname
, nvprops
,
3260 zct
.zct_zplprops
, &is_insensitive
);
3262 nvlist_free(zct
.zct_zplprops
);
3267 error
= dmu_objset_create(fsname
, type
,
3268 is_insensitive
? DS_FLAG_CI_DATASET
: 0, cbfunc
, &zct
);
3269 nvlist_free(zct
.zct_zplprops
);
3272 * It would be nice to do this atomically.
3275 error
= zfs_set_prop_nvlist(fsname
, ZPROP_SRC_LOCAL
,
3278 (void) dsl_destroy_head(fsname
);
3285 * "origin" -> name of origin snapshot
3286 * (optional) "props" -> { prop -> value }
3289 * outnvl: propname -> error code (int32)
3292 zfs_ioc_clone(const char *fsname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3295 nvlist_t
*nvprops
= NULL
;
3298 if (nvlist_lookup_string(innvl
, "origin", &origin_name
) != 0)
3299 return (SET_ERROR(EINVAL
));
3300 (void) nvlist_lookup_nvlist(innvl
, "props", &nvprops
);
3302 if (strchr(fsname
, '@') ||
3303 strchr(fsname
, '%'))
3304 return (SET_ERROR(EINVAL
));
3306 if (dataset_namecheck(origin_name
, NULL
, NULL
) != 0)
3307 return (SET_ERROR(EINVAL
));
3308 error
= dmu_objset_clone(fsname
, origin_name
);
3313 * It would be nice to do this atomically.
3316 error
= zfs_set_prop_nvlist(fsname
, ZPROP_SRC_LOCAL
,
3319 (void) dsl_destroy_head(fsname
);
3326 * "snaps" -> { snapshot1, snapshot2 }
3327 * (optional) "props" -> { prop -> value (string) }
3330 * outnvl: snapshot -> error code (int32)
3333 zfs_ioc_snapshot(const char *poolname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3336 nvlist_t
*props
= NULL
;
3340 (void) nvlist_lookup_nvlist(innvl
, "props", &props
);
3341 if ((error
= zfs_check_userprops(poolname
, props
)) != 0)
3344 if (!nvlist_empty(props
) &&
3345 zfs_earlier_version(poolname
, SPA_VERSION_SNAP_PROPS
))
3346 return (SET_ERROR(ENOTSUP
));
3348 if (nvlist_lookup_nvlist(innvl
, "snaps", &snaps
) != 0)
3349 return (SET_ERROR(EINVAL
));
3350 poollen
= strlen(poolname
);
3351 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
3352 pair
= nvlist_next_nvpair(snaps
, pair
)) {
3353 const char *name
= nvpair_name(pair
);
3354 const char *cp
= strchr(name
, '@');
3357 * The snap name must contain an @, and the part after it must
3358 * contain only valid characters.
3361 zfs_component_namecheck(cp
+ 1, NULL
, NULL
) != 0)
3362 return (SET_ERROR(EINVAL
));
3365 * The snap must be in the specified pool.
3367 if (strncmp(name
, poolname
, poollen
) != 0 ||
3368 (name
[poollen
] != '/' && name
[poollen
] != '@'))
3369 return (SET_ERROR(EXDEV
));
3371 /* This must be the only snap of this fs. */
3372 for (nvpair_t
*pair2
= nvlist_next_nvpair(snaps
, pair
);
3373 pair2
!= NULL
; pair2
= nvlist_next_nvpair(snaps
, pair2
)) {
3374 if (strncmp(name
, nvpair_name(pair2
), cp
- name
+ 1)
3376 return (SET_ERROR(EXDEV
));
3381 error
= dsl_dataset_snapshot(snaps
, props
, outnvl
);
3386 * innvl: "message" -> string
3390 zfs_ioc_log_history(const char *unused
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3398 * The poolname in the ioctl is not set, we get it from the TSD,
3399 * which was set at the end of the last successful ioctl that allows
3400 * logging. The secpolicy func already checked that it is set.
3401 * Only one log ioctl is allowed after each successful ioctl, so
3402 * we clear the TSD here.
3404 poolname
= tsd_get(zfs_allow_log_key
);
3405 (void) tsd_set(zfs_allow_log_key
, NULL
);
3406 error
= spa_open(poolname
, &spa
, FTAG
);
3411 if (nvlist_lookup_string(innvl
, "message", &message
) != 0) {
3412 spa_close(spa
, FTAG
);
3413 return (SET_ERROR(EINVAL
));
3416 if (spa_version(spa
) < SPA_VERSION_ZPOOL_HISTORY
) {
3417 spa_close(spa
, FTAG
);
3418 return (SET_ERROR(ENOTSUP
));
3421 error
= spa_history_log(spa
, message
);
3422 spa_close(spa
, FTAG
);
3427 * The dp_config_rwlock must not be held when calling this, because the
3428 * unmount may need to write out data.
3430 * This function is best-effort. Callers must deal gracefully if it
3431 * remains mounted (or is remounted after this call).
3433 * Returns 0 if the argument is not a snapshot, or it is not currently a
3434 * filesystem, or we were able to unmount it. Returns error code otherwise.
3437 zfs_unmount_snap(const char *snapname
)
3443 if (strchr(snapname
, '@') == NULL
)
3446 vfsp
= zfs_get_vfs(snapname
);
3450 zfsvfs
= vfsp
->vfs_data
;
3451 ASSERT(!dsl_pool_config_held(dmu_objset_pool(zfsvfs
->z_os
)));
3453 err
= vn_vfswlock(vfsp
->vfs_vnodecovered
);
3456 return (SET_ERROR(err
));
3459 * Always force the unmount for snapshots.
3461 (void) dounmount(vfsp
, MS_FORCE
, kcred
);
3467 zfs_unmount_snap_cb(const char *snapname
, void *arg
)
3469 return (zfs_unmount_snap(snapname
));
3473 * When a clone is destroyed, its origin may also need to be destroyed,
3474 * in which case it must be unmounted. This routine will do that unmount
3478 zfs_destroy_unmount_origin(const char *fsname
)
3484 error
= dmu_objset_hold(fsname
, FTAG
, &os
);
3487 ds
= dmu_objset_ds(os
);
3488 if (dsl_dir_is_clone(ds
->ds_dir
) && DS_IS_DEFER_DESTROY(ds
->ds_prev
)) {
3489 char originname
[MAXNAMELEN
];
3490 dsl_dataset_name(ds
->ds_prev
, originname
);
3491 dmu_objset_rele(os
, FTAG
);
3492 (void) zfs_unmount_snap(originname
);
3494 dmu_objset_rele(os
, FTAG
);
3500 * "snaps" -> { snapshot1, snapshot2 }
3501 * (optional boolean) "defer"
3504 * outnvl: snapshot -> error code (int32)
3509 zfs_ioc_destroy_snaps(const char *poolname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3515 if (nvlist_lookup_nvlist(innvl
, "snaps", &snaps
) != 0)
3516 return (SET_ERROR(EINVAL
));
3517 defer
= nvlist_exists(innvl
, "defer");
3519 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
3520 pair
= nvlist_next_nvpair(snaps
, pair
)) {
3521 (void) zfs_unmount_snap(nvpair_name(pair
));
3524 return (dsl_destroy_snapshots_nvl(snaps
, defer
, outnvl
));
3528 * Create bookmarks. Bookmark names are of the form <fs>#<bmark>.
3529 * All bookmarks must be in the same pool.
3532 * bookmark1 -> snapshot1, bookmark2 -> snapshot2
3535 * outnvl: bookmark -> error code (int32)
3540 zfs_ioc_bookmark(const char *poolname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3542 for (nvpair_t
*pair
= nvlist_next_nvpair(innvl
, NULL
);
3543 pair
!= NULL
; pair
= nvlist_next_nvpair(innvl
, pair
)) {
3547 * Verify the snapshot argument.
3549 if (nvpair_value_string(pair
, &snap_name
) != 0)
3550 return (SET_ERROR(EINVAL
));
3553 /* Verify that the keys (bookmarks) are unique */
3554 for (nvpair_t
*pair2
= nvlist_next_nvpair(innvl
, pair
);
3555 pair2
!= NULL
; pair2
= nvlist_next_nvpair(innvl
, pair2
)) {
3556 if (strcmp(nvpair_name(pair
), nvpair_name(pair2
)) == 0)
3557 return (SET_ERROR(EINVAL
));
3561 return (dsl_bookmark_create(innvl
, outnvl
));
3566 * property 1, property 2, ...
3570 * bookmark name 1 -> { property 1, property 2, ... },
3571 * bookmark name 2 -> { property 1, property 2, ... }
3576 zfs_ioc_get_bookmarks(const char *fsname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3578 return (dsl_get_bookmarks(fsname
, innvl
, outnvl
));
3583 * bookmark name 1, bookmark name 2
3586 * outnvl: bookmark -> error code (int32)
3590 zfs_ioc_destroy_bookmarks(const char *poolname
, nvlist_t
*innvl
,
3595 poollen
= strlen(poolname
);
3596 for (nvpair_t
*pair
= nvlist_next_nvpair(innvl
, NULL
);
3597 pair
!= NULL
; pair
= nvlist_next_nvpair(innvl
, pair
)) {
3598 const char *name
= nvpair_name(pair
);
3599 const char *cp
= strchr(name
, '#');
3602 * The bookmark name must contain an #, and the part after it
3603 * must contain only valid characters.
3606 zfs_component_namecheck(cp
+ 1, NULL
, NULL
) != 0)
3607 return (SET_ERROR(EINVAL
));
3610 * The bookmark must be in the specified pool.
3612 if (strncmp(name
, poolname
, poollen
) != 0 ||
3613 (name
[poollen
] != '/' && name
[poollen
] != '#'))
3614 return (SET_ERROR(EXDEV
));
3617 error
= dsl_bookmark_destroy(innvl
, outnvl
);
3623 * zc_name name of dataset to destroy
3624 * zc_objset_type type of objset
3625 * zc_defer_destroy mark for deferred destroy
3630 zfs_ioc_destroy(zfs_cmd_t
*zc
)
3634 if (zc
->zc_objset_type
== DMU_OST_ZFS
) {
3635 err
= zfs_unmount_snap(zc
->zc_name
);
3640 if (strchr(zc
->zc_name
, '@'))
3641 err
= dsl_destroy_snapshot(zc
->zc_name
, zc
->zc_defer_destroy
);
3643 err
= dsl_destroy_head(zc
->zc_name
);
3644 if (zc
->zc_objset_type
== DMU_OST_ZVOL
&& err
== 0)
3645 (void) zvol_remove_minor(zc
->zc_name
);
3650 * fsname is name of dataset to rollback (to most recent snapshot)
3652 * innvl is not used.
3654 * outnvl: "target" -> name of most recent snapshot
3659 zfs_ioc_rollback(const char *fsname
, nvlist_t
*args
, nvlist_t
*outnvl
)
3664 if (getzfsvfs(fsname
, &zfsvfs
) == 0) {
3665 error
= zfs_suspend_fs(zfsvfs
);
3669 error
= dsl_dataset_rollback(fsname
, zfsvfs
, outnvl
);
3670 resume_err
= zfs_resume_fs(zfsvfs
, fsname
);
3671 error
= error
? error
: resume_err
;
3673 VFS_RELE(zfsvfs
->z_vfs
);
3675 error
= dsl_dataset_rollback(fsname
, NULL
, outnvl
);
3681 recursive_unmount(const char *fsname
, void *arg
)
3683 const char *snapname
= arg
;
3684 char fullname
[MAXNAMELEN
];
3686 (void) snprintf(fullname
, sizeof (fullname
), "%s@%s", fsname
, snapname
);
3687 return (zfs_unmount_snap(fullname
));
3692 * zc_name old name of dataset
3693 * zc_value new name of dataset
3694 * zc_cookie recursive flag (only valid for snapshots)
3699 zfs_ioc_rename(zfs_cmd_t
*zc
)
3701 boolean_t recursive
= zc
->zc_cookie
& 1;
3704 zc
->zc_value
[sizeof (zc
->zc_value
) - 1] = '\0';
3705 if (dataset_namecheck(zc
->zc_value
, NULL
, NULL
) != 0 ||
3706 strchr(zc
->zc_value
, '%'))
3707 return (SET_ERROR(EINVAL
));
3709 at
= strchr(zc
->zc_name
, '@');
3711 /* snaps must be in same fs */
3714 if (strncmp(zc
->zc_name
, zc
->zc_value
, at
- zc
->zc_name
+ 1))
3715 return (SET_ERROR(EXDEV
));
3717 if (zc
->zc_objset_type
== DMU_OST_ZFS
) {
3718 error
= dmu_objset_find(zc
->zc_name
,
3719 recursive_unmount
, at
+ 1,
3720 recursive
? DS_FIND_CHILDREN
: 0);
3726 error
= dsl_dataset_rename_snapshot(zc
->zc_name
,
3727 at
+ 1, strchr(zc
->zc_value
, '@') + 1, recursive
);
3732 if (zc
->zc_objset_type
== DMU_OST_ZVOL
)
3733 (void) zvol_remove_minor(zc
->zc_name
);
3734 return (dsl_dir_rename(zc
->zc_name
, zc
->zc_value
));
3739 zfs_check_settable(const char *dsname
, nvpair_t
*pair
, cred_t
*cr
)
3741 const char *propname
= nvpair_name(pair
);
3742 boolean_t issnap
= (strchr(dsname
, '@') != NULL
);
3743 zfs_prop_t prop
= zfs_name_to_prop(propname
);
3747 if (prop
== ZPROP_INVAL
) {
3748 if (zfs_prop_user(propname
)) {
3749 if (err
= zfs_secpolicy_write_perms(dsname
,
3750 ZFS_DELEG_PERM_USERPROP
, cr
))
3755 if (!issnap
&& zfs_prop_userquota(propname
)) {
3756 const char *perm
= NULL
;
3757 const char *uq_prefix
=
3758 zfs_userquota_prop_prefixes
[ZFS_PROP_USERQUOTA
];
3759 const char *gq_prefix
=
3760 zfs_userquota_prop_prefixes
[ZFS_PROP_GROUPQUOTA
];
3762 if (strncmp(propname
, uq_prefix
,
3763 strlen(uq_prefix
)) == 0) {
3764 perm
= ZFS_DELEG_PERM_USERQUOTA
;
3765 } else if (strncmp(propname
, gq_prefix
,
3766 strlen(gq_prefix
)) == 0) {
3767 perm
= ZFS_DELEG_PERM_GROUPQUOTA
;
3769 /* USERUSED and GROUPUSED are read-only */
3770 return (SET_ERROR(EINVAL
));
3773 if (err
= zfs_secpolicy_write_perms(dsname
, perm
, cr
))
3778 return (SET_ERROR(EINVAL
));
3782 return (SET_ERROR(EINVAL
));
3784 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
3786 * dsl_prop_get_all_impl() returns properties in this
3790 VERIFY(nvpair_value_nvlist(pair
, &attrs
) == 0);
3791 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
3796 * Check that this value is valid for this pool version
3799 case ZFS_PROP_COMPRESSION
:
3801 * If the user specified gzip compression, make sure
3802 * the SPA supports it. We ignore any errors here since
3803 * we'll catch them later.
3805 if (nvpair_type(pair
) == DATA_TYPE_UINT64
&&
3806 nvpair_value_uint64(pair
, &intval
) == 0) {
3807 if (intval
>= ZIO_COMPRESS_GZIP_1
&&
3808 intval
<= ZIO_COMPRESS_GZIP_9
&&
3809 zfs_earlier_version(dsname
,
3810 SPA_VERSION_GZIP_COMPRESSION
)) {
3811 return (SET_ERROR(ENOTSUP
));
3814 if (intval
== ZIO_COMPRESS_ZLE
&&
3815 zfs_earlier_version(dsname
,
3816 SPA_VERSION_ZLE_COMPRESSION
))
3817 return (SET_ERROR(ENOTSUP
));
3819 if (intval
== ZIO_COMPRESS_LZ4
) {
3822 if ((err
= spa_open(dsname
, &spa
, FTAG
)) != 0)
3825 if (!spa_feature_is_enabled(spa
,
3826 SPA_FEATURE_LZ4_COMPRESS
)) {
3827 spa_close(spa
, FTAG
);
3828 return (SET_ERROR(ENOTSUP
));
3830 spa_close(spa
, FTAG
);
3834 * If this is a bootable dataset then
3835 * verify that the compression algorithm
3836 * is supported for booting. We must return
3837 * something other than ENOTSUP since it
3838 * implies a downrev pool version.
3840 if (zfs_is_bootfs(dsname
) &&
3841 !BOOTFS_COMPRESS_VALID(intval
)) {
3842 return (SET_ERROR(ERANGE
));
3847 case ZFS_PROP_COPIES
:
3848 if (zfs_earlier_version(dsname
, SPA_VERSION_DITTO_BLOCKS
))
3849 return (SET_ERROR(ENOTSUP
));
3852 case ZFS_PROP_DEDUP
:
3853 if (zfs_earlier_version(dsname
, SPA_VERSION_DEDUP
))
3854 return (SET_ERROR(ENOTSUP
));
3857 case ZFS_PROP_SHARESMB
:
3858 if (zpl_earlier_version(dsname
, ZPL_VERSION_FUID
))
3859 return (SET_ERROR(ENOTSUP
));
3862 case ZFS_PROP_ACLINHERIT
:
3863 if (nvpair_type(pair
) == DATA_TYPE_UINT64
&&
3864 nvpair_value_uint64(pair
, &intval
) == 0) {
3865 if (intval
== ZFS_ACL_PASSTHROUGH_X
&&
3866 zfs_earlier_version(dsname
,
3867 SPA_VERSION_PASSTHROUGH_X
))
3868 return (SET_ERROR(ENOTSUP
));
3873 return (zfs_secpolicy_setprop(dsname
, prop
, pair
, CRED()));
3877 * Checks for a race condition to make sure we don't increment a feature flag
3881 zfs_prop_activate_feature_check(void *arg
, dmu_tx_t
*tx
)
3883 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
3884 spa_feature_t
*featurep
= arg
;
3886 if (!spa_feature_is_active(spa
, *featurep
))
3889 return (SET_ERROR(EBUSY
));
3893 * The callback invoked on feature activation in the sync task caused by
3894 * zfs_prop_activate_feature.
3897 zfs_prop_activate_feature_sync(void *arg
, dmu_tx_t
*tx
)
3899 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
3900 spa_feature_t
*featurep
= arg
;
3902 spa_feature_incr(spa
, *featurep
, tx
);
3906 * Activates a feature on a pool in response to a property setting. This
3907 * creates a new sync task which modifies the pool to reflect the feature
3911 zfs_prop_activate_feature(spa_t
*spa
, spa_feature_t feature
)
3915 /* EBUSY here indicates that the feature is already active */
3916 err
= dsl_sync_task(spa_name(spa
),
3917 zfs_prop_activate_feature_check
, zfs_prop_activate_feature_sync
,
3920 if (err
!= 0 && err
!= EBUSY
)
3927 * Removes properties from the given props list that fail permission checks
3928 * needed to clear them and to restore them in case of a receive error. For each
3929 * property, make sure we have both set and inherit permissions.
3931 * Returns the first error encountered if any permission checks fail. If the
3932 * caller provides a non-NULL errlist, it also gives the complete list of names
3933 * of all the properties that failed a permission check along with the
3934 * corresponding error numbers. The caller is responsible for freeing the
3937 * If every property checks out successfully, zero is returned and the list
3938 * pointed at by errlist is NULL.
3941 zfs_check_clearable(char *dataset
, nvlist_t
*props
, nvlist_t
**errlist
)
3944 nvpair_t
*pair
, *next_pair
;
3951 VERIFY(nvlist_alloc(&errors
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3953 zc
= kmem_alloc(sizeof (zfs_cmd_t
), KM_SLEEP
);
3954 (void) strcpy(zc
->zc_name
, dataset
);
3955 pair
= nvlist_next_nvpair(props
, NULL
);
3956 while (pair
!= NULL
) {
3957 next_pair
= nvlist_next_nvpair(props
, pair
);
3959 (void) strcpy(zc
->zc_value
, nvpair_name(pair
));
3960 if ((err
= zfs_check_settable(dataset
, pair
, CRED())) != 0 ||
3961 (err
= zfs_secpolicy_inherit_prop(zc
, NULL
, CRED())) != 0) {
3962 VERIFY(nvlist_remove_nvpair(props
, pair
) == 0);
3963 VERIFY(nvlist_add_int32(errors
,
3964 zc
->zc_value
, err
) == 0);
3968 kmem_free(zc
, sizeof (zfs_cmd_t
));
3970 if ((pair
= nvlist_next_nvpair(errors
, NULL
)) == NULL
) {
3971 nvlist_free(errors
);
3974 VERIFY(nvpair_value_int32(pair
, &rv
) == 0);
3977 if (errlist
== NULL
)
3978 nvlist_free(errors
);
3986 propval_equals(nvpair_t
*p1
, nvpair_t
*p2
)
3988 if (nvpair_type(p1
) == DATA_TYPE_NVLIST
) {
3989 /* dsl_prop_get_all_impl() format */
3991 VERIFY(nvpair_value_nvlist(p1
, &attrs
) == 0);
3992 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
3996 if (nvpair_type(p2
) == DATA_TYPE_NVLIST
) {
3998 VERIFY(nvpair_value_nvlist(p2
, &attrs
) == 0);
3999 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
4003 if (nvpair_type(p1
) != nvpair_type(p2
))
4006 if (nvpair_type(p1
) == DATA_TYPE_STRING
) {
4007 char *valstr1
, *valstr2
;
4009 VERIFY(nvpair_value_string(p1
, (char **)&valstr1
) == 0);
4010 VERIFY(nvpair_value_string(p2
, (char **)&valstr2
) == 0);
4011 return (strcmp(valstr1
, valstr2
) == 0);
4013 uint64_t intval1
, intval2
;
4015 VERIFY(nvpair_value_uint64(p1
, &intval1
) == 0);
4016 VERIFY(nvpair_value_uint64(p2
, &intval2
) == 0);
4017 return (intval1
== intval2
);
4022 * Remove properties from props if they are not going to change (as determined
4023 * by comparison with origprops). Remove them from origprops as well, since we
4024 * do not need to clear or restore properties that won't change.
4027 props_reduce(nvlist_t
*props
, nvlist_t
*origprops
)
4029 nvpair_t
*pair
, *next_pair
;
4031 if (origprops
== NULL
)
4032 return; /* all props need to be received */
4034 pair
= nvlist_next_nvpair(props
, NULL
);
4035 while (pair
!= NULL
) {
4036 const char *propname
= nvpair_name(pair
);
4039 next_pair
= nvlist_next_nvpair(props
, pair
);
4041 if ((nvlist_lookup_nvpair(origprops
, propname
,
4042 &match
) != 0) || !propval_equals(pair
, match
))
4043 goto next
; /* need to set received value */
4045 /* don't clear the existing received value */
4046 (void) nvlist_remove_nvpair(origprops
, match
);
4047 /* don't bother receiving the property */
4048 (void) nvlist_remove_nvpair(props
, pair
);
4055 static boolean_t zfs_ioc_recv_inject_err
;
4060 * zc_name name of containing filesystem
4061 * zc_nvlist_src{_size} nvlist of properties to apply
4062 * zc_value name of snapshot to create
4063 * zc_string name of clone origin (if DRR_FLAG_CLONE)
4064 * zc_cookie file descriptor to recv from
4065 * zc_begin_record the BEGIN record of the stream (not byteswapped)
4066 * zc_guid force flag
4067 * zc_cleanup_fd cleanup-on-exit file descriptor
4068 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
4071 * zc_cookie number of bytes read
4072 * zc_nvlist_dst{_size} error for each unapplied received property
4073 * zc_obj zprop_errflags_t
4074 * zc_action_handle handle for this guid/ds mapping
4077 zfs_ioc_recv(zfs_cmd_t
*zc
)
4080 dmu_recv_cookie_t drc
;
4081 boolean_t force
= (boolean_t
)zc
->zc_guid
;
4084 int props_error
= 0;
4087 nvlist_t
*props
= NULL
; /* sent properties */
4088 nvlist_t
*origprops
= NULL
; /* existing properties */
4089 char *origin
= NULL
;
4091 char tofs
[ZFS_MAXNAMELEN
];
4092 boolean_t first_recvd_props
= B_FALSE
;
4094 if (dataset_namecheck(zc
->zc_value
, NULL
, NULL
) != 0 ||
4095 strchr(zc
->zc_value
, '@') == NULL
||
4096 strchr(zc
->zc_value
, '%'))
4097 return (SET_ERROR(EINVAL
));
4099 (void) strcpy(tofs
, zc
->zc_value
);
4100 tosnap
= strchr(tofs
, '@');
4103 if (zc
->zc_nvlist_src
!= NULL
&&
4104 (error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
4105 zc
->zc_iflags
, &props
)) != 0)
4112 return (SET_ERROR(EBADF
));
4115 VERIFY(nvlist_alloc(&errors
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4117 if (zc
->zc_string
[0])
4118 origin
= zc
->zc_string
;
4120 error
= dmu_recv_begin(tofs
, tosnap
,
4121 &zc
->zc_begin_record
, force
, origin
, &drc
);
4126 * Set properties before we receive the stream so that they are applied
4127 * to the new data. Note that we must call dmu_recv_stream() if
4128 * dmu_recv_begin() succeeds.
4130 if (props
!= NULL
&& !drc
.drc_newfs
) {
4131 if (spa_version(dsl_dataset_get_spa(drc
.drc_ds
)) >=
4132 SPA_VERSION_RECVD_PROPS
&&
4133 !dsl_prop_get_hasrecvd(tofs
))
4134 first_recvd_props
= B_TRUE
;
4137 * If new received properties are supplied, they are to
4138 * completely replace the existing received properties, so stash
4139 * away the existing ones.
4141 if (dsl_prop_get_received(tofs
, &origprops
) == 0) {
4142 nvlist_t
*errlist
= NULL
;
4144 * Don't bother writing a property if its value won't
4145 * change (and avoid the unnecessary security checks).
4147 * The first receive after SPA_VERSION_RECVD_PROPS is a
4148 * special case where we blow away all local properties
4151 if (!first_recvd_props
)
4152 props_reduce(props
, origprops
);
4153 if (zfs_check_clearable(tofs
, origprops
, &errlist
) != 0)
4154 (void) nvlist_merge(errors
, errlist
, 0);
4155 nvlist_free(errlist
);
4157 if (clear_received_props(tofs
, origprops
,
4158 first_recvd_props
? NULL
: props
) != 0)
4159 zc
->zc_obj
|= ZPROP_ERR_NOCLEAR
;
4161 zc
->zc_obj
|= ZPROP_ERR_NOCLEAR
;
4165 if (props
!= NULL
) {
4166 props_error
= dsl_prop_set_hasrecvd(tofs
);
4168 if (props_error
== 0) {
4169 (void) zfs_set_prop_nvlist(tofs
, ZPROP_SRC_RECEIVED
,
4174 if (zc
->zc_nvlist_dst_size
!= 0 &&
4175 (nvlist_smush(errors
, zc
->zc_nvlist_dst_size
) != 0 ||
4176 put_nvlist(zc
, errors
) != 0)) {
4178 * Caller made zc->zc_nvlist_dst less than the minimum expected
4179 * size or supplied an invalid address.
4181 props_error
= SET_ERROR(EINVAL
);
4185 error
= dmu_recv_stream(&drc
, fp
->f_vnode
, &off
, zc
->zc_cleanup_fd
,
4186 &zc
->zc_action_handle
);
4189 zfsvfs_t
*zfsvfs
= NULL
;
4191 if (getzfsvfs(tofs
, &zfsvfs
) == 0) {
4195 error
= zfs_suspend_fs(zfsvfs
);
4197 * If the suspend fails, then the recv_end will
4198 * likely also fail, and clean up after itself.
4200 end_err
= dmu_recv_end(&drc
, zfsvfs
);
4202 error
= zfs_resume_fs(zfsvfs
, tofs
);
4203 error
= error
? error
: end_err
;
4204 VFS_RELE(zfsvfs
->z_vfs
);
4206 error
= dmu_recv_end(&drc
, NULL
);
4210 zc
->zc_cookie
= off
- fp
->f_offset
;
4211 if (VOP_SEEK(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
4215 if (zfs_ioc_recv_inject_err
) {
4216 zfs_ioc_recv_inject_err
= B_FALSE
;
4221 * On error, restore the original props.
4223 if (error
!= 0 && props
!= NULL
&& !drc
.drc_newfs
) {
4224 if (clear_received_props(tofs
, props
, NULL
) != 0) {
4226 * We failed to clear the received properties.
4227 * Since we may have left a $recvd value on the
4228 * system, we can't clear the $hasrecvd flag.
4230 zc
->zc_obj
|= ZPROP_ERR_NORESTORE
;
4231 } else if (first_recvd_props
) {
4232 dsl_prop_unset_hasrecvd(tofs
);
4235 if (origprops
== NULL
&& !drc
.drc_newfs
) {
4236 /* We failed to stash the original properties. */
4237 zc
->zc_obj
|= ZPROP_ERR_NORESTORE
;
4241 * dsl_props_set() will not convert RECEIVED to LOCAL on or
4242 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
4243 * explictly if we're restoring local properties cleared in the
4244 * first new-style receive.
4246 if (origprops
!= NULL
&&
4247 zfs_set_prop_nvlist(tofs
, (first_recvd_props
?
4248 ZPROP_SRC_LOCAL
: ZPROP_SRC_RECEIVED
),
4249 origprops
, NULL
) != 0) {
4251 * We stashed the original properties but failed to
4254 zc
->zc_obj
|= ZPROP_ERR_NORESTORE
;
4259 nvlist_free(origprops
);
4260 nvlist_free(errors
);
4264 error
= props_error
;
4271 * zc_name name of snapshot to send
4272 * zc_cookie file descriptor to send stream to
4273 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
4274 * zc_sendobj objsetid of snapshot to send
4275 * zc_fromobj objsetid of incremental fromsnap (may be zero)
4276 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
4277 * output size in zc_objset_type.
4278 * zc_flags if =1, WRITE_EMBEDDED records are permitted
4281 * zc_objset_type estimated size, if zc_guid is set
4284 zfs_ioc_send(zfs_cmd_t
*zc
)
4288 boolean_t estimate
= (zc
->zc_guid
!= 0);
4289 boolean_t embedok
= (zc
->zc_flags
& 0x1);
4291 if (zc
->zc_obj
!= 0) {
4293 dsl_dataset_t
*tosnap
;
4295 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
4299 error
= dsl_dataset_hold_obj(dp
, zc
->zc_sendobj
, FTAG
, &tosnap
);
4301 dsl_pool_rele(dp
, FTAG
);
4305 if (dsl_dir_is_clone(tosnap
->ds_dir
))
4306 zc
->zc_fromobj
= tosnap
->ds_dir
->dd_phys
->dd_origin_obj
;
4307 dsl_dataset_rele(tosnap
, FTAG
);
4308 dsl_pool_rele(dp
, FTAG
);
4313 dsl_dataset_t
*tosnap
;
4314 dsl_dataset_t
*fromsnap
= NULL
;
4316 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
4320 error
= dsl_dataset_hold_obj(dp
, zc
->zc_sendobj
, FTAG
, &tosnap
);
4322 dsl_pool_rele(dp
, FTAG
);
4326 if (zc
->zc_fromobj
!= 0) {
4327 error
= dsl_dataset_hold_obj(dp
, zc
->zc_fromobj
,
4330 dsl_dataset_rele(tosnap
, FTAG
);
4331 dsl_pool_rele(dp
, FTAG
);
4336 error
= dmu_send_estimate(tosnap
, fromsnap
,
4337 &zc
->zc_objset_type
);
4339 if (fromsnap
!= NULL
)
4340 dsl_dataset_rele(fromsnap
, FTAG
);
4341 dsl_dataset_rele(tosnap
, FTAG
);
4342 dsl_pool_rele(dp
, FTAG
);
4344 file_t
*fp
= getf(zc
->zc_cookie
);
4346 return (SET_ERROR(EBADF
));
4349 error
= dmu_send_obj(zc
->zc_name
, zc
->zc_sendobj
,
4350 zc
->zc_fromobj
, embedok
, zc
->zc_cookie
, fp
->f_vnode
, &off
);
4352 if (VOP_SEEK(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
4354 releasef(zc
->zc_cookie
);
4361 * zc_name name of snapshot on which to report progress
4362 * zc_cookie file descriptor of send stream
4365 * zc_cookie number of bytes written in send stream thus far
4368 zfs_ioc_send_progress(zfs_cmd_t
*zc
)
4372 dmu_sendarg_t
*dsp
= NULL
;
4375 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
4379 error
= dsl_dataset_hold(dp
, zc
->zc_name
, FTAG
, &ds
);
4381 dsl_pool_rele(dp
, FTAG
);
4385 mutex_enter(&ds
->ds_sendstream_lock
);
4388 * Iterate over all the send streams currently active on this dataset.
4389 * If there's one which matches the specified file descriptor _and_ the
4390 * stream was started by the current process, return the progress of
4393 for (dsp
= list_head(&ds
->ds_sendstreams
); dsp
!= NULL
;
4394 dsp
= list_next(&ds
->ds_sendstreams
, dsp
)) {
4395 if (dsp
->dsa_outfd
== zc
->zc_cookie
&&
4396 dsp
->dsa_proc
== curproc
)
4401 zc
->zc_cookie
= *(dsp
->dsa_off
);
4403 error
= SET_ERROR(ENOENT
);
4405 mutex_exit(&ds
->ds_sendstream_lock
);
4406 dsl_dataset_rele(ds
, FTAG
);
4407 dsl_pool_rele(dp
, FTAG
);
4412 zfs_ioc_inject_fault(zfs_cmd_t
*zc
)
4416 error
= zio_inject_fault(zc
->zc_name
, (int)zc
->zc_guid
, &id
,
4417 &zc
->zc_inject_record
);
4420 zc
->zc_guid
= (uint64_t)id
;
4426 zfs_ioc_clear_fault(zfs_cmd_t
*zc
)
4428 return (zio_clear_fault((int)zc
->zc_guid
));
4432 zfs_ioc_inject_list_next(zfs_cmd_t
*zc
)
4434 int id
= (int)zc
->zc_guid
;
4437 error
= zio_inject_list_next(&id
, zc
->zc_name
, sizeof (zc
->zc_name
),
4438 &zc
->zc_inject_record
);
4446 zfs_ioc_error_log(zfs_cmd_t
*zc
)
4450 size_t count
= (size_t)zc
->zc_nvlist_dst_size
;
4452 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
4455 error
= spa_get_errlog(spa
, (void *)(uintptr_t)zc
->zc_nvlist_dst
,
4458 zc
->zc_nvlist_dst_size
= count
;
4460 zc
->zc_nvlist_dst_size
= spa_get_errlog_size(spa
);
4462 spa_close(spa
, FTAG
);
4468 zfs_ioc_clear(zfs_cmd_t
*zc
)
4475 * On zpool clear we also fix up missing slogs
4477 mutex_enter(&spa_namespace_lock
);
4478 spa
= spa_lookup(zc
->zc_name
);
4480 mutex_exit(&spa_namespace_lock
);
4481 return (SET_ERROR(EIO
));
4483 if (spa_get_log_state(spa
) == SPA_LOG_MISSING
) {
4484 /* we need to let spa_open/spa_load clear the chains */
4485 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
4487 spa
->spa_last_open_failed
= 0;
4488 mutex_exit(&spa_namespace_lock
);
4490 if (zc
->zc_cookie
& ZPOOL_NO_REWIND
) {
4491 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
4494 nvlist_t
*config
= NULL
;
4496 if (zc
->zc_nvlist_src
== NULL
)
4497 return (SET_ERROR(EINVAL
));
4499 if ((error
= get_nvlist(zc
->zc_nvlist_src
,
4500 zc
->zc_nvlist_src_size
, zc
->zc_iflags
, &policy
)) == 0) {
4501 error
= spa_open_rewind(zc
->zc_name
, &spa
, FTAG
,
4503 if (config
!= NULL
) {
4506 if ((err
= put_nvlist(zc
, config
)) != 0)
4508 nvlist_free(config
);
4510 nvlist_free(policy
);
4517 spa_vdev_state_enter(spa
, SCL_NONE
);
4519 if (zc
->zc_guid
== 0) {
4522 vd
= spa_lookup_by_guid(spa
, zc
->zc_guid
, B_TRUE
);
4524 (void) spa_vdev_state_exit(spa
, NULL
, ENODEV
);
4525 spa_close(spa
, FTAG
);
4526 return (SET_ERROR(ENODEV
));
4530 vdev_clear(spa
, vd
);
4532 (void) spa_vdev_state_exit(spa
, NULL
, 0);
4535 * Resume any suspended I/Os.
4537 if (zio_resume(spa
) != 0)
4538 error
= SET_ERROR(EIO
);
4540 spa_close(spa
, FTAG
);
4546 zfs_ioc_pool_reopen(zfs_cmd_t
*zc
)
4551 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
4555 spa_vdev_state_enter(spa
, SCL_NONE
);
4558 * If a resilver is already in progress then set the
4559 * spa_scrub_reopen flag to B_TRUE so that we don't restart
4560 * the scan as a side effect of the reopen. Otherwise, let
4561 * vdev_open() decided if a resilver is required.
4563 spa
->spa_scrub_reopen
= dsl_scan_resilvering(spa
->spa_dsl_pool
);
4564 vdev_reopen(spa
->spa_root_vdev
);
4565 spa
->spa_scrub_reopen
= B_FALSE
;
4567 (void) spa_vdev_state_exit(spa
, NULL
, 0);
4568 spa_close(spa
, FTAG
);
4573 * zc_name name of filesystem
4574 * zc_value name of origin snapshot
4577 * zc_string name of conflicting snapshot, if there is one
4580 zfs_ioc_promote(zfs_cmd_t
*zc
)
4585 * We don't need to unmount *all* the origin fs's snapshots, but
4588 cp
= strchr(zc
->zc_value
, '@');
4591 (void) dmu_objset_find(zc
->zc_value
,
4592 zfs_unmount_snap_cb
, NULL
, DS_FIND_SNAPSHOTS
);
4593 return (dsl_dataset_promote(zc
->zc_name
, zc
->zc_string
));
4597 * Retrieve a single {user|group}{used|quota}@... property.
4600 * zc_name name of filesystem
4601 * zc_objset_type zfs_userquota_prop_t
4602 * zc_value domain name (eg. "S-1-234-567-89")
4603 * zc_guid RID/UID/GID
4606 * zc_cookie property value
4609 zfs_ioc_userspace_one(zfs_cmd_t
*zc
)
4614 if (zc
->zc_objset_type
>= ZFS_NUM_USERQUOTA_PROPS
)
4615 return (SET_ERROR(EINVAL
));
4617 error
= zfsvfs_hold(zc
->zc_name
, FTAG
, &zfsvfs
, B_FALSE
);
4621 error
= zfs_userspace_one(zfsvfs
,
4622 zc
->zc_objset_type
, zc
->zc_value
, zc
->zc_guid
, &zc
->zc_cookie
);
4623 zfsvfs_rele(zfsvfs
, FTAG
);
4630 * zc_name name of filesystem
4631 * zc_cookie zap cursor
4632 * zc_objset_type zfs_userquota_prop_t
4633 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
4636 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
4637 * zc_cookie zap cursor
4640 zfs_ioc_userspace_many(zfs_cmd_t
*zc
)
4643 int bufsize
= zc
->zc_nvlist_dst_size
;
4646 return (SET_ERROR(ENOMEM
));
4648 int error
= zfsvfs_hold(zc
->zc_name
, FTAG
, &zfsvfs
, B_FALSE
);
4652 void *buf
= kmem_alloc(bufsize
, KM_SLEEP
);
4654 error
= zfs_userspace_many(zfsvfs
, zc
->zc_objset_type
, &zc
->zc_cookie
,
4655 buf
, &zc
->zc_nvlist_dst_size
);
4658 error
= xcopyout(buf
,
4659 (void *)(uintptr_t)zc
->zc_nvlist_dst
,
4660 zc
->zc_nvlist_dst_size
);
4662 kmem_free(buf
, bufsize
);
4663 zfsvfs_rele(zfsvfs
, FTAG
);
4670 * zc_name name of filesystem
4676 zfs_ioc_userspace_upgrade(zfs_cmd_t
*zc
)
4682 if (getzfsvfs(zc
->zc_name
, &zfsvfs
) == 0) {
4683 if (!dmu_objset_userused_enabled(zfsvfs
->z_os
)) {
4685 * If userused is not enabled, it may be because the
4686 * objset needs to be closed & reopened (to grow the
4687 * objset_phys_t). Suspend/resume the fs will do that.
4689 error
= zfs_suspend_fs(zfsvfs
);
4691 dmu_objset_refresh_ownership(zfsvfs
->z_os
,
4693 error
= zfs_resume_fs(zfsvfs
, zc
->zc_name
);
4697 error
= dmu_objset_userspace_upgrade(zfsvfs
->z_os
);
4698 VFS_RELE(zfsvfs
->z_vfs
);
4700 /* XXX kind of reading contents without owning */
4701 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
4705 error
= dmu_objset_userspace_upgrade(os
);
4706 dmu_objset_rele(os
, FTAG
);
4713 * We don't want to have a hard dependency
4714 * against some special symbols in sharefs
4715 * nfs, and smbsrv. Determine them if needed when
4716 * the first file system is shared.
4717 * Neither sharefs, nfs or smbsrv are unloadable modules.
4719 int (*znfsexport_fs
)(void *arg
);
4720 int (*zshare_fs
)(enum sharefs_sys_op
, share_t
*, uint32_t);
4721 int (*zsmbexport_fs
)(void *arg
, boolean_t add_share
);
4723 int zfs_nfsshare_inited
;
4724 int zfs_smbshare_inited
;
4726 ddi_modhandle_t nfs_mod
;
4727 ddi_modhandle_t sharefs_mod
;
4728 ddi_modhandle_t smbsrv_mod
;
4729 kmutex_t zfs_share_lock
;
4736 ASSERT(MUTEX_HELD(&zfs_share_lock
));
4737 /* Both NFS and SMB shares also require sharetab support. */
4738 if (sharefs_mod
== NULL
&& ((sharefs_mod
=
4739 ddi_modopen("fs/sharefs",
4740 KRTLD_MODE_FIRST
, &error
)) == NULL
)) {
4741 return (SET_ERROR(ENOSYS
));
4743 if (zshare_fs
== NULL
&& ((zshare_fs
=
4744 (int (*)(enum sharefs_sys_op
, share_t
*, uint32_t))
4745 ddi_modsym(sharefs_mod
, "sharefs_impl", &error
)) == NULL
)) {
4746 return (SET_ERROR(ENOSYS
));
4752 zfs_ioc_share(zfs_cmd_t
*zc
)
4757 switch (zc
->zc_share
.z_sharetype
) {
4759 case ZFS_UNSHARE_NFS
:
4760 if (zfs_nfsshare_inited
== 0) {
4761 mutex_enter(&zfs_share_lock
);
4762 if (nfs_mod
== NULL
&& ((nfs_mod
= ddi_modopen("fs/nfs",
4763 KRTLD_MODE_FIRST
, &error
)) == NULL
)) {
4764 mutex_exit(&zfs_share_lock
);
4765 return (SET_ERROR(ENOSYS
));
4767 if (znfsexport_fs
== NULL
&&
4768 ((znfsexport_fs
= (int (*)(void *))
4770 "nfs_export", &error
)) == NULL
)) {
4771 mutex_exit(&zfs_share_lock
);
4772 return (SET_ERROR(ENOSYS
));
4774 error
= zfs_init_sharefs();
4776 mutex_exit(&zfs_share_lock
);
4777 return (SET_ERROR(ENOSYS
));
4779 zfs_nfsshare_inited
= 1;
4780 mutex_exit(&zfs_share_lock
);
4784 case ZFS_UNSHARE_SMB
:
4785 if (zfs_smbshare_inited
== 0) {
4786 mutex_enter(&zfs_share_lock
);
4787 if (smbsrv_mod
== NULL
&& ((smbsrv_mod
=
4788 ddi_modopen("drv/smbsrv",
4789 KRTLD_MODE_FIRST
, &error
)) == NULL
)) {
4790 mutex_exit(&zfs_share_lock
);
4791 return (SET_ERROR(ENOSYS
));
4793 if (zsmbexport_fs
== NULL
&& ((zsmbexport_fs
=
4794 (int (*)(void *, boolean_t
))ddi_modsym(smbsrv_mod
,
4795 "smb_server_share", &error
)) == NULL
)) {
4796 mutex_exit(&zfs_share_lock
);
4797 return (SET_ERROR(ENOSYS
));
4799 error
= zfs_init_sharefs();
4801 mutex_exit(&zfs_share_lock
);
4802 return (SET_ERROR(ENOSYS
));
4804 zfs_smbshare_inited
= 1;
4805 mutex_exit(&zfs_share_lock
);
4809 return (SET_ERROR(EINVAL
));
4812 switch (zc
->zc_share
.z_sharetype
) {
4814 case ZFS_UNSHARE_NFS
:
4816 znfsexport_fs((void *)
4817 (uintptr_t)zc
->zc_share
.z_exportdata
))
4821 case ZFS_UNSHARE_SMB
:
4822 if (error
= zsmbexport_fs((void *)
4823 (uintptr_t)zc
->zc_share
.z_exportdata
,
4824 zc
->zc_share
.z_sharetype
== ZFS_SHARE_SMB
?
4831 opcode
= (zc
->zc_share
.z_sharetype
== ZFS_SHARE_NFS
||
4832 zc
->zc_share
.z_sharetype
== ZFS_SHARE_SMB
) ?
4833 SHAREFS_ADD
: SHAREFS_REMOVE
;
4836 * Add or remove share from sharetab
4838 error
= zshare_fs(opcode
,
4839 (void *)(uintptr_t)zc
->zc_share
.z_sharedata
,
4840 zc
->zc_share
.z_sharemax
);
4846 ace_t full_access
[] = {
4847 {(uid_t
)-1, ACE_ALL_PERMS
, ACE_EVERYONE
, 0}
4852 * zc_name name of containing filesystem
4853 * zc_obj object # beyond which we want next in-use object #
4856 * zc_obj next in-use object #
4859 zfs_ioc_next_obj(zfs_cmd_t
*zc
)
4861 objset_t
*os
= NULL
;
4864 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
4868 error
= dmu_object_next(os
, &zc
->zc_obj
, B_FALSE
,
4869 os
->os_dsl_dataset
->ds_phys
->ds_prev_snap_txg
);
4871 dmu_objset_rele(os
, FTAG
);
4877 * zc_name name of filesystem
4878 * zc_value prefix name for snapshot
4879 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
4882 * zc_value short name of new snapshot
4885 zfs_ioc_tmp_snapshot(zfs_cmd_t
*zc
)
4892 error
= zfs_onexit_fd_hold(zc
->zc_cleanup_fd
, &minor
);
4896 snap_name
= kmem_asprintf("%s-%016llx", zc
->zc_value
,
4897 (u_longlong_t
)ddi_get_lbolt64());
4898 hold_name
= kmem_asprintf("%%%s", zc
->zc_value
);
4900 error
= dsl_dataset_snapshot_tmp(zc
->zc_name
, snap_name
, minor
,
4903 (void) strcpy(zc
->zc_value
, snap_name
);
4906 zfs_onexit_fd_rele(zc
->zc_cleanup_fd
);
4912 * zc_name name of "to" snapshot
4913 * zc_value name of "from" snapshot
4914 * zc_cookie file descriptor to write diff data on
4917 * dmu_diff_record_t's to the file descriptor
4920 zfs_ioc_diff(zfs_cmd_t
*zc
)
4926 fp
= getf(zc
->zc_cookie
);
4928 return (SET_ERROR(EBADF
));
4932 error
= dmu_diff(zc
->zc_name
, zc
->zc_value
, fp
->f_vnode
, &off
);
4934 if (VOP_SEEK(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
4936 releasef(zc
->zc_cookie
);
4942 * Remove all ACL files in shares dir
4945 zfs_smb_acl_purge(znode_t
*dzp
)
4948 zap_attribute_t zap
;
4949 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
4952 for (zap_cursor_init(&zc
, zfsvfs
->z_os
, dzp
->z_id
);
4953 (error
= zap_cursor_retrieve(&zc
, &zap
)) == 0;
4954 zap_cursor_advance(&zc
)) {
4955 if ((error
= VOP_REMOVE(ZTOV(dzp
), zap
.za_name
, kcred
,
4959 zap_cursor_fini(&zc
);
4964 zfs_ioc_smb_acl(zfs_cmd_t
*zc
)
4968 vnode_t
*resourcevp
= NULL
;
4977 if ((error
= lookupname(zc
->zc_value
, UIO_SYSSPACE
,
4978 NO_FOLLOW
, NULL
, &vp
)) != 0)
4981 /* Now make sure mntpnt and dataset are ZFS */
4983 if (vp
->v_vfsp
->vfs_fstype
!= zfsfstype
||
4984 (strcmp((char *)refstr_value(vp
->v_vfsp
->vfs_resource
),
4985 zc
->zc_name
) != 0)) {
4987 return (SET_ERROR(EINVAL
));
4991 zfsvfs
= dzp
->z_zfsvfs
;
4995 * Create share dir if its missing.
4997 mutex_enter(&zfsvfs
->z_lock
);
4998 if (zfsvfs
->z_shares_dir
== 0) {
5001 tx
= dmu_tx_create(zfsvfs
->z_os
);
5002 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
, TRUE
,
5004 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, FALSE
, NULL
);
5005 error
= dmu_tx_assign(tx
, TXG_WAIT
);
5009 error
= zfs_create_share_dir(zfsvfs
, tx
);
5013 mutex_exit(&zfsvfs
->z_lock
);
5019 mutex_exit(&zfsvfs
->z_lock
);
5021 ASSERT(zfsvfs
->z_shares_dir
);
5022 if ((error
= zfs_zget(zfsvfs
, zfsvfs
->z_shares_dir
, &sharedir
)) != 0) {
5028 switch (zc
->zc_cookie
) {
5029 case ZFS_SMB_ACL_ADD
:
5030 vattr
.va_mask
= AT_MODE
|AT_UID
|AT_GID
|AT_TYPE
;
5031 vattr
.va_type
= VREG
;
5032 vattr
.va_mode
= S_IFREG
|0777;
5036 vsec
.vsa_mask
= VSA_ACE
;
5037 vsec
.vsa_aclentp
= &full_access
;
5038 vsec
.vsa_aclentsz
= sizeof (full_access
);
5039 vsec
.vsa_aclcnt
= 1;
5041 error
= VOP_CREATE(ZTOV(sharedir
), zc
->zc_string
,
5042 &vattr
, EXCL
, 0, &resourcevp
, kcred
, 0, NULL
, &vsec
);
5044 VN_RELE(resourcevp
);
5047 case ZFS_SMB_ACL_REMOVE
:
5048 error
= VOP_REMOVE(ZTOV(sharedir
), zc
->zc_string
, kcred
,
5052 case ZFS_SMB_ACL_RENAME
:
5053 if ((error
= get_nvlist(zc
->zc_nvlist_src
,
5054 zc
->zc_nvlist_src_size
, zc
->zc_iflags
, &nvlist
)) != 0) {
5059 if (nvlist_lookup_string(nvlist
, ZFS_SMB_ACL_SRC
, &src
) ||
5060 nvlist_lookup_string(nvlist
, ZFS_SMB_ACL_TARGET
,
5063 VN_RELE(ZTOV(sharedir
));
5065 nvlist_free(nvlist
);
5068 error
= VOP_RENAME(ZTOV(sharedir
), src
, ZTOV(sharedir
), target
,
5070 nvlist_free(nvlist
);
5073 case ZFS_SMB_ACL_PURGE
:
5074 error
= zfs_smb_acl_purge(sharedir
);
5078 error
= SET_ERROR(EINVAL
);
5083 VN_RELE(ZTOV(sharedir
));
5092 * "holds" -> { snapname -> holdname (string), ... }
5093 * (optional) "cleanup_fd" -> fd (int32)
5097 * snapname -> error value (int32)
5103 zfs_ioc_hold(const char *pool
, nvlist_t
*args
, nvlist_t
*errlist
)
5106 int cleanup_fd
= -1;
5110 error
= nvlist_lookup_nvlist(args
, "holds", &holds
);
5112 return (SET_ERROR(EINVAL
));
5114 if (nvlist_lookup_int32(args
, "cleanup_fd", &cleanup_fd
) == 0) {
5115 error
= zfs_onexit_fd_hold(cleanup_fd
, &minor
);
5120 error
= dsl_dataset_user_hold(holds
, minor
, errlist
);
5122 zfs_onexit_fd_rele(cleanup_fd
);
5127 * innvl is not used.
5130 * holdname -> time added (uint64 seconds since epoch)
5136 zfs_ioc_get_holds(const char *snapname
, nvlist_t
*args
, nvlist_t
*outnvl
)
5138 return (dsl_dataset_get_holds(snapname
, outnvl
));
5143 * snapname -> { holdname, ... }
5148 * snapname -> error value (int32)
5154 zfs_ioc_release(const char *pool
, nvlist_t
*holds
, nvlist_t
*errlist
)
5156 return (dsl_dataset_user_release(holds
, errlist
));
5161 * zc_name name of new filesystem or snapshot
5162 * zc_value full name of old snapshot
5165 * zc_cookie space in bytes
5166 * zc_objset_type compressed space in bytes
5167 * zc_perm_action uncompressed space in bytes
5170 zfs_ioc_space_written(zfs_cmd_t
*zc
)
5174 dsl_dataset_t
*new, *old
;
5176 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
5179 error
= dsl_dataset_hold(dp
, zc
->zc_name
, FTAG
, &new);
5181 dsl_pool_rele(dp
, FTAG
);
5184 error
= dsl_dataset_hold(dp
, zc
->zc_value
, FTAG
, &old
);
5186 dsl_dataset_rele(new, FTAG
);
5187 dsl_pool_rele(dp
, FTAG
);
5191 error
= dsl_dataset_space_written(old
, new, &zc
->zc_cookie
,
5192 &zc
->zc_objset_type
, &zc
->zc_perm_action
);
5193 dsl_dataset_rele(old
, FTAG
);
5194 dsl_dataset_rele(new, FTAG
);
5195 dsl_pool_rele(dp
, FTAG
);
5201 * "firstsnap" -> snapshot name
5205 * "used" -> space in bytes
5206 * "compressed" -> compressed space in bytes
5207 * "uncompressed" -> uncompressed space in bytes
5211 zfs_ioc_space_snaps(const char *lastsnap
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
5215 dsl_dataset_t
*new, *old
;
5217 uint64_t used
, comp
, uncomp
;
5219 if (nvlist_lookup_string(innvl
, "firstsnap", &firstsnap
) != 0)
5220 return (SET_ERROR(EINVAL
));
5222 error
= dsl_pool_hold(lastsnap
, FTAG
, &dp
);
5226 error
= dsl_dataset_hold(dp
, lastsnap
, FTAG
, &new);
5228 dsl_pool_rele(dp
, FTAG
);
5231 error
= dsl_dataset_hold(dp
, firstsnap
, FTAG
, &old
);
5233 dsl_dataset_rele(new, FTAG
);
5234 dsl_pool_rele(dp
, FTAG
);
5238 error
= dsl_dataset_space_wouldfree(old
, new, &used
, &comp
, &uncomp
);
5239 dsl_dataset_rele(old
, FTAG
);
5240 dsl_dataset_rele(new, FTAG
);
5241 dsl_pool_rele(dp
, FTAG
);
5242 fnvlist_add_uint64(outnvl
, "used", used
);
5243 fnvlist_add_uint64(outnvl
, "compressed", comp
);
5244 fnvlist_add_uint64(outnvl
, "uncompressed", uncomp
);
5250 * "fd" -> file descriptor to write stream to (int32)
5251 * (optional) "fromsnap" -> full snap name to send an incremental from
5252 * (optional) "embedok" -> (value ignored)
5253 * presence indicates DRR_WRITE_EMBEDDED records are permitted
5260 zfs_ioc_send_new(const char *snapname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
5264 char *fromname
= NULL
;
5268 error
= nvlist_lookup_int32(innvl
, "fd", &fd
);
5270 return (SET_ERROR(EINVAL
));
5272 (void) nvlist_lookup_string(innvl
, "fromsnap", &fromname
);
5274 embedok
= nvlist_exists(innvl
, "embedok");
5276 file_t
*fp
= getf(fd
);
5278 return (SET_ERROR(EBADF
));
5281 error
= dmu_send(snapname
, fromname
, embedok
, fd
, fp
->f_vnode
, &off
);
5283 if (VOP_SEEK(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
5290 * Determine approximately how large a zfs send stream will be -- the number
5291 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
5294 * (optional) "fromsnap" -> full snap name to send an incremental from
5298 * "space" -> bytes of space (uint64)
5302 zfs_ioc_send_space(const char *snapname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
5305 dsl_dataset_t
*fromsnap
= NULL
;
5306 dsl_dataset_t
*tosnap
;
5311 error
= dsl_pool_hold(snapname
, FTAG
, &dp
);
5315 error
= dsl_dataset_hold(dp
, snapname
, FTAG
, &tosnap
);
5317 dsl_pool_rele(dp
, FTAG
);
5321 error
= nvlist_lookup_string(innvl
, "fromsnap", &fromname
);
5323 error
= dsl_dataset_hold(dp
, fromname
, FTAG
, &fromsnap
);
5325 dsl_dataset_rele(tosnap
, FTAG
);
5326 dsl_pool_rele(dp
, FTAG
);
5331 error
= dmu_send_estimate(tosnap
, fromsnap
, &space
);
5332 fnvlist_add_uint64(outnvl
, "space", space
);
5334 if (fromsnap
!= NULL
)
5335 dsl_dataset_rele(fromsnap
, FTAG
);
5336 dsl_dataset_rele(tosnap
, FTAG
);
5337 dsl_pool_rele(dp
, FTAG
);
5342 static zfs_ioc_vec_t zfs_ioc_vec
[ZFS_IOC_LAST
- ZFS_IOC_FIRST
];
5345 zfs_ioctl_register_legacy(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5346 zfs_secpolicy_func_t
*secpolicy
, zfs_ioc_namecheck_t namecheck
,
5347 boolean_t log_history
, zfs_ioc_poolcheck_t pool_check
)
5349 zfs_ioc_vec_t
*vec
= &zfs_ioc_vec
[ioc
- ZFS_IOC_FIRST
];
5351 ASSERT3U(ioc
, >=, ZFS_IOC_FIRST
);
5352 ASSERT3U(ioc
, <, ZFS_IOC_LAST
);
5353 ASSERT3P(vec
->zvec_legacy_func
, ==, NULL
);
5354 ASSERT3P(vec
->zvec_func
, ==, NULL
);
5356 vec
->zvec_legacy_func
= func
;
5357 vec
->zvec_secpolicy
= secpolicy
;
5358 vec
->zvec_namecheck
= namecheck
;
5359 vec
->zvec_allow_log
= log_history
;
5360 vec
->zvec_pool_check
= pool_check
;
5364 * See the block comment at the beginning of this file for details on
5365 * each argument to this function.
5368 zfs_ioctl_register(const char *name
, zfs_ioc_t ioc
, zfs_ioc_func_t
*func
,
5369 zfs_secpolicy_func_t
*secpolicy
, zfs_ioc_namecheck_t namecheck
,
5370 zfs_ioc_poolcheck_t pool_check
, boolean_t smush_outnvlist
,
5371 boolean_t allow_log
)
5373 zfs_ioc_vec_t
*vec
= &zfs_ioc_vec
[ioc
- ZFS_IOC_FIRST
];
5375 ASSERT3U(ioc
, >=, ZFS_IOC_FIRST
);
5376 ASSERT3U(ioc
, <, ZFS_IOC_LAST
);
5377 ASSERT3P(vec
->zvec_legacy_func
, ==, NULL
);
5378 ASSERT3P(vec
->zvec_func
, ==, NULL
);
5380 /* if we are logging, the name must be valid */
5381 ASSERT(!allow_log
|| namecheck
!= NO_NAME
);
5383 vec
->zvec_name
= name
;
5384 vec
->zvec_func
= func
;
5385 vec
->zvec_secpolicy
= secpolicy
;
5386 vec
->zvec_namecheck
= namecheck
;
5387 vec
->zvec_pool_check
= pool_check
;
5388 vec
->zvec_smush_outnvlist
= smush_outnvlist
;
5389 vec
->zvec_allow_log
= allow_log
;
5393 zfs_ioctl_register_pool(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5394 zfs_secpolicy_func_t
*secpolicy
, boolean_t log_history
,
5395 zfs_ioc_poolcheck_t pool_check
)
5397 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5398 POOL_NAME
, log_history
, pool_check
);
5402 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5403 zfs_secpolicy_func_t
*secpolicy
, zfs_ioc_poolcheck_t pool_check
)
5405 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5406 DATASET_NAME
, B_FALSE
, pool_check
);
5410 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
)
5412 zfs_ioctl_register_legacy(ioc
, func
, zfs_secpolicy_config
,
5413 POOL_NAME
, B_TRUE
, POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
5417 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5418 zfs_secpolicy_func_t
*secpolicy
)
5420 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5421 NO_NAME
, B_FALSE
, POOL_CHECK_NONE
);
5425 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc
,
5426 zfs_ioc_legacy_func_t
*func
, zfs_secpolicy_func_t
*secpolicy
)
5428 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5429 DATASET_NAME
, B_FALSE
, POOL_CHECK_SUSPENDED
);
5433 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
)
5435 zfs_ioctl_register_dataset_read_secpolicy(ioc
, func
,
5436 zfs_secpolicy_read
);
5440 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5441 zfs_secpolicy_func_t
*secpolicy
)
5443 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5444 DATASET_NAME
, B_TRUE
, POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
5448 zfs_ioctl_init(void)
5450 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT
,
5451 zfs_ioc_snapshot
, zfs_secpolicy_snapshot
, POOL_NAME
,
5452 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5454 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY
,
5455 zfs_ioc_log_history
, zfs_secpolicy_log_history
, NO_NAME
,
5456 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_FALSE
, B_FALSE
);
5458 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS
,
5459 zfs_ioc_space_snaps
, zfs_secpolicy_read
, DATASET_NAME
,
5460 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
5462 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW
,
5463 zfs_ioc_send_new
, zfs_secpolicy_send_new
, DATASET_NAME
,
5464 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
5466 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE
,
5467 zfs_ioc_send_space
, zfs_secpolicy_read
, DATASET_NAME
,
5468 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
5470 zfs_ioctl_register("create", ZFS_IOC_CREATE
,
5471 zfs_ioc_create
, zfs_secpolicy_create_clone
, DATASET_NAME
,
5472 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5474 zfs_ioctl_register("clone", ZFS_IOC_CLONE
,
5475 zfs_ioc_clone
, zfs_secpolicy_create_clone
, DATASET_NAME
,
5476 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5478 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS
,
5479 zfs_ioc_destroy_snaps
, zfs_secpolicy_destroy_snaps
, POOL_NAME
,
5480 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5482 zfs_ioctl_register("hold", ZFS_IOC_HOLD
,
5483 zfs_ioc_hold
, zfs_secpolicy_hold
, POOL_NAME
,
5484 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5485 zfs_ioctl_register("release", ZFS_IOC_RELEASE
,
5486 zfs_ioc_release
, zfs_secpolicy_release
, POOL_NAME
,
5487 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5489 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS
,
5490 zfs_ioc_get_holds
, zfs_secpolicy_read
, DATASET_NAME
,
5491 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
5493 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK
,
5494 zfs_ioc_rollback
, zfs_secpolicy_rollback
, DATASET_NAME
,
5495 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_FALSE
, B_TRUE
);
5497 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK
,
5498 zfs_ioc_bookmark
, zfs_secpolicy_bookmark
, POOL_NAME
,
5499 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5501 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS
,
5502 zfs_ioc_get_bookmarks
, zfs_secpolicy_read
, DATASET_NAME
,
5503 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
5505 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS
,
5506 zfs_ioc_destroy_bookmarks
, zfs_secpolicy_destroy_bookmarks
,
5508 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5510 /* IOCTLS that use the legacy function signature */
5512 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE
, zfs_ioc_pool_freeze
,
5513 zfs_secpolicy_config
, NO_NAME
, B_FALSE
, POOL_CHECK_READONLY
);
5515 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE
, zfs_ioc_pool_create
,
5516 zfs_secpolicy_config
, B_TRUE
, POOL_CHECK_NONE
);
5517 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN
,
5519 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE
,
5520 zfs_ioc_pool_upgrade
);
5521 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD
,
5523 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE
,
5524 zfs_ioc_vdev_remove
);
5525 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE
,
5526 zfs_ioc_vdev_set_state
);
5527 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH
,
5528 zfs_ioc_vdev_attach
);
5529 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH
,
5530 zfs_ioc_vdev_detach
);
5531 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH
,
5532 zfs_ioc_vdev_setpath
);
5533 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU
,
5534 zfs_ioc_vdev_setfru
);
5535 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS
,
5536 zfs_ioc_pool_set_props
);
5537 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT
,
5538 zfs_ioc_vdev_split
);
5539 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID
,
5540 zfs_ioc_pool_reguid
);
5542 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS
,
5543 zfs_ioc_pool_configs
, zfs_secpolicy_none
);
5544 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT
,
5545 zfs_ioc_pool_tryimport
, zfs_secpolicy_config
);
5546 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT
,
5547 zfs_ioc_inject_fault
, zfs_secpolicy_inject
);
5548 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT
,
5549 zfs_ioc_clear_fault
, zfs_secpolicy_inject
);
5550 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT
,
5551 zfs_ioc_inject_list_next
, zfs_secpolicy_inject
);
5554 * pool destroy, and export don't log the history as part of
5555 * zfsdev_ioctl, but rather zfs_ioc_pool_export
5556 * does the logging of those commands.
5558 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY
, zfs_ioc_pool_destroy
,
5559 zfs_secpolicy_config
, B_FALSE
, POOL_CHECK_NONE
);
5560 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT
, zfs_ioc_pool_export
,
5561 zfs_secpolicy_config
, B_FALSE
, POOL_CHECK_NONE
);
5563 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS
, zfs_ioc_pool_stats
,
5564 zfs_secpolicy_read
, B_FALSE
, POOL_CHECK_NONE
);
5565 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS
, zfs_ioc_pool_get_props
,
5566 zfs_secpolicy_read
, B_FALSE
, POOL_CHECK_NONE
);
5568 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG
, zfs_ioc_error_log
,
5569 zfs_secpolicy_inject
, B_FALSE
, POOL_CHECK_SUSPENDED
);
5570 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME
,
5571 zfs_ioc_dsobj_to_dsname
,
5572 zfs_secpolicy_diff
, B_FALSE
, POOL_CHECK_SUSPENDED
);
5573 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY
,
5574 zfs_ioc_pool_get_history
,
5575 zfs_secpolicy_config
, B_FALSE
, POOL_CHECK_SUSPENDED
);
5577 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT
, zfs_ioc_pool_import
,
5578 zfs_secpolicy_config
, B_TRUE
, POOL_CHECK_NONE
);
5580 zfs_ioctl_register_pool(ZFS_IOC_CLEAR
, zfs_ioc_clear
,
5581 zfs_secpolicy_config
, B_TRUE
, POOL_CHECK_NONE
);
5582 zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN
, zfs_ioc_pool_reopen
,
5583 zfs_secpolicy_config
, B_TRUE
, POOL_CHECK_SUSPENDED
);
5585 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN
,
5586 zfs_ioc_space_written
);
5587 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS
,
5588 zfs_ioc_objset_recvd_props
);
5589 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ
,
5591 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL
,
5593 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS
,
5594 zfs_ioc_objset_stats
);
5595 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS
,
5596 zfs_ioc_objset_zplprops
);
5597 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT
,
5598 zfs_ioc_dataset_list_next
);
5599 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT
,
5600 zfs_ioc_snapshot_list_next
);
5601 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS
,
5602 zfs_ioc_send_progress
);
5604 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF
,
5605 zfs_ioc_diff
, zfs_secpolicy_diff
);
5606 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS
,
5607 zfs_ioc_obj_to_stats
, zfs_secpolicy_diff
);
5608 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH
,
5609 zfs_ioc_obj_to_path
, zfs_secpolicy_diff
);
5610 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE
,
5611 zfs_ioc_userspace_one
, zfs_secpolicy_userspace_one
);
5612 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY
,
5613 zfs_ioc_userspace_many
, zfs_secpolicy_userspace_many
);
5614 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND
,
5615 zfs_ioc_send
, zfs_secpolicy_send
);
5617 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP
, zfs_ioc_set_prop
,
5618 zfs_secpolicy_none
);
5619 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY
, zfs_ioc_destroy
,
5620 zfs_secpolicy_destroy
);
5621 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME
, zfs_ioc_rename
,
5622 zfs_secpolicy_rename
);
5623 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV
, zfs_ioc_recv
,
5624 zfs_secpolicy_recv
);
5625 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE
, zfs_ioc_promote
,
5626 zfs_secpolicy_promote
);
5627 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP
,
5628 zfs_ioc_inherit_prop
, zfs_secpolicy_inherit_prop
);
5629 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL
, zfs_ioc_set_fsacl
,
5630 zfs_secpolicy_set_fsacl
);
5632 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE
, zfs_ioc_share
,
5633 zfs_secpolicy_share
, POOL_CHECK_NONE
);
5634 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL
, zfs_ioc_smb_acl
,
5635 zfs_secpolicy_smb_acl
, POOL_CHECK_NONE
);
5636 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE
,
5637 zfs_ioc_userspace_upgrade
, zfs_secpolicy_userspace_upgrade
,
5638 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
5639 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT
,
5640 zfs_ioc_tmp_snapshot
, zfs_secpolicy_tmp_snapshot
,
5641 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
5645 pool_status_check(const char *name
, zfs_ioc_namecheck_t type
,
5646 zfs_ioc_poolcheck_t check
)
5651 ASSERT(type
== POOL_NAME
|| type
== DATASET_NAME
);
5653 if (check
& POOL_CHECK_NONE
)
5656 error
= spa_open(name
, &spa
, FTAG
);
5658 if ((check
& POOL_CHECK_SUSPENDED
) && spa_suspended(spa
))
5659 error
= SET_ERROR(EAGAIN
);
5660 else if ((check
& POOL_CHECK_READONLY
) && !spa_writeable(spa
))
5661 error
= SET_ERROR(EROFS
);
5662 spa_close(spa
, FTAG
);
5668 * Find a free minor number.
5671 zfsdev_minor_alloc(void)
5673 static minor_t last_minor
;
5676 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
5678 for (m
= last_minor
+ 1; m
!= last_minor
; m
++) {
5679 if (m
> ZFSDEV_MAX_MINOR
)
5681 if (ddi_get_soft_state(zfsdev_state
, m
) == NULL
) {
5691 zfs_ctldev_init(dev_t
*devp
)
5694 zfs_soft_state_t
*zs
;
5696 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
5697 ASSERT(getminor(*devp
) == 0);
5699 minor
= zfsdev_minor_alloc();
5701 return (SET_ERROR(ENXIO
));
5703 if (ddi_soft_state_zalloc(zfsdev_state
, minor
) != DDI_SUCCESS
)
5704 return (SET_ERROR(EAGAIN
));
5706 *devp
= makedevice(getemajor(*devp
), minor
);
5708 zs
= ddi_get_soft_state(zfsdev_state
, minor
);
5709 zs
->zss_type
= ZSST_CTLDEV
;
5710 zfs_onexit_init((zfs_onexit_t
**)&zs
->zss_data
);
5716 zfs_ctldev_destroy(zfs_onexit_t
*zo
, minor_t minor
)
5718 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
5720 zfs_onexit_destroy(zo
);
5721 ddi_soft_state_free(zfsdev_state
, minor
);
5725 zfsdev_get_soft_state(minor_t minor
, enum zfs_soft_state_type which
)
5727 zfs_soft_state_t
*zp
;
5729 zp
= ddi_get_soft_state(zfsdev_state
, minor
);
5730 if (zp
== NULL
|| zp
->zss_type
!= which
)
5733 return (zp
->zss_data
);
5737 zfsdev_open(dev_t
*devp
, int flag
, int otyp
, cred_t
*cr
)
5741 if (getminor(*devp
) != 0)
5742 return (zvol_open(devp
, flag
, otyp
, cr
));
5744 /* This is the control device. Allocate a new minor if requested. */
5746 mutex_enter(&zfsdev_state_lock
);
5747 error
= zfs_ctldev_init(devp
);
5748 mutex_exit(&zfsdev_state_lock
);
5755 zfsdev_close(dev_t dev
, int flag
, int otyp
, cred_t
*cr
)
5758 minor_t minor
= getminor(dev
);
5763 mutex_enter(&zfsdev_state_lock
);
5764 zo
= zfsdev_get_soft_state(minor
, ZSST_CTLDEV
);
5766 mutex_exit(&zfsdev_state_lock
);
5767 return (zvol_close(dev
, flag
, otyp
, cr
));
5769 zfs_ctldev_destroy(zo
, minor
);
5770 mutex_exit(&zfsdev_state_lock
);
5776 zfsdev_ioctl(dev_t dev
, int cmd
, intptr_t arg
, int flag
, cred_t
*cr
, int *rvalp
)
5781 minor_t minor
= getminor(dev
);
5782 const zfs_ioc_vec_t
*vec
;
5783 char *saved_poolname
= NULL
;
5784 nvlist_t
*innvl
= NULL
;
5787 zfsdev_get_soft_state(minor
, ZSST_CTLDEV
) == NULL
)
5788 return (zvol_ioctl(dev
, cmd
, arg
, flag
, cr
, rvalp
));
5790 vecnum
= cmd
- ZFS_IOC_FIRST
;
5791 ASSERT3U(getmajor(dev
), ==, ddi_driver_major(zfs_dip
));
5793 if (vecnum
>= sizeof (zfs_ioc_vec
) / sizeof (zfs_ioc_vec
[0]))
5794 return (SET_ERROR(EINVAL
));
5795 vec
= &zfs_ioc_vec
[vecnum
];
5797 zc
= kmem_zalloc(sizeof (zfs_cmd_t
), KM_SLEEP
);
5799 error
= ddi_copyin((void *)arg
, zc
, sizeof (zfs_cmd_t
), flag
);
5801 error
= SET_ERROR(EFAULT
);
5805 zc
->zc_iflags
= flag
& FKIOCTL
;
5806 if (zc
->zc_nvlist_src_size
!= 0) {
5807 error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
5808 zc
->zc_iflags
, &innvl
);
5814 * Ensure that all pool/dataset names are valid before we pass down to
5817 zc
->zc_name
[sizeof (zc
->zc_name
) - 1] = '\0';
5818 switch (vec
->zvec_namecheck
) {
5820 if (pool_namecheck(zc
->zc_name
, NULL
, NULL
) != 0)
5821 error
= SET_ERROR(EINVAL
);
5823 error
= pool_status_check(zc
->zc_name
,
5824 vec
->zvec_namecheck
, vec
->zvec_pool_check
);
5828 if (dataset_namecheck(zc
->zc_name
, NULL
, NULL
) != 0)
5829 error
= SET_ERROR(EINVAL
);
5831 error
= pool_status_check(zc
->zc_name
,
5832 vec
->zvec_namecheck
, vec
->zvec_pool_check
);
5840 if (error
== 0 && !(flag
& FKIOCTL
))
5841 error
= vec
->zvec_secpolicy(zc
, innvl
, cr
);
5846 /* legacy ioctls can modify zc_name */
5847 len
= strcspn(zc
->zc_name
, "/@#") + 1;
5848 saved_poolname
= kmem_alloc(len
, KM_SLEEP
);
5849 (void) strlcpy(saved_poolname
, zc
->zc_name
, len
);
5851 if (vec
->zvec_func
!= NULL
) {
5855 nvlist_t
*lognv
= NULL
;
5857 ASSERT(vec
->zvec_legacy_func
== NULL
);
5860 * Add the innvl to the lognv before calling the func,
5861 * in case the func changes the innvl.
5863 if (vec
->zvec_allow_log
) {
5864 lognv
= fnvlist_alloc();
5865 fnvlist_add_string(lognv
, ZPOOL_HIST_IOCTL
,
5867 if (!nvlist_empty(innvl
)) {
5868 fnvlist_add_nvlist(lognv
, ZPOOL_HIST_INPUT_NVL
,
5873 outnvl
= fnvlist_alloc();
5874 error
= vec
->zvec_func(zc
->zc_name
, innvl
, outnvl
);
5876 if (error
== 0 && vec
->zvec_allow_log
&&
5877 spa_open(zc
->zc_name
, &spa
, FTAG
) == 0) {
5878 if (!nvlist_empty(outnvl
)) {
5879 fnvlist_add_nvlist(lognv
, ZPOOL_HIST_OUTPUT_NVL
,
5882 (void) spa_history_log_nvl(spa
, lognv
);
5883 spa_close(spa
, FTAG
);
5885 fnvlist_free(lognv
);
5887 if (!nvlist_empty(outnvl
) || zc
->zc_nvlist_dst_size
!= 0) {
5889 if (vec
->zvec_smush_outnvlist
) {
5890 smusherror
= nvlist_smush(outnvl
,
5891 zc
->zc_nvlist_dst_size
);
5893 if (smusherror
== 0)
5894 puterror
= put_nvlist(zc
, outnvl
);
5900 nvlist_free(outnvl
);
5902 error
= vec
->zvec_legacy_func(zc
);
5907 rc
= ddi_copyout(zc
, (void *)arg
, sizeof (zfs_cmd_t
), flag
);
5908 if (error
== 0 && rc
!= 0)
5909 error
= SET_ERROR(EFAULT
);
5910 if (error
== 0 && vec
->zvec_allow_log
) {
5911 char *s
= tsd_get(zfs_allow_log_key
);
5914 (void) tsd_set(zfs_allow_log_key
, saved_poolname
);
5916 if (saved_poolname
!= NULL
)
5917 strfree(saved_poolname
);
5920 kmem_free(zc
, sizeof (zfs_cmd_t
));
5925 zfs_attach(dev_info_t
*dip
, ddi_attach_cmd_t cmd
)
5927 if (cmd
!= DDI_ATTACH
)
5928 return (DDI_FAILURE
);
5930 if (ddi_create_minor_node(dip
, "zfs", S_IFCHR
, 0,
5931 DDI_PSEUDO
, 0) == DDI_FAILURE
)
5932 return (DDI_FAILURE
);
5936 ddi_report_dev(dip
);
5938 return (DDI_SUCCESS
);
5942 zfs_detach(dev_info_t
*dip
, ddi_detach_cmd_t cmd
)
5944 if (spa_busy() || zfs_busy() || zvol_busy())
5945 return (DDI_FAILURE
);
5947 if (cmd
!= DDI_DETACH
)
5948 return (DDI_FAILURE
);
5952 ddi_prop_remove_all(dip
);
5953 ddi_remove_minor_node(dip
, NULL
);
5955 return (DDI_SUCCESS
);
5960 zfs_info(dev_info_t
*dip
, ddi_info_cmd_t infocmd
, void *arg
, void **result
)
5963 case DDI_INFO_DEVT2DEVINFO
:
5965 return (DDI_SUCCESS
);
5967 case DDI_INFO_DEVT2INSTANCE
:
5968 *result
= (void *)0;
5969 return (DDI_SUCCESS
);
5972 return (DDI_FAILURE
);
5976 * OK, so this is a little weird.
5978 * /dev/zfs is the control node, i.e. minor 0.
5979 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
5981 * /dev/zfs has basically nothing to do except serve up ioctls,
5982 * so most of the standard driver entry points are in zvol.c.
5984 static struct cb_ops zfs_cb_ops
= {
5985 zfsdev_open
, /* open */
5986 zfsdev_close
, /* close */
5987 zvol_strategy
, /* strategy */
5989 zvol_dump
, /* dump */
5990 zvol_read
, /* read */
5991 zvol_write
, /* write */
5992 zfsdev_ioctl
, /* ioctl */
5996 nochpoll
, /* poll */
5997 ddi_prop_op
, /* prop_op */
5998 NULL
, /* streamtab */
5999 D_NEW
| D_MP
| D_64BIT
, /* Driver compatibility flag */
6000 CB_REV
, /* version */
6001 nodev
, /* async read */
6002 nodev
, /* async write */
6005 static struct dev_ops zfs_dev_ops
= {
6006 DEVO_REV
, /* version */
6008 zfs_info
, /* info */
6009 nulldev
, /* identify */
6010 nulldev
, /* probe */
6011 zfs_attach
, /* attach */
6012 zfs_detach
, /* detach */
6014 &zfs_cb_ops
, /* driver operations */
6015 NULL
, /* no bus operations */
6017 ddi_quiesce_not_needed
, /* quiesce */
6020 static struct modldrv zfs_modldrv
= {
6026 static struct modlinkage modlinkage
= {
6028 (void *)&zfs_modlfs
,
6029 (void *)&zfs_modldrv
,
6034 zfs_allow_log_destroy(void *arg
)
6036 char *poolname
= arg
;
6045 spa_init(FREAD
| FWRITE
);
6050 if ((error
= mod_install(&modlinkage
)) != 0) {
6057 tsd_create(&zfs_fsyncer_key
, NULL
);
6058 tsd_create(&rrw_tsd_key
, rrw_tsd_destroy
);
6059 tsd_create(&zfs_allow_log_key
, zfs_allow_log_destroy
);
6061 error
= ldi_ident_from_mod(&modlinkage
, &zfs_li
);
6063 mutex_init(&zfs_share_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
6073 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled
)
6074 return (SET_ERROR(EBUSY
));
6076 if ((error
= mod_remove(&modlinkage
)) != 0)
6082 if (zfs_nfsshare_inited
)
6083 (void) ddi_modclose(nfs_mod
);
6084 if (zfs_smbshare_inited
)
6085 (void) ddi_modclose(smbsrv_mod
);
6086 if (zfs_nfsshare_inited
|| zfs_smbshare_inited
)
6087 (void) ddi_modclose(sharefs_mod
);
6089 tsd_destroy(&zfs_fsyncer_key
);
6090 ldi_ident_release(zfs_li
);
6092 mutex_destroy(&zfs_share_lock
);
6098 _info(struct modinfo
*modinfop
)
6100 return (mod_info(&modlinkage
, modinfop
));