blob: 5f14309a1faf2c64a3473e590f59dc3afeafcd59 [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.
David Woodhouse6088c052010-08-08 14:15:22 +01005 * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Created by David Woodhouse <dwmw2@infradead.org>
8 *
9 * For licensing information, see the file 'LICENCE' in this directory.
10 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
13#include <linux/kernel.h>
14#include <linux/slab.h>
15#include <linux/mtd/mtd.h>
16#include <linux/compiler.h>
17#include <linux/crc32.h>
18#include <linux/sched.h>
19#include <linux/pagemap.h>
20#include "nodelist.h"
21
22struct erase_priv_struct {
23 struct jffs2_eraseblock *jeb;
24 struct jffs2_sb_info *c;
25};
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#ifndef __ECOS
28static void jffs2_erase_callback(struct erase_info *);
29#endif
30static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset);
31static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
33
34static void jffs2_erase_block(struct jffs2_sb_info *c,
35 struct jffs2_eraseblock *jeb)
36{
37 int ret;
38 uint32_t bad_offset;
39#ifdef __ECOS
40 ret = jffs2_flash_erase(c, jeb);
41 if (!ret) {
David Woodhouseef53cb02007-07-10 10:01:22 +010042 jffs2_erase_succeeded(c, jeb);
43 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 }
45 bad_offset = jeb->offset;
46#else /* Linux */
47 struct erase_info *instr;
48
Joe Perches9c261b32012-02-15 15:56:43 -080049 jffs2_dbg(1, "%s(): erase block %#08x (range %#08x-%#08x)\n",
50 __func__,
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) {
Joe Perchesda320f02012-02-15 15:56:44 -080054 pr_warn("kmalloc for struct erase_info in jffs2_erase_block failed. Refiling block for later\n");
David Woodhouseced22072008-04-22 15:13:40 +010055 mutex_lock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 spin_lock(&c->erase_completion_lock);
Akinobu Mitaf1166292006-06-26 00:24:46 -070057 list_move(&jeb->list, &c->erase_pending_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 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);
David Woodhouseced22072008-04-22 15:13:40 +010062 mutex_unlock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 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]);
Adrian Hunterbb0eb212008-08-12 12:40:50 +030073 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
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
Artem Bityutskiy7e1f0dc2011-12-23 15:25:39 +020078 ret = mtd_erase(c->mtd, instr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 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 */
Joe Perches9c261b32012-02-15 15:56:43 -080088 jffs2_dbg(1, "Erase at 0x%08x failed: %d. Refiling on erase_pending_list\n",
89 jeb->offset, ret);
David Woodhouseced22072008-04-22 15:13:40 +010090 mutex_lock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 spin_lock(&c->erase_completion_lock);
Akinobu Mitaf1166292006-06-26 00:24:46 -070092 list_move(&jeb->list, &c->erase_pending_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 c->erasing_size -= c->sector_size;
94 c->dirty_size += c->sector_size;
95 jeb->dirty_size = c->sector_size;
96 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +010097 mutex_unlock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return;
99 }
100
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000101 if (ret == -EROFS)
Joe Perchesda320f02012-02-15 15:56:44 -0800102 pr_warn("Erase at 0x%08x failed immediately: -EROFS. Is the sector locked?\n",
103 jeb->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 else
Joe Perchesda320f02012-02-15 15:56:44 -0800105 pr_warn("Erase at 0x%08x failed immediately: errno %d\n",
106 jeb->offset, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 jffs2_erase_failed(c, jeb, bad_offset);
109}
110
Joakim Tjernlund9957abe2010-05-19 16:32:52 +0100111int jffs2_erase_pending_blocks(struct jffs2_sb_info *c, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113 struct jffs2_eraseblock *jeb;
Joakim Tjernlund9957abe2010-05-19 16:32:52 +0100114 int work_done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
David Woodhouseced22072008-04-22 15:13:40 +0100116 mutex_lock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 spin_lock(&c->erase_completion_lock);
119
120 while (!list_empty(&c->erase_complete_list) ||
121 !list_empty(&c->erase_pending_list)) {
122
123 if (!list_empty(&c->erase_complete_list)) {
124 jeb = list_entry(c->erase_complete_list.next, struct jffs2_eraseblock, list);
David Woodhousee2bc3222008-04-23 14:15:24 +0100125 list_move(&jeb->list, &c->erase_checking_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +0100127 mutex_unlock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 jffs2_mark_erased_block(c, jeb);
129
Joakim Tjernlund9957abe2010-05-19 16:32:52 +0100130 work_done++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 if (!--count) {
Joe Perches9c261b32012-02-15 15:56:43 -0800132 jffs2_dbg(1, "Count reached. jffs2_erase_pending_blocks leaving\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 goto done;
134 }
135
136 } else if (!list_empty(&c->erase_pending_list)) {
137 jeb = list_entry(c->erase_pending_list.next, struct jffs2_eraseblock, list);
Joe Perches9c261b32012-02-15 15:56:43 -0800138 jffs2_dbg(1, "Starting erase of pending block 0x%08x\n",
139 jeb->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 list_del(&jeb->list);
141 c->erasing_size += c->sector_size;
142 c->wasted_size -= jeb->wasted_size;
143 c->free_size -= jeb->free_size;
144 c->used_size -= jeb->used_size;
145 c->dirty_size -= jeb->dirty_size;
146 jeb->wasted_size = jeb->used_size = jeb->dirty_size = jeb->free_size = 0;
David Woodhousec38c1b62006-05-25 01:38:27 +0100147 jffs2_free_jeb_node_refs(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 list_add(&jeb->list, &c->erasing_list);
149 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +0100150 mutex_unlock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 jffs2_erase_block(c, jeb);
153
154 } else {
155 BUG();
156 }
157
158 /* Be nice */
Wolfram Sang3866f672010-09-01 18:03:41 +0200159 cond_resched();
David Woodhouseced22072008-04-22 15:13:40 +0100160 mutex_lock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 spin_lock(&c->erase_completion_lock);
162 }
163
164 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +0100165 mutex_unlock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 done:
Joe Perches9c261b32012-02-15 15:56:43 -0800167 jffs2_dbg(1, "jffs2_erase_pending_blocks completed\n");
Joakim Tjernlund9957abe2010-05-19 16:32:52 +0100168 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
171static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
172{
Joe Perches9c261b32012-02-15 15:56:43 -0800173 jffs2_dbg(1, "Erase completed successfully at 0x%08x\n", jeb->offset);
David Woodhouseced22072008-04-22 15:13:40 +0100174 mutex_lock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 spin_lock(&c->erase_completion_lock);
Akinobu Mitaf1166292006-06-26 00:24:46 -0700176 list_move_tail(&jeb->list, &c->erase_complete_list);
David Woodhouseae3b6ba2010-05-19 17:05:14 +0100177 /* Wake the GC thread to mark them clean */
178 jffs2_garbage_collect_trigger(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +0100180 mutex_unlock(&c->erase_free_sem);
David Woodhouse0717bf82010-05-19 16:37:13 +0100181 wake_up(&c->erase_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
184static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset)
185{
186 /* For NAND, if the failure did not occur at the device level for a
187 specific physical page, don't bother updating the bad block table. */
Adrian Hunter69423d92008-12-10 13:37:21 +0000188 if (jffs2_cleanmarker_oob(c) && (bad_offset != (uint32_t)MTD_FAIL_ADDR_UNKNOWN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 /* We had a device-level failure to erase. Let's see if we've
190 failed too many times. */
191 if (!jffs2_write_nand_badblock(c, jeb, bad_offset)) {
192 /* We'd like to give this block another try. */
David Woodhouseced22072008-04-22 15:13:40 +0100193 mutex_lock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 spin_lock(&c->erase_completion_lock);
Akinobu Mitaf1166292006-06-26 00:24:46 -0700195 list_move(&jeb->list, &c->erase_pending_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 c->erasing_size -= c->sector_size;
197 c->dirty_size += c->sector_size;
198 jeb->dirty_size = c->sector_size;
199 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +0100200 mutex_unlock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return;
202 }
203 }
204
David Woodhouseced22072008-04-22 15:13:40 +0100205 mutex_lock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 spin_lock(&c->erase_completion_lock);
207 c->erasing_size -= c->sector_size;
208 c->bad_size += c->sector_size;
Akinobu Mitaf1166292006-06-26 00:24:46 -0700209 list_move(&jeb->list, &c->bad_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 c->nr_erasing_blocks--;
211 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +0100212 mutex_unlock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 wake_up(&c->erase_wait);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000214}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216#ifndef __ECOS
217static void jffs2_erase_callback(struct erase_info *instr)
218{
219 struct erase_priv_struct *priv = (void *)instr->priv;
220
221 if(instr->state != MTD_ERASE_DONE) {
Joe Perchesda320f02012-02-15 15:56:44 -0800222 pr_warn("Erase at 0x%08llx finished, but state != MTD_ERASE_DONE. State is 0x%x instead.\n",
Adrian Hunter69423d92008-12-10 13:37:21 +0000223 (unsigned long long)instr->addr, instr->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 jffs2_erase_failed(priv->c, priv->jeb, instr->fail_addr);
225 } else {
226 jffs2_erase_succeeded(priv->c, priv->jeb);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 kfree(instr);
229}
230#endif /* !__ECOS */
231
232/* Hmmm. Maybe we should accept the extra space it takes and make
233 this a standard doubly-linked list? */
234static inline void jffs2_remove_node_refs_from_ino_list(struct jffs2_sb_info *c,
235 struct jffs2_raw_node_ref *ref, struct jffs2_eraseblock *jeb)
236{
237 struct jffs2_inode_cache *ic = NULL;
238 struct jffs2_raw_node_ref **prev;
239
240 prev = &ref->next_in_ino;
241
242 /* Walk the inode's list once, removing any nodes from this eraseblock */
243 while (1) {
244 if (!(*prev)->next_in_ino) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000245 /* We're looking at the jffs2_inode_cache, which is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 at the end of the linked list. Stash it and continue
247 from the beginning of the list */
248 ic = (struct jffs2_inode_cache *)(*prev);
249 prev = &ic->nodes;
250 continue;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Andrew Victor3be36672005-02-09 09:09:05 +0000253 if (SECTOR_ADDR((*prev)->flash_offset) == jeb->offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 /* It's in the block we're erasing */
255 struct jffs2_raw_node_ref *this;
256
257 this = *prev;
258 *prev = this->next_in_ino;
259 this->next_in_ino = NULL;
260
261 if (this == ref)
262 break;
263
264 continue;
265 }
266 /* Not to be deleted. Skip */
267 prev = &((*prev)->next_in_ino);
268 }
269
270 /* PARANOIA */
271 if (!ic) {
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900272 JFFS2_WARNING("inode_cache/xattr_datum/xattr_ref"
273 " not found in remove_node_refs()!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return;
275 }
276
Joe Perches9c261b32012-02-15 15:56:43 -0800277 jffs2_dbg(1, "Removed nodes in range 0x%08x-0x%08x from ino #%u\n",
278 jeb->offset, jeb->offset + c->sector_size, ic->ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 D2({
281 int i=0;
282 struct jffs2_raw_node_ref *this;
Joe Perchesad361c92009-07-06 13:05:40 -0700283 printk(KERN_DEBUG "After remove_node_refs_from_ino_list: \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 this = ic->nodes;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000286
Joe Perchesad361c92009-07-06 13:05:40 -0700287 printk(KERN_DEBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 while(this) {
Joe Perchesda320f02012-02-15 15:56:44 -0800289 pr_cont("0x%08x(%d)->",
Joe Perchesad361c92009-07-06 13:05:40 -0700290 ref_offset(this), ref_flags(this));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (++i == 5) {
Joe Perchesad361c92009-07-06 13:05:40 -0700292 printk(KERN_DEBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 i=0;
294 }
295 this = this->next_in_ino;
296 }
Joe Perchesda320f02012-02-15 15:56:44 -0800297 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 });
299
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900300 switch (ic->class) {
301#ifdef CONFIG_JFFS2_FS_XATTR
302 case RAWNODE_CLASS_XATTR_DATUM:
303 jffs2_release_xattr_datum(c, (struct jffs2_xattr_datum *)ic);
304 break;
305 case RAWNODE_CLASS_XATTR_REF:
306 jffs2_release_xattr_ref(c, (struct jffs2_xattr_ref *)ic);
307 break;
308#endif
309 default:
David Woodhouse27c72b02008-05-01 18:47:17 +0100310 if (ic->nodes == (void *)ic && ic->pino_nlink == 0)
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900311 jffs2_del_ino_cache(c, ic);
312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
David Woodhousec38c1b62006-05-25 01:38:27 +0100315void jffs2_free_jeb_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
David Woodhouse9bfeb692006-05-26 21:19:05 +0100317 struct jffs2_raw_node_ref *block, *ref;
Joe Perches9c261b32012-02-15 15:56:43 -0800318 jffs2_dbg(1, "Freeing all node refs for eraseblock offset 0x%08x\n",
319 jeb->offset);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000320
David Woodhouse9bfeb692006-05-26 21:19:05 +0100321 block = ref = jeb->first_node;
322
323 while (ref) {
324 if (ref->flash_offset == REF_LINK_NODE) {
325 ref = ref->next_in_ino;
326 jffs2_free_refblock(block);
327 block = ref;
328 continue;
329 }
330 if (ref->flash_offset != REF_EMPTY_NODE && ref->next_in_ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 jffs2_remove_node_refs_from_ino_list(c, ref, jeb);
332 /* else it was a non-inode node or already removed, so don't bother */
333
David Woodhouse9bfeb692006-05-26 21:19:05 +0100334 ref++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
David Woodhouse9bfeb692006-05-26 21:19:05 +0100336 jeb->first_node = jeb->last_node = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
Thomas Gleixner5d157882005-07-15 08:14:44 +0200339static int jffs2_block_check_erase(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t *bad_offset)
340{
341 void *ebuf;
342 uint32_t ofs;
343 size_t retlen;
Artem Bityutskiybce41d62012-01-10 15:32:29 +0200344 int ret;
Artem Bityutskiy10934472011-12-28 15:55:42 +0200345 unsigned long *wordebuf;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000346
Artem Bityutskiy10934472011-12-28 15:55:42 +0200347 ret = mtd_point(c->mtd, jeb->offset, c->sector_size, &retlen,
348 &ebuf, NULL);
349 if (ret != -EOPNOTSUPP) {
Joakim Tjernlundfab2c392007-06-01 15:14:09 +0200350 if (ret) {
Joe Perches9c261b32012-02-15 15:56:43 -0800351 jffs2_dbg(1, "MTD point failed %d\n", ret);
Joakim Tjernlundfab2c392007-06-01 15:14:09 +0200352 goto do_flash_read;
353 }
354 if (retlen < c->sector_size) {
355 /* Don't muck about if it won't let us point to the whole erase sector */
Joe Perches9c261b32012-02-15 15:56:43 -0800356 jffs2_dbg(1, "MTD point returned len too short: 0x%zx\n",
357 retlen);
Artem Bityutskiy72197782011-12-23 17:05:52 +0200358 mtd_unpoint(c->mtd, jeb->offset, retlen);
Joakim Tjernlundfab2c392007-06-01 15:14:09 +0200359 goto do_flash_read;
360 }
361 wordebuf = ebuf-sizeof(*wordebuf);
362 retlen /= sizeof(*wordebuf);
363 do {
364 if (*++wordebuf != ~0)
365 break;
366 } while(--retlen);
Artem Bityutskiy72197782011-12-23 17:05:52 +0200367 mtd_unpoint(c->mtd, jeb->offset, c->sector_size);
Anders Grafström8a0f5722008-03-12 20:29:23 +0100368 if (retlen) {
Joe Perchesda320f02012-02-15 15:56:44 -0800369 pr_warn("Newly-erased block contained word 0x%lx at offset 0x%08tx\n",
370 *wordebuf,
371 jeb->offset +
372 c->sector_size-retlen * sizeof(*wordebuf));
Anders Grafström8a0f5722008-03-12 20:29:23 +0100373 return -EIO;
374 }
Joakim Tjernlundfab2c392007-06-01 15:14:09 +0200375 return 0;
376 }
377 do_flash_read:
Thomas Gleixner5d157882005-07-15 08:14:44 +0200378 ebuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
379 if (!ebuf) {
Joe Perchesda320f02012-02-15 15:56:44 -0800380 pr_warn("Failed to allocate page buffer for verifying erase at 0x%08x. Refiling\n",
381 jeb->offset);
Thomas Gleixner5d157882005-07-15 08:14:44 +0200382 return -EAGAIN;
383 }
384
Joe Perches9c261b32012-02-15 15:56:43 -0800385 jffs2_dbg(1, "Verifying erase at 0x%08x\n", jeb->offset);
Thomas Gleixner5d157882005-07-15 08:14:44 +0200386
387 for (ofs = jeb->offset; ofs < jeb->offset + c->sector_size; ) {
388 uint32_t readlen = min((uint32_t)PAGE_SIZE, jeb->offset + c->sector_size - ofs);
389 int i;
390
391 *bad_offset = ofs;
392
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200393 ret = mtd_read(c->mtd, ofs, readlen, &retlen, ebuf);
Thomas Gleixner5d157882005-07-15 08:14:44 +0200394 if (ret) {
Joe Perchesda320f02012-02-15 15:56:44 -0800395 pr_warn("Read of newly-erased block at 0x%08x failed: %d. Putting on bad_list\n",
396 ofs, ret);
Anders Grafström8a0f5722008-03-12 20:29:23 +0100397 ret = -EIO;
Thomas Gleixner5d157882005-07-15 08:14:44 +0200398 goto fail;
399 }
400 if (retlen != readlen) {
Joe Perchesda320f02012-02-15 15:56:44 -0800401 pr_warn("Short read from newly-erased block at 0x%08x. Wanted %d, got %zd\n",
402 ofs, readlen, retlen);
Anders Grafström8a0f5722008-03-12 20:29:23 +0100403 ret = -EIO;
Thomas Gleixner5d157882005-07-15 08:14:44 +0200404 goto fail;
405 }
406 for (i=0; i<readlen; i += sizeof(unsigned long)) {
407 /* It's OK. We know it's properly aligned */
408 unsigned long *datum = ebuf + i;
409 if (*datum + 1) {
410 *bad_offset += i;
Joe Perchesda320f02012-02-15 15:56:44 -0800411 pr_warn("Newly-erased block contained word 0x%lx at offset 0x%08x\n",
412 *datum, *bad_offset);
Anders Grafström8a0f5722008-03-12 20:29:23 +0100413 ret = -EIO;
Thomas Gleixner5d157882005-07-15 08:14:44 +0200414 goto fail;
415 }
416 }
417 ofs += readlen;
418 cond_resched();
419 }
420 ret = 0;
421fail:
422 kfree(ebuf);
423 return ret;
424}
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
427{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 size_t retlen;
429 int ret;
Andrew Mortonf4e35642007-08-10 14:01:30 -0700430 uint32_t uninitialized_var(bad_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Thomas Gleixner5d157882005-07-15 08:14:44 +0200432 switch (jffs2_block_check_erase(c, jeb, &bad_offset)) {
433 case -EAGAIN: goto refile;
434 case -EIO: goto filebad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000437 /* Write the erase complete marker */
Joe Perches9c261b32012-02-15 15:56:43 -0800438 jffs2_dbg(1, "Writing erased marker to block at 0x%08x\n", jeb->offset);
Thomas Gleixner5d157882005-07-15 08:14:44 +0200439 bad_offset = jeb->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Thomas Gleixner5d157882005-07-15 08:14:44 +0200441 /* Cleanmarker in oob area or no cleanmarker at all ? */
442 if (jffs2_cleanmarker_oob(c) || c->cleanmarker_size == 0) {
443
444 if (jffs2_cleanmarker_oob(c)) {
445 if (jffs2_write_nand_cleanmarker(c, jeb))
446 goto filebad;
447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 } else {
Thomas Gleixner5d157882005-07-15 08:14:44 +0200449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 struct kvec vecs[1];
451 struct jffs2_unknown_node marker = {
452 .magic = cpu_to_je16(JFFS2_MAGIC_BITMASK),
453 .nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER),
454 .totlen = cpu_to_je32(c->cleanmarker_size)
455 };
456
David Woodhouse046b8b92006-05-25 01:50:35 +0100457 jffs2_prealloc_raw_node_refs(c, jeb, 1);
Thomas Gleixner5d157882005-07-15 08:14:44 +0200458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 marker.hdr_crc = cpu_to_je32(crc32(0, &marker, sizeof(struct jffs2_unknown_node)-4));
460
461 vecs[0].iov_base = (unsigned char *) &marker;
462 vecs[0].iov_len = sizeof(marker);
463 ret = jffs2_flash_direct_writev(c, vecs, 1, jeb->offset, &retlen);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000464
Thomas Gleixner5d157882005-07-15 08:14:44 +0200465 if (ret || retlen != sizeof(marker)) {
466 if (ret)
Joe Perchesda320f02012-02-15 15:56:44 -0800467 pr_warn("Write clean marker to block at 0x%08x failed: %d\n",
Thomas Gleixner5d157882005-07-15 08:14:44 +0200468 jeb->offset, ret);
469 else
Joe Perchesda320f02012-02-15 15:56:44 -0800470 pr_warn("Short write to newly-erased block at 0x%08x: Wanted %zd, got %zd\n",
Thomas Gleixner5d157882005-07-15 08:14:44 +0200471 jeb->offset, sizeof(marker), retlen);
472
Thomas Gleixner5d157882005-07-15 08:14:44 +0200473 goto filebad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
David Woodhouse014b1642008-04-22 23:54:38 +0100476 /* Everything else got zeroed before the erase */
477 jeb->free_size = c->sector_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
David Woodhouseced22072008-04-22 15:13:40 +0100479 mutex_lock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 spin_lock(&c->erase_completion_lock);
David Woodhouse014b1642008-04-22 23:54:38 +0100481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 c->erasing_size -= c->sector_size;
David Woodhouse014b1642008-04-22 23:54:38 +0100483 c->free_size += c->sector_size;
484
485 /* Account for cleanmarker now, if it's in-band */
486 if (c->cleanmarker_size && !jffs2_cleanmarker_oob(c))
487 jffs2_link_node_ref(c, jeb, jeb->offset | REF_NORMAL, c->cleanmarker_size, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
David Woodhousee2bc3222008-04-23 14:15:24 +0100489 list_move_tail(&jeb->list, &c->free_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 c->nr_erasing_blocks--;
491 c->nr_free_blocks++;
David Woodhouse85a62db2008-04-23 01:17:51 +0100492
493 jffs2_dbg_acct_sanity_check_nolock(c, jeb);
494 jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +0100497 mutex_unlock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 wake_up(&c->erase_wait);
Thomas Gleixner5d157882005-07-15 08:14:44 +0200499 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Thomas Gleixner5d157882005-07-15 08:14:44 +0200501filebad:
Thomas Gleixner5d157882005-07-15 08:14:44 +0200502 jffs2_erase_failed(c, jeb, bad_offset);
503 return;
504
505refile:
506 /* Stick it back on the list from whence it came and come back later */
David Woodhouseced22072008-04-22 15:13:40 +0100507 mutex_lock(&c->erase_free_sem);
Thomas Gleixner5d157882005-07-15 08:14:44 +0200508 spin_lock(&c->erase_completion_lock);
David Woodhouseae3b6ba2010-05-19 17:05:14 +0100509 jffs2_garbage_collect_trigger(c);
David Woodhousee2bc3222008-04-23 14:15:24 +0100510 list_move(&jeb->list, &c->erase_complete_list);
Thomas Gleixner5d157882005-07-15 08:14:44 +0200511 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +0100512 mutex_unlock(&c->erase_free_sem);
Thomas Gleixner5d157882005-07-15 08:14:44 +0200513 return;
514}