2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright (C) 2001-2003 Red Hat, Inc.
6 * Created by David Woodhouse <dwmw2@infradead.org>
8 * For licensing information, see the file 'LICENCE' in this directory.
10 * $Id: erase.c,v 1.85 2005/09/20 14:53:15 dedekind Exp $
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/mtd/mtd.h>
17 #include <linux/compiler.h>
18 #include <linux/crc32.h>
19 #include <linux/sched.h>
20 #include <linux/pagemap.h>
23 struct erase_priv_struct {
24 struct jffs2_eraseblock *jeb;
25 struct jffs2_sb_info *c;
29 static void jffs2_erase_callback(struct erase_info *);
31 static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset);
32 static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
33 static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
35 static void jffs2_erase_block(struct jffs2_sb_info *c,
36 struct jffs2_eraseblock *jeb)
41 ret = jffs2_flash_erase(c, jeb);
43 jffs2_erase_succeeded(c, jeb);
46 bad_offset = jeb->offset;
48 struct erase_info *instr;
50 D1(printk(KERN_DEBUG "jffs2_erase_block(): erase block %#08x (range %#08x-%#08x)\n",
51 jeb->offset, jeb->offset, jeb->offset + c->sector_size));
52 instr = kmalloc(sizeof(struct erase_info) + sizeof(struct erase_priv_struct), GFP_KERNEL);
54 printk(KERN_WARNING "kmalloc for struct erase_info in jffs2_erase_block failed. Refiling block for later\n");
55 spin_lock(&c->erase_completion_lock);
56 list_move(&jeb->list, &c->erase_pending_list);
57 c->erasing_size -= c->sector_size;
58 c->dirty_size += c->sector_size;
59 jeb->dirty_size = c->sector_size;
60 spin_unlock(&c->erase_completion_lock);
64 memset(instr, 0, sizeof(*instr));
67 instr->addr = jeb->offset;
68 instr->len = c->sector_size;
69 instr->callback = jffs2_erase_callback;
70 instr->priv = (unsigned long)(&instr[1]);
71 instr->fail_addr = 0xffffffff;
73 ((struct erase_priv_struct *)instr->priv)->jeb = jeb;
74 ((struct erase_priv_struct *)instr->priv)->c = c;
76 ret = c->mtd->erase(c->mtd, instr);
80 bad_offset = instr->fail_addr;
84 if (ret == -ENOMEM || ret == -EAGAIN) {
85 /* Erase failed immediately. Refile it on the list */
86 D1(printk(KERN_DEBUG "Erase at 0x%08x failed: %d. Refiling on erase_pending_list\n", jeb->offset, ret));
87 spin_lock(&c->erase_completion_lock);
88 list_move(&jeb->list, &c->erase_pending_list);
89 c->erasing_size -= c->sector_size;
90 c->dirty_size += c->sector_size;
91 jeb->dirty_size = c->sector_size;
92 spin_unlock(&c->erase_completion_lock);
97 printk(KERN_WARNING "Erase at 0x%08x failed immediately: -EROFS. Is the sector locked?\n", jeb->offset);
99 printk(KERN_WARNING "Erase at 0x%08x failed immediately: errno %d\n", jeb->offset, ret);
101 jffs2_erase_failed(c, jeb, bad_offset);
104 void jffs2_erase_pending_blocks(struct jffs2_sb_info *c, int count)
106 struct jffs2_eraseblock *jeb;
108 down(&c->erase_free_sem);
110 spin_lock(&c->erase_completion_lock);
112 while (!list_empty(&c->erase_complete_list) ||
113 !list_empty(&c->erase_pending_list)) {
115 if (!list_empty(&c->erase_complete_list)) {
116 jeb = list_entry(c->erase_complete_list.next, struct jffs2_eraseblock, list);
117 list_del(&jeb->list);
118 spin_unlock(&c->erase_completion_lock);
119 jffs2_mark_erased_block(c, jeb);
122 D1(printk(KERN_DEBUG "Count reached. jffs2_erase_pending_blocks leaving\n"));
126 } else if (!list_empty(&c->erase_pending_list)) {
127 jeb = list_entry(c->erase_pending_list.next, struct jffs2_eraseblock, list);
128 D1(printk(KERN_DEBUG "Starting erase of pending block 0x%08x\n", jeb->offset));
129 list_del(&jeb->list);
130 c->erasing_size += c->sector_size;
131 c->wasted_size -= jeb->wasted_size;
132 c->free_size -= jeb->free_size;
133 c->used_size -= jeb->used_size;
134 c->dirty_size -= jeb->dirty_size;
135 jeb->wasted_size = jeb->used_size = jeb->dirty_size = jeb->free_size = 0;
136 jffs2_free_jeb_node_refs(c, jeb);
137 list_add(&jeb->list, &c->erasing_list);
138 spin_unlock(&c->erase_completion_lock);
140 jffs2_erase_block(c, jeb);
148 spin_lock(&c->erase_completion_lock);
151 spin_unlock(&c->erase_completion_lock);
153 D1(printk(KERN_DEBUG "jffs2_erase_pending_blocks completed\n"));
155 up(&c->erase_free_sem);
158 static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
160 D1(printk(KERN_DEBUG "Erase completed successfully at 0x%08x\n", jeb->offset));
161 spin_lock(&c->erase_completion_lock);
162 list_move_tail(&jeb->list, &c->erase_complete_list);
163 spin_unlock(&c->erase_completion_lock);
164 /* Ensure that kupdated calls us again to mark them clean */
165 jffs2_erase_pending_trigger(c);
168 static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset)
170 /* For NAND, if the failure did not occur at the device level for a
171 specific physical page, don't bother updating the bad block table. */
172 if (jffs2_cleanmarker_oob(c) && (bad_offset != 0xffffffff)) {
173 /* We had a device-level failure to erase. Let's see if we've
174 failed too many times. */
175 if (!jffs2_write_nand_badblock(c, jeb, bad_offset)) {
176 /* We'd like to give this block another try. */
177 spin_lock(&c->erase_completion_lock);
178 list_move(&jeb->list, &c->erase_pending_list);
179 c->erasing_size -= c->sector_size;
180 c->dirty_size += c->sector_size;
181 jeb->dirty_size = c->sector_size;
182 spin_unlock(&c->erase_completion_lock);
187 spin_lock(&c->erase_completion_lock);
188 c->erasing_size -= c->sector_size;
189 c->bad_size += c->sector_size;
190 list_move(&jeb->list, &c->bad_list);
191 c->nr_erasing_blocks--;
192 spin_unlock(&c->erase_completion_lock);
193 wake_up(&c->erase_wait);
197 static void jffs2_erase_callback(struct erase_info *instr)
199 struct erase_priv_struct *priv = (void *)instr->priv;
201 if(instr->state != MTD_ERASE_DONE) {
202 printk(KERN_WARNING "Erase at 0x%08x finished, but state != MTD_ERASE_DONE. State is 0x%x instead.\n", instr->addr, instr->state);
203 jffs2_erase_failed(priv->c, priv->jeb, instr->fail_addr);
205 jffs2_erase_succeeded(priv->c, priv->jeb);
211 /* Hmmm. Maybe we should accept the extra space it takes and make
212 this a standard doubly-linked list? */
213 static inline void jffs2_remove_node_refs_from_ino_list(struct jffs2_sb_info *c,
214 struct jffs2_raw_node_ref *ref, struct jffs2_eraseblock *jeb)
216 struct jffs2_inode_cache *ic = NULL;
217 struct jffs2_raw_node_ref **prev;
219 prev = &ref->next_in_ino;
221 /* Walk the inode's list once, removing any nodes from this eraseblock */
223 if (!(*prev)->next_in_ino) {
224 /* We're looking at the jffs2_inode_cache, which is
225 at the end of the linked list. Stash it and continue
226 from the beginning of the list */
227 ic = (struct jffs2_inode_cache *)(*prev);
232 if (SECTOR_ADDR((*prev)->flash_offset) == jeb->offset) {
233 /* It's in the block we're erasing */
234 struct jffs2_raw_node_ref *this;
237 *prev = this->next_in_ino;
238 this->next_in_ino = NULL;
245 /* Not to be deleted. Skip */
246 prev = &((*prev)->next_in_ino);
251 JFFS2_WARNING("inode_cache/xattr_datum/xattr_ref"
252 " not found in remove_node_refs()!!\n");
256 D1(printk(KERN_DEBUG "Removed nodes in range 0x%08x-0x%08x from ino #%u\n",
257 jeb->offset, jeb->offset + c->sector_size, ic->ino));
261 struct jffs2_raw_node_ref *this;
262 printk(KERN_DEBUG "After remove_node_refs_from_ino_list: \n" KERN_DEBUG);
267 printk( "0x%08x(%d)->", ref_offset(this), ref_flags(this));
269 printk("\n" KERN_DEBUG);
272 this = this->next_in_ino;
278 #ifdef CONFIG_JFFS2_FS_XATTR
279 case RAWNODE_CLASS_XATTR_DATUM:
280 jffs2_release_xattr_datum(c, (struct jffs2_xattr_datum *)ic);
282 case RAWNODE_CLASS_XATTR_REF:
283 jffs2_release_xattr_ref(c, (struct jffs2_xattr_ref *)ic);
287 if (ic->nodes == (void *)ic && ic->nlink == 0)
288 jffs2_del_ino_cache(c, ic);
292 void jffs2_free_jeb_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
294 struct jffs2_raw_node_ref *block, *ref;
295 D1(printk(KERN_DEBUG "Freeing all node refs for eraseblock offset 0x%08x\n", jeb->offset));
297 block = ref = jeb->first_node;
300 if (ref->flash_offset == REF_LINK_NODE) {
301 ref = ref->next_in_ino;
302 jffs2_free_refblock(block);
306 if (ref->flash_offset != REF_EMPTY_NODE && ref->next_in_ino)
307 jffs2_remove_node_refs_from_ino_list(c, ref, jeb);
308 /* else it was a non-inode node or already removed, so don't bother */
312 jeb->first_node = jeb->last_node = NULL;
315 static int jffs2_block_check_erase(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t *bad_offset)
322 ebuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
324 printk(KERN_WARNING "Failed to allocate page buffer for verifying erase at 0x%08x. Refiling\n", jeb->offset);
328 D1(printk(KERN_DEBUG "Verifying erase at 0x%08x\n", jeb->offset));
330 for (ofs = jeb->offset; ofs < jeb->offset + c->sector_size; ) {
331 uint32_t readlen = min((uint32_t)PAGE_SIZE, jeb->offset + c->sector_size - ofs);
336 ret = jffs2_flash_read(c, ofs, readlen, &retlen, ebuf);
338 printk(KERN_WARNING "Read of newly-erased block at 0x%08x failed: %d. Putting on bad_list\n", ofs, ret);
341 if (retlen != readlen) {
342 printk(KERN_WARNING "Short read from newly-erased block at 0x%08x. Wanted %d, got %zd\n", ofs, readlen, retlen);
345 for (i=0; i<readlen; i += sizeof(unsigned long)) {
346 /* It's OK. We know it's properly aligned */
347 unsigned long *datum = ebuf + i;
350 printk(KERN_WARNING "Newly-erased block contained word 0x%lx at offset 0x%08x\n", *datum, *bad_offset);
363 static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
369 switch (jffs2_block_check_erase(c, jeb, &bad_offset)) {
370 case -EAGAIN: goto refile;
371 case -EIO: goto filebad;
374 /* Write the erase complete marker */
375 D1(printk(KERN_DEBUG "Writing erased marker to block at 0x%08x\n", jeb->offset));
376 bad_offset = jeb->offset;
378 /* Cleanmarker in oob area or no cleanmarker at all ? */
379 if (jffs2_cleanmarker_oob(c) || c->cleanmarker_size == 0) {
381 if (jffs2_cleanmarker_oob(c)) {
382 if (jffs2_write_nand_cleanmarker(c, jeb))
386 /* Everything else got zeroed before the erase */
387 jeb->free_size = c->sector_size;
391 struct jffs2_unknown_node marker = {
392 .magic = cpu_to_je16(JFFS2_MAGIC_BITMASK),
393 .nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER),
394 .totlen = cpu_to_je32(c->cleanmarker_size)
397 jffs2_prealloc_raw_node_refs(c, jeb, 1);
399 marker.hdr_crc = cpu_to_je32(crc32(0, &marker, sizeof(struct jffs2_unknown_node)-4));
401 vecs[0].iov_base = (unsigned char *) ▮
402 vecs[0].iov_len = sizeof(marker);
403 ret = jffs2_flash_direct_writev(c, vecs, 1, jeb->offset, &retlen);
405 if (ret || retlen != sizeof(marker)) {
407 printk(KERN_WARNING "Write clean marker to block at 0x%08x failed: %d\n",
410 printk(KERN_WARNING "Short write to newly-erased block at 0x%08x: Wanted %zd, got %zd\n",
411 jeb->offset, sizeof(marker), retlen);
416 /* Everything else got zeroed before the erase */
417 jeb->free_size = c->sector_size;
418 /* FIXME Special case for cleanmarker in empty block */
419 jffs2_link_node_ref(c, jeb, jeb->offset | REF_NORMAL, c->cleanmarker_size, NULL);
422 spin_lock(&c->erase_completion_lock);
423 c->erasing_size -= c->sector_size;
424 c->free_size += jeb->free_size;
425 c->used_size += jeb->used_size;
427 jffs2_dbg_acct_sanity_check_nolock(c,jeb);
428 jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
430 list_add_tail(&jeb->list, &c->free_list);
431 c->nr_erasing_blocks--;
433 spin_unlock(&c->erase_completion_lock);
434 wake_up(&c->erase_wait);
438 spin_lock(&c->erase_completion_lock);
439 /* Stick it on a list (any list) so erase_failed can take it
440 right off again. Silly, but shouldn't happen often. */
441 list_add(&jeb->list, &c->erasing_list);
442 spin_unlock(&c->erase_completion_lock);
443 jffs2_erase_failed(c, jeb, bad_offset);
447 /* Stick it back on the list from whence it came and come back later */
448 jffs2_erase_pending_trigger(c);
449 spin_lock(&c->erase_completion_lock);
450 list_add(&jeb->list, &c->erase_complete_list);
451 spin_unlock(&c->erase_completion_lock);