xref: /netbsd/usr.sbin/dumpfs/dumpfs.c (revision 6550d01e)
1 /*	$NetBSD: dumpfs.c,v 1.57 2010/02/27 12:07:40 mlelstv Exp $	*/
2 
3 /*
4  * Copyright (c) 1983, 1992, 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 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1983, 1992, 1993\
35  The Regents of the University of California.  All rights reserved.");
36 #endif /* not lint */
37 
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)dumpfs.c	8.5 (Berkeley) 4/29/95";
41 #else
42 __RCSID("$NetBSD: dumpfs.c,v 1.57 2010/02/27 12:07:40 mlelstv Exp $");
43 #endif
44 #endif /* not lint */
45 
46 #include <sys/param.h>
47 #include <sys/time.h>
48 
49 #include <sys/wapbl.h>
50 #include <sys/wapbl_replay.h>
51 
52 #include <ufs/ufs/dinode.h>
53 #include <ufs/ufs/ufs_bswap.h>
54 #include <ufs/ufs/ufs_wapbl.h>
55 #include <ufs/ffs/fs.h>
56 #include <ufs/ffs/ffs_extern.h>
57 
58 #include <err.h>
59 #include <errno.h>
60 #include <fcntl.h>
61 #include <fstab.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <stddef.h>
66 #include <unistd.h>
67 #include <util.h>
68 
69 union fsun {
70 	struct fs fs;
71 	char pad[MAXBSIZE];
72 } fsun;
73 #define	afs	fsun.fs
74 
75 uint16_t opostblsave[32*8];
76 
77 union {
78 	struct cg cg;
79 	char pad[MAXBSIZE];
80 } cgun;
81 #define	acg	cgun.cg
82 
83 union {
84 	struct wapbl_wc_header wh;
85 	struct wapbl_wc_null wn;
86 	char pad[MAXBSIZE];
87 } jbuf;
88 #define awh 	jbuf.wh
89 #define awn 	jbuf.wn
90 
91 #define OPT_FLAG(ch)	(1 << ((ch) & 31))
92 #define ISOPT(opt)	(opt_flags & (opt))
93 #define opt_alt_super	OPT_FLAG('a')
94 #define opt_superblock	OPT_FLAG('s')
95 #define opt_cg_summary	OPT_FLAG('m')
96 #define opt_cg_info	OPT_FLAG('c')
97 #define opt_inodes	OPT_FLAG('i')
98 #define opt_journal	OPT_FLAG('j')
99 #define opt_verbose	OPT_FLAG('v')
100 #define DFLT_CHECK (opt_alt_super | opt_cg_info | opt_inodes | \
101     opt_cg_summary | opt_superblock | opt_journal )
102 #define DFLT_OPTS	(opt_superblock | opt_cg_summary | opt_cg_info | opt_verbose)
103 
104 long	dev_bsize = 512;
105 int	needswap, printold, is_ufs2;
106 int	Fflag;
107 
108 uint	opt_flags;
109 
110 int	dumpfs(const char *);
111 int	print_superblock(struct fs *, uint16_t *, const char *, int, off_t);
112 int	print_cgsum(const char *, int);
113 int	print_cginfo(const char *, int);
114 int	print_inodes(const char *, int, int, int);
115 int	print_alt_super(const char *, int);
116 int	print_journal(const char *, int);
117 void	print_journal_header(const char *);
118 off_t	print_journal_entries(const char *, size_t);
119 int	dumpcg(const char *, int, int);
120 int	main(int, char **);
121 int	openpartition(const char *, int, char *, size_t);
122 void	pbits(int, void *, int);
123 void	usage(void);
124 void	print_ufs1_inode(int, int, void *);
125 void	print_ufs2_inode(int, int, void *);
126 void	fix_superblock(struct fs *, uint16_t *);
127 
128 int
129 main(int argc, char *argv[])
130 {
131 	int ch, eval;
132 
133 	while ((ch = getopt(argc, argv, "acijmsvF")) != -1)
134 		switch(ch) {
135 		case 'a':	/* alternate superblocks */
136 		case 'c':	/* cylinder group info */
137 		case 'i':	/* actual inodes */
138 		case 'j':	/* journal */
139 		case 'm':	/* cylinder group summary */
140 		case 's':	/* superblock */
141 		case 'v':	/* more verbose */
142 			opt_flags |= OPT_FLAG(ch);
143 			break;
144 		case 'F':	/* File (not device) */
145 			Fflag = 1;
146 			break;
147 		case '?':
148 		default:
149 			usage();
150 		}
151 	argc -= optind;
152 	argv += optind;
153 
154 	if ((opt_flags & DFLT_CHECK) == 0)
155 		opt_flags |= DFLT_OPTS;
156 
157 	if (argc < 1)
158 		usage();
159 
160 	for (eval = 0; *argv; ++argv) {
161 		eval |= dumpfs(*argv);
162 	}
163 
164 	exit(eval);
165 }
166 
167 
168 int
169 dumpfs(const char *name)
170 {
171 	static const off_t sblock_try[] = SBLOCKSEARCH;
172 	char device[MAXPATHLEN];
173 	int fd, i;
174 	int rval = 1;
175 
176 	if (Fflag)
177 		fd = open(name, O_RDONLY);
178 	else {
179 		fd = openpartition(name, O_RDONLY, device, sizeof(device));
180 		name = device;
181 	}
182 	if (fd == -1)
183 		goto err;
184 
185 	for (i = 0; ; i++) {
186 		if (sblock_try[i] == -1) {
187 			warnx("%s: could not find superblock, skipped", name);
188 			return 1;
189 		}
190 		if (lseek(fd, sblock_try[i], SEEK_SET) == (off_t)-1)
191 			continue;
192 		if (read(fd, &afs, SBLOCKSIZE) != SBLOCKSIZE)
193 			continue;
194 		switch(afs.fs_magic) {
195 		case FS_UFS2_MAGIC:
196 			is_ufs2 = 1;
197 			break;
198 		case FS_UFS1_MAGIC:
199 			break;
200 		case FS_UFS2_MAGIC_SWAPPED:
201 			is_ufs2 = 1;
202 			needswap = 1;
203 			break;
204 		case FS_UFS1_MAGIC_SWAPPED:
205 			needswap = 1;
206 			break;
207 		default:
208 			continue;
209 		}
210 		fix_superblock(&afs, opostblsave);
211 		if (printold) {
212 			if (sblock_try[i] == SBLOCK_UFS2)
213 				/* This might be an alternate superblock */
214 				continue;
215 		} else {
216 			if (sblock_try[i] != afs.fs_sblockloc)
217 				/* This must be an alternate superblock */
218 				continue;
219 		}
220 		break;
221 	}
222 
223 
224 	dev_bsize = afs.fs_fsize / fsbtodb(&afs, 1);
225 
226 	rval = 0;
227 	printf("file system: %s\n", name);
228 
229 	if (ISOPT(opt_superblock))
230 		rval = print_superblock(&afs, opostblsave, name, fd, sblock_try[i]);
231 	if (rval == 0 && ISOPT(opt_cg_summary))
232 		rval = print_cgsum(name, fd);
233 	if (rval == 0 && ISOPT(opt_alt_super))
234 		rval = print_alt_super(name, fd);
235 	if (rval == 0 && ISOPT(opt_journal))
236 		rval = print_journal(name, fd);
237 	if (rval == 0 && ISOPT(opt_cg_info))
238 		rval = print_cginfo(name, fd);
239 	else if (rval == 0 && ISOPT(opt_inodes))
240 		rval = print_inodes(name, fd, 0, afs.fs_ncg);
241 
242     err:
243 	if (fd != -1)
244 		(void)close(fd);
245 	if (rval)
246 		warn("%s", name);
247 	return rval;
248 }
249 
250 void
251 fix_superblock(struct fs *fs, uint16_t *opostbl)
252 {
253 	if (needswap &&
254 	    (((int32_t)bswap32(fs->fs_old_postblformat) == FS_42POSTBLFMT) ||
255 	     (bswap32(fs->fs_old_postbloff) == offsetof(struct fs, fs_old_postbl_start)))) {
256 		int i;
257 		memcpy(opostbl, &fs->fs_old_postbl_start, 512);
258 		for(i=0;i<256;i++)
259 			opostbl[i] = bswap16(opostbl[i]);
260 	} else if (!needswap &&
261 	   ((fs->fs_old_postblformat == FS_42POSTBLFMT) ||
262 	    (fs->fs_old_postbloff == offsetof(struct fs, fs_old_postbl_start)))) {
263 		memcpy(opostbl, &fs->fs_old_postbl_start, 512);
264 	}
265 
266 	if (needswap)
267 		ffs_sb_swap(fs, fs);
268 
269 	printold = (fs->fs_magic == FS_UFS1_MAGIC &&
270 		    (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0);
271 	if (printold) {
272 		fs->fs_sblockloc = SBLOCK_UFS1;
273 		fs->fs_flags = fs->fs_old_flags;
274 		fs->fs_maxbsize = fs->fs_bsize;
275 		fs->fs_time = fs->fs_old_time;
276 		fs->fs_size = fs->fs_old_size;
277 		fs->fs_dsize = fs->fs_old_dsize;
278 		fs->fs_csaddr = fs->fs_old_csaddr;
279 		fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
280 		fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
281 		fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
282 		fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
283 	}
284 
285 	if (printold && fs->fs_old_postblformat == FS_42POSTBLFMT)
286 		fs->fs_old_nrpos = 8;
287 }
288 
289 int
290 print_superblock(struct fs *fs, uint16_t *opostbl,
291     const char *name, int fd, off_t sblock)
292 {
293 	int i, size;
294 	time_t t;
295 	int32_t fsflags;
296 
297 	printf("format\tFFSv%d\n", is_ufs2+1);
298 #if BYTE_ORDER == LITTLE_ENDIAN
299 	if (needswap)
300 #else
301 	if (!needswap)
302 #endif
303 		printf("endian\tbig-endian\n");
304 	else
305 		printf("endian\tlittle-endian\n");
306 	t = fs->fs_time;
307 	if ((sblock != SBLOCK_UFS1) || ISOPT(opt_alt_super))
308 		printf("location %lld\t(-b %lld)\n",
309 		    (long long)sblock, (long long)(sblock/dev_bsize));
310 	printf("magic\t%-8x\ttime\t%s",
311 	    fs->fs_magic, ctime(&t));
312 
313 	if (is_ufs2)
314 		i = 5;
315 	else {
316 		i = 0;
317 		if (fs->fs_old_postblformat != FS_42POSTBLFMT) {
318 			i++;
319 			if (fs->fs_old_inodefmt >= FS_44INODEFMT) {
320 				int max;
321 
322 				i++;
323 				max = fs->fs_maxcontig;
324 				size = fs->fs_contigsumsize;
325 				if ((max < 2 && size == 0)
326 				    || (max > 1 && size >= MIN(max, FS_MAXCONTIG)))
327 					i++;
328 			}
329 			if (fs->fs_old_flags & FS_FLAGS_UPDATED) {
330 				i = 4;
331 			}
332 		}
333 	}
334 	if (!printold || fs->fs_sblockloc != SBLOCK_UFS1 ||
335 	    fs->fs_id[0] || fs->fs_id[1])
336 		printf("superblock location\t%jd\tid\t[ %x %x ]\n",
337 		    (intmax_t)fs->fs_sblockloc, fs->fs_id[0], fs->fs_id[1]);
338 	printf("cylgrp\t%s\tinodes\t%s\tsblock\t%s\tfslevel %d\n",
339 	    i < 1 ? "static" : "dynamic",
340 	    i < 2 ? "4.2/4.3BSD" : i < 5 ? "4.4BSD" : "FFSv2",
341 	    i < 4 ? "FFSv1" : "FFSv2", i);
342 	printf("nbfree\t%lld\tndir\t%lld\tnifree\t%lld\tnffree\t%lld\n",
343 	    (long long)fs->fs_cstotal.cs_nbfree,
344 	    (long long)fs->fs_cstotal.cs_ndir,
345 	    (long long)fs->fs_cstotal.cs_nifree,
346 	    (long long)fs->fs_cstotal.cs_nffree);
347 	if (printold)
348 		printf("ncg\t%d\tncyl\t%d\tsize\t%lld\tblocks\t%lld\n",
349 		    fs->fs_ncg, fs->fs_old_ncyl, (long long)fs->fs_size, (long long)fs->fs_dsize);
350 	else
351 		printf("ncg\t%d\tsize\t%lld\tblocks\t%lld\n",
352 		    fs->fs_ncg, (long long)fs->fs_size, (long long)fs->fs_dsize);
353 	printf("bsize\t%d\tshift\t%d\tmask\t0x%08x\n",
354 	    fs->fs_bsize, fs->fs_bshift, fs->fs_bmask);
355 	printf("fsize\t%d\tshift\t%d\tmask\t0x%08x\n",
356 	    fs->fs_fsize, fs->fs_fshift, fs->fs_fmask);
357 	printf("frag\t%d\tshift\t%d\tfsbtodb\t%d\n",
358 	    fs->fs_frag, fs->fs_fragshift, fs->fs_fsbtodb);
359 	if (printold)
360 		printf("cpg\t%d\t", fs->fs_old_cpg);
361 	printf("bpg\t%d\tfpg\t%d\tipg\t%d\n",
362 	    fs->fs_fpg / fs->fs_frag, fs->fs_fpg, fs->fs_ipg);
363 	printf("minfree\t%d%%\toptim\t%s\tmaxcontig %d\tmaxbpg\t%d\n",
364 	    fs->fs_minfree, fs->fs_optim == FS_OPTSPACE ? "space" : "time",
365 	    fs->fs_maxcontig, fs->fs_maxbpg);
366 	if (printold) {
367 		printf("rotdelay %dms\theadswitch %dus\ttrackseek %dus\trps\t%d\n",
368 		    fs->fs_old_rotdelay, fs->fs_old_headswitch,
369 		    fs->fs_old_trkseek, fs->fs_old_rps);
370 		printf("ntrak\t%d\tnsect\t%d\tnpsect\t%d\tspc\t%d\n",
371 		    fs->fs_spare2, fs->fs_old_nsect, fs->fs_old_npsect,
372 		    fs->fs_old_spc);
373 	}
374 	printf("symlinklen %d\t", fs->fs_maxsymlinklen);
375 	if (printold)
376 		printf("trackskew %d\tinterleave %d\t",
377 		    fs->fs_old_trackskew, fs->fs_old_interleave);
378 	printf("contigsumsize %d\n", fs->fs_contigsumsize);
379 	printf("maxfilesize 0x%016llx\n",
380 	    (unsigned long long)fs->fs_maxfilesize);
381 	if (printold)
382 		printf("nindir\t%d\tinopb\t%d\tnspf\t%d\n", fs->fs_nindir,
383 		    fs->fs_inopb, fs->fs_old_nspf);
384 	else
385 		printf("nindir\t%d\tinopb\t%d\n", fs->fs_nindir, fs->fs_inopb);
386 	if (!printold || (fs->fs_avgfilesize > 0) || (fs->fs_avgfpdir > 0))
387 		printf("avgfilesize %d\tavgfpdir %d\n",
388 		    fs->fs_avgfilesize, fs->fs_avgfpdir);
389 	printf("sblkno\t%d\tcblkno\t%d\tiblkno\t%d\tdblkno\t%d\n",
390 	    fs->fs_sblkno, fs->fs_cblkno, fs->fs_iblkno, fs->fs_dblkno);
391 	printf("sbsize\t%d\tcgsize\t%d", fs->fs_sbsize, fs->fs_cgsize);
392 	if (printold)
393 		printf("\toffset\t%d\tmask\t0x%08x",
394 		    fs->fs_old_cgoffset, fs->fs_old_cgmask);
395 	printf("\ncsaddr\t%lld\tcssize\t%d",
396 	    (long long)fs->fs_csaddr, fs->fs_cssize);
397 	if (printold)
398 		printf("\tshift\t%d\tmask\t0x%08x",
399 		    fs->fs_old_csshift, fs->fs_old_csmask);
400 	printf("\ncgrotor\t%d\tfmod\t%d\tronly\t%d\tclean\t0x%02x\n",
401 	    fs->fs_cgrotor, fs->fs_fmod, fs->fs_ronly, fs->fs_clean);
402 	printf("wapbl version 0x%x\tlocation %u\tflags 0x%x\n",
403 	    fs->fs_journal_version, fs->fs_journal_location,
404 	    fs->fs_journal_flags);
405 	printf("wapbl loc0 %" PRIu64 "\tloc1 %" PRIu64,
406 	    fs->fs_journallocs[0], fs->fs_journallocs[1]);
407 	printf("\tloc2 %" PRIu64 "\tloc3 %" PRIu64 "\n",
408 	    fs->fs_journallocs[2], fs->fs_journallocs[3]);
409 	printf("flags\t");
410 	if (fs->fs_flags == 0)
411 		printf("none");
412 	if (fs->fs_flags & FS_UNCLEAN)
413 		printf("unclean ");
414 	if (fs->fs_flags & FS_DOSOFTDEP)
415 		printf("soft-updates ");
416 	if (fs->fs_flags & FS_NEEDSFSCK)
417 		printf("needs fsck run ");
418 	if (fs->fs_flags & FS_INDEXDIRS)
419 		printf("indexed directories ");
420 	if (fs->fs_flags & FS_ACLS)
421 		printf("acls ");
422 	if (fs->fs_flags & FS_MULTILABEL)
423 		printf("multilabel ");
424 	if (fs->fs_flags & FS_FLAGS_UPDATED)
425 		printf("fs_flags expanded ");
426 	if (fs->fs_flags & FS_DOWAPBL)
427 		printf("wapbl ");
428 	fsflags = fs->fs_flags & ~(FS_UNCLEAN | FS_DOSOFTDEP | FS_NEEDSFSCK |
429 			FS_INDEXDIRS | FS_ACLS | FS_MULTILABEL |
430 			FS_FLAGS_UPDATED | FS_DOWAPBL);
431 	if (fsflags != 0)
432 		printf("unknown flags (%#x)", fsflags);
433 	printf("\nfsmnt\t%s\n", fs->fs_fsmnt);
434 	if (!printold)
435 		printf("volname\t%s\tswuid\t%ju\n",
436 		    fs->fs_volname, (uintmax_t)fs->fs_swuid);
437 	if (printold) {
438 		if (fs->fs_old_cpc != 0)
439 			printf("blocks available in each of %d rotational "
440 			       "positions\n", fs->fs_old_nrpos);
441 		else
442 			printf("(no rotational position table)\n\n");
443 		if (ISOPT(opt_verbose)) {
444 			int c, j, k;
445 			for (c = 0; c < fs->fs_old_cpc; c++) {
446 				printf("cylinder number %d:", c);
447 				for (i = 0; i < fs->fs_old_nrpos; i++) {
448 					if (old_fs_postbl(&afs, c, opostbl)[i] == -1)
449 						continue;
450 					printf("\n   position %d:\t", i);
451 					for (j = old_fs_postbl(&afs, c, opostbl)[i], k = 1; ;
452 							 j += old_fs_rotbl(&afs)[j], k++) {
453 						printf("%5d", j);
454 						if (k % 12 == 0)
455 							printf("\n\t\t");
456 						if (old_fs_rotbl(&afs)[j] == 0)
457 							break;
458 					}
459 				}
460 				printf("\n");
461 			}
462 		}
463 	}
464 
465 	return 0;
466 }
467 
468 int
469 print_cgsum(const char *name, int fd)
470 {
471 	struct csum *ccsp;
472 	int i, j, size;
473 
474 	afs.fs_csp = calloc(1, afs.fs_cssize);
475 	for (i = 0, j = 0; i < afs.fs_cssize; i += afs.fs_bsize, j++) {
476 		size = afs.fs_cssize - i < afs.fs_bsize ?
477 		    afs.fs_cssize - i : afs.fs_bsize;
478 		ccsp = (struct csum *)((char *)afs.fs_csp + i);
479 		if (lseek(fd,
480 		    (off_t)(fsbtodb(&afs, (afs.fs_csaddr + j * afs.fs_frag))) *
481 		    dev_bsize, SEEK_SET) == (off_t)-1)
482 			return 1;
483 		if (read(fd, ccsp, size) != size)
484 			return 1;
485 		if (needswap)
486 			ffs_csum_swap(ccsp, ccsp, size);
487 	}
488 
489 	printf("cs[].cs_(nbfree,ndir,nifree,nffree):\n\t");
490 	for (i = 0; i < afs.fs_ncg; i++) {
491 		struct csum *cs = &afs.fs_cs(&afs, i);
492 		if (i && i % 4 == 0)
493 			printf("\n\t");
494 		printf("(%d,%d,%d,%d) ",
495 		    cs->cs_nbfree, cs->cs_ndir, cs->cs_nifree, cs->cs_nffree);
496 	}
497 	printf("\n");
498 	if (printold && (afs.fs_old_ncyl % afs.fs_old_cpg)) {
499 		printf("cylinders in last group %d\n",
500 		    i = afs.fs_old_ncyl % afs.fs_old_cpg);
501 		printf("blocks in last group %d\n",
502 		    i * afs.fs_old_spc / (afs.fs_old_nspf << afs.fs_fragshift));
503 	}
504 	free(afs.fs_csp);
505 	afs.fs_csp = NULL;
506 
507 	return 0;
508 }
509 
510 int
511 print_alt_super(const char *name, int fd)
512 {
513 	union fsun alt;
514 	int i;
515 	off_t loc;
516 	uint16_t alt_opostblsave[32*8];
517 	int save_printold;
518 
519 	for (i = 0; i < afs.fs_ncg; i++) {
520 		loc = (off_t)(fsbtodb(&afs, cgsblock(&afs, i))) * dev_bsize;
521 		printf("\nalternate %d\n", i);
522 		if (pread(fd, &alt, sizeof alt, loc) != sizeof alt) {
523 			warnx("%s: error reading alt %d", name, i);
524 			return (1);
525 		}
526 		save_printold = printold;
527 		fix_superblock(&alt.fs, alt_opostblsave);
528 		if (print_superblock(&alt.fs, alt_opostblsave, name, fd, loc)) {
529 			printold = save_printold;
530 			return 1;
531 		}
532 		printold = save_printold;
533 	}
534 	return 0;
535 }
536 
537 int
538 print_cginfo(const char *name, int fd)
539 {
540 	int i;
541 
542 	printf("\n");
543 	for (i = 0; i < afs.fs_ncg; i++) {
544 		printf("\n");
545 		if (dumpcg(name, fd, i))
546 			return 1;
547 		if (ISOPT(opt_inodes) && print_inodes(name, fd, i, 1))
548 			return 1;
549 	}
550 	return 0;
551 }
552 
553 int
554 print_inodes(const char *name, int fd, int c, int n)
555 {
556 	void *ino_buf = malloc(afs.fs_bsize);
557 	void (*print_inode)(int, int, void *);
558 	int i, inum;
559 
560 	if (ino_buf == 0)
561 		return 1;
562 
563 	print_inode = is_ufs2 ? print_ufs2_inode : print_ufs1_inode;
564 
565 	for (inum = c * afs.fs_ipg ; inum < (c+n) * afs.fs_ipg; inum += afs.fs_inopb) {
566 		if (pread(fd, ino_buf, afs.fs_bsize,
567 		    ino_to_fsba(&afs, inum) * afs.fs_fsize) != afs.fs_bsize) {
568 			free(ino_buf);
569 			return 1;
570 		}
571 		for (i = 0; i < afs.fs_inopb; i++)
572 			print_inode(inum + i, i, ino_buf);
573 	}
574 
575 	free(ino_buf);
576 	return 0;
577 }
578 
579 int
580 dumpcg(const char *name, int fd, int c)
581 {
582 	off_t cur;
583 	int i, j;
584 	time_t t;
585 
586 	printf("cg %d:\n", c);
587 	if ((cur = lseek(fd, (off_t)(fsbtodb(&afs, cgtod(&afs, c))) * dev_bsize,
588 	    SEEK_SET)) == (off_t)-1)
589 		return (1);
590 	if (read(fd, &acg, afs.fs_bsize) != afs.fs_bsize) {
591 		warnx("%s: error reading cg", name);
592 		return (1);
593 	}
594 	if (needswap)
595 		ffs_cg_swap(&acg, &acg, &afs);
596 	if (printold)
597 		t = acg.cg_old_time;
598 	else
599 		t = acg.cg_time;
600 	printf("magic\t%x\ttell\t%llx\ttime\t%s",
601 	    afs.fs_old_postblformat == FS_42POSTBLFMT ?
602 	    ((struct ocg *)&acg)->cg_magic : acg.cg_magic,
603 	    (long long)cur, ctime(&t));
604 	if (printold)
605 		printf("cgx\t%d\tncyl\t%d\tniblk\t%d\tndblk\t%d\n",
606 		    acg.cg_cgx, acg.cg_old_ncyl, acg.cg_old_niblk,
607 		    acg.cg_ndblk);
608 	else
609 		printf("cgx\t%d\tniblk\t%d\tndblk\t%d\n",
610 		    acg.cg_cgx, acg.cg_niblk, acg.cg_ndblk);
611 	printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
612 	    acg.cg_cs.cs_nbfree, acg.cg_cs.cs_ndir,
613 	    acg.cg_cs.cs_nifree, acg.cg_cs.cs_nffree);
614 	printf("rotor\t%d\tirotor\t%d\tfrotor\t%d\nfrsum",
615 	    acg.cg_rotor, acg.cg_irotor, acg.cg_frotor);
616 	for (i = 1, j = 0; i < afs.fs_frag; i++) {
617 		printf("\t%d", acg.cg_frsum[i]);
618 		j += i * acg.cg_frsum[i];
619 	}
620 	printf("\nsum of frsum: %d", j);
621 	if (afs.fs_contigsumsize > 0) {
622 		for (i = 1; i < afs.fs_contigsumsize; i++) {
623 			if ((i - 1) % 8 == 0)
624 				printf("\nclusters %d-%d:", i,
625 				    afs.fs_contigsumsize - 1 < i + 7 ?
626 				    afs.fs_contigsumsize - 1 : i + 7);
627 			printf("\t%d", cg_clustersum(&acg, 0)[i]);
628 		}
629 		printf("\nclusters size %d and over: %d\n",
630 		    afs.fs_contigsumsize,
631 		    cg_clustersum(&acg, 0)[afs.fs_contigsumsize]);
632 		printf("clusters free:\t");
633 		pbits(0, cg_clustersfree(&acg, 0), acg.cg_nclusterblks);
634 	} else
635 		printf("\n");
636 	printf("iused:\t");
637 	pbits(0 * c * afs.fs_ipg, cg_inosused(&acg, 0), afs.fs_ipg);
638 	printf("free:\t");
639 	pbits(0, cg_blksfree(&acg, 0), afs.fs_fpg);
640 	if (printold && ISOPT(opt_verbose)) {
641 		printf("b:\n");
642 		for (i = 0; i < afs.fs_old_cpg; i++) {
643 			if (old_cg_blktot(&acg, 0)[i] == 0)
644 				continue;
645 			printf("   c%d:\t(%d)\t", i, old_cg_blktot(&acg, 0)[i]);
646 			for (j = 0; j < afs.fs_old_nrpos; j++) {
647 				if (afs.fs_old_cpc > 0 &&
648 						old_fs_postbl(&afs, i % afs.fs_old_cpc, opostblsave)[j] == -1)
649 					continue;
650 				printf(" %d", old_cg_blks(&afs, &acg, i, 0)[j]);
651 			}
652 			printf("\n");
653 		}
654 	}
655 	return (0);
656 }
657 
658 void
659 print_ufs1_inode(int inum, int i_off, void *ibuf)
660 {
661 	struct ufs1_dinode *i = ibuf;
662 
663 	i += i_off;
664 
665 	if (needswap)
666 		ffs_dinode1_swap(i,i);
667 
668 	if (afs.fs_old_inodefmt < FS_44INODEFMT) {
669 		i->di_uid = i->di_ouid;
670 		i->di_gid = i->di_ogid;
671 	}
672 
673 	if (inum % afs.fs_ipg == 0)
674 		printf("   inode:   mode  nlink                 size"
675 		    "      ctime.nsec         flags     blocks"
676 		    " generation      uid        gid\n");
677 	if (i->di_mode == 0 && i->di_nlink == 0 && !ISOPT(opt_verbose))
678 		return;
679 	printf("%8u: %6o %6d %20" PRIu64 " %10u.%09u %8x %10u %8x %10u %10u\n",
680 		inum, i->di_mode, i->di_nlink, i->di_size,
681 		i->di_ctime, i->di_ctimensec, i->di_flags, i->di_blocks,
682 		i->di_gen, i->di_uid, i->di_gid);
683 }
684 
685 void
686 print_ufs2_inode(int inum, int i_off, void *ibuf)
687 {
688 	struct ufs2_dinode *i = ibuf;
689 
690 	i += i_off;
691 
692 	if (needswap)
693 		ffs_dinode2_swap(i,i);
694 
695 	if (inum % afs.fs_ipg == 0)
696 		printf("   inode:   mode  nlink                 size"
697 		    "      ctime.nsec         flags     blocks"
698 		    " generation      uid        gid\n");
699 
700 	if (i->di_mode == 0 && i->di_nlink == 0 && !ISOPT(opt_verbose))
701 		return;
702 
703 	printf("%8u: %6o %6d %20" PRIu64 " %10" PRIu64 ".%09u %8x %10" PRIu64 " %8x %10u %10u\n",
704 		inum, i->di_mode, i->di_nlink, i->di_size,
705 		i->di_ctime, i->di_ctimensec, i->di_flags, i->di_blocks,
706 		i->di_gen, i->di_uid, i->di_gid);
707 }
708 
709 void
710 pbits(int offset, void *vp, int max)
711 {
712 	int i;
713 	char *p;
714 	int count, j;
715 
716 	for (count = i = 0, p = vp; i < max; i++)
717 		if (isset(p, i)) {
718 			if (count)
719 				printf(",%s", count % 6 ? " " : "\n\t");
720 			count++;
721 			printf("%d", offset + i);
722 			j = i;
723 			while ((i+1)<max && isset(p, i+1))
724 				i++;
725 			if (i != j)
726 				printf("-%d", offset + i);
727 		}
728 	printf("\n");
729 }
730 
731 int
732 print_journal(const char *name, int fd)
733 {
734 	daddr_t off;
735 	size_t count, blklen, bno, skip;
736 	off_t boff, head, tail, len;
737 	uint32_t generation;
738 
739 	if (afs.fs_journal_version != UFS_WAPBL_VERSION)
740 		return 0;
741 
742 	generation = 0;
743 	head = tail = 0;
744 
745 	switch (afs.fs_journal_location) {
746 	case UFS_WAPBL_JOURNALLOC_END_PARTITION:
747 	case UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM:
748 
749 		off    = afs.fs_journallocs[0];
750 		count  = afs.fs_journallocs[1];
751 		blklen = afs.fs_journallocs[2];
752 
753 		for (bno=0; bno<count; bno += skip / blklen) {
754 
755 			skip = blklen;
756 
757 			boff = bno * blklen;
758 			if (bno * blklen >= 2 * blklen &&
759 			  ((head >= tail && (boff < tail || boff >= head)) ||
760 			  (head < tail && (boff >= head && boff < tail))))
761 				continue;
762 
763 			printf("journal block %lu offset %lld\n",
764 				(unsigned long)bno, (long long) boff);
765 
766 			if (lseek(fd, (off_t)(off*blklen) + boff, SEEK_SET)
767 			    == (off_t)-1)
768 				return (1);
769 			if (read(fd, &jbuf, blklen) != (ssize_t)blklen) {
770 				warnx("%s: error reading journal", name);
771 				return 1;
772 			}
773 
774 			switch (awh.wc_type) {
775 			case 0:
776 				break;
777 			case WAPBL_WC_HEADER:
778 				print_journal_header(name);
779 				if (awh.wc_generation > generation) {
780 					head = awh.wc_head;
781 					tail = awh.wc_tail;
782 				}
783 				generation = awh.wc_generation;
784 				skip = awh.wc_len;
785 				break;
786 			default:
787 				len = print_journal_entries(name, blklen);
788 				skip = awh.wc_len;
789 				if (len != (off_t)skip)
790 					printf("  CORRUPTED RECORD\n");
791 				break;
792 			}
793 
794 			if (blklen == 0)
795 				break;
796 
797 			skip = (skip + blklen - 1) / blklen * blklen;
798 			if (skip == 0)
799 				break;
800 
801 		}
802 		break;
803 	}
804 
805 	return 0;
806 }
807 
808 static const char *
809 wapbl_type_string(unsigned t)
810 {
811 	static char buf[12];
812 
813 	switch (t) {
814 	case WAPBL_WC_BLOCKS:
815 		return "blocks";
816 	case WAPBL_WC_REVOCATIONS:
817 		return "revocations";
818 	case WAPBL_WC_INODES:
819 		return "inodes";
820 	case WAPBL_WC_HEADER:
821 		return "header";
822 	}
823 
824 	snprintf(buf,sizeof(buf),"%08x",t);
825 	return buf;
826 }
827 
828 void
829 print_journal_header(const char *name)
830 {
831 	printf("  type %s len %d  version %u\n",
832 		wapbl_type_string(awh.wc_type), awh.wc_len,
833 		awh.wc_version);
834 	printf("  checksum      %08x  generation %9u\n",
835 		awh.wc_checksum, awh.wc_generation);
836 	printf("  fsid %08x.%08x  time %llu nsec %u\n",
837 		awh.wc_fsid[0], awh.wc_fsid[1],
838 		(unsigned long long)awh.wc_time, awh.wc_timensec);
839 	printf("  log_bshift  %10u  fs_bshift %10u\n",
840 		awh.wc_log_dev_bshift, awh.wc_fs_dev_bshift);
841 	printf("  head        %10lld  tail      %10lld\n",
842 		(long long)awh.wc_head, (long long)awh.wc_tail);
843 	printf("  circ_off    %10lld  circ_size %10lld\n",
844 		(long long)awh.wc_circ_off, (long long)awh.wc_circ_size);
845 }
846 
847 off_t
848 print_journal_entries(const char *name, size_t blklen)
849 {
850 	int i, n;
851 	struct wapbl_wc_blocklist *wcb;
852 	struct wapbl_wc_inodelist *wci;
853 	off_t len = 0;
854 	int ph;
855 
856 	printf("  type %s len %d",
857 		wapbl_type_string(awn.wc_type), awn.wc_len);
858 
859 	switch (awn.wc_type) {
860 	case WAPBL_WC_BLOCKS:
861 	case WAPBL_WC_REVOCATIONS:
862 		wcb = (struct wapbl_wc_blocklist *)&awn;
863 		printf("  blkcount %u\n", wcb->wc_blkcount);
864 		ph = (blklen - offsetof(struct wapbl_wc_blocklist, wc_blocks))
865 			/ sizeof(wcb->wc_blocks[0]);
866 		n = MIN(wcb->wc_blkcount, ph);
867 		for (i=0; i<n; i++) {
868 			if (ISOPT(opt_verbose)) {
869 				printf("  %3d: daddr %14llu  dlen %d\n", i,
870 				 (unsigned long long)wcb->wc_blocks[i].wc_daddr,
871 				 wcb->wc_blocks[i].wc_dlen);
872 			}
873 			len += wcb->wc_blocks[i].wc_dlen;
874 		}
875 		if (awn.wc_type == WAPBL_WC_BLOCKS) {
876 			if (len % blklen)
877 				len += blklen - len % blklen;
878 		} else
879 			len = 0;
880 		break;
881 	case WAPBL_WC_INODES:
882 		wci = (struct wapbl_wc_inodelist *)&awn;
883 		printf("  count %u clear %u\n",
884 			wci->wc_inocnt, wci->wc_clear);
885 		ph = (blklen - offsetof(struct wapbl_wc_inodelist, wc_inodes))
886 			/ sizeof(wci->wc_inodes[0]);
887 		n = MIN(wci->wc_inocnt, ph);
888 		for (i=0; i<n; ++i) {
889 			if (ISOPT(opt_verbose)) {
890 				printf("  %3d: inumber %10u  imode %08x\n", i,
891 					wci->wc_inodes[i].wc_inumber,
892 					wci->wc_inodes[i].wc_imode);
893 			}
894 		}
895 		break;
896 	default:
897 		printf("\n");
898 		break;
899 	}
900 
901 	return len + blklen;
902 }
903 
904 void
905 usage(void)
906 {
907 
908 	(void)fprintf(stderr, "usage: dumpfs [-acFijmsv] filesys | device [...]\n");
909 	exit(1);
910 }
911 
912 int
913 openpartition(const char *name, int flags, char *device, size_t devicelen)
914 {
915 	char		rawspec[MAXPATHLEN], *p;
916 	struct fstab	*fs;
917 	int		fd, oerrno;
918 
919 	fs = getfsfile(name);
920 	if (fs) {
921 		if ((p = strrchr(fs->fs_spec, '/')) != NULL) {
922 			snprintf(rawspec, sizeof(rawspec), "%.*s/r%s",
923 			    (int)(p - fs->fs_spec), fs->fs_spec, p + 1);
924 			name = rawspec;
925 		} else
926 			name = fs->fs_spec;
927 	}
928 	fd = opendisk(name, flags, device, devicelen, 0);
929 	if (fd == -1 && errno == ENOENT) {
930 		oerrno = errno;
931 		strlcpy(device, name, devicelen);
932 		errno = oerrno;
933 	}
934 	return (fd);
935 }
936