-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.c
1810 lines (1594 loc) · 52 KB
/
table.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
/*
* Deduplication metadata table.
*
* Copyright (c) 2020-2023 Jiansheng Qiu <[email protected]>
*
* 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/atomic.h>
#include <linux/string.h>
#include "nova.h"
#include "faststr.h"
#include "arithmetic.h"
#include "joinable.h"
#include "rhashtable-ext.h"
#include "uaccess-ext.h"
#include "entry.h"
// #define static _Static_assert(1, "2333");
// static inline void
// assign_pmm_entry_to_blocknr(struct light_dedup_meta *meta,
// unsigned long blocknr, struct nova_pmm_entry *pentry,
// struct nova_write_para_normal *wp)
// {
// struct nova_sb_info *sbi = light_dedup_meta_to_sbi(meta);
// __le64 *offset = meta->entry_allocator.map_blocknr_to_pentry + blocknr;
// *offset = nova_get_addr_off(sbi, pentry);
// if (!in_the_same_cacheline(offset, wp->dirty_map_blocknr_to_pentry) &&
// wp->dirty_map_blocknr_to_pentry != NULL)
// {
// nova_flush_cacheline(wp->dirty_map_blocknr_to_pentry, false);
// }
// wp->dirty_map_blocknr_to_pentry = offset;
// }
// static inline void
// clear_pmm_entry_at_blocknr(struct light_dedup_meta *meta,
// unsigned long blocknr)
// {
// struct nova_sb_info *sbi = light_dedup_meta_to_sbi(meta);
// __le64 *offset = meta->entry_allocator.map_blocknr_to_pentry + blocknr;
// BUG_ON(*offset == 0);
// nova_unlock_write_flush(sbi, offset, 0, false);
// }
// static inline struct nova_pmm_entry *
// blocknr_pmm_entry(struct light_dedup_meta *meta, unsigned long blocknr)
// {
// return nova_get_block(meta->sblock,
// le64_to_cpu(
// meta->entry_allocator.map_blocknr_to_pentry[blocknr]));
// }
// PBN to FP mapping, for fast deletion
struct nova_revmap_entry {
// struct rb_node node;
__le64 blocknr;
struct nova_fp fp;
};
DECLARE_PER_CPU(struct nova_rht_entry *, last_accessed_fpentry_per_cpu);
DEFINE_STATIC_SRCU(srcu);
int light_dedup_srcu_read_lock(void) {
return srcu_read_lock(&srcu);
}
void light_dedup_srcu_read_unlock(int idx) {
srcu_read_unlock(&srcu, idx);
}
static inline struct nova_revmap_entry* revmap_entry_alloc(
struct light_dedup_meta *meta)
{
return kmem_cache_alloc(meta->revmap_entry_cache, GFP_ATOMIC);
}
static void nova_revmap_entry_free(struct light_dedup_meta *meta, void *entry)
{
kmem_cache_free(meta->revmap_entry_cache, entry);
}
static void nova_insert_revmap_entry(struct light_dedup_meta *meta,
struct nova_revmap_entry *entry)
{
xa_store(&meta->revmap, entry->blocknr, entry, GFP_ATOMIC);
}
static void nova_delete_revmap_entry(struct light_dedup_meta *meta,
struct nova_revmap_entry *entry)
{
xa_erase(&meta->revmap, entry->blocknr);
}
static struct nova_revmap_entry *nova_search_revmap_entry(
struct light_dedup_meta *meta, unsigned long blocknr)
{
return xa_load(&meta->revmap, blocknr);
}
static u32 nova_rht_entry_key_hashfn(const void *data, u32 len, u32 seed)
{
struct nova_fp *fp = (struct nova_fp *)data;
return fp->index;
}
static u32 nova_rht_entry_hashfn(const void *data, u32 len, u32 seed)
{
struct nova_rht_entry *entry = (struct nova_rht_entry *)data;
return entry->fp.index;
}
static int nova_rht_key_entry_cmp(
struct rhashtable_compare_arg *arg,
const void *obj)
{
const struct nova_fp *fp = (const struct nova_fp *)arg->key;
struct nova_rht_entry *entry = (struct nova_rht_entry *)obj;
// printk("%s: %llx, %llx", __func__, fp->value, entry->fp.value);
return fp->value != entry->fp.value;
}
const struct rhashtable_params nova_rht_params = {
.key_len = sizeof(struct nova_fp),
.head_offset = offsetof(struct nova_rht_entry, node),
.automatic_shrinking = true,
.hashfn = nova_rht_entry_key_hashfn,
.obj_hashfn = nova_rht_entry_hashfn,
.obj_cmpfn = nova_rht_key_entry_cmp,
};
struct rht_entry_free_task {
struct rcu_head head;
struct light_dedup_meta *meta;
struct nova_rht_entry *pentry;
};
static inline struct nova_rht_entry* rht_entry_alloc(
struct light_dedup_meta *meta)
{
struct nova_rht_entry* entry = kmem_cache_zalloc(meta->rht_entry_cache, GFP_ATOMIC);
// ensure that we can use the lowest 3 bits of next_hint
BUG_ON(((u64)entry & TRUST_DEGREE_MASK) != 0);
return entry;
}
static void nova_rht_entry_free(void *entry, void *arg)
{
struct kmem_cache *c = (struct kmem_cache *)arg;
kmem_cache_free(c, entry);
}
static void rcu_rht_entry_free_only_entry(struct rcu_head *head)
{
struct rht_entry_free_task *task =
container_of(head, struct rht_entry_free_task, head);
struct light_dedup_meta *meta = task->meta;
// might be added back
if (atomic_read(&task->pentry->num_holders) == 0) {
// write_lock(&meta->worker_lock);
// while(atomic64_read(&meta->rcu_unprotected_worker_num) > 0) {
// write_unlock(&meta->worker_lock);
// cpu_relax();
// write_lock(&meta->worker_lock);
// }
// write_unlock(&meta->worker_lock);
nova_rht_entry_free(task->pentry, meta->rht_entry_cache);
}
kfree(task);
}
/**
* @brief Decrease the reference count of a GLT entry, avoiding accessing the freed entry.
*
* @param meta GogetaFS runtime structure
* @param pentry the GLT entry to be decreased
*/
inline void decr_holders(struct light_dedup_meta *meta, struct nova_rht_entry *pentry)
{
if (atomic_dec_and_test(&pentry->num_holders)) {
struct rht_entry_free_task *task;
nova_dbgv("Block %lu has no holder now\n", pentry->blocknr);
// nova_rht_entry_free(pentry, meta->rht_entry_cache);
task = kmalloc(sizeof(struct rht_entry_free_task), GFP_ATOMIC);
if (task) {
task->meta = meta;
task->pentry = pentry;
// call_rcu(&task->head, rcu_rht_entry_free_only_entry);
// Call in sleeping context
call_srcu(&srcu, &task->head, rcu_rht_entry_free_only_entry);
} else {
BUG_ON(1);
}
return;
}
nova_dbgv("%s: Block %lu has %d holders now\n",
__func__, pentry->blocknr, atomic_read(&pentry->num_holders));
}
/**
* @brief Increase the reference count of a GLT entry.
*
* @param pentry the GLT entry to be increased
*/
inline void incr_holders(struct nova_rht_entry *pentry)
{
atomic_inc(&pentry->num_holders);
nova_dbgv("%s: Block %lu has %d holders now\n",
__func__, pentry->blocknr, atomic_read(&pentry->num_holders));
}
// struct pentry_free_task {
// struct rcu_head head;
// struct entry_allocator *allocator;
// struct nova_pmm_entry *pentry;
// };
// static void __rcu_pentry_free(struct entry_allocator *allocator,
// struct nova_pmm_entry *pentry)
// {
// struct light_dedup_meta *meta =
// entry_allocator_to_light_dedup_meta(allocator);
// struct super_block *sb = meta->sblock;
// unsigned long blocknr = nova_pmm_entry_blocknr(pentry);
// BUG_ON(blocknr == 0);
// clear_pmm_entry_at_blocknr(meta, blocknr);
// nova_free_data_block(sb, blocknr);
// nova_free_entry(allocator, pentry);
// }
static void __rcu_rht_entry_free(struct light_dedup_meta *meta,
struct nova_rht_entry *pentry)
{
struct kmem_cache *rht_entry_cache = meta->rht_entry_cache;
uint64_t hint = le64_to_cpu(atomic64_read(&pentry->next_hint));
u64 addr = hint & HINT_ADDR_MASK;
struct nova_rht_entry *next_pentry = (struct nova_rht_entry *)addr;
nova_free_data_block(meta->sblock, pentry->blocknr);
if (addr) {
// do not hold the next entry
decr_holders(meta, next_pentry);
}
// do not hold the entry itself
decr_holders(meta, pentry);
}
static void rcu_rht_entry_free(struct rcu_head *head)
{
struct rht_entry_free_task *task =
container_of(head, struct rht_entry_free_task, head);
__rcu_rht_entry_free(task->meta, task->pentry);
kfree(task);
}
// static inline void new_dirty_fpentry(struct nova_rht_entry *last_pentries[2],
// struct nova_rht_entry *pentry)
// {
// last_pentries[1] = last_pentries[0];
// last_pentries[0] = pentry;
// }
static void free_rht_entry(
struct light_dedup_meta *meta,
struct nova_rht_entry *pentry)
{
struct rht_entry_free_task *task;
struct nova_revmap_entry *rev_entry;
// Remove the entry first to make it invisible to other threads.
int ret = rhashtable_remove_fast(&meta->rht, &pentry->node, nova_rht_params);
BUG_ON(ret < 0);
// printk("Block %lu removed from rhashtable\n",
// nova_pmm_entry_blocknr(entry->pentry));
// nova_pmm_entry_mark_to_be_freed(entry->pentry);
// NOTE: Do not delay to rcu.
spin_lock(&meta->revmap_lock);
rev_entry = nova_search_revmap_entry(meta, pentry->blocknr);
nova_delete_revmap_entry(meta, rev_entry);
nova_revmap_entry_free(meta, rev_entry);
spin_unlock(&meta->revmap_lock);
// task = kmalloc(sizeof(struct rht_entry_free_task), GFP_ATOMIC);
// if (task) {
// task->meta = meta;
// task->pentry = pentry;
// call_rcu(&task->head, rcu_rht_entry_free);
// } else {
// BUG_ON(1);
// // printk(KERN_ERR "%s: Fail to allocate task\n", __func__);
// // synchronize_rcu();
// // __rcu_rht_entry_free(&meta->entry_allocator, entry);
// }
__rcu_rht_entry_free(meta, pentry);
}
static void print(const char *addr) {
int i;
for (i = 0; i < 4096; ++i) {
printk(KERN_CONT "%02x ", addr[i] & 0xff);
}
printk("\n");
}
static int alloc_and_fill_block(
struct super_block *sb,
struct nova_write_para_normal *wp)
{
void *xmem;
// unsigned long irq_flags = 0;
INIT_TIMING(memcpy_time);
wp->blocknr = nova_new_data_block(sb);
if (wp->blocknr == 0)
return -ENOSPC;
// printk("%s: Block %ld allocated", __func__, wp->blocknr);
xmem = nova_blocknr_to_addr(sb, wp->blocknr);
// nova_memunlock_block(sb, xmem, &irq_flags);
NOVA_START_TIMING(memcpy_data_block_t, memcpy_time);
// memcpy_flushcache((char *)xmem, wp->addr, 4096);
if (wp->kbytes < PAGE_SIZE || wp->kofs != 0)
memcpy_flushcache((char *)xmem, wp->addr, PAGE_SIZE);
else
memcpy_to_pmem_nocache((char *)xmem, wp->ubuf, PAGE_SIZE);
NOVA_END_TIMING(memcpy_data_block_t, memcpy_time);
// nova_memlock_block(sb, xmem, &irq_flags);
return NO_DEDUP;
}
#if 0
static int rewrite_block(
struct super_block *sb,
struct nova_write_para_normal *__wp)
{
struct nova_write_para_rewrite *wp = (struct nova_write_para_rewrite *)__wp;
void *xmem;
unsigned long irq_flags = 0;
INIT_TIMING(memcpy_time);
xmem = nova_blocknr_to_addr(sb, wp->normal.blocknr);
NOVA_START_TIMING(memcpy_data_block_t, memcpy_time);
nova_memunlock_range(sb, xmem + wp->offset, wp->len, &irq_flags);
memcpy_flushcache((char *)xmem + wp->offset, (const char *)wp->normal.addr + wp->offset, wp->len);
nova_memlock_range(sb, xmem + wp->offset, wp->len, &irq_flags);
NOVA_END_TIMING(memcpy_data_block_t, memcpy_time);
return 0;
}
#endif
static int handle_new_block(
struct light_dedup_meta *meta,
struct nova_write_para_normal *wp,
int get_new_block(struct super_block *, struct nova_write_para_normal *))
{
struct super_block *sb = meta->sblock;
struct nova_rht_entry *pentry;
struct nova_revmap_entry *rev_entry;
struct nova_fp fp = wp->base.fp;
int cpu, idx;
int64_t refcount;
int ret;
INIT_TIMING(time);
INIT_TIMING(index_insert_new_entry_time);
NOVA_START_TIMING(handle_new_blk_t, time);
pentry = rht_entry_alloc(meta);
if (pentry == NULL) {
ret = -ENOMEM;
goto fail0;
}
rev_entry = revmap_entry_alloc(meta);
if (rev_entry == NULL) {
ret = -ENOMEM;
goto fail1;
}
ret = get_new_block(sb, wp);
if (ret < 0) {
goto fail2;
}
light_dedup_init_entry(pentry, fp, wp->blocknr);
rev_entry->blocknr = wp->blocknr;
rev_entry->fp = fp;
// insert revmap unless the entry is inserted into rhashtable
spin_lock(&meta->revmap_lock);
nova_insert_revmap_entry(meta, rev_entry);
spin_unlock(&meta->revmap_lock);
NOVA_START_TIMING(index_insert_new_entry_t,
index_insert_new_entry_time);
ret = rhashtable_lookup_insert_key(&meta->rht, &fp, &pentry->node,
nova_rht_params);
NOVA_END_TIMING(index_insert_new_entry_t, index_insert_new_entry_time);
if (ret < 0) {
printk("Block %lu with fp %llx fail to insert into rhashtable "
"with error code %d\n", wp->blocknr, fp.value, ret);
spin_lock(&meta->revmap_lock);
nova_delete_revmap_entry(meta, rev_entry);
spin_unlock(&meta->revmap_lock);
goto fail2;
}
refcount = atomic64_cmpxchg(&pentry->refcount, 0, 1);
BUG_ON(refcount != 0);
wp->last_accessed = pentry;
NOVA_END_TIMING(handle_new_blk_t, time);
return 0;
fail2:
nova_revmap_entry_free(meta, rev_entry);
fail1:
nova_rht_entry_free(pentry, meta->rht_entry_cache);
fail0:
NOVA_END_TIMING(handle_new_blk_t, time);
return ret;
}
// True: Not equal. False: Equal
static bool cmp_content(struct super_block *sb, unsigned long blocknr, const void *addr) {
INIT_TIMING(memcmp_time);
const char *content;
size_t i;
bool res;
NOVA_START_TIMING(memcmp_t, memcmp_time);
content = nova_blocknr_to_addr(sb, blocknr);
for (i = 0; i < 16; ++i)
prefetcht0(content + i * 256);
for (i = 0; i < 16; ++i) {
prefetcht0(content + i * 256 + 64);
prefetcht0(content + i * 256 + 64 * 2);
prefetcht0(content + i * 256 + 64 * 3);
}
res = cmp64((const uint64_t *)content, addr);
NOVA_END_TIMING(memcmp_t, memcmp_time);
// if (res) {
// print(content);
// nova_dbgv("\n");
// print(addr);
// }
return res;
}
static int incr_ref(struct light_dedup_meta *meta,
struct nova_write_para_normal *wp,
int (*get_new_block)(struct super_block *,
struct nova_write_para_normal *))
{
struct super_block *sb = meta->sblock;
struct rhashtable *rht = &meta->rht;
struct nova_rht_entry *pentry;
unsigned long blocknr;
// unsigned long irq_flags = 0;
int ret, idx = 0;
INIT_TIMING(index_lookup_time);
retry:
rcu_read_lock();
NOVA_START_TIMING(index_lookup_t, index_lookup_time);
pentry = rhashtable_lookup(rht, &wp->base.fp, nova_rht_params);
NOVA_END_TIMING(index_lookup_t, index_lookup_time);
// We have to hold the read lock because if it is a hash collision,
// then the entry, pentry, and blocknr could be freed by another thread.
if (pentry == NULL) {
rcu_read_unlock();
// printk("Block with fp %llx not found in rhashtable %p\n",
// wp->base.fp.value, rht);
ret = handle_new_block(meta, wp, get_new_block);
if (ret == -EEXIST)
goto retry;
wp->base.refcount = 1;
return NO_DEDUP;
}
blocknr = pentry->blocknr;
BUG_ON(blocknr == 0);
if (cmp_content(sb, blocknr, wp->addr)) {
rcu_read_unlock();
wp->last_accessed = NULL;
nova_dbgv("fp:%llx rentry.fp:%llx",wp->base.fp.value, pentry->fp.value);
nova_dbg("Collision, just write it.");
wp->base.refcount = 0;
return get_new_block(sb, wp);
// const void *content = nova_get_block(sb, nova_sb_blocknr_to_addr(sb, le64_to_cpu(leaf->blocknr), NOVA_BLOCK_TYPE_4K));
// printk("First 8 bytes of existed_entry: %llx, chunk_id = %llx, fingerprint = %llx %llx %llx %llx\nFirst 8 bytes of incoming block: %llx, fingerprint = %llx %llx %llx %llx\n",
// *(uint64_t *)content, leaf->blocknr, leaf->fp_strong.u64s[0], leaf->fp_strong.u64s[1], leaf->fp_strong.u64s[2], leaf->fp_strong.u64s[3],
// *(uint64_t *)addr, entry->fp_strong.u64s[0], entry->fp_strong.u64s[1], entry->fp_strong.u64s[2], entry->fp_strong.u64s[3]);
}
wp->blocknr = blocknr;// retrieval block info
// nova_memunlock_range(sb, &pentry->refcount,
// sizeof(pentry->refcount), &irq_flags);
wp->base.refcount = atomic64_fetch_add_unless(&pentry->refcount, 1, 0);
// nova_memlock_range(sb, &pentry->refcount,
// sizeof(pentry->refcount), &irq_flags);
rcu_read_unlock();
if (wp->base.refcount == 0)
return -EAGAIN;
wp->base.refcount += 1;
// new_dirty_fpentry(wp->last_ref_entries, pentry);
wp->last_accessed = pentry;
// printk("Block %lu (fpentry %p) has refcount %lld now\n",
// wp->blocknr, pentry, wp->base.refcount);
return DEDUP_SUCCESS;
}
static int incr_ref_normal(struct light_dedup_meta *meta,
struct nova_write_para_normal *wp)
{
return incr_ref(meta, wp, alloc_and_fill_block);
}
static int light_dedup_incr_ref_atomic(struct light_dedup_meta *meta, unsigned long kofs, unsigned long kbytes,
const void *addr, const void * __user ubuf, struct nova_write_para_normal *wp)
{
int ret;
INIT_TIMING(incr_ref_time);
NOVA_START_TIMING(incr_ref_t, incr_ref_time);
BUG_ON(nova_fp_calc(&meta->fp_ctx, addr, &wp->base.fp));
wp->kofs = kofs;
wp->kbytes = kbytes;
wp->addr = addr;
wp->ubuf = ubuf;
ret = incr_ref_normal(meta, wp);
NOVA_END_TIMING(incr_ref_t, incr_ref_time);
return ret;
}
/**
* @brief Deduplicate one block of data.
*
* @param meta GogetaFS runtime structure
* @param kofs The offset from the kernel buffer
* @param kbytes The bytes to write
* @param addr kernel buffer address
* @param ubuf user buffer address
* @param wp write parameters
* @return int 0 deduplication success, 1 no deduplication, others failed
*/
int light_dedup_incr_ref(struct light_dedup_meta *meta, unsigned long kofs, unsigned long kbytes,
const void* addr, const void* __user ubuf, struct nova_write_para_normal *wp)
{
int ret;
while (1) {
ret = light_dedup_incr_ref_atomic(meta, kofs, kbytes, addr, ubuf, wp);
if (likely(ret != -EAGAIN))
break;
schedule();
};
return ret;
}
static void free_pentry(struct light_dedup_meta *meta,
struct nova_rht_entry *pentry)
{
// struct rhashtable *rht = &meta->rht;
// INIT_TIMING(index_lookup_time);
// rcu_read_lock();
// NOVA_START_TIMING(index_lookup_t, index_lookup_time);
// entry = rhashtable_lookup(rht, &pentry->fp, nova_rht_params);
// NOVA_END_TIMING(index_lookup_t, index_lookup_time);
// BUG_ON(entry == NULL);
// BUG_ON(entry->pentry != pentry);
// rcu_read_unlock();
free_rht_entry(meta, pentry);
}
static int64_t decr_ref(struct light_dedup_meta *meta,
struct nova_rht_entry *pentry)
{
unsigned long blocknr;
int64_t refcount;
// blocknr = nova_pmm_entry_blocknr(pentry);
// BUG_ON(blocknr == 0);
// nova_memunlock_range(sb, &pentry->refcount, sizeof(pentry->refcount),
// &irq_flags);
refcount = atomic64_add_return(-1, &pentry->refcount);
// nova_memlock_range(sb, &pentry->refcount, sizeof(pentry->refcount),
// &irq_flags);
BUG_ON(refcount < 0);
if (refcount == 0) {
// Now only we can free the entry,
// because there are no any other deleter.
free_pentry(meta, pentry);
}
return refcount;
}
/**
* @brief Try to remove a duplicated block. If the refcount is 1, then remove it.
*
* @param meta GogetaFS runtime structure
* @param blocknr the blocknr to be removed
* @param last_pentry the last accessed entry, pass out
*/
void light_dedup_decr_ref(struct light_dedup_meta *meta, unsigned long blocknr,
struct nova_rht_entry **last_pentry)
{
struct super_block *sb = meta->sblock;
struct rhashtable *rht = &meta->rht;
INIT_TIMING(decr_ref_time);
INIT_TIMING(index_lookup_time);
struct nova_rht_entry *pentry;
struct nova_revmap_entry *rev_entry;
int64_t refcount;
BUG_ON(blocknr == 0);
spin_lock(&meta->revmap_lock);
rev_entry = nova_search_revmap_entry(meta, blocknr);
spin_unlock(&meta->revmap_lock);
if (!rev_entry) {
nova_warn("Block without deduplication info: %lu\n", blocknr);
nova_free_data_block(sb, blocknr);
return;
}
rcu_read_lock();
NOVA_START_TIMING(index_lookup_t, index_lookup_time);
pentry = rhashtable_lookup(rht, &rev_entry->fp, nova_rht_params);
NOVA_END_TIMING(index_lookup_t, index_lookup_time);
// We have to hold the read lock because if it is a hash collision,
// then the entry could be freed by another thread.
if (!pentry) {
rcu_read_unlock();
// Collision happened. Just free it.
printk("Fingerprint %ld can not be found in the hash table.", rev_entry->fp);
BUG_ON(1);
}
// The entry won't be freed by others
// because we are referencing it.
rcu_read_unlock();
NOVA_START_TIMING(decr_ref_t, decr_ref_time);
refcount = decr_ref(meta, pentry);
NOVA_END_TIMING(decr_ref_t, decr_ref_time);
if (refcount != 0) {
*last_pentry = pentry;
}
}
// refcount-- only if refcount == 1
static int decr_ref_1(
struct light_dedup_meta *meta,
struct nova_write_para_normal *wp)
{
struct rhashtable *rht = &meta->rht;
struct nova_rht_entry *pentry;
unsigned long blocknr;
int64_t refcount;
INIT_TIMING(index_lookup_time);
rcu_read_lock();
NOVA_START_TIMING(index_lookup_t, index_lookup_time);
pentry = rhashtable_lookup(rht, &wp->base.fp, nova_rht_params);
NOVA_END_TIMING(index_lookup_t, index_lookup_time);
// We have to hold the read lock because if it is a hash collision,
// then the entry could be freed by another thread.
if (!pentry) {
rcu_read_unlock();
// Collision happened. Just free it.
printk("Block %ld can not be found in the hash table.", wp->blocknr);
wp->base.refcount = 0;
return 0;
}
blocknr = pentry->blocknr;
BUG_ON(blocknr == 0);
if (blocknr != wp->blocknr) {
rcu_read_unlock();
// Collision happened. Just free it.
printk("%s: Blocknr mismatch: blocknr = %ld, expected %ld\n",
__func__, blocknr, wp->blocknr);
wp->base.refcount = 0;
return 0;
}
// The entry won't be freed by others
// because we are referencing it.
refcount = atomic64_cmpxchg(&pentry->refcount, 1, 0);
BUG_ON(refcount == 0);
rcu_read_unlock();
if (refcount == 1) {
free_rht_entry(meta, pentry);
wp->base.refcount = 0;
return 0;
}
// refcount >= 2. So we do not decrease refcount.
wp->base.refcount = refcount;
// printk(KERN_WARNING " found at %d, ref %llu\n", leaf_index, refcount);
return 0;
}
// Deprecated
long light_dedup_decr_ref_1(struct light_dedup_meta *meta, const void *addr,
unsigned long blocknr)
{
struct nova_write_para_normal wp;
int retval;
INIT_TIMING(decr_ref_time);
BUG_ON(blocknr == 0);
BUG_ON(nova_fp_calc(&meta->fp_ctx, addr, &wp.base.fp));
wp.addr = addr;
wp.blocknr = blocknr;
NOVA_START_TIMING(decr_ref_t, decr_ref_time);
retval = decr_ref_1(meta, &wp);
NOVA_END_TIMING(decr_ref_t, decr_ref_time);
return retval < 0 ? retval : wp.base.refcount;
}
/**
* @brief Insert a GLT entry into the rhashtable. Used for recovery
*
* @param meta GogetaFS runtime structure
* @param pentry the parameter entry that holds the FP and blocknr
* @return int 0 if success, others if failed
*/
int light_dedup_insert_rht_entry(struct light_dedup_meta *meta, struct nova_rht_entry_pm *pentry)
{
struct nova_rht_entry *entry = rht_entry_alloc(meta);
int ret;
INIT_TIMING(insert_entry_time);
if (entry == NULL)
return -ENOMEM;
NOVA_START_TIMING(insert_rht_entry_t, insert_entry_time);
entry->blocknr = pentry->blocknr;
entry->fp = pentry->fp;
atomic64_set(&entry->refcount, pentry->refcount);
while (1) {
ret = rhashtable_insert_fast(&meta->rht, &entry->node,
nova_rht_params);
if (ret != -EBUSY)
break;
schedule();
};
if (ret < 0) {
printk("%s: rhashtable_insert_fast returns %d\n",
__func__, ret);
nova_rht_entry_free(entry, meta->rht_entry_cache);
}
NOVA_END_TIMING(insert_rht_entry_t, insert_entry_time);
return ret;
}
/**
* @brief Lookup a GLT entry in the rhashtable. Please hold the rcu lock outside. Used for recovery
*
* @param meta GogetaFS runtime structure
* @param pentry the entry holds the FP.
* @return struct nova_rht_entry* the GLT entry if found, NULL otherwise
*/
struct nova_rht_entry *light_dedup_lookup_rht_entry(struct light_dedup_meta *meta, struct nova_rht_entry_pm *pentry)
{
struct rhashtable *rht = &meta->rht;
struct nova_rht_entry *entry = NULL;
INIT_TIMING(lookup_entry_time);
NOVA_START_TIMING(index_lookup_t, lookup_entry_time);
entry = rhashtable_lookup(rht, &pentry->fp, nova_rht_params);
NOVA_END_TIMING(index_lookup_t, lookup_entry_time);
return entry;
}
/**
* @brief Insert a revmap entry into the revmap. The revmap maintains a mapping from blocknr to FP for fast deletion. This function is used for recovery.
*
* @param meta GogetaFS runtime structure
* @param pentry the parameter entry that holds the FP and blocknr
* @return int 0 if success, others if failed
*/
int light_dedup_insert_revmap_entry(struct light_dedup_meta *meta,
struct nova_rht_entry_pm *pentry)
{
struct nova_revmap_entry *rev_entry;
rev_entry = nova_search_revmap_entry(meta, pentry->blocknr);
if (rev_entry) {
// nova_info("%s: Block %lu already exists in revmap\n", __func__, pentry->blocknr);
BUG_ON(1);
return 0;
}
rev_entry = revmap_entry_alloc(meta);
if (rev_entry == NULL)
return -ENOMEM;
rev_entry->blocknr = pentry->blocknr;
rev_entry->fp = pentry->fp;
spin_lock(&meta->revmap_lock);
nova_insert_revmap_entry(meta, rev_entry);
spin_unlock(&meta->revmap_lock);
return 0;
}
static inline void incr_stream_trust_degree(
struct nova_write_para_continuous *wp)
{
if (wp->stream_trust_degree < TRUST_DEGREE_MAX)
wp->stream_trust_degree += 1;
}
static inline void decr_stream_trust_degree(
struct nova_write_para_continuous *wp)
{
if (wp->stream_trust_degree < TRUST_DEGREE_MIN + 2)
wp->stream_trust_degree = TRUST_DEGREE_MIN;
else
wp->stream_trust_degree -= 2;
}
static inline bool hint_trustable(uint8_t trust_degree)
{
return trust_degree >= HINT_TRUST_DEGREE_THRESHOLD;
}
// Return the original persistent hint.
static u64 __update_hint(struct light_dedup_meta *meta, atomic64_t *next_hint, u64 old_hint, u64 new_hint)
{
uint64_t hint = le64_to_cpu(atomic64_cmpxchg_relaxed(
next_hint,
cpu_to_le64(old_hint),
cpu_to_le64(new_hint)));
struct nova_rht_entry *pentry;
nova_dbgv("%s: next_hint = %llx, old_hint = %llx, new_hint = %llx, hint = %llx\n", __func__, atomic64_read(next_hint), old_hint, new_hint, hint);
if ((old_hint & HINT_ADDR_MASK) == (new_hint & HINT_ADDR_MASK)) {
// The hinted fpentry is not changed.
return hint;
}
if (hint == cpu_to_le64(old_hint)) {
// change holder only when exchange successfully
// this is to avoid the case that the old hint has
// been changed by others.
pentry = (struct nova_rht_entry *)(new_hint & HINT_ADDR_MASK);
BUG_ON(pentry == NULL);
incr_holders(pentry);
if (old_hint & HINT_ADDR_MASK) {
pentry = (struct nova_rht_entry *)(old_hint & HINT_ADDR_MASK);
decr_holders(meta, pentry);
}
}
return hint;
}
static inline bool trust_degree_out_of_bound(uint8_t trust_degree)
{
return trust_degree & (1 << TRUST_DEGREE_BITS);
}
// Return 0: Successful
// Return x (!= 0): The offset has been changed, and the new hint is x.
static u64 __incr_trust_degree(struct light_dedup_meta *meta, atomic64_t *next_hint, u64 addr_ori,
uint8_t trust_degree)
{
__le64 old_hint = cpu_to_le64(addr_ori | trust_degree);
__le64 tmp;
uint64_t hint;
while (1) {
if (trust_degree == TRUST_DEGREE_MAX)
return 0;
trust_degree += 1;
hint = addr_ori | trust_degree;
tmp = __update_hint(meta, next_hint, old_hint, cpu_to_le64(hint));
// atomic64_cmpxchg_relaxed(next_hint, old_hint,
// cpu_to_le64(hint));
if (tmp == old_hint)
return 0;
hint = le64_to_cpu(tmp);
if ((hint & HINT_ADDR_MASK) != addr_ori) {
// The hinted fpentry has been changed.
return hint;
}
trust_degree = hint & TRUST_DEGREE_MASK;
old_hint = tmp;
}
}
// Update offset to offset_new if the resulting trust degree is not trustable.
// Return 0: Successful
// Return x (!= 0): The offset has been changed, and the new hint is x.
static u64 __decr_trust_degree(struct light_dedup_meta *meta, atomic64_t *next_hint, u64 addr_ori,
u64 addr_new, uint8_t trust_degree)
{
__le64 old_hint = cpu_to_le64(addr_ori | trust_degree);
__le64 tmp;
uint64_t hint;
while (1) {
if (trust_degree < TRUST_DEGREE_MIN + 2) {
trust_degree = TRUST_DEGREE_MIN;
} else {
trust_degree -= 2;
}
if (!hint_trustable(trust_degree)) {
hint = addr_new | trust_degree;
} else {
hint = addr_ori | trust_degree;
}
// tmp = atomic64_cmpxchg_relaxed(next_hint, old_hint,
// cpu_to_le64(hint));
tmp = __update_hint(meta, next_hint, old_hint, cpu_to_le64(hint));
if (tmp == old_hint)
return 0;
hint = le64_to_cpu(tmp);
if ((hint & HINT_ADDR_MASK) != addr_ori) {
// The hinted fpentry has been changed.
return hint;
}
trust_degree = hint & TRUST_DEGREE_MASK;
old_hint = tmp;
}
}
static u64 incr_trust_degree(struct nova_sb_info *sbi, atomic64_t *next_hint,
u64 addr_ori, uint8_t trust_degree)
{
u64 ret;
// unsigned long irq_flags = 0;
INIT_TIMING(update_hint_time);
NOVA_START_TIMING(update_hint_t, update_hint_time);
// nova_sbi_memunlock_range(sbi, next_hint, sizeof(*next_hint),
// &irq_flags);
ret = __incr_trust_degree(&sbi->light_dedup_meta, next_hint, addr_ori, trust_degree);
// nova_sbi_memlock_range(sbi, next_hint, sizeof(*next_hint), &irq_flags);
// nova_flush_cacheline(next_hint, false);
NOVA_END_TIMING(update_hint_t, update_hint_time);
return ret;
}
static inline u64 decr_trust_degree(struct nova_sb_info *sbi,
atomic64_t *next_hint, u64 addr_ori, u64 addr_new,
uint8_t trust_degree)
{
u64 ret;
// unsigned long irq_flags = 0;
INIT_TIMING(update_hint_time);
NOVA_START_TIMING(update_hint_t, update_hint_time);
// nova_sbi_memunlock_range(sbi, next_hint, sizeof(*next_hint),
// &irq_flags);
ret = __decr_trust_degree(&sbi->light_dedup_meta, next_hint, addr_ori, addr_new,
trust_degree);
// nova_sbi_memlock_range(sbi, next_hint, sizeof(*next_hint), &irq_flags);
// nova_flush_cacheline(next_hint, false);
NOVA_END_TIMING(update_hint_t, update_hint_time);
return ret;
}
static inline void attach_blocknr(struct nova_write_para_continuous *wp,
unsigned long blocknr)
{
// we still want to gather the block information
if (wp->blocknr == 0) {
wp->blocknr = blocknr;
wp->num = 1;
} else if (wp->blocknr + wp->num == blocknr) {
wp->num += 1;
} else {
wp->blocknr_next = blocknr;
}
}