Deleted Added
full compact
dumpfs.c (110174) dumpfs.c (110884)
1/*
2 * Copyright (c) 2002 Networks Associates Technology, Inc.
3 * All rights reserved.
4 *
5 * This software was developed for the FreeBSD Project by Marshall
6 * Kirk McKusick and Network Associates Laboratories, the Security
7 * Research Division of Network Associates, Inc. under DARPA/SPAWAR
8 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
1/*
2 * Copyright (c) 2002 Networks Associates Technology, Inc.
3 * All rights reserved.
4 *
5 * This software was developed for the FreeBSD Project by Marshall
6 * Kirk McKusick and Network Associates Laboratories, the Security
7 * Research Division of Network Associates, Inc. under DARPA/SPAWAR
8 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
9 * research program
9 * research program.
10 *
10 *
11 * Copyright (c) 1982, 1989, 1993
12 * The Regents of the University of California. All rights reserved.
13 * (c) UNIX System Laboratories, Inc.
14 * Copyright (c) 1983, 1992, 1993
15 * The Regents of the University of California. All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. All advertising materials mentioning features or use of this software
26 * must display the following acknowledgement:
27 * This product includes software developed by the University of
28 * California, Berkeley and its contributors.
29 * 4. Neither the name of the University nor the names of its contributors
30 * may be used to endorse or promote products derived from this software
31 * without specific prior written permission.
32 *
33 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
34 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * SUCH DAMAGE.
44 */
45
46#ifndef lint
47static const char copyright[] =
48"@(#) Copyright (c) 1983, 1992, 1993\n\
49 The Regents of the University of California. All rights reserved.\n";
50#endif /* not lint */
51
52#ifndef lint
53#if 0
54static char sccsid[] = "@(#)dumpfs.c 8.5 (Berkeley) 4/29/95";
55#endif
56static const char rcsid[] =
11 * Copyright (c) 1983, 1992, 1993
12 * The Regents of the University of California. All rights reserved.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the University of
25 * California, Berkeley and its contributors.
26 * 4. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
41 */
42
43#ifndef lint
44static const char copyright[] =
45"@(#) Copyright (c) 1983, 1992, 1993\n\
46 The Regents of the University of California. All rights reserved.\n";
47#endif /* not lint */
48
49#ifndef lint
50#if 0
51static char sccsid[] = "@(#)dumpfs.c 8.5 (Berkeley) 4/29/95";
52#endif
53static const char rcsid[] =
57 "$FreeBSD: head/sbin/dumpfs/dumpfs.c 110174 2003-02-01 04:17:10Z gordon $";
54 "$FreeBSD: head/sbin/dumpfs/dumpfs.c 110884 2003-02-14 21:08:14Z mckusick $";
58#endif /* not lint */
59
60#include <sys/param.h>
61#include <sys/time.h>
62#include <sys/disklabel.h>
63
64#include <ufs/ufs/dinode.h>
65#include <ufs/ffs/fs.h>
66
67#include <err.h>
68#include <errno.h>
69#include <fcntl.h>
70#include <fstab.h>
71#include <libufs.h>
72#include <stdint.h>
73#include <stdio.h>
74#include <stdlib.h>
75#include <unistd.h>
76
77#define afs disk.d_fs
78#define acg disk.d_cg
79
80struct uufsd disk;
81
82int dumpfs(const char *);
83int dumpcg(void);
84int marshal(const char *);
85void pbits(void *, int);
86void ufserr(const char *);
87void usage(void) __dead2;
88
89int
90main(int argc, char *argv[])
91{
92 const char *name;
93 int ch, domarshal, eval;
94
95 domarshal = eval = 0;
96
97 while ((ch = getopt(argc, argv, "m")) != -1) {
98 switch (ch) {
99 case 'm':
100 domarshal = 1;
101 break;
102 case '?':
103 default:
104 usage();
105 }
106 }
107 argc -= optind;
108 argv += optind;
109
110 if (argc < 1)
111 usage();
112
113 while ((name = *argv++) != NULL) {
114 if (ufs_disk_fillout(&disk, name) == -1) {
115 ufserr(name);
116 eval |= 1;
117 continue;
118 }
119 if (domarshal)
120 eval |= marshal(name);
121 else
122 eval |= dumpfs(name);
123 ufs_disk_close(&disk);
124 }
125 exit(eval);
126}
127
128int
129dumpfs(const char *name)
130{
131 time_t fstime;
132 int64_t fssize;
133 int32_t fsflags;
134 int i;
135
136 switch (disk.d_ufs) {
137 case 2:
138 fssize = afs.fs_size;
139 fstime = afs.fs_time;
140 printf("magic\t%x (UFS2)\ttime\t%s",
141 afs.fs_magic, ctime(&fstime));
142 printf("superblock location\t%qd\tid\t[ %x %x ]\n",
143 afs.fs_sblockloc, afs.fs_id[0], afs.fs_id[1]);
144 printf("ncg\t%d\tsize\t%qd\tblocks\t%jd\n",
145 afs.fs_ncg, fssize, (intmax_t)afs.fs_dsize);
146 break;
147 case 1:
148 fssize = afs.fs_old_size;
149 fstime = afs.fs_old_time;
150 printf("magic\t%x (UFS1)\ttime\t%s",
151 afs.fs_magic, ctime(&fstime));
152 printf("id\t[ %x %x ]\n", afs.fs_id[0], afs.fs_id[1]);
153 printf("ncg\t%d\tsize\t%qd\tblocks\t%jd\n",
154 afs.fs_ncg, fssize, (intmax_t)afs.fs_dsize);
155 break;
156 default:
157 goto err;
158 }
159 printf("bsize\t%d\tshift\t%d\tmask\t0x%08x\n",
160 afs.fs_bsize, afs.fs_bshift, afs.fs_bmask);
161 printf("fsize\t%d\tshift\t%d\tmask\t0x%08x\n",
162 afs.fs_fsize, afs.fs_fshift, afs.fs_fmask);
163 printf("frag\t%d\tshift\t%d\tfsbtodb\t%d\n",
164 afs.fs_frag, afs.fs_fragshift, afs.fs_fsbtodb);
165 printf("minfree\t%d%%\toptim\t%s\tsymlinklen %d\n",
166 afs.fs_minfree, afs.fs_optim == FS_OPTSPACE ? "space" : "time",
167 afs.fs_maxsymlinklen);
168 switch (disk.d_ufs) {
169 case 2:
170 printf("%s %d\tmaxbpg\t%d\tmaxcontig %d\tcontigsumsize %d\n",
171 "maxbsize", afs.fs_maxbsize, afs.fs_maxbpg,
172 afs.fs_maxcontig, afs.fs_contigsumsize);
173 printf("nbfree\t%qd\tndir\t%qd\tnifree\t%qd\tnffree\t%qd\n",
174 afs.fs_cstotal.cs_nbfree, afs.fs_cstotal.cs_ndir,
175 afs.fs_cstotal.cs_nifree, afs.fs_cstotal.cs_nffree);
176 printf("bpg\t%d\tfpg\t%d\tipg\t%d\n",
177 afs.fs_fpg / afs.fs_frag, afs.fs_fpg, afs.fs_ipg);
178 printf("nindir\t%d\tinopb\t%d\tmaxfilesize\t%qu\n",
179 afs.fs_nindir, afs.fs_inopb, afs.fs_maxfilesize);
180 printf("sbsize\t%d\tcgsize\t%d\tcsaddr\t%jd\tcssize\t%d\n",
181 afs.fs_sbsize, afs.fs_cgsize, (intmax_t)afs.fs_csaddr,
182 afs.fs_cssize);
183 break;
184 case 1:
185 printf("maxbpg\t%d\tmaxcontig %d\tcontigsumsize %d\n",
186 afs.fs_maxbpg, afs.fs_maxcontig, afs.fs_contigsumsize);
187 printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
188 afs.fs_old_cstotal.cs_nbfree, afs.fs_old_cstotal.cs_ndir,
189 afs.fs_old_cstotal.cs_nifree, afs.fs_old_cstotal.cs_nffree);
190 printf("cpg\t%d\tbpg\t%d\tfpg\t%d\tipg\t%d\n",
191 afs.fs_old_cpg, afs.fs_fpg / afs.fs_frag, afs.fs_fpg,
192 afs.fs_ipg);
193 printf("nindir\t%d\tinopb\t%d\tnspf\t%d\tmaxfilesize\t%qu\n",
194 afs.fs_nindir, afs.fs_inopb, afs.fs_old_nspf,
195 afs.fs_maxfilesize);
196 printf("sbsize\t%d\tcgsize\t%d\tcgoffset %d\tcgmask\t0x%08x\n",
197 afs.fs_sbsize, afs.fs_cgsize, afs.fs_old_cgoffset,
198 afs.fs_old_cgmask);
199 printf("csaddr\t%d\tcssize\t%d\n",
200 afs.fs_old_csaddr, afs.fs_cssize);
201 printf("rotdelay %dms\trps\t%d\ttrackskew %d\tinterleave %d\n",
202 afs.fs_old_rotdelay, afs.fs_old_rps, afs.fs_old_trackskew,
203 afs.fs_old_interleave);
204 printf("nsect\t%d\tnpsect\t%d\tspc\t%d\n",
205 afs.fs_old_nsect, afs.fs_old_npsect, afs.fs_old_spc);
206 break;
207 default:
208 goto err;
209 }
210 printf("sblkno\t%d\tcblkno\t%d\tiblkno\t%d\tdblkno\t%d\n",
211 afs.fs_sblkno, afs.fs_cblkno, afs.fs_iblkno, afs.fs_dblkno);
212 printf("cgrotor\t%d\tfmod\t%d\tronly\t%d\tclean\t%d\n",
213 afs.fs_cgrotor, afs.fs_fmod, afs.fs_ronly, afs.fs_clean);
214 printf("flags\t");
215 if (afs.fs_old_flags & FS_FLAGS_UPDATED)
216 fsflags = afs.fs_flags;
217 else
218 fsflags = afs.fs_old_flags;
219 if (fsflags == 0)
220 printf("none");
221 if (fsflags & FS_UNCLEAN)
222 printf("unclean ");
223 if (fsflags & FS_DOSOFTDEP)
224 printf("soft-updates ");
225 if (fsflags & FS_NEEDSFSCK)
226 printf("needs fsck run ");
227 if (fsflags & FS_INDEXDIRS)
228 printf("indexed directories ");
229 if (fsflags & FS_ACLS)
230 printf("acls ");
231 if (fsflags & FS_MULTILABEL)
232 printf("multilabel ");
233 if (fsflags & FS_FLAGS_UPDATED)
234 printf("fs_flags expanded ");
235 fsflags &= ~(FS_UNCLEAN | FS_DOSOFTDEP | FS_NEEDSFSCK | FS_INDEXDIRS |
236 FS_ACLS | FS_MULTILABEL | FS_FLAGS_UPDATED);
237 if (fsflags != 0)
238 printf("unknown flags (%#x)", fsflags);
239 putchar('\n');
240 printf("fsmnt\t%s\n", afs.fs_fsmnt);
241 printf("volname\t%s\tswuid\t%qu\n",
242 afs.fs_volname, afs.fs_swuid);
243 printf("\ncs[].cs_(nbfree,ndir,nifree,nffree):\n\t");
244 afs.fs_csp = calloc(1, afs.fs_cssize);
245 if (bread(&disk, fsbtodb(&afs, afs.fs_csaddr), afs.fs_csp, afs.fs_cssize) == -1)
246 goto err;
247 for (i = 0; i < afs.fs_ncg; i++) {
248 struct csum *cs = &afs.fs_cs(&afs, i);
249 if (i && i % 4 == 0)
250 printf("\n\t");
251 printf("(%d,%d,%d,%d) ",
252 cs->cs_nbfree, cs->cs_ndir, cs->cs_nifree, cs->cs_nffree);
253 }
254 printf("\n");
255 if (fssize % afs.fs_fpg) {
256 if (disk.d_ufs == 1)
257 printf("cylinders in last group %d\n",
258 howmany(afs.fs_old_size % afs.fs_fpg,
259 afs.fs_old_spc / afs.fs_old_nspf));
260 printf("blocks in last group %ld\n\n",
261 (long)((fssize % afs.fs_fpg) / afs.fs_frag));
262 }
263 while ((i = cgread(&disk)) != 0) {
264 if (i == -1 || dumpcg())
265 goto err;
266 }
267 return (0);
268
269err: ufserr(name);
270 return (1);
271}
272
273int
274dumpcg(void)
275{
276 time_t cgtime;
277 off_t cur;
278 int i, j;
279
280 printf("\ncg %d:\n", disk.d_lcg);
281 cur = fsbtodb(&afs, cgtod(&afs, disk.d_lcg)) * disk.d_bsize;
282 switch (disk.d_ufs) {
283 case 2:
284 cgtime = acg.cg_time;
285 printf("magic\t%x\ttell\t%qx\ttime\t%s",
286 acg.cg_magic, cur, ctime(&cgtime));
287 printf("cgx\t%d\tndblk\t%d\tniblk\t%d\tinitiblk %d\n",
288 acg.cg_cgx, acg.cg_ndblk, acg.cg_niblk, acg.cg_initediblk);
289 break;
290 case 1:
291 cgtime = acg.cg_old_time;
292 printf("magic\t%x\ttell\t%qx\ttime\t%s",
293 acg.cg_magic, cur, ctime(&cgtime));
294 printf("cgx\t%d\tncyl\t%d\tniblk\t%d\tndblk\t%d\n",
295 acg.cg_cgx, acg.cg_old_ncyl, acg.cg_old_niblk,
296 acg.cg_ndblk);
297 break;
298 default:
299 break;
300 }
301 printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
302 acg.cg_cs.cs_nbfree, acg.cg_cs.cs_ndir,
303 acg.cg_cs.cs_nifree, acg.cg_cs.cs_nffree);
304 printf("rotor\t%d\tirotor\t%d\tfrotor\t%d\nfrsum",
305 acg.cg_rotor, acg.cg_irotor, acg.cg_frotor);
306 for (i = 1, j = 0; i < afs.fs_frag; i++) {
307 printf("\t%d", acg.cg_frsum[i]);
308 j += i * acg.cg_frsum[i];
309 }
310 printf("\nsum of frsum: %d", j);
311 if (afs.fs_contigsumsize > 0) {
312 for (i = 1; i < afs.fs_contigsumsize; i++) {
313 if ((i - 1) % 8 == 0)
314 printf("\nclusters %d-%d:", i,
315 afs.fs_contigsumsize - 1 < i + 7 ?
316 afs.fs_contigsumsize - 1 : i + 7);
317 printf("\t%d", cg_clustersum(&acg)[i]);
318 }
319 printf("\nclusters size %d and over: %d\n",
320 afs.fs_contigsumsize,
321 cg_clustersum(&acg)[afs.fs_contigsumsize]);
322 printf("clusters free:\t");
323 pbits(cg_clustersfree(&acg), acg.cg_nclusterblks);
324 } else
325 printf("\n");
326 printf("inodes used:\t");
327 pbits(cg_inosused(&acg), afs.fs_ipg);
328 printf("blks free:\t");
329 pbits(cg_blksfree(&acg), afs.fs_fpg);
330 return (0);
331}
332
333int
334marshal(const char *name)
335{
336 struct fs *fs;
337
338 fs = &disk.d_fs;
339
340 printf("# newfs command for %s (%s)\n", name, disk.d_name);
341 printf("newfs ");
342 printf("-O %d ", disk.d_ufs);
343 if (fs->fs_flags & FS_DOSOFTDEP)
344 printf("-U ");
345 printf("-a %d ", fs->fs_maxcontig);
346 printf("-b %d ", fs->fs_bsize);
347 /* -c is dumb */
348 printf("-d %d ", fs->fs_maxbsize);
349 printf("-e %d ", fs->fs_maxbpg);
350 printf("-f %d ", fs->fs_fsize);
351 printf("-g %d ", fs->fs_avgfilesize);
352 printf("-h %d ", fs->fs_avgfpdir);
353 /* -i is dumb */
354 /* -j..l unimplemented */
355 printf("-m %d ", fs->fs_minfree);
356 /* -n unimplemented */
357 printf("-o ");
358 switch (fs->fs_optim) {
359 case FS_OPTSPACE:
360 printf("space ");
361 break;
362 case FS_OPTTIME:
363 printf("time ");
364 break;
365 default:
366 printf("unknown ");
367 break;
368 }
369 /* -p..r unimplemented */
370 printf("-s %jd ", (intmax_t)fs->fs_size);
371 printf("%s ", disk.d_name);
372 printf("\n");
373
374 return 0;
375}
376
377void
378pbits(void *vp, int max)
379{
380 int i;
381 char *p;
382 int count, j;
383
384 for (count = i = 0, p = vp; i < max; i++)
385 if (isset(p, i)) {
386 if (count)
387 printf(",%s", count % 6 ? " " : "\n\t");
388 count++;
389 printf("%d", i);
390 j = i;
391 while ((i+1)<max && isset(p, i+1))
392 i++;
393 if (i != j)
394 printf("-%d", i);
395 }
396 printf("\n");
397}
398
399void
400ufserr(const char *name)
401{
402 if (disk.d_error != NULL)
403 warnx("%s: %s", name, disk.d_error);
404 else if (errno)
405 warn("%s", name);
406}
407
408void
409usage(void)
410{
411 (void)fprintf(stderr, "usage: dumpfs [-m] filesys | device\n");
412 exit(1);
413}
55#endif /* not lint */
56
57#include <sys/param.h>
58#include <sys/time.h>
59#include <sys/disklabel.h>
60
61#include <ufs/ufs/dinode.h>
62#include <ufs/ffs/fs.h>
63
64#include <err.h>
65#include <errno.h>
66#include <fcntl.h>
67#include <fstab.h>
68#include <libufs.h>
69#include <stdint.h>
70#include <stdio.h>
71#include <stdlib.h>
72#include <unistd.h>
73
74#define afs disk.d_fs
75#define acg disk.d_cg
76
77struct uufsd disk;
78
79int dumpfs(const char *);
80int dumpcg(void);
81int marshal(const char *);
82void pbits(void *, int);
83void ufserr(const char *);
84void usage(void) __dead2;
85
86int
87main(int argc, char *argv[])
88{
89 const char *name;
90 int ch, domarshal, eval;
91
92 domarshal = eval = 0;
93
94 while ((ch = getopt(argc, argv, "m")) != -1) {
95 switch (ch) {
96 case 'm':
97 domarshal = 1;
98 break;
99 case '?':
100 default:
101 usage();
102 }
103 }
104 argc -= optind;
105 argv += optind;
106
107 if (argc < 1)
108 usage();
109
110 while ((name = *argv++) != NULL) {
111 if (ufs_disk_fillout(&disk, name) == -1) {
112 ufserr(name);
113 eval |= 1;
114 continue;
115 }
116 if (domarshal)
117 eval |= marshal(name);
118 else
119 eval |= dumpfs(name);
120 ufs_disk_close(&disk);
121 }
122 exit(eval);
123}
124
125int
126dumpfs(const char *name)
127{
128 time_t fstime;
129 int64_t fssize;
130 int32_t fsflags;
131 int i;
132
133 switch (disk.d_ufs) {
134 case 2:
135 fssize = afs.fs_size;
136 fstime = afs.fs_time;
137 printf("magic\t%x (UFS2)\ttime\t%s",
138 afs.fs_magic, ctime(&fstime));
139 printf("superblock location\t%qd\tid\t[ %x %x ]\n",
140 afs.fs_sblockloc, afs.fs_id[0], afs.fs_id[1]);
141 printf("ncg\t%d\tsize\t%qd\tblocks\t%jd\n",
142 afs.fs_ncg, fssize, (intmax_t)afs.fs_dsize);
143 break;
144 case 1:
145 fssize = afs.fs_old_size;
146 fstime = afs.fs_old_time;
147 printf("magic\t%x (UFS1)\ttime\t%s",
148 afs.fs_magic, ctime(&fstime));
149 printf("id\t[ %x %x ]\n", afs.fs_id[0], afs.fs_id[1]);
150 printf("ncg\t%d\tsize\t%qd\tblocks\t%jd\n",
151 afs.fs_ncg, fssize, (intmax_t)afs.fs_dsize);
152 break;
153 default:
154 goto err;
155 }
156 printf("bsize\t%d\tshift\t%d\tmask\t0x%08x\n",
157 afs.fs_bsize, afs.fs_bshift, afs.fs_bmask);
158 printf("fsize\t%d\tshift\t%d\tmask\t0x%08x\n",
159 afs.fs_fsize, afs.fs_fshift, afs.fs_fmask);
160 printf("frag\t%d\tshift\t%d\tfsbtodb\t%d\n",
161 afs.fs_frag, afs.fs_fragshift, afs.fs_fsbtodb);
162 printf("minfree\t%d%%\toptim\t%s\tsymlinklen %d\n",
163 afs.fs_minfree, afs.fs_optim == FS_OPTSPACE ? "space" : "time",
164 afs.fs_maxsymlinklen);
165 switch (disk.d_ufs) {
166 case 2:
167 printf("%s %d\tmaxbpg\t%d\tmaxcontig %d\tcontigsumsize %d\n",
168 "maxbsize", afs.fs_maxbsize, afs.fs_maxbpg,
169 afs.fs_maxcontig, afs.fs_contigsumsize);
170 printf("nbfree\t%qd\tndir\t%qd\tnifree\t%qd\tnffree\t%qd\n",
171 afs.fs_cstotal.cs_nbfree, afs.fs_cstotal.cs_ndir,
172 afs.fs_cstotal.cs_nifree, afs.fs_cstotal.cs_nffree);
173 printf("bpg\t%d\tfpg\t%d\tipg\t%d\n",
174 afs.fs_fpg / afs.fs_frag, afs.fs_fpg, afs.fs_ipg);
175 printf("nindir\t%d\tinopb\t%d\tmaxfilesize\t%qu\n",
176 afs.fs_nindir, afs.fs_inopb, afs.fs_maxfilesize);
177 printf("sbsize\t%d\tcgsize\t%d\tcsaddr\t%jd\tcssize\t%d\n",
178 afs.fs_sbsize, afs.fs_cgsize, (intmax_t)afs.fs_csaddr,
179 afs.fs_cssize);
180 break;
181 case 1:
182 printf("maxbpg\t%d\tmaxcontig %d\tcontigsumsize %d\n",
183 afs.fs_maxbpg, afs.fs_maxcontig, afs.fs_contigsumsize);
184 printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
185 afs.fs_old_cstotal.cs_nbfree, afs.fs_old_cstotal.cs_ndir,
186 afs.fs_old_cstotal.cs_nifree, afs.fs_old_cstotal.cs_nffree);
187 printf("cpg\t%d\tbpg\t%d\tfpg\t%d\tipg\t%d\n",
188 afs.fs_old_cpg, afs.fs_fpg / afs.fs_frag, afs.fs_fpg,
189 afs.fs_ipg);
190 printf("nindir\t%d\tinopb\t%d\tnspf\t%d\tmaxfilesize\t%qu\n",
191 afs.fs_nindir, afs.fs_inopb, afs.fs_old_nspf,
192 afs.fs_maxfilesize);
193 printf("sbsize\t%d\tcgsize\t%d\tcgoffset %d\tcgmask\t0x%08x\n",
194 afs.fs_sbsize, afs.fs_cgsize, afs.fs_old_cgoffset,
195 afs.fs_old_cgmask);
196 printf("csaddr\t%d\tcssize\t%d\n",
197 afs.fs_old_csaddr, afs.fs_cssize);
198 printf("rotdelay %dms\trps\t%d\ttrackskew %d\tinterleave %d\n",
199 afs.fs_old_rotdelay, afs.fs_old_rps, afs.fs_old_trackskew,
200 afs.fs_old_interleave);
201 printf("nsect\t%d\tnpsect\t%d\tspc\t%d\n",
202 afs.fs_old_nsect, afs.fs_old_npsect, afs.fs_old_spc);
203 break;
204 default:
205 goto err;
206 }
207 printf("sblkno\t%d\tcblkno\t%d\tiblkno\t%d\tdblkno\t%d\n",
208 afs.fs_sblkno, afs.fs_cblkno, afs.fs_iblkno, afs.fs_dblkno);
209 printf("cgrotor\t%d\tfmod\t%d\tronly\t%d\tclean\t%d\n",
210 afs.fs_cgrotor, afs.fs_fmod, afs.fs_ronly, afs.fs_clean);
211 printf("flags\t");
212 if (afs.fs_old_flags & FS_FLAGS_UPDATED)
213 fsflags = afs.fs_flags;
214 else
215 fsflags = afs.fs_old_flags;
216 if (fsflags == 0)
217 printf("none");
218 if (fsflags & FS_UNCLEAN)
219 printf("unclean ");
220 if (fsflags & FS_DOSOFTDEP)
221 printf("soft-updates ");
222 if (fsflags & FS_NEEDSFSCK)
223 printf("needs fsck run ");
224 if (fsflags & FS_INDEXDIRS)
225 printf("indexed directories ");
226 if (fsflags & FS_ACLS)
227 printf("acls ");
228 if (fsflags & FS_MULTILABEL)
229 printf("multilabel ");
230 if (fsflags & FS_FLAGS_UPDATED)
231 printf("fs_flags expanded ");
232 fsflags &= ~(FS_UNCLEAN | FS_DOSOFTDEP | FS_NEEDSFSCK | FS_INDEXDIRS |
233 FS_ACLS | FS_MULTILABEL | FS_FLAGS_UPDATED);
234 if (fsflags != 0)
235 printf("unknown flags (%#x)", fsflags);
236 putchar('\n');
237 printf("fsmnt\t%s\n", afs.fs_fsmnt);
238 printf("volname\t%s\tswuid\t%qu\n",
239 afs.fs_volname, afs.fs_swuid);
240 printf("\ncs[].cs_(nbfree,ndir,nifree,nffree):\n\t");
241 afs.fs_csp = calloc(1, afs.fs_cssize);
242 if (bread(&disk, fsbtodb(&afs, afs.fs_csaddr), afs.fs_csp, afs.fs_cssize) == -1)
243 goto err;
244 for (i = 0; i < afs.fs_ncg; i++) {
245 struct csum *cs = &afs.fs_cs(&afs, i);
246 if (i && i % 4 == 0)
247 printf("\n\t");
248 printf("(%d,%d,%d,%d) ",
249 cs->cs_nbfree, cs->cs_ndir, cs->cs_nifree, cs->cs_nffree);
250 }
251 printf("\n");
252 if (fssize % afs.fs_fpg) {
253 if (disk.d_ufs == 1)
254 printf("cylinders in last group %d\n",
255 howmany(afs.fs_old_size % afs.fs_fpg,
256 afs.fs_old_spc / afs.fs_old_nspf));
257 printf("blocks in last group %ld\n\n",
258 (long)((fssize % afs.fs_fpg) / afs.fs_frag));
259 }
260 while ((i = cgread(&disk)) != 0) {
261 if (i == -1 || dumpcg())
262 goto err;
263 }
264 return (0);
265
266err: ufserr(name);
267 return (1);
268}
269
270int
271dumpcg(void)
272{
273 time_t cgtime;
274 off_t cur;
275 int i, j;
276
277 printf("\ncg %d:\n", disk.d_lcg);
278 cur = fsbtodb(&afs, cgtod(&afs, disk.d_lcg)) * disk.d_bsize;
279 switch (disk.d_ufs) {
280 case 2:
281 cgtime = acg.cg_time;
282 printf("magic\t%x\ttell\t%qx\ttime\t%s",
283 acg.cg_magic, cur, ctime(&cgtime));
284 printf("cgx\t%d\tndblk\t%d\tniblk\t%d\tinitiblk %d\n",
285 acg.cg_cgx, acg.cg_ndblk, acg.cg_niblk, acg.cg_initediblk);
286 break;
287 case 1:
288 cgtime = acg.cg_old_time;
289 printf("magic\t%x\ttell\t%qx\ttime\t%s",
290 acg.cg_magic, cur, ctime(&cgtime));
291 printf("cgx\t%d\tncyl\t%d\tniblk\t%d\tndblk\t%d\n",
292 acg.cg_cgx, acg.cg_old_ncyl, acg.cg_old_niblk,
293 acg.cg_ndblk);
294 break;
295 default:
296 break;
297 }
298 printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
299 acg.cg_cs.cs_nbfree, acg.cg_cs.cs_ndir,
300 acg.cg_cs.cs_nifree, acg.cg_cs.cs_nffree);
301 printf("rotor\t%d\tirotor\t%d\tfrotor\t%d\nfrsum",
302 acg.cg_rotor, acg.cg_irotor, acg.cg_frotor);
303 for (i = 1, j = 0; i < afs.fs_frag; i++) {
304 printf("\t%d", acg.cg_frsum[i]);
305 j += i * acg.cg_frsum[i];
306 }
307 printf("\nsum of frsum: %d", j);
308 if (afs.fs_contigsumsize > 0) {
309 for (i = 1; i < afs.fs_contigsumsize; i++) {
310 if ((i - 1) % 8 == 0)
311 printf("\nclusters %d-%d:", i,
312 afs.fs_contigsumsize - 1 < i + 7 ?
313 afs.fs_contigsumsize - 1 : i + 7);
314 printf("\t%d", cg_clustersum(&acg)[i]);
315 }
316 printf("\nclusters size %d and over: %d\n",
317 afs.fs_contigsumsize,
318 cg_clustersum(&acg)[afs.fs_contigsumsize]);
319 printf("clusters free:\t");
320 pbits(cg_clustersfree(&acg), acg.cg_nclusterblks);
321 } else
322 printf("\n");
323 printf("inodes used:\t");
324 pbits(cg_inosused(&acg), afs.fs_ipg);
325 printf("blks free:\t");
326 pbits(cg_blksfree(&acg), afs.fs_fpg);
327 return (0);
328}
329
330int
331marshal(const char *name)
332{
333 struct fs *fs;
334
335 fs = &disk.d_fs;
336
337 printf("# newfs command for %s (%s)\n", name, disk.d_name);
338 printf("newfs ");
339 printf("-O %d ", disk.d_ufs);
340 if (fs->fs_flags & FS_DOSOFTDEP)
341 printf("-U ");
342 printf("-a %d ", fs->fs_maxcontig);
343 printf("-b %d ", fs->fs_bsize);
344 /* -c is dumb */
345 printf("-d %d ", fs->fs_maxbsize);
346 printf("-e %d ", fs->fs_maxbpg);
347 printf("-f %d ", fs->fs_fsize);
348 printf("-g %d ", fs->fs_avgfilesize);
349 printf("-h %d ", fs->fs_avgfpdir);
350 /* -i is dumb */
351 /* -j..l unimplemented */
352 printf("-m %d ", fs->fs_minfree);
353 /* -n unimplemented */
354 printf("-o ");
355 switch (fs->fs_optim) {
356 case FS_OPTSPACE:
357 printf("space ");
358 break;
359 case FS_OPTTIME:
360 printf("time ");
361 break;
362 default:
363 printf("unknown ");
364 break;
365 }
366 /* -p..r unimplemented */
367 printf("-s %jd ", (intmax_t)fs->fs_size);
368 printf("%s ", disk.d_name);
369 printf("\n");
370
371 return 0;
372}
373
374void
375pbits(void *vp, int max)
376{
377 int i;
378 char *p;
379 int count, j;
380
381 for (count = i = 0, p = vp; i < max; i++)
382 if (isset(p, i)) {
383 if (count)
384 printf(",%s", count % 6 ? " " : "\n\t");
385 count++;
386 printf("%d", i);
387 j = i;
388 while ((i+1)<max && isset(p, i+1))
389 i++;
390 if (i != j)
391 printf("-%d", i);
392 }
393 printf("\n");
394}
395
396void
397ufserr(const char *name)
398{
399 if (disk.d_error != NULL)
400 warnx("%s: %s", name, disk.d_error);
401 else if (errno)
402 warn("%s", name);
403}
404
405void
406usage(void)
407{
408 (void)fprintf(stderr, "usage: dumpfs [-m] filesys | device\n");
409 exit(1);
410}