nodelist.c 29.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9
/*
 * JFFS2 -- Journalling Flash File System, Version 2.
 *
 * Copyright (C) 2001-2003 Red Hat, Inc.
 *
 * Created by David Woodhouse <dwmw2@infradead.org>
 *
 * For licensing information, see the file 'LICENCE' in this directory.
 *
10
 * $Id: nodelist.c,v 1.115 2005/11/07 11:14:40 gleixner Exp $
L
Linus Torvalds 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 */

#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/fs.h>
#include <linux/mtd/mtd.h>
#include <linux/rbtree.h>
#include <linux/crc32.h>
#include <linux/slab.h>
#include <linux/pagemap.h>
#include "nodelist.h"

void jffs2_add_fd_to_list(struct jffs2_sb_info *c, struct jffs2_full_dirent *new, struct jffs2_full_dirent **list)
{
	struct jffs2_full_dirent **prev = list;
27

28
	dbg_dentlist("add dirent \"%s\", ino #%u\n", new->name, new->ino);
L
Linus Torvalds 已提交
29 30 31 32 33

	while ((*prev) && (*prev)->nhash <= new->nhash) {
		if ((*prev)->nhash == new->nhash && !strcmp((*prev)->name, new->name)) {
			/* Duplicate. Free one */
			if (new->version < (*prev)->version) {
34
				dbg_dentlist("Eep! Marking new dirent node is obsolete, old is \"%s\", ino #%u\n",
35
					(*prev)->name, (*prev)->ino);
L
Linus Torvalds 已提交
36 37 38
				jffs2_mark_node_obsolete(c, new->raw);
				jffs2_free_full_dirent(new);
			} else {
39
				dbg_dentlist("marking old dirent \"%s\", ino #%u bsolete\n",
40
					(*prev)->name, (*prev)->ino);
L
Linus Torvalds 已提交
41 42 43 44 45
				new->next = (*prev)->next;
				jffs2_mark_node_obsolete(c, ((*prev)->raw));
				jffs2_free_full_dirent(*prev);
				*prev = new;
			}
46
			return;
L
Linus Torvalds 已提交
47 48 49 50 51 52 53
		}
		prev = &((*prev)->next);
	}
	new->next = *prev;
	*prev = new;
}

54 55 56 57
void jffs2_truncate_fragtree(struct jffs2_sb_info *c, struct rb_root *list, uint32_t size)
{
	struct jffs2_node_frag *frag = jffs2_lookup_node_frag(list, size);

58
	dbg_fragtree("truncating fragtree to 0x%08x bytes\n", size);
59 60 61

	/* We know frag->ofs <= size. That's what lookup does for us */
	if (frag && frag->ofs != size) {
62
		if (frag->ofs+frag->size > size) {
63 64 65 66 67 68 69 70 71 72 73
			frag->size = size - frag->ofs;
		}
		frag = frag_next(frag);
	}
	while (frag && frag->ofs >= size) {
		struct jffs2_node_frag *next = frag_next(frag);

		frag_erase(frag, list);
		jffs2_obsolete_node_frag(c, frag);
		frag = next;
	}
74 75 76 77

	if (size == 0)
		return;

78
	/*
79 80 81 82
	 * If the last fragment starts at the RAM page boundary, it is
	 * REF_PRISTINE irrespective of its size.
	 */
	frag = frag_last(list);
83
	if (frag->node && (frag->ofs & (PAGE_CACHE_SIZE - 1)) == 0) {
84
		dbg_fragtree2("marking the last fragment 0x%08x-0x%08x REF_PRISTINE.\n",
85
			frag->ofs, frag->ofs + frag->size);
86 87
		frag->node->raw->flash_offset = ref_offset(frag->node->raw) | REF_PRISTINE;
	}
88 89
}

90
void jffs2_obsolete_node_frag(struct jffs2_sb_info *c, struct jffs2_node_frag *this)
L
Linus Torvalds 已提交
91
{
92 93 94 95
	if (this->node) {
		this->node->frags--;
		if (!this->node->frags) {
			/* The node has no valid frags left. It's totally obsoleted */
96
			dbg_fragtree2("marking old node @0x%08x (0x%04x-0x%04x) obsolete\n",
97
				ref_offset(this->node->raw), this->node->ofs, this->node->ofs+this->node->size);
98 99 100
			jffs2_mark_node_obsolete(c, this->node->raw);
			jffs2_free_full_dnode(this->node);
		} else {
101
			dbg_fragtree2("marking old node @0x%08x (0x%04x-0x%04x) REF_NORMAL. frags is %d\n",
102
				ref_offset(this->node->raw), this->node->ofs, this->node->ofs+this->node->size, this->node->frags);
103 104
			mark_ref_normal(this->node->raw);
		}
105

106 107
	}
	jffs2_free_node_frag(this);
L
Linus Torvalds 已提交
108 109
}

