gc.c 40.9 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3
/*
 * JFFS2 -- Journalling Flash File System, Version 2.
 *
4
 * Copyright (C) 2001-2003 Red Hat, Inc.
Linus Torvalds's avatar
Linus Torvalds committed
5
 *
6
 * Created by David Woodhouse <dwmw2@redhat.com>
Linus Torvalds's avatar
Linus Torvalds committed
7
 *
8
 * For licensing information, see the file 'LICENCE' in this directory.
Linus Torvalds's avatar
Linus Torvalds committed
9
 *
David Woodhouse's avatar
David Woodhouse committed
10
 * $Id: gc.c,v 1.136 2004/05/27 19:06:09 gleixner Exp $
Linus Torvalds's avatar
Linus Torvalds committed
11 12 13 14 15 16 17
 *
 */

#include <linux/kernel.h>
#include <linux/mtd/mtd.h>
#include <linux/slab.h>
#include <linux/pagemap.h>
Linus Torvalds's avatar
Linus Torvalds committed
18
#include <linux/crc32.h>
David Woodhouse's avatar
David Woodhouse committed
19
#include <linux/compiler.h>
David Woodhouse's avatar
David Woodhouse committed
20
#include <linux/stat.h>
Linus Torvalds's avatar
Linus Torvalds committed
21
#include "nodelist.h"
David Woodhouse's avatar
David Woodhouse committed
22
#include "compr.h"
Linus Torvalds's avatar
Linus Torvalds committed
23

David Woodhouse's avatar
David Woodhouse committed
24 25 26
static int jffs2_garbage_collect_pristine(struct jffs2_sb_info *c, 
					  struct jffs2_inode_cache *ic,
					  struct jffs2_raw_node_ref *raw);
Linus Torvalds's avatar
Linus Torvalds committed
27
static int jffs2_garbage_collect_metadata(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, 
28
					struct jffs2_inode_info *f, struct jffs2_full_dnode *fd);
Linus Torvalds's avatar
Linus Torvalds committed
29
static int jffs2_garbage_collect_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, 
30
					struct jffs2_inode_info *f, struct jffs2_full_dirent *fd);
Linus Torvalds's avatar
Linus Torvalds committed
31
static int jffs2_garbage_collect_deletion_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, 
32
					struct jffs2_inode_info *f, struct jffs2_full_dirent *fd);
Linus Torvalds's avatar
Linus Torvalds committed
33
static int jffs2_garbage_collect_hole(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
34 35
				      struct jffs2_inode_info *f, struct jffs2_full_dnode *fn,
				      uint32_t start, uint32_t end);
Linus Torvalds's avatar
Linus Torvalds committed
36
static int jffs2_garbage_collect_dnode(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
37 38
				       struct jffs2_inode_info *f, struct jffs2_full_dnode *fn,
				       uint32_t start, uint32_t end);
David Woodhouse's avatar
David Woodhouse committed
39
static int jffs2_garbage_collect_live(struct jffs2_sb_info *c,  struct jffs2_eraseblock *jeb,
David Woodhouse's avatar
David Woodhouse committed
40
			       struct jffs2_raw_node_ref *raw, struct jffs2_inode_info *f);
Linus Torvalds's avatar
Linus Torvalds committed
41 42 43 44 45 46

/* Called with erase_completion_lock held */
static struct jffs2_eraseblock *jffs2_find_gc_block(struct jffs2_sb_info *c)
{
	struct jffs2_eraseblock *ret;
	struct list_head *nextlist = NULL;
47
	int n = jiffies % 128;
Linus Torvalds's avatar
Linus Torvalds committed
48 49 50

	/* Pick an eraseblock to garbage collect next. This is where we'll
	   put the clever wear-levelling algorithms. Eventually.  */
David Woodhouse's avatar
David Woodhouse committed
51 52
	/* We possibly want to favour the dirtier blocks more when the
	   number of free blocks is low. */
53
	if (!list_empty(&c->bad_used_list) && c->nr_free_blocks > c->resv_blocks_gcbad) {
Linus Torvalds's avatar
Linus Torvalds committed
54 55
		D1(printk(KERN_DEBUG "Picking block from bad_used_list to GC next\n"));
		nextlist = &c->bad_used_list;
David Woodhouse's avatar
David Woodhouse committed
56 57 58
	} else if (n < 50 && !list_empty(&c->erasable_list)) {
		/* Note that most of them will have gone directly to be erased. 
		   So don't favour the erasable_list _too_ much. */
59 60
		D1(printk(KERN_DEBUG "Picking block from erasable_list to GC next\n"));
		nextlist = &c->erasable_list;
David Woodhouse's avatar
David Woodhouse committed
61 62 63 64
	} else if (n < 110 && !list_empty(&c->very_dirty_list)) {
		/* Most of the time, pick one off the very_dirty list */
		D1(printk(KERN_DEBUG "Picking block from very_dirty_list to GC next\n"));
		nextlist = &c->very_dirty_list;
65
	} else if (n < 126 && !list_empty(&c->dirty_list)) {
Linus Torvalds's avatar
Linus Torvalds committed
66 67 68 69 70 71 72 73 74
		D1(printk(KERN_DEBUG "Picking block from dirty_list to GC next\n"));
		nextlist = &c->dirty_list;
	} else if (!list_empty(&c->clean_list)) {
		D1(printk(KERN_DEBUG "Picking block from clean_list to GC next\n"));
		nextlist = &c->clean_list;
	} else if (!list_empty(&c->dirty_list)) {
		D1(printk(KERN_DEBUG "Picking block from dirty_list to GC next (clean_list was empty)\n"));

		nextlist = &c->dirty_list;
David Woodhouse's avatar
David Woodhouse committed
75 76 77
	} else if (!list_empty(&c->very_dirty_list)) {
		D1(printk(KERN_DEBUG "Picking block from very_dirty_list to GC next (clean_list and dirty_list were empty)\n"));
		nextlist = &c->very_dirty_list;
78
	} else if (!list_empty(&c->erasable_list)) {
David Woodhouse's avatar
David Woodhouse committed
79
		D1(printk(KERN_DEBUG "Picking block from erasable_list to GC next (clean_list and {very_,}dirty_list were empty)\n"));
80 81

		nextlist = &c->erasable_list;
Linus Torvalds's avatar
Linus Torvalds committed
82
	} else {
David Woodhouse's avatar
David Woodhouse committed
83
		/* Eep. All were empty */
David Woodhouse's avatar
David Woodhouse committed
84
		D1(printk(KERN_NOTICE "jffs2: No clean, dirty _or_ erasable blocks to GC from! Where are they all?\n"));
Linus Torvalds's avatar
Linus Torvalds committed
85 86 87 88 89 90 91 92 93 94 95
		return NULL;
	}

	ret = list_entry(nextlist->next, struct jffs2_eraseblock, list);
	list_del(&ret->list);
	c->gcblock = ret;
	ret->gc_node = ret->first_node;
	if (!ret->gc_node) {
		printk(KERN_WARNING "Eep. ret->gc_node for block at 0x%08x is NULL\n", ret->offset);
		BUG();
	}
David Woodhouse's avatar
David Woodhouse committed
96 97 98 99 100 101 102 103 104
	
	/* Have we accidentally picked a clean block with wasted space ? */
	if (ret->wasted_size) {
		D1(printk(KERN_DEBUG "Converting wasted_size %08x to dirty_size\n", ret->wasted_size));
		ret->dirty_size += ret->wasted_size;
		c->wasted_size -= ret->wasted_size;
		c->dirty_size += ret->wasted_size;
		ret->wasted_size = 0;
	}
David Woodhouse's avatar
David Woodhouse committed
105 106

	D1(jffs2_dump_block_lists(c));
Linus Torvalds's avatar
Linus Torvalds committed
107 108 109 110 111 112 113 114 115
	return ret;
}

/* jffs2_garbage_collect_pass
 * Make a single attempt to progress GC. Move one node, and possibly
 * start erasing one eraseblock.
 */
