new = kmalloc(sizeof(*new), GFP_KERNEL); if (!new) {
jffs2_dbg(1, "No memory to allocate inodirty. Fallback to all considered dirty\n");
jffs2_clear_wbuf_ino_list(c);
c->wbuf_inodes = &inodirty_nomem; return;
} new->ino = ino; new->next = c->wbuf_inodes;
c->wbuf_inodes = new; return;
}
jffs2_dbg(1, "Removing eraseblock at 0x%08x from erasable_pending_wbuf_list...\n",
jeb->offset);
list_del(this); if ((jiffies + (n++)) & 127) { /* Most of the time, we just erase it immediately. Otherwise we
spend ages scanning it on mount, etc. */
jffs2_dbg(1, "...and adding to erase_pending_list\n");
list_add_tail(&jeb->list, &c->erase_pending_list);
c->nr_erasing_blocks++;
jffs2_garbage_collect_trigger(c);
} else { /* Sometimes, however, we leave it elsewhere so it doesn't get
immediately reused, and we spread the load a bit. */
jffs2_dbg(1, "...and adding to erasable_list\n");
list_add_tail(&jeb->list, &c->erasable_list);
}
}
}
#define REFILE_NOTEMPTY 0 #define REFILE_ANYWAY 1
staticvoid jffs2_block_refile(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, int allow_empty)
{
jffs2_dbg(1, "About to refile bad block at %08x\n", jeb->offset);
/* File the existing block on the bad_used_list.... */ if (c->nextblock == jeb)
c->nextblock = NULL; else/* Not sure this should ever happen... need more coffee */
list_del(&jeb->list); if (jeb->first_node) {
jffs2_dbg(1, "Refiling block at %08x to bad_used_list\n",
jeb->offset);
list_add(&jeb->list, &c->bad_used_list);
} else {
BUG_ON(allow_empty == REFILE_NOTEMPTY); /* It has to have had some nodes or we couldn't be here */
jffs2_dbg(1, "Refiling block at %08x to erase_pending_list\n",
jeb->offset);
list_add(&jeb->list, &c->erase_pending_list);
c->nr_erasing_blocks++;
jffs2_garbage_collect_trigger(c);
}
if (!jffs2_prealloc_raw_node_refs(c, jeb, 1)) {
uint32_t oldfree = jeb->free_size;
switch (je16_to_cpu(node->u.nodetype)) { case JFFS2_NODETYPE_INODE: if (f->metadata && f->metadata->raw == raw) {
dbg_noderef("Will replace ->raw in f->metadata at %p\n", f->metadata); return &f->metadata->raw;
}
frag = jffs2_lookup_node_frag(&f->fragtree, je32_to_cpu(node->i.offset));
BUG_ON(!frag); /* Find a frag which refers to the full_dnode we want to modify */ while (!frag->node || frag->node->raw != raw) {
frag = frag_next(frag);
BUG_ON(!frag);
}
dbg_noderef("Will replace ->raw in full_dnode at %p\n", frag->node); return &frag->node->raw;
case JFFS2_NODETYPE_DIRENT: for (fd = f->dents; fd; fd = fd->next) { if (fd->raw == raw) {
dbg_noderef("Will replace ->raw in full_dirent at %p\n", fd); return &fd->raw;
}
}
BUG();
default:
dbg_noderef("Don't care about replacing raw for nodetype %x\n",
je16_to_cpu(node->u.nodetype)); break;
} return NULL;
}
ret = mtd_read(c->mtd, ofs, c->wbuf_pagesize, &retlen, c->wbuf_verify); if (ret && ret != -EUCLEAN && ret != -EBADMSG) {
pr_warn("%s(): Read back of page at %08x failed: %d\n",
__func__, c->wbuf_ofs, ret); return ret;
} elseif (retlen != c->wbuf_pagesize) {
pr_warn("%s(): Read back of page at %08x gave short read: %zd not %d\n",
__func__, ofs, retlen, c->wbuf_pagesize); return -EIO;
} if (!memcmp(buf, c->wbuf_verify, c->wbuf_pagesize)) return0;
/* Find the first node to be recovered, by skipping over every
node which ends before the wbuf starts, or which is obsolete. */ for (next = raw = jeb->first_node; next; raw = next) {
next = ref_next(raw);
if (ref_obsolete(raw) ||
(next && ref_offset(next) <= c->wbuf_ofs)) {
dbg_noderef("Skipping node at 0x%08x(%d)-0x%08x which is either before 0x%08x or obsolete\n",
ref_offset(raw), ref_flags(raw),
(ref_offset(raw) + ref_totlen(c, jeb, raw)),
c->wbuf_ofs); continue;
}
dbg_noderef("First node to be recovered is at 0x%08x(%d)-0x%08x\n",
ref_offset(raw), ref_flags(raw),
(ref_offset(raw) + ref_totlen(c, jeb, raw)));
first_raw = raw; break;
}
if (!first_raw) { /* All nodes were obsolete. Nothing to recover. */
jffs2_dbg(1, "No non-obsolete nodes to be recovered. Just filing block bad\n");
c->wbuf_len = 0; return;
}
start = ref_offset(first_raw);
end = ref_offset(jeb->last_node);
nr_refile = 1;
/* Count the number of refs which need to be copied */ while ((raw = ref_next(raw)) != jeb->last_node)
nr_refile++;
dbg_noderef("wbuf recover %08x-%08x (%d bytes in %d nodes)\n",
start, end, end - start, nr_refile);
buf = NULL; if (start < c->wbuf_ofs) { /* First affected node was already partially written.
* Attempt to reread the old data into our buffer. */
buf = kmalloc(end - start, GFP_KERNEL); if (!buf) {
pr_crit("Malloc failure in wbuf recovery. Data loss ensues.\n");
goto read_failed;
}
/* Do the read... */
ret = mtd_read(c->mtd, start, c->wbuf_ofs - start, &retlen,
buf);
/* ECC recovered ? */ if ((ret == -EUCLEAN || ret == -EBADMSG) &&
(retlen == c->wbuf_ofs - start))
ret = 0;
if (ret || retlen != c->wbuf_ofs - start) {
pr_crit("Old data are already lost in wbuf recovery. Data loss ensues.\n");
/* If this was the only node to be recovered, give up */ if (!first_raw) {
c->wbuf_len = 0; return;
}
/* It wasn't. Go on and try to recover nodes complete in the wbuf */
start = ref_offset(first_raw);
dbg_noderef("wbuf now recover %08x-%08x (%d bytes in %d nodes)\n",
start, end, end - start, nr_refile);
} else { /* Read succeeded. Copy the remaining data from the wbuf */
memcpy(buf + (c->wbuf_ofs - start), c->wbuf, end - c->wbuf_ofs);
}
} /* OK... we're to rewrite (end-start) bytes of data from first_raw onwards.
Either 'buf' contains the data, or we find it in the wbuf */
/* ... and get an allocation of space from a shiny new block instead */
ret = jffs2_reserve_space_gc(c, end-start, &len, JFFS2_SUMMARY_NOSUM_SIZE); if (ret) {
pr_warn("Failed to allocate space for wbuf recovery. Data loss ensues.\n");
kfree(buf); return;
}
/* The summary is not recovered, so it must be disabled for this erase block */
jffs2_sum_disable_collecting(c->summary);
ret = jffs2_prealloc_raw_node_refs(c, c->nextblock, nr_refile); if (ret) {
pr_warn("Failed to allocate node refs for wbuf recovery. Data loss ensues.\n");
kfree(buf); return;
}
ofs = write_ofs(c);
if (end-start >= c->wbuf_pagesize) { /* Need to do another write immediately, but it's possible thatthisisjustbecausethewbufitselfiscompletely full,andthere'snothingearlierreadbackfromthe flash.Hence'buf'isn'tnecessarilywhatwe'rewriting
from. */ unsignedchar *rewrite_buf = buf?:c->wbuf;
uint32_t towrite = (end-start) - ((end-start)%c->wbuf_pagesize);
jffs2_dbg(1, "Write 0x%x bytes at 0x%08x in wbuf recover\n",
towrite, ofs);
#ifdef BREAKMEHEADER staticint breakme; if (breakme++ == 20) {
pr_notice("Faking write error at 0x%08x\n", ofs);
breakme = 0;
mtd_write(c->mtd, ofs, towrite, &retlen, brokenbuf);
ret = -EIO;
} else #endif
ret = mtd_write(c->mtd, ofs, towrite, &retlen,
rewrite_buf);
if (ret || retlen != towrite || jffs2_verify_write(c, rewrite_buf, ofs)) { /* Argh. We tried. Really we did. */
pr_crit("Recovery of wbuf failed due to a second write error\n");
kfree(buf);
if (retlen)
jffs2_add_physical_node_ref(c, ofs | REF_OBSOLETE, ref_totlen(c, jeb, first_raw), NULL);
return;
}
pr_notice("Recovery of wbuf succeeded to %08x\n", ofs);
c->wbuf_len = (end - start) - towrite;
c->wbuf_ofs = ofs + towrite;
memmove(c->wbuf, rewrite_buf + towrite, c->wbuf_len); /* Don't muck about with c->wbuf_inodes. False positives are harmless. */
} else { /* OK, now we're left with the dregs in whichever buffer we're using */ if (buf) {
memcpy(c->wbuf, buf, end-start);
} else {
memmove(c->wbuf, c->wbuf + (start - c->wbuf_ofs), end - start);
}
c->wbuf_ofs = ofs;
c->wbuf_len = end - start;
}
/* Now sort out the jffs2_raw_node_refs, moving them from the old to the next block */
new_jeb = &c->blocks[ofs / c->sector_size];
/* Remove the old node from the per-inode list */ while (*p && *p != (void *)ic) { if (*p == raw) {
(*p) = (raw->next_in_ino);
raw->next_in_ino = NULL; break;
}
p = &((*p)->next_in_ino);
}
if (ic->state == INO_STATE_PRESENT && !ref_obsolete(raw)) { /* If it's an in-core inode, then we have to adjust any full_direntorfull_dnodestructuretopointtothe
new version instead of the old */
f = jffs2_gc_fetch_inode(c, ic->ino, !ic->pino_nlink); if (IS_ERR(f)) { /* Should never happen; it _must_ be present */
JFFS2_ERROR("Failed to iget() ino #%u, err %ld\n",
ic->ino, PTR_ERR(f));
BUG();
} /* We don't lock f->sem. There's a number of ways we could endupinherewithitalreadybeinglocked,andnobody's goingtomodifyitonusanywaybecauseweholdthe alloc_sem.We'reonlychangingone->rawpointertoo,
which we can get away with without upsetting readers. */
adjust_ref = jffs2_incore_replace_raw(c, f, raw,
(void *)(buf?:c->wbuf) + (ref_offset(raw) - start));
} elseif (unlikely(ic->state != INO_STATE_PRESENT &&
ic->state != INO_STATE_CHECKEDABSENT &&
ic->state != INO_STATE_GC)) {
JFFS2_ERROR("Inode #%u is in strange state %d!\n", ic->ino, ic->state);
BUG();
}
}
/* Fix up the original jeb now it's on the bad_list */ if (first_raw == jeb->first_node) {
jffs2_dbg(1, "Failing block at %08x is now empty. Moving to erase_pending_list\n",
jeb->offset);
list_move(&jeb->list, &c->erase_pending_list);
c->nr_erasing_blocks++;
jffs2_garbage_collect_trigger(c);
}
jffs2_dbg(1, "wbuf recovery completed OK. wbuf_ofs 0x%08x, len 0x%x\n",
c->wbuf_ofs, c->wbuf_len);
}
/* Meaning of pad argument: 0:Donotpad.Probablypointless-weonlyeverusethiswhenwecan'tpadanyway. 1:Pad,donotadjustnextblockfree_size 2:Pad,adjustnextblockfree_size
*/ #define NOPAD 0 #define PAD_NOACCOUNT 1 #define PAD_ACCOUNTING 2
staticint __jffs2_flush_wbuf(struct jffs2_sb_info *c, int pad)
{ struct jffs2_eraseblock *wbuf_jeb; int ret;
size_t retlen;
/* Nothing to do if not write-buffering the flash. In particular, we shouldn't
call timer_delete() on the timer we never initialised. */ if (!jffs2_is_writebuffered(c)) return0;
if (!mutex_is_locked(&c->alloc_sem)) {
pr_crit("jffs2_flush_wbuf() called with alloc_sem not locked!\n");
BUG();
}
if (!c->wbuf_len) /* already checked c->wbuf above */ return0;
/* claim remaining space on the page thishappens,ifwehaveachangetoanewblock, oriffsyncforcesustoflushthewritebuffer. ifwehaveaswitchtonextpage,wewillnothave enoughremainingspaceforthis.
*/ if (pad ) {
c->wbuf_len = PAD(c->wbuf_len);
/* Pad with JFFS2_DIRTY_BITMASK initially. this helps out ECC'd NOR
with 8 byte page size */
memset(c->wbuf + c->wbuf_len, 0, c->wbuf_pagesize - c->wbuf_len);
if ( c->wbuf_len + sizeof(struct jffs2_unknown_node) < c->wbuf_pagesize) { struct jffs2_unknown_node *padnode = (void *)(c->wbuf + c->wbuf_len);
padnode->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
padnode->nodetype = cpu_to_je16(JFFS2_NODETYPE_PADDING);
padnode->totlen = cpu_to_je32(c->wbuf_pagesize - c->wbuf_len);
padnode->hdr_crc = cpu_to_je32(crc32(0, padnode, sizeof(*padnode)-4));
}
} /* else jffs2_flash_writev has actually filled in the rest of the
buffer for us, and will deal with the node refs etc. later. */
#ifdef BREAKME staticint breakme; if (breakme++ == 20) {
pr_notice("Faking write error at 0x%08x\n", c->wbuf_ofs);
breakme = 0;
mtd_write(c->mtd, c->wbuf_ofs, c->wbuf_pagesize, &retlen,
brokenbuf);
ret = -EIO;
} else #endif
ret = mtd_write(c->mtd, c->wbuf_ofs, c->wbuf_pagesize,
&retlen, c->wbuf);
if (ret) {
pr_warn("jffs2_flush_wbuf(): Write failed with %d\n", ret); goto wfail;
} elseif (retlen != c->wbuf_pagesize) {
pr_warn("jffs2_flush_wbuf(): Write was short: %zd instead of %d\n",
retlen, c->wbuf_pagesize);
ret = -EIO; goto wfail;
} elseif ((ret = jffs2_verify_write(c, c->wbuf, c->wbuf_ofs))) {
wfail:
jffs2_wbuf_recover(c);
return ret;
}
/* Adjust free size of the block if we padded. */ if (pad) {
uint32_t waste = c->wbuf_pagesize - c->wbuf_len;
jffs2_dbg(1, "jffs2_flush_wbuf() adjusting free_size of %sblock at %08x\n",
(wbuf_jeb == c->nextblock) ? "next" : "",
wbuf_jeb->offset);
/* wbuf_pagesize - wbuf_len is the amount of space that's to be padded.Ifthereislessfreespaceintheblockthanthat,
something screwed up */ if (wbuf_jeb->free_size < waste) {
pr_crit("jffs2_flush_wbuf(): Accounting error. wbuf at 0x%08x has 0x%03x bytes, 0x%03x left.\n",
c->wbuf_ofs, c->wbuf_len, waste);
pr_crit("jffs2_flush_wbuf(): But free_size for block at 0x%08x is only 0x%08x\n",
wbuf_jeb->offset, wbuf_jeb->free_size);
BUG();
}
spin_lock(&c->erase_completion_lock);
jffs2_link_node_ref(c, wbuf_jeb, (c->wbuf_ofs + c->wbuf_len) | REF_OBSOLETE, waste, NULL); /* FIXME: that made it count as dirty. Convert to wasted */
wbuf_jeb->dirty_size -= waste;
c->dirty_size -= waste;
wbuf_jeb->wasted_size += waste;
c->wasted_size += waste;
} else
spin_lock(&c->erase_completion_lock);
/* Stick any now-obsoleted blocks on the erase_pending_list */
jffs2_refile_wbuf_blocks(c);
jffs2_clear_wbuf_ino_list(c);
spin_unlock(&c->erase_completion_lock);
memset(c->wbuf,0xff,c->wbuf_pagesize); /* adjust write buffer offset, else we get a non contiguous write bug */
c->wbuf_ofs += c->wbuf_pagesize;
c->wbuf_len = 0; return0;
}
/* Trigger garbage collection to flush the write-buffer. Ifinoargiszero,doitif_any_real(i.e.notGC)writesare outstanding.Ifinoargnon-zero,doitonlyifawriteforthe
given inode is outstanding. */ int jffs2_flush_wbuf_gc(struct jffs2_sb_info *c, uint32_t ino)
{
uint32_t old_wbuf_ofs;
uint32_t old_wbuf_len; int ret = 0;
jffs2_dbg(1, "jffs2_flush_wbuf_gc() called for ino #%u...\n", ino);
if (!c->wbuf) return0;
mutex_lock(&c->alloc_sem); if (!jffs2_wbuf_pending_for_ino(c, ino)) {
jffs2_dbg(1, "Ino #%d not pending in wbuf. Returning\n", ino);
mutex_unlock(&c->alloc_sem); return0;
}
if (c->unchecked_size) { /* GC won't make any progress for a while */
jffs2_dbg(1, "%s(): padding. Not finished checking\n",
__func__);
down_write(&c->wbuf_sem);
ret = __jffs2_flush_wbuf(c, PAD_ACCOUNTING); /* retry flushing wbuf in case jffs2_wbuf_recover
left some data in the wbuf */ if (ret)
ret = __jffs2_flush_wbuf(c, PAD_ACCOUNTING);
up_write(&c->wbuf_sem);
} elsewhile (old_wbuf_len &&
old_wbuf_ofs == c->wbuf_ofs) {
mutex_unlock(&c->alloc_sem);
jffs2_dbg(1, "%s(): calls gc pass\n", __func__);
ret = jffs2_garbage_collect_pass(c); if (ret) { /* GC failed. Flush it with padding instead */
mutex_lock(&c->alloc_sem);
down_write(&c->wbuf_sem);
ret = __jffs2_flush_wbuf(c, PAD_ACCOUNTING); /* retry flushing wbuf in case jffs2_wbuf_recover
left some data in the wbuf */ if (ret)
ret = __jffs2_flush_wbuf(c, PAD_ACCOUNTING);
up_write(&c->wbuf_sem); break;
}
mutex_lock(&c->alloc_sem);
}
jffs2_dbg(1, "%s(): ends...\n", __func__);
mutex_unlock(&c->alloc_sem); return ret;
}
/* Pad write-buffer to end and write it, wasting space. */ int jffs2_flush_wbuf_pad(struct jffs2_sb_info *c)
{ int ret;
if (!c->wbuf) return0;
down_write(&c->wbuf_sem);
ret = __jffs2_flush_wbuf(c, PAD_NOACCOUNT); /* retry - maybe wbuf recover left some data in wbuf. */ if (ret)
ret = __jffs2_flush_wbuf(c, PAD_NOACCOUNT);
up_write(&c->wbuf_sem);
/* If not writebuffered flash, don't bother */ if (!jffs2_is_writebuffered(c)) return jffs2_flash_direct_writev(c, invecs, count, to, retlen);
down_write(&c->wbuf_sem);
/* If wbuf_ofs is not initialized, set it to target address */ if (c->wbuf_ofs == 0xFFFFFFFF) {
c->wbuf_ofs = PAGE_DIV(to);
c->wbuf_len = PAGE_MOD(to);
memset(c->wbuf,0xff,c->wbuf_pagesize);
}
/* *Sanitychecksontargetaddress.It'spermittedtowrite *atPAD(c->wbuf_len+c->wbuf_ofs),andit'spermittedto *writeatthebeginningofaneweraseblock.Anythingelse, *andyoudie.Newblockstartsatxxx000c(0-b=block *header)
*/ if (SECTOR_ADDR(to) != SECTOR_ADDR(c->wbuf_ofs)) { /* It's a write to a new block */ if (c->wbuf_len) {
jffs2_dbg(1, "%s(): to 0x%lx causes flush of wbuf at 0x%08x\n",
__func__, (unsignedlong)to, c->wbuf_ofs);
ret = __jffs2_flush_wbuf(c, PAD_NOACCOUNT); if (ret) goto outerr;
} /* set pointer to new block */
c->wbuf_ofs = PAGE_DIV(to);
c->wbuf_len = PAGE_MOD(to);
}
if (to != PAD(c->wbuf_ofs + c->wbuf_len)) { /* We're not writing immediately after the writebuffer. Bad. */
pr_crit("%s(): Non-contiguous write to %08lx\n",
__func__, (unsignedlong)to); if (c->wbuf_len)
pr_crit("wbuf was previously %08x-%08x\n",
c->wbuf_ofs, c->wbuf_ofs + c->wbuf_len);
BUG();
}
/* adjust alignment offset */ if (c->wbuf_len != PAGE_MOD(to)) {
c->wbuf_len = PAGE_MOD(to); /* take care of alignment to next page */ if (!c->wbuf_len) {
c->wbuf_len = c->wbuf_pagesize;
ret = __jffs2_flush_wbuf(c, NOPAD); if (ret) goto outerr;
}
}
for (invec = 0; invec < count; invec++) { int vlen = invecs[invec].iov_len;
uint8_t *v = invecs[invec].iov_base;
wbuf_retlen = jffs2_fill_wbuf(c, v, vlen);
if (c->wbuf_len == c->wbuf_pagesize) {
ret = __jffs2_flush_wbuf(c, NOPAD); if (ret) goto outerr;
}
vlen -= wbuf_retlen;
outvec_to += wbuf_retlen;
donelen += wbuf_retlen;
v += wbuf_retlen;
if (vlen >= c->wbuf_pagesize) {
ret = mtd_write(c->mtd, outvec_to, PAGE_DIV(vlen),
&wbuf_retlen, v); if (ret < 0 || wbuf_retlen != PAGE_DIV(vlen)) goto outfile;
int jffs2_nor_wbuf_flash_setup(struct jffs2_sb_info *c) { /* Cleanmarker currently occupies whole programming regions,
* either one or 2 for 8Byte STMicro flashes. */
c->cleanmarker_size = max(16u, c->mtd->writesize);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.