wbuf.c 35.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11
/*
 * JFFS2 -- Journalling Flash File System, Version 2.
 *
 * Copyright (C) 2001-2003 Red Hat, Inc.
 * Copyright (C) 2004 Thomas Gleixner <tglx@linutronix.de>
 *
 * Created by David Woodhouse <dwmw2@infradead.org>
 * Modified debugged and enhanced by Thomas Gleixner <tglx@linutronix.de>
 *
 * For licensing information, see the file 'LICENCE' in this directory.
 *
12
 * $Id: wbuf.c,v 1.100 2005/09/30 13:59:13 dedekind Exp $
L
Linus Torvalds 已提交
13 14 15 16 17 18 19 20
 *
 */

#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/mtd/mtd.h>
#include <linux/crc32.h>
#include <linux/mtd/nand.h>
T
Tim Schmielau 已提交
21 22
#include <linux/jiffies.h>

L
Linus Torvalds 已提交
23 24 25 26 27 28 29 30 31 32
#include "nodelist.h"

/* For testing write failures */
#undef BREAKME
#undef BREAKMEHEADER

#ifdef BREAKME
static unsigned char *brokenbuf;
#endif

33 34 35
#define PAGE_DIV(x) ( ((unsigned long)(x) / (unsigned long)(c->wbuf_pagesize)) * (unsigned long)(c->wbuf_pagesize) )
#define PAGE_MOD(x) ( (unsigned long)(x) % (unsigned long)(c->wbuf_pagesize) )

L
Linus Torvalds 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
/* max. erase failures before we mark a block bad */
#define MAX_ERASE_FAILURES 	2

struct jffs2_inodirty {
	uint32_t ino;
	struct jffs2_inodirty *next;
};

static struct jffs2_inodirty inodirty_nomem;

static int jffs2_wbuf_pending_for_ino(struct jffs2_sb_info *c, uint32_t ino)
{
	struct jffs2_inodirty *this = c->wbuf_inodes;

	/* If a malloc failed, consider _everything_ dirty */
	if (this == &inodirty_nomem)
		return 1;

	/* If ino == 0, _any_ non-GC writes mean 'yes' */
	if (this && !ino)
		return 1;

	/* Look to see if the inode in question is pending in the wbuf */
	while (this) {
		if (this->ino == ino)
			return 1;
		this = this->next;
	}
	return 0;
}

static void jffs2_clear_wbuf_ino_list(struct jffs2_sb_info *c)
{
	struct jffs2_inodirty *this;

	this = c->wbuf_inodes;

	if (this != &inodirty_nomem) {
		while (this) {
			struct jffs2_inodirty *next = this->next;
			kfree(this);
			this = next;
		}
	}
	c->wbuf_inodes = NULL;
}

static void jffs2_wbuf_dirties_inode(struct jffs2_sb_info *c, uint32_t ino)
{
	struct jffs2_inodirty *new;

	/* Mark the superblock dirty so that kupdated will flush... */
88
	jffs2_erase_pending_trigger(c);
L
Linus Torvalds 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

	if (jffs2_wbuf_pending_for_ino(c, ino))
		return;

	new = kmalloc(sizeof(*new), GFP_KERNEL);
	if (!new) {
		D1(printk(KERN_DEBUG "No memory to allocate inodirty. Fallback to all considered dirty\n"));
		jffs2_clear_wbuf_ino_list(c);
		c->wbuf_inodes = &inodirty_nomem;
		return;
	}
	new->ino = ino;
	new->next = c->wbuf_inodes;
	c->wbuf_inodes = new;
	return;
}

static inline void jffs2_refile_wbuf_blocks(struct jffs2_sb_info *c)
{
	struct list_head *this, *next;
	static int n;

	if (list_empty(&c->erasable_pending_wbuf_list))
		return;

	list_for_each_safe(this, next, &c->erasable_pending_wbuf_list) {
		struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);

		D1(printk(KERN_DEBUG "Removing eraseblock at 0x%08x from erasable_pending_wbuf_list...\n", jeb->offset));
		list_del(this);
		if ((jiffies + (n++)) & 127) {
			/* Most of the time, we just erase it immediately. Otherwise we
			   spend ages scanning it on mount, etc. */
			D1(printk(KERN_DEBUG "...and adding to erase_pending_list\n"));
			list_add_tail(&jeb->list, &c->erase_pending_list);
			c->nr_erasing_blocks++;
			jffs2_erase_pending_trigger(c);
		} else {
			/* Sometimes, however, we leave it elsewhere so it doesn't get
			   immediately reused, and we spread the load a bit. */
			D1(printk(KERN_DEBUG "...and adding to erasable_list\n"));
			list_add_tail(&jeb->list, &c->erasable_list);
		}
	}
}

E
Estelle Hammache 已提交
135 136 137 138
#define REFILE_NOTEMPTY 0
#define REFILE_ANYWAY   1

static void jffs2_block_refile(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, int allow_empty)
L
Linus Torvalds 已提交
139 140 141 142 143 144 145 146 147 148 149 150
{
	D1(printk("About to refile bad block at %08x\n", jeb->offset));

	/* File the existing block on the bad_used_list.... */
	if (c->nextblock == jeb)
		c->nextblock = NULL;
	else /* Not sure this should ever happen... need more coffee */
		list_del(&jeb->list);
	if (jeb->first_node) {
		D1(printk("Refiling block at %08x to bad_used_list\n", jeb->offset));
		list_add(&jeb->list, &c->bad_used_list);
	} else {
E
Estelle Hammache 已提交
151
		BUG_ON(allow_empty == REFILE_NOTEMPTY);
L
Linus Torvalds 已提交
152 153 154 155 156 157 158
		/* It has to have had some nodes or we couldn't be here */
		D1(printk("Refiling block at %08x to erase_pending_list\n", jeb->offset));
		list_add(&jeb->list, &c->erase_pending_list);
		c->nr_erasing_blocks++;
		jffs2_erase_pending_trigger(c);
	}

159 160 161 162 163 164 165 166 167 168 169 170
	if (!jffs2_prealloc_raw_node_refs(c, jeb, 1)) {
		uint32_t oldfree = jeb->free_size;

		jffs2_link_node_ref(c, jeb, 
				    (jeb->offset+c->sector_size-oldfree) | REF_OBSOLETE,
				    oldfree, NULL);
		/* convert to wasted */
		c->wasted_size += oldfree;
		jeb->wasted_size += oldfree;
		c->dirty_size -= oldfree;
		jeb->dirty_size -= oldfree;
	}
L
Linus Torvalds 已提交
171

172 173 174
	jffs2_dbg_dump_block_lists_nolock(c);
	jffs2_dbg_acct_sanity_check_nolock(c,jeb);
	jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
L
Linus Torvalds 已提交
175 176
}

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
static struct jffs2_raw_node_ref **jffs2_incore_replace_raw(struct jffs2_sb_info *c,
							    struct jffs2_inode_info *f,
							    struct jffs2_raw_node_ref *raw,
							    union jffs2_node_union *node)
{
	struct jffs2_node_frag *frag;
	struct jffs2_full_dirent *fd;

	dbg_noderef("incore_replace_raw: node at %p is {%04x,%04x}\n",
		    node, je16_to_cpu(node->u.magic), je16_to_cpu(node->u.nodetype));

