summaryrefslogtreecommitdiffstats
path: root/src/ufs_ffinfo.c
blob: 9851547e3f0952bfc53c33a9196f5479c9405bf9 (plain) (blame)
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
#include <ufs/ffs/fs.h>
#include <libufs.h>

#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <strings.h>
#include <stdlib.h>

#define MAXBINS 20

#ifndef max
# define max(a,b) ((a) > (b) ? (a) : (b))
#endif

struct ffinfo {
	uint64_t bins[MAXBINS];	
	uint64_t free_blocks[MAXBINS];	
	uint64_t blocks_num;
};

struct ffinfo_ctx {
	struct fs *fs;
	struct ffinfo* info_cg;
	struct ffinfo info_sum;
	uint8_t verbose;
};

static int
rdfs(ufs2_daddr_t bno, size_t size, void *bf, int fsi)
{
	ssize_t	n;

	if (bno < 0) {
		fprintf(stderr, "rdfs: attempting to read negative block number");
		return -1;
	}

	if (lseek(fsi, (off_t)bno * DEV_BSIZE, 0) < 0) {
		fprintf(stderr, "rdfs: seek error: %jd", (intmax_t)bno);
		return -1;
	}

	n = read(fsi, bf, size);

	if (n != (ssize_t)size) {
		fprintf(stderr, "rdfs: read error: %jd", (intmax_t)bno);
		return -1;
	}

	return 0;
}

static int
isblock(const struct fs *fs, unsigned char *cp, int h)
{
	unsigned char mask;

	switch (fs->fs_frag) {
	case 8:
		return (cp[h] == 0xff);
	case 4:
		mask = 0x0f << ((h & 0x1) << 2);
		return ((cp[h >> 1] & mask) == mask);
	case 2:
		mask = 0x03 << ((h & 0x3) << 1);
		return ((cp[h >> 2] & mask) == mask);
	case 1:
		mask = 0x01 << (h & 0x7);
		return ((cp[h >> 3] & mask) == mask);
	default:
		fprintf(stderr, "isblock bad fs_frag %d\n", fs->fs_frag);
		return (0);
	}
}

/*
 * Read the superblock and init the work ctx
 */

static struct ffinfo_ctx *
ffinfo_ctx_init(int fd, uint8_t verbose)
{
	struct fs *fs;
	struct ffinfo_ctx *ctx;
	int ret;

	// Read the superblock
	if ((ret = sbget(fd, &fs, -1)) != 0) {
		fprintf(stderr, "Unable to read superblock\n");
		return NULL;
	}

	ctx = calloc(1, sizeof(struct ffinfo_ctx));
	if (!ctx) {
		fprintf(stderr, "Unable to allocate memory for ctx\n");
		goto err_free_sb_mem;
	}

	ctx->fs = fs;
	ctx->verbose = verbose;

	if (verbose) {
		fprintf(stdout, "Superblock found,\n"
			"size (KB) total: %llu, CG num: %d, block size: %d, frags per block: %d, max_bpg: %d\n",
			fs->fs_size * fs->fs_fsize / 1024llu, fs->fs_ncg, fs->fs_bsize, fs->fs_frag, fs->fs_maxbpg);
	}

	ctx->info_cg = calloc(fs->fs_ncg, sizeof(struct ffinfo));
	if (!ctx->info_cg) {
		fprintf(stderr, "Unable to allocate memory for info_cg\n");
		goto err_free_ctx;
	}

	return ctx;

err_free_ctx:
	free(ctx);

err_free_sb_mem:
	free(fs);

	return NULL;
}

static void
ffinfo_ctx_free(struct ffinfo_ctx *ctx)
{
	free(ctx->info_cg);
	free(ctx->fs);
	free(ctx);
}

static void
ffinfo_print_hysto(uint64_t hysto_blocks_num, uint64_t total_blocks_num, const uint64_t *bins, const uint64_t *free_blocks)
{
	uint64_t blocks;
	int e;
	for (blocks = 1, e = 0; blocks < hysto_blocks_num && e < MAXBINS; blocks <<= 1, e++) {
		fprintf(stdout, "  [%lu ... %lu) - %lu %lu %.2f\n", blocks, blocks << 1, bins[e], free_blocks[e], 100 * free_blocks[e]/(float)total_blocks_num);
	}
}

