1 /*
2 * JFFS2 -- Journalling Flash File System, Version 2.
3 *
4 * Copyright (C) 2001-2003 Red Hat, Inc.
5 *
6 * Created by David Woodhouse <dwmw2@infradead.org>
7 *
8 * For licensing information, see the file 'LICENCE' in this directory.
9 *
10 * $Id: erase.c,v 1.66 2004/11/16 20:36:11 dwmw2 Exp $
11 *
12 */
13
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/mtd/mtd.h>
17 #include <linux/compiler.h>
18 #include <linux/crc32.h>
19 #include <linux/sched.h>
20 #include <linux/pagemap.h>
21 #include "nodelist.h"
22
23 struct erase_priv_struct {
24 struct jffs2_eraseblock *jeb;
25 struct jffs2_sb_info *c;
26 };
27
28 #ifndef __ECOS
29 static void jffs2_erase_callback(struct erase_info *);
30 #endif
31 static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset);
32 static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
33 static void jffs2_free_all_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
34 static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
35
36 void jffs2_erase_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
37 {
38 int ret;
39 uint32_t bad_offset;
40 #ifdef __ECOS
41 ret = jffs2_flash_erase(c, jeb);
42 if (!ret) {
43 jffs2_erase_succeeded(c, jeb);
44 return;
45 }
46 bad_offset = jeb->offset;
47 #else /* Linux */
48 struct erase_info *instr;
49
50 instr = kmalloc(sizeof(struct erase_info) + sizeof(struct erase_priv_struct), GFP_KERNEL);
51 if (!instr) {
52 printk(KERN_WARNING "kmalloc for struct erase_info in jffs2_erase_block failed. Refiling block for later\n");
53 spin_lock(&c->erase_completion_lock);
54 list_del(&jeb->list);
55 list_add(&jeb->list, &c->erase_pending_list);
56 c->erasing_size -= c->sector_size;
57 c->dirty_size += c->sector_size;
58 jeb->dirty_size = c->sector_size;
59 spin_unlock(&c->erase_completion_lock);
60 return;
61 }
62
63 memset(instr, 0, sizeof(*instr));
64
65 instr->mtd = c->mtd;
66 instr->addr = jeb->offset;
67 instr->len = c->sector_size;
68 instr->callback = jffs2_erase_callback;
69 instr->priv = (unsigned long)(&instr[1]);
70 instr->fail_addr = 0xffffffff;
71
72 ((struct erase_priv_struct *)instr->priv)->jeb = jeb;
73 ((struct erase_priv_struct *)instr->priv)->c = c;
74
75 ret = c->mtd->erase(c->mtd, instr);
76 if (!ret)
77 return;
78
79 bad_offset = instr->fail_addr;
80 kfree(instr);
81 #endif /* __ECOS */
82
83 if (ret == -ENOMEM || ret == -EAGAIN) {
84 /* Erase failed immediately. Refile it on the list */
85 D1(printk(KERN_DEBUG "Erase at 0x%08x failed: %d. Refiling on erase_pending_list\n", jeb->offset, ret));
86 spin_lock(&c->erase_completion_lock);
87 list_del(&jeb->list);
88 list_add(&jeb->list, &c->erase_pending_list);
89 c->erasing_size -= c->sector_size;
90 c->dirty_size += c->sector_size;
91 jeb->dirty_size = c->sector_size;
92 spin_unlock(&c->erase_completion_lock);
93 return;
94 }
95
96 if (ret == -EROFS)
97 printk(KERN_WARNING "Erase at 0x%08x failed immediately: -EROFS. Is the sector locked?\n", jeb->offset);
98 else
99 printk(KERN_WARNING "Erase at 0x%08x failed immediately: errno %d\n", jeb->offset, ret);
100
101 jffs2_erase_failed(c, jeb, bad_offset);
102 }
103
104 void jffs2_erase_pending_blocks(struct jffs2_sb_info *c, int count)
105 {
106 struct jffs2_eraseblock *jeb;
107
108 down(&c->erase_free_sem);
109
110 spin_lock(&c->erase_completion_lock);
111
112 while (!list_empty(&c->erase_complete_list) ||
113 !list_empty(&c->erase_pending_list)) {
114
115 if (!list_empty(&c->erase_complete_list)) {
116 jeb = list_entry(c->erase_complete_list.next, struct jffs2_eraseblock, list);
117 list_del(&jeb->list);
118 spin_unlock(&c->erase_completion_lock);
119 jffs2_mark_erased_block(c, jeb);
120
121 if (!--count) {
122 D1(printk(KERN_DEBUG "Count reached. jffs2_erase_pending_blocks leaving\n"));
123 goto done;
124 }
125
126 } else if (!list_empty(&c->erase_pending_list)) {
127 jeb = list_entry(c->erase_pending_list.next, struct jffs2_eraseblock, list);
128 D1(printk(KERN_DEBUG "Starting erase of pending block 0x%08x\n", jeb->offset));
129 list_del(&jeb->list);
130 c->erasing_size += c->sector_size;
131 c->wasted_size -= jeb->wasted_size;
132 c->free_size -= jeb->free_size;
133 c->used_size -= jeb->used_size;
134 c->dirty_size -= jeb->dirty_size;
135 jeb->wasted_size = jeb->used_size = jeb->dirty_size = jeb->free_size = 0;
136 jffs2_free_all_node_refs(c, jeb);
137 list_add(&jeb->list, &c->erasing_list);
138 spin_unlock(&c->erase_completion_lock);
139
140 jffs2_erase_block(c, jeb);
141
142 } else {
143 BUG();
144 }
145
146 /* Be nice */
147 cond_resched();
148 spin_lock(&c->erase_completion_lock);
149 }
150
151 spin_unlock(&c->erase_completion_lock);
152 done:
153 D1(printk(KERN_DEBUG "jffs2_erase_pending_blocks completed\n"));
154
155 up(&c->erase_free_sem);
156 }
157
158 static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
159 {
160 D1(printk(KERN_DEBUG "Erase completed successfully at 0x%08x\n", jeb->offset));
161 spin_lock(&c->erase_completion_lock);
162 list_del(&jeb->list);
163 list_add_tail(&jeb->list, &c->erase_complete_list);
164 spin_unlock(&c->erase_completion_lock);
165 /* Ensure that kupdated calls us again to mark them clean */
166 jffs2_erase_pending_trigger(c);
167 }
168
169 static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset)
170 {
171 /* For NAND, if the failure did not occur at the device level for a
172 specific physical page, don't bother updating the bad block table. */
173 if (jffs2_cleanmarker_oob(c) && (bad_offset != 0xffffffff)) {
174 /* We had a device-level failure to erase. Let's see if we've
175 failed too many times. */
176 if (!jffs2_write_nand_badblock(c, jeb, bad_offset)) {
177 /* We'd like to give this block another try. */
178 spin_lock(&c->erase_completion_lock);
179 list_del(&jeb->list);
180 list_add(&jeb->list, &c->erase_pending_list);
181 c->erasing_size -= c->sector_size;
182 c->dirty_size += c->sector_size;
183 jeb->dirty_size = c->sector_size;
184 spin_unlock(&c->erase_completion_lock);
185 return;
186 }
187 }
188
189 spin_lock(&c->erase_completion_lock);
190 c->erasing_size -= c->sector_size;
191 c->bad_size += c->sector_size;
192 list_del(&jeb->list);
193 list_add(&jeb->list, &c->bad_list);
194 c->nr_erasing_blocks--;
195 spin_unlock(&c->erase_completion_lock);
196 wake_up(&c->erase_wait);
197 }
198
199 #ifndef __ECOS
200 static void jffs2_erase_callback(struct erase_info *instr)
201 {
202 struct erase_priv_struct *priv = (void *)instr->priv;
203
204 if(instr->state != MTD_ERASE_DONE) {
205 printk(KERN_WARNING "Erase at 0x%08x finished, but state != MTD_ERASE_DONE. State is 0x%x instead.\n", instr->addr, instr->state);
206 jffs2_erase_failed(priv->c, priv->jeb, instr->fail_addr);
207 } else {
208 jffs2_erase_succeeded(priv->c, priv->jeb);
209 }
210 kfree(instr);
211 }
212 #endif /* !__ECOS */
213
214 /* Hmmm. Maybe we should accept the extra space it takes and make
215 this a standard doubly-linked list? */
216 static inline void jffs2_remove_node_refs_from_ino_list(struct jffs2_sb_info *c,
217 struct jffs2_raw_node_ref *ref, struct jffs2_eraseblock *jeb)
218 {
219 struct jffs2_inode_cache *ic = NULL;
220 struct jffs2_raw_node_ref **prev;
221
222 prev = &ref->next_in_ino;
223
224 /* Walk the inode's list once, removing any nodes from this eraseblock */
225 while (1) {
226 if (!(*prev)->next_in_ino) {
227 /* We're looking at the jffs2_inode_cache, which is
228 at the end of the linked list. Stash it and continue
229 from the beginning of the list */
230 ic = (struct jffs2_inode_cache *)(*prev);
231 prev = &ic->nodes;
232 continue;
233 }
234
235 if (((*prev)->flash_offset & ~(c->sector_size -1)) == jeb->offset) {
236 /* It's in the block we're erasing */
237 struct jffs2_raw_node_ref *this;
238
239 this = *prev;
240 *prev = this->next_in_ino;
241 this->next_in_ino = NULL;
242
243 if (this == ref)
244 break;
245
246 continue;
247 }
248 /* Not to be deleted. Skip */
249 prev = &((*prev)->next_in_ino);
250 }
251
252 /* PARANOIA */
253 if (!ic) {
254 printk(KERN_WARNING "inode_cache not found in remove_node_refs()!!\n");
255 return;
256 }
257
258 D1(printk(KERN_DEBUG "Removed nodes in range 0x%08x-0x%08x from ino #%u\n",
259 jeb->offset, jeb->offset + c->sector_size, ic->ino));
260
261 D2({
262 int i=0;
263 struct jffs2_raw_node_ref *this;
264 printk(KERN_DEBUG "After remove_node_refs_from_ino_list: \n" KERN_DEBUG);
265
266 this = ic->nodes;
267
268 while(this) {
269 printk( "0x%08x(%d)->", ref_offset(this), ref_flags(this));
270 if (++i == 5) {
271 printk("\n" KERN_DEBUG);
272 i=0;
273 }
274 this = this->next_in_ino;
275 }
276 printk("\n");
277 });
278
279 if (ic->nodes == (void *)ic) {
280 D1(printk(KERN_DEBUG "inocache for ino #%u is all gone now. Freeing\n", ic->ino));
281 jffs2_del_ino_cache(c, ic);
282 jffs2_free_inode_cache(ic);
283 }
284 }
285
286 static void jffs2_free_all_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
287 {
288 struct jffs2_raw_node_ref *ref;
289 D1(printk(KERN_DEBUG "Freeing all node refs for eraseblock offset 0x%08x\n", jeb->offset));
290 while(jeb->first_node) {
291 ref = jeb->first_node;
292 jeb->first_node = ref->next_phys;
293
294 /* Remove from the inode-list */
295 if (ref->next_in_ino)
296 jffs2_remove_node_refs_from_ino_list(c, ref, jeb);
297 /* else it was a non-inode node or already removed, so don't bother */
298
299 jffs2_free_raw_node_ref(ref);
300 }
301 jeb->last_node = NULL;
302 }
303
304 static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
305 {
306 struct jffs2_raw_node_ref *marker_ref = NULL;
307 unsigned char *ebuf;
308 size_t retlen;
309 int ret;
310 uint32_t bad_offset;
311
312 if (!jffs2_cleanmarker_oob(c)) {
313 marker_ref = jffs2_alloc_raw_node_ref();
314 if (!marker_ref) {
315 printk(KERN_WARNING "Failed to allocate raw node ref for clean marker\n");
316 /* Stick it back on the list from whence it came and come back later */
317 jffs2_erase_pending_trigger(c);
318 spin_lock(&c->erase_completion_lock);
319 list_add(&jeb->list, &c->erase_complete_list);
320 spin_unlock(&c->erase_completion_lock);
321 return;
322 }
323 }
324 ebuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
325 if (!ebuf) {
326 printk(KERN_WARNING "Failed to allocate page buffer for verifying erase at 0x%08x. Assuming it worked\n", jeb->offset);
327 } else {
328 uint32_t ofs = jeb->offset;
329
330 D1(printk(KERN_DEBUG "Verifying erase at 0x%08x\n", jeb->offset));
331 while(ofs < jeb->offset + c->sector_size) {
332 uint32_t readlen = min((uint32_t)PAGE_SIZE, jeb->offset + c->sector_size - ofs);
333 int i;
334
335 bad_offset = ofs;
336
337 ret = jffs2_flash_read(c, ofs, readlen, &retlen, ebuf);
338 if (ret) {
339 printk(KERN_WARNING "Read of newly-erased block at 0x%08x failed: %d. Putting on bad_list\n", ofs, ret);
340 goto bad;
341 }
342 if (retlen != readlen) {
343 printk(KERN_WARNING "Short read from newly-erased block at 0x%08x. Wanted %d, got %zd\n", ofs, readlen, retlen);
344 goto bad;
345 }
346 for (i=0; i<readlen; i += sizeof(unsigned long)) {
347 /* It's OK. We know it's properly aligned */
348 unsigned long datum = *(unsigned long *)(&ebuf[i]);
349 if (datum + 1) {
350 bad_offset += i;
351 printk(KERN_WARNING "Newly-erased block contained word 0x%lx at offset 0x%08x\n", datum, bad_offset);
352 bad:
353 if (!jffs2_cleanmarker_oob(c))
354 jffs2_free_raw_node_ref(marker_ref);
355 kfree(ebuf);
356 bad2:
357 spin_lock(&c->erase_completion_lock);
358 /* Stick it on a list (any list) so
359 erase_failed can take it right off
360 again. Silly, but shouldn't happen
361 often. */
362 list_add(&jeb->list, &c->erasing_list);
363 spin_unlock(&c->erase_completion_lock);
364 jffs2_erase_failed(c, jeb, bad_offset);
365 return;
366 }
367 }
368 ofs += readlen;
369 cond_resched();
370 }
371 kfree(ebuf);
372 }
373
374 bad_offset = jeb->offset;
375
376 /* Write the erase complete marker */
377 D1(printk(KERN_DEBUG "Writing erased marker to block at 0x%08x\n", jeb->offset));
378 if (jffs2_cleanmarker_oob(c)) {
379
380 if (jffs2_write_nand_cleanmarker(c, jeb))
381 goto bad2;
382
383 jeb->first_node = jeb->last_node = NULL;
384
385 jeb->free_size = c->sector_size;
386 jeb->used_size = 0;
387 jeb->dirty_size = 0;
388 jeb->wasted_size = 0;
389 } else {
390 struct kvec vecs[1];
391 struct jffs2_unknown_node marker = {
392 .magic = cpu_to_je16(JFFS2_MAGIC_BITMASK),
393 .nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER),
394 .totlen = cpu_to_je32(c->cleanmarker_size)
395 };
396
397 marker.hdr_crc = cpu_to_je32(crc32(0, &marker, sizeof(struct jffs2_unknown_node)-4));
398
399 vecs[0].iov_base = (unsigned char *) ▮
400 vecs[0].iov_len = sizeof(marker);
401 ret = jffs2_flash_direct_writev(c, vecs, 1, jeb->offset, &retlen);
402
403 if (ret) {
404 printk(KERN_WARNING "Write clean marker to block at 0x%08x failed: %d\n",
405 jeb->offset, ret);
406 goto bad2;
407 }
408 if (retlen != sizeof(marker)) {
409 printk(KERN_WARNING "Short write to newly-erased block at 0x%08x: Wanted %zd, got %zd\n",
410 jeb->offset, sizeof(marker), retlen);
411 goto bad2;
412 }
413
414 marker_ref->next_in_ino = NULL;
415 marker_ref->next_phys = NULL;
416 marker_ref->flash_offset = jeb->offset | REF_NORMAL;
417 marker_ref->__totlen = c->cleanmarker_size;
418
419 jeb->first_node = jeb->last_node = marker_ref;
420
421 jeb->free_size = c->sector_size - c->cleanmarker_size;
422 jeb->used_size = c->cleanmarker_size;
423 jeb->dirty_size = 0;
424 jeb->wasted_size = 0;
425 }
426
427 spin_lock(&c->erase_completion_lock);
428 c->erasing_size -= c->sector_size;
429 c->free_size += jeb->free_size;
430 c->used_size += jeb->used_size;
431
432 ACCT_SANITY_CHECK(c,jeb);
433 D1(ACCT_PARANOIA_CHECK(jeb));
434
435 list_add_tail(&jeb->list, &c->free_list);
436 c->nr_erasing_blocks--;
437 c->nr_free_blocks++;
438 spin_unlock(&c->erase_completion_lock);
439 wake_up(&c->erase_wait);
440 }
441
442
|
This page was automatically generated by the
LXR engine.
|