110
static void jffs2_fragtree_insert(struct jffs2_node_frag *newfrag, struct jffs2_node_frag *base)
L
Linus Torvalds 已提交
111
{
112 113
	struct rb_node *parent = &base->rb;
	struct rb_node **link = &parent;
114

115
	dbg_fragtree2("insert frag (0x%04x-0x%04x)\n", newfrag->ofs, newfrag->ofs + newfrag->size);
116

117 118 119
	while (*link) {
		parent = *link;
		base = rb_entry(parent, struct jffs2_node_frag, rb);
120

121 122 123 124
		if (newfrag->ofs > base->ofs)
			link = &base->rb.rb_right;
		else if (newfrag->ofs < base->ofs)
			link = &base->rb.rb_left;
125
		else {
126
			JFFS2_ERROR("duplicate frag at %08x (%p,%p)\n", newfrag->ofs, newfrag, base);
127
			BUG();
128
		}
L
Linus Torvalds 已提交
129
	}
130 131

	rb_link_node(&newfrag->rb, &base->rb, link);
L
Linus Torvalds 已提交
132 133
}

134 135 136
/*
 * Allocate and initializes a new fragment.
 */
137
static struct jffs2_node_frag * new_fragment(struct jffs2_full_dnode *fn, uint32_t ofs, uint32_t size)
138 139
{
	struct jffs2_node_frag *newfrag;
140

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
	newfrag = jffs2_alloc_node_frag();
	if (likely(newfrag)) {
		newfrag->ofs = ofs;
		newfrag->size = size;
		newfrag->node = fn;
	} else {
		JFFS2_ERROR("cannot allocate a jffs2_node_frag object\n");
	}

	return newfrag;
}

/*
 * Called when there is no overlapping fragment exist. Inserts a hole before the new
 * fragment and inserts the new fragment to the fragtree.
 */
static int no_overlapping_node(struct jffs2_sb_info *c, struct rb_root *root,
		 	       struct jffs2_node_frag *newfrag,
			       struct jffs2_node_frag *this, uint32_t lastend)
{
	if (lastend < newfrag->node->ofs) {
		/* put a hole in before the new fragment */
		struct jffs2_node_frag *holefrag;

		holefrag= new_fragment(NULL, lastend, newfrag->node->ofs - lastend);
		if (unlikely(!holefrag)) {
			jffs2_free_node_frag(newfrag);
			return -ENOMEM;
		}

		if (this) {
172
			/* By definition, the 'this' node has no right-hand child,
173 174
			   because there are no frags with offset greater than it.
			   So that's where we want to put the hole */
175
			dbg_fragtree2("add hole frag %#04x-%#04x on the right of the new frag.\n",
176 177 178
				holefrag->ofs, holefrag->ofs + holefrag->size);
			rb_link_node(&holefrag->rb, &this->rb, &this->rb.rb_right);
		} else {
179
			dbg_fragtree2("Add hole frag %#04x-%#04x to the root of the tree.\n",
180 181 182 183 184 185
				holefrag->ofs, holefrag->ofs + holefrag->size);
			rb_link_node(&holefrag->rb, NULL, &root->rb_node);
		}
		rb_insert_color(&holefrag->rb, root);
		this = holefrag;
	}
186

187
	if (this) {
188
		/* By definition, the 'this' node has no right-hand child,
189 190
		   because there are no frags with offset greater than it.
		   So that's where we want to put new fragment */
191
		dbg_fragtree2("add the new node at the right\n");
192
		rb_link_node(&newfrag->rb, &this->rb, &this->rb.rb_right);
193
	} else {
194
		dbg_fragtree2("insert the new node at the root of the tree\n");
195 196 197 198 199 200 201
		rb_link_node(&newfrag->rb, NULL, &root->rb_node);
	}
	rb_insert_color(&newfrag->rb, root);

	return 0;
}