int jffs2_garbage_collect_pass(struct jffs2_sb_info *c)
{
David Woodhouse's avatar
David Woodhouse committed
116
	struct jffs2_inode_info *f;
David Woodhouse's avatar
David Woodhouse committed
117
	struct jffs2_inode_cache *ic;
Linus Torvalds's avatar
Linus Torvalds committed
118 119
	struct jffs2_eraseblock *jeb;
	struct jffs2_raw_node_ref *raw;
David Woodhouse's avatar
David Woodhouse committed
120
	int ret = 0, inum, nlink;
Linus Torvalds's avatar
Linus Torvalds committed
121 122 123 124

	if (down_interruptible(&c->alloc_sem))
		return -EINTR;

David Woodhouse's avatar
David Woodhouse committed
125 126 127 128
	for (;;) {
		spin_lock(&c->erase_completion_lock);
		if (!c->unchecked_size)
			break;
Linus Torvalds's avatar
Linus Torvalds committed
129

David Woodhouse's avatar
David Woodhouse committed
130
		/* We can't start doing GC yet. We haven't finished checking
David Woodhouse's avatar
David Woodhouse committed
131 132 133
		   the node CRCs etc. Do it now. */
		
		/* checked_ino is protected by the alloc_sem */
David Woodhouse's avatar
David Woodhouse committed
134 135 136 137
		if (c->checked_ino > c->highest_ino) {
			printk(KERN_CRIT "Checked all inodes but still 0x%x bytes of unchecked space?\n",
			       c->unchecked_size);
			D1(jffs2_dump_block_lists(c));
David Woodhouse's avatar
David Woodhouse committed
138
			spin_unlock(&c->erase_completion_lock);
David Woodhouse's avatar
David Woodhouse committed
139 140
			BUG();
		}
David Woodhouse's avatar
David Woodhouse committed
141 142 143 144 145

		spin_unlock(&c->erase_completion_lock);

		spin_lock(&c->inocache_lock);

David Woodhouse's avatar
David Woodhouse committed
146
		ic = jffs2_get_ino_cache(c, c->checked_ino++);
David Woodhouse's avatar
David Woodhouse committed
147 148 149

		if (!ic) {
			spin_unlock(&c->inocache_lock);
David Woodhouse's avatar
David Woodhouse committed
150
			continue;
David Woodhouse's avatar
David Woodhouse committed
151 152
		}

David Woodhouse's avatar
David Woodhouse committed
153 154 155
		if (!ic->nlink) {
			D1(printk(KERN_DEBUG "Skipping check of ino #%d with nlink zero\n",
				  ic->ino));
David Woodhouse's avatar
David Woodhouse committed
156
			spin_unlock(&c->inocache_lock);
David Woodhouse's avatar
David Woodhouse committed
157 158
			continue;
		}
David Woodhouse's avatar
David Woodhouse committed
159 160 161 162 163
		switch(ic->state) {
		case INO_STATE_CHECKEDABSENT:
		case INO_STATE_PRESENT:
			D1(printk(KERN_DEBUG "Skipping ino #%u already checked\n", ic->ino));
			spin_unlock(&c->inocache_lock);
David Woodhouse's avatar
David Woodhouse committed
164 165
			continue;

David Woodhouse's avatar
David Woodhouse committed
166 167 168 169 170
		case INO_STATE_GC:
		case INO_STATE_CHECKING:
			printk(KERN_WARNING "Inode #%u is in state %d during CRC check phase!\n", ic->ino, ic->state);
			spin_unlock(&c->inocache_lock);
			BUG();
David Woodhouse's avatar
David Woodhouse committed
171

David Woodhouse's avatar
David Woodhouse committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185
		case INO_STATE_READING:
			/* We need to wait for it to finish, lest we move on
			   and trigger the BUG() above while we haven't yet 
			   finished checking all its nodes */
			D1(printk(KERN_DEBUG "Waiting for ino #%u to finish reading\n", ic->ino));
			up(&c->alloc_sem);
			sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
			return 0;

		default:
			BUG();

		case INO_STATE_UNCHECKED:
			;
David Woodhouse's avatar
David Woodhouse committed
186
		}
David Woodhouse's avatar
David Woodhouse committed
187 188
		ic->state = INO_STATE_CHECKING;
		spin_unlock(&c->inocache_lock);
David Woodhouse's avatar
David Woodhouse committed
189

David Woodhouse's avatar
David Woodhouse committed
190
		D1(printk(KERN_DEBUG "jffs2_garbage_collect_pass() triggering inode scan of ino#%u\n", ic->ino));
David Woodhouse's avatar
David Woodhouse committed
191 192 193 194 195 196

		ret = jffs2_do_crccheck_inode(c, ic);
		if (ret)
			printk(KERN_WARNING "Returned error for crccheck of ino #%u. Expect badness...\n", ic->ino);

		jffs2_set_inocache_state(c, ic, INO_STATE_CHECKEDABSENT);
David Woodhouse's avatar
David Woodhouse committed
197 198 199 200
		up(&c->alloc_sem);
		return ret;
	}

Linus Torvalds's avatar
Linus Torvalds committed
201 202 203 204 205 206 207
	/* First, work out which block we're garbage-collecting */
	jeb = c->gcblock;

	if (!jeb)
		jeb = jffs2_find_gc_block(c);

	if (!jeb) {
David Woodhouse's avatar
David Woodhouse committed
208
		D1 (printk(KERN_NOTICE "jffs2: Couldn't find erase block to garbage collect!\n"));
David Woodhouse's avatar
David Woodhouse committed
209
		spin_unlock(&c->erase_completion_lock);
Linus Torvalds's avatar
Linus Torvalds committed
210 211 212 213
		up(&c->alloc_sem);
		return -EIO;
	}

David Woodhouse's avatar
David Woodhouse committed
214 215
	D1(printk(KERN_DEBUG "GC from block %08x, used_size %08x, dirty_size %08x, free_size %08x\n", jeb->offset, jeb->used_size, jeb->dirty_size, jeb->free_size));
	D1(if (c->nextblock)
David Woodhouse's avatar
David Woodhouse committed
216
	   printk(KERN_DEBUG "Nextblock at  %08x, used_size %08x, dirty_size %08x, wasted_size %08x, free_size %08x\n", c->nextblock->offset, c->nextblock->used_size, c->nextblock->dirty_size, c->nextblock->wasted_size, c->nextblock->free_size));
Linus Torvalds's avatar
Linus Torvalds committed
217

David Woodhouse's avatar
David Woodhouse committed
218 219
	if (!jeb->used_size) {
		up(&c->alloc_sem);
Linus Torvalds's avatar
Linus Torvalds committed
220
		goto eraseit;
David Woodhouse's avatar
David Woodhouse committed
221
	}
Linus Torvalds's avatar
Linus Torvalds committed
222 223 224

	raw = jeb->gc_node;
			
David Woodhouse's avatar
David Woodhouse committed
225 226
	while(ref_obsolete(raw)) {
		D1(printk(KERN_DEBUG "Node at 0x%08x is obsolete... skipping\n", ref_offset(raw)));
David Woodhouse's avatar
David Woodhouse committed
227 228
		raw = raw->next_phys;
		if (unlikely(!raw)) {
Linus Torvalds's avatar
Linus Torvalds committed
229 230 231
			printk(KERN_WARNING "eep. End of raw list while still supposedly nodes to GC\n");
			printk(KERN_WARNING "erase block at 0x%08x. free_size 0x%08x, dirty_size 0x%08x, used_size 0x%08x\n", 
			       jeb->offset, jeb->free_size, jeb->dirty_size, jeb->used_size);
David Woodhouse's avatar
David Woodhouse committed
232
			jeb->gc_node = raw;
David Woodhouse's avatar
David Woodhouse committed
233
			spin_unlock(&c->erase_completion_lock);
Linus Torvalds's avatar
Linus Torvalds committed
234 235 236 237
			up(&c->alloc_sem);
			BUG();
		}
	}
David Woodhouse's avatar
David Woodhouse committed
238 239
	jeb->gc_node = raw;

David Woodhouse's avatar
David Woodhouse committed
240
	D1(printk(KERN_DEBUG "Going to garbage collect node at 0x%08x\n", ref_offset(raw)));
David Woodhouse's avatar
David Woodhouse committed
241

Linus Torvalds's avatar
Linus Torvalds committed
242 243
	if (!raw->next_in_ino) {
		/* Inode-less node. Clean marker, snapshot or something like that */
244 245
		/* FIXME: If it's something that needs to be copied, including something
		   we don't grok that has JFFS2_NODETYPE_RWCOMPAT_COPY, we should do so */
David Woodhouse's avatar
David Woodhouse committed
246
		spin_unlock(&c->erase_completion_lock);
Linus Torvalds's avatar
Linus Torvalds committed
247
		jffs2_mark_node_obsolete(c, raw);
David Woodhouse's avatar
David Woodhouse committed
248
		up(&c->alloc_sem);
Linus Torvalds's avatar
Linus Torvalds committed
249 250
		goto eraseit_lock;
	}
David Woodhouse's avatar
David Woodhouse committed
251 252 253 254 255 256 257

	ic = jffs2_raw_ref_to_ic(raw);

	/* We need to hold the inocache. Either the erase_completion_lock or
	   the inocache_lock are sufficient; we trade down since the inocache_lock 
	   causes less contention. */
	spin_lock(&c->inocache_lock);
Linus Torvalds's avatar
Linus Torvalds committed
258

David Woodhouse's avatar
David Woodhouse committed
259 260
	spin_unlock(&c->erase_completion_lock);

David Woodhouse's avatar
David Woodhouse committed
261
	D1(printk(KERN_DEBUG "jffs2_garbage_collect_pass collecting from block @0x%08x. Node @0x%08x(%d), ino #%u\n", jeb->offset, ref_offset(raw), ref_flags(raw), ic->ino));
David Woodhouse's avatar
David Woodhouse committed
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280

	/* Three possibilities:
	   1. Inode is already in-core. We must iget it and do proper
	      updating to its fragtree, etc.
	   2. Inode is not in-core, node is REF_PRISTINE. We lock the
	      inocache to prevent a read_inode(), copy the node intact.
	   3. Inode is not in-core, node is not pristine. We must iget()
	      and take the slow path.
	*/

	switch(ic->state) {
	case INO_STATE_CHECKEDABSENT:
		/* It's been checked, but it's not currently in-core. 
		   We can just copy any pristine nodes, but have
		   to prevent anyone else from doing read_inode() while
		   we're at it, so we set the state accordingly */
		if (ref_flags(raw) == REF_PRISTINE)
			ic->state = INO_STATE_GC;
		else {
281
			D1(printk(KERN_DEBUG "Ino #%u is absent but node not REF_PRISTINE. Reading.\n", 
David Woodhouse's avatar
David Woodhouse committed
282
				  ic->ino));
David Woodhouse's avatar
David Woodhouse committed
283 284 285 286
		}
		break;

	case INO_STATE_PRESENT:
David Woodhouse's avatar
David Woodhouse committed
287
		/* It's in-core. GC must iget() it. */
David Woodhouse's avatar
David Woodhouse committed
288 289
		break;

David Woodhouse's avatar
David Woodhouse committed
290
	case INO_STATE_UNCHECKED:
David Woodhouse's avatar
David Woodhouse committed
291
	case INO_STATE_CHECKING:
David Woodhouse's avatar
David Woodhouse committed
292
	case INO_STATE_GC:
David Woodhouse's avatar
David Woodhouse committed
293
		/* Should never happen. We should have finished checking
David Woodhouse's avatar
David Woodhouse committed
294 295 296 297 298 299 300 301
		   by the time we actually start doing any GC, and since 
		   we're holding the alloc_sem, no other garbage collection 
		   can happen.
		*/
		printk(KERN_CRIT "Inode #%u already in state %d in jffs2_garbage_collect_pass()!\n",
		       ic->ino, ic->state);
		up(&c->alloc_sem);
		spin_unlock(&c->inocache_lock);
David Woodhouse's avatar
David Woodhouse committed
302 303 304 305 306 307 308 309 310 311 312
		BUG();

	case INO_STATE_READING:
		/* Someone's currently trying to read it. We must wait for
		   them to finish and then go through the full iget() route
		   to do the GC. However, sometimes read_inode() needs to get
		   the alloc_sem() (for marking nodes invalid) so we must
		   drop the alloc_sem before sleeping. */

		up(&c->alloc_sem);
		D1(printk(KERN_DEBUG "jffs2_garbage_collect_pass() waiting for ino #%u in state %d\n",
David Woodhouse's avatar
David Woodhouse committed
313
			  ic->ino, ic->state));
David Woodhouse's avatar
David Woodhouse committed
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
		sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
		/* And because we dropped the alloc_sem we must start again from the 
		   beginning. Ponder chance of livelock here -- we're returning success
		   without actually making any progress.

		   Q: What are the chances that the inode is back in INO_STATE_READING 
		   again by the time we next enter this function? And that this happens
		   enough times to cause a real delay?

		   A: Small enough that I don't care :) 
		*/
		return 0;
	}

	/* OK. Now if the inode is in state INO_STATE_GC, we are going to copy the
	   node intact, and we don't have to muck about with the fragtree etc. 
	   because we know it's not in-core. If it _was_ in-core, we go through
	   all the iget() crap anyway */

	if (ic->state == INO_STATE_GC) {
David Woodhouse's avatar
David Woodhouse committed
334 335
		spin_unlock(&c->inocache_lock);

David Woodhouse's avatar
David Woodhouse committed
336 337
		ret = jffs2_garbage_collect_pristine(c, ic, raw);

David Woodhouse's avatar
David Woodhouse committed
338 339 340 341 342 343
		spin_lock(&c->inocache_lock);
		ic->state = INO_STATE_CHECKEDABSENT;
		wake_up(&c->inocache_wq);

		if (ret != -EBADFD) {
			spin_unlock(&c->inocache_lock);
David Woodhouse's avatar
David Woodhouse committed
344
			goto release_sem;
David Woodhouse's avatar
David Woodhouse committed
345
		}
David Woodhouse's avatar
David Woodhouse committed
346

David Woodhouse's avatar
David Woodhouse committed
347
		/* Fall through if it wanted us to, with inocache_lock held */
David Woodhouse's avatar
David Woodhouse committed
348 349
	}

David Woodhouse's avatar
David Woodhouse committed
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
	/* Prevent the fairly unlikely race where the gcblock is
	   entirely obsoleted by the final close of a file which had
	   the only valid nodes in the block, followed by erasure,
	   followed by freeing of the ic because the erased block(s)
	   held _all_ the nodes of that inode.... never been seen but
	   it's vaguely possible. */

	inum = ic->ino;
	nlink = ic->nlink;
	spin_unlock(&c->inocache_lock);

	f = jffs2_gc_fetch_inode(c, inum, nlink);
	if (IS_ERR(f))
		return PTR_ERR(f);
	if (!f)
		return 0;

	ret = jffs2_garbage_collect_live(c, jeb, raw, f);

	jffs2_gc_release_inode(c, f);
David Woodhouse's avatar
David Woodhouse committed
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390

 release_sem:
	up(&c->alloc_sem);

 eraseit_lock:
	/* If we've finished this block, start it erasing */
	spin_lock(&c->erase_completion_lock);

 eraseit:
	if (c->gcblock && !c->gcblock->used_size) {
		D1(printk(KERN_DEBUG "Block at 0x%08x completely obsoleted by GC. Moving to erase_pending_list\n", c->gcblock->offset));
		/* We're GC'ing an empty block? */
		list_add_tail(&c->gcblock->list, &c->erase_pending_list);
		c->gcblock = NULL;
		c->nr_erasing_blocks++;
		jffs2_erase_pending_trigger(c);
	}
	spin_unlock(&c->erase_completion_lock);

	return ret;
}
Linus Torvalds's avatar
Linus Torvalds committed
391

