blob: b47679be118a5fe00a04585d54b07f9797e7f700 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * JFFS2 -- Journalling Flash File System, Version 2.
3 *
David Woodhousec00c3102007-04-25 14:16:47 +01004 * Copyright © 2001-2007 Red Hat, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Created by David Woodhouse <dwmw2@infradead.org>
7 *
8 * For licensing information, see the file 'LICENCE' in this directory.
9 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
12#include <linux/kernel.h>
13#include <linux/slab.h>
14#include <linux/mtd/mtd.h>
15#include <linux/compiler.h>
16#include <linux/crc32.h>
17#include <linux/sched.h>
18#include <linux/pagemap.h>
19#include "nodelist.h"
20
21struct erase_priv_struct {
22 struct jffs2_eraseblock *jeb;
23 struct jffs2_sb_info *c;
24};
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000025
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#ifndef __ECOS
27static void jffs2_erase_callback(struct erase_info *);
28#endif
29static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset);
30static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
32
33static void jffs2_erase_block(struct jffs2_sb_info *c,
34 struct jffs2_eraseblock *jeb)
35{
36 int ret;
37 uint32_t bad_offset;
38#ifdef __ECOS
39 ret = jffs2_flash_erase(c, jeb);
40 if (!ret) {
David Woodhouseef53cb02007-07-10 10:01:22 +010041 jffs2_erase_succeeded(c, jeb);
42 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 }
44 bad_offset = jeb->offset;
45#else /* Linux */
46 struct erase_info *instr;
47
Artem B. Bityutskiye0c8e422005-07-24 16:14:17 +010048 D1(printk(KERN_DEBUG "jffs2_erase_block(): erase block %#08x (range %#08x-%#08x)\n",
49 jeb->offset, jeb->offset, jeb->offset + c->sector_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 instr = kmalloc(sizeof(struct erase_info) + sizeof(struct erase_priv_struct), GFP_KERNEL);
51 if (!instr) {
52 printk(KERN_WARNING "kmalloc for struct erase_info in jffs2_erase_block failed. Refiling block for later\n");
David Woodhouseced22072008-04-22 15:13:40 +010053 mutex_lock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 spin_lock(&c->erase_completion_lock);
Akinobu Mitaf1166292006-06-26 00:24:46 -070055 list_move(&jeb->list, &c->erase_pending_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 c->erasing_size -= c->sector_size;
57 c->dirty_size += c->sector_size;
58 jeb->dirty_size = c->sector_size;
59 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +010060 mutex_unlock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 return;
62 }
63
64 memset(instr, 0, sizeof(*instr));
65
66 instr->mtd = c->mtd;
67 instr->addr = jeb->offset;
68 instr->len = c->sector_size;
69 instr->callback = jffs2_erase_callback;
70 instr->priv = (unsigned long)(&instr[1]);
Adrian Hunterbb0eb212008-08-12 12:40:50 +030071 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 ((struct erase_priv_struct *)instr->priv)->jeb = jeb;
74 ((struct erase_priv_struct *)instr->priv)->c = c;
75
76 ret = c->mtd->erase(c->mtd, instr);
77 if (!ret)
78 return;
79
80 bad_offset = instr->fail_addr;
81 kfree(instr);
82#endif /* __ECOS */
83
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));
David Woodhouseced22072008-04-22 15:13:40 +010087 mutex_lock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 spin_lock(&c->erase_completion_lock);
Akinobu Mitaf1166292006-06-26 00:24:46 -070089 list_move(&jeb->list, &c->erase_pending_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 c->erasing_size -= c->sector_size;
91 c->dirty_size += c->sector_size;
92 jeb->dirty_size = c->sector_size;
93 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +010094 mutex_unlock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 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
David Woodhouseced22072008-04-22 15:13:40 +0100110 mutex_lock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
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);
David Woodhousee2bc3222008-04-23 14:15:24 +0100119 list_move(&jeb->list, &c->erase_checking_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +0100121 mutex_unlock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 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;
David Woodhousec38c1b62006-05-25 01:38:27 +0100139 jffs2_free_jeb_node_refs(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 list_add(&jeb->list, &c->erasing_list);
141 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +0100142 mutex_unlock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 jffs2_erase_block(c, jeb);
145
146 } else {
147 BUG();
148 }
149
150 /* Be nice */
Joakim Tjernlundfd532492007-06-26 23:32:10 +0200151 yield();
David Woodhouseced22072008-04-22 15:13:40 +0100152 mutex_lock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 spin_lock(&c->erase_completion_lock);
154 }
155
156 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +0100157 mutex_unlock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 done:
159 D1(printk(KERN_DEBUG "jffs2_erase_pending_blocks completed\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
162static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
163{
164 D1(printk(KERN_DEBUG "Erase completed successfully at 0x%08x\n", jeb->offset));
David Woodhouseced22072008-04-22 15:13:40 +0100165 mutex_lock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 spin_lock(&c->erase_completion_lock);
Akinobu Mitaf1166292006-06-26 00:24:46 -0700167 list_move_tail(&jeb->list, &c->erase_complete_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +0100169 mutex_unlock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 /* Ensure that kupdated calls us again to mark them clean */
171 jffs2_erase_pending_trigger(c);
172}
173
174static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset)
175{
176 /* For NAND, if the failure did not occur at the device level for a
177 specific physical page, don't bother updating the bad block table. */
Adrian Hunter69423d92008-12-10 13:37:21 +0000178 if (jffs2_cleanmarker_oob(c) && (bad_offset != (uint32_t)MTD_FAIL_ADDR_UNKNOWN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 /* We had a device-level failure to erase. Let's see if we've
180 failed too many times. */
181 if (!jffs2_write_nand_badblock(c, jeb, bad_offset)) {
182 /* We'd like to give this block another try. */
David Woodhouseced22072008-04-22 15:13:40 +0100183 mutex_lock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 spin_lock(&c->erase_completion_lock);
Akinobu Mitaf1166292006-06-26 00:24:46 -0700185 list_move(&jeb->list, &c->erase_pending_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 c->erasing_size -= c->sector_size;
187 c->dirty_size += c->sector_size;
188 jeb->dirty_size = c->sector_size;
189 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +0100190 mutex_unlock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return;
192 }
193 }
194
David Woodhouseced22072008-04-22 15:13:40 +0100195 mutex_lock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 spin_lock(&c->erase_completion_lock);
197 c->erasing_size -= c->sector_size;
198 c->bad_size += c->sector_size;
Akinobu Mitaf1166292006-06-26 00:24:46 -0700199 list_move(&jeb->list, &c->bad_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 c->nr_erasing_blocks--;
201 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +0100202 mutex_unlock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 wake_up(&c->erase_wait);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000204}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206#ifndef __ECOS
207static void jffs2_erase_callback(struct erase_info *instr)
208{
209 struct erase_priv_struct *priv = (void *)instr->priv;
210
211 if(instr->state != MTD_ERASE_DONE) {
Adrian Hunter69423d92008-12-10 13:37:21 +0000212 printk(KERN_WARNING "Erase at 0x%08llx finished, but state != MTD_ERASE_DONE. State is 0x%x instead.\n",
213 (unsigned long long)instr->addr, instr->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 jffs2_erase_failed(priv->c, priv->jeb, instr->fail_addr);
215 } else {
216 jffs2_erase_succeeded(priv->c, priv->jeb);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 kfree(instr);
219}
220#endif /* !__ECOS */
221
222/* Hmmm. Maybe we should accept the extra space it takes and make
223 this a standard doubly-linked list? */
224static inline void jffs2_remove_node_refs_from_ino_list(struct jffs2_sb_info *c,
225 struct jffs2_raw_node_ref *ref, struct jffs2_eraseblock *jeb)
226{
227 struct jffs2_inode_cache *ic = NULL;
228 struct jffs2_raw_node_ref **prev;
229
230 prev = &ref->next_in_ino;
231
232 /* Walk the inode's list once, removing any nodes from this eraseblock */
233 while (1) {
234 if (!(*prev)->next_in_ino) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000235 /* We're looking at the jffs2_inode_cache, which is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 at the end of the linked list. Stash it and continue
237 from the beginning of the list */
238 ic = (struct jffs2_inode_cache *)(*prev);
239 prev = &ic->nodes;
240 continue;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Andrew Victor3be36672005-02-09 09:09:05 +0000243 if (SECTOR_ADDR((*prev)->flash_offset) == jeb->offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 /* It's in the block we're erasing */
245 struct jffs2_raw_node_ref *this;
246
247 this = *prev;
248 *prev = this->next_in_ino;
249 this->next_in_ino = NULL;
250
251 if (this == ref)
252 break;
253
254 continue;
255 }
256 /* Not to be deleted. Skip */
257 prev = &((*prev)->next_in_ino);
258 }
259
260 /* PARANOIA */
261 if (!ic) {
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900262 JFFS2_WARNING("inode_cache/xattr_datum/xattr_ref"
263 " not found in remove_node_refs()!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return;
265 }
266
267 D1(printk(KERN_DEBUG "Removed nodes in range 0x%08x-0x%08x from ino #%u\n",
268 jeb->offset, jeb->offset + c->sector_size, ic->ino));
269
270 D2({
271 int i=0;
272 struct jffs2_raw_node_ref *this;
Joe Perchesad361c92009-07-06 13:05:40 -0700273 printk(KERN_DEBUG "After remove_node_refs_from_ino_list: \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 this = ic->nodes;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000276
Joe Perchesad361c92009-07-06 13:05:40 -0700277 printk(KERN_DEBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 while(this) {
Joe Perchesad361c92009-07-06 13:05:40 -0700279 printk(KERN_CONT "0x%08x(%d)->",
280 ref_offset(this), ref_flags(this));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (++i == 5) {
Joe Perchesad361c92009-07-06 13:05:40 -0700282 printk(KERN_DEBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 i=0;
284 }
285 this = this->next_in_ino;
286 }
Joe Perchesad361c92009-07-06 13:05:40 -0700287 printk(KERN_CONT "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 });
289
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900290 switch (ic->class) {
291#ifdef CONFIG_JFFS2_FS_XATTR
292 case RAWNODE_CLASS_XATTR_DATUM:
293 jffs2_release_xattr_datum(c, (struct jffs2_xattr_datum *)ic);
294 break;
295 case RAWNODE_CLASS_XATTR_REF:
296 jffs2_release_xattr_ref(c, (struct jffs2_xattr_ref *)ic);
297 break;
298#endif
299 default:
David Woodhouse27c72b02008-05-01 18:47:17 +0100300 if (ic->nodes == (void *)ic && ic->pino_nlink == 0)
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900301 jffs2_del_ino_cache(c, ic);
302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
David Woodhousec38c1b62006-05-25 01:38:27 +0100305void jffs2_free_jeb_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
David Woodhouse9bfeb692006-05-26 21:19:05 +0100307 struct jffs2_raw_node_ref *block, *ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 D1(printk(KERN_DEBUG "Freeing all node refs for eraseblock offset 0x%08x\n", jeb->offset));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000309
David Woodhouse9bfeb692006-05-26 21:19:05 +0100310 block = ref = jeb->first_node;
311
312 while (ref) {
313 if (ref->flash_offset == REF_LINK_NODE) {
314 ref = ref->next_in_ino;
315 jffs2_free_refblock(block);
316 block = ref;
317 continue;
318 }
319 if (ref->flash_offset != REF_EMPTY_NODE && ref->next_in_ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 jffs2_remove_node_refs_from_ino_list(c, ref, jeb);
321 /* else it was a non-inode node or already removed, so don't bother */
322
David Woodhouse9bfeb692006-05-26 21:19:05 +0100323 ref++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
David Woodhouse9bfeb692006-05-26 21:19:05 +0100325 jeb->first_node = jeb->last_node = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
Thomas Gleixner5d157882005-07-15 08:14:44 +0200328static int jffs2_block_check_erase(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t *bad_offset)
329{
330 void *ebuf;
331 uint32_t ofs;
332 size_t retlen;
333 int ret = -EIO;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000334
Joakim Tjernlundfab2c392007-06-01 15:14:09 +0200335 if (c->mtd->point) {
336 unsigned long *wordebuf;
337
Jared Hulberta98889f2008-04-29 23:26:49 -0700338 ret = c->mtd->point(c->mtd, jeb->offset, c->sector_size,
339 &retlen, &ebuf, NULL);
Joakim Tjernlundfab2c392007-06-01 15:14:09 +0200340 if (ret) {
341 D1(printk(KERN_DEBUG "MTD point failed %d\n", ret));
342 goto do_flash_read;
343 }
344 if (retlen < c->sector_size) {
345 /* Don't muck about if it won't let us point to the whole erase sector */
346 D1(printk(KERN_DEBUG "MTD point returned len too short: 0x%zx\n", retlen));
Jared Hulberta98889f2008-04-29 23:26:49 -0700347 c->mtd->unpoint(c->mtd, jeb->offset, retlen);
Joakim Tjernlundfab2c392007-06-01 15:14:09 +0200348 goto do_flash_read;
349 }
350 wordebuf = ebuf-sizeof(*wordebuf);
351 retlen /= sizeof(*wordebuf);
352 do {
353 if (*++wordebuf != ~0)
354 break;
355 } while(--retlen);
Jared Hulberta98889f2008-04-29 23:26:49 -0700356 c->mtd->unpoint(c->mtd, jeb->offset, c->sector_size);
Anders Grafström8a0f5722008-03-12 20:29:23 +0100357 if (retlen) {
Andrew Mortonf4e35642007-08-10 14:01:30 -0700358 printk(KERN_WARNING "Newly-erased block contained word 0x%lx at offset 0x%08tx\n",
Joakim Tjernlundfab2c392007-06-01 15:14:09 +0200359 *wordebuf, jeb->offset + c->sector_size-retlen*sizeof(*wordebuf));
Anders Grafström8a0f5722008-03-12 20:29:23 +0100360 return -EIO;
361 }
Joakim Tjernlundfab2c392007-06-01 15:14:09 +0200362 return 0;
363 }
364 do_flash_read:
Thomas Gleixner5d157882005-07-15 08:14:44 +0200365 ebuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
366 if (!ebuf) {
367 printk(KERN_WARNING "Failed to allocate page buffer for verifying erase at 0x%08x. Refiling\n", jeb->offset);
368 return -EAGAIN;
369 }
370
371 D1(printk(KERN_DEBUG "Verifying erase at 0x%08x\n", jeb->offset));
372
373 for (ofs = jeb->offset; ofs < jeb->offset + c->sector_size; ) {
374 uint32_t readlen = min((uint32_t)PAGE_SIZE, jeb->offset + c->sector_size - ofs);
375 int i;
376
377 *bad_offset = ofs;
378
Artem Bityutskiyb0afbbe2007-03-21 11:07:05 +0200379 ret = c->mtd->read(c->mtd, ofs, readlen, &retlen, ebuf);
Thomas Gleixner5d157882005-07-15 08:14:44 +0200380 if (ret) {
381 printk(KERN_WARNING "Read of newly-erased block at 0x%08x failed: %d. Putting on bad_list\n", ofs, ret);
Anders Grafström8a0f5722008-03-12 20:29:23 +0100382 ret = -EIO;
Thomas Gleixner5d157882005-07-15 08:14:44 +0200383 goto fail;
384 }
385 if (retlen != readlen) {
386 printk(KERN_WARNING "Short read from newly-erased block at 0x%08x. Wanted %d, got %zd\n", ofs, readlen, retlen);
Anders Grafström8a0f5722008-03-12 20:29:23 +0100387 ret = -EIO;
Thomas Gleixner5d157882005-07-15 08:14:44 +0200388 goto fail;
389 }
390 for (i=0; i<readlen; i += sizeof(unsigned long)) {
391 /* It's OK. We know it's properly aligned */
392 unsigned long *datum = ebuf + i;
393 if (*datum + 1) {
394 *bad_offset += i;
395 printk(KERN_WARNING "Newly-erased block contained word 0x%lx at offset 0x%08x\n", *datum, *bad_offset);
Anders Grafström8a0f5722008-03-12 20:29:23 +0100396 ret = -EIO;
Thomas Gleixner5d157882005-07-15 08:14:44 +0200397 goto fail;
398 }
399 }
400 ofs += readlen;
401 cond_resched();
402 }
403 ret = 0;
404fail:
405 kfree(ebuf);
406 return ret;
407}
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
410{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 size_t retlen;
412 int ret;
Andrew Mortonf4e35642007-08-10 14:01:30 -0700413 uint32_t uninitialized_var(bad_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Thomas Gleixner5d157882005-07-15 08:14:44 +0200415 switch (jffs2_block_check_erase(c, jeb, &bad_offset)) {
416 case -EAGAIN: goto refile;
417 case -EIO: goto filebad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000420 /* Write the erase complete marker */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 D1(printk(KERN_DEBUG "Writing erased marker to block at 0x%08x\n", jeb->offset));
Thomas Gleixner5d157882005-07-15 08:14:44 +0200422 bad_offset = jeb->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Thomas Gleixner5d157882005-07-15 08:14:44 +0200424 /* Cleanmarker in oob area or no cleanmarker at all ? */
425 if (jffs2_cleanmarker_oob(c) || c->cleanmarker_size == 0) {
426
427 if (jffs2_cleanmarker_oob(c)) {
428 if (jffs2_write_nand_cleanmarker(c, jeb))
429 goto filebad;
430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 } else {
Thomas Gleixner5d157882005-07-15 08:14:44 +0200432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 struct kvec vecs[1];
434 struct jffs2_unknown_node marker = {
435 .magic = cpu_to_je16(JFFS2_MAGIC_BITMASK),
436 .nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER),
437 .totlen = cpu_to_je32(c->cleanmarker_size)
438 };
439
David Woodhouse046b8b92006-05-25 01:50:35 +0100440 jffs2_prealloc_raw_node_refs(c, jeb, 1);
Thomas Gleixner5d157882005-07-15 08:14:44 +0200441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 marker.hdr_crc = cpu_to_je32(crc32(0, &marker, sizeof(struct jffs2_unknown_node)-4));
443
444 vecs[0].iov_base = (unsigned char *) &marker;
445 vecs[0].iov_len = sizeof(marker);
446 ret = jffs2_flash_direct_writev(c, vecs, 1, jeb->offset, &retlen);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000447
Thomas Gleixner5d157882005-07-15 08:14:44 +0200448 if (ret || retlen != sizeof(marker)) {
449 if (ret)
450 printk(KERN_WARNING "Write clean marker to block at 0x%08x failed: %d\n",
451 jeb->offset, ret);
452 else
453 printk(KERN_WARNING "Short write to newly-erased block at 0x%08x: Wanted %zd, got %zd\n",
454 jeb->offset, sizeof(marker), retlen);
455
Thomas Gleixner5d157882005-07-15 08:14:44 +0200456 goto filebad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
David Woodhouse014b1642008-04-22 23:54:38 +0100459 /* Everything else got zeroed before the erase */
460 jeb->free_size = c->sector_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
David Woodhouseced22072008-04-22 15:13:40 +0100462 mutex_lock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 spin_lock(&c->erase_completion_lock);
David Woodhouse014b1642008-04-22 23:54:38 +0100464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 c->erasing_size -= c->sector_size;
David Woodhouse014b1642008-04-22 23:54:38 +0100466 c->free_size += c->sector_size;
467
468 /* Account for cleanmarker now, if it's in-band */
469 if (c->cleanmarker_size && !jffs2_cleanmarker_oob(c))
470 jffs2_link_node_ref(c, jeb, jeb->offset | REF_NORMAL, c->cleanmarker_size, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
David Woodhousee2bc3222008-04-23 14:15:24 +0100472 list_move_tail(&jeb->list, &c->free_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 c->nr_erasing_blocks--;
474 c->nr_free_blocks++;
David Woodhouse85a62db2008-04-23 01:17:51 +0100475
476 jffs2_dbg_acct_sanity_check_nolock(c, jeb);
477 jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +0100480 mutex_unlock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 wake_up(&c->erase_wait);
Thomas Gleixner5d157882005-07-15 08:14:44 +0200482 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Thomas Gleixner5d157882005-07-15 08:14:44 +0200484filebad:
Thomas Gleixner5d157882005-07-15 08:14:44 +0200485 jffs2_erase_failed(c, jeb, bad_offset);
486 return;
487
488refile:
489 /* Stick it back on the list from whence it came and come back later */
490 jffs2_erase_pending_trigger(c);
David Woodhouseced22072008-04-22 15:13:40 +0100491 mutex_lock(&c->erase_free_sem);
Thomas Gleixner5d157882005-07-15 08:14:44 +0200492 spin_lock(&c->erase_completion_lock);
David Woodhousee2bc3222008-04-23 14:15:24 +0100493 list_move(&jeb->list, &c->erase_complete_list);
Thomas Gleixner5d157882005-07-15 08:14:44 +0200494 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +0100495 mutex_unlock(&c->erase_free_sem);
Thomas Gleixner5d157882005-07-15 08:14:44 +0200496 return;
497}