blob: 1862e8bc101d4124fde048306512dd2e68daa18c [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
34
35static void jffs2_erase_block(struct jffs2_sb_info *c,
36 struct jffs2_eraseblock *jeb)
37{
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;
45 }
46 bad_offset = jeb->offset;
47#else /* Linux */
48 struct erase_info *instr;
49
Artem B. Bityutskiye0c8e422005-07-24 16:14:17 +010050 D1(printk(KERN_DEBUG "jffs2_erase_block(): erase block %#08x (range %#08x-%#08x)\n",
51 jeb->offset, jeb->offset, jeb->offset + c->sector_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 instr = kmalloc(sizeof(struct erase_info) + sizeof(struct erase_priv_struct), GFP_KERNEL);
53 if (!instr) {
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_del(&jeb->list);
57 list_add(&jeb->list, &c->erase_pending_list);
58 c->erasing_size -= c->sector_size;
59 c->dirty_size += c->sector_size;
60 jeb->dirty_size = c->sector_size;
61 spin_unlock(&c->erase_completion_lock);
62 return;
63 }
64
65 memset(instr, 0, sizeof(*instr));
66
67 instr->mtd = c->mtd;
68 instr->addr = jeb->offset;
69 instr->len = c->sector_size;
70 instr->callback = jffs2_erase_callback;
71 instr->priv = (unsigned long)(&instr[1]);
72 instr->fail_addr = 0xffffffff;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000073
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 ((struct erase_priv_struct *)instr->priv)->jeb = jeb;
75 ((struct erase_priv_struct *)instr->priv)->c = c;
76
77 ret = c->mtd->erase(c->mtd, instr);
78 if (!ret)
79 return;
80
81 bad_offset = instr->fail_addr;
82 kfree(instr);
83#endif /* __ECOS */
84
85 if (ret == -ENOMEM || ret == -EAGAIN) {
86 /* Erase failed immediately. Refile it on the list */
87 D1(printk(KERN_DEBUG "Erase at 0x%08x failed: %d. Refiling on erase_pending_list\n", jeb->offset, ret));
88 spin_lock(&c->erase_completion_lock);
89 list_del(&jeb->list);
90 list_add(&jeb->list, &c->erase_pending_list);
91 c->erasing_size -= c->sector_size;
92 c->dirty_size += c->sector_size;
93 jeb->dirty_size = c->sector_size;
94 spin_unlock(&c->erase_completion_lock);
95 return;
96 }
97
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000098 if (ret == -EROFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 printk(KERN_WARNING "Erase at 0x%08x failed immediately: -EROFS. Is the sector locked?\n", jeb->offset);
100 else
101 printk(KERN_WARNING "Erase at 0x%08x failed immediately: errno %d\n", jeb->offset, ret);
102
103 jffs2_erase_failed(c, jeb, bad_offset);
104}
105
106void jffs2_erase_pending_blocks(struct jffs2_sb_info *c, int count)
107{
108 struct jffs2_eraseblock *jeb;
109
110 down(&c->erase_free_sem);
111
112 spin_lock(&c->erase_completion_lock);
113
114 while (!list_empty(&c->erase_complete_list) ||
115 !list_empty(&c->erase_pending_list)) {
116
117 if (!list_empty(&c->erase_complete_list)) {
118 jeb = list_entry(c->erase_complete_list.next, struct jffs2_eraseblock, list);
119 list_del(&jeb->list);
120 spin_unlock(&c->erase_completion_lock);
121 jffs2_mark_erased_block(c, jeb);
122
123 if (!--count) {
124 D1(printk(KERN_DEBUG "Count reached. jffs2_erase_pending_blocks leaving\n"));
125 goto done;
126 }
127
128 } else if (!list_empty(&c->erase_pending_list)) {
129 jeb = list_entry(c->erase_pending_list.next, struct jffs2_eraseblock, list);
130 D1(printk(KERN_DEBUG "Starting erase of pending block 0x%08x\n", jeb->offset));
131 list_del(&jeb->list);
132 c->erasing_size += c->sector_size;
133 c->wasted_size -= jeb->wasted_size;
134 c->free_size -= jeb->free_size;
135 c->used_size -= jeb->used_size;
136 c->dirty_size -= jeb->dirty_size;
137 jeb->wasted_size = jeb->used_size = jeb->dirty_size = jeb->free_size = 0;
David Woodhousec38c1b62006-05-25 01:38:27 +0100138 jffs2_free_jeb_node_refs(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 list_add(&jeb->list, &c->erasing_list);
140 spin_unlock(&c->erase_completion_lock);
141
142 jffs2_erase_block(c, jeb);
143
144 } else {
145 BUG();
146 }
147
148 /* Be nice */
149 cond_resched();
150 spin_lock(&c->erase_completion_lock);
151 }
152
153 spin_unlock(&c->erase_completion_lock);
154 done:
155 D1(printk(KERN_DEBUG "jffs2_erase_pending_blocks completed\n"));
156
157 up(&c->erase_free_sem);
158}
159
160static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
161{
162 D1(printk(KERN_DEBUG "Erase completed successfully at 0x%08x\n", jeb->offset));
163 spin_lock(&c->erase_completion_lock);
164 list_del(&jeb->list);
165 list_add_tail(&jeb->list, &c->erase_complete_list);
166 spin_unlock(&c->erase_completion_lock);
167 /* Ensure that kupdated calls us again to mark them clean */
168 jffs2_erase_pending_trigger(c);
169}
170
171static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset)
172{
173 /* For NAND, if the failure did not occur at the device level for a
174 specific physical page, don't bother updating the bad block table. */
175 if (jffs2_cleanmarker_oob(c) && (bad_offset != 0xffffffff)) {
176 /* We had a device-level failure to erase. Let's see if we've
177 failed too many times. */
178 if (!jffs2_write_nand_badblock(c, jeb, bad_offset)) {
179 /* We'd like to give this block another try. */
180 spin_lock(&c->erase_completion_lock);
181 list_del(&jeb->list);
182 list_add(&jeb->list, &c->erase_pending_list);
183 c->erasing_size -= c->sector_size;
184 c->dirty_size += c->sector_size;
185 jeb->dirty_size = c->sector_size;
186 spin_unlock(&c->erase_completion_lock);
187 return;
188 }
189 }
190
191 spin_lock(&c->erase_completion_lock);
192 c->erasing_size -= c->sector_size;
193 c->bad_size += c->sector_size;
194 list_del(&jeb->list);
195 list_add(&jeb->list, &c->bad_list);
196 c->nr_erasing_blocks--;
197 spin_unlock(&c->erase_completion_lock);
198 wake_up(&c->erase_wait);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000199}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201#ifndef __ECOS
202static void jffs2_erase_callback(struct erase_info *instr)
203{
204 struct erase_priv_struct *priv = (void *)instr->priv;
205
206 if(instr->state != MTD_ERASE_DONE) {
207 printk(KERN_WARNING "Erase at 0x%08x finished, but state != MTD_ERASE_DONE. State is 0x%x instead.\n", instr->addr, instr->state);
208 jffs2_erase_failed(priv->c, priv->jeb, instr->fail_addr);
209 } else {
210 jffs2_erase_succeeded(priv->c, priv->jeb);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 kfree(instr);
213}
214#endif /* !__ECOS */
215
216/* Hmmm. Maybe we should accept the extra space it takes and make
217 this a standard doubly-linked list? */
218static inline void jffs2_remove_node_refs_from_ino_list(struct jffs2_sb_info *c,
219 struct jffs2_raw_node_ref *ref, struct jffs2_eraseblock *jeb)
220{
221 struct jffs2_inode_cache *ic = NULL;
222 struct jffs2_raw_node_ref **prev;
223
224 prev = &ref->next_in_ino;
225
226 /* Walk the inode's list once, removing any nodes from this eraseblock */
227 while (1) {
228 if (!(*prev)->next_in_ino) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000229 /* We're looking at the jffs2_inode_cache, which is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 at the end of the linked list. Stash it and continue
231 from the beginning of the list */
232 ic = (struct jffs2_inode_cache *)(*prev);
David Woodhouse0eac9402006-05-22 16:29:23 +0100233 BUG_ON(ic->class != RAWNODE_CLASS_INODE_CACHE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 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
David Woodhousec38c1b62006-05-25 01:38:27 +0100286void jffs2_free_jeb_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
David Woodhouse9bfeb692006-05-26 21:19:05 +0100288 struct jffs2_raw_node_ref *block, *ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 D1(printk(KERN_DEBUG "Freeing all node refs for eraseblock offset 0x%08x\n", jeb->offset));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000290
David Woodhouse9bfeb692006-05-26 21:19:05 +0100291 block = ref = jeb->first_node;
292
293 while (ref) {
294 if (ref->flash_offset == REF_LINK_NODE) {
295 ref = ref->next_in_ino;
296 jffs2_free_refblock(block);
297 block = ref;
298 continue;
299 }
300 if (ref->flash_offset != REF_EMPTY_NODE && ref->next_in_ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 jffs2_remove_node_refs_from_ino_list(c, ref, jeb);
302 /* else it was a non-inode node or already removed, so don't bother */
303
David Woodhouse9bfeb692006-05-26 21:19:05 +0100304 ref++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
David Woodhouse9bfeb692006-05-26 21:19:05 +0100306 jeb->first_node = jeb->last_node = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
Thomas Gleixner5d157882005-07-15 08:14:44 +0200309static int jffs2_block_check_erase(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t *bad_offset)
310{
311 void *ebuf;
312 uint32_t ofs;
313 size_t retlen;
314 int ret = -EIO;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000315
Thomas Gleixner5d157882005-07-15 08:14:44 +0200316 ebuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
317 if (!ebuf) {
318 printk(KERN_WARNING "Failed to allocate page buffer for verifying erase at 0x%08x. Refiling\n", jeb->offset);
319 return -EAGAIN;
320 }
321
322 D1(printk(KERN_DEBUG "Verifying erase at 0x%08x\n", jeb->offset));
323
324 for (ofs = jeb->offset; ofs < jeb->offset + c->sector_size; ) {
325 uint32_t readlen = min((uint32_t)PAGE_SIZE, jeb->offset + c->sector_size - ofs);
326 int i;
327
328 *bad_offset = ofs;
329
330 ret = jffs2_flash_read(c, ofs, readlen, &retlen, ebuf);
331 if (ret) {
332 printk(KERN_WARNING "Read of newly-erased block at 0x%08x failed: %d. Putting on bad_list\n", ofs, ret);
333 goto fail;
334 }
335 if (retlen != readlen) {
336 printk(KERN_WARNING "Short read from newly-erased block at 0x%08x. Wanted %d, got %zd\n", ofs, readlen, retlen);
337 goto fail;
338 }
339 for (i=0; i<readlen; i += sizeof(unsigned long)) {
340 /* It's OK. We know it's properly aligned */
341 unsigned long *datum = ebuf + i;
342 if (*datum + 1) {
343 *bad_offset += i;
344 printk(KERN_WARNING "Newly-erased block contained word 0x%lx at offset 0x%08x\n", *datum, *bad_offset);
345 goto fail;
346 }
347 }
348 ofs += readlen;
349 cond_resched();
350 }
351 ret = 0;
352fail:
353 kfree(ebuf);
354 return ret;
355}
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
358{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 size_t retlen;
360 int ret;
361 uint32_t bad_offset;
362
Thomas Gleixner5d157882005-07-15 08:14:44 +0200363 switch (jffs2_block_check_erase(c, jeb, &bad_offset)) {
364 case -EAGAIN: goto refile;
365 case -EIO: goto filebad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000368 /* Write the erase complete marker */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 D1(printk(KERN_DEBUG "Writing erased marker to block at 0x%08x\n", jeb->offset));
Thomas Gleixner5d157882005-07-15 08:14:44 +0200370 bad_offset = jeb->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Thomas Gleixner5d157882005-07-15 08:14:44 +0200372 /* Cleanmarker in oob area or no cleanmarker at all ? */
373 if (jffs2_cleanmarker_oob(c) || c->cleanmarker_size == 0) {
374
375 if (jffs2_cleanmarker_oob(c)) {
376 if (jffs2_write_nand_cleanmarker(c, jeb))
377 goto filebad;
378 }
379
David Woodhousef1f96712006-05-20 19:45:26 +0100380 /* Everything else got zeroed before the erase */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 jeb->free_size = c->sector_size;
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
David Woodhouse046b8b92006-05-25 01:50:35 +0100391 jffs2_prealloc_raw_node_refs(c, jeb, 1);
Thomas Gleixner5d157882005-07-15 08:14:44 +0200392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 marker.hdr_crc = cpu_to_je32(crc32(0, &marker, sizeof(struct jffs2_unknown_node)-4));
394
395 vecs[0].iov_base = (unsigned char *) &marker;
396 vecs[0].iov_len = sizeof(marker);
397 ret = jffs2_flash_direct_writev(c, vecs, 1, jeb->offset, &retlen);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000398
Thomas Gleixner5d157882005-07-15 08:14:44 +0200399 if (ret || retlen != sizeof(marker)) {
400 if (ret)
401 printk(KERN_WARNING "Write clean marker to block at 0x%08x failed: %d\n",
402 jeb->offset, ret);
403 else
404 printk(KERN_WARNING "Short write to newly-erased block at 0x%08x: Wanted %zd, got %zd\n",
405 jeb->offset, sizeof(marker), retlen);
406
Thomas Gleixner5d157882005-07-15 08:14:44 +0200407 goto filebad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
409
David Woodhousef1f96712006-05-20 19:45:26 +0100410 /* Everything else got zeroed before the erase */
411 jeb->free_size = c->sector_size;
David Woodhouse2f785402006-05-24 02:04:45 +0100412 /* FIXME Special case for cleanmarker in empty block */
413 jffs2_link_node_ref(c, jeb, jeb->offset | REF_NORMAL, c->cleanmarker_size, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
415
416 spin_lock(&c->erase_completion_lock);
417 c->erasing_size -= c->sector_size;
418 c->free_size += jeb->free_size;
419 c->used_size += jeb->used_size;
420
Artem B. Bityutskiye0c8e422005-07-24 16:14:17 +0100421 jffs2_dbg_acct_sanity_check_nolock(c,jeb);
422 jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 list_add_tail(&jeb->list, &c->free_list);
425 c->nr_erasing_blocks--;
426 c->nr_free_blocks++;
427 spin_unlock(&c->erase_completion_lock);
428 wake_up(&c->erase_wait);
Thomas Gleixner5d157882005-07-15 08:14:44 +0200429 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Thomas Gleixner5d157882005-07-15 08:14:44 +0200431filebad:
432 spin_lock(&c->erase_completion_lock);
433 /* Stick it on a list (any list) so erase_failed can take it
434 right off again. Silly, but shouldn't happen often. */
435 list_add(&jeb->list, &c->erasing_list);
436 spin_unlock(&c->erase_completion_lock);
437 jffs2_erase_failed(c, jeb, bad_offset);
438 return;
439
440refile:
441 /* Stick it back on the list from whence it came and come back later */
442 jffs2_erase_pending_trigger(c);
443 spin_lock(&c->erase_completion_lock);
444 list_add(&jeb->list, &c->erase_complete_list);
445 spin_unlock(&c->erase_completion_lock);
446 return;
447}