David Woodhouse's avatar
David Woodhouse committed
392
static int jffs2_garbage_collect_live(struct jffs2_sb_info *c,  struct jffs2_eraseblock *jeb,
David Woodhouse's avatar
David Woodhouse committed
393
				      struct jffs2_raw_node_ref *raw, struct jffs2_inode_info *f)
David Woodhouse's avatar
David Woodhouse committed
394 395 396 397 398 399 400
{
	struct jffs2_node_frag *frag;
	struct jffs2_full_dnode *fn = NULL;
	struct jffs2_full_dirent *fd;
	uint32_t start = 0, end = 0, nrfrags = 0;
	int ret = 0;

Linus Torvalds's avatar
Linus Torvalds committed
401
	down(&f->sem);
402

Linus Torvalds's avatar
Linus Torvalds committed
403 404 405
	/* Now we have the lock for this inode. Check that it's still the one at the head
	   of the list. */

David Woodhouse's avatar
David Woodhouse committed
406 407 408 409 410 411 412
	spin_lock(&c->erase_completion_lock);

	if (c->gcblock != jeb) {
		spin_unlock(&c->erase_completion_lock);
		D1(printk(KERN_DEBUG "GC block is no longer gcblock. Restart\n"));
		goto upnout;
	}
David Woodhouse's avatar
David Woodhouse committed
413
	if (ref_obsolete(raw)) {
David Woodhouse's avatar
David Woodhouse committed
414
		spin_unlock(&c->erase_completion_lock);
Linus Torvalds's avatar
Linus Torvalds committed
415 416 417 418
		D1(printk(KERN_DEBUG "node to be GC'd was obsoleted in the meantime.\n"));
		/* They'll call again */
		goto upnout;
	}
David Woodhouse's avatar
David Woodhouse committed
419 420
	spin_unlock(&c->erase_completion_lock);

Linus Torvalds's avatar
Linus Torvalds committed
421 422 423
	/* OK. Looks safe. And nobody can get us now because we have the semaphore. Move the block */
	if (f->metadata && f->metadata->raw == raw) {
		fn = f->metadata;
424
		ret = jffs2_garbage_collect_metadata(c, jeb, f, fn);
Linus Torvalds's avatar
Linus Torvalds committed
425 426
		goto upnout;
	}
David Woodhouse's avatar
David Woodhouse committed
427 428 429

	/* FIXME. Read node and do lookup? */
	for (frag = frag_first(&f->fragtree); frag; frag = frag_next(frag)) {
Linus Torvalds's avatar
Linus Torvalds committed
430 431 432 433 434 435 436 437 438 439
		if (frag->node && frag->node->raw == raw) {
			fn = frag->node;
			end = frag->ofs + frag->size;
			if (!nrfrags++)
				start = frag->ofs;
			if (nrfrags == frag->node->frags)
				break; /* We've found them all */
		}
	}
	if (fn) {
David Woodhouse's avatar
David Woodhouse committed
440
		if (ref_flags(raw) == REF_PRISTINE) {
David Woodhouse's avatar
David Woodhouse committed
441
			ret = jffs2_garbage_collect_pristine(c, f->inocache, raw);
David Woodhouse's avatar
David Woodhouse committed
442 443
			if (!ret) {
				/* Urgh. Return it sensibly. */
David Woodhouse's avatar
David Woodhouse committed
444
				frag->node->raw = f->inocache->nodes;
David Woodhouse's avatar
David Woodhouse committed
445 446 447 448
			}	
			if (ret != -EBADFD)
				goto upnout;
		}
Linus Torvalds's avatar
Linus Torvalds committed
449 450 451
		/* We found a datanode. Do the GC */
		if((start >> PAGE_CACHE_SHIFT) < ((end-1) >> PAGE_CACHE_SHIFT)) {
			/* It crosses a page boundary. Therefore, it must be a hole. */
452
			ret = jffs2_garbage_collect_hole(c, jeb, f, fn, start, end);
Linus Torvalds's avatar
Linus Torvalds committed
453 454
		} else {
			/* It could still be a hole. But we GC the page this way anyway */
455
			ret = jffs2_garbage_collect_dnode(c, jeb, f, fn, start, end);
Linus Torvalds's avatar
Linus Torvalds committed
456 457 458 459 460 461 462 463 464 465 466
		}
		goto upnout;
	}
	
	/* Wasn't a dnode. Try dirent */
	for (fd = f->dents; fd; fd=fd->next) {
		if (fd->raw == raw)
			break;
	}

	if (fd && fd->ino) {
467
		ret = jffs2_garbage_collect_dirent(c, jeb, f, fd);
Linus Torvalds's avatar
Linus Torvalds committed
468
	} else if (fd) {
469
		ret = jffs2_garbage_collect_deletion_dirent(c, jeb, f, fd);
Linus Torvalds's avatar
Linus Torvalds committed
470
	} else {
471
		printk(KERN_WARNING "Raw node at 0x%08x wasn't in node lists for ino #%u\n",
David Woodhouse's avatar
David Woodhouse committed
472 473
		       ref_offset(raw), f->inocache->ino);
		if (ref_obsolete(raw)) {
Linus Torvalds's avatar
Linus Torvalds committed
474 475 476 477 478 479 480 481
			printk(KERN_WARNING "But it's obsolete so we don't mind too much\n");
		} else {
			ret = -EIO;
		}
	}
 upnout:
	up(&f->sem);

David Woodhouse's avatar
David Woodhouse committed
482 483
	return ret;
}
Linus Torvalds's avatar
Linus Torvalds committed
484

