GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: sbin/dumpfs/dumpfs.c Lines: 0 245 0.0 %
Date: 2017-11-07 Branches: 0 114 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: dumpfs.c,v 1.33 2015/11/23 19:19:29 deraadt 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, 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
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
		case '?':
93
		default:
94
			usage();
95
		}
96
	}
97
	argc -= optind;
98
	argv += optind;
99
100
	if (argc < 1)
101
		usage();
102
103
	if (pledge("stdio rpath disklabel flock cpath wpath", NULL) == -1)
104
		err(1, "pledge");
105
106
	for (; *argv != NULL; argv++) {
107
		if ((fs = getfsfile(*argv)) != NULL)
108
			name = fs->fs_spec;
109
		else
110
			name = *argv;
111
		if ((fd = open_disk(name)) == -1) {
112
			eval |= 1;
113
			continue;
114
		}
115
		if (domarshal)
116
			eval |= marshal(name);
117
		else
118
			eval |= dumpfs(fd, name);
119
		close(fd);
120
	}
121
	exit(eval);
122
}
123
124
int
125
open_disk(const char *name)
126
{
127
	int fd, i, sbtry[] = SBLOCKSEARCH;
128
	ssize_t n;
129
130
	/* XXX - should retry w/raw device on failure */
131
	if ((fd = opendev(name, O_RDONLY, 0, NULL)) < 0) {
132
		warn("%s", name);
133
		return(-1);
134
	}
135
136
	/* Read superblock, could be UFS1 or UFS2. */
137
	for (i = 0; sbtry[i] != -1; i++) {
138
		n = pread(fd, &afs, SBLOCKSIZE, (off_t)sbtry[i]);
139
		if (n == SBLOCKSIZE && (afs.fs_magic == FS_UFS1_MAGIC ||
140
		    (afs.fs_magic == FS_UFS2_MAGIC &&
141
		    afs.fs_sblockloc == sbtry[i])) &&
142
		    afs.fs_bsize <= MAXBSIZE &&
143
		    afs.fs_bsize >= sizeof(struct fs))
144
			break;
145
	}
146
	if (sbtry[i] == -1) {
147
		warnx("cannot find filesystem superblock");
148
		close(fd);
149
		return (-1);
150
	}
151
152
	return (fd);
153
}
154
155
int
156
dumpfs(int fd, const char *name)
157
{
158
	time_t fstime;
159
	int64_t fssize;
160
	int32_t fsflags;
161
	size_t size;
162
	off_t off;
163
	int i, j;
164
165
	switch (afs.fs_magic) {
166
	case FS_UFS2_MAGIC:
167
		fssize = afs.fs_size;
168
		fstime = afs.fs_time;
169
		printf("magic\t%x (FFS2)\ttime\t%s",
170
		    afs.fs_magic, ctime(&fstime));
171
		printf("superblock location\t%jd\tid\t[ %x %x ]\n",
172
		    (intmax_t)afs.fs_sblockloc, afs.fs_id[0], afs.fs_id[1]);
173
		printf("ncg\t%d\tsize\t%jd\tblocks\t%jd\n",
174
		    afs.fs_ncg, (intmax_t)fssize, (intmax_t)afs.fs_dsize);
175
		break;
176
	case FS_UFS1_MAGIC:
177
		fssize = afs.fs_ffs1_size;
178
		fstime = afs.fs_ffs1_time;
179
		printf("magic\t%x (FFS1)\ttime\t%s",
180
		    afs.fs_magic, ctime(&fstime));
181
		printf("id\t[ %x %x ]\n", afs.fs_id[0], afs.fs_id[1]);
182
		i = 0;
183
		if (afs.fs_postblformat != FS_42POSTBLFMT) {
184
			i++;
185
			if (afs.fs_inodefmt >= FS_44INODEFMT) {
186
				size_t max;
187
188
				i++;
189
				max = afs.fs_maxcontig;
190
				size = afs.fs_contigsumsize;
191
				if ((max < 2 && size == 0) ||
192
				    (max > 1 && size >= MINIMUM(max, FS_MAXCONTIG)))
193
					i++;
194
			}
195
		}
196
		printf("cylgrp\t%s\tinodes\t%s\tfslevel %d\n",
197
		    i < 1 ? "static" : "dynamic",
198
		    i < 2 ? "4.2/4.3BSD" : "4.4BSD", i);
199
		printf("ncg\t%d\tncyl\t%d\tsize\t%d\tblocks\t%d\n",
200
		    afs.fs_ncg, afs.fs_ncyl, afs.fs_ffs1_size, afs.fs_ffs1_dsize);
201
		break;
202
	default:
203
		goto err;
204
	}
205
	printf("bsize\t%d\tshift\t%d\tmask\t0x%08x\n",
206
	    afs.fs_bsize, afs.fs_bshift, afs.fs_bmask);
207
	printf("fsize\t%d\tshift\t%d\tmask\t0x%08x\n",
208
	    afs.fs_fsize, afs.fs_fshift, afs.fs_fmask);
209
	printf("frag\t%d\tshift\t%d\tfsbtodb\t%d\n",
210
	    afs.fs_frag, afs.fs_fragshift, afs.fs_fsbtodb);
211
	printf("minfree\t%d%%\toptim\t%s\tsymlinklen %d\n",
212
	    afs.fs_minfree, afs.fs_optim == FS_OPTSPACE ? "space" : "time",
213
	    afs.fs_maxsymlinklen);
214
	switch (afs.fs_magic) {
215
	case FS_UFS2_MAGIC:
216
		printf("%s %d\tmaxbpg\t%d\tmaxcontig %d\tcontigsumsize %d\n",
217
		    "maxbsize", afs.fs_maxbsize, afs.fs_maxbpg,
218
		    afs.fs_maxcontig, afs.fs_contigsumsize);
219
		printf("nbfree\t%jd\tndir\t%jd\tnifree\t%jd\tnffree\t%jd\n",
220
		    (intmax_t)afs.fs_cstotal.cs_nbfree,
221
		    (intmax_t)afs.fs_cstotal.cs_ndir,
222
		    (intmax_t)afs.fs_cstotal.cs_nifree,
223
		    (intmax_t)afs.fs_cstotal.cs_nffree);
224
		printf("bpg\t%d\tfpg\t%d\tipg\t%d\n",
225
		    afs.fs_fpg / afs.fs_frag, afs.fs_fpg, afs.fs_ipg);
226
		printf("nindir\t%d\tinopb\t%d\tmaxfilesize\t%ju\n",
227
		    afs.fs_nindir, afs.fs_inopb,
228
		    (uintmax_t)afs.fs_maxfilesize);
229
		printf("sbsize\t%d\tcgsize\t%d\tcsaddr\t%jd\tcssize\t%d\n",
230
		    afs.fs_sbsize, afs.fs_cgsize, (intmax_t)afs.fs_csaddr,
231
		    afs.fs_cssize);
232
		break;
233
	case FS_UFS1_MAGIC:
234
		printf("maxbpg\t%d\tmaxcontig %d\tcontigsumsize %d\n",
235
		    afs.fs_maxbpg, afs.fs_maxcontig, afs.fs_contigsumsize);
236
		printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
237
		    afs.fs_ffs1_cstotal.cs_nbfree, afs.fs_ffs1_cstotal.cs_ndir,
238
		    afs.fs_ffs1_cstotal.cs_nifree, afs.fs_ffs1_cstotal.cs_nffree);
239
		printf("cpg\t%d\tbpg\t%d\tfpg\t%d\tipg\t%d\n",
240
		    afs.fs_cpg, afs.fs_fpg / afs.fs_frag, afs.fs_fpg,
241
		    afs.fs_ipg);
242
		printf("nindir\t%d\tinopb\t%d\tnspf\t%d\tmaxfilesize\t%ju\n",
243
		    afs.fs_nindir, afs.fs_inopb, afs.fs_nspf,
244
		    (uintmax_t)afs.fs_maxfilesize);
245
		printf("sbsize\t%d\tcgsize\t%d\tcgoffset %d\tcgmask\t0x%08x\n",
246
		    afs.fs_sbsize, afs.fs_cgsize, afs.fs_cgoffset,
247
		    afs.fs_cgmask);
248
		printf("csaddr\t%d\tcssize\t%d\n",
249
		    afs.fs_ffs1_csaddr, afs.fs_cssize);
250
		printf("rotdelay %dms\trps\t%d\tinterleave %d\n",
251
		    afs.fs_rotdelay, afs.fs_rps, afs.fs_interleave);
252
		printf("nsect\t%d\tnpsect\t%d\tspc\t%d\n",
253
		    afs.fs_nsect, afs.fs_npsect, afs.fs_spc);
254
		break;
255
	default:
256
		goto err;
257
	}
