-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfs.c
More file actions
860 lines (734 loc) · 20.3 KB
/
fs.c
File metadata and controls
860 lines (734 loc) · 20.3 KB
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
#include "fs.h"
#include "disk.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#define FS_MAGIC 0xf0f03410
#define INODES_PER_BLOCK 128
#define POINTERS_PER_INODE 5
#define POINTERS_PER_BLOCK 1024
#define BYTES_PER_BLOCK 4096
struct fs_superblock {
int magic;
int nblocks;
int ninodeblocks;
int ninodes;
};
struct fs_inode {
int isvalid;
int size;
int direct[POINTERS_PER_INODE];
int indirect;
};
union fs_block {
struct fs_superblock super;
struct fs_inode inode[INODES_PER_BLOCK];
int pointers[POINTERS_PER_BLOCK];
char data[DISK_BLOCK_SIZE];
};
// Global Variables
_Bool fs_mounted = 0;
_Bool *bitmap;
int bitmapSize = 0;
// prototypes
int getNewInode(void);
int fs_format()
{
if(fs_mounted == 1)
{
printf("Formatting Error: Can't format a mounted FS\n");
return 0;
}
int blocks = disk_size();
int ninode_blocks = blocks / 10;
if(blocks < 3)
{
printf("Not enough blocks to build a file system!\n");
return 0;
}
if(ninode_blocks != 0){
if(blocks % ninode_blocks != 0)
ninode_blocks++; // Round up
}
else
{
ninode_blocks = 1;
}
union fs_block block;
disk_read(0,block.data);
// Format super
block.super.magic = FS_MAGIC;
block.super.nblocks = blocks;
block.super.ninodeblocks = ninode_blocks;
block.super.ninodes = ninode_blocks*INODES_PER_BLOCK;
disk_write(0,block.data);
// Invalidate all inodes
int i;
int j;
union fs_block inode_block;
for(i = 0; i < ninode_blocks; i++)
{
disk_read(i+1, inode_block.data);
for(j = 0; j < INODES_PER_BLOCK ; j++)
{
inode_block.inode[j].isvalid = 0;
}
disk_write(i+1,inode_block.data);
}
return 1;
}
void fs_debug()
{
union fs_block block;
disk_read(0,block.data);
printf("superblock:\n");
printf("\t%d blocks\n",block.super.nblocks);
printf("\t%d inode blocks\n",block.super.ninodeblocks);
printf("\t%d inodes\n",block.super.ninodes);
int i;
int j;
int k;
union fs_block inode_blocks[block.super.ninodeblocks];
for(i = 1; i <= block.super.ninodeblocks; i++){
disk_read(i, inode_blocks[i-1].data);
for(j = 0; j < INODES_PER_BLOCK ; j++){
if(inode_blocks[i-1].inode[j].isvalid == 1){
printf("inode %d:\n",j+INODES_PER_BLOCK*(i-1));
int size = inode_blocks[i-1].inode[j].size;
printf("\tsize: %d bytes\n",size);
int nblocks = size/BYTES_PER_BLOCK;
if(size%BYTES_PER_BLOCK != 0)
nblocks += 1;
int direct_blocks = ( nblocks > POINTERS_PER_INODE ? POINTERS_PER_INODE : nblocks);
int indirect_blocks = 0;
if(nblocks > POINTERS_PER_INODE)
indirect_blocks = nblocks - POINTERS_PER_INODE;
if(indirect_blocks > POINTERS_PER_BLOCK)
{
printf("Size exceeds FileSystem Capability\n");
return ;
}
printf("\tdirect blocks:");
for(k = 0; k < direct_blocks; k++)
{
printf(" %d",inode_blocks[i-1].inode[j].direct[k]);
}
printf("\n");
if( indirect_blocks > 0)
{
printf("\tindirect block: %d\n",inode_blocks[i-1].inode[j].indirect);
printf("\tindirect data blocks:");
union fs_block pointers_block;
disk_read(inode_blocks[i-1].inode[j].indirect, pointers_block.data);
for(k = 0; k < indirect_blocks; k++)
{
printf(" %d",pointers_block.pointers[k]);
}
printf("\n");
}
}
}
}
}
void print_bitmap(void)
{
int i;
int nl = 0;
for(i = 0; i < bitmapSize; i++)
{
printf("%d:%d,", i,bitmap[i]);
nl +=1;
if(nl > 10){
printf("\n");
nl = 0;
}
}
printf("\n");
}
int fs_mount()
{
union fs_block block;
disk_read(0,block.data);
// Check Magic
if(block.super.magic != FS_MAGIC)
{
printf("Did not find proper filesystem. Operation Failed\n");
return 0;
}
if(bitmap != NULL)
{
free(bitmap);
bitmap = NULL;
}
bitmap = malloc((block.super.nblocks) * sizeof(_Bool));
// Initialize bitmap to zero
int b;
for(b = 0; b < block.super.nblocks; b++)
{
bitmap[b] = 0;
}
bitmap[0] = 1;
int diskSize = disk_size();
// Read used data blocks
int i;
int j;
int k;
union fs_block inode_blocks[block.super.ninodeblocks];
for(i = 1; i <= block.super.ninodeblocks; i++){
bitmap[i] = 1;
disk_read(i, inode_blocks[i-1].data);
for(j = 0; j < INODES_PER_BLOCK ; j++){
if(inode_blocks[i-1].inode[j].isvalid == 1){
int size = inode_blocks[i-1].inode[j].size;
int nblocks = size/BYTES_PER_BLOCK;
if(size%BYTES_PER_BLOCK != 0)
nblocks += 1;
int direct_blocks = ( nblocks > POINTERS_PER_INODE ? POINTERS_PER_INODE : nblocks);
int indirect_blocks = 0;
if(nblocks > POINTERS_PER_INODE)
indirect_blocks = nblocks - POINTERS_PER_INODE;
if(indirect_blocks > POINTERS_PER_BLOCK)
{
printf("Error Mounting: A file with a too large size was detected.\n");
free(bitmap);
bitmap = NULL;
return 0;
}
for(k = 0; k < direct_blocks; k++)
{
int blockNum = inode_blocks[i-1].inode[j].direct[k];
if(blockNum <=block.super.ninodeblocks || blockNum >= diskSize )
{
printf("Error Mounting FS: Invalid block number detected in Filesystem.\n");
free(bitmap);
bitmap = NULL;
return 0;
}
bitmap[blockNum] = 1;
}
if( indirect_blocks > 0)
{
bitmap[inode_blocks[i-1].inode[j].indirect] = 1;
union fs_block pointers_block;
disk_read(inode_blocks[i-1].inode[j].indirect, pointers_block.data);
for(k = 0; k < indirect_blocks; k++)
{
int blockNum = pointers_block.pointers[k];
if(blockNum <=block.super.ninodeblocks || blockNum >= diskSize )
{
printf("Error Mounting FS: Invalid block number detected in Filesystem.\n");
free(bitmap);
bitmap = NULL;
return 0;
}
bitmap[blockNum] = 1;
}
}
}
}
}
fs_mounted = 1;
bitmapSize = block.super.nblocks;
//print_bitmap();
return 1;
}
int fs_create()
{
if(!fs_mounted)
{
printf("No mounted filesystem found\n");
return 0;
}
union fs_block super;
union fs_block inodeB;
disk_read(0, super.data);
int i;
int j;
int foundInode = -1;
_Bool found = 0;
for(i= 0; i < super.super.ninodeblocks;i++)
{
disk_read(i+1, inodeB.data);
for(j = 0; j < INODES_PER_BLOCK; j++)
{
if(!inodeB.inode[j].isvalid && j+i != 0)
{
found = 1;
foundInode = j;
break;
}
}
if(found)
break;
}
// Create inode
if(found)
{
inodeB.inode[foundInode].isvalid = 1;
inodeB.inode[foundInode].size = 0;
disk_write(i+1, inodeB.data);
return foundInode + i*INODES_PER_BLOCK;
}
else
{
return 0;
}
}
int fs_delete( int inumber )
{
if(!fs_mounted)
{
printf("No mounted filesystem found\n");
return 0;
}
union fs_block super;
union fs_block inodeB;
disk_read(0, super.data);
int inodeBlock = inumber/INODES_PER_BLOCK;
if(inodeBlock > super.super.ninodeblocks || inumber > super.super.ninodes || inumber < 1){
printf("Invalid inumber\n");
return 0;
}
_Bool Error = 0;
disk_read(inodeBlock + 1, inodeB.data);
int inodeIndex = inumber - INODES_PER_BLOCK*inodeBlock;
if(inodeB.inode[inodeIndex].isvalid)
{
int k;
int size = inodeB.inode[inodeIndex].size;
int diskSize = disk_size();
int nblocks = size/BYTES_PER_BLOCK;
if(size%BYTES_PER_BLOCK != 0)
nblocks += 1;
int direct_blocks = ( nblocks > POINTERS_PER_INODE ? POINTERS_PER_INODE : nblocks);
int indirect_blocks = 0;
if(nblocks > POINTERS_PER_INODE)
indirect_blocks = nblocks - POINTERS_PER_INODE;
if(indirect_blocks > POINTERS_PER_BLOCK)
{
printf("Error Deleting Inode: A file with a too large size was detected.Possible corruption in filesystem. An attempt to fix the corruption will be made.\n");
indirect_blocks = POINTERS_PER_BLOCK;
Error = 1;
}
for(k = 0; k < direct_blocks; k++)
{
int blockNum = inodeB.inode[inodeIndex].direct[k];
if(blockNum <=super.super.ninodeblocks || blockNum >= diskSize )
{
printf("Error Deleting: Invalid block number detected in Filesystem.\n");
Error = 1;
}
else{
bitmap[blockNum] = 0;
}
}
if( indirect_blocks > 0)
{
union fs_block pointers_block;
disk_read(inodeB.inode[inodeIndex].indirect, pointers_block.data);
for(k = 0; k < indirect_blocks; k++)
{
int blockNum = pointers_block.pointers[k];
if(blockNum <=super.super.ninodeblocks || blockNum >= diskSize )
{
printf("Error Deleting Inode: Invalid block number detected in Filesystem.\n");
Error = 1;
}
else{
bitmap[blockNum] = 0;
}
}
bitmap[inodeB.inode[inodeIndex].indirect] = 0; // free indirect block
}
}
else{
printf("Error Deleting Inode: The inode is invalid\n");
return 0;
}
// Write to inode
inodeB.inode[inodeIndex].size = 0;
inodeB.inode[inodeIndex].isvalid = 0;
disk_write(inodeBlock + 1, inodeB.data);
if(Error)
{
printf("Inode was succesfully deleted, but there may be some corruption in data\n");
}
return 1;
}
int fs_getsize( int inumber )
{
if(!fs_mounted)
{
printf("GetSize Error: No mounted filesystem found\n");
return -1;
}
union fs_block super;
union fs_block inodeB;
disk_read(0, super.data);
int inodeBlock = inumber/INODES_PER_BLOCK;
if(inodeBlock > super.super.ninodeblocks || inumber > super.super.ninodes || inumber < 1){
printf("GetSize Error: Invalid inumber\n");
return -1;
}
disk_read(inodeBlock + 1, inodeB.data);
int inodeIndex = inumber - INODES_PER_BLOCK*inodeBlock;
if(inodeB.inode[inodeIndex].isvalid)
{
return inodeB.inode[inodeIndex].size;
}
else
{
printf("GetSize Error: Invalid Inode\n");
return -1;
}
}
int fs_read( int inumber, char *data, int length, int offset )
{
if(!fs_mounted)
{
printf("No mounted filesystem found\n");
return 0;
}
union fs_block super;
int read = 0; // Bytes read
union fs_block inodeB;
disk_read(0, super.data);
int inodeBlock = inumber/INODES_PER_BLOCK;
if(inodeBlock > super.super.ninodeblocks || inumber > super.super.ninodes || inumber < 1){
printf("Read Error: Invalid inumber\n");
return 0;
}
disk_read(inodeBlock + 1, inodeB.data);
int inodeIndex = inumber - INODES_PER_BLOCK*inodeBlock;
if(inodeB.inode[inodeIndex].isvalid)
{
int size = inodeB.inode[inodeIndex].size;
if(length + offset > size)
{
length = size - offset;
}
if(offset > size)
{
return 0;
}
int diskSize = disk_size();
int k;
int nblocks = size/BYTES_PER_BLOCK;
int startDirectByte = 0;
int startIndirectByte = 0;
int startDirectBlock;
int startIndirectBlock;
if(size%BYTES_PER_BLOCK != 0)
nblocks += 1;
startDirectByte = offset%BYTES_PER_BLOCK;
startIndirectByte = (offset-(BYTES_PER_BLOCK*POINTERS_PER_INODE))%BYTES_PER_BLOCK;
if(startIndirectByte < 0)
startIndirectByte = 0;
startDirectBlock = offset/BYTES_PER_BLOCK;
startIndirectBlock = (offset - (BYTES_PER_BLOCK*POINTERS_PER_INODE))/BYTES_PER_BLOCK;
if(startIndirectBlock < 0)
startIndirectBlock = 0;
int direct_blocks = ( nblocks > POINTERS_PER_INODE ? POINTERS_PER_INODE : nblocks);
int indirect_blocks = 0;
int to_read = 0;
if(nblocks > POINTERS_PER_INODE)
indirect_blocks = nblocks - POINTERS_PER_INODE;
if(indirect_blocks > POINTERS_PER_BLOCK)
{
printf("Error Reading Inode: A file with a too large size was detected.Possible corruption in filesystem. An attempt to read will be made.\n");
indirect_blocks = POINTERS_PER_BLOCK;
}
//printf("startDirectBlock:%d\nstartIndirectBlock:%d\ndirect_blocks:%d\nindirect_blocks:%d\nlength:%d\noffset:%d\nsize:%d\n",startDirectBlock, startIndirectBlock, direct_blocks, indirect_blocks, length, offset, size);
for(k = startDirectBlock; k < direct_blocks && read < length; k++)
{
union fs_block readBlock;
int blockNum = inodeB.inode[inodeIndex].direct[k];
// printf("Reading block %d\n",blockNum);
if(blockNum <=super.super.ninodeblocks || blockNum >= diskSize )
{
printf("Error Reading: Invalid block number detected in Filesystem.\n");
return read;
}
else{
if(startDirectByte > 0)
{
memmove(readBlock.data, &readBlock.data[startDirectByte], BYTES_PER_BLOCK-startDirectByte);
to_read = ((length-read) > BYTES_PER_BLOCK - startDirectByte) ? (BYTES_PER_BLOCK-startDirectByte) : length-read;
startDirectByte = 0;
}
else
{
to_read = ((length - read) > BYTES_PER_BLOCK) ? BYTES_PER_BLOCK : length-read;
}
// printf("to_read is: %d\nread is:%d\nlength is:%d\n",to_read, read, length);
// printf("disk_read segfaults\n");
disk_read(blockNum, readBlock.data);
// printf("strncat segfaults\n");
memcpy(&data[read], readBlock.data, to_read);
// printf("It doesn't\n");
read += to_read;
}
}
if( indirect_blocks > 0)
{
// printf("reading indirect\n");
// printf("startDirectBlock:%d\nstartIndirectBlock:%d\ndirect_blocks:%d\nindirect_blocks:%d\nlength:%d\noffset:%d\nsize:%d\nread:%d\n",startDirectBlock, startIndirectBlock, direct_blocks, indirect_blocks, length, offset, size, read);
union fs_block readBlock;
union fs_block pointers_block;
disk_read(inodeB.inode[inodeIndex].indirect, pointers_block.data);
for(k = startIndirectBlock; k < indirect_blocks && read < length; k++)
{
int blockNum = pointers_block.pointers[k];
//printf("Reading block %d\n",blockNum);
if(blockNum <=super.super.ninodeblocks || blockNum >= diskSize )
{
printf("Error Reading: Invalid block number detected in Filesystem.\n");
return read;
}
else{
if(startIndirectByte > 0)
{
memmove(readBlock.data, &readBlock.data[startIndirectByte], BYTES_PER_BLOCK-startIndirectByte);
to_read = ((length-read) > BYTES_PER_BLOCK - startIndirectByte) ? (BYTES_PER_BLOCK-startIndirectByte) : length-read;
startIndirectByte = 0;
}
else
{
to_read = ((length - read) > BYTES_PER_BLOCK) ? BYTES_PER_BLOCK : length-read;
}
disk_read(blockNum, readBlock.data);
memcpy(&data[read], readBlock.data, to_read);
read += to_read;
}
}
}
}
else{
printf("Error Reading: The inode is invalid\n");
return 0;
}
return read;
}
int fs_write( int inumber, const char *data, int length, int offset )
{
// Check Mounted
if(!fs_mounted)
{
printf("No mounted filesystem found\n");
return 0;
}
// Check Inumber
union fs_block super;
int size;
union fs_block inodeB;
disk_read(0, super.data);
int inodeBlock = inumber/INODES_PER_BLOCK;
if(inodeBlock > super.super.ninodeblocks || inumber > super.super.ninodes || inumber < 1){
printf("Write Error: Invalid inumber\n");
return 0;
}
// Read Inode
disk_read(inodeBlock + 1, inodeB.data);
int written = 0;
int inodeIndex = inumber - INODES_PER_BLOCK*inodeBlock;
if(inodeB.inode[inodeIndex].isvalid)
{
// Find size and test offset
size = inodeB.inode[inodeIndex].size;
// Find starting bytes and blocks
int diskSize = disk_size();
int k;
int nblocks = size/BYTES_PER_BLOCK;
int startDirectByte = 0;
int startIndirectByte = 0;
int startDirectBlock;
int startIndirectBlock;
_Bool allocateIndirectBlock = 0;
if(size%BYTES_PER_BLOCK != 0)
nblocks += 1;
startDirectByte = offset%BYTES_PER_BLOCK;
startIndirectByte = (offset-(BYTES_PER_BLOCK*POINTERS_PER_INODE))%BYTES_PER_BLOCK;
if(startIndirectByte < 0)
startIndirectByte = 0;
startDirectBlock = offset/BYTES_PER_BLOCK;
startIndirectBlock = (offset - (BYTES_PER_BLOCK*POINTERS_PER_INODE))/BYTES_PER_BLOCK;
if(startIndirectBlock < 0)
startIndirectBlock = 0;
// Find blocks to allocate
int bytesToAllocate = ((offset + length) - nblocks*BYTES_PER_BLOCK);
int blocksToAllocate = bytesToAllocate/BYTES_PER_BLOCK;
if(bytesToAllocate % BYTES_PER_BLOCK != 0)
blocksToAllocate += 1;
int directAllocateIndex = POINTERS_PER_INODE - 1;
int indirectAllocateIndex = -1;
if(nblocks >= POINTERS_PER_INODE)
{
indirectAllocateIndex = nblocks - POINTERS_PER_INODE - 1;
}
else
{
directAllocateIndex = nblocks -1;
}
int onblocks = nblocks;
nblocks += blocksToAllocate;
if(onblocks <= POINTERS_PER_INODE && nblocks > POINTERS_PER_INODE)
{
allocateIndirectBlock = 1;
}
if(offset > BYTES_PER_BLOCK*onblocks)
{
offset = BYTES_PER_BLOCK*onblocks;
}
// Find blocks to load
int direct_blocks = ( nblocks > POINTERS_PER_INODE ? POINTERS_PER_INODE : nblocks);
int indirect_blocks = 0;
int to_write = 0;
if(nblocks > POINTERS_PER_INODE)
indirect_blocks = nblocks - POINTERS_PER_INODE;
if(indirect_blocks > POINTERS_PER_BLOCK)
{
printf("Error Reading Inode: A file with a too large size was detected.Possible corruption in filesystem. An attempt to write will be made.\n");
indirect_blocks = POINTERS_PER_BLOCK;
}
_Bool changedInodeBlock = 0;
_Bool changedPointersBlock = 0;
_Bool ranOutOfMemory = 0;
int blockNum;
for(k = startDirectBlock; k < direct_blocks && written < length; k++)
{
union fs_block writeBlock;
if(k > directAllocateIndex){
blockNum = getNewInode();
if(blockNum < 0)
{
ranOutOfMemory = 1;
printf("System has run out of memory. Please delete some files to free memory\n");
}
else{
inodeB.inode[inodeIndex].direct[k] = blockNum;
changedInodeBlock = 1;
}
}
else
{
blockNum = inodeB.inode[inodeIndex].direct[k];
}
if(blockNum <=super.super.ninodeblocks || blockNum >= diskSize)
{
if(!ranOutOfMemory)
printf("Error Writing: Invalid block number detected in Filesystem.\n");
}
else{
if(startDirectByte > 0)
{
to_write = ((length-written) > BYTES_PER_BLOCK - startDirectByte) ? (BYTES_PER_BLOCK-startDirectByte) : length-written;
memcpy(&writeBlock.data[startDirectByte], &data[written], to_write); // written should always be 0 at this point
startDirectByte = 0;
}
else
{
to_write = ((length - written) > BYTES_PER_BLOCK) ? BYTES_PER_BLOCK : length-written;
memcpy(writeBlock.data, &data[written],to_write);
}
//printf("writeBlock data: %s\n", writeBlock.data);
disk_write(blockNum, writeBlock.data);
written += to_write;
}
}
if(changedInodeBlock)
disk_write(inodeBlock + 1, inodeB.data);
if( indirect_blocks > 0 && !ranOutOfMemory)
{
union fs_block writeBlock;
union fs_block pointers_block;
if(allocateIndirectBlock){
int newInode = getNewInode();
if(newInode < 0)
{
ranOutOfMemory = 1;
printf("System has ran out of memory\n");
}
else
{
inodeB.inode[inodeIndex].indirect = newInode; // Will be written to disk later along size
//printf("Allocating new indirect block:%d\n",inodeB.inode[inodeIndex].indirect);
}
}
disk_read(inodeB.inode[inodeIndex].indirect, pointers_block.data);
int blockNum;
for(k = startIndirectBlock; k < indirect_blocks && written < length && !ranOutOfMemory; k++)
{
if(k > indirectAllocateIndex){
blockNum = getNewInode();
if(blockNum < 0)
{
printf("System ran out of memory\n");
ranOutOfMemory = 1;
break;
}
pointers_block.pointers[k] = blockNum;
changedPointersBlock = 1;
}
else
{
blockNum = pointers_block.pointers[k];
}
int blockNum = pointers_block.pointers[k];
if(blockNum <=super.super.ninodeblocks || blockNum >= diskSize )
{
printf("Error Writing: Invalid block number detected in Filesystem.\n");
return written;
}
else{
if(startIndirectByte > 0)
{
to_write = ((length-written) > BYTES_PER_BLOCK - startIndirectByte) ? (BYTES_PER_BLOCK-startIndirectByte) : length-written;
memcpy(&writeBlock.data[startIndirectByte], &data[written], to_write);
startIndirectByte = 0;
}
else
{
to_write = ((length - written) > BYTES_PER_BLOCK) ? BYTES_PER_BLOCK : length-written;
memcpy(writeBlock.data, &data[written],to_write);
}
disk_write(blockNum, writeBlock.data);
written += to_write;
}
}
if(changedPointersBlock)
disk_write(inodeB.inode[inodeIndex].indirect, pointers_block.data);
}
}
else{
printf("Error Writing: The inode is invalid\n");
return 0;
}
// Above we made sure bytes written would not exceed max file size
int max_size = BYTES_PER_BLOCK*(POINTERS_PER_INODE + POINTERS_PER_BLOCK);
if(offset+written > size){
int new_size = (offset + written > max_size) ? max_size : offset + written;
inodeB.inode[inodeIndex].size = new_size;
disk_write(inodeBlock + 1, inodeB.data);
}
return written;
}
int getNewInode()
{
int i;
_Bool found = 0;
for(i = 0; i<bitmapSize ; i++)
{
if(bitmap[i] == 0)
{
bitmap[i] = 1;
found = 1;
break;
}
}
if(found)
return i;
else
return -1;
}