static void
ffinfo_print(const struct ffinfo_ctx *ctx)
{
	uint32_t c;
	uint32_t blocks;
	uint32_t e;
	uint64_t free_blocks;

	struct ffinfo* info;

	uint64_t sum_bins[MAXBINS] = {0};
	uint64_t sum_free_blocks[MAXBINS] = {0};
	uint64_t max_blocks_num = 0;
	uint64_t total_free_blocks_num = 0;

	for (c = 0; c < ctx->fs->fs_ncg; c++) {
		free_blocks = 0;
		info = &ctx->info_cg[c];
		max_blocks_num = max(max_blocks_num, ctx->info_cg[c].blocks_num);
		for (blocks = 1, e = 0; blocks < ctx->info_cg[c].blocks_num && e < MAXBINS; blocks <<= 1, e++) {
			free_blocks += info->free_blocks[e];
			sum_bins[e] += info->bins[e];
			sum_free_blocks[e] += info->free_blocks[e];
		}
		total_free_blocks_num += free_blocks;

		fprintf(stdout, "Cylinder group %d, total blocks: %lu, free blocks: %lu\n", c, ctx->info_cg[c].blocks_num, free_blocks);
		ffinfo_print_hysto(ctx->info_cg[c].blocks_num, free_blocks, info->bins, info->free_blocks);
	}

	fprintf(stdout, "Summary\n");

	ffinfo_print_hysto(max_blocks_num, total_free_blocks_num, sum_bins, sum_free_blocks);


}

static union {
	struct cg	cg;
	char		pad[MAXBSIZE];
} cgun2;
#define	aocg	cgun2.cg

static void
ffinfo_scan_freemap(const struct fs* fs, char* map, uint32_t size, struct ffinfo* res)
{
	uint32_t d;
	uint32_t blocks = size / fs->fs_frag;
	uint32_t bin;
	uint32_t free_count= 0;

	for (d = 0; d < blocks; d++) {
		if (isblock(fs, map, d)) {
			free_count++;
		} else {
			if (free_count) {
				bin = fls(free_count) - 1;

				if (bin >= MAXBINS)
					break;

				res->bins[bin]++;
				res->free_blocks[bin] += free_count;;

				free_count = 0;
			}
		}
	}

	if (free_count) {
		bin = fls(free_count) - 1;
		if (bin >= MAXBINS)
			return;
		res->bins[bin]++;
		res->free_blocks[bin] += free_count;
	}
}

static int
ffinfo_run(int fd)
{
	uint32_t c;
	char* map;
	struct ffinfo_ctx *ctx;

        ctx = ffinfo_ctx_init(fd, 1);
	if (!ctx) {
		return -1;
	}

	for (c = 0; c < ctx->fs->fs_ncg; c++) {
		if (rdfs(fsbtodb(ctx->fs, cgtod(ctx->fs, c)),
			(size_t)ctx->fs->fs_cgsize, (void*)&aocg, fd) < 0) {
			goto err_free_ctx;
		}

		ctx->info_cg[c].blocks_num = aocg.cg_ndblk / ctx->fs->fs_frag;
		
		map = cg_blksfree(&aocg);

		ffinfo_scan_freemap(ctx->fs, map, aocg.cg_ndblk, &ctx->info_cg[c]);
	}

	ffinfo_print(ctx);

	ffinfo_ctx_free(ctx);

	return 0;

err_free_ctx:

	ffinfo_ctx_free(ctx);
	return -1;
}

int
main(int argc, char *argv[])
{
	const char* special;
	int fsfd;

	if (argc != 2)
		fprintf(stderr, "no block device given\n");

	special = argv[1];

	if ((fsfd = open(special, O_RDONLY)) < 0 || ffinfo_run(fsfd) < 0)
		fprintf(stderr, "err\n");

	return 0;
}