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