initial commit with v2.6.9
[linux-2.6.9-moxart.git] / fs / jffs2 / erase.c
blobf6efdbabbf3e20b674cf8ac086a9574356265fd1
1 /*
2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright (C) 2001-2003 Red Hat, Inc.
6 * Created by David Woodhouse <dwmw2@redhat.com>
8 * For licensing information, see the file 'LICENCE' in this directory.
10 * $Id: erase.c,v 1.60 2004/06/30 17:26:15 dbrown 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>
21 #include "nodelist.h"
23 struct erase_priv_struct {
24 struct jffs2_eraseblock *jeb;
25 struct jffs2_sb_info *c;
28 #ifndef __ECOS
29 static void jffs2_erase_callback(struct erase_info *);
30 #endif
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_free_all_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
34 static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
36 void jffs2_erase_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
38 int ret;
39 uint32_t bad_offset;
40 #ifdef __ECOS
41 ret = jffs2_flash_erase(c, jeb);
42 if (!ret) {
43 jffs2_erase_succeeded(c, jeb);
44 return;
46 #else /* Linux */
47 struct erase_info *instr;
49 instr = kmalloc(sizeof(struct erase_info) + sizeof(struct erase_priv_struct), GFP_KERNEL);
50 if (!instr) {
51 printk(KERN_WARNING "kmalloc for struct erase_info in jffs2_erase_block failed. Refiling block for later\n");
52 spin_lock(&c->erase_completion_lock);
53 list_del(&jeb->list);
54 list_add(&jeb->list, &c->erase_pending_list);
55 c->erasing_size -= c->sector_size;
56 c->dirty_size += c->sector_size;
57 jeb->dirty_size = c->sector_size;
58 spin_unlock(&c->erase_completion_lock);
59 return;
62 memset(instr, 0, sizeof(*instr));
64 instr->mtd = c->mtd;
65 instr->addr = jeb->offset;
66 instr->len = c->sector_size;
67 instr->callback = jffs2_erase_callback;
68 instr->priv = (unsigned long)(&instr[1]);
69 instr->fail_addr = 0xffffffff;
71 ((struct erase_priv_struct *)instr->priv)->jeb = jeb;
72 ((struct erase_priv_struct *)instr->priv)->c = c;
74 ret = c->mtd->erase(c->mtd, instr);
75 if (!ret)
76 return;
78 bad_offset = instr->fail_addr;
79 kfree(instr);
80 #endif /* __ECOS */
82 if (ret == -ENOMEM || ret == -EAGAIN) {
83 /* Erase failed immediately. Refile it on the list */
84 D1(printk(KERN_DEBUG "Erase at 0x%08x failed: %d. Refiling on erase_pending_list\n", jeb->offset, ret));
85 spin_lock(&c->erase_completion_lock);
86 list_del(&jeb->list);
87 list_add(&jeb->list, &c->erase_pending_list);
88 c->erasing_size -= c->sector_size;
89 c->dirty_size += c->sector_size;
90 jeb->dirty_size = c->sector_size;
91 spin_unlock(&c->erase_completion_lock);
92 return;
95 if (ret == -EROFS)
96 printk(KERN_WARNING "Erase at 0x%08x failed immediately: -EROFS. Is the sector locked?\n", jeb->offset);
97 else
98 printk(KERN_WARNING "Erase at 0x%08x failed immediately: errno %d\n", jeb->offset, ret);
100 jffs2_erase_failed(c, jeb, bad_offset);
103 void jffs2_erase_pending_blocks(struct jffs2_sb_info *c, int count)
105 struct jffs2_eraseblock *jeb;
107 down(&c->erase_free_sem);
109 spin_lock(&c->erase_completion_lock);
111 while (!list_empty(&c->erase_complete_list) ||
112 !list_empty(&c->erase_pending_list)) {
114 if (!list_empty(&c->erase_complete_list)) {
115 jeb = list_entry(c->erase_complete_list.next, struct jffs2_eraseblock, list);
116 list_del(&jeb->list);
117 spin_unlock(&c->erase_completion_lock);
118 jffs2_mark_erased_block(c, jeb);
120 if (!--count) {
121 D1(printk(KERN_DEBUG "Count reached. jffs2_erase_pending_blocks leaving\n"));
122 goto done;
125 } else if (!list_empty(&c->erase_pending_list)) {
126 jeb = list_entry(c->erase_pending_list.next, struct jffs2_eraseblock, list);
127 D1(printk(KERN_DEBUG "Starting erase of pending block 0x%08x\n", jeb->offset));
128 list_del(&jeb->list);
129 c->erasing_size += c->sector_size;
130 c->wasted_size -= jeb->wasted_size;
131 c->free_size -= jeb->free_size;
132 c->used_size -= jeb->used_size;
133 c->dirty_size -= jeb->dirty_size;
134 jeb->wasted_size = jeb->used_size = jeb->dirty_size = jeb->free_size = 0;
135 jffs2_free_all_node_refs(c, jeb);
136 list_add(&jeb->list, &c->erasing_list);
137 spin_unlock(&c->erase_completion_lock);
139 jffs2_erase_block(c, jeb);
141 } else {
142 BUG();
145 /* Be nice */
146 cond_resched();
147 spin_lock(&c->erase_completion_lock);
150 spin_unlock(&c->erase_completion_lock);
151 done:
152 D1(printk(KERN_DEBUG "jffs2_erase_pending_blocks completed\n"));
154 up(&c->erase_free_sem);
157 static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
159 D1(printk(KERN_DEBUG "Erase completed successfully at 0x%08x\n", jeb->offset));
160 spin_lock(&c->erase_completion_lock);
161 list_del(&jeb->list);
162 list_add_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_del(&jeb->list);
179 list_add(&jeb->list, &c->erase_pending_list);
180 c->erasing_size -= c->sector_size;
181 c->dirty_size += c->sector_size;
182 jeb->dirty_size = c->sector_size;
183 spin_unlock(&c->erase_completion_lock);
184 return;
188 spin_lock(&c->erase_completion_lock);
189 c->erasing_size -= c->sector_size;
190 c->bad_size += c->sector_size;
191 list_del(&jeb->list);
192 list_add(&jeb->list, &c->bad_list);
193 c->nr_erasing_blocks--;
194 spin_unlock(&c->erase_completion_lock);
195 wake_up(&c->erase_wait);
198 #ifndef __ECOS
199 static void jffs2_erase_callback(struct erase_info *instr)
201 struct erase_priv_struct *priv = (void *)instr->priv;
203 if(instr->state != MTD_ERASE_DONE) {
204 printk(KERN_WARNING "Erase at 0x%08x finished, but state != MTD_ERASE_DONE. State is 0x%x instead.\n", instr->addr, instr->state);
205 jffs2_erase_failed(priv->c, priv->jeb, instr->fail_addr);
206 } else {
207 jffs2_erase_succeeded(priv->c, priv->jeb);
209 kfree(instr);
211 #endif /* !__ECOS */
213 /* Hmmm. Maybe we should accept the extra space it takes and make
214 this a standard doubly-linked list? */
215 static inline void jffs2_remove_node_refs_from_ino_list(struct jffs2_sb_info *c,
216 struct jffs2_raw_node_ref *ref, struct jffs2_eraseblock *jeb)
218 struct jffs2_inode_cache *ic = NULL;
219 struct jffs2_raw_node_ref **prev;
221 prev = &ref->next_in_ino;
223 /* Walk the inode's list once, removing any nodes from this eraseblock */
224 while (1) {
225 if (!(*prev)->next_in_ino) {
226 /* We're looking at the jffs2_inode_cache, which is
227 at the end of the linked list. Stash it and continue
228 from the beginning of the list */
229 ic = (struct jffs2_inode_cache *)(*prev);
230 prev = &ic->nodes;
231 continue;
234 if (((*prev)->flash_offset & ~(c->sector_size -1)) == jeb->offset) {
235 /* It's in the block we're erasing */
236 struct jffs2_raw_node_ref *this;
238 this = *prev;
239 *prev = this->next_in_ino;
240 this->next_in_ino = NULL;
242 if (this == ref)
243 break;
245 continue;
247 /* Not to be deleted. Skip */
248 prev = &((*prev)->next_in_ino);
251 /* PARANOIA */
252 if (!ic) {
253 printk(KERN_WARNING "inode_cache not found in remove_node_refs()!!\n");
254 return;
257 D1(printk(KERN_DEBUG "Removed nodes in range 0x%08x-0x%08x from ino #%u\n",
258 jeb->offset, jeb->offset + c->sector_size, ic->ino));
260 D2({
261 int i=0;
262 struct jffs2_raw_node_ref *this;
263 printk(KERN_DEBUG "After remove_node_refs_from_ino_list: \n" KERN_DEBUG);
265 this = ic->nodes;
267 while(this) {
268 printk( "0x%08x(%d)->", ref_offset(this), ref_flags(this));
269 if (++i == 5) {
270 printk("\n" KERN_DEBUG);
271 i=0;
273 this = this->next_in_ino;
275 printk("\n");
278 if (ic->nodes == (void *)ic) {
279 D1(printk(KERN_DEBUG "inocache for ino #%u is all gone now. Freeing\n", ic->ino));
280 jffs2_del_ino_cache(c, ic);
281 jffs2_free_inode_cache(ic);
285 static void jffs2_free_all_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
287 struct jffs2_raw_node_ref *ref;
288 D1(printk(KERN_DEBUG "Freeing all node refs for eraseblock offset 0x%08x\n", jeb->offset));
289 while(jeb->first_node) {
290 ref = jeb->first_node;
291 jeb->first_node = ref->next_phys;
293 /* Remove from the inode-list */
294 if (ref->next_in_ino)
295 jffs2_remove_node_refs_from_ino_list(c, ref, jeb);
296 /* else it was a non-inode node or already removed, so don't bother */
298 jffs2_free_raw_node_ref(ref);
300 jeb->last_node = NULL;
303 static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
305 struct jffs2_raw_node_ref *marker_ref = NULL;
306 unsigned char *ebuf;
307 size_t retlen;
308 int ret;
309 uint32_t bad_offset;
311 if (!jffs2_cleanmarker_oob(c)) {
312 marker_ref = jffs2_alloc_raw_node_ref();
313 if (!marker_ref) {
314 printk(KERN_WARNING "Failed to allocate raw node ref for clean marker\n");
315 /* Stick it back on the list from whence it came and come back later */
316 jffs2_erase_pending_trigger(c);
317 spin_lock(&c->erase_completion_lock);
318 list_add(&jeb->list, &c->erase_complete_list);
319 spin_unlock(&c->erase_completion_lock);
320 return;
323 ebuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
324 if (!ebuf) {
325 printk(KERN_WARNING "Failed to allocate page buffer for verifying erase at 0x%08x. Assuming it worked\n", jeb->offset);
326 } else {
327 uint32_t ofs = jeb->offset;
329 D1(printk(KERN_DEBUG "Verifying erase at 0x%08x\n", jeb->offset));
330 while(ofs < jeb->offset + c->sector_size) {
331 uint32_t readlen = min((uint32_t)PAGE_SIZE, jeb->offset + c->sector_size - ofs);
332 int i;
334 bad_offset = ofs;
336 ret = jffs2_flash_read(c, ofs, readlen, &retlen, ebuf);
337 if (ret) {
338 printk(KERN_WARNING "Read of newly-erased block at 0x%08x failed: %d. Putting on bad_list\n", ofs, ret);
339 goto bad;
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);
343 goto bad;
345 for (i=0; i<readlen; i += sizeof(unsigned long)) {
346 /* It's OK. We know it's properly aligned */
347 unsigned long datum = *(unsigned long *)(&ebuf[i]);
348 if (datum + 1) {
349 bad_offset += i;
350 printk(KERN_WARNING "Newly-erased block contained word 0x%lx at offset 0x%08x\n", datum, bad_offset);
351 bad:
352 if (!jffs2_cleanmarker_oob(c))
353 jffs2_free_raw_node_ref(marker_ref);
354 kfree(ebuf);
355 bad2:
356 spin_lock(&c->erase_completion_lock);
357 /* Stick it on a list (any list) so
358 erase_failed can take it right off
359 again. Silly, but shouldn't happen
360 often. */
361 list_add(&jeb->list, &c->erasing_list);
362 spin_unlock(&c->erase_completion_lock);
363 jffs2_erase_failed(c, jeb, bad_offset);
364 return;
367 ofs += readlen;
368 cond_resched();
370 kfree(ebuf);
373 bad_offset = jeb->offset;
375 /* Write the erase complete marker */
376 D1(printk(KERN_DEBUG "Writing erased marker to block at 0x%08x\n", jeb->offset));
377 if (jffs2_cleanmarker_oob(c)) {
379 if (jffs2_write_nand_cleanmarker(c, jeb))
380 goto bad2;
382 jeb->first_node = jeb->last_node = NULL;
384 jeb->free_size = c->sector_size;
385 jeb->used_size = 0;
386 jeb->dirty_size = 0;
387 jeb->wasted_size = 0;
388 } else {
389 struct jffs2_unknown_node marker = {
390 .magic = cpu_to_je16(JFFS2_MAGIC_BITMASK),
391 .nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER),
392 .totlen = cpu_to_je32(c->cleanmarker_size)
395 marker.hdr_crc = cpu_to_je32(crc32(0, &marker, sizeof(struct jffs2_unknown_node)-4));
397 /* We only write the header; the rest was noise or padding anyway */
398 ret = jffs2_flash_write(c, jeb->offset, sizeof(marker), &retlen, (char *)&marker);
399 if (ret) {
400 printk(KERN_WARNING "Write clean marker to block at 0x%08x failed: %d\n",
401 jeb->offset, ret);
402 goto bad2;
404 if (retlen != sizeof(marker)) {
405 printk(KERN_WARNING "Short write to newly-erased block at 0x%08x: Wanted %zd, got %zd\n",
406 jeb->offset, sizeof(marker), retlen);
407 goto bad2;
410 marker_ref->next_in_ino = NULL;
411 marker_ref->next_phys = NULL;
412 marker_ref->flash_offset = jeb->offset | REF_NORMAL;
413 marker_ref->__totlen = c->cleanmarker_size;
415 jeb->first_node = jeb->last_node = marker_ref;
417 jeb->free_size = c->sector_size - c->cleanmarker_size;
418 jeb->used_size = c->cleanmarker_size;
419 jeb->dirty_size = 0;
420 jeb->wasted_size = 0;
423 spin_lock(&c->erase_completion_lock);
424 c->erasing_size -= c->sector_size;
425 c->free_size += jeb->free_size;
426 c->used_size += jeb->used_size;
428 ACCT_SANITY_CHECK(c,jeb);
429 D1(ACCT_PARANOIA_CHECK(jeb));
431 list_add_tail(&jeb->list, &c->free_list);
432 c->nr_erasing_blocks--;
433 c->nr_free_blocks++;
434 spin_unlock(&c->erase_completion_lock);
435 wake_up(&c->erase_wait);