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