1 /* $OpenBSD: dumpfs.c,v 1.39 2024/05/09 08:35:40 florian Exp $ */
2
3 /*
4 * Copyright (c) 2002 Networks Associates Technology, Inc.
5 * All rights reserved.
6 *
7 * This software was developed for the FreeBSD Project by Marshall
8 * Kirk McKusick and Network Associates Laboratories, the Security
9 * Research Division of Network Associates, Inc. under DARPA/SPAWAR
10 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
11 * research program.
12 *
13 * Copyright (c) 1983, 1992, 1993
14 * The Regents of the University of California. All rights reserved.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 */
40
41 #include <sys/param.h> /* DEV_BSIZE MAXBSIZE isset */
42 #include <sys/time.h>
43
44 #include <ufs/ufs/dinode.h>
45 #include <ufs/ffs/fs.h>
46
47 #include <err.h>
48 #include <errno.h>
49 #include <fcntl.h>
50 #include <fstab.h>
51 #include <stdint.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <unistd.h>
55 #include <util.h>
56
57 #define MINIMUM(a, b) (((a) < (b)) ? (a) : (b))
58
59 union {
60 struct fs fs;
61 char pad[MAXBSIZE];
62 } fsun;
63 #define afs fsun.fs
64
65 union {
66 struct cg cg;
67 char pad[MAXBSIZE];
68 } cgun;
69 #define acg cgun.cg
70
71 int dumpfs(int, const char *);
72 int dumpcg(const char *, int, u_int);
73 int marshal(const char *);
74 int open_disk(const char *);
75 void pbits(void *, int);
76 __dead void usage(void);
77
78 int
main(int argc,char * argv[])79 main(int argc, char *argv[])
80 {
81 struct fstab *fs;
82 const char *name;
83 int ch, domarshal, eval, fd;
84
85 domarshal = eval = 0;
86
87 while ((ch = getopt(argc, argv, "m")) != -1) {
88 switch (ch) {
89 case 'm':
90 domarshal = 1;
91 break;
92 default:
93 usage();
94 }
95 }
96 argc -= optind;
97 argv += optind;
98
99 if (argc < 1)
100 usage();
101
102 if (pledge("stdio rpath disklabel", NULL) == -1)
103 err(1, "pledge");
104
105 for (; *argv != NULL; argv++) {
106 if ((fs = getfsfile(*argv)) != NULL)
107 name = fs->fs_spec;
108 else
109 name = *argv;
110 if ((fd = open_disk(name)) == -1) {
111 eval |= 1;
112 continue;
113 }
114 if (domarshal)
115 eval |= marshal(name);
116 else
117 eval |= dumpfs(fd, name);
118 close(fd);
119 }
120 exit(eval);
121 }
122
123 int
open_disk(const char * name)124 open_disk(const char *name)
125 {
126 int fd, i, sbtry[] = SBLOCKSEARCH;
127 ssize_t n;
128
129 /* XXX - should retry w/raw device on failure */
130 if ((fd = opendev(name, O_RDONLY, 0, NULL)) == -1) {
131 warn("%s", name);
132 return(-1);
133 }
134
135 /* Read superblock, could be UFS1 or UFS2. */
136 for (i = 0; sbtry[i] != -1; i++) {
137 n = pread(fd, &afs, SBLOCKSIZE, (off_t)sbtry[i]);
138 if (n == SBLOCKSIZE && (afs.fs_magic == FS_UFS1_MAGIC ||
139 (afs.fs_magic == FS_UFS2_MAGIC &&
140 afs.fs_sblockloc == sbtry[i])) &&
141 !(afs.fs_magic == FS_UFS1_MAGIC &&
142 sbtry[i] == SBLOCK_UFS2) &&
143 afs.fs_bsize <= MAXBSIZE &&
144 afs.fs_bsize >= sizeof(struct fs))
145 break;
146 }
147 if (sbtry[i] == -1) {
148 warnx("cannot find filesystem superblock");
149 close(fd);
150 return (-1);
151 }
152
153 return (fd);
154 }
155
156 int
dumpfs(int fd,const char * name)157 dumpfs(int fd, const char *name)
158 {
159 time_t fstime;
160 int64_t fssize;
161 int32_t fsflags;
162 size_t size;
163 off_t off;
164 int i, j;
165 u_int cg;
166 char *ct;
167
168 switch (afs.fs_magic) {
169 case FS_UFS2_MAGIC:
170 fssize = afs.fs_size;
171 fstime = afs.fs_time;
172 ct = ctime(&fstime);
173 if (ct)
174 printf("magic\t%x (FFS2)\ttime\t%s",
175 afs.fs_magic, ctime(&fstime));
176 else
177 printf("magic\t%x (FFS2)\ttime\t%lld\n",
178 afs.fs_magic, fstime);
179 printf("superblock location\t%jd\tid\t[ %x %x ]\n",
180 (intmax_t)afs.fs_sblockloc, afs.fs_id[0], afs.fs_id[1]);
181 printf("ncg\t%u\tsize\t%jd\tblocks\t%jd\n",
182 afs.fs_ncg, (intmax_t)fssize, (intmax_t)afs.fs_dsize);
183 break;
184 case FS_UFS1_MAGIC:
185 fssize = afs.fs_ffs1_size;
186 fstime = afs.fs_ffs1_time;
187 ct = ctime(&fstime);
188 if (ct)
189 printf("magic\t%x (FFS1)\ttime\t%s",
190 afs.fs_magic, ctime(&fstime));
191 else
192 printf("magic\t%x (FFS1)\ttime\t%lld\n",
193 afs.fs_magic, fstime);
194 printf("id\t[ %x %x ]\n", afs.fs_id[0], afs.fs_id[1]);
195 i = 0;
196 if (afs.fs_postblformat != FS_42POSTBLFMT) {
197 i++;
198 if (afs.fs_inodefmt >= FS_44INODEFMT) {
199 size_t max;
200
201 i++;
202 max = afs.fs_maxcontig;
203 size = afs.fs_contigsumsize;
204 if ((max < 2 && size == 0) ||
205 (max > 1 && size >= MINIMUM(max, FS_MAXCONTIG)))
206 i++;
207 }
208 }
209 printf("cylgrp\t%s\tinodes\t%s\tfslevel %d\n",
210 i < 1 ? "static" : "dynamic",
211 i < 2 ? "4.2/4.3BSD" : "4.4BSD", i);
212 printf("ncg\t%u\tncyl\t%d\tsize\t%d\tblocks\t%d\n",
213 afs.fs_ncg, afs.fs_ncyl, afs.fs_ffs1_size, afs.fs_ffs1_dsize);
214 break;
215 default:
216 goto err;
217 }
218 printf("bsize\t%d\tshift\t%d\tmask\t0x%08x\n",
219 afs.fs_bsize, afs.fs_bshift, afs.fs_bmask);
220 printf("fsize\t%d\tshift\t%d\tmask\t0x%08x\n",
221 afs.fs_fsize, afs.fs_fshift, afs.fs_fmask);
222 printf("frag\t%d\tshift\t%d\tfsbtodb\t%d\n",
223 afs.fs_frag, afs.fs_fragshift, afs.fs_fsbtodb);
224 printf("minfree\t%d%%\toptim\t%s\tsymlinklen %d\n",
225 afs.fs_minfree, afs.fs_optim == FS_OPTSPACE ? "space" : "time",
226 afs.fs_maxsymlinklen);
227 switch (afs.fs_magic) {
228 case FS_UFS2_MAGIC:
229 printf("%s %d\tmaxbpg\t%d\tmaxcontig %d\tcontigsumsize %d\n",
230 "maxbsize", afs.fs_maxbsize, afs.fs_maxbpg,
231 afs.fs_maxcontig, afs.fs_contigsumsize);
232 printf("nbfree\t%jd\tndir\t%jd\tnifree\t%jd\tnffree\t%jd\n",
233 (intmax_t)afs.fs_cstotal.cs_nbfree,
234 (intmax_t)afs.fs_cstotal.cs_ndir,
235 (intmax_t)afs.fs_cstotal.cs_nifree,
236 (intmax_t)afs.fs_cstotal.cs_nffree);
237 printf("bpg\t%d\tfpg\t%d\tipg\t%u\n",
238 afs.fs_fpg / afs.fs_frag, afs.fs_fpg, afs.fs_ipg);
239 printf("nindir\t%d\tinopb\t%u\tmaxfilesize\t%ju\n",
240 afs.fs_nindir, afs.fs_inopb,
241 (uintmax_t)afs.fs_maxfilesize);
242 printf("sbsize\t%d\tcgsize\t%d\tcsaddr\t%jd\tcssize\t%d\n",
243 afs.fs_sbsize, afs.fs_cgsize, (intmax_t)afs.fs_csaddr,
244 afs.fs_cssize);
245 break;
246 case FS_UFS1_MAGIC:
247 printf("maxbpg\t%d\tmaxcontig %d\tcontigsumsize %d\n",
248 afs.fs_maxbpg, afs.fs_maxcontig, afs.fs_contigsumsize);
249 printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
250 afs.fs_ffs1_cstotal.cs_nbfree, afs.fs_ffs1_cstotal.cs_ndir,
251 afs.fs_ffs1_cstotal.cs_nifree, afs.fs_ffs1_cstotal.cs_nffree);
252 printf("cpg\t%d\tbpg\t%d\tfpg\t%d\tipg\t%u\n",
253 afs.fs_cpg, afs.fs_fpg / afs.fs_frag, afs.fs_fpg,
254 afs.fs_ipg);
255 printf("nindir\t%d\tinopb\t%u\tnspf\t%d\tmaxfilesize\t%ju\n",
256 afs.fs_nindir, afs.fs_inopb, afs.fs_nspf,
257 (uintmax_t)afs.fs_maxfilesize);
258 printf("sbsize\t%d\tcgsize\t%d\tcgoffset %d\tcgmask\t0x%08x\n",
259 afs.fs_sbsize, afs.fs_cgsize, afs.fs_cgoffset,
260 afs.fs_cgmask);
261 printf("csaddr\t%d\tcssize\t%d\n",
262 afs.fs_ffs1_csaddr, afs.fs_cssize);
263 printf("rotdelay %dms\trps\t%d\tinterleave %d\n",
264 afs.fs_rotdelay, afs.fs_rps, afs.fs_interleave);
265 printf("nsect\t%d\tnpsect\t%d\tspc\t%d\n",
266 afs.fs_nsect, afs.fs_npsect, afs.fs_spc);
267 break;
268 default:
269 goto err;
270 }
271 printf("sblkno\t%d\tcblkno\t%d\tiblkno\t%d\tdblkno\t%d\n",
272 afs.fs_sblkno, afs.fs_cblkno, afs.fs_iblkno, afs.fs_dblkno);
273 printf("cgrotor\t%d\tfmod\t%d\tronly\t%d\tclean\t%d\n",
274 afs.fs_cgrotor, afs.fs_fmod, afs.fs_ronly, afs.fs_clean);
275 printf("avgfpdir %u\tavgfilesize %u\n",
276 afs.fs_avgfpdir, afs.fs_avgfilesize);
277 printf("flags\t");
278 if (afs.fs_magic == FS_UFS2_MAGIC ||
279 afs.fs_ffs1_flags & FS_FLAGS_UPDATED)
280 fsflags = afs.fs_flags;
281 else
282 fsflags = afs.fs_ffs1_flags;
283 if (fsflags == 0)
284 printf("none");
285 if (fsflags & FS_UNCLEAN)
286 printf("unclean ");
287 if (fsflags & FS_FLAGS_UPDATED)
288 printf("updated ");
289 #if 0
290 fsflags &= ~(FS_UNCLEAN | FS_FLAGS_UPDATED);
291 if (fsflags != 0)
292 printf("unknown flags (%#x)", fsflags);
293 #endif
294 putchar('\n');
295 printf("fsmnt\t%s\n", afs.fs_fsmnt);
296 printf("volname\t%s\tswuid\t%ju\n",
297 afs.fs_volname, (uintmax_t)afs.fs_swuid);
298 printf("\ncs[].cs_(nbfree,ndir,nifree,nffree):\n\t");
299 afs.fs_csp = calloc(1, afs.fs_cssize);
300 for (i = 0, j = 0; i < afs.fs_cssize; i += afs.fs_bsize, j++) {
301 size = afs.fs_cssize - i < afs.fs_bsize ?
302 afs.fs_cssize - i : afs.fs_bsize;
303 off = (off_t)(fsbtodb(&afs, (afs.fs_csaddr + j *
304 afs.fs_frag))) * DEV_BSIZE;
305 if (pread(fd, (char *)afs.fs_csp + i, size, off) != size)
306 goto err;
307 }
308 for (cg = 0; cg < afs.fs_ncg; cg++) {
309 struct csum *cs = &afs.fs_cs(&afs, cg);
310 if (i && i % 4 == 0)
311 printf("\n\t");
312 printf("(%d,%d,%d,%d) ",
313 cs->cs_nbfree, cs->cs_ndir, cs->cs_nifree, cs->cs_nffree);
314 }
315 printf("\n");
316 if (fssize % afs.fs_fpg) {
317 if (afs.fs_magic == FS_UFS1_MAGIC)
318 printf("cylinders in last group %d\n",
319 howmany(afs.fs_ffs1_size % afs.fs_fpg,
320 afs.fs_spc / afs.fs_nspf));
321 printf("blocks in last group %ld\n\n",
322 (long)((fssize % afs.fs_fpg) / afs.fs_frag));
323 }
324 for (cg = 0; cg < afs.fs_ncg; cg++)
325 if (dumpcg(name, fd, cg))
326 goto err;
327 return (0);
328
329 err: warn("%s", name);
330 return (1);
331 }
332
333 int
dumpcg(const char * name,int fd,u_int c)334 dumpcg(const char *name, int fd, u_int c)
335 {
336 time_t cgtime;
337 off_t cur;
338 int i, j;
339 char *ct;
340
341 printf("\ncg %u:\n", c);
342 cur = (off_t)fsbtodb(&afs, cgtod(&afs, c)) * DEV_BSIZE;
343 if (pread(fd, &acg, afs.fs_bsize, cur) != afs.fs_bsize) {
344 warn("%s: error reading cg", name);
345 return(1);
346 }
347 switch (afs.fs_magic) {
348 case FS_UFS2_MAGIC:
349 cgtime = acg.cg_ffs2_time;
350 ct = ctime(&cgtime);
351 if (ct)
352 printf("magic\t%x\ttell\t%jx\ttime\t%s",
353 acg.cg_magic, (intmax_t)cur, ct);
354 else
355 printf("magic\t%x\ttell\t%jx\ttime\t%lld\n",
356 acg.cg_magic, (intmax_t)cur, cgtime);
357 printf("cgx\t%u\tndblk\t%u\tniblk\t%u\tinitiblk %u\n",
358 acg.cg_cgx, acg.cg_ndblk, acg.cg_ffs2_niblk,
359 acg.cg_initediblk);
360 break;
361 case FS_UFS1_MAGIC:
362 cgtime = acg.cg_time;
363 ct = ctime(&cgtime);
364 if (ct)
365 printf("magic\t%x\ttell\t%jx\ttime\t%s",
366 afs.fs_postblformat == FS_42POSTBLFMT ?
367 ((struct ocg *)&acg)->cg_magic : acg.cg_magic,
368 (intmax_t)cur, ct);
369 else
370 printf("magic\t%x\ttell\t%jx\ttime\t%lld\n",
371 afs.fs_postblformat == FS_42POSTBLFMT ?
372 ((struct ocg *)&acg)->cg_magic : acg.cg_magic,
373 (intmax_t)cur, cgtime);
374 printf("cgx\t%u\tncyl\t%d\tniblk\t%d\tndblk\t%u\n",
375 acg.cg_cgx, acg.cg_ncyl, acg.cg_niblk, acg.cg_ndblk);
376 break;
377 default:
378 break;
379 }
380 printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
381 acg.cg_cs.cs_nbfree, acg.cg_cs.cs_ndir,
382 acg.cg_cs.cs_nifree, acg.cg_cs.cs_nffree);
383 printf("rotor\t%u\tirotor\t%u\tfrotor\t%u\nfrsum",
384 acg.cg_rotor, acg.cg_irotor, acg.cg_frotor);
385 for (i = 1, j = 0; i < afs.fs_frag; i++) {
386 printf("\t%u", acg.cg_frsum[i]);
387 j += i * acg.cg_frsum[i];
388 }
389 printf("\nsum of frsum: %d", j);
390 if (afs.fs_contigsumsize > 0) {
391 for (i = 1; i < afs.fs_contigsumsize; i++) {
392 if ((i - 1) % 8 == 0)
393 printf("\nclusters %d-%d:", i,
394 afs.fs_contigsumsize - 1 < i + 7 ?
395 afs.fs_contigsumsize - 1 : i + 7);
396 printf("\t%d", cg_clustersum(&acg)[i]);
397 }
398 printf("\nclusters size %d and over: %d\n",
399 afs.fs_contigsumsize,
400 cg_clustersum(&acg)[afs.fs_contigsumsize]);
401 printf("clusters free:\t");
402 pbits(cg_clustersfree(&acg), acg.cg_nclusterblks);
403 } else
404 printf("\n");
405 printf("inodes used:\t");
406 pbits(cg_inosused(&acg), afs.fs_ipg);
407 printf("blks free:\t");
408 pbits(cg_blksfree(&acg), afs.fs_fpg);
409 #if 0
410 /* XXX - keep this? */
411 if (afs.fs_magic == FS_UFS1_MAGIC) {
412 printf("b:\n");
413 for (i = 0; i < afs.fs_cpg; i++) {
414 if (cg_blktot(&acg)[i] == 0)
415 continue;
416 printf(" c%d:\t(%d)\t", i, cg_blktot(&acg)[i]);
417 printf("\n");
418 }
419 }
420 #endif
421 return (0);
422 }
423
424 int
marshal(const char * name)425 marshal(const char *name)
426 {
427 int Oflag;
428
429 printf("# newfs command for %s\n", name);
430 printf("newfs ");
431 if (afs.fs_volname[0] != '\0')
432 printf("-L %s ", afs.fs_volname);
433
434 Oflag = (afs.fs_magic == FS_UFS2_MAGIC) +
435 (afs.fs_inodefmt == FS_44INODEFMT);
436 printf("-O %d ", Oflag);
437 printf("-b %d ", afs.fs_bsize);
438 /* -c unimplemented */
439 printf("-e %d ", afs.fs_maxbpg);
440 printf("-f %d ", afs.fs_fsize);
441 printf("-g %u ", afs.fs_avgfilesize);
442 printf("-h %u ", afs.fs_avgfpdir);
443 /* -i unimplemented */
444 printf("-m %d ", afs.fs_minfree);
445 printf("-o ");
446 switch (afs.fs_optim) {
447 case FS_OPTSPACE:
448 printf("space ");
449 break;
450 case FS_OPTTIME:
451 printf("time ");
452 break;
453 default:
454 printf("unknown ");
455 break;
456 }
457 /* -S unimplemented */
458 printf("-s %jd ", (intmax_t)afs.fs_size * (afs.fs_fsize / DEV_BSIZE));
459 printf("%s ", name);
460 printf("\n");
461
462 return 0;
463 }
464
465 void
pbits(void * vp,int max)466 pbits(void *vp, int max)
467 {
468 int i;
469 char *p;
470 int count, j;
471
472 for (count = i = 0, p = vp; i < max; i++)
473 if (isset(p, i)) {
474 if (count)
475 printf(",%s", count % 6 ? " " : "\n\t");
476 count++;
477 printf("%d", i);
478 j = i;
479 while ((i+1)<max && isset(p, i+1))
480 i++;
481 if (i != j)
482 printf("-%d", i);
483 }
484 printf("\n");
485 }
486
487 __dead void
usage(void)488 usage(void)
489 {
490 (void)fprintf(stderr, "usage: dumpfs [-m] filesys | device\n");
491 exit(1);
492 }
493