xref: /openbsd-src/sbin/scan_ffs/scan_ffs.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*	$OpenBSD: scan_ffs.c,v 1.10 2003/07/02 21:44:58 deraadt Exp $	*/
2 
3 /*
4  * Copyright (c) 1998 Niklas Hallqvist, Tobias Weingartner
5  * All rights reserved.
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  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/fcntl.h>
31 #include <ufs/ffs/fs.h>
32 #include <unistd.h>
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <time.h>
37 #include <err.h>
38 #include <util.h>
39 
40 #define SBCOUNT 64		/* XXX - Should be configurable */
41 
42 /* Flags to control ourselves... */
43 #define FLAG_VERBOSE		1
44 #define FLAG_SMART		2
45 #define FLAG_LABELS		4
46 
47 static int
48 ufsscan(int fd, daddr_t beg, daddr_t end, int flags)
49 {
50 	static char lastmount[MAXMNTLEN];
51 	static u_int8_t buf[SBSIZE * SBCOUNT];
52 	struct fs *sb;
53 	daddr_t blk, lastblk;
54 	int n;
55 
56 	lastblk = -1;
57 	memset(lastmount, 0, MAXMNTLEN);
58 
59 	for (blk = beg; blk <= ((end<0)?blk:end); blk += (SBCOUNT*SBSIZE/512)){
60 		memset(buf, 0, SBSIZE * SBCOUNT);
61 		if (lseek(fd, (off_t)blk * 512, SEEK_SET) < 0)
62 		    err(1, "lseek");
63 		if (read(fd, buf, SBSIZE * SBCOUNT) < 0)
64 			err(1, "read");
65 
66 		for (n = 0; n < (SBSIZE * SBCOUNT); n += 512){
67 			sb = (struct fs*)(&buf[n]);
68 			if (sb->fs_magic == FS_MAGIC) {
69 				if (flags & FLAG_VERBOSE)
70 					printf("block %d id %x,%x size %d\n",
71 					    blk + (n/512), sb->fs_id[0],
72 					    sb->fs_id[1], sb->fs_size);
73 
74 				if (((blk+(n/512)) - lastblk) == (SBSIZE/512)) {
75 					if (flags & FLAG_LABELS ) {
76 						printf("X: %d %d 4.2BSD %d %d %d # %s\n",
77 						    (daddr_t)((off_t)sb->fs_size *
78 						    sb->fs_fsize / 512),
79 						    blk+(n/512)-(2*SBSIZE/512),
80 						    sb->fs_fsize, sb->fs_bsize,
81 						    sb->fs_cpg, lastmount);
82 					} else {
83 						printf("ffs at %d size %lld "
84 						    "mount %s time %s",
85 						    blk+(n/512)-(2*SBSIZE/512),
86 						    (long long)(off_t)sb->fs_size *
87 						    sb->fs_fsize,
88 						    lastmount, ctime(&sb->fs_time));
89 					}
90 
91 					if (flags & FLAG_SMART) {
92 						off_t size = (off_t)sb->fs_size *
93 						    sb->fs_fsize;
94 
95 						if ((n + size) < (SBSIZE * SBCOUNT))
96 							n += size;
97 						else {
98 							blk += (size/512 -
99 							    (SBCOUNT*SBCOUNT));
100 							break;
101 						}
102 					}
103 				}
104 
105 				/* Update last potential FS SBs seen */
106 				lastblk = blk + (n/512);
107 				memcpy(lastmount, sb->fs_fsmnt, MAXMNTLEN);
108 			}
109 		}
110 	}
111 	return(0);
112 }
113 
114 
115 static void
116 usage(int code)
117 {
118 	extern char *__progname;
119 
120 	fprintf(stderr, "usage: %s [-lsv] [-b begin] [-e end] device\n",
121 	    __progname);
122 	exit(code);
123 }
124 
125 
126 int
127 main(int argc, char *argv[])
128 {
129 	int ch, fd, flags = 0;
130 	daddr_t beg = 0, end = -1;
131 
132 	while ((ch = getopt(argc, argv, "lsvb:e:")) != -1)
133 		switch(ch) {
134 		case 'b':
135 			beg = atoi(optarg);
136 			break;
137 		case 'e':
138 			end = atoi(optarg);
139 			break;
140 		case 'v':
141 			flags |= FLAG_VERBOSE;
142 			break;
143 		case 's':
144 			flags |= FLAG_SMART;
145 			break;
146 		case 'l':
147 			flags |= FLAG_LABELS;
148 			break;
149 		default:
150 			usage(1);
151 			/* NOTREACHED */
152 	}
153 	argc -= optind;
154 	argv += optind;
155 
156 	if (argc != 1)
157 		usage(1);
158 
159 	fd = opendev(argv[0], O_RDONLY, OPENDEV_PART, NULL);
160 	if (fd < 0)
161 		err(1, "%s", argv[1]);
162 
163 	return (ufsscan(fd, beg, end, flags));
164 }
165