2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright (C) 2001-2003 Red Hat, Inc.
6 * Created by David Woodhouse <dwmw2@infradead.org>
8 * For licensing information, see the file 'LICENCE' in this directory.
10 * $Id: erase.c,v 1.80 2005/07/14 19:46:24 joern Exp $
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>
23 struct erase_priv_struct
{
24 struct jffs2_eraseblock
*jeb
;
25 struct jffs2_sb_info
*c
;
29 static void jffs2_erase_callback(struct erase_info
*);
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
);
36 static void jffs2_erase_block(struct jffs2_sb_info
*c
,
37 struct jffs2_eraseblock
*jeb
)
42 ret
= jffs2_flash_erase(c
, jeb
);
44 jffs2_erase_succeeded(c
, jeb
);
47 bad_offset
= jeb
->offset
;
49 struct erase_info
*instr
;
51 D1(printk(KERN_DEBUG
"jffs2_erase_block(): erase block %#x (range %#x-%#x)\n", jeb
->offset
, jeb
->offset
, jeb
->offset
+ c
->sector_size
));
52 instr
= kmalloc(sizeof(struct erase_info
) + sizeof(struct erase_priv_struct
), GFP_KERNEL
);
54 printk(KERN_WARNING
"kmalloc for struct erase_info in jffs2_erase_block failed. Refiling block for later\n");
55 spin_lock(&c
->erase_completion_lock
);
57 list_add(&jeb
->list
, &c
->erase_pending_list
);
58 c
->erasing_size
-= c
->sector_size
;
59 c
->dirty_size
+= c
->sector_size
;
60 jeb
->dirty_size
= c
->sector_size
;
61 spin_unlock(&c
->erase_completion_lock
);
65 memset(instr
, 0, sizeof(*instr
));
68 instr
->addr
= jeb
->offset
;
69 instr
->len
= c
->sector_size
;
70 instr
->callback
= jffs2_erase_callback
;
71 instr
->priv
= (unsigned long)(&instr
[1]);
72 instr
->fail_addr
= 0xffffffff;
74 ((struct erase_priv_struct
*)instr
->priv
)->jeb
= jeb
;
75 ((struct erase_priv_struct
*)instr
->priv
)->c
= c
;
77 ret
= c
->mtd
->erase(c
->mtd
, instr
);
81 bad_offset
= instr
->fail_addr
;
85 if (ret
== -ENOMEM
|| ret
== -EAGAIN
) {
86 /* Erase failed immediately. Refile it on the list */
87 D1(printk(KERN_DEBUG
"Erase at 0x%08x failed: %d. Refiling on erase_pending_list\n", jeb
->offset
, ret
));
88 spin_lock(&c
->erase_completion_lock
);
90 list_add(&jeb
->list
, &c
->erase_pending_list
);
91 c
->erasing_size
-= c
->sector_size
;
92 c
->dirty_size
+= c
->sector_size
;
93 jeb
->dirty_size
= c
->sector_size
;
94 spin_unlock(&c
->erase_completion_lock
);
99 printk(KERN_WARNING
"Erase at 0x%08x failed immediately: -EROFS. Is the sector locked?\n", jeb
->offset
);
101 printk(KERN_WARNING
"Erase at 0x%08x failed immediately: errno %d\n", jeb
->offset
, ret
);
103 jffs2_erase_failed(c
, jeb
, bad_offset
);
106 void jffs2_erase_pending_blocks(struct jffs2_sb_info
*c
, int count
)
108 struct jffs2_eraseblock
*jeb
;
110 down(&c
->erase_free_sem
);
112 spin_lock(&c
->erase_completion_lock
);
114 while (!list_empty(&c
->erase_complete_list
) ||
115 !list_empty(&c
->erase_pending_list
)) {
117 if (!list_empty(&c
->erase_complete_list
)) {
118 jeb
= list_entry(c
->erase_complete_list
.next
, struct jffs2_eraseblock
, list
);
119 list_del(&jeb
->list
);
120 spin_unlock(&c
->erase_completion_lock
);
121 jffs2_mark_erased_block(c
, jeb
);
124 D1(printk(KERN_DEBUG
"Count reached. jffs2_erase_pending_blocks leaving\n"));
128 } else if (!list_empty(&c
->erase_pending_list
)) {
129 jeb
= list_entry(c
->erase_pending_list
.next
, struct jffs2_eraseblock
, list
);
130 D1(printk(KERN_DEBUG
"Starting erase of pending block 0x%08x\n", jeb
->offset
));
131 list_del(&jeb
->list
);
132 c
->erasing_size
+= c
->sector_size
;
133 c
->wasted_size
-= jeb
->wasted_size
;
134 c
->free_size
-= jeb
->free_size
;
135 c
->used_size
-= jeb
->used_size
;
136 c
->dirty_size
-= jeb
->dirty_size
;
137 jeb
->wasted_size
= jeb
->used_size
= jeb
->dirty_size
= jeb
->free_size
= 0;
138 jffs2_free_all_node_refs(c
, jeb
);
139 list_add(&jeb
->list
, &c
->erasing_list
);
140 spin_unlock(&c
->erase_completion_lock
);
142 jffs2_erase_block(c
, jeb
);
150 spin_lock(&c
->erase_completion_lock
);
153 spin_unlock(&c
->erase_completion_lock
);
155 D1(printk(KERN_DEBUG
"jffs2_erase_pending_blocks completed\n"));
157 up(&c
->erase_free_sem
);
160 static void jffs2_erase_succeeded(struct jffs2_sb_info
*c
, struct jffs2_eraseblock
*jeb
)
162 D1(printk(KERN_DEBUG
"Erase completed successfully at 0x%08x\n", jeb
->offset
));
163 spin_lock(&c
->erase_completion_lock
);
164 list_del(&jeb
->list
);
165 list_add_tail(&jeb
->list
, &c
->erase_complete_list
);
166 spin_unlock(&c
->erase_completion_lock
);
167 /* Ensure that kupdated calls us again to mark them clean */
168 jffs2_erase_pending_trigger(c
);
171 static void jffs2_erase_failed(struct jffs2_sb_info
*c
, struct jffs2_eraseblock
*jeb
, uint32_t bad_offset
)
173 /* For NAND, if the failure did not occur at the device level for a
174 specific physical page, don't bother updating the bad block table. */
175 if (jffs2_cleanmarker_oob(c
) && (bad_offset
!= 0xffffffff)) {
176 /* We had a device-level failure to erase. Let's see if we've
177 failed too many times. */
178 if (!jffs2_write_nand_badblock(c
, jeb
, bad_offset
)) {
179 /* We'd like to give this block another try. */
180 spin_lock(&c
->erase_completion_lock
);
181 list_del(&jeb
->list
);
182 list_add(&jeb
->list
, &c
->erase_pending_list
);
183 c
->erasing_size
-= c
->sector_size
;
184 c
->dirty_size
+= c
->sector_size
;
185 jeb
->dirty_size
= c
->sector_size
;
186 spin_unlock(&c
->erase_completion_lock
);
191 spin_lock(&c
->erase_completion_lock
);
192 c
->erasing_size
-= c
->sector_size
;
193 c
->bad_size
+= c
->sector_size
;
194 list_del(&jeb
->list
);
195 list_add(&jeb
->list
, &c
->bad_list
);
196 c
->nr_erasing_blocks
--;
197 spin_unlock(&c
->erase_completion_lock
);
198 wake_up(&c
->erase_wait
);
202 static void jffs2_erase_callback(struct erase_info
*instr
)
204 struct erase_priv_struct
*priv
= (void *)instr
->priv
;
206 if(instr
->state
!= MTD_ERASE_DONE
) {
207 printk(KERN_WARNING
"Erase at 0x%08x finished, but state != MTD_ERASE_DONE. State is 0x%x instead.\n", instr
->addr
, instr
->state
);
208 jffs2_erase_failed(priv
->c
, priv
->jeb
, instr
->fail_addr
);
210 jffs2_erase_succeeded(priv
->c
, priv
->jeb
);
216 /* Hmmm. Maybe we should accept the extra space it takes and make
217 this a standard doubly-linked list? */
218 static inline void jffs2_remove_node_refs_from_ino_list(struct jffs2_sb_info
*c
,
219 struct jffs2_raw_node_ref
*ref
, struct jffs2_eraseblock
*jeb
)
221 struct jffs2_inode_cache
*ic
= NULL
;
222 struct jffs2_raw_node_ref
**prev
;
224 prev
= &ref
->next_in_ino
;
226 /* Walk the inode's list once, removing any nodes from this eraseblock */
228 if (!(*prev
)->next_in_ino
) {
229 /* We're looking at the jffs2_inode_cache, which is
230 at the end of the linked list. Stash it and continue
231 from the beginning of the list */
232 ic
= (struct jffs2_inode_cache
*)(*prev
);
237 if (SECTOR_ADDR((*prev
)->flash_offset
) == jeb
->offset
) {
238 /* It's in the block we're erasing */
239 struct jffs2_raw_node_ref
*this;
242 *prev
= this->next_in_ino
;
243 this->next_in_ino
= NULL
;
250 /* Not to be deleted. Skip */
251 prev
= &((*prev
)->next_in_ino
);
256 printk(KERN_WARNING
"inode_cache not found in remove_node_refs()!!\n");
260 D1(printk(KERN_DEBUG
"Removed nodes in range 0x%08x-0x%08x from ino #%u\n",
261 jeb
->offset
, jeb
->offset
+ c
->sector_size
, ic
->ino
));
265 struct jffs2_raw_node_ref
*this;
266 printk(KERN_DEBUG
"After remove_node_refs_from_ino_list: \n" KERN_DEBUG
);
271 printk( "0x%08x(%d)->", ref_offset(this), ref_flags(this));
273 printk("\n" KERN_DEBUG
);
276 this = this->next_in_ino
;
281 if (ic
->nodes
== (void *)ic
&& ic
->nlink
== 0)
282 jffs2_del_ino_cache(c
, ic
);
285 static void jffs2_free_all_node_refs(struct jffs2_sb_info
*c
, struct jffs2_eraseblock
*jeb
)
287 struct jffs2_raw_node_ref
*ref
;
288 D1(printk(KERN_DEBUG
"Freeing all node refs for eraseblock offset 0x%08x\n", jeb
->offset
));
289 while(jeb
->first_node
) {
290 ref
= jeb
->first_node
;
291 jeb
->first_node
= ref
->next_phys
;
293 /* Remove from the inode-list */
294 if (ref
->next_in_ino
)
295 jffs2_remove_node_refs_from_ino_list(c
, ref
, jeb
);
296 /* else it was a non-inode node or already removed, so don't bother */
298 jffs2_free_raw_node_ref(ref
);
300 jeb
->last_node
= NULL
;
303 static int jffs2_block_check_erase(struct jffs2_sb_info
*c
, struct jffs2_eraseblock
*jeb
, uint32_t *bad_offset
)
310 ebuf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
312 printk(KERN_WARNING
"Failed to allocate page buffer for verifying erase at 0x%08x. Refiling\n", jeb
->offset
);
316 D1(printk(KERN_DEBUG
"Verifying erase at 0x%08x\n", jeb
->offset
));
318 for (ofs
= jeb
->offset
; ofs
< jeb
->offset
+ c
->sector_size
; ) {
319 uint32_t readlen
= min((uint32_t)PAGE_SIZE
, jeb
->offset
+ c
->sector_size
- ofs
);
324 ret
= jffs2_flash_read(c
, ofs
, readlen
, &retlen
, ebuf
);
326 printk(KERN_WARNING
"Read of newly-erased block at 0x%08x failed: %d. Putting on bad_list\n", ofs
, ret
);
329 if (retlen
!= readlen
) {
330 printk(KERN_WARNING
"Short read from newly-erased block at 0x%08x. Wanted %d, got %zd\n", ofs
, readlen
, retlen
);
333 for (i
=0; i
<readlen
; i
+= sizeof(unsigned long)) {
334 /* It's OK. We know it's properly aligned */
335 unsigned long *datum
= ebuf
+ i
;
338 printk(KERN_WARNING
"Newly-erased block contained word 0x%lx at offset 0x%08x\n", *datum
, *bad_offset
);
351 static void jffs2_mark_erased_block(struct jffs2_sb_info
*c
, struct jffs2_eraseblock
*jeb
)
353 struct jffs2_raw_node_ref
*marker_ref
= NULL
;
358 switch (jffs2_block_check_erase(c
, jeb
, &bad_offset
)) {
359 case -EAGAIN
: goto refile
;
360 case -EIO
: goto filebad
;
363 /* Write the erase complete marker */
364 D1(printk(KERN_DEBUG
"Writing erased marker to block at 0x%08x\n", jeb
->offset
));
365 bad_offset
= jeb
->offset
;
367 /* Cleanmarker in oob area or no cleanmarker at all ? */
368 if (jffs2_cleanmarker_oob(c
) || c
->cleanmarker_size
== 0) {
370 if (jffs2_cleanmarker_oob(c
)) {
371 if (jffs2_write_nand_cleanmarker(c
, jeb
))
375 jeb
->first_node
= jeb
->last_node
= NULL
;
376 jeb
->free_size
= c
->sector_size
;
379 jeb
->wasted_size
= 0;
384 struct jffs2_unknown_node marker
= {
385 .magic
= cpu_to_je16(JFFS2_MAGIC_BITMASK
),
386 .nodetype
= cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER
),
387 .totlen
= cpu_to_je32(c
->cleanmarker_size
)
390 marker_ref
= jffs2_alloc_raw_node_ref();
392 printk(KERN_WARNING
"Failed to allocate raw node ref for clean marker. Refiling\n");
396 marker
.hdr_crc
= cpu_to_je32(crc32(0, &marker
, sizeof(struct jffs2_unknown_node
)-4));
398 vecs
[0].iov_base
= (unsigned char *) &marker
;
399 vecs
[0].iov_len
= sizeof(marker
);
400 ret
= jffs2_flash_direct_writev(c
, vecs
, 1, jeb
->offset
, &retlen
);
402 if (ret
|| retlen
!= sizeof(marker
)) {
404 printk(KERN_WARNING
"Write clean marker to block at 0x%08x failed: %d\n",
407 printk(KERN_WARNING
"Short write to newly-erased block at 0x%08x: Wanted %zd, got %zd\n",
408 jeb
->offset
, sizeof(marker
), retlen
);
410 jffs2_free_raw_node_ref(marker_ref
);
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
;
419 jeb
->first_node
= jeb
->last_node
= marker_ref
;
421 jeb
->free_size
= c
->sector_size
- c
->cleanmarker_size
;
422 jeb
->used_size
= c
->cleanmarker_size
;
424 jeb
->wasted_size
= 0;
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
;
432 ACCT_SANITY_CHECK(c
,jeb
);
433 D1(ACCT_PARANOIA_CHECK(jeb
));
435 list_add_tail(&jeb
->list
, &c
->free_list
);
436 c
->nr_erasing_blocks
--;
438 spin_unlock(&c
->erase_completion_lock
);
439 wake_up(&c
->erase_wait
);
443 spin_lock(&c
->erase_completion_lock
);
444 /* Stick it on a list (any list) so erase_failed can take it
445 right off again. Silly, but shouldn't happen often. */
446 list_add(&jeb
->list
, &c
->erasing_list
);
447 spin_unlock(&c
->erase_completion_lock
);
448 jffs2_erase_failed(c
, jeb
, bad_offset
);
452 /* Stick it back on the list from whence it came and come back later */
453 jffs2_erase_pending_trigger(c
);
454 spin_lock(&c
->erase_completion_lock
);
455 list_add(&jeb
->list
, &c
->erase_complete_list
);
456 spin_unlock(&c
->erase_completion_lock
);