t_basic.c revision 314817
1/*	$NetBSD: t_basic.c,v 1.4 2017/01/13 21:30:40 christos Exp $	*/
2
3#include <sys/types.h>
4#include <sys/mount.h>
5
6#include <atf-c.h>
7#include <err.h>
8#include <errno.h>
9#include <fcntl.h>
10#include <stdio.h>
11#include <unistd.h>
12#include <string.h>
13#include <stdlib.h>
14
15#include <rump/rump.h>
16#include <rump/rump_syscalls.h>
17
18#include <miscfs/nullfs/null.h>
19#include <fs/tmpfs/tmpfs_args.h>
20
21#include "h_macros.h"
22
23ATF_TC(basic);
24ATF_TC_HEAD(basic, tc)
25{
26	atf_tc_set_md_var(tc, "descr", "basic nullfs functionality");
27}
28
29#define MSTR "magic bus"
30
31static void
32xput_tfile(const char *path, const char *mstr)
33{
34	int fd;
35
36	fd = rump_sys_open(path, O_CREAT | O_RDWR, 0777);
37	if (fd == -1)
38		atf_tc_fail_errno("create %s", path);
39	if (rump_sys_write(fd, MSTR, sizeof(MSTR)) != sizeof(MSTR))
40		atf_tc_fail_errno("write to testfile");
41	rump_sys_close(fd);
42}
43
44static int
45xread_tfile(const char *path, const char *mstr)
46{
47	char buf[128];
48	int fd;
49
50	fd = rump_sys_open(path, O_RDONLY);
51	if (fd == -1)
52		return errno;
53	if (rump_sys_read(fd, buf, sizeof(buf)) == -1)
54		atf_tc_fail_errno("read tfile");
55	rump_sys_close(fd);
56	if (strcmp(buf, MSTR) == 0)
57		return 0;
58	return EPROGMISMATCH;
59}
60
61static void
62mountnull(const char *what, const char *mp, int flags)
63{
64	struct null_args nargs;
65
66	memset(&nargs, 0, sizeof(nargs));
67	nargs.nulla_target = __UNCONST(what);
68	if (rump_sys_mount(MOUNT_NULL, mp, flags, &nargs, sizeof(nargs)) == -1)
69		atf_tc_fail_errno("could not mount nullfs");
70
71}
72
73ATF_TC_BODY(basic, tc)
74{
75	struct tmpfs_args targs;
76	struct stat sb;
77	int error;
78
79	rump_init();
80	if (rump_sys_mkdir("/td1", 0777) == -1)
81		atf_tc_fail_errno("mp1");
82	if (rump_sys_mkdir("/td2", 0777) == -1)
83		atf_tc_fail_errno("mp1");
84
85	/* use tmpfs because rumpfs doesn't support regular files */
86	memset(&targs, 0, sizeof(targs));
87	targs.ta_version = TMPFS_ARGS_VERSION;
88	targs.ta_root_mode = 0777;
89	if (rump_sys_mount(MOUNT_TMPFS, "/td1", 0, &targs, sizeof(targs)) == -1)
90		atf_tc_fail_errno("could not mount tmpfs td1");
91
92	mountnull("/td1", "/td2", 0);
93
94	/* test unnull -> null */
95	xput_tfile("/td1/tensti", "jeppe");
96	error = xread_tfile("/td2/tensti", "jeppe");
97	if (error != 0)
98		atf_tc_fail("null compare failed: %d (%s)",
99		    error, strerror(error));
100
101	/* test null -> unnull */
102	xput_tfile("/td2/kiekko", "keppi");
103	error = xread_tfile("/td1/kiekko", "keppi");
104	if (error != 0)
105		atf_tc_fail("unnull compare failed: %d (%s)",
106		    error, strerror(error));
107
108	/* test unnull -> null overwrite */
109	xput_tfile("/td1/tensti", "se oolannin sota");
110	error = xread_tfile("/td2/tensti", "se oolannin sota");
111	if (error != 0)
112		atf_tc_fail("unnull compare failed: %d (%s)",
113		    error, strerror(error));
114
115	/* test that /td2 is unaffected in "real life" */
116	if (rump_sys_unmount("/td2", 0) == -1)
117		atf_tc_fail_errno("cannot unmount nullfs");
118	if ((error = rump_sys_stat("/td2/tensti", &sb)) != -1
119	    || errno != ENOENT) {
120		atf_tc_fail("stat tensti should return ENOENT, got %d", error);
121	}
122	if ((error = rump_sys_stat("/td2/kiekko", &sb)) != -1
123	    || errno != ENOENT) {
124		atf_tc_fail("stat kiekko should return ENOENT, got %d", error);
125	}
126
127	/* done */
128}
129
130ATF_TC(twistymount);
131ATF_TC_HEAD(twistymount, tc)
132{
133
134	/* this is expected to fail until the PR is fixed */
135	atf_tc_set_md_var(tc, "descr", "\"recursive\" mounts deadlock"
136	    " (kern/43439)");
137}
138
139/*
140 * Mapping to identifiers in kern/43439:
141 *  /td		= /home/current/pkgsrc
142 *  /td/dist	= /home/current/pkgsrc/distiles
143 *  /mp		= /usr/pkgsrc
144 *  /mp/dist	= /usr/pkgsrc/distfiles -- "created" by first null mount
145 */
146
147ATF_TC_BODY(twistymount, tc)
148{
149	int mkd = 0;
150
151	rump_init();
152
153	if (rump_sys_mkdir("/td", 0777) == -1)
154		atf_tc_fail_errno("mkdir %d", mkd++);
155	if (rump_sys_mkdir("/td/dist", 0777) == -1)
156		atf_tc_fail_errno("mkdir %d", mkd++);
157	if (rump_sys_mkdir("/mp", 0777) == -1)
158		atf_tc_fail_errno("mkdir %d", mkd++);
159
160	/* MNT_RDONLY doesn't matter, but just for compat with the PR */
161	mountnull("/td", "/mp", MNT_RDONLY);
162	mountnull("/td/dist", "/mp/dist", 0);
163
164	/* if we didn't get a locking-against-meself panic, we passed */
165}
166
167ATF_TP_ADD_TCS(tp)
168{
169
170	ATF_TP_ADD_TC(tp, basic);
171	ATF_TP_ADD_TC(tp, twistymount);
172
173	return atf_no_error();
174}
175