1 /* 2 * Copyright (c) 2007 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Matthew Dillon <dillon@backplane.com> 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 3. Neither the name of The DragonFly Project nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific, prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/types.h> 36 #include <sys/stat.h> 37 #include <sys/syslimits.h> 38 #include <vfs/hammer/hammer_mount.h> 39 #include <vfs/hammer/hammer_disk.h> 40 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <stdarg.h> 44 #include <stddef.h> 45 #include <unistd.h> 46 #include <string.h> 47 #include <fcntl.h> 48 #include <err.h> 49 #include <mntopts.h> 50 51 static void test_master_id(const char *progname, int master_id); 52 static void extract_volumes(struct hammer_mount_info *info, char **av, int ac); 53 static void free_volumes(struct hammer_mount_info *info); 54 static void test_volumes(struct hammer_mount_info *info); 55 static void usage(void); 56 57 #define MOPT_HAMMEROPTS \ 58 { "history", 1, HMNT_NOHISTORY, 1 }, \ 59 { "master=", 0, HMNT_MASTERID, 1 }, \ 60 { "mirror", 1, HMNT_NOMIRROR, 1 } 61 62 static struct mntopt mopts[] = { MOPT_STDOPTS, MOPT_HAMMEROPTS, 63 MOPT_UPDATE, MOPT_NULL }; 64 65 int 66 main(int ac, char **av) 67 { 68 struct hammer_mount_info info; 69 struct vfsconf vfc; 70 int mount_flags = 0; 71 int init_flags = 0; 72 int error; 73 int ch; 74 char *mountpt; 75 char *ptr; 76 77 bzero(&info, sizeof(info)); 78 79 while ((ch = getopt(ac, av, "o:T:u")) != -1) { 80 switch(ch) { 81 case 'T': 82 info.asof = strtoull(optarg, NULL, 0); 83 break; 84 case 'o': 85 getmntopts(optarg, mopts, &mount_flags, &info.hflags); 86 87 /* 88 * Handle extended flags with parameters. 89 */ 90 if (info.hflags & HMNT_MASTERID) { 91 ptr = strstr(optarg, "master="); 92 if (ptr) { 93 info.master_id = strtol(ptr + 7, NULL, 0); 94 test_master_id("mount_hammer", info.master_id); 95 } 96 } 97 if (info.hflags & HMNT_NOMIRROR) { 98 ptr = strstr(optarg, "nomirror"); 99 if (ptr) 100 info.master_id = -1; 101 } 102 break; 103 case 'u': 104 init_flags |= MNT_UPDATE; 105 break; 106 default: 107 usage(); 108 /* not reached */ 109 } 110 } 111 ac -= optind; 112 av += optind; 113 mount_flags |= init_flags; 114 115 /* 116 * Only the mount point need be specified in update mode. 117 */ 118 if (init_flags & MNT_UPDATE) { 119 if (ac != 1) { 120 usage(); 121 /* not reached */ 122 } 123 mountpt = av[0]; 124 if (mount(vfc.vfc_name, mountpt, mount_flags, &info)) 125 err(1, "mountpoint %s", mountpt); 126 exit(0); 127 } 128 129 if (ac < 2) { 130 usage(); 131 /* not reached */ 132 } 133 134 /* 135 * Mount arguments: vol [vol...] mountpt 136 */ 137 extract_volumes(&info, av, ac - 1); 138 mountpt = av[ac - 1]; 139 140 /* 141 * Load the hammer module if necessary (this bit stolen from 142 * mount_null). 143 */ 144 error = getvfsbyname("hammer", &vfc); 145 if (error && vfsisloadable("hammer")) { 146 if (vfsload("hammer") != 0) 147 err(1, "vfsload(hammer)"); 148 endvfsent(); 149 error = getvfsbyname("hammer", &vfc); 150 } 151 if (error) 152 errx(1, "hammer filesystem is not available"); 153 154 if (mount(vfc.vfc_name, mountpt, mount_flags, &info)) { 155 perror("mount"); 156 test_volumes(&info); 157 exit(1); 158 } 159 free_volumes(&info); 160 exit(0); 161 } 162 163 static void test_master_id(const char *progname, int master_id) 164 { 165 switch (master_id) { 166 case 0: 167 fprintf(stderr, 168 "%s: Warning: a master id of 0 is the default, " 169 "explicit settings should use 1-15\n", 170 progname); 171 break; 172 case -1: 173 fprintf(stderr, 174 "%s: Warning: a master id of -1 is nomirror mode, " 175 "equivalent to -o nomirror option\n", 176 progname); 177 break; 178 case 1 ... 15: /* gcc */ 179 /* Expected values via -o master= option */ 180 break; 181 default: 182 /* This will eventually fail in hammer_vfs_mount() */ 183 fprintf(stderr, 184 "%s: Warning: A master id of %d is not supported\n", 185 progname, master_id); 186 break; 187 } 188 } 189 190 /* 191 * Extract a volume list 192 */ 193 static 194 void 195 extract_volumes(struct hammer_mount_info *info, char **av, int ac) 196 { 197 int idx = 0; 198 int arymax = 32; 199 char **ary = malloc(sizeof(char *) * arymax); 200 char *ptr; 201 char *next; 202 char *orig; 203 204 while (ac) { 205 if (idx == arymax) { 206 arymax += 32; 207 ary = realloc(ary, sizeof(char *) * arymax); 208 } 209 if (strchr(*av, ':') == NULL) { 210 ary[idx++] = strdup(*av); 211 } else { 212 orig = next = strdup(*av); 213 while ((ptr = next) != NULL) { 214 if (idx == arymax) { 215 arymax += 32; 216 ary = realloc(ary, sizeof(char *) * 217 arymax); 218 } 219 if ((next = strchr(ptr, ':')) != NULL) 220 *next++ = 0; 221 ary[idx++] = strdup(ptr); 222 } 223 free(orig); 224 } 225 --ac; 226 ++av; 227 } 228 info->nvolumes = idx; 229 info->volumes = ary; 230 } 231 232 static 233 void 234 free_volumes(struct hammer_mount_info *info) 235 { 236 int i; 237 238 for (i = 0; i < info->nvolumes; i++) 239 free(info->volumes[i]); 240 free(info->volumes); 241 } 242 243 static 244 void 245 test_volumes(struct hammer_mount_info *info) 246 { 247 int i, fd, ret; 248 const char *vol; 249 char buf[HAMMER_BUFSIZE]; 250 hammer_volume_ondisk_t ondisk; 251 252 for (i = 0; i < info->nvolumes; i++) { 253 vol = info->volumes[i]; 254 fd = open(vol, O_RDONLY); 255 if (fd < 0) { 256 fprintf(stderr, "%s: Failed to open\n", vol); 257 goto next; 258 } 259 260 bzero(buf, HAMMER_BUFSIZE); 261 ret = pread(fd, buf, HAMMER_BUFSIZE, 0); 262 if (ret != HAMMER_BUFSIZE) { 263 fprintf(stderr, 264 "%s: Failed to read volume header\n", vol); 265 goto next; 266 } 267 268 ondisk = (hammer_volume_ondisk_t)buf; 269 if (ondisk->vol_signature != HAMMER_FSBUF_VOLUME) { 270 fprintf(stderr, 271 "%s: Invalid volume signature %016jx\n", 272 vol, ondisk->vol_signature); 273 goto next; 274 } 275 if (ondisk->vol_count != info->nvolumes) { 276 fprintf(stderr, 277 "%s: Volume header says %d volumes\n", 278 vol, ondisk->vol_count); 279 goto next; 280 } 281 next: 282 close(fd); 283 } 284 } 285 286 static 287 void 288 usage(void) 289 { 290 fprintf(stderr, "usage: mount_hammer [-o options] [-T transaction-id] " 291 "special ... node\n"); 292 fprintf(stderr, " mount_hammer [-o options] [-T transaction-id] " 293 "special[:special]* node\n"); 294 fprintf(stderr, " mount_hammer -u [-o options] node\n"); 295 exit(1); 296 } 297