Home | History | Annotate | Line # | Download | only in common
h_fsmacros.h revision 1.27
      1 /*	$NetBSD: h_fsmacros.h,v 1.27 2011/01/07 11:41:40 pooka 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(ext2fs);
     53 FSPROTOS(ffs);
     54 FSPROTOS(lfs);
     55 FSPROTOS(msdosfs);
     56 FSPROTOS(nfs);
     57 FSPROTOS(nfsro);
     58 FSPROTOS(p2k_ffs);
     59 FSPROTOS(puffs);
     60 FSPROTOS(rumpfs);
     61 FSPROTOS(sysvbfs);
     62 FSPROTOS(tmpfs);
     63 
     64 #ifndef FSTEST_IMGNAME
     65 #define FSTEST_IMGNAME "image.fs"
     66 #endif
     67 #ifndef FSTEST_IMGSIZE
     68 #define FSTEST_IMGSIZE (10000 * 512)
     69 #endif
     70 #ifndef FSTEST_MNTNAME
     71 #define FSTEST_MNTNAME "/mnt"
     72 #endif
     73 
     74 #define FSTEST_CONSTRUCTOR(_tc_, _fs_, _args_)				\
     75 do {									\
     76 	if (_fs_##_fstest_newfs(_tc_, &_args_,				\
     77 	    FSTEST_IMGNAME, FSTEST_IMGSIZE, NULL) != 0)			\
     78 		atf_tc_fail_errno("newfs failed");			\
     79 	if (_fs_##_fstest_mount(_tc_, _args_, FSTEST_MNTNAME, 0) != 0)	\
     80 		atf_tc_fail_errno("mount failed");			\
     81 } while (/*CONSTCOND*/0);
     82 
     83 #define FSTEST_CONSTRUCTOR_FSPRIV(_tc_, _fs_, _args_, _privargs_)	\
     84 do {									\
     85 	if (_fs_##_fstest_newfs(_tc_, &_args_,				\
     86 	    FSTEST_IMGNAME, FSTEST_IMGSIZE, _privargs_) != 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_DESTRUCTOR(_tc_, _fs_, _args_)				\
     93 do {									\
     94 	if (_fs_##_fstest_unmount(_tc_, FSTEST_MNTNAME, 0) != 0) {	\
     95 		rump_pub_vfs_mount_print(FSTEST_MNTNAME, 1);		\
     96 		atf_tc_fail_errno("unmount failed");			\
     97 	}								\
     98 	if (_fs_##_fstest_delfs(_tc_, _args_) != 0)			\
     99 		atf_tc_fail_errno("delfs failed");			\
    100 } while (/*CONSTCOND*/0);
    101 
    102 #define ATF_TC_FSADD(fs,type,func,desc)					\
    103 	ATF_TC_WITH_CLEANUP(fs##_##func);				\
    104 	ATF_TC_HEAD(fs##_##func,tc)					\
    105 	{								\
    106 		atf_tc_set_md_var(tc, "descr", type " test for " desc);	\
    107 		atf_tc_set_md_var(tc, "X-fs.type", #fs);		\
    108 	}								\
    109 	void *fs##func##tmp;						\
    110 									\
    111 	ATF_TC_BODY(fs##_##func,tc)					\
    112 	{								\
    113 		if (!atf_check_fstype(tc, type))			\
    114 			atf_tc_skip("filesystem not selected");		\
    115 		FSTEST_CONSTRUCTOR(tc,fs,fs##func##tmp);		\
    116 		func(tc,FSTEST_MNTNAME);				\
    117 		if (fs##_fstest_unmount(tc, FSTEST_MNTNAME, 0) != 0) {	\
    118 			rump_pub_vfs_mount_print(FSTEST_MNTNAME, 1);	\
    119 			atf_tc_fail_errno("unmount failed");		\
    120 		}							\
    121 	}								\
    122 									\
    123 	ATF_TC_CLEANUP(fs##_##func,tc)					\
    124 	{								\
    125 		if (!atf_check_fstype(tc, type))			\
    126 			return;						\
    127 		if (fs##_fstest_delfs(tc, fs##func##tmp) != 0)		\
    128 			atf_tc_fail_errno("delfs failed");		\
    129 	}
    130 
    131 #define ATF_TC_FSADD_RO(_fs_,_type_,_func_,_desc_,_gen_)		\
    132 	ATF_TC_WITH_CLEANUP(_fs_##_##_func_);				\
    133 	ATF_TC_HEAD(_fs_##_##_func_,tc)					\
    134 	{								\
    135 		atf_tc_set_md_var(tc, "descr",_type_" test for "_desc_);\
    136 		atf_tc_set_md_var(tc, "X-fs.type", #_fs_);		\
    137 	}								\
    138 	void *_fs_##_func_##tmp;					\
    139 									\
    140 	ATF_TC_BODY(_fs_##_##_func_,tc)					\
    141 	{								\
    142 		if (!atf_check_fstype(tc, _type_))			\
    143 			atf_tc_skip("filesystem not selected");		\
    144 		FSTEST_CONSTRUCTOR(tc,_fs_,_fs_##_func_##tmp);		\
    145 		_gen_(tc,FSTEST_MNTNAME);				\
    146 		if (_fs_##_fstest_unmount(tc, FSTEST_MNTNAME, 0) != 0)	\
    147 			atf_tc_fail_errno("unmount r/w failed");	\
    148 		if (_fs_##_fstest_mount(tc, _fs_##_func_##tmp,		\
    149 		    FSTEST_MNTNAME, MNT_RDONLY) != 0)			\
    150 		atf_tc_fail_errno("mount ro failed");			\
    151 		_func_(tc,FSTEST_MNTNAME);				\
    152 		if (_fs_##_fstest_unmount(tc, FSTEST_MNTNAME, 0) != 0) {\
    153 			rump_pub_vfs_mount_print(FSTEST_MNTNAME, 1);	\
    154 			atf_tc_fail_errno("unmount failed");		\
    155 		}							\
    156 	}								\
    157 									\
    158 	ATF_TC_CLEANUP(_fs_##_##_func_,tc)				\
    159 	{								\
    160 		if (!atf_check_fstype(tc, _type_))			\
    161 			return;						\
    162 		if (_fs_##_fstest_delfs(tc, _fs_##_func_##tmp) != 0)	\
    163 			atf_tc_fail_errno("delfs failed");		\
    164 	}
    165 
    166 #define ATF_TP_FSADD(fs,func)						\
    167   ATF_TP_ADD_TC(tp,fs##_##func)
    168 
    169 #define ATF_TC_FSAPPLY(func,desc)					\
    170   ATF_TC_FSADD(ext2fs,MOUNT_EXT2FS,func,desc)				\
    171   ATF_TC_FSADD(ffs,MOUNT_FFS,func,desc)					\
    172   ATF_TC_FSADD(lfs,MOUNT_LFS,func,desc)					\
    173   ATF_TC_FSADD(msdosfs,MOUNT_MSDOS,func,desc)				\
    174   ATF_TC_FSADD(nfs,MOUNT_NFS,func,desc)					\
    175   ATF_TC_FSADD(p2k_ffs,MOUNT_PUFFS,func,desc)				\
    176   ATF_TC_FSADD(puffs,MOUNT_PUFFS,func,desc)				\
    177   ATF_TC_FSADD(rumpfs,MOUNT_RUMPFS,func,desc)				\
    178   ATF_TC_FSADD(sysvbfs,MOUNT_SYSVBFS,func,desc)				\
    179   ATF_TC_FSADD(tmpfs,MOUNT_TMPFS,func,desc)
    180 
    181 #define ATF_TP_FSAPPLY(func)						\
    182   ATF_TP_FSADD(ext2fs,func);						\
    183   ATF_TP_FSADD(ffs,func);						\
    184   ATF_TP_FSADD(lfs,func);						\
    185   ATF_TP_FSADD(msdosfs,func);						\
    186   ATF_TP_FSADD(nfs,func);						\
    187   ATF_TP_FSADD(p2k_ffs,func);						\
    188   ATF_TP_FSADD(puffs,func);						\
    189   ATF_TP_FSADD(rumpfs,func);						\
    190   ATF_TP_FSADD(sysvbfs,func);						\
    191   ATF_TP_FSADD(tmpfs,func);
    192 
    193 /*
    194  * Same as above, but generate a file system image first and perform
    195  * tests for a r/o mount.
    196  *
    197  * Missing the following file systems:
    198  *   + lfs (fstest_lfs routines cannot handle remount.  FIXME!)
    199  *   + tmpfs (memory backend)
    200  *   + rumpfs (memory backend)
    201  *   + puffs (memory backend, but could be run in theory)
    202  */
    203 
    204 #define ATF_TC_FSAPPLY_RO(func,desc,gen)				\
    205   ATF_TC_FSADD_RO(ext2fs,MOUNT_EXT2FS,func,desc,gen)			\
    206   ATF_TC_FSADD_RO(ffs,MOUNT_FFS,func,desc,gen)				\
    207   ATF_TC_FSADD_RO(msdosfs,MOUNT_MSDOS,func,desc,gen)			\
    208   ATF_TC_FSADD_RO(nfs,MOUNT_NFS,func,desc,gen)				\
    209   ATF_TC_FSADD_RO(nfsro,MOUNT_NFS,func,desc,gen)			\
    210   ATF_TC_FSADD_RO(sysvbfs,MOUNT_SYSVBFS,func,desc,gen)
    211 
    212 #define ATF_TP_FSAPPLY_RO(func)						\
    213   ATF_TP_FSADD(ext2fs,func);						\
    214   ATF_TP_FSADD(ffs,func);						\
    215   ATF_TP_FSADD(msdosfs,func);						\
    216   ATF_TP_FSADD(nfs,func);						\
    217   ATF_TP_FSADD(nfsro,func);						\
    218   ATF_TP_FSADD(sysvbfs,func);
    219 
    220 #define ATF_FSAPPLY(func,desc)						\
    221 	ATF_TC_FSAPPLY(func,desc);					\
    222 	ATF_TP_ADD_TCS(tp)						\
    223 	{								\
    224 		ATF_TP_FSAPPLY(func);					\
    225 		return atf_no_error();					\
    226 	}
    227 
    228 static __inline bool
    229 atf_check_fstype(const atf_tc_t *tc, const char *fs)
    230 {
    231 	const char *fstype;
    232 
    233 	if (!atf_tc_has_config_var(tc, "fstype"))
    234 		return true;
    235 
    236 	fstype = atf_tc_get_config_var(tc, "fstype");
    237 	if (strcmp(fstype, fs) == 0)
    238 		return true;
    239 	return false;
    240 }
    241 
    242 #define FSTYPE_EXT2FS(tc)\
    243     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "ext2fs") == 0)
    244 #define FSTYPE_FFS(tc)\
    245     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "ffs") == 0)
    246 #define FSTYPE_LFS(tc)\
    247     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "lfs") == 0)
    248 #define FSTYPE_MSDOS(tc)\
    249     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "msdosfs") == 0)
    250 #define FSTYPE_NFS(tc)\
    251     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "nfs") == 0)
    252 #define FSTYPE_NFSRO(tc)\
    253     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "nfsro") == 0)
    254 #define FSTYPE_P2K_FFS(tc)\
    255     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "p2k_ffs") == 0)
    256 #define FSTYPE_PUFFS(tc)\
    257     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "puffs") == 0)
    258 #define FSTYPE_RUMPFS(tc)\
    259     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "rumpfs") == 0)
    260 #define FSTYPE_SYSVBFS(tc)\
    261     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "sysvbfs") == 0)
    262 #define FSTYPE_TMPFS(tc)\
    263     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "tmpfs") == 0)
    264 
    265 #define FSTEST_ENTER()							\
    266 	if (rump_sys_chdir(FSTEST_MNTNAME) == -1)			\
    267 		atf_tc_fail_errno("failed to cd into test mount")
    268 
    269 #define FSTEST_EXIT()							\
    270 	if (rump_sys_chdir("/") == -1)					\
    271 		atf_tc_fail_errno("failed to cd out of test mount")
    272 
    273 /*
    274  * file system args structures
    275  */
    276 
    277 struct nfstestargs {
    278 	pid_t ta_childpid;
    279 	char ta_ethername[MAXPATHLEN];
    280 };
    281 
    282 struct puffstestargs {
    283 	uint8_t			*pta_pargs;
    284 	size_t			pta_pargslen;
    285 
    286 	int			pta_pflags;
    287 	pid_t			pta_childpid;
    288 
    289 	int			pta_rumpfd;
    290 	int			pta_servfd;
    291 
    292 	char			pta_dev[MAXPATHLEN];
    293 	char			pta_dir[MAXPATHLEN];
    294 
    295 	int			pta_mntflags;
    296 
    297 	int			pta_vfs_toserv_ops[PUFFS_VFS_MAX];
    298 	int			pta_vn_toserv_ops[PUFFS_VN_MAX];
    299 };
    300 
    301 #endif /* __H_FSMACROS_H_ */
    302