	BUG_ON(je16_to_cpu(node->u.magic) != 0x1985 &&
	       je16_to_cpu(node->u.magic) != 0);

	switch (je16_to_cpu(node->u.nodetype)) {
	case JFFS2_NODETYPE_INODE:
193 194 195 196
		if (f->metadata && f->metadata->raw == raw) {
			dbg_noderef("Will replace ->raw in f->metadata at %p\n", f->metadata);
			return &f->metadata->raw;
		}
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
		frag = jffs2_lookup_node_frag(&f->fragtree, je32_to_cpu(node->i.offset));
		BUG_ON(!frag);
		/* Find a frag which refers to the full_dnode we want to modify */
		while (!frag->node || frag->node->raw != raw) {
			frag = frag_next(frag);
			BUG_ON(!frag);
		}
		dbg_noderef("Will replace ->raw in full_dnode at %p\n", frag->node);
		return &frag->node->raw;

	case JFFS2_NODETYPE_DIRENT:
		for (fd = f->dents; fd; fd = fd->next) {
			if (fd->raw == raw) {
				dbg_noderef("Will replace ->raw in full_dirent at %p\n", fd);
				return &fd->raw;
			}
		}
		BUG();
215

216 217 218 219 220 221 222 223
	default:
		dbg_noderef("Don't care about replacing raw for nodetype %x\n",
			    je16_to_cpu(node->u.nodetype));
		break;
	}
	return NULL;
}

L
Linus Torvalds 已提交
224 225 226 227 228 229
/* Recover from failure to write wbuf. Recover the nodes up to the
 * wbuf, not the one which we were starting to try to write. */

