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