h_fsmacros.h revision 1.32 1 /* $NetBSD: h_fsmacros.h,v 1.32 2011/01/07 19:54:48 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(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_WITH_CLEANUP(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 ATF_TC_CLEANUP(fs##_##func,tc) \
126 { \
127 if (!atf_check_fstype(tc, #fs)) \
128 return; \
129 if (fs##_fstest_delfs(tc, fs##func##tmp) != 0) \
130 atf_tc_fail_errno("delfs failed"); \
131 }
132
133 #define ATF_TC_FSADD_RO(_fs_,_type_,_func_,_desc_,_gen_) \
134 ATF_TC_WITH_CLEANUP(_fs_##_##_func_); \
135 ATF_TC_HEAD(_fs_##_##_func_,tc) \
136 { \
137 atf_tc_set_md_var(tc, "descr",_type_" test for "_desc_);\
138 atf_tc_set_md_var(tc, "X-fs.type", #_fs_); \
139 atf_tc_set_md_var(tc, "X-fs.mntname", _type_); \
140 } \
141 void *_fs_##_func_##tmp; \
142 \
143 ATF_TC_BODY(_fs_##_##_func_,tc) \
144 { \
145 if (!atf_check_fstype(tc, #_fs_)) \
146 atf_tc_skip("filesystem not selected"); \
147 FSTEST_CONSTRUCTOR(tc,_fs_,_fs_##_func_##tmp); \
148 _gen_(tc,FSTEST_MNTNAME); \
149 if (_fs_##_fstest_unmount(tc, FSTEST_MNTNAME, 0) != 0) \
150 atf_tc_fail_errno("unmount r/w failed"); \
151 if (_fs_##_fstest_mount(tc, _fs_##_func_##tmp, \
152 FSTEST_MNTNAME, MNT_RDONLY) != 0) \
153 atf_tc_fail_errno("mount ro failed"); \
154 _func_(tc,FSTEST_MNTNAME); \
155 if (_fs_##_fstest_unmount(tc, FSTEST_MNTNAME, 0) != 0) {\
156 rump_pub_vfs_mount_print(FSTEST_MNTNAME, 1); \
157 atf_tc_fail_errno("unmount failed"); \
158 } \
159 } \
160 \
161 ATF_TC_CLEANUP(_fs_##_##_func_,tc) \
162 { \
163 if (!atf_check_fstype(tc, #_fs_)) \
164 return; \
165 if (_fs_##_fstest_delfs(tc, _fs_##_func_##tmp) != 0) \
166 atf_tc_fail_errno("delfs failed"); \
167 }
168
169 #define ATF_TP_FSADD(fs,func) \
170 ATF_TP_ADD_TC(tp,fs##_##func)
171
172 #define ATF_TC_FSAPPLY(func,desc) \
173 ATF_TC_FSADD(ext2fs,MOUNT_EXT2FS,func,desc) \
174 ATF_TC_FSADD(ffs,MOUNT_FFS,func,desc) \
175 ATF_TC_FSADD(ffslog,MOUNT_FFS,func,desc) \
176 ATF_TC_FSADD(lfs,MOUNT_LFS,func,desc) \
177 ATF_TC_FSADD(msdosfs,MOUNT_MSDOS,func,desc) \
178 ATF_TC_FSADD(nfs,MOUNT_NFS,func,desc) \
179 ATF_TC_FSADD(puffs,MOUNT_PUFFS,func,desc) \
180 ATF_TC_FSADD(rumpfs,MOUNT_RUMPFS,func,desc) \
181 ATF_TC_FSADD(sysvbfs,MOUNT_SYSVBFS,func,desc) \
182 ATF_TC_FSADD(tmpfs,MOUNT_TMPFS,func,desc)
183
184 #define ATF_TP_FSAPPLY(func) \
185 ATF_TP_FSADD(ext2fs,func); \
186 ATF_TP_FSADD(ffs,func); \
187 ATF_TP_FSADD(ffslog,func); \
188 ATF_TP_FSADD(lfs,func); \
189 ATF_TP_FSADD(msdosfs,func); \
190 ATF_TP_FSADD(nfs,func); \
191 ATF_TP_FSADD(puffs,func); \
192 ATF_TP_FSADD(rumpfs,func); \
193 ATF_TP_FSADD(sysvbfs,func); \
194 ATF_TP_FSADD(tmpfs,func);
195
196 /*
197 * Same as above, but generate a file system image first and perform
198 * tests for a r/o mount.
199 *
200 * Missing the following file systems:
201 * + lfs (fstest_lfs routines cannot handle remount. FIXME!)
202 * + tmpfs (memory backend)
203 * + rumpfs (memory backend)
204 * + puffs (memory backend, but could be run in theory)
205 */
206
207 #define ATF_TC_FSAPPLY_RO(func,desc,gen) \
208 ATF_TC_FSADD_RO(ext2fs,MOUNT_EXT2FS,func,desc,gen) \
209 ATF_TC_FSADD_RO(ffs,MOUNT_FFS,func,desc,gen) \
210 ATF_TC_FSADD_RO(ffslog,MOUNT_FFS,func,desc,gen) \
211 ATF_TC_FSADD_RO(msdosfs,MOUNT_MSDOS,func,desc,gen) \
212 ATF_TC_FSADD_RO(nfs,MOUNT_NFS,func,desc,gen) \
213 ATF_TC_FSADD_RO(nfsro,MOUNT_NFS,func,desc,gen) \
214 ATF_TC_FSADD_RO(sysvbfs,MOUNT_SYSVBFS,func,desc,gen)
215
216 #define ATF_TP_FSAPPLY_RO(func) \
217 ATF_TP_FSADD(ext2fs,func); \
218 ATF_TP_FSADD(ffs,func); \
219 ATF_TP_FSADD(ffslog,func); \
220 ATF_TP_FSADD(msdosfs,func); \
221 ATF_TP_FSADD(nfs,func); \
222 ATF_TP_FSADD(nfsro,func); \
223 ATF_TP_FSADD(sysvbfs,func);
224
225 #define ATF_FSAPPLY(func,desc) \
226 ATF_TC_FSAPPLY(func,desc); \
227 ATF_TP_ADD_TCS(tp) \
228 { \
229 ATF_TP_FSAPPLY(func); \
230 return atf_no_error(); \
231 }
232
233 static __inline bool
234 atf_check_fstype(const atf_tc_t *tc, const char *fs)
235 {
236 const char *fstype;
237
238 if (!atf_tc_has_config_var(tc, "fstype"))
239 return true;
240
241 fstype = atf_tc_get_config_var(tc, "fstype");
242 if (strcmp(fstype, fs) == 0)
243 return true;
244 return false;
245 }
246
247 #define FSTYPE_EXT2FS(tc)\
248 (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "ext2fs") == 0)
249 #define FSTYPE_FFS(tc)\
250 (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "ffs") == 0)
251 #define FSTYPE_FFSLOG(tc)\
252 (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "ffslog") == 0)
253 #define FSTYPE_LFS(tc)\
254 (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "lfs") == 0)
255 #define FSTYPE_MSDOS(tc)\
256 (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "msdosfs") == 0)
257 #define FSTYPE_NFS(tc)\
258 (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "nfs") == 0)
259 #define FSTYPE_NFSRO(tc)\
260 (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "nfsro") == 0)
261 #define FSTYPE_P2K_FFS(tc)\
262 (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "p2k_ffs") == 0)
263 #define FSTYPE_PUFFS(tc)\
264 (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "puffs") == 0)
265 #define FSTYPE_RUMPFS(tc)\
266 (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "rumpfs") == 0)
267 #define FSTYPE_SYSVBFS(tc)\
268 (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "sysvbfs") == 0)
269 #define FSTYPE_TMPFS(tc)\
270 (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "tmpfs") == 0)
271
272 #define FSTEST_ENTER() \
273 if (rump_sys_chdir(FSTEST_MNTNAME) == -1) \
274 atf_tc_fail_errno("failed to cd into test mount")
275
276 #define FSTEST_EXIT() \
277 if (rump_sys_chdir("/") == -1) \
278 atf_tc_fail_errno("failed to cd out of test mount")
279
280 /*
281 * file system args structures
282 */
283
284 struct nfstestargs {
285 pid_t ta_childpid;
286 char ta_ethername[MAXPATHLEN];
287 };
288
289 struct puffstestargs {
290 uint8_t *pta_pargs;
291 size_t pta_pargslen;
292
293 int pta_pflags;
294 pid_t pta_childpid;
295
296 int pta_rumpfd;
297 int pta_servfd;
298
299 char pta_dev[MAXPATHLEN];
300 char pta_dir[MAXPATHLEN];
301
302 int pta_mntflags;
303
304 int pta_vfs_toserv_ops[PUFFS_VFS_MAX];
305 int pta_vn_toserv_ops[PUFFS_VN_MAX];
306 };
307
308 #endif /* __H_FSMACROS_H_ */
309