xref: /openbmc/linux/fs/jffs2/scan.c (revision 48396413)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * JFFS2 -- Journalling Flash File System, Version 2.
31da177e4SLinus Torvalds  *
4c00c310eSDavid Woodhouse  * Copyright © 2001-2007 Red Hat, Inc.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Created by David Woodhouse <dwmw2@infradead.org>
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * For licensing information, see the file 'LICENCE' in this directory.
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  */
11c00c310eSDavid Woodhouse 
121da177e4SLinus Torvalds #include <linux/kernel.h>
131da177e4SLinus Torvalds #include <linux/sched.h>
141da177e4SLinus Torvalds #include <linux/slab.h>
151da177e4SLinus Torvalds #include <linux/mtd/mtd.h>
161da177e4SLinus Torvalds #include <linux/pagemap.h>
171da177e4SLinus Torvalds #include <linux/crc32.h>
181da177e4SLinus Torvalds #include <linux/compiler.h>
191da177e4SLinus Torvalds #include "nodelist.h"
20e631ddbaSFerenc Havasi #include "summary.h"
21e631ddbaSFerenc Havasi #include "debug.h"
221da177e4SLinus Torvalds 
233be36675SAndrew Victor #define DEFAULT_EMPTY_SCAN_SIZE 1024
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds #define noisy_printk(noise, args...) do { \
261da177e4SLinus Torvalds 	if (*(noise)) { \
271da177e4SLinus Torvalds 		printk(KERN_NOTICE args); \
281da177e4SLinus Torvalds 		 (*(noise))--; \
291da177e4SLinus Torvalds 		 if (!(*(noise))) { \
301da177e4SLinus Torvalds 			 printk(KERN_NOTICE "Further such events for this erase block will not be printed\n"); \
311da177e4SLinus Torvalds 		 } \
321da177e4SLinus Torvalds 	} \
331da177e4SLinus Torvalds } while(0)
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds static uint32_t pseudo_random;
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
38e631ddbaSFerenc Havasi 				  unsigned char *buf, uint32_t buf_size, struct jffs2_summary *s);
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds /* These helper functions _must_ increase ofs and also do the dirty/used space accounting.
411da177e4SLinus Torvalds  * Returning an error will abort the mount - bad checksums etc. should just mark the space
421da177e4SLinus Torvalds  * as dirty.
431da177e4SLinus Torvalds  */
441da177e4SLinus Torvalds static int jffs2_scan_inode_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
45e631ddbaSFerenc Havasi 				 struct jffs2_raw_inode *ri, uint32_t ofs, struct jffs2_summary *s);
461da177e4SLinus Torvalds static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
47e631ddbaSFerenc Havasi 				 struct jffs2_raw_dirent *rd, uint32_t ofs, struct jffs2_summary *s);
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds static inline int min_free(struct jffs2_sb_info *c)
501da177e4SLinus Torvalds {
511da177e4SLinus Torvalds 	uint32_t min = 2 * sizeof(struct jffs2_raw_inode);
522f82ce1eSAndrew Victor #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
531da177e4SLinus Torvalds 	if (!jffs2_can_mark_obsolete(c) && min < c->wbuf_pagesize)
541da177e4SLinus Torvalds 		return c->wbuf_pagesize;
551da177e4SLinus Torvalds #endif
561da177e4SLinus Torvalds 	return min;
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds }
593be36675SAndrew Victor 
603be36675SAndrew Victor static inline uint32_t EMPTY_SCAN_SIZE(uint32_t sector_size) {
613be36675SAndrew Victor 	if (sector_size < DEFAULT_EMPTY_SCAN_SIZE)
623be36675SAndrew Victor 		return sector_size;
633be36675SAndrew Victor 	else
643be36675SAndrew Victor 		return DEFAULT_EMPTY_SCAN_SIZE;
653be36675SAndrew Victor }
663be36675SAndrew Victor 
6725090a6bSDavid Woodhouse static int file_dirty(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
6825090a6bSDavid Woodhouse {
69a6a8bef7SDavid Woodhouse 	int ret;
70a6a8bef7SDavid Woodhouse 
71a6a8bef7SDavid Woodhouse 	if ((ret = jffs2_prealloc_raw_node_refs(c, jeb, 1)))
72a6a8bef7SDavid Woodhouse 		return ret;
73a6a8bef7SDavid Woodhouse 	if ((ret = jffs2_scan_dirty_space(c, jeb, jeb->free_size)))
7425090a6bSDavid Woodhouse 		return ret;
7525090a6bSDavid Woodhouse 	/* Turned wasted size into dirty, since we apparently
7625090a6bSDavid Woodhouse 	   think it's recoverable now. */
7725090a6bSDavid Woodhouse 	jeb->dirty_size += jeb->wasted_size;
7825090a6bSDavid Woodhouse 	c->dirty_size += jeb->wasted_size;
7925090a6bSDavid Woodhouse 	c->wasted_size -= jeb->wasted_size;
8025090a6bSDavid Woodhouse 	jeb->wasted_size = 0;
8125090a6bSDavid Woodhouse 	if (VERYDIRTY(c, jeb->dirty_size)) {
8225090a6bSDavid Woodhouse 		list_add(&jeb->list, &c->very_dirty_list);
8325090a6bSDavid Woodhouse 	} else {
8425090a6bSDavid Woodhouse 		list_add(&jeb->list, &c->dirty_list);
8525090a6bSDavid Woodhouse 	}
8625090a6bSDavid Woodhouse 	return 0;
8725090a6bSDavid Woodhouse }
8825090a6bSDavid Woodhouse 
891da177e4SLinus Torvalds int jffs2_scan_medium(struct jffs2_sb_info *c)
901da177e4SLinus Torvalds {
911da177e4SLinus Torvalds 	int i, ret;
921da177e4SLinus Torvalds 	uint32_t empty_blocks = 0, bad_blocks = 0;
931da177e4SLinus Torvalds 	unsigned char *flashbuf = NULL;
941da177e4SLinus Torvalds 	uint32_t buf_size = 0;
95e631ddbaSFerenc Havasi 	struct jffs2_summary *s = NULL; /* summary info collected by the scan process */
961da177e4SLinus Torvalds #ifndef __ECOS
971da177e4SLinus Torvalds 	size_t pointlen;
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds 	if (c->mtd->point) {
100a98889f3SJared Hulbert 		ret = c->mtd->point(c->mtd, 0, c->mtd->size, &pointlen,
101a98889f3SJared Hulbert 				    (void **)&flashbuf, NULL);
1021da177e4SLinus Torvalds 		if (!ret && pointlen < c->mtd->size) {
1031da177e4SLinus Torvalds 			/* Don't muck about if it won't let us point to the whole flash */
1041da177e4SLinus Torvalds 			D1(printk(KERN_DEBUG "MTD point returned len too short: 0x%zx\n", pointlen));
105a98889f3SJared Hulbert 			c->mtd->unpoint(c->mtd, 0, pointlen);
1061da177e4SLinus Torvalds 			flashbuf = NULL;
1071da177e4SLinus Torvalds 		}
1081da177e4SLinus Torvalds 		if (ret)
1091da177e4SLinus Torvalds 			D1(printk(KERN_DEBUG "MTD point failed %d\n", ret));
1101da177e4SLinus Torvalds 	}
1111da177e4SLinus Torvalds #endif
1121da177e4SLinus Torvalds 	if (!flashbuf) {
1131da177e4SLinus Torvalds 		/* For NAND it's quicker to read a whole eraseblock at a time,
1141da177e4SLinus Torvalds 		   apparently */
1151da177e4SLinus Torvalds 		if (jffs2_cleanmarker_oob(c))
1161da177e4SLinus Torvalds 			buf_size = c->sector_size;
1171da177e4SLinus Torvalds 		else
1181da177e4SLinus Torvalds 			buf_size = PAGE_SIZE;
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds 		/* Respect kmalloc limitations */
1211da177e4SLinus Torvalds 		if (buf_size > 128*1024)
1221da177e4SLinus Torvalds 			buf_size = 128*1024;
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds 		D1(printk(KERN_DEBUG "Allocating readbuf of %d bytes\n", buf_size));
1251da177e4SLinus Torvalds 		flashbuf = kmalloc(buf_size, GFP_KERNEL);
1261da177e4SLinus Torvalds 		if (!flashbuf)
1271da177e4SLinus Torvalds 			return -ENOMEM;
1281da177e4SLinus Torvalds 	}
1291da177e4SLinus Torvalds 
130e631ddbaSFerenc Havasi 	if (jffs2_sum_active()) {
1313d375d9eSYan Burman 		s = kzalloc(sizeof(struct jffs2_summary), GFP_KERNEL);
132e631ddbaSFerenc Havasi 		if (!s) {
133e631ddbaSFerenc Havasi 			JFFS2_WARNING("Can't allocate memory for summary\n");
13448396413SDavid Woodhouse 			ret = -ENOMEM;
13548396413SDavid Woodhouse 			goto out;
136e631ddbaSFerenc Havasi 		}
137e631ddbaSFerenc Havasi 	}
138e631ddbaSFerenc Havasi 
1391da177e4SLinus Torvalds 	for (i=0; i<c->nr_blocks; i++) {
1401da177e4SLinus Torvalds 		struct jffs2_eraseblock *jeb = &c->blocks[i];
1411da177e4SLinus Torvalds 
142a2166b93SArtem Bityutskiy 		cond_resched();
143a2166b93SArtem Bityutskiy 
144e631ddbaSFerenc Havasi 		/* reset summary info for next eraseblock scan */
145e631ddbaSFerenc Havasi 		jffs2_sum_reset_collected(s);
146e631ddbaSFerenc Havasi 
147e631ddbaSFerenc Havasi 		ret = jffs2_scan_eraseblock(c, jeb, buf_size?flashbuf:(flashbuf+jeb->offset),
148e631ddbaSFerenc Havasi 						buf_size, s);
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds 		if (ret < 0)
1511da177e4SLinus Torvalds 			goto out;
1521da177e4SLinus Torvalds 
153e0c8e42fSArtem B. Bityutskiy 		jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds 		/* Now decide which list to put it on */
1561da177e4SLinus Torvalds 		switch(ret) {
1571da177e4SLinus Torvalds 		case BLK_STATE_ALLFF:
1581da177e4SLinus Torvalds 			/*
1591da177e4SLinus Torvalds 			 * Empty block.   Since we can't be sure it
1601da177e4SLinus Torvalds 			 * was entirely erased, we just queue it for erase
1611da177e4SLinus Torvalds 			 * again.  It will be marked as such when the erase
1621da177e4SLinus Torvalds 			 * is complete.  Meanwhile we still count it as empty
1631da177e4SLinus Torvalds 			 * for later checks.
1641da177e4SLinus Torvalds 			 */
1651da177e4SLinus Torvalds 			empty_blocks++;
1661da177e4SLinus Torvalds 			list_add(&jeb->list, &c->erase_pending_list);
1671da177e4SLinus Torvalds 			c->nr_erasing_blocks++;
1681da177e4SLinus Torvalds 			break;
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds 		case BLK_STATE_CLEANMARKER:
1711da177e4SLinus Torvalds 			/* Only a CLEANMARKER node is valid */
1721da177e4SLinus Torvalds 			if (!jeb->dirty_size) {
1731da177e4SLinus Torvalds 				/* It's actually free */
1741da177e4SLinus Torvalds 				list_add(&jeb->list, &c->free_list);
1751da177e4SLinus Torvalds 				c->nr_free_blocks++;
1761da177e4SLinus Torvalds 			} else {
1771da177e4SLinus Torvalds 				/* Dirt */
1781da177e4SLinus Torvalds 				D1(printk(KERN_DEBUG "Adding all-dirty block at 0x%08x to erase_pending_list\n", jeb->offset));
1791da177e4SLinus Torvalds 				list_add(&jeb->list, &c->erase_pending_list);
1801da177e4SLinus Torvalds 				c->nr_erasing_blocks++;
1811da177e4SLinus Torvalds 			}
1821da177e4SLinus Torvalds 			break;
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds 		case BLK_STATE_CLEAN:
1851da177e4SLinus Torvalds 			/* Full (or almost full) of clean data. Clean list */
1861da177e4SLinus Torvalds 			list_add(&jeb->list, &c->clean_list);
1871da177e4SLinus Torvalds 			break;
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds 		case BLK_STATE_PARTDIRTY:
1901da177e4SLinus Torvalds 			/* Some data, but not full. Dirty list. */
1911da177e4SLinus Torvalds 			/* We want to remember the block with most free space
1921da177e4SLinus Torvalds 			and stick it in the 'nextblock' position to start writing to it. */
1931da177e4SLinus Torvalds 			if (jeb->free_size > min_free(c) &&
1941da177e4SLinus Torvalds 					(!c->nextblock || c->nextblock->free_size < jeb->free_size)) {
1951da177e4SLinus Torvalds 				/* Better candidate for the next writes to go to */
1961da177e4SLinus Torvalds 				if (c->nextblock) {
19725090a6bSDavid Woodhouse 					ret = file_dirty(c, c->nextblock);
19825090a6bSDavid Woodhouse 					if (ret)
199a2ab0ce0SChristian Engelmayer 						goto out;
200e631ddbaSFerenc Havasi 					/* deleting summary information of the old nextblock */
201e631ddbaSFerenc Havasi 					jffs2_sum_reset_collected(c->summary);
2021da177e4SLinus Torvalds 				}
20325090a6bSDavid Woodhouse 				/* update collected summary information for the current nextblock */
204e631ddbaSFerenc Havasi 				jffs2_sum_move_collected(c, s);
205e631ddbaSFerenc Havasi 				D1(printk(KERN_DEBUG "jffs2_scan_medium(): new nextblock = 0x%08x\n", jeb->offset));
2061da177e4SLinus Torvalds 				c->nextblock = jeb;
2071da177e4SLinus Torvalds 			} else {
20825090a6bSDavid Woodhouse 				ret = file_dirty(c, jeb);
20925090a6bSDavid Woodhouse 				if (ret)
210a2ab0ce0SChristian Engelmayer 					goto out;
2111da177e4SLinus Torvalds 			}
2121da177e4SLinus Torvalds 			break;
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 		case BLK_STATE_ALLDIRTY:
2151da177e4SLinus Torvalds 			/* Nothing valid - not even a clean marker. Needs erasing. */
2161da177e4SLinus Torvalds 			/* For now we just put it on the erasing list. We'll start the erases later */
2171da177e4SLinus Torvalds 			D1(printk(KERN_NOTICE "JFFS2: Erase block at 0x%08x is not formatted. It will be erased\n", jeb->offset));
2181da177e4SLinus Torvalds 			list_add(&jeb->list, &c->erase_pending_list);
2191da177e4SLinus Torvalds 			c->nr_erasing_blocks++;
2201da177e4SLinus Torvalds 			break;
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 		case BLK_STATE_BADBLOCK:
2231da177e4SLinus Torvalds 			D1(printk(KERN_NOTICE "JFFS2: Block at 0x%08x is bad\n", jeb->offset));
2241da177e4SLinus Torvalds 			list_add(&jeb->list, &c->bad_list);
2251da177e4SLinus Torvalds 			c->bad_size += c->sector_size;
2261da177e4SLinus Torvalds 			c->free_size -= c->sector_size;
2271da177e4SLinus Torvalds 			bad_blocks++;
2281da177e4SLinus Torvalds 			break;
2291da177e4SLinus Torvalds 		default:
2301da177e4SLinus Torvalds 			printk(KERN_WARNING "jffs2_scan_medium(): unknown block state\n");
2311da177e4SLinus Torvalds 			BUG();
2321da177e4SLinus Torvalds 		}
2331da177e4SLinus Torvalds 	}
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 	/* Nextblock dirty is always seen as wasted, because we cannot recycle it now */
2361da177e4SLinus Torvalds 	if (c->nextblock && (c->nextblock->dirty_size)) {
2371da177e4SLinus Torvalds 		c->nextblock->wasted_size += c->nextblock->dirty_size;
2381da177e4SLinus Torvalds 		c->wasted_size += c->nextblock->dirty_size;
2391da177e4SLinus Torvalds 		c->dirty_size -= c->nextblock->dirty_size;
2401da177e4SLinus Torvalds 		c->nextblock->dirty_size = 0;
2411da177e4SLinus Torvalds 	}
2422f82ce1eSAndrew Victor #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
243e96fb230SDavid Woodhouse 	if (!jffs2_can_mark_obsolete(c) && c->wbuf_pagesize && c->nextblock && (c->nextblock->free_size % c->wbuf_pagesize)) {
2441da177e4SLinus Torvalds 		/* If we're going to start writing into a block which already
2451da177e4SLinus Torvalds 		   contains data, and the end of the data isn't page-aligned,
2461da177e4SLinus Torvalds 		   skip a little and align it. */
2471da177e4SLinus Torvalds 
248daba5cc4SArtem B. Bityutskiy 		uint32_t skip = c->nextblock->free_size % c->wbuf_pagesize;
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds 		D1(printk(KERN_DEBUG "jffs2_scan_medium(): Skipping %d bytes in nextblock to ensure page alignment\n",
2511da177e4SLinus Torvalds 			  skip));
252046b8b98SDavid Woodhouse 		jffs2_prealloc_raw_node_refs(c, c->nextblock, 1);
253f560928bSDavid Woodhouse 		jffs2_scan_dirty_space(c, c->nextblock, skip);
2541da177e4SLinus Torvalds 	}
2551da177e4SLinus Torvalds #endif
2561da177e4SLinus Torvalds 	if (c->nr_erasing_blocks) {
2571da177e4SLinus Torvalds 		if ( !c->used_size && ((c->nr_free_blocks+empty_blocks+bad_blocks)!= c->nr_blocks || bad_blocks == c->nr_blocks) ) {
2581da177e4SLinus Torvalds 			printk(KERN_NOTICE "Cowardly refusing to erase blocks on filesystem with no valid JFFS2 nodes\n");
2591da177e4SLinus Torvalds 			printk(KERN_NOTICE "empty_blocks %d, bad_blocks %d, c->nr_blocks %d\n",empty_blocks,bad_blocks,c->nr_blocks);
2601da177e4SLinus Torvalds 			ret = -EIO;
2611da177e4SLinus Torvalds 			goto out;
2621da177e4SLinus Torvalds 		}
2631da177e4SLinus Torvalds 		jffs2_erase_pending_trigger(c);
2641da177e4SLinus Torvalds 	}
2651da177e4SLinus Torvalds 	ret = 0;
2661da177e4SLinus Torvalds  out:
2671da177e4SLinus Torvalds 	if (buf_size)
2681da177e4SLinus Torvalds 		kfree(flashbuf);
2691da177e4SLinus Torvalds #ifndef __ECOS
2701da177e4SLinus Torvalds 	else
271a98889f3SJared Hulbert 		c->mtd->unpoint(c->mtd, 0, c->mtd->size);
2721da177e4SLinus Torvalds #endif
2735b5ffbc1SFlorin Malita 	if (s)
2745b5ffbc1SFlorin Malita 		kfree(s);
2755b5ffbc1SFlorin Malita 
2761da177e4SLinus Torvalds 	return ret;
2771da177e4SLinus Torvalds }
2781da177e4SLinus Torvalds 
279c05d52c7SAdrian Bunk static int jffs2_fill_scan_buf(struct jffs2_sb_info *c, void *buf,
2801da177e4SLinus Torvalds 			       uint32_t ofs, uint32_t len)
2811da177e4SLinus Torvalds {
2821da177e4SLinus Torvalds 	int ret;
2831da177e4SLinus Torvalds 	size_t retlen;
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	ret = jffs2_flash_read(c, ofs, len, &retlen, buf);
2861da177e4SLinus Torvalds 	if (ret) {
2871da177e4SLinus Torvalds 		D1(printk(KERN_WARNING "mtd->read(0x%x bytes from 0x%x) returned %d\n", len, ofs, ret));
2881da177e4SLinus Torvalds 		return ret;
2891da177e4SLinus Torvalds 	}
2901da177e4SLinus Torvalds 	if (retlen < len) {
2911da177e4SLinus Torvalds 		D1(printk(KERN_WARNING "Read at 0x%x gave only 0x%zx bytes\n", ofs, retlen));
2921da177e4SLinus Torvalds 		return -EIO;
2931da177e4SLinus Torvalds 	}
2941da177e4SLinus Torvalds 	return 0;
2951da177e4SLinus Torvalds }
2961da177e4SLinus Torvalds 
297e631ddbaSFerenc Havasi int jffs2_scan_classify_jeb(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
298e631ddbaSFerenc Havasi {
299e631ddbaSFerenc Havasi 	if ((jeb->used_size + jeb->unchecked_size) == PAD(c->cleanmarker_size) && !jeb->dirty_size
30099988f7bSDavid Woodhouse 	    && (!jeb->first_node || !ref_next(jeb->first_node)) )
301e631ddbaSFerenc Havasi 		return BLK_STATE_CLEANMARKER;
302e631ddbaSFerenc Havasi 
303e631ddbaSFerenc Havasi 	/* move blocks with max 4 byte dirty space to cleanlist */
304e631ddbaSFerenc Havasi 	else if (!ISDIRTY(c->sector_size - (jeb->used_size + jeb->unchecked_size))) {
305e631ddbaSFerenc Havasi 		c->dirty_size -= jeb->dirty_size;
306e631ddbaSFerenc Havasi 		c->wasted_size += jeb->dirty_size;
307e631ddbaSFerenc Havasi 		jeb->wasted_size += jeb->dirty_size;
308e631ddbaSFerenc Havasi 		jeb->dirty_size = 0;
309e631ddbaSFerenc Havasi 		return BLK_STATE_CLEAN;
310e631ddbaSFerenc Havasi 	} else if (jeb->used_size || jeb->unchecked_size)
311e631ddbaSFerenc Havasi 		return BLK_STATE_PARTDIRTY;
312e631ddbaSFerenc Havasi 	else
313e631ddbaSFerenc Havasi 		return BLK_STATE_ALLDIRTY;
314e631ddbaSFerenc Havasi }
315e631ddbaSFerenc Havasi 
316aa98d7cfSKaiGai Kohei #ifdef CONFIG_JFFS2_FS_XATTR
317aa98d7cfSKaiGai Kohei static int jffs2_scan_xattr_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
318aa98d7cfSKaiGai Kohei 				 struct jffs2_raw_xattr *rx, uint32_t ofs,
319aa98d7cfSKaiGai Kohei 				 struct jffs2_summary *s)
320aa98d7cfSKaiGai Kohei {
321aa98d7cfSKaiGai Kohei 	struct jffs2_xattr_datum *xd;
322c9f700f8SKaiGai Kohei 	uint32_t xid, version, totlen, crc;
32368270995SDavid Woodhouse 	int err;
324aa98d7cfSKaiGai Kohei 
325aa98d7cfSKaiGai Kohei 	crc = crc32(0, rx, sizeof(struct jffs2_raw_xattr) - 4);
326aa98d7cfSKaiGai Kohei 	if (crc != je32_to_cpu(rx->node_crc)) {
327aa98d7cfSKaiGai Kohei 		JFFS2_WARNING("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
328aa98d7cfSKaiGai Kohei 			      ofs, je32_to_cpu(rx->node_crc), crc);
32968270995SDavid Woodhouse 		if ((err = jffs2_scan_dirty_space(c, jeb, je32_to_cpu(rx->totlen))))
33068270995SDavid Woodhouse 			return err;
331aa98d7cfSKaiGai Kohei 		return 0;
332aa98d7cfSKaiGai Kohei 	}
333aa98d7cfSKaiGai Kohei 
334c9f700f8SKaiGai Kohei 	xid = je32_to_cpu(rx->xid);
335c9f700f8SKaiGai Kohei 	version = je32_to_cpu(rx->version);
336c9f700f8SKaiGai Kohei 
3378a13695cSKaiGai Kohei 	totlen = PAD(sizeof(struct jffs2_raw_xattr)
3388a13695cSKaiGai Kohei 			+ rx->name_len + 1 + je16_to_cpu(rx->value_len));
339aa98d7cfSKaiGai Kohei 	if (totlen != je32_to_cpu(rx->totlen)) {
340aa98d7cfSKaiGai Kohei 		JFFS2_WARNING("node length mismatch at %#08x, read=%u, calc=%u\n",
341aa98d7cfSKaiGai Kohei 			      ofs, je32_to_cpu(rx->totlen), totlen);
34268270995SDavid Woodhouse 		if ((err = jffs2_scan_dirty_space(c, jeb, je32_to_cpu(rx->totlen))))
34368270995SDavid Woodhouse 			return err;
344aa98d7cfSKaiGai Kohei 		return 0;
345aa98d7cfSKaiGai Kohei 	}
346aa98d7cfSKaiGai Kohei 
347c9f700f8SKaiGai Kohei 	xd = jffs2_setup_xattr_datum(c, xid, version);
348c9f700f8SKaiGai Kohei 	if (IS_ERR(xd))
349aa98d7cfSKaiGai Kohei 		return PTR_ERR(xd);
350c9f700f8SKaiGai Kohei 
351c9f700f8SKaiGai Kohei 	if (xd->version > version) {
352c9f700f8SKaiGai Kohei 		struct jffs2_raw_node_ref *raw
353c9f700f8SKaiGai Kohei 			= jffs2_link_node_ref(c, jeb, ofs | REF_PRISTINE, totlen, NULL);
354c9f700f8SKaiGai Kohei 		raw->next_in_ino = xd->node->next_in_ino;
355c9f700f8SKaiGai Kohei 		xd->node->next_in_ino = raw;
356c9f700f8SKaiGai Kohei 	} else {
357c9f700f8SKaiGai Kohei 		xd->version = version;
358aa98d7cfSKaiGai Kohei 		xd->xprefix = rx->xprefix;
359aa98d7cfSKaiGai Kohei 		xd->name_len = rx->name_len;
360aa98d7cfSKaiGai Kohei 		xd->value_len = je16_to_cpu(rx->value_len);
361aa98d7cfSKaiGai Kohei 		xd->data_crc = je32_to_cpu(rx->data_crc);
362aa98d7cfSKaiGai Kohei 
363c9f700f8SKaiGai Kohei 		jffs2_link_node_ref(c, jeb, ofs | REF_PRISTINE, totlen, (void *)xd);
364c9f700f8SKaiGai Kohei 	}
365f1f9671bSDavid Woodhouse 
366aa98d7cfSKaiGai Kohei 	if (jffs2_sum_active())
367aa98d7cfSKaiGai Kohei 		jffs2_sum_add_xattr_mem(s, rx, ofs - jeb->offset);
368aa98d7cfSKaiGai Kohei 	dbg_xattr("scaning xdatum at %#08x (xid=%u, version=%u)\n",
369aa98d7cfSKaiGai Kohei 		  ofs, xd->xid, xd->version);
370aa98d7cfSKaiGai Kohei 	return 0;
371aa98d7cfSKaiGai Kohei }
372aa98d7cfSKaiGai Kohei 
373aa98d7cfSKaiGai Kohei static int jffs2_scan_xref_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
374aa98d7cfSKaiGai Kohei 				struct jffs2_raw_xref *rr, uint32_t ofs,
375aa98d7cfSKaiGai Kohei 				struct jffs2_summary *s)
376aa98d7cfSKaiGai Kohei {
377aa98d7cfSKaiGai Kohei 	struct jffs2_xattr_ref *ref;
378aa98d7cfSKaiGai Kohei 	uint32_t crc;
37968270995SDavid Woodhouse 	int err;
380aa98d7cfSKaiGai Kohei 
381aa98d7cfSKaiGai Kohei 	crc = crc32(0, rr, sizeof(*rr) - 4);
382aa98d7cfSKaiGai Kohei 	if (crc != je32_to_cpu(rr->node_crc)) {
383aa98d7cfSKaiGai Kohei 		JFFS2_WARNING("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
384aa98d7cfSKaiGai Kohei 			      ofs, je32_to_cpu(rr->node_crc), crc);
38568270995SDavid Woodhouse 		if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(rr->totlen)))))
38668270995SDavid Woodhouse 			return err;
387aa98d7cfSKaiGai Kohei 		return 0;
388aa98d7cfSKaiGai Kohei 	}
389aa98d7cfSKaiGai Kohei 
390aa98d7cfSKaiGai Kohei 	if (PAD(sizeof(struct jffs2_raw_xref)) != je32_to_cpu(rr->totlen)) {
39189291a9dSDavid Woodhouse 		JFFS2_WARNING("node length mismatch at %#08x, read=%u, calc=%zd\n",
392aa98d7cfSKaiGai Kohei 			      ofs, je32_to_cpu(rr->totlen),
393aa98d7cfSKaiGai Kohei 			      PAD(sizeof(struct jffs2_raw_xref)));
39468270995SDavid Woodhouse 		if ((err = jffs2_scan_dirty_space(c, jeb, je32_to_cpu(rr->totlen))))
39568270995SDavid Woodhouse 			return err;
396aa98d7cfSKaiGai Kohei 		return 0;
397aa98d7cfSKaiGai Kohei 	}
398aa98d7cfSKaiGai Kohei 
399aa98d7cfSKaiGai Kohei 	ref = jffs2_alloc_xattr_ref();
400aa98d7cfSKaiGai Kohei 	if (!ref)
401aa98d7cfSKaiGai Kohei 		return -ENOMEM;
402aa98d7cfSKaiGai Kohei 
403aa98d7cfSKaiGai Kohei 	/* BEFORE jffs2_build_xattr_subsystem() called,
404c9f700f8SKaiGai Kohei 	 * and AFTER xattr_ref is marked as a dead xref,
405aa98d7cfSKaiGai Kohei 	 * ref->xid is used to store 32bit xid, xd is not used
406aa98d7cfSKaiGai Kohei 	 * ref->ino is used to store 32bit inode-number, ic is not used
407aa98d7cfSKaiGai Kohei 	 * Thoes variables are declared as union, thus using those
4088f2b6f49SKaiGai Kohei 	 * are exclusive. In a similar way, ref->next is temporarily
409aa98d7cfSKaiGai Kohei 	 * used to chain all xattr_ref object. It's re-chained to
410aa98d7cfSKaiGai Kohei 	 * jffs2_inode_cache in jffs2_build_xattr_subsystem() correctly.
411aa98d7cfSKaiGai Kohei 	 */
412aa98d7cfSKaiGai Kohei 	ref->ino = je32_to_cpu(rr->ino);
413aa98d7cfSKaiGai Kohei 	ref->xid = je32_to_cpu(rr->xid);
414c9f700f8SKaiGai Kohei 	ref->xseqno = je32_to_cpu(rr->xseqno);
415c9f700f8SKaiGai Kohei 	if (ref->xseqno > c->highest_xseqno)
416c9f700f8SKaiGai Kohei 		c->highest_xseqno = (ref->xseqno & ~XREF_DELETE_MARKER);
4178f2b6f49SKaiGai Kohei 	ref->next = c->xref_temp;
4188f2b6f49SKaiGai Kohei 	c->xref_temp = ref;
419aa98d7cfSKaiGai Kohei 
420c9f700f8SKaiGai Kohei 	jffs2_link_node_ref(c, jeb, ofs | REF_PRISTINE, PAD(je32_to_cpu(rr->totlen)), (void *)ref);
421f1f9671bSDavid Woodhouse 
422aa98d7cfSKaiGai Kohei 	if (jffs2_sum_active())
423aa98d7cfSKaiGai Kohei 		jffs2_sum_add_xref_mem(s, rr, ofs - jeb->offset);
424aa98d7cfSKaiGai Kohei 	dbg_xattr("scan xref at %#08x (xid=%u, ino=%u)\n",
425aa98d7cfSKaiGai Kohei 		  ofs, ref->xid, ref->ino);
426aa98d7cfSKaiGai Kohei 	return 0;
427aa98d7cfSKaiGai Kohei }
428aa98d7cfSKaiGai Kohei #endif
429aa98d7cfSKaiGai Kohei 
4309641b784SDavid Woodhouse /* Called with 'buf_size == 0' if buf is in fact a pointer _directly_ into
4319641b784SDavid Woodhouse    the flash, XIP-style */
4321da177e4SLinus Torvalds static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
433e631ddbaSFerenc Havasi 				  unsigned char *buf, uint32_t buf_size, struct jffs2_summary *s) {
4341da177e4SLinus Torvalds 	struct jffs2_unknown_node *node;
4351da177e4SLinus Torvalds 	struct jffs2_unknown_node crcnode;
4361da177e4SLinus Torvalds 	uint32_t ofs, prevofs;
4371da177e4SLinus Torvalds 	uint32_t hdr_crc, buf_ofs, buf_len;
4381da177e4SLinus Torvalds 	int err;
4391da177e4SLinus Torvalds 	int noise = 0;
440e631ddbaSFerenc Havasi 
441e631ddbaSFerenc Havasi 
4422f82ce1eSAndrew Victor #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
4431da177e4SLinus Torvalds 	int cleanmarkerfound = 0;
4441da177e4SLinus Torvalds #endif
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds 	ofs = jeb->offset;
4471da177e4SLinus Torvalds 	prevofs = jeb->offset - 1;
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 	D1(printk(KERN_DEBUG "jffs2_scan_eraseblock(): Scanning block at 0x%x\n", ofs));
4501da177e4SLinus Torvalds 
4512f82ce1eSAndrew Victor #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
4521da177e4SLinus Torvalds 	if (jffs2_cleanmarker_oob(c)) {
453a7a6ace1SArtem Bityutskiy 		int ret;
454a7a6ace1SArtem Bityutskiy 
455a7a6ace1SArtem Bityutskiy 		if (c->mtd->block_isbad(c->mtd, jeb->offset))
456a7a6ace1SArtem Bityutskiy 			return BLK_STATE_BADBLOCK;
457a7a6ace1SArtem Bityutskiy 
458a7a6ace1SArtem Bityutskiy 		ret = jffs2_check_nand_cleanmarker(c, jeb);
4591da177e4SLinus Torvalds 		D2(printk(KERN_NOTICE "jffs_check_nand_cleanmarker returned %d\n",ret));
460a7a6ace1SArtem Bityutskiy 
4611da177e4SLinus Torvalds 		/* Even if it's not found, we still scan to see
4621da177e4SLinus Torvalds 		   if the block is empty. We use this information
4631da177e4SLinus Torvalds 		   to decide whether to erase it or not. */
4641da177e4SLinus Torvalds 		switch (ret) {
4651da177e4SLinus Torvalds 		case 0:		cleanmarkerfound = 1; break;
4661da177e4SLinus Torvalds 		case 1: 	break;
4671da177e4SLinus Torvalds 		default: 	return ret;
4681da177e4SLinus Torvalds 		}
4691da177e4SLinus Torvalds 	}
4701da177e4SLinus Torvalds #endif
471e631ddbaSFerenc Havasi 
472e631ddbaSFerenc Havasi 	if (jffs2_sum_active()) {
4739641b784SDavid Woodhouse 		struct jffs2_sum_marker *sm;
4749641b784SDavid Woodhouse 		void *sumptr = NULL;
4759641b784SDavid Woodhouse 		uint32_t sumlen;
476e631ddbaSFerenc Havasi 
4779641b784SDavid Woodhouse 		if (!buf_size) {
4789641b784SDavid Woodhouse 			/* XIP case. Just look, point at the summary if it's there */
47913ba42dfSDavid Woodhouse 			sm = (void *)buf + c->sector_size - sizeof(*sm);
480e631ddbaSFerenc Havasi 			if (je32_to_cpu(sm->magic) == JFFS2_SUM_MAGIC) {
4819641b784SDavid Woodhouse 				sumptr = buf + je32_to_cpu(sm->offset);
4829641b784SDavid Woodhouse 				sumlen = c->sector_size - je32_to_cpu(sm->offset);
4839641b784SDavid Woodhouse 			}
4849641b784SDavid Woodhouse 		} else {
4859641b784SDavid Woodhouse 			/* If NAND flash, read a whole page of it. Else just the end */
4869641b784SDavid Woodhouse 			if (c->wbuf_pagesize)
4879641b784SDavid Woodhouse 				buf_len = c->wbuf_pagesize;
4889641b784SDavid Woodhouse 			else
4899641b784SDavid Woodhouse 				buf_len = sizeof(*sm);
4909641b784SDavid Woodhouse 
4919641b784SDavid Woodhouse 			/* Read as much as we want into the _end_ of the preallocated buffer */
4929641b784SDavid Woodhouse 			err = jffs2_fill_scan_buf(c, buf + buf_size - buf_len,
4939641b784SDavid Woodhouse 						  jeb->offset + c->sector_size - buf_len,
4949641b784SDavid Woodhouse 						  buf_len);
4959641b784SDavid Woodhouse 			if (err)
4969641b784SDavid Woodhouse 				return err;
4979641b784SDavid Woodhouse 
4989641b784SDavid Woodhouse 			sm = (void *)buf + buf_size - sizeof(*sm);
4999641b784SDavid Woodhouse 			if (je32_to_cpu(sm->magic) == JFFS2_SUM_MAGIC) {
5009641b784SDavid Woodhouse 				sumlen = c->sector_size - je32_to_cpu(sm->offset);
5019641b784SDavid Woodhouse 				sumptr = buf + buf_size - sumlen;
5029641b784SDavid Woodhouse 
5039641b784SDavid Woodhouse 				/* Now, make sure the summary itself is available */
5049641b784SDavid Woodhouse 				if (sumlen > buf_size) {
5059641b784SDavid Woodhouse 					/* Need to kmalloc for this. */
5069641b784SDavid Woodhouse 					sumptr = kmalloc(sumlen, GFP_KERNEL);
5079641b784SDavid Woodhouse 					if (!sumptr)
5089641b784SDavid Woodhouse 						return -ENOMEM;
5099641b784SDavid Woodhouse 					memcpy(sumptr + sumlen - buf_len, buf + buf_size - buf_len, buf_len);
5109641b784SDavid Woodhouse 				}
5119641b784SDavid Woodhouse 				if (buf_len < sumlen) {
5129641b784SDavid Woodhouse 					/* Need to read more so that the entire summary node is present */
5139641b784SDavid Woodhouse 					err = jffs2_fill_scan_buf(c, sumptr,
5149641b784SDavid Woodhouse 								  jeb->offset + c->sector_size - sumlen,
5159641b784SDavid Woodhouse 								  sumlen - buf_len);
5169641b784SDavid Woodhouse 					if (err)
517e631ddbaSFerenc Havasi 						return err;
518e631ddbaSFerenc Havasi 				}
519e631ddbaSFerenc Havasi 			}
520e631ddbaSFerenc Havasi 
5219641b784SDavid Woodhouse 		}
5229641b784SDavid Woodhouse 
5239641b784SDavid Woodhouse 		if (sumptr) {
5249641b784SDavid Woodhouse 			err = jffs2_sum_scan_sumnode(c, jeb, sumptr, sumlen, &pseudo_random);
5253560160aSDavid Woodhouse 
5269641b784SDavid Woodhouse 			if (buf_size && sumlen > buf_size)
5279641b784SDavid Woodhouse 				kfree(sumptr);
5283560160aSDavid Woodhouse 			/* If it returns with a real error, bail.
5293560160aSDavid Woodhouse 			   If it returns positive, that's a block classification
5303560160aSDavid Woodhouse 			   (i.e. BLK_STATE_xxx) so return that too.
5313560160aSDavid Woodhouse 			   If it returns zero, fall through to full scan. */
5323560160aSDavid Woodhouse 			if (err)
5333560160aSDavid Woodhouse 				return err;
5349641b784SDavid Woodhouse 		}
535e631ddbaSFerenc Havasi 	}
536e631ddbaSFerenc Havasi 
5371da177e4SLinus Torvalds 	buf_ofs = jeb->offset;
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds 	if (!buf_size) {
5409641b784SDavid Woodhouse 		/* This is the XIP case -- we're reading _directly_ from the flash chip */
5411da177e4SLinus Torvalds 		buf_len = c->sector_size;
5421da177e4SLinus Torvalds 	} else {
5433be36675SAndrew Victor 		buf_len = EMPTY_SCAN_SIZE(c->sector_size);
5441da177e4SLinus Torvalds 		err = jffs2_fill_scan_buf(c, buf, buf_ofs, buf_len);
5451da177e4SLinus Torvalds 		if (err)
5461da177e4SLinus Torvalds 			return err;
5471da177e4SLinus Torvalds 	}
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds 	/* We temporarily use 'ofs' as a pointer into the buffer/jeb */
5501da177e4SLinus Torvalds 	ofs = 0;
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds 	/* Scan only 4KiB of 0xFF before declaring it's empty */
5533be36675SAndrew Victor 	while(ofs < EMPTY_SCAN_SIZE(c->sector_size) && *(uint32_t *)(&buf[ofs]) == 0xFFFFFFFF)
5541da177e4SLinus Torvalds 		ofs += 4;
5551da177e4SLinus Torvalds 
5563be36675SAndrew Victor 	if (ofs == EMPTY_SCAN_SIZE(c->sector_size)) {
5572f82ce1eSAndrew Victor #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
5581da177e4SLinus Torvalds 		if (jffs2_cleanmarker_oob(c)) {
5591da177e4SLinus Torvalds 			/* scan oob, take care of cleanmarker */
5601da177e4SLinus Torvalds 			int ret = jffs2_check_oob_empty(c, jeb, cleanmarkerfound);
5611da177e4SLinus Torvalds 			D2(printk(KERN_NOTICE "jffs2_check_oob_empty returned %d\n",ret));
5621da177e4SLinus Torvalds 			switch (ret) {
5631da177e4SLinus Torvalds 			case 0:		return cleanmarkerfound ? BLK_STATE_CLEANMARKER : BLK_STATE_ALLFF;
5641da177e4SLinus Torvalds 			case 1: 	return BLK_STATE_ALLDIRTY;
5651da177e4SLinus Torvalds 			default: 	return ret;
5661da177e4SLinus Torvalds 			}
5671da177e4SLinus Torvalds 		}
5681da177e4SLinus Torvalds #endif
5691da177e4SLinus Torvalds 		D1(printk(KERN_DEBUG "Block at 0x%08x is empty (erased)\n", jeb->offset));
5708f15fd55SAndrew Victor 		if (c->cleanmarker_size == 0)
5718f15fd55SAndrew Victor 			return BLK_STATE_CLEANMARKER;	/* don't bother with re-erase */
5728f15fd55SAndrew Victor 		else
5731da177e4SLinus Torvalds 			return BLK_STATE_ALLFF;	/* OK to erase if all blocks are like this */
5741da177e4SLinus Torvalds 	}
5751da177e4SLinus Torvalds 	if (ofs) {
5761da177e4SLinus Torvalds 		D1(printk(KERN_DEBUG "Free space at %08x ends at %08x\n", jeb->offset,
5771da177e4SLinus Torvalds 			  jeb->offset + ofs));
578a6a8bef7SDavid Woodhouse 		if ((err = jffs2_prealloc_raw_node_refs(c, jeb, 1)))
579a6a8bef7SDavid Woodhouse 			return err;
58068270995SDavid Woodhouse 		if ((err = jffs2_scan_dirty_space(c, jeb, ofs)))
58168270995SDavid Woodhouse 			return err;
5821da177e4SLinus Torvalds 	}
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds 	/* Now ofs is a complete physical flash offset as it always was... */
5851da177e4SLinus Torvalds 	ofs += jeb->offset;
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds 	noise = 10;
5881da177e4SLinus Torvalds 
589733802d9SArtem B. Bityutskiy 	dbg_summary("no summary found in jeb 0x%08x. Apply original scan.\n",jeb->offset);
590e631ddbaSFerenc Havasi 
5911da177e4SLinus Torvalds scan_more:
5921da177e4SLinus Torvalds 	while(ofs < jeb->offset + c->sector_size) {
5931da177e4SLinus Torvalds 
594e0c8e42fSArtem B. Bityutskiy 		jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
5951da177e4SLinus Torvalds 
5962f785402SDavid Woodhouse 		/* Make sure there are node refs available for use */
597046b8b98SDavid Woodhouse 		err = jffs2_prealloc_raw_node_refs(c, jeb, 2);
5982f785402SDavid Woodhouse 		if (err)
5992f785402SDavid Woodhouse 			return err;
6002f785402SDavid Woodhouse 
6011da177e4SLinus Torvalds 		cond_resched();
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 		if (ofs & 3) {
6041da177e4SLinus Torvalds 			printk(KERN_WARNING "Eep. ofs 0x%08x not word-aligned!\n", ofs);
6051da177e4SLinus Torvalds 			ofs = PAD(ofs);
6061da177e4SLinus Torvalds 			continue;
6071da177e4SLinus Torvalds 		}
6081da177e4SLinus Torvalds 		if (ofs == prevofs) {
6091da177e4SLinus Torvalds 			printk(KERN_WARNING "ofs 0x%08x has already been seen. Skipping\n", ofs);
61068270995SDavid Woodhouse 			if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
61168270995SDavid Woodhouse 				return err;
6121da177e4SLinus Torvalds 			ofs += 4;
6131da177e4SLinus Torvalds 			continue;
6141da177e4SLinus Torvalds 		}
6151da177e4SLinus Torvalds 		prevofs = ofs;
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds 		if (jeb->offset + c->sector_size < ofs + sizeof(*node)) {
6181da177e4SLinus Torvalds 			D1(printk(KERN_DEBUG "Fewer than %zd bytes left to end of block. (%x+%x<%x+%zx) Not reading\n", sizeof(struct jffs2_unknown_node),
6191da177e4SLinus Torvalds 				  jeb->offset, c->sector_size, ofs, sizeof(*node)));
62068270995SDavid Woodhouse 			if ((err = jffs2_scan_dirty_space(c, jeb, (jeb->offset + c->sector_size)-ofs)))
62168270995SDavid Woodhouse 				return err;
6221da177e4SLinus Torvalds 			break;
6231da177e4SLinus Torvalds 		}
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds 		if (buf_ofs + buf_len < ofs + sizeof(*node)) {
6261da177e4SLinus Torvalds 			buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
6271da177e4SLinus Torvalds 			D1(printk(KERN_DEBUG "Fewer than %zd bytes (node header) left to end of buf. Reading 0x%x at 0x%08x\n",
6281da177e4SLinus Torvalds 				  sizeof(struct jffs2_unknown_node), buf_len, ofs));
6291da177e4SLinus Torvalds 			err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
6301da177e4SLinus Torvalds 			if (err)
6311da177e4SLinus Torvalds 				return err;
6321da177e4SLinus Torvalds 			buf_ofs = ofs;
6331da177e4SLinus Torvalds 		}
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds 		node = (struct jffs2_unknown_node *)&buf[ofs-buf_ofs];
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds 		if (*(uint32_t *)(&buf[ofs-buf_ofs]) == 0xffffffff) {
6381da177e4SLinus Torvalds 			uint32_t inbuf_ofs;
639c2aecda7SJoakim Tjernlund 			uint32_t empty_start, scan_end;
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds 			empty_start = ofs;
6421da177e4SLinus Torvalds 			ofs += 4;
643c2aecda7SJoakim Tjernlund 			scan_end = min_t(uint32_t, EMPTY_SCAN_SIZE(c->sector_size)/8, buf_len);
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds 			D1(printk(KERN_DEBUG "Found empty flash at 0x%08x\n", ofs));
6461da177e4SLinus Torvalds 		more_empty:
6471da177e4SLinus Torvalds 			inbuf_ofs = ofs - buf_ofs;
648c2aecda7SJoakim Tjernlund 			while (inbuf_ofs < scan_end) {
649c2aecda7SJoakim Tjernlund 				if (unlikely(*(uint32_t *)(&buf[inbuf_ofs]) != 0xffffffff)) {
6501da177e4SLinus Torvalds 					printk(KERN_WARNING "Empty flash at 0x%08x ends at 0x%08x\n",
6511da177e4SLinus Torvalds 					       empty_start, ofs);
65268270995SDavid Woodhouse 					if ((err = jffs2_scan_dirty_space(c, jeb, ofs-empty_start)))
65368270995SDavid Woodhouse 						return err;
6541da177e4SLinus Torvalds 					goto scan_more;
6551da177e4SLinus Torvalds 				}
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 				inbuf_ofs+=4;
6581da177e4SLinus Torvalds 				ofs += 4;
6591da177e4SLinus Torvalds 			}
6601da177e4SLinus Torvalds 			/* Ran off end. */
6611da177e4SLinus Torvalds 			D1(printk(KERN_DEBUG "Empty flash to end of buffer at 0x%08x\n", ofs));
6621da177e4SLinus Torvalds 
6631da177e4SLinus Torvalds 			/* If we're only checking the beginning of a block with a cleanmarker,
6641da177e4SLinus Torvalds 			   bail now */
6651da177e4SLinus Torvalds 			if (buf_ofs == jeb->offset && jeb->used_size == PAD(c->cleanmarker_size) &&
66699988f7bSDavid Woodhouse 			    c->cleanmarker_size && !jeb->dirty_size && !ref_next(jeb->first_node)) {
6673be36675SAndrew Victor 				D1(printk(KERN_DEBUG "%d bytes at start of block seems clean... assuming all clean\n", EMPTY_SCAN_SIZE(c->sector_size)));
6681da177e4SLinus Torvalds 				return BLK_STATE_CLEANMARKER;
6691da177e4SLinus Torvalds 			}
670c2aecda7SJoakim Tjernlund 			if (!buf_size && (scan_end != buf_len)) {/* XIP/point case */
671c2aecda7SJoakim Tjernlund 				scan_end = buf_len;
672c2aecda7SJoakim Tjernlund 				goto more_empty;
673c2aecda7SJoakim Tjernlund 			}
6741da177e4SLinus Torvalds 
6751da177e4SLinus Torvalds 			/* See how much more there is to read in this eraseblock... */
6761da177e4SLinus Torvalds 			buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
6771da177e4SLinus Torvalds 			if (!buf_len) {
6781da177e4SLinus Torvalds 				/* No more to read. Break out of main loop without marking
6791da177e4SLinus Torvalds 				   this range of empty space as dirty (because it's not) */
6801da177e4SLinus Torvalds 				D1(printk(KERN_DEBUG "Empty flash at %08x runs to end of block. Treating as free_space\n",
6811da177e4SLinus Torvalds 					  empty_start));
6821da177e4SLinus Torvalds 				break;
6831da177e4SLinus Torvalds 			}
684c2aecda7SJoakim Tjernlund 			/* point never reaches here */
685c2aecda7SJoakim Tjernlund 			scan_end = buf_len;
6861da177e4SLinus Torvalds 			D1(printk(KERN_DEBUG "Reading another 0x%x at 0x%08x\n", buf_len, ofs));
6871da177e4SLinus Torvalds 			err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
6881da177e4SLinus Torvalds 			if (err)
6891da177e4SLinus Torvalds 				return err;
6901da177e4SLinus Torvalds 			buf_ofs = ofs;
6911da177e4SLinus Torvalds 			goto more_empty;
6921da177e4SLinus Torvalds 		}
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds 		if (ofs == jeb->offset && je16_to_cpu(node->magic) == KSAMTIB_CIGAM_2SFFJ) {
6951da177e4SLinus Torvalds 			printk(KERN_WARNING "Magic bitmask is backwards at offset 0x%08x. Wrong endian filesystem?\n", ofs);
69668270995SDavid Woodhouse 			if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
69768270995SDavid Woodhouse 				return err;
6981da177e4SLinus Torvalds 			ofs += 4;
6991da177e4SLinus Torvalds 			continue;
7001da177e4SLinus Torvalds 		}
7011da177e4SLinus Torvalds 		if (je16_to_cpu(node->magic) == JFFS2_DIRTY_BITMASK) {
7021da177e4SLinus Torvalds 			D1(printk(KERN_DEBUG "Dirty bitmask at 0x%08x\n", ofs));
70368270995SDavid Woodhouse 			if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
70468270995SDavid Woodhouse 				return err;
7051da177e4SLinus Torvalds 			ofs += 4;
7061da177e4SLinus Torvalds 			continue;
7071da177e4SLinus Torvalds 		}
7081da177e4SLinus Torvalds 		if (je16_to_cpu(node->magic) == JFFS2_OLD_MAGIC_BITMASK) {
7091da177e4SLinus Torvalds 			printk(KERN_WARNING "Old JFFS2 bitmask found at 0x%08x\n", ofs);
7101da177e4SLinus Torvalds 			printk(KERN_WARNING "You cannot use older JFFS2 filesystems with newer kernels\n");
71168270995SDavid Woodhouse 			if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
71268270995SDavid Woodhouse 				return err;
7131da177e4SLinus Torvalds 			ofs += 4;
7141da177e4SLinus Torvalds 			continue;
7151da177e4SLinus Torvalds 		}
7161da177e4SLinus Torvalds 		if (je16_to_cpu(node->magic) != JFFS2_MAGIC_BITMASK) {
7171da177e4SLinus Torvalds 			/* OK. We're out of possibilities. Whinge and move on */
7181da177e4SLinus Torvalds 			noisy_printk(&noise, "jffs2_scan_eraseblock(): Magic bitmask 0x%04x not found at 0x%08x: 0x%04x instead\n",
7191da177e4SLinus Torvalds 				     JFFS2_MAGIC_BITMASK, ofs,
7201da177e4SLinus Torvalds 				     je16_to_cpu(node->magic));
72168270995SDavid Woodhouse 			if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
72268270995SDavid Woodhouse 				return err;
7231da177e4SLinus Torvalds 			ofs += 4;
7241da177e4SLinus Torvalds 			continue;
7251da177e4SLinus Torvalds 		}
7261da177e4SLinus Torvalds 		/* We seem to have a node of sorts. Check the CRC */
7271da177e4SLinus Torvalds 		crcnode.magic = node->magic;
7281da177e4SLinus Torvalds 		crcnode.nodetype = cpu_to_je16( je16_to_cpu(node->nodetype) | JFFS2_NODE_ACCURATE);
7291da177e4SLinus Torvalds 		crcnode.totlen = node->totlen;
7301da177e4SLinus Torvalds 		hdr_crc = crc32(0, &crcnode, sizeof(crcnode)-4);
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds 		if (hdr_crc != je32_to_cpu(node->hdr_crc)) {
7331da177e4SLinus Torvalds 			noisy_printk(&noise, "jffs2_scan_eraseblock(): Node at 0x%08x {0x%04x, 0x%04x, 0x%08x) has invalid CRC 0x%08x (calculated 0x%08x)\n",
7341da177e4SLinus Torvalds 				     ofs, je16_to_cpu(node->magic),
7351da177e4SLinus Torvalds 				     je16_to_cpu(node->nodetype),
7361da177e4SLinus Torvalds 				     je32_to_cpu(node->totlen),
7371da177e4SLinus Torvalds 				     je32_to_cpu(node->hdr_crc),
7381da177e4SLinus Torvalds 				     hdr_crc);
73968270995SDavid Woodhouse 			if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
74068270995SDavid Woodhouse 				return err;
7411da177e4SLinus Torvalds 			ofs += 4;
7421da177e4SLinus Torvalds 			continue;
7431da177e4SLinus Torvalds 		}
7441da177e4SLinus Torvalds 
7450dec4c8bSJoakim Tjernlund 		if (ofs + je32_to_cpu(node->totlen) > jeb->offset + c->sector_size) {
7461da177e4SLinus Torvalds 			/* Eep. Node goes over the end of the erase block. */
7471da177e4SLinus Torvalds 			printk(KERN_WARNING "Node at 0x%08x with length 0x%08x would run over the end of the erase block\n",
7481da177e4SLinus Torvalds 			       ofs, je32_to_cpu(node->totlen));
7491da177e4SLinus Torvalds 			printk(KERN_WARNING "Perhaps the file system was created with the wrong erase size?\n");
75068270995SDavid Woodhouse 			if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
75168270995SDavid Woodhouse 				return err;
7521da177e4SLinus Torvalds 			ofs += 4;
7531da177e4SLinus Torvalds 			continue;
7541da177e4SLinus Torvalds 		}
7551da177e4SLinus Torvalds 
7561da177e4SLinus Torvalds 		if (!(je16_to_cpu(node->nodetype) & JFFS2_NODE_ACCURATE)) {
7571da177e4SLinus Torvalds 			/* Wheee. This is an obsoleted node */
7581da177e4SLinus Torvalds 			D2(printk(KERN_DEBUG "Node at 0x%08x is obsolete. Skipping\n", ofs));
75968270995SDavid Woodhouse 			if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen)))))
76068270995SDavid Woodhouse 				return err;
7611da177e4SLinus Torvalds 			ofs += PAD(je32_to_cpu(node->totlen));
7621da177e4SLinus Torvalds 			continue;
7631da177e4SLinus Torvalds 		}
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 		switch(je16_to_cpu(node->nodetype)) {
7661da177e4SLinus Torvalds 		case JFFS2_NODETYPE_INODE:
7671da177e4SLinus Torvalds 			if (buf_ofs + buf_len < ofs + sizeof(struct jffs2_raw_inode)) {
7681da177e4SLinus Torvalds 				buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
7691da177e4SLinus Torvalds 				D1(printk(KERN_DEBUG "Fewer than %zd bytes (inode node) left to end of buf. Reading 0x%x at 0x%08x\n",
7701da177e4SLinus Torvalds 					  sizeof(struct jffs2_raw_inode), buf_len, ofs));
7711da177e4SLinus Torvalds 				err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
7721da177e4SLinus Torvalds 				if (err)
7731da177e4SLinus Torvalds 					return err;
7741da177e4SLinus Torvalds 				buf_ofs = ofs;
7751da177e4SLinus Torvalds 				node = (void *)buf;
7761da177e4SLinus Torvalds 			}
777e631ddbaSFerenc Havasi 			err = jffs2_scan_inode_node(c, jeb, (void *)node, ofs, s);
7781da177e4SLinus Torvalds 			if (err) return err;
7791da177e4SLinus Torvalds 			ofs += PAD(je32_to_cpu(node->totlen));
7801da177e4SLinus Torvalds 			break;
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds 		case JFFS2_NODETYPE_DIRENT:
7831da177e4SLinus Torvalds 			if (buf_ofs + buf_len < ofs + je32_to_cpu(node->totlen)) {
7841da177e4SLinus Torvalds 				buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
7851da177e4SLinus Torvalds 				D1(printk(KERN_DEBUG "Fewer than %d bytes (dirent node) left to end of buf. Reading 0x%x at 0x%08x\n",
7861da177e4SLinus Torvalds 					  je32_to_cpu(node->totlen), buf_len, ofs));
7871da177e4SLinus Torvalds 				err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
7881da177e4SLinus Torvalds 				if (err)
7891da177e4SLinus Torvalds 					return err;
7901da177e4SLinus Torvalds 				buf_ofs = ofs;
7911da177e4SLinus Torvalds 				node = (void *)buf;
7921da177e4SLinus Torvalds 			}
793e631ddbaSFerenc Havasi 			err = jffs2_scan_dirent_node(c, jeb, (void *)node, ofs, s);
7941da177e4SLinus Torvalds 			if (err) return err;
7951da177e4SLinus Torvalds 			ofs += PAD(je32_to_cpu(node->totlen));
7961da177e4SLinus Torvalds 			break;
7971da177e4SLinus Torvalds 
798aa98d7cfSKaiGai Kohei #ifdef CONFIG_JFFS2_FS_XATTR
799aa98d7cfSKaiGai Kohei 		case JFFS2_NODETYPE_XATTR:
800aa98d7cfSKaiGai Kohei 			if (buf_ofs + buf_len < ofs + je32_to_cpu(node->totlen)) {
801aa98d7cfSKaiGai Kohei 				buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
802aa98d7cfSKaiGai Kohei 				D1(printk(KERN_DEBUG "Fewer than %d bytes (xattr node)"
803aa98d7cfSKaiGai Kohei 					  " left to end of buf. Reading 0x%x at 0x%08x\n",
804aa98d7cfSKaiGai Kohei 					  je32_to_cpu(node->totlen), buf_len, ofs));
805aa98d7cfSKaiGai Kohei 				err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
806aa98d7cfSKaiGai Kohei 				if (err)
807aa98d7cfSKaiGai Kohei 					return err;
808aa98d7cfSKaiGai Kohei 				buf_ofs = ofs;
809aa98d7cfSKaiGai Kohei 				node = (void *)buf;
810aa98d7cfSKaiGai Kohei 			}
811aa98d7cfSKaiGai Kohei 			err = jffs2_scan_xattr_node(c, jeb, (void *)node, ofs, s);
812aa98d7cfSKaiGai Kohei 			if (err)
813aa98d7cfSKaiGai Kohei 				return err;
814aa98d7cfSKaiGai Kohei 			ofs += PAD(je32_to_cpu(node->totlen));
815aa98d7cfSKaiGai Kohei 			break;
816aa98d7cfSKaiGai Kohei 		case JFFS2_NODETYPE_XREF:
817aa98d7cfSKaiGai Kohei 			if (buf_ofs + buf_len < ofs + je32_to_cpu(node->totlen)) {
818aa98d7cfSKaiGai Kohei 				buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
819aa98d7cfSKaiGai Kohei 				D1(printk(KERN_DEBUG "Fewer than %d bytes (xref node)"
820aa98d7cfSKaiGai Kohei 					  " left to end of buf. Reading 0x%x at 0x%08x\n",
821aa98d7cfSKaiGai Kohei 					  je32_to_cpu(node->totlen), buf_len, ofs));
822aa98d7cfSKaiGai Kohei 				err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
823aa98d7cfSKaiGai Kohei 				if (err)
824aa98d7cfSKaiGai Kohei 					return err;
825aa98d7cfSKaiGai Kohei 				buf_ofs = ofs;
826aa98d7cfSKaiGai Kohei 				node = (void *)buf;
827aa98d7cfSKaiGai Kohei 			}
828aa98d7cfSKaiGai Kohei 			err = jffs2_scan_xref_node(c, jeb, (void *)node, ofs, s);
829aa98d7cfSKaiGai Kohei 			if (err)
830aa98d7cfSKaiGai Kohei 				return err;
831aa98d7cfSKaiGai Kohei 			ofs += PAD(je32_to_cpu(node->totlen));
832aa98d7cfSKaiGai Kohei 			break;
833aa98d7cfSKaiGai Kohei #endif	/* CONFIG_JFFS2_FS_XATTR */
834aa98d7cfSKaiGai Kohei 
8351da177e4SLinus Torvalds 		case JFFS2_NODETYPE_CLEANMARKER:
8361da177e4SLinus Torvalds 			D1(printk(KERN_DEBUG "CLEANMARKER node found at 0x%08x\n", ofs));
8371da177e4SLinus Torvalds 			if (je32_to_cpu(node->totlen) != c->cleanmarker_size) {
8381da177e4SLinus Torvalds 				printk(KERN_NOTICE "CLEANMARKER node found at 0x%08x has totlen 0x%x != normal 0x%x\n",
8391da177e4SLinus Torvalds 				       ofs, je32_to_cpu(node->totlen), c->cleanmarker_size);
84068270995SDavid Woodhouse 				if ((err = jffs2_scan_dirty_space(c, jeb, PAD(sizeof(struct jffs2_unknown_node)))))
84168270995SDavid Woodhouse 					return err;
8421da177e4SLinus Torvalds 				ofs += PAD(sizeof(struct jffs2_unknown_node));
8431da177e4SLinus Torvalds 			} else if (jeb->first_node) {
8441da177e4SLinus Torvalds 				printk(KERN_NOTICE "CLEANMARKER node found at 0x%08x, not first node in block (0x%08x)\n", ofs, jeb->offset);
84568270995SDavid Woodhouse 				if ((err = jffs2_scan_dirty_space(c, jeb, PAD(sizeof(struct jffs2_unknown_node)))))
84668270995SDavid Woodhouse 					return err;
8471da177e4SLinus Torvalds 				ofs += PAD(sizeof(struct jffs2_unknown_node));
8481da177e4SLinus Torvalds 			} else {
8492f785402SDavid Woodhouse 				jffs2_link_node_ref(c, jeb, ofs | REF_NORMAL, c->cleanmarker_size, NULL);
850f1f9671bSDavid Woodhouse 
8511da177e4SLinus Torvalds 				ofs += PAD(c->cleanmarker_size);
8521da177e4SLinus Torvalds 			}
8531da177e4SLinus Torvalds 			break;
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds 		case JFFS2_NODETYPE_PADDING:
856e631ddbaSFerenc Havasi 			if (jffs2_sum_active())
857e631ddbaSFerenc Havasi 				jffs2_sum_add_padding_mem(s, je32_to_cpu(node->totlen));
85868270995SDavid Woodhouse 			if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen)))))
85968270995SDavid Woodhouse 				return err;
8601da177e4SLinus Torvalds 			ofs += PAD(je32_to_cpu(node->totlen));
8611da177e4SLinus Torvalds 			break;
8621da177e4SLinus Torvalds 
8631da177e4SLinus Torvalds 		default:
8641da177e4SLinus Torvalds 			switch (je16_to_cpu(node->nodetype) & JFFS2_COMPAT_MASK) {
8651da177e4SLinus Torvalds 			case JFFS2_FEATURE_ROCOMPAT:
8661da177e4SLinus Torvalds 				printk(KERN_NOTICE "Read-only compatible feature node (0x%04x) found at offset 0x%08x\n", je16_to_cpu(node->nodetype), ofs);
8671da177e4SLinus Torvalds 				c->flags |= JFFS2_SB_FLAG_RO;
8681da177e4SLinus Torvalds 				if (!(jffs2_is_readonly(c)))
8691da177e4SLinus Torvalds 					return -EROFS;
87068270995SDavid Woodhouse 				if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen)))))
87168270995SDavid Woodhouse 					return err;
8721da177e4SLinus Torvalds 				ofs += PAD(je32_to_cpu(node->totlen));
8731da177e4SLinus Torvalds 				break;
8741da177e4SLinus Torvalds 
8751da177e4SLinus Torvalds 			case JFFS2_FEATURE_INCOMPAT:
8761da177e4SLinus Torvalds 				printk(KERN_NOTICE "Incompatible feature node (0x%04x) found at offset 0x%08x\n", je16_to_cpu(node->nodetype), ofs);
8771da177e4SLinus Torvalds 				return -EINVAL;
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds 			case JFFS2_FEATURE_RWCOMPAT_DELETE:
8801da177e4SLinus Torvalds 				D1(printk(KERN_NOTICE "Unknown but compatible feature node (0x%04x) found at offset 0x%08x\n", je16_to_cpu(node->nodetype), ofs));
88168270995SDavid Woodhouse 				if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen)))))
88268270995SDavid Woodhouse 					return err;
8831da177e4SLinus Torvalds 				ofs += PAD(je32_to_cpu(node->totlen));
8841da177e4SLinus Torvalds 				break;
8851da177e4SLinus Torvalds 
8866171586aSDavid Woodhouse 			case JFFS2_FEATURE_RWCOMPAT_COPY: {
8871da177e4SLinus Torvalds 				D1(printk(KERN_NOTICE "Unknown but compatible feature node (0x%04x) found at offset 0x%08x\n", je16_to_cpu(node->nodetype), ofs));
8886171586aSDavid Woodhouse 
8892f785402SDavid Woodhouse 				jffs2_link_node_ref(c, jeb, ofs | REF_PRISTINE, PAD(je32_to_cpu(node->totlen)), NULL);
8906171586aSDavid Woodhouse 
8916171586aSDavid Woodhouse 				/* We can't summarise nodes we don't grok */
8926171586aSDavid Woodhouse 				jffs2_sum_disable_collecting(s);
8931da177e4SLinus Torvalds 				ofs += PAD(je32_to_cpu(node->totlen));
8941da177e4SLinus Torvalds 				break;
8951da177e4SLinus Torvalds 				}
8961da177e4SLinus Torvalds 			}
8971da177e4SLinus Torvalds 		}
8986171586aSDavid Woodhouse 	}
8991da177e4SLinus Torvalds 
900e631ddbaSFerenc Havasi 	if (jffs2_sum_active()) {
901e631ddbaSFerenc Havasi 		if (PAD(s->sum_size + JFFS2_SUMMARY_FRAME_SIZE) > jeb->free_size) {
902733802d9SArtem B. Bityutskiy 			dbg_summary("There is not enough space for "
903e631ddbaSFerenc Havasi 				"summary information, disabling for this jeb!\n");
904e631ddbaSFerenc Havasi 			jffs2_sum_disable_collecting(s);
905e631ddbaSFerenc Havasi 		}
906e631ddbaSFerenc Havasi 	}
9071da177e4SLinus Torvalds 
9088b9e9fe8SDavid Woodhouse 	D1(printk(KERN_DEBUG "Block at 0x%08x: free 0x%08x, dirty 0x%08x, unchecked 0x%08x, used 0x%08x, wasted 0x%08x\n",
9098b9e9fe8SDavid Woodhouse 		  jeb->offset,jeb->free_size, jeb->dirty_size, jeb->unchecked_size, jeb->used_size, jeb->wasted_size));
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds 	/* mark_node_obsolete can add to wasted !! */
9121da177e4SLinus Torvalds 	if (jeb->wasted_size) {
9131da177e4SLinus Torvalds 		jeb->dirty_size += jeb->wasted_size;
9141da177e4SLinus Torvalds 		c->dirty_size += jeb->wasted_size;
9151da177e4SLinus Torvalds 		c->wasted_size -= jeb->wasted_size;
9161da177e4SLinus Torvalds 		jeb->wasted_size = 0;
9171da177e4SLinus Torvalds 	}
9181da177e4SLinus Torvalds 
919e631ddbaSFerenc Havasi 	return jffs2_scan_classify_jeb(c, jeb);
9201da177e4SLinus Torvalds }
9211da177e4SLinus Torvalds 
922e631ddbaSFerenc Havasi struct jffs2_inode_cache *jffs2_scan_make_ino_cache(struct jffs2_sb_info *c, uint32_t ino)
9231da177e4SLinus Torvalds {
9241da177e4SLinus Torvalds 	struct jffs2_inode_cache *ic;
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds 	ic = jffs2_get_ino_cache(c, ino);
9271da177e4SLinus Torvalds 	if (ic)
9281da177e4SLinus Torvalds 		return ic;
9291da177e4SLinus Torvalds 
9301da177e4SLinus Torvalds 	if (ino > c->highest_ino)
9311da177e4SLinus Torvalds 		c->highest_ino = ino;
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds 	ic = jffs2_alloc_inode_cache();
9341da177e4SLinus Torvalds 	if (!ic) {
9351da177e4SLinus Torvalds 		printk(KERN_NOTICE "jffs2_scan_make_inode_cache(): allocation of inode cache failed\n");
9361da177e4SLinus Torvalds 		return NULL;
9371da177e4SLinus Torvalds 	}
9381da177e4SLinus Torvalds 	memset(ic, 0, sizeof(*ic));
9391da177e4SLinus Torvalds 
9401da177e4SLinus Torvalds 	ic->ino = ino;
9411da177e4SLinus Torvalds 	ic->nodes = (void *)ic;
9421da177e4SLinus Torvalds 	jffs2_add_ino_cache(c, ic);
9431da177e4SLinus Torvalds 	if (ino == 1)
94427c72b04SDavid Woodhouse 		ic->pino_nlink = 1;
9451da177e4SLinus Torvalds 	return ic;
9461da177e4SLinus Torvalds }
9471da177e4SLinus Torvalds 
9481da177e4SLinus Torvalds static int jffs2_scan_inode_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
949e631ddbaSFerenc Havasi 				 struct jffs2_raw_inode *ri, uint32_t ofs, struct jffs2_summary *s)
9501da177e4SLinus Torvalds {
9511da177e4SLinus Torvalds 	struct jffs2_inode_cache *ic;
95253043002SThomas Gleixner 	uint32_t crc, ino = je32_to_cpu(ri->ino);
9531da177e4SLinus Torvalds 
9541da177e4SLinus Torvalds 	D1(printk(KERN_DEBUG "jffs2_scan_inode_node(): Node at 0x%08x\n", ofs));
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds 	/* We do very little here now. Just check the ino# to which we should attribute
9571da177e4SLinus Torvalds 	   this node; we can do all the CRC checking etc. later. There's a tradeoff here --
9581da177e4SLinus Torvalds 	   we used to scan the flash once only, reading everything we want from it into
9591da177e4SLinus Torvalds 	   memory, then building all our in-core data structures and freeing the extra
9601da177e4SLinus Torvalds 	   information. Now we allow the first part of the mount to complete a lot quicker,
9611da177e4SLinus Torvalds 	   but we have to go _back_ to the flash in order to finish the CRC checking, etc.
9621da177e4SLinus Torvalds 	   Which means that the _full_ amount of time to get to proper write mode with GC
9631da177e4SLinus Torvalds 	   operational may actually be _longer_ than before. Sucks to be me. */
9641da177e4SLinus Torvalds 
96553043002SThomas Gleixner 	/* Check the node CRC in any case. */
96653043002SThomas Gleixner 	crc = crc32(0, ri, sizeof(*ri)-8);
96753043002SThomas Gleixner 	if (crc != je32_to_cpu(ri->node_crc)) {
96853043002SThomas Gleixner 		printk(KERN_NOTICE "jffs2_scan_inode_node(): CRC failed on "
96953043002SThomas Gleixner 		       "node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
97053043002SThomas Gleixner 		       ofs, je32_to_cpu(ri->node_crc), crc);
97153043002SThomas Gleixner 		/*
97253043002SThomas Gleixner 		 * We believe totlen because the CRC on the node
97353043002SThomas Gleixner 		 * _header_ was OK, just the node itself failed.
97453043002SThomas Gleixner 		 */
97553043002SThomas Gleixner 		return jffs2_scan_dirty_space(c, jeb,
97653043002SThomas Gleixner 					      PAD(je32_to_cpu(ri->totlen)));
97753043002SThomas Gleixner 	}
97853043002SThomas Gleixner 
9791da177e4SLinus Torvalds 	ic = jffs2_get_ino_cache(c, ino);
9801da177e4SLinus Torvalds 	if (!ic) {
9811da177e4SLinus Torvalds 		ic = jffs2_scan_make_ino_cache(c, ino);
9822f785402SDavid Woodhouse 		if (!ic)
9831da177e4SLinus Torvalds 			return -ENOMEM;
9841da177e4SLinus Torvalds 	}
9851da177e4SLinus Torvalds 
9861da177e4SLinus Torvalds 	/* Wheee. It worked */
9872f785402SDavid Woodhouse 	jffs2_link_node_ref(c, jeb, ofs | REF_UNCHECKED, PAD(je32_to_cpu(ri->totlen)), ic);
9881da177e4SLinus Torvalds 
9891da177e4SLinus Torvalds 	D1(printk(KERN_DEBUG "Node is ino #%u, version %d. Range 0x%x-0x%x\n",
9901da177e4SLinus Torvalds 		  je32_to_cpu(ri->ino), je32_to_cpu(ri->version),
9911da177e4SLinus Torvalds 		  je32_to_cpu(ri->offset),
9921da177e4SLinus Torvalds 		  je32_to_cpu(ri->offset)+je32_to_cpu(ri->dsize)));
9931da177e4SLinus Torvalds 
9941da177e4SLinus Torvalds 	pseudo_random += je32_to_cpu(ri->version);
9951da177e4SLinus Torvalds 
996e631ddbaSFerenc Havasi 	if (jffs2_sum_active()) {
997e631ddbaSFerenc Havasi 		jffs2_sum_add_inode_mem(s, ri, ofs - jeb->offset);
998e631ddbaSFerenc Havasi 	}
999e631ddbaSFerenc Havasi 
10001da177e4SLinus Torvalds 	return 0;
10011da177e4SLinus Torvalds }
10021da177e4SLinus Torvalds 
10031da177e4SLinus Torvalds static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
1004e631ddbaSFerenc Havasi 				  struct jffs2_raw_dirent *rd, uint32_t ofs, struct jffs2_summary *s)
10051da177e4SLinus Torvalds {
10061da177e4SLinus Torvalds 	struct jffs2_full_dirent *fd;
10071da177e4SLinus Torvalds 	struct jffs2_inode_cache *ic;
1008b534e70cSDavid Woodhouse 	uint32_t checkedlen;
10091da177e4SLinus Torvalds 	uint32_t crc;
101068270995SDavid Woodhouse 	int err;
10111da177e4SLinus Torvalds 
10121da177e4SLinus Torvalds 	D1(printk(KERN_DEBUG "jffs2_scan_dirent_node(): Node at 0x%08x\n", ofs));
10131da177e4SLinus Torvalds 
10141da177e4SLinus Torvalds 	/* We don't get here unless the node is still valid, so we don't have to
10151da177e4SLinus Torvalds 	   mask in the ACCURATE bit any more. */
10161da177e4SLinus Torvalds 	crc = crc32(0, rd, sizeof(*rd)-8);
10171da177e4SLinus Torvalds 
10181da177e4SLinus Torvalds 	if (crc != je32_to_cpu(rd->node_crc)) {
10191da177e4SLinus Torvalds 		printk(KERN_NOTICE "jffs2_scan_dirent_node(): Node CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
10201da177e4SLinus Torvalds 		       ofs, je32_to_cpu(rd->node_crc), crc);
10211da177e4SLinus Torvalds 		/* We believe totlen because the CRC on the node _header_ was OK, just the node itself failed. */
102268270995SDavid Woodhouse 		if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(rd->totlen)))))
102368270995SDavid Woodhouse 			return err;
10241da177e4SLinus Torvalds 		return 0;
10251da177e4SLinus Torvalds 	}
10261da177e4SLinus Torvalds 
10271da177e4SLinus Torvalds 	pseudo_random += je32_to_cpu(rd->version);
10281da177e4SLinus Torvalds 
1029b534e70cSDavid Woodhouse 	/* Should never happen. Did. (OLPC trac #4184)*/
1030b534e70cSDavid Woodhouse 	checkedlen = strnlen(rd->name, rd->nsize);
1031b534e70cSDavid Woodhouse 	if (checkedlen < rd->nsize) {
1032b534e70cSDavid Woodhouse 		printk(KERN_ERR "Dirent at %08x has zeroes in name. Truncating to %d chars\n",
1033b534e70cSDavid Woodhouse 		       ofs, checkedlen);
1034b534e70cSDavid Woodhouse 	}
1035b534e70cSDavid Woodhouse 	fd = jffs2_alloc_full_dirent(checkedlen+1);
10361da177e4SLinus Torvalds 	if (!fd) {
10371da177e4SLinus Torvalds 		return -ENOMEM;
10381da177e4SLinus Torvalds 	}
1039b534e70cSDavid Woodhouse 	memcpy(&fd->name, rd->name, checkedlen);
1040b534e70cSDavid Woodhouse 	fd->name[checkedlen] = 0;
10411da177e4SLinus Torvalds 
10421da177e4SLinus Torvalds 	crc = crc32(0, fd->name, rd->nsize);
10431da177e4SLinus Torvalds 	if (crc != je32_to_cpu(rd->name_crc)) {
10441da177e4SLinus Torvalds 		printk(KERN_NOTICE "jffs2_scan_dirent_node(): Name CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
10451da177e4SLinus Torvalds 		       ofs, je32_to_cpu(rd->name_crc), crc);
10461da177e4SLinus Torvalds 		D1(printk(KERN_NOTICE "Name for which CRC failed is (now) '%s', ino #%d\n", fd->name, je32_to_cpu(rd->ino)));
10471da177e4SLinus Torvalds 		jffs2_free_full_dirent(fd);
10481da177e4SLinus Torvalds 		/* FIXME: Why do we believe totlen? */
10491da177e4SLinus Torvalds 		/* We believe totlen because the CRC on the node _header_ was OK, just the name failed. */
105068270995SDavid Woodhouse 		if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(rd->totlen)))))
105168270995SDavid Woodhouse 			return err;
10521da177e4SLinus Torvalds 		return 0;
10531da177e4SLinus Torvalds 	}
10541da177e4SLinus Torvalds 	ic = jffs2_scan_make_ino_cache(c, je32_to_cpu(rd->pino));
10551da177e4SLinus Torvalds 	if (!ic) {
10561da177e4SLinus Torvalds 		jffs2_free_full_dirent(fd);
10571da177e4SLinus Torvalds 		return -ENOMEM;
10581da177e4SLinus Torvalds 	}
10591da177e4SLinus Torvalds 
106043dfa07fSDavid Woodhouse 	fd->raw = jffs2_link_node_ref(c, jeb, ofs | dirent_node_state(rd),
106143dfa07fSDavid Woodhouse 				      PAD(je32_to_cpu(rd->totlen)), ic);
10621da177e4SLinus Torvalds 
10631da177e4SLinus Torvalds 	fd->next = NULL;
10641da177e4SLinus Torvalds 	fd->version = je32_to_cpu(rd->version);
10651da177e4SLinus Torvalds 	fd->ino = je32_to_cpu(rd->ino);
1066b534e70cSDavid Woodhouse 	fd->nhash = full_name_hash(fd->name, checkedlen);
10671da177e4SLinus Torvalds 	fd->type = rd->type;
10681da177e4SLinus Torvalds 	jffs2_add_fd_to_list(c, fd, &ic->scan_dents);
10691da177e4SLinus Torvalds 
1070e631ddbaSFerenc Havasi 	if (jffs2_sum_active()) {
1071e631ddbaSFerenc Havasi 		jffs2_sum_add_dirent_mem(s, rd, ofs - jeb->offset);
1072e631ddbaSFerenc Havasi 	}
1073e631ddbaSFerenc Havasi 
10741da177e4SLinus Torvalds 	return 0;
10751da177e4SLinus Torvalds }
10761da177e4SLinus Torvalds 
10771da177e4SLinus Torvalds static int count_list(struct list_head *l)
10781da177e4SLinus Torvalds {
10791da177e4SLinus Torvalds 	uint32_t count = 0;
10801da177e4SLinus Torvalds 	struct list_head *tmp;
10811da177e4SLinus Torvalds 
10821da177e4SLinus Torvalds 	list_for_each(tmp, l) {
10831da177e4SLinus Torvalds 		count++;
10841da177e4SLinus Torvalds 	}
10851da177e4SLinus Torvalds 	return count;
10861da177e4SLinus Torvalds }
10871da177e4SLinus Torvalds 
10881da177e4SLinus Torvalds /* Note: This breaks if list_empty(head). I don't care. You
10891da177e4SLinus Torvalds    might, if you copy this code and use it elsewhere :) */
10901da177e4SLinus Torvalds static void rotate_list(struct list_head *head, uint32_t count)
10911da177e4SLinus Torvalds {
10921da177e4SLinus Torvalds 	struct list_head *n = head->next;
10931da177e4SLinus Torvalds 
10941da177e4SLinus Torvalds 	list_del(head);
10951da177e4SLinus Torvalds 	while(count--) {
10961da177e4SLinus Torvalds 		n = n->next;
10971da177e4SLinus Torvalds 	}
10981da177e4SLinus Torvalds 	list_add(head, n);
10991da177e4SLinus Torvalds }
11001da177e4SLinus Torvalds 
11011da177e4SLinus Torvalds void jffs2_rotate_lists(struct jffs2_sb_info *c)
11021da177e4SLinus Torvalds {
11031da177e4SLinus Torvalds 	uint32_t x;
11041da177e4SLinus Torvalds 	uint32_t rotateby;
11051da177e4SLinus Torvalds 
11061da177e4SLinus Torvalds 	x = count_list(&c->clean_list);
11071da177e4SLinus Torvalds 	if (x) {
11081da177e4SLinus Torvalds 		rotateby = pseudo_random % x;
11091da177e4SLinus Torvalds 		rotate_list((&c->clean_list), rotateby);
11101da177e4SLinus Torvalds 	}
11111da177e4SLinus Torvalds 
11121da177e4SLinus Torvalds 	x = count_list(&c->very_dirty_list);
11131da177e4SLinus Torvalds 	if (x) {
11141da177e4SLinus Torvalds 		rotateby = pseudo_random % x;
11151da177e4SLinus Torvalds 		rotate_list((&c->very_dirty_list), rotateby);
11161da177e4SLinus Torvalds 	}
11171da177e4SLinus Torvalds 
11181da177e4SLinus Torvalds 	x = count_list(&c->dirty_list);
11191da177e4SLinus Torvalds 	if (x) {
11201da177e4SLinus Torvalds 		rotateby = pseudo_random % x;
11211da177e4SLinus Torvalds 		rotate_list((&c->dirty_list), rotateby);
11221da177e4SLinus Torvalds 	}
11231da177e4SLinus Torvalds 
11241da177e4SLinus Torvalds 	x = count_list(&c->erasable_list);
11251da177e4SLinus Torvalds 	if (x) {
11261da177e4SLinus Torvalds 		rotateby = pseudo_random % x;
11271da177e4SLinus Torvalds 		rotate_list((&c->erasable_list), rotateby);
11281da177e4SLinus Torvalds 	}
11291da177e4SLinus Torvalds 
11301da177e4SLinus Torvalds 	if (c->nr_erasing_blocks) {
11311da177e4SLinus Torvalds 		rotateby = pseudo_random % c->nr_erasing_blocks;
11321da177e4SLinus Torvalds 		rotate_list((&c->erase_pending_list), rotateby);
11331da177e4SLinus Torvalds 	}
11341da177e4SLinus Torvalds 
11351da177e4SLinus Torvalds 	if (c->nr_free_blocks) {
11361da177e4SLinus Torvalds 		rotateby = pseudo_random % c->nr_free_blocks;
11371da177e4SLinus Torvalds 		rotate_list((&c->free_list), rotateby);
11381da177e4SLinus Torvalds 	}
11391da177e4SLinus Torvalds }
1140