-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdedupe.c
executable file
·434 lines (400 loc) · 11.8 KB
/
dedupe.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
#include <linux/pagemap.h>
#include <crypto/hash.h>
#include <crypto/md5.h>
#include <crypto/sha.h>
#include <crypto/algapi.h>
#include "dedupe.h"
#include <linux/f2fs_fs.h>
#include <linux/vmalloc.h>
int f2fs_dedupe_calc_hash(struct page *p, u8 hash[], struct dedupe_info *dedupe_info)
{
//int i;
int ret;
struct {
struct shash_desc desc;
char ctx[dedupe_info->crypto_shash_descsize];
} sdesc;
char *d;
sdesc.desc.tfm = dedupe_info->tfm;
sdesc.desc.flags = 0;
ret = crypto_shash_init(&sdesc.desc);
if (ret)
return ret;
d = kmap(p);
ret = crypto_shash_digest(&sdesc.desc, d, PAGE_SIZE, hash);
kunmap(p);
/*for(i=0;i<4;i++)
{
printk("%llx",be64_to_cpu(*(long long*)&hash[i*8]));
}
printk("\n");*/
return ret;
}
#ifdef F2FS_BLOOM_FILTER
int f2fs_dedupe_bloom_filter(u8 hash[], struct dedupe_info *dedupe_info)
{
int i;
unsigned int *pos = (unsigned int *)hash;
for(i=0;i<dedupe_info->bloom_filter_hash_fun_count;i++)
{
if(0 == dedupe_info->bloom_filter[*(pos++)&dedupe_info->bloom_filter_mask])
{
return 1;//stand for not in hash table;
}
}
return 0;
}
void init_f2fs_dedupe_bloom_filter(struct dedupe_info *dedupe_info)
{
struct dedupe *cur;
int i;
for(cur = dedupe_info->dedupe_md; cur < dedupe_info->dedupe_md + dedupe_info->dedupe_block_count * DEDUPE_PER_BLOCK;cur++)
{
if(unlikely(cur->ref))
{
unsigned int *pos = (unsigned int *)cur->hash;
for(i=0;i<dedupe_info->bloom_filter_hash_fun_count;i++)
{
dedupe_info->bloom_filter[*(pos++)&dedupe_info->bloom_filter_mask]++;
}
}
}
}
#endif
struct dedupe *f2fs_dedupe_search(u8 hash[], struct dedupe_info *dedupe_info)
{
struct dedupe *c = &dedupe_info->dedupe_md[(*(unsigned int *)hash)%(dedupe_info->dedupe_block_count/64) * DEDUPE_PER_BLOCK*64],*cur;
#ifdef F2FS_NO_HASH
c = dedupe_info->dedupe_md;
#endif
#ifdef F2FS_BLOOM_FILTER
if(f2fs_dedupe_bloom_filter(hash, dedupe_info)) return NULL;
#endif
for(cur=c; cur < dedupe_info->dedupe_md + dedupe_info->dedupe_block_count * DEDUPE_PER_BLOCK; cur++)
{
if(unlikely(cur->ref&&!memcmp(hash, cur->hash, dedupe_info->digest_len)))
{
dedupe_info->logical_blk_cnt++;
return cur;
}
}
for(cur = dedupe_info->dedupe_md; cur < c; cur++)
{
if(unlikely(cur->ref&&!memcmp(hash, cur->hash, dedupe_info->digest_len)))
{
dedupe_info->logical_blk_cnt++;
return cur;
}
}
return NULL;
}
void set_dedupe_dirty(struct dedupe_info *dedupe_info, struct dedupe *dedupe)
{
set_bit((dedupe - dedupe_info->dedupe_md)/DEDUPE_PER_BLOCK, (long unsigned int *)dedupe_info->dedupe_md_dirty_bitmap);
}
void test_summary_table(struct dedupe_info *dedupe_info,int index)
{
struct summary_table_entry *entry;
struct dedupe *dedupe=&dedupe_info->dedupe_md[index];
printk("---------------------summary table--------------------------\n");
if(dedupe->start_pos_st!=-1)
{
entry=dedupe_info->sum_table+dedupe->start_pos_st;
while(entry->next!=-1)
{
printk("nid:%d ",entry->nid);
printk("ofs_in_node:%d ",entry->ofs_in_node);
printk("next:%d\n",entry->next);
if(entry->next!=-1)
{
entry=dedupe_info->sum_table+entry->next;
}
}
printk("nid:%d ",entry->nid);
printk("ofs_in_node:%d ",entry->ofs_in_node);
printk("next:%d\n",entry->next);
}
else
printk("---------------empty------------------------------\n");
}
int f2fs_del_summary_table_entry(struct dedupe_info *dedupe_info,int index,struct summary_table_entry *origin_summary,struct summary_table_entry del_summary)
{
struct summary_table_entry *entry;
struct summary_table_entry *pre_entry;
struct dedupe *dedupe=&dedupe_info->dedupe_md[index];
if(dedupe->start_pos_st!=-1)
{
if(origin_summary->nid==del_summary.nid
&&origin_summary->ofs_in_node==del_summary.ofs_in_node)
{
entry=dedupe_info->sum_table+dedupe->start_pos_st;
origin_summary->nid=entry->nid;
origin_summary->ofs_in_node=entry->ofs_in_node;
dedupe->start_pos_st=entry->next;
entry->next=dedupe_info->sum_table->next;
dedupe_info->sum_table->next=entry-dedupe_info->sum_table;
return 1;
}
else
{
pre_entry=dedupe_info->sum_table+dedupe->start_pos_st;
if(pre_entry->nid==del_summary.nid
&&pre_entry->ofs_in_node==del_summary.ofs_in_node)
{
dedupe->start_pos_st=pre_entry->next;
pre_entry->next=dedupe_info->sum_table->next;
dedupe_info->sum_table->next=pre_entry-dedupe_info->sum_table;
}
else
{
while(pre_entry->next!=-1)
{
entry=dedupe_info->sum_table+pre_entry->next;
if(unlikely(entry->nid==del_summary.nid&&entry->ofs_in_node==del_summary.ofs_in_node))
{
pre_entry->next=entry->next;
entry->next=dedupe_info->sum_table->next;
dedupe_info->sum_table->next=entry-dedupe_info->sum_table;
return 0;
}
else
pre_entry=entry;
}
}
}
}
return -1;
}
void f2fs_gc_change_reverse_and_bloom(struct dedupe_info *dedupe_info, block_t old_blkaddr,block_t new_blkaddr,int offset)
{
#ifdef F2FS_REVERSE_ADDR
dedupe_info->reverse_addr[old_blkaddr]=-1;
dedupe_info->reverse_addr[new_blkaddr]=offset;
#endif
}
int f2fs_dedupe_delete_addr(block_t addr, struct dedupe_info *dedupe_info,int *index)
{
struct dedupe *cur,*c = dedupe_info->last_delete_dedupe;
spin_lock(&dedupe_info->lock);
if(NEW_ADDR == addr) return -1;
#ifdef F2FS_REVERSE_ADDR
if(-1 == dedupe_info->reverse_addr[addr])
{
return -1;
}
cur = &dedupe_info->dedupe_md[dedupe_info->reverse_addr[addr]];
if(cur->ref)
{
goto aa;
}
else
{
return -1;
}
#endif
for(cur=c; cur < dedupe_info->dedupe_md + dedupe_info->dedupe_block_count * DEDUPE_PER_BLOCK; cur++)
{
if(unlikely(cur->ref && addr == cur->addr))
{
*index=cur-dedupe_info->dedupe_md;
cur->ref--;
dedupe_info->logical_blk_cnt--;
dedupe_info->last_delete_dedupe = cur;
set_dedupe_dirty(dedupe_info, cur);
if(0 == cur->ref)
{
#ifdef F2FS_BLOOM_FILTER
int i;
unsigned int *pos = (unsigned int *)cur->hash;
for(i=0;i<dedupe_info->bloom_filter_hash_fun_count;i++)
{
dedupe_info->bloom_filter[*(pos++)&dedupe_info->bloom_filter_mask]--;
}
#endif
*index=-1;
cur->addr = 0;
dedupe_info->physical_blk_cnt--;
return 0;
}
else
{
return cur->ref;
}
}
}
for(cur = dedupe_info->dedupe_md; cur < c; cur++)
{
if(unlikely(cur->ref && addr == cur->addr))
{
#ifdef F2FS_REVERSE_ADDR
aa:
#endif
*index=cur-dedupe_info->dedupe_md;
cur->ref--;
dedupe_info->logical_blk_cnt--;
dedupe_info->last_delete_dedupe = cur;
set_dedupe_dirty(dedupe_info, cur);
if(0 == cur->ref)
{
#ifdef F2FS_BLOOM_FILTER
int i;
unsigned int *pos = (unsigned int *)cur->hash;
for(i=0;i<dedupe_info->bloom_filter_hash_fun_count;i++)
{
dedupe_info->bloom_filter[*(pos++)&dedupe_info->bloom_filter_mask]--;
}
#endif
*index=-1;
cur->addr = 0;
dedupe_info->physical_blk_cnt--;
#ifdef F2FS_REVERSE_ADDR
dedupe_info->reverse_addr[addr] = -1;
#endif
return 0;
}
else
{
return cur->ref;
}
}
}
return -1;
}
int f2fs_dedupe_add(u8 hash[], struct dedupe_info *dedupe_info, block_t addr)
{
int ret = 0;
int search_count = 0;
struct dedupe* cur = &dedupe_info->dedupe_md[(*(unsigned int *)hash)%(dedupe_info->dedupe_block_count/64) * DEDUPE_PER_BLOCK* 64];
#ifdef F2FS_NO_HASH
cur = dedupe_info->dedupe_md;
#endif
while(cur->ref)
{
if(likely(cur != dedupe_info->dedupe_md + dedupe_info->dedupe_block_count * DEDUPE_PER_BLOCK - 1))
{
cur++;
}
else
{
cur = dedupe_info->dedupe_md;
}
search_count++;
if(search_count>dedupe_info->dedupe_block_count * DEDUPE_PER_BLOCK)
{
printk("can not add f2fs dedupe md.\n");
ret = -1;
break;
}
}
if(0 == ret)
{
#ifdef F2FS_BLOOM_FILTER
unsigned int *pos;
int i;
#endif
cur->addr = addr;
cur->ref = 1;
cur->start_pos_st=-1;
memcpy(cur->hash, hash, dedupe_info->digest_len);
#ifdef F2FS_REVERSE_ADDR
dedupe_info->reverse_addr[addr] = cur - dedupe_info->dedupe_md;
#endif
#ifdef F2FS_BLOOM_FILTER
pos = (unsigned int *)cur->hash;
for(i=0;i<dedupe_info->bloom_filter_hash_fun_count;i++)
{
dedupe_info->bloom_filter[*(pos++)&dedupe_info->bloom_filter_mask]++;
//printk("add %d\n", *(pos++)&dedupe_info->bloom_filter_mask);
}
#endif
set_dedupe_dirty(dedupe_info, cur);
dedupe_info->logical_blk_cnt++;
dedupe_info->physical_blk_cnt++;
}
return ret;
}
int f2fs_dedupe_O_log2(unsigned int x)
{
unsigned char log_2[256] = {
0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
};
int l = -1;
while (x >= 256) { l += 8; x >>= 8; }
return l + log_2[x];
}
void init_summary_table(struct dedupe_info *dedupe_info)
{
int i;
struct summary_table_entry *entry;
dedupe_info->sum_table=vmalloc(SUM_TABLE_LEN*sizeof(struct summary_table_entry));
memset(dedupe_info->sum_table,0,SUM_TABLE_LEN*sizeof(struct summary_table_entry));
entry=dedupe_info->sum_table;
for(i=0;i<SUM_TABLE_LEN;i++)
{
(entry+i)->next=i+1;
}
}
int init_dedupe_info(struct dedupe_info *dedupe_info)
{
int ret = 0;
dedupe_info->digest_len = 16;
spin_lock_init(&dedupe_info->lock);
INIT_LIST_HEAD(&dedupe_info->queue);
dedupe_info->dedupe_md = vmalloc(dedupe_info->dedupe_size);
memset(dedupe_info->dedupe_md, 0, dedupe_info->dedupe_size);
dedupe_info->dedupe_md_dirty_bitmap = kzalloc(dedupe_info->dedupe_bitmap_size, GFP_KERNEL);
dedupe_info->dedupe_segment_count = DEDUPE_SEGMENT_COUNT;
#ifdef F2FS_BLOOM_FILTER
dedupe_info->bloom_filter_mask = (1<<(f2fs_dedupe_O_log2(dedupe_info->dedupe_block_count) + 10)) -1;
dedupe_info->bloom_filter = vmalloc((dedupe_info->bloom_filter_mask + 1) * sizeof(unsigned int));
memset(dedupe_info->bloom_filter, 0, dedupe_info->bloom_filter_mask * sizeof(unsigned int));
dedupe_info->bloom_filter_hash_fun_count = 4;
#endif
init_summary_table(dedupe_info);
dedupe_info->last_delete_dedupe = dedupe_info->dedupe_md;
dedupe_info->tfm = crypto_alloc_shash("md5", 0, 0);
dedupe_info->crypto_shash_descsize = crypto_shash_descsize(dedupe_info->tfm);
return ret;
}
void exit_dedupe_info(struct dedupe_info *dedupe_info)
{
vfree(dedupe_info->dedupe_md);
vfree(dedupe_info->sum_table);
kfree(dedupe_info->dedupe_md_dirty_bitmap);
kfree(dedupe_info->dedupe_bitmap);
#ifdef F2FS_REVERSE_ADDR
vfree(dedupe_info->reverse_addr);
#endif
crypto_free_shash(dedupe_info->tfm);
#ifdef F2FS_BLOOM_FILTER
vfree(dedupe_info->bloom_filter);
#endif
}
//f2fs_gc_dedupe
void set_summary_table_entry(struct summary_table_entry *entry,__le32 nid,__le16 ofs_in_node)
{
entry->nid=nid;
entry->ofs_in_node=ofs_in_node;
}
int f2fs_add_summary_table_entry(struct dedupe_info *dedupe_info,struct dedupe *dedupe,__le32 nid,__le16 ofs_in_node)
{
struct summary_table_entry *entry=NULL;
if(unlikely(dedupe_info->sum_table->next>=SUM_TABLE_LEN))
{
printk("can't add summary table entry!\n");
return -1;//beyond the array length;
}
entry=dedupe_info->sum_table+dedupe_info->sum_table->next;
set_summary_table_entry(entry, nid, ofs_in_node);
dedupe_info->sum_table->next=entry->next;
entry->next=dedupe->start_pos_st;
dedupe->start_pos_st=entry-dedupe_info->sum_table;
return 0;//add success;
}