11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * JFFS2 -- Journalling Flash File System, Version 2. 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 2001-2003 Red Hat, Inc. 51da177e4SLinus Torvalds * Copyright (C) 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 * 12daba5cc4SArtem B. Bityutskiy * $Id: wbuf.c,v 1.100 2005/09/30 13:59:13 dedekind Exp $ 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds */ 151da177e4SLinus Torvalds 161da177e4SLinus Torvalds #include <linux/kernel.h> 171da177e4SLinus Torvalds #include <linux/slab.h> 181da177e4SLinus Torvalds #include <linux/mtd/mtd.h> 191da177e4SLinus Torvalds #include <linux/crc32.h> 201da177e4SLinus Torvalds #include <linux/mtd/nand.h> 214e57b681STim Schmielau #include <linux/jiffies.h> 224e57b681STim Schmielau 231da177e4SLinus Torvalds #include "nodelist.h" 241da177e4SLinus Torvalds 251da177e4SLinus Torvalds /* For testing write failures */ 261da177e4SLinus Torvalds #undef BREAKME 271da177e4SLinus Torvalds #undef BREAKMEHEADER 281da177e4SLinus Torvalds 291da177e4SLinus Torvalds #ifdef BREAKME 301da177e4SLinus Torvalds static unsigned char *brokenbuf; 311da177e4SLinus Torvalds #endif 321da177e4SLinus Torvalds 33daba5cc4SArtem B. Bityutskiy #define PAGE_DIV(x) ( ((unsigned long)(x) / (unsigned long)(c->wbuf_pagesize)) * (unsigned long)(c->wbuf_pagesize) ) 34daba5cc4SArtem B. Bityutskiy #define PAGE_MOD(x) ( (unsigned long)(x) % (unsigned long)(c->wbuf_pagesize) ) 35daba5cc4SArtem B. Bityutskiy 361da177e4SLinus Torvalds /* max. erase failures before we mark a block bad */ 371da177e4SLinus Torvalds #define MAX_ERASE_FAILURES 2 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds struct jffs2_inodirty { 401da177e4SLinus Torvalds uint32_t ino; 411da177e4SLinus Torvalds struct jffs2_inodirty *next; 421da177e4SLinus Torvalds }; 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds static struct jffs2_inodirty inodirty_nomem; 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds static int jffs2_wbuf_pending_for_ino(struct jffs2_sb_info *c, uint32_t ino) 471da177e4SLinus Torvalds { 481da177e4SLinus Torvalds struct jffs2_inodirty *this = c->wbuf_inodes; 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds /* If a malloc failed, consider _everything_ dirty */ 511da177e4SLinus Torvalds if (this == &inodirty_nomem) 521da177e4SLinus Torvalds return 1; 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds /* If ino == 0, _any_ non-GC writes mean 'yes' */ 551da177e4SLinus Torvalds if (this && !ino) 561da177e4SLinus Torvalds return 1; 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds /* Look to see if the inode in question is pending in the wbuf */ 591da177e4SLinus Torvalds while (this) { 601da177e4SLinus Torvalds if (this->ino == ino) 611da177e4SLinus Torvalds return 1; 621da177e4SLinus Torvalds this = this->next; 631da177e4SLinus Torvalds } 641da177e4SLinus Torvalds return 0; 651da177e4SLinus Torvalds } 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds static void jffs2_clear_wbuf_ino_list(struct jffs2_sb_info *c) 681da177e4SLinus Torvalds { 691da177e4SLinus Torvalds struct jffs2_inodirty *this; 701da177e4SLinus Torvalds 711da177e4SLinus Torvalds this = c->wbuf_inodes; 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds if (this != &inodirty_nomem) { 741da177e4SLinus Torvalds while (this) { 751da177e4SLinus Torvalds struct jffs2_inodirty *next = this->next; 761da177e4SLinus Torvalds kfree(this); 771da177e4SLinus Torvalds this = next; 781da177e4SLinus Torvalds } 791da177e4SLinus Torvalds } 801da177e4SLinus Torvalds c->wbuf_inodes = NULL; 811da177e4SLinus Torvalds } 821da177e4SLinus Torvalds 831da177e4SLinus Torvalds static void jffs2_wbuf_dirties_inode(struct jffs2_sb_info *c, uint32_t ino) 841da177e4SLinus Torvalds { 851da177e4SLinus Torvalds struct jffs2_inodirty *new; 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds /* Mark the superblock dirty so that kupdated will flush... */ 884d952709SArtem B. Bityuckiy jffs2_erase_pending_trigger(c); 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds if (jffs2_wbuf_pending_for_ino(c, ino)) 911da177e4SLinus Torvalds return; 921da177e4SLinus Torvalds 931da177e4SLinus Torvalds new = kmalloc(sizeof(*new), GFP_KERNEL); 941da177e4SLinus Torvalds if (!new) { 951da177e4SLinus Torvalds D1(printk(KERN_DEBUG "No memory to allocate inodirty. Fallback to all considered dirty\n")); 961da177e4SLinus Torvalds jffs2_clear_wbuf_ino_list(c); 971da177e4SLinus Torvalds c->wbuf_inodes = &inodirty_nomem; 981da177e4SLinus Torvalds return; 991da177e4SLinus Torvalds } 1001da177e4SLinus Torvalds new->ino = ino; 1011da177e4SLinus Torvalds new->next = c->wbuf_inodes; 1021da177e4SLinus Torvalds c->wbuf_inodes = new; 1031da177e4SLinus Torvalds return; 1041da177e4SLinus Torvalds } 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds static inline void jffs2_refile_wbuf_blocks(struct jffs2_sb_info *c) 1071da177e4SLinus Torvalds { 1081da177e4SLinus Torvalds struct list_head *this, *next; 1091da177e4SLinus Torvalds static int n; 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds if (list_empty(&c->erasable_pending_wbuf_list)) 1121da177e4SLinus Torvalds return; 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds list_for_each_safe(this, next, &c->erasable_pending_wbuf_list) { 1151da177e4SLinus Torvalds struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list); 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds D1(printk(KERN_DEBUG "Removing eraseblock at 0x%08x from erasable_pending_wbuf_list...\n", jeb->offset)); 1181da177e4SLinus Torvalds list_del(this); 1191da177e4SLinus Torvalds if ((jiffies + (n++)) & 127) { 1201da177e4SLinus Torvalds /* Most of the time, we just erase it immediately. Otherwise we 1211da177e4SLinus Torvalds spend ages scanning it on mount, etc. */ 1221da177e4SLinus Torvalds D1(printk(KERN_DEBUG "...and adding to erase_pending_list\n")); 1231da177e4SLinus Torvalds list_add_tail(&jeb->list, &c->erase_pending_list); 1241da177e4SLinus Torvalds c->nr_erasing_blocks++; 1251da177e4SLinus Torvalds jffs2_erase_pending_trigger(c); 1261da177e4SLinus Torvalds } else { 1271da177e4SLinus Torvalds /* Sometimes, however, we leave it elsewhere so it doesn't get 1281da177e4SLinus Torvalds immediately reused, and we spread the load a bit. */ 1291da177e4SLinus Torvalds D1(printk(KERN_DEBUG "...and adding to erasable_list\n")); 1301da177e4SLinus Torvalds list_add_tail(&jeb->list, &c->erasable_list); 1311da177e4SLinus Torvalds } 1321da177e4SLinus Torvalds } 1331da177e4SLinus Torvalds } 1341da177e4SLinus Torvalds 1357f716cf3SEstelle Hammache #define REFILE_NOTEMPTY 0 1367f716cf3SEstelle Hammache #define REFILE_ANYWAY 1 1377f716cf3SEstelle Hammache 1387f716cf3SEstelle Hammache static void jffs2_block_refile(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, int allow_empty) 1391da177e4SLinus Torvalds { 1401da177e4SLinus Torvalds D1(printk("About to refile bad block at %08x\n", jeb->offset)); 1411da177e4SLinus Torvalds 1421da177e4SLinus Torvalds /* File the existing block on the bad_used_list.... */ 1431da177e4SLinus Torvalds if (c->nextblock == jeb) 1441da177e4SLinus Torvalds c->nextblock = NULL; 1451da177e4SLinus Torvalds else /* Not sure this should ever happen... need more coffee */ 1461da177e4SLinus Torvalds list_del(&jeb->list); 1471da177e4SLinus Torvalds if (jeb->first_node) { 1481da177e4SLinus Torvalds D1(printk("Refiling block at %08x to bad_used_list\n", jeb->offset)); 1491da177e4SLinus Torvalds list_add(&jeb->list, &c->bad_used_list); 1501da177e4SLinus Torvalds } else { 1519b88f473SEstelle Hammache BUG_ON(allow_empty == REFILE_NOTEMPTY); 1521da177e4SLinus Torvalds /* It has to have had some nodes or we couldn't be here */ 1531da177e4SLinus Torvalds D1(printk("Refiling block at %08x to erase_pending_list\n", jeb->offset)); 1541da177e4SLinus Torvalds list_add(&jeb->list, &c->erase_pending_list); 1551da177e4SLinus Torvalds c->nr_erasing_blocks++; 1561da177e4SLinus Torvalds jffs2_erase_pending_trigger(c); 1571da177e4SLinus Torvalds } 1581da177e4SLinus Torvalds 1599bfeb691SDavid Woodhouse if (!jffs2_prealloc_raw_node_refs(c, jeb, 1)) { 1609bfeb691SDavid Woodhouse uint32_t oldfree = jeb->free_size; 1619bfeb691SDavid Woodhouse 1629bfeb691SDavid Woodhouse jffs2_link_node_ref(c, jeb, 1639bfeb691SDavid Woodhouse (jeb->offset+c->sector_size-oldfree) | REF_OBSOLETE, 1649bfeb691SDavid Woodhouse oldfree, NULL); 1659bfeb691SDavid Woodhouse /* convert to wasted */ 1669bfeb691SDavid Woodhouse c->wasted_size += oldfree; 1679bfeb691SDavid Woodhouse jeb->wasted_size += oldfree; 1689bfeb691SDavid Woodhouse c->dirty_size -= oldfree; 1699bfeb691SDavid Woodhouse jeb->dirty_size -= oldfree; 1709bfeb691SDavid Woodhouse } 1711da177e4SLinus Torvalds 172e0c8e42fSArtem B. Bityutskiy jffs2_dbg_dump_block_lists_nolock(c); 173e0c8e42fSArtem B. Bityutskiy jffs2_dbg_acct_sanity_check_nolock(c,jeb); 174e0c8e42fSArtem B. Bityutskiy jffs2_dbg_acct_paranoia_check_nolock(c, jeb); 1751da177e4SLinus Torvalds } 1761da177e4SLinus Torvalds 1779bfeb691SDavid Woodhouse static struct jffs2_raw_node_ref **jffs2_incore_replace_raw(struct jffs2_sb_info *c, 1789bfeb691SDavid Woodhouse struct jffs2_inode_info *f, 1799bfeb691SDavid Woodhouse struct jffs2_raw_node_ref *raw, 1809bfeb691SDavid Woodhouse union jffs2_node_union *node) 1819bfeb691SDavid Woodhouse { 1829bfeb691SDavid Woodhouse struct jffs2_node_frag *frag; 1839bfeb691SDavid Woodhouse struct jffs2_full_dirent *fd; 1849bfeb691SDavid Woodhouse 1859bfeb691SDavid Woodhouse dbg_noderef("incore_replace_raw: node at %p is {%04x,%04x}\n", 1869bfeb691SDavid Woodhouse node, je16_to_cpu(node->u.magic), je16_to_cpu(node->u.nodetype)); 1879bfeb691SDavid Woodhouse 1889bfeb691SDavid Woodhouse BUG_ON(je16_to_cpu(node->u.magic) != 0x1985 && 1899bfeb691SDavid Woodhouse je16_to_cpu(node->u.magic) != 0); 1909bfeb691SDavid Woodhouse 1919bfeb691SDavid Woodhouse switch (je16_to_cpu(node->u.nodetype)) { 1929bfeb691SDavid Woodhouse case JFFS2_NODETYPE_INODE: 193ddc58bd6SDavid Woodhouse if (f->metadata && f->metadata->raw == raw) { 194ddc58bd6SDavid Woodhouse dbg_noderef("Will replace ->raw in f->metadata at %p\n", f->metadata); 195ddc58bd6SDavid Woodhouse return &f->metadata->raw; 196ddc58bd6SDavid Woodhouse } 1979bfeb691SDavid Woodhouse frag = jffs2_lookup_node_frag(&f->fragtree, je32_to_cpu(node->i.offset)); 1989bfeb691SDavid Woodhouse BUG_ON(!frag); 1999bfeb691SDavid Woodhouse /* Find a frag which refers to the full_dnode we want to modify */ 2009bfeb691SDavid Woodhouse while (!frag->node || frag->node->raw != raw) { 2019bfeb691SDavid Woodhouse frag = frag_next(frag); 2029bfeb691SDavid Woodhouse BUG_ON(!frag); 2039bfeb691SDavid Woodhouse } 2049bfeb691SDavid Woodhouse dbg_noderef("Will replace ->raw in full_dnode at %p\n", frag->node); 2059bfeb691SDavid Woodhouse return &frag->node->raw; 2069bfeb691SDavid Woodhouse 2079bfeb691SDavid Woodhouse case JFFS2_NODETYPE_DIRENT: 2089bfeb691SDavid Woodhouse for (fd = f->dents; fd; fd = fd->next) { 2099bfeb691SDavid Woodhouse if (fd->raw == raw) { 2109bfeb691SDavid Woodhouse dbg_noderef("Will replace ->raw in full_dirent at %p\n", fd); 2119bfeb691SDavid Woodhouse return &fd->raw; 2129bfeb691SDavid Woodhouse } 2139bfeb691SDavid Woodhouse } 2149bfeb691SDavid Woodhouse BUG(); 215ddc58bd6SDavid Woodhouse 2169bfeb691SDavid Woodhouse default: 2179bfeb691SDavid Woodhouse dbg_noderef("Don't care about replacing raw for nodetype %x\n", 2189bfeb691SDavid Woodhouse je16_to_cpu(node->u.nodetype)); 2199bfeb691SDavid Woodhouse break; 2209bfeb691SDavid Woodhouse } 2219bfeb691SDavid Woodhouse return NULL; 2229bfeb691SDavid Woodhouse } 2239bfeb691SDavid Woodhouse 2241da177e4SLinus Torvalds /* Recover from failure to write wbuf. Recover the nodes up to the 2251da177e4SLinus Torvalds * wbuf, not the one which we were starting to try to write. */ 2261da177e4SLinus Torvalds 2271da177e4SLinus Torvalds static void jffs2_wbuf_recover(struct jffs2_sb_info *c) 2281da177e4SLinus Torvalds { 2291da177e4SLinus Torvalds struct jffs2_eraseblock *jeb, *new_jeb; 2309bfeb691SDavid Woodhouse struct jffs2_raw_node_ref *raw, *next, *first_raw = NULL; 2311da177e4SLinus Torvalds size_t retlen; 2321da177e4SLinus Torvalds int ret; 2339bfeb691SDavid Woodhouse int nr_refile = 0; 2341da177e4SLinus Torvalds unsigned char *buf; 2351da177e4SLinus Torvalds uint32_t start, end, ofs, len; 2361da177e4SLinus Torvalds 237046b8b98SDavid Woodhouse jeb = &c->blocks[c->wbuf_ofs / c->sector_size]; 238046b8b98SDavid Woodhouse 2391da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 2407f716cf3SEstelle Hammache jffs2_block_refile(c, jeb, REFILE_NOTEMPTY); 2419bfeb691SDavid Woodhouse spin_unlock(&c->erase_completion_lock); 2429bfeb691SDavid Woodhouse 2439bfeb691SDavid Woodhouse BUG_ON(!ref_obsolete(jeb->last_node)); 2441da177e4SLinus Torvalds 2451da177e4SLinus Torvalds /* Find the first node to be recovered, by skipping over every 2461da177e4SLinus Torvalds node which ends before the wbuf starts, or which is obsolete. */ 2479bfeb691SDavid Woodhouse for (next = raw = jeb->first_node; next; raw = next) { 2489bfeb691SDavid Woodhouse next = ref_next(raw); 2499bfeb691SDavid Woodhouse 2509bfeb691SDavid Woodhouse if (ref_obsolete(raw) || 2519bfeb691SDavid Woodhouse (next && ref_offset(next) <= c->wbuf_ofs)) { 2529bfeb691SDavid Woodhouse dbg_noderef("Skipping node at 0x%08x(%d)-0x%08x which is either before 0x%08x or obsolete\n", 2539bfeb691SDavid Woodhouse ref_offset(raw), ref_flags(raw), 2549bfeb691SDavid Woodhouse (ref_offset(raw) + ref_totlen(c, jeb, raw)), 2559bfeb691SDavid Woodhouse c->wbuf_ofs); 2569bfeb691SDavid Woodhouse continue; 2579bfeb691SDavid Woodhouse } 2589bfeb691SDavid Woodhouse dbg_noderef("First node to be recovered is at 0x%08x(%d)-0x%08x\n", 2599bfeb691SDavid Woodhouse ref_offset(raw), ref_flags(raw), 2609bfeb691SDavid Woodhouse (ref_offset(raw) + ref_totlen(c, jeb, raw))); 2619bfeb691SDavid Woodhouse 2629bfeb691SDavid Woodhouse first_raw = raw; 2639bfeb691SDavid Woodhouse break; 2641da177e4SLinus Torvalds } 2651da177e4SLinus Torvalds 2669bfeb691SDavid Woodhouse if (!first_raw) { 2671da177e4SLinus Torvalds /* All nodes were obsolete. Nothing to recover. */ 2681da177e4SLinus Torvalds D1(printk(KERN_DEBUG "No non-obsolete nodes to be recovered. Just filing block bad\n")); 2699bfeb691SDavid Woodhouse c->wbuf_len = 0; 2701da177e4SLinus Torvalds return; 2711da177e4SLinus Torvalds } 2721da177e4SLinus Torvalds 2739bfeb691SDavid Woodhouse start = ref_offset(first_raw); 2749bfeb691SDavid Woodhouse end = ref_offset(jeb->last_node); 2759bfeb691SDavid Woodhouse nr_refile = 1; 2761da177e4SLinus Torvalds 2779bfeb691SDavid Woodhouse /* Count the number of refs which need to be copied */ 2789bfeb691SDavid Woodhouse while ((raw = ref_next(raw)) != jeb->last_node) 2799bfeb691SDavid Woodhouse nr_refile++; 2801da177e4SLinus Torvalds 2819bfeb691SDavid Woodhouse dbg_noderef("wbuf recover %08x-%08x (%d bytes in %d nodes)\n", 2829bfeb691SDavid Woodhouse start, end, end - start, nr_refile); 2831da177e4SLinus Torvalds 2841da177e4SLinus Torvalds buf = NULL; 2851da177e4SLinus Torvalds if (start < c->wbuf_ofs) { 2861da177e4SLinus Torvalds /* First affected node was already partially written. 2871da177e4SLinus Torvalds * Attempt to reread the old data into our buffer. */ 2881da177e4SLinus Torvalds 2891da177e4SLinus Torvalds buf = kmalloc(end - start, GFP_KERNEL); 2901da177e4SLinus Torvalds if (!buf) { 2911da177e4SLinus Torvalds printk(KERN_CRIT "Malloc failure in wbuf recovery. Data loss ensues.\n"); 2921da177e4SLinus Torvalds 2931da177e4SLinus Torvalds goto read_failed; 2941da177e4SLinus Torvalds } 2951da177e4SLinus Torvalds 2961da177e4SLinus Torvalds /* Do the read... */ 2971da177e4SLinus Torvalds ret = c->mtd->read(c->mtd, start, c->wbuf_ofs - start, &retlen, buf); 2981da177e4SLinus Torvalds 2999a1fcdfdSThomas Gleixner /* ECC recovered ? */ 3009a1fcdfdSThomas Gleixner if ((ret == -EUCLEAN || ret == -EBADMSG) && 3019a1fcdfdSThomas Gleixner (retlen == c->wbuf_ofs - start)) 3021da177e4SLinus Torvalds ret = 0; 3039a1fcdfdSThomas Gleixner 3041da177e4SLinus Torvalds if (ret || retlen != c->wbuf_ofs - start) { 3051da177e4SLinus Torvalds printk(KERN_CRIT "Old data are already lost in wbuf recovery. Data loss ensues.\n"); 3061da177e4SLinus Torvalds 3071da177e4SLinus Torvalds kfree(buf); 3081da177e4SLinus Torvalds buf = NULL; 3091da177e4SLinus Torvalds read_failed: 3109bfeb691SDavid Woodhouse first_raw = ref_next(first_raw); 3119bfeb691SDavid Woodhouse nr_refile--; 3129bfeb691SDavid Woodhouse while (first_raw && ref_obsolete(first_raw)) { 3139bfeb691SDavid Woodhouse first_raw = ref_next(first_raw); 3149bfeb691SDavid Woodhouse nr_refile--; 3159bfeb691SDavid Woodhouse } 3169bfeb691SDavid Woodhouse 3171da177e4SLinus Torvalds /* If this was the only node to be recovered, give up */ 3189bfeb691SDavid Woodhouse if (!first_raw) { 3199bfeb691SDavid Woodhouse c->wbuf_len = 0; 3201da177e4SLinus Torvalds return; 3219bfeb691SDavid Woodhouse } 3221da177e4SLinus Torvalds 3231da177e4SLinus Torvalds /* It wasn't. Go on and try to recover nodes complete in the wbuf */ 3249bfeb691SDavid Woodhouse start = ref_offset(first_raw); 3259bfeb691SDavid Woodhouse dbg_noderef("wbuf now recover %08x-%08x (%d bytes in %d nodes)\n", 3269bfeb691SDavid Woodhouse start, end, end - start, nr_refile); 3279bfeb691SDavid Woodhouse 3281da177e4SLinus Torvalds } else { 3291da177e4SLinus Torvalds /* Read succeeded. Copy the remaining data from the wbuf */ 3301da177e4SLinus Torvalds memcpy(buf + (c->wbuf_ofs - start), c->wbuf, end - c->wbuf_ofs); 3311da177e4SLinus Torvalds } 3321da177e4SLinus Torvalds } 3331da177e4SLinus Torvalds /* OK... we're to rewrite (end-start) bytes of data from first_raw onwards. 3341da177e4SLinus Torvalds Either 'buf' contains the data, or we find it in the wbuf */ 3351da177e4SLinus Torvalds 3361da177e4SLinus Torvalds /* ... and get an allocation of space from a shiny new block instead */ 3379fe4854cSDavid Woodhouse ret = jffs2_reserve_space_gc(c, end-start, &len, JFFS2_SUMMARY_NOSUM_SIZE); 3381da177e4SLinus Torvalds if (ret) { 3391da177e4SLinus Torvalds printk(KERN_WARNING "Failed to allocate space for wbuf recovery. Data loss ensues.\n"); 3401da177e4SLinus Torvalds kfree(buf); 3411da177e4SLinus Torvalds return; 3421da177e4SLinus Torvalds } 3439bfeb691SDavid Woodhouse 3449bfeb691SDavid Woodhouse ret = jffs2_prealloc_raw_node_refs(c, c->nextblock, nr_refile); 3459bfeb691SDavid Woodhouse if (ret) { 3469bfeb691SDavid Woodhouse printk(KERN_WARNING "Failed to allocate node refs for wbuf recovery. Data loss ensues.\n"); 3479bfeb691SDavid Woodhouse kfree(buf); 3489bfeb691SDavid Woodhouse return; 3499bfeb691SDavid Woodhouse } 3509bfeb691SDavid Woodhouse 3519fe4854cSDavid Woodhouse ofs = write_ofs(c); 3529fe4854cSDavid Woodhouse 3531da177e4SLinus Torvalds if (end-start >= c->wbuf_pagesize) { 3547f716cf3SEstelle Hammache /* Need to do another write immediately, but it's possible 3557f716cf3SEstelle Hammache that this is just because the wbuf itself is completely 3567f716cf3SEstelle Hammache full, and there's nothing earlier read back from the 3577f716cf3SEstelle Hammache flash. Hence 'buf' isn't necessarily what we're writing 3587f716cf3SEstelle Hammache from. */ 3597f716cf3SEstelle Hammache unsigned char *rewrite_buf = buf?:c->wbuf; 3601da177e4SLinus Torvalds uint32_t towrite = (end-start) - ((end-start)%c->wbuf_pagesize); 3611da177e4SLinus Torvalds 3621da177e4SLinus Torvalds D1(printk(KERN_DEBUG "Write 0x%x bytes at 0x%08x in wbuf recover\n", 3631da177e4SLinus Torvalds towrite, ofs)); 3641da177e4SLinus Torvalds 3651da177e4SLinus Torvalds #ifdef BREAKMEHEADER 3661da177e4SLinus Torvalds static int breakme; 3671da177e4SLinus Torvalds if (breakme++ == 20) { 3681da177e4SLinus Torvalds printk(KERN_NOTICE "Faking write error at 0x%08x\n", ofs); 3691da177e4SLinus Torvalds breakme = 0; 3709223a456SThomas Gleixner c->mtd->write(c->mtd, ofs, towrite, &retlen, 3719223a456SThomas Gleixner brokenbuf); 3721da177e4SLinus Torvalds ret = -EIO; 3731da177e4SLinus Torvalds } else 3741da177e4SLinus Torvalds #endif 3759223a456SThomas Gleixner ret = c->mtd->write(c->mtd, ofs, towrite, &retlen, 3769223a456SThomas Gleixner rewrite_buf); 3771da177e4SLinus Torvalds 3781da177e4SLinus Torvalds if (ret || retlen != towrite) { 3791da177e4SLinus Torvalds /* Argh. We tried. Really we did. */ 3801da177e4SLinus Torvalds printk(KERN_CRIT "Recovery of wbuf failed due to a second write error\n"); 3811da177e4SLinus Torvalds kfree(buf); 3821da177e4SLinus Torvalds 3832f785402SDavid Woodhouse if (retlen) 3849bfeb691SDavid Woodhouse jffs2_add_physical_node_ref(c, ofs | REF_OBSOLETE, ref_totlen(c, jeb, first_raw), NULL); 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds return; 3871da177e4SLinus Torvalds } 3881da177e4SLinus Torvalds printk(KERN_NOTICE "Recovery of wbuf succeeded to %08x\n", ofs); 3891da177e4SLinus Torvalds 3901da177e4SLinus Torvalds c->wbuf_len = (end - start) - towrite; 3911da177e4SLinus Torvalds c->wbuf_ofs = ofs + towrite; 3927f716cf3SEstelle Hammache memmove(c->wbuf, rewrite_buf + towrite, c->wbuf_len); 3931da177e4SLinus Torvalds /* Don't muck about with c->wbuf_inodes. False positives are harmless. */ 3941da177e4SLinus Torvalds } else { 3951da177e4SLinus Torvalds /* OK, now we're left with the dregs in whichever buffer we're using */ 3961da177e4SLinus Torvalds if (buf) { 3971da177e4SLinus Torvalds memcpy(c->wbuf, buf, end-start); 3981da177e4SLinus Torvalds } else { 3991da177e4SLinus Torvalds memmove(c->wbuf, c->wbuf + (start - c->wbuf_ofs), end - start); 4001da177e4SLinus Torvalds } 4011da177e4SLinus Torvalds c->wbuf_ofs = ofs; 4021da177e4SLinus Torvalds c->wbuf_len = end - start; 4031da177e4SLinus Torvalds } 4041da177e4SLinus Torvalds 4051da177e4SLinus Torvalds /* Now sort out the jffs2_raw_node_refs, moving them from the old to the next block */ 4061da177e4SLinus Torvalds new_jeb = &c->blocks[ofs / c->sector_size]; 4071da177e4SLinus Torvalds 4081da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 4099bfeb691SDavid Woodhouse for (raw = first_raw; raw != jeb->last_node; raw = ref_next(raw)) { 4109bfeb691SDavid Woodhouse uint32_t rawlen = ref_totlen(c, jeb, raw); 4119bfeb691SDavid Woodhouse struct jffs2_inode_cache *ic; 4129bfeb691SDavid Woodhouse struct jffs2_raw_node_ref *new_ref; 4139bfeb691SDavid Woodhouse struct jffs2_raw_node_ref **adjust_ref = NULL; 4149bfeb691SDavid Woodhouse struct jffs2_inode_info *f = NULL; 4151da177e4SLinus Torvalds 4161da177e4SLinus Torvalds D1(printk(KERN_DEBUG "Refiling block of %08x at %08x(%d) to %08x\n", 4179bfeb691SDavid Woodhouse rawlen, ref_offset(raw), ref_flags(raw), ofs)); 4181da177e4SLinus Torvalds 4199bfeb691SDavid Woodhouse ic = jffs2_raw_ref_to_ic(raw); 4209bfeb691SDavid Woodhouse 4219bfeb691SDavid Woodhouse /* Ick. This XATTR mess should be fixed shortly... */ 4229bfeb691SDavid Woodhouse if (ic && ic->class == RAWNODE_CLASS_XATTR_DATUM) { 4239bfeb691SDavid Woodhouse struct jffs2_xattr_datum *xd = (void *)ic; 4249bfeb691SDavid Woodhouse BUG_ON(xd->node != raw); 4259bfeb691SDavid Woodhouse adjust_ref = &xd->node; 4269bfeb691SDavid Woodhouse raw->next_in_ino = NULL; 4279bfeb691SDavid Woodhouse ic = NULL; 4289bfeb691SDavid Woodhouse } else if (ic && ic->class == RAWNODE_CLASS_XATTR_REF) { 4299bfeb691SDavid Woodhouse struct jffs2_xattr_datum *xr = (void *)ic; 4309bfeb691SDavid Woodhouse BUG_ON(xr->node != raw); 4319bfeb691SDavid Woodhouse adjust_ref = &xr->node; 4329bfeb691SDavid Woodhouse raw->next_in_ino = NULL; 4339bfeb691SDavid Woodhouse ic = NULL; 4349bfeb691SDavid Woodhouse } else if (ic && ic->class == RAWNODE_CLASS_INODE_CACHE) { 4359bfeb691SDavid Woodhouse struct jffs2_raw_node_ref **p = &ic->nodes; 4369bfeb691SDavid Woodhouse 4379bfeb691SDavid Woodhouse /* Remove the old node from the per-inode list */ 4389bfeb691SDavid Woodhouse while (*p && *p != (void *)ic) { 4399bfeb691SDavid Woodhouse if (*p == raw) { 4409bfeb691SDavid Woodhouse (*p) = (raw->next_in_ino); 4419bfeb691SDavid Woodhouse raw->next_in_ino = NULL; 4429bfeb691SDavid Woodhouse break; 4439bfeb691SDavid Woodhouse } 4449bfeb691SDavid Woodhouse p = &((*p)->next_in_ino); 4459bfeb691SDavid Woodhouse } 4469bfeb691SDavid Woodhouse 4479bfeb691SDavid Woodhouse if (ic->state == INO_STATE_PRESENT && !ref_obsolete(raw)) { 4489bfeb691SDavid Woodhouse /* If it's an in-core inode, then we have to adjust any 4499bfeb691SDavid Woodhouse full_dirent or full_dnode structure to point to the 4509bfeb691SDavid Woodhouse new version instead of the old */ 4519bfeb691SDavid Woodhouse f = jffs2_gc_fetch_inode(c, ic->ino, ic->nlink); 4529bfeb691SDavid Woodhouse if (IS_ERR(f)) { 4539bfeb691SDavid Woodhouse /* Should never happen; it _must_ be present */ 4549bfeb691SDavid Woodhouse JFFS2_ERROR("Failed to iget() ino #%u, err %ld\n", 4559bfeb691SDavid Woodhouse ic->ino, PTR_ERR(f)); 4569bfeb691SDavid Woodhouse BUG(); 4579bfeb691SDavid Woodhouse } 4589bfeb691SDavid Woodhouse /* We don't lock f->sem. There's a number of ways we could 4599bfeb691SDavid Woodhouse end up in here with it already being locked, and nobody's 4609bfeb691SDavid Woodhouse going to modify it on us anyway because we hold the 4619bfeb691SDavid Woodhouse alloc_sem. We're only changing one ->raw pointer too, 4629bfeb691SDavid Woodhouse which we can get away with without upsetting readers. */ 4639bfeb691SDavid Woodhouse adjust_ref = jffs2_incore_replace_raw(c, f, raw, 4649bfeb691SDavid Woodhouse (void *)(buf?:c->wbuf) + (ref_offset(raw) - start)); 4659bfeb691SDavid Woodhouse } else if (unlikely(ic->state != INO_STATE_PRESENT && 4669bfeb691SDavid Woodhouse ic->state != INO_STATE_CHECKEDABSENT && 4679bfeb691SDavid Woodhouse ic->state != INO_STATE_GC)) { 4689bfeb691SDavid Woodhouse JFFS2_ERROR("Inode #%u is in strange state %d!\n", ic->ino, ic->state); 4699bfeb691SDavid Woodhouse BUG(); 4709bfeb691SDavid Woodhouse } 4719bfeb691SDavid Woodhouse } 4729bfeb691SDavid Woodhouse 4739bfeb691SDavid Woodhouse new_ref = jffs2_link_node_ref(c, new_jeb, ofs | ref_flags(raw), rawlen, ic); 4749bfeb691SDavid Woodhouse 4759bfeb691SDavid Woodhouse if (adjust_ref) { 4769bfeb691SDavid Woodhouse BUG_ON(*adjust_ref != raw); 4779bfeb691SDavid Woodhouse *adjust_ref = new_ref; 4789bfeb691SDavid Woodhouse } 4799bfeb691SDavid Woodhouse if (f) 4809bfeb691SDavid Woodhouse jffs2_gc_release_inode(c, f); 4819bfeb691SDavid Woodhouse 4829bfeb691SDavid Woodhouse if (!ref_obsolete(raw)) { 4831da177e4SLinus Torvalds jeb->dirty_size += rawlen; 4841da177e4SLinus Torvalds jeb->used_size -= rawlen; 4851da177e4SLinus Torvalds c->dirty_size += rawlen; 4869bfeb691SDavid Woodhouse c->used_size -= rawlen; 4879bfeb691SDavid Woodhouse raw->flash_offset = ref_offset(raw) | REF_OBSOLETE; 4889bfeb691SDavid Woodhouse BUG_ON(raw->next_in_ino); 4891da177e4SLinus Torvalds } 4901da177e4SLinus Torvalds ofs += rawlen; 4911da177e4SLinus Torvalds } 4921da177e4SLinus Torvalds 4939bfeb691SDavid Woodhouse kfree(buf); 4949bfeb691SDavid Woodhouse 4951da177e4SLinus Torvalds /* Fix up the original jeb now it's on the bad_list */ 4969bfeb691SDavid Woodhouse if (first_raw == jeb->first_node) { 4971da177e4SLinus Torvalds D1(printk(KERN_DEBUG "Failing block at %08x is now empty. Moving to erase_pending_list\n", jeb->offset)); 4981da177e4SLinus Torvalds list_del(&jeb->list); 4991da177e4SLinus Torvalds list_add(&jeb->list, &c->erase_pending_list); 5001da177e4SLinus Torvalds c->nr_erasing_blocks++; 5011da177e4SLinus Torvalds jffs2_erase_pending_trigger(c); 5021da177e4SLinus Torvalds } 5031da177e4SLinus Torvalds 504e0c8e42fSArtem B. Bityutskiy jffs2_dbg_acct_sanity_check_nolock(c, jeb); 505e0c8e42fSArtem B. Bityutskiy jffs2_dbg_acct_paranoia_check_nolock(c, jeb); 5061da177e4SLinus Torvalds 507e0c8e42fSArtem B. Bityutskiy jffs2_dbg_acct_sanity_check_nolock(c, new_jeb); 508e0c8e42fSArtem B. Bityutskiy jffs2_dbg_acct_paranoia_check_nolock(c, new_jeb); 5091da177e4SLinus Torvalds 5101da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 5111da177e4SLinus Torvalds 5129bfeb691SDavid Woodhouse D1(printk(KERN_DEBUG "wbuf recovery completed OK. wbuf_ofs 0x%08x, len 0x%x\n", c->wbuf_ofs, c->wbuf_len)); 5139bfeb691SDavid Woodhouse 5141da177e4SLinus Torvalds } 5151da177e4SLinus Torvalds 5161da177e4SLinus Torvalds /* Meaning of pad argument: 5171da177e4SLinus Torvalds 0: Do not pad. Probably pointless - we only ever use this when we can't pad anyway. 5181da177e4SLinus Torvalds 1: Pad, do not adjust nextblock free_size 5191da177e4SLinus Torvalds 2: Pad, adjust nextblock free_size 5201da177e4SLinus Torvalds */ 5211da177e4SLinus Torvalds #define NOPAD 0 5221da177e4SLinus Torvalds #define PAD_NOACCOUNT 1 5231da177e4SLinus Torvalds #define PAD_ACCOUNTING 2 5241da177e4SLinus Torvalds 5251da177e4SLinus Torvalds static int __jffs2_flush_wbuf(struct jffs2_sb_info *c, int pad) 5261da177e4SLinus Torvalds { 5279bfeb691SDavid Woodhouse struct jffs2_eraseblock *wbuf_jeb; 5281da177e4SLinus Torvalds int ret; 5291da177e4SLinus Torvalds size_t retlen; 5301da177e4SLinus Torvalds 5313be36675SAndrew Victor /* Nothing to do if not write-buffering the flash. In particular, we shouldn't 5321da177e4SLinus Torvalds del_timer() the timer we never initialised. */ 5333be36675SAndrew Victor if (!jffs2_is_writebuffered(c)) 5341da177e4SLinus Torvalds return 0; 5351da177e4SLinus Torvalds 5361da177e4SLinus Torvalds if (!down_trylock(&c->alloc_sem)) { 5371da177e4SLinus Torvalds up(&c->alloc_sem); 5381da177e4SLinus Torvalds printk(KERN_CRIT "jffs2_flush_wbuf() called with alloc_sem not locked!\n"); 5391da177e4SLinus Torvalds BUG(); 5401da177e4SLinus Torvalds } 5411da177e4SLinus Torvalds 5423be36675SAndrew Victor if (!c->wbuf_len) /* already checked c->wbuf above */ 5431da177e4SLinus Torvalds return 0; 5441da177e4SLinus Torvalds 5459bfeb691SDavid Woodhouse wbuf_jeb = &c->blocks[c->wbuf_ofs / c->sector_size]; 5469bfeb691SDavid Woodhouse if (jffs2_prealloc_raw_node_refs(c, wbuf_jeb, c->nextblock->allocated_refs + 1)) 5472f785402SDavid Woodhouse return -ENOMEM; 5482f785402SDavid Woodhouse 5491da177e4SLinus Torvalds /* claim remaining space on the page 5501da177e4SLinus Torvalds this happens, if we have a change to a new block, 5511da177e4SLinus Torvalds or if fsync forces us to flush the writebuffer. 5521da177e4SLinus Torvalds if we have a switch to next page, we will not have 5531da177e4SLinus Torvalds enough remaining space for this. 5541da177e4SLinus Torvalds */ 555daba5cc4SArtem B. Bityutskiy if (pad ) { 5561da177e4SLinus Torvalds c->wbuf_len = PAD(c->wbuf_len); 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds /* Pad with JFFS2_DIRTY_BITMASK initially. this helps out ECC'd NOR 5591da177e4SLinus Torvalds with 8 byte page size */ 5601da177e4SLinus Torvalds memset(c->wbuf + c->wbuf_len, 0, c->wbuf_pagesize - c->wbuf_len); 5611da177e4SLinus Torvalds 5621da177e4SLinus Torvalds if ( c->wbuf_len + sizeof(struct jffs2_unknown_node) < c->wbuf_pagesize) { 5631da177e4SLinus Torvalds struct jffs2_unknown_node *padnode = (void *)(c->wbuf + c->wbuf_len); 5641da177e4SLinus Torvalds padnode->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); 5651da177e4SLinus Torvalds padnode->nodetype = cpu_to_je16(JFFS2_NODETYPE_PADDING); 5661da177e4SLinus Torvalds padnode->totlen = cpu_to_je32(c->wbuf_pagesize - c->wbuf_len); 5671da177e4SLinus Torvalds padnode->hdr_crc = cpu_to_je32(crc32(0, padnode, sizeof(*padnode)-4)); 5681da177e4SLinus Torvalds } 5691da177e4SLinus Torvalds } 5701da177e4SLinus Torvalds /* else jffs2_flash_writev has actually filled in the rest of the 5711da177e4SLinus Torvalds buffer for us, and will deal with the node refs etc. later. */ 5721da177e4SLinus Torvalds 5731da177e4SLinus Torvalds #ifdef BREAKME 5741da177e4SLinus Torvalds static int breakme; 5751da177e4SLinus Torvalds if (breakme++ == 20) { 5761da177e4SLinus Torvalds printk(KERN_NOTICE "Faking write error at 0x%08x\n", c->wbuf_ofs); 5771da177e4SLinus Torvalds breakme = 0; 5789223a456SThomas Gleixner c->mtd->write(c->mtd, c->wbuf_ofs, c->wbuf_pagesize, &retlen, 5799223a456SThomas Gleixner brokenbuf); 5801da177e4SLinus Torvalds ret = -EIO; 5811da177e4SLinus Torvalds } else 5821da177e4SLinus Torvalds #endif 5831da177e4SLinus Torvalds 5841da177e4SLinus Torvalds ret = c->mtd->write(c->mtd, c->wbuf_ofs, c->wbuf_pagesize, &retlen, c->wbuf); 5851da177e4SLinus Torvalds 5861da177e4SLinus Torvalds if (ret || retlen != c->wbuf_pagesize) { 5871da177e4SLinus Torvalds if (ret) 5881da177e4SLinus Torvalds printk(KERN_WARNING "jffs2_flush_wbuf(): Write failed with %d\n",ret); 5891da177e4SLinus Torvalds else { 5901da177e4SLinus Torvalds printk(KERN_WARNING "jffs2_flush_wbuf(): Write was short: %zd instead of %d\n", 5911da177e4SLinus Torvalds retlen, c->wbuf_pagesize); 5921da177e4SLinus Torvalds ret = -EIO; 5931da177e4SLinus Torvalds } 5941da177e4SLinus Torvalds 5951da177e4SLinus Torvalds jffs2_wbuf_recover(c); 5961da177e4SLinus Torvalds 5971da177e4SLinus Torvalds return ret; 5981da177e4SLinus Torvalds } 5991da177e4SLinus Torvalds 6001da177e4SLinus Torvalds /* Adjust free size of the block if we padded. */ 601daba5cc4SArtem B. Bityutskiy if (pad) { 6020bcc099dSDavid Woodhouse uint32_t waste = c->wbuf_pagesize - c->wbuf_len; 6031da177e4SLinus Torvalds 6041da177e4SLinus Torvalds D1(printk(KERN_DEBUG "jffs2_flush_wbuf() adjusting free_size of %sblock at %08x\n", 6059bfeb691SDavid Woodhouse (wbuf_jeb==c->nextblock)?"next":"", wbuf_jeb->offset)); 6061da177e4SLinus Torvalds 6071da177e4SLinus Torvalds /* wbuf_pagesize - wbuf_len is the amount of space that's to be 6081da177e4SLinus Torvalds padded. If there is less free space in the block than that, 6091da177e4SLinus Torvalds something screwed up */ 6109bfeb691SDavid Woodhouse if (wbuf_jeb->free_size < waste) { 6111da177e4SLinus Torvalds printk(KERN_CRIT "jffs2_flush_wbuf(): Accounting error. wbuf at 0x%08x has 0x%03x bytes, 0x%03x left.\n", 6120bcc099dSDavid Woodhouse c->wbuf_ofs, c->wbuf_len, waste); 6131da177e4SLinus Torvalds printk(KERN_CRIT "jffs2_flush_wbuf(): But free_size for block at 0x%08x is only 0x%08x\n", 6149bfeb691SDavid Woodhouse wbuf_jeb->offset, wbuf_jeb->free_size); 6151da177e4SLinus Torvalds BUG(); 6161da177e4SLinus Torvalds } 6170bcc099dSDavid Woodhouse 6180bcc099dSDavid Woodhouse spin_lock(&c->erase_completion_lock); 6190bcc099dSDavid Woodhouse 6209bfeb691SDavid Woodhouse jffs2_link_node_ref(c, wbuf_jeb, (c->wbuf_ofs + c->wbuf_len) | REF_OBSOLETE, waste, NULL); 6210bcc099dSDavid Woodhouse /* FIXME: that made it count as dirty. Convert to wasted */ 6229bfeb691SDavid Woodhouse wbuf_jeb->dirty_size -= waste; 6230bcc099dSDavid Woodhouse c->dirty_size -= waste; 6249bfeb691SDavid Woodhouse wbuf_jeb->wasted_size += waste; 6250bcc099dSDavid Woodhouse c->wasted_size += waste; 6260bcc099dSDavid Woodhouse } else 6270bcc099dSDavid Woodhouse spin_lock(&c->erase_completion_lock); 6281da177e4SLinus Torvalds 6291da177e4SLinus Torvalds /* Stick any now-obsoleted blocks on the erase_pending_list */ 6301da177e4SLinus Torvalds jffs2_refile_wbuf_blocks(c); 6311da177e4SLinus Torvalds jffs2_clear_wbuf_ino_list(c); 6321da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 6331da177e4SLinus Torvalds 6341da177e4SLinus Torvalds memset(c->wbuf,0xff,c->wbuf_pagesize); 6351da177e4SLinus Torvalds /* adjust write buffer offset, else we get a non contiguous write bug */ 6361da177e4SLinus Torvalds c->wbuf_ofs += c->wbuf_pagesize; 6371da177e4SLinus Torvalds c->wbuf_len = 0; 6381da177e4SLinus Torvalds return 0; 6391da177e4SLinus Torvalds } 6401da177e4SLinus Torvalds 6411da177e4SLinus Torvalds /* Trigger garbage collection to flush the write-buffer. 6421da177e4SLinus Torvalds If ino arg is zero, do it if _any_ real (i.e. not GC) writes are 6431da177e4SLinus Torvalds outstanding. If ino arg non-zero, do it only if a write for the 6441da177e4SLinus Torvalds given inode is outstanding. */ 6451da177e4SLinus Torvalds int jffs2_flush_wbuf_gc(struct jffs2_sb_info *c, uint32_t ino) 6461da177e4SLinus Torvalds { 6471da177e4SLinus Torvalds uint32_t old_wbuf_ofs; 6481da177e4SLinus Torvalds uint32_t old_wbuf_len; 6491da177e4SLinus Torvalds int ret = 0; 6501da177e4SLinus Torvalds 6511da177e4SLinus Torvalds D1(printk(KERN_DEBUG "jffs2_flush_wbuf_gc() called for ino #%u...\n", ino)); 6521da177e4SLinus Torvalds 6538aee6ac1SDavid Woodhouse if (!c->wbuf) 6548aee6ac1SDavid Woodhouse return 0; 6558aee6ac1SDavid Woodhouse 6561da177e4SLinus Torvalds down(&c->alloc_sem); 6571da177e4SLinus Torvalds if (!jffs2_wbuf_pending_for_ino(c, ino)) { 6581da177e4SLinus Torvalds D1(printk(KERN_DEBUG "Ino #%d not pending in wbuf. Returning\n", ino)); 6591da177e4SLinus Torvalds up(&c->alloc_sem); 6601da177e4SLinus Torvalds return 0; 6611da177e4SLinus Torvalds } 6621da177e4SLinus Torvalds 6631da177e4SLinus Torvalds old_wbuf_ofs = c->wbuf_ofs; 6641da177e4SLinus Torvalds old_wbuf_len = c->wbuf_len; 6651da177e4SLinus Torvalds 6661da177e4SLinus Torvalds if (c->unchecked_size) { 6671da177e4SLinus Torvalds /* GC won't make any progress for a while */ 6681da177e4SLinus Torvalds D1(printk(KERN_DEBUG "jffs2_flush_wbuf_gc() padding. Not finished checking\n")); 6691da177e4SLinus Torvalds down_write(&c->wbuf_sem); 6701da177e4SLinus Torvalds ret = __jffs2_flush_wbuf(c, PAD_ACCOUNTING); 6717f716cf3SEstelle Hammache /* retry flushing wbuf in case jffs2_wbuf_recover 6727f716cf3SEstelle Hammache left some data in the wbuf */ 6737f716cf3SEstelle Hammache if (ret) 6747f716cf3SEstelle Hammache ret = __jffs2_flush_wbuf(c, PAD_ACCOUNTING); 6751da177e4SLinus Torvalds up_write(&c->wbuf_sem); 6761da177e4SLinus Torvalds } else while (old_wbuf_len && 6771da177e4SLinus Torvalds old_wbuf_ofs == c->wbuf_ofs) { 6781da177e4SLinus Torvalds 6791da177e4SLinus Torvalds up(&c->alloc_sem); 6801da177e4SLinus Torvalds 6811da177e4SLinus Torvalds D1(printk(KERN_DEBUG "jffs2_flush_wbuf_gc() calls gc pass\n")); 6821da177e4SLinus Torvalds 6831da177e4SLinus Torvalds ret = jffs2_garbage_collect_pass(c); 6841da177e4SLinus Torvalds if (ret) { 6851da177e4SLinus Torvalds /* GC failed. Flush it with padding instead */ 6861da177e4SLinus Torvalds down(&c->alloc_sem); 6871da177e4SLinus Torvalds down_write(&c->wbuf_sem); 6881da177e4SLinus Torvalds ret = __jffs2_flush_wbuf(c, PAD_ACCOUNTING); 6897f716cf3SEstelle Hammache /* retry flushing wbuf in case jffs2_wbuf_recover 6907f716cf3SEstelle Hammache left some data in the wbuf */ 6917f716cf3SEstelle Hammache if (ret) 6927f716cf3SEstelle Hammache ret = __jffs2_flush_wbuf(c, PAD_ACCOUNTING); 6931da177e4SLinus Torvalds up_write(&c->wbuf_sem); 6941da177e4SLinus Torvalds break; 6951da177e4SLinus Torvalds } 6961da177e4SLinus Torvalds down(&c->alloc_sem); 6971da177e4SLinus Torvalds } 6981da177e4SLinus Torvalds 6991da177e4SLinus Torvalds D1(printk(KERN_DEBUG "jffs2_flush_wbuf_gc() ends...\n")); 7001da177e4SLinus Torvalds 7011da177e4SLinus Torvalds up(&c->alloc_sem); 7021da177e4SLinus Torvalds return ret; 7031da177e4SLinus Torvalds } 7041da177e4SLinus Torvalds 7051da177e4SLinus Torvalds /* Pad write-buffer to end and write it, wasting space. */ 7061da177e4SLinus Torvalds int jffs2_flush_wbuf_pad(struct jffs2_sb_info *c) 7071da177e4SLinus Torvalds { 7081da177e4SLinus Torvalds int ret; 7091da177e4SLinus Torvalds 7108aee6ac1SDavid Woodhouse if (!c->wbuf) 7118aee6ac1SDavid Woodhouse return 0; 7128aee6ac1SDavid Woodhouse 7131da177e4SLinus Torvalds down_write(&c->wbuf_sem); 7141da177e4SLinus Torvalds ret = __jffs2_flush_wbuf(c, PAD_NOACCOUNT); 7157f716cf3SEstelle Hammache /* retry - maybe wbuf recover left some data in wbuf. */ 7167f716cf3SEstelle Hammache if (ret) 7177f716cf3SEstelle Hammache ret = __jffs2_flush_wbuf(c, PAD_NOACCOUNT); 7181da177e4SLinus Torvalds up_write(&c->wbuf_sem); 7191da177e4SLinus Torvalds 7201da177e4SLinus Torvalds return ret; 7211da177e4SLinus Torvalds } 7221da177e4SLinus Torvalds 723dcb09328SThomas Gleixner static size_t jffs2_fill_wbuf(struct jffs2_sb_info *c, const uint8_t *buf, 724dcb09328SThomas Gleixner size_t len) 725dcb09328SThomas Gleixner { 726dcb09328SThomas Gleixner if (len && !c->wbuf_len && (len >= c->wbuf_pagesize)) 727dcb09328SThomas Gleixner return 0; 728dcb09328SThomas Gleixner 729dcb09328SThomas Gleixner if (len > (c->wbuf_pagesize - c->wbuf_len)) 730dcb09328SThomas Gleixner len = c->wbuf_pagesize - c->wbuf_len; 731dcb09328SThomas Gleixner memcpy(c->wbuf + c->wbuf_len, buf, len); 732dcb09328SThomas Gleixner c->wbuf_len += (uint32_t) len; 733dcb09328SThomas Gleixner return len; 734dcb09328SThomas Gleixner } 735dcb09328SThomas Gleixner 736dcb09328SThomas Gleixner int jffs2_flash_writev(struct jffs2_sb_info *c, const struct kvec *invecs, 737dcb09328SThomas Gleixner unsigned long count, loff_t to, size_t *retlen, 738dcb09328SThomas Gleixner uint32_t ino) 739dcb09328SThomas Gleixner { 740dcb09328SThomas Gleixner struct jffs2_eraseblock *jeb; 741dcb09328SThomas Gleixner size_t wbuf_retlen, donelen = 0; 742dcb09328SThomas Gleixner uint32_t outvec_to = to; 743dcb09328SThomas Gleixner int ret, invec; 744dcb09328SThomas Gleixner 745dcb09328SThomas Gleixner /* If not writebuffered flash, don't bother */ 7463be36675SAndrew Victor if (!jffs2_is_writebuffered(c)) 7471da177e4SLinus Torvalds return jffs2_flash_direct_writev(c, invecs, count, to, retlen); 7481da177e4SLinus Torvalds 7491da177e4SLinus Torvalds down_write(&c->wbuf_sem); 7501da177e4SLinus Torvalds 7511da177e4SLinus Torvalds /* If wbuf_ofs is not initialized, set it to target address */ 7521da177e4SLinus Torvalds if (c->wbuf_ofs == 0xFFFFFFFF) { 7531da177e4SLinus Torvalds c->wbuf_ofs = PAGE_DIV(to); 7541da177e4SLinus Torvalds c->wbuf_len = PAGE_MOD(to); 7551da177e4SLinus Torvalds memset(c->wbuf,0xff,c->wbuf_pagesize); 7561da177e4SLinus Torvalds } 7571da177e4SLinus Torvalds 758dcb09328SThomas Gleixner /* 759dcb09328SThomas Gleixner * Sanity checks on target address. It's permitted to write 760dcb09328SThomas Gleixner * at PAD(c->wbuf_len+c->wbuf_ofs), and it's permitted to 761dcb09328SThomas Gleixner * write at the beginning of a new erase block. Anything else, 762dcb09328SThomas Gleixner * and you die. New block starts at xxx000c (0-b = block 763dcb09328SThomas Gleixner * header) 7641da177e4SLinus Torvalds */ 7653be36675SAndrew Victor if (SECTOR_ADDR(to) != SECTOR_ADDR(c->wbuf_ofs)) { 7661da177e4SLinus Torvalds /* It's a write to a new block */ 7671da177e4SLinus Torvalds if (c->wbuf_len) { 768dcb09328SThomas Gleixner D1(printk(KERN_DEBUG "jffs2_flash_writev() to 0x%lx " 769dcb09328SThomas Gleixner "causes flush of wbuf at 0x%08x\n", 770dcb09328SThomas Gleixner (unsigned long)to, c->wbuf_ofs)); 7711da177e4SLinus Torvalds ret = __jffs2_flush_wbuf(c, PAD_NOACCOUNT); 772dcb09328SThomas Gleixner if (ret) 773dcb09328SThomas Gleixner goto outerr; 7741da177e4SLinus Torvalds } 7751da177e4SLinus Torvalds /* set pointer to new block */ 7761da177e4SLinus Torvalds c->wbuf_ofs = PAGE_DIV(to); 7771da177e4SLinus Torvalds c->wbuf_len = PAGE_MOD(to); 7781da177e4SLinus Torvalds } 7791da177e4SLinus Torvalds 7801da177e4SLinus Torvalds if (to != PAD(c->wbuf_ofs + c->wbuf_len)) { 7811da177e4SLinus Torvalds /* We're not writing immediately after the writebuffer. Bad. */ 782dcb09328SThomas Gleixner printk(KERN_CRIT "jffs2_flash_writev(): Non-contiguous write " 783dcb09328SThomas Gleixner "to %08lx\n", (unsigned long)to); 7841da177e4SLinus Torvalds if (c->wbuf_len) 7851da177e4SLinus Torvalds printk(KERN_CRIT "wbuf was previously %08x-%08x\n", 7861da177e4SLinus Torvalds c->wbuf_ofs, c->wbuf_ofs+c->wbuf_len); 7871da177e4SLinus Torvalds BUG(); 7881da177e4SLinus Torvalds } 7891da177e4SLinus Torvalds 7901da177e4SLinus Torvalds /* adjust alignment offset */ 7911da177e4SLinus Torvalds if (c->wbuf_len != PAGE_MOD(to)) { 7921da177e4SLinus Torvalds c->wbuf_len = PAGE_MOD(to); 7931da177e4SLinus Torvalds /* take care of alignment to next page */ 794dcb09328SThomas Gleixner if (!c->wbuf_len) { 7951da177e4SLinus Torvalds c->wbuf_len = c->wbuf_pagesize; 7961da177e4SLinus Torvalds ret = __jffs2_flush_wbuf(c, NOPAD); 797dcb09328SThomas Gleixner if (ret) 798dcb09328SThomas Gleixner goto outerr; 7991da177e4SLinus Torvalds } 8001da177e4SLinus Torvalds } 8011da177e4SLinus Torvalds 802dcb09328SThomas Gleixner for (invec = 0; invec < count; invec++) { 803dcb09328SThomas Gleixner int vlen = invecs[invec].iov_len; 804dcb09328SThomas Gleixner uint8_t *v = invecs[invec].iov_base; 8051da177e4SLinus Torvalds 806dcb09328SThomas Gleixner wbuf_retlen = jffs2_fill_wbuf(c, v, vlen); 8071da177e4SLinus Torvalds 808dcb09328SThomas Gleixner if (c->wbuf_len == c->wbuf_pagesize) { 809dcb09328SThomas Gleixner ret = __jffs2_flush_wbuf(c, NOPAD); 810dcb09328SThomas Gleixner if (ret) 811dcb09328SThomas Gleixner goto outerr; 8121da177e4SLinus Torvalds } 813dcb09328SThomas Gleixner vlen -= wbuf_retlen; 814dcb09328SThomas Gleixner outvec_to += wbuf_retlen; 8151da177e4SLinus Torvalds donelen += wbuf_retlen; 816dcb09328SThomas Gleixner v += wbuf_retlen; 8171da177e4SLinus Torvalds 818dcb09328SThomas Gleixner if (vlen >= c->wbuf_pagesize) { 819dcb09328SThomas Gleixner ret = c->mtd->write(c->mtd, outvec_to, PAGE_DIV(vlen), 820dcb09328SThomas Gleixner &wbuf_retlen, v); 821dcb09328SThomas Gleixner if (ret < 0 || wbuf_retlen != PAGE_DIV(vlen)) 822dcb09328SThomas Gleixner goto outfile; 823dcb09328SThomas Gleixner 824dcb09328SThomas Gleixner vlen -= wbuf_retlen; 825dcb09328SThomas Gleixner outvec_to += wbuf_retlen; 826dcb09328SThomas Gleixner c->wbuf_ofs = outvec_to; 827dcb09328SThomas Gleixner donelen += wbuf_retlen; 828dcb09328SThomas Gleixner v += wbuf_retlen; 8291da177e4SLinus Torvalds } 8301da177e4SLinus Torvalds 831dcb09328SThomas Gleixner wbuf_retlen = jffs2_fill_wbuf(c, v, vlen); 832dcb09328SThomas Gleixner if (c->wbuf_len == c->wbuf_pagesize) { 833dcb09328SThomas Gleixner ret = __jffs2_flush_wbuf(c, NOPAD); 834dcb09328SThomas Gleixner if (ret) 835dcb09328SThomas Gleixner goto outerr; 8361da177e4SLinus Torvalds } 8371da177e4SLinus Torvalds 838dcb09328SThomas Gleixner outvec_to += wbuf_retlen; 839dcb09328SThomas Gleixner donelen += wbuf_retlen; 8401da177e4SLinus Torvalds } 8411da177e4SLinus Torvalds 842dcb09328SThomas Gleixner /* 843dcb09328SThomas Gleixner * If there's a remainder in the wbuf and it's a non-GC write, 844dcb09328SThomas Gleixner * remember that the wbuf affects this ino 845dcb09328SThomas Gleixner */ 8461da177e4SLinus Torvalds *retlen = donelen; 8471da177e4SLinus Torvalds 848e631ddbaSFerenc Havasi if (jffs2_sum_active()) { 849e631ddbaSFerenc Havasi int res = jffs2_sum_add_kvec(c, invecs, count, (uint32_t) to); 850e631ddbaSFerenc Havasi if (res) 851e631ddbaSFerenc Havasi return res; 852e631ddbaSFerenc Havasi } 853e631ddbaSFerenc Havasi 8541da177e4SLinus Torvalds if (c->wbuf_len && ino) 8551da177e4SLinus Torvalds jffs2_wbuf_dirties_inode(c, ino); 8561da177e4SLinus Torvalds 8571da177e4SLinus Torvalds ret = 0; 858dcb09328SThomas Gleixner up_write(&c->wbuf_sem); 859dcb09328SThomas Gleixner return ret; 8601da177e4SLinus Torvalds 861dcb09328SThomas Gleixner outfile: 862dcb09328SThomas Gleixner /* 863dcb09328SThomas Gleixner * At this point we have no problem, c->wbuf is empty. However 864dcb09328SThomas Gleixner * refile nextblock to avoid writing again to same address. 865dcb09328SThomas Gleixner */ 866dcb09328SThomas Gleixner 867dcb09328SThomas Gleixner spin_lock(&c->erase_completion_lock); 868dcb09328SThomas Gleixner 869dcb09328SThomas Gleixner jeb = &c->blocks[outvec_to / c->sector_size]; 870dcb09328SThomas Gleixner jffs2_block_refile(c, jeb, REFILE_ANYWAY); 871dcb09328SThomas Gleixner 872dcb09328SThomas Gleixner spin_unlock(&c->erase_completion_lock); 873dcb09328SThomas Gleixner 874dcb09328SThomas Gleixner outerr: 875dcb09328SThomas Gleixner *retlen = 0; 8761da177e4SLinus Torvalds up_write(&c->wbuf_sem); 8771da177e4SLinus Torvalds return ret; 8781da177e4SLinus Torvalds } 8791da177e4SLinus Torvalds 8801da177e4SLinus Torvalds /* 8811da177e4SLinus Torvalds * This is the entry for flash write. 8821da177e4SLinus Torvalds * Check, if we work on NAND FLASH, if so build an kvec and write it via vritev 8831da177e4SLinus Torvalds */ 8849bfeb691SDavid Woodhouse int jffs2_flash_write(struct jffs2_sb_info *c, loff_t ofs, size_t len, 8859bfeb691SDavid Woodhouse size_t *retlen, const u_char *buf) 8861da177e4SLinus Torvalds { 8871da177e4SLinus Torvalds struct kvec vecs[1]; 8881da177e4SLinus Torvalds 8893be36675SAndrew Victor if (!jffs2_is_writebuffered(c)) 890e631ddbaSFerenc Havasi return jffs2_flash_direct_write(c, ofs, len, retlen, buf); 8911da177e4SLinus Torvalds 8921da177e4SLinus Torvalds vecs[0].iov_base = (unsigned char *) buf; 8931da177e4SLinus Torvalds vecs[0].iov_len = len; 8941da177e4SLinus Torvalds return jffs2_flash_writev(c, vecs, 1, ofs, retlen, 0); 8951da177e4SLinus Torvalds } 8961da177e4SLinus Torvalds 8971da177e4SLinus Torvalds /* 8981da177e4SLinus Torvalds Handle readback from writebuffer and ECC failure return 8991da177e4SLinus Torvalds */ 9001da177e4SLinus Torvalds int jffs2_flash_read(struct jffs2_sb_info *c, loff_t ofs, size_t len, size_t *retlen, u_char *buf) 9011da177e4SLinus Torvalds { 9021da177e4SLinus Torvalds loff_t orbf = 0, owbf = 0, lwbf = 0; 9031da177e4SLinus Torvalds int ret; 9041da177e4SLinus Torvalds 9053be36675SAndrew Victor if (!jffs2_is_writebuffered(c)) 9063be36675SAndrew Victor return c->mtd->read(c->mtd, ofs, len, retlen, buf); 9071da177e4SLinus Torvalds 9083be36675SAndrew Victor /* Read flash */ 909894214d1SArtem B. Bityuckiy down_read(&c->wbuf_sem); 9101da177e4SLinus Torvalds ret = c->mtd->read(c->mtd, ofs, len, retlen, buf); 9111da177e4SLinus Torvalds 9129a1fcdfdSThomas Gleixner if ( (ret == -EBADMSG || ret == -EUCLEAN) && (*retlen == len) ) { 9139a1fcdfdSThomas Gleixner if (ret == -EBADMSG) 9149a1fcdfdSThomas Gleixner printk(KERN_WARNING "mtd->read(0x%zx bytes from 0x%llx)" 9159a1fcdfdSThomas Gleixner " returned ECC error\n", len, ofs); 9161da177e4SLinus Torvalds /* 9179a1fcdfdSThomas Gleixner * We have the raw data without ECC correction in the buffer, 9189a1fcdfdSThomas Gleixner * maybe we are lucky and all data or parts are correct. We 9199a1fcdfdSThomas Gleixner * check the node. If data are corrupted node check will sort 9209a1fcdfdSThomas Gleixner * it out. We keep this block, it will fail on write or erase 9219a1fcdfdSThomas Gleixner * and the we mark it bad. Or should we do that now? But we 9229a1fcdfdSThomas Gleixner * should give him a chance. Maybe we had a system crash or 9239a1fcdfdSThomas Gleixner * power loss before the ecc write or a erase was completed. 9241da177e4SLinus Torvalds * So we return success. :) 9251da177e4SLinus Torvalds */ 9261da177e4SLinus Torvalds ret = 0; 9271da177e4SLinus Torvalds } 9281da177e4SLinus Torvalds 9291da177e4SLinus Torvalds /* if no writebuffer available or write buffer empty, return */ 9301da177e4SLinus Torvalds if (!c->wbuf_pagesize || !c->wbuf_len) 931894214d1SArtem B. Bityuckiy goto exit; 9321da177e4SLinus Torvalds 9331da177e4SLinus Torvalds /* if we read in a different block, return */ 9343be36675SAndrew Victor if (SECTOR_ADDR(ofs) != SECTOR_ADDR(c->wbuf_ofs)) 935894214d1SArtem B. Bityuckiy goto exit; 9361da177e4SLinus Torvalds 9371da177e4SLinus Torvalds if (ofs >= c->wbuf_ofs) { 9381da177e4SLinus Torvalds owbf = (ofs - c->wbuf_ofs); /* offset in write buffer */ 9391da177e4SLinus Torvalds if (owbf > c->wbuf_len) /* is read beyond write buffer ? */ 9401da177e4SLinus Torvalds goto exit; 9411da177e4SLinus Torvalds lwbf = c->wbuf_len - owbf; /* number of bytes to copy */ 9421da177e4SLinus Torvalds if (lwbf > len) 9431da177e4SLinus Torvalds lwbf = len; 9441da177e4SLinus Torvalds } else { 9451da177e4SLinus Torvalds orbf = (c->wbuf_ofs - ofs); /* offset in read buffer */ 9461da177e4SLinus Torvalds if (orbf > len) /* is write beyond write buffer ? */ 9471da177e4SLinus Torvalds goto exit; 9481da177e4SLinus Torvalds lwbf = len - orbf; /* number of bytes to copy */ 9491da177e4SLinus Torvalds if (lwbf > c->wbuf_len) 9501da177e4SLinus Torvalds lwbf = c->wbuf_len; 9511da177e4SLinus Torvalds } 9521da177e4SLinus Torvalds if (lwbf > 0) 9531da177e4SLinus Torvalds memcpy(buf+orbf,c->wbuf+owbf,lwbf); 9541da177e4SLinus Torvalds 9551da177e4SLinus Torvalds exit: 9561da177e4SLinus Torvalds up_read(&c->wbuf_sem); 9571da177e4SLinus Torvalds return ret; 9581da177e4SLinus Torvalds } 9591da177e4SLinus Torvalds 9608593fbc6SThomas Gleixner #define NR_OOB_SCAN_PAGES 4 9618593fbc6SThomas Gleixner 9621da177e4SLinus Torvalds /* 9631da177e4SLinus Torvalds * Check, if the out of band area is empty 9641da177e4SLinus Torvalds */ 9658593fbc6SThomas Gleixner int jffs2_check_oob_empty(struct jffs2_sb_info *c, 9668593fbc6SThomas Gleixner struct jffs2_eraseblock *jeb, int mode) 9671da177e4SLinus Torvalds { 9688593fbc6SThomas Gleixner int i, page, ret; 9698593fbc6SThomas Gleixner int oobsize = c->mtd->oobsize; 9708593fbc6SThomas Gleixner struct mtd_oob_ops ops; 9711da177e4SLinus Torvalds 9728593fbc6SThomas Gleixner ops.len = NR_OOB_SCAN_PAGES * oobsize; 9738593fbc6SThomas Gleixner ops.ooblen = oobsize; 9748593fbc6SThomas Gleixner ops.oobbuf = c->oobbuf; 9758593fbc6SThomas Gleixner ops.ooboffs = 0; 9768593fbc6SThomas Gleixner ops.datbuf = NULL; 9778593fbc6SThomas Gleixner ops.mode = MTD_OOB_PLACE; 9788593fbc6SThomas Gleixner 9798593fbc6SThomas Gleixner ret = c->mtd->read_oob(c->mtd, jeb->offset, &ops); 9801da177e4SLinus Torvalds if (ret) { 9818593fbc6SThomas Gleixner D1(printk(KERN_WARNING "jffs2_check_oob_empty(): Read OOB " 9828593fbc6SThomas Gleixner "failed %d for block at %08x\n", ret, jeb->offset)); 9838593fbc6SThomas Gleixner return ret; 9841da177e4SLinus Torvalds } 9851da177e4SLinus Torvalds 9868593fbc6SThomas Gleixner if (ops.retlen < ops.len) { 9878593fbc6SThomas Gleixner D1(printk(KERN_WARNING "jffs2_check_oob_empty(): Read OOB " 9888593fbc6SThomas Gleixner "returned short read (%zd bytes not %d) for block " 9898593fbc6SThomas Gleixner "at %08x\n", ops.retlen, ops.len, jeb->offset)); 9908593fbc6SThomas Gleixner return -EIO; 9911da177e4SLinus Torvalds } 9921da177e4SLinus Torvalds 9931da177e4SLinus Torvalds /* Special check for first page */ 9948593fbc6SThomas Gleixner for(i = 0; i < oobsize ; i++) { 9951da177e4SLinus Torvalds /* Yeah, we know about the cleanmarker. */ 9961da177e4SLinus Torvalds if (mode && i >= c->fsdata_pos && 9971da177e4SLinus Torvalds i < c->fsdata_pos + c->fsdata_len) 9981da177e4SLinus Torvalds continue; 9991da177e4SLinus Torvalds 10008593fbc6SThomas Gleixner if (ops.oobbuf[i] != 0xFF) { 10018593fbc6SThomas Gleixner D2(printk(KERN_DEBUG "Found %02x at %x in OOB for " 10028593fbc6SThomas Gleixner "%08x\n", ops.oobbuf[i], i, jeb->offset)); 10038593fbc6SThomas Gleixner return 1; 10041da177e4SLinus Torvalds } 10051da177e4SLinus Torvalds } 10061da177e4SLinus Torvalds 10071da177e4SLinus Torvalds /* we know, we are aligned :) */ 10088593fbc6SThomas Gleixner for (page = oobsize; page < ops.len; page += sizeof(long)) { 10098593fbc6SThomas Gleixner long dat = *(long *)(&ops.oobbuf[page]); 10108593fbc6SThomas Gleixner if(dat != -1) 10118593fbc6SThomas Gleixner return 1; 10121da177e4SLinus Torvalds } 10138593fbc6SThomas Gleixner return 0; 10141da177e4SLinus Torvalds } 10151da177e4SLinus Torvalds 10161da177e4SLinus Torvalds /* 10171da177e4SLinus Torvalds * Scan for a valid cleanmarker and for bad blocks 10181da177e4SLinus Torvalds */ 10198593fbc6SThomas Gleixner int jffs2_check_nand_cleanmarker (struct jffs2_sb_info *c, 10208593fbc6SThomas Gleixner struct jffs2_eraseblock *jeb) 10211da177e4SLinus Torvalds { 10221da177e4SLinus Torvalds struct jffs2_unknown_node n; 10238593fbc6SThomas Gleixner struct mtd_oob_ops ops; 10248593fbc6SThomas Gleixner int oobsize = c->mtd->oobsize; 10258593fbc6SThomas Gleixner unsigned char *p,*b; 10268593fbc6SThomas Gleixner int i, ret; 10278593fbc6SThomas Gleixner size_t offset = jeb->offset; 10281da177e4SLinus Torvalds 10291da177e4SLinus Torvalds /* Check first if the block is bad. */ 10301da177e4SLinus Torvalds if (c->mtd->block_isbad(c->mtd, offset)) { 10318593fbc6SThomas Gleixner D1 (printk(KERN_WARNING "jffs2_check_nand_cleanmarker()" 10328593fbc6SThomas Gleixner ": Bad block at %08x\n", jeb->offset)); 10331da177e4SLinus Torvalds return 2; 10341da177e4SLinus Torvalds } 10351da177e4SLinus Torvalds 10368593fbc6SThomas Gleixner ops.len = oobsize; 10378593fbc6SThomas Gleixner ops.ooblen = oobsize; 10388593fbc6SThomas Gleixner ops.oobbuf = c->oobbuf; 10398593fbc6SThomas Gleixner ops.ooboffs = 0; 10408593fbc6SThomas Gleixner ops.datbuf = NULL; 10418593fbc6SThomas Gleixner ops.mode = MTD_OOB_PLACE; 10428593fbc6SThomas Gleixner 10438593fbc6SThomas Gleixner ret = c->mtd->read_oob(c->mtd, offset, &ops); 10441da177e4SLinus Torvalds if (ret) { 10458593fbc6SThomas Gleixner D1 (printk(KERN_WARNING "jffs2_check_nand_cleanmarker(): " 10468593fbc6SThomas Gleixner "Read OOB failed %d for block at %08x\n", 10478593fbc6SThomas Gleixner ret, jeb->offset)); 10481da177e4SLinus Torvalds return ret; 10491da177e4SLinus Torvalds } 10508593fbc6SThomas Gleixner 10518593fbc6SThomas Gleixner if (ops.retlen < ops.len) { 10528593fbc6SThomas Gleixner D1 (printk (KERN_WARNING "jffs2_check_nand_cleanmarker(): " 10538593fbc6SThomas Gleixner "Read OOB return short read (%zd bytes not %d) " 10548593fbc6SThomas Gleixner "for block at %08x\n", ops.retlen, ops.len, 10558593fbc6SThomas Gleixner jeb->offset)); 10561da177e4SLinus Torvalds return -EIO; 10571da177e4SLinus Torvalds } 10581da177e4SLinus Torvalds 10591da177e4SLinus Torvalds n.magic = cpu_to_je16 (JFFS2_MAGIC_BITMASK); 10601da177e4SLinus Torvalds n.nodetype = cpu_to_je16 (JFFS2_NODETYPE_CLEANMARKER); 10611da177e4SLinus Torvalds n.totlen = cpu_to_je32 (8); 10621da177e4SLinus Torvalds p = (unsigned char *) &n; 10638593fbc6SThomas Gleixner b = c->oobbuf + c->fsdata_pos; 10641da177e4SLinus Torvalds 10658593fbc6SThomas Gleixner for (i = c->fsdata_len; i; i--) { 10668593fbc6SThomas Gleixner if (*b++ != *p++) 10678593fbc6SThomas Gleixner ret = 1; 10681da177e4SLinus Torvalds } 10698593fbc6SThomas Gleixner 10708593fbc6SThomas Gleixner D1(if (ret == 1) { 10718593fbc6SThomas Gleixner printk(KERN_WARNING "jffs2_check_nand_cleanmarker(): " 10728593fbc6SThomas Gleixner "Cleanmarker node not detected in block at %08x\n", 10738593fbc6SThomas Gleixner offset); 10749bfeb691SDavid Woodhouse printk(KERN_WARNING "OOB at %08zx was ", offset); 10758593fbc6SThomas Gleixner for (i=0; i < oobsize; i++) 10768593fbc6SThomas Gleixner printk("%02x ", c->oobbuf[i]); 10771da177e4SLinus Torvalds printk("\n"); 10788593fbc6SThomas Gleixner }); 10798593fbc6SThomas Gleixner return ret; 10801da177e4SLinus Torvalds } 10811da177e4SLinus Torvalds 10828593fbc6SThomas Gleixner int jffs2_write_nand_cleanmarker(struct jffs2_sb_info *c, 10838593fbc6SThomas Gleixner struct jffs2_eraseblock *jeb) 10841da177e4SLinus Torvalds { 10851da177e4SLinus Torvalds struct jffs2_unknown_node n; 10861da177e4SLinus Torvalds int ret; 10878593fbc6SThomas Gleixner struct mtd_oob_ops ops; 10881da177e4SLinus Torvalds 10891da177e4SLinus Torvalds n.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); 10901da177e4SLinus Torvalds n.nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER); 10911da177e4SLinus Torvalds n.totlen = cpu_to_je32(8); 10921da177e4SLinus Torvalds 10938593fbc6SThomas Gleixner ops.len = c->fsdata_len; 10948593fbc6SThomas Gleixner ops.ooblen = c->fsdata_len;; 10958593fbc6SThomas Gleixner ops.oobbuf = (uint8_t *)&n; 10968593fbc6SThomas Gleixner ops.ooboffs = c->fsdata_pos; 10978593fbc6SThomas Gleixner ops.datbuf = NULL; 10988593fbc6SThomas Gleixner ops.mode = MTD_OOB_PLACE; 10998593fbc6SThomas Gleixner 11008593fbc6SThomas Gleixner ret = c->mtd->write_oob(c->mtd, jeb->offset, &ops); 11011da177e4SLinus Torvalds 11021da177e4SLinus Torvalds if (ret) { 11038593fbc6SThomas Gleixner D1(printk(KERN_WARNING "jffs2_write_nand_cleanmarker(): " 11048593fbc6SThomas Gleixner "Write failed for block at %08x: error %d\n", 11058593fbc6SThomas Gleixner jeb->offset, ret)); 11061da177e4SLinus Torvalds return ret; 11071da177e4SLinus Torvalds } 11088593fbc6SThomas Gleixner if (ops.retlen != ops.len) { 11098593fbc6SThomas Gleixner D1(printk(KERN_WARNING "jffs2_write_nand_cleanmarker(): " 11108593fbc6SThomas Gleixner "Short write for block at %08x: %zd not %d\n", 11118593fbc6SThomas Gleixner jeb->offset, ops.retlen, ops.len)); 11128593fbc6SThomas Gleixner return -EIO; 11131da177e4SLinus Torvalds } 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 11191da177e4SLinus Torvalds * than MAX_ERASE_FAILURES we mark it finaly 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 11361da177e4SLinus Torvalds D1(printk(KERN_WARNING "jffs2_write_nand_badblock(): Marking bad block at %08x\n", bad_offset)); 11371da177e4SLinus Torvalds ret = c->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 11461da177e4SLinus Torvalds static int jffs2_nand_set_oobinfo(struct jffs2_sb_info *c) 11471da177e4SLinus Torvalds { 11485bd34c09SThomas Gleixner struct nand_ecclayout *oinfo = c->mtd->ecclayout; 11491da177e4SLinus Torvalds 11501da177e4SLinus Torvalds /* Do this only, if we have an oob buffer */ 11511da177e4SLinus Torvalds if (!c->mtd->oobsize) 11521da177e4SLinus Torvalds return 0; 11531da177e4SLinus Torvalds 11541da177e4SLinus Torvalds /* Cleanmarker is out-of-band, so inline size zero */ 11551da177e4SLinus Torvalds c->cleanmarker_size = 0; 11561da177e4SLinus Torvalds 11571da177e4SLinus Torvalds /* Should we use autoplacement ? */ 11585bd34c09SThomas Gleixner if (!oinfo) { 11591da177e4SLinus Torvalds D1(printk(KERN_DEBUG "JFFS2 on NAND. No autoplacment info found\n")); 11601da177e4SLinus Torvalds return -EINVAL; 11611da177e4SLinus Torvalds } 11625bd34c09SThomas Gleixner 11635bd34c09SThomas Gleixner D1(printk(KERN_DEBUG "JFFS2 using autoplace on NAND\n")); 11645bd34c09SThomas Gleixner /* Get the position of the free bytes */ 11655bd34c09SThomas Gleixner if (!oinfo->oobfree[0].length) { 11665bd34c09SThomas Gleixner printk (KERN_WARNING "jffs2_nand_set_oobinfo(): Eeep." 11675bd34c09SThomas Gleixner " Autoplacement selected and no empty space in oob\n"); 11685bd34c09SThomas Gleixner return -ENOSPC; 11691da177e4SLinus Torvalds } 11705bd34c09SThomas Gleixner c->fsdata_pos = oinfo->oobfree[0].offset; 11715bd34c09SThomas Gleixner c->fsdata_len = oinfo->oobfree[0].length; 11725bd34c09SThomas Gleixner if (c->fsdata_len > 8) 11735bd34c09SThomas Gleixner c->fsdata_len = 8; 11745bd34c09SThomas Gleixner 11751da177e4SLinus Torvalds return 0; 11761da177e4SLinus Torvalds } 11771da177e4SLinus Torvalds 11781da177e4SLinus Torvalds int jffs2_nand_flash_setup(struct jffs2_sb_info *c) 11791da177e4SLinus Torvalds { 11801da177e4SLinus Torvalds int res; 11811da177e4SLinus Torvalds 11821da177e4SLinus Torvalds /* Initialise write buffer */ 11831da177e4SLinus Torvalds init_rwsem(&c->wbuf_sem); 118428318776SJoern Engel c->wbuf_pagesize = c->mtd->writesize; 11851da177e4SLinus Torvalds c->wbuf_ofs = 0xFFFFFFFF; 11861da177e4SLinus Torvalds 11871da177e4SLinus Torvalds c->wbuf = kmalloc(c->wbuf_pagesize, GFP_KERNEL); 11881da177e4SLinus Torvalds if (!c->wbuf) 11891da177e4SLinus Torvalds return -ENOMEM; 11901da177e4SLinus Torvalds 11918593fbc6SThomas Gleixner c->oobbuf = kmalloc(NR_OOB_SCAN_PAGES * c->mtd->oobsize, GFP_KERNEL); 11928593fbc6SThomas Gleixner if (!c->oobbuf) 11938593fbc6SThomas Gleixner return -ENOMEM; 11948593fbc6SThomas Gleixner 11951da177e4SLinus Torvalds res = jffs2_nand_set_oobinfo(c); 11961da177e4SLinus Torvalds 11971da177e4SLinus Torvalds #ifdef BREAKME 11981da177e4SLinus Torvalds if (!brokenbuf) 11991da177e4SLinus Torvalds brokenbuf = kmalloc(c->wbuf_pagesize, GFP_KERNEL); 12001da177e4SLinus Torvalds if (!brokenbuf) { 12011da177e4SLinus Torvalds kfree(c->wbuf); 12021da177e4SLinus Torvalds return -ENOMEM; 12031da177e4SLinus Torvalds } 12041da177e4SLinus Torvalds memset(brokenbuf, 0xdb, c->wbuf_pagesize); 12051da177e4SLinus Torvalds #endif 12061da177e4SLinus Torvalds return res; 12071da177e4SLinus Torvalds } 12081da177e4SLinus Torvalds 12091da177e4SLinus Torvalds void jffs2_nand_flash_cleanup(struct jffs2_sb_info *c) 12101da177e4SLinus Torvalds { 12111da177e4SLinus Torvalds kfree(c->wbuf); 12128593fbc6SThomas Gleixner kfree(c->oobbuf); 12131da177e4SLinus Torvalds } 12141da177e4SLinus Torvalds 12158f15fd55SAndrew Victor int jffs2_dataflash_setup(struct jffs2_sb_info *c) { 12168f15fd55SAndrew Victor c->cleanmarker_size = 0; /* No cleanmarkers needed */ 12178f15fd55SAndrew Victor 12188f15fd55SAndrew Victor /* Initialize write buffer */ 12198f15fd55SAndrew Victor init_rwsem(&c->wbuf_sem); 12208f15fd55SAndrew Victor 1221daba5cc4SArtem B. Bityutskiy 1222daba5cc4SArtem B. Bityutskiy c->wbuf_pagesize = c->mtd->erasesize; 1223daba5cc4SArtem B. Bityutskiy 1224daba5cc4SArtem B. Bityutskiy /* Find a suitable c->sector_size 1225daba5cc4SArtem B. Bityutskiy * - Not too much sectors 1226daba5cc4SArtem B. Bityutskiy * - Sectors have to be at least 4 K + some bytes 1227daba5cc4SArtem B. Bityutskiy * - All known dataflashes have erase sizes of 528 or 1056 1228daba5cc4SArtem B. Bityutskiy * - we take at least 8 eraseblocks and want to have at least 8K size 1229daba5cc4SArtem B. Bityutskiy * - The concatenation should be a power of 2 1230daba5cc4SArtem B. Bityutskiy */ 1231daba5cc4SArtem B. Bityutskiy 1232daba5cc4SArtem B. Bityutskiy c->sector_size = 8 * c->mtd->erasesize; 1233daba5cc4SArtem B. Bityutskiy 1234daba5cc4SArtem B. Bityutskiy while (c->sector_size < 8192) { 1235daba5cc4SArtem B. Bityutskiy c->sector_size *= 2; 1236daba5cc4SArtem B. Bityutskiy } 1237daba5cc4SArtem B. Bityutskiy 1238daba5cc4SArtem B. Bityutskiy /* It may be necessary to adjust the flash size */ 1239daba5cc4SArtem B. Bityutskiy c->flash_size = c->mtd->size; 1240daba5cc4SArtem B. Bityutskiy 1241daba5cc4SArtem B. Bityutskiy if ((c->flash_size % c->sector_size) != 0) { 1242daba5cc4SArtem B. Bityutskiy c->flash_size = (c->flash_size / c->sector_size) * c->sector_size; 1243daba5cc4SArtem B. Bityutskiy printk(KERN_WARNING "JFFS2 flash size adjusted to %dKiB\n", c->flash_size); 1244daba5cc4SArtem B. Bityutskiy }; 1245daba5cc4SArtem B. Bityutskiy 1246daba5cc4SArtem B. Bityutskiy c->wbuf_ofs = 0xFFFFFFFF; 12478f15fd55SAndrew Victor c->wbuf = kmalloc(c->wbuf_pagesize, GFP_KERNEL); 12488f15fd55SAndrew Victor if (!c->wbuf) 12498f15fd55SAndrew Victor return -ENOMEM; 12508f15fd55SAndrew Victor 1251daba5cc4SArtem B. Bityutskiy printk(KERN_INFO "JFFS2 write-buffering enabled buffer (%d) erasesize (%d)\n", c->wbuf_pagesize, c->sector_size); 12528f15fd55SAndrew Victor 12538f15fd55SAndrew Victor return 0; 12548f15fd55SAndrew Victor } 12558f15fd55SAndrew Victor 12568f15fd55SAndrew Victor void jffs2_dataflash_cleanup(struct jffs2_sb_info *c) { 12578f15fd55SAndrew Victor kfree(c->wbuf); 12588f15fd55SAndrew Victor } 12598f15fd55SAndrew Victor 126059da721aSNicolas Pitre int jffs2_nor_wbuf_flash_setup(struct jffs2_sb_info *c) { 1261c8b229deSJoern Engel /* Cleanmarker currently occupies whole programming regions, 1262c8b229deSJoern Engel * either one or 2 for 8Byte STMicro flashes. */ 1263c8b229deSJoern Engel c->cleanmarker_size = max(16u, c->mtd->writesize); 126459da721aSNicolas Pitre 126559da721aSNicolas Pitre /* Initialize write buffer */ 126659da721aSNicolas Pitre init_rwsem(&c->wbuf_sem); 126728318776SJoern Engel c->wbuf_pagesize = c->mtd->writesize; 126859da721aSNicolas Pitre c->wbuf_ofs = 0xFFFFFFFF; 126959da721aSNicolas Pitre 127059da721aSNicolas Pitre c->wbuf = kmalloc(c->wbuf_pagesize, GFP_KERNEL); 127159da721aSNicolas Pitre if (!c->wbuf) 127259da721aSNicolas Pitre return -ENOMEM; 127359da721aSNicolas Pitre 127459da721aSNicolas Pitre return 0; 127559da721aSNicolas Pitre } 127659da721aSNicolas Pitre 127759da721aSNicolas Pitre void jffs2_nor_wbuf_flash_cleanup(struct jffs2_sb_info *c) { 127859da721aSNicolas Pitre kfree(c->wbuf); 127959da721aSNicolas Pitre } 1280