Blame view

fs/jffs2/erase.c 13.4 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
  /*
   * JFFS2 -- Journalling Flash File System, Version 2.
   *
c00c310ea   David Woodhouse   [JFFS2] Tidy up l...
4
   * Copyright © 2001-2007 Red Hat, Inc.
6088c0587   David Woodhouse   jffs2: Update cop...
5
   * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
6
7
8
9
10
   *
   * Created by David Woodhouse <dwmw2@infradead.org>
   *
   * For licensing information, see the file 'LICENCE' in this directory.
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
11
   */
5a528957e   Joe Perches   jffs2: Use pr_fmt...
12
  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
13
14
15
16
17
18
19
20
  #include <linux/kernel.h>
  #include <linux/slab.h>
  #include <linux/mtd/mtd.h>
  #include <linux/compiler.h>
  #include <linux/crc32.h>
  #include <linux/sched.h>
  #include <linux/pagemap.h>
  #include "nodelist.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
21
22
  static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset);
  static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
23
24
25
26
27
28
29
30
31
32
  static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
  
  static void jffs2_erase_block(struct jffs2_sb_info *c,
  			      struct jffs2_eraseblock *jeb)
  {
  	int ret;
  	uint32_t bad_offset;
  #ifdef __ECOS
         ret = jffs2_flash_erase(c, jeb);
         if (!ret) {
ef53cb02f   David Woodhouse   [JFFS2] Whitespac...
33
34
  	       jffs2_erase_succeeded(c, jeb);
  	       return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
35
36
37
38
         }
         bad_offset = jeb->offset;
  #else /* Linux */
  	struct erase_info *instr;
9c261b33a   Joe Perches   jffs2: Convert mo...
39
40
41
42
  	jffs2_dbg(1, "%s(): erase block %#08x (range %#08x-%#08x)
  ",
  		  __func__,
  		  jeb->offset, jeb->offset, jeb->offset + c->sector_size);
884cfd902   Boris Brezillon   mtd: Stop assumin...
43
  	instr = kmalloc(sizeof(struct erase_info), GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
44
  	if (!instr) {
da320f055   Joe Perches   jffs2: Convert pr...
45
46
  		pr_warn("kmalloc for struct erase_info in jffs2_erase_block failed. Refiling block for later
  ");
ced220703   David Woodhouse   [JFFS2] semaphore...
47
  		mutex_lock(&c->erase_free_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
48
  		spin_lock(&c->erase_completion_lock);
f116629d0   Akinobu Mita   [PATCH] fs: use l...
49
  		list_move(&jeb->list, &c->erase_pending_list);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
50
51
52
53
  		c->erasing_size -= c->sector_size;
  		c->dirty_size += c->sector_size;
  		jeb->dirty_size = c->sector_size;
  		spin_unlock(&c->erase_completion_lock);
ced220703   David Woodhouse   [JFFS2] semaphore...
54
  		mutex_unlock(&c->erase_free_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
55
56
57
58
  		return;
  	}
  
  	memset(instr, 0, sizeof(*instr));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
59
60
  	instr->addr = jeb->offset;
  	instr->len = c->sector_size;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
61

7e1f0dc05   Artem Bityutskiy   mtd: introduce mt...
62
  	ret = mtd_erase(c->mtd, instr);
884cfd902   Boris Brezillon   mtd: Stop assumin...
63
64
65
  	if (!ret) {
  		jffs2_erase_succeeded(c, jeb);
  		kfree(instr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
66
  		return;
884cfd902   Boris Brezillon   mtd: Stop assumin...
67
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
68
69
70
71
72
73
74
  
  	bad_offset = instr->fail_addr;
  	kfree(instr);
  #endif /* __ECOS */
  
  	if (ret == -ENOMEM || ret == -EAGAIN) {
  		/* Erase failed immediately. Refile it on the list */
9c261b33a   Joe Perches   jffs2: Convert mo...
75
76
77
  		jffs2_dbg(1, "Erase at 0x%08x failed: %d. Refiling on erase_pending_list
  ",
  			  jeb->offset, ret);
ced220703   David Woodhouse   [JFFS2] semaphore...
78
  		mutex_lock(&c->erase_free_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
79
  		spin_lock(&c->erase_completion_lock);
f116629d0   Akinobu Mita   [PATCH] fs: use l...
80
  		list_move(&jeb->list, &c->erase_pending_list);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
81
82
83
84
  		c->erasing_size -= c->sector_size;
  		c->dirty_size += c->sector_size;
  		jeb->dirty_size = c->sector_size;
  		spin_unlock(&c->erase_completion_lock);
ced220703   David Woodhouse   [JFFS2] semaphore...
85
  		mutex_unlock(&c->erase_free_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
86
87
  		return;
  	}
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
88
  	if (ret == -EROFS)
da320f055   Joe Perches   jffs2: Convert pr...
89
90
91
  		pr_warn("Erase at 0x%08x failed immediately: -EROFS. Is the sector locked?
  ",
  			jeb->offset);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
92
  	else
da320f055   Joe Perches   jffs2: Convert pr...
93
94
95
  		pr_warn("Erase at 0x%08x failed immediately: errno %d
  ",
  			jeb->offset, ret);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
96
97
98
  
  	jffs2_erase_failed(c, jeb, bad_offset);
  }
9957abea3   Joakim Tjernlund   jffs2: Add 'work_...
99
  int jffs2_erase_pending_blocks(struct jffs2_sb_info *c, int count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
100
101
  {
  	struct jffs2_eraseblock *jeb;
9957abea3   Joakim Tjernlund   jffs2: Add 'work_...
102
  	int work_done = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
103

ced220703   David Woodhouse   [JFFS2] semaphore...
104
  	mutex_lock(&c->erase_free_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
105
106
107
108
109
110
111
112
  
  	spin_lock(&c->erase_completion_lock);
  
  	while (!list_empty(&c->erase_complete_list) ||
  	       !list_empty(&c->erase_pending_list)) {
  
  		if (!list_empty(&c->erase_complete_list)) {
  			jeb = list_entry(c->erase_complete_list.next, struct jffs2_eraseblock, list);
e2bc322bf   David Woodhouse   [JFFS2] Add erase...
113
  			list_move(&jeb->list, &c->erase_checking_list);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
114
  			spin_unlock(&c->erase_completion_lock);
ced220703   David Woodhouse   [JFFS2] semaphore...
115
  			mutex_unlock(&c->erase_free_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
116
  			jffs2_mark_erased_block(c, jeb);
9957abea3   Joakim Tjernlund   jffs2: Add 'work_...
117
  			work_done++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
118
  			if (!--count) {
9c261b33a   Joe Perches   jffs2: Convert mo...
119
120
  				jffs2_dbg(1, "Count reached. jffs2_erase_pending_blocks leaving
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
121
122
123
124
125
  				goto done;
  			}
  
  		} else if (!list_empty(&c->erase_pending_list)) {
  			jeb = list_entry(c->erase_pending_list.next, struct jffs2_eraseblock, list);
9c261b33a   Joe Perches   jffs2: Convert mo...
126
127
128
  			jffs2_dbg(1, "Starting erase of pending block 0x%08x
  ",
  				  jeb->offset);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
129
130
131
132
133
134
135
  			list_del(&jeb->list);
  			c->erasing_size += c->sector_size;
  			c->wasted_size -= jeb->wasted_size;
  			c->free_size -= jeb->free_size;
  			c->used_size -= jeb->used_size;
  			c->dirty_size -= jeb->dirty_size;
  			jeb->wasted_size = jeb->used_size = jeb->dirty_size = jeb->free_size = 0;
c38c1b613   David Woodhouse   [JFFS2] jffs2_fre...
136
  			jffs2_free_jeb_node_refs(c, jeb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
137
138
  			list_add(&jeb->list, &c->erasing_list);
  			spin_unlock(&c->erase_completion_lock);
ced220703   David Woodhouse   [JFFS2] semaphore...
139
  			mutex_unlock(&c->erase_free_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
140
141
142
143
144
145
146
147
  
  			jffs2_erase_block(c, jeb);
  
  		} else {
  			BUG();
  		}
  
  		/* Be nice */
3866f673e   Wolfram Sang   jffs2: use cond_r...
148
  		cond_resched();
ced220703   David Woodhouse   [JFFS2] semaphore...
149
  		mutex_lock(&c->erase_free_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
150
151
152
153
  		spin_lock(&c->erase_completion_lock);
  	}
  
  	spin_unlock(&c->erase_completion_lock);
ced220703   David Woodhouse   [JFFS2] semaphore...
154
  	mutex_unlock(&c->erase_free_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
155
   done:
9c261b33a   Joe Perches   jffs2: Convert mo...
156
157
  	jffs2_dbg(1, "jffs2_erase_pending_blocks completed
  ");
9957abea3   Joakim Tjernlund   jffs2: Add 'work_...
158
  	return work_done;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
159
160
161
162
  }
  
  static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
  {
9c261b33a   Joe Perches   jffs2: Convert mo...
163
164
  	jffs2_dbg(1, "Erase completed successfully at 0x%08x
  ", jeb->offset);
ced220703   David Woodhouse   [JFFS2] semaphore...
165
  	mutex_lock(&c->erase_free_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
166
  	spin_lock(&c->erase_completion_lock);
f116629d0   Akinobu Mita   [PATCH] fs: use l...
167
  	list_move_tail(&jeb->list, &c->erase_complete_list);
ae3b6ba06   David Woodhouse   jffs2: Use jffs2_...
168
169
  	/* Wake the GC thread to mark them clean */
  	jffs2_garbage_collect_trigger(c);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
170
  	spin_unlock(&c->erase_completion_lock);
ced220703   David Woodhouse   [JFFS2] semaphore...
171
  	mutex_unlock(&c->erase_free_sem);
0717bf841   David Woodhouse   jffs2: Erase pend...
172
  	wake_up(&c->erase_wait);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
173
174
175
176
177
178
  }
  
  static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset)
  {
  	/* For NAND, if the failure did not occur at the device level for a
  	   specific physical page, don't bother updating the bad block table. */
69423d99f   Adrian Hunter   [MTD] update inte...
179
  	if (jffs2_cleanmarker_oob(c) && (bad_offset != (uint32_t)MTD_FAIL_ADDR_UNKNOWN)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
180
181
182
183
  		/* We had a device-level failure to erase.  Let's see if we've
  		   failed too many times. */
  		if (!jffs2_write_nand_badblock(c, jeb, bad_offset)) {
  			/* We'd like to give this block another try. */
ced220703   David Woodhouse   [JFFS2] semaphore...
184
  			mutex_lock(&c->erase_free_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
185
  			spin_lock(&c->erase_completion_lock);
f116629d0   Akinobu Mita   [PATCH] fs: use l...
186
  			list_move(&jeb->list, &c->erase_pending_list);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
187
188
189
190
  			c->erasing_size -= c->sector_size;
  			c->dirty_size += c->sector_size;
  			jeb->dirty_size = c->sector_size;
  			spin_unlock(&c->erase_completion_lock);
ced220703   David Woodhouse   [JFFS2] semaphore...
191
  			mutex_unlock(&c->erase_free_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
192
193
194
  			return;
  		}
  	}
ced220703   David Woodhouse   [JFFS2] semaphore...
195
  	mutex_lock(&c->erase_free_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
196
197
198
  	spin_lock(&c->erase_completion_lock);
  	c->erasing_size -= c->sector_size;
  	c->bad_size += c->sector_size;
f116629d0   Akinobu Mita   [PATCH] fs: use l...
199
  	list_move(&jeb->list, &c->bad_list);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
200
201
  	c->nr_erasing_blocks--;
  	spin_unlock(&c->erase_completion_lock);
ced220703   David Woodhouse   [JFFS2] semaphore...
202
  	mutex_unlock(&c->erase_free_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
203
  	wake_up(&c->erase_wait);
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
204
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
205

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
206
207
208
209
210
211
212
213
214
215
216
217
218
  /* Hmmm. Maybe we should accept the extra space it takes and make
     this a standard doubly-linked list? */
  static inline void jffs2_remove_node_refs_from_ino_list(struct jffs2_sb_info *c,
  			struct jffs2_raw_node_ref *ref, struct jffs2_eraseblock *jeb)
  {
  	struct jffs2_inode_cache *ic = NULL;
  	struct jffs2_raw_node_ref **prev;
  
  	prev = &ref->next_in_ino;
  
  	/* Walk the inode's list once, removing any nodes from this eraseblock */
  	while (1) {
  		if (!(*prev)->next_in_ino) {
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
219
  			/* We're looking at the jffs2_inode_cache, which is
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
220
221
222
223
224
  			   at the end of the linked list. Stash it and continue
  			   from the beginning of the list */
  			ic = (struct jffs2_inode_cache *)(*prev);
  			prev = &ic->nodes;
  			continue;
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
225
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
226

3be36675d   Andrew Victor   [JFFS2] Core chan...
227
  		if (SECTOR_ADDR((*prev)->flash_offset) == jeb->offset) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
  			/* It's in the block we're erasing */
  			struct jffs2_raw_node_ref *this;
  
  			this = *prev;
  			*prev = this->next_in_ino;
  			this->next_in_ino = NULL;
  
  			if (this == ref)
  				break;
  
  			continue;
  		}
  		/* Not to be deleted. Skip */
  		prev = &((*prev)->next_in_ino);
  	}
  
  	/* PARANOIA */
  	if (!ic) {
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
246
247
248
  		JFFS2_WARNING("inode_cache/xattr_datum/xattr_ref"
  			      " not found in remove_node_refs()!!
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
249
250
  		return;
  	}
9c261b33a   Joe Perches   jffs2: Convert mo...
251
252
253
  	jffs2_dbg(1, "Removed nodes in range 0x%08x-0x%08x from ino #%u
  ",
  		  jeb->offset, jeb->offset + c->sector_size, ic->ino);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
254
255
256
257
  
  	D2({
  		int i=0;
  		struct jffs2_raw_node_ref *this;
ad361c988   Joe Perches   Remove multiple K...
258
259
  		printk(KERN_DEBUG "After remove_node_refs_from_ino_list: 
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
260
261
  
  		this = ic->nodes;
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
262

ad361c988   Joe Perches   Remove multiple K...
263
  		printk(KERN_DEBUG);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
264
  		while(this) {
da320f055   Joe Perches   jffs2: Convert pr...
265
  			pr_cont("0x%08x(%d)->",
ad361c988   Joe Perches   Remove multiple K...
266
  			       ref_offset(this), ref_flags(this));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
267
  			if (++i == 5) {
ad361c988   Joe Perches   Remove multiple K...
268
  				printk(KERN_DEBUG);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
269
270
271
272
  				i=0;
  			}
  			this = this->next_in_ino;
  		}
da320f055   Joe Perches   jffs2: Convert pr...
273
274
  		pr_cont("
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
275
  	});
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
276
277
278
279
280
281
282
283
284
285
  	switch (ic->class) {
  #ifdef CONFIG_JFFS2_FS_XATTR
  		case RAWNODE_CLASS_XATTR_DATUM:
  			jffs2_release_xattr_datum(c, (struct jffs2_xattr_datum *)ic);
  			break;
  		case RAWNODE_CLASS_XATTR_REF:
  			jffs2_release_xattr_ref(c, (struct jffs2_xattr_ref *)ic);
  			break;
  #endif
  		default:
27c72b040   David Woodhouse   [JFFS2] Track par...
286
  			if (ic->nodes == (void *)ic && ic->pino_nlink == 0)
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
287
288
  				jffs2_del_ino_cache(c, ic);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
289
  }
c38c1b613   David Woodhouse   [JFFS2] jffs2_fre...
290
  void jffs2_free_jeb_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
291
  {
9bfeb691e   David Woodhouse   [JFFS2] Switch to...
292
  	struct jffs2_raw_node_ref *block, *ref;
9c261b33a   Joe Perches   jffs2: Convert mo...
293
294
295
  	jffs2_dbg(1, "Freeing all node refs for eraseblock offset 0x%08x
  ",
  		  jeb->offset);
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
296

9bfeb691e   David Woodhouse   [JFFS2] Switch to...
297
298
299
300
301
302
303
304
305
306
  	block = ref = jeb->first_node;
  
  	while (ref) {
  		if (ref->flash_offset == REF_LINK_NODE) {
  			ref = ref->next_in_ino;
  			jffs2_free_refblock(block);
  			block = ref;
  			continue;
  		}
  		if (ref->flash_offset != REF_EMPTY_NODE && ref->next_in_ino)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
307
308
  			jffs2_remove_node_refs_from_ino_list(c, ref, jeb);
  		/* else it was a non-inode node or already removed, so don't bother */
9bfeb691e   David Woodhouse   [JFFS2] Switch to...
309
  		ref++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
310
  	}
9bfeb691e   David Woodhouse   [JFFS2] Switch to...
311
  	jeb->first_node = jeb->last_node = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
312
  }
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
313
  static int jffs2_block_check_erase(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t *bad_offset)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
314
  {
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
315
316
  	void *ebuf;
  	uint32_t ofs;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
317
  	size_t retlen;
bce41d601   Artem Bityutskiy   jffs2: do not ini...
318
  	int ret;
10934478e   Artem Bityutskiy   mtd: do use mtd->...
319
  	unsigned long *wordebuf;
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
320

10934478e   Artem Bityutskiy   mtd: do use mtd->...
321
322
323
  	ret = mtd_point(c->mtd, jeb->offset, c->sector_size, &retlen,
  			&ebuf, NULL);
  	if (ret != -EOPNOTSUPP) {
fab2c3991   Joakim Tjernlund   [JFFS2] Use point...
324
  		if (ret) {
9c261b33a   Joe Perches   jffs2: Convert mo...
325
326
  			jffs2_dbg(1, "MTD point failed %d
  ", ret);
fab2c3991   Joakim Tjernlund   [JFFS2] Use point...
327
328
329
330
  			goto do_flash_read;
  		}
  		if (retlen < c->sector_size) {
  			/* Don't muck about if it won't let us point to the whole erase sector */
9c261b33a   Joe Perches   jffs2: Convert mo...
331
332
333
  			jffs2_dbg(1, "MTD point returned len too short: 0x%zx
  ",
  				  retlen);
7219778ad   Artem Bityutskiy   mtd: introduce mt...
334
  			mtd_unpoint(c->mtd, jeb->offset, retlen);
fab2c3991   Joakim Tjernlund   [JFFS2] Use point...
335
336
337
338
339
340
341
342
  			goto do_flash_read;
  		}
  		wordebuf = ebuf-sizeof(*wordebuf);
  		retlen /= sizeof(*wordebuf);
  		do {
  		   if (*++wordebuf != ~0)
  			   break;
  		} while(--retlen);
7219778ad   Artem Bityutskiy   mtd: introduce mt...
343
  		mtd_unpoint(c->mtd, jeb->offset, c->sector_size);
8a0f57239   Anders Grafström   [JFFS2] Return va...
344
  		if (retlen) {
da320f055   Joe Perches   jffs2: Convert pr...
345
346
347
348
349
  			pr_warn("Newly-erased block contained word 0x%lx at offset 0x%08tx
  ",
  				*wordebuf,
  				jeb->offset +
  				c->sector_size-retlen * sizeof(*wordebuf));
8a0f57239   Anders Grafström   [JFFS2] Return va...
350
351
  			return -EIO;
  		}
fab2c3991   Joakim Tjernlund   [JFFS2] Use point...
352
353
354
  		return 0;
  	}
   do_flash_read:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
355
356
  	ebuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  	if (!ebuf) {
da320f055   Joe Perches   jffs2: Convert pr...
357
358
359
  		pr_warn("Failed to allocate page buffer for verifying erase at 0x%08x. Refiling
  ",
  			jeb->offset);
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
360
361
  		return -EAGAIN;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
362

9c261b33a   Joe Perches   jffs2: Convert mo...
363
364
  	jffs2_dbg(1, "Verifying erase at 0x%08x
  ", jeb->offset);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
365

5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
366
367
368
  	for (ofs = jeb->offset; ofs < jeb->offset + c->sector_size; ) {
  		uint32_t readlen = min((uint32_t)PAGE_SIZE, jeb->offset + c->sector_size - ofs);
  		int i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
369

5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
370
  		*bad_offset = ofs;
894214d1a   Artem B. Bityuckiy   [JFFS2] Fix race ...
371

329ad399a   Artem Bityutskiy   mtd: introduce mt...
372
  		ret = mtd_read(c->mtd, ofs, readlen, &retlen, ebuf);
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
373
  		if (ret) {
da320f055   Joe Perches   jffs2: Convert pr...
374
375
376
  			pr_warn("Read of newly-erased block at 0x%08x failed: %d. Putting on bad_list
  ",
  				ofs, ret);
8a0f57239   Anders Grafström   [JFFS2] Return va...
377
  			ret = -EIO;
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
378
379
380
  			goto fail;
  		}
  		if (retlen != readlen) {
da320f055   Joe Perches   jffs2: Convert pr...
381
382
383
  			pr_warn("Short read from newly-erased block at 0x%08x. Wanted %d, got %zd
  ",
  				ofs, readlen, retlen);
8a0f57239   Anders Grafström   [JFFS2] Return va...
384
  			ret = -EIO;
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
385
386
387
388
389
390
391
  			goto fail;
  		}
  		for (i=0; i<readlen; i += sizeof(unsigned long)) {
  			/* It's OK. We know it's properly aligned */
  			unsigned long *datum = ebuf + i;
  			if (*datum + 1) {
  				*bad_offset += i;
da320f055   Joe Perches   jffs2: Convert pr...
392
393
394
  				pr_warn("Newly-erased block contained word 0x%lx at offset 0x%08x
  ",
  					*datum, *bad_offset);
8a0f57239   Anders Grafström   [JFFS2] Return va...
395
  				ret = -EIO;
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
396
  				goto fail;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
397
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
398
  		}
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
399
400
  		ofs += readlen;
  		cond_resched();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
401
  	}
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
402
403
404
405
406
  	ret = 0;
  fail:
  	kfree(ebuf);
  	return ret;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
407

5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
408
409
  static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
  {
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
410
411
  	size_t retlen;
  	int ret;
3f649ab72   Kees Cook   treewide: Remove ...
412
  	uint32_t bad_offset;
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
413
414
415
416
417
  
  	switch (jffs2_block_check_erase(c, jeb, &bad_offset)) {
  	case -EAGAIN:	goto refile;
  	case -EIO:	goto filebad;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
418

182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
419
  	/* Write the erase complete marker */
9c261b33a   Joe Perches   jffs2: Convert mo...
420
421
  	jffs2_dbg(1, "Writing erased marker to block at 0x%08x
  ", jeb->offset);
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
422
  	bad_offset = jeb->offset;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
423

5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
424
425
  	/* Cleanmarker in oob area or no cleanmarker at all ? */
  	if (jffs2_cleanmarker_oob(c) || c->cleanmarker_size == 0) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
426

5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
427
428
429
430
  		if (jffs2_cleanmarker_oob(c)) {
  			if (jffs2_write_nand_cleanmarker(c, jeb))
  				goto filebad;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
431
  	} else {
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
432

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
433
434
435
436
437
438
  		struct kvec vecs[1];
  		struct jffs2_unknown_node marker = {
  			.magic =	cpu_to_je16(JFFS2_MAGIC_BITMASK),
  			.nodetype =	cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER),
  			.totlen =	cpu_to_je32(c->cleanmarker_size)
  		};
046b8b980   David Woodhouse   [JFFS2] Add 'jeb'...
439
  		jffs2_prealloc_raw_node_refs(c, jeb, 1);
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
440

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
441
442
443
444
445
  		marker.hdr_crc = cpu_to_je32(crc32(0, &marker, sizeof(struct jffs2_unknown_node)-4));
  
  		vecs[0].iov_base = (unsigned char *) &marker;
  		vecs[0].iov_len = sizeof(marker);
  		ret = jffs2_flash_direct_writev(c, vecs, 1, jeb->offset, &retlen);
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
446

5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
447
448
  		if (ret || retlen != sizeof(marker)) {
  			if (ret)
da320f055   Joe Perches   jffs2: Convert pr...
449
450
  				pr_warn("Write clean marker to block at 0x%08x failed: %d
  ",
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
451
452
  				       jeb->offset, ret);
  			else
da320f055   Joe Perches   jffs2: Convert pr...
453
454
  				pr_warn("Short write to newly-erased block at 0x%08x: Wanted %zd, got %zd
  ",
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
455
  				       jeb->offset, sizeof(marker), retlen);
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
456
  			goto filebad;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
457
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
458
  	}
014b164e1   David Woodhouse   [JFFS2] Fix free ...
459
460
  	/* Everything else got zeroed before the erase */
  	jeb->free_size = c->sector_size;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
461

ced220703   David Woodhouse   [JFFS2] semaphore...
462
  	mutex_lock(&c->erase_free_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
463
  	spin_lock(&c->erase_completion_lock);
014b164e1   David Woodhouse   [JFFS2] Fix free ...
464

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
465
  	c->erasing_size -= c->sector_size;
014b164e1   David Woodhouse   [JFFS2] Fix free ...
466
467
468
469
470
  	c->free_size += c->sector_size;
  
  	/* Account for cleanmarker now, if it's in-band */
  	if (c->cleanmarker_size && !jffs2_cleanmarker_oob(c))
  		jffs2_link_node_ref(c, jeb, jeb->offset | REF_NORMAL, c->cleanmarker_size, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
471

e2bc322bf   David Woodhouse   [JFFS2] Add erase...
472
  	list_move_tail(&jeb->list, &c->free_list);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
473
474
  	c->nr_erasing_blocks--;
  	c->nr_free_blocks++;
85a62db62   David Woodhouse   [JFFS2] Add paran...
475
476
477
  
  	jffs2_dbg_acct_sanity_check_nolock(c, jeb);
  	jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
478
  	spin_unlock(&c->erase_completion_lock);
ced220703   David Woodhouse   [JFFS2] semaphore...
479
  	mutex_unlock(&c->erase_free_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
480
  	wake_up(&c->erase_wait);
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
481
482
483
  	return;
  
  filebad:
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
484
485
  	jffs2_erase_failed(c, jeb, bad_offset);
  	return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
486

5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
487
488
  refile:
  	/* Stick it back on the list from whence it came and come back later */
ced220703   David Woodhouse   [JFFS2] semaphore...
489
  	mutex_lock(&c->erase_free_sem);
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
490
  	spin_lock(&c->erase_completion_lock);
ae3b6ba06   David Woodhouse   jffs2: Use jffs2_...
491
  	jffs2_garbage_collect_trigger(c);
e2bc322bf   David Woodhouse   [JFFS2] Add erase...
492
  	list_move(&jeb->list, &c->erase_complete_list);
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
493
  	spin_unlock(&c->erase_completion_lock);
ced220703   David Woodhouse   [JFFS2] semaphore...
494
  	mutex_unlock(&c->erase_free_sem);
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
495
496
  	return;
  }