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