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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
17 #include <linux/mtd/mtd.h>
18 #include <linux/compiler.h>
19 #include <linux/crc32.h>
20 #include <linux/sched.h>
21 #include <linux/pagemap.h>
24 static void jffs2_erase_failed(struct jffs2_sb_info
*c
, struct jffs2_eraseblock
*jeb
, uint32_t bad_offset
);
25 static void jffs2_erase_succeeded(struct jffs2_sb_info
*c
, struct jffs2_eraseblock
*jeb
);
26 static void jffs2_mark_erased_block(struct jffs2_sb_info
*c
, struct jffs2_eraseblock
*jeb
);
28 static void jffs2_erase_block(struct jffs2_sb_info
*c
,
29 struct jffs2_eraseblock
*jeb
)
34 ret
= jffs2_flash_erase(c
, jeb
);
36 jffs2_erase_succeeded(c
, jeb
);
39 bad_offset
= jeb
->offset
;
41 struct erase_info
*instr
;
43 jffs2_dbg(1, "%s(): erase block %#08x (range %#08x-%#08x)\n",
45 jeb
->offset
, jeb
->offset
, jeb
->offset
+ c
->sector_size
);
46 instr
= kzalloc(sizeof(struct erase_info
), GFP_KERNEL
);
48 pr_warn("kzalloc for struct erase_info in jffs2_erase_block failed. Refiling block for later\n");
49 mutex_lock(&c
->erase_free_sem
);
50 spin_lock(&c
->erase_completion_lock
);
51 list_move(&jeb
->list
, &c
->erase_pending_list
);
52 c
->erasing_size
-= c
->sector_size
;
53 c
->dirty_size
+= c
->sector_size
;
54 jeb
->dirty_size
= c
->sector_size
;
55 spin_unlock(&c
->erase_completion_lock
);
56 mutex_unlock(&c
->erase_free_sem
);
60 instr
->addr
= jeb
->offset
;
61 instr
->len
= c
->sector_size
;
63 ret
= mtd_erase(c
->mtd
, instr
);
65 jffs2_erase_succeeded(c
, jeb
);
70 bad_offset
= instr
->fail_addr
;
74 if (ret
== -ENOMEM
|| ret
== -EAGAIN
) {
75 /* Erase failed immediately. Refile it on the list */
76 jffs2_dbg(1, "Erase at 0x%08x failed: %d. Refiling on erase_pending_list\n",
78 mutex_lock(&c
->erase_free_sem
);
79 spin_lock(&c
->erase_completion_lock
);
80 list_move(&jeb
->list
, &c
->erase_pending_list
);
81 c
->erasing_size
-= c
->sector_size
;
82 c
->dirty_size
+= c
->sector_size
;
83 jeb
->dirty_size
= c
->sector_size
;
84 spin_unlock(&c
->erase_completion_lock
);
85 mutex_unlock(&c
->erase_free_sem
);
90 pr_warn("Erase at 0x%08x failed immediately: -EROFS. Is the sector locked?\n",
93 pr_warn("Erase at 0x%08x failed immediately: errno %d\n",
96 jffs2_erase_failed(c
, jeb
, bad_offset
);
99 int jffs2_erase_pending_blocks(struct jffs2_sb_info
*c
, int count
)
101 struct jffs2_eraseblock
*jeb
;
104 mutex_lock(&c
->erase_free_sem
);
106 spin_lock(&c
->erase_completion_lock
);
108 while (!list_empty(&c
->erase_complete_list
) ||
109 !list_empty(&c
->erase_pending_list
)) {
111 if (!list_empty(&c
->erase_complete_list
)) {
112 jeb
= list_entry(c
->erase_complete_list
.next
, struct jffs2_eraseblock
, list
);
113 list_move(&jeb
->list
, &c
->erase_checking_list
);
114 spin_unlock(&c
->erase_completion_lock
);
115 mutex_unlock(&c
->erase_free_sem
);
116 jffs2_mark_erased_block(c
, jeb
);
120 jffs2_dbg(1, "Count reached. jffs2_erase_pending_blocks leaving\n");
124 } else if (!list_empty(&c
->erase_pending_list
)) {
125 jeb
= list_entry(c
->erase_pending_list
.next
, struct jffs2_eraseblock
, list
);
126 jffs2_dbg(1, "Starting erase of pending block 0x%08x\n",
128 list_del(&jeb
->list
);
129 c
->erasing_size
+= c
->sector_size
;
130 c
->wasted_size
-= jeb
->wasted_size
;
131 c
->free_size
-= jeb
->free_size
;
132 c
->used_size
-= jeb
->used_size
;
133 c
->dirty_size
-= jeb
->dirty_size
;
134 jeb
->wasted_size
= jeb
->used_size
= jeb
->dirty_size
= jeb
->free_size
= 0;
135 jffs2_free_jeb_node_refs(c
, jeb
);
136 list_add(&jeb
->list
, &c
->erasing_list
);
137 spin_unlock(&c
->erase_completion_lock
);
138 mutex_unlock(&c
->erase_free_sem
);
140 jffs2_erase_block(c
, jeb
);
148 mutex_lock(&c
->erase_free_sem
);
149 spin_lock(&c
->erase_completion_lock
);
152 spin_unlock(&c
->erase_completion_lock
);
153 mutex_unlock(&c
->erase_free_sem
);
155 jffs2_dbg(1, "jffs2_erase_pending_blocks completed\n");
159 static void jffs2_erase_succeeded(struct jffs2_sb_info
*c
, struct jffs2_eraseblock
*jeb
)
161 jffs2_dbg(1, "Erase completed successfully at 0x%08x\n", jeb
->offset
);
162 mutex_lock(&c
->erase_free_sem
);
163 spin_lock(&c
->erase_completion_lock
);
164 list_move_tail(&jeb
->list
, &c
->erase_complete_list
);
165 /* Wake the GC thread to mark them clean */
166 jffs2_garbage_collect_trigger(c
);
167 spin_unlock(&c
->erase_completion_lock
);
168 mutex_unlock(&c
->erase_free_sem
);
169 wake_up(&c
->erase_wait
);
172 static void jffs2_erase_failed(struct jffs2_sb_info
*c
, struct jffs2_eraseblock
*jeb
, uint32_t bad_offset
)
174 /* For NAND, if the failure did not occur at the device level for a
175 specific physical page, don't bother updating the bad block table. */
176 if (jffs2_cleanmarker_oob(c
) && (bad_offset
!= (uint32_t)MTD_FAIL_ADDR_UNKNOWN
)) {
177 /* We had a device-level failure to erase. Let's see if we've
178 failed too many times. */
179 if (!jffs2_write_nand_badblock(c
, jeb
, bad_offset
)) {
180 /* We'd like to give this block another try. */
181 mutex_lock(&c
->erase_free_sem
);
182 spin_lock(&c
->erase_completion_lock
);
183 list_move(&jeb
->list
, &c
->erase_pending_list
);
184 c
->erasing_size
-= c
->sector_size
;
185 c
->dirty_size
+= c
->sector_size
;
186 jeb
->dirty_size
= c
->sector_size
;
187 spin_unlock(&c
->erase_completion_lock
);
188 mutex_unlock(&c
->erase_free_sem
);
193 mutex_lock(&c
->erase_free_sem
);
194 spin_lock(&c
->erase_completion_lock
);
195 c
->erasing_size
-= c
->sector_size
;
196 c
->bad_size
+= c
->sector_size
;
197 list_move(&jeb
->list
, &c
->bad_list
);
198 c
->nr_erasing_blocks
--;
199 spin_unlock(&c
->erase_completion_lock
);
200 mutex_unlock(&c
->erase_free_sem
);
201 wake_up(&c
->erase_wait
);
204 /* Hmmm. Maybe we should accept the extra space it takes and make
205 this a standard doubly-linked list? */
206 static inline void jffs2_remove_node_refs_from_ino_list(struct jffs2_sb_info
*c
,
207 struct jffs2_raw_node_ref
*ref
, struct jffs2_eraseblock
*jeb
)
209 struct jffs2_inode_cache
*ic
= NULL
;
210 struct jffs2_raw_node_ref
**prev
;
212 prev
= &ref
->next_in_ino
;
214 /* Walk the inode's list once, removing any nodes from this eraseblock */
216 if (!(*prev
)->next_in_ino
) {
217 /* We're looking at the jffs2_inode_cache, which is
218 at the end of the linked list. Stash it and continue
219 from the beginning of the list */
220 ic
= (struct jffs2_inode_cache
*)(*prev
);
225 if (SECTOR_ADDR((*prev
)->flash_offset
) == jeb
->offset
) {
226 /* It's in the block we're erasing */
227 struct jffs2_raw_node_ref
*this;
230 *prev
= this->next_in_ino
;
231 this->next_in_ino
= NULL
;
238 /* Not to be deleted. Skip */
239 prev
= &((*prev
)->next_in_ino
);
244 JFFS2_WARNING("inode_cache/xattr_datum/xattr_ref"
245 " not found in remove_node_refs()!!\n");
249 jffs2_dbg(1, "Removed nodes in range 0x%08x-0x%08x from ino #%u\n",
250 jeb
->offset
, jeb
->offset
+ c
->sector_size
, ic
->ino
);
254 struct jffs2_raw_node_ref
*this;
255 printk(KERN_DEBUG
"After remove_node_refs_from_ino_list: \n");
261 pr_cont("0x%08x(%d)->",
262 ref_offset(this), ref_flags(this));
267 this = this->next_in_ino
;
273 #ifdef CONFIG_JFFS2_FS_XATTR
274 case RAWNODE_CLASS_XATTR_DATUM
:
275 jffs2_release_xattr_datum(c
, (struct jffs2_xattr_datum
*)ic
);
277 case RAWNODE_CLASS_XATTR_REF
:
278 jffs2_release_xattr_ref(c
, (struct jffs2_xattr_ref
*)ic
);
282 if (ic
->nodes
== (void *)ic
&& ic
->pino_nlink
== 0)
283 jffs2_del_ino_cache(c
, ic
);
287 void jffs2_free_jeb_node_refs(struct jffs2_sb_info
*c
, struct jffs2_eraseblock
*jeb
)
289 struct jffs2_raw_node_ref
*block
, *ref
;
290 jffs2_dbg(1, "Freeing all node refs for eraseblock offset 0x%08x\n",
293 block
= ref
= jeb
->first_node
;
296 if (ref
->flash_offset
== REF_LINK_NODE
) {
297 ref
= ref
->next_in_ino
;
298 jffs2_free_refblock(block
);
302 if (ref
->flash_offset
!= REF_EMPTY_NODE
&& ref
->next_in_ino
)
303 jffs2_remove_node_refs_from_ino_list(c
, ref
, jeb
);
304 /* else it was a non-inode node or already removed, so don't bother */
308 jeb
->first_node
= jeb
->last_node
= NULL
;
311 static int jffs2_block_check_erase(struct jffs2_sb_info
*c
, struct jffs2_eraseblock
*jeb
, uint32_t *bad_offset
)
317 unsigned long *wordebuf
;
319 ret
= mtd_point(c
->mtd
, jeb
->offset
, c
->sector_size
, &retlen
,
321 if (ret
!= -EOPNOTSUPP
) {
323 jffs2_dbg(1, "MTD point failed %d\n", ret
);
326 if (retlen
< c
->sector_size
) {
327 /* Don't muck about if it won't let us point to the whole erase sector */
328 jffs2_dbg(1, "MTD point returned len too short: 0x%zx\n",
330 mtd_unpoint(c
->mtd
, jeb
->offset
, retlen
);
333 wordebuf
= ebuf
-sizeof(*wordebuf
);
334 retlen
/= sizeof(*wordebuf
);
336 if (*++wordebuf
!= ~0)
339 mtd_unpoint(c
->mtd
, jeb
->offset
, c
->sector_size
);
341 *bad_offset
= jeb
->offset
+ c
->sector_size
- retlen
* sizeof(*wordebuf
);
342 pr_warn("Newly-erased block contained word 0x%lx at offset 0x%08x\n",
343 *wordebuf
, *bad_offset
);
349 ebuf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
351 pr_warn("Failed to allocate page buffer for verifying erase at 0x%08x. Refiling\n",
356 jffs2_dbg(1, "Verifying erase at 0x%08x\n", jeb
->offset
);
358 for (ofs
= jeb
->offset
; ofs
< jeb
->offset
+ c
->sector_size
; ) {
359 uint32_t readlen
= min((uint32_t)PAGE_SIZE
, jeb
->offset
+ c
->sector_size
- ofs
);
364 ret
= mtd_read(c
->mtd
, ofs
, readlen
, &retlen
, ebuf
);
366 pr_warn("Read of newly-erased block at 0x%08x failed: %d. Putting on bad_list\n",
371 if (retlen
!= readlen
) {
372 pr_warn("Short read from newly-erased block at 0x%08x. Wanted %d, got %zd\n",
373 ofs
, readlen
, retlen
);
377 for (i
=0; i
<readlen
; i
+= sizeof(unsigned long)) {
378 /* It's OK. We know it's properly aligned */
379 unsigned long *datum
= ebuf
+ i
;
382 pr_warn("Newly-erased block contained word 0x%lx at offset 0x%08x\n",
383 *datum
, *bad_offset
);
397 static void jffs2_mark_erased_block(struct jffs2_sb_info
*c
, struct jffs2_eraseblock
*jeb
)
403 switch (jffs2_block_check_erase(c
, jeb
, &bad_offset
)) {
404 case -EAGAIN
: goto refile
;
405 case -EIO
: goto filebad
;
408 /* Write the erase complete marker */
409 jffs2_dbg(1, "Writing erased marker to block at 0x%08x\n", jeb
->offset
);
410 bad_offset
= jeb
->offset
;
412 /* Cleanmarker in oob area or no cleanmarker at all ? */
413 if (jffs2_cleanmarker_oob(c
) || c
->cleanmarker_size
== 0) {
415 if (jffs2_cleanmarker_oob(c
)) {
416 if (jffs2_write_nand_cleanmarker(c
, jeb
))
422 struct jffs2_unknown_node marker
= {
423 .magic
= cpu_to_je16(JFFS2_MAGIC_BITMASK
),
424 .nodetype
= cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER
),
425 .totlen
= cpu_to_je32(c
->cleanmarker_size
)
428 jffs2_prealloc_raw_node_refs(c
, jeb
, 1);
430 marker
.hdr_crc
= cpu_to_je32(crc32(0, &marker
, sizeof(struct jffs2_unknown_node
)-4));
432 vecs
[0].iov_base
= (unsigned char *) &marker
;
433 vecs
[0].iov_len
= sizeof(marker
);
434 ret
= jffs2_flash_direct_writev(c
, vecs
, 1, jeb
->offset
, &retlen
);
436 if (ret
|| retlen
!= sizeof(marker
)) {
438 pr_warn("Write clean marker to block at 0x%08x failed: %d\n",
441 pr_warn("Short write to newly-erased block at 0x%08x: Wanted %zd, got %zd\n",
442 jeb
->offset
, sizeof(marker
), retlen
);
447 /* Everything else got zeroed before the erase */
448 jeb
->free_size
= c
->sector_size
;
450 mutex_lock(&c
->erase_free_sem
);
451 spin_lock(&c
->erase_completion_lock
);
453 c
->erasing_size
-= c
->sector_size
;
454 c
->free_size
+= c
->sector_size
;
456 /* Account for cleanmarker now, if it's in-band */
457 if (c
->cleanmarker_size
&& !jffs2_cleanmarker_oob(c
))
458 jffs2_link_node_ref(c
, jeb
, jeb
->offset
| REF_NORMAL
, c
->cleanmarker_size
, NULL
);
460 list_move_tail(&jeb
->list
, &c
->free_list
);
461 c
->nr_erasing_blocks
--;
464 jffs2_dbg_acct_sanity_check_nolock(c
, jeb
);
465 jffs2_dbg_acct_paranoia_check_nolock(c
, jeb
);
467 spin_unlock(&c
->erase_completion_lock
);
468 mutex_unlock(&c
->erase_free_sem
);
469 wake_up(&c
->erase_wait
);
473 jffs2_erase_failed(c
, jeb
, bad_offset
);
477 /* Stick it back on the list from whence it came and come back later */
478 mutex_lock(&c
->erase_free_sem
);
479 spin_lock(&c
->erase_completion_lock
);
480 jffs2_garbage_collect_trigger(c
);
481 list_move(&jeb
->list
, &c
->erase_complete_list
);
482 spin_unlock(&c
->erase_completion_lock
);
483 mutex_unlock(&c
->erase_free_sem
);