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 jffs2_dbg(1, "%s(): erase block %#08x (range %#08x-%#08x)\n",
51 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 mutex_lock(&c->erase_free_sem);
56 spin_lock(&c->erase_completion_lock);
57 list_move(&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);
62 mutex_unlock(&c->erase_free_sem);
66 memset(instr, 0, sizeof(*instr));
69 instr->addr = jeb->offset;
70 instr->len = c->sector_size;
71 instr->callback = jffs2_erase_callback;
72 instr->priv = (unsigned long)(&instr[1]);
73 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
75 ((struct erase_priv_struct *)instr->priv)->jeb = jeb;
76 ((struct erase_priv_struct *)instr->priv)->c = c;
78 ret = mtd_erase(c->mtd, instr);
82 bad_offset = instr->fail_addr;
86 if (ret == -ENOMEM || ret == -EAGAIN) {
87 /* Erase failed immediately. Refile it on the list */
88 jffs2_dbg(1, "Erase at 0x%08x failed: %d. Refiling on erase_pending_list\n",
90 mutex_lock(&c->erase_free_sem);
91 spin_lock(&c->erase_completion_lock);
92 list_move(&jeb->list, &c->erase_pending_list);
93 c->erasing_size -= c->sector_size;
94 c->dirty_size += c->sector_size;
95 jeb->dirty_size = c->sector_size;
96 spin_unlock(&c->erase_completion_lock);
97 mutex_unlock(&c->erase_free_sem);
102 printk(KERN_WARNING "Erase at 0x%08x failed immediately: -EROFS. Is the sector locked?\n", jeb->offset);
104 printk(KERN_WARNING "Erase at 0x%08x failed immediately: errno %d\n", jeb->offset, ret);
106 jffs2_erase_failed(c, jeb, bad_offset);
109 int jffs2_erase_pending_blocks(struct jffs2_sb_info *c, int count)
111 struct jffs2_eraseblock *jeb;
114 mutex_lock(&c->erase_free_sem);
116 spin_lock(&c->erase_completion_lock);
118 while (!list_empty(&c->erase_complete_list) ||
119 !list_empty(&c->erase_pending_list)) {
121 if (!list_empty(&c->erase_complete_list)) {
122 jeb = list_entry(c->erase_complete_list.next, struct jffs2_eraseblock, list);
123 list_move(&jeb->list, &c->erase_checking_list);
124 spin_unlock(&c->erase_completion_lock);
125 mutex_unlock(&c->erase_free_sem);
126 jffs2_mark_erased_block(c, jeb);
130 jffs2_dbg(1, "Count reached. jffs2_erase_pending_blocks leaving\n");
134 } else if (!list_empty(&c->erase_pending_list)) {
135 jeb = list_entry(c->erase_pending_list.next, struct jffs2_eraseblock, list);
136 jffs2_dbg(1, "Starting erase of pending block 0x%08x\n",
138 list_del(&jeb->list);
139 c->erasing_size += c->sector_size;
140 c->wasted_size -= jeb->wasted_size;
141 c->free_size -= jeb->free_size;
142 c->used_size -= jeb->used_size;
143 c->dirty_size -= jeb->dirty_size;
144 jeb->wasted_size = jeb->used_size = jeb->dirty_size = jeb->free_size = 0;
145 jffs2_free_jeb_node_refs(c, jeb);
146 list_add(&jeb->list, &c->erasing_list);
147 spin_unlock(&c->erase_completion_lock);
148 mutex_unlock(&c->erase_free_sem);
150 jffs2_erase_block(c, jeb);
158 mutex_lock(&c->erase_free_sem);
159 spin_lock(&c->erase_completion_lock);
162 spin_unlock(&c->erase_completion_lock);
163 mutex_unlock(&c->erase_free_sem);
165 jffs2_dbg(1, "jffs2_erase_pending_blocks completed\n");
169 static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
171 jffs2_dbg(1, "Erase completed successfully at 0x%08x\n", jeb->offset);
172 mutex_lock(&c->erase_free_sem);
173 spin_lock(&c->erase_completion_lock);
174 list_move_tail(&jeb->list, &c->erase_complete_list);
175 /* Wake the GC thread to mark them clean */
176 jffs2_garbage_collect_trigger(c);
177 spin_unlock(&c->erase_completion_lock);
178 mutex_unlock(&c->erase_free_sem);
179 wake_up(&c->erase_wait);
182 static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset)
184 /* For NAND, if the failure did not occur at the device level for a
185 specific physical page, don't bother updating the bad block table. */
186 if (jffs2_cleanmarker_oob(c) && (bad_offset != (uint32_t)MTD_FAIL_ADDR_UNKNOWN)) {
187 /* We had a device-level failure to erase. Let's see if we've
188 failed too many times. */
189 if (!jffs2_write_nand_badblock(c, jeb, bad_offset)) {
190 /* We'd like to give this block another try. */
191 mutex_lock(&c->erase_free_sem);
192 spin_lock(&c->erase_completion_lock);
193 list_move(&jeb->list, &c->erase_pending_list);
194 c->erasing_size -= c->sector_size;
195 c->dirty_size += c->sector_size;
196 jeb->dirty_size = c->sector_size;
197 spin_unlock(&c->erase_completion_lock);
198 mutex_unlock(&c->erase_free_sem);
203 mutex_lock(&c->erase_free_sem);
204 spin_lock(&c->erase_completion_lock);
205 c->erasing_size -= c->sector_size;
206 c->bad_size += c->sector_size;
207 list_move(&jeb->list, &c->bad_list);
208 c->nr_erasing_blocks--;
209 spin_unlock(&c->erase_completion_lock);
210 mutex_unlock(&c->erase_free_sem);
211 wake_up(&c->erase_wait);
215 static void jffs2_erase_callback(struct erase_info *instr)
217 struct erase_priv_struct *priv = (void *)instr->priv;
219 if(instr->state != MTD_ERASE_DONE) {
220 printk(KERN_WARNING "Erase at 0x%08llx finished, but state != MTD_ERASE_DONE. State is 0x%x instead.\n",
221 (unsigned long long)instr->addr, instr->state);
222 jffs2_erase_failed(priv->c, priv->jeb, instr->fail_addr);
224 jffs2_erase_succeeded(priv->c, priv->jeb);
230 /* Hmmm. Maybe we should accept the extra space it takes and make
231 this a standard doubly-linked list? */
232 static inline void jffs2_remove_node_refs_from_ino_list(struct jffs2_sb_info *c,
233 struct jffs2_raw_node_ref *ref, struct jffs2_eraseblock *jeb)
235 struct jffs2_inode_cache *ic = NULL;
236 struct jffs2_raw_node_ref **prev;
238 prev = &ref->next_in_ino;
240 /* Walk the inode's list once, removing any nodes from this eraseblock */
242 if (!(*prev)->next_in_ino) {
243 /* We're looking at the jffs2_inode_cache, which is
244 at the end of the linked list. Stash it and continue
245 from the beginning of the list */
246 ic = (struct jffs2_inode_cache *)(*prev);
251 if (SECTOR_ADDR((*prev)->flash_offset) == jeb->offset) {
252 /* It's in the block we're erasing */
253 struct jffs2_raw_node_ref *this;
256 *prev = this->next_in_ino;
257 this->next_in_ino = NULL;
264 /* Not to be deleted. Skip */
265 prev = &((*prev)->next_in_ino);
270 JFFS2_WARNING("inode_cache/xattr_datum/xattr_ref"
271 " not found in remove_node_refs()!!\n");
275 jffs2_dbg(1, "Removed nodes in range 0x%08x-0x%08x from ino #%u\n",
276 jeb->offset, jeb->offset + c->sector_size, ic->ino);
280 struct jffs2_raw_node_ref *this;
281 printk(KERN_DEBUG "After remove_node_refs_from_ino_list: \n");
287 printk(KERN_CONT "0x%08x(%d)->",
288 ref_offset(this), ref_flags(this));
293 this = this->next_in_ino;
295 printk(KERN_CONT "\n");
299 #ifdef CONFIG_JFFS2_FS_XATTR
300 case RAWNODE_CLASS_XATTR_DATUM:
301 jffs2_release_xattr_datum(c, (struct jffs2_xattr_datum *)ic);
303 case RAWNODE_CLASS_XATTR_REF:
304 jffs2_release_xattr_ref(c, (struct jffs2_xattr_ref *)ic);
308 if (ic->nodes == (void *)ic && ic->pino_nlink == 0)
309 jffs2_del_ino_cache(c, ic);
313 void jffs2_free_jeb_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
315 struct jffs2_raw_node_ref *block, *ref;
316 jffs2_dbg(1, "Freeing all node refs for eraseblock offset 0x%08x\n",
319 block = ref = jeb->first_node;
322 if (ref->flash_offset == REF_LINK_NODE) {
323 ref = ref->next_in_ino;
324 jffs2_free_refblock(block);
328 if (ref->flash_offset != REF_EMPTY_NODE && ref->next_in_ino)
329 jffs2_remove_node_refs_from_ino_list(c, ref, jeb);
330 /* else it was a non-inode node or already removed, so don't bother */
334 jeb->first_node = jeb->last_node = NULL;
337 static int jffs2_block_check_erase(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t *bad_offset)
343 unsigned long *wordebuf;
345 ret = mtd_point(c->mtd, jeb->offset, c->sector_size, &retlen,
347 if (ret != -EOPNOTSUPP) {
349 jffs2_dbg(1, "MTD point failed %d\n", ret);
352 if (retlen < c->sector_size) {
353 /* Don't muck about if it won't let us point to the whole erase sector */
354 jffs2_dbg(1, "MTD point returned len too short: 0x%zx\n",
356 mtd_unpoint(c->mtd, jeb->offset, retlen);
359 wordebuf = ebuf-sizeof(*wordebuf);
360 retlen /= sizeof(*wordebuf);
362 if (*++wordebuf != ~0)
365 mtd_unpoint(c->mtd, jeb->offset, c->sector_size);
367 printk(KERN_WARNING "Newly-erased block contained word 0x%lx at offset 0x%08tx\n",
368 *wordebuf, jeb->offset + c->sector_size-retlen*sizeof(*wordebuf));
374 ebuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
376 printk(KERN_WARNING "Failed to allocate page buffer for verifying erase at 0x%08x. Refiling\n", jeb->offset);
380 jffs2_dbg(1, "Verifying erase at 0x%08x\n", jeb->offset);
382 for (ofs = jeb->offset; ofs < jeb->offset + c->sector_size; ) {
383 uint32_t readlen = min((uint32_t)PAGE_SIZE, jeb->offset + c->sector_size - ofs);
388 ret = mtd_read(c->mtd, ofs, readlen, &retlen, ebuf);
390 printk(KERN_WARNING "Read of newly-erased block at 0x%08x failed: %d. Putting on bad_list\n", ofs, ret);
394 if (retlen != readlen) {
395 printk(KERN_WARNING "Short read from newly-erased block at 0x%08x. Wanted %d, got %zd\n", ofs, readlen, retlen);
399 for (i=0; i<readlen; i += sizeof(unsigned long)) {
400 /* It's OK. We know it's properly aligned */
401 unsigned long *datum = ebuf + i;
404 printk(KERN_WARNING "Newly-erased block contained word 0x%lx at offset 0x%08x\n", *datum, *bad_offset);
418 static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
422 uint32_t uninitialized_var(bad_offset);
424 switch (jffs2_block_check_erase(c, jeb, &bad_offset)) {
425 case -EAGAIN: goto refile;
426 case -EIO: goto filebad;
429 /* Write the erase complete marker */
430 jffs2_dbg(1, "Writing erased marker to block at 0x%08x\n", jeb->offset);
431 bad_offset = jeb->offset;
433 /* Cleanmarker in oob area or no cleanmarker at all ? */
434 if (jffs2_cleanmarker_oob(c) || c->cleanmarker_size == 0) {
436 if (jffs2_cleanmarker_oob(c)) {
437 if (jffs2_write_nand_cleanmarker(c, jeb))
443 struct jffs2_unknown_node marker = {
444 .magic = cpu_to_je16(JFFS2_MAGIC_BITMASK),
445 .nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER),
446 .totlen = cpu_to_je32(c->cleanmarker_size)
449 jffs2_prealloc_raw_node_refs(c, jeb, 1);
451 marker.hdr_crc = cpu_to_je32(crc32(0, &marker, sizeof(struct jffs2_unknown_node)-4));
453 vecs[0].iov_base = (unsigned char *) ▮
454 vecs[0].iov_len = sizeof(marker);
455 ret = jffs2_flash_direct_writev(c, vecs, 1, jeb->offset, &retlen);
457 if (ret || retlen != sizeof(marker)) {
459 printk(KERN_WARNING "Write clean marker to block at 0x%08x failed: %d\n",
462 printk(KERN_WARNING "Short write to newly-erased block at 0x%08x: Wanted %zd, got %zd\n",
463 jeb->offset, sizeof(marker), retlen);
468 /* Everything else got zeroed before the erase */
469 jeb->free_size = c->sector_size;
471 mutex_lock(&c->erase_free_sem);
472 spin_lock(&c->erase_completion_lock);
474 c->erasing_size -= c->sector_size;
475 c->free_size += c->sector_size;
477 /* Account for cleanmarker now, if it's in-band */
478 if (c->cleanmarker_size && !jffs2_cleanmarker_oob(c))
479 jffs2_link_node_ref(c, jeb, jeb->offset | REF_NORMAL, c->cleanmarker_size, NULL);
481 list_move_tail(&jeb->list, &c->free_list);
482 c->nr_erasing_blocks--;
485 jffs2_dbg_acct_sanity_check_nolock(c, jeb);
486 jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
488 spin_unlock(&c->erase_completion_lock);
489 mutex_unlock(&c->erase_free_sem);
490 wake_up(&c->erase_wait);
494 jffs2_erase_failed(c, jeb, bad_offset);
498 /* Stick it back on the list from whence it came and come back later */
499 mutex_lock(&c->erase_free_sem);
500 spin_lock(&c->erase_completion_lock);
501 jffs2_garbage_collect_trigger(c);
502 list_move(&jeb->list, &c->erase_complete_list);
503 spin_unlock(&c->erase_completion_lock);
504 mutex_unlock(&c->erase_free_sem);