This repository was archived by the owner on Dec 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3600fs.c
2027 lines (1774 loc) · 58 KB
/
3600fs.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
/*
* CS3600, Spring 2014
* Project 2 Starter Code
* (c) 2013 Alan Mislove
*
* This file contains all of the basic functions that you will need
* to implement for this project. Please see the project handout
* for more details on any particular function, and ask on Piazza if
* you get stuck.
*/
#define FUSE_USE_VERSION 26
#ifdef linux
/* For pread()/pwrite() */
#define _XOPEN_SOURCE 500
#endif
#define _POSIX_C_SOURCE 199309
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <errno.h>
#include <assert.h>
#include <sys/statfs.h>
#include <sys/types.h>
#include <limits.h>
#ifdef HAVE_SETXATTR
#include <sys/xattr.h>
#endif
#include "3600fs.h"
#include "disk.h"
// Cache size; supports for at most 1000 entries
#define CACHE_SIZE 1000
// Magic number to identify our disk
const int MAGICNUMBER = 184901;
// Number of direntries per dirent
const int NUM_DIRENTRIES = BLOCK_SIZE / sizeof(direntry);
// Number of block in an indirect
const int NUM_INDIRECT_BLOCKS = BLOCK_SIZE / sizeof(blocknum);
// Cache of file_locs
cache_entry *cache[CACHE_SIZE];
// Get the file_loc specified by the given path
// If the file_loc returned is invalid, then the file
// is not in the cache.
static file_loc get_cache_entry(char *path) {
// check each occupied cache entry for the path
for (int i = 0; i < CACHE_SIZE; i++) {
// cache hit
if (!cache[i]->open && strcmp(cache[i]->path, path) == 0) {
return cache[i]->loc;
}
}
// cache miss
file_loc loc;
loc.valid = 0;
return loc;
}
// Gets the next eviction entry (the oldest one).
static int next_evict() {
long long oldest = ULLONG_MAX;
for (int i = 0; i < CACHE_SIZE; i++) {
// update the oldest entry
if (oldest > cache[i]->ts) {
oldest = cache[i]->ts;
}
}
return oldest;
}
// Add the given file to the cache with the given file_loc
// If the file is already in cache, we replace that entry.
// Otherwise if there is no open cache entry, we evict the
// entry located at the next_cache_entry.
static void add_cache_entry(char *path, file_loc loc) {
// create a new path copy
int path_len = strlen(path) + 1;
char * new_path = (char *) malloc(path_len);
strncpy(new_path, path, path_len);
for (int i = 0; i < CACHE_SIZE; i++) {
// there is an open slot; add a new cache entry there
if (cache[i]->open) {
// free old path
free(cache[i]->path);
// add new path
cache[i]->path = new_path;
// add loc; entry is now used
cache[i]->loc = loc;
cache[i]->open = 0;
return;
}
}
// otherwise no open entry was found, replace the oldest
int oldest = next_evict();
cache[oldest]->path = new_path;
cache[oldest]->loc = loc;
}
// Update the cache entry specified by the file from to
// the file specified by to.
static void update_cache_entry(char *from, char *to) {
for (int i = 0; i < CACHE_SIZE; i++) {
// we found the file from; change it to
if (!cache[i]->open && strcmp(cache[i]->path, from) == 0) {
// free old path
free(cache[i]->path);
// add new path
int path_len = strlen(to) + 1;
char * new_path = (char *) malloc(path_len);
strncpy(new_path, to, path_len);
cache[i]->path = new_path;
return;
}
}
}
// Remove the given file from the cache if it exists
static void remove_cache_entry(char *path) {
for (int i = 0; i < CACHE_SIZE; i++) {
// mark an entry that is not open and contains the path as open
if (!cache[i]->open && strcmp(cache[i]->path, path) == 0) {
cache[i]->open = 1;
return;
}
}
}
// Initialize the cache state. All entries are open.
static void init_cache() {
for (int i = 0; i < CACHE_SIZE; i++) {
// open cache entry
cache_entry *ce = (cache_entry *) malloc(sizeof(cache_entry));
ce->path = NULL;
ce->open = 1;
ce->ts = 0;
// set each cache entry to open
cache[i] = ce;
}
}
/*
* Initialize filesystem. Read in file system metadata and initialize
* memory structures. If there are inconsistencies, now would also be
* a good time to deal with that.
*
* HINT: You don't need to deal with the 'conn' parameter AND you may
* just return NULL.
*
*/
static void* vfs_mount(struct fuse_conn_info *conn) {
fprintf(stderr, "vfs_mount called\n");
// Do not touch or move this code; connects the disk
dconnect();
vcb myVcb;
char *tmp = (char*) malloc(BLOCKSIZE);
// Write to block 0
dread(0, tmp);
memcpy(&myVcb, tmp, sizeof(vcb));
// wrong file system; disconnect
if (myVcb.magic != MAGICNUMBER) {
fprintf(stdout, "Attempt to mount wrong filesystem!\n");
// disconnect
dunconnect();
}
// initialize the file_loc cache
init_cache();
return NULL;
}
/*
* Called when your file system is unmounted.
*
*/
static void vfs_unmount (void *private_data) {
fprintf(stderr, "vfs_unmount called\n");
/* 3600: YOU SHOULD ADD CODE HERE TO MAKE SURE YOUR ON-DISK STRUCTURES
ARE IN-SYNC BEFORE THE DISK IS UNMOUNTED (ONLY NECESSARY IF YOU
KEEP DATA CACHED THAT'S NOT ON DISK */
// Do not touch or move this code; unconnects the disk
dunconnect();
}
/*
*
* Given an absolute path to a file/directory (i.e., /foo ---all
* paths will start with the root directory of the CS3600 file
* system, "/"), you need to return the file attributes that is
* similar stat system call.
*
* HINT: You must implement stbuf->stmode, stbuf->st_size, and
* stbuf->st_blocks correctly.
*
*/
static int vfs_getattr(const char *path, struct stat *stbuf) {
fprintf(stderr, "\nIN vfs_getattr\n");
// Do not mess with this code
stbuf->st_nlink = 1; // hard links
stbuf->st_rdev = 0;
stbuf->st_blksize = BLOCKSIZE;
// create zeroed buffer
char buf[BLOCKSIZE];
memset(buf, 0, BLOCKSIZE);
// get attr if exists
file_loc parent = get_root_dir();
file_loc loc = get_dir(path, path, &parent);
// check if the file loc is a directory
if (loc.valid && loc.is_dir) {
// read in dnode
dnode this_dnode = get_dnode(loc.node_block.block, buf);
stbuf->st_mode = this_dnode.mode | S_IFDIR;
// update stats
stbuf->st_uid = this_dnode.user; // file uid
stbuf->st_gid = this_dnode.group; // file gid
stbuf->st_atime = this_dnode.access_time.tv_sec; // access time
stbuf->st_mtime = this_dnode.modify_time.tv_sec; // modify time
stbuf->st_ctime = this_dnode.create_time.tv_sec; // create time
stbuf->st_size = this_dnode.size; // file size
stbuf->st_blocks = this_dnode.size / BLOCKSIZE; // file size in blocks
return 0;
}
// we have a regular file
else if (loc.valid) {
// read in inode
inode this_inode;
dread(loc.node_block.block, buf);
memcpy(&this_inode, buf, sizeof(inode));
stbuf->st_mode = this_inode.mode | S_IFREG;
// update stats
stbuf->st_uid = this_inode.user; // file uid
stbuf->st_gid = this_inode.group; // file gid
stbuf->st_atime = this_inode.access_time.tv_sec; // access time
stbuf->st_mtime = this_inode.modify_time.tv_sec; // modify time
stbuf->st_ctime = this_inode.create_time.tv_sec; // create time
stbuf->st_size = this_inode.size; // file size
stbuf->st_blocks = this_inode.size / BLOCKSIZE; // file size in blocks
return 0;
}
// otherwise file not found
else {
return -ENOENT;
}
}
/*
* Given an absolute path to a directory (which may or may not end in
* '/'), vfs_mkdir will create a new directory named dirname in that
* directory, and will create it with the specified initial mode.
*
* HINT: Don't forget to create . and .. while creating a
* directory.
*/
static int vfs_mkdir(const char *path, mode_t mode) {
fprintf(stderr, "\nIN vfs_mkdir\n");
// Allocate appropriate memory
char buf[BLOCKSIZE];
// Get the dnode
dnode thisDnode = get_dnode(1, buf);
blocknum file;
file_loc loc;
// blocknum of Inode of file
file_loc parent = get_root_dir();
loc = get_dir(path, path, &parent);
// See if the file exists
if (loc.valid) {
// File exists...
return -EEXIST;
}
// Get next free block
blocknum this_dnode = get_free();
// Check that the free block is valid
if (this_dnode.valid) {
// Set up Inode; if not 1, the inode was not set
if (create_dnode(this_dnode, buf, mode) != 1)
return -1;
// Loop through dnode direct
if (create_node_direct_dirent(&thisDnode, this_dnode, loc.name, 1, buf) == 1) {
return 0;
}
// Loop through dnode -> single_indirect
if (create_node_single_indirect_dirent(thisDnode.single_indirect, this_dnode, loc.name, 1, buf) == 1) {
memset(buf, 0, BLOCKSIZE);
memcpy(buf, &thisDnode, sizeof(dnode));
dwrite(1, buf);
return 0;
}
// Loop through dnode -> double_indirect
else if (create_node_double_indirect_dirent(thisDnode.double_indirect, this_dnode, loc.name, 1, buf) == 1) {
memset(buf, 0, BLOCKSIZE);
memcpy(buf, &thisDnode, sizeof(dnode));
dwrite(1, buf);
return 0;
}
// No direntries available
else {
return -1;
}
}
// Out of memory message?
return -1;
}
/** Read directory
*
* Given an absolute path to a directory, vfs_readdir will return
* all the files and directories in that directory.
*
* HINT:
* Use the filler parameter to fill in, look at fusexmp.c to see an example
* Prototype below
*
* Function to add an entry in a readdir() operation
*
* @param buf the buffer passed to the readdir() operation
* @param name the file name of the directory entry
* @param stat file attributes, can be NULL
* @param off offset of the next entry or zero
* @return 1 if buffer is full, zero otherwise
* typedef int (*fuse_fill_dir_t) (void *buf, const char *name,
* const struct stat *stbuf, off_t off);
*
* Your solution should not need to touch fi
*
*/
static int vfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi)
{
fprintf(stderr, "\nIN vfs_readdir\n");
// get the file_loc
file_loc parent = get_root_dir();
file_loc loc = get_dir(path, path, &parent);
// we know it's the root dir
char tmp_buf[BLOCKSIZE];
dnode this_dnode = get_dnode(loc.node_block.block, tmp_buf);
// list the direct entries of the dnode
list_entries(this_dnode.direct, NUM_DIRECT, filler, buf);
// list the single indirect entries
list_single(this_dnode.single_indirect, filler, buf);
// list the double indirect entries of the dnode
list_double(this_dnode.double_indirect, filler, buf);
return 0;
}
// list entries in the double indirect if there are any
void list_double(blocknum d, fuse_fill_dir_t filler, void *buf) {
// check that there are single indirect entries
if (d.valid) {
// get the double indirect entries
indirect dub = get_indirect(d);
// list entries for each indirect entry
for (int j = 0; j < NUM_INDIRECT_BLOCKS; j++) {
// list the entries of the ith indirect if there is data there
if (dub.blocks[j].valid) {
indirect i = get_indirect(dub.blocks[j]);
list_entries(i.blocks, NUM_INDIRECT_BLOCKS, filler, buf);
}
}
}
}
// list entries in the single indirect if there are any
void list_single(blocknum s, fuse_fill_dir_t filler, void *buf) {
// check that there are single indirect entries
if (s.valid) {
// get the indirect
indirect i = get_indirect(s);
// list the entries
list_entries(i.blocks, NUM_INDIRECT_BLOCKS, filler, buf);
}
}
indirect get_indirect(blocknum b) {
char tmp_buf[BLOCKSIZE];
memset(tmp_buf, 0, BLOCKSIZE);
// read indirect from disk
indirect i;
dread(b.block, tmp_buf);
memcpy(&i, tmp_buf, sizeof(indirect));
return i;
}
// List entries in the given array of dirent blocknums
void list_entries(blocknum d[], size_t size, fuse_fill_dir_t filler, void *buf) {
// temporary buffer
char tmp_buf[BLOCKSIZE];
// iterate over all dirents in d
for (int i = 0; i < size; i++) {
// load dirent from disk into memory
blocknum dirent_b = d[i];
// read only valid dirents
if (dirent_b.valid) {
dirent de;
memset(tmp_buf, 0, BLOCKSIZE);
dread(dirent_b.block, tmp_buf);
memcpy(&de, tmp_buf, sizeof(dirent));
// iterate over all direnties in dirent_b
for (int j = 0; j < NUM_DIRENTRIES; j++) {
// get the jth entry and load into a stat struct
direntry dentry = de.entries[j];
// read only valid direntries
if (dentry.block.valid) {
struct stat st;
memset(&st, 0, sizeof(st));
// set inode block number
st.st_ino = dentry.block.block;
// set the mode
st.st_mode = dentry.type;
// fill in the filler
char *name = dentry.name;
// the filler is non 0 when buf is filled
if (filler(buf, name, &st, 0) != 0) {
break;
}
}
}
}
}
}
/*
* Given an absolute path to a file (for example /a/b/myFile), vfs_create
* will create a new file named myFile in the /a/b directory.
*
*/
static int vfs_create(const char *path, mode_t mode, struct fuse_file_info *fi) {
fprintf(stderr, "\nIN vfs_create\n");
// Allocate appropriate memory
char buf[BLOCKSIZE];
blocknum file;
file_loc loc;
// blocknum of Inode of file
file_loc parent = get_root_dir();
loc = get_dir(path, path, &parent);
// dnode where we want to write file to
dnode thisDnode = get_dnode(parent.node_block.block, buf);
// See if the file exists
if (loc.valid) {
// File exists...
return -EEXIST;
}
// Get next free block
blocknum this_inode = get_free();
// Check that the free block is valid
if (this_inode.valid) {
// Set up Inode; if not 1, the inode was not set
if (create_inode(this_inode, buf, mode) != 1)
return -1;
// Loop through dnode direct
if (create_node_direct_dirent(&thisDnode, this_inode, loc.name, 0, buf) == 1) {
return 0;
}
// Loop through dnode -> single_indirect
if (create_node_single_indirect_dirent(thisDnode.single_indirect, this_inode, loc.name, 0, buf) == 1) {
memset(buf, 0, BLOCKSIZE);
memcpy(buf, &thisDnode, sizeof(dnode));
dwrite(1, buf);
return 0;
}
// Loop through dnode -> double_indirect
else if (create_node_double_indirect_dirent(thisDnode.double_indirect, this_inode, loc.name, 0, buf) == 1) {
memset(buf, 0, BLOCKSIZE);
memcpy(buf, &thisDnode, sizeof(dnode));
dwrite(1, buf);
return 0;
}
// No direntries available
else {
return -1;
}
}
return -1;
}
// Initialize dnode metadata to the given dnode blocknum.
// Returns 0 if the block b is not valid.
// buf should be BLOCKSIZE bytes.
int create_dnode(blocknum b, char *buf, mode_t mode) {
// make sure the block is valid
if (b.valid) {
dnode new_node;
// set file metadata
new_node.size = 0;
new_node.user = getuid();
new_node.group = getgid();
new_node.mode = mode;
clockid_t now = CLOCK_REALTIME;
clock_gettime(now, &new_node.access_time);
clock_gettime(now, &new_node.modify_time);
clock_gettime(now, &new_node.create_time);
// Invalidate all direct blocks
blocknum invalid;
invalid.valid = 0;
invalid.block = 0;
for (int i = 0; i < NUM_DIRECT; i++) {
new_node.direct[i] = invalid;
}
// invalidate the single and double indirects
new_node.single_indirect.valid = 0;
new_node.double_indirect.valid = 0;
// zero out the buffer and write inode to block b
memset(buf, 0, BLOCKSIZE);
memcpy(buf, &new_node, sizeof(inode));
dwrite(b.block, buf);
return 1;
}
// the block is not valid
return 0;
}
// Initialize inode metadata to the given inode blocknum.
// Returns 0 if the block b is not valid.
// buf should be BLOCKSIZE bytes.
int create_inode(blocknum b, char *buf, mode_t mode) {
// make sure the block is valid
if (b.valid) {
inode new_node;
// set file metadata
new_node.size = 0;
new_node.user = getuid();
new_node.group = getgid();
new_node.mode = mode;
clockid_t now = CLOCK_REALTIME;
clock_gettime(now, &new_node.access_time);
clock_gettime(now, &new_node.modify_time);
clock_gettime(now, &new_node.create_time);
// Invalidate all direct blocks
blocknum invalid;
invalid.valid = 0;
invalid.block = 0;
for (int i = 0; i < NUM_DIRECT; i++) {
new_node.direct[i] = invalid;
}
// invalidate the single and double indirects
new_node.single_indirect.valid = 0;
new_node.double_indirect.valid = 0;
// zero out the buffer and write inode to block b
memset(buf, 0, BLOCKSIZE);
memcpy(buf, &new_node, sizeof(inode));
dwrite(b.block, buf);
return 1;
}
// the block is not valid
return 0;
}
// Initialize the given blocknum to a dirent
// returns 0 if there is an error in doing so
int create_dirent(blocknum b, char *buf) {
// make sure the given block is valid
if (b.valid) {
// put an indirect structure in the given block
memset(buf, 0, BLOCKSIZE);
dirent new_dirent;
// invalidate all blocks
for (int i = 0; i < NUM_DIRENTRIES; i++) {
new_dirent.entries[i].block.valid = 0;
}
memcpy(buf, &new_dirent, sizeof(dirent));
dwrite(b.block, buf);
return 1;
}
// Block was not valid, error
return 0;
}
// Initialize the given blocknum to an indirect
// returns 0 if there is an error
int create_indirect(blocknum b, char *buf) {
// make sure the given block is valid
if (b.valid) {
// put an indirect structure in the given block
memset(buf, 0, BLOCKSIZE);
indirect new_indirect;
// invalidate all blocks
for (int i = 0; i < NUM_INDIRECT_BLOCKS; i++) {
new_indirect.blocks[i].valid = 0;
}
memcpy(buf, &new_indirect, sizeof(indirect));
dwrite(b.block, buf);
return 1;
}
// Block was not valid, error
return 0;
}
// Create a file at the next open direntry in this dirent
// returns 0 if there are no open direntries
// Returns 1 on success.
// Returns -1 on error.
// the passed in buf is meant as a reusable buf so we can save memory.
int create_node_dirent(blocknum d, blocknum node, const char *name, unsigned int type, char *buf) {
// this must now be valid
d.valid = 1;
// get the dirent object
memset(buf, 0, BLOCKSIZE);
dread(d.block, buf);
dirent dir;
memcpy(&dir, buf, BLOCKSIZE);
// look through each of the direntries
for (int i = 0; i < NUM_DIRENTRIES; i++) {
// add at the ith direntry
if (!dir.entries[i].block.valid) {
// create a new direntry for the inode
dir.entries[i].block = node;
// add the name
strncpy(dir.entries[i].name, name, MAX_FILENAME_LEN - 1);
dir.entries[i].name[MAX_FILENAME_LEN - 1] = '/0';
// set the type to a file
dir.entries[i].type = type;
// write to dirent to disk
memset(buf, 0, BLOCKSIZE);
memcpy(buf, &dir, BLOCKSIZE);
dwrite(d.block, buf);
return 1;
}
}
return 0;
}
// Create a file at the next open direntry in the given direct array.
// Returns 0 if there is no space available for the new file in direct.
// Returns 1 on success.
// Returns -1 on error.
// the passed in buf is meant as a reusable buf so we can save memory.
int create_node_direct_dirent(dnode *thisDnode, blocknum node, const char *name, unsigned int type, char *buf) {
// Look for available direntry location to put this new file
// Loop through dnode -> direct
for (int i = 0; i < NUM_DIRECT; i++) {
// check if valid, if not get one, assign to i, call function
if (!(thisDnode->direct[i].valid)) {
// create a dirent for the ith block
// get the next free block
blocknum temp_free = get_free();
// try to create a dirent
if (!(create_dirent(temp_free, buf))) {
// error with create_indirect
return -1;
}
// otherwise we were able to create a dirent, set that to the ith block
thisDnode->direct[i] = temp_free;
}
// dirent is valid, look through direntries for next open slot
if (create_node_dirent(thisDnode->direct[i], node, name, type, buf)) {
memset(buf, 0, BLOCKSIZE);
memcpy(buf, thisDnode, sizeof(dnode));
dwrite(1, buf);
return 1;
}
}
// No space in direct for new file
return 0;
}
// Create a file at the next open direntry in this single_indirect
// returns 0 if there are no open direntries
// Returns 1 on success.
// Returns -1 on error.
// the passed in buf is meant as a reusable buf so we can save memory.
int create_node_single_indirect_dirent(blocknum s, blocknum node, const char *name, unsigned int type, char *buf) {
// All other blocks free, now this must be valid
s.valid = 1;
memset(buf, 0, BLOCKSIZE);
dread(s.block, buf);
indirect single_indirect;
memcpy(&single_indirect, buf, BLOCKSIZE);
for (int i = 0; i < NUM_INDIRECT_BLOCKS; i++) {
// check if valid, if not get one, assign to i, call function
if (!(single_indirect.blocks[i].valid)) {
// create a single indirect for the ith block
// get the next free block
blocknum temp_free = get_free();
// try to create an indirect
if (!(create_indirect(temp_free, buf))) {
// error with create_indirect
return -1;
}
// otherwise we were able to create an indirect, set that to the ith block
single_indirect.blocks[i] = temp_free;
}
// try to create a block at i
if (create_node_dirent(single_indirect.blocks[i], node, name, type, buf)) {
return 1;
}
}
// No space available
return 0;
}
// Create a file at the next open direntry in this double_indirect
// returns 0 if there are no open direntries
// Returns 1 on success.
// Returns -1 on error.
// the passed in buf is meant as a reusable buf so we can save memory.
int create_node_double_indirect_dirent(blocknum d, blocknum node, const char *name, unsigned int type, char *buf) {
// All other blocks free, now this must be valid
d.valid = 1;
memset(buf, 0, BLOCKSIZE);
dread(d.block, buf);
indirect double_indirect;
memcpy(&double_indirect, buf, BLOCKSIZE);
for (int i = 0; i < NUM_INDIRECT_BLOCKS; i++) {
// check if valid, if not get one, assign to i, call function
if (!(double_indirect.blocks[i].valid)) {
// create a single indirect for the ith block
// get the next free block
blocknum temp_free = get_free();
// try to create an indirect
if (!(create_indirect(temp_free, buf))) {
// error with create_indirect
return -1;
}
// otherwise we were able to create an indirect, set that to the ith block
double_indirect.blocks[i] = temp_free;
}
// try to create a block at i
if (create_node_single_indirect_dirent(double_indirect.blocks[i], node, name, type, buf)) {
return 1;
}
}
// No space available
return 0;
}
// Reads the vcb at the given block number into buf
vcb get_vcb(char *buf) {
// Allocate appropriate memory
memset(buf, 0, BLOCKSIZE);
// Read Vcb
dread(0, buf);
// Put the read data into a Dnode struct
vcb this_vcb;
memcpy(&this_vcb, buf, sizeof(vcb));
return this_vcb;
}
// Returns the file_loc of the given path's Inode form the parent.
// If the file specified by the path does not have an associated Inode,
// then the function returns an invalid file_loc.
file_loc get_file(char *abs_path, char *path, file_loc parent) {
// Start at the Dnode and search the direct, single_indirect,
// and double_indirect
// general-purpose temporary buffer
char *buf = (char *) malloc(BLOCKSIZE);
// temporary file_loc buffer
file_loc loc;
// check the cache for our file
loc = get_cache_entry(abs_path);
if (loc.valid) {
fprintf(stderr, "Cache Hit: %s\n", abs_path);
return loc;
}
// Read data into a Dnode struct
dnode thisDnode = get_dnode(parent.node_block.block, buf);
// look at the right snippet of the path
char *file_path = path;
unsigned int name_len = 1;
while (*file_path != '\0' && *file_path != '/') {
name_len++;
file_path++;
}
char name[name_len];
strncpy(name, path, name_len);
name[name_len - 1] = '\0';
// Check each dirent in direct to see if the file specified by
// path exists in the filesystem.
loc = get_inode_direct_dirent(&thisDnode, buf, name);
// If valid return the value of the file_loc; update the cache
if (loc.valid) {
free(buf);
add_cache_entry(abs_path, loc);
return loc;
}
// Check each dirent in the single_indirect to see if the file specified by
// path exists in the filesystem.
loc = get_inode_single_indirect_dirent(thisDnode.single_indirect, buf, name);
// If valid return the value of the file_loc; update the cache
if (loc.valid) {
free(buf);
// set the direct, single_indirect, and double_indirect flags
loc.direct = 0;
loc.single_indirect = 1;
loc.double_indirect = 0;
add_cache_entry(abs_path, loc);
return loc;
}
// Check each dirent in each indirect in the double_indirect to see if the
// file specified by path exists in the filesystem.
loc = get_inode_double_indirect_dirent(thisDnode.double_indirect, buf, name);
// If valid return the value of the file_loc; update the cache
if (loc.valid) {
free(buf);
// set the direct, single_indirect, and double_indirect flags
loc.direct = 0;
loc.single_indirect = 0;
loc.double_indirect = 1;
add_cache_entry(abs_path, loc);
return loc;
}
// The file specified by path does not exist in our file system.
// Return invalid file_loc with the short name.
free(buf);
strncpy(loc.name, name, name_len);
return loc;
}
// Get the root file_loc.
file_loc get_root_dir() {
// the root block
blocknum b;
b.valid = 1;
b.block = 1;
// root file_loc
file_loc root;
root.node_block = b;
root.is_dir = 1;
root.valid = 1;
return root;
}
// get the directory specified by path in the parent directory.
// The abs path is what the cache uses to perform lookups.
file_loc get_dir(char *abs_path, char *path, file_loc *parent) {
// for the annoying case of when / is its own parent
if (strcmp(path, "/") == 0) {
return *parent;
}
// a pointer to walk the string
char *p = path;
// remove leading /
if (*p == '/') {
p++;
}
// the start address of the first token
char *start = p;
// the length of a sub string delimited by /; '/0'
int child_len = 1;
// get the next token
while (*p != '/' && *p != '\0') {
p++;
child_len++;
}
// set the child path
char child_path[child_len];
strncpy(child_path, start, child_len);
child_path[child_len - 1] = '\0';
// check if the child exists
file_loc child_loc = get_file(abs_path, child_path, *parent);
// file not found return the invalid file loc
// if the child is a file return it
if (!child_loc.valid || !child_loc.is_dir) {
return child_loc;
}
// if the child is a dir, recur if there more
else if (*p != '\0') {
parent->node_block = child_loc.node_block;
return get_dir(abs_path, p, parent);
}
// otherwise return the directory file_loc
else {
return child_loc;
}
}
// Reads the dnode at the given block number into buf
dnode get_dnode(unsigned int b, char *buf) {
// Allocate appropriate memory
memset(buf, 0, BLOCKSIZE);
// Read Dnode
dread(b, buf);
// Put the read data into a dnode struct
dnode this_dnode;
memcpy(&this_dnode, buf, sizeof(dnode));
return this_dnode;
}
// Write the given dnode (d) to disk at the given block (b)
int write_dnode(unsigned int b, char *buf, dnode d) {
// Allocate appropriate memory
memset(buf, 0, BLOCKSIZE);
// Put the dnode into the buf
memcpy(buf, &d, sizeof(dnode));
// Write dnode data, return 1 if successful
if (dwrite(b, buf) < 0) {
return 1;
}
// Error in write
return 0;
}
// Reads the inode at the given block number
inode get_inode(unsigned int b, char *buf) {
// Allocate appropriate memory
memset(buf, 0, BLOCKSIZE);
// Read Dnode
dread(b, buf);
// Put the read data into a inode struct
inode this_inode;
memcpy(&this_inode, buf, sizeof(inode));
return this_inode;
}
// Write the given inode (i) to disk at the given block (b)
// The passed in buf should be of size BLOCKSIZE
int write_inode(unsigned int b, char *buf, inode i) {
// Allocate appropriate memory
memset(buf, 0, BLOCKSIZE);
// Put the inode into the buf
memcpy(buf, &i, sizeof(inode));
// Write inode data, return 1 if successful
if (dwrite(b, buf) < 0) {
return 1;
}
// Error in write