nrelease - fix/improve livecd
[dragonfly.git] / sbin / ffsinfo / ffsinfo.c
1 /*
2  * Copyright (c) 2000 Christoph Herrmann, Thomas-Henning von Kamptz
3  * Copyright (c) 1980, 1989, 1993 The Regents of the University of California.
4  * All rights reserved.
5  * 
6  * This code is derived from software contributed to Berkeley by
7  * Christoph Herrmann and Thomas-Henning von Kamptz, Munich and Frankfurt.
8  * 
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
25  * 
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
36  * SUCH DAMAGE.
37  *
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 $
40  *
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 $
43  */
44
45 /* ********************************************************** INCLUDES ***** */
46 #include <sys/param.h>
47 #include <sys/diskslice.h>
48 #include <sys/stat.h>
49
50 #include <stdio.h>
51 #include <paths.h>
52 #include <ctype.h>
53 #include <err.h>
54 #include <fcntl.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58
59 #include "debug.h"
60
61 /* *********************************************************** GLOBALS ***** */
62 #ifdef FS_DEBUG
63 int     _dbg_lvl_ = (DL_INFO); /* DL_TRC */
64 #endif /* FS_DEBUG */
65
66 static union {
67         struct fs       fs;
68         char    pad[SBSIZE];
69 } fsun1, fsun2;
70 #define sblock  fsun1.fs
71 #define osblock fsun2.fs
72
73 static union {
74         struct cg       cg;
75         char    pad[MAXBSIZE];
76 } cgun1;
77 #define acg     cgun1.cg
78
79 static char     ablk[MAXBSIZE];
80 static char     i1blk[MAXBSIZE];
81 static char     i2blk[MAXBSIZE];
82 static char     i3blk[MAXBSIZE];
83
84 static struct csum      *fscs;
85
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);
91
92 /* ************************************************************** rdfs ***** */
93 /*
94  * Here we read some block(s) from disk.
95  */
96 void
97 rdfs(daddr_t bno, size_t size, void *bf, int fsi)
98 {
99         ssize_t n;
100
101         DBG_ENTER;
102
103         if (lseek(fsi, (off_t)bno * DEV_BSIZE, 0) < 0) {
104                 err(33, "rdfs: seek error: %ld", (long)bno);
105         }
106         n = read(fsi, bf, size);
107         if (n != (ssize_t)size) {
108                 err(34, "rdfs: read error: %ld", (long)bno);
109         }
110
111         DBG_LEAVE;
112         return;
113 }
114
115 /* ************************************************************** main ***** */
116 /*
117  * ffsinfo(8) is a tool to dump all metadata of a filesystem. It helps to find
118  * errors is the filesystem much easier. You can run ffsinfo before and  after
119  * an  fsck(8),  and compare the two ascii dumps easy with diff, and  you  see
120  * directly where the problem is. You can control how much detail you want  to
121  * see  with some command line arguments. You can also easy check  the  status
122  * of  a filesystem, like is there is enough space for growing  a  filesystem,
123  * or  how  many active snapshots do we have. It provides much  more  detailed
124  * information  then dumpfs. Snapshots, as they are very new, are  not  really
125  * supported.  They  are just mentioned currently, but it is  planned  to  run
126  * also over active snapshots, to even get that output.
127  */
128 int
129 main(int argc, char **argv)
130 {
131         char    *device, *special;
132         char    ch;
133         size_t  len;
134         struct stat     st;
135         struct partinfo pinfo;
136         int     fsi;
137         struct csum     *dbg_csp;
138         int     dbg_csc;
139         char    dbg_line[80];
140         int     cylno,i;
141         int     cfg_cg, cfg_in, cfg_lv;
142         int     cg_start, cg_stop;
143         ino_t   in;
144         char    *out_file = NULL;
145         int     Lflag=0;
146
147         DBG_ENTER;
148
149         cfg_lv=0xff;
150         cfg_in=-2;
151         cfg_cg=-2;
152
153         while ((ch=getopt(argc, argv, "Lg:i:l:o:")) != -1) {
154                 switch(ch) {
155                 case 'L':
156                         Lflag=1;
157                         break;
158                 case 'g':
159                         cfg_cg=atol(optarg);
160                         if(cfg_cg < -1) {
161                                 usage();
162                         }
163                         break;
164                 case 'i':
165                         cfg_in=atol(optarg);
166                         if(cfg_in < 0) {
167                                 usage();
168                         }
169                         break; 
170                 case 'l':
171                         cfg_lv=atol(optarg);
172                         if(cfg_lv < 0x1||cfg_lv > 0x3ff) {
173                                 usage();
174                         }
175                         break;
176                 case 'o':
177                         if (out_file)
178                                 free(out_file);
179                         out_file = strdup(optarg);
180                         break;
181                 case '?':
182                         /* FALLTHROUGH */
183                 default:
184                         usage();
185                 }
186         }
187         argc -= optind;
188         argv += optind;
189
190         if(argc != 1) {
191                 usage();
192         }
193         device=*argv;
194         
195         /*
196          * Now we try to guess the (raw)device name.
197          */
198         if (0 == strrchr(device, '/') && (stat(device, &st) == -1)) {
199                 /*
200                  * No path prefix was given, so try in that order:
201                  *     /dev/r%s
202                  *     /dev/%s
203                  *     /dev/vinum/r%s
204                  *     /dev/vinum/%s.
205                  * 
206                  * FreeBSD now doesn't distinguish between raw and  block
207                  * devices any longer, but it should still work this way.
208                  */
209                 len=strlen(device)+strlen(_PATH_DEV)+2+strlen("vinum/");
210                 special=(char *)malloc(len);
211                 if(special == NULL) {
212                         errx(1, "malloc failed");
213                 }
214                 snprintf(special, len, "%sr%s", _PATH_DEV, device);
215                 if (stat(special, &st) == -1) {
216                         snprintf(special, len, "%s%s", _PATH_DEV, device);
217                         if (stat(special, &st) == -1) {
218                                 snprintf(special, len, "%svinum/r%s",
219                                     _PATH_DEV, device);
220                                 if (stat(special, &st) == -1) {
221                                         /*
222                                          * For now this is the 'last resort'.
223                                          */
224                                         snprintf(special, len, "%svinum/%s",
225                                             _PATH_DEV, device);
226                                 }
227                         }
228                 }
229                 device = special;
230         }
231
232         /*
233          * Open our device for reading.
234          */
235         fsi = open(device, O_RDONLY);
236         if (fsi < 0) {
237                 err(1, "%s", device);
238         }
239
240         stat(device, &st);
241         
242         if(S_ISREG(st.st_mode)) { /* label check not supported for files */
243                 Lflag=1;
244         }
245
246         if(!Lflag) {
247                 /*
248                  * Try  to read a label and gess the slice if not  specified.
249                  * This code should guess the right thing and avaid to bother
250                  * the user user with the task of specifying the option -v on
251                  * vinum volumes.
252                  */
253                 if (ioctl(fsi, DIOCGPART, &pinfo) < 0) {
254                         pinfo.media_size = st.st_size;
255                         pinfo.media_blksize = DEV_BSIZE;
256                         pinfo.media_blocks = pinfo.media_size / DEV_BSIZE;
257                 }
258         
259                 /*
260                  * Check if that partition looks suited for dumping.
261                  */
262                 if (pinfo.media_size == 0) {
263                         errx(1, "partition is unavailable");
264                 }
265         }
266
267         /*
268          * Read the current superblock.
269          */
270         rdfs((daddr_t)(SBOFF/DEV_BSIZE), (size_t)SBSIZE, &sblock, fsi);
271         if (sblock.fs_magic != FS_MAGIC) {
272                 errx(1, "superblock not recognized");
273         }
274
275         DBG_OPEN(out_file); /* already here we need a superblock */
276
277         if(cfg_lv & 0x001) {
278                 DBG_DUMP_FS(&sblock,
279                     "primary sblock");
280         }
281
282         /*
283          * Determine here what cylinder groups to dump.
284          */
285         if(cfg_cg==-2) {
286                 cg_start=0;
287                 cg_stop=sblock.fs_ncg;
288         } else if (cfg_cg==-1) {
289                 cg_start=sblock.fs_ncg-1;
290                 cg_stop=sblock.fs_ncg;
291         } else if (cfg_cg<sblock.fs_ncg) {
292                 cg_start=cfg_cg;
293                 cg_stop=cfg_cg+1;
294         } else {
295                 cg_start=sblock.fs_ncg;
296                 cg_stop=sblock.fs_ncg;
297         }
298
299         if (cfg_lv & 0x004) {
300                 fscs = (struct csum *)calloc((size_t)1,
301                     (size_t)sblock.fs_cssize);
302                 if(fscs == NULL) {
303                         errx(1, "calloc failed");
304                 }
305
306                 /*
307                  * Get the cylinder summary into the memory ...
308                  */
309                 for (i = 0; i < sblock.fs_cssize; i += sblock.fs_bsize) {
310                         rdfs(fsbtodb(&sblock, sblock.fs_csaddr +
311                             numfrags(&sblock, i)), (size_t)(sblock.fs_cssize-i<
312                             sblock.fs_bsize ? sblock.fs_cssize - i :
313                             sblock.fs_bsize), (void *)(((char *)fscs)+i), fsi);
314                 }
315
316                 dbg_csp=fscs;
317                 /*
318                  * ... and dump it.
319                  */
320                 for(dbg_csc=0; dbg_csc<sblock.fs_ncg; dbg_csc++) {
321                         snprintf(dbg_line, sizeof(dbg_line),
322                             "%d. csum in fscs", dbg_csc);
323                         DBG_DUMP_CSUM(&sblock,
324                             dbg_line,
325                             dbg_csp++);
326                 }
327         }
328
329         /*
330          * For each requested cylinder group ...
331          */
332         for(cylno=cg_start; cylno<cg_stop; cylno++) {
333                 snprintf(dbg_line, sizeof(dbg_line), "cgr %d", cylno);
334                 if(cfg_lv & 0x002) {
335                         /*
336                          * ... dump the superblock copies ...
337                          */
338                         rdfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)),
339                             (size_t)SBSIZE, &osblock, fsi);
340                         DBG_DUMP_FS(&osblock,
341                             dbg_line);
342                 }
343                 /*
344                  * ... read the cylinder group and dump whatever was requested.
345                  */
346                 rdfs(fsbtodb(&sblock, cgtod(&sblock, cylno)),
347                     (size_t)sblock.fs_cgsize, &acg, fsi);
348                 if(cfg_lv & 0x008) {
349                         DBG_DUMP_CG(&sblock,
350                             dbg_line,
351                             &acg);
352                 }
353                 if(cfg_lv & 0x010) {
354                         DBG_DUMP_INMAP(&sblock,
355                             dbg_line,
356                             &acg);
357                 }
358                 if(cfg_lv & 0x020) {
359                         DBG_DUMP_FRMAP(&sblock,
360                             dbg_line,
361                             &acg);
362                 }
363                 if(cfg_lv & 0x040) {
364                         DBG_DUMP_CLMAP(&sblock,
365                             dbg_line,
366                             &acg);
367                         DBG_DUMP_CLSUM(&sblock,
368                             dbg_line,
369                             &acg);
370                 }
371                 if(cfg_lv & 0x080) {
372                         DBG_DUMP_SPTBL(&sblock,
373                             dbg_line,
374                             &acg);
375                 }
376         }
377         /*
378          * Dump the requested inode(s).
379          */
380         if(cfg_in != -2) {
381                 dump_whole_inode((ino_t)cfg_in, fsi, cfg_lv);
382         } else {
383                 for(in=cg_start*sblock.fs_ipg; in<(ino_t)cg_stop*sblock.fs_ipg;
384                     in++) {
385                         dump_whole_inode(in, fsi, cfg_lv);
386                 }
387         }
388
389         DBG_CLOSE;
390
391         close(fsi);
392
393         DBG_LEAVE;
394         return 0;
395 }
396
397 /* ************************************************** dump_whole_inode ***** */
398 /*
399  * Here we dump a list of all blocks allocated by this inode. We follow
400  * all indirect blocks.
401  */
402 void
403 dump_whole_inode(ino_t inode, int fsi, int level)
404 {
405         struct ufs1_dinode      *ino;
406         int     rb;
407         unsigned int    ind2ctr, ind3ctr;
408         ufs_daddr_t     *ind2ptr, *ind3ptr;
409         char    comment[80];
410         
411         DBG_ENTER;
412
413         /*
414          * Read the inode from disk/cache.
415          */
416         ino=ginode(inode, fsi);
417
418         if(ino->di_nlink==0) {
419                 DBG_LEAVE;
420                 return; /* inode not in use */
421         }
422
423         /*
424          * Dump the main inode structure.
425          */
426         snprintf(comment, sizeof(comment), "Inode 0x%08jx", (uintmax_t)inode);
427         if (level & 0x100) {
428                 DBG_DUMP_INO(&sblock,
429                     comment,
430                     ino);
431         }
432
433         if (!(level & 0x200)) {
434                 DBG_LEAVE;
435                 return;
436         }
437
438         /*
439          * Ok, now prepare for dumping all direct and indirect pointers.
440          */
441         rb=howmany(ino->di_size, sblock.fs_bsize)-UFS_NDADDR;
442         if(rb>0) {
443                 /*
444                  * Dump single indirect block.
445                  */
446                 rdfs(fsbtodb(&sblock, ino->di_ib[0]), (size_t)sblock.fs_bsize,
447                     &i1blk, fsi);
448                 snprintf(comment, sizeof(comment), "Inode 0x%08jx: indirect 0",
449                     (uintmax_t)inode);
450                 DBG_DUMP_IBLK(&sblock,
451                     comment,
452                     i1blk,
453                     (size_t)rb);
454                 rb-=howmany(sblock.fs_bsize, sizeof(ufs_daddr_t));
455         }
456         if(rb>0) {
457                 /*
458                  * Dump double indirect blocks.
459                  */
460                 rdfs(fsbtodb(&sblock, ino->di_ib[1]), (size_t)sblock.fs_bsize,
461                     &i2blk, fsi);
462                 snprintf(comment, sizeof(comment), "Inode 0x%08jx: indirect 1",
463                     (uintmax_t)inode);
464                 DBG_DUMP_IBLK(&sblock,
465                     comment,
466                     i2blk,
467                     howmany(rb, howmany(sblock.fs_bsize, sizeof(ufs_daddr_t))));
468                 for(ind2ctr=0; ((ind2ctr < howmany(sblock.fs_bsize,
469                     sizeof(ufs_daddr_t)))&&(rb>0)); ind2ctr++) {
470                         ind2ptr=&((ufs_daddr_t *)(void *)&i2blk)[ind2ctr];
471
472                         rdfs(fsbtodb(&sblock, *ind2ptr),
473                             (size_t)sblock.fs_bsize, &i1blk, fsi);
474                         snprintf(comment, sizeof(comment),
475                             "Inode 0x%08jx: indirect 1->%d", (uintmax_t)inode,
476                             ind2ctr);
477                         DBG_DUMP_IBLK(&sblock,
478                             comment,
479                             i1blk,
480                             (size_t)rb);
481                         rb-=howmany(sblock.fs_bsize, sizeof(ufs_daddr_t));
482                 }
483         }
484         if(rb>0) {
485                 /*
486                  * Dump triple indirect blocks.
487                  */
488                 rdfs(fsbtodb(&sblock, ino->di_ib[2]), (size_t)sblock.fs_bsize,
489                     &i3blk, fsi);
490                 snprintf(comment, sizeof(comment), "Inode 0x%08jx: indirect 2",
491                     (uintmax_t)inode);
492 #define SQUARE(a) ((a)*(a))
493                 DBG_DUMP_IBLK(&sblock,
494                     comment,
495                     i3blk,
496                     howmany(rb,
497                       SQUARE(howmany(sblock.fs_bsize, sizeof(ufs_daddr_t)))));
498 #undef SQUARE
499                 for(ind3ctr=0; ((ind3ctr < howmany(sblock.fs_bsize,
500                     sizeof(ufs_daddr_t)))&&(rb>0)); ind3ctr ++) {
501                         ind3ptr=&((ufs_daddr_t *)(void *)&i3blk)[ind3ctr];
502
503                         rdfs(fsbtodb(&sblock, *ind3ptr),
504                             (size_t)sblock.fs_bsize, &i2blk, fsi);
505                         snprintf(comment, sizeof(comment),
506                             "Inode 0x%08jx: indirect 2->%d", (uintmax_t)inode,
507                             ind3ctr);
508                         DBG_DUMP_IBLK(&sblock,
509                             comment,
510                             i2blk,
511                             howmany(rb,
512                               howmany(sblock.fs_bsize, sizeof(ufs_daddr_t))));
513                         for(ind2ctr=0; ((ind2ctr < howmany(sblock.fs_bsize,
514                             sizeof(ufs_daddr_t)))&&(rb>0)); ind2ctr ++) {
515                                 ind2ptr=&((ufs_daddr_t *)(void *)&i2blk)
516                                     [ind2ctr];
517                                 rdfs(fsbtodb(&sblock, *ind2ptr),
518                                     (size_t)sblock.fs_bsize, &i1blk, fsi);
519                                 snprintf(comment, sizeof(comment),
520                                     "Inode 0x%08jx: indirect 2->%d->%d",
521                                     (uintmax_t)inode, ind3ctr, ind3ctr);
522                                 DBG_DUMP_IBLK(&sblock,
523                                     comment,
524                                     i1blk,
525                                     (size_t)rb);
526                                 rb-=howmany(sblock.fs_bsize,
527                                     sizeof(ufs_daddr_t));
528                         }
529                 }
530         }
531
532         DBG_LEAVE;
533         return;
534 }
535
536 /* ************************************************************* usage ***** */
537 /*
538  * Dump a line of usage.
539  */
540 void
541 usage(void)
542 {
543         DBG_ENTER;
544
545         fprintf(stderr,
546             "usage: ffsinfo [-L] [-g cylgrp] [-i inode] [-l level] "
547             "[-o outfile]\n"
548             "               special | file\n");
549
550         DBG_LEAVE;
551         exit(1);
552 }
553
554 /* ************************************************************ ginode ***** */
555 /*
556  * This function provides access to an individual inode. We find out in which
557  * block  the  requested inode is located, read it from disk if  needed,  and
558  * return  the pointer into that block. We maintain a cache of one  block  to
559  * not  read the same block again and again if we iterate linearly  over  all
560  * inodes.
561  */
562 struct ufs1_dinode *
563 ginode(ino_t inumber, int fsi)
564 {
565         ufs_daddr_t     iblk;
566         static ino_t    startinum=0;    /* first inode in cached block */
567         struct ufs1_dinode      *pi;
568
569         DBG_ENTER;
570
571         pi=(struct ufs1_dinode *)(void *)ablk;
572         if (startinum == 0 || inumber < startinum ||
573             inumber >= startinum + INOPB(&sblock)) {
574                 /*
575                  * The block needed is not cached, so we have to read it from
576                  * disk now.
577                  */
578                 iblk = ino_to_fsba(&sblock, inumber);
579                 rdfs(fsbtodb(&sblock, iblk), (size_t)sblock.fs_bsize,
580                     &ablk, fsi);
581                 startinum = rounddown(inumber, INOPB(&sblock));
582         }
583
584         DBG_LEAVE;
585         return (&(pi[inumber % INOPB(&sblock)]));
586 }
587