fstest_puffs.c revision 1.2 1 1.2 pooka /* $NetBSD: fstest_puffs.c,v 1.2 2010/07/30 16:15:05 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.1 pooka /* use static thread id */
76 1.1 pooka rump_pub_lwp_alloc_and_switch(0, 10);
77 1.1 pooka
78 1.1 pooka for (;;) {
79 1.1 pooka ssize_t n;
80 1.1 pooka
81 1.1 pooka n = rump_sys_read(puffsfd, buf, sizeof(*phdr));
82 1.1 pooka if (n <= 0)
83 1.1 pooka break;
84 1.1 pooka
85 1.1 pooka assert(phdr->pth_framelen < BUFSIZE);
86 1.1 pooka n = rump_sys_read(puffsfd, buf+sizeof(*phdr),
87 1.1 pooka phdr->pth_framelen - sizeof(*phdr));
88 1.1 pooka if (n <= 0)
89 1.1 pooka break;
90 1.1 pooka
91 1.1 pooka /* Analyze request */
92 1.1 pooka if (PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VFS) {
93 1.1 pooka assert(preq->preq_optype < PUFFS_VFS_MAX);
94 1.2 pooka args->pta_vfs_toserv_ops[preq->preq_optype]++;
95 1.1 pooka } else if (PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN) {
96 1.1 pooka assert(preq->preq_optype < PUFFS_VN_MAX);
97 1.2 pooka args->pta_vn_toserv_ops[preq->preq_optype]++;
98 1.1 pooka }
99 1.1 pooka
100 1.1 pooka n = phdr->pth_framelen;
101 1.1 pooka if (write(comfd, buf, n) != n)
102 1.1 pooka break;
103 1.1 pooka }
104 1.1 pooka
105 1.1 pooka return NULL;
106 1.1 pooka }
107 1.1 pooka
108 1.1 pooka static void *
109 1.1 pooka writeshovel(void *arg)
110 1.1 pooka {
111 1.2 pooka struct puffstestargs *args = arg;
112 1.1 pooka struct putter_hdr *phdr;
113 1.1 pooka char buf[BUFSIZE];
114 1.1 pooka size_t toread;
115 1.1 pooka int comfd, puffsfd;
116 1.1 pooka
117 1.1 pooka /* use static thread id */
118 1.1 pooka rump_pub_lwp_alloc_and_switch(0, 11);
119 1.1 pooka
120 1.2 pooka comfd = args->pta_servfd;
121 1.2 pooka puffsfd = args->pta_rumpfd;
122 1.1 pooka
123 1.1 pooka phdr = (struct putter_hdr *)buf;
124 1.1 pooka
125 1.1 pooka for (;;) {
126 1.1 pooka uint64_t off;
127 1.1 pooka ssize_t n;
128 1.1 pooka
129 1.1 pooka /*
130 1.1 pooka * Need to write everything to the "kernel" in one chunk,
131 1.1 pooka * so make sure we have it here.
132 1.1 pooka */
133 1.1 pooka off = 0;
134 1.1 pooka toread = sizeof(struct putter_hdr);
135 1.1 pooka assert(toread < BUFSIZE);
136 1.1 pooka do {
137 1.1 pooka n = read(comfd, buf+off, toread);
138 1.1 pooka if (n <= 0) {
139 1.1 pooka break;
140 1.1 pooka }
141 1.1 pooka off += n;
142 1.1 pooka if (off >= sizeof(struct putter_hdr))
143 1.1 pooka toread = phdr->pth_framelen - off;
144 1.1 pooka else
145 1.1 pooka toread = off - sizeof(struct putter_hdr);
146 1.1 pooka } while (toread);
147 1.1 pooka
148 1.1 pooka n = rump_sys_write(puffsfd, buf, phdr->pth_framelen);
149 1.1 pooka if ((size_t)n != phdr->pth_framelen)
150 1.1 pooka break;
151 1.1 pooka }
152 1.1 pooka
153 1.1 pooka return NULL;
154 1.1 pooka }
155 1.1 pooka
156 1.1 pooka static void
157 1.2 pooka rumpshovels(struct puffstestargs *args)
158 1.1 pooka {
159 1.1 pooka pthread_t pt;
160 1.1 pooka int rv;
161 1.1 pooka
162 1.1 pooka if ((rv = rump_init()) == -1)
163 1.1 pooka err(1, "rump_init");
164 1.1 pooka
165 1.2 pooka if (pthread_create(&pt, NULL, readshovel, args) == -1)
166 1.1 pooka err(1, "read shovel");
167 1.1 pooka pthread_detach(pt);
168 1.2 pooka
169 1.2 pooka if (pthread_create(&pt, NULL, writeshovel, args) == -1)
170 1.1 pooka err(1, "write shovel");
171 1.1 pooka pthread_detach(pt);
172 1.1 pooka }
173 1.1 pooka
174 1.1 pooka static void
175 1.1 pooka childfail(int sign)
176 1.1 pooka {
177 1.1 pooka
178 1.1 pooka atf_tc_fail("child died"); /* almost signal-safe */
179 1.1 pooka }
180 1.1 pooka
181 1.2 pooka struct puffstestargs *theargs; /* XXX */
182 1.2 pooka
183 1.1 pooka /* XXX: we don't support size */
184 1.1 pooka int
185 1.1 pooka puffs_fstest_newfs(const atf_tc_t *tc, void **argp,
186 1.2 pooka const char *image, off_t size, void *fspriv)
187 1.1 pooka {
188 1.1 pooka struct puffstestargs *args;
189 1.1 pooka char dtfs_path[MAXPATHLEN];
190 1.1 pooka char *dtfsargv[6];
191 1.2 pooka char **theargv;
192 1.1 pooka pid_t childpid;
193 1.1 pooka int *pflags;
194 1.1 pooka char comfd[16];
195 1.1 pooka int sv[2];
196 1.1 pooka int mntflags;
197 1.1 pooka size_t len;
198 1.1 pooka ssize_t n;
199 1.1 pooka
200 1.1 pooka *argp = NULL;
201 1.1 pooka
202 1.1 pooka args = malloc(sizeof(*args));
203 1.1 pooka if (args == NULL)
204 1.1 pooka return errno;
205 1.1 pooka
206 1.1 pooka pflags = &args->pta_pflags;
207 1.1 pooka
208 1.1 pooka /* build dtfs exec path from atf test dir */
209 1.1 pooka sprintf(dtfs_path, "%s/../puffs/h_dtfs/h_dtfs",
210 1.1 pooka atf_tc_get_config_var(tc, "srcdir"));
211 1.2 pooka
212 1.2 pooka if (fspriv) {
213 1.2 pooka theargv = fspriv;
214 1.2 pooka theargv[0] = dtfs_path;
215 1.2 pooka } else {
216 1.2 pooka dtfsargv[0] = dtfs_path;
217 1.2 pooka dtfsargv[1] = __UNCONST("-i");
218 1.2 pooka dtfsargv[2] = __UNCONST("-s");
219 1.2 pooka dtfsargv[3] = __UNCONST("dtfs");
220 1.2 pooka dtfsargv[4] = __UNCONST("fictional");
221 1.2 pooka dtfsargv[5] = NULL;
222 1.2 pooka
223 1.2 pooka theargv = dtfsargv;
224 1.2 pooka }
225 1.1 pooka
226 1.1 pooka /* Create sucketpair for communication with the real file server */
227 1.1 pooka if (socketpair(PF_LOCAL, SOCK_STREAM, 0, sv) == -1)
228 1.1 pooka return errno;
229 1.1 pooka
230 1.1 pooka signal(SIGCHLD, childfail);
231 1.1 pooka
232 1.1 pooka switch ((childpid = fork())) {
233 1.1 pooka case 0:
234 1.1 pooka close(sv[1]);
235 1.1 pooka snprintf(comfd, sizeof(sv[0]), "%d", sv[0]);
236 1.1 pooka if (setenv("PUFFS_COMFD", comfd, 1) == -1)
237 1.1 pooka return errno;
238 1.1 pooka
239 1.2 pooka if (execvp(theargv[0], theargv) == -1)
240 1.1 pooka return errno;
241 1.1 pooka case -1:
242 1.1 pooka return errno;
243 1.1 pooka default:
244 1.1 pooka close(sv[0]);
245 1.1 pooka break;
246 1.1 pooka }
247 1.1 pooka
248 1.1 pooka /* read args */
249 1.1 pooka if ((n = read(sv[1], &len, sizeof(len))) != sizeof(len))
250 1.1 pooka err(1, "mp 1 %zd", n);
251 1.1 pooka if (len > MAXPATHLEN)
252 1.1 pooka err(1, "mntpath > MAXPATHLEN");
253 1.1 pooka if ((size_t)read(sv[1], args->pta_dir, len) != len)
254 1.1 pooka err(1, "mp 2");
255 1.1 pooka if (read(sv[1], &len, sizeof(len)) != sizeof(len))
256 1.1 pooka err(1, "fn 1");
257 1.1 pooka if (len > MAXPATHLEN)
258 1.1 pooka err(1, "devpath > MAXPATHLEN");
259 1.1 pooka if ((size_t)read(sv[1], args->pta_dev, len) != len)
260 1.1 pooka err(1, "fn 2");
261 1.1 pooka if (read(sv[1], &mntflags, sizeof(mntflags)) != sizeof(mntflags))
262 1.1 pooka err(1, "mntflags");
263 1.2 pooka if (read(sv[1], &args->pta_pargslen, sizeof(args->pta_pargslen))
264 1.2 pooka != sizeof(args->pta_pargslen))
265 1.1 pooka err(1, "puffstest_args len");
266 1.1 pooka args->pta_pargs = malloc(args->pta_pargslen);
267 1.1 pooka if (args->pta_pargs == NULL)
268 1.1 pooka err(1, "malloc");
269 1.2 pooka if (read(sv[1], args->pta_pargs, args->pta_pargslen)
270 1.2 pooka != (ssize_t)args->pta_pargslen)
271 1.1 pooka err(1, "puffstest_args");
272 1.1 pooka if (read(sv[1], pflags, sizeof(*pflags)) != sizeof(*pflags))
273 1.1 pooka err(1, "pflags");
274 1.1 pooka
275 1.1 pooka args->pta_childpid = childpid;
276 1.1 pooka args->pta_servfd = sv[1];
277 1.1 pooka strlcpy(args->pta_dev, image, sizeof(args->pta_dev));
278 1.1 pooka
279 1.2 pooka *argp = theargs = args;
280 1.1 pooka
281 1.1 pooka return 0;
282 1.1 pooka }
283 1.1 pooka
284 1.1 pooka int
285 1.1 pooka puffs_fstest_mount(const atf_tc_t *tc, void *arg, const char *path, int flags)
286 1.1 pooka {
287 1.1 pooka struct puffstestargs *pargs = arg;
288 1.1 pooka int fd;
289 1.1 pooka
290 1.1 pooka rump_init();
291 1.1 pooka fd = rump_sys_open("/dev/puffs", O_RDWR);
292 1.1 pooka if (fd == -1)
293 1.1 pooka return fd;
294 1.1 pooka
295 1.1 pooka #if 0
296 1.1 pooka pa->pa_fd = fd;
297 1.1 pooka #else
298 1.1 pooka assert(fd == 0); /* XXX: FIXME */
299 1.1 pooka #endif
300 1.1 pooka
301 1.1 pooka if (rump_sys_mkdir(path, 0777) == -1)
302 1.1 pooka return -1;
303 1.1 pooka
304 1.1 pooka if (rump_sys_mount(MOUNT_PUFFS, path, flags,
305 1.1 pooka pargs->pta_pargs, pargs->pta_pargslen) == -1) {
306 1.1 pooka /* apply "to kill a child" to avoid atf hang (kludge) */
307 1.1 pooka kill(pargs->pta_childpid, SIGKILL);
308 1.1 pooka return -1;
309 1.1 pooka }
310 1.1 pooka
311 1.2 pooka pargs->pta_rumpfd = fd;
312 1.2 pooka rumpshovels(pargs);
313 1.1 pooka
314 1.1 pooka return 0;
315 1.1 pooka }
316 1.1 pooka
317 1.1 pooka int
318 1.1 pooka puffs_fstest_delfs(const atf_tc_t *tc, void *arg)
319 1.1 pooka {
320 1.1 pooka
321 1.2 pooka /* useless ... */
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_unmount(const atf_tc_t *tc, const char *path, int flags)
327 1.1 pooka {
328 1.2 pooka struct puffstestargs *pargs = theargs;
329 1.2 pooka int status;
330 1.1 pooka int rv;
331 1.1 pooka
332 1.1 pooka /* ok, child might exit here */
333 1.1 pooka signal(SIGCHLD, SIG_IGN);
334 1.1 pooka
335 1.1 pooka rv = rump_sys_unmount(path, flags);
336 1.1 pooka if (rv)
337 1.1 pooka return rv;
338 1.1 pooka
339 1.2 pooka if ((rv = rump_sys_rmdir(path)) != 0)
340 1.2 pooka return rv;
341 1.2 pooka
342 1.2 pooka if (waitpid(pargs->pta_childpid, &status, WNOHANG) > 0)
343 1.2 pooka return 0;
344 1.2 pooka kill(pargs->pta_childpid, SIGTERM);
345 1.2 pooka usleep(10);
346 1.2 pooka if (waitpid(pargs->pta_childpid, &status, WNOHANG) > 0)
347 1.2 pooka return 0;
348 1.2 pooka kill(pargs->pta_childpid, SIGKILL);
349 1.2 pooka usleep(500);
350 1.2 pooka wait(&status);
351 1.2 pooka
352 1.2 pooka return 0;
353 1.1 pooka }
354