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