2000-12-09 15:27:35 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2000 Christoph Herrmann, Thomas-Henning von Kamptz
|
|
|
|
* Copyright (c) 1980, 1989, 1993 The Regents of the University of California.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Christoph Herrmann and Thomas-Henning von Kamptz, Munich and Frankfurt.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgment:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors, as well as Christoph
|
|
|
|
* Herrmann and Thomas-Henning von Kamptz.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
2000-12-12 20:03:17 +00:00
|
|
|
* $TSHeader: src/sbin/ffsinfo/ffsinfo.c,v 1.4 2000/12/12 19:30:55 tomsoft Exp $
|
2000-12-09 15:27:35 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
|
|
|
static const char copyright[] =
|
|
|
|
"@(#) Copyright (c) 2000 Christoph Herrmann, Thomas-Henning von Kamptz\n\
|
|
|
|
Copyright (c) 1980, 1989, 1993 The Regents of the University of California.\n\
|
|
|
|
All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
|
|
|
static const char rcsid[] =
|
|
|
|
"$FreeBSD$";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
/* ********************************************************** INCLUDES ***** */
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/disklabel.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <paths.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
/* *********************************************************** GLOBALS ***** */
|
|
|
|
#ifdef FS_DEBUG
|
|
|
|
int _dbg_lvl_ = (DL_INFO); /* DL_TRC */
|
|
|
|
#endif /* FS_DEBUG */
|
|
|
|
|
|
|
|
static union {
|
|
|
|
struct fs fs;
|
|
|
|
char pad[SBSIZE];
|
|
|
|
} fsun1, fsun2;
|
|
|
|
#define sblock fsun1.fs
|
|
|
|
#define osblock fsun2.fs
|
|
|
|
|
|
|
|
static union {
|
|
|
|
struct cg cg;
|
|
|
|
char pad[MAXBSIZE];
|
|
|
|
} cgun1;
|
|
|
|
#define acg cgun1.cg
|
|
|
|
|
|
|
|
static char ablk[MAXBSIZE];
|
|
|
|
static char i1blk[MAXBSIZE];
|
|
|
|
static char i2blk[MAXBSIZE];
|
|
|
|
static char i3blk[MAXBSIZE];
|
|
|
|
|
|
|
|
static struct csum *fscs;
|
|
|
|
|
|
|
|
/* ******************************************************** PROTOTYPES ***** */
|
2001-06-07 20:43:00 +00:00
|
|
|
static void rdfs(daddr_t, size_t, void *, int);
|
2000-12-12 20:03:17 +00:00
|
|
|
static void usage(void);
|
2000-12-09 15:27:35 +00:00
|
|
|
static struct disklabel *get_disklabel(int);
|
|
|
|
static struct dinode *ginode(ino_t, int);
|
|
|
|
static void dump_whole_inode(ino_t, int, int);
|
|
|
|
|
|
|
|
/* ************************************************************** rdfs ***** */
|
|
|
|
/*
|
|
|
|
* Here we read some block(s) from disk.
|
|
|
|
*/
|
|
|
|
void
|
2001-06-07 20:43:00 +00:00
|
|
|
rdfs(daddr_t bno, size_t size, void *bf, int fsi)
|
2000-12-09 15:27:35 +00:00
|
|
|
{
|
|
|
|
DBG_FUNC("rdfs")
|
2001-06-07 20:43:00 +00:00
|
|
|
ssize_t n;
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
DBG_ENTER;
|
|
|
|
|
|
|
|
if (lseek(fsi, (off_t)bno * DEV_BSIZE, 0) < 0) {
|
2000-12-12 20:03:17 +00:00
|
|
|
err(33, "rdfs: seek error: %ld", (long)bno);
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
2001-06-07 20:43:00 +00:00
|
|
|
n = read(fsi, bf, size);
|
|
|
|
if (n != (ssize_t)size) {
|
2000-12-12 20:03:17 +00:00
|
|
|
err(34, "rdfs: read error: %ld", (long)bno);
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DBG_LEAVE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ************************************************************** main ***** */
|
|
|
|
/*
|
2002-08-21 18:11:48 +00:00
|
|
|
* ffsinfo(8) is a tool to dump all metadata of a file system. It helps to find
|
|
|
|
* errors is the file system much easier. You can run ffsinfo before and after
|
2000-12-09 15:27:35 +00:00
|
|
|
* an fsck(8), and compare the two ascii dumps easy with diff, and you see
|
|
|
|
* directly where the problem is. You can control how much detail you want to
|
|
|
|
* see with some command line arguments. You can also easy check the status
|
2002-08-21 18:11:48 +00:00
|
|
|
* of a file system, like is there is enough space for growing a file system,
|
2000-12-09 15:27:35 +00:00
|
|
|
* or how many active snapshots do we have. It provides much more detailed
|
|
|
|
* information then dumpfs. Snapshots, as they are very new, are not really
|
|
|
|
* supported. They are just mentioned currently, but it is planned to run
|
|
|
|
* also over active snapshots, to even get that output.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
DBG_FUNC("main")
|
2000-12-12 20:03:17 +00:00
|
|
|
char *device, *special, *cp;
|
2000-12-09 15:27:35 +00:00
|
|
|
char ch;
|
|
|
|
size_t len;
|
|
|
|
struct stat st;
|
|
|
|
struct disklabel *lp;
|
|
|
|
struct partition *pp;
|
|
|
|
int fsi;
|
|
|
|
struct csum *dbg_csp;
|
|
|
|
int dbg_csc;
|
|
|
|
char dbg_line[80];
|
|
|
|
int cylno,i;
|
|
|
|
int cfg_cg, cfg_in, cfg_lv;
|
|
|
|
int cg_start, cg_stop;
|
|
|
|
ino_t in;
|
|
|
|
char *out_file;
|
|
|
|
int Lflag=0;
|
|
|
|
|
|
|
|
DBG_ENTER;
|
|
|
|
|
|
|
|
cfg_lv=0xff;
|
|
|
|
cfg_in=-2;
|
|
|
|
cfg_cg=-2;
|
|
|
|
out_file=strdup("/var/tmp/ffsinfo");
|
2000-12-12 20:03:17 +00:00
|
|
|
if(out_file == NULL) {
|
|
|
|
errx(1, "strdup failed");
|
|
|
|
}
|
|
|
|
|
2000-12-09 15:27:35 +00:00
|
|
|
while ((ch=getopt(argc, argv, "Lg:i:l:o:")) != -1) {
|
|
|
|
switch(ch) {
|
|
|
|
case 'L':
|
|
|
|
Lflag=1;
|
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
cfg_cg=atol(optarg);
|
|
|
|
if(cfg_cg < -1) {
|
2000-12-12 20:03:17 +00:00
|
|
|
usage();
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
cfg_in=atol(optarg);
|
|
|
|
if(cfg_in < 0) {
|
2000-12-12 20:03:17 +00:00
|
|
|
usage();
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
cfg_lv=atol(optarg);
|
|
|
|
if(cfg_lv < 0x1||cfg_lv > 0x3ff) {
|
2000-12-12 20:03:17 +00:00
|
|
|
usage();
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
free(out_file);
|
|
|
|
out_file=strdup(optarg);
|
2000-12-12 20:03:17 +00:00
|
|
|
if(out_file == NULL) {
|
|
|
|
errx(1, "strdup failed");
|
|
|
|
}
|
2000-12-09 15:27:35 +00:00
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
default:
|
2000-12-12 20:03:17 +00:00
|
|
|
usage();
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if(argc != 1) {
|
2000-12-12 20:03:17 +00:00
|
|
|
usage();
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
device=*argv;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now we try to guess the (raw)device name.
|
|
|
|
*/
|
|
|
|
if (0 == strrchr(device, '/') && (stat(device, &st) == -1)) {
|
|
|
|
/*
|
|
|
|
* No path prefix was given, so try in that order:
|
|
|
|
* /dev/r%s
|
|
|
|
* /dev/%s
|
|
|
|
* /dev/vinum/r%s
|
|
|
|
* /dev/vinum/%s.
|
|
|
|
*
|
|
|
|
* FreeBSD now doesn't distinguish between raw and block
|
|
|
|
* devices any longer, but it should still work this way.
|
|
|
|
*/
|
|
|
|
len=strlen(device)+strlen(_PATH_DEV)+2+strlen("vinum/");
|
|
|
|
special=(char *)malloc(len);
|
2000-12-12 20:03:17 +00:00
|
|
|
if(special == NULL) {
|
|
|
|
errx(1, "malloc failed");
|
|
|
|
}
|
2000-12-09 15:27:35 +00:00
|
|
|
snprintf(special, len, "%sr%s", _PATH_DEV, device);
|
|
|
|
if (stat(special, &st) == -1) {
|
|
|
|
snprintf(special, len, "%s%s", _PATH_DEV, device);
|
|
|
|
if (stat(special, &st) == -1) {
|
|
|
|
snprintf(special, len, "%svinum/r%s",
|
|
|
|
_PATH_DEV, device);
|
|
|
|
if (stat(special, &st) == -1) {
|
|
|
|
/*
|
|
|
|
* For now this is the 'last resort'.
|
|
|
|
*/
|
|
|
|
snprintf(special, len, "%svinum/%s",
|
|
|
|
_PATH_DEV, device);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
device = special;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open our device for reading.
|
|
|
|
*/
|
|
|
|
fsi = open(device, O_RDONLY);
|
|
|
|
if (fsi < 0) {
|
2000-12-12 20:03:17 +00:00
|
|
|
err(1, "%s", device);
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stat(device, &st);
|
|
|
|
|
|
|
|
if(S_ISREG(st.st_mode)) { /* label check not supported for files */
|
|
|
|
Lflag=1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!Lflag) {
|
|
|
|
/*
|
|
|
|
* Try to read a label and gess the slice if not specified.
|
|
|
|
* This code should guess the right thing and avaid to bother
|
|
|
|
* the user user with the task of specifying the option -v on
|
|
|
|
* vinum volumes.
|
|
|
|
*/
|
|
|
|
cp=device+strlen(device)-1;
|
|
|
|
lp = get_disklabel(fsi);
|
|
|
|
if(lp->d_type == DTYPE_VINUM) {
|
|
|
|
pp = &lp->d_partitions[0];
|
|
|
|
} else if (isdigit(*cp)) {
|
|
|
|
pp = &lp->d_partitions[2];
|
|
|
|
} else if (*cp>='a' && *cp<='h') {
|
|
|
|
pp = &lp->d_partitions[*cp - 'a'];
|
|
|
|
} else {
|
2000-12-12 20:03:17 +00:00
|
|
|
errx(1, "unknown device");
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if that partition looks suited for dumping.
|
|
|
|
*/
|
|
|
|
if (pp->p_size < 1) {
|
2000-12-12 20:03:17 +00:00
|
|
|
errx(1, "partition is unavailable");
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
if (pp->p_fstype != FS_BSDFFS) {
|
2000-12-12 20:03:17 +00:00
|
|
|
errx(1, "partition not 4.2BSD");
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read the current superblock.
|
|
|
|
*/
|
2001-06-07 20:43:00 +00:00
|
|
|
rdfs((daddr_t)(SBOFF/DEV_BSIZE), (size_t)SBSIZE, (void *)&sblock, fsi);
|
2000-12-09 15:27:35 +00:00
|
|
|
if (sblock.fs_magic != FS_MAGIC) {
|
2000-12-12 20:03:17 +00:00
|
|
|
errx(1, "superblock not recognized");
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DBG_OPEN(out_file); /* already here we need a superblock */
|
|
|
|
|
|
|
|
if(cfg_lv & 0x001) {
|
2000-12-12 20:03:17 +00:00
|
|
|
DBG_DUMP_FS(&sblock,
|
|
|
|
"primary sblock");
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Determine here what cylinder groups to dump.
|
|
|
|
*/
|
|
|
|
if(cfg_cg==-2) {
|
|
|
|
cg_start=0;
|
|
|
|
cg_stop=sblock.fs_ncg;
|
|
|
|
} else if (cfg_cg==-1) {
|
|
|
|
cg_start=sblock.fs_ncg-1;
|
|
|
|
cg_stop=sblock.fs_ncg;
|
|
|
|
} else if (cfg_cg<sblock.fs_ncg) {
|
|
|
|
cg_start=cfg_cg;
|
|
|
|
cg_stop=cfg_cg+1;
|
|
|
|
} else {
|
|
|
|
cg_start=sblock.fs_ncg;
|
|
|
|
cg_stop=sblock.fs_ncg;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cfg_lv & 0x004) {
|
2001-06-07 20:43:00 +00:00
|
|
|
fscs = (struct csum *)calloc((size_t)1,
|
|
|
|
(size_t)sblock.fs_cssize);
|
2000-12-12 20:03:17 +00:00
|
|
|
if(fscs == NULL) {
|
|
|
|
errx(1, "calloc failed");
|
|
|
|
}
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the cylinder summary into the memory ...
|
|
|
|
*/
|
|
|
|
for (i = 0; i < sblock.fs_cssize; i += sblock.fs_bsize) {
|
|
|
|
rdfs(fsbtodb(&sblock, sblock.fs_csaddr +
|
2001-06-07 20:43:00 +00:00
|
|
|
numfrags(&sblock, i)), (size_t)(sblock.fs_cssize-i<
|
2000-12-09 15:27:35 +00:00
|
|
|
sblock.fs_bsize ? sblock.fs_cssize - i :
|
2001-06-07 20:43:00 +00:00
|
|
|
sblock.fs_bsize), (void *)(((char *)fscs)+i), fsi);
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dbg_csp=fscs;
|
|
|
|
/*
|
|
|
|
* ... and dump it.
|
|
|
|
*/
|
|
|
|
for(dbg_csc=0; dbg_csc<sblock.fs_ncg; dbg_csc++) {
|
2001-06-07 20:43:00 +00:00
|
|
|
snprintf(dbg_line, sizeof(dbg_line),
|
|
|
|
"%d. csum in fscs", dbg_csc);
|
2000-12-12 20:03:17 +00:00
|
|
|
DBG_DUMP_CSUM(&sblock,
|
|
|
|
dbg_line,
|
|
|
|
dbg_csp++);
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For each requested cylinder group ...
|
|
|
|
*/
|
|
|
|
for(cylno=cg_start; cylno<cg_stop; cylno++) {
|
2001-06-07 20:43:00 +00:00
|
|
|
snprintf(dbg_line, sizeof(dbg_line), "cgr %d", cylno);
|
2000-12-09 15:27:35 +00:00
|
|
|
if(cfg_lv & 0x002) {
|
|
|
|
/*
|
|
|
|
* ... dump the superblock copies ...
|
|
|
|
*/
|
|
|
|
rdfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)),
|
2001-06-07 20:43:00 +00:00
|
|
|
(size_t)SBSIZE, (void *)&osblock, fsi);
|
2000-12-12 20:03:17 +00:00
|
|
|
DBG_DUMP_FS(&osblock,
|
|
|
|
dbg_line);
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* ... read the cylinder group and dump whatever was requested.
|
|
|
|
*/
|
2001-06-07 20:43:00 +00:00
|
|
|
rdfs(fsbtodb(&sblock, cgtod(&sblock, cylno)),
|
|
|
|
(size_t)sblock.fs_cgsize, (void *)&acg, fsi);
|
2000-12-09 15:27:35 +00:00
|
|
|
if(cfg_lv & 0x008) {
|
2000-12-12 20:03:17 +00:00
|
|
|
DBG_DUMP_CG(&sblock,
|
|
|
|
dbg_line,
|
|
|
|
&acg);
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
if(cfg_lv & 0x010) {
|
2000-12-12 20:03:17 +00:00
|
|
|
DBG_DUMP_INMAP(&sblock,
|
|
|
|
dbg_line,
|
|
|
|
&acg);
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
if(cfg_lv & 0x020) {
|
2000-12-12 20:03:17 +00:00
|
|
|
DBG_DUMP_FRMAP(&sblock,
|
|
|
|
dbg_line,
|
|
|
|
&acg);
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
if(cfg_lv & 0x040) {
|
2000-12-12 20:03:17 +00:00
|
|
|
DBG_DUMP_CLMAP(&sblock,
|
|
|
|
dbg_line,
|
|
|
|
&acg);
|
|
|
|
DBG_DUMP_CLSUM(&sblock,
|
|
|
|
dbg_line,
|
|
|
|
&acg);
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
if(cfg_lv & 0x080) {
|
2000-12-12 20:03:17 +00:00
|
|
|
DBG_DUMP_SPTBL(&sblock,
|
|
|
|
dbg_line,
|
|
|
|
&acg);
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Dump the requested inode(s).
|
|
|
|
*/
|
|
|
|
if(cfg_in != -2) {
|
|
|
|
dump_whole_inode((ino_t)cfg_in, fsi, cfg_lv);
|
|
|
|
} else {
|
|
|
|
for(in=cg_start*sblock.fs_ipg; in<(ino_t)cg_stop*sblock.fs_ipg;
|
|
|
|
in++) {
|
|
|
|
dump_whole_inode(in, fsi, cfg_lv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DBG_CLOSE;
|
|
|
|
|
|
|
|
close(fsi);
|
|
|
|
|
|
|
|
DBG_LEAVE;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ************************************************** dump_whole_inode ***** */
|
|
|
|
/*
|
|
|
|
* Here we dump a list of all blocks allocated by this inode. We follow
|
|
|
|
* all indirect blocks.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
dump_whole_inode(ino_t inode, int fsi, int level)
|
|
|
|
{
|
|
|
|
DBG_FUNC("dump_whole_inode")
|
|
|
|
struct dinode *ino;
|
|
|
|
int rb;
|
|
|
|
unsigned int ind2ctr, ind3ctr;
|
|
|
|
ufs_daddr_t *ind2ptr, *ind3ptr;
|
|
|
|
char comment[80];
|
|
|
|
|
|
|
|
DBG_ENTER;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read the inode from disk/cache.
|
|
|
|
*/
|
|
|
|
ino=ginode(inode, fsi);
|
|
|
|
|
|
|
|
if(ino->di_nlink==0) {
|
|
|
|
DBG_LEAVE;
|
|
|
|
return; /* inode not in use */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Dump the main inode structure.
|
|
|
|
*/
|
2001-06-07 20:43:00 +00:00
|
|
|
snprintf(comment, sizeof(comment), "Inode 0x%08x", inode);
|
2000-12-09 15:27:35 +00:00
|
|
|
if (level & 0x100) {
|
2000-12-12 20:03:17 +00:00
|
|
|
DBG_DUMP_INO(&sblock,
|
|
|
|
comment,
|
|
|
|
ino);
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!(level & 0x200)) {
|
|
|
|
DBG_LEAVE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ok, now prepare for dumping all direct and indirect pointers.
|
|
|
|
*/
|
|
|
|
rb=howmany(ino->di_size, sblock.fs_bsize)-NDADDR;
|
|
|
|
if(rb>0) {
|
|
|
|
/*
|
|
|
|
* Dump single indirect block.
|
|
|
|
*/
|
2001-06-07 20:43:00 +00:00
|
|
|
rdfs(fsbtodb(&sblock, ino->di_ib[0]), (size_t)sblock.fs_bsize,
|
|
|
|
(void *)&i1blk, fsi);
|
|
|
|
snprintf(comment, sizeof(comment), "Inode 0x%08x: indirect 0",
|
|
|
|
inode);
|
2000-12-12 20:03:17 +00:00
|
|
|
DBG_DUMP_IBLK(&sblock,
|
|
|
|
comment,
|
|
|
|
i1blk,
|
|
|
|
(size_t)rb);
|
2000-12-09 15:27:35 +00:00
|
|
|
rb-=howmany(sblock.fs_bsize, sizeof(ufs_daddr_t));
|
|
|
|
}
|
|
|
|
if(rb>0) {
|
|
|
|
/*
|
|
|
|
* Dump double indirect blocks.
|
|
|
|
*/
|
2001-06-07 20:43:00 +00:00
|
|
|
rdfs(fsbtodb(&sblock, ino->di_ib[1]), (size_t)sblock.fs_bsize,
|
|
|
|
(void *)&i2blk, fsi);
|
|
|
|
snprintf(comment, sizeof(comment), "Inode 0x%08x: indirect 1",
|
|
|
|
inode);
|
2000-12-12 20:03:17 +00:00
|
|
|
DBG_DUMP_IBLK(&sblock,
|
|
|
|
comment,
|
|
|
|
i2blk,
|
|
|
|
howmany(rb, howmany(sblock.fs_bsize, sizeof(ufs_daddr_t))));
|
2000-12-09 15:27:35 +00:00
|
|
|
for(ind2ctr=0; ((ind2ctr < howmany(sblock.fs_bsize,
|
|
|
|
sizeof(ufs_daddr_t)))&&(rb>0)); ind2ctr++) {
|
2001-06-07 20:43:00 +00:00
|
|
|
ind2ptr=&((ufs_daddr_t *)(void *)&i2blk)[ind2ctr];
|
2000-12-09 15:27:35 +00:00
|
|
|
|
2001-06-07 20:43:00 +00:00
|
|
|
rdfs(fsbtodb(&sblock, *ind2ptr),
|
|
|
|
(size_t)sblock.fs_bsize, (void *)&i1blk, fsi);
|
|
|
|
snprintf(comment, sizeof(comment),
|
|
|
|
"Inode 0x%08x: indirect 1->%d", inode, ind2ctr);
|
2000-12-12 20:03:17 +00:00
|
|
|
DBG_DUMP_IBLK(&sblock,
|
|
|
|
comment,
|
|
|
|
i1blk,
|
|
|
|
(size_t)rb);
|
2000-12-09 15:27:35 +00:00
|
|
|
rb-=howmany(sblock.fs_bsize, sizeof(ufs_daddr_t));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(rb>0) {
|
|
|
|
/*
|
|
|
|
* Dump triple indirect blocks.
|
|
|
|
*/
|
2001-06-07 20:43:00 +00:00
|
|
|
rdfs(fsbtodb(&sblock, ino->di_ib[2]), (size_t)sblock.fs_bsize,
|
|
|
|
(void *)&i3blk, fsi);
|
|
|
|
snprintf(comment, sizeof(comment), "Inode 0x%08x: indirect 2",
|
|
|
|
inode);
|
2000-12-09 15:27:35 +00:00
|
|
|
#define SQUARE(a) ((a)*(a))
|
2000-12-12 20:03:17 +00:00
|
|
|
DBG_DUMP_IBLK(&sblock,
|
|
|
|
comment,
|
|
|
|
i3blk,
|
|
|
|
howmany(rb,
|
|
|
|
SQUARE(howmany(sblock.fs_bsize, sizeof(ufs_daddr_t)))));
|
2000-12-09 15:27:35 +00:00
|
|
|
#undef SQUARE
|
|
|
|
for(ind3ctr=0; ((ind3ctr < howmany(sblock.fs_bsize,
|
|
|
|
sizeof(ufs_daddr_t)))&&(rb>0)); ind3ctr ++) {
|
2001-06-07 20:43:00 +00:00
|
|
|
ind3ptr=&((ufs_daddr_t *)(void *)&i3blk)[ind3ctr];
|
2000-12-09 15:27:35 +00:00
|
|
|
|
2001-06-07 20:43:00 +00:00
|
|
|
rdfs(fsbtodb(&sblock, *ind3ptr),
|
|
|
|
(size_t)sblock.fs_bsize, (void *)&i2blk, fsi);
|
|
|
|
snprintf(comment, sizeof(comment),
|
|
|
|
"Inode 0x%08x: indirect 2->%d", inode, ind3ctr);
|
2000-12-12 20:03:17 +00:00
|
|
|
DBG_DUMP_IBLK(&sblock,
|
|
|
|
comment,
|
|
|
|
i2blk,
|
|
|
|
howmany(rb,
|
|
|
|
howmany(sblock.fs_bsize, sizeof(ufs_daddr_t))));
|
2000-12-09 15:27:35 +00:00
|
|
|
for(ind2ctr=0; ((ind2ctr < howmany(sblock.fs_bsize,
|
|
|
|
sizeof(ufs_daddr_t)))&&(rb>0)); ind2ctr ++) {
|
2001-06-07 20:43:00 +00:00
|
|
|
ind2ptr=&((ufs_daddr_t *)(void *)&i2blk)
|
|
|
|
[ind2ctr];
|
2000-12-09 15:27:35 +00:00
|
|
|
rdfs(fsbtodb(&sblock, *ind2ptr),
|
2001-06-07 20:43:00 +00:00
|
|
|
(size_t)sblock.fs_bsize, (void *)&i1blk,
|
|
|
|
fsi);
|
|
|
|
snprintf(comment, sizeof(comment),
|
2000-12-09 15:27:35 +00:00
|
|
|
"Inode 0x%08x: indirect 2->%d->%d", inode,
|
|
|
|
ind3ctr, ind3ctr);
|
2000-12-12 20:03:17 +00:00
|
|
|
DBG_DUMP_IBLK(&sblock,
|
|
|
|
comment,
|
|
|
|
i1blk,
|
|
|
|
(size_t)rb);
|
2000-12-09 15:27:35 +00:00
|
|
|
rb-=howmany(sblock.fs_bsize,
|
|
|
|
sizeof(ufs_daddr_t));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DBG_LEAVE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ***************************************************** get_disklabel ***** */
|
|
|
|
/*
|
|
|
|
* Read the disklabel from disk.
|
|
|
|
*/
|
|
|
|
struct disklabel *
|
|
|
|
get_disklabel(int fd)
|
|
|
|
{
|
|
|
|
DBG_FUNC("get_disklabel")
|
|
|
|
static struct disklabel *lab;
|
|
|
|
|
|
|
|
DBG_ENTER;
|
|
|
|
|
|
|
|
lab=(struct disklabel *)malloc(sizeof(struct disklabel));
|
|
|
|
if (!lab) {
|
2000-12-12 20:03:17 +00:00
|
|
|
errx(1, "malloc failed");
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
if (ioctl(fd, DIOCGDINFO, (char *)lab) < 0) {
|
2000-12-12 20:03:17 +00:00
|
|
|
errx(1, "DIOCGDINFO failed");
|
2000-12-09 15:27:35 +00:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
DBG_LEAVE;
|
|
|
|
return (lab);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ************************************************************* usage ***** */
|
|
|
|
/*
|
|
|
|
* Dump a line of usage.
|
|
|
|
*/
|
|
|
|
void
|
2000-12-12 20:03:17 +00:00
|
|
|
usage(void)
|
2000-12-09 15:27:35 +00:00
|
|
|
{
|
|
|
|
DBG_FUNC("usage")
|
|
|
|
|
|
|
|
DBG_ENTER;
|
|
|
|
|
|
|
|
fprintf(stderr,
|
2000-12-12 20:03:17 +00:00
|
|
|
"usage: ffsinfo [-L] [-g cylgrp] [-i inode] [-l level] "
|
|
|
|
"[-o outfile]\n"
|
|
|
|
" special | file\n");
|
2000-12-09 15:27:35 +00:00
|
|
|
|
|
|
|
DBG_LEAVE;
|
2000-12-12 20:03:17 +00:00
|
|
|
exit(1);
|
2000-12-09 15:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ************************************************************ ginode ***** */
|
|
|
|
/*
|
|
|
|
* This function provides access to an individual inode. We find out in which
|
|
|
|
* block the requested inode is located, read it from disk if needed, and
|
|
|
|
* return the pointer into that block. We maintain a cache of one block to
|
2000-12-12 20:03:17 +00:00
|
|
|
* not read the same block again and again if we iterate linearly over all
|
2000-12-09 15:27:35 +00:00
|
|
|
* inodes.
|
|
|
|
*/
|
|
|
|
struct dinode *
|
|
|
|
ginode(ino_t inumber, int fsi)
|
|
|
|
{
|
|
|
|
DBG_FUNC("ginode")
|
|
|
|
ufs_daddr_t iblk;
|
|
|
|
static ino_t startinum=0; /* first inode in cached block */
|
|
|
|
struct dinode *pi;
|
|
|
|
|
|
|
|
DBG_ENTER;
|
|
|
|
|
2001-06-07 20:43:00 +00:00
|
|
|
pi=(struct dinode *)(void *)ablk;
|
2000-12-09 15:27:35 +00:00
|
|
|
if (startinum == 0 || inumber < startinum ||
|
|
|
|
inumber >= startinum + INOPB(&sblock)) {
|
|
|
|
/*
|
|
|
|
* The block needed is not cached, so we have to read it from
|
|
|
|
* disk now.
|
|
|
|
*/
|
|
|
|
iblk = ino_to_fsba(&sblock, inumber);
|
2001-06-07 20:43:00 +00:00
|
|
|
rdfs(fsbtodb(&sblock, iblk), (size_t)sblock.fs_bsize,
|
|
|
|
(void *)&ablk, fsi);
|
2000-12-09 15:27:35 +00:00
|
|
|
startinum = (inumber / INOPB(&sblock)) * INOPB(&sblock);
|
|
|
|
}
|
|
|
|
|
|
|
|
DBG_LEAVE;
|
|
|
|
return (&(pi[inumber % INOPB(&sblock)]));
|
|
|
|
}
|
|
|
|
|