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]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/types.h>
27 #include <sys/param.h>
28 #include <sys/errno.h>
31 #include <sys/modctl.h>
36 #include <sys/cmn_err.h>
38 #include <sys/zfs_ioctl.h>
39 #include <sys/zfs_znode.h>
42 #include <sys/spa_impl.h>
44 #include <sys/vdev_impl.h>
46 #include <sys/dsl_dir.h>
47 #include <sys/dsl_dataset.h>
48 #include <sys/dsl_prop.h>
49 #include <sys/dsl_deleg.h>
50 #include <sys/dmu_objset.h>
52 #include <sys/sunddi.h>
53 #include <sys/sunldi.h>
54 #include <sys/policy.h>
56 #include <sys/nvpair.h>
57 #include <sys/pathname.h>
58 #include <sys/mount.h>
60 #include <sys/fs/zfs.h>
61 #include <sys/zfs_ctldir.h>
62 #include <sys/zfs_dir.h>
64 #include <sharefs/share.h>
65 #include <sys/dmu_objset.h>
67 #include "zfs_namecheck.h"
69 #include "zfs_deleg.h"
71 extern struct modlfs zfs_modlfs
;
73 extern void zfs_init(void);
74 extern void zfs_fini(void);
76 ldi_ident_t zfs_li
= NULL
;
79 typedef int zfs_ioc_func_t(zfs_cmd_t
*);
80 typedef int zfs_secpolicy_func_t(zfs_cmd_t
*, cred_t
*);
82 typedef struct zfs_ioc_vec
{
83 zfs_ioc_func_t
*zvec_func
;
84 zfs_secpolicy_func_t
*zvec_secpolicy
;
90 boolean_t zvec_his_log
;
93 static void clear_props(char *dataset
, nvlist_t
*props
, nvlist_t
*newprops
);
94 static int zfs_fill_zplprops_root(uint64_t, nvlist_t
*, nvlist_t
*,
96 int zfs_set_prop_nvlist(const char *, nvlist_t
*);
98 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
100 __dprintf(const char *file
, const char *func
, int line
, const char *fmt
, ...)
107 * Get rid of annoying "../common/" prefix to filename.
109 newfile
= strrchr(file
, '/');
110 if (newfile
!= NULL
) {
111 newfile
= newfile
+ 1; /* Get rid of leading / */
117 (void) vsnprintf(buf
, sizeof (buf
), fmt
, adx
);
121 * To get this data, use the zfs-dprintf probe as so:
122 * dtrace -q -n 'zfs-dprintf \
123 * /stringof(arg0) == "dbuf.c"/ \
124 * {printf("%s: %s", stringof(arg1), stringof(arg3))}'
126 * arg1 = function name
130 DTRACE_PROBE4(zfs__dprintf
,
131 char *, newfile
, char *, func
, int, line
, char *, buf
);
135 history_str_free(char *buf
)
137 kmem_free(buf
, HIS_MAX_RECORD_LEN
);
141 history_str_get(zfs_cmd_t
*zc
)
145 if (zc
->zc_history
== NULL
)
148 buf
= kmem_alloc(HIS_MAX_RECORD_LEN
, KM_SLEEP
);
149 if (copyinstr((void *)(uintptr_t)zc
->zc_history
,
150 buf
, HIS_MAX_RECORD_LEN
, NULL
) != 0) {
151 history_str_free(buf
);
155 buf
[HIS_MAX_RECORD_LEN
-1] = '\0';
161 * Check to see if the named dataset is currently defined as bootable
164 zfs_is_bootfs(const char *name
)
167 boolean_t ret
= B_FALSE
;
169 if (spa_open(name
, &spa
, FTAG
) == 0) {
170 if (spa
->spa_bootfs
) {
173 if (dmu_objset_open(name
, DMU_OST_ZFS
,
174 DS_MODE_USER
| DS_MODE_READONLY
, &os
) == 0) {
175 ret
= (dmu_objset_id(os
) == spa
->spa_bootfs
);
176 dmu_objset_close(os
);
179 spa_close(spa
, FTAG
);
185 * zfs_earlier_version
187 * Return non-zero if the spa version is less than requested version.
190 zfs_earlier_version(const char *name
, int version
)
194 if (spa_open(name
, &spa
, FTAG
) == 0) {
195 if (spa_version(spa
) < version
) {
196 spa_close(spa
, FTAG
);
199 spa_close(spa
, FTAG
);
205 * zpl_earlier_version
207 * Return TRUE if the ZPL version is less than requested version.
210 zpl_earlier_version(const char *name
, int version
)
213 boolean_t rc
= B_TRUE
;
215 if (dmu_objset_open(name
, DMU_OST_ANY
,
216 DS_MODE_USER
| DS_MODE_READONLY
, &os
) == 0) {
219 if (zfs_get_zplprop(os
, ZFS_PROP_VERSION
, &zplversion
) == 0)
220 rc
= zplversion
< version
;
221 dmu_objset_close(os
);
227 zfs_log_history(zfs_cmd_t
*zc
)
232 if ((buf
= history_str_get(zc
)) == NULL
)
235 if (spa_open(zc
->zc_name
, &spa
, FTAG
) == 0) {
236 if (spa_version(spa
) >= SPA_VERSION_ZPOOL_HISTORY
)
237 (void) spa_history_log(spa
, buf
, LOG_CMD_NORMAL
);
238 spa_close(spa
, FTAG
);
240 history_str_free(buf
);
244 * Policy for top-level read operations (list pools). Requires no privileges,
245 * and can be used in the local zone, as there is no associated dataset.
249 zfs_secpolicy_none(zfs_cmd_t
*zc
, cred_t
*cr
)
255 * Policy for dataset read operations (list children, get statistics). Requires
256 * no privileges, but must be visible in the local zone.
260 zfs_secpolicy_read(zfs_cmd_t
*zc
, cred_t
*cr
)
262 if (INGLOBALZONE(curproc
) ||
263 zone_dataset_visible(zc
->zc_name
, NULL
))
270 zfs_dozonecheck(const char *dataset
, cred_t
*cr
)
276 * The dataset must be visible by this zone -- check this first
277 * so they don't see EPERM on something they shouldn't know about.
279 if (!INGLOBALZONE(curproc
) &&
280 !zone_dataset_visible(dataset
, &writable
))
283 if (dsl_prop_get_integer(dataset
, "zoned", &zoned
, NULL
))
286 if (INGLOBALZONE(curproc
)) {
288 * If the fs is zoned, only root can access it from the
291 if (secpolicy_zfs(cr
) && zoned
)
295 * If we are in a local zone, the 'zoned' property must be set.
300 /* must be writable by this zone */
308 zfs_secpolicy_write_perms(const char *name
, const char *perm
, cred_t
*cr
)
312 error
= zfs_dozonecheck(name
, cr
);
314 error
= secpolicy_zfs(cr
);
316 error
= dsl_deleg_access(name
, perm
, cr
);
322 zfs_secpolicy_setprop(const char *name
, zfs_prop_t prop
, cred_t
*cr
)
325 * Check permissions for special properties.
330 * Disallow setting of 'zoned' from within a local zone.
332 if (!INGLOBALZONE(curproc
))
337 if (!INGLOBALZONE(curproc
)) {
339 char setpoint
[MAXNAMELEN
];
341 * Unprivileged users are allowed to modify the
342 * quota on things *under* (ie. contained by)
343 * the thing they own.
345 if (dsl_prop_get_integer(name
, "zoned", &zoned
,
348 if (!zoned
|| strlen(name
) <= strlen(setpoint
))
354 return (zfs_secpolicy_write_perms(name
, zfs_prop_to_name(prop
), cr
));
358 zfs_secpolicy_fsacl(zfs_cmd_t
*zc
, cred_t
*cr
)
362 error
= zfs_dozonecheck(zc
->zc_name
, cr
);
367 * permission to set permissions will be evaluated later in
368 * dsl_deleg_can_allow()
374 zfs_secpolicy_rollback(zfs_cmd_t
*zc
, cred_t
*cr
)
377 error
= zfs_secpolicy_write_perms(zc
->zc_name
,
378 ZFS_DELEG_PERM_ROLLBACK
, cr
);
380 error
= zfs_secpolicy_write_perms(zc
->zc_name
,
381 ZFS_DELEG_PERM_MOUNT
, cr
);
386 zfs_secpolicy_send(zfs_cmd_t
*zc
, cred_t
*cr
)
388 return (zfs_secpolicy_write_perms(zc
->zc_name
,
389 ZFS_DELEG_PERM_SEND
, cr
));
393 zfs_secpolicy_share(zfs_cmd_t
*zc
, cred_t
*cr
)
395 if (!INGLOBALZONE(curproc
))
398 if (secpolicy_nfs(cr
) == 0) {
404 if ((error
= lookupname(zc
->zc_value
, UIO_SYSSPACE
,
405 NO_FOLLOW
, NULL
, &vp
)) != 0)
408 /* Now make sure mntpnt and dataset are ZFS */
410 if (vp
->v_vfsp
->vfs_fstype
!= zfsfstype
||
411 (strcmp((char *)refstr_value(vp
->v_vfsp
->vfs_resource
),
412 zc
->zc_name
) != 0)) {
418 return (dsl_deleg_access(zc
->zc_name
,
419 ZFS_DELEG_PERM_SHARE
, cr
));
424 zfs_get_parent(const char *datasetname
, char *parent
, int parentsize
)
429 * Remove the @bla or /bla from the end of the name to get the parent.
431 (void) strncpy(parent
, datasetname
, parentsize
);
432 cp
= strrchr(parent
, '@');
436 cp
= strrchr(parent
, '/');
446 zfs_secpolicy_destroy_perms(const char *name
, cred_t
*cr
)
450 if ((error
= zfs_secpolicy_write_perms(name
,
451 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
454 return (zfs_secpolicy_write_perms(name
, ZFS_DELEG_PERM_DESTROY
, cr
));
458 zfs_secpolicy_destroy(zfs_cmd_t
*zc
, cred_t
*cr
)
460 return (zfs_secpolicy_destroy_perms(zc
->zc_name
, cr
));
464 * Must have sys_config privilege to check the iscsi permission
468 zfs_secpolicy_iscsi(zfs_cmd_t
*zc
, cred_t
*cr
)
470 return (secpolicy_zfs(cr
));
474 zfs_secpolicy_rename_perms(const char *from
, const char *to
, cred_t
*cr
)
476 char parentname
[MAXNAMELEN
];
479 if ((error
= zfs_secpolicy_write_perms(from
,
480 ZFS_DELEG_PERM_RENAME
, cr
)) != 0)
483 if ((error
= zfs_secpolicy_write_perms(from
,
484 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
487 if ((error
= zfs_get_parent(to
, parentname
,
488 sizeof (parentname
))) != 0)
491 if ((error
= zfs_secpolicy_write_perms(parentname
,
492 ZFS_DELEG_PERM_CREATE
, cr
)) != 0)
495 if ((error
= zfs_secpolicy_write_perms(parentname
,
496 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
503 zfs_secpolicy_rename(zfs_cmd_t
*zc
, cred_t
*cr
)
505 return (zfs_secpolicy_rename_perms(zc
->zc_name
, zc
->zc_value
, cr
));
509 zfs_secpolicy_promote(zfs_cmd_t
*zc
, cred_t
*cr
)
511 char parentname
[MAXNAMELEN
];
515 error
= zfs_secpolicy_write_perms(zc
->zc_name
,
516 ZFS_DELEG_PERM_PROMOTE
, cr
);
520 error
= dmu_objset_open(zc
->zc_name
, DMU_OST_ANY
,
521 DS_MODE_USER
| DS_MODE_READONLY
, &clone
);
524 dsl_dataset_t
*pclone
= NULL
;
526 dd
= clone
->os
->os_dsl_dataset
->ds_dir
;
528 rw_enter(&dd
->dd_pool
->dp_config_rwlock
, RW_READER
);
529 error
= dsl_dataset_hold_obj(dd
->dd_pool
,
530 dd
->dd_phys
->dd_origin_obj
, FTAG
, &pclone
);
531 rw_exit(&dd
->dd_pool
->dp_config_rwlock
);
533 dmu_objset_close(clone
);
537 error
= zfs_secpolicy_write_perms(zc
->zc_name
,
538 ZFS_DELEG_PERM_MOUNT
, cr
);
540 dsl_dataset_name(pclone
, parentname
);
541 dmu_objset_close(clone
);
542 dsl_dataset_rele(pclone
, FTAG
);
544 error
= zfs_secpolicy_write_perms(parentname
,
545 ZFS_DELEG_PERM_PROMOTE
, cr
);
551 zfs_secpolicy_receive(zfs_cmd_t
*zc
, cred_t
*cr
)
555 if ((error
= zfs_secpolicy_write_perms(zc
->zc_name
,
556 ZFS_DELEG_PERM_RECEIVE
, cr
)) != 0)
559 if ((error
= zfs_secpolicy_write_perms(zc
->zc_name
,
560 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
563 return (zfs_secpolicy_write_perms(zc
->zc_name
,
564 ZFS_DELEG_PERM_CREATE
, cr
));
568 zfs_secpolicy_snapshot_perms(const char *name
, cred_t
*cr
)
572 if ((error
= zfs_secpolicy_write_perms(name
,
573 ZFS_DELEG_PERM_SNAPSHOT
, cr
)) != 0)
576 error
= zfs_secpolicy_write_perms(name
,
577 ZFS_DELEG_PERM_MOUNT
, cr
);
583 zfs_secpolicy_snapshot(zfs_cmd_t
*zc
, cred_t
*cr
)
586 return (zfs_secpolicy_snapshot_perms(zc
->zc_name
, cr
));
590 zfs_secpolicy_create(zfs_cmd_t
*zc
, cred_t
*cr
)
592 char parentname
[MAXNAMELEN
];
595 if ((error
= zfs_get_parent(zc
->zc_name
, parentname
,
596 sizeof (parentname
))) != 0)
599 if (zc
->zc_value
[0] != '\0') {
600 if ((error
= zfs_secpolicy_write_perms(zc
->zc_value
,
601 ZFS_DELEG_PERM_CLONE
, cr
)) != 0)
605 if ((error
= zfs_secpolicy_write_perms(parentname
,
606 ZFS_DELEG_PERM_CREATE
, cr
)) != 0)
609 error
= zfs_secpolicy_write_perms(parentname
,
610 ZFS_DELEG_PERM_MOUNT
, cr
);
616 zfs_secpolicy_umount(zfs_cmd_t
*zc
, cred_t
*cr
)
620 error
= secpolicy_fs_unmount(cr
, NULL
);
622 error
= dsl_deleg_access(zc
->zc_name
, ZFS_DELEG_PERM_MOUNT
, cr
);
628 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
629 * SYS_CONFIG privilege, which is not available in a local zone.
633 zfs_secpolicy_config(zfs_cmd_t
*zc
, cred_t
*cr
)
635 if (secpolicy_sys_config(cr
, B_FALSE
) != 0)
642 * Just like zfs_secpolicy_config, except that we will check for
643 * mount permission on the dataset for permission to create/remove
647 zfs_secpolicy_minor(zfs_cmd_t
*zc
, cred_t
*cr
)
649 if (secpolicy_sys_config(cr
, B_FALSE
) != 0) {
650 return (dsl_deleg_access(zc
->zc_name
,
651 ZFS_DELEG_PERM_MOUNT
, cr
));
658 * Policy for fault injection. Requires all privileges.
662 zfs_secpolicy_inject(zfs_cmd_t
*zc
, cred_t
*cr
)
664 return (secpolicy_zinject(cr
));
668 zfs_secpolicy_inherit(zfs_cmd_t
*zc
, cred_t
*cr
)
670 zfs_prop_t prop
= zfs_name_to_prop(zc
->zc_value
);
672 if (prop
== ZPROP_INVAL
) {
673 if (!zfs_prop_user(zc
->zc_value
))
675 return (zfs_secpolicy_write_perms(zc
->zc_name
,
676 ZFS_DELEG_PERM_USERPROP
, cr
));
678 if (!zfs_prop_inheritable(prop
))
680 return (zfs_secpolicy_setprop(zc
->zc_name
, prop
, cr
));
685 * Returns the nvlist as specified by the user in the zfs_cmd_t.
688 get_nvlist(uint64_t nvl
, uint64_t size
, nvlist_t
**nvp
)
692 nvlist_t
*list
= NULL
;
695 * Read in and unpack the user-supplied nvlist.
700 packed
= kmem_alloc(size
, KM_SLEEP
);
702 if ((error
= xcopyin((void *)(uintptr_t)nvl
, packed
, size
)) != 0) {
703 kmem_free(packed
, size
);
707 if ((error
= nvlist_unpack(packed
, size
, &list
, 0)) != 0) {
708 kmem_free(packed
, size
);
712 kmem_free(packed
, size
);
719 put_nvlist(zfs_cmd_t
*zc
, nvlist_t
*nvl
)
725 VERIFY(nvlist_size(nvl
, &size
, NV_ENCODE_NATIVE
) == 0);
727 if (size
> zc
->zc_nvlist_dst_size
) {
730 packed
= kmem_alloc(size
, KM_SLEEP
);
731 VERIFY(nvlist_pack(nvl
, &packed
, &size
, NV_ENCODE_NATIVE
,
733 error
= xcopyout(packed
, (void *)(uintptr_t)zc
->zc_nvlist_dst
,
735 kmem_free(packed
, size
);
738 zc
->zc_nvlist_dst_size
= size
;
743 zfs_ioc_pool_create(zfs_cmd_t
*zc
)
746 nvlist_t
*config
, *props
= NULL
;
747 nvlist_t
*rootprops
= NULL
;
748 nvlist_t
*zplprops
= NULL
;
751 if (error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
755 if (zc
->zc_nvlist_src_size
!= 0 && (error
=
756 get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
, &props
))) {
762 nvlist_t
*nvl
= NULL
;
763 uint64_t version
= SPA_VERSION
;
765 (void) nvlist_lookup_uint64(props
,
766 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
);
767 if (version
< SPA_VERSION_INITIAL
|| version
> SPA_VERSION
) {
771 (void) nvlist_lookup_nvlist(props
, ZPOOL_ROOTFS_PROPS
, &nvl
);
773 error
= nvlist_dup(nvl
, &rootprops
, KM_SLEEP
);
779 (void) nvlist_remove_all(props
, ZPOOL_ROOTFS_PROPS
);
781 VERIFY(nvlist_alloc(&zplprops
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
782 error
= zfs_fill_zplprops_root(version
, rootprops
,
788 buf
= history_str_get(zc
);
790 error
= spa_create(zc
->zc_name
, config
, props
, buf
, zplprops
);
793 * Set the remaining root properties
796 (error
= zfs_set_prop_nvlist(zc
->zc_name
, rootprops
)) != 0)
797 (void) spa_destroy(zc
->zc_name
);
800 history_str_free(buf
);
803 nvlist_free(rootprops
);
804 nvlist_free(zplprops
);
812 zfs_ioc_pool_destroy(zfs_cmd_t
*zc
)
816 error
= spa_destroy(zc
->zc_name
);
821 zfs_ioc_pool_import(zfs_cmd_t
*zc
)
824 nvlist_t
*config
, *props
= NULL
;
827 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
831 if (zc
->zc_nvlist_src_size
!= 0 && (error
=
832 get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
, &props
))) {
837 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &guid
) != 0 ||
840 else if (zc
->zc_cookie
)
841 error
= spa_import_faulted(zc
->zc_name
, config
,
844 error
= spa_import(zc
->zc_name
, config
, props
);
855 zfs_ioc_pool_export(zfs_cmd_t
*zc
)
858 boolean_t force
= (boolean_t
)zc
->zc_cookie
;
859 boolean_t hardforce
= (boolean_t
)zc
->zc_guid
;
862 error
= spa_export(zc
->zc_name
, NULL
, force
, hardforce
);
867 zfs_ioc_pool_configs(zfs_cmd_t
*zc
)
872 if ((configs
= spa_all_configs(&zc
->zc_cookie
)) == NULL
)
875 error
= put_nvlist(zc
, configs
);
877 nvlist_free(configs
);
883 zfs_ioc_pool_stats(zfs_cmd_t
*zc
)
889 error
= spa_get_stats(zc
->zc_name
, &config
, zc
->zc_value
,
890 sizeof (zc
->zc_value
));
892 if (config
!= NULL
) {
893 ret
= put_nvlist(zc
, config
);
897 * The config may be present even if 'error' is non-zero.
898 * In this case we return success, and preserve the real errno
901 zc
->zc_cookie
= error
;
910 * Try to import the given pool, returning pool stats as appropriate so that
911 * user land knows which devices are available and overall pool health.
914 zfs_ioc_pool_tryimport(zfs_cmd_t
*zc
)
916 nvlist_t
*tryconfig
, *config
;
919 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
923 config
= spa_tryimport(tryconfig
);
925 nvlist_free(tryconfig
);
930 error
= put_nvlist(zc
, config
);
937 zfs_ioc_pool_scrub(zfs_cmd_t
*zc
)
942 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
945 error
= spa_scrub(spa
, zc
->zc_cookie
);
947 spa_close(spa
, FTAG
);
953 zfs_ioc_pool_freeze(zfs_cmd_t
*zc
)
958 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
961 spa_close(spa
, FTAG
);
967 zfs_ioc_pool_upgrade(zfs_cmd_t
*zc
)
972 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
975 if (zc
->zc_cookie
< spa_version(spa
) || zc
->zc_cookie
> SPA_VERSION
) {
976 spa_close(spa
, FTAG
);
980 spa_upgrade(spa
, zc
->zc_cookie
);
981 spa_close(spa
, FTAG
);
987 zfs_ioc_pool_get_history(zfs_cmd_t
*zc
)
994 if ((size
= zc
->zc_history_len
) == 0)
997 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1000 if (spa_version(spa
) < SPA_VERSION_ZPOOL_HISTORY
) {
1001 spa_close(spa
, FTAG
);
1005 hist_buf
= kmem_alloc(size
, KM_SLEEP
);
1006 if ((error
= spa_history_get(spa
, &zc
->zc_history_offset
,
1007 &zc
->zc_history_len
, hist_buf
)) == 0) {
1008 error
= xcopyout(hist_buf
,
1009 (char *)(uintptr_t)zc
->zc_history
,
1010 zc
->zc_history_len
);
1013 spa_close(spa
, FTAG
);
1014 kmem_free(hist_buf
, size
);
1019 zfs_ioc_dsobj_to_dsname(zfs_cmd_t
*zc
)
1023 if (error
= dsl_dsobj_to_dsname(zc
->zc_name
, zc
->zc_obj
, zc
->zc_value
))
1030 zfs_ioc_obj_to_path(zfs_cmd_t
*zc
)
1035 if ((error
= dmu_objset_open(zc
->zc_name
, DMU_OST_ZFS
,
1036 DS_MODE_USER
| DS_MODE_READONLY
, &osp
)) != 0)
1038 error
= zfs_obj_to_path(osp
, zc
->zc_obj
, zc
->zc_value
,
1039 sizeof (zc
->zc_value
));
1040 dmu_objset_close(osp
);
1046 zfs_ioc_vdev_add(zfs_cmd_t
*zc
)
1050 nvlist_t
*config
, **l2cache
, **spares
;
1051 uint_t nl2cache
= 0, nspares
= 0;
1053 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1057 error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1059 (void) nvlist_lookup_nvlist_array(config
, ZPOOL_CONFIG_L2CACHE
,
1060 &l2cache
, &nl2cache
);
1062 (void) nvlist_lookup_nvlist_array(config
, ZPOOL_CONFIG_SPARES
,
1066 * A root pool with concatenated devices is not supported.
1067 * Thus, can not add a device to a root pool.
1069 * Intent log device can not be added to a rootpool because
1070 * during mountroot, zil is replayed, a seperated log device
1071 * can not be accessed during the mountroot time.
1073 * l2cache and spare devices are ok to be added to a rootpool.
1075 if (spa
->spa_bootfs
!= 0 && nl2cache
== 0 && nspares
== 0) {
1076 spa_close(spa
, FTAG
);
1081 error
= spa_vdev_add(spa
, config
);
1082 nvlist_free(config
);
1084 spa_close(spa
, FTAG
);
1089 zfs_ioc_vdev_remove(zfs_cmd_t
*zc
)
1094 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1097 error
= spa_vdev_remove(spa
, zc
->zc_guid
, B_FALSE
);
1098 spa_close(spa
, FTAG
);
1103 zfs_ioc_vdev_set_state(zfs_cmd_t
*zc
)
1107 vdev_state_t newstate
= VDEV_STATE_UNKNOWN
;
1109 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1111 switch (zc
->zc_cookie
) {
1112 case VDEV_STATE_ONLINE
:
1113 error
= vdev_online(spa
, zc
->zc_guid
, zc
->zc_obj
, &newstate
);
1116 case VDEV_STATE_OFFLINE
:
1117 error
= vdev_offline(spa
, zc
->zc_guid
, zc
->zc_obj
);
1120 case VDEV_STATE_FAULTED
:
1121 error
= vdev_fault(spa
, zc
->zc_guid
);
1124 case VDEV_STATE_DEGRADED
:
1125 error
= vdev_degrade(spa
, zc
->zc_guid
);
1131 zc
->zc_cookie
= newstate
;
1132 spa_close(spa
, FTAG
);
1137 zfs_ioc_vdev_attach(zfs_cmd_t
*zc
)
1140 int replacing
= zc
->zc_cookie
;
1144 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1147 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1149 error
= spa_vdev_attach(spa
, zc
->zc_guid
, config
, replacing
);
1150 nvlist_free(config
);
1153 spa_close(spa
, FTAG
);
1158 zfs_ioc_vdev_detach(zfs_cmd_t
*zc
)
1163 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1166 error
= spa_vdev_detach(spa
, zc
->zc_guid
, 0, B_FALSE
);
1168 spa_close(spa
, FTAG
);
1173 zfs_ioc_vdev_setpath(zfs_cmd_t
*zc
)
1176 char *path
= zc
->zc_value
;
1177 uint64_t guid
= zc
->zc_guid
;
1180 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1184 error
= spa_vdev_setpath(spa
, guid
, path
);
1185 spa_close(spa
, FTAG
);
1191 * zc_name name of filesystem
1192 * zc_nvlist_dst_size size of buffer for property nvlist
1195 * zc_objset_stats stats
1196 * zc_nvlist_dst property nvlist
1197 * zc_nvlist_dst_size size of property nvlist
1200 zfs_ioc_objset_stats(zfs_cmd_t
*zc
)
1202 objset_t
*os
= NULL
;
1206 if (error
= dmu_objset_open(zc
->zc_name
,
1207 DMU_OST_ANY
, DS_MODE_USER
| DS_MODE_READONLY
, &os
))
1210 dmu_objset_fast_stat(os
, &zc
->zc_objset_stats
);
1212 if (zc
->zc_nvlist_dst
!= 0 &&
1213 (error
= dsl_prop_get_all(os
, &nv
, FALSE
)) == 0) {
1214 dmu_objset_stats(os
, nv
);
1216 * NB: zvol_get_stats() will read the objset contents,
1217 * which we aren't supposed to do with a
1218 * DS_MODE_USER hold, because it could be
1219 * inconsistent. So this is a bit of a workaround...
1221 if (!zc
->zc_objset_stats
.dds_inconsistent
) {
1222 if (dmu_objset_type(os
) == DMU_OST_ZVOL
)
1223 VERIFY(zvol_get_stats(os
, nv
) == 0);
1225 error
= put_nvlist(zc
, nv
);
1229 dmu_objset_close(os
);
1234 nvl_add_zplprop(objset_t
*os
, nvlist_t
*props
, zfs_prop_t prop
)
1240 * zfs_get_zplprop() will either find a value or give us
1241 * the default value (if there is one).
1243 if ((error
= zfs_get_zplprop(os
, prop
, &value
)) != 0)
1245 VERIFY(nvlist_add_uint64(props
, zfs_prop_to_name(prop
), value
) == 0);
1251 * zc_name name of filesystem
1252 * zc_nvlist_dst_size size of buffer for zpl property nvlist
1255 * zc_nvlist_dst zpl property nvlist
1256 * zc_nvlist_dst_size size of zpl property nvlist
1259 zfs_ioc_objset_zplprops(zfs_cmd_t
*zc
)
1264 if (err
= dmu_objset_open(zc
->zc_name
,
1265 DMU_OST_ANY
, DS_MODE_USER
| DS_MODE_READONLY
, &os
))
1268 dmu_objset_fast_stat(os
, &zc
->zc_objset_stats
);
1271 * NB: nvl_add_zplprop() will read the objset contents,
1272 * which we aren't supposed to do with a DS_MODE_USER
1273 * hold, because it could be inconsistent.
1275 if (zc
->zc_nvlist_dst
!= NULL
&&
1276 !zc
->zc_objset_stats
.dds_inconsistent
&&
1277 dmu_objset_type(os
) == DMU_OST_ZFS
) {
1280 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1281 if ((err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_VERSION
)) == 0 &&
1282 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_NORMALIZE
)) == 0 &&
1283 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_UTF8ONLY
)) == 0 &&
1284 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_CASE
)) == 0)
1285 err
= put_nvlist(zc
, nv
);
1290 dmu_objset_close(os
);
1296 * zc_name name of filesystem
1297 * zc_cookie zap cursor
1298 * zc_nvlist_dst_size size of buffer for property nvlist
1301 * zc_name name of next filesystem
1302 * zc_objset_stats stats
1303 * zc_nvlist_dst property nvlist
1304 * zc_nvlist_dst_size size of property nvlist
1307 zfs_ioc_dataset_list_next(zfs_cmd_t
*zc
)
1313 if (error
= dmu_objset_open(zc
->zc_name
,
1314 DMU_OST_ANY
, DS_MODE_USER
| DS_MODE_READONLY
, &os
)) {
1315 if (error
== ENOENT
)
1320 p
= strrchr(zc
->zc_name
, '/');
1321 if (p
== NULL
|| p
[1] != '\0')
1322 (void) strlcat(zc
->zc_name
, "/", sizeof (zc
->zc_name
));
1323 p
= zc
->zc_name
+ strlen(zc
->zc_name
);
1325 if (zc
->zc_cookie
== 0) {
1326 uint64_t cookie
= 0;
1327 int len
= sizeof (zc
->zc_name
) - (p
- zc
->zc_name
);
1329 while (dmu_dir_list_next(os
, len
, p
, NULL
, &cookie
) == 0)
1330 dmu_objset_prefetch(p
, NULL
);
1334 error
= dmu_dir_list_next(os
,
1335 sizeof (zc
->zc_name
) - (p
- zc
->zc_name
), p
,
1336 NULL
, &zc
->zc_cookie
);
1337 if (error
== ENOENT
)
1339 } while (error
== 0 && !INGLOBALZONE(curproc
) &&
1340 !zone_dataset_visible(zc
->zc_name
, NULL
));
1341 dmu_objset_close(os
);
1344 * If it's a hidden dataset (ie. with a '$' in its name), don't
1345 * try to get stats for it. Userland will skip over it.
1347 if (error
== 0 && strchr(zc
->zc_name
, '$') == NULL
)
1348 error
= zfs_ioc_objset_stats(zc
); /* fill in the stats */
1355 * zc_name name of filesystem
1356 * zc_cookie zap cursor
1357 * zc_nvlist_dst_size size of buffer for property nvlist
1360 * zc_name name of next snapshot
1361 * zc_objset_stats stats
1362 * zc_nvlist_dst property nvlist
1363 * zc_nvlist_dst_size size of property nvlist
1366 zfs_ioc_snapshot_list_next(zfs_cmd_t
*zc
)
1371 error
= dmu_objset_open(zc
->zc_name
,
1372 DMU_OST_ANY
, DS_MODE_USER
| DS_MODE_READONLY
, &os
);
1374 return (error
== ENOENT
? ESRCH
: error
);
1376 if (zc
->zc_cookie
== 0)
1377 dmu_objset_find(zc
->zc_name
, dmu_objset_prefetch
,
1378 NULL
, DS_FIND_SNAPSHOTS
);
1380 * A dataset name of maximum length cannot have any snapshots,
1381 * so exit immediately.
1383 if (strlcat(zc
->zc_name
, "@", sizeof (zc
->zc_name
)) >= MAXNAMELEN
) {
1384 dmu_objset_close(os
);
1388 error
= dmu_snapshot_list_next(os
,
1389 sizeof (zc
->zc_name
) - strlen(zc
->zc_name
),
1390 zc
->zc_name
+ strlen(zc
->zc_name
), NULL
, &zc
->zc_cookie
, NULL
);
1391 dmu_objset_close(os
);
1393 error
= zfs_ioc_objset_stats(zc
); /* fill in the stats */
1394 else if (error
== ENOENT
)
1397 /* if we failed, undo the @ that we tacked on to zc_name */
1399 *strchr(zc
->zc_name
, '@') = '\0';
1404 zfs_set_prop_nvlist(const char *name
, nvlist_t
*nvl
)
1412 * First validate permission to set all of the properties
1415 while ((elem
= nvlist_next_nvpair(nvl
, elem
)) != NULL
) {
1416 const char *propname
= nvpair_name(elem
);
1417 zfs_prop_t prop
= zfs_name_to_prop(propname
);
1419 if (prop
== ZPROP_INVAL
) {
1421 * If this is a user-defined property, it must be a
1422 * string, and there is no further validation to do.
1424 if (!zfs_prop_user(propname
) ||
1425 nvpair_type(elem
) != DATA_TYPE_STRING
)
1428 if (error
= zfs_secpolicy_write_perms(name
,
1429 ZFS_DELEG_PERM_USERPROP
, CRED()))
1434 if ((error
= zfs_secpolicy_setprop(name
, prop
, CRED())) != 0)
1438 * Check that this value is valid for this pool version
1441 case ZFS_PROP_COMPRESSION
:
1443 * If the user specified gzip compression, make sure
1444 * the SPA supports it. We ignore any errors here since
1445 * we'll catch them later.
1447 if (nvpair_type(elem
) == DATA_TYPE_UINT64
&&
1448 nvpair_value_uint64(elem
, &intval
) == 0) {
1449 if (intval
>= ZIO_COMPRESS_GZIP_1
&&
1450 intval
<= ZIO_COMPRESS_GZIP_9
&&
1451 zfs_earlier_version(name
,
1452 SPA_VERSION_GZIP_COMPRESSION
))
1456 * If this is a bootable dataset then
1457 * verify that the compression algorithm
1458 * is supported for booting. We must return
1459 * something other than ENOTSUP since it
1460 * implies a downrev pool version.
1462 if (zfs_is_bootfs(name
) &&
1463 !BOOTFS_COMPRESS_VALID(intval
))
1468 case ZFS_PROP_COPIES
:
1469 if (zfs_earlier_version(name
,
1470 SPA_VERSION_DITTO_BLOCKS
))
1474 case ZFS_PROP_SHARESMB
:
1475 if (zpl_earlier_version(name
, ZPL_VERSION_FUID
))
1479 case ZFS_PROP_ACLINHERIT
:
1480 if (nvpair_type(elem
) == DATA_TYPE_UINT64
&&
1481 nvpair_value_uint64(elem
, &intval
) == 0)
1482 if (intval
== ZFS_ACL_PASSTHROUGH_X
&&
1483 zfs_earlier_version(name
,
1484 SPA_VERSION_PASSTHROUGH_X
))
1490 while ((elem
= nvlist_next_nvpair(nvl
, elem
)) != NULL
) {
1491 const char *propname
= nvpair_name(elem
);
1492 zfs_prop_t prop
= zfs_name_to_prop(propname
);
1494 if (prop
== ZPROP_INVAL
) {
1495 VERIFY(nvpair_value_string(elem
, &strval
) == 0);
1496 error
= dsl_prop_set(name
, propname
, 1,
1497 strlen(strval
) + 1, strval
);
1505 case ZFS_PROP_QUOTA
:
1506 if ((error
= nvpair_value_uint64(elem
, &intval
)) != 0 ||
1507 (error
= dsl_dir_set_quota(name
, intval
)) != 0)
1511 case ZFS_PROP_REFQUOTA
:
1512 if ((error
= nvpair_value_uint64(elem
, &intval
)) != 0 ||
1513 (error
= dsl_dataset_set_quota(name
, intval
)) != 0)
1517 case ZFS_PROP_RESERVATION
:
1518 if ((error
= nvpair_value_uint64(elem
, &intval
)) != 0 ||
1519 (error
= dsl_dir_set_reservation(name
,
1524 case ZFS_PROP_REFRESERVATION
:
1525 if ((error
= nvpair_value_uint64(elem
, &intval
)) != 0 ||
1526 (error
= dsl_dataset_set_reservation(name
,
1531 case ZFS_PROP_VOLSIZE
:
1532 if ((error
= nvpair_value_uint64(elem
, &intval
)) != 0 ||
1533 (error
= zvol_set_volsize(name
,
1534 ddi_driver_major(zfs_dip
), intval
)) != 0)
1538 case ZFS_PROP_VOLBLOCKSIZE
:
1539 if ((error
= nvpair_value_uint64(elem
, &intval
)) != 0 ||
1540 (error
= zvol_set_volblocksize(name
, intval
)) != 0)
1544 case ZFS_PROP_VERSION
:
1545 if ((error
= nvpair_value_uint64(elem
, &intval
)) != 0 ||
1546 (error
= zfs_set_version(name
, intval
)) != 0)
1551 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
1552 if (zfs_prop_get_type(prop
) !=
1555 VERIFY(nvpair_value_string(elem
, &strval
) == 0);
1556 if ((error
= dsl_prop_set(name
,
1557 nvpair_name(elem
), 1, strlen(strval
) + 1,
1560 } else if (nvpair_type(elem
) == DATA_TYPE_UINT64
) {
1563 VERIFY(nvpair_value_uint64(elem
, &intval
) == 0);
1565 switch (zfs_prop_get_type(prop
)) {
1566 case PROP_TYPE_NUMBER
:
1568 case PROP_TYPE_STRING
:
1570 case PROP_TYPE_INDEX
:
1571 if (zfs_prop_index_to_string(prop
,
1572 intval
, &unused
) != 0)
1577 "unknown property type");
1581 if ((error
= dsl_prop_set(name
, propname
,
1582 8, 1, &intval
)) != 0)
1596 * zc_name name of filesystem
1597 * zc_value name of property to inherit
1598 * zc_nvlist_src{_size} nvlist of properties to apply
1599 * zc_cookie clear existing local props?
1604 zfs_ioc_set_prop(zfs_cmd_t
*zc
)
1609 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1613 if (zc
->zc_cookie
) {
1614 nvlist_t
*origprops
;
1617 if (dmu_objset_open(zc
->zc_name
, DMU_OST_ANY
,
1618 DS_MODE_USER
| DS_MODE_READONLY
, &os
) == 0) {
1619 if (dsl_prop_get_all(os
, &origprops
, TRUE
) == 0) {
1620 clear_props(zc
->zc_name
, origprops
, nvl
);
1621 nvlist_free(origprops
);
1623 dmu_objset_close(os
);
1628 error
= zfs_set_prop_nvlist(zc
->zc_name
, nvl
);
1636 * zc_name name of filesystem
1637 * zc_value name of property to inherit
1642 zfs_ioc_inherit_prop(zfs_cmd_t
*zc
)
1644 /* the property name has been validated by zfs_secpolicy_inherit() */
1645 return (dsl_prop_set(zc
->zc_name
, zc
->zc_value
, 0, 0, NULL
));
1649 zfs_ioc_pool_set_props(zfs_cmd_t
*zc
)
1656 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1661 * If the only property is the configfile, then just do a spa_lookup()
1662 * to handle the faulted case.
1664 elem
= nvlist_next_nvpair(props
, NULL
);
1665 if (elem
!= NULL
&& strcmp(nvpair_name(elem
),
1666 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
)) == 0 &&
1667 nvlist_next_nvpair(props
, elem
) == NULL
) {
1668 mutex_enter(&spa_namespace_lock
);
1669 if ((spa
= spa_lookup(zc
->zc_name
)) != NULL
) {
1670 spa_configfile_set(spa
, props
, B_FALSE
);
1671 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
1673 mutex_exit(&spa_namespace_lock
);
1678 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0) {
1683 error
= spa_prop_set(spa
, props
);
1686 spa_close(spa
, FTAG
);
1692 zfs_ioc_pool_get_props(zfs_cmd_t
*zc
)
1696 nvlist_t
*nvp
= NULL
;
1698 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0) {
1700 * If the pool is faulted, there may be properties we can still
1701 * get (such as altroot and cachefile), so attempt to get them
1704 mutex_enter(&spa_namespace_lock
);
1705 if ((spa
= spa_lookup(zc
->zc_name
)) != NULL
)
1706 error
= spa_prop_get(spa
, &nvp
);
1707 mutex_exit(&spa_namespace_lock
);
1709 error
= spa_prop_get(spa
, &nvp
);
1710 spa_close(spa
, FTAG
);
1713 if (error
== 0 && zc
->zc_nvlist_dst
!= NULL
)
1714 error
= put_nvlist(zc
, nvp
);
1723 zfs_ioc_iscsi_perm_check(zfs_cmd_t
*zc
)
1733 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1738 if ((error
= nvlist_lookup_uint32(nvp
,
1739 ZFS_DELEG_PERM_UID
, &uid
)) != 0) {
1744 if ((error
= nvlist_lookup_uint32(nvp
,
1745 ZFS_DELEG_PERM_GID
, &gid
)) != 0) {
1750 if ((error
= nvlist_lookup_uint32_array(nvp
, ZFS_DELEG_PERM_GROUPS
,
1751 &groups
, &group_cnt
)) != 0) {
1755 usercred
= cralloc();
1756 if ((crsetugid(usercred
, uid
, gid
) != 0) ||
1757 (crsetgroups(usercred
, group_cnt
, (gid_t
*)groups
) != 0)) {
1763 error
= dsl_deleg_access(zc
->zc_name
,
1764 zfs_prop_to_name(ZFS_PROP_SHAREISCSI
), usercred
);
1771 * zc_name name of filesystem
1772 * zc_nvlist_src{_size} nvlist of delegated permissions
1773 * zc_perm_action allow/unallow flag
1778 zfs_ioc_set_fsacl(zfs_cmd_t
*zc
)
1781 nvlist_t
*fsaclnv
= NULL
;
1783 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1788 * Verify nvlist is constructed correctly
1790 if ((error
= zfs_deleg_verify_nvlist(fsaclnv
)) != 0) {
1791 nvlist_free(fsaclnv
);
1796 * If we don't have PRIV_SYS_MOUNT, then validate
1797 * that user is allowed to hand out each permission in
1801 error
= secpolicy_zfs(CRED());
1803 if (zc
->zc_perm_action
== B_FALSE
) {
1804 error
= dsl_deleg_can_allow(zc
->zc_name
,
1807 error
= dsl_deleg_can_unallow(zc
->zc_name
,
1813 error
= dsl_deleg_set(zc
->zc_name
, fsaclnv
, zc
->zc_perm_action
);
1815 nvlist_free(fsaclnv
);
1821 * zc_name name of filesystem
1824 * zc_nvlist_src{_size} nvlist of delegated permissions
1827 zfs_ioc_get_fsacl(zfs_cmd_t
*zc
)
1832 if ((error
= dsl_deleg_get(zc
->zc_name
, &nvp
)) == 0) {
1833 error
= put_nvlist(zc
, nvp
);
1842 * zc_name name of volume
1847 zfs_ioc_create_minor(zfs_cmd_t
*zc
)
1849 return (zvol_create_minor(zc
->zc_name
, ddi_driver_major(zfs_dip
)));
1854 * zc_name name of volume
1859 zfs_ioc_remove_minor(zfs_cmd_t
*zc
)
1861 return (zvol_remove_minor(zc
->zc_name
));
1865 * Search the vfs list for a specified resource. Returns a pointer to it
1866 * or NULL if no suitable entry is found. The caller of this routine
1867 * is responsible for releasing the returned vfs pointer.
1870 zfs_get_vfs(const char *resource
)
1873 struct vfs
*vfs_found
= NULL
;
1875 vfs_list_read_lock();
1878 if (strcmp(refstr_value(vfsp
->vfs_resource
), resource
) == 0) {
1883 vfsp
= vfsp
->vfs_next
;
1884 } while (vfsp
!= rootvfs
);
1891 zfs_create_cb(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
)
1893 zfs_creat_t
*zct
= arg
;
1895 zfs_create_fs(os
, cr
, zct
->zct_zplprops
, tx
);
1898 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
1902 * createprops list of properties requested by creator
1903 * default_zplver zpl version to use if unspecified in createprops
1904 * fuids_ok fuids allowed in this version of the spa?
1905 * os parent objset pointer (NULL if root fs)
1908 * zplprops values for the zplprops we attach to the master node object
1909 * is_ci true if requested file system will be purely case-insensitive
1911 * Determine the settings for utf8only, normalization and
1912 * casesensitivity. Specific values may have been requested by the
1913 * creator and/or we can inherit values from the parent dataset. If
1914 * the file system is of too early a vintage, a creator can not
1915 * request settings for these properties, even if the requested
1916 * setting is the default value. We don't actually want to create dsl
1917 * properties for these, so remove them from the source nvlist after
1921 zfs_fill_zplprops_impl(objset_t
*os
, uint64_t default_zplver
,
1922 boolean_t fuids_ok
, nvlist_t
*createprops
, nvlist_t
*zplprops
,
1925 uint64_t zplver
= default_zplver
;
1926 uint64_t sense
= ZFS_PROP_UNDEFINED
;
1927 uint64_t norm
= ZFS_PROP_UNDEFINED
;
1928 uint64_t u8
= ZFS_PROP_UNDEFINED
;
1930 ASSERT(zplprops
!= NULL
);
1933 * Pull out creator prop choices, if any.
1936 (void) nvlist_lookup_uint64(createprops
,
1937 zfs_prop_to_name(ZFS_PROP_VERSION
), &zplver
);
1938 (void) nvlist_lookup_uint64(createprops
,
1939 zfs_prop_to_name(ZFS_PROP_NORMALIZE
), &norm
);
1940 (void) nvlist_remove_all(createprops
,
1941 zfs_prop_to_name(ZFS_PROP_NORMALIZE
));
1942 (void) nvlist_lookup_uint64(createprops
,
1943 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), &u8
);
1944 (void) nvlist_remove_all(createprops
,
1945 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
));
1946 (void) nvlist_lookup_uint64(createprops
,
1947 zfs_prop_to_name(ZFS_PROP_CASE
), &sense
);
1948 (void) nvlist_remove_all(createprops
,
1949 zfs_prop_to_name(ZFS_PROP_CASE
));
1953 * If the zpl version requested is whacky or the file system
1954 * or pool is version is too "young" to support normalization
1955 * and the creator tried to set a value for one of the props,
1958 if ((zplver
< ZPL_VERSION_INITIAL
|| zplver
> ZPL_VERSION
) ||
1959 (zplver
>= ZPL_VERSION_FUID
&& !fuids_ok
) ||
1960 (zplver
< ZPL_VERSION_NORMALIZATION
&&
1961 (norm
!= ZFS_PROP_UNDEFINED
|| u8
!= ZFS_PROP_UNDEFINED
||
1962 sense
!= ZFS_PROP_UNDEFINED
)))
1966 * Put the version in the zplprops
1968 VERIFY(nvlist_add_uint64(zplprops
,
1969 zfs_prop_to_name(ZFS_PROP_VERSION
), zplver
) == 0);
1971 if (norm
== ZFS_PROP_UNDEFINED
)
1972 VERIFY(zfs_get_zplprop(os
, ZFS_PROP_NORMALIZE
, &norm
) == 0);
1973 VERIFY(nvlist_add_uint64(zplprops
,
1974 zfs_prop_to_name(ZFS_PROP_NORMALIZE
), norm
) == 0);
1977 * If we're normalizing, names must always be valid UTF-8 strings.
1981 if (u8
== ZFS_PROP_UNDEFINED
)
1982 VERIFY(zfs_get_zplprop(os
, ZFS_PROP_UTF8ONLY
, &u8
) == 0);
1983 VERIFY(nvlist_add_uint64(zplprops
,
1984 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), u8
) == 0);
1986 if (sense
== ZFS_PROP_UNDEFINED
)
1987 VERIFY(zfs_get_zplprop(os
, ZFS_PROP_CASE
, &sense
) == 0);
1988 VERIFY(nvlist_add_uint64(zplprops
,
1989 zfs_prop_to_name(ZFS_PROP_CASE
), sense
) == 0);
1992 *is_ci
= (sense
== ZFS_CASE_INSENSITIVE
);
1998 zfs_fill_zplprops(const char *dataset
, nvlist_t
*createprops
,
1999 nvlist_t
*zplprops
, boolean_t
*is_ci
)
2001 boolean_t fuids_ok
= B_TRUE
;
2002 uint64_t zplver
= ZPL_VERSION
;
2003 objset_t
*os
= NULL
;
2004 char parentname
[MAXNAMELEN
];
2008 (void) strlcpy(parentname
, dataset
, sizeof (parentname
));
2009 cp
= strrchr(parentname
, '/');
2013 if (zfs_earlier_version(dataset
, SPA_VERSION_FUID
)) {
2014 zplver
= ZPL_VERSION_FUID
- 1;
2019 * Open parent object set so we can inherit zplprop values.
2021 if ((error
= dmu_objset_open(parentname
, DMU_OST_ANY
,
2022 DS_MODE_USER
| DS_MODE_READONLY
, &os
)) != 0)
2025 error
= zfs_fill_zplprops_impl(os
, zplver
, fuids_ok
, createprops
,
2027 dmu_objset_close(os
);
2032 zfs_fill_zplprops_root(uint64_t spa_vers
, nvlist_t
*createprops
,
2033 nvlist_t
*zplprops
, boolean_t
*is_ci
)
2035 boolean_t fuids_ok
= B_TRUE
;
2036 uint64_t zplver
= ZPL_VERSION
;
2039 if (spa_vers
< SPA_VERSION_FUID
) {
2040 zplver
= ZPL_VERSION_FUID
- 1;
2044 error
= zfs_fill_zplprops_impl(NULL
, zplver
, fuids_ok
, createprops
,
2051 * zc_objset_type type of objset to create (fs vs zvol)
2052 * zc_name name of new objset
2053 * zc_value name of snapshot to clone from (may be empty)
2054 * zc_nvlist_src{_size} nvlist of properties to apply
2059 zfs_ioc_create(zfs_cmd_t
*zc
)
2064 nvlist_t
*nvprops
= NULL
;
2065 void (*cbfunc
)(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
);
2066 dmu_objset_type_t type
= zc
->zc_objset_type
;
2071 cbfunc
= zfs_create_cb
;
2075 cbfunc
= zvol_create_cb
;
2082 if (strchr(zc
->zc_name
, '@') ||
2083 strchr(zc
->zc_name
, '%'))
2086 if (zc
->zc_nvlist_src
!= NULL
&&
2087 (error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2091 zct
.zct_zplprops
= NULL
;
2092 zct
.zct_props
= nvprops
;
2094 if (zc
->zc_value
[0] != '\0') {
2096 * We're creating a clone of an existing snapshot.
2098 zc
->zc_value
[sizeof (zc
->zc_value
) - 1] = '\0';
2099 if (dataset_namecheck(zc
->zc_value
, NULL
, NULL
) != 0) {
2100 nvlist_free(nvprops
);
2104 error
= dmu_objset_open(zc
->zc_value
, type
,
2105 DS_MODE_USER
| DS_MODE_READONLY
, &clone
);
2107 nvlist_free(nvprops
);
2111 error
= dmu_objset_create(zc
->zc_name
, type
, clone
, 0,
2114 dmu_objset_close(clone
);
2115 nvlist_free(nvprops
);
2118 dmu_objset_close(clone
);
2120 boolean_t is_insensitive
= B_FALSE
;
2122 if (cbfunc
== NULL
) {
2123 nvlist_free(nvprops
);
2127 if (type
== DMU_OST_ZVOL
) {
2128 uint64_t volsize
, volblocksize
;
2130 if (nvprops
== NULL
||
2131 nvlist_lookup_uint64(nvprops
,
2132 zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
2134 nvlist_free(nvprops
);
2138 if ((error
= nvlist_lookup_uint64(nvprops
,
2139 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
2140 &volblocksize
)) != 0 && error
!= ENOENT
) {
2141 nvlist_free(nvprops
);
2146 volblocksize
= zfs_prop_default_numeric(
2147 ZFS_PROP_VOLBLOCKSIZE
);
2149 if ((error
= zvol_check_volblocksize(
2150 volblocksize
)) != 0 ||
2151 (error
= zvol_check_volsize(volsize
,
2152 volblocksize
)) != 0) {
2153 nvlist_free(nvprops
);
2156 } else if (type
== DMU_OST_ZFS
) {
2160 * We have to have normalization and
2161 * case-folding flags correct when we do the
2162 * file system creation, so go figure them out
2165 VERIFY(nvlist_alloc(&zct
.zct_zplprops
,
2166 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2167 error
= zfs_fill_zplprops(zc
->zc_name
, nvprops
,
2168 zct
.zct_zplprops
, &is_insensitive
);
2170 nvlist_free(nvprops
);
2171 nvlist_free(zct
.zct_zplprops
);
2175 error
= dmu_objset_create(zc
->zc_name
, type
, NULL
,
2176 is_insensitive
? DS_FLAG_CI_DATASET
: 0, cbfunc
, &zct
);
2177 nvlist_free(zct
.zct_zplprops
);
2181 * It would be nice to do this atomically.
2184 if ((error
= zfs_set_prop_nvlist(zc
->zc_name
, nvprops
)) != 0)
2185 (void) dmu_objset_destroy(zc
->zc_name
);
2187 nvlist_free(nvprops
);
2191 struct snap_prop_arg
{
2193 const char *snapname
;
2197 set_snap_props(char *name
, void *arg
)
2199 struct snap_prop_arg
*snpa
= arg
;
2200 int len
= strlen(name
) + strlen(snpa
->snapname
) + 2;
2201 char *buf
= kmem_alloc(len
, KM_SLEEP
);
2204 (void) snprintf(buf
, len
, "%s@%s", name
, snpa
->snapname
);
2205 err
= zfs_set_prop_nvlist(buf
, snpa
->nvprops
);
2207 (void) dmu_objset_destroy(buf
);
2208 kmem_free(buf
, len
);
2214 * zc_name name of filesystem
2215 * zc_value short name of snapshot
2216 * zc_cookie recursive flag
2221 zfs_ioc_snapshot(zfs_cmd_t
*zc
)
2223 nvlist_t
*nvprops
= NULL
;
2225 boolean_t recursive
= zc
->zc_cookie
;
2227 if (snapshot_namecheck(zc
->zc_value
, NULL
, NULL
) != 0)
2230 if (zc
->zc_nvlist_src
!= NULL
&&
2231 (error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2235 error
= dmu_objset_snapshot(zc
->zc_name
, zc
->zc_value
, recursive
);
2238 * It would be nice to do this atomically.
2241 struct snap_prop_arg snpa
;
2242 snpa
.nvprops
= nvprops
;
2243 snpa
.snapname
= zc
->zc_value
;
2245 error
= dmu_objset_find(zc
->zc_name
,
2246 set_snap_props
, &snpa
, DS_FIND_CHILDREN
);
2248 (void) dmu_snapshots_destroy(zc
->zc_name
,
2252 error
= set_snap_props(zc
->zc_name
, &snpa
);
2255 nvlist_free(nvprops
);
2260 zfs_unmount_snap(char *name
, void *arg
)
2265 char *snapname
= arg
;
2266 int len
= strlen(name
) + strlen(snapname
) + 2;
2267 char *buf
= kmem_alloc(len
, KM_SLEEP
);
2269 (void) strcpy(buf
, name
);
2270 (void) strcat(buf
, "@");
2271 (void) strcat(buf
, snapname
);
2272 vfsp
= zfs_get_vfs(buf
);
2273 kmem_free(buf
, len
);
2274 } else if (strchr(name
, '@')) {
2275 vfsp
= zfs_get_vfs(name
);
2280 * Always force the unmount for snapshots.
2282 int flag
= MS_FORCE
;
2285 if ((err
= vn_vfswlock(vfsp
->vfs_vnodecovered
)) != 0) {
2290 if ((err
= dounmount(vfsp
, flag
, kcred
)) != 0)
2298 * zc_name name of filesystem
2299 * zc_value short name of snapshot
2304 zfs_ioc_destroy_snaps(zfs_cmd_t
*zc
)
2308 if (snapshot_namecheck(zc
->zc_value
, NULL
, NULL
) != 0)
2310 err
= dmu_objset_find(zc
->zc_name
,
2311 zfs_unmount_snap
, zc
->zc_value
, DS_FIND_CHILDREN
);
2314 return (dmu_snapshots_destroy(zc
->zc_name
, zc
->zc_value
));
2319 * zc_name name of dataset to destroy
2320 * zc_objset_type type of objset
2325 zfs_ioc_destroy(zfs_cmd_t
*zc
)
2327 if (strchr(zc
->zc_name
, '@') && zc
->zc_objset_type
== DMU_OST_ZFS
) {
2328 int err
= zfs_unmount_snap(zc
->zc_name
, NULL
);
2333 return (dmu_objset_destroy(zc
->zc_name
));
2338 * zc_name name of dataset to rollback (to most recent snapshot)
2343 zfs_ioc_rollback(zfs_cmd_t
*zc
)
2347 zfsvfs_t
*zfsvfs
= NULL
;
2350 * Get the zfsvfs for the receiving objset. There
2351 * won't be one if we're operating on a zvol, if the
2352 * objset doesn't exist yet, or is not mounted.
2354 error
= dmu_objset_open(zc
->zc_name
, DMU_OST_ANY
, DS_MODE_USER
, &os
);
2358 if (dmu_objset_type(os
) == DMU_OST_ZFS
) {
2359 mutex_enter(&os
->os
->os_user_ptr_lock
);
2360 zfsvfs
= dmu_objset_get_user(os
);
2362 VFS_HOLD(zfsvfs
->z_vfs
);
2363 mutex_exit(&os
->os
->os_user_ptr_lock
);
2366 if (zfsvfs
!= NULL
) {
2370 osname
= kmem_alloc(MAXNAMELEN
, KM_SLEEP
);
2371 error
= zfs_suspend_fs(zfsvfs
, osname
, &mode
);
2375 ASSERT(strcmp(osname
, zc
->zc_name
) == 0);
2376 error
= dmu_objset_rollback(os
);
2377 resume_err
= zfs_resume_fs(zfsvfs
, osname
, mode
);
2378 error
= error
? error
: resume_err
;
2380 dmu_objset_close(os
);
2382 kmem_free(osname
, MAXNAMELEN
);
2383 VFS_RELE(zfsvfs
->z_vfs
);
2385 error
= dmu_objset_rollback(os
);
2387 /* Note, the dmu_objset_rollback() releases the objset for us. */
2394 * zc_name old name of dataset
2395 * zc_value new name of dataset
2396 * zc_cookie recursive flag (only valid for snapshots)
2401 zfs_ioc_rename(zfs_cmd_t
*zc
)
2403 boolean_t recursive
= zc
->zc_cookie
& 1;
2405 zc
->zc_value
[sizeof (zc
->zc_value
) - 1] = '\0';
2406 if (dataset_namecheck(zc
->zc_value
, NULL
, NULL
) != 0 ||
2407 strchr(zc
->zc_value
, '%'))
2411 * Unmount snapshot unless we're doing a recursive rename,
2412 * in which case the dataset code figures out which snapshots
2415 if (!recursive
&& strchr(zc
->zc_name
, '@') != NULL
&&
2416 zc
->zc_objset_type
== DMU_OST_ZFS
) {
2417 int err
= zfs_unmount_snap(zc
->zc_name
, NULL
);
2421 return (dmu_objset_rename(zc
->zc_name
, zc
->zc_value
, recursive
));
2425 clear_props(char *dataset
, nvlist_t
*props
, nvlist_t
*newprops
)
2432 zc
= kmem_alloc(sizeof (zfs_cmd_t
), KM_SLEEP
);
2433 (void) strcpy(zc
->zc_name
, dataset
);
2434 for (prop
= nvlist_next_nvpair(props
, NULL
); prop
;
2435 prop
= nvlist_next_nvpair(props
, prop
)) {
2436 if (newprops
!= NULL
&&
2437 nvlist_exists(newprops
, nvpair_name(prop
)))
2439 (void) strcpy(zc
->zc_value
, nvpair_name(prop
));
2440 if (zfs_secpolicy_inherit(zc
, CRED()) == 0)
2441 (void) zfs_ioc_inherit_prop(zc
);
2443 kmem_free(zc
, sizeof (zfs_cmd_t
));
2448 * zc_name name of containing filesystem
2449 * zc_nvlist_src{_size} nvlist of properties to apply
2450 * zc_value name of snapshot to create
2451 * zc_string name of clone origin (if DRR_FLAG_CLONE)
2452 * zc_cookie file descriptor to recv from
2453 * zc_begin_record the BEGIN record of the stream (not byteswapped)
2454 * zc_guid force flag
2457 * zc_cookie number of bytes read
2460 zfs_ioc_recv(zfs_cmd_t
*zc
)
2464 dmu_recv_cookie_t drc
;
2465 zfsvfs_t
*zfsvfs
= NULL
;
2466 boolean_t force
= (boolean_t
)zc
->zc_guid
;
2469 nvlist_t
*props
= NULL
;
2470 nvlist_t
*origprops
= NULL
;
2471 objset_t
*origin
= NULL
;
2473 char tofs
[ZFS_MAXNAMELEN
];
2475 if (dataset_namecheck(zc
->zc_value
, NULL
, NULL
) != 0 ||
2476 strchr(zc
->zc_value
, '@') == NULL
||
2477 strchr(zc
->zc_value
, '%'))
2480 (void) strcpy(tofs
, zc
->zc_value
);
2481 tosnap
= strchr(tofs
, '@');
2485 if (zc
->zc_nvlist_src
!= NULL
&&
2486 (error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2497 if (dmu_objset_open(tofs
, DMU_OST_ANY
,
2498 DS_MODE_USER
| DS_MODE_READONLY
, &os
) == 0) {
2500 * Try to get the zfsvfs for the receiving objset.
2501 * There won't be one if we're operating on a zvol,
2502 * if the objset doesn't exist yet, or is not mounted.
2504 mutex_enter(&os
->os
->os_user_ptr_lock
);
2505 if (zfsvfs
= dmu_objset_get_user(os
)) {
2506 if (!mutex_tryenter(&zfsvfs
->z_online_recv_lock
)) {
2507 mutex_exit(&os
->os
->os_user_ptr_lock
);
2508 dmu_objset_close(os
);
2513 VFS_HOLD(zfsvfs
->z_vfs
);
2515 mutex_exit(&os
->os
->os_user_ptr_lock
);
2518 * If new properties are supplied, they are to completely
2519 * replace the existing ones, so stash away the existing ones.
2522 (void) dsl_prop_get_all(os
, &origprops
, TRUE
);
2524 dmu_objset_close(os
);
2527 if (zc
->zc_string
[0]) {
2528 error
= dmu_objset_open(zc
->zc_string
, DMU_OST_ANY
,
2529 DS_MODE_USER
| DS_MODE_READONLY
, &origin
);
2534 error
= dmu_recv_begin(tofs
, tosnap
, &zc
->zc_begin_record
,
2535 force
, origin
, zfsvfs
!= NULL
, &drc
);
2537 dmu_objset_close(origin
);
2542 * Reset properties. We do this before we receive the stream
2543 * so that the properties are applied to the new data.
2546 clear_props(tofs
, origprops
, props
);
2548 * XXX - Note, this is all-or-nothing; should be best-effort.
2550 (void) zfs_set_prop_nvlist(tofs
, props
);
2554 error
= dmu_recv_stream(&drc
, fp
->f_vnode
, &off
);
2556 if (error
== 0 && zfsvfs
) {
2561 osname
= kmem_alloc(MAXNAMELEN
, KM_SLEEP
);
2562 error
= zfs_suspend_fs(zfsvfs
, osname
, &mode
);
2566 error
= dmu_recv_end(&drc
);
2567 resume_err
= zfs_resume_fs(zfsvfs
, osname
, mode
);
2568 error
= error
? error
: resume_err
;
2570 dmu_recv_abort_cleanup(&drc
);
2572 kmem_free(osname
, MAXNAMELEN
);
2573 } else if (error
== 0) {
2574 error
= dmu_recv_end(&drc
);
2577 zc
->zc_cookie
= off
- fp
->f_offset
;
2578 if (VOP_SEEK(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
2582 * On error, restore the original props.
2584 if (error
&& props
) {
2585 clear_props(tofs
, props
, NULL
);
2586 (void) zfs_set_prop_nvlist(tofs
, origprops
);
2590 mutex_exit(&zfsvfs
->z_online_recv_lock
);
2591 VFS_RELE(zfsvfs
->z_vfs
);
2594 nvlist_free(origprops
);
2601 * zc_name name of snapshot to send
2602 * zc_value short name of incremental fromsnap (may be empty)
2603 * zc_cookie file descriptor to send stream to
2604 * zc_obj fromorigin flag (mutually exclusive with zc_value)
2609 zfs_ioc_send(zfs_cmd_t
*zc
)
2611 objset_t
*fromsnap
= NULL
;
2617 error
= dmu_objset_open(zc
->zc_name
, DMU_OST_ANY
,
2618 DS_MODE_USER
| DS_MODE_READONLY
, &tosnap
);
2622 if (zc
->zc_value
[0] != '\0') {
2626 buf
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
2627 (void) strncpy(buf
, zc
->zc_name
, MAXPATHLEN
);
2628 cp
= strchr(buf
, '@');
2631 (void) strncat(buf
, zc
->zc_value
, MAXPATHLEN
);
2632 error
= dmu_objset_open(buf
, DMU_OST_ANY
,
2633 DS_MODE_USER
| DS_MODE_READONLY
, &fromsnap
);
2634 kmem_free(buf
, MAXPATHLEN
);
2636 dmu_objset_close(tosnap
);
2641 fp
= getf(zc
->zc_cookie
);
2643 dmu_objset_close(tosnap
);
2645 dmu_objset_close(fromsnap
);
2650 error
= dmu_sendbackup(tosnap
, fromsnap
, zc
->zc_obj
, fp
->f_vnode
, &off
);
2652 if (VOP_SEEK(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
2654 releasef(zc
->zc_cookie
);
2656 dmu_objset_close(fromsnap
);
2657 dmu_objset_close(tosnap
);
2662 zfs_ioc_inject_fault(zfs_cmd_t
*zc
)
2666 error
= zio_inject_fault(zc
->zc_name
, (int)zc
->zc_guid
, &id
,
2667 &zc
->zc_inject_record
);
2670 zc
->zc_guid
= (uint64_t)id
;
2676 zfs_ioc_clear_fault(zfs_cmd_t
*zc
)
2678 return (zio_clear_fault((int)zc
->zc_guid
));
2682 zfs_ioc_inject_list_next(zfs_cmd_t
*zc
)
2684 int id
= (int)zc
->zc_guid
;
2687 error
= zio_inject_list_next(&id
, zc
->zc_name
, sizeof (zc
->zc_name
),
2688 &zc
->zc_inject_record
);
2696 zfs_ioc_error_log(zfs_cmd_t
*zc
)
2700 size_t count
= (size_t)zc
->zc_nvlist_dst_size
;
2702 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
2705 error
= spa_get_errlog(spa
, (void *)(uintptr_t)zc
->zc_nvlist_dst
,
2708 zc
->zc_nvlist_dst_size
= count
;
2710 zc
->zc_nvlist_dst_size
= spa_get_errlog_size(spa
);
2712 spa_close(spa
, FTAG
);
2718 zfs_ioc_clear(zfs_cmd_t
*zc
)
2725 * On zpool clear we also fix up missing slogs
2727 mutex_enter(&spa_namespace_lock
);
2728 spa
= spa_lookup(zc
->zc_name
);
2730 mutex_exit(&spa_namespace_lock
);
2733 if (spa
->spa_log_state
== SPA_LOG_MISSING
) {
2734 /* we need to let spa_open/spa_load clear the chains */
2735 spa
->spa_log_state
= SPA_LOG_CLEAR
;
2737 mutex_exit(&spa_namespace_lock
);
2739 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
2742 spa_vdev_state_enter(spa
);
2744 if (zc
->zc_guid
== 0) {
2747 vd
= spa_lookup_by_guid(spa
, zc
->zc_guid
, B_TRUE
);
2749 (void) spa_vdev_state_exit(spa
, NULL
, ENODEV
);
2750 spa_close(spa
, FTAG
);
2755 vdev_clear(spa
, vd
);
2757 (void) spa_vdev_state_exit(spa
, NULL
, 0);
2760 * Resume any suspended I/Os.
2764 spa_close(spa
, FTAG
);
2771 * zc_name name of filesystem
2772 * zc_value name of origin snapshot
2777 zfs_ioc_promote(zfs_cmd_t
*zc
)
2782 * We don't need to unmount *all* the origin fs's snapshots, but
2785 cp
= strchr(zc
->zc_value
, '@');
2788 (void) dmu_objset_find(zc
->zc_value
,
2789 zfs_unmount_snap
, NULL
, DS_FIND_SNAPSHOTS
);
2790 return (dsl_dataset_promote(zc
->zc_name
));
2794 * We don't want to have a hard dependency
2795 * against some special symbols in sharefs
2796 * nfs, and smbsrv. Determine them if needed when
2797 * the first file system is shared.
2798 * Neither sharefs, nfs or smbsrv are unloadable modules.
2800 int (*znfsexport_fs
)(void *arg
);
2801 int (*zshare_fs
)(enum sharefs_sys_op
, share_t
*, uint32_t);
2802 int (*zsmbexport_fs
)(void *arg
, boolean_t add_share
);
2804 int zfs_nfsshare_inited
;
2805 int zfs_smbshare_inited
;
2807 ddi_modhandle_t nfs_mod
;
2808 ddi_modhandle_t sharefs_mod
;
2809 ddi_modhandle_t smbsrv_mod
;
2810 kmutex_t zfs_share_lock
;
2817 ASSERT(MUTEX_HELD(&zfs_share_lock
));
2818 /* Both NFS and SMB shares also require sharetab support. */
2819 if (sharefs_mod
== NULL
&& ((sharefs_mod
=
2820 ddi_modopen("fs/sharefs",
2821 KRTLD_MODE_FIRST
, &error
)) == NULL
)) {
2824 if (zshare_fs
== NULL
&& ((zshare_fs
=
2825 (int (*)(enum sharefs_sys_op
, share_t
*, uint32_t))
2826 ddi_modsym(sharefs_mod
, "sharefs_impl", &error
)) == NULL
)) {
2833 zfs_ioc_share(zfs_cmd_t
*zc
)
2838 switch (zc
->zc_share
.z_sharetype
) {
2840 case ZFS_UNSHARE_NFS
:
2841 if (zfs_nfsshare_inited
== 0) {
2842 mutex_enter(&zfs_share_lock
);
2843 if (nfs_mod
== NULL
&& ((nfs_mod
= ddi_modopen("fs/nfs",
2844 KRTLD_MODE_FIRST
, &error
)) == NULL
)) {
2845 mutex_exit(&zfs_share_lock
);
2848 if (znfsexport_fs
== NULL
&&
2849 ((znfsexport_fs
= (int (*)(void *))
2851 "nfs_export", &error
)) == NULL
)) {
2852 mutex_exit(&zfs_share_lock
);
2855 error
= zfs_init_sharefs();
2857 mutex_exit(&zfs_share_lock
);
2860 zfs_nfsshare_inited
= 1;
2861 mutex_exit(&zfs_share_lock
);
2865 case ZFS_UNSHARE_SMB
:
2866 if (zfs_smbshare_inited
== 0) {
2867 mutex_enter(&zfs_share_lock
);
2868 if (smbsrv_mod
== NULL
&& ((smbsrv_mod
=
2869 ddi_modopen("drv/smbsrv",
2870 KRTLD_MODE_FIRST
, &error
)) == NULL
)) {
2871 mutex_exit(&zfs_share_lock
);
2874 if (zsmbexport_fs
== NULL
&& ((zsmbexport_fs
=
2875 (int (*)(void *, boolean_t
))ddi_modsym(smbsrv_mod
,
2876 "smb_server_share", &error
)) == NULL
)) {
2877 mutex_exit(&zfs_share_lock
);
2880 error
= zfs_init_sharefs();
2882 mutex_exit(&zfs_share_lock
);
2885 zfs_smbshare_inited
= 1;
2886 mutex_exit(&zfs_share_lock
);
2893 switch (zc
->zc_share
.z_sharetype
) {
2895 case ZFS_UNSHARE_NFS
:
2897 znfsexport_fs((void *)
2898 (uintptr_t)zc
->zc_share
.z_exportdata
))
2902 case ZFS_UNSHARE_SMB
:
2903 if (error
= zsmbexport_fs((void *)
2904 (uintptr_t)zc
->zc_share
.z_exportdata
,
2905 zc
->zc_share
.z_sharetype
== ZFS_SHARE_SMB
?
2906 B_TRUE
: B_FALSE
)) {
2912 opcode
= (zc
->zc_share
.z_sharetype
== ZFS_SHARE_NFS
||
2913 zc
->zc_share
.z_sharetype
== ZFS_SHARE_SMB
) ?
2914 SHAREFS_ADD
: SHAREFS_REMOVE
;
2917 * Add or remove share from sharetab
2919 error
= zshare_fs(opcode
,
2920 (void *)(uintptr_t)zc
->zc_share
.z_sharedata
,
2921 zc
->zc_share
.z_sharemax
);
2928 * pool create, destroy, and export don't log the history as part of
2929 * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
2930 * do the logging of those commands.
2932 static zfs_ioc_vec_t zfs_ioc_vec
[] = {
2933 { zfs_ioc_pool_create
, zfs_secpolicy_config
, POOL_NAME
, B_FALSE
},
2934 { zfs_ioc_pool_destroy
, zfs_secpolicy_config
, POOL_NAME
, B_FALSE
},
2935 { zfs_ioc_pool_import
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
},
2936 { zfs_ioc_pool_export
, zfs_secpolicy_config
, POOL_NAME
, B_FALSE
},
2937 { zfs_ioc_pool_configs
, zfs_secpolicy_none
, NO_NAME
, B_FALSE
},
2938 { zfs_ioc_pool_stats
, zfs_secpolicy_read
, POOL_NAME
, B_FALSE
},
2939 { zfs_ioc_pool_tryimport
, zfs_secpolicy_config
, NO_NAME
, B_FALSE
},
2940 { zfs_ioc_pool_scrub
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
},
2941 { zfs_ioc_pool_freeze
, zfs_secpolicy_config
, NO_NAME
, B_FALSE
},
2942 { zfs_ioc_pool_upgrade
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
},
2943 { zfs_ioc_pool_get_history
, zfs_secpolicy_config
, POOL_NAME
, B_FALSE
},
2944 { zfs_ioc_vdev_add
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
},
2945 { zfs_ioc_vdev_remove
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
},
2946 { zfs_ioc_vdev_set_state
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
},
2947 { zfs_ioc_vdev_attach
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
},
2948 { zfs_ioc_vdev_detach
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
},
2949 { zfs_ioc_vdev_setpath
, zfs_secpolicy_config
, POOL_NAME
, B_FALSE
},
2950 { zfs_ioc_objset_stats
, zfs_secpolicy_read
, DATASET_NAME
, B_FALSE
},
2951 { zfs_ioc_objset_zplprops
, zfs_secpolicy_read
, DATASET_NAME
, B_FALSE
},
2952 { zfs_ioc_dataset_list_next
, zfs_secpolicy_read
,
2953 DATASET_NAME
, B_FALSE
},
2954 { zfs_ioc_snapshot_list_next
, zfs_secpolicy_read
,
2955 DATASET_NAME
, B_FALSE
},
2956 { zfs_ioc_set_prop
, zfs_secpolicy_none
, DATASET_NAME
, B_TRUE
},
2957 { zfs_ioc_create_minor
, zfs_secpolicy_minor
, DATASET_NAME
, B_FALSE
},
2958 { zfs_ioc_remove_minor
, zfs_secpolicy_minor
, DATASET_NAME
, B_FALSE
},
2959 { zfs_ioc_create
, zfs_secpolicy_create
, DATASET_NAME
, B_TRUE
},
2960 { zfs_ioc_destroy
, zfs_secpolicy_destroy
, DATASET_NAME
, B_TRUE
},
2961 { zfs_ioc_rollback
, zfs_secpolicy_rollback
, DATASET_NAME
, B_TRUE
},
2962 { zfs_ioc_rename
, zfs_secpolicy_rename
, DATASET_NAME
, B_TRUE
},
2963 { zfs_ioc_recv
, zfs_secpolicy_receive
, DATASET_NAME
, B_TRUE
},
2964 { zfs_ioc_send
, zfs_secpolicy_send
, DATASET_NAME
, B_TRUE
},
2965 { zfs_ioc_inject_fault
, zfs_secpolicy_inject
, NO_NAME
, B_FALSE
},
2966 { zfs_ioc_clear_fault
, zfs_secpolicy_inject
, NO_NAME
, B_FALSE
},
2967 { zfs_ioc_inject_list_next
, zfs_secpolicy_inject
, NO_NAME
, B_FALSE
},
2968 { zfs_ioc_error_log
, zfs_secpolicy_inject
, POOL_NAME
, B_FALSE
},
2969 { zfs_ioc_clear
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
},
2970 { zfs_ioc_promote
, zfs_secpolicy_promote
, DATASET_NAME
, B_TRUE
},
2971 { zfs_ioc_destroy_snaps
, zfs_secpolicy_destroy
, DATASET_NAME
, B_TRUE
},
2972 { zfs_ioc_snapshot
, zfs_secpolicy_snapshot
, DATASET_NAME
, B_TRUE
},
2973 { zfs_ioc_dsobj_to_dsname
, zfs_secpolicy_config
, POOL_NAME
, B_FALSE
},
2974 { zfs_ioc_obj_to_path
, zfs_secpolicy_config
, NO_NAME
, B_FALSE
},
2975 { zfs_ioc_pool_set_props
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
},
2976 { zfs_ioc_pool_get_props
, zfs_secpolicy_read
, POOL_NAME
, B_FALSE
},
2977 { zfs_ioc_set_fsacl
, zfs_secpolicy_fsacl
, DATASET_NAME
, B_TRUE
},
2978 { zfs_ioc_get_fsacl
, zfs_secpolicy_read
, DATASET_NAME
, B_FALSE
},
2979 { zfs_ioc_iscsi_perm_check
, zfs_secpolicy_iscsi
,
2980 DATASET_NAME
, B_FALSE
},
2981 { zfs_ioc_share
, zfs_secpolicy_share
, DATASET_NAME
, B_FALSE
},
2982 { zfs_ioc_inherit_prop
, zfs_secpolicy_inherit
, DATASET_NAME
, B_TRUE
},
2986 zfsdev_ioctl(dev_t dev
, int cmd
, intptr_t arg
, int flag
, cred_t
*cr
, int *rvalp
)
2992 if (getminor(dev
) != 0)
2993 return (zvol_ioctl(dev
, cmd
, arg
, flag
, cr
, rvalp
));
2995 vec
= cmd
- ZFS_IOC
;
2996 ASSERT3U(getmajor(dev
), ==, ddi_driver_major(zfs_dip
));
2998 if (vec
>= sizeof (zfs_ioc_vec
) / sizeof (zfs_ioc_vec
[0]))
3001 zc
= kmem_zalloc(sizeof (zfs_cmd_t
), KM_SLEEP
);
3003 error
= xcopyin((void *)arg
, zc
, sizeof (zfs_cmd_t
));
3006 error
= zfs_ioc_vec
[vec
].zvec_secpolicy(zc
, cr
);
3009 * Ensure that all pool/dataset names are valid before we pass down to
3013 zc
->zc_name
[sizeof (zc
->zc_name
) - 1] = '\0';
3014 switch (zfs_ioc_vec
[vec
].zvec_namecheck
) {
3016 if (pool_namecheck(zc
->zc_name
, NULL
, NULL
) != 0)
3021 if (dataset_namecheck(zc
->zc_name
, NULL
, NULL
) != 0)
3031 error
= zfs_ioc_vec
[vec
].zvec_func(zc
);
3033 rc
= xcopyout(zc
, (void *)arg
, sizeof (zfs_cmd_t
));
3036 if (zfs_ioc_vec
[vec
].zvec_his_log
== B_TRUE
)
3037 zfs_log_history(zc
);
3040 kmem_free(zc
, sizeof (zfs_cmd_t
));
3045 zfs_attach(dev_info_t
*dip
, ddi_attach_cmd_t cmd
)
3047 if (cmd
!= DDI_ATTACH
)
3048 return (DDI_FAILURE
);
3050 if (ddi_create_minor_node(dip
, "zfs", S_IFCHR
, 0,
3051 DDI_PSEUDO
, 0) == DDI_FAILURE
)
3052 return (DDI_FAILURE
);
3056 ddi_report_dev(dip
);
3058 return (DDI_SUCCESS
);
3062 zfs_detach(dev_info_t
*dip
, ddi_detach_cmd_t cmd
)
3064 if (spa_busy() || zfs_busy() || zvol_busy())
3065 return (DDI_FAILURE
);
3067 if (cmd
!= DDI_DETACH
)
3068 return (DDI_FAILURE
);
3072 ddi_prop_remove_all(dip
);
3073 ddi_remove_minor_node(dip
, NULL
);
3075 return (DDI_SUCCESS
);
3080 zfs_info(dev_info_t
*dip
, ddi_info_cmd_t infocmd
, void *arg
, void **result
)
3083 case DDI_INFO_DEVT2DEVINFO
:
3085 return (DDI_SUCCESS
);
3087 case DDI_INFO_DEVT2INSTANCE
:
3088 *result
= (void *)0;
3089 return (DDI_SUCCESS
);
3092 return (DDI_FAILURE
);
3096 * OK, so this is a little weird.
3098 * /dev/zfs is the control node, i.e. minor 0.
3099 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
3101 * /dev/zfs has basically nothing to do except serve up ioctls,
3102 * so most of the standard driver entry points are in zvol.c.
3104 static struct cb_ops zfs_cb_ops
= {
3105 zvol_open
, /* open */
3106 zvol_close
, /* close */
3107 zvol_strategy
, /* strategy */
3109 zvol_dump
, /* dump */
3110 zvol_read
, /* read */
3111 zvol_write
, /* write */
3112 zfsdev_ioctl
, /* ioctl */
3116 nochpoll
, /* poll */
3117 ddi_prop_op
, /* prop_op */
3118 NULL
, /* streamtab */
3119 D_NEW
| D_MP
| D_64BIT
, /* Driver compatibility flag */
3120 CB_REV
, /* version */
3121 nodev
, /* async read */
3122 nodev
, /* async write */
3125 static struct dev_ops zfs_dev_ops
= {
3126 DEVO_REV
, /* version */
3128 zfs_info
, /* info */
3129 nulldev
, /* identify */
3130 nulldev
, /* probe */
3131 zfs_attach
, /* attach */
3132 zfs_detach
, /* detach */
3134 &zfs_cb_ops
, /* driver operations */
3135 NULL
, /* no bus operations */
3137 ddi_quiesce_not_needed
, /* quiesce */
3140 static struct modldrv zfs_modldrv
= {
3146 static struct modlinkage modlinkage
= {
3148 (void *)&zfs_modlfs
,
3149 (void *)&zfs_modldrv
,
3154 uint_t zfs_fsyncer_key
;
3155 extern uint_t rrw_tsd_key
;
3162 spa_init(FREAD
| FWRITE
);
3166 if ((error
= mod_install(&modlinkage
)) != 0) {
3173 tsd_create(&zfs_fsyncer_key
, NULL
);
3174 tsd_create(&rrw_tsd_key
, NULL
);
3176 error
= ldi_ident_from_mod(&modlinkage
, &zfs_li
);
3178 mutex_init(&zfs_share_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
3188 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled
)
3191 if ((error
= mod_remove(&modlinkage
)) != 0)
3197 if (zfs_nfsshare_inited
)
3198 (void) ddi_modclose(nfs_mod
);
3199 if (zfs_smbshare_inited
)
3200 (void) ddi_modclose(smbsrv_mod
);
3201 if (zfs_nfsshare_inited
|| zfs_smbshare_inited
)
3202 (void) ddi_modclose(sharefs_mod
);
3204 tsd_destroy(&zfs_fsyncer_key
);
3205 ldi_ident_release(zfs_li
);
3207 mutex_destroy(&zfs_share_lock
);
3213 _info(struct modinfo
*modinfop
)
3215 return (mod_info(&modlinkage
, modinfop
));