David Woodhouse's avatar
David Woodhouse committed
485 486 487 488 489 490 491 492 493
static int jffs2_garbage_collect_pristine(struct jffs2_sb_info *c, 
					  struct jffs2_inode_cache *ic,
					  struct jffs2_raw_node_ref *raw)
{
	union jffs2_node_union *node;
	struct jffs2_raw_node_ref *nraw;
	size_t retlen;
	int ret;
	uint32_t phys_ofs, alloclen;
David Woodhouse's avatar
David Woodhouse committed
494
	uint32_t crc, rawlen;
495
	int retried = 0;
David Woodhouse's avatar
David Woodhouse committed
496 497 498

	D1(printk(KERN_DEBUG "Going to GC REF_PRISTINE node at 0x%08x\n", ref_offset(raw)));

David Woodhouse's avatar
David Woodhouse committed
499 500
	rawlen = ref_totlen(c, c->gcblock, raw);

David Woodhouse's avatar
David Woodhouse committed
501 502 503
	/* Ask for a small amount of space (or the totlen if smaller) because we
	   don't want to force wastage of the end of a block if splitting would
	   work. */
David Woodhouse's avatar
David Woodhouse committed
504 505
	ret = jffs2_reserve_space_gc(c, min_t(uint32_t, sizeof(struct jffs2_raw_inode) + JFFS2_MIN_DATA_LEN, 
					      rawlen), &phys_ofs, &alloclen);
David Woodhouse's avatar
David Woodhouse committed
506 507 508
	if (ret)
		return ret;

David Woodhouse's avatar
David Woodhouse committed
509
	if (alloclen < rawlen) {
David Woodhouse's avatar
David Woodhouse committed
510 511 512 513
		/* Doesn't fit untouched. We'll go the old route and split it */
		return -EBADFD;
	}

David Woodhouse's avatar
David Woodhouse committed
514
	node = kmalloc(rawlen, GFP_KERNEL);
David Woodhouse's avatar
David Woodhouse committed
515 516 517
	if (!node)
               return -ENOMEM;

David Woodhouse's avatar
David Woodhouse committed
518 519
	ret = jffs2_flash_read(c, ref_offset(raw), rawlen, &retlen, (char *)node);
	if (!ret && retlen != rawlen)
David Woodhouse's avatar
David Woodhouse committed
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
		ret = -EIO;
	if (ret)
		goto out_node;

	crc = crc32(0, node, sizeof(struct jffs2_unknown_node)-4);
	if (je32_to_cpu(node->u.hdr_crc) != crc) {
		printk(KERN_WARNING "Header CRC failed on REF_PRISTINE node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
		       ref_offset(raw), je32_to_cpu(node->u.hdr_crc), crc);
		goto bail;
	}

	switch(je16_to_cpu(node->u.nodetype)) {
	case JFFS2_NODETYPE_INODE:
		crc = crc32(0, node, sizeof(node->i)-8);
		if (je32_to_cpu(node->i.node_crc) != crc) {
			printk(KERN_WARNING "Node CRC failed on REF_PRISTINE data node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
			       ref_offset(raw), je32_to_cpu(node->i.node_crc), crc);
			goto bail;
		}

		if (je32_to_cpu(node->i.dsize)) {
			crc = crc32(0, node->i.data, je32_to_cpu(node->i.csize));
			if (je32_to_cpu(node->i.data_crc) != crc) {
				printk(KERN_WARNING "Data CRC failed on REF_PRISTINE data node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
				       ref_offset(raw), je32_to_cpu(node->i.data_crc), crc);
				goto bail;
			}
		}
		break;

	case JFFS2_NODETYPE_DIRENT:
		crc = crc32(0, node, sizeof(node->d)-8);
		if (je32_to_cpu(node->d.node_crc) != crc) {
			printk(KERN_WARNING "Node CRC failed on REF_PRISTINE dirent node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
			       ref_offset(raw), je32_to_cpu(node->d.node_crc), crc);
			goto bail;
		}

		if (node->d.nsize) {
			crc = crc32(0, node->d.name, node->d.nsize);
			if (je32_to_cpu(node->d.name_crc) != crc) {
				printk(KERN_WARNING "Name CRC failed on REF_PRISTINE dirent ode at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
				       ref_offset(raw), je32_to_cpu(node->d.name_crc), crc);
				goto bail;
			}
		}
		break;
	default:
		printk(KERN_WARNING "Unknown node type for REF_PRISTINE node at 0x%08x: 0x%04x\n", 
		       ref_offset(raw), je16_to_cpu(node->u.nodetype));
		goto bail;
Linus Torvalds's avatar
Linus Torvalds committed
571 572
	}

David Woodhouse's avatar
David Woodhouse committed
573 574 575 576 577
	nraw = jffs2_alloc_raw_node_ref();
	if (!nraw) {
		ret = -ENOMEM;
		goto out_node;
	}
578 579 580

	/* OK, all the CRCs are good; this node can just be copied as-is. */
 retry:
David Woodhouse's avatar
David Woodhouse committed
581
	nraw->flash_offset = phys_ofs;
David Woodhouse's avatar
David Woodhouse committed
582
	nraw->__totlen = rawlen;
David Woodhouse's avatar
David Woodhouse committed
583 584
	nraw->next_phys = NULL;

David Woodhouse's avatar
David Woodhouse committed
585
	ret = jffs2_flash_write(c, phys_ofs, rawlen, &retlen, (char *)node);
586

David Woodhouse's avatar
David Woodhouse committed
587
	if (ret || (retlen != rawlen)) {
David Woodhouse's avatar
David Woodhouse committed
588
		printk(KERN_NOTICE "Write of %d bytes at 0x%08x failed. returned %d, retlen %zd\n",
David Woodhouse's avatar
David Woodhouse committed
589
                       rawlen, phys_ofs, ret, retlen);
David Woodhouse's avatar
David Woodhouse committed
590 591 592 593 594 595 596 597 598
		if (retlen) {
                        /* Doesn't belong to any inode */
			nraw->next_in_ino = NULL;

			nraw->flash_offset |= REF_OBSOLETE;
			jffs2_add_physical_node_ref(c, nraw);
			jffs2_mark_node_obsolete(c, nraw);
		} else {
			printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", nraw->flash_offset);
599
                        jffs2_free_raw_node_ref(nraw);
David Woodhouse's avatar
David Woodhouse committed
600
		}
601 602 603 604 605 606 607 608 609 610 611 612
		if (!retried && (nraw == jffs2_alloc_raw_node_ref())) {
			/* Try to reallocate space and retry */
			uint32_t dummy;
			struct jffs2_eraseblock *jeb = &c->blocks[phys_ofs / c->sector_size];

			retried = 1;

			D1(printk(KERN_DEBUG "Retrying failed write of REF_PRISTINE node.\n"));
			
			ACCT_SANITY_CHECK(c,jeb);
			D1(ACCT_PARANOIA_CHECK(jeb));

David Woodhouse's avatar
David Woodhouse committed
613
			ret = jffs2_reserve_space_gc(c, rawlen, &phys_ofs, &dummy);
614 615 616 617 618 619 620 621 622 623 624 625 626

			if (!ret) {
				D1(printk(KERN_DEBUG "Allocated space at 0x%08x to retry failed write.\n", phys_ofs));

				ACCT_SANITY_CHECK(c,jeb);
				D1(ACCT_PARANOIA_CHECK(jeb));

				goto retry;
			}
			D1(printk(KERN_DEBUG "Failed to allocate space to retry failed write: %d!\n", ret));
			jffs2_free_raw_node_ref(nraw);
		}

David Woodhouse's avatar
David Woodhouse committed
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
		if (!ret)
			ret = -EIO;
		goto out_node;
	}
	nraw->flash_offset |= REF_PRISTINE;
	jffs2_add_physical_node_ref(c, nraw);

