Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * JFFS2 -- Journalling Flash File System, Version 2. |
| 3 | * |
David Woodhouse | c00c310 | 2007-04-25 14:16:47 +0100 | [diff] [blame] | 4 | * Copyright © 2001-2007 Red Hat, Inc. |
David Woodhouse | 6088c05 | 2010-08-08 14:15:22 +0100 | [diff] [blame] | 5 | * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * Created by David Woodhouse <dwmw2@infradead.org> |
| 8 | * |
| 9 | * For licensing information, see the file 'LICENCE' in this directory. |
| 10 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | */ |
| 12 | |
Joe Perches | 5a52895 | 2012-02-15 15:56:45 -0800 | [diff] [blame] | 13 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 14 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
| 22 | #include "nodelist.h" |
| 23 | |
| 24 | struct erase_priv_struct { |
| 25 | struct jffs2_eraseblock *jeb; |
| 26 | struct jffs2_sb_info *c; |
| 27 | }; |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #ifndef __ECOS |
| 30 | static void jffs2_erase_callback(struct erase_info *); |
| 31 | #endif |
| 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb); |
| 35 | |
| 36 | static void jffs2_erase_block(struct jffs2_sb_info *c, |
| 37 | struct jffs2_eraseblock *jeb) |
| 38 | { |
| 39 | int ret; |
| 40 | uint32_t bad_offset; |
| 41 | #ifdef __ECOS |
| 42 | ret = jffs2_flash_erase(c, jeb); |
| 43 | if (!ret) { |
David Woodhouse | ef53cb0 | 2007-07-10 10:01:22 +0100 | [diff] [blame] | 44 | jffs2_erase_succeeded(c, jeb); |
| 45 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | } |
| 47 | bad_offset = jeb->offset; |
| 48 | #else /* Linux */ |
| 49 | struct erase_info *instr; |
| 50 | |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame] | 51 | jffs2_dbg(1, "%s(): erase block %#08x (range %#08x-%#08x)\n", |
| 52 | __func__, |
| 53 | jeb->offset, jeb->offset, jeb->offset + c->sector_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | instr = kmalloc(sizeof(struct erase_info) + sizeof(struct erase_priv_struct), GFP_KERNEL); |
| 55 | if (!instr) { |
Joe Perches | da320f0 | 2012-02-15 15:56:44 -0800 | [diff] [blame] | 56 | pr_warn("kmalloc for struct erase_info in jffs2_erase_block failed. Refiling block for later\n"); |
David Woodhouse | ced2207 | 2008-04-22 15:13:40 +0100 | [diff] [blame] | 57 | mutex_lock(&c->erase_free_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | spin_lock(&c->erase_completion_lock); |
Akinobu Mita | f116629 | 2006-06-26 00:24:46 -0700 | [diff] [blame] | 59 | list_move(&jeb->list, &c->erase_pending_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
David Woodhouse | ced2207 | 2008-04-22 15:13:40 +0100 | [diff] [blame] | 64 | mutex_unlock(&c->erase_free_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | return; |
| 66 | } |
| 67 | |
| 68 | memset(instr, 0, sizeof(*instr)); |
| 69 | |
| 70 | instr->mtd = c->mtd; |
| 71 | instr->addr = jeb->offset; |
| 72 | instr->len = c->sector_size; |
| 73 | instr->callback = jffs2_erase_callback; |
| 74 | instr->priv = (unsigned long)(&instr[1]); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 75 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | ((struct erase_priv_struct *)instr->priv)->jeb = jeb; |
| 77 | ((struct erase_priv_struct *)instr->priv)->c = c; |
| 78 | |
Artem Bityutskiy | 7e1f0dc | 2011-12-23 15:25:39 +0200 | [diff] [blame] | 79 | ret = mtd_erase(c->mtd, instr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | if (!ret) |
| 81 | return; |
| 82 | |
| 83 | bad_offset = instr->fail_addr; |
| 84 | kfree(instr); |
| 85 | #endif /* __ECOS */ |
| 86 | |
| 87 | if (ret == -ENOMEM || ret == -EAGAIN) { |
| 88 | /* Erase failed immediately. Refile it on the list */ |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame] | 89 | jffs2_dbg(1, "Erase at 0x%08x failed: %d. Refiling on erase_pending_list\n", |
| 90 | jeb->offset, ret); |
David Woodhouse | ced2207 | 2008-04-22 15:13:40 +0100 | [diff] [blame] | 91 | mutex_lock(&c->erase_free_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | spin_lock(&c->erase_completion_lock); |
Akinobu Mita | f116629 | 2006-06-26 00:24:46 -0700 | [diff] [blame] | 93 | list_move(&jeb->list, &c->erase_pending_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | c->erasing_size -= c->sector_size; |
| 95 | c->dirty_size += c->sector_size; |
| 96 | jeb->dirty_size = c->sector_size; |
| 97 | spin_unlock(&c->erase_completion_lock); |
David Woodhouse | ced2207 | 2008-04-22 15:13:40 +0100 | [diff] [blame] | 98 | mutex_unlock(&c->erase_free_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | return; |
| 100 | } |
| 101 | |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 102 | if (ret == -EROFS) |
Joe Perches | da320f0 | 2012-02-15 15:56:44 -0800 | [diff] [blame] | 103 | pr_warn("Erase at 0x%08x failed immediately: -EROFS. Is the sector locked?\n", |
| 104 | jeb->offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | else |
Joe Perches | da320f0 | 2012-02-15 15:56:44 -0800 | [diff] [blame] | 106 | pr_warn("Erase at 0x%08x failed immediately: errno %d\n", |
| 107 | jeb->offset, ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
| 109 | jffs2_erase_failed(c, jeb, bad_offset); |
| 110 | } |
| 111 | |
Joakim Tjernlund | 9957abe | 2010-05-19 16:32:52 +0100 | [diff] [blame] | 112 | int jffs2_erase_pending_blocks(struct jffs2_sb_info *c, int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | { |
| 114 | struct jffs2_eraseblock *jeb; |
Joakim Tjernlund | 9957abe | 2010-05-19 16:32:52 +0100 | [diff] [blame] | 115 | int work_done = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
David Woodhouse | ced2207 | 2008-04-22 15:13:40 +0100 | [diff] [blame] | 117 | mutex_lock(&c->erase_free_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
| 119 | spin_lock(&c->erase_completion_lock); |
| 120 | |
| 121 | while (!list_empty(&c->erase_complete_list) || |
| 122 | !list_empty(&c->erase_pending_list)) { |
| 123 | |
| 124 | if (!list_empty(&c->erase_complete_list)) { |
| 125 | jeb = list_entry(c->erase_complete_list.next, struct jffs2_eraseblock, list); |
David Woodhouse | e2bc322 | 2008-04-23 14:15:24 +0100 | [diff] [blame] | 126 | list_move(&jeb->list, &c->erase_checking_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | spin_unlock(&c->erase_completion_lock); |
David Woodhouse | ced2207 | 2008-04-22 15:13:40 +0100 | [diff] [blame] | 128 | mutex_unlock(&c->erase_free_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | jffs2_mark_erased_block(c, jeb); |
| 130 | |
Joakim Tjernlund | 9957abe | 2010-05-19 16:32:52 +0100 | [diff] [blame] | 131 | work_done++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | if (!--count) { |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame] | 133 | jffs2_dbg(1, "Count reached. jffs2_erase_pending_blocks leaving\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | goto done; |
| 135 | } |
| 136 | |
| 137 | } else if (!list_empty(&c->erase_pending_list)) { |
| 138 | jeb = list_entry(c->erase_pending_list.next, struct jffs2_eraseblock, list); |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame] | 139 | jffs2_dbg(1, "Starting erase of pending block 0x%08x\n", |
| 140 | jeb->offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | list_del(&jeb->list); |
| 142 | c->erasing_size += c->sector_size; |
| 143 | c->wasted_size -= jeb->wasted_size; |
| 144 | c->free_size -= jeb->free_size; |
| 145 | c->used_size -= jeb->used_size; |
| 146 | c->dirty_size -= jeb->dirty_size; |
| 147 | jeb->wasted_size = jeb->used_size = jeb->dirty_size = jeb->free_size = 0; |
David Woodhouse | c38c1b6 | 2006-05-25 01:38:27 +0100 | [diff] [blame] | 148 | jffs2_free_jeb_node_refs(c, jeb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | list_add(&jeb->list, &c->erasing_list); |
| 150 | spin_unlock(&c->erase_completion_lock); |
David Woodhouse | ced2207 | 2008-04-22 15:13:40 +0100 | [diff] [blame] | 151 | mutex_unlock(&c->erase_free_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
| 153 | jffs2_erase_block(c, jeb); |
| 154 | |
| 155 | } else { |
| 156 | BUG(); |
| 157 | } |
| 158 | |
| 159 | /* Be nice */ |
Wolfram Sang | 3866f67 | 2010-09-01 18:03:41 +0200 | [diff] [blame] | 160 | cond_resched(); |
David Woodhouse | ced2207 | 2008-04-22 15:13:40 +0100 | [diff] [blame] | 161 | mutex_lock(&c->erase_free_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | spin_lock(&c->erase_completion_lock); |
| 163 | } |
| 164 | |
| 165 | spin_unlock(&c->erase_completion_lock); |
David Woodhouse | ced2207 | 2008-04-22 15:13:40 +0100 | [diff] [blame] | 166 | mutex_unlock(&c->erase_free_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | done: |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame] | 168 | jffs2_dbg(1, "jffs2_erase_pending_blocks completed\n"); |
Joakim Tjernlund | 9957abe | 2010-05-19 16:32:52 +0100 | [diff] [blame] | 169 | return work_done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb) |
| 173 | { |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame] | 174 | jffs2_dbg(1, "Erase completed successfully at 0x%08x\n", jeb->offset); |
David Woodhouse | ced2207 | 2008-04-22 15:13:40 +0100 | [diff] [blame] | 175 | mutex_lock(&c->erase_free_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | spin_lock(&c->erase_completion_lock); |
Akinobu Mita | f116629 | 2006-06-26 00:24:46 -0700 | [diff] [blame] | 177 | list_move_tail(&jeb->list, &c->erase_complete_list); |
David Woodhouse | ae3b6ba | 2010-05-19 17:05:14 +0100 | [diff] [blame] | 178 | /* Wake the GC thread to mark them clean */ |
| 179 | jffs2_garbage_collect_trigger(c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | spin_unlock(&c->erase_completion_lock); |
David Woodhouse | ced2207 | 2008-04-22 15:13:40 +0100 | [diff] [blame] | 181 | mutex_unlock(&c->erase_free_sem); |
David Woodhouse | 0717bf8 | 2010-05-19 16:37:13 +0100 | [diff] [blame] | 182 | wake_up(&c->erase_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset) |
| 186 | { |
| 187 | /* For NAND, if the failure did not occur at the device level for a |
| 188 | specific physical page, don't bother updating the bad block table. */ |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 189 | if (jffs2_cleanmarker_oob(c) && (bad_offset != (uint32_t)MTD_FAIL_ADDR_UNKNOWN)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | /* We had a device-level failure to erase. Let's see if we've |
| 191 | failed too many times. */ |
| 192 | if (!jffs2_write_nand_badblock(c, jeb, bad_offset)) { |
| 193 | /* We'd like to give this block another try. */ |
David Woodhouse | ced2207 | 2008-04-22 15:13:40 +0100 | [diff] [blame] | 194 | mutex_lock(&c->erase_free_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | spin_lock(&c->erase_completion_lock); |
Akinobu Mita | f116629 | 2006-06-26 00:24:46 -0700 | [diff] [blame] | 196 | list_move(&jeb->list, &c->erase_pending_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | c->erasing_size -= c->sector_size; |
| 198 | c->dirty_size += c->sector_size; |
| 199 | jeb->dirty_size = c->sector_size; |
| 200 | spin_unlock(&c->erase_completion_lock); |
David Woodhouse | ced2207 | 2008-04-22 15:13:40 +0100 | [diff] [blame] | 201 | mutex_unlock(&c->erase_free_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | return; |
| 203 | } |
| 204 | } |
| 205 | |
David Woodhouse | ced2207 | 2008-04-22 15:13:40 +0100 | [diff] [blame] | 206 | mutex_lock(&c->erase_free_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | spin_lock(&c->erase_completion_lock); |
| 208 | c->erasing_size -= c->sector_size; |
| 209 | c->bad_size += c->sector_size; |
Akinobu Mita | f116629 | 2006-06-26 00:24:46 -0700 | [diff] [blame] | 210 | list_move(&jeb->list, &c->bad_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | c->nr_erasing_blocks--; |
| 212 | spin_unlock(&c->erase_completion_lock); |
David Woodhouse | ced2207 | 2008-04-22 15:13:40 +0100 | [diff] [blame] | 213 | mutex_unlock(&c->erase_free_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | wake_up(&c->erase_wait); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 215 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
| 217 | #ifndef __ECOS |
| 218 | static void jffs2_erase_callback(struct erase_info *instr) |
| 219 | { |
| 220 | struct erase_priv_struct *priv = (void *)instr->priv; |
| 221 | |
| 222 | if(instr->state != MTD_ERASE_DONE) { |
Joe Perches | da320f0 | 2012-02-15 15:56:44 -0800 | [diff] [blame] | 223 | pr_warn("Erase at 0x%08llx finished, but state != MTD_ERASE_DONE. State is 0x%x instead.\n", |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 224 | (unsigned long long)instr->addr, instr->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | jffs2_erase_failed(priv->c, priv->jeb, instr->fail_addr); |
| 226 | } else { |
| 227 | jffs2_erase_succeeded(priv->c, priv->jeb); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 228 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | kfree(instr); |
| 230 | } |
| 231 | #endif /* !__ECOS */ |
| 232 | |
| 233 | /* Hmmm. Maybe we should accept the extra space it takes and make |
| 234 | this a standard doubly-linked list? */ |
| 235 | static inline void jffs2_remove_node_refs_from_ino_list(struct jffs2_sb_info *c, |
| 236 | struct jffs2_raw_node_ref *ref, struct jffs2_eraseblock *jeb) |
| 237 | { |
| 238 | struct jffs2_inode_cache *ic = NULL; |
| 239 | struct jffs2_raw_node_ref **prev; |
| 240 | |
| 241 | prev = &ref->next_in_ino; |
| 242 | |
| 243 | /* Walk the inode's list once, removing any nodes from this eraseblock */ |
| 244 | while (1) { |
| 245 | if (!(*prev)->next_in_ino) { |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 246 | /* We're looking at the jffs2_inode_cache, which is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | at the end of the linked list. Stash it and continue |
| 248 | from the beginning of the list */ |
| 249 | ic = (struct jffs2_inode_cache *)(*prev); |
| 250 | prev = &ic->nodes; |
| 251 | continue; |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 252 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
Andrew Victor | 3be3667 | 2005-02-09 09:09:05 +0000 | [diff] [blame] | 254 | if (SECTOR_ADDR((*prev)->flash_offset) == jeb->offset) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | /* It's in the block we're erasing */ |
| 256 | struct jffs2_raw_node_ref *this; |
| 257 | |
| 258 | this = *prev; |
| 259 | *prev = this->next_in_ino; |
| 260 | this->next_in_ino = NULL; |
| 261 | |
| 262 | if (this == ref) |
| 263 | break; |
| 264 | |
| 265 | continue; |
| 266 | } |
| 267 | /* Not to be deleted. Skip */ |
| 268 | prev = &((*prev)->next_in_ino); |
| 269 | } |
| 270 | |
| 271 | /* PARANOIA */ |
| 272 | if (!ic) { |
KaiGai Kohei | c9f700f | 2006-06-11 10:35:15 +0900 | [diff] [blame] | 273 | JFFS2_WARNING("inode_cache/xattr_datum/xattr_ref" |
| 274 | " not found in remove_node_refs()!!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | return; |
| 276 | } |
| 277 | |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame] | 278 | jffs2_dbg(1, "Removed nodes in range 0x%08x-0x%08x from ino #%u\n", |
| 279 | jeb->offset, jeb->offset + c->sector_size, ic->ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | |
| 281 | D2({ |
| 282 | int i=0; |
| 283 | struct jffs2_raw_node_ref *this; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 284 | printk(KERN_DEBUG "After remove_node_refs_from_ino_list: \n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
| 286 | this = ic->nodes; |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 287 | |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 288 | printk(KERN_DEBUG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | while(this) { |
Joe Perches | da320f0 | 2012-02-15 15:56:44 -0800 | [diff] [blame] | 290 | pr_cont("0x%08x(%d)->", |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 291 | ref_offset(this), ref_flags(this)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | if (++i == 5) { |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 293 | printk(KERN_DEBUG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | i=0; |
| 295 | } |
| 296 | this = this->next_in_ino; |
| 297 | } |
Joe Perches | da320f0 | 2012-02-15 15:56:44 -0800 | [diff] [blame] | 298 | pr_cont("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | }); |
| 300 | |
KaiGai Kohei | c9f700f | 2006-06-11 10:35:15 +0900 | [diff] [blame] | 301 | switch (ic->class) { |
| 302 | #ifdef CONFIG_JFFS2_FS_XATTR |
| 303 | case RAWNODE_CLASS_XATTR_DATUM: |
| 304 | jffs2_release_xattr_datum(c, (struct jffs2_xattr_datum *)ic); |
| 305 | break; |
| 306 | case RAWNODE_CLASS_XATTR_REF: |
| 307 | jffs2_release_xattr_ref(c, (struct jffs2_xattr_ref *)ic); |
| 308 | break; |
| 309 | #endif |
| 310 | default: |
David Woodhouse | 27c72b0 | 2008-05-01 18:47:17 +0100 | [diff] [blame] | 311 | if (ic->nodes == (void *)ic && ic->pino_nlink == 0) |
KaiGai Kohei | c9f700f | 2006-06-11 10:35:15 +0900 | [diff] [blame] | 312 | jffs2_del_ino_cache(c, ic); |
| 313 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | } |
| 315 | |
David Woodhouse | c38c1b6 | 2006-05-25 01:38:27 +0100 | [diff] [blame] | 316 | void jffs2_free_jeb_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | { |
David Woodhouse | 9bfeb69 | 2006-05-26 21:19:05 +0100 | [diff] [blame] | 318 | struct jffs2_raw_node_ref *block, *ref; |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame] | 319 | jffs2_dbg(1, "Freeing all node refs for eraseblock offset 0x%08x\n", |
| 320 | jeb->offset); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 321 | |
David Woodhouse | 9bfeb69 | 2006-05-26 21:19:05 +0100 | [diff] [blame] | 322 | block = ref = jeb->first_node; |
| 323 | |
| 324 | while (ref) { |
| 325 | if (ref->flash_offset == REF_LINK_NODE) { |
| 326 | ref = ref->next_in_ino; |
| 327 | jffs2_free_refblock(block); |
| 328 | block = ref; |
| 329 | continue; |
| 330 | } |
| 331 | if (ref->flash_offset != REF_EMPTY_NODE && ref->next_in_ino) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | jffs2_remove_node_refs_from_ino_list(c, ref, jeb); |
| 333 | /* else it was a non-inode node or already removed, so don't bother */ |
| 334 | |
David Woodhouse | 9bfeb69 | 2006-05-26 21:19:05 +0100 | [diff] [blame] | 335 | ref++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | } |
David Woodhouse | 9bfeb69 | 2006-05-26 21:19:05 +0100 | [diff] [blame] | 337 | jeb->first_node = jeb->last_node = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Thomas Gleixner | 5d15788 | 2005-07-15 08:14:44 +0200 | [diff] [blame] | 340 | static int jffs2_block_check_erase(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t *bad_offset) |
| 341 | { |
| 342 | void *ebuf; |
| 343 | uint32_t ofs; |
| 344 | size_t retlen; |
Artem Bityutskiy | bce41d6 | 2012-01-10 15:32:29 +0200 | [diff] [blame] | 345 | int ret; |
Artem Bityutskiy | 1093447 | 2011-12-28 15:55:42 +0200 | [diff] [blame] | 346 | unsigned long *wordebuf; |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 347 | |
Artem Bityutskiy | 1093447 | 2011-12-28 15:55:42 +0200 | [diff] [blame] | 348 | ret = mtd_point(c->mtd, jeb->offset, c->sector_size, &retlen, |
| 349 | &ebuf, NULL); |
| 350 | if (ret != -EOPNOTSUPP) { |
Joakim Tjernlund | fab2c39 | 2007-06-01 15:14:09 +0200 | [diff] [blame] | 351 | if (ret) { |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame] | 352 | jffs2_dbg(1, "MTD point failed %d\n", ret); |
Joakim Tjernlund | fab2c39 | 2007-06-01 15:14:09 +0200 | [diff] [blame] | 353 | goto do_flash_read; |
| 354 | } |
| 355 | if (retlen < c->sector_size) { |
| 356 | /* Don't muck about if it won't let us point to the whole erase sector */ |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame] | 357 | jffs2_dbg(1, "MTD point returned len too short: 0x%zx\n", |
| 358 | retlen); |
Artem Bityutskiy | 7219778 | 2011-12-23 17:05:52 +0200 | [diff] [blame] | 359 | mtd_unpoint(c->mtd, jeb->offset, retlen); |
Joakim Tjernlund | fab2c39 | 2007-06-01 15:14:09 +0200 | [diff] [blame] | 360 | goto do_flash_read; |
| 361 | } |
| 362 | wordebuf = ebuf-sizeof(*wordebuf); |
| 363 | retlen /= sizeof(*wordebuf); |
| 364 | do { |
| 365 | if (*++wordebuf != ~0) |
| 366 | break; |
| 367 | } while(--retlen); |
Artem Bityutskiy | 7219778 | 2011-12-23 17:05:52 +0200 | [diff] [blame] | 368 | mtd_unpoint(c->mtd, jeb->offset, c->sector_size); |
Anders Grafström | 8a0f572 | 2008-03-12 20:29:23 +0100 | [diff] [blame] | 369 | if (retlen) { |
Joe Perches | da320f0 | 2012-02-15 15:56:44 -0800 | [diff] [blame] | 370 | pr_warn("Newly-erased block contained word 0x%lx at offset 0x%08tx\n", |
| 371 | *wordebuf, |
| 372 | jeb->offset + |
| 373 | c->sector_size-retlen * sizeof(*wordebuf)); |
Anders Grafström | 8a0f572 | 2008-03-12 20:29:23 +0100 | [diff] [blame] | 374 | return -EIO; |
| 375 | } |
Joakim Tjernlund | fab2c39 | 2007-06-01 15:14:09 +0200 | [diff] [blame] | 376 | return 0; |
| 377 | } |
| 378 | do_flash_read: |
Thomas Gleixner | 5d15788 | 2005-07-15 08:14:44 +0200 | [diff] [blame] | 379 | ebuf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 380 | if (!ebuf) { |
Joe Perches | da320f0 | 2012-02-15 15:56:44 -0800 | [diff] [blame] | 381 | pr_warn("Failed to allocate page buffer for verifying erase at 0x%08x. Refiling\n", |
| 382 | jeb->offset); |
Thomas Gleixner | 5d15788 | 2005-07-15 08:14:44 +0200 | [diff] [blame] | 383 | return -EAGAIN; |
| 384 | } |
| 385 | |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame] | 386 | jffs2_dbg(1, "Verifying erase at 0x%08x\n", jeb->offset); |
Thomas Gleixner | 5d15788 | 2005-07-15 08:14:44 +0200 | [diff] [blame] | 387 | |
| 388 | for (ofs = jeb->offset; ofs < jeb->offset + c->sector_size; ) { |
| 389 | uint32_t readlen = min((uint32_t)PAGE_SIZE, jeb->offset + c->sector_size - ofs); |
| 390 | int i; |
| 391 | |
| 392 | *bad_offset = ofs; |
| 393 | |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 394 | ret = mtd_read(c->mtd, ofs, readlen, &retlen, ebuf); |
Thomas Gleixner | 5d15788 | 2005-07-15 08:14:44 +0200 | [diff] [blame] | 395 | if (ret) { |
Joe Perches | da320f0 | 2012-02-15 15:56:44 -0800 | [diff] [blame] | 396 | pr_warn("Read of newly-erased block at 0x%08x failed: %d. Putting on bad_list\n", |
| 397 | ofs, ret); |
Anders Grafström | 8a0f572 | 2008-03-12 20:29:23 +0100 | [diff] [blame] | 398 | ret = -EIO; |
Thomas Gleixner | 5d15788 | 2005-07-15 08:14:44 +0200 | [diff] [blame] | 399 | goto fail; |
| 400 | } |
| 401 | if (retlen != readlen) { |
Joe Perches | da320f0 | 2012-02-15 15:56:44 -0800 | [diff] [blame] | 402 | pr_warn("Short read from newly-erased block at 0x%08x. Wanted %d, got %zd\n", |
| 403 | ofs, readlen, retlen); |
Anders Grafström | 8a0f572 | 2008-03-12 20:29:23 +0100 | [diff] [blame] | 404 | ret = -EIO; |
Thomas Gleixner | 5d15788 | 2005-07-15 08:14:44 +0200 | [diff] [blame] | 405 | goto fail; |
| 406 | } |
| 407 | for (i=0; i<readlen; i += sizeof(unsigned long)) { |
| 408 | /* It's OK. We know it's properly aligned */ |
| 409 | unsigned long *datum = ebuf + i; |
| 410 | if (*datum + 1) { |
| 411 | *bad_offset += i; |
Joe Perches | da320f0 | 2012-02-15 15:56:44 -0800 | [diff] [blame] | 412 | pr_warn("Newly-erased block contained word 0x%lx at offset 0x%08x\n", |
| 413 | *datum, *bad_offset); |
Anders Grafström | 8a0f572 | 2008-03-12 20:29:23 +0100 | [diff] [blame] | 414 | ret = -EIO; |
Thomas Gleixner | 5d15788 | 2005-07-15 08:14:44 +0200 | [diff] [blame] | 415 | goto fail; |
| 416 | } |
| 417 | } |
| 418 | ofs += readlen; |
| 419 | cond_resched(); |
| 420 | } |
| 421 | ret = 0; |
| 422 | fail: |
| 423 | kfree(ebuf); |
| 424 | return ret; |
| 425 | } |
| 426 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb) |
| 428 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | size_t retlen; |
| 430 | int ret; |
Andrew Morton | f4e3564 | 2007-08-10 14:01:30 -0700 | [diff] [blame] | 431 | uint32_t uninitialized_var(bad_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | |
Thomas Gleixner | 5d15788 | 2005-07-15 08:14:44 +0200 | [diff] [blame] | 433 | switch (jffs2_block_check_erase(c, jeb, &bad_offset)) { |
| 434 | case -EAGAIN: goto refile; |
| 435 | case -EIO: goto filebad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 438 | /* Write the erase complete marker */ |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame] | 439 | jffs2_dbg(1, "Writing erased marker to block at 0x%08x\n", jeb->offset); |
Thomas Gleixner | 5d15788 | 2005-07-15 08:14:44 +0200 | [diff] [blame] | 440 | bad_offset = jeb->offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | |
Thomas Gleixner | 5d15788 | 2005-07-15 08:14:44 +0200 | [diff] [blame] | 442 | /* Cleanmarker in oob area or no cleanmarker at all ? */ |
| 443 | if (jffs2_cleanmarker_oob(c) || c->cleanmarker_size == 0) { |
| 444 | |
| 445 | if (jffs2_cleanmarker_oob(c)) { |
| 446 | if (jffs2_write_nand_cleanmarker(c, jeb)) |
| 447 | goto filebad; |
| 448 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | } else { |
Thomas Gleixner | 5d15788 | 2005-07-15 08:14:44 +0200 | [diff] [blame] | 450 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | struct kvec vecs[1]; |
| 452 | struct jffs2_unknown_node marker = { |
| 453 | .magic = cpu_to_je16(JFFS2_MAGIC_BITMASK), |
| 454 | .nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER), |
| 455 | .totlen = cpu_to_je32(c->cleanmarker_size) |
| 456 | }; |
| 457 | |
David Woodhouse | 046b8b9 | 2006-05-25 01:50:35 +0100 | [diff] [blame] | 458 | jffs2_prealloc_raw_node_refs(c, jeb, 1); |
Thomas Gleixner | 5d15788 | 2005-07-15 08:14:44 +0200 | [diff] [blame] | 459 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | marker.hdr_crc = cpu_to_je32(crc32(0, &marker, sizeof(struct jffs2_unknown_node)-4)); |
| 461 | |
| 462 | vecs[0].iov_base = (unsigned char *) ▮ |
| 463 | vecs[0].iov_len = sizeof(marker); |
| 464 | ret = jffs2_flash_direct_writev(c, vecs, 1, jeb->offset, &retlen); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 465 | |
Thomas Gleixner | 5d15788 | 2005-07-15 08:14:44 +0200 | [diff] [blame] | 466 | if (ret || retlen != sizeof(marker)) { |
| 467 | if (ret) |
Joe Perches | da320f0 | 2012-02-15 15:56:44 -0800 | [diff] [blame] | 468 | pr_warn("Write clean marker to block at 0x%08x failed: %d\n", |
Thomas Gleixner | 5d15788 | 2005-07-15 08:14:44 +0200 | [diff] [blame] | 469 | jeb->offset, ret); |
| 470 | else |
Joe Perches | da320f0 | 2012-02-15 15:56:44 -0800 | [diff] [blame] | 471 | pr_warn("Short write to newly-erased block at 0x%08x: Wanted %zd, got %zd\n", |
Thomas Gleixner | 5d15788 | 2005-07-15 08:14:44 +0200 | [diff] [blame] | 472 | jeb->offset, sizeof(marker), retlen); |
| 473 | |
Thomas Gleixner | 5d15788 | 2005-07-15 08:14:44 +0200 | [diff] [blame] | 474 | goto filebad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | } |
David Woodhouse | 014b164 | 2008-04-22 23:54:38 +0100 | [diff] [blame] | 477 | /* Everything else got zeroed before the erase */ |
| 478 | jeb->free_size = c->sector_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | |
David Woodhouse | ced2207 | 2008-04-22 15:13:40 +0100 | [diff] [blame] | 480 | mutex_lock(&c->erase_free_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | spin_lock(&c->erase_completion_lock); |
David Woodhouse | 014b164 | 2008-04-22 23:54:38 +0100 | [diff] [blame] | 482 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | c->erasing_size -= c->sector_size; |
David Woodhouse | 014b164 | 2008-04-22 23:54:38 +0100 | [diff] [blame] | 484 | c->free_size += c->sector_size; |
| 485 | |
| 486 | /* Account for cleanmarker now, if it's in-band */ |
| 487 | if (c->cleanmarker_size && !jffs2_cleanmarker_oob(c)) |
| 488 | jffs2_link_node_ref(c, jeb, jeb->offset | REF_NORMAL, c->cleanmarker_size, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | |
David Woodhouse | e2bc322 | 2008-04-23 14:15:24 +0100 | [diff] [blame] | 490 | list_move_tail(&jeb->list, &c->free_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | c->nr_erasing_blocks--; |
| 492 | c->nr_free_blocks++; |
David Woodhouse | 85a62db | 2008-04-23 01:17:51 +0100 | [diff] [blame] | 493 | |
| 494 | jffs2_dbg_acct_sanity_check_nolock(c, jeb); |
| 495 | jffs2_dbg_acct_paranoia_check_nolock(c, jeb); |
| 496 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | spin_unlock(&c->erase_completion_lock); |
David Woodhouse | ced2207 | 2008-04-22 15:13:40 +0100 | [diff] [blame] | 498 | mutex_unlock(&c->erase_free_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | wake_up(&c->erase_wait); |
Thomas Gleixner | 5d15788 | 2005-07-15 08:14:44 +0200 | [diff] [blame] | 500 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | |
Thomas Gleixner | 5d15788 | 2005-07-15 08:14:44 +0200 | [diff] [blame] | 502 | filebad: |
Thomas Gleixner | 5d15788 | 2005-07-15 08:14:44 +0200 | [diff] [blame] | 503 | jffs2_erase_failed(c, jeb, bad_offset); |
| 504 | return; |
| 505 | |
| 506 | refile: |
| 507 | /* Stick it back on the list from whence it came and come back later */ |
David Woodhouse | ced2207 | 2008-04-22 15:13:40 +0100 | [diff] [blame] | 508 | mutex_lock(&c->erase_free_sem); |
Thomas Gleixner | 5d15788 | 2005-07-15 08:14:44 +0200 | [diff] [blame] | 509 | spin_lock(&c->erase_completion_lock); |
David Woodhouse | ae3b6ba | 2010-05-19 17:05:14 +0100 | [diff] [blame] | 510 | jffs2_garbage_collect_trigger(c); |
David Woodhouse | e2bc322 | 2008-04-23 14:15:24 +0100 | [diff] [blame] | 511 | list_move(&jeb->list, &c->erase_complete_list); |
Thomas Gleixner | 5d15788 | 2005-07-15 08:14:44 +0200 | [diff] [blame] | 512 | spin_unlock(&c->erase_completion_lock); |
David Woodhouse | ced2207 | 2008-04-22 15:13:40 +0100 | [diff] [blame] | 513 | mutex_unlock(&c->erase_free_sem); |
Thomas Gleixner | 5d15788 | 2005-07-15 08:14:44 +0200 | [diff] [blame] | 514 | return; |
| 515 | } |