-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
898 lines (783 loc) · 29 KB
/
server.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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
#include <stdio.h>
#include "udp.h"
#include "signal.h"
#include "structure.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#define BUFFER_SIZE (4096)
int sd;
int fd;
struct sockaddr_in addr;
int portNumber;
char* IMGFILENAME;
super_t superBlock;
super_t* sb;
int blocksAvail;
unsigned int iMapStartPosition;
unsigned int dMapStartPosition;
int inodeStartPosition;
int dataStartPosition;
int imgSize;
char* startAddress;
char* imapAddress;
char* dmapAddress;
char* inodeBlockAddress;
char* dataBlockAddress;
void intHandler(int dummy) {
UDP_Close(sd);
exit(130);
}
//These two functions take the pointer to the beginning of the inode or data block bitmap region and an integer that is the inode or data block number.
unsigned int get_bit(unsigned int *bitmap, int position) {
int index = position / 32;
int offset = 31 - (position % 32);
return (bitmap[index] >> offset) & 0x1;
}
void set_bit(unsigned int *bitmap, int position) {
int index = position / 32;
int offset = 31 - (position % 32);
bitmap[index] |= 0x1 << offset;
}
void reset_bit(unsigned int *bitmap, int position) {
int index = position / 32;
int offset = 31 - (position % 32);
bitmap[index] &= ~(0x1 << offset);
}
int throwErr(Msg* reply){
printf("THROW ERROR \n\n");
reply->requestType = -1;
reply->inum = -1;
fsync(fd);
UDP_Write(sd, &addr, (char*)(void*)reply, sizeof(Msg));
return -1;
}
int findFreeDataBlock() {
for(int i = 0; i<sb->num_data; i++) {
int bit = get_bit((unsigned int *)dmapAddress, i);
if(bit == 0) {
return i;
}
}
return -1;
}
inode_t* getpinode(int pinum){
// int pinumPosition = inodeStartPosition + pinum * sizeof(inode_t);
//check inode bitmap
//check inode type
inode_t* pinode = malloc(sizeof(inode_t));
//move read pointer to the position, SEEK_SET means starting from the beginning
// lseek(fd, pinumPosition, SEEK_SET);
// read(fd, pinode, sizeof(inode_t));
pinode = (inode_t*)(inodeBlockAddress + pinum * sizeof(inode_t));
return pinode;
}
// int checkimap(int pinum){
// int pinumBlock = superBlock.inode_region_addr + (pinum * sizeof(inode_t)) / UFS_BLOCK_SIZE;
// // pinode array position(in address)
// int pinumPosition = inodeStartPosition + pinum * sizeof(inode_t);
// //check inode bitmap
// unsigned int iMapStartPos = (unsigned int)iMapStartPosition;
// return get_bit(&iMapStartPos, pinumBlock);
// }
int lookup(int pinum, char *name){
Msg reply;
reply.inum = -1;
reply.requestType = 9; // type is reply
inode_t* pinode = malloc(sizeof(inode_t));
pinode = getpinode(pinum);
if(pinode->type != MFS_DIRECTORY){
printf("lookup: not directory\n");
fsync(fd);
UDP_Write(sd, &addr, (char*)&reply, sizeof(Msg));
return -1;
}
for(int i = 0; i < DIRECT_PTRS; i++){
if(pinode->direct[i] != -1){
// printf("test %d\n", i);
for(int j = 0; j < 128; j++){
dir_ent_t* entryAddr = (dir_ent_t*) (startAddress + (pinode->direct[i] * UFS_BLOCK_SIZE) + j * sizeof(dir_ent_t));
//printf(" direntry : %p, entry inum : %d, name : %s\n", entryAddr, entryAddr->inum, entryAddr->name);
// char* entryName = (entryAddr+j)->name;
// printf("name : %s, name should find %s\n", entryName, name);
if(strcmp((entryAddr)->name, name) == 0 && entryAddr->inum != -1){
reply.inum = (entryAddr)->inum;
fsync(fd);
UDP_Write(sd, &addr, (char*)&reply, sizeof(Msg));
return reply.inum;
}
}
}
}
printf("lookup: didn't find name in pinode\n");
fsync(fd);
UDP_Write(sd, &addr, (char*)&reply, sizeof(Msg));
return -1;
// // pinode in the bitmap(in block number)
// int pinumBlock = superBlock.inode_region_addr + (pinum * sizeof(inode_t)) / UFS_BLOCK_SIZE;
// // pinode array position(in address)
// int pinumPosition = inodeStartPosition + pinum * sizeof(inode_t);
// //check inode bitmap
// unsigned int iMapStartPos = (unsigned int)iMapStartPosition;
// if(get_bit(&iMapStartPos, pinumBlock) == 0){
// reply.inum = -1;
// }
// else{
// //check inode type
// inode_t pinode;
// //move read pointer to the position, SEEK_SET means starting from the beginning
// lseek(fd, pinumPosition, SEEK_SET);
// read(fd, &pinode, sizeof(inode_t));
// if(pinode.type == UFS_REGULAR_FILE){
// reply.inum = -1;
// }
// else{
// //go through all 30 dir entries
// for(int i = 0; i< DIRECT_PTRS; i++){
// //not sure if it is block number or address
// unsigned int dirEntryBlock = pinode.direct[i];
// // read data block, get block number, the index should start from superblocks
// int dirEntryBlockAddr = dirEntryBlock * UFS_BLOCK_SIZE;
// // int dirEntryBlockAddr = dataStartPosition + dirEntryBlock * UFS_BLOCK_SIZE;
// lseek(fd, dirEntryBlockAddr, SEEK_SET);
// char* entryBlock = malloc(UFS_BLOCK_SIZE);
// read(fd, entryBlock, UFS_BLOCK_SIZE);
// dir_ent_t* dirEntry;
// for(int j = 0; j < UFS_BLOCK_SIZE / sizeof(dir_ent_t); j++){
// dirEntry = (dir_ent_t*)(entryBlock + j * sizeof(dir_ent_t));
// if(dirEntry->inum != -1){
// if(strcmp(dirEntry->name, name) == 0){
// reply.inum = dirEntry->inum;
// return dirEntry->inum;
// }
// }
// }
// }
// }
// }
}
int FS_Stat(int inum, MFS_Stat_t *m){
Msg reply;
reply.inum = -1;
reply.requestType = 9; // type is reply
// pinode in the bitmap(in block number)
int inumBlock = superBlock.inode_region_addr + (inum * sizeof(inode_t)) / UFS_BLOCK_SIZE;
printf("inumBlock %d\n", inumBlock);
// pinode array position(in address)
// int inumPosition = inodeStartPosition + inum * sizeof(inode_t);
//check inode bitmap
if(get_bit((unsigned int *)imapAddress, inum) == 0){
reply.inum = -1;
reply.stat.size = -1;
reply.stat.type = -1;
fsync(fd);
UDP_Write(sd, &addr, (char*)&reply, sizeof(Msg));
printf("stat: inode bimap invalid\n");
return -1;
}
else{
//check inode type
inode_t* inode;
inode = getpinode(inum);
//move read pointer to the position, SEEK_SET means starting from the beginning
// lseek(fd, inumPosition, SEEK_SET);
// read(fd, &inode, sizeof(inode_t));
reply.inum = 0;
// reply.stat = malloc(sizeof(MFS_Stat_t));
reply.stat.size = inode->size;
reply.stat.type = inode->type;
}
int rc = UDP_Write(sd, &addr, (char*)&reply, sizeof(Msg));
printf("finished sending stat\n");
return rc;
}
int FS_Write(int inum, char *buffer, int offset, int nbytes){
Msg response;
response.requestType = 4;
if(nbytes > 4096 || get_bit( (unsigned int*) imapAddress, inum) != 1){
fprintf(stderr, "DEBUG server write err1\n");
throwErr(&response);
return -1;
}
inode_t* inode = getpinode(inum);
int type = inode->type;
int size = inode->size;
if(type == MFS_DIRECTORY || size < offset){
fprintf(stderr, "DEBUG server write err2\n");
throwErr(&response);
return -1;
}
// write addresses
int remainingBytes = nbytes;
int bufferOffset = 0;
int startBlock = offset / UFS_BLOCK_SIZE;
int startOffset = offset % UFS_BLOCK_SIZE;
int writeOffset = startOffset;
int curBlock = inode->direct[startBlock]; // current writing block (in blocks)
//printf("%i num data blocks \n",sb->num_data);
if(curBlock < 0){
if(blocksAvail == 0){
fprintf(stderr, "DEBUG server write err3\n");
throwErr(&response);
return -1;
}
fprintf(stderr,"MONICA \n\n");
//find a new block
unsigned int newBlock = 0;
// for(int i = 0; i < sb->num_data; i++){
// if(get_bit((unsigned int*) dmapAddress, i) == 0){
// newBlock = i + sb->data_region_addr;
// set_bit((unsigned int*)dmapAddress, i);
// curBlock = newBlock;
// break;
// }
// }
int newBlockNum = findFreeDataBlock();
if(newBlockNum == -1){
fprintf(stderr, "DEBUG server write err3\n");
throwErr(&response);
return -1;
}
newBlock = newBlockNum + sb->data_region_addr;
curBlock = newBlock;
set_bit((unsigned int*)dmapAddress, newBlockNum);
blocksAvail--;
}
inode->direct[startBlock] = curBlock;
if(!get_bit((unsigned int*)dmapAddress, curBlock - sb->data_region_addr)){
throwErr(&response);
return -1;
}
// relative addresses
unsigned long currBlock_Addr = curBlock * UFS_BLOCK_SIZE;
unsigned long curWrite_Addr = currBlock_Addr + startOffset;
// write until the current block is full. If not find an unused block
//unused blocks can be either next block, or any other block
int i = 0;
unsigned long writeAddresses[nbytes];
unsigned long newBlockUsed = -1;
//byte by byte writing (or finding the addr atleast)
while(remainingBytes > 0){
if(writeOffset < 4096){
writeAddresses[bufferOffset] = curWrite_Addr;
curWrite_Addr += 1;
bufferOffset += 1;
writeOffset += 1;
remainingBytes -= 1;
}else{
// next block
int flag = 0;
i += 1;
if(startBlock + i > DIRECT_PTRS){
fprintf(stderr, "DEBUG server write err3\n");
throwErr(&response);
return -1;
}
if(inode->direct[startBlock + i] != -1){
// found
curBlock = inode->direct[startBlock + i];
currBlock_Addr = curBlock * UFS_BLOCK_SIZE;
curWrite_Addr = currBlock_Addr;
//flag = 1;
writeOffset = 0;
set_bit((unsigned int*)dmapAddress,curBlock);
blocksAvail--;
continue;
}
//new block
int freeFlag = 0;
if(!flag){
// for(j = 0; j < sb->num_data; j++){
// if(get_bit((unsigned int*)dmapAddress, j)) continue; // jth data block is used, continue find
int j = findFreeDataBlock();
fprintf(stderr,"%i : JJJJJJ ", j);
if(j == -1){
throwErr(&response);
return -1;
}
// use jth data block
set_bit((unsigned int*)dmapAddress,curBlock);
blocksAvail--;
curBlock = j + sb->data_region_addr;
currBlock_Addr = curBlock * UFS_BLOCK_SIZE;
curWrite_Addr = currBlock_Addr;
writeOffset = 0;
freeFlag = 1;
// jth data block will be used
newBlockUsed = curBlock;
break;
// no place remaining!
if(!freeFlag){
throwErr(&response);
return -1;
}
}
}
}
//actually writing
for(int i = 0; i < nbytes; i++){
*(char*)(void*)(unsigned long)(writeAddresses[i] + (unsigned long)startAddress) = *(buffer + i); // real address
}
inode->size += nbytes;
for(int i = 0; i < DIRECT_PTRS; i++){
if(inode->direct[i] == -1){
if(newBlockUsed != -1){
inode->direct[i] = newBlockUsed + sb->data_region_addr;
break;
}
}
}
// synchronize to disk
// if(msync(startAddress, imgSize, MS_SYNC) == -1){
// fprintf(stderr, "DEBUG server write msync err\n");
// throwErr(&reply);
// return -1;
// }
fsync(fd);
// reply
response.inum = inum;
fsync(fd);
UDP_Write(sd, &addr, (char*)&response, sizeof(Msg));
return 0;
}
//TODO: Do UDP write after writing to buffer and send reply
int FS_Read(int inum, char *buffer, int offset, int nbytes)
{
Msg response;
response.requestType = 5;
unsigned long iBit = get_bit((unsigned int*)imapAddress, inum);
//fprintf(stderr, "DEBUG server read:: nbytes: %d\n", nbytes);
if(nbytes > 4096 || iBit != 1 || sizeof(buffer) > MFS_BLOCK_SIZE){
throwErr(&response);
return -1;
}
inode_t* inode = getpinode(inum);
int size = inode->size;
int type = inode->type;
if(offset >= size){
throwErr(&response);
return -1;
}
if(type != MFS_REGULAR_FILE && type != MFS_DIRECTORY){
throwErr(&response);
return -1;
}
// find the start location of offset to read
int startBlock = offset / UFS_BLOCK_SIZE;
int startOffset = offset % UFS_BLOCK_SIZE;
int remainingBytes = nbytes;
int readOffset = startOffset;
int bufferOffset = 0;
unsigned long curBlock = inode->direct[startBlock]; // in blocks current writing block
unsigned long curBlockAddress = curBlock * UFS_BLOCK_SIZE;
unsigned long currentRead_Address = curBlockAddress + startOffset;
// Directory case: cannot read if it doesn't read from start of each entry
if(type == MFS_DIRECTORY && startOffset % 32 != 0){
fprintf(stderr, "DEBUG server read err4\n");
throwErr(&response);
return -1;
}
// read until this block is full
// to another block(if there's following block, use that, otherwise use a unused)
int i = 0;
while(remainingBytes > 0){
if(readOffset < 4096){
*(buffer + bufferOffset) = *(char*)((unsigned long)currentRead_Address + (unsigned long)startAddress); //TODO: check this
currentRead_Address += 1;
remainingBytes -= 1;
bufferOffset += 1;
readOffset += 1;
}else{
// find the next block
int flag = 0;
while( inode->direct[startBlock + i] != -1 && startBlock + i < 30){
// found next block
if(inode->direct[startBlock + i] != -1){
curBlock = inode->direct[startBlock + i];
curBlockAddress = curBlock * UFS_BLOCK_SIZE;
currentRead_Address = curBlockAddress;
flag = 1;
readOffset = 0;
break;
}
i++;
}
// did not find the next block, error
if(!flag){
throwErr(&response);
return -1;
}
}
}
response.inum = inum;
//actually writing to buffer
for(int i = 0; i < nbytes; i++){
response.buffer[i] = *(buffer+i);
}
fsync(fd);
UDP_Write(sd, &addr, (char*)&response, sizeof(Msg));
return 0;
}
// Msg reply;
// //int inumBlock = superBlock.inode_bitmap_addr + (inum * sizeof(inode_t))/UFS_BLOCK_SIZE;
// //int inumPos = (sizeof(inode_t) * inum) +inodeStartPosition;
// if(get_bit((unsigned int*)imapAddress,inum) != 1) {
// reply.requestType = -1;
// UDP_Write(sd, &addr, (char*)&reply, sizeof(Msg));
// return -1;
// }
// // inode_t inode;
// // // Msg reply;
// // lseek(fd,inumPos,SEEK_SET);
// // read(fd,&inode, sizeof(inode_t));
// printf("bulle\n");
// inode_t* inode = malloc(sizeof(inode_t));
// inode = getpinode(inum);
// printf("%i : name \n ",inode->type);
// // if(inode.type == UFS_DIRECTORY){
// //READ DIR CONTENTS
// if( (inode -> type = MFS_DIRECTORY) && (nbytes % sizeof(dir_ent_t) != 0 || offset % sizeof(dir_ent_t) != 0)){
// reply.requestType = -1;
// UDP_Write(sd, &addr, (char*)&reply, sizeof(Msg));
// return -1;
// }
// int dataBlock = 0;//offset/UFS_BLOCK_SIZE;
// printf("data block: %i\n", dataBlock);
// //int offsetRemaining = 0;
// int nbytes1 =0;
// if(offset%UFS_BLOCK_SIZE >= nbytes){
// int nbytes1 = (inode->direct[dataBlock+1] - offset); //no of bytes to read in first block
// nbytes = nbytes - nbytes1;
// }
// if(inode->direct[dataBlock] == -1 || offset>inode->size){
// reply.requestType = -1;
// UDP_Write(sd, &addr, (char*)&reply, sizeof(Msg));
// return -1;
// }
// char* test;
// printf("block number : %i \n",inode->direct[dataBlock]);
// lseek(fd,dataStartPosition+inode->direct[dataBlock],SEEK_SET);
// read(fd,&test,nbytes1);
// if(nbytes1 ){
// printf("remaining case \n");
// //figure out how to read into buffer at buffer+remaining
// lseek(fd,dataStartPosition+inode->direct[dataBlock],SEEK_SET);
// read(fd,&buffer,nbytes1);
// lseek(fd,dataStartPosition+inode->direct[dataBlock+1],SEEK_SET);
// read(fd,&buffer+nbytes1,nbytes);
// strncpy(reply.buffer,buffer,nbytes+nbytes+1);
// return (UDP_Write(sd, &addr, (char*)&reply, sizeof(Msg)));
// }
// else{
// printf("perfect case");
// fseek(fd,(int*)dataBlockAddress+inode->direct[dataBlock],SEEK_SET);
// fread(buffer,1,1,(FILE*)startAddress);
// strncpy(reply.buffer,buffer,nbytes+nbytes+1);
// return (UDP_Write(sd, &addr, (char*)&reply, sizeof(Msg)));
// }
//}
// else{
// //READ FILE CONTENTS INTO buffer
// int dataBlock = offset/UFS_BLOCK_SIZE;
// //int offsetRemaining = 0;
// int nbytes1 =0;
// if(offset%UFS_BLOCK_SIZE != 0){
// int nbytes1 = (inode.direct[dataBlock+1] - offset); //no of bytes to read in first block
// nbytes = nbytes - nbytes1;
// }
// if(inode.direct[dataBlock] == -1 || offset>inode.size){
// reply.requestType = -1;
// int rc = UDP_Write(sd, addr, (char*)&reply, sizeof(Msg));
// return -1;
// }
// if(nbytes1 ){
// //figure out how to read into buffer at buffer+remaining
// lseek(fd,dataStartPosition+inode.direct[dataBlock],SEEK_SET);
// read(fd,&buffer,nbytes1);
// lseek(fd,dataStartPosition+inode.direct[dataBlock+1],SEEK_SET);
// buffer = buffer+nbytes1;
// read(fd,&buffer+nbytes1,nbytes);
// strncpy(reply.buffer,buffer,nbytes+nbytes+1);
// return 1;
// }
// else{
// lseek(fd,dataStartPosition+inode.direct[dataBlock],SEEK_SET);
// read(fd,&buffer,nbytes);
// strncpy(reply.buffer,buffer,nbytes+nbytes+1);
// return 1;
// }
// }
int findFreeInode() {
for(int i = 0; i<superBlock.num_inodes; i++) {
int bit = get_bit((unsigned int *)imapAddress, i);
if(bit==0) {
return i;
}
}
return -1;
}
int FS_Creat(int pinum, int type, char *name){
Msg reply;
reply.type = 9;
reply.inum = -1;
//check name length
if(strlen(name) > 27) {
throwErr(&reply);
return -1;
}
//get pinode
inode_t* pinode = getpinode(pinum);
if(pinode->type != MFS_DIRECTORY){
reply.inum = -1;
fsync(fd);
UDP_Write(sd, &addr, (char*)&reply, sizeof(Msg));
return 0;
}
//get newest freeinode
int freeInodeNum = findFreeInode();
printf("freeInodeNum : %d\n", freeInodeNum);
inode_t* freeInode;
// freeInode = (inode_t*)(startAddress + sb->inode_region_addr * freeInodeNum);
freeInode = (inode_t*) (inodeBlockAddress + freeInodeNum * sizeof(inode_t));
//get the datablock of directory
// printf("pinode->direct[0] : %d\n", pinode->direct[0]);
printf("start address : %p\n", startAddress);
dir_ent_t* pinodeDataBlockAddress = (dir_ent_t*) (startAddress + (pinode->direct[0]) * UFS_BLOCK_SIZE);
for(int i = 0; i< UFS_BLOCK_SIZE/sizeof(dir_ent_t); i++) {
// int entryAddress = pinodeDataBlockAddress + sizeof(dir_ent_t) * i;
// lseek(fd, entryAddress, SEEK_SET);
dir_ent_t* dirEntry;
dirEntry = pinodeDataBlockAddress + i;
// read(fd, dirEntry, sizeof(dir_ent_t));
printf(" direntry : %p, entry inum : %d\n", dirEntry, dirEntry->inum);
if(dirEntry->inum == -1){
dirEntry->inum = freeInodeNum;
// strcpy(dirEntry->name, name);
for(int z = 0; z != 28; z++){
dirEntry->name[z] = *(name+z);
}
// printf("create file name : %s\n", dirEntry->name);
set_bit((unsigned int *)imapAddress, freeInodeNum);
printf("imap value %d\n", get_bit((unsigned int *)imapAddress, 1));
pinode->size += sizeof(dir_ent_t);
freeInode->type = type;
reply.inum = 0;
break;
}
}
if(type == MFS_REGULAR_FILE) {
for(int i = 0; i < DIRECT_PTRS; i++){
int newDataBlockNum = findFreeDataBlock();
freeInode->direct[i] = newDataBlockNum + sb->data_region_addr;
set_bit((unsigned int *)dmapAddress, newDataBlockNum);
}
//printf("finish creating regular file\n");
}
else if(type == MFS_DIRECTORY){
int newDataBlockNum = findFreeDataBlock() + sb->data_region_addr;
freeInode->direct[0] = newDataBlockNum;
//printf("new free data block number %d\n", newDataBlockNum);
dir_ent_t* allDirEntryInsideBlock;
allDirEntryInsideBlock =(dir_ent_t*) (startAddress + newDataBlockNum * UFS_BLOCK_SIZE);
//printf("create direntry address %p\n", allDirEntryInsideBlock);
//set '.' and '..'
strcpy(allDirEntryInsideBlock->name, ".");
strcpy((allDirEntryInsideBlock+1)->name, "..");
allDirEntryInsideBlock->inum = freeInodeNum;
(allDirEntryInsideBlock+1)->inum = pinum;
for(int i = 2; i<UFS_BLOCK_SIZE/sizeof(dir_ent_t); i++) {
(allDirEntryInsideBlock+i)->inum = -1;
}
freeInode->size = 2 * (sizeof(dir_ent_t));
set_bit((unsigned int *)dmapAddress, newDataBlockNum - sb->data_region_addr);
for(int i = 1; i < 30; i++){
freeInode->direct[i] = -1;
}
//printf("finish creating directory\n");
}
fsync(fd);
UDP_Write(sd, &addr, (char*)&reply, sizeof(Msg));
return 0;
}
int FS_Unlink(int pinum, char *name)
{
Msg reply;
reply.type = 9;
reply.inum = -1;
//check it is directry
inode_t* pinode = getpinode(pinum);
if(pinode->type == MFS_REGULAR_FILE){
UDP_Write(sd, &addr, (char*)&reply, sizeof(Msg));
return -1;
}
dir_ent_t* parentDatablockAddr = (dir_ent_t*) (startAddress + pinode->direct[0] * UFS_BLOCK_SIZE);
int foundName = 0;
//recurse all the dir entries
for(int i = 0; i < 128; i++){
if(strcmp(name, (parentDatablockAddr+i)->name) == 0 && (parentDatablockAddr+i)->inum != -1){
foundName = 1;
printf("find name\n");
int targetInum = (parentDatablockAddr+i)->inum;
inode_t* targetInode = getpinode(targetInum);
strcpy((parentDatablockAddr+i)->name, "\0");
if(targetInode->type == MFS_DIRECTORY){
printf("unlink directory size : %d\n", targetInode->size);
//chekc if directory is empty
if(targetInode->size > 2 * sizeof(dir_ent_t)){
printf("non empty directory\n");
UDP_Write(sd, &addr, (char*)&reply, sizeof(Msg));
return -1;
}
dir_ent_t* targetDataBlockAddr = (dir_ent_t*) (startAddress + targetInode->direct[0] * UFS_BLOCK_SIZE);
//reset inum of dir entry
targetDataBlockAddr->inum = -1;
(targetDataBlockAddr+1)->inum = -1;
//reset inode bitmap
reset_bit((unsigned int *)imapAddress, targetInum);
//reset datablock bitmap
reset_bit((unsigned int*)dmapAddress, targetInode->direct[0] - sb->data_region_addr);
reply.inum = 0;
(parentDatablockAddr+i)->inum = -1;
UDP_Write(sd, &addr, (char*)&reply, sizeof(Msg));
return 0;
}
else{
printf("unlink file\n");
for(int i = 0; i < 30; i++){
int targetDataBlockNumber = targetInode->direct[i];
reset_bit((unsigned int*) dmapAddress, targetDataBlockNumber - sb->data_region_addr);
}
reset_bit((unsigned int *)imapAddress, targetInum);
(parentDatablockAddr+i)->inum = -1;
reply.inum = 0;
UDP_Write(sd, &addr, (char*)&reply, sizeof(Msg));
return 0;
}
}
}
if(foundName == 0){
reply.inum = 0;
}
UDP_Write(sd, &addr, (char*)&reply, sizeof(Msg));
return 0;
}
int FS_Shutdown(){
Msg reply;
reply.type = 9;
fsync(fd);
close(fd);
// m->c_received_rc = 0;
//isShutdown = true;
UDP_Write(sd, &addr, (char*)&reply, sizeof(Msg));
UDP_Close(portNumber);
printf("shutdown!\n");
exit(0);
return 0;
}
int fsInitMmap(int fd){
struct stat fileStat;
if(fstat(fd,&fileStat) < 0)
{
exit(1);
}
imgSize = fileStat.st_size;
assert(sd > -1);
void* fileAddr = mmap(NULL, fileStat.st_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
sb = (super_t *) fileAddr;
startAddress = fileAddr;
printf("start address %p\n", startAddress);
imapAddress = (char *)sb + sb->inode_bitmap_addr * UFS_BLOCK_SIZE;
printf("imap address %p\n", imapAddress);
dmapAddress = (char *)sb + sb->data_bitmap_addr * UFS_BLOCK_SIZE;
inodeBlockAddress = (char *)sb + sb->inode_region_addr * UFS_BLOCK_SIZE;
dataBlockAddress = (char *)sb + sb->data_region_addr * UFS_BLOCK_SIZE;
blocksAvail = sb ->num_data;
return 0;
}
int fsInit(char* img){
if ((fd = open (img, O_RDWR|O_CREAT, S_IRWXU)) < 0)
{
fprintf(stderr,"An error has occurred\n");
return -1;
}
read(fd, &superBlock, sizeof(super_t));
fsInitMmap(fd);
printf("inode_bitmap_len : %d\n", superBlock.data_bitmap_len);
printf("data_bitmap_len : %d\n", superBlock.data_bitmap_len);
printf("inode_region_len : %d\n", superBlock.inode_region_len);
printf("data_region_bitmap_len : %d\n", superBlock.data_region_len);
printf("num_inodes : %d\n", superBlock.num_inodes);
printf("num_data : %d\n", superBlock.num_data);
iMapStartPosition = superBlock.inode_bitmap_addr * UFS_BLOCK_SIZE; //should be equal to 4096
dMapStartPosition = superBlock.data_bitmap_addr * UFS_BLOCK_SIZE;
inodeStartPosition = superBlock.inode_region_addr * UFS_BLOCK_SIZE;
dataStartPosition = superBlock.data_region_addr * UFS_BLOCK_SIZE;
return 0;
}
int startServer(int port, char* img){
portNumber = port;
printf("server port %d \n", port);
sd = UDP_Open(port);
assert(sd > -1);
//char *src, *dst;
//struct stat statbuf;
if(fsInit(img) < 0) return -1;
while (1) {
Msg* request = malloc(sizeof(Msg));
printf("server:: waiting...\n");
int rc = UDP_Read(sd, &addr, (char*)request, sizeof(Msg));
// rc = UDP_Write(sd, &addr,"test connection", BUFFER_SIZE);
//int responseRet;
printf("server:: read message [size:%d contents:(%s)]\n", rc, (char*)request);
if (rc > 0) {
switch (request->requestType)
{
case 1:
break;
case 2:
printf("request2\n");
lookup(request->inum, request->name);
break;
case 3:
printf("request3\n");
FS_Stat(request->inum,&request->stat);
break;
case 4:
FS_Write(request->inum,request->buffer,request->offset,request -> nbytes);
break;
case 5:
FS_Read(request->inum,request->buffer,request->offset,request -> nbytes);
break;
case 6:
FS_Creat(request ->inum,request->type,request->name);
break;
case 7:
FS_Unlink(request -> inum, request -> name);
break;
case 8:
FS_Shutdown();
exit(0);
break;
default:
break;
}
}
}
return 0;
}
int main(int argc, char *argv[]) {
printf("server beginning\n");
// signal(SIGINT, intHandler);
if (argc != 3)
perror("Error : Incorrect arguments\n");
IMGFILENAME = (char*) malloc(100 * sizeof(char));
strcpy(IMGFILENAME, argv[2]);
printf("before start server\n");
startServer(atoi(argv[1]), IMGFILENAME);
}