258
	printf("sblkno\t%d\tcblkno\t%d\tiblkno\t%d\tdblkno\t%d\n",
259
	    afs.fs_sblkno, afs.fs_cblkno, afs.fs_iblkno, afs.fs_dblkno);
260
	printf("cgrotor\t%d\tfmod\t%d\tronly\t%d\tclean\t%d\n",
261
	    afs.fs_cgrotor, afs.fs_fmod, afs.fs_ronly, afs.fs_clean);
262
	printf("avgfpdir %d\tavgfilesize %d\n",
263
	    afs.fs_avgfpdir, afs.fs_avgfilesize);
264
	printf("flags\t");
265
	if (afs.fs_magic == FS_UFS2_MAGIC ||
266
	    afs.fs_ffs1_flags & FS_FLAGS_UPDATED)
267
		fsflags = afs.fs_flags;
268
	else
269
		fsflags = afs.fs_ffs1_flags;
270
	if (fsflags == 0)
271
		printf("none");
272
	if (fsflags & FS_UNCLEAN)
273
		printf("unclean ");
274
	if (fsflags & FS_DOSOFTDEP)
275
		printf("soft-updates ");
276
	if (fsflags & FS_FLAGS_UPDATED)
277
		printf("updated ");
278
#if 0
279
	fsflags &= ~(FS_UNCLEAN | FS_DOSOFTDEP | FS_FLAGS_UPDATED);
