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 struct erase_priv_struct {
25 struct jffs2_eraseblock *jeb;
26 struct jffs2_sb_info *c;
30 static void jffs2_erase_callback(struct erase_info *);
32 static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset);
33 static void jffs2_erase_succeeded(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 jffs2_dbg(1, "%s(): erase block %#08x (range %#08x-%#08x)\n",
53 jeb->offset, jeb->offset, jeb->offset + c->sector_size);
54 instr = kmalloc(sizeof(struct erase_info) + sizeof(struct erase_priv_struct), GFP_KERNEL);
56 pr_warn("kmalloc for struct erase_info in jffs2_erase_block failed. Refiling block for later\n");
57 mutex_lock(&c->erase_free_sem);
58 spin_lock(&c->erase_completion_lock);
59 list_move(&jeb->list, &c->erase_pending_list);
60 c->erasing_size -= c->sector_size;
61 c->dirty_size += c->sector_size;
62 jeb->dirty_size = c->sector_size;
63 spin_unlock(&c->erase_completion_lock);
64 mutex_unlock(&c->erase_free_sem);
68 memset(instr, 0, sizeof(*instr));
71 instr->addr = jeb->offset;
72 instr->len = c->sector_size;
73 instr->callback = jffs2_erase_callback;
74 instr->priv = (unsigned long)(&instr[1]);
75 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
77 ((struct erase_priv_struct *)instr->priv)->jeb = jeb;
78 ((struct erase_priv_struct *)instr->priv)->c = c;
80 ret = mtd_erase(c->mtd, instr);
84 bad_offset = instr->fail_addr;
88 if (ret == -ENOMEM || ret == -EAGAIN) {
89 /* Erase failed immediately. Refile it on the list */
90 jffs2_dbg(1, "Erase at 0x%08x failed: %d. Refiling on erase_pending_list\n",
92 mutex_lock(&c->erase_free_sem);
93 spin_lock(&c->erase_completion_lock);
94 list_move(&jeb->list, &c->erase_pending_list);
95 c->erasing_size -= c->sector_size;
96 c->dirty_size += c->sector_size;
97 jeb->dirty_size = c->sector_size;
98 spin_unlock(&c->erase_completion_lock);
99 mutex_unlock(&c->erase_free_sem);
104 pr_warn("Erase at 0x%08x failed immediately: -EROFS. Is the sector locked?\n",
107 pr_warn("Erase at 0x%08x failed immediately: errno %d\n",
110 jffs2_erase_failed(c, jeb, bad_offset);
113 int jffs2_erase_pending_blocks(struct jffs2_sb_info *c, int count)
115 struct jffs2_eraseblock *jeb;
118 mutex_lock(&c->erase_free_sem);
120 spin_lock(&c->erase_completion_lock);
122 while (!list_empty(&c->erase_complete_list) ||
123 !list_empty(&c->erase_pending_list)) {
125 if (!list_empty(&c->erase_complete_list)) {
126 jeb = list_entry(c->erase_complete_list.next, struct jffs2_eraseblock, list);
127 list_move(&jeb->list, &c->erase_checking_list);
128 spin_unlock(&c->erase_completion_lock);
129 mutex_unlock(&c->erase_free_sem);
130 jffs2_mark_erased_block(c, jeb);
134 jffs2_dbg(1, "Count reached. jffs2_erase_pending_blocks leaving\n");
138 } else if (!list_empty(&c->erase_pending_list)) {
139 jeb = list_entry(c->erase_pending_list.next, struct jffs2_eraseblock, list);
140 jffs2_dbg(1, "Starting erase of pending block 0x%08x\n",
142 list_del(&jeb->list);
143 c->erasing_size += c->sector_size;
144 c->wasted_size -= jeb->wasted_size;
145 c->free_size -= jeb->free_size;
146 c->used_size -= jeb->used_size;
147 c->dirty_size -= jeb->dirty_size;
148 jeb->wasted_size = jeb->used_size = jeb->dirty_size = jeb->free_size = 0;
149 jffs2_free_jeb_node_refs(c, jeb);
150 list_add(&jeb->list, &c->erasing_list);
151 spin_unlock(&c->erase_completion_lock);
152 mutex_unlock(&c->erase_free_sem);
154 jffs2_erase_block(c, jeb);
162 mutex_lock(&c->erase_free_sem);
163 spin_lock(&c->erase_completion_lock);
166 spin_unlock(&c->erase_completion_lock);
167 mutex_unlock(&c->erase_free_sem);
169 jffs2_dbg(1, "jffs2_erase_pending_blocks completed\n");
173 static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
175 jffs2_dbg(1, "Erase completed successfully at 0x%08x\n", jeb->offset);
176 mutex_lock(&c->erase_free_sem);
177 spin_lock(&c->erase_completion_lock);
178 list_move_tail(&jeb->list, &c->erase_complete_list);
179 /* Wake the GC thread to mark them clean */
180 jffs2_garbage_collect_trigger(c);
181 spin_unlock(&c->erase_completion_lock);
182 mutex_unlock(&c->erase_free_sem);
183 wake_up(&c->erase_wait);
186 static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset)
188 /* For NAND, if the failure did not occur at the device level for a
189 specific physical page, don't bother updating the bad block table. */
190 if (jffs2_cleanmarker_oob(c) && (bad_offset != (uint32_t)MTD_FAIL_ADDR_UNKNOWN)) {
191 /* We had a device-level failure to erase. Let's see if we've
192 failed too many times. */
193 if (!jffs2_write_nand_badblock(c, jeb, bad_offset)) {
194 /* We'd like to give this block another try. */
195 mutex_lock(&c->erase_free_sem);
196 spin_lock(&c->erase_completion_lock);
197 list_move(&jeb->list, &c->erase_pending_list);
198 c->erasing_size -= c->sector_size;
199 c->dirty_size += c->sector_size;
200 jeb->dirty_size = c->sector_size;
201 spin_unlock(&c->erase_completion_lock);
202 mutex_unlock(&c->erase_free_sem);
207 mutex_lock(&c->erase_free_sem);
208 spin_lock(&c->erase_completion_lock);
209 c->erasing_size -= c->sector_size;
210 c->bad_size += c->sector_size;
211 list_move(&jeb->list, &c->bad_list);
212 c->nr_erasing_blocks--;
213 spin_unlock(&c->erase_completion_lock);
214 mutex_unlock(&c->erase_free_sem);
215 wake_up(&c->erase_wait);
219 static void jffs2_erase_callback(struct erase_info *instr)
221 struct erase_priv_struct *priv = (void *)instr->priv;
223 if(instr->state != MTD_ERASE_DONE) {
224 pr_warn("Erase at 0x%08llx finished, but state != MTD_ERASE_DONE. State is 0x%x instead.\n",
225 (unsigned long long)instr->addr, instr->state);
226 jffs2_erase_failed(priv->c, priv->jeb, instr->fail_addr);
228 jffs2_erase_succeeded(priv->c, priv->jeb);
234 /* Hmmm. Maybe we should accept the extra space it takes and make
235 this a standard doubly-linked list? */
236 static inline void jffs2_remove_node_refs_from_ino_list(struct jffs2_sb_info *c,
237 struct jffs2_raw_node_ref *ref, struct jffs2_eraseblock *jeb)
239 struct jffs2_inode_cache *ic = NULL;
240 struct jffs2_raw_node_ref **prev;
242 prev = &ref->next_in_ino;
244 /* Walk the inode's list once, removing any nodes from this eraseblock */
246 if (!(*prev)->next_in_ino) {
247 /* We're looking at the jffs2_inode_cache, which is
248 at the end of the linked list. Stash it and continue
249 from the beginning of the list */
250 ic = (struct jffs2_inode_cache *)(*prev);
255 if (SECTOR_ADDR((*prev)->flash_offset) == jeb->offset) {
256 /* It's in the block we're erasing */
257 struct jffs2_raw_node_ref *this;
260 *prev = this->next_in_ino;
261 this->next_in_ino = NULL;
268 /* Not to be deleted. Skip */
269 prev = &((*prev)->next_in_ino);
274 JFFS2_WARNING("inode_cache/xattr_datum/xattr_ref"
275 " not found in remove_node_refs()!!\n");
279 jffs2_dbg(1, "Removed nodes in range 0x%08x-0x%08x from ino #%u\n",
280 jeb->offset, jeb->offset + c->sector_size, ic->ino);
284 struct jffs2_raw_node_ref *this;
285 printk(KERN_DEBUG "After remove_node_refs_from_ino_list: \n");
291 pr_cont("0x%08x(%d)->",
292 ref_offset(this), ref_flags(this));
297 this = this->next_in_ino;
303 #ifdef CONFIG_JFFS2_FS_XATTR
304 case RAWNODE_CLASS_XATTR_DATUM:
305 jffs2_release_xattr_datum(c, (struct jffs2_xattr_datum *)ic);
307 case RAWNODE_CLASS_XATTR_REF:
308 jffs2_release_xattr_ref(c, (struct jffs2_xattr_ref *)ic);
312 if (ic->nodes == (void *)ic && ic->pino_nlink == 0)
313 jffs2_del_ino_cache(c, ic);
317 void jffs2_free_jeb_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
319 struct jffs2_raw_node_ref *block, *ref;
320 jffs2_dbg(1, "Freeing all node refs for eraseblock offset 0x%08x\n",
323 block = ref = jeb->first_node;
326 if (ref->flash_offset == REF_LINK_NODE) {
327 ref = ref->next_in_ino;
328 jffs2_free_refblock(block);
332 if (ref->flash_offset != REF_EMPTY_NODE && ref->next_in_ino)
333 jffs2_remove_node_refs_from_ino_list(c, ref, jeb);
334 /* else it was a non-inode node or already removed, so don't bother */
338 jeb->first_node = jeb->last_node = NULL;
341 static int jffs2_block_check_erase(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t *bad_offset)
347 unsigned long *wordebuf;
349 ret = mtd_point(c->mtd, jeb->offset, c->sector_size, &retlen,
351 if (ret != -EOPNOTSUPP) {
353 jffs2_dbg(1, "MTD point failed %d\n", ret);
356 if (retlen < c->sector_size) {
357 /* Don't muck about if it won't let us point to the whole erase sector */
358 jffs2_dbg(1, "MTD point returned len too short: 0x%zx\n",
360 mtd_unpoint(c->mtd, jeb->offset, retlen);
363 wordebuf = ebuf-sizeof(*wordebuf);
364 retlen /= sizeof(*wordebuf);
366 if (*++wordebuf != ~0)
369 mtd_unpoint(c->mtd, jeb->offset, c->sector_size);
371 pr_warn("Newly-erased block contained word 0x%lx at offset 0x%08tx\n",
374 c->sector_size-retlen * sizeof(*wordebuf));
380 ebuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
382 pr_warn("Failed to allocate page buffer for verifying erase at 0x%08x. Refiling\n",
387 jffs2_dbg(1, "Verifying erase at 0x%08x\n", jeb->offset);
389 for (ofs = jeb->offset; ofs < jeb->offset + c->sector_size; ) {
390 uint32_t readlen = min((uint32_t)PAGE_SIZE, jeb->offset + c->sector_size - ofs);
395 ret = mtd_read(c->mtd, ofs, readlen, &retlen, ebuf);
397 pr_warn("Read of newly-erased block at 0x%08x failed: %d. Putting on bad_list\n",
402 if (retlen != readlen) {
403 pr_warn("Short read from newly-erased block at 0x%08x. Wanted %d, got %zd\n",
404 ofs, readlen, retlen);
408 for (i=0; i<readlen; i += sizeof(unsigned long)) {
409 /* It's OK. We know it's properly aligned */
410 unsigned long *datum = ebuf + i;
413 pr_warn("Newly-erased block contained word 0x%lx at offset 0x%08x\n",
414 *datum, *bad_offset);
428 static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
432 uint32_t uninitialized_var(bad_offset);
434 switch (jffs2_block_check_erase(c, jeb, &bad_offset)) {
435 case -EAGAIN: goto refile;
436 case -EIO: goto filebad;
439 /* Write the erase complete marker */
440 jffs2_dbg(1, "Writing erased marker to block at 0x%08x\n", jeb->offset);
441 bad_offset = jeb->offset;
443 /* Cleanmarker in oob area or no cleanmarker at all ? */
444 if (jffs2_cleanmarker_oob(c) || c->cleanmarker_size == 0) {
446 if (jffs2_cleanmarker_oob(c)) {
447 if (jffs2_write_nand_cleanmarker(c, jeb))
453 struct jffs2_unknown_node marker = {
454 .magic = cpu_to_je16(JFFS2_MAGIC_BITMASK),
455 .nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER),
456 .totlen = cpu_to_je32(c->cleanmarker_size)
459 jffs2_prealloc_raw_node_refs(c, jeb, 1);
461 marker.hdr_crc = cpu_to_je32(crc32(0, &marker, sizeof(struct jffs2_unknown_node)-4));
463 vecs[0].iov_base = (unsigned char *) ▮
464 vecs[0].iov_len = sizeof(marker);
465 ret = jffs2_flash_direct_writev(c, vecs, 1, jeb->offset, &retlen);
467 if (ret || retlen != sizeof(marker)) {
469 pr_warn("Write clean marker to block at 0x%08x failed: %d\n",
472 pr_warn("Short write to newly-erased block at 0x%08x: Wanted %zd, got %zd\n",
473 jeb->offset, sizeof(marker), retlen);
478 /* Everything else got zeroed before the erase */
479 jeb->free_size = c->sector_size;
481 mutex_lock(&c->erase_free_sem);
482 spin_lock(&c->erase_completion_lock);
484 c->erasing_size -= c->sector_size;
485 c->free_size += c->sector_size;
487 /* Account for cleanmarker now, if it's in-band */
488 if (c->cleanmarker_size && !jffs2_cleanmarker_oob(c))
489 jffs2_link_node_ref(c, jeb, jeb->offset | REF_NORMAL, c->cleanmarker_size, NULL);
491 list_move_tail(&jeb->list, &c->free_list);
492 c->nr_erasing_blocks--;
495 jffs2_dbg_acct_sanity_check_nolock(c, jeb);
496 jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
498 spin_unlock(&c->erase_completion_lock);
499 mutex_unlock(&c->erase_free_sem);
500 wake_up(&c->erase_wait);
504 jffs2_erase_failed(c, jeb, bad_offset);
508 /* Stick it back on the list from whence it came and come back later */
509 mutex_lock(&c->erase_free_sem);
510 spin_lock(&c->erase_completion_lock);
511 jffs2_garbage_collect_trigger(c);
512 list_move(&jeb->list, &c->erase_complete_list);
513 spin_unlock(&c->erase_completion_lock);
514 mutex_unlock(&c->erase_free_sem);