11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * JFFS2 -- Journalling Flash File System, Version 2. 31da177e4SLinus Torvalds * 4c00c310eSDavid Woodhouse * Copyright © 2001-2007 Red Hat, Inc. 56088c058SDavid Woodhouse * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org> 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Created by David Woodhouse <dwmw2@infradead.org> 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * For licensing information, see the file 'LICENCE' in this directory. 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds */ 121da177e4SLinus Torvalds 131da177e4SLinus Torvalds #include <linux/kernel.h> 141da177e4SLinus Torvalds #include <linux/slab.h> 151da177e4SLinus Torvalds #include <linux/mtd/mtd.h> 161da177e4SLinus Torvalds #include <linux/compiler.h> 171da177e4SLinus Torvalds #include <linux/crc32.h> 181da177e4SLinus Torvalds #include <linux/sched.h> 191da177e4SLinus Torvalds #include <linux/pagemap.h> 201da177e4SLinus Torvalds #include "nodelist.h" 211da177e4SLinus Torvalds 221da177e4SLinus Torvalds struct erase_priv_struct { 231da177e4SLinus Torvalds struct jffs2_eraseblock *jeb; 241da177e4SLinus Torvalds struct jffs2_sb_info *c; 251da177e4SLinus Torvalds }; 261da177e4SLinus Torvalds 271da177e4SLinus Torvalds #ifndef __ECOS 281da177e4SLinus Torvalds static void jffs2_erase_callback(struct erase_info *); 291da177e4SLinus Torvalds #endif 301da177e4SLinus Torvalds static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset); 311da177e4SLinus Torvalds static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb); 321da177e4SLinus Torvalds static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb); 331da177e4SLinus Torvalds 341da177e4SLinus Torvalds static void jffs2_erase_block(struct jffs2_sb_info *c, 351da177e4SLinus Torvalds struct jffs2_eraseblock *jeb) 361da177e4SLinus Torvalds { 371da177e4SLinus Torvalds int ret; 381da177e4SLinus Torvalds uint32_t bad_offset; 391da177e4SLinus Torvalds #ifdef __ECOS 401da177e4SLinus Torvalds ret = jffs2_flash_erase(c, jeb); 411da177e4SLinus Torvalds if (!ret) { 421da177e4SLinus Torvalds jffs2_erase_succeeded(c, jeb); 431da177e4SLinus Torvalds return; 441da177e4SLinus Torvalds } 451da177e4SLinus Torvalds bad_offset = jeb->offset; 461da177e4SLinus Torvalds #else /* Linux */ 471da177e4SLinus Torvalds struct erase_info *instr; 481da177e4SLinus Torvalds 49e0c8e42fSArtem B. Bityutskiy D1(printk(KERN_DEBUG "jffs2_erase_block(): erase block %#08x (range %#08x-%#08x)\n", 50e0c8e42fSArtem B. Bityutskiy jeb->offset, jeb->offset, jeb->offset + c->sector_size)); 511da177e4SLinus Torvalds instr = kmalloc(sizeof(struct erase_info) + sizeof(struct erase_priv_struct), GFP_KERNEL); 521da177e4SLinus Torvalds if (!instr) { 531da177e4SLinus Torvalds printk(KERN_WARNING "kmalloc for struct erase_info in jffs2_erase_block failed. Refiling block for later\n"); 54ced22070SDavid Woodhouse mutex_lock(&c->erase_free_sem); 551da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 56f116629dSAkinobu Mita list_move(&jeb->list, &c->erase_pending_list); 571da177e4SLinus Torvalds c->erasing_size -= c->sector_size; 581da177e4SLinus Torvalds c->dirty_size += c->sector_size; 591da177e4SLinus Torvalds jeb->dirty_size = c->sector_size; 601da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 61ced22070SDavid Woodhouse mutex_unlock(&c->erase_free_sem); 621da177e4SLinus Torvalds return; 631da177e4SLinus Torvalds } 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds memset(instr, 0, sizeof(*instr)); 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds instr->mtd = c->mtd; 681da177e4SLinus Torvalds instr->addr = jeb->offset; 691da177e4SLinus Torvalds instr->len = c->sector_size; 701da177e4SLinus Torvalds instr->callback = jffs2_erase_callback; 711da177e4SLinus Torvalds instr->priv = (unsigned long)(&instr[1]); 72bb0eb217SAdrian Hunter instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN; 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds ((struct erase_priv_struct *)instr->priv)->jeb = jeb; 751da177e4SLinus Torvalds ((struct erase_priv_struct *)instr->priv)->c = c; 761da177e4SLinus Torvalds 777e1f0dc0SArtem Bityutskiy ret = mtd_erase(c->mtd, instr); 781da177e4SLinus Torvalds if (!ret) 791da177e4SLinus Torvalds return; 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds bad_offset = instr->fail_addr; 821da177e4SLinus Torvalds kfree(instr); 831da177e4SLinus Torvalds #endif /* __ECOS */ 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds if (ret == -ENOMEM || ret == -EAGAIN) { 861da177e4SLinus Torvalds /* Erase failed immediately. Refile it on the list */ 871da177e4SLinus Torvalds D1(printk(KERN_DEBUG "Erase at 0x%08x failed: %d. Refiling on erase_pending_list\n", jeb->offset, ret)); 88ced22070SDavid Woodhouse mutex_lock(&c->erase_free_sem); 891da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 90f116629dSAkinobu Mita list_move(&jeb->list, &c->erase_pending_list); 911da177e4SLinus Torvalds c->erasing_size -= c->sector_size; 921da177e4SLinus Torvalds c->dirty_size += c->sector_size; 931da177e4SLinus Torvalds jeb->dirty_size = c->sector_size; 941da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 95ced22070SDavid Woodhouse mutex_unlock(&c->erase_free_sem); 961da177e4SLinus Torvalds return; 971da177e4SLinus Torvalds } 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds if (ret == -EROFS) 1001da177e4SLinus Torvalds printk(KERN_WARNING "Erase at 0x%08x failed immediately: -EROFS. Is the sector locked?\n", jeb->offset); 1011da177e4SLinus Torvalds else 1021da177e4SLinus Torvalds printk(KERN_WARNING "Erase at 0x%08x failed immediately: errno %d\n", jeb->offset, ret); 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds jffs2_erase_failed(c, jeb, bad_offset); 1051da177e4SLinus Torvalds } 1061da177e4SLinus Torvalds 1079957abeaSJoakim Tjernlund int jffs2_erase_pending_blocks(struct jffs2_sb_info *c, int count) 1081da177e4SLinus Torvalds { 1091da177e4SLinus Torvalds struct jffs2_eraseblock *jeb; 1109957abeaSJoakim Tjernlund int work_done = 0; 1111da177e4SLinus Torvalds 112ced22070SDavid Woodhouse mutex_lock(&c->erase_free_sem); 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds while (!list_empty(&c->erase_complete_list) || 1171da177e4SLinus Torvalds !list_empty(&c->erase_pending_list)) { 1181da177e4SLinus Torvalds 1191da177e4SLinus Torvalds if (!list_empty(&c->erase_complete_list)) { 1201da177e4SLinus Torvalds jeb = list_entry(c->erase_complete_list.next, struct jffs2_eraseblock, list); 121e2bc322bSDavid Woodhouse list_move(&jeb->list, &c->erase_checking_list); 1221da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 123ced22070SDavid Woodhouse mutex_unlock(&c->erase_free_sem); 1241da177e4SLinus Torvalds jffs2_mark_erased_block(c, jeb); 1251da177e4SLinus Torvalds 1269957abeaSJoakim Tjernlund work_done++; 1271da177e4SLinus Torvalds if (!--count) { 1281da177e4SLinus Torvalds D1(printk(KERN_DEBUG "Count reached. jffs2_erase_pending_blocks leaving\n")); 1291da177e4SLinus Torvalds goto done; 1301da177e4SLinus Torvalds } 1311da177e4SLinus Torvalds 1321da177e4SLinus Torvalds } else if (!list_empty(&c->erase_pending_list)) { 1331da177e4SLinus Torvalds jeb = list_entry(c->erase_pending_list.next, struct jffs2_eraseblock, list); 1341da177e4SLinus Torvalds D1(printk(KERN_DEBUG "Starting erase of pending block 0x%08x\n", jeb->offset)); 1351da177e4SLinus Torvalds list_del(&jeb->list); 1361da177e4SLinus Torvalds c->erasing_size += c->sector_size; 1371da177e4SLinus Torvalds c->wasted_size -= jeb->wasted_size; 1381da177e4SLinus Torvalds c->free_size -= jeb->free_size; 1391da177e4SLinus Torvalds c->used_size -= jeb->used_size; 1401da177e4SLinus Torvalds c->dirty_size -= jeb->dirty_size; 1411da177e4SLinus Torvalds jeb->wasted_size = jeb->used_size = jeb->dirty_size = jeb->free_size = 0; 142c38c1b61SDavid Woodhouse jffs2_free_jeb_node_refs(c, jeb); 1431da177e4SLinus Torvalds list_add(&jeb->list, &c->erasing_list); 1441da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 145ced22070SDavid Woodhouse mutex_unlock(&c->erase_free_sem); 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds jffs2_erase_block(c, jeb); 1481da177e4SLinus Torvalds 1491da177e4SLinus Torvalds } else { 1501da177e4SLinus Torvalds BUG(); 1511da177e4SLinus Torvalds } 1521da177e4SLinus Torvalds 1531da177e4SLinus Torvalds /* Be nice */ 1543866f673SWolfram Sang cond_resched(); 155ced22070SDavid Woodhouse mutex_lock(&c->erase_free_sem); 1561da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 1571da177e4SLinus Torvalds } 1581da177e4SLinus Torvalds 1591da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 160ced22070SDavid Woodhouse mutex_unlock(&c->erase_free_sem); 1611da177e4SLinus Torvalds done: 1621da177e4SLinus Torvalds D1(printk(KERN_DEBUG "jffs2_erase_pending_blocks completed\n")); 1639957abeaSJoakim Tjernlund return work_done; 1641da177e4SLinus Torvalds } 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb) 1671da177e4SLinus Torvalds { 1681da177e4SLinus Torvalds D1(printk(KERN_DEBUG "Erase completed successfully at 0x%08x\n", jeb->offset)); 169ced22070SDavid Woodhouse mutex_lock(&c->erase_free_sem); 1701da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 171f116629dSAkinobu Mita list_move_tail(&jeb->list, &c->erase_complete_list); 172ae3b6ba0SDavid Woodhouse /* Wake the GC thread to mark them clean */ 173ae3b6ba0SDavid Woodhouse jffs2_garbage_collect_trigger(c); 1741da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 175ced22070SDavid Woodhouse mutex_unlock(&c->erase_free_sem); 1760717bf84SDavid Woodhouse wake_up(&c->erase_wait); 1771da177e4SLinus Torvalds } 1781da177e4SLinus Torvalds 1791da177e4SLinus Torvalds static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset) 1801da177e4SLinus Torvalds { 1811da177e4SLinus Torvalds /* For NAND, if the failure did not occur at the device level for a 1821da177e4SLinus Torvalds specific physical page, don't bother updating the bad block table. */ 18369423d99SAdrian Hunter if (jffs2_cleanmarker_oob(c) && (bad_offset != (uint32_t)MTD_FAIL_ADDR_UNKNOWN)) { 1841da177e4SLinus Torvalds /* We had a device-level failure to erase. Let's see if we've 1851da177e4SLinus Torvalds failed too many times. */ 1861da177e4SLinus Torvalds if (!jffs2_write_nand_badblock(c, jeb, bad_offset)) { 1871da177e4SLinus Torvalds /* We'd like to give this block another try. */ 188ced22070SDavid Woodhouse mutex_lock(&c->erase_free_sem); 1891da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 190f116629dSAkinobu Mita list_move(&jeb->list, &c->erase_pending_list); 1911da177e4SLinus Torvalds c->erasing_size -= c->sector_size; 1921da177e4SLinus Torvalds c->dirty_size += c->sector_size; 1931da177e4SLinus Torvalds jeb->dirty_size = c->sector_size; 1941da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 195ced22070SDavid Woodhouse mutex_unlock(&c->erase_free_sem); 1961da177e4SLinus Torvalds return; 1971da177e4SLinus Torvalds } 1981da177e4SLinus Torvalds } 1991da177e4SLinus Torvalds 200ced22070SDavid Woodhouse mutex_lock(&c->erase_free_sem); 2011da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 2021da177e4SLinus Torvalds c->erasing_size -= c->sector_size; 2031da177e4SLinus Torvalds c->bad_size += c->sector_size; 204f116629dSAkinobu Mita list_move(&jeb->list, &c->bad_list); 2051da177e4SLinus Torvalds c->nr_erasing_blocks--; 2061da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 207ced22070SDavid Woodhouse mutex_unlock(&c->erase_free_sem); 2081da177e4SLinus Torvalds wake_up(&c->erase_wait); 2091da177e4SLinus Torvalds } 2101da177e4SLinus Torvalds 2111da177e4SLinus Torvalds #ifndef __ECOS 2121da177e4SLinus Torvalds static void jffs2_erase_callback(struct erase_info *instr) 2131da177e4SLinus Torvalds { 2141da177e4SLinus Torvalds struct erase_priv_struct *priv = (void *)instr->priv; 2151da177e4SLinus Torvalds 2161da177e4SLinus Torvalds if(instr->state != MTD_ERASE_DONE) { 21769423d99SAdrian Hunter printk(KERN_WARNING "Erase at 0x%08llx finished, but state != MTD_ERASE_DONE. State is 0x%x instead.\n", 21869423d99SAdrian Hunter (unsigned long long)instr->addr, instr->state); 2191da177e4SLinus Torvalds jffs2_erase_failed(priv->c, priv->jeb, instr->fail_addr); 2201da177e4SLinus Torvalds } else { 2211da177e4SLinus Torvalds jffs2_erase_succeeded(priv->c, priv->jeb); 2221da177e4SLinus Torvalds } 2231da177e4SLinus Torvalds kfree(instr); 2241da177e4SLinus Torvalds } 2251da177e4SLinus Torvalds #endif /* !__ECOS */ 2261da177e4SLinus Torvalds 2271da177e4SLinus Torvalds /* Hmmm. Maybe we should accept the extra space it takes and make 2281da177e4SLinus Torvalds this a standard doubly-linked list? */ 2291da177e4SLinus Torvalds static inline void jffs2_remove_node_refs_from_ino_list(struct jffs2_sb_info *c, 2301da177e4SLinus Torvalds struct jffs2_raw_node_ref *ref, struct jffs2_eraseblock *jeb) 2311da177e4SLinus Torvalds { 2321da177e4SLinus Torvalds struct jffs2_inode_cache *ic = NULL; 2331da177e4SLinus Torvalds struct jffs2_raw_node_ref **prev; 2341da177e4SLinus Torvalds 2351da177e4SLinus Torvalds prev = &ref->next_in_ino; 2361da177e4SLinus Torvalds 2371da177e4SLinus Torvalds /* Walk the inode's list once, removing any nodes from this eraseblock */ 2381da177e4SLinus Torvalds while (1) { 2391da177e4SLinus Torvalds if (!(*prev)->next_in_ino) { 2401da177e4SLinus Torvalds /* We're looking at the jffs2_inode_cache, which is 2411da177e4SLinus Torvalds at the end of the linked list. Stash it and continue 2421da177e4SLinus Torvalds from the beginning of the list */ 2431da177e4SLinus Torvalds ic = (struct jffs2_inode_cache *)(*prev); 2441da177e4SLinus Torvalds prev = &ic->nodes; 2451da177e4SLinus Torvalds continue; 2461da177e4SLinus Torvalds } 2471da177e4SLinus Torvalds 2483be36675SAndrew Victor if (SECTOR_ADDR((*prev)->flash_offset) == jeb->offset) { 2491da177e4SLinus Torvalds /* It's in the block we're erasing */ 2501da177e4SLinus Torvalds struct jffs2_raw_node_ref *this; 2511da177e4SLinus Torvalds 2521da177e4SLinus Torvalds this = *prev; 2531da177e4SLinus Torvalds *prev = this->next_in_ino; 2541da177e4SLinus Torvalds this->next_in_ino = NULL; 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds if (this == ref) 2571da177e4SLinus Torvalds break; 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds continue; 2601da177e4SLinus Torvalds } 2611da177e4SLinus Torvalds /* Not to be deleted. Skip */ 2621da177e4SLinus Torvalds prev = &((*prev)->next_in_ino); 2631da177e4SLinus Torvalds } 2641da177e4SLinus Torvalds 2651da177e4SLinus Torvalds /* PARANOIA */ 2661da177e4SLinus Torvalds if (!ic) { 267c9f700f8SKaiGai Kohei JFFS2_WARNING("inode_cache/xattr_datum/xattr_ref" 268c9f700f8SKaiGai Kohei " not found in remove_node_refs()!!\n"); 2691da177e4SLinus Torvalds return; 2701da177e4SLinus Torvalds } 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds D1(printk(KERN_DEBUG "Removed nodes in range 0x%08x-0x%08x from ino #%u\n", 2731da177e4SLinus Torvalds jeb->offset, jeb->offset + c->sector_size, ic->ino)); 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds D2({ 2761da177e4SLinus Torvalds int i=0; 2771da177e4SLinus Torvalds struct jffs2_raw_node_ref *this; 278ad361c98SJoe Perches printk(KERN_DEBUG "After remove_node_refs_from_ino_list: \n"); 2791da177e4SLinus Torvalds 2801da177e4SLinus Torvalds this = ic->nodes; 2811da177e4SLinus Torvalds 282ad361c98SJoe Perches printk(KERN_DEBUG); 2831da177e4SLinus Torvalds while(this) { 284ad361c98SJoe Perches printk(KERN_CONT "0x%08x(%d)->", 285ad361c98SJoe Perches ref_offset(this), ref_flags(this)); 2861da177e4SLinus Torvalds if (++i == 5) { 287ad361c98SJoe Perches printk(KERN_DEBUG); 2881da177e4SLinus Torvalds i=0; 2891da177e4SLinus Torvalds } 2901da177e4SLinus Torvalds this = this->next_in_ino; 2911da177e4SLinus Torvalds } 292ad361c98SJoe Perches printk(KERN_CONT "\n"); 2931da177e4SLinus Torvalds }); 2941da177e4SLinus Torvalds 295c9f700f8SKaiGai Kohei switch (ic->class) { 296c9f700f8SKaiGai Kohei #ifdef CONFIG_JFFS2_FS_XATTR 297c9f700f8SKaiGai Kohei case RAWNODE_CLASS_XATTR_DATUM: 298c9f700f8SKaiGai Kohei jffs2_release_xattr_datum(c, (struct jffs2_xattr_datum *)ic); 299c9f700f8SKaiGai Kohei break; 300c9f700f8SKaiGai Kohei case RAWNODE_CLASS_XATTR_REF: 301c9f700f8SKaiGai Kohei jffs2_release_xattr_ref(c, (struct jffs2_xattr_ref *)ic); 302c9f700f8SKaiGai Kohei break; 303c9f700f8SKaiGai Kohei #endif 304c9f700f8SKaiGai Kohei default: 30527c72b04SDavid Woodhouse if (ic->nodes == (void *)ic && ic->pino_nlink == 0) 3061da177e4SLinus Torvalds jffs2_del_ino_cache(c, ic); 3071da177e4SLinus Torvalds } 308c9f700f8SKaiGai Kohei } 3091da177e4SLinus Torvalds 310c38c1b61SDavid Woodhouse void jffs2_free_jeb_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb) 3111da177e4SLinus Torvalds { 3129bfeb691SDavid Woodhouse struct jffs2_raw_node_ref *block, *ref; 3131da177e4SLinus Torvalds D1(printk(KERN_DEBUG "Freeing all node refs for eraseblock offset 0x%08x\n", jeb->offset)); 3141da177e4SLinus Torvalds 3159bfeb691SDavid Woodhouse block = ref = jeb->first_node; 3169bfeb691SDavid Woodhouse 3179bfeb691SDavid Woodhouse while (ref) { 3189bfeb691SDavid Woodhouse if (ref->flash_offset == REF_LINK_NODE) { 3199bfeb691SDavid Woodhouse ref = ref->next_in_ino; 3209bfeb691SDavid Woodhouse jffs2_free_refblock(block); 3219bfeb691SDavid Woodhouse block = ref; 3229bfeb691SDavid Woodhouse continue; 3239bfeb691SDavid Woodhouse } 3249bfeb691SDavid Woodhouse if (ref->flash_offset != REF_EMPTY_NODE && ref->next_in_ino) 3251da177e4SLinus Torvalds jffs2_remove_node_refs_from_ino_list(c, ref, jeb); 3261da177e4SLinus Torvalds /* else it was a non-inode node or already removed, so don't bother */ 3271da177e4SLinus Torvalds 3289bfeb691SDavid Woodhouse ref++; 3291da177e4SLinus Torvalds } 3309bfeb691SDavid Woodhouse jeb->first_node = jeb->last_node = NULL; 3311da177e4SLinus Torvalds } 3321da177e4SLinus Torvalds 3335d157885SThomas Gleixner static int jffs2_block_check_erase(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t *bad_offset) 3341da177e4SLinus Torvalds { 3355d157885SThomas Gleixner void *ebuf; 3365d157885SThomas Gleixner uint32_t ofs; 3371da177e4SLinus Torvalds size_t retlen; 3385d157885SThomas Gleixner int ret = -EIO; 339fab2c399SJoakim Tjernlund unsigned long *wordebuf; 340fab2c399SJoakim Tjernlund 341d35ea200SArtem Bityutskiy ret = mtd_point(c->mtd, jeb->offset, c->sector_size, &retlen, 342d35ea200SArtem Bityutskiy &ebuf, NULL); 343*10934478SArtem Bityutskiy if (ret != -EOPNOTSUPP) { 344fab2c399SJoakim Tjernlund if (ret) { 345fab2c399SJoakim Tjernlund D1(printk(KERN_DEBUG "MTD point failed %d\n", ret)); 346fab2c399SJoakim Tjernlund goto do_flash_read; 347fab2c399SJoakim Tjernlund } 348fab2c399SJoakim Tjernlund if (retlen < c->sector_size) { 349fab2c399SJoakim Tjernlund /* Don't muck about if it won't let us point to the whole erase sector */ 350fab2c399SJoakim Tjernlund D1(printk(KERN_DEBUG "MTD point returned len too short: 0x%zx\n", retlen)); 3517219778aSArtem Bityutskiy mtd_unpoint(c->mtd, jeb->offset, retlen); 352fab2c399SJoakim Tjernlund goto do_flash_read; 353fab2c399SJoakim Tjernlund } 354fab2c399SJoakim Tjernlund wordebuf = ebuf-sizeof(*wordebuf); 355fab2c399SJoakim Tjernlund retlen /= sizeof(*wordebuf); 356fab2c399SJoakim Tjernlund do { 357fab2c399SJoakim Tjernlund if (*++wordebuf != ~0) 358fab2c399SJoakim Tjernlund break; 359fab2c399SJoakim Tjernlund } while(--retlen); 3607219778aSArtem Bityutskiy mtd_unpoint(c->mtd, jeb->offset, c->sector_size); 3618a0f5723SAnders Grafström if (retlen) { 362f4e35647SAndrew Morton printk(KERN_WARNING "Newly-erased block contained word 0x%lx at offset 0x%08tx\n", 363fab2c399SJoakim Tjernlund *wordebuf, jeb->offset + c->sector_size-retlen*sizeof(*wordebuf)); 3648a0f5723SAnders Grafström return -EIO; 3658a0f5723SAnders Grafström } 366fab2c399SJoakim Tjernlund return 0; 367fab2c399SJoakim Tjernlund } 368fab2c399SJoakim Tjernlund do_flash_read: 3691da177e4SLinus Torvalds ebuf = kmalloc(PAGE_SIZE, GFP_KERNEL); 3701da177e4SLinus Torvalds if (!ebuf) { 3715d157885SThomas Gleixner printk(KERN_WARNING "Failed to allocate page buffer for verifying erase at 0x%08x. Refiling\n", jeb->offset); 3725d157885SThomas Gleixner return -EAGAIN; 3735d157885SThomas Gleixner } 3741da177e4SLinus Torvalds 3751da177e4SLinus Torvalds D1(printk(KERN_DEBUG "Verifying erase at 0x%08x\n", jeb->offset)); 3765d157885SThomas Gleixner 3775d157885SThomas Gleixner for (ofs = jeb->offset; ofs < jeb->offset + c->sector_size; ) { 3781da177e4SLinus Torvalds uint32_t readlen = min((uint32_t)PAGE_SIZE, jeb->offset + c->sector_size - ofs); 3791da177e4SLinus Torvalds int i; 3801da177e4SLinus Torvalds 3815d157885SThomas Gleixner *bad_offset = ofs; 3821da177e4SLinus Torvalds 383329ad399SArtem Bityutskiy ret = mtd_read(c->mtd, ofs, readlen, &retlen, ebuf); 3841da177e4SLinus Torvalds if (ret) { 3851da177e4SLinus Torvalds printk(KERN_WARNING "Read of newly-erased block at 0x%08x failed: %d. Putting on bad_list\n", ofs, ret); 3868a0f5723SAnders Grafström ret = -EIO; 3875d157885SThomas Gleixner goto fail; 3881da177e4SLinus Torvalds } 3891da177e4SLinus Torvalds if (retlen != readlen) { 3901da177e4SLinus Torvalds printk(KERN_WARNING "Short read from newly-erased block at 0x%08x. Wanted %d, got %zd\n", ofs, readlen, retlen); 3918a0f5723SAnders Grafström ret = -EIO; 3925d157885SThomas Gleixner goto fail; 3931da177e4SLinus Torvalds } 3941da177e4SLinus Torvalds for (i=0; i<readlen; i += sizeof(unsigned long)) { 3951da177e4SLinus Torvalds /* It's OK. We know it's properly aligned */ 3965d157885SThomas Gleixner unsigned long *datum = ebuf + i; 3975d157885SThomas Gleixner if (*datum + 1) { 3985d157885SThomas Gleixner *bad_offset += i; 3995d157885SThomas Gleixner printk(KERN_WARNING "Newly-erased block contained word 0x%lx at offset 0x%08x\n", *datum, *bad_offset); 4008a0f5723SAnders Grafström ret = -EIO; 4015d157885SThomas Gleixner goto fail; 4021da177e4SLinus Torvalds } 4031da177e4SLinus Torvalds } 4041da177e4SLinus Torvalds ofs += readlen; 4051da177e4SLinus Torvalds cond_resched(); 4061da177e4SLinus Torvalds } 4075d157885SThomas Gleixner ret = 0; 4085d157885SThomas Gleixner fail: 4091da177e4SLinus Torvalds kfree(ebuf); 4105d157885SThomas Gleixner return ret; 4111da177e4SLinus Torvalds } 4121da177e4SLinus Torvalds 4135d157885SThomas Gleixner static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb) 4145d157885SThomas Gleixner { 4155d157885SThomas Gleixner size_t retlen; 4165d157885SThomas Gleixner int ret; 417f4e35647SAndrew Morton uint32_t uninitialized_var(bad_offset); 4185d157885SThomas Gleixner 4195d157885SThomas Gleixner switch (jffs2_block_check_erase(c, jeb, &bad_offset)) { 4205d157885SThomas Gleixner case -EAGAIN: goto refile; 4215d157885SThomas Gleixner case -EIO: goto filebad; 4225d157885SThomas Gleixner } 4231da177e4SLinus Torvalds 4241da177e4SLinus Torvalds /* Write the erase complete marker */ 4251da177e4SLinus Torvalds D1(printk(KERN_DEBUG "Writing erased marker to block at 0x%08x\n", jeb->offset)); 4265d157885SThomas Gleixner bad_offset = jeb->offset; 4275d157885SThomas Gleixner 4285d157885SThomas Gleixner /* Cleanmarker in oob area or no cleanmarker at all ? */ 4295d157885SThomas Gleixner if (jffs2_cleanmarker_oob(c) || c->cleanmarker_size == 0) { 4305d157885SThomas Gleixner 4311da177e4SLinus Torvalds if (jffs2_cleanmarker_oob(c)) { 4321da177e4SLinus Torvalds if (jffs2_write_nand_cleanmarker(c, jeb)) 4335d157885SThomas Gleixner goto filebad; 4345d157885SThomas Gleixner } 4351da177e4SLinus Torvalds } else { 4365d157885SThomas Gleixner 4371da177e4SLinus Torvalds struct kvec vecs[1]; 4381da177e4SLinus Torvalds struct jffs2_unknown_node marker = { 4391da177e4SLinus Torvalds .magic = cpu_to_je16(JFFS2_MAGIC_BITMASK), 4401da177e4SLinus Torvalds .nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER), 4411da177e4SLinus Torvalds .totlen = cpu_to_je32(c->cleanmarker_size) 4421da177e4SLinus Torvalds }; 4431da177e4SLinus Torvalds 444046b8b98SDavid Woodhouse jffs2_prealloc_raw_node_refs(c, jeb, 1); 4455d157885SThomas Gleixner 4461da177e4SLinus Torvalds marker.hdr_crc = cpu_to_je32(crc32(0, &marker, sizeof(struct jffs2_unknown_node)-4)); 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds vecs[0].iov_base = (unsigned char *) ▮ 4491da177e4SLinus Torvalds vecs[0].iov_len = sizeof(marker); 4501da177e4SLinus Torvalds ret = jffs2_flash_direct_writev(c, vecs, 1, jeb->offset, &retlen); 4511da177e4SLinus Torvalds 4525d157885SThomas Gleixner if (ret || retlen != sizeof(marker)) { 4535d157885SThomas Gleixner if (ret) 4541da177e4SLinus Torvalds printk(KERN_WARNING "Write clean marker to block at 0x%08x failed: %d\n", 4551da177e4SLinus Torvalds jeb->offset, ret); 4565d157885SThomas Gleixner else 4571da177e4SLinus Torvalds printk(KERN_WARNING "Short write to newly-erased block at 0x%08x: Wanted %zd, got %zd\n", 4581da177e4SLinus Torvalds jeb->offset, sizeof(marker), retlen); 4595d157885SThomas Gleixner 4605d157885SThomas Gleixner goto filebad; 4611da177e4SLinus Torvalds } 462014b164eSDavid Woodhouse } 463f1f9671bSDavid Woodhouse /* Everything else got zeroed before the erase */ 464f1f9671bSDavid Woodhouse jeb->free_size = c->sector_size; 4651da177e4SLinus Torvalds 466ced22070SDavid Woodhouse mutex_lock(&c->erase_free_sem); 4671da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 468014b164eSDavid Woodhouse 4691da177e4SLinus Torvalds c->erasing_size -= c->sector_size; 470014b164eSDavid Woodhouse c->free_size += c->sector_size; 471014b164eSDavid Woodhouse 472014b164eSDavid Woodhouse /* Account for cleanmarker now, if it's in-band */ 473014b164eSDavid Woodhouse if (c->cleanmarker_size && !jffs2_cleanmarker_oob(c)) 474014b164eSDavid Woodhouse jffs2_link_node_ref(c, jeb, jeb->offset | REF_NORMAL, c->cleanmarker_size, NULL); 4751da177e4SLinus Torvalds 476e2bc322bSDavid Woodhouse list_move_tail(&jeb->list, &c->free_list); 4771da177e4SLinus Torvalds c->nr_erasing_blocks--; 4781da177e4SLinus Torvalds c->nr_free_blocks++; 47985a62db6SDavid Woodhouse 48085a62db6SDavid Woodhouse jffs2_dbg_acct_sanity_check_nolock(c, jeb); 48185a62db6SDavid Woodhouse jffs2_dbg_acct_paranoia_check_nolock(c, jeb); 48285a62db6SDavid Woodhouse 4831da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 484ced22070SDavid Woodhouse mutex_unlock(&c->erase_free_sem); 4851da177e4SLinus Torvalds wake_up(&c->erase_wait); 4865d157885SThomas Gleixner return; 4871da177e4SLinus Torvalds 4885d157885SThomas Gleixner filebad: 4895d157885SThomas Gleixner jffs2_erase_failed(c, jeb, bad_offset); 4905d157885SThomas Gleixner return; 4915d157885SThomas Gleixner 4925d157885SThomas Gleixner refile: 4935d157885SThomas Gleixner /* Stick it back on the list from whence it came and come back later */ 494ced22070SDavid Woodhouse mutex_lock(&c->erase_free_sem); 4955d157885SThomas Gleixner spin_lock(&c->erase_completion_lock); 496ae3b6ba0SDavid Woodhouse jffs2_garbage_collect_trigger(c); 497e2bc322bSDavid Woodhouse list_move(&jeb->list, &c->erase_complete_list); 4985d157885SThomas Gleixner spin_unlock(&c->erase_completion_lock); 499ced22070SDavid Woodhouse mutex_unlock(&c->erase_free_sem); 5005d157885SThomas Gleixner return; 5015d157885SThomas Gleixner } 502