202
/* Doesn't set inode->i_size */
203
static int jffs2_add_frag_to_fragtree(struct jffs2_sb_info *c, struct rb_root *root, struct jffs2_node_frag *newfrag)
L
Linus Torvalds 已提交
204
{
205 206
	struct jffs2_node_frag *this;
	uint32_t lastend;
L
Linus Torvalds 已提交
207

208
	/* Skip all the nodes which are completed before this one starts */
209
	this = jffs2_lookup_node_frag(root, newfrag->node->ofs);
L
Linus Torvalds 已提交
210

211
	if (this) {
212
		dbg_fragtree2("lookup gave frag 0x%04x-0x%04x; phys 0x%08x (*%p)\n",
213
			  this->ofs, this->ofs+this->size, this->node?(ref_offset(this->node->raw)):0xffffffff, this);
214 215
		lastend = this->ofs + this->size;
	} else {
216
		dbg_fragtree2("lookup gave no frag\n");
217
		lastend = 0;
L
Linus Torvalds 已提交
218
	}
219

220
	/* See if we ran off the end of the fragtree */
221 222 223 224 225 226 227 228 229 230 231 232
	if (lastend <= newfrag->ofs) {
		/* We did */

		/* Check if 'this' node was on the same page as the new node.
		   If so, both 'this' and the new node get marked REF_NORMAL so
		   the GC can take a look.
		*/
		if (lastend && (lastend-1) >> PAGE_CACHE_SHIFT == newfrag->ofs >> PAGE_CACHE_SHIFT) {
			if (this->node)
				mark_ref_normal(this->node->raw);
			mark_ref_normal(newfrag->node->raw);
		}
L
Linus Torvalds 已提交
233

234
		return no_overlapping_node(c, root, newfrag, this, lastend);
235 236
	}

237
	if (this->node)
238
		dbg_fragtree2("dealing with frag %u-%u, phys %#08x(%d).\n",
239 240 241
		this->ofs, this->ofs + this->size,
		ref_offset(this->node->raw), ref_flags(this->node->raw));
	else
242
		dbg_fragtree2("dealing with hole frag %u-%u.\n",
243
		this->ofs, this->ofs + this->size);
244

245
	/* OK. 'this' is pointing at the first frag that newfrag->ofs at least partially obsoletes,
246
	 * - i.e. newfrag->ofs < this->ofs+this->size && newfrag->ofs >= this->ofs
247
	 */
248 249 250 251 252 253 254 255 256 257 258
	if (newfrag->ofs > this->ofs) {
		/* This node isn't completely obsoleted. The start of it remains valid */

		/* Mark the new node and the partially covered node REF_NORMAL -- let
		   the GC take a look at them */
		mark_ref_normal(newfrag->node->raw);
		if (this->node)
			mark_ref_normal(this->node->raw);

		if (this->ofs + this->size > newfrag->ofs + newfrag->size) {
			/* The new node splits 'this' frag into two */
259 260
			struct jffs2_node_frag *newfrag2;

261
			if (this->node)
262
				dbg_fragtree2("split old frag 0x%04x-0x%04x, phys 0x%08x\n",
263
					this->ofs, this->ofs+this->size, ref_offset(this->node->raw));
264
			else
265
				dbg_fragtree2("split old hole frag 0x%04x-0x%04x\n",
266
					this->ofs, this->ofs+this->size);
267

268
			/* New second frag pointing to this's node */
269 270 271 272
			newfrag2 = new_fragment(this->node, newfrag->ofs + newfrag->size,
						this->ofs + this->size - newfrag->ofs - newfrag->size);
			if (unlikely(!newfrag2))
				return -ENOMEM;
273 274 275 276 277 278 279 280 281 282 283 284 285
			if (this->node)
				this->node->frags++;

			/* Adjust size of original 'this' */
			this->size = newfrag->ofs - this->ofs;

			/* Now, we know there's no node with offset
			   greater than this->ofs but smaller than
			   newfrag2->ofs or newfrag->ofs, for obvious
			   reasons. So we can do a tree insert from
			   'this' to insert newfrag, and a tree insert
			   from newfrag to insert newfrag2. */
			jffs2_fragtree_insert(newfrag, this);
286
			rb_insert_color(&newfrag->rb, root);
287

288
			jffs2_fragtree_insert(newfrag2, newfrag);
289
			rb_insert_color(&newfrag2->rb, root);
290

291
			return 0;
292
		}
293 294
		/* New node just reduces 'this' frag in size, doesn't split it */
		this->size = newfrag->ofs - this->ofs;
295

296 297
		/* Again, we know it lives down here in the tree */
		jffs2_fragtree_insert(newfrag, this);
298
		rb_insert_color(&newfrag->rb, root);
299
	} else {
300
		/* New frag starts at the same point as 'this' used to. Replace
301
		   it in the tree without doing a delete and insertion */
302
		dbg_fragtree2("inserting newfrag (*%p),%d-%d in before 'this' (*%p),%d-%d\n",
303
			  newfrag, newfrag->ofs, newfrag->ofs+newfrag->size, this, this->ofs, this->ofs+this->size);
304

305
		rb_replace_node(&this->rb, &newfrag->rb, root);
306

307
		if (newfrag->ofs + newfrag->size >= this->ofs+this->size) {
308
			dbg_fragtree2("obsoleting node frag %p (%x-%x)\n", this, this->ofs, this->ofs+this->size);
309
			jffs2_obsolete_node_frag(c, this);
310
		} else {
311 312 313 314
			this->ofs += newfrag->size;
			this->size -= newfrag->size;

			jffs2_fragtree_insert(this, newfrag);
315
			rb_insert_color(&this->rb, root);
316
			return 0;
317 318
		}
	}
319
	/* OK, now we have newfrag added in the correct place in the tree, but
320
	   frag_next(newfrag) may be a fragment which is overlapped by it
321 322 323
	*/
	while ((this = frag_next(newfrag)) && newfrag->ofs + newfrag->size >= this->ofs + this->size) {
		/* 'this' frag is obsoleted completely. */
324
		dbg_fragtree2("obsoleting node frag %p (%x-%x) and removing from tree\n",
325
			this, this->ofs, this->ofs+this->size);
326
		rb_erase(&this->rb, root);
327
		jffs2_obsolete_node_frag(c, this);
328
	}
329
	/* Now we're pointing at the first frag which isn't totally obsoleted by
330
	   the new frag */
331

332
	if (!this || newfrag->ofs + newfrag->size == this->ofs)
333
		return 0;
334

335 336 337 338 339 340 341 342
	/* Still some overlap but we don't need to move it in the tree */
	this->size = (this->ofs + this->size) - (newfrag->ofs + newfrag->size);
	this->ofs = newfrag->ofs + newfrag->size;

	/* And mark them REF_NORMAL so the GC takes a look at them */
	if (this->node)
		mark_ref_normal(this->node->raw);
	mark_ref_normal(newfrag->node->raw);
343 344 345 346

	return 0;
}

347
/*
348 349
 * Given an inode, probably with existing tree of fragments, add the new node
 * to the fragment tree.
350
 */
