-
Notifications
You must be signed in to change notification settings - Fork 7
/
dm-dedup-ram.c
595 lines (451 loc) · 12.2 KB
/
dm-dedup-ram.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
/*
* Copyright (C) 2012-2017 Vasily Tarasov
* Copyright (C) 2012-2014 Geoff Kuenning
* Copyright (C) 2012-2014 Sonam Mandal
* Copyright (C) 2012-2014 Karthikeyani Palanisami
* Copyright (C) 2012-2014 Philip Shilane
* Copyright (C) 2012-2014 Sagar Trehan
* Copyright (C) 2012-2017 Erez Zadok
* Copyright (c) 2012-2017 Stony Brook University
* Copyright (c) 2012-2017 The Research Foundation for SUNY
* This file is released under the GPL.
*/
#include <linux/vmalloc.h>
#include <linux/errno.h>
#include "dm-dedup-kvstore.h"
#include "dm-dedup-ram.h"
#include "dm-dedup-backend.h"
#define EMPTY_ENTRY -5
#define DELETED_ENTRY -6
#define UINT32_MAX (4294967295U)
#define HASHTABLE_OVERPROV (10)
struct metadata {
/* Space Map */
u32 *smap;
u64 smax;
u64 allocptr;
/*
* XXX: Currently we support only one linear and one sparse KVS.
*/
struct kvstore_inram *kvs_linear;
struct kvstore_inram *kvs_sparse;
};
struct kvstore_inram {
struct kvstore ckvs;
u32 kmax;
char *store;
};
static struct metadata *init_meta_inram(void *init_param, bool *unformatted)
{
u64 smap_size, tmp;
struct metadata *md;
struct init_param_inram *p = (struct init_param_inram *)init_param;
DMINFO("Initializing INRAM backend");
*unformatted = true;
md = kmalloc(sizeof(*md), GFP_NOIO);
if (!md)
return ERR_PTR(-ENOMEM);
smap_size = p->blocks * sizeof(uint32_t);
md->smap = vmalloc(smap_size);
if (!md->smap) {
kfree(md);
return ERR_PTR(-ENOMEM);
}
tmp = smap_size;
(void)do_div(tmp, (1024 * 1024));
DMINFO("Space allocated for pbn reference count map: %llu.%06llu MB\n",
tmp, smap_size - (tmp * (1024 * 1024)));
memset(md->smap, 0, smap_size);
md->smax = p->blocks;
md->allocptr = 0;
md->kvs_linear = NULL;
md->kvs_sparse = NULL;
return md;
}
static void exit_meta_inram(struct metadata *md)
{
if (md->smap)
vfree(md->smap);
if (md->kvs_linear) {
if (md->kvs_linear->store)
vfree(md->kvs_linear->store);
kfree(md->kvs_linear);
}
if (md->kvs_sparse) {
if (md->kvs_sparse->store)
vfree(md->kvs_sparse->store);
kfree(md->kvs_sparse);
}
kfree(md);
}
static int flush_meta_inram(struct metadata *md)
{
return 0;
}
/********************************************************
* Space Management Functions *
********************************************************/
static uint64_t next_head(u64 current_head, u64 smax)
{
current_head += 1;
return dm_sector_div64(current_head, smax);
}
static int alloc_data_block_inram(struct metadata *md, uint64_t *blockn)
{
u64 head, tail;
head = md->allocptr;
tail = md->allocptr;
do {
if (!md->smap[head]) {
md->smap[head] = 1;
*blockn = head;
md->allocptr = next_head(head, md->smax);
return 0;
}
head = next_head(head, md->smax);
} while (head != tail);
return -ENOSPC;
}
static int inc_refcount_inram(struct metadata *md, uint64_t blockn)
{
if (blockn >= md->smax)
return -ERANGE;
if (md->smap[blockn] != UINT32_MAX)
md->smap[blockn]++;
else
return -E2BIG;
return 0;
}
static int dec_refcount_inram(struct metadata *md, uint64_t blockn)
{
if (blockn >= md->smax)
return -ERANGE;
if (md->smap[blockn])
md->smap[blockn]--;
else
return -EFAULT;
return 0;
}
static int get_refcount_inram(struct metadata *md, uint64_t blockn)
{
if (blockn >= md->smax)
return -ERANGE;
return md->smap[blockn];
}
/********************************************************
* General KVS Functions *
********************************************************/
static int is_empty(char *ptr, int length)
{
int i = 0;
while ((i < length) && (ptr[i] == EMPTY_ENTRY))
i++;
return i == length;
}
static int is_deleted(char *ptr, int length)
{
int i = 0;
while ((i < length) && (ptr[i] == DELETED_ENTRY))
i++;
return i == length;
}
/*********************************************************
* Linear KVS Functions *
*********************************************************/
static int kvs_delete_linear_inram(struct kvstore *kvs,
void *key, int32_t ksize)
{
u64 idx;
char *ptr;
struct kvstore_inram *kvinram = NULL;
kvinram = container_of(kvs, struct kvstore_inram, ckvs);
if (ksize != kvs->ksize)
return -EINVAL;
idx = *((uint64_t *)key);
if (idx > kvinram->kmax)
return -ERANGE;
ptr = kvinram->store + kvs->vsize * idx;
if (is_empty(ptr, kvs->vsize))
return -ENODEV;
memset(ptr, EMPTY_ENTRY, kvs->vsize);
return 0;
}
/*
* 0 - not found
* 1 - found
* < 0 - error on lookup
*/
static int kvs_lookup_linear_inram(struct kvstore *kvs, void *key,
s32 ksize, void *value,
int32_t *vsize)
{
u64 idx;
char *ptr;
struct kvstore_inram *kvinram = NULL;
kvinram = container_of(kvs, struct kvstore_inram, ckvs);
if (ksize != kvs->ksize)
return -EINVAL;
idx = *((uint64_t *)key);
if (idx > kvinram->kmax)
return -ERANGE;
ptr = kvinram->store + kvs->vsize * idx;
if (is_empty(ptr, kvs->vsize))
return 0;
memcpy(value, ptr, kvs->vsize);
*vsize = kvs->vsize;
return 1;
}
static int kvs_insert_linear_inram(struct kvstore *kvs, void *key,
s32 ksize, void *value,
int32_t vsize)
{
u64 idx;
char *ptr;
struct kvstore_inram *kvinram = NULL;
kvinram = container_of(kvs, struct kvstore_inram, ckvs);
if (ksize != kvs->ksize)
return -EINVAL;
if (vsize != kvs->vsize)
return -EINVAL;
idx = *((uint64_t *)key);
if (idx > kvinram->kmax)
return -ERANGE;
ptr = kvinram->store + kvs->vsize * idx;
memcpy(ptr, value, kvs->vsize);
return 0;
}
/*
* NOTE: if iteration_action() is a deletion/cleanup function,
* Make sure that the store is implemented such that
* deletion in-place is safe while iterating.
*/
static int kvs_iterate_linear_inram(struct kvstore *kvs,
int (*iteration_action)
(void *key, int32_t ksize,
void *value, int32_t vsize,
void *data), void *data)
{
int ret = 0;
u64 i = 0;
char *ptr = NULL;
struct kvstore_inram *kvinram = NULL;
kvinram = container_of(kvs, struct kvstore_inram, ckvs);
for (i = 0; i < kvinram->kmax; i++) {
ptr = kvinram->store + (i * kvs->vsize);
ret = is_empty(ptr, kvs->vsize);
if (!ret) {
ret = iteration_action((void *)&i, kvs->ksize,
(void *)ptr, kvs->vsize, data);
if (ret < 0)
goto out;
}
}
out:
return ret;
}
static struct kvstore *kvs_create_linear_inram(struct metadata *md,
u32 ksize, u32 vsize,
u32 kmax, bool unformatted)
{
struct kvstore_inram *kvs;
u64 kvstore_size, tmp;
if (!vsize || !ksize || !kmax)
return ERR_PTR(-ENOTSUPP);
/* Currently only 64bit keys are supported */
if (ksize != 8)
return ERR_PTR(-ENOTSUPP);
/* We do not support two or more KVSs at the moment */
if (md->kvs_linear)
return ERR_PTR(-EBUSY);
kvs = kmalloc(sizeof(*kvs), GFP_NOIO);
if (!kvs)
return ERR_PTR(-ENOMEM);
kvstore_size = (kmax + 1) * vsize;
kvs->store = vmalloc(kvstore_size);
if (!kvs->store) {
kfree(kvs);
return ERR_PTR(-ENOMEM);
}
tmp = kvstore_size;
(void)do_div(tmp, (1024 * 1024));
DMINFO("Space allocated for linear key value store: %llu.%06llu MB\n",
tmp, kvstore_size - (tmp * (1024 * 1024)));
memset(kvs->store, EMPTY_ENTRY, kvstore_size);
kvs->ckvs.vsize = vsize;
kvs->ckvs.ksize = ksize;
kvs->kmax = kmax;
kvs->ckvs.kvs_insert = kvs_insert_linear_inram;
kvs->ckvs.kvs_lookup = kvs_lookup_linear_inram;
kvs->ckvs.kvs_delete = kvs_delete_linear_inram;
kvs->ckvs.kvs_iterate = kvs_iterate_linear_inram;
md->kvs_linear = kvs;
return &(kvs->ckvs);
}
/********************************************************
* Sparse KVS Functions *
********************************************************/
static int kvs_delete_sparse_inram(struct kvstore *kvs,
void *key, int32_t ksize)
{
u64 idxhead = *((uint64_t *)key);
u32 entry_size, head, tail;
char *ptr;
struct kvstore_inram *kvinram = NULL;
if (ksize != kvs->ksize)
return -EINVAL;
kvinram = container_of(kvs, struct kvstore_inram, ckvs);
entry_size = kvs->vsize + kvs->ksize;
head = do_div(idxhead, kvinram->kmax);
tail = head;
do {
ptr = kvinram->store + entry_size * head;
if (is_empty(ptr, entry_size))
goto doesnotexist;
if (memcmp(ptr, key, kvs->ksize)) {
head = next_head(head, kvinram->kmax);
} else {
memset(ptr, DELETED_ENTRY, entry_size);
return 0;
}
} while (head != tail);
doesnotexist:
return -ENODEV;
}
/*
* 0 - not found
* 1 - found
* < 0 - error on lookup
*/
static int kvs_lookup_sparse_inram(struct kvstore *kvs, void *key,
s32 ksize, void *value, int32_t *vsize)
{
u64 idxhead = *((uint64_t *)key);
u32 entry_size, head, tail;
char *ptr;
struct kvstore_inram *kvinram = NULL;
if (ksize != kvs->ksize)
return -EINVAL;
kvinram = container_of(kvs, struct kvstore_inram, ckvs);
entry_size = kvs->vsize + kvs->ksize;
head = do_div(idxhead, kvinram->kmax);
tail = head;
do {
ptr = kvinram->store + entry_size * head;
if (is_empty(ptr, entry_size))
return 0;
if (memcmp(ptr, key, kvs->ksize)) {
head = next_head(head, kvinram->kmax);
} else {
memcpy(value, ptr + kvs->ksize, kvs->vsize);
return 1;
}
} while (head != tail);
return 0;
}
static int kvs_insert_sparse_inram(struct kvstore *kvs, void *key,
s32 ksize, void *value, s32 vsize)
{
u64 idxhead = *((uint64_t *)key);
u32 entry_size, head, tail;
char *ptr;
struct kvstore_inram *kvinram = NULL;
BUG_ON(!kvs);
if (ksize > kvs->ksize)
return -EINVAL;
kvinram = container_of(kvs, struct kvstore_inram, ckvs);
entry_size = kvs->vsize + kvs->ksize;
head = do_div(idxhead, kvinram->kmax);
tail = head;
do {
ptr = kvinram->store + entry_size * head;
if (is_empty(ptr, entry_size) || is_deleted(ptr, entry_size)) {
memcpy(ptr, key, kvs->ksize);
memcpy(ptr + kvs->ksize, value, kvs->vsize);
return 0;
}
head = next_head(head, kvinram->kmax);
} while (head != tail);
return -ENOSPC;
}
/*
*
* NOTE: if iteration_action() is a deletion/cleanup function,
* Make sure that the store is implemented such that
* deletion in-place is safe while iterating.
*/
static int kvs_iterate_sparse_inram(struct kvstore *kvs,
int (*iteration_action)
(void *key, int32_t ksize,
void *value, int32_t vsize,
void *data), void *data)
{
int err = 0;
u32 kvalue_size, head = 0;
char *ptr = NULL;
struct kvstore_inram *kvinram = NULL;
BUG_ON(!kvs);
kvinram = container_of(kvs, struct kvstore_inram, ckvs);
kvalue_size = kvs->vsize + kvs->ksize;
do {
ptr = kvinram->store + (head * kvalue_size);
if (!is_empty(ptr, kvalue_size) &&
!is_deleted(ptr, kvalue_size)) {
err = iteration_action((void *)ptr, kvs->ksize,
(void *)(ptr + kvs->ksize),
kvs->vsize, data);
if (err < 0)
goto out;
}
head = next_head(head, kvinram->kmax);
} while (head);
out:
return err;
}
static struct kvstore *kvs_create_sparse_inram(struct metadata *md,
u32 ksize, u32 vsize,
u32 knummax, bool unformatted)
{
struct kvstore_inram *kvs;
u64 kvstore_size, tmp;
if (!vsize || !ksize || !knummax)
return ERR_PTR(-ENOTSUPP);
/* We do not support two or more KVSs at the moment */
if (md->kvs_sparse)
return ERR_PTR(-EBUSY);
kvs = kmalloc(sizeof(*kvs), GFP_NOIO);
if (!kvs)
return ERR_PTR(-ENOMEM);
knummax += (knummax * HASHTABLE_OVERPROV) / 100;
kvstore_size = (knummax * (vsize + ksize));
kvs->store = vmalloc(kvstore_size);
if (!kvs->store) {
kfree(kvs);
return ERR_PTR(-ENOMEM);
}
tmp = kvstore_size;
(void)do_div(tmp, (1024 * 1024));
DMINFO("Space allocated for sparse key value store: %llu.%06llu MB\n",
tmp, kvstore_size - (tmp * (1024 * 1024)));
memset(kvs->store, EMPTY_ENTRY, kvstore_size);
kvs->ckvs.vsize = vsize;
kvs->ckvs.ksize = ksize;
kvs->kmax = knummax;
kvs->ckvs.kvs_insert = kvs_insert_sparse_inram;
kvs->ckvs.kvs_lookup = kvs_lookup_sparse_inram;
kvs->ckvs.kvs_delete = kvs_delete_sparse_inram;
kvs->ckvs.kvs_iterate = kvs_iterate_sparse_inram;
md->kvs_sparse = kvs;
return &(kvs->ckvs);
}
struct metadata_ops metadata_ops_inram = {
.init_meta = init_meta_inram,
.exit_meta = exit_meta_inram,
.kvs_create_linear = kvs_create_linear_inram,
.kvs_create_sparse = kvs_create_sparse_inram,
.alloc_data_block = alloc_data_block_inram,
.inc_refcount = inc_refcount_inram,
.dec_refcount = dec_refcount_inram,
.get_refcount = get_refcount_inram,
.flush_meta = flush_meta_inram,
.flush_bufio_cache = NULL,
};