Merge branch 'vendor/BINUTILS225'
[dragonfly.git] / sbin / mount_hammer / mount_hammer.c
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 extract_volumes(struct hammer_mount_info *info, char **av, int ac);
52 static void free_volumes(struct hammer_mount_info *info);
53 static void test_volumes(struct hammer_mount_info *info);
54 static void usage(void);
55
56 #define MOPT_UPDATE         { "update",     0, MNT_UPDATE, 0 }
57
58 #define MOPT_HAMMEROPTS         \
59         { "history", 1, HMNT_NOHISTORY, 1 },    \
60         { "master=", 0, HMNT_MASTERID, 1 },     \
61         { "mirror", 1, HMNT_NOMIRROR, 1 }
62
63 static struct mntopt mopts[] = { MOPT_STDOPTS, MOPT_HAMMEROPTS,
64                                  MOPT_UPDATE, MOPT_NULL };
65
66 int
67 main(int ac, char **av)
68 {
69         struct hammer_mount_info info;
70         struct vfsconf vfc;
71         int mount_flags = 0;
72         int init_flags = 0;
73         int error;
74         int ch;
75         char *mountpt;
76         char *ptr;
77
78         bzero(&info, sizeof(info));
79
80         while ((ch = getopt(ac, av, "o:T:u")) != -1) {
81                 switch(ch) {
82                 case 'T':
83                         info.asof = strtoull(optarg, NULL, 0);
84                         break;
85                 case 'o':
86                         getmntopts(optarg, mopts, &mount_flags, &info.hflags);
87
88                         /*
89                          * Handle extended flags with parameters.
90                          */
91                         if (info.hflags & HMNT_MASTERID) {
92                                 ptr = strstr(optarg, "master=");
93                                 if (ptr) {
94                                         info.master_id = strtol(ptr + 7, NULL, 0);
95                                         if (info.master_id == 0) {
96                                                 fprintf(stderr,
97         "mount_hammer: Warning: a master id of 0 is the default, explicit\n"
98         "settings should probably use 1-15\n");
99                                         }
100                                 }
101                         }
102                         if (info.hflags & HMNT_NOMIRROR) {
103                                 ptr = strstr(optarg, "nomirror");
104                                 if (ptr)
105                                         info.master_id = -1;
106                         }
107                         break;
108                 case 'u':
109                         init_flags |= MNT_UPDATE;
110                         break;
111                 default:
112                         usage();
113                         /* not reached */
114                 }
115         }
116         ac -= optind;
117         av += optind;
118         mount_flags |= init_flags;
119
120         /*
121          * Only the mount point need be specified in update mode.
122          */
123         if (init_flags & MNT_UPDATE) {
124                 if (ac != 1) {
125                         usage();
126                         /* not reached */
127                 }
128                 mountpt = av[0];
129                 if (mount(vfc.vfc_name, mountpt, mount_flags, &info))
130                         err(1, "mountpoint %s", mountpt);
131                 exit(0);
132         }
133
134         if (ac < 2) {
135                 usage();
136                 /* not reached */
137         }
138
139         /*
140          * Mount arguments: vol [vol...] mountpt
141          */
142         extract_volumes(&info, av, ac - 1);
143         mountpt = av[ac - 1];
144
145         /*
146          * Load the hammer module if necessary (this bit stolen from
147          * mount_null).
148          */
149         error = getvfsbyname("hammer", &vfc);
150         if (error && vfsisloadable("hammer")) {
151                 if (vfsload("hammer") != 0)
152                         err(1, "vfsload(hammer)");
153                 endvfsent();
154                 error = getvfsbyname("hammer", &vfc);
155         }
156         if (error)
157                 errx(1, "hammer filesystem is not available");
158
159         if (mount(vfc.vfc_name, mountpt, mount_flags, &info)) {
160                 perror("mount");
161                 test_volumes(&info);
162                 exit(1);
163         }
164         free_volumes(&info);
165         exit(0);
166 }
167
168 /*
169  * Extract a volume list
170  */
171 static
172 void
173 extract_volumes(struct hammer_mount_info *info, char **av, int ac)
174 {
175         int idx = 0;
176         int arymax = 32;
177         const char **ary = malloc(sizeof(char *) * arymax);
178         char *ptr;
179         char *next;
180         char *orig;
181
182         while (ac) {
183                 if (idx == arymax) {
184                         arymax += 32;
185                         ary = realloc(ary, sizeof(char *) * arymax);
186                 }
187                 if (strchr(*av, ':') == NULL) {
188                         ary[idx++] = strdup(*av);
189                 } else {
190                         orig = next = strdup(*av);
191                         while ((ptr = next) != NULL) {
192                                 if (idx == arymax) {
193                                         arymax += 32;
194                                         ary = realloc(ary, sizeof(char *) *
195                                                       arymax);
196                                 }
197                                 if ((next = strchr(ptr, ':')) != NULL)
198                                         *next++ = 0;
199                                 ary[idx++] = strdup(ptr);
200                         }
201                         free(orig);
202                 }
203                 --ac;
204                 ++av;
205         }
206         info->nvolumes = idx;
207         info->volumes = ary;
208 }
209
210 static
211 void
212 free_volumes(struct hammer_mount_info *info)
213 {
214         int i;
215
216         for (i = 0; i < info->nvolumes; i++)
217                 ; /* free((char*)info->volumes[i]); */
218         free(info->volumes);
219 }
220
221 static
222 void
223 test_volumes(struct hammer_mount_info *info)
224 {
225         int i, fd, ret;
226         const char *vol;
227         char buf[HAMMER_BUFSIZE];
228         hammer_volume_ondisk_t ondisk;
229
230         for (i = 0; i < info->nvolumes; i++) {
231                 vol = info->volumes[i];
232                 fd = open(vol, O_RDONLY);
233                 if (fd < 0) {
234                         fprintf(stderr, "%s: Failed to open\n", vol);
235                         goto next;
236                 }
237
238                 bzero(buf, HAMMER_BUFSIZE);
239                 ret = pread(fd, buf, HAMMER_BUFSIZE, 0);
240                 if (ret != HAMMER_BUFSIZE) {
241                         fprintf(stderr,
242                                 "%s: Failed to read volume header\n", vol);
243                         goto next;
244                 }
245
246                 ondisk = (hammer_volume_ondisk_t)buf;
247                 if (ondisk->vol_signature != HAMMER_FSBUF_VOLUME) {
248                         fprintf(stderr,
249                                 "%s: Not a valid HAMMER filesystem\n", vol);
250                         goto next;
251                 }
252                 if (ondisk->vol_count != info->nvolumes) {
253                         fprintf(stderr,
254                                 "%s: Volume header says %d volumes\n",
255                                 vol, ondisk->vol_count);
256                         goto next;
257                 }
258 next:
259                 close(fd);
260         }
261 }
262
263 static
264 void
265 usage(void)
266 {
267         fprintf(stderr, "usage: mount_hammer [-o options] [-T transaction-id] "
268                         "special ... node\n");
269         fprintf(stderr, "       mount_hammer [-o options] [-T transaction-id] "
270                         "special[:special]* node\n");
271         fprintf(stderr, "       mount_hammer -u [-o options] node\n");
272         exit(1);
273 }