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 49*9c261b33SJoe Perches jffs2_dbg(1, "%s(): erase block %#08x (range %#08x-%#08x)\n", 50*9c261b33SJoe Perches __func__, 51*9c261b33SJoe Perches jeb->offset, jeb->offset, jeb->offset + c->sector_size); 521da177e4SLinus Torvalds instr = kmalloc(sizeof(struct erase_info) + sizeof(struct erase_priv_struct), GFP_KERNEL); 531da177e4SLinus Torvalds if (!instr) { 541da177e4SLinus Torvalds printk(KERN_WARNING "kmalloc for struct erase_info in jffs2_erase_block failed. Refiling block for later\n"); 55ced22070SDavid Woodhouse mutex_lock(&c->erase_free_sem); 561da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 57f116629dSAkinobu Mita list_move(&jeb->list, &c->erase_pending_list); 581da177e4SLinus Torvalds c->erasing_size -= c->sector_size; 591da177e4SLinus Torvalds c->dirty_size += c->sector_size; 601da177e4SLinus Torvalds jeb->dirty_size = c->sector_size; 611da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 62ced22070SDavid Woodhouse mutex_unlock(&c->erase_free_sem); 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]); 73bb0eb217SAdrian Hunter instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN; 741da177e4SLinus Torvalds 751da177e4SLinus Torvalds ((struct erase_priv_struct *)instr->priv)->jeb = jeb; 761da177e4SLinus Torvalds ((struct erase_priv_struct *)instr->priv)->c = c; 771da177e4SLinus Torvalds 787e1f0dc0SArtem Bityutskiy ret = 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 */ 88*9c261b33SJoe Perches jffs2_dbg(1, "Erase at 0x%08x failed: %d. Refiling on erase_pending_list\n", 89*9c261b33SJoe Perches jeb->offset, ret); 90ced22070SDavid Woodhouse mutex_lock(&c->erase_free_sem); 911da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 92f116629dSAkinobu Mita list_move(&jeb->list, &c->erase_pending_list); 931da177e4SLinus Torvalds c->erasing_size -= c->sector_size; 941da177e4SLinus Torvalds c->dirty_size += c->sector_size; 951da177e4SLinus Torvalds jeb->dirty_size = c->sector_size; 961da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 97ced22070SDavid Woodhouse mutex_unlock(&c->erase_free_sem); 981da177e4SLinus Torvalds return; 991da177e4SLinus Torvalds } 1001da177e4SLinus Torvalds 1011da177e4SLinus Torvalds if (ret == -EROFS) 1021da177e4SLinus Torvalds printk(KERN_WARNING "Erase at 0x%08x failed immediately: -EROFS. Is the sector locked?\n", jeb->offset); 1031da177e4SLinus Torvalds else 1041da177e4SLinus Torvalds printk(KERN_WARNING "Erase at 0x%08x failed immediately: errno %d\n", jeb->offset, ret); 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds jffs2_erase_failed(c, jeb, bad_offset); 1071da177e4SLinus Torvalds } 1081da177e4SLinus Torvalds 1099957abeaSJoakim Tjernlund int jffs2_erase_pending_blocks(struct jffs2_sb_info *c, int count) 1101da177e4SLinus Torvalds { 1111da177e4SLinus Torvalds struct jffs2_eraseblock *jeb; 1129957abeaSJoakim Tjernlund int work_done = 0; 1131da177e4SLinus Torvalds 114ced22070SDavid Woodhouse mutex_lock(&c->erase_free_sem); 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds while (!list_empty(&c->erase_complete_list) || 1191da177e4SLinus Torvalds !list_empty(&c->erase_pending_list)) { 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds if (!list_empty(&c->erase_complete_list)) { 1221da177e4SLinus Torvalds jeb = list_entry(c->erase_complete_list.next, struct jffs2_eraseblock, list); 123e2bc322bSDavid Woodhouse list_move(&jeb->list, &c->erase_checking_list); 1241da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 125ced22070SDavid Woodhouse mutex_unlock(&c->erase_free_sem); 1261da177e4SLinus Torvalds jffs2_mark_erased_block(c, jeb); 1271da177e4SLinus Torvalds 1289957abeaSJoakim Tjernlund work_done++; 1291da177e4SLinus Torvalds if (!--count) { 130*9c261b33SJoe Perches jffs2_dbg(1, "Count reached. jffs2_erase_pending_blocks leaving\n"); 1311da177e4SLinus Torvalds goto done; 1321da177e4SLinus Torvalds } 1331da177e4SLinus Torvalds 1341da177e4SLinus Torvalds } else if (!list_empty(&c->erase_pending_list)) { 1351da177e4SLinus Torvalds jeb = list_entry(c->erase_pending_list.next, struct jffs2_eraseblock, list); 136*9c261b33SJoe Perches jffs2_dbg(1, "Starting erase of pending block 0x%08x\n", 137*9c261b33SJoe Perches jeb->offset); 1381da177e4SLinus Torvalds list_del(&jeb->list); 1391da177e4SLinus Torvalds c->erasing_size += c->sector_size; 1401da177e4SLinus Torvalds c->wasted_size -= jeb->wasted_size; 1411da177e4SLinus Torvalds c->free_size -= jeb->free_size; 1421da177e4SLinus Torvalds c->used_size -= jeb->used_size; 1431da177e4SLinus Torvalds c->dirty_size -= jeb->dirty_size; 1441da177e4SLinus Torvalds jeb->wasted_size = jeb->used_size = jeb->dirty_size = jeb->free_size = 0; 145c38c1b61SDavid Woodhouse jffs2_free_jeb_node_refs(c, jeb); 1461da177e4SLinus Torvalds list_add(&jeb->list, &c->erasing_list); 1471da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 148ced22070SDavid Woodhouse mutex_unlock(&c->erase_free_sem); 1491da177e4SLinus Torvalds 1501da177e4SLinus Torvalds jffs2_erase_block(c, jeb); 1511da177e4SLinus Torvalds 1521da177e4SLinus Torvalds } else { 1531da177e4SLinus Torvalds BUG(); 1541da177e4SLinus Torvalds } 1551da177e4SLinus Torvalds 1561da177e4SLinus Torvalds /* Be nice */ 1573866f673SWolfram Sang cond_resched(); 158ced22070SDavid Woodhouse mutex_lock(&c->erase_free_sem); 1591da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 1601da177e4SLinus Torvalds } 1611da177e4SLinus Torvalds 1621da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 163ced22070SDavid Woodhouse mutex_unlock(&c->erase_free_sem); 1641da177e4SLinus Torvalds done: 165*9c261b33SJoe Perches jffs2_dbg(1, "jffs2_erase_pending_blocks completed\n"); 1669957abeaSJoakim Tjernlund return work_done; 1671da177e4SLinus Torvalds } 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb) 1701da177e4SLinus Torvalds { 171*9c261b33SJoe Perches jffs2_dbg(1, "Erase completed successfully at 0x%08x\n", jeb->offset); 172ced22070SDavid Woodhouse mutex_lock(&c->erase_free_sem); 1731da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 174f116629dSAkinobu Mita list_move_tail(&jeb->list, &c->erase_complete_list); 175ae3b6ba0SDavid Woodhouse /* Wake the GC thread to mark them clean */ 176ae3b6ba0SDavid Woodhouse jffs2_garbage_collect_trigger(c); 1771da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 178ced22070SDavid Woodhouse mutex_unlock(&c->erase_free_sem); 1790717bf84SDavid Woodhouse wake_up(&c->erase_wait); 1801da177e4SLinus Torvalds } 1811da177e4SLinus Torvalds 1821da177e4SLinus Torvalds static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset) 1831da177e4SLinus Torvalds { 1841da177e4SLinus Torvalds /* For NAND, if the failure did not occur at the device level for a 1851da177e4SLinus Torvalds specific physical page, don't bother updating the bad block table. */ 18669423d99SAdrian Hunter if (jffs2_cleanmarker_oob(c) && (bad_offset != (uint32_t)MTD_FAIL_ADDR_UNKNOWN)) { 1871da177e4SLinus Torvalds /* We had a device-level failure to erase. Let's see if we've 1881da177e4SLinus Torvalds failed too many times. */ 1891da177e4SLinus Torvalds if (!jffs2_write_nand_badblock(c, jeb, bad_offset)) { 1901da177e4SLinus Torvalds /* We'd like to give this block another try. */ 191ced22070SDavid Woodhouse mutex_lock(&c->erase_free_sem); 1921da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 193f116629dSAkinobu Mita list_move(&jeb->list, &c->erase_pending_list); 1941da177e4SLinus Torvalds c->erasing_size -= c->sector_size; 1951da177e4SLinus Torvalds c->dirty_size += c->sector_size; 1961da177e4SLinus Torvalds jeb->dirty_size = c->sector_size; 1971da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 198ced22070SDavid Woodhouse mutex_unlock(&c->erase_free_sem); 1991da177e4SLinus Torvalds return; 2001da177e4SLinus Torvalds } 2011da177e4SLinus Torvalds } 2021da177e4SLinus Torvalds 203ced22070SDavid Woodhouse mutex_lock(&c->erase_free_sem); 2041da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 2051da177e4SLinus Torvalds c->erasing_size -= c->sector_size; 2061da177e4SLinus Torvalds c->bad_size += c->sector_size; 207f116629dSAkinobu Mita list_move(&jeb->list, &c->bad_list); 2081da177e4SLinus Torvalds c->nr_erasing_blocks--; 2091da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 210ced22070SDavid Woodhouse mutex_unlock(&c->erase_free_sem); 2111da177e4SLinus Torvalds wake_up(&c->erase_wait); 2121da177e4SLinus Torvalds } 2131da177e4SLinus Torvalds 2141da177e4SLinus Torvalds #ifndef __ECOS 2151da177e4SLinus Torvalds static void jffs2_erase_callback(struct erase_info *instr) 2161da177e4SLinus Torvalds { 2171da177e4SLinus Torvalds struct erase_priv_struct *priv = (void *)instr->priv; 2181da177e4SLinus Torvalds 2191da177e4SLinus Torvalds if(instr->state != MTD_ERASE_DONE) { 22069423d99SAdrian Hunter printk(KERN_WARNING "Erase at 0x%08llx finished, but state != MTD_ERASE_DONE. State is 0x%x instead.\n", 22169423d99SAdrian Hunter (unsigned long long)instr->addr, instr->state); 2221da177e4SLinus Torvalds jffs2_erase_failed(priv->c, priv->jeb, instr->fail_addr); 2231da177e4SLinus Torvalds } else { 2241da177e4SLinus Torvalds jffs2_erase_succeeded(priv->c, priv->jeb); 2251da177e4SLinus Torvalds } 2261da177e4SLinus Torvalds kfree(instr); 2271da177e4SLinus Torvalds } 2281da177e4SLinus Torvalds #endif /* !__ECOS */ 2291da177e4SLinus Torvalds 2301da177e4SLinus Torvalds /* Hmmm. Maybe we should accept the extra space it takes and make 2311da177e4SLinus Torvalds this a standard doubly-linked list? */ 2321da177e4SLinus Torvalds static inline void jffs2_remove_node_refs_from_ino_list(struct jffs2_sb_info *c, 2331da177e4SLinus Torvalds struct jffs2_raw_node_ref *ref, struct jffs2_eraseblock *jeb) 2341da177e4SLinus Torvalds { 2351da177e4SLinus Torvalds struct jffs2_inode_cache *ic = NULL; 2361da177e4SLinus Torvalds struct jffs2_raw_node_ref **prev; 2371da177e4SLinus Torvalds 2381da177e4SLinus Torvalds prev = &ref->next_in_ino; 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds /* Walk the inode's list once, removing any nodes from this eraseblock */ 2411da177e4SLinus Torvalds while (1) { 2421da177e4SLinus Torvalds if (!(*prev)->next_in_ino) { 2431da177e4SLinus Torvalds /* We're looking at the jffs2_inode_cache, which is 2441da177e4SLinus Torvalds at the end of the linked list. Stash it and continue 2451da177e4SLinus Torvalds from the beginning of the list */ 2461da177e4SLinus Torvalds ic = (struct jffs2_inode_cache *)(*prev); 2471da177e4SLinus Torvalds prev = &ic->nodes; 2481da177e4SLinus Torvalds continue; 2491da177e4SLinus Torvalds } 2501da177e4SLinus Torvalds 2513be36675SAndrew Victor if (SECTOR_ADDR((*prev)->flash_offset) == jeb->offset) { 2521da177e4SLinus Torvalds /* It's in the block we're erasing */ 2531da177e4SLinus Torvalds struct jffs2_raw_node_ref *this; 2541da177e4SLinus Torvalds 2551da177e4SLinus Torvalds this = *prev; 2561da177e4SLinus Torvalds *prev = this->next_in_ino; 2571da177e4SLinus Torvalds this->next_in_ino = NULL; 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds if (this == ref) 2601da177e4SLinus Torvalds break; 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds continue; 2631da177e4SLinus Torvalds } 2641da177e4SLinus Torvalds /* Not to be deleted. Skip */ 2651da177e4SLinus Torvalds prev = &((*prev)->next_in_ino); 2661da177e4SLinus Torvalds } 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds /* PARANOIA */ 2691da177e4SLinus Torvalds if (!ic) { 270c9f700f8SKaiGai Kohei JFFS2_WARNING("inode_cache/xattr_datum/xattr_ref" 271c9f700f8SKaiGai Kohei " not found in remove_node_refs()!!\n"); 2721da177e4SLinus Torvalds return; 2731da177e4SLinus Torvalds } 2741da177e4SLinus Torvalds 275*9c261b33SJoe Perches jffs2_dbg(1, "Removed nodes in range 0x%08x-0x%08x from ino #%u\n", 276*9c261b33SJoe Perches jeb->offset, jeb->offset + c->sector_size, ic->ino); 2771da177e4SLinus Torvalds 2781da177e4SLinus Torvalds D2({ 2791da177e4SLinus Torvalds int i=0; 2801da177e4SLinus Torvalds struct jffs2_raw_node_ref *this; 281ad361c98SJoe Perches printk(KERN_DEBUG "After remove_node_refs_from_ino_list: \n"); 2821da177e4SLinus Torvalds 2831da177e4SLinus Torvalds this = ic->nodes; 2841da177e4SLinus Torvalds 285ad361c98SJoe Perches printk(KERN_DEBUG); 2861da177e4SLinus Torvalds while(this) { 287ad361c98SJoe Perches printk(KERN_CONT "0x%08x(%d)->", 288ad361c98SJoe Perches ref_offset(this), ref_flags(this)); 2891da177e4SLinus Torvalds if (++i == 5) { 290ad361c98SJoe Perches printk(KERN_DEBUG); 2911da177e4SLinus Torvalds i=0; 2921da177e4SLinus Torvalds } 2931da177e4SLinus Torvalds this = this->next_in_ino; 2941da177e4SLinus Torvalds } 295ad361c98SJoe Perches printk(KERN_CONT "\n"); 2961da177e4SLinus Torvalds }); 2971da177e4SLinus Torvalds 298c9f700f8SKaiGai Kohei switch (ic->class) { 299c9f700f8SKaiGai Kohei #ifdef CONFIG_JFFS2_FS_XATTR 300c9f700f8SKaiGai Kohei case RAWNODE_CLASS_XATTR_DATUM: 301c9f700f8SKaiGai Kohei jffs2_release_xattr_datum(c, (struct jffs2_xattr_datum *)ic); 302c9f700f8SKaiGai Kohei break; 303c9f700f8SKaiGai Kohei case RAWNODE_CLASS_XATTR_REF: 304c9f700f8SKaiGai Kohei jffs2_release_xattr_ref(c, (struct jffs2_xattr_ref *)ic); 305c9f700f8SKaiGai Kohei break; 306c9f700f8SKaiGai Kohei #endif 307c9f700f8SKaiGai Kohei default: 30827c72b04SDavid Woodhouse if (ic->nodes == (void *)ic && ic->pino_nlink == 0) 3091da177e4SLinus Torvalds jffs2_del_ino_cache(c, ic); 3101da177e4SLinus Torvalds } 311c9f700f8SKaiGai Kohei } 3121da177e4SLinus Torvalds 313c38c1b61SDavid Woodhouse void jffs2_free_jeb_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb) 3141da177e4SLinus Torvalds { 3159bfeb691SDavid Woodhouse struct jffs2_raw_node_ref *block, *ref; 316*9c261b33SJoe Perches jffs2_dbg(1, "Freeing all node refs for eraseblock offset 0x%08x\n", 317*9c261b33SJoe Perches jeb->offset); 3181da177e4SLinus Torvalds 3199bfeb691SDavid Woodhouse block = ref = jeb->first_node; 3209bfeb691SDavid Woodhouse 3219bfeb691SDavid Woodhouse while (ref) { 3229bfeb691SDavid Woodhouse if (ref->flash_offset == REF_LINK_NODE) { 3239bfeb691SDavid Woodhouse ref = ref->next_in_ino; 3249bfeb691SDavid Woodhouse jffs2_free_refblock(block); 3259bfeb691SDavid Woodhouse block = ref; 3269bfeb691SDavid Woodhouse continue; 3279bfeb691SDavid Woodhouse } 3289bfeb691SDavid Woodhouse if (ref->flash_offset != REF_EMPTY_NODE && ref->next_in_ino) 3291da177e4SLinus Torvalds jffs2_remove_node_refs_from_ino_list(c, ref, jeb); 3301da177e4SLinus Torvalds /* else it was a non-inode node or already removed, so don't bother */ 3311da177e4SLinus Torvalds 3329bfeb691SDavid Woodhouse ref++; 3331da177e4SLinus Torvalds } 3349bfeb691SDavid Woodhouse jeb->first_node = jeb->last_node = NULL; 3351da177e4SLinus Torvalds } 3361da177e4SLinus Torvalds 3375d157885SThomas Gleixner static int jffs2_block_check_erase(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t *bad_offset) 3381da177e4SLinus Torvalds { 3395d157885SThomas Gleixner void *ebuf; 3405d157885SThomas Gleixner uint32_t ofs; 3411da177e4SLinus Torvalds size_t retlen; 342bce41d60SArtem Bityutskiy int ret; 343fab2c399SJoakim Tjernlund unsigned long *wordebuf; 344fab2c399SJoakim Tjernlund 345d35ea200SArtem Bityutskiy ret = mtd_point(c->mtd, jeb->offset, c->sector_size, &retlen, 346d35ea200SArtem Bityutskiy &ebuf, NULL); 34710934478SArtem Bityutskiy if (ret != -EOPNOTSUPP) { 348fab2c399SJoakim Tjernlund if (ret) { 349*9c261b33SJoe Perches jffs2_dbg(1, "MTD point failed %d\n", ret); 350fab2c399SJoakim Tjernlund goto do_flash_read; 351fab2c399SJoakim Tjernlund } 352fab2c399SJoakim Tjernlund if (retlen < c->sector_size) { 353fab2c399SJoakim Tjernlund /* Don't muck about if it won't let us point to the whole erase sector */ 354*9c261b33SJoe Perches jffs2_dbg(1, "MTD point returned len too short: 0x%zx\n", 355*9c261b33SJoe Perches retlen); 3567219778aSArtem Bityutskiy mtd_unpoint(c->mtd, jeb->offset, retlen); 357fab2c399SJoakim Tjernlund goto do_flash_read; 358fab2c399SJoakim Tjernlund } 359fab2c399SJoakim Tjernlund wordebuf = ebuf-sizeof(*wordebuf); 360fab2c399SJoakim Tjernlund retlen /= sizeof(*wordebuf); 361fab2c399SJoakim Tjernlund do { 362fab2c399SJoakim Tjernlund if (*++wordebuf != ~0) 363fab2c399SJoakim Tjernlund break; 364fab2c399SJoakim Tjernlund } while(--retlen); 3657219778aSArtem Bityutskiy mtd_unpoint(c->mtd, jeb->offset, c->sector_size); 3668a0f5723SAnders Grafström if (retlen) { 367f4e35647SAndrew Morton printk(KERN_WARNING "Newly-erased block contained word 0x%lx at offset 0x%08tx\n", 368fab2c399SJoakim Tjernlund *wordebuf, jeb->offset + c->sector_size-retlen*sizeof(*wordebuf)); 3698a0f5723SAnders Grafström return -EIO; 3708a0f5723SAnders Grafström } 371fab2c399SJoakim Tjernlund return 0; 372fab2c399SJoakim Tjernlund } 373fab2c399SJoakim Tjernlund do_flash_read: 3741da177e4SLinus Torvalds ebuf = kmalloc(PAGE_SIZE, GFP_KERNEL); 3751da177e4SLinus Torvalds if (!ebuf) { 3765d157885SThomas Gleixner printk(KERN_WARNING "Failed to allocate page buffer for verifying erase at 0x%08x. Refiling\n", jeb->offset); 3775d157885SThomas Gleixner return -EAGAIN; 3785d157885SThomas Gleixner } 3791da177e4SLinus Torvalds 380*9c261b33SJoe Perches jffs2_dbg(1, "Verifying erase at 0x%08x\n", jeb->offset); 3815d157885SThomas Gleixner 3825d157885SThomas Gleixner for (ofs = jeb->offset; ofs < jeb->offset + c->sector_size; ) { 3831da177e4SLinus Torvalds uint32_t readlen = min((uint32_t)PAGE_SIZE, jeb->offset + c->sector_size - ofs); 3841da177e4SLinus Torvalds int i; 3851da177e4SLinus Torvalds 3865d157885SThomas Gleixner *bad_offset = ofs; 3871da177e4SLinus Torvalds 388329ad399SArtem Bityutskiy ret = mtd_read(c->mtd, ofs, readlen, &retlen, ebuf); 3891da177e4SLinus Torvalds if (ret) { 3901da177e4SLinus Torvalds printk(KERN_WARNING "Read of newly-erased block at 0x%08x failed: %d. Putting on bad_list\n", ofs, ret); 3918a0f5723SAnders Grafström ret = -EIO; 3925d157885SThomas Gleixner goto fail; 3931da177e4SLinus Torvalds } 3941da177e4SLinus Torvalds if (retlen != readlen) { 3951da177e4SLinus Torvalds printk(KERN_WARNING "Short read from newly-erased block at 0x%08x. Wanted %d, got %zd\n", ofs, readlen, retlen); 3968a0f5723SAnders Grafström ret = -EIO; 3975d157885SThomas Gleixner goto fail; 3981da177e4SLinus Torvalds } 3991da177e4SLinus Torvalds for (i=0; i<readlen; i += sizeof(unsigned long)) { 4001da177e4SLinus Torvalds /* It's OK. We know it's properly aligned */ 4015d157885SThomas Gleixner unsigned long *datum = ebuf + i; 4025d157885SThomas Gleixner if (*datum + 1) { 4035d157885SThomas Gleixner *bad_offset += i; 4045d157885SThomas Gleixner printk(KERN_WARNING "Newly-erased block contained word 0x%lx at offset 0x%08x\n", *datum, *bad_offset); 4058a0f5723SAnders Grafström ret = -EIO; 4065d157885SThomas Gleixner goto fail; 4071da177e4SLinus Torvalds } 4081da177e4SLinus Torvalds } 4091da177e4SLinus Torvalds ofs += readlen; 4101da177e4SLinus Torvalds cond_resched(); 4111da177e4SLinus Torvalds } 4125d157885SThomas Gleixner ret = 0; 4135d157885SThomas Gleixner fail: 4141da177e4SLinus Torvalds kfree(ebuf); 4155d157885SThomas Gleixner return ret; 4161da177e4SLinus Torvalds } 4171da177e4SLinus Torvalds 4185d157885SThomas Gleixner static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb) 4195d157885SThomas Gleixner { 4205d157885SThomas Gleixner size_t retlen; 4215d157885SThomas Gleixner int ret; 422f4e35647SAndrew Morton uint32_t uninitialized_var(bad_offset); 4235d157885SThomas Gleixner 4245d157885SThomas Gleixner switch (jffs2_block_check_erase(c, jeb, &bad_offset)) { 4255d157885SThomas Gleixner case -EAGAIN: goto refile; 4265d157885SThomas Gleixner case -EIO: goto filebad; 4275d157885SThomas Gleixner } 4281da177e4SLinus Torvalds 4291da177e4SLinus Torvalds /* Write the erase complete marker */ 430*9c261b33SJoe Perches jffs2_dbg(1, "Writing erased marker to block at 0x%08x\n", jeb->offset); 4315d157885SThomas Gleixner bad_offset = jeb->offset; 4325d157885SThomas Gleixner 4335d157885SThomas Gleixner /* Cleanmarker in oob area or no cleanmarker at all ? */ 4345d157885SThomas Gleixner if (jffs2_cleanmarker_oob(c) || c->cleanmarker_size == 0) { 4355d157885SThomas Gleixner 4361da177e4SLinus Torvalds if (jffs2_cleanmarker_oob(c)) { 4371da177e4SLinus Torvalds if (jffs2_write_nand_cleanmarker(c, jeb)) 4385d157885SThomas Gleixner goto filebad; 4395d157885SThomas Gleixner } 4401da177e4SLinus Torvalds } else { 4415d157885SThomas Gleixner 4421da177e4SLinus Torvalds struct kvec vecs[1]; 4431da177e4SLinus Torvalds struct jffs2_unknown_node marker = { 4441da177e4SLinus Torvalds .magic = cpu_to_je16(JFFS2_MAGIC_BITMASK), 4451da177e4SLinus Torvalds .nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER), 4461da177e4SLinus Torvalds .totlen = cpu_to_je32(c->cleanmarker_size) 4471da177e4SLinus Torvalds }; 4481da177e4SLinus Torvalds 449046b8b98SDavid Woodhouse jffs2_prealloc_raw_node_refs(c, jeb, 1); 4505d157885SThomas Gleixner 4511da177e4SLinus Torvalds marker.hdr_crc = cpu_to_je32(crc32(0, &marker, sizeof(struct jffs2_unknown_node)-4)); 4521da177e4SLinus Torvalds 4531da177e4SLinus Torvalds vecs[0].iov_base = (unsigned char *) ▮ 4541da177e4SLinus Torvalds vecs[0].iov_len = sizeof(marker); 4551da177e4SLinus Torvalds ret = jffs2_flash_direct_writev(c, vecs, 1, jeb->offset, &retlen); 4561da177e4SLinus Torvalds 4575d157885SThomas Gleixner if (ret || retlen != sizeof(marker)) { 4585d157885SThomas Gleixner if (ret) 4591da177e4SLinus Torvalds printk(KERN_WARNING "Write clean marker to block at 0x%08x failed: %d\n", 4601da177e4SLinus Torvalds jeb->offset, ret); 4615d157885SThomas Gleixner else 4621da177e4SLinus Torvalds printk(KERN_WARNING "Short write to newly-erased block at 0x%08x: Wanted %zd, got %zd\n", 4631da177e4SLinus Torvalds jeb->offset, sizeof(marker), retlen); 4645d157885SThomas Gleixner 4655d157885SThomas Gleixner goto filebad; 4661da177e4SLinus Torvalds } 467014b164eSDavid Woodhouse } 468f1f9671bSDavid Woodhouse /* Everything else got zeroed before the erase */ 469f1f9671bSDavid Woodhouse jeb->free_size = c->sector_size; 4701da177e4SLinus Torvalds 471ced22070SDavid Woodhouse mutex_lock(&c->erase_free_sem); 4721da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 473014b164eSDavid Woodhouse 4741da177e4SLinus Torvalds c->erasing_size -= c->sector_size; 475014b164eSDavid Woodhouse c->free_size += c->sector_size; 476014b164eSDavid Woodhouse 477014b164eSDavid Woodhouse /* Account for cleanmarker now, if it's in-band */ 478014b164eSDavid Woodhouse if (c->cleanmarker_size && !jffs2_cleanmarker_oob(c)) 479014b164eSDavid Woodhouse jffs2_link_node_ref(c, jeb, jeb->offset | REF_NORMAL, c->cleanmarker_size, NULL); 4801da177e4SLinus Torvalds 481e2bc322bSDavid Woodhouse list_move_tail(&jeb->list, &c->free_list); 4821da177e4SLinus Torvalds c->nr_erasing_blocks--; 4831da177e4SLinus Torvalds c->nr_free_blocks++; 48485a62db6SDavid Woodhouse 48585a62db6SDavid Woodhouse jffs2_dbg_acct_sanity_check_nolock(c, jeb); 48685a62db6SDavid Woodhouse jffs2_dbg_acct_paranoia_check_nolock(c, jeb); 48785a62db6SDavid Woodhouse 4881da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 489ced22070SDavid Woodhouse mutex_unlock(&c->erase_free_sem); 4901da177e4SLinus Torvalds wake_up(&c->erase_wait); 4915d157885SThomas Gleixner return; 4921da177e4SLinus Torvalds 4935d157885SThomas Gleixner filebad: 4945d157885SThomas Gleixner jffs2_erase_failed(c, jeb, bad_offset); 4955d157885SThomas Gleixner return; 4965d157885SThomas Gleixner 4975d157885SThomas Gleixner refile: 4985d157885SThomas Gleixner /* Stick it back on the list from whence it came and come back later */ 499ced22070SDavid Woodhouse mutex_lock(&c->erase_free_sem); 5005d157885SThomas Gleixner spin_lock(&c->erase_completion_lock); 501ae3b6ba0SDavid Woodhouse jffs2_garbage_collect_trigger(c); 502e2bc322bSDavid Woodhouse list_move(&jeb->list, &c->erase_complete_list); 5035d157885SThomas Gleixner spin_unlock(&c->erase_completion_lock); 504ced22070SDavid Woodhouse mutex_unlock(&c->erase_free_sem); 5055d157885SThomas Gleixner return; 5065d157885SThomas Gleixner } 507