2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright © 2001-2007 Red Hat, Inc.
5 * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
7 * Created by David Woodhouse <dwmw2@infradead.org>
9 * For licensing information, see the file 'LICENCE' in this directory.
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/mtd/mtd.h>
16 #include <linux/compiler.h>
17 #include <linux/crc32.h>
18 #include <linux/sched.h>
19 #include <linux/pagemap.h>
22 struct erase_priv_struct
{
23 struct jffs2_eraseblock
*jeb
;
24 struct jffs2_sb_info
*c
;
28 static void jffs2_erase_callback(struct erase_info
*);
30 static void jffs2_erase_failed(struct jffs2_sb_info
*c
, struct jffs2_eraseblock
*jeb
, uint32_t bad_offset
);
31 static void jffs2_erase_succeeded(struct jffs2_sb_info
*c
, struct jffs2_eraseblock
*jeb
);
32 static void jffs2_mark_erased_block(struct jffs2_sb_info
*c
, struct jffs2_eraseblock
*jeb
);
34 static void jffs2_erase_block(struct jffs2_sb_info
*c
,
35 struct jffs2_eraseblock
*jeb
)
40 ret
= jffs2_flash_erase(c
, jeb
);
42 jffs2_erase_succeeded(c
, jeb
);
45 bad_offset
= jeb
->offset
;
47 struct erase_info
*instr
;
49 D1(printk(KERN_DEBUG
"jffs2_erase_block(): erase block %#08x (range %#08x-%#08x)\n",
50 jeb
->offset
, jeb
->offset
, jeb
->offset
+ c
->sector_size
));
51 instr
= kmalloc(sizeof(struct erase_info
) + sizeof(struct erase_priv_struct
), GFP_KERNEL
);
53 printk(KERN_WARNING
"kmalloc for struct erase_info in jffs2_erase_block failed. Refiling block for later\n");
54 mutex_lock(&c
->erase_free_sem
);
55 spin_lock(&c
->erase_completion_lock
);
56 list_move(&jeb
->list
, &c
->erase_pending_list
);
57 c
->erasing_size
-= c
->sector_size
;
58 c
->dirty_size
+= c
->sector_size
;
59 jeb
->dirty_size
= c
->sector_size
;
60 spin_unlock(&c
->erase_completion_lock
);
61 mutex_unlock(&c
->erase_free_sem
);
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
= MTD_FAIL_ADDR_UNKNOWN
;
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 mutex_lock(&c
->erase_free_sem
);
89 spin_lock(&c
->erase_completion_lock
);
90 list_move(&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
);
95 mutex_unlock(&c
->erase_free_sem
);
100 printk(KERN_WARNING
"Erase at 0x%08x failed immediately: -EROFS. Is the sector locked?\n", jeb
->offset
);
102 printk(KERN_WARNING
"Erase at 0x%08x failed immediately: errno %d\n", jeb
->offset
, ret
);
104 jffs2_erase_failed(c
, jeb
, bad_offset
);
107 int jffs2_erase_pending_blocks(struct jffs2_sb_info
*c
, int count
)
109 struct jffs2_eraseblock
*jeb
;
112 mutex_lock(&c
->erase_free_sem
);
114 spin_lock(&c
->erase_completion_lock
);
116 while (!list_empty(&c
->erase_complete_list
) ||
117 !list_empty(&c
->erase_pending_list
)) {
119 if (!list_empty(&c
->erase_complete_list
)) {
120 jeb
= list_entry(c
->erase_complete_list
.next
, struct jffs2_eraseblock
, list
);
121 list_move(&jeb
->list
, &c
->erase_checking_list
);
122 spin_unlock(&c
->erase_completion_lock
);
123 mutex_unlock(&c
->erase_free_sem
);
124 jffs2_mark_erased_block(c
, jeb
);
128 D1(printk(KERN_DEBUG
"Count reached. jffs2_erase_pending_blocks leaving\n"));
132 } else if (!list_empty(&c
->erase_pending_list
)) {
133 jeb
= list_entry(c
->erase_pending_list
.next
, struct jffs2_eraseblock
, list
);
134 D1(printk(KERN_DEBUG
"Starting erase of pending block 0x%08x\n", jeb
->offset
));
135 list_del(&jeb
->list
);
136 c
->erasing_size
+= c
->sector_size
;
137 c
->wasted_size
-= jeb
->wasted_size
;
138 c
->free_size
-= jeb
->free_size
;
139 c
->used_size
-= jeb
->used_size
;
140 c
->dirty_size
-= jeb
->dirty_size
;
141 jeb
->wasted_size
= jeb
->used_size
= jeb
->dirty_size
= jeb
->free_size
= 0;
142 jffs2_free_jeb_node_refs(c
, jeb
);
143 list_add(&jeb
->list
, &c
->erasing_list
);
144 spin_unlock(&c
->erase_completion_lock
);
145 mutex_unlock(&c
->erase_free_sem
);
147 jffs2_erase_block(c
, jeb
);
155 mutex_lock(&c
->erase_free_sem
);
156 spin_lock(&c
->erase_completion_lock
);
159 spin_unlock(&c
->erase_completion_lock
);
160 mutex_unlock(&c
->erase_free_sem
);
162 D1(printk(KERN_DEBUG
"jffs2_erase_pending_blocks completed\n"));
166 static void jffs2_erase_succeeded(struct jffs2_sb_info
*c
, struct jffs2_eraseblock
*jeb
)
168 D1(printk(KERN_DEBUG
"Erase completed successfully at 0x%08x\n", jeb
->offset
));
169 mutex_lock(&c
->erase_free_sem
);
170 spin_lock(&c
->erase_completion_lock
);
171 list_move_tail(&jeb
->list
, &c
->erase_complete_list
);
172 /* Wake the GC thread to mark them clean */
173 jffs2_garbage_collect_trigger(c
);
174 spin_unlock(&c
->erase_completion_lock
);
175 mutex_unlock(&c
->erase_free_sem
);
176 wake_up(&c
->erase_wait
);
179 static void jffs2_erase_failed(struct jffs2_sb_info
*c
, struct jffs2_eraseblock
*jeb
, uint32_t bad_offset
)
181 /* For NAND, if the failure did not occur at the device level for a
182 specific physical page, don't bother updating the bad block table. */
183 if (jffs2_cleanmarker_oob(c
) && (bad_offset
!= (uint32_t)MTD_FAIL_ADDR_UNKNOWN
)) {
184 /* We had a device-level failure to erase. Let's see if we've
185 failed too many times. */
186 if (!jffs2_write_nand_badblock(c
, jeb
, bad_offset
)) {
187 /* We'd like to give this block another try. */
188 mutex_lock(&c
->erase_free_sem
);
189 spin_lock(&c
->erase_completion_lock
);
190 list_move(&jeb
->list
, &c
->erase_pending_list
);
191 c
->erasing_size
-= c
->sector_size
;
192 c
->dirty_size
+= c
->sector_size
;
193 jeb
->dirty_size
= c
->sector_size
;
194 spin_unlock(&c
->erase_completion_lock
);
195 mutex_unlock(&c
->erase_free_sem
);
200 mutex_lock(&c
->erase_free_sem
);
201 spin_lock(&c
->erase_completion_lock
);
202 c
->erasing_size
-= c
->sector_size
;
203 c
->bad_size
+= c
->sector_size
;
204 list_move(&jeb
->list
, &c
->bad_list
);
205 c
->nr_erasing_blocks
--;
206 spin_unlock(&c
->erase_completion_lock
);
207 mutex_unlock(&c
->erase_free_sem
);
208 wake_up(&c
->erase_wait
);
212 static void jffs2_erase_callback(struct erase_info
*instr
)
214 struct erase_priv_struct
*priv
= (void *)instr
->priv
;
216 if(instr
->state
!= MTD_ERASE_DONE
) {
217 printk(KERN_WARNING
"Erase at 0x%08llx finished, but state != MTD_ERASE_DONE. State is 0x%x instead.\n",
218 (unsigned long long)instr
->addr
, instr
->state
);
219 jffs2_erase_failed(priv
->c
, priv
->jeb
, instr
->fail_addr
);
221 jffs2_erase_succeeded(priv
->c
, priv
->jeb
);
227 /* Hmmm. Maybe we should accept the extra space it takes and make
228 this a standard doubly-linked list? */
229 static inline void jffs2_remove_node_refs_from_ino_list(struct jffs2_sb_info
*c
,
230 struct jffs2_raw_node_ref
*ref
, struct jffs2_eraseblock
*jeb
)
232 struct jffs2_inode_cache
*ic
= NULL
;
233 struct jffs2_raw_node_ref
**prev
;
235 prev
= &ref
->next_in_ino
;
237 /* Walk the inode's list once, removing any nodes from this eraseblock */
239 if (!(*prev
)->next_in_ino
) {
240 /* We're looking at the jffs2_inode_cache, which is
241 at the end of the linked list. Stash it and continue
242 from the beginning of the list */
243 ic
= (struct jffs2_inode_cache
*)(*prev
);
248 if (SECTOR_ADDR((*prev
)->flash_offset
) == jeb
->offset
) {
249 /* It's in the block we're erasing */
250 struct jffs2_raw_node_ref
*this;
253 *prev
= this->next_in_ino
;
254 this->next_in_ino
= NULL
;
261 /* Not to be deleted. Skip */
262 prev
= &((*prev
)->next_in_ino
);
267 JFFS2_WARNING("inode_cache/xattr_datum/xattr_ref"
268 " not found in remove_node_refs()!!\n");
272 D1(printk(KERN_DEBUG
"Removed nodes in range 0x%08x-0x%08x from ino #%u\n",
273 jeb
->offset
, jeb
->offset
+ c
->sector_size
, ic
->ino
));
277 struct jffs2_raw_node_ref
*this;
278 printk(KERN_DEBUG
"After remove_node_refs_from_ino_list: \n");
284 printk(KERN_CONT
"0x%08x(%d)->",
285 ref_offset(this), ref_flags(this));
290 this = this->next_in_ino
;
292 printk(KERN_CONT
"\n");
296 #ifdef CONFIG_JFFS2_FS_XATTR
297 case RAWNODE_CLASS_XATTR_DATUM
:
298 jffs2_release_xattr_datum(c
, (struct jffs2_xattr_datum
*)ic
);
300 case RAWNODE_CLASS_XATTR_REF
:
301 jffs2_release_xattr_ref(c
, (struct jffs2_xattr_ref
*)ic
);
305 if (ic
->nodes
== (void *)ic
&& ic
->pino_nlink
== 0)
306 jffs2_del_ino_cache(c
, ic
);
310 void jffs2_free_jeb_node_refs(struct jffs2_sb_info
*c
, struct jffs2_eraseblock
*jeb
)
312 struct jffs2_raw_node_ref
*block
, *ref
;
313 D1(printk(KERN_DEBUG
"Freeing all node refs for eraseblock offset 0x%08x\n", jeb
->offset
));
315 block
= ref
= jeb
->first_node
;
318 if (ref
->flash_offset
== REF_LINK_NODE
) {
319 ref
= ref
->next_in_ino
;
320 jffs2_free_refblock(block
);
324 if (ref
->flash_offset
!= REF_EMPTY_NODE
&& ref
->next_in_ino
)
325 jffs2_remove_node_refs_from_ino_list(c
, ref
, jeb
);
326 /* else it was a non-inode node or already removed, so don't bother */
330 jeb
->first_node
= jeb
->last_node
= NULL
;
333 static int jffs2_block_check_erase(struct jffs2_sb_info
*c
, struct jffs2_eraseblock
*jeb
, uint32_t *bad_offset
)
341 unsigned long *wordebuf
;
343 ret
= c
->mtd
->point(c
->mtd
, jeb
->offset
, c
->sector_size
,
344 &retlen
, &ebuf
, NULL
);
346 D1(printk(KERN_DEBUG
"MTD point failed %d\n", ret
));
349 if (retlen
< c
->sector_size
) {
350 /* Don't muck about if it won't let us point to the whole erase sector */
351 D1(printk(KERN_DEBUG
"MTD point returned len too short: 0x%zx\n", retlen
));
352 c
->mtd
->unpoint(c
->mtd
, jeb
->offset
, retlen
);
355 wordebuf
= ebuf
-sizeof(*wordebuf
);
356 retlen
/= sizeof(*wordebuf
);
358 if (*++wordebuf
!= ~0)
361 c
->mtd
->unpoint(c
->mtd
, jeb
->offset
, c
->sector_size
);
363 printk(KERN_WARNING
"Newly-erased block contained word 0x%lx at offset 0x%08tx\n",
364 *wordebuf
, jeb
->offset
+ c
->sector_size
-retlen
*sizeof(*wordebuf
));
370 ebuf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
372 printk(KERN_WARNING
"Failed to allocate page buffer for verifying erase at 0x%08x. Refiling\n", jeb
->offset
);
376 D1(printk(KERN_DEBUG
"Verifying erase at 0x%08x\n", jeb
->offset
));
378 for (ofs
= jeb
->offset
; ofs
< jeb
->offset
+ c
->sector_size
; ) {
379 uint32_t readlen
= min((uint32_t)PAGE_SIZE
, jeb
->offset
+ c
->sector_size
- ofs
);
384 ret
= c
->mtd
->read(c
->mtd
, ofs
, readlen
, &retlen
, ebuf
);
386 printk(KERN_WARNING
"Read of newly-erased block at 0x%08x failed: %d. Putting on bad_list\n", ofs
, ret
);
390 if (retlen
!= readlen
) {
391 printk(KERN_WARNING
"Short read from newly-erased block at 0x%08x. Wanted %d, got %zd\n", ofs
, readlen
, retlen
);
395 for (i
=0; i
<readlen
; i
+= sizeof(unsigned long)) {
396 /* It's OK. We know it's properly aligned */
397 unsigned long *datum
= ebuf
+ i
;
400 printk(KERN_WARNING
"Newly-erased block contained word 0x%lx at offset 0x%08x\n", *datum
, *bad_offset
);
414 static void jffs2_mark_erased_block(struct jffs2_sb_info
*c
, struct jffs2_eraseblock
*jeb
)
418 uint32_t uninitialized_var(bad_offset
);
420 switch (jffs2_block_check_erase(c
, jeb
, &bad_offset
)) {
421 case -EAGAIN
: goto refile
;
422 case -EIO
: goto filebad
;
425 /* Write the erase complete marker */
426 D1(printk(KERN_DEBUG
"Writing erased marker to block at 0x%08x\n", jeb
->offset
));
427 bad_offset
= jeb
->offset
;
429 /* Cleanmarker in oob area or no cleanmarker at all ? */
430 if (jffs2_cleanmarker_oob(c
) || c
->cleanmarker_size
== 0) {
432 if (jffs2_cleanmarker_oob(c
)) {
433 if (jffs2_write_nand_cleanmarker(c
, jeb
))
439 struct jffs2_unknown_node marker
= {
440 .magic
= cpu_to_je16(JFFS2_MAGIC_BITMASK
),
441 .nodetype
= cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER
),
442 .totlen
= cpu_to_je32(c
->cleanmarker_size
)
445 jffs2_prealloc_raw_node_refs(c
, jeb
, 1);
447 marker
.hdr_crc
= cpu_to_je32(crc32(0, &marker
, sizeof(struct jffs2_unknown_node
)-4));
449 vecs
[0].iov_base
= (unsigned char *) &marker
;
450 vecs
[0].iov_len
= sizeof(marker
);
451 ret
= jffs2_flash_direct_writev(c
, vecs
, 1, jeb
->offset
, &retlen
);
453 if (ret
|| retlen
!= sizeof(marker
)) {
455 printk(KERN_WARNING
"Write clean marker to block at 0x%08x failed: %d\n",
458 printk(KERN_WARNING
"Short write to newly-erased block at 0x%08x: Wanted %zd, got %zd\n",
459 jeb
->offset
, sizeof(marker
), retlen
);
464 /* Everything else got zeroed before the erase */
465 jeb
->free_size
= c
->sector_size
;
467 mutex_lock(&c
->erase_free_sem
);
468 spin_lock(&c
->erase_completion_lock
);
470 c
->erasing_size
-= c
->sector_size
;
471 c
->free_size
+= c
->sector_size
;
473 /* Account for cleanmarker now, if it's in-band */
474 if (c
->cleanmarker_size
&& !jffs2_cleanmarker_oob(c
))
475 jffs2_link_node_ref(c
, jeb
, jeb
->offset
| REF_NORMAL
, c
->cleanmarker_size
, NULL
);
477 list_move_tail(&jeb
->list
, &c
->free_list
);
478 c
->nr_erasing_blocks
--;
481 jffs2_dbg_acct_sanity_check_nolock(c
, jeb
);
482 jffs2_dbg_acct_paranoia_check_nolock(c
, jeb
);
484 spin_unlock(&c
->erase_completion_lock
);
485 mutex_unlock(&c
->erase_free_sem
);
486 wake_up(&c
->erase_wait
);
490 jffs2_erase_failed(c
, jeb
, bad_offset
);
494 /* Stick it back on the list from whence it came and come back later */
495 mutex_lock(&c
->erase_free_sem
);
496 spin_lock(&c
->erase_completion_lock
);
497 jffs2_garbage_collect_trigger(c
);
498 list_move(&jeb
->list
, &c
->erase_complete_list
);
499 spin_unlock(&c
->erase_completion_lock
);
500 mutex_unlock(&c
->erase_free_sem
);