forked from kdave/btrfs-progs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
btrfs-fragments.c
462 lines (404 loc) · 10.7 KB
/
btrfs-fragments.c
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
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License v2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 021110-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <libgen.h>
#include <limits.h>
#include <uuid/uuid.h>
#include <ctype.h>
#include <gd.h>
#undef ULONG_MAX
#include "kerncompat.h"
#include "ctree.h"
#include "ioctl.h"
#include "utils.h"
static int use_color;
static void
push_im(gdImagePtr im, char *name, char *dir)
{
char fullname[2000];
FILE *pngout;
if (!im)
return;
snprintf(fullname, sizeof(fullname), "%s/%s", dir, name);
pngout = fopen(fullname, "w");
if (!pngout) {
printf("unable to create file %s\n", fullname);
exit(1);
}
gdImagePng(im, pngout);
fclose(pngout);
gdImageDestroy(im);
}
static char *
chunk_type(u64 flags)
{
switch (flags & (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_DATA |
BTRFS_BLOCK_GROUP_METADATA)) {
case BTRFS_BLOCK_GROUP_SYSTEM:
return "system";
case BTRFS_BLOCK_GROUP_DATA:
return "data";
case BTRFS_BLOCK_GROUP_METADATA:
return "metadata";
case BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA:
return "mixed";
default:
return "invalid";
}
}
static void
print_bg(FILE *html, char *name, u64 start, u64 len, u64 used, u64 flags,
u64 areas)
{
double frag = (double)areas / (len / 4096) * 2;
fprintf(html, "<p>%s chunk starts at %lld, size is %s, %.2f%% used, "
"%.2f%% fragmented</p>\n", chunk_type(flags), start,
pretty_size(len), 100.0 * used / len, 100.0 * frag);
fprintf(html, "<img src=\"%s\" border=\"1\" />\n", name);
}
enum tree_colors {
COLOR_ROOT = 0,
COLOR_EXTENT,
COLOR_CHUNK,
COLOR_DEV,
COLOR_FS,
COLOR_CSUM,
COLOR_RELOC,
COLOR_DATA,
COLOR_UNKNOWN,
COLOR_MAX
};
static int
get_color(struct btrfs_extent_item *item, int len)
{
u64 refs;
u64 flags;
u8 type;
u64 offset;
struct btrfs_extent_inline_ref *ref;
refs = btrfs_stack_extent_refs(item);
flags = btrfs_stack_extent_flags(item);
if (flags & BTRFS_EXTENT_FLAG_DATA)
return COLOR_DATA;
if (refs > 1) {
/* this must be an fs tree */
return COLOR_FS;
}
ref = (void *)item + sizeof(struct btrfs_extent_item) +
sizeof(struct btrfs_tree_block_info);
type = btrfs_stack_extent_inline_ref_type(ref);
offset = btrfs_stack_extent_inline_ref_offset(ref);
switch (type) {
case BTRFS_EXTENT_DATA_REF_KEY:
return COLOR_DATA;
case BTRFS_SHARED_BLOCK_REF_KEY:
case BTRFS_SHARED_DATA_REF_KEY:
return COLOR_FS;
case BTRFS_TREE_BLOCK_REF_KEY:
break;
default:
return COLOR_UNKNOWN;
}
switch (offset) {
case BTRFS_ROOT_TREE_OBJECTID:
return COLOR_ROOT;
case BTRFS_EXTENT_TREE_OBJECTID:
return COLOR_EXTENT;
case BTRFS_CHUNK_TREE_OBJECTID:
return COLOR_CHUNK;
case BTRFS_DEV_TREE_OBJECTID:
return COLOR_DEV;
case BTRFS_FS_TREE_OBJECTID:
return COLOR_FS;
case BTRFS_CSUM_TREE_OBJECTID:
return COLOR_CSUM;
case BTRFS_DATA_RELOC_TREE_OBJECTID:
return COLOR_RELOC;
}
return COLOR_UNKNOWN;
}
static void
init_colors(gdImagePtr im, int *colors)
{
colors[COLOR_ROOT] = gdImageColorAllocate(im, 255, 0, 0);
colors[COLOR_EXTENT] = gdImageColorAllocate(im, 0, 255, 0);
colors[COLOR_CHUNK] = gdImageColorAllocate(im, 255, 0, 0);
colors[COLOR_DEV] = gdImageColorAllocate(im, 255, 0, 0);
colors[COLOR_FS] = gdImageColorAllocate(im, 0, 0, 0);
colors[COLOR_CSUM] = gdImageColorAllocate(im, 0, 0, 255);
colors[COLOR_RELOC] = gdImageColorAllocate(im, 128, 128, 128);
colors[COLOR_DATA] = gdImageColorAllocate(im, 100, 0, 0);
colors[COLOR_UNKNOWN] = gdImageColorAllocate(im, 50, 50, 50);
}
int
list_fragments(int fd, u64 flags, char *dir)
{
int ret;
struct btrfs_ioctl_search_args args;
struct btrfs_ioctl_search_key *sk = &args.key;
int i;
struct btrfs_ioctl_search_header *sh;
unsigned long off = 0;
int bgnum = 0;
u64 bgstart = 0;
u64 bglen = 0;
u64 bgend = 0;
u64 bgflags = 0;
u64 bgused = 0;
u64 saved_extent = 0;
u64 saved_len = 0;
int saved_color = 0;
u64 last_end = 0;
u64 areas = 0;
long px;
char name[1000];
FILE *html;
int colors[COLOR_MAX];
gdImagePtr im = NULL;
int black = 0;
int width = 800;
snprintf(name, sizeof(name), "%s/index.html", dir);
html = fopen(name, "w");
if (!html) {
printf("unable to create %s\n", name);
exit(1);
}
fprintf(html, "<html><header>\n");
fprintf(html, "<title>Btrfs Block Group Allocation Map</title>\n");
fprintf(html, "<style type=\"text/css\">\n");
fprintf(html, "img {margin-left: 1em; margin-bottom: 2em;}\n");
fprintf(html, "</style>\n");
fprintf(html, "</header><body>\n");
memset(&args, 0, sizeof(args));
sk->tree_id = BTRFS_EXTENT_TREE_OBJECTID;
sk->max_type = -1;
sk->min_type = 0;
sk->max_objectid = (u64)-1;
sk->max_offset = (u64)-1;
sk->max_transid = (u64)-1;
/* just a big number, doesn't matter much */
sk->nr_items = 4096;
while(1) {
ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
if (ret < 0) {
fprintf(stderr, "ERROR: can't perform the search\n");
goto out_close;
}
/* the ioctl returns the number of item it found in nr_items */
if (sk->nr_items == 0)
break;
off = 0;
for (i = 0; i < sk->nr_items; i++) {
int j;
sh = (struct btrfs_ioctl_search_header *)(args.buf +
off);
off += sizeof(*sh);
if (btrfs_search_header_type(sh)
== BTRFS_BLOCK_GROUP_ITEM_KEY) {
struct btrfs_block_group_item *bg;
if (im) {
push_im(im, name, dir);
im = NULL;
print_bg(html, name, bgstart, bglen,
bgused, bgflags, areas);
}
++bgnum;
bg = (struct btrfs_block_group_item *)
(args.buf + off);
bgflags = btrfs_block_group_flags(bg);
bgused = btrfs_block_group_used(bg);
printf("found block group %lld len %lld "
"flags %lld\n",
btrfs_search_header_objectid(sh),
btrfs_search_header_offset(sh),
bgflags);
if (!(bgflags & flags)) {
/* skip this block group */
sk->min_objectid =
btrfs_search_header_objectid(sh) +
btrfs_search_header_offset(sh);
sk->min_type = 0;
sk->min_offset = 0;
break;
}
im = gdImageCreate(width,
(btrfs_search_header_offset(sh)
/ 4096 + 799) / width);
black = gdImageColorAllocate(im, 0, 0, 0);
for (j = 0; j < ARRAY_SIZE(colors); ++j)
colors[j] = black;
init_colors(im, colors);
bgstart = btrfs_search_header_objectid(sh);
bglen = btrfs_search_header_offset(sh);
bgend = bgstart + bglen;
snprintf(name, sizeof(name), "bg%d.png", bgnum);
last_end = bgstart;
if (saved_len) {
px = (saved_extent - bgstart) / 4096;
for (j = 0; j < saved_len / 4096; ++j) {
int x = (px + j) % width;
int y = (px + j) / width;
gdImageSetPixel(im, x, y,
saved_color);
}
last_end += saved_len;
}
areas = 0;
saved_len = 0;
}
if (im && btrfs_search_header_type(sh)
== BTRFS_EXTENT_ITEM_KEY) {
int c;
struct btrfs_extent_item *item;
item = (struct btrfs_extent_item *)
(args.buf + off);
if (use_color)
c = colors[get_color(item,
btrfs_search_header_len(sh))];
else
c = black;
if (btrfs_search_header_objectid(sh) > bgend) {
printf("WARN: extent %lld is without "
"block group\n",
btrfs_search_header_objectid(sh));
goto skip;
}
if (btrfs_search_header_objectid(sh) == bgend) {
saved_extent =
btrfs_search_header_objectid(sh);
saved_len =
btrfs_search_header_offset(sh);
saved_color = c;
goto skip;
}
px = (btrfs_search_header_objectid(sh)
- bgstart) / 4096;
for (j = 0;
j < btrfs_search_header_offset(sh) / 4096;
++j) {
int x = (px + j) % width;
int y = (px + j) / width;
gdImageSetPixel(im, x, y, c);
}
if (btrfs_search_header_objectid(sh) != last_end)
++areas;
last_end = btrfs_search_header_objectid(sh)
+ btrfs_search_header_offset(sh);
skip:;
}
off += btrfs_search_header_len(sh);
/*
* record the mins in sk so we can make sure the
* next search doesn't repeat this root
*/
sk->min_objectid = btrfs_search_header_objectid(sh);
sk->min_type = btrfs_search_header_type(sh);
sk->min_offset = btrfs_search_header_offset(sh);
}
sk->nr_items = 4096;
/* increment by one */
if (++sk->min_offset == 0)
if (++sk->min_type == 0)
if (++sk->min_objectid == 0)
break;
}
if (im) {
push_im(im, name, dir);
print_bg(html, name, bgstart, bglen, bgused, bgflags, areas);
}
if (use_color) {
fprintf(html, "<p>");
fprintf(html, "data - dark red, ");
fprintf(html, "fs tree - black, ");
fprintf(html, "extent tree - green, ");
fprintf(html, "csum tree - blue, ");
fprintf(html, "reloc tree - grey, ");
fprintf(html, "other trees - red, ");
fprintf(html, "unknown tree - dark grey");
fprintf(html, "</p>");
}
fprintf(html, "</body></html>\n");
out_close:
fclose(html);
return ret;
}
void fragments_usage(void)
{
printf("usage: btrfs-fragments [options] <path>\n");
printf(" -c use color\n");
printf(" -d print data chunks\n");
printf(" -m print metadata chunks\n");
printf(" -s print system chunks\n");
printf(" (default is data+metadata)\n");
printf(" -o <dir> output directory, default is html\n");
exit(1);
}
int main(int argc, char **argv)
{
char *path;
int fd;
int ret;
u64 flags = 0;
char *dir = "html";
DIR *dirstream = NULL;
while (1) {
int c = getopt(argc, argv, "cmso:h");
if (c < 0)
break;
switch (c) {
case 'c':
use_color = 1;
break;
case 'd':
flags |= BTRFS_BLOCK_GROUP_DATA;
break;
case 'm':
flags |= BTRFS_BLOCK_GROUP_METADATA;
break;
case 's':
flags |= BTRFS_BLOCK_GROUP_SYSTEM;
break;
case 'o':
dir = optarg;
break;
case 'h':
default:
fragments_usage();
}
}
set_argv0(argv);
if (check_argc_min(argc - optind, 1))
fragments_usage();
path = argv[optind++];
fd = btrfs_open_dir(path, &dirstream, 1);
if (fd < 0)
exit(1);
if (flags == 0)
flags = BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA;
ret = list_fragments(fd, flags, dir);
close_file_or_dir(fd, dirstream);
if (ret)
exit(1);
exit(0);
}