-
Notifications
You must be signed in to change notification settings - Fork 0
/
write_buffer.c
253 lines (196 loc) · 5.49 KB
/
write_buffer.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
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include "uthash/utlist.h"
#include "option.h"
#include "write_buffer.h"
static void fb_destroy_wb_entry(struct fb_wb_pg_t *wb_pg) {
if (!wb_pg) { return; }
if (wb_pg->data) vfree(wb_pg->data);
vfree(wb_pg);
}
static struct fb_wb_pg_t *fb_create_wb_entry(u32 pg_size) {
struct fb_wb_pg_t *wb_pg = vmalloc(sizeof(struct fb_wb_pg_t));
if (!wb_pg) {
printk(KERN_ERR "flashbench: Allocating buffered page failed.\n");
goto FAIL;
}
wb_pg->data = vmalloc(sizeof(u8) * pg_size);
if (!wb_pg->data) {
printk(KERN_ERR "flashbench: Allocating data buffer failed.\n");
goto FAIL;
}
wb_pg->lpa = -1;
wb_pg->wflag = false;
return wb_pg;
FAIL:
fb_destroy_wb_entry(wb_pg);
return NULL;
}
static void fb_wb_set_free_pg(struct fb_wb *wb, struct fb_wb_pg_t *wb_pg) {
DL_APPEND(wb->free_pgs, wb_pg);
}
static void fb_wb_reset_free_pg(struct fb_wb *wb, struct fb_wb_pg_t *wb_pg) {
DL_DELETE(wb->free_pgs, wb_pg);
}
static void fb_wb_set_writing_pg(struct fb_wb *wb, struct fb_wb_pg_t *wb_pg) {
DL_APPEND(wb->writing_pgs, wb_pg);
wb_pg->wflag = true;
}
static void fb_wb_reset_writing_pg(struct fb_wb *wb, struct fb_wb_pg_t *wb_pg) {
DL_DELETE(wb->writing_pgs, wb_pg);
wb_pg->wflag = false;
}
static u32 fb_wb_set_buf_pg(struct fb_wb *wb, struct fb_wb_pg_t *wb_pg) {
DL_APPEND(wb->buf_pgs, wb_pg);
return ++wb->nr_entries;
}
static struct fb_wb_pg_t *fb_wb_get_buf_pg(struct fb_wb *wb, u32 lpa) {
struct fb_wb_pg_t *wb_pg = NULL;
HASH_FIND(hh, wb->hash_pgs, &lpa, sizeof(u32), wb_pg);
return wb_pg;
}
static u32 fb_wb_reset_buf_pg(struct fb_wb *wb, struct fb_wb_pg_t *wb_pg) {
DL_DELETE(wb->buf_pgs, wb_pg);
return --wb->nr_entries;
}
struct fb_wb *fb_create_write_buffer(u32 nr_max_entries, u32 pg_size) {
struct fb_wb *wb = vmalloc(sizeof(struct fb_wb));
if (!wb) {
printk(KERN_ERR "flashbench: Allocating write buffer failed.\n");
goto FAIL;
}
wb->pg_size = pg_size;
wb->nr_entries = 0;
wb->free_pgs = NULL;
wb->writing_pgs = NULL;
wb->buf_pgs = NULL;
wb->hash_pgs = NULL;
for (u32 i = 0; i < nr_max_entries; i++) {
struct fb_wb_pg_t *wb_pg = fb_create_wb_entry(pg_size);
if (!wb_pg) {
printk(KERN_ERR "flashbench: Creating wb entry failed.\n");
goto FAIL;
}
fb_wb_set_free_pg(wb, wb_pg);
}
wb->pg_buf = vmalloc(sizeof(u8) * PHYSICAL_PAGE_SIZE);
if (!wb->pg_buf) {
printk(KERN_ERR "flashbench: Allocating page buffer failed.\n");
goto FAIL;
}
init_completion(&wb->wb_lock);
complete(&wb->wb_lock);
return wb;
FAIL:
fb_destroy_write_buffer(wb);
return NULL;
}
void fb_destroy_write_buffer(struct fb_wb *wb) {
if (!wb) { return; }
if (wb->free_pgs) {
for (;;) {
struct fb_wb_pg_t *wb_pg = wb->free_pgs;
if (!wb_pg) { break; }
fb_wb_reset_free_pg(wb, wb_pg);
fb_destroy_wb_entry(wb_pg);
}
}
if (wb->buf_pgs) {
for (;;) {
struct fb_wb_pg_t *wb_pg = wb->buf_pgs;
if (!wb_pg) { break; }
fb_wb_reset_buf_pg(wb, wb_pg);
HASH_DEL(wb->hash_pgs, wb_pg);
fb_destroy_wb_entry(wb_pg);
}
}
if (wb->writing_pgs) {
for (;;) {
struct fb_wb_pg_t *wb_pg = wb->writing_pgs;
if (!wb_pg) { break; }
fb_wb_reset_writing_pg(wb, wb_pg);
HASH_DEL(wb->hash_pgs, wb_pg);
fb_destroy_wb_entry(wb_pg);
}
}
vfree(wb);
}
static u32 fb_get_nr_pgs_in_wb(struct fb_wb *wb, int lock) {
if (lock) {
wait_for_completion(&wb->wb_lock);
reinit_completion(&wb->wb_lock);
}
u32 ret = wb->nr_entries;
if (lock) {
complete(&wb->wb_lock);
}
return ret;
}
int fb_get_pg_data(struct fb_wb *wb, u32 lpa, u8 *dest) {
int ret = -1;
wait_for_completion(&wb->wb_lock);
reinit_completion(&wb->wb_lock);
struct fb_wb_pg_t *wb_pg = fb_wb_get_buf_pg(wb, lpa);
if (wb_pg) {
memcpy(dest, wb_pg->data, wb->pg_size);
ret = 0;
}
complete(&wb->wb_lock);
return ret;
}
int fb_get_pgs_to_write(struct fb_wb *wb, u32 nr_pgs, u32 *lpas, u8 *dest) {
int ret = -1;
wait_for_completion(&wb->wb_lock);
reinit_completion(&wb->wb_lock);
if (fb_get_nr_pgs_in_wb(wb, false) >= nr_pgs) {
for (u32 i = 0; i < nr_pgs; i++) {
struct fb_wb_pg_t *wb_pg = wb->buf_pgs;
fb_wb_reset_buf_pg(wb, wb_pg);
fb_wb_set_writing_pg(wb, wb_pg);
memcpy(dest + (i * wb->pg_size), wb_pg->data, wb->pg_size);
lpas[i] = wb_pg->lpa;
}
ret = 0;
}
complete(&wb->wb_lock);
return ret;
}
int fb_put_pg(struct fb_wb *wb, u32 lpa, u8 *src) {
struct fb_wb_pg_t *wb_pg = fb_wb_get_buf_pg(wb, lpa);
if (!wb_pg) {
wb_pg = wb->free_pgs;
if (!wb_pg) { return -1; }
fb_wb_reset_free_pg(wb, wb_pg);
wb_pg->lpa = lpa;
HASH_ADD(hh, wb->hash_pgs, lpa, sizeof(u32), wb_pg);
} else {
if (wb_pg->wflag) {
fb_wb_reset_writing_pg(wb, wb_pg);
} else {
fb_wb_reset_buf_pg(wb, wb_pg);
}
}
memcpy(wb_pg->data, src, wb->pg_size);
fb_wb_set_buf_pg(wb, wb_pg);
return 0;
}
void fb_rm_written_pgs(struct fb_wb *wb) {
for (;;) {
struct fb_wb_pg_t *wb_pg = wb->writing_pgs;
if (!wb_pg) { break; }
fb_wb_reset_writing_pg(wb, wb_pg);
HASH_DEL(wb->hash_pgs, wb_pg);
fb_wb_set_free_pg(wb, wb_pg);
}
}
void fb_rm_buf_pg(struct fb_wb *wb, u32 lpa) {
struct fb_wb_pg_t *wb_pg = fb_wb_get_buf_pg(wb, lpa);
if (!wb_pg) { return; }
if (wb_pg->wflag) {
fb_wb_reset_writing_pg(wb, wb_pg);
} else {
fb_wb_reset_buf_pg(wb, wb_pg);
}
HASH_DEL(wb->hash_pgs, wb_pg);
fb_wb_set_free_pg(wb, wb_pg);
}