351
int jffs2_add_full_dnode_to_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_full_dnode *fn)
352
{
353 354
	int ret;
	struct jffs2_node_frag *newfrag;
355

356 357
	if (unlikely(!fn->size))
		return 0;
358

359
	newfrag = new_fragment(fn, fn->ofs, fn->size);
360 361
	if (unlikely(!newfrag))
		return -ENOMEM;
362
	newfrag->node->frags = 1;
L
Linus Torvalds 已提交
363

364
	dbg_fragtree("adding node %#04x-%#04x @0x%08x on flash, newfrag *%p\n",
365
		  fn->ofs, fn->ofs+fn->size, ref_offset(fn->raw), newfrag);
366

367 368 369 370 371 372 373 374 375 376
	ret = jffs2_add_frag_to_fragtree(c, &f->fragtree, newfrag);
	if (unlikely(ret))
		return ret;

	/* If we now share a page with other nodes, mark either previous
	   or next node REF_NORMAL, as appropriate.  */
	if (newfrag->ofs & (PAGE_CACHE_SIZE-1)) {
		struct jffs2_node_frag *prev = frag_prev(newfrag);

		mark_ref_normal(fn->raw);
377
		/* If we don't start at zero there's _always_ a previous */
378 379 380
		if (prev->node)
			mark_ref_normal(prev->node->raw);
	}
L
Linus Torvalds 已提交
381

382 383
	if ((newfrag->ofs+newfrag->size) & (PAGE_CACHE_SIZE-1)) {
		struct jffs2_node_frag *next = frag_next(newfrag);
384

385 386 387 388
		if (next) {
			mark_ref_normal(fn->raw);
			if (next->node)
				mark_ref_normal(next->node->raw);
L
Linus Torvalds 已提交
389 390
		}
	}
391
	jffs2_dbg_fragtree_paranoia_check_nolock(f);
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408

	return 0;
}

/*
 * Check the data CRC of the node.
 *
 * Returns: 0 if the data CRC is correct;
 * 	    1 - if incorrect;
 *	    error code if an error occured.
 */
static int check_node_data(struct jffs2_sb_info *c, struct jffs2_tmp_dnode_info *tn)
{
	struct jffs2_raw_node_ref *ref = tn->fn->raw;
	int err = 0, pointed = 0;
	struct jffs2_eraseblock *jeb;
	unsigned char *buffer;
A
Atsushi Nemoto 已提交
409 410
	uint32_t crc, ofs, len;
	size_t retlen;
411 412 413

	BUG_ON(tn->csize == 0);

414 415
	if (!jffs2_is_writebuffered(c))
		goto adj_acc;
416

417 418
	/* Calculate how many bytes were already checked */
	ofs = ref_offset(ref) + sizeof(struct jffs2_raw_inode);
419
	len = ofs % c->wbuf_pagesize;
420 421
	if (likely(len))
		len = c->wbuf_pagesize - len;
422 423

	if (len >= tn->csize) {
424
		dbg_readinode("no need to check node at %#08x, data length %u, data starts at %#08x - it has already been checked.\n",
425 426 427
			ref_offset(ref), tn->csize, ofs);
		goto adj_acc;
	}
428

429 430
	ofs += len;
	len = tn->csize - len;
431

432
	dbg_readinode("check node at %#08x, data length %u, partial CRC %#08x, correct CRC %#08x, data starts at %#08x, start checking from %#08x - %u bytes.\n",
433
		ref_offset(ref), tn->csize, tn->partial_crc, tn->data_crc, ofs - len, ofs, len);
434

435 436 437 438 439 440 441 442 443 444 445 446 447 448
#ifndef __ECOS
	/* TODO: instead, incapsulate point() stuff to jffs2_flash_read(),
	 * adding and jffs2_flash_read_end() interface. */
	if (c->mtd->point) {
		err = c->mtd->point(c->mtd, ofs, len, &retlen, &buffer);
		if (!err && retlen < tn->csize) {
			JFFS2_WARNING("MTD point returned len too short: %u instead of %u.\n", retlen, tn->csize);
			c->mtd->unpoint(c->mtd, buffer, ofs, len);
		} else if (err)
			JFFS2_WARNING("MTD point failed: error code %d.\n", err);
		else
			pointed = 1; /* succefully pointed to device */
	}
#endif
449

450 451 452 453
	if (!pointed) {
		buffer = kmalloc(len, GFP_KERNEL);
		if (unlikely(!buffer))
			return -ENOMEM;
454

455 456 457 458 459 460 461
		/* TODO: this is very frequent pattern, make it a separate
		 * routine */
		err = jffs2_flash_read(c, ofs, len, &retlen, buffer);
		if (err) {
			JFFS2_ERROR("can not read %d bytes from 0x%08x, error code: %d.\n", len, ofs, err);
			goto free_out;
		}
462

463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
		if (retlen != len) {
			JFFS2_ERROR("short read at %#08x: %d instead of %d.\n", ofs, retlen, len);
			err = -EIO;
			goto free_out;
		}
	}

	/* Continue calculating CRC */
	crc = crc32(tn->partial_crc, buffer, len);
	if(!pointed)
		kfree(buffer);
#ifndef __ECOS
	else
		c->mtd->unpoint(c->mtd, buffer, ofs, len);
#endif

	if (crc != tn->data_crc) {
480
		JFFS2_NOTICE("wrong data CRC in data node at 0x%08x: read %#08x, calculated %#08x.\n",
481 482 483 484 485 486 487 488
			ofs, tn->data_crc, crc);
		return 1;
	}

adj_acc:
	jeb = &c->blocks[ref->flash_offset / c->sector_size];
	len = ref_totlen(c, jeb, ref);

489
	/*
490 491 492 493 494 495 496 497 498 499
	 * Mark the node as having been checked and fix the
	 * accounting accordingly.
	 */
	spin_lock(&c->erase_completion_lock);
	jeb->used_size += len;
	jeb->unchecked_size -= len;
	c->used_size += len;
	c->unchecked_size -= len;
	spin_unlock(&c->erase_completion_lock);

L
Linus Torvalds 已提交
500
	return 0;
501 502 503 504 505 506 507 508 509

free_out:
	if(!pointed)
		kfree(buffer);
#ifndef __ECOS
	else
		c->mtd->unpoint(c->mtd, buffer, ofs, len);
#endif
	return err;
L
Linus Torvalds 已提交
510 511
}

