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