HAMMER - Verbosity patch for mount_hammer.
[dragonfly.git] / sbin / mount_hammer / mount_hammer.c
CommitLineData
a89ff8c8
MD
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 *
da44aa75 34 * $DragonFly: src/sbin/mount_hammer/mount_hammer.c,v 1.9 2008/07/19 18:48:15 dillon Exp $
a89ff8c8
MD
35 */
36
37#include <sys/types.h>
38#include <sys/diskslice.h>
39#include <sys/diskmbr.h>
40#include <sys/stat.h>
41#include <sys/time.h>
1a607e3e 42#include <sys/syslimits.h>
a89ff8c8 43#include <vfs/hammer/hammer_mount.h>
1a607e3e 44#include <vfs/hammer/hammer_disk.h>
a89ff8c8
MD
45
46#include <stdio.h>
47#include <stdlib.h>
48#include <stdarg.h>
49#include <stddef.h>
50#include <unistd.h>
51#include <string.h>
52#include <errno.h>
53#include <fcntl.h>
54#include <uuid.h>
55#include <err.h>
56#include <assert.h>
195c19a1 57#include <ctype.h>
a89ff8c8
MD
58
59#include "mntopts.h"
60
4adc29ed
MD
61typedef const char **ary_ptr_t;
62
4adc29ed
MD
63static void extract_volumes(ary_ptr_t *aryp, int *countp, char **av, int ac);
64
65#define MOPT_UPDATE { "update", 0, MNT_UPDATE, 0 }
195c19a1
MD
66
67#define MOPT_HAMMEROPTS \
da44aa75
MD
68 { "history", 1, HMNT_NOHISTORY, 1 }, \
69 { "master=", 0, HMNT_MASTERID, 1 }, \
aa209291 70 { "mirror", 1, HMNT_MASTERID, 1 }
195c19a1 71
4adc29ed
MD
72static struct mntopt mopts[] = { MOPT_STDOPTS, MOPT_HAMMEROPTS,
73 MOPT_UPDATE, MOPT_NULL };
a89ff8c8
MD
74
75static void usage(void);
76
77int
78main(int ac, char **av)
79{
80 struct hammer_mount_info info;
81 struct vfsconf vfc;
1a607e3e 82 struct hammer_volume_ondisk *od;
4adc29ed 83 int mount_flags = 0;
a89ff8c8
MD
84 int error;
85 int ch;
4adc29ed 86 int init_flags = 0;
1a607e3e
MD
87 int ax;
88 int fd;
89 int pr;
90 int fdevs_size;
a89ff8c8 91 char *mountpt;
da44aa75 92 char *ptr;
1a607e3e
MD
93 char *fdevs;
94
a89ff8c8 95
195c19a1
MD
96 bzero(&info, sizeof(info));
97 info.asof = 0;
a89ff8c8 98 mount_flags = 0;
195c19a1
MD
99 info.hflags = 0;
100
4adc29ed 101 while ((ch = getopt(ac, av, "o:T:u")) != -1) {
a89ff8c8
MD
102 switch(ch) {
103 case 'T':
b5aaba7f 104 info.asof = strtoull(optarg, NULL, 0);
a89ff8c8
MD
105 break;
106 case 'o':
195c19a1 107 getmntopts(optarg, mopts, &mount_flags, &info.hflags);
da44aa75
MD
108
109
110 /*
111 * Handle extended flags with parameters.
112 */
113 if (info.hflags & HMNT_MASTERID) {
114 ptr = strstr(optarg, "master=");
115 if (ptr) {
116 info.master_id = strtol(ptr + 7, NULL, 0);
117 if (info.master_id == 0) {
118 fprintf(stderr,
119 "hammer_mount: Warning: a master id of 0 is the default, explicit\n"
120 "settings should probably use 1-15\n");
121 }
122 }
123 ptr = strstr(optarg, "nomirror");
124 if (ptr)
125 info.master_id = -1;
126 }
a89ff8c8 127 break;
4adc29ed
MD
128 case 'u':
129 init_flags |= MNT_UPDATE;
130 break;
a89ff8c8
MD
131 default:
132 usage();
133 /* not reached */
134 }
135 }
136 ac -= optind;
137 av += optind;
4adc29ed
MD
138 mount_flags |= init_flags;
139
140 /*
141 * Only the mount point need be specified in update mode.
142 */
143 if (init_flags & MNT_UPDATE) {
144 if (ac != 1) {
145 usage();
146 /* not reached */
147 }
148 mountpt = av[0];
149 if (mount(vfc.vfc_name, mountpt, mount_flags, &info))
1a607e3e 150 err(1, "mountpoint %s", mountpt);
4adc29ed
MD
151 exit(0);
152 }
a89ff8c8
MD
153
154 if (ac < 2) {
155 usage();
156 /* not reached */
157 }
158
159 /*
160 * Mount arguments: vol [vol...] mountpt
161 */
4adc29ed 162 extract_volumes(&info.volumes, &info.nvolumes, av, ac - 1);
a89ff8c8
MD
163 mountpt = av[ac - 1];
164
165 /*
166 * Load the hammer module if necessary (this bit stolen from
167 * mount_null).
168 */
169 error = getvfsbyname("hammer", &vfc);
170 if (error && vfsisloadable("hammer")) {
171 if (vfsload("hammer") != 0)
172 err(1, "vfsload(hammer)");
173 endvfsent();
174 error = getvfsbyname("hammer", &vfc);
175 }
176 if (error)
177 errx(1, "hammer filesystem is not available");
178
1a607e3e
MD
179 if (mount(vfc.vfc_name, mountpt, mount_flags, &info)) {
180 /* Build fdevs in case of error to report failed devices */
181 fdevs_size = ac * PATH_MAX;
182 fdevs = malloc(fdevs_size);
183 for (ax = 0; ax < ac - 1; ax++) {
184 fd = open(info.volumes[ax], O_RDONLY);
185 if (fd < 0 ) {
186 printf ("%s: open failed\n", info.volumes[ax]);
187 strlcat(fdevs, info.volumes[ax], fdevs_size);
188 if (ax < ac - 2)
189 strlcat(fdevs, " ", fdevs_size);
190 continue;
191 }
192
193 od = malloc(HAMMER_BUFSIZE);
194 if (od == NULL) {
195 close (fd);
196 perror("malloc");
197 continue;
198 }
199
200 bzero(od, HAMMER_BUFSIZE);
201 pr = pread(fd, od, HAMMER_BUFSIZE, 0);
202 if (pr != HAMMER_BUFSIZE ||
203 od->vol_signature != HAMMER_FSBUF_VOLUME) {
204 printf("%s: Not a valid HAMMER filesystem\n", info.volumes[ax]);
205 strlcat(fdevs, info.volumes[ax], fdevs_size);
206 if (ax < ac - 2)
207 strlcat(fdevs, " ", fdevs_size);
208 }
209 close(fd);
210 }
211 err(1,"mount %s on %s", fdevs, mountpt);
212 }
213 exit (0);
a89ff8c8
MD
214}
215
4adc29ed
MD
216/*
217 * Extract a volume list
218 */
219static void
220extract_volumes(ary_ptr_t *aryp, int *countp, char **av, int ac)
221{
222 int idx = 0;
223 int arymax = 32;
224 const char **ary = malloc(sizeof(char *) * 32);
225 char *ptr;
226 char *next;
227
228 while (ac) {
229 if (idx == arymax) {
230 arymax += 32;
231 ary = realloc(ary, sizeof(char *) * arymax);
232 }
233 if (strchr(*av, ':') == NULL) {
234 ary[idx++] = *av;
235 } else {
236 next = strdup(*av);
237 while ((ptr = next) != NULL) {
238 if (idx == arymax) {
239 arymax += 32;
240 ary = realloc(ary, sizeof(char *) *
241 arymax);
242 }
243 if ((next = strchr(ptr, ':')) != NULL)
244 *next++ = 0;
245 ary[idx++] = ptr;
246 }
247 }
248 --ac;
249 ++av;
250
251 }
252 *aryp = ary;
253 *countp = idx;
254}
255
a89ff8c8
MD
256static
257void
258usage(void)
259{
28ffd1b3
TN
260 fprintf(stderr, "usage: mount_hammer [-o options] [-T transaction-id] "
261 "special ... node\n");
262 fprintf(stderr, " mount_hammer [-o options] [-T transaction-id] "
263 "special[:special]* node\n");
264 fprintf(stderr, " mount_hammer -u [-o options] node\n");
195c19a1 265 exit(1);
a89ff8c8 266}