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