512 513 514 515 516
/*
 * Helper function for jffs2_add_older_frag_to_fragtree().
 *
 * Checks the node if we are in the checking stage.
 */
517
static int check_node(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_tmp_dnode_info *tn)
518 519
{
	int ret;
520

521 522 523 524 525
	BUG_ON(ref_obsolete(tn->fn->raw));

	/* We only check the data CRC of unchecked nodes */
	if (ref_flags(tn->fn->raw) != REF_UNCHECKED)
		return 0;
526

527
	dbg_fragtree2("check node %#04x-%#04x, phys offs %#08x.\n",
528
		tn->fn->ofs, tn->fn->ofs + tn->fn->size, ref_offset(tn->fn->raw));
529 530 531 532 533 534

	ret = check_node_data(c, tn);
	if (unlikely(ret < 0)) {
		JFFS2_ERROR("check_node_data() returned error: %d.\n",
			ret);
	} else if (unlikely(ret > 0)) {
535
		dbg_fragtree2("CRC error, mark it obsolete.\n");
536 537 538 539 540 541
		jffs2_mark_node_obsolete(c, tn->fn->raw);
	}

	return ret;
}

542
/*
543 544 545 546 547 548 549 550
 * Helper function for jffs2_add_older_frag_to_fragtree().
 *
 * Called when the new fragment that is being inserted
 * splits a hole fragment.
 */
static int split_hole(struct jffs2_sb_info *c, struct rb_root *root,
		      struct jffs2_node_frag *newfrag, struct jffs2_node_frag *hole)
{
551
	dbg_fragtree2("fragment %#04x-%#04x splits the hole %#04x-%#04x\n",
552 553 554
		newfrag->ofs, newfrag->ofs + newfrag->size, hole->ofs, hole->ofs + hole->size);

	if (hole->ofs == newfrag->ofs) {
555
		/*
556 557 558 559
		 * Well, the new fragment actually starts at the same offset as
		 * the hole.
		 */
		if (hole->ofs + hole->size > newfrag->ofs + newfrag->size) {
560
			/*
561 562 563
			 * We replace the overlapped left part of the hole by
			 * the new node.
			 */
564

565
			dbg_fragtree2("insert fragment %#04x-%#04x and cut the left part of the hole\n",
566 567
				newfrag->ofs, newfrag->ofs + newfrag->size);
			rb_replace_node(&hole->rb, &newfrag->rb, root);
568

569 570
			hole->ofs += newfrag->size;
			hole->size -= newfrag->size;
571 572

			/*
573 574 575 576 577 578
			 * We know that 'hole' should be the right hand
			 * fragment.
			 */
			jffs2_fragtree_insert(hole, newfrag);
			rb_insert_color(&hole->rb, root);
		} else {
579
			/*
580 581 582
			 * Ah, the new fragment is of the same size as the hole.
			 * Relace the hole by it.
			 */
583
			dbg_fragtree2("insert fragment %#04x-%#04x and overwrite hole\n",
584 585 586 587 588 589
				newfrag->ofs, newfrag->ofs + newfrag->size);
			rb_replace_node(&hole->rb, &newfrag->rb, root);
			jffs2_free_node_frag(hole);
		}
	} else {
		/* The new fragment lefts some hole space at the left */
590

591 592 593 594 595 596 597 598 599 600 601 602 603 604
		struct jffs2_node_frag * newfrag2 = NULL;

		if (hole->ofs + hole->size > newfrag->ofs + newfrag->size) {
			/* The new frag also lefts some space at the right */
			newfrag2 = new_fragment(NULL, newfrag->ofs +
				newfrag->size, hole->ofs + hole->size
				- newfrag->ofs - newfrag->size);
			if (unlikely(!newfrag2)) {
				jffs2_free_node_frag(newfrag);
				return -ENOMEM;
			}
		}

		hole->size = newfrag->ofs - hole->ofs;
605
		dbg_fragtree2("left the hole %#04x-%#04x at the left and inserd fragment %#04x-%#04x\n",
606 607 608 609
			hole->ofs, hole->ofs + hole->size, newfrag->ofs, newfrag->ofs + newfrag->size);

		jffs2_fragtree_insert(newfrag, hole);
		rb_insert_color(&newfrag->rb, root);
610

611
		if (newfrag2) {
612
			dbg_fragtree2("left the hole %#04x-%#04x at the right\n",
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
				newfrag2->ofs, newfrag2->ofs + newfrag2->size);
			jffs2_fragtree_insert(newfrag2, newfrag);
			rb_insert_color(&newfrag2->rb, root);
		}
	}

	return 0;
}

