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