4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Portions Copyright 2011 Martin Matuska
25 * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
26 * Portions Copyright 2012 Pawel Jakub Dawidek <pawel@dawidek.net>
27 * Copyright (c) 2014, 2016 Joyent, Inc. All rights reserved.
28 * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
29 * Copyright (c) 2014, Joyent, Inc. All rights reserved.
30 * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
31 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
32 * Copyright (c) 2013 Steven Hartland. All rights reserved.
33 * Copyright (c) 2014 Integros [integros.com]
34 * Copyright 2016 Toomas Soome <tsoome@me.com>
35 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
36 * Copyright (c) 2018, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
37 * Copyright 2017 RackTop Systems.
38 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
39 * Copyright (c) 2019 Datto Inc.
45 * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
46 * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
48 * There are two ways that we handle ioctls: the legacy way where almost
49 * all of the logic is in the ioctl callback, and the new way where most
50 * of the marshalling is handled in the common entry point, zfsdev_ioctl().
52 * Non-legacy ioctls should be registered by calling
53 * zfs_ioctl_register() from zfs_ioctl_init(). The ioctl is invoked
54 * from userland by lzc_ioctl().
56 * The registration arguments are as follows:
59 * The name of the ioctl. This is used for history logging. If the
60 * ioctl returns successfully (the callback returns 0), and allow_log
61 * is true, then a history log entry will be recorded with the input &
62 * output nvlists. The log entry can be printed with "zpool history -i".
65 * The ioctl request number, which userland will pass to ioctl(2).
66 * We want newer versions of libzfs and libzfs_core to run against
67 * existing zfs kernel modules (i.e. a deferred reboot after an update).
68 * Therefore the ioctl numbers cannot change from release to release.
70 * zfs_secpolicy_func_t *secpolicy
71 * This function will be called before the zfs_ioc_func_t, to
72 * determine if this operation is permitted. It should return EPERM
73 * on failure, and 0 on success. Checks include determining if the
74 * dataset is visible in this zone, and if the user has either all
75 * zfs privileges in the zone (SYS_MOUNT), or has been granted permission
76 * to do this operation on this dataset with "zfs allow".
78 * zfs_ioc_namecheck_t namecheck
79 * This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
80 * name, a dataset name, or nothing. If the name is not well-formed,
81 * the ioctl will fail and the callback will not be called.
82 * Therefore, the callback can assume that the name is well-formed
83 * (e.g. is null-terminated, doesn't have more than one '@' character,
84 * doesn't have invalid characters).
86 * zfs_ioc_poolcheck_t pool_check
87 * This specifies requirements on the pool state. If the pool does
88 * not meet them (is suspended or is readonly), the ioctl will fail
89 * and the callback will not be called. If any checks are specified
90 * (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
91 * Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
92 * POOL_CHECK_READONLY).
94 * zfs_ioc_key_t *nvl_keys
95 * The list of expected/allowable innvl input keys. This list is used
96 * to validate the nvlist input to the ioctl.
98 * boolean_t smush_outnvlist
99 * If smush_outnvlist is true, then the output is presumed to be a
100 * list of errors, and it will be "smushed" down to fit into the
101 * caller's buffer, by removing some entries and replacing them with a
102 * single "N_MORE_ERRORS" entry indicating how many were removed. See
103 * nvlist_smush() for details. If smush_outnvlist is false, and the
104 * outnvlist does not fit into the userland-provided buffer, then the
105 * ioctl will fail with ENOMEM.
107 * zfs_ioc_func_t *func
108 * The callback function that will perform the operation.
110 * The callback should return 0 on success, or an error number on
111 * failure. If the function fails, the userland ioctl will return -1,
112 * and errno will be set to the callback's return value. The callback
113 * will be called with the following arguments:
116 * The name of the pool or dataset to operate on, from
117 * zfs_cmd_t:zc_name. The 'namecheck' argument specifies the
118 * expected type (pool, dataset, or none).
121 * The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src. Or
122 * NULL if no input nvlist was provided. Changes to this nvlist are
123 * ignored. If the input nvlist could not be deserialized, the
124 * ioctl will fail and the callback will not be called.
127 * The output nvlist, initially empty. The callback can fill it in,
128 * and it will be returned to userland by serializing it into
129 * zfs_cmd_t:zc_nvlist_dst. If it is non-empty, and serialization
130 * fails (e.g. because the caller didn't supply a large enough
131 * buffer), then the overall ioctl will fail. See the
132 * 'smush_nvlist' argument above for additional behaviors.
134 * There are two typical uses of the output nvlist:
135 * - To return state, e.g. property values. In this case,
136 * smush_outnvlist should be false. If the buffer was not large
137 * enough, the caller will reallocate a larger buffer and try
140 * - To return multiple errors from an ioctl which makes on-disk
141 * changes. In this case, smush_outnvlist should be true.
142 * Ioctls which make on-disk modifications should generally not
143 * use the outnvl if they succeed, because the caller can not
144 * distinguish between the operation failing, and
145 * deserialization failing.
147 * IOCTL Interface Errors
149 * The following ioctl input errors can be returned:
150 * ZFS_ERR_IOC_CMD_UNAVAIL the ioctl number is not supported by kernel
151 * ZFS_ERR_IOC_ARG_UNAVAIL an input argument is not supported by kernel
152 * ZFS_ERR_IOC_ARG_REQUIRED a required input argument is missing
153 * ZFS_ERR_IOC_ARG_BADTYPE an input argument has an invalid type
156 #include <sys/types.h>
157 #include <sys/param.h>
158 #include <sys/errno.h>
160 #include <sys/file.h>
161 #include <sys/kmem.h>
162 #include <sys/cmn_err.h>
163 #include <sys/stat.h>
164 #include <sys/zfs_ioctl.h>
165 #include <sys/zfs_vfsops.h>
166 #include <sys/zfs_znode.h>
169 #include <sys/spa_impl.h>
170 #include <sys/vdev.h>
171 #include <sys/vdev_impl.h>
173 #include <sys/dsl_dir.h>
174 #include <sys/dsl_dataset.h>
175 #include <sys/dsl_prop.h>
176 #include <sys/dsl_deleg.h>
177 #include <sys/dmu_objset.h>
178 #include <sys/dmu_impl.h>
179 #include <sys/dmu_redact.h>
180 #include <sys/dmu_tx.h>
181 #include <sys/sunddi.h>
182 #include <sys/policy.h>
183 #include <sys/zone.h>
184 #include <sys/nvpair.h>
185 #include <sys/pathname.h>
187 #include <sys/fs/zfs.h>
188 #include <sys/zfs_ctldir.h>
189 #include <sys/zfs_dir.h>
190 #include <sys/zfs_onexit.h>
191 #include <sys/zvol.h>
192 #include <sys/dsl_scan.h>
193 #include <sys/fm/util.h>
194 #include <sys/dsl_crypt.h>
196 #include <sys/dmu_recv.h>
197 #include <sys/dmu_send.h>
198 #include <sys/dmu_recv.h>
199 #include <sys/dsl_destroy.h>
200 #include <sys/dsl_bookmark.h>
201 #include <sys/dsl_userhold.h>
202 #include <sys/zfeature.h>
204 #include <sys/zio_checksum.h>
205 #include <sys/vdev_removal.h>
206 #include <sys/zfs_sysfs.h>
207 #include <sys/vdev_impl.h>
208 #include <sys/vdev_initialize.h>
209 #include <sys/vdev_trim.h>
211 #include <linux/miscdevice.h>
212 #include <linux/slab.h>
214 #include "zfs_namecheck.h"
215 #include "zfs_prop.h"
216 #include "zfs_deleg.h"
217 #include "zfs_comutil.h"
219 #include <sys/lua/lua.h>
220 #include <sys/lua/lauxlib.h>
223 * Limit maximum nvlist size. We don't want users passing in insane values
224 * for zc->zc_nvlist_src_size, since we will need to allocate that much memory.
226 #define MAX_NVLIST_SRC_SIZE KMALLOC_MAX_SIZE
228 kmutex_t zfsdev_state_lock
;
229 zfsdev_state_t
*zfsdev_state_list
;
231 extern void zfs_init(void);
232 extern void zfs_fini(void);
234 uint_t zfs_fsyncer_key
;
235 extern uint_t rrw_tsd_key
;
236 static uint_t zfs_allow_log_key
;
238 typedef int zfs_ioc_legacy_func_t(zfs_cmd_t
*);
239 typedef int zfs_ioc_func_t(const char *, nvlist_t
*, nvlist_t
*);
240 typedef int zfs_secpolicy_func_t(zfs_cmd_t
*, nvlist_t
*, cred_t
*);
243 * IOC Keys are used to document and validate user->kernel interface inputs.
244 * See zfs_keys_recv_new for an example declaration. Any key name that is not
245 * listed will be rejected as input.
247 * The keyname 'optional' is always allowed, and must be an nvlist if present.
248 * Arguments which older kernels can safely ignore can be placed under the
251 * When adding new keys to an existing ioc for new functionality, consider:
252 * - adding an entry into zfs_sysfs.c zfs_features[] list
253 * - updating the libzfs_input_check.c test utility
255 * Note: in the ZK_WILDCARDLIST case, the name serves as documentation
256 * for the expected name (bookmark, snapshot, property, etc) but there
257 * is no validation in the preflight zfs_check_input_nvpairs() check.
260 ZK_OPTIONAL
= 1 << 0, /* pair is optional */
261 ZK_WILDCARDLIST
= 1 << 1, /* one or more unspecified key names */
264 /* DATA_TYPE_ANY is used when zkey_type can vary. */
265 #define DATA_TYPE_ANY DATA_TYPE_UNKNOWN
267 typedef struct zfs_ioc_key
{
268 const char *zkey_name
;
269 data_type_t zkey_type
;
270 ioc_key_flag_t zkey_flags
;
278 } zfs_ioc_namecheck_t
;
281 POOL_CHECK_NONE
= 1 << 0,
282 POOL_CHECK_SUSPENDED
= 1 << 1,
283 POOL_CHECK_READONLY
= 1 << 2,
284 } zfs_ioc_poolcheck_t
;
286 typedef struct zfs_ioc_vec
{
287 zfs_ioc_legacy_func_t
*zvec_legacy_func
;
288 zfs_ioc_func_t
*zvec_func
;
289 zfs_secpolicy_func_t
*zvec_secpolicy
;
290 zfs_ioc_namecheck_t zvec_namecheck
;
291 boolean_t zvec_allow_log
;
292 zfs_ioc_poolcheck_t zvec_pool_check
;
293 boolean_t zvec_smush_outnvlist
;
294 const char *zvec_name
;
295 const zfs_ioc_key_t
*zvec_nvl_keys
;
296 size_t zvec_nvl_key_count
;
299 /* This array is indexed by zfs_userquota_prop_t */
300 static const char *userquota_perms
[] = {
301 ZFS_DELEG_PERM_USERUSED
,
302 ZFS_DELEG_PERM_USERQUOTA
,
303 ZFS_DELEG_PERM_GROUPUSED
,
304 ZFS_DELEG_PERM_GROUPQUOTA
,
305 ZFS_DELEG_PERM_USEROBJUSED
,
306 ZFS_DELEG_PERM_USEROBJQUOTA
,
307 ZFS_DELEG_PERM_GROUPOBJUSED
,
308 ZFS_DELEG_PERM_GROUPOBJQUOTA
,
309 ZFS_DELEG_PERM_PROJECTUSED
,
310 ZFS_DELEG_PERM_PROJECTQUOTA
,
311 ZFS_DELEG_PERM_PROJECTOBJUSED
,
312 ZFS_DELEG_PERM_PROJECTOBJQUOTA
,
315 static int zfs_ioc_userspace_upgrade(zfs_cmd_t
*zc
);
316 static int zfs_ioc_id_quota_upgrade(zfs_cmd_t
*zc
);
317 static int zfs_check_settable(const char *name
, nvpair_t
*property
,
319 static int zfs_check_clearable(char *dataset
, nvlist_t
*props
,
321 static int zfs_fill_zplprops_root(uint64_t, nvlist_t
*, nvlist_t
*,
323 int zfs_set_prop_nvlist(const char *, zprop_source_t
, nvlist_t
*, nvlist_t
*);
324 static int get_nvlist(uint64_t nvl
, uint64_t size
, int iflag
, nvlist_t
**nvp
);
327 history_str_free(char *buf
)
329 kmem_free(buf
, HIS_MAX_RECORD_LEN
);
333 history_str_get(zfs_cmd_t
*zc
)
337 if (zc
->zc_history
== 0)
340 buf
= kmem_alloc(HIS_MAX_RECORD_LEN
, KM_SLEEP
);
341 if (copyinstr((void *)(uintptr_t)zc
->zc_history
,
342 buf
, HIS_MAX_RECORD_LEN
, NULL
) != 0) {
343 history_str_free(buf
);
347 buf
[HIS_MAX_RECORD_LEN
-1] = '\0';
353 * Check to see if the named dataset is currently defined as bootable
356 zfs_is_bootfs(const char *name
)
360 if (dmu_objset_hold(name
, FTAG
, &os
) == 0) {
362 ret
= (dmu_objset_id(os
) == spa_bootfs(dmu_objset_spa(os
)));
363 dmu_objset_rele(os
, FTAG
);
370 * Return non-zero if the spa version is less than requested version.
373 zfs_earlier_version(const char *name
, int version
)
377 if (spa_open(name
, &spa
, FTAG
) == 0) {
378 if (spa_version(spa
) < version
) {
379 spa_close(spa
, FTAG
);
382 spa_close(spa
, FTAG
);
388 * Return TRUE if the ZPL version is less than requested version.
391 zpl_earlier_version(const char *name
, int version
)
394 boolean_t rc
= B_TRUE
;
396 if (dmu_objset_hold(name
, FTAG
, &os
) == 0) {
399 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
400 dmu_objset_rele(os
, FTAG
);
403 /* XXX reading from non-owned objset */
404 if (zfs_get_zplprop(os
, ZFS_PROP_VERSION
, &zplversion
) == 0)
405 rc
= zplversion
< version
;
406 dmu_objset_rele(os
, FTAG
);
412 zfs_log_history(zfs_cmd_t
*zc
)
417 if ((buf
= history_str_get(zc
)) == NULL
)
420 if (spa_open(zc
->zc_name
, &spa
, FTAG
) == 0) {
421 if (spa_version(spa
) >= SPA_VERSION_ZPOOL_HISTORY
)
422 (void) spa_history_log(spa
, buf
);
423 spa_close(spa
, FTAG
);
425 history_str_free(buf
);
429 * Policy for top-level read operations (list pools). Requires no privileges,
430 * and can be used in the local zone, as there is no associated dataset.
434 zfs_secpolicy_none(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
440 * Policy for dataset read operations (list children, get statistics). Requires
441 * no privileges, but must be visible in the local zone.
445 zfs_secpolicy_read(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
447 if (INGLOBALZONE(curproc
) ||
448 zone_dataset_visible(zc
->zc_name
, NULL
))
451 return (SET_ERROR(ENOENT
));
455 zfs_dozonecheck_impl(const char *dataset
, uint64_t zoned
, cred_t
*cr
)
460 * The dataset must be visible by this zone -- check this first
461 * so they don't see EPERM on something they shouldn't know about.
463 if (!INGLOBALZONE(curproc
) &&
464 !zone_dataset_visible(dataset
, &writable
))
465 return (SET_ERROR(ENOENT
));
467 if (INGLOBALZONE(curproc
)) {
469 * If the fs is zoned, only root can access it from the
472 if (secpolicy_zfs(cr
) && zoned
)
473 return (SET_ERROR(EPERM
));
476 * If we are in a local zone, the 'zoned' property must be set.
479 return (SET_ERROR(EPERM
));
481 /* must be writable by this zone */
483 return (SET_ERROR(EPERM
));
489 zfs_dozonecheck(const char *dataset
, cred_t
*cr
)
493 if (dsl_prop_get_integer(dataset
, "zoned", &zoned
, NULL
))
494 return (SET_ERROR(ENOENT
));
496 return (zfs_dozonecheck_impl(dataset
, zoned
, cr
));
500 zfs_dozonecheck_ds(const char *dataset
, dsl_dataset_t
*ds
, cred_t
*cr
)
504 if (dsl_prop_get_int_ds(ds
, "zoned", &zoned
))
505 return (SET_ERROR(ENOENT
));
507 return (zfs_dozonecheck_impl(dataset
, zoned
, cr
));
511 zfs_secpolicy_write_perms_ds(const char *name
, dsl_dataset_t
*ds
,
512 const char *perm
, cred_t
*cr
)
516 error
= zfs_dozonecheck_ds(name
, ds
, cr
);
518 error
= secpolicy_zfs(cr
);
520 error
= dsl_deleg_access_impl(ds
, perm
, cr
);
526 zfs_secpolicy_write_perms(const char *name
, const char *perm
, cred_t
*cr
)
533 * First do a quick check for root in the global zone, which
534 * is allowed to do all write_perms. This ensures that zfs_ioc_*
535 * will get to handle nonexistent datasets.
537 if (INGLOBALZONE(curproc
) && secpolicy_zfs(cr
) == 0)
540 error
= dsl_pool_hold(name
, FTAG
, &dp
);
544 error
= dsl_dataset_hold(dp
, name
, FTAG
, &ds
);
546 dsl_pool_rele(dp
, FTAG
);
550 error
= zfs_secpolicy_write_perms_ds(name
, ds
, perm
, cr
);
552 dsl_dataset_rele(ds
, FTAG
);
553 dsl_pool_rele(dp
, FTAG
);
558 * Policy for setting the security label property.
560 * Returns 0 for success, non-zero for access and other errors.
563 zfs_set_slabel_policy(const char *name
, char *strval
, cred_t
*cr
)
566 char ds_hexsl
[MAXNAMELEN
];
567 bslabel_t ds_sl
, new_sl
;
568 boolean_t new_default
= FALSE
;
570 int needed_priv
= -1;
573 /* First get the existing dataset label. */
574 error
= dsl_prop_get(name
, zfs_prop_to_name(ZFS_PROP_MLSLABEL
),
575 1, sizeof (ds_hexsl
), &ds_hexsl
, NULL
);
577 return (SET_ERROR(EPERM
));
579 if (strcasecmp(strval
, ZFS_MLSLABEL_DEFAULT
) == 0)
582 /* The label must be translatable */
583 if (!new_default
&& (hexstr_to_label(strval
, &new_sl
) != 0))
584 return (SET_ERROR(EINVAL
));
587 * In a non-global zone, disallow attempts to set a label that
588 * doesn't match that of the zone; otherwise no other checks
591 if (!INGLOBALZONE(curproc
)) {
592 if (new_default
|| !blequal(&new_sl
, CR_SL(CRED())))
593 return (SET_ERROR(EPERM
));
598 * For global-zone datasets (i.e., those whose zoned property is
599 * "off", verify that the specified new label is valid for the
602 if (dsl_prop_get_integer(name
,
603 zfs_prop_to_name(ZFS_PROP_ZONED
), &zoned
, NULL
))
604 return (SET_ERROR(EPERM
));
606 if (zfs_check_global_label(name
, strval
) != 0)
607 return (SET_ERROR(EPERM
));
611 * If the existing dataset label is nondefault, check if the
612 * dataset is mounted (label cannot be changed while mounted).
613 * Get the zfsvfs_t; if there isn't one, then the dataset isn't
614 * mounted (or isn't a dataset, doesn't exist, ...).
616 if (strcasecmp(ds_hexsl
, ZFS_MLSLABEL_DEFAULT
) != 0) {
618 static char *setsl_tag
= "setsl_tag";
621 * Try to own the dataset; abort if there is any error,
622 * (e.g., already mounted, in use, or other error).
624 error
= dmu_objset_own(name
, DMU_OST_ZFS
, B_TRUE
, B_TRUE
,
627 return (SET_ERROR(EPERM
));
629 dmu_objset_disown(os
, B_TRUE
, setsl_tag
);
632 needed_priv
= PRIV_FILE_DOWNGRADE_SL
;
636 if (hexstr_to_label(strval
, &new_sl
) != 0)
637 return (SET_ERROR(EPERM
));
639 if (blstrictdom(&ds_sl
, &new_sl
))
640 needed_priv
= PRIV_FILE_DOWNGRADE_SL
;
641 else if (blstrictdom(&new_sl
, &ds_sl
))
642 needed_priv
= PRIV_FILE_UPGRADE_SL
;
644 /* dataset currently has a default label */
646 needed_priv
= PRIV_FILE_UPGRADE_SL
;
650 if (needed_priv
!= -1)
651 return (PRIV_POLICY(cr
, needed_priv
, B_FALSE
, EPERM
, NULL
));
654 return (SET_ERROR(ENOTSUP
));
655 #endif /* HAVE_MLSLABEL */
659 zfs_secpolicy_setprop(const char *dsname
, zfs_prop_t prop
, nvpair_t
*propval
,
665 * Check permissions for special properties.
672 * Disallow setting of 'zoned' from within a local zone.
674 if (!INGLOBALZONE(curproc
))
675 return (SET_ERROR(EPERM
));
679 case ZFS_PROP_FILESYSTEM_LIMIT
:
680 case ZFS_PROP_SNAPSHOT_LIMIT
:
681 if (!INGLOBALZONE(curproc
)) {
683 char setpoint
[ZFS_MAX_DATASET_NAME_LEN
];
685 * Unprivileged users are allowed to modify the
686 * limit on things *under* (ie. contained by)
687 * the thing they own.
689 if (dsl_prop_get_integer(dsname
, "zoned", &zoned
,
691 return (SET_ERROR(EPERM
));
692 if (!zoned
|| strlen(dsname
) <= strlen(setpoint
))
693 return (SET_ERROR(EPERM
));
697 case ZFS_PROP_MLSLABEL
:
698 if (!is_system_labeled())
699 return (SET_ERROR(EPERM
));
701 if (nvpair_value_string(propval
, &strval
) == 0) {
704 err
= zfs_set_slabel_policy(dsname
, strval
, CRED());
711 return (zfs_secpolicy_write_perms(dsname
, zfs_prop_to_name(prop
), cr
));
716 zfs_secpolicy_set_fsacl(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
720 error
= zfs_dozonecheck(zc
->zc_name
, cr
);
725 * permission to set permissions will be evaluated later in
726 * dsl_deleg_can_allow()
733 zfs_secpolicy_rollback(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
735 return (zfs_secpolicy_write_perms(zc
->zc_name
,
736 ZFS_DELEG_PERM_ROLLBACK
, cr
));
741 zfs_secpolicy_send(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
749 * Generate the current snapshot name from the given objsetid, then
750 * use that name for the secpolicy/zone checks.
752 cp
= strchr(zc
->zc_name
, '@');
754 return (SET_ERROR(EINVAL
));
755 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
759 error
= dsl_dataset_hold_obj(dp
, zc
->zc_sendobj
, FTAG
, &ds
);
761 dsl_pool_rele(dp
, FTAG
);
765 dsl_dataset_name(ds
, zc
->zc_name
);
767 error
= zfs_secpolicy_write_perms_ds(zc
->zc_name
, ds
,
768 ZFS_DELEG_PERM_SEND
, cr
);
769 dsl_dataset_rele(ds
, FTAG
);
770 dsl_pool_rele(dp
, FTAG
);
777 zfs_secpolicy_send_new(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
779 return (zfs_secpolicy_write_perms(zc
->zc_name
,
780 ZFS_DELEG_PERM_SEND
, cr
));
784 zfs_secpolicy_share(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
786 return (SET_ERROR(ENOTSUP
));
790 zfs_secpolicy_smb_acl(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
792 return (SET_ERROR(ENOTSUP
));
796 zfs_get_parent(const char *datasetname
, char *parent
, int parentsize
)
801 * Remove the @bla or /bla from the end of the name to get the parent.
803 (void) strncpy(parent
, datasetname
, parentsize
);
804 cp
= strrchr(parent
, '@');
808 cp
= strrchr(parent
, '/');
810 return (SET_ERROR(ENOENT
));
818 zfs_secpolicy_destroy_perms(const char *name
, cred_t
*cr
)
822 if ((error
= zfs_secpolicy_write_perms(name
,
823 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
826 return (zfs_secpolicy_write_perms(name
, ZFS_DELEG_PERM_DESTROY
, cr
));
831 zfs_secpolicy_destroy(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
833 return (zfs_secpolicy_destroy_perms(zc
->zc_name
, cr
));
837 * Destroying snapshots with delegated permissions requires
838 * descendant mount and destroy permissions.
842 zfs_secpolicy_destroy_snaps(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
845 nvpair_t
*pair
, *nextpair
;
848 snaps
= fnvlist_lookup_nvlist(innvl
, "snaps");
850 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
852 nextpair
= nvlist_next_nvpair(snaps
, pair
);
853 error
= zfs_secpolicy_destroy_perms(nvpair_name(pair
), cr
);
854 if (error
== ENOENT
) {
856 * Ignore any snapshots that don't exist (we consider
857 * them "already destroyed"). Remove the name from the
858 * nvl here in case the snapshot is created between
859 * now and when we try to destroy it (in which case
860 * we don't want to destroy it since we haven't
861 * checked for permission).
863 fnvlist_remove_nvpair(snaps
, pair
);
874 zfs_secpolicy_rename_perms(const char *from
, const char *to
, cred_t
*cr
)
876 char parentname
[ZFS_MAX_DATASET_NAME_LEN
];
879 if ((error
= zfs_secpolicy_write_perms(from
,
880 ZFS_DELEG_PERM_RENAME
, cr
)) != 0)
883 if ((error
= zfs_secpolicy_write_perms(from
,
884 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
887 if ((error
= zfs_get_parent(to
, parentname
,
888 sizeof (parentname
))) != 0)
891 if ((error
= zfs_secpolicy_write_perms(parentname
,
892 ZFS_DELEG_PERM_CREATE
, cr
)) != 0)
895 if ((error
= zfs_secpolicy_write_perms(parentname
,
896 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
904 zfs_secpolicy_rename(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
906 return (zfs_secpolicy_rename_perms(zc
->zc_name
, zc
->zc_value
, cr
));
911 zfs_secpolicy_promote(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
914 dsl_dataset_t
*clone
;
917 error
= zfs_secpolicy_write_perms(zc
->zc_name
,
918 ZFS_DELEG_PERM_PROMOTE
, cr
);
922 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
926 error
= dsl_dataset_hold(dp
, zc
->zc_name
, FTAG
, &clone
);
929 char parentname
[ZFS_MAX_DATASET_NAME_LEN
];
930 dsl_dataset_t
*origin
= NULL
;
934 error
= dsl_dataset_hold_obj(dd
->dd_pool
,
935 dsl_dir_phys(dd
)->dd_origin_obj
, FTAG
, &origin
);
937 dsl_dataset_rele(clone
, FTAG
);
938 dsl_pool_rele(dp
, FTAG
);
942 error
= zfs_secpolicy_write_perms_ds(zc
->zc_name
, clone
,
943 ZFS_DELEG_PERM_MOUNT
, cr
);
945 dsl_dataset_name(origin
, parentname
);
947 error
= zfs_secpolicy_write_perms_ds(parentname
, origin
,
948 ZFS_DELEG_PERM_PROMOTE
, cr
);
950 dsl_dataset_rele(clone
, FTAG
);
951 dsl_dataset_rele(origin
, FTAG
);
953 dsl_pool_rele(dp
, FTAG
);
959 zfs_secpolicy_recv(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
963 if ((error
= zfs_secpolicy_write_perms(zc
->zc_name
,
964 ZFS_DELEG_PERM_RECEIVE
, cr
)) != 0)
967 if ((error
= zfs_secpolicy_write_perms(zc
->zc_name
,
968 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
971 return (zfs_secpolicy_write_perms(zc
->zc_name
,
972 ZFS_DELEG_PERM_CREATE
, cr
));
977 zfs_secpolicy_recv_new(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
979 return (zfs_secpolicy_recv(zc
, innvl
, cr
));
983 zfs_secpolicy_snapshot_perms(const char *name
, cred_t
*cr
)
985 return (zfs_secpolicy_write_perms(name
,
986 ZFS_DELEG_PERM_SNAPSHOT
, cr
));
990 * Check for permission to create each snapshot in the nvlist.
994 zfs_secpolicy_snapshot(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1000 snaps
= fnvlist_lookup_nvlist(innvl
, "snaps");
1002 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
1003 pair
= nvlist_next_nvpair(snaps
, pair
)) {
1004 char *name
= nvpair_name(pair
);
1005 char *atp
= strchr(name
, '@');
1008 error
= SET_ERROR(EINVAL
);
1012 error
= zfs_secpolicy_snapshot_perms(name
, cr
);
1021 * Check for permission to create each bookmark in the nvlist.
1025 zfs_secpolicy_bookmark(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1029 for (nvpair_t
*pair
= nvlist_next_nvpair(innvl
, NULL
);
1030 pair
!= NULL
; pair
= nvlist_next_nvpair(innvl
, pair
)) {
1031 char *name
= nvpair_name(pair
);
1032 char *hashp
= strchr(name
, '#');
1034 if (hashp
== NULL
) {
1035 error
= SET_ERROR(EINVAL
);
1039 error
= zfs_secpolicy_write_perms(name
,
1040 ZFS_DELEG_PERM_BOOKMARK
, cr
);
1050 zfs_secpolicy_remap(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1052 return (zfs_secpolicy_write_perms(zc
->zc_name
,
1053 ZFS_DELEG_PERM_REMAP
, cr
));
1058 zfs_secpolicy_destroy_bookmarks(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1060 nvpair_t
*pair
, *nextpair
;
1063 for (pair
= nvlist_next_nvpair(innvl
, NULL
); pair
!= NULL
;
1065 char *name
= nvpair_name(pair
);
1066 char *hashp
= strchr(name
, '#');
1067 nextpair
= nvlist_next_nvpair(innvl
, pair
);
1069 if (hashp
== NULL
) {
1070 error
= SET_ERROR(EINVAL
);
1075 error
= zfs_secpolicy_write_perms(name
,
1076 ZFS_DELEG_PERM_DESTROY
, cr
);
1078 if (error
== ENOENT
) {
1080 * Ignore any filesystems that don't exist (we consider
1081 * their bookmarks "already destroyed"). Remove
1082 * the name from the nvl here in case the filesystem
1083 * is created between now and when we try to destroy
1084 * the bookmark (in which case we don't want to
1085 * destroy it since we haven't checked for permission).
1087 fnvlist_remove_nvpair(innvl
, pair
);
1099 zfs_secpolicy_log_history(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1102 * Even root must have a proper TSD so that we know what pool
1105 if (tsd_get(zfs_allow_log_key
) == NULL
)
1106 return (SET_ERROR(EPERM
));
1111 zfs_secpolicy_create_clone(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1113 char parentname
[ZFS_MAX_DATASET_NAME_LEN
];
1117 if ((error
= zfs_get_parent(zc
->zc_name
, parentname
,
1118 sizeof (parentname
))) != 0)
1121 if (nvlist_lookup_string(innvl
, "origin", &origin
) == 0 &&
1122 (error
= zfs_secpolicy_write_perms(origin
,
1123 ZFS_DELEG_PERM_CLONE
, cr
)) != 0)
1126 if ((error
= zfs_secpolicy_write_perms(parentname
,
1127 ZFS_DELEG_PERM_CREATE
, cr
)) != 0)
1130 return (zfs_secpolicy_write_perms(parentname
,
1131 ZFS_DELEG_PERM_MOUNT
, cr
));
1135 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
1136 * SYS_CONFIG privilege, which is not available in a local zone.
1140 zfs_secpolicy_config(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1142 if (secpolicy_sys_config(cr
, B_FALSE
) != 0)
1143 return (SET_ERROR(EPERM
));
1149 * Policy for object to name lookups.
1153 zfs_secpolicy_diff(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1157 if ((error
= secpolicy_sys_config(cr
, B_FALSE
)) == 0)
1160 error
= zfs_secpolicy_write_perms(zc
->zc_name
, ZFS_DELEG_PERM_DIFF
, cr
);
1165 * Policy for fault injection. Requires all privileges.
1169 zfs_secpolicy_inject(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1171 return (secpolicy_zinject(cr
));
1176 zfs_secpolicy_inherit_prop(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1178 zfs_prop_t prop
= zfs_name_to_prop(zc
->zc_value
);
1180 if (prop
== ZPROP_INVAL
) {
1181 if (!zfs_prop_user(zc
->zc_value
))
1182 return (SET_ERROR(EINVAL
));
1183 return (zfs_secpolicy_write_perms(zc
->zc_name
,
1184 ZFS_DELEG_PERM_USERPROP
, cr
));
1186 return (zfs_secpolicy_setprop(zc
->zc_name
, prop
,
1192 zfs_secpolicy_userspace_one(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1194 int err
= zfs_secpolicy_read(zc
, innvl
, cr
);
1198 if (zc
->zc_objset_type
>= ZFS_NUM_USERQUOTA_PROPS
)
1199 return (SET_ERROR(EINVAL
));
1201 if (zc
->zc_value
[0] == 0) {
1203 * They are asking about a posix uid/gid. If it's
1204 * themself, allow it.
1206 if (zc
->zc_objset_type
== ZFS_PROP_USERUSED
||
1207 zc
->zc_objset_type
== ZFS_PROP_USERQUOTA
||
1208 zc
->zc_objset_type
== ZFS_PROP_USEROBJUSED
||
1209 zc
->zc_objset_type
== ZFS_PROP_USEROBJQUOTA
) {
1210 if (zc
->zc_guid
== crgetuid(cr
))
1212 } else if (zc
->zc_objset_type
== ZFS_PROP_GROUPUSED
||
1213 zc
->zc_objset_type
== ZFS_PROP_GROUPQUOTA
||
1214 zc
->zc_objset_type
== ZFS_PROP_GROUPOBJUSED
||
1215 zc
->zc_objset_type
== ZFS_PROP_GROUPOBJQUOTA
) {
1216 if (groupmember(zc
->zc_guid
, cr
))
1219 /* else is for project quota/used */
1222 return (zfs_secpolicy_write_perms(zc
->zc_name
,
1223 userquota_perms
[zc
->zc_objset_type
], cr
));
1227 zfs_secpolicy_userspace_many(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1229 int err
= zfs_secpolicy_read(zc
, innvl
, cr
);
1233 if (zc
->zc_objset_type
>= ZFS_NUM_USERQUOTA_PROPS
)
1234 return (SET_ERROR(EINVAL
));
1236 return (zfs_secpolicy_write_perms(zc
->zc_name
,
1237 userquota_perms
[zc
->zc_objset_type
], cr
));
1242 zfs_secpolicy_userspace_upgrade(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1244 return (zfs_secpolicy_setprop(zc
->zc_name
, ZFS_PROP_VERSION
,
1250 zfs_secpolicy_hold(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1256 holds
= fnvlist_lookup_nvlist(innvl
, "holds");
1258 for (pair
= nvlist_next_nvpair(holds
, NULL
); pair
!= NULL
;
1259 pair
= nvlist_next_nvpair(holds
, pair
)) {
1260 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
1261 error
= dmu_fsname(nvpair_name(pair
), fsname
);
1264 error
= zfs_secpolicy_write_perms(fsname
,
1265 ZFS_DELEG_PERM_HOLD
, cr
);
1274 zfs_secpolicy_release(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1279 for (pair
= nvlist_next_nvpair(innvl
, NULL
); pair
!= NULL
;
1280 pair
= nvlist_next_nvpair(innvl
, pair
)) {
1281 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
1282 error
= dmu_fsname(nvpair_name(pair
), fsname
);
1285 error
= zfs_secpolicy_write_perms(fsname
,
1286 ZFS_DELEG_PERM_RELEASE
, cr
);
1294 * Policy for allowing temporary snapshots to be taken or released
1297 zfs_secpolicy_tmp_snapshot(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1300 * A temporary snapshot is the same as a snapshot,
1301 * hold, destroy and release all rolled into one.
1302 * Delegated diff alone is sufficient that we allow this.
1306 if ((error
= zfs_secpolicy_write_perms(zc
->zc_name
,
1307 ZFS_DELEG_PERM_DIFF
, cr
)) == 0)
1310 error
= zfs_secpolicy_snapshot_perms(zc
->zc_name
, cr
);
1312 if (innvl
!= NULL
) {
1314 error
= zfs_secpolicy_hold(zc
, innvl
, cr
);
1316 error
= zfs_secpolicy_release(zc
, innvl
, cr
);
1318 error
= zfs_secpolicy_destroy(zc
, innvl
, cr
);
1324 zfs_secpolicy_load_key(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1326 return (zfs_secpolicy_write_perms(zc
->zc_name
,
1327 ZFS_DELEG_PERM_LOAD_KEY
, cr
));
1331 zfs_secpolicy_change_key(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1333 return (zfs_secpolicy_write_perms(zc
->zc_name
,
1334 ZFS_DELEG_PERM_CHANGE_KEY
, cr
));
1338 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1341 get_nvlist(uint64_t nvl
, uint64_t size
, int iflag
, nvlist_t
**nvp
)
1345 nvlist_t
*list
= NULL
;
1348 * Read in and unpack the user-supplied nvlist.
1351 return (SET_ERROR(EINVAL
));
1353 packed
= vmem_alloc(size
, KM_SLEEP
);
1355 if ((error
= ddi_copyin((void *)(uintptr_t)nvl
, packed
, size
,
1357 vmem_free(packed
, size
);
1358 return (SET_ERROR(EFAULT
));
1361 if ((error
= nvlist_unpack(packed
, size
, &list
, 0)) != 0) {
1362 vmem_free(packed
, size
);
1366 vmem_free(packed
, size
);
1373 * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
1374 * Entries will be removed from the end of the nvlist, and one int32 entry
1375 * named "N_MORE_ERRORS" will be added indicating how many entries were
1379 nvlist_smush(nvlist_t
*errors
, size_t max
)
1383 size
= fnvlist_size(errors
);
1386 nvpair_t
*more_errors
;
1390 return (SET_ERROR(ENOMEM
));
1392 fnvlist_add_int32(errors
, ZPROP_N_MORE_ERRORS
, 0);
1393 more_errors
= nvlist_prev_nvpair(errors
, NULL
);
1396 nvpair_t
*pair
= nvlist_prev_nvpair(errors
,
1398 fnvlist_remove_nvpair(errors
, pair
);
1400 size
= fnvlist_size(errors
);
1401 } while (size
> max
);
1403 fnvlist_remove_nvpair(errors
, more_errors
);
1404 fnvlist_add_int32(errors
, ZPROP_N_MORE_ERRORS
, n
);
1405 ASSERT3U(fnvlist_size(errors
), <=, max
);
1412 put_nvlist(zfs_cmd_t
*zc
, nvlist_t
*nvl
)
1414 char *packed
= NULL
;
1418 size
= fnvlist_size(nvl
);
1420 if (size
> zc
->zc_nvlist_dst_size
) {
1421 error
= SET_ERROR(ENOMEM
);
1423 packed
= fnvlist_pack(nvl
, &size
);
1424 if (ddi_copyout(packed
, (void *)(uintptr_t)zc
->zc_nvlist_dst
,
1425 size
, zc
->zc_iflags
) != 0)
1426 error
= SET_ERROR(EFAULT
);
1427 fnvlist_pack_free(packed
, size
);
1430 zc
->zc_nvlist_dst_size
= size
;
1431 zc
->zc_nvlist_dst_filled
= B_TRUE
;
1436 getzfsvfs_impl(objset_t
*os
, zfsvfs_t
**zfvp
)
1439 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
1440 return (SET_ERROR(EINVAL
));
1443 mutex_enter(&os
->os_user_ptr_lock
);
1444 *zfvp
= dmu_objset_get_user(os
);
1445 /* bump s_active only when non-zero to prevent umount race */
1446 if (*zfvp
== NULL
|| (*zfvp
)->z_sb
== NULL
||
1447 !atomic_inc_not_zero(&((*zfvp
)->z_sb
->s_active
))) {
1448 error
= SET_ERROR(ESRCH
);
1450 mutex_exit(&os
->os_user_ptr_lock
);
1455 getzfsvfs(const char *dsname
, zfsvfs_t
**zfvp
)
1460 error
= dmu_objset_hold(dsname
, FTAG
, &os
);
1464 error
= getzfsvfs_impl(os
, zfvp
);
1465 dmu_objset_rele(os
, FTAG
);
1470 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
1471 * case its z_sb will be NULL, and it will be opened as the owner.
1472 * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1473 * which prevents all inode ops from running.
1476 zfsvfs_hold(const char *name
, void *tag
, zfsvfs_t
**zfvp
, boolean_t writer
)
1480 if (getzfsvfs(name
, zfvp
) != 0)
1481 error
= zfsvfs_create(name
, B_FALSE
, zfvp
);
1483 rrm_enter(&(*zfvp
)->z_teardown_lock
, (writer
) ? RW_WRITER
:
1485 if ((*zfvp
)->z_unmounted
) {
1487 * XXX we could probably try again, since the unmounting
1488 * thread should be just about to disassociate the
1489 * objset from the zfsvfs.
1491 rrm_exit(&(*zfvp
)->z_teardown_lock
, tag
);
1492 return (SET_ERROR(EBUSY
));
1499 zfsvfs_rele(zfsvfs_t
*zfsvfs
, void *tag
)
1501 rrm_exit(&zfsvfs
->z_teardown_lock
, tag
);
1504 deactivate_super(zfsvfs
->z_sb
);
1506 dmu_objset_disown(zfsvfs
->z_os
, B_TRUE
, zfsvfs
);
1507 zfsvfs_free(zfsvfs
);
1512 zfs_ioc_pool_create(zfs_cmd_t
*zc
)
1515 nvlist_t
*config
, *props
= NULL
;
1516 nvlist_t
*rootprops
= NULL
;
1517 nvlist_t
*zplprops
= NULL
;
1518 dsl_crypto_params_t
*dcp
= NULL
;
1519 char *spa_name
= zc
->zc_name
;
1520 boolean_t unload_wkey
= B_TRUE
;
1522 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1523 zc
->zc_iflags
, &config
)))
1526 if (zc
->zc_nvlist_src_size
!= 0 && (error
=
1527 get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1528 zc
->zc_iflags
, &props
))) {
1529 nvlist_free(config
);
1534 nvlist_t
*nvl
= NULL
;
1535 nvlist_t
*hidden_args
= NULL
;
1536 uint64_t version
= SPA_VERSION
;
1539 (void) nvlist_lookup_uint64(props
,
1540 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
);
1541 if (!SPA_VERSION_IS_SUPPORTED(version
)) {
1542 error
= SET_ERROR(EINVAL
);
1543 goto pool_props_bad
;
1545 (void) nvlist_lookup_nvlist(props
, ZPOOL_ROOTFS_PROPS
, &nvl
);
1547 error
= nvlist_dup(nvl
, &rootprops
, KM_SLEEP
);
1549 goto pool_props_bad
;
1550 (void) nvlist_remove_all(props
, ZPOOL_ROOTFS_PROPS
);
1553 (void) nvlist_lookup_nvlist(props
, ZPOOL_HIDDEN_ARGS
,
1555 error
= dsl_crypto_params_create_nvlist(DCP_CMD_NONE
,
1556 rootprops
, hidden_args
, &dcp
);
1558 goto pool_props_bad
;
1559 (void) nvlist_remove_all(props
, ZPOOL_HIDDEN_ARGS
);
1561 VERIFY(nvlist_alloc(&zplprops
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1562 error
= zfs_fill_zplprops_root(version
, rootprops
,
1565 goto pool_props_bad
;
1567 if (nvlist_lookup_string(props
,
1568 zpool_prop_to_name(ZPOOL_PROP_TNAME
), &tname
) == 0)
1572 error
= spa_create(zc
->zc_name
, config
, props
, zplprops
, dcp
);
1575 * Set the remaining root properties
1577 if (!error
&& (error
= zfs_set_prop_nvlist(spa_name
,
1578 ZPROP_SRC_LOCAL
, rootprops
, NULL
)) != 0) {
1579 (void) spa_destroy(spa_name
);
1580 unload_wkey
= B_FALSE
; /* spa_destroy() unloads wrapping keys */
1584 nvlist_free(rootprops
);
1585 nvlist_free(zplprops
);
1586 nvlist_free(config
);
1588 dsl_crypto_params_free(dcp
, unload_wkey
&& !!error
);
1594 zfs_ioc_pool_destroy(zfs_cmd_t
*zc
)
1597 zfs_log_history(zc
);
1598 error
= spa_destroy(zc
->zc_name
);
1604 zfs_ioc_pool_import(zfs_cmd_t
*zc
)
1606 nvlist_t
*config
, *props
= NULL
;
1610 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1611 zc
->zc_iflags
, &config
)) != 0)
1614 if (zc
->zc_nvlist_src_size
!= 0 && (error
=
1615 get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1616 zc
->zc_iflags
, &props
))) {
1617 nvlist_free(config
);
1621 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &guid
) != 0 ||
1622 guid
!= zc
->zc_guid
)
1623 error
= SET_ERROR(EINVAL
);
1625 error
= spa_import(zc
->zc_name
, config
, props
, zc
->zc_cookie
);
1627 if (zc
->zc_nvlist_dst
!= 0) {
1630 if ((err
= put_nvlist(zc
, config
)) != 0)
1634 nvlist_free(config
);
1641 zfs_ioc_pool_export(zfs_cmd_t
*zc
)
1644 boolean_t force
= (boolean_t
)zc
->zc_cookie
;
1645 boolean_t hardforce
= (boolean_t
)zc
->zc_guid
;
1647 zfs_log_history(zc
);
1648 error
= spa_export(zc
->zc_name
, NULL
, force
, hardforce
);
1654 zfs_ioc_pool_configs(zfs_cmd_t
*zc
)
1659 if ((configs
= spa_all_configs(&zc
->zc_cookie
)) == NULL
)
1660 return (SET_ERROR(EEXIST
));
1662 error
= put_nvlist(zc
, configs
);
1664 nvlist_free(configs
);
1671 * zc_name name of the pool
1674 * zc_cookie real errno
1675 * zc_nvlist_dst config nvlist
1676 * zc_nvlist_dst_size size of config nvlist
1679 zfs_ioc_pool_stats(zfs_cmd_t
*zc
)
1685 error
= spa_get_stats(zc
->zc_name
, &config
, zc
->zc_value
,
1686 sizeof (zc
->zc_value
));
1688 if (config
!= NULL
) {
1689 ret
= put_nvlist(zc
, config
);
1690 nvlist_free(config
);
1693 * The config may be present even if 'error' is non-zero.
1694 * In this case we return success, and preserve the real errno
1697 zc
->zc_cookie
= error
;
1706 * Try to import the given pool, returning pool stats as appropriate so that
1707 * user land knows which devices are available and overall pool health.
1710 zfs_ioc_pool_tryimport(zfs_cmd_t
*zc
)
1712 nvlist_t
*tryconfig
, *config
= NULL
;
1715 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1716 zc
->zc_iflags
, &tryconfig
)) != 0)
1719 config
= spa_tryimport(tryconfig
);
1721 nvlist_free(tryconfig
);
1724 return (SET_ERROR(EINVAL
));
1726 error
= put_nvlist(zc
, config
);
1727 nvlist_free(config
);
1734 * zc_name name of the pool
1735 * zc_cookie scan func (pool_scan_func_t)
1736 * zc_flags scrub pause/resume flag (pool_scrub_cmd_t)
1739 zfs_ioc_pool_scan(zfs_cmd_t
*zc
)
1744 if (zc
->zc_flags
>= POOL_SCRUB_FLAGS_END
)
1745 return (SET_ERROR(EINVAL
));
1747 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1750 if (zc
->zc_flags
== POOL_SCRUB_PAUSE
)
1751 error
= spa_scrub_pause_resume(spa
, POOL_SCRUB_PAUSE
);
1752 else if (zc
->zc_cookie
== POOL_SCAN_NONE
)
1753 error
= spa_scan_stop(spa
);
1755 error
= spa_scan(spa
, zc
->zc_cookie
);
1757 spa_close(spa
, FTAG
);
1763 zfs_ioc_pool_freeze(zfs_cmd_t
*zc
)
1768 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1771 spa_close(spa
, FTAG
);
1777 zfs_ioc_pool_upgrade(zfs_cmd_t
*zc
)
1782 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1785 if (zc
->zc_cookie
< spa_version(spa
) ||
1786 !SPA_VERSION_IS_SUPPORTED(zc
->zc_cookie
)) {
1787 spa_close(spa
, FTAG
);
1788 return (SET_ERROR(EINVAL
));
1791 spa_upgrade(spa
, zc
->zc_cookie
);
1792 spa_close(spa
, FTAG
);
1798 zfs_ioc_pool_get_history(zfs_cmd_t
*zc
)
1805 if ((size
= zc
->zc_history_len
) == 0)
1806 return (SET_ERROR(EINVAL
));
1808 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1811 if (spa_version(spa
) < SPA_VERSION_ZPOOL_HISTORY
) {
1812 spa_close(spa
, FTAG
);
1813 return (SET_ERROR(ENOTSUP
));
1816 hist_buf
= vmem_alloc(size
, KM_SLEEP
);
1817 if ((error
= spa_history_get(spa
, &zc
->zc_history_offset
,
1818 &zc
->zc_history_len
, hist_buf
)) == 0) {
1819 error
= ddi_copyout(hist_buf
,
1820 (void *)(uintptr_t)zc
->zc_history
,
1821 zc
->zc_history_len
, zc
->zc_iflags
);
1824 spa_close(spa
, FTAG
);
1825 vmem_free(hist_buf
, size
);
1830 zfs_ioc_pool_reguid(zfs_cmd_t
*zc
)
1835 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1837 error
= spa_change_guid(spa
);
1838 spa_close(spa
, FTAG
);
1844 zfs_ioc_dsobj_to_dsname(zfs_cmd_t
*zc
)
1846 return (dsl_dsobj_to_dsname(zc
->zc_name
, zc
->zc_obj
, zc
->zc_value
));
1851 * zc_name name of filesystem
1852 * zc_obj object to find
1855 * zc_value name of object
1858 zfs_ioc_obj_to_path(zfs_cmd_t
*zc
)
1863 /* XXX reading from objset not owned */
1864 if ((error
= dmu_objset_hold_flags(zc
->zc_name
, B_TRUE
,
1867 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
1868 dmu_objset_rele_flags(os
, B_TRUE
, FTAG
);
1869 return (SET_ERROR(EINVAL
));
1871 error
= zfs_obj_to_path(os
, zc
->zc_obj
, zc
->zc_value
,
1872 sizeof (zc
->zc_value
));
1873 dmu_objset_rele_flags(os
, B_TRUE
, FTAG
);
1880 * zc_name name of filesystem
1881 * zc_obj object to find
1884 * zc_stat stats on object
1885 * zc_value path to object
1888 zfs_ioc_obj_to_stats(zfs_cmd_t
*zc
)
1893 /* XXX reading from objset not owned */
1894 if ((error
= dmu_objset_hold_flags(zc
->zc_name
, B_TRUE
,
1897 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
1898 dmu_objset_rele_flags(os
, B_TRUE
, FTAG
);
1899 return (SET_ERROR(EINVAL
));
1901 error
= zfs_obj_to_stats(os
, zc
->zc_obj
, &zc
->zc_stat
, zc
->zc_value
,
1902 sizeof (zc
->zc_value
));
1903 dmu_objset_rele_flags(os
, B_TRUE
, FTAG
);
1909 zfs_ioc_vdev_add(zfs_cmd_t
*zc
)
1915 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1919 error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1920 zc
->zc_iflags
, &config
);
1922 error
= spa_vdev_add(spa
, config
);
1923 nvlist_free(config
);
1925 spa_close(spa
, FTAG
);
1931 * zc_name name of the pool
1932 * zc_guid guid of vdev to remove
1933 * zc_cookie cancel removal
1936 zfs_ioc_vdev_remove(zfs_cmd_t
*zc
)
1941 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1944 if (zc
->zc_cookie
!= 0) {
1945 error
= spa_vdev_remove_cancel(spa
);
1947 error
= spa_vdev_remove(spa
, zc
->zc_guid
, B_FALSE
);
1949 spa_close(spa
, FTAG
);
1954 zfs_ioc_vdev_set_state(zfs_cmd_t
*zc
)
1958 vdev_state_t newstate
= VDEV_STATE_UNKNOWN
;
1960 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1962 switch (zc
->zc_cookie
) {
1963 case VDEV_STATE_ONLINE
:
1964 error
= vdev_online(spa
, zc
->zc_guid
, zc
->zc_obj
, &newstate
);
1967 case VDEV_STATE_OFFLINE
:
1968 error
= vdev_offline(spa
, zc
->zc_guid
, zc
->zc_obj
);
1971 case VDEV_STATE_FAULTED
:
1972 if (zc
->zc_obj
!= VDEV_AUX_ERR_EXCEEDED
&&
1973 zc
->zc_obj
!= VDEV_AUX_EXTERNAL
&&
1974 zc
->zc_obj
!= VDEV_AUX_EXTERNAL_PERSIST
)
1975 zc
->zc_obj
= VDEV_AUX_ERR_EXCEEDED
;
1977 error
= vdev_fault(spa
, zc
->zc_guid
, zc
->zc_obj
);
1980 case VDEV_STATE_DEGRADED
:
1981 if (zc
->zc_obj
!= VDEV_AUX_ERR_EXCEEDED
&&
1982 zc
->zc_obj
!= VDEV_AUX_EXTERNAL
)
1983 zc
->zc_obj
= VDEV_AUX_ERR_EXCEEDED
;
1985 error
= vdev_degrade(spa
, zc
->zc_guid
, zc
->zc_obj
);
1989 error
= SET_ERROR(EINVAL
);
1991 zc
->zc_cookie
= newstate
;
1992 spa_close(spa
, FTAG
);
1997 zfs_ioc_vdev_attach(zfs_cmd_t
*zc
)
2000 int replacing
= zc
->zc_cookie
;
2004 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
2007 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
2008 zc
->zc_iflags
, &config
)) == 0) {
2009 error
= spa_vdev_attach(spa
, zc
->zc_guid
, config
, replacing
);
2010 nvlist_free(config
);
2013 spa_close(spa
, FTAG
);
2018 zfs_ioc_vdev_detach(zfs_cmd_t
*zc
)
2023 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
2026 error
= spa_vdev_detach(spa
, zc
->zc_guid
, 0, B_FALSE
);
2028 spa_close(spa
, FTAG
);
2033 zfs_ioc_vdev_split(zfs_cmd_t
*zc
)
2036 nvlist_t
*config
, *props
= NULL
;
2038 boolean_t exp
= !!(zc
->zc_cookie
& ZPOOL_EXPORT_AFTER_SPLIT
);
2040 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
2043 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
2044 zc
->zc_iflags
, &config
))) {
2045 spa_close(spa
, FTAG
);
2049 if (zc
->zc_nvlist_src_size
!= 0 && (error
=
2050 get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2051 zc
->zc_iflags
, &props
))) {
2052 spa_close(spa
, FTAG
);
2053 nvlist_free(config
);
2057 error
= spa_vdev_split_mirror(spa
, zc
->zc_string
, config
, props
, exp
);
2059 spa_close(spa
, FTAG
);
2061 nvlist_free(config
);
2068 zfs_ioc_vdev_setpath(zfs_cmd_t
*zc
)
2071 char *path
= zc
->zc_value
;
2072 uint64_t guid
= zc
->zc_guid
;
2075 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
2079 error
= spa_vdev_setpath(spa
, guid
, path
);
2080 spa_close(spa
, FTAG
);
2085 zfs_ioc_vdev_setfru(zfs_cmd_t
*zc
)
2088 char *fru
= zc
->zc_value
;
2089 uint64_t guid
= zc
->zc_guid
;
2092 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
2096 error
= spa_vdev_setfru(spa
, guid
, fru
);
2097 spa_close(spa
, FTAG
);
2102 zfs_ioc_objset_stats_impl(zfs_cmd_t
*zc
, objset_t
*os
)
2107 dmu_objset_fast_stat(os
, &zc
->zc_objset_stats
);
2109 if (zc
->zc_nvlist_dst
!= 0 &&
2110 (error
= dsl_prop_get_all(os
, &nv
)) == 0) {
2111 dmu_objset_stats(os
, nv
);
2113 * NB: zvol_get_stats() will read the objset contents,
2114 * which we aren't supposed to do with a
2115 * DS_MODE_USER hold, because it could be
2116 * inconsistent. So this is a bit of a workaround...
2117 * XXX reading with out owning
2119 if (!zc
->zc_objset_stats
.dds_inconsistent
&&
2120 dmu_objset_type(os
) == DMU_OST_ZVOL
) {
2121 error
= zvol_get_stats(os
, nv
);
2129 error
= put_nvlist(zc
, nv
);
2138 * zc_name name of filesystem
2139 * zc_nvlist_dst_size size of buffer for property nvlist
2142 * zc_objset_stats stats
2143 * zc_nvlist_dst property nvlist
2144 * zc_nvlist_dst_size size of property nvlist
2147 zfs_ioc_objset_stats(zfs_cmd_t
*zc
)
2152 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
2154 error
= zfs_ioc_objset_stats_impl(zc
, os
);
2155 dmu_objset_rele(os
, FTAG
);
2163 * zc_name name of filesystem
2164 * zc_nvlist_dst_size size of buffer for property nvlist
2167 * zc_nvlist_dst received property nvlist
2168 * zc_nvlist_dst_size size of received property nvlist
2170 * Gets received properties (distinct from local properties on or after
2171 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
2172 * local property values.
2175 zfs_ioc_objset_recvd_props(zfs_cmd_t
*zc
)
2181 * Without this check, we would return local property values if the
2182 * caller has not already received properties on or after
2183 * SPA_VERSION_RECVD_PROPS.
2185 if (!dsl_prop_get_hasrecvd(zc
->zc_name
))
2186 return (SET_ERROR(ENOTSUP
));
2188 if (zc
->zc_nvlist_dst
!= 0 &&
2189 (error
= dsl_prop_get_received(zc
->zc_name
, &nv
)) == 0) {
2190 error
= put_nvlist(zc
, nv
);
2198 nvl_add_zplprop(objset_t
*os
, nvlist_t
*props
, zfs_prop_t prop
)
2204 * zfs_get_zplprop() will either find a value or give us
2205 * the default value (if there is one).
2207 if ((error
= zfs_get_zplprop(os
, prop
, &value
)) != 0)
2209 VERIFY(nvlist_add_uint64(props
, zfs_prop_to_name(prop
), value
) == 0);
2215 * zc_name name of filesystem
2216 * zc_nvlist_dst_size size of buffer for zpl property nvlist
2219 * zc_nvlist_dst zpl property nvlist
2220 * zc_nvlist_dst_size size of zpl property nvlist
2223 zfs_ioc_objset_zplprops(zfs_cmd_t
*zc
)
2228 /* XXX reading without owning */
2229 if ((err
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
)))
2232 dmu_objset_fast_stat(os
, &zc
->zc_objset_stats
);
2235 * NB: nvl_add_zplprop() will read the objset contents,
2236 * which we aren't supposed to do with a DS_MODE_USER
2237 * hold, because it could be inconsistent.
2239 if (zc
->zc_nvlist_dst
!= 0 &&
2240 !zc
->zc_objset_stats
.dds_inconsistent
&&
2241 dmu_objset_type(os
) == DMU_OST_ZFS
) {
2244 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2245 if ((err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_VERSION
)) == 0 &&
2246 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_NORMALIZE
)) == 0 &&
2247 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_UTF8ONLY
)) == 0 &&
2248 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_CASE
)) == 0)
2249 err
= put_nvlist(zc
, nv
);
2252 err
= SET_ERROR(ENOENT
);
2254 dmu_objset_rele(os
, FTAG
);
2260 * zc_name name of filesystem
2261 * zc_cookie zap cursor
2262 * zc_nvlist_dst_size size of buffer for property nvlist
2265 * zc_name name of next filesystem
2266 * zc_cookie zap cursor
2267 * zc_objset_stats stats
2268 * zc_nvlist_dst property nvlist
2269 * zc_nvlist_dst_size size of property nvlist
2272 zfs_ioc_dataset_list_next(zfs_cmd_t
*zc
)
2277 size_t orig_len
= strlen(zc
->zc_name
);
2280 if ((error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
))) {
2281 if (error
== ENOENT
)
2282 error
= SET_ERROR(ESRCH
);
2286 p
= strrchr(zc
->zc_name
, '/');
2287 if (p
== NULL
|| p
[1] != '\0')
2288 (void) strlcat(zc
->zc_name
, "/", sizeof (zc
->zc_name
));
2289 p
= zc
->zc_name
+ strlen(zc
->zc_name
);
2292 error
= dmu_dir_list_next(os
,
2293 sizeof (zc
->zc_name
) - (p
- zc
->zc_name
), p
,
2294 NULL
, &zc
->zc_cookie
);
2295 if (error
== ENOENT
)
2296 error
= SET_ERROR(ESRCH
);
2297 } while (error
== 0 && zfs_dataset_name_hidden(zc
->zc_name
));
2298 dmu_objset_rele(os
, FTAG
);
2301 * If it's an internal dataset (ie. with a '$' in its name),
2302 * don't try to get stats for it, otherwise we'll return ENOENT.
2304 if (error
== 0 && strchr(zc
->zc_name
, '$') == NULL
) {
2305 error
= zfs_ioc_objset_stats(zc
); /* fill in the stats */
2306 if (error
== ENOENT
) {
2307 /* We lost a race with destroy, get the next one. */
2308 zc
->zc_name
[orig_len
] = '\0';
2317 * zc_name name of filesystem
2318 * zc_cookie zap cursor
2319 * zc_nvlist_src iteration range nvlist
2320 * zc_nvlist_src_size size of iteration range nvlist
2323 * zc_name name of next snapshot
2324 * zc_objset_stats stats
2325 * zc_nvlist_dst property nvlist
2326 * zc_nvlist_dst_size size of property nvlist
2329 zfs_ioc_snapshot_list_next(zfs_cmd_t
*zc
)
2332 objset_t
*os
, *ossnap
;
2334 uint64_t min_txg
= 0, max_txg
= 0;
2336 if (zc
->zc_nvlist_src_size
!= 0) {
2337 nvlist_t
*props
= NULL
;
2338 error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2339 zc
->zc_iflags
, &props
);
2342 (void) nvlist_lookup_uint64(props
, SNAP_ITER_MIN_TXG
,
2344 (void) nvlist_lookup_uint64(props
, SNAP_ITER_MAX_TXG
,
2349 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
2351 return (error
== ENOENT
? ESRCH
: error
);
2355 * A dataset name of maximum length cannot have any snapshots,
2356 * so exit immediately.
2358 if (strlcat(zc
->zc_name
, "@", sizeof (zc
->zc_name
)) >=
2359 ZFS_MAX_DATASET_NAME_LEN
) {
2360 dmu_objset_rele(os
, FTAG
);
2361 return (SET_ERROR(ESRCH
));
2364 while (error
== 0) {
2365 if (issig(JUSTLOOKING
) && issig(FORREAL
)) {
2366 error
= SET_ERROR(EINTR
);
2370 error
= dmu_snapshot_list_next(os
,
2371 sizeof (zc
->zc_name
) - strlen(zc
->zc_name
),
2372 zc
->zc_name
+ strlen(zc
->zc_name
), &zc
->zc_obj
,
2373 &zc
->zc_cookie
, NULL
);
2374 if (error
== ENOENT
) {
2375 error
= SET_ERROR(ESRCH
);
2377 } else if (error
!= 0) {
2381 error
= dsl_dataset_hold_obj(dmu_objset_pool(os
), zc
->zc_obj
,
2386 if ((min_txg
!= 0 && dsl_get_creationtxg(ds
) < min_txg
) ||
2387 (max_txg
!= 0 && dsl_get_creationtxg(ds
) > max_txg
)) {
2388 dsl_dataset_rele(ds
, FTAG
);
2389 /* undo snapshot name append */
2390 *(strchr(zc
->zc_name
, '@') + 1) = '\0';
2395 if (zc
->zc_simple
) {
2396 dsl_dataset_rele(ds
, FTAG
);
2400 if ((error
= dmu_objset_from_ds(ds
, &ossnap
)) != 0) {
2401 dsl_dataset_rele(ds
, FTAG
);
2404 if ((error
= zfs_ioc_objset_stats_impl(zc
, ossnap
)) != 0) {
2405 dsl_dataset_rele(ds
, FTAG
);
2408 dsl_dataset_rele(ds
, FTAG
);
2412 dmu_objset_rele(os
, FTAG
);
2413 /* if we failed, undo the @ that we tacked on to zc_name */
2415 *strchr(zc
->zc_name
, '@') = '\0';
2420 zfs_prop_set_userquota(const char *dsname
, nvpair_t
*pair
)
2422 const char *propname
= nvpair_name(pair
);
2424 unsigned int vallen
;
2427 zfs_userquota_prop_t type
;
2433 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2435 VERIFY(nvpair_value_nvlist(pair
, &attrs
) == 0);
2436 if (nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
2438 return (SET_ERROR(EINVAL
));
2442 * A correctly constructed propname is encoded as
2443 * userquota@<rid>-<domain>.
2445 if ((dash
= strchr(propname
, '-')) == NULL
||
2446 nvpair_value_uint64_array(pair
, &valary
, &vallen
) != 0 ||
2448 return (SET_ERROR(EINVAL
));
2455 err
= zfsvfs_hold(dsname
, FTAG
, &zfsvfs
, B_FALSE
);
2457 err
= zfs_set_userquota(zfsvfs
, type
, domain
, rid
, quota
);
2458 zfsvfs_rele(zfsvfs
, FTAG
);
2465 * If the named property is one that has a special function to set its value,
2466 * return 0 on success and a positive error code on failure; otherwise if it is
2467 * not one of the special properties handled by this function, return -1.
2469 * XXX: It would be better for callers of the property interface if we handled
2470 * these special cases in dsl_prop.c (in the dsl layer).
2473 zfs_prop_set_special(const char *dsname
, zprop_source_t source
,
2476 const char *propname
= nvpair_name(pair
);
2477 zfs_prop_t prop
= zfs_name_to_prop(propname
);
2478 uint64_t intval
= 0;
2479 char *strval
= NULL
;
2482 if (prop
== ZPROP_INVAL
) {
2483 if (zfs_prop_userquota(propname
))
2484 return (zfs_prop_set_userquota(dsname
, pair
));
2488 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2490 VERIFY(nvpair_value_nvlist(pair
, &attrs
) == 0);
2491 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
2495 /* all special properties are numeric except for keylocation */
2496 if (zfs_prop_get_type(prop
) == PROP_TYPE_STRING
) {
2497 strval
= fnvpair_value_string(pair
);
2499 intval
= fnvpair_value_uint64(pair
);
2503 case ZFS_PROP_QUOTA
:
2504 err
= dsl_dir_set_quota(dsname
, source
, intval
);
2506 case ZFS_PROP_REFQUOTA
:
2507 err
= dsl_dataset_set_refquota(dsname
, source
, intval
);
2509 case ZFS_PROP_FILESYSTEM_LIMIT
:
2510 case ZFS_PROP_SNAPSHOT_LIMIT
:
2511 if (intval
== UINT64_MAX
) {
2512 /* clearing the limit, just do it */
2515 err
= dsl_dir_activate_fs_ss_limit(dsname
);
2518 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2519 * default path to set the value in the nvlist.
2524 case ZFS_PROP_KEYLOCATION
:
2525 err
= dsl_crypto_can_set_keylocation(dsname
, strval
);
2528 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2529 * default path to set the value in the nvlist.
2534 case ZFS_PROP_RESERVATION
:
2535 err
= dsl_dir_set_reservation(dsname
, source
, intval
);
2537 case ZFS_PROP_REFRESERVATION
:
2538 err
= dsl_dataset_set_refreservation(dsname
, source
, intval
);
2540 case ZFS_PROP_VOLSIZE
:
2541 err
= zvol_set_volsize(dsname
, intval
);
2543 case ZFS_PROP_SNAPDEV
:
2544 err
= zvol_set_snapdev(dsname
, source
, intval
);
2546 case ZFS_PROP_VOLMODE
:
2547 err
= zvol_set_volmode(dsname
, source
, intval
);
2549 case ZFS_PROP_VERSION
:
2553 if ((err
= zfsvfs_hold(dsname
, FTAG
, &zfsvfs
, B_TRUE
)) != 0)
2556 err
= zfs_set_version(zfsvfs
, intval
);
2557 zfsvfs_rele(zfsvfs
, FTAG
);
2559 if (err
== 0 && intval
>= ZPL_VERSION_USERSPACE
) {
2562 zc
= kmem_zalloc(sizeof (zfs_cmd_t
), KM_SLEEP
);
2563 (void) strcpy(zc
->zc_name
, dsname
);
2564 (void) zfs_ioc_userspace_upgrade(zc
);
2565 (void) zfs_ioc_id_quota_upgrade(zc
);
2566 kmem_free(zc
, sizeof (zfs_cmd_t
));
2578 * This function is best effort. If it fails to set any of the given properties,
2579 * it continues to set as many as it can and returns the last error
2580 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2581 * with the list of names of all the properties that failed along with the
2582 * corresponding error numbers.
2584 * If every property is set successfully, zero is returned and errlist is not
2588 zfs_set_prop_nvlist(const char *dsname
, zprop_source_t source
, nvlist_t
*nvl
,
2597 nvlist_t
*genericnvl
= fnvlist_alloc();
2598 nvlist_t
*retrynvl
= fnvlist_alloc();
2601 while ((pair
= nvlist_next_nvpair(nvl
, pair
)) != NULL
) {
2602 const char *propname
= nvpair_name(pair
);
2603 zfs_prop_t prop
= zfs_name_to_prop(propname
);
2606 /* decode the property value */
2608 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2610 attrs
= fnvpair_value_nvlist(pair
);
2611 if (nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
2613 err
= SET_ERROR(EINVAL
);
2616 /* Validate value type */
2617 if (err
== 0 && source
== ZPROP_SRC_INHERITED
) {
2618 /* inherited properties are expected to be booleans */
2619 if (nvpair_type(propval
) != DATA_TYPE_BOOLEAN
)
2620 err
= SET_ERROR(EINVAL
);
2621 } else if (err
== 0 && prop
== ZPROP_INVAL
) {
2622 if (zfs_prop_user(propname
)) {
2623 if (nvpair_type(propval
) != DATA_TYPE_STRING
)
2624 err
= SET_ERROR(EINVAL
);
2625 } else if (zfs_prop_userquota(propname
)) {
2626 if (nvpair_type(propval
) !=
2627 DATA_TYPE_UINT64_ARRAY
)
2628 err
= SET_ERROR(EINVAL
);
2630 err
= SET_ERROR(EINVAL
);
2632 } else if (err
== 0) {
2633 if (nvpair_type(propval
) == DATA_TYPE_STRING
) {
2634 if (zfs_prop_get_type(prop
) != PROP_TYPE_STRING
)
2635 err
= SET_ERROR(EINVAL
);
2636 } else if (nvpair_type(propval
) == DATA_TYPE_UINT64
) {
2639 intval
= fnvpair_value_uint64(propval
);
2641 switch (zfs_prop_get_type(prop
)) {
2642 case PROP_TYPE_NUMBER
:
2644 case PROP_TYPE_STRING
:
2645 err
= SET_ERROR(EINVAL
);
2647 case PROP_TYPE_INDEX
:
2648 if (zfs_prop_index_to_string(prop
,
2649 intval
, &unused
) != 0)
2650 err
= SET_ERROR(EINVAL
);
2654 "unknown property type");
2657 err
= SET_ERROR(EINVAL
);
2661 /* Validate permissions */
2663 err
= zfs_check_settable(dsname
, pair
, CRED());
2666 if (source
== ZPROP_SRC_INHERITED
)
2667 err
= -1; /* does not need special handling */
2669 err
= zfs_prop_set_special(dsname
, source
,
2673 * For better performance we build up a list of
2674 * properties to set in a single transaction.
2676 err
= nvlist_add_nvpair(genericnvl
, pair
);
2677 } else if (err
!= 0 && nvl
!= retrynvl
) {
2679 * This may be a spurious error caused by
2680 * receiving quota and reservation out of order.
2681 * Try again in a second pass.
2683 err
= nvlist_add_nvpair(retrynvl
, pair
);
2688 if (errlist
!= NULL
)
2689 fnvlist_add_int32(errlist
, propname
, err
);
2694 if (nvl
!= retrynvl
&& !nvlist_empty(retrynvl
)) {
2699 if (!nvlist_empty(genericnvl
) &&
2700 dsl_props_set(dsname
, source
, genericnvl
) != 0) {
2702 * If this fails, we still want to set as many properties as we
2703 * can, so try setting them individually.
2706 while ((pair
= nvlist_next_nvpair(genericnvl
, pair
)) != NULL
) {
2707 const char *propname
= nvpair_name(pair
);
2711 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2713 attrs
= fnvpair_value_nvlist(pair
);
2714 propval
= fnvlist_lookup_nvpair(attrs
,
2718 if (nvpair_type(propval
) == DATA_TYPE_STRING
) {
2719 strval
= fnvpair_value_string(propval
);
2720 err
= dsl_prop_set_string(dsname
, propname
,
2722 } else if (nvpair_type(propval
) == DATA_TYPE_BOOLEAN
) {
2723 err
= dsl_prop_inherit(dsname
, propname
,
2726 intval
= fnvpair_value_uint64(propval
);
2727 err
= dsl_prop_set_int(dsname
, propname
, source
,
2732 if (errlist
!= NULL
) {
2733 fnvlist_add_int32(errlist
, propname
,
2740 nvlist_free(genericnvl
);
2741 nvlist_free(retrynvl
);
2747 * Check that all the properties are valid user properties.
2750 zfs_check_userprops(const char *fsname
, nvlist_t
*nvl
)
2752 nvpair_t
*pair
= NULL
;
2755 while ((pair
= nvlist_next_nvpair(nvl
, pair
)) != NULL
) {
2756 const char *propname
= nvpair_name(pair
);
2758 if (!zfs_prop_user(propname
) ||
2759 nvpair_type(pair
) != DATA_TYPE_STRING
)
2760 return (SET_ERROR(EINVAL
));
2762 if ((error
= zfs_secpolicy_write_perms(fsname
,
2763 ZFS_DELEG_PERM_USERPROP
, CRED())))
2766 if (strlen(propname
) >= ZAP_MAXNAMELEN
)
2767 return (SET_ERROR(ENAMETOOLONG
));
2769 if (strlen(fnvpair_value_string(pair
)) >= ZAP_MAXVALUELEN
)
2770 return (SET_ERROR(E2BIG
));
2776 props_skip(nvlist_t
*props
, nvlist_t
*skipped
, nvlist_t
**newprops
)
2780 VERIFY(nvlist_alloc(newprops
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2783 while ((pair
= nvlist_next_nvpair(props
, pair
)) != NULL
) {
2784 if (nvlist_exists(skipped
, nvpair_name(pair
)))
2787 VERIFY(nvlist_add_nvpair(*newprops
, pair
) == 0);
2792 clear_received_props(const char *dsname
, nvlist_t
*props
,
2796 nvlist_t
*cleared_props
= NULL
;
2797 props_skip(props
, skipped
, &cleared_props
);
2798 if (!nvlist_empty(cleared_props
)) {
2800 * Acts on local properties until the dataset has received
2801 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2803 zprop_source_t flags
= (ZPROP_SRC_NONE
|
2804 (dsl_prop_get_hasrecvd(dsname
) ? ZPROP_SRC_RECEIVED
: 0));
2805 err
= zfs_set_prop_nvlist(dsname
, flags
, cleared_props
, NULL
);
2807 nvlist_free(cleared_props
);
2813 * zc_name name of filesystem
2814 * zc_value name of property to set
2815 * zc_nvlist_src{_size} nvlist of properties to apply
2816 * zc_cookie received properties flag
2819 * zc_nvlist_dst{_size} error for each unapplied received property
2822 zfs_ioc_set_prop(zfs_cmd_t
*zc
)
2825 boolean_t received
= zc
->zc_cookie
;
2826 zprop_source_t source
= (received
? ZPROP_SRC_RECEIVED
:
2831 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2832 zc
->zc_iflags
, &nvl
)) != 0)
2836 nvlist_t
*origprops
;
2838 if (dsl_prop_get_received(zc
->zc_name
, &origprops
) == 0) {
2839 (void) clear_received_props(zc
->zc_name
,
2841 nvlist_free(origprops
);
2844 error
= dsl_prop_set_hasrecvd(zc
->zc_name
);
2847 errors
= fnvlist_alloc();
2849 error
= zfs_set_prop_nvlist(zc
->zc_name
, source
, nvl
, errors
);
2851 if (zc
->zc_nvlist_dst
!= 0 && errors
!= NULL
) {
2852 (void) put_nvlist(zc
, errors
);
2855 nvlist_free(errors
);
2862 * zc_name name of filesystem
2863 * zc_value name of property to inherit
2864 * zc_cookie revert to received value if TRUE
2869 zfs_ioc_inherit_prop(zfs_cmd_t
*zc
)
2871 const char *propname
= zc
->zc_value
;
2872 zfs_prop_t prop
= zfs_name_to_prop(propname
);
2873 boolean_t received
= zc
->zc_cookie
;
2874 zprop_source_t source
= (received
2875 ? ZPROP_SRC_NONE
/* revert to received value, if any */
2876 : ZPROP_SRC_INHERITED
); /* explicitly inherit */
2884 * Only check this in the non-received case. We want to allow
2885 * 'inherit -S' to revert non-inheritable properties like quota
2886 * and reservation to the received or default values even though
2887 * they are not considered inheritable.
2889 if (prop
!= ZPROP_INVAL
&& !zfs_prop_inheritable(prop
))
2890 return (SET_ERROR(EINVAL
));
2893 if (prop
== ZPROP_INVAL
) {
2894 if (!zfs_prop_user(propname
))
2895 return (SET_ERROR(EINVAL
));
2897 type
= PROP_TYPE_STRING
;
2898 } else if (prop
== ZFS_PROP_VOLSIZE
|| prop
== ZFS_PROP_VERSION
) {
2899 return (SET_ERROR(EINVAL
));
2901 type
= zfs_prop_get_type(prop
);
2905 * zfs_prop_set_special() expects properties in the form of an
2906 * nvpair with type info.
2908 dummy
= fnvlist_alloc();
2911 case PROP_TYPE_STRING
:
2912 VERIFY(0 == nvlist_add_string(dummy
, propname
, ""));
2914 case PROP_TYPE_NUMBER
:
2915 case PROP_TYPE_INDEX
:
2916 VERIFY(0 == nvlist_add_uint64(dummy
, propname
, 0));
2919 err
= SET_ERROR(EINVAL
);
2923 pair
= nvlist_next_nvpair(dummy
, NULL
);
2925 err
= SET_ERROR(EINVAL
);
2927 err
= zfs_prop_set_special(zc
->zc_name
, source
, pair
);
2928 if (err
== -1) /* property is not "special", needs handling */
2929 err
= dsl_prop_inherit(zc
->zc_name
, zc
->zc_value
,
2939 zfs_ioc_pool_set_props(zfs_cmd_t
*zc
)
2946 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2947 zc
->zc_iflags
, &props
)))
2951 * If the only property is the configfile, then just do a spa_lookup()
2952 * to handle the faulted case.
2954 pair
= nvlist_next_nvpair(props
, NULL
);
2955 if (pair
!= NULL
&& strcmp(nvpair_name(pair
),
2956 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
)) == 0 &&
2957 nvlist_next_nvpair(props
, pair
) == NULL
) {
2958 mutex_enter(&spa_namespace_lock
);
2959 if ((spa
= spa_lookup(zc
->zc_name
)) != NULL
) {
2960 spa_configfile_set(spa
, props
, B_FALSE
);
2961 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
2963 mutex_exit(&spa_namespace_lock
);
2970 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0) {
2975 error
= spa_prop_set(spa
, props
);
2978 spa_close(spa
, FTAG
);
2984 zfs_ioc_pool_get_props(zfs_cmd_t
*zc
)
2988 nvlist_t
*nvp
= NULL
;
2990 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0) {
2992 * If the pool is faulted, there may be properties we can still
2993 * get (such as altroot and cachefile), so attempt to get them
2996 mutex_enter(&spa_namespace_lock
);
2997 if ((spa
= spa_lookup(zc
->zc_name
)) != NULL
)
2998 error
= spa_prop_get(spa
, &nvp
);
2999 mutex_exit(&spa_namespace_lock
);
3001 error
= spa_prop_get(spa
, &nvp
);
3002 spa_close(spa
, FTAG
);
3005 if (error
== 0 && zc
->zc_nvlist_dst
!= 0)
3006 error
= put_nvlist(zc
, nvp
);
3008 error
= SET_ERROR(EFAULT
);
3016 * zc_name name of filesystem
3017 * zc_nvlist_src{_size} nvlist of delegated permissions
3018 * zc_perm_action allow/unallow flag
3023 zfs_ioc_set_fsacl(zfs_cmd_t
*zc
)
3026 nvlist_t
*fsaclnv
= NULL
;
3028 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
3029 zc
->zc_iflags
, &fsaclnv
)) != 0)
3033 * Verify nvlist is constructed correctly
3035 if ((error
= zfs_deleg_verify_nvlist(fsaclnv
)) != 0) {
3036 nvlist_free(fsaclnv
);
3037 return (SET_ERROR(EINVAL
));
3041 * If we don't have PRIV_SYS_MOUNT, then validate
3042 * that user is allowed to hand out each permission in
3046 error
= secpolicy_zfs(CRED());
3048 if (zc
->zc_perm_action
== B_FALSE
) {
3049 error
= dsl_deleg_can_allow(zc
->zc_name
,
3052 error
= dsl_deleg_can_unallow(zc
->zc_name
,
3058 error
= dsl_deleg_set(zc
->zc_name
, fsaclnv
, zc
->zc_perm_action
);
3060 nvlist_free(fsaclnv
);
3066 * zc_name name of filesystem
3069 * zc_nvlist_src{_size} nvlist of delegated permissions
3072 zfs_ioc_get_fsacl(zfs_cmd_t
*zc
)
3077 if ((error
= dsl_deleg_get(zc
->zc_name
, &nvp
)) == 0) {
3078 error
= put_nvlist(zc
, nvp
);
3087 zfs_create_cb(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
)
3089 zfs_creat_t
*zct
= arg
;
3091 zfs_create_fs(os
, cr
, zct
->zct_zplprops
, tx
);
3094 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
3098 * os parent objset pointer (NULL if root fs)
3099 * fuids_ok fuids allowed in this version of the spa?
3100 * sa_ok SAs allowed in this version of the spa?
3101 * createprops list of properties requested by creator
3104 * zplprops values for the zplprops we attach to the master node object
3105 * is_ci true if requested file system will be purely case-insensitive
3107 * Determine the settings for utf8only, normalization and
3108 * casesensitivity. Specific values may have been requested by the
3109 * creator and/or we can inherit values from the parent dataset. If
3110 * the file system is of too early a vintage, a creator can not
3111 * request settings for these properties, even if the requested
3112 * setting is the default value. We don't actually want to create dsl
3113 * properties for these, so remove them from the source nvlist after
3117 zfs_fill_zplprops_impl(objset_t
*os
, uint64_t zplver
,
3118 boolean_t fuids_ok
, boolean_t sa_ok
, nvlist_t
*createprops
,
3119 nvlist_t
*zplprops
, boolean_t
*is_ci
)
3121 uint64_t sense
= ZFS_PROP_UNDEFINED
;
3122 uint64_t norm
= ZFS_PROP_UNDEFINED
;
3123 uint64_t u8
= ZFS_PROP_UNDEFINED
;
3126 ASSERT(zplprops
!= NULL
);
3128 /* parent dataset must be a filesystem */
3129 if (os
!= NULL
&& os
->os_phys
->os_type
!= DMU_OST_ZFS
)
3130 return (SET_ERROR(ZFS_ERR_WRONG_PARENT
));
3133 * Pull out creator prop choices, if any.
3136 (void) nvlist_lookup_uint64(createprops
,
3137 zfs_prop_to_name(ZFS_PROP_VERSION
), &zplver
);
3138 (void) nvlist_lookup_uint64(createprops
,
3139 zfs_prop_to_name(ZFS_PROP_NORMALIZE
), &norm
);
3140 (void) nvlist_remove_all(createprops
,
3141 zfs_prop_to_name(ZFS_PROP_NORMALIZE
));
3142 (void) nvlist_lookup_uint64(createprops
,
3143 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), &u8
);
3144 (void) nvlist_remove_all(createprops
,
3145 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
));
3146 (void) nvlist_lookup_uint64(createprops
,
3147 zfs_prop_to_name(ZFS_PROP_CASE
), &sense
);
3148 (void) nvlist_remove_all(createprops
,
3149 zfs_prop_to_name(ZFS_PROP_CASE
));
3153 * If the zpl version requested is whacky or the file system
3154 * or pool is version is too "young" to support normalization
3155 * and the creator tried to set a value for one of the props,
3158 if ((zplver
< ZPL_VERSION_INITIAL
|| zplver
> ZPL_VERSION
) ||
3159 (zplver
>= ZPL_VERSION_FUID
&& !fuids_ok
) ||
3160 (zplver
>= ZPL_VERSION_SA
&& !sa_ok
) ||
3161 (zplver
< ZPL_VERSION_NORMALIZATION
&&
3162 (norm
!= ZFS_PROP_UNDEFINED
|| u8
!= ZFS_PROP_UNDEFINED
||
3163 sense
!= ZFS_PROP_UNDEFINED
)))
3164 return (SET_ERROR(ENOTSUP
));
3167 * Put the version in the zplprops
3169 VERIFY(nvlist_add_uint64(zplprops
,
3170 zfs_prop_to_name(ZFS_PROP_VERSION
), zplver
) == 0);
3172 if (norm
== ZFS_PROP_UNDEFINED
&&
3173 (error
= zfs_get_zplprop(os
, ZFS_PROP_NORMALIZE
, &norm
)) != 0)
3175 VERIFY(nvlist_add_uint64(zplprops
,
3176 zfs_prop_to_name(ZFS_PROP_NORMALIZE
), norm
) == 0);
3179 * If we're normalizing, names must always be valid UTF-8 strings.
3183 if (u8
== ZFS_PROP_UNDEFINED
&&
3184 (error
= zfs_get_zplprop(os
, ZFS_PROP_UTF8ONLY
, &u8
)) != 0)
3186 VERIFY(nvlist_add_uint64(zplprops
,
3187 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), u8
) == 0);
3189 if (sense
== ZFS_PROP_UNDEFINED
&&
3190 (error
= zfs_get_zplprop(os
, ZFS_PROP_CASE
, &sense
)) != 0)
3192 VERIFY(nvlist_add_uint64(zplprops
,
3193 zfs_prop_to_name(ZFS_PROP_CASE
), sense
) == 0);
3196 *is_ci
= (sense
== ZFS_CASE_INSENSITIVE
);
3202 zfs_fill_zplprops(const char *dataset
, nvlist_t
*createprops
,
3203 nvlist_t
*zplprops
, boolean_t
*is_ci
)
3205 boolean_t fuids_ok
, sa_ok
;
3206 uint64_t zplver
= ZPL_VERSION
;
3207 objset_t
*os
= NULL
;
3208 char parentname
[ZFS_MAX_DATASET_NAME_LEN
];
3213 zfs_get_parent(dataset
, parentname
, sizeof (parentname
));
3215 if ((error
= spa_open(dataset
, &spa
, FTAG
)) != 0)
3218 spa_vers
= spa_version(spa
);
3219 spa_close(spa
, FTAG
);
3221 zplver
= zfs_zpl_version_map(spa_vers
);
3222 fuids_ok
= (zplver
>= ZPL_VERSION_FUID
);
3223 sa_ok
= (zplver
>= ZPL_VERSION_SA
);
3226 * Open parent object set so we can inherit zplprop values.
3228 if ((error
= dmu_objset_hold(parentname
, FTAG
, &os
)) != 0)
3231 error
= zfs_fill_zplprops_impl(os
, zplver
, fuids_ok
, sa_ok
, createprops
,
3233 dmu_objset_rele(os
, FTAG
);
3238 zfs_fill_zplprops_root(uint64_t spa_vers
, nvlist_t
*createprops
,
3239 nvlist_t
*zplprops
, boolean_t
*is_ci
)
3243 uint64_t zplver
= ZPL_VERSION
;
3246 zplver
= zfs_zpl_version_map(spa_vers
);
3247 fuids_ok
= (zplver
>= ZPL_VERSION_FUID
);
3248 sa_ok
= (zplver
>= ZPL_VERSION_SA
);
3250 error
= zfs_fill_zplprops_impl(NULL
, zplver
, fuids_ok
, sa_ok
,
3251 createprops
, zplprops
, is_ci
);
3257 * "type" -> dmu_objset_type_t (int32)
3258 * (optional) "props" -> { prop -> value }
3259 * (optional) "hidden_args" -> { "wkeydata" -> value }
3260 * raw uint8_t array of encryption wrapping key data (32 bytes)
3263 * outnvl: propname -> error code (int32)
3266 static const zfs_ioc_key_t zfs_keys_create
[] = {
3267 {"type", DATA_TYPE_INT32
, 0},
3268 {"props", DATA_TYPE_NVLIST
, ZK_OPTIONAL
},
3269 {"hidden_args", DATA_TYPE_NVLIST
, ZK_OPTIONAL
},
3273 zfs_ioc_create(const char *fsname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3276 zfs_creat_t zct
= { 0 };
3277 nvlist_t
*nvprops
= NULL
;
3278 nvlist_t
*hidden_args
= NULL
;
3279 void (*cbfunc
)(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
);
3280 dmu_objset_type_t type
;
3281 boolean_t is_insensitive
= B_FALSE
;
3282 dsl_crypto_params_t
*dcp
= NULL
;
3284 type
= (dmu_objset_type_t
)fnvlist_lookup_int32(innvl
, "type");
3285 (void) nvlist_lookup_nvlist(innvl
, "props", &nvprops
);
3286 (void) nvlist_lookup_nvlist(innvl
, ZPOOL_HIDDEN_ARGS
, &hidden_args
);
3290 cbfunc
= zfs_create_cb
;
3294 cbfunc
= zvol_create_cb
;
3301 if (strchr(fsname
, '@') ||
3302 strchr(fsname
, '%'))
3303 return (SET_ERROR(EINVAL
));
3305 zct
.zct_props
= nvprops
;
3308 return (SET_ERROR(EINVAL
));
3310 if (type
== DMU_OST_ZVOL
) {
3311 uint64_t volsize
, volblocksize
;
3313 if (nvprops
== NULL
)
3314 return (SET_ERROR(EINVAL
));
3315 if (nvlist_lookup_uint64(nvprops
,
3316 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &volsize
) != 0)
3317 return (SET_ERROR(EINVAL
));
3319 if ((error
= nvlist_lookup_uint64(nvprops
,
3320 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
3321 &volblocksize
)) != 0 && error
!= ENOENT
)
3322 return (SET_ERROR(EINVAL
));
3325 volblocksize
= zfs_prop_default_numeric(
3326 ZFS_PROP_VOLBLOCKSIZE
);
3328 if ((error
= zvol_check_volblocksize(fsname
,
3329 volblocksize
)) != 0 ||
3330 (error
= zvol_check_volsize(volsize
,
3331 volblocksize
)) != 0)
3333 } else if (type
== DMU_OST_ZFS
) {
3337 * We have to have normalization and
3338 * case-folding flags correct when we do the
3339 * file system creation, so go figure them out
3342 VERIFY(nvlist_alloc(&zct
.zct_zplprops
,
3343 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3344 error
= zfs_fill_zplprops(fsname
, nvprops
,
3345 zct
.zct_zplprops
, &is_insensitive
);
3347 nvlist_free(zct
.zct_zplprops
);
3352 error
= dsl_crypto_params_create_nvlist(DCP_CMD_NONE
, nvprops
,
3355 nvlist_free(zct
.zct_zplprops
);
3359 error
= dmu_objset_create(fsname
, type
,
3360 is_insensitive
? DS_FLAG_CI_DATASET
: 0, dcp
, cbfunc
, &zct
);
3362 nvlist_free(zct
.zct_zplprops
);
3363 dsl_crypto_params_free(dcp
, !!error
);
3366 * It would be nice to do this atomically.
3369 error
= zfs_set_prop_nvlist(fsname
, ZPROP_SRC_LOCAL
,
3376 * Volumes will return EBUSY and cannot be destroyed
3377 * until all asynchronous minor handling has completed.
3378 * Wait for the spa_zvol_taskq to drain then retry.
3380 error2
= dsl_destroy_head(fsname
);
3381 while ((error2
== EBUSY
) && (type
== DMU_OST_ZVOL
)) {
3382 error2
= spa_open(fsname
, &spa
, FTAG
);
3384 taskq_wait(spa
->spa_zvol_taskq
);
3385 spa_close(spa
, FTAG
);
3387 error2
= dsl_destroy_head(fsname
);
3396 * "origin" -> name of origin snapshot
3397 * (optional) "props" -> { prop -> value }
3398 * (optional) "hidden_args" -> { "wkeydata" -> value }
3399 * raw uint8_t array of encryption wrapping key data (32 bytes)
3403 * outnvl: propname -> error code (int32)
3405 static const zfs_ioc_key_t zfs_keys_clone
[] = {
3406 {"origin", DATA_TYPE_STRING
, 0},
3407 {"props", DATA_TYPE_NVLIST
, ZK_OPTIONAL
},
3408 {"hidden_args", DATA_TYPE_NVLIST
, ZK_OPTIONAL
},
3412 zfs_ioc_clone(const char *fsname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3415 nvlist_t
*nvprops
= NULL
;
3418 origin_name
= fnvlist_lookup_string(innvl
, "origin");
3419 (void) nvlist_lookup_nvlist(innvl
, "props", &nvprops
);
3421 if (strchr(fsname
, '@') ||
3422 strchr(fsname
, '%'))
3423 return (SET_ERROR(EINVAL
));
3425 if (dataset_namecheck(origin_name
, NULL
, NULL
) != 0)
3426 return (SET_ERROR(EINVAL
));
3428 error
= dmu_objset_clone(fsname
, origin_name
);
3431 * It would be nice to do this atomically.
3434 error
= zfs_set_prop_nvlist(fsname
, ZPROP_SRC_LOCAL
,
3437 (void) dsl_destroy_head(fsname
);
3442 static const zfs_ioc_key_t zfs_keys_remap
[] = {
3448 zfs_ioc_remap(const char *fsname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3450 if (strchr(fsname
, '@') ||
3451 strchr(fsname
, '%'))
3452 return (SET_ERROR(EINVAL
));
3454 return (dmu_objset_remap_indirects(fsname
));
3459 * "snaps" -> { snapshot1, snapshot2 }
3460 * (optional) "props" -> { prop -> value (string) }
3463 * outnvl: snapshot -> error code (int32)
3465 static const zfs_ioc_key_t zfs_keys_snapshot
[] = {
3466 {"snaps", DATA_TYPE_NVLIST
, 0},
3467 {"props", DATA_TYPE_NVLIST
, ZK_OPTIONAL
},
3471 zfs_ioc_snapshot(const char *poolname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3474 nvlist_t
*props
= NULL
;
3478 (void) nvlist_lookup_nvlist(innvl
, "props", &props
);
3479 if ((error
= zfs_check_userprops(poolname
, props
)) != 0)
3482 if (!nvlist_empty(props
) &&
3483 zfs_earlier_version(poolname
, SPA_VERSION_SNAP_PROPS
))
3484 return (SET_ERROR(ENOTSUP
));
3486 snaps
= fnvlist_lookup_nvlist(innvl
, "snaps");
3487 poollen
= strlen(poolname
);
3488 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
3489 pair
= nvlist_next_nvpair(snaps
, pair
)) {
3490 const char *name
= nvpair_name(pair
);
3491 const char *cp
= strchr(name
, '@');
3494 * The snap name must contain an @, and the part after it must
3495 * contain only valid characters.
3498 zfs_component_namecheck(cp
+ 1, NULL
, NULL
) != 0)
3499 return (SET_ERROR(EINVAL
));
3502 * The snap must be in the specified pool.
3504 if (strncmp(name
, poolname
, poollen
) != 0 ||
3505 (name
[poollen
] != '/' && name
[poollen
] != '@'))
3506 return (SET_ERROR(EXDEV
));
3508 /* This must be the only snap of this fs. */
3509 for (nvpair_t
*pair2
= nvlist_next_nvpair(snaps
, pair
);
3510 pair2
!= NULL
; pair2
= nvlist_next_nvpair(snaps
, pair2
)) {
3511 if (strncmp(name
, nvpair_name(pair2
), cp
- name
+ 1)
3513 return (SET_ERROR(EXDEV
));
3518 error
= dsl_dataset_snapshot(snaps
, props
, outnvl
);
3524 * innvl: "message" -> string
3526 static const zfs_ioc_key_t zfs_keys_log_history
[] = {
3527 {"message", DATA_TYPE_STRING
, 0},
3532 zfs_ioc_log_history(const char *unused
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3540 * The poolname in the ioctl is not set, we get it from the TSD,
3541 * which was set at the end of the last successful ioctl that allows
3542 * logging. The secpolicy func already checked that it is set.
3543 * Only one log ioctl is allowed after each successful ioctl, so
3544 * we clear the TSD here.
3546 poolname
= tsd_get(zfs_allow_log_key
);
3547 if (poolname
== NULL
)
3548 return (SET_ERROR(EINVAL
));
3549 (void) tsd_set(zfs_allow_log_key
, NULL
);
3550 error
= spa_open(poolname
, &spa
, FTAG
);
3555 message
= fnvlist_lookup_string(innvl
, "message");
3557 if (spa_version(spa
) < SPA_VERSION_ZPOOL_HISTORY
) {
3558 spa_close(spa
, FTAG
);
3559 return (SET_ERROR(ENOTSUP
));
3562 error
= spa_history_log(spa
, message
);
3563 spa_close(spa
, FTAG
);
3568 * The dp_config_rwlock must not be held when calling this, because the
3569 * unmount may need to write out data.
3571 * This function is best-effort. Callers must deal gracefully if it
3572 * remains mounted (or is remounted after this call).
3574 * Returns 0 if the argument is not a snapshot, or it is not currently a
3575 * filesystem, or we were able to unmount it. Returns error code otherwise.
3578 zfs_unmount_snap(const char *snapname
)
3580 if (strchr(snapname
, '@') == NULL
)
3583 (void) zfsctl_snapshot_unmount((char *)snapname
, MNT_FORCE
);
3588 zfs_unmount_snap_cb(const char *snapname
, void *arg
)
3590 zfs_unmount_snap(snapname
);
3595 * When a clone is destroyed, its origin may also need to be destroyed,
3596 * in which case it must be unmounted. This routine will do that unmount
3600 zfs_destroy_unmount_origin(const char *fsname
)
3606 error
= dmu_objset_hold(fsname
, FTAG
, &os
);
3609 ds
= dmu_objset_ds(os
);
3610 if (dsl_dir_is_clone(ds
->ds_dir
) && DS_IS_DEFER_DESTROY(ds
->ds_prev
)) {
3611 char originname
[ZFS_MAX_DATASET_NAME_LEN
];
3612 dsl_dataset_name(ds
->ds_prev
, originname
);
3613 dmu_objset_rele(os
, FTAG
);
3614 zfs_unmount_snap(originname
);
3616 dmu_objset_rele(os
, FTAG
);
3622 * "snaps" -> { snapshot1, snapshot2 }
3623 * (optional boolean) "defer"
3626 * outnvl: snapshot -> error code (int32)
3628 static const zfs_ioc_key_t zfs_keys_destroy_snaps
[] = {
3629 {"snaps", DATA_TYPE_NVLIST
, 0},
3630 {"defer", DATA_TYPE_BOOLEAN
, ZK_OPTIONAL
},
3635 zfs_ioc_destroy_snaps(const char *poolname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3641 snaps
= fnvlist_lookup_nvlist(innvl
, "snaps");
3642 defer
= nvlist_exists(innvl
, "defer");
3644 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
3645 pair
= nvlist_next_nvpair(snaps
, pair
)) {
3646 zfs_unmount_snap(nvpair_name(pair
));
3649 return (dsl_destroy_snapshots_nvl(snaps
, defer
, outnvl
));
3653 * Create bookmarks. Bookmark names are of the form <fs>#<bmark>.
3654 * All bookmarks must be in the same pool.
3657 * bookmark1 -> snapshot1, bookmark2 -> snapshot2
3660 * outnvl: bookmark -> error code (int32)
3663 static const zfs_ioc_key_t zfs_keys_bookmark
[] = {
3664 {"<bookmark>...", DATA_TYPE_STRING
, ZK_WILDCARDLIST
},
3669 zfs_ioc_bookmark(const char *poolname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3671 for (nvpair_t
*pair
= nvlist_next_nvpair(innvl
, NULL
);
3672 pair
!= NULL
; pair
= nvlist_next_nvpair(innvl
, pair
)) {
3676 * Verify the snapshot argument.
3678 if (nvpair_value_string(pair
, &snap_name
) != 0)
3679 return (SET_ERROR(EINVAL
));
3682 /* Verify that the keys (bookmarks) are unique */
3683 for (nvpair_t
*pair2
= nvlist_next_nvpair(innvl
, pair
);
3684 pair2
!= NULL
; pair2
= nvlist_next_nvpair(innvl
, pair2
)) {
3685 if (strcmp(nvpair_name(pair
), nvpair_name(pair2
)) == 0)
3686 return (SET_ERROR(EINVAL
));
3690 return (dsl_bookmark_create(innvl
, outnvl
));
3695 * property 1, property 2, ...
3699 * bookmark name 1 -> { property 1, property 2, ... },
3700 * bookmark name 2 -> { property 1, property 2, ... }
3704 static const zfs_ioc_key_t zfs_keys_get_bookmarks
[] = {
3705 {"<property>...", DATA_TYPE_BOOLEAN
, ZK_WILDCARDLIST
| ZK_OPTIONAL
},
3709 zfs_ioc_get_bookmarks(const char *fsname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3711 return (dsl_get_bookmarks(fsname
, innvl
, outnvl
));
3715 * innvl is not used.
3718 * property 1, property 2, ...
3722 static const zfs_ioc_key_t zfs_keys_get_bookmark_props
[] = {
3728 zfs_ioc_get_bookmark_props(const char *bookmark
, nvlist_t
*innvl
,
3731 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
3734 bmname
= strchr(bookmark
, '#');
3736 return (SET_ERROR(EINVAL
));
3739 (void) strlcpy(fsname
, bookmark
, sizeof (fsname
));
3740 *(strchr(fsname
, '#')) = '\0';
3742 return (dsl_get_bookmark_props(fsname
, bmname
, outnvl
));
3747 * bookmark name 1, bookmark name 2
3750 * outnvl: bookmark -> error code (int32)
3753 static const zfs_ioc_key_t zfs_keys_destroy_bookmarks
[] = {
3754 {"<bookmark>...", DATA_TYPE_BOOLEAN
, ZK_WILDCARDLIST
},
3758 zfs_ioc_destroy_bookmarks(const char *poolname
, nvlist_t
*innvl
,
3763 poollen
= strlen(poolname
);
3764 for (nvpair_t
*pair
= nvlist_next_nvpair(innvl
, NULL
);
3765 pair
!= NULL
; pair
= nvlist_next_nvpair(innvl
, pair
)) {
3766 const char *name
= nvpair_name(pair
);
3767 const char *cp
= strchr(name
, '#');
3770 * The bookmark name must contain an #, and the part after it
3771 * must contain only valid characters.
3774 zfs_component_namecheck(cp
+ 1, NULL
, NULL
) != 0)
3775 return (SET_ERROR(EINVAL
));
3778 * The bookmark must be in the specified pool.
3780 if (strncmp(name
, poolname
, poollen
) != 0 ||
3781 (name
[poollen
] != '/' && name
[poollen
] != '#'))
3782 return (SET_ERROR(EXDEV
));
3785 error
= dsl_bookmark_destroy(innvl
, outnvl
);
3789 static const zfs_ioc_key_t zfs_keys_channel_program
[] = {
3790 {"program", DATA_TYPE_STRING
, 0},
3791 {"arg", DATA_TYPE_ANY
, 0},
3792 {"sync", DATA_TYPE_BOOLEAN_VALUE
, ZK_OPTIONAL
},
3793 {"instrlimit", DATA_TYPE_UINT64
, ZK_OPTIONAL
},
3794 {"memlimit", DATA_TYPE_UINT64
, ZK_OPTIONAL
},
3798 zfs_ioc_channel_program(const char *poolname
, nvlist_t
*innvl
,
3802 uint64_t instrlimit
, memlimit
;
3803 boolean_t sync_flag
;
3804 nvpair_t
*nvarg
= NULL
;
3806 program
= fnvlist_lookup_string(innvl
, ZCP_ARG_PROGRAM
);
3807 if (0 != nvlist_lookup_boolean_value(innvl
, ZCP_ARG_SYNC
, &sync_flag
)) {
3810 if (0 != nvlist_lookup_uint64(innvl
, ZCP_ARG_INSTRLIMIT
, &instrlimit
)) {
3811 instrlimit
= ZCP_DEFAULT_INSTRLIMIT
;
3813 if (0 != nvlist_lookup_uint64(innvl
, ZCP_ARG_MEMLIMIT
, &memlimit
)) {
3814 memlimit
= ZCP_DEFAULT_MEMLIMIT
;
3816 nvarg
= fnvlist_lookup_nvpair(innvl
, ZCP_ARG_ARGLIST
);
3818 if (instrlimit
== 0 || instrlimit
> zfs_lua_max_instrlimit
)
3820 if (memlimit
== 0 || memlimit
> zfs_lua_max_memlimit
)
3823 return (zcp_eval(poolname
, program
, sync_flag
, instrlimit
, memlimit
,
3831 static const zfs_ioc_key_t zfs_keys_pool_checkpoint
[] = {
3837 zfs_ioc_pool_checkpoint(const char *poolname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3839 return (spa_checkpoint(poolname
));
3846 static const zfs_ioc_key_t zfs_keys_pool_discard_checkpoint
[] = {
3852 zfs_ioc_pool_discard_checkpoint(const char *poolname
, nvlist_t
*innvl
,
3855 return (spa_checkpoint_discard(poolname
));
3860 * zc_name name of dataset to destroy
3861 * zc_defer_destroy mark for deferred destroy
3866 zfs_ioc_destroy(zfs_cmd_t
*zc
)
3869 dmu_objset_type_t ost
;
3872 err
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
3875 ost
= dmu_objset_type(os
);
3876 dmu_objset_rele(os
, FTAG
);
3878 if (ost
== DMU_OST_ZFS
)
3879 zfs_unmount_snap(zc
->zc_name
);
3881 if (strchr(zc
->zc_name
, '@')) {
3882 err
= dsl_destroy_snapshot(zc
->zc_name
, zc
->zc_defer_destroy
);
3884 err
= dsl_destroy_head(zc
->zc_name
);
3885 if (err
== EEXIST
) {
3887 * It is possible that the given DS may have
3888 * hidden child (%recv) datasets - "leftovers"
3889 * resulting from the previously interrupted
3892 * 6 extra bytes for /%recv
3894 char namebuf
[ZFS_MAX_DATASET_NAME_LEN
+ 6];
3896 if (snprintf(namebuf
, sizeof (namebuf
), "%s/%s",
3897 zc
->zc_name
, recv_clone_name
) >=
3899 return (SET_ERROR(EINVAL
));
3902 * Try to remove the hidden child (%recv) and after
3903 * that try to remove the target dataset.
3904 * If the hidden child (%recv) does not exist
3905 * the original error (EEXIST) will be returned
3907 err
= dsl_destroy_head(namebuf
);
3909 err
= dsl_destroy_head(zc
->zc_name
);
3910 else if (err
== ENOENT
)
3911 err
= SET_ERROR(EEXIST
);
3920 * "initialize_command" -> POOL_INITIALIZE_{CANCEL|START|SUSPEND} (uint64)
3921 * "initialize_vdevs": { -> guids to initialize (nvlist)
3922 * "vdev_path_1": vdev_guid_1, (uint64),
3923 * "vdev_path_2": vdev_guid_2, (uint64),
3929 * "initialize_vdevs": { -> initialization errors (nvlist)
3930 * "vdev_path_1": errno, see function body for possible errnos (uint64)
3931 * "vdev_path_2": errno, ... (uint64)
3936 * EINVAL is returned for an unknown commands or if any of the provided vdev
3937 * guids have be specified with a type other than uint64.
3939 static const zfs_ioc_key_t zfs_keys_pool_initialize
[] = {
3940 {ZPOOL_INITIALIZE_COMMAND
, DATA_TYPE_UINT64
, 0},
3941 {ZPOOL_INITIALIZE_VDEVS
, DATA_TYPE_NVLIST
, 0}
3945 zfs_ioc_pool_initialize(const char *poolname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3948 if (nvlist_lookup_uint64(innvl
, ZPOOL_INITIALIZE_COMMAND
,
3950 return (SET_ERROR(EINVAL
));
3953 if (!(cmd_type
== POOL_INITIALIZE_CANCEL
||
3954 cmd_type
== POOL_INITIALIZE_START
||
3955 cmd_type
== POOL_INITIALIZE_SUSPEND
)) {
3956 return (SET_ERROR(EINVAL
));
3959 nvlist_t
*vdev_guids
;
3960 if (nvlist_lookup_nvlist(innvl
, ZPOOL_INITIALIZE_VDEVS
,
3961 &vdev_guids
) != 0) {
3962 return (SET_ERROR(EINVAL
));
3965 for (nvpair_t
*pair
= nvlist_next_nvpair(vdev_guids
, NULL
);
3966 pair
!= NULL
; pair
= nvlist_next_nvpair(vdev_guids
, pair
)) {
3968 if (nvpair_value_uint64(pair
, &vdev_guid
) != 0) {
3969 return (SET_ERROR(EINVAL
));
3974 int error
= spa_open(poolname
, &spa
, FTAG
);
3978 nvlist_t
*vdev_errlist
= fnvlist_alloc();
3979 int total_errors
= spa_vdev_initialize(spa
, vdev_guids
, cmd_type
,
3982 if (fnvlist_size(vdev_errlist
) > 0) {
3983 fnvlist_add_nvlist(outnvl
, ZPOOL_INITIALIZE_VDEVS
,
3986 fnvlist_free(vdev_errlist
);
3988 spa_close(spa
, FTAG
);
3989 return (total_errors
> 0 ? EINVAL
: 0);
3994 * "trim_command" -> POOL_TRIM_{CANCEL|START|SUSPEND} (uint64)
3995 * "trim_vdevs": { -> guids to TRIM (nvlist)
3996 * "vdev_path_1": vdev_guid_1, (uint64),
3997 * "vdev_path_2": vdev_guid_2, (uint64),
4000 * "trim_rate" -> Target TRIM rate in bytes/sec.
4001 * "trim_secure" -> Set to request a secure TRIM.
4005 * "trim_vdevs": { -> TRIM errors (nvlist)
4006 * "vdev_path_1": errno, see function body for possible errnos (uint64)
4007 * "vdev_path_2": errno, ... (uint64)
4012 * EINVAL is returned for an unknown commands or if any of the provided vdev
4013 * guids have be specified with a type other than uint64.
4015 static const zfs_ioc_key_t zfs_keys_pool_trim
[] = {
4016 {ZPOOL_TRIM_COMMAND
, DATA_TYPE_UINT64
, 0},
4017 {ZPOOL_TRIM_VDEVS
, DATA_TYPE_NVLIST
, 0},
4018 {ZPOOL_TRIM_RATE
, DATA_TYPE_UINT64
, ZK_OPTIONAL
},
4019 {ZPOOL_TRIM_SECURE
, DATA_TYPE_BOOLEAN_VALUE
, ZK_OPTIONAL
},
4023 zfs_ioc_pool_trim(const char *poolname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
4026 if (nvlist_lookup_uint64(innvl
, ZPOOL_TRIM_COMMAND
, &cmd_type
) != 0)
4027 return (SET_ERROR(EINVAL
));
4029 if (!(cmd_type
== POOL_TRIM_CANCEL
||
4030 cmd_type
== POOL_TRIM_START
||
4031 cmd_type
== POOL_TRIM_SUSPEND
)) {
4032 return (SET_ERROR(EINVAL
));
4035 nvlist_t
*vdev_guids
;
4036 if (nvlist_lookup_nvlist(innvl
, ZPOOL_TRIM_VDEVS
, &vdev_guids
) != 0)
4037 return (SET_ERROR(EINVAL
));
4039 for (nvpair_t
*pair
= nvlist_next_nvpair(vdev_guids
, NULL
);
4040 pair
!= NULL
; pair
= nvlist_next_nvpair(vdev_guids
, pair
)) {
4042 if (nvpair_value_uint64(pair
, &vdev_guid
) != 0) {
4043 return (SET_ERROR(EINVAL
));
4047 /* Optional, defaults to maximum rate when not provided */
4049 if (nvlist_lookup_uint64(innvl
, ZPOOL_TRIM_RATE
, &rate
) != 0)
4052 /* Optional, defaults to standard TRIM when not provided */
4054 if (nvlist_lookup_boolean_value(innvl
, ZPOOL_TRIM_SECURE
,
4060 int error
= spa_open(poolname
, &spa
, FTAG
);
4064 nvlist_t
*vdev_errlist
= fnvlist_alloc();
4065 int total_errors
= spa_vdev_trim(spa
, vdev_guids
, cmd_type
,
4066 rate
, !!zfs_trim_metaslab_skip
, secure
, vdev_errlist
);
4068 if (fnvlist_size(vdev_errlist
) > 0)
4069 fnvlist_add_nvlist(outnvl
, ZPOOL_TRIM_VDEVS
, vdev_errlist
);
4071 fnvlist_free(vdev_errlist
);
4073 spa_close(spa
, FTAG
);
4074 return (total_errors
> 0 ? EINVAL
: 0);
4078 * fsname is name of dataset to rollback (to most recent snapshot)
4080 * innvl may contain name of expected target snapshot
4082 * outnvl: "target" -> name of most recent snapshot
4085 static const zfs_ioc_key_t zfs_keys_rollback
[] = {
4086 {"target", DATA_TYPE_STRING
, ZK_OPTIONAL
},
4091 zfs_ioc_rollback(const char *fsname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
4095 char *target
= NULL
;
4098 (void) nvlist_lookup_string(innvl
, "target", &target
);
4099 if (target
!= NULL
) {
4100 const char *cp
= strchr(target
, '@');
4103 * The snap name must contain an @, and the part after it must
4104 * contain only valid characters.
4107 zfs_component_namecheck(cp
+ 1, NULL
, NULL
) != 0)
4108 return (SET_ERROR(EINVAL
));
4111 if (getzfsvfs(fsname
, &zfsvfs
) == 0) {
4114 ds
= dmu_objset_ds(zfsvfs
->z_os
);
4115 error
= zfs_suspend_fs(zfsvfs
);
4119 error
= dsl_dataset_rollback(fsname
, target
, zfsvfs
,
4121 resume_err
= zfs_resume_fs(zfsvfs
, ds
);
4122 error
= error
? error
: resume_err
;
4124 deactivate_super(zfsvfs
->z_sb
);
4125 } else if ((zv
= zvol_suspend(fsname
)) != NULL
) {
4126 error
= dsl_dataset_rollback(fsname
, target
, zvol_tag(zv
),
4130 error
= dsl_dataset_rollback(fsname
, target
, NULL
, outnvl
);
4136 recursive_unmount(const char *fsname
, void *arg
)
4138 const char *snapname
= arg
;
4141 fullname
= kmem_asprintf("%s@%s", fsname
, snapname
);
4142 zfs_unmount_snap(fullname
);
4150 * snapname is the snapshot to redact.
4152 * "bookname" -> (string)
4153 * name of the redaction bookmark to generate
4154 * "snapnv" -> (nvlist, values ignored)
4155 * snapshots to redact snapname with respect to
4162 static const zfs_ioc_key_t zfs_keys_redact
[] = {
4163 {"bookname", DATA_TYPE_STRING
, 0},
4164 {"snapnv", DATA_TYPE_NVLIST
, 0},
4167 zfs_ioc_redact(const char *snapname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
4169 nvlist_t
*redactnvl
= NULL
;
4170 char *redactbook
= NULL
;
4172 if (nvlist_lookup_nvlist(innvl
, "snapnv", &redactnvl
) != 0)
4173 return (SET_ERROR(EINVAL
));
4174 if (fnvlist_num_pairs(redactnvl
) == 0)
4175 return (SET_ERROR(ENXIO
));
4176 if (nvlist_lookup_string(innvl
, "bookname", &redactbook
) != 0)
4177 return (SET_ERROR(EINVAL
));
4179 return (dmu_redact_snap(snapname
, redactnvl
, redactbook
));
4184 * zc_name old name of dataset
4185 * zc_value new name of dataset
4186 * zc_cookie recursive flag (only valid for snapshots)
4191 zfs_ioc_rename(zfs_cmd_t
*zc
)
4194 dmu_objset_type_t ost
;
4195 boolean_t recursive
= zc
->zc_cookie
& 1;
4199 /* "zfs rename" from and to ...%recv datasets should both fail */
4200 zc
->zc_name
[sizeof (zc
->zc_name
) - 1] = '\0';
4201 zc
->zc_value
[sizeof (zc
->zc_value
) - 1] = '\0';
4202 if (dataset_namecheck(zc
->zc_name
, NULL
, NULL
) != 0 ||
4203 dataset_namecheck(zc
->zc_value
, NULL
, NULL
) != 0 ||
4204 strchr(zc
->zc_name
, '%') || strchr(zc
->zc_value
, '%'))
4205 return (SET_ERROR(EINVAL
));
4207 err
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
4210 ost
= dmu_objset_type(os
);
4211 dmu_objset_rele(os
, FTAG
);
4213 at
= strchr(zc
->zc_name
, '@');
4215 /* snaps must be in same fs */
4218 if (strncmp(zc
->zc_name
, zc
->zc_value
, at
- zc
->zc_name
+ 1))
4219 return (SET_ERROR(EXDEV
));
4221 if (ost
== DMU_OST_ZFS
) {
4222 error
= dmu_objset_find(zc
->zc_name
,
4223 recursive_unmount
, at
+ 1,
4224 recursive
? DS_FIND_CHILDREN
: 0);
4230 error
= dsl_dataset_rename_snapshot(zc
->zc_name
,
4231 at
+ 1, strchr(zc
->zc_value
, '@') + 1, recursive
);
4236 return (dsl_dir_rename(zc
->zc_name
, zc
->zc_value
));
4241 zfs_check_settable(const char *dsname
, nvpair_t
*pair
, cred_t
*cr
)
4243 const char *propname
= nvpair_name(pair
);
4244 boolean_t issnap
= (strchr(dsname
, '@') != NULL
);
4245 zfs_prop_t prop
= zfs_name_to_prop(propname
);
4249 if (prop
== ZPROP_INVAL
) {
4250 if (zfs_prop_user(propname
)) {
4251 if ((err
= zfs_secpolicy_write_perms(dsname
,
4252 ZFS_DELEG_PERM_USERPROP
, cr
)))
4257 if (!issnap
&& zfs_prop_userquota(propname
)) {
4258 const char *perm
= NULL
;
4259 const char *uq_prefix
=
4260 zfs_userquota_prop_prefixes
[ZFS_PROP_USERQUOTA
];
4261 const char *gq_prefix
=
4262 zfs_userquota_prop_prefixes
[ZFS_PROP_GROUPQUOTA
];
4263 const char *uiq_prefix
=
4264 zfs_userquota_prop_prefixes
[ZFS_PROP_USEROBJQUOTA
];
4265 const char *giq_prefix
=
4266 zfs_userquota_prop_prefixes
[ZFS_PROP_GROUPOBJQUOTA
];
4267 const char *pq_prefix
=
4268 zfs_userquota_prop_prefixes
[ZFS_PROP_PROJECTQUOTA
];
4269 const char *piq_prefix
= zfs_userquota_prop_prefixes
[\
4270 ZFS_PROP_PROJECTOBJQUOTA
];
4272 if (strncmp(propname
, uq_prefix
,
4273 strlen(uq_prefix
)) == 0) {
4274 perm
= ZFS_DELEG_PERM_USERQUOTA
;
4275 } else if (strncmp(propname
, uiq_prefix
,
4276 strlen(uiq_prefix
)) == 0) {
4277 perm
= ZFS_DELEG_PERM_USEROBJQUOTA
;
4278 } else if (strncmp(propname
, gq_prefix
,
4279 strlen(gq_prefix
)) == 0) {
4280 perm
= ZFS_DELEG_PERM_GROUPQUOTA
;
4281 } else if (strncmp(propname
, giq_prefix
,
4282 strlen(giq_prefix
)) == 0) {
4283 perm
= ZFS_DELEG_PERM_GROUPOBJQUOTA
;
4284 } else if (strncmp(propname
, pq_prefix
,
4285 strlen(pq_prefix
)) == 0) {
4286 perm
= ZFS_DELEG_PERM_PROJECTQUOTA
;
4287 } else if (strncmp(propname
, piq_prefix
,
4288 strlen(piq_prefix
)) == 0) {
4289 perm
= ZFS_DELEG_PERM_PROJECTOBJQUOTA
;
4291 /* {USER|GROUP|PROJECT}USED are read-only */
4292 return (SET_ERROR(EINVAL
));
4295 if ((err
= zfs_secpolicy_write_perms(dsname
, perm
, cr
)))
4300 return (SET_ERROR(EINVAL
));
4304 return (SET_ERROR(EINVAL
));
4306 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
4308 * dsl_prop_get_all_impl() returns properties in this
4312 VERIFY(nvpair_value_nvlist(pair
, &attrs
) == 0);
4313 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
4318 * Check that this value is valid for this pool version
4321 case ZFS_PROP_COMPRESSION
:
4323 * If the user specified gzip compression, make sure
4324 * the SPA supports it. We ignore any errors here since
4325 * we'll catch them later.
4327 if (nvpair_value_uint64(pair
, &intval
) == 0) {
4328 if (intval
>= ZIO_COMPRESS_GZIP_1
&&
4329 intval
<= ZIO_COMPRESS_GZIP_9
&&
4330 zfs_earlier_version(dsname
,
4331 SPA_VERSION_GZIP_COMPRESSION
)) {
4332 return (SET_ERROR(ENOTSUP
));
4335 if (intval
== ZIO_COMPRESS_ZLE
&&
4336 zfs_earlier_version(dsname
,
4337 SPA_VERSION_ZLE_COMPRESSION
))
4338 return (SET_ERROR(ENOTSUP
));
4340 if (intval
== ZIO_COMPRESS_LZ4
) {
4343 if ((err
= spa_open(dsname
, &spa
, FTAG
)) != 0)
4346 if (!spa_feature_is_enabled(spa
,
4347 SPA_FEATURE_LZ4_COMPRESS
)) {
4348 spa_close(spa
, FTAG
);
4349 return (SET_ERROR(ENOTSUP
));
4351 spa_close(spa
, FTAG
);
4355 * If this is a bootable dataset then
4356 * verify that the compression algorithm
4357 * is supported for booting. We must return
4358 * something other than ENOTSUP since it
4359 * implies a downrev pool version.
4361 if (zfs_is_bootfs(dsname
) &&
4362 !BOOTFS_COMPRESS_VALID(intval
)) {
4363 return (SET_ERROR(ERANGE
));
4368 case ZFS_PROP_COPIES
:
4369 if (zfs_earlier_version(dsname
, SPA_VERSION_DITTO_BLOCKS
))
4370 return (SET_ERROR(ENOTSUP
));
4373 case ZFS_PROP_VOLBLOCKSIZE
:
4374 case ZFS_PROP_RECORDSIZE
:
4375 /* Record sizes above 128k need the feature to be enabled */
4376 if (nvpair_value_uint64(pair
, &intval
) == 0 &&
4377 intval
> SPA_OLD_MAXBLOCKSIZE
) {
4381 * We don't allow setting the property above 1MB,
4382 * unless the tunable has been changed.
4384 if (intval
> zfs_max_recordsize
||
4385 intval
> SPA_MAXBLOCKSIZE
)
4386 return (SET_ERROR(ERANGE
));
4388 if ((err
= spa_open(dsname
, &spa
, FTAG
)) != 0)
4391 if (!spa_feature_is_enabled(spa
,
4392 SPA_FEATURE_LARGE_BLOCKS
)) {
4393 spa_close(spa
, FTAG
);
4394 return (SET_ERROR(ENOTSUP
));
4396 spa_close(spa
, FTAG
);
4400 case ZFS_PROP_DNODESIZE
:
4401 /* Dnode sizes above 512 need the feature to be enabled */
4402 if (nvpair_value_uint64(pair
, &intval
) == 0 &&
4403 intval
!= ZFS_DNSIZE_LEGACY
) {
4407 * If this is a bootable dataset then
4408 * we don't allow large (>512B) dnodes,
4409 * because GRUB doesn't support them.
4411 if (zfs_is_bootfs(dsname
) &&
4412 intval
!= ZFS_DNSIZE_LEGACY
) {
4413 return (SET_ERROR(EDOM
));
4416 if ((err
= spa_open(dsname
, &spa
, FTAG
)) != 0)
4419 if (!spa_feature_is_enabled(spa
,
4420 SPA_FEATURE_LARGE_DNODE
)) {
4421 spa_close(spa
, FTAG
);
4422 return (SET_ERROR(ENOTSUP
));
4424 spa_close(spa
, FTAG
);
4428 case ZFS_PROP_SPECIAL_SMALL_BLOCKS
:
4430 * This property could require the allocation classes
4431 * feature to be active for setting, however we allow
4432 * it so that tests of settable properties succeed.
4433 * The CLI will issue a warning in this case.
4437 case ZFS_PROP_SHARESMB
:
4438 if (zpl_earlier_version(dsname
, ZPL_VERSION_FUID
))
4439 return (SET_ERROR(ENOTSUP
));
4442 case ZFS_PROP_ACLINHERIT
:
4443 if (nvpair_type(pair
) == DATA_TYPE_UINT64
&&
4444 nvpair_value_uint64(pair
, &intval
) == 0) {
4445 if (intval
== ZFS_ACL_PASSTHROUGH_X
&&
4446 zfs_earlier_version(dsname
,
4447 SPA_VERSION_PASSTHROUGH_X
))
4448 return (SET_ERROR(ENOTSUP
));
4451 case ZFS_PROP_CHECKSUM
:
4452 case ZFS_PROP_DEDUP
:
4454 spa_feature_t feature
;
4458 /* dedup feature version checks */
4459 if (prop
== ZFS_PROP_DEDUP
&&
4460 zfs_earlier_version(dsname
, SPA_VERSION_DEDUP
))
4461 return (SET_ERROR(ENOTSUP
));
4463 if (nvpair_type(pair
) == DATA_TYPE_UINT64
&&
4464 nvpair_value_uint64(pair
, &intval
) == 0) {
4465 /* check prop value is enabled in features */
4466 feature
= zio_checksum_to_feature(
4467 intval
& ZIO_CHECKSUM_MASK
);
4468 if (feature
== SPA_FEATURE_NONE
)
4471 if ((err
= spa_open(dsname
, &spa
, FTAG
)) != 0)
4474 if (!spa_feature_is_enabled(spa
, feature
)) {
4475 spa_close(spa
, FTAG
);
4476 return (SET_ERROR(ENOTSUP
));
4478 spa_close(spa
, FTAG
);
4487 return (zfs_secpolicy_setprop(dsname
, prop
, pair
, CRED()));
4491 * Removes properties from the given props list that fail permission checks
4492 * needed to clear them and to restore them in case of a receive error. For each
4493 * property, make sure we have both set and inherit permissions.
4495 * Returns the first error encountered if any permission checks fail. If the
4496 * caller provides a non-NULL errlist, it also gives the complete list of names
4497 * of all the properties that failed a permission check along with the
4498 * corresponding error numbers. The caller is responsible for freeing the
4501 * If every property checks out successfully, zero is returned and the list
4502 * pointed at by errlist is NULL.
4505 zfs_check_clearable(char *dataset
, nvlist_t
*props
, nvlist_t
**errlist
)
4508 nvpair_t
*pair
, *next_pair
;
4515 VERIFY(nvlist_alloc(&errors
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4517 zc
= kmem_alloc(sizeof (zfs_cmd_t
), KM_SLEEP
);
4518 (void) strlcpy(zc
->zc_name
, dataset
, sizeof (zc
->zc_name
));
4519 pair
= nvlist_next_nvpair(props
, NULL
);
4520 while (pair
!= NULL
) {
4521 next_pair
= nvlist_next_nvpair(props
, pair
);
4523 (void) strlcpy(zc
->zc_value
, nvpair_name(pair
),
4524 sizeof (zc
->zc_value
));
4525 if ((err
= zfs_check_settable(dataset
, pair
, CRED())) != 0 ||
4526 (err
= zfs_secpolicy_inherit_prop(zc
, NULL
, CRED())) != 0) {
4527 VERIFY(nvlist_remove_nvpair(props
, pair
) == 0);
4528 VERIFY(nvlist_add_int32(errors
,
4529 zc
->zc_value
, err
) == 0);
4533 kmem_free(zc
, sizeof (zfs_cmd_t
));
4535 if ((pair
= nvlist_next_nvpair(errors
, NULL
)) == NULL
) {
4536 nvlist_free(errors
);
4539 VERIFY(nvpair_value_int32(pair
, &rv
) == 0);
4542 if (errlist
== NULL
)
4543 nvlist_free(errors
);
4551 propval_equals(nvpair_t
*p1
, nvpair_t
*p2
)
4553 if (nvpair_type(p1
) == DATA_TYPE_NVLIST
) {
4554 /* dsl_prop_get_all_impl() format */
4556 VERIFY(nvpair_value_nvlist(p1
, &attrs
) == 0);
4557 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
4561 if (nvpair_type(p2
) == DATA_TYPE_NVLIST
) {
4563 VERIFY(nvpair_value_nvlist(p2
, &attrs
) == 0);
4564 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
4568 if (nvpair_type(p1
) != nvpair_type(p2
))
4571 if (nvpair_type(p1
) == DATA_TYPE_STRING
) {
4572 char *valstr1
, *valstr2
;
4574 VERIFY(nvpair_value_string(p1
, (char **)&valstr1
) == 0);
4575 VERIFY(nvpair_value_string(p2
, (char **)&valstr2
) == 0);
4576 return (strcmp(valstr1
, valstr2
) == 0);
4578 uint64_t intval1
, intval2
;
4580 VERIFY(nvpair_value_uint64(p1
, &intval1
) == 0);
4581 VERIFY(nvpair_value_uint64(p2
, &intval2
) == 0);
4582 return (intval1
== intval2
);
4587 * Remove properties from props if they are not going to change (as determined
4588 * by comparison with origprops). Remove them from origprops as well, since we
4589 * do not need to clear or restore properties that won't change.
4592 props_reduce(nvlist_t
*props
, nvlist_t
*origprops
)
4594 nvpair_t
*pair
, *next_pair
;
4596 if (origprops
== NULL
)
4597 return; /* all props need to be received */
4599 pair
= nvlist_next_nvpair(props
, NULL
);
4600 while (pair
!= NULL
) {
4601 const char *propname
= nvpair_name(pair
);
4604 next_pair
= nvlist_next_nvpair(props
, pair
);
4606 if ((nvlist_lookup_nvpair(origprops
, propname
,
4607 &match
) != 0) || !propval_equals(pair
, match
))
4608 goto next
; /* need to set received value */
4610 /* don't clear the existing received value */
4611 (void) nvlist_remove_nvpair(origprops
, match
);
4612 /* don't bother receiving the property */
4613 (void) nvlist_remove_nvpair(props
, pair
);
4620 * Extract properties that cannot be set PRIOR to the receipt of a dataset.
4621 * For example, refquota cannot be set until after the receipt of a dataset,
4622 * because in replication streams, an older/earlier snapshot may exceed the
4623 * refquota. We want to receive the older/earlier snapshot, but setting
4624 * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent
4625 * the older/earlier snapshot from being received (with EDQUOT).
4627 * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario.
4629 * libzfs will need to be judicious handling errors encountered by props
4630 * extracted by this function.
4633 extract_delay_props(nvlist_t
*props
)
4635 nvlist_t
*delayprops
;
4636 nvpair_t
*nvp
, *tmp
;
4637 static const zfs_prop_t delayable
[] = {
4639 ZFS_PROP_KEYLOCATION
,
4644 VERIFY(nvlist_alloc(&delayprops
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4646 for (nvp
= nvlist_next_nvpair(props
, NULL
); nvp
!= NULL
;
4647 nvp
= nvlist_next_nvpair(props
, nvp
)) {
4649 * strcmp() is safe because zfs_prop_to_name() always returns
4652 for (i
= 0; delayable
[i
] != 0; i
++) {
4653 if (strcmp(zfs_prop_to_name(delayable
[i
]),
4654 nvpair_name(nvp
)) == 0) {
4658 if (delayable
[i
] != 0) {
4659 tmp
= nvlist_prev_nvpair(props
, nvp
);
4660 VERIFY(nvlist_add_nvpair(delayprops
, nvp
) == 0);
4661 VERIFY(nvlist_remove_nvpair(props
, nvp
) == 0);
4666 if (nvlist_empty(delayprops
)) {
4667 nvlist_free(delayprops
);
4670 return (delayprops
);
4674 static boolean_t zfs_ioc_recv_inject_err
;
4678 * nvlist 'errors' is always allocated. It will contain descriptions of
4679 * encountered errors, if any. It's the callers responsibility to free.
4682 zfs_ioc_recv_impl(char *tofs
, char *tosnap
, char *origin
, nvlist_t
*recvprops
,
4683 nvlist_t
*localprops
, nvlist_t
*hidden_args
, boolean_t force
,
4684 boolean_t resumable
, int input_fd
, dmu_replay_record_t
*begin_record
,
4685 int cleanup_fd
, uint64_t *read_bytes
, uint64_t *errflags
,
4686 uint64_t *action_handle
, nvlist_t
**errors
)
4688 dmu_recv_cookie_t drc
;
4690 int props_error
= 0;
4692 nvlist_t
*local_delayprops
= NULL
;
4693 nvlist_t
*recv_delayprops
= NULL
;
4694 nvlist_t
*origprops
= NULL
; /* existing properties */
4695 nvlist_t
*origrecvd
= NULL
; /* existing received properties */
4696 boolean_t first_recvd_props
= B_FALSE
;
4697 boolean_t tofs_was_redacted
;
4702 *errors
= fnvlist_alloc();
4704 input_fp
= getf(input_fd
);
4705 if (input_fp
== NULL
)
4706 return (SET_ERROR(EBADF
));
4708 off
= input_fp
->f_offset
;
4709 error
= dmu_recv_begin(tofs
, tosnap
, begin_record
, force
,
4710 resumable
, localprops
, hidden_args
, origin
, &drc
, input_fp
->f_vnode
,
4714 tofs_was_redacted
= dsl_get_redacted(drc
.drc_ds
);
4717 * Set properties before we receive the stream so that they are applied
4718 * to the new data. Note that we must call dmu_recv_stream() if
4719 * dmu_recv_begin() succeeds.
4721 if (recvprops
!= NULL
&& !drc
.drc_newfs
) {
4722 if (spa_version(dsl_dataset_get_spa(drc
.drc_ds
)) >=
4723 SPA_VERSION_RECVD_PROPS
&&
4724 !dsl_prop_get_hasrecvd(tofs
))
4725 first_recvd_props
= B_TRUE
;
4728 * If new received properties are supplied, they are to
4729 * completely replace the existing received properties,
4730 * so stash away the existing ones.
4732 if (dsl_prop_get_received(tofs
, &origrecvd
) == 0) {
4733 nvlist_t
*errlist
= NULL
;
4735 * Don't bother writing a property if its value won't
4736 * change (and avoid the unnecessary security checks).
4738 * The first receive after SPA_VERSION_RECVD_PROPS is a
4739 * special case where we blow away all local properties
4742 if (!first_recvd_props
)
4743 props_reduce(recvprops
, origrecvd
);
4744 if (zfs_check_clearable(tofs
, origrecvd
, &errlist
) != 0)
4745 (void) nvlist_merge(*errors
, errlist
, 0);
4746 nvlist_free(errlist
);
4748 if (clear_received_props(tofs
, origrecvd
,
4749 first_recvd_props
? NULL
: recvprops
) != 0)
4750 *errflags
|= ZPROP_ERR_NOCLEAR
;
4752 *errflags
|= ZPROP_ERR_NOCLEAR
;
4757 * Stash away existing properties so we can restore them on error unless
4758 * we're doing the first receive after SPA_VERSION_RECVD_PROPS, in which
4759 * case "origrecvd" will take care of that.
4761 if (localprops
!= NULL
&& !drc
.drc_newfs
&& !first_recvd_props
) {
4763 if (dmu_objset_hold(tofs
, FTAG
, &os
) == 0) {
4764 if (dsl_prop_get_all(os
, &origprops
) != 0) {
4765 *errflags
|= ZPROP_ERR_NOCLEAR
;
4767 dmu_objset_rele(os
, FTAG
);
4769 *errflags
|= ZPROP_ERR_NOCLEAR
;
4773 if (recvprops
!= NULL
) {
4774 props_error
= dsl_prop_set_hasrecvd(tofs
);
4776 if (props_error
== 0) {
4777 recv_delayprops
= extract_delay_props(recvprops
);
4778 (void) zfs_set_prop_nvlist(tofs
, ZPROP_SRC_RECEIVED
,
4779 recvprops
, *errors
);
4783 if (localprops
!= NULL
) {
4784 nvlist_t
*oprops
= fnvlist_alloc();
4785 nvlist_t
*xprops
= fnvlist_alloc();
4786 nvpair_t
*nvp
= NULL
;
4788 while ((nvp
= nvlist_next_nvpair(localprops
, nvp
)) != NULL
) {
4789 if (nvpair_type(nvp
) == DATA_TYPE_BOOLEAN
) {
4791 const char *name
= nvpair_name(nvp
);
4792 zfs_prop_t prop
= zfs_name_to_prop(name
);
4793 if (prop
!= ZPROP_INVAL
) {
4794 if (!zfs_prop_inheritable(prop
))
4796 } else if (!zfs_prop_user(name
))
4798 fnvlist_add_boolean(xprops
, name
);
4800 /* -o property=value */
4801 fnvlist_add_nvpair(oprops
, nvp
);
4805 local_delayprops
= extract_delay_props(oprops
);
4806 (void) zfs_set_prop_nvlist(tofs
, ZPROP_SRC_LOCAL
,
4808 (void) zfs_set_prop_nvlist(tofs
, ZPROP_SRC_INHERITED
,
4811 nvlist_free(oprops
);
4812 nvlist_free(xprops
);
4815 error
= dmu_recv_stream(&drc
, cleanup_fd
, action_handle
, &off
);
4818 zfsvfs_t
*zfsvfs
= NULL
;
4819 zvol_state_t
*zv
= NULL
;
4821 if (getzfsvfs(tofs
, &zfsvfs
) == 0) {
4825 boolean_t stream_is_redacted
= DMU_GET_FEATUREFLAGS(
4826 begin_record
->drr_u
.drr_begin
.
4827 drr_versioninfo
) & DMU_BACKUP_FEATURE_REDACTED
;
4829 ds
= dmu_objset_ds(zfsvfs
->z_os
);
4830 error
= zfs_suspend_fs(zfsvfs
);
4832 * If the suspend fails, then the recv_end will
4833 * likely also fail, and clean up after itself.
4835 end_err
= dmu_recv_end(&drc
, zfsvfs
);
4837 * If the dataset was not redacted, but we received a
4838 * redacted stream onto it, we need to unmount the
4839 * dataset. Otherwise, resume the filesystem.
4841 if (error
== 0 && !drc
.drc_newfs
&&
4842 stream_is_redacted
&& !tofs_was_redacted
) {
4843 error
= zfs_end_fs(zfsvfs
, ds
);
4844 } else if (error
== 0) {
4845 error
= zfs_resume_fs(zfsvfs
, ds
);
4847 error
= error
? error
: end_err
;
4848 deactivate_super(zfsvfs
->z_sb
);
4849 } else if ((zv
= zvol_suspend(tofs
)) != NULL
) {
4850 error
= dmu_recv_end(&drc
, zvol_tag(zv
));
4853 error
= dmu_recv_end(&drc
, NULL
);
4856 /* Set delayed properties now, after we're done receiving. */
4857 if (recv_delayprops
!= NULL
&& error
== 0) {
4858 (void) zfs_set_prop_nvlist(tofs
, ZPROP_SRC_RECEIVED
,
4859 recv_delayprops
, *errors
);
4861 if (local_delayprops
!= NULL
&& error
== 0) {
4862 (void) zfs_set_prop_nvlist(tofs
, ZPROP_SRC_LOCAL
,
4863 local_delayprops
, *errors
);
4868 * Merge delayed props back in with initial props, in case
4869 * we're DEBUG and zfs_ioc_recv_inject_err is set (which means
4870 * we have to make sure clear_received_props() includes
4871 * the delayed properties).
4873 * Since zfs_ioc_recv_inject_err is only in DEBUG kernels,
4874 * using ASSERT() will be just like a VERIFY.
4876 if (recv_delayprops
!= NULL
) {
4877 ASSERT(nvlist_merge(recvprops
, recv_delayprops
, 0) == 0);
4878 nvlist_free(recv_delayprops
);
4880 if (local_delayprops
!= NULL
) {
4881 ASSERT(nvlist_merge(localprops
, local_delayprops
, 0) == 0);
4882 nvlist_free(local_delayprops
);
4885 *read_bytes
= off
- input_fp
->f_offset
;
4886 if (VOP_SEEK(input_fp
->f_vnode
, input_fp
->f_offset
, &off
, NULL
) == 0)
4887 input_fp
->f_offset
= off
;
4890 if (zfs_ioc_recv_inject_err
) {
4891 zfs_ioc_recv_inject_err
= B_FALSE
;
4897 * On error, restore the original props.
4899 if (error
!= 0 && recvprops
!= NULL
&& !drc
.drc_newfs
) {
4900 if (clear_received_props(tofs
, recvprops
, NULL
) != 0) {
4902 * We failed to clear the received properties.
4903 * Since we may have left a $recvd value on the
4904 * system, we can't clear the $hasrecvd flag.
4906 *errflags
|= ZPROP_ERR_NORESTORE
;
4907 } else if (first_recvd_props
) {
4908 dsl_prop_unset_hasrecvd(tofs
);
4911 if (origrecvd
== NULL
&& !drc
.drc_newfs
) {
4912 /* We failed to stash the original properties. */
4913 *errflags
|= ZPROP_ERR_NORESTORE
;
4917 * dsl_props_set() will not convert RECEIVED to LOCAL on or
4918 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
4919 * explicitly if we're restoring local properties cleared in the
4920 * first new-style receive.
4922 if (origrecvd
!= NULL
&&
4923 zfs_set_prop_nvlist(tofs
, (first_recvd_props
?
4924 ZPROP_SRC_LOCAL
: ZPROP_SRC_RECEIVED
),
4925 origrecvd
, NULL
) != 0) {
4927 * We stashed the original properties but failed to
4930 *errflags
|= ZPROP_ERR_NORESTORE
;
4933 if (error
!= 0 && localprops
!= NULL
&& !drc
.drc_newfs
&&
4934 !first_recvd_props
) {
4936 nvlist_t
*inheritprops
;
4939 if (origprops
== NULL
) {
4940 /* We failed to stash the original properties. */
4941 *errflags
|= ZPROP_ERR_NORESTORE
;
4945 /* Restore original props */
4946 setprops
= fnvlist_alloc();
4947 inheritprops
= fnvlist_alloc();
4949 while ((nvp
= nvlist_next_nvpair(localprops
, nvp
)) != NULL
) {
4950 const char *name
= nvpair_name(nvp
);
4954 if (!nvlist_exists(origprops
, name
)) {
4956 * Property was not present or was explicitly
4957 * inherited before the receive, restore this.
4959 fnvlist_add_boolean(inheritprops
, name
);
4962 attrs
= fnvlist_lookup_nvlist(origprops
, name
);
4963 source
= fnvlist_lookup_string(attrs
, ZPROP_SOURCE
);
4965 /* Skip received properties */
4966 if (strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) == 0)
4969 if (strcmp(source
, tofs
) == 0) {
4970 /* Property was locally set */
4971 fnvlist_add_nvlist(setprops
, name
, attrs
);
4973 /* Property was implicitly inherited */
4974 fnvlist_add_boolean(inheritprops
, name
);
4978 if (zfs_set_prop_nvlist(tofs
, ZPROP_SRC_LOCAL
, setprops
,
4980 *errflags
|= ZPROP_ERR_NORESTORE
;
4981 if (zfs_set_prop_nvlist(tofs
, ZPROP_SRC_INHERITED
, inheritprops
,
4983 *errflags
|= ZPROP_ERR_NORESTORE
;
4985 nvlist_free(setprops
);
4986 nvlist_free(inheritprops
);
4990 nvlist_free(origrecvd
);
4991 nvlist_free(origprops
);
4994 error
= props_error
;
5001 * zc_name name of containing filesystem (unused)
5002 * zc_nvlist_src{_size} nvlist of properties to apply
5003 * zc_nvlist_conf{_size} nvlist of properties to exclude
5004 * (DATA_TYPE_BOOLEAN) and override (everything else)
5005 * zc_value name of snapshot to create
5006 * zc_string name of clone origin (if DRR_FLAG_CLONE)
5007 * zc_cookie file descriptor to recv from
5008 * zc_begin_record the BEGIN record of the stream (not byteswapped)
5009 * zc_guid force flag
5010 * zc_cleanup_fd cleanup-on-exit file descriptor
5011 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
5014 * zc_cookie number of bytes read
5015 * zc_obj zprop_errflags_t
5016 * zc_action_handle handle for this guid/ds mapping
5017 * zc_nvlist_dst{_size} error for each unapplied received property
5020 zfs_ioc_recv(zfs_cmd_t
*zc
)
5022 dmu_replay_record_t begin_record
;
5023 nvlist_t
*errors
= NULL
;
5024 nvlist_t
*recvdprops
= NULL
;
5025 nvlist_t
*localprops
= NULL
;
5026 char *origin
= NULL
;
5028 char tofs
[ZFS_MAX_DATASET_NAME_LEN
];
5031 if (dataset_namecheck(zc
->zc_value
, NULL
, NULL
) != 0 ||
5032 strchr(zc
->zc_value
, '@') == NULL
||
5033 strchr(zc
->zc_value
, '%'))
5034 return (SET_ERROR(EINVAL
));
5036 (void) strlcpy(tofs
, zc
->zc_value
, sizeof (tofs
));
5037 tosnap
= strchr(tofs
, '@');
5040 if (zc
->zc_nvlist_src
!= 0 &&
5041 (error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
5042 zc
->zc_iflags
, &recvdprops
)) != 0)
5045 if (zc
->zc_nvlist_conf
!= 0 &&
5046 (error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
5047 zc
->zc_iflags
, &localprops
)) != 0)
5050 if (zc
->zc_string
[0])
5051 origin
= zc
->zc_string
;
5053 begin_record
.drr_type
= DRR_BEGIN
;
5054 begin_record
.drr_payloadlen
= 0;
5055 begin_record
.drr_u
.drr_begin
= zc
->zc_begin_record
;
5057 error
= zfs_ioc_recv_impl(tofs
, tosnap
, origin
, recvdprops
, localprops
,
5058 NULL
, zc
->zc_guid
, B_FALSE
, zc
->zc_cookie
, &begin_record
,
5059 zc
->zc_cleanup_fd
, &zc
->zc_cookie
, &zc
->zc_obj
,
5060 &zc
->zc_action_handle
, &errors
);
5061 nvlist_free(recvdprops
);
5062 nvlist_free(localprops
);
5065 * Now that all props, initial and delayed, are set, report the prop
5066 * errors to the caller.
5068 if (zc
->zc_nvlist_dst_size
!= 0 && errors
!= NULL
&&
5069 (nvlist_smush(errors
, zc
->zc_nvlist_dst_size
) != 0 ||
5070 put_nvlist(zc
, errors
) != 0)) {
5072 * Caller made zc->zc_nvlist_dst less than the minimum expected
5073 * size or supplied an invalid address.
5075 error
= SET_ERROR(EINVAL
);
5078 nvlist_free(errors
);
5085 * "snapname" -> full name of the snapshot to create
5086 * (optional) "props" -> received properties to set (nvlist)
5087 * (optional) "localprops" -> override and exclude properties (nvlist)
5088 * (optional) "origin" -> name of clone origin (DRR_FLAG_CLONE)
5089 * "begin_record" -> non-byteswapped dmu_replay_record_t
5090 * "input_fd" -> file descriptor to read stream from (int32)
5091 * (optional) "force" -> force flag (value ignored)
5092 * (optional) "resumable" -> resumable flag (value ignored)
5093 * (optional) "cleanup_fd" -> cleanup-on-exit file descriptor
5094 * (optional) "action_handle" -> handle for this guid/ds mapping
5095 * (optional) "hidden_args" -> { "wkeydata" -> value }
5099 * "read_bytes" -> number of bytes read
5100 * "error_flags" -> zprop_errflags_t
5101 * "action_handle" -> handle for this guid/ds mapping
5102 * "errors" -> error for each unapplied received property (nvlist)
5105 static const zfs_ioc_key_t zfs_keys_recv_new
[] = {
5106 {"snapname", DATA_TYPE_STRING
, 0},
5107 {"props", DATA_TYPE_NVLIST
, ZK_OPTIONAL
},
5108 {"localprops", DATA_TYPE_NVLIST
, ZK_OPTIONAL
},
5109 {"origin", DATA_TYPE_STRING
, ZK_OPTIONAL
},
5110 {"begin_record", DATA_TYPE_BYTE_ARRAY
, 0},
5111 {"input_fd", DATA_TYPE_INT32
, 0},
5112 {"force", DATA_TYPE_BOOLEAN
, ZK_OPTIONAL
},
5113 {"resumable", DATA_TYPE_BOOLEAN
, ZK_OPTIONAL
},
5114 {"cleanup_fd", DATA_TYPE_INT32
, ZK_OPTIONAL
},
5115 {"action_handle", DATA_TYPE_UINT64
, ZK_OPTIONAL
},
5116 {"hidden_args", DATA_TYPE_NVLIST
, ZK_OPTIONAL
},
5120 zfs_ioc_recv_new(const char *fsname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
5122 dmu_replay_record_t
*begin_record
;
5123 uint_t begin_record_size
;
5124 nvlist_t
*errors
= NULL
;
5125 nvlist_t
*recvprops
= NULL
;
5126 nvlist_t
*localprops
= NULL
;
5127 nvlist_t
*hidden_args
= NULL
;
5129 char *origin
= NULL
;
5131 char tofs
[ZFS_MAX_DATASET_NAME_LEN
];
5133 boolean_t resumable
;
5134 uint64_t action_handle
= 0;
5135 uint64_t read_bytes
= 0;
5136 uint64_t errflags
= 0;
5138 int cleanup_fd
= -1;
5141 snapname
= fnvlist_lookup_string(innvl
, "snapname");
5143 if (dataset_namecheck(snapname
, NULL
, NULL
) != 0 ||
5144 strchr(snapname
, '@') == NULL
||
5145 strchr(snapname
, '%'))
5146 return (SET_ERROR(EINVAL
));
5148 (void) strcpy(tofs
, snapname
);
5149 tosnap
= strchr(tofs
, '@');
5152 error
= nvlist_lookup_string(innvl
, "origin", &origin
);
5153 if (error
&& error
!= ENOENT
)
5156 error
= nvlist_lookup_byte_array(innvl
, "begin_record",
5157 (uchar_t
**)&begin_record
, &begin_record_size
);
5158 if (error
!= 0 || begin_record_size
!= sizeof (*begin_record
))
5159 return (SET_ERROR(EINVAL
));
5161 input_fd
= fnvlist_lookup_int32(innvl
, "input_fd");
5163 force
= nvlist_exists(innvl
, "force");
5164 resumable
= nvlist_exists(innvl
, "resumable");
5166 error
= nvlist_lookup_int32(innvl
, "cleanup_fd", &cleanup_fd
);
5167 if (error
&& error
!= ENOENT
)
5170 error
= nvlist_lookup_uint64(innvl
, "action_handle", &action_handle
);
5171 if (error
&& error
!= ENOENT
)
5174 /* we still use "props" here for backwards compatibility */
5175 error
= nvlist_lookup_nvlist(innvl
, "props", &recvprops
);
5176 if (error
&& error
!= ENOENT
)
5179 error
= nvlist_lookup_nvlist(innvl
, "localprops", &localprops
);
5180 if (error
&& error
!= ENOENT
)
5183 error
= nvlist_lookup_nvlist(innvl
, ZPOOL_HIDDEN_ARGS
, &hidden_args
);
5184 if (error
&& error
!= ENOENT
)
5187 error
= zfs_ioc_recv_impl(tofs
, tosnap
, origin
, recvprops
, localprops
,
5188 hidden_args
, force
, resumable
, input_fd
, begin_record
, cleanup_fd
,
5189 &read_bytes
, &errflags
, &action_handle
, &errors
);
5191 fnvlist_add_uint64(outnvl
, "read_bytes", read_bytes
);
5192 fnvlist_add_uint64(outnvl
, "error_flags", errflags
);
5193 fnvlist_add_uint64(outnvl
, "action_handle", action_handle
);
5194 fnvlist_add_nvlist(outnvl
, "errors", errors
);
5196 nvlist_free(errors
);
5197 nvlist_free(recvprops
);
5198 nvlist_free(localprops
);
5203 typedef struct dump_bytes_io
{
5211 dump_bytes_cb(void *arg
)
5213 dump_bytes_io_t
*dbi
= (dump_bytes_io_t
*)arg
;
5214 ssize_t resid
; /* have to get resid to get detailed errno */
5216 dbi
->dbi_err
= vn_rdwr(UIO_WRITE
, dbi
->dbi_vp
,
5217 (caddr_t
)dbi
->dbi_buf
, dbi
->dbi_len
,
5218 0, UIO_SYSSPACE
, FAPPEND
, RLIM64_INFINITY
, CRED(), &resid
);
5222 dump_bytes(objset_t
*os
, void *buf
, int len
, void *arg
)
5224 dump_bytes_io_t dbi
;
5230 #if defined(HAVE_LARGE_STACKS)
5231 dump_bytes_cb(&dbi
);
5234 * The vn_rdwr() call is performed in a taskq to ensure that there is
5235 * always enough stack space to write safely to the target filesystem.
5236 * The ZIO_TYPE_FREE threads are used because there can be a lot of
5237 * them and they are used in vdev_file.c for a similar purpose.
5239 spa_taskq_dispatch_sync(dmu_objset_spa(os
), ZIO_TYPE_FREE
,
5240 ZIO_TASKQ_ISSUE
, dump_bytes_cb
, &dbi
, TQ_SLEEP
);
5241 #endif /* HAVE_LARGE_STACKS */
5243 return (dbi
.dbi_err
);
5248 * zc_name name of snapshot to send
5249 * zc_cookie file descriptor to send stream to
5250 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
5251 * zc_sendobj objsetid of snapshot to send
5252 * zc_fromobj objsetid of incremental fromsnap (may be zero)
5253 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
5254 * output size in zc_objset_type.
5255 * zc_flags lzc_send_flags
5258 * zc_objset_type estimated size, if zc_guid is set
5260 * NOTE: This is no longer the preferred interface, any new functionality
5261 * should be added to zfs_ioc_send_new() instead.
5264 zfs_ioc_send(zfs_cmd_t
*zc
)
5268 boolean_t estimate
= (zc
->zc_guid
!= 0);
5269 boolean_t embedok
= (zc
->zc_flags
& 0x1);
5270 boolean_t large_block_ok
= (zc
->zc_flags
& 0x2);
5271 boolean_t compressok
= (zc
->zc_flags
& 0x4);
5272 boolean_t rawok
= (zc
->zc_flags
& 0x8);
5274 if (zc
->zc_obj
!= 0) {
5276 dsl_dataset_t
*tosnap
;
5278 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
5282 error
= dsl_dataset_hold_obj(dp
, zc
->zc_sendobj
, FTAG
, &tosnap
);
5284 dsl_pool_rele(dp
, FTAG
);
5288 if (dsl_dir_is_clone(tosnap
->ds_dir
))
5290 dsl_dir_phys(tosnap
->ds_dir
)->dd_origin_obj
;
5291 dsl_dataset_rele(tosnap
, FTAG
);
5292 dsl_pool_rele(dp
, FTAG
);
5297 dsl_dataset_t
*tosnap
;
5298 dsl_dataset_t
*fromsnap
= NULL
;
5300 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
5304 error
= dsl_dataset_hold_obj(dp
, zc
->zc_sendobj
,
5307 dsl_pool_rele(dp
, FTAG
);
5311 if (zc
->zc_fromobj
!= 0) {
5312 error
= dsl_dataset_hold_obj(dp
, zc
->zc_fromobj
,
5315 dsl_dataset_rele(tosnap
, FTAG
);
5316 dsl_pool_rele(dp
, FTAG
);
5321 error
= dmu_send_estimate_fast(tosnap
, fromsnap
, NULL
,
5322 compressok
|| rawok
, &zc
->zc_objset_type
);
5324 if (fromsnap
!= NULL
)
5325 dsl_dataset_rele(fromsnap
, FTAG
);
5326 dsl_dataset_rele(tosnap
, FTAG
);
5327 dsl_pool_rele(dp
, FTAG
);
5329 file_t
*fp
= getf(zc
->zc_cookie
);
5331 return (SET_ERROR(EBADF
));
5334 dmu_send_outparams_t out
= {0};
5335 out
.dso_outfunc
= dump_bytes
;
5336 out
.dso_arg
= fp
->f_vnode
;
5337 out
.dso_dryrun
= B_FALSE
;
5338 error
= dmu_send_obj(zc
->zc_name
, zc
->zc_sendobj
,
5339 zc
->zc_fromobj
, embedok
, large_block_ok
, compressok
, rawok
,
5340 zc
->zc_cookie
, &off
, &out
);
5342 if (VOP_SEEK(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
5344 releasef(zc
->zc_cookie
);
5351 * zc_name name of snapshot on which to report progress
5352 * zc_cookie file descriptor of send stream
5355 * zc_cookie number of bytes written in send stream thus far
5356 * zc_objset_type logical size of data traversed by send thus far
5359 zfs_ioc_send_progress(zfs_cmd_t
*zc
)
5363 dmu_sendstatus_t
*dsp
= NULL
;
5366 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
5370 error
= dsl_dataset_hold(dp
, zc
->zc_name
, FTAG
, &ds
);
5372 dsl_pool_rele(dp
, FTAG
);
5376 mutex_enter(&ds
->ds_sendstream_lock
);
5379 * Iterate over all the send streams currently active on this dataset.
5380 * If there's one which matches the specified file descriptor _and_ the
5381 * stream was started by the current process, return the progress of
5385 for (dsp
= list_head(&ds
->ds_sendstreams
); dsp
!= NULL
;
5386 dsp
= list_next(&ds
->ds_sendstreams
, dsp
)) {
5387 if (dsp
->dss_outfd
== zc
->zc_cookie
&&
5388 dsp
->dss_proc
== curproc
)
5393 zc
->zc_cookie
= atomic_cas_64((volatile uint64_t *)dsp
->dss_off
,
5395 /* This is the closest thing we have to atomic_read_64. */
5396 zc
->zc_objset_type
= atomic_cas_64(&dsp
->dss_blocks
, 0, 0);
5398 error
= SET_ERROR(ENOENT
);
5401 mutex_exit(&ds
->ds_sendstream_lock
);
5402 dsl_dataset_rele(ds
, FTAG
);
5403 dsl_pool_rele(dp
, FTAG
);
5408 zfs_ioc_inject_fault(zfs_cmd_t
*zc
)
5412 error
= zio_inject_fault(zc
->zc_name
, (int)zc
->zc_guid
, &id
,
5413 &zc
->zc_inject_record
);
5416 zc
->zc_guid
= (uint64_t)id
;
5422 zfs_ioc_clear_fault(zfs_cmd_t
*zc
)
5424 return (zio_clear_fault((int)zc
->zc_guid
));
5428 zfs_ioc_inject_list_next(zfs_cmd_t
*zc
)
5430 int id
= (int)zc
->zc_guid
;
5433 error
= zio_inject_list_next(&id
, zc
->zc_name
, sizeof (zc
->zc_name
),
5434 &zc
->zc_inject_record
);
5442 zfs_ioc_error_log(zfs_cmd_t
*zc
)
5446 size_t count
= (size_t)zc
->zc_nvlist_dst_size
;
5448 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
5451 error
= spa_get_errlog(spa
, (void *)(uintptr_t)zc
->zc_nvlist_dst
,
5454 zc
->zc_nvlist_dst_size
= count
;
5456 zc
->zc_nvlist_dst_size
= spa_get_errlog_size(spa
);
5458 spa_close(spa
, FTAG
);
5464 zfs_ioc_clear(zfs_cmd_t
*zc
)
5471 * On zpool clear we also fix up missing slogs
5473 mutex_enter(&spa_namespace_lock
);
5474 spa
= spa_lookup(zc
->zc_name
);
5476 mutex_exit(&spa_namespace_lock
);
5477 return (SET_ERROR(EIO
));
5479 if (spa_get_log_state(spa
) == SPA_LOG_MISSING
) {
5480 /* we need to let spa_open/spa_load clear the chains */
5481 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
5483 spa
->spa_last_open_failed
= 0;
5484 mutex_exit(&spa_namespace_lock
);
5486 if (zc
->zc_cookie
& ZPOOL_NO_REWIND
) {
5487 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
5490 nvlist_t
*config
= NULL
;
5492 if (zc
->zc_nvlist_src
== 0)
5493 return (SET_ERROR(EINVAL
));
5495 if ((error
= get_nvlist(zc
->zc_nvlist_src
,
5496 zc
->zc_nvlist_src_size
, zc
->zc_iflags
, &policy
)) == 0) {
5497 error
= spa_open_rewind(zc
->zc_name
, &spa
, FTAG
,
5499 if (config
!= NULL
) {
5502 if ((err
= put_nvlist(zc
, config
)) != 0)
5504 nvlist_free(config
);
5506 nvlist_free(policy
);
5514 * If multihost is enabled, resuming I/O is unsafe as another
5515 * host may have imported the pool.
5517 if (spa_multihost(spa
) && spa_suspended(spa
))
5518 return (SET_ERROR(EINVAL
));
5520 spa_vdev_state_enter(spa
, SCL_NONE
);
5522 if (zc
->zc_guid
== 0) {
5525 vd
= spa_lookup_by_guid(spa
, zc
->zc_guid
, B_TRUE
);
5527 (void) spa_vdev_state_exit(spa
, NULL
, ENODEV
);
5528 spa_close(spa
, FTAG
);
5529 return (SET_ERROR(ENODEV
));
5533 vdev_clear(spa
, vd
);
5535 (void) spa_vdev_state_exit(spa
, spa_suspended(spa
) ?
5536 NULL
: spa
->spa_root_vdev
, 0);
5539 * Resume any suspended I/Os.
5541 if (zio_resume(spa
) != 0)
5542 error
= SET_ERROR(EIO
);
5544 spa_close(spa
, FTAG
);
5550 * Reopen all the vdevs associated with the pool.
5553 * "scrub_restart" -> when true and scrub is running, allow to restart
5554 * scrub as the side effect of the reopen (boolean).
5559 static const zfs_ioc_key_t zfs_keys_pool_reopen
[] = {
5560 {"scrub_restart", DATA_TYPE_BOOLEAN_VALUE
, 0},
5565 zfs_ioc_pool_reopen(const char *pool
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
5569 boolean_t scrub_restart
= B_TRUE
;
5572 scrub_restart
= fnvlist_lookup_boolean_value(innvl
,
5576 error
= spa_open(pool
, &spa
, FTAG
);
5580 spa_vdev_state_enter(spa
, SCL_NONE
);
5583 * If the scrub_restart flag is B_FALSE and a scrub is already
5584 * in progress then set spa_scrub_reopen flag to B_TRUE so that
5585 * we don't restart the scrub as a side effect of the reopen.
5586 * Otherwise, let vdev_open() decided if a resilver is required.
5589 spa
->spa_scrub_reopen
= (!scrub_restart
&&
5590 dsl_scan_scrubbing(spa
->spa_dsl_pool
));
5591 vdev_reopen(spa
->spa_root_vdev
);
5592 spa
->spa_scrub_reopen
= B_FALSE
;
5594 (void) spa_vdev_state_exit(spa
, NULL
, 0);
5595 spa_close(spa
, FTAG
);
5601 * zc_name name of filesystem
5604 * zc_string name of conflicting snapshot, if there is one
5607 zfs_ioc_promote(zfs_cmd_t
*zc
)
5610 dsl_dataset_t
*ds
, *ods
;
5611 char origin
[ZFS_MAX_DATASET_NAME_LEN
];
5615 zc
->zc_name
[sizeof (zc
->zc_name
) - 1] = '\0';
5616 if (dataset_namecheck(zc
->zc_name
, NULL
, NULL
) != 0 ||
5617 strchr(zc
->zc_name
, '%'))
5618 return (SET_ERROR(EINVAL
));
5620 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
5624 error
= dsl_dataset_hold(dp
, zc
->zc_name
, FTAG
, &ds
);
5626 dsl_pool_rele(dp
, FTAG
);
5630 if (!dsl_dir_is_clone(ds
->ds_dir
)) {
5631 dsl_dataset_rele(ds
, FTAG
);
5632 dsl_pool_rele(dp
, FTAG
);
5633 return (SET_ERROR(EINVAL
));
5636 error
= dsl_dataset_hold_obj(dp
,
5637 dsl_dir_phys(ds
->ds_dir
)->dd_origin_obj
, FTAG
, &ods
);
5639 dsl_dataset_rele(ds
, FTAG
);
5640 dsl_pool_rele(dp
, FTAG
);
5644 dsl_dataset_name(ods
, origin
);
5645 dsl_dataset_rele(ods
, FTAG
);
5646 dsl_dataset_rele(ds
, FTAG
);
5647 dsl_pool_rele(dp
, FTAG
);
5650 * We don't need to unmount *all* the origin fs's snapshots, but
5653 cp
= strchr(origin
, '@');
5656 (void) dmu_objset_find(origin
,
5657 zfs_unmount_snap_cb
, NULL
, DS_FIND_SNAPSHOTS
);
5658 return (dsl_dataset_promote(zc
->zc_name
, zc
->zc_string
));
5662 * Retrieve a single {user|group|project}{used|quota}@... property.
5665 * zc_name name of filesystem
5666 * zc_objset_type zfs_userquota_prop_t
5667 * zc_value domain name (eg. "S-1-234-567-89")
5668 * zc_guid RID/UID/GID
5671 * zc_cookie property value
5674 zfs_ioc_userspace_one(zfs_cmd_t
*zc
)
5679 if (zc
->zc_objset_type
>= ZFS_NUM_USERQUOTA_PROPS
)
5680 return (SET_ERROR(EINVAL
));
5682 error
= zfsvfs_hold(zc
->zc_name
, FTAG
, &zfsvfs
, B_FALSE
);
5686 error
= zfs_userspace_one(zfsvfs
,
5687 zc
->zc_objset_type
, zc
->zc_value
, zc
->zc_guid
, &zc
->zc_cookie
);
5688 zfsvfs_rele(zfsvfs
, FTAG
);
5695 * zc_name name of filesystem
5696 * zc_cookie zap cursor
5697 * zc_objset_type zfs_userquota_prop_t
5698 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
5701 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
5702 * zc_cookie zap cursor
5705 zfs_ioc_userspace_many(zfs_cmd_t
*zc
)
5708 int bufsize
= zc
->zc_nvlist_dst_size
;
5711 return (SET_ERROR(ENOMEM
));
5713 int error
= zfsvfs_hold(zc
->zc_name
, FTAG
, &zfsvfs
, B_FALSE
);
5717 void *buf
= vmem_alloc(bufsize
, KM_SLEEP
);
5719 error
= zfs_userspace_many(zfsvfs
, zc
->zc_objset_type
, &zc
->zc_cookie
,
5720 buf
, &zc
->zc_nvlist_dst_size
);
5723 error
= xcopyout(buf
,
5724 (void *)(uintptr_t)zc
->zc_nvlist_dst
,
5725 zc
->zc_nvlist_dst_size
);
5727 vmem_free(buf
, bufsize
);
5728 zfsvfs_rele(zfsvfs
, FTAG
);
5735 * zc_name name of filesystem
5741 zfs_ioc_userspace_upgrade(zfs_cmd_t
*zc
)
5747 if (getzfsvfs(zc
->zc_name
, &zfsvfs
) == 0) {
5748 if (!dmu_objset_userused_enabled(zfsvfs
->z_os
)) {
5750 * If userused is not enabled, it may be because the
5751 * objset needs to be closed & reopened (to grow the
5752 * objset_phys_t). Suspend/resume the fs will do that.
5754 dsl_dataset_t
*ds
, *newds
;
5756 ds
= dmu_objset_ds(zfsvfs
->z_os
);
5757 error
= zfs_suspend_fs(zfsvfs
);
5759 dmu_objset_refresh_ownership(ds
, &newds
,
5761 error
= zfs_resume_fs(zfsvfs
, newds
);
5765 error
= dmu_objset_userspace_upgrade(zfsvfs
->z_os
);
5766 deactivate_super(zfsvfs
->z_sb
);
5768 /* XXX kind of reading contents without owning */
5769 error
= dmu_objset_hold_flags(zc
->zc_name
, B_TRUE
, FTAG
, &os
);
5773 error
= dmu_objset_userspace_upgrade(os
);
5774 dmu_objset_rele_flags(os
, B_TRUE
, FTAG
);
5782 * zc_name name of filesystem
5788 zfs_ioc_id_quota_upgrade(zfs_cmd_t
*zc
)
5793 error
= dmu_objset_hold_flags(zc
->zc_name
, B_TRUE
, FTAG
, &os
);
5797 if (dmu_objset_userobjspace_upgradable(os
) ||
5798 dmu_objset_projectquota_upgradable(os
)) {
5799 mutex_enter(&os
->os_upgrade_lock
);
5800 if (os
->os_upgrade_id
== 0) {
5801 /* clear potential error code and retry */
5802 os
->os_upgrade_status
= 0;
5803 mutex_exit(&os
->os_upgrade_lock
);
5805 dmu_objset_id_quota_upgrade(os
);
5807 mutex_exit(&os
->os_upgrade_lock
);
5810 dsl_pool_rele(dmu_objset_pool(os
), FTAG
);
5812 taskq_wait_id(os
->os_spa
->spa_upgrade_taskq
, os
->os_upgrade_id
);
5813 error
= os
->os_upgrade_status
;
5815 dsl_pool_rele(dmu_objset_pool(os
), FTAG
);
5818 dsl_dataset_rele_flags(dmu_objset_ds(os
), DS_HOLD_FLAG_DECRYPT
, FTAG
);
5824 zfs_ioc_share(zfs_cmd_t
*zc
)
5826 return (SET_ERROR(ENOSYS
));
5829 ace_t full_access
[] = {
5830 {(uid_t
)-1, ACE_ALL_PERMS
, ACE_EVERYONE
, 0}
5835 * zc_name name of containing filesystem
5836 * zc_obj object # beyond which we want next in-use object #
5839 * zc_obj next in-use object #
5842 zfs_ioc_next_obj(zfs_cmd_t
*zc
)
5844 objset_t
*os
= NULL
;
5847 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
5851 error
= dmu_object_next(os
, &zc
->zc_obj
, B_FALSE
, 0);
5853 dmu_objset_rele(os
, FTAG
);
5859 * zc_name name of filesystem
5860 * zc_value prefix name for snapshot
5861 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
5864 * zc_value short name of new snapshot
5867 zfs_ioc_tmp_snapshot(zfs_cmd_t
*zc
)
5874 error
= zfs_onexit_fd_hold(zc
->zc_cleanup_fd
, &minor
);
5878 snap_name
= kmem_asprintf("%s-%016llx", zc
->zc_value
,
5879 (u_longlong_t
)ddi_get_lbolt64());
5880 hold_name
= kmem_asprintf("%%%s", zc
->zc_value
);
5882 error
= dsl_dataset_snapshot_tmp(zc
->zc_name
, snap_name
, minor
,
5885 (void) strlcpy(zc
->zc_value
, snap_name
,
5886 sizeof (zc
->zc_value
));
5889 zfs_onexit_fd_rele(zc
->zc_cleanup_fd
);
5895 * zc_name name of "to" snapshot
5896 * zc_value name of "from" snapshot
5897 * zc_cookie file descriptor to write diff data on
5900 * dmu_diff_record_t's to the file descriptor
5903 zfs_ioc_diff(zfs_cmd_t
*zc
)
5909 fp
= getf(zc
->zc_cookie
);
5911 return (SET_ERROR(EBADF
));
5915 error
= dmu_diff(zc
->zc_name
, zc
->zc_value
, fp
->f_vnode
, &off
);
5917 if (VOP_SEEK(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
5919 releasef(zc
->zc_cookie
);
5925 zfs_ioc_smb_acl(zfs_cmd_t
*zc
)
5927 return (SET_ERROR(ENOTSUP
));
5932 * "holds" -> { snapname -> holdname (string), ... }
5933 * (optional) "cleanup_fd" -> fd (int32)
5937 * snapname -> error value (int32)
5941 static const zfs_ioc_key_t zfs_keys_hold
[] = {
5942 {"holds", DATA_TYPE_NVLIST
, 0},
5943 {"cleanup_fd", DATA_TYPE_INT32
, ZK_OPTIONAL
},
5948 zfs_ioc_hold(const char *pool
, nvlist_t
*args
, nvlist_t
*errlist
)
5952 int cleanup_fd
= -1;
5956 holds
= fnvlist_lookup_nvlist(args
, "holds");
5958 /* make sure the user didn't pass us any invalid (empty) tags */
5959 for (pair
= nvlist_next_nvpair(holds
, NULL
); pair
!= NULL
;
5960 pair
= nvlist_next_nvpair(holds
, pair
)) {
5963 error
= nvpair_value_string(pair
, &htag
);
5965 return (SET_ERROR(error
));
5967 if (strlen(htag
) == 0)
5968 return (SET_ERROR(EINVAL
));
5971 if (nvlist_lookup_int32(args
, "cleanup_fd", &cleanup_fd
) == 0) {
5972 error
= zfs_onexit_fd_hold(cleanup_fd
, &minor
);
5977 error
= dsl_dataset_user_hold(holds
, minor
, errlist
);
5979 zfs_onexit_fd_rele(cleanup_fd
);
5984 * innvl is not used.
5987 * holdname -> time added (uint64 seconds since epoch)
5991 static const zfs_ioc_key_t zfs_keys_get_holds
[] = {
5997 zfs_ioc_get_holds(const char *snapname
, nvlist_t
*args
, nvlist_t
*outnvl
)
5999 return (dsl_dataset_get_holds(snapname
, outnvl
));
6004 * snapname -> { holdname, ... }
6009 * snapname -> error value (int32)
6013 static const zfs_ioc_key_t zfs_keys_release
[] = {
6014 {"<snapname>...", DATA_TYPE_NVLIST
, ZK_WILDCARDLIST
},
6019 zfs_ioc_release(const char *pool
, nvlist_t
*holds
, nvlist_t
*errlist
)
6021 return (dsl_dataset_user_release(holds
, errlist
));
6026 * zc_guid flags (ZEVENT_NONBLOCK)
6027 * zc_cleanup_fd zevent file descriptor
6030 * zc_nvlist_dst next nvlist event
6031 * zc_cookie dropped events since last get
6034 zfs_ioc_events_next(zfs_cmd_t
*zc
)
6037 nvlist_t
*event
= NULL
;
6039 uint64_t dropped
= 0;
6042 error
= zfs_zevent_fd_hold(zc
->zc_cleanup_fd
, &minor
, &ze
);
6047 error
= zfs_zevent_next(ze
, &event
,
6048 &zc
->zc_nvlist_dst_size
, &dropped
);
6049 if (event
!= NULL
) {
6050 zc
->zc_cookie
= dropped
;
6051 error
= put_nvlist(zc
, event
);
6055 if (zc
->zc_guid
& ZEVENT_NONBLOCK
)
6058 if ((error
== 0) || (error
!= ENOENT
))
6061 error
= zfs_zevent_wait(ze
);
6066 zfs_zevent_fd_rele(zc
->zc_cleanup_fd
);
6073 * zc_cookie cleared events count
6076 zfs_ioc_events_clear(zfs_cmd_t
*zc
)
6080 zfs_zevent_drain_all(&count
);
6081 zc
->zc_cookie
= count
;
6088 * zc_guid eid | ZEVENT_SEEK_START | ZEVENT_SEEK_END
6089 * zc_cleanup zevent file descriptor
6092 zfs_ioc_events_seek(zfs_cmd_t
*zc
)
6098 error
= zfs_zevent_fd_hold(zc
->zc_cleanup_fd
, &minor
, &ze
);
6102 error
= zfs_zevent_seek(ze
, zc
->zc_guid
);
6103 zfs_zevent_fd_rele(zc
->zc_cleanup_fd
);
6110 * zc_name name of later filesystem or snapshot
6111 * zc_value full name of old snapshot or bookmark
6114 * zc_cookie space in bytes
6115 * zc_objset_type compressed space in bytes
6116 * zc_perm_action uncompressed space in bytes
6119 zfs_ioc_space_written(zfs_cmd_t
*zc
)
6125 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
6128 error
= dsl_dataset_hold(dp
, zc
->zc_name
, FTAG
, &new);
6130 dsl_pool_rele(dp
, FTAG
);
6133 if (strchr(zc
->zc_value
, '#') != NULL
) {
6134 zfs_bookmark_phys_t bmp
;
6135 error
= dsl_bookmark_lookup(dp
, zc
->zc_value
,
6138 error
= dsl_dataset_space_written_bookmark(&bmp
, new,
6140 &zc
->zc_objset_type
, &zc
->zc_perm_action
);
6144 error
= dsl_dataset_hold(dp
, zc
->zc_value
, FTAG
, &old
);
6147 error
= dsl_dataset_space_written(old
, new,
6149 &zc
->zc_objset_type
, &zc
->zc_perm_action
);
6150 dsl_dataset_rele(old
, FTAG
);
6153 dsl_dataset_rele(new, FTAG
);
6154 dsl_pool_rele(dp
, FTAG
);
6160 * "firstsnap" -> snapshot name
6164 * "used" -> space in bytes
6165 * "compressed" -> compressed space in bytes
6166 * "uncompressed" -> uncompressed space in bytes
6169 static const zfs_ioc_key_t zfs_keys_space_snaps
[] = {
6170 {"firstsnap", DATA_TYPE_STRING
, 0},
6174 zfs_ioc_space_snaps(const char *lastsnap
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
6178 dsl_dataset_t
*new, *old
;
6180 uint64_t used
, comp
, uncomp
;
6182 firstsnap
= fnvlist_lookup_string(innvl
, "firstsnap");
6184 error
= dsl_pool_hold(lastsnap
, FTAG
, &dp
);
6188 error
= dsl_dataset_hold(dp
, lastsnap
, FTAG
, &new);
6189 if (error
== 0 && !new->ds_is_snapshot
) {
6190 dsl_dataset_rele(new, FTAG
);
6191 error
= SET_ERROR(EINVAL
);
6194 dsl_pool_rele(dp
, FTAG
);
6197 error
= dsl_dataset_hold(dp
, firstsnap
, FTAG
, &old
);
6198 if (error
== 0 && !old
->ds_is_snapshot
) {
6199 dsl_dataset_rele(old
, FTAG
);
6200 error
= SET_ERROR(EINVAL
);
6203 dsl_dataset_rele(new, FTAG
);
6204 dsl_pool_rele(dp
, FTAG
);
6208 error
= dsl_dataset_space_wouldfree(old
, new, &used
, &comp
, &uncomp
);
6209 dsl_dataset_rele(old
, FTAG
);
6210 dsl_dataset_rele(new, FTAG
);
6211 dsl_pool_rele(dp
, FTAG
);
6212 fnvlist_add_uint64(outnvl
, "used", used
);
6213 fnvlist_add_uint64(outnvl
, "compressed", comp
);
6214 fnvlist_add_uint64(outnvl
, "uncompressed", uncomp
);
6220 * "fd" -> file descriptor to write stream to (int32)
6221 * (optional) "fromsnap" -> full snap name to send an incremental from
6222 * (optional) "largeblockok" -> (value ignored)
6223 * indicates that blocks > 128KB are permitted
6224 * (optional) "embedok" -> (value ignored)
6225 * presence indicates DRR_WRITE_EMBEDDED records are permitted
6226 * (optional) "compressok" -> (value ignored)
6227 * presence indicates compressed DRR_WRITE records are permitted
6228 * (optional) "rawok" -> (value ignored)
6229 * presence indicates raw encrypted records should be used.
6230 * (optional) "resume_object" and "resume_offset" -> (uint64)
6231 * if present, resume send stream from specified object and offset.
6232 * (optional) "redactbook" -> (string)
6233 * if present, use this bookmark's redaction list to generate a redacted
6239 static const zfs_ioc_key_t zfs_keys_send_new
[] = {
6240 {"fd", DATA_TYPE_INT32
, 0},
6241 {"fromsnap", DATA_TYPE_STRING
, ZK_OPTIONAL
},
6242 {"largeblockok", DATA_TYPE_BOOLEAN
, ZK_OPTIONAL
},
6243 {"embedok", DATA_TYPE_BOOLEAN
, ZK_OPTIONAL
},
6244 {"compressok", DATA_TYPE_BOOLEAN
, ZK_OPTIONAL
},
6245 {"rawok", DATA_TYPE_BOOLEAN
, ZK_OPTIONAL
},
6246 {"resume_object", DATA_TYPE_UINT64
, ZK_OPTIONAL
},
6247 {"resume_offset", DATA_TYPE_UINT64
, ZK_OPTIONAL
},
6248 {"redactbook", DATA_TYPE_STRING
, ZK_OPTIONAL
},
6253 zfs_ioc_send_new(const char *snapname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
6257 char *fromname
= NULL
;
6260 boolean_t largeblockok
;
6262 boolean_t compressok
;
6264 uint64_t resumeobj
= 0;
6265 uint64_t resumeoff
= 0;
6266 char *redactbook
= NULL
;
6268 fd
= fnvlist_lookup_int32(innvl
, "fd");
6270 (void) nvlist_lookup_string(innvl
, "fromsnap", &fromname
);
6272 largeblockok
= nvlist_exists(innvl
, "largeblockok");
6273 embedok
= nvlist_exists(innvl
, "embedok");
6274 compressok
= nvlist_exists(innvl
, "compressok");
6275 rawok
= nvlist_exists(innvl
, "rawok");
6277 (void) nvlist_lookup_uint64(innvl
, "resume_object", &resumeobj
);
6278 (void) nvlist_lookup_uint64(innvl
, "resume_offset", &resumeoff
);
6280 (void) nvlist_lookup_string(innvl
, "redactbook", &redactbook
);
6282 if ((fp
= getf(fd
)) == NULL
)
6283 return (SET_ERROR(EBADF
));
6286 dmu_send_outparams_t out
= {0};
6287 out
.dso_outfunc
= dump_bytes
;
6288 out
.dso_arg
= fp
->f_vnode
;
6289 out
.dso_dryrun
= B_FALSE
;
6290 error
= dmu_send(snapname
, fromname
, embedok
, largeblockok
, compressok
,
6291 rawok
, resumeobj
, resumeoff
, redactbook
, fd
, &off
, &out
);
6293 if (VOP_SEEK(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
6302 send_space_sum(objset_t
*os
, void *buf
, int len
, void *arg
)
6304 uint64_t *size
= arg
;
6310 * Determine approximately how large a zfs send stream will be -- the number
6311 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
6314 * (optional) "from" -> full snap or bookmark name to send an incremental
6316 * (optional) "largeblockok" -> (value ignored)
6317 * indicates that blocks > 128KB are permitted
6318 * (optional) "embedok" -> (value ignored)
6319 * presence indicates DRR_WRITE_EMBEDDED records are permitted
6320 * (optional) "compressok" -> (value ignored)
6321 * presence indicates compressed DRR_WRITE records are permitted
6322 * (optional) "rawok" -> (value ignored)
6323 * presence indicates raw encrypted records should be used.
6324 * (optional) "fd" -> file descriptor to use as a cookie for progress
6329 * "space" -> bytes of space (uint64)
6332 static const zfs_ioc_key_t zfs_keys_send_space
[] = {
6333 {"from", DATA_TYPE_STRING
, ZK_OPTIONAL
},
6334 {"fromsnap", DATA_TYPE_STRING
, ZK_OPTIONAL
},
6335 {"largeblockok", DATA_TYPE_BOOLEAN
, ZK_OPTIONAL
},
6336 {"embedok", DATA_TYPE_BOOLEAN
, ZK_OPTIONAL
},
6337 {"compressok", DATA_TYPE_BOOLEAN
, ZK_OPTIONAL
},
6338 {"rawok", DATA_TYPE_BOOLEAN
, ZK_OPTIONAL
},
6339 {"fd", DATA_TYPE_INT32
, ZK_OPTIONAL
},
6340 {"redactbook", DATA_TYPE_STRING
, ZK_OPTIONAL
},
6341 {"resumeobj", DATA_TYPE_UINT64
, ZK_OPTIONAL
},
6342 {"resumeoff", DATA_TYPE_UINT64
, ZK_OPTIONAL
},
6343 {"bytes", DATA_TYPE_UINT64
, ZK_OPTIONAL
},
6347 zfs_ioc_send_space(const char *snapname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
6350 dsl_dataset_t
*tosnap
;
6351 dsl_dataset_t
*fromsnap
= NULL
;
6353 char *fromname
= NULL
;
6354 char *redactlist_book
= NULL
;
6355 boolean_t largeblockok
;
6357 boolean_t compressok
;
6360 boolean_t full_estimate
= B_FALSE
;
6361 uint64_t resumeobj
= 0;
6362 uint64_t resumeoff
= 0;
6363 uint64_t resume_bytes
= 0;
6365 zfs_bookmark_phys_t zbm
= {0};
6367 error
= dsl_pool_hold(snapname
, FTAG
, &dp
);
6371 error
= dsl_dataset_hold(dp
, snapname
, FTAG
, &tosnap
);
6373 dsl_pool_rele(dp
, FTAG
);
6376 (void) nvlist_lookup_int32(innvl
, "fd", &fd
);
6378 largeblockok
= nvlist_exists(innvl
, "largeblockok");
6379 embedok
= nvlist_exists(innvl
, "embedok");
6380 compressok
= nvlist_exists(innvl
, "compressok");
6381 rawok
= nvlist_exists(innvl
, "rawok");
6382 boolean_t from
= (nvlist_lookup_string(innvl
, "from", &fromname
) == 0);
6383 boolean_t altbook
= (nvlist_lookup_string(innvl
, "redactbook",
6384 &redactlist_book
) == 0);
6386 (void) nvlist_lookup_uint64(innvl
, "resume_object", &resumeobj
);
6387 (void) nvlist_lookup_uint64(innvl
, "resume_offset", &resumeoff
);
6388 (void) nvlist_lookup_uint64(innvl
, "bytes", &resume_bytes
);
6391 full_estimate
= B_TRUE
;
6393 if (strchr(fromname
, '#')) {
6394 error
= dsl_bookmark_lookup(dp
, fromname
, tosnap
, &zbm
);
6397 * dsl_bookmark_lookup() will fail with EXDEV if
6398 * the from-bookmark and tosnap are at the same txg.
6399 * However, it's valid to do a send (and therefore,
6400 * a send estimate) from and to the same time point,
6401 * if the bookmark is redacted (the incremental send
6402 * can change what's redacted on the target). In
6403 * this case, dsl_bookmark_lookup() fills in zbm
6404 * but returns EXDEV. Ignore this error.
6406 if (error
== EXDEV
&& zbm
.zbm_redaction_obj
!= 0 &&
6408 dsl_dataset_phys(tosnap
)->ds_guid
)
6412 dsl_dataset_rele(tosnap
, FTAG
);
6413 dsl_pool_rele(dp
, FTAG
);
6416 if (zbm
.zbm_redaction_obj
!= 0 || !(zbm
.zbm_flags
&
6417 ZBM_FLAG_HAS_FBN
)) {
6418 full_estimate
= B_TRUE
;
6420 } else if (strchr(fromname
, '@')) {
6421 error
= dsl_dataset_hold(dp
, fromname
, FTAG
, &fromsnap
);
6423 dsl_dataset_rele(tosnap
, FTAG
);
6424 dsl_pool_rele(dp
, FTAG
);
6428 if (!dsl_dataset_is_before(tosnap
, fromsnap
, 0)) {
6429 full_estimate
= B_TRUE
;
6430 dsl_dataset_rele(fromsnap
, FTAG
);
6434 * from is not properly formatted as a snapshot or
6437 dsl_dataset_rele(tosnap
, FTAG
);
6438 dsl_pool_rele(dp
, FTAG
);
6439 return (SET_ERROR(EINVAL
));
6443 if (full_estimate
) {
6444 dmu_send_outparams_t out
= {0};
6446 out
.dso_outfunc
= send_space_sum
;
6447 out
.dso_arg
= &space
;
6448 out
.dso_dryrun
= B_TRUE
;
6450 * We have to release these holds so dmu_send can take them. It
6451 * will do all the error checking we need.
6453 dsl_dataset_rele(tosnap
, FTAG
);
6454 dsl_pool_rele(dp
, FTAG
);
6455 error
= dmu_send(snapname
, fromname
, embedok
, largeblockok
,
6456 compressok
, rawok
, resumeobj
, resumeoff
, redactlist_book
,
6459 error
= dmu_send_estimate_fast(tosnap
, fromsnap
,
6460 (from
&& strchr(fromname
, '#') != NULL
? &zbm
: NULL
),
6461 compressok
|| rawok
, &space
);
6462 space
-= resume_bytes
;
6463 if (fromsnap
!= NULL
)
6464 dsl_dataset_rele(fromsnap
, FTAG
);
6465 dsl_dataset_rele(tosnap
, FTAG
);
6466 dsl_pool_rele(dp
, FTAG
);
6469 fnvlist_add_uint64(outnvl
, "space", space
);
6475 * Sync the currently open TXG to disk for the specified pool.
6476 * This is somewhat similar to 'zfs_sync()'.
6477 * For cases that do not result in error this ioctl will wait for
6478 * the currently open TXG to commit before returning back to the caller.
6481 * "force" -> when true, force uberblock update even if there is no dirty data.
6482 * In addition this will cause the vdev configuration to be written
6483 * out including updating the zpool cache file. (boolean_t)
6488 static const zfs_ioc_key_t zfs_keys_pool_sync
[] = {
6489 {"force", DATA_TYPE_BOOLEAN_VALUE
, 0},
6494 zfs_ioc_pool_sync(const char *pool
, nvlist_t
*innvl
, nvlist_t
*onvl
)
6497 boolean_t force
= B_FALSE
;
6500 if ((err
= spa_open(pool
, &spa
, FTAG
)) != 0)
6504 force
= fnvlist_lookup_boolean_value(innvl
, "force");
6507 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_WRITER
);
6508 vdev_config_dirty(spa
->spa_root_vdev
);
6509 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
6511 txg_wait_synced(spa_get_dsl(spa
), 0);
6513 spa_close(spa
, FTAG
);
6519 * Load a user's wrapping key into the kernel.
6521 * "hidden_args" -> { "wkeydata" -> value }
6522 * raw uint8_t array of encryption wrapping key data (32 bytes)
6523 * (optional) "noop" -> (value ignored)
6524 * presence indicated key should only be verified, not loaded
6527 static const zfs_ioc_key_t zfs_keys_load_key
[] = {
6528 {"hidden_args", DATA_TYPE_NVLIST
, 0},
6529 {"noop", DATA_TYPE_BOOLEAN
, ZK_OPTIONAL
},
6534 zfs_ioc_load_key(const char *dsname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
6537 dsl_crypto_params_t
*dcp
= NULL
;
6538 nvlist_t
*hidden_args
;
6539 boolean_t noop
= nvlist_exists(innvl
, "noop");
6541 if (strchr(dsname
, '@') != NULL
|| strchr(dsname
, '%') != NULL
) {
6542 ret
= SET_ERROR(EINVAL
);
6546 hidden_args
= fnvlist_lookup_nvlist(innvl
, ZPOOL_HIDDEN_ARGS
);
6548 ret
= dsl_crypto_params_create_nvlist(DCP_CMD_NONE
, NULL
,
6553 ret
= spa_keystore_load_wkey(dsname
, dcp
, noop
);
6557 dsl_crypto_params_free(dcp
, noop
);
6562 dsl_crypto_params_free(dcp
, B_TRUE
);
6567 * Unload a user's wrapping key from the kernel.
6568 * Both innvl and outnvl are unused.
6570 static const zfs_ioc_key_t zfs_keys_unload_key
[] = {
6576 zfs_ioc_unload_key(const char *dsname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
6580 if (strchr(dsname
, '@') != NULL
|| strchr(dsname
, '%') != NULL
) {
6581 ret
= (SET_ERROR(EINVAL
));
6585 ret
= spa_keystore_unload_wkey(dsname
);
6594 * Changes a user's wrapping key used to decrypt a dataset. The keyformat,
6595 * keylocation, pbkdf2salt, and pbkdf2iters properties can also be specified
6596 * here to change how the key is derived in userspace.
6599 * "hidden_args" (optional) -> { "wkeydata" -> value }
6600 * raw uint8_t array of new encryption wrapping key data (32 bytes)
6601 * "props" (optional) -> { prop -> value }
6606 static const zfs_ioc_key_t zfs_keys_change_key
[] = {
6607 {"crypt_cmd", DATA_TYPE_UINT64
, ZK_OPTIONAL
},
6608 {"hidden_args", DATA_TYPE_NVLIST
, ZK_OPTIONAL
},
6609 {"props", DATA_TYPE_NVLIST
, ZK_OPTIONAL
},
6614 zfs_ioc_change_key(const char *dsname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
6617 uint64_t cmd
= DCP_CMD_NONE
;
6618 dsl_crypto_params_t
*dcp
= NULL
;
6619 nvlist_t
*args
= NULL
, *hidden_args
= NULL
;
6621 if (strchr(dsname
, '@') != NULL
|| strchr(dsname
, '%') != NULL
) {
6622 ret
= (SET_ERROR(EINVAL
));
6626 (void) nvlist_lookup_uint64(innvl
, "crypt_cmd", &cmd
);
6627 (void) nvlist_lookup_nvlist(innvl
, "props", &args
);
6628 (void) nvlist_lookup_nvlist(innvl
, ZPOOL_HIDDEN_ARGS
, &hidden_args
);
6630 ret
= dsl_crypto_params_create_nvlist(cmd
, args
, hidden_args
, &dcp
);
6634 ret
= spa_keystore_change_key(dsname
, dcp
);
6638 dsl_crypto_params_free(dcp
, B_FALSE
);
6643 dsl_crypto_params_free(dcp
, B_TRUE
);
6647 static zfs_ioc_vec_t zfs_ioc_vec
[ZFS_IOC_LAST
- ZFS_IOC_FIRST
];
6650 zfs_ioctl_register_legacy(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
6651 zfs_secpolicy_func_t
*secpolicy
, zfs_ioc_namecheck_t namecheck
,
6652 boolean_t log_history
, zfs_ioc_poolcheck_t pool_check
)
6654 zfs_ioc_vec_t
*vec
= &zfs_ioc_vec
[ioc
- ZFS_IOC_FIRST
];
6656 ASSERT3U(ioc
, >=, ZFS_IOC_FIRST
);
6657 ASSERT3U(ioc
, <, ZFS_IOC_LAST
);
6658 ASSERT3P(vec
->zvec_legacy_func
, ==, NULL
);
6659 ASSERT3P(vec
->zvec_func
, ==, NULL
);
6661 vec
->zvec_legacy_func
= func
;
6662 vec
->zvec_secpolicy
= secpolicy
;
6663 vec
->zvec_namecheck
= namecheck
;
6664 vec
->zvec_allow_log
= log_history
;
6665 vec
->zvec_pool_check
= pool_check
;
6669 * See the block comment at the beginning of this file for details on
6670 * each argument to this function.
6673 zfs_ioctl_register(const char *name
, zfs_ioc_t ioc
, zfs_ioc_func_t
*func
,
6674 zfs_secpolicy_func_t
*secpolicy
, zfs_ioc_namecheck_t namecheck
,
6675 zfs_ioc_poolcheck_t pool_check
, boolean_t smush_outnvlist
,
6676 boolean_t allow_log
, const zfs_ioc_key_t
*nvl_keys
, size_t num_keys
)
6678 zfs_ioc_vec_t
*vec
= &zfs_ioc_vec
[ioc
- ZFS_IOC_FIRST
];
6680 ASSERT3U(ioc
, >=, ZFS_IOC_FIRST
);
6681 ASSERT3U(ioc
, <, ZFS_IOC_LAST
);
6682 ASSERT3P(vec
->zvec_legacy_func
, ==, NULL
);
6683 ASSERT3P(vec
->zvec_func
, ==, NULL
);
6685 /* if we are logging, the name must be valid */
6686 ASSERT(!allow_log
|| namecheck
!= NO_NAME
);
6688 vec
->zvec_name
= name
;
6689 vec
->zvec_func
= func
;
6690 vec
->zvec_secpolicy
= secpolicy
;
6691 vec
->zvec_namecheck
= namecheck
;
6692 vec
->zvec_pool_check
= pool_check
;
6693 vec
->zvec_smush_outnvlist
= smush_outnvlist
;
6694 vec
->zvec_allow_log
= allow_log
;
6695 vec
->zvec_nvl_keys
= nvl_keys
;
6696 vec
->zvec_nvl_key_count
= num_keys
;
6700 zfs_ioctl_register_pool(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
6701 zfs_secpolicy_func_t
*secpolicy
, boolean_t log_history
,
6702 zfs_ioc_poolcheck_t pool_check
)
6704 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
6705 POOL_NAME
, log_history
, pool_check
);
6709 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
6710 zfs_secpolicy_func_t
*secpolicy
, zfs_ioc_poolcheck_t pool_check
)
6712 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
6713 DATASET_NAME
, B_FALSE
, pool_check
);
6717 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
)
6719 zfs_ioctl_register_legacy(ioc
, func
, zfs_secpolicy_config
,
6720 POOL_NAME
, B_TRUE
, POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
6724 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
6725 zfs_secpolicy_func_t
*secpolicy
)
6727 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
6728 NO_NAME
, B_FALSE
, POOL_CHECK_NONE
);
6732 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc
,
6733 zfs_ioc_legacy_func_t
*func
, zfs_secpolicy_func_t
*secpolicy
)
6735 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
6736 DATASET_NAME
, B_FALSE
, POOL_CHECK_SUSPENDED
);
6740 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
)
6742 zfs_ioctl_register_dataset_read_secpolicy(ioc
, func
,
6743 zfs_secpolicy_read
);
6747 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
6748 zfs_secpolicy_func_t
*secpolicy
)
6750 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
6751 DATASET_NAME
, B_TRUE
, POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
6755 zfs_ioctl_init(void)
6757 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT
,
6758 zfs_ioc_snapshot
, zfs_secpolicy_snapshot
, POOL_NAME
,
6759 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
,
6760 zfs_keys_snapshot
, ARRAY_SIZE(zfs_keys_snapshot
));
6762 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY
,
6763 zfs_ioc_log_history
, zfs_secpolicy_log_history
, NO_NAME
,
6764 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_FALSE
, B_FALSE
,
6765 zfs_keys_log_history
, ARRAY_SIZE(zfs_keys_log_history
));
6767 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS
,
6768 zfs_ioc_space_snaps
, zfs_secpolicy_read
, DATASET_NAME
,
6769 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
,
6770 zfs_keys_space_snaps
, ARRAY_SIZE(zfs_keys_space_snaps
));
6772 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW
,
6773 zfs_ioc_send_new
, zfs_secpolicy_send_new
, DATASET_NAME
,
6774 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
,
6775 zfs_keys_send_new
, ARRAY_SIZE(zfs_keys_send_new
));
6777 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE
,
6778 zfs_ioc_send_space
, zfs_secpolicy_read
, DATASET_NAME
,
6779 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
,
6780 zfs_keys_send_space
, ARRAY_SIZE(zfs_keys_send_space
));
6782 zfs_ioctl_register("create", ZFS_IOC_CREATE
,
6783 zfs_ioc_create
, zfs_secpolicy_create_clone
, DATASET_NAME
,
6784 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
,
6785 zfs_keys_create
, ARRAY_SIZE(zfs_keys_create
));
6787 zfs_ioctl_register("clone", ZFS_IOC_CLONE
,
6788 zfs_ioc_clone
, zfs_secpolicy_create_clone
, DATASET_NAME
,
6789 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
,
6790 zfs_keys_clone
, ARRAY_SIZE(zfs_keys_clone
));
6792 zfs_ioctl_register("remap", ZFS_IOC_REMAP
,
6793 zfs_ioc_remap
, zfs_secpolicy_remap
, DATASET_NAME
,
6794 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_FALSE
, B_TRUE
,
6795 zfs_keys_remap
, ARRAY_SIZE(zfs_keys_remap
));
6797 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS
,
6798 zfs_ioc_destroy_snaps
, zfs_secpolicy_destroy_snaps
, POOL_NAME
,
6799 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
,
6800 zfs_keys_destroy_snaps
, ARRAY_SIZE(zfs_keys_destroy_snaps
));
6802 zfs_ioctl_register("hold", ZFS_IOC_HOLD
,
6803 zfs_ioc_hold
, zfs_secpolicy_hold
, POOL_NAME
,
6804 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
,
6805 zfs_keys_hold
, ARRAY_SIZE(zfs_keys_hold
));
6806 zfs_ioctl_register("release", ZFS_IOC_RELEASE
,
6807 zfs_ioc_release
, zfs_secpolicy_release
, POOL_NAME
,
6808 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
,
6809 zfs_keys_release
, ARRAY_SIZE(zfs_keys_release
));
6811 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS
,
6812 zfs_ioc_get_holds
, zfs_secpolicy_read
, DATASET_NAME
,
6813 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
,
6814 zfs_keys_get_holds
, ARRAY_SIZE(zfs_keys_get_holds
));
6816 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK
,
6817 zfs_ioc_rollback
, zfs_secpolicy_rollback
, DATASET_NAME
,
6818 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_FALSE
, B_TRUE
,
6819 zfs_keys_rollback
, ARRAY_SIZE(zfs_keys_rollback
));
6821 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK
,
6822 zfs_ioc_bookmark
, zfs_secpolicy_bookmark
, POOL_NAME
,
6823 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
,
6824 zfs_keys_bookmark
, ARRAY_SIZE(zfs_keys_bookmark
));
6826 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS
,
6827 zfs_ioc_get_bookmarks
, zfs_secpolicy_read
, DATASET_NAME
,
6828 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
,
6829 zfs_keys_get_bookmarks
, ARRAY_SIZE(zfs_keys_get_bookmarks
));
6831 zfs_ioctl_register("get_bookmark_props", ZFS_IOC_GET_BOOKMARK_PROPS
,
6832 zfs_ioc_get_bookmark_props
, zfs_secpolicy_read
, ENTITY_NAME
,
6833 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
, zfs_keys_get_bookmark_props
,
6834 ARRAY_SIZE(zfs_keys_get_bookmark_props
));
6836 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS
,
6837 zfs_ioc_destroy_bookmarks
, zfs_secpolicy_destroy_bookmarks
,
6839 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
,
6840 zfs_keys_destroy_bookmarks
,
6841 ARRAY_SIZE(zfs_keys_destroy_bookmarks
));
6843 zfs_ioctl_register("receive", ZFS_IOC_RECV_NEW
,
6844 zfs_ioc_recv_new
, zfs_secpolicy_recv_new
, DATASET_NAME
,
6845 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
,
6846 zfs_keys_recv_new
, ARRAY_SIZE(zfs_keys_recv_new
));
6847 zfs_ioctl_register("load-key", ZFS_IOC_LOAD_KEY
,
6848 zfs_ioc_load_key
, zfs_secpolicy_load_key
,
6849 DATASET_NAME
, POOL_CHECK_SUSPENDED
, B_TRUE
, B_TRUE
,
6850 zfs_keys_load_key
, ARRAY_SIZE(zfs_keys_load_key
));
6851 zfs_ioctl_register("unload-key", ZFS_IOC_UNLOAD_KEY
,
6852 zfs_ioc_unload_key
, zfs_secpolicy_load_key
,
6853 DATASET_NAME
, POOL_CHECK_SUSPENDED
, B_TRUE
, B_TRUE
,
6854 zfs_keys_unload_key
, ARRAY_SIZE(zfs_keys_unload_key
));
6855 zfs_ioctl_register("change-key", ZFS_IOC_CHANGE_KEY
,
6856 zfs_ioc_change_key
, zfs_secpolicy_change_key
,
6857 DATASET_NAME
, POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
,
6858 B_TRUE
, B_TRUE
, zfs_keys_change_key
,
6859 ARRAY_SIZE(zfs_keys_change_key
));
6861 zfs_ioctl_register("sync", ZFS_IOC_POOL_SYNC
,
6862 zfs_ioc_pool_sync
, zfs_secpolicy_none
, POOL_NAME
,
6863 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_FALSE
, B_FALSE
,
6864 zfs_keys_pool_sync
, ARRAY_SIZE(zfs_keys_pool_sync
));
6865 zfs_ioctl_register("reopen", ZFS_IOC_POOL_REOPEN
, zfs_ioc_pool_reopen
,
6866 zfs_secpolicy_config
, POOL_NAME
, POOL_CHECK_SUSPENDED
, B_TRUE
,
6867 B_TRUE
, zfs_keys_pool_reopen
, ARRAY_SIZE(zfs_keys_pool_reopen
));
6869 zfs_ioctl_register("channel_program", ZFS_IOC_CHANNEL_PROGRAM
,
6870 zfs_ioc_channel_program
, zfs_secpolicy_config
,
6871 POOL_NAME
, POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
,
6872 B_TRUE
, zfs_keys_channel_program
,
6873 ARRAY_SIZE(zfs_keys_channel_program
));
6875 zfs_ioctl_register("redact", ZFS_IOC_REDACT
,
6876 zfs_ioc_redact
, zfs_secpolicy_config
, DATASET_NAME
,
6877 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
,
6878 zfs_keys_redact
, ARRAY_SIZE(zfs_keys_redact
));
6880 zfs_ioctl_register("zpool_checkpoint", ZFS_IOC_POOL_CHECKPOINT
,
6881 zfs_ioc_pool_checkpoint
, zfs_secpolicy_config
, POOL_NAME
,
6882 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
,
6883 zfs_keys_pool_checkpoint
, ARRAY_SIZE(zfs_keys_pool_checkpoint
));
6885 zfs_ioctl_register("zpool_discard_checkpoint",
6886 ZFS_IOC_POOL_DISCARD_CHECKPOINT
, zfs_ioc_pool_discard_checkpoint
,
6887 zfs_secpolicy_config
, POOL_NAME
,
6888 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
,
6889 zfs_keys_pool_discard_checkpoint
,
6890 ARRAY_SIZE(zfs_keys_pool_discard_checkpoint
));
6892 zfs_ioctl_register("initialize", ZFS_IOC_POOL_INITIALIZE
,
6893 zfs_ioc_pool_initialize
, zfs_secpolicy_config
, POOL_NAME
,
6894 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
,
6895 zfs_keys_pool_initialize
, ARRAY_SIZE(zfs_keys_pool_initialize
));
6897 zfs_ioctl_register("trim", ZFS_IOC_POOL_TRIM
,
6898 zfs_ioc_pool_trim
, zfs_secpolicy_config
, POOL_NAME
,
6899 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
,
6900 zfs_keys_pool_trim
, ARRAY_SIZE(zfs_keys_pool_trim
));
6902 /* IOCTLS that use the legacy function signature */
6904 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE
, zfs_ioc_pool_freeze
,
6905 zfs_secpolicy_config
, NO_NAME
, B_FALSE
, POOL_CHECK_READONLY
);
6907 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE
, zfs_ioc_pool_create
,
6908 zfs_secpolicy_config
, B_TRUE
, POOL_CHECK_NONE
);
6909 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN
,
6911 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE
,
6912 zfs_ioc_pool_upgrade
);
6913 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD
,
6915 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE
,
6916 zfs_ioc_vdev_remove
);
6917 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE
,
6918 zfs_ioc_vdev_set_state
);
6919 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH
,
6920 zfs_ioc_vdev_attach
);
6921 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH
,
6922 zfs_ioc_vdev_detach
);
6923 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH
,
6924 zfs_ioc_vdev_setpath
);
6925 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU
,
6926 zfs_ioc_vdev_setfru
);
6927 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS
,
6928 zfs_ioc_pool_set_props
);
6929 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT
,
6930 zfs_ioc_vdev_split
);
6931 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID
,
6932 zfs_ioc_pool_reguid
);
6934 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS
,
6935 zfs_ioc_pool_configs
, zfs_secpolicy_none
);
6936 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT
,
6937 zfs_ioc_pool_tryimport
, zfs_secpolicy_config
);
6938 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT
,
6939 zfs_ioc_inject_fault
, zfs_secpolicy_inject
);
6940 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT
,
6941 zfs_ioc_clear_fault
, zfs_secpolicy_inject
);
6942 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT
,
6943 zfs_ioc_inject_list_next
, zfs_secpolicy_inject
);
6946 * pool destroy, and export don't log the history as part of
6947 * zfsdev_ioctl, but rather zfs_ioc_pool_export
6948 * does the logging of those commands.
6950 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY
, zfs_ioc_pool_destroy
,
6951 zfs_secpolicy_config
, B_FALSE
, POOL_CHECK_SUSPENDED
);
6952 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT
, zfs_ioc_pool_export
,
6953 zfs_secpolicy_config
, B_FALSE
, POOL_CHECK_SUSPENDED
);
6955 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS
, zfs_ioc_pool_stats
,
6956 zfs_secpolicy_read
, B_FALSE
, POOL_CHECK_NONE
);
6957 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS
, zfs_ioc_pool_get_props
,
6958 zfs_secpolicy_read
, B_FALSE
, POOL_CHECK_NONE
);
6960 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG
, zfs_ioc_error_log
,
6961 zfs_secpolicy_inject
, B_FALSE
, POOL_CHECK_SUSPENDED
);
6962 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME
,
6963 zfs_ioc_dsobj_to_dsname
,
6964 zfs_secpolicy_diff
, B_FALSE
, POOL_CHECK_SUSPENDED
);
6965 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY
,
6966 zfs_ioc_pool_get_history
,
6967 zfs_secpolicy_config
, B_FALSE
, POOL_CHECK_SUSPENDED
);
6969 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT
, zfs_ioc_pool_import
,
6970 zfs_secpolicy_config
, B_TRUE
, POOL_CHECK_NONE
);
6972 zfs_ioctl_register_pool(ZFS_IOC_CLEAR
, zfs_ioc_clear
,
6973 zfs_secpolicy_config
, B_TRUE
, POOL_CHECK_READONLY
);
6975 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN
,
6976 zfs_ioc_space_written
);
6977 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS
,
6978 zfs_ioc_objset_recvd_props
);
6979 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ
,
6981 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL
,
6983 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS
,
6984 zfs_ioc_objset_stats
);
6985 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS
,
6986 zfs_ioc_objset_zplprops
);
6987 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT
,
6988 zfs_ioc_dataset_list_next
);
6989 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT
,
6990 zfs_ioc_snapshot_list_next
);
6991 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS
,
6992 zfs_ioc_send_progress
);
6994 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF
,
6995 zfs_ioc_diff
, zfs_secpolicy_diff
);
6996 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS
,
6997 zfs_ioc_obj_to_stats
, zfs_secpolicy_diff
);
6998 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH
,
6999 zfs_ioc_obj_to_path
, zfs_secpolicy_diff
);
7000 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE
,
7001 zfs_ioc_userspace_one
, zfs_secpolicy_userspace_one
);
7002 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY
,
7003 zfs_ioc_userspace_many
, zfs_secpolicy_userspace_many
);
7004 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND
,
7005 zfs_ioc_send
, zfs_secpolicy_send
);
7007 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP
, zfs_ioc_set_prop
,
7008 zfs_secpolicy_none
);
7009 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY
, zfs_ioc_destroy
,
7010 zfs_secpolicy_destroy
);
7011 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME
, zfs_ioc_rename
,
7012 zfs_secpolicy_rename
);
7013 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV
, zfs_ioc_recv
,
7014 zfs_secpolicy_recv
);
7015 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE
, zfs_ioc_promote
,
7016 zfs_secpolicy_promote
);
7017 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP
,
7018 zfs_ioc_inherit_prop
, zfs_secpolicy_inherit_prop
);
7019 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL
, zfs_ioc_set_fsacl
,
7020 zfs_secpolicy_set_fsacl
);
7022 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE
, zfs_ioc_share
,
7023 zfs_secpolicy_share
, POOL_CHECK_NONE
);
7024 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL
, zfs_ioc_smb_acl
,
7025 zfs_secpolicy_smb_acl
, POOL_CHECK_NONE
);
7026 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE
,
7027 zfs_ioc_userspace_upgrade
, zfs_secpolicy_userspace_upgrade
,
7028 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
7029 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT
,
7030 zfs_ioc_tmp_snapshot
, zfs_secpolicy_tmp_snapshot
,
7031 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
7036 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_NEXT
, zfs_ioc_events_next
,
7037 zfs_secpolicy_config
, NO_NAME
, B_FALSE
, POOL_CHECK_NONE
);
7038 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_CLEAR
, zfs_ioc_events_clear
,
7039 zfs_secpolicy_config
, NO_NAME
, B_FALSE
, POOL_CHECK_NONE
);
7040 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_SEEK
, zfs_ioc_events_seek
,
7041 zfs_secpolicy_config
, NO_NAME
, B_FALSE
, POOL_CHECK_NONE
);
7045 * Verify that for non-legacy ioctls the input nvlist
7046 * pairs match against the expected input.
7048 * Possible errors are:
7049 * ZFS_ERR_IOC_ARG_UNAVAIL An unrecognized nvpair was encountered
7050 * ZFS_ERR_IOC_ARG_REQUIRED A required nvpair is missing
7051 * ZFS_ERR_IOC_ARG_BADTYPE Invalid type for nvpair
7054 zfs_check_input_nvpairs(nvlist_t
*innvl
, const zfs_ioc_vec_t
*vec
)
7056 const zfs_ioc_key_t
*nvl_keys
= vec
->zvec_nvl_keys
;
7057 boolean_t required_keys_found
= B_FALSE
;
7060 * examine each input pair
7062 for (nvpair_t
*pair
= nvlist_next_nvpair(innvl
, NULL
);
7063 pair
!= NULL
; pair
= nvlist_next_nvpair(innvl
, pair
)) {
7064 char *name
= nvpair_name(pair
);
7065 data_type_t type
= nvpair_type(pair
);
7066 boolean_t identified
= B_FALSE
;
7069 * check pair against the documented names and type
7071 for (int k
= 0; k
< vec
->zvec_nvl_key_count
; k
++) {
7072 /* if not a wild card name, check for an exact match */
7073 if ((nvl_keys
[k
].zkey_flags
& ZK_WILDCARDLIST
) == 0 &&
7074 strcmp(nvl_keys
[k
].zkey_name
, name
) != 0)
7077 identified
= B_TRUE
;
7079 if (nvl_keys
[k
].zkey_type
!= DATA_TYPE_ANY
&&
7080 nvl_keys
[k
].zkey_type
!= type
) {
7081 return (SET_ERROR(ZFS_ERR_IOC_ARG_BADTYPE
));
7084 if (nvl_keys
[k
].zkey_flags
& ZK_OPTIONAL
)
7087 required_keys_found
= B_TRUE
;
7091 /* allow an 'optional' key, everything else is invalid */
7093 (strcmp(name
, "optional") != 0 ||
7094 type
!= DATA_TYPE_NVLIST
)) {
7095 return (SET_ERROR(ZFS_ERR_IOC_ARG_UNAVAIL
));
7099 /* verify that all required keys were found */
7100 for (int k
= 0; k
< vec
->zvec_nvl_key_count
; k
++) {
7101 if (nvl_keys
[k
].zkey_flags
& ZK_OPTIONAL
)
7104 if (nvl_keys
[k
].zkey_flags
& ZK_WILDCARDLIST
) {
7105 /* at least one non-optionial key is expected here */
7106 if (!required_keys_found
)
7107 return (SET_ERROR(ZFS_ERR_IOC_ARG_REQUIRED
));
7111 if (!nvlist_exists(innvl
, nvl_keys
[k
].zkey_name
))
7112 return (SET_ERROR(ZFS_ERR_IOC_ARG_REQUIRED
));
7119 pool_status_check(const char *name
, zfs_ioc_namecheck_t type
,
7120 zfs_ioc_poolcheck_t check
)
7125 ASSERT(type
== POOL_NAME
|| type
== DATASET_NAME
||
7126 type
== ENTITY_NAME
);
7128 if (check
& POOL_CHECK_NONE
)
7131 error
= spa_open(name
, &spa
, FTAG
);
7133 if ((check
& POOL_CHECK_SUSPENDED
) && spa_suspended(spa
))
7134 error
= SET_ERROR(EAGAIN
);
7135 else if ((check
& POOL_CHECK_READONLY
) && !spa_writeable(spa
))
7136 error
= SET_ERROR(EROFS
);
7137 spa_close(spa
, FTAG
);
7143 zfsdev_get_state_impl(minor_t minor
, enum zfsdev_state_type which
)
7147 for (zs
= zfsdev_state_list
; zs
!= NULL
; zs
= zs
->zs_next
) {
7148 if (zs
->zs_minor
== minor
) {
7152 return (zs
->zs_onexit
);
7154 return (zs
->zs_zevent
);
7165 zfsdev_get_state(minor_t minor
, enum zfsdev_state_type which
)
7169 ptr
= zfsdev_get_state_impl(minor
, which
);
7175 zfsdev_getminor(struct file
*filp
, minor_t
*minorp
)
7177 zfsdev_state_t
*zs
, *fpd
;
7179 ASSERT(filp
!= NULL
);
7180 ASSERT(!MUTEX_HELD(&zfsdev_state_lock
));
7182 fpd
= filp
->private_data
;
7184 return (SET_ERROR(EBADF
));
7186 mutex_enter(&zfsdev_state_lock
);
7188 for (zs
= zfsdev_state_list
; zs
!= NULL
; zs
= zs
->zs_next
) {
7190 if (zs
->zs_minor
== -1)
7194 *minorp
= fpd
->zs_minor
;
7195 mutex_exit(&zfsdev_state_lock
);
7200 mutex_exit(&zfsdev_state_lock
);
7202 return (SET_ERROR(EBADF
));
7206 * Find a free minor number. The zfsdev_state_list is expected to
7207 * be short since it is only a list of currently open file handles.
7210 zfsdev_minor_alloc(void)
7212 static minor_t last_minor
= 0;
7215 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
7217 for (m
= last_minor
+ 1; m
!= last_minor
; m
++) {
7218 if (m
> ZFSDEV_MAX_MINOR
)
7220 if (zfsdev_get_state_impl(m
, ZST_ALL
) == NULL
) {
7230 zfsdev_state_init(struct file
*filp
)
7232 zfsdev_state_t
*zs
, *zsprev
= NULL
;
7234 boolean_t newzs
= B_FALSE
;
7236 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
7238 minor
= zfsdev_minor_alloc();
7240 return (SET_ERROR(ENXIO
));
7242 for (zs
= zfsdev_state_list
; zs
!= NULL
; zs
= zs
->zs_next
) {
7243 if (zs
->zs_minor
== -1)
7249 zs
= kmem_zalloc(sizeof (zfsdev_state_t
), KM_SLEEP
);
7254 filp
->private_data
= zs
;
7256 zfs_onexit_init((zfs_onexit_t
**)&zs
->zs_onexit
);
7257 zfs_zevent_init((zfs_zevent_t
**)&zs
->zs_zevent
);
7261 * In order to provide for lock-free concurrent read access
7262 * to the minor list in zfsdev_get_state_impl(), new entries
7263 * must be completely written before linking them into the
7264 * list whereas existing entries are already linked; the last
7265 * operation must be updating zs_minor (from -1 to the new
7269 zs
->zs_minor
= minor
;
7271 zsprev
->zs_next
= zs
;
7274 zs
->zs_minor
= minor
;
7281 zfsdev_state_destroy(struct file
*filp
)
7285 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
7286 ASSERT(filp
->private_data
!= NULL
);
7288 zs
= filp
->private_data
;
7290 zfs_onexit_destroy(zs
->zs_onexit
);
7291 zfs_zevent_destroy(zs
->zs_zevent
);
7297 zfsdev_open(struct inode
*ino
, struct file
*filp
)
7301 mutex_enter(&zfsdev_state_lock
);
7302 error
= zfsdev_state_init(filp
);
7303 mutex_exit(&zfsdev_state_lock
);
7309 zfsdev_release(struct inode
*ino
, struct file
*filp
)
7313 mutex_enter(&zfsdev_state_lock
);
7314 error
= zfsdev_state_destroy(filp
);
7315 mutex_exit(&zfsdev_state_lock
);
7321 zfsdev_ioctl(struct file
*filp
, unsigned cmd
, unsigned long arg
)
7325 int error
, rc
, flag
= 0;
7326 const zfs_ioc_vec_t
*vec
;
7327 char *saved_poolname
= NULL
;
7328 nvlist_t
*innvl
= NULL
;
7329 fstrans_cookie_t cookie
;
7331 vecnum
= cmd
- ZFS_IOC_FIRST
;
7332 if (vecnum
>= sizeof (zfs_ioc_vec
) / sizeof (zfs_ioc_vec
[0]))
7333 return (-SET_ERROR(ZFS_ERR_IOC_CMD_UNAVAIL
));
7334 vec
= &zfs_ioc_vec
[vecnum
];
7337 * The registered ioctl list may be sparse, verify that either
7338 * a normal or legacy handler are registered.
7340 if (vec
->zvec_func
== NULL
&& vec
->zvec_legacy_func
== NULL
)
7341 return (-SET_ERROR(ZFS_ERR_IOC_CMD_UNAVAIL
));
7343 zc
= kmem_zalloc(sizeof (zfs_cmd_t
), KM_SLEEP
);
7345 error
= ddi_copyin((void *)arg
, zc
, sizeof (zfs_cmd_t
), flag
);
7347 error
= SET_ERROR(EFAULT
);
7351 zc
->zc_iflags
= flag
& FKIOCTL
;
7352 if (zc
->zc_nvlist_src_size
> MAX_NVLIST_SRC_SIZE
) {
7354 * Make sure the user doesn't pass in an insane value for
7355 * zc_nvlist_src_size. We have to check, since we will end
7356 * up allocating that much memory inside of get_nvlist(). This
7357 * prevents a nefarious user from allocating tons of kernel
7360 * Also, we return EINVAL instead of ENOMEM here. The reason
7361 * being that returning ENOMEM from an ioctl() has a special
7362 * connotation; that the user's size value is too small and
7363 * needs to be expanded to hold the nvlist. See
7364 * zcmd_expand_dst_nvlist() for details.
7366 error
= SET_ERROR(EINVAL
); /* User's size too big */
7368 } else if (zc
->zc_nvlist_src_size
!= 0) {
7369 error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
7370 zc
->zc_iflags
, &innvl
);
7376 * Ensure that all pool/dataset names are valid before we pass down to
7379 zc
->zc_name
[sizeof (zc
->zc_name
) - 1] = '\0';
7380 switch (vec
->zvec_namecheck
) {
7382 if (pool_namecheck(zc
->zc_name
, NULL
, NULL
) != 0)
7383 error
= SET_ERROR(EINVAL
);
7385 error
= pool_status_check(zc
->zc_name
,
7386 vec
->zvec_namecheck
, vec
->zvec_pool_check
);
7390 if (dataset_namecheck(zc
->zc_name
, NULL
, NULL
) != 0)
7391 error
= SET_ERROR(EINVAL
);
7393 error
= pool_status_check(zc
->zc_name
,
7394 vec
->zvec_namecheck
, vec
->zvec_pool_check
);
7398 if (entity_namecheck(zc
->zc_name
, NULL
, NULL
) != 0) {
7399 error
= SET_ERROR(EINVAL
);
7401 error
= pool_status_check(zc
->zc_name
,
7402 vec
->zvec_namecheck
, vec
->zvec_pool_check
);
7410 * Ensure that all input pairs are valid before we pass them down
7411 * to the lower layers.
7413 * The vectored functions can use fnvlist_lookup_{type} for any
7414 * required pairs since zfs_check_input_nvpairs() confirmed that
7415 * they exist and are of the correct type.
7417 if (error
== 0 && vec
->zvec_func
!= NULL
) {
7418 error
= zfs_check_input_nvpairs(innvl
, vec
);
7424 cookie
= spl_fstrans_mark();
7425 error
= vec
->zvec_secpolicy(zc
, innvl
, CRED());
7426 spl_fstrans_unmark(cookie
);
7432 /* legacy ioctls can modify zc_name */
7433 saved_poolname
= strdup(zc
->zc_name
);
7434 if (saved_poolname
== NULL
) {
7435 error
= SET_ERROR(ENOMEM
);
7438 saved_poolname
[strcspn(saved_poolname
, "/@#")] = '\0';
7441 if (vec
->zvec_func
!= NULL
) {
7445 nvlist_t
*lognv
= NULL
;
7447 ASSERT(vec
->zvec_legacy_func
== NULL
);
7450 * Add the innvl to the lognv before calling the func,
7451 * in case the func changes the innvl.
7453 if (vec
->zvec_allow_log
) {
7454 lognv
= fnvlist_alloc();
7455 fnvlist_add_string(lognv
, ZPOOL_HIST_IOCTL
,
7457 if (!nvlist_empty(innvl
)) {
7458 fnvlist_add_nvlist(lognv
, ZPOOL_HIST_INPUT_NVL
,
7463 outnvl
= fnvlist_alloc();
7464 cookie
= spl_fstrans_mark();
7465 error
= vec
->zvec_func(zc
->zc_name
, innvl
, outnvl
);
7466 spl_fstrans_unmark(cookie
);
7469 * Some commands can partially execute, modify state, and still
7470 * return an error. In these cases, attempt to record what
7474 (cmd
== ZFS_IOC_CHANNEL_PROGRAM
&& error
!= EINVAL
)) &&
7475 vec
->zvec_allow_log
&&
7476 spa_open(zc
->zc_name
, &spa
, FTAG
) == 0) {
7477 if (!nvlist_empty(outnvl
)) {
7478 fnvlist_add_nvlist(lognv
, ZPOOL_HIST_OUTPUT_NVL
,
7482 fnvlist_add_int64(lognv
, ZPOOL_HIST_ERRNO
,
7485 (void) spa_history_log_nvl(spa
, lognv
);
7486 spa_close(spa
, FTAG
);
7488 fnvlist_free(lognv
);
7490 if (!nvlist_empty(outnvl
) || zc
->zc_nvlist_dst_size
!= 0) {
7492 if (vec
->zvec_smush_outnvlist
) {
7493 smusherror
= nvlist_smush(outnvl
,
7494 zc
->zc_nvlist_dst_size
);
7496 if (smusherror
== 0)
7497 puterror
= put_nvlist(zc
, outnvl
);
7503 nvlist_free(outnvl
);
7505 cookie
= spl_fstrans_mark();
7506 error
= vec
->zvec_legacy_func(zc
);
7507 spl_fstrans_unmark(cookie
);
7512 rc
= ddi_copyout(zc
, (void *)arg
, sizeof (zfs_cmd_t
), flag
);
7513 if (error
== 0 && rc
!= 0)
7514 error
= SET_ERROR(EFAULT
);
7515 if (error
== 0 && vec
->zvec_allow_log
) {
7516 char *s
= tsd_get(zfs_allow_log_key
);
7519 (void) tsd_set(zfs_allow_log_key
, saved_poolname
);
7521 if (saved_poolname
!= NULL
)
7522 strfree(saved_poolname
);
7525 kmem_free(zc
, sizeof (zfs_cmd_t
));
7529 #ifdef CONFIG_COMPAT
7531 zfsdev_compat_ioctl(struct file
*filp
, unsigned cmd
, unsigned long arg
)
7533 return (zfsdev_ioctl(filp
, cmd
, arg
));
7536 #define zfsdev_compat_ioctl NULL
7539 static const struct file_operations zfsdev_fops
= {
7540 .open
= zfsdev_open
,
7541 .release
= zfsdev_release
,
7542 .unlocked_ioctl
= zfsdev_ioctl
,
7543 .compat_ioctl
= zfsdev_compat_ioctl
,
7544 .owner
= THIS_MODULE
,
7547 static struct miscdevice zfs_misc
= {
7548 .minor
= ZFS_DEVICE_MINOR
,
7550 .fops
= &zfsdev_fops
,
7553 MODULE_ALIAS_MISCDEV(ZFS_DEVICE_MINOR
);
7554 MODULE_ALIAS("devname:zfs");
7561 mutex_init(&zfsdev_state_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
7562 zfsdev_state_list
= kmem_zalloc(sizeof (zfsdev_state_t
), KM_SLEEP
);
7563 zfsdev_state_list
->zs_minor
= -1;
7565 error
= misc_register(&zfs_misc
);
7566 if (error
== -EBUSY
) {
7568 * Fallback to dynamic minor allocation in the event of a
7569 * collision with a reserved minor in linux/miscdevice.h.
7570 * In this case the kernel modules must be manually loaded.
7572 printk(KERN_INFO
"ZFS: misc_register() with static minor %d "
7573 "failed %d, retrying with MISC_DYNAMIC_MINOR\n",
7574 ZFS_DEVICE_MINOR
, error
);
7576 zfs_misc
.minor
= MISC_DYNAMIC_MINOR
;
7577 error
= misc_register(&zfs_misc
);
7581 printk(KERN_INFO
"ZFS: misc_register() failed %d\n", error
);
7589 zfsdev_state_t
*zs
, *zsprev
= NULL
;
7591 misc_deregister(&zfs_misc
);
7592 mutex_destroy(&zfsdev_state_lock
);
7594 for (zs
= zfsdev_state_list
; zs
!= NULL
; zs
= zs
->zs_next
) {
7596 kmem_free(zsprev
, sizeof (zfsdev_state_t
));
7600 kmem_free(zsprev
, sizeof (zfsdev_state_t
));
7604 zfs_allow_log_destroy(void *arg
)
7606 char *poolname
= arg
;
7608 if (poolname
!= NULL
)
7613 #define ZFS_DEBUG_STR " (DEBUG mode)"
7615 #define ZFS_DEBUG_STR ""
7623 if ((error
= -zvol_init()) != 0)
7626 spa_init(FREAD
| FWRITE
);
7632 if ((error
= zfs_attach()) != 0)
7635 tsd_create(&zfs_fsyncer_key
, NULL
);
7636 tsd_create(&rrw_tsd_key
, rrw_tsd_destroy
);
7637 tsd_create(&zfs_allow_log_key
, zfs_allow_log_destroy
);
7639 printk(KERN_NOTICE
"ZFS: Loaded module v%s-%s%s, "
7640 "ZFS pool version %s, ZFS filesystem version %s\n",
7641 ZFS_META_VERSION
, ZFS_META_RELEASE
, ZFS_DEBUG_STR
,
7642 SPA_VERSION_STRING
, ZPL_VERSION_STRING
);
7643 #ifndef CONFIG_FS_POSIX_ACL
7644 printk(KERN_NOTICE
"ZFS: Posix ACLs disabled by kernel\n");
7645 #endif /* CONFIG_FS_POSIX_ACL */
7654 printk(KERN_NOTICE
"ZFS: Failed to Load ZFS Filesystem v%s-%s%s"
7655 ", rc = %d\n", ZFS_META_VERSION
, ZFS_META_RELEASE
,
7656 ZFS_DEBUG_STR
, error
);
7670 tsd_destroy(&zfs_fsyncer_key
);
7671 tsd_destroy(&rrw_tsd_key
);
7672 tsd_destroy(&zfs_allow_log_key
);
7674 printk(KERN_NOTICE
"ZFS: Unloaded module v%s-%s%s\n",
7675 ZFS_META_VERSION
, ZFS_META_RELEASE
, ZFS_DEBUG_STR
);
7678 #if defined(_KERNEL)
7682 MODULE_DESCRIPTION("ZFS");
7683 MODULE_AUTHOR(ZFS_META_AUTHOR
);
7684 MODULE_LICENSE(ZFS_META_LICENSE
);
7685 MODULE_VERSION(ZFS_META_VERSION
"-" ZFS_META_RELEASE
);