dumplfs.c revision 1.28 1 /* $NetBSD: dumplfs.c,v 1.28 2003/08/07 11:25:20 agc Exp $ */
2
3 /*-
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33
34 #ifndef lint
35 __COPYRIGHT(
36 "@(#) Copyright (c) 1991, 1993\n\
37 The Regents of the University of California. All rights reserved.\n");
38 #endif /* not lint */
39
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)dumplfs.c 8.5 (Berkeley) 5/24/95";
43 #else
44 __RCSID("$NetBSD: dumplfs.c,v 1.28 2003/08/07 11:25:20 agc Exp $");
45 #endif
46 #endif /* not lint */
47
48 #include <sys/param.h>
49 #include <sys/ucred.h>
50 #include <sys/mount.h>
51 #include <sys/time.h>
52
53 #include <ufs/ufs/dinode.h>
54 #include <ufs/lfs/lfs.h>
55
56 #include <err.h>
57 #include <errno.h>
58 #include <fcntl.h>
59 #include <fstab.h>
60 #include <stdlib.h>
61 #include <stdio.h>
62 #include <string.h>
63 #include <unistd.h>
64 #include "extern.h"
65
66 static void addseg(char *);
67 static void dump_cleaner_info(struct lfs *, void *);
68 static void dump_dinode(struct ufs1_dinode *);
69 static void dump_ifile(int, struct lfs *, int, int, daddr_t);
70 static int dump_ipage_ifile(struct lfs *, int, char *, int);
71 static int dump_ipage_segusage(struct lfs *, int, char *, int);
72 static void dump_segment(int, int, daddr_t, struct lfs *, int);
73 static int dump_sum(int, struct lfs *, SEGSUM *, int, daddr_t);
74 static void dump_super(struct lfs *);
75 static void usage(void);
76
77 extern u_long cksum(void *, size_t);
78
79 typedef struct seglist SEGLIST;
80 struct seglist {
81 SEGLIST *next;
82 int num;
83 };
84 SEGLIST *seglist;
85
86 char *special;
87
88 /* Segment Usage formats */
89 #define print_suheader \
90 (void)printf("segnum\tflags\tnbytes\tninos\tnsums\tlastmod\n")
91
92 #define print_suentry(i, sp, fs) \
93 (void)printf("%d\t%c%c%c\t%d\t%d\t%d\t%s", i, \
94 (((sp)->su_flags & SEGUSE_ACTIVE) ? 'A' : ' '), \
95 (((sp)->su_flags & SEGUSE_DIRTY) ? 'D' : 'C'), \
96 (((sp)->su_flags & SEGUSE_SUPERBLOCK) ? 'S' : ' '), \
97 (sp)->su_nbytes, (sp)->su_ninos, (sp)->su_nsums, \
98 ((fs)->lfs_version == 1 ? ctime((time_t *)&(sp)->su_olastmod) : \
99 ctime((time_t *)&(sp)->su_lastmod)))
100
101 /* Ifile formats */
102 #define print_iheader \
103 (void)printf("inum\tstatus\tversion\tdaddr\t\tfreeptr\n")
104 #define print_ientry(i, ip) \
105 if ((ip)->if_daddr == LFS_UNUSED_DADDR) \
106 (void)printf("%d\tFREE\t%d\t \t\t%d\n", \
107 i, (ip)->if_version, (ip)->if_nextfree); \
108 else \
109 (void)printf("%d\tINUSE\t%d\t%8X \n", \
110 i, (ip)->if_version, (ip)->if_daddr)
111 #define fsbtobyte(fs, b) fsbtob((fs), (off_t)((b)))
112
113 int datasum_check = 0;
114
115 int
116 main(int argc, char **argv)
117 {
118 struct lfs lfs_sb1, lfs_sb2, *lfs_master;
119 daddr_t seg_addr, idaddr, sbdaddr;
120 int ch, do_allsb, do_ientries, do_segentries, fd, segnum;
121
122 do_allsb = 0;
123 do_ientries = 0;
124 do_segentries = 0;
125 idaddr = 0x0;
126 sbdaddr = 0x0;
127 while ((ch = getopt(argc, argv, "ab:diI:Ss:")) != -1)
128 switch(ch) {
129 case 'a': /* Dump all superblocks */
130 do_allsb = 1;
131 break;
132 case 'b': /* Use this superblock */
133 sbdaddr = strtol(optarg, NULL, 0);
134 break;
135 case 'd':
136 datasum_check = 1;
137 break;
138 case 'i': /* Dump ifile entries */
139 do_ientries = !do_ientries;
140 break;
141 case 'I': /* Use this ifile inode */
142 idaddr = strtol(optarg, NULL, 0);
143 break;
144 case 'S':
145 do_segentries = !do_segentries;
146 break;
147 case 's': /* Dump out these segments */
148 addseg(optarg);
149 break;
150 default:
151 usage();
152 }
153 argc -= optind;
154 argv += optind;
155
156 if (argc != 1)
157 usage();
158
159 special = argv[0];
160 if ((fd = open(special, O_RDONLY, 0)) < 0)
161 err(1, "%s", special);
162
163 if (sbdaddr == 0x0) {
164 /* Read the proto-superblock */
165 get(fd, LFS_LABELPAD, &(lfs_sb1.lfs_dlfs), sizeof(struct dlfs));
166
167 /* If that wasn't the real first sb, get the real first sb */
168 if (lfs_sb1.lfs_version > 1 &&
169 lfs_sb1.lfs_sboffs[0] > btofsb(&lfs_sb1, LFS_LABELPAD))
170 get(fd, fsbtob(&lfs_sb1, lfs_sb1.lfs_sboffs[0]),
171 &(lfs_sb1.lfs_dlfs), sizeof(struct dlfs));
172
173 /*
174 * Read the second superblock and figure out which check point is
175 * most up to date.
176 */
177 get(fd,
178 fsbtobyte(&lfs_sb1, lfs_sb1.lfs_sboffs[1]),
179 &(lfs_sb2.lfs_dlfs), sizeof(struct dlfs));
180
181 lfs_master = &lfs_sb1;
182 if (lfs_sb1.lfs_version > 1) {
183 if (lfs_sb1.lfs_serial > lfs_sb2.lfs_serial) {
184 lfs_master = &lfs_sb2;
185 sbdaddr = lfs_sb1.lfs_sboffs[1];
186 } else
187 sbdaddr = lfs_sb1.lfs_sboffs[0];
188 } else {
189 if (lfs_sb1.lfs_otstamp > lfs_sb2.lfs_otstamp) {
190 lfs_master = &lfs_sb2;
191 sbdaddr = lfs_sb1.lfs_sboffs[1];
192 } else
193 sbdaddr = lfs_sb1.lfs_sboffs[0];
194 }
195 } else {
196 /* Read the first superblock */
197 get(fd, dbtob((off_t)sbdaddr), &(lfs_sb1.lfs_dlfs),
198 sizeof(struct dlfs));
199 lfs_master = &lfs_sb1;
200 }
201
202 /* Compatibility */
203 if (lfs_master->lfs_version == 1) {
204 lfs_master->lfs_sumsize = LFS_V1_SUMMARY_SIZE;
205 lfs_master->lfs_ibsize = lfs_master->lfs_bsize;
206 lfs_master->lfs_start = lfs_master->lfs_sboffs[0];
207 lfs_master->lfs_tstamp = lfs_master->lfs_otstamp;
208 lfs_master->lfs_fsbtodb = 0;
209 }
210
211 (void)printf("Master Superblock at 0x%llx:\n", (long long)sbdaddr);
212 dump_super(lfs_master);
213
214 dump_ifile(fd, lfs_master, do_ientries, do_segentries, idaddr);
215
216 if (seglist != NULL)
217 for (; seglist != NULL; seglist = seglist->next) {
218 seg_addr = sntod(lfs_master, seglist->num);
219 dump_segment(fd, seglist->num, seg_addr, lfs_master,
220 do_allsb);
221 }
222 else
223 for (segnum = 0, seg_addr = sntod(lfs_master, 0);
224 segnum < lfs_master->lfs_nseg;
225 segnum++, seg_addr = sntod(lfs_master, segnum))
226 dump_segment(fd, segnum, seg_addr, lfs_master,
227 do_allsb);
228
229 (void)close(fd);
230 exit(0);
231 }
232
233 /*
234 * We are reading all the blocks of an inode and dumping out the ifile table.
235 * This code could be tighter, but this is a first pass at getting the stuff
236 * printed out rather than making this code incredibly efficient.
237 */
238 static void
239 dump_ifile(int fd, struct lfs *lfsp, int do_ientries, int do_segentries, daddr_t addr)
240 {
241 char *ipage;
242 struct ufs1_dinode *dip, *dpage;
243 /* XXX ondisk32 */
244 int32_t *addrp, *dindir, *iaddrp, *indir;
245 int block_limit, i, inum, j, nblocks, psize;
246
247 psize = lfsp->lfs_bsize;
248 if (!addr)
249 addr = lfsp->lfs_idaddr;
250
251 if (!(dpage = malloc(psize)))
252 err(1, "malloc");
253 get(fd, fsbtobyte(lfsp, addr), dpage, psize);
254
255 for (dip = dpage + INOPB(lfsp) - 1; dip >= dpage; --dip)
256 if (dip->di_inumber == LFS_IFILE_INUM)
257 break;
258
259 if (dip < dpage)
260 errx(1, "unable to locate ifile inode at disk address 0x%llx",
261 (long long)addr);
262
263 (void)printf("\nIFILE inode\n");
264 dump_dinode(dip);
265
266 (void)printf("\nIFILE contents\n");
267 nblocks = dip->di_size >> lfsp->lfs_bshift;
268 block_limit = MIN(nblocks, NDADDR);
269
270 /* Get the direct block */
271 if ((ipage = malloc(psize)) == NULL)
272 err(1, "malloc");
273 for (inum = 0, addrp = dip->di_db, i = 0; i < block_limit;
274 i++, addrp++) {
275 get(fd, fsbtobyte(lfsp, *addrp), ipage, psize);
276 if (i < lfsp->lfs_cleansz) {
277 dump_cleaner_info(lfsp, ipage);
278 if (do_segentries)
279 print_suheader;
280 continue;
281 }
282
283 if (i < (lfsp->lfs_segtabsz + lfsp->lfs_cleansz)) {
284 if (do_segentries)
285 inum = dump_ipage_segusage(lfsp, inum, ipage,
286 lfsp->lfs_sepb);
287 else
288 inum = (i < lfsp->lfs_segtabsz + lfsp->lfs_cleansz - 1);
289 if (!inum) {
290 if(!do_ientries)
291 goto e0;
292 else
293 print_iheader;
294 }
295 } else
296 inum = dump_ipage_ifile(lfsp, inum, ipage, lfsp->lfs_ifpb);
297 }
298
299 if (nblocks <= NDADDR)
300 goto e0;
301
302 /* Dump out blocks off of single indirect block */
303 if (!(indir = malloc(psize)))
304 err(1, "malloc");
305 get(fd, fsbtobyte(lfsp, dip->di_ib[0]), indir, psize);
306 block_limit = MIN(i + lfsp->lfs_nindir, nblocks);
307 for (addrp = indir; i < block_limit; i++, addrp++) {
308 if (*addrp == LFS_UNUSED_DADDR)
309 break;
310 get(fd, fsbtobyte(lfsp, *addrp), ipage, psize);
311 if (i < lfsp->lfs_cleansz) {
312 dump_cleaner_info(lfsp, ipage);
313 continue;
314 }
315
316 if (i < lfsp->lfs_segtabsz + lfsp->lfs_cleansz) {
317 if (do_segentries)
318 inum = dump_ipage_segusage(lfsp, inum, ipage,
319 lfsp->lfs_sepb);
320 else
321 inum = (i < lfsp->lfs_segtabsz + lfsp->lfs_cleansz - 1);
322 if (!inum) {
323 if(!do_ientries)
324 goto e1;
325 else
326 print_iheader;
327 }
328 } else
329 inum = dump_ipage_ifile(lfsp, inum, ipage, lfsp->lfs_ifpb);
330 }
331
332 if (nblocks <= lfsp->lfs_nindir * lfsp->lfs_ifpb)
333 goto e1;
334
335 /* Get the double indirect block */
336 if (!(dindir = malloc(psize)))
337 err(1, "malloc");
338 get(fd, fsbtobyte(lfsp, dip->di_ib[1]), dindir, psize);
339 for (iaddrp = dindir, j = 0; j < lfsp->lfs_nindir; j++, iaddrp++) {
340 if (*iaddrp == LFS_UNUSED_DADDR)
341 break;
342 get(fd, fsbtobyte(lfsp, *iaddrp), indir, psize);
343 block_limit = MIN(i + lfsp->lfs_nindir, nblocks);
344 for (addrp = indir; i < block_limit; i++, addrp++) {
345 if (*addrp == LFS_UNUSED_DADDR)
346 break;
347 get(fd, fsbtobyte(lfsp, *addrp), ipage, psize);
348 if (i < lfsp->lfs_cleansz) {
349 dump_cleaner_info(lfsp, ipage);
350 continue;
351 }
352
353 if (i < lfsp->lfs_segtabsz + lfsp->lfs_cleansz) {
354 if (do_segentries)
355 inum = dump_ipage_segusage(lfsp,
356 inum, ipage, lfsp->lfs_sepb);
357 else
358 inum = (i < lfsp->lfs_segtabsz +
359 lfsp->lfs_cleansz - 1);
360 if (!inum) {
361 if(!do_ientries)
362 goto e2;
363 else
364 print_iheader;
365 }
366 } else
367 inum = dump_ipage_ifile(lfsp, inum,
368 ipage, lfsp->lfs_ifpb);
369 }
370 }
371 e2: free(dindir);
372 e1: free(indir);
373 e0: free(dpage);
374 free(ipage);
375 }
376
377 static int
378 dump_ipage_ifile(struct lfs *lfsp, int i, char *pp, int tot)
379 {
380 char *ip;
381 int cnt, max, entsize;
382
383 if (lfsp->lfs_version == 1)
384 entsize = sizeof(IFILE_V1);
385 else
386 entsize = sizeof(IFILE);
387 max = i + tot;
388
389 for (ip = pp, cnt = i; cnt < max; cnt++, ip += entsize)
390 print_ientry(cnt, (IFILE *)ip);
391 return (max);
392 }
393
394 static int
395 dump_ipage_segusage(struct lfs *lfsp, int i, char *pp, int tot)
396 {
397 SEGUSE *sp;
398 int cnt, max;
399 struct seglist *slp;
400
401 max = i + tot;
402 for (sp = (SEGUSE *)pp, cnt = i;
403 cnt < lfsp->lfs_nseg && cnt < max; cnt++) {
404 if (seglist == NULL)
405 print_suentry(cnt, sp, lfsp);
406 else {
407 for (slp = seglist; slp != NULL; slp = slp->next)
408 if (cnt == slp->num) {
409 print_suentry(cnt, sp, lfsp);
410 break;
411 }
412 }
413 if (lfsp->lfs_version > 1)
414 ++sp;
415 else
416 sp = (SEGUSE *)((SEGUSE_V1 *)sp + 1);
417 }
418 if (max >= lfsp->lfs_nseg)
419 return (0);
420 else
421 return (max);
422 }
423
424 static void
425 dump_dinode(struct ufs1_dinode *dip)
426 {
427 int i;
428 time_t at, mt, ct;
429
430 at = dip->di_atime;
431 mt = dip->di_mtime;
432 ct = dip->di_ctime;
433
434 (void)printf(" %so%o\t%s%d\t%s%d\t%s%d\t%s%llu\n",
435 "mode ", dip->di_mode,
436 "nlink ", dip->di_nlink,
437 "uid ", dip->di_uid,
438 "gid ", dip->di_gid,
439 "size ", (long long)dip->di_size);
440 (void)printf(" %s%s %s%s %s%s",
441 "atime ", ctime(&at),
442 "mtime ", ctime(&mt),
443 "ctime ", ctime(&ct));
444 (void)printf(" inum %d\n", dip->di_inumber);
445 (void)printf(" Direct Addresses\n");
446 for (i = 0; i < NDADDR; i++) {
447 (void)printf("\t0x%x", dip->di_db[i]);
448 if ((i % 6) == 5)
449 (void)printf("\n");
450 }
451 for (i = 0; i < NIADDR; i++)
452 (void)printf("\t0x%x", dip->di_ib[i]);
453 (void)printf("\n");
454 }
455
456 static int
457 dump_sum(int fd, struct lfs *lfsp, SEGSUM *sp, int segnum, daddr_t addr)
458 {
459 FINFO *fp;
460 int32_t *dp, *idp;
461 int i, j, acc;
462 int ck;
463 int numbytes, numblocks;
464 char *datap;
465 struct ufs1_dinode *inop;
466 size_t el_size;
467 u_int32_t datasum;
468 char *buf;
469
470 if (sp->ss_magic != SS_MAGIC ||
471 sp->ss_sumsum != (ck = cksum(&sp->ss_datasum,
472 lfsp->lfs_sumsize - sizeof(sp->ss_sumsum)))) {
473 /* Don't print "corrupt" if we're just too close to the edge */
474 if (dtosn(lfsp, addr + fsbtodb(lfsp, 1)) ==
475 dtosn(lfsp, addr))
476 (void)printf("dumplfs: %s %d address 0x%llx\n",
477 "corrupt summary block; segment", segnum,
478 (long long)addr);
479 return (0);
480 }
481 if (lfsp->lfs_version > 1 && sp->ss_ident != lfsp->lfs_ident) {
482 (void)printf("dumplfs: %s %d address 0x%llx\n",
483 "summary from a former life; segment", segnum,
484 (long long)addr);
485 return (0);
486 }
487
488 (void)printf("Segment Summary Info at 0x%llx\n", (long long)addr);
489 (void)printf(" %s0x%x\t%s%d\t%s%d\t%s%c%c\n %s0x%x\t%s0x%x",
490 "next ", sp->ss_next,
491 "nfinfo ", sp->ss_nfinfo,
492 "ninos ", sp->ss_ninos,
493 "flags ", (sp->ss_flags & SS_DIROP) ? 'D' : '-',
494 (sp->ss_flags & SS_CONT) ? 'C' : '-',
495 "sumsum ", sp->ss_sumsum,
496 "datasum ", sp->ss_datasum );
497 if (lfsp->lfs_version == 1)
498 (void)printf("\tcreate %s\n", ctime((time_t *)&sp->ss_ident));
499 else {
500 (void)printf("\tcreate %s", ctime((time_t *)&sp->ss_create));
501 (void)printf(" roll_id %-8x", sp->ss_ident);
502 (void)printf(" serial %lld\n", (long long)sp->ss_serial);
503 }
504
505 /* Dump out inode disk addresses */
506 dp = (int32_t *)sp;
507 dp += lfsp->lfs_sumsize / sizeof(int32_t);
508 inop = malloc(lfsp->lfs_bsize);
509 printf(" Inode addresses:");
510 numbytes = 0;
511 numblocks = 0;
512 for (dp--, i = 0; i < sp->ss_ninos; dp--) {
513 ++numblocks;
514 numbytes += lfsp->lfs_ibsize; /* add bytes for inode block */
515 printf("\t0x%x {", *dp);
516 get(fd, fsbtobyte(lfsp, *dp), inop, lfsp->lfs_ibsize);
517 for (j = 0; i < sp->ss_ninos && j < INOPB(lfsp); j++, i++) {
518 if (j > 0)
519 (void)printf(", ");
520 (void)printf("%dv%d", inop[j].di_inumber, inop[j].di_gen);
521 }
522 (void)printf("}");
523 if (((i/INOPB(lfsp)) % 4) == 3)
524 (void)printf("\n");
525 }
526 free(inop);
527
528 printf("\n");
529
530 if (lfsp->lfs_version == 1)
531 fp = (FINFO *)((SEGSUM_V1 *)sp + 1);
532 else
533 fp = (FINFO *)(sp + 1);
534 for (i = 0; i < sp->ss_nfinfo; i++) {
535 (void)printf(" FINFO for inode: %d version %d nblocks %d lastlength %d\n",
536 fp->fi_ino, fp->fi_version, fp->fi_nblocks,
537 fp->fi_lastlength);
538 dp = &(fp->fi_blocks[0]);
539 numblocks += fp->fi_nblocks;
540 for (j = 0; j < fp->fi_nblocks; j++, dp++) {
541 (void)printf("\t%d", *dp);
542 if ((j % 8) == 7)
543 (void)printf("\n");
544 if (j == fp->fi_nblocks - 1)
545 numbytes += fp->fi_lastlength;
546 else
547 numbytes += lfsp->lfs_bsize;
548 }
549 if ((j % 8) != 0)
550 (void)printf("\n");
551 fp = (FINFO *)dp;
552 }
553
554 if (datasum_check == 0)
555 return (numbytes);
556
557 /*
558 * Now that we know the number of blocks, run back through and
559 * compute the data checksum. (A bad data checksum is not enough
560 * to prevent us from continuing, but it odes merit a warning.)
561 */
562 idp = (int32_t *)sp;
563 idp += lfsp->lfs_sumsize / sizeof(int32_t);
564 --idp;
565 if (lfsp->lfs_version == 1) {
566 fp = (FINFO *)((SEGSUM_V1 *)sp + 1);
567 el_size = sizeof(unsigned long);
568 } else {
569 fp = (FINFO *)(sp + 1);
570 el_size = sizeof(u_int32_t);
571 }
572 datap = (char *)malloc(el_size * numblocks);
573 memset(datap, 0, el_size * numblocks);
574 acc = 0;
575 addr += btofsb(lfsp, lfsp->lfs_sumsize);
576 buf = malloc(lfsp->lfs_bsize);
577 for (i = 0; i < sp->ss_nfinfo; i++) {
578 while (addr == *idp) {
579 get(fd, fsbtobyte(lfsp, addr), buf, lfsp->lfs_ibsize);
580 memcpy(datap + acc * el_size, buf, el_size);
581 addr += btofsb(lfsp, lfsp->lfs_ibsize);
582 --idp;
583 ++acc;
584 }
585 for (j = 0; j < fp->fi_nblocks; j++) {
586 get(fd, fsbtobyte(lfsp, addr), buf, lfsp->lfs_fsize);
587 memcpy(datap + acc * el_size, buf, el_size);
588 if (j == fp->fi_nblocks - 1)
589 addr += btofsb(lfsp, fp->fi_lastlength);
590 else
591 addr += btofsb(lfsp, lfsp->lfs_bsize);
592 ++acc;
593 }
594 fp = (FINFO *)&(fp->fi_blocks[fp->fi_nblocks]);
595 }
596 while (addr == *idp) {
597 get(fd, fsbtobyte(lfsp, addr), buf, lfsp->lfs_ibsize);
598 memcpy(datap + acc * el_size, buf, el_size);
599 addr += btofsb(lfsp, lfsp->lfs_ibsize);
600 --idp;
601 ++acc;
602 }
603 free(buf);
604 if (acc != numblocks)
605 printf("** counted %d blocks but should have been %d\n",
606 acc, numblocks);
607 datasum = cksum(datap, numblocks * el_size);
608 if (datasum != sp->ss_datasum)
609 printf("** computed datasum 0x%lx does not match given datasum 0x%lx\n", (unsigned long)datasum, (unsigned long)sp->ss_datasum);
610 free(datap);
611
612 return (numbytes);
613 }
614
615 static void
616 dump_segment(int fd, int segnum, daddr_t addr, struct lfs *lfsp, int dump_sb)
617 {
618 struct lfs lfs_sb, *sbp;
619 SEGSUM *sump;
620 char *sumblock;
621 int did_one, nbytes, sb;
622 off_t sum_offset;
623 daddr_t new_addr;
624
625 (void)printf("\nSEGMENT %lld (Disk Address 0x%llx)\n",
626 (long long)dtosn(lfsp, addr), (long long)addr);
627 sum_offset = fsbtobyte(lfsp, addr);
628 sumblock = malloc(lfsp->lfs_sumsize);
629
630 if (lfsp->lfs_version > 1 && segnum == 0) {
631 if (fsbtob(lfsp, lfsp->lfs_start) < LFS_LABELPAD) {
632 /* First segment eats the disklabel */
633 sum_offset += fragroundup(lfsp, LFS_LABELPAD) -
634 fsbtob(lfsp, lfsp->lfs_start);
635 addr += btofsb(lfsp, fragroundup(lfsp, LFS_LABELPAD)) -
636 lfsp->lfs_start;
637 printf("Disklabel at 0x0\n");
638 }
639 }
640
641 sb = 0;
642 did_one = 0;
643 do {
644 get(fd, sum_offset, sumblock, lfsp->lfs_sumsize);
645 sump = (SEGSUM *)sumblock;
646 if ((lfsp->lfs_version > 1 &&
647 sump->ss_ident != lfsp->lfs_ident) ||
648 sump->ss_sumsum != cksum (&sump->ss_datasum,
649 lfsp->lfs_sumsize - sizeof(sump->ss_sumsum))) {
650 sbp = (struct lfs *)sump;
651 if ((sb = (sbp->lfs_magic == LFS_MAGIC))) {
652 printf("Superblock at 0x%x\n",
653 (unsigned)btofsb(lfsp, sum_offset));
654 if (dump_sb) {
655 get(fd, sum_offset, &(lfs_sb.lfs_dlfs),
656 sizeof(struct dlfs));
657 dump_super(&lfs_sb);
658 }
659 if (lfsp->lfs_version > 1)
660 sum_offset += fragroundup(lfsp, LFS_SBPAD);
661 else
662 sum_offset += LFS_SBPAD;
663 } else if (did_one)
664 break;
665 else {
666 printf("Segment at 0x%llx empty or corrupt\n",
667 (long long)addr);
668 break;
669 }
670 } else {
671 nbytes = dump_sum(fd, lfsp, sump, segnum,
672 btofsb(lfsp, sum_offset));
673 if (nbytes)
674 sum_offset += lfsp->lfs_sumsize + nbytes;
675 else
676 sum_offset = 0;
677 did_one = 1;
678 }
679 /* If the segment ends right on a boundary, it still ends */
680 new_addr = btofsb(lfsp, sum_offset);
681 /* printf("end daddr = 0x%lx\n", (long)new_addr); */
682 if (dtosn(lfsp, new_addr) != dtosn(lfsp, addr))
683 break;
684 } while (sum_offset);
685
686 return;
687 }
688
689 static void
690 dump_super(struct lfs *lfsp)
691 {
692 int i;
693
694 (void)printf(" %s0x%-8x %s0x%-8x %s%-10d\n",
695 "magic ", lfsp->lfs_magic,
696 "version ", lfsp->lfs_version,
697 "size ", lfsp->lfs_size);
698 (void)printf(" %s%-10d %s%-10d %s%-10d\n",
699 "ssize ", lfsp->lfs_ssize,
700 "dsize ", lfsp->lfs_dsize,
701 "bsize ", lfsp->lfs_bsize);
702 (void)printf(" %s%-10d %s%-10d %s%-10d\n",
703 "fsize ", lfsp->lfs_fsize,
704 "frag ", lfsp->lfs_frag,
705 "minfree ", lfsp->lfs_minfree);
706 (void)printf(" %s%-10d %s%-10d %s%-10d\n",
707 "inopb ", lfsp->lfs_inopb,
708 "ifpb ", lfsp->lfs_ifpb,
709 "nindir ", lfsp->lfs_nindir);
710 (void)printf(" %s%-10d %s%-10d %s%-10d\n",
711 "nseg ", lfsp->lfs_nseg,
712 "sepb ", lfsp->lfs_sepb,
713 "cleansz ", lfsp->lfs_cleansz);
714 (void)printf(" %s%-10d %s0x%-8x %s%-10d\n",
715 "segtabsz ", lfsp->lfs_segtabsz,
716 "segmask ", lfsp->lfs_segmask,
717 "segshift ", lfsp->lfs_segshift);
718 (void)printf(" %s0x%-8qx %s%-10d %s0x%-8qX\n",
719 "bmask ", (long long)lfsp->lfs_bmask,
720 "bshift ", lfsp->lfs_bshift,
721 "ffmask ", (long long)lfsp->lfs_ffmask);
722 (void)printf(" %s%-10d %s0x%-8qx %s%u\n",
723 "ffshift ", lfsp->lfs_ffshift,
724 "fbmask ", (long long)lfsp->lfs_fbmask,
725 "fbshift ", lfsp->lfs_fbshift);
726
727 (void)printf(" %s%-10d %s%-10d %s0x%-8x\n",
728 "sushift ", lfsp->lfs_sushift,
729 "fsbtodb ", lfsp->lfs_fsbtodb,
730 "cksum ", lfsp->lfs_cksum);
731 (void)printf(" %s%-10d %s%-10d %s%-10d\n",
732 "nclean ", lfsp->lfs_nclean,
733 "dmeta ", lfsp->lfs_dmeta,
734 "minfreeseg ", lfsp->lfs_minfreeseg);
735 (void)printf(" %s0x%-8x %s%-9d %s%-10d\n",
736 "roll_id ", lfsp->lfs_ident,
737 "interleave ", lfsp->lfs_interleave,
738 "sumsize ", lfsp->lfs_sumsize);
739 (void)printf(" %s%-10d %s0x%-8qx\n",
740 "seg0addr ", lfsp->lfs_start,
741 "maxfilesize ", (long long)lfsp->lfs_maxfilesize);
742
743
744 (void)printf(" Superblock disk addresses:\n ");
745 for (i = 0; i < LFS_MAXNUMSB; i++) {
746 (void)printf(" 0x%-8x", lfsp->lfs_sboffs[i]);
747 if (i == (LFS_MAXNUMSB >> 1))
748 (void)printf("\n ");
749 }
750 (void)printf("\n");
751
752 (void)printf(" Checkpoint Info\n");
753 (void)printf(" %s%-10d %s0x%-8x %s%-10d\n",
754 "freehd ", lfsp->lfs_freehd,
755 "idaddr ", lfsp->lfs_idaddr,
756 "ifile ", lfsp->lfs_ifile);
757 (void)printf(" %s%-10d %s%-10d %s%-10d\n",
758 "uinodes ", lfsp->lfs_uinodes,
759 "bfree ", lfsp->lfs_bfree,
760 "avail ", lfsp->lfs_avail);
761 (void)printf(" %s%-10d %s0x%-8x %s0x%-8x\n",
762 "nfiles ", lfsp->lfs_nfiles,
763 "lastseg ", lfsp->lfs_lastseg,
764 "nextseg ", lfsp->lfs_nextseg);
765 (void)printf(" %s0x%-8x %s0x%-8x %s%-10lld\n",
766 "curseg ", lfsp->lfs_curseg,
767 "offset ", lfsp->lfs_offset,
768 "serial ", (long long)lfsp->lfs_serial);
769 (void)printf(" tstamp %s", ctime((time_t *)&lfsp->lfs_tstamp));
770 }
771
772 static void
773 addseg(char *arg)
774 {
775 SEGLIST *p;
776
777 if ((p = malloc(sizeof(SEGLIST))) == NULL)
778 err(1, "malloc");
779 p->next = seglist;
780 p->num = atoi(arg);
781 seglist = p;
782 }
783
784 static void
785 dump_cleaner_info(struct lfs *lfsp, void *ipage)
786 {
787 CLEANERINFO *cip;
788
789 cip = (CLEANERINFO *)ipage;
790 if (lfsp->lfs_version > 1) {
791 (void)printf("free_head %d\n", cip->free_head);
792 (void)printf("free_tail %d\n", cip->free_tail);
793 }
794 (void)printf("clean\t%d\tdirty\t%d\n",
795 cip->clean, cip->dirty);
796 (void)printf("bfree\t%d\tavail\t%d\n\n",
797 cip->bfree, cip->avail);
798 }
799
800 static void
801 usage(void)
802 {
803 (void)fprintf(stderr, "usage: dumplfs [-adiS] [-b blkno] [-I blkno] [-s segno] filesys|device\n");
804 exit(1);
805 }
806