11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * JFFS2 -- Journalling Flash File System, Version 2. 31da177e4SLinus Torvalds * 4c00c310eSDavid Woodhouse * Copyright © 2001-2007 Red Hat, Inc. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Created by David Woodhouse <dwmw2@infradead.org> 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * For licensing information, see the file 'LICENCE' in this directory. 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds #include <linux/kernel.h> 131da177e4SLinus Torvalds #include <linux/slab.h> 141da177e4SLinus Torvalds #include <linux/mtd/mtd.h> 151da177e4SLinus Torvalds #include <linux/compiler.h> 161da177e4SLinus Torvalds #include <linux/crc32.h> 171da177e4SLinus Torvalds #include <linux/sched.h> 181da177e4SLinus Torvalds #include <linux/pagemap.h> 191da177e4SLinus Torvalds #include "nodelist.h" 201da177e4SLinus Torvalds 211da177e4SLinus Torvalds struct erase_priv_struct { 221da177e4SLinus Torvalds struct jffs2_eraseblock *jeb; 231da177e4SLinus Torvalds struct jffs2_sb_info *c; 241da177e4SLinus Torvalds }; 251da177e4SLinus Torvalds 261da177e4SLinus Torvalds #ifndef __ECOS 271da177e4SLinus Torvalds static void jffs2_erase_callback(struct erase_info *); 281da177e4SLinus Torvalds #endif 291da177e4SLinus Torvalds static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset); 301da177e4SLinus Torvalds static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb); 311da177e4SLinus Torvalds static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb); 321da177e4SLinus Torvalds 331da177e4SLinus Torvalds static void jffs2_erase_block(struct jffs2_sb_info *c, 341da177e4SLinus Torvalds struct jffs2_eraseblock *jeb) 351da177e4SLinus Torvalds { 361da177e4SLinus Torvalds int ret; 371da177e4SLinus Torvalds uint32_t bad_offset; 381da177e4SLinus Torvalds #ifdef __ECOS 391da177e4SLinus Torvalds ret = jffs2_flash_erase(c, jeb); 401da177e4SLinus Torvalds if (!ret) { 411da177e4SLinus Torvalds jffs2_erase_succeeded(c, jeb); 421da177e4SLinus Torvalds return; 431da177e4SLinus Torvalds } 441da177e4SLinus Torvalds bad_offset = jeb->offset; 451da177e4SLinus Torvalds #else /* Linux */ 461da177e4SLinus Torvalds struct erase_info *instr; 471da177e4SLinus Torvalds 48e0c8e42fSArtem B. Bityutskiy D1(printk(KERN_DEBUG "jffs2_erase_block(): erase block %#08x (range %#08x-%#08x)\n", 49e0c8e42fSArtem B. Bityutskiy jeb->offset, jeb->offset, jeb->offset + c->sector_size)); 501da177e4SLinus Torvalds instr = kmalloc(sizeof(struct erase_info) + sizeof(struct erase_priv_struct), GFP_KERNEL); 511da177e4SLinus Torvalds if (!instr) { 521da177e4SLinus Torvalds printk(KERN_WARNING "kmalloc for struct erase_info in jffs2_erase_block failed. Refiling block for later\n"); 53ced22070SDavid Woodhouse mutex_lock(&c->erase_free_sem); 541da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 55f116629dSAkinobu Mita list_move(&jeb->list, &c->erase_pending_list); 561da177e4SLinus Torvalds c->erasing_size -= c->sector_size; 571da177e4SLinus Torvalds c->dirty_size += c->sector_size; 581da177e4SLinus Torvalds jeb->dirty_size = c->sector_size; 591da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 60ced22070SDavid Woodhouse mutex_unlock(&c->erase_free_sem); 611da177e4SLinus Torvalds return; 621da177e4SLinus Torvalds } 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds memset(instr, 0, sizeof(*instr)); 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds instr->mtd = c->mtd; 671da177e4SLinus Torvalds instr->addr = jeb->offset; 681da177e4SLinus Torvalds instr->len = c->sector_size; 691da177e4SLinus Torvalds instr->callback = jffs2_erase_callback; 701da177e4SLinus Torvalds instr->priv = (unsigned long)(&instr[1]); 71*bb0eb217SAdrian Hunter instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN; 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds ((struct erase_priv_struct *)instr->priv)->jeb = jeb; 741da177e4SLinus Torvalds ((struct erase_priv_struct *)instr->priv)->c = c; 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds ret = c->mtd->erase(c->mtd, instr); 771da177e4SLinus Torvalds if (!ret) 781da177e4SLinus Torvalds return; 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds bad_offset = instr->fail_addr; 811da177e4SLinus Torvalds kfree(instr); 821da177e4SLinus Torvalds #endif /* __ECOS */ 831da177e4SLinus Torvalds 841da177e4SLinus Torvalds if (ret == -ENOMEM || ret == -EAGAIN) { 851da177e4SLinus Torvalds /* Erase failed immediately. Refile it on the list */ 861da177e4SLinus Torvalds D1(printk(KERN_DEBUG "Erase at 0x%08x failed: %d. Refiling on erase_pending_list\n", jeb->offset, ret)); 87ced22070SDavid Woodhouse mutex_lock(&c->erase_free_sem); 881da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 89f116629dSAkinobu Mita list_move(&jeb->list, &c->erase_pending_list); 901da177e4SLinus Torvalds c->erasing_size -= c->sector_size; 911da177e4SLinus Torvalds c->dirty_size += c->sector_size; 921da177e4SLinus Torvalds jeb->dirty_size = c->sector_size; 931da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 94ced22070SDavid Woodhouse mutex_unlock(&c->erase_free_sem); 951da177e4SLinus Torvalds return; 961da177e4SLinus Torvalds } 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds if (ret == -EROFS) 991da177e4SLinus Torvalds printk(KERN_WARNING "Erase at 0x%08x failed immediately: -EROFS. Is the sector locked?\n", jeb->offset); 1001da177e4SLinus Torvalds else 1011da177e4SLinus Torvalds printk(KERN_WARNING "Erase at 0x%08x failed immediately: errno %d\n", jeb->offset, ret); 1021da177e4SLinus Torvalds 1031da177e4SLinus Torvalds jffs2_erase_failed(c, jeb, bad_offset); 1041da177e4SLinus Torvalds } 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds void jffs2_erase_pending_blocks(struct jffs2_sb_info *c, int count) 1071da177e4SLinus Torvalds { 1081da177e4SLinus Torvalds struct jffs2_eraseblock *jeb; 1091da177e4SLinus Torvalds 110ced22070SDavid Woodhouse mutex_lock(&c->erase_free_sem); 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds while (!list_empty(&c->erase_complete_list) || 1151da177e4SLinus Torvalds !list_empty(&c->erase_pending_list)) { 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds if (!list_empty(&c->erase_complete_list)) { 1181da177e4SLinus Torvalds jeb = list_entry(c->erase_complete_list.next, struct jffs2_eraseblock, list); 119e2bc322bSDavid Woodhouse list_move(&jeb->list, &c->erase_checking_list); 1201da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 121ced22070SDavid Woodhouse mutex_unlock(&c->erase_free_sem); 1221da177e4SLinus Torvalds jffs2_mark_erased_block(c, jeb); 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds if (!--count) { 1251da177e4SLinus Torvalds D1(printk(KERN_DEBUG "Count reached. jffs2_erase_pending_blocks leaving\n")); 1261da177e4SLinus Torvalds goto done; 1271da177e4SLinus Torvalds } 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds } else if (!list_empty(&c->erase_pending_list)) { 1301da177e4SLinus Torvalds jeb = list_entry(c->erase_pending_list.next, struct jffs2_eraseblock, list); 1311da177e4SLinus Torvalds D1(printk(KERN_DEBUG "Starting erase of pending block 0x%08x\n", jeb->offset)); 1321da177e4SLinus Torvalds list_del(&jeb->list); 1331da177e4SLinus Torvalds c->erasing_size += c->sector_size; 1341da177e4SLinus Torvalds c->wasted_size -= jeb->wasted_size; 1351da177e4SLinus Torvalds c->free_size -= jeb->free_size; 1361da177e4SLinus Torvalds c->used_size -= jeb->used_size; 1371da177e4SLinus Torvalds c->dirty_size -= jeb->dirty_size; 1381da177e4SLinus Torvalds jeb->wasted_size = jeb->used_size = jeb->dirty_size = jeb->free_size = 0; 139c38c1b61SDavid Woodhouse jffs2_free_jeb_node_refs(c, jeb); 1401da177e4SLinus Torvalds list_add(&jeb->list, &c->erasing_list); 1411da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 142ced22070SDavid Woodhouse mutex_unlock(&c->erase_free_sem); 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds jffs2_erase_block(c, jeb); 1451da177e4SLinus Torvalds 1461da177e4SLinus Torvalds } else { 1471da177e4SLinus Torvalds BUG(); 1481da177e4SLinus Torvalds } 1491da177e4SLinus Torvalds 1501da177e4SLinus Torvalds /* Be nice */ 151fd532490SJoakim Tjernlund yield(); 152ced22070SDavid Woodhouse mutex_lock(&c->erase_free_sem); 1531da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 1541da177e4SLinus Torvalds } 1551da177e4SLinus Torvalds 1561da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 157ced22070SDavid Woodhouse mutex_unlock(&c->erase_free_sem); 1581da177e4SLinus Torvalds done: 1591da177e4SLinus Torvalds D1(printk(KERN_DEBUG "jffs2_erase_pending_blocks completed\n")); 1601da177e4SLinus Torvalds } 1611da177e4SLinus Torvalds 1621da177e4SLinus Torvalds static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb) 1631da177e4SLinus Torvalds { 1641da177e4SLinus Torvalds D1(printk(KERN_DEBUG "Erase completed successfully at 0x%08x\n", jeb->offset)); 165ced22070SDavid Woodhouse mutex_lock(&c->erase_free_sem); 1661da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 167f116629dSAkinobu Mita list_move_tail(&jeb->list, &c->erase_complete_list); 1681da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 169ced22070SDavid Woodhouse mutex_unlock(&c->erase_free_sem); 1701da177e4SLinus Torvalds /* Ensure that kupdated calls us again to mark them clean */ 1711da177e4SLinus Torvalds jffs2_erase_pending_trigger(c); 1721da177e4SLinus Torvalds } 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset) 1751da177e4SLinus Torvalds { 1761da177e4SLinus Torvalds /* For NAND, if the failure did not occur at the device level for a 1771da177e4SLinus Torvalds specific physical page, don't bother updating the bad block table. */ 178*bb0eb217SAdrian Hunter if (jffs2_cleanmarker_oob(c) && (bad_offset != MTD_FAIL_ADDR_UNKNOWN)) { 1791da177e4SLinus Torvalds /* We had a device-level failure to erase. Let's see if we've 1801da177e4SLinus Torvalds failed too many times. */ 1811da177e4SLinus Torvalds if (!jffs2_write_nand_badblock(c, jeb, bad_offset)) { 1821da177e4SLinus Torvalds /* We'd like to give this block another try. */ 183ced22070SDavid Woodhouse mutex_lock(&c->erase_free_sem); 1841da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 185f116629dSAkinobu Mita list_move(&jeb->list, &c->erase_pending_list); 1861da177e4SLinus Torvalds c->erasing_size -= c->sector_size; 1871da177e4SLinus Torvalds c->dirty_size += c->sector_size; 1881da177e4SLinus Torvalds jeb->dirty_size = c->sector_size; 1891da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 190ced22070SDavid Woodhouse mutex_unlock(&c->erase_free_sem); 1911da177e4SLinus Torvalds return; 1921da177e4SLinus Torvalds } 1931da177e4SLinus Torvalds } 1941da177e4SLinus Torvalds 195ced22070SDavid Woodhouse mutex_lock(&c->erase_free_sem); 1961da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 1971da177e4SLinus Torvalds c->erasing_size -= c->sector_size; 1981da177e4SLinus Torvalds c->bad_size += c->sector_size; 199f116629dSAkinobu Mita list_move(&jeb->list, &c->bad_list); 2001da177e4SLinus Torvalds c->nr_erasing_blocks--; 2011da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 202ced22070SDavid Woodhouse mutex_unlock(&c->erase_free_sem); 2031da177e4SLinus Torvalds wake_up(&c->erase_wait); 2041da177e4SLinus Torvalds } 2051da177e4SLinus Torvalds 2061da177e4SLinus Torvalds #ifndef __ECOS 2071da177e4SLinus Torvalds static void jffs2_erase_callback(struct erase_info *instr) 2081da177e4SLinus Torvalds { 2091da177e4SLinus Torvalds struct erase_priv_struct *priv = (void *)instr->priv; 2101da177e4SLinus Torvalds 2111da177e4SLinus Torvalds if(instr->state != MTD_ERASE_DONE) { 2121da177e4SLinus Torvalds printk(KERN_WARNING "Erase at 0x%08x finished, but state != MTD_ERASE_DONE. State is 0x%x instead.\n", instr->addr, instr->state); 2131da177e4SLinus Torvalds jffs2_erase_failed(priv->c, priv->jeb, instr->fail_addr); 2141da177e4SLinus Torvalds } else { 2151da177e4SLinus Torvalds jffs2_erase_succeeded(priv->c, priv->jeb); 2161da177e4SLinus Torvalds } 2171da177e4SLinus Torvalds kfree(instr); 2181da177e4SLinus Torvalds } 2191da177e4SLinus Torvalds #endif /* !__ECOS */ 2201da177e4SLinus Torvalds 2211da177e4SLinus Torvalds /* Hmmm. Maybe we should accept the extra space it takes and make 2221da177e4SLinus Torvalds this a standard doubly-linked list? */ 2231da177e4SLinus Torvalds static inline void jffs2_remove_node_refs_from_ino_list(struct jffs2_sb_info *c, 2241da177e4SLinus Torvalds struct jffs2_raw_node_ref *ref, struct jffs2_eraseblock *jeb) 2251da177e4SLinus Torvalds { 2261da177e4SLinus Torvalds struct jffs2_inode_cache *ic = NULL; 2271da177e4SLinus Torvalds struct jffs2_raw_node_ref **prev; 2281da177e4SLinus Torvalds 2291da177e4SLinus Torvalds prev = &ref->next_in_ino; 2301da177e4SLinus Torvalds 2311da177e4SLinus Torvalds /* Walk the inode's list once, removing any nodes from this eraseblock */ 2321da177e4SLinus Torvalds while (1) { 2331da177e4SLinus Torvalds if (!(*prev)->next_in_ino) { 2341da177e4SLinus Torvalds /* We're looking at the jffs2_inode_cache, which is 2351da177e4SLinus Torvalds at the end of the linked list. Stash it and continue 2361da177e4SLinus Torvalds from the beginning of the list */ 2371da177e4SLinus Torvalds ic = (struct jffs2_inode_cache *)(*prev); 2381da177e4SLinus Torvalds prev = &ic->nodes; 2391da177e4SLinus Torvalds continue; 2401da177e4SLinus Torvalds } 2411da177e4SLinus Torvalds 2423be36675SAndrew Victor if (SECTOR_ADDR((*prev)->flash_offset) == jeb->offset) { 2431da177e4SLinus Torvalds /* It's in the block we're erasing */ 2441da177e4SLinus Torvalds struct jffs2_raw_node_ref *this; 2451da177e4SLinus Torvalds 2461da177e4SLinus Torvalds this = *prev; 2471da177e4SLinus Torvalds *prev = this->next_in_ino; 2481da177e4SLinus Torvalds this->next_in_ino = NULL; 2491da177e4SLinus Torvalds 2501da177e4SLinus Torvalds if (this == ref) 2511da177e4SLinus Torvalds break; 2521da177e4SLinus Torvalds 2531da177e4SLinus Torvalds continue; 2541da177e4SLinus Torvalds } 2551da177e4SLinus Torvalds /* Not to be deleted. Skip */ 2561da177e4SLinus Torvalds prev = &((*prev)->next_in_ino); 2571da177e4SLinus Torvalds } 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds /* PARANOIA */ 2601da177e4SLinus Torvalds if (!ic) { 261c9f700f8SKaiGai Kohei JFFS2_WARNING("inode_cache/xattr_datum/xattr_ref" 262c9f700f8SKaiGai Kohei " not found in remove_node_refs()!!\n"); 2631da177e4SLinus Torvalds return; 2641da177e4SLinus Torvalds } 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds D1(printk(KERN_DEBUG "Removed nodes in range 0x%08x-0x%08x from ino #%u\n", 2671da177e4SLinus Torvalds jeb->offset, jeb->offset + c->sector_size, ic->ino)); 2681da177e4SLinus Torvalds 2691da177e4SLinus Torvalds D2({ 2701da177e4SLinus Torvalds int i=0; 2711da177e4SLinus Torvalds struct jffs2_raw_node_ref *this; 2721da177e4SLinus Torvalds printk(KERN_DEBUG "After remove_node_refs_from_ino_list: \n" KERN_DEBUG); 2731da177e4SLinus Torvalds 2741da177e4SLinus Torvalds this = ic->nodes; 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds while(this) { 2771da177e4SLinus Torvalds printk( "0x%08x(%d)->", ref_offset(this), ref_flags(this)); 2781da177e4SLinus Torvalds if (++i == 5) { 2791da177e4SLinus Torvalds printk("\n" KERN_DEBUG); 2801da177e4SLinus Torvalds i=0; 2811da177e4SLinus Torvalds } 2821da177e4SLinus Torvalds this = this->next_in_ino; 2831da177e4SLinus Torvalds } 2841da177e4SLinus Torvalds printk("\n"); 2851da177e4SLinus Torvalds }); 2861da177e4SLinus Torvalds 287c9f700f8SKaiGai Kohei switch (ic->class) { 288c9f700f8SKaiGai Kohei #ifdef CONFIG_JFFS2_FS_XATTR 289c9f700f8SKaiGai Kohei case RAWNODE_CLASS_XATTR_DATUM: 290c9f700f8SKaiGai Kohei jffs2_release_xattr_datum(c, (struct jffs2_xattr_datum *)ic); 291c9f700f8SKaiGai Kohei break; 292c9f700f8SKaiGai Kohei case RAWNODE_CLASS_XATTR_REF: 293c9f700f8SKaiGai Kohei jffs2_release_xattr_ref(c, (struct jffs2_xattr_ref *)ic); 294c9f700f8SKaiGai Kohei break; 295c9f700f8SKaiGai Kohei #endif 296c9f700f8SKaiGai Kohei default: 29727c72b04SDavid Woodhouse if (ic->nodes == (void *)ic && ic->pino_nlink == 0) 2981da177e4SLinus Torvalds jffs2_del_ino_cache(c, ic); 2991da177e4SLinus Torvalds } 300c9f700f8SKaiGai Kohei } 3011da177e4SLinus Torvalds 302c38c1b61SDavid Woodhouse void jffs2_free_jeb_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb) 3031da177e4SLinus Torvalds { 3049bfeb691SDavid Woodhouse struct jffs2_raw_node_ref *block, *ref; 3051da177e4SLinus Torvalds D1(printk(KERN_DEBUG "Freeing all node refs for eraseblock offset 0x%08x\n", jeb->offset)); 3061da177e4SLinus Torvalds 3079bfeb691SDavid Woodhouse block = ref = jeb->first_node; 3089bfeb691SDavid Woodhouse 3099bfeb691SDavid Woodhouse while (ref) { 3109bfeb691SDavid Woodhouse if (ref->flash_offset == REF_LINK_NODE) { 3119bfeb691SDavid Woodhouse ref = ref->next_in_ino; 3129bfeb691SDavid Woodhouse jffs2_free_refblock(block); 3139bfeb691SDavid Woodhouse block = ref; 3149bfeb691SDavid Woodhouse continue; 3159bfeb691SDavid Woodhouse } 3169bfeb691SDavid Woodhouse if (ref->flash_offset != REF_EMPTY_NODE && ref->next_in_ino) 3171da177e4SLinus Torvalds jffs2_remove_node_refs_from_ino_list(c, ref, jeb); 3181da177e4SLinus Torvalds /* else it was a non-inode node or already removed, so don't bother */ 3191da177e4SLinus Torvalds 3209bfeb691SDavid Woodhouse ref++; 3211da177e4SLinus Torvalds } 3229bfeb691SDavid Woodhouse jeb->first_node = jeb->last_node = NULL; 3231da177e4SLinus Torvalds } 3241da177e4SLinus Torvalds 3255d157885SThomas Gleixner static int jffs2_block_check_erase(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t *bad_offset) 3261da177e4SLinus Torvalds { 3275d157885SThomas Gleixner void *ebuf; 3285d157885SThomas Gleixner uint32_t ofs; 3291da177e4SLinus Torvalds size_t retlen; 3305d157885SThomas Gleixner int ret = -EIO; 3311da177e4SLinus Torvalds 332fab2c399SJoakim Tjernlund if (c->mtd->point) { 333fab2c399SJoakim Tjernlund unsigned long *wordebuf; 334fab2c399SJoakim Tjernlund 335a98889f3SJared Hulbert ret = c->mtd->point(c->mtd, jeb->offset, c->sector_size, 336a98889f3SJared Hulbert &retlen, &ebuf, NULL); 337fab2c399SJoakim Tjernlund if (ret) { 338fab2c399SJoakim Tjernlund D1(printk(KERN_DEBUG "MTD point failed %d\n", ret)); 339fab2c399SJoakim Tjernlund goto do_flash_read; 340fab2c399SJoakim Tjernlund } 341fab2c399SJoakim Tjernlund if (retlen < c->sector_size) { 342fab2c399SJoakim Tjernlund /* Don't muck about if it won't let us point to the whole erase sector */ 343fab2c399SJoakim Tjernlund D1(printk(KERN_DEBUG "MTD point returned len too short: 0x%zx\n", retlen)); 344a98889f3SJared Hulbert c->mtd->unpoint(c->mtd, jeb->offset, retlen); 345fab2c399SJoakim Tjernlund goto do_flash_read; 346fab2c399SJoakim Tjernlund } 347fab2c399SJoakim Tjernlund wordebuf = ebuf-sizeof(*wordebuf); 348fab2c399SJoakim Tjernlund retlen /= sizeof(*wordebuf); 349fab2c399SJoakim Tjernlund do { 350fab2c399SJoakim Tjernlund if (*++wordebuf != ~0) 351fab2c399SJoakim Tjernlund break; 352fab2c399SJoakim Tjernlund } while(--retlen); 353a98889f3SJared Hulbert c->mtd->unpoint(c->mtd, jeb->offset, c->sector_size); 3548a0f5723SAnders Grafström if (retlen) { 355f4e35647SAndrew Morton printk(KERN_WARNING "Newly-erased block contained word 0x%lx at offset 0x%08tx\n", 356fab2c399SJoakim Tjernlund *wordebuf, jeb->offset + c->sector_size-retlen*sizeof(*wordebuf)); 3578a0f5723SAnders Grafström return -EIO; 3588a0f5723SAnders Grafström } 359fab2c399SJoakim Tjernlund return 0; 360fab2c399SJoakim Tjernlund } 361fab2c399SJoakim Tjernlund do_flash_read: 3621da177e4SLinus Torvalds ebuf = kmalloc(PAGE_SIZE, GFP_KERNEL); 3631da177e4SLinus Torvalds if (!ebuf) { 3645d157885SThomas Gleixner printk(KERN_WARNING "Failed to allocate page buffer for verifying erase at 0x%08x. Refiling\n", jeb->offset); 3655d157885SThomas Gleixner return -EAGAIN; 3665d157885SThomas Gleixner } 3671da177e4SLinus Torvalds 3681da177e4SLinus Torvalds D1(printk(KERN_DEBUG "Verifying erase at 0x%08x\n", jeb->offset)); 3695d157885SThomas Gleixner 3705d157885SThomas Gleixner for (ofs = jeb->offset; ofs < jeb->offset + c->sector_size; ) { 3711da177e4SLinus Torvalds uint32_t readlen = min((uint32_t)PAGE_SIZE, jeb->offset + c->sector_size - ofs); 3721da177e4SLinus Torvalds int i; 3731da177e4SLinus Torvalds 3745d157885SThomas Gleixner *bad_offset = ofs; 3751da177e4SLinus Torvalds 376b0afbbecSArtem Bityutskiy ret = c->mtd->read(c->mtd, ofs, readlen, &retlen, ebuf); 3771da177e4SLinus Torvalds if (ret) { 3781da177e4SLinus Torvalds printk(KERN_WARNING "Read of newly-erased block at 0x%08x failed: %d. Putting on bad_list\n", ofs, ret); 3798a0f5723SAnders Grafström ret = -EIO; 3805d157885SThomas Gleixner goto fail; 3811da177e4SLinus Torvalds } 3821da177e4SLinus Torvalds if (retlen != readlen) { 3831da177e4SLinus Torvalds printk(KERN_WARNING "Short read from newly-erased block at 0x%08x. Wanted %d, got %zd\n", ofs, readlen, retlen); 3848a0f5723SAnders Grafström ret = -EIO; 3855d157885SThomas Gleixner goto fail; 3861da177e4SLinus Torvalds } 3871da177e4SLinus Torvalds for (i=0; i<readlen; i += sizeof(unsigned long)) { 3881da177e4SLinus Torvalds /* It's OK. We know it's properly aligned */ 3895d157885SThomas Gleixner unsigned long *datum = ebuf + i; 3905d157885SThomas Gleixner if (*datum + 1) { 3915d157885SThomas Gleixner *bad_offset += i; 3925d157885SThomas Gleixner printk(KERN_WARNING "Newly-erased block contained word 0x%lx at offset 0x%08x\n", *datum, *bad_offset); 3938a0f5723SAnders Grafström ret = -EIO; 3945d157885SThomas Gleixner goto fail; 3951da177e4SLinus Torvalds } 3961da177e4SLinus Torvalds } 3971da177e4SLinus Torvalds ofs += readlen; 3981da177e4SLinus Torvalds cond_resched(); 3991da177e4SLinus Torvalds } 4005d157885SThomas Gleixner ret = 0; 4015d157885SThomas Gleixner fail: 4021da177e4SLinus Torvalds kfree(ebuf); 4035d157885SThomas Gleixner return ret; 4041da177e4SLinus Torvalds } 4051da177e4SLinus Torvalds 4065d157885SThomas Gleixner static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb) 4075d157885SThomas Gleixner { 4085d157885SThomas Gleixner size_t retlen; 4095d157885SThomas Gleixner int ret; 410f4e35647SAndrew Morton uint32_t uninitialized_var(bad_offset); 4115d157885SThomas Gleixner 4125d157885SThomas Gleixner switch (jffs2_block_check_erase(c, jeb, &bad_offset)) { 4135d157885SThomas Gleixner case -EAGAIN: goto refile; 4145d157885SThomas Gleixner case -EIO: goto filebad; 4155d157885SThomas Gleixner } 4161da177e4SLinus Torvalds 4171da177e4SLinus Torvalds /* Write the erase complete marker */ 4181da177e4SLinus Torvalds D1(printk(KERN_DEBUG "Writing erased marker to block at 0x%08x\n", jeb->offset)); 4195d157885SThomas Gleixner bad_offset = jeb->offset; 4205d157885SThomas Gleixner 4215d157885SThomas Gleixner /* Cleanmarker in oob area or no cleanmarker at all ? */ 4225d157885SThomas Gleixner if (jffs2_cleanmarker_oob(c) || c->cleanmarker_size == 0) { 4235d157885SThomas Gleixner 4241da177e4SLinus Torvalds if (jffs2_cleanmarker_oob(c)) { 4251da177e4SLinus Torvalds if (jffs2_write_nand_cleanmarker(c, jeb)) 4265d157885SThomas Gleixner goto filebad; 4275d157885SThomas Gleixner } 4281da177e4SLinus Torvalds } else { 4295d157885SThomas Gleixner 4301da177e4SLinus Torvalds struct kvec vecs[1]; 4311da177e4SLinus Torvalds struct jffs2_unknown_node marker = { 4321da177e4SLinus Torvalds .magic = cpu_to_je16(JFFS2_MAGIC_BITMASK), 4331da177e4SLinus Torvalds .nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER), 4341da177e4SLinus Torvalds .totlen = cpu_to_je32(c->cleanmarker_size) 4351da177e4SLinus Torvalds }; 4361da177e4SLinus Torvalds 437046b8b98SDavid Woodhouse jffs2_prealloc_raw_node_refs(c, jeb, 1); 4385d157885SThomas Gleixner 4391da177e4SLinus Torvalds marker.hdr_crc = cpu_to_je32(crc32(0, &marker, sizeof(struct jffs2_unknown_node)-4)); 4401da177e4SLinus Torvalds 4411da177e4SLinus Torvalds vecs[0].iov_base = (unsigned char *) ▮ 4421da177e4SLinus Torvalds vecs[0].iov_len = sizeof(marker); 4431da177e4SLinus Torvalds ret = jffs2_flash_direct_writev(c, vecs, 1, jeb->offset, &retlen); 4441da177e4SLinus Torvalds 4455d157885SThomas Gleixner if (ret || retlen != sizeof(marker)) { 4465d157885SThomas Gleixner if (ret) 4471da177e4SLinus Torvalds printk(KERN_WARNING "Write clean marker to block at 0x%08x failed: %d\n", 4481da177e4SLinus Torvalds jeb->offset, ret); 4495d157885SThomas Gleixner else 4501da177e4SLinus Torvalds printk(KERN_WARNING "Short write to newly-erased block at 0x%08x: Wanted %zd, got %zd\n", 4511da177e4SLinus Torvalds jeb->offset, sizeof(marker), retlen); 4525d157885SThomas Gleixner 4535d157885SThomas Gleixner goto filebad; 4541da177e4SLinus Torvalds } 455014b164eSDavid Woodhouse } 456f1f9671bSDavid Woodhouse /* Everything else got zeroed before the erase */ 457f1f9671bSDavid Woodhouse jeb->free_size = c->sector_size; 4581da177e4SLinus Torvalds 459ced22070SDavid Woodhouse mutex_lock(&c->erase_free_sem); 4601da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 461014b164eSDavid Woodhouse 4621da177e4SLinus Torvalds c->erasing_size -= c->sector_size; 463014b164eSDavid Woodhouse c->free_size += c->sector_size; 464014b164eSDavid Woodhouse 465014b164eSDavid Woodhouse /* Account for cleanmarker now, if it's in-band */ 466014b164eSDavid Woodhouse if (c->cleanmarker_size && !jffs2_cleanmarker_oob(c)) 467014b164eSDavid Woodhouse jffs2_link_node_ref(c, jeb, jeb->offset | REF_NORMAL, c->cleanmarker_size, NULL); 4681da177e4SLinus Torvalds 469e2bc322bSDavid Woodhouse list_move_tail(&jeb->list, &c->free_list); 4701da177e4SLinus Torvalds c->nr_erasing_blocks--; 4711da177e4SLinus Torvalds c->nr_free_blocks++; 47285a62db6SDavid Woodhouse 47385a62db6SDavid Woodhouse jffs2_dbg_acct_sanity_check_nolock(c, jeb); 47485a62db6SDavid Woodhouse jffs2_dbg_acct_paranoia_check_nolock(c, jeb); 47585a62db6SDavid Woodhouse 4761da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 477ced22070SDavid Woodhouse mutex_unlock(&c->erase_free_sem); 4781da177e4SLinus Torvalds wake_up(&c->erase_wait); 4795d157885SThomas Gleixner return; 4801da177e4SLinus Torvalds 4815d157885SThomas Gleixner filebad: 482ced22070SDavid Woodhouse mutex_lock(&c->erase_free_sem); 4835d157885SThomas Gleixner spin_lock(&c->erase_completion_lock); 4845d157885SThomas Gleixner /* Stick it on a list (any list) so erase_failed can take it 4855d157885SThomas Gleixner right off again. Silly, but shouldn't happen often. */ 486e2bc322bSDavid Woodhouse list_move(&jeb->list, &c->erasing_list); 4875d157885SThomas Gleixner spin_unlock(&c->erase_completion_lock); 488ced22070SDavid Woodhouse mutex_unlock(&c->erase_free_sem); 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 */ 4945d157885SThomas Gleixner jffs2_erase_pending_trigger(c); 495ced22070SDavid Woodhouse mutex_lock(&c->erase_free_sem); 4965d157885SThomas Gleixner spin_lock(&c->erase_completion_lock); 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