sbin/mount_hammer: Add /* not reached */
[dragonfly.git] / sbin / mount_hammer / mount_hammer.c
bloba2ba32ed3843e5e9f03340cb3a5c7235393ffb57
1 /*
2 * Copyright (c) 2007 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
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.
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>
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>
51 static void test_master_id(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);
57 #define hwarnx(format, args...) warnx("WARNING: "format,## args)
59 #define MOPT_HAMMEROPTS \
60 { "history", 1, HMNT_NOHISTORY, 1 }, \
61 { "master=", 0, HMNT_MASTERID, 1 }, \
62 { "mirror", 1, HMNT_NOMIRROR, 1 }
64 static struct mntopt mopts[] = { MOPT_STDOPTS, MOPT_HAMMEROPTS,
65 MOPT_UPDATE, MOPT_NULL };
67 int
68 main(int ac, char **av)
70 struct hammer_mount_info info;
71 struct vfsconf vfc;
72 int mount_flags = 0;
73 int init_flags = 0;
74 int error;
75 int ch;
76 char *mountpt;
77 char *ptr;
79 bzero(&info, sizeof(info));
81 while ((ch = getopt(ac, av, "o:T:u")) != -1) {
82 switch(ch) {
83 case 'T':
84 info.asof = strtoull(optarg, NULL, 0);
85 break;
86 case 'o':
87 getmntopts(optarg, mopts, &mount_flags, &info.hflags);
90 * Handle extended flags with parameters.
92 if (info.hflags & HMNT_MASTERID) {
93 ptr = strstr(optarg, "master=");
94 if (ptr) {
95 info.master_id = strtol(ptr + 7, NULL, 0);
96 test_master_id(info.master_id);
99 if (info.hflags & HMNT_NOMIRROR) {
100 ptr = strstr(optarg, "nomirror");
101 if (ptr)
102 info.master_id = -1;
104 break;
105 case 'u':
106 init_flags |= MNT_UPDATE;
107 break;
108 default:
109 usage();
110 /* not reached */
113 ac -= optind;
114 av += optind;
115 mount_flags |= init_flags;
118 * Only the mount point need be specified in update mode.
120 if (init_flags & MNT_UPDATE) {
121 if (ac != 1) {
122 usage();
123 /* not reached */
125 mountpt = av[0];
126 if (mount(vfc.vfc_name, mountpt, mount_flags, &info)) {
127 err(1, "mountpoint %s", mountpt);
128 /* not reached */
130 exit(0);
133 if (ac < 2) {
134 usage();
135 /* not reached */
139 * Mount arguments: vol [vol...] mountpt
141 extract_volumes(&info, av, ac - 1);
142 mountpt = av[ac - 1];
145 * Load the hammer module if necessary (this bit stolen from
146 * mount_null).
148 error = getvfsbyname("hammer", &vfc);
149 if (error && vfsisloadable("hammer")) {
150 if (vfsload("hammer") != 0) {
151 err(1, "vfsload(hammer)");
152 /* not reached */
154 endvfsent();
155 error = getvfsbyname("hammer", &vfc);
157 if (error) {
158 errx(1, "hammer filesystem is not available");
159 /* not reached */
162 if (mount(vfc.vfc_name, mountpt, mount_flags, &info)) {
163 perror("mount");
164 test_volumes(&info);
165 exit(1);
167 free_volumes(&info);
169 return(0);
172 static void test_master_id(int master_id)
174 switch (master_id) {
175 case 0:
176 hwarnx("A master id of 0 is the default, "
177 "explicit settings should use 1-15");
178 break;
179 case -1:
180 hwarnx("A master id of -1 is nomirror mode, "
181 "equivalent to -o nomirror option");
182 break;
183 case 1 ... 15: /* gcc */
184 /* Expected values via -o master= option */
185 break;
186 default:
187 /* This will eventually fail in hammer_vfs_mount() */
188 hwarnx("A master id of %d is not supported", master_id);
189 break;
194 * Extract a volume list
196 static
197 void
198 extract_volumes(struct hammer_mount_info *info, char **av, int ac)
200 int idx = 0;
201 int arymax = 32;
202 char **ary = malloc(sizeof(char *) * arymax);
203 char *ptr;
204 char *next;
205 char *orig;
207 while (ac) {
208 if (idx == arymax) {
209 arymax += 32;
210 ary = realloc(ary, sizeof(char *) * arymax);
212 if (strchr(*av, ':') == NULL) {
213 ary[idx++] = strdup(*av);
214 } else {
215 orig = next = strdup(*av);
216 while ((ptr = next) != NULL) {
217 if (idx == arymax) {
218 arymax += 32;
219 ary = realloc(ary, sizeof(char *) *
220 arymax);
222 if ((next = strchr(ptr, ':')) != NULL)
223 *next++ = 0;
224 ary[idx++] = strdup(ptr);
226 free(orig);
228 --ac;
229 ++av;
231 info->nvolumes = idx;
232 info->volumes = ary;
235 static
236 void
237 free_volumes(struct hammer_mount_info *info)
239 int i;
241 for (i = 0; i < info->nvolumes; i++)
242 free(info->volumes[i]);
243 free(info->volumes);
247 * This function is based on __verify_volume() in sbin/hammer/ondisk.c.
249 static
250 void
251 __verify_volume(hammer_volume_ondisk_t ondisk,
252 const char *vol_name, int vol_count)
254 if (ondisk->vol_signature != HAMMER_FSBUF_VOLUME) {
255 errx(1, "%s: Invalid volume signature %016jx",
256 vol_name, ondisk->vol_signature);
257 /* not reached */
259 if (ondisk->vol_count != vol_count) {
260 errx(1, "%s: Invalid volume count %d, "
261 "volume header says %d volumes",
262 vol_name, vol_count, ondisk->vol_count);
263 /* not reached */
265 if (ondisk->vol_rootvol != HAMMER_ROOT_VOLNO) {
266 errx(1, "%s: Invalid root volume# %d",
267 vol_name, ondisk->vol_rootvol);
268 /* not reached */
273 * This function prints a possible reason that mount(2) failed,
274 * which isn't really necessary as the real reason is likely to
275 * be in dmesg anyway, but was originally added by 1a607e3e.
277 static
278 void
279 test_volumes(struct hammer_mount_info *info)
281 int i, fd;
282 char buf[2048]; /* sizeof(*ondisk) is 1928 */
283 hammer_volume_ondisk_t ondisk = (hammer_volume_ondisk_t)buf;
285 for (i = 0; i < info->nvolumes; i++) {
286 const char *vol = info->volumes[i];
287 fd = open(vol, O_RDONLY);
288 if (fd < 0) {
289 err(1, "%s: Failed to open", vol);
290 /* not reached */
293 bzero(buf, sizeof(buf));
294 if (pread(fd, buf, sizeof(buf), 0) != sizeof(buf)) {
295 err(1, "%s: Failed to read volume header", vol);
296 /* not reached */
299 __verify_volume(ondisk, vol, info->nvolumes);
300 close(fd);
304 static
305 void
306 usage(void)
308 fprintf(stderr, "usage: mount_hammer [-o options] [-T transaction-id] "
309 "special ... node\n");
310 fprintf(stderr, " mount_hammer [-o options] [-T transaction-id] "
311 "special[:special]* node\n");
312 fprintf(stderr, " mount_hammer -u [-o options] node\n");
313 exit(1);