static void jffs2_wbuf_recover(struct jffs2_sb_info *c)
{
	struct jffs2_eraseblock *jeb, *new_jeb;
230
	struct jffs2_raw_node_ref *raw, *next, *first_raw = NULL;
L
Linus Torvalds 已提交
231 232
	size_t retlen;
	int ret;
233
	int nr_refile = 0;
L
Linus Torvalds 已提交
234 235 236
	unsigned char *buf;
	uint32_t start, end, ofs, len;

237 238
	jeb = &c->blocks[c->wbuf_ofs / c->sector_size];

L
Linus Torvalds 已提交
239
	spin_lock(&c->erase_completion_lock);
E
Estelle Hammache 已提交
240
	jffs2_block_refile(c, jeb, REFILE_NOTEMPTY);
241 242 243
	spin_unlock(&c->erase_completion_lock);

	BUG_ON(!ref_obsolete(jeb->last_node));
L
Linus Torvalds 已提交
244 245 246

	/* Find the first node to be recovered, by skipping over every
	   node which ends before the wbuf starts, or which is obsolete. */
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
	for (next = raw = jeb->first_node; next; raw = next) {
		next = ref_next(raw);

		if (ref_obsolete(raw) || 
		    (next && ref_offset(next) <= c->wbuf_ofs)) {
			dbg_noderef("Skipping node at 0x%08x(%d)-0x%08x which is either before 0x%08x or obsolete\n",
				    ref_offset(raw), ref_flags(raw),
				    (ref_offset(raw) + ref_totlen(c, jeb, raw)),
				    c->wbuf_ofs);
			continue;
		}
		dbg_noderef("First node to be recovered is at 0x%08x(%d)-0x%08x\n",
			    ref_offset(raw), ref_flags(raw),
			    (ref_offset(raw) + ref_totlen(c, jeb, raw)));

		first_raw = raw;
		break;
	}

	if (!first_raw) {
L
Linus Torvalds 已提交
267 268
		/* All nodes were obsolete. Nothing to recover. */
		D1(printk(KERN_DEBUG "No non-obsolete nodes to be recovered. Just filing block bad\n"));
269
		c->wbuf_len = 0;
L
Linus Torvalds 已提交
270 271 272
		return;
	}

273 274 275
	start = ref_offset(first_raw);
	end = ref_offset(jeb->last_node);
	nr_refile = 1;
L
Linus Torvalds 已提交
276

277 278 279
	/* Count the number of refs which need to be copied */
	while ((raw = ref_next(raw)) != jeb->last_node)
		nr_refile++;
L
Linus Torvalds 已提交
280

281 282
	dbg_noderef("wbuf recover %08x-%08x (%d bytes in %d nodes)\n",
		    start, end, end - start, nr_refile);
L
Linus Torvalds 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295 296

	buf = NULL;
	if (start < c->wbuf_ofs) {
		/* First affected node was already partially written.
		 * Attempt to reread the old data into our buffer. */

		buf = kmalloc(end - start, GFP_KERNEL);
		if (!buf) {
			printk(KERN_CRIT "Malloc failure in wbuf recovery. Data loss ensues.\n");

			goto read_failed;
		}

		/* Do the read... */
297
		ret = c->mtd->read(c->mtd, start, c->wbuf_ofs - start, &retlen, buf);
298

299 300 301
		/* ECC recovered ? */
		if ((ret == -EUCLEAN || ret == -EBADMSG) &&
		    (retlen == c->wbuf_ofs - start))
L
Linus Torvalds 已提交
302
			ret = 0;
303

L
Linus Torvalds 已提交
304 305 306 307 308 309
		if (ret || retlen != c->wbuf_ofs - start) {
			printk(KERN_CRIT "Old data are already lost in wbuf recovery. Data loss ensues.\n");

			kfree(buf);
			buf = NULL;
		read_failed:
310 311 312 313 314 315 316
			first_raw = ref_next(first_raw);
			nr_refile--;
			while (first_raw && ref_obsolete(first_raw)) {
				first_raw = ref_next(first_raw);
				nr_refile--;
			}

L
Linus Torvalds 已提交
317
			/* If this was the only node to be recovered, give up */
318 319
			if (!first_raw) {
				c->wbuf_len = 0;
L
Linus Torvalds 已提交
320
				return;
321
			}
L
Linus Torvalds 已提交
322 323

			/* It wasn't. Go on and try to recover nodes complete in the wbuf */
324 325 326 327
			start = ref_offset(first_raw);
			dbg_noderef("wbuf now recover %08x-%08x (%d bytes in %d nodes)\n",
				    start, end, end - start, nr_refile);

L
Linus Torvalds 已提交
328 329 330 331 332 333 334 335 336
		} else {
			/* Read succeeded. Copy the remaining data from the wbuf */
			memcpy(buf + (c->wbuf_ofs - start), c->wbuf, end - c->wbuf_ofs);
		}
	}
	/* OK... we're to rewrite (end-start) bytes of data from first_raw onwards.
	   Either 'buf' contains the data, or we find it in the wbuf */

	/* ... and get an allocation of space from a shiny new block instead */
337
	ret = jffs2_reserve_space_gc(c, end-start, &len, JFFS2_SUMMARY_NOSUM_SIZE);
L
Linus Torvalds 已提交
338 339
	if (ret) {
		printk(KERN_WARNING "Failed to allocate space for wbuf recovery. Data loss ensues.\n");
E
Estelle Hammache 已提交
340
		kfree(buf);
L
Linus Torvalds 已提交
341 342
		return;
	}
343 344 345 346 347 348 349 350

	ret = jffs2_prealloc_raw_node_refs(c, c->nextblock, nr_refile);
	if (ret) {
		printk(KERN_WARNING "Failed to allocate node refs for wbuf recovery. Data loss ensues.\n");
		kfree(buf);
		return;
	}

351 352
	ofs = write_ofs(c);

L
Linus Torvalds 已提交
353
	if (end-start >= c->wbuf_pagesize) {
E
Estelle Hammache 已提交
354
		/* Need to do another write immediately, but it's possible
E
Estelle Hammache 已提交
355
		   that this is just because the wbuf itself is completely
356 357
		   full, and there's nothing earlier read back from the
		   flash. Hence 'buf' isn't necessarily what we're writing
E
Estelle Hammache 已提交
358
		   from. */
E
Estelle Hammache 已提交
359
		unsigned char *rewrite_buf = buf?:c->wbuf;
L
Linus Torvalds 已提交
360 361 362 363
		uint32_t towrite = (end-start) - ((end-start)%c->wbuf_pagesize);

		D1(printk(KERN_DEBUG "Write 0x%x bytes at 0x%08x in wbuf recover\n",
			  towrite, ofs));
364

L
Linus Torvalds 已提交
365 366 367 368 369
#ifdef BREAKMEHEADER
		static int breakme;
		if (breakme++ == 20) {
			printk(KERN_NOTICE "Faking write error at 0x%08x\n", ofs);
			breakme = 0;
370 371
			c->mtd->write(c->mtd, ofs, towrite, &retlen,
				      brokenbuf);
L
Linus Torvalds 已提交
372 373 374
			ret = -EIO;
		} else
#endif
375 376
			ret = c->mtd->write(c->mtd, ofs, towrite, &retlen,
					    rewrite_buf);
L
Linus Torvalds 已提交
377 378 379 380

		if (ret || retlen != towrite) {
			/* Argh. We tried. Really we did. */
			printk(KERN_CRIT "Recovery of wbuf failed due to a second write error\n");
E
Estelle Hammache 已提交
381
			kfree(buf);
L
Linus Torvalds 已提交
382

383
			if (retlen)
384
				jffs2_add_physical_node_ref(c, ofs | REF_OBSOLETE, ref_totlen(c, jeb, first_raw), NULL);
L
Linus Torvalds 已提交
385 386 387 388 389 390 391

			return;
		}
		printk(KERN_NOTICE "Recovery of wbuf succeeded to %08x\n", ofs);

		c->wbuf_len = (end - start) - towrite;
		c->wbuf_ofs = ofs + towrite;
E
Estelle Hammache 已提交
392
		memmove(c->wbuf, rewrite_buf + towrite, c->wbuf_len);
L
Linus Torvalds 已提交
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
		/* Don't muck about with c->wbuf_inodes. False positives are harmless. */
	} else {
		/* OK, now we're left with the dregs in whichever buffer we're using */
		if (buf) {
			memcpy(c->wbuf, buf, end-start);
		} else {
			memmove(c->wbuf, c->wbuf + (start - c->wbuf_ofs), end - start);
		}
		c->wbuf_ofs = ofs;
		c->wbuf_len = end - start;
	}

	/* Now sort out the jffs2_raw_node_refs, moving them from the old to the next block */
	new_jeb = &c->blocks[ofs / c->sector_size];

	spin_lock(&c->erase_completion_lock);
409 410 411 412 413 414
	for (raw = first_raw; raw != jeb->last_node; raw = ref_next(raw)) {
		uint32_t rawlen = ref_totlen(c, jeb, raw);
		struct jffs2_inode_cache *ic;
		struct jffs2_raw_node_ref *new_ref;
		struct jffs2_raw_node_ref **adjust_ref = NULL;
		struct jffs2_inode_info *f = NULL;
L
Linus Torvalds 已提交
415 416

		D1(printk(KERN_DEBUG "Refiling block of %08x at %08x(%d) to %08x\n",
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
			  rawlen, ref_offset(raw), ref_flags(raw), ofs));

		ic = jffs2_raw_ref_to_ic(raw);

		/* Ick. This XATTR mess should be fixed shortly... */
		if (ic && ic->class == RAWNODE_CLASS_XATTR_DATUM) {
			struct jffs2_xattr_datum *xd = (void *)ic;
			BUG_ON(xd->node != raw);
			adjust_ref = &xd->node;
			raw->next_in_ino = NULL;
			ic = NULL;
		} else if (ic && ic->class == RAWNODE_CLASS_XATTR_REF) {
			struct jffs2_xattr_datum *xr = (void *)ic;
			BUG_ON(xr->node != raw);
			adjust_ref = &xr->node;
			raw->next_in_ino = NULL;
			ic = NULL;
		} else if (ic && ic->class == RAWNODE_CLASS_INODE_CACHE) {
			struct jffs2_raw_node_ref **p = &ic->nodes;

			/* Remove the old node from the per-inode list */
			while (*p && *p != (void *)ic) {
				if (*p == raw) {
					(*p) = (raw->next_in_ino);
					raw->next_in_ino = NULL;
					break;
				}
				p = &((*p)->next_in_ino);
			}
L
Linus Torvalds 已提交
446

447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
			if (ic->state == INO_STATE_PRESENT && !ref_obsolete(raw)) {
				/* If it's an in-core inode, then we have to adjust any
				   full_dirent or full_dnode structure to point to the
				   new version instead of the old */
				f = jffs2_gc_fetch_inode(c, ic->ino, ic->nlink);
				if (IS_ERR(f)) {
					/* Should never happen; it _must_ be present */
					JFFS2_ERROR("Failed to iget() ino #%u, err %ld\n",
						    ic->ino, PTR_ERR(f));
					BUG();
				}
				/* We don't lock f->sem. There's a number of ways we could
				   end up in here with it already being locked, and nobody's
				   going to modify it on us anyway because we hold the
				   alloc_sem. We're only changing one ->raw pointer too,
				   which we can get away with without upsetting readers. */
				adjust_ref = jffs2_incore_replace_raw(c, f, raw,
								      (void *)(buf?:c->wbuf) + (ref_offset(raw) - start));
			} else if (unlikely(ic->state != INO_STATE_PRESENT &&
					    ic->state != INO_STATE_CHECKEDABSENT &&
					    ic->state != INO_STATE_GC)) {
				JFFS2_ERROR("Inode #%u is in strange state %d!\n", ic->ino, ic->state);
				BUG();
			}
		}

		new_ref = jffs2_link_node_ref(c, new_jeb, ofs | ref_flags(raw), rawlen, ic);

		if (adjust_ref) {
			BUG_ON(*adjust_ref != raw);
			*adjust_ref = new_ref;
		}
		if (f)
			jffs2_gc_release_inode(c, f);

		if (!ref_obsolete(raw)) {
L
Linus Torvalds 已提交
483 484 485
			jeb->dirty_size += rawlen;
			jeb->used_size  -= rawlen;
			c->dirty_size += rawlen;
486 487 488
			c->used_size -= rawlen;
			raw->flash_offset = ref_offset(raw) | REF_OBSOLETE;
			BUG_ON(raw->next_in_ino);
L
Linus Torvalds 已提交
489 490 491 492
		}
		ofs += rawlen;
	}

493 494
	kfree(buf);

L
Linus Torvalds 已提交
495
	/* Fix up the original jeb now it's on the bad_list */
496
	if (first_raw == jeb->first_node) {
L
Linus Torvalds 已提交
497 498 499 500 501 502 503
		D1(printk(KERN_DEBUG "Failing block at %08x is now empty. Moving to erase_pending_list\n", jeb->offset));
		list_del(&jeb->list);
		list_add(&jeb->list, &c->erase_pending_list);
		c->nr_erasing_blocks++;
		jffs2_erase_pending_trigger(c);
	}

504
	jffs2_dbg_acct_sanity_check_nolock(c, jeb);
505
	jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
L
Linus Torvalds 已提交
506

507
	jffs2_dbg_acct_sanity_check_nolock(c, new_jeb);
508
	jffs2_dbg_acct_paranoia_check_nolock(c, new_jeb);
L
Linus Torvalds 已提交
509 510 511

	spin_unlock(&c->erase_completion_lock);

512 513
	D1(printk(KERN_DEBUG "wbuf recovery completed OK. wbuf_ofs 0x%08x, len 0x%x\n", c->wbuf_ofs, c->wbuf_len));

L
Linus Torvalds 已提交
514 515 516 517 518 519 520 521 522 523 524 525 526
}

