2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright © 2001-2007 Red Hat, Inc.
6 * Created by David Woodhouse <dwmw2@infradead.org>
8 * For licensing information, see the file 'LICENCE' in this directory.
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/mtd/mtd.h>
15 #include <linux/compiler.h>
16 #include <linux/crc32.h>
17 #include <linux/sched.h>
18 #include <linux/pagemap.h>
21 struct erase_priv_struct
{
22 struct jffs2_eraseblock
*jeb
;
23 struct jffs2_sb_info
*c
;
27 static void jffs2_erase_callback(struct erase_info
*);
29 static void jffs2_erase_failed(struct jffs2_sb_info
*c
, struct jffs2_eraseblock
*jeb
, uint32_t bad_offset
);
30 static void jffs2_erase_succeeded(struct jffs2_sb_info
*c
, struct jffs2_eraseblock
*jeb
);
31 static void jffs2_mark_erased_block(struct jffs2_sb_info
*c
, struct jffs2_eraseblock
*jeb
);
33 static void jffs2_erase_block(struct jffs2_sb_info
*c
,
34 struct jffs2_eraseblock
*jeb
)
39 ret
= jffs2_flash_erase(c
, jeb
);
41 jffs2_erase_succeeded(c
, jeb
);
44 bad_offset
= jeb
->offset
;
46 struct erase_info
*instr
;
48 D1(printk(KERN_DEBUG
"jffs2_erase_block(): erase block %#08x (range %#08x-%#08x)\n",
49 jeb
->offset
, jeb
->offset
, jeb
->offset
+ c
->sector_size
));
50 instr
= kmalloc(sizeof(struct erase_info
) + sizeof(struct erase_priv_struct
), GFP_KERNEL
);
52 printk(KERN_WARNING
"kmalloc for struct erase_info in jffs2_erase_block failed. Refiling block for later\n");
53 mutex_lock(&c
->erase_free_sem
);
54 spin_lock(&c
->erase_completion_lock
);
55 list_move(&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 mutex_unlock(&c
->erase_free_sem
);
64 memset(instr
, 0, sizeof(*instr
));
67 instr
->addr
= jeb
->offset
;
68 instr
->len
= c
->sector_size
;
69 instr
->callback
= jffs2_erase_callback
;
70 instr
->priv
= (unsigned long)(&instr
[1]);
71 instr
->fail_addr
= MTD_FAIL_ADDR_UNKNOWN
;
73 ((struct erase_priv_struct
*)instr
->priv
)->jeb
= jeb
;
74 ((struct erase_priv_struct
*)instr
->priv
)->c
= c
;
76 ret
= c
->mtd
->erase(c
->mtd
, instr
);
80 bad_offset
= instr
->fail_addr
;
84 if (ret
== -ENOMEM
|| ret
== -EAGAIN
) {
85 /* Erase failed immediately. Refile it on the list */
86 D1(printk(KERN_DEBUG
"Erase at 0x%08x failed: %d. Refiling on erase_pending_list\n", jeb
->offset
, ret
));
87 mutex_lock(&c
->erase_free_sem
);
88 spin_lock(&c
->erase_completion_lock
);
89 list_move(&jeb
->list
, &c
->erase_pending_list
);
90 c
->erasing_size
-= c
->sector_size
;
91 c
->dirty_size
+= c
->sector_size
;
92 jeb
->dirty_size
= c
->sector_size
;
93 spin_unlock(&c
->erase_completion_lock
);
94 mutex_unlock(&c
->erase_free_sem
);
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 int jffs2_erase_pending_blocks(struct jffs2_sb_info
*c
, int count
)
108 struct jffs2_eraseblock
*jeb
;
111 mutex_lock(&c
->erase_free_sem
);
113 spin_lock(&c
->erase_completion_lock
);
115 while (!list_empty(&c
->erase_complete_list
) ||
116 !list_empty(&c
->erase_pending_list
)) {
118 if (!list_empty(&c
->erase_complete_list
)) {
119 jeb
= list_entry(c
->erase_complete_list
.next
, struct jffs2_eraseblock
, list
);
120 list_move(&jeb
->list
, &c
->erase_checking_list
);
121 spin_unlock(&c
->erase_completion_lock
);
122 mutex_unlock(&c
->erase_free_sem
);
123 jffs2_mark_erased_block(c
, jeb
);
127 D1(printk(KERN_DEBUG
"Count reached. jffs2_erase_pending_blocks leaving\n"));
131 } else if (!list_empty(&c
->erase_pending_list
)) {
132 jeb
= list_entry(c
->erase_pending_list
.next
, struct jffs2_eraseblock
, list
);
133 D1(printk(KERN_DEBUG
"Starting erase of pending block 0x%08x\n", jeb
->offset
));
134 list_del(&jeb
->list
);
135 c
->erasing_size
+= c
->sector_size
;
136 c
->wasted_size
-= jeb
->wasted_size
;
137 c
->free_size
-= jeb
->free_size
;
138 c
->used_size
-= jeb
->used_size
;
139 c
->dirty_size
-= jeb
->dirty_size
;
140 jeb
->wasted_size
= jeb
->used_size
= jeb
->dirty_size
= jeb
->free_size
= 0;
141 jffs2_free_jeb_node_refs(c
, jeb
);
142 list_add(&jeb
->list
, &c
->erasing_list
);
143 spin_unlock(&c
->erase_completion_lock
);
144 mutex_unlock(&c
->erase_free_sem
);
146 jffs2_erase_block(c
, jeb
);
154 mutex_lock(&c
->erase_free_sem
);
155 spin_lock(&c
->erase_completion_lock
);
158 spin_unlock(&c
->erase_completion_lock
);
159 mutex_unlock(&c
->erase_free_sem
);
161 D1(printk(KERN_DEBUG
"jffs2_erase_pending_blocks completed\n"));
165 static void jffs2_erase_succeeded(struct jffs2_sb_info
*c
, struct jffs2_eraseblock
*jeb
)
167 D1(printk(KERN_DEBUG
"Erase completed successfully at 0x%08x\n", jeb
->offset
));
168 mutex_lock(&c
->erase_free_sem
);
169 spin_lock(&c
->erase_completion_lock
);
170 list_move_tail(&jeb
->list
, &c
->erase_complete_list
);
171 /* Wake the GC thread to mark them clean */
172 jffs2_garbage_collect_trigger(c
);
173 spin_unlock(&c
->erase_completion_lock
);
174 mutex_unlock(&c
->erase_free_sem
);
175 wake_up(&c
->erase_wait
);
178 static void jffs2_erase_failed(struct jffs2_sb_info
*c
, struct jffs2_eraseblock
*jeb
, uint32_t bad_offset
)
180 /* For NAND, if the failure did not occur at the device level for a
181 specific physical page, don't bother updating the bad block table. */
182 if (jffs2_cleanmarker_oob(c
) && (bad_offset
!= (uint32_t)MTD_FAIL_ADDR_UNKNOWN
)) {
183 /* We had a device-level failure to erase. Let's see if we've
184 failed too many times. */
185 if (!jffs2_write_nand_badblock(c
, jeb
, bad_offset
)) {
186 /* We'd like to give this block another try. */
187 mutex_lock(&c
->erase_free_sem
);
188 spin_lock(&c
->erase_completion_lock
);
189 list_move(&jeb
->list
, &c
->erase_pending_list
);
190 c
->erasing_size
-= c
->sector_size
;
191 c
->dirty_size
+= c
->sector_size
;
192 jeb
->dirty_size
= c
->sector_size
;
193 spin_unlock(&c
->erase_completion_lock
);
194 mutex_unlock(&c
->erase_free_sem
);
199 mutex_lock(&c
->erase_free_sem
);
200 spin_lock(&c
->erase_completion_lock
);
201 c
->erasing_size
-= c
->sector_size
;
202 c
->bad_size
+= c
->sector_size
;
203 list_move(&jeb
->list
, &c
->bad_list
);
204 c
->nr_erasing_blocks
--;
205 spin_unlock(&c
->erase_completion_lock
);
206 mutex_unlock(&c
->erase_free_sem
);
207 wake_up(&c
->erase_wait
);
211 static void jffs2_erase_callback(struct erase_info
*instr
)
213 struct erase_priv_struct
*priv
= (void *)instr
->priv
;
215 if(instr
->state
!= MTD_ERASE_DONE
) {
216 printk(KERN_WARNING
"Erase at 0x%08llx finished, but state != MTD_ERASE_DONE. State is 0x%x instead.\n",
217 (unsigned long long)instr
->addr
, instr
->state
);
218 jffs2_erase_failed(priv
->c
, priv
->jeb
, instr
->fail_addr
);
220 jffs2_erase_succeeded(priv
->c
, priv
->jeb
);
226 /* Hmmm. Maybe we should accept the extra space it takes and make
227 this a standard doubly-linked list? */
228 static inline void jffs2_remove_node_refs_from_ino_list(struct jffs2_sb_info
*c
,
229 struct jffs2_raw_node_ref
*ref
, struct jffs2_eraseblock
*jeb
)
231 struct jffs2_inode_cache
*ic
= NULL
;
232 struct jffs2_raw_node_ref
**prev
;
234 prev
= &ref
->next_in_ino
;
236 /* Walk the inode's list once, removing any nodes from this eraseblock */
238 if (!(*prev
)->next_in_ino
) {
239 /* We're looking at the jffs2_inode_cache, which is
240 at the end of the linked list. Stash it and continue
241 from the beginning of the list */
242 ic
= (struct jffs2_inode_cache
*)(*prev
);
247 if (SECTOR_ADDR((*prev
)->flash_offset
) == jeb
->offset
) {
248 /* It's in the block we're erasing */
249 struct jffs2_raw_node_ref
*this;
252 *prev
= this->next_in_ino
;
253 this->next_in_ino
= NULL
;
260 /* Not to be deleted. Skip */
261 prev
= &((*prev
)->next_in_ino
);
266 JFFS2_WARNING("inode_cache/xattr_datum/xattr_ref"
267 " not found in remove_node_refs()!!\n");
271 D1(printk(KERN_DEBUG
"Removed nodes in range 0x%08x-0x%08x from ino #%u\n",
272 jeb
->offset
, jeb
->offset
+ c
->sector_size
, ic
->ino
));
276 struct jffs2_raw_node_ref
*this;
277 printk(KERN_DEBUG
"After remove_node_refs_from_ino_list: \n");
283 printk(KERN_CONT
"0x%08x(%d)->",
284 ref_offset(this), ref_flags(this));
289 this = this->next_in_ino
;
291 printk(KERN_CONT
"\n");
295 #ifdef CONFIG_JFFS2_FS_XATTR
296 case RAWNODE_CLASS_XATTR_DATUM
:
297 jffs2_release_xattr_datum(c
, (struct jffs2_xattr_datum
*)ic
);
299 case RAWNODE_CLASS_XATTR_REF
:
300 jffs2_release_xattr_ref(c
, (struct jffs2_xattr_ref
*)ic
);
304 if (ic
->nodes
== (void *)ic
&& ic
->pino_nlink
== 0)
305 jffs2_del_ino_cache(c
, ic
);
309 void jffs2_free_jeb_node_refs(struct jffs2_sb_info
*c
, struct jffs2_eraseblock
*jeb
)
311 struct jffs2_raw_node_ref
*block
, *ref
;
312 D1(printk(KERN_DEBUG
"Freeing all node refs for eraseblock offset 0x%08x\n", jeb
->offset
));
314 block
= ref
= jeb
->first_node
;
317 if (ref
->flash_offset
== REF_LINK_NODE
) {
318 ref
= ref
->next_in_ino
;
319 jffs2_free_refblock(block
);
323 if (ref
->flash_offset
!= REF_EMPTY_NODE
&& ref
->next_in_ino
)
324 jffs2_remove_node_refs_from_ino_list(c
, ref
, jeb
);
325 /* else it was a non-inode node or already removed, so don't bother */
329 jeb
->first_node
= jeb
->last_node
= NULL
;
332 static int jffs2_block_check_erase(struct jffs2_sb_info
*c
, struct jffs2_eraseblock
*jeb
, uint32_t *bad_offset
)
340 unsigned long *wordebuf
;
342 ret
= c
->mtd
->point(c
->mtd
, jeb
->offset
, c
->sector_size
,
343 &retlen
, &ebuf
, NULL
);
345 D1(printk(KERN_DEBUG
"MTD point failed %d\n", ret
));
348 if (retlen
< c
->sector_size
) {
349 /* Don't muck about if it won't let us point to the whole erase sector */
350 D1(printk(KERN_DEBUG
"MTD point returned len too short: 0x%zx\n", retlen
));
351 c
->mtd
->unpoint(c
->mtd
, jeb
->offset
, retlen
);
354 wordebuf
= ebuf
-sizeof(*wordebuf
);
355 retlen
/= sizeof(*wordebuf
);
357 if (*++wordebuf
!= ~0)
360 c
->mtd
->unpoint(c
->mtd
, jeb
->offset
, c
->sector_size
);
362 printk(KERN_WARNING
"Newly-erased block contained word 0x%lx at offset 0x%08tx\n",
363 *wordebuf
, jeb
->offset
+ c
->sector_size
-retlen
*sizeof(*wordebuf
));
369 ebuf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
371 printk(KERN_WARNING
"Failed to allocate page buffer for verifying erase at 0x%08x. Refiling\n", jeb
->offset
);
375 D1(printk(KERN_DEBUG
"Verifying erase at 0x%08x\n", jeb
->offset
));
377 for (ofs
= jeb
->offset
; ofs
< jeb
->offset
+ c
->sector_size
; ) {
378 uint32_t readlen
= min((uint32_t)PAGE_SIZE
, jeb
->offset
+ c
->sector_size
- ofs
);
383 ret
= c
->mtd
->read(c
->mtd
, ofs
, readlen
, &retlen
, ebuf
);
385 printk(KERN_WARNING
"Read of newly-erased block at 0x%08x failed: %d. Putting on bad_list\n", ofs
, ret
);
389 if (retlen
!= readlen
) {
390 printk(KERN_WARNING
"Short read from newly-erased block at 0x%08x. Wanted %d, got %zd\n", ofs
, readlen
, retlen
);
394 for (i
=0; i
<readlen
; i
+= sizeof(unsigned long)) {
395 /* It's OK. We know it's properly aligned */
396 unsigned long *datum
= ebuf
+ i
;
399 printk(KERN_WARNING
"Newly-erased block contained word 0x%lx at offset 0x%08x\n", *datum
, *bad_offset
);
413 static void jffs2_mark_erased_block(struct jffs2_sb_info
*c
, struct jffs2_eraseblock
*jeb
)
417 uint32_t uninitialized_var(bad_offset
);
419 switch (jffs2_block_check_erase(c
, jeb
, &bad_offset
)) {
420 case -EAGAIN
: goto refile
;
421 case -EIO
: goto filebad
;
424 /* Write the erase complete marker */
425 D1(printk(KERN_DEBUG
"Writing erased marker to block at 0x%08x\n", jeb
->offset
));
426 bad_offset
= jeb
->offset
;
428 /* Cleanmarker in oob area or no cleanmarker at all ? */
429 if (jffs2_cleanmarker_oob(c
) || c
->cleanmarker_size
== 0) {
431 if (jffs2_cleanmarker_oob(c
)) {
432 if (jffs2_write_nand_cleanmarker(c
, jeb
))
438 struct jffs2_unknown_node marker
= {
439 .magic
= cpu_to_je16(JFFS2_MAGIC_BITMASK
),
440 .nodetype
= cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER
),
441 .totlen
= cpu_to_je32(c
->cleanmarker_size
)
444 jffs2_prealloc_raw_node_refs(c
, jeb
, 1);
446 marker
.hdr_crc
= cpu_to_je32(crc32(0, &marker
, sizeof(struct jffs2_unknown_node
)-4));
448 vecs
[0].iov_base
= (unsigned char *) &marker
;
449 vecs
[0].iov_len
= sizeof(marker
);
450 ret
= jffs2_flash_direct_writev(c
, vecs
, 1, jeb
->offset
, &retlen
);
452 if (ret
|| retlen
!= sizeof(marker
)) {
454 printk(KERN_WARNING
"Write clean marker to block at 0x%08x failed: %d\n",
457 printk(KERN_WARNING
"Short write to newly-erased block at 0x%08x: Wanted %zd, got %zd\n",
458 jeb
->offset
, sizeof(marker
), retlen
);
463 /* Everything else got zeroed before the erase */
464 jeb
->free_size
= c
->sector_size
;
466 mutex_lock(&c
->erase_free_sem
);
467 spin_lock(&c
->erase_completion_lock
);
469 c
->erasing_size
-= c
->sector_size
;
470 c
->free_size
+= c
->sector_size
;
472 /* Account for cleanmarker now, if it's in-band */
473 if (c
->cleanmarker_size
&& !jffs2_cleanmarker_oob(c
))
474 jffs2_link_node_ref(c
, jeb
, jeb
->offset
| REF_NORMAL
, c
->cleanmarker_size
, NULL
);
476 list_move_tail(&jeb
->list
, &c
->free_list
);
477 c
->nr_erasing_blocks
--;
480 jffs2_dbg_acct_sanity_check_nolock(c
, jeb
);
481 jffs2_dbg_acct_paranoia_check_nolock(c
, jeb
);
483 spin_unlock(&c
->erase_completion_lock
);
484 mutex_unlock(&c
->erase_free_sem
);
485 wake_up(&c
->erase_wait
);
489 jffs2_erase_failed(c
, jeb
, bad_offset
);
493 /* Stick it back on the list from whence it came and come back later */
494 mutex_lock(&c
->erase_free_sem
);
495 spin_lock(&c
->erase_completion_lock
);
496 jffs2_garbage_collect_trigger(c
);
497 list_move(&jeb
->list
, &c
->erase_complete_list
);
498 spin_unlock(&c
->erase_completion_lock
);
499 mutex_unlock(&c
->erase_free_sem
);