280
	if (fsflags != 0)
281
		printf("unknown flags (%#x)", fsflags);
282
#endif
283
	putchar('\n');
284
	printf("fsmnt\t%s\n", afs.fs_fsmnt);
285
	printf("volname\t%s\tswuid\t%ju\n",
286
		afs.fs_volname, (uintmax_t)afs.fs_swuid);
287
	printf("\ncs[].cs_(nbfree,ndir,nifree,nffree):\n\t");
288
	afs.fs_csp = calloc(1, afs.fs_cssize);
289
	for (i = 0, j = 0; i < afs.fs_cssize; i += afs.fs_bsize, j++) {
290
		size = afs.fs_cssize - i < afs.fs_bsize ?
291
		    afs.fs_cssize - i : afs.fs_bsize;
292
		off = (off_t)(fsbtodb(&afs, (afs.fs_csaddr + j *
293
		    afs.fs_frag))) * DEV_BSIZE;
294
		if (pread(fd, (char *)afs.fs_csp + i, size, off) != size)
295
			goto err;
296
	}
297
	for (i = 0; i < afs.fs_ncg; i++) {
298
		struct csum *cs = &afs.fs_cs(&afs, i);
299
		if (i && i % 4 == 0)
300
			printf("\n\t");
301
		printf("(%d,%d,%d,%d) ",
302
		    cs->cs_nbfree, cs->cs_ndir, cs->cs_nifree, cs->cs_nffree);
303
	}
304
	printf("\n");
305
	if (fssize % afs.fs_fpg) {
306
		if (afs.fs_magic == FS_UFS1_MAGIC)
307
			printf("cylinders in last group %d\n",
308
			    howmany(afs.fs_ffs1_size % afs.fs_fpg,
309
			    afs.fs_spc / afs.fs_nspf));
310
		printf("blocks in last group %ld\n\n",
311
		    (long)((fssize % afs.fs_fpg) / afs.fs_frag));
312
	}
313
	for (i = 0; i < afs.fs_ncg; i++)
314
		if (dumpcg(name, fd, i))
315
			goto err;
316
	return (0);
317
318
err:	warn("%s", name);
319
	return (1);
320
}
321
322
int
323
dumpcg(const char *name, int fd, int c)
324
{
325
	time_t cgtime;
326
	off_t cur;
327
	int i, j;
328
329
	printf("\ncg %d:\n", c);
330
	cur = (off_t)fsbtodb(&afs, cgtod(&afs, c)) * DEV_BSIZE;
331
	if (pread(fd, &acg, afs.fs_bsize, cur) != afs.fs_bsize) {
332
		warn("%s: error reading cg", name);
333
		return(1);
334
	}
335
	switch (afs.fs_magic) {
336
	case FS_UFS2_MAGIC:
337
		cgtime = acg.cg_ffs2_time;
338
		printf("magic\t%x\ttell\t%jx\ttime\t%s",
339
		    acg.cg_magic, (intmax_t)cur, ctime(&cgtime));
340
		printf("cgx\t%d\tndblk\t%d\tniblk\t%d\tinitiblk %d\n",
341
		    acg.cg_cgx, acg.cg_ndblk, acg.cg_ffs2_niblk,
342
		    acg.cg_initediblk);
343
		break;
344
	case FS_UFS1_MAGIC:
345
		cgtime = acg.cg_time;
346
		printf("magic\t%x\ttell\t%jx\ttime\t%s",
347
		    afs.fs_postblformat == FS_42POSTBLFMT ?
348
		    ((struct ocg *)&acg)->cg_magic : acg.cg_magic,
349
		    (intmax_t)cur, ctime(&cgtime));
350
		printf("cgx\t%d\tncyl\t%d\tniblk\t%d\tndblk\t%d\n",
351
		    acg.cg_cgx, acg.cg_ncyl, acg.cg_niblk, acg.cg_ndblk);
352
		break;
353
	default:
354
		break;
355
	}
356
	printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
357
	    acg.cg_cs.cs_nbfree, acg.cg_cs.cs_ndir,
358
	    acg.cg_cs.cs_nifree, acg.cg_cs.cs_nffree);
