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