2 * Copyright (c) 2000 Christoph Herrmann, Thomas-Henning von Kamptz
3 * Copyright (c) 1980, 1989, 1993 The Regents of the University of California.
6 * This code is derived from software contributed to Berkeley by
7 * Christoph Herrmann and Thomas-Henning von Kamptz, Munich and Frankfurt.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgment:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors, as well as Christoph
21 * Herrmann and Thomas-Henning von Kamptz.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * $TSHeader: src/sbin/ffsinfo/ffsinfo.c,v 1.4 2000/12/12 19:30:55 tomsoft Exp $
43 static const char copyright[] =
44 "@(#) Copyright (c) 2000 Christoph Herrmann, Thomas-Henning von Kamptz\n\
45 Copyright (c) 1980, 1989, 1993 The Regents of the University of California.\n\
46 All rights reserved.\n";
50 static const char rcsid[] =
54 /* ********************************************************** INCLUDES ***** */
55 #include <sys/param.h>
56 #include <sys/disklabel.h>
57 #include <sys/mount.h>
60 #include <ufs/ufs/ufsmount.h>
61 #include <ufs/ufs/dinode.h>
62 #include <ufs/ffs/fs.h>
77 /* *********************************************************** GLOBALS ***** */
79 int _dbg_lvl_ = (DL_INFO); /* DL_TRC */
82 static struct uufsd disk;
84 #define sblock disk.d_fs
92 #define osblock fsun.fs
94 static char i1blk[MAXBSIZE];
95 static char i2blk[MAXBSIZE];
96 static char i3blk[MAXBSIZE];
98 static struct csum *fscs;
100 /* ******************************************************** PROTOTYPES ***** */
101 static void usage(void);
102 static void dump_whole_ufs1_inode(ino_t, int);
103 static void dump_whole_ufs2_inode(ino_t, int);
105 #define DUMP_WHOLE_INODE(A,B) \
107 ? dump_whole_ufs1_inode((A),(B)) : dump_whole_ufs2_inode((A),(B)) )
109 /* ************************************************************** main ***** */
111 * ffsinfo(8) is a tool to dump all metadata of a file system. It helps to find
112 * errors is the file system much easier. You can run ffsinfo before and after
113 * an fsck(8), and compare the two ascii dumps easy with diff, and you see
114 * directly where the problem is. You can control how much detail you want to
115 * see with some command line arguments. You can also easy check the status
116 * of a file system, like is there is enough space for growing a file system,
117 * or how many active snapshots do we have. It provides much more detailed
118 * information then dumpfs. Snapshots, as they are very new, are not really
119 * supported. They are just mentioned currently, but it is planned to run
120 * also over active snapshots, to even get that output.
123 main(int argc, char **argv)
126 char *device, *special;
130 struct csum *dbg_csp;
134 int cfg_cg, cfg_in, cfg_lv;
135 int cg_start, cg_stop;
144 out_file = strdup("-");
146 while ((ch = getopt(argc, argv, "g:i:l:o:")) != -1) {
149 cfg_cg = strtol(optarg, NULL, 0);
150 if (errno == EINVAL || errno == ERANGE)
151 err(1, "%s", optarg);
156 cfg_in = strtol(optarg, NULL, 0);
157 if (errno == EINVAL || errno == ERANGE)
158 err(1, "%s", optarg);
163 cfg_lv = strtol(optarg, NULL, 0);
164 if (errno == EINVAL||errno == ERANGE)
165 err(1, "%s", optarg);
166 if (cfg_lv < 0x1 || cfg_lv > 0x3ff)
171 out_file = strdup(optarg);
172 if (out_file == NULL)
173 errx(1, "strdup failed");
189 * Now we try to guess the (raw)device name.
191 if (0 == strrchr(device, '/') && stat(device, &st) == -1) {
193 * No path prefix was given, so try in this order:
199 * FreeBSD now doesn't distinguish between raw and block
200 * devices any longer, but it should still work this way.
202 len = strlen(device) + strlen(_PATH_DEV) + 2 + strlen("vinum/");
203 special = (char *)malloc(len);
205 errx(1, "malloc failed");
206 snprintf(special, len, "%sr%s", _PATH_DEV, device);
207 if (stat(special, &st) == -1) {
208 snprintf(special, len, "%s%s", _PATH_DEV, device);
209 if (stat(special, &st) == -1) {
210 snprintf(special, len, "%svinum/r%s",
212 if (stat(special, &st) == -1)
213 /* For now this is the 'last resort' */
214 snprintf(special, len, "%svinum/%s",
221 if (ufs_disk_fillout(&disk, device) == -1)
222 err(1, "ufs_disk_fillout(%s) failed: %s", device, disk.d_error);
224 DBG_OPEN(out_file); /* already here we need a superblock */
227 DBG_DUMP_FS(&sblock, "primary sblock");
229 /* Determine here what cylinder groups to dump */
232 cg_stop = sblock.fs_ncg;
233 } else if (cfg_cg == -1) {
234 cg_start = sblock.fs_ncg - 1;
235 cg_stop = sblock.fs_ncg;
236 } else if (cfg_cg < sblock.fs_ncg) {
238 cg_stop = cfg_cg + 1;
240 cg_start = sblock.fs_ncg;
241 cg_stop = sblock.fs_ncg;
244 if (cfg_lv & 0x004) {
245 fscs = (struct csum *)calloc((size_t)1,
246 (size_t)sblock.fs_cssize);
248 errx(1, "calloc failed");
250 /* get the cylinder summary into the memory ... */
251 for (i = 0; i < sblock.fs_cssize; i += sblock.fs_bsize) {
252 if (bread(&disk, fsbtodb(&sblock,
253 sblock.fs_csaddr + numfrags(&sblock, i)),
254 (void *)(((char *)fscs)+i),
255 (size_t)(sblock.fs_cssize-i < sblock.fs_bsize ?
256 sblock.fs_cssize - i : sblock.fs_bsize)) == -1)
257 err(1, "bread: %s", disk.d_error);
261 /* ... and dump it */
262 for(dbg_csc=0; dbg_csc<sblock.fs_ncg; dbg_csc++) {
263 snprintf(dbg_line, sizeof(dbg_line),
264 "%d. csum in fscs", dbg_csc);
265 DBG_DUMP_CSUM(&sblock,
272 /* for each requested cylinder group ... */
273 for (cylno = cg_start; cylno < cg_stop; cylno++) {
274 snprintf(dbg_line, sizeof(dbg_line), "cgr %d", cylno);
275 if (cfg_lv & 0x002) {
276 /* dump the superblock copies */
277 if (bread(&disk, fsbtodb(&sblock,
278 cgsblock(&sblock, cylno)),
279 (void *)&osblock, SBLOCKSIZE) == -1)
280 err(1, "bread: %s", disk.d_error);
281 DBG_DUMP_FS(&osblock, dbg_line);
285 * Read the cylinder group and dump whatever was
288 if (bread(&disk, fsbtodb(&sblock,
289 cgtod(&sblock, cylno)), (void *)&acg,
290 (size_t)sblock.fs_cgsize) == -1)
291 err(1, "bread: %s", disk.d_error);
294 DBG_DUMP_CG(&sblock, dbg_line, &acg);
296 DBG_DUMP_INMAP(&sblock, dbg_line, &acg);
298 DBG_DUMP_FRMAP(&sblock, dbg_line, &acg);
299 if (cfg_lv & 0x040) {
300 DBG_DUMP_CLMAP(&sblock, dbg_line, &acg);
301 DBG_DUMP_CLSUM(&sblock, dbg_line, &acg);
305 * See the comment in sbin/growfs/debug.c for why this
306 * is currently disabled, and what needs to be done to
309 if (disk.d_ufs == 1 && cfg_lv & 0x080)
310 DBG_DUMP_SPTBL(&sblock, dbg_line, &acg);
315 if (cfg_lv & 0x300) {
316 /* Dump the requested inode(s) */
318 DUMP_WHOLE_INODE((ino_t)cfg_in, cfg_lv);
320 for (in = cg_start * sblock.fs_ipg;
321 in < (ino_t)cg_stop * sblock.fs_ipg;
323 DUMP_WHOLE_INODE(in, cfg_lv);
333 /* ********************************************** dump_whole_ufs1_inode ***** */
335 * Here we dump a list of all blocks allocated by this inode. We follow
336 * all indirect blocks.
339 dump_whole_ufs1_inode(ino_t inode, int level)
341 DBG_FUNC("dump_whole_ufs1_inode")
342 struct ufs1_dinode *ino;
344 unsigned int ind2ctr, ind3ctr;
345 ufs1_daddr_t *ind2ptr, *ind3ptr;
351 * Read the inode from disk/cache.
353 if (getino(&disk, (void **)&ino, inode, &mode) == -1)
354 err(1, "getino: %s", disk.d_error);
356 if(ino->di_nlink==0) {
358 return; /* inode not in use */
362 * Dump the main inode structure.
364 snprintf(comment, sizeof(comment), "Inode 0x%08x", inode);
366 DBG_DUMP_INO(&sblock,
371 if (!(level & 0x200)) {
377 * Ok, now prepare for dumping all direct and indirect pointers.
379 rb=howmany(ino->di_size, sblock.fs_bsize)-NDADDR;
382 * Dump single indirect block.
384 if (bread(&disk, fsbtodb(&sblock, ino->di_ib[0]), (void *)&i1blk,
385 (size_t)sblock.fs_bsize) == -1) {
386 err(1, "bread: %s", disk.d_error);
388 snprintf(comment, sizeof(comment), "Inode 0x%08x: indirect 0",
390 DBG_DUMP_IBLK(&sblock,
394 rb-=howmany(sblock.fs_bsize, sizeof(ufs1_daddr_t));
398 * Dump double indirect blocks.
400 if (bread(&disk, fsbtodb(&sblock, ino->di_ib[1]), (void *)&i2blk,
401 (size_t)sblock.fs_bsize) == -1) {
402 err(1, "bread: %s", disk.d_error);
404 snprintf(comment, sizeof(comment), "Inode 0x%08x: indirect 1",
406 DBG_DUMP_IBLK(&sblock,
409 howmany(rb, howmany(sblock.fs_bsize, sizeof(ufs1_daddr_t))));
410 for(ind2ctr=0; ((ind2ctr < howmany(sblock.fs_bsize,
411 sizeof(ufs1_daddr_t))) && (rb>0)); ind2ctr++) {
412 ind2ptr=&((ufs1_daddr_t *)(void *)&i2blk)[ind2ctr];
414 if (bread(&disk, fsbtodb(&sblock, *ind2ptr), (void *)&i1blk,
415 (size_t)sblock.fs_bsize) == -1) {
416 err(1, "bread: %s", disk.d_error);
418 snprintf(comment, sizeof(comment),
419 "Inode 0x%08x: indirect 1->%d", inode, ind2ctr);
420 DBG_DUMP_IBLK(&sblock,
424 rb-=howmany(sblock.fs_bsize, sizeof(ufs1_daddr_t));
429 * Dump triple indirect blocks.
431 if (bread(&disk, fsbtodb(&sblock, ino->di_ib[2]), (void *)&i3blk,
432 (size_t)sblock.fs_bsize) == -1) {
433 err(1, "bread: %s", disk.d_error);
435 snprintf(comment, sizeof(comment), "Inode 0x%08x: indirect 2",
437 #define SQUARE(a) ((a)*(a))
438 DBG_DUMP_IBLK(&sblock,
442 SQUARE(howmany(sblock.fs_bsize, sizeof(ufs1_daddr_t)))));
444 for(ind3ctr=0; ((ind3ctr<howmany(sblock.fs_bsize,
445 sizeof(ufs1_daddr_t)))&&(rb>0)); ind3ctr++) {
446 ind3ptr=&((ufs1_daddr_t *)(void *)&i3blk)[ind3ctr];
448 if (bread(&disk, fsbtodb(&sblock, *ind3ptr), (void *)&i2blk,
449 (size_t)sblock.fs_bsize) == -1) {
450 err(1, "bread: %s", disk.d_error);
452 snprintf(comment, sizeof(comment),
453 "Inode 0x%08x: indirect 2->%d", inode, ind3ctr);
454 DBG_DUMP_IBLK(&sblock,
458 howmany(sblock.fs_bsize, sizeof(ufs1_daddr_t))));
459 for(ind2ctr=0; ((ind2ctr < howmany(sblock.fs_bsize,
460 sizeof(ufs1_daddr_t)))&&(rb>0)); ind2ctr++) {
461 ind2ptr=&((ufs1_daddr_t *)(void *)&i2blk)
463 if (bread(&disk, fsbtodb(&sblock, *ind2ptr),
464 (void *)&i1blk, (size_t)sblock.fs_bsize)
466 err(1, "bread: %s", disk.d_error);
468 snprintf(comment, sizeof(comment),
469 "Inode 0x%08x: indirect 2->%d->%d", inode,
471 DBG_DUMP_IBLK(&sblock,
475 rb-=howmany(sblock.fs_bsize,
476 sizeof(ufs1_daddr_t));
485 /* ********************************************** dump_whole_ufs2_inode ***** */
487 * Here we dump a list of all blocks allocated by this inode. We follow
488 * all indirect blocks.
491 dump_whole_ufs2_inode(ino_t inode, int level)
493 DBG_FUNC("dump_whole_ufs2_inode")
494 struct ufs2_dinode *ino;
496 unsigned int ind2ctr, ind3ctr;
497 ufs2_daddr_t *ind2ptr, *ind3ptr;
503 * Read the inode from disk/cache.
505 if (getino(&disk, (void **)&ino, inode, &mode) == -1)
506 err(1, "getino: %s", disk.d_error);
508 if (ino->di_nlink == 0) {
510 return; /* inode not in use */
514 * Dump the main inode structure.
516 snprintf(comment, sizeof(comment), "Inode 0x%08x", inode);
518 DBG_DUMP_INO(&sblock, comment, ino);
521 if (!(level & 0x200)) {
527 * Ok, now prepare for dumping all direct and indirect pointers.
529 rb = howmany(ino->di_size, sblock.fs_bsize) - NDADDR;
532 * Dump single indirect block.
534 if (bread(&disk, fsbtodb(&sblock, ino->di_ib[0]), (void *)&i1blk,
535 (size_t)sblock.fs_bsize) == -1) {
536 err(1, "bread: %s", disk.d_error);
538 snprintf(comment, sizeof(comment), "Inode 0x%08x: indirect 0", inode);
539 DBG_DUMP_IBLK(&sblock, comment, i1blk, (size_t)rb);
540 rb -= howmany(sblock.fs_bsize, sizeof(ufs2_daddr_t));
544 * Dump double indirect blocks.
546 if (bread(&disk, fsbtodb(&sblock, ino->di_ib[1]), (void *)&i2blk,
547 (size_t)sblock.fs_bsize) == -1) {
548 err(1, "bread: %s", disk.d_error);
550 snprintf(comment, sizeof(comment), "Inode 0x%08x: indirect 1", inode);
551 DBG_DUMP_IBLK(&sblock,
554 howmany(rb, howmany(sblock.fs_bsize, sizeof(ufs2_daddr_t))));
555 for (ind2ctr = 0; ((ind2ctr < howmany(sblock.fs_bsize,
556 sizeof(ufs2_daddr_t))) && (rb>0)); ind2ctr++) {
557 ind2ptr = &((ufs2_daddr_t *)(void *)&i2blk)[ind2ctr];
559 if (bread(&disk, fsbtodb(&sblock, *ind2ptr), (void *)&i1blk,
560 (size_t)sblock.fs_bsize) == -1) {
561 err(1, "bread: %s", disk.d_error);
563 snprintf(comment, sizeof(comment),
564 "Inode 0x%08x: indirect 1->%d", inode, ind2ctr);
565 DBG_DUMP_IBLK(&sblock, comment, i1blk, (size_t)rb);
566 rb -= howmany(sblock.fs_bsize, sizeof(ufs2_daddr_t));
571 * Dump triple indirect blocks.
573 if (bread(&disk, fsbtodb(&sblock, ino->di_ib[2]), (void *)&i3blk,
574 (size_t)sblock.fs_bsize) == -1) {
575 err(1, "bread: %s", disk.d_error);
577 snprintf(comment, sizeof(comment), "Inode 0x%08x: indirect 2", inode);
578 #define SQUARE(a) ((a)*(a))
579 DBG_DUMP_IBLK(&sblock,
583 SQUARE(howmany(sblock.fs_bsize, sizeof(ufs2_daddr_t)))));
585 for (ind3ctr = 0; ((ind3ctr < howmany(sblock.fs_bsize,
586 sizeof(ufs2_daddr_t))) && (rb > 0)); ind3ctr++) {
587 ind3ptr = &((ufs2_daddr_t *)(void *)&i3blk)[ind3ctr];
589 if (bread(&disk, fsbtodb(&sblock, *ind3ptr), (void *)&i2blk,
590 (size_t)sblock.fs_bsize) == -1) {
591 err(1, "bread: %s", disk.d_error);
593 snprintf(comment, sizeof(comment),
594 "Inode 0x%08x: indirect 2->%d", inode, ind3ctr);
595 DBG_DUMP_IBLK(&sblock,
599 howmany(sblock.fs_bsize, sizeof(ufs2_daddr_t))));
600 for (ind2ctr = 0; ((ind2ctr < howmany(sblock.fs_bsize,
601 sizeof(ufs2_daddr_t))) && (rb > 0)); ind2ctr++) {
602 ind2ptr = &((ufs2_daddr_t *)(void *)&i2blk) [ind2ctr];
603 if (bread(&disk, fsbtodb(&sblock, *ind2ptr), (void *)&i1blk,
604 (size_t)sblock.fs_bsize) == -1) {
605 err(1, "bread: %s", disk.d_error);
607 snprintf(comment, sizeof(comment),
608 "Inode 0x%08x: indirect 2->%d->%d", inode,
610 DBG_DUMP_IBLK(&sblock, comment, i1blk, (size_t)rb);
611 rb -= howmany(sblock.fs_bsize, sizeof(ufs2_daddr_t));
620 /* ************************************************************* usage ***** */
622 * Dump a line of usage.
632 "usage: ffsinfo [-g cylinder_group] [-i inode] [-l level] "
634 " special | file\n");