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 161 return(0); 162 } 163 164 static void test_master_id(const char *progname, int master_id) 165 { 166 switch (master_id) { 167 case 0: 168 fprintf(stderr, 169 "%s: Warning: a master id of 0 is the default, " 170 "explicit settings should use 1-15\n", 171 progname); 172 break; 173 case -1: 174 fprintf(stderr, 175 "%s: Warning: a master id of -1 is nomirror mode, " 176 "equivalent to -o nomirror option\n", 177 progname); 178 break; 179 case 1 ... 15: /* gcc */ 180 /* Expected values via -o master= option */ 181 break; 182 default: 183 /* This will eventually fail in hammer_vfs_mount() */ 184 fprintf(stderr, 185 "%s: Warning: A master id of %d is not supported\n", 186 progname, master_id); 187 break; 188 } 189 } 190 191 /* 192 * Extract a volume list 193 */ 194 static 195 void 196 extract_volumes(struct hammer_mount_info *info, char **av, int ac) 197 { 198 int idx = 0; 199 int arymax = 32; 200 char **ary = malloc(sizeof(char *) * arymax); 201 char *ptr; 202 char *next; 203 char *orig; 204 205 while (ac) { 206 if (idx == arymax) { 207 arymax += 32; 208 ary = realloc(ary, sizeof(char *) * arymax); 209 } 210 if (strchr(*av, ':') == NULL) { 211 ary[idx++] = strdup(*av); 212 } else { 213 orig = next = strdup(*av); 214 while ((ptr = next) != NULL) { 215 if (idx == arymax) { 216 arymax += 32; 217 ary = realloc(ary, sizeof(char *) * 218 arymax); 219 } 220 if ((next = strchr(ptr, ':')) != NULL) 221 *next++ = 0; 222 ary[idx++] = strdup(ptr); 223 } 224 free(orig); 225 } 226 --ac; 227 ++av; 228 } 229 info->nvolumes = idx; 230 info->volumes = ary; 231 } 232 233 static 234 void 235 free_volumes(struct hammer_mount_info *info) 236 { 237 int i; 238 239 for (i = 0; i < info->nvolumes; i++) 240 free(info->volumes[i]); 241 free(info->volumes); 242 } 243 244 /* 245 * This function is based on __verify_volume() in sbin/hammer/ondisk.c. 246 */ 247 static 248 void 249 __verify_volume(hammer_volume_ondisk_t ondisk, 250 const char *vol_name, int vol_count) 251 { 252 if (ondisk->vol_signature != HAMMER_FSBUF_VOLUME) { 253 errx(1, "%s: Invalid volume signature %016jx", 254 vol_name, ondisk->vol_signature); 255 } 256 if (ondisk->vol_count != vol_count) { 257 errx(1, "%s: Invalid volume count %d, " 258 "volume header says %d volumes", 259 vol_name, vol_count, ondisk->vol_count); 260 } 261 if (ondisk->vol_rootvol != HAMMER_ROOT_VOLNO) { 262 errx(1, "%s: Invalid root volume# %d", 263 vol_name, ondisk->vol_rootvol); 264 } 265 } 266 267 /* 268 * This function prints a possible reason that mount(2) failed, 269 * which isn't really necessary as the real reason is likely to 270 * be in dmesg anyway, but was originally added by 1a607e3e. 271 */ 272 static 273 void 274 test_volumes(struct hammer_mount_info *info) 275 { 276 int i, fd; 277 char buf[2048]; /* sizeof(*ondisk) is 1928 */ 278 hammer_volume_ondisk_t ondisk = (hammer_volume_ondisk_t)buf; 279 280 for (i = 0; i < info->nvolumes; i++) { 281 const char *vol = info->volumes[i]; 282 fd = open(vol, O_RDONLY); 283 if (fd < 0) 284 err(1, "%s: Failed to open", vol); 285 286 bzero(buf, sizeof(buf)); 287 if (pread(fd, buf, sizeof(buf), 0) != sizeof(buf)) 288 err(1, "%s: Failed to read volume header", vol); 289 290 __verify_volume(ondisk, vol, info->nvolumes); 291 close(fd); 292 } 293 } 294 295 static 296 void 297 usage(void) 298 { 299 fprintf(stderr, "usage: mount_hammer [-o options] [-T transaction-id] " 300 "special ... node\n"); 301 fprintf(stderr, " mount_hammer [-o options] [-T transaction-id] " 302 "special[:special]* node\n"); 303 fprintf(stderr, " mount_hammer -u [-o options] node\n"); 304 exit(1); 305 } 306