Skip to content

Commit 9bfeb69

Browse files
committed
[JFFS2] Switch to using an array of jffs2_raw_node_refs instead of a list.
This allows us to drop another pointer from the struct jffs2_raw_node_ref, shrinking it to 8 bytes on 32-bit machines (if the TEST_TOTLEN) paranoia check is turned off, which will be committed soon). Signed-off-by: David Woodhouse <dwmw2@infradead.org>
1 parent f75e509 commit 9bfeb69

9 files changed

Lines changed: 351 additions & 225 deletions

File tree

‎fs/jffs2/erase.c‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -285,20 +285,25 @@ static inline void jffs2_remove_node_refs_from_ino_list(struct jffs2_sb_info *c,
285285

286286
void jffs2_free_jeb_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
287287
{
288-
struct jffs2_raw_node_ref *ref;
288+
struct jffs2_raw_node_ref *block, *ref;
289289
D1(printk(KERN_DEBUG "Freeing all node refs for eraseblock offset 0x%08x\n", jeb->offset));
290-
while(jeb->first_node) {
291-
ref = jeb->first_node;
292-
jeb->first_node = ref->next_phys;
293290

294-
/* Remove from the inode-list */
295-
if (ref->next_in_ino)
291+
block = ref = jeb->first_node;
292+
293+
while (ref) {
294+
if (ref->flash_offset == REF_LINK_NODE) {
295+
ref = ref->next_in_ino;
296+
jffs2_free_refblock(block);
297+
block = ref;
298+
continue;
299+
}
300+
if (ref->flash_offset != REF_EMPTY_NODE && ref->next_in_ino)
296301
jffs2_remove_node_refs_from_ino_list(c, ref, jeb);
297302
/* else it was a non-inode node or already removed, so don't bother */
298303

299-
__jffs2_free_raw_node_ref(ref);
304+
ref++;
300305
}
301-
jeb->last_node = NULL;
306+
jeb->first_node = jeb->last_node = NULL;
302307
}
303308

304309
static int jffs2_block_check_erase(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t *bad_offset)

‎fs/jffs2/jffs2_fs_sb.h‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ struct jffs2_inodirty;
2626
struct jffs2_sb_info {
2727
struct mtd_info *mtd;
2828

29-
struct jffs2_raw_node_ref *refs;
30-
int reserved_refs;
31-
3229
uint32_t highest_ino;
3330
uint32_t checked_ino;
3431

‎fs/jffs2/malloc.c‎

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ int __init jffs2_create_slab_caches(void)
5757
if (!tmp_dnode_info_slab)
5858
goto err;
5959

60-
raw_node_ref_slab = kmem_cache_create("jffs2_raw_node_ref",
61-
sizeof(struct jffs2_raw_node_ref),
60+
raw_node_ref_slab = kmem_cache_create("jffs2_refblock",
61+
sizeof(struct jffs2_raw_node_ref) * (REFS_PER_BLOCK + 1),
6262
0, 0, NULL, NULL);
6363
if (!raw_node_ref_slab)
6464
goto err;
@@ -190,38 +190,65 @@ void jffs2_free_tmp_dnode_info(struct jffs2_tmp_dnode_info *x)
190190
kmem_cache_free(tmp_dnode_info_slab, x);
191191
}
192192

193+
struct jffs2_raw_node_ref *jffs2_alloc_refblock(void)
194+
{
195+
struct jffs2_raw_node_ref *ret;
196+
197+
ret = kmem_cache_alloc(raw_node_ref_slab, GFP_KERNEL);
198+
if (ret) {
199+
int i = 0;
200+
for (i=0; i < REFS_PER_BLOCK; i++) {
201+
ret[i].flash_offset = REF_EMPTY_NODE;
202+
ret[i].next_in_ino = NULL;
203+
}
204+
ret[i].flash_offset = REF_LINK_NODE;
205+
ret[i].next_in_ino = NULL;
206+
}
207+
return ret;
208+
}
209+
193210
int jffs2_prealloc_raw_node_refs(struct jffs2_sb_info *c,
194211
struct jffs2_eraseblock *jeb, int nr)
195212
{
196-
struct jffs2_raw_node_ref *p = c->refs;
213+
struct jffs2_raw_node_ref **p, *ref;
214+
int i = nr;
197215

198216
dbg_memalloc("%d\n", nr);
199217

200-
while (nr && p) {
201-
p = p->next_in_ino;
202-
nr--;
203-
}
204-
while (nr) {
205-
p = __jffs2_alloc_raw_node_ref();
206-
if (!p)
207-
return -ENOMEM;
208-
p->next_in_ino = c->refs;
209-
c->refs = p;
210-
nr--;
218+
p = &jeb->last_node;
219+
ref = *p;
220+
221+
dbg_memalloc("Reserving %d refs for block @0x%08x\n", nr, jeb->offset);
222+
223+
/* If jeb->last_node is really a valid node then skip over it */
224+
if (ref && ref->flash_offset != REF_EMPTY_NODE)
225+
ref++;
226+
227+
while (i) {
228+
if (!ref) {
229+
dbg_memalloc("Allocating new refblock linked from %p\n", p);
230+
ref = *p = jffs2_alloc_refblock();
231+
if (!ref)
232+
return -ENOMEM;
233+
}
234+
if (ref->flash_offset == REF_LINK_NODE) {
235+
p = &ref->next_in_ino;
236+
ref = *p;
237+
continue;
238+
}
239+
i--;
240+
ref++;
211241
}
212-
c->reserved_refs = nr;
213-
return 0;
214-
}
242+
jeb->allocated_refs = nr;
215243

216-
struct jffs2_raw_node_ref *__jffs2_alloc_raw_node_ref(void)
217-
{
218-
struct jffs2_raw_node_ref *ret;
219-
ret = kmem_cache_alloc(raw_node_ref_slab, GFP_KERNEL);
220-
dbg_memalloc("%p\n", ret);
221-
return ret;
244+
dbg_memalloc("Reserved %d refs for block @0x%08x, last_node is %p (%08x,%p)\n",
245+
nr, jeb->offset, jeb->last_node, jeb->last_node->flash_offset,
246+
jeb->last_node->next_in_ino);
247+
248+
return 0;
222249
}
223250

224-
void __jffs2_free_raw_node_ref(struct jffs2_raw_node_ref *x)
251+
void jffs2_free_refblock(struct jffs2_raw_node_ref *x)
225252
{
226253
dbg_memalloc("%p\n", x);
227254
kmem_cache_free(raw_node_ref_slab, x);

‎fs/jffs2/nodelist.c‎

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -954,18 +954,16 @@ void jffs2_free_raw_node_refs(struct jffs2_sb_info *c)
954954
for (i=0; i<c->nr_blocks; i++) {
955955
this = c->blocks[i].first_node;
956956
while (this) {
957-
next = this->next_phys;
958-
__jffs2_free_raw_node_ref(this);
957+
if (this[REFS_PER_BLOCK].flash_offset == REF_LINK_NODE)
958+
next = this[REFS_PER_BLOCK].next_in_ino;
959+
else
960+
next = NULL;
961+
962+
jffs2_free_refblock(this);
959963
this = next;
960964
}
961965
c->blocks[i].first_node = c->blocks[i].last_node = NULL;
962966
}
963-
this = c->refs;
964-
while (this) {
965-
next = this->next_in_ino;
966-
__jffs2_free_raw_node_ref(this);
967-
this = next;
968-
}
969967
}
970968

971969
struct jffs2_node_frag *jffs2_lookup_node_frag(struct rb_root *fragtree, uint32_t offset)
@@ -1060,32 +1058,37 @@ struct jffs2_raw_node_ref *jffs2_link_node_ref(struct jffs2_sb_info *c,
10601058
{
10611059
struct jffs2_raw_node_ref *ref;
10621060

1063-
/* These will be preallocated _very_ shortly. */
1064-
ref = c->refs;
1065-
if (!c->refs) {
1066-
JFFS2_WARNING("Using non-preallocated refs!\n");
1067-
ref = __jffs2_alloc_raw_node_ref();
1068-
BUG_ON(!ref);
1069-
WARN_ON(1);
1070-
} else {
1071-
c->refs = ref->next_in_ino;
1061+
BUG_ON(!jeb->allocated_refs);
1062+
jeb->allocated_refs--;
1063+
1064+
ref = jeb->last_node;
1065+
1066+
dbg_noderef("Last node at %p is (%08x,%p)\n", ref, ref->flash_offset,
1067+
ref->next_in_ino);
1068+
1069+
while (ref->flash_offset != REF_EMPTY_NODE) {
1070+
if (ref->flash_offset == REF_LINK_NODE)
1071+
ref = ref->next_in_ino;
1072+
else
1073+
ref++;
10721074
}
10731075

1074-
ref->next_phys = NULL;
1076+
dbg_noderef("New ref is %p (%08x becomes %08x,%p) len 0x%x\n", ref,
1077+
ref->flash_offset, ofs, ref->next_in_ino, len);
1078+
10751079
ref->flash_offset = ofs;
10761080

1077-
if (!jeb->first_node)
1081+
if (!jeb->first_node) {
10781082
jeb->first_node = ref;
1079-
if (jeb->last_node) {
1080-
jeb->last_node->next_phys = ref;
1081-
#ifdef TEST_TOTLEN
1082-
if (ref_offset(jeb->last_node) + jeb->last_node->__totlen != ref_offset(ref)) {
1083-
printk(KERN_CRIT "Adding new ref %p at (0x%08x-0x%08x) not immediately after previous (0x%08x-0x%08x)\n",
1084-
ref, ref_offset(ref), ref_offset(ref)+ref->__totlen,
1085-
ref_offset(jeb->last_node), ref_offset(jeb->last_node)+jeb->last_node->__totlen);
1086-
WARN_ON(1);
1087-
}
1088-
#endif
1083+
BUG_ON(ref_offset(ref) != jeb->offset);
1084+
} else if (unlikely(ref_offset(ref) != jeb->offset + c->sector_size - jeb->free_size)) {
1085+
uint32_t last_len = ref_totlen(c, jeb, jeb->last_node);
1086+
1087+
JFFS2_ERROR("Adding new ref %p at (0x%08x-0x%08x) not immediately after previous (0x%08x-0x%08x)\n",
1088+
ref, ref_offset(ref), ref_offset(ref)+len,
1089+
ref_offset(jeb->last_node),
1090+
ref_offset(jeb->last_node)+last_len);
1091+
BUG();
10891092
}
10901093
jeb->last_node = ref;
10911094

@@ -1130,12 +1133,13 @@ int jffs2_scan_dirty_space(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb
11301133
{
11311134
if (!size)
11321135
return 0;
1133-
if (size > c->sector_size - jeb->used_size) {
1134-
printk(KERN_CRIT "Dirty space 0x%x larger then used_size 0x%x (wasted 0x%x)\n",
1135-
size, jeb->used_size, jeb->wasted_size);
1136+
if (unlikely(size > jeb->free_size)) {
1137+
printk(KERN_CRIT "Dirty space 0x%x larger then free_size 0x%x (wasted 0x%x)\n",
1138+
size, jeb->free_size, jeb->wasted_size);
11361139
BUG();
11371140
}
1138-
if (jeb->last_node && ref_obsolete(jeb->last_node)) {
1141+
/* REF_EMPTY_NODE is !obsolete, so that works OK */
1142+
if (ref_obsolete(jeb->last_node)) {
11391143
#ifdef TEST_TOTLEN
11401144
jeb->last_node->__totlen += size;
11411145
#endif
@@ -1168,7 +1172,7 @@ static inline uint32_t __ref_totlen(struct jffs2_sb_info *c,
11681172
jeb = &c->blocks[ref->flash_offset / c->sector_size];
11691173

11701174
/* Last node in block. Use free_space */
1171-
if (ref != jeb->last_node) {
1175+
if (unlikely(ref != jeb->last_node)) {
11721176
printk(KERN_CRIT "ref %p @0x%08x is not jeb->last_node (%p @0x%08x)\n",
11731177
ref, ref_offset(ref), jeb->last_node, jeb->last_node?ref_offset(jeb->last_node):0);
11741178
BUG();
@@ -1183,17 +1187,13 @@ uint32_t __jffs2_ref_totlen(struct jffs2_sb_info *c, struct jffs2_eraseblock *je
11831187
{
11841188
uint32_t ret;
11851189

1186-
#if CONFIG_JFFS2_FS_DEBUG > 0
1187-
if (jeb && jeb != &c->blocks[ref->flash_offset / c->sector_size]) {
1188-
printk(KERN_CRIT "ref_totlen called with wrong block -- at 0x%08x instead of 0x%08x; ref 0x%08x\n",
1189-
jeb->offset, c->blocks[ref->flash_offset / c->sector_size].offset, ref_offset(ref));
1190-
BUG();
1191-
}
1192-
#endif
1193-
11941190
ret = __ref_totlen(c, jeb, ref);
1191+
11951192
#ifdef TEST_TOTLEN
1196-
if (ret != ref->__totlen) {
1193+
if (unlikely(ret != ref->__totlen)) {
1194+
if (!jeb)
1195+
jeb = &c->blocks[ref->flash_offset / c->sector_size];
1196+
11971197
printk(KERN_CRIT "Totlen for ref at %p (0x%08x-0x%08x) miscalculated as 0x%x instead of %x\n",
11981198
ref, ref_offset(ref), ref_offset(ref)+ref->__totlen,
11991199
ret, ref->__totlen);
@@ -1204,13 +1204,14 @@ uint32_t __jffs2_ref_totlen(struct jffs2_sb_info *c, struct jffs2_eraseblock *je
12041204
printk(KERN_CRIT "No next ref. jeb->last_node is %p\n", jeb->last_node);
12051205

12061206
printk(KERN_CRIT "jeb->wasted_size %x, dirty_size %x, used_size %x, free_size %x\n", jeb->wasted_size, jeb->dirty_size, jeb->used_size, jeb->free_size);
1207-
ret = ref->__totlen;
1208-
if (!jeb)
1209-
jeb = &c->blocks[ref->flash_offset / c->sector_size];
1207+
12101208
#if defined(JFFS2_DBG_DUMPS) || defined(JFFS2_DBG_PARANOIA_CHECKS)
12111209
__jffs2_dbg_dump_node_refs_nolock(c, jeb);
12121210
#endif
1211+
12131212
WARN_ON(1);
1213+
1214+
ret = ref->__totlen;
12141215
}
12151216
#endif /* TEST_TOTLEN */
12161217
return ret;

‎fs/jffs2/nodelist.h‎

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,36 @@ struct jffs2_raw_node_ref
8080
for this object. If this _is_ the last, it points to the inode_cache,
8181
xattr_ref or xattr_datum instead. The common part of those structures
8282
has NULL in the first word. See jffs2_raw_ref_to_ic() below */
83-
struct jffs2_raw_node_ref *next_phys;
8483
uint32_t flash_offset;
8584
#define TEST_TOTLEN
8685
#ifdef TEST_TOTLEN
8786
uint32_t __totlen; /* This may die; use ref_totlen(c, jeb, ) below */
8887
#endif
8988
};
9089

91-
#define ref_next(r) ((r)->next_phys)
90+
#define REF_LINK_NODE ((int32_t)-1)
91+
#define REF_EMPTY_NODE ((int32_t)-2)
92+
93+
/* Use blocks of about 256 bytes */
94+
#define REFS_PER_BLOCK ((255/sizeof(struct jffs2_raw_node_ref))-1)
95+
96+
static inline struct jffs2_raw_node_ref *ref_next(struct jffs2_raw_node_ref *ref)
97+
{
98+
ref++;
99+
100+
/* Link to another block of refs */
101+
if (ref->flash_offset == REF_LINK_NODE) {
102+
ref = ref->next_in_ino;
103+
if (!ref)
104+
return ref;
105+
}
106+
107+
/* End of chain */
108+
if (ref->flash_offset == REF_EMPTY_NODE)
109+
return NULL;
110+
111+
return ref;
112+
}
92113

93114
static inline struct jffs2_inode_cache *jffs2_raw_ref_to_ic(struct jffs2_raw_node_ref *raw)
94115
{
@@ -234,6 +255,7 @@ struct jffs2_eraseblock
234255
uint32_t wasted_size;
235256
uint32_t free_size; /* Note that sector_size - free_size
236257
is the address of the first free space */
258+
uint32_t allocated_refs;
237259
struct jffs2_raw_node_ref *first_node;
238260
struct jffs2_raw_node_ref *last_node;
239261

@@ -378,10 +400,9 @@ struct jffs2_raw_inode *jffs2_alloc_raw_inode(void);
378400
void jffs2_free_raw_inode(struct jffs2_raw_inode *);
379401
struct jffs2_tmp_dnode_info *jffs2_alloc_tmp_dnode_info(void);
380402
void jffs2_free_tmp_dnode_info(struct jffs2_tmp_dnode_info *);
381-
int jffs2_prealloc_raw_node_refs(struct jffs2_sb_info *c,
403+
int jffs2_prealloc_raw_node_refs(struct jffs2_sb_info *c,
382404
struct jffs2_eraseblock *jeb, int nr);
383-
struct jffs2_raw_node_ref *__jffs2_alloc_raw_node_ref(void);
384-
void __jffs2_free_raw_node_ref(struct jffs2_raw_node_ref *);
405+
void jffs2_free_refblock(struct jffs2_raw_node_ref *);
385406
struct jffs2_node_frag *jffs2_alloc_node_frag(void);
386407
void jffs2_free_node_frag(struct jffs2_node_frag *);
387408
struct jffs2_inode_cache *jffs2_alloc_inode_cache(void);

0 commit comments

Comments
 (0)