	/* Link into per-inode list. This is safe because of the ic
	   state being INO_STATE_GC. Note that if we're doing this
	   for an inode which is in-code, the 'nraw' pointer is then
	   going to be fetched from ic->nodes by our caller. */
        nraw->next_in_ino = ic->nodes;
        ic->nodes = nraw;

	jffs2_mark_node_obsolete(c, raw);
	D1(printk(KERN_DEBUG "WHEEE! GC REF_PRISTINE node at 0x%08x succeeded\n", ref_offset(raw)));

 out_node:
	kfree(node);
Linus Torvalds's avatar
Linus Torvalds committed
646
	return ret;
David Woodhouse's avatar
David Woodhouse committed
647 648 649
 bail:
	ret = -EBADFD;
	goto out_node;
Linus Torvalds's avatar
Linus Torvalds committed
650 651 652
}

static int jffs2_garbage_collect_metadata(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, 
653
					struct jffs2_inode_info *f, struct jffs2_full_dnode *fn)
Linus Torvalds's avatar
Linus Torvalds committed
654 655 656
{
	struct jffs2_full_dnode *new_fn;
	struct jffs2_raw_inode ri;
David Woodhouse's avatar
David Woodhouse committed
657
	jint16_t dev;
Linus Torvalds's avatar
Linus Torvalds committed
658
	char *mdata = NULL, mdatalen = 0;
659
	uint32_t alloclen, phys_ofs;
Linus Torvalds's avatar
Linus Torvalds committed
660 661
	int ret;

662 663
	if (S_ISBLK(JFFS2_F_I_MODE(f)) ||
	    S_ISCHR(JFFS2_F_I_MODE(f)) ) {
Linus Torvalds's avatar
Linus Torvalds committed
664
		/* For these, we don't actually need to read the old node */
665
		/* FIXME: for minor or major > 255. */
David Woodhouse's avatar
David Woodhouse committed
666 667
		dev = cpu_to_je16(((JFFS2_F_I_RDEV_MAJ(f) << 8) | 
			JFFS2_F_I_RDEV_MIN(f)));
Linus Torvalds's avatar
Linus Torvalds committed
668 669 670
		mdata = (char *)&dev;
		mdatalen = sizeof(dev);
		D1(printk(KERN_DEBUG "jffs2_garbage_collect_metadata(): Writing %d bytes of kdev_t\n", mdatalen));
671
	} else if (S_ISLNK(JFFS2_F_I_MODE(f))) {
Linus Torvalds's avatar
Linus Torvalds committed
672 673 674 675 676 677
		mdatalen = fn->size;
		mdata = kmalloc(fn->size, GFP_KERNEL);
		if (!mdata) {
			printk(KERN_WARNING "kmalloc of mdata failed in jffs2_garbage_collect_metadata()\n");
			return -ENOMEM;
		}
David Woodhouse's avatar
David Woodhouse committed
678
		ret = jffs2_read_dnode(c, f, fn, mdata, 0, mdatalen);
Linus Torvalds's avatar
Linus Torvalds committed
679 680 681 682 683 684 685 686 687 688 689
		if (ret) {
			printk(KERN_WARNING "read of old metadata failed in jffs2_garbage_collect_metadata(): %d\n", ret);
			kfree(mdata);
			return ret;
		}
		D1(printk(KERN_DEBUG "jffs2_garbage_collect_metadata(): Writing %d bites of symlink target\n", mdatalen));

	}
	
	ret = jffs2_reserve_space_gc(c, sizeof(ri) + mdatalen, &phys_ofs, &alloclen);
	if (ret) {
David Woodhouse's avatar
David Woodhouse committed
690
		printk(KERN_WARNING "jffs2_reserve_space_gc of %zd bytes for garbage_collect_metadata failed: %d\n",
Linus Torvalds's avatar
Linus Torvalds committed
691 692 693 694 695
		       sizeof(ri)+ mdatalen, ret);
		goto out;
	}
	
	memset(&ri, 0, sizeof(ri));
David Woodhouse's avatar
David Woodhouse committed
696 697 698 699 700 701 702
	ri.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
	ri.nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
	ri.totlen = cpu_to_je32(sizeof(ri) + mdatalen);
	ri.hdr_crc = cpu_to_je32(crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4));

	ri.ino = cpu_to_je32(f->inocache->ino);
	ri.version = cpu_to_je32(++f->highest_version);
David Woodhouse's avatar
David Woodhouse committed
703
	ri.mode = cpu_to_jemode(JFFS2_F_I_MODE(f));
David Woodhouse's avatar
David Woodhouse committed
704 705 706 707 708 709 710 711 712
	ri.uid = cpu_to_je16(JFFS2_F_I_UID(f));
	ri.gid = cpu_to_je16(JFFS2_F_I_GID(f));
	ri.isize = cpu_to_je32(JFFS2_F_I_SIZE(f));
	ri.atime = cpu_to_je32(JFFS2_F_I_ATIME(f));
	ri.ctime = cpu_to_je32(JFFS2_F_I_CTIME(f));
	ri.mtime = cpu_to_je32(JFFS2_F_I_MTIME(f));
	ri.offset = cpu_to_je32(0);
	ri.csize = cpu_to_je32(mdatalen);
	ri.dsize = cpu_to_je32(mdatalen);
Linus Torvalds's avatar
Linus Torvalds committed
713
	ri.compr = JFFS2_COMPR_NONE;
David Woodhouse's avatar
David Woodhouse committed
714 715
	ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
	ri.data_crc = cpu_to_je32(crc32(0, mdata, mdatalen));
Linus Torvalds's avatar
Linus Torvalds committed
716

717
	new_fn = jffs2_write_dnode(c, f, &ri, mdata, mdatalen, phys_ofs, ALLOC_GC);
Linus Torvalds's avatar
Linus Torvalds committed
718 719 720 721 722 723 724 725 726 727

	if (IS_ERR(new_fn)) {
		printk(KERN_WARNING "Error writing new dnode: %ld\n", PTR_ERR(new_fn));
		ret = PTR_ERR(new_fn);
		goto out;
	}
	jffs2_mark_node_obsolete(c, fn->raw);
	jffs2_free_full_dnode(fn);
	f->metadata = new_fn;
 out:
728
	if (S_ISLNK(JFFS2_F_I_MODE(f)))
Linus Torvalds's avatar
Linus Torvalds committed
729 730 731 732 733
		kfree(mdata);
	return ret;
}

static int jffs2_garbage_collect_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, 
734
					struct jffs2_inode_info *f, struct jffs2_full_dirent *fd)
Linus Torvalds's avatar
Linus Torvalds committed
735 736 737
{
	struct jffs2_full_dirent *new_fd;
	struct jffs2_raw_dirent rd;
738
	uint32_t alloclen, phys_ofs;
Linus Torvalds's avatar
Linus Torvalds committed
739 740
	int ret;

David Woodhouse's avatar
David Woodhouse committed
741 742
	rd.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
	rd.nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
Linus Torvalds's avatar
Linus Torvalds committed
743
	rd.nsize = strlen(fd->name);
David Woodhouse's avatar
David Woodhouse committed
744 745
	rd.totlen = cpu_to_je32(sizeof(rd) + rd.nsize);
	rd.hdr_crc = cpu_to_je32(crc32(0, &rd, sizeof(struct jffs2_unknown_node)-4));
Linus Torvalds's avatar
Linus Torvalds committed
746

David Woodhouse's avatar
David Woodhouse committed
747 748 749 750
	rd.pino = cpu_to_je32(f->inocache->ino);
	rd.version = cpu_to_je32(++f->highest_version);
	rd.ino = cpu_to_je32(fd->ino);
	rd.mctime = cpu_to_je32(max(JFFS2_F_I_MTIME(f), JFFS2_F_I_CTIME(f)));
Linus Torvalds's avatar
Linus Torvalds committed
751
	rd.type = fd->type;
David Woodhouse's avatar
David Woodhouse committed
752 753
	rd.node_crc = cpu_to_je32(crc32(0, &rd, sizeof(rd)-8));
	rd.name_crc = cpu_to_je32(crc32(0, fd->name, rd.nsize));
Linus Torvalds's avatar
Linus Torvalds committed
754 755 756
	
	ret = jffs2_reserve_space_gc(c, sizeof(rd)+rd.nsize, &phys_ofs, &alloclen);
	if (ret) {
David Woodhouse's avatar
David Woodhouse committed
757
		printk(KERN_WARNING "jffs2_reserve_space_gc of %zd bytes for garbage_collect_dirent failed: %d\n",
Linus Torvalds's avatar
Linus Torvalds committed
758 759 760
		       sizeof(rd)+rd.nsize, ret);
		return ret;
	}
761
	new_fd = jffs2_write_dirent(c, f, &rd, fd->name, rd.nsize, phys_ofs, ALLOC_GC);
Linus Torvalds's avatar
Linus Torvalds committed
762 763 764 765 766 767 768 769 770 771

	if (IS_ERR(new_fd)) {
		printk(KERN_WARNING "jffs2_write_dirent in garbage_collect_dirent failed: %ld\n", PTR_ERR(new_fd));
		return PTR_ERR(new_fd);
	}
	jffs2_add_fd_to_list(c, new_fd, &f->dents);
	return 0;
}

