Home | History | Annotate | Line # | Download | only in common
h_fsmacros.h revision 1.44.8.1
      1 /*	$NetBSD: h_fsmacros.h,v 1.44.8.1 2024/08/12 22:35:30 perseant Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Nicolas Joly.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #ifndef __H_FSMACROS_H_
     33 #define __H_FSMACROS_H_
     34 
     35 #include <sys/mount.h>
     36 
     37 #include <atf-c.h>
     38 #include <puffsdump.h>
     39 #include <string.h>
     40 
     41 #include <rump/rump.h>
     42 
     43 #include "h_macros.h"
     44 
     45 #define FSPROTOS(_fs_)							\
     46 int _fs_##_fstest_newfs(const atf_tc_t *, void **, const char *,	\
     47 			off_t, void *);					\
     48 int _fs_##_fstest_delfs(const atf_tc_t *, void *);			\
     49 int _fs_##_fstest_mount(const atf_tc_t *, void *, const char *, int);	\
     50 int _fs_##_fstest_unmount(const atf_tc_t *, const char *, int);
     51 
     52 FSPROTOS(exfatfs);
     53 FSPROTOS(ext2fs);
     54 FSPROTOS(ffs);
     55 FSPROTOS(ffslog);
     56 FSPROTOS(lfs);
     57 FSPROTOS(msdosfs);
     58 FSPROTOS(nfs);
     59 FSPROTOS(nfsro);
     60 FSPROTOS(p2k_ffs);
     61 FSPROTOS(puffs);
     62 FSPROTOS(rumpfs);
     63 FSPROTOS(sysvbfs);
     64 FSPROTOS(tmpfs);
     65 FSPROTOS(udf);
     66 FSPROTOS(v7fs);
     67 FSPROTOS(zfs);
     68 
     69 #ifndef FSTEST_IMGNAME
     70 #define FSTEST_IMGNAME "image.fs"
     71 #endif
     72 #ifndef FSTEST_IMGSIZE
     73 #define FSTEST_IMGSIZE (10000 * 512)
     74 #endif
     75 #ifndef FSTEST_MNTNAME
     76 #define FSTEST_MNTNAME "/mnt"
     77 #endif
     78 
     79 #define FSTEST_CONSTRUCTOR(_tc_, _fs_, _args_)				\
     80 do {									\
     81 	struct statvfs fsstat;						\
     82 	if (statvfs(".", &fsstat) == 0 &&				\
     83 	    (fsstat.f_frsize * fsstat.f_bfree) <= FSTEST_IMGSIZE)	\
     84 		atf_tc_skip("not enough free space in work directory");	\
     85 	if (_fs_##_fstest_newfs(_tc_, &_args_,				\
     86 	    FSTEST_IMGNAME, FSTEST_IMGSIZE, NULL) != 0)			\
     87 		atf_tc_fail_errno("newfs failed");			\
     88 	if (_fs_##_fstest_mount(_tc_, _args_, FSTEST_MNTNAME, 0) != 0)	\
     89 		atf_tc_fail_errno("mount failed");			\
     90 } while (/*CONSTCOND*/0)
     91 
     92 #define FSTEST_CONSTRUCTOR_FSPRIV(_tc_, _fs_, _args_, _privargs_)	\
     93 do {									\
     94 	if (_fs_##_fstest_newfs(_tc_, &_args_,				\
     95 	    FSTEST_IMGNAME, FSTEST_IMGSIZE, _privargs_) != 0)		\
     96 		atf_tc_fail_errno("newfs failed");			\
     97 	if (_fs_##_fstest_mount(_tc_, _args_, FSTEST_MNTNAME, 0) != 0)	\
     98 		atf_tc_fail_errno("mount failed");			\
     99 } while (/*CONSTCOND*/0)
    100 
    101 #define FSTEST_DESTRUCTOR(_tc_, _fs_, _args_)				\
    102 do {									\
    103 	if (_fs_##_fstest_unmount(_tc_, FSTEST_MNTNAME, 0) != 0) {	\
    104 		rump_pub_vfs_mount_print(FSTEST_MNTNAME, 1);		\
    105 		atf_tc_fail_errno("unmount failed");			\
    106 	}								\
    107 	if (_fs_##_fstest_delfs(_tc_, _args_) != 0)			\
    108 		atf_tc_fail_errno("delfs failed");			\
    109 } while (/*CONSTCOND*/0)
    110 
    111 #define ATF_TC_FSADD(fs,type,func,desc)					\
    112 	ATF_TC(fs##_##func);						\
    113 	ATF_TC_HEAD(fs##_##func,tc)					\
    114 	{								\
    115 		atf_tc_set_md_var(tc, "descr", type " test for " desc);	\
    116 		atf_tc_set_md_var(tc, "X-fs.type", #fs);		\
    117 		atf_tc_set_md_var(tc, "X-fs.mntname", type);		\
    118 	}								\
    119 	void *fs##func##tmp;						\
    120 									\
    121 	ATF_TC_BODY(fs##_##func,tc)					\
    122 	{								\
    123 		if (!atf_check_fstype(tc, #fs))				\
    124 			atf_tc_skip("filesystem not selected");		\
    125 		FSTEST_CONSTRUCTOR(tc,fs,fs##func##tmp);		\
    126 		func(tc,FSTEST_MNTNAME);				\
    127 		if (fs##_fstest_unmount(tc, FSTEST_MNTNAME, 0) != 0) {	\
    128 			rump_pub_vfs_mount_print(FSTEST_MNTNAME, 1);	\
    129 			atf_tc_fail_errno("unmount failed");		\
    130 		}							\
    131 	}
    132 
    133 #define ATF_TC_FSADD_RO(_fs_,_type_,_func_,_desc_,_gen_)		\
    134 	ATF_TC(_fs_##_##_func_);					\
    135 	ATF_TC_HEAD(_fs_##_##_func_,tc)					\
    136 	{								\
    137 		atf_tc_set_md_var(tc, "descr",_type_" test for "_desc_);\
    138 		atf_tc_set_md_var(tc, "X-fs.type", #_fs_);		\
    139 		atf_tc_set_md_var(tc, "X-fs.mntname", _type_);		\
    140 	}								\
    141 	void *_fs_##_func_##tmp;					\
    142 									\
    143 	ATF_TC_BODY(_fs_##_##_func_,tc)					\
    144 	{								\
    145 		if (!atf_check_fstype(tc, #_fs_))			\
    146 			atf_tc_skip("filesystem not selected");		\
    147 		FSTEST_CONSTRUCTOR(tc,_fs_,_fs_##_func_##tmp);		\
    148 		_gen_(tc,FSTEST_MNTNAME);				\
    149 		if (_fs_##_fstest_unmount(tc, FSTEST_MNTNAME, 0) != 0)	\
    150 			atf_tc_fail_errno("unmount r/w failed");	\
    151 		if (_fs_##_fstest_mount(tc, _fs_##_func_##tmp,		\
    152 		    FSTEST_MNTNAME, MNT_RDONLY) != 0)			\
    153 			atf_tc_fail_errno("mount ro failed");		\
    154 		_func_(tc,FSTEST_MNTNAME);				\
    155 		if (_fs_##_fstest_unmount(tc, FSTEST_MNTNAME, 0) != 0) {\
    156 			rump_pub_vfs_mount_print(FSTEST_MNTNAME, 1);	\
    157 			atf_tc_fail_errno("unmount failed");		\
    158 		}							\
    159 	}
    160 
    161 #define ATF_TP_FSADD(fs,func)						\
    162   ATF_TP_ADD_TC(tp,fs##_##func)
    163 
    164 #ifdef WANT_EXFATFS_TESTS
    165 # define ATF_TC_FSADD_EXFATFS(func,desc) ATF_TC_FSADD(exfatfs,MOUNT_EXFATFS,func,desc)
    166 # define ATF_TP_FSADD_EXFATFS(func) ATF_TP_FSADD(exfatfs,func);
    167 # define ATF_TP_FSADD_EXFATFS(func)  ATF_TP_FSADD(exfatfs,func);
    168 # define ATF_TC_FSADD_RO_EXFATFS(func,desc,gen) \
    169 	ATF_TC_FSADD_RO(exfatfs,MOUNT_EXFATFS,func,desc,gen)
    170 #else /* !WANT_EXFATFS_TESTS */
    171 # define ATF_TC_FSADD_EXFATFS(func,desc)
    172 # define ATF_TP_FSADD_EXFATFS(func)
    173 # define ATF_TP_FSADD_EXFATFS(func)
    174 # define ATF_TC_FSADD_RO_EXFATFS(func,desc,gen)
    175 #endif /* WANT_EXFATFS_TESTS */
    176 
    177 #ifdef WANT_ZFS_TESTS
    178 # define ATF_TC_FSADD_ZFS(func,desc) ATF_TC_FSADD(zfs,MOUNT_ZFS,func,desc)
    179 # define ATF_TP_FSADD_ZFS(func) ATF_TP_FSADD(zfs,func);
    180 # define ATF_TP_FSADD_ZFS(func)  ATF_TP_FSADD(zfs,func);
    181 # define ATF_TC_FSADD_RO_ZFS(func,desc,gen) \
    182 	ATF_TC_FSADD_RO(zfs,MOUNT_ZFS,func,desc,gen)
    183 #else /* !WANT_ZFS_TESTS */
    184 # define ATF_TC_FSADD_ZFS(func,desc)
    185 # define ATF_TP_FSADD_ZFS(func)
    186 # define ATF_TP_FSADD_ZFS(func)
    187 # define ATF_TC_FSADD_RO_ZFS(func,desc,gen)
    188 #endif /* WANT_ZFS_TESTS */
    189 
    190 #define ATF_TC_FSAPPLY(func,desc)					\
    191   ATF_TC_FSADD_EXFATFS(func,desc)					\
    192   ATF_TC_FSADD(ext2fs,MOUNT_EXT2FS,func,desc)				\
    193   ATF_TC_FSADD(ffs,MOUNT_FFS,func,desc)					\
    194   ATF_TC_FSADD(ffslog,MOUNT_FFS,func,desc)				\
    195   ATF_TC_FSADD(lfs,MOUNT_LFS,func,desc)					\
    196   ATF_TC_FSADD(msdosfs,MOUNT_MSDOS,func,desc)				\
    197   ATF_TC_FSADD(nfs,MOUNT_NFS,func,desc)					\
    198   ATF_TC_FSADD(puffs,MOUNT_PUFFS,func,desc)				\
    199   ATF_TC_FSADD(p2k_ffs,MOUNT_PUFFS,func,desc)				\
    200   ATF_TC_FSADD(rumpfs,MOUNT_RUMPFS,func,desc)				\
    201   ATF_TC_FSADD(sysvbfs,MOUNT_SYSVBFS,func,desc)				\
    202   ATF_TC_FSADD(tmpfs,MOUNT_TMPFS,func,desc)				\
    203   ATF_TC_FSADD(udf,MOUNT_UDF,func,desc)					\
    204   ATF_TC_FSADD(v7fs,MOUNT_V7FS,func,desc)				\
    205   ATF_TC_FSADD_ZFS(func,desc)
    206 #define ATF_TP_FSAPPLY(func)						\
    207   ATF_TP_FSADD_EXFATFS(func)						\
    208   ATF_TP_FSADD(ext2fs,func);						\
    209   ATF_TP_FSADD(ffs,func);						\
    210   ATF_TP_FSADD(ffslog,func);						\
    211   ATF_TP_FSADD(lfs,func);						\
    212   ATF_TP_FSADD(msdosfs,func);						\
    213   ATF_TP_FSADD(nfs,func);						\
    214   ATF_TP_FSADD(puffs,func);						\
    215   ATF_TP_FSADD(p2k_ffs,func);						\
    216   ATF_TP_FSADD(rumpfs,func);						\
    217   ATF_TP_FSADD(sysvbfs,func);						\
    218   ATF_TP_FSADD(tmpfs,func);						\
    219   ATF_TP_FSADD(udf,func);						\
    220   ATF_TP_FSADD(v7fs,func);						\
    221   ATF_TP_FSADD_ZFS(func)
    222 
    223 /*
    224  * Same as above, but generate a file system image first and perform
    225  * tests for a r/o mount.
    226  *
    227  * Missing the following file systems:
    228  *   + lfs (fstest_lfs routines cannot handle remount.  FIXME!)
    229  *   + tmpfs (memory backend)
    230  *   + rumpfs (memory backend)
    231  *   + puffs (memory backend, but could be run in theory)
    232  */
    233 
    234 #define ATF_TC_FSAPPLY_RO(func,desc,gen)				\
    235   ATF_TC_FSADD_RO_EXFATFS(func,desc,gen)				\
    236   ATF_TC_FSADD_RO(ext2fs,MOUNT_EXT2FS,func,desc,gen)			\
    237   ATF_TC_FSADD_RO(ffs,MOUNT_FFS,func,desc,gen)				\
    238   ATF_TC_FSADD_RO(ffslog,MOUNT_FFS,func,desc,gen)			\
    239   ATF_TC_FSADD_RO(msdosfs,MOUNT_MSDOS,func,desc,gen)			\
    240   ATF_TC_FSADD_RO(nfs,MOUNT_NFS,func,desc,gen)				\
    241   ATF_TC_FSADD_RO(nfsro,MOUNT_NFS,func,desc,gen)			\
    242   ATF_TC_FSADD_RO(sysvbfs,MOUNT_SYSVBFS,func,desc,gen)			\
    243   ATF_TC_FSADD_RO(udf,MOUNT_UDF,func,desc,gen)			\
    244   ATF_TC_FSADD_RO(v7fs,MOUNT_V7FS,func,desc,gen)			\
    245   ATF_TC_FSADD_RO_ZFS(func,desc,gen)
    246 
    247 #define ATF_TP_FSAPPLY_RO(func)						\
    248   ATF_TP_FSADD_EXFATFS(func)						\
    249   ATF_TP_FSADD(ext2fs,func);						\
    250   ATF_TP_FSADD(ffs,func);						\
    251   ATF_TP_FSADD(ffslog,func);						\
    252   ATF_TP_FSADD(msdosfs,func);						\
    253   ATF_TP_FSADD(nfs,func);						\
    254   ATF_TP_FSADD(nfsro,func);						\
    255   ATF_TP_FSADD(sysvbfs,func);						\
    256   ATF_TP_FSADD(udf,func);						\
    257   ATF_TP_FSADD(v7fs,func);						\
    258   ATF_TP_FSADD_ZFS(func)
    259 
    260 #define ATF_FSAPPLY(func,desc)						\
    261 	ATF_TC_FSAPPLY(func,desc);					\
    262 	ATF_TP_ADD_TCS(tp)						\
    263 	{								\
    264 		ATF_TP_FSAPPLY(func);					\
    265 		return atf_no_error();					\
    266 	}
    267 
    268 static __inline bool
    269 atf_check_fstype(const atf_tc_t *tc, const char *fs)
    270 {
    271 	const char *fstype;
    272 	struct statvfs fsstat;
    273 
    274 	if (strcmp(fs, "zfs") == 0) {
    275 		/* XXX ZFS hardcodes a minimal size */
    276 		if (statvfs(".", &fsstat) == 0 &&
    277 		    (fsstat.f_frsize * fsstat.f_bfree) <= 64*1024*1024)
    278 			atf_tc_skip("not enough free space in work directory");
    279 	}
    280 
    281 	if (!atf_tc_has_config_var(tc, "fstype"))
    282 		return true;
    283 
    284 	fstype = atf_tc_get_config_var(tc, "fstype");
    285 	if (strcmp(fstype, fs) == 0)
    286 		return true;
    287 	return false;
    288 }
    289 
    290 #define FSTYPE_EXFATFS(tc)\
    291     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "exfatfs") == 0)
    292 #define FSTYPE_EXT2FS(tc)\
    293     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "ext2fs") == 0)
    294 #define FSTYPE_FFS(tc)\
    295     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "ffs") == 0)
    296 #define FSTYPE_FFSLOG(tc)\
    297     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "ffslog") == 0)
    298 #define FSTYPE_LFS(tc)\
    299     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "lfs") == 0)
    300 #define FSTYPE_MSDOS(tc)\
    301     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "msdosfs") == 0)
    302 #define FSTYPE_NFS(tc)\
    303     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "nfs") == 0)
    304 #define FSTYPE_NFSRO(tc)\
    305     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "nfsro") == 0)
    306 #define FSTYPE_P2K_FFS(tc)\
    307     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "p2k_ffs") == 0)
    308 #define FSTYPE_PUFFS(tc)\
    309     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "puffs") == 0)
    310 #define FSTYPE_RUMPFS(tc)\
    311     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "rumpfs") == 0)
    312 #define FSTYPE_SYSVBFS(tc)\
    313     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "sysvbfs") == 0)
    314 #define FSTYPE_TMPFS(tc)\
    315     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "tmpfs") == 0)
    316 #define FSTYPE_UDF(tc)\
    317     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "udf") == 0)
    318 #define FSTYPE_V7FS(tc)\
    319     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "v7fs") == 0)
    320 #define FSTYPE_ZFS(tc)\
    321     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "zfs") == 0)
    322 
    323 #define FSTEST_ENTER()							\
    324 	if (rump_sys_chdir(FSTEST_MNTNAME) == -1)			\
    325 		atf_tc_fail_errno("failed to cd into test mount")
    326 
    327 #define FSTEST_EXIT()							\
    328 	if (rump_sys_chdir("/") == -1)					\
    329 		atf_tc_fail_errno("failed to cd out of test mount")
    330 
    331 /*
    332  * file system args structures
    333  */
    334 
    335 struct nfstestargs {
    336 	pid_t ta_childpid;
    337 	char ta_ethername[MAXPATHLEN];
    338 };
    339 
    340 struct puffstestargs {
    341 	uint8_t			*pta_pargs;
    342 	size_t			pta_pargslen;
    343 
    344 	int			pta_pflags;
    345 	pid_t			pta_childpid;
    346 
    347 	int			pta_rumpfd;
    348 	int			pta_servfd;
    349 
    350 	char			pta_dev[MAXPATHLEN];
    351 	char			pta_dir[MAXPATHLEN];
    352 
    353 	int			pta_mntflags;
    354 
    355 	int			pta_vfs_toserv_ops[PUFFS_VFS_MAX];
    356 	int			pta_vn_toserv_ops[PUFFS_VN_MAX];
    357 };
    358 
    359 #endif /* __H_FSMACROS_H_ */
    360