/*
 * This function is used when we build inode. It expects the nodes are passed
 * in the decreasing version order. The whole point of this is to improve the
 * inodes checking on NAND: we check the nodes' data CRC only when they are not
 * obsoleted. Previously, add_frag_to_fragtree() function was used and
 * nodes were passed to it in the increasing version ordes and CRCs of all
 * nodes were checked.
 *
 * Note: tn->fn->size shouldn't be zero.
 *
 * Returns 0 if the node was inserted
 *         1 if it wasn't inserted (since it is obsolete)
 *         < 0 an if error occured
 */
int jffs2_add_older_frag_to_fragtree(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
				     struct jffs2_tmp_dnode_info *tn)
{
	struct jffs2_node_frag *this, *newfrag;
	uint32_t lastend;
	struct jffs2_full_dnode *fn = tn->fn;
	struct rb_root *root = &f->fragtree;
	uint32_t fn_size = fn->size, fn_ofs = fn->ofs;
	int err, checked = 0;
	int ref_flag;

647
	dbg_fragtree("insert fragment %#04x-%#04x, ver %u\n", fn_ofs, fn_ofs + fn_size, tn->version);
648 649 650 651

	/* Skip all the nodes which are completed before this one starts */
	this = jffs2_lookup_node_frag(root, fn_ofs);
	if (this)
652
		dbg_fragtree2("'this' found %#04x-%#04x (%s)\n", this->ofs, this->ofs + this->size, this->node ? "data" : "hole");
653 654 655 656 657

	if (this)
		lastend = this->ofs + this->size;
	else
		lastend = 0;
658

659 660 661 662 663
	/* Detect the preliminary type of node */
	if (fn->size >= PAGE_CACHE_SIZE)
		ref_flag = REF_PRISTINE;
	else
		ref_flag = REF_NORMAL;
664

665 666 667
	/* See if we ran off the end of the root */
	if (lastend <= fn_ofs) {
		/* We did */
668 669

		/*
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
		 * We are going to insert the new node into the
		 * fragment tree, so check it.
		 */
		err = check_node(c, f, tn);
		if (err != 0)
			return err;

		fn->frags = 1;

		newfrag = new_fragment(fn, fn_ofs, fn_size);
		if (unlikely(!newfrag))
			return -ENOMEM;

		err = no_overlapping_node(c, root, newfrag, this, lastend);
		if (unlikely(err != 0)) {
			jffs2_free_node_frag(newfrag);
			return err;
		}

		goto out_ok;
	}

	fn->frags = 0;

	while (1) {
695
		/*
696 697
		 * Here we have:
		 * fn_ofs < this->ofs + this->size && fn_ofs >= this->ofs.
698
		 *
699 700 701 702 703
		 * Remember, 'this' has higher version, any non-hole node
		 * which is already in the fragtree is newer then the newly
		 * inserted.
		 */
		if (!this->node) {
704
			/*
705 706 707
			 * 'this' is the hole fragment, so at least the
			 * beginning of the new fragment is valid.
			 */
708 709

			/*
710 711 712 713 714 715 716 717 718
			 * We are going to insert the new node into the
			 * fragment tree, so check it.
			 */
			if (!checked) {
				err = check_node(c, f, tn);
				if (unlikely(err != 0))
					return err;
				checked = 1;
			}
719

720 721 722 723 724 725 726 727 728 729 730 731 732 733
			if (this->ofs + this->size >= fn_ofs + fn_size) {
				/* We split the hole on two parts */

				fn->frags += 1;
				newfrag = new_fragment(fn, fn_ofs, fn_size);
				if (unlikely(!newfrag))
					return -ENOMEM;

				err = split_hole(c, root, newfrag, this);
				if (unlikely(err))
					return err;
				goto out_ok;
			}

734
			/*
735 736 737 738 739 740 741 742 743 744 745
			 * The beginning of the new fragment is valid since it
			 * overlaps the hole node.
			 */

			ref_flag = REF_NORMAL;

			fn->frags += 1;
			newfrag = new_fragment(fn, fn_ofs,
					this->ofs + this->size - fn_ofs);
			if (unlikely(!newfrag))
				return -ENOMEM;
746

747
			if (fn_ofs == this->ofs) {
748
				/*
749 750 751
				 * The new node starts at the same offset as
				 * the hole and supersieds the hole.
				 */
752
				dbg_fragtree2("add the new fragment instead of hole %#04x-%#04x, refcnt %d\n",
753 754 755 756 757
					fn_ofs, fn_ofs + this->ofs + this->size - fn_ofs, fn->frags);

				rb_replace_node(&this->rb, &newfrag->rb, root);
				jffs2_free_node_frag(this);
			} else {
758
				/*
759 760 761
				 * The hole becomes shorter as its right part
				 * is supersieded by the new fragment.
				 */
762
				dbg_fragtree2("reduce size of hole %#04x-%#04x to %#04x-%#04x\n",
763
					this->ofs, this->ofs + this->size, this->ofs, this->ofs + this->size - newfrag->size);
764

765
				dbg_fragtree2("add new fragment %#04x-%#04x, refcnt %d\n", fn_ofs,
766
					fn_ofs + this->ofs + this->size - fn_ofs, fn->frags);
767

768 769 770 771
				this->size -= newfrag->size;
				jffs2_fragtree_insert(newfrag, this);
				rb_insert_color(&newfrag->rb, root);
			}
772

773 774 775 776 777
			fn_ofs += newfrag->size;
			fn_size -= newfrag->size;
			this = rb_entry(rb_next(&newfrag->rb),
					struct jffs2_node_frag, rb);

778
			dbg_fragtree2("switch to the next 'this' fragment: %#04x-%#04x %s\n",
779 780 781
				this->ofs, this->ofs + this->size, this->node ? "(data)" : "(hole)");
		}

782
		/*
783 784 785 786 787 788
		 * 'This' node is not the hole so it obsoletes the new fragment
		 * either fully or partially.
		 */
		if (this->ofs + this->size >= fn_ofs + fn_size) {
			/* The new node is obsolete, drop it */
			if (fn->frags == 0) {
789
				dbg_fragtree2("%#04x-%#04x is obsolete, mark it obsolete\n", fn_ofs, fn_ofs + fn_size);
790 791 792 793 794
				ref_flag = REF_OBSOLETE;
			}
			goto out_ok;
		} else {
			struct jffs2_node_frag *new_this;
795

796
			/* 'This' node obsoletes the beginning of the new node */
797
			dbg_fragtree2("the beginning %#04x-%#04x is obsolete\n", fn_ofs, this->ofs + this->size);
798 799

			ref_flag = REF_NORMAL;
800

801 802
			fn_size -= this->ofs + this->size - fn_ofs;
			fn_ofs = this->ofs + this->size;
803
			dbg_fragtree2("now considering %#04x-%#04x\n", fn_ofs, fn_ofs + fn_size);
804

805 806
			new_this = rb_entry(rb_next(&this->rb), struct jffs2_node_frag, rb);
			if (!new_this) {
807
				/*
808 809 810 811 812 813 814 815 816
				 * There is no next fragment. Add the rest of
				 * the new node as the right-hand child.
				 */
				if (!checked) {
					err = check_node(c, f, tn);
					if (unlikely(err != 0))
						return err;
					checked = 1;
				}
817

818 819 820 821 822
				fn->frags += 1;
				newfrag = new_fragment(fn, fn_ofs, fn_size);
				if (unlikely(!newfrag))
					return -ENOMEM;

823
				dbg_fragtree2("there are no more fragments, insert %#04x-%#04x\n",
824
					newfrag->ofs, newfrag->ofs + newfrag->size);
825
				rb_link_node(&newfrag->rb, &this->rb, &this->rb.rb_right);
826 827 828 829
				rb_insert_color(&newfrag->rb, root);
				goto out_ok;
			} else {
				this = new_this;
830
				dbg_fragtree2("switch to the next 'this' fragment: %#04x-%#04x %s\n",
831 832 833 834 835 836 837 838 839
					this->ofs, this->ofs + this->size, this->node ? "(data)" : "(hole)");
			}
		}
	}

out_ok:
	BUG_ON(fn->size < PAGE_CACHE_SIZE && ref_flag == REF_PRISTINE);