static int jffs2_garbage_collect_deletion_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, 
772
					struct jffs2_inode_info *f, struct jffs2_full_dirent *fd)
Linus Torvalds's avatar
Linus Torvalds committed
773 774 775 776
{
	struct jffs2_full_dirent **fdp = &f->dents;
	int found = 0;

777 778 779 780 781 782
	/* On a medium where we can't actually mark nodes obsolete
	   pernamently, such as NAND flash, we need to work out
	   whether this deletion dirent is still needed to actively
	   delete a 'real' dirent with the same name that's still
	   somewhere else on the flash. */
	if (!jffs2_can_mark_obsolete(c)) {
David Woodhouse's avatar
David Woodhouse committed
783
		struct jffs2_raw_dirent *rd;
784 785 786 787 788
		struct jffs2_raw_node_ref *raw;
		int ret;
		size_t retlen;
		int name_len = strlen(fd->name);
		uint32_t name_crc = crc32(0, fd->name, name_len);
David Woodhouse's avatar
David Woodhouse committed
789 790 791 792 793
		uint32_t rawlen = ref_totlen(c, jeb, fd->raw);

		rd = kmalloc(rawlen, GFP_KERNEL);
		if (!rd)
			return -ENOMEM;
794 795 796 797 798 799 800

		/* Prevent the erase code from nicking the obsolete node refs while
		   we're looking at them. I really don't like this extra lock but
		   can't see any alternative. Suggestions on a postcard to... */
		down(&c->erase_free_sem);

		for (raw = f->inocache->nodes; raw != (void *)f->inocache; raw = raw->next_in_ino) {
David Woodhouse's avatar
David Woodhouse committed
801

802
			/* We only care about obsolete ones */
David Woodhouse's avatar
David Woodhouse committed
803
			if (!(ref_obsolete(raw)))
804 805
				continue;

David Woodhouse's avatar
David Woodhouse committed
806 807 808 809
			/* Any dirent with the same name is going to have the same length... */
			if (ref_totlen(c, NULL, raw) != rawlen)
				continue;

810 811 812 813 814 815
			/* Doesn't matter if there's one in the same erase block. We're going to 
			   delete it too at the same time. */
			if ((raw->flash_offset & ~(c->sector_size-1)) ==
			    (fd->raw->flash_offset & ~(c->sector_size-1)))
				continue;

David Woodhouse's avatar
David Woodhouse committed
816 817 818 819 820
			D1(printk(KERN_DEBUG "Check potential deletion dirent at %08x\n", ref_offset(raw)));

			/* This is an obsolete node belonging to the same directory, and it's of the right
			   length. We need to take a closer look...*/
			ret = jffs2_flash_read(c, ref_offset(raw), rawlen, &retlen, (char *)rd);
821
			if (ret) {
David Woodhouse's avatar
David Woodhouse committed
822
				printk(KERN_WARNING "jffs2_g_c_deletion_dirent(): Read error (%d) reading obsolete node at %08x\n", ret, ref_offset(raw));
David Woodhouse's avatar
David Woodhouse committed
823
				/* If we can't read it, we don't need to continue to obsolete it. Continue */
824 825
				continue;
			}
David Woodhouse's avatar
David Woodhouse committed
826
			if (retlen != rawlen) {
David Woodhouse's avatar
David Woodhouse committed
827
				printk(KERN_WARNING "jffs2_g_c_deletion_dirent(): Short read (%zd not %zd) reading header from obsolete node at %08x\n",
David Woodhouse's avatar
David Woodhouse committed
828
				       retlen, rawlen, ref_offset(raw));
829 830 831
				continue;
			}

David Woodhouse's avatar
David Woodhouse committed
832
			if (je16_to_cpu(rd->nodetype) != JFFS2_NODETYPE_DIRENT)
833 834 835
				continue;

			/* If the name CRC doesn't match, skip */
David Woodhouse's avatar
David Woodhouse committed
836
			if (je32_to_cpu(rd->name_crc) != name_crc)
837
				continue;
David Woodhouse's avatar
David Woodhouse committed
838

839
			/* If the name length doesn't match, or it's another deletion dirent, skip */
David Woodhouse's avatar
David Woodhouse committed
840
			if (rd->nsize != name_len || !je32_to_cpu(rd->ino))
841 842 843
				continue;

			/* OK, check the actual name now */
David Woodhouse's avatar
David Woodhouse committed
844
			if (memcmp(rd->name, fd->name, name_len))
845 846 847 848 849 850
				continue;

			/* OK. The name really does match. There really is still an older node on
			   the flash which our deletion dirent obsoletes. So we have to write out
			   a new deletion dirent to replace it */
			up(&c->erase_free_sem);
David Woodhouse's avatar
David Woodhouse committed
851 852 853 854 855

			D1(printk(KERN_DEBUG "Deletion dirent at %08x still obsoletes real dirent \"%s\" at %08x for ino #%u\n",
				  ref_offset(fd->raw), fd->name, ref_offset(raw), je32_to_cpu(rd->ino)));
			kfree(rd);

856 857 858 859
			return jffs2_garbage_collect_dirent(c, jeb, f, fd);
		}

		up(&c->erase_free_sem);
David Woodhouse's avatar
David Woodhouse committed
860
		kfree(rd);
861 862 863
	}

	/* No need for it any more. Just mark it obsolete and remove it from the list */
Linus Torvalds's avatar
Linus Torvalds committed
864 865 866 867 868 869 870 871 872
	while (*fdp) {
		if ((*fdp) == fd) {
			found = 1;
			*fdp = fd->next;
			break;
		}
		fdp = &(*fdp)->next;
	}
	if (!found) {
873
		printk(KERN_WARNING "Deletion dirent \"%s\" not found in list for ino #%u\n", fd->name, f->inocache->ino);
Linus Torvalds's avatar
Linus Torvalds committed
874 875 876 877 878 879 880
	}
	jffs2_mark_node_obsolete(c, fd->raw);
	jffs2_free_full_dirent(fd);
	return 0;
}

static int jffs2_garbage_collect_hole(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
881 882
				      struct jffs2_inode_info *f, struct jffs2_full_dnode *fn,
				      uint32_t start, uint32_t end)
Linus Torvalds's avatar
Linus Torvalds committed
883 884 885 886
{
	struct jffs2_raw_inode ri;
	struct jffs2_node_frag *frag;
	struct jffs2_full_dnode *new_fn;
887
	uint32_t alloclen, phys_ofs;
Linus Torvalds's avatar
Linus Torvalds committed
888 889
	int ret;

890 891
	D1(printk(KERN_DEBUG "Writing replacement hole node for ino #%u from offset 0x%x to 0x%x\n",
		  f->inocache->ino, start, end));
Linus Torvalds's avatar
Linus Torvalds committed
892 893 894 895 896
	
	memset(&ri, 0, sizeof(ri));

	if(fn->frags > 1) {
		size_t readlen;
897
		uint32_t crc;
Linus Torvalds's avatar
Linus Torvalds committed
898 899
		/* It's partially obsoleted by a later write. So we have to 
		   write it out again with the _same_ version as before */
David Woodhouse's avatar
David Woodhouse committed
900
		ret = jffs2_flash_read(c, ref_offset(fn->raw), sizeof(ri), &readlen, (char *)&ri);
Linus Torvalds's avatar
Linus Torvalds committed
901
		if (readlen != sizeof(ri) || ret) {
David Woodhouse's avatar
David Woodhouse committed
902
			printk(KERN_WARNING "Node read failed in jffs2_garbage_collect_hole. Ret %d, retlen %zd. Data will be lost by writing new hole node\n", ret, readlen);
Linus Torvalds's avatar
Linus Torvalds committed
903 904
			goto fill;
		}
David Woodhouse's avatar
David Woodhouse committed
905
		if (je16_to_cpu(ri.nodetype) != JFFS2_NODETYPE_INODE) {
Linus Torvalds's avatar
Linus Torvalds committed
906
			printk(KERN_WARNING "jffs2_garbage_collect_hole: Node at 0x%08x had node type 0x%04x instead of JFFS2_NODETYPE_INODE(0x%04x)\n",
David Woodhouse's avatar
David Woodhouse committed
907 908
			       ref_offset(fn->raw),
			       je16_to_cpu(ri.nodetype), JFFS2_NODETYPE_INODE);
Linus Torvalds's avatar
Linus Torvalds committed
909 910
			return -EIO;
		}
David Woodhouse's avatar
David Woodhouse committed
911
		if (je32_to_cpu(ri.totlen) != sizeof(ri)) {
David Woodhouse's avatar
David Woodhouse committed
912
			printk(KERN_WARNING "jffs2_garbage_collect_hole: Node at 0x%08x had totlen 0x%x instead of expected 0x%zx\n",
David Woodhouse's avatar
David Woodhouse committed
913 914
			       ref_offset(fn->raw),
			       je32_to_cpu(ri.totlen), sizeof(ri));
Linus Torvalds's avatar
Linus Torvalds committed
915 916 917
			return -EIO;
		}
		crc = crc32(0, &ri, sizeof(ri)-8);
David Woodhouse's avatar
David Woodhouse committed
918
		if (crc != je32_to_cpu(ri.node_crc)) {
Linus Torvalds's avatar
Linus Torvalds committed
919
			printk(KERN_WARNING "jffs2_garbage_collect_hole: Node at 0x%08x had CRC 0x%08x which doesn't match calculated CRC 0x%08x\n",
David Woodhouse's avatar
David Woodhouse committed
920 921
			       ref_offset(fn->raw), 
			       je32_to_cpu(ri.node_crc), crc);
Linus Torvalds's avatar
Linus Torvalds committed
922
			/* FIXME: We could possibly deal with this by writing new holes for each frag */
923 924
			printk(KERN_WARNING "Data in the range 0x%08x to 0x%08x of inode #%u will be lost\n", 
			       start, end, f->inocache->ino);
Linus Torvalds's avatar
Linus Torvalds committed
925 926 927
			goto fill;
		}
		if (ri.compr != JFFS2_COMPR_ZERO) {
David Woodhouse's avatar
David Woodhouse committed
928
			printk(KERN_WARNING "jffs2_garbage_collect_hole: Node 0x%08x wasn't a hole node!\n", ref_offset(fn->raw));
929 930
			printk(KERN_WARNING "Data in the range 0x%08x to 0x%08x of inode #%u will be lost\n", 
			       start, end, f->inocache->ino);
Linus Torvalds's avatar
Linus Torvalds committed
931 932 933 934
			goto fill;
		}
	} else {
	fill:
David Woodhouse's avatar
David Woodhouse committed
935 936 937 938 939 940 941 942 943 944
		ri.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
		ri.nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
		ri.totlen = cpu_to_je32(sizeof(ri));
		ri.hdr_crc = cpu_to_je32(crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4));

		ri.ino = cpu_to_je32(f->inocache->ino);
		ri.version = cpu_to_je32(++f->highest_version);
		ri.offset = cpu_to_je32(start);
		ri.dsize = cpu_to_je32(end - start);
		ri.csize = cpu_to_je32(0);
Linus Torvalds's avatar
Linus Torvalds committed
945 946
		ri.compr = JFFS2_COMPR_ZERO;
	}
