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_objset_type type of objset
3580 * zc_defer_destroy mark for deferred destroy
3585 zfs_ioc_destroy(zfs_cmd_t
*zc
)
3589 if (zc
->zc_objset_type
== DMU_OST_ZFS
)
3590 zfs_unmount_snap(zc
->zc_name
);
3592 if (strchr(zc
->zc_name
, '@'))
3593 err
= dsl_destroy_snapshot(zc
->zc_name
, zc
->zc_defer_destroy
);
3595 err
= dsl_destroy_head(zc
->zc_name
);
3596 if (zc
->zc_objset_type
== DMU_OST_ZVOL
&& err
== 0)
3597 (void) zvol_remove_minor(zc
->zc_name
);
3604 * guid 1, guid 2, ...
3606 * func: POOL_INITIALIZE_{CANCEL|DO|SUSPEND}
3610 * [func: EINVAL (if provided command type didn't make sense)],
3612 * guid1: errno, (see function body for possible errnos)
3619 zfs_ioc_pool_initialize(const char *poolname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3624 error
= spa_open(poolname
, &spa
, FTAG
);
3629 if (nvlist_lookup_uint64(innvl
, ZPOOL_INITIALIZE_COMMAND
,
3631 spa_close(spa
, FTAG
);
3632 return (SET_ERROR(EINVAL
));
3634 if (!(cmd_type
== POOL_INITIALIZE_CANCEL
||
3635 cmd_type
== POOL_INITIALIZE_DO
||
3636 cmd_type
== POOL_INITIALIZE_SUSPEND
)) {
3637 spa_close(spa
, FTAG
);
3638 return (SET_ERROR(EINVAL
));
3641 nvlist_t
*vdev_guids
;
3642 if (nvlist_lookup_nvlist(innvl
, ZPOOL_INITIALIZE_VDEVS
,
3643 &vdev_guids
) != 0) {
3644 spa_close(spa
, FTAG
);
3645 return (SET_ERROR(EINVAL
));
3648 nvlist_t
*vdev_errlist
= fnvlist_alloc();
3649 int total_errors
= 0;
3651 for (nvpair_t
*pair
= nvlist_next_nvpair(vdev_guids
, NULL
);
3652 pair
!= NULL
; pair
= nvlist_next_nvpair(vdev_guids
, pair
)) {
3653 uint64_t vdev_guid
= fnvpair_value_uint64(pair
);
3655 error
= spa_vdev_initialize(spa
, vdev_guid
, cmd_type
);
3657 char guid_as_str
[MAXNAMELEN
];
3659 (void) snprintf(guid_as_str
, sizeof (guid_as_str
),
3660 "%llu", (unsigned long long)vdev_guid
);
3661 fnvlist_add_int64(vdev_errlist
, guid_as_str
, error
);
3665 if (fnvlist_size(vdev_errlist
) > 0) {
3666 fnvlist_add_nvlist(outnvl
, ZPOOL_INITIALIZE_VDEVS
,
3669 fnvlist_free(vdev_errlist
);
3671 spa_close(spa
, FTAG
);
3672 return (total_errors
> 0 ? EINVAL
: 0);
3676 * fsname is name of dataset to rollback (to most recent snapshot)
3678 * innvl may contain name of expected target snapshot
3680 * outnvl: "target" -> name of most recent snapshot
3685 zfs_ioc_rollback(const char *fsname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3688 char *target
= NULL
;
3691 (void) nvlist_lookup_string(innvl
, "target", &target
);
3692 if (target
!= NULL
) {
3693 const char *cp
= strchr(target
, '@');
3696 * The snap name must contain an @, and the part after it must
3697 * contain only valid characters.
3700 zfs_component_namecheck(cp
+ 1, NULL
, NULL
) != 0)
3701 return (SET_ERROR(EINVAL
));
3704 if (getzfsvfs(fsname
, &zfsvfs
) == 0) {
3707 ds
= dmu_objset_ds(zfsvfs
->z_os
);
3708 error
= zfs_suspend_fs(zfsvfs
);
3712 error
= dsl_dataset_rollback(fsname
, target
, zfsvfs
,
3714 resume_err
= zfs_resume_fs(zfsvfs
, ds
);
3715 error
= error
? error
: resume_err
;
3717 VFS_RELE(zfsvfs
->z_vfs
);
3719 error
= dsl_dataset_rollback(fsname
, target
, NULL
, outnvl
);
3725 recursive_unmount(const char *fsname
, void *arg
)
3727 const char *snapname
= arg
;
3728 char fullname
[ZFS_MAX_DATASET_NAME_LEN
];
3730 (void) snprintf(fullname
, sizeof (fullname
), "%s@%s", fsname
, snapname
);
3731 zfs_unmount_snap(fullname
);
3738 * zc_name old name of dataset
3739 * zc_value new name of dataset
3740 * zc_cookie recursive flag (only valid for snapshots)
3745 zfs_ioc_rename(zfs_cmd_t
*zc
)
3747 boolean_t recursive
= zc
->zc_cookie
& 1;
3750 /* "zfs rename" from and to ...%recv datasets should both fail */
3751 zc
->zc_name
[sizeof (zc
->zc_name
) - 1] = '\0';
3752 zc
->zc_value
[sizeof (zc
->zc_value
) - 1] = '\0';
3753 if (dataset_namecheck(zc
->zc_name
, NULL
, NULL
) != 0 ||
3754 dataset_namecheck(zc
->zc_value
, NULL
, NULL
) != 0 ||
3755 strchr(zc
->zc_name
, '%') || strchr(zc
->zc_value
, '%'))
3756 return (SET_ERROR(EINVAL
));
3758 at
= strchr(zc
->zc_name
, '@');
3760 /* snaps must be in same fs */
3763 if (strncmp(zc
->zc_name
, zc
->zc_value
, at
- zc
->zc_name
+ 1))
3764 return (SET_ERROR(EXDEV
));
3766 if (zc
->zc_objset_type
== DMU_OST_ZFS
) {
3767 error
= dmu_objset_find(zc
->zc_name
,
3768 recursive_unmount
, at
+ 1,
3769 recursive
? DS_FIND_CHILDREN
: 0);
3775 error
= dsl_dataset_rename_snapshot(zc
->zc_name
,
3776 at
+ 1, strchr(zc
->zc_value
, '@') + 1, recursive
);
3781 if (zc
->zc_objset_type
== DMU_OST_ZVOL
)
3782 (void) zvol_remove_minor(zc
->zc_name
);
3783 return (dsl_dir_rename(zc
->zc_name
, zc
->zc_value
));
3788 zfs_check_settable(const char *dsname
, nvpair_t
*pair
, cred_t
*cr
)
3790 const char *propname
= nvpair_name(pair
);
3791 boolean_t issnap
= (strchr(dsname
, '@') != NULL
);
3792 zfs_prop_t prop
= zfs_name_to_prop(propname
);
3796 if (prop
== ZPROP_INVAL
) {
3797 if (zfs_prop_user(propname
)) {
3798 if (err
= zfs_secpolicy_write_perms(dsname
,
3799 ZFS_DELEG_PERM_USERPROP
, cr
))
3804 if (!issnap
&& zfs_prop_userquota(propname
)) {
3805 const char *perm
= NULL
;
3806 const char *uq_prefix
=
3807 zfs_userquota_prop_prefixes
[ZFS_PROP_USERQUOTA
];
3808 const char *gq_prefix
=
3809 zfs_userquota_prop_prefixes
[ZFS_PROP_GROUPQUOTA
];
3811 if (strncmp(propname
, uq_prefix
,
3812 strlen(uq_prefix
)) == 0) {
3813 perm
= ZFS_DELEG_PERM_USERQUOTA
;
3814 } else if (strncmp(propname
, gq_prefix
,
3815 strlen(gq_prefix
)) == 0) {
3816 perm
= ZFS_DELEG_PERM_GROUPQUOTA
;
3818 /* USERUSED and GROUPUSED are read-only */
3819 return (SET_ERROR(EINVAL
));
3822 if (err
= zfs_secpolicy_write_perms(dsname
, perm
, cr
))
3827 return (SET_ERROR(EINVAL
));
3831 return (SET_ERROR(EINVAL
));
3833 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
3835 * dsl_prop_get_all_impl() returns properties in this
3839 VERIFY(nvpair_value_nvlist(pair
, &attrs
) == 0);
3840 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
3845 * Check that this value is valid for this pool version
3848 case ZFS_PROP_COMPRESSION
:
3850 * If the user specified gzip compression, make sure
3851 * the SPA supports it. We ignore any errors here since
3852 * we'll catch them later.
3854 if (nvpair_value_uint64(pair
, &intval
) == 0) {
3855 if (intval
>= ZIO_COMPRESS_GZIP_1
&&
3856 intval
<= ZIO_COMPRESS_GZIP_9
&&
3857 zfs_earlier_version(dsname
,
3858 SPA_VERSION_GZIP_COMPRESSION
)) {
3859 return (SET_ERROR(ENOTSUP
));
3862 if (intval
== ZIO_COMPRESS_ZLE
&&
3863 zfs_earlier_version(dsname
,
3864 SPA_VERSION_ZLE_COMPRESSION
))
3865 return (SET_ERROR(ENOTSUP
));
3867 if (intval
== ZIO_COMPRESS_LZ4
) {
3870 if ((err
= spa_open(dsname
, &spa
, FTAG
)) != 0)
3873 if (!spa_feature_is_enabled(spa
,
3874 SPA_FEATURE_LZ4_COMPRESS
)) {
3875 spa_close(spa
, FTAG
);
3876 return (SET_ERROR(ENOTSUP
));
3878 spa_close(spa
, FTAG
);
3882 * If this is a bootable dataset then
3883 * verify that the compression algorithm
3884 * is supported for booting. We must return
3885 * something other than ENOTSUP since it
3886 * implies a downrev pool version.
3888 if (zfs_is_bootfs(dsname
) &&
3889 !BOOTFS_COMPRESS_VALID(intval
)) {
3890 return (SET_ERROR(ERANGE
));
3895 case ZFS_PROP_COPIES
:
3896 if (zfs_earlier_version(dsname
, SPA_VERSION_DITTO_BLOCKS
))
3897 return (SET_ERROR(ENOTSUP
));
3900 case ZFS_PROP_RECORDSIZE
:
3901 /* Record sizes above 128k need the feature to be enabled */
3902 if (nvpair_value_uint64(pair
, &intval
) == 0 &&
3903 intval
> SPA_OLD_MAXBLOCKSIZE
) {
3907 * We don't allow setting the property above 1MB,
3908 * unless the tunable has been changed.
3910 if (intval
> zfs_max_recordsize
||
3911 intval
> SPA_MAXBLOCKSIZE
)
3912 return (SET_ERROR(ERANGE
));
3914 if ((err
= spa_open(dsname
, &spa
, FTAG
)) != 0)
3917 if (!spa_feature_is_enabled(spa
,
3918 SPA_FEATURE_LARGE_BLOCKS
)) {
3919 spa_close(spa
, FTAG
);
3920 return (SET_ERROR(ENOTSUP
));
3922 spa_close(spa
, FTAG
);
3926 case ZFS_PROP_SHARESMB
:
3927 if (zpl_earlier_version(dsname
, ZPL_VERSION_FUID
))
3928 return (SET_ERROR(ENOTSUP
));
3931 case ZFS_PROP_ACLINHERIT
:
3932 if (nvpair_type(pair
) == DATA_TYPE_UINT64
&&
3933 nvpair_value_uint64(pair
, &intval
) == 0) {
3934 if (intval
== ZFS_ACL_PASSTHROUGH_X
&&
3935 zfs_earlier_version(dsname
,
3936 SPA_VERSION_PASSTHROUGH_X
))
3937 return (SET_ERROR(ENOTSUP
));
3941 case ZFS_PROP_CHECKSUM
:
3942 case ZFS_PROP_DEDUP
:
3944 spa_feature_t feature
;
3947 /* dedup feature version checks */
3948 if (prop
== ZFS_PROP_DEDUP
&&
3949 zfs_earlier_version(dsname
, SPA_VERSION_DEDUP
))
3950 return (SET_ERROR(ENOTSUP
));
3952 if (nvpair_value_uint64(pair
, &intval
) != 0)
3953 return (SET_ERROR(EINVAL
));
3955 /* check prop value is enabled in features */
3956 feature
= zio_checksum_to_feature(intval
& ZIO_CHECKSUM_MASK
);
3957 if (feature
== SPA_FEATURE_NONE
)
3960 if ((err
= spa_open(dsname
, &spa
, FTAG
)) != 0)
3963 if (!spa_feature_is_enabled(spa
, feature
)) {
3964 spa_close(spa
, FTAG
);
3965 return (SET_ERROR(ENOTSUP
));
3967 spa_close(spa
, FTAG
);
3972 return (zfs_secpolicy_setprop(dsname
, prop
, pair
, CRED()));
3976 * Checks for a race condition to make sure we don't increment a feature flag
3980 zfs_prop_activate_feature_check(void *arg
, dmu_tx_t
*tx
)
3982 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
3983 spa_feature_t
*featurep
= arg
;
3985 if (!spa_feature_is_active(spa
, *featurep
))
3988 return (SET_ERROR(EBUSY
));
3992 * The callback invoked on feature activation in the sync task caused by
3993 * zfs_prop_activate_feature.
3996 zfs_prop_activate_feature_sync(void *arg
, dmu_tx_t
*tx
)
3998 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
3999 spa_feature_t
*featurep
= arg
;
4001 spa_feature_incr(spa
, *featurep
, tx
);
4005 * Activates a feature on a pool in response to a property setting. This
4006 * creates a new sync task which modifies the pool to reflect the feature
4010 zfs_prop_activate_feature(spa_t
*spa
, spa_feature_t feature
)
4014 /* EBUSY here indicates that the feature is already active */
4015 err
= dsl_sync_task(spa_name(spa
),
4016 zfs_prop_activate_feature_check
, zfs_prop_activate_feature_sync
,
4017 &feature
, 2, ZFS_SPACE_CHECK_RESERVED
);
4019 if (err
!= 0 && err
!= EBUSY
)
4026 * Removes properties from the given props list that fail permission checks
4027 * needed to clear them and to restore them in case of a receive error. For each
4028 * property, make sure we have both set and inherit permissions.
4030 * Returns the first error encountered if any permission checks fail. If the
4031 * caller provides a non-NULL errlist, it also gives the complete list of names
4032 * of all the properties that failed a permission check along with the
4033 * corresponding error numbers. The caller is responsible for freeing the
4036 * If every property checks out successfully, zero is returned and the list
4037 * pointed at by errlist is NULL.
4040 zfs_check_clearable(char *dataset
, nvlist_t
*props
, nvlist_t
**errlist
)
4043 nvpair_t
*pair
, *next_pair
;
4050 VERIFY(nvlist_alloc(&errors
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4052 zc
= kmem_alloc(sizeof (zfs_cmd_t
), KM_SLEEP
);
4053 (void) strcpy(zc
->zc_name
, dataset
);
4054 pair
= nvlist_next_nvpair(props
, NULL
);
4055 while (pair
!= NULL
) {
4056 next_pair
= nvlist_next_nvpair(props
, pair
);
4058 (void) strcpy(zc
->zc_value
, nvpair_name(pair
));
4059 if ((err
= zfs_check_settable(dataset
, pair
, CRED())) != 0 ||
4060 (err
= zfs_secpolicy_inherit_prop(zc
, NULL
, CRED())) != 0) {
4061 VERIFY(nvlist_remove_nvpair(props
, pair
) == 0);
4062 VERIFY(nvlist_add_int32(errors
,
4063 zc
->zc_value
, err
) == 0);
4067 kmem_free(zc
, sizeof (zfs_cmd_t
));
4069 if ((pair
= nvlist_next_nvpair(errors
, NULL
)) == NULL
) {
4070 nvlist_free(errors
);
4073 VERIFY(nvpair_value_int32(pair
, &rv
) == 0);
4076 if (errlist
== NULL
)
4077 nvlist_free(errors
);
4085 propval_equals(nvpair_t
*p1
, nvpair_t
*p2
)
4087 if (nvpair_type(p1
) == DATA_TYPE_NVLIST
) {
4088 /* dsl_prop_get_all_impl() format */
4090 VERIFY(nvpair_value_nvlist(p1
, &attrs
) == 0);
4091 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
4095 if (nvpair_type(p2
) == DATA_TYPE_NVLIST
) {
4097 VERIFY(nvpair_value_nvlist(p2
, &attrs
) == 0);
4098 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
4102 if (nvpair_type(p1
) != nvpair_type(p2
))
4105 if (nvpair_type(p1
) == DATA_TYPE_STRING
) {
4106 char *valstr1
, *valstr2
;
4108 VERIFY(nvpair_value_string(p1
, (char **)&valstr1
) == 0);
4109 VERIFY(nvpair_value_string(p2
, (char **)&valstr2
) == 0);
4110 return (strcmp(valstr1
, valstr2
) == 0);
4112 uint64_t intval1
, intval2
;
4114 VERIFY(nvpair_value_uint64(p1
, &intval1
) == 0);
4115 VERIFY(nvpair_value_uint64(p2
, &intval2
) == 0);
4116 return (intval1
== intval2
);
4121 * Remove properties from props if they are not going to change (as determined
4122 * by comparison with origprops). Remove them from origprops as well, since we
4123 * do not need to clear or restore properties that won't change.
4126 props_reduce(nvlist_t
*props
, nvlist_t
*origprops
)
4128 nvpair_t
*pair
, *next_pair
;
4130 if (origprops
== NULL
)
4131 return; /* all props need to be received */
4133 pair
= nvlist_next_nvpair(props
, NULL
);
4134 while (pair
!= NULL
) {
4135 const char *propname
= nvpair_name(pair
);
4138 next_pair
= nvlist_next_nvpair(props
, pair
);
4140 if ((nvlist_lookup_nvpair(origprops
, propname
,
4141 &match
) != 0) || !propval_equals(pair
, match
))
4142 goto next
; /* need to set received value */
4144 /* don't clear the existing received value */
4145 (void) nvlist_remove_nvpair(origprops
, match
);
4146 /* don't bother receiving the property */
4147 (void) nvlist_remove_nvpair(props
, pair
);
4154 * Extract properties that cannot be set PRIOR to the receipt of a dataset.
4155 * For example, refquota cannot be set until after the receipt of a dataset,
4156 * because in replication streams, an older/earlier snapshot may exceed the
4157 * refquota. We want to receive the older/earlier snapshot, but setting
4158 * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent
4159 * the older/earlier snapshot from being received (with EDQUOT).
4161 * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario.
4163 * libzfs will need to be judicious handling errors encountered by props
4164 * extracted by this function.
4167 extract_delay_props(nvlist_t
*props
)
4169 nvlist_t
*delayprops
;
4170 nvpair_t
*nvp
, *tmp
;
4171 static const zfs_prop_t delayable
[] = { ZFS_PROP_REFQUOTA
, 0 };
4174 VERIFY(nvlist_alloc(&delayprops
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4176 for (nvp
= nvlist_next_nvpair(props
, NULL
); nvp
!= NULL
;
4177 nvp
= nvlist_next_nvpair(props
, nvp
)) {
4179 * strcmp() is safe because zfs_prop_to_name() always returns
4182 for (i
= 0; delayable
[i
] != 0; i
++) {
4183 if (strcmp(zfs_prop_to_name(delayable
[i
]),
4184 nvpair_name(nvp
)) == 0) {
4188 if (delayable
[i
] != 0) {
4189 tmp
= nvlist_prev_nvpair(props
, nvp
);
4190 VERIFY(nvlist_add_nvpair(delayprops
, nvp
) == 0);
4191 VERIFY(nvlist_remove_nvpair(props
, nvp
) == 0);
4196 if (nvlist_empty(delayprops
)) {
4197 nvlist_free(delayprops
);
4200 return (delayprops
);
4204 static boolean_t zfs_ioc_recv_inject_err
;
4209 * zc_name name of containing filesystem
4210 * zc_nvlist_src{_size} nvlist of properties to apply
4211 * zc_value name of snapshot to create
4212 * zc_string name of clone origin (if DRR_FLAG_CLONE)
4213 * zc_cookie file descriptor to recv from
4214 * zc_begin_record the BEGIN record of the stream (not byteswapped)
4215 * zc_guid force flag
4216 * zc_cleanup_fd cleanup-on-exit file descriptor
4217 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
4218 * zc_resumable if data is incomplete assume sender will resume
4221 * zc_cookie number of bytes read
4222 * zc_nvlist_dst{_size} error for each unapplied received property
4223 * zc_obj zprop_errflags_t
4224 * zc_action_handle handle for this guid/ds mapping
4227 zfs_ioc_recv(zfs_cmd_t
*zc
)
4230 dmu_recv_cookie_t drc
;
4231 boolean_t force
= (boolean_t
)zc
->zc_guid
;
4234 int props_error
= 0;
4237 nvlist_t
*props
= NULL
; /* sent properties */
4238 nvlist_t
*origprops
= NULL
; /* existing properties */
4239 nvlist_t
*delayprops
= NULL
; /* sent properties applied post-receive */
4240 char *origin
= NULL
;
4242 char tofs
[ZFS_MAX_DATASET_NAME_LEN
];
4243 boolean_t first_recvd_props
= B_FALSE
;
4245 if (dataset_namecheck(zc
->zc_value
, NULL
, NULL
) != 0 ||
4246 strchr(zc
->zc_value
, '@') == NULL
||
4247 strchr(zc
->zc_value
, '%'))
4248 return (SET_ERROR(EINVAL
));
4250 (void) strcpy(tofs
, zc
->zc_value
);
4251 tosnap
= strchr(tofs
, '@');
4254 if (zc
->zc_nvlist_src
!= (uintptr_t)NULL
&&
4255 (error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
4256 zc
->zc_iflags
, &props
)) != 0)
4263 return (SET_ERROR(EBADF
));
4266 errors
= fnvlist_alloc();
4268 if (zc
->zc_string
[0])
4269 origin
= zc
->zc_string
;
4271 error
= dmu_recv_begin(tofs
, tosnap
,
4272 &zc
->zc_begin_record
, force
, zc
->zc_resumable
, origin
, &drc
);
4277 * Set properties before we receive the stream so that they are applied
4278 * to the new data. Note that we must call dmu_recv_stream() if
4279 * dmu_recv_begin() succeeds.
4281 if (props
!= NULL
&& !drc
.drc_newfs
) {
4282 if (spa_version(dsl_dataset_get_spa(drc
.drc_ds
)) >=
4283 SPA_VERSION_RECVD_PROPS
&&
4284 !dsl_prop_get_hasrecvd(tofs
))
4285 first_recvd_props
= B_TRUE
;
4288 * If new received properties are supplied, they are to
4289 * completely replace the existing received properties, so stash
4290 * away the existing ones.
4292 if (dsl_prop_get_received(tofs
, &origprops
) == 0) {
4293 nvlist_t
*errlist
= NULL
;
4295 * Don't bother writing a property if its value won't
4296 * change (and avoid the unnecessary security checks).
4298 * The first receive after SPA_VERSION_RECVD_PROPS is a
4299 * special case where we blow away all local properties
4302 if (!first_recvd_props
)
4303 props_reduce(props
, origprops
);
4304 if (zfs_check_clearable(tofs
, origprops
, &errlist
) != 0)
4305 (void) nvlist_merge(errors
, errlist
, 0);
4306 nvlist_free(errlist
);
4308 if (clear_received_props(tofs
, origprops
,
4309 first_recvd_props
? NULL
: props
) != 0)
4310 zc
->zc_obj
|= ZPROP_ERR_NOCLEAR
;
4312 zc
->zc_obj
|= ZPROP_ERR_NOCLEAR
;
4316 if (props
!= NULL
) {
4317 props_error
= dsl_prop_set_hasrecvd(tofs
);
4319 if (props_error
== 0) {
4320 delayprops
= extract_delay_props(props
);
4321 (void) zfs_set_prop_nvlist(tofs
, ZPROP_SRC_RECEIVED
,
4327 error
= dmu_recv_stream(&drc
, fp
->f_vnode
, &off
, zc
->zc_cleanup_fd
,
4328 &zc
->zc_action_handle
);
4331 zfsvfs_t
*zfsvfs
= NULL
;
4333 if (getzfsvfs(tofs
, &zfsvfs
) == 0) {
4338 ds
= dmu_objset_ds(zfsvfs
->z_os
);
4339 error
= zfs_suspend_fs(zfsvfs
);
4341 * If the suspend fails, then the recv_end will
4342 * likely also fail, and clean up after itself.
4344 end_err
= dmu_recv_end(&drc
, zfsvfs
);
4346 error
= zfs_resume_fs(zfsvfs
, ds
);
4347 error
= error
? error
: end_err
;
4348 VFS_RELE(zfsvfs
->z_vfs
);
4350 error
= dmu_recv_end(&drc
, NULL
);
4353 /* Set delayed properties now, after we're done receiving. */
4354 if (delayprops
!= NULL
&& error
== 0) {
4355 (void) zfs_set_prop_nvlist(tofs
, ZPROP_SRC_RECEIVED
,
4356 delayprops
, errors
);
4360 if (delayprops
!= NULL
) {
4362 * Merge delayed props back in with initial props, in case
4363 * we're DEBUG and zfs_ioc_recv_inject_err is set (which means
4364 * we have to make sure clear_received_props() includes
4365 * the delayed properties).
4367 * Since zfs_ioc_recv_inject_err is only in DEBUG kernels,
4368 * using ASSERT() will be just like a VERIFY.
4370 ASSERT(nvlist_merge(props
, delayprops
, 0) == 0);
4371 nvlist_free(delayprops
);
4375 * Now that all props, initial and delayed, are set, report the prop
4376 * errors to the caller.
4378 if (zc
->zc_nvlist_dst_size
!= 0 &&
4379 (nvlist_smush(errors
, zc
->zc_nvlist_dst_size
) != 0 ||
4380 put_nvlist(zc
, errors
) != 0)) {
4382 * Caller made zc->zc_nvlist_dst less than the minimum expected
4383 * size or supplied an invalid address.
4385 props_error
= SET_ERROR(EINVAL
);
4388 zc
->zc_cookie
= off
- fp
->f_offset
;
4389 if (fop_seek(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
4393 if (zfs_ioc_recv_inject_err
) {
4394 zfs_ioc_recv_inject_err
= B_FALSE
;
4399 * On error, restore the original props.
4401 if (error
!= 0 && props
!= NULL
&& !drc
.drc_newfs
) {
4402 if (clear_received_props(tofs
, props
, NULL
) != 0) {
4404 * We failed to clear the received properties.
4405 * Since we may have left a $recvd value on the
4406 * system, we can't clear the $hasrecvd flag.
4408 zc
->zc_obj
|= ZPROP_ERR_NORESTORE
;
4409 } else if (first_recvd_props
) {
4410 dsl_prop_unset_hasrecvd(tofs
);
4413 if (origprops
== NULL
&& !drc
.drc_newfs
) {
4414 /* We failed to stash the original properties. */
4415 zc
->zc_obj
|= ZPROP_ERR_NORESTORE
;
4419 * dsl_props_set() will not convert RECEIVED to LOCAL on or
4420 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
4421 * explictly if we're restoring local properties cleared in the
4422 * first new-style receive.
4424 if (origprops
!= NULL
&&
4425 zfs_set_prop_nvlist(tofs
, (first_recvd_props
?
4426 ZPROP_SRC_LOCAL
: ZPROP_SRC_RECEIVED
),
4427 origprops
, NULL
) != 0) {
4429 * We stashed the original properties but failed to
4432 zc
->zc_obj
|= ZPROP_ERR_NORESTORE
;
4437 nvlist_free(origprops
);
4438 nvlist_free(errors
);
4442 error
= props_error
;
4449 * zc_name name of snapshot to send
4450 * zc_cookie file descriptor to send stream to
4451 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
4452 * zc_sendobj objsetid of snapshot to send
4453 * zc_fromobj objsetid of incremental fromsnap (may be zero)
4454 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
4455 * output size in zc_objset_type.
4456 * zc_flags lzc_send_flags
4459 * zc_objset_type estimated size, if zc_guid is set
4462 zfs_ioc_send(zfs_cmd_t
*zc
)
4466 boolean_t estimate
= (zc
->zc_guid
!= 0);
4467 boolean_t embedok
= (zc
->zc_flags
& 0x1);
4468 boolean_t large_block_ok
= (zc
->zc_flags
& 0x2);
4469 boolean_t compressok
= (zc
->zc_flags
& 0x4);
4471 if (zc
->zc_obj
!= 0) {
4473 dsl_dataset_t
*tosnap
;
4475 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
4479 error
= dsl_dataset_hold_obj(dp
, zc
->zc_sendobj
, FTAG
, &tosnap
);
4481 dsl_pool_rele(dp
, FTAG
);
4485 if (dsl_dir_is_clone(tosnap
->ds_dir
))
4487 dsl_dir_phys(tosnap
->ds_dir
)->dd_origin_obj
;
4488 dsl_dataset_rele(tosnap
, FTAG
);
4489 dsl_pool_rele(dp
, FTAG
);
4494 dsl_dataset_t
*tosnap
;
4495 dsl_dataset_t
*fromsnap
= NULL
;
4497 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
4501 error
= dsl_dataset_hold_obj(dp
, zc
->zc_sendobj
, FTAG
, &tosnap
);
4503 dsl_pool_rele(dp
, FTAG
);
4507 if (zc
->zc_fromobj
!= 0) {
4508 error
= dsl_dataset_hold_obj(dp
, zc
->zc_fromobj
,
4511 dsl_dataset_rele(tosnap
, FTAG
);
4512 dsl_pool_rele(dp
, FTAG
);
4517 error
= dmu_send_estimate(tosnap
, fromsnap
, compressok
,
4518 &zc
->zc_objset_type
);
4520 if (fromsnap
!= NULL
)
4521 dsl_dataset_rele(fromsnap
, FTAG
);
4522 dsl_dataset_rele(tosnap
, FTAG
);
4523 dsl_pool_rele(dp
, FTAG
);
4525 file_t
*fp
= getf(zc
->zc_cookie
);
4527 return (SET_ERROR(EBADF
));
4530 error
= dmu_send_obj(zc
->zc_name
, zc
->zc_sendobj
,
4531 zc
->zc_fromobj
, embedok
, large_block_ok
, compressok
,
4532 zc
->zc_cookie
, fp
->f_vnode
, &off
);
4534 if (fop_seek(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
4536 releasef(zc
->zc_cookie
);
4543 * zc_name name of snapshot on which to report progress
4544 * zc_cookie file descriptor of send stream
4547 * zc_cookie number of bytes written in send stream thus far
4550 zfs_ioc_send_progress(zfs_cmd_t
*zc
)
4554 dmu_sendarg_t
*dsp
= NULL
;
4557 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
4561 error
= dsl_dataset_hold(dp
, zc
->zc_name
, FTAG
, &ds
);
4563 dsl_pool_rele(dp
, FTAG
);
4567 mutex_enter(&ds
->ds_sendstream_lock
);
4570 * Iterate over all the send streams currently active on this dataset.
4571 * If there's one which matches the specified file descriptor _and_ the
4572 * stream was started by the current process, return the progress of
4575 for (dsp
= list_head(&ds
->ds_sendstreams
); dsp
!= NULL
;
4576 dsp
= list_next(&ds
->ds_sendstreams
, dsp
)) {
4577 if (dsp
->dsa_outfd
== zc
->zc_cookie
&&
4578 dsp
->dsa_proc
== curproc
)
4583 zc
->zc_cookie
= *(dsp
->dsa_off
);
4585 error
= SET_ERROR(ENOENT
);
4587 mutex_exit(&ds
->ds_sendstream_lock
);
4588 dsl_dataset_rele(ds
, FTAG
);
4589 dsl_pool_rele(dp
, FTAG
);
4594 zfs_ioc_inject_fault(zfs_cmd_t
*zc
)
4598 error
= zio_inject_fault(zc
->zc_name
, (int)zc
->zc_guid
, &id
,
4599 &zc
->zc_inject_record
);
4602 zc
->zc_guid
= (uint64_t)id
;
4608 zfs_ioc_clear_fault(zfs_cmd_t
*zc
)
4610 return (zio_clear_fault((int)zc
->zc_guid
));
4614 zfs_ioc_inject_list_next(zfs_cmd_t
*zc
)
4616 int id
= (int)zc
->zc_guid
;
4619 error
= zio_inject_list_next(&id
, zc
->zc_name
, sizeof (zc
->zc_name
),
4620 &zc
->zc_inject_record
);
4628 zfs_ioc_error_log(zfs_cmd_t
*zc
)
4632 size_t count
= (size_t)zc
->zc_nvlist_dst_size
;
4634 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
4637 error
= spa_get_errlog(spa
, (void *)(uintptr_t)zc
->zc_nvlist_dst
,
4640 zc
->zc_nvlist_dst_size
= count
;
4642 zc
->zc_nvlist_dst_size
= spa_get_errlog_size(spa
);
4644 spa_close(spa
, FTAG
);
4650 zfs_ioc_clear(zfs_cmd_t
*zc
)
4657 * On zpool clear we also fix up missing slogs
4659 mutex_enter(&spa_namespace_lock
);
4660 spa
= spa_lookup(zc
->zc_name
);
4662 mutex_exit(&spa_namespace_lock
);
4663 return (SET_ERROR(EIO
));
4665 if (spa_get_log_state(spa
) == SPA_LOG_MISSING
) {
4666 /* we need to let spa_open/spa_load clear the chains */
4667 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
4669 spa
->spa_last_open_failed
= 0;
4670 mutex_exit(&spa_namespace_lock
);
4672 if (zc
->zc_cookie
& ZPOOL_NO_REWIND
) {
4673 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
4676 nvlist_t
*config
= NULL
;
4678 if (zc
->zc_nvlist_src
== (uintptr_t)NULL
)
4679 return (SET_ERROR(EINVAL
));
4681 if ((error
= get_nvlist(zc
->zc_nvlist_src
,
4682 zc
->zc_nvlist_src_size
, zc
->zc_iflags
, &policy
)) == 0) {
4683 error
= spa_open_rewind(zc
->zc_name
, &spa
, FTAG
,
4685 if (config
!= NULL
) {
4688 if ((err
= put_nvlist(zc
, config
)) != 0)
4690 nvlist_free(config
);
4692 nvlist_free(policy
);
4699 spa_vdev_state_enter(spa
, SCL_NONE
);
4701 if (zc
->zc_guid
== 0) {
4704 vd
= spa_lookup_by_guid(spa
, zc
->zc_guid
, B_TRUE
);
4706 (void) spa_vdev_state_exit(spa
, NULL
, ENODEV
);
4707 spa_close(spa
, FTAG
);
4708 return (SET_ERROR(ENODEV
));
4712 vdev_clear(spa
, vd
);
4714 (void) spa_vdev_state_exit(spa
, NULL
, 0);
4717 * Resume any suspended I/Os.
4719 if (zio_resume(spa
) != 0)
4720 error
= SET_ERROR(EIO
);
4722 spa_close(spa
, FTAG
);
4728 zfs_ioc_pool_reopen(zfs_cmd_t
*zc
)
4733 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
4737 spa_vdev_state_enter(spa
, SCL_NONE
);
4740 * If a resilver is already in progress then set the
4741 * spa_scrub_reopen flag to B_TRUE so that we don't restart
4742 * the scan as a side effect of the reopen. Otherwise, let
4743 * vdev_open() decided if a resilver is required.
4745 spa
->spa_scrub_reopen
= dsl_scan_resilvering(spa
->spa_dsl_pool
);
4746 vdev_reopen(spa
->spa_root_vdev
);
4747 spa
->spa_scrub_reopen
= B_FALSE
;
4749 (void) spa_vdev_state_exit(spa
, NULL
, 0);
4750 spa_close(spa
, FTAG
);
4755 * zc_name name of filesystem
4758 * zc_string name of conflicting snapshot, if there is one
4761 zfs_ioc_promote(zfs_cmd_t
*zc
)
4764 dsl_dataset_t
*ds
, *ods
;
4765 char origin
[ZFS_MAX_DATASET_NAME_LEN
];
4769 zc
->zc_name
[sizeof (zc
->zc_name
) - 1] = '\0';
4770 if (dataset_namecheck(zc
->zc_name
, NULL
, NULL
) != 0 ||
4771 strchr(zc
->zc_name
, '%'))
4772 return (SET_ERROR(EINVAL
));
4774 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
4778 error
= dsl_dataset_hold(dp
, zc
->zc_name
, FTAG
, &ds
);
4780 dsl_pool_rele(dp
, FTAG
);
4784 if (!dsl_dir_is_clone(ds
->ds_dir
)) {
4785 dsl_dataset_rele(ds
, FTAG
);
4786 dsl_pool_rele(dp
, FTAG
);
4787 return (SET_ERROR(EINVAL
));
4790 error
= dsl_dataset_hold_obj(dp
,
4791 dsl_dir_phys(ds
->ds_dir
)->dd_origin_obj
, FTAG
, &ods
);
4793 dsl_dataset_rele(ds
, FTAG
);
4794 dsl_pool_rele(dp
, FTAG
);
4798 dsl_dataset_name(ods
, origin
);
4799 dsl_dataset_rele(ods
, FTAG
);
4800 dsl_dataset_rele(ds
, FTAG
);
4801 dsl_pool_rele(dp
, FTAG
);
4804 * We don't need to unmount *all* the origin fs's snapshots, but
4807 cp
= strchr(origin
, '@');
4810 (void) dmu_objset_find(origin
,
4811 zfs_unmount_snap_cb
, NULL
, DS_FIND_SNAPSHOTS
);
4812 return (dsl_dataset_promote(zc
->zc_name
, zc
->zc_string
));
4816 * Retrieve a single {user|group}{used|quota}@... property.
4819 * zc_name name of filesystem
4820 * zc_objset_type zfs_userquota_prop_t
4821 * zc_value domain name (eg. "S-1-234-567-89")
4822 * zc_guid RID/UID/GID
4825 * zc_cookie property value
4828 zfs_ioc_userspace_one(zfs_cmd_t
*zc
)
4833 if (zc
->zc_objset_type
>= ZFS_NUM_USERQUOTA_PROPS
)
4834 return (SET_ERROR(EINVAL
));
4836 error
= zfsvfs_hold(zc
->zc_name
, FTAG
, &zfsvfs
, B_FALSE
);
4840 error
= zfs_userspace_one(zfsvfs
,
4841 zc
->zc_objset_type
, zc
->zc_value
, zc
->zc_guid
, &zc
->zc_cookie
);
4842 zfsvfs_rele(zfsvfs
, FTAG
);
4849 * zc_name name of filesystem
4850 * zc_cookie zap cursor
4851 * zc_objset_type zfs_userquota_prop_t
4852 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
4855 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
4856 * zc_cookie zap cursor
4859 zfs_ioc_userspace_many(zfs_cmd_t
*zc
)
4862 int bufsize
= zc
->zc_nvlist_dst_size
;
4865 return (SET_ERROR(ENOMEM
));
4867 int error
= zfsvfs_hold(zc
->zc_name
, FTAG
, &zfsvfs
, B_FALSE
);
4871 void *buf
= kmem_alloc(bufsize
, KM_SLEEP
);
4873 error
= zfs_userspace_many(zfsvfs
, zc
->zc_objset_type
, &zc
->zc_cookie
,
4874 buf
, &zc
->zc_nvlist_dst_size
);
4877 error
= xcopyout(buf
,
4878 (void *)(uintptr_t)zc
->zc_nvlist_dst
,
4879 zc
->zc_nvlist_dst_size
);
4881 kmem_free(buf
, bufsize
);
4882 zfsvfs_rele(zfsvfs
, FTAG
);
4889 * zc_name name of filesystem
4895 zfs_ioc_userspace_upgrade(zfs_cmd_t
*zc
)
4901 if (getzfsvfs(zc
->zc_name
, &zfsvfs
) == 0) {
4902 if (!dmu_objset_userused_enabled(zfsvfs
->z_os
)) {
4904 * If userused is not enabled, it may be because the
4905 * objset needs to be closed & reopened (to grow the
4906 * objset_phys_t). Suspend/resume the fs will do that.
4908 dsl_dataset_t
*ds
, *newds
;
4910 ds
= dmu_objset_ds(zfsvfs
->z_os
);
4911 error
= zfs_suspend_fs(zfsvfs
);
4913 dmu_objset_refresh_ownership(ds
, &newds
,
4915 error
= zfs_resume_fs(zfsvfs
, newds
);
4919 error
= dmu_objset_userspace_upgrade(zfsvfs
->z_os
);
4920 VFS_RELE(zfsvfs
->z_vfs
);
4922 /* XXX kind of reading contents without owning */
4923 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
4927 error
= dmu_objset_userspace_upgrade(os
);
4928 dmu_objset_rele(os
, FTAG
);
4935 * We don't want to have a hard dependency
4936 * against some special symbols in sharefs
4937 * nfs, and smbsrv. Determine them if needed when
4938 * the first file system is shared.
4939 * Neither sharefs, nfs or smbsrv are unloadable modules.
4941 int (*znfsexport_fs
)(void *arg
);
4942 int (*zshare_fs
)(enum sharefs_sys_op
, share_t
*, uint32_t);
4943 int (*zsmbexport_fs
)(void *arg
, boolean_t add_share
);
4945 int zfs_nfsshare_inited
;
4946 int zfs_smbshare_inited
;
4948 ddi_modhandle_t nfs_mod
;
4949 ddi_modhandle_t sharefs_mod
;
4950 ddi_modhandle_t smbsrv_mod
;
4951 kmutex_t zfs_share_lock
;
4958 ASSERT(MUTEX_HELD(&zfs_share_lock
));
4959 /* Both NFS and SMB shares also require sharetab support. */
4960 if (sharefs_mod
== NULL
&& ((sharefs_mod
=
4961 ddi_modopen("fs/sharefs",
4962 KRTLD_MODE_FIRST
, &error
)) == NULL
)) {
4963 return (SET_ERROR(ENOSYS
));
4965 if (zshare_fs
== NULL
&& ((zshare_fs
=
4966 (int (*)(enum sharefs_sys_op
, share_t
*, uint32_t))
4967 ddi_modsym(sharefs_mod
, "sharefs_impl", &error
)) == NULL
)) {
4968 return (SET_ERROR(ENOSYS
));
4974 zfs_ioc_share(zfs_cmd_t
*zc
)
4979 switch (zc
->zc_share
.z_sharetype
) {
4981 case ZFS_UNSHARE_NFS
:
4982 if (zfs_nfsshare_inited
== 0) {
4983 mutex_enter(&zfs_share_lock
);
4984 if (nfs_mod
== NULL
&& ((nfs_mod
= ddi_modopen("fs/nfs",
4985 KRTLD_MODE_FIRST
, &error
)) == NULL
)) {
4986 mutex_exit(&zfs_share_lock
);
4987 return (SET_ERROR(ENOSYS
));
4989 if (znfsexport_fs
== NULL
&&
4990 ((znfsexport_fs
= (int (*)(void *))
4992 "nfs_export", &error
)) == NULL
)) {
4993 mutex_exit(&zfs_share_lock
);
4994 return (SET_ERROR(ENOSYS
));
4996 error
= zfs_init_sharefs();
4998 mutex_exit(&zfs_share_lock
);
4999 return (SET_ERROR(ENOSYS
));
5001 zfs_nfsshare_inited
= 1;
5002 mutex_exit(&zfs_share_lock
);
5006 case ZFS_UNSHARE_SMB
:
5007 if (zfs_smbshare_inited
== 0) {
5008 mutex_enter(&zfs_share_lock
);
5009 if (smbsrv_mod
== NULL
&& ((smbsrv_mod
=
5010 ddi_modopen("drv/smbsrv",
5011 KRTLD_MODE_FIRST
, &error
)) == NULL
)) {
5012 mutex_exit(&zfs_share_lock
);
5013 return (SET_ERROR(ENOSYS
));
5015 if (zsmbexport_fs
== NULL
&& ((zsmbexport_fs
=
5016 (int (*)(void *, boolean_t
))ddi_modsym(smbsrv_mod
,
5017 "smb_server_share", &error
)) == NULL
)) {
5018 mutex_exit(&zfs_share_lock
);
5019 return (SET_ERROR(ENOSYS
));
5021 error
= zfs_init_sharefs();
5023 mutex_exit(&zfs_share_lock
);
5024 return (SET_ERROR(ENOSYS
));
5026 zfs_smbshare_inited
= 1;
5027 mutex_exit(&zfs_share_lock
);
5031 return (SET_ERROR(EINVAL
));
5034 switch (zc
->zc_share
.z_sharetype
) {
5036 case ZFS_UNSHARE_NFS
:
5038 znfsexport_fs((void *)
5039 (uintptr_t)zc
->zc_share
.z_exportdata
))
5043 case ZFS_UNSHARE_SMB
:
5044 if (error
= zsmbexport_fs((void *)
5045 (uintptr_t)zc
->zc_share
.z_exportdata
,
5046 zc
->zc_share
.z_sharetype
== ZFS_SHARE_SMB
?
5053 opcode
= (zc
->zc_share
.z_sharetype
== ZFS_SHARE_NFS
||
5054 zc
->zc_share
.z_sharetype
== ZFS_SHARE_SMB
) ?
5055 SHAREFS_ADD
: SHAREFS_REMOVE
;
5058 * Add or remove share from sharetab
5060 error
= zshare_fs(opcode
,
5061 (void *)(uintptr_t)zc
->zc_share
.z_sharedata
,
5062 zc
->zc_share
.z_sharemax
);
5068 ace_t full_access
[] = {
5069 {(uid_t
)-1, ACE_ALL_PERMS
, ACE_EVERYONE
, 0}
5074 * zc_name name of containing filesystem
5075 * zc_obj object # beyond which we want next in-use object #
5078 * zc_obj next in-use object #
5081 zfs_ioc_next_obj(zfs_cmd_t
*zc
)
5083 objset_t
*os
= NULL
;
5086 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
5090 error
= dmu_object_next(os
, &zc
->zc_obj
, B_FALSE
,
5091 dsl_dataset_phys(os
->os_dsl_dataset
)->ds_prev_snap_txg
);
5093 dmu_objset_rele(os
, FTAG
);
5099 * zc_name name of filesystem
5100 * zc_value prefix name for snapshot
5101 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
5104 * zc_value short name of new snapshot
5107 zfs_ioc_tmp_snapshot(zfs_cmd_t
*zc
)
5114 error
= zfs_onexit_fd_hold(zc
->zc_cleanup_fd
, &minor
);
5118 snap_name
= kmem_asprintf("%s-%016llx", zc
->zc_value
,
5119 (u_longlong_t
)ddi_get_lbolt64());
5120 hold_name
= kmem_asprintf("%%%s", zc
->zc_value
);
5122 error
= dsl_dataset_snapshot_tmp(zc
->zc_name
, snap_name
, minor
,
5125 (void) strcpy(zc
->zc_value
, snap_name
);
5128 zfs_onexit_fd_rele(zc
->zc_cleanup_fd
);
5134 * zc_name name of "to" snapshot
5135 * zc_value name of "from" snapshot
5136 * zc_cookie file descriptor to write diff data on
5139 * dmu_diff_record_t's to the file descriptor
5142 zfs_ioc_diff(zfs_cmd_t
*zc
)
5148 fp
= getf(zc
->zc_cookie
);
5150 return (SET_ERROR(EBADF
));
5154 error
= dmu_diff(zc
->zc_name
, zc
->zc_value
, fp
->f_vnode
, &off
);
5156 if (fop_seek(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
5158 releasef(zc
->zc_cookie
);
5164 * Remove all ACL files in shares dir
5167 zfs_smb_acl_purge(znode_t
*dzp
)
5170 zap_attribute_t zap
;
5171 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
5174 for (zap_cursor_init(&zc
, zfsvfs
->z_os
, dzp
->z_id
);
5175 (error
= zap_cursor_retrieve(&zc
, &zap
)) == 0;
5176 zap_cursor_advance(&zc
)) {
5177 if ((error
= fop_remove(ZTOV(dzp
), zap
.za_name
, kcred
,
5181 zap_cursor_fini(&zc
);
5186 zfs_ioc_smb_acl(zfs_cmd_t
*zc
)
5190 vnode_t
*resourcevp
= NULL
;
5199 if ((error
= lookupname(zc
->zc_value
, UIO_SYSSPACE
,
5200 NO_FOLLOW
, NULL
, &vp
)) != 0)
5203 /* Now make sure mntpnt and dataset are ZFS */
5205 if (vp
->v_vfsp
->vfs_fstype
!= zfsfstype
||
5206 (strcmp((char *)refstr_value(vp
->v_vfsp
->vfs_resource
),
5207 zc
->zc_name
) != 0)) {
5209 return (SET_ERROR(EINVAL
));
5213 zfsvfs
= dzp
->z_zfsvfs
;
5217 * Create share dir if its missing.
5219 mutex_enter(&zfsvfs
->z_lock
);
5220 if (zfsvfs
->z_shares_dir
== 0) {
5223 tx
= dmu_tx_create(zfsvfs
->z_os
);
5224 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
, TRUE
,
5226 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, FALSE
, NULL
);
5227 error
= dmu_tx_assign(tx
, TXG_WAIT
);
5231 error
= zfs_create_share_dir(zfsvfs
, tx
);
5235 mutex_exit(&zfsvfs
->z_lock
);
5241 mutex_exit(&zfsvfs
->z_lock
);
5243 ASSERT(zfsvfs
->z_shares_dir
);
5244 if ((error
= zfs_zget(zfsvfs
, zfsvfs
->z_shares_dir
, &sharedir
)) != 0) {
5250 switch (zc
->zc_cookie
) {
5251 case ZFS_SMB_ACL_ADD
:
5252 vattr
.va_mask
= VATTR_MODE
|VATTR_UID
|VATTR_GID
|VATTR_TYPE
;
5253 vattr
.va_type
= VREG
;
5254 vattr
.va_mode
= S_IFREG
|0777;
5258 vsec
.vsa_mask
= VSA_ACE
;
5259 vsec
.vsa_aclentp
= &full_access
;
5260 vsec
.vsa_aclentsz
= sizeof (full_access
);
5261 vsec
.vsa_aclcnt
= 1;
5263 error
= fop_create(ZTOV(sharedir
), zc
->zc_string
,
5264 &vattr
, EXCL
, 0, &resourcevp
, kcred
, 0, NULL
, &vsec
);
5266 VN_RELE(resourcevp
);
5269 case ZFS_SMB_ACL_REMOVE
:
5270 error
= fop_remove(ZTOV(sharedir
), zc
->zc_string
, kcred
,
5274 case ZFS_SMB_ACL_RENAME
:
5275 if ((error
= get_nvlist(zc
->zc_nvlist_src
,
5276 zc
->zc_nvlist_src_size
, zc
->zc_iflags
, &nvlist
)) != 0) {
5278 VN_RELE(ZTOV(sharedir
));
5282 if (nvlist_lookup_string(nvlist
, ZFS_SMB_ACL_SRC
, &src
) ||
5283 nvlist_lookup_string(nvlist
, ZFS_SMB_ACL_TARGET
,
5286 VN_RELE(ZTOV(sharedir
));
5288 nvlist_free(nvlist
);
5291 error
= fop_rename(ZTOV(sharedir
), src
, ZTOV(sharedir
), target
,
5293 nvlist_free(nvlist
);
5296 case ZFS_SMB_ACL_PURGE
:
5297 error
= zfs_smb_acl_purge(sharedir
);
5301 error
= SET_ERROR(EINVAL
);
5306 VN_RELE(ZTOV(sharedir
));
5315 * "holds" -> { snapname -> holdname (string), ... }
5316 * (optional) "cleanup_fd" -> fd (int32)
5320 * snapname -> error value (int32)
5326 zfs_ioc_hold(const char *pool
, nvlist_t
*args
, nvlist_t
*errlist
)
5330 int cleanup_fd
= -1;
5334 error
= nvlist_lookup_nvlist(args
, "holds", &holds
);
5336 return (SET_ERROR(EINVAL
));
5338 /* make sure the user didn't pass us any invalid (empty) tags */
5339 for (pair
= nvlist_next_nvpair(holds
, NULL
); pair
!= NULL
;
5340 pair
= nvlist_next_nvpair(holds
, pair
)) {
5343 error
= nvpair_value_string(pair
, &htag
);
5345 return (SET_ERROR(error
));
5347 if (strlen(htag
) == 0)
5348 return (SET_ERROR(EINVAL
));
5351 if (nvlist_lookup_int32(args
, "cleanup_fd", &cleanup_fd
) == 0) {
5352 error
= zfs_onexit_fd_hold(cleanup_fd
, &minor
);
5357 error
= dsl_dataset_user_hold(holds
, minor
, errlist
);
5359 zfs_onexit_fd_rele(cleanup_fd
);
5364 * innvl is not used.
5367 * holdname -> time added (uint64 seconds since epoch)
5373 zfs_ioc_get_holds(const char *snapname
, nvlist_t
*args
, nvlist_t
*outnvl
)
5375 return (dsl_dataset_get_holds(snapname
, outnvl
));
5380 * snapname -> { holdname, ... }
5385 * snapname -> error value (int32)
5391 zfs_ioc_release(const char *pool
, nvlist_t
*holds
, nvlist_t
*errlist
)
5393 return (dsl_dataset_user_release(holds
, errlist
));
5398 * zc_name name of new filesystem or snapshot
5399 * zc_value full name of old snapshot
5402 * zc_cookie space in bytes
5403 * zc_objset_type compressed space in bytes
5404 * zc_perm_action uncompressed space in bytes
5407 zfs_ioc_space_written(zfs_cmd_t
*zc
)
5411 dsl_dataset_t
*new, *old
;
5413 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
5416 error
= dsl_dataset_hold(dp
, zc
->zc_name
, FTAG
, &new);
5418 dsl_pool_rele(dp
, FTAG
);
5421 error
= dsl_dataset_hold(dp
, zc
->zc_value
, FTAG
, &old
);
5423 dsl_dataset_rele(new, FTAG
);
5424 dsl_pool_rele(dp
, FTAG
);
5428 error
= dsl_dataset_space_written(old
, new, &zc
->zc_cookie
,
5429 &zc
->zc_objset_type
, &zc
->zc_perm_action
);
5430 dsl_dataset_rele(old
, FTAG
);
5431 dsl_dataset_rele(new, FTAG
);
5432 dsl_pool_rele(dp
, FTAG
);
5438 * "firstsnap" -> snapshot name
5442 * "used" -> space in bytes
5443 * "compressed" -> compressed space in bytes
5444 * "uncompressed" -> uncompressed space in bytes
5448 zfs_ioc_space_snaps(const char *lastsnap
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
5452 dsl_dataset_t
*new, *old
;
5454 uint64_t used
, comp
, uncomp
;
5456 if (nvlist_lookup_string(innvl
, "firstsnap", &firstsnap
) != 0)
5457 return (SET_ERROR(EINVAL
));
5459 error
= dsl_pool_hold(lastsnap
, FTAG
, &dp
);
5463 error
= dsl_dataset_hold(dp
, lastsnap
, FTAG
, &new);
5464 if (error
== 0 && !new->ds_is_snapshot
) {
5465 dsl_dataset_rele(new, FTAG
);
5466 error
= SET_ERROR(EINVAL
);
5469 dsl_pool_rele(dp
, FTAG
);
5472 error
= dsl_dataset_hold(dp
, firstsnap
, FTAG
, &old
);
5473 if (error
== 0 && !old
->ds_is_snapshot
) {
5474 dsl_dataset_rele(old
, FTAG
);
5475 error
= SET_ERROR(EINVAL
);
5478 dsl_dataset_rele(new, FTAG
);
5479 dsl_pool_rele(dp
, FTAG
);
5483 error
= dsl_dataset_space_wouldfree(old
, new, &used
, &comp
, &uncomp
);
5484 dsl_dataset_rele(old
, FTAG
);
5485 dsl_dataset_rele(new, FTAG
);
5486 dsl_pool_rele(dp
, FTAG
);
5487 fnvlist_add_uint64(outnvl
, "used", used
);
5488 fnvlist_add_uint64(outnvl
, "compressed", comp
);
5489 fnvlist_add_uint64(outnvl
, "uncompressed", uncomp
);
5495 * "fd" -> file descriptor to write stream to (int32)
5496 * (optional) "fromsnap" -> full snap name to send an incremental from
5497 * (optional) "largeblockok" -> (value ignored)
5498 * indicates that blocks > 128KB are permitted
5499 * (optional) "embedok" -> (value ignored)
5500 * presence indicates DRR_WRITE_EMBEDDED records are permitted
5501 * (optional) "compressok" -> (value ignored)
5502 * presence indicates compressed DRR_WRITE records are permitted
5503 * (optional) "resume_object" and "resume_offset" -> (uint64)
5504 * if present, resume send stream from specified object and offset.
5511 zfs_ioc_send_new(const char *snapname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
5515 char *fromname
= NULL
;
5517 boolean_t largeblockok
;
5519 boolean_t compressok
;
5520 uint64_t resumeobj
= 0;
5521 uint64_t resumeoff
= 0;
5523 error
= nvlist_lookup_int32(innvl
, "fd", &fd
);
5525 return (SET_ERROR(EINVAL
));
5527 (void) nvlist_lookup_string(innvl
, "fromsnap", &fromname
);
5529 largeblockok
= nvlist_exists(innvl
, "largeblockok");
5530 embedok
= nvlist_exists(innvl
, "embedok");
5531 compressok
= nvlist_exists(innvl
, "compressok");
5533 (void) nvlist_lookup_uint64(innvl
, "resume_object", &resumeobj
);
5534 (void) nvlist_lookup_uint64(innvl
, "resume_offset", &resumeoff
);
5536 file_t
*fp
= getf(fd
);
5538 return (SET_ERROR(EBADF
));
5541 error
= dmu_send(snapname
, fromname
, embedok
, largeblockok
, compressok
,
5542 fd
, resumeobj
, resumeoff
, fp
->f_vnode
, &off
);
5544 if (fop_seek(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
5551 * Determine approximately how large a zfs send stream will be -- the number
5552 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
5555 * (optional) "from" -> full snap or bookmark name to send an incremental
5557 * (optional) "largeblockok" -> (value ignored)
5558 * indicates that blocks > 128KB are permitted
5559 * (optional) "embedok" -> (value ignored)
5560 * presence indicates DRR_WRITE_EMBEDDED records are permitted
5561 * (optional) "compressok" -> (value ignored)
5562 * presence indicates compressed DRR_WRITE records are permitted
5566 * "space" -> bytes of space (uint64)
5570 zfs_ioc_send_space(const char *snapname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
5573 dsl_dataset_t
*tosnap
;
5576 boolean_t compressok
;
5579 error
= dsl_pool_hold(snapname
, FTAG
, &dp
);
5583 error
= dsl_dataset_hold(dp
, snapname
, FTAG
, &tosnap
);
5585 dsl_pool_rele(dp
, FTAG
);
5589 compressok
= nvlist_exists(innvl
, "compressok");
5591 error
= nvlist_lookup_string(innvl
, "from", &fromname
);
5593 if (strchr(fromname
, '@') != NULL
) {
5595 * If from is a snapshot, hold it and use the more
5596 * efficient dmu_send_estimate to estimate send space
5597 * size using deadlists.
5599 dsl_dataset_t
*fromsnap
;
5600 error
= dsl_dataset_hold(dp
, fromname
, FTAG
, &fromsnap
);
5603 error
= dmu_send_estimate(tosnap
, fromsnap
, compressok
,
5605 dsl_dataset_rele(fromsnap
, FTAG
);
5606 } else if (strchr(fromname
, '#') != NULL
) {
5608 * If from is a bookmark, fetch the creation TXG of the
5609 * snapshot it was created from and use that to find
5610 * blocks that were born after it.
5612 zfs_bookmark_phys_t frombm
;
5614 error
= dsl_bookmark_lookup(dp
, fromname
, tosnap
,
5618 error
= dmu_send_estimate_from_txg(tosnap
,
5619 frombm
.zbm_creation_txg
, compressok
, &space
);
5622 * from is not properly formatted as a snapshot or
5625 error
= SET_ERROR(EINVAL
);
5630 * If estimating the size of a full send, use dmu_send_estimate.
5632 error
= dmu_send_estimate(tosnap
, NULL
, compressok
, &space
);
5635 fnvlist_add_uint64(outnvl
, "space", space
);
5638 dsl_dataset_rele(tosnap
, FTAG
);
5639 dsl_pool_rele(dp
, FTAG
);
5643 static zfs_ioc_vec_t zfs_ioc_vec
[ZFS_IOC_LAST
- ZFS_IOC_FIRST
];
5646 zfs_ioctl_register_legacy(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5647 zfs_secpolicy_func_t
*secpolicy
, zfs_ioc_namecheck_t namecheck
,
5648 boolean_t log_history
, zfs_ioc_poolcheck_t pool_check
)
5650 zfs_ioc_vec_t
*vec
= &zfs_ioc_vec
[ioc
- ZFS_IOC_FIRST
];
5652 ASSERT3U(ioc
, >=, ZFS_IOC_FIRST
);
5653 ASSERT3U(ioc
, <, ZFS_IOC_LAST
);
5654 ASSERT3P(vec
->zvec_legacy_func
, ==, NULL
);
5655 ASSERT3P(vec
->zvec_func
, ==, NULL
);
5657 vec
->zvec_legacy_func
= func
;
5658 vec
->zvec_secpolicy
= secpolicy
;
5659 vec
->zvec_namecheck
= namecheck
;
5660 vec
->zvec_allow_log
= log_history
;
5661 vec
->zvec_pool_check
= pool_check
;
5665 * See the block comment at the beginning of this file for details on
5666 * each argument to this function.
5669 zfs_ioctl_register(const char *name
, zfs_ioc_t ioc
, zfs_ioc_func_t
*func
,
5670 zfs_secpolicy_func_t
*secpolicy
, zfs_ioc_namecheck_t namecheck
,
5671 zfs_ioc_poolcheck_t pool_check
, boolean_t smush_outnvlist
,
5672 boolean_t allow_log
)
5674 zfs_ioc_vec_t
*vec
= &zfs_ioc_vec
[ioc
- ZFS_IOC_FIRST
];
5676 ASSERT3U(ioc
, >=, ZFS_IOC_FIRST
);
5677 ASSERT3U(ioc
, <, ZFS_IOC_LAST
);
5678 ASSERT3P(vec
->zvec_legacy_func
, ==, NULL
);
5679 ASSERT3P(vec
->zvec_func
, ==, NULL
);
5681 /* if we are logging, the name must be valid */
5682 ASSERT(!allow_log
|| namecheck
!= NO_NAME
);
5684 vec
->zvec_name
= name
;
5685 vec
->zvec_func
= func
;
5686 vec
->zvec_secpolicy
= secpolicy
;
5687 vec
->zvec_namecheck
= namecheck
;
5688 vec
->zvec_pool_check
= pool_check
;
5689 vec
->zvec_smush_outnvlist
= smush_outnvlist
;
5690 vec
->zvec_allow_log
= allow_log
;
5694 zfs_ioctl_register_pool(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5695 zfs_secpolicy_func_t
*secpolicy
, boolean_t log_history
,
5696 zfs_ioc_poolcheck_t pool_check
)
5698 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5699 POOL_NAME
, log_history
, pool_check
);
5703 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5704 zfs_secpolicy_func_t
*secpolicy
, zfs_ioc_poolcheck_t pool_check
)
5706 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5707 DATASET_NAME
, B_FALSE
, pool_check
);
5711 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
)
5713 zfs_ioctl_register_legacy(ioc
, func
, zfs_secpolicy_config
,
5714 POOL_NAME
, B_TRUE
, POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
5718 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5719 zfs_secpolicy_func_t
*secpolicy
)
5721 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5722 NO_NAME
, B_FALSE
, POOL_CHECK_NONE
);
5726 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc
,
5727 zfs_ioc_legacy_func_t
*func
, zfs_secpolicy_func_t
*secpolicy
)
5729 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5730 DATASET_NAME
, B_FALSE
, POOL_CHECK_SUSPENDED
);
5734 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
)
5736 zfs_ioctl_register_dataset_read_secpolicy(ioc
, func
,
5737 zfs_secpolicy_read
);
5741 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5742 zfs_secpolicy_func_t
*secpolicy
)
5744 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5745 DATASET_NAME
, B_TRUE
, POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
5749 zfs_ioctl_init(void)
5751 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT
,
5752 zfs_ioc_snapshot
, zfs_secpolicy_snapshot
, POOL_NAME
,
5753 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5755 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY
,
5756 zfs_ioc_log_history
, zfs_secpolicy_log_history
, NO_NAME
,
5757 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_FALSE
, B_FALSE
);
5759 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS
,
5760 zfs_ioc_space_snaps
, zfs_secpolicy_read
, DATASET_NAME
,
5761 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
5763 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW
,
5764 zfs_ioc_send_new
, zfs_secpolicy_send_new
, DATASET_NAME
,
5765 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
5767 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE
,
5768 zfs_ioc_send_space
, zfs_secpolicy_read
, DATASET_NAME
,
5769 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
5771 zfs_ioctl_register("create", ZFS_IOC_CREATE
,
5772 zfs_ioc_create
, zfs_secpolicy_create_clone
, DATASET_NAME
,
5773 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5775 zfs_ioctl_register("clone", ZFS_IOC_CLONE
,
5776 zfs_ioc_clone
, zfs_secpolicy_create_clone
, DATASET_NAME
,
5777 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5779 zfs_ioctl_register("remap", ZFS_IOC_REMAP
,
5780 zfs_ioc_remap
, zfs_secpolicy_remap
, DATASET_NAME
,
5781 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_FALSE
, B_TRUE
);
5783 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS
,
5784 zfs_ioc_destroy_snaps
, zfs_secpolicy_destroy_snaps
, POOL_NAME
,
5785 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5787 zfs_ioctl_register("hold", ZFS_IOC_HOLD
,
5788 zfs_ioc_hold
, zfs_secpolicy_hold
, POOL_NAME
,
5789 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5790 zfs_ioctl_register("release", ZFS_IOC_RELEASE
,
5791 zfs_ioc_release
, zfs_secpolicy_release
, POOL_NAME
,
5792 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5794 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS
,
5795 zfs_ioc_get_holds
, zfs_secpolicy_read
, DATASET_NAME
,
5796 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
5798 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK
,
5799 zfs_ioc_rollback
, zfs_secpolicy_rollback
, DATASET_NAME
,
5800 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_FALSE
, B_TRUE
);
5802 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK
,
5803 zfs_ioc_bookmark
, zfs_secpolicy_bookmark
, POOL_NAME
,
5804 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5806 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS
,
5807 zfs_ioc_get_bookmarks
, zfs_secpolicy_read
, DATASET_NAME
,
5808 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
5810 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS
,
5811 zfs_ioc_destroy_bookmarks
, zfs_secpolicy_destroy_bookmarks
,
5813 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5815 zfs_ioctl_register("channel_program", ZFS_IOC_CHANNEL_PROGRAM
,
5816 zfs_ioc_channel_program
, zfs_secpolicy_config
,
5817 POOL_NAME
, POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
,
5820 zfs_ioctl_register("zpool_checkpoint", ZFS_IOC_POOL_CHECKPOINT
,
5821 zfs_ioc_pool_checkpoint
, zfs_secpolicy_config
, POOL_NAME
,
5822 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5824 zfs_ioctl_register("zpool_discard_checkpoint",
5825 ZFS_IOC_POOL_DISCARD_CHECKPOINT
, zfs_ioc_pool_discard_checkpoint
,
5826 zfs_secpolicy_config
, POOL_NAME
,
5827 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5829 zfs_ioctl_register("initialize", ZFS_IOC_POOL_INITIALIZE
,
5830 zfs_ioc_pool_initialize
, zfs_secpolicy_config
, POOL_NAME
,
5831 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5833 /* IOCTLS that use the legacy function signature */
5835 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE
, zfs_ioc_pool_freeze
,
5836 zfs_secpolicy_config
, NO_NAME
, B_FALSE
, POOL_CHECK_READONLY
);
5838 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE
, zfs_ioc_pool_create
,
5839 zfs_secpolicy_config
, B_TRUE
, POOL_CHECK_NONE
);
5840 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN
,
5842 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE
,
5843 zfs_ioc_pool_upgrade
);
5844 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD
,
5846 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE
,
5847 zfs_ioc_vdev_remove
);
5848 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE
,
5849 zfs_ioc_vdev_set_state
);
5850 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH
,
5851 zfs_ioc_vdev_attach
);
5852 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH
,
5853 zfs_ioc_vdev_detach
);
5854 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH
,
5855 zfs_ioc_vdev_setpath
);
5856 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU
,
5857 zfs_ioc_vdev_setfru
);
5858 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS
,
5859 zfs_ioc_pool_set_props
);
5860 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT
,
5861 zfs_ioc_vdev_split
);
5862 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID
,
5863 zfs_ioc_pool_reguid
);
5865 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS
,
5866 zfs_ioc_pool_configs
, zfs_secpolicy_none
);
5867 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT
,
5868 zfs_ioc_pool_tryimport
, zfs_secpolicy_config
);
5869 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT
,
5870 zfs_ioc_inject_fault
, zfs_secpolicy_inject
);
5871 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT
,
5872 zfs_ioc_clear_fault
, zfs_secpolicy_inject
);
5873 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT
,
5874 zfs_ioc_inject_list_next
, zfs_secpolicy_inject
);
5877 * pool destroy, and export don't log the history as part of
5878 * zfsdev_ioctl, but rather zfs_ioc_pool_export
5879 * does the logging of those commands.
5881 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY
, zfs_ioc_pool_destroy
,
5882 zfs_secpolicy_config
, B_FALSE
, POOL_CHECK_NONE
);
5883 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT
, zfs_ioc_pool_export
,
5884 zfs_secpolicy_config
, B_FALSE
, POOL_CHECK_NONE
);
5886 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS
, zfs_ioc_pool_stats
,
5887 zfs_secpolicy_read
, B_FALSE
, POOL_CHECK_NONE
);
5888 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS
, zfs_ioc_pool_get_props
,
5889 zfs_secpolicy_read
, B_FALSE
, POOL_CHECK_NONE
);
5891 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG
, zfs_ioc_error_log
,
5892 zfs_secpolicy_inject
, B_FALSE
, POOL_CHECK_SUSPENDED
);
5893 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME
,
5894 zfs_ioc_dsobj_to_dsname
,
5895 zfs_secpolicy_diff
, B_FALSE
, POOL_CHECK_SUSPENDED
);
5896 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY
,
5897 zfs_ioc_pool_get_history
,
5898 zfs_secpolicy_config
, B_FALSE
, POOL_CHECK_SUSPENDED
);
5900 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT
, zfs_ioc_pool_import
,
5901 zfs_secpolicy_config
, B_TRUE
, POOL_CHECK_NONE
);
5903 zfs_ioctl_register_pool(ZFS_IOC_CLEAR
, zfs_ioc_clear
,
5904 zfs_secpolicy_config
, B_TRUE
, POOL_CHECK_READONLY
);
5905 zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN
, zfs_ioc_pool_reopen
,
5906 zfs_secpolicy_config
, B_TRUE
, POOL_CHECK_SUSPENDED
);
5908 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN
,
5909 zfs_ioc_space_written
);
5910 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS
,
5911 zfs_ioc_objset_recvd_props
);
5912 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ
,
5914 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL
,
5916 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS
,
5917 zfs_ioc_objset_stats
);
5918 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS
,
5919 zfs_ioc_objset_zplprops
);
5920 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT
,
5921 zfs_ioc_dataset_list_next
);
5922 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT
,
5923 zfs_ioc_snapshot_list_next
);
5924 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS
,
5925 zfs_ioc_send_progress
);
5927 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF
,
5928 zfs_ioc_diff
, zfs_secpolicy_diff
);
5929 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS
,
5930 zfs_ioc_obj_to_stats
, zfs_secpolicy_diff
);
5931 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH
,
5932 zfs_ioc_obj_to_path
, zfs_secpolicy_diff
);
5933 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE
,
5934 zfs_ioc_userspace_one
, zfs_secpolicy_userspace_one
);
5935 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY
,
5936 zfs_ioc_userspace_many
, zfs_secpolicy_userspace_many
);
5937 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND
,
5938 zfs_ioc_send
, zfs_secpolicy_send
);
5940 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP
, zfs_ioc_set_prop
,
5941 zfs_secpolicy_none
);
5942 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY
, zfs_ioc_destroy
,
5943 zfs_secpolicy_destroy
);
5944 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME
, zfs_ioc_rename
,
5945 zfs_secpolicy_rename
);
5946 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV
, zfs_ioc_recv
,
5947 zfs_secpolicy_recv
);
5948 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE
, zfs_ioc_promote
,
5949 zfs_secpolicy_promote
);
5950 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP
,
5951 zfs_ioc_inherit_prop
, zfs_secpolicy_inherit_prop
);
5952 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL
, zfs_ioc_set_fsacl
,
5953 zfs_secpolicy_set_fsacl
);
5955 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE
, zfs_ioc_share
,
5956 zfs_secpolicy_share
, POOL_CHECK_NONE
);
5957 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL
, zfs_ioc_smb_acl
,
5958 zfs_secpolicy_smb_acl
, POOL_CHECK_NONE
);
5959 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE
,
5960 zfs_ioc_userspace_upgrade
, zfs_secpolicy_userspace_upgrade
,
5961 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
5962 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT
,
5963 zfs_ioc_tmp_snapshot
, zfs_secpolicy_tmp_snapshot
,
5964 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
5968 pool_status_check(const char *name
, zfs_ioc_namecheck_t type
,
5969 zfs_ioc_poolcheck_t check
)
5974 ASSERT(type
== POOL_NAME
|| type
== DATASET_NAME
);
5976 if (check
& POOL_CHECK_NONE
)
5979 error
= spa_open(name
, &spa
, FTAG
);
5981 if ((check
& POOL_CHECK_SUSPENDED
) && spa_suspended(spa
))
5982 error
= SET_ERROR(EAGAIN
);
5983 else if ((check
& POOL_CHECK_READONLY
) && !spa_writeable(spa
))
5984 error
= SET_ERROR(EROFS
);
5985 spa_close(spa
, FTAG
);
5991 * Find a free minor number.
5994 zfsdev_minor_alloc(void)
5996 static minor_t last_minor
;
5999 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
6001 for (m
= last_minor
+ 1; m
!= last_minor
; m
++) {
6002 if (m
> ZFSDEV_MAX_MINOR
)
6004 if (ddi_get_soft_state(zfsdev_state
, m
) == NULL
) {
6014 zfs_ctldev_init(dev_t
*devp
)
6017 zfs_soft_state_t
*zs
;
6019 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
6020 ASSERT(getminor(*devp
) == 0);
6022 minor
= zfsdev_minor_alloc();
6024 return (SET_ERROR(ENXIO
));
6026 if (ddi_soft_state_zalloc(zfsdev_state
, minor
) != DDI_SUCCESS
)
6027 return (SET_ERROR(EAGAIN
));
6029 *devp
= makedevice(getemajor(*devp
), minor
);
6031 zs
= ddi_get_soft_state(zfsdev_state
, minor
);
6032 zs
->zss_type
= ZSST_CTLDEV
;
6033 zfs_onexit_init((zfs_onexit_t
**)&zs
->zss_data
);
6039 zfs_ctldev_destroy(zfs_onexit_t
*zo
, minor_t minor
)
6041 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
6043 zfs_onexit_destroy(zo
);
6044 ddi_soft_state_free(zfsdev_state
, minor
);
6048 zfsdev_get_soft_state(minor_t minor
, enum zfs_soft_state_type which
)
6050 zfs_soft_state_t
*zp
;
6052 zp
= ddi_get_soft_state(zfsdev_state
, minor
);
6053 if (zp
== NULL
|| zp
->zss_type
!= which
)
6056 return (zp
->zss_data
);
6060 zfsdev_open(dev_t
*devp
, int flag
, int otyp
, cred_t
*cr
)
6064 if (getminor(*devp
) != 0)
6065 return (zvol_open(devp
, flag
, otyp
, cr
));
6067 /* This is the control device. Allocate a new minor if requested. */
6069 mutex_enter(&zfsdev_state_lock
);
6070 error
= zfs_ctldev_init(devp
);
6071 mutex_exit(&zfsdev_state_lock
);
6078 zfsdev_close(dev_t dev
, int flag
, int otyp
, cred_t
*cr
)
6081 minor_t minor
= getminor(dev
);
6086 mutex_enter(&zfsdev_state_lock
);
6087 zo
= zfsdev_get_soft_state(minor
, ZSST_CTLDEV
);
6089 mutex_exit(&zfsdev_state_lock
);
6090 return (zvol_close(dev
, flag
, otyp
, cr
));
6092 zfs_ctldev_destroy(zo
, minor
);
6093 mutex_exit(&zfsdev_state_lock
);
6099 zfsdev_ioctl(dev_t dev
, int cmd
, intptr_t arg
, int flag
, cred_t
*cr
, int *rvalp
)
6104 minor_t minor
= getminor(dev
);
6105 const zfs_ioc_vec_t
*vec
;
6106 char *saved_poolname
= NULL
;
6107 nvlist_t
*innvl
= NULL
;
6110 zfsdev_get_soft_state(minor
, ZSST_CTLDEV
) == NULL
)
6111 return (zvol_ioctl(dev
, cmd
, arg
, flag
, cr
, rvalp
));
6113 vecnum
= cmd
- ZFS_IOC_FIRST
;
6114 ASSERT3U(getmajor(dev
), ==, ddi_driver_major(zfs_dip
));
6116 if (vecnum
>= sizeof (zfs_ioc_vec
) / sizeof (zfs_ioc_vec
[0]))
6117 return (SET_ERROR(EINVAL
));
6118 vec
= &zfs_ioc_vec
[vecnum
];
6120 zc
= kmem_zalloc(sizeof (zfs_cmd_t
), KM_SLEEP
);
6122 error
= ddi_copyin((void *)arg
, zc
, sizeof (zfs_cmd_t
), flag
);
6124 error
= SET_ERROR(EFAULT
);
6128 zc
->zc_iflags
= flag
& FKIOCTL
;
6129 if (zc
->zc_nvlist_src_size
!= 0) {
6130 error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
6131 zc
->zc_iflags
, &innvl
);
6137 * Ensure that all pool/dataset names are valid before we pass down to
6140 zc
->zc_name
[sizeof (zc
->zc_name
) - 1] = '\0';
6141 switch (vec
->zvec_namecheck
) {
6143 if (pool_namecheck(zc
->zc_name
, NULL
, NULL
) != 0)
6144 error
= SET_ERROR(EINVAL
);
6146 error
= pool_status_check(zc
->zc_name
,
6147 vec
->zvec_namecheck
, vec
->zvec_pool_check
);
6151 if (dataset_namecheck(zc
->zc_name
, NULL
, NULL
) != 0)
6152 error
= SET_ERROR(EINVAL
);
6154 error
= pool_status_check(zc
->zc_name
,
6155 vec
->zvec_namecheck
, vec
->zvec_pool_check
);
6164 error
= vec
->zvec_secpolicy(zc
, innvl
, cr
);
6169 /* legacy ioctls can modify zc_name */
6170 len
= strcspn(zc
->zc_name
, "/@#") + 1;
6171 saved_poolname
= kmem_alloc(len
, KM_SLEEP
);
6172 (void) strlcpy(saved_poolname
, zc
->zc_name
, len
);
6174 if (vec
->zvec_func
!= NULL
) {
6178 nvlist_t
*lognv
= NULL
;
6180 ASSERT(vec
->zvec_legacy_func
== NULL
);
6183 * Add the innvl to the lognv before calling the func,
6184 * in case the func changes the innvl.
6186 if (vec
->zvec_allow_log
) {
6187 lognv
= fnvlist_alloc();
6188 fnvlist_add_string(lognv
, ZPOOL_HIST_IOCTL
,
6190 if (!nvlist_empty(innvl
)) {
6191 fnvlist_add_nvlist(lognv
, ZPOOL_HIST_INPUT_NVL
,
6196 outnvl
= fnvlist_alloc();
6197 error
= vec
->zvec_func(zc
->zc_name
, innvl
, outnvl
);
6200 * Some commands can partially execute, modfiy state, and still
6201 * return an error. In these cases, attempt to record what
6205 (cmd
== ZFS_IOC_CHANNEL_PROGRAM
&& error
!= EINVAL
)) &&
6206 vec
->zvec_allow_log
&&
6207 spa_open(zc
->zc_name
, &spa
, FTAG
) == 0) {
6208 if (!nvlist_empty(outnvl
)) {
6209 fnvlist_add_nvlist(lognv
, ZPOOL_HIST_OUTPUT_NVL
,
6213 fnvlist_add_int64(lognv
, ZPOOL_HIST_ERRNO
,
6216 (void) spa_history_log_nvl(spa
, lognv
);
6217 spa_close(spa
, FTAG
);
6219 fnvlist_free(lognv
);
6221 if (!nvlist_empty(outnvl
) || zc
->zc_nvlist_dst_size
!= 0) {
6223 if (vec
->zvec_smush_outnvlist
) {
6224 smusherror
= nvlist_smush(outnvl
,
6225 zc
->zc_nvlist_dst_size
);
6227 if (smusherror
== 0)
6228 puterror
= put_nvlist(zc
, outnvl
);
6234 nvlist_free(outnvl
);
6236 error
= vec
->zvec_legacy_func(zc
);
6241 rc
= ddi_copyout(zc
, (void *)arg
, sizeof (zfs_cmd_t
), flag
);
6242 if (error
== 0 && rc
!= 0)
6243 error
= SET_ERROR(EFAULT
);
6244 if (error
== 0 && vec
->zvec_allow_log
) {
6245 char *s
= tsd_get(zfs_allow_log_key
);
6248 (void) tsd_set(zfs_allow_log_key
, saved_poolname
);
6250 if (saved_poolname
!= NULL
)
6251 strfree(saved_poolname
);
6254 kmem_free(zc
, sizeof (zfs_cmd_t
));
6259 zfs_attach(dev_info_t
*dip
, ddi_attach_cmd_t cmd
)
6261 if (cmd
!= DDI_ATTACH
)
6262 return (DDI_FAILURE
);
6264 if (ddi_create_minor_node(dip
, "zfs", S_IFCHR
, 0,
6265 DDI_PSEUDO
, 0) == DDI_FAILURE
)
6266 return (DDI_FAILURE
);
6270 ddi_report_dev(dip
);
6272 return (DDI_SUCCESS
);
6276 zfs_detach(dev_info_t
*dip
, ddi_detach_cmd_t cmd
)
6278 if (spa_busy() || zfs_busy() || zvol_busy())
6279 return (DDI_FAILURE
);
6281 if (cmd
!= DDI_DETACH
)
6282 return (DDI_FAILURE
);
6286 ddi_prop_remove_all(dip
);
6287 ddi_remove_minor_node(dip
, NULL
);
6289 return (DDI_SUCCESS
);
6294 zfs_info(dev_info_t
*dip
, ddi_info_cmd_t infocmd
, void *arg
, void **result
)
6297 case DDI_INFO_DEVT2DEVINFO
:
6299 return (DDI_SUCCESS
);
6301 case DDI_INFO_DEVT2INSTANCE
:
6303 return (DDI_SUCCESS
);
6306 return (DDI_FAILURE
);
6310 * OK, so this is a little weird.
6312 * /dev/zfs is the control node, i.e. minor 0.
6313 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
6315 * /dev/zfs has basically nothing to do except serve up ioctls,
6316 * so most of the standard driver entry points are in zvol.c.
6318 static struct cb_ops zfs_cb_ops
= {
6319 zfsdev_open
, /* open */
6320 zfsdev_close
, /* close */
6321 zvol_strategy
, /* strategy */
6323 zvol_dump
, /* dump */
6324 zvol_read
, /* read */
6325 zvol_write
, /* write */
6326 zfsdev_ioctl
, /* ioctl */
6330 nochpoll
, /* poll */
6331 ddi_prop_op
, /* prop_op */
6332 NULL
, /* streamtab */
6333 D_NEW
| D_MP
| D_64BIT
, /* Driver compatibility flag */
6334 CB_REV
, /* version */
6335 nodev
, /* async read */
6336 nodev
, /* async write */
6339 static struct dev_ops zfs_dev_ops
= {
6340 DEVO_REV
, /* version */
6342 zfs_info
, /* info */
6343 nulldev
, /* identify */
6344 nulldev
, /* probe */
6345 zfs_attach
, /* attach */
6346 zfs_detach
, /* detach */
6348 &zfs_cb_ops
, /* driver operations */
6349 NULL
, /* no bus operations */
6351 ddi_quiesce_not_needed
, /* quiesce */
6354 static struct modldrv zfs_modldrv
= {
6360 static struct modlinkage modlinkage
= {
6362 (void *)&zfs_modlfs
,
6363 (void *)&zfs_modldrv
,
6368 zfs_allow_log_destroy(void *arg
)
6370 char *poolname
= arg
;
6379 spa_init(FREAD
| FWRITE
);
6384 if ((error
= mod_install(&modlinkage
)) != 0) {
6391 tsd_create(&zfs_fsyncer_key
, NULL
);
6392 tsd_create(&rrw_tsd_key
, rrw_tsd_destroy
);
6393 tsd_create(&zfs_allow_log_key
, zfs_allow_log_destroy
);
6395 error
= ldi_ident_from_mod(&modlinkage
, &zfs_li
);
6397 mutex_init(&zfs_share_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
6407 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled
)
6408 return (SET_ERROR(EBUSY
));
6410 if ((error
= mod_remove(&modlinkage
)) != 0)
6416 if (zfs_nfsshare_inited
)
6417 (void) ddi_modclose(nfs_mod
);
6418 if (zfs_smbshare_inited
)
6419 (void) ddi_modclose(smbsrv_mod
);
6420 if (zfs_nfsshare_inited
|| zfs_smbshare_inited
)
6421 (void) ddi_modclose(sharefs_mod
);
6423 tsd_destroy(&zfs_fsyncer_key
);
6424 ldi_ident_release(zfs_li
);
6426 mutex_destroy(&zfs_share_lock
);
6432 _info(struct modinfo
*modinfop
)
6434 return (mod_info(&modlinkage
, modinfop
));