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