11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * JFFS2 -- Journalling Flash File System, Version 2. 31da177e4SLinus Torvalds * 4c00c310eSDavid Woodhouse * Copyright © 2001-2007 Red Hat, Inc. 5c00c310eSDavid Woodhouse * Copyright © 2004 Thomas Gleixner <tglx@linutronix.de> 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Created by David Woodhouse <dwmw2@infradead.org> 81da177e4SLinus Torvalds * Modified debugged and enhanced by Thomas Gleixner <tglx@linutronix.de> 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * For licensing information, see the file 'LICENCE' in this directory. 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds */ 131da177e4SLinus Torvalds 141da177e4SLinus Torvalds #include <linux/kernel.h> 151da177e4SLinus Torvalds #include <linux/slab.h> 161da177e4SLinus Torvalds #include <linux/mtd/mtd.h> 171da177e4SLinus Torvalds #include <linux/crc32.h> 181da177e4SLinus Torvalds #include <linux/mtd/nand.h> 194e57b681STim Schmielau #include <linux/jiffies.h> 20914e2637SAl Viro #include <linux/sched.h> 214e57b681STim Schmielau 221da177e4SLinus Torvalds #include "nodelist.h" 231da177e4SLinus Torvalds 241da177e4SLinus Torvalds /* For testing write failures */ 251da177e4SLinus Torvalds #undef BREAKME 261da177e4SLinus Torvalds #undef BREAKMEHEADER 271da177e4SLinus Torvalds 281da177e4SLinus Torvalds #ifdef BREAKME 291da177e4SLinus Torvalds static unsigned char *brokenbuf; 301da177e4SLinus Torvalds #endif 311da177e4SLinus Torvalds 32daba5cc4SArtem B. Bityutskiy #define PAGE_DIV(x) ( ((unsigned long)(x) / (unsigned long)(c->wbuf_pagesize)) * (unsigned long)(c->wbuf_pagesize) ) 33daba5cc4SArtem B. Bityutskiy #define PAGE_MOD(x) ( (unsigned long)(x) % (unsigned long)(c->wbuf_pagesize) ) 34daba5cc4SArtem B. Bityutskiy 351da177e4SLinus Torvalds /* max. erase failures before we mark a block bad */ 361da177e4SLinus Torvalds #define MAX_ERASE_FAILURES 2 371da177e4SLinus Torvalds 381da177e4SLinus Torvalds struct jffs2_inodirty { 391da177e4SLinus Torvalds uint32_t ino; 401da177e4SLinus Torvalds struct jffs2_inodirty *next; 411da177e4SLinus Torvalds }; 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds static struct jffs2_inodirty inodirty_nomem; 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds static int jffs2_wbuf_pending_for_ino(struct jffs2_sb_info *c, uint32_t ino) 461da177e4SLinus Torvalds { 471da177e4SLinus Torvalds struct jffs2_inodirty *this = c->wbuf_inodes; 481da177e4SLinus Torvalds 491da177e4SLinus Torvalds /* If a malloc failed, consider _everything_ dirty */ 501da177e4SLinus Torvalds if (this == &inodirty_nomem) 511da177e4SLinus Torvalds return 1; 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds /* If ino == 0, _any_ non-GC writes mean 'yes' */ 541da177e4SLinus Torvalds if (this && !ino) 551da177e4SLinus Torvalds return 1; 561da177e4SLinus Torvalds 571da177e4SLinus Torvalds /* Look to see if the inode in question is pending in the wbuf */ 581da177e4SLinus Torvalds while (this) { 591da177e4SLinus Torvalds if (this->ino == ino) 601da177e4SLinus Torvalds return 1; 611da177e4SLinus Torvalds this = this->next; 621da177e4SLinus Torvalds } 631da177e4SLinus Torvalds return 0; 641da177e4SLinus Torvalds } 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds static void jffs2_clear_wbuf_ino_list(struct jffs2_sb_info *c) 671da177e4SLinus Torvalds { 681da177e4SLinus Torvalds struct jffs2_inodirty *this; 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds this = c->wbuf_inodes; 711da177e4SLinus Torvalds 721da177e4SLinus Torvalds if (this != &inodirty_nomem) { 731da177e4SLinus Torvalds while (this) { 741da177e4SLinus Torvalds struct jffs2_inodirty *next = this->next; 751da177e4SLinus Torvalds kfree(this); 761da177e4SLinus Torvalds this = next; 771da177e4SLinus Torvalds } 781da177e4SLinus Torvalds } 791da177e4SLinus Torvalds c->wbuf_inodes = NULL; 801da177e4SLinus Torvalds } 811da177e4SLinus Torvalds 821da177e4SLinus Torvalds static void jffs2_wbuf_dirties_inode(struct jffs2_sb_info *c, uint32_t ino) 831da177e4SLinus Torvalds { 841da177e4SLinus Torvalds struct jffs2_inodirty *new; 851da177e4SLinus Torvalds 861da177e4SLinus Torvalds /* Mark the superblock dirty so that kupdated will flush... */ 8764a5c2ebSJoakim Tjernlund jffs2_dirty_trigger(c); 881da177e4SLinus Torvalds 891da177e4SLinus Torvalds if (jffs2_wbuf_pending_for_ino(c, ino)) 901da177e4SLinus Torvalds return; 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds new = kmalloc(sizeof(*new), GFP_KERNEL); 931da177e4SLinus Torvalds if (!new) { 941da177e4SLinus Torvalds D1(printk(KERN_DEBUG "No memory to allocate inodirty. Fallback to all considered dirty\n")); 951da177e4SLinus Torvalds jffs2_clear_wbuf_ino_list(c); 961da177e4SLinus Torvalds c->wbuf_inodes = &inodirty_nomem; 971da177e4SLinus Torvalds return; 981da177e4SLinus Torvalds } 991da177e4SLinus Torvalds new->ino = ino; 1001da177e4SLinus Torvalds new->next = c->wbuf_inodes; 1011da177e4SLinus Torvalds c->wbuf_inodes = new; 1021da177e4SLinus Torvalds return; 1031da177e4SLinus Torvalds } 1041da177e4SLinus Torvalds 1051da177e4SLinus Torvalds static inline void jffs2_refile_wbuf_blocks(struct jffs2_sb_info *c) 1061da177e4SLinus Torvalds { 1071da177e4SLinus Torvalds struct list_head *this, *next; 1081da177e4SLinus Torvalds static int n; 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds if (list_empty(&c->erasable_pending_wbuf_list)) 1111da177e4SLinus Torvalds return; 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds list_for_each_safe(this, next, &c->erasable_pending_wbuf_list) { 1141da177e4SLinus Torvalds struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list); 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds D1(printk(KERN_DEBUG "Removing eraseblock at 0x%08x from erasable_pending_wbuf_list...\n", jeb->offset)); 1171da177e4SLinus Torvalds list_del(this); 1181da177e4SLinus Torvalds if ((jiffies + (n++)) & 127) { 1191da177e4SLinus Torvalds /* Most of the time, we just erase it immediately. Otherwise we 1201da177e4SLinus Torvalds spend ages scanning it on mount, etc. */ 1211da177e4SLinus Torvalds D1(printk(KERN_DEBUG "...and adding to erase_pending_list\n")); 1221da177e4SLinus Torvalds list_add_tail(&jeb->list, &c->erase_pending_list); 1231da177e4SLinus Torvalds c->nr_erasing_blocks++; 124ae3b6ba0SDavid Woodhouse jffs2_garbage_collect_trigger(c); 1251da177e4SLinus Torvalds } else { 1261da177e4SLinus Torvalds /* Sometimes, however, we leave it elsewhere so it doesn't get 1271da177e4SLinus Torvalds immediately reused, and we spread the load a bit. */ 1281da177e4SLinus Torvalds D1(printk(KERN_DEBUG "...and adding to erasable_list\n")); 1291da177e4SLinus Torvalds list_add_tail(&jeb->list, &c->erasable_list); 1301da177e4SLinus Torvalds } 1311da177e4SLinus Torvalds } 1321da177e4SLinus Torvalds } 1331da177e4SLinus Torvalds 1347f716cf3SEstelle Hammache #define REFILE_NOTEMPTY 0 1357f716cf3SEstelle Hammache #define REFILE_ANYWAY 1 1367f716cf3SEstelle Hammache 1377f716cf3SEstelle Hammache static void jffs2_block_refile(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, int allow_empty) 1381da177e4SLinus Torvalds { 1391da177e4SLinus Torvalds D1(printk("About to refile bad block at %08x\n", jeb->offset)); 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds /* File the existing block on the bad_used_list.... */ 1421da177e4SLinus Torvalds if (c->nextblock == jeb) 1431da177e4SLinus Torvalds c->nextblock = NULL; 1441da177e4SLinus Torvalds else /* Not sure this should ever happen... need more coffee */ 1451da177e4SLinus Torvalds list_del(&jeb->list); 1461da177e4SLinus Torvalds if (jeb->first_node) { 1471da177e4SLinus Torvalds D1(printk("Refiling block at %08x to bad_used_list\n", jeb->offset)); 1481da177e4SLinus Torvalds list_add(&jeb->list, &c->bad_used_list); 1491da177e4SLinus Torvalds } else { 1509b88f473SEstelle Hammache BUG_ON(allow_empty == REFILE_NOTEMPTY); 1511da177e4SLinus Torvalds /* It has to have had some nodes or we couldn't be here */ 1521da177e4SLinus Torvalds D1(printk("Refiling block at %08x to erase_pending_list\n", jeb->offset)); 1531da177e4SLinus Torvalds list_add(&jeb->list, &c->erase_pending_list); 1541da177e4SLinus Torvalds c->nr_erasing_blocks++; 155ae3b6ba0SDavid Woodhouse jffs2_garbage_collect_trigger(c); 1561da177e4SLinus Torvalds } 1571da177e4SLinus Torvalds 1589bfeb691SDavid Woodhouse if (!jffs2_prealloc_raw_node_refs(c, jeb, 1)) { 1599bfeb691SDavid Woodhouse uint32_t oldfree = jeb->free_size; 1609bfeb691SDavid Woodhouse 1619bfeb691SDavid Woodhouse jffs2_link_node_ref(c, jeb, 1629bfeb691SDavid Woodhouse (jeb->offset+c->sector_size-oldfree) | REF_OBSOLETE, 1639bfeb691SDavid Woodhouse oldfree, NULL); 1649bfeb691SDavid Woodhouse /* convert to wasted */ 1659bfeb691SDavid Woodhouse c->wasted_size += oldfree; 1669bfeb691SDavid Woodhouse jeb->wasted_size += oldfree; 1679bfeb691SDavid Woodhouse c->dirty_size -= oldfree; 1689bfeb691SDavid Woodhouse jeb->dirty_size -= oldfree; 1699bfeb691SDavid Woodhouse } 1701da177e4SLinus Torvalds 171e0c8e42fSArtem B. Bityutskiy jffs2_dbg_dump_block_lists_nolock(c); 172e0c8e42fSArtem B. Bityutskiy jffs2_dbg_acct_sanity_check_nolock(c,jeb); 173e0c8e42fSArtem B. Bityutskiy jffs2_dbg_acct_paranoia_check_nolock(c, jeb); 1741da177e4SLinus Torvalds } 1751da177e4SLinus Torvalds 1769bfeb691SDavid Woodhouse static struct jffs2_raw_node_ref **jffs2_incore_replace_raw(struct jffs2_sb_info *c, 1779bfeb691SDavid Woodhouse struct jffs2_inode_info *f, 1789bfeb691SDavid Woodhouse struct jffs2_raw_node_ref *raw, 1799bfeb691SDavid Woodhouse union jffs2_node_union *node) 1809bfeb691SDavid Woodhouse { 1819bfeb691SDavid Woodhouse struct jffs2_node_frag *frag; 1829bfeb691SDavid Woodhouse struct jffs2_full_dirent *fd; 1839bfeb691SDavid Woodhouse 1849bfeb691SDavid Woodhouse dbg_noderef("incore_replace_raw: node at %p is {%04x,%04x}\n", 1859bfeb691SDavid Woodhouse node, je16_to_cpu(node->u.magic), je16_to_cpu(node->u.nodetype)); 1869bfeb691SDavid Woodhouse 1879bfeb691SDavid Woodhouse BUG_ON(je16_to_cpu(node->u.magic) != 0x1985 && 1889bfeb691SDavid Woodhouse je16_to_cpu(node->u.magic) != 0); 1899bfeb691SDavid Woodhouse 1909bfeb691SDavid Woodhouse switch (je16_to_cpu(node->u.nodetype)) { 1919bfeb691SDavid Woodhouse case JFFS2_NODETYPE_INODE: 192ddc58bd6SDavid Woodhouse if (f->metadata && f->metadata->raw == raw) { 193ddc58bd6SDavid Woodhouse dbg_noderef("Will replace ->raw in f->metadata at %p\n", f->metadata); 194ddc58bd6SDavid Woodhouse return &f->metadata->raw; 195ddc58bd6SDavid Woodhouse } 1969bfeb691SDavid Woodhouse frag = jffs2_lookup_node_frag(&f->fragtree, je32_to_cpu(node->i.offset)); 1979bfeb691SDavid Woodhouse BUG_ON(!frag); 1989bfeb691SDavid Woodhouse /* Find a frag which refers to the full_dnode we want to modify */ 1999bfeb691SDavid Woodhouse while (!frag->node || frag->node->raw != raw) { 2009bfeb691SDavid Woodhouse frag = frag_next(frag); 2019bfeb691SDavid Woodhouse BUG_ON(!frag); 2029bfeb691SDavid Woodhouse } 2039bfeb691SDavid Woodhouse dbg_noderef("Will replace ->raw in full_dnode at %p\n", frag->node); 2049bfeb691SDavid Woodhouse return &frag->node->raw; 2059bfeb691SDavid Woodhouse 2069bfeb691SDavid Woodhouse case JFFS2_NODETYPE_DIRENT: 2079bfeb691SDavid Woodhouse for (fd = f->dents; fd; fd = fd->next) { 2089bfeb691SDavid Woodhouse if (fd->raw == raw) { 2099bfeb691SDavid Woodhouse dbg_noderef("Will replace ->raw in full_dirent at %p\n", fd); 2109bfeb691SDavid Woodhouse return &fd->raw; 2119bfeb691SDavid Woodhouse } 2129bfeb691SDavid Woodhouse } 2139bfeb691SDavid Woodhouse BUG(); 214ddc58bd6SDavid Woodhouse 2159bfeb691SDavid Woodhouse default: 2169bfeb691SDavid Woodhouse dbg_noderef("Don't care about replacing raw for nodetype %x\n", 2179bfeb691SDavid Woodhouse je16_to_cpu(node->u.nodetype)); 2189bfeb691SDavid Woodhouse break; 2199bfeb691SDavid Woodhouse } 2209bfeb691SDavid Woodhouse return NULL; 2219bfeb691SDavid Woodhouse } 2229bfeb691SDavid Woodhouse 223a6bc432eSDavid Woodhouse #ifdef CONFIG_JFFS2_FS_WBUF_VERIFY 224a6bc432eSDavid Woodhouse static int jffs2_verify_write(struct jffs2_sb_info *c, unsigned char *buf, 225a6bc432eSDavid Woodhouse uint32_t ofs) 226a6bc432eSDavid Woodhouse { 227a6bc432eSDavid Woodhouse int ret; 228a6bc432eSDavid Woodhouse size_t retlen; 229a6bc432eSDavid Woodhouse char *eccstr; 230a6bc432eSDavid Woodhouse 231329ad399SArtem Bityutskiy ret = mtd_read(c->mtd, ofs, c->wbuf_pagesize, &retlen, c->wbuf_verify); 232a6bc432eSDavid Woodhouse if (ret && ret != -EUCLEAN && ret != -EBADMSG) { 233a6bc432eSDavid Woodhouse printk(KERN_WARNING "jffs2_verify_write(): Read back of page at %08x failed: %d\n", c->wbuf_ofs, ret); 234a6bc432eSDavid Woodhouse return ret; 235a6bc432eSDavid Woodhouse } else if (retlen != c->wbuf_pagesize) { 236a6bc432eSDavid Woodhouse printk(KERN_WARNING "jffs2_verify_write(): Read back of page at %08x gave short read: %zd not %d.\n", ofs, retlen, c->wbuf_pagesize); 237a6bc432eSDavid Woodhouse return -EIO; 238a6bc432eSDavid Woodhouse } 239a6bc432eSDavid Woodhouse if (!memcmp(buf, c->wbuf_verify, c->wbuf_pagesize)) 240a6bc432eSDavid Woodhouse return 0; 241a6bc432eSDavid Woodhouse 242a6bc432eSDavid Woodhouse if (ret == -EUCLEAN) 243a6bc432eSDavid Woodhouse eccstr = "corrected"; 244a6bc432eSDavid Woodhouse else if (ret == -EBADMSG) 245a6bc432eSDavid Woodhouse eccstr = "correction failed"; 246a6bc432eSDavid Woodhouse else 247a6bc432eSDavid Woodhouse eccstr = "OK or unused"; 248a6bc432eSDavid Woodhouse 249a6bc432eSDavid Woodhouse printk(KERN_WARNING "Write verify error (ECC %s) at %08x. Wrote:\n", 250a6bc432eSDavid Woodhouse eccstr, c->wbuf_ofs); 251a6bc432eSDavid Woodhouse print_hex_dump(KERN_WARNING, "", DUMP_PREFIX_OFFSET, 16, 1, 252a6bc432eSDavid Woodhouse c->wbuf, c->wbuf_pagesize, 0); 253a6bc432eSDavid Woodhouse 254a6bc432eSDavid Woodhouse printk(KERN_WARNING "Read back:\n"); 255a6bc432eSDavid Woodhouse print_hex_dump(KERN_WARNING, "", DUMP_PREFIX_OFFSET, 16, 1, 256a6bc432eSDavid Woodhouse c->wbuf_verify, c->wbuf_pagesize, 0); 257a6bc432eSDavid Woodhouse 258a6bc432eSDavid Woodhouse return -EIO; 259a6bc432eSDavid Woodhouse } 260a6bc432eSDavid Woodhouse #else 261a6bc432eSDavid Woodhouse #define jffs2_verify_write(c,b,o) (0) 262a6bc432eSDavid Woodhouse #endif 263a6bc432eSDavid Woodhouse 2641da177e4SLinus Torvalds /* Recover from failure to write wbuf. Recover the nodes up to the 2651da177e4SLinus Torvalds * wbuf, not the one which we were starting to try to write. */ 2661da177e4SLinus Torvalds 2671da177e4SLinus Torvalds static void jffs2_wbuf_recover(struct jffs2_sb_info *c) 2681da177e4SLinus Torvalds { 2691da177e4SLinus Torvalds struct jffs2_eraseblock *jeb, *new_jeb; 2709bfeb691SDavid Woodhouse struct jffs2_raw_node_ref *raw, *next, *first_raw = NULL; 2711da177e4SLinus Torvalds size_t retlen; 2721da177e4SLinus Torvalds int ret; 2739bfeb691SDavid Woodhouse int nr_refile = 0; 2741da177e4SLinus Torvalds unsigned char *buf; 2751da177e4SLinus Torvalds uint32_t start, end, ofs, len; 2761da177e4SLinus Torvalds 277046b8b98SDavid Woodhouse jeb = &c->blocks[c->wbuf_ofs / c->sector_size]; 278046b8b98SDavid Woodhouse 2791da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 280180bfb31SVitaly Wool if (c->wbuf_ofs % c->mtd->erasesize) 2817f716cf3SEstelle Hammache jffs2_block_refile(c, jeb, REFILE_NOTEMPTY); 282180bfb31SVitaly Wool else 283180bfb31SVitaly Wool jffs2_block_refile(c, jeb, REFILE_ANYWAY); 2849bfeb691SDavid Woodhouse spin_unlock(&c->erase_completion_lock); 2859bfeb691SDavid Woodhouse 2869bfeb691SDavid Woodhouse BUG_ON(!ref_obsolete(jeb->last_node)); 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds /* Find the first node to be recovered, by skipping over every 2891da177e4SLinus Torvalds node which ends before the wbuf starts, or which is obsolete. */ 2909bfeb691SDavid Woodhouse for (next = raw = jeb->first_node; next; raw = next) { 2919bfeb691SDavid Woodhouse next = ref_next(raw); 2929bfeb691SDavid Woodhouse 2939bfeb691SDavid Woodhouse if (ref_obsolete(raw) || 2949bfeb691SDavid Woodhouse (next && ref_offset(next) <= c->wbuf_ofs)) { 2959bfeb691SDavid Woodhouse dbg_noderef("Skipping node at 0x%08x(%d)-0x%08x which is either before 0x%08x or obsolete\n", 2969bfeb691SDavid Woodhouse ref_offset(raw), ref_flags(raw), 2979bfeb691SDavid Woodhouse (ref_offset(raw) + ref_totlen(c, jeb, raw)), 2989bfeb691SDavid Woodhouse c->wbuf_ofs); 2999bfeb691SDavid Woodhouse continue; 3009bfeb691SDavid Woodhouse } 3019bfeb691SDavid Woodhouse dbg_noderef("First node to be recovered is at 0x%08x(%d)-0x%08x\n", 3029bfeb691SDavid Woodhouse ref_offset(raw), ref_flags(raw), 3039bfeb691SDavid Woodhouse (ref_offset(raw) + ref_totlen(c, jeb, raw))); 3049bfeb691SDavid Woodhouse 3059bfeb691SDavid Woodhouse first_raw = raw; 3069bfeb691SDavid Woodhouse break; 3071da177e4SLinus Torvalds } 3081da177e4SLinus Torvalds 3099bfeb691SDavid Woodhouse if (!first_raw) { 3101da177e4SLinus Torvalds /* All nodes were obsolete. Nothing to recover. */ 3111da177e4SLinus Torvalds D1(printk(KERN_DEBUG "No non-obsolete nodes to be recovered. Just filing block bad\n")); 3129bfeb691SDavid Woodhouse c->wbuf_len = 0; 3131da177e4SLinus Torvalds return; 3141da177e4SLinus Torvalds } 3151da177e4SLinus Torvalds 3169bfeb691SDavid Woodhouse start = ref_offset(first_raw); 3179bfeb691SDavid Woodhouse end = ref_offset(jeb->last_node); 3189bfeb691SDavid Woodhouse nr_refile = 1; 3191da177e4SLinus Torvalds 3209bfeb691SDavid Woodhouse /* Count the number of refs which need to be copied */ 3219bfeb691SDavid Woodhouse while ((raw = ref_next(raw)) != jeb->last_node) 3229bfeb691SDavid Woodhouse nr_refile++; 3231da177e4SLinus Torvalds 3249bfeb691SDavid Woodhouse dbg_noderef("wbuf recover %08x-%08x (%d bytes in %d nodes)\n", 3259bfeb691SDavid Woodhouse start, end, end - start, nr_refile); 3261da177e4SLinus Torvalds 3271da177e4SLinus Torvalds buf = NULL; 3281da177e4SLinus Torvalds if (start < c->wbuf_ofs) { 3291da177e4SLinus Torvalds /* First affected node was already partially written. 3301da177e4SLinus Torvalds * Attempt to reread the old data into our buffer. */ 3311da177e4SLinus Torvalds 3321da177e4SLinus Torvalds buf = kmalloc(end - start, GFP_KERNEL); 3331da177e4SLinus Torvalds if (!buf) { 3341da177e4SLinus Torvalds printk(KERN_CRIT "Malloc failure in wbuf recovery. Data loss ensues.\n"); 3351da177e4SLinus Torvalds 3361da177e4SLinus Torvalds goto read_failed; 3371da177e4SLinus Torvalds } 3381da177e4SLinus Torvalds 3391da177e4SLinus Torvalds /* Do the read... */ 340329ad399SArtem Bityutskiy ret = mtd_read(c->mtd, start, c->wbuf_ofs - start, &retlen, 341329ad399SArtem Bityutskiy buf); 3421da177e4SLinus Torvalds 3439a1fcdfdSThomas Gleixner /* ECC recovered ? */ 3449a1fcdfdSThomas Gleixner if ((ret == -EUCLEAN || ret == -EBADMSG) && 3459a1fcdfdSThomas Gleixner (retlen == c->wbuf_ofs - start)) 3461da177e4SLinus Torvalds ret = 0; 3479a1fcdfdSThomas Gleixner 3481da177e4SLinus Torvalds if (ret || retlen != c->wbuf_ofs - start) { 3491da177e4SLinus Torvalds printk(KERN_CRIT "Old data are already lost in wbuf recovery. Data loss ensues.\n"); 3501da177e4SLinus Torvalds 3511da177e4SLinus Torvalds kfree(buf); 3521da177e4SLinus Torvalds buf = NULL; 3531da177e4SLinus Torvalds read_failed: 3549bfeb691SDavid Woodhouse first_raw = ref_next(first_raw); 3559bfeb691SDavid Woodhouse nr_refile--; 3569bfeb691SDavid Woodhouse while (first_raw && ref_obsolete(first_raw)) { 3579bfeb691SDavid Woodhouse first_raw = ref_next(first_raw); 3589bfeb691SDavid Woodhouse nr_refile--; 3599bfeb691SDavid Woodhouse } 3609bfeb691SDavid Woodhouse 3611da177e4SLinus Torvalds /* If this was the only node to be recovered, give up */ 3629bfeb691SDavid Woodhouse if (!first_raw) { 3639bfeb691SDavid Woodhouse c->wbuf_len = 0; 3641da177e4SLinus Torvalds return; 3659bfeb691SDavid Woodhouse } 3661da177e4SLinus Torvalds 3671da177e4SLinus Torvalds /* It wasn't. Go on and try to recover nodes complete in the wbuf */ 3689bfeb691SDavid Woodhouse start = ref_offset(first_raw); 3699bfeb691SDavid Woodhouse dbg_noderef("wbuf now recover %08x-%08x (%d bytes in %d nodes)\n", 3709bfeb691SDavid Woodhouse start, end, end - start, nr_refile); 3719bfeb691SDavid Woodhouse 3721da177e4SLinus Torvalds } else { 3731da177e4SLinus Torvalds /* Read succeeded. Copy the remaining data from the wbuf */ 3741da177e4SLinus Torvalds memcpy(buf + (c->wbuf_ofs - start), c->wbuf, end - c->wbuf_ofs); 3751da177e4SLinus Torvalds } 3761da177e4SLinus Torvalds } 3771da177e4SLinus Torvalds /* OK... we're to rewrite (end-start) bytes of data from first_raw onwards. 3781da177e4SLinus Torvalds Either 'buf' contains the data, or we find it in the wbuf */ 3791da177e4SLinus Torvalds 3801da177e4SLinus Torvalds /* ... and get an allocation of space from a shiny new block instead */ 3819fe4854cSDavid Woodhouse ret = jffs2_reserve_space_gc(c, end-start, &len, JFFS2_SUMMARY_NOSUM_SIZE); 3821da177e4SLinus Torvalds if (ret) { 3831da177e4SLinus Torvalds printk(KERN_WARNING "Failed to allocate space for wbuf recovery. Data loss ensues.\n"); 3841da177e4SLinus Torvalds kfree(buf); 3851da177e4SLinus Torvalds return; 3861da177e4SLinus Torvalds } 3879bfeb691SDavid Woodhouse 3887f762ab2SAdrian Hunter /* The summary is not recovered, so it must be disabled for this erase block */ 3897f762ab2SAdrian Hunter jffs2_sum_disable_collecting(c->summary); 3907f762ab2SAdrian Hunter 3919bfeb691SDavid Woodhouse ret = jffs2_prealloc_raw_node_refs(c, c->nextblock, nr_refile); 3929bfeb691SDavid Woodhouse if (ret) { 3939bfeb691SDavid Woodhouse printk(KERN_WARNING "Failed to allocate node refs for wbuf recovery. Data loss ensues.\n"); 3949bfeb691SDavid Woodhouse kfree(buf); 3959bfeb691SDavid Woodhouse return; 3969bfeb691SDavid Woodhouse } 3979bfeb691SDavid Woodhouse 3989fe4854cSDavid Woodhouse ofs = write_ofs(c); 3999fe4854cSDavid Woodhouse 4001da177e4SLinus Torvalds if (end-start >= c->wbuf_pagesize) { 4017f716cf3SEstelle Hammache /* Need to do another write immediately, but it's possible 4027f716cf3SEstelle Hammache that this is just because the wbuf itself is completely 4037f716cf3SEstelle Hammache full, and there's nothing earlier read back from the 4047f716cf3SEstelle Hammache flash. Hence 'buf' isn't necessarily what we're writing 4057f716cf3SEstelle Hammache from. */ 4067f716cf3SEstelle Hammache unsigned char *rewrite_buf = buf?:c->wbuf; 4071da177e4SLinus Torvalds uint32_t towrite = (end-start) - ((end-start)%c->wbuf_pagesize); 4081da177e4SLinus Torvalds 4091da177e4SLinus Torvalds D1(printk(KERN_DEBUG "Write 0x%x bytes at 0x%08x in wbuf recover\n", 4101da177e4SLinus Torvalds towrite, ofs)); 4111da177e4SLinus Torvalds 4121da177e4SLinus Torvalds #ifdef BREAKMEHEADER 4131da177e4SLinus Torvalds static int breakme; 4141da177e4SLinus Torvalds if (breakme++ == 20) { 4151da177e4SLinus Torvalds printk(KERN_NOTICE "Faking write error at 0x%08x\n", ofs); 4161da177e4SLinus Torvalds breakme = 0; 417eda95cbfSArtem Bityutskiy mtd_write(c->mtd, ofs, towrite, &retlen, brokenbuf); 4181da177e4SLinus Torvalds ret = -EIO; 4191da177e4SLinus Torvalds } else 4201da177e4SLinus Torvalds #endif 421eda95cbfSArtem Bityutskiy ret = mtd_write(c->mtd, ofs, towrite, &retlen, 4229223a456SThomas Gleixner rewrite_buf); 4231da177e4SLinus Torvalds 424a6bc432eSDavid Woodhouse if (ret || retlen != towrite || jffs2_verify_write(c, rewrite_buf, ofs)) { 4251da177e4SLinus Torvalds /* Argh. We tried. Really we did. */ 4261da177e4SLinus Torvalds printk(KERN_CRIT "Recovery of wbuf failed due to a second write error\n"); 4271da177e4SLinus Torvalds kfree(buf); 4281da177e4SLinus Torvalds 4292f785402SDavid Woodhouse if (retlen) 4309bfeb691SDavid Woodhouse jffs2_add_physical_node_ref(c, ofs | REF_OBSOLETE, ref_totlen(c, jeb, first_raw), NULL); 4311da177e4SLinus Torvalds 4321da177e4SLinus Torvalds return; 4331da177e4SLinus Torvalds } 4341da177e4SLinus Torvalds printk(KERN_NOTICE "Recovery of wbuf succeeded to %08x\n", ofs); 4351da177e4SLinus Torvalds 4361da177e4SLinus Torvalds c->wbuf_len = (end - start) - towrite; 4371da177e4SLinus Torvalds c->wbuf_ofs = ofs + towrite; 4387f716cf3SEstelle Hammache memmove(c->wbuf, rewrite_buf + towrite, c->wbuf_len); 4391da177e4SLinus Torvalds /* Don't muck about with c->wbuf_inodes. False positives are harmless. */ 4401da177e4SLinus Torvalds } else { 4411da177e4SLinus Torvalds /* OK, now we're left with the dregs in whichever buffer we're using */ 4421da177e4SLinus Torvalds if (buf) { 4431da177e4SLinus Torvalds memcpy(c->wbuf, buf, end-start); 4441da177e4SLinus Torvalds } else { 4451da177e4SLinus Torvalds memmove(c->wbuf, c->wbuf + (start - c->wbuf_ofs), end - start); 4461da177e4SLinus Torvalds } 4471da177e4SLinus Torvalds c->wbuf_ofs = ofs; 4481da177e4SLinus Torvalds c->wbuf_len = end - start; 4491da177e4SLinus Torvalds } 4501da177e4SLinus Torvalds 4511da177e4SLinus Torvalds /* Now sort out the jffs2_raw_node_refs, moving them from the old to the next block */ 4521da177e4SLinus Torvalds new_jeb = &c->blocks[ofs / c->sector_size]; 4531da177e4SLinus Torvalds 4541da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 4559bfeb691SDavid Woodhouse for (raw = first_raw; raw != jeb->last_node; raw = ref_next(raw)) { 4569bfeb691SDavid Woodhouse uint32_t rawlen = ref_totlen(c, jeb, raw); 4579bfeb691SDavid Woodhouse struct jffs2_inode_cache *ic; 4589bfeb691SDavid Woodhouse struct jffs2_raw_node_ref *new_ref; 4599bfeb691SDavid Woodhouse struct jffs2_raw_node_ref **adjust_ref = NULL; 4609bfeb691SDavid Woodhouse struct jffs2_inode_info *f = NULL; 4611da177e4SLinus Torvalds 4621da177e4SLinus Torvalds D1(printk(KERN_DEBUG "Refiling block of %08x at %08x(%d) to %08x\n", 4639bfeb691SDavid Woodhouse rawlen, ref_offset(raw), ref_flags(raw), ofs)); 4641da177e4SLinus Torvalds 4659bfeb691SDavid Woodhouse ic = jffs2_raw_ref_to_ic(raw); 4669bfeb691SDavid Woodhouse 4679bfeb691SDavid Woodhouse /* Ick. This XATTR mess should be fixed shortly... */ 4689bfeb691SDavid Woodhouse if (ic && ic->class == RAWNODE_CLASS_XATTR_DATUM) { 4699bfeb691SDavid Woodhouse struct jffs2_xattr_datum *xd = (void *)ic; 4709bfeb691SDavid Woodhouse BUG_ON(xd->node != raw); 4719bfeb691SDavid Woodhouse adjust_ref = &xd->node; 4729bfeb691SDavid Woodhouse raw->next_in_ino = NULL; 4739bfeb691SDavid Woodhouse ic = NULL; 4749bfeb691SDavid Woodhouse } else if (ic && ic->class == RAWNODE_CLASS_XATTR_REF) { 4759bfeb691SDavid Woodhouse struct jffs2_xattr_datum *xr = (void *)ic; 4769bfeb691SDavid Woodhouse BUG_ON(xr->node != raw); 4779bfeb691SDavid Woodhouse adjust_ref = &xr->node; 4789bfeb691SDavid Woodhouse raw->next_in_ino = NULL; 4799bfeb691SDavid Woodhouse ic = NULL; 4809bfeb691SDavid Woodhouse } else if (ic && ic->class == RAWNODE_CLASS_INODE_CACHE) { 4819bfeb691SDavid Woodhouse struct jffs2_raw_node_ref **p = &ic->nodes; 4829bfeb691SDavid Woodhouse 4839bfeb691SDavid Woodhouse /* Remove the old node from the per-inode list */ 4849bfeb691SDavid Woodhouse while (*p && *p != (void *)ic) { 4859bfeb691SDavid Woodhouse if (*p == raw) { 4869bfeb691SDavid Woodhouse (*p) = (raw->next_in_ino); 4879bfeb691SDavid Woodhouse raw->next_in_ino = NULL; 4889bfeb691SDavid Woodhouse break; 4899bfeb691SDavid Woodhouse } 4909bfeb691SDavid Woodhouse p = &((*p)->next_in_ino); 4919bfeb691SDavid Woodhouse } 4929bfeb691SDavid Woodhouse 4939bfeb691SDavid Woodhouse if (ic->state == INO_STATE_PRESENT && !ref_obsolete(raw)) { 4949bfeb691SDavid Woodhouse /* If it's an in-core inode, then we have to adjust any 4959bfeb691SDavid Woodhouse full_dirent or full_dnode structure to point to the 4969bfeb691SDavid Woodhouse new version instead of the old */ 49727c72b04SDavid Woodhouse f = jffs2_gc_fetch_inode(c, ic->ino, !ic->pino_nlink); 4989bfeb691SDavid Woodhouse if (IS_ERR(f)) { 4999bfeb691SDavid Woodhouse /* Should never happen; it _must_ be present */ 5009bfeb691SDavid Woodhouse JFFS2_ERROR("Failed to iget() ino #%u, err %ld\n", 5019bfeb691SDavid Woodhouse ic->ino, PTR_ERR(f)); 5029bfeb691SDavid Woodhouse BUG(); 5039bfeb691SDavid Woodhouse } 5049bfeb691SDavid Woodhouse /* We don't lock f->sem. There's a number of ways we could 5059bfeb691SDavid Woodhouse end up in here with it already being locked, and nobody's 5069bfeb691SDavid Woodhouse going to modify it on us anyway because we hold the 5079bfeb691SDavid Woodhouse alloc_sem. We're only changing one ->raw pointer too, 5089bfeb691SDavid Woodhouse which we can get away with without upsetting readers. */ 5099bfeb691SDavid Woodhouse adjust_ref = jffs2_incore_replace_raw(c, f, raw, 5109bfeb691SDavid Woodhouse (void *)(buf?:c->wbuf) + (ref_offset(raw) - start)); 5119bfeb691SDavid Woodhouse } else if (unlikely(ic->state != INO_STATE_PRESENT && 5129bfeb691SDavid Woodhouse ic->state != INO_STATE_CHECKEDABSENT && 5139bfeb691SDavid Woodhouse ic->state != INO_STATE_GC)) { 5149bfeb691SDavid Woodhouse JFFS2_ERROR("Inode #%u is in strange state %d!\n", ic->ino, ic->state); 5159bfeb691SDavid Woodhouse BUG(); 5169bfeb691SDavid Woodhouse } 5179bfeb691SDavid Woodhouse } 5189bfeb691SDavid Woodhouse 5199bfeb691SDavid Woodhouse new_ref = jffs2_link_node_ref(c, new_jeb, ofs | ref_flags(raw), rawlen, ic); 5209bfeb691SDavid Woodhouse 5219bfeb691SDavid Woodhouse if (adjust_ref) { 5229bfeb691SDavid Woodhouse BUG_ON(*adjust_ref != raw); 5239bfeb691SDavid Woodhouse *adjust_ref = new_ref; 5249bfeb691SDavid Woodhouse } 5259bfeb691SDavid Woodhouse if (f) 5269bfeb691SDavid Woodhouse jffs2_gc_release_inode(c, f); 5279bfeb691SDavid Woodhouse 5289bfeb691SDavid Woodhouse if (!ref_obsolete(raw)) { 5291da177e4SLinus Torvalds jeb->dirty_size += rawlen; 5301da177e4SLinus Torvalds jeb->used_size -= rawlen; 5311da177e4SLinus Torvalds c->dirty_size += rawlen; 5329bfeb691SDavid Woodhouse c->used_size -= rawlen; 5339bfeb691SDavid Woodhouse raw->flash_offset = ref_offset(raw) | REF_OBSOLETE; 5349bfeb691SDavid Woodhouse BUG_ON(raw->next_in_ino); 5351da177e4SLinus Torvalds } 5361da177e4SLinus Torvalds ofs += rawlen; 5371da177e4SLinus Torvalds } 5381da177e4SLinus Torvalds 5399bfeb691SDavid Woodhouse kfree(buf); 5409bfeb691SDavid Woodhouse 5411da177e4SLinus Torvalds /* Fix up the original jeb now it's on the bad_list */ 5429bfeb691SDavid Woodhouse if (first_raw == jeb->first_node) { 5431da177e4SLinus Torvalds D1(printk(KERN_DEBUG "Failing block at %08x is now empty. Moving to erase_pending_list\n", jeb->offset)); 544f116629dSAkinobu Mita list_move(&jeb->list, &c->erase_pending_list); 5451da177e4SLinus Torvalds c->nr_erasing_blocks++; 546ae3b6ba0SDavid Woodhouse jffs2_garbage_collect_trigger(c); 5471da177e4SLinus Torvalds } 5481da177e4SLinus Torvalds 549e0c8e42fSArtem B. Bityutskiy jffs2_dbg_acct_sanity_check_nolock(c, jeb); 550e0c8e42fSArtem B. Bityutskiy jffs2_dbg_acct_paranoia_check_nolock(c, jeb); 5511da177e4SLinus Torvalds 552e0c8e42fSArtem B. Bityutskiy jffs2_dbg_acct_sanity_check_nolock(c, new_jeb); 553e0c8e42fSArtem B. Bityutskiy jffs2_dbg_acct_paranoia_check_nolock(c, new_jeb); 5541da177e4SLinus Torvalds 5551da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 5561da177e4SLinus Torvalds 5579bfeb691SDavid Woodhouse D1(printk(KERN_DEBUG "wbuf recovery completed OK. wbuf_ofs 0x%08x, len 0x%x\n", c->wbuf_ofs, c->wbuf_len)); 5589bfeb691SDavid Woodhouse 5591da177e4SLinus Torvalds } 5601da177e4SLinus Torvalds 5611da177e4SLinus Torvalds /* Meaning of pad argument: 5621da177e4SLinus Torvalds 0: Do not pad. Probably pointless - we only ever use this when we can't pad anyway. 5631da177e4SLinus Torvalds 1: Pad, do not adjust nextblock free_size 5641da177e4SLinus Torvalds 2: Pad, adjust nextblock free_size 5651da177e4SLinus Torvalds */ 5661da177e4SLinus Torvalds #define NOPAD 0 5671da177e4SLinus Torvalds #define PAD_NOACCOUNT 1 5681da177e4SLinus Torvalds #define PAD_ACCOUNTING 2 5691da177e4SLinus Torvalds 5701da177e4SLinus Torvalds static int __jffs2_flush_wbuf(struct jffs2_sb_info *c, int pad) 5711da177e4SLinus Torvalds { 5729bfeb691SDavid Woodhouse struct jffs2_eraseblock *wbuf_jeb; 5731da177e4SLinus Torvalds int ret; 5741da177e4SLinus Torvalds size_t retlen; 5751da177e4SLinus Torvalds 5763be36675SAndrew Victor /* Nothing to do if not write-buffering the flash. In particular, we shouldn't 5771da177e4SLinus Torvalds del_timer() the timer we never initialised. */ 5783be36675SAndrew Victor if (!jffs2_is_writebuffered(c)) 5791da177e4SLinus Torvalds return 0; 5801da177e4SLinus Torvalds 58151b11e36SAlexey Khoroshilov if (!mutex_is_locked(&c->alloc_sem)) { 5821da177e4SLinus Torvalds printk(KERN_CRIT "jffs2_flush_wbuf() called with alloc_sem not locked!\n"); 5831da177e4SLinus Torvalds BUG(); 5841da177e4SLinus Torvalds } 5851da177e4SLinus Torvalds 5863be36675SAndrew Victor if (!c->wbuf_len) /* already checked c->wbuf above */ 5871da177e4SLinus Torvalds return 0; 5881da177e4SLinus Torvalds 5899bfeb691SDavid Woodhouse wbuf_jeb = &c->blocks[c->wbuf_ofs / c->sector_size]; 5909bfeb691SDavid Woodhouse if (jffs2_prealloc_raw_node_refs(c, wbuf_jeb, c->nextblock->allocated_refs + 1)) 5912f785402SDavid Woodhouse return -ENOMEM; 5922f785402SDavid Woodhouse 5931da177e4SLinus Torvalds /* claim remaining space on the page 5941da177e4SLinus Torvalds this happens, if we have a change to a new block, 5951da177e4SLinus Torvalds or if fsync forces us to flush the writebuffer. 5961da177e4SLinus Torvalds if we have a switch to next page, we will not have 5971da177e4SLinus Torvalds enough remaining space for this. 5981da177e4SLinus Torvalds */ 599daba5cc4SArtem B. Bityutskiy if (pad ) { 6001da177e4SLinus Torvalds c->wbuf_len = PAD(c->wbuf_len); 6011da177e4SLinus Torvalds 6021da177e4SLinus Torvalds /* Pad with JFFS2_DIRTY_BITMASK initially. this helps out ECC'd NOR 6031da177e4SLinus Torvalds with 8 byte page size */ 6041da177e4SLinus Torvalds memset(c->wbuf + c->wbuf_len, 0, c->wbuf_pagesize - c->wbuf_len); 6051da177e4SLinus Torvalds 6061da177e4SLinus Torvalds if ( c->wbuf_len + sizeof(struct jffs2_unknown_node) < c->wbuf_pagesize) { 6071da177e4SLinus Torvalds struct jffs2_unknown_node *padnode = (void *)(c->wbuf + c->wbuf_len); 6081da177e4SLinus Torvalds padnode->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); 6091da177e4SLinus Torvalds padnode->nodetype = cpu_to_je16(JFFS2_NODETYPE_PADDING); 6101da177e4SLinus Torvalds padnode->totlen = cpu_to_je32(c->wbuf_pagesize - c->wbuf_len); 6111da177e4SLinus Torvalds padnode->hdr_crc = cpu_to_je32(crc32(0, padnode, sizeof(*padnode)-4)); 6121da177e4SLinus Torvalds } 6131da177e4SLinus Torvalds } 6141da177e4SLinus Torvalds /* else jffs2_flash_writev has actually filled in the rest of the 6151da177e4SLinus Torvalds buffer for us, and will deal with the node refs etc. later. */ 6161da177e4SLinus Torvalds 6171da177e4SLinus Torvalds #ifdef BREAKME 6181da177e4SLinus Torvalds static int breakme; 6191da177e4SLinus Torvalds if (breakme++ == 20) { 6201da177e4SLinus Torvalds printk(KERN_NOTICE "Faking write error at 0x%08x\n", c->wbuf_ofs); 6211da177e4SLinus Torvalds breakme = 0; 622eda95cbfSArtem Bityutskiy mtd_write(c->mtd, c->wbuf_ofs, c->wbuf_pagesize, &retlen, 6239223a456SThomas Gleixner brokenbuf); 6241da177e4SLinus Torvalds ret = -EIO; 6251da177e4SLinus Torvalds } else 6261da177e4SLinus Torvalds #endif 6271da177e4SLinus Torvalds 628eda95cbfSArtem Bityutskiy ret = mtd_write(c->mtd, c->wbuf_ofs, c->wbuf_pagesize, 629eda95cbfSArtem Bityutskiy &retlen, c->wbuf); 6301da177e4SLinus Torvalds 631a6bc432eSDavid Woodhouse if (ret) { 6321da177e4SLinus Torvalds printk(KERN_WARNING "jffs2_flush_wbuf(): Write failed with %d\n", ret); 633a6bc432eSDavid Woodhouse goto wfail; 634a6bc432eSDavid Woodhouse } else if (retlen != c->wbuf_pagesize) { 6351da177e4SLinus Torvalds printk(KERN_WARNING "jffs2_flush_wbuf(): Write was short: %zd instead of %d\n", 6361da177e4SLinus Torvalds retlen, c->wbuf_pagesize); 6371da177e4SLinus Torvalds ret = -EIO; 638a6bc432eSDavid Woodhouse goto wfail; 639a6bc432eSDavid Woodhouse } else if ((ret = jffs2_verify_write(c, c->wbuf, c->wbuf_ofs))) { 640a6bc432eSDavid Woodhouse wfail: 6411da177e4SLinus Torvalds jffs2_wbuf_recover(c); 6421da177e4SLinus Torvalds 6431da177e4SLinus Torvalds return ret; 6441da177e4SLinus Torvalds } 6451da177e4SLinus Torvalds 6461da177e4SLinus Torvalds /* Adjust free size of the block if we padded. */ 647daba5cc4SArtem B. Bityutskiy if (pad) { 6480bcc099dSDavid Woodhouse uint32_t waste = c->wbuf_pagesize - c->wbuf_len; 6491da177e4SLinus Torvalds 6501da177e4SLinus Torvalds D1(printk(KERN_DEBUG "jffs2_flush_wbuf() adjusting free_size of %sblock at %08x\n", 6519bfeb691SDavid Woodhouse (wbuf_jeb==c->nextblock)?"next":"", wbuf_jeb->offset)); 6521da177e4SLinus Torvalds 6531da177e4SLinus Torvalds /* wbuf_pagesize - wbuf_len is the amount of space that's to be 6541da177e4SLinus Torvalds padded. If there is less free space in the block than that, 6551da177e4SLinus Torvalds something screwed up */ 6569bfeb691SDavid Woodhouse if (wbuf_jeb->free_size < waste) { 6571da177e4SLinus Torvalds printk(KERN_CRIT "jffs2_flush_wbuf(): Accounting error. wbuf at 0x%08x has 0x%03x bytes, 0x%03x left.\n", 6580bcc099dSDavid Woodhouse c->wbuf_ofs, c->wbuf_len, waste); 6591da177e4SLinus Torvalds printk(KERN_CRIT "jffs2_flush_wbuf(): But free_size for block at 0x%08x is only 0x%08x\n", 6609bfeb691SDavid Woodhouse wbuf_jeb->offset, wbuf_jeb->free_size); 6611da177e4SLinus Torvalds BUG(); 6621da177e4SLinus Torvalds } 6630bcc099dSDavid Woodhouse 6640bcc099dSDavid Woodhouse spin_lock(&c->erase_completion_lock); 6650bcc099dSDavid Woodhouse 6669bfeb691SDavid Woodhouse jffs2_link_node_ref(c, wbuf_jeb, (c->wbuf_ofs + c->wbuf_len) | REF_OBSOLETE, waste, NULL); 6670bcc099dSDavid Woodhouse /* FIXME: that made it count as dirty. Convert to wasted */ 6689bfeb691SDavid Woodhouse wbuf_jeb->dirty_size -= waste; 6690bcc099dSDavid Woodhouse c->dirty_size -= waste; 6709bfeb691SDavid Woodhouse wbuf_jeb->wasted_size += waste; 6710bcc099dSDavid Woodhouse c->wasted_size += waste; 6720bcc099dSDavid Woodhouse } else 6730bcc099dSDavid Woodhouse spin_lock(&c->erase_completion_lock); 6741da177e4SLinus Torvalds 6751da177e4SLinus Torvalds /* Stick any now-obsoleted blocks on the erase_pending_list */ 6761da177e4SLinus Torvalds jffs2_refile_wbuf_blocks(c); 6771da177e4SLinus Torvalds jffs2_clear_wbuf_ino_list(c); 6781da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 6791da177e4SLinus Torvalds 6801da177e4SLinus Torvalds memset(c->wbuf,0xff,c->wbuf_pagesize); 6811da177e4SLinus Torvalds /* adjust write buffer offset, else we get a non contiguous write bug */ 6821da177e4SLinus Torvalds c->wbuf_ofs += c->wbuf_pagesize; 6831da177e4SLinus Torvalds c->wbuf_len = 0; 6841da177e4SLinus Torvalds return 0; 6851da177e4SLinus Torvalds } 6861da177e4SLinus Torvalds 6871da177e4SLinus Torvalds /* Trigger garbage collection to flush the write-buffer. 6881da177e4SLinus Torvalds If ino arg is zero, do it if _any_ real (i.e. not GC) writes are 6891da177e4SLinus Torvalds outstanding. If ino arg non-zero, do it only if a write for the 6901da177e4SLinus Torvalds given inode is outstanding. */ 6911da177e4SLinus Torvalds int jffs2_flush_wbuf_gc(struct jffs2_sb_info *c, uint32_t ino) 6921da177e4SLinus Torvalds { 6931da177e4SLinus Torvalds uint32_t old_wbuf_ofs; 6941da177e4SLinus Torvalds uint32_t old_wbuf_len; 6951da177e4SLinus Torvalds int ret = 0; 6961da177e4SLinus Torvalds 6971da177e4SLinus Torvalds D1(printk(KERN_DEBUG "jffs2_flush_wbuf_gc() called for ino #%u...\n", ino)); 6981da177e4SLinus Torvalds 6998aee6ac1SDavid Woodhouse if (!c->wbuf) 7008aee6ac1SDavid Woodhouse return 0; 7018aee6ac1SDavid Woodhouse 702ced22070SDavid Woodhouse mutex_lock(&c->alloc_sem); 7031da177e4SLinus Torvalds if (!jffs2_wbuf_pending_for_ino(c, ino)) { 7041da177e4SLinus Torvalds D1(printk(KERN_DEBUG "Ino #%d not pending in wbuf. Returning\n", ino)); 705ced22070SDavid Woodhouse mutex_unlock(&c->alloc_sem); 7061da177e4SLinus Torvalds return 0; 7071da177e4SLinus Torvalds } 7081da177e4SLinus Torvalds 7091da177e4SLinus Torvalds old_wbuf_ofs = c->wbuf_ofs; 7101da177e4SLinus Torvalds old_wbuf_len = c->wbuf_len; 7111da177e4SLinus Torvalds 7121da177e4SLinus Torvalds if (c->unchecked_size) { 7131da177e4SLinus Torvalds /* GC won't make any progress for a while */ 7141da177e4SLinus Torvalds D1(printk(KERN_DEBUG "jffs2_flush_wbuf_gc() padding. Not finished checking\n")); 7151da177e4SLinus Torvalds down_write(&c->wbuf_sem); 7161da177e4SLinus Torvalds ret = __jffs2_flush_wbuf(c, PAD_ACCOUNTING); 7177f716cf3SEstelle Hammache /* retry flushing wbuf in case jffs2_wbuf_recover 7187f716cf3SEstelle Hammache left some data in the wbuf */ 7197f716cf3SEstelle Hammache if (ret) 7207f716cf3SEstelle Hammache ret = __jffs2_flush_wbuf(c, PAD_ACCOUNTING); 7211da177e4SLinus Torvalds up_write(&c->wbuf_sem); 7221da177e4SLinus Torvalds } else while (old_wbuf_len && 7231da177e4SLinus Torvalds old_wbuf_ofs == c->wbuf_ofs) { 7241da177e4SLinus Torvalds 725ced22070SDavid Woodhouse mutex_unlock(&c->alloc_sem); 7261da177e4SLinus Torvalds 7271da177e4SLinus Torvalds D1(printk(KERN_DEBUG "jffs2_flush_wbuf_gc() calls gc pass\n")); 7281da177e4SLinus Torvalds 7291da177e4SLinus Torvalds ret = jffs2_garbage_collect_pass(c); 7301da177e4SLinus Torvalds if (ret) { 7311da177e4SLinus Torvalds /* GC failed. Flush it with padding instead */ 732ced22070SDavid Woodhouse mutex_lock(&c->alloc_sem); 7331da177e4SLinus Torvalds down_write(&c->wbuf_sem); 7341da177e4SLinus Torvalds ret = __jffs2_flush_wbuf(c, PAD_ACCOUNTING); 7357f716cf3SEstelle Hammache /* retry flushing wbuf in case jffs2_wbuf_recover 7367f716cf3SEstelle Hammache left some data in the wbuf */ 7377f716cf3SEstelle Hammache if (ret) 7387f716cf3SEstelle Hammache ret = __jffs2_flush_wbuf(c, PAD_ACCOUNTING); 7391da177e4SLinus Torvalds up_write(&c->wbuf_sem); 7401da177e4SLinus Torvalds break; 7411da177e4SLinus Torvalds } 742ced22070SDavid Woodhouse mutex_lock(&c->alloc_sem); 7431da177e4SLinus Torvalds } 7441da177e4SLinus Torvalds 7451da177e4SLinus Torvalds D1(printk(KERN_DEBUG "jffs2_flush_wbuf_gc() ends...\n")); 7461da177e4SLinus Torvalds 747ced22070SDavid Woodhouse mutex_unlock(&c->alloc_sem); 7481da177e4SLinus Torvalds return ret; 7491da177e4SLinus Torvalds } 7501da177e4SLinus Torvalds 7511da177e4SLinus Torvalds /* Pad write-buffer to end and write it, wasting space. */ 7521da177e4SLinus Torvalds int jffs2_flush_wbuf_pad(struct jffs2_sb_info *c) 7531da177e4SLinus Torvalds { 7541da177e4SLinus Torvalds int ret; 7551da177e4SLinus Torvalds 7568aee6ac1SDavid Woodhouse if (!c->wbuf) 7578aee6ac1SDavid Woodhouse return 0; 7588aee6ac1SDavid Woodhouse 7591da177e4SLinus Torvalds down_write(&c->wbuf_sem); 7601da177e4SLinus Torvalds ret = __jffs2_flush_wbuf(c, PAD_NOACCOUNT); 7617f716cf3SEstelle Hammache /* retry - maybe wbuf recover left some data in wbuf. */ 7627f716cf3SEstelle Hammache if (ret) 7637f716cf3SEstelle Hammache ret = __jffs2_flush_wbuf(c, PAD_NOACCOUNT); 7641da177e4SLinus Torvalds up_write(&c->wbuf_sem); 7651da177e4SLinus Torvalds 7661da177e4SLinus Torvalds return ret; 7671da177e4SLinus Torvalds } 7681da177e4SLinus Torvalds 769dcb09328SThomas Gleixner static size_t jffs2_fill_wbuf(struct jffs2_sb_info *c, const uint8_t *buf, 770dcb09328SThomas Gleixner size_t len) 771dcb09328SThomas Gleixner { 772dcb09328SThomas Gleixner if (len && !c->wbuf_len && (len >= c->wbuf_pagesize)) 773dcb09328SThomas Gleixner return 0; 774dcb09328SThomas Gleixner 775dcb09328SThomas Gleixner if (len > (c->wbuf_pagesize - c->wbuf_len)) 776dcb09328SThomas Gleixner len = c->wbuf_pagesize - c->wbuf_len; 777dcb09328SThomas Gleixner memcpy(c->wbuf + c->wbuf_len, buf, len); 778dcb09328SThomas Gleixner c->wbuf_len += (uint32_t) len; 779dcb09328SThomas Gleixner return len; 780dcb09328SThomas Gleixner } 781dcb09328SThomas Gleixner 782dcb09328SThomas Gleixner int jffs2_flash_writev(struct jffs2_sb_info *c, const struct kvec *invecs, 783dcb09328SThomas Gleixner unsigned long count, loff_t to, size_t *retlen, 784dcb09328SThomas Gleixner uint32_t ino) 785dcb09328SThomas Gleixner { 786dcb09328SThomas Gleixner struct jffs2_eraseblock *jeb; 787dcb09328SThomas Gleixner size_t wbuf_retlen, donelen = 0; 788dcb09328SThomas Gleixner uint32_t outvec_to = to; 789dcb09328SThomas Gleixner int ret, invec; 790dcb09328SThomas Gleixner 791dcb09328SThomas Gleixner /* If not writebuffered flash, don't bother */ 7923be36675SAndrew Victor if (!jffs2_is_writebuffered(c)) 7931da177e4SLinus Torvalds return jffs2_flash_direct_writev(c, invecs, count, to, retlen); 7941da177e4SLinus Torvalds 7951da177e4SLinus Torvalds down_write(&c->wbuf_sem); 7961da177e4SLinus Torvalds 7971da177e4SLinus Torvalds /* If wbuf_ofs is not initialized, set it to target address */ 7981da177e4SLinus Torvalds if (c->wbuf_ofs == 0xFFFFFFFF) { 7991da177e4SLinus Torvalds c->wbuf_ofs = PAGE_DIV(to); 8001da177e4SLinus Torvalds c->wbuf_len = PAGE_MOD(to); 8011da177e4SLinus Torvalds memset(c->wbuf,0xff,c->wbuf_pagesize); 8021da177e4SLinus Torvalds } 8031da177e4SLinus Torvalds 804dcb09328SThomas Gleixner /* 805dcb09328SThomas Gleixner * Sanity checks on target address. It's permitted to write 806dcb09328SThomas Gleixner * at PAD(c->wbuf_len+c->wbuf_ofs), and it's permitted to 807dcb09328SThomas Gleixner * write at the beginning of a new erase block. Anything else, 808dcb09328SThomas Gleixner * and you die. New block starts at xxx000c (0-b = block 809dcb09328SThomas Gleixner * header) 8101da177e4SLinus Torvalds */ 8113be36675SAndrew Victor if (SECTOR_ADDR(to) != SECTOR_ADDR(c->wbuf_ofs)) { 8121da177e4SLinus Torvalds /* It's a write to a new block */ 8131da177e4SLinus Torvalds if (c->wbuf_len) { 814dcb09328SThomas Gleixner D1(printk(KERN_DEBUG "jffs2_flash_writev() to 0x%lx " 815dcb09328SThomas Gleixner "causes flush of wbuf at 0x%08x\n", 816dcb09328SThomas Gleixner (unsigned long)to, c->wbuf_ofs)); 8171da177e4SLinus Torvalds ret = __jffs2_flush_wbuf(c, PAD_NOACCOUNT); 818dcb09328SThomas Gleixner if (ret) 819dcb09328SThomas Gleixner goto outerr; 8201da177e4SLinus Torvalds } 8211da177e4SLinus Torvalds /* set pointer to new block */ 8221da177e4SLinus Torvalds c->wbuf_ofs = PAGE_DIV(to); 8231da177e4SLinus Torvalds c->wbuf_len = PAGE_MOD(to); 8241da177e4SLinus Torvalds } 8251da177e4SLinus Torvalds 8261da177e4SLinus Torvalds if (to != PAD(c->wbuf_ofs + c->wbuf_len)) { 8271da177e4SLinus Torvalds /* We're not writing immediately after the writebuffer. Bad. */ 828dcb09328SThomas Gleixner printk(KERN_CRIT "jffs2_flash_writev(): Non-contiguous write " 829dcb09328SThomas Gleixner "to %08lx\n", (unsigned long)to); 8301da177e4SLinus Torvalds if (c->wbuf_len) 8311da177e4SLinus Torvalds printk(KERN_CRIT "wbuf was previously %08x-%08x\n", 8321da177e4SLinus Torvalds c->wbuf_ofs, c->wbuf_ofs+c->wbuf_len); 8331da177e4SLinus Torvalds BUG(); 8341da177e4SLinus Torvalds } 8351da177e4SLinus Torvalds 8361da177e4SLinus Torvalds /* adjust alignment offset */ 8371da177e4SLinus Torvalds if (c->wbuf_len != PAGE_MOD(to)) { 8381da177e4SLinus Torvalds c->wbuf_len = PAGE_MOD(to); 8391da177e4SLinus Torvalds /* take care of alignment to next page */ 840dcb09328SThomas Gleixner if (!c->wbuf_len) { 8411da177e4SLinus Torvalds c->wbuf_len = c->wbuf_pagesize; 8421da177e4SLinus Torvalds ret = __jffs2_flush_wbuf(c, NOPAD); 843dcb09328SThomas Gleixner if (ret) 844dcb09328SThomas Gleixner goto outerr; 8451da177e4SLinus Torvalds } 8461da177e4SLinus Torvalds } 8471da177e4SLinus Torvalds 848dcb09328SThomas Gleixner for (invec = 0; invec < count; invec++) { 849dcb09328SThomas Gleixner int vlen = invecs[invec].iov_len; 850dcb09328SThomas Gleixner uint8_t *v = invecs[invec].iov_base; 8511da177e4SLinus Torvalds 852dcb09328SThomas Gleixner wbuf_retlen = jffs2_fill_wbuf(c, v, vlen); 8531da177e4SLinus Torvalds 854dcb09328SThomas Gleixner if (c->wbuf_len == c->wbuf_pagesize) { 855dcb09328SThomas Gleixner ret = __jffs2_flush_wbuf(c, NOPAD); 856dcb09328SThomas Gleixner if (ret) 857dcb09328SThomas Gleixner goto outerr; 8581da177e4SLinus Torvalds } 859dcb09328SThomas Gleixner vlen -= wbuf_retlen; 860dcb09328SThomas Gleixner outvec_to += wbuf_retlen; 8611da177e4SLinus Torvalds donelen += wbuf_retlen; 862dcb09328SThomas Gleixner v += wbuf_retlen; 8631da177e4SLinus Torvalds 864dcb09328SThomas Gleixner if (vlen >= c->wbuf_pagesize) { 865eda95cbfSArtem Bityutskiy ret = mtd_write(c->mtd, outvec_to, PAGE_DIV(vlen), 866dcb09328SThomas Gleixner &wbuf_retlen, v); 867dcb09328SThomas Gleixner if (ret < 0 || wbuf_retlen != PAGE_DIV(vlen)) 868dcb09328SThomas Gleixner goto outfile; 869dcb09328SThomas Gleixner 870dcb09328SThomas Gleixner vlen -= wbuf_retlen; 871dcb09328SThomas Gleixner outvec_to += wbuf_retlen; 872dcb09328SThomas Gleixner c->wbuf_ofs = outvec_to; 873dcb09328SThomas Gleixner donelen += wbuf_retlen; 874dcb09328SThomas Gleixner v += wbuf_retlen; 8751da177e4SLinus Torvalds } 8761da177e4SLinus Torvalds 877dcb09328SThomas Gleixner wbuf_retlen = jffs2_fill_wbuf(c, v, vlen); 878dcb09328SThomas Gleixner if (c->wbuf_len == c->wbuf_pagesize) { 879dcb09328SThomas Gleixner ret = __jffs2_flush_wbuf(c, NOPAD); 880dcb09328SThomas Gleixner if (ret) 881dcb09328SThomas Gleixner goto outerr; 8821da177e4SLinus Torvalds } 8831da177e4SLinus Torvalds 884dcb09328SThomas Gleixner outvec_to += wbuf_retlen; 885dcb09328SThomas Gleixner donelen += wbuf_retlen; 8861da177e4SLinus Torvalds } 8871da177e4SLinus Torvalds 888dcb09328SThomas Gleixner /* 889dcb09328SThomas Gleixner * If there's a remainder in the wbuf and it's a non-GC write, 890dcb09328SThomas Gleixner * remember that the wbuf affects this ino 891dcb09328SThomas Gleixner */ 8921da177e4SLinus Torvalds *retlen = donelen; 8931da177e4SLinus Torvalds 894e631ddbaSFerenc Havasi if (jffs2_sum_active()) { 895e631ddbaSFerenc Havasi int res = jffs2_sum_add_kvec(c, invecs, count, (uint32_t) to); 896e631ddbaSFerenc Havasi if (res) 897e631ddbaSFerenc Havasi return res; 898e631ddbaSFerenc Havasi } 899e631ddbaSFerenc Havasi 9001da177e4SLinus Torvalds if (c->wbuf_len && ino) 9011da177e4SLinus Torvalds jffs2_wbuf_dirties_inode(c, ino); 9021da177e4SLinus Torvalds 9031da177e4SLinus Torvalds ret = 0; 904dcb09328SThomas Gleixner up_write(&c->wbuf_sem); 905dcb09328SThomas Gleixner return ret; 9061da177e4SLinus Torvalds 907dcb09328SThomas Gleixner outfile: 908dcb09328SThomas Gleixner /* 909dcb09328SThomas Gleixner * At this point we have no problem, c->wbuf is empty. However 910dcb09328SThomas Gleixner * refile nextblock to avoid writing again to same address. 911dcb09328SThomas Gleixner */ 912dcb09328SThomas Gleixner 913dcb09328SThomas Gleixner spin_lock(&c->erase_completion_lock); 914dcb09328SThomas Gleixner 915dcb09328SThomas Gleixner jeb = &c->blocks[outvec_to / c->sector_size]; 916dcb09328SThomas Gleixner jffs2_block_refile(c, jeb, REFILE_ANYWAY); 917dcb09328SThomas Gleixner 918dcb09328SThomas Gleixner spin_unlock(&c->erase_completion_lock); 919dcb09328SThomas Gleixner 920dcb09328SThomas Gleixner outerr: 921dcb09328SThomas Gleixner *retlen = 0; 9221da177e4SLinus Torvalds up_write(&c->wbuf_sem); 9231da177e4SLinus Torvalds return ret; 9241da177e4SLinus Torvalds } 9251da177e4SLinus Torvalds 9261da177e4SLinus Torvalds /* 9271da177e4SLinus Torvalds * This is the entry for flash write. 9281da177e4SLinus Torvalds * Check, if we work on NAND FLASH, if so build an kvec and write it via vritev 9291da177e4SLinus Torvalds */ 9309bfeb691SDavid Woodhouse int jffs2_flash_write(struct jffs2_sb_info *c, loff_t ofs, size_t len, 9319bfeb691SDavid Woodhouse size_t *retlen, const u_char *buf) 9321da177e4SLinus Torvalds { 9331da177e4SLinus Torvalds struct kvec vecs[1]; 9341da177e4SLinus Torvalds 9353be36675SAndrew Victor if (!jffs2_is_writebuffered(c)) 936e631ddbaSFerenc Havasi return jffs2_flash_direct_write(c, ofs, len, retlen, buf); 9371da177e4SLinus Torvalds 9381da177e4SLinus Torvalds vecs[0].iov_base = (unsigned char *) buf; 9391da177e4SLinus Torvalds vecs[0].iov_len = len; 9401da177e4SLinus Torvalds return jffs2_flash_writev(c, vecs, 1, ofs, retlen, 0); 9411da177e4SLinus Torvalds } 9421da177e4SLinus Torvalds 9431da177e4SLinus Torvalds /* 9441da177e4SLinus Torvalds Handle readback from writebuffer and ECC failure return 9451da177e4SLinus Torvalds */ 9461da177e4SLinus Torvalds int jffs2_flash_read(struct jffs2_sb_info *c, loff_t ofs, size_t len, size_t *retlen, u_char *buf) 9471da177e4SLinus Torvalds { 9481da177e4SLinus Torvalds loff_t orbf = 0, owbf = 0, lwbf = 0; 9491da177e4SLinus Torvalds int ret; 9501da177e4SLinus Torvalds 9513be36675SAndrew Victor if (!jffs2_is_writebuffered(c)) 952329ad399SArtem Bityutskiy return mtd_read(c->mtd, ofs, len, retlen, buf); 9531da177e4SLinus Torvalds 9543be36675SAndrew Victor /* Read flash */ 955894214d1SArtem B. Bityuckiy down_read(&c->wbuf_sem); 956329ad399SArtem Bityutskiy ret = mtd_read(c->mtd, ofs, len, retlen, buf); 9571da177e4SLinus Torvalds 9589a1fcdfdSThomas Gleixner if ( (ret == -EBADMSG || ret == -EUCLEAN) && (*retlen == len) ) { 9599a1fcdfdSThomas Gleixner if (ret == -EBADMSG) 9609a1fcdfdSThomas Gleixner printk(KERN_WARNING "mtd->read(0x%zx bytes from 0x%llx)" 9619a1fcdfdSThomas Gleixner " returned ECC error\n", len, ofs); 9621da177e4SLinus Torvalds /* 9639a1fcdfdSThomas Gleixner * We have the raw data without ECC correction in the buffer, 9649a1fcdfdSThomas Gleixner * maybe we are lucky and all data or parts are correct. We 9659a1fcdfdSThomas Gleixner * check the node. If data are corrupted node check will sort 9669a1fcdfdSThomas Gleixner * it out. We keep this block, it will fail on write or erase 9679a1fcdfdSThomas Gleixner * and the we mark it bad. Or should we do that now? But we 9689a1fcdfdSThomas Gleixner * should give him a chance. Maybe we had a system crash or 9699a1fcdfdSThomas Gleixner * power loss before the ecc write or a erase was completed. 9701da177e4SLinus Torvalds * So we return success. :) 9711da177e4SLinus Torvalds */ 9721da177e4SLinus Torvalds ret = 0; 9731da177e4SLinus Torvalds } 9741da177e4SLinus Torvalds 9751da177e4SLinus Torvalds /* if no writebuffer available or write buffer empty, return */ 9761da177e4SLinus Torvalds if (!c->wbuf_pagesize || !c->wbuf_len) 977894214d1SArtem B. Bityuckiy goto exit; 9781da177e4SLinus Torvalds 9791da177e4SLinus Torvalds /* if we read in a different block, return */ 9803be36675SAndrew Victor if (SECTOR_ADDR(ofs) != SECTOR_ADDR(c->wbuf_ofs)) 981894214d1SArtem B. Bityuckiy goto exit; 9821da177e4SLinus Torvalds 9831da177e4SLinus Torvalds if (ofs >= c->wbuf_ofs) { 9841da177e4SLinus Torvalds owbf = (ofs - c->wbuf_ofs); /* offset in write buffer */ 9851da177e4SLinus Torvalds if (owbf > c->wbuf_len) /* is read beyond write buffer ? */ 9861da177e4SLinus Torvalds goto exit; 9871da177e4SLinus Torvalds lwbf = c->wbuf_len - owbf; /* number of bytes to copy */ 9881da177e4SLinus Torvalds if (lwbf > len) 9891da177e4SLinus Torvalds lwbf = len; 9901da177e4SLinus Torvalds } else { 9911da177e4SLinus Torvalds orbf = (c->wbuf_ofs - ofs); /* offset in read buffer */ 9921da177e4SLinus Torvalds if (orbf > len) /* is write beyond write buffer ? */ 9931da177e4SLinus Torvalds goto exit; 9941da177e4SLinus Torvalds lwbf = len - orbf; /* number of bytes to copy */ 9951da177e4SLinus Torvalds if (lwbf > c->wbuf_len) 9961da177e4SLinus Torvalds lwbf = c->wbuf_len; 9971da177e4SLinus Torvalds } 9981da177e4SLinus Torvalds if (lwbf > 0) 9991da177e4SLinus Torvalds memcpy(buf+orbf,c->wbuf+owbf,lwbf); 10001da177e4SLinus Torvalds 10011da177e4SLinus Torvalds exit: 10021da177e4SLinus Torvalds up_read(&c->wbuf_sem); 10031da177e4SLinus Torvalds return ret; 10041da177e4SLinus Torvalds } 10051da177e4SLinus Torvalds 10068593fbc6SThomas Gleixner #define NR_OOB_SCAN_PAGES 4 10078593fbc6SThomas Gleixner 100809b3fba5SDavid Woodhouse /* For historical reasons we use only 8 bytes for OOB clean marker */ 100909b3fba5SDavid Woodhouse #define OOB_CM_SIZE 8 1010a7a6ace1SArtem Bityutskiy 1011a7a6ace1SArtem Bityutskiy static const struct jffs2_unknown_node oob_cleanmarker = 1012a7a6ace1SArtem Bityutskiy { 1013566865a2SDavid Woodhouse .magic = constant_cpu_to_je16(JFFS2_MAGIC_BITMASK), 1014566865a2SDavid Woodhouse .nodetype = constant_cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER), 1015566865a2SDavid Woodhouse .totlen = constant_cpu_to_je32(8) 1016a7a6ace1SArtem Bityutskiy }; 1017a7a6ace1SArtem Bityutskiy 10181da177e4SLinus Torvalds /* 1019a7a6ace1SArtem Bityutskiy * Check, if the out of band area is empty. This function knows about the clean 1020a7a6ace1SArtem Bityutskiy * marker and if it is present in OOB, treats the OOB as empty anyway. 10211da177e4SLinus Torvalds */ 10228593fbc6SThomas Gleixner int jffs2_check_oob_empty(struct jffs2_sb_info *c, 10238593fbc6SThomas Gleixner struct jffs2_eraseblock *jeb, int mode) 10241da177e4SLinus Torvalds { 1025a7a6ace1SArtem Bityutskiy int i, ret; 1026a7a6ace1SArtem Bityutskiy int cmlen = min_t(int, c->oobavail, OOB_CM_SIZE); 10278593fbc6SThomas Gleixner struct mtd_oob_ops ops; 10281da177e4SLinus Torvalds 10290612b9ddSBrian Norris ops.mode = MTD_OPS_AUTO_OOB; 1030a7a6ace1SArtem Bityutskiy ops.ooblen = NR_OOB_SCAN_PAGES * c->oobavail; 10318593fbc6SThomas Gleixner ops.oobbuf = c->oobbuf; 1032a7a6ace1SArtem Bityutskiy ops.len = ops.ooboffs = ops.retlen = ops.oobretlen = 0; 10338593fbc6SThomas Gleixner ops.datbuf = NULL; 10348593fbc6SThomas Gleixner 1035fd2819bbSArtem Bityutskiy ret = mtd_read_oob(c->mtd, jeb->offset, &ops); 1036a7a6ace1SArtem Bityutskiy if (ret || ops.oobretlen != ops.ooblen) { 10377be26bfbSAndrew Morton printk(KERN_ERR "cannot read OOB for EB at %08x, requested %zd" 10387be26bfbSAndrew Morton " bytes, read %zd bytes, error %d\n", 10397be26bfbSAndrew Morton jeb->offset, ops.ooblen, ops.oobretlen, ret); 1040a7a6ace1SArtem Bityutskiy if (!ret) 1041a7a6ace1SArtem Bityutskiy ret = -EIO; 10428593fbc6SThomas Gleixner return ret; 10431da177e4SLinus Torvalds } 10441da177e4SLinus Torvalds 1045a7a6ace1SArtem Bityutskiy for(i = 0; i < ops.ooblen; i++) { 1046a7a6ace1SArtem Bityutskiy if (mode && i < cmlen) 1047a7a6ace1SArtem Bityutskiy /* Yeah, we know about the cleanmarker */ 10481da177e4SLinus Torvalds continue; 10491da177e4SLinus Torvalds 10508593fbc6SThomas Gleixner if (ops.oobbuf[i] != 0xFF) { 10518593fbc6SThomas Gleixner D2(printk(KERN_DEBUG "Found %02x at %x in OOB for " 10528593fbc6SThomas Gleixner "%08x\n", ops.oobbuf[i], i, jeb->offset)); 10538593fbc6SThomas Gleixner return 1; 10541da177e4SLinus Torvalds } 10551da177e4SLinus Torvalds } 10561da177e4SLinus Torvalds 10578593fbc6SThomas Gleixner return 0; 10581da177e4SLinus Torvalds } 10591da177e4SLinus Torvalds 10601da177e4SLinus Torvalds /* 1061a7a6ace1SArtem Bityutskiy * Check for a valid cleanmarker. 1062a7a6ace1SArtem Bityutskiy * Returns: 0 if a valid cleanmarker was found 1063a7a6ace1SArtem Bityutskiy * 1 if no cleanmarker was found 1064a7a6ace1SArtem Bityutskiy * negative error code if an error occurred 10651da177e4SLinus Torvalds */ 10668593fbc6SThomas Gleixner int jffs2_check_nand_cleanmarker(struct jffs2_sb_info *c, 10678593fbc6SThomas Gleixner struct jffs2_eraseblock *jeb) 10681da177e4SLinus Torvalds { 10698593fbc6SThomas Gleixner struct mtd_oob_ops ops; 1070a7a6ace1SArtem Bityutskiy int ret, cmlen = min_t(int, c->oobavail, OOB_CM_SIZE); 10711da177e4SLinus Torvalds 10720612b9ddSBrian Norris ops.mode = MTD_OPS_AUTO_OOB; 1073a7a6ace1SArtem Bityutskiy ops.ooblen = cmlen; 10748593fbc6SThomas Gleixner ops.oobbuf = c->oobbuf; 1075a7a6ace1SArtem Bityutskiy ops.len = ops.ooboffs = ops.retlen = ops.oobretlen = 0; 10768593fbc6SThomas Gleixner ops.datbuf = NULL; 10778593fbc6SThomas Gleixner 1078fd2819bbSArtem Bityutskiy ret = mtd_read_oob(c->mtd, jeb->offset, &ops); 1079a7a6ace1SArtem Bityutskiy if (ret || ops.oobretlen != ops.ooblen) { 10807be26bfbSAndrew Morton printk(KERN_ERR "cannot read OOB for EB at %08x, requested %zd" 10817be26bfbSAndrew Morton " bytes, read %zd bytes, error %d\n", 10827be26bfbSAndrew Morton jeb->offset, ops.ooblen, ops.oobretlen, ret); 1083a7a6ace1SArtem Bityutskiy if (!ret) 1084a7a6ace1SArtem Bityutskiy ret = -EIO; 10851da177e4SLinus Torvalds return ret; 10861da177e4SLinus Torvalds } 10878593fbc6SThomas Gleixner 1088a7a6ace1SArtem Bityutskiy return !!memcmp(&oob_cleanmarker, c->oobbuf, cmlen); 10891da177e4SLinus Torvalds } 10901da177e4SLinus Torvalds 10918593fbc6SThomas Gleixner int jffs2_write_nand_cleanmarker(struct jffs2_sb_info *c, 10928593fbc6SThomas Gleixner struct jffs2_eraseblock *jeb) 10931da177e4SLinus Torvalds { 10941da177e4SLinus Torvalds int ret; 10958593fbc6SThomas Gleixner struct mtd_oob_ops ops; 1096a7a6ace1SArtem Bityutskiy int cmlen = min_t(int, c->oobavail, OOB_CM_SIZE); 10971da177e4SLinus Torvalds 10980612b9ddSBrian Norris ops.mode = MTD_OPS_AUTO_OOB; 1099a7a6ace1SArtem Bityutskiy ops.ooblen = cmlen; 1100a7a6ace1SArtem Bityutskiy ops.oobbuf = (uint8_t *)&oob_cleanmarker; 1101a7a6ace1SArtem Bityutskiy ops.len = ops.ooboffs = ops.retlen = ops.oobretlen = 0; 11028593fbc6SThomas Gleixner ops.datbuf = NULL; 11038593fbc6SThomas Gleixner 1104a2cc5ba0SArtem Bityutskiy ret = mtd_write_oob(c->mtd, jeb->offset, &ops); 1105a7a6ace1SArtem Bityutskiy if (ret || ops.oobretlen != ops.ooblen) { 11067be26bfbSAndrew Morton printk(KERN_ERR "cannot write OOB for EB at %08x, requested %zd" 11077be26bfbSAndrew Morton " bytes, read %zd bytes, error %d\n", 11087be26bfbSAndrew Morton jeb->offset, ops.ooblen, ops.oobretlen, ret); 1109a7a6ace1SArtem Bityutskiy if (!ret) 1110a7a6ace1SArtem Bityutskiy ret = -EIO; 11111da177e4SLinus Torvalds return ret; 11121da177e4SLinus Torvalds } 1113a7a6ace1SArtem Bityutskiy 11141da177e4SLinus Torvalds return 0; 11151da177e4SLinus Torvalds } 11161da177e4SLinus Torvalds 11171da177e4SLinus Torvalds /* 11181da177e4SLinus Torvalds * On NAND we try to mark this block bad. If the block was erased more 111925985edcSLucas De Marchi * than MAX_ERASE_FAILURES we mark it finally bad. 11201da177e4SLinus Torvalds * Don't care about failures. This block remains on the erase-pending 11211da177e4SLinus Torvalds * or badblock list as long as nobody manipulates the flash with 11221da177e4SLinus Torvalds * a bootloader or something like that. 11231da177e4SLinus Torvalds */ 11241da177e4SLinus Torvalds 11251da177e4SLinus Torvalds int jffs2_write_nand_badblock(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset) 11261da177e4SLinus Torvalds { 11271da177e4SLinus Torvalds int ret; 11281da177e4SLinus Torvalds 11291da177e4SLinus Torvalds /* if the count is < max, we try to write the counter to the 2nd page oob area */ 11301da177e4SLinus Torvalds if( ++jeb->bad_count < MAX_ERASE_FAILURES) 11311da177e4SLinus Torvalds return 0; 11321da177e4SLinus Torvalds 11331da177e4SLinus Torvalds if (!c->mtd->block_markbad) 11341da177e4SLinus Torvalds return 1; // What else can we do? 11351da177e4SLinus Torvalds 11360feba829SArtem Bityutskiy printk(KERN_WARNING "JFFS2: marking eraseblock at %08x\n as bad", bad_offset); 11375942ddbcSArtem Bityutskiy ret = mtd_block_markbad(c->mtd, bad_offset); 11381da177e4SLinus Torvalds 11391da177e4SLinus Torvalds if (ret) { 11401da177e4SLinus Torvalds D1(printk(KERN_WARNING "jffs2_write_nand_badblock(): Write failed for block at %08x: error %d\n", jeb->offset, ret)); 11411da177e4SLinus Torvalds return ret; 11421da177e4SLinus Torvalds } 11431da177e4SLinus Torvalds return 1; 11441da177e4SLinus Torvalds } 11451da177e4SLinus Torvalds 1146a7a6ace1SArtem Bityutskiy int jffs2_nand_flash_setup(struct jffs2_sb_info *c) 11471da177e4SLinus Torvalds { 11485bd34c09SThomas Gleixner struct nand_ecclayout *oinfo = c->mtd->ecclayout; 11491da177e4SLinus Torvalds 11501da177e4SLinus Torvalds if (!c->mtd->oobsize) 11511da177e4SLinus Torvalds return 0; 11521da177e4SLinus Torvalds 11531da177e4SLinus Torvalds /* Cleanmarker is out-of-band, so inline size zero */ 11541da177e4SLinus Torvalds c->cleanmarker_size = 0; 11551da177e4SLinus Torvalds 1156a7a6ace1SArtem Bityutskiy if (!oinfo || oinfo->oobavail == 0) { 1157a7a6ace1SArtem Bityutskiy printk(KERN_ERR "inconsistent device description\n"); 11581da177e4SLinus Torvalds return -EINVAL; 11591da177e4SLinus Torvalds } 11605bd34c09SThomas Gleixner 1161a7a6ace1SArtem Bityutskiy D1(printk(KERN_DEBUG "JFFS2 using OOB on NAND\n")); 11625bd34c09SThomas Gleixner 1163a7a6ace1SArtem Bityutskiy c->oobavail = oinfo->oobavail; 11641da177e4SLinus Torvalds 11651da177e4SLinus Torvalds /* Initialise write buffer */ 11661da177e4SLinus Torvalds init_rwsem(&c->wbuf_sem); 116728318776SJoern Engel c->wbuf_pagesize = c->mtd->writesize; 11681da177e4SLinus Torvalds c->wbuf_ofs = 0xFFFFFFFF; 11691da177e4SLinus Torvalds 11701da177e4SLinus Torvalds c->wbuf = kmalloc(c->wbuf_pagesize, GFP_KERNEL); 11711da177e4SLinus Torvalds if (!c->wbuf) 11721da177e4SLinus Torvalds return -ENOMEM; 11731da177e4SLinus Torvalds 1174a7a6ace1SArtem Bityutskiy c->oobbuf = kmalloc(NR_OOB_SCAN_PAGES * c->oobavail, GFP_KERNEL); 1175a7a6ace1SArtem Bityutskiy if (!c->oobbuf) { 11761da177e4SLinus Torvalds kfree(c->wbuf); 11771da177e4SLinus Torvalds return -ENOMEM; 11781da177e4SLinus Torvalds } 1179a7a6ace1SArtem Bityutskiy 1180a6bc432eSDavid Woodhouse #ifdef CONFIG_JFFS2_FS_WBUF_VERIFY 1181a6bc432eSDavid Woodhouse c->wbuf_verify = kmalloc(c->wbuf_pagesize, GFP_KERNEL); 1182a6bc432eSDavid Woodhouse if (!c->wbuf_verify) { 1183a6bc432eSDavid Woodhouse kfree(c->oobbuf); 1184a6bc432eSDavid Woodhouse kfree(c->wbuf); 1185a6bc432eSDavid Woodhouse return -ENOMEM; 1186a6bc432eSDavid Woodhouse } 1187a6bc432eSDavid Woodhouse #endif 1188a7a6ace1SArtem Bityutskiy return 0; 11891da177e4SLinus Torvalds } 11901da177e4SLinus Torvalds 11911da177e4SLinus Torvalds void jffs2_nand_flash_cleanup(struct jffs2_sb_info *c) 11921da177e4SLinus Torvalds { 1193a6bc432eSDavid Woodhouse #ifdef CONFIG_JFFS2_FS_WBUF_VERIFY 1194a6bc432eSDavid Woodhouse kfree(c->wbuf_verify); 1195a6bc432eSDavid Woodhouse #endif 11961da177e4SLinus Torvalds kfree(c->wbuf); 11978593fbc6SThomas Gleixner kfree(c->oobbuf); 11981da177e4SLinus Torvalds } 11991da177e4SLinus Torvalds 12008f15fd55SAndrew Victor int jffs2_dataflash_setup(struct jffs2_sb_info *c) { 12018f15fd55SAndrew Victor c->cleanmarker_size = 0; /* No cleanmarkers needed */ 12028f15fd55SAndrew Victor 12038f15fd55SAndrew Victor /* Initialize write buffer */ 12048f15fd55SAndrew Victor init_rwsem(&c->wbuf_sem); 12058f15fd55SAndrew Victor 1206daba5cc4SArtem B. Bityutskiy 1207daba5cc4SArtem B. Bityutskiy c->wbuf_pagesize = c->mtd->erasesize; 1208daba5cc4SArtem B. Bityutskiy 1209daba5cc4SArtem B. Bityutskiy /* Find a suitable c->sector_size 1210daba5cc4SArtem B. Bityutskiy * - Not too much sectors 1211daba5cc4SArtem B. Bityutskiy * - Sectors have to be at least 4 K + some bytes 1212daba5cc4SArtem B. Bityutskiy * - All known dataflashes have erase sizes of 528 or 1056 1213daba5cc4SArtem B. Bityutskiy * - we take at least 8 eraseblocks and want to have at least 8K size 1214daba5cc4SArtem B. Bityutskiy * - The concatenation should be a power of 2 1215daba5cc4SArtem B. Bityutskiy */ 1216daba5cc4SArtem B. Bityutskiy 1217daba5cc4SArtem B. Bityutskiy c->sector_size = 8 * c->mtd->erasesize; 1218daba5cc4SArtem B. Bityutskiy 1219daba5cc4SArtem B. Bityutskiy while (c->sector_size < 8192) { 1220daba5cc4SArtem B. Bityutskiy c->sector_size *= 2; 1221daba5cc4SArtem B. Bityutskiy } 1222daba5cc4SArtem B. Bityutskiy 1223daba5cc4SArtem B. Bityutskiy /* It may be necessary to adjust the flash size */ 1224daba5cc4SArtem B. Bityutskiy c->flash_size = c->mtd->size; 1225daba5cc4SArtem B. Bityutskiy 1226daba5cc4SArtem B. Bityutskiy if ((c->flash_size % c->sector_size) != 0) { 1227daba5cc4SArtem B. Bityutskiy c->flash_size = (c->flash_size / c->sector_size) * c->sector_size; 1228daba5cc4SArtem B. Bityutskiy printk(KERN_WARNING "JFFS2 flash size adjusted to %dKiB\n", c->flash_size); 1229daba5cc4SArtem B. Bityutskiy }; 1230daba5cc4SArtem B. Bityutskiy 1231daba5cc4SArtem B. Bityutskiy c->wbuf_ofs = 0xFFFFFFFF; 12328f15fd55SAndrew Victor c->wbuf = kmalloc(c->wbuf_pagesize, GFP_KERNEL); 12338f15fd55SAndrew Victor if (!c->wbuf) 12348f15fd55SAndrew Victor return -ENOMEM; 12358f15fd55SAndrew Victor 1236cca15841Smichael #ifdef CONFIG_JFFS2_FS_WBUF_VERIFY 1237cca15841Smichael c->wbuf_verify = kmalloc(c->wbuf_pagesize, GFP_KERNEL); 1238cca15841Smichael if (!c->wbuf_verify) { 1239cca15841Smichael kfree(c->oobbuf); 1240cca15841Smichael kfree(c->wbuf); 1241cca15841Smichael return -ENOMEM; 1242cca15841Smichael } 1243cca15841Smichael #endif 1244cca15841Smichael 1245daba5cc4SArtem B. Bityutskiy printk(KERN_INFO "JFFS2 write-buffering enabled buffer (%d) erasesize (%d)\n", c->wbuf_pagesize, c->sector_size); 12468f15fd55SAndrew Victor 12478f15fd55SAndrew Victor return 0; 12488f15fd55SAndrew Victor } 12498f15fd55SAndrew Victor 12508f15fd55SAndrew Victor void jffs2_dataflash_cleanup(struct jffs2_sb_info *c) { 1251cca15841Smichael #ifdef CONFIG_JFFS2_FS_WBUF_VERIFY 1252cca15841Smichael kfree(c->wbuf_verify); 1253cca15841Smichael #endif 12548f15fd55SAndrew Victor kfree(c->wbuf); 12558f15fd55SAndrew Victor } 12568f15fd55SAndrew Victor 125759da721aSNicolas Pitre int jffs2_nor_wbuf_flash_setup(struct jffs2_sb_info *c) { 1258c8b229deSJoern Engel /* Cleanmarker currently occupies whole programming regions, 1259c8b229deSJoern Engel * either one or 2 for 8Byte STMicro flashes. */ 1260c8b229deSJoern Engel c->cleanmarker_size = max(16u, c->mtd->writesize); 126159da721aSNicolas Pitre 126259da721aSNicolas Pitre /* Initialize write buffer */ 126359da721aSNicolas Pitre init_rwsem(&c->wbuf_sem); 126428318776SJoern Engel c->wbuf_pagesize = c->mtd->writesize; 126559da721aSNicolas Pitre c->wbuf_ofs = 0xFFFFFFFF; 126659da721aSNicolas Pitre 126759da721aSNicolas Pitre c->wbuf = kmalloc(c->wbuf_pagesize, GFP_KERNEL); 126859da721aSNicolas Pitre if (!c->wbuf) 126959da721aSNicolas Pitre return -ENOMEM; 127059da721aSNicolas Pitre 1271bc8cec0dSMassimo Cirillo #ifdef CONFIG_JFFS2_FS_WBUF_VERIFY 1272bc8cec0dSMassimo Cirillo c->wbuf_verify = kmalloc(c->wbuf_pagesize, GFP_KERNEL); 1273bc8cec0dSMassimo Cirillo if (!c->wbuf_verify) { 1274bc8cec0dSMassimo Cirillo kfree(c->wbuf); 1275bc8cec0dSMassimo Cirillo return -ENOMEM; 1276bc8cec0dSMassimo Cirillo } 1277bc8cec0dSMassimo Cirillo #endif 127859da721aSNicolas Pitre return 0; 127959da721aSNicolas Pitre } 128059da721aSNicolas Pitre 128159da721aSNicolas Pitre void jffs2_nor_wbuf_flash_cleanup(struct jffs2_sb_info *c) { 1282bc8cec0dSMassimo Cirillo #ifdef CONFIG_JFFS2_FS_WBUF_VERIFY 1283bc8cec0dSMassimo Cirillo kfree(c->wbuf_verify); 1284bc8cec0dSMassimo Cirillo #endif 128559da721aSNicolas Pitre kfree(c->wbuf); 128659da721aSNicolas Pitre } 12870029da3bSArtem Bityutskiy 12880029da3bSArtem Bityutskiy int jffs2_ubivol_setup(struct jffs2_sb_info *c) { 12890029da3bSArtem Bityutskiy c->cleanmarker_size = 0; 12900029da3bSArtem Bityutskiy 12910029da3bSArtem Bityutskiy if (c->mtd->writesize == 1) 12920029da3bSArtem Bityutskiy /* We do not need write-buffer */ 12930029da3bSArtem Bityutskiy return 0; 12940029da3bSArtem Bityutskiy 12950029da3bSArtem Bityutskiy init_rwsem(&c->wbuf_sem); 12960029da3bSArtem Bityutskiy 12970029da3bSArtem Bityutskiy c->wbuf_pagesize = c->mtd->writesize; 12980029da3bSArtem Bityutskiy c->wbuf_ofs = 0xFFFFFFFF; 12990029da3bSArtem Bityutskiy c->wbuf = kmalloc(c->wbuf_pagesize, GFP_KERNEL); 13000029da3bSArtem Bityutskiy if (!c->wbuf) 13010029da3bSArtem Bityutskiy return -ENOMEM; 13020029da3bSArtem Bityutskiy 13030029da3bSArtem Bityutskiy printk(KERN_INFO "JFFS2 write-buffering enabled buffer (%d) erasesize (%d)\n", c->wbuf_pagesize, c->sector_size); 13040029da3bSArtem Bityutskiy 13050029da3bSArtem Bityutskiy return 0; 13060029da3bSArtem Bityutskiy } 13070029da3bSArtem Bityutskiy 13080029da3bSArtem Bityutskiy void jffs2_ubivol_cleanup(struct jffs2_sb_info *c) { 13090029da3bSArtem Bityutskiy kfree(c->wbuf); 13100029da3bSArtem Bityutskiy } 1311