/* Meaning of pad argument:
   0: Do not pad. Probably pointless - we only ever use this when we can't pad anyway.
   1: Pad, do not adjust nextblock free_size
   2: Pad, adjust nextblock free_size
*/
#define NOPAD		0
#define PAD_NOACCOUNT	1
#define PAD_ACCOUNTING	2

static int __jffs2_flush_wbuf(struct jffs2_sb_info *c, int pad)
{
527
	struct jffs2_eraseblock *wbuf_jeb;
L
Linus Torvalds 已提交
528 529 530
	int ret;
	size_t retlen;

531
	/* Nothing to do if not write-buffering the flash. In particular, we shouldn't
L
Linus Torvalds 已提交
532
	   del_timer() the timer we never initialised. */
533
	if (!jffs2_is_writebuffered(c))
L
Linus Torvalds 已提交
534 535 536 537 538 539 540 541
		return 0;

	if (!down_trylock(&c->alloc_sem)) {
		up(&c->alloc_sem);
		printk(KERN_CRIT "jffs2_flush_wbuf() called with alloc_sem not locked!\n");
		BUG();
	}

542
	if (!c->wbuf_len)	/* already checked c->wbuf above */
L
Linus Torvalds 已提交
543 544
		return 0;

545 546
	wbuf_jeb = &c->blocks[c->wbuf_ofs / c->sector_size];
	if (jffs2_prealloc_raw_node_refs(c, wbuf_jeb, c->nextblock->allocated_refs + 1))
547 548
		return -ENOMEM;

L
Linus Torvalds 已提交
549 550 551 552
	/* claim remaining space on the page
	   this happens, if we have a change to a new block,
	   or if fsync forces us to flush the writebuffer.
	   if we have a switch to next page, we will not have
553
	   enough remaining space for this.
L
Linus Torvalds 已提交
554
	*/
555
	if (pad ) {
L
Linus Torvalds 已提交
556 557 558 559 560
		c->wbuf_len = PAD(c->wbuf_len);

		/* Pad with JFFS2_DIRTY_BITMASK initially.  this helps out ECC'd NOR
		   with 8 byte page size */
		memset(c->wbuf + c->wbuf_len, 0, c->wbuf_pagesize - c->wbuf_len);
561

L
Linus Torvalds 已提交
562 563 564 565 566 567 568 569 570 571
		if ( c->wbuf_len + sizeof(struct jffs2_unknown_node) < c->wbuf_pagesize) {
			struct jffs2_unknown_node *padnode = (void *)(c->wbuf + c->wbuf_len);
			padnode->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
			padnode->nodetype = cpu_to_je16(JFFS2_NODETYPE_PADDING);
			padnode->totlen = cpu_to_je32(c->wbuf_pagesize - c->wbuf_len);
			padnode->hdr_crc = cpu_to_je32(crc32(0, padnode, sizeof(*padnode)-4));
		}
	}
	/* else jffs2_flash_writev has actually filled in the rest of the
	   buffer for us, and will deal with the node refs etc. later. */
572

L
Linus Torvalds 已提交
573 574 575 576 577
#ifdef BREAKME
	static int breakme;
	if (breakme++ == 20) {
		printk(KERN_NOTICE "Faking write error at 0x%08x\n", c->wbuf_ofs);
		breakme = 0;
578 579
		c->mtd->write(c->mtd, c->wbuf_ofs, c->wbuf_pagesize, &retlen,
			      brokenbuf);
L
Linus Torvalds 已提交
580
		ret = -EIO;
581
	} else
L
Linus Torvalds 已提交
582
#endif
583

L
Linus Torvalds 已提交
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
		ret = c->mtd->write(c->mtd, c->wbuf_ofs, c->wbuf_pagesize, &retlen, c->wbuf);

	if (ret || retlen != c->wbuf_pagesize) {
		if (ret)
			printk(KERN_WARNING "jffs2_flush_wbuf(): Write failed with %d\n",ret);
		else {
			printk(KERN_WARNING "jffs2_flush_wbuf(): Write was short: %zd instead of %d\n",
				retlen, c->wbuf_pagesize);
			ret = -EIO;
		}

		jffs2_wbuf_recover(c);

		return ret;
	}

	/* Adjust free size of the block if we padded. */
601
	if (pad) {
602
		uint32_t waste = c->wbuf_pagesize - c->wbuf_len;
L
Linus Torvalds 已提交
603 604

		D1(printk(KERN_DEBUG "jffs2_flush_wbuf() adjusting free_size of %sblock at %08x\n",
605
			  (wbuf_jeb==c->nextblock)?"next":"", wbuf_jeb->offset));
L
Linus Torvalds 已提交
606

607
		/* wbuf_pagesize - wbuf_len is the amount of space that's to be
L
Linus Torvalds 已提交
608 609
		   padded. If there is less free space in the block than that,
		   something screwed up */
610
		if (wbuf_jeb->free_size < waste) {
L
Linus Torvalds 已提交
611
			printk(KERN_CRIT "jffs2_flush_wbuf(): Accounting error. wbuf at 0x%08x has 0x%03x bytes, 0x%03x left.\n",
612
			       c->wbuf_ofs, c->wbuf_len, waste);
L
Linus Torvalds 已提交
613
			printk(KERN_CRIT "jffs2_flush_wbuf(): But free_size for block at 0x%08x is only 0x%08x\n",
614
			       wbuf_jeb->offset, wbuf_jeb->free_size);
L
Linus Torvalds 已提交
615 616
			BUG();
		}
617 618 619

		spin_lock(&c->erase_completion_lock);

620
		jffs2_link_node_ref(c, wbuf_jeb, (c->wbuf_ofs + c->wbuf_len) | REF_OBSOLETE, waste, NULL);
621
		/* FIXME: that made it count as dirty. Convert to wasted */
622
		wbuf_jeb->dirty_size -= waste;
623
		c->dirty_size -= waste;
624
		wbuf_jeb->wasted_size += waste;
625 626 627
		c->wasted_size += waste;
	} else
		spin_lock(&c->erase_completion_lock);
L
Linus Torvalds 已提交
628 629 630 631 632 633 634 635 636 637 638 639 640

	/* Stick any now-obsoleted blocks on the erase_pending_list */
	jffs2_refile_wbuf_blocks(c);
	jffs2_clear_wbuf_ino_list(c);
	spin_unlock(&c->erase_completion_lock);

	memset(c->wbuf,0xff,c->wbuf_pagesize);
	/* adjust write buffer offset, else we get a non contiguous write bug */
	c->wbuf_ofs += c->wbuf_pagesize;
	c->wbuf_len = 0;
	return 0;
}

