fstest_puffs.c revision 1.4 1 1.4 pooka /* $NetBSD: fstest_puffs.c,v 1.4 2010/10/31 22:05:35 pooka Exp $ */
2 1.1 pooka
3 1.1 pooka /*
4 1.1 pooka * Copyright (c) 2010 The NetBSD Foundation, Inc.
5 1.1 pooka *
6 1.1 pooka * Redistribution and use in source and binary forms, with or without
7 1.1 pooka * modification, are permitted provided that the following conditions
8 1.1 pooka * are met:
9 1.1 pooka * 1. Redistributions of source code must retain the above copyright
10 1.1 pooka * notice, this list of conditions and the following disclaimer.
11 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 pooka * notice, this list of conditions and the following disclaimer in the
13 1.1 pooka * documentation and/or other materials provided with the distribution.
14 1.1 pooka *
15 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1 pooka * SUCH DAMAGE.
26 1.1 pooka */
27 1.1 pooka
28 1.1 pooka #include <sys/types.h>
29 1.1 pooka #include <sys/mount.h>
30 1.1 pooka #include <sys/socket.h>
31 1.1 pooka #include <sys/statvfs.h>
32 1.1 pooka #include <sys/wait.h>
33 1.1 pooka
34 1.1 pooka #include <assert.h>
35 1.1 pooka #include <atf-c.h>
36 1.1 pooka #include <err.h>
37 1.1 pooka #include <errno.h>
38 1.1 pooka #include <fcntl.h>
39 1.1 pooka #include <pthread.h>
40 1.1 pooka #include <puffs.h>
41 1.1 pooka #include <puffsdump.h>
42 1.1 pooka #include <signal.h>
43 1.1 pooka #include <stdio.h>
44 1.1 pooka #include <unistd.h>
45 1.1 pooka #include <string.h>
46 1.1 pooka #include <stdlib.h>
47 1.1 pooka
48 1.1 pooka #include <rump/rump.h>
49 1.1 pooka #include <rump/rump_syscalls.h>
50 1.1 pooka
51 1.1 pooka #include "h_fsmacros.h"
52 1.1 pooka
53 1.1 pooka #define BUFSIZE (128*1024)
54 1.1 pooka #define DTFS_DUMP "-o","dump"
55 1.1 pooka
56 1.1 pooka /*
57 1.1 pooka * Threads which shovel data between comfd and /dev/puffs.
58 1.1 pooka * (cannot use polling since fd's are in different namespaces)
59 1.1 pooka */
60 1.1 pooka static void *
61 1.1 pooka readshovel(void *arg)
62 1.1 pooka {
63 1.1 pooka struct putter_hdr *phdr;
64 1.1 pooka struct puffs_req *preq;
65 1.2 pooka struct puffstestargs *args = arg;
66 1.1 pooka char buf[BUFSIZE];
67 1.1 pooka int comfd, puffsfd;
68 1.1 pooka
69 1.2 pooka comfd = args->pta_servfd;
70 1.2 pooka puffsfd = args->pta_rumpfd;
71 1.1 pooka
72 1.1 pooka phdr = (void *)buf;
73 1.1 pooka preq = (void *)buf;
74 1.1 pooka
75 1.4 pooka rump_pub_lwproc_newlwp(1);
76 1.1 pooka
77 1.1 pooka for (;;) {
78 1.1 pooka ssize_t n;
79 1.1 pooka
80 1.1 pooka n = rump_sys_read(puffsfd, buf, sizeof(*phdr));
81 1.4 pooka if (n <= 0) {
82 1.4 pooka fprintf(stderr, "readshovel r1 %d / %d\n", n, errno);
83 1.1 pooka break;
84 1.4 pooka }
85 1.1 pooka
86 1.1 pooka assert(phdr->pth_framelen < BUFSIZE);
87 1.1 pooka n = rump_sys_read(puffsfd, buf+sizeof(*phdr),
88 1.1 pooka phdr->pth_framelen - sizeof(*phdr));
89 1.4 pooka if (n <= 0) {
90 1.4 pooka fprintf(stderr, "readshovel r2 %d / %d\n", n, errno);
91 1.1 pooka break;
92 1.4 pooka }
93 1.1 pooka
94 1.1 pooka /* Analyze request */
95 1.1 pooka if (PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VFS) {
96 1.1 pooka assert(preq->preq_optype < PUFFS_VFS_MAX);
97 1.2 pooka args->pta_vfs_toserv_ops[preq->preq_optype]++;
98 1.1 pooka } else if (PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN) {
99 1.1 pooka assert(preq->preq_optype < PUFFS_VN_MAX);
100 1.2 pooka args->pta_vn_toserv_ops[preq->preq_optype]++;
101 1.1 pooka }
102 1.1 pooka
103 1.1 pooka n = phdr->pth_framelen;
104 1.4 pooka if (write(comfd, buf, n) != n) {
105 1.4 pooka fprintf(stderr, "readshovel write %d / %d\n", n, errno);
106 1.1 pooka break;
107 1.4 pooka }
108 1.1 pooka }
109 1.1 pooka
110 1.4 pooka abort();
111 1.1 pooka }
112 1.1 pooka
113 1.1 pooka static void *
114 1.1 pooka writeshovel(void *arg)
115 1.1 pooka {
116 1.2 pooka struct puffstestargs *args = arg;
117 1.1 pooka struct putter_hdr *phdr;
118 1.1 pooka char buf[BUFSIZE];
119 1.1 pooka size_t toread;
120 1.1 pooka int comfd, puffsfd;
121 1.1 pooka
122 1.4 pooka rump_pub_lwproc_newlwp(1);
123 1.1 pooka
124 1.2 pooka comfd = args->pta_servfd;
125 1.2 pooka puffsfd = args->pta_rumpfd;
126 1.1 pooka
127 1.1 pooka phdr = (struct putter_hdr *)buf;
128 1.1 pooka
129 1.1 pooka for (;;) {
130 1.1 pooka uint64_t off;
131 1.1 pooka ssize_t n;
132 1.1 pooka
133 1.1 pooka /*
134 1.1 pooka * Need to write everything to the "kernel" in one chunk,
135 1.1 pooka * so make sure we have it here.
136 1.1 pooka */
137 1.1 pooka off = 0;
138 1.1 pooka toread = sizeof(struct putter_hdr);
139 1.1 pooka assert(toread < BUFSIZE);
140 1.1 pooka do {
141 1.1 pooka n = read(comfd, buf+off, toread);
142 1.1 pooka if (n <= 0) {
143 1.4 pooka fprintf(stderr, "writeshovel read %d / %d\n",
144 1.4 pooka n, errno);
145 1.1 pooka break;
146 1.1 pooka }
147 1.1 pooka off += n;
148 1.1 pooka if (off >= sizeof(struct putter_hdr))
149 1.1 pooka toread = phdr->pth_framelen - off;
150 1.1 pooka else
151 1.1 pooka toread = off - sizeof(struct putter_hdr);
152 1.1 pooka } while (toread);
153 1.1 pooka
154 1.1 pooka n = rump_sys_write(puffsfd, buf, phdr->pth_framelen);
155 1.4 pooka if ((size_t)n != phdr->pth_framelen) {
156 1.4 pooka fprintf(stderr, "writeshovel wr %d / %d\n", n, errno);
157 1.1 pooka break;
158 1.4 pooka }
159 1.1 pooka }
160 1.1 pooka
161 1.4 pooka abort();
162 1.1 pooka }
163 1.1 pooka
164 1.1 pooka static void
165 1.2 pooka rumpshovels(struct puffstestargs *args)
166 1.1 pooka {
167 1.1 pooka pthread_t pt;
168 1.1 pooka int rv;
169 1.1 pooka
170 1.1 pooka if ((rv = rump_init()) == -1)
171 1.1 pooka err(1, "rump_init");
172 1.1 pooka
173 1.2 pooka if (pthread_create(&pt, NULL, readshovel, args) == -1)
174 1.1 pooka err(1, "read shovel");
175 1.1 pooka pthread_detach(pt);
176 1.2 pooka
177 1.2 pooka if (pthread_create(&pt, NULL, writeshovel, args) == -1)
178 1.1 pooka err(1, "write shovel");
179 1.1 pooka pthread_detach(pt);
180 1.1 pooka }
181 1.1 pooka
182 1.1 pooka static void
183 1.1 pooka childfail(int sign)
184 1.1 pooka {
185 1.1 pooka
186 1.1 pooka atf_tc_fail("child died"); /* almost signal-safe */
187 1.1 pooka }
188 1.1 pooka
189 1.2 pooka struct puffstestargs *theargs; /* XXX */
190 1.2 pooka
191 1.1 pooka /* XXX: we don't support size */
192 1.1 pooka int
193 1.1 pooka puffs_fstest_newfs(const atf_tc_t *tc, void **argp,
194 1.2 pooka const char *image, off_t size, void *fspriv)
195 1.1 pooka {
196 1.1 pooka struct puffstestargs *args;
197 1.1 pooka char dtfs_path[MAXPATHLEN];
198 1.1 pooka char *dtfsargv[6];
199 1.2 pooka char **theargv;
200 1.1 pooka pid_t childpid;
201 1.1 pooka int *pflags;
202 1.1 pooka char comfd[16];
203 1.1 pooka int sv[2];
204 1.1 pooka int mntflags;
205 1.1 pooka size_t len;
206 1.1 pooka ssize_t n;
207 1.1 pooka
208 1.1 pooka *argp = NULL;
209 1.1 pooka
210 1.1 pooka args = malloc(sizeof(*args));
211 1.1 pooka if (args == NULL)
212 1.1 pooka return errno;
213 1.1 pooka
214 1.1 pooka pflags = &args->pta_pflags;
215 1.1 pooka
216 1.1 pooka /* build dtfs exec path from atf test dir */
217 1.1 pooka sprintf(dtfs_path, "%s/../puffs/h_dtfs/h_dtfs",
218 1.1 pooka atf_tc_get_config_var(tc, "srcdir"));
219 1.2 pooka
220 1.2 pooka if (fspriv) {
221 1.2 pooka theargv = fspriv;
222 1.2 pooka theargv[0] = dtfs_path;
223 1.2 pooka } else {
224 1.2 pooka dtfsargv[0] = dtfs_path;
225 1.2 pooka dtfsargv[1] = __UNCONST("-i");
226 1.2 pooka dtfsargv[2] = __UNCONST("-s");
227 1.2 pooka dtfsargv[3] = __UNCONST("dtfs");
228 1.2 pooka dtfsargv[4] = __UNCONST("fictional");
229 1.2 pooka dtfsargv[5] = NULL;
230 1.2 pooka
231 1.2 pooka theargv = dtfsargv;
232 1.2 pooka }
233 1.1 pooka
234 1.1 pooka /* Create sucketpair for communication with the real file server */
235 1.1 pooka if (socketpair(PF_LOCAL, SOCK_STREAM, 0, sv) == -1)
236 1.1 pooka return errno;
237 1.1 pooka
238 1.1 pooka signal(SIGCHLD, childfail);
239 1.1 pooka
240 1.1 pooka switch ((childpid = fork())) {
241 1.1 pooka case 0:
242 1.1 pooka close(sv[1]);
243 1.1 pooka snprintf(comfd, sizeof(sv[0]), "%d", sv[0]);
244 1.1 pooka if (setenv("PUFFS_COMFD", comfd, 1) == -1)
245 1.1 pooka return errno;
246 1.1 pooka
247 1.2 pooka if (execvp(theargv[0], theargv) == -1)
248 1.1 pooka return errno;
249 1.1 pooka case -1:
250 1.1 pooka return errno;
251 1.1 pooka default:
252 1.1 pooka close(sv[0]);
253 1.1 pooka break;
254 1.1 pooka }
255 1.1 pooka
256 1.1 pooka /* read args */
257 1.1 pooka if ((n = read(sv[1], &len, sizeof(len))) != sizeof(len))
258 1.1 pooka err(1, "mp 1 %zd", n);
259 1.1 pooka if (len > MAXPATHLEN)
260 1.1 pooka err(1, "mntpath > MAXPATHLEN");
261 1.1 pooka if ((size_t)read(sv[1], args->pta_dir, len) != len)
262 1.1 pooka err(1, "mp 2");
263 1.1 pooka if (read(sv[1], &len, sizeof(len)) != sizeof(len))
264 1.1 pooka err(1, "fn 1");
265 1.1 pooka if (len > MAXPATHLEN)
266 1.1 pooka err(1, "devpath > MAXPATHLEN");
267 1.1 pooka if ((size_t)read(sv[1], args->pta_dev, len) != len)
268 1.1 pooka err(1, "fn 2");
269 1.1 pooka if (read(sv[1], &mntflags, sizeof(mntflags)) != sizeof(mntflags))
270 1.1 pooka err(1, "mntflags");
271 1.2 pooka if (read(sv[1], &args->pta_pargslen, sizeof(args->pta_pargslen))
272 1.2 pooka != sizeof(args->pta_pargslen))
273 1.1 pooka err(1, "puffstest_args len");
274 1.1 pooka args->pta_pargs = malloc(args->pta_pargslen);
275 1.1 pooka if (args->pta_pargs == NULL)
276 1.1 pooka err(1, "malloc");
277 1.2 pooka if (read(sv[1], args->pta_pargs, args->pta_pargslen)
278 1.2 pooka != (ssize_t)args->pta_pargslen)
279 1.1 pooka err(1, "puffstest_args");
280 1.1 pooka if (read(sv[1], pflags, sizeof(*pflags)) != sizeof(*pflags))
281 1.1 pooka err(1, "pflags");
282 1.1 pooka
283 1.1 pooka args->pta_childpid = childpid;
284 1.1 pooka args->pta_servfd = sv[1];
285 1.1 pooka strlcpy(args->pta_dev, image, sizeof(args->pta_dev));
286 1.1 pooka
287 1.2 pooka *argp = theargs = args;
288 1.1 pooka
289 1.1 pooka return 0;
290 1.1 pooka }
291 1.1 pooka
292 1.1 pooka int
293 1.1 pooka puffs_fstest_mount(const atf_tc_t *tc, void *arg, const char *path, int flags)
294 1.1 pooka {
295 1.1 pooka struct puffstestargs *pargs = arg;
296 1.1 pooka int fd;
297 1.1 pooka
298 1.1 pooka rump_init();
299 1.1 pooka fd = rump_sys_open("/dev/puffs", O_RDWR);
300 1.1 pooka if (fd == -1)
301 1.1 pooka return fd;
302 1.1 pooka
303 1.1 pooka #if 0
304 1.1 pooka pa->pa_fd = fd;
305 1.1 pooka #else
306 1.1 pooka assert(fd == 0); /* XXX: FIXME */
307 1.1 pooka #endif
308 1.1 pooka
309 1.1 pooka if (rump_sys_mkdir(path, 0777) == -1)
310 1.1 pooka return -1;
311 1.1 pooka
312 1.1 pooka if (rump_sys_mount(MOUNT_PUFFS, path, flags,
313 1.1 pooka pargs->pta_pargs, pargs->pta_pargslen) == -1) {
314 1.1 pooka /* apply "to kill a child" to avoid atf hang (kludge) */
315 1.1 pooka kill(pargs->pta_childpid, SIGKILL);
316 1.1 pooka return -1;
317 1.1 pooka }
318 1.1 pooka
319 1.2 pooka pargs->pta_rumpfd = fd;
320 1.2 pooka rumpshovels(pargs);
321 1.1 pooka
322 1.1 pooka return 0;
323 1.1 pooka }
324 1.1 pooka
325 1.1 pooka int
326 1.1 pooka puffs_fstest_delfs(const atf_tc_t *tc, void *arg)
327 1.1 pooka {
328 1.1 pooka
329 1.2 pooka /* useless ... */
330 1.1 pooka return 0;
331 1.1 pooka }
332 1.1 pooka
333 1.1 pooka int
334 1.1 pooka puffs_fstest_unmount(const atf_tc_t *tc, const char *path, int flags)
335 1.1 pooka {
336 1.2 pooka struct puffstestargs *pargs = theargs;
337 1.2 pooka int status;
338 1.1 pooka int rv;
339 1.1 pooka
340 1.1 pooka /* ok, child might exit here */
341 1.1 pooka signal(SIGCHLD, SIG_IGN);
342 1.1 pooka
343 1.1 pooka rv = rump_sys_unmount(path, flags);
344 1.1 pooka if (rv)
345 1.1 pooka return rv;
346 1.1 pooka
347 1.2 pooka if ((rv = rump_sys_rmdir(path)) != 0)
348 1.2 pooka return rv;
349 1.2 pooka
350 1.2 pooka if (waitpid(pargs->pta_childpid, &status, WNOHANG) > 0)
351 1.2 pooka return 0;
352 1.2 pooka kill(pargs->pta_childpid, SIGTERM);
353 1.2 pooka usleep(10);
354 1.2 pooka if (waitpid(pargs->pta_childpid, &status, WNOHANG) > 0)
355 1.2 pooka return 0;
356 1.2 pooka kill(pargs->pta_childpid, SIGKILL);
357 1.2 pooka usleep(500);
358 1.2 pooka wait(&status);
359 1.2 pooka
360 1.2 pooka return 0;
361 1.1 pooka }
362