-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnbdftl.cpp
139 lines (117 loc) · 3.55 KB
/
nbdftl.cpp
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
/*
NBDFTL - NBD driver for FTL testing on the host
Copyright (c) 2024 Earle F. Philhower, III <earlephilhower@yahoo.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this program. If not, see https://www.gnu.org/licenses/
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <cassert>
#include <list>
#include <map>
#include "SPIFTL.h"
#include "FlashInterfaceRAM.h"
#define NBDKIT_API_VERSION 2
#include <nbdkit-plugin.h>
#define THREAD_MODEL NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS
FlashInterfaceRAM fi(1 * 1024 * 1024);
SPIFTL ftl(&fi);
int flashLBAs;
uint8_t *lbaCopy;
static void ftl_load(void) {
ftl.start();
ftl.check();
flashLBAs = ftl.lbaCount();
lbaCopy = (uint8_t *)calloc(flashLBAs, 512);
FILE *f = fopen("lba.bin", "rb");
if (f) {
fread(lbaCopy, 512, flashLBAs, f);
fclose(f);
}
ftl.check();
}
static void ftl_close(void *handle) {
ftl.persist();
FILE *f = fopen("lba.bin", "wb");
if (f) {
fwrite(lbaCopy, 512, flashLBAs, f);
fclose(f);
}
}
static void *ftl_open(int readonly) {
return NBDKIT_HANDLE_NOT_NEEDED;
}
static int64_t ftl_get_size(void *handle) {
return (int64_t)flashLBAs * 512;
}
static int ftl_pwrite(void *handle, const void *buf, uint32_t count, uint64_t offset, uint32_t flags) {
uint8_t *b = (uint8_t*)buf;
while (count) {
int lba = offset / 512;
ftl.write(lba, b);
memcpy(lbaCopy + lba * 512, b, 512);
for (int i = 0; i < flashLBAs; i++) {
uint8_t tmp[512];
ftl.read(i, tmp);
if (memcmp(tmp, lbaCopy + i * 512, 512)) {
fprintf(stderr, "ERROR, lba mismatch %d\n", i);
}
}
b += 512;
offset += 512;
count -= 512;
}
return 0;
}
static int ftl_pread(void *handle, void *buf, uint32_t count, uint64_t offset, uint32_t flags) {
uint8_t *b = (uint8_t*)buf;
while (count) {
int lba = offset / 512;
ftl.read(lba, b);
offset += 512;
count -= 512;
}
return 0;
}
static int ftl_block_size(void *handle, uint32_t *minimum, uint32_t *preferred, uint32_t *maximum) {
*minimum = 512;
*preferred = 512;
*maximum = 512;
return 0;
}
static int ftl_can_trim(void *handle) {
return 1;
}
static int ftl_trim(void *handle, uint32_t count, uint64_t offset, uint32_t flags) {
while (count) {
int lba = offset / 512;
ftl.trim(lba);
offset += 512;
count -= 512;
}
return 1;
}
static struct nbdkit_plugin plugin = {
.name = "spiftl",
.version = "1.0",
.load = ftl_load,
.open = ftl_open,
.close = ftl_close,
.get_size = ftl_get_size,
.can_trim = ftl_can_trim,
.pread = ftl_pread,
.pwrite = ftl_pwrite,
.trim = ftl_trim,
.block_size = ftl_block_size
};
NBDKIT_REGISTER_PLUGIN(plugin)