641
/* Trigger garbage collection to flush the write-buffer.
L
Linus Torvalds 已提交
642
   If ino arg is zero, do it if _any_ real (i.e. not GC) writes are
643
   outstanding. If ino arg non-zero, do it only if a write for the
L
Linus Torvalds 已提交
644 645 646 647 648 649 650 651 652
   given inode is outstanding. */
int jffs2_flush_wbuf_gc(struct jffs2_sb_info *c, uint32_t ino)
{
	uint32_t old_wbuf_ofs;
	uint32_t old_wbuf_len;
	int ret = 0;

	D1(printk(KERN_DEBUG "jffs2_flush_wbuf_gc() called for ino #%u...\n", ino));

653 654 655
	if (!c->wbuf)
		return 0;

L
Linus Torvalds 已提交
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
	down(&c->alloc_sem);
	if (!jffs2_wbuf_pending_for_ino(c, ino)) {
		D1(printk(KERN_DEBUG "Ino #%d not pending in wbuf. Returning\n", ino));
		up(&c->alloc_sem);
		return 0;
	}

	old_wbuf_ofs = c->wbuf_ofs;
	old_wbuf_len = c->wbuf_len;

	if (c->unchecked_size) {
		/* GC won't make any progress for a while */
		D1(printk(KERN_DEBUG "jffs2_flush_wbuf_gc() padding. Not finished checking\n"));
		down_write(&c->wbuf_sem);
		ret = __jffs2_flush_wbuf(c, PAD_ACCOUNTING);
E
Estelle Hammache 已提交
671 672 673 674
		/* retry flushing wbuf in case jffs2_wbuf_recover
		   left some data in the wbuf */
		if (ret)
			ret = __jffs2_flush_wbuf(c, PAD_ACCOUNTING);
L
Linus Torvalds 已提交
675 676 677 678 679 680 681 682 683 684 685 686 687 688
		up_write(&c->wbuf_sem);
	} else while (old_wbuf_len &&
		      old_wbuf_ofs == c->wbuf_ofs) {

		up(&c->alloc_sem);

		D1(printk(KERN_DEBUG "jffs2_flush_wbuf_gc() calls gc pass\n"));

		ret = jffs2_garbage_collect_pass(c);
		if (ret) {
			/* GC failed. Flush it with padding instead */
			down(&c->alloc_sem);
			down_write(&c->wbuf_sem);
			ret = __jffs2_flush_wbuf(c, PAD_ACCOUNTING);
E
Estelle Hammache 已提交
689 690 691 692
			/* retry flushing wbuf in case jffs2_wbuf_recover
			   left some data in the wbuf */
			if (ret)
				ret = __jffs2_flush_wbuf(c, PAD_ACCOUNTING);
L
Linus Torvalds 已提交
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
			up_write(&c->wbuf_sem);
			break;
		}
		down(&c->alloc_sem);
	}

	D1(printk(KERN_DEBUG "jffs2_flush_wbuf_gc() ends...\n"));

	up(&c->alloc_sem);
	return ret;
}

/* Pad write-buffer to end and write it, wasting space. */
int jffs2_flush_wbuf_pad(struct jffs2_sb_info *c)
{
	int ret;

710 711 712
	if (!c->wbuf)
		return 0;

L
Linus Torvalds 已提交
713 714
	down_write(&c->wbuf_sem);
	ret = __jffs2_flush_wbuf(c, PAD_NOACCOUNT);
E
Estelle Hammache 已提交
715 716 717
	/* retry - maybe wbuf recover left some data in wbuf. */
	if (ret)
		ret = __jffs2_flush_wbuf(c, PAD_NOACCOUNT);
L
Linus Torvalds 已提交
718 719 720 721
	up_write(&c->wbuf_sem);

	return ret;
}
722 723 724

static size_t jffs2_fill_wbuf(struct jffs2_sb_info *c, const uint8_t *buf,
			      size_t len)
L
Linus Torvalds 已提交
725
{
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
	if (len && !c->wbuf_len && (len >= c->wbuf_pagesize))
		return 0;

	if (len > (c->wbuf_pagesize - c->wbuf_len))
		len = c->wbuf_pagesize - c->wbuf_len;
	memcpy(c->wbuf + c->wbuf_len, buf, len);
	c->wbuf_len += (uint32_t) len;
	return len;
}

