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