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