xref: /netbsd/tests/fs/vfs/t_union.c (revision 6550d01e)
1 /*	$NetBSD: t_union.c,v 1.5 2011/01/13 11:00:19 pooka 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/union/union.h>
19 
20 #include "../../h_macros.h"
21 #include "../common/h_fsmacros.h"
22 
23 #define MSTR "magic bus"
24 
25 static void
26 xput_tfile(const char *mp, const char *path)
27 {
28 	char pathb[MAXPATHLEN];
29 	int fd;
30 
31 	strcpy(pathb, mp);
32 	strcat(pathb, "/");
33 	strcat(pathb, path);
34 
35 	RL(fd = rump_sys_open(pathb, O_CREAT | O_RDWR, 0777));
36 	if (rump_sys_write(fd, MSTR, sizeof(MSTR)) != sizeof(MSTR))
37 		atf_tc_fail_errno("write to testfile");
38 	RL(rump_sys_close(fd));
39 }
40 
41 static int
42 xread_tfile(const char *mp, const char *path)
43 {
44 	char pathb[MAXPATHLEN];
45 	char buf[128];
46 	int fd;
47 
48 	strcpy(pathb, mp);
49 	strcat(pathb, "/");
50 	strcat(pathb, path);
51 
52 	fd = rump_sys_open(pathb, O_RDONLY);
53 	if (fd == -1)
54 		return errno;
55 	if (rump_sys_read(fd, buf, sizeof(buf)) == -1)
56 		atf_tc_fail_errno("read tfile");
57 	RL(rump_sys_close(fd));
58 	if (strcmp(buf, MSTR) == 0)
59 		return 0;
60 	return EPROGMISMATCH;
61 }
62 
63 /*
64  * Mount a unionfs for testing.  Before calling, "mp" contains
65  * the upper layer.  Lowerpath is constructed so that the directory
66  * contains rumpfs.
67  */
68 static void
69 mountunion(const char *mp, char *lowerpath)
70 {
71 	struct union_args unionargs;
72 
73 	sprintf(lowerpath, "/lower");
74 	rump_sys_mkdir(lowerpath, 0777);
75 
76 	/* mount the union with our testfs as the upper layer */
77 	memset(&unionargs, 0, sizeof(unionargs));
78 	unionargs.target = lowerpath;
79 	unionargs.mntflags = UNMNT_BELOW;
80 
81 	if (rump_sys_mount(MOUNT_UNION, mp, 0,
82 	    &unionargs, sizeof(unionargs)) == -1) {
83 		if (errno == EOPNOTSUPP) {
84 			atf_tc_skip("fs does not support VOP_WHITEOUT");
85 		} else {
86 			atf_tc_fail_errno("union mount");
87 		}
88 	}
89 }
90 
91 #if 0
92 static void
93 toggleroot(void)
94 {
95 	static int status;
96 
97 	status ^= MNT_RDONLY;
98 
99 	printf("0x%x\n", status);
100 	RL(rump_sys_mount(MOUNT_RUMPFS, "/", status | MNT_UPDATE, NULL, 0));
101 }
102 #endif
103 
104 #define TFILE "tensti"
105 #define TDIR "testdir"
106 #define TDFILE TDIR "/indir"
107 
108 static void
109 basic(const atf_tc_t *tc, const char *mp)
110 {
111 	char lowerpath[MAXPATHLEN];
112 	char dbuf[8192];
113 	struct stat sb;
114 	struct dirent *dp;
115 	int error, fd, dsize;
116 
117 	mountunion(mp, lowerpath);
118 
119 	/* create a file in the lower layer */
120 	xput_tfile(lowerpath, TFILE);
121 
122 	/* first, test we can read the old file from the new namespace */
123 	error = xread_tfile(mp, TFILE);
124 	if (error != 0)
125 		atf_tc_fail("union compare failed: %d (%s)",
126 		    error, strerror(error));
127 
128 	/* then, test upper layer writes don't affect the lower layer */
129 	xput_tfile(mp, "kiekko");
130 	if ((error = xread_tfile(lowerpath, "kiekko")) != ENOENT)
131 		atf_tc_fail("invisibility failed: %d (%s)",
132 		    error, strerror(error));
133 
134 	/* check that we can whiteout stuff in the upper layer */
135 	FSTEST_ENTER();
136 	if (FSTYPE_FFSLOG(tc)) {
137 		atf_tc_expect_signal(SIGABRT, "PR kern/44377");
138 	}
139 	RL(rump_sys_unlink(TFILE));
140 	atf_tc_expect_pass();
141 	ATF_REQUIRE_ERRNO(ENOENT, rump_sys_stat(TFILE, &sb) == -1);
142 	FSTEST_EXIT();
143 
144 	/* check that the removed node is not in the directory listing */
145 	RL(fd = rump_sys_open(mp, O_RDONLY));
146 	RL(dsize = rump_sys_getdents(fd, dbuf, sizeof(dbuf)));
147 	for (dp = (struct dirent *)dbuf;
148 	    (char *)dp < dbuf + dsize;
149 	    dp = _DIRENT_NEXT(dp)) {
150 		if (strcmp(dp->d_name, TFILE) == 0 && dp->d_type != DT_WHT)
151 			atf_tc_fail("removed file non-white-outed");
152 	}
153 	RL(rump_sys_close(fd));
154 
155 	RL(rump_sys_unmount(mp, 0));
156 }
157 
158 static void
159 whiteout(const atf_tc_t *tc, const char *mp)
160 {
161 	char lower[MAXPATHLEN];
162 	struct stat sb;
163 	void *fsarg;
164 
165 	/*
166 	 * XXX: use ffs here to make sure any screwups in rumpfs don't
167 	 * affect the test
168 	 */
169 	RL(ffs_fstest_newfs(tc, &fsarg, "daimage", 1024*1024*5, NULL));
170 	RL(ffs_fstest_mount(tc, fsarg, "/lower", 0));
171 
172 	/* create a file in the lower layer */
173 	RL(rump_sys_chdir("/lower"));
174 	RL(rump_sys_mkdir(TDIR, 0777));
175 	RL(rump_sys_mkdir(TDFILE, 0777));
176 	RL(rump_sys_chdir("/"));
177 
178 	RL(ffs_fstest_unmount(tc, "/lower", 0));
179 	RL(ffs_fstest_mount(tc, fsarg, "/lower", MNT_RDONLY));
180 
181 	mountunion(mp, lower);
182 
183 	/* all file systems fail sooner or later */
184 	atf_tc_expect_fail("PR kern/44383");
185 	FSTEST_ENTER();
186 	RL(rump_sys_rmdir(TDIR));
187 	ATF_REQUIRE_ERRNO(ENOENT, rump_sys_stat(TDFILE, &sb) == -1);
188 	ATF_REQUIRE_ERRNO(ENOENT, rump_sys_stat(TDIR, &sb) == -1);
189 
190 	RL(rump_sys_mkdir(TDIR, 0777));
191 	RL(rump_sys_stat(TDIR, &sb));
192 	ATF_REQUIRE_ERRNO(ENOENT, rump_sys_stat(TDFILE, &sb) == -1);
193 	FSTEST_EXIT();
194 
195 	RL(rump_sys_unmount(mp, 0));
196 }
197 
198 ATF_TC_FSAPPLY(basic, "check basic union functionality");
199 ATF_TC_FSAPPLY(whiteout, "create whiteout in upper layer");
200 
201 ATF_TP_ADD_TCS(tp)
202 {
203 
204 	ATF_TP_FSAPPLY(basic);
205 	ATF_TP_FSAPPLY(whiteout);
206 
207 	return atf_no_error();
208 }
209