1/*	$NetBSD: h_fsmacros.h,v 1.44 2020/03/15 20:10:26 martin 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_)							\
46int _fs_##_fstest_newfs(const atf_tc_t *, void **, const char *,	\
47			off_t, void *);					\
48int _fs_##_fstest_delfs(const atf_tc_t *, void *);			\
49int _fs_##_fstest_mount(const atf_tc_t *, void *, const char *, int);	\
50int _fs_##_fstest_unmount(const atf_tc_t *, const char *, int);
51
52FSPROTOS(ext2fs);
53FSPROTOS(ffs);
54FSPROTOS(ffslog);
55FSPROTOS(lfs);
56FSPROTOS(msdosfs);
57FSPROTOS(nfs);
58FSPROTOS(nfsro);
59FSPROTOS(p2k_ffs);
60FSPROTOS(puffs);
61FSPROTOS(rumpfs);
62FSPROTOS(sysvbfs);
63FSPROTOS(tmpfs);
64FSPROTOS(udf);
65FSPROTOS(v7fs);
66FSPROTOS(zfs);
67
68#ifndef FSTEST_IMGNAME
69#define FSTEST_IMGNAME "image.fs"
70#endif
71#ifndef FSTEST_IMGSIZE
72#define FSTEST_IMGSIZE (10000 * 512)
73#endif
74#ifndef FSTEST_MNTNAME
75#define FSTEST_MNTNAME "/mnt"
76#endif
77
78#define FSTEST_CONSTRUCTOR(_tc_, _fs_, _args_)				\
79do {									\
80	struct statvfs fsstat;						\
81	if (statvfs(".", &fsstat) == 0 &&				\
82	    (fsstat.f_frsize * fsstat.f_bfree) <= FSTEST_IMGSIZE)	\
83		atf_tc_skip("not enough free space in work directory");	\
84	if (_fs_##_fstest_newfs(_tc_, &_args_,				\
85	    FSTEST_IMGNAME, FSTEST_IMGSIZE, NULL) != 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_CONSTRUCTOR_FSPRIV(_tc_, _fs_, _args_, _privargs_)	\
92do {									\
93	if (_fs_##_fstest_newfs(_tc_, &_args_,				\
94	    FSTEST_IMGNAME, FSTEST_IMGSIZE, _privargs_) != 0)		\
95		atf_tc_fail_errno("newfs failed");			\
96	if (_fs_##_fstest_mount(_tc_, _args_, FSTEST_MNTNAME, 0) != 0)	\
97		atf_tc_fail_errno("mount failed");			\
98} while (/*CONSTCOND*/0)
99
100#define FSTEST_DESTRUCTOR(_tc_, _fs_, _args_)				\
101do {									\
102	if (_fs_##_fstest_unmount(_tc_, FSTEST_MNTNAME, 0) != 0) {	\
103		rump_pub_vfs_mount_print(FSTEST_MNTNAME, 1);		\
104		atf_tc_fail_errno("unmount failed");			\
105	}								\
106	if (_fs_##_fstest_delfs(_tc_, _args_) != 0)			\
107		atf_tc_fail_errno("delfs failed");			\
108} while (/*CONSTCOND*/0)
109
110#define ATF_TC_FSADD(fs,type,func,desc)					\
111	ATF_TC(fs##_##func);						\
112	ATF_TC_HEAD(fs##_##func,tc)					\
113	{								\
114		atf_tc_set_md_var(tc, "descr", type " test for " desc);	\
115		atf_tc_set_md_var(tc, "X-fs.type", #fs);		\
116		atf_tc_set_md_var(tc, "X-fs.mntname", type);		\
117	}								\
118	void *fs##func##tmp;						\
119									\
120	ATF_TC_BODY(fs##_##func,tc)					\
121	{								\
122		if (!atf_check_fstype(tc, #fs))				\
123			atf_tc_skip("filesystem not selected");		\
124		FSTEST_CONSTRUCTOR(tc,fs,fs##func##tmp);		\
125		func(tc,FSTEST_MNTNAME);				\
126		if (fs##_fstest_unmount(tc, FSTEST_MNTNAME, 0) != 0) {	\
127			rump_pub_vfs_mount_print(FSTEST_MNTNAME, 1);	\
128			atf_tc_fail_errno("unmount failed");		\
129		}							\
130	}
131
132#define ATF_TC_FSADD_RO(_fs_,_type_,_func_,_desc_,_gen_)		\
133	ATF_TC(_fs_##_##_func_);					\
134	ATF_TC_HEAD(_fs_##_##_func_,tc)					\
135	{								\
136		atf_tc_set_md_var(tc, "descr",_type_" test for "_desc_);\
137		atf_tc_set_md_var(tc, "X-fs.type", #_fs_);		\
138		atf_tc_set_md_var(tc, "X-fs.mntname", _type_);		\
139	}								\
140	void *_fs_##_func_##tmp;					\
141									\
142	ATF_TC_BODY(_fs_##_##_func_,tc)					\
143	{								\
144		if (!atf_check_fstype(tc, #_fs_))			\
145			atf_tc_skip("filesystem not selected");		\
146		FSTEST_CONSTRUCTOR(tc,_fs_,_fs_##_func_##tmp);		\
147		_gen_(tc,FSTEST_MNTNAME);				\
148		if (_fs_##_fstest_unmount(tc, FSTEST_MNTNAME, 0) != 0)	\
149			atf_tc_fail_errno("unmount r/w failed");	\
150		if (_fs_##_fstest_mount(tc, _fs_##_func_##tmp,		\
151		    FSTEST_MNTNAME, MNT_RDONLY) != 0)			\
152			atf_tc_fail_errno("mount ro failed");		\
153		_func_(tc,FSTEST_MNTNAME);				\
154		if (_fs_##_fstest_unmount(tc, FSTEST_MNTNAME, 0) != 0) {\
155			rump_pub_vfs_mount_print(FSTEST_MNTNAME, 1);	\
156			atf_tc_fail_errno("unmount failed");		\
157		}							\
158	}
159
160#define ATF_TP_FSADD(fs,func)						\
161  ATF_TP_ADD_TC(tp,fs##_##func)
162
163#define ATF_TC_FSAPPLY_NOZFS(func,desc)					\
164  ATF_TC_FSADD(ext2fs,MOUNT_EXT2FS,func,desc)				\
165  ATF_TC_FSADD(ffs,MOUNT_FFS,func,desc)					\
166  ATF_TC_FSADD(ffslog,MOUNT_FFS,func,desc)				\
167  ATF_TC_FSADD(lfs,MOUNT_LFS,func,desc)					\
168  ATF_TC_FSADD(msdosfs,MOUNT_MSDOS,func,desc)				\
169  ATF_TC_FSADD(nfs,MOUNT_NFS,func,desc)					\
170  ATF_TC_FSADD(puffs,MOUNT_PUFFS,func,desc)				\
171  ATF_TC_FSADD(p2k_ffs,MOUNT_PUFFS,func,desc)				\
172  ATF_TC_FSADD(rumpfs,MOUNT_RUMPFS,func,desc)				\
173  ATF_TC_FSADD(sysvbfs,MOUNT_SYSVBFS,func,desc)				\
174  ATF_TC_FSADD(tmpfs,MOUNT_TMPFS,func,desc)				\
175  ATF_TC_FSADD(udf,MOUNT_UDF,func,desc)				\
176  ATF_TC_FSADD(v7fs,MOUNT_V7FS,func,desc)
177
178#define ATF_TP_FSAPPLY_NOZFS(func)					\
179  ATF_TP_FSADD(ext2fs,func);						\
180  ATF_TP_FSADD(ffs,func);						\
181  ATF_TP_FSADD(ffslog,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(p2k_ffs,func);						\
187  ATF_TP_FSADD(rumpfs,func);						\
188  ATF_TP_FSADD(sysvbfs,func);						\
189  ATF_TP_FSADD(tmpfs,func);						\
190  ATF_TP_FSADD(udf,func);						\
191  ATF_TP_FSADD(v7fs,func);
192
193/* XXX: this will not scale */
194#ifdef WANT_ZFS_TESTS
195#define ATF_TC_FSAPPLY(func,desc)					\
196  ATF_TC_FSAPPLY_NOZFS(func,desc)					\
197  ATF_TC_FSADD(zfs,MOUNT_ZFS,func,desc)
198#define ATF_TP_FSAPPLY(func)						\
199  ATF_TP_FSAPPLY_NOZFS(func)						\
200  ATF_TP_FSADD(zfs,func);
201
202#else /* !WANT_ZFS_TESTS */
203
204#define ATF_TC_FSAPPLY(func,desc)					\
205  ATF_TC_FSAPPLY_NOZFS(func,desc)
206#define ATF_TP_FSAPPLY(func)						\
207  ATF_TP_FSAPPLY_NOZFS(func)
208
209#endif /* WANT_ZFS_TESTS */
210
211/*
212 * Same as above, but generate a file system image first and perform
213 * tests for a r/o mount.
214 *
215 * Missing the following file systems:
216 *   + lfs (fstest_lfs routines cannot handle remount.  FIXME!)
217 *   + tmpfs (memory backend)
218 *   + rumpfs (memory backend)
219 *   + puffs (memory backend, but could be run in theory)
220 */
221
222#define ATF_TC_FSAPPLY_RO(func,desc,gen)				\
223  ATF_TC_FSADD_RO(ext2fs,MOUNT_EXT2FS,func,desc,gen)			\
224  ATF_TC_FSADD_RO(ffs,MOUNT_FFS,func,desc,gen)				\
225  ATF_TC_FSADD_RO(ffslog,MOUNT_FFS,func,desc,gen)			\
226  ATF_TC_FSADD_RO(msdosfs,MOUNT_MSDOS,func,desc,gen)			\
227  ATF_TC_FSADD_RO(nfs,MOUNT_NFS,func,desc,gen)				\
228  ATF_TC_FSADD_RO(nfsro,MOUNT_NFS,func,desc,gen)			\
229  ATF_TC_FSADD_RO(sysvbfs,MOUNT_SYSVBFS,func,desc,gen)			\
230  ATF_TC_FSADD_RO(udf,MOUNT_UDF,func,desc,gen)			\
231  ATF_TC_FSADD_RO(v7fs,MOUNT_V7FS,func,desc,gen)
232
233#define ATF_TP_FSAPPLY_RO(func)						\
234  ATF_TP_FSADD(ext2fs,func);						\
235  ATF_TP_FSADD(ffs,func);						\
236  ATF_TP_FSADD(ffslog,func);						\
237  ATF_TP_FSADD(msdosfs,func);						\
238  ATF_TP_FSADD(nfs,func);						\
239  ATF_TP_FSADD(nfsro,func);						\
240  ATF_TP_FSADD(sysvbfs,func);						\
241  ATF_TP_FSADD(udf,func);						\
242  ATF_TP_FSADD(v7fs,func);
243
244#define ATF_FSAPPLY(func,desc)						\
245	ATF_TC_FSAPPLY(func,desc);					\
246	ATF_TP_ADD_TCS(tp)						\
247	{								\
248		ATF_TP_FSAPPLY(func);					\
249		return atf_no_error();					\
250	}
251
252static __inline bool
253atf_check_fstype(const atf_tc_t *tc, const char *fs)
254{
255	const char *fstype;
256	struct statvfs fsstat;
257
258	if (strcmp(fs, "zfs") == 0) {
259		/* XXX ZFS hardcodes a minimal size */
260		if (statvfs(".", &fsstat) == 0 &&
261		    (fsstat.f_frsize * fsstat.f_bfree) <= 64*1024*1024)
262			atf_tc_skip("not enough free space in work directory");
263	}
264
265	if (!atf_tc_has_config_var(tc, "fstype"))
266		return true;
267
268	fstype = atf_tc_get_config_var(tc, "fstype");
269	if (strcmp(fstype, fs) == 0)
270		return true;
271	return false;
272}
273
274#define FSTYPE_EXT2FS(tc)\
275    (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "ext2fs") == 0)
276#define FSTYPE_FFS(tc)\
277    (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "ffs") == 0)
278#define FSTYPE_FFSLOG(tc)\
279    (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "ffslog") == 0)
280#define FSTYPE_LFS(tc)\
281    (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "lfs") == 0)
282#define FSTYPE_MSDOS(tc)\
283    (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "msdosfs") == 0)
284#define FSTYPE_NFS(tc)\
285    (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "nfs") == 0)
286#define FSTYPE_NFSRO(tc)\
287    (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "nfsro") == 0)
288#define FSTYPE_P2K_FFS(tc)\
289    (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "p2k_ffs") == 0)
290#define FSTYPE_PUFFS(tc)\
291    (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "puffs") == 0)
292#define FSTYPE_RUMPFS(tc)\
293    (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "rumpfs") == 0)
294#define FSTYPE_SYSVBFS(tc)\
295    (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "sysvbfs") == 0)
296#define FSTYPE_TMPFS(tc)\
297    (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "tmpfs") == 0)
298#define FSTYPE_UDF(tc)\
299    (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "udf") == 0)
300#define FSTYPE_V7FS(tc)\
301    (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "v7fs") == 0)
302#define FSTYPE_ZFS(tc)\
303    (strcmp(atf_tc_get_md_var(tc, "X-fs.type"), "zfs") == 0)
304
305#define FSTEST_ENTER()							\
306	if (rump_sys_chdir(FSTEST_MNTNAME) == -1)			\
307		atf_tc_fail_errno("failed to cd into test mount")
308
309#define FSTEST_EXIT()							\
310	if (rump_sys_chdir("/") == -1)					\
311		atf_tc_fail_errno("failed to cd out of test mount")
312
313/*
314 * file system args structures
315 */
316
317struct nfstestargs {
318	pid_t ta_childpid;
319	char ta_ethername[MAXPATHLEN];
320};
321
322struct puffstestargs {
323	uint8_t			*pta_pargs;
324	size_t			pta_pargslen;
325
326	int			pta_pflags;
327	pid_t			pta_childpid;
328
329	int			pta_rumpfd;
330	int			pta_servfd;
331
332	char			pta_dev[MAXPATHLEN];
333	char			pta_dir[MAXPATHLEN];
334
335	int			pta_mntflags;
336
337	int			pta_vfs_toserv_ops[PUFFS_VFS_MAX];
338	int			pta_vn_toserv_ops[PUFFS_VN_MAX];
339};
340
341#endif /* __H_FSMACROS_H_ */
342