forked from chouqin/simpleFs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
myfs.c
622 lines (547 loc) · 15.3 KB
/
myfs.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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
/*
* =====================================================================================
*
* Filename: myfs.c
*
* Description:
*
* Version: 1.0
* Created: 2011年12月10日 20时11分37秒
* Revision: none
* Compiler: gcc
*
* Author: YOUR NAME (),
* Company:
*
* =====================================================================================
*/
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include "myfs.h"
/* private functions of other file */
#include "buf.h"
#include "inode.h"
#include "super.h"
#include "file.h"
#include "dir.h"
#include "./socket/client.h"
/*private function to do strcpy */
void my_strcpy(char des[DIRSIZ], const char * src){
memset(des, 0, DIRSIZ);
int i = 0;
strcpy(des, src);
for (i = strlen(des); i < DIRSIZ; ++ i)
des[i] = '\0';
}
void init()
{
/* inode part, there is no inode in the memory now, set them all to 0*/
struct inode * rip;
for (rip = &inode[0]; rip < &inode[NR_INODES]; ++ rip){
rip->i_count = 0;
rip->i_mode = I_NOT_ALLOC;
rip->i_dirt = CLEAN;
rip->i_num = 0;
}
/* buf part, init the hash chain and free chain*/
struct buf * bp;
int i;
for(bp = &buf[0]; bp < &buf[NR_BUFS]; ++ bp){
/* bp->b_blocknr = 0; */
if(bp != &buf[NR_BUFS - 1]){
bp->b_next = bp + 1;
bp->b_hash = bp + 1;
}
else {
bp->b_next = NIL_BUF;
bp->b_hash = NIL_BUF;
}
if (bp != &buf[0])
bp->b_prev = bp - 1;
else
bp->b_prev = NIL_BUF;
bp->b_blocknr = 0;
bp->b_dirt = CLEAN;
bp->b_count = 0;
}
for (i = 1; i < NR_BUFS; ++ i){
buf_hash[i] = NIL_BUF;
}
buf_hash[0] = &buf[0];
front = &buf[0];
rear = &buf[NR_BUFS -1];
bufs_in_use = 0;
/*filp part ,same as inode*/
struct filp * f;
for (f = &filp[0]; f < &filp[NR_FILPS]; f++) {
f->filp_count = 0;
f->filp_ino = NIL_INODE;
f->filp_type = FILP_CLOSED;
f->filp_pos = 0;
}
/* superblock part*/
super_block.s_isearch = 0;
super_block.s_zsearch = 0;
}
void mkroot(){
/* init(); */
/*first, let the bit 0 don't be allocated as most fs do*/
struct buf * bp = get_block(START_BLOCK);
/* printf("here\n");exit(0); */
int *wptr = &bp->b_bitmap[0];
*wptr = (int)1;
bp->b_dirt = DIRTY;
put_block(bp);
bp = get_block(START_BLOCK + S_IMAP_BLOCKS);
/* printf("here\n");exit(0); */
wptr = &bp->b_bitmap[0];
*wptr = (int)1;
bp->b_dirt = DIRTY;
put_block(bp);
/*allocate bit 1 for root inode */
/* bit_t root_bit = alloc_bit(IMAP, 1); */
/* printf("root bit is %d\n", (int) root_bit); */
int mode = 0x00000003;
struct inode * root_inode = alloc_inode(mode);
if (root_inode == NIL_INODE){
printf("can't allocate root_inode\n");
exit(1);
}
if (root_inode->i_num != 1){
printf("root bit allocate error: can't set root to bit 1, the inode number is %d\n", (int)root_inode->i_num);
exit(1);
}
if (!(root_inode->i_mode & I_DIRECTORY)){
printf("root is not a directory!!\n");
exit(1);
}
root_inode->i_dirt = DIRTY;
/* put_inode(root_inode); */
root_inode = get_inode(1);
if (!(root_inode->i_mode & I_DIRECTORY)){
printf("after put and get: root is not a directory!!\n");
printf("this time the i number is %d and imode is %d\n", (int)root_inode->i_num, root_inode->i_mode);
exit(1);
}
/* allocate disk for root inode */
zone_t root_zone = alloc_zone(S_FIRSTDATAZONE);
if (root_zone != (S_FIRSTDATAZONE + 1)){
printf("root zone is not the first data zone!!\n");
exit(1);
}
struct buf *rp = get_block(root_zone);
struct direct *cdir = &rp->b_dir[0];
struct direct *udir = &rp->b_dir[1];
cdir->d_ino = (int) 1;
strcpy(cdir->d_name, ".");
udir->d_ino = (int)1;
strcpy(udir->d_name, "..");
rp->b_dirt = DIRTY;
put_block(rp);
root_inode->i_zone[0] = root_zone;
root_inode->i_dirt = DIRTY;
put_inode(root_inode);
}
void write_back(){
/*buf part*/
struct buf * bp;
for(bp = &buf[0]; bp < &buf[NR_BUFS]; ++ bp){
if(bp->b_dirt == DIRTY && bp->b_blocknr != 0){
write_block(bp->b_blocknr, (char *)&bp->b);
}
}
}
int my_open(char * path){
struct inode * rip;
struct inode * ip;
char string[DIRSIZ];
struct filp * fp;
int fd;
int r;
rip = last_dir(path, string);
if (rip == NIL_INODE){
printf("the path name is not correct: incorrect dir!\n");
return errno;
}
ip = advance(rip, string);
put_inode(rip);
if (ip == NIL_INODE){
printf("the path name is not correct: incorrect filename!\n");
return errno;
}
if (r = get_fd(&fd, &fp) != OK)
return r;
fp->filp_ino = ip;
fp->filp_count = 1;
fp->filp_type = RDWR;
fp->filp_pos = 0;
return fd;
}
int my_create(char * path){
struct inode * rip;
struct inode * ip;
char string[DIRSIZ];
struct filp * fp;
int fd;
int r;
int *inode_num, number;
inode_num = &number;
rip = last_dir(path, string);
if (rip == NIL_INODE){
printf("the path name is not correct!\n");
return errno;
}
*inode_num = 0;
if(r = search_dir(rip, string, inode_num, LOOK_UP) != OK){
printf("error occured when trying to search diretory");
put_inode(rip);
return;
}
if(*inode_num != 0){
printf("item with the same name exits, please delete that item first\n ");
put_inode(rip);
return;
}
ip = alloc_inode(1);
printf("allocate a new inode for %s, inode_num is %d\n", string, ip->i_num);
*inode_num = ip->i_num;
if (r = search_dir(rip, string, inode_num, ENTER) != OK){
printf("enter a item error: %s\n", strerror(r));
put_inode(rip);
return r;
}
rip->i_dirt = DIRTY;
put_inode(rip);
if (r = get_fd(&fd, &fp) != OK)
return r;
fp->filp_ino = ip;
fp->filp_count = 1;
fp->filp_type = RDWR;
fp->filp_pos = 0;
return fd;
}
int my_read(int fd, char * buf, int count){
struct inode *rip;
struct buf *bp;
char * data;
int position,b, buf_pos, pos;
int tmp;
int left;
int i;
rip = filp[fd].filp_ino;
if(rip == NIL_INODE){
printf("incorrect fd!!\n");
return -1;
}
printf("size of this file is %d\n", rip->i_size);
if(filp[fd].filp_type == READ){
position = filp[fd].filp_pos;
}else{
position = 0;
}
if ((position + count) > rip->i_size) {
printf("count is bigger than file size\n");
return -1;
}
tmp = count + position;
buf_pos = 0;
/*if the last block still left some size*/
if(position % S_BLOCK_SIZE != 0){
left = S_BLOCK_SIZE - position % S_BLOCK_SIZE;
pos = position % S_BLOCK_SIZE;
b = read_map(rip, position);
bp = get_block(b);
for(i = 0; i < left; ++ i){
buf[buf_pos++] = bp->b_data[pos+i];
}
put_block(bp);
position += left;
}
for(; position < tmp; position += S_BLOCK_SIZE){
b = read_map(rip, position);
if(b <= 0){
printf("read map error\n");
return -1;
}
/* printf("read the inode block, block number is %d\n", b); */
bp = get_block(b);
left = tmp - position;
if (left >= S_BLOCK_SIZE){
for(i = 0; i < S_BLOCK_SIZE; ++i)
buf[buf_pos++] = bp->b_data[i];
}else{
for(i = 0; i < left; ++i)
buf[buf_pos++] = bp->b_data[i];
}
put_block(bp);
}
filp[fd].filp_type = READ;
filp[fd].filp_pos = tmp;
return buf_pos;
}
int my_write(int fd, char *buf, int count){
struct inode *rip;
struct buf *bp;
char * data;
int position,b, pos, buf_pos;
int left;
int i;
int tmp;
rip = filp[fd].filp_ino;
if(rip == NIL_INODE){
printf("incorrect fd!!\n");
return -1;
}
/* printf("size of this file is %d\n", rip->i_size); */
if(filp[fd].filp_type == WRITE){
position = filp[fd].filp_pos;
printf("current position is %d\n", position);
}else{
position = 0;
if(rip->i_size > 0){
empty_inode_space(rip);
wipe_inode(rip);
/* return; */
}
}
tmp = count + position;
buf_pos = 0;
/*if the last block still left some size*/
if((position % S_BLOCK_SIZE) != 0){
left = S_BLOCK_SIZE - position % S_BLOCK_SIZE;
pos = position % S_BLOCK_SIZE;
b = read_map(rip, position);
bp = get_block(b);
for(i = 0; i < left; ++ i){
bp->b_data[pos + i] = buf[buf_pos];
buf_pos ++;
}
bp->b_dirt = DIRTY;
put_block(bp);
position += left;
printf("current position is %d and tmp is %d and buf_pos is %d\n", position, tmp, buf_pos);
/* exit(0); */
}
for(; position < tmp; position += S_BLOCK_SIZE){
bp = new_block(rip, position);
if(bp == NIL_BUF){
printf("create a new block error\n");
return -1;
}
/* printf("write a new inode block, block number is %d\n", bp->b_blocknr); */
left = tmp - position;
if (left >= S_BLOCK_SIZE){
for(i = 0; i < S_BLOCK_SIZE; ++i){
if(buf_pos >= count){
printf("buf_pos bigger than count, this time left is %d, and position is%d\n", left, position);
exit(1);
}
bp->b_data[i] = buf[buf_pos++];
}
}else{
/* if(left != 128){ */
/* printf("here, current left is %d buf_pos is %d\n",left, buf_pos); */
/* exit(0); */
/* } */
for(i = 0; i < left; ++i){
if(buf_pos > count){
printf("buf_pos bigger than count\n");
exit(1);
}
bp->b_data[i] = buf[buf_pos++];
}
}
bp->b_dirt = DIRTY;
put_block(bp);
}
rip->i_size = tmp;
rip->i_dirt = DIRTY;
filp[fd].filp_type = WRITE;
filp[fd].filp_pos = tmp;
return buf_pos;
}
int my_close(int fd){
struct inode * rip;
filp[fd].filp_count = 0;
rip = filp[fd].filp_ino;
filp[fd].filp_ino = NIL_INODE;
filp[fd].filp_type = FILP_CLOSED;
filp[fd].filp_pos = 0;
if(rip == NIL_INODE){
printf("incorrect fd!!\n");
return -1;
}
put_inode(rip);
return OK;
}
int my_remove(char * name){
struct inode *rip;
struct inode *ip;
char string[DIRSIZ];
int r;
int *inode_num;
int number;
inode_num = &number;
rip = last_dir(name, string);
if(rip == NIL_INODE){
printf("search directory error: please enter the correct path\n");
return -1;
}
if(r = search_dir(rip, string, inode_num, DELETE) != OK){
printf("delete file error: %s", strerror(r));
}
put_inode(rip);
ip = get_inode(*inode_num);
empty_inode_space(ip);
put_inode(ip);
free_inode(*inode_num);
return r;
}
int my_rename(char * oldname, char * newname){
struct inode *rip;
struct inode *ip;
char string[DIRSIZ];
char newstring[DIRSIZ];
int r;
rip = last_dir(oldname, string);
if(rip == NIL_INODE){
printf("search directory error: please enter the correct oldname path\n");
return -1;
}
ip = last_dir(newname, newstring);
if((ip == NIL_INODE) || (ip->i_num != rip->i_num)){
printf("search directory error: please enter the correct newname path\n");
return -1;
}
put_inode(ip);
/* my_strcpy(newstring, newname); */
if (r = dir_rename(rip, string, newstring) != OK){
printf("rename file error: please enter the correct name\n");
}
/* rip->i_dirt = DIRTY; */
put_inode(rip);
return r;
}
int my_mkdir(char * name){
struct inode * rip;
struct inode * ip;
char string[DIRSIZ];
int r;
int *inode_num, number;
inode_num = &number;
rip = last_dir(name, string);
if (rip == NIL_INODE){
printf("the path name is not correct!\n");
return errno;
}
*inode_num = 0;
if(r = search_dir(rip, string, inode_num, LOOK_UP) != OK){
printf("error occured when trying to search diretory");
put_inode(rip);
return;
}
if(*inode_num != 0){
printf("item with the same name exits, please delete that item first\n ");
put_inode(rip);
return;
}
ip = alloc_inode(3);
printf("allocate a new inode for %s, inode_num is %d\n", string, ip->i_num);
*inode_num = ip->i_num;
if (r = search_dir(rip, string, inode_num, ENTER) != OK){
printf("enter a item %s error: %s\n", string, strerror(r));
}
my_strcpy(string, "..");
*inode_num = rip->i_num;
if (r = search_dir(ip, string, inode_num, ENTER) != OK){
printf("enter a item %s error: %s\n", string, strerror(r));
}
my_strcpy(string, ".");
*inode_num = ip->i_num;
if (r = search_dir(ip, string, inode_num, ENTER) != OK){
printf("enter a item %s error: %s\n", string, strerror(r));
}
ip->i_dirt = DIRTY;
put_inode(ip);
rip->i_dirt = DIRTY;
put_inode(rip);
return r;
}
int my_rmdir(char * name){
struct inode *rip;
struct inode *ip;
char string[DIRSIZ];
int r;
int *inode_num;
int number = 0;
inode_num = &number;
rip = last_dir(name, string);
if(rip == NIL_INODE){
printf("search directory error: please enter the correct path\n");
return -1;
}
if(r = search_dir(rip, string, inode_num, DELETE) != OK){
printf("delete file error: %s", strerror(r));
put_inode(rip);
return -1;
}
put_inode(rip);
ip = get_inode(*inode_num);
if(ip == NIL_INODE || !(ip->i_mode & I_DIRECTORY)){
printf("search directory error: please enter the correct path\n");
put_inode(ip);
return -1;
}
delete_dir(ip, 0);
empty_inode_space(ip);
put_inode(ip);
free_inode(*inode_num);
/* return delete_dir(ip); */
return OK;
}
void show_file_list(){
printf("files in the disk:\n");
struct inode * root_inode = get_inode(1);
list_dir(root_inode, 0);
put_inode(root_inode);
}
void reset_disk(){
struct buf *bp;
struct inode * root;
int * bit;
int i;
char string[S_BLOCK_SIZE];
int number;
memset(string, 0, S_BLOCK_SIZE);
/* for(i = 0; i < S_ZONES; ++i){ */
/* write_block(i, string); */
/* } */
/*no_block*/
/* 1~3 imap block */
bp = get_block(1);
bit = &bp->b_bitmap[0];
*bit = 1;
bp->b_dirt = DIRTY;
put_block(bp);
/* 4~103 zmap block*/
bp = get_block(4);
bit = &bp->b_bitmap[0];
*bit = 1;
bp->b_dirt = DIRTY;
put_block(bp);
/* 104~1353 inode block */
root = alloc_inode(3);
if(root->i_num != 1){
printf("root is not allocate as 1!! it is %d\n", root->i_num);
number = root->i_num;
put_inode(root);
free_inode(number);
return;
}
put_inode(root);
}
void my_mkfs(){
init();
}