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 $
39 * $FreeBSD: src/sbin/ffsinfo/ffsinfo.c,v 1.3.2.1 2001/07/16 15:01:56 tomsoft Exp $
41 * @(#) Copyright (c) 2000 Christoph Herrmann, Thomas-Henning von Kamptz Copyright (c) 1980, 1989, 1993 The Regents of the University of California. All rights reserved.
42 * $FreeBSD: src/sbin/ffsinfo/ffsinfo.c,v 1.3.2.1 2001/07/16 15:01:56 tomsoft Exp $
45 /* ********************************************************** INCLUDES ***** */
46 #include <sys/param.h>
47 #include <sys/diskslice.h>
61 /* *********************************************************** GLOBALS ***** */
63 int _dbg_lvl_ = (DL_INFO); /* DL_TRC */
70 #define sblock fsun1.fs
71 #define osblock fsun2.fs
79 static char ablk[MAXBSIZE];
80 static char i1blk[MAXBSIZE];
81 static char i2blk[MAXBSIZE];
82 static char i3blk[MAXBSIZE];
84 static struct csum *fscs;
86 /* ******************************************************** PROTOTYPES ***** */
87 static void rdfs(daddr_t, size_t, void *, int);
88 static void usage(void);
89 static struct ufs1_dinode *ginode(ino_t, int);
90 static void dump_whole_inode(ino_t, int, int);
92 /* ************************************************************** rdfs ***** */
94 * Here we read some block(s) from disk.
97 rdfs(daddr_t bno, size_t size, void *bf, int fsi)
104 if (lseek(fsi, (off_t)bno * DEV_BSIZE, 0) < 0) {
105 err(33, "rdfs: seek error: %ld", (long)bno);
107 n = read(fsi, bf, size);
108 if (n != (ssize_t)size) {
109 err(34, "rdfs: read error: %ld", (long)bno);
116 /* ************************************************************** main ***** */
118 * ffsinfo(8) is a tool to dump all metadata of a filesystem. It helps to find
119 * errors is the filesystem much easier. You can run ffsinfo before and after
120 * an fsck(8), and compare the two ascii dumps easy with diff, and you see
121 * directly where the problem is. You can control how much detail you want to
122 * see with some command line arguments. You can also easy check the status
123 * of a filesystem, like is there is enough space for growing a filesystem,
124 * or how many active snapshots do we have. It provides much more detailed
125 * information then dumpfs. Snapshots, as they are very new, are not really
126 * supported. They are just mentioned currently, but it is planned to run
127 * also over active snapshots, to even get that output.
130 main(int argc, char **argv)
133 char *device, *special, *cp;
137 struct partinfo pinfo;
139 struct csum *dbg_csp;
143 int cfg_cg, cfg_in, cfg_lv;
144 int cg_start, cg_stop;
146 char *out_file = NULL;
155 while ((ch=getopt(argc, argv, "Lg:i:l:o:")) != -1) {
174 if(cfg_lv < 0x1||cfg_lv > 0x3ff) {
181 out_file = strdup(optarg);
198 * Now we try to guess the (raw)device name.
200 if (0 == strrchr(device, '/') && (stat(device, &st) == -1)) {
202 * No path prefix was given, so try in that order:
208 * FreeBSD now doesn't distinguish between raw and block
209 * devices any longer, but it should still work this way.
211 len=strlen(device)+strlen(_PATH_DEV)+2+strlen("vinum/");
212 special=(char *)malloc(len);
213 if(special == NULL) {
214 errx(1, "malloc failed");
216 snprintf(special, len, "%sr%s", _PATH_DEV, device);
217 if (stat(special, &st) == -1) {
218 snprintf(special, len, "%s%s", _PATH_DEV, device);
219 if (stat(special, &st) == -1) {
220 snprintf(special, len, "%svinum/r%s",
222 if (stat(special, &st) == -1) {
224 * For now this is the 'last resort'.
226 snprintf(special, len, "%svinum/%s",
235 * Open our device for reading.
237 fsi = open(device, O_RDONLY);
239 err(1, "%s", device);
244 if(S_ISREG(st.st_mode)) { /* label check not supported for files */
250 * Try to read a label and gess the slice if not specified.
251 * This code should guess the right thing and avaid to bother
252 * the user user with the task of specifying the option -v on
255 cp = device+strlen(device)-1;
256 if (ioctl(fsi, DIOCGPART, &pinfo) < 0) {
257 pinfo.media_size = st.st_size;
258 pinfo.media_blksize = DEV_BSIZE;
259 pinfo.media_blocks = pinfo.media_size / DEV_BSIZE;
263 * Check if that partition looks suited for dumping.
265 if (pinfo.media_size == 0) {
266 errx(1, "partition is unavailable");
271 * Read the current superblock.
273 rdfs((daddr_t)(SBOFF/DEV_BSIZE), (size_t)SBSIZE, &sblock, fsi);
274 if (sblock.fs_magic != FS_MAGIC) {
275 errx(1, "superblock not recognized");
278 DBG_OPEN(out_file); /* already here we need a superblock */
286 * Determine here what cylinder groups to dump.
290 cg_stop=sblock.fs_ncg;
291 } else if (cfg_cg==-1) {
292 cg_start=sblock.fs_ncg-1;
293 cg_stop=sblock.fs_ncg;
294 } else if (cfg_cg<sblock.fs_ncg) {
298 cg_start=sblock.fs_ncg;
299 cg_stop=sblock.fs_ncg;
302 if (cfg_lv & 0x004) {
303 fscs = (struct csum *)calloc((size_t)1,
304 (size_t)sblock.fs_cssize);
306 errx(1, "calloc failed");
310 * Get the cylinder summary into the memory ...
312 for (i = 0; i < sblock.fs_cssize; i += sblock.fs_bsize) {
313 rdfs(fsbtodb(&sblock, sblock.fs_csaddr +
314 numfrags(&sblock, i)), (size_t)(sblock.fs_cssize-i<
315 sblock.fs_bsize ? sblock.fs_cssize - i :
316 sblock.fs_bsize), (void *)(((char *)fscs)+i), fsi);
323 for(dbg_csc=0; dbg_csc<sblock.fs_ncg; dbg_csc++) {
324 snprintf(dbg_line, sizeof(dbg_line),
325 "%d. csum in fscs", dbg_csc);
326 DBG_DUMP_CSUM(&sblock,
333 * For each requested cylinder group ...
335 for(cylno=cg_start; cylno<cg_stop; cylno++) {
336 snprintf(dbg_line, sizeof(dbg_line), "cgr %d", cylno);
339 * ... dump the superblock copies ...
341 rdfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)),
342 (size_t)SBSIZE, &osblock, fsi);
343 DBG_DUMP_FS(&osblock,
347 * ... read the cylinder group and dump whatever was requested.
349 rdfs(fsbtodb(&sblock, cgtod(&sblock, cylno)),
350 (size_t)sblock.fs_cgsize, &acg, fsi);
357 DBG_DUMP_INMAP(&sblock,
362 DBG_DUMP_FRMAP(&sblock,
367 DBG_DUMP_CLMAP(&sblock,
370 DBG_DUMP_CLSUM(&sblock,
375 DBG_DUMP_SPTBL(&sblock,
381 * Dump the requested inode(s).
384 dump_whole_inode((ino_t)cfg_in, fsi, cfg_lv);
386 for(in=cg_start*sblock.fs_ipg; in<(ino_t)cg_stop*sblock.fs_ipg;
388 dump_whole_inode(in, fsi, cfg_lv);
400 /* ************************************************** dump_whole_inode ***** */
402 * Here we dump a list of all blocks allocated by this inode. We follow
403 * all indirect blocks.
406 dump_whole_inode(ino_t inode, int fsi, int level)
408 DBG_FUNC("dump_whole_inode")
409 struct ufs1_dinode *ino;
411 unsigned int ind2ctr, ind3ctr;
412 ufs_daddr_t *ind2ptr, *ind3ptr;
418 * Read the inode from disk/cache.
420 ino=ginode(inode, fsi);
422 if(ino->di_nlink==0) {
424 return; /* inode not in use */
428 * Dump the main inode structure.
430 snprintf(comment, sizeof(comment), "Inode 0x%08jx", (uintmax_t)inode);
432 DBG_DUMP_INO(&sblock,
437 if (!(level & 0x200)) {
443 * Ok, now prepare for dumping all direct and indirect pointers.
445 rb=howmany(ino->di_size, sblock.fs_bsize)-NDADDR;
448 * Dump single indirect block.
450 rdfs(fsbtodb(&sblock, ino->di_ib[0]), (size_t)sblock.fs_bsize,
452 snprintf(comment, sizeof(comment), "Inode 0x%08jx: indirect 0",
454 DBG_DUMP_IBLK(&sblock,
458 rb-=howmany(sblock.fs_bsize, sizeof(ufs_daddr_t));
462 * Dump double indirect blocks.
464 rdfs(fsbtodb(&sblock, ino->di_ib[1]), (size_t)sblock.fs_bsize,
466 snprintf(comment, sizeof(comment), "Inode 0x%08jx: indirect 1",
468 DBG_DUMP_IBLK(&sblock,
471 howmany(rb, howmany(sblock.fs_bsize, sizeof(ufs_daddr_t))));
472 for(ind2ctr=0; ((ind2ctr < howmany(sblock.fs_bsize,
473 sizeof(ufs_daddr_t)))&&(rb>0)); ind2ctr++) {
474 ind2ptr=&((ufs_daddr_t *)(void *)&i2blk)[ind2ctr];
476 rdfs(fsbtodb(&sblock, *ind2ptr),
477 (size_t)sblock.fs_bsize, &i1blk, fsi);
478 snprintf(comment, sizeof(comment),
479 "Inode 0x%08jx: indirect 1->%d", (uintmax_t)inode,
481 DBG_DUMP_IBLK(&sblock,
485 rb-=howmany(sblock.fs_bsize, sizeof(ufs_daddr_t));
490 * Dump triple indirect blocks.
492 rdfs(fsbtodb(&sblock, ino->di_ib[2]), (size_t)sblock.fs_bsize,
494 snprintf(comment, sizeof(comment), "Inode 0x%08jx: indirect 2",
496 #define SQUARE(a) ((a)*(a))
497 DBG_DUMP_IBLK(&sblock,
501 SQUARE(howmany(sblock.fs_bsize, sizeof(ufs_daddr_t)))));
503 for(ind3ctr=0; ((ind3ctr < howmany(sblock.fs_bsize,
504 sizeof(ufs_daddr_t)))&&(rb>0)); ind3ctr ++) {
505 ind3ptr=&((ufs_daddr_t *)(void *)&i3blk)[ind3ctr];
507 rdfs(fsbtodb(&sblock, *ind3ptr),
508 (size_t)sblock.fs_bsize, &i2blk, fsi);
509 snprintf(comment, sizeof(comment),
510 "Inode 0x%08jx: indirect 2->%d", (uintmax_t)inode,
512 DBG_DUMP_IBLK(&sblock,
516 howmany(sblock.fs_bsize, sizeof(ufs_daddr_t))));
517 for(ind2ctr=0; ((ind2ctr < howmany(sblock.fs_bsize,
518 sizeof(ufs_daddr_t)))&&(rb>0)); ind2ctr ++) {
519 ind2ptr=&((ufs_daddr_t *)(void *)&i2blk)
521 rdfs(fsbtodb(&sblock, *ind2ptr),
522 (size_t)sblock.fs_bsize, &i1blk, fsi);
523 snprintf(comment, sizeof(comment),
524 "Inode 0x%08jx: indirect 2->%d->%d",
525 (uintmax_t)inode, ind3ctr, ind3ctr);
526 DBG_DUMP_IBLK(&sblock,
530 rb-=howmany(sblock.fs_bsize,
531 sizeof(ufs_daddr_t));
540 /* ************************************************************* usage ***** */
542 * Dump a line of usage.
552 "usage: ffsinfo [-L] [-g cylgrp] [-i inode] [-l level] "
554 " special | file\n");
560 /* ************************************************************ ginode ***** */
562 * This function provides access to an individual inode. We find out in which
563 * block the requested inode is located, read it from disk if needed, and
564 * return the pointer into that block. We maintain a cache of one block to
565 * not read the same block again and again if we iterate linearly over all
569 ginode(ino_t inumber, int fsi)
573 static ino_t startinum=0; /* first inode in cached block */
574 struct ufs1_dinode *pi;
578 pi=(struct ufs1_dinode *)(void *)ablk;
579 if (startinum == 0 || inumber < startinum ||
580 inumber >= startinum + INOPB(&sblock)) {
582 * The block needed is not cached, so we have to read it from
585 iblk = ino_to_fsba(&sblock, inumber);
586 rdfs(fsbtodb(&sblock, iblk), (size_t)sblock.fs_bsize,
588 startinum = (inumber / INOPB(&sblock)) * INOPB(&sblock);
592 return (&(pi[inumber % INOPB(&sblock)]));