David Woodhouse's avatar
David Woodhouse committed
947
	ri.mode = cpu_to_jemode(JFFS2_F_I_MODE(f));
David Woodhouse's avatar
David Woodhouse committed
948 949 950 951 952 953 954 955
	ri.uid = cpu_to_je16(JFFS2_F_I_UID(f));
	ri.gid = cpu_to_je16(JFFS2_F_I_GID(f));
	ri.isize = cpu_to_je32(JFFS2_F_I_SIZE(f));
	ri.atime = cpu_to_je32(JFFS2_F_I_ATIME(f));
	ri.ctime = cpu_to_je32(JFFS2_F_I_CTIME(f));
	ri.mtime = cpu_to_je32(JFFS2_F_I_MTIME(f));
	ri.data_crc = cpu_to_je32(0);
	ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
Linus Torvalds's avatar
Linus Torvalds committed
956 957 958

	ret = jffs2_reserve_space_gc(c, sizeof(ri), &phys_ofs, &alloclen);
	if (ret) {
David Woodhouse's avatar
David Woodhouse committed
959
		printk(KERN_WARNING "jffs2_reserve_space_gc of %zd bytes for garbage_collect_hole failed: %d\n",
Linus Torvalds's avatar
Linus Torvalds committed
960 961 962
		       sizeof(ri), ret);
		return ret;
	}
963
	new_fn = jffs2_write_dnode(c, f, &ri, NULL, 0, phys_ofs, ALLOC_GC);
Linus Torvalds's avatar
Linus Torvalds committed
964 965 966 967 968

	if (IS_ERR(new_fn)) {
		printk(KERN_WARNING "Error writing new hole node: %ld\n", PTR_ERR(new_fn));
		return PTR_ERR(new_fn);
	}
David Woodhouse's avatar
David Woodhouse committed
969
	if (je32_to_cpu(ri.version) == f->highest_version) {
Linus Torvalds's avatar
Linus Torvalds committed
970 971 972 973 974 975
		jffs2_add_full_dnode_to_inode(c, f, new_fn);
		if (f->metadata) {
			jffs2_mark_node_obsolete(c, f->metadata->raw);
			jffs2_free_full_dnode(f->metadata);
			f->metadata = NULL;
		}
David Woodhouse's avatar
David Woodhouse committed
976
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
977
	}
David Woodhouse's avatar
David Woodhouse committed
978 979 980 981 982 983 984 985 986

	/* 
	 * We should only get here in the case where the node we are
	 * replacing had more than one frag, so we kept the same version
	 * number as before. (Except in case of error -- see 'goto fill;' 
	 * above.)
	 */
	D1(if(unlikely(fn->frags <= 1)) {
		printk(KERN_WARNING "jffs2_garbage_collect_hole: Replacing fn with %d frag(s) but new ver %d != highest_version %d of ino #%d\n",
David Woodhouse's avatar
David Woodhouse committed
987 988
		       fn->frags, je32_to_cpu(ri.version), f->highest_version,
		       je32_to_cpu(ri.ino));
David Woodhouse's avatar
David Woodhouse committed
989 990
	});

David Woodhouse's avatar
David Woodhouse committed
991 992 993
	/* This is a partially-overlapped hole node. Mark it REF_NORMAL not REF_PRISTINE */
	mark_ref_normal(new_fn->raw);

David Woodhouse's avatar
David Woodhouse committed
994 995
	for (frag = jffs2_lookup_node_frag(&f->fragtree, fn->ofs); 
	     frag; frag = frag_next(frag)) {
Linus Torvalds's avatar
Linus Torvalds committed
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
		if (frag->ofs > fn->size + fn->ofs)
			break;
		if (frag->node == fn) {
			frag->node = new_fn;
			new_fn->frags++;
			fn->frags--;
		}
	}
	if (fn->frags) {
		printk(KERN_WARNING "jffs2_garbage_collect_hole: Old node still has frags!\n");
		BUG();
	}
	if (!new_fn->frags) {
		printk(KERN_WARNING "jffs2_garbage_collect_hole: New node has no frags!\n");
		BUG();
	}
		
	jffs2_mark_node_obsolete(c, fn->raw);
	jffs2_free_full_dnode(fn);
	
	return 0;
}

static int jffs2_garbage_collect_dnode(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
1020 1021
				       struct jffs2_inode_info *f, struct jffs2_full_dnode *fn,
				       uint32_t start, uint32_t end)
