-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint.c
366 lines (295 loc) · 10.7 KB
/
print.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
/*
* SPDX-License-Identifier: ISC
* SPDX-URL: https://spdx.org/licenses/ISC.html
*
* Copyright (C) 2023-2024 Aaron M. D. Jones <[email protected]>
*/
#include <inttypes.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/fiemap.h>
#include "filemap.h"
static const char * FM_RETURNS_NONNULL
fm_readable_size(const enum fm_readable_which which, const uint64_t insize)
{
static const size_t result_buflen = 128U;
static char result_offset[128U];
static char result_length[128U];
static char result_size[128U];
static char result_gap[128U];
bool do_readable = false;
char *result = NULL;
switch (which)
{
case FM_READABLE_OFFSET:
do_readable = fm_readable_offsets;
result = result_offset;
break;
case FM_READABLE_LENGTH:
do_readable = fm_readable_lengths;
result = result_length;
break;
case FM_READABLE_SIZE:
do_readable = fm_readable_sizes;
result = result_size;
break;
case FM_READABLE_GAP:
do_readable = fm_readable_gaps;
result = result_gap;
break;
}
(void) memset(result, 0x00, result_buflen);
if (do_readable)
{
static const char *suffixes[] = { " B", "KiB", "MiB", "GiB", "TiB", "PiB" };
long double insized = (long double) insize;
unsigned int suffidx = 0U;
while (insized >= 1024 && suffidx < 6U)
{
insized /= 1024;
suffidx++;
}
(void) snprintf(result, result_buflen, "%.2Lf %s", insized, suffixes[suffidx]);
}
else
(void) snprintf(result, result_buflen, "%" PRIu64, insize);
return result;
}
static const char * FM_NONNULL(1) FM_RETURNS_NONNULL
fm_build_inode_flags(const struct fm_inode *const restrict inode)
{
static char result[16U];
(void) memset(result, 0x00, sizeof result);
if (inode->flags & FM_IFLAGS_UNALIGNED)
// Data is not aligned
(void) strcat(result, "A");
if ((inode->sb.st_mode & S_IFMT) == S_IFDIR)
// This inode is a directory
(void) strcat(result, "D");
if (inode->flags & FM_IFLAGS_FRAGMENTED || inode->extcount != 1U)
// Data is not contiguous
(void) strcat(result, "F");
if (inode->namecount > 1U)
// This inode has multiple filenames (hardlinks)
(void) strcat(result, "L");
if (inode->extcount > 1U)
// Data is made up of multiple extents
(void) strcat(result, "M");
if (inode->flags & FM_IFLAGS_UNORDERED)
// Data is not in order
(void) strcat(result, "U");
return result;
}
static const char * FM_NONNULL(1) FM_RETURNS_NONNULL
fm_build_extent_flags(const struct fm_extent *const restrict extent)
{
static char result[16U];
(void) memset(result, 0x00, sizeof result);
if (extent->flags & FIEMAP_EXTENT_NOT_ALIGNED)
// Extent offset and/or length is not aligned (not a multiple of the filesystem block size)
(void) strcat(result, "A");
if (extent->inode->extcount > 1U && extent->pos != extent->inode->extcount)
// Data is made up of multiple extents; this is not the last; data continues after this
(void) strcat(result, "C");
if (extent->flags & FIEMAP_EXTENT_DELALLOC)
/* Delayed allocation; the block allocator is waiting for more data
* and/or looking for a suitable space to put the data it has
*/
(void) strcat(result, "D");
if (extent->flags & FIEMAP_EXTENT_LAST)
// This is the last extent (whether the data is made up of multiple extents or not)
(void) strcat(result, "E");
if (extent->flags & FIEMAP_EXTENT_DATA_INLINE)
// Extent is located within a metadata block; inline allocation
(void) strcat(result, "I");
if (extent->flags & FIEMAP_EXTENT_MERGED)
/* Filesystem does not support extents or this file is not using them;
* the kernel has merged contiguous filesystem data blocks into a
* pseudo extent for us instead
*/
(void) strcat(result, "M");
if (extent->flags & FIEMAP_EXTENT_DATA_TAIL)
// Extent contains data from multiple files
(void) strcat(result, "T");
if (extent->flags & FIEMAP_EXTENT_UNKNOWN)
// No storage has been allocated for this extent yet
(void) strcat(result, "U");
if (extent->flags & FIEMAP_EXTENT_UNWRITTEN)
/* Extent allocated but not initialised; reading from a file descriptor
* will return zeroes, but reading this extent directly from the volume
* may return different, possibly nonsensical data
*/
(void) strcat(result, "W");
if (extent->flags & FIEMAP_EXTENT_ENCODED)
/* This extent contains data that is encoded somehow (compressed, encrypted, ...);
* reading from a file descriptor will work normally, but reading this extent
* directly from the volume will return different data
*/
(void) strcat(result, "X");
return result;
}
void FM_NONNULL(1) FM_PRINTF(1, 2)
fm_print_message(const char *const restrict fmt, ...)
{
va_list argp;
va_start(argp, fmt);
if (isatty(STDERR_FILENO) == 1)
{
(void) fprintf(stderr, "\033[2K\r");
(void) vfprintf(stderr, fmt, argp);
(void) fflush(stderr);
}
va_end(argp);
}
void
fm_print_results(void)
{
const struct fm_name *fname;
uint64_t fragged_extents = 0U;
uint64_t fragged_inodes = 0U;
uint64_t prev_extoff = 0U;
uint64_t prev_extlen = 0U;
struct fm_extent *extent;
struct fm_extent *etmp;
struct fm_inode *inode;
struct fm_inode *itmp;
(void) fm_print_message("");
if (! fm_extent_count)
return;
HASH_ITER(hh, fm_inodes, inode, itmp)
{
if (! (inode->flags & FM_IFLAGS_FRAGMENTED))
continue;
fragged_extents += inode->extcount;
fragged_inodes++;
}
if (fm_skip_preamble)
goto results;
const long double inofragpcnt = 100.0 * (((long double) fragged_inodes) / ((long double) fm_inode_count));
const long double extfragratio = (((long double) fragged_extents) / ((long double) fragged_inodes));
if (! (fm_fragmented_only && ! fragged_inodes))
{
// Only print information about interpreting upcoming extents if we are going to print any extents
if (fm_readable_offsets)
(void) printf("Extent offsets are in ....... : human-readable units\n");
else if (fm_integral_blksz)
(void) printf("Extent offsets are in ....... : multiples of filesystem blocks "
"(%" PRIu64 " bytes)\n", fm_blksz);
else
(void) printf("Extent offsets are in ....... : bytes\n");
if (fm_readable_lengths)
(void) printf("Extent lengths are in ....... : human-readable units\n");
else if (fm_integral_blksz)
(void) printf("Extent lengths are in ....... : multiples of filesystem blocks "
"(%" PRIu64 " bytes)\n", fm_blksz);
else
(void) printf("Extent lengths are in ....... : bytes\n");
if (fm_readable_sizes)
(void) printf("File sizes are in ........... : human-readable units\n");
else
(void) printf("File sizes are in ........... : bytes\n");
}
if (fm_scan_directories)
(void) printf("Mapped ...................... : %" PRIu64 " files & %" PRIu64 " dirs (%" PRIu64
" inodes) consisting of %" PRIu64 " extents\n", fm_file_count, fm_dir_count,
fm_inode_count, fm_extent_count);
else
(void) printf("Mapped ...................... : %" PRIu64 " files (%" PRIu64 " inodes) consisting "
"of %" PRIu64 " extents\n", fm_file_count, fm_inode_count, fm_extent_count);
if (fragged_inodes)
(void) printf("Fragmented inodes ........... : %" PRIu64 "/%" PRIu64 " (%.2Lf%%); average %.2Lf "
"extents per fragmented inode\n", fragged_inodes, fm_inode_count, inofragpcnt,
extfragratio);
if (fm_fragmented_only)
{
const char *const fwhich = ((fm_scan_directories) ? "files & dirs" : "files");
(void) printf("\n");
if (fragged_inodes)
(void) printf("Requested to show only fragmented %s\n", fwhich);
else
(void) printf("Requested to show only fragmented %s; however, there are none\n", fwhich);
}
results:
if (fm_fragmented_only && ! fragged_inodes)
return;
if (! fm_names_only)
{
(void) printf("\n");
(void) printf("%20s %20s %12s %12s %12s %12s %20s %s\n", "Extent Offset", "Extent Length",
"Extent Count", "Extent Flags", "Inode Number", "Inode Flags", "File Size",
"File Name(s)");
(void) printf("-------------------- -------------------- ------------ ------------ "
"------------ ------------ -------------------- ------------\n\n");
}
HASH_ITER(hh, fm_extents, extent, etmp)
{
if (fm_fragmented_only && ! (extent->inode->flags & FM_IFLAGS_FRAGMENTED))
continue;
if (fm_print_gaps && prev_extoff && (prev_extoff + prev_extlen) < extent->off && ! fm_names_only)
{
const uint64_t gap = (extent->off - (prev_extoff + prev_extlen));
(void) printf("%20s %-20s %12s %12s %12s %12s %20s\n",
" ", fm_readable_size(FM_READABLE_GAP, gap), " ", " ", " ", " ", " ");
}
DL_FOREACH(extent->inode->names, fname)
{
if (fm_names_only)
{
if (! (extent->inode->flags & FM_IFLAGS_PRINTED))
{
(void) printf("%s", fname->name);
if (fm_names_zero)
(void) putchar('\0');
else
(void) putchar('\n');
}
continue;
}
if (fname == extent->inode->names)
{
const uint64_t extoff = ((fm_integral_blksz && ! fm_readable_offsets) ? \
(extent->off / fm_blksz) : extent->off);
const uint64_t extlen = ((fm_integral_blksz && ! fm_readable_lengths) ? \
(extent->len / fm_blksz) : extent->len);
const char *const inoflags = fm_build_inode_flags(extent->inode);
const char *const extflags = fm_build_extent_flags(extent);
const uint64_t fsize = (uint64_t) extent->inode->sb.st_size;
char extpos[128U];
(void) memset(extpos, 0x00, sizeof extpos);
(void) snprintf(extpos, sizeof extpos, "%" PRIu64 "/%" PRIu64,
extent->pos, extent->inode->extcount);
// Print full details for the first file name pointing to this inode
(void) printf("%20s %20s %12s %12s %12" PRIu64 " %12s %20s %s\n",
fm_readable_size(FM_READABLE_OFFSET, extoff),
fm_readable_size(FM_READABLE_LENGTH, extlen),
extpos, extflags, extent->inode->inum, inoflags,
fm_readable_size(FM_READABLE_SIZE, fsize),
fname->name);
}
else if (! (extent->inode->flags & FM_IFLAGS_PRINTED))
{
/* Print only the file name for other file names pointing to this inode,
* but only if we have not yet done so for this inode already
*/
(void) printf("%20s %20s %12s %12s %12s %12s %20s %s\n",
"-----", " ", " ", " ", " ", " ", " ", fname->name);
}
else
{
// We have already printed other file names for this inode, skip doing so
(void) printf("%20s %20s %12s %12s %12s %12s %20s %s\n",
"+++++", " ", " ", " ", " ", " ", " ", "+++++");
break;
}
}
extent->inode->flags |= FM_IFLAGS_PRINTED;
prev_extoff = extent->off;
prev_extlen = extent->len;
(void) fflush(stdout);
}
}