forked from Light-Dedup/Light-Dedup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inode.c
1457 lines (1235 loc) · 37.6 KB
/
inode.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
/*
* BRIEF DESCRIPTION
*
* Inode methods (allocate/free/read/write).
*
* Copyright 2015-2016 Regents of the University of California,
* UCSD Non-Volatile Systems Lab, Andiry Xu <[email protected]>
* Copyright 2012-2013 Intel Corporation
* Copyright 2009-2011 Marco Stornelli <[email protected]>
* Copyright 2003 Sony Corporation
* Copyright 2003 Matsushita Electric Industrial Co., Ltd.
* 2003-2004 (c) MontaVista Software, Inc. , Steve Longerbeam
* This file is licensed under the terms of the GNU General Public
* License version 2. This program is licensed "as is" without any
* warranty of any kind, whether express or implied.
*/
#include <linux/fs.h>
#include <linux/aio.h>
#include <linux/highuid.h>
#include <linux/module.h>
#include <linux/mpage.h>
#include <linux/backing-dev.h>
#include <linux/types.h>
#include <linux/ratelimit.h>
#include "nova.h"
#include "inode.h"
unsigned int blk_type_to_shift[NOVA_BLOCK_TYPE_MAX] = {12, 21, 30};
uint32_t blk_type_to_size[NOVA_BLOCK_TYPE_MAX] = {0x1000, 0x200000, 0x40000000};
int nova_init_inode_inuse_list(struct super_block *sb)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
struct nova_range_node *range_node;
struct inode_map *inode_map;
unsigned long range_high;
int i;
int ret;
sbi->s_inodes_used_count = NOVA_NORMAL_INODE_START;
range_high = NOVA_NORMAL_INODE_START / sbi->cpus;
if (NOVA_NORMAL_INODE_START % sbi->cpus)
range_high++;
for (i = 0; i < sbi->cpus; i++) {
inode_map = &sbi->inode_maps[i];
range_node = nova_alloc_inode_node(sb);
if (range_node == NULL)
/* FIXME: free allocated memories */
return -ENOMEM;
range_node->range_low = 0;
range_node->range_high = range_high;
nova_update_range_node_checksum(range_node);
ret = nova_insert_inodetree(sbi, range_node, i);
if (ret) {
nova_err(sb, "%s failed\n", __func__);
nova_free_inode_node(range_node);
return ret;
}
inode_map->num_range_node_inode = 1;
inode_map->first_inode_range = range_node;
}
return 0;
}
static int nova_alloc_inode_table(struct super_block *sb,
struct nova_inode_info_header *sih, int version)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
struct inode_table *inode_table;
unsigned long blocknr;
u64 block;
int allocated;
int i;
unsigned long irq_flags = 0;
for (i = 0; i < sbi->cpus; i++) {
inode_table = nova_get_inode_table(sb, version, i);
if (!inode_table)
return -EINVAL;
/* Allocate replicate inodes from tail */
allocated = nova_new_log_blocks(sb, sih, &blocknr, 1,
ALLOC_INIT_ZERO, i,
version ? ALLOC_FROM_TAIL : ALLOC_FROM_HEAD);
nova_dbgv("%s: allocate log @ 0x%lx\n", __func__,
blocknr);
if (allocated != 1 || blocknr == 0)
return -ENOSPC;
block = nova_get_block_off(sb, blocknr, NOVA_BLOCK_TYPE_2M);
nova_memunlock_range(sb, inode_table, CACHELINE_SIZE, &irq_flags);
inode_table->log_head = block;
nova_memlock_range(sb, inode_table, CACHELINE_SIZE, &irq_flags);
nova_flush_buffer(inode_table, CACHELINE_SIZE, 0);
}
return 0;
}
int nova_init_inode_table(struct super_block *sb)
{
struct nova_inode *pi = nova_get_inode_by_ino(sb, NOVA_INODETABLE_INO);
struct nova_inode_info_header sih;
int num_tables;
int ret = 0;
int i;
unsigned long irq_flags = 0;
nova_memunlock_inode(sb, pi, &irq_flags);
pi->i_mode = 0;
pi->i_uid = 0;
pi->i_gid = 0;
pi->i_links_count = cpu_to_le16(1);
pi->i_flags = 0;
pi->nova_ino = NOVA_INODETABLE_INO;
pi->i_blk_type = NOVA_BLOCK_TYPE_2M;
nova_memlock_inode(sb, pi, &irq_flags);
sih.ino = NOVA_INODETABLE_INO;
sih.i_blk_type = NOVA_BLOCK_TYPE_2M;
num_tables = 1;
if (metadata_csum)
num_tables = 2;
for (i = 0; i < num_tables; i++) {
ret = nova_alloc_inode_table(sb, &sih, i);
if (ret)
return ret;
}
PERSISTENT_BARRIER();
return ret;
}
inline int nova_insert_inodetree(struct nova_sb_info *sbi,
struct nova_range_node *new_node, int cpu)
{
struct rb_root *tree;
int ret;
tree = &sbi->inode_maps[cpu].inode_inuse_tree;
ret = nova_insert_range_node(tree, new_node, NODE_INODE);
if (ret)
nova_dbg("ERROR: %s failed %d\n", __func__, ret);
return ret;
}
inline int nova_search_inodetree(struct nova_sb_info *sbi,
unsigned long ino, struct nova_range_node **ret_node)
{
struct rb_root *tree;
unsigned long internal_ino;
int cpu;
cpu = ino % sbi->cpus;
tree = &sbi->inode_maps[cpu].inode_inuse_tree;
internal_ino = ino / sbi->cpus;
return nova_find_range_node(tree, internal_ino,
NODE_INODE, ret_node);
}
/* Get the address in PMEM of an inode by inode number. Allocate additional
* block to store additional inodes if necessary.
*/
int nova_get_inode_address(struct super_block *sb, u64 ino, int version,
u64 *pi_addr, int extendable, int extend_alternate)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
struct nova_inode_info_header sih;
struct inode_table *inode_table;
unsigned int data_bits;
unsigned int num_inodes_bits;
u64 curr;
unsigned int superpage_count;
u64 alternate_pi_addr = 0;
u64 internal_ino;
int cpuid;
int extended = 0;
unsigned int index;
unsigned int i = 0;
unsigned long blocknr;
unsigned long curr_addr;
int allocated;
unsigned long irq_flags = 0;
if (ino < NOVA_NORMAL_INODE_START) {
*pi_addr = nova_get_reserved_inode_addr(sb, ino);
return 0;
}
sih.ino = NOVA_INODETABLE_INO;
sih.i_blk_type = NOVA_BLOCK_TYPE_2M;
data_bits = blk_type_to_shift[sih.i_blk_type];
num_inodes_bits = data_bits - NOVA_INODE_BITS;
cpuid = ino % sbi->cpus;
internal_ino = ino / sbi->cpus;
inode_table = nova_get_inode_table(sb, version, cpuid);
superpage_count = internal_ino >> num_inodes_bits;
index = internal_ino & ((1 << num_inodes_bits) - 1);
curr = inode_table->log_head;
if (curr == 0)
return -EINVAL;
for (i = 0; i < superpage_count; i++) {
if (curr == 0)
return -EINVAL;
curr_addr = (unsigned long)nova_get_block(sb, curr);
/* Next page pointer in the last 8 bytes of the superpage */
curr_addr += nova_inode_blk_size(&sih) - 8;
curr = *(u64 *)(curr_addr);
if (curr == 0) {
if (extendable == 0)
return -EINVAL;
extended = 1;
allocated = nova_new_log_blocks(sb, &sih, &blocknr,
1, ALLOC_INIT_ZERO, cpuid,
version ? ALLOC_FROM_TAIL : ALLOC_FROM_HEAD);
if (allocated != 1)
return allocated;
curr = nova_get_block_off(sb, blocknr,
NOVA_BLOCK_TYPE_2M);
nova_memunlock_range(sb, (void *)curr_addr,
CACHELINE_SIZE, &irq_flags);
*(u64 *)(curr_addr) = curr;
nova_memlock_range(sb, (void *)curr_addr,
CACHELINE_SIZE, &irq_flags);
nova_flush_buffer((void *)curr_addr,
NOVA_INODE_SIZE, 1);
}
}
/* Extend alternate inode table */
if (extended && extend_alternate && metadata_csum)
nova_get_inode_address(sb, ino, version + 1,
&alternate_pi_addr, extendable, 0);
*pi_addr = curr + index * NOVA_INODE_SIZE;
return 0;
}
int nova_get_alter_inode_address(struct super_block *sb, u64 ino,
u64 *alter_pi_addr)
{
int ret;
if (metadata_csum == 0) {
nova_err(sb, "Access alter inode when replica inode disabled\n");
return 0;
}
if (ino < NOVA_NORMAL_INODE_START) {
*alter_pi_addr = nova_get_alter_reserved_inode_addr(sb, ino);
} else {
ret = nova_get_inode_address(sb, ino, 1, alter_pi_addr, 0, 0);
if (ret)
return ret;
}
return 0;
}
int nova_delete_file_tree(struct super_block *sb,
struct nova_inode_info_header *sih, unsigned long start_pgoff,
unsigned long last_pgoff, bool delete_nvmm, bool delete_dead,
u64 epoch_id)
{
struct nova_file_write_entry *entry;
struct nova_file_write_entry *entryc, entry_copy;
struct nova_file_write_entry *old_entry = NULL;
unsigned long pgoff = start_pgoff;
unsigned long old_pgoff = 0;
unsigned int num_free = 0;
int freed = 0;
void *ret;
INIT_TIMING(delete_time);
NOVA_START_TIMING(delete_file_tree_t, delete_time);
entryc = (metadata_csum == 0) ? entry : &entry_copy;
/* Handle EOF blocks */
do {
entry = radix_tree_lookup(&sih->tree, pgoff);
if (entry) {
ret = radix_tree_delete(&sih->tree, pgoff);
BUG_ON(!ret || ret != entry);
if (entry != old_entry) {
if (old_entry && delete_nvmm) {
nova_free_old_entry(sb, sih,
old_entry, old_pgoff,
num_free, delete_dead,
epoch_id);
freed += num_free;
}
old_entry = entry;
old_pgoff = pgoff;
num_free = 1;
} else {
num_free++;
}
pgoff++;
} else {
/* We are finding a hole. Jump to the next entry. */
entry = nova_find_next_entry(sb, sih, pgoff);
if (!entry)
break;
if (metadata_csum == 0)
entryc = entry;
else if (!nova_verify_entry_csum(sb, entry, entryc))
break;
pgoff++;
pgoff = pgoff > entryc->pgoff ? pgoff : entryc->pgoff;
}
} while (pgoff <= last_pgoff);
if (old_entry && delete_nvmm) {
nova_free_old_entry(sb, sih, old_entry, old_pgoff,
num_free, delete_dead, epoch_id);
freed += num_free;
}
nova_dbgv("Inode %lu: delete file tree from pgoff %lu to %lu, %d blocks freed\n",
sih->ino, start_pgoff, last_pgoff, freed);
NOVA_END_TIMING(delete_file_tree_t, delete_time);
return freed;
}
static int nova_free_dram_resource(struct super_block *sb,
struct nova_inode_info_header *sih)
{
unsigned long last_blocknr;
int freed = 0;
if (sih->ino == 0)
return 0;
if (!(S_ISREG(sih->i_mode)) && !(S_ISDIR(sih->i_mode)))
return 0;
if (S_ISREG(sih->i_mode)) {
last_blocknr = nova_get_last_blocknr(sb, sih);
freed = nova_delete_file_tree(sb, sih, 0,
last_blocknr, false, false, 0);
} else {
nova_delete_dir_tree(sb, sih);
freed = 1;
}
return freed;
}
static inline void check_eof_blocks(struct super_block *sb,
struct nova_inode *pi, struct inode *inode,
struct nova_inode_info_header *sih)
{
unsigned long irq_flags = 0;
if ((pi->i_flags & cpu_to_le32(NOVA_EOFBLOCKS_FL)) &&
(inode->i_size + sb->s_blocksize) > (sih->i_blocks
<< sb->s_blocksize_bits)) {
nova_memunlock_inode(sb, pi, &irq_flags);
pi->i_flags &= cpu_to_le32(~NOVA_EOFBLOCKS_FL);
nova_update_inode_checksum(pi);
nova_update_alter_inode(sb, inode, pi);
nova_memlock_inode(sb, pi, &irq_flags);
}
}
/*
* Free data blocks from inode in the range start <=> end
*/
void nova_truncate_file_blocks(struct inode *inode, loff_t start,
loff_t end, u64 epoch_id)
{
struct super_block *sb = inode->i_sb;
struct nova_inode *pi = nova_get_inode(sb, inode);
struct nova_inode_info *si = NOVA_I(inode);
struct nova_inode_info_header *sih = &si->header;
unsigned int data_bits = blk_type_to_shift[sih->i_blk_type];
unsigned long first_blocknr, last_blocknr;
int freed = 0;
inode->i_mtime = inode->i_ctime = current_time(inode);
nova_dbg_verbose("truncate: pi %p iblocks %lx %llx %llx %llx\n", pi,
sih->i_blocks, start, end, pi->i_size);
first_blocknr = (start + (1UL << data_bits) - 1) >> data_bits;
if (end == 0)
return;
last_blocknr = (end - 1) >> data_bits;
if (first_blocknr > last_blocknr)
return;
freed = nova_delete_file_tree(sb, sih, first_blocknr,
last_blocknr, true, false, epoch_id);
inode->i_blocks -= (freed * (1 << (data_bits -
sb->s_blocksize_bits)));
sih->i_blocks = inode->i_blocks;
/* Check for the flag EOFBLOCKS is still valid after the set size */
check_eof_blocks(sb, pi, inode, sih);
}
/* search the radix tree to find hole or data
* in the specified range
* Input:
* first_blocknr: first block in the specified range
* last_blocknr: last_blocknr in the specified range
* @data_found: indicates whether data blocks were found
* @hole_found: indicates whether a hole was found
* hole: whether we are looking for a hole or data
*/
static int nova_lookup_hole_in_range(struct super_block *sb,
struct nova_inode_info_header *sih,
unsigned long first_blocknr, unsigned long last_blocknr,
int *data_found, int *hole_found, int hole)
{
struct nova_file_write_entry *entry;
struct nova_file_write_entry *entryc, entry_copy;
unsigned long blocks = 0;
unsigned long pgoff, old_pgoff;
entryc = (metadata_csum == 0) ? entry : &entry_copy;
pgoff = first_blocknr;
while (pgoff <= last_blocknr) {
old_pgoff = pgoff;
entry = radix_tree_lookup(&sih->tree, pgoff);
if (entry) {
*data_found = 1;
if (!hole)
goto done;
pgoff++;
} else {
*hole_found = 1;
entry = nova_find_next_entry(sb, sih, pgoff);
pgoff++;
if (entry) {
if (metadata_csum == 0)
entryc = entry;
else if (!nova_verify_entry_csum(sb, entry,
entryc))
goto done;
pgoff = pgoff > entryc->pgoff ?
pgoff : entryc->pgoff;
if (pgoff > last_blocknr)
pgoff = last_blocknr + 1;
}
}
if (!*hole_found || !hole)
blocks += pgoff - old_pgoff;
}
done:
return blocks;
}
/* copy persistent state to struct inode */
static int nova_read_inode(struct super_block *sb, struct inode *inode,
u64 pi_addr)
{
struct nova_inode_info *si = NOVA_I(inode);
struct nova_inode *pi, fake_pi;
struct nova_inode_info_header *sih = &si->header;
int ret = -EIO;
unsigned long ino;
ret = nova_get_reference(sb, pi_addr, &fake_pi,
(void **)&pi, sizeof(struct nova_inode));
if (ret) {
nova_dbg("%s: read pi @ 0x%llx failed\n",
__func__, pi_addr);
goto bad_inode;
}
inode->i_mode = sih->i_mode;
i_uid_write(inode, le32_to_cpu(pi->i_uid));
i_gid_write(inode, le32_to_cpu(pi->i_gid));
// set_nlink(inode, le16_to_cpu(pi->i_links_count));
inode->i_generation = le32_to_cpu(pi->i_generation);
nova_set_inode_flags(inode, pi, le32_to_cpu(pi->i_flags));
ino = inode->i_ino;
/* check if the inode is active. */
if (inode->i_mode == 0 || pi->deleted == 1) {
/* this inode is deleted */
ret = -ESTALE;
goto bad_inode;
}
inode->i_blocks = sih->i_blocks;
inode->i_mapping->a_ops = &nova_aops_dax;
switch (inode->i_mode & S_IFMT) {
case S_IFREG:
inode->i_op = &nova_file_inode_operations;
if (!test_opt(inode->i_sb, DATA_COW) && wprotect == 0)
inode->i_fop = &nova_dax_file_operations;
else
inode->i_fop = &nova_wrap_file_operations;
break;
case S_IFDIR:
inode->i_op = &nova_dir_inode_operations;
inode->i_fop = &nova_dir_operations;
break;
case S_IFLNK:
inode->i_op = &nova_symlink_inode_operations;
break;
default:
inode->i_op = &nova_special_inode_operations;
init_special_inode(inode, inode->i_mode,
le32_to_cpu(pi->dev.rdev));
break;
}
/* Update size and time after rebuild the tree */
inode->i_size = le64_to_cpu(sih->i_size);
inode->i_atime.tv_sec = (__s32)le32_to_cpu(pi->i_atime);
inode->i_ctime.tv_sec = (__s32)le32_to_cpu(pi->i_ctime);
inode->i_mtime.tv_sec = (__s32)le32_to_cpu(pi->i_mtime);
inode->i_atime.tv_nsec = inode->i_mtime.tv_nsec =
inode->i_ctime.tv_nsec = 0;
set_nlink(inode, le16_to_cpu(pi->i_links_count));
return 0;
bad_inode:
make_bad_inode(inode);
return ret;
}
static void nova_get_inode_flags(struct inode *inode, struct nova_inode *pi)
{
unsigned int flags = inode->i_flags;
unsigned int nova_flags = le32_to_cpu(pi->i_flags);
nova_flags &= ~(FS_SYNC_FL | FS_APPEND_FL | FS_IMMUTABLE_FL |
FS_NOATIME_FL | FS_DIRSYNC_FL);
if (flags & S_SYNC)
nova_flags |= FS_SYNC_FL;
if (flags & S_APPEND)
nova_flags |= FS_APPEND_FL;
if (flags & S_IMMUTABLE)
nova_flags |= FS_IMMUTABLE_FL;
if (flags & S_NOATIME)
nova_flags |= FS_NOATIME_FL;
if (flags & S_DIRSYNC)
nova_flags |= FS_DIRSYNC_FL;
pi->i_flags = cpu_to_le32(nova_flags);
}
static void nova_init_inode(struct inode *inode, struct nova_inode *pi)
{
pi->i_mode = cpu_to_le16(inode->i_mode);
pi->i_uid = cpu_to_le32(i_uid_read(inode));
pi->i_gid = cpu_to_le32(i_gid_read(inode));
pi->i_links_count = cpu_to_le16(inode->i_nlink);
pi->i_size = cpu_to_le64(inode->i_size);
pi->i_atime = cpu_to_le32(inode->i_atime.tv_sec);
pi->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
pi->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
pi->i_generation = cpu_to_le32(inode->i_generation);
pi->log_head = 0;
pi->log_tail = 0;
pi->alter_log_head = 0;
pi->alter_log_tail = 0;
pi->deleted = 0;
pi->delete_epoch_id = 0;
nova_get_inode_flags(inode, pi);
if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
pi->dev.rdev = cpu_to_le32(inode->i_rdev);
}
static int nova_alloc_unused_inode(struct super_block *sb, int cpuid,
unsigned long *ino)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
struct inode_map *inode_map;
struct nova_range_node *i, *next_i;
struct rb_node *temp, *next;
unsigned long next_range_low;
unsigned long new_ino;
unsigned long MAX_INODE = 1UL << 31;
inode_map = &sbi->inode_maps[cpuid];
i = inode_map->first_inode_range;
NOVA_ASSERT(i);
if (!nova_range_node_checksum_ok(i)) {
nova_dbg("%s: first node failed\n", __func__);
return -EIO;
}
temp = &i->node;
next = rb_next(temp);
if (!next) {
next_i = NULL;
next_range_low = MAX_INODE;
} else {
next_i = container_of(next, struct nova_range_node, node);
if (!nova_range_node_checksum_ok(next_i)) {
nova_dbg("%s: second node failed\n", __func__);
return -EIO;
}
next_range_low = next_i->range_low;
}
new_ino = i->range_high + 1;
if (next_i && new_ino == (next_range_low - 1)) {
/* Fill the gap completely */
i->range_high = next_i->range_high;
nova_update_range_node_checksum(i);
rb_erase(&next_i->node, &inode_map->inode_inuse_tree);
nova_free_inode_node(next_i);
inode_map->num_range_node_inode--;
} else if (new_ino < (next_range_low - 1)) {
/* Aligns to left */
i->range_high = new_ino;
nova_update_range_node_checksum(i);
} else {
nova_dbg("%s: ERROR: new ino %lu, next low %lu\n", __func__,
new_ino, next_range_low);
return -ENOSPC;
}
*ino = new_ino * sbi->cpus + cpuid;
sbi->s_inodes_used_count++;
inode_map->allocated++;
nova_dbg_verbose("Alloc ino %lu\n", *ino);
return 0;
}
static int nova_free_inuse_inode(struct super_block *sb, unsigned long ino)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
struct inode_map *inode_map;
struct nova_range_node *i = NULL;
struct nova_range_node *curr_node;
int found = 0;
int cpuid = ino % sbi->cpus;
unsigned long internal_ino = ino / sbi->cpus;
int ret = 0;
nova_dbg_verbose("Free inuse ino: %lu\n", ino);
inode_map = &sbi->inode_maps[cpuid];
mutex_lock(&inode_map->inode_table_mutex);
found = nova_search_inodetree(sbi, ino, &i);
if (!found) {
nova_dbg("%s ERROR: ino %lu not found\n", __func__, ino);
mutex_unlock(&inode_map->inode_table_mutex);
return -EINVAL;
}
if ((internal_ino == i->range_low) && (internal_ino == i->range_high)) {
/* fits entire node */
rb_erase(&i->node, &inode_map->inode_inuse_tree);
nova_free_inode_node(i);
inode_map->num_range_node_inode--;
goto block_found;
}
if ((internal_ino == i->range_low) && (internal_ino < i->range_high)) {
/* Aligns left */
i->range_low = internal_ino + 1;
nova_update_range_node_checksum(i);
goto block_found;
}
if ((internal_ino > i->range_low) && (internal_ino == i->range_high)) {
/* Aligns right */
i->range_high = internal_ino - 1;
nova_update_range_node_checksum(i);
goto block_found;
}
if ((internal_ino > i->range_low) && (internal_ino < i->range_high)) {
/* Aligns somewhere in the middle */
curr_node = nova_alloc_inode_node(sb);
NOVA_ASSERT(curr_node);
if (curr_node == NULL) {
/* returning without freeing the block */
goto block_found;
}
curr_node->range_low = internal_ino + 1;
curr_node->range_high = i->range_high;
nova_update_range_node_checksum(curr_node);
i->range_high = internal_ino - 1;
nova_update_range_node_checksum(i);
ret = nova_insert_inodetree(sbi, curr_node, cpuid);
if (ret) {
nova_free_inode_node(curr_node);
goto err;
}
inode_map->num_range_node_inode++;
goto block_found;
}
err:
nova_error_mng(sb, "Unable to free inode %lu\n", ino);
nova_error_mng(sb, "Found inuse block %lu - %lu\n",
i->range_low, i->range_high);
mutex_unlock(&inode_map->inode_table_mutex);
return ret;
block_found:
sbi->s_inodes_used_count--;
inode_map->freed++;
mutex_unlock(&inode_map->inode_table_mutex);
return ret;
}
static int nova_free_inode(struct super_block *sb, struct nova_inode *pi,
struct nova_inode_info_header *sih)
{
int err = 0;
INIT_TIMING(free_time);
NOVA_START_TIMING(free_inode_t, free_time);
nova_free_inode_log(sb, pi, sih);
sih->log_pages = 0;
sih->i_mode = 0;
sih->pi_addr = 0;
sih->alter_pi_addr = 0;
sih->i_size = 0;
sih->i_blocks = 0;
err = nova_free_inuse_inode(sb, pi->nova_ino);
NOVA_END_TIMING(free_inode_t, free_time);
return err;
}
struct inode *nova_iget(struct super_block *sb, unsigned long ino)
{
struct nova_inode_info *si;
struct inode *inode;
u64 pi_addr;
int err;
inode = iget_locked(sb, ino);
if (unlikely(!inode))
return ERR_PTR(-ENOMEM);
if (!(inode->i_state & I_NEW))
return inode;
si = NOVA_I(inode);
nova_dbgv("%s: inode %lu\n", __func__, ino);
err = nova_get_inode_address(sb, ino, 0, &pi_addr, 0, 0);
if (err) {
nova_dbg("%s: get inode %lu address failed %d\n",
__func__, ino, err);
goto fail;
}
if (pi_addr == 0) {
nova_dbg("%s: failed to get pi_addr for inode %lu\n",
__func__, ino);
err = -EACCES;
goto fail;
}
err = nova_rebuild_inode(sb, si, ino, pi_addr, 1);
if (err) {
nova_dbg("%s: failed to rebuild inode %lu\n", __func__, ino);
goto fail;
}
err = nova_read_inode(sb, inode, pi_addr);
if (unlikely(err)) {
nova_dbg("%s: failed to read inode %lu\n", __func__, ino);
goto fail;
}
inode->i_ino = ino;
unlock_new_inode(inode);
return inode;
fail:
iget_failed(inode);
return ERR_PTR(err);
}
unsigned long nova_get_last_blocknr(struct super_block *sb,
struct nova_inode_info_header *sih)
{
struct nova_inode *pi, fake_pi;
unsigned long last_blocknr;
unsigned int btype;
unsigned int data_bits;
int ret;
ret = nova_get_reference(sb, sih->pi_addr, &fake_pi,
(void **)&pi, sizeof(struct nova_inode));
if (ret) {
nova_dbg("%s: read pi @ 0x%lx failed\n",
__func__, sih->pi_addr);
btype = 0;
} else {
btype = sih->i_blk_type;
}
data_bits = blk_type_to_shift[btype];
if (sih->i_size == 0)
last_blocknr = 0;
else
last_blocknr = (sih->i_size - 1) >> data_bits;
return last_blocknr;
}
static int nova_free_inode_resource(struct super_block *sb,
struct nova_inode *pi, struct nova_inode_info_header *sih)
{
unsigned long last_blocknr;
int ret = 0;
int freed = 0;
struct nova_inode *alter_pi;
unsigned long irq_flags = 0;
nova_memunlock_inode(sb, pi, &irq_flags);
pi->deleted = 1;
if (pi->valid) {
nova_dbg("%s: inode %lu still valid\n",
__func__, sih->ino);
pi->valid = 0;
}
nova_update_inode_checksum(pi);
if (metadata_csum && sih->alter_pi_addr) {
alter_pi = (struct nova_inode *)nova_get_block(sb,
sih->alter_pi_addr);
memcpy_to_pmem_nocache(alter_pi, pi, sizeof(struct nova_inode));
}
nova_memlock_inode(sb, pi, &irq_flags);
/* We need the log to free the blocks from the b-tree */
switch (__le16_to_cpu(pi->i_mode) & S_IFMT) {
case S_IFREG:
last_blocknr = nova_get_last_blocknr(sb, sih);
nova_dbgv("%s: file ino %lu\n", __func__, sih->ino);
freed = nova_delete_file_tree(sb, sih, 0,
last_blocknr, true, true, 0);
break;
case S_IFDIR:
nova_dbgv("%s: dir ino %lu\n", __func__, sih->ino);
nova_delete_dir_tree(sb, sih);
break;
case S_IFLNK:
/* Log will be freed later */
nova_dbgv("%s: symlink ino %lu\n",
__func__, sih->ino);
freed = nova_delete_file_tree(sb, sih, 0, 0,
true, true, 0);
break;
default:
nova_dbgv("%s: special ino %lu\n",
__func__, sih->ino);
break;
}
nova_dbg_verbose("%s: Freed %d\n", __func__, freed);
/* Then we can free the inode */
ret = nova_free_inode(sb, pi, sih);
if (ret)
nova_err(sb, "%s: free inode %lu failed\n",
__func__, sih->ino);
return ret;
}
void nova_evict_inode(struct inode *inode)
{
struct super_block *sb = inode->i_sb;
struct nova_inode *pi = nova_get_inode(sb, inode);
struct nova_inode_info_header *sih = NOVA_IH(inode);
INIT_TIMING(evict_time);
int destroy = 0;
int ret;
NOVA_START_TIMING(evict_inode_t, evict_time);
if (!sih) {
nova_err(sb, "%s: ino %lu sih is NULL!\n",
__func__, inode->i_ino);
NOVA_ASSERT(0);
goto out;
}
// pi can be NULL if the file has already been deleted, but a handle
// remains.
if (pi && pi->nova_ino != inode->i_ino) {
nova_err(sb, "%s: inode %lu ino does not match: %llu\n",
__func__, inode->i_ino, pi->nova_ino);
nova_dbg("inode size %llu, pi addr 0x%lx, pi head 0x%llx, tail 0x%llx, mode %u\n",
inode->i_size, sih->pi_addr, sih->log_head,
sih->log_tail, pi->i_mode);
nova_dbg("sih: ino %lu, inode size %lu, mode %u, inode mode %u\n",
sih->ino, sih->i_size,
sih->i_mode, inode->i_mode);
nova_print_inode_log(sb, inode);
}
/* Check if this inode exists in at least one snapshot. */
if (pi && pi->valid == 0) {
ret = nova_append_inode_to_snapshot(sb, pi);
if (ret == 0)
goto out;
}
nova_dbg_verbose("%s: %lu\n", __func__, inode->i_ino);
if (!inode->i_nlink && !is_bad_inode(inode)) {
if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
goto out;
if (pi) {
ret = nova_free_inode_resource(sb, pi, sih);
if (ret)
goto out;
}
destroy = 1;
pi = NULL; /* we no longer own the nova_inode */
inode->i_mtime = inode->i_ctime = current_time(inode);
inode->i_size = 0;
}
out:
if (destroy == 0) {
nova_dbgv("%s: destroying %lu\n", __func__, inode->i_ino);
nova_free_dram_resource(sb, sih);
}
/* TODO: Since we don't use page-cache, do we really need the following
* call?
*/
truncate_inode_pages(&inode->i_data, 0);
clear_inode(inode);
NOVA_END_TIMING(evict_inode_t, evict_time);
}
/* First rebuild the inode tree, then free the blocks */
int nova_delete_dead_inode(struct super_block *sb, u64 ino)
{
struct nova_inode_info si;
struct nova_inode_info_header *sih;
struct nova_inode *pi;
u64 pi_addr = 0;
int err;
if (ino < NOVA_NORMAL_INODE_START) {
nova_dbg("%s: invalid inode %llu\n", __func__, ino);
return -EINVAL;
}
err = nova_get_inode_address(sb, ino, 0, &pi_addr, 0, 0);
if (err) {
nova_dbg("%s: get inode %llu address failed %d\n",
__func__, ino, err);
return -EINVAL;
}
if (pi_addr == 0)
return -EACCES;