blob: e7594c604d2885d5cc50c14f108c9874e6974101 [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
Joe Perches5a528952012-02-15 15:56:45 -080013#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/kernel.h>
16#include <linux/slab.h>
17#include <linux/mtd/mtd.h>
18#include <linux/compiler.h>
19#include <linux/crc32.h>
20#include <linux/sched.h>
21#include <linux/pagemap.h>
22#include "nodelist.h"
23
24struct erase_priv_struct {
25 struct jffs2_eraseblock *jeb;
26 struct jffs2_sb_info *c;
27};
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#ifndef __ECOS
30static void jffs2_erase_callback(struct erase_info *);
31#endif
32static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset);
33static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034static 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) {
David Woodhouseef53cb02007-07-10 10:01:22 +010044 jffs2_erase_succeeded(c, jeb);
45 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 }
47 bad_offset = jeb->offset;
48#else /* Linux */
49 struct erase_info *instr;
50
Joe Perches9c261b32012-02-15 15:56:43 -080051 jffs2_dbg(1, "%s(): erase block %#08x (range %#08x-%#08x)\n",
52 __func__,
53 jeb->offset, jeb->offset, jeb->offset + c->sector_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 instr = kmalloc(sizeof(struct erase_info) + sizeof(struct erase_priv_struct), GFP_KERNEL);
55 if (!instr) {
Joe Perchesda320f02012-02-15 15:56:44 -080056 pr_warn("kmalloc for struct erase_info in jffs2_erase_block failed. Refiling block for later\n");
David Woodhouseced22072008-04-22 15:13:40 +010057 mutex_lock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 spin_lock(&c->erase_completion_lock);
Akinobu Mitaf1166292006-06-26 00:24:46 -070059 list_move(&jeb->list, &c->erase_pending_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 c->erasing_size -= c->sector_size;
61 c->dirty_size += c->sector_size;
62 jeb->dirty_size = c->sector_size;
63 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +010064 mutex_unlock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 return;
66 }
67
68 memset(instr, 0, sizeof(*instr));
69
70 instr->mtd = c->mtd;
71 instr->addr = jeb->offset;
72 instr->len = c->sector_size;
73 instr->callback = jffs2_erase_callback;
74 instr->priv = (unsigned long)(&instr[1]);
Adrian Hunterbb0eb212008-08-12 12:40:50 +030075 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 ((struct erase_priv_struct *)instr->priv)->jeb = jeb;
78 ((struct erase_priv_struct *)instr->priv)->c = c;
79
Artem Bityutskiy7e1f0dc2011-12-23 15:25:39 +020080 ret = mtd_erase(c->mtd, instr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 if (!ret)
82 return;
83
84 bad_offset = instr->fail_addr;
85 kfree(instr);
86#endif /* __ECOS */
87
88 if (ret == -ENOMEM || ret == -EAGAIN) {
89 /* Erase failed immediately. Refile it on the list */
Joe Perches9c261b32012-02-15 15:56:43 -080090 jffs2_dbg(1, "Erase at 0x%08x failed: %d. Refiling on erase_pending_list\n",
91 jeb->offset, ret);
David Woodhouseced22072008-04-22 15:13:40 +010092 mutex_lock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 spin_lock(&c->erase_completion_lock);
Akinobu Mitaf1166292006-06-26 00:24:46 -070094 list_move(&jeb->list, &c->erase_pending_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 c->erasing_size -= c->sector_size;
96 c->dirty_size += c->sector_size;
97 jeb->dirty_size = c->sector_size;
98 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +010099 mutex_unlock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return;
101 }
102
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000103 if (ret == -EROFS)
Joe Perchesda320f02012-02-15 15:56:44 -0800104 pr_warn("Erase at 0x%08x failed immediately: -EROFS. Is the sector locked?\n",
105 jeb->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 else
Joe Perchesda320f02012-02-15 15:56:44 -0800107 pr_warn("Erase at 0x%08x failed immediately: errno %d\n",
108 jeb->offset, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 jffs2_erase_failed(c, jeb, bad_offset);
111}
112
Joakim Tjernlund9957abe2010-05-19 16:32:52 +0100113int jffs2_erase_pending_blocks(struct jffs2_sb_info *c, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115 struct jffs2_eraseblock *jeb;
Joakim Tjernlund9957abe2010-05-19 16:32:52 +0100116 int work_done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
David Woodhouseced22072008-04-22 15:13:40 +0100118 mutex_lock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 spin_lock(&c->erase_completion_lock);
121
122 while (!list_empty(&c->erase_complete_list) ||
123 !list_empty(&c->erase_pending_list)) {
124
125 if (!list_empty(&c->erase_complete_list)) {
126 jeb = list_entry(c->erase_complete_list.next, struct jffs2_eraseblock, list);
David Woodhousee2bc3222008-04-23 14:15:24 +0100127 list_move(&jeb->list, &c->erase_checking_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +0100129 mutex_unlock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 jffs2_mark_erased_block(c, jeb);
131
Joakim Tjernlund9957abe2010-05-19 16:32:52 +0100132 work_done++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 if (!--count) {
Joe Perches9c261b32012-02-15 15:56:43 -0800134 jffs2_dbg(1, "Count reached. jffs2_erase_pending_blocks leaving\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 goto done;
136 }
137
138 } else if (!list_empty(&c->erase_pending_list)) {
139 jeb = list_entry(c->erase_pending_list.next, struct jffs2_eraseblock, list);
Joe Perches9c261b32012-02-15 15:56:43 -0800140 jffs2_dbg(1, "Starting erase of pending block 0x%08x\n",
141 jeb->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 list_del(&jeb->list);
143 c->erasing_size += c->sector_size;
144 c->wasted_size -= jeb->wasted_size;
145 c->free_size -= jeb->free_size;
146 c->used_size -= jeb->used_size;
147 c->dirty_size -= jeb->dirty_size;
148 jeb->wasted_size = jeb->used_size = jeb->dirty_size = jeb->free_size = 0;
David Woodhousec38c1b62006-05-25 01:38:27 +0100149 jffs2_free_jeb_node_refs(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 list_add(&jeb->list, &c->erasing_list);
151 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +0100152 mutex_unlock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 jffs2_erase_block(c, jeb);
155
156 } else {
157 BUG();
158 }
159
160 /* Be nice */
Wolfram Sang3866f672010-09-01 18:03:41 +0200161 cond_resched();
David Woodhouseced22072008-04-22 15:13:40 +0100162 mutex_lock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 spin_lock(&c->erase_completion_lock);
164 }
165
166 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +0100167 mutex_unlock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 done:
Joe Perches9c261b32012-02-15 15:56:43 -0800169 jffs2_dbg(1, "jffs2_erase_pending_blocks completed\n");
Joakim Tjernlund9957abe2010-05-19 16:32:52 +0100170 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
173static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
174{
Joe Perches9c261b32012-02-15 15:56:43 -0800175 jffs2_dbg(1, "Erase completed successfully at 0x%08x\n", jeb->offset);
David Woodhouseced22072008-04-22 15:13:40 +0100176 mutex_lock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 spin_lock(&c->erase_completion_lock);
Akinobu Mitaf1166292006-06-26 00:24:46 -0700178 list_move_tail(&jeb->list, &c->erase_complete_list);
David Woodhouseae3b6ba2010-05-19 17:05:14 +0100179 /* Wake the GC thread to mark them clean */
180 jffs2_garbage_collect_trigger(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +0100182 mutex_unlock(&c->erase_free_sem);
David Woodhouse0717bf82010-05-19 16:37:13 +0100183 wake_up(&c->erase_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
186static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset)
187{
188 /* For NAND, if the failure did not occur at the device level for a
189 specific physical page, don't bother updating the bad block table. */
Adrian Hunter69423d92008-12-10 13:37:21 +0000190 if (jffs2_cleanmarker_oob(c) && (bad_offset != (uint32_t)MTD_FAIL_ADDR_UNKNOWN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 /* We had a device-level failure to erase. Let's see if we've
192 failed too many times. */
193 if (!jffs2_write_nand_badblock(c, jeb, bad_offset)) {
194 /* We'd like to give this block another try. */
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);
Akinobu Mitaf1166292006-06-26 00:24:46 -0700197 list_move(&jeb->list, &c->erase_pending_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 c->erasing_size -= c->sector_size;
199 c->dirty_size += c->sector_size;
200 jeb->dirty_size = c->sector_size;
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 return;
204 }
205 }
206
David Woodhouseced22072008-04-22 15:13:40 +0100207 mutex_lock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 spin_lock(&c->erase_completion_lock);
209 c->erasing_size -= c->sector_size;
210 c->bad_size += c->sector_size;
Akinobu Mitaf1166292006-06-26 00:24:46 -0700211 list_move(&jeb->list, &c->bad_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 c->nr_erasing_blocks--;
213 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +0100214 mutex_unlock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 wake_up(&c->erase_wait);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000216}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218#ifndef __ECOS
219static void jffs2_erase_callback(struct erase_info *instr)
220{
221 struct erase_priv_struct *priv = (void *)instr->priv;
222
223 if(instr->state != MTD_ERASE_DONE) {
Joe Perchesda320f02012-02-15 15:56:44 -0800224 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 +0000225 (unsigned long long)instr->addr, instr->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 jffs2_erase_failed(priv->c, priv->jeb, instr->fail_addr);
227 } else {
228 jffs2_erase_succeeded(priv->c, priv->jeb);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 kfree(instr);
231}
232#endif /* !__ECOS */
233
234/* Hmmm. Maybe we should accept the extra space it takes and make
235 this a standard doubly-linked list? */
236static inline void jffs2_remove_node_refs_from_ino_list(struct jffs2_sb_info *c,
237 struct jffs2_raw_node_ref *ref, struct jffs2_eraseblock *jeb)
238{
239 struct jffs2_inode_cache *ic = NULL;
240 struct jffs2_raw_node_ref **prev;
241
242 prev = &ref->next_in_ino;
243
244 /* Walk the inode's list once, removing any nodes from this eraseblock */
245 while (1) {
246 if (!(*prev)->next_in_ino) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000247 /* We're looking at the jffs2_inode_cache, which is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 at the end of the linked list. Stash it and continue
249 from the beginning of the list */
250 ic = (struct jffs2_inode_cache *)(*prev);
251 prev = &ic->nodes;
252 continue;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Andrew Victor3be36672005-02-09 09:09:05 +0000255 if (SECTOR_ADDR((*prev)->flash_offset) == jeb->offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 /* It's in the block we're erasing */
257 struct jffs2_raw_node_ref *this;
258
259 this = *prev;
260 *prev = this->next_in_ino;
261 this->next_in_ino = NULL;
262
263 if (this == ref)
264 break;
265
266 continue;
267 }
268 /* Not to be deleted. Skip */
269 prev = &((*prev)->next_in_ino);
270 }
271
272 /* PARANOIA */
273 if (!ic) {
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900274 JFFS2_WARNING("inode_cache/xattr_datum/xattr_ref"
275 " not found in remove_node_refs()!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return;
277 }
278
Joe Perches9c261b32012-02-15 15:56:43 -0800279 jffs2_dbg(1, "Removed nodes in range 0x%08x-0x%08x from ino #%u\n",
280 jeb->offset, jeb->offset + c->sector_size, ic->ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 D2({
283 int i=0;
284 struct jffs2_raw_node_ref *this;
Joe Perchesad361c92009-07-06 13:05:40 -0700285 printk(KERN_DEBUG "After remove_node_refs_from_ino_list: \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 this = ic->nodes;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000288
Joe Perchesad361c92009-07-06 13:05:40 -0700289 printk(KERN_DEBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 while(this) {
Joe Perchesda320f02012-02-15 15:56:44 -0800291 pr_cont("0x%08x(%d)->",
Joe Perchesad361c92009-07-06 13:05:40 -0700292 ref_offset(this), ref_flags(this));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 if (++i == 5) {
Joe Perchesad361c92009-07-06 13:05:40 -0700294 printk(KERN_DEBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 i=0;
296 }
297 this = this->next_in_ino;
298 }
Joe Perchesda320f02012-02-15 15:56:44 -0800299 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 });
301
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900302 switch (ic->class) {
303#ifdef CONFIG_JFFS2_FS_XATTR
304 case RAWNODE_CLASS_XATTR_DATUM:
305 jffs2_release_xattr_datum(c, (struct jffs2_xattr_datum *)ic);
306 break;
307 case RAWNODE_CLASS_XATTR_REF:
308 jffs2_release_xattr_ref(c, (struct jffs2_xattr_ref *)ic);
309 break;
310#endif
311 default:
David Woodhouse27c72b02008-05-01 18:47:17 +0100312 if (ic->nodes == (void *)ic && ic->pino_nlink == 0)
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900313 jffs2_del_ino_cache(c, ic);
314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
David Woodhousec38c1b62006-05-25 01:38:27 +0100317void jffs2_free_jeb_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
David Woodhouse9bfeb692006-05-26 21:19:05 +0100319 struct jffs2_raw_node_ref *block, *ref;
Joe Perches9c261b32012-02-15 15:56:43 -0800320 jffs2_dbg(1, "Freeing all node refs for eraseblock offset 0x%08x\n",
321 jeb->offset);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000322
David Woodhouse9bfeb692006-05-26 21:19:05 +0100323 block = ref = jeb->first_node;
324
325 while (ref) {
326 if (ref->flash_offset == REF_LINK_NODE) {
327 ref = ref->next_in_ino;
328 jffs2_free_refblock(block);
329 block = ref;
330 continue;
331 }
332 if (ref->flash_offset != REF_EMPTY_NODE && ref->next_in_ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 jffs2_remove_node_refs_from_ino_list(c, ref, jeb);
334 /* else it was a non-inode node or already removed, so don't bother */
335
David Woodhouse9bfeb692006-05-26 21:19:05 +0100336 ref++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
David Woodhouse9bfeb692006-05-26 21:19:05 +0100338 jeb->first_node = jeb->last_node = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
Thomas Gleixner5d157882005-07-15 08:14:44 +0200341static int jffs2_block_check_erase(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t *bad_offset)
342{
343 void *ebuf;
344 uint32_t ofs;
345 size_t retlen;
Artem Bityutskiybce41d62012-01-10 15:32:29 +0200346 int ret;
Artem Bityutskiy10934472011-12-28 15:55:42 +0200347 unsigned long *wordebuf;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000348
Artem Bityutskiy10934472011-12-28 15:55:42 +0200349 ret = mtd_point(c->mtd, jeb->offset, c->sector_size, &retlen,
350 &ebuf, NULL);
351 if (ret != -EOPNOTSUPP) {
Joakim Tjernlundfab2c392007-06-01 15:14:09 +0200352 if (ret) {
Joe Perches9c261b32012-02-15 15:56:43 -0800353 jffs2_dbg(1, "MTD point failed %d\n", ret);
Joakim Tjernlundfab2c392007-06-01 15:14:09 +0200354 goto do_flash_read;
355 }
356 if (retlen < c->sector_size) {
357 /* Don't muck about if it won't let us point to the whole erase sector */
Joe Perches9c261b32012-02-15 15:56:43 -0800358 jffs2_dbg(1, "MTD point returned len too short: 0x%zx\n",
359 retlen);
Artem Bityutskiy72197782011-12-23 17:05:52 +0200360 mtd_unpoint(c->mtd, jeb->offset, retlen);
Joakim Tjernlundfab2c392007-06-01 15:14:09 +0200361 goto do_flash_read;
362 }
363 wordebuf = ebuf-sizeof(*wordebuf);
364 retlen /= sizeof(*wordebuf);
365 do {
366 if (*++wordebuf != ~0)
367 break;
368 } while(--retlen);
Artem Bityutskiy72197782011-12-23 17:05:52 +0200369 mtd_unpoint(c->mtd, jeb->offset, c->sector_size);
Anders Grafström8a0f5722008-03-12 20:29:23 +0100370 if (retlen) {
Joe Perchesda320f02012-02-15 15:56:44 -0800371 pr_warn("Newly-erased block contained word 0x%lx at offset 0x%08tx\n",
372 *wordebuf,
373 jeb->offset +
374 c->sector_size-retlen * sizeof(*wordebuf));
Anders Grafström8a0f5722008-03-12 20:29:23 +0100375 return -EIO;
376 }
Joakim Tjernlundfab2c392007-06-01 15:14:09 +0200377 return 0;
378 }
379 do_flash_read:
Thomas Gleixner5d157882005-07-15 08:14:44 +0200380 ebuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
381 if (!ebuf) {
Joe Perchesda320f02012-02-15 15:56:44 -0800382 pr_warn("Failed to allocate page buffer for verifying erase at 0x%08x. Refiling\n",
383 jeb->offset);
Thomas Gleixner5d157882005-07-15 08:14:44 +0200384 return -EAGAIN;
385 }
386
Joe Perches9c261b32012-02-15 15:56:43 -0800387 jffs2_dbg(1, "Verifying erase at 0x%08x\n", jeb->offset);
Thomas Gleixner5d157882005-07-15 08:14:44 +0200388
389 for (ofs = jeb->offset; ofs < jeb->offset + c->sector_size; ) {
390 uint32_t readlen = min((uint32_t)PAGE_SIZE, jeb->offset + c->sector_size - ofs);
391 int i;
392
393 *bad_offset = ofs;
394
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200395 ret = mtd_read(c->mtd, ofs, readlen, &retlen, ebuf);
Thomas Gleixner5d157882005-07-15 08:14:44 +0200396 if (ret) {
Joe Perchesda320f02012-02-15 15:56:44 -0800397 pr_warn("Read of newly-erased block at 0x%08x failed: %d. Putting on bad_list\n",
398 ofs, ret);
Anders Grafström8a0f5722008-03-12 20:29:23 +0100399 ret = -EIO;
Thomas Gleixner5d157882005-07-15 08:14:44 +0200400 goto fail;
401 }
402 if (retlen != readlen) {
Joe Perchesda320f02012-02-15 15:56:44 -0800403 pr_warn("Short read from newly-erased block at 0x%08x. Wanted %d, got %zd\n",
404 ofs, readlen, retlen);
Anders Grafström8a0f5722008-03-12 20:29:23 +0100405 ret = -EIO;
Thomas Gleixner5d157882005-07-15 08:14:44 +0200406 goto fail;
407 }
408 for (i=0; i<readlen; i += sizeof(unsigned long)) {
409 /* It's OK. We know it's properly aligned */
410 unsigned long *datum = ebuf + i;
411 if (*datum + 1) {
412 *bad_offset += i;
Joe Perchesda320f02012-02-15 15:56:44 -0800413 pr_warn("Newly-erased block contained word 0x%lx at offset 0x%08x\n",
414 *datum, *bad_offset);
Anders Grafström8a0f5722008-03-12 20:29:23 +0100415 ret = -EIO;
Thomas Gleixner5d157882005-07-15 08:14:44 +0200416 goto fail;
417 }
418 }
419 ofs += readlen;
420 cond_resched();
421 }
422 ret = 0;
423fail:
424 kfree(ebuf);
425 return ret;
426}
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
429{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 size_t retlen;
431 int ret;
Andrew Mortonf4e35642007-08-10 14:01:30 -0700432 uint32_t uninitialized_var(bad_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Thomas Gleixner5d157882005-07-15 08:14:44 +0200434 switch (jffs2_block_check_erase(c, jeb, &bad_offset)) {
435 case -EAGAIN: goto refile;
436 case -EIO: goto filebad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000439 /* Write the erase complete marker */
Joe Perches9c261b32012-02-15 15:56:43 -0800440 jffs2_dbg(1, "Writing erased marker to block at 0x%08x\n", jeb->offset);
Thomas Gleixner5d157882005-07-15 08:14:44 +0200441 bad_offset = jeb->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Thomas Gleixner5d157882005-07-15 08:14:44 +0200443 /* Cleanmarker in oob area or no cleanmarker at all ? */
444 if (jffs2_cleanmarker_oob(c) || c->cleanmarker_size == 0) {
445
446 if (jffs2_cleanmarker_oob(c)) {
447 if (jffs2_write_nand_cleanmarker(c, jeb))
448 goto filebad;
449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 } else {
Thomas Gleixner5d157882005-07-15 08:14:44 +0200451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 struct kvec vecs[1];
453 struct jffs2_unknown_node marker = {
454 .magic = cpu_to_je16(JFFS2_MAGIC_BITMASK),
455 .nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER),
456 .totlen = cpu_to_je32(c->cleanmarker_size)
457 };
458
David Woodhouse046b8b92006-05-25 01:50:35 +0100459 jffs2_prealloc_raw_node_refs(c, jeb, 1);
Thomas Gleixner5d157882005-07-15 08:14:44 +0200460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 marker.hdr_crc = cpu_to_je32(crc32(0, &marker, sizeof(struct jffs2_unknown_node)-4));
462
463 vecs[0].iov_base = (unsigned char *) &marker;
464 vecs[0].iov_len = sizeof(marker);
465 ret = jffs2_flash_direct_writev(c, vecs, 1, jeb->offset, &retlen);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000466
Thomas Gleixner5d157882005-07-15 08:14:44 +0200467 if (ret || retlen != sizeof(marker)) {
468 if (ret)
Joe Perchesda320f02012-02-15 15:56:44 -0800469 pr_warn("Write clean marker to block at 0x%08x failed: %d\n",
Thomas Gleixner5d157882005-07-15 08:14:44 +0200470 jeb->offset, ret);
471 else
Joe Perchesda320f02012-02-15 15:56:44 -0800472 pr_warn("Short write to newly-erased block at 0x%08x: Wanted %zd, got %zd\n",
Thomas Gleixner5d157882005-07-15 08:14:44 +0200473 jeb->offset, sizeof(marker), retlen);
474
Thomas Gleixner5d157882005-07-15 08:14:44 +0200475 goto filebad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
David Woodhouse014b1642008-04-22 23:54:38 +0100478 /* Everything else got zeroed before the erase */
479 jeb->free_size = c->sector_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
David Woodhouseced22072008-04-22 15:13:40 +0100481 mutex_lock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 spin_lock(&c->erase_completion_lock);
David Woodhouse014b1642008-04-22 23:54:38 +0100483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 c->erasing_size -= c->sector_size;
David Woodhouse014b1642008-04-22 23:54:38 +0100485 c->free_size += c->sector_size;
486
487 /* Account for cleanmarker now, if it's in-band */
488 if (c->cleanmarker_size && !jffs2_cleanmarker_oob(c))
489 jffs2_link_node_ref(c, jeb, jeb->offset | REF_NORMAL, c->cleanmarker_size, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
David Woodhousee2bc3222008-04-23 14:15:24 +0100491 list_move_tail(&jeb->list, &c->free_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 c->nr_erasing_blocks--;
493 c->nr_free_blocks++;
David Woodhouse85a62db2008-04-23 01:17:51 +0100494
495 jffs2_dbg_acct_sanity_check_nolock(c, jeb);
496 jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +0100499 mutex_unlock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 wake_up(&c->erase_wait);
Thomas Gleixner5d157882005-07-15 08:14:44 +0200501 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Thomas Gleixner5d157882005-07-15 08:14:44 +0200503filebad:
Thomas Gleixner5d157882005-07-15 08:14:44 +0200504 jffs2_erase_failed(c, jeb, bad_offset);
505 return;
506
507refile:
508 /* Stick it back on the list from whence it came and come back later */
David Woodhouseced22072008-04-22 15:13:40 +0100509 mutex_lock(&c->erase_free_sem);
Thomas Gleixner5d157882005-07-15 08:14:44 +0200510 spin_lock(&c->erase_completion_lock);
David Woodhouseae3b6ba2010-05-19 17:05:14 +0100511 jffs2_garbage_collect_trigger(c);
David Woodhousee2bc3222008-04-23 14:15:24 +0100512 list_move(&jeb->list, &c->erase_complete_list);
Thomas Gleixner5d157882005-07-15 08:14:44 +0200513 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +0100514 mutex_unlock(&c->erase_free_sem);
Thomas Gleixner5d157882005-07-15 08:14:44 +0200515 return;
516}