359
	printf("rotor\t%d\tirotor\t%d\tfrotor\t%d\nfrsum",
360
	    acg.cg_rotor, acg.cg_irotor, acg.cg_frotor);
361
	for (i = 1, j = 0; i < afs.fs_frag; i++) {
362
		printf("\t%d", acg.cg_frsum[i]);
363
		j += i * acg.cg_frsum[i];
364
	}
365
	printf("\nsum of frsum: %d", j);
366
	if (afs.fs_contigsumsize > 0) {
367
		for (i = 1; i < afs.fs_contigsumsize; i++) {
368
			if ((i - 1) % 8 == 0)
369
				printf("\nclusters %d-%d:", i,
370
				    afs.fs_contigsumsize - 1 < i + 7 ?
371
				    afs.fs_contigsumsize - 1 : i + 7);
372
			printf("\t%d", cg_clustersum(&acg)[i]);
373
		}
374
		printf("\nclusters size %d and over: %d\n",
375
		    afs.fs_contigsumsize,
376
		    cg_clustersum(&acg)[afs.fs_contigsumsize]);
377
		printf("clusters free:\t");
378
		pbits(cg_clustersfree(&acg), acg.cg_nclusterblks);
379
	} else
380
		printf("\n");
381
	printf("inodes used:\t");
382
	pbits(cg_inosused(&acg), afs.fs_ipg);
383
	printf("blks free:\t");
384
	pbits(cg_blksfree(&acg), afs.fs_fpg);
385
#if 0
386
	/* XXX - keep this? */
387
	if (afs.fs_magic == FS_UFS1_MAGIC) {
388
		printf("b:\n");
389
		for (i = 0; i < afs.fs_cpg; i++) {
390
			if (cg_blktot(&acg)[i] == 0)
391
				continue;
392
			printf("   c%d:\t(%d)\t", i, cg_blktot(&acg)[i]);
393
			printf("\n");
394
		}
395
	}
396
#endif
397
	return (0);
398
}
399
400
int
401
marshal(const char *name)
402
{
403
	int Oflag;
404
405
	printf("# newfs command for %s\n", name);
406
	printf("newfs ");
407
	if (afs.fs_volname[0] != '\0')
408
		printf("-L %s ", afs.fs_volname);
409
410
	Oflag = (afs.fs_magic == FS_UFS2_MAGIC) +
411
	    (afs.fs_inodefmt == FS_44INODEFMT);
412
	printf("-O %d ", Oflag);
413
	printf("-b %d ", afs.fs_bsize);
414
	/* -c unimplemented */
415
	printf("-e %d ", afs.fs_maxbpg);
416
	printf("-f %d ", afs.fs_fsize);
417
	printf("-g %d ", afs.fs_avgfilesize);
418
	printf("-h %d ", afs.fs_avgfpdir);
419
	/* -i unimplemented */
420
	printf("-m %d ", afs.fs_minfree);
421
	printf("-o ");
422
	switch (afs.fs_optim) {
423
	case FS_OPTSPACE:
424
		printf("space ");
425
		break;
426
	case FS_OPTTIME:
427
		printf("time ");
428
		break;
429
	default:
430
		printf("unknown ");
431
		break;
432
	}
433
	/* -S unimplemented */
434
	printf("-s %jd ", (intmax_t)afs.fs_size * (afs.fs_fsize / DEV_BSIZE));
435
	printf("%s ", name);
436
	printf("\n");
437
438
	return 0;
439
}
440
441
void
442
pbits(void *vp, int max)
443
{
444
	int i;
445
	char *p;
446
	int count, j;
447
448
	for (count = i = 0, p = vp; i < max; i++)
449
		if (isset(p, i)) {
450
			if (count)
451
				printf(",%s", count % 6 ? " " : "\n\t");
452
			count++;
453
			printf("%d", i);
454
			j = i;
455
			while ((i+1)<max && isset(p, i+1))
456
				i++;
457
			if (i != j)
458
				printf("-%d", i);
459
		}
460
	printf("\n");
461
}
462
463
__dead void
464
usage(void)
465
{
466
	(void)fprintf(stderr, "usage: dumpfs [-m] filesys | device\n");
467
	exit(1);
468
}