-
Notifications
You must be signed in to change notification settings - Fork 0
/
nova.h
1382 lines (1169 loc) · 37.9 KB
/
nova.h
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
*
* Definitions for the NOVA filesystem.
*
* 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.
*/
#ifndef __NOVA_H
#define __NOVA_H
#include <linux/fs.h>
#include <linux/dax.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/vmalloc.h>
#include <linux/sched.h>
#include <linux/crc16.h>
#include <linux/mutex.h>
#include <linux/pagemap.h>
#include <linux/backing-dev.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/rcupdate.h>
#include <linux/types.h>
#include <linux/rbtree.h>
#include <linux/radix-tree.h>
#include <linux/version.h>
#include <linux/kthread.h>
#include <linux/buffer_head.h>
#include <linux/uio.h>
#include <asm/tlbflush.h>
#include <linux/version.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0)
#include <linux/pfn_t.h>
#endif
#include "nova_def.h"
#include "journal.h"
#include "stats.h"
#include "record.h"
#include "average.h"
#define PAGE_SHIFT_2M 21
#define PAGE_SHIFT_1G 30
/* wwb add 2020-6-27 */
/* wwb add 2020-6-27 */
#define EIGHT_G 8589934592
/* 16G */
#define SIXTEEN_G 17179869184
/* 128G */
#define ONE_H_A_T_E_G 137438953472
#define BLOSK_SIZES 4096
// 128GB
#define BLOCKS_MAX (SIXTEEN_G / BLOSK_SIZES)
extern unsigned long nova_base_address;
extern spinlock_t block_write_count_lock;
extern struct mutex ** inode_page_locks;
extern unsigned long * block_write_count;
extern unsigned long global_max_update;
extern atomic64_t write_traffic;
/* end 2020-6-27 */
#define NOVA_ASSERT(x) \
if (!(x)) { \
printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
__FILE__, __LINE__, #x); \
}
/*
* Debug code
*/
#ifdef pr_fmt
#undef pr_fmt
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#endif
/* #define nova_dbg(s, args...) pr_debug(s, ## args) */
#define nova_dbg(s, args ...) pr_info(s, ## args)
#define nova_dbg1(s, args ...)
#define nova_err(sb, s, args ...) nova_error_mng(sb, s, ## args)
#define nova_warn(s, args ...) pr_warning(s, ## args)
#define nova_info(s, args ...) pr_info(s, ## args)
extern unsigned int nova_dbgmask;
#define NOVA_DBGMASK_MMAPHUGE (0x00000001)
#define NOVA_DBGMASK_MMAP4K (0x00000002)
#define NOVA_DBGMASK_MMAPVERBOSE (0x00000004)
#define NOVA_DBGMASK_MMAPVVERBOSE (0x00000008)
#define NOVA_DBGMASK_VERBOSE (0x00000010)
#define NOVA_DBGMASK_TRANSACTION (0x00000020)
#define nova_dbg_mmap4k(s, args ...) \
((nova_dbgmask & NOVA_DBGMASK_MMAP4K) ? nova_dbg(s, args) : 0)
#define nova_dbg_mmapv(s, args ...) \
((nova_dbgmask & NOVA_DBGMASK_MMAPVERBOSE) ? nova_dbg(s, args) : 0)
#define nova_dbg_mmapvv(s, args ...) \
((nova_dbgmask & NOVA_DBGMASK_MMAPVVERBOSE) ? nova_dbg(s, args) : 0)
#define nova_dbg_verbose(s, args ...) \
((nova_dbgmask & NOVA_DBGMASK_VERBOSE) ? nova_dbg(s, ##args) : 0)
#define nova_dbgv(s, args ...) nova_dbg_verbose(s, ##args)
#define nova_dbg_trans(s, args ...) \
((nova_dbgmask & NOVA_DBGMASK_TRANSACTION) ? nova_dbg(s, ##args) : 0)
#define nova_set_bit __test_and_set_bit_le
#define nova_clear_bit __test_and_clear_bit_le
#define nova_find_next_zero_bit find_next_zero_bit_le
#define clear_opt(o, opt) (o &= ~NOVA_MOUNT_ ## opt)
#define set_opt(o, opt) (o |= NOVA_MOUNT_ ## opt)
#define test_opt(sb, opt) (NOVA_SB(sb)->s_mount_opt & NOVA_MOUNT_ ## opt)
#define NOVA_LARGE_INODE_TABLE_SIZE (0x200000)
/* NOVA size threshold for using 2M blocks for inode table */
#define NOVA_LARGE_INODE_TABLE_THREASHOLD (0x20000000)
/*
* nova inode flags
*
* NOVA_EOFBLOCKS_FL There are blocks allocated beyond eof
*/
#define NOVA_EOFBLOCKS_FL 0x20000000
/* Flags that should be inherited by new inodes from their parent. */
#define NOVA_FL_INHERITED (FS_SECRM_FL | FS_UNRM_FL | FS_COMPR_FL | \
FS_SYNC_FL | FS_NODUMP_FL | FS_NOATIME_FL | \
FS_COMPRBLK_FL | FS_NOCOMP_FL | FS_JOURNAL_DATA_FL | \
FS_NOTAIL_FL | FS_DIRSYNC_FL)
/* Flags that are appropriate for regular files (all but dir-specific ones). */
#define NOVA_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
/* Flags that are appropriate for non-directories/regular files. */
#define NOVA_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
#define NOVA_FL_USER_VISIBLE (FS_FL_USER_VISIBLE | NOVA_EOFBLOCKS_FL)
/* IOCTLs */
#define NOVA_PRINT_TIMING 0xBCD00010
#define NOVA_CLEAR_STATS 0xBCD00011
#define NOVA_PRINT_LOG 0xBCD00013
#define NOVA_PRINT_LOG_BLOCKNODE 0xBCD00014
#define NOVA_PRINT_LOG_PAGES 0xBCD00015
#define NOVA_PRINT_FREE_LISTS 0xBCD00018
#define READDIR_END (ULONG_MAX)
#define INVALID_CPU (-1)
#define SHARED_CPU (65536)
#define FREE_BATCH (16)
extern int measure_timing;
extern unsigned int blk_type_to_shift[NOVA_BLOCK_TYPE_MAX];
extern unsigned int blk_type_to_size[NOVA_BLOCK_TYPE_MAX];
/* ======================= Log entry ========================= */
/* Inode entry in the log */
#define INVALID_MASK 4095
#define BLOCK_OFF(p) ((p) & ~INVALID_MASK)
#define ENTRY_LOC(p) ((p) & INVALID_MASK)
enum nova_entry_type {
FILE_WRITE = 1,
DIR_LOG,
SET_ATTR,
LINK_CHANGE,
NEXT_PAGE,
};
static inline u8 nova_get_entry_type(void *p)
{
return *(u8 *)p;
}
static inline void nova_set_entry_type(void *p, enum nova_entry_type type)
{
*(u8 *)p = type;
}
struct nova_file_write_entry {
/* ret of find_nvmm_block, the lowest byte is entry type */
__le64 block;
__le64 pgoff;
__le32 num_pages;
__le32 invalid_pages;
/* For both ctime and mtime */
__le32 mtime;
__le32 padding;
__le64 size;
} __attribute((__packed__));
struct nova_inode_page_tail {
__le64 padding1;
__le64 padding2;
__le64 padding3;
__le64 next_page;
} __attribute((__packed__));
#define LAST_ENTRY 4064
#define PAGE_TAIL(p) (((p) & ~INVALID_MASK) + LAST_ENTRY)
/* Fit in PAGE_SIZE */
struct nova_inode_log_page {
char padding[LAST_ENTRY];
struct nova_inode_page_tail page_tail;
} __attribute((__packed__));
#define EXTEND_THRESHOLD 256
/*
* Structure of a directory log entry in NOVA.
* Update DIR_LOG_REC_LEN if modify this struct!
*/
struct nova_dentry {
u8 entry_type;
u8 name_len; /* length of the dentry name */
u8 file_type; /* file type */
u8 invalid; /* Invalid now? */
__le16 de_len; /* length of this dentry */
__le16 links_count;
__le32 mtime; /* For both mtime and ctime */
__le64 ino; /* inode no pointed to by this entry */
__le64 size;
char name[NOVA_NAME_LEN + 1]; /* File name */
} __attribute((__packed__));
#define NOVA_DIR_PAD 8 /* Align to 8 bytes boundary */
#define NOVA_DIR_ROUND (NOVA_DIR_PAD - 1)
#define NOVA_DIR_LOG_REC_LEN(name_len) (((name_len) + 29 + NOVA_DIR_ROUND) & \
~NOVA_DIR_ROUND)
/* Struct of inode attributes change log (setattr) */
struct nova_setattr_logentry {
u8 entry_type;
u8 attr;
__le16 mode;
__le32 uid;
__le32 gid;
__le32 atime;
__le32 mtime;
__le32 ctime;
__le64 size;
} __attribute((__packed__));
/* Do we need this to be 32 bytes? */
struct nova_link_change_entry {
u8 entry_type;
u8 padding;
__le16 links;
__le32 ctime;
__le32 flags;
__le32 generation;
__le64 paddings[2];
} __attribute((__packed__));
enum alloc_type {
LOG = 1,
DATA,
};
#define MMAP_WRITE_BIT 0x20UL // mmaped for write
#define IS_MAP_WRITE(p) ((p) & (MMAP_WRITE_BIT))
#define MMAP_ADDR(p) ((p) & (PAGE_MASK))
/* wwb add 2020-6-28 */
static inline struct nova_inode *nova_get_inode_by_ino_or_pi_addr(struct super_block *sb,
u64 ino, unsigned long pi_addr);
static inline void inode_page_count_increase_by_vir(u64 viraddr)
{
u64 block_num = (viraddr - nova_base_address) >> 12;
char is_malicious;
if(block_num >= BLOCKS_MAX)
{
printk("WWB: write blocks overflow \n");
return;
}
spin_lock(&block_write_count_lock);
#ifdef MIGRATE_MALICIOUS_APP
is_malicious = is_in_malicious_process_list(current->pid);
if(is_malicious)
{
if(global_blk_write_count[block_num].write_back_threshold >= WB_THRESHOLD)
{
global_blk_write_count[block_num].write_count ++;
global_blk_write_count[block_num].write_back_threshold = 0;
}
global_blk_write_count[block_num].write_back_threshold ++;
goto OUT;
}
#endif
global_blk_write_count[block_num].write_count++;
OUT:
block_write_count[block_num]++;
if(global_max_update < block_write_count[block_num])
global_max_update = block_write_count[block_num];
atomic64_inc(&write_traffic);
spin_unlock(&block_write_count_lock);
//////////////// wwb 20210127 ///////////////////
atomic64_inc(&count_hand);
if(atomic64_read(&count_hand) >= BLOCKS_MAX)
{
atomic64_inc(&count_average);
atomic64_set(&count_hand, 0);
}
/////////////////////////////////////////////////
}
/* end 2020-6-28 */
/* symlink.c */
int nova_block_symlink(struct super_block *sb, struct nova_inode *pi,
struct inode *inode, u64 log_block,
unsigned long name_blocknr, const char *symname, int len);
/* Inline functions start here */
/* Mask out flags that are inappropriate for the given type of inode. */
static inline __le32 nova_mask_flags(umode_t mode, __le32 flags)
{
flags &= cpu_to_le32(NOVA_FL_INHERITED);
if (S_ISDIR(mode))
return flags;
else if (S_ISREG(mode))
return flags & cpu_to_le32(NOVA_REG_FLMASK);
else
return flags & cpu_to_le32(NOVA_OTHER_FLMASK);
}
static inline int nova_calc_checksum(u8 *data, int n)
{
u16 crc = 0;
crc = crc16(~0, (__u8 *)data + sizeof(__le16), n - sizeof(__le16));
if (*((__le16 *)data) == cpu_to_le16(crc))
return 0;
else
return 1;
}
struct nova_range_node_lowhigh {
__le64 range_low;
__le64 range_high;
};
#define RANGENODE_PER_PAGE 254
struct nova_range_node {
struct rb_node node;
unsigned long range_low;
unsigned long range_high;
};
struct nova_inode_info_header {
struct radix_tree_root tree; /* Dir name entry tree root */
struct radix_tree_root cache_tree; /* Mmap cache tree root */
unsigned short i_mode; /* Dir or file? */
unsigned long log_pages; /* Num of log pages */
unsigned long i_size;
unsigned long ino;
unsigned long pi_addr;
unsigned long mmap_pages; /* Num of mmap pages */
unsigned long low_dirty; /* Mmap dirty low range */
unsigned long high_dirty; /* Mmap dirty high range */
unsigned long valid_bytes; /* For thorough GC */
u64 last_setattr; /* Last setattr entry */
u64 last_link_change; /* Last link change entry */
};
struct nova_inode_info {
struct nova_inode_info_header header;
struct inode vfs_inode;
};
enum bm_type {
BM_4K = 0,
BM_2M,
BM_1G,
};
struct single_scan_bm {
unsigned long bitmap_size;
unsigned long *bitmap;
};
struct scan_bitmap {
struct single_scan_bm scan_bm_4K;
struct single_scan_bm scan_bm_2M;
struct single_scan_bm scan_bm_1G;
};
struct free_list {
spinlock_t s_lock;
struct rb_root block_free_tree;
struct nova_range_node *first_node;
unsigned long block_start;
unsigned long block_end;
unsigned long num_free_blocks;
unsigned long num_blocknode;
struct nova_range_node *p_node;
/* Statistics */
unsigned long alloc_log_count;
unsigned long alloc_data_count;
unsigned long free_log_count;
unsigned long free_data_count;
unsigned long alloc_log_pages;
unsigned long alloc_data_pages;
unsigned long freed_log_pages;
unsigned long freed_data_pages;
u64 padding[8]; /* Cache line break */
};
/*
* The first block contains super blocks and reserved inodes;
* The second block contains pointers to journal pages.
* The third block contains pointers to inode tables.
*/
#define RESERVED_BLOCKS 3
struct inode_map {
struct mutex inode_table_mutex;
struct rb_root inode_inuse_tree;
unsigned long num_range_node_inode;
struct nova_range_node *first_inode_range;
int allocated;
int freed;
};
/* wwb add 2020-7-2 */
/* 增加一个磨损轻微的list,目前占时不按照cpu来进行划分 */
struct slight_page
{
unsigned long block_num;
struct slight_page * next;
};
struct slight_page_head
{
int count;
struct slight_page * page_head;
struct slight_page * page_tail;
};
/* end 2020-7-2 */
/*
* NOVA super-block data in memory
*/
struct nova_sb_info {
struct super_block *sb;
struct block_device *s_bdev;
/*
* base physical and virtual address of NOVA (which is also
* the pointer to the super block)
*/
phys_addr_t phys_addr;
void *virt_addr;
unsigned long num_blocks;
/*
* Backing store option:
* 1 = no load, 2 = no store,
* else do both
*/
unsigned int nova_backing_option;
/* Mount options */
unsigned long bpi;
unsigned long num_inodes;
unsigned long blocksize;
unsigned long initsize;
unsigned long s_mount_opt;
kuid_t uid; /* Mount uid for root directory */
kgid_t gid; /* Mount gid for root directory */
umode_t mode; /* Mount mode for root directory */
atomic_t next_generation;
/* inode tracking */
unsigned long s_inodes_used_count;
unsigned long reserved_blocks;
struct mutex s_lock; /* protects the SB's buffer-head */
int cpus;
struct proc_dir_entry *s_proc;
/* ZEROED page for cache page initialized */
void *zeroed_page;
/* Per-CPU journal lock */
spinlock_t *journal_locks;
/* Per-CPU inode map */
struct inode_map *inode_maps;
/* Decide new inode map id */
unsigned long map_id;
/* Per-CPU free block list */
struct free_list *free_lists;
/* wwb add 2020-6-29 */
unsigned long ** inode_page_mapping_tables;
struct mutex * inode_page_mapping_table_locks;
/* 2020-7-5 增加双指针,用来给每个inode page上锁*/
//struct mutex ** inode_page_locks;
/* end 2020-6-29 */
/* Shared free block list */
unsigned long per_list_blocks;
struct free_list shared_free_list;
};
static inline struct nova_sb_info *NOVA_SB(struct super_block *sb)
{
return sb->s_fs_info;
}
static inline struct nova_inode_info *NOVA_I(struct inode *inode)
{
return container_of(inode, struct nova_inode_info, vfs_inode);
}
/* If this is part of a read-modify-write of the super block,
* nova_memunlock_super() before calling! */
static inline struct nova_super_block *nova_get_super(struct super_block *sb)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
return (struct nova_super_block *)sbi->virt_addr;
}
static inline struct nova_super_block *nova_get_redund_super(struct super_block *sb)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
return (struct nova_super_block *)(sbi->virt_addr + NOVA_SB_SIZE);
}
/* If this is part of a read-modify-write of the block,
* nova_memunlock_block() before calling! */
static inline void *nova_get_block(struct super_block *sb, u64 block)
{
struct nova_super_block *ps = nova_get_super(sb);
return block ? ((void *)ps + block) : NULL;
}
static inline u64
nova_get_addr_off(struct nova_sb_info *sbi, void *addr)
{
NOVA_ASSERT((addr >= sbi->virt_addr) &&
(addr < (sbi->virt_addr + sbi->initsize)));
return (u64)(addr - sbi->virt_addr);
}
static inline u64
nova_get_block_off(struct super_block *sb, unsigned long blocknr,
unsigned short btype)
{
return (u64)blocknr << PAGE_SHIFT;
}
static inline
struct free_list *nova_get_free_list(struct super_block *sb, int cpu)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
if (cpu < sbi->cpus)
return &sbi->free_lists[cpu];
else
return &sbi->shared_free_list;
}
/* wwb add 2020-6-29 */
/* 双指针重写,这个是一样的,只是返回了数组头 */
static inline
unsigned long *nova_get_inode_page_mapping_table(struct super_block *sb, int cpu)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
if (cpu < sbi->cpus)
return (unsigned long *)(sbi->inode_page_mapping_tables[cpu]);
else
{
printk("WWB: cant get the inode_page_mapping_table \n");
return NULL;
}
}
/* 2020-7-5 获取inode page lock */
static inline
struct mutex *nova_get_inode_page_lock(int cpu)
{
if(cpu < 40)
return (struct mutex *)(inode_page_locks[cpu]);
else
printk("cpuid overflow \n");
}
static inline
struct mutex *nova_get_specific_inode_page_lock(u64 ino)
{
struct mutex *inode_page_lock;
unsigned int page_count;
unsigned int cpuid = ino % 40;
unsigned int internal_ino = ino / 40;
page_count = internal_ino >> 5;
if (cpuid < 40)
{
inode_page_lock = inode_page_locks[cpuid];
inode_page_lock = &inode_page_lock[page_count];
return inode_page_lock;
}
else
{
printk("WWB: cant get the inode_inode_page_lock \n");
return NULL;
}
}
/* end 2020-6-29 */
#if 0
/* wwb add 2020-6-29 */
static inline
unsigned long *nova_get_inode_page_mapping_table(struct super_block *sb, int cpu)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
if (cpu < sbi->cpus)
return &sbi->inode_page_mapping_tables[cpu];
else
{
printk("WWB: cant get the inode_page_mapping_table \n");
return NULL;
}
}
/* end 2020-6-29 */
#endif
struct ptr_pair {
__le64 journal_head;
__le64 journal_tail;
};
static inline
struct ptr_pair *nova_get_journal_pointers(struct super_block *sb, int cpu)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
if (cpu >= sbi->cpus)
return NULL;
return (struct ptr_pair *)((char *)nova_get_block(sb,
NOVA_DEF_BLOCK_SIZE_4K) + cpu * CACHELINE_SIZE);
}
struct inode_table {
__le64 log_head;
};
static inline
struct inode_table *nova_get_inode_table(struct super_block *sb, int cpu)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
if (cpu >= sbi->cpus)
return NULL;
return (struct inode_table *)((char *)nova_get_block(sb,
NOVA_DEF_BLOCK_SIZE_4K * 2) + cpu * CACHELINE_SIZE);
}
// BKDR String Hash Function
static inline unsigned long BKDRHash(const char *str, int length)
{
unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
unsigned long hash = 0;
int i;
for (i = 0; i < length; i++) {
hash = hash * seed + (*str++);
}
return hash;
}
/* uses CPU instructions to atomically write up to 8 bytes */
static inline void nova_memcpy_atomic (void *dst, const void *src, u8 size)
{
switch (size) {
case 1: {
volatile u8 *daddr = dst;
const u8 *saddr = src;
*daddr = *saddr;
break;
}
case 2: {
volatile __le16 *daddr = dst;
const u16 *saddr = src;
*daddr = cpu_to_le16(*saddr);
break;
}
case 4: {
volatile __le32 *daddr = dst;
const u32 *saddr = src;
*daddr = cpu_to_le32(*saddr);
break;
}
case 8: {
volatile __le64 *daddr = dst;
const u64 *saddr = src;
*daddr = cpu_to_le64(*saddr);
break;
}
default:
nova_dbg("error: memcpy_atomic called with %d bytes\n",
size);
//BUG();
}
}
static inline int memcpy_to_pmem_nocache(void *dst, const void *src,
unsigned int size)
{
int ret;
ret = __copy_from_user_inatomic_nocache(dst, src, size);
return ret;
}
/* assumes the length to be 4-byte aligned */
static inline void memset_nt(void *dest, uint32_t dword, size_t length)
{
uint64_t dummy1, dummy2;
uint64_t qword = ((uint64_t)dword << 32) | dword;
asm volatile ("movl %%edx,%%ecx\n"
"andl $63,%%edx\n"
"shrl $6,%%ecx\n"
"jz 9f\n"
"1: movnti %%rax,(%%rdi)\n"
"2: movnti %%rax,1*8(%%rdi)\n"
"3: movnti %%rax,2*8(%%rdi)\n"
"4: movnti %%rax,3*8(%%rdi)\n"
"5: movnti %%rax,4*8(%%rdi)\n"
"8: movnti %%rax,5*8(%%rdi)\n"
"7: movnti %%rax,6*8(%%rdi)\n"
"8: movnti %%rax,7*8(%%rdi)\n"
"leaq 64(%%rdi),%%rdi\n"
"decl %%ecx\n"
"jnz 1b\n"
"9: movl %%edx,%%ecx\n"
"andl $7,%%edx\n"
"shrl $3,%%ecx\n"
"jz 11f\n"
"10: movnti %%rax,(%%rdi)\n"
"leaq 8(%%rdi),%%rdi\n"
"decl %%ecx\n"
"jnz 10b\n"
"11: movl %%edx,%%ecx\n"
"shrl $2,%%ecx\n"
"jz 12f\n"
"movnti %%eax,(%%rdi)\n"
"12:\n"
: "=D"(dummy1), "=d" (dummy2) : "D" (dest), "a" (qword), "d" (length) : "memory", "rcx");
}
static inline struct nova_file_write_entry *
nova_get_write_entry(struct super_block *sb,
struct nova_inode_info *si, unsigned long blocknr)
{
struct nova_inode_info_header *sih = &si->header;
struct nova_file_write_entry *entry;
entry = radix_tree_lookup(&sih->tree, blocknr);
return entry;
}
void nova_print_curr_log_page(struct super_block *sb, u64 curr);
void nova_print_nova_log(struct super_block *sb,
struct nova_inode_info_header *sih, struct nova_inode *pi);
int nova_get_nova_log_pages(struct super_block *sb,
struct nova_inode_info_header *sih, struct nova_inode *pi);
void nova_print_nova_log_pages(struct super_block *sb,
struct nova_inode_info_header *sih, struct nova_inode *pi);
static inline unsigned long get_nvmm(struct super_block *sb,
struct nova_inode_info_header *sih,
struct nova_file_write_entry *data, unsigned long pgoff)
{
if (data->pgoff > pgoff || (unsigned long)data->pgoff +
(unsigned long)data->num_pages <= pgoff) {
struct nova_sb_info *sbi = NOVA_SB(sb);
struct nova_inode *pi;
u64 curr;
curr = nova_get_addr_off(sbi, data);
nova_dbg("WWB Entry ERROR: inode %lu, curr 0x%llx, pgoff %lu, "
"entry pgoff %llu, num %u\n", sih->ino,
curr, pgoff, data->pgoff, data->num_pages);
/* wwb add 2020-6-30 */
//pi = nova_get_block(sb, sih->pi_addr);
pi = (struct nova_inode *)nova_get_inode_by_ino_or_pi_addr(sb, sih->ino, sih->pi_addr);
/* end 2020-6-30 */
nova_print_nova_log_pages(sb, sih, pi);
nova_print_nova_log(sb, sih, pi);
NOVA_ASSERT(0);
}
return (unsigned long)(data->block >> PAGE_SHIFT) + pgoff
- data->pgoff;
}
static inline u64 nova_find_nvmm_block(struct super_block *sb,
struct nova_inode_info *si, struct nova_file_write_entry *entry,
unsigned long blocknr)
{
unsigned long nvmm;
if (!entry) {
entry = nova_get_write_entry(sb, si, blocknr);
if (!entry)
return 0;
}
nvmm = get_nvmm(sb, &si->header, entry, blocknr);
return nvmm << PAGE_SHIFT;
}
static inline unsigned long nova_get_cache_addr(struct super_block *sb,
struct nova_inode_info *si, unsigned long blocknr)
{
struct nova_inode_info_header *sih = &si->header;
unsigned long addr;
addr = (unsigned long)radix_tree_lookup(&sih->cache_tree, blocknr);
nova_dbgv("%s: inode %lu, blocknr %lu, addr 0x%lx\n",
__func__, sih->ino, blocknr, addr);
return addr;
}
static inline unsigned int nova_inode_blk_shift (struct nova_inode *pi)
{
return blk_type_to_shift[pi->i_blk_type];
}
static inline uint32_t nova_inode_blk_size (struct nova_inode *pi)
{
return blk_type_to_size[pi->i_blk_type];
}
/*
* ROOT_INO: Start from NOVA_SB_SIZE * 2
*/
static inline struct nova_inode *nova_get_basic_inode(struct super_block *sb,
u64 inode_number)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
return (struct nova_inode *)(sbi->virt_addr + NOVA_SB_SIZE * 2 +
(inode_number - NOVA_ROOT_INO) * NOVA_INODE_SIZE);
}
/* If this is part of a read-modify-write of the inode metadata,
* nova_memunlock_inode() before calling! */
static inline struct nova_inode *nova_get_inode_by_ino(struct super_block *sb,
u64 ino)
{
if (ino == 0 || ino >= NOVA_NORMAL_INODE_START)
return NULL;
return nova_get_basic_inode(sb, ino);
}
#if 0
// 由于pi_addr的原因,这里重写nova_get_inode,但是速度会降低
static inline struct nova_inode *nova_get_inode(struct super_block *sb,
struct inode *inode)
{
struct nova_inode_info *si = NOVA_I(inode);
struct nova_inode_info_header *sih = &si->header;
return (struct nova_inode *)nova_get_block(sb, sih->pi_addr);
}
#endif
static inline unsigned long
nova_get_numblocks(unsigned short btype)
{
unsigned long num_blocks;
if (btype == NOVA_BLOCK_TYPE_4K) {
num_blocks = 1;
} else if (btype == NOVA_BLOCK_TYPE_2M) {
num_blocks = 512;
} else {
//btype == NOVA_BLOCK_TYPE_1G
num_blocks = 0x40000;
}
return num_blocks;
}
static inline unsigned long
nova_get_blocknr(struct super_block *sb, u64 block, unsigned short btype)
{
return block >> PAGE_SHIFT;
}
static inline unsigned long nova_get_pfn(struct super_block *sb, u64 block)
{
return (NOVA_SB(sb)->phys_addr + block) >> PAGE_SHIFT;
}
static inline int nova_is_mounting(struct super_block *sb)
{
struct nova_sb_info *sbi = (struct nova_sb_info *)sb->s_fs_info;
return sbi->s_mount_opt & NOVA_MOUNT_MOUNTING;
}
static inline void check_eof_blocks(struct super_block *sb,
struct nova_inode *pi, loff_t size)
{
if ((pi->i_flags & cpu_to_le32(NOVA_EOFBLOCKS_FL)) &&
(size + sb->s_blocksize) > (le64_to_cpu(pi->i_blocks)
<< sb->s_blocksize_bits))
pi->i_flags &= cpu_to_le32(~NOVA_EOFBLOCKS_FL);
}
enum nova_new_inode_type {
TYPE_CREATE = 0,
TYPE_MKNOD,
TYPE_SYMLINK,
TYPE_MKDIR
};
static inline u64 next_log_page(struct super_block *sb, u64 curr_p)
{
void *curr_addr = nova_get_block(sb, curr_p);
unsigned long page_tail = ((unsigned long)curr_addr & ~INVALID_MASK)
+ LAST_ENTRY;
return ((struct nova_inode_page_tail *)page_tail)->next_page;
}
static inline void nova_set_next_page_address(struct super_block *sb,
struct nova_inode_log_page *curr_page, u64 next_page, int fence)
{
curr_page->page_tail.next_page = next_page;
nova_flush_buffer(&curr_page->page_tail,
sizeof(struct nova_inode_page_tail), 0);
if (fence)
PERSISTENT_BARRIER();
}
#define CACHE_ALIGN(p) ((p) & ~(CACHELINE_SIZE - 1))
static inline bool is_last_entry(u64 curr_p, size_t size)
{
unsigned int entry_end;
entry_end = ENTRY_LOC(curr_p) + size;
return entry_end > LAST_ENTRY;
}
static inline bool goto_next_page(struct super_block *sb, u64 curr_p)
{
void *addr;
u8 type;
/* Each kind of entry takes at least 32 bytes */
if (ENTRY_LOC(curr_p) + 32 > LAST_ENTRY)