	if (ref_flag == REF_OBSOLETE) {
840
		dbg_fragtree2("the node is obsolete now\n");
841 842 843 844 845
		/* jffs2_mark_node_obsolete() will adjust space accounting */
		jffs2_mark_node_obsolete(c, fn->raw);
		return 1;
	}

846
	dbg_fragtree2("the node is \"%s\" now\n", ref_flag == REF_NORMAL ? "REF_NORMAL" : "REF_PRISTINE");
847 848 849 850 851 852 853 854

	/* Space accounting was adjusted at check_node_data() */
	spin_lock(&c->erase_completion_lock);
	fn->raw->flash_offset = ref_offset(fn->raw) | ref_flag;
	spin_unlock(&c->erase_completion_lock);

	return 0;
}
855

L
Linus Torvalds 已提交
856 857 858 859 860 861 862 863 864 865
void jffs2_set_inocache_state(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic, int state)
{
	spin_lock(&c->inocache_lock);
	ic->state = state;
	wake_up(&c->inocache_wq);
	spin_unlock(&c->inocache_lock);
}

/* During mount, this needs no locking. During normal operation, its
   callers want to do other stuff while still holding the inocache_lock.
866
   Rather than introducing special case get_ino_cache functions or
L
Linus Torvalds 已提交
867
   callbacks, we just let the caller do the locking itself. */
868

L
Linus Torvalds 已提交
869 870 871 872 873 874 875 876
struct jffs2_inode_cache *jffs2_get_ino_cache(struct jffs2_sb_info *c, uint32_t ino)
{
	struct jffs2_inode_cache *ret;

	ret = c->inocache_list[ino % INOCACHE_HASHSIZE];
	while (ret && ret->ino < ino) {
		ret = ret->next;
	}
877

L
Linus Torvalds 已提交
878 879 880 881 882 883 884 885 886
	if (ret && ret->ino != ino)
		ret = NULL;

	return ret;
}

void jffs2_add_ino_cache (struct jffs2_sb_info *c, struct jffs2_inode_cache *new)
{
	struct jffs2_inode_cache **prev;
887

L
Linus Torvalds 已提交
888
	spin_lock(&c->inocache_lock);
889 890 891
	if (!new->ino)
		new->ino = ++c->highest_ino;

892
	dbg_inocache("add %p (ino #%u)\n", new, new->ino);
893

L
Linus Torvalds 已提交
894 895 896 897 898 899 900 901 902 903 904 905 906 907
	prev = &c->inocache_list[new->ino % INOCACHE_HASHSIZE];

	while ((*prev) && (*prev)->ino < new->ino) {
		prev = &(*prev)->next;
	}
	new->next = *prev;
	*prev = new;

	spin_unlock(&c->inocache_lock);
}

