-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnamei.c
1795 lines (1679 loc) · 60.9 KB
/
namei.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
#include <asm/uaccess.h>
#include <linux/blkdev.h>
#include <linux/buffer_head.h>
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/mpage.h>
#include <linux/proc_fs.h>
#include <linux/slab.h>
#include <linux/statfs.h>
#include <linux/types.h>
#include <linux/version.h>
#include "logging.h"
#include "vvsfs.h"
struct inode *vvsfs_iget(struct super_block *sb, unsigned long ino);
/* Find a dentry within a given block by name
* (returned through parameter)
*
* @bh: Data block buffer
* @dentry_count: Number of dentries in this block
* @i: Data block index
* @target_name: Name of the dentry to be found
* @target_name_len: Length of target name
* @flags: Behaviour flags for bufloc_t construction
* @out_loc: Returned data for any potentially found
* matching dentry
*
* @return: (int) 0 if found, 1 otherwise
*/
static int vvsfs_find_entry_in_block(struct buffer_head *bh,
int dentry_count,
int i,
const char *target_name,
int target_name_len,
int flags,
struct bufloc_t *out_loc) {
struct vvsfs_dir_entry *dentry;
char *name;
uint32_t inumber;
int d;
for (d = 0; d < dentry_count; d++) {
// Access the current dentry
dentry = READ_DENTRY(bh, d);
name = dentry->name;
inumber = dentry->inode_number;
// Skip if reserved or name does not match
DEBUG_LOG("vvsfs - find_entry_in_block - "
"comparing %s (%zu) == %s (%d)\n",
name,
strlen(name),
target_name,
target_name_len);
if (!inumber || !namecmp(name, target_name, target_name_len) != 0) {
continue;
}
out_loc->b_index = i;
out_loc->d_index = d;
out_loc->flags = flags;
if (bl_flag_set(flags, BL_PERSIST_BUFFER)) {
out_loc->bh = bh;
out_loc->dentry =
bl_flag_set(flags, BL_PERSIST_DENTRY) ? dentry : NULL;
} else {
out_loc->bh = NULL;
out_loc->dentry = NULL;
brelse(bh);
}
DEBUG_LOG("vvsfs - find_entry_in_block - "
"done (found)\n");
return 0;
}
brelse(bh);
DEBUG_LOG("vvsfs - find_entry_in_block - done "
"(not found)\n");
return 1;
}
/* Find a given entry within the given directory inode
* direct blocks
*
* @vi: Inode info of directory to search
* @sb: Superblock of filesystem
* @dentry: Target dentry (name, length, etc)
* @flags: Behavioural flags for bufloc_t data
* @out_loc: Returned data for location of entry if
* found
*
* @return: (int): 0 if found, 1 if not found,
* otherwise an error
*/
static int vvsfs_find_entry_direct(struct vvsfs_inode_info *vi,
struct super_block *sb,
struct dentry *dentry,
struct bufloc_t *out_loc,
unsigned flags,
int last_block_dentry_count) {
struct buffer_head *bh;
const char *target_name;
int i;
int direct_blocks;
int current_block_dentry_count;
int target_name_len;
DEBUG_LOG("vvsfs - find_entry - direct blocks\n");
target_name = dentry->d_name.name;
target_name_len = dentry->d_name.len;
direct_blocks =
min(vi->i_db_count, (uint32_t)VVSFS_LAST_DIRECT_BLOCK_INDEX);
for (i = 0; i < direct_blocks; i++) {
LOG("vvsfs - find_entry - reading dno: "
"%d, disk block: %d",
vi->i_data[i],
vvsfs_get_data_block(vi->i_data[i]));
bh = READ_BLOCK(sb, vi, i);
if (!bh) {
// Buffer read failed, no more data when
// we expected some
return -EIO;
}
current_block_dentry_count = i == vi->i_db_count - 1
? last_block_dentry_count
: VVSFS_N_DENTRY_PER_BLOCK;
if (!vvsfs_find_entry_in_block(bh,
current_block_dentry_count,
i,
target_name,
target_name_len,
flags,
out_loc)) {
DEBUG_LOG("vvsfs - find_entry - direct done (found)");
return 0;
}
// buffer_head release is handled within block
// search
}
return 1;
}
/* Find a given entry within the given directory inode
* indirect blocks
*
* @vi: Inode info of directory to search
* @sb: Superblock of filesystem
* @dentry: Target dentry (name, length, etc)
* @flags: Behavioural flags for bufloc_t data
* @out_loc: Returned data for location of entry if
* found
*
* @return: (int): 0 if found, 1 if not found,
* otherwise an error
*/
static int vvsfs_find_entry_indirect(struct vvsfs_inode_info *vi,
struct super_block *sb,
struct dentry *dentry,
struct bufloc_t *out_loc,
unsigned flags,
int last_block_dentry_count) {
struct buffer_head *i_bh;
struct buffer_head *bh;
const char *target_name;
int indirect_blocks;
int current_block_dentry_count;
int i;
int target_name_len;
DEBUG_LOG("vvsfs - find_entry - indirect blocks\n");
target_name = dentry->d_name.name;
target_name_len = dentry->d_name.len;
i_bh = READ_BLOCK(sb, vi, VVSFS_LAST_DIRECT_BLOCK_INDEX);
if (!i_bh) {
return -EIO;
}
indirect_blocks = vi->i_db_count - VVSFS_LAST_DIRECT_BLOCK_INDEX;
for (i = 0; i < indirect_blocks; i++) {
bh = READ_INDIRECT_BLOCK(sb, i_bh, i);
if (!bh) {
brelse(i_bh);
return -EIO;
}
current_block_dentry_count = i == vi->i_db_count - 1
? last_block_dentry_count
: VVSFS_N_DENTRY_PER_BLOCK;
if (!vvsfs_find_entry_in_block(bh,
current_block_dentry_count,
i,
target_name,
target_name_len,
flags,
out_loc)) {
DEBUG_LOG("vvsfs - find_entry - indirect done (found)");
out_loc->b_index += VVSFS_LAST_DIRECT_BLOCK_INDEX;
brelse(i_bh);
return 0;
}
// buffer_head release is handled within block
// search
}
brelse(i_bh);
return 1;
}
/* Find a given entry within the given directory inode
*
* @dir: Inode representation of directory to search
* @dentry: Target dentry (name, length, etc)
* @flags: Behavioural flags for bufloc_t data
* @out_loc: Returned data for location of entry if
* found
*
* @return: (int): 0 if found, 1 if not found,
* otherwise and error
*/
int vvsfs_find_entry(struct inode *dir,
struct dentry *dentry,
unsigned flags,
struct bufloc_t *out_loc) {
struct vvsfs_inode_info *vi;
struct super_block *sb;
int result;
int last_block_dentry_count;
DEBUG_LOG("vvsfs - find_entry\n");
// Retrieve vvsfs specific inode data from dir
// inode
vi = VVSFS_I(dir);
sb = dir->i_sb;
LAST_BLOCK_DENTRY_COUNT(dir, last_block_dentry_count);
DEBUG_LOG("vvsfs - find_entry - number of blocks "
"to read %d\n",
vi->i_db_count);
// Progressively load datablocks into memory and
// check dentries
result = vvsfs_find_entry_direct(
vi, sb, dentry, out_loc, flags, last_block_dentry_count);
if (result <= 0) {
return result;
}
result = vvsfs_find_entry_indirect(
vi, sb, dentry, out_loc, flags, last_block_dentry_count);
if (result <= 0) {
return result;
}
DEBUG_LOG("vvsfs - find_entry - done (not found)");
return 1;
}
/* Shift back blocks and fill holes for direct blocks only.
* Assumed that the target block is within the direct blocks.
*
* @vi: Inode information for inode of target blocks and dentries
* @i_sb: VVSFS specific superblock information
* @bh: Buffer for the indirect block in the last entry of direct
* blocks in this inode
* @block_indexx: Target freed data block index
*/
static void vvsfs_shift_direct_only(struct vvsfs_inode_info *vi,
uint32_t block_index) {
size_t count;
DEBUG_LOG(
"vvsfs - shift_blocks_back - only direct, index: %u, blocks: %u\n",
block_index,
vi->i_db_count);
count = (--vi->i_db_count) - block_index;
DEBUG_LOG("vvsfs - shift_blocks_back - count: %zu\n", count);
DEBUG_LOG("vvsfs - shift_blocks_back - "
"i_db_count after: %d\n",
vi->i_db_count);
if (count > 0) {
// Implementation specific behaviour for memmove len of 0
// just don't do it and avoid poptential issues
memmove(&vi->i_data[block_index],
&vi->i_data[block_index + 1],
count * VVSFS_INDIRECT_PTR_SIZE);
}
// Ensure the last block is not set (avoids
// duplication of last element from shift back)
vi->i_data[vi->i_db_count] = 0;
}
/* Shift back blocks and fill holes for indirect blocks only.
* Assumed that the target block is within the indirect blocks.
*
* @vi: Inode information for inode of target blocks and dentries
* @i_sb: VVSFS specific superblock information
* @bh: Buffer for the indirect block in the last entry of direct
* blocks in this inode
* @block_indexx: Target freed data block index
*/
static void vvsfs_shift_indirect_only(struct vvsfs_inode_info *vi,
struct vvsfs_sb_info *i_sb,
struct buffer_head *bh,
uint32_t block_index) {
size_t count;
DEBUG_LOG("vvsfs - shift_blocks_back - only indirect, index: %u, "
"blocks: %u\n",
block_index,
vi->i_db_count);
count = (--vi->i_db_count) - block_index;
DEBUG_LOG("vvsfs - shift_blocks_back - "
"i_db_count after: %d\n",
vi->i_db_count);
if (count > 0) {
memmove(bh->b_data + (block_index * VVSFS_INDIRECT_PTR_SIZE),
bh->b_data + ((block_index + 1) * VVSFS_INDIRECT_PTR_SIZE),
count * VVSFS_INDIRECT_PTR_SIZE);
}
if (vi->i_db_count == VVSFS_N_BLOCKS) {
DEBUG_LOG("vvsfs - shift_blocks_back - was last indrect, freeing "
"indirect block\n");
vvsfs_free_data_block(i_sb->dmap,
vi->i_data[VVSFS_LAST_DIRECT_BLOCK_INDEX]);
} else {
DEBUG_LOG("vvsfs - shift_blocks_back - was not last indirect, "
"setting last index %d to zero\n",
vi->i_db_count - VVSFS_LAST_DIRECT_BLOCK_INDEX);
// Ensure the last block is not set (avoids
// duplication of last element from shift back)
write_int_to_buffer(
bh->b_data + ((vi->i_db_count - VVSFS_LAST_DIRECT_BLOCK_INDEX) *
VVSFS_INDIRECT_PTR_SIZE),
0);
}
mark_buffer_dirty(bh);
brelse(bh);
}
/* Shifts data blocks around to ensure that there are not holes,
* this method assumes that a data block has been freed, which
* is the target to fill. Full generality across direct and
* indirect blocks is supported (all support conditional
* deallocation of the last block if newly emptied to fill
* earlierblock dentry hole):
* - Direct only movement (no indirect blocks in use)
* - Indirect block dentry moved to direct block hole
* - Indirect block dentry moved to other indirect block hole
*
* Note that this method will mark the buffer dirt and release
* it, persiting any changes made.
*
* @vi: Inode information for inode of target blocks and dentries
* @sb: Superblock of the filesystem
* @i_sb: VVSFS specific superblock information
* @block_index: Target freed data block index
*
* @return: (int) 0 if successful, error otherwise
*/
static int vvsfs_shift_blocks_back(struct vvsfs_inode_info *vi,
struct super_block *sb,
struct vvsfs_sb_info *i_sb,
uint32_t block_index) {
struct buffer_head *bh;
uint32_t replacement;
size_t count;
if (vi->i_db_count < VVSFS_N_BLOCKS) {
// Only direct blocks
vvsfs_shift_direct_only(vi, block_index);
return 0;
}
bh = READ_BLOCK(sb, vi, VVSFS_LAST_DIRECT_BLOCK_INDEX);
if (!bh) {
return -EIO;
}
if (block_index >= VVSFS_N_BLOCKS) {
// Only indirect blocks
vvsfs_shift_indirect_only(vi, i_sb, bh, block_index);
return 0;
}
replacement = read_int_from_buffer(bh->b_data);
vi->i_db_count--;
count = VVSFS_N_BLOCKS - block_index;
if (block_index < VVSFS_LAST_DIRECT_BLOCK_INDEX) {
// Move indirect block to direct
DEBUG_LOG("vvsfs - shift_blocks_back - has indirect, is direct, index: "
"%u, blocks: %u\n",
block_index,
vi->i_db_count + 1);
memmove(&vi->i_data[block_index],
&vi->i_data[block_index + 1],
count * sizeof(uint32_t));
DEBUG_LOG("vvsfs - shift_blocks_back - was not lat indect, setting "
"last index %d to zero\n",
vi->i_db_count - VVSFS_N_BLOCKS);
// Overwrite the old last block pointer with zeros
write_int_to_buffer(
bh->b_data + ((vi->i_db_count - VVSFS_LAST_DIRECT_BLOCK_INDEX) *
VVSFS_INDIRECT_PTR_SIZE),
0);
// Actually assign the indirect block to the end of the direct blocks
// this also avoids needing to write zeros to the end
vi->i_data[VVSFS_LAST_DIRECT_BLOCK_INDEX - 2] = replacement;
DEBUG_LOG("vvsfs - shift_blocks_back - writing first entry %u from "
"indirect blocks to last direct block\n",
replacement);
}
if (vi->i_db_count == VVSFS_LAST_DIRECT_BLOCK_INDEX) {
// Moved indirect block was last, since it is now a direct block
// we can free the indirect block in the last index of vi->i_data
DEBUG_LOG("vvsfs - shift_blocks_back - shifted last indirect block, "
"freeing indirect block\n");
brelse(bh);
vvsfs_free_data_block(i_sb->dmap,
vi->i_data[VVSFS_LAST_DIRECT_BLOCK_INDEX]);
vi->i_data[VVSFS_LAST_DIRECT_BLOCK_INDEX] = 0;
DEBUG_LOG("vvsfs - shift_blocks_back - zeroed last direct block "
"(indirect pointer)\n");
return 0;
}
count = block_index - VVSFS_LAST_DIRECT_BLOCK_INDEX;
if (count > 0) {
// Shift all indirect blocks after hole down
memmove(bh->b_data,
bh->b_data + VVSFS_INDIRECT_PTR_SIZE,
count * VVSFS_INDIRECT_PTR_SIZE);
}
DEBUG_LOG("vvsfs - shift_blocks_back - was not last indirect, setting last "
"index %d to zero\n",
vi->i_db_count - VVSFS_N_BLOCKS);
// Overwrite the old last block pointer with zeros
write_int_to_buffer(bh->b_data +
((vi->i_db_count - VVSFS_LAST_DIRECT_BLOCK_INDEX) *
VVSFS_INDIRECT_PTR_SIZE),
0);
mark_buffer_dirty(bh);
brelse(bh);
return 0;
}
/* Deallocate a data block from the given inode
*
* @inode: Target inode to deallocate data block from
* @block_index: index into inode data blocks, can be
* direct or indirect
*
* @return: (int) 0 if successful, error otherwise
*/
static int vvsfs_dealloc_data_block(struct inode *inode, int block_index) {
struct vvsfs_inode_info *vi;
struct super_block *sb;
struct vvsfs_sb_info *sb_info;
int err;
int raw_db_index;
uint32_t db_index;
DEBUG_LOG("vvsfs - dealloc_data_block\n");
if (block_index < 0 || block_index >= VVSFS_MAX_INODE_BLOCKS) {
DEBUG_LOG("vvsfs - dealloc_data_block - "
"block_index (%d) out of range "
"%d-%d\n",
block_index,
0,
(int)VVSFS_MAX_INODE_BLOCKS - 1);
return -EINVAL;
}
vi = VVSFS_I(inode);
sb = inode->i_sb;
sb_info = sb->s_fs_info;
DEBUG_LOG("vvsfs - dealloc_data_block - target block: %d\n", block_index);
raw_db_index = vvsfs_index_data_block(vi, sb, (uint32_t)block_index);
if (raw_db_index < 0) {
DEBUG_LOG("vvsfs - dealloc_data_block - indexing block %d failed\n",
block_index);
return raw_db_index;
}
db_index = (uint32_t)raw_db_index;
DEBUG_LOG("vvsfs - dealloc_data_block - removing "
"block %d @ %u\n",
block_index,
db_index);
vvsfs_free_data_block(sb_info->dmap, db_index);
// Move all subsequent blocks back to fill the
// holes
if ((err = vvsfs_shift_blocks_back(vi, sb, sb_info, block_index))) {
DEBUG_LOG("vvsfs - dealloc_data_block - unable to shift blocks, block "
"has been freed\n");
return err;
}
mark_inode_dirty(inode);
DEBUG_LOG("vvsfs - dealloc_data_block - done\n");
return 0;
}
/* Remove the dentry specified via bufloc from the
* last data block
*
* @dir: Target directory inode
* @bufloc: Specification of dentry location in data
* block (assumes already resolved)
*
* @return: (int) 0 if successful, error otherwise
*/
static int vvsfs_delete_entry_last_block(struct inode *dir,
struct bufloc_t *bufloc) {
struct vvsfs_dir_entry *last_dentry;
int last_block_dentry_count;
int err;
DEBUG_LOG("vvsfs - delete_entry_last_block\n");
LAST_BLOCK_DENTRY_COUNT(dir, last_block_dentry_count);
if (bufloc->d_index == last_block_dentry_count - 1) {
// Last dentry in block remove cleanly
DEBUG_LOG("vvsfs - delete_entry_bufloc - "
"last block, last dentry "
"in block, zero the entry\n");
memset(bufloc->dentry, 0, VVSFS_DENTRYSIZE);
DEBUG_LOG("vvsfs - delete_entry_bufloc - last count: %d, b_index: %u\n",
last_block_dentry_count,
bufloc->b_index);
if (last_block_dentry_count == 1 &&
(err = vvsfs_dealloc_data_block(dir, bufloc->b_index))) {
return err;
}
} else {
// Move last dentry in block to hole
DEBUG_LOG("vvsfs - delete_entry_bufloc - "
"last block, not last "
"dentry in block, move last entry "
"to hole\n");
last_dentry = READ_DENTRY(bufloc->bh, last_block_dentry_count - 1);
memcpy(bufloc->dentry, last_dentry, VVSFS_DENTRYSIZE);
// Delete the last dentry (as it has been
// moved)
memset(last_dentry, 0, VVSFS_DENTRYSIZE);
}
DEBUG_LOG("vvsfs - delete_entry_last_block - done\n");
return 0;
}
/* Remove the dentry specified via bufloc from the
* current data block
*
* @dir: Target directory inode
* @bufloc: Specification of dentry location in data
* block (assumed already resolved)
*
* @return: (int) 0 if successful, error otherwise
*/
static int vvsfs_delete_entry_block(struct inode *dir,
struct vvsfs_inode_info *vi,
struct bufloc_t *bufloc) {
struct vvsfs_dir_entry *last_dentry;
struct buffer_head *i_bh;
struct buffer_head *bh;
struct super_block *sb;
int last_block_dentry_count;
int err;
uint32_t index;
DEBUG_LOG("vvsfs - delete_entry_block\n");
LAST_BLOCK_DENTRY_COUNT(dir, last_block_dentry_count);
DEBUG_LOG("vvsfs - delete_entry_block - last block dentry count: %d\n",
last_block_dentry_count);
sb = dir->i_sb;
// Fill the hole with the last dentry in the last
// block
DEBUG_LOG("vvsfs - delete_entry_bufloc - not "
"last block, fill hole "
"from last block\n");
if (vi->i_db_count < VVSFS_N_BLOCKS) {
DEBUG_LOG("vvsfs - get_remove_last_dentry - direct block: %u\n",
vi->i_db_count - 1);
bh = READ_BLOCK(sb, vi, vi->i_db_count - 1);
if (!bh) {
DEBUG_LOG("vvsfs - get_remove_last_dentry - failed to read direct "
"block\n");
return -EIO;
}
} else {
i_bh = READ_BLOCK(sb, vi, VVSFS_LAST_DIRECT_BLOCK_INDEX);
if (!i_bh) {
DEBUG_LOG("vvsfs - get_remove_last_dentry - failed to read "
"indirect block\n");
return -EIO;
}
DEBUG_LOG("vvsfs - get_remove_last_dentry - indirect block: %u\n",
vi->i_db_count - VVSFS_N_BLOCKS);
index = read_int_from_buffer(
i_bh->b_data +
((vi->i_db_count - VVSFS_N_BLOCKS) * VVSFS_INDIRECT_PTR_SIZE));
bh = READ_BLOCK_OFF(sb, index);
if (!bh) {
brelse(i_bh);
return -EIO;
}
brelse(i_bh);
}
last_dentry = READ_DENTRY(bh, last_block_dentry_count - 1);
memcpy(bufloc->dentry, last_dentry, VVSFS_DENTRYSIZE);
// Delete the last dentry (as it has been moved)
memset(last_dentry, 0, VVSFS_DENTRYSIZE);
// Deallocate last block since we move the only dentry in it
if (last_block_dentry_count == 1 &&
(err = vvsfs_dealloc_data_block(dir, vi->i_db_count - 1))) {
return err;
}
mark_buffer_dirty(bh);
brelse(bh);
DEBUG_LOG("vvsfs - delete_entry_block - done \n");
return 0;
}
/* Delete and entry in a directory based on data
* obtained through invocation of
* `vvsfs_find_entry(...)`
*
* @dir: Inode representation of directory to search
* @loc: Location of entry in data blocks
*
* @return: (int) 0 if successful, error otherwise
*/
static int vvsfs_delete_entry_bufloc(struct inode *dir,
struct bufloc_t *bufloc) {
struct vvsfs_inode_info *vi;
int err;
DEBUG_LOG("vvsfs - delete_entry_bufloc\n");
vi = VVSFS_I(dir);
// Resolve the buffer head and dentry if not
// already done (indiciated by flags)
if ((err = vvsfs_resolve_bufloc(dir, vi, bufloc))) {
DEBUG_LOG("vvsfs - delete_entry_bufloc - "
"failed to resolve bufloc\n");
return err;
}
DEBUG_LOG("vvsfs - delete_dentry_bufloc - block index: %u block count "
"(index): %u\n",
bufloc->b_index,
vi->i_db_count - 1);
// Determine if we are in the last block
if (bufloc->b_index == vi->i_db_count - 1) {
if ((err = vvsfs_delete_entry_last_block(dir, bufloc))) {
DEBUG_LOG("vvsfs - delete_entry_bufloc - "
"failed to delete entry in "
"last block\n");
return err;
}
} else {
if ((err = vvsfs_delete_entry_block(dir, vi, bufloc))) {
DEBUG_LOG("vvsfs - delete_entry_bufloc - "
"failed to delete entry in "
"block\n");
return err;
}
}
// Updated parent inode size and times
dir->i_size -= VVSFS_DENTRYSIZE;
dir->i_ctime = dir->i_mtime = current_time(dir);
mark_buffer_dirty(bufloc->bh);
brelse(bufloc->bh);
mark_inode_dirty(dir);
DEBUG_LOG("vvsfs - delete_entry_bufloc - done\n");
return err;
}
/* Free all data blocks in a given inode
*
* @inode: Target inode to free all indirect and direct
* data blocks
*
* @return: (int) 0 if successful, error otherwise
*/
int vvsfs_free_inode_blocks(struct inode *inode) {
struct super_block *sb;
struct vvsfs_sb_info *i_sb;
struct vvsfs_inode_info *vi;
struct buffer_head *bh;
int i;
int indirect;
int direct;
uint32_t index;
DEBUG_LOG("vvsfs - free inode blocks - %lu", inode->i_ino);
if (inode->i_nlink != 0) {
DEBUG_LOG(
"vvsfs - free inode blocks called on allocated inode (links %u)",
inode->i_nlink);
return -EIO;
}
vi = VVSFS_I(inode);
sb = inode->i_sb;
i_sb = sb->s_fs_info;
direct = min((int)VVSFS_LAST_DIRECT_BLOCK_INDEX, (int)vi->i_db_count);
indirect =
max((int)0, ((int)vi->i_db_count) - VVSFS_LAST_DIRECT_BLOCK_INDEX);
for (i = 0; i < direct; i++) {
vvsfs_free_data_block(i_sb->dmap, vi->i_data[i]);
}
if (indirect == 0) {
goto free_inode;
}
bh = READ_BLOCK(sb, vi, VVSFS_LAST_DIRECT_BLOCK_INDEX);
if (!bh) {
return -EIO;
}
for (i = 0; i < indirect; i++) {
index =
read_int_from_buffer(bh->b_data + (i * VVSFS_INDIRECT_PTR_SIZE));
vvsfs_free_data_block(i_sb->dmap, index);
}
brelse(bh);
vvsfs_free_data_block(i_sb->dmap,
vi->i_data[VVSFS_LAST_DIRECT_BLOCK_INDEX]);
free_inode:
vvsfs_free_inode_block(i_sb->imap, inode->i_ino);
return 0;
}
/* Drops the link count of a given inode and frees the resources if applicable
*
* @inode: Target inode to drop link count of
*
* @return: (int) 0 if successful, error otherwise
*/
int vvsfs_drop_inode_link(struct inode *inode) {
DEBUG_LOG("vvsfs - drop inode link");
inode_dec_link_count(inode);
if (inode->i_nlink == 0) {
return vvsfs_free_inode_blocks(inode);
}
return 0;
}
/* Calculate the data block map index for a given position
* within the given inode data blocks.
*
* @vi: Inode information of the target inode
* @sb: Superblock of the filesytsem
* @d_pos: Position of the data block within the inode
*
* @return: (int): 0 or greater if data block exists in
* inode, error otherwise
*/
int vvsfs_index_data_block(struct vvsfs_inode_info *vi,
struct super_block *sb,
uint32_t d_pos) {
struct buffer_head *bh;
int offset;
uint32_t index;
DEBUG_LOG("vvsfs - index_data_block\n");
DEBUG_LOG("vvsfs - index_data_block - d_pos: %u\n", d_pos);
if (d_pos < VVSFS_LAST_DIRECT_BLOCK_INDEX) {
DEBUG_LOG("vvsfs - index_data_block - direct done\n");
return vi->i_data[d_pos];
}
bh = READ_BLOCK(sb, vi, VVSFS_LAST_DIRECT_BLOCK_INDEX);
if (!bh) {
DEBUG_LOG("vvsfs - index_data_block - failed to read buffer data\n");
return -EIO;
}
offset = (d_pos - VVSFS_LAST_DIRECT_BLOCK_INDEX) * VVSFS_INDIRECT_PTR_SIZE;
DEBUG_LOG("vvsfs - index_data_block - offset: %u\n", offset);
index = read_int_from_buffer(bh->b_data + offset);
brelse(bh);
DEBUG_LOG(
"vvsfs - index_data_block - indirect done: %u -> %d\n", offset, index);
return index;
}
/* Given a position into the target inode data blocks,
* create and assign a new data block.
*
* @dir_info: Inode information of target inode
* @sb: Superblock of the filesystem
* @d_pos: Data block position to create at
*
* @return: (int) 0 or greater, data block map index,
* error otherwise
*/
int vvsfs_assign_data_block(struct vvsfs_inode_info *dir_info,
struct super_block *sb,
uint32_t d_pos) {
struct buffer_head *bh;
struct vvsfs_sb_info *sbi = sb->s_fs_info;
int offset;
uint32_t indirect_block;
uint32_t newblock;
DEBUG_LOG("vvsfs - assign_data_block\n");
newblock = vvsfs_reserve_data_block(sbi->dmap);
if (!newblock) {
return -ENOSPC;
}
DEBUG_LOG("vvsfs - assign-data_block - current block count: %u\n",
dir_info->i_db_count);
if (d_pos < VVSFS_LAST_DIRECT_BLOCK_INDEX) {
DEBUG_LOG("vvsfs - assign_data_block - direct blocks free, allocating "
"direct\n");
// Still have room in direct blocks, assign
// there
dir_info->i_data[d_pos] = newblock;
dir_info->i_db_count++;
goto done;
}
DEBUG_LOG("vvsfs - assign_data_block - no direct blocks free, allocating "
"indirect\n");
if (unlikely(dir_info->i_db_count < VVSFS_N_BLOCKS)) {
// We don't have a block to store indirect pointers
// use the newblock just allocated for consistency
// and reserve another block for the actual data
DEBUG_LOG("vvsfs - assign_data_block - indirect block not allocated, "
"allocating\n");
indirect_block = newblock;
newblock = vvsfs_reserve_data_block(sbi->dmap);
if (!newblock) {
vvsfs_free_data_block(sbi->dmap, indirect_block);
return -ENOSPC;
}
dir_info->i_data[VVSFS_LAST_DIRECT_BLOCK_INDEX] = indirect_block;
}
bh = READ_BLOCK(sb, dir_info, VVSFS_LAST_DIRECT_BLOCK_INDEX);
if (!bh) {
DEBUG_LOG("vvsfs - assign_data_block - buffer read failed\n");
vvsfs_free_data_block(sbi->dmap, newblock);
vvsfs_free_data_block(sbi->dmap, indirect_block);
dir_info->i_data[VVSFS_LAST_DIRECT_BLOCK_INDEX] = 0;
return -EIO;
}
dir_info->i_db_count++;
offset = (d_pos - VVSFS_LAST_DIRECT_BLOCK_INDEX) * VVSFS_INDIRECT_PTR_SIZE;
DEBUG_LOG("vvsfs - assign_data_block - indirect block offset: %d <- %u\n",
offset,
newblock);
write_int_to_buffer(bh->b_data + offset, newblock);
mark_buffer_dirty(bh);
sync_dirty_buffer(bh);
brelse(bh);
done:
DEBUG_LOG("vvsfs - assign_data_block - done\n");
return newblock;
}
// vvsfs_reserve_inode_block
// @map: a bitmap representing inode blocks on disk
//
// On success, this function returns a valid inode number and update the
// corresponding inode bitmap. On failure, it will return 0 (which is an invalid
// inode number), and leave the bitmap unchanged.
uint32_t vvsfs_reserve_inode_block(uint8_t *map) {
uint32_t i = vvsfs_find_free_block(map, VVSFS_IMAP_SIZE);
if (i == 0)
return 0;
return BNO_TO_INO(i);
}
// vvsfs_new_inode - find and construct a new inode.
// @dir: the inode of the parent directory where the new inode is supposed to be
// attached to.
// @mode: the mode information of the new inode
//
// This is a helper function for the inode operation "create" (implemented in
// vvsfs_create() ). It takes care of reserving an inode block on disk (by
// modifiying the inode bitmap), creating an VFS inode object (in memory) and
// attach filesystem-specific information to that VFS inode.
struct inode *
vvsfs_new_inode(const struct inode *dir, umode_t mode, dev_t rdev) {
struct vvsfs_inode_info *inode_info;
struct super_block *sb;
struct vvsfs_sb_info *sbi;
struct inode *inode;
unsigned long dno, ino;
int i;
LOG("vvsfs - new inode\n");
// get the filesystem specific info for the super block. The sbi object
// contains the inode bitmap.
sb = dir->i_sb;
sbi = sb->s_fs_info;
/*
Find a spare inode in the vvsfs.
The vvsfs_reserve_inode_block() will attempt to find the first free
inode and allocates it, and returns the inode number. Note that the inode
number is *not* the same as the disk block address on disk.
*/
ino = vvsfs_reserve_inode_block(sbi->imap);
if (BAD_INO(ino))
return ERR_PTR(-ENOSPC);
/* create a new VFS (in memory) inode */
inode = new_inode(sb);
if (!inode) {
// if failed, release the inode/data blocks so they can be reused.
vvsfs_free_inode_block(sbi->imap, ino);
vvsfs_free_data_block(sbi->dmap, dno);
return ERR_PTR(-ENOMEM);
}
// fill in various information for the VFS inode.
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 3, 0)
inode_init_owner(&init_user_ns, inode, dir, mode);
#else
inode_init_owner(&nop_mnt_idmap, inode, dir, mode);
#endif
inode->i_ino = ino;
inode->i_ctime = inode->i_mtime = inode->i_atime = current_time(inode);
inode->i_mode = mode;
inode->i_size = 0;
inode->i_blocks = (VVSFS_BLOCKSIZE / VVSFS_SECTORSIZE);
// increment the link counter. This basically increments inode->i_nlink,
// but that member cannot be modified directly. Use instead set_nlink to set
// it to a specific value.
set_nlink(inode, 1);
// check if the inode is for a directory, using the macro S_ISDIR
if (S_ISREG(mode)) {
inode->i_op = &vvsfs_file_inode_operations;
inode->i_fop = &vvsfs_file_operations;
inode->i_mapping->a_ops = &vvsfs_as_operations;
} else if (S_ISDIR(mode)) {
inode->i_op = &vvsfs_dir_inode_operations;
inode->i_fop = &vvsfs_dir_operations;
} else if (S_ISLNK(mode)) {
inode->i_op = &vvsfs_symlink_inode_operations;
// since we are using page_symlink we need to set this first
inode_nohighmem(inode);
inode->i_mapping->a_ops = &vvsfs_as_operations;
} else {
// inode is special
init_special_inode(inode, inode->i_mode, rdev);
}
/*
Now fill in the filesystem specific information.
This is done by first obtaining the vvsfs_inode_info struct from
the VFS inode using the VVSFS_I macro.
*/
inode_info = VVSFS_I(inode);
inode_info->i_db_count = 0;
for (i = 0; i < VVSFS_N_BLOCKS; ++i)
inode_info->i_data[i] = 0;
// Make sure you hash the inode, so that VFS can keep track of its "dirty"
// status and writes it to disk if needed.
insert_inode_hash(inode);
// Mark the inode as "dirty". This will inform the VFS that this inode needs
// to be written to disk. The procedure for writing to disk is implemented
// in vvsfs_write_inode() (as part of the "super" operations).
mark_inode_dirty(inode);
LOG("vvsfs - new_inode - done");
return inode;
}
// This is a helper function for the "create"
// inode operation. It adds a new entry to the
// list of directory entries in the parent
// directory.
static int vvsfs_add_new_entry(struct inode *dir,
struct dentry *dentry,
struct inode *inode) {
struct vvsfs_inode_info *dir_info = VVSFS_I(dir);
struct super_block *sb = dir->i_sb;
struct vvsfs_dir_entry *dent;
struct buffer_head *bh;
int num_dirs;
uint32_t d_pos, d_off, dno;
int newblock;
int raw_dno;
// calculate the number of entries from the i_size
// of the directory's inode.
num_dirs = dir->i_size / VVSFS_DENTRYSIZE;
if (num_dirs >= VVSFS_MAX_DENTRIES) {
DEBUG_LOG("vvsfs - add_new_entry - exceeded max dentries %d >= %u, "
"(i_size: %lld)\n",
num_dirs,
(uint32_t)VVSFS_MAX_DENTRIES,
dir->i_size);
return -ENOSPC;
}
// Calculate the position of the new entry within
// the data blocks
d_pos = num_dirs / VVSFS_N_DENTRY_PER_BLOCK;
d_off = num_dirs % VVSFS_N_DENTRY_PER_BLOCK;
DEBUG_LOG(
"vvsfs - add_new_entry - position: %u, offset: %u\n", d_pos, d_off);
/* If the block is not yet allocated, allocate it.
*/
if (d_pos >= dir_info->i_db_count) {
LOG("vvsfs - add_new_entry - add new data block "
"for directory entry\n");
newblock = vvsfs_assign_data_block(dir_info, sb, d_pos);
if (newblock < 0) {
DEBUG_LOG("vvsfs - add_new_entry - failed data block assignment\n");
return newblock;
}
dno = (uint32_t)newblock;
} else {
if ((raw_dno = vvsfs_index_data_block(dir_info, sb, d_pos)) < 0) {
DEBUG_LOG(
"vvsfs - add_new_entry - failed get data block index %d\n",
raw_dno);
return raw_dno;
}
dno = (uint32_t)raw_dno;