int jffs2_flash_writev(struct jffs2_sb_info *c, const struct kvec *invecs,
		       unsigned long count, loff_t to, size_t *retlen,
		       uint32_t ino)
{
	struct jffs2_eraseblock *jeb;
	size_t wbuf_retlen, donelen = 0;
L
Linus Torvalds 已提交
742
	uint32_t outvec_to = to;
743
	int ret, invec;
L
Linus Torvalds 已提交
744

745
	/* If not writebuffered flash, don't bother */
746
	if (!jffs2_is_writebuffered(c))
L
Linus Torvalds 已提交
747
		return jffs2_flash_direct_writev(c, invecs, count, to, retlen);
748

L
Linus Torvalds 已提交
749 750 751 752 753
	down_write(&c->wbuf_sem);

	/* If wbuf_ofs is not initialized, set it to target address */
	if (c->wbuf_ofs == 0xFFFFFFFF) {
		c->wbuf_ofs = PAGE_DIV(to);
754
		c->wbuf_len = PAGE_MOD(to);
L
Linus Torvalds 已提交
755 756 757
		memset(c->wbuf,0xff,c->wbuf_pagesize);
	}

758 759 760 761 762 763 764
	/*
	 * Sanity checks on target address.  It's permitted to write
	 * at PAD(c->wbuf_len+c->wbuf_ofs), and it's permitted to
	 * write at the beginning of a new erase block. Anything else,
	 * and you die.  New block starts at xxx000c (0-b = block
	 * header)
	 */
765
	if (SECTOR_ADDR(to) != SECTOR_ADDR(c->wbuf_ofs)) {
L
Linus Torvalds 已提交
766 767
		/* It's a write to a new block */
		if (c->wbuf_len) {
768 769 770
			D1(printk(KERN_DEBUG "jffs2_flash_writev() to 0x%lx "
				  "causes flush of wbuf at 0x%08x\n",
				  (unsigned long)to, c->wbuf_ofs));
L
Linus Torvalds 已提交
771
			ret = __jffs2_flush_wbuf(c, PAD_NOACCOUNT);
772 773
			if (ret)
				goto outerr;
L
Linus Torvalds 已提交
774 775 776
		}
		/* set pointer to new block */
		c->wbuf_ofs = PAGE_DIV(to);
777 778
		c->wbuf_len = PAGE_MOD(to);
	}
L
Linus Torvalds 已提交
779 780 781

	if (to != PAD(c->wbuf_ofs + c->wbuf_len)) {
		/* We're not writing immediately after the writebuffer. Bad. */
782 783
		printk(KERN_CRIT "jffs2_flash_writev(): Non-contiguous write "
		       "to %08lx\n", (unsigned long)to);
L
Linus Torvalds 已提交
784 785
		if (c->wbuf_len)
			printk(KERN_CRIT "wbuf was previously %08x-%08x\n",
786
			       c->wbuf_ofs, c->wbuf_ofs+c->wbuf_len);
L
Linus Torvalds 已提交
787 788 789
		BUG();
	}

790 791 792 793 794 795 796 797 798
	/* adjust alignment offset */
	if (c->wbuf_len != PAGE_MOD(to)) {
		c->wbuf_len = PAGE_MOD(to);
		/* take care of alignment to next page */
		if (!c->wbuf_len) {
			c->wbuf_len = c->wbuf_pagesize;
			ret = __jffs2_flush_wbuf(c, NOPAD);
			if (ret)
				goto outerr;
L
Linus Torvalds 已提交
799 800 801
		}
	}

802 803 804
	for (invec = 0; invec < count; invec++) {
		int vlen = invecs[invec].iov_len;
		uint8_t *v = invecs[invec].iov_base;
E
Estelle Hammache 已提交
805

806
		wbuf_retlen = jffs2_fill_wbuf(c, v, vlen);
E
Estelle Hammache 已提交
807

808 809 810 811
		if (c->wbuf_len == c->wbuf_pagesize) {
			ret = __jffs2_flush_wbuf(c, NOPAD);
			if (ret)
				goto outerr;
L
Linus Torvalds 已提交
812
		}
813 814
		vlen -= wbuf_retlen;
		outvec_to += wbuf_retlen;
L
Linus Torvalds 已提交
815
		donelen += wbuf_retlen;
816 817 818 819 820 821 822 823 824 825 826 827 828
		v += wbuf_retlen;

		if (vlen >= c->wbuf_pagesize) {
			ret = c->mtd->write(c->mtd, outvec_to, PAGE_DIV(vlen),
					    &wbuf_retlen, v);
			if (ret < 0 || wbuf_retlen != PAGE_DIV(vlen))
				goto outfile;

			vlen -= wbuf_retlen;
			outvec_to += wbuf_retlen;
			c->wbuf_ofs = outvec_to;
			donelen += wbuf_retlen;
			v += wbuf_retlen;
L
Linus Torvalds 已提交
829 830
		}

831 832 833 834 835 836
		wbuf_retlen = jffs2_fill_wbuf(c, v, vlen);
		if (c->wbuf_len == c->wbuf_pagesize) {
			ret = __jffs2_flush_wbuf(c, NOPAD);
			if (ret)
				goto outerr;
		}
L
Linus Torvalds 已提交
837

838 839
		outvec_to += wbuf_retlen;
		donelen += wbuf_retlen;
L
Linus Torvalds 已提交
840 841
	}

842 843 844 845
	/*
	 * If there's a remainder in the wbuf and it's a non-GC write,
	 * remember that the wbuf affects this ino
	 */
L
Linus Torvalds 已提交
846 847
	*retlen = donelen;

848 849 850 851 852 853
	if (jffs2_sum_active()) {
		int res = jffs2_sum_add_kvec(c, invecs, count, (uint32_t) to);
		if (res)
			return res;
	}

L
Linus Torvalds 已提交
854 855 856 857
	if (c->wbuf_len && ino)
		jffs2_wbuf_dirties_inode(c, ino);

	ret = 0;
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
	up_write(&c->wbuf_sem);
	return ret;

outfile:
	/*
	 * At this point we have no problem, c->wbuf is empty. However
	 * refile nextblock to avoid writing again to same address.
	 */

	spin_lock(&c->erase_completion_lock);

	jeb = &c->blocks[outvec_to / c->sector_size];
	jffs2_block_refile(c, jeb, REFILE_ANYWAY);

	spin_unlock(&c->erase_completion_lock);
873

874 875
outerr:
	*retlen = 0;
L
Linus Torvalds 已提交
876 877 878 879 880 881 882 883
	up_write(&c->wbuf_sem);
	return ret;
}

/*
 *	This is the entry for flash write.
 *	Check, if we work on NAND FLASH, if so build an kvec and write it via vritev
*/
884 885
int jffs2_flash_write(struct jffs2_sb_info *c, loff_t ofs, size_t len,
		      size_t *retlen, const u_char *buf)
L
Linus Torvalds 已提交
886 887 888
{
	struct kvec vecs[1];

889
	if (!jffs2_is_writebuffered(c))
890
		return jffs2_flash_direct_write(c, ofs, len, retlen, buf);
L
Linus Torvalds 已提交
891 892 893 894 895 896 897 898 899 900 901 902 903 904

	vecs[0].iov_base = (unsigned char *) buf;
	vecs[0].iov_len = len;
	return jffs2_flash_writev(c, vecs, 1, ofs, retlen, 0);
}

/*
	Handle readback from writebuffer and ECC failure return
*/
int jffs2_flash_read(struct jffs2_sb_info *c, loff_t ofs, size_t len, size_t *retlen, u_char *buf)
{
	loff_t	orbf = 0, owbf = 0, lwbf = 0;
	int	ret;

905
	if (!jffs2_is_writebuffered(c))
L
Linus Torvalds 已提交
906 907
		return c->mtd->read(c->mtd, ofs, len, retlen, buf);

908
	/* Read flash */
909
	down_read(&c->wbuf_sem);
910
	ret = c->mtd->read(c->mtd, ofs, len, retlen, buf);
911

912 913 914 915
	if ( (ret == -EBADMSG || ret == -EUCLEAN) && (*retlen == len) ) {
		if (ret == -EBADMSG)
			printk(KERN_WARNING "mtd->read(0x%zx bytes from 0x%llx)"
			       " returned ECC error\n", len, ofs);
916
		/*
917 918 919 920 921 922 923
		 * We have the raw data without ECC correction in the buffer,
		 * maybe we are lucky and all data or parts are correct. We
		 * check the node.  If data are corrupted node check will sort
		 * it out.  We keep this block, it will fail on write or erase
		 * and the we mark it bad. Or should we do that now? But we
		 * should give him a chance.  Maybe we had a system crash or
		 * power loss before the ecc write or a erase was completed.
924 925
		 * So we return success. :)
		 */
926
		ret = 0;
927
	}
928

L
Linus Torvalds 已提交
929 930
	/* if no writebuffer available or write buffer empty, return */
	if (!c->wbuf_pagesize || !c->wbuf_len)
931
		goto exit;
L
Linus Torvalds 已提交
932 933

	/* if we read in a different block, return */
934
	if (SECTOR_ADDR(ofs) != SECTOR_ADDR(c->wbuf_ofs))
935
		goto exit;
L
Linus Torvalds 已提交
936 937 938 939 940 941

	if (ofs >= c->wbuf_ofs) {
		owbf = (ofs - c->wbuf_ofs);	/* offset in write buffer */
		if (owbf > c->wbuf_len)		/* is read beyond write buffer ? */
			goto exit;
		lwbf = c->wbuf_len - owbf;	/* number of bytes to copy */
942
		if (lwbf > len)
L
Linus Torvalds 已提交
943
			lwbf = len;
944
	} else {
L
Linus Torvalds 已提交
945 946 947
		orbf = (c->wbuf_ofs - ofs);	/* offset in read buffer */
		if (orbf > len)			/* is write beyond write buffer ? */
			goto exit;
948
		lwbf = len - orbf;		/* number of bytes to copy */
949
		if (lwbf > c->wbuf_len)
L
Linus Torvalds 已提交
950
			lwbf = c->wbuf_len;
951
	}
L
Linus Torvalds 已提交
952 953 954 955 956 957 958 959
	if (lwbf > 0)
		memcpy(buf+orbf,c->wbuf+owbf,lwbf);

exit:
	up_read(&c->wbuf_sem);
	return ret;
}

