-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfraggy.agg.c
220 lines (176 loc) · 5.85 KB
/
fraggy.agg.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
/*******************************************************************************
* get fragmentation distribution of a filesystem,
* which implements `FIEMAP` as an io system call.
*
* @author Lars Thoms
* @date 2020-01-24
******************************************************************************/
#include "fraggy.agg.h"
/*******************************************************************************
* FIEMAP
******************************************************************************/
static int filefrag_fiemap(long fd, unsigned int *num_extents)
{
__u64 buf[2048];
struct fiemap* fiemap = (struct fiemap*) buf;
struct fiemap_extent* fm_ext = &fiemap->fm_extents[0];
struct fiemap_extent fm_last;
int count = (sizeof(buf) - sizeof(*fiemap)) / sizeof(struct fiemap_extent);
unsigned long long expected = 0, expected_dense = 0;
unsigned long flags = 0;
unsigned int tot_extents = 0, i;
int n = 0, last = 0, rc;
memset(fiemap, 0, sizeof(struct fiemap));
memset(&fm_last, 0, sizeof(fm_last));
do
{
fiemap->fm_length = ~0ULL;
fiemap->fm_flags = flags;
fiemap->fm_extent_count = count;
rc = ioctl(fd, FS_IOC_FIEMAP, (unsigned long) fiemap);
if(rc < 0)
{
static int fiemap_incompat_printed;
rc = -errno;
if(rc == -EBADR && !fiemap_incompat_printed)
{
fprintf(stderr, "FIEMAP failed with unknown flags %x\n", fiemap->fm_flags);
fiemap_incompat_printed = 1;
}
return rc;
}
/* If 0 extents are returned, then more ioctls are not needed */
if(fiemap->fm_mapped_extents == 0)
{
break;
}
for (i = 0; i < fiemap->fm_mapped_extents; i++)
{
expected_dense = fm_last.fe_physical + fm_last.fe_length;
expected = fm_last.fe_physical + fm_ext[i].fe_logical - fm_last.fe_logical;
if((fm_ext[i].fe_logical != 0) &&
(fm_ext[i].fe_physical != expected) &&
(fm_ext[i].fe_physical != expected_dense))
{
tot_extents++;
}
else
{
expected = 0;
if(!tot_extents)
{
tot_extents = 1;
}
}
if(fm_ext[i].fe_flags & FIEMAP_EXTENT_LAST)
{
last = 1;
}
fm_last = fm_ext[i];
n++;
}
fiemap->fm_start = (fm_ext[i - 1].fe_logical + fm_ext[i - 1].fe_length);
}
while(last == 0);
*num_extents = tot_extents;
return 0;
}
/*******************************************************************************
* node handler
******************************************************************************/
static int node_fragmentation(const char *node_path, const struct stat *node_info,
const int node_typeflag, struct FTW *node_pathinfo)
{
unsigned int num_extents = 1;
int rc = 0;
// Given path is a not readable directory
if(node_typeflag == FTW_DNR)
{
return -(errno = EACCES);
}
if((node_typeflag == FTW_F) || (node_typeflag == FTW_D) || (node_typeflag == FTW_DP))
{
// Initialize file descriptor
long file = open(node_path, O_RDONLY);
// Check for possible failures
if(file < 0)
{
perror(node_path);
return -errno;
}
rc = filefrag_fiemap(file, &num_extents);
close(file);
// Empty files are not interesting
if((rc == 0) && (num_extents > 0))
{
increase_distribution(num_extents);
}
}
return -(errno = rc);
}
/*******************************************************************************
* usage
******************************************************************************/
static void print_usage(const char *program_name)
{
fprintf(stderr, "Usage: %1$s [OPTIONS] [ FILE | DIRECTORY ]\n"
"Get a list of all amount of extents per node in filesystem\n\n"
"OPTIONS\n"
" -i Ignore file errors\n\n"
"STDOUT\n"
" Comma separated fragmentation distribution\n"
" Output redirection on the shell is required to dump data to a file.\n"
" E.g.: %1$s /mnt 1> frag.csv\n", program_name);
}
/*******************************************************************************
* main application
******************************************************************************/
int main(int argc, char *argv[])
{
int option;
int iflag = 0;
int rc;
/* get options */
while((option = getopt (argc, argv, "hi")) != -1)
{
switch(option)
{
case 'h':
print_usage(argv[0]);
return EXIT_FAILURE;
case 'i':
iflag = 1;
break;
default:
abort();
}
}
/* get directory path */
if(optind < argc)
{
/* check for invalid directory path */
if(argv[optind] == NULL || *argv[optind] == '\0')
{
fprintf(stderr, "%s.\n", strerror(errno = EINVAL));
return EXIT_FAILURE;
}
rc = nftw(argv[optind], node_fragmentation, USE_FDS, FTW_PHYS);
if(rc)
{
fprintf(stderr, "%s.\n", strerror(errno = rc));
if(!iflag)
{
return EXIT_FAILURE;
}
}
}
/* print usage*/
else
{
print_usage(argv[0]);
return EXIT_FAILURE;
}
/* print distribution to stdout */
print_distribution();
return EXIT_SUCCESS;
}