11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * JFFS2 -- Journalling Flash File System, Version 2. 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 2001-2003 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 * 10733802d9SArtem B. Bityutskiy * $Id: erase.c,v 1.85 2005/09/20 14:53:15 dedekind Exp $ 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds */ 131da177e4SLinus Torvalds 141da177e4SLinus Torvalds #include <linux/kernel.h> 151da177e4SLinus Torvalds #include <linux/slab.h> 161da177e4SLinus Torvalds #include <linux/mtd/mtd.h> 171da177e4SLinus Torvalds #include <linux/compiler.h> 181da177e4SLinus Torvalds #include <linux/crc32.h> 191da177e4SLinus Torvalds #include <linux/sched.h> 201da177e4SLinus Torvalds #include <linux/pagemap.h> 211da177e4SLinus Torvalds #include "nodelist.h" 221da177e4SLinus Torvalds 231da177e4SLinus Torvalds struct erase_priv_struct { 241da177e4SLinus Torvalds struct jffs2_eraseblock *jeb; 251da177e4SLinus Torvalds struct jffs2_sb_info *c; 261da177e4SLinus Torvalds }; 271da177e4SLinus Torvalds 281da177e4SLinus Torvalds #ifndef __ECOS 291da177e4SLinus Torvalds static void jffs2_erase_callback(struct erase_info *); 301da177e4SLinus Torvalds #endif 311da177e4SLinus Torvalds static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset); 321da177e4SLinus Torvalds static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb); 331da177e4SLinus Torvalds static void jffs2_free_all_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb); 341da177e4SLinus Torvalds static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb); 351da177e4SLinus Torvalds 361da177e4SLinus Torvalds static void jffs2_erase_block(struct jffs2_sb_info *c, 371da177e4SLinus Torvalds struct jffs2_eraseblock *jeb) 381da177e4SLinus Torvalds { 391da177e4SLinus Torvalds int ret; 401da177e4SLinus Torvalds uint32_t bad_offset; 411da177e4SLinus Torvalds #ifdef __ECOS 421da177e4SLinus Torvalds ret = jffs2_flash_erase(c, jeb); 431da177e4SLinus Torvalds if (!ret) { 441da177e4SLinus Torvalds jffs2_erase_succeeded(c, jeb); 451da177e4SLinus Torvalds return; 461da177e4SLinus Torvalds } 471da177e4SLinus Torvalds bad_offset = jeb->offset; 481da177e4SLinus Torvalds #else /* Linux */ 491da177e4SLinus Torvalds struct erase_info *instr; 501da177e4SLinus Torvalds 51e0c8e42fSArtem B. Bityutskiy D1(printk(KERN_DEBUG "jffs2_erase_block(): erase block %#08x (range %#08x-%#08x)\n", 52e0c8e42fSArtem B. Bityutskiy jeb->offset, jeb->offset, jeb->offset + c->sector_size)); 531da177e4SLinus Torvalds instr = kmalloc(sizeof(struct erase_info) + sizeof(struct erase_priv_struct), GFP_KERNEL); 541da177e4SLinus Torvalds if (!instr) { 551da177e4SLinus Torvalds printk(KERN_WARNING "kmalloc for struct erase_info in jffs2_erase_block failed. Refiling block for later\n"); 561da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 571da177e4SLinus Torvalds list_del(&jeb->list); 581da177e4SLinus Torvalds list_add(&jeb->list, &c->erase_pending_list); 591da177e4SLinus Torvalds c->erasing_size -= c->sector_size; 601da177e4SLinus Torvalds c->dirty_size += c->sector_size; 611da177e4SLinus Torvalds jeb->dirty_size = c->sector_size; 621da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 631da177e4SLinus Torvalds return; 641da177e4SLinus Torvalds } 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds memset(instr, 0, sizeof(*instr)); 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds instr->mtd = c->mtd; 691da177e4SLinus Torvalds instr->addr = jeb->offset; 701da177e4SLinus Torvalds instr->len = c->sector_size; 711da177e4SLinus Torvalds instr->callback = jffs2_erase_callback; 721da177e4SLinus Torvalds instr->priv = (unsigned long)(&instr[1]); 731da177e4SLinus Torvalds instr->fail_addr = 0xffffffff; 741da177e4SLinus Torvalds 751da177e4SLinus Torvalds ((struct erase_priv_struct *)instr->priv)->jeb = jeb; 761da177e4SLinus Torvalds ((struct erase_priv_struct *)instr->priv)->c = c; 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds ret = c->mtd->erase(c->mtd, instr); 791da177e4SLinus Torvalds if (!ret) 801da177e4SLinus Torvalds return; 811da177e4SLinus Torvalds 821da177e4SLinus Torvalds bad_offset = instr->fail_addr; 831da177e4SLinus Torvalds kfree(instr); 841da177e4SLinus Torvalds #endif /* __ECOS */ 851da177e4SLinus Torvalds 861da177e4SLinus Torvalds if (ret == -ENOMEM || ret == -EAGAIN) { 871da177e4SLinus Torvalds /* Erase failed immediately. Refile it on the list */ 881da177e4SLinus Torvalds D1(printk(KERN_DEBUG "Erase at 0x%08x failed: %d. Refiling on erase_pending_list\n", jeb->offset, ret)); 891da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 901da177e4SLinus Torvalds list_del(&jeb->list); 911da177e4SLinus Torvalds list_add(&jeb->list, &c->erase_pending_list); 921da177e4SLinus Torvalds c->erasing_size -= c->sector_size; 931da177e4SLinus Torvalds c->dirty_size += c->sector_size; 941da177e4SLinus Torvalds jeb->dirty_size = c->sector_size; 951da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 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 1071da177e4SLinus Torvalds void jffs2_erase_pending_blocks(struct jffs2_sb_info *c, int count) 1081da177e4SLinus Torvalds { 1091da177e4SLinus Torvalds struct jffs2_eraseblock *jeb; 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds down(&c->erase_free_sem); 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 1141da177e4SLinus Torvalds 1151da177e4SLinus Torvalds while (!list_empty(&c->erase_complete_list) || 1161da177e4SLinus Torvalds !list_empty(&c->erase_pending_list)) { 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds if (!list_empty(&c->erase_complete_list)) { 1191da177e4SLinus Torvalds jeb = list_entry(c->erase_complete_list.next, struct jffs2_eraseblock, list); 1201da177e4SLinus Torvalds list_del(&jeb->list); 1211da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 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; 1391da177e4SLinus Torvalds jffs2_free_all_node_refs(c, jeb); 1401da177e4SLinus Torvalds list_add(&jeb->list, &c->erasing_list); 1411da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds jffs2_erase_block(c, jeb); 1441da177e4SLinus Torvalds 1451da177e4SLinus Torvalds } else { 1461da177e4SLinus Torvalds BUG(); 1471da177e4SLinus Torvalds } 1481da177e4SLinus Torvalds 1491da177e4SLinus Torvalds /* Be nice */ 1501da177e4SLinus Torvalds cond_resched(); 1511da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 1521da177e4SLinus Torvalds } 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 1551da177e4SLinus Torvalds done: 1561da177e4SLinus Torvalds D1(printk(KERN_DEBUG "jffs2_erase_pending_blocks completed\n")); 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds up(&c->erase_free_sem); 1591da177e4SLinus Torvalds } 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb) 1621da177e4SLinus Torvalds { 1631da177e4SLinus Torvalds D1(printk(KERN_DEBUG "Erase completed successfully at 0x%08x\n", jeb->offset)); 1641da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 1651da177e4SLinus Torvalds list_del(&jeb->list); 1661da177e4SLinus Torvalds list_add_tail(&jeb->list, &c->erase_complete_list); 1671da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 1681da177e4SLinus Torvalds /* Ensure that kupdated calls us again to mark them clean */ 1691da177e4SLinus Torvalds jffs2_erase_pending_trigger(c); 1701da177e4SLinus Torvalds } 1711da177e4SLinus Torvalds 1721da177e4SLinus Torvalds static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset) 1731da177e4SLinus Torvalds { 1741da177e4SLinus Torvalds /* For NAND, if the failure did not occur at the device level for a 1751da177e4SLinus Torvalds specific physical page, don't bother updating the bad block table. */ 1761da177e4SLinus Torvalds if (jffs2_cleanmarker_oob(c) && (bad_offset != 0xffffffff)) { 1771da177e4SLinus Torvalds /* We had a device-level failure to erase. Let's see if we've 1781da177e4SLinus Torvalds failed too many times. */ 1791da177e4SLinus Torvalds if (!jffs2_write_nand_badblock(c, jeb, bad_offset)) { 1801da177e4SLinus Torvalds /* We'd like to give this block another try. */ 1811da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 1821da177e4SLinus Torvalds list_del(&jeb->list); 1831da177e4SLinus Torvalds list_add(&jeb->list, &c->erase_pending_list); 1841da177e4SLinus Torvalds c->erasing_size -= c->sector_size; 1851da177e4SLinus Torvalds c->dirty_size += c->sector_size; 1861da177e4SLinus Torvalds jeb->dirty_size = c->sector_size; 1871da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 1881da177e4SLinus Torvalds return; 1891da177e4SLinus Torvalds } 1901da177e4SLinus Torvalds } 1911da177e4SLinus Torvalds 1921da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 1931da177e4SLinus Torvalds c->erasing_size -= c->sector_size; 1941da177e4SLinus Torvalds c->bad_size += c->sector_size; 1951da177e4SLinus Torvalds list_del(&jeb->list); 1961da177e4SLinus Torvalds list_add(&jeb->list, &c->bad_list); 1971da177e4SLinus Torvalds c->nr_erasing_blocks--; 1981da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 1991da177e4SLinus Torvalds wake_up(&c->erase_wait); 2001da177e4SLinus Torvalds } 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds #ifndef __ECOS 2031da177e4SLinus Torvalds static void jffs2_erase_callback(struct erase_info *instr) 2041da177e4SLinus Torvalds { 2051da177e4SLinus Torvalds struct erase_priv_struct *priv = (void *)instr->priv; 2061da177e4SLinus Torvalds 2071da177e4SLinus Torvalds if(instr->state != MTD_ERASE_DONE) { 2081da177e4SLinus Torvalds printk(KERN_WARNING "Erase at 0x%08x finished, but state != MTD_ERASE_DONE. State is 0x%x instead.\n", instr->addr, instr->state); 2091da177e4SLinus Torvalds jffs2_erase_failed(priv->c, priv->jeb, instr->fail_addr); 2101da177e4SLinus Torvalds } else { 2111da177e4SLinus Torvalds jffs2_erase_succeeded(priv->c, priv->jeb); 2121da177e4SLinus Torvalds } 2131da177e4SLinus Torvalds kfree(instr); 2141da177e4SLinus Torvalds } 2151da177e4SLinus Torvalds #endif /* !__ECOS */ 2161da177e4SLinus Torvalds 2171da177e4SLinus Torvalds /* Hmmm. Maybe we should accept the extra space it takes and make 2181da177e4SLinus Torvalds this a standard doubly-linked list? */ 2191da177e4SLinus Torvalds static inline void jffs2_remove_node_refs_from_ino_list(struct jffs2_sb_info *c, 2201da177e4SLinus Torvalds struct jffs2_raw_node_ref *ref, struct jffs2_eraseblock *jeb) 2211da177e4SLinus Torvalds { 2221da177e4SLinus Torvalds struct jffs2_inode_cache *ic = NULL; 2231da177e4SLinus Torvalds struct jffs2_raw_node_ref **prev; 2241da177e4SLinus Torvalds 2251da177e4SLinus Torvalds prev = &ref->next_in_ino; 2261da177e4SLinus Torvalds 2271da177e4SLinus Torvalds /* Walk the inode's list once, removing any nodes from this eraseblock */ 2281da177e4SLinus Torvalds while (1) { 2291da177e4SLinus Torvalds if (!(*prev)->next_in_ino) { 2301da177e4SLinus Torvalds /* We're looking at the jffs2_inode_cache, which is 2311da177e4SLinus Torvalds at the end of the linked list. Stash it and continue 2321da177e4SLinus Torvalds from the beginning of the list */ 2331da177e4SLinus Torvalds ic = (struct jffs2_inode_cache *)(*prev); 2341da177e4SLinus Torvalds prev = &ic->nodes; 2351da177e4SLinus Torvalds continue; 2361da177e4SLinus Torvalds } 2371da177e4SLinus Torvalds 2383be36675SAndrew Victor if (SECTOR_ADDR((*prev)->flash_offset) == jeb->offset) { 2391da177e4SLinus Torvalds /* It's in the block we're erasing */ 2401da177e4SLinus Torvalds struct jffs2_raw_node_ref *this; 2411da177e4SLinus Torvalds 2421da177e4SLinus Torvalds this = *prev; 2431da177e4SLinus Torvalds *prev = this->next_in_ino; 2441da177e4SLinus Torvalds this->next_in_ino = NULL; 2451da177e4SLinus Torvalds 2461da177e4SLinus Torvalds if (this == ref) 2471da177e4SLinus Torvalds break; 2481da177e4SLinus Torvalds 2491da177e4SLinus Torvalds continue; 2501da177e4SLinus Torvalds } 2511da177e4SLinus Torvalds /* Not to be deleted. Skip */ 2521da177e4SLinus Torvalds prev = &((*prev)->next_in_ino); 2531da177e4SLinus Torvalds } 2541da177e4SLinus Torvalds 2551da177e4SLinus Torvalds /* PARANOIA */ 2561da177e4SLinus Torvalds if (!ic) { 2571da177e4SLinus Torvalds printk(KERN_WARNING "inode_cache not found in remove_node_refs()!!\n"); 2581da177e4SLinus Torvalds return; 2591da177e4SLinus Torvalds } 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds D1(printk(KERN_DEBUG "Removed nodes in range 0x%08x-0x%08x from ino #%u\n", 2621da177e4SLinus Torvalds jeb->offset, jeb->offset + c->sector_size, ic->ino)); 2631da177e4SLinus Torvalds 2641da177e4SLinus Torvalds D2({ 2651da177e4SLinus Torvalds int i=0; 2661da177e4SLinus Torvalds struct jffs2_raw_node_ref *this; 2671da177e4SLinus Torvalds printk(KERN_DEBUG "After remove_node_refs_from_ino_list: \n" KERN_DEBUG); 2681da177e4SLinus Torvalds 2691da177e4SLinus Torvalds this = ic->nodes; 2701da177e4SLinus Torvalds 2711da177e4SLinus Torvalds while(this) { 2721da177e4SLinus Torvalds printk( "0x%08x(%d)->", ref_offset(this), ref_flags(this)); 2731da177e4SLinus Torvalds if (++i == 5) { 2741da177e4SLinus Torvalds printk("\n" KERN_DEBUG); 2751da177e4SLinus Torvalds i=0; 2761da177e4SLinus Torvalds } 2771da177e4SLinus Torvalds this = this->next_in_ino; 2781da177e4SLinus Torvalds } 2791da177e4SLinus Torvalds printk("\n"); 2801da177e4SLinus Torvalds }); 2811da177e4SLinus Torvalds 282437316d9SArtem B. Bityuckiy if (ic->nodes == (void *)ic && ic->nlink == 0) 2831da177e4SLinus Torvalds jffs2_del_ino_cache(c, ic); 2841da177e4SLinus Torvalds } 2851da177e4SLinus Torvalds 2861da177e4SLinus Torvalds static void jffs2_free_all_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb) 2871da177e4SLinus Torvalds { 2881da177e4SLinus Torvalds struct jffs2_raw_node_ref *ref; 2891da177e4SLinus Torvalds D1(printk(KERN_DEBUG "Freeing all node refs for eraseblock offset 0x%08x\n", jeb->offset)); 2901da177e4SLinus Torvalds while(jeb->first_node) { 2911da177e4SLinus Torvalds ref = jeb->first_node; 2921da177e4SLinus Torvalds jeb->first_node = ref->next_phys; 2931da177e4SLinus Torvalds 2941da177e4SLinus Torvalds /* Remove from the inode-list */ 2951da177e4SLinus Torvalds if (ref->next_in_ino) 2961da177e4SLinus Torvalds jffs2_remove_node_refs_from_ino_list(c, ref, jeb); 2971da177e4SLinus Torvalds /* else it was a non-inode node or already removed, so don't bother */ 2981da177e4SLinus Torvalds 2991da177e4SLinus Torvalds jffs2_free_raw_node_ref(ref); 3001da177e4SLinus Torvalds } 3011da177e4SLinus Torvalds jeb->last_node = NULL; 3021da177e4SLinus Torvalds } 3031da177e4SLinus Torvalds 3045d157885SThomas Gleixner static int jffs2_block_check_erase(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t *bad_offset) 3051da177e4SLinus Torvalds { 3065d157885SThomas Gleixner void *ebuf; 3075d157885SThomas Gleixner uint32_t ofs; 3081da177e4SLinus Torvalds size_t retlen; 3095d157885SThomas Gleixner int ret = -EIO; 3101da177e4SLinus Torvalds 3111da177e4SLinus Torvalds ebuf = kmalloc(PAGE_SIZE, GFP_KERNEL); 3121da177e4SLinus Torvalds if (!ebuf) { 3135d157885SThomas Gleixner printk(KERN_WARNING "Failed to allocate page buffer for verifying erase at 0x%08x. Refiling\n", jeb->offset); 3145d157885SThomas Gleixner return -EAGAIN; 3155d157885SThomas Gleixner } 3161da177e4SLinus Torvalds 3171da177e4SLinus Torvalds D1(printk(KERN_DEBUG "Verifying erase at 0x%08x\n", jeb->offset)); 3185d157885SThomas Gleixner 3195d157885SThomas Gleixner for (ofs = jeb->offset; ofs < jeb->offset + c->sector_size; ) { 3201da177e4SLinus Torvalds uint32_t readlen = min((uint32_t)PAGE_SIZE, jeb->offset + c->sector_size - ofs); 3211da177e4SLinus Torvalds int i; 3221da177e4SLinus Torvalds 3235d157885SThomas Gleixner *bad_offset = ofs; 3241da177e4SLinus Torvalds 3255d157885SThomas Gleixner ret = jffs2_flash_read(c, ofs, readlen, &retlen, ebuf); 3261da177e4SLinus Torvalds if (ret) { 3271da177e4SLinus Torvalds printk(KERN_WARNING "Read of newly-erased block at 0x%08x failed: %d. Putting on bad_list\n", ofs, ret); 3285d157885SThomas Gleixner goto fail; 3291da177e4SLinus Torvalds } 3301da177e4SLinus Torvalds if (retlen != readlen) { 3311da177e4SLinus Torvalds printk(KERN_WARNING "Short read from newly-erased block at 0x%08x. Wanted %d, got %zd\n", ofs, readlen, retlen); 3325d157885SThomas Gleixner goto fail; 3331da177e4SLinus Torvalds } 3341da177e4SLinus Torvalds for (i=0; i<readlen; i += sizeof(unsigned long)) { 3351da177e4SLinus Torvalds /* It's OK. We know it's properly aligned */ 3365d157885SThomas Gleixner unsigned long *datum = ebuf + i; 3375d157885SThomas Gleixner if (*datum + 1) { 3385d157885SThomas Gleixner *bad_offset += i; 3395d157885SThomas Gleixner printk(KERN_WARNING "Newly-erased block contained word 0x%lx at offset 0x%08x\n", *datum, *bad_offset); 3405d157885SThomas Gleixner goto fail; 3411da177e4SLinus Torvalds } 3421da177e4SLinus Torvalds } 3431da177e4SLinus Torvalds ofs += readlen; 3441da177e4SLinus Torvalds cond_resched(); 3451da177e4SLinus Torvalds } 3465d157885SThomas Gleixner ret = 0; 3475d157885SThomas Gleixner fail: 3481da177e4SLinus Torvalds kfree(ebuf); 3495d157885SThomas Gleixner return ret; 3501da177e4SLinus Torvalds } 3511da177e4SLinus Torvalds 3525d157885SThomas Gleixner static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb) 3535d157885SThomas Gleixner { 3545d157885SThomas Gleixner struct jffs2_raw_node_ref *marker_ref = NULL; 3555d157885SThomas Gleixner size_t retlen; 3565d157885SThomas Gleixner int ret; 3575d157885SThomas Gleixner uint32_t bad_offset; 3585d157885SThomas Gleixner 3595d157885SThomas Gleixner switch (jffs2_block_check_erase(c, jeb, &bad_offset)) { 3605d157885SThomas Gleixner case -EAGAIN: goto refile; 3615d157885SThomas Gleixner case -EIO: goto filebad; 3625d157885SThomas Gleixner } 3631da177e4SLinus Torvalds 3641da177e4SLinus Torvalds /* Write the erase complete marker */ 3651da177e4SLinus Torvalds D1(printk(KERN_DEBUG "Writing erased marker to block at 0x%08x\n", jeb->offset)); 3665d157885SThomas Gleixner bad_offset = jeb->offset; 3675d157885SThomas Gleixner 3685d157885SThomas Gleixner /* Cleanmarker in oob area or no cleanmarker at all ? */ 3695d157885SThomas Gleixner if (jffs2_cleanmarker_oob(c) || c->cleanmarker_size == 0) { 3705d157885SThomas Gleixner 3711da177e4SLinus Torvalds if (jffs2_cleanmarker_oob(c)) { 3721da177e4SLinus Torvalds if (jffs2_write_nand_cleanmarker(c, jeb)) 3735d157885SThomas Gleixner goto filebad; 3745d157885SThomas Gleixner } 3751da177e4SLinus Torvalds 376*f1f9671bSDavid Woodhouse /* Everything else got zeroed before the erase */ 3771da177e4SLinus Torvalds jeb->free_size = c->sector_size; 3781da177e4SLinus Torvalds } else { 3795d157885SThomas Gleixner 3801da177e4SLinus Torvalds struct kvec vecs[1]; 3811da177e4SLinus Torvalds struct jffs2_unknown_node marker = { 3821da177e4SLinus Torvalds .magic = cpu_to_je16(JFFS2_MAGIC_BITMASK), 3831da177e4SLinus Torvalds .nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER), 3841da177e4SLinus Torvalds .totlen = cpu_to_je32(c->cleanmarker_size) 3851da177e4SLinus Torvalds }; 3861da177e4SLinus Torvalds 3875d157885SThomas Gleixner marker_ref = jffs2_alloc_raw_node_ref(); 3885d157885SThomas Gleixner if (!marker_ref) { 3895d157885SThomas Gleixner printk(KERN_WARNING "Failed to allocate raw node ref for clean marker. Refiling\n"); 3905d157885SThomas Gleixner goto refile; 3915d157885SThomas Gleixner } 3925d157885SThomas Gleixner 3931da177e4SLinus Torvalds marker.hdr_crc = cpu_to_je32(crc32(0, &marker, sizeof(struct jffs2_unknown_node)-4)); 3941da177e4SLinus Torvalds 3951da177e4SLinus Torvalds vecs[0].iov_base = (unsigned char *) ▮ 3961da177e4SLinus Torvalds vecs[0].iov_len = sizeof(marker); 3971da177e4SLinus Torvalds ret = jffs2_flash_direct_writev(c, vecs, 1, jeb->offset, &retlen); 3981da177e4SLinus Torvalds 3995d157885SThomas Gleixner if (ret || retlen != sizeof(marker)) { 4005d157885SThomas Gleixner if (ret) 4011da177e4SLinus Torvalds printk(KERN_WARNING "Write clean marker to block at 0x%08x failed: %d\n", 4021da177e4SLinus Torvalds jeb->offset, ret); 4035d157885SThomas Gleixner else 4041da177e4SLinus Torvalds printk(KERN_WARNING "Short write to newly-erased block at 0x%08x: Wanted %zd, got %zd\n", 4051da177e4SLinus Torvalds jeb->offset, sizeof(marker), retlen); 4065d157885SThomas Gleixner 4075d157885SThomas Gleixner jffs2_free_raw_node_ref(marker_ref); 4085d157885SThomas Gleixner goto filebad; 4091da177e4SLinus Torvalds } 4101da177e4SLinus Torvalds 411*f1f9671bSDavid Woodhouse /* Everything else got zeroed before the erase */ 412*f1f9671bSDavid Woodhouse jeb->free_size = c->sector_size; 413*f1f9671bSDavid Woodhouse 4141da177e4SLinus Torvalds marker_ref->next_in_ino = NULL; 4151da177e4SLinus Torvalds marker_ref->flash_offset = jeb->offset | REF_NORMAL; 4161da177e4SLinus Torvalds 417*f1f9671bSDavid Woodhouse jffs2_link_node_ref(c, jeb, marker_ref, c->cleanmarker_size); 4181da177e4SLinus Torvalds } 4191da177e4SLinus Torvalds 4201da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 4211da177e4SLinus Torvalds c->erasing_size -= c->sector_size; 4221da177e4SLinus Torvalds c->free_size += jeb->free_size; 4231da177e4SLinus Torvalds c->used_size += jeb->used_size; 4241da177e4SLinus Torvalds 425e0c8e42fSArtem B. Bityutskiy jffs2_dbg_acct_sanity_check_nolock(c,jeb); 426e0c8e42fSArtem B. Bityutskiy jffs2_dbg_acct_paranoia_check_nolock(c, jeb); 4271da177e4SLinus Torvalds 4281da177e4SLinus Torvalds list_add_tail(&jeb->list, &c->free_list); 4291da177e4SLinus Torvalds c->nr_erasing_blocks--; 4301da177e4SLinus Torvalds c->nr_free_blocks++; 4311da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 4321da177e4SLinus Torvalds wake_up(&c->erase_wait); 4335d157885SThomas Gleixner return; 4341da177e4SLinus Torvalds 4355d157885SThomas Gleixner filebad: 4365d157885SThomas Gleixner spin_lock(&c->erase_completion_lock); 4375d157885SThomas Gleixner /* Stick it on a list (any list) so erase_failed can take it 4385d157885SThomas Gleixner right off again. Silly, but shouldn't happen often. */ 4395d157885SThomas Gleixner list_add(&jeb->list, &c->erasing_list); 4405d157885SThomas Gleixner spin_unlock(&c->erase_completion_lock); 4415d157885SThomas Gleixner jffs2_erase_failed(c, jeb, bad_offset); 4425d157885SThomas Gleixner return; 4435d157885SThomas Gleixner 4445d157885SThomas Gleixner refile: 4455d157885SThomas Gleixner /* Stick it back on the list from whence it came and come back later */ 4465d157885SThomas Gleixner jffs2_erase_pending_trigger(c); 4475d157885SThomas Gleixner spin_lock(&c->erase_completion_lock); 4485d157885SThomas Gleixner list_add(&jeb->list, &c->erase_complete_list); 4495d157885SThomas Gleixner spin_unlock(&c->erase_completion_lock); 4505d157885SThomas Gleixner return; 4515d157885SThomas Gleixner } 452