xref: /dragonfly/sbin/hammer2/cmd_info.c (revision 98d44196)
1 /*
2  * Copyright (c) 2015 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@dragonflybsd.org>
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 #include "hammer2.h"
35 
36 static void h2disk_check(const char *devpath,
37 		    void (*callback1)(const char *, hammer2_blockref_t *, int));
38 static void h2pfs_check(int fd, hammer2_blockref_t *bref,
39 		    void (*callback2)(const char *, hammer2_blockref_t *, int));
40 
41 static void info_callback1(const char *, hammer2_blockref_t *, int);
42 static void info_callback2(const char *, hammer2_blockref_t *, int);
43 
44 typedef void (*cmd_callback)(const char *, hammer2_blockref_t *, int);
45 
46 static
47 void
48 h2disk_check_serno(cmd_callback fn)
49 {
50 	DIR *dir;
51 
52 	if ((dir = opendir("/dev/serno")) != NULL) {
53 		struct dirent *den;
54 
55 		while ((den = readdir(dir)) != NULL) {
56 			const char *ptr;
57 			int slice;
58 			char part;
59 			char *devpath;
60 
61 			if (!strcmp(den->d_name, ".") ||
62 			    !strcmp(den->d_name, ".."))
63 				continue;
64 			ptr = strrchr(den->d_name, '.');
65 			if (ptr && sscanf(ptr, ".s%d%c", &slice, &part) == 2) {
66 				asprintf(&devpath, "/dev/serno/%s",
67 					 den->d_name);
68 				h2disk_check(devpath, fn);
69 				free(devpath);
70 			}
71 		}
72 		closedir(dir);
73 	}
74 }
75 
76 static
77 void
78 h2disk_check_dm(cmd_callback fn)
79 {
80 	DIR *dir;
81 
82 	if ((dir = opendir("/dev/mapper")) != NULL) {
83 		struct dirent *den;
84 
85 		while ((den = readdir(dir)) != NULL) {
86 			char *devpath;
87 
88 			if (!strcmp(den->d_name, ".") ||
89 			    !strcmp(den->d_name, "..") ||
90 			    !strcmp(den->d_name, "control"))
91 				continue;
92 			asprintf(&devpath, "/dev/mapper/%s",
93 				 den->d_name);
94 			h2disk_check(devpath, fn);
95 			free(devpath);
96 		}
97 		closedir(dir);
98 	}
99 }
100 
101 static
102 void
103 h2disk_check_misc(cmd_callback fn)
104 {
105 	DIR *dir;
106 
107 	if ((dir = opendir("/dev")) != NULL) {
108 		struct dirent *den;
109 
110 		while ((den = readdir(dir)) != NULL) {
111 			char *devpath;
112 
113 			if (!strcmp(den->d_name, ".") ||
114 			    !strcmp(den->d_name, ".."))
115 				continue;
116 			if (strncmp(den->d_name, "ad", 2) &&
117 			    strncmp(den->d_name, "vn", 2))
118 				continue;
119 			if (!strcmp(den->d_name, "vn"))
120 				continue;
121 			if (!strncmp(den->d_name, "ad", 2) &&
122 			    den->d_namlen <= 3)
123 				continue;
124 			asprintf(&devpath, "/dev/%s", den->d_name);
125 			h2disk_check(devpath, fn);
126 			free(devpath);
127 		}
128 		closedir(dir);
129 	}
130 }
131 
132 int
133 cmd_info(int ac, const char **av)
134 {
135 	int i;
136 
137 	for (i = 0; i < ac; ++i)
138 		h2disk_check(av[i], info_callback1);
139 	if (ac == 0) {
140 		h2disk_check_serno(info_callback1);
141 		h2disk_check_dm(info_callback1);
142 		h2disk_check_misc(info_callback1);
143 	}
144 	return 0;
145 }
146 
147 static
148 void
149 info_callback1(const char *path, hammer2_blockref_t *bref, int fd)
150 {
151 	printf("%s:\n", path);
152 	h2pfs_check(fd, bref, info_callback2);
153 }
154 
155 static
156 void
157 info_callback2(const char *pfsname,
158 	       hammer2_blockref_t *bref __unused, int fd __unused)
159 {
160 	printf("    %s\n", pfsname);
161 }
162 
163 static void mount_callback1(const char *, hammer2_blockref_t *, int);
164 static void mount_callback2(const char *, hammer2_blockref_t *, int);
165 static void cmd_mountall_alarm(int signo);
166 
167 static volatile sig_atomic_t DidAlarm;
168 
169 int
170 cmd_mountall(int ac, const char **av)
171 {
172 	int i;
173 	pid_t pid;
174 
175 	for (i = 0; i < ac; ++i)
176 		h2disk_check(av[i], mount_callback1);
177 	if (ac == 0) {
178 		h2disk_check_serno(mount_callback1);
179 		h2disk_check_dm(mount_callback1);
180 		h2disk_check_misc(mount_callback1);
181 	}
182 	signal(SIGALRM, cmd_mountall_alarm);
183 	for (;;) {
184 		alarm(15);
185 		pid = wait3(NULL, 0, NULL);
186 		if (pid < 0 && errno == ECHILD)
187 			break;
188 		if (pid < 0 && DidAlarm) {
189 			printf("Timeout waiting for mounts to complete\n");
190 			break;
191 		}
192 	}
193 	alarm(0);
194 
195 	return 0;
196 }
197 
198 static
199 void
200 cmd_mountall_alarm(int signo __unused)
201 {
202 	DidAlarm = 1;
203 }
204 
205 static const char *mount_path;
206 static const char *mount_comp;
207 
208 static
209 void
210 mount_callback1(const char *devpath, hammer2_blockref_t *bref, int fd)
211 {
212 	mount_path = devpath;
213 	mount_comp = strrchr(devpath, '/');
214 	if (mount_comp) {
215 		++mount_comp;
216 		h2pfs_check(fd, bref, mount_callback2);
217 	}
218 }
219 
220 static
221 void
222 mount_callback2(const char *pfsname,
223 		hammer2_blockref_t *bref __unused, int fd)
224 {
225 	char *tmp_path;
226 	char *label;
227 	int tfd;
228 
229 	if (strcmp(pfsname, "LOCAL") == 0) {
230 		if ((tfd = open("/dev/null", O_RDONLY)) >= 0) {
231 			dup2(tfd, fd);
232 			close(tfd);
233 		} else {
234 			perror("open(/dev/null)");
235 			exit(1);
236 		}
237 		asprintf(&tmp_path, "/var/hammer2/LOCAL.%s", mount_comp);
238 		asprintf(&label, "%s@LOCAL", mount_path);
239 		mkdir("/var/hammer2", 0700);
240 		mkdir(tmp_path, 0700);
241 		printf("mount %s\n", tmp_path);
242 		if (fork() == 0) {
243 			execl("/sbin/mount_hammer2",
244 			      "mount",
245 			      label,
246 			      tmp_path,
247 			      NULL);
248 		}
249 		free(label);
250 		free(tmp_path);
251 	}
252 }
253 
254 static
255 void
256 h2disk_check(const char *devpath,
257 	     void (*callback1)(const char *, hammer2_blockref_t *, int))
258 {
259 	hammer2_blockref_t broot;
260 	hammer2_blockref_t best;
261 	hammer2_media_data_t media;
262 	struct partinfo partinfo;
263 	int fd;
264 	int i;
265 	int best_i;
266 
267 	fd = open(devpath, O_RDONLY);
268 	if (fd < 0) {
269 		fprintf(stderr, "Unable to open \"%s\"\n", devpath);
270 		return;
271 	}
272 	if (ioctl(fd, DIOCGPART, &partinfo) == -1) {
273 		fprintf(stderr, "DIOCGPART failed on \"%s\"\n", devpath);
274 		goto done;
275 	}
276 
277 	/*
278 	 * Check partition or slice for HAMMER2 designation.  Validate the
279 	 * designation either from the fstype (typically set for disklabel
280 	 * partitions), or the fstype_uuid (typically set for direct-mapped
281 	 * hammer2 GPT slices).
282 	 */
283 	if (partinfo.fstype != FS_HAMMER2) {
284 		uint32_t status;
285 		uuid_t h2uuid;
286 		int is_nil = uuid_is_nil(&partinfo.fstype_uuid, NULL);
287 
288 		uuid_from_string(HAMMER2_UUID_STRING, &h2uuid, &status);
289 		if (!is_nil && (status != uuid_s_ok ||
290 		    uuid_compare(&partinfo.fstype_uuid, &h2uuid, NULL) != 0)) {
291 			goto done;
292 		}
293 	}
294 
295 	/*
296 	 * Find the best volume header.
297 	 */
298 	best_i = -1;
299 	bzero(&best, sizeof(best));
300 	for (i = 0; i < HAMMER2_NUM_VOLHDRS; ++i) {
301 		bzero(&broot, sizeof(broot));
302 		broot.type = HAMMER2_BREF_TYPE_VOLUME;
303 		broot.data_off = (i * HAMMER2_ZONE_BYTES64) |
304 				 HAMMER2_PBUFRADIX;
305 		lseek(fd, broot.data_off & ~HAMMER2_OFF_MASK_RADIX, SEEK_SET);
306 		if (read(fd, &media, HAMMER2_PBUFSIZE) ==
307 		    (ssize_t)HAMMER2_PBUFSIZE &&
308 		    media.voldata.magic == HAMMER2_VOLUME_ID_HBO) {
309 			broot.mirror_tid = media.voldata.mirror_tid;
310 			if (best_i < 0 || best.mirror_tid < broot.mirror_tid) {
311 				best_i = i;
312 				best = broot;
313 			}
314 		}
315 	}
316 	if (best_i >= 0)
317 		callback1(devpath, &best, fd);
318 done:
319 	close(fd);
320 }
321 
322 static
323 void
324 h2pfs_check(int fd, hammer2_blockref_t *bref,
325 	    void (*callback2)(const char *, hammer2_blockref_t *, int))
326 {
327 	hammer2_media_data_t media;
328 	hammer2_blockref_t *bscan;
329 	int bcount;
330 	int i;
331 	size_t bytes;
332 	size_t io_bytes;
333 	size_t boff;
334 	uint32_t cv;
335 	uint64_t cv64;
336 	hammer2_off_t io_off;
337 	hammer2_off_t io_base;
338 
339 	bytes = (size_t)1 << (bref->data_off & HAMMER2_OFF_MASK_RADIX);
340 
341 	io_off = bref->data_off & ~HAMMER2_OFF_MASK_RADIX;
342 	io_base = io_off & ~(hammer2_off_t)(HAMMER2_LBUFSIZE - 1);
343 	io_bytes = bytes;
344 	boff = io_off - io_base;
345 
346 	io_bytes = HAMMER2_LBUFSIZE;
347 	while (io_bytes + boff < bytes)
348 		io_bytes <<= 1;
349 
350 	if (io_bytes > sizeof(media)) {
351 		printf("(bad block size %zu)\n", bytes);
352 		return;
353 	}
354 	if (bref->type != HAMMER2_BREF_TYPE_DATA) {
355 		lseek(fd, io_base, SEEK_SET);
356 		if (read(fd, &media, io_bytes) != (ssize_t)io_bytes) {
357 			printf("(media read failed)\n");
358 			return;
359 		}
360 		if (boff)
361 			bcopy((char *)&media + boff, &media, bytes);
362 	}
363 
364 	bscan = NULL;
365 	bcount = 0;
366 
367 	/*
368 	 * Check data integrity in verbose mode, otherwise we are just doing
369 	 * a quick meta-data scan.  Meta-data integrity is always checked.
370 	 * (Also see the check above that ensures the media data is loaded,
371 	 * otherwise there's no data to check!).
372 	 */
373 	if (bref->type != HAMMER2_BREF_TYPE_DATA || VerboseOpt >= 1) {
374 		switch(HAMMER2_DEC_CHECK(bref->methods)) {
375 		case HAMMER2_CHECK_NONE:
376 			break;
377 		case HAMMER2_CHECK_DISABLED:
378 			break;
379 		case HAMMER2_CHECK_ISCSI32:
380 			cv = hammer2_icrc32(&media, bytes);
381 			if (bref->check.iscsi32.value != cv) {
382 				printf("\t(icrc failed %02x:%08x/%08x)\n",
383 				       bref->methods,
384 				       bref->check.iscsi32.value,
385 				       cv);
386 			}
387 			break;
388 		case HAMMER2_CHECK_XXHASH64:
389 			cv64 = XXH64(&media, bytes, XXH_HAMMER2_SEED);
390 			if (bref->check.xxhash64.value != cv64) {
391 				printf("\t(xxhash failed %02x:%016jx/%016jx)\n",
392 				       bref->methods,
393 				       bref->check.xxhash64.value,
394 				       cv64);
395 			}
396 			break;
397 		case HAMMER2_CHECK_SHA192:
398 			break;
399 		case HAMMER2_CHECK_FREEMAP:
400 			cv = hammer2_icrc32(&media, bytes);
401 			if (bref->check.freemap.icrc32 != cv) {
402 				printf("\t(fcrc %02x:%08x/%08x)\n",
403 					bref->methods,
404 					bref->check.freemap.icrc32,
405 					cv);
406 			}
407 			break;
408 		}
409 	}
410 
411 	switch(bref->type) {
412 	case HAMMER2_BREF_TYPE_INODE:
413 		if (media.ipdata.meta.pfs_type == HAMMER2_PFSTYPE_SUPROOT) {
414 			if ((media.ipdata.meta.op_flags &
415 			     HAMMER2_OPFLAG_DIRECTDATA) == 0) {
416 				bscan = &media.ipdata.u.blockset.blockref[0];
417 				bcount = HAMMER2_SET_COUNT;
418 			}
419 		} else if (media.ipdata.meta.op_flags & HAMMER2_OPFLAG_PFSROOT) {
420 			callback2((char*)media.ipdata.filename, bref, fd);
421 			bscan = NULL;
422 			bcount = 0;
423 		} else {
424 			bscan = NULL;
425 			bcount = 0;
426 		}
427 		break;
428 	case HAMMER2_BREF_TYPE_INDIRECT:
429 		bscan = &media.npdata[0];
430 		bcount = bytes / sizeof(hammer2_blockref_t);
431 		break;
432 	case HAMMER2_BREF_TYPE_VOLUME:
433 		bscan = &media.voldata.sroot_blockset.blockref[0];
434 		bcount = HAMMER2_SET_COUNT;
435 		break;
436 	default:
437 		break;
438 	}
439 	for (i = 0; i < bcount; ++i) {
440 		if (bscan[i].type != HAMMER2_BREF_TYPE_EMPTY)
441 			h2pfs_check(fd, &bscan[i], callback2);
442 	}
443 }
444