960 961
#define NR_OOB_SCAN_PAGES	4

L
Linus Torvalds 已提交
962
/*
963
 * Check, if the out of band area is empty
L
Linus Torvalds 已提交
964
 */
965 966
int jffs2_check_oob_empty(struct jffs2_sb_info *c,
			  struct jffs2_eraseblock *jeb, int mode)
L
Linus Torvalds 已提交
967
{
968 969 970 971 972 973 974 975 976 977 978 979
	int i, page, ret;
	int oobsize = c->mtd->oobsize;
	struct mtd_oob_ops ops;

	ops.len = NR_OOB_SCAN_PAGES * oobsize;
	ops.ooblen = oobsize;
	ops.oobbuf = c->oobbuf;
	ops.ooboffs = 0;
	ops.datbuf = NULL;
	ops.mode = MTD_OOB_PLACE;

	ret = c->mtd->read_oob(c->mtd, jeb->offset, &ops);
L
Linus Torvalds 已提交
980
	if (ret) {
981 982 983
		D1(printk(KERN_WARNING "jffs2_check_oob_empty(): Read OOB "
			  "failed %d for block at %08x\n", ret, jeb->offset));
		return ret;
L
Linus Torvalds 已提交
984
	}
985

986 987 988 989 990
	if (ops.retlen < ops.len) {
		D1(printk(KERN_WARNING "jffs2_check_oob_empty(): Read OOB "
			  "returned short read (%zd bytes not %d) for block "
			  "at %08x\n", ops.retlen, ops.len, jeb->offset));
		return -EIO;
L
Linus Torvalds 已提交
991
	}
992

L
Linus Torvalds 已提交
993
	/* Special check for first page */
994
	for(i = 0; i < oobsize ; i++) {
L
Linus Torvalds 已提交
995
		/* Yeah, we know about the cleanmarker. */
996
		if (mode && i >= c->fsdata_pos &&
L
Linus Torvalds 已提交
997 998 999
		    i < c->fsdata_pos + c->fsdata_len)
			continue;

1000 1001 1002 1003
		if (ops.oobbuf[i] != 0xFF) {
			D2(printk(KERN_DEBUG "Found %02x at %x in OOB for "
				  "%08x\n", ops.oobbuf[i], i, jeb->offset));
			return 1;
L
Linus Torvalds 已提交
1004 1005 1006
		}
	}

1007
	/* we know, we are aligned :) */
1008 1009 1010 1011
	for (page = oobsize; page < ops.len; page += sizeof(long)) {
		long dat = *(long *)(&ops.oobbuf[page]);
		if(dat != -1)
			return 1;
L
Linus Torvalds 已提交
1012
	}
1013
	return 0;
L
Linus Torvalds 已提交
1014 1015 1016
}

/*
1017 1018 1019 1020
 * Scan for a valid cleanmarker and for bad blocks
 */
int jffs2_check_nand_cleanmarker (struct jffs2_sb_info *c,
				  struct jffs2_eraseblock *jeb)
L
Linus Torvalds 已提交
1021 1022
{
	struct jffs2_unknown_node n;
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
	struct mtd_oob_ops ops;
	int oobsize = c->mtd->oobsize;
	unsigned char *p,*b;
	int i, ret;
	size_t offset = jeb->offset;

	/* Check first if the block is bad. */
	if (c->mtd->block_isbad(c->mtd, offset)) {
		D1 (printk(KERN_WARNING "jffs2_check_nand_cleanmarker()"
			   ": Bad block at %08x\n", jeb->offset));
		return 2;
	}
L
Linus Torvalds 已提交
1035

1036 1037 1038 1039 1040 1041
	ops.len = oobsize;
	ops.ooblen = oobsize;
	ops.oobbuf = c->oobbuf;
	ops.ooboffs = 0;
	ops.datbuf = NULL;
	ops.mode = MTD_OOB_PLACE;
L
Linus Torvalds 已提交
1042

1043 1044 1045 1046 1047 1048 1049
	ret = c->mtd->read_oob(c->mtd, offset, &ops);
	if (ret) {
		D1 (printk(KERN_WARNING "jffs2_check_nand_cleanmarker(): "
			   "Read OOB failed %d for block at %08x\n",
			   ret, jeb->offset));
		return ret;
	}
L
Linus Torvalds 已提交
1050

1051 1052 1053 1054 1055 1056
	if (ops.retlen < ops.len) {
		D1 (printk (KERN_WARNING "jffs2_check_nand_cleanmarker(): "
			    "Read OOB return short read (%zd bytes not %d) "
			    "for block at %08x\n", ops.retlen, ops.len,
			    jeb->offset));
		return -EIO;
L
Linus Torvalds 已提交
1057
	}
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079

	n.magic = cpu_to_je16 (JFFS2_MAGIC_BITMASK);
	n.nodetype = cpu_to_je16 (JFFS2_NODETYPE_CLEANMARKER);
	n.totlen = cpu_to_je32 (8);
	p = (unsigned char *) &n;
	b = c->oobbuf + c->fsdata_pos;

	for (i = c->fsdata_len; i; i--) {
		if (*b++ != *p++)
			ret = 1;
	}

	D1(if (ret == 1) {
		printk(KERN_WARNING "jffs2_check_nand_cleanmarker(): "
		       "Cleanmarker node not detected in block at %08x\n",
		       offset);
		printk(KERN_WARNING "OOB at %08zx was ", offset);
		for (i=0; i < oobsize; i++)
			printk("%02x ", c->oobbuf[i]);
		printk("\n");
	});
	return ret;
L
Linus Torvalds 已提交
1080 1081
}

1082 1083
int jffs2_write_nand_cleanmarker(struct jffs2_sb_info *c,
				 struct jffs2_eraseblock *jeb)
L
Linus Torvalds 已提交
1084
{
1085 1086 1087
	struct jffs2_unknown_node n;
	int	ret;
	struct mtd_oob_ops ops;
L
Linus Torvalds 已提交
1088 1089 1090 1091 1092

	n.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
	n.nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER);
	n.totlen = cpu_to_je32(8);

1093 1094 1095 1096 1097 1098 1099 1100
	ops.len = c->fsdata_len;
	ops.ooblen = c->fsdata_len;;
	ops.oobbuf = (uint8_t *)&n;
	ops.ooboffs = c->fsdata_pos;
	ops.datbuf = NULL;
	ops.mode = MTD_OOB_PLACE;

	ret = c->mtd->write_oob(c->mtd, jeb->offset, &ops);
1101

L
Linus Torvalds 已提交
1102
	if (ret) {
1103 1104 1105
		D1(printk(KERN_WARNING "jffs2_write_nand_cleanmarker(): "
			  "Write failed for block at %08x: error %d\n",
			  jeb->offset, ret));
L
Linus Torvalds 已提交
1106 1107
		return ret;
	}
