]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * JFFS2 -- Journalling Flash File System, Version 2. | |
3 | * | |
c00c310e | 4 | * Copyright © 2001-2007 Red Hat, Inc. |
6088c058 | 5 | * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org> |
1da177e4 LT |
6 | * |
7 | * Created by David Woodhouse <dwmw2@infradead.org> | |
8 | * | |
9 | * For licensing information, see the file 'LICENCE' in this directory. | |
10 | * | |
1da177e4 LT |
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 | }; | |
182ec4ee | 26 | |
1da177e4 LT |
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); | |
1da177e4 LT |
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) { | |
ef53cb02 DW |
42 | jffs2_erase_succeeded(c, jeb); |
43 | return; | |
1da177e4 LT |
44 | } |
45 | bad_offset = jeb->offset; | |
46 | #else /* Linux */ | |
47 | struct erase_info *instr; | |
48 | ||
e0c8e42f AB |
49 | D1(printk(KERN_DEBUG "jffs2_erase_block(): erase block %#08x (range %#08x-%#08x)\n", |
50 | jeb->offset, jeb->offset, jeb->offset + c->sector_size)); | |
1da177e4 LT |
51 | instr = kmalloc(sizeof(struct erase_info) + sizeof(struct erase_priv_struct), GFP_KERNEL); |
52 | if (!instr) { | |
53 | printk(KERN_WARNING "kmalloc for struct erase_info in jffs2_erase_block failed. Refiling block for later\n"); | |
ced22070 | 54 | mutex_lock(&c->erase_free_sem); |
1da177e4 | 55 | spin_lock(&c->erase_completion_lock); |
f116629d | 56 | list_move(&jeb->list, &c->erase_pending_list); |
1da177e4 LT |
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); | |
ced22070 | 61 | mutex_unlock(&c->erase_free_sem); |
1da177e4 LT |
62 | return; |
63 | } | |
64 | ||
65 | memset(instr, 0, sizeof(*instr)); | |
66 | ||
67 | instr->mtd = c->mtd; | |
68 | instr->addr = jeb->offset; | |
69 | instr->len = c->sector_size; | |
70 | instr->callback = jffs2_erase_callback; | |
71 | instr->priv = (unsigned long)(&instr[1]); | |
bb0eb217 | 72 | instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN; |
182ec4ee | 73 | |
1da177e4 LT |
74 | ((struct erase_priv_struct *)instr->priv)->jeb = jeb; |
75 | ((struct erase_priv_struct *)instr->priv)->c = c; | |
76 | ||
7e1f0dc0 | 77 | ret = mtd_erase(c->mtd, instr); |
1da177e4 LT |
78 | if (!ret) |
79 | return; | |
80 | ||
81 | bad_offset = instr->fail_addr; | |
82 | kfree(instr); | |
83 | #endif /* __ECOS */ | |
84 | ||
85 | if (ret == -ENOMEM || ret == -EAGAIN) { | |
86 | /* Erase failed immediately. Refile it on the list */ | |
87 | D1(printk(KERN_DEBUG "Erase at 0x%08x failed: %d. Refiling on erase_pending_list\n", jeb->offset, ret)); | |
ced22070 | 88 | mutex_lock(&c->erase_free_sem); |
1da177e4 | 89 | spin_lock(&c->erase_completion_lock); |
f116629d | 90 | list_move(&jeb->list, &c->erase_pending_list); |
1da177e4 LT |
91 | c->erasing_size -= c->sector_size; |
92 | c->dirty_size += c->sector_size; | |
93 | jeb->dirty_size = c->sector_size; | |
94 | spin_unlock(&c->erase_completion_lock); | |
ced22070 | 95 | mutex_unlock(&c->erase_free_sem); |
1da177e4 LT |
96 | return; |
97 | } | |
98 | ||
182ec4ee | 99 | if (ret == -EROFS) |
1da177e4 LT |
100 | printk(KERN_WARNING "Erase at 0x%08x failed immediately: -EROFS. Is the sector locked?\n", jeb->offset); |
101 | else | |
102 | printk(KERN_WARNING "Erase at 0x%08x failed immediately: errno %d\n", jeb->offset, ret); | |
103 | ||
104 | jffs2_erase_failed(c, jeb, bad_offset); | |
105 | } | |
106 | ||
9957abea | 107 | int jffs2_erase_pending_blocks(struct jffs2_sb_info *c, int count) |
1da177e4 LT |
108 | { |
109 | struct jffs2_eraseblock *jeb; | |
9957abea | 110 | int work_done = 0; |
1da177e4 | 111 | |
ced22070 | 112 | mutex_lock(&c->erase_free_sem); |
1da177e4 LT |
113 | |
114 | spin_lock(&c->erase_completion_lock); | |
115 | ||
116 | while (!list_empty(&c->erase_complete_list) || | |
117 | !list_empty(&c->erase_pending_list)) { | |
118 | ||
119 | if (!list_empty(&c->erase_complete_list)) { | |
120 | jeb = list_entry(c->erase_complete_list.next, struct jffs2_eraseblock, list); | |
e2bc322b | 121 | list_move(&jeb->list, &c->erase_checking_list); |
1da177e4 | 122 | spin_unlock(&c->erase_completion_lock); |
ced22070 | 123 | mutex_unlock(&c->erase_free_sem); |
1da177e4 LT |
124 | jffs2_mark_erased_block(c, jeb); |
125 | ||
9957abea | 126 | work_done++; |
1da177e4 LT |
127 | if (!--count) { |
128 | D1(printk(KERN_DEBUG "Count reached. jffs2_erase_pending_blocks leaving\n")); | |
129 | goto done; | |
130 | } | |
131 | ||
132 | } else if (!list_empty(&c->erase_pending_list)) { | |
133 | jeb = list_entry(c->erase_pending_list.next, struct jffs2_eraseblock, list); | |
134 | D1(printk(KERN_DEBUG "Starting erase of pending block 0x%08x\n", jeb->offset)); | |
135 | list_del(&jeb->list); | |
136 | c->erasing_size += c->sector_size; | |
137 | c->wasted_size -= jeb->wasted_size; | |
138 | c->free_size -= jeb->free_size; | |
139 | c->used_size -= jeb->used_size; | |
140 | c->dirty_size -= jeb->dirty_size; | |
141 | jeb->wasted_size = jeb->used_size = jeb->dirty_size = jeb->free_size = 0; | |
c38c1b61 | 142 | jffs2_free_jeb_node_refs(c, jeb); |
1da177e4 LT |
143 | list_add(&jeb->list, &c->erasing_list); |
144 | spin_unlock(&c->erase_completion_lock); | |
ced22070 | 145 | mutex_unlock(&c->erase_free_sem); |
1da177e4 LT |
146 | |
147 | jffs2_erase_block(c, jeb); | |
148 | ||
149 | } else { | |
150 | BUG(); | |
151 | } | |
152 | ||
153 | /* Be nice */ | |
3866f673 | 154 | cond_resched(); |
ced22070 | 155 | mutex_lock(&c->erase_free_sem); |
1da177e4 LT |
156 | spin_lock(&c->erase_completion_lock); |
157 | } | |
158 | ||
159 | spin_unlock(&c->erase_completion_lock); | |
ced22070 | 160 | mutex_unlock(&c->erase_free_sem); |
1da177e4 LT |
161 | done: |
162 | D1(printk(KERN_DEBUG "jffs2_erase_pending_blocks completed\n")); | |
9957abea | 163 | return work_done; |
1da177e4 LT |
164 | } |
165 | ||
166 | static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb) | |
167 | { | |
168 | D1(printk(KERN_DEBUG "Erase completed successfully at 0x%08x\n", jeb->offset)); | |
ced22070 | 169 | mutex_lock(&c->erase_free_sem); |
1da177e4 | 170 | spin_lock(&c->erase_completion_lock); |
f116629d | 171 | list_move_tail(&jeb->list, &c->erase_complete_list); |
ae3b6ba0 DW |
172 | /* Wake the GC thread to mark them clean */ |
173 | jffs2_garbage_collect_trigger(c); | |
1da177e4 | 174 | spin_unlock(&c->erase_completion_lock); |
ced22070 | 175 | mutex_unlock(&c->erase_free_sem); |
0717bf84 | 176 | wake_up(&c->erase_wait); |
1da177e4 LT |
177 | } |
178 | ||
179 | static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset) | |
180 | { | |
181 | /* For NAND, if the failure did not occur at the device level for a | |
182 | specific physical page, don't bother updating the bad block table. */ | |
69423d99 | 183 | if (jffs2_cleanmarker_oob(c) && (bad_offset != (uint32_t)MTD_FAIL_ADDR_UNKNOWN)) { |
1da177e4 LT |
184 | /* We had a device-level failure to erase. Let's see if we've |
185 | failed too many times. */ | |
186 | if (!jffs2_write_nand_badblock(c, jeb, bad_offset)) { | |
187 | /* We'd like to give this block another try. */ | |
ced22070 | 188 | mutex_lock(&c->erase_free_sem); |
1da177e4 | 189 | spin_lock(&c->erase_completion_lock); |
f116629d | 190 | list_move(&jeb->list, &c->erase_pending_list); |
1da177e4 LT |
191 | c->erasing_size -= c->sector_size; |
192 | c->dirty_size += c->sector_size; | |
193 | jeb->dirty_size = c->sector_size; | |
194 | spin_unlock(&c->erase_completion_lock); | |
ced22070 | 195 | mutex_unlock(&c->erase_free_sem); |
1da177e4 LT |
196 | return; |
197 | } | |
198 | } | |
199 | ||
ced22070 | 200 | mutex_lock(&c->erase_free_sem); |
1da177e4 LT |
201 | spin_lock(&c->erase_completion_lock); |
202 | c->erasing_size -= c->sector_size; | |
203 | c->bad_size += c->sector_size; | |
f116629d | 204 | list_move(&jeb->list, &c->bad_list); |
1da177e4 LT |
205 | c->nr_erasing_blocks--; |
206 | spin_unlock(&c->erase_completion_lock); | |
ced22070 | 207 | mutex_unlock(&c->erase_free_sem); |
1da177e4 | 208 | wake_up(&c->erase_wait); |
182ec4ee | 209 | } |
1da177e4 LT |
210 | |
211 | #ifndef __ECOS | |
212 | static void jffs2_erase_callback(struct erase_info *instr) | |
213 | { | |
214 | struct erase_priv_struct *priv = (void *)instr->priv; | |
215 | ||
216 | if(instr->state != MTD_ERASE_DONE) { | |
69423d99 AH |
217 | printk(KERN_WARNING "Erase at 0x%08llx finished, but state != MTD_ERASE_DONE. State is 0x%x instead.\n", |
218 | (unsigned long long)instr->addr, instr->state); | |
1da177e4 LT |
219 | jffs2_erase_failed(priv->c, priv->jeb, instr->fail_addr); |
220 | } else { | |
221 | jffs2_erase_succeeded(priv->c, priv->jeb); | |
182ec4ee | 222 | } |
1da177e4 LT |
223 | kfree(instr); |
224 | } | |
225 | #endif /* !__ECOS */ | |
226 | ||
227 | /* Hmmm. Maybe we should accept the extra space it takes and make | |
228 | this a standard doubly-linked list? */ | |
229 | static inline void jffs2_remove_node_refs_from_ino_list(struct jffs2_sb_info *c, | |
230 | struct jffs2_raw_node_ref *ref, struct jffs2_eraseblock *jeb) | |
231 | { | |
232 | struct jffs2_inode_cache *ic = NULL; | |
233 | struct jffs2_raw_node_ref **prev; | |
234 | ||
235 | prev = &ref->next_in_ino; | |
236 | ||
237 | /* Walk the inode's list once, removing any nodes from this eraseblock */ | |
238 | while (1) { | |
239 | if (!(*prev)->next_in_ino) { | |
182ec4ee | 240 | /* We're looking at the jffs2_inode_cache, which is |
1da177e4 LT |
241 | at the end of the linked list. Stash it and continue |
242 | from the beginning of the list */ | |
243 | ic = (struct jffs2_inode_cache *)(*prev); | |
244 | prev = &ic->nodes; | |
245 | continue; | |
182ec4ee | 246 | } |
1da177e4 | 247 | |
3be36675 | 248 | if (SECTOR_ADDR((*prev)->flash_offset) == jeb->offset) { |
1da177e4 LT |
249 | /* It's in the block we're erasing */ |
250 | struct jffs2_raw_node_ref *this; | |
251 | ||
252 | this = *prev; | |
253 | *prev = this->next_in_ino; | |
254 | this->next_in_ino = NULL; | |
255 | ||
256 | if (this == ref) | |
257 | break; | |
258 | ||
259 | continue; | |
260 | } | |
261 | /* Not to be deleted. Skip */ | |
262 | prev = &((*prev)->next_in_ino); | |
263 | } | |
264 | ||
265 | /* PARANOIA */ | |
266 | if (!ic) { | |
c9f700f8 KK |
267 | JFFS2_WARNING("inode_cache/xattr_datum/xattr_ref" |
268 | " not found in remove_node_refs()!!\n"); | |
1da177e4 LT |
269 | return; |
270 | } | |
271 | ||
272 | D1(printk(KERN_DEBUG "Removed nodes in range 0x%08x-0x%08x from ino #%u\n", | |
273 | jeb->offset, jeb->offset + c->sector_size, ic->ino)); | |
274 | ||
275 | D2({ | |
276 | int i=0; | |
277 | struct jffs2_raw_node_ref *this; | |
ad361c98 | 278 | printk(KERN_DEBUG "After remove_node_refs_from_ino_list: \n"); |
1da177e4 LT |
279 | |
280 | this = ic->nodes; | |
182ec4ee | 281 | |
ad361c98 | 282 | printk(KERN_DEBUG); |
1da177e4 | 283 | while(this) { |
ad361c98 JP |
284 | printk(KERN_CONT "0x%08x(%d)->", |
285 | ref_offset(this), ref_flags(this)); | |
1da177e4 | 286 | if (++i == 5) { |
ad361c98 | 287 | printk(KERN_DEBUG); |
1da177e4 LT |
288 | i=0; |
289 | } | |
290 | this = this->next_in_ino; | |
291 | } | |
ad361c98 | 292 | printk(KERN_CONT "\n"); |
1da177e4 LT |
293 | }); |
294 | ||
c9f700f8 KK |
295 | switch (ic->class) { |
296 | #ifdef CONFIG_JFFS2_FS_XATTR | |
297 | case RAWNODE_CLASS_XATTR_DATUM: | |
298 | jffs2_release_xattr_datum(c, (struct jffs2_xattr_datum *)ic); | |
299 | break; | |
300 | case RAWNODE_CLASS_XATTR_REF: | |
301 | jffs2_release_xattr_ref(c, (struct jffs2_xattr_ref *)ic); | |
302 | break; | |
303 | #endif | |
304 | default: | |
27c72b04 | 305 | if (ic->nodes == (void *)ic && ic->pino_nlink == 0) |
c9f700f8 KK |
306 | jffs2_del_ino_cache(c, ic); |
307 | } | |
1da177e4 LT |
308 | } |
309 | ||
c38c1b61 | 310 | void jffs2_free_jeb_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb) |
1da177e4 | 311 | { |
9bfeb691 | 312 | struct jffs2_raw_node_ref *block, *ref; |
1da177e4 | 313 | D1(printk(KERN_DEBUG "Freeing all node refs for eraseblock offset 0x%08x\n", jeb->offset)); |
182ec4ee | 314 | |
9bfeb691 DW |
315 | block = ref = jeb->first_node; |
316 | ||
317 | while (ref) { | |
318 | if (ref->flash_offset == REF_LINK_NODE) { | |
319 | ref = ref->next_in_ino; | |
320 | jffs2_free_refblock(block); | |
321 | block = ref; | |
322 | continue; | |
323 | } | |
324 | if (ref->flash_offset != REF_EMPTY_NODE && ref->next_in_ino) | |
1da177e4 LT |
325 | jffs2_remove_node_refs_from_ino_list(c, ref, jeb); |
326 | /* else it was a non-inode node or already removed, so don't bother */ | |
327 | ||
9bfeb691 | 328 | ref++; |
1da177e4 | 329 | } |
9bfeb691 | 330 | jeb->first_node = jeb->last_node = NULL; |
1da177e4 LT |
331 | } |
332 | ||
5d157885 | 333 | static int jffs2_block_check_erase(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t *bad_offset) |
1da177e4 | 334 | { |
5d157885 TG |
335 | void *ebuf; |
336 | uint32_t ofs; | |
1da177e4 | 337 | size_t retlen; |
5d157885 | 338 | int ret = -EIO; |
182ec4ee | 339 | |
fab2c399 JT |
340 | if (c->mtd->point) { |
341 | unsigned long *wordebuf; | |
342 | ||
d35ea200 AB |
343 | ret = mtd_point(c->mtd, jeb->offset, c->sector_size, &retlen, |
344 | &ebuf, NULL); | |
fab2c399 JT |
345 | if (ret) { |
346 | D1(printk(KERN_DEBUG "MTD point failed %d\n", ret)); | |
347 | goto do_flash_read; | |
348 | } | |
349 | if (retlen < c->sector_size) { | |
350 | /* Don't muck about if it won't let us point to the whole erase sector */ | |
351 | D1(printk(KERN_DEBUG "MTD point returned len too short: 0x%zx\n", retlen)); | |
7219778a | 352 | mtd_unpoint(c->mtd, jeb->offset, retlen); |
fab2c399 JT |
353 | goto do_flash_read; |
354 | } | |
355 | wordebuf = ebuf-sizeof(*wordebuf); | |
356 | retlen /= sizeof(*wordebuf); | |
357 | do { | |
358 | if (*++wordebuf != ~0) | |
359 | break; | |
360 | } while(--retlen); | |
7219778a | 361 | mtd_unpoint(c->mtd, jeb->offset, c->sector_size); |
8a0f5723 | 362 | if (retlen) { |
f4e35647 | 363 | printk(KERN_WARNING "Newly-erased block contained word 0x%lx at offset 0x%08tx\n", |
fab2c399 | 364 | *wordebuf, jeb->offset + c->sector_size-retlen*sizeof(*wordebuf)); |
8a0f5723 AG |
365 | return -EIO; |
366 | } | |
fab2c399 JT |
367 | return 0; |
368 | } | |
369 | do_flash_read: | |
1da177e4 LT |
370 | ebuf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
371 | if (!ebuf) { | |
5d157885 TG |
372 | printk(KERN_WARNING "Failed to allocate page buffer for verifying erase at 0x%08x. Refiling\n", jeb->offset); |
373 | return -EAGAIN; | |
374 | } | |
1da177e4 | 375 | |
5d157885 | 376 | D1(printk(KERN_DEBUG "Verifying erase at 0x%08x\n", jeb->offset)); |
1da177e4 | 377 | |
5d157885 TG |
378 | for (ofs = jeb->offset; ofs < jeb->offset + c->sector_size; ) { |
379 | uint32_t readlen = min((uint32_t)PAGE_SIZE, jeb->offset + c->sector_size - ofs); | |
380 | int i; | |
1da177e4 | 381 | |
5d157885 | 382 | *bad_offset = ofs; |
894214d1 | 383 | |
b0afbbec | 384 | ret = c->mtd->read(c->mtd, ofs, readlen, &retlen, ebuf); |
5d157885 TG |
385 | if (ret) { |
386 | printk(KERN_WARNING "Read of newly-erased block at 0x%08x failed: %d. Putting on bad_list\n", ofs, ret); | |
8a0f5723 | 387 | ret = -EIO; |
5d157885 TG |
388 | goto fail; |
389 | } | |
390 | if (retlen != readlen) { | |
391 | printk(KERN_WARNING "Short read from newly-erased block at 0x%08x. Wanted %d, got %zd\n", ofs, readlen, retlen); | |
8a0f5723 | 392 | ret = -EIO; |
5d157885 TG |
393 | goto fail; |
394 | } | |
395 | for (i=0; i<readlen; i += sizeof(unsigned long)) { | |
396 | /* It's OK. We know it's properly aligned */ | |
397 | unsigned long *datum = ebuf + i; | |
398 | if (*datum + 1) { | |
399 | *bad_offset += i; | |
400 | printk(KERN_WARNING "Newly-erased block contained word 0x%lx at offset 0x%08x\n", *datum, *bad_offset); | |
8a0f5723 | 401 | ret = -EIO; |
5d157885 | 402 | goto fail; |
1da177e4 | 403 | } |
1da177e4 | 404 | } |
5d157885 TG |
405 | ofs += readlen; |
406 | cond_resched(); | |
1da177e4 | 407 | } |
5d157885 TG |
408 | ret = 0; |
409 | fail: | |
410 | kfree(ebuf); | |
411 | return ret; | |
412 | } | |
1da177e4 | 413 | |
5d157885 TG |
414 | static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb) |
415 | { | |
5d157885 TG |
416 | size_t retlen; |
417 | int ret; | |
f4e35647 | 418 | uint32_t uninitialized_var(bad_offset); |
5d157885 TG |
419 | |
420 | switch (jffs2_block_check_erase(c, jeb, &bad_offset)) { | |
421 | case -EAGAIN: goto refile; | |
422 | case -EIO: goto filebad; | |
423 | } | |
1da177e4 | 424 | |
182ec4ee | 425 | /* Write the erase complete marker */ |
1da177e4 | 426 | D1(printk(KERN_DEBUG "Writing erased marker to block at 0x%08x\n", jeb->offset)); |
5d157885 | 427 | bad_offset = jeb->offset; |
1da177e4 | 428 | |
5d157885 TG |
429 | /* Cleanmarker in oob area or no cleanmarker at all ? */ |
430 | if (jffs2_cleanmarker_oob(c) || c->cleanmarker_size == 0) { | |
1da177e4 | 431 | |
5d157885 TG |
432 | if (jffs2_cleanmarker_oob(c)) { |
433 | if (jffs2_write_nand_cleanmarker(c, jeb)) | |
434 | goto filebad; | |
435 | } | |
1da177e4 | 436 | } else { |
5d157885 | 437 | |
1da177e4 LT |
438 | struct kvec vecs[1]; |
439 | struct jffs2_unknown_node marker = { | |
440 | .magic = cpu_to_je16(JFFS2_MAGIC_BITMASK), | |
441 | .nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER), | |
442 | .totlen = cpu_to_je32(c->cleanmarker_size) | |
443 | }; | |
444 | ||
046b8b98 | 445 | jffs2_prealloc_raw_node_refs(c, jeb, 1); |
5d157885 | 446 | |
1da177e4 LT |
447 | marker.hdr_crc = cpu_to_je32(crc32(0, &marker, sizeof(struct jffs2_unknown_node)-4)); |
448 | ||
449 | vecs[0].iov_base = (unsigned char *) ▮ | |
450 | vecs[0].iov_len = sizeof(marker); | |
451 | ret = jffs2_flash_direct_writev(c, vecs, 1, jeb->offset, &retlen); | |
182ec4ee | 452 | |
5d157885 TG |
453 | if (ret || retlen != sizeof(marker)) { |
454 | if (ret) | |
455 | printk(KERN_WARNING "Write clean marker to block at 0x%08x failed: %d\n", | |
456 | jeb->offset, ret); | |
457 | else | |
458 | printk(KERN_WARNING "Short write to newly-erased block at 0x%08x: Wanted %zd, got %zd\n", | |
459 | jeb->offset, sizeof(marker), retlen); | |
460 | ||
5d157885 | 461 | goto filebad; |
1da177e4 | 462 | } |
1da177e4 | 463 | } |
014b164e DW |
464 | /* Everything else got zeroed before the erase */ |
465 | jeb->free_size = c->sector_size; | |
1da177e4 | 466 | |
ced22070 | 467 | mutex_lock(&c->erase_free_sem); |
1da177e4 | 468 | spin_lock(&c->erase_completion_lock); |
014b164e | 469 | |
1da177e4 | 470 | c->erasing_size -= c->sector_size; |
014b164e DW |
471 | c->free_size += c->sector_size; |
472 | ||
473 | /* Account for cleanmarker now, if it's in-band */ | |
474 | if (c->cleanmarker_size && !jffs2_cleanmarker_oob(c)) | |
475 | jffs2_link_node_ref(c, jeb, jeb->offset | REF_NORMAL, c->cleanmarker_size, NULL); | |
1da177e4 | 476 | |
e2bc322b | 477 | list_move_tail(&jeb->list, &c->free_list); |
1da177e4 LT |
478 | c->nr_erasing_blocks--; |
479 | c->nr_free_blocks++; | |
85a62db6 DW |
480 | |
481 | jffs2_dbg_acct_sanity_check_nolock(c, jeb); | |
482 | jffs2_dbg_acct_paranoia_check_nolock(c, jeb); | |
483 | ||
1da177e4 | 484 | spin_unlock(&c->erase_completion_lock); |
ced22070 | 485 | mutex_unlock(&c->erase_free_sem); |
1da177e4 | 486 | wake_up(&c->erase_wait); |
5d157885 TG |
487 | return; |
488 | ||
489 | filebad: | |
5d157885 TG |
490 | jffs2_erase_failed(c, jeb, bad_offset); |
491 | return; | |
1da177e4 | 492 | |
5d157885 TG |
493 | refile: |
494 | /* Stick it back on the list from whence it came and come back later */ | |
ced22070 | 495 | mutex_lock(&c->erase_free_sem); |
5d157885 | 496 | spin_lock(&c->erase_completion_lock); |
ae3b6ba0 | 497 | jffs2_garbage_collect_trigger(c); |
e2bc322b | 498 | list_move(&jeb->list, &c->erase_complete_list); |
5d157885 | 499 | spin_unlock(&c->erase_completion_lock); |
ced22070 | 500 | mutex_unlock(&c->erase_free_sem); |
5d157885 TG |
501 | return; |
502 | } |