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