1108 1109 1110 1111 1112
	if (ops.retlen != ops.len) {
		D1(printk(KERN_WARNING "jffs2_write_nand_cleanmarker(): "
			  "Short write for block at %08x: %zd not %d\n",
			  jeb->offset, ops.retlen, ops.len));
		return -EIO;
L
Linus Torvalds 已提交
1113 1114 1115 1116
	}
	return 0;
}

1117
/*
L
Linus Torvalds 已提交
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
 * On NAND we try to mark this block bad. If the block was erased more
 * than MAX_ERASE_FAILURES we mark it finaly bad.
 * Don't care about failures. This block remains on the erase-pending
 * or badblock list as long as nobody manipulates the flash with
 * a bootloader or something like that.
 */

int jffs2_write_nand_badblock(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset)
{
	int 	ret;

	/* if the count is < max, we try to write the counter to the 2nd page oob area */
	if( ++jeb->bad_count < MAX_ERASE_FAILURES)
		return 0;

	if (!c->mtd->block_markbad)
		return 1; // What else can we do?

	D1(printk(KERN_WARNING "jffs2_write_nand_badblock(): Marking bad block at %08x\n", bad_offset));
	ret = c->mtd->block_markbad(c->mtd, bad_offset);
1138

L
Linus Torvalds 已提交
1139 1140 1141 1142 1143 1144 1145 1146 1147
	if (ret) {
		D1(printk(KERN_WARNING "jffs2_write_nand_badblock(): Write failed for block at %08x: error %d\n", jeb->offset, ret));
		return ret;
	}
	return 1;
}

static int jffs2_nand_set_oobinfo(struct jffs2_sb_info *c)
{
1148
	struct nand_ecclayout *oinfo = c->mtd->ecclayout;
L
Linus Torvalds 已提交
1149 1150 1151 1152

	/* Do this only, if we have an oob buffer */
	if (!c->mtd->oobsize)
		return 0;
1153

L
Linus Torvalds 已提交
1154 1155 1156 1157
	/* Cleanmarker is out-of-band, so inline size zero */
	c->cleanmarker_size = 0;

	/* Should we use autoplacement ? */
1158 1159 1160 1161
	if (!oinfo) {
		D1(printk(KERN_DEBUG "JFFS2 on NAND. No autoplacment info found\n"));
		return -EINVAL;
	}
1162

1163 1164 1165 1166 1167 1168
	D1(printk(KERN_DEBUG "JFFS2 using autoplace on NAND\n"));
	/* Get the position of the free bytes */
	if (!oinfo->oobfree[0].length) {
		printk (KERN_WARNING "jffs2_nand_set_oobinfo(): Eeep."
			" Autoplacement selected and no empty space in oob\n");
		return -ENOSPC;
L
Linus Torvalds 已提交
1169
	}
1170 1171 1172 1173 1174
	c->fsdata_pos = oinfo->oobfree[0].offset;
	c->fsdata_len = oinfo->oobfree[0].length;
	if (c->fsdata_len > 8)
		c->fsdata_len = 8;

L
Linus Torvalds 已提交
1175 1176 1177 1178 1179 1180 1181 1182 1183
	return 0;
}

int jffs2_nand_flash_setup(struct jffs2_sb_info *c)
{
	int res;

	/* Initialise write buffer */
	init_rwsem(&c->wbuf_sem);
J
Joern Engel 已提交
1184
	c->wbuf_pagesize = c->mtd->writesize;
L
Linus Torvalds 已提交
1185
	c->wbuf_ofs = 0xFFFFFFFF;
1186

L
Linus Torvalds 已提交
1187 1188 1189 1190
	c->wbuf = kmalloc(c->wbuf_pagesize, GFP_KERNEL);
	if (!c->wbuf)
		return -ENOMEM;

1191 1192 1193 1194
	c->oobbuf = kmalloc(NR_OOB_SCAN_PAGES * c->mtd->oobsize, GFP_KERNEL);
	if (!c->oobbuf)
		return -ENOMEM;

L
Linus Torvalds 已提交
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
	res = jffs2_nand_set_oobinfo(c);

#ifdef BREAKME
	if (!brokenbuf)
		brokenbuf = kmalloc(c->wbuf_pagesize, GFP_KERNEL);
	if (!brokenbuf) {
		kfree(c->wbuf);
		return -ENOMEM;
	}
	memset(brokenbuf, 0xdb, c->wbuf_pagesize);
#endif
	return res;
}

void jffs2_nand_flash_cleanup(struct jffs2_sb_info *c)
{
	kfree(c->wbuf);
1212
	kfree(c->oobbuf);
L
Linus Torvalds 已提交
1213 1214
}

1215 1216
int jffs2_dataflash_setup(struct jffs2_sb_info *c) {
	c->cleanmarker_size = 0;		/* No cleanmarkers needed */
1217

1218 1219 1220
	/* Initialize write buffer */
	init_rwsem(&c->wbuf_sem);

1221

1222
	c->wbuf_pagesize =  c->mtd->erasesize;
1223

1224 1225 1226 1227 1228 1229 1230 1231 1232
	/* Find a suitable c->sector_size
	 * - Not too much sectors
	 * - Sectors have to be at least 4 K + some bytes
	 * - All known dataflashes have erase sizes of 528 or 1056
	 * - we take at least 8 eraseblocks and want to have at least 8K size
	 * - The concatenation should be a power of 2
	*/

	c->sector_size = 8 * c->mtd->erasesize;
1233

1234 1235 1236
	while (c->sector_size < 8192) {
		c->sector_size *= 2;
	}
1237

1238 1239
	/* It may be necessary to adjust the flash size */
	c->flash_size = c->mtd->size;
1240

1241 1242 1243 1244
	if ((c->flash_size % c->sector_size) != 0) {
		c->flash_size = (c->flash_size / c->sector_size) * c->sector_size;
		printk(KERN_WARNING "JFFS2 flash size adjusted to %dKiB\n", c->flash_size);
	};
1245

1246
	c->wbuf_ofs = 0xFFFFFFFF;
1247 1248 1249 1250
	c->wbuf = kmalloc(c->wbuf_pagesize, GFP_KERNEL);
	if (!c->wbuf)
		return -ENOMEM;

1251
	printk(KERN_INFO "JFFS2 write-buffering enabled buffer (%d) erasesize (%d)\n", c->wbuf_pagesize, c->sector_size);
1252 1253 1254 1255 1256 1257 1258 1259

	return 0;
}

void jffs2_dataflash_cleanup(struct jffs2_sb_info *c) {
	kfree(c->wbuf);
}

1260
int jffs2_nor_wbuf_flash_setup(struct jffs2_sb_info *c) {
1261 1262 1263
	/* Cleanmarker currently occupies whole programming regions,
	 * either one or 2 for 8Byte STMicro flashes. */
	c->cleanmarker_size = max(16u, c->mtd->writesize);
1264 1265 1266

	/* Initialize write buffer */
	init_rwsem(&c->wbuf_sem);
J
Joern Engel 已提交
1267
	c->wbuf_pagesize = c->mtd->writesize;
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
	c->wbuf_ofs = 0xFFFFFFFF;

	c->wbuf = kmalloc(c->wbuf_pagesize, GFP_KERNEL);
	if (!c->wbuf)
		return -ENOMEM;

	return 0;
}

void jffs2_nor_wbuf_flash_cleanup(struct jffs2_sb_info *c) {
	kfree(c->wbuf);
}