dumpfs.c revision 1.38 1 /* $NetBSD: dumpfs.c,v 1.38 2003/08/30 12:48:11 wiz 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\n\
35 The Regents of the University of California. All rights reserved.\n");
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.38 2003/08/30 12:48:11 wiz Exp $");
43 #endif
44 #endif /* not lint */
45
46 #include <sys/param.h>
47 #include <sys/time.h>
48
49 #include <ufs/ufs/dinode.h>
50 #include <ufs/ufs/ufs_bswap.h>
51 #include <ufs/ffs/fs.h>
52 #include <ufs/ffs/ffs_extern.h>
53
54 #include <err.h>
55 #include <errno.h>
56 #include <fcntl.h>
57 #include <fstab.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
62 #include <util.h>
63
64 union {
65 struct fs fs;
66 char pad[MAXBSIZE];
67 } fsun;
68 #define afs fsun.fs
69
70 union {
71 struct cg cg;
72 char pad[MAXBSIZE];
73 } cgun;
74 #define acg cgun.cg
75
76 #define OPT_FLAG(ch) (1 << ((ch) & 31))
77 #define ISOPT(opt) (opt_flags & (opt))
78 #define opt_superblock OPT_FLAG('s')
79 #define opt_cg_summary OPT_FLAG('m')
80 #define opt_cg_info OPT_FLAG('c')
81 #define opt_inodes OPT_FLAG('i')
82 #define opt_verbose OPT_FLAG('v')
83 #define DFLT_OPTS (opt_superblock | opt_cg_summary | opt_cg_info)
84
85 long dev_bsize = 1;
86 int needswap, printold, is_ufs2;
87 int Fflag;
88
89 uint opt_flags;
90
91 int dumpfs(const char *);
92 int print_superblock(const char *, int, off_t);
93 int print_cgsum(const char *, int);
94 int print_cginfo(const char *, int);
95 int print_inodes(const char *, int);
96 int dumpcg(const char *, int, int);
97 int main(int, char **);
98 int openpartition(const char *, int, char *, size_t);
99 void pbits(int, void *, int);
100 void usage(void);
101 void swap_cg(struct cg *);
102 void print_ufs1_inode(int, int, void *);
103 void print_ufs2_inode(int, int, void *);
104
105 int
106 main(int argc, char *argv[])
107 {
108 int ch, eval;
109
110 while ((ch = getopt(argc, argv, "Fcimsv")) != -1)
111 switch(ch) {
112 case 'c': /* cylinder group info */
113 case 'i': /* actual inodes */
114 case 'm': /* cylinder group summary */
115 case 's': /* superblock */
116 case 'v': /* more verbose */
117 opt_flags |= OPT_FLAG(ch);
118 break;
119 case 'F': /* File (not device) */
120 Fflag = 1;
121 case '?':
122 default:
123 usage();
124 }
125 argc -= optind;
126 argv += optind;
127
128 if (opt_flags == 0)
129 opt_flags = DFLT_OPTS;
130
131 if (argc < 1)
132 usage();
133
134 for (eval = 0; *argv; ++argv) {
135 eval |= dumpfs(*argv);
136 printf("\n");
137 }
138
139 exit(eval);
140 }
141
142
143 int
144 dumpfs(const char *name)
145 {
146 const static off_t sblock_try[] = SBLOCKSEARCH;
147 char device[MAXPATHLEN];
148 int fd, i;
149 int rval = 1;
150
151 if (Fflag)
152 fd = open(name, O_RDONLY);
153 else {
154 fd = openpartition(name, O_RDONLY, device, sizeof(device));
155 name = device;
156 }
157 if (fd == -1)
158 goto err;
159
160 for (i = 0; ; i++) {
161 if (sblock_try[i] == -1) {
162 warnx("%s: could not find superblock, skipped", name);
163 return 1;
164 }
165 if (lseek(fd, sblock_try[i], SEEK_SET) == (off_t)-1)
166 continue;
167 if (read(fd, &afs, SBLOCKSIZE) != SBLOCKSIZE)
168 continue;
169 switch(afs.fs_magic) {
170 case FS_UFS2_MAGIC:
171 is_ufs2 = 1;
172 break;
173 case FS_UFS1_MAGIC:
174 break;
175 case FS_UFS2_MAGIC_SWAPPED:
176 is_ufs2 = 1;
177 needswap = 1;
178 break;
179 case FS_UFS1_MAGIC_SWAPPED:
180 needswap = 1;
181 break;
182 default:
183 continue;
184 }
185 break;
186 }
187
188 if (needswap)
189 ffs_sb_swap(&afs, &afs);
190
191 printold = (afs.fs_magic == FS_UFS1_MAGIC &&
192 (afs.fs_old_flags & FS_FLAGS_UPDATED) == 0);
193 if (printold) {
194 afs.fs_flags = afs.fs_old_flags;
195 afs.fs_maxbsize = afs.fs_bsize;
196 afs.fs_time = afs.fs_old_time;
197 afs.fs_size = afs.fs_old_size;
198 afs.fs_dsize = afs.fs_old_dsize;
199 afs.fs_csaddr = afs.fs_old_csaddr;
200 afs.fs_cstotal.cs_ndir = afs.fs_old_cstotal.cs_ndir;
201 afs.fs_cstotal.cs_nbfree = afs.fs_old_cstotal.cs_nbfree;
202 afs.fs_cstotal.cs_nifree = afs.fs_old_cstotal.cs_nifree;
203 afs.fs_cstotal.cs_nffree = afs.fs_old_cstotal.cs_nffree;
204 }
205
206 if (printold && afs.fs_old_postblformat == FS_42POSTBLFMT)
207 afs.fs_old_nrpos = 8;
208 dev_bsize = afs.fs_fsize / fsbtodb(&afs, 1);
209
210 rval = 0;
211 printf("file system: %s\n", name);
212
213 if (ISOPT(opt_superblock))
214 rval = print_superblock(name, fd, sblock_try[i]);
215 if (rval == 0 && ISOPT(opt_cg_summary))
216 rval = print_cgsum(name, fd);
217 if (rval == 0 && ISOPT(opt_cg_info))
218 rval = print_cginfo(name, fd);
219 if (rval == 0 && ISOPT(opt_inodes))
220 rval = print_inodes(name, fd);
221
222 err:
223 if (fd != -1)
224 (void)close(fd);
225 if (rval)
226 warn("%s", name);
227 return rval;
228 }
229
230 int
231 print_superblock(const char *name, int fd, off_t sblock)
232 {
233 int i, size;
234 time_t t;
235
236 #if BYTE_ORDER == LITTLE_ENDIAN
237 if (needswap)
238 #else
239 if (!needswap)
240 #endif
241 printf("endian\tbig-endian\n");
242 else
243 printf("endian\tlittle-endian\n");
244 t = afs.fs_time;
245 printf("location%lld\tmagic\t%x\ttime\t%s", (long long)sblock,
246 afs.fs_magic, ctime(&t));
247
248 if (is_ufs2)
249 i = 4;
250 else {
251 i = 0;
252 if (!printold && afs.fs_old_postblformat != FS_42POSTBLFMT) {
253 i++;
254 if (afs.fs_old_inodefmt >= FS_44INODEFMT) {
255 int max;
256
257 i++;
258 max = afs.fs_maxcontig;
259 size = afs.fs_contigsumsize;
260 if ((max < 2 && size == 0)
261 || (max > 1 && size >= MIN(max, FS_MAXCONTIG)))
262 i++;
263 }
264 }
265 }
266 printf("id\t[ %x %x ]\n", afs.fs_id[0], afs.fs_id[1]);
267 printf("cylgrp\t%s\tinodes\t%s\tfslevel %d\tsoftdep %sabled\n",
268 i < 1 ? "static" : "dynamic",
269 i < 2 ? "4.2/4.3BSD" : i < 4 ? "4.4BSD" : "FFSv2", i,
270 (afs.fs_flags & FS_DOSOFTDEP) ? "en" : "dis");
271 printf("nbfree\t%lld\tndir\t%lld\tnifree\t%lld\tnffree\t%lld\n",
272 (long long)afs.fs_cstotal.cs_nbfree,
273 (long long)afs.fs_cstotal.cs_ndir,
274 (long long)afs.fs_cstotal.cs_nifree,
275 (long long)afs.fs_cstotal.cs_nffree);
276 printf("ncg\t%d\tsize\t%lld\tblocks\t%lld\n",
277 afs.fs_ncg, (long long)afs.fs_size, (long long)afs.fs_dsize);
278 if (printold)
279 printf("ncyl\t%d\n", afs.fs_old_ncyl);
280 printf("bsize\t%d\tshift\t%d\tmask\t0x%08x\n",
281 afs.fs_bsize, afs.fs_bshift, afs.fs_bmask);
282 printf("fsize\t%d\tshift\t%d\tmask\t0x%08x\n",
283 afs.fs_fsize, afs.fs_fshift, afs.fs_fmask);
284 printf("frag\t%d\tshift\t%d\tfsbtodb\t%d\n",
285 afs.fs_frag, afs.fs_fragshift, afs.fs_fsbtodb);
286 printf("bpg\t%d\tfpg\t%d\tipg\t%d\n",
287 afs.fs_fpg / afs.fs_frag, afs.fs_fpg, afs.fs_ipg);
288 printf("minfree\t%d%%\toptim\t%s\tmaxcontig %d\tmaxbpg\t%d\n",
289 afs.fs_minfree, afs.fs_optim == FS_OPTSPACE ? "space" : "time",
290 afs.fs_maxcontig, afs.fs_maxbpg);
291 if (printold) {
292 printf("cpg\t%d\trotdelay %dms\trps\t%d\n",
293 afs.fs_old_cpg, afs.fs_old_rotdelay, afs.fs_old_rps);
294 printf("ntrak\t%d\tnsect\t%d\tnpsect\t%d\tspc\t%d\n",
295 afs.fs_spare2, afs.fs_old_nsect, afs.fs_old_npsect,
296 afs.fs_old_spc);
297 printf("trackskew %d\tinterleave %d\n",
298 afs.fs_old_trackskew, afs.fs_old_interleave);
299 }
300 printf("symlinklen %d\tcontigsumsize %d\n",
301 afs.fs_maxsymlinklen, afs.fs_contigsumsize);
302 printf("maxfilesize 0x%016llx\n",
303 (unsigned long long)afs.fs_maxfilesize);
304 printf("nindir\t%d\tinopb\t%d\n", afs.fs_nindir, afs.fs_inopb);
305 if (printold)
306 printf("nspf\t%d\n", afs.fs_old_nspf);
307 printf("avgfilesize %d\tavgfpdir %d\n",
308 afs.fs_avgfilesize, afs.fs_avgfpdir);
309 printf("sblkno\t%d\tcblkno\t%d\tiblkno\t%d\tdblkno\t%d\n",
310 afs.fs_sblkno, afs.fs_cblkno, afs.fs_iblkno, afs.fs_dblkno);
311 printf("sbsize\t%d\tcgsize\t%d\n", afs.fs_sbsize, afs.fs_cgsize);
312 if (printold)
313 printf("offset\t%d\tmask\t0x%08x\n",
314 afs.fs_old_cgoffset, afs.fs_old_cgmask);
315 printf("csaddr\t%lld\tcssize %d\n",
316 (long long)afs.fs_csaddr, afs.fs_cssize);
317 if (printold)
318 printf("shift\t%d\tmask\t0x%08x\n",
319 afs.fs_spare1[0], afs.fs_spare1[1]);
320 printf("cgrotor\t%d\tfmod\t%d\tronly\t%d\tclean\t0x%02x\n",
321 afs.fs_cgrotor, afs.fs_fmod, afs.fs_ronly, afs.fs_clean);
322 if (printold) {
323 if (afs.fs_old_cpc != 0)
324 printf("blocks available in each of %d rotational "
325 "positions", afs.fs_old_nrpos);
326 else
327 printf("(no rotational position table)\n");
328 }
329
330 return 0;
331 }
332
333 int
334 print_cgsum(const char *name, int fd)
335 {
336 struct csum *ccsp;
337 int i, j, size;
338
339 afs.fs_csp = calloc(1, afs.fs_cssize);
340 for (i = 0, j = 0; i < afs.fs_cssize; i += afs.fs_bsize, j++) {
341 size = afs.fs_cssize - i < afs.fs_bsize ?
342 afs.fs_cssize - i : afs.fs_bsize;
343 ccsp = (struct csum *)((char *)afs.fs_csp + i);
344 if (lseek(fd,
345 (off_t)(fsbtodb(&afs, (afs.fs_csaddr + j * afs.fs_frag))) *
346 dev_bsize, SEEK_SET) == (off_t)-1)
347 return 1;
348 if (read(fd, ccsp, size) != size)
349 return 1;
350 if (needswap)
351 ffs_csum_swap(ccsp, ccsp, size);
352 }
353
354 printf("\ncs[].cs_(nbfree,ndir,nifree,nffree):\n\t");
355 for (i = 0; i < afs.fs_ncg; i++) {
356 struct csum *cs = &afs.fs_cs(&afs, i);
357 if (i && i % 4 == 0)
358 printf("\n\t");
359 printf("(%d,%d,%d,%d) ",
360 cs->cs_nbfree, cs->cs_ndir, cs->cs_nifree, cs->cs_nffree);
361 }
362 if (i % 4 == 0)
363 printf("\n");
364
365 if (printold && (afs.fs_old_ncyl % afs.fs_old_cpg)) {
366 printf("cylinders in last group %d\n",
367 i = afs.fs_old_ncyl % afs.fs_old_cpg);
368 printf("blocks in last group %d\n",
369 i * afs.fs_old_spc / (afs.fs_old_nspf << afs.fs_fragshift));
370 }
371 printf("\n");
372 free(afs.fs_csp);
373 afs.fs_csp = NULL;
374
375 return 0;
376 }
377
378 int
379 print_cginfo(const char *name, int fd)
380 {
381 int i;
382
383 for (i = 0; i < afs.fs_ncg; i++)
384 if (dumpcg(name, fd, i))
385 return 1;
386 return 0;
387 }
388
389 int
390 print_inodes(const char *name, int fd)
391 {
392 void *ino_buf = malloc(afs.fs_bsize);
393 void (*print_inode)(int, int, void *);
394 int i, inum;
395
396 if (ino_buf == 0)
397 return 1;
398
399 print_inode = is_ufs2 ? print_ufs2_inode : print_ufs1_inode;
400
401 for (inum = 0; inum < afs.fs_ncg * afs.fs_ipg; inum += afs.fs_inopb) {
402 if (pread(fd, ino_buf, afs.fs_bsize,
403 ino_to_fsba(&afs, inum) * afs.fs_fsize) != afs.fs_bsize)
404 return 1;
405 for (i = 0; i < afs.fs_inopb; i++)
406 print_inode(inum + i, i, ino_buf);
407 }
408
409 free(ino_buf);
410 return 0;
411 }
412
413 int
414 dumpcg(const char *name, int fd, int c)
415 {
416 off_t cur;
417 int i, j;
418 time_t t;
419
420 printf("\ncg %d:\n", c);
421 if ((cur = lseek(fd, (off_t)(fsbtodb(&afs, cgtod(&afs, c))) * dev_bsize,
422 SEEK_SET)) == (off_t)-1)
423 return (1);
424 if (read(fd, &acg, afs.fs_bsize) != afs.fs_bsize) {
425 warnx("%s: error reading cg", name);
426 return (1);
427 }
428 if (needswap)
429 ffs_cg_swap(&acg, &acg, &afs);
430 t = acg.cg_time;
431 printf("magic\t%x\ttell\t%llx\ttime\t%s",
432 afs.fs_old_postblformat == FS_42POSTBLFMT ?
433 ((struct ocg *)&acg)->cg_magic : acg.cg_magic,
434 (long long)cur, ctime(&t));
435 printf("cgx\t%d\tniblk\t%d\tndblk\t%d\n",
436 acg.cg_cgx, acg.cg_niblk, acg.cg_ndblk);
437 printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
438 acg.cg_cs.cs_nbfree, acg.cg_cs.cs_ndir,
439 acg.cg_cs.cs_nifree, acg.cg_cs.cs_nffree);
440 printf("rotor\t%d\tirotor\t%d\tfrotor\t%d\nfrsum",
441 acg.cg_rotor, acg.cg_irotor, acg.cg_frotor);
442 for (i = 1, j = 0; i < afs.fs_frag; i++) {
443 printf("\t%d", acg.cg_frsum[i]);
444 j += i * acg.cg_frsum[i];
445 }
446 printf("\nsum of frsum: %d", j);
447 if (afs.fs_contigsumsize > 0) {
448 for (i = 1; i < afs.fs_contigsumsize; i++) {
449 if ((i - 1) % 8 == 0)
450 printf("\nclusters %d-%d:", i,
451 afs.fs_contigsumsize - 1 < i + 7 ?
452 afs.fs_contigsumsize - 1 : i + 7);
453 printf("\t%d", cg_clustersum(&acg, 0)[i]);
454 }
455 printf("\nclusters size %d and over: %d\n",
456 afs.fs_contigsumsize,
457 cg_clustersum(&acg, 0)[afs.fs_contigsumsize]);
458 printf("clusters free:\t");
459 pbits(0, cg_clustersfree(&acg, 0), acg.cg_nclusterblks);
460 } else
461 printf("\n");
462 printf("iused:\t");
463 pbits(c * afs.fs_ipg, cg_inosused(&acg, 0), afs.fs_ipg);
464 printf("free:\t");
465 pbits(0, cg_blksfree(&acg, 0), afs.fs_fpg);
466 return (0);
467 }
468
469 void
470 print_ufs1_inode(int inum, int i_off, void *ibuf)
471 {
472 struct ufs1_dinode *i = ibuf;
473
474 i += i_off;
475
476 if (inum == 0)
477 printf("\n inode: mode nlink size"
478 " ctime.nsec flags blocks"
479 " generation uid gid\n");
480 if (i->di_mode == 0 && i->di_nlink == 0 && !ISOPT(opt_verbose))
481 return;
482 printf("%8u: %6o %6d %20" PRIu64 " %10u.%09u %8x %10u %8x %10u %10u\n",
483 inum, i->di_mode, i->di_nlink, i->di_size,
484 i->di_ctime, i->di_ctimensec, i->di_flags, i->di_blocks,
485 i->di_gen, i->di_uid, i->di_gid);
486 }
487
488 void
489 print_ufs2_inode(int inum, int i_off, void *ibuf)
490 {
491 struct ufs2_dinode *i = ibuf;
492
493 i += i_off;
494
495 if (inum == 0)
496 printf("\n inode: mode nlink size"
497 " ctime.nsec flags blocks"
498 " generation uid gid\n");
499
500 if (i->di_mode == 0 && i->di_nlink == 0 && !ISOPT(opt_verbose))
501 return;
502
503 printf("%8u: %6o %6d %20" PRIu64 " %10" PRIu64 ".%09u %8x %10" PRIu64 " %8x %10u %10u\n",
504 inum, i->di_mode, i->di_nlink, i->di_size,
505 i->di_ctime, i->di_ctimensec, i->di_flags, i->di_blocks,
506 i->di_gen, i->di_uid, i->di_gid);
507 }
508
509
510 void
511 pbits(int offset, void *vp, int max)
512 {
513 int i;
514 char *p;
515 int count, j;
516
517 for (count = i = 0, p = vp; i < max; i++)
518 if (isset(p, i)) {
519 if (count)
520 printf(",%s", count % 6 ? " " : "\n\t");
521 count++;
522 printf("%d", offset + i);
523 j = i;
524 while ((i+1)<max && isset(p, i+1))
525 i++;
526 if (i != j)
527 printf("-%d", offset + i);
528 }
529 printf("\n");
530 }
531
532 void
533 usage(void)
534 {
535
536 (void)fprintf(stderr, "usage: dumpfs [-cFimsv] filesys | device [...]\n");
537 exit(1);
538 }
539
540 int
541 openpartition(const char *name, int flags, char *device, size_t devicelen)
542 {
543 char rawspec[MAXPATHLEN], *p;
544 struct fstab *fs;
545 int fd, oerrno;
546
547 fs = getfsfile(name);
548 if (fs) {
549 if ((p = strrchr(fs->fs_spec, '/')) != NULL) {
550 snprintf(rawspec, sizeof(rawspec), "%.*s/r%s",
551 (int)(p - fs->fs_spec), fs->fs_spec, p + 1);
552 name = rawspec;
553 } else
554 name = fs->fs_spec;
555 }
556 fd = opendisk(name, flags, device, devicelen, 0);
557 if (fd == -1 && errno == ENOENT) {
558 oerrno = errno;
559 strlcpy(device, name, devicelen);
560 errno = oerrno;
561 }
562 return (fd);
563 }
564