Skip to content

Commit c28a280

Browse files
committed
Adopted ctz skip-list structure earlier than expected
The primary data structure backing the little fs was planned to be a little ctz based skip-list for O(logn) lookup and O(1) append. Was initially planning to start with a simple linked list of index blocks, but was having trouble implementing the free-list on top of the structure. Went ahead and adopted the skip-list structure since it may have actually been easier.
1 parent 160299d commit c28a280

File tree

4 files changed

+97
-111
lines changed

4 files changed

+97
-111
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ ASM := $(SRC:.c=.s)
1212
ifdef DEBUG
1313
CFLAGS += -O0 -g3
1414
else
15-
CFLAGS += -O2
15+
CFLAGS += -Os
1616
endif
1717
ifdef WORD
1818
CFLAGS += -m$(WORD)

lfs.c

Lines changed: 85 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -26,135 +26,119 @@ static uint32_t lfs_crc(const uint8_t *data, lfs_size_t size, uint32_t crc) {
2626
return crc;
2727
}
2828

29-
lfs_error_t lfs_create(lfs_t *lfs, lfs_bd_t *bd, const struct lfs_bd_ops *ops) {
30-
// TODO rm me, for debugging
31-
memset(lfs, 0, sizeof(lfs_t));
29+
static lfs_error_t lfs_alloc(lfs_t *lfs, lfs_ino_t *ino);
30+
static lfs_error_t lfs_free(lfs_t *lfs, lfs_ino_t ino);
3231

33-
lfs->bd = bd;
34-
lfs->ops = ops;
3532

36-
lfs_error_t err = lfs->ops->info(lfs->bd, &lfs->info);
37-
if (err) {
38-
return err;
39-
}
40-
41-
return 0;
42-
}
33+
// Next index offset
34+
static lfs_off_t lfs_inext(lfs_t *lfs, lfs_off_t ioff) {
35+
ioff += 1;
4336

44-
static lfs_off_t lfs_calc_irem(lfs_t *lfs, lfs_size_t isize) {
45-
lfs_size_t icount = lfs->info.erase_size/4;
46-
47-
if (isize <= icount) {
48-
return isize;
49-
} else {
50-
return ((isize-2) % (icount-1)) + 1;
37+
lfs_size_t wcount = lfs->info.erase_size/4;
38+
while (ioff % wcount == 0) {
39+
ioff += lfs_min(lfs_ctz(ioff/wcount + 1), wcount-1) + 1;
5140
}
52-
}
5341

54-
static lfs_off_t lfs_calc_ioff(lfs_t *lfs, lfs_size_t ioff) {
55-
lfs_size_t icount = lfs->info.erase_size/4;
56-
57-
if (ioff < icount) {
58-
return ioff;
59-
} else {
60-
return ((ioff-1) % (icount-1)) + 1;
61-
}
42+
return ioff;
6243
}
6344

45+
// Find index in index chain given its index offset
6446
static lfs_error_t lfs_ifind(lfs_t *lfs, lfs_ino_t head,
65-
lfs_size_t isize, lfs_off_t ioff, lfs_ino_t *ino) {
66-
if (ioff >= isize) {
67-
return -15;
68-
} else if (isize == 1) {
69-
*ino = head;
70-
return 0;
71-
}
72-
73-
lfs_off_t ilookback = isize - ioff;
74-
lfs_off_t irealoff = lfs_calc_ioff(lfs, ioff);
75-
76-
while (true) {
77-
lfs_size_t irem = lfs_calc_irem(lfs, isize);
78-
if (ilookback <= irem) {
79-
return lfs->ops->read(lfs->bd, (void*)ino,
80-
head, 4*irealoff, 4);
81-
}
82-
83-
lfs_error_t err = lfs->ops->read(lfs->bd, (void*)&head, head, 0, 4);
47+
lfs_size_t icount, lfs_off_t ioff, lfs_ino_t *ino) {
48+
lfs_size_t wcount = lfs->info.erase_size/4;
49+
lfs_off_t iitarget = ioff / wcount;
50+
lfs_off_t iicurrent = (icount-1) / wcount;
51+
52+
while (iitarget != iicurrent) {
53+
lfs_size_t skip = lfs_min(
54+
lfs_min(lfs_ctz(iicurrent+1), wcount-1),
55+
lfs_npw2((iitarget ^ iicurrent)+1)-1);
56+
57+
lfs_error_t err = lfs->ops->read(lfs->bd, (void*)&head,
58+
head, 4*skip, 4);
8459
if (err) {
8560
return err;
8661
}
87-
ilookback -= irem;
88-
isize -= irem;
89-
}
90-
}
9162

92-
static lfs_error_t lfs_alloc(lfs_t *lfs, lfs_ino_t *ino) {
93-
lfs_error_t err = lfs_ifind(lfs, lfs->free.head,
94-
lfs->free.rev[1], lfs->free.rev[0], ino);
95-
if (err) {
96-
return err;
63+
iicurrent -= 1 << skip;
9764
}
9865

99-
err = lfs->ops->erase(lfs->bd, *ino, 0, lfs->info.erase_size);
100-
if (err) {
101-
return err;
102-
}
103-
104-
lfs->free.rev[0] += 1;
105-
return 0;
66+
return lfs->ops->read(lfs->bd, (void*)ino, head, 4*(ioff % wcount), 4);
10667
}
10768

108-
static lfs_error_t lfs_free(lfs_t *lfs, lfs_ino_t ino) {
109-
// TODO handle overflow?
110-
if (lfs->free.rev[1] == 0) {
111-
lfs->free.head = ino;
112-
lfs->free.rev[1]++;
113-
lfs->free.off = lfs->info.erase_size;
114-
return 0;
115-
}
69+
// Append index to index chain, updates head and icount
70+
static lfs_error_t lfs_iappend(lfs_t *lfs, lfs_ino_t *headp,
71+
lfs_size_t *icountp, lfs_ino_t ino) {
72+
lfs_ino_t head = *headp;
73+
lfs_size_t ioff = *icountp - 1;
74+
lfs_size_t wcount = lfs->info.erase_size/4;
11675

117-
if (lfs->free.off == lfs->info.erase_size || !lfs->free.head) {
118-
lfs_ino_t nhead = 0;
76+
ioff += 1;
77+
78+
while (ioff % wcount == 0) {
79+
lfs_ino_t nhead;
11980
lfs_error_t err = lfs_alloc(lfs, &nhead);
12081
if (err) {
12182
return err;
12283
}
12384

124-
if (lfs->free.off == lfs->info.erase_size) {
125-
err = lfs->ops->write(lfs->bd, (void*)&lfs->free.head, nhead, 0, 4);
85+
lfs_off_t skips = lfs_min(lfs_ctz(ioff/wcount + 1), wcount-1) + 1;
86+
for (lfs_off_t i = 0; i < skips; i++) {
87+
err = lfs->ops->write(lfs->bd, (void*)&head, nhead, 4*i, 4);
12688
if (err) {
12789
return err;
12890
}
129-
} else {
130-
for (lfs_off_t i = 0; i < lfs->free.off; i += 4) {
131-
lfs_ino_t ino;
132-
lfs_error_t err = lfs->ops->read(lfs->bd, (void*)&ino,
133-
lfs->free.phead, i, 4);
134-
if (err) {
135-
return err;
136-
}
13791

138-
err = lfs->ops->write(lfs->bd, (void*)&ino,
139-
nhead, i, 4);
92+
if (head && i != skips-1) {
93+
err = lfs->ops->read(lfs->bd, (void*)&head, head, 4*i, 4);
14094
if (err) {
14195
return err;
14296
}
14397
}
14498
}
14599

146-
lfs->free.head = nhead;
147-
lfs->free.off = 4;
100+
ioff += skips;
101+
head = nhead;
148102
}
149103

150104
lfs_error_t err = lfs->ops->write(lfs->bd, (void*)&ino,
151-
lfs->free.head, lfs->free.off, 4);
105+
head, 4*(ioff % wcount), 4);
106+
if (err) {
107+
return err;
108+
}
109+
110+
*headp = head;
111+
*icountp = ioff + 1;
112+
return 0;
113+
}
114+
115+
// Memory managment
116+
static lfs_error_t lfs_alloc(lfs_t *lfs, lfs_ino_t *ino) {
117+
lfs_error_t err = lfs_ifind(lfs, lfs->free.head,
118+
lfs->free.icount, lfs->free.ioff, ino);
152119
if (err) {
153120
return err;
154121
}
155122

156-
lfs->free.off += 4;
157-
lfs->free.rev[1] += 1;
123+
lfs->free.ioff = lfs_inext(lfs, lfs->free.ioff);
124+
125+
return lfs->ops->erase(lfs->bd, *ino, 0, lfs->info.erase_size);
126+
}
127+
128+
static lfs_error_t lfs_free(lfs_t *lfs, lfs_ino_t ino) {
129+
return lfs_iappend(lfs, &lfs->free.head, &lfs->free.icount, ino);
130+
}
131+
132+
// Little filesystem operations
133+
lfs_error_t lfs_create(lfs_t *lfs, lfs_bd_t *bd, const struct lfs_bd_ops *ops) {
134+
lfs->bd = bd;
135+
lfs->ops = ops;
136+
137+
lfs_error_t err = lfs->ops->info(lfs->bd, &lfs->info);
138+
if (err) {
139+
return err;
140+
}
141+
158142
return 0;
159143
}
160144

@@ -165,22 +149,21 @@ lfs_error_t lfs_format(lfs_t *lfs) {
165149
return err;
166150
}
167151

168-
err = lfs->ops->erase(lfs->bd, 0, 0, info.erase_size);
152+
err = lfs->ops->erase(lfs->bd, 0, 0, 5*info.erase_size);
169153
if (err) {
170154
return err;
171155
}
172156

173-
// TODO erase what could be misinterpreted (pairs of blocks)
157+
// TODO make sure that erase clobbered blocks
174158

175159
{ // Create free list
176-
lfs->free.rev[0] = 0;
177-
lfs->free.rev[1] = 0;
178-
lfs->free.phead = 0;
179-
lfs->free.head = 0;
180-
lfs->free.off = 0;
160+
lfs->free.head = 4;
161+
lfs->free.ioff = 1;
162+
lfs->free.icount = 1;
163+
lfs->free.rev = 1;
181164

182165
lfs_size_t block_count = lfs->info.total_size / lfs->info.erase_size;
183-
for (lfs_ino_t i = 4; i < block_count; i++) {
166+
for (lfs_ino_t i = 5; i < block_count; i++) {
184167
lfs_error_t err = lfs_free(lfs, i);
185168
if (err) {
186169
return err;
@@ -194,10 +177,8 @@ lfs_error_t lfs_format(lfs_t *lfs) {
194177
lfs_word_t rev;
195178
lfs_size_t len;
196179
lfs_ino_t tail[2];
197-
lfs_word_t free_rev[2];
198-
lfs_ino_t free_ino;
199-
} header = {1, 0, {0, 0},
200-
{lfs->free.rev[0], lfs->free.rev[1]}, lfs->free.head};
180+
struct lfs_free_list free;
181+
} header = {1, 0, {0, 0}, lfs->free};
201182
err = lfs->ops->write(lfs->bd, (void*)&header, 2, 0, sizeof(header));
202183
if (err) {
203184
return err;
@@ -227,12 +208,10 @@ lfs_error_t lfs_format(lfs_t *lfs) {
227208
lfs_word_t rev;
228209
lfs_word_t len;
229210
lfs_word_t tail[2];
230-
lfs_word_t free_head;
231-
lfs_word_t free_end;
232-
lfs_ino_t free_ino;
211+
struct lfs_free_list free;
233212
char magic[4];
234213
struct lfs_bd_info info;
235-
} header = {1, 0, {2, 3}, 0, 0, 0, {"lfs"}, info};
214+
} header = {1, 0, {2, 3}, {0}, {"lfs"}, info};
236215
err = lfs->ops->write(lfs->bd, (void*)&header, 0, 0, sizeof(header));
237216
if (err) {
238217
return err;

lfs.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111
#include "lfs_bd.h"
1212

1313
struct lfs_free_list {
14-
lfs_word_t rev[2];
15-
lfs_ino_t phead;
1614
lfs_ino_t head;
17-
lfs_ino_t tip;
18-
lfs_off_t off;
15+
lfs_word_t ioff;
16+
lfs_word_t icount;
17+
lfs_word_t rev;
1918
};
2019

2120
typedef struct lfs {

lfs_config.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,12 @@ static inline lfs_word_t lfs_min(lfs_word_t a, lfs_word_t b) {
3434
return (a < b) ? a : b;
3535
}
3636

37+
static inline lfs_word_t lfs_ctz(lfs_word_t a) {
38+
return __builtin_ctz(a);
39+
}
40+
41+
static inline lfs_word_t lfs_npw2(lfs_word_t a) {
42+
return 32 - __builtin_clz(a-1);
43+
}
44+
3745
#endif

0 commit comments

Comments
 (0)