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(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 hwarnx(format, args...)	warnx("WARNING: "format,## args)
58 
59 #define MOPT_HAMMEROPTS		\
60 	{ "history", 1, HMNT_NOHISTORY, 1 },	\
61 	{ "master=", 0, HMNT_MASTERID, 1 },	\
62 	{ "mirror", 1, HMNT_NOMIRROR, 1 }
63 
64 static struct mntopt mopts[] = { MOPT_STDOPTS, MOPT_HAMMEROPTS,
65 				 MOPT_UPDATE, MOPT_NULL };
66 
67 int
68 main(int ac, char **av)
69 {
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;
78 
79 	bzero(&info, sizeof(info));
80 
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);
88 
89 			/*
90 			 * Handle extended flags with parameters.
91 			 */
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);
97 				}
98 			}
99 			if (info.hflags & HMNT_NOMIRROR) {
100 				ptr = strstr(optarg, "nomirror");
101 				if (ptr)
102 					info.master_id = -1;
103 			}
104 			break;
105 		case 'u':
106 			init_flags |= MNT_UPDATE;
107 			break;
108 		default:
109 			usage();
110 			/* not reached */
111 		}
112 	}
113 	ac -= optind;
114 	av += optind;
115 	mount_flags |= init_flags;
116 
117 	/*
118 	 * Only the mount point need be specified in update mode.
119 	 */
120 	if (init_flags & MNT_UPDATE) {
121 		if (ac != 1) {
122 			usage();
123 			/* not reached */
124 		}
125 		mountpt = av[0];
126 		if (mount(vfc.vfc_name, mountpt, mount_flags, &info))
127 			err(1, "mountpoint %s", mountpt);
128 		exit(0);
129 	}
130 
131 	if (ac < 2) {
132 		usage();
133 		/* not reached */
134 	}
135 
136 	/*
137 	 * Mount arguments: vol [vol...] mountpt
138 	 */
139 	extract_volumes(&info, av, ac - 1);
140 	mountpt = av[ac - 1];
141 
142 	/*
143 	 * Load the hammer module if necessary (this bit stolen from
144 	 * mount_null).
145 	 */
146 	error = getvfsbyname("hammer", &vfc);
147 	if (error && vfsisloadable("hammer")) {
148 		if (vfsload("hammer") != 0)
149 			err(1, "vfsload(hammer)");
150 		endvfsent();
151 		error = getvfsbyname("hammer", &vfc);
152 	}
153 	if (error)
154 		errx(1, "hammer filesystem is not available");
155 
156 	if (mount(vfc.vfc_name, mountpt, mount_flags, &info)) {
157 		perror("mount");
158 		test_volumes(&info);
159 		exit(1);
160 	}
161 	free_volumes(&info);
162 
163 	return(0);
164 }
165 
166 static void test_master_id(int master_id)
167 {
168 	switch (master_id) {
169 	case 0:
170 		hwarnx("A master id of 0 is the default, "
171 			"explicit settings should use 1-15");
172 		break;
173 	case -1:
174 		hwarnx("A master id of -1 is nomirror mode, "
175 			"equivalent to -o nomirror option");
176 		break;
177 	case 1 ... 15: /* gcc */
178 		/* Expected values via -o master= option */
179 		break;
180 	default:
181 		/* This will eventually fail in hammer_vfs_mount() */
182 		hwarnx("A master id of %d is not supported", master_id);
183 		break;
184 	}
185 }
186 
187 /*
188  * Extract a volume list
189  */
190 static
191 void
192 extract_volumes(struct hammer_mount_info *info, char **av, int ac)
193 {
194 	int idx = 0;
195 	int arymax = 32;
196 	char **ary = malloc(sizeof(char *) * arymax);
197 	char *ptr;
198 	char *next;
199 	char *orig;
200 
201 	while (ac) {
202 		if (idx == arymax) {
203 			arymax += 32;
204 			ary = realloc(ary, sizeof(char *) * arymax);
205 		}
206 		if (strchr(*av, ':') == NULL) {
207 			ary[idx++] = strdup(*av);
208 		} else {
209 			orig = next = strdup(*av);
210 			while ((ptr = next) != NULL) {
211 				if (idx == arymax) {
212 					arymax += 32;
213 					ary = realloc(ary, sizeof(char *) *
214 						      arymax);
215 				}
216 				if ((next = strchr(ptr, ':')) != NULL)
217 					*next++ = 0;
218 				ary[idx++] = strdup(ptr);
219 			}
220 			free(orig);
221 		}
222 		--ac;
223 		++av;
224 	}
225 	info->nvolumes = idx;
226 	info->volumes = ary;
227 }
228 
229 static
230 void
231 free_volumes(struct hammer_mount_info *info)
232 {
233 	int i;
234 
235 	for (i = 0; i < info->nvolumes; i++)
236 		free(info->volumes[i]);
237 	free(info->volumes);
238 }
239 
240 /*
241  * This function is based on __verify_volume() in sbin/hammer/ondisk.c.
242  */
243 static
244 void
245 __verify_volume(hammer_volume_ondisk_t ondisk,
246 	const char *vol_name, int vol_count)
247 {
248 	if (ondisk->vol_signature != HAMMER_FSBUF_VOLUME) {
249 		errx(1, "%s: Invalid volume signature %016jx",
250 			vol_name, ondisk->vol_signature);
251 	}
252 	if (ondisk->vol_count != vol_count) {
253 		errx(1, "%s: Invalid volume count %d, "
254 			"volume header says %d volumes",
255 			vol_name, vol_count, ondisk->vol_count);
256 	}
257 	if (ondisk->vol_rootvol != HAMMER_ROOT_VOLNO) {
258 		errx(1, "%s: Invalid root volume# %d",
259 			vol_name, ondisk->vol_rootvol);
260 	}
261 }
262 
263 /*
264  * This function prints a possible reason that mount(2) failed,
265  * which isn't really necessary as the real reason is likely to
266  * be in dmesg anyway, but was originally added by 1a607e3e.
267  */
268 static
269 void
270 test_volumes(struct hammer_mount_info *info)
271 {
272 	int i, fd;
273 	char buf[2048]; /* sizeof(*ondisk) is 1928 */
274 	hammer_volume_ondisk_t ondisk = (hammer_volume_ondisk_t)buf;
275 
276 	for (i = 0; i < info->nvolumes; i++) {
277 		const char *vol = info->volumes[i];
278 		fd = open(vol, O_RDONLY);
279 		if (fd < 0)
280 			err(1, "%s: Failed to open", vol);
281 
282 		bzero(buf, sizeof(buf));
283 		if (pread(fd, buf, sizeof(buf), 0) != sizeof(buf))
284 			err(1, "%s: Failed to read volume header", vol);
285 
286 		__verify_volume(ondisk, vol, info->nvolumes);
287 		close(fd);
288 	}
289 }
290 
291 static
292 void
293 usage(void)
294 {
295 	fprintf(stderr, "usage: mount_hammer [-o options] [-T transaction-id] "
296 			"special ... node\n");
297 	fprintf(stderr, "       mount_hammer [-o options] [-T transaction-id] "
298 			"special[:special]* node\n");
299 	fprintf(stderr, "       mount_hammer -u [-o options] node\n");
300 	exit(1);
301 }
302