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