1 1.3 pooka /* $NetBSD: rump_nqmfs.c,v 1.3 2010/03/31 14:54:07 pooka Exp $ */ 2 1.1 pooka 3 1.1 pooka /* 4 1.1 pooka * Copyright (c) 2010 Antti Kantee. All Rights Reserved. 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 /* 29 1.1 pooka * Not Quite MFS. Instead of allocating memory and creating a FFS file system 30 1.1 pooka * there, mmap a file (which should contain ffs) and use that as the backend. 31 1.1 pooka * You can also give a newly created image as the file ... 32 1.1 pooka */ 33 1.1 pooka 34 1.1 pooka #include <sys/types.h> 35 1.1 pooka #include <sys/mman.h> 36 1.1 pooka #include <sys/mount.h> 37 1.1 pooka #include <sys/stat.h> 38 1.1 pooka 39 1.1 pooka #include <ufs/ufs/ufsmount.h> 40 1.1 pooka 41 1.1 pooka #include <err.h> 42 1.1 pooka #include <mntopts.h> 43 1.1 pooka #include <puffs.h> 44 1.1 pooka #include <stdio.h> 45 1.1 pooka #include <stdlib.h> 46 1.1 pooka #include <unistd.h> 47 1.1 pooka 48 1.1 pooka #include <rump/p2k.h> 49 1.1 pooka #include <rump/ukfs.h> 50 1.1 pooka 51 1.1 pooka const struct mntopt mopts[] = { 52 1.1 pooka MOPT_STDOPTS, 53 1.1 pooka MOPT_NULL, 54 1.1 pooka }; 55 1.1 pooka 56 1.1 pooka static void 57 1.1 pooka usage(void) 58 1.1 pooka { 59 1.1 pooka 60 1.1 pooka fprintf(stderr, "%s: [-o args] fspec mountpoint\n", getprogname()); 61 1.1 pooka exit(1); 62 1.1 pooka } 63 1.1 pooka 64 1.1 pooka static struct mfs_args args; 65 1.1 pooka static char *mntpath, *mntfile; 66 1.1 pooka static int mntflags, altflags; 67 1.1 pooka 68 1.1 pooka int 69 1.1 pooka main(int argc, char *argv[]) 70 1.1 pooka { 71 1.1 pooka struct stat sb; 72 1.1 pooka mntoptparse_t mp; 73 1.1 pooka void *membase; 74 1.1 pooka int ch, fd, rdonly, shared; 75 1.1 pooka 76 1.1 pooka setprogname(argv[0]); 77 1.1 pooka puffs_unmountonsignal(SIGINT, true); 78 1.1 pooka puffs_unmountonsignal(SIGTERM, true); 79 1.1 pooka 80 1.1 pooka shared = mntflags = altflags = 0; 81 1.1 pooka 82 1.1 pooka memset(&args, 0, sizeof(args)); 83 1.1 pooka while ((ch = getopt(argc, argv, "o:s")) != -1) { 84 1.1 pooka switch (ch) { 85 1.1 pooka case 'o': 86 1.1 pooka mp = getmntopts(optarg, mopts, &mntflags, &altflags); 87 1.1 pooka if (mp == NULL) 88 1.1 pooka err(1, "getmntopts"); 89 1.1 pooka freemntopts(mp); 90 1.1 pooka break; 91 1.1 pooka case 's': 92 1.1 pooka shared = 1; 93 1.1 pooka break; 94 1.1 pooka default: 95 1.1 pooka usage(); 96 1.1 pooka break; 97 1.1 pooka } 98 1.1 pooka } 99 1.1 pooka argc -= optind; 100 1.1 pooka argv += optind; 101 1.1 pooka 102 1.1 pooka if (argc != 2) 103 1.1 pooka usage(); 104 1.1 pooka mntfile = argv[0]; 105 1.1 pooka mntpath = argv[1]; 106 1.1 pooka rdonly = mntflags & MNT_RDONLY; 107 1.1 pooka 108 1.1 pooka if (stat(mntfile, &sb) == -1) 109 1.1 pooka err(1, "stat fspec"); 110 1.1 pooka if (!S_ISREG(sb.st_mode)) 111 1.1 pooka errx(1, "fspec must be a regular file"); 112 1.1 pooka 113 1.1 pooka fd = open(mntfile, rdonly ? O_RDONLY: O_RDWR); 114 1.1 pooka if (fd == -1) 115 1.1 pooka err(1, "open fspec"); 116 1.1 pooka 117 1.1 pooka membase = mmap(NULL, sb.st_size, PROT_READ | (rdonly ? 0 : PROT_WRITE), 118 1.2 pooka MAP_FILE | (shared ? MAP_SHARED : MAP_PRIVATE), fd, 0); 119 1.1 pooka if (membase == MAP_FAILED) 120 1.3 pooka err(1, "cannot mmap fspec"); 121 1.1 pooka 122 1.1 pooka args.fspec = mntfile; 123 1.1 pooka args.base = membase; 124 1.1 pooka args.size = sb.st_size; 125 1.1 pooka 126 1.1 pooka if (p2k_run_fs(MOUNT_MFS, "/swabbie", mntpath, 127 1.1 pooka 0, &args, sizeof(args), 0) == -1) 128 1.1 pooka err(1, "p2k"); 129 1.1 pooka 130 1.1 pooka return 0; 131 1.1 pooka } 132