4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Portions Copyright 2011 Martin Matuska
25 * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
26 * Portions Copyright 2012 Pawel Jakub Dawidek <pawel@dawidek.net>
27 * Copyright (c) 2014, 2016 Joyent, Inc. All rights reserved.
28 * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
29 * Copyright (c) 2014, Joyent, Inc. All rights reserved.
30 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
31 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
32 * Copyright (c) 2013 Steven Hartland. All rights reserved.
33 * Copyright (c) 2014 Integros [integros.com]
34 * Copyright 2016 Toomas Soome <tsoome@me.com>
35 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
36 * Copyright (c) 2017, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
37 * Copyright (c) 2017 Datto Inc.
38 * Copyright 2017 RackTop Systems.
44 * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
45 * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
47 * There are two ways that we handle ioctls: the legacy way where almost
48 * all of the logic is in the ioctl callback, and the new way where most
49 * of the marshalling is handled in the common entry point, zfsdev_ioctl().
51 * Non-legacy ioctls should be registered by calling
52 * zfs_ioctl_register() from zfs_ioctl_init(). The ioctl is invoked
53 * from userland by lzc_ioctl().
55 * The registration arguments are as follows:
58 * The name of the ioctl. This is used for history logging. If the
59 * ioctl returns successfully (the callback returns 0), and allow_log
60 * is true, then a history log entry will be recorded with the input &
61 * output nvlists. The log entry can be printed with "zpool history -i".
64 * The ioctl request number, which userland will pass to ioctl(2).
65 * The ioctl numbers can change from release to release, because
66 * the caller (libzfs) must be matched to the kernel.
68 * zfs_secpolicy_func_t *secpolicy
69 * This function will be called before the zfs_ioc_func_t, to
70 * determine if this operation is permitted. It should return EPERM
71 * on failure, and 0 on success. Checks include determining if the
72 * dataset is visible in this zone, and if the user has either all
73 * zfs privileges in the zone (SYS_MOUNT), or has been granted permission
74 * to do this operation on this dataset with "zfs allow".
76 * zfs_ioc_namecheck_t namecheck
77 * This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
78 * name, a dataset name, or nothing. If the name is not well-formed,
79 * the ioctl will fail and the callback will not be called.
80 * Therefore, the callback can assume that the name is well-formed
81 * (e.g. is null-terminated, doesn't have more than one '@' character,
82 * doesn't have invalid characters).
84 * zfs_ioc_poolcheck_t pool_check
85 * This specifies requirements on the pool state. If the pool does
86 * not meet them (is suspended or is readonly), the ioctl will fail
87 * and the callback will not be called. If any checks are specified
88 * (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
89 * Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
90 * POOL_CHECK_READONLY).
92 * boolean_t smush_outnvlist
93 * If smush_outnvlist is true, then the output is presumed to be a
94 * list of errors, and it will be "smushed" down to fit into the
95 * caller's buffer, by removing some entries and replacing them with a
96 * single "N_MORE_ERRORS" entry indicating how many were removed. See
97 * nvlist_smush() for details. If smush_outnvlist is false, and the
98 * outnvlist does not fit into the userland-provided buffer, then the
99 * ioctl will fail with ENOMEM.
101 * zfs_ioc_func_t *func
102 * The callback function that will perform the operation.
104 * The callback should return 0 on success, or an error number on
105 * failure. If the function fails, the userland ioctl will return -1,
106 * and errno will be set to the callback's return value. The callback
107 * will be called with the following arguments:
110 * The name of the pool or dataset to operate on, from
111 * zfs_cmd_t:zc_name. The 'namecheck' argument specifies the
112 * expected type (pool, dataset, or none).
115 * The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src. Or
116 * NULL if no input nvlist was provided. Changes to this nvlist are
117 * ignored. If the input nvlist could not be deserialized, the
118 * ioctl will fail and the callback will not be called.
121 * The output nvlist, initially empty. The callback can fill it in,
122 * and it will be returned to userland by serializing it into
123 * zfs_cmd_t:zc_nvlist_dst. If it is non-empty, and serialization
124 * fails (e.g. because the caller didn't supply a large enough
125 * buffer), then the overall ioctl will fail. See the
126 * 'smush_nvlist' argument above for additional behaviors.
128 * There are two typical uses of the output nvlist:
129 * - To return state, e.g. property values. In this case,
130 * smush_outnvlist should be false. If the buffer was not large
131 * enough, the caller will reallocate a larger buffer and try
134 * - To return multiple errors from an ioctl which makes on-disk
135 * changes. In this case, smush_outnvlist should be true.
136 * Ioctls which make on-disk modifications should generally not
137 * use the outnvl if they succeed, because the caller can not
138 * distinguish between the operation failing, and
139 * deserialization failing.
142 #include <sys/types.h>
143 #include <sys/param.h>
144 #include <sys/errno.h>
147 #include <sys/modctl.h>
148 #include <sys/open.h>
149 #include <sys/file.h>
150 #include <sys/kmem.h>
151 #include <sys/conf.h>
152 #include <sys/cmn_err.h>
153 #include <sys/stat.h>
154 #include <sys/zfs_ioctl.h>
155 #include <sys/zfs_vfsops.h>
156 #include <sys/zfs_znode.h>
159 #include <sys/spa_impl.h>
160 #include <sys/vdev.h>
161 #include <sys/vdev_impl.h>
162 #include <sys/priv_impl.h>
164 #include <sys/dsl_dir.h>
165 #include <sys/dsl_dataset.h>
166 #include <sys/dsl_prop.h>
167 #include <sys/dsl_deleg.h>
168 #include <sys/dmu_objset.h>
169 #include <sys/dmu_impl.h>
170 #include <sys/dmu_tx.h>
172 #include <sys/sunddi.h>
173 #include <sys/sunldi.h>
174 #include <sys/policy.h>
175 #include <sys/zone.h>
176 #include <sys/nvpair.h>
177 #include <sys/pathname.h>
178 #include <sys/mount.h>
180 #include <sys/fs/zfs.h>
181 #include <sys/zfs_ctldir.h>
182 #include <sys/zfs_dir.h>
183 #include <sys/zfs_onexit.h>
184 #include <sys/zvol.h>
185 #include <sys/dsl_scan.h>
186 #include <sharefs/share.h>
187 #include <sys/fm/util.h>
189 #include <sys/dmu_send.h>
190 #include <sys/dsl_destroy.h>
191 #include <sys/dsl_bookmark.h>
192 #include <sys/dsl_userhold.h>
193 #include <sys/zfeature.h>
194 #include <sys/zio_checksum.h>
196 #include <linux/miscdevice.h>
197 #include <linux/slab.h>
199 #include "zfs_namecheck.h"
200 #include "zfs_prop.h"
201 #include "zfs_deleg.h"
202 #include "zfs_comutil.h"
205 * Limit maximum nvlist size. We don't want users passing in insane values
206 * for zc->zc_nvlist_src_size, since we will need to allocate that much memory.
208 #define MAX_NVLIST_SRC_SIZE KMALLOC_MAX_SIZE
210 kmutex_t zfsdev_state_lock
;
211 zfsdev_state_t
*zfsdev_state_list
;
213 extern void zfs_init(void);
214 extern void zfs_fini(void);
216 uint_t zfs_fsyncer_key
;
217 extern uint_t rrw_tsd_key
;
218 static uint_t zfs_allow_log_key
;
220 typedef int zfs_ioc_legacy_func_t(zfs_cmd_t
*);
221 typedef int zfs_ioc_func_t(const char *, nvlist_t
*, nvlist_t
*);
222 typedef int zfs_secpolicy_func_t(zfs_cmd_t
*, nvlist_t
*, cred_t
*);
228 } zfs_ioc_namecheck_t
;
231 POOL_CHECK_NONE
= 1 << 0,
232 POOL_CHECK_SUSPENDED
= 1 << 1,
233 POOL_CHECK_READONLY
= 1 << 2,
234 } zfs_ioc_poolcheck_t
;
236 typedef struct zfs_ioc_vec
{
237 zfs_ioc_legacy_func_t
*zvec_legacy_func
;
238 zfs_ioc_func_t
*zvec_func
;
239 zfs_secpolicy_func_t
*zvec_secpolicy
;
240 zfs_ioc_namecheck_t zvec_namecheck
;
241 boolean_t zvec_allow_log
;
242 zfs_ioc_poolcheck_t zvec_pool_check
;
243 boolean_t zvec_smush_outnvlist
;
244 const char *zvec_name
;
247 /* This array is indexed by zfs_userquota_prop_t */
248 static const char *userquota_perms
[] = {
249 ZFS_DELEG_PERM_USERUSED
,
250 ZFS_DELEG_PERM_USERQUOTA
,
251 ZFS_DELEG_PERM_GROUPUSED
,
252 ZFS_DELEG_PERM_GROUPQUOTA
,
253 ZFS_DELEG_PERM_USEROBJUSED
,
254 ZFS_DELEG_PERM_USEROBJQUOTA
,
255 ZFS_DELEG_PERM_GROUPOBJUSED
,
256 ZFS_DELEG_PERM_GROUPOBJQUOTA
,
259 static int zfs_ioc_userspace_upgrade(zfs_cmd_t
*zc
);
260 static int zfs_ioc_userobjspace_upgrade(zfs_cmd_t
*zc
);
261 static int zfs_check_settable(const char *name
, nvpair_t
*property
,
263 static int zfs_check_clearable(char *dataset
, nvlist_t
*props
,
265 static int zfs_fill_zplprops_root(uint64_t, nvlist_t
*, nvlist_t
*,
267 int zfs_set_prop_nvlist(const char *, zprop_source_t
, nvlist_t
*, nvlist_t
*);
268 static int get_nvlist(uint64_t nvl
, uint64_t size
, int iflag
, nvlist_t
**nvp
);
271 history_str_free(char *buf
)
273 kmem_free(buf
, HIS_MAX_RECORD_LEN
);
277 history_str_get(zfs_cmd_t
*zc
)
281 if (zc
->zc_history
== 0)
284 buf
= kmem_alloc(HIS_MAX_RECORD_LEN
, KM_SLEEP
);
285 if (copyinstr((void *)(uintptr_t)zc
->zc_history
,
286 buf
, HIS_MAX_RECORD_LEN
, NULL
) != 0) {
287 history_str_free(buf
);
291 buf
[HIS_MAX_RECORD_LEN
-1] = '\0';
297 * Check to see if the named dataset is currently defined as bootable
300 zfs_is_bootfs(const char *name
)
304 if (dmu_objset_hold(name
, FTAG
, &os
) == 0) {
306 ret
= (dmu_objset_id(os
) == spa_bootfs(dmu_objset_spa(os
)));
307 dmu_objset_rele(os
, FTAG
);
314 * Return non-zero if the spa version is less than requested version.
317 zfs_earlier_version(const char *name
, int version
)
321 if (spa_open(name
, &spa
, FTAG
) == 0) {
322 if (spa_version(spa
) < version
) {
323 spa_close(spa
, FTAG
);
326 spa_close(spa
, FTAG
);
332 * Return TRUE if the ZPL version is less than requested version.
335 zpl_earlier_version(const char *name
, int version
)
338 boolean_t rc
= B_TRUE
;
340 if (dmu_objset_hold(name
, FTAG
, &os
) == 0) {
343 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
344 dmu_objset_rele(os
, FTAG
);
347 /* XXX reading from non-owned objset */
348 if (zfs_get_zplprop(os
, ZFS_PROP_VERSION
, &zplversion
) == 0)
349 rc
= zplversion
< version
;
350 dmu_objset_rele(os
, FTAG
);
356 zfs_log_history(zfs_cmd_t
*zc
)
361 if ((buf
= history_str_get(zc
)) == NULL
)
364 if (spa_open(zc
->zc_name
, &spa
, FTAG
) == 0) {
365 if (spa_version(spa
) >= SPA_VERSION_ZPOOL_HISTORY
)
366 (void) spa_history_log(spa
, buf
);
367 spa_close(spa
, FTAG
);
369 history_str_free(buf
);
373 * Policy for top-level read operations (list pools). Requires no privileges,
374 * and can be used in the local zone, as there is no associated dataset.
378 zfs_secpolicy_none(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
384 * Policy for dataset read operations (list children, get statistics). Requires
385 * no privileges, but must be visible in the local zone.
389 zfs_secpolicy_read(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
391 if (INGLOBALZONE(curproc
) ||
392 zone_dataset_visible(zc
->zc_name
, NULL
))
395 return (SET_ERROR(ENOENT
));
399 zfs_dozonecheck_impl(const char *dataset
, uint64_t zoned
, cred_t
*cr
)
404 * The dataset must be visible by this zone -- check this first
405 * so they don't see EPERM on something they shouldn't know about.
407 if (!INGLOBALZONE(curproc
) &&
408 !zone_dataset_visible(dataset
, &writable
))
409 return (SET_ERROR(ENOENT
));
411 if (INGLOBALZONE(curproc
)) {
413 * If the fs is zoned, only root can access it from the
416 if (secpolicy_zfs(cr
) && zoned
)
417 return (SET_ERROR(EPERM
));
420 * If we are in a local zone, the 'zoned' property must be set.
423 return (SET_ERROR(EPERM
));
425 /* must be writable by this zone */
427 return (SET_ERROR(EPERM
));
433 zfs_dozonecheck(const char *dataset
, cred_t
*cr
)
437 if (dsl_prop_get_integer(dataset
, "zoned", &zoned
, NULL
))
438 return (SET_ERROR(ENOENT
));
440 return (zfs_dozonecheck_impl(dataset
, zoned
, cr
));
444 zfs_dozonecheck_ds(const char *dataset
, dsl_dataset_t
*ds
, cred_t
*cr
)
448 if (dsl_prop_get_int_ds(ds
, "zoned", &zoned
))
449 return (SET_ERROR(ENOENT
));
451 return (zfs_dozonecheck_impl(dataset
, zoned
, cr
));
455 zfs_secpolicy_write_perms_ds(const char *name
, dsl_dataset_t
*ds
,
456 const char *perm
, cred_t
*cr
)
460 error
= zfs_dozonecheck_ds(name
, ds
, cr
);
462 error
= secpolicy_zfs(cr
);
464 error
= dsl_deleg_access_impl(ds
, perm
, cr
);
470 zfs_secpolicy_write_perms(const char *name
, const char *perm
, cred_t
*cr
)
477 * First do a quick check for root in the global zone, which
478 * is allowed to do all write_perms. This ensures that zfs_ioc_*
479 * will get to handle nonexistent datasets.
481 if (INGLOBALZONE(curproc
) && secpolicy_zfs(cr
) == 0)
484 error
= dsl_pool_hold(name
, FTAG
, &dp
);
488 error
= dsl_dataset_hold(dp
, name
, FTAG
, &ds
);
490 dsl_pool_rele(dp
, FTAG
);
494 error
= zfs_secpolicy_write_perms_ds(name
, ds
, perm
, cr
);
496 dsl_dataset_rele(ds
, FTAG
);
497 dsl_pool_rele(dp
, FTAG
);
502 * Policy for setting the security label property.
504 * Returns 0 for success, non-zero for access and other errors.
507 zfs_set_slabel_policy(const char *name
, char *strval
, cred_t
*cr
)
510 char ds_hexsl
[MAXNAMELEN
];
511 bslabel_t ds_sl
, new_sl
;
512 boolean_t new_default
= FALSE
;
514 int needed_priv
= -1;
517 /* First get the existing dataset label. */
518 error
= dsl_prop_get(name
, zfs_prop_to_name(ZFS_PROP_MLSLABEL
),
519 1, sizeof (ds_hexsl
), &ds_hexsl
, NULL
);
521 return (SET_ERROR(EPERM
));
523 if (strcasecmp(strval
, ZFS_MLSLABEL_DEFAULT
) == 0)
526 /* The label must be translatable */
527 if (!new_default
&& (hexstr_to_label(strval
, &new_sl
) != 0))
528 return (SET_ERROR(EINVAL
));
531 * In a non-global zone, disallow attempts to set a label that
532 * doesn't match that of the zone; otherwise no other checks
535 if (!INGLOBALZONE(curproc
)) {
536 if (new_default
|| !blequal(&new_sl
, CR_SL(CRED())))
537 return (SET_ERROR(EPERM
));
542 * For global-zone datasets (i.e., those whose zoned property is
543 * "off", verify that the specified new label is valid for the
546 if (dsl_prop_get_integer(name
,
547 zfs_prop_to_name(ZFS_PROP_ZONED
), &zoned
, NULL
))
548 return (SET_ERROR(EPERM
));
550 if (zfs_check_global_label(name
, strval
) != 0)
551 return (SET_ERROR(EPERM
));
555 * If the existing dataset label is nondefault, check if the
556 * dataset is mounted (label cannot be changed while mounted).
557 * Get the zfsvfs_t; if there isn't one, then the dataset isn't
558 * mounted (or isn't a dataset, doesn't exist, ...).
560 if (strcasecmp(ds_hexsl
, ZFS_MLSLABEL_DEFAULT
) != 0) {
562 static char *setsl_tag
= "setsl_tag";
565 * Try to own the dataset; abort if there is any error,
566 * (e.g., already mounted, in use, or other error).
568 error
= dmu_objset_own(name
, DMU_OST_ZFS
, B_TRUE
,
571 return (SET_ERROR(EPERM
));
573 dmu_objset_disown(os
, setsl_tag
);
576 needed_priv
= PRIV_FILE_DOWNGRADE_SL
;
580 if (hexstr_to_label(strval
, &new_sl
) != 0)
581 return (SET_ERROR(EPERM
));
583 if (blstrictdom(&ds_sl
, &new_sl
))
584 needed_priv
= PRIV_FILE_DOWNGRADE_SL
;
585 else if (blstrictdom(&new_sl
, &ds_sl
))
586 needed_priv
= PRIV_FILE_UPGRADE_SL
;
588 /* dataset currently has a default label */
590 needed_priv
= PRIV_FILE_UPGRADE_SL
;
594 if (needed_priv
!= -1)
595 return (PRIV_POLICY(cr
, needed_priv
, B_FALSE
, EPERM
, NULL
));
599 #endif /* HAVE_MLSLABEL */
603 zfs_secpolicy_setprop(const char *dsname
, zfs_prop_t prop
, nvpair_t
*propval
,
609 * Check permissions for special properties.
616 * Disallow setting of 'zoned' from within a local zone.
618 if (!INGLOBALZONE(curproc
))
619 return (SET_ERROR(EPERM
));
623 case ZFS_PROP_FILESYSTEM_LIMIT
:
624 case ZFS_PROP_SNAPSHOT_LIMIT
:
625 if (!INGLOBALZONE(curproc
)) {
627 char setpoint
[ZFS_MAX_DATASET_NAME_LEN
];
629 * Unprivileged users are allowed to modify the
630 * limit on things *under* (ie. contained by)
631 * the thing they own.
633 if (dsl_prop_get_integer(dsname
, "zoned", &zoned
,
635 return (SET_ERROR(EPERM
));
636 if (!zoned
|| strlen(dsname
) <= strlen(setpoint
))
637 return (SET_ERROR(EPERM
));
641 case ZFS_PROP_MLSLABEL
:
642 if (!is_system_labeled())
643 return (SET_ERROR(EPERM
));
645 if (nvpair_value_string(propval
, &strval
) == 0) {
648 err
= zfs_set_slabel_policy(dsname
, strval
, CRED());
655 return (zfs_secpolicy_write_perms(dsname
, zfs_prop_to_name(prop
), cr
));
660 zfs_secpolicy_set_fsacl(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
664 error
= zfs_dozonecheck(zc
->zc_name
, cr
);
669 * permission to set permissions will be evaluated later in
670 * dsl_deleg_can_allow()
677 zfs_secpolicy_rollback(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
679 return (zfs_secpolicy_write_perms(zc
->zc_name
,
680 ZFS_DELEG_PERM_ROLLBACK
, cr
));
685 zfs_secpolicy_send(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
693 * Generate the current snapshot name from the given objsetid, then
694 * use that name for the secpolicy/zone checks.
696 cp
= strchr(zc
->zc_name
, '@');
698 return (SET_ERROR(EINVAL
));
699 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
703 error
= dsl_dataset_hold_obj(dp
, zc
->zc_sendobj
, FTAG
, &ds
);
705 dsl_pool_rele(dp
, FTAG
);
709 dsl_dataset_name(ds
, zc
->zc_name
);
711 error
= zfs_secpolicy_write_perms_ds(zc
->zc_name
, ds
,
712 ZFS_DELEG_PERM_SEND
, cr
);
713 dsl_dataset_rele(ds
, FTAG
);
714 dsl_pool_rele(dp
, FTAG
);
721 zfs_secpolicy_send_new(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
723 return (zfs_secpolicy_write_perms(zc
->zc_name
,
724 ZFS_DELEG_PERM_SEND
, cr
));
727 #ifdef HAVE_SMB_SHARE
730 zfs_secpolicy_deleg_share(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
735 if ((error
= lookupname(zc
->zc_value
, UIO_SYSSPACE
,
736 NO_FOLLOW
, NULL
, &vp
)) != 0)
739 /* Now make sure mntpnt and dataset are ZFS */
741 if (vp
->v_vfsp
->vfs_fstype
!= zfsfstype
||
742 (strcmp((char *)refstr_value(vp
->v_vfsp
->vfs_resource
),
743 zc
->zc_name
) != 0)) {
745 return (SET_ERROR(EPERM
));
749 return (dsl_deleg_access(zc
->zc_name
,
750 ZFS_DELEG_PERM_SHARE
, cr
));
752 #endif /* HAVE_SMB_SHARE */
755 zfs_secpolicy_share(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
757 #ifdef HAVE_SMB_SHARE
758 if (!INGLOBALZONE(curproc
))
759 return (SET_ERROR(EPERM
));
761 if (secpolicy_nfs(cr
) == 0) {
764 return (zfs_secpolicy_deleg_share(zc
, innvl
, cr
));
767 return (SET_ERROR(ENOTSUP
));
768 #endif /* HAVE_SMB_SHARE */
772 zfs_secpolicy_smb_acl(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
774 #ifdef HAVE_SMB_SHARE
775 if (!INGLOBALZONE(curproc
))
776 return (SET_ERROR(EPERM
));
778 if (secpolicy_smb(cr
) == 0) {
781 return (zfs_secpolicy_deleg_share(zc
, innvl
, cr
));
784 return (SET_ERROR(ENOTSUP
));
785 #endif /* HAVE_SMB_SHARE */
789 zfs_get_parent(const char *datasetname
, char *parent
, int parentsize
)
794 * Remove the @bla or /bla from the end of the name to get the parent.
796 (void) strncpy(parent
, datasetname
, parentsize
);
797 cp
= strrchr(parent
, '@');
801 cp
= strrchr(parent
, '/');
803 return (SET_ERROR(ENOENT
));
811 zfs_secpolicy_destroy_perms(const char *name
, cred_t
*cr
)
815 if ((error
= zfs_secpolicy_write_perms(name
,
816 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
819 return (zfs_secpolicy_write_perms(name
, ZFS_DELEG_PERM_DESTROY
, cr
));
824 zfs_secpolicy_destroy(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
826 return (zfs_secpolicy_destroy_perms(zc
->zc_name
, cr
));
830 * Destroying snapshots with delegated permissions requires
831 * descendant mount and destroy permissions.
835 zfs_secpolicy_destroy_snaps(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
838 nvpair_t
*pair
, *nextpair
;
841 if (nvlist_lookup_nvlist(innvl
, "snaps", &snaps
) != 0)
842 return (SET_ERROR(EINVAL
));
843 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
845 nextpair
= nvlist_next_nvpair(snaps
, pair
);
846 error
= zfs_secpolicy_destroy_perms(nvpair_name(pair
), cr
);
847 if (error
== ENOENT
) {
849 * Ignore any snapshots that don't exist (we consider
850 * them "already destroyed"). Remove the name from the
851 * nvl here in case the snapshot is created between
852 * now and when we try to destroy it (in which case
853 * we don't want to destroy it since we haven't
854 * checked for permission).
856 fnvlist_remove_nvpair(snaps
, pair
);
867 zfs_secpolicy_rename_perms(const char *from
, const char *to
, cred_t
*cr
)
869 char parentname
[ZFS_MAX_DATASET_NAME_LEN
];
872 if ((error
= zfs_secpolicy_write_perms(from
,
873 ZFS_DELEG_PERM_RENAME
, cr
)) != 0)
876 if ((error
= zfs_secpolicy_write_perms(from
,
877 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
880 if ((error
= zfs_get_parent(to
, parentname
,
881 sizeof (parentname
))) != 0)
884 if ((error
= zfs_secpolicy_write_perms(parentname
,
885 ZFS_DELEG_PERM_CREATE
, cr
)) != 0)
888 if ((error
= zfs_secpolicy_write_perms(parentname
,
889 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
897 zfs_secpolicy_rename(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
899 return (zfs_secpolicy_rename_perms(zc
->zc_name
, zc
->zc_value
, cr
));
904 zfs_secpolicy_promote(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
907 dsl_dataset_t
*clone
;
910 error
= zfs_secpolicy_write_perms(zc
->zc_name
,
911 ZFS_DELEG_PERM_PROMOTE
, cr
);
915 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
919 error
= dsl_dataset_hold(dp
, zc
->zc_name
, FTAG
, &clone
);
922 char parentname
[ZFS_MAX_DATASET_NAME_LEN
];
923 dsl_dataset_t
*origin
= NULL
;
927 error
= dsl_dataset_hold_obj(dd
->dd_pool
,
928 dsl_dir_phys(dd
)->dd_origin_obj
, FTAG
, &origin
);
930 dsl_dataset_rele(clone
, FTAG
);
931 dsl_pool_rele(dp
, FTAG
);
935 error
= zfs_secpolicy_write_perms_ds(zc
->zc_name
, clone
,
936 ZFS_DELEG_PERM_MOUNT
, cr
);
938 dsl_dataset_name(origin
, parentname
);
940 error
= zfs_secpolicy_write_perms_ds(parentname
, origin
,
941 ZFS_DELEG_PERM_PROMOTE
, cr
);
943 dsl_dataset_rele(clone
, FTAG
);
944 dsl_dataset_rele(origin
, FTAG
);
946 dsl_pool_rele(dp
, FTAG
);
952 zfs_secpolicy_recv(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
956 if ((error
= zfs_secpolicy_write_perms(zc
->zc_name
,
957 ZFS_DELEG_PERM_RECEIVE
, cr
)) != 0)
960 if ((error
= zfs_secpolicy_write_perms(zc
->zc_name
,
961 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
964 return (zfs_secpolicy_write_perms(zc
->zc_name
,
965 ZFS_DELEG_PERM_CREATE
, cr
));
970 zfs_secpolicy_recv_new(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
972 return (zfs_secpolicy_recv(zc
, innvl
, cr
));
976 zfs_secpolicy_snapshot_perms(const char *name
, cred_t
*cr
)
978 return (zfs_secpolicy_write_perms(name
,
979 ZFS_DELEG_PERM_SNAPSHOT
, cr
));
983 * Check for permission to create each snapshot in the nvlist.
987 zfs_secpolicy_snapshot(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
993 if (nvlist_lookup_nvlist(innvl
, "snaps", &snaps
) != 0)
994 return (SET_ERROR(EINVAL
));
995 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
996 pair
= nvlist_next_nvpair(snaps
, pair
)) {
997 char *name
= nvpair_name(pair
);
998 char *atp
= strchr(name
, '@');
1001 error
= SET_ERROR(EINVAL
);
1005 error
= zfs_secpolicy_snapshot_perms(name
, cr
);
1014 * Check for permission to create each snapshot in the nvlist.
1018 zfs_secpolicy_bookmark(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1023 for (pair
= nvlist_next_nvpair(innvl
, NULL
);
1024 pair
!= NULL
; pair
= nvlist_next_nvpair(innvl
, pair
)) {
1025 char *name
= nvpair_name(pair
);
1026 char *hashp
= strchr(name
, '#');
1028 if (hashp
== NULL
) {
1029 error
= SET_ERROR(EINVAL
);
1033 error
= zfs_secpolicy_write_perms(name
,
1034 ZFS_DELEG_PERM_BOOKMARK
, cr
);
1044 zfs_secpolicy_destroy_bookmarks(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1046 nvpair_t
*pair
, *nextpair
;
1049 for (pair
= nvlist_next_nvpair(innvl
, NULL
); pair
!= NULL
;
1051 char *name
= nvpair_name(pair
);
1052 char *hashp
= strchr(name
, '#');
1053 nextpair
= nvlist_next_nvpair(innvl
, pair
);
1055 if (hashp
== NULL
) {
1056 error
= SET_ERROR(EINVAL
);
1061 error
= zfs_secpolicy_write_perms(name
,
1062 ZFS_DELEG_PERM_DESTROY
, cr
);
1064 if (error
== ENOENT
) {
1066 * Ignore any filesystems that don't exist (we consider
1067 * their bookmarks "already destroyed"). Remove
1068 * the name from the nvl here in case the filesystem
1069 * is created between now and when we try to destroy
1070 * the bookmark (in which case we don't want to
1071 * destroy it since we haven't checked for permission).
1073 fnvlist_remove_nvpair(innvl
, pair
);
1085 zfs_secpolicy_log_history(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1088 * Even root must have a proper TSD so that we know what pool
1091 if (tsd_get(zfs_allow_log_key
) == NULL
)
1092 return (SET_ERROR(EPERM
));
1097 zfs_secpolicy_create_clone(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1099 char parentname
[ZFS_MAX_DATASET_NAME_LEN
];
1103 if ((error
= zfs_get_parent(zc
->zc_name
, parentname
,
1104 sizeof (parentname
))) != 0)
1107 if (nvlist_lookup_string(innvl
, "origin", &origin
) == 0 &&
1108 (error
= zfs_secpolicy_write_perms(origin
,
1109 ZFS_DELEG_PERM_CLONE
, cr
)) != 0)
1112 if ((error
= zfs_secpolicy_write_perms(parentname
,
1113 ZFS_DELEG_PERM_CREATE
, cr
)) != 0)
1116 return (zfs_secpolicy_write_perms(parentname
,
1117 ZFS_DELEG_PERM_MOUNT
, cr
));
1121 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
1122 * SYS_CONFIG privilege, which is not available in a local zone.
1126 zfs_secpolicy_config(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1128 if (secpolicy_sys_config(cr
, B_FALSE
) != 0)
1129 return (SET_ERROR(EPERM
));
1135 * Policy for object to name lookups.
1139 zfs_secpolicy_diff(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1143 if ((error
= secpolicy_sys_config(cr
, B_FALSE
)) == 0)
1146 error
= zfs_secpolicy_write_perms(zc
->zc_name
, ZFS_DELEG_PERM_DIFF
, cr
);
1151 * Policy for fault injection. Requires all privileges.
1155 zfs_secpolicy_inject(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1157 return (secpolicy_zinject(cr
));
1162 zfs_secpolicy_inherit_prop(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1164 zfs_prop_t prop
= zfs_name_to_prop(zc
->zc_value
);
1166 if (prop
== ZPROP_INVAL
) {
1167 if (!zfs_prop_user(zc
->zc_value
))
1168 return (SET_ERROR(EINVAL
));
1169 return (zfs_secpolicy_write_perms(zc
->zc_name
,
1170 ZFS_DELEG_PERM_USERPROP
, cr
));
1172 return (zfs_secpolicy_setprop(zc
->zc_name
, prop
,
1178 zfs_secpolicy_userspace_one(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1180 int err
= zfs_secpolicy_read(zc
, innvl
, cr
);
1184 if (zc
->zc_objset_type
>= ZFS_NUM_USERQUOTA_PROPS
)
1185 return (SET_ERROR(EINVAL
));
1187 if (zc
->zc_value
[0] == 0) {
1189 * They are asking about a posix uid/gid. If it's
1190 * themself, allow it.
1192 if (zc
->zc_objset_type
== ZFS_PROP_USERUSED
||
1193 zc
->zc_objset_type
== ZFS_PROP_USERQUOTA
||
1194 zc
->zc_objset_type
== ZFS_PROP_USEROBJUSED
||
1195 zc
->zc_objset_type
== ZFS_PROP_USEROBJQUOTA
) {
1196 if (zc
->zc_guid
== crgetuid(cr
))
1199 if (groupmember(zc
->zc_guid
, cr
))
1204 return (zfs_secpolicy_write_perms(zc
->zc_name
,
1205 userquota_perms
[zc
->zc_objset_type
], cr
));
1209 zfs_secpolicy_userspace_many(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1211 int err
= zfs_secpolicy_read(zc
, innvl
, cr
);
1215 if (zc
->zc_objset_type
>= ZFS_NUM_USERQUOTA_PROPS
)
1216 return (SET_ERROR(EINVAL
));
1218 return (zfs_secpolicy_write_perms(zc
->zc_name
,
1219 userquota_perms
[zc
->zc_objset_type
], cr
));
1224 zfs_secpolicy_userspace_upgrade(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1226 return (zfs_secpolicy_setprop(zc
->zc_name
, ZFS_PROP_VERSION
,
1232 zfs_secpolicy_hold(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1238 error
= nvlist_lookup_nvlist(innvl
, "holds", &holds
);
1240 return (SET_ERROR(EINVAL
));
1242 for (pair
= nvlist_next_nvpair(holds
, NULL
); pair
!= NULL
;
1243 pair
= nvlist_next_nvpair(holds
, pair
)) {
1244 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
1245 error
= dmu_fsname(nvpair_name(pair
), fsname
);
1248 error
= zfs_secpolicy_write_perms(fsname
,
1249 ZFS_DELEG_PERM_HOLD
, cr
);
1258 zfs_secpolicy_release(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1263 for (pair
= nvlist_next_nvpair(innvl
, NULL
); pair
!= NULL
;
1264 pair
= nvlist_next_nvpair(innvl
, pair
)) {
1265 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
1266 error
= dmu_fsname(nvpair_name(pair
), fsname
);
1269 error
= zfs_secpolicy_write_perms(fsname
,
1270 ZFS_DELEG_PERM_RELEASE
, cr
);
1278 * Policy for allowing temporary snapshots to be taken or released
1281 zfs_secpolicy_tmp_snapshot(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1284 * A temporary snapshot is the same as a snapshot,
1285 * hold, destroy and release all rolled into one.
1286 * Delegated diff alone is sufficient that we allow this.
1290 if ((error
= zfs_secpolicy_write_perms(zc
->zc_name
,
1291 ZFS_DELEG_PERM_DIFF
, cr
)) == 0)
1294 error
= zfs_secpolicy_snapshot_perms(zc
->zc_name
, cr
);
1296 error
= zfs_secpolicy_hold(zc
, innvl
, cr
);
1298 error
= zfs_secpolicy_release(zc
, innvl
, cr
);
1300 error
= zfs_secpolicy_destroy(zc
, innvl
, cr
);
1305 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1308 get_nvlist(uint64_t nvl
, uint64_t size
, int iflag
, nvlist_t
**nvp
)
1312 nvlist_t
*list
= NULL
;
1315 * Read in and unpack the user-supplied nvlist.
1318 return (SET_ERROR(EINVAL
));
1320 packed
= vmem_alloc(size
, KM_SLEEP
);
1322 if ((error
= ddi_copyin((void *)(uintptr_t)nvl
, packed
, size
,
1324 vmem_free(packed
, size
);
1325 return (SET_ERROR(EFAULT
));
1328 if ((error
= nvlist_unpack(packed
, size
, &list
, 0)) != 0) {
1329 vmem_free(packed
, size
);
1333 vmem_free(packed
, size
);
1340 * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
1341 * Entries will be removed from the end of the nvlist, and one int32 entry
1342 * named "N_MORE_ERRORS" will be added indicating how many entries were
1346 nvlist_smush(nvlist_t
*errors
, size_t max
)
1350 size
= fnvlist_size(errors
);
1353 nvpair_t
*more_errors
;
1357 return (SET_ERROR(ENOMEM
));
1359 fnvlist_add_int32(errors
, ZPROP_N_MORE_ERRORS
, 0);
1360 more_errors
= nvlist_prev_nvpair(errors
, NULL
);
1363 nvpair_t
*pair
= nvlist_prev_nvpair(errors
,
1365 fnvlist_remove_nvpair(errors
, pair
);
1367 size
= fnvlist_size(errors
);
1368 } while (size
> max
);
1370 fnvlist_remove_nvpair(errors
, more_errors
);
1371 fnvlist_add_int32(errors
, ZPROP_N_MORE_ERRORS
, n
);
1372 ASSERT3U(fnvlist_size(errors
), <=, max
);
1379 put_nvlist(zfs_cmd_t
*zc
, nvlist_t
*nvl
)
1381 char *packed
= NULL
;
1385 size
= fnvlist_size(nvl
);
1387 if (size
> zc
->zc_nvlist_dst_size
) {
1388 error
= SET_ERROR(ENOMEM
);
1390 packed
= fnvlist_pack(nvl
, &size
);
1391 if (ddi_copyout(packed
, (void *)(uintptr_t)zc
->zc_nvlist_dst
,
1392 size
, zc
->zc_iflags
) != 0)
1393 error
= SET_ERROR(EFAULT
);
1394 fnvlist_pack_free(packed
, size
);
1397 zc
->zc_nvlist_dst_size
= size
;
1398 zc
->zc_nvlist_dst_filled
= B_TRUE
;
1403 getzfsvfs(const char *dsname
, zfsvfs_t
**zfvp
)
1408 error
= dmu_objset_hold(dsname
, FTAG
, &os
);
1411 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
1412 dmu_objset_rele(os
, FTAG
);
1413 return (SET_ERROR(EINVAL
));
1416 mutex_enter(&os
->os_user_ptr_lock
);
1417 *zfvp
= dmu_objset_get_user(os
);
1418 /* bump s_active only when non-zero to prevent umount race */
1419 if (*zfvp
== NULL
|| (*zfvp
)->z_sb
== NULL
||
1420 !atomic_inc_not_zero(&((*zfvp
)->z_sb
->s_active
))) {
1421 error
= SET_ERROR(ESRCH
);
1423 mutex_exit(&os
->os_user_ptr_lock
);
1424 dmu_objset_rele(os
, FTAG
);
1429 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
1430 * case its z_sb will be NULL, and it will be opened as the owner.
1431 * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1432 * which prevents all inode ops from running.
1435 zfsvfs_hold(const char *name
, void *tag
, zfsvfs_t
**zfvp
, boolean_t writer
)
1439 if (getzfsvfs(name
, zfvp
) != 0)
1440 error
= zfsvfs_create(name
, zfvp
);
1442 rrm_enter(&(*zfvp
)->z_teardown_lock
, (writer
) ? RW_WRITER
:
1444 if ((*zfvp
)->z_unmounted
) {
1446 * XXX we could probably try again, since the unmounting
1447 * thread should be just about to disassociate the
1448 * objset from the zfsvfs.
1450 rrm_exit(&(*zfvp
)->z_teardown_lock
, tag
);
1451 return (SET_ERROR(EBUSY
));
1458 zfsvfs_rele(zfsvfs_t
*zfsvfs
, void *tag
)
1460 rrm_exit(&zfsvfs
->z_teardown_lock
, tag
);
1463 deactivate_super(zfsvfs
->z_sb
);
1465 dmu_objset_disown(zfsvfs
->z_os
, zfsvfs
);
1466 zfsvfs_free(zfsvfs
);
1471 zfs_ioc_pool_create(zfs_cmd_t
*zc
)
1474 nvlist_t
*config
, *props
= NULL
;
1475 nvlist_t
*rootprops
= NULL
;
1476 nvlist_t
*zplprops
= NULL
;
1478 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1479 zc
->zc_iflags
, &config
)))
1482 if (zc
->zc_nvlist_src_size
!= 0 && (error
=
1483 get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1484 zc
->zc_iflags
, &props
))) {
1485 nvlist_free(config
);
1490 nvlist_t
*nvl
= NULL
;
1491 uint64_t version
= SPA_VERSION
;
1493 (void) nvlist_lookup_uint64(props
,
1494 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
);
1495 if (!SPA_VERSION_IS_SUPPORTED(version
)) {
1496 error
= SET_ERROR(EINVAL
);
1497 goto pool_props_bad
;
1499 (void) nvlist_lookup_nvlist(props
, ZPOOL_ROOTFS_PROPS
, &nvl
);
1501 error
= nvlist_dup(nvl
, &rootprops
, KM_SLEEP
);
1503 nvlist_free(config
);
1507 (void) nvlist_remove_all(props
, ZPOOL_ROOTFS_PROPS
);
1509 VERIFY(nvlist_alloc(&zplprops
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1510 error
= zfs_fill_zplprops_root(version
, rootprops
,
1513 goto pool_props_bad
;
1516 error
= spa_create(zc
->zc_name
, config
, props
, zplprops
);
1519 * Set the remaining root properties
1521 if (!error
&& (error
= zfs_set_prop_nvlist(zc
->zc_name
,
1522 ZPROP_SRC_LOCAL
, rootprops
, NULL
)) != 0)
1523 (void) spa_destroy(zc
->zc_name
);
1526 nvlist_free(rootprops
);
1527 nvlist_free(zplprops
);
1528 nvlist_free(config
);
1535 zfs_ioc_pool_destroy(zfs_cmd_t
*zc
)
1538 zfs_log_history(zc
);
1539 error
= spa_destroy(zc
->zc_name
);
1545 zfs_ioc_pool_import(zfs_cmd_t
*zc
)
1547 nvlist_t
*config
, *props
= NULL
;
1551 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1552 zc
->zc_iflags
, &config
)) != 0)
1555 if (zc
->zc_nvlist_src_size
!= 0 && (error
=
1556 get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1557 zc
->zc_iflags
, &props
))) {
1558 nvlist_free(config
);
1562 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &guid
) != 0 ||
1563 guid
!= zc
->zc_guid
)
1564 error
= SET_ERROR(EINVAL
);
1566 error
= spa_import(zc
->zc_name
, config
, props
, zc
->zc_cookie
);
1568 if (zc
->zc_nvlist_dst
!= 0) {
1571 if ((err
= put_nvlist(zc
, config
)) != 0)
1575 nvlist_free(config
);
1582 zfs_ioc_pool_export(zfs_cmd_t
*zc
)
1585 boolean_t force
= (boolean_t
)zc
->zc_cookie
;
1586 boolean_t hardforce
= (boolean_t
)zc
->zc_guid
;
1588 zfs_log_history(zc
);
1589 error
= spa_export(zc
->zc_name
, NULL
, force
, hardforce
);
1595 zfs_ioc_pool_configs(zfs_cmd_t
*zc
)
1600 if ((configs
= spa_all_configs(&zc
->zc_cookie
)) == NULL
)
1601 return (SET_ERROR(EEXIST
));
1603 error
= put_nvlist(zc
, configs
);
1605 nvlist_free(configs
);
1612 * zc_name name of the pool
1615 * zc_cookie real errno
1616 * zc_nvlist_dst config nvlist
1617 * zc_nvlist_dst_size size of config nvlist
1620 zfs_ioc_pool_stats(zfs_cmd_t
*zc
)
1626 error
= spa_get_stats(zc
->zc_name
, &config
, zc
->zc_value
,
1627 sizeof (zc
->zc_value
));
1629 if (config
!= NULL
) {
1630 ret
= put_nvlist(zc
, config
);
1631 nvlist_free(config
);
1634 * The config may be present even if 'error' is non-zero.
1635 * In this case we return success, and preserve the real errno
1638 zc
->zc_cookie
= error
;
1647 * Try to import the given pool, returning pool stats as appropriate so that
1648 * user land knows which devices are available and overall pool health.
1651 zfs_ioc_pool_tryimport(zfs_cmd_t
*zc
)
1653 nvlist_t
*tryconfig
, *config
= NULL
;
1656 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1657 zc
->zc_iflags
, &tryconfig
)) != 0)
1660 config
= spa_tryimport(tryconfig
);
1662 nvlist_free(tryconfig
);
1665 return (SET_ERROR(EINVAL
));
1667 error
= put_nvlist(zc
, config
);
1668 nvlist_free(config
);
1675 * zc_name name of the pool
1676 * zc_cookie scan func (pool_scan_func_t)
1677 * zc_flags scrub pause/resume flag (pool_scrub_cmd_t)
1680 zfs_ioc_pool_scan(zfs_cmd_t
*zc
)
1685 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1688 if (zc
->zc_flags
>= POOL_SCRUB_FLAGS_END
)
1689 return (SET_ERROR(EINVAL
));
1691 if (zc
->zc_flags
== POOL_SCRUB_PAUSE
)
1692 error
= spa_scrub_pause_resume(spa
, POOL_SCRUB_PAUSE
);
1693 else if (zc
->zc_cookie
== POOL_SCAN_NONE
)
1694 error
= spa_scan_stop(spa
);
1696 error
= spa_scan(spa
, zc
->zc_cookie
);
1698 spa_close(spa
, FTAG
);
1704 zfs_ioc_pool_freeze(zfs_cmd_t
*zc
)
1709 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1712 spa_close(spa
, FTAG
);
1718 zfs_ioc_pool_upgrade(zfs_cmd_t
*zc
)
1723 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1726 if (zc
->zc_cookie
< spa_version(spa
) ||
1727 !SPA_VERSION_IS_SUPPORTED(zc
->zc_cookie
)) {
1728 spa_close(spa
, FTAG
);
1729 return (SET_ERROR(EINVAL
));
1732 spa_upgrade(spa
, zc
->zc_cookie
);
1733 spa_close(spa
, FTAG
);
1739 zfs_ioc_pool_get_history(zfs_cmd_t
*zc
)
1746 if ((size
= zc
->zc_history_len
) == 0)
1747 return (SET_ERROR(EINVAL
));
1749 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1752 if (spa_version(spa
) < SPA_VERSION_ZPOOL_HISTORY
) {
1753 spa_close(spa
, FTAG
);
1754 return (SET_ERROR(ENOTSUP
));
1757 hist_buf
= vmem_alloc(size
, KM_SLEEP
);
1758 if ((error
= spa_history_get(spa
, &zc
->zc_history_offset
,
1759 &zc
->zc_history_len
, hist_buf
)) == 0) {
1760 error
= ddi_copyout(hist_buf
,
1761 (void *)(uintptr_t)zc
->zc_history
,
1762 zc
->zc_history_len
, zc
->zc_iflags
);
1765 spa_close(spa
, FTAG
);
1766 vmem_free(hist_buf
, size
);
1771 zfs_ioc_pool_reguid(zfs_cmd_t
*zc
)
1776 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1778 error
= spa_change_guid(spa
);
1779 spa_close(spa
, FTAG
);
1785 zfs_ioc_dsobj_to_dsname(zfs_cmd_t
*zc
)
1787 return (dsl_dsobj_to_dsname(zc
->zc_name
, zc
->zc_obj
, zc
->zc_value
));
1792 * zc_name name of filesystem
1793 * zc_obj object to find
1796 * zc_value name of object
1799 zfs_ioc_obj_to_path(zfs_cmd_t
*zc
)
1804 /* XXX reading from objset not owned */
1805 if ((error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
)) != 0)
1807 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
1808 dmu_objset_rele(os
, FTAG
);
1809 return (SET_ERROR(EINVAL
));
1811 error
= zfs_obj_to_path(os
, zc
->zc_obj
, zc
->zc_value
,
1812 sizeof (zc
->zc_value
));
1813 dmu_objset_rele(os
, FTAG
);
1820 * zc_name name of filesystem
1821 * zc_obj object to find
1824 * zc_stat stats on object
1825 * zc_value path to object
1828 zfs_ioc_obj_to_stats(zfs_cmd_t
*zc
)
1833 /* XXX reading from objset not owned */
1834 if ((error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
)) != 0)
1836 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
1837 dmu_objset_rele(os
, FTAG
);
1838 return (SET_ERROR(EINVAL
));
1840 error
= zfs_obj_to_stats(os
, zc
->zc_obj
, &zc
->zc_stat
, zc
->zc_value
,
1841 sizeof (zc
->zc_value
));
1842 dmu_objset_rele(os
, FTAG
);
1848 zfs_ioc_vdev_add(zfs_cmd_t
*zc
)
1854 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1858 error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1859 zc
->zc_iflags
, &config
);
1861 error
= spa_vdev_add(spa
, config
);
1862 nvlist_free(config
);
1864 spa_close(spa
, FTAG
);
1870 * zc_name name of the pool
1871 * zc_nvlist_conf nvlist of devices to remove
1872 * zc_cookie to stop the remove?
1875 zfs_ioc_vdev_remove(zfs_cmd_t
*zc
)
1880 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1883 error
= spa_vdev_remove(spa
, zc
->zc_guid
, B_FALSE
);
1884 spa_close(spa
, FTAG
);
1889 zfs_ioc_vdev_set_state(zfs_cmd_t
*zc
)
1893 vdev_state_t newstate
= VDEV_STATE_UNKNOWN
;
1895 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1897 switch (zc
->zc_cookie
) {
1898 case VDEV_STATE_ONLINE
:
1899 error
= vdev_online(spa
, zc
->zc_guid
, zc
->zc_obj
, &newstate
);
1902 case VDEV_STATE_OFFLINE
:
1903 error
= vdev_offline(spa
, zc
->zc_guid
, zc
->zc_obj
);
1906 case VDEV_STATE_FAULTED
:
1907 if (zc
->zc_obj
!= VDEV_AUX_ERR_EXCEEDED
&&
1908 zc
->zc_obj
!= VDEV_AUX_EXTERNAL
&&
1909 zc
->zc_obj
!= VDEV_AUX_EXTERNAL_PERSIST
)
1910 zc
->zc_obj
= VDEV_AUX_ERR_EXCEEDED
;
1912 error
= vdev_fault(spa
, zc
->zc_guid
, zc
->zc_obj
);
1915 case VDEV_STATE_DEGRADED
:
1916 if (zc
->zc_obj
!= VDEV_AUX_ERR_EXCEEDED
&&
1917 zc
->zc_obj
!= VDEV_AUX_EXTERNAL
)
1918 zc
->zc_obj
= VDEV_AUX_ERR_EXCEEDED
;
1920 error
= vdev_degrade(spa
, zc
->zc_guid
, zc
->zc_obj
);
1924 error
= SET_ERROR(EINVAL
);
1926 zc
->zc_cookie
= newstate
;
1927 spa_close(spa
, FTAG
);
1932 zfs_ioc_vdev_attach(zfs_cmd_t
*zc
)
1935 int replacing
= zc
->zc_cookie
;
1939 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1942 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1943 zc
->zc_iflags
, &config
)) == 0) {
1944 error
= spa_vdev_attach(spa
, zc
->zc_guid
, config
, replacing
);
1945 nvlist_free(config
);
1948 spa_close(spa
, FTAG
);
1953 zfs_ioc_vdev_detach(zfs_cmd_t
*zc
)
1958 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1961 error
= spa_vdev_detach(spa
, zc
->zc_guid
, 0, B_FALSE
);
1963 spa_close(spa
, FTAG
);
1968 zfs_ioc_vdev_split(zfs_cmd_t
*zc
)
1971 nvlist_t
*config
, *props
= NULL
;
1973 boolean_t exp
= !!(zc
->zc_cookie
& ZPOOL_EXPORT_AFTER_SPLIT
);
1975 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1978 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1979 zc
->zc_iflags
, &config
))) {
1980 spa_close(spa
, FTAG
);
1984 if (zc
->zc_nvlist_src_size
!= 0 && (error
=
1985 get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1986 zc
->zc_iflags
, &props
))) {
1987 spa_close(spa
, FTAG
);
1988 nvlist_free(config
);
1992 error
= spa_vdev_split_mirror(spa
, zc
->zc_string
, config
, props
, exp
);
1994 spa_close(spa
, FTAG
);
1996 nvlist_free(config
);
2003 zfs_ioc_vdev_setpath(zfs_cmd_t
*zc
)
2006 char *path
= zc
->zc_value
;
2007 uint64_t guid
= zc
->zc_guid
;
2010 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
2014 error
= spa_vdev_setpath(spa
, guid
, path
);
2015 spa_close(spa
, FTAG
);
2020 zfs_ioc_vdev_setfru(zfs_cmd_t
*zc
)
2023 char *fru
= zc
->zc_value
;
2024 uint64_t guid
= zc
->zc_guid
;
2027 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
2031 error
= spa_vdev_setfru(spa
, guid
, fru
);
2032 spa_close(spa
, FTAG
);
2037 zfs_ioc_objset_stats_impl(zfs_cmd_t
*zc
, objset_t
*os
)
2042 dmu_objset_fast_stat(os
, &zc
->zc_objset_stats
);
2044 if (zc
->zc_nvlist_dst
!= 0 &&
2045 (error
= dsl_prop_get_all(os
, &nv
)) == 0) {
2046 dmu_objset_stats(os
, nv
);
2048 * NB: zvol_get_stats() will read the objset contents,
2049 * which we aren't supposed to do with a
2050 * DS_MODE_USER hold, because it could be
2051 * inconsistent. So this is a bit of a workaround...
2052 * XXX reading with out owning
2054 if (!zc
->zc_objset_stats
.dds_inconsistent
&&
2055 dmu_objset_type(os
) == DMU_OST_ZVOL
) {
2056 error
= zvol_get_stats(os
, nv
);
2064 error
= put_nvlist(zc
, nv
);
2073 * zc_name name of filesystem
2074 * zc_nvlist_dst_size size of buffer for property nvlist
2077 * zc_objset_stats stats
2078 * zc_nvlist_dst property nvlist
2079 * zc_nvlist_dst_size size of property nvlist
2082 zfs_ioc_objset_stats(zfs_cmd_t
*zc
)
2087 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
2089 error
= zfs_ioc_objset_stats_impl(zc
, os
);
2090 dmu_objset_rele(os
, FTAG
);
2098 * zc_name name of filesystem
2099 * zc_nvlist_dst_size size of buffer for property nvlist
2102 * zc_nvlist_dst received property nvlist
2103 * zc_nvlist_dst_size size of received property nvlist
2105 * Gets received properties (distinct from local properties on or after
2106 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
2107 * local property values.
2110 zfs_ioc_objset_recvd_props(zfs_cmd_t
*zc
)
2116 * Without this check, we would return local property values if the
2117 * caller has not already received properties on or after
2118 * SPA_VERSION_RECVD_PROPS.
2120 if (!dsl_prop_get_hasrecvd(zc
->zc_name
))
2121 return (SET_ERROR(ENOTSUP
));
2123 if (zc
->zc_nvlist_dst
!= 0 &&
2124 (error
= dsl_prop_get_received(zc
->zc_name
, &nv
)) == 0) {
2125 error
= put_nvlist(zc
, nv
);
2133 nvl_add_zplprop(objset_t
*os
, nvlist_t
*props
, zfs_prop_t prop
)
2139 * zfs_get_zplprop() will either find a value or give us
2140 * the default value (if there is one).
2142 if ((error
= zfs_get_zplprop(os
, prop
, &value
)) != 0)
2144 VERIFY(nvlist_add_uint64(props
, zfs_prop_to_name(prop
), value
) == 0);
2150 * zc_name name of filesystem
2151 * zc_nvlist_dst_size size of buffer for zpl property nvlist
2154 * zc_nvlist_dst zpl property nvlist
2155 * zc_nvlist_dst_size size of zpl property nvlist
2158 zfs_ioc_objset_zplprops(zfs_cmd_t
*zc
)
2163 /* XXX reading without owning */
2164 if ((err
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
)))
2167 dmu_objset_fast_stat(os
, &zc
->zc_objset_stats
);
2170 * NB: nvl_add_zplprop() will read the objset contents,
2171 * which we aren't supposed to do with a DS_MODE_USER
2172 * hold, because it could be inconsistent.
2174 if (zc
->zc_nvlist_dst
!= 0 &&
2175 !zc
->zc_objset_stats
.dds_inconsistent
&&
2176 dmu_objset_type(os
) == DMU_OST_ZFS
) {
2179 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2180 if ((err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_VERSION
)) == 0 &&
2181 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_NORMALIZE
)) == 0 &&
2182 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_UTF8ONLY
)) == 0 &&
2183 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_CASE
)) == 0)
2184 err
= put_nvlist(zc
, nv
);
2187 err
= SET_ERROR(ENOENT
);
2189 dmu_objset_rele(os
, FTAG
);
2194 dataset_name_hidden(const char *name
)
2197 * Skip over datasets that are not visible in this zone,
2198 * internal datasets (which have a $ in their name), and
2199 * temporary datasets (which have a % in their name).
2201 if (strchr(name
, '$') != NULL
)
2203 if (strchr(name
, '%') != NULL
)
2205 if (!INGLOBALZONE(curproc
) && !zone_dataset_visible(name
, NULL
))
2212 * zc_name name of filesystem
2213 * zc_cookie zap cursor
2214 * zc_nvlist_dst_size size of buffer for property nvlist
2217 * zc_name name of next filesystem
2218 * zc_cookie zap cursor
2219 * zc_objset_stats stats
2220 * zc_nvlist_dst property nvlist
2221 * zc_nvlist_dst_size size of property nvlist
2224 zfs_ioc_dataset_list_next(zfs_cmd_t
*zc
)
2229 size_t orig_len
= strlen(zc
->zc_name
);
2232 if ((error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
))) {
2233 if (error
== ENOENT
)
2234 error
= SET_ERROR(ESRCH
);
2238 p
= strrchr(zc
->zc_name
, '/');
2239 if (p
== NULL
|| p
[1] != '\0')
2240 (void) strlcat(zc
->zc_name
, "/", sizeof (zc
->zc_name
));
2241 p
= zc
->zc_name
+ strlen(zc
->zc_name
);
2244 error
= dmu_dir_list_next(os
,
2245 sizeof (zc
->zc_name
) - (p
- zc
->zc_name
), p
,
2246 NULL
, &zc
->zc_cookie
);
2247 if (error
== ENOENT
)
2248 error
= SET_ERROR(ESRCH
);
2249 } while (error
== 0 && dataset_name_hidden(zc
->zc_name
));
2250 dmu_objset_rele(os
, FTAG
);
2253 * If it's an internal dataset (ie. with a '$' in its name),
2254 * don't try to get stats for it, otherwise we'll return ENOENT.
2256 if (error
== 0 && strchr(zc
->zc_name
, '$') == NULL
) {
2257 error
= zfs_ioc_objset_stats(zc
); /* fill in the stats */
2258 if (error
== ENOENT
) {
2259 /* We lost a race with destroy, get the next one. */
2260 zc
->zc_name
[orig_len
] = '\0';
2269 * zc_name name of filesystem
2270 * zc_cookie zap cursor
2271 * zc_nvlist_dst_size size of buffer for property nvlist
2274 * zc_name name of next snapshot
2275 * zc_objset_stats stats
2276 * zc_nvlist_dst property nvlist
2277 * zc_nvlist_dst_size size of property nvlist
2280 zfs_ioc_snapshot_list_next(zfs_cmd_t
*zc
)
2285 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
2287 return (error
== ENOENT
? ESRCH
: error
);
2291 * A dataset name of maximum length cannot have any snapshots,
2292 * so exit immediately.
2294 if (strlcat(zc
->zc_name
, "@", sizeof (zc
->zc_name
)) >=
2295 ZFS_MAX_DATASET_NAME_LEN
) {
2296 dmu_objset_rele(os
, FTAG
);
2297 return (SET_ERROR(ESRCH
));
2300 error
= dmu_snapshot_list_next(os
,
2301 sizeof (zc
->zc_name
) - strlen(zc
->zc_name
),
2302 zc
->zc_name
+ strlen(zc
->zc_name
), &zc
->zc_obj
, &zc
->zc_cookie
,
2305 if (error
== 0 && !zc
->zc_simple
) {
2307 dsl_pool_t
*dp
= os
->os_dsl_dataset
->ds_dir
->dd_pool
;
2309 error
= dsl_dataset_hold_obj(dp
, zc
->zc_obj
, FTAG
, &ds
);
2313 error
= dmu_objset_from_ds(ds
, &ossnap
);
2315 error
= zfs_ioc_objset_stats_impl(zc
, ossnap
);
2316 dsl_dataset_rele(ds
, FTAG
);
2318 } else if (error
== ENOENT
) {
2319 error
= SET_ERROR(ESRCH
);
2322 dmu_objset_rele(os
, FTAG
);
2323 /* if we failed, undo the @ that we tacked on to zc_name */
2325 *strchr(zc
->zc_name
, '@') = '\0';
2330 zfs_prop_set_userquota(const char *dsname
, nvpair_t
*pair
)
2332 const char *propname
= nvpair_name(pair
);
2334 unsigned int vallen
;
2337 zfs_userquota_prop_t type
;
2343 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2345 VERIFY(nvpair_value_nvlist(pair
, &attrs
) == 0);
2346 if (nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
2348 return (SET_ERROR(EINVAL
));
2352 * A correctly constructed propname is encoded as
2353 * userquota@<rid>-<domain>.
2355 if ((dash
= strchr(propname
, '-')) == NULL
||
2356 nvpair_value_uint64_array(pair
, &valary
, &vallen
) != 0 ||
2358 return (SET_ERROR(EINVAL
));
2365 err
= zfsvfs_hold(dsname
, FTAG
, &zfsvfs
, B_FALSE
);
2367 err
= zfs_set_userquota(zfsvfs
, type
, domain
, rid
, quota
);
2368 zfsvfs_rele(zfsvfs
, FTAG
);
2375 * If the named property is one that has a special function to set its value,
2376 * return 0 on success and a positive error code on failure; otherwise if it is
2377 * not one of the special properties handled by this function, return -1.
2379 * XXX: It would be better for callers of the property interface if we handled
2380 * these special cases in dsl_prop.c (in the dsl layer).
2383 zfs_prop_set_special(const char *dsname
, zprop_source_t source
,
2386 const char *propname
= nvpair_name(pair
);
2387 zfs_prop_t prop
= zfs_name_to_prop(propname
);
2391 if (prop
== ZPROP_INVAL
) {
2392 if (zfs_prop_userquota(propname
))
2393 return (zfs_prop_set_userquota(dsname
, pair
));
2397 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2399 VERIFY(nvpair_value_nvlist(pair
, &attrs
) == 0);
2400 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
2404 if (zfs_prop_get_type(prop
) == PROP_TYPE_STRING
)
2407 VERIFY(0 == nvpair_value_uint64(pair
, &intval
));
2410 case ZFS_PROP_QUOTA
:
2411 err
= dsl_dir_set_quota(dsname
, source
, intval
);
2413 case ZFS_PROP_REFQUOTA
:
2414 err
= dsl_dataset_set_refquota(dsname
, source
, intval
);
2416 case ZFS_PROP_FILESYSTEM_LIMIT
:
2417 case ZFS_PROP_SNAPSHOT_LIMIT
:
2418 if (intval
== UINT64_MAX
) {
2419 /* clearing the limit, just do it */
2422 err
= dsl_dir_activate_fs_ss_limit(dsname
);
2425 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2426 * default path to set the value in the nvlist.
2431 case ZFS_PROP_RESERVATION
:
2432 err
= dsl_dir_set_reservation(dsname
, source
, intval
);
2434 case ZFS_PROP_REFRESERVATION
:
2435 err
= dsl_dataset_set_refreservation(dsname
, source
, intval
);
2437 case ZFS_PROP_VOLSIZE
:
2438 err
= zvol_set_volsize(dsname
, intval
);
2440 case ZFS_PROP_SNAPDEV
:
2441 err
= zvol_set_snapdev(dsname
, source
, intval
);
2443 case ZFS_PROP_VOLMODE
:
2444 err
= zvol_set_volmode(dsname
, source
, intval
);
2446 case ZFS_PROP_VERSION
:
2450 if ((err
= zfsvfs_hold(dsname
, FTAG
, &zfsvfs
, B_TRUE
)) != 0)
2453 err
= zfs_set_version(zfsvfs
, intval
);
2454 zfsvfs_rele(zfsvfs
, FTAG
);
2456 if (err
== 0 && intval
>= ZPL_VERSION_USERSPACE
) {
2459 zc
= kmem_zalloc(sizeof (zfs_cmd_t
), KM_SLEEP
);
2460 (void) strcpy(zc
->zc_name
, dsname
);
2461 (void) zfs_ioc_userspace_upgrade(zc
);
2462 (void) zfs_ioc_userobjspace_upgrade(zc
);
2463 kmem_free(zc
, sizeof (zfs_cmd_t
));
2475 * This function is best effort. If it fails to set any of the given properties,
2476 * it continues to set as many as it can and returns the last error
2477 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2478 * with the list of names of all the properties that failed along with the
2479 * corresponding error numbers.
2481 * If every property is set successfully, zero is returned and errlist is not
2485 zfs_set_prop_nvlist(const char *dsname
, zprop_source_t source
, nvlist_t
*nvl
,
2494 nvlist_t
*genericnvl
= fnvlist_alloc();
2495 nvlist_t
*retrynvl
= fnvlist_alloc();
2498 while ((pair
= nvlist_next_nvpair(nvl
, pair
)) != NULL
) {
2499 const char *propname
= nvpair_name(pair
);
2500 zfs_prop_t prop
= zfs_name_to_prop(propname
);
2503 /* decode the property value */
2505 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2507 attrs
= fnvpair_value_nvlist(pair
);
2508 if (nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
2510 err
= SET_ERROR(EINVAL
);
2513 /* Validate value type */
2514 if (err
== 0 && source
== ZPROP_SRC_INHERITED
) {
2515 /* inherited properties are expected to be booleans */
2516 if (nvpair_type(propval
) != DATA_TYPE_BOOLEAN
)
2517 err
= SET_ERROR(EINVAL
);
2518 } else if (err
== 0 && prop
== ZPROP_INVAL
) {
2519 if (zfs_prop_user(propname
)) {
2520 if (nvpair_type(propval
) != DATA_TYPE_STRING
)
2521 err
= SET_ERROR(EINVAL
);
2522 } else if (zfs_prop_userquota(propname
)) {
2523 if (nvpair_type(propval
) !=
2524 DATA_TYPE_UINT64_ARRAY
)
2525 err
= SET_ERROR(EINVAL
);
2527 err
= SET_ERROR(EINVAL
);
2529 } else if (err
== 0) {
2530 if (nvpair_type(propval
) == DATA_TYPE_STRING
) {
2531 if (zfs_prop_get_type(prop
) != PROP_TYPE_STRING
)
2532 err
= SET_ERROR(EINVAL
);
2533 } else if (nvpair_type(propval
) == DATA_TYPE_UINT64
) {
2536 intval
= fnvpair_value_uint64(propval
);
2538 switch (zfs_prop_get_type(prop
)) {
2539 case PROP_TYPE_NUMBER
:
2541 case PROP_TYPE_STRING
:
2542 err
= SET_ERROR(EINVAL
);
2544 case PROP_TYPE_INDEX
:
2545 if (zfs_prop_index_to_string(prop
,
2546 intval
, &unused
) != 0)
2547 err
= SET_ERROR(EINVAL
);
2551 "unknown property type");
2554 err
= SET_ERROR(EINVAL
);
2558 /* Validate permissions */
2560 err
= zfs_check_settable(dsname
, pair
, CRED());
2563 if (source
== ZPROP_SRC_INHERITED
)
2564 err
= -1; /* does not need special handling */
2566 err
= zfs_prop_set_special(dsname
, source
,
2570 * For better performance we build up a list of
2571 * properties to set in a single transaction.
2573 err
= nvlist_add_nvpair(genericnvl
, pair
);
2574 } else if (err
!= 0 && nvl
!= retrynvl
) {
2576 * This may be a spurious error caused by
2577 * receiving quota and reservation out of order.
2578 * Try again in a second pass.
2580 err
= nvlist_add_nvpair(retrynvl
, pair
);
2585 if (errlist
!= NULL
)
2586 fnvlist_add_int32(errlist
, propname
, err
);
2591 if (nvl
!= retrynvl
&& !nvlist_empty(retrynvl
)) {
2596 if (!nvlist_empty(genericnvl
) &&
2597 dsl_props_set(dsname
, source
, genericnvl
) != 0) {
2599 * If this fails, we still want to set as many properties as we
2600 * can, so try setting them individually.
2603 while ((pair
= nvlist_next_nvpair(genericnvl
, pair
)) != NULL
) {
2604 const char *propname
= nvpair_name(pair
);
2608 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2610 attrs
= fnvpair_value_nvlist(pair
);
2611 propval
= fnvlist_lookup_nvpair(attrs
,
2615 if (nvpair_type(propval
) == DATA_TYPE_STRING
) {
2616 strval
= fnvpair_value_string(propval
);
2617 err
= dsl_prop_set_string(dsname
, propname
,
2619 } else if (nvpair_type(propval
) == DATA_TYPE_BOOLEAN
) {
2620 err
= dsl_prop_inherit(dsname
, propname
,
2623 intval
= fnvpair_value_uint64(propval
);
2624 err
= dsl_prop_set_int(dsname
, propname
, source
,
2629 if (errlist
!= NULL
) {
2630 fnvlist_add_int32(errlist
, propname
,
2637 nvlist_free(genericnvl
);
2638 nvlist_free(retrynvl
);
2644 * Check that all the properties are valid user properties.
2647 zfs_check_userprops(const char *fsname
, nvlist_t
*nvl
)
2649 nvpair_t
*pair
= NULL
;
2652 while ((pair
= nvlist_next_nvpair(nvl
, pair
)) != NULL
) {
2653 const char *propname
= nvpair_name(pair
);
2655 if (!zfs_prop_user(propname
) ||
2656 nvpair_type(pair
) != DATA_TYPE_STRING
)
2657 return (SET_ERROR(EINVAL
));
2659 if ((error
= zfs_secpolicy_write_perms(fsname
,
2660 ZFS_DELEG_PERM_USERPROP
, CRED())))
2663 if (strlen(propname
) >= ZAP_MAXNAMELEN
)
2664 return (SET_ERROR(ENAMETOOLONG
));
2666 if (strlen(fnvpair_value_string(pair
)) >= ZAP_MAXVALUELEN
)
2667 return (SET_ERROR(E2BIG
));
2673 props_skip(nvlist_t
*props
, nvlist_t
*skipped
, nvlist_t
**newprops
)
2677 VERIFY(nvlist_alloc(newprops
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2680 while ((pair
= nvlist_next_nvpair(props
, pair
)) != NULL
) {
2681 if (nvlist_exists(skipped
, nvpair_name(pair
)))
2684 VERIFY(nvlist_add_nvpair(*newprops
, pair
) == 0);
2689 clear_received_props(const char *dsname
, nvlist_t
*props
,
2693 nvlist_t
*cleared_props
= NULL
;
2694 props_skip(props
, skipped
, &cleared_props
);
2695 if (!nvlist_empty(cleared_props
)) {
2697 * Acts on local properties until the dataset has received
2698 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2700 zprop_source_t flags
= (ZPROP_SRC_NONE
|
2701 (dsl_prop_get_hasrecvd(dsname
) ? ZPROP_SRC_RECEIVED
: 0));
2702 err
= zfs_set_prop_nvlist(dsname
, flags
, cleared_props
, NULL
);
2704 nvlist_free(cleared_props
);
2710 * zc_name name of filesystem
2711 * zc_value name of property to set
2712 * zc_nvlist_src{_size} nvlist of properties to apply
2713 * zc_cookie received properties flag
2716 * zc_nvlist_dst{_size} error for each unapplied received property
2719 zfs_ioc_set_prop(zfs_cmd_t
*zc
)
2722 boolean_t received
= zc
->zc_cookie
;
2723 zprop_source_t source
= (received
? ZPROP_SRC_RECEIVED
:
2728 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2729 zc
->zc_iflags
, &nvl
)) != 0)
2733 nvlist_t
*origprops
;
2735 if (dsl_prop_get_received(zc
->zc_name
, &origprops
) == 0) {
2736 (void) clear_received_props(zc
->zc_name
,
2738 nvlist_free(origprops
);
2741 error
= dsl_prop_set_hasrecvd(zc
->zc_name
);
2744 errors
= fnvlist_alloc();
2746 error
= zfs_set_prop_nvlist(zc
->zc_name
, source
, nvl
, errors
);
2748 if (zc
->zc_nvlist_dst
!= 0 && errors
!= NULL
) {
2749 (void) put_nvlist(zc
, errors
);
2752 nvlist_free(errors
);
2759 * zc_name name of filesystem
2760 * zc_value name of property to inherit
2761 * zc_cookie revert to received value if TRUE
2766 zfs_ioc_inherit_prop(zfs_cmd_t
*zc
)
2768 const char *propname
= zc
->zc_value
;
2769 zfs_prop_t prop
= zfs_name_to_prop(propname
);
2770 boolean_t received
= zc
->zc_cookie
;
2771 zprop_source_t source
= (received
2772 ? ZPROP_SRC_NONE
/* revert to received value, if any */
2773 : ZPROP_SRC_INHERITED
); /* explicitly inherit */
2781 * Only check this in the non-received case. We want to allow
2782 * 'inherit -S' to revert non-inheritable properties like quota
2783 * and reservation to the received or default values even though
2784 * they are not considered inheritable.
2786 if (prop
!= ZPROP_INVAL
&& !zfs_prop_inheritable(prop
))
2787 return (SET_ERROR(EINVAL
));
2790 if (prop
== ZPROP_INVAL
) {
2791 if (!zfs_prop_user(propname
))
2792 return (SET_ERROR(EINVAL
));
2794 type
= PROP_TYPE_STRING
;
2795 } else if (prop
== ZFS_PROP_VOLSIZE
|| prop
== ZFS_PROP_VERSION
) {
2796 return (SET_ERROR(EINVAL
));
2798 type
= zfs_prop_get_type(prop
);
2802 * zfs_prop_set_special() expects properties in the form of an
2803 * nvpair with type info.
2805 dummy
= fnvlist_alloc();
2808 case PROP_TYPE_STRING
:
2809 VERIFY(0 == nvlist_add_string(dummy
, propname
, ""));
2811 case PROP_TYPE_NUMBER
:
2812 case PROP_TYPE_INDEX
:
2813 VERIFY(0 == nvlist_add_uint64(dummy
, propname
, 0));
2816 err
= SET_ERROR(EINVAL
);
2820 pair
= nvlist_next_nvpair(dummy
, NULL
);
2822 err
= SET_ERROR(EINVAL
);
2824 err
= zfs_prop_set_special(zc
->zc_name
, source
, pair
);
2825 if (err
== -1) /* property is not "special", needs handling */
2826 err
= dsl_prop_inherit(zc
->zc_name
, zc
->zc_value
,
2836 zfs_ioc_pool_set_props(zfs_cmd_t
*zc
)
2843 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2844 zc
->zc_iflags
, &props
)))
2848 * If the only property is the configfile, then just do a spa_lookup()
2849 * to handle the faulted case.
2851 pair
= nvlist_next_nvpair(props
, NULL
);
2852 if (pair
!= NULL
&& strcmp(nvpair_name(pair
),
2853 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
)) == 0 &&
2854 nvlist_next_nvpair(props
, pair
) == NULL
) {
2855 mutex_enter(&spa_namespace_lock
);
2856 if ((spa
= spa_lookup(zc
->zc_name
)) != NULL
) {
2857 spa_configfile_set(spa
, props
, B_FALSE
);
2858 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
2860 mutex_exit(&spa_namespace_lock
);
2867 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0) {
2872 error
= spa_prop_set(spa
, props
);
2875 spa_close(spa
, FTAG
);
2881 zfs_ioc_pool_get_props(zfs_cmd_t
*zc
)
2885 nvlist_t
*nvp
= NULL
;
2887 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0) {
2889 * If the pool is faulted, there may be properties we can still
2890 * get (such as altroot and cachefile), so attempt to get them
2893 mutex_enter(&spa_namespace_lock
);
2894 if ((spa
= spa_lookup(zc
->zc_name
)) != NULL
)
2895 error
= spa_prop_get(spa
, &nvp
);
2896 mutex_exit(&spa_namespace_lock
);
2898 error
= spa_prop_get(spa
, &nvp
);
2899 spa_close(spa
, FTAG
);
2902 if (error
== 0 && zc
->zc_nvlist_dst
!= 0)
2903 error
= put_nvlist(zc
, nvp
);
2905 error
= SET_ERROR(EFAULT
);
2913 * zc_name name of filesystem
2914 * zc_nvlist_src{_size} nvlist of delegated permissions
2915 * zc_perm_action allow/unallow flag
2920 zfs_ioc_set_fsacl(zfs_cmd_t
*zc
)
2923 nvlist_t
*fsaclnv
= NULL
;
2925 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2926 zc
->zc_iflags
, &fsaclnv
)) != 0)
2930 * Verify nvlist is constructed correctly
2932 if ((error
= zfs_deleg_verify_nvlist(fsaclnv
)) != 0) {
2933 nvlist_free(fsaclnv
);
2934 return (SET_ERROR(EINVAL
));
2938 * If we don't have PRIV_SYS_MOUNT, then validate
2939 * that user is allowed to hand out each permission in
2943 error
= secpolicy_zfs(CRED());
2945 if (zc
->zc_perm_action
== B_FALSE
) {
2946 error
= dsl_deleg_can_allow(zc
->zc_name
,
2949 error
= dsl_deleg_can_unallow(zc
->zc_name
,
2955 error
= dsl_deleg_set(zc
->zc_name
, fsaclnv
, zc
->zc_perm_action
);
2957 nvlist_free(fsaclnv
);
2963 * zc_name name of filesystem
2966 * zc_nvlist_src{_size} nvlist of delegated permissions
2969 zfs_ioc_get_fsacl(zfs_cmd_t
*zc
)
2974 if ((error
= dsl_deleg_get(zc
->zc_name
, &nvp
)) == 0) {
2975 error
= put_nvlist(zc
, nvp
);
2984 zfs_create_cb(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
)
2986 zfs_creat_t
*zct
= arg
;
2988 zfs_create_fs(os
, cr
, zct
->zct_zplprops
, tx
);
2991 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
2995 * os parent objset pointer (NULL if root fs)
2996 * fuids_ok fuids allowed in this version of the spa?
2997 * sa_ok SAs allowed in this version of the spa?
2998 * createprops list of properties requested by creator
3001 * zplprops values for the zplprops we attach to the master node object
3002 * is_ci true if requested file system will be purely case-insensitive
3004 * Determine the settings for utf8only, normalization and
3005 * casesensitivity. Specific values may have been requested by the
3006 * creator and/or we can inherit values from the parent dataset. If
3007 * the file system is of too early a vintage, a creator can not
3008 * request settings for these properties, even if the requested
3009 * setting is the default value. We don't actually want to create dsl
3010 * properties for these, so remove them from the source nvlist after
3014 zfs_fill_zplprops_impl(objset_t
*os
, uint64_t zplver
,
3015 boolean_t fuids_ok
, boolean_t sa_ok
, nvlist_t
*createprops
,
3016 nvlist_t
*zplprops
, boolean_t
*is_ci
)
3018 uint64_t sense
= ZFS_PROP_UNDEFINED
;
3019 uint64_t norm
= ZFS_PROP_UNDEFINED
;
3020 uint64_t u8
= ZFS_PROP_UNDEFINED
;
3023 ASSERT(zplprops
!= NULL
);
3025 if (os
!= NULL
&& os
->os_phys
->os_type
!= DMU_OST_ZFS
)
3026 return (SET_ERROR(EINVAL
));
3029 * Pull out creator prop choices, if any.
3032 (void) nvlist_lookup_uint64(createprops
,
3033 zfs_prop_to_name(ZFS_PROP_VERSION
), &zplver
);
3034 (void) nvlist_lookup_uint64(createprops
,
3035 zfs_prop_to_name(ZFS_PROP_NORMALIZE
), &norm
);
3036 (void) nvlist_remove_all(createprops
,
3037 zfs_prop_to_name(ZFS_PROP_NORMALIZE
));
3038 (void) nvlist_lookup_uint64(createprops
,
3039 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), &u8
);
3040 (void) nvlist_remove_all(createprops
,
3041 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
));
3042 (void) nvlist_lookup_uint64(createprops
,
3043 zfs_prop_to_name(ZFS_PROP_CASE
), &sense
);
3044 (void) nvlist_remove_all(createprops
,
3045 zfs_prop_to_name(ZFS_PROP_CASE
));
3049 * If the zpl version requested is whacky or the file system
3050 * or pool is version is too "young" to support normalization
3051 * and the creator tried to set a value for one of the props,
3054 if ((zplver
< ZPL_VERSION_INITIAL
|| zplver
> ZPL_VERSION
) ||
3055 (zplver
>= ZPL_VERSION_FUID
&& !fuids_ok
) ||
3056 (zplver
>= ZPL_VERSION_SA
&& !sa_ok
) ||
3057 (zplver
< ZPL_VERSION_NORMALIZATION
&&
3058 (norm
!= ZFS_PROP_UNDEFINED
|| u8
!= ZFS_PROP_UNDEFINED
||
3059 sense
!= ZFS_PROP_UNDEFINED
)))
3060 return (SET_ERROR(ENOTSUP
));
3063 * Put the version in the zplprops
3065 VERIFY(nvlist_add_uint64(zplprops
,
3066 zfs_prop_to_name(ZFS_PROP_VERSION
), zplver
) == 0);
3068 if (norm
== ZFS_PROP_UNDEFINED
&&
3069 (error
= zfs_get_zplprop(os
, ZFS_PROP_NORMALIZE
, &norm
)) != 0)
3071 VERIFY(nvlist_add_uint64(zplprops
,
3072 zfs_prop_to_name(ZFS_PROP_NORMALIZE
), norm
) == 0);
3075 * If we're normalizing, names must always be valid UTF-8 strings.
3079 if (u8
== ZFS_PROP_UNDEFINED
&&
3080 (error
= zfs_get_zplprop(os
, ZFS_PROP_UTF8ONLY
, &u8
)) != 0)
3082 VERIFY(nvlist_add_uint64(zplprops
,
3083 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), u8
) == 0);
3085 if (sense
== ZFS_PROP_UNDEFINED
&&
3086 (error
= zfs_get_zplprop(os
, ZFS_PROP_CASE
, &sense
)) != 0)
3088 VERIFY(nvlist_add_uint64(zplprops
,
3089 zfs_prop_to_name(ZFS_PROP_CASE
), sense
) == 0);
3092 *is_ci
= (sense
== ZFS_CASE_INSENSITIVE
);
3098 zfs_fill_zplprops(const char *dataset
, nvlist_t
*createprops
,
3099 nvlist_t
*zplprops
, boolean_t
*is_ci
)
3101 boolean_t fuids_ok
, sa_ok
;
3102 uint64_t zplver
= ZPL_VERSION
;
3103 objset_t
*os
= NULL
;
3104 char parentname
[ZFS_MAX_DATASET_NAME_LEN
];
3110 (void) strlcpy(parentname
, dataset
, sizeof (parentname
));
3111 cp
= strrchr(parentname
, '/');
3115 if ((error
= spa_open(dataset
, &spa
, FTAG
)) != 0)
3118 spa_vers
= spa_version(spa
);
3119 spa_close(spa
, FTAG
);
3121 zplver
= zfs_zpl_version_map(spa_vers
);
3122 fuids_ok
= (zplver
>= ZPL_VERSION_FUID
);
3123 sa_ok
= (zplver
>= ZPL_VERSION_SA
);
3126 * Open parent object set so we can inherit zplprop values.
3128 if ((error
= dmu_objset_hold(parentname
, FTAG
, &os
)) != 0)
3131 error
= zfs_fill_zplprops_impl(os
, zplver
, fuids_ok
, sa_ok
, createprops
,
3133 dmu_objset_rele(os
, FTAG
);
3138 zfs_fill_zplprops_root(uint64_t spa_vers
, nvlist_t
*createprops
,
3139 nvlist_t
*zplprops
, boolean_t
*is_ci
)
3143 uint64_t zplver
= ZPL_VERSION
;
3146 zplver
= zfs_zpl_version_map(spa_vers
);
3147 fuids_ok
= (zplver
>= ZPL_VERSION_FUID
);
3148 sa_ok
= (zplver
>= ZPL_VERSION_SA
);
3150 error
= zfs_fill_zplprops_impl(NULL
, zplver
, fuids_ok
, sa_ok
,
3151 createprops
, zplprops
, is_ci
);
3157 * "type" -> dmu_objset_type_t (int32)
3158 * (optional) "props" -> { prop -> value }
3161 * outnvl: propname -> error code (int32)
3164 zfs_ioc_create(const char *fsname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3167 zfs_creat_t zct
= { 0 };
3168 nvlist_t
*nvprops
= NULL
;
3169 void (*cbfunc
)(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
);
3171 dmu_objset_type_t type
;
3172 boolean_t is_insensitive
= B_FALSE
;
3174 if (nvlist_lookup_int32(innvl
, "type", &type32
) != 0)
3175 return (SET_ERROR(EINVAL
));
3177 (void) nvlist_lookup_nvlist(innvl
, "props", &nvprops
);
3181 cbfunc
= zfs_create_cb
;
3185 cbfunc
= zvol_create_cb
;
3192 if (strchr(fsname
, '@') ||
3193 strchr(fsname
, '%'))
3194 return (SET_ERROR(EINVAL
));
3196 zct
.zct_props
= nvprops
;
3199 return (SET_ERROR(EINVAL
));
3201 if (type
== DMU_OST_ZVOL
) {
3202 uint64_t volsize
, volblocksize
;
3204 if (nvprops
== NULL
)
3205 return (SET_ERROR(EINVAL
));
3206 if (nvlist_lookup_uint64(nvprops
,
3207 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &volsize
) != 0)
3208 return (SET_ERROR(EINVAL
));
3210 if ((error
= nvlist_lookup_uint64(nvprops
,
3211 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
3212 &volblocksize
)) != 0 && error
!= ENOENT
)
3213 return (SET_ERROR(EINVAL
));
3216 volblocksize
= zfs_prop_default_numeric(
3217 ZFS_PROP_VOLBLOCKSIZE
);
3219 if ((error
= zvol_check_volblocksize(fsname
,
3220 volblocksize
)) != 0 ||
3221 (error
= zvol_check_volsize(volsize
,
3222 volblocksize
)) != 0)
3224 } else if (type
== DMU_OST_ZFS
) {
3228 * We have to have normalization and
3229 * case-folding flags correct when we do the
3230 * file system creation, so go figure them out
3233 VERIFY(nvlist_alloc(&zct
.zct_zplprops
,
3234 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3235 error
= zfs_fill_zplprops(fsname
, nvprops
,
3236 zct
.zct_zplprops
, &is_insensitive
);
3238 nvlist_free(zct
.zct_zplprops
);
3243 error
= dmu_objset_create(fsname
, type
,
3244 is_insensitive
? DS_FLAG_CI_DATASET
: 0, cbfunc
, &zct
);
3245 nvlist_free(zct
.zct_zplprops
);
3248 * It would be nice to do this atomically.
3251 error
= zfs_set_prop_nvlist(fsname
, ZPROP_SRC_LOCAL
,
3258 * Volumes will return EBUSY and cannot be destroyed
3259 * until all asynchronous minor handling has completed.
3260 * Wait for the spa_zvol_taskq to drain then retry.
3262 error2
= dsl_destroy_head(fsname
);
3263 while ((error2
== EBUSY
) && (type
== DMU_OST_ZVOL
)) {
3264 error2
= spa_open(fsname
, &spa
, FTAG
);
3266 taskq_wait(spa
->spa_zvol_taskq
);
3267 spa_close(spa
, FTAG
);
3269 error2
= dsl_destroy_head(fsname
);
3278 * "origin" -> name of origin snapshot
3279 * (optional) "props" -> { prop -> value }
3283 * outnvl: propname -> error code (int32)
3286 zfs_ioc_clone(const char *fsname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3289 nvlist_t
*nvprops
= NULL
;
3292 if (nvlist_lookup_string(innvl
, "origin", &origin_name
) != 0)
3293 return (SET_ERROR(EINVAL
));
3294 (void) nvlist_lookup_nvlist(innvl
, "props", &nvprops
);
3296 if (strchr(fsname
, '@') ||
3297 strchr(fsname
, '%'))
3298 return (SET_ERROR(EINVAL
));
3300 if (dataset_namecheck(origin_name
, NULL
, NULL
) != 0)
3301 return (SET_ERROR(EINVAL
));
3302 error
= dmu_objset_clone(fsname
, origin_name
);
3307 * It would be nice to do this atomically.
3310 error
= zfs_set_prop_nvlist(fsname
, ZPROP_SRC_LOCAL
,
3313 (void) dsl_destroy_head(fsname
);
3320 * "snaps" -> { snapshot1, snapshot2 }
3321 * (optional) "props" -> { prop -> value (string) }
3324 * outnvl: snapshot -> error code (int32)
3327 zfs_ioc_snapshot(const char *poolname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3330 nvlist_t
*props
= NULL
;
3332 nvpair_t
*pair
, *pair2
;
3334 (void) nvlist_lookup_nvlist(innvl
, "props", &props
);
3335 if ((error
= zfs_check_userprops(poolname
, props
)) != 0)
3338 if (!nvlist_empty(props
) &&
3339 zfs_earlier_version(poolname
, SPA_VERSION_SNAP_PROPS
))
3340 return (SET_ERROR(ENOTSUP
));
3342 if (nvlist_lookup_nvlist(innvl
, "snaps", &snaps
) != 0)
3343 return (SET_ERROR(EINVAL
));
3344 poollen
= strlen(poolname
);
3345 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
3346 pair
= nvlist_next_nvpair(snaps
, pair
)) {
3347 const char *name
= nvpair_name(pair
);
3348 const char *cp
= strchr(name
, '@');
3351 * The snap name must contain an @, and the part after it must
3352 * contain only valid characters.
3355 zfs_component_namecheck(cp
+ 1, NULL
, NULL
) != 0)
3356 return (SET_ERROR(EINVAL
));
3359 * The snap must be in the specified pool.
3361 if (strncmp(name
, poolname
, poollen
) != 0 ||
3362 (name
[poollen
] != '/' && name
[poollen
] != '@'))
3363 return (SET_ERROR(EXDEV
));
3365 /* This must be the only snap of this fs. */
3366 for (pair2
= nvlist_next_nvpair(snaps
, pair
);
3367 pair2
!= NULL
; pair2
= nvlist_next_nvpair(snaps
, pair2
)) {
3368 if (strncmp(name
, nvpair_name(pair2
), cp
- name
+ 1)
3370 return (SET_ERROR(EXDEV
));
3375 error
= dsl_dataset_snapshot(snaps
, props
, outnvl
);
3381 * innvl: "message" -> string
3385 zfs_ioc_log_history(const char *unused
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3393 * The poolname in the ioctl is not set, we get it from the TSD,
3394 * which was set at the end of the last successful ioctl that allows
3395 * logging. The secpolicy func already checked that it is set.
3396 * Only one log ioctl is allowed after each successful ioctl, so
3397 * we clear the TSD here.
3399 poolname
= tsd_get(zfs_allow_log_key
);
3400 if (poolname
== NULL
)
3401 return (SET_ERROR(EINVAL
));
3402 (void) tsd_set(zfs_allow_log_key
, NULL
);
3403 error
= spa_open(poolname
, &spa
, FTAG
);
3408 if (nvlist_lookup_string(innvl
, "message", &message
) != 0) {
3409 spa_close(spa
, FTAG
);
3410 return (SET_ERROR(EINVAL
));
3413 if (spa_version(spa
) < SPA_VERSION_ZPOOL_HISTORY
) {
3414 spa_close(spa
, FTAG
);
3415 return (SET_ERROR(ENOTSUP
));
3418 error
= spa_history_log(spa
, message
);
3419 spa_close(spa
, FTAG
);
3424 * The dp_config_rwlock must not be held when calling this, because the
3425 * unmount may need to write out data.
3427 * This function is best-effort. Callers must deal gracefully if it
3428 * remains mounted (or is remounted after this call).
3430 * Returns 0 if the argument is not a snapshot, or it is not currently a
3431 * filesystem, or we were able to unmount it. Returns error code otherwise.
3434 zfs_unmount_snap(const char *snapname
)
3438 if (strchr(snapname
, '@') == NULL
)
3441 err
= zfsctl_snapshot_unmount((char *)snapname
, MNT_FORCE
);
3442 if (err
!= 0 && err
!= ENOENT
)
3443 return (SET_ERROR(err
));
3450 zfs_unmount_snap_cb(const char *snapname
, void *arg
)
3452 return (zfs_unmount_snap(snapname
));
3456 * When a clone is destroyed, its origin may also need to be destroyed,
3457 * in which case it must be unmounted. This routine will do that unmount
3461 zfs_destroy_unmount_origin(const char *fsname
)
3467 error
= dmu_objset_hold(fsname
, FTAG
, &os
);
3470 ds
= dmu_objset_ds(os
);
3471 if (dsl_dir_is_clone(ds
->ds_dir
) && DS_IS_DEFER_DESTROY(ds
->ds_prev
)) {
3472 char originname
[ZFS_MAX_DATASET_NAME_LEN
];
3473 dsl_dataset_name(ds
->ds_prev
, originname
);
3474 dmu_objset_rele(os
, FTAG
);
3475 (void) zfs_unmount_snap(originname
);
3477 dmu_objset_rele(os
, FTAG
);
3483 * "snaps" -> { snapshot1, snapshot2 }
3484 * (optional boolean) "defer"
3487 * outnvl: snapshot -> error code (int32)
3491 zfs_ioc_destroy_snaps(const char *poolname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3497 if (nvlist_lookup_nvlist(innvl
, "snaps", &snaps
) != 0)
3498 return (SET_ERROR(EINVAL
));
3499 defer
= nvlist_exists(innvl
, "defer");
3501 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
3502 pair
= nvlist_next_nvpair(snaps
, pair
)) {
3503 (void) zfs_unmount_snap(nvpair_name(pair
));
3506 return (dsl_destroy_snapshots_nvl(snaps
, defer
, outnvl
));
3510 * Create bookmarks. Bookmark names are of the form <fs>#<bmark>.
3511 * All bookmarks must be in the same pool.
3514 * bookmark1 -> snapshot1, bookmark2 -> snapshot2
3517 * outnvl: bookmark -> error code (int32)
3522 zfs_ioc_bookmark(const char *poolname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3524 nvpair_t
*pair
, *pair2
;
3526 for (pair
= nvlist_next_nvpair(innvl
, NULL
);
3527 pair
!= NULL
; pair
= nvlist_next_nvpair(innvl
, pair
)) {
3531 * Verify the snapshot argument.
3533 if (nvpair_value_string(pair
, &snap_name
) != 0)
3534 return (SET_ERROR(EINVAL
));
3537 /* Verify that the keys (bookmarks) are unique */
3538 for (pair2
= nvlist_next_nvpair(innvl
, pair
);
3539 pair2
!= NULL
; pair2
= nvlist_next_nvpair(innvl
, pair2
)) {
3540 if (strcmp(nvpair_name(pair
), nvpair_name(pair2
)) == 0)
3541 return (SET_ERROR(EINVAL
));
3545 return (dsl_bookmark_create(innvl
, outnvl
));
3550 * property 1, property 2, ...
3554 * bookmark name 1 -> { property 1, property 2, ... },
3555 * bookmark name 2 -> { property 1, property 2, ... }
3560 zfs_ioc_get_bookmarks(const char *fsname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3562 return (dsl_get_bookmarks(fsname
, innvl
, outnvl
));
3567 * bookmark name 1, bookmark name 2
3570 * outnvl: bookmark -> error code (int32)
3574 zfs_ioc_destroy_bookmarks(const char *poolname
, nvlist_t
*innvl
,
3580 poollen
= strlen(poolname
);
3581 for (pair
= nvlist_next_nvpair(innvl
, NULL
);
3582 pair
!= NULL
; pair
= nvlist_next_nvpair(innvl
, pair
)) {
3583 const char *name
= nvpair_name(pair
);
3584 const char *cp
= strchr(name
, '#');
3587 * The bookmark name must contain an #, and the part after it
3588 * must contain only valid characters.
3591 zfs_component_namecheck(cp
+ 1, NULL
, NULL
) != 0)
3592 return (SET_ERROR(EINVAL
));
3595 * The bookmark must be in the specified pool.
3597 if (strncmp(name
, poolname
, poollen
) != 0 ||
3598 (name
[poollen
] != '/' && name
[poollen
] != '#'))
3599 return (SET_ERROR(EXDEV
));
3602 error
= dsl_bookmark_destroy(innvl
, outnvl
);
3608 * zc_name name of dataset to destroy
3609 * zc_objset_type type of objset
3610 * zc_defer_destroy mark for deferred destroy
3615 zfs_ioc_destroy(zfs_cmd_t
*zc
)
3619 if (zc
->zc_objset_type
== DMU_OST_ZFS
) {
3620 err
= zfs_unmount_snap(zc
->zc_name
);
3625 if (strchr(zc
->zc_name
, '@')) {
3626 err
= dsl_destroy_snapshot(zc
->zc_name
, zc
->zc_defer_destroy
);
3628 err
= dsl_destroy_head(zc
->zc_name
);
3629 if (err
== EEXIST
) {
3631 * It is possible that the given DS may have
3632 * hidden child (%recv) datasets - "leftovers"
3633 * resulting from the previously interrupted
3636 * 6 extra bytes for /%recv
3638 char namebuf
[ZFS_MAX_DATASET_NAME_LEN
+ 6];
3640 if (snprintf(namebuf
, sizeof (namebuf
), "%s/%s",
3641 zc
->zc_name
, recv_clone_name
) >=
3643 return (SET_ERROR(EINVAL
));
3646 * Try to remove the hidden child (%recv) and after
3647 * that try to remove the target dataset.
3648 * If the hidden child (%recv) does not exist
3649 * the original error (EEXIST) will be returned
3651 err
= dsl_destroy_head(namebuf
);
3653 err
= dsl_destroy_head(zc
->zc_name
);
3654 else if (err
== ENOENT
)
3663 * fsname is name of dataset to rollback (to most recent snapshot)
3665 * innvl may contain name of expected target snapshot
3667 * outnvl: "target" -> name of most recent snapshot
3672 zfs_ioc_rollback(const char *fsname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3676 char *target
= NULL
;
3679 (void) nvlist_lookup_string(innvl
, "target", &target
);
3680 if (target
!= NULL
) {
3681 int fslen
= strlen(fsname
);
3683 if (strncmp(fsname
, target
, fslen
) != 0)
3684 return (SET_ERROR(EINVAL
));
3685 if (target
[fslen
] != '@')
3686 return (SET_ERROR(EINVAL
));
3689 if (getzfsvfs(fsname
, &zfsvfs
) == 0) {
3692 ds
= dmu_objset_ds(zfsvfs
->z_os
);
3693 error
= zfs_suspend_fs(zfsvfs
);
3697 error
= dsl_dataset_rollback(fsname
, target
, zfsvfs
,
3699 resume_err
= zfs_resume_fs(zfsvfs
, ds
);
3700 error
= error
? error
: resume_err
;
3702 deactivate_super(zfsvfs
->z_sb
);
3703 } else if ((zv
= zvol_suspend(fsname
)) != NULL
) {
3704 error
= dsl_dataset_rollback(fsname
, target
, zvol_tag(zv
),
3708 error
= dsl_dataset_rollback(fsname
, target
, NULL
, outnvl
);
3714 recursive_unmount(const char *fsname
, void *arg
)
3716 const char *snapname
= arg
;
3720 fullname
= kmem_asprintf("%s@%s", fsname
, snapname
);
3721 error
= zfs_unmount_snap(fullname
);
3729 * zc_name old name of dataset
3730 * zc_value new name of dataset
3731 * zc_cookie recursive flag (only valid for snapshots)
3736 zfs_ioc_rename(zfs_cmd_t
*zc
)
3738 boolean_t recursive
= zc
->zc_cookie
& 1;
3741 /* "zfs rename" from and to ...%recv datasets should both fail */
3742 zc
->zc_name
[sizeof (zc
->zc_name
) - 1] = '\0';
3743 zc
->zc_value
[sizeof (zc
->zc_value
) - 1] = '\0';
3744 if (dataset_namecheck(zc
->zc_name
, NULL
, NULL
) != 0 ||
3745 dataset_namecheck(zc
->zc_value
, NULL
, NULL
) != 0 ||
3746 strchr(zc
->zc_name
, '%') || strchr(zc
->zc_value
, '%'))
3747 return (SET_ERROR(EINVAL
));
3749 at
= strchr(zc
->zc_name
, '@');
3751 /* snaps must be in same fs */
3754 if (strncmp(zc
->zc_name
, zc
->zc_value
, at
- zc
->zc_name
+ 1))
3755 return (SET_ERROR(EXDEV
));
3757 if (zc
->zc_objset_type
== DMU_OST_ZFS
) {
3758 error
= dmu_objset_find(zc
->zc_name
,
3759 recursive_unmount
, at
+ 1,
3760 recursive
? DS_FIND_CHILDREN
: 0);
3766 error
= dsl_dataset_rename_snapshot(zc
->zc_name
,
3767 at
+ 1, strchr(zc
->zc_value
, '@') + 1, recursive
);
3772 return (dsl_dir_rename(zc
->zc_name
, zc
->zc_value
));
3777 zfs_check_settable(const char *dsname
, nvpair_t
*pair
, cred_t
*cr
)
3779 const char *propname
= nvpair_name(pair
);
3780 boolean_t issnap
= (strchr(dsname
, '@') != NULL
);
3781 zfs_prop_t prop
= zfs_name_to_prop(propname
);
3785 if (prop
== ZPROP_INVAL
) {
3786 if (zfs_prop_user(propname
)) {
3787 if ((err
= zfs_secpolicy_write_perms(dsname
,
3788 ZFS_DELEG_PERM_USERPROP
, cr
)))
3793 if (!issnap
&& zfs_prop_userquota(propname
)) {
3794 const char *perm
= NULL
;
3795 const char *uq_prefix
=
3796 zfs_userquota_prop_prefixes
[ZFS_PROP_USERQUOTA
];
3797 const char *gq_prefix
=
3798 zfs_userquota_prop_prefixes
[ZFS_PROP_GROUPQUOTA
];
3799 const char *uiq_prefix
=
3800 zfs_userquota_prop_prefixes
[ZFS_PROP_USEROBJQUOTA
];
3801 const char *giq_prefix
=
3802 zfs_userquota_prop_prefixes
[ZFS_PROP_GROUPOBJQUOTA
];
3804 if (strncmp(propname
, uq_prefix
,
3805 strlen(uq_prefix
)) == 0) {
3806 perm
= ZFS_DELEG_PERM_USERQUOTA
;
3807 } else if (strncmp(propname
, uiq_prefix
,
3808 strlen(uiq_prefix
)) == 0) {
3809 perm
= ZFS_DELEG_PERM_USEROBJQUOTA
;
3810 } else if (strncmp(propname
, gq_prefix
,
3811 strlen(gq_prefix
)) == 0) {
3812 perm
= ZFS_DELEG_PERM_GROUPQUOTA
;
3813 } else if (strncmp(propname
, giq_prefix
,
3814 strlen(giq_prefix
)) == 0) {
3815 perm
= ZFS_DELEG_PERM_GROUPOBJQUOTA
;
3817 /* USERUSED and GROUPUSED are read-only */
3818 return (SET_ERROR(EINVAL
));
3821 if ((err
= zfs_secpolicy_write_perms(dsname
, perm
, cr
)))
3826 return (SET_ERROR(EINVAL
));
3830 return (SET_ERROR(EINVAL
));
3832 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
3834 * dsl_prop_get_all_impl() returns properties in this
3838 VERIFY(nvpair_value_nvlist(pair
, &attrs
) == 0);
3839 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
3844 * Check that this value is valid for this pool version
3847 case ZFS_PROP_COMPRESSION
:
3849 * If the user specified gzip compression, make sure
3850 * the SPA supports it. We ignore any errors here since
3851 * we'll catch them later.
3853 if (nvpair_value_uint64(pair
, &intval
) == 0) {
3854 if (intval
>= ZIO_COMPRESS_GZIP_1
&&
3855 intval
<= ZIO_COMPRESS_GZIP_9
&&
3856 zfs_earlier_version(dsname
,
3857 SPA_VERSION_GZIP_COMPRESSION
)) {
3858 return (SET_ERROR(ENOTSUP
));
3861 if (intval
== ZIO_COMPRESS_ZLE
&&
3862 zfs_earlier_version(dsname
,
3863 SPA_VERSION_ZLE_COMPRESSION
))
3864 return (SET_ERROR(ENOTSUP
));
3866 if (intval
== ZIO_COMPRESS_LZ4
) {
3869 if ((err
= spa_open(dsname
, &spa
, FTAG
)) != 0)
3872 if (!spa_feature_is_enabled(spa
,
3873 SPA_FEATURE_LZ4_COMPRESS
)) {
3874 spa_close(spa
, FTAG
);
3875 return (SET_ERROR(ENOTSUP
));
3877 spa_close(spa
, FTAG
);
3881 * If this is a bootable dataset then
3882 * verify that the compression algorithm
3883 * is supported for booting. We must return
3884 * something other than ENOTSUP since it
3885 * implies a downrev pool version.
3887 if (zfs_is_bootfs(dsname
) &&
3888 !BOOTFS_COMPRESS_VALID(intval
)) {
3889 return (SET_ERROR(ERANGE
));
3894 case ZFS_PROP_COPIES
:
3895 if (zfs_earlier_version(dsname
, SPA_VERSION_DITTO_BLOCKS
))
3896 return (SET_ERROR(ENOTSUP
));
3899 case ZFS_PROP_VOLBLOCKSIZE
:
3900 case ZFS_PROP_RECORDSIZE
:
3901 /* Record sizes above 128k need the feature to be enabled */
3902 if (nvpair_value_uint64(pair
, &intval
) == 0 &&
3903 intval
> SPA_OLD_MAXBLOCKSIZE
) {
3907 * We don't allow setting the property above 1MB,
3908 * unless the tunable has been changed.
3910 if (intval
> zfs_max_recordsize
||
3911 intval
> SPA_MAXBLOCKSIZE
)
3912 return (SET_ERROR(ERANGE
));
3914 if ((err
= spa_open(dsname
, &spa
, FTAG
)) != 0)
3917 if (!spa_feature_is_enabled(spa
,
3918 SPA_FEATURE_LARGE_BLOCKS
)) {
3919 spa_close(spa
, FTAG
);
3920 return (SET_ERROR(ENOTSUP
));
3922 spa_close(spa
, FTAG
);
3926 case ZFS_PROP_DNODESIZE
:
3927 /* Dnode sizes above 512 need the feature to be enabled */
3928 if (nvpair_value_uint64(pair
, &intval
) == 0 &&
3929 intval
!= ZFS_DNSIZE_LEGACY
) {
3933 * If this is a bootable dataset then
3934 * we don't allow large (>512B) dnodes,
3935 * because GRUB doesn't support them.
3937 if (zfs_is_bootfs(dsname
) &&
3938 intval
!= ZFS_DNSIZE_LEGACY
) {
3939 return (SET_ERROR(EDOM
));
3942 if ((err
= spa_open(dsname
, &spa
, FTAG
)) != 0)
3945 if (!spa_feature_is_enabled(spa
,
3946 SPA_FEATURE_LARGE_DNODE
)) {
3947 spa_close(spa
, FTAG
);
3948 return (SET_ERROR(ENOTSUP
));
3950 spa_close(spa
, FTAG
);
3954 case ZFS_PROP_SHARESMB
:
3955 if (zpl_earlier_version(dsname
, ZPL_VERSION_FUID
))
3956 return (SET_ERROR(ENOTSUP
));
3959 case ZFS_PROP_ACLINHERIT
:
3960 if (nvpair_type(pair
) == DATA_TYPE_UINT64
&&
3961 nvpair_value_uint64(pair
, &intval
) == 0) {
3962 if (intval
== ZFS_ACL_PASSTHROUGH_X
&&
3963 zfs_earlier_version(dsname
,
3964 SPA_VERSION_PASSTHROUGH_X
))
3965 return (SET_ERROR(ENOTSUP
));
3968 case ZFS_PROP_CHECKSUM
:
3969 case ZFS_PROP_DEDUP
:
3971 spa_feature_t feature
;
3976 /* dedup feature version checks */
3977 if (prop
== ZFS_PROP_DEDUP
&&
3978 zfs_earlier_version(dsname
, SPA_VERSION_DEDUP
))
3979 return (SET_ERROR(ENOTSUP
));
3981 if (nvpair_value_uint64(pair
, &intval
) != 0)
3982 return (SET_ERROR(EINVAL
));
3984 /* check prop value is enabled in features */
3985 feature
= zio_checksum_to_feature(intval
& ZIO_CHECKSUM_MASK
);
3986 if (feature
== SPA_FEATURE_NONE
)
3989 if ((err
= spa_open(dsname
, &spa
, FTAG
)) != 0)
3992 * Salted checksums are not supported on root pools.
3994 if (spa_bootfs(spa
) != 0 &&
3995 intval
< ZIO_CHECKSUM_FUNCTIONS
&&
3996 (zio_checksum_table
[intval
].ci_flags
&
3997 ZCHECKSUM_FLAG_SALTED
)) {
3998 spa_close(spa
, FTAG
);
3999 return (SET_ERROR(ERANGE
));
4001 if (!spa_feature_is_enabled(spa
, feature
)) {
4002 spa_close(spa
, FTAG
);
4003 return (SET_ERROR(ENOTSUP
));
4005 spa_close(spa
, FTAG
);
4013 return (zfs_secpolicy_setprop(dsname
, prop
, pair
, CRED()));
4017 * Removes properties from the given props list that fail permission checks
4018 * needed to clear them and to restore them in case of a receive error. For each
4019 * property, make sure we have both set and inherit permissions.
4021 * Returns the first error encountered if any permission checks fail. If the
4022 * caller provides a non-NULL errlist, it also gives the complete list of names
4023 * of all the properties that failed a permission check along with the
4024 * corresponding error numbers. The caller is responsible for freeing the
4027 * If every property checks out successfully, zero is returned and the list
4028 * pointed at by errlist is NULL.
4031 zfs_check_clearable(char *dataset
, nvlist_t
*props
, nvlist_t
**errlist
)
4034 nvpair_t
*pair
, *next_pair
;
4041 VERIFY(nvlist_alloc(&errors
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4043 zc
= kmem_alloc(sizeof (zfs_cmd_t
), KM_SLEEP
);
4044 (void) strlcpy(zc
->zc_name
, dataset
, sizeof (zc
->zc_name
));
4045 pair
= nvlist_next_nvpair(props
, NULL
);
4046 while (pair
!= NULL
) {
4047 next_pair
= nvlist_next_nvpair(props
, pair
);
4049 (void) strlcpy(zc
->zc_value
, nvpair_name(pair
),
4050 sizeof (zc
->zc_value
));
4051 if ((err
= zfs_check_settable(dataset
, pair
, CRED())) != 0 ||
4052 (err
= zfs_secpolicy_inherit_prop(zc
, NULL
, CRED())) != 0) {
4053 VERIFY(nvlist_remove_nvpair(props
, pair
) == 0);
4054 VERIFY(nvlist_add_int32(errors
,
4055 zc
->zc_value
, err
) == 0);
4059 kmem_free(zc
, sizeof (zfs_cmd_t
));
4061 if ((pair
= nvlist_next_nvpair(errors
, NULL
)) == NULL
) {
4062 nvlist_free(errors
);
4065 VERIFY(nvpair_value_int32(pair
, &rv
) == 0);
4068 if (errlist
== NULL
)
4069 nvlist_free(errors
);
4077 propval_equals(nvpair_t
*p1
, nvpair_t
*p2
)
4079 if (nvpair_type(p1
) == DATA_TYPE_NVLIST
) {
4080 /* dsl_prop_get_all_impl() format */
4082 VERIFY(nvpair_value_nvlist(p1
, &attrs
) == 0);
4083 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
4087 if (nvpair_type(p2
) == DATA_TYPE_NVLIST
) {
4089 VERIFY(nvpair_value_nvlist(p2
, &attrs
) == 0);
4090 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
4094 if (nvpair_type(p1
) != nvpair_type(p2
))
4097 if (nvpair_type(p1
) == DATA_TYPE_STRING
) {
4098 char *valstr1
, *valstr2
;
4100 VERIFY(nvpair_value_string(p1
, (char **)&valstr1
) == 0);
4101 VERIFY(nvpair_value_string(p2
, (char **)&valstr2
) == 0);
4102 return (strcmp(valstr1
, valstr2
) == 0);
4104 uint64_t intval1
, intval2
;
4106 VERIFY(nvpair_value_uint64(p1
, &intval1
) == 0);
4107 VERIFY(nvpair_value_uint64(p2
, &intval2
) == 0);
4108 return (intval1
== intval2
);
4113 * Remove properties from props if they are not going to change (as determined
4114 * by comparison with origprops). Remove them from origprops as well, since we
4115 * do not need to clear or restore properties that won't change.
4118 props_reduce(nvlist_t
*props
, nvlist_t
*origprops
)
4120 nvpair_t
*pair
, *next_pair
;
4122 if (origprops
== NULL
)
4123 return; /* all props need to be received */
4125 pair
= nvlist_next_nvpair(props
, NULL
);
4126 while (pair
!= NULL
) {
4127 const char *propname
= nvpair_name(pair
);
4130 next_pair
= nvlist_next_nvpair(props
, pair
);
4132 if ((nvlist_lookup_nvpair(origprops
, propname
,
4133 &match
) != 0) || !propval_equals(pair
, match
))
4134 goto next
; /* need to set received value */
4136 /* don't clear the existing received value */
4137 (void) nvlist_remove_nvpair(origprops
, match
);
4138 /* don't bother receiving the property */
4139 (void) nvlist_remove_nvpair(props
, pair
);
4146 * Extract properties that cannot be set PRIOR to the receipt of a dataset.
4147 * For example, refquota cannot be set until after the receipt of a dataset,
4148 * because in replication streams, an older/earlier snapshot may exceed the
4149 * refquota. We want to receive the older/earlier snapshot, but setting
4150 * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent
4151 * the older/earlier snapshot from being received (with EDQUOT).
4153 * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario.
4155 * libzfs will need to be judicious handling errors encountered by props
4156 * extracted by this function.
4159 extract_delay_props(nvlist_t
*props
)
4161 nvlist_t
*delayprops
;
4162 nvpair_t
*nvp
, *tmp
;
4163 static const zfs_prop_t delayable
[] = { ZFS_PROP_REFQUOTA
, 0 };
4166 VERIFY(nvlist_alloc(&delayprops
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4168 for (nvp
= nvlist_next_nvpair(props
, NULL
); nvp
!= NULL
;
4169 nvp
= nvlist_next_nvpair(props
, nvp
)) {
4171 * strcmp() is safe because zfs_prop_to_name() always returns
4174 for (i
= 0; delayable
[i
] != 0; i
++) {
4175 if (strcmp(zfs_prop_to_name(delayable
[i
]),
4176 nvpair_name(nvp
)) == 0) {
4180 if (delayable
[i
] != 0) {
4181 tmp
= nvlist_prev_nvpair(props
, nvp
);
4182 VERIFY(nvlist_add_nvpair(delayprops
, nvp
) == 0);
4183 VERIFY(nvlist_remove_nvpair(props
, nvp
) == 0);
4188 if (nvlist_empty(delayprops
)) {
4189 nvlist_free(delayprops
);
4192 return (delayprops
);
4196 static boolean_t zfs_ioc_recv_inject_err
;
4200 * nvlist 'errors' is always allocated. It will contain descriptions of
4201 * encountered errors, if any. It's the callers responsibility to free.
4204 zfs_ioc_recv_impl(char *tofs
, char *tosnap
, char *origin
, nvlist_t
*recvprops
,
4205 nvlist_t
*localprops
, boolean_t force
, boolean_t resumable
, int input_fd
,
4206 dmu_replay_record_t
*begin_record
, int cleanup_fd
, uint64_t *read_bytes
,
4207 uint64_t *errflags
, uint64_t *action_handle
, nvlist_t
**errors
)
4209 dmu_recv_cookie_t drc
;
4211 int props_error
= 0;
4213 nvlist_t
*delayprops
= NULL
; /* sent properties applied post-receive */
4214 nvlist_t
*origprops
= NULL
; /* existing properties */
4215 nvlist_t
*origrecvd
= NULL
; /* existing received properties */
4216 boolean_t first_recvd_props
= B_FALSE
;
4221 *errors
= fnvlist_alloc();
4223 input_fp
= getf(input_fd
);
4224 if (input_fp
== NULL
)
4225 return (SET_ERROR(EBADF
));
4227 error
= dmu_recv_begin(tofs
, tosnap
,
4228 begin_record
, force
, resumable
, origin
, &drc
);
4233 * Set properties before we receive the stream so that they are applied
4234 * to the new data. Note that we must call dmu_recv_stream() if
4235 * dmu_recv_begin() succeeds.
4237 if (recvprops
!= NULL
&& !drc
.drc_newfs
) {
4238 if (spa_version(dsl_dataset_get_spa(drc
.drc_ds
)) >=
4239 SPA_VERSION_RECVD_PROPS
&&
4240 !dsl_prop_get_hasrecvd(tofs
))
4241 first_recvd_props
= B_TRUE
;
4244 * If new received properties are supplied, they are to
4245 * completely replace the existing received properties, so stash
4246 * away the existing ones.
4248 if (dsl_prop_get_received(tofs
, &origrecvd
) == 0) {
4249 nvlist_t
*errlist
= NULL
;
4251 * Don't bother writing a property if its value won't
4252 * change (and avoid the unnecessary security checks).
4254 * The first receive after SPA_VERSION_RECVD_PROPS is a
4255 * special case where we blow away all local properties
4258 if (!first_recvd_props
)
4259 props_reduce(recvprops
, origrecvd
);
4260 if (zfs_check_clearable(tofs
, origrecvd
, &errlist
) != 0)
4261 (void) nvlist_merge(*errors
, errlist
, 0);
4262 nvlist_free(errlist
);
4264 if (clear_received_props(tofs
, origrecvd
,
4265 first_recvd_props
? NULL
: recvprops
) != 0)
4266 *errflags
|= ZPROP_ERR_NOCLEAR
;
4268 *errflags
|= ZPROP_ERR_NOCLEAR
;
4273 * Stash away existing properties so we can restore them on error unless
4274 * we're doing the first receive after SPA_VERSION_RECVD_PROPS, in which
4275 * case "origrecvd" will take care of that.
4277 if (localprops
!= NULL
&& !drc
.drc_newfs
&& !first_recvd_props
) {
4279 if (dmu_objset_hold(tofs
, FTAG
, &os
) == 0) {
4280 if (dsl_prop_get_all(os
, &origprops
) != 0) {
4281 *errflags
|= ZPROP_ERR_NOCLEAR
;
4283 dmu_objset_rele(os
, FTAG
);
4285 *errflags
|= ZPROP_ERR_NOCLEAR
;
4289 if (recvprops
!= NULL
) {
4290 props_error
= dsl_prop_set_hasrecvd(tofs
);
4292 if (props_error
== 0) {
4293 delayprops
= extract_delay_props(recvprops
);
4294 (void) zfs_set_prop_nvlist(tofs
, ZPROP_SRC_RECEIVED
,
4295 recvprops
, *errors
);
4299 if (localprops
!= NULL
) {
4300 nvlist_t
*oprops
= fnvlist_alloc();
4301 nvlist_t
*xprops
= fnvlist_alloc();
4302 nvpair_t
*nvp
= NULL
;
4304 while ((nvp
= nvlist_next_nvpair(localprops
, nvp
)) != NULL
) {
4305 if (nvpair_type(nvp
) == DATA_TYPE_BOOLEAN
) {
4307 const char *name
= nvpair_name(nvp
);
4308 zfs_prop_t prop
= zfs_name_to_prop(name
);
4309 if (prop
!= ZPROP_INVAL
) {
4310 if (!zfs_prop_inheritable(prop
))
4312 } else if (!zfs_prop_user(name
))
4314 fnvlist_add_boolean(xprops
, name
);
4316 /* -o property=value */
4317 fnvlist_add_nvpair(oprops
, nvp
);
4320 (void) zfs_set_prop_nvlist(tofs
, ZPROP_SRC_LOCAL
,
4322 (void) zfs_set_prop_nvlist(tofs
, ZPROP_SRC_INHERITED
,
4325 nvlist_free(oprops
);
4326 nvlist_free(xprops
);
4329 off
= input_fp
->f_offset
;
4330 error
= dmu_recv_stream(&drc
, input_fp
->f_vnode
, &off
, cleanup_fd
,
4334 zfsvfs_t
*zfsvfs
= NULL
;
4335 zvol_state_t
*zv
= NULL
;
4337 if (getzfsvfs(tofs
, &zfsvfs
) == 0) {
4342 ds
= dmu_objset_ds(zfsvfs
->z_os
);
4343 error
= zfs_suspend_fs(zfsvfs
);
4345 * If the suspend fails, then the recv_end will
4346 * likely also fail, and clean up after itself.
4348 end_err
= dmu_recv_end(&drc
, zfsvfs
);
4350 error
= zfs_resume_fs(zfsvfs
, ds
);
4351 error
= error
? error
: end_err
;
4352 deactivate_super(zfsvfs
->z_sb
);
4353 } else if ((zv
= zvol_suspend(tofs
)) != NULL
) {
4354 error
= dmu_recv_end(&drc
, zvol_tag(zv
));
4357 error
= dmu_recv_end(&drc
, NULL
);
4360 /* Set delayed properties now, after we're done receiving. */
4361 if (delayprops
!= NULL
&& error
== 0) {
4362 (void) zfs_set_prop_nvlist(tofs
, ZPROP_SRC_RECEIVED
,
4363 delayprops
, *errors
);
4367 if (delayprops
!= NULL
) {
4369 * Merge delayed props back in with initial props, in case
4370 * we're DEBUG and zfs_ioc_recv_inject_err is set (which means
4371 * we have to make sure clear_received_props() includes
4372 * the delayed properties).
4374 * Since zfs_ioc_recv_inject_err is only in DEBUG kernels,
4375 * using ASSERT() will be just like a VERIFY.
4377 ASSERT(nvlist_merge(recvprops
, delayprops
, 0) == 0);
4378 nvlist_free(delayprops
);
4382 *read_bytes
= off
- input_fp
->f_offset
;
4383 if (VOP_SEEK(input_fp
->f_vnode
, input_fp
->f_offset
, &off
, NULL
) == 0)
4384 input_fp
->f_offset
= off
;
4387 if (zfs_ioc_recv_inject_err
) {
4388 zfs_ioc_recv_inject_err
= B_FALSE
;
4394 * On error, restore the original props.
4396 if (error
!= 0 && recvprops
!= NULL
&& !drc
.drc_newfs
) {
4397 if (clear_received_props(tofs
, recvprops
, NULL
) != 0) {
4399 * We failed to clear the received properties.
4400 * Since we may have left a $recvd value on the
4401 * system, we can't clear the $hasrecvd flag.
4403 *errflags
|= ZPROP_ERR_NORESTORE
;
4404 } else if (first_recvd_props
) {
4405 dsl_prop_unset_hasrecvd(tofs
);
4408 if (origrecvd
== NULL
&& !drc
.drc_newfs
) {
4409 /* We failed to stash the original properties. */
4410 *errflags
|= ZPROP_ERR_NORESTORE
;
4414 * dsl_props_set() will not convert RECEIVED to LOCAL on or
4415 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
4416 * explicitly if we're restoring local properties cleared in the
4417 * first new-style receive.
4419 if (origrecvd
!= NULL
&&
4420 zfs_set_prop_nvlist(tofs
, (first_recvd_props
?
4421 ZPROP_SRC_LOCAL
: ZPROP_SRC_RECEIVED
),
4422 origrecvd
, NULL
) != 0) {
4424 * We stashed the original properties but failed to
4427 *errflags
|= ZPROP_ERR_NORESTORE
;
4430 if (error
!= 0 && localprops
!= NULL
&& !drc
.drc_newfs
&&
4431 !first_recvd_props
) {
4433 nvlist_t
*inheritprops
;
4436 if (origprops
== NULL
) {
4437 /* We failed to stash the original properties. */
4438 *errflags
|= ZPROP_ERR_NORESTORE
;
4442 /* Restore original props */
4443 setprops
= fnvlist_alloc();
4444 inheritprops
= fnvlist_alloc();
4446 while ((nvp
= nvlist_next_nvpair(localprops
, nvp
)) != NULL
) {
4447 const char *name
= nvpair_name(nvp
);
4451 if (!nvlist_exists(origprops
, name
)) {
4453 * Property was not present or was explicitly
4454 * inherited before the receive, restore this.
4456 fnvlist_add_boolean(inheritprops
, name
);
4459 attrs
= fnvlist_lookup_nvlist(origprops
, name
);
4460 source
= fnvlist_lookup_string(attrs
, ZPROP_SOURCE
);
4462 /* Skip received properties */
4463 if (strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) == 0)
4466 if (strcmp(source
, tofs
) == 0) {
4467 /* Property was locally set */
4468 fnvlist_add_nvlist(setprops
, name
, attrs
);
4470 /* Property was implicitly inherited */
4471 fnvlist_add_boolean(inheritprops
, name
);
4475 if (zfs_set_prop_nvlist(tofs
, ZPROP_SRC_LOCAL
, setprops
,
4477 *errflags
|= ZPROP_ERR_NORESTORE
;
4478 if (zfs_set_prop_nvlist(tofs
, ZPROP_SRC_INHERITED
, inheritprops
,
4480 *errflags
|= ZPROP_ERR_NORESTORE
;
4482 nvlist_free(setprops
);
4483 nvlist_free(inheritprops
);
4487 nvlist_free(origrecvd
);
4488 nvlist_free(origprops
);
4491 error
= props_error
;
4498 * zc_name name of containing filesystem (unused)
4499 * zc_nvlist_src{_size} nvlist of properties to apply
4500 * zc_nvlist_conf{_size} nvlist of properties to exclude
4501 * (DATA_TYPE_BOOLEAN) and override (everything else)
4502 * zc_value name of snapshot to create
4503 * zc_string name of clone origin (if DRR_FLAG_CLONE)
4504 * zc_cookie file descriptor to recv from
4505 * zc_begin_record the BEGIN record of the stream (not byteswapped)
4506 * zc_guid force flag
4507 * zc_cleanup_fd cleanup-on-exit file descriptor
4508 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
4511 * zc_cookie number of bytes read
4512 * zc_obj zprop_errflags_t
4513 * zc_action_handle handle for this guid/ds mapping
4514 * zc_nvlist_dst{_size} error for each unapplied received property
4517 zfs_ioc_recv(zfs_cmd_t
*zc
)
4519 dmu_replay_record_t begin_record
;
4520 nvlist_t
*errors
= NULL
;
4521 nvlist_t
*recvdprops
= NULL
;
4522 nvlist_t
*localprops
= NULL
;
4523 char *origin
= NULL
;
4525 char tofs
[ZFS_MAX_DATASET_NAME_LEN
];
4528 if (dataset_namecheck(zc
->zc_value
, NULL
, NULL
) != 0 ||
4529 strchr(zc
->zc_value
, '@') == NULL
||
4530 strchr(zc
->zc_value
, '%'))
4531 return (SET_ERROR(EINVAL
));
4533 (void) strlcpy(tofs
, zc
->zc_value
, sizeof (tofs
));
4534 tosnap
= strchr(tofs
, '@');
4537 if (zc
->zc_nvlist_src
!= 0 &&
4538 (error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
4539 zc
->zc_iflags
, &recvdprops
)) != 0)
4542 if (zc
->zc_nvlist_conf
!= 0 &&
4543 (error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
4544 zc
->zc_iflags
, &localprops
)) != 0)
4547 if (zc
->zc_string
[0])
4548 origin
= zc
->zc_string
;
4550 begin_record
.drr_type
= DRR_BEGIN
;
4551 begin_record
.drr_payloadlen
= 0;
4552 begin_record
.drr_u
.drr_begin
= zc
->zc_begin_record
;
4554 error
= zfs_ioc_recv_impl(tofs
, tosnap
, origin
, recvdprops
, localprops
,
4555 zc
->zc_guid
, B_FALSE
, zc
->zc_cookie
, &begin_record
,
4556 zc
->zc_cleanup_fd
, &zc
->zc_cookie
, &zc
->zc_obj
,
4557 &zc
->zc_action_handle
, &errors
);
4558 nvlist_free(recvdprops
);
4559 nvlist_free(localprops
);
4562 * Now that all props, initial and delayed, are set, report the prop
4563 * errors to the caller.
4565 if (zc
->zc_nvlist_dst_size
!= 0 && errors
!= NULL
&&
4566 (nvlist_smush(errors
, zc
->zc_nvlist_dst_size
) != 0 ||
4567 put_nvlist(zc
, errors
) != 0)) {
4569 * Caller made zc->zc_nvlist_dst less than the minimum expected
4570 * size or supplied an invalid address.
4572 error
= SET_ERROR(EINVAL
);
4575 nvlist_free(errors
);
4582 * "snapname" -> full name of the snapshot to create
4583 * (optional) "props" -> received properties to set (nvlist)
4584 * (optional) "localprops" -> override and exclude properties (nvlist)
4585 * (optional) "origin" -> name of clone origin (DRR_FLAG_CLONE)
4586 * "begin_record" -> non-byteswapped dmu_replay_record_t
4587 * "input_fd" -> file descriptor to read stream from (int32)
4588 * (optional) "force" -> force flag (value ignored)
4589 * (optional) "resumable" -> resumable flag (value ignored)
4590 * (optional) "cleanup_fd" -> cleanup-on-exit file descriptor
4591 * (optional) "action_handle" -> handle for this guid/ds mapping
4595 * "read_bytes" -> number of bytes read
4596 * "error_flags" -> zprop_errflags_t
4597 * "action_handle" -> handle for this guid/ds mapping
4598 * "errors" -> error for each unapplied received property (nvlist)
4602 zfs_ioc_recv_new(const char *fsname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
4604 dmu_replay_record_t
*begin_record
;
4605 uint_t begin_record_size
;
4606 nvlist_t
*errors
= NULL
;
4607 nvlist_t
*recvprops
= NULL
;
4608 nvlist_t
*localprops
= NULL
;
4609 char *snapname
= NULL
;
4610 char *origin
= NULL
;
4612 char tofs
[ZFS_MAX_DATASET_NAME_LEN
];
4614 boolean_t resumable
;
4615 uint64_t action_handle
= 0;
4616 uint64_t read_bytes
= 0;
4617 uint64_t errflags
= 0;
4619 int cleanup_fd
= -1;
4622 error
= nvlist_lookup_string(innvl
, "snapname", &snapname
);
4624 return (SET_ERROR(EINVAL
));
4626 if (dataset_namecheck(snapname
, NULL
, NULL
) != 0 ||
4627 strchr(snapname
, '@') == NULL
||
4628 strchr(snapname
, '%'))
4629 return (SET_ERROR(EINVAL
));
4631 (void) strcpy(tofs
, snapname
);
4632 tosnap
= strchr(tofs
, '@');
4635 error
= nvlist_lookup_string(innvl
, "origin", &origin
);
4636 if (error
&& error
!= ENOENT
)
4639 error
= nvlist_lookup_byte_array(innvl
, "begin_record",
4640 (uchar_t
**)&begin_record
, &begin_record_size
);
4641 if (error
!= 0 || begin_record_size
!= sizeof (*begin_record
))
4642 return (SET_ERROR(EINVAL
));
4644 error
= nvlist_lookup_int32(innvl
, "input_fd", &input_fd
);
4646 return (SET_ERROR(EINVAL
));
4648 force
= nvlist_exists(innvl
, "force");
4649 resumable
= nvlist_exists(innvl
, "resumable");
4651 error
= nvlist_lookup_int32(innvl
, "cleanup_fd", &cleanup_fd
);
4652 if (error
&& error
!= ENOENT
)
4655 error
= nvlist_lookup_uint64(innvl
, "action_handle", &action_handle
);
4656 if (error
&& error
!= ENOENT
)
4659 /* we still use "props" here for backwards compatibility */
4660 error
= nvlist_lookup_nvlist(innvl
, "props", &recvprops
);
4661 if (error
&& error
!= ENOENT
)
4664 error
= nvlist_lookup_nvlist(innvl
, "localprops", &localprops
);
4665 if (error
&& error
!= ENOENT
)
4668 error
= zfs_ioc_recv_impl(tofs
, tosnap
, origin
, recvprops
, localprops
,
4669 force
, resumable
, input_fd
, begin_record
, cleanup_fd
, &read_bytes
,
4670 &errflags
, &action_handle
, &errors
);
4672 fnvlist_add_uint64(outnvl
, "read_bytes", read_bytes
);
4673 fnvlist_add_uint64(outnvl
, "error_flags", errflags
);
4674 fnvlist_add_uint64(outnvl
, "action_handle", action_handle
);
4675 fnvlist_add_nvlist(outnvl
, "errors", errors
);
4677 nvlist_free(errors
);
4678 nvlist_free(recvprops
);
4679 nvlist_free(localprops
);
4686 * zc_name name of snapshot to send
4687 * zc_cookie file descriptor to send stream to
4688 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
4689 * zc_sendobj objsetid of snapshot to send
4690 * zc_fromobj objsetid of incremental fromsnap (may be zero)
4691 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
4692 * output size in zc_objset_type.
4693 * zc_flags lzc_send_flags
4696 * zc_objset_type estimated size, if zc_guid is set
4699 zfs_ioc_send(zfs_cmd_t
*zc
)
4703 boolean_t estimate
= (zc
->zc_guid
!= 0);
4704 boolean_t embedok
= (zc
->zc_flags
& 0x1);
4705 boolean_t large_block_ok
= (zc
->zc_flags
& 0x2);
4706 boolean_t compressok
= (zc
->zc_flags
& 0x4);
4708 if (zc
->zc_obj
!= 0) {
4710 dsl_dataset_t
*tosnap
;
4712 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
4716 error
= dsl_dataset_hold_obj(dp
, zc
->zc_sendobj
, FTAG
, &tosnap
);
4718 dsl_pool_rele(dp
, FTAG
);
4722 if (dsl_dir_is_clone(tosnap
->ds_dir
))
4724 dsl_dir_phys(tosnap
->ds_dir
)->dd_origin_obj
;
4725 dsl_dataset_rele(tosnap
, FTAG
);
4726 dsl_pool_rele(dp
, FTAG
);
4731 dsl_dataset_t
*tosnap
;
4732 dsl_dataset_t
*fromsnap
= NULL
;
4734 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
4738 error
= dsl_dataset_hold_obj(dp
, zc
->zc_sendobj
, FTAG
, &tosnap
);
4740 dsl_pool_rele(dp
, FTAG
);
4744 if (zc
->zc_fromobj
!= 0) {
4745 error
= dsl_dataset_hold_obj(dp
, zc
->zc_fromobj
,
4748 dsl_dataset_rele(tosnap
, FTAG
);
4749 dsl_pool_rele(dp
, FTAG
);
4754 error
= dmu_send_estimate(tosnap
, fromsnap
, compressok
,
4755 &zc
->zc_objset_type
);
4757 if (fromsnap
!= NULL
)
4758 dsl_dataset_rele(fromsnap
, FTAG
);
4759 dsl_dataset_rele(tosnap
, FTAG
);
4760 dsl_pool_rele(dp
, FTAG
);
4762 file_t
*fp
= getf(zc
->zc_cookie
);
4764 return (SET_ERROR(EBADF
));
4767 error
= dmu_send_obj(zc
->zc_name
, zc
->zc_sendobj
,
4768 zc
->zc_fromobj
, embedok
, large_block_ok
, compressok
,
4769 zc
->zc_cookie
, fp
->f_vnode
, &off
);
4771 if (VOP_SEEK(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
4773 releasef(zc
->zc_cookie
);
4780 * zc_name name of snapshot on which to report progress
4781 * zc_cookie file descriptor of send stream
4784 * zc_cookie number of bytes written in send stream thus far
4787 zfs_ioc_send_progress(zfs_cmd_t
*zc
)
4791 dmu_sendarg_t
*dsp
= NULL
;
4794 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
4798 error
= dsl_dataset_hold(dp
, zc
->zc_name
, FTAG
, &ds
);
4800 dsl_pool_rele(dp
, FTAG
);
4804 mutex_enter(&ds
->ds_sendstream_lock
);
4807 * Iterate over all the send streams currently active on this dataset.
4808 * If there's one which matches the specified file descriptor _and_ the
4809 * stream was started by the current process, return the progress of
4813 for (dsp
= list_head(&ds
->ds_sendstreams
); dsp
!= NULL
;
4814 dsp
= list_next(&ds
->ds_sendstreams
, dsp
)) {
4815 if (dsp
->dsa_outfd
== zc
->zc_cookie
&&
4816 dsp
->dsa_proc
->group_leader
== curproc
->group_leader
)
4821 zc
->zc_cookie
= *(dsp
->dsa_off
);
4823 error
= SET_ERROR(ENOENT
);
4825 mutex_exit(&ds
->ds_sendstream_lock
);
4826 dsl_dataset_rele(ds
, FTAG
);
4827 dsl_pool_rele(dp
, FTAG
);
4832 zfs_ioc_inject_fault(zfs_cmd_t
*zc
)
4836 error
= zio_inject_fault(zc
->zc_name
, (int)zc
->zc_guid
, &id
,
4837 &zc
->zc_inject_record
);
4840 zc
->zc_guid
= (uint64_t)id
;
4846 zfs_ioc_clear_fault(zfs_cmd_t
*zc
)
4848 return (zio_clear_fault((int)zc
->zc_guid
));
4852 zfs_ioc_inject_list_next(zfs_cmd_t
*zc
)
4854 int id
= (int)zc
->zc_guid
;
4857 error
= zio_inject_list_next(&id
, zc
->zc_name
, sizeof (zc
->zc_name
),
4858 &zc
->zc_inject_record
);
4866 zfs_ioc_error_log(zfs_cmd_t
*zc
)
4870 size_t count
= (size_t)zc
->zc_nvlist_dst_size
;
4872 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
4875 error
= spa_get_errlog(spa
, (void *)(uintptr_t)zc
->zc_nvlist_dst
,
4878 zc
->zc_nvlist_dst_size
= count
;
4880 zc
->zc_nvlist_dst_size
= spa_get_errlog_size(spa
);
4882 spa_close(spa
, FTAG
);
4888 zfs_ioc_clear(zfs_cmd_t
*zc
)
4895 * On zpool clear we also fix up missing slogs
4897 mutex_enter(&spa_namespace_lock
);
4898 spa
= spa_lookup(zc
->zc_name
);
4900 mutex_exit(&spa_namespace_lock
);
4901 return (SET_ERROR(EIO
));
4903 if (spa_get_log_state(spa
) == SPA_LOG_MISSING
) {
4904 /* we need to let spa_open/spa_load clear the chains */
4905 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
4907 spa
->spa_last_open_failed
= 0;
4908 mutex_exit(&spa_namespace_lock
);
4910 if (zc
->zc_cookie
& ZPOOL_NO_REWIND
) {
4911 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
4914 nvlist_t
*config
= NULL
;
4916 if (zc
->zc_nvlist_src
== 0)
4917 return (SET_ERROR(EINVAL
));
4919 if ((error
= get_nvlist(zc
->zc_nvlist_src
,
4920 zc
->zc_nvlist_src_size
, zc
->zc_iflags
, &policy
)) == 0) {
4921 error
= spa_open_rewind(zc
->zc_name
, &spa
, FTAG
,
4923 if (config
!= NULL
) {
4926 if ((err
= put_nvlist(zc
, config
)) != 0)
4928 nvlist_free(config
);
4930 nvlist_free(policy
);
4937 spa_vdev_state_enter(spa
, SCL_NONE
);
4939 if (zc
->zc_guid
== 0) {
4942 vd
= spa_lookup_by_guid(spa
, zc
->zc_guid
, B_TRUE
);
4944 (void) spa_vdev_state_exit(spa
, NULL
, ENODEV
);
4945 spa_close(spa
, FTAG
);
4946 return (SET_ERROR(ENODEV
));
4950 vdev_clear(spa
, vd
);
4952 (void) spa_vdev_state_exit(spa
, spa_suspended(spa
) ?
4953 NULL
: spa
->spa_root_vdev
, 0);
4956 * Resume any suspended I/Os.
4958 if (zio_resume(spa
) != 0)
4959 error
= SET_ERROR(EIO
);
4961 spa_close(spa
, FTAG
);
4967 zfs_ioc_pool_reopen(zfs_cmd_t
*zc
)
4972 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
4976 spa_vdev_state_enter(spa
, SCL_NONE
);
4979 * If a resilver is already in progress then set the
4980 * spa_scrub_reopen flag to B_TRUE so that we don't restart
4981 * the scan as a side effect of the reopen. Otherwise, let
4982 * vdev_open() decided if a resilver is required.
4984 spa
->spa_scrub_reopen
= dsl_scan_resilvering(spa
->spa_dsl_pool
);
4985 vdev_reopen(spa
->spa_root_vdev
);
4986 spa
->spa_scrub_reopen
= B_FALSE
;
4988 (void) spa_vdev_state_exit(spa
, NULL
, 0);
4989 spa_close(spa
, FTAG
);
4994 * zc_name name of filesystem
4997 * zc_string name of conflicting snapshot, if there is one
5000 zfs_ioc_promote(zfs_cmd_t
*zc
)
5003 dsl_dataset_t
*ds
, *ods
;
5004 char origin
[ZFS_MAX_DATASET_NAME_LEN
];
5008 zc
->zc_name
[sizeof (zc
->zc_name
) - 1] = '\0';
5009 if (dataset_namecheck(zc
->zc_name
, NULL
, NULL
) != 0 ||
5010 strchr(zc
->zc_name
, '%'))
5011 return (SET_ERROR(EINVAL
));
5013 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
5017 error
= dsl_dataset_hold(dp
, zc
->zc_name
, FTAG
, &ds
);
5019 dsl_pool_rele(dp
, FTAG
);
5023 if (!dsl_dir_is_clone(ds
->ds_dir
)) {
5024 dsl_dataset_rele(ds
, FTAG
);
5025 dsl_pool_rele(dp
, FTAG
);
5026 return (SET_ERROR(EINVAL
));
5029 error
= dsl_dataset_hold_obj(dp
,
5030 dsl_dir_phys(ds
->ds_dir
)->dd_origin_obj
, FTAG
, &ods
);
5032 dsl_dataset_rele(ds
, FTAG
);
5033 dsl_pool_rele(dp
, FTAG
);
5037 dsl_dataset_name(ods
, origin
);
5038 dsl_dataset_rele(ods
, FTAG
);
5039 dsl_dataset_rele(ds
, FTAG
);
5040 dsl_pool_rele(dp
, FTAG
);
5043 * We don't need to unmount *all* the origin fs's snapshots, but
5046 cp
= strchr(origin
, '@');
5049 (void) dmu_objset_find(origin
,
5050 zfs_unmount_snap_cb
, NULL
, DS_FIND_SNAPSHOTS
);
5051 return (dsl_dataset_promote(zc
->zc_name
, zc
->zc_string
));
5055 * Retrieve a single {user|group}{used|quota}@... property.
5058 * zc_name name of filesystem
5059 * zc_objset_type zfs_userquota_prop_t
5060 * zc_value domain name (eg. "S-1-234-567-89")
5061 * zc_guid RID/UID/GID
5064 * zc_cookie property value
5067 zfs_ioc_userspace_one(zfs_cmd_t
*zc
)
5072 if (zc
->zc_objset_type
>= ZFS_NUM_USERQUOTA_PROPS
)
5073 return (SET_ERROR(EINVAL
));
5075 error
= zfsvfs_hold(zc
->zc_name
, FTAG
, &zfsvfs
, B_FALSE
);
5079 error
= zfs_userspace_one(zfsvfs
,
5080 zc
->zc_objset_type
, zc
->zc_value
, zc
->zc_guid
, &zc
->zc_cookie
);
5081 zfsvfs_rele(zfsvfs
, FTAG
);
5088 * zc_name name of filesystem
5089 * zc_cookie zap cursor
5090 * zc_objset_type zfs_userquota_prop_t
5091 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
5094 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
5095 * zc_cookie zap cursor
5098 zfs_ioc_userspace_many(zfs_cmd_t
*zc
)
5101 int bufsize
= zc
->zc_nvlist_dst_size
;
5106 return (SET_ERROR(ENOMEM
));
5108 error
= zfsvfs_hold(zc
->zc_name
, FTAG
, &zfsvfs
, B_FALSE
);
5112 buf
= vmem_alloc(bufsize
, KM_SLEEP
);
5114 error
= zfs_userspace_many(zfsvfs
, zc
->zc_objset_type
, &zc
->zc_cookie
,
5115 buf
, &zc
->zc_nvlist_dst_size
);
5118 error
= xcopyout(buf
,
5119 (void *)(uintptr_t)zc
->zc_nvlist_dst
,
5120 zc
->zc_nvlist_dst_size
);
5122 vmem_free(buf
, bufsize
);
5123 zfsvfs_rele(zfsvfs
, FTAG
);
5130 * zc_name name of filesystem
5136 zfs_ioc_userspace_upgrade(zfs_cmd_t
*zc
)
5142 if (getzfsvfs(zc
->zc_name
, &zfsvfs
) == 0) {
5143 if (!dmu_objset_userused_enabled(zfsvfs
->z_os
)) {
5145 * If userused is not enabled, it may be because the
5146 * objset needs to be closed & reopened (to grow the
5147 * objset_phys_t). Suspend/resume the fs will do that.
5151 ds
= dmu_objset_ds(zfsvfs
->z_os
);
5152 error
= zfs_suspend_fs(zfsvfs
);
5154 dmu_objset_refresh_ownership(zfsvfs
->z_os
,
5156 error
= zfs_resume_fs(zfsvfs
, ds
);
5160 error
= dmu_objset_userspace_upgrade(zfsvfs
->z_os
);
5161 deactivate_super(zfsvfs
->z_sb
);
5163 /* XXX kind of reading contents without owning */
5164 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
5168 error
= dmu_objset_userspace_upgrade(os
);
5169 dmu_objset_rele(os
, FTAG
);
5177 * zc_name name of filesystem
5183 zfs_ioc_userobjspace_upgrade(zfs_cmd_t
*zc
)
5188 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
5192 dsl_dataset_long_hold(dmu_objset_ds(os
), FTAG
);
5193 dsl_pool_rele(dmu_objset_pool(os
), FTAG
);
5195 if (dmu_objset_userobjspace_upgradable(os
)) {
5196 mutex_enter(&os
->os_upgrade_lock
);
5197 if (os
->os_upgrade_id
== 0) {
5198 /* clear potential error code and retry */
5199 os
->os_upgrade_status
= 0;
5200 mutex_exit(&os
->os_upgrade_lock
);
5202 dmu_objset_userobjspace_upgrade(os
);
5204 mutex_exit(&os
->os_upgrade_lock
);
5207 taskq_wait_id(os
->os_spa
->spa_upgrade_taskq
, os
->os_upgrade_id
);
5208 error
= os
->os_upgrade_status
;
5211 dsl_dataset_long_rele(dmu_objset_ds(os
), FTAG
);
5212 dsl_dataset_rele(dmu_objset_ds(os
), FTAG
);
5218 zfs_ioc_share(zfs_cmd_t
*zc
)
5220 return (SET_ERROR(ENOSYS
));
5223 ace_t full_access
[] = {
5224 {(uid_t
)-1, ACE_ALL_PERMS
, ACE_EVERYONE
, 0}
5229 * zc_name name of containing filesystem
5230 * zc_obj object # beyond which we want next in-use object #
5233 * zc_obj next in-use object #
5236 zfs_ioc_next_obj(zfs_cmd_t
*zc
)
5238 objset_t
*os
= NULL
;
5241 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
5245 error
= dmu_object_next(os
, &zc
->zc_obj
, B_FALSE
, 0);
5247 dmu_objset_rele(os
, FTAG
);
5253 * zc_name name of filesystem
5254 * zc_value prefix name for snapshot
5255 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
5258 * zc_value short name of new snapshot
5261 zfs_ioc_tmp_snapshot(zfs_cmd_t
*zc
)
5268 error
= zfs_onexit_fd_hold(zc
->zc_cleanup_fd
, &minor
);
5272 snap_name
= kmem_asprintf("%s-%016llx", zc
->zc_value
,
5273 (u_longlong_t
)ddi_get_lbolt64());
5274 hold_name
= kmem_asprintf("%%%s", zc
->zc_value
);
5276 error
= dsl_dataset_snapshot_tmp(zc
->zc_name
, snap_name
, minor
,
5279 (void) strlcpy(zc
->zc_value
, snap_name
,
5280 sizeof (zc
->zc_value
));
5283 zfs_onexit_fd_rele(zc
->zc_cleanup_fd
);
5289 * zc_name name of "to" snapshot
5290 * zc_value name of "from" snapshot
5291 * zc_cookie file descriptor to write diff data on
5294 * dmu_diff_record_t's to the file descriptor
5297 zfs_ioc_diff(zfs_cmd_t
*zc
)
5303 fp
= getf(zc
->zc_cookie
);
5305 return (SET_ERROR(EBADF
));
5309 error
= dmu_diff(zc
->zc_name
, zc
->zc_value
, fp
->f_vnode
, &off
);
5311 if (VOP_SEEK(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
5313 releasef(zc
->zc_cookie
);
5319 * Remove all ACL files in shares dir
5321 #ifdef HAVE_SMB_SHARE
5323 zfs_smb_acl_purge(znode_t
*dzp
)
5326 zap_attribute_t zap
;
5327 zfsvfs_t
*zfsvfs
= ZTOZSB(dzp
);
5330 for (zap_cursor_init(&zc
, zfsvfs
->z_os
, dzp
->z_id
);
5331 (error
= zap_cursor_retrieve(&zc
, &zap
)) == 0;
5332 zap_cursor_advance(&zc
)) {
5333 if ((error
= VOP_REMOVE(ZTOV(dzp
), zap
.za_name
, kcred
,
5337 zap_cursor_fini(&zc
);
5340 #endif /* HAVE_SMB_SHARE */
5343 zfs_ioc_smb_acl(zfs_cmd_t
*zc
)
5345 #ifdef HAVE_SMB_SHARE
5348 vnode_t
*resourcevp
= NULL
;
5357 if ((error
= lookupname(zc
->zc_value
, UIO_SYSSPACE
,
5358 NO_FOLLOW
, NULL
, &vp
)) != 0)
5361 /* Now make sure mntpnt and dataset are ZFS */
5363 if (vp
->v_vfsp
->vfs_fstype
!= zfsfstype
||
5364 (strcmp((char *)refstr_value(vp
->v_vfsp
->vfs_resource
),
5365 zc
->zc_name
) != 0)) {
5367 return (SET_ERROR(EINVAL
));
5371 zfsvfs
= ZTOZSB(dzp
);
5375 * Create share dir if its missing.
5377 mutex_enter(&zfsvfs
->z_lock
);
5378 if (zfsvfs
->z_shares_dir
== 0) {
5381 tx
= dmu_tx_create(zfsvfs
->z_os
);
5382 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
, TRUE
,
5384 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, FALSE
, NULL
);
5385 error
= dmu_tx_assign(tx
, TXG_WAIT
);
5389 error
= zfs_create_share_dir(zfsvfs
, tx
);
5393 mutex_exit(&zfsvfs
->z_lock
);
5399 mutex_exit(&zfsvfs
->z_lock
);
5401 ASSERT(zfsvfs
->z_shares_dir
);
5402 if ((error
= zfs_zget(zfsvfs
, zfsvfs
->z_shares_dir
, &sharedir
)) != 0) {
5408 switch (zc
->zc_cookie
) {
5409 case ZFS_SMB_ACL_ADD
:
5410 vattr
.va_mask
= AT_MODE
|AT_UID
|AT_GID
|AT_TYPE
;
5411 vattr
.va_mode
= S_IFREG
|0777;
5415 vsec
.vsa_mask
= VSA_ACE
;
5416 vsec
.vsa_aclentp
= &full_access
;
5417 vsec
.vsa_aclentsz
= sizeof (full_access
);
5418 vsec
.vsa_aclcnt
= 1;
5420 error
= VOP_CREATE(ZTOV(sharedir
), zc
->zc_string
,
5421 &vattr
, EXCL
, 0, &resourcevp
, kcred
, 0, NULL
, &vsec
);
5423 VN_RELE(resourcevp
);
5426 case ZFS_SMB_ACL_REMOVE
:
5427 error
= VOP_REMOVE(ZTOV(sharedir
), zc
->zc_string
, kcred
,
5431 case ZFS_SMB_ACL_RENAME
:
5432 if ((error
= get_nvlist(zc
->zc_nvlist_src
,
5433 zc
->zc_nvlist_src_size
, zc
->zc_iflags
, &nvlist
)) != 0) {
5435 VN_RELE(ZTOV(sharedir
));
5439 if (nvlist_lookup_string(nvlist
, ZFS_SMB_ACL_SRC
, &src
) ||
5440 nvlist_lookup_string(nvlist
, ZFS_SMB_ACL_TARGET
,
5443 VN_RELE(ZTOV(sharedir
));
5445 nvlist_free(nvlist
);
5448 error
= VOP_RENAME(ZTOV(sharedir
), src
, ZTOV(sharedir
), target
,
5450 nvlist_free(nvlist
);
5453 case ZFS_SMB_ACL_PURGE
:
5454 error
= zfs_smb_acl_purge(sharedir
);
5458 error
= SET_ERROR(EINVAL
);
5463 VN_RELE(ZTOV(sharedir
));
5469 return (SET_ERROR(ENOTSUP
));
5470 #endif /* HAVE_SMB_SHARE */
5475 * "holds" -> { snapname -> holdname (string), ... }
5476 * (optional) "cleanup_fd" -> fd (int32)
5480 * snapname -> error value (int32)
5486 zfs_ioc_hold(const char *pool
, nvlist_t
*args
, nvlist_t
*errlist
)
5490 int cleanup_fd
= -1;
5494 error
= nvlist_lookup_nvlist(args
, "holds", &holds
);
5496 return (SET_ERROR(EINVAL
));
5498 /* make sure the user didn't pass us any invalid (empty) tags */
5499 for (pair
= nvlist_next_nvpair(holds
, NULL
); pair
!= NULL
;
5500 pair
= nvlist_next_nvpair(holds
, pair
)) {
5503 error
= nvpair_value_string(pair
, &htag
);
5505 return (SET_ERROR(error
));
5507 if (strlen(htag
) == 0)
5508 return (SET_ERROR(EINVAL
));
5511 if (nvlist_lookup_int32(args
, "cleanup_fd", &cleanup_fd
) == 0) {
5512 error
= zfs_onexit_fd_hold(cleanup_fd
, &minor
);
5517 error
= dsl_dataset_user_hold(holds
, minor
, errlist
);
5519 zfs_onexit_fd_rele(cleanup_fd
);
5524 * innvl is not used.
5527 * holdname -> time added (uint64 seconds since epoch)
5533 zfs_ioc_get_holds(const char *snapname
, nvlist_t
*args
, nvlist_t
*outnvl
)
5535 ASSERT3P(args
, ==, NULL
);
5536 return (dsl_dataset_get_holds(snapname
, outnvl
));
5541 * snapname -> { holdname, ... }
5546 * snapname -> error value (int32)
5552 zfs_ioc_release(const char *pool
, nvlist_t
*holds
, nvlist_t
*errlist
)
5554 return (dsl_dataset_user_release(holds
, errlist
));
5559 * zc_guid flags (ZEVENT_NONBLOCK)
5560 * zc_cleanup_fd zevent file descriptor
5563 * zc_nvlist_dst next nvlist event
5564 * zc_cookie dropped events since last get
5567 zfs_ioc_events_next(zfs_cmd_t
*zc
)
5570 nvlist_t
*event
= NULL
;
5572 uint64_t dropped
= 0;
5575 error
= zfs_zevent_fd_hold(zc
->zc_cleanup_fd
, &minor
, &ze
);
5580 error
= zfs_zevent_next(ze
, &event
,
5581 &zc
->zc_nvlist_dst_size
, &dropped
);
5582 if (event
!= NULL
) {
5583 zc
->zc_cookie
= dropped
;
5584 error
= put_nvlist(zc
, event
);
5588 if (zc
->zc_guid
& ZEVENT_NONBLOCK
)
5591 if ((error
== 0) || (error
!= ENOENT
))
5594 error
= zfs_zevent_wait(ze
);
5599 zfs_zevent_fd_rele(zc
->zc_cleanup_fd
);
5606 * zc_cookie cleared events count
5609 zfs_ioc_events_clear(zfs_cmd_t
*zc
)
5613 zfs_zevent_drain_all(&count
);
5614 zc
->zc_cookie
= count
;
5621 * zc_guid eid | ZEVENT_SEEK_START | ZEVENT_SEEK_END
5622 * zc_cleanup zevent file descriptor
5625 zfs_ioc_events_seek(zfs_cmd_t
*zc
)
5631 error
= zfs_zevent_fd_hold(zc
->zc_cleanup_fd
, &minor
, &ze
);
5635 error
= zfs_zevent_seek(ze
, zc
->zc_guid
);
5636 zfs_zevent_fd_rele(zc
->zc_cleanup_fd
);
5643 * zc_name name of new filesystem or snapshot
5644 * zc_value full name of old snapshot
5647 * zc_cookie space in bytes
5648 * zc_objset_type compressed space in bytes
5649 * zc_perm_action uncompressed space in bytes
5652 zfs_ioc_space_written(zfs_cmd_t
*zc
)
5656 dsl_dataset_t
*new, *old
;
5658 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
5661 error
= dsl_dataset_hold(dp
, zc
->zc_name
, FTAG
, &new);
5663 dsl_pool_rele(dp
, FTAG
);
5666 error
= dsl_dataset_hold(dp
, zc
->zc_value
, FTAG
, &old
);
5668 dsl_dataset_rele(new, FTAG
);
5669 dsl_pool_rele(dp
, FTAG
);
5673 error
= dsl_dataset_space_written(old
, new, &zc
->zc_cookie
,
5674 &zc
->zc_objset_type
, &zc
->zc_perm_action
);
5675 dsl_dataset_rele(old
, FTAG
);
5676 dsl_dataset_rele(new, FTAG
);
5677 dsl_pool_rele(dp
, FTAG
);
5683 * "firstsnap" -> snapshot name
5687 * "used" -> space in bytes
5688 * "compressed" -> compressed space in bytes
5689 * "uncompressed" -> uncompressed space in bytes
5693 zfs_ioc_space_snaps(const char *lastsnap
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
5697 dsl_dataset_t
*new, *old
;
5699 uint64_t used
, comp
, uncomp
;
5701 if (nvlist_lookup_string(innvl
, "firstsnap", &firstsnap
) != 0)
5702 return (SET_ERROR(EINVAL
));
5704 error
= dsl_pool_hold(lastsnap
, FTAG
, &dp
);
5708 error
= dsl_dataset_hold(dp
, lastsnap
, FTAG
, &new);
5709 if (error
== 0 && !new->ds_is_snapshot
) {
5710 dsl_dataset_rele(new, FTAG
);
5711 error
= SET_ERROR(EINVAL
);
5714 dsl_pool_rele(dp
, FTAG
);
5717 error
= dsl_dataset_hold(dp
, firstsnap
, FTAG
, &old
);
5718 if (error
== 0 && !old
->ds_is_snapshot
) {
5719 dsl_dataset_rele(old
, FTAG
);
5720 error
= SET_ERROR(EINVAL
);
5723 dsl_dataset_rele(new, FTAG
);
5724 dsl_pool_rele(dp
, FTAG
);
5728 error
= dsl_dataset_space_wouldfree(old
, new, &used
, &comp
, &uncomp
);
5729 dsl_dataset_rele(old
, FTAG
);
5730 dsl_dataset_rele(new, FTAG
);
5731 dsl_pool_rele(dp
, FTAG
);
5732 fnvlist_add_uint64(outnvl
, "used", used
);
5733 fnvlist_add_uint64(outnvl
, "compressed", comp
);
5734 fnvlist_add_uint64(outnvl
, "uncompressed", uncomp
);
5740 * "fd" -> file descriptor to write stream to (int32)
5741 * (optional) "fromsnap" -> full snap name to send an incremental from
5742 * (optional) "largeblockok" -> (value ignored)
5743 * indicates that blocks > 128KB are permitted
5744 * (optional) "embedok" -> (value ignored)
5745 * presence indicates DRR_WRITE_EMBEDDED records are permitted
5746 * (optional) "compressok" -> (value ignored)
5747 * presence indicates compressed DRR_WRITE records are permitted
5748 * (optional) "resume_object" and "resume_offset" -> (uint64)
5749 * if present, resume send stream from specified object and offset.
5756 zfs_ioc_send_new(const char *snapname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
5760 char *fromname
= NULL
;
5763 boolean_t largeblockok
;
5765 boolean_t compressok
;
5766 uint64_t resumeobj
= 0;
5767 uint64_t resumeoff
= 0;
5769 error
= nvlist_lookup_int32(innvl
, "fd", &fd
);
5771 return (SET_ERROR(EINVAL
));
5773 (void) nvlist_lookup_string(innvl
, "fromsnap", &fromname
);
5775 largeblockok
= nvlist_exists(innvl
, "largeblockok");
5776 embedok
= nvlist_exists(innvl
, "embedok");
5777 compressok
= nvlist_exists(innvl
, "compressok");
5779 (void) nvlist_lookup_uint64(innvl
, "resume_object", &resumeobj
);
5780 (void) nvlist_lookup_uint64(innvl
, "resume_offset", &resumeoff
);
5782 if ((fp
= getf(fd
)) == NULL
)
5783 return (SET_ERROR(EBADF
));
5786 error
= dmu_send(snapname
, fromname
, embedok
, largeblockok
, compressok
,
5787 fd
, resumeobj
, resumeoff
, fp
->f_vnode
, &off
);
5789 if (VOP_SEEK(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
5797 * Determine approximately how large a zfs send stream will be -- the number
5798 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
5801 * (optional) "from" -> full snap or bookmark name to send an incremental
5803 * (optional) "largeblockok" -> (value ignored)
5804 * indicates that blocks > 128KB are permitted
5805 * (optional) "embedok" -> (value ignored)
5806 * presence indicates DRR_WRITE_EMBEDDED records are permitted
5807 * (optional) "compressok" -> (value ignored)
5808 * presence indicates compressed DRR_WRITE records are permitted
5812 * "space" -> bytes of space (uint64)
5816 zfs_ioc_send_space(const char *snapname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
5819 dsl_dataset_t
*tosnap
;
5822 /* LINTED E_FUNC_SET_NOT_USED */
5823 boolean_t largeblockok
;
5824 /* LINTED E_FUNC_SET_NOT_USED */
5826 boolean_t compressok
;
5829 error
= dsl_pool_hold(snapname
, FTAG
, &dp
);
5833 error
= dsl_dataset_hold(dp
, snapname
, FTAG
, &tosnap
);
5835 dsl_pool_rele(dp
, FTAG
);
5839 largeblockok
= nvlist_exists(innvl
, "largeblockok");
5840 embedok
= nvlist_exists(innvl
, "embedok");
5841 compressok
= nvlist_exists(innvl
, "compressok");
5843 error
= nvlist_lookup_string(innvl
, "from", &fromname
);
5845 if (strchr(fromname
, '@') != NULL
) {
5847 * If from is a snapshot, hold it and use the more
5848 * efficient dmu_send_estimate to estimate send space
5849 * size using deadlists.
5851 dsl_dataset_t
*fromsnap
;
5852 error
= dsl_dataset_hold(dp
, fromname
, FTAG
, &fromsnap
);
5855 error
= dmu_send_estimate(tosnap
, fromsnap
, compressok
,
5857 dsl_dataset_rele(fromsnap
, FTAG
);
5858 } else if (strchr(fromname
, '#') != NULL
) {
5860 * If from is a bookmark, fetch the creation TXG of the
5861 * snapshot it was created from and use that to find
5862 * blocks that were born after it.
5864 zfs_bookmark_phys_t frombm
;
5866 error
= dsl_bookmark_lookup(dp
, fromname
, tosnap
,
5870 error
= dmu_send_estimate_from_txg(tosnap
,
5871 frombm
.zbm_creation_txg
, compressok
, &space
);
5874 * from is not properly formatted as a snapshot or
5877 error
= SET_ERROR(EINVAL
);
5881 // If estimating the size of a full send, use dmu_send_estimate
5882 error
= dmu_send_estimate(tosnap
, NULL
, compressok
, &space
);
5885 fnvlist_add_uint64(outnvl
, "space", space
);
5888 dsl_dataset_rele(tosnap
, FTAG
);
5889 dsl_pool_rele(dp
, FTAG
);
5894 * Sync the currently open TXG to disk for the specified pool.
5895 * This is somewhat similar to 'zfs_sync()'.
5896 * For cases that do not result in error this ioctl will wait for
5897 * the currently open TXG to commit before returning back to the caller.
5900 * "force" -> when true, force uberblock update even if there is no dirty data.
5901 * In addition this will cause the vdev configuration to be written
5902 * out including updating the zpool cache file. (boolean_t)
5909 zfs_ioc_pool_sync(const char *pool
, nvlist_t
*innvl
, nvlist_t
*onvl
)
5912 boolean_t force
= B_FALSE
;
5915 if ((err
= spa_open(pool
, &spa
, FTAG
)) != 0)
5919 if (nvlist_lookup_boolean_value(innvl
, "force", &force
) != 0) {
5920 err
= SET_ERROR(EINVAL
);
5926 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_WRITER
);
5927 vdev_config_dirty(spa
->spa_root_vdev
);
5928 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
5930 txg_wait_synced(spa_get_dsl(spa
), 0);
5932 spa_close(spa
, FTAG
);
5937 static zfs_ioc_vec_t zfs_ioc_vec
[ZFS_IOC_LAST
- ZFS_IOC_FIRST
];
5940 zfs_ioctl_register_legacy(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5941 zfs_secpolicy_func_t
*secpolicy
, zfs_ioc_namecheck_t namecheck
,
5942 boolean_t log_history
, zfs_ioc_poolcheck_t pool_check
)
5944 zfs_ioc_vec_t
*vec
= &zfs_ioc_vec
[ioc
- ZFS_IOC_FIRST
];
5946 ASSERT3U(ioc
, >=, ZFS_IOC_FIRST
);
5947 ASSERT3U(ioc
, <, ZFS_IOC_LAST
);
5948 ASSERT3P(vec
->zvec_legacy_func
, ==, NULL
);
5949 ASSERT3P(vec
->zvec_func
, ==, NULL
);
5951 vec
->zvec_legacy_func
= func
;
5952 vec
->zvec_secpolicy
= secpolicy
;
5953 vec
->zvec_namecheck
= namecheck
;
5954 vec
->zvec_allow_log
= log_history
;
5955 vec
->zvec_pool_check
= pool_check
;
5959 * See the block comment at the beginning of this file for details on
5960 * each argument to this function.
5963 zfs_ioctl_register(const char *name
, zfs_ioc_t ioc
, zfs_ioc_func_t
*func
,
5964 zfs_secpolicy_func_t
*secpolicy
, zfs_ioc_namecheck_t namecheck
,
5965 zfs_ioc_poolcheck_t pool_check
, boolean_t smush_outnvlist
,
5966 boolean_t allow_log
)
5968 zfs_ioc_vec_t
*vec
= &zfs_ioc_vec
[ioc
- ZFS_IOC_FIRST
];
5970 ASSERT3U(ioc
, >=, ZFS_IOC_FIRST
);
5971 ASSERT3U(ioc
, <, ZFS_IOC_LAST
);
5972 ASSERT3P(vec
->zvec_legacy_func
, ==, NULL
);
5973 ASSERT3P(vec
->zvec_func
, ==, NULL
);
5975 /* if we are logging, the name must be valid */
5976 ASSERT(!allow_log
|| namecheck
!= NO_NAME
);
5978 vec
->zvec_name
= name
;
5979 vec
->zvec_func
= func
;
5980 vec
->zvec_secpolicy
= secpolicy
;
5981 vec
->zvec_namecheck
= namecheck
;
5982 vec
->zvec_pool_check
= pool_check
;
5983 vec
->zvec_smush_outnvlist
= smush_outnvlist
;
5984 vec
->zvec_allow_log
= allow_log
;
5988 zfs_ioctl_register_pool(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5989 zfs_secpolicy_func_t
*secpolicy
, boolean_t log_history
,
5990 zfs_ioc_poolcheck_t pool_check
)
5992 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5993 POOL_NAME
, log_history
, pool_check
);
5997 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5998 zfs_secpolicy_func_t
*secpolicy
, zfs_ioc_poolcheck_t pool_check
)
6000 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
6001 DATASET_NAME
, B_FALSE
, pool_check
);
6005 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
)
6007 zfs_ioctl_register_legacy(ioc
, func
, zfs_secpolicy_config
,
6008 POOL_NAME
, B_TRUE
, POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
6012 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
6013 zfs_secpolicy_func_t
*secpolicy
)
6015 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
6016 NO_NAME
, B_FALSE
, POOL_CHECK_NONE
);
6020 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc
,
6021 zfs_ioc_legacy_func_t
*func
, zfs_secpolicy_func_t
*secpolicy
)
6023 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
6024 DATASET_NAME
, B_FALSE
, POOL_CHECK_SUSPENDED
);
6028 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
)
6030 zfs_ioctl_register_dataset_read_secpolicy(ioc
, func
,
6031 zfs_secpolicy_read
);
6035 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
6036 zfs_secpolicy_func_t
*secpolicy
)
6038 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
6039 DATASET_NAME
, B_TRUE
, POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
6043 zfs_ioctl_init(void)
6045 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT
,
6046 zfs_ioc_snapshot
, zfs_secpolicy_snapshot
, POOL_NAME
,
6047 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
6049 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY
,
6050 zfs_ioc_log_history
, zfs_secpolicy_log_history
, NO_NAME
,
6051 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_FALSE
, B_FALSE
);
6053 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS
,
6054 zfs_ioc_space_snaps
, zfs_secpolicy_read
, DATASET_NAME
,
6055 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
6057 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW
,
6058 zfs_ioc_send_new
, zfs_secpolicy_send_new
, DATASET_NAME
,
6059 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
6061 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE
,
6062 zfs_ioc_send_space
, zfs_secpolicy_read
, DATASET_NAME
,
6063 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
6065 zfs_ioctl_register("create", ZFS_IOC_CREATE
,
6066 zfs_ioc_create
, zfs_secpolicy_create_clone
, DATASET_NAME
,
6067 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
6069 zfs_ioctl_register("clone", ZFS_IOC_CLONE
,
6070 zfs_ioc_clone
, zfs_secpolicy_create_clone
, DATASET_NAME
,
6071 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
6073 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS
,
6074 zfs_ioc_destroy_snaps
, zfs_secpolicy_destroy_snaps
, POOL_NAME
,
6075 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
6077 zfs_ioctl_register("hold", ZFS_IOC_HOLD
,
6078 zfs_ioc_hold
, zfs_secpolicy_hold
, POOL_NAME
,
6079 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
6080 zfs_ioctl_register("release", ZFS_IOC_RELEASE
,
6081 zfs_ioc_release
, zfs_secpolicy_release
, POOL_NAME
,
6082 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
6084 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS
,
6085 zfs_ioc_get_holds
, zfs_secpolicy_read
, DATASET_NAME
,
6086 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
6088 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK
,
6089 zfs_ioc_rollback
, zfs_secpolicy_rollback
, DATASET_NAME
,
6090 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_FALSE
, B_TRUE
);
6092 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK
,
6093 zfs_ioc_bookmark
, zfs_secpolicy_bookmark
, POOL_NAME
,
6094 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
6096 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS
,
6097 zfs_ioc_get_bookmarks
, zfs_secpolicy_read
, DATASET_NAME
,
6098 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
6100 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS
,
6101 zfs_ioc_destroy_bookmarks
, zfs_secpolicy_destroy_bookmarks
,
6103 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
6105 zfs_ioctl_register("receive", ZFS_IOC_RECV_NEW
,
6106 zfs_ioc_recv_new
, zfs_secpolicy_recv_new
, DATASET_NAME
,
6107 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
6109 zfs_ioctl_register("sync", ZFS_IOC_POOL_SYNC
,
6110 zfs_ioc_pool_sync
, zfs_secpolicy_none
, POOL_NAME
,
6111 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_FALSE
, B_FALSE
);
6113 /* IOCTLS that use the legacy function signature */
6115 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE
, zfs_ioc_pool_freeze
,
6116 zfs_secpolicy_config
, NO_NAME
, B_FALSE
, POOL_CHECK_READONLY
);
6118 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE
, zfs_ioc_pool_create
,
6119 zfs_secpolicy_config
, B_TRUE
, POOL_CHECK_NONE
);
6120 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN
,
6122 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE
,
6123 zfs_ioc_pool_upgrade
);
6124 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD
,
6126 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE
,
6127 zfs_ioc_vdev_remove
);
6128 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE
,
6129 zfs_ioc_vdev_set_state
);
6130 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH
,
6131 zfs_ioc_vdev_attach
);
6132 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH
,
6133 zfs_ioc_vdev_detach
);
6134 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH
,
6135 zfs_ioc_vdev_setpath
);
6136 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU
,
6137 zfs_ioc_vdev_setfru
);
6138 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS
,
6139 zfs_ioc_pool_set_props
);
6140 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT
,
6141 zfs_ioc_vdev_split
);
6142 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID
,
6143 zfs_ioc_pool_reguid
);
6145 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS
,
6146 zfs_ioc_pool_configs
, zfs_secpolicy_none
);
6147 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT
,
6148 zfs_ioc_pool_tryimport
, zfs_secpolicy_config
);
6149 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT
,
6150 zfs_ioc_inject_fault
, zfs_secpolicy_inject
);
6151 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT
,
6152 zfs_ioc_clear_fault
, zfs_secpolicy_inject
);
6153 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT
,
6154 zfs_ioc_inject_list_next
, zfs_secpolicy_inject
);
6157 * pool destroy, and export don't log the history as part of
6158 * zfsdev_ioctl, but rather zfs_ioc_pool_export
6159 * does the logging of those commands.
6161 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY
, zfs_ioc_pool_destroy
,
6162 zfs_secpolicy_config
, B_FALSE
, POOL_CHECK_SUSPENDED
);
6163 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT
, zfs_ioc_pool_export
,
6164 zfs_secpolicy_config
, B_FALSE
, POOL_CHECK_SUSPENDED
);
6166 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS
, zfs_ioc_pool_stats
,
6167 zfs_secpolicy_read
, B_FALSE
, POOL_CHECK_NONE
);
6168 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS
, zfs_ioc_pool_get_props
,
6169 zfs_secpolicy_read
, B_FALSE
, POOL_CHECK_NONE
);
6171 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG
, zfs_ioc_error_log
,
6172 zfs_secpolicy_inject
, B_FALSE
, POOL_CHECK_SUSPENDED
);
6173 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME
,
6174 zfs_ioc_dsobj_to_dsname
,
6175 zfs_secpolicy_diff
, B_FALSE
, POOL_CHECK_SUSPENDED
);
6176 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY
,
6177 zfs_ioc_pool_get_history
,
6178 zfs_secpolicy_config
, B_FALSE
, POOL_CHECK_SUSPENDED
);
6180 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT
, zfs_ioc_pool_import
,
6181 zfs_secpolicy_config
, B_TRUE
, POOL_CHECK_NONE
);
6183 zfs_ioctl_register_pool(ZFS_IOC_CLEAR
, zfs_ioc_clear
,
6184 zfs_secpolicy_config
, B_TRUE
, POOL_CHECK_READONLY
);
6185 zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN
, zfs_ioc_pool_reopen
,
6186 zfs_secpolicy_config
, B_TRUE
, POOL_CHECK_SUSPENDED
);
6188 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN
,
6189 zfs_ioc_space_written
);
6190 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS
,
6191 zfs_ioc_objset_recvd_props
);
6192 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ
,
6194 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL
,
6196 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS
,
6197 zfs_ioc_objset_stats
);
6198 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS
,
6199 zfs_ioc_objset_zplprops
);
6200 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT
,
6201 zfs_ioc_dataset_list_next
);
6202 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT
,
6203 zfs_ioc_snapshot_list_next
);
6204 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS
,
6205 zfs_ioc_send_progress
);
6207 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF
,
6208 zfs_ioc_diff
, zfs_secpolicy_diff
);
6209 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS
,
6210 zfs_ioc_obj_to_stats
, zfs_secpolicy_diff
);
6211 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH
,
6212 zfs_ioc_obj_to_path
, zfs_secpolicy_diff
);
6213 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE
,
6214 zfs_ioc_userspace_one
, zfs_secpolicy_userspace_one
);
6215 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY
,
6216 zfs_ioc_userspace_many
, zfs_secpolicy_userspace_many
);
6217 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND
,
6218 zfs_ioc_send
, zfs_secpolicy_send
);
6220 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP
, zfs_ioc_set_prop
,
6221 zfs_secpolicy_none
);
6222 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY
, zfs_ioc_destroy
,
6223 zfs_secpolicy_destroy
);
6224 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME
, zfs_ioc_rename
,
6225 zfs_secpolicy_rename
);
6226 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV
, zfs_ioc_recv
,
6227 zfs_secpolicy_recv
);
6228 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE
, zfs_ioc_promote
,
6229 zfs_secpolicy_promote
);
6230 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP
,
6231 zfs_ioc_inherit_prop
, zfs_secpolicy_inherit_prop
);
6232 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL
, zfs_ioc_set_fsacl
,
6233 zfs_secpolicy_set_fsacl
);
6235 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE
, zfs_ioc_share
,
6236 zfs_secpolicy_share
, POOL_CHECK_NONE
);
6237 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL
, zfs_ioc_smb_acl
,
6238 zfs_secpolicy_smb_acl
, POOL_CHECK_NONE
);
6239 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE
,
6240 zfs_ioc_userspace_upgrade
, zfs_secpolicy_userspace_upgrade
,
6241 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
6242 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT
,
6243 zfs_ioc_tmp_snapshot
, zfs_secpolicy_tmp_snapshot
,
6244 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
6249 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_NEXT
, zfs_ioc_events_next
,
6250 zfs_secpolicy_config
, NO_NAME
, B_FALSE
, POOL_CHECK_NONE
);
6251 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_CLEAR
, zfs_ioc_events_clear
,
6252 zfs_secpolicy_config
, NO_NAME
, B_FALSE
, POOL_CHECK_NONE
);
6253 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_SEEK
, zfs_ioc_events_seek
,
6254 zfs_secpolicy_config
, NO_NAME
, B_FALSE
, POOL_CHECK_NONE
);
6258 pool_status_check(const char *name
, zfs_ioc_namecheck_t type
,
6259 zfs_ioc_poolcheck_t check
)
6264 ASSERT(type
== POOL_NAME
|| type
== DATASET_NAME
);
6266 if (check
& POOL_CHECK_NONE
)
6269 error
= spa_open(name
, &spa
, FTAG
);
6271 if ((check
& POOL_CHECK_SUSPENDED
) && spa_suspended(spa
))
6272 error
= SET_ERROR(EAGAIN
);
6273 else if ((check
& POOL_CHECK_READONLY
) && !spa_writeable(spa
))
6274 error
= SET_ERROR(EROFS
);
6275 spa_close(spa
, FTAG
);
6281 zfsdev_get_state_impl(minor_t minor
, enum zfsdev_state_type which
)
6285 for (zs
= zfsdev_state_list
; zs
!= NULL
; zs
= zs
->zs_next
) {
6286 if (zs
->zs_minor
== minor
) {
6290 return (zs
->zs_onexit
);
6292 return (zs
->zs_zevent
);
6303 zfsdev_get_state(minor_t minor
, enum zfsdev_state_type which
)
6307 ptr
= zfsdev_get_state_impl(minor
, which
);
6313 zfsdev_getminor(struct file
*filp
, minor_t
*minorp
)
6315 zfsdev_state_t
*zs
, *fpd
;
6317 ASSERT(filp
!= NULL
);
6318 ASSERT(!MUTEX_HELD(&zfsdev_state_lock
));
6320 fpd
= filp
->private_data
;
6324 mutex_enter(&zfsdev_state_lock
);
6326 for (zs
= zfsdev_state_list
; zs
!= NULL
; zs
= zs
->zs_next
) {
6328 if (zs
->zs_minor
== -1)
6332 *minorp
= fpd
->zs_minor
;
6333 mutex_exit(&zfsdev_state_lock
);
6338 mutex_exit(&zfsdev_state_lock
);
6344 * Find a free minor number. The zfsdev_state_list is expected to
6345 * be short since it is only a list of currently open file handles.
6348 zfsdev_minor_alloc(void)
6350 static minor_t last_minor
= 0;
6353 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
6355 for (m
= last_minor
+ 1; m
!= last_minor
; m
++) {
6356 if (m
> ZFSDEV_MAX_MINOR
)
6358 if (zfsdev_get_state_impl(m
, ZST_ALL
) == NULL
) {
6368 zfsdev_state_init(struct file
*filp
)
6370 zfsdev_state_t
*zs
, *zsprev
= NULL
;
6372 boolean_t newzs
= B_FALSE
;
6374 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
6376 minor
= zfsdev_minor_alloc();
6378 return (SET_ERROR(ENXIO
));
6380 for (zs
= zfsdev_state_list
; zs
!= NULL
; zs
= zs
->zs_next
) {
6381 if (zs
->zs_minor
== -1)
6387 zs
= kmem_zalloc(sizeof (zfsdev_state_t
), KM_SLEEP
);
6392 filp
->private_data
= zs
;
6394 zfs_onexit_init((zfs_onexit_t
**)&zs
->zs_onexit
);
6395 zfs_zevent_init((zfs_zevent_t
**)&zs
->zs_zevent
);
6399 * In order to provide for lock-free concurrent read access
6400 * to the minor list in zfsdev_get_state_impl(), new entries
6401 * must be completely written before linking them into the
6402 * list whereas existing entries are already linked; the last
6403 * operation must be updating zs_minor (from -1 to the new
6407 zs
->zs_minor
= minor
;
6409 zsprev
->zs_next
= zs
;
6412 zs
->zs_minor
= minor
;
6419 zfsdev_state_destroy(struct file
*filp
)
6423 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
6424 ASSERT(filp
->private_data
!= NULL
);
6426 zs
= filp
->private_data
;
6428 zfs_onexit_destroy(zs
->zs_onexit
);
6429 zfs_zevent_destroy(zs
->zs_zevent
);
6435 zfsdev_open(struct inode
*ino
, struct file
*filp
)
6439 mutex_enter(&zfsdev_state_lock
);
6440 error
= zfsdev_state_init(filp
);
6441 mutex_exit(&zfsdev_state_lock
);
6447 zfsdev_release(struct inode
*ino
, struct file
*filp
)
6451 mutex_enter(&zfsdev_state_lock
);
6452 error
= zfsdev_state_destroy(filp
);
6453 mutex_exit(&zfsdev_state_lock
);
6459 zfsdev_ioctl(struct file
*filp
, unsigned cmd
, unsigned long arg
)
6463 int error
, rc
, flag
= 0;
6464 const zfs_ioc_vec_t
*vec
;
6465 char *saved_poolname
= NULL
;
6466 nvlist_t
*innvl
= NULL
;
6467 fstrans_cookie_t cookie
;
6469 vecnum
= cmd
- ZFS_IOC_FIRST
;
6470 if (vecnum
>= sizeof (zfs_ioc_vec
) / sizeof (zfs_ioc_vec
[0]))
6471 return (-SET_ERROR(EINVAL
));
6472 vec
= &zfs_ioc_vec
[vecnum
];
6475 * The registered ioctl list may be sparse, verify that either
6476 * a normal or legacy handler are registered.
6478 if (vec
->zvec_func
== NULL
&& vec
->zvec_legacy_func
== NULL
)
6479 return (-SET_ERROR(EINVAL
));
6481 zc
= kmem_zalloc(sizeof (zfs_cmd_t
), KM_SLEEP
);
6483 error
= ddi_copyin((void *)arg
, zc
, sizeof (zfs_cmd_t
), flag
);
6485 error
= SET_ERROR(EFAULT
);
6489 zc
->zc_iflags
= flag
& FKIOCTL
;
6490 if (zc
->zc_nvlist_src_size
> MAX_NVLIST_SRC_SIZE
) {
6492 * Make sure the user doesn't pass in an insane value for
6493 * zc_nvlist_src_size. We have to check, since we will end
6494 * up allocating that much memory inside of get_nvlist(). This
6495 * prevents a nefarious user from allocating tons of kernel
6498 * Also, we return EINVAL instead of ENOMEM here. The reason
6499 * being that returning ENOMEM from an ioctl() has a special
6500 * connotation; that the user's size value is too small and
6501 * needs to be expanded to hold the nvlist. See
6502 * zcmd_expand_dst_nvlist() for details.
6504 error
= SET_ERROR(EINVAL
); /* User's size too big */
6506 } else if (zc
->zc_nvlist_src_size
!= 0) {
6507 error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
6508 zc
->zc_iflags
, &innvl
);
6514 * Ensure that all pool/dataset names are valid before we pass down to
6517 zc
->zc_name
[sizeof (zc
->zc_name
) - 1] = '\0';
6518 switch (vec
->zvec_namecheck
) {
6520 if (pool_namecheck(zc
->zc_name
, NULL
, NULL
) != 0)
6521 error
= SET_ERROR(EINVAL
);
6523 error
= pool_status_check(zc
->zc_name
,
6524 vec
->zvec_namecheck
, vec
->zvec_pool_check
);
6528 if (dataset_namecheck(zc
->zc_name
, NULL
, NULL
) != 0)
6529 error
= SET_ERROR(EINVAL
);
6531 error
= pool_status_check(zc
->zc_name
,
6532 vec
->zvec_namecheck
, vec
->zvec_pool_check
);
6541 cookie
= spl_fstrans_mark();
6542 error
= vec
->zvec_secpolicy(zc
, innvl
, CRED());
6543 spl_fstrans_unmark(cookie
);
6549 /* legacy ioctls can modify zc_name */
6550 saved_poolname
= strdup(zc
->zc_name
);
6551 if (saved_poolname
== NULL
) {
6552 error
= SET_ERROR(ENOMEM
);
6555 saved_poolname
[strcspn(saved_poolname
, "/@#")] = '\0';
6558 if (vec
->zvec_func
!= NULL
) {
6562 nvlist_t
*lognv
= NULL
;
6564 ASSERT(vec
->zvec_legacy_func
== NULL
);
6567 * Add the innvl to the lognv before calling the func,
6568 * in case the func changes the innvl.
6570 if (vec
->zvec_allow_log
) {
6571 lognv
= fnvlist_alloc();
6572 fnvlist_add_string(lognv
, ZPOOL_HIST_IOCTL
,
6574 if (!nvlist_empty(innvl
)) {
6575 fnvlist_add_nvlist(lognv
, ZPOOL_HIST_INPUT_NVL
,
6580 outnvl
= fnvlist_alloc();
6581 cookie
= spl_fstrans_mark();
6582 error
= vec
->zvec_func(zc
->zc_name
, innvl
, outnvl
);
6583 spl_fstrans_unmark(cookie
);
6585 if (error
== 0 && vec
->zvec_allow_log
&&
6586 spa_open(zc
->zc_name
, &spa
, FTAG
) == 0) {
6587 if (!nvlist_empty(outnvl
)) {
6588 fnvlist_add_nvlist(lognv
, ZPOOL_HIST_OUTPUT_NVL
,
6591 (void) spa_history_log_nvl(spa
, lognv
);
6592 spa_close(spa
, FTAG
);
6594 fnvlist_free(lognv
);
6596 if (!nvlist_empty(outnvl
) || zc
->zc_nvlist_dst_size
!= 0) {
6598 if (vec
->zvec_smush_outnvlist
) {
6599 smusherror
= nvlist_smush(outnvl
,
6600 zc
->zc_nvlist_dst_size
);
6602 if (smusherror
== 0)
6603 puterror
= put_nvlist(zc
, outnvl
);
6609 nvlist_free(outnvl
);
6611 cookie
= spl_fstrans_mark();
6612 error
= vec
->zvec_legacy_func(zc
);
6613 spl_fstrans_unmark(cookie
);
6618 rc
= ddi_copyout(zc
, (void *)arg
, sizeof (zfs_cmd_t
), flag
);
6619 if (error
== 0 && rc
!= 0)
6620 error
= SET_ERROR(EFAULT
);
6621 if (error
== 0 && vec
->zvec_allow_log
) {
6622 char *s
= tsd_get(zfs_allow_log_key
);
6625 (void) tsd_set(zfs_allow_log_key
, saved_poolname
);
6627 if (saved_poolname
!= NULL
)
6628 strfree(saved_poolname
);
6631 kmem_free(zc
, sizeof (zfs_cmd_t
));
6635 #ifdef CONFIG_COMPAT
6637 zfsdev_compat_ioctl(struct file
*filp
, unsigned cmd
, unsigned long arg
)
6639 return (zfsdev_ioctl(filp
, cmd
, arg
));
6642 #define zfsdev_compat_ioctl NULL
6645 static const struct file_operations zfsdev_fops
= {
6646 .open
= zfsdev_open
,
6647 .release
= zfsdev_release
,
6648 .unlocked_ioctl
= zfsdev_ioctl
,
6649 .compat_ioctl
= zfsdev_compat_ioctl
,
6650 .owner
= THIS_MODULE
,
6653 static struct miscdevice zfs_misc
= {
6654 .minor
= MISC_DYNAMIC_MINOR
,
6656 .fops
= &zfsdev_fops
,
6664 mutex_init(&zfsdev_state_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
6665 zfsdev_state_list
= kmem_zalloc(sizeof (zfsdev_state_t
), KM_SLEEP
);
6666 zfsdev_state_list
->zs_minor
= -1;
6668 error
= misc_register(&zfs_misc
);
6670 printk(KERN_INFO
"ZFS: misc_register() failed %d\n", error
);
6680 zfsdev_state_t
*zs
, *zsprev
= NULL
;
6682 misc_deregister(&zfs_misc
);
6683 mutex_destroy(&zfsdev_state_lock
);
6685 for (zs
= zfsdev_state_list
; zs
!= NULL
; zs
= zs
->zs_next
) {
6687 kmem_free(zsprev
, sizeof (zfsdev_state_t
));
6691 kmem_free(zsprev
, sizeof (zfsdev_state_t
));
6695 zfs_allow_log_destroy(void *arg
)
6697 char *poolname
= arg
;
6699 if (poolname
!= NULL
)
6704 #define ZFS_DEBUG_STR " (DEBUG mode)"
6706 #define ZFS_DEBUG_STR ""
6714 error
= -vn_set_pwd("/");
6717 "ZFS: Warning unable to set pwd to '/': %d\n", error
);
6721 if ((error
= -zvol_init()) != 0)
6724 spa_init(FREAD
| FWRITE
);
6729 if ((error
= zfs_attach()) != 0)
6732 tsd_create(&zfs_fsyncer_key
, NULL
);
6733 tsd_create(&rrw_tsd_key
, rrw_tsd_destroy
);
6734 tsd_create(&zfs_allow_log_key
, zfs_allow_log_destroy
);
6736 printk(KERN_NOTICE
"ZFS: Loaded module v%s-%s%s, "
6737 "ZFS pool version %s, ZFS filesystem version %s\n",
6738 ZFS_META_VERSION
, ZFS_META_RELEASE
, ZFS_DEBUG_STR
,
6739 SPA_VERSION_STRING
, ZPL_VERSION_STRING
);
6740 #ifndef CONFIG_FS_POSIX_ACL
6741 printk(KERN_NOTICE
"ZFS: Posix ACLs disabled by kernel\n");
6742 #endif /* CONFIG_FS_POSIX_ACL */
6750 printk(KERN_NOTICE
"ZFS: Failed to Load ZFS Filesystem v%s-%s%s"
6751 ", rc = %d\n", ZFS_META_VERSION
, ZFS_META_RELEASE
,
6752 ZFS_DEBUG_STR
, error
);
6765 tsd_destroy(&zfs_fsyncer_key
);
6766 tsd_destroy(&rrw_tsd_key
);
6767 tsd_destroy(&zfs_allow_log_key
);
6769 printk(KERN_NOTICE
"ZFS: Unloaded module v%s-%s%s\n",
6770 ZFS_META_VERSION
, ZFS_META_RELEASE
, ZFS_DEBUG_STR
);
6777 MODULE_DESCRIPTION("ZFS");
6778 MODULE_AUTHOR(ZFS_META_AUTHOR
);
6779 MODULE_LICENSE(ZFS_META_LICENSE
);
6780 MODULE_VERSION(ZFS_META_VERSION
"-" ZFS_META_RELEASE
);
6781 #endif /* HAVE_SPL */