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