xref: /netbsd-src/tests/fs/common/h_fsmacros.h (revision b45fa494daa2ba02187711d31a4144faf0993066)
1 /*	$NetBSD: h_fsmacros.h,v 1.34 2011/03/22 16:50:16 jmmv 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(ffslog);
55 FSPROTOS(lfs);
56 FSPROTOS(msdosfs);
57 FSPROTOS(nfs);
58 FSPROTOS(nfsro);
59 FSPROTOS(p2k_ffs);
60 FSPROTOS(puffs);
61 FSPROTOS(rumpfs);
62 FSPROTOS(sysvbfs);
63 FSPROTOS(tmpfs);
64 
65 #ifndef FSTEST_IMGNAME
66 #define FSTEST_IMGNAME "image.fs"
67 #endif
68 #ifndef FSTEST_IMGSIZE
69 #define FSTEST_IMGSIZE (10000 * 512)
70 #endif
71 #ifndef FSTEST_MNTNAME
72 #define FSTEST_MNTNAME "/mnt"
73 #endif
74 
75 #define FSTEST_CONSTRUCTOR(_tc_, _fs_, _args_)				\
76 do {									\
77 	if (_fs_##_fstest_newfs(_tc_, &_args_,				\
78 	    FSTEST_IMGNAME, FSTEST_IMGSIZE, NULL) != 0)			\
79 		atf_tc_fail_errno("newfs failed");			\
80 	if (_fs_##_fstest_mount(_tc_, _args_, FSTEST_MNTNAME, 0) != 0)	\
81 		atf_tc_fail_errno("mount failed");			\
82 } while (/*CONSTCOND*/0);
83 
84 #define FSTEST_CONSTRUCTOR_FSPRIV(_tc_, _fs_, _args_, _privargs_)	\
85 do {									\
86 	if (_fs_##_fstest_newfs(_tc_, &_args_,				\
87 	    FSTEST_IMGNAME, FSTEST_IMGSIZE, _privargs_) != 0)		\
88 		atf_tc_fail_errno("newfs failed");			\
89 	if (_fs_##_fstest_mount(_tc_, _args_, FSTEST_MNTNAME, 0) != 0)	\
90 		atf_tc_fail_errno("mount failed");			\
91 } while (/*CONSTCOND*/0);
92 
93 #define FSTEST_DESTRUCTOR(_tc_, _fs_, _args_)				\
94 do {									\
95 	if (_fs_##_fstest_unmount(_tc_, FSTEST_MNTNAME, 0) != 0) {	\
96 		rump_pub_vfs_mount_print(FSTEST_MNTNAME, 1);		\
97 		atf_tc_fail_errno("unmount failed");			\
98 	}								\
99 	if (_fs_##_fstest_delfs(_tc_, _args_) != 0)			\
100 		atf_tc_fail_errno("delfs failed");			\
101 } while (/*CONSTCOND*/0);
102 
103 #define ATF_TC_FSADD(fs,type,func,desc)					\
104 	ATF_TC(fs##_##func);						\
105 	ATF_TC_HEAD(fs##_##func,tc)					\
106 	{								\
107 		atf_tc_set_md_var(tc, "descr", type " test for " desc);	\
108 		atf_tc_set_md_var(tc, "X-fs.type", #fs);		\
109 		atf_tc_set_md_var(tc, "X-fs.mntname", type);		\
110 	}								\
111 	void *fs##func##tmp;						\
112 									\
113 	ATF_TC_BODY(fs##_##func,tc)					\
114 	{								\
115 		if (!atf_check_fstype(tc, #fs))				\
116 			atf_tc_skip("filesystem not selected");		\
117 		FSTEST_CONSTRUCTOR(tc,fs,fs##func##tmp);		\
118 		func(tc,FSTEST_MNTNAME);				\
119 		if (fs##_fstest_unmount(tc, FSTEST_MNTNAME, 0) != 0) {	\
120 			rump_pub_vfs_mount_print(FSTEST_MNTNAME, 1);	\
121 			atf_tc_fail_errno("unmount failed");		\
122 		}							\
123 	}
124 
125 #define ATF_TC_FSADD_RO(_fs_,_type_,_func_,_desc_,_gen_)		\
126 	ATF_TC(_fs_##_##_func_);					\
127 	ATF_TC_HEAD(_fs_##_##_func_,tc)					\
128 	{								\
129 		atf_tc_set_md_var(tc, "descr",_type_" test for "_desc_);\
130 		atf_tc_set_md_var(tc, "X-fs.type", #_fs_);		\
131 		atf_tc_set_md_var(tc, "X-fs.mntname", _type_);		\
132 	}								\
133 	void *_fs_##_func_##tmp;					\
134 									\
135 	ATF_TC_BODY(_fs_##_##_func_,tc)					\
136 	{								\
137 		if (!atf_check_fstype(tc, #_fs_))			\
138 			atf_tc_skip("filesystem not selected");		\
139 		FSTEST_CONSTRUCTOR(tc,_fs_,_fs_##_func_##tmp);		\
140 		_gen_(tc,FSTEST_MNTNAME);				\
141 		if (_fs_##_fstest_unmount(tc, FSTEST_MNTNAME, 0) != 0)	\
142 			atf_tc_fail_errno("unmount r/w failed");	\
143 		if (_fs_##_fstest_mount(tc, _fs_##_func_##tmp,		\
144 		    FSTEST_MNTNAME, MNT_RDONLY) != 0)			\
145 		atf_tc_fail_errno("mount ro failed");			\
146 		_func_(tc,FSTEST_MNTNAME);				\
147 		if (_fs_##_fstest_unmount(tc, FSTEST_MNTNAME, 0) != 0) {\
148 			rump_pub_vfs_mount_print(FSTEST_MNTNAME, 1);	\
149 			atf_tc_fail_errno("unmount failed");		\
150 		}							\
151 	}
152 
153 #define ATF_TP_FSADD(fs,func)						\
154   ATF_TP_ADD_TC(tp,fs##_##func)
155 
156 #define ATF_TC_FSAPPLY(func,desc)					\
157   ATF_TC_FSADD(ext2fs,MOUNT_EXT2FS,func,desc)				\
158   ATF_TC_FSADD(ffs,MOUNT_FFS,func,desc)					\
159   ATF_TC_FSADD(ffslog,MOUNT_FFS,func,desc)				\
160   ATF_TC_FSADD(lfs,MOUNT_LFS,func,desc)					\
161   ATF_TC_FSADD(msdosfs,MOUNT_MSDOS,func,desc)				\
162   ATF_TC_FSADD(nfs,MOUNT_NFS,func,desc)					\
163   ATF_TC_FSADD(puffs,MOUNT_PUFFS,func,desc)				\
164   ATF_TC_FSADD(p2k_ffs,MOUNT_PUFFS,func,desc)				\
165   ATF_TC_FSADD(rumpfs,MOUNT_RUMPFS,func,desc)				\
166   ATF_TC_FSADD(sysvbfs,MOUNT_SYSVBFS,func,desc)				\
167   ATF_TC_FSADD(tmpfs,MOUNT_TMPFS,func,desc)
168 
169 #define ATF_TP_FSAPPLY(func)						\
170   ATF_TP_FSADD(ext2fs,func);						\
171   ATF_TP_FSADD(ffs,func);						\
172   ATF_TP_FSADD(ffslog,func);						\
173   ATF_TP_FSADD(lfs,func);						\
174   ATF_TP_FSADD(msdosfs,func);						\
175   ATF_TP_FSADD(nfs,func);						\
176   ATF_TP_FSADD(puffs,func);						\
177   ATF_TP_FSADD(p2k_ffs,func);						\
178   ATF_TP_FSADD(rumpfs,func);						\
179   ATF_TP_FSADD(sysvbfs,func);						\
180   ATF_TP_FSADD(tmpfs,func);
181 
182 /*
183  * Same as above, but generate a file system image first and perform
184  * tests for a r/o mount.
185  *
186  * Missing the following file systems:
187  *   + lfs (fstest_lfs routines cannot handle remount.  FIXME!)
188  *   + tmpfs (memory backend)
189  *   + rumpfs (memory backend)
190  *   + puffs (memory backend, but could be run in theory)
191  */
192 
193 #define ATF_TC_FSAPPLY_RO(func,desc,gen)				\
194   ATF_TC_FSADD_RO(ext2fs,MOUNT_EXT2FS,func,desc,gen)			\
195   ATF_TC_FSADD_RO(ffs,MOUNT_FFS,func,desc,gen)				\
196   ATF_TC_FSADD_RO(ffslog,MOUNT_FFS,func,desc,gen)			\
197   ATF_TC_FSADD_RO(msdosfs,MOUNT_MSDOS,func,desc,gen)			\
198   ATF_TC_FSADD_RO(nfs,MOUNT_NFS,func,desc,gen)				\
199   ATF_TC_FSADD_RO(nfsro,MOUNT_NFS,func,desc,gen)			\
200   ATF_TC_FSADD_RO(sysvbfs,MOUNT_SYSVBFS,func,desc,gen)
201 
202 #define ATF_TP_FSAPPLY_RO(func)						\
203   ATF_TP_FSADD(ext2fs,func);						\
204   ATF_TP_FSADD(ffs,func);						\
205   ATF_TP_FSADD(ffslog,func);						\
206   ATF_TP_FSADD(msdosfs,func);						\
207   ATF_TP_FSADD(nfs,func);						\
208   ATF_TP_FSADD(nfsro,func);						\
209   ATF_TP_FSADD(sysvbfs,func);
210 
211 #define ATF_FSAPPLY(func,desc)						\
212 	ATF_TC_FSAPPLY(func,desc);					\
213 	ATF_TP_ADD_TCS(tp)						\
214 	{								\
215 		ATF_TP_FSAPPLY(func);					\
216 		return atf_no_error();					\
217 	}
218 
219 static __inline bool
220 atf_check_fstype(const atf_tc_t *tc, const char *fs)
221 {
222 	const char *fstype;
223 
224 	if (!atf_tc_has_config_var(tc, "fstype"))
225 		return true;
226 
227 	fstype = atf_tc_get_config_var(tc, "fstype");
228 	if (strcmp(fstype, fs) == 0)
229 		return true;
230 	return false;
231 }
232 
233 #define FSTYPE_EXT2FS(tc)\
234     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "ext2fs") == 0)
235 #define FSTYPE_FFS(tc)\
236     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "ffs") == 0)
237 #define FSTYPE_FFSLOG(tc)\
238     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "ffslog") == 0)
239 #define FSTYPE_LFS(tc)\
240     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "lfs") == 0)
241 #define FSTYPE_MSDOS(tc)\
242     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "msdosfs") == 0)
243 #define FSTYPE_NFS(tc)\
244     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "nfs") == 0)
245 #define FSTYPE_NFSRO(tc)\
246     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "nfsro") == 0)
247 #define FSTYPE_P2K_FFS(tc)\
248     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "p2k_ffs") == 0)
249 #define FSTYPE_PUFFS(tc)\
250     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "puffs") == 0)
251 #define FSTYPE_RUMPFS(tc)\
252     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "rumpfs") == 0)
253 #define FSTYPE_SYSVBFS(tc)\
254     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "sysvbfs") == 0)
255 #define FSTYPE_TMPFS(tc)\
256     (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "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