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