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 #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>
66 #include <sys/callb.h>
67 #include <sys/taskq.h>
69 #include "zfs_namecheck.h"
71 #include "zfs_deleg.h"
74 static int zfs_cmajor
= -1;
75 static int zfs_bmajor
= -1;
76 #define ddi_driver_major(x) zfs_cmajor
79 extern struct modlfs zfs_modlfs
;
81 extern void zfs_init(void);
82 extern void zfs_fini(void);
84 ldi_ident_t zfs_li
= NULL
;
87 typedef int zfs_ioc_func_t(zfs_cmd_t
*);
88 typedef int zfs_secpolicy_func_t(zfs_cmd_t
*, cred_t
*);
90 typedef struct zfs_ioc_vec
{
91 zfs_ioc_func_t
*zvec_func
;
92 zfs_secpolicy_func_t
*zvec_secpolicy
;
98 boolean_t zvec_his_log
;
101 static void clear_props(char *dataset
, nvlist_t
*props
);
102 static int zfs_fill_zplprops_root(uint64_t, nvlist_t
*, nvlist_t
*,
104 int zfs_set_prop_nvlist(const char *, nvlist_t
*);
106 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
108 __dprintf(const char *file
, const char *func
, int line
, const char *fmt
, ...)
115 * Get rid of annoying "../common/" prefix to filename.
117 newfile
= strrchr(file
, '/');
118 if (newfile
!= NULL
) {
119 newfile
= newfile
+ 1; /* Get rid of leading / */
125 (void) vsnprintf(buf
, sizeof (buf
), fmt
, adx
);
129 * To get this data, use the zfs-dprintf probe as so:
130 * dtrace -q -n 'zfs-dprintf \
131 * /stringof(arg0) == "dbuf.c"/ \
132 * {printf("%s: %s", stringof(arg1), stringof(arg3))}'
134 * arg1 = function name
138 DTRACE_PROBE4(zfs__dprintf
,
139 char *, newfile
, char *, func
, int, line
, char *, buf
);
143 history_str_free(char *buf
)
145 kmem_free(buf
, HIS_MAX_RECORD_LEN
);
149 history_str_get(zfs_cmd_t
*zc
)
153 if (zc
->zc_history
== NULL
)
156 buf
= kmem_alloc(HIS_MAX_RECORD_LEN
, KM_SLEEP
);
157 if (copyinstr((void *)(uintptr_t)zc
->zc_history
,
158 buf
, HIS_MAX_RECORD_LEN
, NULL
) != 0) {
159 history_str_free(buf
);
163 buf
[HIS_MAX_RECORD_LEN
-1] = '\0';
169 * Check to see if the named dataset is currently defined as bootable
172 zfs_is_bootfs(const char *name
)
175 boolean_t ret
= B_FALSE
;
177 if (spa_open(name
, &spa
, FTAG
) == 0) {
178 if (spa
->spa_bootfs
) {
181 if (dmu_objset_open(name
, DMU_OST_ZFS
,
182 DS_MODE_USER
| DS_MODE_READONLY
, &os
) == 0) {
183 ret
= (dmu_objset_id(os
) == spa
->spa_bootfs
);
184 dmu_objset_close(os
);
187 spa_close(spa
, FTAG
);
193 * zfs_earlier_version
195 * Return non-zero if the spa version is less than requested version.
198 zfs_earlier_version(const char *name
, int version
)
202 if (spa_open(name
, &spa
, FTAG
) == 0) {
203 if (spa_version(spa
) < version
) {
204 spa_close(spa
, FTAG
);
207 spa_close(spa
, FTAG
);
213 * zpl_earlier_version
215 * Return TRUE if the ZPL version is less than requested version.
218 zpl_earlier_version(const char *name
, int version
)
221 boolean_t rc
= B_TRUE
;
223 if (dmu_objset_open(name
, DMU_OST_ANY
,
224 DS_MODE_USER
| DS_MODE_READONLY
, &os
) == 0) {
227 if (zfs_get_zplprop(os
, ZFS_PROP_VERSION
, &zplversion
) == 0)
228 rc
= zplversion
< version
;
229 dmu_objset_close(os
);
235 zfs_log_history(zfs_cmd_t
*zc
)
240 if ((buf
= history_str_get(zc
)) == NULL
)
243 if (spa_open(zc
->zc_name
, &spa
, FTAG
) == 0) {
244 if (spa_version(spa
) >= SPA_VERSION_ZPOOL_HISTORY
)
245 (void) spa_history_log(spa
, buf
, LOG_CMD_NORMAL
);
246 spa_close(spa
, FTAG
);
248 history_str_free(buf
);
252 * Policy for top-level read operations (list pools). Requires no privileges,
253 * and can be used in the local zone, as there is no associated dataset.
257 zfs_secpolicy_none(zfs_cmd_t
*zc
, cred_t
*cr
)
263 * Policy for dataset read operations (list children, get statistics). Requires
264 * no privileges, but must be visible in the local zone.
268 zfs_secpolicy_read(zfs_cmd_t
*zc
, cred_t
*cr
)
270 if (INGLOBALZONE(curproc
) ||
271 zone_dataset_visible(zc
->zc_name
, NULL
))
278 zfs_dozonecheck(const char *dataset
, cred_t
*cr
)
284 * The dataset must be visible by this zone -- check this first
285 * so they don't see EPERM on something they shouldn't know about.
287 if (!INGLOBALZONE(curproc
) &&
288 !zone_dataset_visible(dataset
, &writable
))
291 if (dsl_prop_get_integer(dataset
, "zoned", &zoned
, NULL
))
294 if (INGLOBALZONE(curproc
)) {
296 * If the fs is zoned, only root can access it from the
299 if (secpolicy_zfs(cr
) && zoned
)
303 * If we are in a local zone, the 'zoned' property must be set.
308 /* must be writable by this zone */
316 zfs_secpolicy_write_perms(const char *name
, const char *perm
, cred_t
*cr
)
320 error
= zfs_dozonecheck(name
, cr
);
322 error
= secpolicy_zfs(cr
);
324 error
= dsl_deleg_access(name
, perm
, cr
);
330 zfs_secpolicy_setprop(const char *name
, zfs_prop_t prop
, cred_t
*cr
)
333 * Check permissions for special properties.
338 * Disallow setting of 'zoned' from within a local zone.
340 if (!INGLOBALZONE(curproc
))
345 if (!INGLOBALZONE(curproc
)) {
347 char setpoint
[MAXNAMELEN
];
349 * Unprivileged users are allowed to modify the
350 * quota on things *under* (ie. contained by)
351 * the thing they own.
353 if (dsl_prop_get_integer(name
, "zoned", &zoned
,
356 if (!zoned
|| strlen(name
) <= strlen(setpoint
))
362 return (zfs_secpolicy_write_perms(name
, zfs_prop_to_name(prop
), cr
));
366 zfs_secpolicy_fsacl(zfs_cmd_t
*zc
, cred_t
*cr
)
370 error
= zfs_dozonecheck(zc
->zc_name
, cr
);
375 * permission to set permissions will be evaluated later in
376 * dsl_deleg_can_allow()
382 zfs_secpolicy_rollback(zfs_cmd_t
*zc
, cred_t
*cr
)
385 error
= zfs_secpolicy_write_perms(zc
->zc_name
,
386 ZFS_DELEG_PERM_ROLLBACK
, cr
);
388 error
= zfs_secpolicy_write_perms(zc
->zc_name
,
389 ZFS_DELEG_PERM_MOUNT
, cr
);
394 zfs_secpolicy_send(zfs_cmd_t
*zc
, cred_t
*cr
)
396 return (zfs_secpolicy_write_perms(zc
->zc_name
,
397 ZFS_DELEG_PERM_SEND
, cr
));
401 zfs_secpolicy_share(zfs_cmd_t
*zc
, cred_t
*cr
)
404 printf("XXX zfs_secpolicy_share write me\n");
407 if (!INGLOBALZONE(curproc
))
410 if (secpolicy_nfs(cr
) == 0) {
416 if ((error
= lookupname(zc
->zc_value
, UIO_SYSSPACE
,
417 NO_FOLLOW
, NULL
, &vp
)) != 0)
420 /* Now make sure mntpnt and dataset are ZFS */
422 if (vp
->v_vfsp
->vfs_fstype
!= zfsfstype
||
423 (strcmp((char *)refstr_value(vp
->v_vfsp
->vfs_resource
),
424 zc
->zc_name
) != 0)) {
430 return (dsl_deleg_access(zc
->zc_name
,
431 ZFS_DELEG_PERM_SHARE
, cr
));
433 #endif /* __NetBSD__ */
437 zfs_get_parent(const char *datasetname
, char *parent
, int parentsize
)
442 * Remove the @bla or /bla from the end of the name to get the parent.
444 (void) strncpy(parent
, datasetname
, parentsize
);
445 cp
= strrchr(parent
, '@');
449 cp
= strrchr(parent
, '/');
459 zfs_secpolicy_destroy_perms(const char *name
, cred_t
*cr
)
463 if ((error
= zfs_secpolicy_write_perms(name
,
464 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
467 return (zfs_secpolicy_write_perms(name
, ZFS_DELEG_PERM_DESTROY
, cr
));
471 zfs_secpolicy_destroy(zfs_cmd_t
*zc
, cred_t
*cr
)
473 return (zfs_secpolicy_destroy_perms(zc
->zc_name
, cr
));
477 * Must have sys_config privilege to check the iscsi permission
481 zfs_secpolicy_iscsi(zfs_cmd_t
*zc
, cred_t
*cr
)
483 return (secpolicy_zfs(cr
));
487 zfs_secpolicy_rename_perms(const char *from
, const char *to
, cred_t
*cr
)
489 char parentname
[MAXNAMELEN
];
492 if ((error
= zfs_secpolicy_write_perms(from
,
493 ZFS_DELEG_PERM_RENAME
, cr
)) != 0)
496 if ((error
= zfs_secpolicy_write_perms(from
,
497 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
500 if ((error
= zfs_get_parent(to
, parentname
,
501 sizeof (parentname
))) != 0)
504 if ((error
= zfs_secpolicy_write_perms(parentname
,
505 ZFS_DELEG_PERM_CREATE
, cr
)) != 0)
508 if ((error
= zfs_secpolicy_write_perms(parentname
,
509 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
516 zfs_secpolicy_rename(zfs_cmd_t
*zc
, cred_t
*cr
)
518 return (zfs_secpolicy_rename_perms(zc
->zc_name
, zc
->zc_value
, cr
));
522 zfs_secpolicy_promote(zfs_cmd_t
*zc
, cred_t
*cr
)
524 char parentname
[MAXNAMELEN
];
528 error
= zfs_secpolicy_write_perms(zc
->zc_name
,
529 ZFS_DELEG_PERM_PROMOTE
, cr
);
533 error
= dmu_objset_open(zc
->zc_name
, DMU_OST_ANY
,
534 DS_MODE_USER
| DS_MODE_READONLY
, &clone
);
537 dsl_dataset_t
*pclone
= NULL
;
539 dd
= clone
->os
->os_dsl_dataset
->ds_dir
;
541 rw_enter(&dd
->dd_pool
->dp_config_rwlock
, RW_READER
);
542 error
= dsl_dataset_hold_obj(dd
->dd_pool
,
543 dd
->dd_phys
->dd_origin_obj
, FTAG
, &pclone
);
544 rw_exit(&dd
->dd_pool
->dp_config_rwlock
);
546 dmu_objset_close(clone
);
550 error
= zfs_secpolicy_write_perms(zc
->zc_name
,
551 ZFS_DELEG_PERM_MOUNT
, cr
);
553 dsl_dataset_name(pclone
, parentname
);
554 dmu_objset_close(clone
);
555 dsl_dataset_rele(pclone
, FTAG
);
557 error
= zfs_secpolicy_write_perms(parentname
,
558 ZFS_DELEG_PERM_PROMOTE
, cr
);
564 zfs_secpolicy_receive(zfs_cmd_t
*zc
, cred_t
*cr
)
568 if ((error
= zfs_secpolicy_write_perms(zc
->zc_name
,
569 ZFS_DELEG_PERM_RECEIVE
, cr
)) != 0)
572 if ((error
= zfs_secpolicy_write_perms(zc
->zc_name
,
573 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
576 return (zfs_secpolicy_write_perms(zc
->zc_name
,
577 ZFS_DELEG_PERM_CREATE
, cr
));
581 zfs_secpolicy_snapshot_perms(const char *name
, cred_t
*cr
)
585 if ((error
= zfs_secpolicy_write_perms(name
,
586 ZFS_DELEG_PERM_SNAPSHOT
, cr
)) != 0)
589 error
= zfs_secpolicy_write_perms(name
,
590 ZFS_DELEG_PERM_MOUNT
, cr
);
596 zfs_secpolicy_snapshot(zfs_cmd_t
*zc
, cred_t
*cr
)
599 return (zfs_secpolicy_snapshot_perms(zc
->zc_name
, cr
));
603 zfs_secpolicy_create(zfs_cmd_t
*zc
, cred_t
*cr
)
605 char parentname
[MAXNAMELEN
];
608 if ((error
= zfs_get_parent(zc
->zc_name
, parentname
,
609 sizeof (parentname
))) != 0)
612 if (zc
->zc_value
[0] != '\0') {
613 if ((error
= zfs_secpolicy_write_perms(zc
->zc_value
,
614 ZFS_DELEG_PERM_CLONE
, cr
)) != 0)
618 if ((error
= zfs_secpolicy_write_perms(parentname
,
619 ZFS_DELEG_PERM_CREATE
, cr
)) != 0)
622 error
= zfs_secpolicy_write_perms(parentname
,
623 ZFS_DELEG_PERM_MOUNT
, cr
);
629 zfs_secpolicy_umount(zfs_cmd_t
*zc
, cred_t
*cr
)
633 error
= secpolicy_fs_unmount(cr
, NULL
);
635 error
= dsl_deleg_access(zc
->zc_name
, ZFS_DELEG_PERM_MOUNT
, cr
);
641 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
642 * SYS_CONFIG privilege, which is not available in a local zone.
646 zfs_secpolicy_config(zfs_cmd_t
*zc
, cred_t
*cr
)
648 if (secpolicy_sys_config(cr
, B_FALSE
) != 0)
655 * Just like zfs_secpolicy_config, except that we will check for
656 * mount permission on the dataset for permission to create/remove
660 zfs_secpolicy_minor(zfs_cmd_t
*zc
, cred_t
*cr
)
662 if (secpolicy_sys_config(cr
, B_FALSE
) != 0) {
663 return (dsl_deleg_access(zc
->zc_name
,
664 ZFS_DELEG_PERM_MOUNT
, cr
));
671 * Policy for fault injection. Requires all privileges.
675 zfs_secpolicy_inject(zfs_cmd_t
*zc
, cred_t
*cr
)
677 return (secpolicy_zinject(cr
));
681 zfs_secpolicy_inherit(zfs_cmd_t
*zc
, cred_t
*cr
)
683 zfs_prop_t prop
= zfs_name_to_prop(zc
->zc_value
);
685 if (prop
== ZPROP_INVAL
) {
686 if (!zfs_prop_user(zc
->zc_value
))
688 return (zfs_secpolicy_write_perms(zc
->zc_name
,
689 ZFS_DELEG_PERM_USERPROP
, cr
));
691 if (!zfs_prop_inheritable(prop
))
693 return (zfs_secpolicy_setprop(zc
->zc_name
, prop
, cr
));
698 * Returns the nvlist as specified by the user in the zfs_cmd_t.
701 get_nvlist(uint64_t nvl
, uint64_t size
, nvlist_t
**nvp
)
705 nvlist_t
*list
= NULL
;
708 * Read in and unpack the user-supplied nvlist.
713 packed
= kmem_alloc(size
, KM_SLEEP
);
715 if ((error
= xcopyin((void *)(uintptr_t)nvl
, packed
, size
)) != 0) {
716 kmem_free(packed
, size
);
720 if ((error
= nvlist_unpack(packed
, size
, &list
, 0)) != 0) {
721 kmem_free(packed
, size
);
725 kmem_free(packed
, size
);
732 put_nvlist(zfs_cmd_t
*zc
, nvlist_t
*nvl
)
738 VERIFY(nvlist_size(nvl
, &size
, NV_ENCODE_NATIVE
) == 0);
740 if (size
> zc
->zc_nvlist_dst_size
) {
743 packed
= kmem_alloc(size
, KM_SLEEP
);
744 VERIFY(nvlist_pack(nvl
, &packed
, &size
, NV_ENCODE_NATIVE
,
746 error
= xcopyout(packed
, (void *)(uintptr_t)zc
->zc_nvlist_dst
,
748 kmem_free(packed
, size
);
751 zc
->zc_nvlist_dst_size
= size
;
756 zfs_ioc_pool_create(zfs_cmd_t
*zc
)
759 nvlist_t
*config
, *props
= NULL
;
760 nvlist_t
*rootprops
= NULL
;
761 nvlist_t
*zplprops
= NULL
;
764 if (error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
768 if (zc
->zc_nvlist_src_size
!= 0 && (error
=
769 get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
, &props
))) {
775 nvlist_t
*nvl
= NULL
;
776 uint64_t version
= SPA_VERSION
;
778 (void) nvlist_lookup_uint64(props
,
779 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
);
780 if (version
< SPA_VERSION_INITIAL
|| version
> SPA_VERSION
) {
784 (void) nvlist_lookup_nvlist(props
, ZPOOL_ROOTFS_PROPS
, &nvl
);
786 error
= nvlist_dup(nvl
, &rootprops
, KM_SLEEP
);
792 (void) nvlist_remove_all(props
, ZPOOL_ROOTFS_PROPS
);
794 VERIFY(nvlist_alloc(&zplprops
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
795 error
= zfs_fill_zplprops_root(version
, rootprops
,
801 buf
= history_str_get(zc
);
803 error
= spa_create(zc
->zc_name
, config
, props
, buf
, zplprops
);
806 * Set the remaining root properties
809 (error
= zfs_set_prop_nvlist(zc
->zc_name
, rootprops
)) != 0)
810 (void) spa_destroy(zc
->zc_name
);
813 history_str_free(buf
);
816 nvlist_free(rootprops
);
817 nvlist_free(zplprops
);
825 zfs_ioc_pool_destroy(zfs_cmd_t
*zc
)
829 error
= spa_destroy(zc
->zc_name
);
834 zfs_ioc_pool_import(zfs_cmd_t
*zc
)
837 nvlist_t
*config
, *props
= NULL
;
840 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
844 if (zc
->zc_nvlist_src_size
!= 0 && (error
=
845 get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
, &props
))) {
850 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &guid
) != 0 ||
853 else if (zc
->zc_cookie
)
854 error
= spa_import_faulted(zc
->zc_name
, config
,
857 error
= spa_import(zc
->zc_name
, config
, props
);
868 zfs_ioc_pool_export(zfs_cmd_t
*zc
)
871 boolean_t force
= (boolean_t
)zc
->zc_cookie
;
874 error
= spa_export(zc
->zc_name
, NULL
, force
);
879 zfs_ioc_pool_configs(zfs_cmd_t
*zc
)
884 if ((configs
= spa_all_configs(&zc
->zc_cookie
)) == NULL
)
887 error
= put_nvlist(zc
, configs
);
889 nvlist_free(configs
);
895 zfs_ioc_pool_stats(zfs_cmd_t
*zc
)
901 error
= spa_get_stats(zc
->zc_name
, &config
, zc
->zc_value
,
902 sizeof (zc
->zc_value
));
904 if (config
!= NULL
) {
905 ret
= put_nvlist(zc
, config
);
909 * The config may be present even if 'error' is non-zero.
910 * In this case we return success, and preserve the real errno
913 zc
->zc_cookie
= error
;
922 * Try to import the given pool, returning pool stats as appropriate so that
923 * user land knows which devices are available and overall pool health.
926 zfs_ioc_pool_tryimport(zfs_cmd_t
*zc
)
928 nvlist_t
*tryconfig
, *config
;
931 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
935 config
= spa_tryimport(tryconfig
);
937 nvlist_free(tryconfig
);
942 error
= put_nvlist(zc
, config
);
949 zfs_ioc_pool_scrub(zfs_cmd_t
*zc
)
954 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
957 error
= spa_scrub(spa
, zc
->zc_cookie
);
959 spa_close(spa
, FTAG
);
965 zfs_ioc_pool_freeze(zfs_cmd_t
*zc
)
970 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
973 spa_close(spa
, FTAG
);
979 zfs_ioc_pool_upgrade(zfs_cmd_t
*zc
)
984 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
987 if (zc
->zc_cookie
< spa_version(spa
) || zc
->zc_cookie
> SPA_VERSION
) {
988 spa_close(spa
, FTAG
);
992 spa_upgrade(spa
, zc
->zc_cookie
);
993 spa_close(spa
, FTAG
);
999 zfs_ioc_pool_get_history(zfs_cmd_t
*zc
)
1006 if ((size
= zc
->zc_history_len
) == 0)
1009 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1012 if (spa_version(spa
) < SPA_VERSION_ZPOOL_HISTORY
) {
1013 spa_close(spa
, FTAG
);
1017 hist_buf
= kmem_alloc(size
, KM_SLEEP
);
1018 if ((error
= spa_history_get(spa
, &zc
->zc_history_offset
,
1019 &zc
->zc_history_len
, hist_buf
)) == 0) {
1020 error
= xcopyout(hist_buf
,
1021 (char *)(uintptr_t)zc
->zc_history
,
1022 zc
->zc_history_len
);
1025 spa_close(spa
, FTAG
);
1026 kmem_free(hist_buf
, size
);
1031 zfs_ioc_dsobj_to_dsname(zfs_cmd_t
*zc
)
1035 if (error
= dsl_dsobj_to_dsname(zc
->zc_name
, zc
->zc_obj
, zc
->zc_value
))
1042 zfs_ioc_obj_to_path(zfs_cmd_t
*zc
)
1047 if ((error
= dmu_objset_open(zc
->zc_name
, DMU_OST_ZFS
,
1048 DS_MODE_USER
| DS_MODE_READONLY
, &osp
)) != 0)
1050 error
= zfs_obj_to_path(osp
, zc
->zc_obj
, zc
->zc_value
,
1051 sizeof (zc
->zc_value
));
1052 dmu_objset_close(osp
);
1058 zfs_ioc_vdev_add(zfs_cmd_t
*zc
)
1062 nvlist_t
*config
, **l2cache
, **spares
;
1063 uint_t nl2cache
= 0, nspares
= 0;
1065 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1069 error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1071 (void) nvlist_lookup_nvlist_array(config
, ZPOOL_CONFIG_L2CACHE
,
1072 &l2cache
, &nl2cache
);
1074 (void) nvlist_lookup_nvlist_array(config
, ZPOOL_CONFIG_SPARES
,
1078 * A root pool with concatenated devices is not supported.
1079 * Thus, can not add a device to a root pool.
1081 * Intent log device can not be added to a rootpool because
1082 * during mountroot, zil is replayed, a seperated log device
1083 * can not be accessed during the mountroot time.
1085 * l2cache and spare devices are ok to be added to a rootpool.
1087 if (spa
->spa_bootfs
!= 0 && nl2cache
== 0 && nspares
== 0) {
1088 spa_close(spa
, FTAG
);
1093 error
= spa_vdev_add(spa
, config
);
1094 nvlist_free(config
);
1096 spa_close(spa
, FTAG
);
1101 zfs_ioc_vdev_remove(zfs_cmd_t
*zc
)
1106 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1109 error
= spa_vdev_remove(spa
, zc
->zc_guid
, B_FALSE
);
1110 spa_close(spa
, FTAG
);
1115 zfs_ioc_vdev_set_state(zfs_cmd_t
*zc
)
1119 vdev_state_t newstate
= VDEV_STATE_UNKNOWN
;
1121 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1123 switch (zc
->zc_cookie
) {
1124 case VDEV_STATE_ONLINE
:
1125 error
= vdev_online(spa
, zc
->zc_guid
, zc
->zc_obj
, &newstate
);
1128 case VDEV_STATE_OFFLINE
:
1129 error
= vdev_offline(spa
, zc
->zc_guid
, zc
->zc_obj
);
1132 case VDEV_STATE_FAULTED
:
1133 error
= vdev_fault(spa
, zc
->zc_guid
);
1136 case VDEV_STATE_DEGRADED
:
1137 error
= vdev_degrade(spa
, zc
->zc_guid
);
1143 zc
->zc_cookie
= newstate
;
1144 spa_close(spa
, FTAG
);
1149 zfs_ioc_vdev_attach(zfs_cmd_t
*zc
)
1152 int replacing
= zc
->zc_cookie
;
1156 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1159 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1161 error
= spa_vdev_attach(spa
, zc
->zc_guid
, config
, replacing
);
1162 nvlist_free(config
);
1165 spa_close(spa
, FTAG
);
1170 zfs_ioc_vdev_detach(zfs_cmd_t
*zc
)
1175 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1178 error
= spa_vdev_detach(spa
, zc
->zc_guid
, B_FALSE
);
1180 spa_close(spa
, FTAG
);
1185 zfs_ioc_vdev_setpath(zfs_cmd_t
*zc
)
1188 char *path
= zc
->zc_value
;
1189 uint64_t guid
= zc
->zc_guid
;
1192 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1196 error
= spa_vdev_setpath(spa
, guid
, path
);
1197 spa_close(spa
, FTAG
);
1203 * zc_name name of filesystem
1204 * zc_nvlist_dst_size size of buffer for property nvlist
1207 * zc_objset_stats stats
1208 * zc_nvlist_dst property nvlist
1209 * zc_nvlist_dst_size size of property nvlist
1212 zfs_ioc_objset_stats(zfs_cmd_t
*zc
)
1214 objset_t
*os
= NULL
;
1218 if (error
= dmu_objset_open(zc
->zc_name
,
1219 DMU_OST_ANY
, DS_MODE_USER
| DS_MODE_READONLY
, &os
))
1222 dmu_objset_fast_stat(os
, &zc
->zc_objset_stats
);
1224 if (zc
->zc_nvlist_dst
!= 0 &&
1225 (error
= dsl_prop_get_all(os
, &nv
, FALSE
)) == 0) {
1226 dmu_objset_stats(os
, nv
);
1228 * NB: zvol_get_stats() will read the objset contents,
1229 * which we aren't supposed to do with a
1230 * DS_MODE_USER hold, because it could be
1231 * inconsistent. So this is a bit of a workaround...
1233 if (!zc
->zc_objset_stats
.dds_inconsistent
) {
1234 if (dmu_objset_type(os
) == DMU_OST_ZVOL
)
1235 VERIFY(zvol_get_stats(os
, nv
) == 0);
1237 error
= put_nvlist(zc
, nv
);
1241 dmu_objset_close(os
);
1246 nvl_add_zplprop(objset_t
*os
, nvlist_t
*props
, zfs_prop_t prop
)
1252 * zfs_get_zplprop() will either find a value or give us
1253 * the default value (if there is one).
1255 if ((error
= zfs_get_zplprop(os
, prop
, &value
)) != 0)
1257 VERIFY(nvlist_add_uint64(props
, zfs_prop_to_name(prop
), value
) == 0);
1263 * zc_name name of filesystem
1264 * zc_nvlist_dst_size size of buffer for zpl property nvlist
1267 * zc_nvlist_dst zpl property nvlist
1268 * zc_nvlist_dst_size size of zpl property nvlist
1271 zfs_ioc_objset_zplprops(zfs_cmd_t
*zc
)
1276 if (err
= dmu_objset_open(zc
->zc_name
,
1277 DMU_OST_ANY
, DS_MODE_USER
| DS_MODE_READONLY
, &os
))
1280 dmu_objset_fast_stat(os
, &zc
->zc_objset_stats
);
1283 * NB: nvl_add_zplprop() will read the objset contents,
1284 * which we aren't supposed to do with a DS_MODE_USER
1285 * hold, because it could be inconsistent.
1287 if (zc
->zc_nvlist_dst
!= NULL
&&
1288 !zc
->zc_objset_stats
.dds_inconsistent
&&
1289 dmu_objset_type(os
) == DMU_OST_ZFS
) {
1292 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1293 if ((err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_VERSION
)) == 0 &&
1294 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_NORMALIZE
)) == 0 &&
1295 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_UTF8ONLY
)) == 0 &&
1296 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_CASE
)) == 0)
1297 err
= put_nvlist(zc
, nv
);
1302 dmu_objset_close(os
);
1307 zfs_prefetch_datasets(zfs_cmd_t
*zc
, objset_t
*os
, char *p
)
1309 uint64_t cookie
= 0;
1313 error
= dmu_dir_list_next(os
,
1314 sizeof (zc
->zc_name
) - (p
- zc
->zc_name
), p
,
1316 } while (error
== 0 && !INGLOBALZONE(curproc
) &&
1317 !zone_dataset_visible(zc
->zc_name
, NULL
) &&
1318 !dmu_objset_prefetch(zc
->zc_name
, NULL
));
1322 zfs_prefetch_snapshots(zfs_cmd_t
*zc
)
1324 dmu_objset_find(zc
->zc_name
, dmu_objset_prefetch
,
1325 NULL
, DS_FIND_SNAPSHOTS
);
1330 * zc_name name of filesystem
1331 * zc_cookie zap cursor
1332 * zc_nvlist_dst_size size of buffer for property nvlist
1335 * zc_name name of next filesystem
1336 * zc_objset_stats stats
1337 * zc_nvlist_dst property nvlist
1338 * zc_nvlist_dst_size size of property nvlist
1341 zfs_ioc_dataset_list_next(zfs_cmd_t
*zc
)
1347 if (error
= dmu_objset_open(zc
->zc_name
,
1348 DMU_OST_ANY
, DS_MODE_USER
| DS_MODE_READONLY
, &os
)) {
1349 if (error
== ENOENT
)
1354 p
= strrchr(zc
->zc_name
, '/');
1355 if (p
== NULL
|| p
[1] != '\0')
1356 (void) strlcat(zc
->zc_name
, "/", sizeof (zc
->zc_name
));
1357 p
= zc
->zc_name
+ strlen(zc
->zc_name
);
1359 if (zc
->zc_cookie
== 0)
1360 zfs_prefetch_datasets(zc
, os
, p
);
1362 error
= dmu_dir_list_next(os
,
1363 sizeof (zc
->zc_name
) - (p
- zc
->zc_name
), p
,
1364 NULL
, &zc
->zc_cookie
);
1365 if (error
== ENOENT
)
1367 } while (error
== 0 && !INGLOBALZONE(curproc
) &&
1368 !zone_dataset_visible(zc
->zc_name
, NULL
));
1369 dmu_objset_close(os
);
1372 * If it's a hidden dataset (ie. with a '$' in its name), don't
1373 * try to get stats for it. Userland will skip over it.
1375 if (error
== 0 && strchr(zc
->zc_name
, '$') == NULL
)
1376 error
= zfs_ioc_objset_stats(zc
); /* fill in the stats */
1383 * zc_name name of filesystem
1384 * zc_cookie zap cursor
1385 * zc_nvlist_dst_size size of buffer for property nvlist
1388 * zc_name name of next snapshot
1389 * zc_objset_stats stats
1390 * zc_nvlist_dst property nvlist
1391 * zc_nvlist_dst_size size of property nvlist
1394 zfs_ioc_snapshot_list_next(zfs_cmd_t
*zc
)
1399 error
= dmu_objset_open(zc
->zc_name
,
1400 DMU_OST_ANY
, DS_MODE_USER
| DS_MODE_READONLY
, &os
);
1402 return (error
== ENOENT
? ESRCH
: error
);
1404 if (zc
->zc_cookie
== 0)
1405 zfs_prefetch_snapshots(zc
);
1407 * A dataset name of maximum length cannot have any snapshots,
1408 * so exit immediately.
1410 if (strlcat(zc
->zc_name
, "@", sizeof (zc
->zc_name
)) >= MAXNAMELEN
) {
1411 dmu_objset_close(os
);
1415 error
= dmu_snapshot_list_next(os
,
1416 sizeof (zc
->zc_name
) - strlen(zc
->zc_name
),
1417 zc
->zc_name
+ strlen(zc
->zc_name
), NULL
, &zc
->zc_cookie
, NULL
);
1418 dmu_objset_close(os
);
1420 error
= zfs_ioc_objset_stats(zc
); /* fill in the stats */
1421 else if (error
== ENOENT
)
1424 /* if we failed, undo the @ that we tacked on to zc_name */
1426 *strchr(zc
->zc_name
, '@') = '\0';
1431 zfs_set_prop_nvlist(const char *name
, nvlist_t
*nvl
)
1439 * First validate permission to set all of the properties
1442 while ((elem
= nvlist_next_nvpair(nvl
, elem
)) != NULL
) {
1443 const char *propname
= nvpair_name(elem
);
1444 zfs_prop_t prop
= zfs_name_to_prop(propname
);
1446 if (prop
== ZPROP_INVAL
) {
1448 * If this is a user-defined property, it must be a
1449 * string, and there is no further validation to do.
1451 if (!zfs_prop_user(propname
) ||
1452 nvpair_type(elem
) != DATA_TYPE_STRING
)
1455 if (error
= zfs_secpolicy_write_perms(name
,
1456 ZFS_DELEG_PERM_USERPROP
, CRED()))
1461 if ((error
= zfs_secpolicy_setprop(name
, prop
, CRED())) != 0)
1465 * Check that this value is valid for this pool version
1468 case ZFS_PROP_COMPRESSION
:
1470 * If the user specified gzip compression, make sure
1471 * the SPA supports it. We ignore any errors here since
1472 * we'll catch them later.
1474 if (nvpair_type(elem
) == DATA_TYPE_UINT64
&&
1475 nvpair_value_uint64(elem
, &intval
) == 0) {
1476 if (intval
>= ZIO_COMPRESS_GZIP_1
&&
1477 intval
<= ZIO_COMPRESS_GZIP_9
&&
1478 zfs_earlier_version(name
,
1479 SPA_VERSION_GZIP_COMPRESSION
))
1483 * If this is a bootable dataset then
1484 * verify that the compression algorithm
1485 * is supported for booting. We must return
1486 * something other than ENOTSUP since it
1487 * implies a downrev pool version.
1489 if (zfs_is_bootfs(name
) &&
1490 !BOOTFS_COMPRESS_VALID(intval
))
1495 case ZFS_PROP_COPIES
:
1496 if (zfs_earlier_version(name
,
1497 SPA_VERSION_DITTO_BLOCKS
))
1501 case ZFS_PROP_SHARESMB
:
1502 if (zpl_earlier_version(name
, ZPL_VERSION_FUID
))
1506 case ZFS_PROP_ACLINHERIT
:
1507 if (nvpair_type(elem
) == DATA_TYPE_UINT64
&&
1508 nvpair_value_uint64(elem
, &intval
) == 0)
1509 if (intval
== ZFS_ACL_PASSTHROUGH_X
&&
1510 zfs_earlier_version(name
,
1511 SPA_VERSION_PASSTHROUGH_X
))
1517 while ((elem
= nvlist_next_nvpair(nvl
, elem
)) != NULL
) {
1518 const char *propname
= nvpair_name(elem
);
1519 zfs_prop_t prop
= zfs_name_to_prop(propname
);
1521 if (prop
== ZPROP_INVAL
) {
1522 VERIFY(nvpair_value_string(elem
, &strval
) == 0);
1523 error
= dsl_prop_set(name
, propname
, 1,
1524 strlen(strval
) + 1, strval
);
1532 case ZFS_PROP_QUOTA
:
1533 if ((error
= nvpair_value_uint64(elem
, &intval
)) != 0 ||
1534 (error
= dsl_dir_set_quota(name
, intval
)) != 0)
1538 case ZFS_PROP_REFQUOTA
:
1539 if ((error
= nvpair_value_uint64(elem
, &intval
)) != 0 ||
1540 (error
= dsl_dataset_set_quota(name
, intval
)) != 0)
1544 case ZFS_PROP_RESERVATION
:
1545 if ((error
= nvpair_value_uint64(elem
, &intval
)) != 0 ||
1546 (error
= dsl_dir_set_reservation(name
,
1551 case ZFS_PROP_REFRESERVATION
:
1552 if ((error
= nvpair_value_uint64(elem
, &intval
)) != 0 ||
1553 (error
= dsl_dataset_set_reservation(name
,
1558 case ZFS_PROP_VOLSIZE
:
1559 if ((error
= nvpair_value_uint64(elem
, &intval
)) != 0 ||
1560 (error
= zvol_set_volsize(name
,
1561 ddi_driver_major(zfs_dip
), intval
)) != 0)
1565 case ZFS_PROP_VOLBLOCKSIZE
:
1566 if ((error
= nvpair_value_uint64(elem
, &intval
)) != 0 ||
1567 (error
= zvol_set_volblocksize(name
, intval
)) != 0)
1571 case ZFS_PROP_VERSION
:
1572 if ((error
= nvpair_value_uint64(elem
, &intval
)) != 0 ||
1573 (error
= zfs_set_version(name
, intval
)) != 0)
1578 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
1579 if (zfs_prop_get_type(prop
) !=
1582 VERIFY(nvpair_value_string(elem
, &strval
) == 0);
1583 if ((error
= dsl_prop_set(name
,
1584 nvpair_name(elem
), 1, strlen(strval
) + 1,
1587 } else if (nvpair_type(elem
) == DATA_TYPE_UINT64
) {
1590 VERIFY(nvpair_value_uint64(elem
, &intval
) == 0);
1592 switch (zfs_prop_get_type(prop
)) {
1593 case PROP_TYPE_NUMBER
:
1595 case PROP_TYPE_STRING
:
1597 case PROP_TYPE_INDEX
:
1598 if (zfs_prop_index_to_string(prop
,
1599 intval
, &unused
) != 0)
1604 "unknown property type");
1608 if ((error
= dsl_prop_set(name
, propname
,
1609 8, 1, &intval
)) != 0)
1623 * zc_name name of filesystem
1624 * zc_value name of property to inherit
1625 * zc_nvlist_src{_size} nvlist of properties to apply
1626 * zc_cookie clear existing local props?
1631 zfs_ioc_set_prop(zfs_cmd_t
*zc
)
1636 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1640 if (zc
->zc_cookie
) {
1641 nvlist_t
*origprops
;
1644 if (dmu_objset_open(zc
->zc_name
, DMU_OST_ANY
,
1645 DS_MODE_USER
| DS_MODE_READONLY
, &os
) == 0) {
1646 if (dsl_prop_get_all(os
, &origprops
, TRUE
) == 0) {
1647 clear_props(zc
->zc_name
, origprops
);
1648 nvlist_free(origprops
);
1650 dmu_objset_close(os
);
1655 error
= zfs_set_prop_nvlist(zc
->zc_name
, nvl
);
1663 * zc_name name of filesystem
1664 * zc_value name of property to inherit
1669 zfs_ioc_inherit_prop(zfs_cmd_t
*zc
)
1671 /* the property name has been validated by zfs_secpolicy_inherit() */
1672 return (dsl_prop_set(zc
->zc_name
, zc
->zc_value
, 0, 0, NULL
));
1676 zfs_ioc_pool_set_props(zfs_cmd_t
*zc
)
1682 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1686 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0) {
1691 error
= spa_prop_set(spa
, props
);
1694 spa_close(spa
, FTAG
);
1700 zfs_ioc_pool_get_props(zfs_cmd_t
*zc
)
1704 nvlist_t
*nvp
= NULL
;
1706 dprintf("zfs_ioc_pool_get_props called %s \n", spa
->spa_name
);
1707 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1710 error
= spa_prop_get(spa
, &nvp
);
1712 if (error
== 0 && zc
->zc_nvlist_dst
!= NULL
)
1713 error
= put_nvlist(zc
, nvp
);
1717 spa_close(spa
, FTAG
);
1721 dprintf("zfs_ioc_pool_get_props property list set\n");
1726 zfs_ioc_iscsi_perm_check(zfs_cmd_t
*zc
)
1736 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1741 if ((error
= nvlist_lookup_uint32(nvp
,
1742 ZFS_DELEG_PERM_UID
, &uid
)) != 0) {
1747 if ((error
= nvlist_lookup_uint32(nvp
,
1748 ZFS_DELEG_PERM_GID
, &gid
)) != 0) {
1753 if ((error
= nvlist_lookup_uint32_array(nvp
, ZFS_DELEG_PERM_GROUPS
,
1754 &groups
, &group_cnt
)) != 0) {
1758 usercred
= cralloc();
1759 if ((crsetugid(usercred
, uid
, gid
) != 0) ||
1760 (crsetgroups(usercred
, group_cnt
, (gid_t
*)groups
) != 0)) {
1766 error
= dsl_deleg_access(zc
->zc_name
,
1767 zfs_prop_to_name(ZFS_PROP_SHAREISCSI
), usercred
);
1774 * zc_name name of filesystem
1775 * zc_nvlist_src{_size} nvlist of delegated permissions
1776 * zc_perm_action allow/unallow flag
1781 zfs_ioc_set_fsacl(zfs_cmd_t
*zc
)
1784 nvlist_t
*fsaclnv
= NULL
;
1786 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1791 * Verify nvlist is constructed correctly
1793 if ((error
= zfs_deleg_verify_nvlist(fsaclnv
)) != 0) {
1794 nvlist_free(fsaclnv
);
1799 * If we don't have PRIV_SYS_MOUNT, then validate
1800 * that user is allowed to hand out each permission in
1804 error
= secpolicy_zfs(CRED());
1806 if (zc
->zc_perm_action
== B_FALSE
) {
1807 error
= dsl_deleg_can_allow(zc
->zc_name
,
1810 error
= dsl_deleg_can_unallow(zc
->zc_name
,
1816 error
= dsl_deleg_set(zc
->zc_name
, fsaclnv
, zc
->zc_perm_action
);
1818 nvlist_free(fsaclnv
);
1824 * zc_name name of filesystem
1827 * zc_nvlist_src{_size} nvlist of delegated permissions
1830 zfs_ioc_get_fsacl(zfs_cmd_t
*zc
)
1835 if ((error
= dsl_deleg_get(zc
->zc_name
, &nvp
)) == 0) {
1836 error
= put_nvlist(zc
, nvp
);
1845 * zc_name name of volume
1850 zfs_ioc_create_minor(zfs_cmd_t
*zc
)
1852 return (zvol_create_minor(zc
->zc_name
, ddi_driver_major(zfs_dip
)));
1857 * zc_name name of volume
1862 zfs_ioc_remove_minor(zfs_cmd_t
*zc
)
1864 return (zvol_remove_minor(zc
->zc_name
));
1868 * Search the vfs list for a specified resource. Returns a pointer to it
1869 * or NULL if no suitable entry is found. The caller of this routine
1870 * is responsible for releasing the returned vfs pointer.
1873 zfs_get_vfs(const char *resource
)
1876 printf("XXX zfs_get_vfs write me\n");
1882 zfs_create_cb(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
)
1884 zfs_creat_t
*zct
= arg
;
1886 zfs_create_fs(os
, cr
, zct
->zct_zplprops
, tx
);
1889 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
1893 * createprops list of properties requested by creator
1894 * default_zplver zpl version to use if unspecified in createprops
1895 * fuids_ok fuids allowed in this version of the spa?
1896 * os parent objset pointer (NULL if root fs)
1899 * zplprops values for the zplprops we attach to the master node object
1900 * is_ci true if requested file system will be purely case-insensitive
1902 * Determine the settings for utf8only, normalization and
1903 * casesensitivity. Specific values may have been requested by the
1904 * creator and/or we can inherit values from the parent dataset. If
1905 * the file system is of too early a vintage, a creator can not
1906 * request settings for these properties, even if the requested
1907 * setting is the default value. We don't actually want to create dsl
1908 * properties for these, so remove them from the source nvlist after
1912 zfs_fill_zplprops_impl(objset_t
*os
, uint64_t default_zplver
,
1913 boolean_t fuids_ok
, nvlist_t
*createprops
, nvlist_t
*zplprops
,
1916 uint64_t zplver
= default_zplver
;
1917 uint64_t sense
= ZFS_PROP_UNDEFINED
;
1918 uint64_t norm
= ZFS_PROP_UNDEFINED
;
1919 uint64_t u8
= ZFS_PROP_UNDEFINED
;
1921 ASSERT(zplprops
!= NULL
);
1924 * Pull out creator prop choices, if any.
1927 (void) nvlist_lookup_uint64(createprops
,
1928 zfs_prop_to_name(ZFS_PROP_VERSION
), &zplver
);
1929 (void) nvlist_lookup_uint64(createprops
,
1930 zfs_prop_to_name(ZFS_PROP_NORMALIZE
), &norm
);
1931 (void) nvlist_remove_all(createprops
,
1932 zfs_prop_to_name(ZFS_PROP_NORMALIZE
));
1933 (void) nvlist_lookup_uint64(createprops
,
1934 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), &u8
);
1935 (void) nvlist_remove_all(createprops
,
1936 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
));
1937 (void) nvlist_lookup_uint64(createprops
,
1938 zfs_prop_to_name(ZFS_PROP_CASE
), &sense
);
1939 (void) nvlist_remove_all(createprops
,
1940 zfs_prop_to_name(ZFS_PROP_CASE
));
1944 * If the zpl version requested is whacky or the file system
1945 * or pool is version is too "young" to support normalization
1946 * and the creator tried to set a value for one of the props,
1949 if ((zplver
< ZPL_VERSION_INITIAL
|| zplver
> ZPL_VERSION
) ||
1950 (zplver
>= ZPL_VERSION_FUID
&& !fuids_ok
) ||
1951 (zplver
< ZPL_VERSION_NORMALIZATION
&&
1952 (norm
!= ZFS_PROP_UNDEFINED
|| u8
!= ZFS_PROP_UNDEFINED
||
1953 sense
!= ZFS_PROP_UNDEFINED
)))
1957 * Put the version in the zplprops
1959 VERIFY(nvlist_add_uint64(zplprops
,
1960 zfs_prop_to_name(ZFS_PROP_VERSION
), zplver
) == 0);
1962 if (norm
== ZFS_PROP_UNDEFINED
)
1963 VERIFY(zfs_get_zplprop(os
, ZFS_PROP_NORMALIZE
, &norm
) == 0);
1964 VERIFY(nvlist_add_uint64(zplprops
,
1965 zfs_prop_to_name(ZFS_PROP_NORMALIZE
), norm
) == 0);
1968 * If we're normalizing, names must always be valid UTF-8 strings.
1972 if (u8
== ZFS_PROP_UNDEFINED
)
1973 VERIFY(zfs_get_zplprop(os
, ZFS_PROP_UTF8ONLY
, &u8
) == 0);
1974 VERIFY(nvlist_add_uint64(zplprops
,
1975 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), u8
) == 0);
1977 if (sense
== ZFS_PROP_UNDEFINED
)
1978 VERIFY(zfs_get_zplprop(os
, ZFS_PROP_CASE
, &sense
) == 0);
1979 VERIFY(nvlist_add_uint64(zplprops
,
1980 zfs_prop_to_name(ZFS_PROP_CASE
), sense
) == 0);
1983 *is_ci
= (sense
== ZFS_CASE_INSENSITIVE
);
1989 zfs_fill_zplprops(const char *dataset
, nvlist_t
*createprops
,
1990 nvlist_t
*zplprops
, boolean_t
*is_ci
)
1992 boolean_t fuids_ok
= B_TRUE
;
1993 uint64_t zplver
= ZPL_VERSION
;
1994 objset_t
*os
= NULL
;
1995 char parentname
[MAXNAMELEN
];
1999 (void) strlcpy(parentname
, dataset
, sizeof (parentname
));
2000 cp
= strrchr(parentname
, '/');
2004 if (zfs_earlier_version(dataset
, SPA_VERSION_FUID
)) {
2005 zplver
= ZPL_VERSION_FUID
- 1;
2010 * Open parent object set so we can inherit zplprop values.
2012 if ((error
= dmu_objset_open(parentname
, DMU_OST_ANY
,
2013 DS_MODE_USER
| DS_MODE_READONLY
, &os
)) != 0)
2016 error
= zfs_fill_zplprops_impl(os
, zplver
, fuids_ok
, createprops
,
2018 dmu_objset_close(os
);
2023 zfs_fill_zplprops_root(uint64_t spa_vers
, nvlist_t
*createprops
,
2024 nvlist_t
*zplprops
, boolean_t
*is_ci
)
2026 boolean_t fuids_ok
= B_TRUE
;
2027 uint64_t zplver
= ZPL_VERSION
;
2030 if (spa_vers
< SPA_VERSION_FUID
) {
2031 zplver
= ZPL_VERSION_FUID
- 1;
2035 error
= zfs_fill_zplprops_impl(NULL
, zplver
, fuids_ok
, createprops
,
2042 * zc_objset_type type of objset to create (fs vs zvol)
2043 * zc_name name of new objset
2044 * zc_value name of snapshot to clone from (may be empty)
2045 * zc_nvlist_src{_size} nvlist of properties to apply
2050 zfs_ioc_create(zfs_cmd_t
*zc
)
2055 nvlist_t
*nvprops
= NULL
;
2056 void (*cbfunc
)(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
);
2057 dmu_objset_type_t type
= zc
->zc_objset_type
;
2062 cbfunc
= zfs_create_cb
;
2066 cbfunc
= zvol_create_cb
;
2073 if (strchr(zc
->zc_name
, '@') ||
2074 strchr(zc
->zc_name
, '%'))
2077 if (zc
->zc_nvlist_src
!= NULL
&&
2078 (error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2082 zct
.zct_zplprops
= NULL
;
2083 zct
.zct_props
= nvprops
;
2085 if (zc
->zc_value
[0] != '\0') {
2087 * We're creating a clone of an existing snapshot.
2089 zc
->zc_value
[sizeof (zc
->zc_value
) - 1] = '\0';
2090 if (dataset_namecheck(zc
->zc_value
, NULL
, NULL
) != 0) {
2091 nvlist_free(nvprops
);
2095 error
= dmu_objset_open(zc
->zc_value
, type
,
2096 DS_MODE_USER
| DS_MODE_READONLY
, &clone
);
2098 nvlist_free(nvprops
);
2102 error
= dmu_objset_create(zc
->zc_name
, type
, clone
, 0,
2105 dmu_objset_close(clone
);
2106 nvlist_free(nvprops
);
2109 dmu_objset_close(clone
);
2111 boolean_t is_insensitive
= B_FALSE
;
2113 if (cbfunc
== NULL
) {
2114 nvlist_free(nvprops
);
2118 if (type
== DMU_OST_ZVOL
) {
2119 uint64_t volsize
, volblocksize
;
2121 if (nvprops
== NULL
||
2122 nvlist_lookup_uint64(nvprops
,
2123 zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
2125 nvlist_free(nvprops
);
2129 if ((error
= nvlist_lookup_uint64(nvprops
,
2130 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
2131 &volblocksize
)) != 0 && error
!= ENOENT
) {
2132 nvlist_free(nvprops
);
2137 volblocksize
= zfs_prop_default_numeric(
2138 ZFS_PROP_VOLBLOCKSIZE
);
2140 if ((error
= zvol_check_volblocksize(
2141 volblocksize
)) != 0 ||
2142 (error
= zvol_check_volsize(volsize
,
2143 volblocksize
)) != 0) {
2144 nvlist_free(nvprops
);
2147 } else if (type
== DMU_OST_ZFS
) {
2151 * We have to have normalization and
2152 * case-folding flags correct when we do the
2153 * file system creation, so go figure them out
2156 VERIFY(nvlist_alloc(&zct
.zct_zplprops
,
2157 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2158 error
= zfs_fill_zplprops(zc
->zc_name
, nvprops
,
2159 zct
.zct_zplprops
, &is_insensitive
);
2161 nvlist_free(nvprops
);
2162 nvlist_free(zct
.zct_zplprops
);
2166 error
= dmu_objset_create(zc
->zc_name
, type
, NULL
,
2167 is_insensitive
? DS_FLAG_CI_DATASET
: 0, cbfunc
, &zct
);
2168 nvlist_free(zct
.zct_zplprops
);
2172 * It would be nice to do this atomically.
2175 if ((error
= zfs_set_prop_nvlist(zc
->zc_name
, nvprops
)) != 0)
2176 (void) dmu_objset_destroy(zc
->zc_name
);
2178 nvlist_free(nvprops
);
2182 struct snap_prop_arg
{
2184 const char *snapname
;
2188 set_snap_props(char *name
, void *arg
)
2190 struct snap_prop_arg
*snpa
= arg
;
2191 int len
= strlen(name
) + strlen(snpa
->snapname
) + 2;
2192 char *buf
= kmem_alloc(len
, KM_SLEEP
);
2195 (void) snprintf(buf
, len
, "%s@%s", name
, snpa
->snapname
);
2196 err
= zfs_set_prop_nvlist(buf
, snpa
->nvprops
);
2198 (void) dmu_objset_destroy(buf
);
2199 kmem_free(buf
, len
);
2205 * zc_name name of filesystem
2206 * zc_value short name of snapshot
2207 * zc_cookie recursive flag
2212 zfs_ioc_snapshot(zfs_cmd_t
*zc
)
2214 nvlist_t
*nvprops
= NULL
;
2216 boolean_t recursive
= zc
->zc_cookie
;
2218 if (snapshot_namecheck(zc
->zc_value
, NULL
, NULL
) != 0)
2221 if (zc
->zc_nvlist_src
!= NULL
&&
2222 (error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2226 error
= dmu_objset_snapshot(zc
->zc_name
, zc
->zc_value
, recursive
);
2229 * It would be nice to do this atomically.
2232 struct snap_prop_arg snpa
;
2233 snpa
.nvprops
= nvprops
;
2234 snpa
.snapname
= zc
->zc_value
;
2236 error
= dmu_objset_find(zc
->zc_name
,
2237 set_snap_props
, &snpa
, DS_FIND_CHILDREN
);
2239 (void) dmu_snapshots_destroy(zc
->zc_name
,
2243 error
= set_snap_props(zc
->zc_name
, &snpa
);
2246 nvlist_free(nvprops
);
2251 zfs_unmount_snap(char *name
, void *arg
)
2256 char *snapname
= arg
;
2257 int len
= strlen(name
) + strlen(snapname
) + 2;
2258 char *buf
= kmem_alloc(len
, KM_SLEEP
);
2260 (void) strcpy(buf
, name
);
2261 (void) strcat(buf
, "@");
2262 (void) strcat(buf
, snapname
);
2263 vfsp
= zfs_get_vfs(buf
);
2264 kmem_free(buf
, len
);
2265 } else if (strchr(name
, '@')) {
2266 vfsp
= zfs_get_vfs(name
);
2272 if ((err
= dounmount(vfsp
, MNT_FORCE
, curlwp
)) != 0)
2277 * Always force the unmount for snapshots.
2279 int flag
= MS_FORCE
;
2282 if ((err
= vn_vfswlock(vfsp
->vfs_vnodecovered
)) != 0) {
2287 if ((err
= dounmount(vfsp
, flag
, kcred
)) != 0)
2296 * zc_name name of filesystem
2297 * zc_value short name of snapshot
2302 zfs_ioc_destroy_snaps(zfs_cmd_t
*zc
)
2306 if (snapshot_namecheck(zc
->zc_value
, NULL
, NULL
) != 0)
2308 err
= dmu_objset_find(zc
->zc_name
,
2309 zfs_unmount_snap
, zc
->zc_value
, DS_FIND_CHILDREN
);
2312 return (dmu_snapshots_destroy(zc
->zc_name
, zc
->zc_value
));
2317 * zc_name name of dataset to destroy
2318 * zc_objset_type type of objset
2323 zfs_ioc_destroy(zfs_cmd_t
*zc
)
2325 if (strchr(zc
->zc_name
, '@') && zc
->zc_objset_type
== DMU_OST_ZFS
) {
2326 int err
= zfs_unmount_snap(zc
->zc_name
, NULL
);
2331 return (dmu_objset_destroy(zc
->zc_name
));
2336 * zc_name name of dataset to rollback (to most recent snapshot)
2341 zfs_ioc_rollback(zfs_cmd_t
*zc
)
2345 zfsvfs_t
*zfsvfs
= NULL
;
2348 * Get the zfsvfs for the receiving objset. There
2349 * won't be one if we're operating on a zvol, if the
2350 * objset doesn't exist yet, or is not mounted.
2352 error
= dmu_objset_open(zc
->zc_name
, DMU_OST_ANY
, DS_MODE_USER
, &os
);
2356 if (dmu_objset_type(os
) == DMU_OST_ZFS
) {
2357 mutex_enter(&os
->os
->os_user_ptr_lock
);
2358 zfsvfs
= dmu_objset_get_user(os
);
2360 VFS_HOLD(zfsvfs
->z_vfs
);
2361 mutex_exit(&os
->os
->os_user_ptr_lock
);
2364 if (zfsvfs
!= NULL
) {
2368 osname
= kmem_alloc(MAXNAMELEN
, KM_SLEEP
);
2369 error
= zfs_suspend_fs(zfsvfs
, osname
, &mode
);
2373 ASSERT(strcmp(osname
, zc
->zc_name
) == 0);
2374 error
= dmu_objset_rollback(os
);
2375 resume_err
= zfs_resume_fs(zfsvfs
, osname
, mode
);
2376 error
= error
? error
: resume_err
;
2378 dmu_objset_close(os
);
2380 kmem_free(osname
, MAXNAMELEN
);
2381 VFS_RELE(zfsvfs
->z_vfs
);
2383 error
= dmu_objset_rollback(os
);
2385 /* Note, the dmu_objset_rollback() releases the objset for us. */
2392 * zc_name old name of dataset
2393 * zc_value new name of dataset
2394 * zc_cookie recursive flag (only valid for snapshots)
2399 zfs_ioc_rename(zfs_cmd_t
*zc
)
2401 boolean_t recursive
= zc
->zc_cookie
& 1;
2403 zc
->zc_value
[sizeof (zc
->zc_value
) - 1] = '\0';
2404 if (dataset_namecheck(zc
->zc_value
, NULL
, NULL
) != 0 ||
2405 strchr(zc
->zc_value
, '%'))
2409 * Unmount snapshot unless we're doing a recursive rename,
2410 * in which case the dataset code figures out which snapshots
2413 if (!recursive
&& strchr(zc
->zc_name
, '@') != NULL
&&
2414 zc
->zc_objset_type
== DMU_OST_ZFS
) {
2415 int err
= zfs_unmount_snap(zc
->zc_name
, NULL
);
2419 return (dmu_objset_rename(zc
->zc_name
, zc
->zc_value
, recursive
));
2423 clear_props(char *dataset
, nvlist_t
*props
)
2430 zc
= kmem_alloc(sizeof (zfs_cmd_t
), KM_SLEEP
);
2431 (void) strcpy(zc
->zc_name
, dataset
);
2432 for (prop
= nvlist_next_nvpair(props
, NULL
); prop
;
2433 prop
= nvlist_next_nvpair(props
, prop
)) {
2434 (void) strcpy(zc
->zc_value
, nvpair_name(prop
));
2435 if (zfs_secpolicy_inherit(zc
, CRED()) == 0)
2436 (void) zfs_ioc_inherit_prop(zc
);
2438 kmem_free(zc
, sizeof (zfs_cmd_t
));
2443 * zc_name name of containing filesystem
2444 * zc_nvlist_src{_size} nvlist of properties to apply
2445 * zc_value name of snapshot to create
2446 * zc_string name of clone origin (if DRR_FLAG_CLONE)
2447 * zc_cookie file descriptor to recv from
2448 * zc_begin_record the BEGIN record of the stream (not byteswapped)
2449 * zc_guid force flag
2452 * zc_cookie number of bytes read
2455 zfs_ioc_recv(zfs_cmd_t
*zc
)
2459 dmu_recv_cookie_t drc
;
2460 zfsvfs_t
*zfsvfs
= NULL
;
2461 boolean_t force
= (boolean_t
)zc
->zc_guid
;
2464 nvlist_t
*props
= NULL
;
2465 nvlist_t
*origprops
= NULL
;
2466 objset_t
*origin
= NULL
;
2468 char tofs
[ZFS_MAXNAMELEN
];
2470 if (dataset_namecheck(zc
->zc_value
, NULL
, NULL
) != 0 ||
2471 strchr(zc
->zc_value
, '@') == NULL
||
2472 strchr(zc
->zc_value
, '%'))
2475 (void) strcpy(tofs
, zc
->zc_value
);
2476 tosnap
= strchr(tofs
, '@');
2480 if (zc
->zc_nvlist_src
!= NULL
&&
2481 (error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2486 error
= fd_getvnode(fd
, &fp
);
2492 if (dmu_objset_open(tofs
, DMU_OST_ANY
,
2493 DS_MODE_USER
| DS_MODE_READONLY
, &os
) == 0) {
2495 * Try to get the zfsvfs for the receiving objset.
2496 * There won't be one if we're operating on a zvol,
2497 * if the objset doesn't exist yet, or is not mounted.
2499 mutex_enter(&os
->os
->os_user_ptr_lock
);
2500 if (zfsvfs
= dmu_objset_get_user(os
)) {
2501 if (!mutex_tryenter(&zfsvfs
->z_online_recv_lock
)) {
2502 mutex_exit(&os
->os
->os_user_ptr_lock
);
2503 dmu_objset_close(os
);
2508 VFS_HOLD(zfsvfs
->z_vfs
);
2510 mutex_exit(&os
->os
->os_user_ptr_lock
);
2513 * If new properties are supplied, they are to completely
2514 * replace the existing ones, so stash away the existing ones.
2517 (void) dsl_prop_get_all(os
, &origprops
, TRUE
);
2519 dmu_objset_close(os
);
2522 if (zc
->zc_string
[0]) {
2523 error
= dmu_objset_open(zc
->zc_string
, DMU_OST_ANY
,
2524 DS_MODE_USER
| DS_MODE_READONLY
, &origin
);
2529 error
= dmu_recv_begin(tofs
, tosnap
, &zc
->zc_begin_record
,
2530 force
, origin
, zfsvfs
!= NULL
, &drc
);
2532 dmu_objset_close(origin
);
2537 * Reset properties. We do this before we receive the stream
2538 * so that the properties are applied to the new data.
2541 clear_props(tofs
, origprops
);
2543 * XXX - Note, this is all-or-nothing; should be best-effort.
2545 (void) zfs_set_prop_nvlist(tofs
, props
);
2549 error
= dmu_recv_stream(&drc
, fp
->f_data
, &off
);
2551 if (error
== 0 && zfsvfs
) {
2556 osname
= kmem_alloc(MAXNAMELEN
, KM_SLEEP
);
2557 error
= zfs_suspend_fs(zfsvfs
, osname
, &mode
);
2561 error
= dmu_recv_end(&drc
);
2562 resume_err
= zfs_resume_fs(zfsvfs
, osname
, mode
);
2563 error
= error
? error
: resume_err
;
2565 dmu_recv_abort_cleanup(&drc
);
2567 kmem_free(osname
, MAXNAMELEN
);
2568 } else if (error
== 0) {
2569 error
= dmu_recv_end(&drc
);
2572 zc
->zc_cookie
= off
- fp
->f_offset
;
2573 if (VOP_SEEK(fp
->f_data
, fp
->f_offset
, &off
, NULL
) == 0)
2577 * On error, restore the original props.
2579 if (error
&& props
) {
2580 clear_props(tofs
, props
);
2581 (void) zfs_set_prop_nvlist(tofs
, origprops
);
2585 mutex_exit(&zfsvfs
->z_online_recv_lock
);
2586 VFS_RELE(zfsvfs
->z_vfs
);
2589 nvlist_free(origprops
);
2596 * zc_name name of snapshot to send
2597 * zc_value short name of incremental fromsnap (may be empty)
2598 * zc_cookie file descriptor to send stream to
2599 * zc_obj fromorigin flag (mutually exclusive with zc_value)
2604 zfs_ioc_send(zfs_cmd_t
*zc
)
2606 objset_t
*fromsnap
= NULL
;
2612 error
= dmu_objset_open(zc
->zc_name
, DMU_OST_ANY
,
2613 DS_MODE_USER
| DS_MODE_READONLY
, &tosnap
);
2617 if (zc
->zc_value
[0] != '\0') {
2621 buf
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
2622 (void) strncpy(buf
, zc
->zc_name
, MAXPATHLEN
);
2623 cp
= strchr(buf
, '@');
2626 (void) strncat(buf
, zc
->zc_value
, MAXPATHLEN
);
2627 error
= dmu_objset_open(buf
, DMU_OST_ANY
,
2628 DS_MODE_USER
| DS_MODE_READONLY
, &fromsnap
);
2629 kmem_free(buf
, MAXPATHLEN
);
2631 dmu_objset_close(tosnap
);
2636 error
= fd_getvnode(zc
->zc_cookie
, &fp
);
2638 dmu_objset_close(tosnap
);
2640 dmu_objset_close(fromsnap
);
2645 error
= dmu_sendbackup(tosnap
, fromsnap
, zc
->zc_obj
, fp
->f_data
, &off
);
2647 if (VOP_SEEK(fp
->f_data
, fp
->f_offset
, &off
, NULL
) == 0)
2649 fd_putfile(zc
->zc_cookie
);
2651 dmu_objset_close(fromsnap
);
2652 dmu_objset_close(tosnap
);
2657 zfs_ioc_inject_fault(zfs_cmd_t
*zc
)
2661 error
= zio_inject_fault(zc
->zc_name
, (int)zc
->zc_guid
, &id
,
2662 &zc
->zc_inject_record
);
2665 zc
->zc_guid
= (uint64_t)id
;
2671 zfs_ioc_clear_fault(zfs_cmd_t
*zc
)
2673 return (zio_clear_fault((int)zc
->zc_guid
));
2677 zfs_ioc_inject_list_next(zfs_cmd_t
*zc
)
2679 int id
= (int)zc
->zc_guid
;
2682 error
= zio_inject_list_next(&id
, zc
->zc_name
, sizeof (zc
->zc_name
),
2683 &zc
->zc_inject_record
);
2691 zfs_ioc_error_log(zfs_cmd_t
*zc
)
2695 size_t count
= (size_t)zc
->zc_nvlist_dst_size
;
2697 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
2700 error
= spa_get_errlog(spa
, (void *)(uintptr_t)zc
->zc_nvlist_dst
,
2703 zc
->zc_nvlist_dst_size
= count
;
2705 zc
->zc_nvlist_dst_size
= spa_get_errlog_size(spa
);
2707 spa_close(spa
, FTAG
);
2713 zfs_ioc_clear(zfs_cmd_t
*zc
)
2720 * On zpool clear we also fix up missing slogs
2722 mutex_enter(&spa_namespace_lock
);
2723 spa
= spa_lookup(zc
->zc_name
);
2725 mutex_exit(&spa_namespace_lock
);
2728 if (spa
->spa_log_state
== SPA_LOG_MISSING
) {
2729 /* we need to let spa_open/spa_load clear the chains */
2730 spa
->spa_log_state
= SPA_LOG_CLEAR
;
2732 mutex_exit(&spa_namespace_lock
);
2734 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
2737 spa_vdev_state_enter(spa
);
2739 if (zc
->zc_guid
== 0) {
2742 vd
= spa_lookup_by_guid(spa
, zc
->zc_guid
, B_TRUE
);
2744 (void) spa_vdev_state_exit(spa
, NULL
, ENODEV
);
2745 spa_close(spa
, FTAG
);
2750 vdev_clear(spa
, vd
);
2752 (void) spa_vdev_state_exit(spa
, NULL
, 0);
2755 * Resume any suspended I/Os.
2759 spa_close(spa
, FTAG
);
2766 * zc_name name of filesystem
2767 * zc_value name of origin snapshot
2772 zfs_ioc_promote(zfs_cmd_t
*zc
)
2777 * We don't need to unmount *all* the origin fs's snapshots, but
2780 cp
= strchr(zc
->zc_value
, '@');
2783 (void) dmu_objset_find(zc
->zc_value
,
2784 zfs_unmount_snap
, NULL
, DS_FIND_SNAPSHOTS
);
2785 return (dsl_dataset_promote(zc
->zc_name
));
2789 * We don't want to have a hard dependency
2790 * against some special symbols in sharefs
2791 * nfs, and smbsrv. Determine them if needed when
2792 * the first file system is shared.
2793 * Neither sharefs, nfs or smbsrv are unloadable modules.
2798 zfs_ioc_share(zfs_cmd_t
*zc
)
2804 #else /* __NetBSD__ */
2806 int (*znfsexport_fs
)(void *arg
);
2807 int (*zshare_fs
)(enum sharefs_sys_op
, share_t
*, uint32_t);
2808 int (*zsmbexport_fs
)(void *arg
, boolean_t add_share
);
2810 int zfs_nfsshare_inited
;
2811 int zfs_smbshare_inited
;
2813 ddi_modhandle_t nfs_mod
;
2814 ddi_modhandle_t sharefs_mod
;
2815 ddi_modhandle_t smbsrv_mod
;
2816 kmutex_t zfs_share_lock
;
2823 ASSERT(MUTEX_HELD(&zfs_share_lock
));
2824 /* Both NFS and SMB shares also require sharetab support. */
2825 if (sharefs_mod
== NULL
&& ((sharefs_mod
=
2826 ddi_modopen("fs/sharefs",
2827 KRTLD_MODE_FIRST
, &error
)) == NULL
)) {
2830 if (zshare_fs
== NULL
&& ((zshare_fs
=
2831 (int (*)(enum sharefs_sys_op
, share_t
*, uint32_t))
2832 ddi_modsym(sharefs_mod
, "sharefs_impl", &error
)) == NULL
)) {
2839 zfs_ioc_share(zfs_cmd_t
*zc
)
2844 switch (zc
->zc_share
.z_sharetype
) {
2846 case ZFS_UNSHARE_NFS
:
2847 if (zfs_nfsshare_inited
== 0) {
2848 mutex_enter(&zfs_share_lock
);
2849 if (nfs_mod
== NULL
&& ((nfs_mod
= ddi_modopen("fs/nfs",
2850 KRTLD_MODE_FIRST
, &error
)) == NULL
)) {
2851 mutex_exit(&zfs_share_lock
);
2854 if (znfsexport_fs
== NULL
&&
2855 ((znfsexport_fs
= (int (*)(void *))
2857 "nfs_export", &error
)) == NULL
)) {
2858 mutex_exit(&zfs_share_lock
);
2861 error
= zfs_init_sharefs();
2863 mutex_exit(&zfs_share_lock
);
2866 zfs_nfsshare_inited
= 1;
2867 mutex_exit(&zfs_share_lock
);
2871 case ZFS_UNSHARE_SMB
:
2872 if (zfs_smbshare_inited
== 0) {
2873 mutex_enter(&zfs_share_lock
);
2874 if (smbsrv_mod
== NULL
&& ((smbsrv_mod
=
2875 ddi_modopen("drv/smbsrv",
2876 KRTLD_MODE_FIRST
, &error
)) == NULL
)) {
2877 mutex_exit(&zfs_share_lock
);
2880 if (zsmbexport_fs
== NULL
&& ((zsmbexport_fs
=
2881 (int (*)(void *, boolean_t
))ddi_modsym(smbsrv_mod
,
2882 "smb_server_share", &error
)) == NULL
)) {
2883 mutex_exit(&zfs_share_lock
);
2886 error
= zfs_init_sharefs();
2888 mutex_exit(&zfs_share_lock
);
2891 zfs_smbshare_inited
= 1;
2892 mutex_exit(&zfs_share_lock
);
2899 switch (zc
->zc_share
.z_sharetype
) {
2901 case ZFS_UNSHARE_NFS
:
2903 znfsexport_fs((void *)
2904 (uintptr_t)zc
->zc_share
.z_exportdata
))
2908 case ZFS_UNSHARE_SMB
:
2909 if (error
= zsmbexport_fs((void *)
2910 (uintptr_t)zc
->zc_share
.z_exportdata
,
2911 zc
->zc_share
.z_sharetype
== ZFS_SHARE_SMB
?
2912 B_TRUE
: B_FALSE
)) {
2918 opcode
= (zc
->zc_share
.z_sharetype
== ZFS_SHARE_NFS
||
2919 zc
->zc_share
.z_sharetype
== ZFS_SHARE_SMB
) ?
2920 SHAREFS_ADD
: SHAREFS_REMOVE
;
2923 * Add or remove share from sharetab
2925 error
= zshare_fs(opcode
,
2926 (void *)(uintptr_t)zc
->zc_share
.z_sharedata
,
2927 zc
->zc_share
.z_sharemax
);
2932 #endif /* __NetBSD__ */
2935 * pool create, destroy, and export don't log the history as part of
2936 * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
2937 * do the logging of those commands.
2939 static zfs_ioc_vec_t zfs_ioc_vec
[] = {
2940 { zfs_ioc_pool_create
, zfs_secpolicy_config
, POOL_NAME
, B_FALSE
}, /* 0 */
2941 { zfs_ioc_pool_destroy
, zfs_secpolicy_config
, POOL_NAME
, B_FALSE
},
2942 { zfs_ioc_pool_import
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
},
2943 { zfs_ioc_pool_export
, zfs_secpolicy_config
, POOL_NAME
, B_FALSE
},
2944 { zfs_ioc_pool_configs
, zfs_secpolicy_none
, NO_NAME
, B_FALSE
},
2945 { zfs_ioc_pool_stats
, zfs_secpolicy_read
, POOL_NAME
, B_FALSE
}, /* 5 */
2946 { zfs_ioc_pool_tryimport
, zfs_secpolicy_config
, NO_NAME
, B_FALSE
},
2947 { zfs_ioc_pool_scrub
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
},
2948 { zfs_ioc_pool_freeze
, zfs_secpolicy_config
, NO_NAME
, B_FALSE
},
2949 { zfs_ioc_pool_upgrade
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
},
2950 { zfs_ioc_pool_get_history
, zfs_secpolicy_config
, POOL_NAME
, B_FALSE
}, /* 10 */
2951 { zfs_ioc_vdev_add
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
},
2952 { zfs_ioc_vdev_remove
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
},
2953 { zfs_ioc_vdev_set_state
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
},
2954 { zfs_ioc_vdev_attach
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
},
2955 { zfs_ioc_vdev_detach
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
}, /* 15 */
2956 { zfs_ioc_vdev_setpath
, zfs_secpolicy_config
, POOL_NAME
, B_FALSE
},
2957 { zfs_ioc_objset_stats
, zfs_secpolicy_read
, DATASET_NAME
, B_FALSE
},
2958 { zfs_ioc_objset_zplprops
, zfs_secpolicy_read
, DATASET_NAME
, B_FALSE
},
2959 { zfs_ioc_dataset_list_next
, zfs_secpolicy_read
,
2960 DATASET_NAME
, B_FALSE
},
2961 { zfs_ioc_snapshot_list_next
, zfs_secpolicy_read
,
2962 DATASET_NAME
, B_FALSE
}, /* 20*/
2963 { zfs_ioc_set_prop
, zfs_secpolicy_none
, DATASET_NAME
, B_TRUE
},
2964 { zfs_ioc_create_minor
, zfs_secpolicy_minor
, DATASET_NAME
, B_FALSE
},
2965 { zfs_ioc_remove_minor
, zfs_secpolicy_minor
, DATASET_NAME
, B_FALSE
},
2966 { zfs_ioc_create
, zfs_secpolicy_create
, DATASET_NAME
, B_TRUE
},
2967 { zfs_ioc_destroy
, zfs_secpolicy_destroy
, DATASET_NAME
, B_TRUE
}, /* 25 */
2968 { zfs_ioc_rollback
, zfs_secpolicy_rollback
, DATASET_NAME
, B_TRUE
},
2969 { zfs_ioc_rename
, zfs_secpolicy_rename
, DATASET_NAME
, B_TRUE
},
2970 { zfs_ioc_recv
, zfs_secpolicy_receive
, DATASET_NAME
, B_TRUE
},
2971 { zfs_ioc_send
, zfs_secpolicy_send
, DATASET_NAME
, B_TRUE
},
2972 { zfs_ioc_inject_fault
, zfs_secpolicy_inject
, NO_NAME
, B_FALSE
}, /* 30 */
2973 { zfs_ioc_clear_fault
, zfs_secpolicy_inject
, NO_NAME
, B_FALSE
},
2974 { zfs_ioc_inject_list_next
, zfs_secpolicy_inject
, NO_NAME
, B_FALSE
},
2975 { zfs_ioc_error_log
, zfs_secpolicy_inject
, POOL_NAME
, B_FALSE
},
2976 { zfs_ioc_clear
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
},
2977 { zfs_ioc_promote
, zfs_secpolicy_promote
, DATASET_NAME
, B_TRUE
}, /* 35 */
2978 { zfs_ioc_destroy_snaps
, zfs_secpolicy_destroy
, DATASET_NAME
, B_TRUE
},
2979 { zfs_ioc_snapshot
, zfs_secpolicy_snapshot
, DATASET_NAME
, B_TRUE
},
2980 { zfs_ioc_dsobj_to_dsname
, zfs_secpolicy_config
, POOL_NAME
, B_FALSE
},
2981 { zfs_ioc_obj_to_path
, zfs_secpolicy_config
, NO_NAME
, B_FALSE
},
2982 { zfs_ioc_pool_set_props
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
}, /* 40 */
2983 { zfs_ioc_pool_get_props
, zfs_secpolicy_read
, POOL_NAME
, B_FALSE
},
2984 { zfs_ioc_set_fsacl
, zfs_secpolicy_fsacl
, DATASET_NAME
, B_TRUE
},
2985 { zfs_ioc_get_fsacl
, zfs_secpolicy_read
, DATASET_NAME
, B_FALSE
},
2986 { zfs_ioc_iscsi_perm_check
, zfs_secpolicy_iscsi
,
2987 DATASET_NAME
, B_FALSE
},
2988 { zfs_ioc_share
, zfs_secpolicy_share
, DATASET_NAME
, B_FALSE
},
2989 { zfs_ioc_inherit_prop
, zfs_secpolicy_inherit
, DATASET_NAME
, B_TRUE
}, /* 46 */
2993 zfsdev_ioctl(dev_t dev
, int cmd
, intptr_t arg
, int flag
, cred_t
*cr
, int *rvalp
)
2999 dprintf("zfsdev_ioctl called \n");
3001 if (getminor(dev
) != 0)
3002 return (zvol_ioctl(dev
, cmd
, arg
, flag
, cr
, rvalp
));
3003 dprintf("zfsdev_ioctl -> zvol_ioctl\n");
3004 vec
= cmd
- ZFS_IOC
;
3005 ASSERT3U(getmajor(dev
), ==, ddi_driver_major(zfs_dip
));
3007 if (vec
>= sizeof (zfs_ioc_vec
) / sizeof (zfs_ioc_vec
[0]))
3010 zc
= kmem_zalloc(sizeof (zfs_cmd_t
), KM_SLEEP
);
3012 error
= xcopyin((void *)arg
, zc
, sizeof (zfs_cmd_t
));
3013 dprintf("zfsdev_ioct zc_value %s, zc_string\n", zc
->zc_value
, zc
->zc_string
);
3014 dprintf("zfsdev_ioctl -> calling zfs_ioc_vec secpolicy function on %d\n", vec
);
3016 error
= zfs_ioc_vec
[vec
].zvec_secpolicy(zc
, cr
);
3019 * Ensure that all pool/dataset names are valid before we pass down to
3023 dprintf("zfsdev_ioctl, zc->zc_name %s\n", zc
->zc_name
);
3024 zc
->zc_name
[sizeof (zc
->zc_name
) - 1] = '\0';
3025 switch (zfs_ioc_vec
[vec
].zvec_namecheck
) {
3027 if (pool_namecheck(zc
->zc_name
, NULL
, NULL
) != 0)
3032 if (dataset_namecheck(zc
->zc_name
, NULL
, NULL
) != 0)
3041 dprintf("zfsdev_ioctl -> calling zfs_ioc_vec zvec_func on %d\n", vec
);
3043 error
= zfs_ioc_vec
[vec
].zvec_func(zc
);
3045 rc
= xcopyout(zc
, (void *)arg
, sizeof (zfs_cmd_t
));
3048 if (zfs_ioc_vec
[vec
].zvec_his_log
== B_TRUE
)
3049 zfs_log_history(zc
);
3052 kmem_free(zc
, sizeof (zfs_cmd_t
));
3058 #include <sys/module.h>
3059 #include <uvm/uvm_extern.h>
3061 MODULE(MODULE_CLASS_VFS
, zfs
, "solaris");
3064 nb_zvol_copen(dev_t dev
, int flag
, int mode
, lwp_t
*l
)
3067 return zvol_open(&dev
, flag
, OTYPCHR
, kauth_cred_get());
3071 nb_zvol_cclose(dev_t dev
, int flag
, int mode
, lwp_t
*l
)
3074 return zvol_close(dev
, flag
, OTYPCHR
, kauth_cred_get());
3078 nb_zvol_bopen(dev_t dev
, int flag
, int mode
, lwp_t
*l
)
3081 return zvol_open(&dev
, flag
, OTYPBLK
, kauth_cred_get());
3085 nb_zvol_bclose(dev_t dev
, int flag
, int mode
, lwp_t
*l
)
3088 return zvol_close(dev
, flag
, OTYPBLK
, kauth_cred_get());
3092 nb_zvol_read(dev_t dev
, struct uio
*uio
, int flag
)
3095 return zvol_read(dev
, uio
, kauth_cred_get());
3099 nb_zvol_write(dev_t dev
, struct uio
*uio
, int flag
)
3102 return zvol_write(dev
, uio
, kauth_cred_get());
3106 nb_zfsdev_ioctl(dev_t dev
, u_long cmd
, void *argp
, int flag
, lwp_t
*l
)
3110 return zfsdev_ioctl(dev
, cmd
, (intptr_t)argp
, flag
, kauth_cred_get(),
3114 const struct bdevsw zfs_bdevsw
= {
3115 .d_open
= nb_zvol_bopen
,
3116 .d_close
= nb_zvol_bclose
,
3117 .d_strategy
= zvol_strategy
,
3118 .d_ioctl
= nb_zfsdev_ioctl
,
3121 .d_flag
= D_DISK
| D_MPSAFE
3124 const struct cdevsw zfs_cdevsw
= {
3125 .d_open
= nb_zvol_copen
,
3126 .d_close
= nb_zvol_cclose
,
3127 .d_read
= nb_zvol_read
,
3128 .d_write
= nb_zvol_write
,
3129 .d_ioctl
= nb_zfsdev_ioctl
,
3134 .d_kqfilter
= nokqfilter
,
3135 .d_flag
= D_DISK
| D_MPSAFE
3138 uint_t zfs_fsyncer_key
;
3139 extern uint_t rrw_tsd_key
;
3141 /* ZFS must be used on machines with at least 512Mb. */
3142 #define ZFS_MIN_MEGS 512
3145 zfs_modcmd(modcmd_t cmd
, void *arg
)
3148 int active
, inactive
;
3152 case MODULE_CMD_INIT
:
3153 printf("WARNING: ZFS on NetBSD is under development\n");
3154 availrmem
= (uint64_t)physmem
* PAGE_SIZE
/ 1048576;
3155 if (availrmem
< ZFS_MIN_MEGS
* 80 / 100) {
3156 printf("ERROR: at least %dMB of memory required to"
3157 "use ZFS\n", ZFS_MIN_MEGS
);
3160 error
= lwp_specific_key_create(&zfs_fsyncer_key
, NULL
);
3164 error
= lwp_specific_key_create(&rrw_tsd_key
, NULL
);
3166 lwp_specific_key_delete(zfs_fsyncer_key
);
3169 spa_init(FREAD
| FWRITE
);
3172 zfs_vfsinit(16, MOUNT_ZFS
); /* I need to use well defined args. */
3173 error
= devsw_attach("zfs", &zfs_bdevsw
, &zfs_bmajor
,
3174 &zfs_cdevsw
, &zfs_cmajor
);
3179 lwp_specific_key_delete(zfs_fsyncer_key
);
3180 lwp_specific_key_delete(rrw_tsd_key
);
3184 case MODULE_CMD_FINI
:
3185 if (spa_busy() || zfs_busy() || zvol_busy() ||
3186 zio_injection_enabled
)
3188 error
= devsw_detach(&zfs_bdevsw
, &zfs_cdevsw
);
3193 lwp_specific_key_delete(zfs_fsyncer_key
);
3194 lwp_specific_key_delete(rrw_tsd_key
);
3197 case MODULE_CMD_AUTOUNLOAD
:
3199 * We don't want to be autounloaded because unlike
3200 * other subsystems, we read our own configuration
3201 * from disk and provide things that might be used
3211 #else /* __NetBSD__ */
3218 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled
)
3221 if ((error
= mod_remove(&modlinkage
)) != 0)
3227 if (zfs_nfsshare_inited
)
3228 (void) ddi_modclose(nfs_mod
);
3229 if (zfs_smbshare_inited
)
3230 (void) ddi_modclose(smbsrv_mod
);
3231 if (zfs_nfsshare_inited
|| zfs_smbshare_inited
)
3232 (void) ddi_modclose(sharefs_mod
);
3234 tsd_destroy(&zfs_fsyncer_key
);
3235 ldi_ident_release(zfs_li
);
3237 mutex_destroy(&zfs_share_lock
);
3243 zfs_attach(dev_info_t
*dip
, ddi_attach_cmd_t cmd
)
3245 if (cmd
!= DDI_ATTACH
)
3246 return (DDI_FAILURE
);
3248 if (ddi_create_minor_node(dip
, "zfs", S_IFCHR
, 0,
3249 DDI_PSEUDO
, 0) == DDI_FAILURE
)
3250 return (DDI_FAILURE
);
3254 ddi_report_dev(dip
);
3256 return (DDI_SUCCESS
);
3260 zfs_detach(dev_info_t
*dip
, ddi_detach_cmd_t cmd
)
3262 if (spa_busy() || zfs_busy() || zvol_busy())
3263 return (DDI_FAILURE
);
3265 if (cmd
!= DDI_DETACH
)
3266 return (DDI_FAILURE
);
3270 ddi_prop_remove_all(dip
);
3271 ddi_remove_minor_node(dip
, NULL
);
3273 return (DDI_SUCCESS
);
3278 zfs_info(dev_info_t
*dip
, ddi_info_cmd_t infocmd
, void *arg
, void **result
)
3281 case DDI_INFO_DEVT2DEVINFO
:
3283 return (DDI_SUCCESS
);
3285 case DDI_INFO_DEVT2INSTANCE
:
3286 *result
= (void *)0;
3287 return (DDI_SUCCESS
);
3290 return (DDI_FAILURE
);
3294 * OK, so this is a little weird.
3296 * /dev/zfs is the control node, i.e. minor 0.
3297 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
3299 * /dev/zfs has basically nothing to do except serve up ioctls,
3300 * so most of the standard driver entry points are in zvol.c.
3302 static struct cb_ops zfs_cb_ops
= {
3303 zvol_open
, /* open */
3304 zvol_close
, /* close */
3305 zvol_strategy
, /* strategy */
3307 zvol_dump
, /* dump */
3308 zvol_read
, /* read */
3309 zvol_write
, /* write */
3310 zfsdev_ioctl
, /* ioctl */
3314 nochpoll
, /* poll */
3315 ddi_prop_op
, /* prop_op */
3316 NULL
, /* streamtab */
3317 D_NEW
| D_MP
| D_64BIT
, /* Driver compatibility flag */
3318 CB_REV
, /* version */
3319 nodev
, /* async read */
3320 nodev
, /* async write */
3323 static struct dev_ops zfs_dev_ops
= {
3324 DEVO_REV
, /* version */
3326 zfs_info
, /* info */
3327 nulldev
, /* identify */
3328 nulldev
, /* probe */
3329 zfs_attach
, /* attach */
3330 zfs_detach
, /* detach */
3332 &zfs_cb_ops
, /* driver operations */
3333 NULL
, /* no bus operations */
3335 ddi_quiesce_not_needed
, /* quiesce */
3338 static struct modldrv zfs_modldrv
= {
3344 static struct modlinkage modlinkage
= {
3346 (void *)&zfs_modlfs
,
3347 (void *)&zfs_modldrv
,
3352 uint_t zfs_fsyncer_key
;
3353 extern uint_t rrw_tsd_key
;
3360 spa_init(FREAD
| FWRITE
);
3364 if ((error
= mod_install(&modlinkage
)) != 0) {
3371 tsd_create(&zfs_fsyncer_key
, NULL
);
3372 tsd_create(&rrw_tsd_key
, NULL
);
3374 error
= ldi_ident_from_mod(&modlinkage
, &zfs_li
);
3376 mutex_init(&zfs_share_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
3386 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled
)
3389 if ((error
= mod_remove(&modlinkage
)) != 0)
3395 if (zfs_nfsshare_inited
)
3396 (void) ddi_modclose(nfs_mod
);
3397 if (zfs_smbshare_inited
)
3398 (void) ddi_modclose(smbsrv_mod
);
3399 if (zfs_nfsshare_inited
|| zfs_smbshare_inited
)
3400 (void) ddi_modclose(sharefs_mod
);
3402 tsd_destroy(&zfs_fsyncer_key
);
3403 ldi_ident_release(zfs_li
);
3405 mutex_destroy(&zfs_share_lock
);
3411 _info(struct modinfo
*modinfop
)
3413 return (mod_info(&modlinkage
, modinfop
));
3415 #endif /* __NetBSD__ */