void jffs2_del_ino_cache(struct jffs2_sb_info *c, struct jffs2_inode_cache *old)
{
	struct jffs2_inode_cache **prev;
908

909
	dbg_inocache("del %p (ino #%u)\n", old, old->ino);
L
Linus Torvalds 已提交
910
	spin_lock(&c->inocache_lock);
911

L
Linus Torvalds 已提交
912
	prev = &c->inocache_list[old->ino % INOCACHE_HASHSIZE];
913

L
Linus Torvalds 已提交
914 915 916 917 918 919 920
	while ((*prev) && (*prev)->ino < old->ino) {
		prev = &(*prev)->next;
	}
	if ((*prev) == old) {
		*prev = old->next;
	}

921 922
	/* Free it now unless it's in READING or CLEARING state, which
	   are the transitions upon read_inode() and clear_inode(). The
923
	   rest of the time we know nobody else is looking at it, and
924 925 926 927 928
	   if it's held by read_inode() or clear_inode() they'll free it
	   for themselves. */
	if (old->state != INO_STATE_READING && old->state != INO_STATE_CLEARING)
		jffs2_free_inode_cache(old);

L
Linus Torvalds 已提交
929 930 931 932 933 934 935
	spin_unlock(&c->inocache_lock);
}

void jffs2_free_ino_caches(struct jffs2_sb_info *c)
{
	int i;
	struct jffs2_inode_cache *this, *next;
936

L
Linus Torvalds 已提交
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
	for (i=0; i<INOCACHE_HASHSIZE; i++) {
		this = c->inocache_list[i];
		while (this) {
			next = this->next;
			jffs2_free_inode_cache(this);
			this = next;
		}
		c->inocache_list[i] = NULL;
	}
}

void jffs2_free_raw_node_refs(struct jffs2_sb_info *c)
{
	int i;
	struct jffs2_raw_node_ref *this, *next;

	for (i=0; i<c->nr_blocks; i++) {
		this = c->blocks[i].first_node;
		while(this) {
			next = this->next_phys;
			jffs2_free_raw_node_ref(this);
			this = next;
		}
		c->blocks[i].first_node = c->blocks[i].last_node = NULL;
	}
}
963

L
Linus Torvalds 已提交
964 965
struct jffs2_node_frag *jffs2_lookup_node_frag(struct rb_root *fragtree, uint32_t offset)
{
966
	/* The common case in lookup is that there will be a node
L
Linus Torvalds 已提交
967 968 969 970 971
	   which precisely matches. So we go looking for that first */
	struct rb_node *next;
	struct jffs2_node_frag *prev = NULL;
	struct jffs2_node_frag *frag = NULL;

972
	dbg_fragtree2("root %p, offset %d\n", fragtree, offset);
L
Linus Torvalds 已提交
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994

	next = fragtree->rb_node;

	while(next) {
		frag = rb_entry(next, struct jffs2_node_frag, rb);

		if (frag->ofs + frag->size <= offset) {
			/* Remember the closest smaller match on the way down */
			if (!prev || frag->ofs > prev->ofs)
				prev = frag;
			next = frag->rb.rb_right;
		} else if (frag->ofs > offset) {
			next = frag->rb.rb_left;
		} else {
			return frag;
		}
	}

	/* Exact match not found. Go back up looking at each parent,
	   and return the closest smaller one */

	if (prev)
995
		dbg_fragtree2("no match. Returning frag %#04x-%#04x, closest previous\n",
996
			  prev->ofs, prev->ofs+prev->size);
997
	else
998
		dbg_fragtree2("returning NULL, empty fragtree\n");
999

L
Linus Torvalds 已提交
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
	return prev;
}

/* Pass 'c' argument to indicate that nodes should be marked obsolete as
   they're killed. */
void jffs2_kill_fragtree(struct rb_root *root, struct jffs2_sb_info *c)
{
	struct jffs2_node_frag *frag;
	struct jffs2_node_frag *parent;

	if (!root->rb_node)
		return;

1013
	dbg_fragtree("killing\n");
1014

L
Linus Torvalds 已提交
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
	frag = (rb_entry(root->rb_node, struct jffs2_node_frag, rb));
	while(frag) {
		if (frag->rb.rb_left) {
			frag = frag_left(frag);
			continue;
		}
		if (frag->rb.rb_right) {
			frag = frag_right(frag);
			continue;
		}

		if (frag->node && !(--frag->node->frags)) {
1027
			/* Not a hole, and it's the final remaining frag
L
Linus Torvalds 已提交
1028 1029 1030
			   of this node. Free the node */
			if (c)
				jffs2_mark_node_obsolete(c, frag->node->raw);
1031

L
Linus Torvalds 已提交
1032 1033 1034 1035 1036 1037
			jffs2_free_full_dnode(frag->node);
		}
		parent = frag_parent(frag);
		if (parent) {
			if (frag_left(parent) == frag)
				parent->rb.rb_left = NULL;
1038
			else
L
Linus Torvalds 已提交
1039 1040 1041 1042 1043 1044 1045 1046 1047
				parent->rb.rb_right = NULL;
		}

		jffs2_free_node_frag(frag);
		frag = parent;

		cond_resched();
	}
}