scan_ffs.c revision 1.16 1 /* $NetBSD: scan_ffs.c,v 1.16 2007/02/16 01:32:21 xtraeme Exp $ */
2
3 /*
4 * Copyright (c) 2005, 2006, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Juan Romero Pardines.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Juan Romero Pardines
21 * for the NetBSD Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright (c) 1998 Niklas Hallqvist, Tobias Weingartner
41 * All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
53 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
54 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
55 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
56 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
57 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
61 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 */
63
64 /*
65 * Currently it can detect FFS and LFS partitions (version 1 or 2)
66 * up to 8192/65536 fragsize/blocksize.
67 */
68
69 #include <sys/cdefs.h>
70 #ifndef lint
71 __RCSID("$NetBSD: scan_ffs.c,v 1.16 2007/02/16 01:32:21 xtraeme Exp $");
72 #endif /* not lint */
73
74 #include <sys/types.h>
75 #include <sys/param.h>
76 #include <sys/disklabel.h>
77 #include <sys/dkio.h>
78 #include <sys/ioctl.h>
79 #include <sys/fcntl.h>
80 #include <sys/mount.h>
81
82 #include <ufs/ufs/dinode.h>
83 #include <ufs/lfs/lfs.h>
84
85 /* Undefine macros defined by both lfs/lfs.h and ffs/fs.h */
86 #undef fsbtodb
87 #undef dbtofsb
88 #undef blkoff
89 #undef fragoff
90 #undef lblktosize
91 #undef lblkno
92 #undef numfrags
93 #undef blkroundup
94 #undef fragroundup
95 #undef fragstoblks
96 #undef blkstofrags
97 #undef fragnum
98 #undef blknum
99 #undef blksize
100 #undef INOPB
101 #undef INOPF
102 #undef NINDIR
103
104 #include <ufs/ffs/fs.h>
105
106 /* Undefine macros defined by both lfs/lfs.h and ffs/fs.h */
107 /* ...to make sure we don't later depend on their (ambigious) definition */
108 #undef fsbtodb
109 #undef dbtofsb
110 #undef blkoff
111 #undef fragoff
112 #undef lblktosize
113 #undef lblkno
114 #undef numfrags
115 #undef blkroundup
116 #undef fragroundup
117 #undef fragstoblks
118 #undef blkstofrags
119 #undef fragnum
120 #undef blknum
121 #undef blksize
122 #undef INOPB
123 #undef INOPF
124 #undef NINDIR
125
126 #include <unistd.h>
127 #include <stdlib.h>
128 #include <stdio.h>
129 #include <string.h>
130 #include <err.h>
131 #include <util.h>
132
133 #define BLK_CNT (blk + (n / 512))
134
135 /* common struct for FFS/LFS */
136 struct sblockinfo {
137 struct lfs *lfs;
138 struct fs *ffs;
139 uint64_t lfs_off;
140 uint64_t ffs_off;
141 char lfs_path[MAXMNTLEN];
142 char ffs_path[MAXMNTLEN];
143 };
144
145 static daddr_t blk, lastblk;
146
147 static int eflag = 0;
148 static int fflag = 0;
149 static int flags = 0;
150 static int sbaddr = 0; /* counter for the LFS superblocks */
151
152 static char device[MAXPATHLEN];
153 static const char *fstypes[] = { "NONE", "FFSv1", "FFSv2" };
154
155 #define FSTYPE_NONE 0
156 #define FSTYPE_FFSV1 1
157 #define FSTYPE_FFSV2 2
158
159 #define SBCOUNT 128 /* may be changed */
160 #define SBPASS (SBCOUNT * SBLOCKSIZE / 512)
161
162 /* This is only useful for LFS */
163
164 /* first sblock address contains the correct offset */
165 #define FIRST_SBLOCK_ADDRESS 1
166 /* second and third sblock address contain lfs_fsmnt[MAXMNTLEN] */
167 #define SECOND_SBLOCK_ADDRESS 2
168 /* last sblock address in a LFS partition */
169 #define MAX_SBLOCK_ADDRESS 10
170
171 enum { NADA, VERBOSE, LABELS };
172
173 /* FFS functions */
174 static void ffs_printpart(struct sblockinfo *, int, size_t, int);
175 static void ffs_scan(struct sblockinfo *, int);
176 static int ffs_checkver(struct sblockinfo *);
177 /* LFS functions */
178 static void lfs_printpart(struct sblockinfo *, int, int);
179 static void lfs_scan(struct sblockinfo *, int);
180 /* common functions */
181 static void usage(void) __attribute__((__noreturn__));
182 static int scan_disk(int, daddr_t, daddr_t, int);
183
184 static int
185 ffs_checkver(struct sblockinfo *sbi)
186 {
187 switch (sbi->ffs->fs_magic) {
188 case FS_UFS1_MAGIC:
189 case FS_UFS1_MAGIC_SWAPPED:
190 sbi->ffs->fs_size = sbi->ffs->fs_old_size;
191 return FSTYPE_FFSV1;
192 case FS_UFS2_MAGIC:
193 case FS_UFS2_MAGIC_SWAPPED:
194 return FSTYPE_FFSV2;
195 default:
196 return FSTYPE_NONE;
197 }
198 }
199
200 static void
201 ffs_printpart(struct sblockinfo *sbi, int flag, size_t ffsize, int n)
202 {
203
204 switch (flag) {
205 case VERBOSE:
206 switch (ffs_checkver(sbi)) {
207 case FSTYPE_FFSV1:
208 (void)printf("offset: %" PRIu64 " n: %d "
209 "id: %x,%x size: %" PRIu64 "\n",
210 BLK_CNT - (2 * SBLOCKSIZE / 512), n,
211 sbi->ffs->fs_id[0], sbi->ffs->fs_id[1],
212 (uint64_t)sbi->ffs->fs_size *
213 sbi->ffs->fs_fsize / 512);
214 break;
215 case FSTYPE_FFSV2:
216 (void)printf("offset: %" PRIu64 " n: %d "
217 "id: %x,%x size: %" PRIu64 "\n",
218 BLK_CNT - (ffsize * SBLOCKSIZE / 512+128),
219 n, sbi->ffs->fs_id[0], sbi->ffs->fs_id[1],
220 (uint64_t)sbi->ffs->fs_size *
221 sbi->ffs->fs_fsize / 512);
222 break;
223 default:
224 break;
225 }
226 break;
227 case LABELS:
228 (void)printf("X: %9" PRIu64,
229 (uint64_t)(sbi->ffs->fs_size *
230 sbi->ffs->fs_fsize / 512));
231 switch (ffs_checkver(sbi)) {
232 case FSTYPE_FFSV1:
233 (void)printf(" %9" PRIu64,
234 BLK_CNT - (ffsize * SBLOCKSIZE / 512));
235 break;
236 case FSTYPE_FFSV2:
237 (void)printf(" %9" PRIu64,
238 BLK_CNT - (ffsize * SBLOCKSIZE / 512 + 128));
239 break;
240 default:
241 break;
242 }
243 (void)printf(" 4.2BSD %6d %5d %7d # %s [%s]\n",
244 sbi->ffs->fs_fsize, sbi->ffs->fs_bsize,
245 sbi->ffs->fs_old_cpg,
246 sbi->ffs_path, fstypes[ffs_checkver(sbi)]);
247 break;
248 default:
249 (void)printf("%s ", fstypes[ffs_checkver(sbi)]);
250 switch (ffs_checkver(sbi)) {
251 case FSTYPE_FFSV1:
252 (void)printf("at %" PRIu64,
253 BLK_CNT - (2 * SBLOCKSIZE / 512));
254 break;
255 case FSTYPE_FFSV2:
256 (void)printf("at %" PRIu64,
257 BLK_CNT - (ffsize * SBLOCKSIZE / 512 + 128));
258 break;
259 default:
260 break;
261 }
262 (void)printf(" size %" PRIu64 ", last mounted on %s\n",
263 (uint64_t)(sbi->ffs->fs_size *
264 sbi->ffs->fs_fsize / 512), sbi->ffs_path);
265 break;
266 }
267 }
268
269 static void
270 ffs_scan(struct sblockinfo *sbi, int n)
271 {
272 size_t i = 0;
273
274 if (flags & VERBOSE)
275 ffs_printpart(sbi, VERBOSE, NADA, n);
276 switch (ffs_checkver(sbi)) {
277 case FSTYPE_FFSV1:
278 /* fsize/bsize > 512/4096 and < 4096/32768. */
279 if ((BLK_CNT - lastblk) == (SBLOCKSIZE / 512)) {
280 i = 2;
281 /* fsize/bsize 4096/32768. */
282 } else if ((BLK_CNT - lastblk) == (SBLOCKSIZE / 170)) {
283 i = 4;
284 /* fsize/bsize 8192/65536 */
285 } else if ((BLK_CNT - lastblk) == (SBLOCKSIZE / 73)) {
286 i = 8;
287 } else
288 break;
289
290 if (flags & LABELS)
291 ffs_printpart(sbi, LABELS, i, n);
292 else
293 ffs_printpart(sbi, NADA, i, n);
294
295 break;
296 case FSTYPE_FFSV2:
297 /*
298 * That checks for FFSv2 partitions with fragsize/blocksize:
299 * 512/4096, 1024/8192, 2048/16384, 4096/32768 and 8192/65536.
300 * Really enough for now.
301 */
302 for (i = 1; i < 16; i <<= 1)
303 if ((BLK_CNT - lastblk) == (i * SBLOCKSIZE / 512)) {
304 if (flags & LABELS)
305 ffs_printpart(sbi, LABELS, i, n);
306 else
307 ffs_printpart(sbi, NADA, i, n);
308 }
309 break;
310 }
311 }
312
313 static void
314 lfs_printpart(struct sblockinfo *sbi, int flag, int n)
315 {
316 if (flags & VERBOSE)
317 (void)printf("offset: %" PRIu64 " size %" PRIu32 "\n",
318 sbi->lfs_off, sbi->lfs->lfs_size);
319 switch (flag) {
320 case LABELS:
321 (void)printf("X: %9" PRIu64,
322 (uint64_t)(sbi->lfs->lfs_size *
323 sbi->lfs->lfs_fsize / 512));
324 (void)printf(" %9" PRIu64, sbi->lfs_off);
325 (void)printf(" 4.4LFS %6d %5d %7d # %s [LFSv%d]\n",
326 sbi->lfs->lfs_fsize, sbi->lfs->lfs_bsize,
327 sbi->lfs->lfs_nseg, sbi->lfs_path,
328 sbi->lfs->lfs_version);
329 break;
330 default:
331 (void)printf("LFSv%d ", sbi->lfs->lfs_version);
332 (void)printf("at %" PRIu64, sbi->lfs_off);
333 (void)printf(" size %" PRIu64 ", last mounted on %s\n",
334 (uint64_t)(sbi->lfs->lfs_size *
335 sbi->lfs->lfs_fsize / 512), sbi->lfs_path);
336 break;
337 }
338 }
339
340 static void
341 lfs_scan(struct sblockinfo *sbi, int n)
342 {
343 /* backup offset */
344 lastblk = BLK_CNT - (LFS_SBPAD / 512);
345 /* increment counter */
346 ++sbaddr;
347
348 switch (sbaddr) {
349 /*
350 * first superblock contains the right offset, but lfs_fsmnt is
351 * empty... afortunately the next superblock address has it.
352 */
353 case FIRST_SBLOCK_ADDRESS:
354 /* copy partition offset */
355 if (sbi->lfs_off != lastblk)
356 sbi->lfs_off = BLK_CNT - (LFS_SBPAD / 512);
357 break;
358 case SECOND_SBLOCK_ADDRESS:
359 /* copy the path of last mount */
360 (void)memcpy(sbi->lfs_path, sbi->lfs->lfs_fsmnt, MAXMNTLEN);
361 /* print now that we have the info */
362 if (flags & LABELS)
363 lfs_printpart(sbi, LABELS, n);
364 else
365 lfs_printpart(sbi, NADA, n);
366 /* clear our struct */
367 (void)memset(sbi, 0, sizeof(*sbi));
368 break;
369 case MAX_SBLOCK_ADDRESS:
370 /*
371 * reset the counter, this is the last superblock address,
372 * the next one will be another partition maybe.
373 */
374 sbaddr = 0;
375 break;
376 default:
377 break;
378 }
379 }
380
381 static int
382 scan_disk(int fd, daddr_t beg, daddr_t end, int fflags)
383 {
384 struct sblockinfo sbinfo;
385 uint8_t buf[SBLOCKSIZE * SBCOUNT];
386 int n;
387
388 n = 0;
389 lastblk = -1;
390
391 /* clear our struct before using it */
392 (void)memset(&sbinfo, 0, sizeof(sbinfo));
393
394 if (fflags & LABELS)
395 (void)printf(
396 "# size offset fstype [fsize bsize cpg/sgs]\n");
397
398 for (blk = beg; blk <= end; blk += SBPASS) {
399 if (pread(fd, buf, sizeof(buf), blk * 512) == -1) {
400 if (fflag && fd >= 0)
401 (void)close(fd);
402 err(1, "pread");
403 }
404
405 for (n = 0; n < (SBLOCKSIZE * SBCOUNT); n += 512) {
406 sbinfo.ffs = (struct fs *)&buf[n];
407 sbinfo.lfs = (struct lfs *)&buf[n];
408
409 switch (ffs_checkver(&sbinfo)) {
410 case FSTYPE_FFSV1:
411 case FSTYPE_FFSV2:
412 ffs_scan(&sbinfo, n);
413 lastblk = BLK_CNT;
414 (void)memcpy(sbinfo.ffs_path,
415 sbinfo.ffs->fs_fsmnt, MAXMNTLEN);
416 break;
417 case FSTYPE_NONE:
418 /* maybe LFS? */
419 if (sbinfo.lfs->lfs_magic == LFS_MAGIC)
420 lfs_scan(&sbinfo, n);
421 break;
422 default:
423 break;
424 }
425 }
426 }
427
428 if (fflag && fd >= 0)
429 (void)close(fd);
430
431 return EXIT_SUCCESS;
432 }
433
434
435 static void
436 usage(void)
437 {
438 (void)fprintf(stderr,
439 "Usage: %s [-lv] [-e end] [-F file] [-s start] "
440 "device\n", getprogname());
441 exit(EXIT_FAILURE);
442 }
443
444
445 int
446 main(int argc, char **argv)
447 {
448 int ch, fd;
449 const char *fpath;
450 daddr_t end = -1, beg = 0;
451 struct disklabel dl;
452
453 fpath = NULL;
454
455 setprogname(*argv);
456 while ((ch = getopt(argc, argv, "e:F:ls:v")) != -1)
457 switch(ch) {
458 case 'e':
459 eflag = 1;
460 end = atoi(optarg);
461 break;
462 case 'F':
463 fflag = 1;
464 fpath = optarg;
465 break;
466 case 'l':
467 flags |= LABELS;
468 break;
469 case 's':
470 beg = atoi(optarg);
471 break;
472 case 'v':
473 flags |= VERBOSE;
474 break;
475 default:
476 usage();
477 /* NOTREACHED */
478 }
479
480 argc -= optind;
481 argv += optind;
482
483 if (fflag) {
484 struct stat stp;
485
486 if (stat(fpath, &stp))
487 err(1, "Cannot stat `%s'", fpath);
488
489 if (!eflag)
490 end = (uint64_t)stp.st_size;
491
492 (void)printf("Total file size: %" PRIu64 "\n\n",
493 (uint64_t)stp.st_size);
494
495 fd = open(fpath, O_RDONLY | O_DIRECT);
496 } else {
497 if (argc != 1)
498 usage();
499
500 fd = opendisk(argv[0], O_RDONLY, device, sizeof(device), 0);
501
502 if (ioctl(fd, DIOCGDINFO, &dl) == -1) {
503 warn("Couldn't retrieve disklabel");
504 (void)memset(&dl, 0, sizeof(dl));
505 dl.d_secperunit = 0x7fffffff;
506 } else {
507 (void)printf("Disk: %s\n", dl.d_typename);
508 (void)printf("Total sectors on disk: %" PRIu32 "\n\n",
509 dl.d_secperunit);
510 }
511 }
512
513 if (!eflag && !fflag)
514 end = dl.d_secperunit; /* default to max sectors */
515
516 if (fd == -1)
517 err(1, "Cannot open `%s'", device);
518 /* NOTREACHED */
519
520 return scan_disk(fd, beg, end, flags);
521 }
522