Root Cause
The crash at fs/jffs2/readinode.c:1365 occurs because jffs2_do_read_inode() (and jffs2_garbage_collect_pass()) fails to handle the INO_STATE_CLEARING (6) state of struct jffs2_inode_cache::state (which matches R09/R12 = 6 in the crash register dump).
1. Missing INO_STATE_CLEARING handling in jffs2_do_read_inode() and jffs2_garbage_collect_pass()
In fs/jffs2/nodelist.h:188-194, JFFS2 defines seven INO_STATE_* values, including INO_STATE_CLEARING (6). However:
-
In fs/jffs2/readinode.c:1339-1366, jffs2_do_read_inode() switches on f->inocache->state under c->inocache_lock:
INO_STATE_UNCHECKED, INO_STATE_CHECKEDABSENT -> transitions to INO_STATE_READING
INO_STATE_CHECKING, INO_STATE_GC -> sleeps on c->inocache_wq and retries
INO_STATE_READING, INO_STATE_PRESENT -> logs an error and sets f->inocache = NULL
default: -> hits BUG(); at fs/jffs2/readinode.c:1365
INO_STATE_CLEARING is not handled anywhere in this switch, so observing f->inocache->state == INO_STATE_CLEARING triggers BUG().
-
Similarly, in fs/jffs2/gc.c:365-419, jffs2_garbage_collect_pass() switches on ic->state after choosing a node to garbage-collect. While it waits on c->inocache_wq for INO_STATE_READING and BUG()s on INO_STATE_UNCHECKED/INO_STATE_CHECKING/INO_STATE_GC, it omits INO_STATE_CLEARING and has no default: branch. Thus, when ic->state == INO_STATE_CLEARING, GC silently falls out of the switch, drops c->inocache_lock, and calls jffs2_gc_fetch_inode(c, inum, !nlink) (fs/jffs2/gc.c:454) -> jffs2_iget() (fs/jffs2/fs.c:673) -> jffs2_do_read_inode() (fs/jffs2/fs.c:277). (Note that the CRC-check loop earlier in jffs2_garbage_collect_pass() at fs/jffs2/gc.c:198-230 also hits default: BUG() on INO_STATE_CLEARING.)
2. How jffs2_do_read_inode() observes INO_STATE_CLEARING
Normally, VFS inode eviction (evict() in fs/inode.c:798-854) invokes op->evict_inode(inode) -> jffs2_evict_inode() (fs/jffs2/fs.c:238) -> jffs2_do_clear_inode() (fs/jffs2/readinode.c:1413) before calling remove_inode_hash(inode). While jffs2_do_clear_inode() holds f->inocache->state in INO_STATE_CLEARING (between fs/jffs2/readinode.c:1423 and 1441), the struct inode remains in inode_hashtable with I_FREEING set, so any concurrent iget_locked() waits in __wait_on_freeing_inode() until eviction finishes.
However, iget_failed() (fs/bad_inode.c:245-250) breaks that assumption:
void iget_failed(struct inode *inode)
{
make_bad_inode(inode); /* calls remove_inode_hash(inode) */
unlock_new_inode(inode);
iput(inode); /* calls evict() -> jffs2_evict_inode() -> jffs2_do_clear_inode() */
}
JFFS2 calls iget_failed(inode) on error paths in:
jffs2_iget() (fs/jffs2/fs.c:368) when jffs2_do_read_inode() or jffs2_read_dnode() fails (e.g., on a corrupted flash node),
jffs2_create(), jffs2_symlink(), jffs2_mkdir(), and jffs2_mknod() (fs/jffs2/dir.c:222, 446, 590, 768) when creation fails after jffs2_new_inode(), and similarly make_bad_inode() + iput() in jffs2_new_inode() (fs/jffs2/fs.c:486-487) if insert_inode_locked() fails.
Because make_bad_inode(inode) removes inode from inode_hashtable before iput(inode) triggers jffs2_evict_inode() -> jffs2_do_clear_inode():
- Thread A runs
jffs2_do_clear_inode(c, f) without holding c->alloc_sem and sets f->inocache->state = INO_STATE_CLEARING (fs/jffs2/readinode.c:1423) while freeing f->metadata, f->fragtree, and f->dents before setting INO_STATE_CHECKEDABSENT and waking c->inocache_wq at line 1441.
- Concurrently,
jffs2_gcd_mtd0 runs jffs2_garbage_collect_pass(), sees ic->state == INO_STATE_CLEARING (with ic->pino_nlink != 0), falls through the switch at fs/jffs2/gc.c:365, and calls jffs2_gc_fetch_inode() -> jffs2_iget().
- Because
inode was already unhashed by make_bad_inode(), iget_locked() does not find the freeing inode in inode_hashtable, allocates a new I_NEW inode, and calls jffs2_do_read_inode().
jffs2_do_read_inode() looks up f->inocache in c->inocache_list, observes f->inocache->state == INO_STATE_CLEARING, and hits BUG() at fs/jffs2/readinode.c:1365.
Fix Direction
Both jffs2_do_read_inode() (fs/jffs2/readinode.c:1345) and jffs2_garbage_collect_pass() (fs/jffs2/gc.c:211 and fs/jffs2/gc.c:397) should treat INO_STATE_CLEARING as a transient state to wait on via sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock) (matching INO_STATE_CHECKING/INO_STATE_GC in jffs2_do_read_inode() and INO_STATE_READING in jffs2_garbage_collect_pass()), since jffs2_do_clear_inode() wakes c->inocache_wq via jffs2_set_inocache_state() when transitioning out of INO_STATE_CLEARING.