Linus Torvalds's avatar
Linus Torvalds committed
1022 1023 1024
{
	struct jffs2_full_dnode *new_fn;
	struct jffs2_raw_inode ri;
1025
	uint32_t alloclen, phys_ofs, offset, orig_end, orig_start;	
Linus Torvalds's avatar
Linus Torvalds committed
1026 1027
	int ret = 0;
	unsigned char *comprbuf = NULL, *writebuf;
David Woodhouse's avatar
David Woodhouse committed
1028
	unsigned long pg;
Linus Torvalds's avatar
Linus Torvalds committed
1029
	unsigned char *pg_ptr;
David Woodhouse's avatar
David Woodhouse committed
1030
 
Linus Torvalds's avatar
Linus Torvalds committed
1031 1032
	memset(&ri, 0, sizeof(ri));

1033 1034
	D1(printk(KERN_DEBUG "Writing replacement dnode for ino #%u from offset 0x%x to 0x%x\n",
		  f->inocache->ino, start, end));
Linus Torvalds's avatar
Linus Torvalds committed
1035 1036

	orig_end = end;
1037
	orig_start = start;
Linus Torvalds's avatar
Linus Torvalds committed
1038

1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
	if (c->nr_free_blocks + c->nr_erasing_blocks > c->resv_blocks_gcmerge) {
		/* Attempt to do some merging. But only expand to cover logically
		   adjacent frags if the block containing them is already considered
		   to be dirty. Otherwise we end up with GC just going round in 
		   circles dirtying the nodes it already wrote out, especially 
		   on NAND where we have small eraseblocks and hence a much higher
		   chance of nodes having to be split to cross boundaries. */

		struct jffs2_node_frag *frag;
		uint32_t min, max;

		min = start & ~(PAGE_CACHE_SIZE-1);
		max = min + PAGE_CACHE_SIZE;

		frag = jffs2_lookup_node_frag(&f->fragtree, start);

		/* BUG_ON(!frag) but that'll happen anyway... */

		BUG_ON(frag->ofs != start);

		/* First grow down... */
		while((frag = frag_prev(frag)) && frag->ofs >= min) {

			/* If the previous frag doesn't even reach the beginning, there's
			   excessive fragmentation. Just merge. */
			if (frag->ofs > min) {
				D1(printk(KERN_DEBUG "Expanding down to cover partial frag (0x%x-0x%x)\n",
					  frag->ofs, frag->ofs+frag->size));
				start = frag->ofs;
				continue;
			}
			/* OK. This frag holds the first byte of the page. */
			if (!frag->node || !frag->node->raw) {
				D1(printk(KERN_DEBUG "First frag in page is hole (0x%x-0x%x). Not expanding down.\n",
					  frag->ofs, frag->ofs+frag->size));
				break;
			} else {

				/* OK, it's a frag which extends to the beginning of the page. Does it live 
				   in a block which is still considered clean? If so, don't obsolete it.
				   If not, cover it anyway. */

				struct jffs2_raw_node_ref *raw = frag->node->raw;
				struct jffs2_eraseblock *jeb;

				jeb = &c->blocks[raw->flash_offset / c->sector_size];

				if (jeb == c->gcblock) {
					D1(printk(KERN_DEBUG "Expanding down to cover frag (0x%x-0x%x) in gcblock at %08x\n",
						  frag->ofs, frag->ofs+frag->size, ref_offset(raw)));
					start = frag->ofs;
					break;
				}
				if (!ISDIRTY(jeb->dirty_size + jeb->wasted_size)) {
					D1(printk(KERN_DEBUG "Not expanding down to cover frag (0x%x-0x%x) in clean block %08x\n",
						  frag->ofs, frag->ofs+frag->size, jeb->offset));
					break;
				}

				D1(printk(KERN_DEBUG "Expanding down to cover frag (0x%x-0x%x) in dirty block %08x\n",
						  frag->ofs, frag->ofs+frag->size, jeb->offset));
				start = frag->ofs;
				break;
			}
		}

		/* ... then up */

		/* Find last frag which is actually part of the node we're to GC. */
		frag = jffs2_lookup_node_frag(&f->fragtree, end-1);

		while((frag = frag_next(frag)) && frag->ofs+frag->size <= max) {

			/* If the previous frag doesn't even reach the beginning, there's lots
			   of fragmentation. Just merge. */
			if (frag->ofs+frag->size < max) {
				D1(printk(KERN_DEBUG "Expanding up to cover partial frag (0x%x-0x%x)\n",
					  frag->ofs, frag->ofs+frag->size));
				end = frag->ofs + frag->size;
				continue;
			}

			if (!frag->node || !frag->node->raw) {
				D1(printk(KERN_DEBUG "Last frag in page is hole (0x%x-0x%x). Not expanding up.\n",
					  frag->ofs, frag->ofs+frag->size));
				break;
			} else {

				/* OK, it's a frag which extends to the beginning of the page. Does it live 
				   in a block which is still considered clean? If so, don't obsolete it.
				   If not, cover it anyway. */

				struct jffs2_raw_node_ref *raw = frag->node->raw;
				struct jffs2_eraseblock *jeb;

				jeb = &c->blocks[raw->flash_offset / c->sector_size];

				if (jeb == c->gcblock) {
					D1(printk(KERN_DEBUG "Expanding up to cover frag (0x%x-0x%x) in gcblock at %08x\n",
						  frag->ofs, frag->ofs+frag->size, ref_offset(raw)));
					end = frag->ofs + frag->size;
					break;
				}
				if (!ISDIRTY(jeb->dirty_size + jeb->wasted_size)) {
					D1(printk(KERN_DEBUG "Not expanding up to cover frag (0x%x-0x%x) in clean block %08x\n",
						  frag->ofs, frag->ofs+frag->size, jeb->offset));
					break;
				}

				D1(printk(KERN_DEBUG "Expanding up to cover frag (0x%x-0x%x) in dirty block %08x\n",
						  frag->ofs, frag->ofs+frag->size, jeb->offset));
				end = frag->ofs + frag->size;
				break;
			}
Linus Torvalds's avatar
Linus Torvalds committed
1153
		}
1154 1155 1156 1157 1158 1159
		D1(printk(KERN_DEBUG "Expanded dnode to write from (0x%x-0x%x) to (0x%x-0x%x)\n", 
			  orig_start, orig_end, start, end));

		BUG_ON(end > JFFS2_F_I_SIZE(f));
		BUG_ON(end < orig_end);
		BUG_ON(start > orig_start);
Linus Torvalds's avatar
Linus Torvalds committed
1160 1161 1162 1163 1164 1165 1166 1167 1168
	}
	
	/* First, use readpage() to read the appropriate page into the page cache */
	/* Q: What happens if we actually try to GC the _same_ page for which commit_write()
	 *    triggered garbage collection in the first place?
	 * A: I _think_ it's OK. read_cache_page shouldn't deadlock, we'll write out the
	 *    page OK. We'll actually write it out again in commit_write, which is a little
	 *    suboptimal, but at least we're correct.
	 */
David Woodhouse's avatar
David Woodhouse committed
1169 1170 1171 1172 1173
	pg_ptr = jffs2_gc_fetch_page(c, f, start, &pg);

	if (IS_ERR(pg_ptr)) {
		printk(KERN_WARNING "read_cache_page() returned error: %ld\n", PTR_ERR(pg_ptr));
		return PTR_ERR(pg_ptr);
Linus Torvalds's avatar
Linus Torvalds committed
1174 1175 1176 1177
	}

	offset = start;
	while(offset < orig_end) {
1178 1179
		uint32_t datalen;
		uint32_t cdatalen;
David Woodhouse's avatar
David Woodhouse committed
1180
		uint16_t comprtype = JFFS2_COMPR_NONE;
Linus Torvalds's avatar
Linus Torvalds committed
1181 1182 1183 1184

		ret = jffs2_reserve_space_gc(c, sizeof(ri) + JFFS2_MIN_DATA_LEN, &phys_ofs, &alloclen);

		if (ret) {
David Woodhouse's avatar
David Woodhouse committed
1185
			printk(KERN_WARNING "jffs2_reserve_space_gc of %zd bytes for garbage_collect_dnode failed: %d\n",
Linus Torvalds's avatar
Linus Torvalds committed
1186 1187 1188
			       sizeof(ri)+ JFFS2_MIN_DATA_LEN, ret);
			break;
		}
David Woodhouse's avatar
David Woodhouse committed
1189
		cdatalen = min_t(uint32_t, alloclen - sizeof(ri), end - offset);
Linus Torvalds's avatar
Linus Torvalds committed
1190 1191 1192 1193
		datalen = end - offset;

		writebuf = pg_ptr + (offset & (PAGE_CACHE_SIZE -1));

David Woodhouse's avatar
David Woodhouse committed
1194 1195
		comprtype = jffs2_compress(c, f, writebuf, &comprbuf, &datalen, &cdatalen);

David Woodhouse's avatar
David Woodhouse committed
1196 1197 1198 1199 1200 1201 1202
		ri.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
		ri.nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
		ri.totlen = cpu_to_je32(sizeof(ri) + cdatalen);
		ri.hdr_crc = cpu_to_je32(crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4));

		ri.ino = cpu_to_je32(f->inocache->ino);
		ri.version = cpu_to_je32(++f->highest_version);
David Woodhouse's avatar
David Woodhouse committed
1203
		ri.mode = cpu_to_jemode(JFFS2_F_I_MODE(f));
David Woodhouse's avatar
David Woodhouse committed
1204 1205 1206 1207 1208 1209 1210 1211 1212
		ri.uid = cpu_to_je16(JFFS2_F_I_UID(f));
		ri.gid = cpu_to_je16(JFFS2_F_I_GID(f));
		ri.isize = cpu_to_je32(JFFS2_F_I_SIZE(f));
		ri.atime = cpu_to_je32(JFFS2_F_I_ATIME(f));
		ri.ctime = cpu_to_je32(JFFS2_F_I_CTIME(f));
		ri.mtime = cpu_to_je32(JFFS2_F_I_MTIME(f));
		ri.offset = cpu_to_je32(offset);
		ri.csize = cpu_to_je32(cdatalen);
		ri.dsize = cpu_to_je32(datalen);
David Woodhouse's avatar
David Woodhouse committed
1213 1214
		ri.compr = comprtype & 0xff;
		ri.usercompr = (comprtype >> 8) & 0xff;
David Woodhouse's avatar
David Woodhouse committed
1215
		ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
David Woodhouse's avatar
David Woodhouse committed
1216
		ri.data_crc = cpu_to_je32(crc32(0, comprbuf, cdatalen));
Linus Torvalds's avatar
Linus Torvalds committed
1217
	
David Woodhouse's avatar
David Woodhouse committed
1218 1219 1220
		new_fn = jffs2_write_dnode(c, f, &ri, comprbuf, cdatalen, phys_ofs, ALLOC_GC);

		jffs2_free_comprbuf(comprbuf, writebuf);
Linus Torvalds's avatar
Linus Torvalds committed
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235

		if (IS_ERR(new_fn)) {
			printk(KERN_WARNING "Error writing new dnode: %ld\n", PTR_ERR(new_fn));
			ret = PTR_ERR(new_fn);
			break;
		}
		ret = jffs2_add_full_dnode_to_inode(c, f, new_fn);
		offset += datalen;
		if (f->metadata) {
			jffs2_mark_node_obsolete(c, f->metadata->raw);
			jffs2_free_full_dnode(f->metadata);
			f->metadata = NULL;
		}
	}

David Woodhouse's avatar
David Woodhouse committed
1236
	jffs2_gc_release_page(c, pg_ptr, &pg);
Linus Torvalds's avatar
Linus Torvalds committed
1237 1238 1239
	return ret;
}