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 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "@(#)zfs_ioctl.c 1.61 08/04/27 SMI"
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/errno.h>
33 #include <sys/modctl.h>
38 #include <sys/cmn_err.h>
40 #include <sys/zfs_ioctl.h>
41 #include <sys/zfs_znode.h>
44 #include <sys/spa_impl.h>
46 #include <sys/vdev_impl.h>
48 #include <sys/dsl_dir.h>
49 #include <sys/dsl_dataset.h>
50 #include <sys/dsl_prop.h>
51 #include <sys/dsl_deleg.h>
52 #include <sys/dmu_objset.h>
54 #include <sys/sunddi.h>
55 #include <sys/sunldi.h>
56 #include <sys/policy.h>
58 #include <sys/nvpair.h>
59 #include <sys/pathname.h>
60 #include <sys/mount.h>
62 #include <sys/fs/zfs.h>
63 #include <sys/zfs_ctldir.h>
64 #include <sys/zfs_dir.h>
66 #include <sharefs/share.h>
67 #include <sys/dmu_objset.h>
69 #include "zfs_namecheck.h"
71 #include "zfs_deleg.h"
73 extern struct modlfs zfs_modlfs
;
75 extern void zfs_init(void);
76 extern void zfs_fini(void);
78 ldi_ident_t zfs_li
= NULL
;
81 typedef int zfs_ioc_func_t(zfs_cmd_t
*);
82 typedef int zfs_secpolicy_func_t(zfs_cmd_t
*, cred_t
*);
84 typedef struct zfs_ioc_vec
{
85 zfs_ioc_func_t
*zvec_func
;
86 zfs_secpolicy_func_t
*zvec_secpolicy
;
92 boolean_t zvec_his_log
;
95 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
97 __dprintf(const char *file
, const char *func
, int line
, const char *fmt
, ...)
104 * Get rid of annoying "../common/" prefix to filename.
106 newfile
= strrchr(file
, '/');
107 if (newfile
!= NULL
) {
108 newfile
= newfile
+ 1; /* Get rid of leading / */
114 (void) vsnprintf(buf
, sizeof (buf
), fmt
, adx
);
118 * To get this data, use the zfs-dprintf probe as so:
119 * dtrace -q -n 'zfs-dprintf \
120 * /stringof(arg0) == "dbuf.c"/ \
121 * {printf("%s: %s", stringof(arg1), stringof(arg3))}'
123 * arg1 = function name
127 DTRACE_PROBE4(zfs__dprintf
,
128 char *, newfile
, char *, func
, int, line
, char *, buf
);
132 history_str_free(char *buf
)
134 kmem_free(buf
, HIS_MAX_RECORD_LEN
);
138 history_str_get(zfs_cmd_t
*zc
)
142 if (zc
->zc_history
== NULL
)
145 buf
= kmem_alloc(HIS_MAX_RECORD_LEN
, KM_SLEEP
);
146 if (copyinstr((void *)(uintptr_t)zc
->zc_history
,
147 buf
, HIS_MAX_RECORD_LEN
, NULL
) != 0) {
148 history_str_free(buf
);
152 buf
[HIS_MAX_RECORD_LEN
-1] = '\0';
160 * Return non-zero if the spa version is less than requested version.
163 zfs_check_version(const char *name
, int version
)
168 if (spa_open(name
, &spa
, FTAG
) == 0) {
169 if (spa_version(spa
) < version
) {
170 spa_close(spa
, FTAG
);
173 spa_close(spa
, FTAG
);
181 * Return non-zero if the ZPL version is less than requested version.
184 zpl_check_version(const char *name
, int version
)
189 if (dmu_objset_open(name
, DMU_OST_ANY
,
190 DS_MODE_STANDARD
| DS_MODE_READONLY
, &os
) == 0) {
191 uint64_t propversion
;
193 if (zfs_get_zplprop(os
, ZFS_PROP_VERSION
,
194 &propversion
) == 0) {
195 rc
= !(propversion
>= version
);
197 dmu_objset_close(os
);
203 zfs_log_history(zfs_cmd_t
*zc
)
208 if ((buf
= history_str_get(zc
)) == NULL
)
211 if (spa_open(zc
->zc_name
, &spa
, FTAG
) == 0) {
212 if (spa_version(spa
) >= SPA_VERSION_ZPOOL_HISTORY
)
213 (void) spa_history_log(spa
, buf
, LOG_CMD_NORMAL
);
214 spa_close(spa
, FTAG
);
216 history_str_free(buf
);
220 * Policy for top-level read operations (list pools). Requires no privileges,
221 * and can be used in the local zone, as there is no associated dataset.
225 zfs_secpolicy_none(zfs_cmd_t
*zc
, cred_t
*cr
)
231 * Policy for dataset read operations (list children, get statistics). Requires
232 * no privileges, but must be visible in the local zone.
236 zfs_secpolicy_read(zfs_cmd_t
*zc
, cred_t
*cr
)
238 if (INGLOBALZONE(curproc
) ||
239 zone_dataset_visible(zc
->zc_name
, NULL
))
246 zfs_dozonecheck(const char *dataset
, cred_t
*cr
)
252 * The dataset must be visible by this zone -- check this first
253 * so they don't see EPERM on something they shouldn't know about.
255 if (!INGLOBALZONE(curproc
) &&
256 !zone_dataset_visible(dataset
, &writable
))
259 if (dsl_prop_get_integer(dataset
, "zoned", &zoned
, NULL
))
262 if (INGLOBALZONE(curproc
)) {
264 * If the fs is zoned, only root can access it from the
267 if (secpolicy_zfs(cr
) && zoned
)
271 * If we are in a local zone, the 'zoned' property must be set.
276 /* must be writable by this zone */
284 zfs_secpolicy_write_perms(const char *name
, const char *perm
, cred_t
*cr
)
288 error
= zfs_dozonecheck(name
, cr
);
290 error
= secpolicy_zfs(cr
);
292 error
= dsl_deleg_access(name
, perm
, cr
);
298 zfs_secpolicy_setprop(const char *name
, zfs_prop_t prop
, cred_t
*cr
)
301 * Check permissions for special properties.
306 * Disallow setting of 'zoned' from within a local zone.
308 if (!INGLOBALZONE(curproc
))
313 if (!INGLOBALZONE(curproc
)) {
315 char setpoint
[MAXNAMELEN
];
317 * Unprivileged users are allowed to modify the
318 * quota on things *under* (ie. contained by)
319 * the thing they own.
321 if (dsl_prop_get_integer(name
, "zoned", &zoned
,
324 if (!zoned
|| strlen(name
) <= strlen(setpoint
))
330 return (zfs_secpolicy_write_perms(name
, zfs_prop_to_name(prop
), cr
));
334 zfs_secpolicy_fsacl(zfs_cmd_t
*zc
, cred_t
*cr
)
338 error
= zfs_dozonecheck(zc
->zc_name
, cr
);
343 * permission to set permissions will be evaluated later in
344 * dsl_deleg_can_allow()
350 zfs_secpolicy_rollback(zfs_cmd_t
*zc
, cred_t
*cr
)
353 error
= zfs_secpolicy_write_perms(zc
->zc_name
,
354 ZFS_DELEG_PERM_ROLLBACK
, cr
);
356 error
= zfs_secpolicy_write_perms(zc
->zc_name
,
357 ZFS_DELEG_PERM_MOUNT
, cr
);
362 zfs_secpolicy_send(zfs_cmd_t
*zc
, cred_t
*cr
)
364 return (zfs_secpolicy_write_perms(zc
->zc_name
,
365 ZFS_DELEG_PERM_SEND
, cr
));
369 zfs_secpolicy_share(zfs_cmd_t
*zc
, cred_t
*cr
)
371 if (!INGLOBALZONE(curproc
))
374 if (secpolicy_nfs(cr
) == 0) {
380 if ((error
= lookupname(zc
->zc_value
, UIO_SYSSPACE
,
381 NO_FOLLOW
, NULL
, &vp
)) != 0)
384 /* Now make sure mntpnt and dataset are ZFS */
386 if (vp
->v_vfsp
->vfs_fstype
!= zfsfstype
||
387 (strcmp((char *)refstr_value(vp
->v_vfsp
->vfs_resource
),
388 zc
->zc_name
) != 0)) {
394 return (dsl_deleg_access(zc
->zc_name
,
395 ZFS_DELEG_PERM_SHARE
, cr
));
400 zfs_get_parent(const char *datasetname
, char *parent
, int parentsize
)
405 * Remove the @bla or /bla from the end of the name to get the parent.
407 (void) strncpy(parent
, datasetname
, parentsize
);
408 cp
= strrchr(parent
, '@');
412 cp
= strrchr(parent
, '/');
422 zfs_secpolicy_destroy_perms(const char *name
, cred_t
*cr
)
426 if ((error
= zfs_secpolicy_write_perms(name
,
427 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
430 return (zfs_secpolicy_write_perms(name
, ZFS_DELEG_PERM_DESTROY
, cr
));
434 zfs_secpolicy_destroy(zfs_cmd_t
*zc
, cred_t
*cr
)
436 return (zfs_secpolicy_destroy_perms(zc
->zc_name
, cr
));
440 * Must have sys_config privilege to check the iscsi permission
444 zfs_secpolicy_iscsi(zfs_cmd_t
*zc
, cred_t
*cr
)
446 return (secpolicy_zfs(cr
));
450 zfs_secpolicy_rename_perms(const char *from
, const char *to
, cred_t
*cr
)
452 char parentname
[MAXNAMELEN
];
455 if ((error
= zfs_secpolicy_write_perms(from
,
456 ZFS_DELEG_PERM_RENAME
, cr
)) != 0)
459 if ((error
= zfs_secpolicy_write_perms(from
,
460 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
463 if ((error
= zfs_get_parent(to
, parentname
,
464 sizeof (parentname
))) != 0)
467 if ((error
= zfs_secpolicy_write_perms(parentname
,
468 ZFS_DELEG_PERM_CREATE
, cr
)) != 0)
471 if ((error
= zfs_secpolicy_write_perms(parentname
,
472 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
479 zfs_secpolicy_rename(zfs_cmd_t
*zc
, cred_t
*cr
)
481 return (zfs_secpolicy_rename_perms(zc
->zc_name
, zc
->zc_value
, cr
));
485 zfs_secpolicy_promote(zfs_cmd_t
*zc
, cred_t
*cr
)
487 char parentname
[MAXNAMELEN
];
491 error
= zfs_secpolicy_write_perms(zc
->zc_name
,
492 ZFS_DELEG_PERM_PROMOTE
, cr
);
496 error
= dmu_objset_open(zc
->zc_name
, DMU_OST_ANY
,
497 DS_MODE_STANDARD
| DS_MODE_READONLY
, &clone
);
500 dsl_dataset_t
*pclone
= NULL
;
502 dd
= clone
->os
->os_dsl_dataset
->ds_dir
;
504 rw_enter(&dd
->dd_pool
->dp_config_rwlock
, RW_READER
);
505 error
= dsl_dataset_open_obj(dd
->dd_pool
,
506 dd
->dd_phys
->dd_origin_obj
, NULL
,
507 DS_MODE_NONE
, FTAG
, &pclone
);
508 rw_exit(&dd
->dd_pool
->dp_config_rwlock
);
510 dmu_objset_close(clone
);
514 error
= zfs_secpolicy_write_perms(zc
->zc_name
,
515 ZFS_DELEG_PERM_MOUNT
, cr
);
517 dsl_dataset_name(pclone
, parentname
);
518 dmu_objset_close(clone
);
519 dsl_dataset_close(pclone
, DS_MODE_NONE
, FTAG
);
521 error
= zfs_secpolicy_write_perms(parentname
,
522 ZFS_DELEG_PERM_PROMOTE
, cr
);
528 zfs_secpolicy_receive(zfs_cmd_t
*zc
, cred_t
*cr
)
532 if ((error
= zfs_secpolicy_write_perms(zc
->zc_name
,
533 ZFS_DELEG_PERM_RECEIVE
, cr
)) != 0)
536 if ((error
= zfs_secpolicy_write_perms(zc
->zc_name
,
537 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
540 return (zfs_secpolicy_write_perms(zc
->zc_name
,
541 ZFS_DELEG_PERM_CREATE
, cr
));
545 zfs_secpolicy_snapshot_perms(const char *name
, cred_t
*cr
)
549 if ((error
= zfs_secpolicy_write_perms(name
,
550 ZFS_DELEG_PERM_SNAPSHOT
, cr
)) != 0)
553 error
= zfs_secpolicy_write_perms(name
,
554 ZFS_DELEG_PERM_MOUNT
, cr
);
560 zfs_secpolicy_snapshot(zfs_cmd_t
*zc
, cred_t
*cr
)
563 return (zfs_secpolicy_snapshot_perms(zc
->zc_name
, cr
));
567 zfs_secpolicy_create(zfs_cmd_t
*zc
, cred_t
*cr
)
569 char parentname
[MAXNAMELEN
];
572 if ((error
= zfs_get_parent(zc
->zc_name
, parentname
,
573 sizeof (parentname
))) != 0)
576 if (zc
->zc_value
[0] != '\0') {
577 if ((error
= zfs_secpolicy_write_perms(zc
->zc_value
,
578 ZFS_DELEG_PERM_CLONE
, cr
)) != 0)
582 if ((error
= zfs_secpolicy_write_perms(parentname
,
583 ZFS_DELEG_PERM_CREATE
, cr
)) != 0)
586 error
= zfs_secpolicy_write_perms(parentname
,
587 ZFS_DELEG_PERM_MOUNT
, cr
);
593 zfs_secpolicy_umount(zfs_cmd_t
*zc
, cred_t
*cr
)
597 error
= secpolicy_fs_unmount(cr
, NULL
);
599 error
= dsl_deleg_access(zc
->zc_name
, ZFS_DELEG_PERM_MOUNT
, cr
);
605 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
606 * SYS_CONFIG privilege, which is not available in a local zone.
610 zfs_secpolicy_config(zfs_cmd_t
*zc
, cred_t
*cr
)
612 if (secpolicy_sys_config(cr
, B_FALSE
) != 0)
619 * Just like zfs_secpolicy_config, except that we will check for
620 * mount permission on the dataset for permission to create/remove
624 zfs_secpolicy_minor(zfs_cmd_t
*zc
, cred_t
*cr
)
626 if (secpolicy_sys_config(cr
, B_FALSE
) != 0) {
627 return (dsl_deleg_access(zc
->zc_name
,
628 ZFS_DELEG_PERM_MOUNT
, cr
));
635 * Policy for fault injection. Requires all privileges.
639 zfs_secpolicy_inject(zfs_cmd_t
*zc
, cred_t
*cr
)
641 return (secpolicy_zinject(cr
));
645 zfs_secpolicy_inherit(zfs_cmd_t
*zc
, cred_t
*cr
)
647 zfs_prop_t prop
= zfs_name_to_prop(zc
->zc_value
);
649 if (prop
== ZPROP_INVAL
) {
650 if (!zfs_prop_user(zc
->zc_value
))
652 return (zfs_secpolicy_write_perms(zc
->zc_name
,
653 ZFS_DELEG_PERM_USERPROP
, cr
));
655 if (!zfs_prop_inheritable(prop
))
657 return (zfs_secpolicy_setprop(zc
->zc_name
, prop
, cr
));
662 * Returns the nvlist as specified by the user in the zfs_cmd_t.
665 get_nvlist(uint64_t nvl
, uint64_t size
, nvlist_t
**nvp
)
669 nvlist_t
*list
= NULL
;
672 * Read in and unpack the user-supplied nvlist.
677 packed
= kmem_alloc(size
, KM_SLEEP
);
679 if ((error
= xcopyin((void *)(uintptr_t)nvl
, packed
, size
)) != 0) {
680 kmem_free(packed
, size
);
684 if ((error
= nvlist_unpack(packed
, size
, &list
, 0)) != 0) {
685 kmem_free(packed
, size
);
689 kmem_free(packed
, size
);
696 put_nvlist(zfs_cmd_t
*zc
, nvlist_t
*nvl
)
702 VERIFY(nvlist_size(nvl
, &size
, NV_ENCODE_NATIVE
) == 0);
704 if (size
> zc
->zc_nvlist_dst_size
) {
707 packed
= kmem_alloc(size
, KM_SLEEP
);
708 VERIFY(nvlist_pack(nvl
, &packed
, &size
, NV_ENCODE_NATIVE
,
710 error
= xcopyout(packed
, (void *)(uintptr_t)zc
->zc_nvlist_dst
,
712 kmem_free(packed
, size
);
715 zc
->zc_nvlist_dst_size
= size
;
720 zfs_ioc_pool_create(zfs_cmd_t
*zc
)
723 nvlist_t
*config
, *props
= NULL
;
726 if (error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
730 if (zc
->zc_nvlist_src_size
!= 0 && (error
=
731 get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
, &props
))) {
736 buf
= history_str_get(zc
);
738 error
= spa_create(zc
->zc_name
, config
, props
, buf
);
741 history_str_free(buf
);
752 zfs_ioc_pool_destroy(zfs_cmd_t
*zc
)
756 error
= spa_destroy(zc
->zc_name
);
761 zfs_ioc_pool_import(zfs_cmd_t
*zc
)
764 nvlist_t
*config
, *props
= NULL
;
767 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
771 if (zc
->zc_nvlist_src_size
!= 0 && (error
=
772 get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
, &props
))) {
777 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &guid
) != 0 ||
781 error
= spa_import(zc
->zc_name
, config
, props
);
792 zfs_ioc_pool_export(zfs_cmd_t
*zc
)
796 error
= spa_export(zc
->zc_name
, NULL
);
801 zfs_ioc_pool_configs(zfs_cmd_t
*zc
)
806 if ((configs
= spa_all_configs(&zc
->zc_cookie
)) == NULL
)
809 error
= put_nvlist(zc
, configs
);
811 nvlist_free(configs
);
817 zfs_ioc_pool_stats(zfs_cmd_t
*zc
)
823 error
= spa_get_stats(zc
->zc_name
, &config
, zc
->zc_value
,
824 sizeof (zc
->zc_value
));
826 if (config
!= NULL
) {
827 ret
= put_nvlist(zc
, config
);
831 * The config may be present even if 'error' is non-zero.
832 * In this case we return success, and preserve the real errno
835 zc
->zc_cookie
= error
;
844 * Try to import the given pool, returning pool stats as appropriate so that
845 * user land knows which devices are available and overall pool health.
848 zfs_ioc_pool_tryimport(zfs_cmd_t
*zc
)
850 nvlist_t
*tryconfig
, *config
;
853 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
857 config
= spa_tryimport(tryconfig
);
859 nvlist_free(tryconfig
);
864 error
= put_nvlist(zc
, config
);
871 zfs_ioc_pool_scrub(zfs_cmd_t
*zc
)
876 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
879 mutex_enter(&spa_namespace_lock
);
880 error
= spa_scrub(spa
, zc
->zc_cookie
, B_FALSE
);
881 mutex_exit(&spa_namespace_lock
);
883 spa_close(spa
, FTAG
);
889 zfs_ioc_pool_freeze(zfs_cmd_t
*zc
)
894 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
897 spa_close(spa
, FTAG
);
903 zfs_ioc_pool_upgrade(zfs_cmd_t
*zc
)
908 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
911 if (zc
->zc_cookie
< spa_version(spa
) || zc
->zc_cookie
> SPA_VERSION
) {
912 spa_close(spa
, FTAG
);
916 spa_upgrade(spa
, zc
->zc_cookie
);
917 spa_close(spa
, FTAG
);
923 zfs_ioc_pool_get_history(zfs_cmd_t
*zc
)
930 if ((size
= zc
->zc_history_len
) == 0)
933 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
936 if (spa_version(spa
) < SPA_VERSION_ZPOOL_HISTORY
) {
937 spa_close(spa
, FTAG
);
941 hist_buf
= kmem_alloc(size
, KM_SLEEP
);
942 if ((error
= spa_history_get(spa
, &zc
->zc_history_offset
,
943 &zc
->zc_history_len
, hist_buf
)) == 0) {
944 error
= xcopyout(hist_buf
,
945 (char *)(uintptr_t)zc
->zc_history
,
949 spa_close(spa
, FTAG
);
950 kmem_free(hist_buf
, size
);
955 zfs_ioc_dsobj_to_dsname(zfs_cmd_t
*zc
)
959 if (error
= dsl_dsobj_to_dsname(zc
->zc_name
, zc
->zc_obj
, zc
->zc_value
))
966 zfs_ioc_obj_to_path(zfs_cmd_t
*zc
)
971 if ((error
= dmu_objset_open(zc
->zc_name
, DMU_OST_ZFS
,
972 DS_MODE_NONE
| DS_MODE_READONLY
, &osp
)) != 0)
975 error
= zfs_obj_to_path(osp
, zc
->zc_obj
, zc
->zc_value
,
976 sizeof (zc
->zc_value
));
977 dmu_objset_close(osp
);
983 zfs_ioc_vdev_add(zfs_cmd_t
*zc
)
987 nvlist_t
*config
, **l2cache
, **spares
;
988 uint_t nl2cache
= 0, nspares
= 0;
990 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
994 error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
996 (void) nvlist_lookup_nvlist_array(config
, ZPOOL_CONFIG_L2CACHE
,
997 &l2cache
, &nl2cache
);
999 (void) nvlist_lookup_nvlist_array(config
, ZPOOL_CONFIG_SPARES
,
1003 * A root pool with concatenated devices is not supported.
1004 * Thus, can not add a device to a root pool.
1006 * Intent log device can not be added to a rootpool because
1007 * during mountroot, zil is replayed, a seperated log device
1008 * can not be accessed during the mountroot time.
1010 * l2cache and spare devices are ok to be added to a rootpool.
1012 if (spa
->spa_bootfs
!= 0 && nl2cache
== 0 && nspares
== 0) {
1013 spa_close(spa
, FTAG
);
1018 error
= spa_vdev_add(spa
, config
);
1019 nvlist_free(config
);
1021 spa_close(spa
, FTAG
);
1026 zfs_ioc_vdev_remove(zfs_cmd_t
*zc
)
1031 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1034 error
= spa_vdev_remove(spa
, zc
->zc_guid
, B_FALSE
);
1035 spa_close(spa
, FTAG
);
1040 zfs_ioc_vdev_set_state(zfs_cmd_t
*zc
)
1044 vdev_state_t newstate
= VDEV_STATE_UNKNOWN
;
1046 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1048 switch (zc
->zc_cookie
) {
1049 case VDEV_STATE_ONLINE
:
1050 error
= vdev_online(spa
, zc
->zc_guid
, zc
->zc_obj
, &newstate
);
1053 case VDEV_STATE_OFFLINE
:
1054 error
= vdev_offline(spa
, zc
->zc_guid
, zc
->zc_obj
);
1057 case VDEV_STATE_FAULTED
:
1058 error
= vdev_fault(spa
, zc
->zc_guid
);
1061 case VDEV_STATE_DEGRADED
:
1062 error
= vdev_degrade(spa
, zc
->zc_guid
);
1068 zc
->zc_cookie
= newstate
;
1069 spa_close(spa
, FTAG
);
1074 zfs_ioc_vdev_attach(zfs_cmd_t
*zc
)
1077 int replacing
= zc
->zc_cookie
;
1081 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1084 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1086 error
= spa_vdev_attach(spa
, zc
->zc_guid
, config
, replacing
);
1087 nvlist_free(config
);
1090 spa_close(spa
, FTAG
);
1095 zfs_ioc_vdev_detach(zfs_cmd_t
*zc
)
1100 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1103 error
= spa_vdev_detach(spa
, zc
->zc_guid
, B_FALSE
);
1105 spa_close(spa
, FTAG
);
1110 zfs_ioc_vdev_setpath(zfs_cmd_t
*zc
)
1113 char *path
= zc
->zc_value
;
1114 uint64_t guid
= zc
->zc_guid
;
1117 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1121 error
= spa_vdev_setpath(spa
, guid
, path
);
1122 spa_close(spa
, FTAG
);
1127 zfs_os_open_retry(char *name
, objset_t
**os
)
1132 error
= dmu_objset_open(name
, DMU_OST_ANY
,
1133 DS_MODE_STANDARD
| DS_MODE_READONLY
, os
);
1136 * This is ugly: dmu_objset_open() can return EBUSY if
1137 * the objset is held exclusively. Fortunately this hold is
1138 * only for a short while, so we retry here.
1139 * This avoids user code having to handle EBUSY,
1140 * for example for a "zfs list".
1142 if (error
== EBUSY
) {
1152 * zc_name name of filesystem
1153 * zc_nvlist_dst_size size of buffer for property nvlist
1156 * zc_objset_stats stats
1157 * zc_nvlist_dst property nvlist
1158 * zc_nvlist_dst_size size of property nvlist
1159 * zc_value alternate root
1162 zfs_ioc_objset_stats(zfs_cmd_t
*zc
)
1164 objset_t
*os
= NULL
;
1168 if ((error
= zfs_os_open_retry(zc
->zc_name
, &os
)) != 0)
1171 dmu_objset_fast_stat(os
, &zc
->zc_objset_stats
);
1173 if (zc
->zc_nvlist_dst
!= 0 &&
1174 (error
= dsl_prop_get_all(os
, &nv
)) == 0) {
1175 dmu_objset_stats(os
, nv
);
1177 * NB: zvol_get_stats() will read the objset contents,
1178 * which we aren't supposed to do with a
1179 * DS_MODE_STANDARD open, because it could be
1180 * inconsistent. So this is a bit of a workaround...
1182 if (!zc
->zc_objset_stats
.dds_inconsistent
) {
1183 if (dmu_objset_type(os
) == DMU_OST_ZVOL
)
1184 VERIFY(zvol_get_stats(os
, nv
) == 0);
1186 error
= put_nvlist(zc
, nv
);
1190 spa_altroot(dmu_objset_spa(os
), zc
->zc_value
, sizeof (zc
->zc_value
));
1192 dmu_objset_close(os
);
1197 nvl_add_zplprop(objset_t
*os
, nvlist_t
*props
, zfs_prop_t prop
)
1203 * zfs_get_zplprop() will either find a value or give us
1204 * the default value (if there is one).
1206 if ((error
= zfs_get_zplprop(os
, prop
, &value
)) != 0)
1208 VERIFY(nvlist_add_uint64(props
, zfs_prop_to_name(prop
), value
) == 0);
1214 * zc_name name of filesystem
1215 * zc_nvlist_dst_size size of buffer for zpl property nvlist
1218 * zc_nvlist_dst zpl property nvlist
1219 * zc_nvlist_dst_size size of zpl property nvlist
1222 zfs_ioc_objset_zplprops(zfs_cmd_t
*zc
)
1227 if ((err
= zfs_os_open_retry(zc
->zc_name
, &os
)) != 0)
1230 dmu_objset_fast_stat(os
, &zc
->zc_objset_stats
);
1233 * NB: nvl_add_zplprop() will read the objset contents,
1234 * which we aren't supposed to do with a DS_MODE_STANDARD
1235 * open, because it could be inconsistent.
1237 if (zc
->zc_nvlist_dst
!= NULL
&&
1238 !zc
->zc_objset_stats
.dds_inconsistent
&&
1239 dmu_objset_type(os
) == DMU_OST_ZFS
) {
1242 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1243 if ((err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_VERSION
)) == 0 &&
1244 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_NORMALIZE
)) == 0 &&
1245 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_UTF8ONLY
)) == 0 &&
1246 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_CASE
)) == 0)
1247 err
= put_nvlist(zc
, nv
);
1252 dmu_objset_close(os
);
1258 * zc_name name of filesystem
1259 * zc_cookie zap cursor
1260 * zc_nvlist_dst_size size of buffer for property nvlist
1263 * zc_name name of next filesystem
1264 * zc_objset_stats stats
1265 * zc_nvlist_dst property nvlist
1266 * zc_nvlist_dst_size size of property nvlist
1267 * zc_value alternate root
1270 zfs_ioc_dataset_list_next(zfs_cmd_t
*zc
)
1276 if ((error
= zfs_os_open_retry(zc
->zc_name
, &os
)) != 0) {
1277 if (error
== ENOENT
)
1282 p
= strrchr(zc
->zc_name
, '/');
1283 if (p
== NULL
|| p
[1] != '\0')
1284 (void) strlcat(zc
->zc_name
, "/", sizeof (zc
->zc_name
));
1285 p
= zc
->zc_name
+ strlen(zc
->zc_name
);
1288 error
= dmu_dir_list_next(os
,
1289 sizeof (zc
->zc_name
) - (p
- zc
->zc_name
), p
,
1290 NULL
, &zc
->zc_cookie
);
1291 if (error
== ENOENT
)
1293 } while (error
== 0 && !INGLOBALZONE(curproc
) &&
1294 !zone_dataset_visible(zc
->zc_name
, NULL
));
1297 * If it's a hidden dataset (ie. with a '$' in its name), don't
1298 * try to get stats for it. Userland will skip over it.
1300 if (error
== 0 && strchr(zc
->zc_name
, '$') == NULL
)
1301 error
= zfs_ioc_objset_stats(zc
); /* fill in the stats */
1303 dmu_objset_close(os
);
1309 * zc_name name of filesystem
1310 * zc_cookie zap cursor
1311 * zc_nvlist_dst_size size of buffer for property nvlist
1314 * zc_name name of next snapshot
1315 * zc_objset_stats stats
1316 * zc_nvlist_dst property nvlist
1317 * zc_nvlist_dst_size size of property nvlist
1318 * zc_value alternate root
1321 zfs_ioc_snapshot_list_next(zfs_cmd_t
*zc
)
1326 if ((error
= zfs_os_open_retry(zc
->zc_name
, &os
)) != 0) {
1327 if (error
== ENOENT
)
1333 * A dataset name of maximum length cannot have any snapshots,
1334 * so exit immediately.
1336 if (strlcat(zc
->zc_name
, "@", sizeof (zc
->zc_name
)) >= MAXNAMELEN
) {
1337 dmu_objset_close(os
);
1341 error
= dmu_snapshot_list_next(os
,
1342 sizeof (zc
->zc_name
) - strlen(zc
->zc_name
),
1343 zc
->zc_name
+ strlen(zc
->zc_name
), NULL
, &zc
->zc_cookie
, NULL
);
1344 if (error
== ENOENT
)
1348 error
= zfs_ioc_objset_stats(zc
); /* fill in the stats */
1350 /* if we failed, undo the @ that we tacked on to zc_name */
1352 *strchr(zc
->zc_name
, '@') = '\0';
1354 dmu_objset_close(os
);
1359 zfs_set_prop_nvlist(const char *name
, nvlist_t
*nvl
)
1367 * First validate permission to set all of the properties
1370 while ((elem
= nvlist_next_nvpair(nvl
, elem
)) != NULL
) {
1371 const char *propname
= nvpair_name(elem
);
1372 zfs_prop_t prop
= zfs_name_to_prop(propname
);
1374 if (prop
== ZPROP_INVAL
) {
1376 * If this is a user-defined property, it must be a
1377 * string, and there is no further validation to do.
1379 if (!zfs_prop_user(propname
) ||
1380 nvpair_type(elem
) != DATA_TYPE_STRING
)
1383 if (error
= zfs_secpolicy_write_perms(name
,
1384 ZFS_DELEG_PERM_USERPROP
, CRED()))
1389 if ((error
= zfs_secpolicy_setprop(name
, prop
, CRED())) != 0)
1393 * Check that this value is valid for this pool version
1396 case ZFS_PROP_COMPRESSION
:
1398 * If the user specified gzip compression, make sure
1399 * the SPA supports it. We ignore any errors here since
1400 * we'll catch them later.
1402 if (nvpair_type(elem
) == DATA_TYPE_UINT64
&&
1403 nvpair_value_uint64(elem
, &intval
) == 0 &&
1404 intval
>= ZIO_COMPRESS_GZIP_1
&&
1405 intval
<= ZIO_COMPRESS_GZIP_9
) {
1406 if (zfs_check_version(name
,
1407 SPA_VERSION_GZIP_COMPRESSION
))
1412 case ZFS_PROP_COPIES
:
1413 if (zfs_check_version(name
, SPA_VERSION_DITTO_BLOCKS
))
1417 case ZFS_PROP_SHARESMB
:
1418 if (zpl_check_version(name
, ZPL_VERSION_FUID
))
1422 if ((error
= zfs_secpolicy_setprop(name
, prop
, CRED())) != 0)
1427 while ((elem
= nvlist_next_nvpair(nvl
, elem
)) != NULL
) {
1428 const char *propname
= nvpair_name(elem
);
1429 zfs_prop_t prop
= zfs_name_to_prop(propname
);
1431 if (prop
== ZPROP_INVAL
) {
1432 VERIFY(nvpair_value_string(elem
, &strval
) == 0);
1433 error
= dsl_prop_set(name
, propname
, 1,
1434 strlen(strval
) + 1, strval
);
1442 case ZFS_PROP_QUOTA
:
1443 if ((error
= nvpair_value_uint64(elem
, &intval
)) != 0 ||
1444 (error
= dsl_dir_set_quota(name
, intval
)) != 0)
1448 case ZFS_PROP_REFQUOTA
:
1449 if ((error
= nvpair_value_uint64(elem
, &intval
)) != 0 ||
1450 (error
= dsl_dataset_set_quota(name
, intval
)) != 0)
1454 case ZFS_PROP_RESERVATION
:
1455 if ((error
= nvpair_value_uint64(elem
, &intval
)) != 0 ||
1456 (error
= dsl_dir_set_reservation(name
,
1461 case ZFS_PROP_REFRESERVATION
:
1462 if ((error
= nvpair_value_uint64(elem
, &intval
)) != 0 ||
1463 (error
= dsl_dataset_set_reservation(name
,
1468 case ZFS_PROP_VOLSIZE
:
1469 if ((error
= nvpair_value_uint64(elem
, &intval
)) != 0 ||
1470 (error
= zvol_set_volsize(name
,
1471 ddi_driver_major(zfs_dip
), intval
)) != 0)
1475 case ZFS_PROP_VOLBLOCKSIZE
:
1476 if ((error
= nvpair_value_uint64(elem
, &intval
)) != 0 ||
1477 (error
= zvol_set_volblocksize(name
, intval
)) != 0)
1481 case ZFS_PROP_VERSION
:
1482 if ((error
= nvpair_value_uint64(elem
, &intval
)) != 0 ||
1483 (error
= zfs_set_version(name
, intval
)) != 0)
1488 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
1489 if (zfs_prop_get_type(prop
) !=
1492 VERIFY(nvpair_value_string(elem
, &strval
) == 0);
1493 if ((error
= dsl_prop_set(name
,
1494 nvpair_name(elem
), 1, strlen(strval
) + 1,
1497 } else if (nvpair_type(elem
) == DATA_TYPE_UINT64
) {
1500 VERIFY(nvpair_value_uint64(elem
, &intval
) == 0);
1502 switch (zfs_prop_get_type(prop
)) {
1503 case PROP_TYPE_NUMBER
:
1505 case PROP_TYPE_STRING
:
1507 case PROP_TYPE_INDEX
:
1508 if (zfs_prop_index_to_string(prop
,
1509 intval
, &unused
) != 0)
1514 "unknown property type");
1518 if ((error
= dsl_prop_set(name
, propname
,
1519 8, 1, &intval
)) != 0)
1533 * zc_name name of filesystem
1534 * zc_value name of property to inherit
1535 * zc_nvlist_src{_size} nvlist of properties to apply
1540 zfs_ioc_set_prop(zfs_cmd_t
*zc
)
1545 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1549 error
= zfs_set_prop_nvlist(zc
->zc_name
, nvl
);
1557 * zc_name name of filesystem
1558 * zc_value name of property to inherit
1563 zfs_ioc_inherit_prop(zfs_cmd_t
*zc
)
1565 /* the property name has been validated by zfs_secpolicy_inherit() */
1566 return (dsl_prop_set(zc
->zc_name
, zc
->zc_value
, 0, 0, NULL
));
1570 zfs_ioc_pool_set_props(zfs_cmd_t
*zc
)
1576 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1580 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0) {
1585 error
= spa_prop_set(spa
, props
);
1588 spa_close(spa
, FTAG
);
1594 zfs_ioc_pool_get_props(zfs_cmd_t
*zc
)
1598 nvlist_t
*nvp
= NULL
;
1600 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1603 error
= spa_prop_get(spa
, &nvp
);
1605 if (error
== 0 && zc
->zc_nvlist_dst
!= NULL
)
1606 error
= put_nvlist(zc
, nvp
);
1610 spa_close(spa
, FTAG
);
1618 zfs_ioc_iscsi_perm_check(zfs_cmd_t
*zc
)
1628 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1633 if ((error
= nvlist_lookup_uint32(nvp
,
1634 ZFS_DELEG_PERM_UID
, &uid
)) != 0) {
1639 if ((error
= nvlist_lookup_uint32(nvp
,
1640 ZFS_DELEG_PERM_GID
, &gid
)) != 0) {
1645 if ((error
= nvlist_lookup_uint32_array(nvp
, ZFS_DELEG_PERM_GROUPS
,
1646 &groups
, &group_cnt
)) != 0) {
1650 usercred
= cralloc();
1651 if ((crsetugid(usercred
, uid
, gid
) != 0) ||
1652 (crsetgroups(usercred
, group_cnt
, (gid_t
*)groups
) != 0)) {
1658 error
= dsl_deleg_access(zc
->zc_name
,
1659 zfs_prop_to_name(ZFS_PROP_SHAREISCSI
), usercred
);
1666 * zc_name name of filesystem
1667 * zc_nvlist_src{_size} nvlist of delegated permissions
1668 * zc_perm_action allow/unallow flag
1673 zfs_ioc_set_fsacl(zfs_cmd_t
*zc
)
1676 nvlist_t
*fsaclnv
= NULL
;
1678 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1683 * Verify nvlist is constructed correctly
1685 if ((error
= zfs_deleg_verify_nvlist(fsaclnv
)) != 0) {
1686 nvlist_free(fsaclnv
);
1691 * If we don't have PRIV_SYS_MOUNT, then validate
1692 * that user is allowed to hand out each permission in
1696 error
= secpolicy_zfs(CRED());
1698 if (zc
->zc_perm_action
== B_FALSE
) {
1699 error
= dsl_deleg_can_allow(zc
->zc_name
,
1702 error
= dsl_deleg_can_unallow(zc
->zc_name
,
1708 error
= dsl_deleg_set(zc
->zc_name
, fsaclnv
, zc
->zc_perm_action
);
1710 nvlist_free(fsaclnv
);
1716 * zc_name name of filesystem
1719 * zc_nvlist_src{_size} nvlist of delegated permissions
1722 zfs_ioc_get_fsacl(zfs_cmd_t
*zc
)
1727 if ((error
= dsl_deleg_get(zc
->zc_name
, &nvp
)) == 0) {
1728 error
= put_nvlist(zc
, nvp
);
1737 * zc_name name of volume
1742 zfs_ioc_create_minor(zfs_cmd_t
*zc
)
1744 return (zvol_create_minor(zc
->zc_name
, ddi_driver_major(zfs_dip
)));
1749 * zc_name name of volume
1754 zfs_ioc_remove_minor(zfs_cmd_t
*zc
)
1756 return (zvol_remove_minor(zc
->zc_name
));
1760 * Search the vfs list for a specified resource. Returns a pointer to it
1761 * or NULL if no suitable entry is found. The caller of this routine
1762 * is responsible for releasing the returned vfs pointer.
1765 zfs_get_vfs(const char *resource
)
1768 struct vfs
*vfs_found
= NULL
;
1770 vfs_list_read_lock();
1773 if (strcmp(refstr_value(vfsp
->vfs_resource
), resource
) == 0) {
1778 vfsp
= vfsp
->vfs_next
;
1779 } while (vfsp
!= rootvfs
);
1786 zfs_create_cb(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
)
1788 zfs_creat_t
*zct
= arg
;
1790 zfs_create_fs(os
, cr
, zct
->zct_zplprops
, tx
);
1793 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
1797 * createprops list of properties requested by creator
1798 * dataset name of dataset we are creating
1801 * zplprops values for the zplprops we attach to the master node object
1803 * Determine the settings for utf8only, normalization and
1804 * casesensitivity. Specific values may have been requested by the
1805 * creator and/or we can inherit values from the parent dataset. If
1806 * the file system is of too early a vintage, a creator can not
1807 * request settings for these properties, even if the requested
1808 * setting is the default value. We don't actually want to create dsl
1809 * properties for these, so remove them from the source nvlist after
1813 zfs_fill_zplprops(const char *dataset
, nvlist_t
*createprops
,
1814 nvlist_t
*zplprops
, uint64_t zplver
, boolean_t
*is_ci
)
1817 char parentname
[MAXNAMELEN
];
1819 uint64_t sense
= ZFS_PROP_UNDEFINED
;
1820 uint64_t norm
= ZFS_PROP_UNDEFINED
;
1821 uint64_t u8
= ZFS_PROP_UNDEFINED
;
1824 ASSERT(zplprops
!= NULL
);
1826 (void) strlcpy(parentname
, dataset
, sizeof (parentname
));
1827 cp
= strrchr(parentname
, '/');
1832 * Pull out creator prop choices, if any.
1835 (void) nvlist_lookup_uint64(createprops
,
1836 zfs_prop_to_name(ZFS_PROP_NORMALIZE
), &norm
);
1837 (void) nvlist_remove_all(createprops
,
1838 zfs_prop_to_name(ZFS_PROP_NORMALIZE
));
1839 (void) nvlist_lookup_uint64(createprops
,
1840 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), &u8
);
1841 (void) nvlist_remove_all(createprops
,
1842 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
));
1843 (void) nvlist_lookup_uint64(createprops
,
1844 zfs_prop_to_name(ZFS_PROP_CASE
), &sense
);
1845 (void) nvlist_remove_all(createprops
,
1846 zfs_prop_to_name(ZFS_PROP_CASE
));
1850 * If the file system or pool is version is too "young" to
1851 * support normalization and the creator tried to set a value
1852 * for one of the props, error out. We only need check the
1853 * ZPL version because we've already checked by now that the
1854 * SPA version is compatible with the selected ZPL version.
1856 if (zplver
< ZPL_VERSION_NORMALIZATION
&&
1857 (norm
!= ZFS_PROP_UNDEFINED
|| u8
!= ZFS_PROP_UNDEFINED
||
1858 sense
!= ZFS_PROP_UNDEFINED
))
1862 * Put the version in the zplprops
1864 VERIFY(nvlist_add_uint64(zplprops
,
1865 zfs_prop_to_name(ZFS_PROP_VERSION
), zplver
) == 0);
1868 * Open parent object set so we can inherit zplprop values if
1871 if ((error
= zfs_os_open_retry(parentname
, &os
)) != 0)
1874 if (norm
== ZFS_PROP_UNDEFINED
)
1875 VERIFY(zfs_get_zplprop(os
, ZFS_PROP_NORMALIZE
, &norm
) == 0);
1876 VERIFY(nvlist_add_uint64(zplprops
,
1877 zfs_prop_to_name(ZFS_PROP_NORMALIZE
), norm
) == 0);
1880 * If we're normalizing, names must always be valid UTF-8 strings.
1884 if (u8
== ZFS_PROP_UNDEFINED
)
1885 VERIFY(zfs_get_zplprop(os
, ZFS_PROP_UTF8ONLY
, &u8
) == 0);
1886 VERIFY(nvlist_add_uint64(zplprops
,
1887 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), u8
) == 0);
1889 if (sense
== ZFS_PROP_UNDEFINED
)
1890 VERIFY(zfs_get_zplprop(os
, ZFS_PROP_CASE
, &sense
) == 0);
1891 VERIFY(nvlist_add_uint64(zplprops
,
1892 zfs_prop_to_name(ZFS_PROP_CASE
), sense
) == 0);
1895 *is_ci
= (sense
== ZFS_CASE_INSENSITIVE
);
1897 dmu_objset_close(os
);
1903 * zc_objset_type type of objset to create (fs vs zvol)
1904 * zc_name name of new objset
1905 * zc_value name of snapshot to clone from (may be empty)
1906 * zc_nvlist_src{_size} nvlist of properties to apply
1911 zfs_ioc_create(zfs_cmd_t
*zc
)
1916 nvlist_t
*nvprops
= NULL
;
1917 void (*cbfunc
)(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
);
1918 dmu_objset_type_t type
= zc
->zc_objset_type
;
1923 cbfunc
= zfs_create_cb
;
1927 cbfunc
= zvol_create_cb
;
1934 if (strchr(zc
->zc_name
, '@') ||
1935 strchr(zc
->zc_name
, '%'))
1938 if (zc
->zc_nvlist_src
!= NULL
&&
1939 (error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1943 zct
.zct_zplprops
= NULL
;
1944 zct
.zct_props
= nvprops
;
1946 if (zc
->zc_value
[0] != '\0') {
1948 * We're creating a clone of an existing snapshot.
1950 zc
->zc_value
[sizeof (zc
->zc_value
) - 1] = '\0';
1951 if (dataset_namecheck(zc
->zc_value
, NULL
, NULL
) != 0) {
1952 nvlist_free(nvprops
);
1956 error
= dmu_objset_open(zc
->zc_value
, type
,
1957 DS_MODE_STANDARD
| DS_MODE_READONLY
, &clone
);
1959 nvlist_free(nvprops
);
1963 error
= dmu_objset_create(zc
->zc_name
, type
, clone
, 0,
1966 dmu_objset_close(clone
);
1967 nvlist_free(nvprops
);
1970 dmu_objset_close(clone
);
1972 boolean_t is_insensitive
= B_FALSE
;
1974 if (cbfunc
== NULL
) {
1975 nvlist_free(nvprops
);
1979 if (type
== DMU_OST_ZVOL
) {
1980 uint64_t volsize
, volblocksize
;
1982 if (nvprops
== NULL
||
1983 nvlist_lookup_uint64(nvprops
,
1984 zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1986 nvlist_free(nvprops
);
1990 if ((error
= nvlist_lookup_uint64(nvprops
,
1991 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
1992 &volblocksize
)) != 0 && error
!= ENOENT
) {
1993 nvlist_free(nvprops
);
1998 volblocksize
= zfs_prop_default_numeric(
1999 ZFS_PROP_VOLBLOCKSIZE
);
2001 if ((error
= zvol_check_volblocksize(
2002 volblocksize
)) != 0 ||
2003 (error
= zvol_check_volsize(volsize
,
2004 volblocksize
)) != 0) {
2005 nvlist_free(nvprops
);
2008 } else if (type
== DMU_OST_ZFS
) {
2013 * Default ZPL version to non-FUID capable if the
2014 * pool is not upgraded to support FUIDs.
2016 if (zfs_check_version(zc
->zc_name
, SPA_VERSION_FUID
))
2017 version
= ZPL_VERSION_FUID
- 1;
2019 version
= ZPL_VERSION
;
2022 * Potentially override default ZPL version based
2023 * on creator's request.
2025 (void) nvlist_lookup_uint64(nvprops
,
2026 zfs_prop_to_name(ZFS_PROP_VERSION
), &version
);
2029 * Make sure version we ended up with is kosher
2031 if ((version
< ZPL_VERSION_INITIAL
||
2032 version
> ZPL_VERSION
) ||
2033 (version
>= ZPL_VERSION_FUID
&&
2034 zfs_check_version(zc
->zc_name
, SPA_VERSION_FUID
))) {
2035 nvlist_free(nvprops
);
2040 * We have to have normalization and
2041 * case-folding flags correct when we do the
2042 * file system creation, so go figure them out
2045 VERIFY(nvlist_alloc(&zct
.zct_zplprops
,
2046 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2047 error
= zfs_fill_zplprops(zc
->zc_name
, nvprops
,
2048 zct
.zct_zplprops
, version
, &is_insensitive
);
2050 nvlist_free(nvprops
);
2051 nvlist_free(zct
.zct_zplprops
);
2055 error
= dmu_objset_create(zc
->zc_name
, type
, NULL
,
2056 is_insensitive
? DS_FLAG_CI_DATASET
: 0, cbfunc
, &zct
);
2057 nvlist_free(zct
.zct_zplprops
);
2061 * It would be nice to do this atomically.
2064 if ((error
= zfs_set_prop_nvlist(zc
->zc_name
, nvprops
)) != 0)
2065 (void) dmu_objset_destroy(zc
->zc_name
);
2067 nvlist_free(nvprops
);
2073 * zc_name name of filesystem
2074 * zc_value short name of snapshot
2075 * zc_cookie recursive flag
2080 zfs_ioc_snapshot(zfs_cmd_t
*zc
)
2082 if (snapshot_namecheck(zc
->zc_value
, NULL
, NULL
) != 0)
2084 return (dmu_objset_snapshot(zc
->zc_name
,
2085 zc
->zc_value
, zc
->zc_cookie
));
2089 zfs_unmount_snap(char *name
, void *arg
)
2091 char *snapname
= arg
;
2096 * Snapshots (which are under .zfs control) must be unmounted
2097 * before they can be destroyed.
2101 (void) strcat(name
, "@");
2102 (void) strcat(name
, snapname
);
2103 vfsp
= zfs_get_vfs(name
);
2104 cp
= strchr(name
, '@');
2106 } else if (strchr(name
, '@')) {
2107 vfsp
= zfs_get_vfs(name
);
2112 * Always force the unmount for snapshots.
2114 int flag
= MS_FORCE
;
2117 if ((err
= vn_vfswlock(vfsp
->vfs_vnodecovered
)) != 0) {
2122 if ((err
= dounmount(vfsp
, flag
, kcred
)) != 0)
2130 * zc_name name of filesystem
2131 * zc_value short name of snapshot
2136 zfs_ioc_destroy_snaps(zfs_cmd_t
*zc
)
2140 if (snapshot_namecheck(zc
->zc_value
, NULL
, NULL
) != 0)
2142 err
= dmu_objset_find(zc
->zc_name
,
2143 zfs_unmount_snap
, zc
->zc_value
, DS_FIND_CHILDREN
);
2146 return (dmu_snapshots_destroy(zc
->zc_name
, zc
->zc_value
));
2151 * zc_name name of dataset to destroy
2152 * zc_objset_type type of objset
2157 zfs_ioc_destroy(zfs_cmd_t
*zc
)
2159 if (strchr(zc
->zc_name
, '@') && zc
->zc_objset_type
== DMU_OST_ZFS
) {
2160 int err
= zfs_unmount_snap(zc
->zc_name
, NULL
);
2165 return (dmu_objset_destroy(zc
->zc_name
));
2170 * zc_name name of dataset to rollback (to most recent snapshot)
2175 zfs_ioc_rollback(zfs_cmd_t
*zc
)
2179 zfsvfs_t
*zfsvfs
= NULL
;
2182 * Get the zfsvfs for the receiving objset. There
2183 * won't be one if we're operating on a zvol, if the
2184 * objset doesn't exist yet, or is not mounted.
2186 error
= dmu_objset_open(zc
->zc_name
, DMU_OST_ANY
,
2187 DS_MODE_STANDARD
, &os
);
2191 if (dmu_objset_type(os
) == DMU_OST_ZFS
) {
2192 mutex_enter(&os
->os
->os_user_ptr_lock
);
2193 zfsvfs
= dmu_objset_get_user(os
);
2195 VFS_HOLD(zfsvfs
->z_vfs
);
2196 mutex_exit(&os
->os
->os_user_ptr_lock
);
2199 if (zfsvfs
!= NULL
) {
2200 char osname
[MAXNAMELEN
];
2203 error
= zfs_suspend_fs(zfsvfs
, osname
, &mode
);
2207 ASSERT(strcmp(osname
, zc
->zc_name
) == 0);
2208 error
= dmu_objset_rollback(os
);
2209 resume_err
= zfs_resume_fs(zfsvfs
, osname
, mode
);
2210 error
= error
? error
: resume_err
;
2212 dmu_objset_close(os
);
2214 VFS_RELE(zfsvfs
->z_vfs
);
2216 error
= dmu_objset_rollback(os
);
2218 /* Note, the dmu_objset_rollback() closes the objset for us. */
2225 * zc_name old name of dataset
2226 * zc_value new name of dataset
2227 * zc_cookie recursive flag (only valid for snapshots)
2232 zfs_ioc_rename(zfs_cmd_t
*zc
)
2234 boolean_t recursive
= zc
->zc_cookie
& 1;
2236 zc
->zc_value
[sizeof (zc
->zc_value
) - 1] = '\0';
2237 if (dataset_namecheck(zc
->zc_value
, NULL
, NULL
) != 0 ||
2238 strchr(zc
->zc_value
, '%'))
2242 * Unmount snapshot unless we're doing a recursive rename,
2243 * in which case the dataset code figures out which snapshots
2246 if (!recursive
&& strchr(zc
->zc_name
, '@') != NULL
&&
2247 zc
->zc_objset_type
== DMU_OST_ZFS
) {
2248 int err
= zfs_unmount_snap(zc
->zc_name
, NULL
);
2253 return (dmu_objset_rename(zc
->zc_name
, zc
->zc_value
, recursive
));
2258 * zc_name name of containing filesystem
2259 * zc_nvlist_src{_size} nvlist of properties to apply
2260 * zc_value name of snapshot to create
2261 * zc_string name of clone origin (if DRR_FLAG_CLONE)
2262 * zc_cookie file descriptor to recv from
2263 * zc_begin_record the BEGIN record of the stream (not byteswapped)
2264 * zc_guid force flag
2267 * zc_cookie number of bytes read
2270 zfs_ioc_recv(zfs_cmd_t
*zc
)
2274 dmu_recv_cookie_t drc
;
2275 zfsvfs_t
*zfsvfs
= NULL
;
2276 boolean_t force
= (boolean_t
)zc
->zc_guid
;
2279 nvlist_t
*props
= NULL
;
2280 objset_t
*origin
= NULL
;
2282 char tofs
[ZFS_MAXNAMELEN
];
2284 if (dataset_namecheck(zc
->zc_value
, NULL
, NULL
) != 0 ||
2285 strchr(zc
->zc_value
, '@') == NULL
||
2286 strchr(zc
->zc_value
, '%'))
2289 (void) strcpy(tofs
, zc
->zc_value
);
2290 tosnap
= strchr(tofs
, '@');
2294 if (zc
->zc_nvlist_src
!= NULL
&&
2295 (error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2307 * Get the zfsvfs for the receiving objset. There
2308 * won't be one if we're operating on a zvol, if the
2309 * objset doesn't exist yet, or is not mounted.
2312 error
= dmu_objset_open(tofs
, DMU_OST_ZFS
,
2313 DS_MODE_STANDARD
| DS_MODE_READONLY
, &os
);
2315 mutex_enter(&os
->os
->os_user_ptr_lock
);
2316 zfsvfs
= dmu_objset_get_user(os
);
2317 if (zfsvfs
!= NULL
) {
2318 VFS_HOLD(zfsvfs
->z_vfs
);
2319 mutex_exit(&os
->os
->os_user_ptr_lock
);
2320 if (!mutex_tryenter(&zfsvfs
->z_online_recv_lock
)) {
2321 VFS_RELE(zfsvfs
->z_vfs
);
2322 dmu_objset_close(os
);
2328 mutex_exit(&os
->os
->os_user_ptr_lock
);
2330 dmu_objset_close(os
);
2333 if (zc
->zc_string
[0]) {
2334 error
= dmu_objset_open(zc
->zc_string
, DMU_OST_ANY
,
2335 DS_MODE_STANDARD
| DS_MODE_READONLY
, &origin
);
2337 if (zfsvfs
!= NULL
) {
2338 mutex_exit(&zfsvfs
->z_online_recv_lock
);
2339 VFS_RELE(zfsvfs
->z_vfs
);
2347 error
= dmu_recv_begin(tofs
, tosnap
, &zc
->zc_begin_record
,
2348 force
, origin
, zfsvfs
!= NULL
, &drc
);
2350 dmu_objset_close(origin
);
2352 if (zfsvfs
!= NULL
) {
2353 mutex_exit(&zfsvfs
->z_online_recv_lock
);
2354 VFS_RELE(zfsvfs
->z_vfs
);
2362 * If properties are supplied, they are to completely replace
2363 * the existing ones; "inherit" any existing properties.
2367 nvlist_t
*nv
= NULL
;
2369 error
= dmu_objset_open(tofs
, DMU_OST_ANY
,
2370 DS_MODE_STANDARD
| DS_MODE_READONLY
| DS_MODE_INCONSISTENT
,
2373 error
= dsl_prop_get_all(os
, &nv
);
2374 dmu_objset_close(os
);
2379 zc2
= kmem_alloc(sizeof (zfs_cmd_t
), KM_SLEEP
);
2381 (void) strcpy(zc2
->zc_name
, tofs
);
2382 for (elem
= nvlist_next_nvpair(nv
, NULL
); elem
;
2383 elem
= nvlist_next_nvpair(nv
, elem
)) {
2384 (void) strcpy(zc2
->zc_value
, nvpair_name(elem
));
2385 if (zfs_secpolicy_inherit(zc2
, CRED()) == 0)
2386 (void) zfs_ioc_inherit_prop(zc2
);
2388 kmem_free(zc2
, sizeof (zfs_cmd_t
));
2395 * Set properties. Note, we ignore errors. Would be better to
2396 * do best-effort in zfs_set_prop_nvlist, too.
2398 (void) zfs_set_prop_nvlist(tofs
, props
);
2402 error
= dmu_recv_stream(&drc
, fp
->f_vnode
, &off
);
2405 if (zfsvfs
!= NULL
) {
2406 char osname
[MAXNAMELEN
];
2409 error
= zfs_suspend_fs(zfsvfs
, osname
, &mode
);
2413 error
= dmu_recv_end(&drc
);
2414 resume_err
= zfs_resume_fs(zfsvfs
,
2416 error
= error
? error
: resume_err
;
2418 dmu_recv_abort_cleanup(&drc
);
2421 error
= dmu_recv_end(&drc
);
2424 if (zfsvfs
!= NULL
) {
2425 mutex_exit(&zfsvfs
->z_online_recv_lock
);
2426 VFS_RELE(zfsvfs
->z_vfs
);
2429 zc
->zc_cookie
= off
- fp
->f_offset
;
2430 if (VOP_SEEK(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
2439 * zc_name name of snapshot to send
2440 * zc_value short name of incremental fromsnap (may be empty)
2441 * zc_cookie file descriptor to send stream to
2442 * zc_obj fromorigin flag (mutually exclusive with zc_value)
2447 zfs_ioc_send(zfs_cmd_t
*zc
)
2449 objset_t
*fromsnap
= NULL
;
2455 error
= dmu_objset_open(zc
->zc_name
, DMU_OST_ANY
,
2456 DS_MODE_STANDARD
| DS_MODE_READONLY
, &tosnap
);
2460 if (zc
->zc_value
[0] != '\0') {
2461 char buf
[MAXPATHLEN
];
2464 (void) strncpy(buf
, zc
->zc_name
, sizeof (buf
));
2465 cp
= strchr(buf
, '@');
2468 (void) strncat(buf
, zc
->zc_value
, sizeof (buf
));
2469 error
= dmu_objset_open(buf
, DMU_OST_ANY
,
2470 DS_MODE_STANDARD
| DS_MODE_READONLY
, &fromsnap
);
2472 dmu_objset_close(tosnap
);
2477 fp
= getf(zc
->zc_cookie
);
2479 dmu_objset_close(tosnap
);
2481 dmu_objset_close(fromsnap
);
2486 error
= dmu_sendbackup(tosnap
, fromsnap
, zc
->zc_obj
, fp
->f_vnode
, &off
);
2488 if (VOP_SEEK(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
2490 releasef(zc
->zc_cookie
);
2492 dmu_objset_close(fromsnap
);
2493 dmu_objset_close(tosnap
);
2498 zfs_ioc_inject_fault(zfs_cmd_t
*zc
)
2502 error
= zio_inject_fault(zc
->zc_name
, (int)zc
->zc_guid
, &id
,
2503 &zc
->zc_inject_record
);
2506 zc
->zc_guid
= (uint64_t)id
;
2512 zfs_ioc_clear_fault(zfs_cmd_t
*zc
)
2514 return (zio_clear_fault((int)zc
->zc_guid
));
2518 zfs_ioc_inject_list_next(zfs_cmd_t
*zc
)
2520 int id
= (int)zc
->zc_guid
;
2523 error
= zio_inject_list_next(&id
, zc
->zc_name
, sizeof (zc
->zc_name
),
2524 &zc
->zc_inject_record
);
2532 zfs_ioc_error_log(zfs_cmd_t
*zc
)
2536 size_t count
= (size_t)zc
->zc_nvlist_dst_size
;
2538 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
2541 error
= spa_get_errlog(spa
, (void *)(uintptr_t)zc
->zc_nvlist_dst
,
2544 zc
->zc_nvlist_dst_size
= count
;
2546 zc
->zc_nvlist_dst_size
= spa_get_errlog_size(spa
);
2548 spa_close(spa
, FTAG
);
2554 zfs_ioc_clear(zfs_cmd_t
*zc
)
2561 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
2565 * Try to resume any I/Os which may have been suspended
2566 * as a result of a complete pool failure.
2568 if (!list_is_empty(&spa
->spa_zio_list
)) {
2569 if (zio_vdev_resume_io(spa
) != 0) {
2570 spa_close(spa
, FTAG
);
2575 txg
= spa_vdev_enter(spa
);
2577 if (zc
->zc_guid
== 0) {
2579 } else if ((vd
= spa_lookup_by_guid(spa
, zc
->zc_guid
)) == NULL
) {
2580 spa_aux_vdev_t
*sav
;
2584 * Check if this is an l2cache device.
2586 ASSERT(spa
!= NULL
);
2587 sav
= &spa
->spa_l2cache
;
2588 for (i
= 0; i
< sav
->sav_count
; i
++) {
2589 if (sav
->sav_vdevs
[i
]->vdev_guid
== zc
->zc_guid
) {
2590 vd
= sav
->sav_vdevs
[i
];
2596 (void) spa_vdev_exit(spa
, NULL
, txg
, ENODEV
);
2597 spa_close(spa
, FTAG
);
2602 vdev_clear(spa
, vd
, B_TRUE
);
2604 (void) spa_vdev_exit(spa
, NULL
, txg
, 0);
2606 spa_close(spa
, FTAG
);
2613 * zc_name name of filesystem
2614 * zc_value name of origin snapshot
2619 zfs_ioc_promote(zfs_cmd_t
*zc
)
2624 * We don't need to unmount *all* the origin fs's snapshots, but
2627 cp
= strchr(zc
->zc_value
, '@');
2630 (void) dmu_objset_find(zc
->zc_value
,
2631 zfs_unmount_snap
, NULL
, DS_FIND_SNAPSHOTS
);
2632 return (dsl_dataset_promote(zc
->zc_name
));
2636 * We don't want to have a hard dependency
2637 * against some special symbols in sharefs
2638 * nfs, and smbsrv. Determine them if needed when
2639 * the first file system is shared.
2640 * Neither sharefs, nfs or smbsrv are unloadable modules.
2642 int (*znfsexport_fs
)(void *arg
);
2643 int (*zshare_fs
)(enum sharefs_sys_op
, share_t
*, uint32_t);
2644 int (*zsmbexport_fs
)(void *arg
, boolean_t add_share
);
2646 int zfs_nfsshare_inited
;
2647 int zfs_smbshare_inited
;
2649 ddi_modhandle_t nfs_mod
;
2650 ddi_modhandle_t sharefs_mod
;
2651 ddi_modhandle_t smbsrv_mod
;
2652 kmutex_t zfs_share_lock
;
2659 ASSERT(MUTEX_HELD(&zfs_share_lock
));
2660 /* Both NFS and SMB shares also require sharetab support. */
2661 if (sharefs_mod
== NULL
&& ((sharefs_mod
=
2662 ddi_modopen("fs/sharefs",
2663 KRTLD_MODE_FIRST
, &error
)) == NULL
)) {
2666 if (zshare_fs
== NULL
&& ((zshare_fs
=
2667 (int (*)(enum sharefs_sys_op
, share_t
*, uint32_t))
2668 ddi_modsym(sharefs_mod
, "sharefs_impl", &error
)) == NULL
)) {
2675 zfs_ioc_share(zfs_cmd_t
*zc
)
2680 switch (zc
->zc_share
.z_sharetype
) {
2682 case ZFS_UNSHARE_NFS
:
2683 if (zfs_nfsshare_inited
== 0) {
2684 mutex_enter(&zfs_share_lock
);
2685 if (nfs_mod
== NULL
&& ((nfs_mod
= ddi_modopen("fs/nfs",
2686 KRTLD_MODE_FIRST
, &error
)) == NULL
)) {
2687 mutex_exit(&zfs_share_lock
);
2690 if (znfsexport_fs
== NULL
&&
2691 ((znfsexport_fs
= (int (*)(void *))
2693 "nfs_export", &error
)) == NULL
)) {
2694 mutex_exit(&zfs_share_lock
);
2697 error
= zfs_init_sharefs();
2699 mutex_exit(&zfs_share_lock
);
2702 zfs_nfsshare_inited
= 1;
2703 mutex_exit(&zfs_share_lock
);
2707 case ZFS_UNSHARE_SMB
:
2708 if (zfs_smbshare_inited
== 0) {
2709 mutex_enter(&zfs_share_lock
);
2710 if (smbsrv_mod
== NULL
&& ((smbsrv_mod
=
2711 ddi_modopen("drv/smbsrv",
2712 KRTLD_MODE_FIRST
, &error
)) == NULL
)) {
2713 mutex_exit(&zfs_share_lock
);
2716 if (zsmbexport_fs
== NULL
&& ((zsmbexport_fs
=
2717 (int (*)(void *, boolean_t
))ddi_modsym(smbsrv_mod
,
2718 "smb_server_share", &error
)) == NULL
)) {
2719 mutex_exit(&zfs_share_lock
);
2722 error
= zfs_init_sharefs();
2724 mutex_exit(&zfs_share_lock
);
2727 zfs_smbshare_inited
= 1;
2728 mutex_exit(&zfs_share_lock
);
2735 switch (zc
->zc_share
.z_sharetype
) {
2737 case ZFS_UNSHARE_NFS
:
2739 znfsexport_fs((void *)
2740 (uintptr_t)zc
->zc_share
.z_exportdata
))
2744 case ZFS_UNSHARE_SMB
:
2745 if (error
= zsmbexport_fs((void *)
2746 (uintptr_t)zc
->zc_share
.z_exportdata
,
2747 zc
->zc_share
.z_sharetype
== ZFS_SHARE_SMB
?
2748 B_TRUE
: B_FALSE
)) {
2754 opcode
= (zc
->zc_share
.z_sharetype
== ZFS_SHARE_NFS
||
2755 zc
->zc_share
.z_sharetype
== ZFS_SHARE_SMB
) ?
2756 SHAREFS_ADD
: SHAREFS_REMOVE
;
2759 * Add or remove share from sharetab
2761 error
= zshare_fs(opcode
,
2762 (void *)(uintptr_t)zc
->zc_share
.z_sharedata
,
2763 zc
->zc_share
.z_sharemax
);
2770 * pool create, destroy, and export don't log the history as part of
2771 * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
2772 * do the logging of those commands.
2774 static zfs_ioc_vec_t zfs_ioc_vec
[] = {
2775 { zfs_ioc_pool_create
, zfs_secpolicy_config
, POOL_NAME
, B_FALSE
},
2776 { zfs_ioc_pool_destroy
, zfs_secpolicy_config
, POOL_NAME
, B_FALSE
},
2777 { zfs_ioc_pool_import
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
},
2778 { zfs_ioc_pool_export
, zfs_secpolicy_config
, POOL_NAME
, B_FALSE
},
2779 { zfs_ioc_pool_configs
, zfs_secpolicy_none
, NO_NAME
, B_FALSE
},
2780 { zfs_ioc_pool_stats
, zfs_secpolicy_read
, POOL_NAME
, B_FALSE
},
2781 { zfs_ioc_pool_tryimport
, zfs_secpolicy_config
, NO_NAME
, B_FALSE
},
2782 { zfs_ioc_pool_scrub
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
},
2783 { zfs_ioc_pool_freeze
, zfs_secpolicy_config
, NO_NAME
, B_FALSE
},
2784 { zfs_ioc_pool_upgrade
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
},
2785 { zfs_ioc_pool_get_history
, zfs_secpolicy_config
, POOL_NAME
, B_FALSE
},
2786 { zfs_ioc_vdev_add
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
},
2787 { zfs_ioc_vdev_remove
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
},
2788 { zfs_ioc_vdev_set_state
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
},
2789 { zfs_ioc_vdev_attach
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
},
2790 { zfs_ioc_vdev_detach
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
},
2791 { zfs_ioc_vdev_setpath
, zfs_secpolicy_config
, POOL_NAME
, B_FALSE
},
2792 { zfs_ioc_objset_stats
, zfs_secpolicy_read
, DATASET_NAME
, B_FALSE
},
2793 { zfs_ioc_objset_zplprops
, zfs_secpolicy_read
, DATASET_NAME
, B_FALSE
},
2794 { zfs_ioc_dataset_list_next
, zfs_secpolicy_read
,
2795 DATASET_NAME
, B_FALSE
},
2796 { zfs_ioc_snapshot_list_next
, zfs_secpolicy_read
,
2797 DATASET_NAME
, B_FALSE
},
2798 { zfs_ioc_set_prop
, zfs_secpolicy_none
, DATASET_NAME
, B_TRUE
},
2799 { zfs_ioc_create_minor
, zfs_secpolicy_minor
, DATASET_NAME
, B_FALSE
},
2800 { zfs_ioc_remove_minor
, zfs_secpolicy_minor
, DATASET_NAME
, B_FALSE
},
2801 { zfs_ioc_create
, zfs_secpolicy_create
, DATASET_NAME
, B_TRUE
},
2802 { zfs_ioc_destroy
, zfs_secpolicy_destroy
, DATASET_NAME
, B_TRUE
},
2803 { zfs_ioc_rollback
, zfs_secpolicy_rollback
, DATASET_NAME
, B_TRUE
},
2804 { zfs_ioc_rename
, zfs_secpolicy_rename
, DATASET_NAME
, B_TRUE
},
2805 { zfs_ioc_recv
, zfs_secpolicy_receive
, DATASET_NAME
, B_TRUE
},
2806 { zfs_ioc_send
, zfs_secpolicy_send
, DATASET_NAME
, B_TRUE
},
2807 { zfs_ioc_inject_fault
, zfs_secpolicy_inject
, NO_NAME
, B_FALSE
},
2808 { zfs_ioc_clear_fault
, zfs_secpolicy_inject
, NO_NAME
, B_FALSE
},
2809 { zfs_ioc_inject_list_next
, zfs_secpolicy_inject
, NO_NAME
, B_FALSE
},
2810 { zfs_ioc_error_log
, zfs_secpolicy_inject
, POOL_NAME
, B_FALSE
},
2811 { zfs_ioc_clear
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
},
2812 { zfs_ioc_promote
, zfs_secpolicy_promote
, DATASET_NAME
, B_TRUE
},
2813 { zfs_ioc_destroy_snaps
, zfs_secpolicy_destroy
, DATASET_NAME
, B_TRUE
},
2814 { zfs_ioc_snapshot
, zfs_secpolicy_snapshot
, DATASET_NAME
, B_TRUE
},
2815 { zfs_ioc_dsobj_to_dsname
, zfs_secpolicy_config
, POOL_NAME
, B_FALSE
},
2816 { zfs_ioc_obj_to_path
, zfs_secpolicy_config
, NO_NAME
, B_FALSE
},
2817 { zfs_ioc_pool_set_props
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
},
2818 { zfs_ioc_pool_get_props
, zfs_secpolicy_read
, POOL_NAME
, B_FALSE
},
2819 { zfs_ioc_set_fsacl
, zfs_secpolicy_fsacl
, DATASET_NAME
, B_TRUE
},
2820 { zfs_ioc_get_fsacl
, zfs_secpolicy_read
, DATASET_NAME
, B_FALSE
},
2821 { zfs_ioc_iscsi_perm_check
, zfs_secpolicy_iscsi
,
2822 DATASET_NAME
, B_FALSE
},
2823 { zfs_ioc_share
, zfs_secpolicy_share
, DATASET_NAME
, B_FALSE
},
2824 { zfs_ioc_inherit_prop
, zfs_secpolicy_inherit
, DATASET_NAME
, B_TRUE
},
2828 zfsdev_ioctl(dev_t dev
, int cmd
, intptr_t arg
, int flag
, cred_t
*cr
, int *rvalp
)
2834 if (getminor(dev
) != 0)
2835 return (zvol_ioctl(dev
, cmd
, arg
, flag
, cr
, rvalp
));
2837 vec
= cmd
- ZFS_IOC
;
2838 ASSERT3U(getmajor(dev
), ==, ddi_driver_major(zfs_dip
));
2840 if (vec
>= sizeof (zfs_ioc_vec
) / sizeof (zfs_ioc_vec
[0]))
2843 zc
= kmem_zalloc(sizeof (zfs_cmd_t
), KM_SLEEP
);
2845 error
= xcopyin((void *)arg
, zc
, sizeof (zfs_cmd_t
));
2848 error
= zfs_ioc_vec
[vec
].zvec_secpolicy(zc
, cr
);
2851 * Ensure that all pool/dataset names are valid before we pass down to
2855 zc
->zc_name
[sizeof (zc
->zc_name
) - 1] = '\0';
2856 switch (zfs_ioc_vec
[vec
].zvec_namecheck
) {
2858 if (pool_namecheck(zc
->zc_name
, NULL
, NULL
) != 0)
2863 if (dataset_namecheck(zc
->zc_name
, NULL
, NULL
) != 0)
2873 error
= zfs_ioc_vec
[vec
].zvec_func(zc
);
2875 rc
= xcopyout(zc
, (void *)arg
, sizeof (zfs_cmd_t
));
2878 if (zfs_ioc_vec
[vec
].zvec_his_log
== B_TRUE
)
2879 zfs_log_history(zc
);
2882 kmem_free(zc
, sizeof (zfs_cmd_t
));
2887 zfs_attach(dev_info_t
*dip
, ddi_attach_cmd_t cmd
)
2889 if (cmd
!= DDI_ATTACH
)
2890 return (DDI_FAILURE
);
2892 if (ddi_create_minor_node(dip
, "zfs", S_IFCHR
, 0,
2893 DDI_PSEUDO
, 0) == DDI_FAILURE
)
2894 return (DDI_FAILURE
);
2898 ddi_report_dev(dip
);
2900 return (DDI_SUCCESS
);
2904 zfs_detach(dev_info_t
*dip
, ddi_detach_cmd_t cmd
)
2906 if (spa_busy() || zfs_busy() || zvol_busy())
2907 return (DDI_FAILURE
);
2909 if (cmd
!= DDI_DETACH
)
2910 return (DDI_FAILURE
);
2914 ddi_prop_remove_all(dip
);
2915 ddi_remove_minor_node(dip
, NULL
);
2917 return (DDI_SUCCESS
);
2922 zfs_info(dev_info_t
*dip
, ddi_info_cmd_t infocmd
, void *arg
, void **result
)
2925 case DDI_INFO_DEVT2DEVINFO
:
2927 return (DDI_SUCCESS
);
2929 case DDI_INFO_DEVT2INSTANCE
:
2930 *result
= (void *)0;
2931 return (DDI_SUCCESS
);
2934 return (DDI_FAILURE
);
2938 * OK, so this is a little weird.
2940 * /dev/zfs is the control node, i.e. minor 0.
2941 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
2943 * /dev/zfs has basically nothing to do except serve up ioctls,
2944 * so most of the standard driver entry points are in zvol.c.
2946 static struct cb_ops zfs_cb_ops
= {
2947 zvol_open
, /* open */
2948 zvol_close
, /* close */
2949 zvol_strategy
, /* strategy */
2951 zvol_dump
, /* dump */
2952 zvol_read
, /* read */
2953 zvol_write
, /* write */
2954 zfsdev_ioctl
, /* ioctl */
2958 nochpoll
, /* poll */
2959 ddi_prop_op
, /* prop_op */
2960 NULL
, /* streamtab */
2961 D_NEW
| D_MP
| D_64BIT
, /* Driver compatibility flag */
2962 CB_REV
, /* version */
2963 nodev
, /* async read */
2964 nodev
, /* async write */
2967 static struct dev_ops zfs_dev_ops
= {
2968 DEVO_REV
, /* version */
2970 zfs_info
, /* info */
2971 nulldev
, /* identify */
2972 nulldev
, /* probe */
2973 zfs_attach
, /* attach */
2974 zfs_detach
, /* detach */
2976 &zfs_cb_ops
, /* driver operations */
2977 NULL
/* no bus operations */
2980 static struct modldrv zfs_modldrv
= {
2981 &mod_driverops
, "ZFS storage pool version " SPA_VERSION_STRING
,
2985 static struct modlinkage modlinkage
= {
2987 (void *)&zfs_modlfs
,
2988 (void *)&zfs_modldrv
,
2993 uint_t zfs_fsyncer_key
;
2994 extern uint_t rrw_tsd_key
;
3001 spa_init(FREAD
| FWRITE
);
3005 if ((error
= mod_install(&modlinkage
)) != 0) {
3012 tsd_create(&zfs_fsyncer_key
, NULL
);
3013 tsd_create(&rrw_tsd_key
, NULL
);
3015 error
= ldi_ident_from_mod(&modlinkage
, &zfs_li
);
3017 mutex_init(&zfs_share_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
3027 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled
)
3030 if ((error
= mod_remove(&modlinkage
)) != 0)
3036 if (zfs_nfsshare_inited
)
3037 (void) ddi_modclose(nfs_mod
);
3038 if (zfs_smbshare_inited
)
3039 (void) ddi_modclose(smbsrv_mod
);
3040 if (zfs_nfsshare_inited
|| zfs_smbshare_inited
)
3041 (void) ddi_modclose(sharefs_mod
);
3043 tsd_destroy(&zfs_fsyncer_key
);
3044 ldi_ident_release(zfs_li
);
3046 mutex_destroy(&zfs_share_lock
);
3052 _info(struct modinfo
*modinfop
)
3054 return (mod_info(&modlinkage
, modinfop
));