xref: /openbmc/linux/fs/jffs2/gc.c (revision 49e91e70)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * JFFS2 -- Journalling Flash File System, Version 2.
31da177e4SLinus Torvalds  *
4c00c310eSDavid Woodhouse  * Copyright © 2001-2007 Red Hat, Inc.
56088c058SDavid Woodhouse  * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Created by David Woodhouse <dwmw2@infradead.org>
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * For licensing information, see the file 'LICENCE' in this directory.
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  */
121da177e4SLinus Torvalds 
135a528957SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
145a528957SJoe Perches 
151da177e4SLinus Torvalds #include <linux/kernel.h>
161da177e4SLinus Torvalds #include <linux/mtd/mtd.h>
171da177e4SLinus Torvalds #include <linux/slab.h>
181da177e4SLinus Torvalds #include <linux/pagemap.h>
191da177e4SLinus Torvalds #include <linux/crc32.h>
201da177e4SLinus Torvalds #include <linux/compiler.h>
211da177e4SLinus Torvalds #include <linux/stat.h>
221da177e4SLinus Torvalds #include "nodelist.h"
231da177e4SLinus Torvalds #include "compr.h"
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds static int jffs2_garbage_collect_pristine(struct jffs2_sb_info *c,
261da177e4SLinus Torvalds 					  struct jffs2_inode_cache *ic,
271da177e4SLinus Torvalds 					  struct jffs2_raw_node_ref *raw);
281da177e4SLinus Torvalds static int jffs2_garbage_collect_metadata(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
291da177e4SLinus Torvalds 					struct jffs2_inode_info *f, struct jffs2_full_dnode *fd);
301da177e4SLinus Torvalds static int jffs2_garbage_collect_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
311da177e4SLinus Torvalds 					struct jffs2_inode_info *f, struct jffs2_full_dirent *fd);
321da177e4SLinus Torvalds static int jffs2_garbage_collect_deletion_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
331da177e4SLinus Torvalds 					struct jffs2_inode_info *f, struct jffs2_full_dirent *fd);
341da177e4SLinus Torvalds static int jffs2_garbage_collect_hole(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
351da177e4SLinus Torvalds 				      struct jffs2_inode_info *f, struct jffs2_full_dnode *fn,
361da177e4SLinus Torvalds 				      uint32_t start, uint32_t end);
371da177e4SLinus Torvalds static int jffs2_garbage_collect_dnode(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
381da177e4SLinus Torvalds 				       struct jffs2_inode_info *f, struct jffs2_full_dnode *fn,
391da177e4SLinus Torvalds 				       uint32_t start, uint32_t end);
401da177e4SLinus Torvalds static int jffs2_garbage_collect_live(struct jffs2_sb_info *c,  struct jffs2_eraseblock *jeb,
411da177e4SLinus Torvalds 			       struct jffs2_raw_node_ref *raw, struct jffs2_inode_info *f);
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds /* Called with erase_completion_lock held */
441da177e4SLinus Torvalds static struct jffs2_eraseblock *jffs2_find_gc_block(struct jffs2_sb_info *c)
451da177e4SLinus Torvalds {
461da177e4SLinus Torvalds 	struct jffs2_eraseblock *ret;
471da177e4SLinus Torvalds 	struct list_head *nextlist = NULL;
481da177e4SLinus Torvalds 	int n = jiffies % 128;
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds 	/* Pick an eraseblock to garbage collect next. This is where we'll
511da177e4SLinus Torvalds 	   put the clever wear-levelling algorithms. Eventually.  */
521da177e4SLinus Torvalds 	/* We possibly want to favour the dirtier blocks more when the
531da177e4SLinus Torvalds 	   number of free blocks is low. */
54a42163d7SArtem B. Bityuckiy again:
551da177e4SLinus Torvalds 	if (!list_empty(&c->bad_used_list) && c->nr_free_blocks > c->resv_blocks_gcbad) {
569c261b33SJoe Perches 		jffs2_dbg(1, "Picking block from bad_used_list to GC next\n");
571da177e4SLinus Torvalds 		nextlist = &c->bad_used_list;
581da177e4SLinus Torvalds 	} else if (n < 50 && !list_empty(&c->erasable_list)) {
591da177e4SLinus Torvalds 		/* Note that most of them will have gone directly to be erased.
601da177e4SLinus Torvalds 		   So don't favour the erasable_list _too_ much. */
619c261b33SJoe Perches 		jffs2_dbg(1, "Picking block from erasable_list to GC next\n");
621da177e4SLinus Torvalds 		nextlist = &c->erasable_list;
631da177e4SLinus Torvalds 	} else if (n < 110 && !list_empty(&c->very_dirty_list)) {
641da177e4SLinus Torvalds 		/* Most of the time, pick one off the very_dirty list */
659c261b33SJoe Perches 		jffs2_dbg(1, "Picking block from very_dirty_list to GC next\n");
661da177e4SLinus Torvalds 		nextlist = &c->very_dirty_list;
671da177e4SLinus Torvalds 	} else if (n < 126 && !list_empty(&c->dirty_list)) {
689c261b33SJoe Perches 		jffs2_dbg(1, "Picking block from dirty_list to GC next\n");
691da177e4SLinus Torvalds 		nextlist = &c->dirty_list;
701da177e4SLinus Torvalds 	} else if (!list_empty(&c->clean_list)) {
719c261b33SJoe Perches 		jffs2_dbg(1, "Picking block from clean_list to GC next\n");
721da177e4SLinus Torvalds 		nextlist = &c->clean_list;
731da177e4SLinus Torvalds 	} else if (!list_empty(&c->dirty_list)) {
749c261b33SJoe Perches 		jffs2_dbg(1, "Picking block from dirty_list to GC next (clean_list was empty)\n");
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds 		nextlist = &c->dirty_list;
771da177e4SLinus Torvalds 	} else if (!list_empty(&c->very_dirty_list)) {
789c261b33SJoe Perches 		jffs2_dbg(1, "Picking block from very_dirty_list to GC next (clean_list and dirty_list were empty)\n");
791da177e4SLinus Torvalds 		nextlist = &c->very_dirty_list;
801da177e4SLinus Torvalds 	} else if (!list_empty(&c->erasable_list)) {
819c261b33SJoe Perches 		jffs2_dbg(1, "Picking block from erasable_list to GC next (clean_list and {very_,}dirty_list were empty)\n");
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds 		nextlist = &c->erasable_list;
84a42163d7SArtem B. Bityuckiy 	} else if (!list_empty(&c->erasable_pending_wbuf_list)) {
85a42163d7SArtem B. Bityuckiy 		/* There are blocks are wating for the wbuf sync */
869c261b33SJoe Perches 		jffs2_dbg(1, "Synching wbuf in order to reuse erasable_pending_wbuf_list blocks\n");
873cceb9f6SArtem B. Bityuckiy 		spin_unlock(&c->erase_completion_lock);
88a42163d7SArtem B. Bityuckiy 		jffs2_flush_wbuf_pad(c);
893cceb9f6SArtem B. Bityuckiy 		spin_lock(&c->erase_completion_lock);
90a42163d7SArtem B. Bityuckiy 		goto again;
911da177e4SLinus Torvalds 	} else {
921da177e4SLinus Torvalds 		/* Eep. All were empty */
935a528957SJoe Perches 		jffs2_dbg(1, "No clean, dirty _or_ erasable blocks to GC from! Where are they all?\n");
941da177e4SLinus Torvalds 		return NULL;
951da177e4SLinus Torvalds 	}
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds 	ret = list_entry(nextlist->next, struct jffs2_eraseblock, list);
981da177e4SLinus Torvalds 	list_del(&ret->list);
991da177e4SLinus Torvalds 	c->gcblock = ret;
1001da177e4SLinus Torvalds 	ret->gc_node = ret->first_node;
1011da177e4SLinus Torvalds 	if (!ret->gc_node) {
102da320f05SJoe Perches 		pr_warn("Eep. ret->gc_node for block at 0x%08x is NULL\n",
103da320f05SJoe Perches 			ret->offset);
1041da177e4SLinus Torvalds 		BUG();
1051da177e4SLinus Torvalds 	}
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds 	/* Have we accidentally picked a clean block with wasted space ? */
1081da177e4SLinus Torvalds 	if (ret->wasted_size) {
1099c261b33SJoe Perches 		jffs2_dbg(1, "Converting wasted_size %08x to dirty_size\n",
1109c261b33SJoe Perches 			  ret->wasted_size);
1111da177e4SLinus Torvalds 		ret->dirty_size += ret->wasted_size;
1121da177e4SLinus Torvalds 		c->wasted_size -= ret->wasted_size;
1131da177e4SLinus Torvalds 		c->dirty_size += ret->wasted_size;
1141da177e4SLinus Torvalds 		ret->wasted_size = 0;
1151da177e4SLinus Torvalds 	}
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds 	return ret;
1181da177e4SLinus Torvalds }
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds /* jffs2_garbage_collect_pass
1211da177e4SLinus Torvalds  * Make a single attempt to progress GC. Move one node, and possibly
1221da177e4SLinus Torvalds  * start erasing one eraseblock.
1231da177e4SLinus Torvalds  */
1241da177e4SLinus Torvalds int jffs2_garbage_collect_pass(struct jffs2_sb_info *c)
1251da177e4SLinus Torvalds {
1261da177e4SLinus Torvalds 	struct jffs2_inode_info *f;
1271da177e4SLinus Torvalds 	struct jffs2_inode_cache *ic;
1281da177e4SLinus Torvalds 	struct jffs2_eraseblock *jeb;
1291da177e4SLinus Torvalds 	struct jffs2_raw_node_ref *raw;
1302665ea84SDavid Woodhouse 	uint32_t gcblock_dirty;
1311da177e4SLinus Torvalds 	int ret = 0, inum, nlink;
132aa98d7cfSKaiGai Kohei 	int xattr = 0;
1331da177e4SLinus Torvalds 
134ced22070SDavid Woodhouse 	if (mutex_lock_interruptible(&c->alloc_sem))
1351da177e4SLinus Torvalds 		return -EINTR;
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds 	for (;;) {
1381da177e4SLinus Torvalds 		spin_lock(&c->erase_completion_lock);
1391da177e4SLinus Torvalds 		if (!c->unchecked_size)
1401da177e4SLinus Torvalds 			break;
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds 		/* We can't start doing GC yet. We haven't finished checking
1431da177e4SLinus Torvalds 		   the node CRCs etc. Do it now. */
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds 		/* checked_ino is protected by the alloc_sem */
146aa98d7cfSKaiGai Kohei 		if (c->checked_ino > c->highest_ino && xattr) {
147da320f05SJoe Perches 			pr_crit("Checked all inodes but still 0x%x bytes of unchecked space?\n",
1481da177e4SLinus Torvalds 				c->unchecked_size);
149e0c8e42fSArtem B. Bityutskiy 			jffs2_dbg_dump_block_lists_nolock(c);
1501da177e4SLinus Torvalds 			spin_unlock(&c->erase_completion_lock);
151ced22070SDavid Woodhouse 			mutex_unlock(&c->alloc_sem);
15244b998e1SDavid Woodhouse 			return -ENOSPC;
1531da177e4SLinus Torvalds 		}
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds 		spin_unlock(&c->erase_completion_lock);
1561da177e4SLinus Torvalds 
157aa98d7cfSKaiGai Kohei 		if (!xattr)
158aa98d7cfSKaiGai Kohei 			xattr = jffs2_verify_xattr(c);
159aa98d7cfSKaiGai Kohei 
1601da177e4SLinus Torvalds 		spin_lock(&c->inocache_lock);
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds 		ic = jffs2_get_ino_cache(c, c->checked_ino++);
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 		if (!ic) {
1651da177e4SLinus Torvalds 			spin_unlock(&c->inocache_lock);
1661da177e4SLinus Torvalds 			continue;
1671da177e4SLinus Torvalds 		}
1681da177e4SLinus Torvalds 
16927c72b04SDavid Woodhouse 		if (!ic->pino_nlink) {
1709c261b33SJoe Perches 			jffs2_dbg(1, "Skipping check of ino #%d with nlink/pino zero\n",
1719c261b33SJoe Perches 				  ic->ino);
1721da177e4SLinus Torvalds 			spin_unlock(&c->inocache_lock);
173355ed4e1SKaiGai Kohei 			jffs2_xattr_delete_inode(c, ic);
1741da177e4SLinus Torvalds 			continue;
1751da177e4SLinus Torvalds 		}
1761da177e4SLinus Torvalds 		switch(ic->state) {
1771da177e4SLinus Torvalds 		case INO_STATE_CHECKEDABSENT:
1781da177e4SLinus Torvalds 		case INO_STATE_PRESENT:
1799c261b33SJoe Perches 			jffs2_dbg(1, "Skipping ino #%u already checked\n",
1809c261b33SJoe Perches 				  ic->ino);
1811da177e4SLinus Torvalds 			spin_unlock(&c->inocache_lock);
1821da177e4SLinus Torvalds 			continue;
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds 		case INO_STATE_GC:
1851da177e4SLinus Torvalds 		case INO_STATE_CHECKING:
186da320f05SJoe Perches 			pr_warn("Inode #%u is in state %d during CRC check phase!\n",
187da320f05SJoe Perches 				ic->ino, ic->state);
1881da177e4SLinus Torvalds 			spin_unlock(&c->inocache_lock);
1891da177e4SLinus Torvalds 			BUG();
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds 		case INO_STATE_READING:
1921da177e4SLinus Torvalds 			/* We need to wait for it to finish, lest we move on
1931da177e4SLinus Torvalds 			   and trigger the BUG() above while we haven't yet
1941da177e4SLinus Torvalds 			   finished checking all its nodes */
1959c261b33SJoe Perches 			jffs2_dbg(1, "Waiting for ino #%u to finish reading\n",
1969c261b33SJoe Perches 				  ic->ino);
197d96fb997SDavid Woodhouse 			/* We need to come back again for the _same_ inode. We've
198d96fb997SDavid Woodhouse 			 made no progress in this case, but that should be OK */
199d96fb997SDavid Woodhouse 			c->checked_ino--;
200d96fb997SDavid Woodhouse 
201ced22070SDavid Woodhouse 			mutex_unlock(&c->alloc_sem);
2021da177e4SLinus Torvalds 			sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
2031da177e4SLinus Torvalds 			return 0;
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds 		default:
2061da177e4SLinus Torvalds 			BUG();
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 		case INO_STATE_UNCHECKED:
2091da177e4SLinus Torvalds 			;
2101da177e4SLinus Torvalds 		}
2111da177e4SLinus Torvalds 		ic->state = INO_STATE_CHECKING;
2121da177e4SLinus Torvalds 		spin_unlock(&c->inocache_lock);
2131da177e4SLinus Torvalds 
2149c261b33SJoe Perches 		jffs2_dbg(1, "%s(): triggering inode scan of ino#%u\n",
2159c261b33SJoe Perches 			  __func__, ic->ino);
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 		ret = jffs2_do_crccheck_inode(c, ic);
2181da177e4SLinus Torvalds 		if (ret)
219da320f05SJoe Perches 			pr_warn("Returned error for crccheck of ino #%u. Expect badness...\n",
220da320f05SJoe Perches 				ic->ino);
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 		jffs2_set_inocache_state(c, ic, INO_STATE_CHECKEDABSENT);
223ced22070SDavid Woodhouse 		mutex_unlock(&c->alloc_sem);
2241da177e4SLinus Torvalds 		return ret;
2251da177e4SLinus Torvalds 	}
2261da177e4SLinus Torvalds 
2270717bf84SDavid Woodhouse 	/* If there are any blocks which need erasing, erase them now */
2280717bf84SDavid Woodhouse 	if (!list_empty(&c->erase_complete_list) ||
2290717bf84SDavid Woodhouse 	    !list_empty(&c->erase_pending_list)) {
2300717bf84SDavid Woodhouse 		spin_unlock(&c->erase_completion_lock);
2310717bf84SDavid Woodhouse 		mutex_unlock(&c->alloc_sem);
2329c261b33SJoe Perches 		jffs2_dbg(1, "%s(): erasing pending blocks\n", __func__);
23381cfc9f1SJoakim Tjernlund 		if (jffs2_erase_pending_blocks(c, 1))
2340717bf84SDavid Woodhouse 			return 0;
23581cfc9f1SJoakim Tjernlund 
2369c261b33SJoe Perches 		jffs2_dbg(1, "No progress from erasing block; doing GC anyway\n");
23781cfc9f1SJoakim Tjernlund 		mutex_lock(&c->alloc_sem);
238226bb7dfSJosh Cartwright 		spin_lock(&c->erase_completion_lock);
2390717bf84SDavid Woodhouse 	}
2400717bf84SDavid Woodhouse 
2411da177e4SLinus Torvalds 	/* First, work out which block we're garbage-collecting */
2421da177e4SLinus Torvalds 	jeb = c->gcblock;
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	if (!jeb)
2451da177e4SLinus Torvalds 		jeb = jffs2_find_gc_block(c);
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds 	if (!jeb) {
248422b1202SDavid Woodhouse 		/* Couldn't find a free block. But maybe we can just erase one and make 'progress'? */
2490717bf84SDavid Woodhouse 		if (c->nr_erasing_blocks) {
250422b1202SDavid Woodhouse 			spin_unlock(&c->erase_completion_lock);
251422b1202SDavid Woodhouse 			mutex_unlock(&c->alloc_sem);
252422b1202SDavid Woodhouse 			return -EAGAIN;
253422b1202SDavid Woodhouse 		}
2545a528957SJoe Perches 		jffs2_dbg(1, "Couldn't find erase block to garbage collect!\n");
2551da177e4SLinus Torvalds 		spin_unlock(&c->erase_completion_lock);
256ced22070SDavid Woodhouse 		mutex_unlock(&c->alloc_sem);
2571da177e4SLinus Torvalds 		return -EIO;
2581da177e4SLinus Torvalds 	}
2591da177e4SLinus Torvalds 
2609c261b33SJoe Perches 	jffs2_dbg(1, "GC from block %08x, used_size %08x, dirty_size %08x, free_size %08x\n",
2619c261b33SJoe Perches 		  jeb->offset, jeb->used_size, jeb->dirty_size, jeb->free_size);
2621da177e4SLinus Torvalds 	D1(if (c->nextblock)
2631da177e4SLinus Torvalds 	   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));
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 	if (!jeb->used_size) {
266ced22070SDavid Woodhouse 		mutex_unlock(&c->alloc_sem);
2671da177e4SLinus Torvalds 		goto eraseit;
2681da177e4SLinus Torvalds 	}
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 	raw = jeb->gc_node;
2712665ea84SDavid Woodhouse 	gcblock_dirty = jeb->dirty_size;
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	while(ref_obsolete(raw)) {
2749c261b33SJoe Perches 		jffs2_dbg(1, "Node at 0x%08x is obsolete... skipping\n",
2759c261b33SJoe Perches 			  ref_offset(raw));
27699988f7bSDavid Woodhouse 		raw = ref_next(raw);
2771da177e4SLinus Torvalds 		if (unlikely(!raw)) {
278da320f05SJoe Perches 			pr_warn("eep. End of raw list while still supposedly nodes to GC\n");
279da320f05SJoe Perches 			pr_warn("erase block at 0x%08x. free_size 0x%08x, dirty_size 0x%08x, used_size 0x%08x\n",
280da320f05SJoe Perches 				jeb->offset, jeb->free_size,
281da320f05SJoe Perches 				jeb->dirty_size, jeb->used_size);
2821da177e4SLinus Torvalds 			jeb->gc_node = raw;
2831da177e4SLinus Torvalds 			spin_unlock(&c->erase_completion_lock);
284ced22070SDavid Woodhouse 			mutex_unlock(&c->alloc_sem);
2851da177e4SLinus Torvalds 			BUG();
2861da177e4SLinus Torvalds 		}
2871da177e4SLinus Torvalds 	}
2881da177e4SLinus Torvalds 	jeb->gc_node = raw;
2891da177e4SLinus Torvalds 
2909c261b33SJoe Perches 	jffs2_dbg(1, "Going to garbage collect node at 0x%08x\n",
2919c261b33SJoe Perches 		  ref_offset(raw));
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 	if (!raw->next_in_ino) {
2941da177e4SLinus Torvalds 		/* Inode-less node. Clean marker, snapshot or something like that */
2951da177e4SLinus Torvalds 		spin_unlock(&c->erase_completion_lock);
2966171586aSDavid Woodhouse 		if (ref_flags(raw) == REF_PRISTINE) {
2976171586aSDavid Woodhouse 			/* It's an unknown node with JFFS2_FEATURE_RWCOMPAT_COPY */
2986171586aSDavid Woodhouse 			jffs2_garbage_collect_pristine(c, NULL, raw);
2996171586aSDavid Woodhouse 		} else {
3006171586aSDavid Woodhouse 			/* Just mark it obsolete */
3011da177e4SLinus Torvalds 			jffs2_mark_node_obsolete(c, raw);
3026171586aSDavid Woodhouse 		}
303ced22070SDavid Woodhouse 		mutex_unlock(&c->alloc_sem);
3041da177e4SLinus Torvalds 		goto eraseit_lock;
3051da177e4SLinus Torvalds 	}
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds 	ic = jffs2_raw_ref_to_ic(raw);
3081da177e4SLinus Torvalds 
309084702e0SKaiGai Kohei #ifdef CONFIG_JFFS2_FS_XATTR
310aa98d7cfSKaiGai Kohei 	/* When 'ic' refers xattr_datum/xattr_ref, this node is GCed as xattr.
311084702e0SKaiGai Kohei 	 * We can decide whether this node is inode or xattr by ic->class.     */
312084702e0SKaiGai Kohei 	if (ic->class == RAWNODE_CLASS_XATTR_DATUM
313084702e0SKaiGai Kohei 	    || ic->class == RAWNODE_CLASS_XATTR_REF) {
314084702e0SKaiGai Kohei 		spin_unlock(&c->erase_completion_lock);
315084702e0SKaiGai Kohei 
316084702e0SKaiGai Kohei 		if (ic->class == RAWNODE_CLASS_XATTR_DATUM) {
317c9f700f8SKaiGai Kohei 			ret = jffs2_garbage_collect_xattr_datum(c, (struct jffs2_xattr_datum *)ic, raw);
318084702e0SKaiGai Kohei 		} else {
319c9f700f8SKaiGai Kohei 			ret = jffs2_garbage_collect_xattr_ref(c, (struct jffs2_xattr_ref *)ic, raw);
320084702e0SKaiGai Kohei 		}
3212665ea84SDavid Woodhouse 		goto test_gcnode;
322084702e0SKaiGai Kohei 	}
323084702e0SKaiGai Kohei #endif
324aa98d7cfSKaiGai Kohei 
3251da177e4SLinus Torvalds 	/* We need to hold the inocache. Either the erase_completion_lock or
3261da177e4SLinus Torvalds 	   the inocache_lock are sufficient; we trade down since the inocache_lock
3271da177e4SLinus Torvalds 	   causes less contention. */
3281da177e4SLinus Torvalds 	spin_lock(&c->inocache_lock);
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 	spin_unlock(&c->erase_completion_lock);
3311da177e4SLinus Torvalds 
3329c261b33SJoe Perches 	jffs2_dbg(1, "%s(): collecting from block @0x%08x. Node @0x%08x(%d), ino #%u\n",
3339c261b33SJoe Perches 		  __func__, jeb->offset, ref_offset(raw), ref_flags(raw),
3349c261b33SJoe Perches 		  ic->ino);
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	/* Three possibilities:
3371da177e4SLinus Torvalds 	   1. Inode is already in-core. We must iget it and do proper
3381da177e4SLinus Torvalds 	      updating to its fragtree, etc.
3391da177e4SLinus Torvalds 	   2. Inode is not in-core, node is REF_PRISTINE. We lock the
3401da177e4SLinus Torvalds 	      inocache to prevent a read_inode(), copy the node intact.
3411da177e4SLinus Torvalds 	   3. Inode is not in-core, node is not pristine. We must iget()
3421da177e4SLinus Torvalds 	      and take the slow path.
3431da177e4SLinus Torvalds 	*/
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 	switch(ic->state) {
3461da177e4SLinus Torvalds 	case INO_STATE_CHECKEDABSENT:
3471da177e4SLinus Torvalds 		/* It's been checked, but it's not currently in-core.
3481da177e4SLinus Torvalds 		   We can just copy any pristine nodes, but have
3491da177e4SLinus Torvalds 		   to prevent anyone else from doing read_inode() while
3501da177e4SLinus Torvalds 		   we're at it, so we set the state accordingly */
3511da177e4SLinus Torvalds 		if (ref_flags(raw) == REF_PRISTINE)
3521da177e4SLinus Torvalds 			ic->state = INO_STATE_GC;
3531da177e4SLinus Torvalds 		else {
3549c261b33SJoe Perches 			jffs2_dbg(1, "Ino #%u is absent but node not REF_PRISTINE. Reading.\n",
3559c261b33SJoe Perches 				  ic->ino);
3561da177e4SLinus Torvalds 		}
3571da177e4SLinus Torvalds 		break;
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 	case INO_STATE_PRESENT:
3601da177e4SLinus Torvalds 		/* It's in-core. GC must iget() it. */
3611da177e4SLinus Torvalds 		break;
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 	case INO_STATE_UNCHECKED:
3641da177e4SLinus Torvalds 	case INO_STATE_CHECKING:
3651da177e4SLinus Torvalds 	case INO_STATE_GC:
3661da177e4SLinus Torvalds 		/* Should never happen. We should have finished checking
3671da177e4SLinus Torvalds 		   by the time we actually start doing any GC, and since
3681da177e4SLinus Torvalds 		   we're holding the alloc_sem, no other garbage collection
3691da177e4SLinus Torvalds 		   can happen.
3701da177e4SLinus Torvalds 		*/
371da320f05SJoe Perches 		pr_crit("Inode #%u already in state %d in jffs2_garbage_collect_pass()!\n",
3721da177e4SLinus Torvalds 			ic->ino, ic->state);
373ced22070SDavid Woodhouse 		mutex_unlock(&c->alloc_sem);
3741da177e4SLinus Torvalds 		spin_unlock(&c->inocache_lock);
3751da177e4SLinus Torvalds 		BUG();
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds 	case INO_STATE_READING:
3781da177e4SLinus Torvalds 		/* Someone's currently trying to read it. We must wait for
3791da177e4SLinus Torvalds 		   them to finish and then go through the full iget() route
3801da177e4SLinus Torvalds 		   to do the GC. However, sometimes read_inode() needs to get
3811da177e4SLinus Torvalds 		   the alloc_sem() (for marking nodes invalid) so we must
3821da177e4SLinus Torvalds 		   drop the alloc_sem before sleeping. */
3831da177e4SLinus Torvalds 
384ced22070SDavid Woodhouse 		mutex_unlock(&c->alloc_sem);
3859c261b33SJoe Perches 		jffs2_dbg(1, "%s(): waiting for ino #%u in state %d\n",
3869c261b33SJoe Perches 			  __func__, ic->ino, ic->state);
3871da177e4SLinus Torvalds 		sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
3881da177e4SLinus Torvalds 		/* And because we dropped the alloc_sem we must start again from the
3891da177e4SLinus Torvalds 		   beginning. Ponder chance of livelock here -- we're returning success
3901da177e4SLinus Torvalds 		   without actually making any progress.
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds 		   Q: What are the chances that the inode is back in INO_STATE_READING
3931da177e4SLinus Torvalds 		   again by the time we next enter this function? And that this happens
3941da177e4SLinus Torvalds 		   enough times to cause a real delay?
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 		   A: Small enough that I don't care :)
3971da177e4SLinus Torvalds 		*/
3981da177e4SLinus Torvalds 		return 0;
3991da177e4SLinus Torvalds 	}
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 	/* OK. Now if the inode is in state INO_STATE_GC, we are going to copy the
4021da177e4SLinus Torvalds 	   node intact, and we don't have to muck about with the fragtree etc.
4031da177e4SLinus Torvalds 	   because we know it's not in-core. If it _was_ in-core, we go through
4041da177e4SLinus Torvalds 	   all the iget() crap anyway */
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds 	if (ic->state == INO_STATE_GC) {
4071da177e4SLinus Torvalds 		spin_unlock(&c->inocache_lock);
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds 		ret = jffs2_garbage_collect_pristine(c, ic, raw);
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds 		spin_lock(&c->inocache_lock);
4121da177e4SLinus Torvalds 		ic->state = INO_STATE_CHECKEDABSENT;
4131da177e4SLinus Torvalds 		wake_up(&c->inocache_wq);
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds 		if (ret != -EBADFD) {
4161da177e4SLinus Torvalds 			spin_unlock(&c->inocache_lock);
4172665ea84SDavid Woodhouse 			goto test_gcnode;
4181da177e4SLinus Torvalds 		}
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds 		/* Fall through if it wanted us to, with inocache_lock held */
4211da177e4SLinus Torvalds 	}
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds 	/* Prevent the fairly unlikely race where the gcblock is
4241da177e4SLinus Torvalds 	   entirely obsoleted by the final close of a file which had
4251da177e4SLinus Torvalds 	   the only valid nodes in the block, followed by erasure,
4261da177e4SLinus Torvalds 	   followed by freeing of the ic because the erased block(s)
4271da177e4SLinus Torvalds 	   held _all_ the nodes of that inode.... never been seen but
4281da177e4SLinus Torvalds 	   it's vaguely possible. */
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 	inum = ic->ino;
43127c72b04SDavid Woodhouse 	nlink = ic->pino_nlink;
4321da177e4SLinus Torvalds 	spin_unlock(&c->inocache_lock);
4331da177e4SLinus Torvalds 
4341b690b48SDavid Woodhouse 	f = jffs2_gc_fetch_inode(c, inum, !nlink);
4351da177e4SLinus Torvalds 	if (IS_ERR(f)) {
4361da177e4SLinus Torvalds 		ret = PTR_ERR(f);
4371da177e4SLinus Torvalds 		goto release_sem;
4381da177e4SLinus Torvalds 	}
4391da177e4SLinus Torvalds 	if (!f) {
4401da177e4SLinus Torvalds 		ret = 0;
4411da177e4SLinus Torvalds 		goto release_sem;
4421da177e4SLinus Torvalds 	}
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds 	ret = jffs2_garbage_collect_live(c, jeb, raw, f);
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds 	jffs2_gc_release_inode(c, f);
4471da177e4SLinus Torvalds 
4482665ea84SDavid Woodhouse  test_gcnode:
4492665ea84SDavid Woodhouse 	if (jeb->dirty_size == gcblock_dirty && !ref_obsolete(jeb->gc_node)) {
4502665ea84SDavid Woodhouse 		/* Eep. This really should never happen. GC is broken */
451da320f05SJoe Perches 		pr_err("Error garbage collecting node at %08x!\n",
452da320f05SJoe Perches 		       ref_offset(jeb->gc_node));
4532665ea84SDavid Woodhouse 		ret = -ENOSPC;
4544fc8a607SDavid Woodhouse 	}
4551da177e4SLinus Torvalds  release_sem:
456ced22070SDavid Woodhouse 	mutex_unlock(&c->alloc_sem);
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds  eraseit_lock:
4591da177e4SLinus Torvalds 	/* If we've finished this block, start it erasing */
4601da177e4SLinus Torvalds 	spin_lock(&c->erase_completion_lock);
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds  eraseit:
4631da177e4SLinus Torvalds 	if (c->gcblock && !c->gcblock->used_size) {
4649c261b33SJoe Perches 		jffs2_dbg(1, "Block at 0x%08x completely obsoleted by GC. Moving to erase_pending_list\n",
4659c261b33SJoe Perches 			  c->gcblock->offset);
4661da177e4SLinus Torvalds 		/* We're GC'ing an empty block? */
4671da177e4SLinus Torvalds 		list_add_tail(&c->gcblock->list, &c->erase_pending_list);
4681da177e4SLinus Torvalds 		c->gcblock = NULL;
4691da177e4SLinus Torvalds 		c->nr_erasing_blocks++;
470ae3b6ba0SDavid Woodhouse 		jffs2_garbage_collect_trigger(c);
4711da177e4SLinus Torvalds 	}
4721da177e4SLinus Torvalds 	spin_unlock(&c->erase_completion_lock);
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds 	return ret;
4751da177e4SLinus Torvalds }
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds static int jffs2_garbage_collect_live(struct jffs2_sb_info *c,  struct jffs2_eraseblock *jeb,
4781da177e4SLinus Torvalds 				      struct jffs2_raw_node_ref *raw, struct jffs2_inode_info *f)
4791da177e4SLinus Torvalds {
4801da177e4SLinus Torvalds 	struct jffs2_node_frag *frag;
4811da177e4SLinus Torvalds 	struct jffs2_full_dnode *fn = NULL;
4821da177e4SLinus Torvalds 	struct jffs2_full_dirent *fd;
4831da177e4SLinus Torvalds 	uint32_t start = 0, end = 0, nrfrags = 0;
4841da177e4SLinus Torvalds 	int ret = 0;
4851da177e4SLinus Torvalds 
486ced22070SDavid Woodhouse 	mutex_lock(&f->sem);
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds 	/* Now we have the lock for this inode. Check that it's still the one at the head
4891da177e4SLinus Torvalds 	   of the list. */
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds 	spin_lock(&c->erase_completion_lock);
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds 	if (c->gcblock != jeb) {
4941da177e4SLinus Torvalds 		spin_unlock(&c->erase_completion_lock);
4959c261b33SJoe Perches 		jffs2_dbg(1, "GC block is no longer gcblock. Restart\n");
4961da177e4SLinus Torvalds 		goto upnout;
4971da177e4SLinus Torvalds 	}
4981da177e4SLinus Torvalds 	if (ref_obsolete(raw)) {
4991da177e4SLinus Torvalds 		spin_unlock(&c->erase_completion_lock);
5009c261b33SJoe Perches 		jffs2_dbg(1, "node to be GC'd was obsoleted in the meantime.\n");
5011da177e4SLinus Torvalds 		/* They'll call again */
5021da177e4SLinus Torvalds 		goto upnout;
5031da177e4SLinus Torvalds 	}
5041da177e4SLinus Torvalds 	spin_unlock(&c->erase_completion_lock);
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds 	/* OK. Looks safe. And nobody can get us now because we have the semaphore. Move the block */
5071da177e4SLinus Torvalds 	if (f->metadata && f->metadata->raw == raw) {
5081da177e4SLinus Torvalds 		fn = f->metadata;
5091da177e4SLinus Torvalds 		ret = jffs2_garbage_collect_metadata(c, jeb, f, fn);
5101da177e4SLinus Torvalds 		goto upnout;
5111da177e4SLinus Torvalds 	}
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds 	/* FIXME. Read node and do lookup? */
5141da177e4SLinus Torvalds 	for (frag = frag_first(&f->fragtree); frag; frag = frag_next(frag)) {
5151da177e4SLinus Torvalds 		if (frag->node && frag->node->raw == raw) {
5161da177e4SLinus Torvalds 			fn = frag->node;
5171da177e4SLinus Torvalds 			end = frag->ofs + frag->size;
5181da177e4SLinus Torvalds 			if (!nrfrags++)
5191da177e4SLinus Torvalds 				start = frag->ofs;
5201da177e4SLinus Torvalds 			if (nrfrags == frag->node->frags)
5211da177e4SLinus Torvalds 				break; /* We've found them all */
5221da177e4SLinus Torvalds 		}
5231da177e4SLinus Torvalds 	}
5241da177e4SLinus Torvalds 	if (fn) {
5251da177e4SLinus Torvalds 		if (ref_flags(raw) == REF_PRISTINE) {
5261da177e4SLinus Torvalds 			ret = jffs2_garbage_collect_pristine(c, f->inocache, raw);
5271da177e4SLinus Torvalds 			if (!ret) {
5281da177e4SLinus Torvalds 				/* Urgh. Return it sensibly. */
5291da177e4SLinus Torvalds 				frag->node->raw = f->inocache->nodes;
5301da177e4SLinus Torvalds 			}
5311da177e4SLinus Torvalds 			if (ret != -EBADFD)
5321da177e4SLinus Torvalds 				goto upnout;
5331da177e4SLinus Torvalds 		}
5341da177e4SLinus Torvalds 		/* We found a datanode. Do the GC */
5351da177e4SLinus Torvalds 		if((start >> PAGE_CACHE_SHIFT) < ((end-1) >> PAGE_CACHE_SHIFT)) {
5361da177e4SLinus Torvalds 			/* It crosses a page boundary. Therefore, it must be a hole. */
5371da177e4SLinus Torvalds 			ret = jffs2_garbage_collect_hole(c, jeb, f, fn, start, end);
5381da177e4SLinus Torvalds 		} else {
5391da177e4SLinus Torvalds 			/* It could still be a hole. But we GC the page this way anyway */
5401da177e4SLinus Torvalds 			ret = jffs2_garbage_collect_dnode(c, jeb, f, fn, start, end);
5411da177e4SLinus Torvalds 		}
5421da177e4SLinus Torvalds 		goto upnout;
5431da177e4SLinus Torvalds 	}
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds 	/* Wasn't a dnode. Try dirent */
5461da177e4SLinus Torvalds 	for (fd = f->dents; fd; fd=fd->next) {
5471da177e4SLinus Torvalds 		if (fd->raw == raw)
5481da177e4SLinus Torvalds 			break;
5491da177e4SLinus Torvalds 	}
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds 	if (fd && fd->ino) {
5521da177e4SLinus Torvalds 		ret = jffs2_garbage_collect_dirent(c, jeb, f, fd);
5531da177e4SLinus Torvalds 	} else if (fd) {
5541da177e4SLinus Torvalds 		ret = jffs2_garbage_collect_deletion_dirent(c, jeb, f, fd);
5551da177e4SLinus Torvalds 	} else {
556da320f05SJoe Perches 		pr_warn("Raw node at 0x%08x wasn't in node lists for ino #%u\n",
5571da177e4SLinus Torvalds 			ref_offset(raw), f->inocache->ino);
5581da177e4SLinus Torvalds 		if (ref_obsolete(raw)) {
559da320f05SJoe Perches 			pr_warn("But it's obsolete so we don't mind too much\n");
5601da177e4SLinus Torvalds 		} else {
561e0c8e42fSArtem B. Bityutskiy 			jffs2_dbg_dump_node(c, ref_offset(raw));
562e0c8e42fSArtem B. Bityutskiy 			BUG();
5631da177e4SLinus Torvalds 		}
5641da177e4SLinus Torvalds 	}
5651da177e4SLinus Torvalds  upnout:
566ced22070SDavid Woodhouse 	mutex_unlock(&f->sem);
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds 	return ret;
5691da177e4SLinus Torvalds }
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds static int jffs2_garbage_collect_pristine(struct jffs2_sb_info *c,
5721da177e4SLinus Torvalds 					  struct jffs2_inode_cache *ic,
5731da177e4SLinus Torvalds 					  struct jffs2_raw_node_ref *raw)
5741da177e4SLinus Torvalds {
5751da177e4SLinus Torvalds 	union jffs2_node_union *node;
5761da177e4SLinus Torvalds 	size_t retlen;
5771da177e4SLinus Torvalds 	int ret;
5781da177e4SLinus Torvalds 	uint32_t phys_ofs, alloclen;
5791da177e4SLinus Torvalds 	uint32_t crc, rawlen;
5801da177e4SLinus Torvalds 	int retried = 0;
5811da177e4SLinus Torvalds 
5829c261b33SJoe Perches 	jffs2_dbg(1, "Going to GC REF_PRISTINE node at 0x%08x\n",
5839c261b33SJoe Perches 		  ref_offset(raw));
5841da177e4SLinus Torvalds 
5856171586aSDavid Woodhouse 	alloclen = rawlen = ref_totlen(c, c->gcblock, raw);
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds 	/* Ask for a small amount of space (or the totlen if smaller) because we
5881da177e4SLinus Torvalds 	   don't want to force wastage of the end of a block if splitting would
5891da177e4SLinus Torvalds 	   work. */
5906171586aSDavid Woodhouse 	if (ic && alloclen > sizeof(struct jffs2_raw_inode) + JFFS2_MIN_DATA_LEN)
5916171586aSDavid Woodhouse 		alloclen = sizeof(struct jffs2_raw_inode) + JFFS2_MIN_DATA_LEN;
5926171586aSDavid Woodhouse 
5939fe4854cSDavid Woodhouse 	ret = jffs2_reserve_space_gc(c, alloclen, &alloclen, rawlen);
5946171586aSDavid Woodhouse 	/* 'rawlen' is not the exact summary size; it is only an upper estimation */
595e631ddbaSFerenc Havasi 
5961da177e4SLinus Torvalds 	if (ret)
5971da177e4SLinus Torvalds 		return ret;
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds 	if (alloclen < rawlen) {
6001da177e4SLinus Torvalds 		/* Doesn't fit untouched. We'll go the old route and split it */
6011da177e4SLinus Torvalds 		return -EBADFD;
6021da177e4SLinus Torvalds 	}
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds 	node = kmalloc(rawlen, GFP_KERNEL);
6051da177e4SLinus Torvalds 	if (!node)
6061da177e4SLinus Torvalds 		return -ENOMEM;
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds 	ret = jffs2_flash_read(c, ref_offset(raw), rawlen, &retlen, (char *)node);
6091da177e4SLinus Torvalds 	if (!ret && retlen != rawlen)
6101da177e4SLinus Torvalds 		ret = -EIO;
6111da177e4SLinus Torvalds 	if (ret)
6121da177e4SLinus Torvalds 		goto out_node;
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds 	crc = crc32(0, node, sizeof(struct jffs2_unknown_node)-4);
6151da177e4SLinus Torvalds 	if (je32_to_cpu(node->u.hdr_crc) != crc) {
616da320f05SJoe Perches 		pr_warn("Header CRC failed on REF_PRISTINE node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
6171da177e4SLinus Torvalds 			ref_offset(raw), je32_to_cpu(node->u.hdr_crc), crc);
6181da177e4SLinus Torvalds 		goto bail;
6191da177e4SLinus Torvalds 	}
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds 	switch(je16_to_cpu(node->u.nodetype)) {
6221da177e4SLinus Torvalds 	case JFFS2_NODETYPE_INODE:
6231da177e4SLinus Torvalds 		crc = crc32(0, node, sizeof(node->i)-8);
6241da177e4SLinus Torvalds 		if (je32_to_cpu(node->i.node_crc) != crc) {
625da320f05SJoe Perches 			pr_warn("Node CRC failed on REF_PRISTINE data node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
626da320f05SJoe Perches 				ref_offset(raw), je32_to_cpu(node->i.node_crc),
627da320f05SJoe Perches 				crc);
6281da177e4SLinus Torvalds 			goto bail;
6291da177e4SLinus Torvalds 		}
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds 		if (je32_to_cpu(node->i.dsize)) {
6321da177e4SLinus Torvalds 			crc = crc32(0, node->i.data, je32_to_cpu(node->i.csize));
6331da177e4SLinus Torvalds 			if (je32_to_cpu(node->i.data_crc) != crc) {
634da320f05SJoe Perches 				pr_warn("Data CRC failed on REF_PRISTINE data node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
635da320f05SJoe Perches 					ref_offset(raw),
636da320f05SJoe Perches 					je32_to_cpu(node->i.data_crc), crc);
6371da177e4SLinus Torvalds 				goto bail;
6381da177e4SLinus Torvalds 			}
6391da177e4SLinus Torvalds 		}
6401da177e4SLinus Torvalds 		break;
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 	case JFFS2_NODETYPE_DIRENT:
6431da177e4SLinus Torvalds 		crc = crc32(0, node, sizeof(node->d)-8);
6441da177e4SLinus Torvalds 		if (je32_to_cpu(node->d.node_crc) != crc) {
645da320f05SJoe Perches 			pr_warn("Node CRC failed on REF_PRISTINE dirent node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
646da320f05SJoe Perches 				ref_offset(raw),
647da320f05SJoe Perches 				je32_to_cpu(node->d.node_crc), crc);
6481da177e4SLinus Torvalds 			goto bail;
6491da177e4SLinus Torvalds 		}
6501da177e4SLinus Torvalds 
651b534e70cSDavid Woodhouse 		if (strnlen(node->d.name, node->d.nsize) != node->d.nsize) {
652da320f05SJoe Perches 			pr_warn("Name in dirent node at 0x%08x contains zeroes\n",
653da320f05SJoe Perches 				ref_offset(raw));
654b534e70cSDavid Woodhouse 			goto bail;
655b534e70cSDavid Woodhouse 		}
656b534e70cSDavid Woodhouse 
6571da177e4SLinus Torvalds 		if (node->d.nsize) {
6581da177e4SLinus Torvalds 			crc = crc32(0, node->d.name, node->d.nsize);
6591da177e4SLinus Torvalds 			if (je32_to_cpu(node->d.name_crc) != crc) {
660da320f05SJoe Perches 				pr_warn("Name CRC failed on REF_PRISTINE dirent node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
661da320f05SJoe Perches 					ref_offset(raw),
662da320f05SJoe Perches 					je32_to_cpu(node->d.name_crc), crc);
6631da177e4SLinus Torvalds 				goto bail;
6641da177e4SLinus Torvalds 			}
6651da177e4SLinus Torvalds 		}
6661da177e4SLinus Torvalds 		break;
6671da177e4SLinus Torvalds 	default:
6686171586aSDavid Woodhouse 		/* If it's inode-less, we don't _know_ what it is. Just copy it intact */
6696171586aSDavid Woodhouse 		if (ic) {
670da320f05SJoe Perches 			pr_warn("Unknown node type for REF_PRISTINE node at 0x%08x: 0x%04x\n",
6711da177e4SLinus Torvalds 				ref_offset(raw), je16_to_cpu(node->u.nodetype));
6721da177e4SLinus Torvalds 			goto bail;
6731da177e4SLinus Torvalds 		}
6746171586aSDavid Woodhouse 	}
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds 	/* OK, all the CRCs are good; this node can just be copied as-is. */
6771da177e4SLinus Torvalds  retry:
6782f785402SDavid Woodhouse 	phys_ofs = write_ofs(c);
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds 	ret = jffs2_flash_write(c, phys_ofs, rawlen, &retlen, (char *)node);
6811da177e4SLinus Torvalds 
6821da177e4SLinus Torvalds 	if (ret || (retlen != rawlen)) {
683da320f05SJoe Perches 		pr_notice("Write of %d bytes at 0x%08x failed. returned %d, retlen %zd\n",
6842f785402SDavid Woodhouse 			  rawlen, phys_ofs, ret, retlen);
6851da177e4SLinus Torvalds 		if (retlen) {
6862f785402SDavid Woodhouse 			jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, rawlen, NULL);
6871da177e4SLinus Torvalds 		} else {
688da320f05SJoe Perches 			pr_notice("Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n",
689da320f05SJoe Perches 				  phys_ofs);
6901da177e4SLinus Torvalds 		}
6912f785402SDavid Woodhouse 		if (!retried) {
6921da177e4SLinus Torvalds 			/* Try to reallocate space and retry */
6931da177e4SLinus Torvalds 			uint32_t dummy;
6941da177e4SLinus Torvalds 			struct jffs2_eraseblock *jeb = &c->blocks[phys_ofs / c->sector_size];
6951da177e4SLinus Torvalds 
6961da177e4SLinus Torvalds 			retried = 1;
6971da177e4SLinus Torvalds 
6989c261b33SJoe Perches 			jffs2_dbg(1, "Retrying failed write of REF_PRISTINE node.\n");
6991da177e4SLinus Torvalds 
700730554d9SArtem B. Bityutskiy 			jffs2_dbg_acct_sanity_check(c,jeb);
701730554d9SArtem B. Bityutskiy 			jffs2_dbg_acct_paranoia_check(c, jeb);
7021da177e4SLinus Torvalds 
7039fe4854cSDavid Woodhouse 			ret = jffs2_reserve_space_gc(c, rawlen, &dummy, rawlen);
704e631ddbaSFerenc Havasi 						/* this is not the exact summary size of it,
705e631ddbaSFerenc Havasi 							it is only an upper estimation */
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 			if (!ret) {
7089c261b33SJoe Perches 				jffs2_dbg(1, "Allocated space at 0x%08x to retry failed write.\n",
7099c261b33SJoe Perches 					  phys_ofs);
7101da177e4SLinus Torvalds 
711730554d9SArtem B. Bityutskiy 				jffs2_dbg_acct_sanity_check(c,jeb);
712730554d9SArtem B. Bityutskiy 				jffs2_dbg_acct_paranoia_check(c, jeb);
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds 				goto retry;
7151da177e4SLinus Torvalds 			}
7169c261b33SJoe Perches 			jffs2_dbg(1, "Failed to allocate space to retry failed write: %d!\n",
7179c261b33SJoe Perches 				  ret);
7181da177e4SLinus Torvalds 		}
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds 		if (!ret)
7211da177e4SLinus Torvalds 			ret = -EIO;
7221da177e4SLinus Torvalds 		goto out_node;
7231da177e4SLinus Torvalds 	}
7242f785402SDavid Woodhouse 	jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, rawlen, ic);
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 	jffs2_mark_node_obsolete(c, raw);
7279c261b33SJoe Perches 	jffs2_dbg(1, "WHEEE! GC REF_PRISTINE node at 0x%08x succeeded\n",
7289c261b33SJoe Perches 		  ref_offset(raw));
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds  out_node:
7311da177e4SLinus Torvalds 	kfree(node);
7321da177e4SLinus Torvalds 	return ret;
7331da177e4SLinus Torvalds  bail:
7341da177e4SLinus Torvalds 	ret = -EBADFD;
7351da177e4SLinus Torvalds 	goto out_node;
7361da177e4SLinus Torvalds }
7371da177e4SLinus Torvalds 
7381da177e4SLinus Torvalds static int jffs2_garbage_collect_metadata(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
7391da177e4SLinus Torvalds 					struct jffs2_inode_info *f, struct jffs2_full_dnode *fn)
7401da177e4SLinus Torvalds {
7411da177e4SLinus Torvalds 	struct jffs2_full_dnode *new_fn;
7421da177e4SLinus Torvalds 	struct jffs2_raw_inode ri;
7438557fd51SArtem B. Bityuckiy 	struct jffs2_node_frag *last_frag;
744aef9ab47SDavid Woodhouse 	union jffs2_device_node dev;
7452e16cfcaSDavid Woodhouse 	char *mdata = NULL;
7462e16cfcaSDavid Woodhouse 	int mdatalen = 0;
7479fe4854cSDavid Woodhouse 	uint32_t alloclen, ilen;
7481da177e4SLinus Torvalds 	int ret;
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds 	if (S_ISBLK(JFFS2_F_I_MODE(f)) ||
7511da177e4SLinus Torvalds 	    S_ISCHR(JFFS2_F_I_MODE(f)) ) {
7521da177e4SLinus Torvalds 		/* For these, we don't actually need to read the old node */
753aef9ab47SDavid Woodhouse 		mdatalen = jffs2_encode_dev(&dev, JFFS2_F_I_RDEV(f));
7541da177e4SLinus Torvalds 		mdata = (char *)&dev;
7559c261b33SJoe Perches 		jffs2_dbg(1, "%s(): Writing %d bytes of kdev_t\n",
7569c261b33SJoe Perches 			  __func__, mdatalen);
7571da177e4SLinus Torvalds 	} else if (S_ISLNK(JFFS2_F_I_MODE(f))) {
7581da177e4SLinus Torvalds 		mdatalen = fn->size;
7591da177e4SLinus Torvalds 		mdata = kmalloc(fn->size, GFP_KERNEL);
7601da177e4SLinus Torvalds 		if (!mdata) {
761da320f05SJoe Perches 			pr_warn("kmalloc of mdata failed in jffs2_garbage_collect_metadata()\n");
7621da177e4SLinus Torvalds 			return -ENOMEM;
7631da177e4SLinus Torvalds 		}
7641da177e4SLinus Torvalds 		ret = jffs2_read_dnode(c, f, fn, mdata, 0, mdatalen);
7651da177e4SLinus Torvalds 		if (ret) {
766da320f05SJoe Perches 			pr_warn("read of old metadata failed in jffs2_garbage_collect_metadata(): %d\n",
767da320f05SJoe Perches 				ret);
7681da177e4SLinus Torvalds 			kfree(mdata);
7691da177e4SLinus Torvalds 			return ret;
7701da177e4SLinus Torvalds 		}
7719c261b33SJoe Perches 		jffs2_dbg(1, "%s(): Writing %d bites of symlink target\n",
7729c261b33SJoe Perches 			  __func__, mdatalen);
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds 	}
7751da177e4SLinus Torvalds 
7769fe4854cSDavid Woodhouse 	ret = jffs2_reserve_space_gc(c, sizeof(ri) + mdatalen, &alloclen,
777e631ddbaSFerenc Havasi 				JFFS2_SUMMARY_INODE_SIZE);
7781da177e4SLinus Torvalds 	if (ret) {
779da320f05SJoe Perches 		pr_warn("jffs2_reserve_space_gc of %zd bytes for garbage_collect_metadata failed: %d\n",
7801da177e4SLinus Torvalds 			sizeof(ri) + mdatalen, ret);
7811da177e4SLinus Torvalds 		goto out;
7821da177e4SLinus Torvalds 	}
7831da177e4SLinus Torvalds 
7848557fd51SArtem B. Bityuckiy 	last_frag = frag_last(&f->fragtree);
7858557fd51SArtem B. Bityuckiy 	if (last_frag)
7868557fd51SArtem B. Bityuckiy 		/* Fetch the inode length from the fragtree rather then
7878557fd51SArtem B. Bityuckiy 		 * from i_size since i_size may have not been updated yet */
7888557fd51SArtem B. Bityuckiy 		ilen = last_frag->ofs + last_frag->size;
7898557fd51SArtem B. Bityuckiy 	else
7908557fd51SArtem B. Bityuckiy 		ilen = JFFS2_F_I_SIZE(f);
7918557fd51SArtem B. Bityuckiy 
7921da177e4SLinus Torvalds 	memset(&ri, 0, sizeof(ri));
7931da177e4SLinus Torvalds 	ri.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
7941da177e4SLinus Torvalds 	ri.nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
7951da177e4SLinus Torvalds 	ri.totlen = cpu_to_je32(sizeof(ri) + mdatalen);
7961da177e4SLinus Torvalds 	ri.hdr_crc = cpu_to_je32(crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4));
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds 	ri.ino = cpu_to_je32(f->inocache->ino);
7991da177e4SLinus Torvalds 	ri.version = cpu_to_je32(++f->highest_version);
8001da177e4SLinus Torvalds 	ri.mode = cpu_to_jemode(JFFS2_F_I_MODE(f));
8011da177e4SLinus Torvalds 	ri.uid = cpu_to_je16(JFFS2_F_I_UID(f));
8021da177e4SLinus Torvalds 	ri.gid = cpu_to_je16(JFFS2_F_I_GID(f));
8038557fd51SArtem B. Bityuckiy 	ri.isize = cpu_to_je32(ilen);
8041da177e4SLinus Torvalds 	ri.atime = cpu_to_je32(JFFS2_F_I_ATIME(f));
8051da177e4SLinus Torvalds 	ri.ctime = cpu_to_je32(JFFS2_F_I_CTIME(f));
8061da177e4SLinus Torvalds 	ri.mtime = cpu_to_je32(JFFS2_F_I_MTIME(f));
8071da177e4SLinus Torvalds 	ri.offset = cpu_to_je32(0);
8081da177e4SLinus Torvalds 	ri.csize = cpu_to_je32(mdatalen);
8091da177e4SLinus Torvalds 	ri.dsize = cpu_to_je32(mdatalen);
8101da177e4SLinus Torvalds 	ri.compr = JFFS2_COMPR_NONE;
8111da177e4SLinus Torvalds 	ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
8121da177e4SLinus Torvalds 	ri.data_crc = cpu_to_je32(crc32(0, mdata, mdatalen));
8131da177e4SLinus Torvalds 
8149fe4854cSDavid Woodhouse 	new_fn = jffs2_write_dnode(c, f, &ri, mdata, mdatalen, ALLOC_GC);
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds 	if (IS_ERR(new_fn)) {
817da320f05SJoe Perches 		pr_warn("Error writing new dnode: %ld\n", PTR_ERR(new_fn));
8181da177e4SLinus Torvalds 		ret = PTR_ERR(new_fn);
8191da177e4SLinus Torvalds 		goto out;
8201da177e4SLinus Torvalds 	}
8211da177e4SLinus Torvalds 	jffs2_mark_node_obsolete(c, fn->raw);
8221da177e4SLinus Torvalds 	jffs2_free_full_dnode(fn);
8231da177e4SLinus Torvalds 	f->metadata = new_fn;
8241da177e4SLinus Torvalds  out:
8251da177e4SLinus Torvalds 	if (S_ISLNK(JFFS2_F_I_MODE(f)))
8261da177e4SLinus Torvalds 		kfree(mdata);
8271da177e4SLinus Torvalds 	return ret;
8281da177e4SLinus Torvalds }
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds static int jffs2_garbage_collect_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
8311da177e4SLinus Torvalds 					struct jffs2_inode_info *f, struct jffs2_full_dirent *fd)
8321da177e4SLinus Torvalds {
8331da177e4SLinus Torvalds 	struct jffs2_full_dirent *new_fd;
8341da177e4SLinus Torvalds 	struct jffs2_raw_dirent rd;
8359fe4854cSDavid Woodhouse 	uint32_t alloclen;
8361da177e4SLinus Torvalds 	int ret;
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds 	rd.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
8391da177e4SLinus Torvalds 	rd.nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
8401da177e4SLinus Torvalds 	rd.nsize = strlen(fd->name);
8411da177e4SLinus Torvalds 	rd.totlen = cpu_to_je32(sizeof(rd) + rd.nsize);
8421da177e4SLinus Torvalds 	rd.hdr_crc = cpu_to_je32(crc32(0, &rd, sizeof(struct jffs2_unknown_node)-4));
8431da177e4SLinus Torvalds 
8441da177e4SLinus Torvalds 	rd.pino = cpu_to_je32(f->inocache->ino);
8451da177e4SLinus Torvalds 	rd.version = cpu_to_je32(++f->highest_version);
8461da177e4SLinus Torvalds 	rd.ino = cpu_to_je32(fd->ino);
8473a69e0cdSArtem B. Bityutskiy 	/* If the times on this inode were set by explicit utime() they can be different,
8483a69e0cdSArtem B. Bityutskiy 	   so refrain from splatting them. */
8493a69e0cdSArtem B. Bityutskiy 	if (JFFS2_F_I_MTIME(f) == JFFS2_F_I_CTIME(f))
8503a69e0cdSArtem B. Bityutskiy 		rd.mctime = cpu_to_je32(JFFS2_F_I_MTIME(f));
8513a69e0cdSArtem B. Bityutskiy 	else
8523a69e0cdSArtem B. Bityutskiy 		rd.mctime = cpu_to_je32(0);
8531da177e4SLinus Torvalds 	rd.type = fd->type;
8541da177e4SLinus Torvalds 	rd.node_crc = cpu_to_je32(crc32(0, &rd, sizeof(rd)-8));
8551da177e4SLinus Torvalds 	rd.name_crc = cpu_to_je32(crc32(0, fd->name, rd.nsize));
8561da177e4SLinus Torvalds 
8579fe4854cSDavid Woodhouse 	ret = jffs2_reserve_space_gc(c, sizeof(rd)+rd.nsize, &alloclen,
858e631ddbaSFerenc Havasi 				JFFS2_SUMMARY_DIRENT_SIZE(rd.nsize));
8591da177e4SLinus Torvalds 	if (ret) {
860da320f05SJoe Perches 		pr_warn("jffs2_reserve_space_gc of %zd bytes for garbage_collect_dirent failed: %d\n",
8611da177e4SLinus Torvalds 			sizeof(rd)+rd.nsize, ret);
8621da177e4SLinus Torvalds 		return ret;
8631da177e4SLinus Torvalds 	}
8649fe4854cSDavid Woodhouse 	new_fd = jffs2_write_dirent(c, f, &rd, fd->name, rd.nsize, ALLOC_GC);
8651da177e4SLinus Torvalds 
8661da177e4SLinus Torvalds 	if (IS_ERR(new_fd)) {
867da320f05SJoe Perches 		pr_warn("jffs2_write_dirent in garbage_collect_dirent failed: %ld\n",
868da320f05SJoe Perches 			PTR_ERR(new_fd));
8691da177e4SLinus Torvalds 		return PTR_ERR(new_fd);
8701da177e4SLinus Torvalds 	}
8711da177e4SLinus Torvalds 	jffs2_add_fd_to_list(c, new_fd, &f->dents);
8721da177e4SLinus Torvalds 	return 0;
8731da177e4SLinus Torvalds }
8741da177e4SLinus Torvalds 
8751da177e4SLinus Torvalds static int jffs2_garbage_collect_deletion_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
8761da177e4SLinus Torvalds 					struct jffs2_inode_info *f, struct jffs2_full_dirent *fd)
8771da177e4SLinus Torvalds {
8781da177e4SLinus Torvalds 	struct jffs2_full_dirent **fdp = &f->dents;
8791da177e4SLinus Torvalds 	int found = 0;
8801da177e4SLinus Torvalds 
8811da177e4SLinus Torvalds 	/* On a medium where we can't actually mark nodes obsolete
8821da177e4SLinus Torvalds 	   pernamently, such as NAND flash, we need to work out
8831da177e4SLinus Torvalds 	   whether this deletion dirent is still needed to actively
8841da177e4SLinus Torvalds 	   delete a 'real' dirent with the same name that's still
8851da177e4SLinus Torvalds 	   somewhere else on the flash. */
8861da177e4SLinus Torvalds 	if (!jffs2_can_mark_obsolete(c)) {
8871da177e4SLinus Torvalds 		struct jffs2_raw_dirent *rd;
8881da177e4SLinus Torvalds 		struct jffs2_raw_node_ref *raw;
8891da177e4SLinus Torvalds 		int ret;
8901da177e4SLinus Torvalds 		size_t retlen;
8911da177e4SLinus Torvalds 		int name_len = strlen(fd->name);
8921da177e4SLinus Torvalds 		uint32_t name_crc = crc32(0, fd->name, name_len);
8931da177e4SLinus Torvalds 		uint32_t rawlen = ref_totlen(c, jeb, fd->raw);
8941da177e4SLinus Torvalds 
8951da177e4SLinus Torvalds 		rd = kmalloc(rawlen, GFP_KERNEL);
8961da177e4SLinus Torvalds 		if (!rd)
8971da177e4SLinus Torvalds 			return -ENOMEM;
8981da177e4SLinus Torvalds 
8991da177e4SLinus Torvalds 		/* Prevent the erase code from nicking the obsolete node refs while
9001da177e4SLinus Torvalds 		   we're looking at them. I really don't like this extra lock but
9011da177e4SLinus Torvalds 		   can't see any alternative. Suggestions on a postcard to... */
902ced22070SDavid Woodhouse 		mutex_lock(&c->erase_free_sem);
9031da177e4SLinus Torvalds 
9041da177e4SLinus Torvalds 		for (raw = f->inocache->nodes; raw != (void *)f->inocache; raw = raw->next_in_ino) {
9051da177e4SLinus Torvalds 
906aba54da3SArtem Bityutskiy 			cond_resched();
907aba54da3SArtem Bityutskiy 
9081da177e4SLinus Torvalds 			/* We only care about obsolete ones */
9091da177e4SLinus Torvalds 			if (!(ref_obsolete(raw)))
9101da177e4SLinus Torvalds 				continue;
9111da177e4SLinus Torvalds 
9121da177e4SLinus Torvalds 			/* Any dirent with the same name is going to have the same length... */
9131da177e4SLinus Torvalds 			if (ref_totlen(c, NULL, raw) != rawlen)
9141da177e4SLinus Torvalds 				continue;
9151da177e4SLinus Torvalds 
9161da177e4SLinus Torvalds 			/* Doesn't matter if there's one in the same erase block. We're going to
9171da177e4SLinus Torvalds 			   delete it too at the same time. */
9183be36675SAndrew Victor 			if (SECTOR_ADDR(raw->flash_offset) == SECTOR_ADDR(fd->raw->flash_offset))
9191da177e4SLinus Torvalds 				continue;
9201da177e4SLinus Torvalds 
9219c261b33SJoe Perches 			jffs2_dbg(1, "Check potential deletion dirent at %08x\n",
9229c261b33SJoe Perches 				  ref_offset(raw));
9231da177e4SLinus Torvalds 
9241da177e4SLinus Torvalds 			/* This is an obsolete node belonging to the same directory, and it's of the right
9251da177e4SLinus Torvalds 			   length. We need to take a closer look...*/
9261da177e4SLinus Torvalds 			ret = jffs2_flash_read(c, ref_offset(raw), rawlen, &retlen, (char *)rd);
9271da177e4SLinus Torvalds 			if (ret) {
928da320f05SJoe Perches 				pr_warn("%s(): Read error (%d) reading obsolete node at %08x\n",
929da320f05SJoe Perches 					__func__, ret, ref_offset(raw));
9301da177e4SLinus Torvalds 				/* If we can't read it, we don't need to continue to obsolete it. Continue */
9311da177e4SLinus Torvalds 				continue;
9321da177e4SLinus Torvalds 			}
9331da177e4SLinus Torvalds 			if (retlen != rawlen) {
934da320f05SJoe Perches 				pr_warn("%s(): Short read (%zd not %u) reading header from obsolete node at %08x\n",
935da320f05SJoe Perches 					__func__, retlen, rawlen,
936da320f05SJoe Perches 					ref_offset(raw));
9371da177e4SLinus Torvalds 				continue;
9381da177e4SLinus Torvalds 			}
9391da177e4SLinus Torvalds 
9401da177e4SLinus Torvalds 			if (je16_to_cpu(rd->nodetype) != JFFS2_NODETYPE_DIRENT)
9411da177e4SLinus Torvalds 				continue;
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds 			/* If the name CRC doesn't match, skip */
9441da177e4SLinus Torvalds 			if (je32_to_cpu(rd->name_crc) != name_crc)
9451da177e4SLinus Torvalds 				continue;
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds 			/* If the name length doesn't match, or it's another deletion dirent, skip */
9481da177e4SLinus Torvalds 			if (rd->nsize != name_len || !je32_to_cpu(rd->ino))
9491da177e4SLinus Torvalds 				continue;
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds 			/* OK, check the actual name now */
9521da177e4SLinus Torvalds 			if (memcmp(rd->name, fd->name, name_len))
9531da177e4SLinus Torvalds 				continue;
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds 			/* OK. The name really does match. There really is still an older node on
9561da177e4SLinus Torvalds 			   the flash which our deletion dirent obsoletes. So we have to write out
9571da177e4SLinus Torvalds 			   a new deletion dirent to replace it */
958ced22070SDavid Woodhouse 			mutex_unlock(&c->erase_free_sem);
9591da177e4SLinus Torvalds 
9609c261b33SJoe Perches 			jffs2_dbg(1, "Deletion dirent at %08x still obsoletes real dirent \"%s\" at %08x for ino #%u\n",
9619c261b33SJoe Perches 				  ref_offset(fd->raw), fd->name,
9629c261b33SJoe Perches 				  ref_offset(raw), je32_to_cpu(rd->ino));
9631da177e4SLinus Torvalds 			kfree(rd);
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds 			return jffs2_garbage_collect_dirent(c, jeb, f, fd);
9661da177e4SLinus Torvalds 		}
9671da177e4SLinus Torvalds 
968ced22070SDavid Woodhouse 		mutex_unlock(&c->erase_free_sem);
9691da177e4SLinus Torvalds 		kfree(rd);
9701da177e4SLinus Torvalds 	}
9711da177e4SLinus Torvalds 
9723a69e0cdSArtem B. Bityutskiy 	/* FIXME: If we're deleting a dirent which contains the current mtime and ctime,
9733a69e0cdSArtem B. Bityutskiy 	   we should update the metadata node with those times accordingly */
9743a69e0cdSArtem B. Bityutskiy 
9751da177e4SLinus Torvalds 	/* No need for it any more. Just mark it obsolete and remove it from the list */
9761da177e4SLinus Torvalds 	while (*fdp) {
9771da177e4SLinus Torvalds 		if ((*fdp) == fd) {
9781da177e4SLinus Torvalds 			found = 1;
9791da177e4SLinus Torvalds 			*fdp = fd->next;
9801da177e4SLinus Torvalds 			break;
9811da177e4SLinus Torvalds 		}
9821da177e4SLinus Torvalds 		fdp = &(*fdp)->next;
9831da177e4SLinus Torvalds 	}
9841da177e4SLinus Torvalds 	if (!found) {
985da320f05SJoe Perches 		pr_warn("Deletion dirent \"%s\" not found in list for ino #%u\n",
986da320f05SJoe Perches 			fd->name, f->inocache->ino);
9871da177e4SLinus Torvalds 	}
9881da177e4SLinus Torvalds 	jffs2_mark_node_obsolete(c, fd->raw);
9891da177e4SLinus Torvalds 	jffs2_free_full_dirent(fd);
9901da177e4SLinus Torvalds 	return 0;
9911da177e4SLinus Torvalds }
9921da177e4SLinus Torvalds 
9931da177e4SLinus Torvalds static int jffs2_garbage_collect_hole(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
9941da177e4SLinus Torvalds 				      struct jffs2_inode_info *f, struct jffs2_full_dnode *fn,
9951da177e4SLinus Torvalds 				      uint32_t start, uint32_t end)
9961da177e4SLinus Torvalds {
9971da177e4SLinus Torvalds 	struct jffs2_raw_inode ri;
9981da177e4SLinus Torvalds 	struct jffs2_node_frag *frag;
9991da177e4SLinus Torvalds 	struct jffs2_full_dnode *new_fn;
10009fe4854cSDavid Woodhouse 	uint32_t alloclen, ilen;
10011da177e4SLinus Torvalds 	int ret;
10021da177e4SLinus Torvalds 
10039c261b33SJoe Perches 	jffs2_dbg(1, "Writing replacement hole node for ino #%u from offset 0x%x to 0x%x\n",
10049c261b33SJoe Perches 		  f->inocache->ino, start, end);
10051da177e4SLinus Torvalds 
10061da177e4SLinus Torvalds 	memset(&ri, 0, sizeof(ri));
10071da177e4SLinus Torvalds 
10081da177e4SLinus Torvalds 	if(fn->frags > 1) {
10091da177e4SLinus Torvalds 		size_t readlen;
10101da177e4SLinus Torvalds 		uint32_t crc;
10111da177e4SLinus Torvalds 		/* It's partially obsoleted by a later write. So we have to
10121da177e4SLinus Torvalds 		   write it out again with the _same_ version as before */
10131da177e4SLinus Torvalds 		ret = jffs2_flash_read(c, ref_offset(fn->raw), sizeof(ri), &readlen, (char *)&ri);
10141da177e4SLinus Torvalds 		if (readlen != sizeof(ri) || ret) {
1015da320f05SJoe Perches 			pr_warn("Node read failed in jffs2_garbage_collect_hole. Ret %d, retlen %zd. Data will be lost by writing new hole node\n",
1016da320f05SJoe Perches 				ret, readlen);
10171da177e4SLinus Torvalds 			goto fill;
10181da177e4SLinus Torvalds 		}
10191da177e4SLinus Torvalds 		if (je16_to_cpu(ri.nodetype) != JFFS2_NODETYPE_INODE) {
1020da320f05SJoe Perches 			pr_warn("%s(): Node at 0x%08x had node type 0x%04x instead of JFFS2_NODETYPE_INODE(0x%04x)\n",
1021da320f05SJoe Perches 				__func__, ref_offset(fn->raw),
10221da177e4SLinus Torvalds 				je16_to_cpu(ri.nodetype), JFFS2_NODETYPE_INODE);
10231da177e4SLinus Torvalds 			return -EIO;
10241da177e4SLinus Torvalds 		}
10251da177e4SLinus Torvalds 		if (je32_to_cpu(ri.totlen) != sizeof(ri)) {
1026da320f05SJoe Perches 			pr_warn("%s(): Node at 0x%08x had totlen 0x%x instead of expected 0x%zx\n",
1027da320f05SJoe Perches 				__func__, ref_offset(fn->raw),
10281da177e4SLinus Torvalds 				je32_to_cpu(ri.totlen), sizeof(ri));
10291da177e4SLinus Torvalds 			return -EIO;
10301da177e4SLinus Torvalds 		}
10311da177e4SLinus Torvalds 		crc = crc32(0, &ri, sizeof(ri)-8);
10321da177e4SLinus Torvalds 		if (crc != je32_to_cpu(ri.node_crc)) {
1033da320f05SJoe Perches 			pr_warn("%s: Node at 0x%08x had CRC 0x%08x which doesn't match calculated CRC 0x%08x\n",
1034da320f05SJoe Perches 				__func__, ref_offset(fn->raw),
10351da177e4SLinus Torvalds 				je32_to_cpu(ri.node_crc), crc);
10361da177e4SLinus Torvalds 			/* FIXME: We could possibly deal with this by writing new holes for each frag */
1037da320f05SJoe Perches 			pr_warn("Data in the range 0x%08x to 0x%08x of inode #%u will be lost\n",
10381da177e4SLinus Torvalds 				start, end, f->inocache->ino);
10391da177e4SLinus Torvalds 			goto fill;
10401da177e4SLinus Torvalds 		}
10411da177e4SLinus Torvalds 		if (ri.compr != JFFS2_COMPR_ZERO) {
1042da320f05SJoe Perches 			pr_warn("%s(): Node 0x%08x wasn't a hole node!\n",
1043da320f05SJoe Perches 				__func__, ref_offset(fn->raw));
1044da320f05SJoe Perches 			pr_warn("Data in the range 0x%08x to 0x%08x of inode #%u will be lost\n",
10451da177e4SLinus Torvalds 				start, end, f->inocache->ino);
10461da177e4SLinus Torvalds 			goto fill;
10471da177e4SLinus Torvalds 		}
10481da177e4SLinus Torvalds 	} else {
10491da177e4SLinus Torvalds 	fill:
10501da177e4SLinus Torvalds 		ri.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
10511da177e4SLinus Torvalds 		ri.nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
10521da177e4SLinus Torvalds 		ri.totlen = cpu_to_je32(sizeof(ri));
10531da177e4SLinus Torvalds 		ri.hdr_crc = cpu_to_je32(crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4));
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds 		ri.ino = cpu_to_je32(f->inocache->ino);
10561da177e4SLinus Torvalds 		ri.version = cpu_to_je32(++f->highest_version);
10571da177e4SLinus Torvalds 		ri.offset = cpu_to_je32(start);
10581da177e4SLinus Torvalds 		ri.dsize = cpu_to_je32(end - start);
10591da177e4SLinus Torvalds 		ri.csize = cpu_to_je32(0);
10601da177e4SLinus Torvalds 		ri.compr = JFFS2_COMPR_ZERO;
10611da177e4SLinus Torvalds 	}
10628557fd51SArtem B. Bityuckiy 
10638557fd51SArtem B. Bityuckiy 	frag = frag_last(&f->fragtree);
10648557fd51SArtem B. Bityuckiy 	if (frag)
10658557fd51SArtem B. Bityuckiy 		/* Fetch the inode length from the fragtree rather then
10668557fd51SArtem B. Bityuckiy 		 * from i_size since i_size may have not been updated yet */
10678557fd51SArtem B. Bityuckiy 		ilen = frag->ofs + frag->size;
10688557fd51SArtem B. Bityuckiy 	else
10698557fd51SArtem B. Bityuckiy 		ilen = JFFS2_F_I_SIZE(f);
10708557fd51SArtem B. Bityuckiy 
10711da177e4SLinus Torvalds 	ri.mode = cpu_to_jemode(JFFS2_F_I_MODE(f));
10721da177e4SLinus Torvalds 	ri.uid = cpu_to_je16(JFFS2_F_I_UID(f));
10731da177e4SLinus Torvalds 	ri.gid = cpu_to_je16(JFFS2_F_I_GID(f));
10748557fd51SArtem B. Bityuckiy 	ri.isize = cpu_to_je32(ilen);
10751da177e4SLinus Torvalds 	ri.atime = cpu_to_je32(JFFS2_F_I_ATIME(f));
10761da177e4SLinus Torvalds 	ri.ctime = cpu_to_je32(JFFS2_F_I_CTIME(f));
10771da177e4SLinus Torvalds 	ri.mtime = cpu_to_je32(JFFS2_F_I_MTIME(f));
10781da177e4SLinus Torvalds 	ri.data_crc = cpu_to_je32(0);
10791da177e4SLinus Torvalds 	ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
10801da177e4SLinus Torvalds 
10819fe4854cSDavid Woodhouse 	ret = jffs2_reserve_space_gc(c, sizeof(ri), &alloclen,
1082e631ddbaSFerenc Havasi 				     JFFS2_SUMMARY_INODE_SIZE);
10831da177e4SLinus Torvalds 	if (ret) {
1084da320f05SJoe Perches 		pr_warn("jffs2_reserve_space_gc of %zd bytes for garbage_collect_hole failed: %d\n",
10851da177e4SLinus Torvalds 			sizeof(ri), ret);
10861da177e4SLinus Torvalds 		return ret;
10871da177e4SLinus Torvalds 	}
10889fe4854cSDavid Woodhouse 	new_fn = jffs2_write_dnode(c, f, &ri, NULL, 0, ALLOC_GC);
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds 	if (IS_ERR(new_fn)) {
1091da320f05SJoe Perches 		pr_warn("Error writing new hole node: %ld\n", PTR_ERR(new_fn));
10921da177e4SLinus Torvalds 		return PTR_ERR(new_fn);
10931da177e4SLinus Torvalds 	}
10941da177e4SLinus Torvalds 	if (je32_to_cpu(ri.version) == f->highest_version) {
10951da177e4SLinus Torvalds 		jffs2_add_full_dnode_to_inode(c, f, new_fn);
10961da177e4SLinus Torvalds 		if (f->metadata) {
10971da177e4SLinus Torvalds 			jffs2_mark_node_obsolete(c, f->metadata->raw);
10981da177e4SLinus Torvalds 			jffs2_free_full_dnode(f->metadata);
10991da177e4SLinus Torvalds 			f->metadata = NULL;
11001da177e4SLinus Torvalds 		}
11011da177e4SLinus Torvalds 		return 0;
11021da177e4SLinus Torvalds 	}
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds 	/*
11051da177e4SLinus Torvalds 	 * We should only get here in the case where the node we are
11061da177e4SLinus Torvalds 	 * replacing had more than one frag, so we kept the same version
11071da177e4SLinus Torvalds 	 * number as before. (Except in case of error -- see 'goto fill;'
11081da177e4SLinus Torvalds 	 * above.)
11091da177e4SLinus Torvalds 	 */
11101da177e4SLinus Torvalds 	D1(if(unlikely(fn->frags <= 1)) {
1111da320f05SJoe Perches 			pr_warn("%s(): Replacing fn with %d frag(s) but new ver %d != highest_version %d of ino #%d\n",
1112da320f05SJoe Perches 				__func__, fn->frags, je32_to_cpu(ri.version),
1113da320f05SJoe Perches 				f->highest_version, je32_to_cpu(ri.ino));
11141da177e4SLinus Torvalds 	});
11151da177e4SLinus Torvalds 
11161da177e4SLinus Torvalds 	/* This is a partially-overlapped hole node. Mark it REF_NORMAL not REF_PRISTINE */
11171da177e4SLinus Torvalds 	mark_ref_normal(new_fn->raw);
11181da177e4SLinus Torvalds 
11191da177e4SLinus Torvalds 	for (frag = jffs2_lookup_node_frag(&f->fragtree, fn->ofs);
11201da177e4SLinus Torvalds 	     frag; frag = frag_next(frag)) {
11211da177e4SLinus Torvalds 		if (frag->ofs > fn->size + fn->ofs)
11221da177e4SLinus Torvalds 			break;
11231da177e4SLinus Torvalds 		if (frag->node == fn) {
11241da177e4SLinus Torvalds 			frag->node = new_fn;
11251da177e4SLinus Torvalds 			new_fn->frags++;
11261da177e4SLinus Torvalds 			fn->frags--;
11271da177e4SLinus Torvalds 		}
11281da177e4SLinus Torvalds 	}
11291da177e4SLinus Torvalds 	if (fn->frags) {
1130da320f05SJoe Perches 		pr_warn("%s(): Old node still has frags!\n", __func__);
11311da177e4SLinus Torvalds 		BUG();
11321da177e4SLinus Torvalds 	}
11331da177e4SLinus Torvalds 	if (!new_fn->frags) {
1134da320f05SJoe Perches 		pr_warn("%s(): New node has no frags!\n", __func__);
11351da177e4SLinus Torvalds 		BUG();
11361da177e4SLinus Torvalds 	}
11371da177e4SLinus Torvalds 
11381da177e4SLinus Torvalds 	jffs2_mark_node_obsolete(c, fn->raw);
11391da177e4SLinus Torvalds 	jffs2_free_full_dnode(fn);
11401da177e4SLinus Torvalds 
11411da177e4SLinus Torvalds 	return 0;
11421da177e4SLinus Torvalds }
11431da177e4SLinus Torvalds 
114425dc30b4SDavid Woodhouse static int jffs2_garbage_collect_dnode(struct jffs2_sb_info *c, struct jffs2_eraseblock *orig_jeb,
11451da177e4SLinus Torvalds 				       struct jffs2_inode_info *f, struct jffs2_full_dnode *fn,
11461da177e4SLinus Torvalds 				       uint32_t start, uint32_t end)
11471da177e4SLinus Torvalds {
11481da177e4SLinus Torvalds 	struct jffs2_full_dnode *new_fn;
11491da177e4SLinus Torvalds 	struct jffs2_raw_inode ri;
11509fe4854cSDavid Woodhouse 	uint32_t alloclen, offset, orig_end, orig_start;
11511da177e4SLinus Torvalds 	int ret = 0;
11521da177e4SLinus Torvalds 	unsigned char *comprbuf = NULL, *writebuf;
11531da177e4SLinus Torvalds 	unsigned long pg;
11541da177e4SLinus Torvalds 	unsigned char *pg_ptr;
11551da177e4SLinus Torvalds 
11561da177e4SLinus Torvalds 	memset(&ri, 0, sizeof(ri));
11571da177e4SLinus Torvalds 
11589c261b33SJoe Perches 	jffs2_dbg(1, "Writing replacement dnode for ino #%u from offset 0x%x to 0x%x\n",
11599c261b33SJoe Perches 		  f->inocache->ino, start, end);
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds 	orig_end = end;
11621da177e4SLinus Torvalds 	orig_start = start;
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds 	if (c->nr_free_blocks + c->nr_erasing_blocks > c->resv_blocks_gcmerge) {
11651da177e4SLinus Torvalds 		/* Attempt to do some merging. But only expand to cover logically
11661da177e4SLinus Torvalds 		   adjacent frags if the block containing them is already considered
11671da177e4SLinus Torvalds 		   to be dirty. Otherwise we end up with GC just going round in
11681da177e4SLinus Torvalds 		   circles dirtying the nodes it already wrote out, especially
11691da177e4SLinus Torvalds 		   on NAND where we have small eraseblocks and hence a much higher
11701da177e4SLinus Torvalds 		   chance of nodes having to be split to cross boundaries. */
11711da177e4SLinus Torvalds 
11721da177e4SLinus Torvalds 		struct jffs2_node_frag *frag;
11731da177e4SLinus Torvalds 		uint32_t min, max;
11741da177e4SLinus Torvalds 
11751da177e4SLinus Torvalds 		min = start & ~(PAGE_CACHE_SIZE-1);
11761da177e4SLinus Torvalds 		max = min + PAGE_CACHE_SIZE;
11771da177e4SLinus Torvalds 
11781da177e4SLinus Torvalds 		frag = jffs2_lookup_node_frag(&f->fragtree, start);
11791da177e4SLinus Torvalds 
11801da177e4SLinus Torvalds 		/* BUG_ON(!frag) but that'll happen anyway... */
11811da177e4SLinus Torvalds 
11821da177e4SLinus Torvalds 		BUG_ON(frag->ofs != start);
11831da177e4SLinus Torvalds 
11841da177e4SLinus Torvalds 		/* First grow down... */
11851da177e4SLinus Torvalds 		while((frag = frag_prev(frag)) && frag->ofs >= min) {
11861da177e4SLinus Torvalds 
11871da177e4SLinus Torvalds 			/* If the previous frag doesn't even reach the beginning, there's
11881da177e4SLinus Torvalds 			   excessive fragmentation. Just merge. */
11891da177e4SLinus Torvalds 			if (frag->ofs > min) {
11909c261b33SJoe Perches 				jffs2_dbg(1, "Expanding down to cover partial frag (0x%x-0x%x)\n",
11919c261b33SJoe Perches 					  frag->ofs, frag->ofs+frag->size);
11921da177e4SLinus Torvalds 				start = frag->ofs;
11931da177e4SLinus Torvalds 				continue;
11941da177e4SLinus Torvalds 			}
11951da177e4SLinus Torvalds 			/* OK. This frag holds the first byte of the page. */
11961da177e4SLinus Torvalds 			if (!frag->node || !frag->node->raw) {
11979c261b33SJoe Perches 				jffs2_dbg(1, "First frag in page is hole (0x%x-0x%x). Not expanding down.\n",
11989c261b33SJoe Perches 					  frag->ofs, frag->ofs+frag->size);
11991da177e4SLinus Torvalds 				break;
12001da177e4SLinus Torvalds 			} else {
12011da177e4SLinus Torvalds 
12021da177e4SLinus Torvalds 				/* OK, it's a frag which extends to the beginning of the page. Does it live
12031da177e4SLinus Torvalds 				   in a block which is still considered clean? If so, don't obsolete it.
12041da177e4SLinus Torvalds 				   If not, cover it anyway. */
12051da177e4SLinus Torvalds 
12061da177e4SLinus Torvalds 				struct jffs2_raw_node_ref *raw = frag->node->raw;
12071da177e4SLinus Torvalds 				struct jffs2_eraseblock *jeb;
12081da177e4SLinus Torvalds 
12091da177e4SLinus Torvalds 				jeb = &c->blocks[raw->flash_offset / c->sector_size];
12101da177e4SLinus Torvalds 
12111da177e4SLinus Torvalds 				if (jeb == c->gcblock) {
12129c261b33SJoe Perches 					jffs2_dbg(1, "Expanding down to cover frag (0x%x-0x%x) in gcblock at %08x\n",
12139c261b33SJoe Perches 						  frag->ofs,
12149c261b33SJoe Perches 						  frag->ofs + frag->size,
12159c261b33SJoe Perches 						  ref_offset(raw));
12161da177e4SLinus Torvalds 					start = frag->ofs;
12171da177e4SLinus Torvalds 					break;
12181da177e4SLinus Torvalds 				}
12191da177e4SLinus Torvalds 				if (!ISDIRTY(jeb->dirty_size + jeb->wasted_size)) {
12209c261b33SJoe Perches 					jffs2_dbg(1, "Not expanding down to cover frag (0x%x-0x%x) in clean block %08x\n",
12219c261b33SJoe Perches 						  frag->ofs,
12229c261b33SJoe Perches 						  frag->ofs + frag->size,
12239c261b33SJoe Perches 						  jeb->offset);
12241da177e4SLinus Torvalds 					break;
12251da177e4SLinus Torvalds 				}
12261da177e4SLinus Torvalds 
12279c261b33SJoe Perches 				jffs2_dbg(1, "Expanding down to cover frag (0x%x-0x%x) in dirty block %08x\n",
12289c261b33SJoe Perches 					  frag->ofs,
12299c261b33SJoe Perches 					  frag->ofs + frag->size,
12309c261b33SJoe Perches 					  jeb->offset);
12311da177e4SLinus Torvalds 				start = frag->ofs;
12321da177e4SLinus Torvalds 				break;
12331da177e4SLinus Torvalds 			}
12341da177e4SLinus Torvalds 		}
12351da177e4SLinus Torvalds 
12361da177e4SLinus Torvalds 		/* ... then up */
12371da177e4SLinus Torvalds 
12381da177e4SLinus Torvalds 		/* Find last frag which is actually part of the node we're to GC. */
12391da177e4SLinus Torvalds 		frag = jffs2_lookup_node_frag(&f->fragtree, end-1);
12401da177e4SLinus Torvalds 
12411da177e4SLinus Torvalds 		while((frag = frag_next(frag)) && frag->ofs+frag->size <= max) {
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds 			/* If the previous frag doesn't even reach the beginning, there's lots
12441da177e4SLinus Torvalds 			   of fragmentation. Just merge. */
12451da177e4SLinus Torvalds 			if (frag->ofs+frag->size < max) {
12469c261b33SJoe Perches 				jffs2_dbg(1, "Expanding up to cover partial frag (0x%x-0x%x)\n",
12479c261b33SJoe Perches 					  frag->ofs, frag->ofs+frag->size);
12481da177e4SLinus Torvalds 				end = frag->ofs + frag->size;
12491da177e4SLinus Torvalds 				continue;
12501da177e4SLinus Torvalds 			}
12511da177e4SLinus Torvalds 
12521da177e4SLinus Torvalds 			if (!frag->node || !frag->node->raw) {
12539c261b33SJoe Perches 				jffs2_dbg(1, "Last frag in page is hole (0x%x-0x%x). Not expanding up.\n",
12549c261b33SJoe Perches 					  frag->ofs, frag->ofs+frag->size);
12551da177e4SLinus Torvalds 				break;
12561da177e4SLinus Torvalds 			} else {
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds 				/* OK, it's a frag which extends to the beginning of the page. Does it live
12591da177e4SLinus Torvalds 				   in a block which is still considered clean? If so, don't obsolete it.
12601da177e4SLinus Torvalds 				   If not, cover it anyway. */
12611da177e4SLinus Torvalds 
12621da177e4SLinus Torvalds 				struct jffs2_raw_node_ref *raw = frag->node->raw;
12631da177e4SLinus Torvalds 				struct jffs2_eraseblock *jeb;
12641da177e4SLinus Torvalds 
12651da177e4SLinus Torvalds 				jeb = &c->blocks[raw->flash_offset / c->sector_size];
12661da177e4SLinus Torvalds 
12671da177e4SLinus Torvalds 				if (jeb == c->gcblock) {
12689c261b33SJoe Perches 					jffs2_dbg(1, "Expanding up to cover frag (0x%x-0x%x) in gcblock at %08x\n",
12699c261b33SJoe Perches 						  frag->ofs,
12709c261b33SJoe Perches 						  frag->ofs + frag->size,
12719c261b33SJoe Perches 						  ref_offset(raw));
12721da177e4SLinus Torvalds 					end = frag->ofs + frag->size;
12731da177e4SLinus Torvalds 					break;
12741da177e4SLinus Torvalds 				}
12751da177e4SLinus Torvalds 				if (!ISDIRTY(jeb->dirty_size + jeb->wasted_size)) {
12769c261b33SJoe Perches 					jffs2_dbg(1, "Not expanding up to cover frag (0x%x-0x%x) in clean block %08x\n",
12779c261b33SJoe Perches 						  frag->ofs,
12789c261b33SJoe Perches 						  frag->ofs + frag->size,
12799c261b33SJoe Perches 						  jeb->offset);
12801da177e4SLinus Torvalds 					break;
12811da177e4SLinus Torvalds 				}
12821da177e4SLinus Torvalds 
12839c261b33SJoe Perches 				jffs2_dbg(1, "Expanding up to cover frag (0x%x-0x%x) in dirty block %08x\n",
12849c261b33SJoe Perches 					  frag->ofs,
12859c261b33SJoe Perches 					  frag->ofs + frag->size,
12869c261b33SJoe Perches 					  jeb->offset);
12871da177e4SLinus Torvalds 				end = frag->ofs + frag->size;
12881da177e4SLinus Torvalds 				break;
12891da177e4SLinus Torvalds 			}
12901da177e4SLinus Torvalds 		}
12919c261b33SJoe Perches 		jffs2_dbg(1, "Expanded dnode to write from (0x%x-0x%x) to (0x%x-0x%x)\n",
12929c261b33SJoe Perches 			  orig_start, orig_end, start, end);
12931da177e4SLinus Torvalds 
12948557fd51SArtem B. Bityuckiy 		D1(BUG_ON(end > frag_last(&f->fragtree)->ofs + frag_last(&f->fragtree)->size));
12951da177e4SLinus Torvalds 		BUG_ON(end < orig_end);
12961da177e4SLinus Torvalds 		BUG_ON(start > orig_start);
12971da177e4SLinus Torvalds 	}
12981da177e4SLinus Torvalds 
129949e91e70SDavid Woodhouse 	/* The rules state that we must obtain the page lock *before* f->sem, so
130049e91e70SDavid Woodhouse 	 * drop f->sem temporarily. Since we also hold c->alloc_sem, nothing's
130149e91e70SDavid Woodhouse 	 * actually going to *change* so we're safe; we only allow reading.
130249e91e70SDavid Woodhouse 	 *
130349e91e70SDavid Woodhouse 	 * It is important to note that jffs2_write_begin() will ensure that its
130449e91e70SDavid Woodhouse 	 * page is marked Uptodate before allocating space. That means that if we
130549e91e70SDavid Woodhouse 	 * end up here trying to GC the *same* page that jffs2_write_begin() is
130649e91e70SDavid Woodhouse 	 * trying to write out, read_cache_page() will not deadlock. */
130749e91e70SDavid Woodhouse 	mutex_unlock(&f->sem);
13081da177e4SLinus Torvalds 	pg_ptr = jffs2_gc_fetch_page(c, f, start, &pg);
130949e91e70SDavid Woodhouse 	mutex_lock(&f->sem);
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds 	if (IS_ERR(pg_ptr)) {
1312da320f05SJoe Perches 		pr_warn("read_cache_page() returned error: %ld\n",
1313da320f05SJoe Perches 			PTR_ERR(pg_ptr));
13141da177e4SLinus Torvalds 		return PTR_ERR(pg_ptr);
13151da177e4SLinus Torvalds 	}
13161da177e4SLinus Torvalds 
13171da177e4SLinus Torvalds 	offset = start;
13181da177e4SLinus Torvalds 	while(offset < orig_end) {
13191da177e4SLinus Torvalds 		uint32_t datalen;
13201da177e4SLinus Torvalds 		uint32_t cdatalen;
13211da177e4SLinus Torvalds 		uint16_t comprtype = JFFS2_COMPR_NONE;
13221da177e4SLinus Torvalds 
13239fe4854cSDavid Woodhouse 		ret = jffs2_reserve_space_gc(c, sizeof(ri) + JFFS2_MIN_DATA_LEN,
1324e631ddbaSFerenc Havasi 					&alloclen, JFFS2_SUMMARY_INODE_SIZE);
13251da177e4SLinus Torvalds 
13261da177e4SLinus Torvalds 		if (ret) {
1327da320f05SJoe Perches 			pr_warn("jffs2_reserve_space_gc of %zd bytes for garbage_collect_dnode failed: %d\n",
13281da177e4SLinus Torvalds 				sizeof(ri) + JFFS2_MIN_DATA_LEN, ret);
13291da177e4SLinus Torvalds 			break;
13301da177e4SLinus Torvalds 		}
13311da177e4SLinus Torvalds 		cdatalen = min_t(uint32_t, alloclen - sizeof(ri), end - offset);
13321da177e4SLinus Torvalds 		datalen = end - offset;
13331da177e4SLinus Torvalds 
13341da177e4SLinus Torvalds 		writebuf = pg_ptr + (offset & (PAGE_CACHE_SIZE -1));
13351da177e4SLinus Torvalds 
13361da177e4SLinus Torvalds 		comprtype = jffs2_compress(c, f, writebuf, &comprbuf, &datalen, &cdatalen);
13371da177e4SLinus Torvalds 
13381da177e4SLinus Torvalds 		ri.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
13391da177e4SLinus Torvalds 		ri.nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
13401da177e4SLinus Torvalds 		ri.totlen = cpu_to_je32(sizeof(ri) + cdatalen);
13411da177e4SLinus Torvalds 		ri.hdr_crc = cpu_to_je32(crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4));
13421da177e4SLinus Torvalds 
13431da177e4SLinus Torvalds 		ri.ino = cpu_to_je32(f->inocache->ino);
13441da177e4SLinus Torvalds 		ri.version = cpu_to_je32(++f->highest_version);
13451da177e4SLinus Torvalds 		ri.mode = cpu_to_jemode(JFFS2_F_I_MODE(f));
13461da177e4SLinus Torvalds 		ri.uid = cpu_to_je16(JFFS2_F_I_UID(f));
13471da177e4SLinus Torvalds 		ri.gid = cpu_to_je16(JFFS2_F_I_GID(f));
13481da177e4SLinus Torvalds 		ri.isize = cpu_to_je32(JFFS2_F_I_SIZE(f));
13491da177e4SLinus Torvalds 		ri.atime = cpu_to_je32(JFFS2_F_I_ATIME(f));
13501da177e4SLinus Torvalds 		ri.ctime = cpu_to_je32(JFFS2_F_I_CTIME(f));
13511da177e4SLinus Torvalds 		ri.mtime = cpu_to_je32(JFFS2_F_I_MTIME(f));
13521da177e4SLinus Torvalds 		ri.offset = cpu_to_je32(offset);
13531da177e4SLinus Torvalds 		ri.csize = cpu_to_je32(cdatalen);
13541da177e4SLinus Torvalds 		ri.dsize = cpu_to_je32(datalen);
13551da177e4SLinus Torvalds 		ri.compr = comprtype & 0xff;
13561da177e4SLinus Torvalds 		ri.usercompr = (comprtype >> 8) & 0xff;
13571da177e4SLinus Torvalds 		ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
13581da177e4SLinus Torvalds 		ri.data_crc = cpu_to_je32(crc32(0, comprbuf, cdatalen));
13591da177e4SLinus Torvalds 
13609fe4854cSDavid Woodhouse 		new_fn = jffs2_write_dnode(c, f, &ri, comprbuf, cdatalen, ALLOC_GC);
13611da177e4SLinus Torvalds 
13621da177e4SLinus Torvalds 		jffs2_free_comprbuf(comprbuf, writebuf);
13631da177e4SLinus Torvalds 
13641da177e4SLinus Torvalds 		if (IS_ERR(new_fn)) {
1365da320f05SJoe Perches 			pr_warn("Error writing new dnode: %ld\n",
1366da320f05SJoe Perches 				PTR_ERR(new_fn));
13671da177e4SLinus Torvalds 			ret = PTR_ERR(new_fn);
13681da177e4SLinus Torvalds 			break;
13691da177e4SLinus Torvalds 		}
13701da177e4SLinus Torvalds 		ret = jffs2_add_full_dnode_to_inode(c, f, new_fn);
13711da177e4SLinus Torvalds 		offset += datalen;
13721da177e4SLinus Torvalds 		if (f->metadata) {
13731da177e4SLinus Torvalds 			jffs2_mark_node_obsolete(c, f->metadata->raw);
13741da177e4SLinus Torvalds 			jffs2_free_full_dnode(f->metadata);
13751da177e4SLinus Torvalds 			f->metadata = NULL;
13761da177e4SLinus Torvalds 		}
13771da177e4SLinus Torvalds 	}
13781da177e4SLinus Torvalds 
13791da177e4SLinus Torvalds 	jffs2_gc_release_page(c, pg_ptr, &pg);
13801da177e4SLinus Torvalds 	return ret;
13811da177e4SLinus Torvalds }
1382