forked from vinothkumarraja/dmdedup4.8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdm-dedup-check.c
249 lines (194 loc) · 5.49 KB
/
dm-dedup-check.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
/*
* Copyright (C) 2017-2017 Vasily Tarasov
* Copyright (C) 2017-2017 Erez Zadok
* Copyright (c) 2017-2017 Vinothumar Raja
* Copyright (c) 2017-2017 Nidhi Panpalia
*
* This file is released under the GPL.
*/
#include "dm-dedup-target.h"
#include "dm-dedup-check.h"
#include "dm-dedup-hash.h"
#include "dm-dedup-backend.h"
#include "dm-dedup-kvstore.h"
/*
* bio - contains the data read from the disk
* io - contains pbn, lbn information obtained from LBN->PBN mapping
*
* Hash is calculated for the data inside bio, and
* checked against HASH->PBN structure. If PBN from both the
* mappings doesn't match, then the function tries to correct
* the error
*
* The refcount for old pbn is reduced so that it gets taken care
* by the garbage collection engine when it reaches 1
*/
static void check_endio(struct bio *bio, struct check_io *io)
{
int r;
struct dedup_config *dc;
u8 hash[MAX_DIGEST_SIZE];
struct hash_pbn_value hashpbn_value;
struct lbn_pbn_value lbnpbn_value;
uint32_t vsize;
if (bio->bi_error < 0)
goto out;
dc = io->dc;
BUG_ON(!dc->check_corruption);
/* calculate hash for the data read from the disk */
r = compute_hash_bio(dc->desc_table, bio, hash);
/* if hash calculation fails, return silently */
if (r)
goto out;
/* retrieve the pbn from HASH->PBN mapping if any */
r = dc->kvs_hash_pbn->kvs_lookup(dc->kvs_hash_pbn, hash,
dc->crypto_key_size, &hashpbn_value, &vsize);
/* HASH->PBN lookup failed */
if (r < 0)
goto out;
if (r == 0)
goto no_hash_pbn_entry;
/* HASH->PBN lookup return a valid entry */
/* Lookup successful, Comparing LBN-PBN with HASH-PBN */
if (io->pbn == hashpbn_value.pbn)
goto out;
/* if fec is not enabled, only report corruption */
if (!dc->fec)
goto no_fec;
/* if PBNs don't match, remove the old LBN->PBN entry */
r = dc->kvs_lbn_pbn->kvs_delete(dc->kvs_lbn_pbn,
(void *)&(io->lbn), sizeof(io->lbn));
if (r < 0)
goto out_fec_fail;
/* decrement the refcount for old PBN */
dc->mdops->dec_refcount(dc->bmd, io->pbn);
lbnpbn_value.pbn = hashpbn_value.pbn;
/* increment the refcount for new PBN */
r = dc->mdops->inc_refcount(dc->bmd, lbnpbn_value.pbn);
if (r < 0)
goto out_fec_fail;
/* insert the new mapping in LBN->PBN mapping */
r = dc->kvs_lbn_pbn->kvs_insert(dc->kvs_lbn_pbn,
(void *)&(io->lbn), sizeof(io->lbn),
(void *)&lbnpbn_value,
sizeof(lbnpbn_value));
if (r < 0) {
dc->mdops->dec_refcount(dc->bmd, lbnpbn_value.pbn);
goto out_fec_fail;
}
goto out_fec_pass;
no_hash_pbn_entry:
/* No matching entry found in HASH->PBN lookup */
/* if fec is not enabled, only report corruption */
if(!dc->fec)
goto no_fec;
/*
* No matching entry found in HASH->PBN lookup
* Insert a new HASH->PBN mapping
* XXX: Leaves an extra entry in HASH->PBN for the
old data.
*/
hashpbn_value.pbn = io->pbn;
r = dc->kvs_hash_pbn->kvs_insert(dc->kvs_hash_pbn,
(void *)hash, dc->crypto_key_size,
(void *)&hashpbn_value, sizeof(hashpbn_value));
if (r < 0)
goto out_fec_fail;
out_fec_pass:
dc->fec_fixed++;
goto out_corruption;
no_fec:
out_fec_fail:
bio->bi_error = -EIO;
out_corruption:
dc->corrupted_blocks++;
DMINFO("Corrupted block: %lld", io->lbn);
out:
kfree(io);
bio_endio(bio);
}
static void issue_work(struct work_struct *ws)
{
struct check_work *data = container_of(ws, struct check_work, worker);
struct check_io *io = (struct check_io *)data->io;
mempool_free(data, io->dc->check_work_pool);
check_endio(io->base_bio, io);
}
void dedup_check_endio(struct bio *clone)
{
struct check_work *data;
struct check_io *io;
io = clone->bi_private;
/* deallocate clone created before disk read */
bio_put(clone);
/*
* initialize a worker for handling the FEC.
* Directly calling check_work would panic
*/
data = mempool_alloc(io->dc->check_work_pool, GFP_NOIO);
if (!data) {
/*
* XXX: Decide whether to fail or silently pass
* if unable to do corruption check
* and set the corresponding error flags
*/
bio_endio(io->base_bio);
kfree(io);
return;
}
data->io = io;
INIT_WORK(&(data->worker), issue_work);
queue_work(io->dc->workqueue, &(data->worker));
}
static void flush_endio(struct bio *bio, struct check_io *io)
{
int r;
struct dedup_config *dc;
if (bio->bi_error < 0)
goto out;
dc = io->dc;
BUG_ON(!(bio->bi_opf & (REQ_PREFLUSH | REQ_FUA)));
r = dc->mdops->flush_meta(dc->bmd);
if (r < 0) {
bio->bi_error = -EIO;
goto out;
}
dc->writes_after_flush = 0;
DMINFO("Finishing FUA flush");
out:
kfree(io);
bio_endio(bio);
}
static void issue_flush(struct work_struct *ws)
{
struct check_work *data = container_of(ws, struct check_work, worker);
struct check_io *io = (struct check_io *)data->io;
mempool_free(data, io->dc->check_work_pool);
flush_endio(io->base_bio, io);
}
void dedup_flush_endio(struct bio *clone)
{
struct check_io *io;
struct check_work *data;
io = clone->bi_private;
/* deallocate clone created before disk read */
bio_put(clone);
/*
* initialize a worker for handling metadata flush.
* Directly calling flush_work would panic
*/
data = mempool_alloc(io->dc->check_work_pool, GFP_NOIO);
if (!data) {
/*
* XXX: Decide whether to fail or silently pass
* if unable to do metadata flush
* and set the corresponding error flags
*/
bio_endio(io->base_bio);
kfree(io);
return;
}
data->io = io;
INIT_WORK(&(data->worker), issue_flush);
queue_work(io->dc->workqueue, &(data->worker));
}