blob: b2d2b6a6e03e28d7136c6b80d3ecd9d3095c5ec7 [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
Joakim Tjernlund9957abe2010-05-19 16:32:52 +0100106int jffs2_erase_pending_blocks(struct jffs2_sb_info *c, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
108 struct jffs2_eraseblock *jeb;
Joakim Tjernlund9957abe2010-05-19 16:32:52 +0100109 int work_done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
David Woodhouseced22072008-04-22 15:13:40 +0100111 mutex_lock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
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);
David Woodhousee2bc3222008-04-23 14:15:24 +0100120 list_move(&jeb->list, &c->erase_checking_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +0100122 mutex_unlock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 jffs2_mark_erased_block(c, jeb);
124
Joakim Tjernlund9957abe2010-05-19 16:32:52 +0100125 work_done++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 if (!--count) {
127 D1(printk(KERN_DEBUG "Count reached. jffs2_erase_pending_blocks leaving\n"));
128 goto done;
129 }
130
131 } else if (!list_empty(&c->erase_pending_list)) {
132 jeb = list_entry(c->erase_pending_list.next, struct jffs2_eraseblock, list);
133 D1(printk(KERN_DEBUG "Starting erase of pending block 0x%08x\n", jeb->offset));
134 list_del(&jeb->list);
135 c->erasing_size += c->sector_size;
136 c->wasted_size -= jeb->wasted_size;
137 c->free_size -= jeb->free_size;
138 c->used_size -= jeb->used_size;
139 c->dirty_size -= jeb->dirty_size;
140 jeb->wasted_size = jeb->used_size = jeb->dirty_size = jeb->free_size = 0;
David Woodhousec38c1b62006-05-25 01:38:27 +0100141 jffs2_free_jeb_node_refs(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 list_add(&jeb->list, &c->erasing_list);
143 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +0100144 mutex_unlock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 jffs2_erase_block(c, jeb);
147
148 } else {
149 BUG();
150 }
151
152 /* Be nice */
Joakim Tjernlundfd532492007-06-26 23:32:10 +0200153 yield();
David Woodhouseced22072008-04-22 15:13:40 +0100154 mutex_lock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 spin_lock(&c->erase_completion_lock);
156 }
157
158 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +0100159 mutex_unlock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 done:
161 D1(printk(KERN_DEBUG "jffs2_erase_pending_blocks completed\n"));
Joakim Tjernlund9957abe2010-05-19 16:32:52 +0100162 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
165static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
166{
167 D1(printk(KERN_DEBUG "Erase completed successfully at 0x%08x\n", jeb->offset));
David Woodhouseced22072008-04-22 15:13:40 +0100168 mutex_lock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 spin_lock(&c->erase_completion_lock);
Akinobu Mitaf1166292006-06-26 00:24:46 -0700170 list_move_tail(&jeb->list, &c->erase_complete_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +0100172 mutex_unlock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 /* Ensure that kupdated calls us again to mark them clean */
174 jffs2_erase_pending_trigger(c);
175}
176
177static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset)
178{
179 /* For NAND, if the failure did not occur at the device level for a
180 specific physical page, don't bother updating the bad block table. */
Adrian Hunter69423d92008-12-10 13:37:21 +0000181 if (jffs2_cleanmarker_oob(c) && (bad_offset != (uint32_t)MTD_FAIL_ADDR_UNKNOWN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 /* We had a device-level failure to erase. Let's see if we've
183 failed too many times. */
184 if (!jffs2_write_nand_badblock(c, jeb, bad_offset)) {
185 /* We'd like to give this block another try. */
David Woodhouseced22072008-04-22 15:13:40 +0100186 mutex_lock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 spin_lock(&c->erase_completion_lock);
Akinobu Mitaf1166292006-06-26 00:24:46 -0700188 list_move(&jeb->list, &c->erase_pending_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 c->erasing_size -= c->sector_size;
190 c->dirty_size += c->sector_size;
191 jeb->dirty_size = c->sector_size;
192 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +0100193 mutex_unlock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return;
195 }
196 }
197
David Woodhouseced22072008-04-22 15:13:40 +0100198 mutex_lock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 spin_lock(&c->erase_completion_lock);
200 c->erasing_size -= c->sector_size;
201 c->bad_size += c->sector_size;
Akinobu Mitaf1166292006-06-26 00:24:46 -0700202 list_move(&jeb->list, &c->bad_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 c->nr_erasing_blocks--;
204 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +0100205 mutex_unlock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 wake_up(&c->erase_wait);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000207}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209#ifndef __ECOS
210static void jffs2_erase_callback(struct erase_info *instr)
211{
212 struct erase_priv_struct *priv = (void *)instr->priv;
213
214 if(instr->state != MTD_ERASE_DONE) {
Adrian Hunter69423d92008-12-10 13:37:21 +0000215 printk(KERN_WARNING "Erase at 0x%08llx finished, but state != MTD_ERASE_DONE. State is 0x%x instead.\n",
216 (unsigned long long)instr->addr, instr->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 jffs2_erase_failed(priv->c, priv->jeb, instr->fail_addr);
218 } else {
219 jffs2_erase_succeeded(priv->c, priv->jeb);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 kfree(instr);
222}
223#endif /* !__ECOS */
224
225/* Hmmm. Maybe we should accept the extra space it takes and make
226 this a standard doubly-linked list? */
227static inline void jffs2_remove_node_refs_from_ino_list(struct jffs2_sb_info *c,
228 struct jffs2_raw_node_ref *ref, struct jffs2_eraseblock *jeb)
229{
230 struct jffs2_inode_cache *ic = NULL;
231 struct jffs2_raw_node_ref **prev;
232
233 prev = &ref->next_in_ino;
234
235 /* Walk the inode's list once, removing any nodes from this eraseblock */
236 while (1) {
237 if (!(*prev)->next_in_ino) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000238 /* We're looking at the jffs2_inode_cache, which is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 at the end of the linked list. Stash it and continue
240 from the beginning of the list */
241 ic = (struct jffs2_inode_cache *)(*prev);
242 prev = &ic->nodes;
243 continue;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Andrew Victor3be36672005-02-09 09:09:05 +0000246 if (SECTOR_ADDR((*prev)->flash_offset) == jeb->offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 /* It's in the block we're erasing */
248 struct jffs2_raw_node_ref *this;
249
250 this = *prev;
251 *prev = this->next_in_ino;
252 this->next_in_ino = NULL;
253
254 if (this == ref)
255 break;
256
257 continue;
258 }
259 /* Not to be deleted. Skip */
260 prev = &((*prev)->next_in_ino);
261 }
262
263 /* PARANOIA */
264 if (!ic) {
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900265 JFFS2_WARNING("inode_cache/xattr_datum/xattr_ref"
266 " not found in remove_node_refs()!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return;
268 }
269
270 D1(printk(KERN_DEBUG "Removed nodes in range 0x%08x-0x%08x from ino #%u\n",
271 jeb->offset, jeb->offset + c->sector_size, ic->ino));
272
273 D2({
274 int i=0;
275 struct jffs2_raw_node_ref *this;
Joe Perchesad361c92009-07-06 13:05:40 -0700276 printk(KERN_DEBUG "After remove_node_refs_from_ino_list: \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 this = ic->nodes;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000279
Joe Perchesad361c92009-07-06 13:05:40 -0700280 printk(KERN_DEBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 while(this) {
Joe Perchesad361c92009-07-06 13:05:40 -0700282 printk(KERN_CONT "0x%08x(%d)->",
283 ref_offset(this), ref_flags(this));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (++i == 5) {
Joe Perchesad361c92009-07-06 13:05:40 -0700285 printk(KERN_DEBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 i=0;
287 }
288 this = this->next_in_ino;
289 }
Joe Perchesad361c92009-07-06 13:05:40 -0700290 printk(KERN_CONT "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 });
292
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900293 switch (ic->class) {
294#ifdef CONFIG_JFFS2_FS_XATTR
295 case RAWNODE_CLASS_XATTR_DATUM:
296 jffs2_release_xattr_datum(c, (struct jffs2_xattr_datum *)ic);
297 break;
298 case RAWNODE_CLASS_XATTR_REF:
299 jffs2_release_xattr_ref(c, (struct jffs2_xattr_ref *)ic);
300 break;
301#endif
302 default:
David Woodhouse27c72b02008-05-01 18:47:17 +0100303 if (ic->nodes == (void *)ic && ic->pino_nlink == 0)
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900304 jffs2_del_ino_cache(c, ic);
305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
David Woodhousec38c1b62006-05-25 01:38:27 +0100308void jffs2_free_jeb_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
David Woodhouse9bfeb692006-05-26 21:19:05 +0100310 struct jffs2_raw_node_ref *block, *ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 D1(printk(KERN_DEBUG "Freeing all node refs for eraseblock offset 0x%08x\n", jeb->offset));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000312
David Woodhouse9bfeb692006-05-26 21:19:05 +0100313 block = ref = jeb->first_node;
314
315 while (ref) {
316 if (ref->flash_offset == REF_LINK_NODE) {
317 ref = ref->next_in_ino;
318 jffs2_free_refblock(block);
319 block = ref;
320 continue;
321 }
322 if (ref->flash_offset != REF_EMPTY_NODE && ref->next_in_ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 jffs2_remove_node_refs_from_ino_list(c, ref, jeb);
324 /* else it was a non-inode node or already removed, so don't bother */
325
David Woodhouse9bfeb692006-05-26 21:19:05 +0100326 ref++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
David Woodhouse9bfeb692006-05-26 21:19:05 +0100328 jeb->first_node = jeb->last_node = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
Thomas Gleixner5d157882005-07-15 08:14:44 +0200331static int jffs2_block_check_erase(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t *bad_offset)
332{
333 void *ebuf;
334 uint32_t ofs;
335 size_t retlen;
336 int ret = -EIO;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000337
Joakim Tjernlundfab2c392007-06-01 15:14:09 +0200338 if (c->mtd->point) {
339 unsigned long *wordebuf;
340
Jared Hulberta98889f2008-04-29 23:26:49 -0700341 ret = c->mtd->point(c->mtd, jeb->offset, c->sector_size,
342 &retlen, &ebuf, NULL);
Joakim Tjernlundfab2c392007-06-01 15:14:09 +0200343 if (ret) {
344 D1(printk(KERN_DEBUG "MTD point failed %d\n", ret));
345 goto do_flash_read;
346 }
347 if (retlen < c->sector_size) {
348 /* Don't muck about if it won't let us point to the whole erase sector */
349 D1(printk(KERN_DEBUG "MTD point returned len too short: 0x%zx\n", retlen));
Jared Hulberta98889f2008-04-29 23:26:49 -0700350 c->mtd->unpoint(c->mtd, jeb->offset, retlen);
Joakim Tjernlundfab2c392007-06-01 15:14:09 +0200351 goto do_flash_read;
352 }
353 wordebuf = ebuf-sizeof(*wordebuf);
354 retlen /= sizeof(*wordebuf);
355 do {
356 if (*++wordebuf != ~0)
357 break;
358 } while(--retlen);
Jared Hulberta98889f2008-04-29 23:26:49 -0700359 c->mtd->unpoint(c->mtd, jeb->offset, c->sector_size);
Anders Grafström8a0f5722008-03-12 20:29:23 +0100360 if (retlen) {
Andrew Mortonf4e35642007-08-10 14:01:30 -0700361 printk(KERN_WARNING "Newly-erased block contained word 0x%lx at offset 0x%08tx\n",
Joakim Tjernlundfab2c392007-06-01 15:14:09 +0200362 *wordebuf, jeb->offset + c->sector_size-retlen*sizeof(*wordebuf));
Anders Grafström8a0f5722008-03-12 20:29:23 +0100363 return -EIO;
364 }
Joakim Tjernlundfab2c392007-06-01 15:14:09 +0200365 return 0;
366 }
367 do_flash_read:
Thomas Gleixner5d157882005-07-15 08:14:44 +0200368 ebuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
369 if (!ebuf) {
370 printk(KERN_WARNING "Failed to allocate page buffer for verifying erase at 0x%08x. Refiling\n", jeb->offset);
371 return -EAGAIN;
372 }
373
374 D1(printk(KERN_DEBUG "Verifying erase at 0x%08x\n", jeb->offset));
375
376 for (ofs = jeb->offset; ofs < jeb->offset + c->sector_size; ) {
377 uint32_t readlen = min((uint32_t)PAGE_SIZE, jeb->offset + c->sector_size - ofs);
378 int i;
379
380 *bad_offset = ofs;
381
Artem Bityutskiyb0afbbe2007-03-21 11:07:05 +0200382 ret = c->mtd->read(c->mtd, ofs, readlen, &retlen, ebuf);
Thomas Gleixner5d157882005-07-15 08:14:44 +0200383 if (ret) {
384 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 +0100385 ret = -EIO;
Thomas Gleixner5d157882005-07-15 08:14:44 +0200386 goto fail;
387 }
388 if (retlen != readlen) {
389 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 +0100390 ret = -EIO;
Thomas Gleixner5d157882005-07-15 08:14:44 +0200391 goto fail;
392 }
393 for (i=0; i<readlen; i += sizeof(unsigned long)) {
394 /* It's OK. We know it's properly aligned */
395 unsigned long *datum = ebuf + i;
396 if (*datum + 1) {
397 *bad_offset += i;
398 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 +0100399 ret = -EIO;
Thomas Gleixner5d157882005-07-15 08:14:44 +0200400 goto fail;
401 }
402 }
403 ofs += readlen;
404 cond_resched();
405 }
406 ret = 0;
407fail:
408 kfree(ebuf);
409 return ret;
410}
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
413{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 size_t retlen;
415 int ret;
Andrew Mortonf4e35642007-08-10 14:01:30 -0700416 uint32_t uninitialized_var(bad_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Thomas Gleixner5d157882005-07-15 08:14:44 +0200418 switch (jffs2_block_check_erase(c, jeb, &bad_offset)) {
419 case -EAGAIN: goto refile;
420 case -EIO: goto filebad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000423 /* Write the erase complete marker */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 D1(printk(KERN_DEBUG "Writing erased marker to block at 0x%08x\n", jeb->offset));
Thomas Gleixner5d157882005-07-15 08:14:44 +0200425 bad_offset = jeb->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Thomas Gleixner5d157882005-07-15 08:14:44 +0200427 /* Cleanmarker in oob area or no cleanmarker at all ? */
428 if (jffs2_cleanmarker_oob(c) || c->cleanmarker_size == 0) {
429
430 if (jffs2_cleanmarker_oob(c)) {
431 if (jffs2_write_nand_cleanmarker(c, jeb))
432 goto filebad;
433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 } else {
Thomas Gleixner5d157882005-07-15 08:14:44 +0200435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 struct kvec vecs[1];
437 struct jffs2_unknown_node marker = {
438 .magic = cpu_to_je16(JFFS2_MAGIC_BITMASK),
439 .nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER),
440 .totlen = cpu_to_je32(c->cleanmarker_size)
441 };
442
David Woodhouse046b8b92006-05-25 01:50:35 +0100443 jffs2_prealloc_raw_node_refs(c, jeb, 1);
Thomas Gleixner5d157882005-07-15 08:14:44 +0200444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 marker.hdr_crc = cpu_to_je32(crc32(0, &marker, sizeof(struct jffs2_unknown_node)-4));
446
447 vecs[0].iov_base = (unsigned char *) &marker;
448 vecs[0].iov_len = sizeof(marker);
449 ret = jffs2_flash_direct_writev(c, vecs, 1, jeb->offset, &retlen);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000450
Thomas Gleixner5d157882005-07-15 08:14:44 +0200451 if (ret || retlen != sizeof(marker)) {
452 if (ret)
453 printk(KERN_WARNING "Write clean marker to block at 0x%08x failed: %d\n",
454 jeb->offset, ret);
455 else
456 printk(KERN_WARNING "Short write to newly-erased block at 0x%08x: Wanted %zd, got %zd\n",
457 jeb->offset, sizeof(marker), retlen);
458
Thomas Gleixner5d157882005-07-15 08:14:44 +0200459 goto filebad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
David Woodhouse014b1642008-04-22 23:54:38 +0100462 /* Everything else got zeroed before the erase */
463 jeb->free_size = c->sector_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
David Woodhouseced22072008-04-22 15:13:40 +0100465 mutex_lock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 spin_lock(&c->erase_completion_lock);
David Woodhouse014b1642008-04-22 23:54:38 +0100467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 c->erasing_size -= c->sector_size;
David Woodhouse014b1642008-04-22 23:54:38 +0100469 c->free_size += c->sector_size;
470
471 /* Account for cleanmarker now, if it's in-band */
472 if (c->cleanmarker_size && !jffs2_cleanmarker_oob(c))
473 jffs2_link_node_ref(c, jeb, jeb->offset | REF_NORMAL, c->cleanmarker_size, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
David Woodhousee2bc3222008-04-23 14:15:24 +0100475 list_move_tail(&jeb->list, &c->free_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 c->nr_erasing_blocks--;
477 c->nr_free_blocks++;
David Woodhouse85a62db2008-04-23 01:17:51 +0100478
479 jffs2_dbg_acct_sanity_check_nolock(c, jeb);
480 jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +0100483 mutex_unlock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 wake_up(&c->erase_wait);
Thomas Gleixner5d157882005-07-15 08:14:44 +0200485 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Thomas Gleixner5d157882005-07-15 08:14:44 +0200487filebad:
Thomas Gleixner5d157882005-07-15 08:14:44 +0200488 jffs2_erase_failed(c, jeb, bad_offset);
489 return;
490
491refile:
492 /* Stick it back on the list from whence it came and come back later */
493 jffs2_erase_pending_trigger(c);
David Woodhouseced22072008-04-22 15:13:40 +0100494 mutex_lock(&c->erase_free_sem);
Thomas Gleixner5d157882005-07-15 08:14:44 +0200495 spin_lock(&c->erase_completion_lock);
David Woodhousee2bc3222008-04-23 14:15:24 +0100496 list_move(&jeb->list, &c->erase_complete_list);
Thomas Gleixner5d157882005-07-15 08:14:44 +0200497 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +0100498 mutex_unlock(&c->erase_free_sem);
Thomas Gleixner5d157882005-07-15 08:14:44 +0200499 return;
500}