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