-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuse_lfs.c
334 lines (297 loc) · 8.39 KB
/
fuse_lfs.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
#define FUSE_USE_VERSION 30
#include <fuse_lowlevel.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
#include <sys/time.h>
#include <time.h>
FILE *log_fp;
char *log_file_path;
char *stats_file_path="stats.txt";
int fs_size;
int fs_read=0;
int fs_write=0;
int gc_called=0;
int gc_write=0;
time_t mount;
time_t unmount;
int block_size = 4096;
int num_blocks;
int free_space;
int offset =0;
typedef struct meta_data{
char *file_name;
char *file_type;
char *parent;
int children;
char *created;
char *updated;
int data_size;
int block_offset;
}Meta_data;
Meta_data *dictionary;
int curr_block=3;
static const char *hello_str = "Hello World!\n";
static const char *hello_name = "hello";
static int lfs_stat(fuse_ino_t ino, struct stat *stbuf){
stbuf->st_ino = ino;
switch (ino) {
case 1:
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
break;
case 2:
stbuf->st_mode = S_IFREG | 0444;
stbuf->st_nlink = 1;
stbuf->st_size = strlen(hello_str);
break;
default:
return -1;
}
return 0;
}
static void lfs_ll_getattr(fuse_req_t req, fuse_ino_t ino,
struct fuse_file_info *fi){
struct stat stbuf;
(void) fi;
memset(&stbuf, 0, sizeof(stbuf));
if (lfs_stat(ino, &stbuf) == -1)
fuse_reply_err(req, ENOENT);
else
fuse_reply_attr(req, &stbuf, 1.0);
}
static void lfs_ll_lookup(fuse_req_t req, fuse_ino_t parent, const char *name){
struct fuse_entry_param e;
if (parent != 1 || strcmp(name, hello_name) != 0)
fuse_reply_err(req, ENOENT);
else {
memset(&e, 0, sizeof(e));
e.ino = 2;
e.attr_timeout = 1.0;
e.entry_timeout = 1.0;
lfs_stat(e.ino, &e.attr);
fuse_reply_entry(req, &e);
}
}
struct dirbuf {
char *p;
size_t size;
};
static void dirbuf_add(fuse_req_t req, struct dirbuf *b, const char *name,
fuse_ino_t ino){
struct stat stbuf;
size_t oldsize = b->size;
b->size += fuse_add_direntry(req, NULL, 0, name, NULL, 0);
b->p = (char *) realloc(b->p, b->size);
memset(&stbuf, 0, sizeof(stbuf));
stbuf.st_ino = ino;
fuse_add_direntry(req, b->p + oldsize, b->size - oldsize, name, &stbuf,
b->size);
}
#define min(x, y) ((x) < (y) ? (x) : (y))
static int reply_buf_limited(fuse_req_t req, const char *buf, size_t bufsize,
off_t off, size_t maxsize){
if (off < bufsize)
return fuse_reply_buf(req, buf + off,
min(bufsize - off, maxsize));
else
return fuse_reply_buf(req, NULL, 0);
}
static void lfs_ll_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
off_t off, struct fuse_file_info *fi){
(void) fi;
if (ino != 1)
fuse_reply_err(req, ENOTDIR);
else {
struct dirbuf b;
memset(&b, 0, sizeof(b));
dirbuf_add(req, &b, ".", 1);
dirbuf_add(req, &b, "..", 1);
dirbuf_add(req, &b, hello_name, 2);
reply_buf_limited(req, b.p, b.size, off, size);
free(b.p);
}
}
static void lfs_ll_open(fuse_req_t req, fuse_ino_t ino,
struct fuse_file_info *fi){
if (ino != 2)
fuse_reply_err(req, EISDIR);
else if ((fi->flags & 3) != O_RDONLY)
fuse_reply_err(req, EACCES);
else
fuse_reply_open(req, fi);
}
static void lfs_ll_read(fuse_req_t req, fuse_ino_t ino, size_t size,
off_t off, struct fuse_file_info *fi){
(void) fi;
assert(ino == 2);
reply_buf_limited(req, hello_str, strlen(hello_str), off, size);
}
static void lfs_ll_create(fuse_req_t req, fuse_ino_t parent, const char *name,
mode_t mode, struct fuse_file_info *fi){
/* Meta_data new_blk;
time_t created_t;
//new_blk.file_name=fi->fh;
struct tm *ts;
char buf[80];
created_t = time(NULL);
tsm = localtime(&created_t);
strftime(buf1, sizeof(buf), "%a %Y-%m-%d %H:%M:%S %Z", ts);
printf("%s",fi->fh);
if(ino != 1)
fuse_reply_err(req,ENOENT);
else
//pwrite(log_fp, buf, size, offset);*/
fuse_reply_write(req,fi);
}
static void lfs_ll_write(fuse_req_t req, fuse_ino_t ino, const char *buf,
size_t size, off_t off, struct fuse_file_info *fi){
/*Meta_data new_blk;
//new_blk.file_name=fi->fh;
printf("%s",fi->fh);
if(ino != 1)
fuse_reply_err(req,ENOENT);
else
//pwrite(log_fp, buf, size, offset);*/
fuse_reply_write(req,fi);
}
static struct fuse_lowlevel_ops lfs_ll_oper = {
.lookup = lfs_ll_lookup,
.getattr = lfs_ll_getattr,
.readdir = lfs_ll_readdir,
.open = lfs_ll_open,
.read = lfs_ll_read,
.write = lfs_ll_write,
.create = lfs_ll_create,
};
int write_stats(){
FILE *stat_fp;
int n;
char buff[300];
struct tm *tsm,*tsu;
char buf1[80];
char buf2[80];
tsm = localtime(&mount);
strftime(buf1, sizeof(buf1), "%a %Y-%m-%d %H:%M:%S %Z", tsm);
tsu = localtime(&unmount);
strftime(buf2, sizeof(buf2), "%a %Y-%m-%d %H:%M:%S %Z", tsu);
n=sprintf(buff,"Mount time:%s\nfs_size: %dMB\nfs_read:%dMB\nfs_write:%dMB\ngc_called:%d\ngc_write:%dMB\nUnmount\
time:%s\n",buf1,(fs_size/1000000),fs_read,fs_write,
gc_called,gc_write,buf2);
stat_fp = fopen (stats_file_path,"a+");
if (stat_fp == NULL) {
printf ("error opening stats\n");
return 1;
}
fseek(stat_fp,0l,SEEK_END);
fwrite(buff,sizeof(char),n,stat_fp);
fclose(stat_fp);
return 0;
}
int initialize_dictionary(void){
char * line = NULL;
size_t len = 0;
ssize_t read;
char *tok;
int count=0;
fseek(log_fp,0l,SEEK_END);
if(ftell(log_fp) == 0){ // new file system
return -1;
}else{
rewind(log_fp);
fseek(log_fp,0l,free_space);
while(!feof(log_fp)){
while((read = getline(&line, &len, log_fp)) != -1);// read line
if(strcmp(line,"<meta_data>") == 0){
Meta_data md;
while(1){
while((read = getline(&line, &len, log_fp)) != -1);// read line
if(strcmp(line,"</meta_data>") == 0){
dictionary[count]=md;
count++;
break;
}else{
tok=strtok(line,":");
if(strcmp("filename",&tok[0]) == 0){
md.file_name = &tok[1];
}else if(strcmp("filetype",&tok[0])== 0){
md.file_type = &tok[1];
}else if(strcmp("parent",&tok[0])== 0){
md.parent = &tok[1];
}else if(strcmp("created",&tok[0])== 0){
md.created = &tok[1];
}else if(strcmp("updated",&tok[0])== 0){
md.updated = &tok[1];
}else if(strcmp("datasize",&tok[0])== 0){
md.data_size = tok[1];
}else if(strcmp("children",&tok[0])== 0){
md.children = tok[1];
}else if(strcmp("offset",&tok[0])== 0){
md.block_offset = tok[1];
}
}
}
}
}
}
return 0;
}
void gabage_collector(){
gc_called++;
// to be implemented
}
int main(int argc, char *argv[]){
// Initialize an empty argument list
struct fuse_args mountpt = FUSE_ARGS_INIT(0, NULL);
// add program and mountpoint
fuse_opt_add_arg(&mountpt, argv[0]);
fuse_opt_add_arg(&mountpt, argv[1]);
//fuse_opt_add_arg(&mountpt, argv[2]);// for debug
//fuse_opt_add_arg(&mountpt, argv[3]);// for debug
log_file_path= argv[2];
fs_size=atoi(argv[3])*1000000;
num_blocks=fs_size/block_size;
if(num_blocks < 1000){
free_space =block_size;
}else
free_space=block_size*4;
dictionary= malloc(num_blocks*sizeof(int));
log_fp = fopen (log_file_path,"a+");
if (log_fp == NULL) {
printf ("Data file could not be opened\n");
return 1;
}
initialize_dictionary();
mount=time(NULL);
struct fuse_args args = FUSE_ARGS_INIT(mountpt.argc, mountpt.argv);
struct fuse_chan *ch;
char *mountpoint;
int err = -1;
if (fuse_parse_cmdline(&args, &mountpoint, NULL, NULL) != -1 &&
(ch = fuse_mount(mountpoint, &args)) != NULL) {
struct fuse_session *se;
se = fuse_lowlevel_new(&args, &lfs_ll_oper,
sizeof(lfs_ll_oper), NULL);
if (se != NULL) {
if (fuse_set_signal_handlers(se) != -1) {
fuse_session_add_chan(se, ch);
/* Block until ctrl+c or fusermount -u */
err = fuse_session_loop(se);
fuse_remove_signal_handlers(se);
fuse_session_remove_chan(ch);
}
fuse_session_destroy(se);
unmount=time(NULL);
write_stats();
}
fuse_unmount(mountpoint, ch);
}
fuse_opt_free_args(&args);
free(dictionary);
fclose(log_fp);
return err ? 1 : 0;
}