-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.c
544 lines (434 loc) · 13.7 KB
/
file.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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/*
* Operating System Lab
* Lab4 (File System in Userspace)
* Copyright (C) 2017 Juhyung Son <[email protected]>
* First Writing: 30/12/2016
*
* file.c :
* - ?
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope 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.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <fuse.h>
#include <ulockmgr.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/file.h>
#include <dirent.h>
#include "include/lab4_fs_types.h"
int lab4_allocate_open_file_table(IOM *iom)
{
struct lab4_super_block *sb = IOM_SB(iom);
struct lab4_sb_info *sbi = IOM_SB_I(iom);
int i = 0, fid = -1;
for (i = START_OPEN_FILE; i < MAX_OPEN_FILE; i++)
{
if (sbi->file_table[i].used == FALSE)
{
fid = i;
break;
}
}
return fid;
}
int lab4_seek(struct lab4_super_block *sb, struct lab4_file_table *ft,long offset, int position)
{
if (position == SEEK_SET) /* SEEK_SET */
ft->rwoffset = offset;
else if (position == SEEK_CUR) /* SEEK_CUR */
ft->rwoffset += offset;
else if (position == SEEK_END) /* SEEK_END */
ft->rwoffset = ft->size - offset;
return LAB4_SUCCESS;
}
int lab4_createfile(IOM *iom, INODE *parent_inode,char *filename, INODE **new_inode_ptr, mode_t mode){
struct lab4_super_block *sb = IOM_SB(iom);
struct lab4_sb_info *sbi = IOM_SB_I(iom);
DIRENTRY *dir_new_entry, *dir_default_entry ,*tmp_dir_entry, dir_entry;
INODE *new_inode;
long blocknr_inbmap=0, read_bytes=0, parent_dir_size=0;
char *buf_new_dir_area, *buf_parent_dir_area;
int cluster_size = sbi->cluster_size, parent_dir_blocknr;
int res = LAB4_ERROR, isdelete = 0, i;
if (strlen(filename) < 1 || strlen(filename) >= FNAME_SIZE) {
return -ENAMETOOLONG;
}
res = lab4_lookup(iom, parent_inode->i_ino, &dir_entry,filename,&isdelete);
if(res == LAB4_SUCCESS)
return -EEXIST;
if (parent_inode->i_links_count == MAX_FILES_PER_DIR)
{
/* directory full */
return LAB4_ERROR;
}
new_inode = alloc_new_inode(iom);
if(new_inode == NULL){
restore_ibmap(iom);
return LAB4_ERROR;
}
new_inode->i_type = LAB4_TYPE_FILE;
new_inode->i_size = 0;
new_inode->i_mode = mode;
new_inode->i_gid = getgid();
new_inode->i_uid = getuid();
new_inode->i_links_count = 1;
new_inode->i_atime = time(NULL);
new_inode->i_ctime = time(NULL);
new_inode->i_mtime = time(NULL);
#if 0
if (S_ISCHR(mode) || S_ISBLK(mode)) {
if (old_valid_dev(dev))
new_inode->i_blocks[0] = old_encode_dev(dev);
else
new_inode->i_blocks[1] = new_encode_dev(dev);
}
#endif
parent_dir_size = parent_inode->i_size;
parent_dir_blocknr = parent_inode->i_blocks[0];
dir_new_entry = (DIRENTRY*)malloc(LAB4_DIR_ENTRY_SIZE);
if(!dir_new_entry)
goto LAB4_FREE1;
memset(dir_new_entry, 0x0, LAB4_DIR_ENTRY_SIZE);
strcpy(dir_new_entry->d_filename,filename);
dir_new_entry->d_ino = new_inode->i_ino;
dir_new_entry->d_flag = LAB4_DIR_USED;
dir_new_entry->d_version = new_inode->i_version;
buf_parent_dir_area = (char*)malloc(cluster_size);
if(!buf_parent_dir_area)
goto LAB4_FREE2;
memset(buf_parent_dir_area, 0x0 , cluster_size);
res = pread(iom->dev_fd, buf_parent_dir_area ,parent_dir_size, parent_dir_blocknr);
if(res == LAB4_ERROR)
goto LAB4_FREE2;
tmp_dir_entry = (struct lab4_dir_entry*)buf_parent_dir_area;
for (read_bytes = 0; read_bytes < parent_dir_size; read_bytes += LAB4_DIR_ENTRY_SIZE, tmp_dir_entry++) {
if (tmp_dir_entry->d_flag != LAB4_DIR_USED) {
break;
}
}
new_inode->i_ptr = read_bytes/LAB4_DIR_ENTRY_SIZE;
res = write_inode_to_itble(iom, new_inode);
if(res == LAB4_ERROR){
restore_itble(iom);
goto LAB4_FREE2;
}
pwrite(iom->dev_fd, dir_new_entry, LAB4_DIR_ENTRY_SIZE, parent_dir_blocknr + read_bytes );
dec_free_inodes(iom);
*new_inode_ptr = new_inode;
update_sb(iom);
update_ibmap(iom);
update_itble(iom);
free(buf_parent_dir_area);
return LAB4_SUCCESS;
LAB4_FREE2:
free(buf_parent_dir_area);
LAB4_FREE1:
free(new_inode);
return LAB4_SUCCESS;
}
int do_openfile(IOM *iom, INODE *parent_inode ,char *filename,int flags,int mode){
struct lab4_super_block *sb = IOM_SB(iom);
struct lab4_sb_info *sbi = IOM_SB_I(iom);
struct lab4_dir_entry file_entry;
struct lab4_inode *file_inode = NULL;
struct lab4_file_table *ft;
int fid = -1, res = 0, isdelete = 0;
memset(&file_entry, 0x0, LAB4_INODE_ENTRY_SIZE);
res = lab4_lookup(iom,parent_inode->i_ino,&file_entry,filename,&isdelete);
if(res == LAB4_ERROR)
return res;
else if (res == -ENOENT)
{
if (flags & O_RDWR || flags & O_CREAT)
{
res = lab4_createfile(iom, parent_inode, filename, &file_inode, mode);
if (res == LAB4_ERROR)
return res;
/*FIXME*/
}
else {
fid = -1;
goto RES;
}
}else{
file_inode = do_read_inode(iom,file_entry.d_ino);
}
if (file_inode->i_type != LAB4_TYPE_FILE) {
printf("This is not a file");
fid = -1;
goto RES;
}
fid = lab4_allocate_open_file_table(iom);
if (fid < 0) {
goto RES;
}
ft = sbi->file_table + fid;
ft->used = TRUE;
ft->ino = file_inode->i_ino;
ft->size = file_inode->i_size;
ft->rwoffset = 0;
ft->flags = flags;
if (O_APPEND & flags)
lab4_seek(sb, ft, file_inode->i_size, SEEK_SET);
else
lab4_seek(sb, ft, 0, SEEK_SET);
RES:
return (fid);
}
int lab4_do_open(IOM *iom, const char *path, int flags, int mode){
struct lab4_inode *parent_inode;
char f_name[FNAME_SIZE]={0.};
char d_name[FNAME_SIZE]={0,};
int res = LAB4_ERROR, fd;
res = do_path_check(path);
if(res == LAB4_ERROR)
return LAB4_ERROR;
get_name_from_path(path, f_name, d_name);
res = lab4_read_inode(iom, d_name, &parent_inode);
if(res == -ENOENT)
return res;
else if(res == LAB4_ERROR)
return res;
fd = do_openfile(iom, parent_inode, f_name, flags, mode);
return fd;
}
void lab4_do_close(IOM *iom, int fid)
{
struct lab4_sb_info *sbi = IOM_SB_I(iom);
struct lab4_file_table *ft;
ft = sbi->file_table + fid;
ft->ino = 0;
ft->size = 0;
ft->used = 0;
ft->rwoffset = 0;
ft->flags = 0;
}
int _delete_block(IOM *iom, unsigned int blocknr, int depth){
int cluster_size = iom->cluster_size, index = 0, res = LAB4_ERROR;
int *i_blocks;
int max = cluster_size / 4;
char buf_zero[cluster_size];
char buf_block[cluster_size];
memset(buf_zero, 0x0, cluster_size);
memset(buf_block, 0x0, cluster_size);
res = pread(iom->dev_fd, buf_block, cluster_size, blocknr);
if(res == LAB4_ERROR){
return res;
}
i_blocks = (int*)buf_block;
if(depth != 0){
for(index; index < max ; index++ ){
_delete_block(iom,i_blocks[index], --depth);
}
}else{
for(index ; index <= max; index++){
if(i_blocks[index] != 0){
res = pwrite(iom->dev_fd, buf_zero, cluster_size,i_blocks[index]);
if(res == LAB4_ERROR)
return res;
}
}
}
return LAB4_SUCCESS;
}
int delete_block(IOM *iom, INODE *del_inode){
int index=0, type = 1;
int cluster_size = iom->cluster_size;
char buf_zero[cluster_size];
int *i_blocks = del_inode->i_blocks, res= LAB4_ERROR;
memset(buf_zero, 0x0, cluster_size);
for(index ; index <= DIRECT_BLOCKS; index++){
if(i_blocks[index] != 0){
res = pwrite(iom->dev_fd, buf_zero, cluster_size,i_blocks[index]);
if(res == LAB4_ERROR)
return res;
}
}
for(index; index <= TINDIRECT_BLOCKS; index++, type++){
if(i_blocks[index] != 0)
_delete_block(iom, i_blocks[index], type);
}
return LAB4_SUCCESS;
}
int do_unlink(IOM *iom, INODE *parent_inode, char *filename)
{
struct lab4_sb_info *sbi = IOM_SB_I(iom);
struct lab4_dir_entry del_entry;
INODE *del_inode=NULL;
inode_t cur_ino;
char tmp_path[256];
char* buf_zero= NULL, *buf_backup=NULL;
int res, cluster_size = sbi->cluster_size, isdelete = 1;
res = lab4_lookup(iom, parent_inode->i_ino, &del_entry,filename, &isdelete);
if(res == LAB4_ERROR)
return LAB4_ERROR;
if(res == -ENOENT)
return -ENOENT;
del_inode = do_read_inode(iom, del_entry.d_ino);
if(!del_inode)
goto FREE1;
del_inode->i_links_count--;
if(del_inode->i_links_count == 0){
if(del_inode->i_size != 0){
delete_block_inbmap(iom, del_inode);
res = delete_block(iom, del_inode);
if(res == LAB4_ERROR){
restore_dbmap(iom);
goto FREE1;
}
}
res = delete_inode(iom, del_inode);
if(res == LAB4_ERROR){
restore_ibmap(iom);
goto FREE1;
}
}
if(isdelete !=-1){
pwrite(iom->dev_fd, &del_entry,LAB4_DIR_ENTRY_SIZE,parent_inode->i_blocks[0]+isdelete);
}
update_sb(iom);
update_ibmap(iom);
update_dbmap(iom);
update_itble(iom);
free(del_inode);
return LAB4_SUCCESS;
FREE1:
free(del_inode);
return LAB4_ERROR;
}
/*
* FIXME
*
* need to support multi block
* */
int do_write(IOM *iom, unsigned int fid, const char *buf, unsigned int size, unsigned int offset)
{
struct lab4_sb_info *sbi = IOM_SB_I(iom);
struct lab4_inode *inode;
struct lab4_file_table *cur_file_info;
int cur_offset=0, cur_count=0,res=LAB4_ERROR, w_byte, cluster_size;
unsigned int cur_remain=0;
long blocknr, blocknr_inbmap;
char *buf_new;
cluster_size = iom->cluster_size;
cur_file_info = &(sbi->file_table[fid]);
cur_file_info->rwoffset = offset;
inode = do_read_inode(iom,cur_file_info->ino);
if(!inode)
return LAB4_ERROR;
if(inode->i_size == 0){
res = alloc_block_inbmap(iom,&blocknr_inbmap);
if(res == LAB4_ERROR){
restore_dbmap(iom);
return LAB4_ERROR;
}
blocknr = blocknr_inbmap*cluster_size + sbi->darea_start;
inode->i_blocks[0] = blocknr;
}else{
blocknr = inode->i_blocks[0];
}
blocknr += offset;
while(size > 0){
cur_offset = cur_file_info->rwoffset & (cluster_size - 1);
cur_remain = cluster_size - offset;
if (cur_remain > size){
cur_remain = size;
}else{
/*FIXME*/
}
if (size && inode->i_size <= cur_file_info->rwoffset)
{
w_byte = pwrite(iom->dev_fd, buf ,cur_remain,blocknr);
if(w_byte == LAB4_ERROR)
goto FREE_ERROR1;
cur_count += cur_remain;
cur_file_info->rwoffset += cur_remain;
size -= cur_remain;
if (cur_file_info->rwoffset > cur_file_info->size)
cur_file_info->size = cur_file_info->rwoffset;
inode->i_size = cur_file_info->size;
}else{
buf_new = (char*)malloc(cluster_size);
memset(buf_new, 0x0 ,cluster_size);
strncpy(buf_new, buf,size);
w_byte = pwrite(iom->dev_fd, buf_new ,cluster_size,blocknr);
if(w_byte == LAB4_ERROR)
goto FREE_ERROR1;
cur_count += cur_remain;
cur_file_info->rwoffset = cur_remain;
cur_file_info->size = cur_file_info->rwoffset;
size -= cur_remain;
inode->i_size = cur_file_info->size;
}
}
res = write_inode_to_itble(iom, inode);
if(res == LAB4_ERROR)
goto FREE_ERROR1;
update_sb(iom);
update_itble(iom);
update_dbmap(iom);
free(inode);
return w_byte;
FREE_ERROR1:
free(inode);
return LAB4_ERROR;
}
int do_read(IOM *iom,unsigned int fid, char *buf,unsigned int size, unsigned int offset){
struct lab4_sb_info *sbi = IOM_SB_I(iom);
struct lab4_inode *inode;
struct lab4_file_table *cur_file_info;
int cur_offset=0, cur_count=0,res=LAB4_ERROR, r_byte, cluster_size;
unsigned int cur_remain=0;
long blocknr, blocknr_inbmap;
char *buf_read;
cluster_size = iom->cluster_size;
cur_file_info = &(sbi->file_table[fid]);
cur_file_info->rwoffset = offset;
inode = do_read_inode(iom,cur_file_info->ino);
if(!inode)
return LAB4_ERROR;
if(inode->i_size == 0){
free(inode);
return 0;
}else{
blocknr = inode->i_blocks[0];
}
buf_read = (char*)malloc(cluster_size);
memset(buf_read, 0x0 ,cluster_size);
while (size > 0 && cur_file_info->rwoffset < inode->i_size) {
pread(iom->dev_fd, buf_read ,size,blocknr);
r_byte = strlen(buf_read);
strncpy(buf, buf_read,r_byte);
memset(buf_read, 0x0, cluster_size);
// bh = nvfuse_get_bh(sb, ictx, inode->i_ino, NVFUSE_SIZE_TO_BLK(of->rwoffset), sync_read, NVFUSE_TYPE_DATA);
cur_offset = cur_file_info->rwoffset & (CLUSTER_SIZE - 1);
cur_remain = CLUSTER_SIZE - cur_offset;
if (cur_remain > size)
cur_remain = size;
cur_count += cur_remain;
cur_file_info->rwoffset += cur_remain;
size -= cur_remain;
}
free(inode);
return r_byte;
FREE_ERROR1:
free(inode);
return LAB4_ERROR;
}