-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyfs.c
1103 lines (960 loc) · 35.2 KB
/
myfs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
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
#define FUSE_USE_VERSION 26
#include <fuse.h>
#include <errno.h>
#include <libgen.h>
#include "myfs.h"
unqlite *pDb;
uuid_t zero_uuid;
/**
* @param id - Given the id, retrieve
* @param data - where returning results will be put into
* @param size - expected size of fetched results
*/
void read_from_db(const uuid_t id, void *data, size_t size)
{
int rc;
unqlite_int64 nBytes = size;
// check if correct results found
rc = unqlite_kv_fetch(pDb, id, KEY_SIZE, NULL, &nBytes);
error_handler(rc);
// check if we retrieved the correct thing with correct size
if (nBytes != size)
{
write_log("fetch: Unexpected size. Expected %d, current %d\n", size, nBytes);
exit(-1);
}
// retrieve the fcb
unqlite_kv_fetch(pDb, id, KEY_SIZE, data, &nBytes);
}
/**
* Write fcb back to the database
* @param id - Given the id, store
* @param data - data to be put into the database
* @param size - size of data
*/
void write_to_db(const uuid_t id, void *data, size_t size)
{
write_log("Write Size: %d \n", size);
int rc = unqlite_kv_store(pDb, id, KEY_SIZE, data, size);
error_handler(rc);
}
/**
* Delete item in database based on the id
* @param id - the key
*/
void delete_from_db(const uuid_t id)
{
write_log("delete %d \n", id);
int rc = unqlite_kv_delete(pDb, id, KEY_SIZE);
error_handler(rc);
}
/**
* Make a new fcb for dir or file
* @param cur_fcb - where to store the new fcb
* @param mode - the mode
* @param is_dir - 1 for dir
*/
void make_new_fcb(fcb *cur_fcb, mode_t mode, int is_dir)
{
memset(cur_fcb, 0, sizeof(fcb));
if (is_dir)
cur_fcb->mode = S_IFDIR | mode;
else
cur_fcb->mode = S_IFREG | mode;
time_t now = time(NULL);
cur_fcb->atime = now;
cur_fcb->mtime = now;
cur_fcb->ctime = now;
cur_fcb->uid = getuid();
cur_fcb->gid = getgid();
cur_fcb->count = 1; // reference count is initialised to 1
}
/**
* Create an empty data block, write to database, and fill its id into arguments
* @param block_id - id to be filled with the new data block
* @param block_size - the size of the new data block
*/
void make_empty_data_block(uuid_t block_id, size_t block_size)
{
uuid_generate(block_id); // generate a new uuid
char *empty_block = malloc(sizeof(char) * block_size); // create a new empty block
memset(empty_block, 0, sizeof(block_size));
write_to_db(block_id, empty_block, sizeof(char) * block_size); // write the new block to database
free(empty_block);
}
/**
* Obtain a new iterator over the current fcb. It will iterate over each data blocks the fcb has
* @param fcb - The fcb whose data blocks will be iterated
* @param create - 1 for creating a new data block when iterating to an used block
* @return The iterator contains information about the current status
*/
fcb_iterator make_iterator(fcb *fcb, int create)
{
fcb_iterator iterator = {
.fcb = fcb,
.level = 0,
.index = 0,
.create = create};
return iterator;
}
/**
* Get the root fcb
* @param root_fcb - where stores the root fcb
* @return 0 on success, < 0 on failure
*/
int get_root_fcb(fcb *root_fcb)
{
read_from_db(ROOT_OBJECT_KEY, root_fcb, sizeof(fcb));
return 0;
}
/**
* Get the next direct data block
* @param iterator - the current iterator
* @param block_size - the size of the data block for potential create_empty_data_block
* @param next_block_id - filled by the next block id
* @return 0 on success, < 0 on failure
*/
int get_next_direct_block(fcb_iterator *iterator, size_t block_size, uuid_t next_block_id)
{
if (iterator->level != 0) // check the level
return -1;
// if meeting an unused block
if (uuid_compare(iterator->fcb->data_blocks[iterator->index], zero_uuid) == 0)
{
if (iterator->create)
{ // creation allowed, creating an empty data block
make_empty_data_block(next_block_id, block_size);
uuid_copy(iterator->fcb->data_blocks[iterator->index], next_block_id); // update the new id
}
else // creation not allowed, indicating that iterating ends
return -1;
}
else // if meeting a used block, directly update the next_block_id
uuid_copy(next_block_id, iterator->fcb->data_blocks[iterator->index]);
iterator->index++; // index increments
if (iterator->index == NUMBER_DIRECT_BLOCKS)
{ // need to move to next level: indirect
iterator->level += 1;
iterator->index = 0;
}
return 0;
}
/**
* Get the next data block in indirect level
* @param iterator - the current iterator
* @param block_size - the size of the data block for potential create_empty_data_block
* @param next_block_id - filled by the next block id
* @return 0 on success, < 0 on failure
*/
int get_next_indirect_block(fcb_iterator *iterator, size_t block_size, uuid_t next_block_id)
{
if (iterator->level != 1 || iterator->index >= MAX_UUIDS_PER_BLOCK)
return -1;
uuid_t blocks[MAX_UUIDS_PER_BLOCK] = {{0}}; // the content of the indirect data block
unqlite_int64 blocks_size = sizeof(uuid_t) * MAX_UUIDS_PER_BLOCK;
if (uuid_compare(iterator->fcb->indirect_data_block, zero_uuid) == 0)
{
// if creating not allowed, then we already iterated to the end, so return NULL
if (iterator->create == 0)
return -1;
// otherwise, we generate a new indirect data block to hold many uuid_t
uuid_generate(iterator->fcb->indirect_data_block);
write_to_db(iterator->fcb->indirect_data_block, *blocks, blocks_size);
}
// read content of the indirect data block
read_from_db(iterator->fcb->indirect_data_block, *blocks, blocks_size);
if (iterator->create && uuid_compare(blocks[iterator->index], zero_uuid) == 0)
{ // if it is an unused block, then create one and generate a new uuid for it
make_empty_data_block(next_block_id, block_size);
uuid_copy(blocks[iterator->index], next_block_id);
write_to_db(iterator->fcb->indirect_data_block, *blocks, blocks_size);
}
else // if it already exists, then copy the existed uuid
uuid_copy(next_block_id, blocks[iterator->index]);
iterator->index++;
return 0;
}
/**
* Get next data block. Iterating over direct blocks first and then iterating over blocks in indirect level
* @param iterator - The iterator
* @param block - where to store the next data block
* @param block_size - the size of the data block
* @param block_id - the id of the data block
* @return data block on success, NULL on failure
*/
void *get_next_data_block(fcb_iterator *iterator, void *block, size_t block_size, uuid_t *block_id)
{
if (iterator->level > MAX_INDEX_LEVEL) // if out of range
return NULL;
uuid_t next_block_id = {0}; // a temporary variable to store
// try to get the id of the next data block
if (iterator->level == 0) // level is 0: we are iterating over direct blocks
{
if (get_next_direct_block(iterator, block_size, next_block_id) < 0)
return NULL;
}
else if (iterator->level == 1) // level is 1: we are iterating over blocks referred by indirect block
{
if (get_next_indirect_block(iterator, block_size, next_block_id) < 0)
return NULL;
}
else
return NULL;
// if no valid next block id is retrieved
if (uuid_compare(next_block_id, zero_uuid) == 0)
return NULL;
uuid_copy(*block_id, next_block_id);
// retrieve the next block to return
read_from_db(next_block_id, block, block_size);
return block;
}
/**
* Get the fcb from its parent dir
* @param parent_dir - the parent dir fcb
* @param name - the name of the dir or file we want to find
* @param cur_fcb - where to store the fetched fcb
* @param cur_fcb_id - where to store the id of fetched fcb. if NULL, then dont have to store
* @return 0 on success, < 0 on failure
*/
int get_fcb_from_dir(fcb *parent_dir, const char *name, uuid_t *cur_fcb_id, fcb *cur_fcb)
{
write_log("get fcb from dir \n");
if (!S_ISDIR(parent_dir->mode))
return -ENOTDIR;
fcb_iterator iterator = make_iterator(parent_dir, 0);
dir_data parent_dir_data;
uuid_t parent_dir_data_id;
// iterate over each data block
while (get_next_data_block(&iterator, &parent_dir_data, sizeof(dir_data), &parent_dir_data_id) != NULL)
{
// iterate over each directory entry in the current data block
for (int index = 0; index < MAX_DIRECTORY_ENTRIES_PER_BLOCK; index++)
{ // if the name is matched
if (strcmp(parent_dir_data.entries[index].name, name) == 0)
{ // retrieve the correct fcb
read_from_db(parent_dir_data.entries[index].fcb_id, cur_fcb, sizeof(fcb));
if (cur_fcb_id != NULL) // obtain the target fcb id
uuid_copy(*cur_fcb_id, parent_dir_data.entries[index].fcb_id);
return 0;
}
}
}
return -ENOENT; // no such directory or file
}
/**
* Get the file control block of the node found at the given path
* @param path - the path of the target fcb
* @param cur_fcb_id - where to store the id of fetched fcb
* @param cur_fcb - where to store the fetched fcb
* @param get_parent - 1 if we want to get the parent fcb, 0 for the base fcb
* @return 0 on success, < 0 on failure
*/
int get_fcb_by_path(const char *path, uuid_t *cur_fcb_id, fcb *cur_fcb, int get_parent)
{
write_log("get fcb by path \n");
char *path_copy = strdup(path);
char *path_remaining;
// if parent fcb wanted, only keep the dir name
if (get_parent)
path_copy = dirname(path_copy);
char *cur_name = strtok_r(path_copy, "/", &path_remaining);
// get the parent directory fcb from root
fcb cur_dir;
get_root_fcb(&cur_dir); // from root fcb to search
if (cur_fcb_id != NULL)
uuid_copy(*cur_fcb_id, ROOT_OBJECT_KEY);
while (cur_name != NULL)
{ // find the next level fcb in the current directory
int rc = get_fcb_from_dir(&cur_dir, cur_name, cur_fcb_id, &cur_dir);
if (rc != 0)
return rc;
cur_name = strtok_r(NULL, "/", &path_remaining); // keep extract the next name
}
memcpy(cur_fcb, &cur_dir, sizeof(fcb));
free(path_copy);
return 0;
}
/**
* Add a fcb to its parent dir
* @param parent_dir_fcb - the fcb of its parent dir
* @param name - the name of fcb to be added
* @param fcb_id - the id of fcb to be added
* @return 0 on success, < 0 on failure
*/
int add_fcb_to_dir(fcb *parent_dir_fcb, const char *name, const uuid_t fcb_id)
{
if (strlen(name) >= MAX_FILENAME_LENGTH)
return -ENAMETOOLONG;
if (!S_ISDIR(parent_dir_fcb->mode))
return -ENOTDIR;
fcb_iterator iterator = make_iterator(parent_dir_fcb, 1);
dir_data parent_dir_data;
uuid_t data_block_id;
while (get_next_data_block(&iterator, &parent_dir_data, sizeof(parent_dir_data), &data_block_id) != NULL)
{
// if all entries have been used in the current data block, continue to the next block
if (parent_dir_data.cur_len == MAX_DIRECTORY_ENTRIES_PER_BLOCK)
continue;
// iterate over each directory entry
for (int index = 0; index < MAX_DIRECTORY_ENTRIES_PER_BLOCK; index++)
{ // if the target name is matched
if (strcmp(parent_dir_data.entries[index].name, "") == 0)
{
// set up the current empty entry
uuid_copy(parent_dir_data.entries[index].fcb_id, fcb_id);
strcpy(parent_dir_data.entries[index].name, name);
// update the used count
parent_dir_data.cur_len += 1;
// update the parent dir data
write_to_db(data_block_id, &parent_dir_data, sizeof(dir_data));
return 0;
}
}
}
return -ENOSPC; // no extra space
}
/**
* Check if a dir is empty
* @param cur_dir - the dir to be checked
* @return 1 on empty, 0 on not empty
*/
int dir_is_empty(fcb *cur_dir)
{
if (!S_ISDIR(cur_dir->mode))
return -ENOTDIR;
fcb_iterator iterator = make_iterator(cur_dir, 0);
dir_data cur_dir_data;
uuid_t cur_dir_data_id;
while (get_next_data_block(&iterator, &cur_dir_data, sizeof(dir_data), &cur_dir_data_id) != NULL)
{
if (cur_dir_data.cur_len > 0) // indicates non-empty dir
return 0;
}
return 1;
}
/**
* Delete a file or dir from its parent dir
* @param parent_dir - the fcb of its parent dir
* @param name - the name of fcb to be deleted
* @param is_dir - 1 if the fcb to be deleted is a dir
* @param delete_data - 1 if unlink the normal file, 0 for unlinking a hard link
*/
int delete_file_or_dir_from_dir(fcb *parent_dir, const char *name, int is_dir, int delete_data)
{
if (!S_ISDIR(parent_dir->mode))
return -ENOTDIR;
fcb_iterator iterator = make_iterator(parent_dir, 0);
uuid_t parent_dir_data_id;
dir_data parent_dir_data;
while (get_next_data_block(&iterator, &parent_dir_data, sizeof(dir_data), &parent_dir_data_id) != NULL)
{
for (int index = 0; index < MAX_DIRECTORY_ENTRIES_PER_BLOCK; index++)
{ // if we find the matching filename
if (strcmp(parent_dir_data.entries[index].name, name) == 0)
{
fcb cur_fcb; // to store the fcb we want to delete
uuid_t fcb_id; // the id the the fcb we want to delete
uuid_copy(fcb_id, parent_dir_data.entries[index].fcb_id); // copy the matched entry's id here
read_from_db(fcb_id, &cur_fcb, sizeof(fcb)); // read fcb we want to delete from database
if (is_dir) // delete a dir
{
if (!S_ISDIR(cur_fcb.mode))
return -ENOTDIR;
int empty_dir = dir_is_empty(&cur_fcb);
if (empty_dir == 0)
return -ENOTEMPTY;
}
else // delete a file
{
if (!S_ISREG(cur_fcb.mode))
return -EISDIR;
}
if (delete_data) // need to delete all data blocks
{
// delete the data blocks of the cur_fcb
uuid_t data_block_id; // the id of the current data block
int size = (is_dir == 1 ? sizeof(dir_data) : sizeof(file_data));
char data_block[size]; // the content of the current data block
fcb_iterator iter = make_iterator(&cur_fcb, 0);
while (get_next_data_block(&iter, &data_block, size, &data_block_id))
delete_from_db(data_block_id); // delete them in database
delete_from_db(fcb_id); // delete fcb itself
}
else // delete a hard link
{
fcb true_fcb; // find the true fcb
read_from_db(fcb_id, &true_fcb, sizeof(fcb));
true_fcb.count -= 1; // decrement the reference count
write_to_db(fcb_id, &true_fcb, sizeof(fcb)); // update
}
// update parent dir
memset(&parent_dir_data.entries[index].fcb_id, 0, sizeof(uuid_t));
memset(&parent_dir_data.entries[index].name, 0, MAX_FILENAME_LENGTH);
parent_dir_data.cur_len -= 1;
// update parent dir in database
write_to_db(parent_dir_data_id, &parent_dir_data, sizeof(dir_data));
return 0;
}
}
}
return -ENOENT;
}
/**
* Reads data from a file data block
* @param dest - where to store the read data
* @param src - where the data come from
* @param offset - the position where to start read
* @param size - the number of bytes to be read
* @return the number of bytes on success
*/
int read_block(char *dest, file_data *src, off_t offset, size_t size)
{
int start = offset;
int end = min(start + size, BLOCK_SIZE);
int bytes_read = end - start;
memcpy(dest, &src->data[offset], bytes_read);
return bytes_read;
}
/**
* Write file data to a data block
* @param dest - the data block where the data written to
* @param src - the source data buffer
* @param offset - the position where to start writing
* @param size - the number of bytes to be written
* @return the number of bytes on success
*/
int write_block(file_data *dest, const char *src, off_t offset, size_t size)
{
int start = offset;
int end = min(start + size, BLOCK_SIZE);
int bytes_written = end - start;
memcpy(&dest->data[start], src, bytes_written);
dest->size = start + bytes_written;
return bytes_written;
}
/**
* Get file and directory attributes (meta-data)
* @param path - path of the file which is retrieve
* @param stbuf - stat buffer to be filled with meta-data of the file
* @return 0 for success, < 0 for failure
*/
static int myfs_getattr(const char *path, struct stat *stbuf)
{
write_log("myfs_getattr(path=\"%s\")\n", path);
fcb cur_dir;
int rc = get_fcb_by_path(path, NULL, &cur_dir, 0);
if (rc != 0)
return rc;
stbuf->st_mode = cur_dir.mode; /* File mode. */
stbuf->st_nlink = cur_dir.count; /* Link count. */
stbuf->st_uid = cur_dir.uid; /* User ID of the file's owner. */
stbuf->st_gid = cur_dir.gid; /* Group ID of the file's group. */
stbuf->st_size = cur_dir.size; /* Size of file, in bytes. */
stbuf->st_atime = cur_dir.atime; /* Time of last access. */
stbuf->st_mtime = cur_dir.mtime; /* Time of last modification. */
stbuf->st_ctime = cur_dir.ctime; /* Time of last status change. */
return 0;
}
/**
* Read a directory
* @param path - The path of the directory to be read
* @param buf - Buffer to be passed to the filler()
* @param filler - A function used to fill the buffer
* @param offset -
* @param fi - File handler
* @return 0 on success, < 0 on failure
*/
static int myfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi)
{
write_log("myfs_readdir(path=\"%s\")\n", path);
fcb cur_dir;
fcb_iterator iterator = make_iterator(&cur_dir, 0);
dir_data cur_dir_data;
uuid_t cur_dir_data_id;
int rc = get_fcb_by_path(path, NULL, &cur_dir, 0);
if (rc != 0)
return rc;
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
while (get_next_data_block(&iterator, &cur_dir_data, sizeof(dir_data), &cur_dir_data_id) != NULL)
{ // iterate over each directory entry
for (int index = 0; index < MAX_DIRECTORY_ENTRIES_PER_BLOCK; index++)
{ // if the entry is not empty, then readdir
if (strcmp(cur_dir_data.entries[index].name, "") != 0)
{
filler(buf, cur_dir_data.entries[index].name, NULL, 0);
}
}
}
return 0;
}
/**
* Read data from a file.
* @param path - the path of the file to be read
* @param buf - the buffer to store the read information
* @param size - the number of bytes to be read
* @param offset - the position where to start reading
* @param fi - file handler
* @return 0 on success, < 0 on failure
*/
static int myfs_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi)
{
write_log("myfs_read(path=\"%s\", size=%zu, offset=%zu", path, size, offset);
if (size <= 0)
return -1;
fcb cur_fcb;
int rc = get_fcb_by_path(path, NULL, &cur_fcb, 0);
if (rc != 0)
return rc;
if (!S_ISREG(cur_fcb.mode))
return -EISDIR; // if not a file
unsigned bytes_read = 0;
fcb_iterator iterator = make_iterator(&cur_fcb, 0);
file_data file_data_block;
uuid_t file_data_id;
size_t remaining_size = size;
while (get_next_data_block(&iterator, &file_data_block, sizeof(file_data), &file_data_id) != NULL)
{
if (remaining_size == 0) // no bytes to read, terminate
return bytes_read;
if (offset >= BLOCK_SIZE)
{
offset -= BLOCK_SIZE;
continue;
}
off_t cur_bytes_read = read_block(&buf[bytes_read], &file_data_block, offset, remaining_size);
offset = max(0, offset - cur_bytes_read);
remaining_size -= cur_bytes_read;
bytes_read += cur_bytes_read;
}
return bytes_read;
}
/**
* Create a new file
* @param path - the path where to create a file
* @param mode - the file mode
* @param fi - file handler
* @return 0 on success, < 0 on failure
*/
static int myfs_create(const char *path, mode_t mode, struct fuse_file_info *fi)
{
write_log("myfs_create(path=\"%s\", mode=0%03o, fi=0x%08x)\n", path, mode, fi);
char *path_copy = strdup(path);
char *filename = basename(path_copy);
fcb cur_dir; // directory where a new file will be added into
uuid_t cur_dir_id;
fcb new_file_fcb; // the new fcb for the new file
uuid_t new_file_fcb_id = {0};
if (strlen(filename) >= MAX_FILENAME_LENGTH)
return -ENAMETOOLONG;
int rc = get_fcb_by_path(path, &cur_dir_id, &cur_dir, 1);
if (rc != 0)
return rc;
// make a new file fcb
make_new_fcb(&new_file_fcb, mode, 0);
// generate a new fcb id for the new fcb
uuid_generate(new_file_fcb_id);
// update parent directory entries
rc = add_fcb_to_dir(&cur_dir, filename, new_file_fcb_id);
// store the new fcb into database
write_to_db(new_file_fcb_id, &new_file_fcb, sizeof(fcb));
// update its parent directory in database
write_to_db(cur_dir_id, &cur_dir, sizeof(fcb));
free(path_copy);
return rc;
}
/**
* Update the modtime for a file.
* @param path - the path of the file which needs to be modified
* @param ubuf - the target timestamps
* @return 0 on success, < 0 on failure
*/
static int myfs_utime(const char *path, struct utimbuf *ubuf)
{
write_log("myfs_utime(path=\"%s\")\n", path);
uuid_t fcb_id;
fcb cur_fcb;
// read fcb information from path
int rc = get_fcb_by_path(path, &fcb_id, &cur_fcb, 0);
if (rc != 0)
return rc;
cur_fcb.mtime = ubuf->modtime;
write_to_db(fcb_id, &cur_fcb, sizeof(fcb));
return 0;
}
/**
* Write data to a file
* @param path - the path of file which to be written
* @param buf - the data to be written
* @param offset - the position where to start writing
* @param size - the number of bytes to be written
* @return 0 on success, < 0 on failure
*/
static int myfs_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi)
{
if (size <= 0)
return -1;
if (size > MAX_FILE_SIZE)
return -EFBIG;
write_log("myfs_write(path=\"%s\", size=%d, offset=%lld,)\n", path, size, offset);
fcb cur_fcb; // the current fcb where to write
uuid_t cur_fcb_id;
int rc = get_fcb_by_path(path, &cur_fcb_id, &cur_fcb, 0);
if (rc != 0)
return rc;
if (!S_ISREG(cur_fcb.mode))
return -EISDIR;
int bytes_written = 0;
off_t original_offset = offset; // keey a copy for offset
uuid_t cur_data_block_id; // the current data block id
file_data cur_data_block;
fcb_iterator iterator = make_iterator(&cur_fcb, 1);
while (get_next_data_block(&iterator, &cur_data_block, sizeof(file_data), &cur_data_block_id) != NULL)
{
if (size == 0)
break;
if (offset >= BLOCK_SIZE)
{
offset -= BLOCK_SIZE;
continue;
}
int cur_bytes_written = write_block(&cur_data_block, &buf[bytes_written], offset, size);
offset = max(0, offset - cur_bytes_written);
size -= cur_bytes_written;
bytes_written += cur_bytes_written;
write_to_db(cur_data_block_id, &cur_data_block, sizeof(file_data));
}
cur_fcb.size = max(cur_fcb.size, original_offset + bytes_written); // update size
write_to_db(cur_fcb_id, &cur_fcb, sizeof(fcb)); // update in database
return bytes_written;
}
/**
* Delete superfluous direct data blocks
* @param cur_fcb - the fcb of the current file truncated
* @param start - the starting index of the data block to be deleted
*/
void truncate_direct_level(fcb *cur_fcb, int start)
{
for (int index = start; index < NUMBER_DIRECT_BLOCKS; index++)
{
if (uuid_compare(cur_fcb->data_blocks[index], zero_uuid) != 0)
{ // if the data block is not empty, clear it
delete_from_db(cur_fcb->data_blocks[index]);
memset(&cur_fcb->data_blocks[index], 0, KEY_SIZE);
}
}
}
/**
* Delete superfluous data blocks referred by the indirect data block
* @param cur_fcb - the fcb of the current file truncated
* @param start - the starting index of the data block to be deleted
*/
void truncate_indirect_level(fcb *cur_fcb, int start)
{
if (uuid_compare(cur_fcb->indirect_data_block, zero_uuid) != 0)
{
uuid_t blocks[MAX_UUIDS_PER_BLOCK];
read_from_db(cur_fcb->indirect_data_block, blocks, sizeof(uuid_t) * MAX_UUIDS_PER_BLOCK);
for (int index = start; index < MAX_UUIDS_PER_BLOCK; index++)
{
if (uuid_compare(blocks[index], zero_uuid) != 0)
{
delete_from_db(blocks[index]);
memset(&blocks[index], 0, KEY_SIZE);
}
}
// clear indirect block itself
delete_from_db(cur_fcb->indirect_data_block);
memset(cur_fcb->indirect_data_block, 0, KEY_SIZE);
}
}
/**
* Set the size of a file. New blocks can be added or used blocks can be deleted
* @param path - the path of the file to be truncated
* @param new_size - the new size of the file
* @return 0 on success, < 0 on failure
*/
static int myfs_truncate(const char *path, off_t new_size)
{
write_log("myfs_truncate(path=\"%s\", new_size=%lld)\n", path, new_size);
if (new_size > MAX_FILE_SIZE)
return -EFBIG;
off_t remaining_size = new_size;
uuid_t cur_fcb_id;
fcb cur_fcb;
int rc = get_fcb_by_path(path, &cur_fcb_id, &cur_fcb, 0);
if (rc != 0)
return rc;
// if we will get a larger file
if (remaining_size > cur_fcb.size)
{
char buffer[remaining_size - cur_fcb.size];
memset(buffer, 0, remaining_size - cur_fcb.size);
myfs_write(path, buffer, remaining_size - cur_fcb.size, cur_fcb.size, NULL);
}
else // if we will get a smaller file
{
fcb_iterator iterator = make_iterator(&cur_fcb, 0);
remaining_size = new_size % BLOCK_SIZE;
// from this block we are gonna edit, after this block everything will be deleted
int block_offset = (int)new_size / BLOCK_SIZE;
file_data cur_data_block;
uuid_t cur_data_block_id;
int index = 0; // the current data block index
while (get_next_data_block(&iterator, &cur_data_block, sizeof(file_data), &cur_data_block_id) != NULL)
{
if (index == block_offset)
{
memset(&cur_data_block.data[remaining_size], 0, BLOCK_SIZE - remaining_size);
cur_data_block.size = remaining_size;
write_to_db(cur_data_block_id, &cur_data_block, sizeof(file_data));
}
else if (index == block_offset + 1) // superfluous data blocks start appearing
{
if (iterator.level == 0)
truncate_direct_level(&cur_fcb, index); // delete following direct blocks
// deleting following data blocks referred by indirect block
truncate_indirect_level(&cur_fcb, index - NUMBER_DIRECT_BLOCKS);
}
else if (index > block_offset + 1)
break;
index++;
}
}
// update the cur fcb in database
cur_fcb.size = new_size;
write_to_db(cur_fcb_id, &cur_fcb, sizeof(fcb));
return 0;
}
/**
* Set the permissions of a file
* @param path - the path of the file whose permissions to be changed
* @param mode - the new permisison
* @return 0 on success, < 0 on failure
*/
static int myfs_chmod(const char *path, mode_t mode)
{
write_log("myfs_chmod(path=\"%s\", mode=0%03o)\n", path, mode);
fcb cur_fcb;
uuid_t cur_fcb_id;
int rc = get_fcb_by_path(path, &cur_fcb_id, &cur_fcb, 0);
if (rc != 0)
return rc;
// update mode
cur_fcb.mode |= mode;
// update the fcb in the database
write_to_db(cur_fcb_id, &cur_fcb, sizeof(fcb));
return 0;
}
/**
* Change ownerships of a given file
* @param path - the path of the file whose owners to be changed
* @param uid - the new owning user
* @param gid - the new owning group
* @return 0 on success, < 0 on failure
*/
static int myfs_chown(const char *path, uid_t uid, gid_t gid)
{
write_log("myfs_chown(path=\"%s\", uid=%d, gid=%d)\n", path, uid, gid);
fcb cur_fcb;
uuid_t cur_fcb_id;
int rc = get_fcb_by_path(path, &cur_fcb_id, &cur_fcb, 0);
if (rc != 0)
return rc;
// update uid and gid
cur_fcb.uid = uid;
cur_fcb.gid = gid;
// update the fcb in the database
write_to_db(cur_fcb_id, &cur_fcb, sizeof(fcb));
return 0;
}
/**
* Create a new directory
* @param path - the path at where to create the new directory
* @param mode - the permissions of the new directory
* @return 0 on success, < 0 on failure
*/
static int myfs_mkdir(const char *path, mode_t mode)
{
write_log("myfs_mkdir(path=\"%s\", mode=0%03o)\n", path, mode);
char *path_copy = strdup(path);
char *filename = basename(path_copy);
if (strlen(filename) >= MAX_FILENAME_LENGTH)
return -ENAMETOOLONG;
// get fcb of parent directory
fcb parent_dir;
uuid_t parent_dir_id;
int rc = get_fcb_by_path(path, &parent_dir_id, &parent_dir, 1);
if (rc != 0)
return rc;
// set up a new directory fcb
fcb new_dir;
uuid_t new_dir_id = {0};
make_new_fcb(&new_dir, mode, 1);
uuid_generate(new_dir_id);
// write the new dir to the database
write_to_db(new_dir_id, &new_dir, sizeof(fcb));
// write the new dir entry to the parent dic
rc = add_fcb_to_dir(&parent_dir, filename, new_dir_id);
// update the parent dir in the database
write_to_db(parent_dir_id, &parent_dir, sizeof(fcb));
return rc;
}
/**
* Delete a file
* @param path - the path of the file to be deleted
* @return 0 on success, < 0 on failure
*/
static int myfs_unlink(const char *path)
{
write_log("myfs_unlink(path=\"%s\")\n", path);
char *path_copy = strdup(path);
char *filename = basename(path_copy);
fcb parent_dir;
fcb cur_fcb;
uuid_t cur_fcb_id;
int rc = get_fcb_by_path(path, NULL, &parent_dir, 1);
if (rc != 0)
return rc;
rc = get_fcb_from_dir(&parent_dir, filename, &cur_fcb_id, &cur_fcb);
if (rc != 0)
return rc;
if ((unsigned)cur_fcb.count == (unsigned)1) // we need to delete the file
rc = delete_file_or_dir_from_dir(&parent_dir, filename, 0, 1);
else // we need to delete the hard link
rc = delete_file_or_dir_from_dir(&parent_dir, filename, 0, 0);
return rc;
}
/**
* Delete the directory
* @param path - the path of the directory to be deleted
* @return 0 on success, < 0 on failure
*/
static int myfs_rmdir(const char *path)
{
write_log("myfs_rmdir(path=\"%s\")\n", path);
fcb parent_dir;
char *path_copy = strdup(path);
char *filename = basename(path_copy);
// get fcb of parent directory
int rc = get_fcb_by_path(path, NULL, &parent_dir, 1);
if (rc != 0)
return rc;
rc = delete_file_or_dir_from_dir(&parent_dir, filename, 1, 1);
free(path_copy);
return rc;
}
/**
* Create a new hard link
* @param from - the path of the src file
* @param to - the path of the new file to be linked
* @return 0 on success, < 0 on failure
*/
static int myfs_link(const char *from, const char *to)
{
char *to_copy = strdup(to);
char *from_copy = strdup(from);
char *dest_filename = basename(to_copy);
char *src_filename = basename(from_copy);
uuid_t dest_parent_fcb_id;
fcb dest_parent_fcb;
uuid_t dest_fcb_id;
fcb dest_fcb;
// get dest parent dir fcb
int rc = get_fcb_by_path(to, &dest_parent_fcb_id, &dest_parent_fcb, 1);
if (rc != 0)
return rc;
// get dest fcb itself
rc = get_fcb_from_dir(&dest_parent_fcb, dest_filename, &dest_fcb_id, &dest_fcb);
if (rc == 0)
return -EEXIST;
if (S_ISDIR(dest_fcb.mode)) // cannot be a directory
return -EISDIR;