/
ext2_fs.c
2655 lines (2020 loc) · 76.9 KB
/
ext2_fs.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
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* ext2_fs.c
*
* Copyright 2013 JS-OS <js@duck-squirell>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
#include <system.h>
#include <vfs.h>
#define TO_U32INT(bytes) ((bytes) / sizeof(u32int))
#define BLOCKS_TO_SECTORS(blocks) (((blocks) * EXT2_BLOCK_SZ) / SECTOR_SIZE)
#define SECTORS_TO_BLOCKS(sectors) (((sectors) * SECTOR_SIZE) / EXT2_BLOCK_SZ)
#define IS_CONS_BLOCKS(first, second) ((((second) - (first)) / (EXT2_BLOCK_SZ / SECTOR_SIZE)) == 1 ? TRUE : FALSE)
//the global path for the current directory
char *ext2_path;
u32int ext2_current_dir_inode = 0;
ext2_inode_t *ext2_root;
char *ext2_root_name;
//caches
ext2_superblock_t *ext2_g_sblock = 0;
ext2_group_descriptor_t *ext2_g_gdesc = 0;
ext2_inode_t *ext2_g_inode_table = 0;
u8int *ext2_g_bb = 0; //the block bitmap
u8int *ext2_g_ib = 0; //the inode bitmap
//caches
//defaults (logged as user) for permisions files need to have in order to be accesed
u32int _Rlogged = EXT2_I_RUSR, _Wlogged = EXT2_I_WUSR, _Xlogged = EXT2_I_XUSR;
static ext2_inode_t *__create_root__(void);
static ext2_inode_t *__create_file__(u32int size);
static ext2_inode_t *__create_dir__(ext2_superblock_t *sblock, ext2_group_descriptor_t *gdesc);
static char *__get_name_of_file__(ext2_inode_t *directory, ext2_inode_t *file);
static struct ext2_dirent dirent;
enum __block_types__
{
EXT2_DIRECT,
EXT2_SINGLY,
EXT2_DOUBLY,
EXT2_TRIPLY
};
u32int ext2_read(ext2_inode_t *node, u32int offset, u32int size, u8int *buffer)
{
//if the user can read it
if(node->mode & _Rlogged)
{
//a size of 0 or an offset greater than the node's size is impossible
if(!size || offset > node->size)
return 0; //fail
//make sure we cap the size
if(size + offset > node->size)
size = node->size - offset;
u32int blocks_to_read = ((size - 1) / EXT2_BLOCK_SZ) + 1, i, out;
for(i = 0; i < blocks_to_read; i++)
{
out = ext2_block_of_set(node, i, (u32int*)(buffer + i * EXT2_BLOCK_SZ));
if(!out)
return 0; //fail
}
//sucess!
return size;
}
}
u32int ext2_read_meta_data(ext2_superblock_t **sblock, ext2_group_descriptor_t **gdesc)
{
ext2_superblock_t *sdata;
//get the sblock data
sdata = (ext2_superblock_t*)kmalloc(sizeof(ext2_superblock_t));
floppy_read((u32int)((EXT2_SBLOCK_OFF) / SECTOR_SIZE), sizeof(ext2_superblock_t), (u32int*)sdata);
//check here if the superblock exists
if(sdata->magic != EXT2_MAGIC)
return 1; //fail!
u32int nblockgroups = sdata->total_blocks / sdata->blocks_per_group;
ext2_group_descriptor_t *gdata;
//get the group descriptor data
gdata = (ext2_group_descriptor_t*)kmalloc(nblockgroups * sizeof(ext2_group_descriptor_t));
floppy_read((EXT2_SBLOCK_OFF + EXT2_BLOCK_SZ) / SECTOR_SIZE, nblockgroups * sizeof(ext2_group_descriptor_t), (u32int*)gdata);
*sblock = sdata;
//~ k_printf("junk location %h, actual %h\n", *sblock, sdata);
*gdesc = gdata;
//TODO make this work with the mutltiple sblocks and gdescs due to possible multiple block groups
//~ memcpy(ext2_g_sblock, sdata, sizeof(ext2_superblock_t));
//~ memcpy(ext2_g_gdesc, gdata, sizeof(ext2_group_descriptor_t));
ext2_g_sblock = sdata;
ext2_g_gdesc = gdata;
//Sucess!
return 0;
}
u32int *ext2_format_block_bitmap(ext2_group_descriptor_t *gdesc, u32int blocks_used)
{
u32int location = gdesc->block_bitmap;
//the offset from the beginning of the file (the end of the inode table)
u32int begining_offset = gdesc->inode_table_id + BLOCKS_TO_SECTORS(gdesc->inode_table_size);
u8int *block_bitmap;
if(!ext2_g_bb)
{
block_bitmap = (u8int*)kmalloc(EXT2_BLOCK_SZ);
ext2_g_bb = block_bitmap;
floppy_read(location, EXT2_BLOCK_SZ, (u32int*)block_bitmap);
}else
block_bitmap = ext2_g_bb;
//if we do not want to allocate any blocks, return 0
if(!blocks_used)
return 0;
u32int *output;
output = (u32int*)kmalloc(blocks_used * sizeof(u32int));
memset(output, 0x0, blocks_used * sizeof(u32int));
//the number of consecutive blocks free
u32int consec_free = 0;
s32int bit_off;
//TODO this loop looks for consecutive blocks, if none are found implement non-consecutive block allocation
u32int off, bit_mask;
for(off = 0; off < EXT2_BLOCK_SZ; off++)
{
//go through all bit mask starting from 0b10000000 to 0b1 inclusive
for(bit_mask = 0b10000000, bit_off = 0; bit_mask >= 0b1; bit_mask >>= 1, bit_off++)
{
//if the bit isolated by bit_mask is 0
if(!(*(block_bitmap + off) & bit_mask))
consec_free++;
else //if the bit isolated by bit_mask is 1
consec_free = 0;
//we have found adequate space
if(consec_free == blocks_used)
{
//offset output to the end of the place where we will be assigning block locations
output += blocks_used;
//going backwards
for(consec_free; consec_free > 0; consec_free--)
{
//flip the current bit and go backwards
*(block_bitmap + off) |= bit_mask;
output--;
//assign the offset in bits from the beginning
*output = BLOCKS_TO_SECTORS((8 * off + bit_off)) + begining_offset;
//decrement everything by one
bit_mask <<= 1;
bit_off--;
//if bit_mask > 0b10000000, then reset bit_mask, bit_off, and move the off back one byte
if(bit_mask > 0b10000000)
{
bit_mask = 0b1;
bit_off = 7;
off--;
}
}
floppy_write((u32int*)block_bitmap, EXT2_BLOCK_SZ, location);
/*purposly not freeing since block_bitmap, regardless of update, is the
* global variable which should not be cleared*/
//TODO in the case of non-consecutive blocks, make this optimized and work
u32int i, *clear;
clear = (u32int*)kmalloc(EXT2_BLOCK_SZ * blocks_used);
memset(clear, 0x0, EXT2_BLOCK_SZ * blocks_used);
//if we have exited, then all of the blocks are sequential, so write as one big chunk
floppy_write(clear, EXT2_BLOCK_SZ * blocks_used, *output);
kfree(clear);
return output;
}
}
}
/*purposly not freeing since block_bitmap, regardless of update,
* is the global variable which should not be cleared*/
kfree(output);
//if we did not exit yet, there must be no space
return 0;
}
u32int ext2_singly_create(u32int *block_locations, u32int offset, u32int nblocks, ext2_group_descriptor_t *gdesc)
{
u32int *location, *block_data, blk, value;
block_data = (u32int*)kmalloc(EXT2_BLOCK_SZ);
location = ext2_format_block_bitmap(gdesc, 1);
//write the locations of the singly indirect blocks in the singly block in the memory (block_data)
for(blk = 0; blk < (nblocks < EXT2_NIND_BLOCK ? nblocks : EXT2_NIND_BLOCK); blk++)
*(block_data + blk) = *(block_locations + offset + blk);
//write the singly block to the physical floppy disk
floppy_write(block_data, EXT2_BLOCK_SZ, *location);
//store the value of location in another place so we can free location and return its location
value = *location;
kfree(block_data);
kfree(location);
//return the location of the singly block
return value;
}
u32int ext2_doubly_create(u32int *block_locaitions, u32int offset, u32int nblocks, ext2_group_descriptor_t *gdesc)
{
u32int *location, *block_data, blk, value;
block_data = (u32int*)kmalloc(EXT2_BLOCK_SZ);
//clear the block data for the new block
memset(block_data, 0x0, EXT2_BLOCK_SZ);
//get a new location for the doubly block
location = ext2_format_block_bitmap(gdesc, 1);
//write the locations of the singly indirect blocks to the doubly block in the tmp memory block (block_data)
for(blk = 0; blk < (nblocks < EXT2_NDIND_BLOCK ? nblocks : EXT2_NDIND_BLOCK); blk += EXT2_NIND_BLOCK)
{
*(block_data + (blk / EXT2_NIND_BLOCK)) = ext2_singly_create(block_locaitions, offset + blk,
(blk + EXT2_NIND_BLOCK) <= nblocks ? EXT2_NIND_BLOCK :
nblocks % EXT2_NIND_BLOCK, gdesc);
}
floppy_write(block_data, EXT2_BLOCK_SZ, *location);
value = *location;
kfree(block_data);
kfree(location);
//return the location of the singly block
return value;
}
u32int ext2_inode_entry_blocks(ext2_inode_t *node, ext2_group_descriptor_t *gdesc,
u32int *block_locations, u32int blocks_used)
{
u32int blk;
//to begin, write all of the blocks a 0, so that there is no accidental junk
for(blk = 0; blk < EXT2_N_BLOCKS; blk++)
node->blocks[blk] = 0;
//if no blocks are used, simply exit
if(!blocks_used)
return 0;
//write the locations of the direct blocks
for(blk = 0; blk < (blocks_used < EXT2_NDIR_BLOCKS ? blocks_used : EXT2_NDIR_BLOCKS); blk++)
node->blocks[blk] = *(block_locations + blk);
if(blocks_used < EXT2_NDIR_BLOCKS)
//we already wrote all of the data needed to be written above, return with sucess
return 0;
else{
u32int *location, *block_data;
block_data = (u32int*)kmalloc(EXT2_BLOCK_SZ);
//clear the block data for the new block
memset(block_data, 0x0, EXT2_BLOCK_SZ);
//subtract the direct blocks that we have accounted for
blocks_used -= EXT2_NDIR_BLOCKS;
location = ext2_format_block_bitmap(gdesc, 1);
//write the locations of the singly indirect blocks
for(blk = 0; blk < (blocks_used < EXT2_NIND_BLOCK ? blocks_used : EXT2_NIND_BLOCK); blk++)
*(block_data + blk) = *(block_locations + EXT2_NDIR_BLOCKS + blk);
floppy_write(block_data, EXT2_BLOCK_SZ, *location);
//write the location of the singly block
node->blocks[EXT2_NDIR_BLOCKS] = *location;
//write the doubly blocks
if(blocks_used > EXT2_NIND_BLOCK)
{
//subtract the singly indirect blocks that we have accounted for
blocks_used -= EXT2_NIND_BLOCK;
//clear the block data for the new block
memset(block_data, 0x0, EXT2_BLOCK_SZ);
//get a new location for the doubly block
location = ext2_format_block_bitmap(gdesc, 1);
//write the locations of the doubly indirect blocks
for(blk = 0; blk < (blocks_used < EXT2_NDIND_BLOCK ? blocks_used : EXT2_NDIND_BLOCK); blk += EXT2_NIND_BLOCK)
{
*(block_data + (blk / EXT2_NIND_BLOCK)) = ext2_singly_create(block_locations, EXT2_NDIR_BLOCKS + blk,
(blk + EXT2_NIND_BLOCK) <= blocks_used ?
EXT2_NIND_BLOCK : blocks_used % EXT2_NIND_BLOCK, gdesc);
}
floppy_write(block_data, EXT2_BLOCK_SZ, *location);
//write the location of the doubly block
node->blocks[EXT2_NDIR_BLOCKS + 1] = *location;
//write the triply blocks
if(blocks_used > EXT2_NDIND_BLOCK)
{
//subtract the singly indirect blocks that we have accounted for
blocks_used -= EXT2_NTIND_BLOCK;
//clear the block data for the new block
memset(block_data, 0x0, EXT2_BLOCK_SZ);
//get a new location for the triply block
location = ext2_format_block_bitmap(gdesc, 1);
//write the locations of the triply indirect blocks
for(blk = 0; blk < (blocks_used < EXT2_NTIND_BLOCK ? blocks_used : EXT2_NTIND_BLOCK); blk += EXT2_NDIND_BLOCK)
{
*(block_data + (blk / EXT2_NIND_BLOCK)) = ext2_doubly_create(block_locations, EXT2_NIND_BLOCK + EXT2_NDIR_BLOCKS + blk,
(blk + EXT2_NDIND_BLOCK) <= blocks_used ? EXT2_NDIND_BLOCK :
blocks_used % EXT2_NDIND_BLOCK, gdesc);
}
floppy_write(block_data, EXT2_BLOCK_SZ, *location);
//write the location of the triply block
node->blocks[EXT2_NDIR_BLOCKS + 2] = *location;
}
}
kfree(location);
kfree(block_data);
}
}
u32int ext2_data_to_inode_table(ext2_inode_t *data, ext2_group_descriptor_t *gdesc, ext2_superblock_t *sblock)
{
ext2_inode_t *buffer;
if(!ext2_g_inode_table)
{
buffer = (ext2_inode_t*)kmalloc(gdesc->inode_table_size * EXT2_BLOCK_SZ);
ext2_g_inode_table = buffer;
floppy_read(gdesc->inode_table_id, gdesc->inode_table_size * EXT2_BLOCK_SZ, (u32int*)buffer);
}else
buffer = ext2_g_inode_table;
//loop until a free space has opened up
u32int off = 0;
while(buffer[off].nlinks && off < sblock->inodes_per_group)
off++;
//if we did not find enough space, return an error
if(off == sblock->inodes_per_group)
return 1; //error
memcpy((u8int*)buffer + sizeof(ext2_inode_t) * off, data, sizeof(ext2_inode_t));
//write the new inode table buffer
floppy_write((u32int*)buffer, gdesc->inode_table_size * EXT2_BLOCK_SZ, gdesc->inode_table_id);
//purposly not freeing since buffer, either way with update, is the global variable which should not be cleared
//sucess!, return where we put it
return off;
}
u32int ext2_inode_from_inode_table(u32int inode_number, ext2_inode_t *output, ext2_group_descriptor_t *gdesc)
{
ext2_inode_t *buffer;
if(!ext2_g_inode_table)
{
buffer = ext2_get_inode_table(gdesc);
ext2_g_inode_table = buffer;
}else
buffer = ext2_g_inode_table;
memcpy(output, (u8int*)buffer + sizeof(ext2_inode_t) * inode_number, sizeof(ext2_inode_t));
//purposly not freeing since buffer, either way with update, is the global variable which should not be cleared
//~ kfree(buffer);
//sucess!
return 0;
}
ext2_inode_t *ext2_file_from_dir(ext2_inode_t *dir, char *name)
{
ext2_superblock_t *sblock;
ext2_group_descriptor_t *gdesc;
if(!ext2_g_sblock || !ext2_g_gdesc)
{
if(ext2_read_meta_data((ext2_superblock_t**)&sblock, (ext2_group_descriptor_t**)&gdesc))
return 0; //error
}else{
sblock = ext2_g_sblock;
gdesc = ext2_g_gdesc;
}
//get the inode table
ext2_inode_t *inode_table;
inode_table = ext2_get_inode_table(gdesc);
ext2_inode_t *inode;
inode = (ext2_inode_t*)kmalloc(sizeof(ext2_inode_t));
if(dir->type == EXT2_DIR)
{
struct ext2_dirent *dirent2;
u32int i;
for(i = 0; i < sblock->total_inodes; i++)
{
//get the dirent information at index i
dirent2 = ext2_dirent_from_dir(dir, i);
if(dirent2)
{
//the input name matches to the dirent2.name that we got
if(!strcmp(name, dirent2->name))
{
inode = ext2_inode_from_offset(dirent2->ino);
kfree(dirent2->name);
//~ kfree(inode);
kfree(sblock);
kfree(gdesc);
return inode;
}
}
kfree(dirent2->name);
}
}
kfree(inode);
kfree(sblock);
kfree(gdesc);
//no file found, error
return 0;
}
struct ext2_dirent *ext2_dirent_from_dir_data(ext2_inode_t *dir, u32int index, u32int *data)
{
if(dir->type == EXT2_DIR) //just to check if the input node is a directory
{
u32int i = 0;
u32int loop = 0, b = 0;
//this no data has been passed
if(!data)
return 0;
//loop forever, we will break when we find it or return and exit if we do not
for(;;)
{
//if the loop equals the index we are looking for
if(loop == index)
{
//if the rec_len of the direct has contents
if(*(u16int*)((u8int*)data + i + sizeof(dirent.ino)))
{
static struct ext2_dirent dirent2;
//extract the dirent information at the offset of i
dirent2.ino = *(u32int*)((u8int*)data + i);
dirent2.rec_len = *(u16int*)((u8int*)data + i + sizeof(dirent2.ino));
dirent2.name_len = *(u8int*)((u8int*)data + i + sizeof(dirent2.ino) + sizeof(dirent2.rec_len));
dirent2.file_type = *(u8int*)((u8int*)data + i + sizeof(dirent2.ino) + sizeof(dirent2.rec_len) + sizeof(dirent2.name_len));
//clears junk that may be contained in memory when kmallocing
dirent2.name = (char*)kmalloc(dirent2.name_len + 1);
//clears junk that may be contained in memory when kmallocing
memset(dirent2.name, 0, dirent2.name_len + 1);
//copies the name to dirent2.name
memcpy(dirent2.name, (char*)((u8int*)data + i + sizeof(dirent2.ino) + sizeof(dirent2.rec_len) + sizeof(dirent2.name_len) + sizeof(dirent2.file_type)), dirent2.name_len + 1);
*(dirent2.name + dirent2.name_len) = 0; //Adds terminating 0 to string
return &dirent2;
}else{
//error
return 0;
}
}else{
//this dirent is not the last one (there are more dirents after this one)
if(*(u16int*)((u8int*)data + i + sizeof(dirent.ino)))
{
//increase i with the rec_len that we get by moving fileheader sizeof(dirent.ino) (4 bytes) and reading its value
i += *(u16int*)((u8int*)data + i + sizeof(dirent.ino));
loop++;
}else //this is the last direct, if we have found nothing, exit
return 0;
}
}
}else{
return 0;
}
}
struct ext2_dirent *ext2_dirent_from_dir(ext2_inode_t *dir, u32int index)
{
if(dir->type == EXT2_DIR) //just to check if the input node is a directory
{
u32int i = 0;
u32int loop = 0, b = 0, *block;
block = (u32int*)kmalloc(EXT2_BLOCK_SZ);
ext2_block_of_set(dir, b, block);
//this dir has no blocks assigned
if(!block)
{
kfree(block);
return 0; //error
}
//loop forever, we will break when we find it or return and exit if we do not
for(;;)
{
//if the loop equals the index we are looking for
if(loop == index)
{
//if the rec_len of the direct has contents
if(*(u16int*)((u8int*)block + i + sizeof(dirent.ino)))
{
static struct ext2_dirent dirent2;
//extract the dirent information at the offset of i
dirent2.ino = *(u32int*)((u8int*)block + i);
dirent2.rec_len = *(u16int*)((u8int*)block + i + sizeof(dirent2.ino));
dirent2.name_len = *(u8int*)((u8int*)block + i + sizeof(dirent2.ino) + sizeof(dirent2.rec_len));
dirent2.file_type = *(u8int*)((u8int*)block + i + sizeof(dirent2.ino) + sizeof(dirent2.rec_len) + sizeof(dirent2.name_len));
//clears junk that may be contained in memory when kmallocing
dirent2.name = (char*)kmalloc(dirent2.name_len + 1);
//clears junk that may be contained in memory when kmallocing
memset(dirent2.name, 0, dirent2.name_len + 1);
//copies the name to dirent2.name
memcpy(dirent2.name, (char*)((u8int*)block + i + sizeof(dirent2.ino) + sizeof(dirent2.rec_len) + sizeof(dirent2.name_len) + sizeof(dirent2.file_type)), dirent2.name_len + 1);
*(dirent2.name + dirent2.name_len) = 0; //Adds terminating 0 to string
kfree(block);
return &dirent2;
}else{
kfree(block);
//error
return 0;
}
}else{
//this dirent is not the last one (there are more dirents after this one)
if(*(u16int*)((u8int*)block + i + sizeof(dirent.ino)))
{
//increase i with the rec_len that we get by moving fileheader sizeof(dirent.ino) (4 bytes) and reading its value
i += *(u16int*)((u8int*)block + i + sizeof(dirent.ino));
loop++;
}else{ //this is the last direct, add 1 to block and reset the offset (i)
i = 0;
//recalculate the block address
ext2_block_of_set(dir, ++b, block);
//this dir has not blocks assigned
if(!block)
{
kfree(block);
return 0;
}
}
}
}
}else{
return 0;
}
}
u32int ext2_block_of_set(ext2_inode_t *file, u32int block_number, u32int *block_output)
{
//if the block is a direct block
if(block_number >= 0 && block_number < EXT2_NDIR_BLOCKS)
{
if(!file->blocks[block_number])
{
block_output = 0;
return 0;
}
floppy_read(file->blocks[block_number], EXT2_BLOCK_SZ, block_output);
return file->blocks[block_number];
//if the block is in the singly set
}else if(block_number >= EXT2_NDIR_BLOCKS && block_number < EXT2_NDIR_BLOCKS + EXT2_NIND_BLOCK)
{
//the offset to the block inside the singly
u32int singly_offset = block_number - EXT2_NDIR_BLOCKS;
u32int *singly;
singly = (u32int*)kmalloc(EXT2_BLOCK_SZ);
if(!file->blocks[EXT2_IND_BLOCK])
{
kfree(singly);
block_output = 0;
return 0;
}
floppy_read(file->blocks[EXT2_IND_BLOCK], EXT2_BLOCK_SZ, singly);
u32int block = *(singly + singly_offset);
if(!block)
{
kfree(singly);
block_output = 0;
return 0;
}
floppy_read(block, EXT2_BLOCK_SZ, block_output);
kfree(singly);
return block;
//if the block is in the doubly set
}else if(block_number >= EXT2_NDIR_BLOCKS + EXT2_NIND_BLOCK &&
block_number < EXT2_NDIR_BLOCKS + EXT2_NIND_BLOCK + EXT2_NDIND_BLOCK)
{
u32int offset = block_number - (EXT2_NDIR_BLOCKS + EXT2_NIND_BLOCK);
u32int doubly_offset = offset / EXT2_NIND_BLOCK;
u32int singly_offset = offset % EXT2_NIND_BLOCK;
u32int *doubly, *singly;
singly = (u32int*)kmalloc(EXT2_BLOCK_SZ);
doubly = (u32int*)kmalloc(EXT2_BLOCK_SZ);
//error checking is always good
if(!file->blocks[EXT2_DIND_BLOCK])
{
kfree(singly);
kfree(doubly);
block_output = 0;
return 0;
}
floppy_read(file->blocks[EXT2_DIND_BLOCK], EXT2_BLOCK_SZ, doubly);
u32int singly_block_location = *(doubly + doubly_offset);
//error checking is always good
if(!singly_block_location)
{
kfree(singly);
kfree(doubly);
block_output = 0;
return 0;
}
floppy_read(singly_block_location, EXT2_BLOCK_SZ, singly);
u32int block = *(singly + singly_offset);
//error checking is always good
if(!block)
{
kfree(singly);
kfree(doubly);
block_output = 0;
return 0;
}
floppy_read(block, EXT2_BLOCK_SZ, block_output);
//free the stuff allocated
kfree(doubly);
kfree(singly);
return block;
//if the block is in the triply set
}else if(block_number >= EXT2_NDIR_BLOCKS + EXT2_NIND_BLOCK + EXT2_NDIND_BLOCK &&
block_number < EXT2_NDIR_BLOCKS + EXT2_NIND_BLOCK + EXT2_NDIND_BLOCK + EXT2_NTIND_BLOCK)
{
u32int offset = block_number - (EXT2_NDIR_BLOCKS + EXT2_NIND_BLOCK + EXT2_NDIND_BLOCK);
u32int triply_offset = offset / EXT2_NDIND_BLOCK;
u32int doubly_offset = (offset % EXT2_NDIND_BLOCK) / EXT2_NIND_BLOCK;
u32int singly_offset = (offset % EXT2_NDIND_BLOCK) % EXT2_NIND_BLOCK;
u32int *triply, *doubly, *singly;
triply = (u32int*)kmalloc(EXT2_BLOCK_SZ);
doubly = (u32int*)kmalloc(EXT2_BLOCK_SZ);
singly = (u32int*)kmalloc(EXT2_BLOCK_SZ);
if(!file->blocks[EXT2_TIND_BLOCK])
{
kfree(singly);
kfree(doubly);
kfree(triply);
block_output = 0;
return 0;
}
floppy_read(file->blocks[EXT2_TIND_BLOCK], EXT2_BLOCK_SZ, triply);
u32int doubly_block_location = *(triply + triply_offset);
if(!doubly_block_location)
{
kfree(singly);
kfree(doubly);
kfree(triply);
block_output = 0;
return 0;
}
floppy_read(doubly_block_location, EXT2_BLOCK_SZ, doubly);
u32int singly_block_location = *(doubly + doubly_offset);
if(!singly_block_location)
{
kfree(singly);
kfree(doubly);
kfree(triply);
block_output = 0;
return 0;
}
floppy_read(singly_block_location, EXT2_BLOCK_SZ, singly);
u32int block = *(singly + singly_offset);
if(!block)
{
kfree(singly);
kfree(doubly);
kfree(triply);
block_output = 0;
return 0;
}
floppy_read(block, EXT2_BLOCK_SZ, block_output);
kfree(triply);
kfree(doubly);
kfree(singly);
return block;
}else{
block_output = 0;
//the block number is not in range, error
return 0;
}
}
u32int ext2_write_block_of_set(ext2_inode_t *file, u32int block_number, u32int *block_data, u32int size)
{
if(!size)
return 0;
else if(size > EXT2_BLOCK_SZ)
size = EXT2_BLOCK_SZ;
//if the block is a direct block
if(block_number >= 0 && block_number < EXT2_NDIR_BLOCKS)
{
if(!file->blocks[block_number])
return 0;
floppy_write(block_data, size, file->blocks[block_number]);
return file->blocks[block_number];
//if the block is in the singly set
}else if(block_number >= EXT2_NDIR_BLOCKS && block_number < EXT2_NDIR_BLOCKS + EXT2_NIND_BLOCK)
{
//the offset to the block inside the singly
u32int singly_offset = block_number - EXT2_NDIR_BLOCKS;
u32int *singly;
singly = (u32int*)kmalloc(EXT2_BLOCK_SZ);
if(!file->blocks[EXT2_IND_BLOCK])
{
kfree(singly);
return 0;
}
floppy_read(file->blocks[EXT2_IND_BLOCK], EXT2_BLOCK_SZ, singly);
u32int block = *(singly + singly_offset);
if(!block)
{
kfree(singly);
return 0;
}
floppy_write(block_data, size, block);
kfree(singly);
return block;
//if the block is in the doubly set
}else if(block_number >= EXT2_NDIR_BLOCKS + EXT2_NIND_BLOCK &&
block_number < EXT2_NDIR_BLOCKS + EXT2_NIND_BLOCK + EXT2_NDIND_BLOCK)
{
u32int offset = block_number - EXT2_NDIR_BLOCKS;
u32int doubly_offset = offset / EXT2_NIND_BLOCK;
u32int singly_offset = offset % EXT2_NIND_BLOCK;
u32int *doubly, *singly;
singly = (u32int*)kmalloc(EXT2_BLOCK_SZ);
doubly = (u32int*)kmalloc(EXT2_BLOCK_SZ);
if(!file->blocks[EXT2_DIND_BLOCK])
{
kfree(singly);
kfree(doubly);
return 0;
}
floppy_read(file->blocks[EXT2_DIND_BLOCK], EXT2_BLOCK_SZ, doubly);
u32int singly_block_location = *(doubly + doubly_offset);
if(!singly_block_location)
{
kfree(singly);
kfree(doubly);
return 0;
}
floppy_read(singly_block_location, EXT2_BLOCK_SZ, singly);
u32int block = *(singly + singly_offset);
if(!block)
{
kfree(singly);
kfree(doubly);
return 0;
}
floppy_write(block_data, size, block);
kfree(doubly);
kfree(singly);
return block;
//if the block is in the triply set
}else if(block_number >= EXT2_NDIR_BLOCKS + EXT2_NIND_BLOCK + EXT2_NDIND_BLOCK &&
block_number < EXT2_NDIR_BLOCKS + EXT2_NIND_BLOCK + EXT2_NDIND_BLOCK + EXT2_NTIND_BLOCK)
{
u32int offset = block_number - EXT2_NDIR_BLOCKS;
u32int triply_offset = offset / EXT2_NDIND_BLOCK;
u32int doubly_offset = (offset % EXT2_NDIND_BLOCK) / EXT2_NIND_BLOCK;
u32int singly_offset = (offset % EXT2_NDIND_BLOCK) % EXT2_NIND_BLOCK;
u32int *triply, *doubly, *singly;
triply = (u32int*)kmalloc(EXT2_BLOCK_SZ);
doubly = (u32int*)kmalloc(EXT2_BLOCK_SZ);
singly = (u32int*)kmalloc(EXT2_BLOCK_SZ);
if(!file->blocks[EXT2_TIND_BLOCK])
{
kfree(singly);
kfree(doubly);
kfree(triply);
return 0;
}
floppy_read(file->blocks[EXT2_TIND_BLOCK], EXT2_BLOCK_SZ, triply);
u32int doubly_block_location = *(triply + triply_offset);
if(!doubly_block_location)
{
kfree(singly);
kfree(doubly);
kfree(triply);
return 0;
}
floppy_read(doubly_block_location, EXT2_BLOCK_SZ, doubly);
u32int singly_block_location = *(doubly + doubly_offset);
if(!singly_block_location)
{
kfree(singly);
kfree(doubly);
kfree(triply);
return 0;
}
floppy_read(singly_block_location, EXT2_BLOCK_SZ, singly);
u32int block = *(singly + singly_offset);
if(!block)
{
kfree(singly);
kfree(doubly);
kfree(triply);
return 0;
}
floppy_write(block_data, size, block);