14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/kernel.h>
17 #include <linux/slab.h>
22 #include <linux/sched.h>
32 static unsigned char *brokenbuf;
35 #define PAGE_DIV(x) ( ((unsigned long)(x) / (unsigned long)(c->wbuf_pagesize)) * (unsigned long)(c->wbuf_pagesize) )
36 #define PAGE_MOD(x) ( (unsigned long)(x) % (unsigned long)(c->wbuf_pagesize) )
39 #define MAX_ERASE_FAILURES 2
53 if (
this == &inodirty_nomem)
69 static void jffs2_clear_wbuf_ino_list(
struct jffs2_sb_info *c)
73 this = c->wbuf_inodes;
75 if (
this != &inodirty_nomem) {
82 c->wbuf_inodes =
NULL;
92 if (jffs2_wbuf_pending_for_ino(c, ino))
97 jffs2_dbg(1,
"No memory to allocate inodirty. Fallback to all considered dirty\n");
98 jffs2_clear_wbuf_ino_list(c);
99 c->wbuf_inodes = &inodirty_nomem;
103 new->next = c->wbuf_inodes;
104 c->wbuf_inodes =
new;
108 static inline void jffs2_refile_wbuf_blocks(
struct jffs2_sb_info *c)
119 jffs2_dbg(1,
"Removing eraseblock at 0x%08x from erasable_pending_wbuf_list...\n",
122 if ((jiffies + (n++)) & 127) {
125 jffs2_dbg(1,
"...and adding to erase_pending_list\n");
132 jffs2_dbg(1,
"...and adding to erasable_list\n");
138 #define REFILE_NOTEMPTY 0
139 #define REFILE_ANYWAY 1
151 jffs2_dbg(1,
"Refiling block at %08x to bad_used_list\n",
157 jffs2_dbg(1,
"Refiling block at %08x to erase_pending_list\n",
190 dbg_noderef(
"incore_replace_raw: node at %p is {%04x,%04x}\n",
205 while (!frag->
node || frag->
node->raw != raw) {
210 return &frag->
node->raw;
214 if (fd->
raw == raw) {
215 dbg_noderef(
"Will replace ->raw in full_dirent at %p\n", fd);
222 dbg_noderef(
"Don't care about replacing raw for nodetype %x\n",
229 #ifdef CONFIG_JFFS2_FS_WBUF_VERIFY
239 pr_warn(
"%s(): Read back of page at %08x failed: %d\n",
240 __func__, c->wbuf_ofs, ret);
243 pr_warn(
"%s(): Read back of page at %08x gave short read: %zd not %d\n",
251 eccstr =
"corrected";
253 eccstr =
"correction failed";
255 eccstr =
"OK or unused";
257 pr_warn(
"Write verify error (ECC %s) at %08x. Wrote:\n",
258 eccstr, c->wbuf_ofs);
269 #define jffs2_verify_write(c,b,o) (0)
288 if (c->wbuf_ofs % c->
mtd->erasesize)
298 for (next = raw = jeb->
first_node; next; raw = next) {
299 next = ref_next(raw);
303 dbg_noderef(
"Skipping node at 0x%08x(%d)-0x%08x which is either before 0x%08x or obsolete\n",
309 dbg_noderef(
"First node to be recovered is at 0x%08x(%d)-0x%08x\n",
319 jffs2_dbg(1,
"No non-obsolete nodes to be recovered. Just filing block bad\n");
329 while ((raw = ref_next(raw)) != jeb->
last_node)
332 dbg_noderef(
"wbuf recover %08x-%08x (%d bytes in %d nodes)\n",
333 start, end, end - start, nr_refile);
336 if (start < c->wbuf_ofs) {
342 pr_crit(
"Malloc failure in wbuf recovery. Data loss ensues.\n");
348 ret =
mtd_read(c->
mtd, start, c->wbuf_ofs - start, &retlen,
353 (retlen == c->wbuf_ofs - start))
356 if (ret || retlen != c->wbuf_ofs - start) {
357 pr_crit(
"Old data are already lost in wbuf recovery. Data loss ensues.\n");
362 first_raw = ref_next(first_raw);
365 first_raw = ref_next(first_raw);
377 dbg_noderef(
"wbuf now recover %08x-%08x (%d bytes in %d nodes)\n",
378 start, end, end - start, nr_refile);
382 memcpy(buf + (c->wbuf_ofs - start), c->wbuf, end - c->wbuf_ofs);
391 pr_warn(
"Failed to allocate space for wbuf recovery. Data loss ensues.\n");
401 pr_warn(
"Failed to allocate node refs for wbuf recovery. Data loss ensues.\n");
414 unsigned char *rewrite_buf = buf?:c->wbuf;
417 jffs2_dbg(1,
"Write 0x%x bytes at 0x%08x in wbuf recover\n",
422 if (breakme++ == 20) {
423 pr_notice(
"Faking write error at 0x%08x\n", ofs);
434 pr_crit(
"Recovery of wbuf failed due to a second write error\n");
442 pr_notice(
"Recovery of wbuf succeeded to %08x\n", ofs);
444 c->wbuf_len = (end -
start) - towrite;
445 c->wbuf_ofs = ofs + towrite;
446 memmove(c->wbuf, rewrite_buf + towrite, c->wbuf_len);
451 memcpy(c->wbuf, buf, end-start);
453 memmove(c->wbuf, c->wbuf + (start - c->wbuf_ofs), end - start);
456 c->wbuf_len = end -
start;
463 for (raw = first_raw; raw != jeb->
last_node; raw = ref_next(raw)) {
470 jffs2_dbg(1,
"Refiling block of %08x at %08x(%d) to %08x\n",
473 ic = jffs2_raw_ref_to_ic(raw);
479 adjust_ref = &xd->
node;
485 adjust_ref = &xr->
node;
492 while (*p && *p != (
void *)ic) {
498 p = &((*p)->next_in_ino);
509 ic->
ino, PTR_ERR(f));
517 adjust_ref = jffs2_incore_replace_raw(c, f, raw,
518 (
void *)(buf?:c->wbuf) + (
ref_offset(raw) - start));
530 BUG_ON(*adjust_ref != raw);
531 *adjust_ref = new_ref;
551 jffs2_dbg(1,
"Failing block at %08x is now empty. Moving to erase_pending_list\n",
566 jffs2_dbg(1,
"wbuf recovery completed OK. wbuf_ofs 0x%08x, len 0x%x\n",
567 c->wbuf_ofs, c->wbuf_len);
577 #define PAD_NOACCOUNT 1
578 #define PAD_ACCOUNTING 2
592 pr_crit(
"jffs2_flush_wbuf() called with alloc_sem not locked!\n");
610 c->wbuf_len =
PAD(c->wbuf_len);
629 if (breakme++ == 20) {
630 pr_notice(
"Faking write error at 0x%08x\n", c->wbuf_ofs);
642 pr_warn(
"jffs2_flush_wbuf(): Write failed with %d\n", ret);
645 pr_warn(
"jffs2_flush_wbuf(): Write was short: %zd instead of %d\n",
651 jffs2_wbuf_recover(c);
660 jffs2_dbg(1,
"jffs2_flush_wbuf() adjusting free_size of %sblock at %08x\n",
661 (wbuf_jeb == c->
nextblock) ?
"next" :
"",
668 pr_crit(
"jffs2_flush_wbuf(): Accounting error. wbuf at 0x%08x has 0x%03x bytes, 0x%03x left.\n",
669 c->wbuf_ofs, c->wbuf_len, waste);
670 pr_crit(
"jffs2_flush_wbuf(): But free_size for block at 0x%08x is only 0x%08x\n",
687 jffs2_refile_wbuf_blocks(c);
688 jffs2_clear_wbuf_ino_list(c);
708 jffs2_dbg(1,
"jffs2_flush_wbuf_gc() called for ino #%u...\n", ino);
714 if (!jffs2_wbuf_pending_for_ino(c, ino)) {
715 jffs2_dbg(1,
"Ino #%d not pending in wbuf. Returning\n", ino);
720 old_wbuf_ofs = c->wbuf_ofs;
721 old_wbuf_len = c->wbuf_len;
725 jffs2_dbg(1,
"%s(): padding. Not finished checking\n",
734 }
else while (old_wbuf_len &&
735 old_wbuf_ofs == c->wbuf_ofs) {
739 jffs2_dbg(1,
"%s(): calls gc pass\n", __func__);
757 jffs2_dbg(1,
"%s(): ends...\n", __func__);
789 memcpy(c->wbuf + c->wbuf_len, buf, len);
795 unsigned long count, loff_t to,
size_t *retlen,
799 size_t wbuf_retlen, donelen = 0;
810 if (c->wbuf_ofs == 0xFFFFFFFF) {
826 jffs2_dbg(1,
"%s(): to 0x%lx causes flush of wbuf at 0x%08x\n",
827 __func__, (
unsigned long)to, c->wbuf_ofs);
837 if (to !=
PAD(c->wbuf_ofs + c->wbuf_len)) {
839 pr_crit(
"%s(): Non-contiguous write to %08lx\n",
840 __func__, (
unsigned long)to);
842 pr_crit(
"wbuf was previously %08x-%08x\n",
843 c->wbuf_ofs, c->wbuf_ofs + c->wbuf_len);
853 ret = __jffs2_flush_wbuf(c,
NOPAD);
859 for (invec = 0; invec <
count; invec++) {
860 int vlen = invecs[invec].
iov_len;
863 wbuf_retlen = jffs2_fill_wbuf(c, v, vlen);
866 ret = __jffs2_flush_wbuf(c,
NOPAD);
871 outvec_to += wbuf_retlen;
872 donelen += wbuf_retlen;
878 if (ret < 0 || wbuf_retlen !=
PAGE_DIV(vlen))
882 outvec_to += wbuf_retlen;
883 c->wbuf_ofs = outvec_to;
884 donelen += wbuf_retlen;
888 wbuf_retlen = jffs2_fill_wbuf(c, v, vlen);
890 ret = __jffs2_flush_wbuf(c,
NOPAD);
895 outvec_to += wbuf_retlen;
896 donelen += wbuf_retlen;
911 if (c->wbuf_len && ino)
912 jffs2_wbuf_dirties_inode(c, ino);
942 size_t *retlen,
const u_char *buf)
949 vecs[0].
iov_base = (
unsigned char *) buf;
959 loff_t orbf = 0, owbf = 0, lwbf = 0;
971 pr_warn(
"mtd->read(0x%zx bytes from 0x%llx) returned ECC error\n",
994 if (ofs >= c->wbuf_ofs) {
995 owbf = (ofs - c->wbuf_ofs);
996 if (owbf > c->wbuf_len)
998 lwbf = c->wbuf_len - owbf;
1002 orbf = (c->wbuf_ofs - ofs);
1006 if (lwbf > c->wbuf_len)
1010 memcpy(buf+orbf,c->wbuf+owbf,lwbf);
1017 #define NR_OOB_SCAN_PAGES 4
1020 #define OOB_CM_SIZE 8
1048 pr_err(
"cannot read OOB for EB at %08x, requested %zd bytes, read %zd bytes, error %d\n",
1050 if (!ret || mtd_is_bitflip(ret))
1055 for(i = 0; i < ops.
ooblen; i++) {
1056 if (mode && i < cmlen)
1060 if (ops.
oobbuf[i] != 0xFF) {
1061 jffs2_dbg(2,
"Found %02x at %x in OOB for "
1090 pr_err(
"cannot read OOB for EB at %08x, requested %zd bytes, read %zd bytes, error %d\n",
1092 if (!ret || mtd_is_bitflip(ret))
1097 return !!
memcmp(&oob_cleanmarker, c->oobbuf, cmlen);
1113 ret = mtd_write_oob(c->
mtd, jeb->
offset, &ops);
1115 pr_err(
"cannot write OOB for EB at %08x, requested %zd bytes, read %zd bytes, error %d\n",
1141 pr_warn(
"marking eraseblock at %08x as bad\n", bad_offset);
1145 jffs2_dbg(1,
"%s(): Write failed for block at %08x: error %d\n",
1146 __func__, jeb->
offset, ret);
1165 spin_lock(&c->wbuf_dwork_lock);
1167 spin_unlock(&c->wbuf_dwork_lock);
1178 unsigned long delay;
1183 spin_lock(&c->wbuf_dwork_lock);
1184 if (!c->wbuf_queued) {
1190 spin_unlock(&c->wbuf_dwork_lock);
1197 if (!c->
mtd->oobsize)
1203 if (!oinfo || oinfo->
oobavail == 0) {
1204 pr_err(
"inconsistent device description\n");
1217 c->wbuf_ofs = 0xFFFFFFFF;
1229 #ifdef CONFIG_JFFS2_FS_WBUF_VERIFY
1231 if (!c->wbuf_verify) {
1242 #ifdef CONFIG_JFFS2_FS_WBUF_VERIFY
1243 kfree(c->wbuf_verify);
1280 c->wbuf_ofs = 0xFFFFFFFF;
1285 #ifdef CONFIG_JFFS2_FS_WBUF_VERIFY
1287 if (!c->wbuf_verify) {
1294 pr_info(
"write-buffering enabled buffer (%d) erasesize (%d)\n",
1301 #ifdef CONFIG_JFFS2_FS_WBUF_VERIFY
1302 kfree(c->wbuf_verify);
1318 c->wbuf_ofs = 0xFFFFFFFF;
1324 #ifdef CONFIG_JFFS2_FS_WBUF_VERIFY
1326 if (!c->wbuf_verify) {
1335 #ifdef CONFIG_JFFS2_FS_WBUF_VERIFY
1336 kfree(c->wbuf_verify);
1344 if (c->
mtd->writesize == 1)
1353 c->wbuf_ofs = 0xFFFFFFFF;
1358 pr_info(
"write-buffering enabled buffer (%d) erasesize (%d)\n",