Skip to content

Commit 84a5764

Browse files
committed
Restructured the major interfaces of the filesystem
1 parent f566846 commit 84a5764

File tree

8 files changed

+470
-433
lines changed

8 files changed

+470
-433
lines changed

emubd/lfs_emubd.c

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818

1919
// Block device emulated on existing filesystem
20-
lfs_error_t lfs_emubd_create(lfs_emubd_t *emu, const char *path) {
20+
int lfs_emubd_create(lfs_emubd_t *emu, const char *path) {
2121
memset(&emu->info, 0, sizeof(emu->info));
2222
memset(&emu->stats, 0, sizeof(emu->stats));
2323

@@ -41,8 +41,8 @@ lfs_error_t lfs_emubd_create(lfs_emubd_t *emu, const char *path) {
4141
return err;
4242
}
4343

44-
emu->info.read_size = lfs_cfg_getu(&cfg, "read_size", 0);
45-
emu->info.write_size = lfs_cfg_getu(&cfg, "write_size", 0);
44+
emu->info.read_size = lfs_cfg_getu(&cfg, "read_size", 0);
45+
emu->info.prog_size = lfs_cfg_getu(&cfg, "prog_size", 0);
4646
emu->info.erase_size = lfs_cfg_getu(&cfg, "erase_size", 0);
4747
emu->info.total_size = lfs_cfg_getu(&cfg, "total_size", 0);
4848

@@ -55,23 +55,24 @@ void lfs_emubd_destroy(lfs_emubd_t *emu) {
5555
free(emu->path);
5656
}
5757

58-
lfs_error_t lfs_emubd_read(lfs_emubd_t *emu, uint8_t *buffer,
59-
lfs_ino_t ino, lfs_off_t off, lfs_size_t size) {
58+
int lfs_emubd_read(lfs_emubd_t *emu, lfs_block_t block,
59+
lfs_off_t off, lfs_size_t size, void *buffer) {
60+
uint8_t *data = buffer;
6061

6162
// Check if read is valid
6263
if (!(off % emu->info.read_size == 0 &&
6364
size % emu->info.read_size == 0 &&
64-
((lfs_lsize_t)ino*emu->info.erase_size + off + size
65+
((uint64_t)block*emu->info.erase_size + off + size
6566
< emu->info.total_size))) {
6667
return -EINVAL;
6768
}
6869

6970
// Zero out buffer for debugging
70-
memset(buffer, 0, size);
71+
memset(data, 0, size);
7172

7273
// Iterate over blocks until enough data is read
7374
while (size > 0) {
74-
snprintf(emu->child, LFS_NAME_MAX, "%d", ino);
75+
snprintf(emu->child, LFS_NAME_MAX, "%d", block);
7576
size_t count = lfs_min(emu->info.erase_size - off, size);
7677

7778
FILE *f = fopen(emu->path, "rb");
@@ -85,7 +86,7 @@ lfs_error_t lfs_emubd_read(lfs_emubd_t *emu, uint8_t *buffer,
8586
return -errno;
8687
}
8788

88-
size_t res = fread(buffer, 1, count, f);
89+
size_t res = fread(data, 1, count, f);
8990
if (res < count && !feof(f)) {
9091
return -errno;
9192
}
@@ -97,29 +98,30 @@ lfs_error_t lfs_emubd_read(lfs_emubd_t *emu, uint8_t *buffer,
9798
}
9899

99100
size -= count;
100-
buffer += count;
101-
ino += 1;
101+
data += count;
102+
block += 1;
102103
off = 0;
103104
}
104105

105106
emu->stats.read_count += 1;
106107
return 0;
107108
}
108109

109-
lfs_error_t lfs_emubd_write(lfs_emubd_t *emu, const uint8_t *buffer,
110-
lfs_ino_t ino, lfs_off_t off, lfs_size_t size) {
110+
int lfs_emubd_prog(lfs_emubd_t *emu, lfs_block_t block,
111+
lfs_off_t off, lfs_size_t size, const void *buffer) {
112+
const uint8_t *data = buffer;
111113

112114
// Check if write is valid
113-
if (!(off % emu->info.write_size == 0 &&
114-
size % emu->info.write_size == 0 &&
115-
((lfs_lsize_t)ino*emu->info.erase_size + off + size
115+
if (!(off % emu->info.prog_size == 0 &&
116+
size % emu->info.prog_size == 0 &&
117+
((uint64_t)block*emu->info.erase_size + off + size
116118
< emu->info.total_size))) {
117119
return -EINVAL;
118120
}
119121

120122
// Iterate over blocks until enough data is read
121123
while (size > 0) {
122-
snprintf(emu->child, LFS_NAME_MAX, "%d", ino);
124+
snprintf(emu->child, LFS_NAME_MAX, "%d", block);
123125
size_t count = lfs_min(emu->info.erase_size - off, size);
124126

125127
FILE *f = fopen(emu->path, "r+b");
@@ -135,7 +137,7 @@ lfs_error_t lfs_emubd_write(lfs_emubd_t *emu, const uint8_t *buffer,
135137
return -errno;
136138
}
137139

138-
size_t res = fwrite(buffer, 1, count, f);
140+
size_t res = fwrite(data, 1, count, f);
139141
if (res < count) {
140142
return -errno;
141143
}
@@ -146,29 +148,29 @@ lfs_error_t lfs_emubd_write(lfs_emubd_t *emu, const uint8_t *buffer,
146148
}
147149

148150
size -= count;
149-
buffer += count;
150-
ino += 1;
151+
data += count;
152+
block += 1;
151153
off = 0;
152154
}
153155

154-
emu->stats.write_count += 1;
156+
emu->stats.prog_count += 1;
155157
return 0;
156158
}
157159

158-
lfs_error_t lfs_emubd_erase(lfs_emubd_t *emu,
159-
lfs_ino_t ino, lfs_off_t off, lfs_size_t size) {
160+
int lfs_emubd_erase(lfs_emubd_t *emu, lfs_block_t block,
161+
lfs_off_t off, lfs_size_t size) {
160162

161163
// Check if erase is valid
162164
if (!(off % emu->info.erase_size == 0 &&
163165
size % emu->info.erase_size == 0 &&
164-
((lfs_lsize_t)ino*emu->info.erase_size + off + size
166+
((uint64_t)block*emu->info.erase_size + off + size
165167
< emu->info.total_size))) {
166168
return -EINVAL;
167169
}
168170

169171
// Iterate and erase blocks
170172
while (size > 0) {
171-
snprintf(emu->child, LFS_NAME_MAX, "%d", ino);
173+
snprintf(emu->child, LFS_NAME_MAX, "%d", block);
172174
struct stat st;
173175
int err = stat(emu->path, &st);
174176
if (err && errno != ENOENT) {
@@ -183,57 +185,57 @@ lfs_error_t lfs_emubd_erase(lfs_emubd_t *emu,
183185
}
184186

185187
size -= emu->info.erase_size;
186-
ino += 1;
188+
block += 1;
187189
off = 0;
188190
}
189191

190192
emu->stats.erase_count += 1;
191193
return 0;
192194
}
193195

194-
lfs_error_t lfs_emubd_sync(lfs_emubd_t *emu) {
196+
int lfs_emubd_sync(lfs_emubd_t *emu) {
195197
// Always in sync
196198
return 0;
197199
}
198200

199-
lfs_error_t lfs_emubd_info(lfs_emubd_t *emu, struct lfs_bd_info *info) {
201+
int lfs_emubd_info(lfs_emubd_t *emu, struct lfs_bd_info *info) {
200202
*info = emu->info;
201203
return 0;
202204
}
203205

204-
lfs_error_t lfs_emubd_stats(lfs_emubd_t *emu, struct lfs_bd_stats *stats) {
206+
int lfs_emubd_stats(lfs_emubd_t *emu, struct lfs_bd_stats *stats) {
205207
*stats = emu->stats;
206208
return 0;
207209
}
208210

209211

210212
// Wrappers for void*s
211-
static lfs_error_t lfs_emubd_bd_read(void *bd, uint8_t *buffer,
212-
lfs_ino_t ino, lfs_off_t off, lfs_size_t size) {
213-
return lfs_emubd_read((lfs_emubd_t*)bd, buffer, ino, off, size);
213+
static int lfs_emubd_bd_read(void *bd, lfs_block_t block,
214+
lfs_off_t off, lfs_size_t size, void *buffer) {
215+
return lfs_emubd_read((lfs_emubd_t*)bd, block, off, size, buffer);
214216
}
215217

216-
static lfs_error_t lfs_emubd_bd_write(void *bd, const uint8_t *buffer,
217-
lfs_ino_t ino, lfs_off_t off, lfs_size_t size) {
218-
return lfs_emubd_write((lfs_emubd_t*)bd, buffer, ino, off, size);
218+
static int lfs_emubd_bd_prog(void *bd, lfs_block_t block,
219+
lfs_off_t off, lfs_size_t size, const void *buffer) {
220+
return lfs_emubd_prog((lfs_emubd_t*)bd, block, off, size, buffer);
219221
}
220222

221-
static lfs_error_t lfs_emubd_bd_erase(void *bd,
222-
lfs_ino_t ino, lfs_off_t off, lfs_size_t size) {
223-
return lfs_emubd_erase((lfs_emubd_t*)bd, ino, off, size);
223+
static int lfs_emubd_bd_erase(void *bd, lfs_block_t block,
224+
lfs_off_t off, lfs_size_t size) {
225+
return lfs_emubd_erase((lfs_emubd_t*)bd, block, off, size);
224226
}
225227

226-
static lfs_error_t lfs_emubd_bd_sync(void *bd) {
228+
static int lfs_emubd_bd_sync(void *bd) {
227229
return lfs_emubd_sync((lfs_emubd_t*)bd);
228230
}
229231

230-
static lfs_error_t lfs_emubd_bd_info(void *bd, struct lfs_bd_info *info) {
232+
static int lfs_emubd_bd_info(void *bd, struct lfs_bd_info *info) {
231233
return lfs_emubd_info((lfs_emubd_t*)bd, info);
232234
}
233235

234236
const struct lfs_bd_ops lfs_emubd_ops = {
235237
.read = lfs_emubd_bd_read,
236-
.write = lfs_emubd_bd_write,
238+
.prog = lfs_emubd_bd_prog,
237239
.erase = lfs_emubd_bd_erase,
238240
.sync = lfs_emubd_bd_sync,
239241
.info = lfs_emubd_bd_info,

emubd/lfs_emubd.h

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
#define LFS_EMUBD_H
99

1010
#include "lfs_config.h"
11+
#include "lfs_util.h"
1112
#include "lfs_bd.h"
1213

1314

1415
// Stats for debugging and optimization
1516
struct lfs_bd_stats {
16-
lfs_lword_t read_count;
17-
lfs_lword_t write_count;
18-
lfs_lword_t erase_count;
17+
uint64_t read_count;
18+
uint64_t prog_count;
19+
uint64_t erase_count;
1920
};
2021

2122
// The emu bd state
@@ -28,40 +29,40 @@ typedef struct lfs_emubd {
2829

2930

3031
// Create a block device using path for the directory to store blocks
31-
lfs_error_t lfs_emubd_create(lfs_emubd_t *emu, const char *path);
32+
int lfs_emubd_create(lfs_emubd_t *emu, const char *path);
3233

3334
// Clean up memory associated with emu block device
3435
void lfs_emubd_destroy(lfs_emubd_t *emu);
3536

3637
// Read a block
37-
lfs_error_t lfs_emubd_read(lfs_emubd_t *bd, uint8_t *buffer,
38-
lfs_ino_t ino, lfs_off_t off, lfs_size_t size);
38+
int lfs_emubd_read(lfs_emubd_t *bd, lfs_block_t block,
39+
lfs_off_t off, lfs_size_t size, void *buffer);
3940

4041
// Program a block
4142
//
4243
// The block must have previously been erased.
43-
lfs_error_t lfs_emubd_write(lfs_emubd_t *bd, const uint8_t *buffer,
44-
lfs_ino_t ino, lfs_off_t off, lfs_size_t size);
44+
int lfs_emubd_prog(lfs_emubd_t *bd, lfs_block_t block,
45+
lfs_off_t off, lfs_size_t size, const void *buffer);
4546

4647
// Erase a block
4748
//
4849
// A block must be erased before being programmed. The
4950
// state of an erased block is undefined.
50-
lfs_error_t lfs_emubd_erase(lfs_emubd_t *bd,
51-
lfs_ino_t ino, lfs_off_t off, lfs_size_t size);
51+
int lfs_emubd_erase(lfs_emubd_t *bd, lfs_block_t block,
52+
lfs_off_t off, lfs_size_t size);
5253

5354
// Sync the block device
54-
lfs_error_t lfs_emubd_sync(lfs_emubd_t *bd);
55+
int lfs_emubd_sync(lfs_emubd_t *bd);
5556

5657
// Get a description of the block device
5758
//
5859
// Any unknown information may be left unmodified
59-
lfs_error_t lfs_emubd_info(lfs_emubd_t *bd, struct lfs_bd_info *info);
60+
int lfs_emubd_info(lfs_emubd_t *bd, struct lfs_bd_info *info);
6061

6162
// Get stats of operations on the block device
6263
//
6364
// Used for debugging and optimizations
64-
lfs_error_t lfs_emubd_stats(lfs_emubd_t *bd, struct lfs_bd_stats *stats);
65+
int lfs_emubd_stats(lfs_emubd_t *bd, struct lfs_bd_stats *stats);
6566

6667
// Block device operations
6768
extern const struct lfs_bd_ops lfs_emubd_ops;

0 commit comments

Comments
 (0)