Home | History | Annotate | Line # | Download | only in common
h_fsmacros.h revision 1.17
      1 /*	$NetBSD: h_fsmacros.h,v 1.17 2010/07/30 16:15:05 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 #define FSPROTOS(_fs_)							\
     42 int _fs_##_fstest_newfs(const atf_tc_t *, void **, const char *,	\
     43 			off_t, void *);					\
     44 int _fs_##_fstest_delfs(const atf_tc_t *, void *);			\
     45 int _fs_##_fstest_mount(const atf_tc_t *, void *, const char *, int);	\
     46 int _fs_##_fstest_unmount(const atf_tc_t *, const char *, int);
     47 
     48 FSPROTOS(ext2fs);
     49 FSPROTOS(ffs);
     50 FSPROTOS(lfs);
     51 FSPROTOS(msdosfs);
     52 FSPROTOS(nfs);
     53 FSPROTOS(puffs);
     54 FSPROTOS(sysvbfs);
     55 FSPROTOS(tmpfs);
     56 
     57 #define FSTEST_IMGNAME "image.fs"
     58 #define FSTEST_IMGSIZE (10000 * 512)
     59 #define FSTEST_MNTNAME "/mnt"
     60 
     61 #define FSTEST_CONSTRUCTOR(_tc_, _fs_, _args_)				\
     62 do {									\
     63 	if (_fs_##_fstest_newfs(_tc_, &_args_,				\
     64 	    FSTEST_IMGNAME, FSTEST_IMGSIZE, NULL) != 0)			\
     65 		atf_tc_fail("newfs failed");				\
     66 	if (_fs_##_fstest_mount(_tc_, _args_, FSTEST_MNTNAME, 0) != 0)	\
     67 		atf_tc_fail("mount failed");				\
     68 } while (/*CONSTCOND*/0);
     69 
     70 #define FSTEST_CONSTRUCTOR_FSPRIV(_tc_, _fs_, _args_, _privargs_)	\
     71 do {									\
     72 	if (_fs_##_fstest_newfs(_tc_, &_args_,				\
     73 	    FSTEST_IMGNAME, FSTEST_IMGSIZE, _privargs_) != 0)		\
     74 		atf_tc_fail("newfs failed");				\
     75 	if (_fs_##_fstest_mount(_tc_, _args_, FSTEST_MNTNAME, 0) != 0)	\
     76 		atf_tc_fail("mount failed");				\
     77 } while (/*CONSTCOND*/0);
     78 
     79 #define FSTEST_DESTRUCTOR(_tc_, _fs_, _args_)				\
     80 do {									\
     81 	if (_fs_##_fstest_unmount(_tc_, FSTEST_MNTNAME, 0) != 0)	\
     82 		atf_tc_fail("unmount failed");				\
     83 	if (_fs_##_fstest_delfs(_tc_, _args_) != 0)			\
     84 		atf_tc_fail("delfs failed");				\
     85 } while (/*CONSTCOND*/0);
     86 
     87 #define ATF_TC_FSADD(fs,type,func,desc) \
     88   ATF_TC_WITH_CLEANUP(fs##_##func); \
     89   ATF_TC_HEAD(fs##_##func,tc) \
     90   { \
     91     atf_tc_set_md_var(tc, "descr", type " test for " desc); \
     92     atf_tc_set_md_var(tc, "use.fs", "true"); \
     93     atf_tc_set_md_var(tc, "X-fs.type", type); \
     94   } \
     95   void *fs##func##tmp; \
     96   ATF_TC_BODY(fs##_##func,tc) \
     97   { \
     98     if (!atf_check_fstype(tc, type)) \
     99       atf_tc_skip("filesystem not selected"); \
    100     FSTEST_CONSTRUCTOR(tc,fs,fs##func##tmp); \
    101     func(tc,FSTEST_MNTNAME); \
    102     if (fs##_fstest_unmount(tc, FSTEST_MNTNAME, 0) != 0) \
    103       atf_tc_fail("unmount failed"); \
    104   } \
    105   ATF_TC_CLEANUP(fs##_##func,tc) \
    106   { \
    107     if (!atf_check_fstype(tc, type)) \
    108       return; \
    109     if (fs##_fstest_delfs(tc, fs##func##tmp) != 0) \
    110       atf_tc_fail("delfs failed"); \
    111   }
    112 
    113 #define ATF_TP_FSADD(fs,func) \
    114   ATF_TP_ADD_TC(tp,fs##_##func)
    115 
    116 #define ATF_TC_FSAPPLY(func,desc) \
    117   ATF_TC_FSADD(ext2fs,MOUNT_EXT2FS,func,desc) \
    118   ATF_TC_FSADD(ffs,MOUNT_FFS,func,desc) \
    119   ATF_TC_FSADD(lfs,MOUNT_LFS,func,desc) \
    120   ATF_TC_FSADD(msdosfs,MOUNT_MSDOS,func,desc) \
    121   ATF_TC_FSADD(nfs,MOUNT_NFS,func,desc) \
    122   ATF_TC_FSADD(puffs,MOUNT_PUFFS,func,desc) \
    123   ATF_TC_FSADD(sysvbfs,MOUNT_SYSVBFS,func,desc) \
    124   ATF_TC_FSADD(tmpfs,MOUNT_TMPFS,func,desc)
    125 
    126 #define ATF_TP_FSAPPLY(func) \
    127   ATF_TP_FSADD(ext2fs,func); \
    128   ATF_TP_FSADD(ffs,func); \
    129   ATF_TP_FSADD(lfs,func); \
    130   ATF_TP_FSADD(msdosfs,func); \
    131   ATF_TP_FSADD(nfs,func); \
    132   ATF_TP_FSADD(puffs,func); \
    133   ATF_TP_FSADD(sysvbfs,func); \
    134   ATF_TP_FSADD(tmpfs,func);
    135 
    136 #define ATF_FSAPPLY(func,desc) \
    137   ATF_TC_FSAPPLY(func,desc); \
    138   ATF_TP_ADD_TCS(tp) \
    139   { \
    140     ATF_TP_FSAPPLY(func); \
    141     return atf_no_error(); \
    142   }
    143 
    144 static __inline bool
    145 atf_check_fstype(const atf_tc_t *tc, const char *fs)
    146 {
    147   const char *fstype;
    148 
    149   if (!atf_tc_has_config_var(tc, "fstype"))
    150     return true;
    151   fstype = atf_tc_get_config_var(tc, "fstype");
    152   if (strcmp(fstype, fs) == 0)
    153     return true;
    154   return false;
    155 }
    156 
    157 #define FSTYPE_EXT2FS(tc)\
    158     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), MOUNT_EXT2FS) == 0)
    159 #define FSTYPE_FFS(tc)\
    160     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), MOUNT_FFS) == 0)
    161 #define FSTYPE_LFS(tc)\
    162     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), MOUNT_LFS) == 0)
    163 #define FSTYPE_MSDOS(tc)\
    164     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), MOUNT_MSDOS) == 0)
    165 #define FSTYPE_NFS(tc)\
    166     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), MOUNT_NFS) == 0)
    167 #define FSTYPE_PUFFS(tc)\
    168     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), MOUNT_PUFFS) == 0)
    169 #define FSTYPE_SYSVBFS(tc)\
    170     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), MOUNT_SYSVBFS) == 0)
    171 #define FSTYPE_TMPFS(tc)\
    172     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), MOUNT_TMPFS) == 0)
    173 
    174 #define FSTEST_ENTER()						\
    175     if (rump_sys_chdir(FSTEST_MNTNAME) == -1)			\
    176 	atf_tc_fail_errno("failed to cd into test mount")
    177 #define FSTEST_EXIT()						\
    178     if (rump_sys_chdir("/") == -1)				\
    179 	atf_tc_fail_errno("failed to cd out of test mount")
    180 
    181 /*
    182  * file system args structures
    183  */
    184 
    185 struct nfstestargs {
    186 	pid_t ta_childpid;
    187 	char ta_ethername[MAXPATHLEN];
    188 };
    189 
    190 struct puffstestargs {
    191 	uint8_t			*pta_pargs;
    192 	size_t			pta_pargslen;
    193 
    194 	int			pta_pflags;
    195 	pid_t			pta_childpid;
    196 
    197 	int			pta_rumpfd;
    198 	int			pta_servfd;
    199 
    200 	char			pta_dev[MAXPATHLEN];
    201 	char			pta_dir[MAXPATHLEN];
    202 
    203 	int			pta_mntflags;
    204 
    205 	int			pta_vfs_toserv_ops[PUFFS_VFS_MAX];
    206 	int			pta_vn_toserv_ops[PUFFS_VN_MAX];
    207 };
    208 
    209 #endif /* __H_FSMACROS_H_ */
    210