-
Notifications
You must be signed in to change notification settings - Fork 6
/
iterator.c
298 lines (238 loc) · 6.02 KB
/
iterator.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
// Iterator.c ver 3.1
//Last Update: Added Bash Commands to get PBA
#include<sys/ioctl.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/fs.h>
#include <assert.h>
#include <dirent.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
//Directory Looping Function
static int ptree(char *curpath, char * const path);
//LBA Extraction Function
void fibmap_fun(char name[20]);
int caller=0;//for choosing between printing the name only or with details
void extract_pba();
unsigned long int pba[830000];
FILE *output;
int main(int argc, char * const argv[])
{
//Step0: Clear Trace File
system("echo > /sys/kernel/debug/tracing/trace");
//Step1 : Check trace file size
FILE *f1;
char c[25];
int i=0;
long val;
system("cat /sys/kernel/debug/tracing/buffer_size_kb > buffer_size.txt");
f1=fopen("buffer_size.txt","r");
while((c[i] = fgetc(f1)) != EOF) i++;
c[i+1]='\0';
fclose(f1);
char *p=c;
while (*p)
{
if ( isdigit(*p) || ( (*p=='-'||*p=='+') && isdigit(*(p+1)) ))
{
val = strtol(p, &p, 10);
// printf("%ld\n", val);
}
else
{
p++;
}
}
//Step2: Resize Trace File if Necessary
if(val==1410) system("echo 780000 > /sys/kernel/debug/tracing/buffer_size_kb");
//Step3: Create PBLK Instance
int create_error = system("sudo nvme lnvm create -d nvme0n1 -n mydevice -t pblk -b 0 -e 3");
if(create_error != 0 ){
system("sudo umount /mnt/nvme");
system("sudo nvme lnvm remove -n mydevice");
system("sudo nvme lnvm create -d nvme0n1 -n mydevice -t pblk -b 0 -e 3");
printf("mydevice is newly created.\n-------------------------------\n\n");
}
system("sudo mount /dev/mydevice /mnt/nvme");
//Step4: invoke pblk_sysfs.c functions by check test_kdy
system("cat /sys/block/mydevice/pblk/test_kdy");
//Step5: EXTRACT PBA from trace file -> make a function and call it inside fibmap
extract_pba();
system("echo > output.txt");
output = fopen("output.txt", "w");
if(output == NULL){
printf("File pointer error!\n\n");
exit(1);
}
int k;
int rval;
for (rval = 0, k = 1; k < argc; k++)
if (ptree(NULL, argv[k]) != 0)
rval = 1;
//calling one more time to print details
fprintf(output, "END\n");
caller=1;
rval=0;
for (rval = 0, k = 1; k < argc; k++)
if (ptree(NULL, argv[k]) != 0)
rval = 1;
//Step6: Remove PBLK
system("sudo umount /mnt/nvme");
system("sudo nvme lnvm remove -n mydevice");
return rval;
}
static int ptree(char *curpath, char * const path)
{
char ep[PATH_MAX];
char p[PATH_MAX];
DIR *dirp;
struct dirent entry;
struct dirent *endp;
struct stat st;
if (curpath != NULL)
snprintf(ep, sizeof(ep), "%s/%s", curpath, path);
else
snprintf(ep, sizeof(ep), "%s", path);
if (stat(ep, &st) == -1)
return -1;
if ((dirp = opendir(ep)) == NULL)
return -1;
//main loop for iterating through the file
for (;;) {
endp = NULL;
if (readdir_r(dirp, &entry, &endp) == -1) {
closedir(dirp);
return -1;
}
if (endp == NULL)
break;
assert(endp == &entry);
if (strcmp(entry.d_name, ".") == 0 ||
strcmp(entry.d_name, "..") == 0)
continue;
if (curpath != NULL)
snprintf(ep, sizeof(ep), "%s/%s/%s", curpath,
path, entry.d_name);
else
snprintf(ep, sizeof(ep), "%s/%s", path,
entry.d_name);
if (stat(ep, &st) == -1) {
closedir(dirp);
return -1;
}
if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))//main condition
{
char new_name[50];//increase the size if the names are too long
int counter=0;
int counter2=0;
while(ep[counter]!='\0')
{
new_name[counter2]=ep[counter];
counter2++;
counter++;
}
new_name[counter2]='\0';
//loop backward until the code finds a backslash / -> use new_name for full name with path notation
//1) find the size of new_name ==counter2
char directory_file= S_ISDIR(st.st_mode) ? 'd' : 'f';
if(directory_file=='f')//if file
{
int k=0;
for(k=counter2;;k--)
{
if(new_name[k]=='/')
{
k++;
break;
}
}
while(k<=counter2)
{
fprintf(output,"%c",new_name[k]);
k++;
}
fprintf(output, "\n");
if(caller==1)
{
fibmap_fun(new_name);
}
}
}
if (S_ISDIR(st.st_mode) == 0)
continue;
if (curpath != NULL)
snprintf(p, sizeof(p), "%s/%s", curpath, path);
else
snprintf(p, sizeof(p), "%s", path);
snprintf(ep, sizeof(ep), "%s", entry.d_name);
ptree(p, ep);
}
closedir(dirp);
return 0;
}
void fibmap_fun(char file_name[20])
{
int fd, i, block, blocksize, blkcnt;
struct stat st;
assert(file_name != NULL);
fd = open(file_name, O_RDONLY);
if (fd <= 0) {
perror("error opening file");
goto end;
}
if (ioctl(fd, FIGETBSZ, &blocksize)) {
perror("FIBMAP ioctl failed");
goto end;
}
if (fstat(fd, &st)) {
perror("fstat error");
goto end;
}
blkcnt = (st.st_size + blocksize - 1) / blocksize;
for (i = 0; i < blkcnt; i++) {
block = i;
if (ioctl(fd, FIBMAP, &block)) {
perror("FIBMAP ioctl failed");
}
if(pba[block-1000] == -1) pba[block-1000] = 4294967295;
fprintf(output, "%d %ld\n", block,pba[block-1000]);
}
end:
fprintf(output, "END\n");
close(fd);
}
void extract_pba(){
int fd = open("/sys/kernel/debug/tracing/trace", O_RDONLY);
// int fd = open("sample", O_RDONLY);
int idx = 0;
char buf [100];
int line = 1, cell = 0;
int i = 0;
if(fd == -1) printf("FD error\n");
while(read(fd, buf+idx, 1)){
if(buf[idx]=='\n'){//newline
if(line > 11){
*(buf + idx + 1) = '\0';
//printf("num %d:%s", line, buf + 65);
char* ptr = strtok(buf + 65, " ");
ptr = strtok(NULL," ");
pba[i] = atoi(ptr);
i++;
idx = 0;
line++;
}
else{
*(buf + idx + 1) = '\0';
idx = 0;
line++;
}
}
else{
idx++;
}
}
}