blob: dad68fdffe9e3e3ac8662fcc1a98dd19126b0152 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * JFFS2 -- Journalling Flash File System, Version 2.
3 *
4 * Copyright (C) 2001-2003 Red Hat, Inc.
5 *
6 * Created by David Woodhouse <dwmw2@infradead.org>
7 *
8 * For licensing information, see the file 'LICENCE' in this directory.
9 *
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +010010 * $Id: erase.c,v 1.85 2005/09/20 14:53:15 dedekind Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 */
13
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"
22
23struct erase_priv_struct {
24 struct jffs2_eraseblock *jeb;
25 struct jffs2_sb_info *c;
26};
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#ifndef __ECOS
29static void jffs2_erase_callback(struct erase_info *);
30#endif
31static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset);
32static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
33static void jffs2_free_all_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
34static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
35
36static void jffs2_erase_block(struct jffs2_sb_info *c,
37 struct jffs2_eraseblock *jeb)
38{
39 int ret;
40 uint32_t bad_offset;
41#ifdef __ECOS
42 ret = jffs2_flash_erase(c, jeb);
43 if (!ret) {
44 jffs2_erase_succeeded(c, jeb);
45 return;
46 }
47 bad_offset = jeb->offset;
48#else /* Linux */
49 struct erase_info *instr;
50
Artem B. Bityutskiye0c8e422005-07-24 16:14:17 +010051 D1(printk(KERN_DEBUG "jffs2_erase_block(): erase block %#08x (range %#08x-%#08x)\n",
52 jeb->offset, jeb->offset, jeb->offset + c->sector_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 instr = kmalloc(sizeof(struct erase_info) + sizeof(struct erase_priv_struct), GFP_KERNEL);
54 if (!instr) {
55 printk(KERN_WARNING "kmalloc for struct erase_info in jffs2_erase_block failed. Refiling block for later\n");
56 spin_lock(&c->erase_completion_lock);
57 list_del(&jeb->list);
58 list_add(&jeb->list, &c->erase_pending_list);
59 c->erasing_size -= c->sector_size;
60 c->dirty_size += c->sector_size;
61 jeb->dirty_size = c->sector_size;
62 spin_unlock(&c->erase_completion_lock);
63 return;
64 }
65
66 memset(instr, 0, sizeof(*instr));
67
68 instr->mtd = c->mtd;
69 instr->addr = jeb->offset;
70 instr->len = c->sector_size;
71 instr->callback = jffs2_erase_callback;
72 instr->priv = (unsigned long)(&instr[1]);
73 instr->fail_addr = 0xffffffff;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 ((struct erase_priv_struct *)instr->priv)->jeb = jeb;
76 ((struct erase_priv_struct *)instr->priv)->c = c;
77
78 ret = c->mtd->erase(c->mtd, instr);
79 if (!ret)
80 return;
81
82 bad_offset = instr->fail_addr;
83 kfree(instr);
84#endif /* __ECOS */
85
86 if (ret == -ENOMEM || ret == -EAGAIN) {
87 /* Erase failed immediately. Refile it on the list */
88 D1(printk(KERN_DEBUG "Erase at 0x%08x failed: %d. Refiling on erase_pending_list\n", jeb->offset, ret));
89 spin_lock(&c->erase_completion_lock);
90 list_del(&jeb->list);
91 list_add(&jeb->list, &c->erase_pending_list);
92 c->erasing_size -= c->sector_size;
93 c->dirty_size += c->sector_size;
94 jeb->dirty_size = c->sector_size;
95 spin_unlock(&c->erase_completion_lock);
96 return;
97 }
98
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000099 if (ret == -EROFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 printk(KERN_WARNING "Erase at 0x%08x failed immediately: -EROFS. Is the sector locked?\n", jeb->offset);
101 else
102 printk(KERN_WARNING "Erase at 0x%08x failed immediately: errno %d\n", jeb->offset, ret);
103
104 jffs2_erase_failed(c, jeb, bad_offset);
105}
106
107void jffs2_erase_pending_blocks(struct jffs2_sb_info *c, int count)
108{
109 struct jffs2_eraseblock *jeb;
110
111 down(&c->erase_free_sem);
112
113 spin_lock(&c->erase_completion_lock);
114
115 while (!list_empty(&c->erase_complete_list) ||
116 !list_empty(&c->erase_pending_list)) {
117
118 if (!list_empty(&c->erase_complete_list)) {
119 jeb = list_entry(c->erase_complete_list.next, struct jffs2_eraseblock, list);
120 list_del(&jeb->list);
121 spin_unlock(&c->erase_completion_lock);
122 jffs2_mark_erased_block(c, jeb);
123
124 if (!--count) {
125 D1(printk(KERN_DEBUG "Count reached. jffs2_erase_pending_blocks leaving\n"));
126 goto done;
127 }
128
129 } else if (!list_empty(&c->erase_pending_list)) {
130 jeb = list_entry(c->erase_pending_list.next, struct jffs2_eraseblock, list);
131 D1(printk(KERN_DEBUG "Starting erase of pending block 0x%08x\n", jeb->offset));
132 list_del(&jeb->list);
133 c->erasing_size += c->sector_size;
134 c->wasted_size -= jeb->wasted_size;
135 c->free_size -= jeb->free_size;
136 c->used_size -= jeb->used_size;
137 c->dirty_size -= jeb->dirty_size;
138 jeb->wasted_size = jeb->used_size = jeb->dirty_size = jeb->free_size = 0;
139 jffs2_free_all_node_refs(c, jeb);
140 list_add(&jeb->list, &c->erasing_list);
141 spin_unlock(&c->erase_completion_lock);
142
143 jffs2_erase_block(c, jeb);
144
145 } else {
146 BUG();
147 }
148
149 /* Be nice */
150 cond_resched();
151 spin_lock(&c->erase_completion_lock);
152 }
153
154 spin_unlock(&c->erase_completion_lock);
155 done:
156 D1(printk(KERN_DEBUG "jffs2_erase_pending_blocks completed\n"));
157
158 up(&c->erase_free_sem);
159}
160
161static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
162{
163 D1(printk(KERN_DEBUG "Erase completed successfully at 0x%08x\n", jeb->offset));
164 spin_lock(&c->erase_completion_lock);
165 list_del(&jeb->list);
166 list_add_tail(&jeb->list, &c->erase_complete_list);
167 spin_unlock(&c->erase_completion_lock);
168 /* Ensure that kupdated calls us again to mark them clean */
169 jffs2_erase_pending_trigger(c);
170}
171
172static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset)
173{
174 /* For NAND, if the failure did not occur at the device level for a
175 specific physical page, don't bother updating the bad block table. */
176 if (jffs2_cleanmarker_oob(c) && (bad_offset != 0xffffffff)) {
177 /* We had a device-level failure to erase. Let's see if we've
178 failed too many times. */
179 if (!jffs2_write_nand_badblock(c, jeb, bad_offset)) {
180 /* We'd like to give this block another try. */
181 spin_lock(&c->erase_completion_lock);
182 list_del(&jeb->list);
183 list_add(&jeb->list, &c->erase_pending_list);
184 c->erasing_size -= c->sector_size;
185 c->dirty_size += c->sector_size;
186 jeb->dirty_size = c->sector_size;
187 spin_unlock(&c->erase_completion_lock);
188 return;
189 }
190 }
191
192 spin_lock(&c->erase_completion_lock);
193 c->erasing_size -= c->sector_size;
194 c->bad_size += c->sector_size;
195 list_del(&jeb->list);
196 list_add(&jeb->list, &c->bad_list);
197 c->nr_erasing_blocks--;
198 spin_unlock(&c->erase_completion_lock);
199 wake_up(&c->erase_wait);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000200}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202#ifndef __ECOS
203static void jffs2_erase_callback(struct erase_info *instr)
204{
205 struct erase_priv_struct *priv = (void *)instr->priv;
206
207 if(instr->state != MTD_ERASE_DONE) {
208 printk(KERN_WARNING "Erase at 0x%08x finished, but state != MTD_ERASE_DONE. State is 0x%x instead.\n", instr->addr, instr->state);
209 jffs2_erase_failed(priv->c, priv->jeb, instr->fail_addr);
210 } else {
211 jffs2_erase_succeeded(priv->c, priv->jeb);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 kfree(instr);
214}
215#endif /* !__ECOS */
216
217/* Hmmm. Maybe we should accept the extra space it takes and make
218 this a standard doubly-linked list? */
219static inline void jffs2_remove_node_refs_from_ino_list(struct jffs2_sb_info *c,
220 struct jffs2_raw_node_ref *ref, struct jffs2_eraseblock *jeb)
221{
222 struct jffs2_inode_cache *ic = NULL;
223 struct jffs2_raw_node_ref **prev;
224
225 prev = &ref->next_in_ino;
226
227 /* Walk the inode's list once, removing any nodes from this eraseblock */
228 while (1) {
229 if (!(*prev)->next_in_ino) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000230 /* We're looking at the jffs2_inode_cache, which is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 at the end of the linked list. Stash it and continue
232 from the beginning of the list */
233 ic = (struct jffs2_inode_cache *)(*prev);
234 prev = &ic->nodes;
235 continue;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Andrew Victor3be36672005-02-09 09:09:05 +0000238 if (SECTOR_ADDR((*prev)->flash_offset) == jeb->offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 /* It's in the block we're erasing */
240 struct jffs2_raw_node_ref *this;
241
242 this = *prev;
243 *prev = this->next_in_ino;
244 this->next_in_ino = NULL;
245
246 if (this == ref)
247 break;
248
249 continue;
250 }
251 /* Not to be deleted. Skip */
252 prev = &((*prev)->next_in_ino);
253 }
254
255 /* PARANOIA */
256 if (!ic) {
257 printk(KERN_WARNING "inode_cache not found in remove_node_refs()!!\n");
258 return;
259 }
260
261 D1(printk(KERN_DEBUG "Removed nodes in range 0x%08x-0x%08x from ino #%u\n",
262 jeb->offset, jeb->offset + c->sector_size, ic->ino));
263
264 D2({
265 int i=0;
266 struct jffs2_raw_node_ref *this;
267 printk(KERN_DEBUG "After remove_node_refs_from_ino_list: \n" KERN_DEBUG);
268
269 this = ic->nodes;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 while(this) {
272 printk( "0x%08x(%d)->", ref_offset(this), ref_flags(this));
273 if (++i == 5) {
274 printk("\n" KERN_DEBUG);
275 i=0;
276 }
277 this = this->next_in_ino;
278 }
279 printk("\n");
280 });
281
Artem B. Bityuckiy437316d2005-03-20 17:46:23 +0000282 if (ic->nodes == (void *)ic && ic->nlink == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 jffs2_del_ino_cache(c, ic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
286static void jffs2_free_all_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
287{
288 struct jffs2_raw_node_ref *ref;
289 D1(printk(KERN_DEBUG "Freeing all node refs for eraseblock offset 0x%08x\n", jeb->offset));
290 while(jeb->first_node) {
291 ref = jeb->first_node;
292 jeb->first_node = ref->next_phys;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 /* Remove from the inode-list */
295 if (ref->next_in_ino)
296 jffs2_remove_node_refs_from_ino_list(c, ref, jeb);
297 /* else it was a non-inode node or already removed, so don't bother */
298
299 jffs2_free_raw_node_ref(ref);
300 }
301 jeb->last_node = NULL;
302}
303
Thomas Gleixner5d157882005-07-15 08:14:44 +0200304static int jffs2_block_check_erase(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t *bad_offset)
305{
306 void *ebuf;
307 uint32_t ofs;
308 size_t retlen;
309 int ret = -EIO;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000310
Thomas Gleixner5d157882005-07-15 08:14:44 +0200311 ebuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
312 if (!ebuf) {
313 printk(KERN_WARNING "Failed to allocate page buffer for verifying erase at 0x%08x. Refiling\n", jeb->offset);
314 return -EAGAIN;
315 }
316
317 D1(printk(KERN_DEBUG "Verifying erase at 0x%08x\n", jeb->offset));
318
319 for (ofs = jeb->offset; ofs < jeb->offset + c->sector_size; ) {
320 uint32_t readlen = min((uint32_t)PAGE_SIZE, jeb->offset + c->sector_size - ofs);
321 int i;
322
323 *bad_offset = ofs;
324
325 ret = jffs2_flash_read(c, ofs, readlen, &retlen, ebuf);
326 if (ret) {
327 printk(KERN_WARNING "Read of newly-erased block at 0x%08x failed: %d. Putting on bad_list\n", ofs, ret);
328 goto fail;
329 }
330 if (retlen != readlen) {
331 printk(KERN_WARNING "Short read from newly-erased block at 0x%08x. Wanted %d, got %zd\n", ofs, readlen, retlen);
332 goto fail;
333 }
334 for (i=0; i<readlen; i += sizeof(unsigned long)) {
335 /* It's OK. We know it's properly aligned */
336 unsigned long *datum = ebuf + i;
337 if (*datum + 1) {
338 *bad_offset += i;
339 printk(KERN_WARNING "Newly-erased block contained word 0x%lx at offset 0x%08x\n", *datum, *bad_offset);
340 goto fail;
341 }
342 }
343 ofs += readlen;
344 cond_resched();
345 }
346 ret = 0;
347fail:
348 kfree(ebuf);
349 return ret;
350}
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
353{
354 struct jffs2_raw_node_ref *marker_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 size_t retlen;
356 int ret;
357 uint32_t bad_offset;
358
Thomas Gleixner5d157882005-07-15 08:14:44 +0200359 switch (jffs2_block_check_erase(c, jeb, &bad_offset)) {
360 case -EAGAIN: goto refile;
361 case -EIO: goto filebad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000364 /* Write the erase complete marker */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 D1(printk(KERN_DEBUG "Writing erased marker to block at 0x%08x\n", jeb->offset));
Thomas Gleixner5d157882005-07-15 08:14:44 +0200366 bad_offset = jeb->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Thomas Gleixner5d157882005-07-15 08:14:44 +0200368 /* Cleanmarker in oob area or no cleanmarker at all ? */
369 if (jffs2_cleanmarker_oob(c) || c->cleanmarker_size == 0) {
370
371 if (jffs2_cleanmarker_oob(c)) {
372 if (jffs2_write_nand_cleanmarker(c, jeb))
373 goto filebad;
374 }
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 jeb->first_node = jeb->last_node = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 jeb->free_size = c->sector_size;
378 jeb->used_size = 0;
379 jeb->dirty_size = 0;
380 jeb->wasted_size = 0;
Andrew Victor8f15fd52005-02-09 09:17:45 +0000381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 } else {
Thomas Gleixner5d157882005-07-15 08:14:44 +0200383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 struct kvec vecs[1];
385 struct jffs2_unknown_node marker = {
386 .magic = cpu_to_je16(JFFS2_MAGIC_BITMASK),
387 .nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER),
388 .totlen = cpu_to_je32(c->cleanmarker_size)
389 };
390
Thomas Gleixner5d157882005-07-15 08:14:44 +0200391 marker_ref = jffs2_alloc_raw_node_ref();
392 if (!marker_ref) {
393 printk(KERN_WARNING "Failed to allocate raw node ref for clean marker. Refiling\n");
394 goto refile;
395 }
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 marker.hdr_crc = cpu_to_je32(crc32(0, &marker, sizeof(struct jffs2_unknown_node)-4));
398
399 vecs[0].iov_base = (unsigned char *) &marker;
400 vecs[0].iov_len = sizeof(marker);
401 ret = jffs2_flash_direct_writev(c, vecs, 1, jeb->offset, &retlen);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000402
Thomas Gleixner5d157882005-07-15 08:14:44 +0200403 if (ret || retlen != sizeof(marker)) {
404 if (ret)
405 printk(KERN_WARNING "Write clean marker to block at 0x%08x failed: %d\n",
406 jeb->offset, ret);
407 else
408 printk(KERN_WARNING "Short write to newly-erased block at 0x%08x: Wanted %zd, got %zd\n",
409 jeb->offset, sizeof(marker), retlen);
410
411 jffs2_free_raw_node_ref(marker_ref);
412 goto filebad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414
415 marker_ref->next_in_ino = NULL;
416 marker_ref->next_phys = NULL;
417 marker_ref->flash_offset = jeb->offset | REF_NORMAL;
418 marker_ref->__totlen = c->cleanmarker_size;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 jeb->first_node = jeb->last_node = marker_ref;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 jeb->free_size = c->sector_size - c->cleanmarker_size;
423 jeb->used_size = c->cleanmarker_size;
424 jeb->dirty_size = 0;
425 jeb->wasted_size = 0;
426 }
427
428 spin_lock(&c->erase_completion_lock);
429 c->erasing_size -= c->sector_size;
430 c->free_size += jeb->free_size;
431 c->used_size += jeb->used_size;
432
Artem B. Bityutskiye0c8e422005-07-24 16:14:17 +0100433 jffs2_dbg_acct_sanity_check_nolock(c,jeb);
434 jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 list_add_tail(&jeb->list, &c->free_list);
437 c->nr_erasing_blocks--;
438 c->nr_free_blocks++;
439 spin_unlock(&c->erase_completion_lock);
440 wake_up(&c->erase_wait);
Thomas Gleixner5d157882005-07-15 08:14:44 +0200441 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Thomas Gleixner5d157882005-07-15 08:14:44 +0200443filebad:
444 spin_lock(&c->erase_completion_lock);
445 /* Stick it on a list (any list) so erase_failed can take it
446 right off again. Silly, but shouldn't happen often. */
447 list_add(&jeb->list, &c->erasing_list);
448 spin_unlock(&c->erase_completion_lock);
449 jffs2_erase_failed(c, jeb, bad_offset);
450 return;
451
452refile:
453 /* Stick it back on the list from whence it came and come back later */
454 jffs2_erase_pending_trigger(c);
455 spin_lock(&c->erase_completion_lock);
456 list_add(&jeb->list, &c->erase_complete_list);
457 spin_unlock(&c->erase_completion_lock);
458 return;
459}