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 * Copyright (c) 2011-2012 Pawel Jakub Dawidek. All rights reserved.
25 * Portions Copyright 2011 Martin Matuska
26 * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
27 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
28 * Copyright (c) 2014, 2016 Joyent, Inc. All rights reserved.
29 * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
30 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
31 * Copyright (c) 2013 Steven Hartland. All rights reserved.
32 * Copyright (c) 2014 Integros [integros.com]
33 * Copyright 2016 Toomas Soome <tsoome@me.com>
34 * Copyright 2017 RackTop Systems.
35 * Copyright (c) 2017 Datto Inc.
41 * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
42 * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
44 * There are two ways that we handle ioctls: the legacy way where almost
45 * all of the logic is in the ioctl callback, and the new way where most
46 * of the marshalling is handled in the common entry point, zfsdev_ioctl().
48 * Non-legacy ioctls should be registered by calling
49 * zfs_ioctl_register() from zfs_ioctl_init(). The ioctl is invoked
50 * from userland by lzc_ioctl().
52 * The registration arguments are as follows:
55 * The name of the ioctl. This is used for history logging. If the
56 * ioctl returns successfully (the callback returns 0), and allow_log
57 * is true, then a history log entry will be recorded with the input &
58 * output nvlists. The log entry can be printed with "zpool history -i".
61 * The ioctl request number, which userland will pass to ioctl(2).
62 * The ioctl numbers can change from release to release, because
63 * the caller (libzfs) must be matched to the kernel.
65 * zfs_secpolicy_func_t *secpolicy
66 * This function will be called before the zfs_ioc_func_t, to
67 * determine if this operation is permitted. It should return EPERM
68 * on failure, and 0 on success. Checks include determining if the
69 * dataset is visible in this zone, and if the user has either all
70 * zfs privileges in the zone (SYS_MOUNT), or has been granted permission
71 * to do this operation on this dataset with "zfs allow".
73 * zfs_ioc_namecheck_t namecheck
74 * This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
75 * name, a dataset name, or nothing. If the name is not well-formed,
76 * the ioctl will fail and the callback will not be called.
77 * Therefore, the callback can assume that the name is well-formed
78 * (e.g. is null-terminated, doesn't have more than one '@' character,
79 * doesn't have invalid characters).
81 * zfs_ioc_poolcheck_t pool_check
82 * This specifies requirements on the pool state. If the pool does
83 * not meet them (is suspended or is readonly), the ioctl will fail
84 * and the callback will not be called. If any checks are specified
85 * (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
86 * Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
87 * POOL_CHECK_READONLY).
89 * boolean_t smush_outnvlist
90 * If smush_outnvlist is true, then the output is presumed to be a
91 * list of errors, and it will be "smushed" down to fit into the
92 * caller's buffer, by removing some entries and replacing them with a
93 * single "N_MORE_ERRORS" entry indicating how many were removed. See
94 * nvlist_smush() for details. If smush_outnvlist is false, and the
95 * outnvlist does not fit into the userland-provided buffer, then the
96 * ioctl will fail with ENOMEM.
98 * zfs_ioc_func_t *func
99 * The callback function that will perform the operation.
101 * The callback should return 0 on success, or an error number on
102 * failure. If the function fails, the userland ioctl will return -1,
103 * and errno will be set to the callback's return value. The callback
104 * will be called with the following arguments:
107 * The name of the pool or dataset to operate on, from
108 * zfs_cmd_t:zc_name. The 'namecheck' argument specifies the
109 * expected type (pool, dataset, or none).
112 * The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src. Or
113 * NULL if no input nvlist was provided. Changes to this nvlist are
114 * ignored. If the input nvlist could not be deserialized, the
115 * ioctl will fail and the callback will not be called.
118 * The output nvlist, initially empty. The callback can fill it in,
119 * and it will be returned to userland by serializing it into
120 * zfs_cmd_t:zc_nvlist_dst. If it is non-empty, and serialization
121 * fails (e.g. because the caller didn't supply a large enough
122 * buffer), then the overall ioctl will fail. See the
123 * 'smush_nvlist' argument above for additional behaviors.
125 * There are two typical uses of the output nvlist:
126 * - To return state, e.g. property values. In this case,
127 * smush_outnvlist should be false. If the buffer was not large
128 * enough, the caller will reallocate a larger buffer and try
131 * - To return multiple errors from an ioctl which makes on-disk
132 * changes. In this case, smush_outnvlist should be true.
133 * Ioctls which make on-disk modifications should generally not
134 * use the outnvl if they succeed, because the caller can not
135 * distinguish between the operation failing, and
136 * deserialization failing.
139 #include <sys/types.h>
140 #include <sys/param.h>
141 #include <sys/errno.h>
144 #include <sys/modctl.h>
145 #include <sys/open.h>
146 #include <sys/file.h>
147 #include <sys/kmem.h>
148 #include <sys/conf.h>
149 #include <sys/cmn_err.h>
150 #include <sys/stat.h>
151 #include <sys/zfs_ioctl.h>
152 #include <sys/zfs_vfsops.h>
153 #include <sys/zfs_znode.h>
156 #include <sys/spa_impl.h>
157 #include <sys/vdev.h>
158 #include <sys/priv_impl.h>
160 #include <sys/dsl_dir.h>
161 #include <sys/dsl_dataset.h>
162 #include <sys/dsl_prop.h>
163 #include <sys/dsl_deleg.h>
164 #include <sys/dmu_objset.h>
165 #include <sys/dmu_impl.h>
166 #include <sys/dmu_tx.h>
168 #include <sys/sunddi.h>
169 #include <sys/sunldi.h>
170 #include <sys/policy.h>
171 #include <sys/zone.h>
172 #include <sys/nvpair.h>
173 #include <sys/pathname.h>
174 #include <sys/mount.h>
176 #include <sys/fs/zfs.h>
177 #include <sys/zfs_ctldir.h>
178 #include <sys/zfs_dir.h>
179 #include <sys/zfs_onexit.h>
180 #include <sys/zvol.h>
181 #include <sys/dsl_scan.h>
182 #include <sharefs/share.h>
183 #include <sys/dmu_objset.h>
184 #include <sys/dmu_send.h>
185 #include <sys/dsl_destroy.h>
186 #include <sys/dsl_bookmark.h>
187 #include <sys/dsl_userhold.h>
188 #include <sys/zfeature.h>
190 #include <sys/zio_checksum.h>
191 #include <sys/vdev_removal.h>
192 #include <sys/vdev_impl.h>
193 #include <sys/vdev_initialize.h>
195 #include "zfs_namecheck.h"
196 #include "zfs_prop.h"
197 #include "zfs_deleg.h"
198 #include "zfs_comutil.h"
203 extern struct modlfs zfs_modlfs
;
205 extern void zfs_init(void);
206 extern void zfs_fini(void);
208 ldi_ident_t zfs_li
= NULL
;
211 uint_t zfs_fsyncer_key
;
212 extern uint_t rrw_tsd_key
;
213 static uint_t zfs_allow_log_key
;
215 typedef int zfs_ioc_legacy_func_t(zfs_cmd_t
*);
216 typedef int zfs_ioc_func_t(const char *, nvlist_t
*, nvlist_t
*);
217 typedef int zfs_secpolicy_func_t(zfs_cmd_t
*, nvlist_t
*, cred_t
*);
223 } zfs_ioc_namecheck_t
;
226 POOL_CHECK_NONE
= 1 << 0,
227 POOL_CHECK_SUSPENDED
= 1 << 1,
228 POOL_CHECK_READONLY
= 1 << 2,
229 } zfs_ioc_poolcheck_t
;
231 typedef struct zfs_ioc_vec
{
232 zfs_ioc_legacy_func_t
*zvec_legacy_func
;
233 zfs_ioc_func_t
*zvec_func
;
234 zfs_secpolicy_func_t
*zvec_secpolicy
;
235 zfs_ioc_namecheck_t zvec_namecheck
;
236 boolean_t zvec_allow_log
;
237 zfs_ioc_poolcheck_t zvec_pool_check
;
238 boolean_t zvec_smush_outnvlist
;
239 const char *zvec_name
;
242 /* This array is indexed by zfs_userquota_prop_t */
243 static const char *userquota_perms
[] = {
244 ZFS_DELEG_PERM_USERUSED
,
245 ZFS_DELEG_PERM_USERQUOTA
,
246 ZFS_DELEG_PERM_GROUPUSED
,
247 ZFS_DELEG_PERM_GROUPQUOTA
,
250 static int zfs_ioc_userspace_upgrade(zfs_cmd_t
*zc
);
251 static int zfs_check_settable(const char *name
, nvpair_t
*property
,
253 static int zfs_check_clearable(char *dataset
, nvlist_t
*props
,
255 static int zfs_fill_zplprops_root(uint64_t, nvlist_t
*, nvlist_t
*,
257 int zfs_set_prop_nvlist(const char *, zprop_source_t
, nvlist_t
*, nvlist_t
*);
258 static int get_nvlist(uint64_t nvl
, uint64_t size
, int iflag
, nvlist_t
**nvp
);
260 static int zfs_prop_activate_feature(spa_t
*spa
, spa_feature_t feature
);
262 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
264 __dprintf(const char *file
, const char *func
, int line
, const char *fmt
, ...)
271 * Get rid of annoying "../common/" prefix to filename.
273 newfile
= strrchr(file
, '/');
274 if (newfile
!= NULL
) {
275 newfile
= newfile
+ 1; /* Get rid of leading / */
281 (void) vsnprintf(buf
, sizeof (buf
), fmt
, adx
);
285 * To get this data, use the zfs-dprintf probe as so:
286 * dtrace -q -n 'zfs-dprintf \
287 * /stringof(arg0) == "dbuf.c"/ \
288 * {printf("%s: %s", stringof(arg1), stringof(arg3))}'
290 * arg1 = function name
294 DTRACE_PROBE4(zfs__dprintf
,
295 char *, newfile
, char *, func
, int, line
, char *, buf
);
299 history_str_free(char *buf
)
301 kmem_free(buf
, HIS_MAX_RECORD_LEN
);
305 history_str_get(zfs_cmd_t
*zc
)
309 if (zc
->zc_history
== (uintptr_t)NULL
)
312 buf
= kmem_alloc(HIS_MAX_RECORD_LEN
, KM_SLEEP
);
313 if (copyinstr((void *)(uintptr_t)zc
->zc_history
,
314 buf
, HIS_MAX_RECORD_LEN
, NULL
) != 0) {
315 history_str_free(buf
);
319 buf
[HIS_MAX_RECORD_LEN
-1] = '\0';
325 * Check to see if the named dataset is currently defined as bootable
328 zfs_is_bootfs(const char *name
)
332 if (dmu_objset_hold(name
, FTAG
, &os
) == 0) {
334 ret
= (dmu_objset_id(os
) == spa_bootfs(dmu_objset_spa(os
)));
335 dmu_objset_rele(os
, FTAG
);
342 * Return non-zero if the spa version is less than requested version.
345 zfs_earlier_version(const char *name
, int version
)
349 if (spa_open(name
, &spa
, FTAG
) == 0) {
350 if (spa_version(spa
) < version
) {
351 spa_close(spa
, FTAG
);
354 spa_close(spa
, FTAG
);
360 * Return TRUE if the ZPL version is less than requested version.
363 zpl_earlier_version(const char *name
, int version
)
366 boolean_t rc
= B_TRUE
;
368 if (dmu_objset_hold(name
, FTAG
, &os
) == 0) {
371 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
372 dmu_objset_rele(os
, FTAG
);
375 /* XXX reading from non-owned objset */
376 if (zfs_get_zplprop(os
, ZFS_PROP_VERSION
, &zplversion
) == 0)
377 rc
= zplversion
< version
;
378 dmu_objset_rele(os
, FTAG
);
384 zfs_log_history(zfs_cmd_t
*zc
)
389 if ((buf
= history_str_get(zc
)) == NULL
)
392 if (spa_open(zc
->zc_name
, &spa
, FTAG
) == 0) {
393 if (spa_version(spa
) >= SPA_VERSION_ZPOOL_HISTORY
)
394 (void) spa_history_log(spa
, buf
);
395 spa_close(spa
, FTAG
);
397 history_str_free(buf
);
401 * Policy for top-level read operations (list pools). Requires no privileges,
402 * and can be used in the local zone, as there is no associated dataset.
406 zfs_secpolicy_none(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
412 * Policy for dataset read operations (list children, get statistics). Requires
413 * no privileges, but must be visible in the local zone.
417 zfs_secpolicy_read(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
419 if (INGLOBALZONE(curproc
) ||
420 zone_dataset_visible(zc
->zc_name
, NULL
))
423 return (SET_ERROR(ENOENT
));
427 zfs_dozonecheck_impl(const char *dataset
, uint64_t zoned
, cred_t
*cr
)
432 * The dataset must be visible by this zone -- check this first
433 * so they don't see EPERM on something they shouldn't know about.
435 if (!INGLOBALZONE(curproc
) &&
436 !zone_dataset_visible(dataset
, &writable
))
437 return (SET_ERROR(ENOENT
));
439 if (INGLOBALZONE(curproc
)) {
441 * If the fs is zoned, only root can access it from the
444 if (secpolicy_zfs(cr
) && zoned
)
445 return (SET_ERROR(EPERM
));
448 * If we are in a local zone, the 'zoned' property must be set.
451 return (SET_ERROR(EPERM
));
453 /* must be writable by this zone */
455 return (SET_ERROR(EPERM
));
461 zfs_dozonecheck(const char *dataset
, cred_t
*cr
)
465 if (dsl_prop_get_integer(dataset
, "zoned", &zoned
, NULL
))
466 return (SET_ERROR(ENOENT
));
468 return (zfs_dozonecheck_impl(dataset
, zoned
, cr
));
472 zfs_dozonecheck_ds(const char *dataset
, dsl_dataset_t
*ds
, cred_t
*cr
)
476 if (dsl_prop_get_int_ds(ds
, "zoned", &zoned
))
477 return (SET_ERROR(ENOENT
));
479 return (zfs_dozonecheck_impl(dataset
, zoned
, cr
));
483 zfs_secpolicy_write_perms_ds(const char *name
, dsl_dataset_t
*ds
,
484 const char *perm
, cred_t
*cr
)
488 error
= zfs_dozonecheck_ds(name
, ds
, cr
);
490 error
= secpolicy_zfs(cr
);
492 error
= dsl_deleg_access_impl(ds
, perm
, cr
);
498 zfs_secpolicy_write_perms(const char *name
, const char *perm
, cred_t
*cr
)
505 * First do a quick check for root in the global zone, which
506 * is allowed to do all write_perms. This ensures that zfs_ioc_*
507 * will get to handle nonexistent datasets.
509 if (INGLOBALZONE(curproc
) && secpolicy_zfs(cr
) == 0)
512 error
= dsl_pool_hold(name
, FTAG
, &dp
);
516 error
= dsl_dataset_hold(dp
, name
, FTAG
, &ds
);
518 dsl_pool_rele(dp
, FTAG
);
522 error
= zfs_secpolicy_write_perms_ds(name
, ds
, perm
, cr
);
524 dsl_dataset_rele(ds
, FTAG
);
525 dsl_pool_rele(dp
, FTAG
);
530 zfs_secpolicy_setprop(const char *dsname
, zfs_prop_t prop
, nvpair_t
*propval
,
536 * Check permissions for special properties.
541 * Disallow setting of 'zoned' from within a local zone.
543 if (!INGLOBALZONE(curproc
))
544 return (SET_ERROR(EPERM
));
548 case ZFS_PROP_FILESYSTEM_LIMIT
:
549 case ZFS_PROP_SNAPSHOT_LIMIT
:
550 if (!INGLOBALZONE(curproc
)) {
552 char setpoint
[ZFS_MAX_DATASET_NAME_LEN
];
554 * Unprivileged users are allowed to modify the
555 * limit on things *under* (ie. contained by)
556 * the thing they own.
558 if (dsl_prop_get_integer(dsname
, "zoned", &zoned
,
560 return (SET_ERROR(EPERM
));
561 if (!zoned
|| strlen(dsname
) <= strlen(setpoint
))
562 return (SET_ERROR(EPERM
));
567 return (zfs_secpolicy_write_perms(dsname
, zfs_prop_to_name(prop
), cr
));
572 zfs_secpolicy_set_fsacl(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
576 error
= zfs_dozonecheck(zc
->zc_name
, cr
);
581 * permission to set permissions will be evaluated later in
582 * dsl_deleg_can_allow()
589 zfs_secpolicy_rollback(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
591 return (zfs_secpolicy_write_perms(zc
->zc_name
,
592 ZFS_DELEG_PERM_ROLLBACK
, cr
));
597 zfs_secpolicy_send(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
605 * Generate the current snapshot name from the given objsetid, then
606 * use that name for the secpolicy/zone checks.
608 cp
= strchr(zc
->zc_name
, '@');
610 return (SET_ERROR(EINVAL
));
611 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
615 error
= dsl_dataset_hold_obj(dp
, zc
->zc_sendobj
, FTAG
, &ds
);
617 dsl_pool_rele(dp
, FTAG
);
621 dsl_dataset_name(ds
, zc
->zc_name
);
623 error
= zfs_secpolicy_write_perms_ds(zc
->zc_name
, ds
,
624 ZFS_DELEG_PERM_SEND
, cr
);
625 dsl_dataset_rele(ds
, FTAG
);
626 dsl_pool_rele(dp
, FTAG
);
633 zfs_secpolicy_send_new(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
635 return (zfs_secpolicy_write_perms(zc
->zc_name
,
636 ZFS_DELEG_PERM_SEND
, cr
));
641 zfs_secpolicy_deleg_share(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
646 if ((error
= lookupname(zc
->zc_value
, UIO_SYSSPACE
,
647 NO_FOLLOW
, NULL
, &vp
)) != 0)
650 /* Now make sure mntpnt and dataset are ZFS */
652 if (vp
->v_vfsp
->vfs_fstype
!= zfsfstype
||
653 (strcmp((char *)refstr_value(vp
->v_vfsp
->vfs_resource
),
654 zc
->zc_name
) != 0)) {
656 return (SET_ERROR(EPERM
));
660 return (dsl_deleg_access(zc
->zc_name
,
661 ZFS_DELEG_PERM_SHARE
, cr
));
665 zfs_secpolicy_share(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
667 if (!INGLOBALZONE(curproc
))
668 return (SET_ERROR(EPERM
));
670 if (secpolicy_nfs(cr
) == 0) {
673 return (zfs_secpolicy_deleg_share(zc
, innvl
, cr
));
678 zfs_secpolicy_smb_acl(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
680 if (!INGLOBALZONE(curproc
))
681 return (SET_ERROR(EPERM
));
683 if (secpolicy_smb(cr
) == 0) {
686 return (zfs_secpolicy_deleg_share(zc
, innvl
, cr
));
691 zfs_get_parent(const char *datasetname
, char *parent
, int parentsize
)
696 * Remove the @bla or /bla from the end of the name to get the parent.
698 (void) strncpy(parent
, datasetname
, parentsize
);
699 cp
= strrchr(parent
, '@');
703 cp
= strrchr(parent
, '/');
705 return (SET_ERROR(ENOENT
));
713 zfs_secpolicy_destroy_perms(const char *name
, cred_t
*cr
)
717 if ((error
= zfs_secpolicy_write_perms(name
,
718 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
721 return (zfs_secpolicy_write_perms(name
, ZFS_DELEG_PERM_DESTROY
, cr
));
726 zfs_secpolicy_destroy(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
728 return (zfs_secpolicy_destroy_perms(zc
->zc_name
, cr
));
732 * Destroying snapshots with delegated permissions requires
733 * descendant mount and destroy permissions.
737 zfs_secpolicy_destroy_snaps(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
740 nvpair_t
*pair
, *nextpair
;
743 if (nvlist_lookup_nvlist(innvl
, "snaps", &snaps
) != 0)
744 return (SET_ERROR(EINVAL
));
745 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
747 nextpair
= nvlist_next_nvpair(snaps
, pair
);
748 error
= zfs_secpolicy_destroy_perms(nvpair_name(pair
), cr
);
749 if (error
== ENOENT
) {
751 * Ignore any snapshots that don't exist (we consider
752 * them "already destroyed"). Remove the name from the
753 * nvl here in case the snapshot is created between
754 * now and when we try to destroy it (in which case
755 * we don't want to destroy it since we haven't
756 * checked for permission).
758 fnvlist_remove_nvpair(snaps
, pair
);
769 zfs_secpolicy_rename_perms(const char *from
, const char *to
, cred_t
*cr
)
771 char parentname
[ZFS_MAX_DATASET_NAME_LEN
];
774 if ((error
= zfs_secpolicy_write_perms(from
,
775 ZFS_DELEG_PERM_RENAME
, cr
)) != 0)
778 if ((error
= zfs_secpolicy_write_perms(from
,
779 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
782 if ((error
= zfs_get_parent(to
, parentname
,
783 sizeof (parentname
))) != 0)
786 if ((error
= zfs_secpolicy_write_perms(parentname
,
787 ZFS_DELEG_PERM_CREATE
, cr
)) != 0)
790 if ((error
= zfs_secpolicy_write_perms(parentname
,
791 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
799 zfs_secpolicy_rename(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
801 return (zfs_secpolicy_rename_perms(zc
->zc_name
, zc
->zc_value
, cr
));
806 zfs_secpolicy_promote(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
809 dsl_dataset_t
*clone
;
812 error
= zfs_secpolicy_write_perms(zc
->zc_name
,
813 ZFS_DELEG_PERM_PROMOTE
, cr
);
817 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
821 error
= dsl_dataset_hold(dp
, zc
->zc_name
, FTAG
, &clone
);
824 char parentname
[ZFS_MAX_DATASET_NAME_LEN
];
825 dsl_dataset_t
*origin
= NULL
;
829 error
= dsl_dataset_hold_obj(dd
->dd_pool
,
830 dsl_dir_phys(dd
)->dd_origin_obj
, FTAG
, &origin
);
832 dsl_dataset_rele(clone
, FTAG
);
833 dsl_pool_rele(dp
, FTAG
);
837 error
= zfs_secpolicy_write_perms_ds(zc
->zc_name
, clone
,
838 ZFS_DELEG_PERM_MOUNT
, cr
);
840 dsl_dataset_name(origin
, parentname
);
842 error
= zfs_secpolicy_write_perms_ds(parentname
, origin
,
843 ZFS_DELEG_PERM_PROMOTE
, cr
);
845 dsl_dataset_rele(clone
, FTAG
);
846 dsl_dataset_rele(origin
, FTAG
);
848 dsl_pool_rele(dp
, FTAG
);
854 zfs_secpolicy_recv(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
858 if ((error
= zfs_secpolicy_write_perms(zc
->zc_name
,
859 ZFS_DELEG_PERM_RECEIVE
, cr
)) != 0)
862 if ((error
= zfs_secpolicy_write_perms(zc
->zc_name
,
863 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
866 return (zfs_secpolicy_write_perms(zc
->zc_name
,
867 ZFS_DELEG_PERM_CREATE
, cr
));
871 zfs_secpolicy_snapshot_perms(const char *name
, cred_t
*cr
)
873 return (zfs_secpolicy_write_perms(name
,
874 ZFS_DELEG_PERM_SNAPSHOT
, cr
));
878 * Check for permission to create each snapshot in the nvlist.
882 zfs_secpolicy_snapshot(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
888 if (nvlist_lookup_nvlist(innvl
, "snaps", &snaps
) != 0)
889 return (SET_ERROR(EINVAL
));
890 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
891 pair
= nvlist_next_nvpair(snaps
, pair
)) {
892 char *name
= nvpair_name(pair
);
893 char *atp
= strchr(name
, '@');
896 error
= SET_ERROR(EINVAL
);
900 error
= zfs_secpolicy_snapshot_perms(name
, cr
);
909 * Check for permission to create each snapshot in the nvlist.
913 zfs_secpolicy_bookmark(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
917 for (nvpair_t
*pair
= nvlist_next_nvpair(innvl
, NULL
);
918 pair
!= NULL
; pair
= nvlist_next_nvpair(innvl
, pair
)) {
919 char *name
= nvpair_name(pair
);
920 char *hashp
= strchr(name
, '#');
923 error
= SET_ERROR(EINVAL
);
927 error
= zfs_secpolicy_write_perms(name
,
928 ZFS_DELEG_PERM_BOOKMARK
, cr
);
938 zfs_secpolicy_remap(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
940 return (zfs_secpolicy_write_perms(zc
->zc_name
,
941 ZFS_DELEG_PERM_REMAP
, cr
));
946 zfs_secpolicy_destroy_bookmarks(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
948 nvpair_t
*pair
, *nextpair
;
951 for (pair
= nvlist_next_nvpair(innvl
, NULL
); pair
!= NULL
;
953 char *name
= nvpair_name(pair
);
954 char *hashp
= strchr(name
, '#');
955 nextpair
= nvlist_next_nvpair(innvl
, pair
);
958 error
= SET_ERROR(EINVAL
);
963 error
= zfs_secpolicy_write_perms(name
,
964 ZFS_DELEG_PERM_DESTROY
, cr
);
966 if (error
== ENOENT
) {
968 * Ignore any filesystems that don't exist (we consider
969 * their bookmarks "already destroyed"). Remove
970 * the name from the nvl here in case the filesystem
971 * is created between now and when we try to destroy
972 * the bookmark (in which case we don't want to
973 * destroy it since we haven't checked for permission).
975 fnvlist_remove_nvpair(innvl
, pair
);
987 zfs_secpolicy_log_history(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
990 * Even root must have a proper TSD so that we know what pool
993 if (tsd_get(zfs_allow_log_key
) == NULL
)
994 return (SET_ERROR(EPERM
));
999 zfs_secpolicy_create_clone(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1001 char parentname
[ZFS_MAX_DATASET_NAME_LEN
];
1005 if ((error
= zfs_get_parent(zc
->zc_name
, parentname
,
1006 sizeof (parentname
))) != 0)
1009 if (nvlist_lookup_string(innvl
, "origin", &origin
) == 0 &&
1010 (error
= zfs_secpolicy_write_perms(origin
,
1011 ZFS_DELEG_PERM_CLONE
, cr
)) != 0)
1014 if ((error
= zfs_secpolicy_write_perms(parentname
,
1015 ZFS_DELEG_PERM_CREATE
, cr
)) != 0)
1018 return (zfs_secpolicy_write_perms(parentname
,
1019 ZFS_DELEG_PERM_MOUNT
, cr
));
1023 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
1024 * SYS_CONFIG privilege, which is not available in a local zone.
1028 zfs_secpolicy_config(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1030 if (secpolicy_sys_config(cr
, B_FALSE
) != 0)
1031 return (SET_ERROR(EPERM
));
1037 * Policy for object to name lookups.
1041 zfs_secpolicy_diff(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1045 if ((error
= secpolicy_sys_config(cr
, B_FALSE
)) == 0)
1048 error
= zfs_secpolicy_write_perms(zc
->zc_name
, ZFS_DELEG_PERM_DIFF
, cr
);
1053 * Policy for fault injection. Requires all privileges.
1057 zfs_secpolicy_inject(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1059 return (secpolicy_zinject(cr
));
1064 zfs_secpolicy_inherit_prop(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1066 zfs_prop_t prop
= zfs_name_to_prop(zc
->zc_value
);
1068 if (prop
== ZPROP_INVAL
) {
1069 if (!zfs_prop_user(zc
->zc_value
))
1070 return (SET_ERROR(EINVAL
));
1071 return (zfs_secpolicy_write_perms(zc
->zc_name
,
1072 ZFS_DELEG_PERM_USERPROP
, cr
));
1074 return (zfs_secpolicy_setprop(zc
->zc_name
, prop
,
1080 zfs_secpolicy_userspace_one(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1082 int err
= zfs_secpolicy_read(zc
, innvl
, cr
);
1086 if (zc
->zc_objset_type
>= ZFS_NUM_USERQUOTA_PROPS
)
1087 return (SET_ERROR(EINVAL
));
1089 if (zc
->zc_value
[0] == 0) {
1091 * They are asking about a posix uid/gid. If it's
1092 * themself, allow it.
1094 if (zc
->zc_objset_type
== ZFS_PROP_USERUSED
||
1095 zc
->zc_objset_type
== ZFS_PROP_USERQUOTA
) {
1096 if (zc
->zc_guid
== crgetuid(cr
))
1099 if (groupmember(zc
->zc_guid
, cr
))
1104 return (zfs_secpolicy_write_perms(zc
->zc_name
,
1105 userquota_perms
[zc
->zc_objset_type
], cr
));
1109 zfs_secpolicy_userspace_many(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1111 int err
= zfs_secpolicy_read(zc
, innvl
, cr
);
1115 if (zc
->zc_objset_type
>= ZFS_NUM_USERQUOTA_PROPS
)
1116 return (SET_ERROR(EINVAL
));
1118 return (zfs_secpolicy_write_perms(zc
->zc_name
,
1119 userquota_perms
[zc
->zc_objset_type
], cr
));
1124 zfs_secpolicy_userspace_upgrade(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1126 return (zfs_secpolicy_setprop(zc
->zc_name
, ZFS_PROP_VERSION
,
1132 zfs_secpolicy_hold(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1138 error
= nvlist_lookup_nvlist(innvl
, "holds", &holds
);
1140 return (SET_ERROR(EINVAL
));
1142 for (pair
= nvlist_next_nvpair(holds
, NULL
); pair
!= NULL
;
1143 pair
= nvlist_next_nvpair(holds
, pair
)) {
1144 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
1145 error
= dmu_fsname(nvpair_name(pair
), fsname
);
1148 error
= zfs_secpolicy_write_perms(fsname
,
1149 ZFS_DELEG_PERM_HOLD
, cr
);
1158 zfs_secpolicy_release(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1163 for (pair
= nvlist_next_nvpair(innvl
, NULL
); pair
!= NULL
;
1164 pair
= nvlist_next_nvpair(innvl
, pair
)) {
1165 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
1166 error
= dmu_fsname(nvpair_name(pair
), fsname
);
1169 error
= zfs_secpolicy_write_perms(fsname
,
1170 ZFS_DELEG_PERM_RELEASE
, cr
);
1178 * Policy for allowing temporary snapshots to be taken or released
1181 zfs_secpolicy_tmp_snapshot(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1184 * A temporary snapshot is the same as a snapshot,
1185 * hold, destroy and release all rolled into one.
1186 * Delegated diff alone is sufficient that we allow this.
1190 if ((error
= zfs_secpolicy_write_perms(zc
->zc_name
,
1191 ZFS_DELEG_PERM_DIFF
, cr
)) == 0)
1194 error
= zfs_secpolicy_snapshot_perms(zc
->zc_name
, cr
);
1196 error
= zfs_secpolicy_hold(zc
, innvl
, cr
);
1198 error
= zfs_secpolicy_release(zc
, innvl
, cr
);
1200 error
= zfs_secpolicy_destroy(zc
, innvl
, cr
);
1205 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1208 get_nvlist(uint64_t nvl
, uint64_t size
, int iflag
, nvlist_t
**nvp
)
1212 nvlist_t
*list
= NULL
;
1215 * Read in and unpack the user-supplied nvlist.
1218 return (SET_ERROR(EINVAL
));
1220 packed
= kmem_alloc(size
, KM_SLEEP
);
1222 if ((error
= ddi_copyin((void *)(uintptr_t)nvl
, packed
, size
,
1224 kmem_free(packed
, size
);
1225 return (SET_ERROR(EFAULT
));
1228 if ((error
= nvlist_unpack(packed
, size
, &list
, 0)) != 0) {
1229 kmem_free(packed
, size
);
1233 kmem_free(packed
, size
);
1240 * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
1241 * Entries will be removed from the end of the nvlist, and one int32 entry
1242 * named "N_MORE_ERRORS" will be added indicating how many entries were
1246 nvlist_smush(nvlist_t
*errors
, size_t max
)
1250 size
= fnvlist_size(errors
);
1253 nvpair_t
*more_errors
;
1257 return (SET_ERROR(ENOMEM
));
1259 fnvlist_add_int32(errors
, ZPROP_N_MORE_ERRORS
, 0);
1260 more_errors
= nvlist_prev_nvpair(errors
, NULL
);
1263 nvpair_t
*pair
= nvlist_prev_nvpair(errors
,
1265 fnvlist_remove_nvpair(errors
, pair
);
1267 size
= fnvlist_size(errors
);
1268 } while (size
> max
);
1270 fnvlist_remove_nvpair(errors
, more_errors
);
1271 fnvlist_add_int32(errors
, ZPROP_N_MORE_ERRORS
, n
);
1272 ASSERT3U(fnvlist_size(errors
), <=, max
);
1279 put_nvlist(zfs_cmd_t
*zc
, nvlist_t
*nvl
)
1281 char *packed
= NULL
;
1285 size
= fnvlist_size(nvl
);
1287 if (size
> zc
->zc_nvlist_dst_size
) {
1288 error
= SET_ERROR(ENOMEM
);
1290 packed
= fnvlist_pack(nvl
, &size
);
1291 if (ddi_copyout(packed
, (void *)(uintptr_t)zc
->zc_nvlist_dst
,
1292 size
, zc
->zc_iflags
) != 0)
1293 error
= SET_ERROR(EFAULT
);
1294 fnvlist_pack_free(packed
, size
);
1297 zc
->zc_nvlist_dst_size
= size
;
1298 zc
->zc_nvlist_dst_filled
= B_TRUE
;
1303 getzfsvfs_impl(objset_t
*os
, zfsvfs_t
**zfvp
)
1306 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
1307 return (SET_ERROR(EINVAL
));
1310 mutex_enter(&os
->os_user_ptr_lock
);
1311 *zfvp
= dmu_objset_get_user(os
);
1313 VFS_HOLD((*zfvp
)->z_vfs
);
1315 error
= SET_ERROR(ESRCH
);
1317 mutex_exit(&os
->os_user_ptr_lock
);
1322 getzfsvfs(const char *dsname
, zfsvfs_t
**zfvp
)
1327 error
= dmu_objset_hold(dsname
, FTAG
, &os
);
1331 error
= getzfsvfs_impl(os
, zfvp
);
1332 dmu_objset_rele(os
, FTAG
);
1337 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
1338 * case its z_vfs will be NULL, and it will be opened as the owner.
1339 * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1340 * which prevents all vnode ops from running.
1343 zfsvfs_hold(const char *name
, void *tag
, zfsvfs_t
**zfvp
, boolean_t writer
)
1347 if (getzfsvfs(name
, zfvp
) != 0)
1348 error
= zfsvfs_create(name
, zfvp
);
1350 rrm_enter(&(*zfvp
)->z_teardown_lock
, (writer
) ? RW_WRITER
:
1352 if ((*zfvp
)->z_unmounted
) {
1354 * XXX we could probably try again, since the unmounting
1355 * thread should be just about to disassociate the
1356 * objset from the zfsvfs.
1358 rrm_exit(&(*zfvp
)->z_teardown_lock
, tag
);
1359 return (SET_ERROR(EBUSY
));
1366 zfsvfs_rele(zfsvfs_t
*zfsvfs
, void *tag
)
1368 rrm_exit(&zfsvfs
->z_teardown_lock
, tag
);
1370 if (zfsvfs
->z_vfs
) {
1371 VFS_RELE(zfsvfs
->z_vfs
);
1373 dmu_objset_disown(zfsvfs
->z_os
, zfsvfs
);
1374 zfsvfs_free(zfsvfs
);
1379 zfs_ioc_pool_create(zfs_cmd_t
*zc
)
1382 nvlist_t
*config
, *props
= NULL
;
1383 nvlist_t
*rootprops
= NULL
;
1384 nvlist_t
*zplprops
= NULL
;
1386 if (error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1387 zc
->zc_iflags
, &config
))
1390 if (zc
->zc_nvlist_src_size
!= 0 && (error
=
1391 get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1392 zc
->zc_iflags
, &props
))) {
1393 nvlist_free(config
);
1398 nvlist_t
*nvl
= NULL
;
1399 uint64_t version
= SPA_VERSION
;
1401 (void) nvlist_lookup_uint64(props
,
1402 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
);
1403 if (!SPA_VERSION_IS_SUPPORTED(version
)) {
1404 error
= SET_ERROR(EINVAL
);
1405 goto pool_props_bad
;
1407 (void) nvlist_lookup_nvlist(props
, ZPOOL_ROOTFS_PROPS
, &nvl
);
1409 error
= nvlist_dup(nvl
, &rootprops
, KM_SLEEP
);
1411 nvlist_free(config
);
1415 (void) nvlist_remove_all(props
, ZPOOL_ROOTFS_PROPS
);
1417 VERIFY(nvlist_alloc(&zplprops
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1418 error
= zfs_fill_zplprops_root(version
, rootprops
,
1421 goto pool_props_bad
;
1424 error
= spa_create(zc
->zc_name
, config
, props
, zplprops
);
1427 * Set the remaining root properties
1429 if (!error
&& (error
= zfs_set_prop_nvlist(zc
->zc_name
,
1430 ZPROP_SRC_LOCAL
, rootprops
, NULL
)) != 0)
1431 (void) spa_destroy(zc
->zc_name
);
1434 nvlist_free(rootprops
);
1435 nvlist_free(zplprops
);
1436 nvlist_free(config
);
1443 zfs_ioc_pool_destroy(zfs_cmd_t
*zc
)
1446 zfs_log_history(zc
);
1447 error
= spa_destroy(zc
->zc_name
);
1449 zvol_remove_minors(zc
->zc_name
);
1454 zfs_ioc_pool_import(zfs_cmd_t
*zc
)
1456 nvlist_t
*config
, *props
= NULL
;
1460 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1461 zc
->zc_iflags
, &config
)) != 0)
1464 if (zc
->zc_nvlist_src_size
!= 0 && (error
=
1465 get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1466 zc
->zc_iflags
, &props
))) {
1467 nvlist_free(config
);
1471 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &guid
) != 0 ||
1472 guid
!= zc
->zc_guid
)
1473 error
= SET_ERROR(EINVAL
);
1475 error
= spa_import(zc
->zc_name
, config
, props
, zc
->zc_cookie
);
1477 if (zc
->zc_nvlist_dst
!= 0) {
1480 if ((err
= put_nvlist(zc
, config
)) != 0)
1484 nvlist_free(config
);
1492 zfs_ioc_pool_export(zfs_cmd_t
*zc
)
1495 boolean_t force
= (boolean_t
)zc
->zc_cookie
;
1496 boolean_t hardforce
= (boolean_t
)zc
->zc_guid
;
1498 zfs_log_history(zc
);
1499 error
= spa_export(zc
->zc_name
, NULL
, force
, hardforce
);
1501 zvol_remove_minors(zc
->zc_name
);
1506 zfs_ioc_pool_configs(zfs_cmd_t
*zc
)
1511 if ((configs
= spa_all_configs(&zc
->zc_cookie
)) == NULL
)
1512 return (SET_ERROR(EEXIST
));
1514 error
= put_nvlist(zc
, configs
);
1516 nvlist_free(configs
);
1523 * zc_name name of the pool
1526 * zc_cookie real errno
1527 * zc_nvlist_dst config nvlist
1528 * zc_nvlist_dst_size size of config nvlist
1531 zfs_ioc_pool_stats(zfs_cmd_t
*zc
)
1537 error
= spa_get_stats(zc
->zc_name
, &config
, zc
->zc_value
,
1538 sizeof (zc
->zc_value
));
1540 if (config
!= NULL
) {
1541 ret
= put_nvlist(zc
, config
);
1542 nvlist_free(config
);
1545 * The config may be present even if 'error' is non-zero.
1546 * In this case we return success, and preserve the real errno
1549 zc
->zc_cookie
= error
;
1558 * Try to import the given pool, returning pool stats as appropriate so that
1559 * user land knows which devices are available and overall pool health.
1562 zfs_ioc_pool_tryimport(zfs_cmd_t
*zc
)
1564 nvlist_t
*tryconfig
, *config
;
1567 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1568 zc
->zc_iflags
, &tryconfig
)) != 0)
1571 config
= spa_tryimport(tryconfig
);
1573 nvlist_free(tryconfig
);
1576 return (SET_ERROR(EINVAL
));
1578 error
= put_nvlist(zc
, config
);
1579 nvlist_free(config
);
1586 * zc_name name of the pool
1587 * zc_cookie scan func (pool_scan_func_t)
1588 * zc_flags scrub pause/resume flag (pool_scrub_cmd_t)
1591 zfs_ioc_pool_scan(zfs_cmd_t
*zc
)
1596 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1599 if (zc
->zc_flags
>= POOL_SCRUB_FLAGS_END
)
1600 return (SET_ERROR(EINVAL
));
1602 if (zc
->zc_flags
== POOL_SCRUB_PAUSE
)
1603 error
= spa_scrub_pause_resume(spa
, POOL_SCRUB_PAUSE
);
1604 else if (zc
->zc_cookie
== POOL_SCAN_NONE
)
1605 error
= spa_scan_stop(spa
);
1607 error
= spa_scan(spa
, zc
->zc_cookie
);
1609 spa_close(spa
, FTAG
);
1615 zfs_ioc_pool_freeze(zfs_cmd_t
*zc
)
1620 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1623 spa_close(spa
, FTAG
);
1629 zfs_ioc_pool_upgrade(zfs_cmd_t
*zc
)
1634 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1637 if (zc
->zc_cookie
< spa_version(spa
) ||
1638 !SPA_VERSION_IS_SUPPORTED(zc
->zc_cookie
)) {
1639 spa_close(spa
, FTAG
);
1640 return (SET_ERROR(EINVAL
));
1643 spa_upgrade(spa
, zc
->zc_cookie
);
1644 spa_close(spa
, FTAG
);
1650 zfs_ioc_pool_get_history(zfs_cmd_t
*zc
)
1657 if ((size
= zc
->zc_history_len
) == 0)
1658 return (SET_ERROR(EINVAL
));
1660 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1663 if (spa_version(spa
) < SPA_VERSION_ZPOOL_HISTORY
) {
1664 spa_close(spa
, FTAG
);
1665 return (SET_ERROR(ENOTSUP
));
1668 hist_buf
= kmem_alloc(size
, KM_SLEEP
);
1669 if ((error
= spa_history_get(spa
, &zc
->zc_history_offset
,
1670 &zc
->zc_history_len
, hist_buf
)) == 0) {
1671 error
= ddi_copyout(hist_buf
,
1672 (void *)(uintptr_t)zc
->zc_history
,
1673 zc
->zc_history_len
, zc
->zc_iflags
);
1676 spa_close(spa
, FTAG
);
1677 kmem_free(hist_buf
, size
);
1682 zfs_ioc_pool_reguid(zfs_cmd_t
*zc
)
1687 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1689 error
= spa_change_guid(spa
);
1690 spa_close(spa
, FTAG
);
1696 zfs_ioc_dsobj_to_dsname(zfs_cmd_t
*zc
)
1698 return (dsl_dsobj_to_dsname(zc
->zc_name
, zc
->zc_obj
, zc
->zc_value
));
1703 * zc_name name of filesystem
1704 * zc_obj object to find
1707 * zc_value name of object
1710 zfs_ioc_obj_to_path(zfs_cmd_t
*zc
)
1715 /* XXX reading from objset not owned */
1716 if ((error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
)) != 0)
1718 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
1719 dmu_objset_rele(os
, FTAG
);
1720 return (SET_ERROR(EINVAL
));
1722 error
= zfs_obj_to_path(os
, zc
->zc_obj
, zc
->zc_value
,
1723 sizeof (zc
->zc_value
));
1724 dmu_objset_rele(os
, FTAG
);
1731 * zc_name name of filesystem
1732 * zc_obj object to find
1735 * zc_stat stats on object
1736 * zc_value path to object
1739 zfs_ioc_obj_to_stats(zfs_cmd_t
*zc
)
1744 /* XXX reading from objset not owned */
1745 if ((error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
)) != 0)
1747 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
1748 dmu_objset_rele(os
, FTAG
);
1749 return (SET_ERROR(EINVAL
));
1751 error
= zfs_obj_to_stats(os
, zc
->zc_obj
, &zc
->zc_stat
, zc
->zc_value
,
1752 sizeof (zc
->zc_value
));
1753 dmu_objset_rele(os
, FTAG
);
1759 zfs_ioc_vdev_add(zfs_cmd_t
*zc
)
1763 nvlist_t
*config
, **l2cache
, **spares
;
1764 uint_t nl2cache
= 0, nspares
= 0;
1766 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1770 error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1771 zc
->zc_iflags
, &config
);
1772 (void) nvlist_lookup_nvlist_array(config
, ZPOOL_CONFIG_L2CACHE
,
1773 &l2cache
, &nl2cache
);
1775 (void) nvlist_lookup_nvlist_array(config
, ZPOOL_CONFIG_SPARES
,
1779 * A root pool with concatenated devices is not supported.
1780 * Thus, can not add a device to a root pool.
1782 * Intent log device can not be added to a rootpool because
1783 * during mountroot, zil is replayed, a seperated log device
1784 * can not be accessed during the mountroot time.
1786 * l2cache and spare devices are ok to be added to a rootpool.
1788 if (spa_bootfs(spa
) != 0 && nl2cache
== 0 && nspares
== 0) {
1789 nvlist_free(config
);
1790 spa_close(spa
, FTAG
);
1791 return (SET_ERROR(EDOM
));
1795 error
= spa_vdev_add(spa
, config
);
1796 nvlist_free(config
);
1798 spa_close(spa
, FTAG
);
1804 * zc_name name of the pool
1805 * zc_guid guid of vdev to remove
1806 * zc_cookie cancel removal
1809 zfs_ioc_vdev_remove(zfs_cmd_t
*zc
)
1814 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1817 if (zc
->zc_cookie
!= 0) {
1818 error
= spa_vdev_remove_cancel(spa
);
1820 error
= spa_vdev_remove(spa
, zc
->zc_guid
, B_FALSE
);
1822 spa_close(spa
, FTAG
);
1827 zfs_ioc_vdev_set_state(zfs_cmd_t
*zc
)
1831 vdev_state_t newstate
= VDEV_STATE_UNKNOWN
;
1833 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1835 switch (zc
->zc_cookie
) {
1836 case VDEV_STATE_ONLINE
:
1837 error
= vdev_online(spa
, zc
->zc_guid
, zc
->zc_obj
, &newstate
);
1840 case VDEV_STATE_OFFLINE
:
1841 error
= vdev_offline(spa
, zc
->zc_guid
, zc
->zc_obj
);
1844 case VDEV_STATE_FAULTED
:
1845 if (zc
->zc_obj
!= VDEV_AUX_ERR_EXCEEDED
&&
1846 zc
->zc_obj
!= VDEV_AUX_EXTERNAL
)
1847 zc
->zc_obj
= VDEV_AUX_ERR_EXCEEDED
;
1849 error
= vdev_fault(spa
, zc
->zc_guid
, zc
->zc_obj
);
1852 case VDEV_STATE_DEGRADED
:
1853 if (zc
->zc_obj
!= VDEV_AUX_ERR_EXCEEDED
&&
1854 zc
->zc_obj
!= VDEV_AUX_EXTERNAL
)
1855 zc
->zc_obj
= VDEV_AUX_ERR_EXCEEDED
;
1857 error
= vdev_degrade(spa
, zc
->zc_guid
, zc
->zc_obj
);
1861 error
= SET_ERROR(EINVAL
);
1863 zc
->zc_cookie
= newstate
;
1864 spa_close(spa
, FTAG
);
1869 zfs_ioc_vdev_attach(zfs_cmd_t
*zc
)
1872 int replacing
= zc
->zc_cookie
;
1876 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1879 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1880 zc
->zc_iflags
, &config
)) == 0) {
1881 error
= spa_vdev_attach(spa
, zc
->zc_guid
, config
, replacing
);
1882 nvlist_free(config
);
1885 spa_close(spa
, FTAG
);
1890 zfs_ioc_vdev_detach(zfs_cmd_t
*zc
)
1895 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1898 error
= spa_vdev_detach(spa
, zc
->zc_guid
, 0, B_FALSE
);
1900 spa_close(spa
, FTAG
);
1905 zfs_ioc_vdev_split(zfs_cmd_t
*zc
)
1908 nvlist_t
*config
, *props
= NULL
;
1910 boolean_t exp
= !!(zc
->zc_cookie
& ZPOOL_EXPORT_AFTER_SPLIT
);
1912 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1915 if (error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1916 zc
->zc_iflags
, &config
)) {
1917 spa_close(spa
, FTAG
);
1921 if (zc
->zc_nvlist_src_size
!= 0 && (error
=
1922 get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1923 zc
->zc_iflags
, &props
))) {
1924 spa_close(spa
, FTAG
);
1925 nvlist_free(config
);
1929 error
= spa_vdev_split_mirror(spa
, zc
->zc_string
, config
, props
, exp
);
1931 spa_close(spa
, FTAG
);
1933 nvlist_free(config
);
1940 zfs_ioc_vdev_setpath(zfs_cmd_t
*zc
)
1943 char *path
= zc
->zc_value
;
1944 uint64_t guid
= zc
->zc_guid
;
1947 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1951 error
= spa_vdev_setpath(spa
, guid
, path
);
1952 spa_close(spa
, FTAG
);
1957 zfs_ioc_vdev_setfru(zfs_cmd_t
*zc
)
1960 char *fru
= zc
->zc_value
;
1961 uint64_t guid
= zc
->zc_guid
;
1964 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1968 error
= spa_vdev_setfru(spa
, guid
, fru
);
1969 spa_close(spa
, FTAG
);
1974 zfs_ioc_objset_stats_impl(zfs_cmd_t
*zc
, objset_t
*os
)
1979 dmu_objset_fast_stat(os
, &zc
->zc_objset_stats
);
1981 if (zc
->zc_nvlist_dst
!= 0 &&
1982 (error
= dsl_prop_get_all(os
, &nv
)) == 0) {
1983 dmu_objset_stats(os
, nv
);
1985 * NB: zvol_get_stats() will read the objset contents,
1986 * which we aren't supposed to do with a
1987 * DS_MODE_USER hold, because it could be
1988 * inconsistent. So this is a bit of a workaround...
1989 * XXX reading with out owning
1991 if (!zc
->zc_objset_stats
.dds_inconsistent
&&
1992 dmu_objset_type(os
) == DMU_OST_ZVOL
) {
1993 error
= zvol_get_stats(os
, nv
);
1998 error
= put_nvlist(zc
, nv
);
2007 * zc_name name of filesystem
2008 * zc_nvlist_dst_size size of buffer for property nvlist
2011 * zc_objset_stats stats
2012 * zc_nvlist_dst property nvlist
2013 * zc_nvlist_dst_size size of property nvlist
2016 zfs_ioc_objset_stats(zfs_cmd_t
*zc
)
2021 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
2023 error
= zfs_ioc_objset_stats_impl(zc
, os
);
2024 dmu_objset_rele(os
, FTAG
);
2032 * zc_name name of filesystem
2033 * zc_nvlist_dst_size size of buffer for property nvlist
2036 * zc_nvlist_dst received property nvlist
2037 * zc_nvlist_dst_size size of received property nvlist
2039 * Gets received properties (distinct from local properties on or after
2040 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
2041 * local property values.
2044 zfs_ioc_objset_recvd_props(zfs_cmd_t
*zc
)
2050 * Without this check, we would return local property values if the
2051 * caller has not already received properties on or after
2052 * SPA_VERSION_RECVD_PROPS.
2054 if (!dsl_prop_get_hasrecvd(zc
->zc_name
))
2055 return (SET_ERROR(ENOTSUP
));
2057 if (zc
->zc_nvlist_dst
!= 0 &&
2058 (error
= dsl_prop_get_received(zc
->zc_name
, &nv
)) == 0) {
2059 error
= put_nvlist(zc
, nv
);
2067 nvl_add_zplprop(objset_t
*os
, nvlist_t
*props
, zfs_prop_t prop
)
2073 * zfs_get_zplprop() will either find a value or give us
2074 * the default value (if there is one).
2076 if ((error
= zfs_get_zplprop(os
, prop
, &value
)) != 0)
2078 VERIFY(nvlist_add_uint64(props
, zfs_prop_to_name(prop
), value
) == 0);
2084 * zc_name name of filesystem
2085 * zc_nvlist_dst_size size of buffer for zpl property nvlist
2088 * zc_nvlist_dst zpl property nvlist
2089 * zc_nvlist_dst_size size of zpl property nvlist
2092 zfs_ioc_objset_zplprops(zfs_cmd_t
*zc
)
2097 /* XXX reading without owning */
2098 if (err
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
))
2101 dmu_objset_fast_stat(os
, &zc
->zc_objset_stats
);
2104 * NB: nvl_add_zplprop() will read the objset contents,
2105 * which we aren't supposed to do with a DS_MODE_USER
2106 * hold, because it could be inconsistent.
2108 if (zc
->zc_nvlist_dst
!= (uintptr_t)NULL
&&
2109 !zc
->zc_objset_stats
.dds_inconsistent
&&
2110 dmu_objset_type(os
) == DMU_OST_ZFS
) {
2113 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2114 if ((err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_VERSION
)) == 0 &&
2115 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_NORMALIZE
)) == 0 &&
2116 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_UTF8ONLY
)) == 0 &&
2117 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_CASE
)) == 0)
2118 err
= put_nvlist(zc
, nv
);
2121 err
= SET_ERROR(ENOENT
);
2123 dmu_objset_rele(os
, FTAG
);
2128 dataset_name_hidden(const char *name
)
2131 * Skip over datasets that are not visible in this zone,
2132 * internal datasets (which have a $ in their name), and
2133 * temporary datasets (which have a % in their name).
2135 if (strchr(name
, '$') != NULL
)
2137 if (strchr(name
, '%') != NULL
)
2139 if (!INGLOBALZONE(curproc
) && !zone_dataset_visible(name
, NULL
))
2146 * zc_name name of filesystem
2147 * zc_cookie zap cursor
2148 * zc_nvlist_dst_size size of buffer for property nvlist
2151 * zc_name name of next filesystem
2152 * zc_cookie zap cursor
2153 * zc_objset_stats stats
2154 * zc_nvlist_dst property nvlist
2155 * zc_nvlist_dst_size size of property nvlist
2158 zfs_ioc_dataset_list_next(zfs_cmd_t
*zc
)
2163 size_t orig_len
= strlen(zc
->zc_name
);
2166 if (error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
)) {
2167 if (error
== ENOENT
)
2168 error
= SET_ERROR(ESRCH
);
2172 p
= strrchr(zc
->zc_name
, '/');
2173 if (p
== NULL
|| p
[1] != '\0')
2174 (void) strlcat(zc
->zc_name
, "/", sizeof (zc
->zc_name
));
2175 p
= zc
->zc_name
+ strlen(zc
->zc_name
);
2178 error
= dmu_dir_list_next(os
,
2179 sizeof (zc
->zc_name
) - (p
- zc
->zc_name
), p
,
2180 NULL
, &zc
->zc_cookie
);
2181 if (error
== ENOENT
)
2182 error
= SET_ERROR(ESRCH
);
2183 } while (error
== 0 && dataset_name_hidden(zc
->zc_name
));
2184 dmu_objset_rele(os
, FTAG
);
2187 * If it's an internal dataset (ie. with a '$' in its name),
2188 * don't try to get stats for it, otherwise we'll return ENOENT.
2190 if (error
== 0 && strchr(zc
->zc_name
, '$') == NULL
) {
2191 error
= zfs_ioc_objset_stats(zc
); /* fill in the stats */
2192 if (error
== ENOENT
) {
2193 /* We lost a race with destroy, get the next one. */
2194 zc
->zc_name
[orig_len
] = '\0';
2203 * zc_name name of filesystem
2204 * zc_cookie zap cursor
2205 * zc_nvlist_dst_size size of buffer for property nvlist
2206 * zc_simple when set, only name is requested
2209 * zc_name name of next snapshot
2210 * zc_objset_stats stats
2211 * zc_nvlist_dst property nvlist
2212 * zc_nvlist_dst_size size of property nvlist
2215 zfs_ioc_snapshot_list_next(zfs_cmd_t
*zc
)
2220 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
2222 return (error
== ENOENT
? ESRCH
: error
);
2226 * A dataset name of maximum length cannot have any snapshots,
2227 * so exit immediately.
2229 if (strlcat(zc
->zc_name
, "@", sizeof (zc
->zc_name
)) >=
2230 ZFS_MAX_DATASET_NAME_LEN
) {
2231 dmu_objset_rele(os
, FTAG
);
2232 return (SET_ERROR(ESRCH
));
2235 error
= dmu_snapshot_list_next(os
,
2236 sizeof (zc
->zc_name
) - strlen(zc
->zc_name
),
2237 zc
->zc_name
+ strlen(zc
->zc_name
), &zc
->zc_obj
, &zc
->zc_cookie
,
2240 if (error
== 0 && !zc
->zc_simple
) {
2242 dsl_pool_t
*dp
= os
->os_dsl_dataset
->ds_dir
->dd_pool
;
2244 error
= dsl_dataset_hold_obj(dp
, zc
->zc_obj
, FTAG
, &ds
);
2248 error
= dmu_objset_from_ds(ds
, &ossnap
);
2250 error
= zfs_ioc_objset_stats_impl(zc
, ossnap
);
2251 dsl_dataset_rele(ds
, FTAG
);
2253 } else if (error
== ENOENT
) {
2254 error
= SET_ERROR(ESRCH
);
2257 dmu_objset_rele(os
, FTAG
);
2258 /* if we failed, undo the @ that we tacked on to zc_name */
2260 *strchr(zc
->zc_name
, '@') = '\0';
2265 zfs_prop_set_userquota(const char *dsname
, nvpair_t
*pair
)
2267 const char *propname
= nvpair_name(pair
);
2269 unsigned int vallen
;
2272 zfs_userquota_prop_t type
;
2278 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2280 VERIFY(nvpair_value_nvlist(pair
, &attrs
) == 0);
2281 if (nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
2283 return (SET_ERROR(EINVAL
));
2287 * A correctly constructed propname is encoded as
2288 * userquota@<rid>-<domain>.
2290 if ((dash
= strchr(propname
, '-')) == NULL
||
2291 nvpair_value_uint64_array(pair
, &valary
, &vallen
) != 0 ||
2293 return (SET_ERROR(EINVAL
));
2300 err
= zfsvfs_hold(dsname
, FTAG
, &zfsvfs
, B_FALSE
);
2302 err
= zfs_set_userquota(zfsvfs
, type
, domain
, rid
, quota
);
2303 zfsvfs_rele(zfsvfs
, FTAG
);
2310 * If the named property is one that has a special function to set its value,
2311 * return 0 on success and a positive error code on failure; otherwise if it is
2312 * not one of the special properties handled by this function, return -1.
2314 * XXX: It would be better for callers of the property interface if we handled
2315 * these special cases in dsl_prop.c (in the dsl layer).
2318 zfs_prop_set_special(const char *dsname
, zprop_source_t source
,
2321 const char *propname
= nvpair_name(pair
);
2322 zfs_prop_t prop
= zfs_name_to_prop(propname
);
2326 if (prop
== ZPROP_INVAL
) {
2327 if (zfs_prop_userquota(propname
))
2328 return (zfs_prop_set_userquota(dsname
, pair
));
2332 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2334 VERIFY(nvpair_value_nvlist(pair
, &attrs
) == 0);
2335 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
2339 if (zfs_prop_get_type(prop
) == PROP_TYPE_STRING
)
2342 VERIFY(0 == nvpair_value_uint64(pair
, &intval
));
2345 case ZFS_PROP_QUOTA
:
2346 err
= dsl_dir_set_quota(dsname
, source
, intval
);
2348 case ZFS_PROP_REFQUOTA
:
2349 err
= dsl_dataset_set_refquota(dsname
, source
, intval
);
2351 case ZFS_PROP_FILESYSTEM_LIMIT
:
2352 case ZFS_PROP_SNAPSHOT_LIMIT
:
2353 if (intval
== UINT64_MAX
) {
2354 /* clearing the limit, just do it */
2357 err
= dsl_dir_activate_fs_ss_limit(dsname
);
2360 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2361 * default path to set the value in the nvlist.
2366 case ZFS_PROP_RESERVATION
:
2367 err
= dsl_dir_set_reservation(dsname
, source
, intval
);
2369 case ZFS_PROP_REFRESERVATION
:
2370 err
= dsl_dataset_set_refreservation(dsname
, source
, intval
);
2372 case ZFS_PROP_VOLSIZE
:
2373 err
= zvol_set_volsize(dsname
, intval
);
2375 case ZFS_PROP_VERSION
:
2379 if ((err
= zfsvfs_hold(dsname
, FTAG
, &zfsvfs
, B_TRUE
)) != 0)
2382 err
= zfs_set_version(zfsvfs
, intval
);
2383 zfsvfs_rele(zfsvfs
, FTAG
);
2385 if (err
== 0 && intval
>= ZPL_VERSION_USERSPACE
) {
2388 zc
= kmem_zalloc(sizeof (zfs_cmd_t
), KM_SLEEP
);
2389 (void) strcpy(zc
->zc_name
, dsname
);
2390 (void) zfs_ioc_userspace_upgrade(zc
);
2391 kmem_free(zc
, sizeof (zfs_cmd_t
));
2403 * This function is best effort. If it fails to set any of the given properties,
2404 * it continues to set as many as it can and returns the last error
2405 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2406 * with the list of names of all the properties that failed along with the
2407 * corresponding error numbers.
2409 * If every property is set successfully, zero is returned and errlist is not
2413 zfs_set_prop_nvlist(const char *dsname
, zprop_source_t source
, nvlist_t
*nvl
,
2421 nvlist_t
*genericnvl
= fnvlist_alloc();
2422 nvlist_t
*retrynvl
= fnvlist_alloc();
2426 while ((pair
= nvlist_next_nvpair(nvl
, pair
)) != NULL
) {
2427 const char *propname
= nvpair_name(pair
);
2428 zfs_prop_t prop
= zfs_name_to_prop(propname
);
2431 /* decode the property value */
2433 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2435 attrs
= fnvpair_value_nvlist(pair
);
2436 if (nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
2438 err
= SET_ERROR(EINVAL
);
2441 /* Validate value type */
2442 if (err
== 0 && prop
== ZPROP_INVAL
) {
2443 if (zfs_prop_user(propname
)) {
2444 if (nvpair_type(propval
) != DATA_TYPE_STRING
)
2445 err
= SET_ERROR(EINVAL
);
2446 } else if (zfs_prop_userquota(propname
)) {
2447 if (nvpair_type(propval
) !=
2448 DATA_TYPE_UINT64_ARRAY
)
2449 err
= SET_ERROR(EINVAL
);
2451 err
= SET_ERROR(EINVAL
);
2453 } else if (err
== 0) {
2454 if (nvpair_type(propval
) == DATA_TYPE_STRING
) {
2455 if (zfs_prop_get_type(prop
) != PROP_TYPE_STRING
)
2456 err
= SET_ERROR(EINVAL
);
2457 } else if (nvpair_type(propval
) == DATA_TYPE_UINT64
) {
2460 intval
= fnvpair_value_uint64(propval
);
2462 switch (zfs_prop_get_type(prop
)) {
2463 case PROP_TYPE_NUMBER
:
2465 case PROP_TYPE_STRING
:
2466 err
= SET_ERROR(EINVAL
);
2468 case PROP_TYPE_INDEX
:
2469 if (zfs_prop_index_to_string(prop
,
2470 intval
, &unused
) != 0)
2471 err
= SET_ERROR(EINVAL
);
2475 "unknown property type");
2478 err
= SET_ERROR(EINVAL
);
2482 /* Validate permissions */
2484 err
= zfs_check_settable(dsname
, pair
, CRED());
2487 err
= zfs_prop_set_special(dsname
, source
, pair
);
2490 * For better performance we build up a list of
2491 * properties to set in a single transaction.
2493 err
= nvlist_add_nvpair(genericnvl
, pair
);
2494 } else if (err
!= 0 && nvl
!= retrynvl
) {
2496 * This may be a spurious error caused by
2497 * receiving quota and reservation out of order.
2498 * Try again in a second pass.
2500 err
= nvlist_add_nvpair(retrynvl
, pair
);
2505 if (errlist
!= NULL
)
2506 fnvlist_add_int32(errlist
, propname
, err
);
2511 if (nvl
!= retrynvl
&& !nvlist_empty(retrynvl
)) {
2516 if (!nvlist_empty(genericnvl
) &&
2517 dsl_props_set(dsname
, source
, genericnvl
) != 0) {
2519 * If this fails, we still want to set as many properties as we
2520 * can, so try setting them individually.
2523 while ((pair
= nvlist_next_nvpair(genericnvl
, pair
)) != NULL
) {
2524 const char *propname
= nvpair_name(pair
);
2528 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2530 attrs
= fnvpair_value_nvlist(pair
);
2531 propval
= fnvlist_lookup_nvpair(attrs
,
2535 if (nvpair_type(propval
) == DATA_TYPE_STRING
) {
2536 strval
= fnvpair_value_string(propval
);
2537 err
= dsl_prop_set_string(dsname
, propname
,
2540 intval
= fnvpair_value_uint64(propval
);
2541 err
= dsl_prop_set_int(dsname
, propname
, source
,
2546 if (errlist
!= NULL
) {
2547 fnvlist_add_int32(errlist
, propname
,
2554 nvlist_free(genericnvl
);
2555 nvlist_free(retrynvl
);
2561 * Check that all the properties are valid user properties.
2564 zfs_check_userprops(const char *fsname
, nvlist_t
*nvl
)
2566 nvpair_t
*pair
= NULL
;
2569 while ((pair
= nvlist_next_nvpair(nvl
, pair
)) != NULL
) {
2570 const char *propname
= nvpair_name(pair
);
2572 if (!zfs_prop_user(propname
) ||
2573 nvpair_type(pair
) != DATA_TYPE_STRING
)
2574 return (SET_ERROR(EINVAL
));
2576 if (error
= zfs_secpolicy_write_perms(fsname
,
2577 ZFS_DELEG_PERM_USERPROP
, CRED()))
2580 if (strlen(propname
) >= ZAP_MAXNAMELEN
)
2581 return (SET_ERROR(ENAMETOOLONG
));
2583 if (strlen(fnvpair_value_string(pair
)) >= ZAP_MAXVALUELEN
)
2590 props_skip(nvlist_t
*props
, nvlist_t
*skipped
, nvlist_t
**newprops
)
2594 VERIFY(nvlist_alloc(newprops
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2597 while ((pair
= nvlist_next_nvpair(props
, pair
)) != NULL
) {
2598 if (nvlist_exists(skipped
, nvpair_name(pair
)))
2601 VERIFY(nvlist_add_nvpair(*newprops
, pair
) == 0);
2606 clear_received_props(const char *dsname
, nvlist_t
*props
,
2610 nvlist_t
*cleared_props
= NULL
;
2611 props_skip(props
, skipped
, &cleared_props
);
2612 if (!nvlist_empty(cleared_props
)) {
2614 * Acts on local properties until the dataset has received
2615 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2617 zprop_source_t flags
= (ZPROP_SRC_NONE
|
2618 (dsl_prop_get_hasrecvd(dsname
) ? ZPROP_SRC_RECEIVED
: 0));
2619 err
= zfs_set_prop_nvlist(dsname
, flags
, cleared_props
, NULL
);
2621 nvlist_free(cleared_props
);
2627 * zc_name name of filesystem
2628 * zc_value name of property to set
2629 * zc_nvlist_src{_size} nvlist of properties to apply
2630 * zc_cookie received properties flag
2633 * zc_nvlist_dst{_size} error for each unapplied received property
2636 zfs_ioc_set_prop(zfs_cmd_t
*zc
)
2639 boolean_t received
= zc
->zc_cookie
;
2640 zprop_source_t source
= (received
? ZPROP_SRC_RECEIVED
:
2645 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2646 zc
->zc_iflags
, &nvl
)) != 0)
2650 nvlist_t
*origprops
;
2652 if (dsl_prop_get_received(zc
->zc_name
, &origprops
) == 0) {
2653 (void) clear_received_props(zc
->zc_name
,
2655 nvlist_free(origprops
);
2658 error
= dsl_prop_set_hasrecvd(zc
->zc_name
);
2661 errors
= fnvlist_alloc();
2663 error
= zfs_set_prop_nvlist(zc
->zc_name
, source
, nvl
, errors
);
2665 if (zc
->zc_nvlist_dst
!= (uintptr_t)NULL
&& errors
!= NULL
) {
2666 (void) put_nvlist(zc
, errors
);
2669 nvlist_free(errors
);
2676 * zc_name name of filesystem
2677 * zc_value name of property to inherit
2678 * zc_cookie revert to received value if TRUE
2683 zfs_ioc_inherit_prop(zfs_cmd_t
*zc
)
2685 const char *propname
= zc
->zc_value
;
2686 zfs_prop_t prop
= zfs_name_to_prop(propname
);
2687 boolean_t received
= zc
->zc_cookie
;
2688 zprop_source_t source
= (received
2689 ? ZPROP_SRC_NONE
/* revert to received value, if any */
2690 : ZPROP_SRC_INHERITED
); /* explicitly inherit */
2699 * zfs_prop_set_special() expects properties in the form of an
2700 * nvpair with type info.
2702 if (prop
== ZPROP_INVAL
) {
2703 if (!zfs_prop_user(propname
))
2704 return (SET_ERROR(EINVAL
));
2706 type
= PROP_TYPE_STRING
;
2707 } else if (prop
== ZFS_PROP_VOLSIZE
||
2708 prop
== ZFS_PROP_VERSION
) {
2709 return (SET_ERROR(EINVAL
));
2711 type
= zfs_prop_get_type(prop
);
2714 VERIFY(nvlist_alloc(&dummy
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2717 case PROP_TYPE_STRING
:
2718 VERIFY(0 == nvlist_add_string(dummy
, propname
, ""));
2720 case PROP_TYPE_NUMBER
:
2721 case PROP_TYPE_INDEX
:
2722 VERIFY(0 == nvlist_add_uint64(dummy
, propname
, 0));
2726 return (SET_ERROR(EINVAL
));
2729 pair
= nvlist_next_nvpair(dummy
, NULL
);
2730 err
= zfs_prop_set_special(zc
->zc_name
, source
, pair
);
2733 return (err
); /* special property already handled */
2736 * Only check this in the non-received case. We want to allow
2737 * 'inherit -S' to revert non-inheritable properties like quota
2738 * and reservation to the received or default values even though
2739 * they are not considered inheritable.
2741 if (prop
!= ZPROP_INVAL
&& !zfs_prop_inheritable(prop
))
2742 return (SET_ERROR(EINVAL
));
2745 /* property name has been validated by zfs_secpolicy_inherit_prop() */
2746 return (dsl_prop_inherit(zc
->zc_name
, zc
->zc_value
, source
));
2750 zfs_ioc_pool_set_props(zfs_cmd_t
*zc
)
2757 if (error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2758 zc
->zc_iflags
, &props
))
2762 * If the only property is the configfile, then just do a spa_lookup()
2763 * to handle the faulted case.
2765 pair
= nvlist_next_nvpair(props
, NULL
);
2766 if (pair
!= NULL
&& strcmp(nvpair_name(pair
),
2767 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
)) == 0 &&
2768 nvlist_next_nvpair(props
, pair
) == NULL
) {
2769 mutex_enter(&spa_namespace_lock
);
2770 if ((spa
= spa_lookup(zc
->zc_name
)) != NULL
) {
2771 spa_configfile_set(spa
, props
, B_FALSE
);
2772 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
2774 mutex_exit(&spa_namespace_lock
);
2781 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0) {
2786 error
= spa_prop_set(spa
, props
);
2789 spa_close(spa
, FTAG
);
2795 zfs_ioc_pool_get_props(zfs_cmd_t
*zc
)
2799 nvlist_t
*nvp
= NULL
;
2801 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0) {
2803 * If the pool is faulted, there may be properties we can still
2804 * get (such as altroot and cachefile), so attempt to get them
2807 mutex_enter(&spa_namespace_lock
);
2808 if ((spa
= spa_lookup(zc
->zc_name
)) != NULL
)
2809 error
= spa_prop_get(spa
, &nvp
);
2810 mutex_exit(&spa_namespace_lock
);
2812 error
= spa_prop_get(spa
, &nvp
);
2813 spa_close(spa
, FTAG
);
2816 if (error
== 0 && zc
->zc_nvlist_dst
!= (uintptr_t)NULL
)
2817 error
= put_nvlist(zc
, nvp
);
2819 error
= SET_ERROR(EFAULT
);
2827 * zc_name name of filesystem
2828 * zc_nvlist_src{_size} nvlist of delegated permissions
2829 * zc_perm_action allow/unallow flag
2834 zfs_ioc_set_fsacl(zfs_cmd_t
*zc
)
2837 nvlist_t
*fsaclnv
= NULL
;
2839 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2840 zc
->zc_iflags
, &fsaclnv
)) != 0)
2844 * Verify nvlist is constructed correctly
2846 if ((error
= zfs_deleg_verify_nvlist(fsaclnv
)) != 0) {
2847 nvlist_free(fsaclnv
);
2848 return (SET_ERROR(EINVAL
));
2852 * If we don't have PRIV_SYS_MOUNT, then validate
2853 * that user is allowed to hand out each permission in
2857 error
= secpolicy_zfs(CRED());
2859 if (zc
->zc_perm_action
== B_FALSE
) {
2860 error
= dsl_deleg_can_allow(zc
->zc_name
,
2863 error
= dsl_deleg_can_unallow(zc
->zc_name
,
2869 error
= dsl_deleg_set(zc
->zc_name
, fsaclnv
, zc
->zc_perm_action
);
2871 nvlist_free(fsaclnv
);
2877 * zc_name name of filesystem
2880 * zc_nvlist_src{_size} nvlist of delegated permissions
2883 zfs_ioc_get_fsacl(zfs_cmd_t
*zc
)
2888 if ((error
= dsl_deleg_get(zc
->zc_name
, &nvp
)) == 0) {
2889 error
= put_nvlist(zc
, nvp
);
2898 zfs_create_cb(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
)
2900 zfs_creat_t
*zct
= arg
;
2902 zfs_create_fs(os
, cr
, zct
->zct_zplprops
, tx
);
2905 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
2909 * os parent objset pointer (NULL if root fs)
2910 * fuids_ok fuids allowed in this version of the spa?
2911 * sa_ok SAs allowed in this version of the spa?
2912 * createprops list of properties requested by creator
2915 * zplprops values for the zplprops we attach to the master node object
2916 * is_ci true if requested file system will be purely case-insensitive
2918 * Determine the settings for utf8only, normalization and
2919 * casesensitivity. Specific values may have been requested by the
2920 * creator and/or we can inherit values from the parent dataset. If
2921 * the file system is of too early a vintage, a creator can not
2922 * request settings for these properties, even if the requested
2923 * setting is the default value. We don't actually want to create dsl
2924 * properties for these, so remove them from the source nvlist after
2928 zfs_fill_zplprops_impl(objset_t
*os
, uint64_t zplver
,
2929 boolean_t fuids_ok
, boolean_t sa_ok
, nvlist_t
*createprops
,
2930 nvlist_t
*zplprops
, boolean_t
*is_ci
)
2932 uint64_t sense
= ZFS_PROP_UNDEFINED
;
2933 uint64_t norm
= ZFS_PROP_UNDEFINED
;
2934 uint64_t u8
= ZFS_PROP_UNDEFINED
;
2936 ASSERT(zplprops
!= NULL
);
2938 if (os
!= NULL
&& os
->os_phys
->os_type
!= DMU_OST_ZFS
)
2939 return (SET_ERROR(EINVAL
));
2942 * Pull out creator prop choices, if any.
2945 (void) nvlist_lookup_uint64(createprops
,
2946 zfs_prop_to_name(ZFS_PROP_VERSION
), &zplver
);
2947 (void) nvlist_lookup_uint64(createprops
,
2948 zfs_prop_to_name(ZFS_PROP_NORMALIZE
), &norm
);
2949 (void) nvlist_remove_all(createprops
,
2950 zfs_prop_to_name(ZFS_PROP_NORMALIZE
));
2951 (void) nvlist_lookup_uint64(createprops
,
2952 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), &u8
);
2953 (void) nvlist_remove_all(createprops
,
2954 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
));
2955 (void) nvlist_lookup_uint64(createprops
,
2956 zfs_prop_to_name(ZFS_PROP_CASE
), &sense
);
2957 (void) nvlist_remove_all(createprops
,
2958 zfs_prop_to_name(ZFS_PROP_CASE
));
2962 * If the zpl version requested is whacky or the file system
2963 * or pool is version is too "young" to support normalization
2964 * and the creator tried to set a value for one of the props,
2967 if ((zplver
< ZPL_VERSION_INITIAL
|| zplver
> ZPL_VERSION
) ||
2968 (zplver
>= ZPL_VERSION_FUID
&& !fuids_ok
) ||
2969 (zplver
>= ZPL_VERSION_SA
&& !sa_ok
) ||
2970 (zplver
< ZPL_VERSION_NORMALIZATION
&&
2971 (norm
!= ZFS_PROP_UNDEFINED
|| u8
!= ZFS_PROP_UNDEFINED
||
2972 sense
!= ZFS_PROP_UNDEFINED
)))
2973 return (SET_ERROR(ENOTSUP
));
2976 * Put the version in the zplprops
2978 VERIFY(nvlist_add_uint64(zplprops
,
2979 zfs_prop_to_name(ZFS_PROP_VERSION
), zplver
) == 0);
2981 if (norm
== ZFS_PROP_UNDEFINED
)
2982 VERIFY(zfs_get_zplprop(os
, ZFS_PROP_NORMALIZE
, &norm
) == 0);
2983 VERIFY(nvlist_add_uint64(zplprops
,
2984 zfs_prop_to_name(ZFS_PROP_NORMALIZE
), norm
) == 0);
2987 * If we're normalizing, names must always be valid UTF-8 strings.
2991 if (u8
== ZFS_PROP_UNDEFINED
)
2992 VERIFY(zfs_get_zplprop(os
, ZFS_PROP_UTF8ONLY
, &u8
) == 0);
2993 VERIFY(nvlist_add_uint64(zplprops
,
2994 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), u8
) == 0);
2996 if (sense
== ZFS_PROP_UNDEFINED
)
2997 VERIFY(zfs_get_zplprop(os
, ZFS_PROP_CASE
, &sense
) == 0);
2998 VERIFY(nvlist_add_uint64(zplprops
,
2999 zfs_prop_to_name(ZFS_PROP_CASE
), sense
) == 0);
3002 *is_ci
= (sense
== ZFS_CASE_INSENSITIVE
);
3008 zfs_fill_zplprops(const char *dataset
, nvlist_t
*createprops
,
3009 nvlist_t
*zplprops
, boolean_t
*is_ci
)
3011 boolean_t fuids_ok
, sa_ok
;
3012 uint64_t zplver
= ZPL_VERSION
;
3013 objset_t
*os
= NULL
;
3014 char parentname
[ZFS_MAX_DATASET_NAME_LEN
];
3020 (void) strlcpy(parentname
, dataset
, sizeof (parentname
));
3021 cp
= strrchr(parentname
, '/');
3025 if ((error
= spa_open(dataset
, &spa
, FTAG
)) != 0)
3028 spa_vers
= spa_version(spa
);
3029 spa_close(spa
, FTAG
);
3031 zplver
= zfs_zpl_version_map(spa_vers
);
3032 fuids_ok
= (zplver
>= ZPL_VERSION_FUID
);
3033 sa_ok
= (zplver
>= ZPL_VERSION_SA
);
3036 * Open parent object set so we can inherit zplprop values.
3038 if ((error
= dmu_objset_hold(parentname
, FTAG
, &os
)) != 0)
3041 error
= zfs_fill_zplprops_impl(os
, zplver
, fuids_ok
, sa_ok
, createprops
,
3043 dmu_objset_rele(os
, FTAG
);
3048 zfs_fill_zplprops_root(uint64_t spa_vers
, nvlist_t
*createprops
,
3049 nvlist_t
*zplprops
, boolean_t
*is_ci
)
3053 uint64_t zplver
= ZPL_VERSION
;
3056 zplver
= zfs_zpl_version_map(spa_vers
);
3057 fuids_ok
= (zplver
>= ZPL_VERSION_FUID
);
3058 sa_ok
= (zplver
>= ZPL_VERSION_SA
);
3060 error
= zfs_fill_zplprops_impl(NULL
, zplver
, fuids_ok
, sa_ok
,
3061 createprops
, zplprops
, is_ci
);
3067 * "type" -> dmu_objset_type_t (int32)
3068 * (optional) "props" -> { prop -> value }
3071 * outnvl: propname -> error code (int32)
3074 zfs_ioc_create(const char *fsname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3077 zfs_creat_t zct
= { 0 };
3078 nvlist_t
*nvprops
= NULL
;
3079 void (*cbfunc
)(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
);
3081 dmu_objset_type_t type
;
3082 boolean_t is_insensitive
= B_FALSE
;
3084 if (nvlist_lookup_int32(innvl
, "type", &type32
) != 0)
3085 return (SET_ERROR(EINVAL
));
3087 (void) nvlist_lookup_nvlist(innvl
, "props", &nvprops
);
3091 cbfunc
= zfs_create_cb
;
3095 cbfunc
= zvol_create_cb
;
3102 if (strchr(fsname
, '@') ||
3103 strchr(fsname
, '%'))
3104 return (SET_ERROR(EINVAL
));
3106 zct
.zct_props
= nvprops
;
3109 return (SET_ERROR(EINVAL
));
3111 if (type
== DMU_OST_ZVOL
) {
3112 uint64_t volsize
, volblocksize
;
3114 if (nvprops
== NULL
)
3115 return (SET_ERROR(EINVAL
));
3116 if (nvlist_lookup_uint64(nvprops
,
3117 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &volsize
) != 0)
3118 return (SET_ERROR(EINVAL
));
3120 if ((error
= nvlist_lookup_uint64(nvprops
,
3121 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
3122 &volblocksize
)) != 0 && error
!= ENOENT
)
3123 return (SET_ERROR(EINVAL
));
3126 volblocksize
= zfs_prop_default_numeric(
3127 ZFS_PROP_VOLBLOCKSIZE
);
3129 if ((error
= zvol_check_volblocksize(
3130 volblocksize
)) != 0 ||
3131 (error
= zvol_check_volsize(volsize
,
3132 volblocksize
)) != 0)
3134 } else if (type
== DMU_OST_ZFS
) {
3138 * We have to have normalization and
3139 * case-folding flags correct when we do the
3140 * file system creation, so go figure them out
3143 VERIFY(nvlist_alloc(&zct
.zct_zplprops
,
3144 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3145 error
= zfs_fill_zplprops(fsname
, nvprops
,
3146 zct
.zct_zplprops
, &is_insensitive
);
3148 nvlist_free(zct
.zct_zplprops
);
3153 error
= dmu_objset_create(fsname
, type
,
3154 is_insensitive
? DS_FLAG_CI_DATASET
: 0, cbfunc
, &zct
);
3155 nvlist_free(zct
.zct_zplprops
);
3158 * It would be nice to do this atomically.
3161 error
= zfs_set_prop_nvlist(fsname
, ZPROP_SRC_LOCAL
,
3164 (void) dsl_destroy_head(fsname
);
3171 * "origin" -> name of origin snapshot
3172 * (optional) "props" -> { prop -> value }
3175 * outnvl: propname -> error code (int32)
3178 zfs_ioc_clone(const char *fsname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3181 nvlist_t
*nvprops
= NULL
;
3184 if (nvlist_lookup_string(innvl
, "origin", &origin_name
) != 0)
3185 return (SET_ERROR(EINVAL
));
3186 (void) nvlist_lookup_nvlist(innvl
, "props", &nvprops
);
3188 if (strchr(fsname
, '@') ||
3189 strchr(fsname
, '%'))
3190 return (SET_ERROR(EINVAL
));
3192 if (dataset_namecheck(origin_name
, NULL
, NULL
) != 0)
3193 return (SET_ERROR(EINVAL
));
3194 error
= dmu_objset_clone(fsname
, origin_name
);
3199 * It would be nice to do this atomically.
3202 error
= zfs_set_prop_nvlist(fsname
, ZPROP_SRC_LOCAL
,
3205 (void) dsl_destroy_head(fsname
);
3212 zfs_ioc_remap(const char *fsname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3214 if (strchr(fsname
, '@') ||
3215 strchr(fsname
, '%'))
3216 return (SET_ERROR(EINVAL
));
3218 return (dmu_objset_remap_indirects(fsname
));
3223 * "snaps" -> { snapshot1, snapshot2 }
3224 * (optional) "props" -> { prop -> value (string) }
3227 * outnvl: snapshot -> error code (int32)
3230 zfs_ioc_snapshot(const char *poolname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3233 nvlist_t
*props
= NULL
;
3237 (void) nvlist_lookup_nvlist(innvl
, "props", &props
);
3238 if ((error
= zfs_check_userprops(poolname
, props
)) != 0)
3241 if (!nvlist_empty(props
) &&
3242 zfs_earlier_version(poolname
, SPA_VERSION_SNAP_PROPS
))
3243 return (SET_ERROR(ENOTSUP
));
3245 if (nvlist_lookup_nvlist(innvl
, "snaps", &snaps
) != 0)
3246 return (SET_ERROR(EINVAL
));
3247 poollen
= strlen(poolname
);
3248 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
3249 pair
= nvlist_next_nvpair(snaps
, pair
)) {
3250 const char *name
= nvpair_name(pair
);
3251 const char *cp
= strchr(name
, '@');
3254 * The snap name must contain an @, and the part after it must
3255 * contain only valid characters.
3258 zfs_component_namecheck(cp
+ 1, NULL
, NULL
) != 0)
3259 return (SET_ERROR(EINVAL
));
3262 * The snap must be in the specified pool.
3264 if (strncmp(name
, poolname
, poollen
) != 0 ||
3265 (name
[poollen
] != '/' && name
[poollen
] != '@'))
3266 return (SET_ERROR(EXDEV
));
3268 /* This must be the only snap of this fs. */
3269 for (nvpair_t
*pair2
= nvlist_next_nvpair(snaps
, pair
);
3270 pair2
!= NULL
; pair2
= nvlist_next_nvpair(snaps
, pair2
)) {
3271 if (strncmp(name
, nvpair_name(pair2
), cp
- name
+ 1)
3273 return (SET_ERROR(EXDEV
));
3278 error
= dsl_dataset_snapshot(snaps
, props
, outnvl
);
3283 * innvl: "message" -> string
3287 zfs_ioc_log_history(const char *unused
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3295 * The poolname in the ioctl is not set, we get it from the TSD,
3296 * which was set at the end of the last successful ioctl that allows
3297 * logging. The secpolicy func already checked that it is set.
3298 * Only one log ioctl is allowed after each successful ioctl, so
3299 * we clear the TSD here.
3301 poolname
= tsd_get(zfs_allow_log_key
);
3302 (void) tsd_set(zfs_allow_log_key
, NULL
);
3303 error
= spa_open(poolname
, &spa
, FTAG
);
3308 if (nvlist_lookup_string(innvl
, "message", &message
) != 0) {
3309 spa_close(spa
, FTAG
);
3310 return (SET_ERROR(EINVAL
));
3313 if (spa_version(spa
) < SPA_VERSION_ZPOOL_HISTORY
) {
3314 spa_close(spa
, FTAG
);
3315 return (SET_ERROR(ENOTSUP
));
3318 error
= spa_history_log(spa
, message
);
3319 spa_close(spa
, FTAG
);
3324 * The dp_config_rwlock must not be held when calling this, because the
3325 * unmount may need to write out data.
3327 * This function is best-effort. Callers must deal gracefully if it
3328 * remains mounted (or is remounted after this call).
3330 * Returns 0 if the argument is not a snapshot, or it is not currently a
3331 * filesystem, or we were able to unmount it. Returns error code otherwise.
3334 zfs_unmount_snap(const char *snapname
)
3337 zfsvfs_t
*zfsvfs
= NULL
;
3339 if (strchr(snapname
, '@') == NULL
)
3342 int err
= getzfsvfs(snapname
, &zfsvfs
);
3344 ASSERT3P(zfsvfs
, ==, NULL
);
3347 vfsp
= zfsvfs
->z_vfs
;
3349 ASSERT(!dsl_pool_config_held(dmu_objset_pool(zfsvfs
->z_os
)));
3351 err
= vn_vfswlock(vfsp
->vfs_vnodecovered
);
3357 * Always force the unmount for snapshots.
3359 (void) dounmount(vfsp
, MS_FORCE
, kcred
);
3364 zfs_unmount_snap_cb(const char *snapname
, void *arg
)
3366 zfs_unmount_snap(snapname
);
3371 * When a clone is destroyed, its origin may also need to be destroyed,
3372 * in which case it must be unmounted. This routine will do that unmount
3376 zfs_destroy_unmount_origin(const char *fsname
)
3382 error
= dmu_objset_hold(fsname
, FTAG
, &os
);
3385 ds
= dmu_objset_ds(os
);
3386 if (dsl_dir_is_clone(ds
->ds_dir
) && DS_IS_DEFER_DESTROY(ds
->ds_prev
)) {
3387 char originname
[ZFS_MAX_DATASET_NAME_LEN
];
3388 dsl_dataset_name(ds
->ds_prev
, originname
);
3389 dmu_objset_rele(os
, FTAG
);
3390 zfs_unmount_snap(originname
);
3392 dmu_objset_rele(os
, FTAG
);
3398 * "snaps" -> { snapshot1, snapshot2 }
3399 * (optional boolean) "defer"
3402 * outnvl: snapshot -> error code (int32)
3407 zfs_ioc_destroy_snaps(const char *poolname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3413 if (nvlist_lookup_nvlist(innvl
, "snaps", &snaps
) != 0)
3414 return (SET_ERROR(EINVAL
));
3415 defer
= nvlist_exists(innvl
, "defer");
3417 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
3418 pair
= nvlist_next_nvpair(snaps
, pair
)) {
3419 zfs_unmount_snap(nvpair_name(pair
));
3422 return (dsl_destroy_snapshots_nvl(snaps
, defer
, outnvl
));
3426 * Create bookmarks. Bookmark names are of the form <fs>#<bmark>.
3427 * All bookmarks must be in the same pool.
3430 * bookmark1 -> snapshot1, bookmark2 -> snapshot2
3433 * outnvl: bookmark -> error code (int32)
3438 zfs_ioc_bookmark(const char *poolname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3440 for (nvpair_t
*pair
= nvlist_next_nvpair(innvl
, NULL
);
3441 pair
!= NULL
; pair
= nvlist_next_nvpair(innvl
, pair
)) {
3445 * Verify the snapshot argument.
3447 if (nvpair_value_string(pair
, &snap_name
) != 0)
3448 return (SET_ERROR(EINVAL
));
3451 /* Verify that the keys (bookmarks) are unique */
3452 for (nvpair_t
*pair2
= nvlist_next_nvpair(innvl
, pair
);
3453 pair2
!= NULL
; pair2
= nvlist_next_nvpair(innvl
, pair2
)) {
3454 if (strcmp(nvpair_name(pair
), nvpair_name(pair2
)) == 0)
3455 return (SET_ERROR(EINVAL
));
3459 return (dsl_bookmark_create(innvl
, outnvl
));
3464 * property 1, property 2, ...
3468 * bookmark name 1 -> { property 1, property 2, ... },
3469 * bookmark name 2 -> { property 1, property 2, ... }
3474 zfs_ioc_get_bookmarks(const char *fsname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3476 return (dsl_get_bookmarks(fsname
, innvl
, outnvl
));
3481 * bookmark name 1, bookmark name 2
3484 * outnvl: bookmark -> error code (int32)
3488 zfs_ioc_destroy_bookmarks(const char *poolname
, nvlist_t
*innvl
,
3493 poollen
= strlen(poolname
);
3494 for (nvpair_t
*pair
= nvlist_next_nvpair(innvl
, NULL
);
3495 pair
!= NULL
; pair
= nvlist_next_nvpair(innvl
, pair
)) {
3496 const char *name
= nvpair_name(pair
);
3497 const char *cp
= strchr(name
, '#');
3500 * The bookmark name must contain an #, and the part after it
3501 * must contain only valid characters.
3504 zfs_component_namecheck(cp
+ 1, NULL
, NULL
) != 0)
3505 return (SET_ERROR(EINVAL
));
3508 * The bookmark must be in the specified pool.
3510 if (strncmp(name
, poolname
, poollen
) != 0 ||
3511 (name
[poollen
] != '/' && name
[poollen
] != '#'))
3512 return (SET_ERROR(EXDEV
));
3515 error
= dsl_bookmark_destroy(innvl
, outnvl
);
3520 zfs_ioc_channel_program(const char *poolname
, nvlist_t
*innvl
,
3524 uint64_t instrlimit
, memlimit
;
3525 boolean_t sync_flag
;
3526 nvpair_t
*nvarg
= NULL
;
3528 if (0 != nvlist_lookup_string(innvl
, ZCP_ARG_PROGRAM
, &program
)) {
3531 if (0 != nvlist_lookup_boolean_value(innvl
, ZCP_ARG_SYNC
, &sync_flag
)) {
3534 if (0 != nvlist_lookup_uint64(innvl
, ZCP_ARG_INSTRLIMIT
, &instrlimit
)) {
3535 instrlimit
= ZCP_DEFAULT_INSTRLIMIT
;
3537 if (0 != nvlist_lookup_uint64(innvl
, ZCP_ARG_MEMLIMIT
, &memlimit
)) {
3538 memlimit
= ZCP_DEFAULT_MEMLIMIT
;
3540 if (0 != nvlist_lookup_nvpair(innvl
, ZCP_ARG_ARGLIST
, &nvarg
)) {
3544 if (instrlimit
== 0 || instrlimit
> zfs_lua_max_instrlimit
)
3546 if (memlimit
== 0 || memlimit
> zfs_lua_max_memlimit
)
3549 return (zcp_eval(poolname
, program
, sync_flag
, instrlimit
, memlimit
,
3559 zfs_ioc_pool_checkpoint(const char *poolname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3561 return (spa_checkpoint(poolname
));
3570 zfs_ioc_pool_discard_checkpoint(const char *poolname
, nvlist_t
*innvl
,
3573 return (spa_checkpoint_discard(poolname
));
3578 * zc_name name of dataset to destroy
3579 * zc_defer_destroy mark for deferred destroy
3584 zfs_ioc_destroy(zfs_cmd_t
*zc
)
3587 dmu_objset_type_t ost
;
3590 err
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
3593 ost
= dmu_objset_type(os
);
3594 dmu_objset_rele(os
, FTAG
);
3596 if (ost
== DMU_OST_ZFS
)
3597 zfs_unmount_snap(zc
->zc_name
);
3599 if (strchr(zc
->zc_name
, '@'))
3600 err
= dsl_destroy_snapshot(zc
->zc_name
, zc
->zc_defer_destroy
);
3602 err
= dsl_destroy_head(zc
->zc_name
);
3603 if (ost
== DMU_OST_ZVOL
&& err
== 0)
3604 (void) zvol_remove_minor(zc
->zc_name
);
3611 * guid 1, guid 2, ...
3613 * func: POOL_INITIALIZE_{CANCEL|DO|SUSPEND}
3617 * [func: EINVAL (if provided command type didn't make sense)],
3619 * guid1: errno, (see function body for possible errnos)
3626 zfs_ioc_pool_initialize(const char *poolname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3631 error
= spa_open(poolname
, &spa
, FTAG
);
3636 if (nvlist_lookup_uint64(innvl
, ZPOOL_INITIALIZE_COMMAND
,
3638 spa_close(spa
, FTAG
);
3639 return (SET_ERROR(EINVAL
));
3641 if (!(cmd_type
== POOL_INITIALIZE_CANCEL
||
3642 cmd_type
== POOL_INITIALIZE_DO
||
3643 cmd_type
== POOL_INITIALIZE_SUSPEND
)) {
3644 spa_close(spa
, FTAG
);
3645 return (SET_ERROR(EINVAL
));
3648 nvlist_t
*vdev_guids
;
3649 if (nvlist_lookup_nvlist(innvl
, ZPOOL_INITIALIZE_VDEVS
,
3650 &vdev_guids
) != 0) {
3651 spa_close(spa
, FTAG
);
3652 return (SET_ERROR(EINVAL
));
3655 nvlist_t
*vdev_errlist
= fnvlist_alloc();
3656 int total_errors
= 0;
3658 for (nvpair_t
*pair
= nvlist_next_nvpair(vdev_guids
, NULL
);
3659 pair
!= NULL
; pair
= nvlist_next_nvpair(vdev_guids
, pair
)) {
3660 uint64_t vdev_guid
= fnvpair_value_uint64(pair
);
3662 error
= spa_vdev_initialize(spa
, vdev_guid
, cmd_type
);
3664 char guid_as_str
[MAXNAMELEN
];
3666 (void) snprintf(guid_as_str
, sizeof (guid_as_str
),
3667 "%llu", (unsigned long long)vdev_guid
);
3668 fnvlist_add_int64(vdev_errlist
, guid_as_str
, error
);
3672 if (fnvlist_size(vdev_errlist
) > 0) {
3673 fnvlist_add_nvlist(outnvl
, ZPOOL_INITIALIZE_VDEVS
,
3676 fnvlist_free(vdev_errlist
);
3678 spa_close(spa
, FTAG
);
3679 return (total_errors
> 0 ? EINVAL
: 0);
3683 * fsname is name of dataset to rollback (to most recent snapshot)
3685 * innvl may contain name of expected target snapshot
3687 * outnvl: "target" -> name of most recent snapshot
3692 zfs_ioc_rollback(const char *fsname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3695 char *target
= NULL
;
3698 (void) nvlist_lookup_string(innvl
, "target", &target
);
3699 if (target
!= NULL
) {
3700 const char *cp
= strchr(target
, '@');
3703 * The snap name must contain an @, and the part after it must
3704 * contain only valid characters.
3707 zfs_component_namecheck(cp
+ 1, NULL
, NULL
) != 0)
3708 return (SET_ERROR(EINVAL
));
3711 if (getzfsvfs(fsname
, &zfsvfs
) == 0) {
3714 ds
= dmu_objset_ds(zfsvfs
->z_os
);
3715 error
= zfs_suspend_fs(zfsvfs
);
3719 error
= dsl_dataset_rollback(fsname
, target
, zfsvfs
,
3721 resume_err
= zfs_resume_fs(zfsvfs
, ds
);
3722 error
= error
? error
: resume_err
;
3724 VFS_RELE(zfsvfs
->z_vfs
);
3726 error
= dsl_dataset_rollback(fsname
, target
, NULL
, outnvl
);
3732 recursive_unmount(const char *fsname
, void *arg
)
3734 const char *snapname
= arg
;
3735 char fullname
[ZFS_MAX_DATASET_NAME_LEN
];
3737 (void) snprintf(fullname
, sizeof (fullname
), "%s@%s", fsname
, snapname
);
3738 zfs_unmount_snap(fullname
);
3745 * zc_name old name of dataset
3746 * zc_value new name of dataset
3747 * zc_cookie recursive flag (only valid for snapshots)
3752 zfs_ioc_rename(zfs_cmd_t
*zc
)
3755 dmu_objset_type_t ost
;
3756 boolean_t recursive
= zc
->zc_cookie
& 1;
3760 /* "zfs rename" from and to ...%recv datasets should both fail */
3761 zc
->zc_name
[sizeof (zc
->zc_name
) - 1] = '\0';
3762 zc
->zc_value
[sizeof (zc
->zc_value
) - 1] = '\0';
3763 if (dataset_namecheck(zc
->zc_name
, NULL
, NULL
) != 0 ||
3764 dataset_namecheck(zc
->zc_value
, NULL
, NULL
) != 0 ||
3765 strchr(zc
->zc_name
, '%') || strchr(zc
->zc_value
, '%'))
3766 return (SET_ERROR(EINVAL
));
3768 err
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
3771 ost
= dmu_objset_type(os
);
3772 dmu_objset_rele(os
, FTAG
);
3774 at
= strchr(zc
->zc_name
, '@');
3776 /* snaps must be in same fs */
3779 if (strncmp(zc
->zc_name
, zc
->zc_value
, at
- zc
->zc_name
+ 1))
3780 return (SET_ERROR(EXDEV
));
3782 if (ost
== DMU_OST_ZFS
) {
3783 error
= dmu_objset_find(zc
->zc_name
,
3784 recursive_unmount
, at
+ 1,
3785 recursive
? DS_FIND_CHILDREN
: 0);
3791 error
= dsl_dataset_rename_snapshot(zc
->zc_name
,
3792 at
+ 1, strchr(zc
->zc_value
, '@') + 1, recursive
);
3797 if (ost
== DMU_OST_ZVOL
)
3798 (void) zvol_remove_minor(zc
->zc_name
);
3799 return (dsl_dir_rename(zc
->zc_name
, zc
->zc_value
));
3804 zfs_check_settable(const char *dsname
, nvpair_t
*pair
, cred_t
*cr
)
3806 const char *propname
= nvpair_name(pair
);
3807 boolean_t issnap
= (strchr(dsname
, '@') != NULL
);
3808 zfs_prop_t prop
= zfs_name_to_prop(propname
);
3812 if (prop
== ZPROP_INVAL
) {
3813 if (zfs_prop_user(propname
)) {
3814 if (err
= zfs_secpolicy_write_perms(dsname
,
3815 ZFS_DELEG_PERM_USERPROP
, cr
))
3820 if (!issnap
&& zfs_prop_userquota(propname
)) {
3821 const char *perm
= NULL
;
3822 const char *uq_prefix
=
3823 zfs_userquota_prop_prefixes
[ZFS_PROP_USERQUOTA
];
3824 const char *gq_prefix
=
3825 zfs_userquota_prop_prefixes
[ZFS_PROP_GROUPQUOTA
];
3827 if (strncmp(propname
, uq_prefix
,
3828 strlen(uq_prefix
)) == 0) {
3829 perm
= ZFS_DELEG_PERM_USERQUOTA
;
3830 } else if (strncmp(propname
, gq_prefix
,
3831 strlen(gq_prefix
)) == 0) {
3832 perm
= ZFS_DELEG_PERM_GROUPQUOTA
;
3834 /* USERUSED and GROUPUSED are read-only */
3835 return (SET_ERROR(EINVAL
));
3838 if (err
= zfs_secpolicy_write_perms(dsname
, perm
, cr
))
3843 return (SET_ERROR(EINVAL
));
3847 return (SET_ERROR(EINVAL
));
3849 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
3851 * dsl_prop_get_all_impl() returns properties in this
3855 VERIFY(nvpair_value_nvlist(pair
, &attrs
) == 0);
3856 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
3861 * Check that this value is valid for this pool version
3864 case ZFS_PROP_COMPRESSION
:
3866 * If the user specified gzip compression, make sure
3867 * the SPA supports it. We ignore any errors here since
3868 * we'll catch them later.
3870 if (nvpair_value_uint64(pair
, &intval
) == 0) {
3871 if (intval
>= ZIO_COMPRESS_GZIP_1
&&
3872 intval
<= ZIO_COMPRESS_GZIP_9
&&
3873 zfs_earlier_version(dsname
,
3874 SPA_VERSION_GZIP_COMPRESSION
)) {
3875 return (SET_ERROR(ENOTSUP
));
3878 if (intval
== ZIO_COMPRESS_ZLE
&&
3879 zfs_earlier_version(dsname
,
3880 SPA_VERSION_ZLE_COMPRESSION
))
3881 return (SET_ERROR(ENOTSUP
));
3883 if (intval
== ZIO_COMPRESS_LZ4
) {
3886 if ((err
= spa_open(dsname
, &spa
, FTAG
)) != 0)
3889 if (!spa_feature_is_enabled(spa
,
3890 SPA_FEATURE_LZ4_COMPRESS
)) {
3891 spa_close(spa
, FTAG
);
3892 return (SET_ERROR(ENOTSUP
));
3894 spa_close(spa
, FTAG
);
3898 * If this is a bootable dataset then
3899 * verify that the compression algorithm
3900 * is supported for booting. We must return
3901 * something other than ENOTSUP since it
3902 * implies a downrev pool version.
3904 if (zfs_is_bootfs(dsname
) &&
3905 !BOOTFS_COMPRESS_VALID(intval
)) {
3906 return (SET_ERROR(ERANGE
));
3911 case ZFS_PROP_COPIES
:
3912 if (zfs_earlier_version(dsname
, SPA_VERSION_DITTO_BLOCKS
))
3913 return (SET_ERROR(ENOTSUP
));
3916 case ZFS_PROP_RECORDSIZE
:
3917 /* Record sizes above 128k need the feature to be enabled */
3918 if (nvpair_value_uint64(pair
, &intval
) == 0 &&
3919 intval
> SPA_OLD_MAXBLOCKSIZE
) {
3923 * We don't allow setting the property above 1MB,
3924 * unless the tunable has been changed.
3926 if (intval
> zfs_max_recordsize
||
3927 intval
> SPA_MAXBLOCKSIZE
)
3928 return (SET_ERROR(ERANGE
));
3930 if ((err
= spa_open(dsname
, &spa
, FTAG
)) != 0)
3933 if (!spa_feature_is_enabled(spa
,
3934 SPA_FEATURE_LARGE_BLOCKS
)) {
3935 spa_close(spa
, FTAG
);
3936 return (SET_ERROR(ENOTSUP
));
3938 spa_close(spa
, FTAG
);
3942 case ZFS_PROP_SHARESMB
:
3943 if (zpl_earlier_version(dsname
, ZPL_VERSION_FUID
))
3944 return (SET_ERROR(ENOTSUP
));
3947 case ZFS_PROP_ACLINHERIT
:
3948 if (nvpair_type(pair
) == DATA_TYPE_UINT64
&&
3949 nvpair_value_uint64(pair
, &intval
) == 0) {
3950 if (intval
== ZFS_ACL_PASSTHROUGH_X
&&
3951 zfs_earlier_version(dsname
,
3952 SPA_VERSION_PASSTHROUGH_X
))
3953 return (SET_ERROR(ENOTSUP
));
3957 case ZFS_PROP_CHECKSUM
:
3958 case ZFS_PROP_DEDUP
:
3960 spa_feature_t feature
;
3963 /* dedup feature version checks */
3964 if (prop
== ZFS_PROP_DEDUP
&&
3965 zfs_earlier_version(dsname
, SPA_VERSION_DEDUP
))
3966 return (SET_ERROR(ENOTSUP
));
3968 if (nvpair_value_uint64(pair
, &intval
) != 0)
3969 return (SET_ERROR(EINVAL
));
3971 /* check prop value is enabled in features */
3972 feature
= zio_checksum_to_feature(intval
& ZIO_CHECKSUM_MASK
);
3973 if (feature
== SPA_FEATURE_NONE
)
3976 if ((err
= spa_open(dsname
, &spa
, FTAG
)) != 0)
3979 if (!spa_feature_is_enabled(spa
, feature
)) {
3980 spa_close(spa
, FTAG
);
3981 return (SET_ERROR(ENOTSUP
));
3983 spa_close(spa
, FTAG
);
3988 return (zfs_secpolicy_setprop(dsname
, prop
, pair
, CRED()));
3992 * Checks for a race condition to make sure we don't increment a feature flag
3996 zfs_prop_activate_feature_check(void *arg
, dmu_tx_t
*tx
)
3998 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
3999 spa_feature_t
*featurep
= arg
;
4001 if (!spa_feature_is_active(spa
, *featurep
))
4004 return (SET_ERROR(EBUSY
));
4008 * The callback invoked on feature activation in the sync task caused by
4009 * zfs_prop_activate_feature.
4012 zfs_prop_activate_feature_sync(void *arg
, dmu_tx_t
*tx
)
4014 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
4015 spa_feature_t
*featurep
= arg
;
4017 spa_feature_incr(spa
, *featurep
, tx
);
4021 * Activates a feature on a pool in response to a property setting. This
4022 * creates a new sync task which modifies the pool to reflect the feature
4026 zfs_prop_activate_feature(spa_t
*spa
, spa_feature_t feature
)
4030 /* EBUSY here indicates that the feature is already active */
4031 err
= dsl_sync_task(spa_name(spa
),
4032 zfs_prop_activate_feature_check
, zfs_prop_activate_feature_sync
,
4033 &feature
, 2, ZFS_SPACE_CHECK_RESERVED
);
4035 if (err
!= 0 && err
!= EBUSY
)
4042 * Removes properties from the given props list that fail permission checks
4043 * needed to clear them and to restore them in case of a receive error. For each
4044 * property, make sure we have both set and inherit permissions.
4046 * Returns the first error encountered if any permission checks fail. If the
4047 * caller provides a non-NULL errlist, it also gives the complete list of names
4048 * of all the properties that failed a permission check along with the
4049 * corresponding error numbers. The caller is responsible for freeing the
4052 * If every property checks out successfully, zero is returned and the list
4053 * pointed at by errlist is NULL.
4056 zfs_check_clearable(char *dataset
, nvlist_t
*props
, nvlist_t
**errlist
)
4059 nvpair_t
*pair
, *next_pair
;
4066 VERIFY(nvlist_alloc(&errors
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4068 zc
= kmem_alloc(sizeof (zfs_cmd_t
), KM_SLEEP
);
4069 (void) strcpy(zc
->zc_name
, dataset
);
4070 pair
= nvlist_next_nvpair(props
, NULL
);
4071 while (pair
!= NULL
) {
4072 next_pair
= nvlist_next_nvpair(props
, pair
);
4074 (void) strcpy(zc
->zc_value
, nvpair_name(pair
));
4075 if ((err
= zfs_check_settable(dataset
, pair
, CRED())) != 0 ||
4076 (err
= zfs_secpolicy_inherit_prop(zc
, NULL
, CRED())) != 0) {
4077 VERIFY(nvlist_remove_nvpair(props
, pair
) == 0);
4078 VERIFY(nvlist_add_int32(errors
,
4079 zc
->zc_value
, err
) == 0);
4083 kmem_free(zc
, sizeof (zfs_cmd_t
));
4085 if ((pair
= nvlist_next_nvpair(errors
, NULL
)) == NULL
) {
4086 nvlist_free(errors
);
4089 VERIFY(nvpair_value_int32(pair
, &rv
) == 0);
4092 if (errlist
== NULL
)
4093 nvlist_free(errors
);
4101 propval_equals(nvpair_t
*p1
, nvpair_t
*p2
)
4103 if (nvpair_type(p1
) == DATA_TYPE_NVLIST
) {
4104 /* dsl_prop_get_all_impl() format */
4106 VERIFY(nvpair_value_nvlist(p1
, &attrs
) == 0);
4107 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
4111 if (nvpair_type(p2
) == DATA_TYPE_NVLIST
) {
4113 VERIFY(nvpair_value_nvlist(p2
, &attrs
) == 0);
4114 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
4118 if (nvpair_type(p1
) != nvpair_type(p2
))
4121 if (nvpair_type(p1
) == DATA_TYPE_STRING
) {
4122 char *valstr1
, *valstr2
;
4124 VERIFY(nvpair_value_string(p1
, (char **)&valstr1
) == 0);
4125 VERIFY(nvpair_value_string(p2
, (char **)&valstr2
) == 0);
4126 return (strcmp(valstr1
, valstr2
) == 0);
4128 uint64_t intval1
, intval2
;
4130 VERIFY(nvpair_value_uint64(p1
, &intval1
) == 0);
4131 VERIFY(nvpair_value_uint64(p2
, &intval2
) == 0);
4132 return (intval1
== intval2
);
4137 * Remove properties from props if they are not going to change (as determined
4138 * by comparison with origprops). Remove them from origprops as well, since we
4139 * do not need to clear or restore properties that won't change.
4142 props_reduce(nvlist_t
*props
, nvlist_t
*origprops
)
4144 nvpair_t
*pair
, *next_pair
;
4146 if (origprops
== NULL
)
4147 return; /* all props need to be received */
4149 pair
= nvlist_next_nvpair(props
, NULL
);
4150 while (pair
!= NULL
) {
4151 const char *propname
= nvpair_name(pair
);
4154 next_pair
= nvlist_next_nvpair(props
, pair
);
4156 if ((nvlist_lookup_nvpair(origprops
, propname
,
4157 &match
) != 0) || !propval_equals(pair
, match
))
4158 goto next
; /* need to set received value */
4160 /* don't clear the existing received value */
4161 (void) nvlist_remove_nvpair(origprops
, match
);
4162 /* don't bother receiving the property */
4163 (void) nvlist_remove_nvpair(props
, pair
);
4170 * Extract properties that cannot be set PRIOR to the receipt of a dataset.
4171 * For example, refquota cannot be set until after the receipt of a dataset,
4172 * because in replication streams, an older/earlier snapshot may exceed the
4173 * refquota. We want to receive the older/earlier snapshot, but setting
4174 * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent
4175 * the older/earlier snapshot from being received (with EDQUOT).
4177 * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario.
4179 * libzfs will need to be judicious handling errors encountered by props
4180 * extracted by this function.
4183 extract_delay_props(nvlist_t
*props
)
4185 nvlist_t
*delayprops
;
4186 nvpair_t
*nvp
, *tmp
;
4187 static const zfs_prop_t delayable
[] = { ZFS_PROP_REFQUOTA
, 0 };
4190 VERIFY(nvlist_alloc(&delayprops
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4192 for (nvp
= nvlist_next_nvpair(props
, NULL
); nvp
!= NULL
;
4193 nvp
= nvlist_next_nvpair(props
, nvp
)) {
4195 * strcmp() is safe because zfs_prop_to_name() always returns
4198 for (i
= 0; delayable
[i
] != 0; i
++) {
4199 if (strcmp(zfs_prop_to_name(delayable
[i
]),
4200 nvpair_name(nvp
)) == 0) {
4204 if (delayable
[i
] != 0) {
4205 tmp
= nvlist_prev_nvpair(props
, nvp
);
4206 VERIFY(nvlist_add_nvpair(delayprops
, nvp
) == 0);
4207 VERIFY(nvlist_remove_nvpair(props
, nvp
) == 0);
4212 if (nvlist_empty(delayprops
)) {
4213 nvlist_free(delayprops
);
4216 return (delayprops
);
4220 static boolean_t zfs_ioc_recv_inject_err
;
4225 * zc_name name of containing filesystem
4226 * zc_nvlist_src{_size} nvlist of properties to apply
4227 * zc_value name of snapshot to create
4228 * zc_string name of clone origin (if DRR_FLAG_CLONE)
4229 * zc_cookie file descriptor to recv from
4230 * zc_begin_record the BEGIN record of the stream (not byteswapped)
4231 * zc_guid force flag
4232 * zc_cleanup_fd cleanup-on-exit file descriptor
4233 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
4234 * zc_resumable if data is incomplete assume sender will resume
4237 * zc_cookie number of bytes read
4238 * zc_nvlist_dst{_size} error for each unapplied received property
4239 * zc_obj zprop_errflags_t
4240 * zc_action_handle handle for this guid/ds mapping
4243 zfs_ioc_recv(zfs_cmd_t
*zc
)
4246 dmu_recv_cookie_t drc
;
4247 boolean_t force
= (boolean_t
)zc
->zc_guid
;
4250 int props_error
= 0;
4253 nvlist_t
*props
= NULL
; /* sent properties */
4254 nvlist_t
*origprops
= NULL
; /* existing properties */
4255 nvlist_t
*delayprops
= NULL
; /* sent properties applied post-receive */
4256 char *origin
= NULL
;
4258 char tofs
[ZFS_MAX_DATASET_NAME_LEN
];
4259 boolean_t first_recvd_props
= B_FALSE
;
4261 if (dataset_namecheck(zc
->zc_value
, NULL
, NULL
) != 0 ||
4262 strchr(zc
->zc_value
, '@') == NULL
||
4263 strchr(zc
->zc_value
, '%'))
4264 return (SET_ERROR(EINVAL
));
4266 (void) strcpy(tofs
, zc
->zc_value
);
4267 tosnap
= strchr(tofs
, '@');
4270 if (zc
->zc_nvlist_src
!= (uintptr_t)NULL
&&
4271 (error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
4272 zc
->zc_iflags
, &props
)) != 0)
4279 return (SET_ERROR(EBADF
));
4282 errors
= fnvlist_alloc();
4284 if (zc
->zc_string
[0])
4285 origin
= zc
->zc_string
;
4287 error
= dmu_recv_begin(tofs
, tosnap
,
4288 &zc
->zc_begin_record
, force
, zc
->zc_resumable
, origin
, &drc
);
4293 * Set properties before we receive the stream so that they are applied
4294 * to the new data. Note that we must call dmu_recv_stream() if
4295 * dmu_recv_begin() succeeds.
4297 if (props
!= NULL
&& !drc
.drc_newfs
) {
4298 if (spa_version(dsl_dataset_get_spa(drc
.drc_ds
)) >=
4299 SPA_VERSION_RECVD_PROPS
&&
4300 !dsl_prop_get_hasrecvd(tofs
))
4301 first_recvd_props
= B_TRUE
;
4304 * If new received properties are supplied, they are to
4305 * completely replace the existing received properties, so stash
4306 * away the existing ones.
4308 if (dsl_prop_get_received(tofs
, &origprops
) == 0) {
4309 nvlist_t
*errlist
= NULL
;
4311 * Don't bother writing a property if its value won't
4312 * change (and avoid the unnecessary security checks).
4314 * The first receive after SPA_VERSION_RECVD_PROPS is a
4315 * special case where we blow away all local properties
4318 if (!first_recvd_props
)
4319 props_reduce(props
, origprops
);
4320 if (zfs_check_clearable(tofs
, origprops
, &errlist
) != 0)
4321 (void) nvlist_merge(errors
, errlist
, 0);
4322 nvlist_free(errlist
);
4324 if (clear_received_props(tofs
, origprops
,
4325 first_recvd_props
? NULL
: props
) != 0)
4326 zc
->zc_obj
|= ZPROP_ERR_NOCLEAR
;
4328 zc
->zc_obj
|= ZPROP_ERR_NOCLEAR
;
4332 if (props
!= NULL
) {
4333 props_error
= dsl_prop_set_hasrecvd(tofs
);
4335 if (props_error
== 0) {
4336 delayprops
= extract_delay_props(props
);
4337 (void) zfs_set_prop_nvlist(tofs
, ZPROP_SRC_RECEIVED
,
4343 error
= dmu_recv_stream(&drc
, fp
->f_vnode
, &off
, zc
->zc_cleanup_fd
,
4344 &zc
->zc_action_handle
);
4347 zfsvfs_t
*zfsvfs
= NULL
;
4349 if (getzfsvfs(tofs
, &zfsvfs
) == 0) {
4354 ds
= dmu_objset_ds(zfsvfs
->z_os
);
4355 error
= zfs_suspend_fs(zfsvfs
);
4357 * If the suspend fails, then the recv_end will
4358 * likely also fail, and clean up after itself.
4360 end_err
= dmu_recv_end(&drc
, zfsvfs
);
4362 error
= zfs_resume_fs(zfsvfs
, ds
);
4363 error
= error
? error
: end_err
;
4364 VFS_RELE(zfsvfs
->z_vfs
);
4366 error
= dmu_recv_end(&drc
, NULL
);
4369 /* Set delayed properties now, after we're done receiving. */
4370 if (delayprops
!= NULL
&& error
== 0) {
4371 (void) zfs_set_prop_nvlist(tofs
, ZPROP_SRC_RECEIVED
,
4372 delayprops
, errors
);
4376 if (delayprops
!= NULL
) {
4378 * Merge delayed props back in with initial props, in case
4379 * we're DEBUG and zfs_ioc_recv_inject_err is set (which means
4380 * we have to make sure clear_received_props() includes
4381 * the delayed properties).
4383 * Since zfs_ioc_recv_inject_err is only in DEBUG kernels,
4384 * using ASSERT() will be just like a VERIFY.
4386 ASSERT(nvlist_merge(props
, delayprops
, 0) == 0);
4387 nvlist_free(delayprops
);
4391 * Now that all props, initial and delayed, are set, report the prop
4392 * errors to the caller.
4394 if (zc
->zc_nvlist_dst_size
!= 0 &&
4395 (nvlist_smush(errors
, zc
->zc_nvlist_dst_size
) != 0 ||
4396 put_nvlist(zc
, errors
) != 0)) {
4398 * Caller made zc->zc_nvlist_dst less than the minimum expected
4399 * size or supplied an invalid address.
4401 props_error
= SET_ERROR(EINVAL
);
4404 zc
->zc_cookie
= off
- fp
->f_offset
;
4405 if (fop_seek(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
4409 if (zfs_ioc_recv_inject_err
) {
4410 zfs_ioc_recv_inject_err
= B_FALSE
;
4415 * On error, restore the original props.
4417 if (error
!= 0 && props
!= NULL
&& !drc
.drc_newfs
) {
4418 if (clear_received_props(tofs
, props
, NULL
) != 0) {
4420 * We failed to clear the received properties.
4421 * Since we may have left a $recvd value on the
4422 * system, we can't clear the $hasrecvd flag.
4424 zc
->zc_obj
|= ZPROP_ERR_NORESTORE
;
4425 } else if (first_recvd_props
) {
4426 dsl_prop_unset_hasrecvd(tofs
);
4429 if (origprops
== NULL
&& !drc
.drc_newfs
) {
4430 /* We failed to stash the original properties. */
4431 zc
->zc_obj
|= ZPROP_ERR_NORESTORE
;
4435 * dsl_props_set() will not convert RECEIVED to LOCAL on or
4436 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
4437 * explictly if we're restoring local properties cleared in the
4438 * first new-style receive.
4440 if (origprops
!= NULL
&&
4441 zfs_set_prop_nvlist(tofs
, (first_recvd_props
?
4442 ZPROP_SRC_LOCAL
: ZPROP_SRC_RECEIVED
),
4443 origprops
, NULL
) != 0) {
4445 * We stashed the original properties but failed to
4448 zc
->zc_obj
|= ZPROP_ERR_NORESTORE
;
4453 nvlist_free(origprops
);
4454 nvlist_free(errors
);
4458 error
= props_error
;
4465 * zc_name name of snapshot to send
4466 * zc_cookie file descriptor to send stream to
4467 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
4468 * zc_sendobj objsetid of snapshot to send
4469 * zc_fromobj objsetid of incremental fromsnap (may be zero)
4470 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
4471 * output size in zc_objset_type.
4472 * zc_flags lzc_send_flags
4475 * zc_objset_type estimated size, if zc_guid is set
4478 zfs_ioc_send(zfs_cmd_t
*zc
)
4482 boolean_t estimate
= (zc
->zc_guid
!= 0);
4483 boolean_t embedok
= (zc
->zc_flags
& 0x1);
4484 boolean_t large_block_ok
= (zc
->zc_flags
& 0x2);
4485 boolean_t compressok
= (zc
->zc_flags
& 0x4);
4487 if (zc
->zc_obj
!= 0) {
4489 dsl_dataset_t
*tosnap
;
4491 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
4495 error
= dsl_dataset_hold_obj(dp
, zc
->zc_sendobj
, FTAG
, &tosnap
);
4497 dsl_pool_rele(dp
, FTAG
);
4501 if (dsl_dir_is_clone(tosnap
->ds_dir
))
4503 dsl_dir_phys(tosnap
->ds_dir
)->dd_origin_obj
;
4504 dsl_dataset_rele(tosnap
, FTAG
);
4505 dsl_pool_rele(dp
, FTAG
);
4510 dsl_dataset_t
*tosnap
;
4511 dsl_dataset_t
*fromsnap
= NULL
;
4513 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
4517 error
= dsl_dataset_hold_obj(dp
, zc
->zc_sendobj
, FTAG
, &tosnap
);
4519 dsl_pool_rele(dp
, FTAG
);
4523 if (zc
->zc_fromobj
!= 0) {
4524 error
= dsl_dataset_hold_obj(dp
, zc
->zc_fromobj
,
4527 dsl_dataset_rele(tosnap
, FTAG
);
4528 dsl_pool_rele(dp
, FTAG
);
4533 error
= dmu_send_estimate(tosnap
, fromsnap
, compressok
,
4534 &zc
->zc_objset_type
);
4536 if (fromsnap
!= NULL
)
4537 dsl_dataset_rele(fromsnap
, FTAG
);
4538 dsl_dataset_rele(tosnap
, FTAG
);
4539 dsl_pool_rele(dp
, FTAG
);
4541 file_t
*fp
= getf(zc
->zc_cookie
);
4543 return (SET_ERROR(EBADF
));
4546 error
= dmu_send_obj(zc
->zc_name
, zc
->zc_sendobj
,
4547 zc
->zc_fromobj
, embedok
, large_block_ok
, compressok
,
4548 zc
->zc_cookie
, fp
->f_vnode
, &off
);
4550 if (fop_seek(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
4552 releasef(zc
->zc_cookie
);
4559 * zc_name name of snapshot on which to report progress
4560 * zc_cookie file descriptor of send stream
4563 * zc_cookie number of bytes written in send stream thus far
4566 zfs_ioc_send_progress(zfs_cmd_t
*zc
)
4570 dmu_sendarg_t
*dsp
= NULL
;
4573 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
4577 error
= dsl_dataset_hold(dp
, zc
->zc_name
, FTAG
, &ds
);
4579 dsl_pool_rele(dp
, FTAG
);
4583 mutex_enter(&ds
->ds_sendstream_lock
);
4586 * Iterate over all the send streams currently active on this dataset.
4587 * If there's one which matches the specified file descriptor _and_ the
4588 * stream was started by the current process, return the progress of
4591 for (dsp
= list_head(&ds
->ds_sendstreams
); dsp
!= NULL
;
4592 dsp
= list_next(&ds
->ds_sendstreams
, dsp
)) {
4593 if (dsp
->dsa_outfd
== zc
->zc_cookie
&&
4594 dsp
->dsa_proc
== curproc
)
4599 zc
->zc_cookie
= *(dsp
->dsa_off
);
4601 error
= SET_ERROR(ENOENT
);
4603 mutex_exit(&ds
->ds_sendstream_lock
);
4604 dsl_dataset_rele(ds
, FTAG
);
4605 dsl_pool_rele(dp
, FTAG
);
4610 zfs_ioc_inject_fault(zfs_cmd_t
*zc
)
4614 error
= zio_inject_fault(zc
->zc_name
, (int)zc
->zc_guid
, &id
,
4615 &zc
->zc_inject_record
);
4618 zc
->zc_guid
= (uint64_t)id
;
4624 zfs_ioc_clear_fault(zfs_cmd_t
*zc
)
4626 return (zio_clear_fault((int)zc
->zc_guid
));
4630 zfs_ioc_inject_list_next(zfs_cmd_t
*zc
)
4632 int id
= (int)zc
->zc_guid
;
4635 error
= zio_inject_list_next(&id
, zc
->zc_name
, sizeof (zc
->zc_name
),
4636 &zc
->zc_inject_record
);
4644 zfs_ioc_error_log(zfs_cmd_t
*zc
)
4648 size_t count
= (size_t)zc
->zc_nvlist_dst_size
;
4650 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
4653 error
= spa_get_errlog(spa
, (void *)(uintptr_t)zc
->zc_nvlist_dst
,
4656 zc
->zc_nvlist_dst_size
= count
;
4658 zc
->zc_nvlist_dst_size
= spa_get_errlog_size(spa
);
4660 spa_close(spa
, FTAG
);
4666 zfs_ioc_clear(zfs_cmd_t
*zc
)
4673 * On zpool clear we also fix up missing slogs
4675 mutex_enter(&spa_namespace_lock
);
4676 spa
= spa_lookup(zc
->zc_name
);
4678 mutex_exit(&spa_namespace_lock
);
4679 return (SET_ERROR(EIO
));
4681 if (spa_get_log_state(spa
) == SPA_LOG_MISSING
) {
4682 /* we need to let spa_open/spa_load clear the chains */
4683 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
4685 spa
->spa_last_open_failed
= 0;
4686 mutex_exit(&spa_namespace_lock
);
4688 if (zc
->zc_cookie
& ZPOOL_NO_REWIND
) {
4689 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
4692 nvlist_t
*config
= NULL
;
4694 if (zc
->zc_nvlist_src
== (uintptr_t)NULL
)
4695 return (SET_ERROR(EINVAL
));
4697 if ((error
= get_nvlist(zc
->zc_nvlist_src
,
4698 zc
->zc_nvlist_src_size
, zc
->zc_iflags
, &policy
)) == 0) {
4699 error
= spa_open_rewind(zc
->zc_name
, &spa
, FTAG
,
4701 if (config
!= NULL
) {
4704 if ((err
= put_nvlist(zc
, config
)) != 0)
4706 nvlist_free(config
);
4708 nvlist_free(policy
);
4715 spa_vdev_state_enter(spa
, SCL_NONE
);
4717 if (zc
->zc_guid
== 0) {
4720 vd
= spa_lookup_by_guid(spa
, zc
->zc_guid
, B_TRUE
);
4722 (void) spa_vdev_state_exit(spa
, NULL
, ENODEV
);
4723 spa_close(spa
, FTAG
);
4724 return (SET_ERROR(ENODEV
));
4728 vdev_clear(spa
, vd
);
4730 (void) spa_vdev_state_exit(spa
, NULL
, 0);
4733 * Resume any suspended I/Os.
4735 if (zio_resume(spa
) != 0)
4736 error
= SET_ERROR(EIO
);
4738 spa_close(spa
, FTAG
);
4744 zfs_ioc_pool_reopen(zfs_cmd_t
*zc
)
4749 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
4753 spa_vdev_state_enter(spa
, SCL_NONE
);
4756 * If a resilver is already in progress then set the
4757 * spa_scrub_reopen flag to B_TRUE so that we don't restart
4758 * the scan as a side effect of the reopen. Otherwise, let
4759 * vdev_open() decided if a resilver is required.
4761 spa
->spa_scrub_reopen
= dsl_scan_resilvering(spa
->spa_dsl_pool
);
4762 vdev_reopen(spa
->spa_root_vdev
);
4763 spa
->spa_scrub_reopen
= B_FALSE
;
4765 (void) spa_vdev_state_exit(spa
, NULL
, 0);
4766 spa_close(spa
, FTAG
);
4771 * zc_name name of filesystem
4774 * zc_string name of conflicting snapshot, if there is one
4777 zfs_ioc_promote(zfs_cmd_t
*zc
)
4780 dsl_dataset_t
*ds
, *ods
;
4781 char origin
[ZFS_MAX_DATASET_NAME_LEN
];
4785 zc
->zc_name
[sizeof (zc
->zc_name
) - 1] = '\0';
4786 if (dataset_namecheck(zc
->zc_name
, NULL
, NULL
) != 0 ||
4787 strchr(zc
->zc_name
, '%'))
4788 return (SET_ERROR(EINVAL
));
4790 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
4794 error
= dsl_dataset_hold(dp
, zc
->zc_name
, FTAG
, &ds
);
4796 dsl_pool_rele(dp
, FTAG
);
4800 if (!dsl_dir_is_clone(ds
->ds_dir
)) {
4801 dsl_dataset_rele(ds
, FTAG
);
4802 dsl_pool_rele(dp
, FTAG
);
4803 return (SET_ERROR(EINVAL
));
4806 error
= dsl_dataset_hold_obj(dp
,
4807 dsl_dir_phys(ds
->ds_dir
)->dd_origin_obj
, FTAG
, &ods
);
4809 dsl_dataset_rele(ds
, FTAG
);
4810 dsl_pool_rele(dp
, FTAG
);
4814 dsl_dataset_name(ods
, origin
);
4815 dsl_dataset_rele(ods
, FTAG
);
4816 dsl_dataset_rele(ds
, FTAG
);
4817 dsl_pool_rele(dp
, FTAG
);
4820 * We don't need to unmount *all* the origin fs's snapshots, but
4823 cp
= strchr(origin
, '@');
4826 (void) dmu_objset_find(origin
,
4827 zfs_unmount_snap_cb
, NULL
, DS_FIND_SNAPSHOTS
);
4828 return (dsl_dataset_promote(zc
->zc_name
, zc
->zc_string
));
4832 * Retrieve a single {user|group}{used|quota}@... property.
4835 * zc_name name of filesystem
4836 * zc_objset_type zfs_userquota_prop_t
4837 * zc_value domain name (eg. "S-1-234-567-89")
4838 * zc_guid RID/UID/GID
4841 * zc_cookie property value
4844 zfs_ioc_userspace_one(zfs_cmd_t
*zc
)
4849 if (zc
->zc_objset_type
>= ZFS_NUM_USERQUOTA_PROPS
)
4850 return (SET_ERROR(EINVAL
));
4852 error
= zfsvfs_hold(zc
->zc_name
, FTAG
, &zfsvfs
, B_FALSE
);
4856 error
= zfs_userspace_one(zfsvfs
,
4857 zc
->zc_objset_type
, zc
->zc_value
, zc
->zc_guid
, &zc
->zc_cookie
);
4858 zfsvfs_rele(zfsvfs
, FTAG
);
4865 * zc_name name of filesystem
4866 * zc_cookie zap cursor
4867 * zc_objset_type zfs_userquota_prop_t
4868 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
4871 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
4872 * zc_cookie zap cursor
4875 zfs_ioc_userspace_many(zfs_cmd_t
*zc
)
4878 int bufsize
= zc
->zc_nvlist_dst_size
;
4881 return (SET_ERROR(ENOMEM
));
4883 int error
= zfsvfs_hold(zc
->zc_name
, FTAG
, &zfsvfs
, B_FALSE
);
4887 void *buf
= kmem_alloc(bufsize
, KM_SLEEP
);
4889 error
= zfs_userspace_many(zfsvfs
, zc
->zc_objset_type
, &zc
->zc_cookie
,
4890 buf
, &zc
->zc_nvlist_dst_size
);
4893 error
= xcopyout(buf
,
4894 (void *)(uintptr_t)zc
->zc_nvlist_dst
,
4895 zc
->zc_nvlist_dst_size
);
4897 kmem_free(buf
, bufsize
);
4898 zfsvfs_rele(zfsvfs
, FTAG
);
4905 * zc_name name of filesystem
4911 zfs_ioc_userspace_upgrade(zfs_cmd_t
*zc
)
4917 if (getzfsvfs(zc
->zc_name
, &zfsvfs
) == 0) {
4918 if (!dmu_objset_userused_enabled(zfsvfs
->z_os
)) {
4920 * If userused is not enabled, it may be because the
4921 * objset needs to be closed & reopened (to grow the
4922 * objset_phys_t). Suspend/resume the fs will do that.
4924 dsl_dataset_t
*ds
, *newds
;
4926 ds
= dmu_objset_ds(zfsvfs
->z_os
);
4927 error
= zfs_suspend_fs(zfsvfs
);
4929 dmu_objset_refresh_ownership(ds
, &newds
,
4931 error
= zfs_resume_fs(zfsvfs
, newds
);
4935 error
= dmu_objset_userspace_upgrade(zfsvfs
->z_os
);
4936 VFS_RELE(zfsvfs
->z_vfs
);
4938 /* XXX kind of reading contents without owning */
4939 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
4943 error
= dmu_objset_userspace_upgrade(os
);
4944 dmu_objset_rele(os
, FTAG
);
4951 * We don't want to have a hard dependency
4952 * against some special symbols in sharefs
4953 * nfs, and smbsrv. Determine them if needed when
4954 * the first file system is shared.
4955 * Neither sharefs, nfs or smbsrv are unloadable modules.
4957 int (*znfsexport_fs
)(void *arg
);
4958 int (*zshare_fs
)(enum sharefs_sys_op
, share_t
*, uint32_t);
4959 int (*zsmbexport_fs
)(void *arg
, boolean_t add_share
);
4961 int zfs_nfsshare_inited
;
4962 int zfs_smbshare_inited
;
4964 ddi_modhandle_t nfs_mod
;
4965 ddi_modhandle_t sharefs_mod
;
4966 ddi_modhandle_t smbsrv_mod
;
4967 kmutex_t zfs_share_lock
;
4974 ASSERT(MUTEX_HELD(&zfs_share_lock
));
4975 /* Both NFS and SMB shares also require sharetab support. */
4976 if (sharefs_mod
== NULL
&& ((sharefs_mod
=
4977 ddi_modopen("fs/sharefs",
4978 KRTLD_MODE_FIRST
, &error
)) == NULL
)) {
4979 return (SET_ERROR(ENOSYS
));
4981 if (zshare_fs
== NULL
&& ((zshare_fs
=
4982 (int (*)(enum sharefs_sys_op
, share_t
*, uint32_t))
4983 ddi_modsym(sharefs_mod
, "sharefs_impl", &error
)) == NULL
)) {
4984 return (SET_ERROR(ENOSYS
));
4990 zfs_ioc_share(zfs_cmd_t
*zc
)
4995 switch (zc
->zc_share
.z_sharetype
) {
4997 case ZFS_UNSHARE_NFS
:
4998 if (zfs_nfsshare_inited
== 0) {
4999 mutex_enter(&zfs_share_lock
);
5000 if (nfs_mod
== NULL
&& ((nfs_mod
= ddi_modopen("fs/nfs",
5001 KRTLD_MODE_FIRST
, &error
)) == NULL
)) {
5002 mutex_exit(&zfs_share_lock
);
5003 return (SET_ERROR(ENOSYS
));
5005 if (znfsexport_fs
== NULL
&&
5006 ((znfsexport_fs
= (int (*)(void *))
5008 "nfs_export", &error
)) == NULL
)) {
5009 mutex_exit(&zfs_share_lock
);
5010 return (SET_ERROR(ENOSYS
));
5012 error
= zfs_init_sharefs();
5014 mutex_exit(&zfs_share_lock
);
5015 return (SET_ERROR(ENOSYS
));
5017 zfs_nfsshare_inited
= 1;
5018 mutex_exit(&zfs_share_lock
);
5022 case ZFS_UNSHARE_SMB
:
5023 if (zfs_smbshare_inited
== 0) {
5024 mutex_enter(&zfs_share_lock
);
5025 if (smbsrv_mod
== NULL
&& ((smbsrv_mod
=
5026 ddi_modopen("drv/smbsrv",
5027 KRTLD_MODE_FIRST
, &error
)) == NULL
)) {
5028 mutex_exit(&zfs_share_lock
);
5029 return (SET_ERROR(ENOSYS
));
5031 if (zsmbexport_fs
== NULL
&& ((zsmbexport_fs
=
5032 (int (*)(void *, boolean_t
))ddi_modsym(smbsrv_mod
,
5033 "smb_server_share", &error
)) == NULL
)) {
5034 mutex_exit(&zfs_share_lock
);
5035 return (SET_ERROR(ENOSYS
));
5037 error
= zfs_init_sharefs();
5039 mutex_exit(&zfs_share_lock
);
5040 return (SET_ERROR(ENOSYS
));
5042 zfs_smbshare_inited
= 1;
5043 mutex_exit(&zfs_share_lock
);
5047 return (SET_ERROR(EINVAL
));
5050 switch (zc
->zc_share
.z_sharetype
) {
5052 case ZFS_UNSHARE_NFS
:
5054 znfsexport_fs((void *)
5055 (uintptr_t)zc
->zc_share
.z_exportdata
))
5059 case ZFS_UNSHARE_SMB
:
5060 if (error
= zsmbexport_fs((void *)
5061 (uintptr_t)zc
->zc_share
.z_exportdata
,
5062 zc
->zc_share
.z_sharetype
== ZFS_SHARE_SMB
?
5069 opcode
= (zc
->zc_share
.z_sharetype
== ZFS_SHARE_NFS
||
5070 zc
->zc_share
.z_sharetype
== ZFS_SHARE_SMB
) ?
5071 SHAREFS_ADD
: SHAREFS_REMOVE
;
5074 * Add or remove share from sharetab
5076 error
= zshare_fs(opcode
,
5077 (void *)(uintptr_t)zc
->zc_share
.z_sharedata
,
5078 zc
->zc_share
.z_sharemax
);
5084 ace_t full_access
[] = {
5085 {(uid_t
)-1, ACE_ALL_PERMS
, ACE_EVERYONE
, 0}
5090 * zc_name name of containing filesystem
5091 * zc_obj object # beyond which we want next in-use object #
5094 * zc_obj next in-use object #
5097 zfs_ioc_next_obj(zfs_cmd_t
*zc
)
5099 objset_t
*os
= NULL
;
5102 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
5106 error
= dmu_object_next(os
, &zc
->zc_obj
, B_FALSE
,
5107 dsl_dataset_phys(os
->os_dsl_dataset
)->ds_prev_snap_txg
);
5109 dmu_objset_rele(os
, FTAG
);
5115 * zc_name name of filesystem
5116 * zc_value prefix name for snapshot
5117 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
5120 * zc_value short name of new snapshot
5123 zfs_ioc_tmp_snapshot(zfs_cmd_t
*zc
)
5130 error
= zfs_onexit_fd_hold(zc
->zc_cleanup_fd
, &minor
);
5134 snap_name
= kmem_asprintf("%s-%016llx", zc
->zc_value
,
5135 (u_longlong_t
)ddi_get_lbolt64());
5136 hold_name
= kmem_asprintf("%%%s", zc
->zc_value
);
5138 error
= dsl_dataset_snapshot_tmp(zc
->zc_name
, snap_name
, minor
,
5141 (void) strcpy(zc
->zc_value
, snap_name
);
5144 zfs_onexit_fd_rele(zc
->zc_cleanup_fd
);
5150 * zc_name name of "to" snapshot
5151 * zc_value name of "from" snapshot
5152 * zc_cookie file descriptor to write diff data on
5155 * dmu_diff_record_t's to the file descriptor
5158 zfs_ioc_diff(zfs_cmd_t
*zc
)
5164 fp
= getf(zc
->zc_cookie
);
5166 return (SET_ERROR(EBADF
));
5170 error
= dmu_diff(zc
->zc_name
, zc
->zc_value
, fp
->f_vnode
, &off
);
5172 if (fop_seek(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
5174 releasef(zc
->zc_cookie
);
5180 * Remove all ACL files in shares dir
5183 zfs_smb_acl_purge(znode_t
*dzp
)
5186 zap_attribute_t zap
;
5187 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
5190 for (zap_cursor_init(&zc
, zfsvfs
->z_os
, dzp
->z_id
);
5191 (error
= zap_cursor_retrieve(&zc
, &zap
)) == 0;
5192 zap_cursor_advance(&zc
)) {
5193 if ((error
= fop_remove(ZTOV(dzp
), zap
.za_name
, kcred
,
5197 zap_cursor_fini(&zc
);
5202 zfs_ioc_smb_acl(zfs_cmd_t
*zc
)
5206 vnode_t
*resourcevp
= NULL
;
5215 if ((error
= lookupname(zc
->zc_value
, UIO_SYSSPACE
,
5216 NO_FOLLOW
, NULL
, &vp
)) != 0)
5219 /* Now make sure mntpnt and dataset are ZFS */
5221 if (vp
->v_vfsp
->vfs_fstype
!= zfsfstype
||
5222 (strcmp((char *)refstr_value(vp
->v_vfsp
->vfs_resource
),
5223 zc
->zc_name
) != 0)) {
5225 return (SET_ERROR(EINVAL
));
5229 zfsvfs
= dzp
->z_zfsvfs
;
5233 * Create share dir if its missing.
5235 mutex_enter(&zfsvfs
->z_lock
);
5236 if (zfsvfs
->z_shares_dir
== 0) {
5239 tx
= dmu_tx_create(zfsvfs
->z_os
);
5240 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
, TRUE
,
5242 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, FALSE
, NULL
);
5243 error
= dmu_tx_assign(tx
, TXG_WAIT
);
5247 error
= zfs_create_share_dir(zfsvfs
, tx
);
5251 mutex_exit(&zfsvfs
->z_lock
);
5257 mutex_exit(&zfsvfs
->z_lock
);
5259 ASSERT(zfsvfs
->z_shares_dir
);
5260 if ((error
= zfs_zget(zfsvfs
, zfsvfs
->z_shares_dir
, &sharedir
)) != 0) {
5266 switch (zc
->zc_cookie
) {
5267 case ZFS_SMB_ACL_ADD
:
5268 vattr
.va_mask
= VATTR_MODE
|VATTR_UID
|VATTR_GID
|VATTR_TYPE
;
5269 vattr
.va_type
= VREG
;
5270 vattr
.va_mode
= S_IFREG
|0777;
5274 vsec
.vsa_mask
= VSA_ACE
;
5275 vsec
.vsa_aclentp
= &full_access
;
5276 vsec
.vsa_aclentsz
= sizeof (full_access
);
5277 vsec
.vsa_aclcnt
= 1;
5279 error
= fop_create(ZTOV(sharedir
), zc
->zc_string
,
5280 &vattr
, EXCL
, 0, &resourcevp
, kcred
, 0, NULL
, &vsec
);
5282 VN_RELE(resourcevp
);
5285 case ZFS_SMB_ACL_REMOVE
:
5286 error
= fop_remove(ZTOV(sharedir
), zc
->zc_string
, kcred
,
5290 case ZFS_SMB_ACL_RENAME
:
5291 if ((error
= get_nvlist(zc
->zc_nvlist_src
,
5292 zc
->zc_nvlist_src_size
, zc
->zc_iflags
, &nvlist
)) != 0) {
5294 VN_RELE(ZTOV(sharedir
));
5298 if (nvlist_lookup_string(nvlist
, ZFS_SMB_ACL_SRC
, &src
) ||
5299 nvlist_lookup_string(nvlist
, ZFS_SMB_ACL_TARGET
,
5302 VN_RELE(ZTOV(sharedir
));
5304 nvlist_free(nvlist
);
5307 error
= fop_rename(ZTOV(sharedir
), src
, ZTOV(sharedir
), target
,
5309 nvlist_free(nvlist
);
5312 case ZFS_SMB_ACL_PURGE
:
5313 error
= zfs_smb_acl_purge(sharedir
);
5317 error
= SET_ERROR(EINVAL
);
5322 VN_RELE(ZTOV(sharedir
));
5331 * "holds" -> { snapname -> holdname (string), ... }
5332 * (optional) "cleanup_fd" -> fd (int32)
5336 * snapname -> error value (int32)
5342 zfs_ioc_hold(const char *pool
, nvlist_t
*args
, nvlist_t
*errlist
)
5346 int cleanup_fd
= -1;
5350 error
= nvlist_lookup_nvlist(args
, "holds", &holds
);
5352 return (SET_ERROR(EINVAL
));
5354 /* make sure the user didn't pass us any invalid (empty) tags */
5355 for (pair
= nvlist_next_nvpair(holds
, NULL
); pair
!= NULL
;
5356 pair
= nvlist_next_nvpair(holds
, pair
)) {
5359 error
= nvpair_value_string(pair
, &htag
);
5361 return (SET_ERROR(error
));
5363 if (strlen(htag
) == 0)
5364 return (SET_ERROR(EINVAL
));
5367 if (nvlist_lookup_int32(args
, "cleanup_fd", &cleanup_fd
) == 0) {
5368 error
= zfs_onexit_fd_hold(cleanup_fd
, &minor
);
5373 error
= dsl_dataset_user_hold(holds
, minor
, errlist
);
5375 zfs_onexit_fd_rele(cleanup_fd
);
5380 * innvl is not used.
5383 * holdname -> time added (uint64 seconds since epoch)
5389 zfs_ioc_get_holds(const char *snapname
, nvlist_t
*args
, nvlist_t
*outnvl
)
5391 return (dsl_dataset_get_holds(snapname
, outnvl
));
5396 * snapname -> { holdname, ... }
5401 * snapname -> error value (int32)
5407 zfs_ioc_release(const char *pool
, nvlist_t
*holds
, nvlist_t
*errlist
)
5409 return (dsl_dataset_user_release(holds
, errlist
));
5414 * zc_name name of new filesystem or snapshot
5415 * zc_value full name of old snapshot
5418 * zc_cookie space in bytes
5419 * zc_objset_type compressed space in bytes
5420 * zc_perm_action uncompressed space in bytes
5423 zfs_ioc_space_written(zfs_cmd_t
*zc
)
5427 dsl_dataset_t
*new, *old
;
5429 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
5432 error
= dsl_dataset_hold(dp
, zc
->zc_name
, FTAG
, &new);
5434 dsl_pool_rele(dp
, FTAG
);
5437 error
= dsl_dataset_hold(dp
, zc
->zc_value
, FTAG
, &old
);
5439 dsl_dataset_rele(new, FTAG
);
5440 dsl_pool_rele(dp
, FTAG
);
5444 error
= dsl_dataset_space_written(old
, new, &zc
->zc_cookie
,
5445 &zc
->zc_objset_type
, &zc
->zc_perm_action
);
5446 dsl_dataset_rele(old
, FTAG
);
5447 dsl_dataset_rele(new, FTAG
);
5448 dsl_pool_rele(dp
, FTAG
);
5454 * "firstsnap" -> snapshot name
5458 * "used" -> space in bytes
5459 * "compressed" -> compressed space in bytes
5460 * "uncompressed" -> uncompressed space in bytes
5464 zfs_ioc_space_snaps(const char *lastsnap
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
5468 dsl_dataset_t
*new, *old
;
5470 uint64_t used
, comp
, uncomp
;
5472 if (nvlist_lookup_string(innvl
, "firstsnap", &firstsnap
) != 0)
5473 return (SET_ERROR(EINVAL
));
5475 error
= dsl_pool_hold(lastsnap
, FTAG
, &dp
);
5479 error
= dsl_dataset_hold(dp
, lastsnap
, FTAG
, &new);
5480 if (error
== 0 && !new->ds_is_snapshot
) {
5481 dsl_dataset_rele(new, FTAG
);
5482 error
= SET_ERROR(EINVAL
);
5485 dsl_pool_rele(dp
, FTAG
);
5488 error
= dsl_dataset_hold(dp
, firstsnap
, FTAG
, &old
);
5489 if (error
== 0 && !old
->ds_is_snapshot
) {
5490 dsl_dataset_rele(old
, FTAG
);
5491 error
= SET_ERROR(EINVAL
);
5494 dsl_dataset_rele(new, FTAG
);
5495 dsl_pool_rele(dp
, FTAG
);
5499 error
= dsl_dataset_space_wouldfree(old
, new, &used
, &comp
, &uncomp
);
5500 dsl_dataset_rele(old
, FTAG
);
5501 dsl_dataset_rele(new, FTAG
);
5502 dsl_pool_rele(dp
, FTAG
);
5503 fnvlist_add_uint64(outnvl
, "used", used
);
5504 fnvlist_add_uint64(outnvl
, "compressed", comp
);
5505 fnvlist_add_uint64(outnvl
, "uncompressed", uncomp
);
5511 * "fd" -> file descriptor to write stream to (int32)
5512 * (optional) "fromsnap" -> full snap name to send an incremental from
5513 * (optional) "largeblockok" -> (value ignored)
5514 * indicates that blocks > 128KB are permitted
5515 * (optional) "embedok" -> (value ignored)
5516 * presence indicates DRR_WRITE_EMBEDDED records are permitted
5517 * (optional) "compressok" -> (value ignored)
5518 * presence indicates compressed DRR_WRITE records are permitted
5519 * (optional) "resume_object" and "resume_offset" -> (uint64)
5520 * if present, resume send stream from specified object and offset.
5527 zfs_ioc_send_new(const char *snapname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
5531 char *fromname
= NULL
;
5533 boolean_t largeblockok
;
5535 boolean_t compressok
;
5536 uint64_t resumeobj
= 0;
5537 uint64_t resumeoff
= 0;
5539 error
= nvlist_lookup_int32(innvl
, "fd", &fd
);
5541 return (SET_ERROR(EINVAL
));
5543 (void) nvlist_lookup_string(innvl
, "fromsnap", &fromname
);
5545 largeblockok
= nvlist_exists(innvl
, "largeblockok");
5546 embedok
= nvlist_exists(innvl
, "embedok");
5547 compressok
= nvlist_exists(innvl
, "compressok");
5549 (void) nvlist_lookup_uint64(innvl
, "resume_object", &resumeobj
);
5550 (void) nvlist_lookup_uint64(innvl
, "resume_offset", &resumeoff
);
5552 file_t
*fp
= getf(fd
);
5554 return (SET_ERROR(EBADF
));
5557 error
= dmu_send(snapname
, fromname
, embedok
, largeblockok
, compressok
,
5558 fd
, resumeobj
, resumeoff
, fp
->f_vnode
, &off
);
5560 if (fop_seek(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
5567 * Determine approximately how large a zfs send stream will be -- the number
5568 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
5571 * (optional) "from" -> full snap or bookmark name to send an incremental
5573 * (optional) "largeblockok" -> (value ignored)
5574 * indicates that blocks > 128KB are permitted
5575 * (optional) "embedok" -> (value ignored)
5576 * presence indicates DRR_WRITE_EMBEDDED records are permitted
5577 * (optional) "compressok" -> (value ignored)
5578 * presence indicates compressed DRR_WRITE records are permitted
5582 * "space" -> bytes of space (uint64)
5586 zfs_ioc_send_space(const char *snapname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
5589 dsl_dataset_t
*tosnap
;
5592 boolean_t compressok
;
5595 error
= dsl_pool_hold(snapname
, FTAG
, &dp
);
5599 error
= dsl_dataset_hold(dp
, snapname
, FTAG
, &tosnap
);
5601 dsl_pool_rele(dp
, FTAG
);
5605 compressok
= nvlist_exists(innvl
, "compressok");
5607 error
= nvlist_lookup_string(innvl
, "from", &fromname
);
5609 if (strchr(fromname
, '@') != NULL
) {
5611 * If from is a snapshot, hold it and use the more
5612 * efficient dmu_send_estimate to estimate send space
5613 * size using deadlists.
5615 dsl_dataset_t
*fromsnap
;
5616 error
= dsl_dataset_hold(dp
, fromname
, FTAG
, &fromsnap
);
5619 error
= dmu_send_estimate(tosnap
, fromsnap
, compressok
,
5621 dsl_dataset_rele(fromsnap
, FTAG
);
5622 } else if (strchr(fromname
, '#') != NULL
) {
5624 * If from is a bookmark, fetch the creation TXG of the
5625 * snapshot it was created from and use that to find
5626 * blocks that were born after it.
5628 zfs_bookmark_phys_t frombm
;
5630 error
= dsl_bookmark_lookup(dp
, fromname
, tosnap
,
5634 error
= dmu_send_estimate_from_txg(tosnap
,
5635 frombm
.zbm_creation_txg
, compressok
, &space
);
5638 * from is not properly formatted as a snapshot or
5641 error
= SET_ERROR(EINVAL
);
5646 * If estimating the size of a full send, use dmu_send_estimate.
5648 error
= dmu_send_estimate(tosnap
, NULL
, compressok
, &space
);
5651 fnvlist_add_uint64(outnvl
, "space", space
);
5654 dsl_dataset_rele(tosnap
, FTAG
);
5655 dsl_pool_rele(dp
, FTAG
);
5659 static zfs_ioc_vec_t zfs_ioc_vec
[ZFS_IOC_LAST
- ZFS_IOC_FIRST
];
5662 zfs_ioctl_register_legacy(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5663 zfs_secpolicy_func_t
*secpolicy
, zfs_ioc_namecheck_t namecheck
,
5664 boolean_t log_history
, zfs_ioc_poolcheck_t pool_check
)
5666 zfs_ioc_vec_t
*vec
= &zfs_ioc_vec
[ioc
- ZFS_IOC_FIRST
];
5668 ASSERT3U(ioc
, >=, ZFS_IOC_FIRST
);
5669 ASSERT3U(ioc
, <, ZFS_IOC_LAST
);
5670 ASSERT3P(vec
->zvec_legacy_func
, ==, NULL
);
5671 ASSERT3P(vec
->zvec_func
, ==, NULL
);
5673 vec
->zvec_legacy_func
= func
;
5674 vec
->zvec_secpolicy
= secpolicy
;
5675 vec
->zvec_namecheck
= namecheck
;
5676 vec
->zvec_allow_log
= log_history
;
5677 vec
->zvec_pool_check
= pool_check
;
5681 * See the block comment at the beginning of this file for details on
5682 * each argument to this function.
5685 zfs_ioctl_register(const char *name
, zfs_ioc_t ioc
, zfs_ioc_func_t
*func
,
5686 zfs_secpolicy_func_t
*secpolicy
, zfs_ioc_namecheck_t namecheck
,
5687 zfs_ioc_poolcheck_t pool_check
, boolean_t smush_outnvlist
,
5688 boolean_t allow_log
)
5690 zfs_ioc_vec_t
*vec
= &zfs_ioc_vec
[ioc
- ZFS_IOC_FIRST
];
5692 ASSERT3U(ioc
, >=, ZFS_IOC_FIRST
);
5693 ASSERT3U(ioc
, <, ZFS_IOC_LAST
);
5694 ASSERT3P(vec
->zvec_legacy_func
, ==, NULL
);
5695 ASSERT3P(vec
->zvec_func
, ==, NULL
);
5697 /* if we are logging, the name must be valid */
5698 ASSERT(!allow_log
|| namecheck
!= NO_NAME
);
5700 vec
->zvec_name
= name
;
5701 vec
->zvec_func
= func
;
5702 vec
->zvec_secpolicy
= secpolicy
;
5703 vec
->zvec_namecheck
= namecheck
;
5704 vec
->zvec_pool_check
= pool_check
;
5705 vec
->zvec_smush_outnvlist
= smush_outnvlist
;
5706 vec
->zvec_allow_log
= allow_log
;
5710 zfs_ioctl_register_pool(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5711 zfs_secpolicy_func_t
*secpolicy
, boolean_t log_history
,
5712 zfs_ioc_poolcheck_t pool_check
)
5714 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5715 POOL_NAME
, log_history
, pool_check
);
5719 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5720 zfs_secpolicy_func_t
*secpolicy
, zfs_ioc_poolcheck_t pool_check
)
5722 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5723 DATASET_NAME
, B_FALSE
, pool_check
);
5727 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
)
5729 zfs_ioctl_register_legacy(ioc
, func
, zfs_secpolicy_config
,
5730 POOL_NAME
, B_TRUE
, POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
5734 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5735 zfs_secpolicy_func_t
*secpolicy
)
5737 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5738 NO_NAME
, B_FALSE
, POOL_CHECK_NONE
);
5742 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc
,
5743 zfs_ioc_legacy_func_t
*func
, zfs_secpolicy_func_t
*secpolicy
)
5745 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5746 DATASET_NAME
, B_FALSE
, POOL_CHECK_SUSPENDED
);
5750 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
)
5752 zfs_ioctl_register_dataset_read_secpolicy(ioc
, func
,
5753 zfs_secpolicy_read
);
5757 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5758 zfs_secpolicy_func_t
*secpolicy
)
5760 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5761 DATASET_NAME
, B_TRUE
, POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
5765 zfs_ioctl_init(void)
5767 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT
,
5768 zfs_ioc_snapshot
, zfs_secpolicy_snapshot
, POOL_NAME
,
5769 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5771 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY
,
5772 zfs_ioc_log_history
, zfs_secpolicy_log_history
, NO_NAME
,
5773 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_FALSE
, B_FALSE
);
5775 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS
,
5776 zfs_ioc_space_snaps
, zfs_secpolicy_read
, DATASET_NAME
,
5777 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
5779 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW
,
5780 zfs_ioc_send_new
, zfs_secpolicy_send_new
, DATASET_NAME
,
5781 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
5783 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE
,
5784 zfs_ioc_send_space
, zfs_secpolicy_read
, DATASET_NAME
,
5785 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
5787 zfs_ioctl_register("create", ZFS_IOC_CREATE
,
5788 zfs_ioc_create
, zfs_secpolicy_create_clone
, DATASET_NAME
,
5789 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5791 zfs_ioctl_register("clone", ZFS_IOC_CLONE
,
5792 zfs_ioc_clone
, zfs_secpolicy_create_clone
, DATASET_NAME
,
5793 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5795 zfs_ioctl_register("remap", ZFS_IOC_REMAP
,
5796 zfs_ioc_remap
, zfs_secpolicy_remap
, DATASET_NAME
,
5797 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_FALSE
, B_TRUE
);
5799 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS
,
5800 zfs_ioc_destroy_snaps
, zfs_secpolicy_destroy_snaps
, POOL_NAME
,
5801 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5803 zfs_ioctl_register("hold", ZFS_IOC_HOLD
,
5804 zfs_ioc_hold
, zfs_secpolicy_hold
, POOL_NAME
,
5805 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5806 zfs_ioctl_register("release", ZFS_IOC_RELEASE
,
5807 zfs_ioc_release
, zfs_secpolicy_release
, POOL_NAME
,
5808 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5810 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS
,
5811 zfs_ioc_get_holds
, zfs_secpolicy_read
, DATASET_NAME
,
5812 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
5814 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK
,
5815 zfs_ioc_rollback
, zfs_secpolicy_rollback
, DATASET_NAME
,
5816 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_FALSE
, B_TRUE
);
5818 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK
,
5819 zfs_ioc_bookmark
, zfs_secpolicy_bookmark
, POOL_NAME
,
5820 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5822 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS
,
5823 zfs_ioc_get_bookmarks
, zfs_secpolicy_read
, DATASET_NAME
,
5824 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
5826 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS
,
5827 zfs_ioc_destroy_bookmarks
, zfs_secpolicy_destroy_bookmarks
,
5829 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5831 zfs_ioctl_register("channel_program", ZFS_IOC_CHANNEL_PROGRAM
,
5832 zfs_ioc_channel_program
, zfs_secpolicy_config
,
5833 POOL_NAME
, POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
,
5836 zfs_ioctl_register("zpool_checkpoint", ZFS_IOC_POOL_CHECKPOINT
,
5837 zfs_ioc_pool_checkpoint
, zfs_secpolicy_config
, POOL_NAME
,
5838 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5840 zfs_ioctl_register("zpool_discard_checkpoint",
5841 ZFS_IOC_POOL_DISCARD_CHECKPOINT
, zfs_ioc_pool_discard_checkpoint
,
5842 zfs_secpolicy_config
, POOL_NAME
,
5843 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5845 zfs_ioctl_register("initialize", ZFS_IOC_POOL_INITIALIZE
,
5846 zfs_ioc_pool_initialize
, zfs_secpolicy_config
, POOL_NAME
,
5847 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5849 /* IOCTLS that use the legacy function signature */
5851 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE
, zfs_ioc_pool_freeze
,
5852 zfs_secpolicy_config
, NO_NAME
, B_FALSE
, POOL_CHECK_READONLY
);
5854 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE
, zfs_ioc_pool_create
,
5855 zfs_secpolicy_config
, B_TRUE
, POOL_CHECK_NONE
);
5856 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN
,
5858 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE
,
5859 zfs_ioc_pool_upgrade
);
5860 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD
,
5862 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE
,
5863 zfs_ioc_vdev_remove
);
5864 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE
,
5865 zfs_ioc_vdev_set_state
);
5866 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH
,
5867 zfs_ioc_vdev_attach
);
5868 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH
,
5869 zfs_ioc_vdev_detach
);
5870 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH
,
5871 zfs_ioc_vdev_setpath
);
5872 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU
,
5873 zfs_ioc_vdev_setfru
);
5874 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS
,
5875 zfs_ioc_pool_set_props
);
5876 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT
,
5877 zfs_ioc_vdev_split
);
5878 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID
,
5879 zfs_ioc_pool_reguid
);
5881 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS
,
5882 zfs_ioc_pool_configs
, zfs_secpolicy_none
);
5883 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT
,
5884 zfs_ioc_pool_tryimport
, zfs_secpolicy_config
);
5885 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT
,
5886 zfs_ioc_inject_fault
, zfs_secpolicy_inject
);
5887 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT
,
5888 zfs_ioc_clear_fault
, zfs_secpolicy_inject
);
5889 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT
,
5890 zfs_ioc_inject_list_next
, zfs_secpolicy_inject
);
5893 * pool destroy, and export don't log the history as part of
5894 * zfsdev_ioctl, but rather zfs_ioc_pool_export
5895 * does the logging of those commands.
5897 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY
, zfs_ioc_pool_destroy
,
5898 zfs_secpolicy_config
, B_FALSE
, POOL_CHECK_NONE
);
5899 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT
, zfs_ioc_pool_export
,
5900 zfs_secpolicy_config
, B_FALSE
, POOL_CHECK_NONE
);
5902 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS
, zfs_ioc_pool_stats
,
5903 zfs_secpolicy_read
, B_FALSE
, POOL_CHECK_NONE
);
5904 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS
, zfs_ioc_pool_get_props
,
5905 zfs_secpolicy_read
, B_FALSE
, POOL_CHECK_NONE
);
5907 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG
, zfs_ioc_error_log
,
5908 zfs_secpolicy_inject
, B_FALSE
, POOL_CHECK_SUSPENDED
);
5909 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME
,
5910 zfs_ioc_dsobj_to_dsname
,
5911 zfs_secpolicy_diff
, B_FALSE
, POOL_CHECK_SUSPENDED
);
5912 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY
,
5913 zfs_ioc_pool_get_history
,
5914 zfs_secpolicy_config
, B_FALSE
, POOL_CHECK_SUSPENDED
);
5916 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT
, zfs_ioc_pool_import
,
5917 zfs_secpolicy_config
, B_TRUE
, POOL_CHECK_NONE
);
5919 zfs_ioctl_register_pool(ZFS_IOC_CLEAR
, zfs_ioc_clear
,
5920 zfs_secpolicy_config
, B_TRUE
, POOL_CHECK_READONLY
);
5921 zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN
, zfs_ioc_pool_reopen
,
5922 zfs_secpolicy_config
, B_TRUE
, POOL_CHECK_SUSPENDED
);
5924 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN
,
5925 zfs_ioc_space_written
);
5926 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS
,
5927 zfs_ioc_objset_recvd_props
);
5928 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ
,
5930 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL
,
5932 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS
,
5933 zfs_ioc_objset_stats
);
5934 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS
,
5935 zfs_ioc_objset_zplprops
);
5936 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT
,
5937 zfs_ioc_dataset_list_next
);
5938 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT
,
5939 zfs_ioc_snapshot_list_next
);
5940 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS
,
5941 zfs_ioc_send_progress
);
5943 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF
,
5944 zfs_ioc_diff
, zfs_secpolicy_diff
);
5945 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS
,
5946 zfs_ioc_obj_to_stats
, zfs_secpolicy_diff
);
5947 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH
,
5948 zfs_ioc_obj_to_path
, zfs_secpolicy_diff
);
5949 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE
,
5950 zfs_ioc_userspace_one
, zfs_secpolicy_userspace_one
);
5951 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY
,
5952 zfs_ioc_userspace_many
, zfs_secpolicy_userspace_many
);
5953 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND
,
5954 zfs_ioc_send
, zfs_secpolicy_send
);
5956 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP
, zfs_ioc_set_prop
,
5957 zfs_secpolicy_none
);
5958 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY
, zfs_ioc_destroy
,
5959 zfs_secpolicy_destroy
);
5960 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME
, zfs_ioc_rename
,
5961 zfs_secpolicy_rename
);
5962 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV
, zfs_ioc_recv
,
5963 zfs_secpolicy_recv
);
5964 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE
, zfs_ioc_promote
,
5965 zfs_secpolicy_promote
);
5966 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP
,
5967 zfs_ioc_inherit_prop
, zfs_secpolicy_inherit_prop
);
5968 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL
, zfs_ioc_set_fsacl
,
5969 zfs_secpolicy_set_fsacl
);
5971 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE
, zfs_ioc_share
,
5972 zfs_secpolicy_share
, POOL_CHECK_NONE
);
5973 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL
, zfs_ioc_smb_acl
,
5974 zfs_secpolicy_smb_acl
, POOL_CHECK_NONE
);
5975 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE
,
5976 zfs_ioc_userspace_upgrade
, zfs_secpolicy_userspace_upgrade
,
5977 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
5978 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT
,
5979 zfs_ioc_tmp_snapshot
, zfs_secpolicy_tmp_snapshot
,
5980 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
5984 pool_status_check(const char *name
, zfs_ioc_namecheck_t type
,
5985 zfs_ioc_poolcheck_t check
)
5990 ASSERT(type
== POOL_NAME
|| type
== DATASET_NAME
);
5992 if (check
& POOL_CHECK_NONE
)
5995 error
= spa_open(name
, &spa
, FTAG
);
5997 if ((check
& POOL_CHECK_SUSPENDED
) && spa_suspended(spa
))
5998 error
= SET_ERROR(EAGAIN
);
5999 else if ((check
& POOL_CHECK_READONLY
) && !spa_writeable(spa
))
6000 error
= SET_ERROR(EROFS
);
6001 spa_close(spa
, FTAG
);
6007 * Find a free minor number.
6010 zfsdev_minor_alloc(void)
6012 static minor_t last_minor
;
6015 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
6017 for (m
= last_minor
+ 1; m
!= last_minor
; m
++) {
6018 if (m
> ZFSDEV_MAX_MINOR
)
6020 if (ddi_get_soft_state(zfsdev_state
, m
) == NULL
) {
6030 zfs_ctldev_init(dev_t
*devp
)
6033 zfs_soft_state_t
*zs
;
6035 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
6036 ASSERT(getminor(*devp
) == 0);
6038 minor
= zfsdev_minor_alloc();
6040 return (SET_ERROR(ENXIO
));
6042 if (ddi_soft_state_zalloc(zfsdev_state
, minor
) != DDI_SUCCESS
)
6043 return (SET_ERROR(EAGAIN
));
6045 *devp
= makedevice(getemajor(*devp
), minor
);
6047 zs
= ddi_get_soft_state(zfsdev_state
, minor
);
6048 zs
->zss_type
= ZSST_CTLDEV
;
6049 zfs_onexit_init((zfs_onexit_t
**)&zs
->zss_data
);
6055 zfs_ctldev_destroy(zfs_onexit_t
*zo
, minor_t minor
)
6057 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
6059 zfs_onexit_destroy(zo
);
6060 ddi_soft_state_free(zfsdev_state
, minor
);
6064 zfsdev_get_soft_state(minor_t minor
, enum zfs_soft_state_type which
)
6066 zfs_soft_state_t
*zp
;
6068 zp
= ddi_get_soft_state(zfsdev_state
, minor
);
6069 if (zp
== NULL
|| zp
->zss_type
!= which
)
6072 return (zp
->zss_data
);
6076 zfsdev_open(dev_t
*devp
, int flag
, int otyp
, cred_t
*cr
)
6080 if (getminor(*devp
) != 0)
6081 return (zvol_open(devp
, flag
, otyp
, cr
));
6083 /* This is the control device. Allocate a new minor if requested. */
6085 mutex_enter(&zfsdev_state_lock
);
6086 error
= zfs_ctldev_init(devp
);
6087 mutex_exit(&zfsdev_state_lock
);
6094 zfsdev_close(dev_t dev
, int flag
, int otyp
, cred_t
*cr
)
6097 minor_t minor
= getminor(dev
);
6102 mutex_enter(&zfsdev_state_lock
);
6103 zo
= zfsdev_get_soft_state(minor
, ZSST_CTLDEV
);
6105 mutex_exit(&zfsdev_state_lock
);
6106 return (zvol_close(dev
, flag
, otyp
, cr
));
6108 zfs_ctldev_destroy(zo
, minor
);
6109 mutex_exit(&zfsdev_state_lock
);
6115 zfsdev_ioctl(dev_t dev
, int cmd
, intptr_t arg
, int flag
, cred_t
*cr
, int *rvalp
)
6120 minor_t minor
= getminor(dev
);
6121 const zfs_ioc_vec_t
*vec
;
6122 char *saved_poolname
= NULL
;
6123 nvlist_t
*innvl
= NULL
;
6126 zfsdev_get_soft_state(minor
, ZSST_CTLDEV
) == NULL
)
6127 return (zvol_ioctl(dev
, cmd
, arg
, flag
, cr
, rvalp
));
6129 vecnum
= cmd
- ZFS_IOC_FIRST
;
6130 ASSERT3U(getmajor(dev
), ==, ddi_driver_major(zfs_dip
));
6132 if (vecnum
>= sizeof (zfs_ioc_vec
) / sizeof (zfs_ioc_vec
[0]))
6133 return (SET_ERROR(EINVAL
));
6134 vec
= &zfs_ioc_vec
[vecnum
];
6136 zc
= kmem_zalloc(sizeof (zfs_cmd_t
), KM_SLEEP
);
6138 error
= ddi_copyin((void *)arg
, zc
, sizeof (zfs_cmd_t
), flag
);
6140 error
= SET_ERROR(EFAULT
);
6144 zc
->zc_iflags
= flag
& FKIOCTL
;
6145 if (zc
->zc_nvlist_src_size
!= 0) {
6146 error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
6147 zc
->zc_iflags
, &innvl
);
6153 * Ensure that all pool/dataset names are valid before we pass down to
6156 zc
->zc_name
[sizeof (zc
->zc_name
) - 1] = '\0';
6157 switch (vec
->zvec_namecheck
) {
6159 if (pool_namecheck(zc
->zc_name
, NULL
, NULL
) != 0)
6160 error
= SET_ERROR(EINVAL
);
6162 error
= pool_status_check(zc
->zc_name
,
6163 vec
->zvec_namecheck
, vec
->zvec_pool_check
);
6167 if (dataset_namecheck(zc
->zc_name
, NULL
, NULL
) != 0)
6168 error
= SET_ERROR(EINVAL
);
6170 error
= pool_status_check(zc
->zc_name
,
6171 vec
->zvec_namecheck
, vec
->zvec_pool_check
);
6180 error
= vec
->zvec_secpolicy(zc
, innvl
, cr
);
6185 /* legacy ioctls can modify zc_name */
6186 len
= strcspn(zc
->zc_name
, "/@#") + 1;
6187 saved_poolname
= kmem_alloc(len
, KM_SLEEP
);
6188 (void) strlcpy(saved_poolname
, zc
->zc_name
, len
);
6190 if (vec
->zvec_func
!= NULL
) {
6194 nvlist_t
*lognv
= NULL
;
6196 ASSERT(vec
->zvec_legacy_func
== NULL
);
6199 * Add the innvl to the lognv before calling the func,
6200 * in case the func changes the innvl.
6202 if (vec
->zvec_allow_log
) {
6203 lognv
= fnvlist_alloc();
6204 fnvlist_add_string(lognv
, ZPOOL_HIST_IOCTL
,
6206 if (!nvlist_empty(innvl
)) {
6207 fnvlist_add_nvlist(lognv
, ZPOOL_HIST_INPUT_NVL
,
6212 outnvl
= fnvlist_alloc();
6213 error
= vec
->zvec_func(zc
->zc_name
, innvl
, outnvl
);
6216 * Some commands can partially execute, modfiy state, and still
6217 * return an error. In these cases, attempt to record what
6221 (cmd
== ZFS_IOC_CHANNEL_PROGRAM
&& error
!= EINVAL
)) &&
6222 vec
->zvec_allow_log
&&
6223 spa_open(zc
->zc_name
, &spa
, FTAG
) == 0) {
6224 if (!nvlist_empty(outnvl
)) {
6225 fnvlist_add_nvlist(lognv
, ZPOOL_HIST_OUTPUT_NVL
,
6229 fnvlist_add_int64(lognv
, ZPOOL_HIST_ERRNO
,
6232 (void) spa_history_log_nvl(spa
, lognv
);
6233 spa_close(spa
, FTAG
);
6235 fnvlist_free(lognv
);
6237 if (!nvlist_empty(outnvl
) || zc
->zc_nvlist_dst_size
!= 0) {
6239 if (vec
->zvec_smush_outnvlist
) {
6240 smusherror
= nvlist_smush(outnvl
,
6241 zc
->zc_nvlist_dst_size
);
6243 if (smusherror
== 0)
6244 puterror
= put_nvlist(zc
, outnvl
);
6250 nvlist_free(outnvl
);
6252 error
= vec
->zvec_legacy_func(zc
);
6257 rc
= ddi_copyout(zc
, (void *)arg
, sizeof (zfs_cmd_t
), flag
);
6258 if (error
== 0 && rc
!= 0)
6259 error
= SET_ERROR(EFAULT
);
6260 if (error
== 0 && vec
->zvec_allow_log
) {
6261 char *s
= tsd_get(zfs_allow_log_key
);
6264 (void) tsd_set(zfs_allow_log_key
, saved_poolname
);
6266 if (saved_poolname
!= NULL
)
6267 strfree(saved_poolname
);
6270 kmem_free(zc
, sizeof (zfs_cmd_t
));
6275 zfs_attach(dev_info_t
*dip
, ddi_attach_cmd_t cmd
)
6277 if (cmd
!= DDI_ATTACH
)
6278 return (DDI_FAILURE
);
6280 if (ddi_create_minor_node(dip
, "zfs", S_IFCHR
, 0,
6281 DDI_PSEUDO
, 0) == DDI_FAILURE
)
6282 return (DDI_FAILURE
);
6286 ddi_report_dev(dip
);
6288 return (DDI_SUCCESS
);
6292 zfs_detach(dev_info_t
*dip
, ddi_detach_cmd_t cmd
)
6294 if (spa_busy() || zfs_busy() || zvol_busy())
6295 return (DDI_FAILURE
);
6297 if (cmd
!= DDI_DETACH
)
6298 return (DDI_FAILURE
);
6302 ddi_prop_remove_all(dip
);
6303 ddi_remove_minor_node(dip
, NULL
);
6305 return (DDI_SUCCESS
);
6310 zfs_info(dev_info_t
*dip
, ddi_info_cmd_t infocmd
, void *arg
, void **result
)
6313 case DDI_INFO_DEVT2DEVINFO
:
6315 return (DDI_SUCCESS
);
6317 case DDI_INFO_DEVT2INSTANCE
:
6319 return (DDI_SUCCESS
);
6322 return (DDI_FAILURE
);
6326 * OK, so this is a little weird.
6328 * /dev/zfs is the control node, i.e. minor 0.
6329 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
6331 * /dev/zfs has basically nothing to do except serve up ioctls,
6332 * so most of the standard driver entry points are in zvol.c.
6334 static struct cb_ops zfs_cb_ops
= {
6335 zfsdev_open
, /* open */
6336 zfsdev_close
, /* close */
6337 zvol_strategy
, /* strategy */
6339 zvol_dump
, /* dump */
6340 zvol_read
, /* read */
6341 zvol_write
, /* write */
6342 zfsdev_ioctl
, /* ioctl */
6346 nochpoll
, /* poll */
6347 ddi_prop_op
, /* prop_op */
6348 NULL
, /* streamtab */
6349 D_NEW
| D_MP
| D_64BIT
, /* Driver compatibility flag */
6350 CB_REV
, /* version */
6351 nodev
, /* async read */
6352 nodev
, /* async write */
6355 static struct dev_ops zfs_dev_ops
= {
6356 DEVO_REV
, /* version */
6358 zfs_info
, /* info */
6359 nulldev
, /* identify */
6360 nulldev
, /* probe */
6361 zfs_attach
, /* attach */
6362 zfs_detach
, /* detach */
6364 &zfs_cb_ops
, /* driver operations */
6365 NULL
, /* no bus operations */
6367 ddi_quiesce_not_needed
, /* quiesce */
6370 static struct modldrv zfs_modldrv
= {
6376 static struct modlinkage modlinkage
= {
6378 (void *)&zfs_modlfs
,
6379 (void *)&zfs_modldrv
,
6384 zfs_allow_log_destroy(void *arg
)
6386 char *poolname
= arg
;
6395 spa_init(FREAD
| FWRITE
);
6400 if ((error
= mod_install(&modlinkage
)) != 0) {
6407 tsd_create(&zfs_fsyncer_key
, NULL
);
6408 tsd_create(&rrw_tsd_key
, rrw_tsd_destroy
);
6409 tsd_create(&zfs_allow_log_key
, zfs_allow_log_destroy
);
6411 error
= ldi_ident_from_mod(&modlinkage
, &zfs_li
);
6413 mutex_init(&zfs_share_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
6423 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled
)
6424 return (SET_ERROR(EBUSY
));
6426 if ((error
= mod_remove(&modlinkage
)) != 0)
6432 if (zfs_nfsshare_inited
)
6433 (void) ddi_modclose(nfs_mod
);
6434 if (zfs_smbshare_inited
)
6435 (void) ddi_modclose(smbsrv_mod
);
6436 if (zfs_nfsshare_inited
|| zfs_smbshare_inited
)
6437 (void) ddi_modclose(sharefs_mod
);
6439 tsd_destroy(&zfs_fsyncer_key
);
6440 ldi_ident_release(zfs_li
);
6442 mutex_destroy(&zfs_share_lock
);
6448 _info(struct modinfo
*modinfop
)
6450 return (mod_info(&modlinkage
, modinfop
));