Blame view

fs/jffs2/erase.c 14.5 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
12
13
14
15
16
17
18
19
20
21
22
23
24
25
   */
  
  #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"
  
  struct erase_priv_struct {
  	struct jffs2_eraseblock *jeb;
  	struct jffs2_sb_info *c;
  };
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
26

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
27
28
29
30
31
  #ifndef __ECOS
  static void jffs2_erase_callback(struct erase_info *);
  #endif
  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
32
33
34
35
36
37
38
39
40
41
  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...
42
43
  	       jffs2_erase_succeeded(c, jeb);
  	       return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
44
45
46
47
         }
         bad_offset = jeb->offset;
  #else /* Linux */
  	struct erase_info *instr;
e0c8e42f8   Artem B. Bityutskiy   [JFFS2] Debug cod...
48
49
50
  	D1(printk(KERN_DEBUG "jffs2_erase_block(): erase block %#08x (range %#08x-%#08x)
  ",
  				jeb->offset, jeb->offset, jeb->offset + c->sector_size));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
51
52
53
54
  	instr = kmalloc(sizeof(struct erase_info) + sizeof(struct erase_priv_struct), GFP_KERNEL);
  	if (!instr) {
  		printk(KERN_WARNING "kmalloc for struct erase_info in jffs2_erase_block failed. Refiling block for later
  ");
ced220703   David Woodhouse   [JFFS2] semaphore...
55
  		mutex_lock(&c->erase_free_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
56
  		spin_lock(&c->erase_completion_lock);
f116629d0   Akinobu Mita   [PATCH] fs: use l...
57
  		list_move(&jeb->list, &c->erase_pending_list);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
58
59
60
61
  		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...
62
  		mutex_unlock(&c->erase_free_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
63
64
65
66
67
68
69
70
71
72
  		return;
  	}
  
  	memset(instr, 0, sizeof(*instr));
  
  	instr->mtd = c->mtd;
  	instr->addr = jeb->offset;
  	instr->len = c->sector_size;
  	instr->callback = jffs2_erase_callback;
  	instr->priv = (unsigned long)(&instr[1]);
bb0eb217c   Adrian Hunter   [MTD] Define and ...
73
  	instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
74

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
75
76
  	((struct erase_priv_struct *)instr->priv)->jeb = jeb;
  	((struct erase_priv_struct *)instr->priv)->c = c;
7e1f0dc05   Artem Bityutskiy   mtd: introduce mt...
77
  	ret = mtd_erase(c->mtd, instr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
78
79
80
81
82
83
84
85
86
87
88
  	if (!ret)
  		return;
  
  	bad_offset = instr->fail_addr;
  	kfree(instr);
  #endif /* __ECOS */
  
  	if (ret == -ENOMEM || ret == -EAGAIN) {
  		/* Erase failed immediately. Refile it on the list */
  		D1(printk(KERN_DEBUG "Erase at 0x%08x failed: %d. Refiling on erase_pending_list
  ", jeb->offset, ret));
ced220703   David Woodhouse   [JFFS2] semaphore...
89
  		mutex_lock(&c->erase_free_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
90
  		spin_lock(&c->erase_completion_lock);
f116629d0   Akinobu Mita   [PATCH] fs: use l...
91
  		list_move(&jeb->list, &c->erase_pending_list);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
92
93
94
95
  		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...
96
  		mutex_unlock(&c->erase_free_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
97
98
  		return;
  	}
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
99
  	if (ret == -EROFS)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
100
101
102
103
104
105
106
107
  		printk(KERN_WARNING "Erase at 0x%08x failed immediately: -EROFS. Is the sector locked?
  ", jeb->offset);
  	else
  		printk(KERN_WARNING "Erase at 0x%08x failed immediately: errno %d
  ", jeb->offset, ret);
  
  	jffs2_erase_failed(c, jeb, bad_offset);
  }
9957abea3   Joakim Tjernlund   jffs2: Add 'work_...
108
  int jffs2_erase_pending_blocks(struct jffs2_sb_info *c, int count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
109
110
  {
  	struct jffs2_eraseblock *jeb;
9957abea3   Joakim Tjernlund   jffs2: Add 'work_...
111
  	int work_done = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
112

ced220703   David Woodhouse   [JFFS2] semaphore...
113
  	mutex_lock(&c->erase_free_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
114
115
116
117
118
119
120
121
  
  	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...
122
  			list_move(&jeb->list, &c->erase_checking_list);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
123
  			spin_unlock(&c->erase_completion_lock);
ced220703   David Woodhouse   [JFFS2] semaphore...
124
  			mutex_unlock(&c->erase_free_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
125
  			jffs2_mark_erased_block(c, jeb);
9957abea3   Joakim Tjernlund   jffs2: Add 'work_...
126
  			work_done++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
  			if (!--count) {
  				D1(printk(KERN_DEBUG "Count reached. jffs2_erase_pending_blocks leaving
  "));
  				goto done;
  			}
  
  		} else if (!list_empty(&c->erase_pending_list)) {
  			jeb = list_entry(c->erase_pending_list.next, struct jffs2_eraseblock, list);
  			D1(printk(KERN_DEBUG "Starting erase of pending block 0x%08x
  ", jeb->offset));
  			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...
144
  			jffs2_free_jeb_node_refs(c, jeb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
145
146
  			list_add(&jeb->list, &c->erasing_list);
  			spin_unlock(&c->erase_completion_lock);
ced220703   David Woodhouse   [JFFS2] semaphore...
147
  			mutex_unlock(&c->erase_free_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
148
149
150
151
152
153
154
155
  
  			jffs2_erase_block(c, jeb);
  
  		} else {
  			BUG();
  		}
  
  		/* Be nice */
3866f673e   Wolfram Sang   jffs2: use cond_r...
156
  		cond_resched();
ced220703   David Woodhouse   [JFFS2] semaphore...
157
  		mutex_lock(&c->erase_free_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
158
159
160
161
  		spin_lock(&c->erase_completion_lock);
  	}
  
  	spin_unlock(&c->erase_completion_lock);
ced220703   David Woodhouse   [JFFS2] semaphore...
162
  	mutex_unlock(&c->erase_free_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
163
164
165
   done:
  	D1(printk(KERN_DEBUG "jffs2_erase_pending_blocks completed
  "));
9957abea3   Joakim Tjernlund   jffs2: Add 'work_...
166
  	return work_done;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
167
168
169
170
171
172
  }
  
  static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
  {
  	D1(printk(KERN_DEBUG "Erase completed successfully at 0x%08x
  ", jeb->offset));
ced220703   David Woodhouse   [JFFS2] semaphore...
173
  	mutex_lock(&c->erase_free_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
174
  	spin_lock(&c->erase_completion_lock);
f116629d0   Akinobu Mita   [PATCH] fs: use l...
175
  	list_move_tail(&jeb->list, &c->erase_complete_list);
ae3b6ba06   David Woodhouse   jffs2: Use jffs2_...
176
177
  	/* Wake the GC thread to mark them clean */
  	jffs2_garbage_collect_trigger(c);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
178
  	spin_unlock(&c->erase_completion_lock);
ced220703   David Woodhouse   [JFFS2] semaphore...
179
  	mutex_unlock(&c->erase_free_sem);
0717bf841   David Woodhouse   jffs2: Erase pend...
180
  	wake_up(&c->erase_wait);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
181
182
183
184
185
186
  }
  
  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...
187
  	if (jffs2_cleanmarker_oob(c) && (bad_offset != (uint32_t)MTD_FAIL_ADDR_UNKNOWN)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
188
189
190
191
  		/* 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...
192
  			mutex_lock(&c->erase_free_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
193
  			spin_lock(&c->erase_completion_lock);
f116629d0   Akinobu Mita   [PATCH] fs: use l...
194
  			list_move(&jeb->list, &c->erase_pending_list);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
195
196
197
198
  			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...
199
  			mutex_unlock(&c->erase_free_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
200
201
202
  			return;
  		}
  	}
ced220703   David Woodhouse   [JFFS2] semaphore...
203
  	mutex_lock(&c->erase_free_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
204
205
206
  	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...
207
  	list_move(&jeb->list, &c->bad_list);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
208
209
  	c->nr_erasing_blocks--;
  	spin_unlock(&c->erase_completion_lock);
ced220703   David Woodhouse   [JFFS2] semaphore...
210
  	mutex_unlock(&c->erase_free_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
211
  	wake_up(&c->erase_wait);
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
212
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
213
214
215
216
217
218
219
  
  #ifndef __ECOS
  static void jffs2_erase_callback(struct erase_info *instr)
  {
  	struct erase_priv_struct *priv = (void *)instr->priv;
  
  	if(instr->state != MTD_ERASE_DONE) {
69423d99f   Adrian Hunter   [MTD] update inte...
220
221
222
  		printk(KERN_WARNING "Erase at 0x%08llx finished, but state != MTD_ERASE_DONE. State is 0x%x instead.
  ",
  			(unsigned long long)instr->addr, instr->state);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
223
224
225
  		jffs2_erase_failed(priv->c, priv->jeb, instr->fail_addr);
  	} else {
  		jffs2_erase_succeeded(priv->c, priv->jeb);
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
226
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
  	kfree(instr);
  }
  #endif /* !__ECOS */
  
  /* 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 ...
244
  			/* We're looking at the jffs2_inode_cache, which is
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
245
246
247
248
249
  			   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 ...
250
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
251

3be36675d   Andrew Victor   [JFFS2] Core chan...
252
  		if (SECTOR_ADDR((*prev)->flash_offset) == jeb->offset) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
  			/* 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...
271
272
273
  		JFFS2_WARNING("inode_cache/xattr_datum/xattr_ref"
  			      " not found in remove_node_refs()!!
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
274
275
276
277
278
279
280
281
282
283
  		return;
  	}
  
  	D1(printk(KERN_DEBUG "Removed nodes in range 0x%08x-0x%08x from ino #%u
  ",
  		  jeb->offset, jeb->offset + c->sector_size, ic->ino));
  
  	D2({
  		int i=0;
  		struct jffs2_raw_node_ref *this;
ad361c988   Joe Perches   Remove multiple K...
284
285
  		printk(KERN_DEBUG "After remove_node_refs_from_ino_list: 
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
286
287
  
  		this = ic->nodes;
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
288

ad361c988   Joe Perches   Remove multiple K...
289
  		printk(KERN_DEBUG);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
290
  		while(this) {
ad361c988   Joe Perches   Remove multiple K...
291
292
  			printk(KERN_CONT "0x%08x(%d)->",
  			       ref_offset(this), ref_flags(this));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
293
  			if (++i == 5) {
ad361c988   Joe Perches   Remove multiple K...
294
  				printk(KERN_DEBUG);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
295
296
297
298
  				i=0;
  			}
  			this = this->next_in_ino;
  		}
ad361c988   Joe Perches   Remove multiple K...
299
300
  		printk(KERN_CONT "
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
301
  	});
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
302
303
304
305
306
307
308
309
310
311
  	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...
312
  			if (ic->nodes == (void *)ic && ic->pino_nlink == 0)
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
313
314
  				jffs2_del_ino_cache(c, ic);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
315
  }
c38c1b613   David Woodhouse   [JFFS2] jffs2_fre...
316
  void jffs2_free_jeb_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
317
  {
9bfeb691e   David Woodhouse   [JFFS2] Switch to...
318
  	struct jffs2_raw_node_ref *block, *ref;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
319
320
  	D1(printk(KERN_DEBUG "Freeing all node refs for eraseblock offset 0x%08x
  ", jeb->offset));
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
321

9bfeb691e   David Woodhouse   [JFFS2] Switch to...
322
323
324
325
326
327
328
329
330
331
  	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
332
333
  			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...
334
  		ref++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
335
  	}
9bfeb691e   David Woodhouse   [JFFS2] Switch to...
336
  	jeb->first_node = jeb->last_node = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
337
  }
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
338
  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
339
  {
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
340
341
  	void *ebuf;
  	uint32_t ofs;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
342
  	size_t retlen;
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
343
  	int ret = -EIO;
10934478e   Artem Bityutskiy   mtd: do use mtd->...
344
  	unsigned long *wordebuf;
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
345

10934478e   Artem Bityutskiy   mtd: do use mtd->...
346
347
348
  	ret = mtd_point(c->mtd, jeb->offset, c->sector_size, &retlen,
  			&ebuf, NULL);
  	if (ret != -EOPNOTSUPP) {
fab2c3991   Joakim Tjernlund   [JFFS2] Use point...
349
350
351
352
353
354
355
356
357
  		if (ret) {
  			D1(printk(KERN_DEBUG "MTD point failed %d
  ", ret));
  			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 */
  			D1(printk(KERN_DEBUG "MTD point returned len too short: 0x%zx
  ", retlen));
7219778ad   Artem Bityutskiy   mtd: introduce mt...
358
  			mtd_unpoint(c->mtd, jeb->offset, retlen);
fab2c3991   Joakim Tjernlund   [JFFS2] Use point...
359
360
361
362
363
364
365
366
  			goto do_flash_read;
  		}
  		wordebuf = ebuf-sizeof(*wordebuf);
  		retlen /= sizeof(*wordebuf);
  		do {
  		   if (*++wordebuf != ~0)
  			   break;
  		} while(--retlen);
7219778ad   Artem Bityutskiy   mtd: introduce mt...
367
  		mtd_unpoint(c->mtd, jeb->offset, c->sector_size);
8a0f57239   Anders Grafström   [JFFS2] Return va...
368
  		if (retlen) {
f4e35647f   Andrew Morton   [JFFS2] fix print...
369
370
  			printk(KERN_WARNING "Newly-erased block contained word 0x%lx at offset 0x%08tx
  ",
fab2c3991   Joakim Tjernlund   [JFFS2] Use point...
371
  			       *wordebuf, jeb->offset + c->sector_size-retlen*sizeof(*wordebuf));
8a0f57239   Anders Grafström   [JFFS2] Return va...
372
373
  			return -EIO;
  		}
fab2c3991   Joakim Tjernlund   [JFFS2] Use point...
374
375
376
  		return 0;
  	}
   do_flash_read:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
377
378
  	ebuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  	if (!ebuf) {
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
379
380
381
382
  		printk(KERN_WARNING "Failed to allocate page buffer for verifying erase at 0x%08x. Refiling
  ", jeb->offset);
  		return -EAGAIN;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
383

5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
384
385
  	D1(printk(KERN_DEBUG "Verifying erase at 0x%08x
  ", jeb->offset));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
386

5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
387
388
389
  	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
390

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

329ad399a   Artem Bityutskiy   mtd: introduce mt...
393
  		ret = mtd_read(c->mtd, ofs, readlen, &retlen, ebuf);
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
394
395
396
  		if (ret) {
  			printk(KERN_WARNING "Read of newly-erased block at 0x%08x failed: %d. Putting on bad_list
  ", ofs, ret);
8a0f57239   Anders Grafström   [JFFS2] Return va...
397
  			ret = -EIO;
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
398
399
400
401
402
  			goto fail;
  		}
  		if (retlen != readlen) {
  			printk(KERN_WARNING "Short read from newly-erased block at 0x%08x. Wanted %d, got %zd
  ", ofs, readlen, retlen);
8a0f57239   Anders Grafström   [JFFS2] Return va...
403
  			ret = -EIO;
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
404
405
406
407
408
409
410
411
412
  			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;
  				printk(KERN_WARNING "Newly-erased block contained word 0x%lx at offset 0x%08x
  ", *datum, *bad_offset);
8a0f57239   Anders Grafström   [JFFS2] Return va...
413
  				ret = -EIO;
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
414
  				goto fail;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
415
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
416
  		}
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
417
418
  		ofs += readlen;
  		cond_resched();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
419
  	}
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
420
421
422
423
424
  	ret = 0;
  fail:
  	kfree(ebuf);
  	return ret;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
425

5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
426
427
  static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
  {
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
428
429
  	size_t retlen;
  	int ret;
f4e35647f   Andrew Morton   [JFFS2] fix print...
430
  	uint32_t uninitialized_var(bad_offset);
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
431
432
433
434
435
  
  	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
436

182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
437
  	/* Write the erase complete marker */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
438
439
  	D1(printk(KERN_DEBUG "Writing erased marker to block at 0x%08x
  ", jeb->offset));
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
440
  	bad_offset = jeb->offset;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
441

5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
442
443
  	/* 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
444

5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
445
446
447
448
  		if (jffs2_cleanmarker_oob(c)) {
  			if (jffs2_write_nand_cleanmarker(c, jeb))
  				goto filebad;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
449
  	} else {
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
450

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
451
452
453
454
455
456
  		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'...
457
  		jffs2_prealloc_raw_node_refs(c, jeb, 1);
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
458

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
459
460
461
462
463
  		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 ...
464

5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
465
466
467
468
469
470
471
472
473
  		if (ret || retlen != sizeof(marker)) {
  			if (ret)
  				printk(KERN_WARNING "Write clean marker to block at 0x%08x failed: %d
  ",
  				       jeb->offset, ret);
  			else
  				printk(KERN_WARNING "Short write to newly-erased block at 0x%08x: Wanted %zd, got %zd
  ",
  				       jeb->offset, sizeof(marker), retlen);
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
474
  			goto filebad;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
475
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
476
  	}
014b164e1   David Woodhouse   [JFFS2] Fix free ...
477
478
  	/* Everything else got zeroed before the erase */
  	jeb->free_size = c->sector_size;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
479

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

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
483
  	c->erasing_size -= c->sector_size;
014b164e1   David Woodhouse   [JFFS2] Fix free ...
484
485
486
487
488
  	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
489

e2bc322bf   David Woodhouse   [JFFS2] Add erase...
490
  	list_move_tail(&jeb->list, &c->free_list);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
491
492
  	c->nr_erasing_blocks--;
  	c->nr_free_blocks++;
85a62db62   David Woodhouse   [JFFS2] Add paran...
493
494
495
  
  	jffs2_dbg_acct_sanity_check_nolock(c, jeb);
  	jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
496
  	spin_unlock(&c->erase_completion_lock);
ced220703   David Woodhouse   [JFFS2] semaphore...
497
  	mutex_unlock(&c->erase_free_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
498
  	wake_up(&c->erase_wait);
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
499
500
501
  	return;
  
  filebad:
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
502
503
  	jffs2_erase_failed(c, jeb, bad_offset);
  	return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
504

5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
505
506
  refile:
  	/* Stick it back on the list from whence it came and come back later */
ced220703   David Woodhouse   [JFFS2] semaphore...
507
  	mutex_lock(&c->erase_free_sem);
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
508
  	spin_lock(&c->erase_completion_lock);
ae3b6ba06   David Woodhouse   jffs2: Use jffs2_...
509
  	jffs2_garbage_collect_trigger(c);
e2bc322bf   David Woodhouse   [JFFS2] Add erase...
510
  	list_move(&jeb->list, &c->erase_complete_list);
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
511
  	spin_unlock(&c->erase_completion_lock);
ced220703   David Woodhouse   [JFFS2] semaphore...
512
  	mutex_unlock(&c->erase_free_sem);
5d157885f   Thomas Gleixner   [JFFS2] Fix node ...
513
514
  	return;
  }