xref: /openbmc/linux/fs/jffs2/scan.c (revision 3be36675)
1 /*
2  * JFFS2 -- Journalling Flash File System, Version 2.
3  *
4  * Copyright (C) 2001-2003 Red Hat, Inc.
5  *
6  * Created by David Woodhouse <dwmw2@infradead.org>
7  *
8  * For licensing information, see the file 'LICENCE' in this directory.
9  *
10  * $Id: scan.c,v 1.116 2005/02/09 09:09:02 pavlov Exp $
11  *
12  */
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/slab.h>
16 #include <linux/mtd/mtd.h>
17 #include <linux/pagemap.h>
18 #include <linux/crc32.h>
19 #include <linux/compiler.h>
20 #include "nodelist.h"
21 
22 #define DEFAULT_EMPTY_SCAN_SIZE 1024
23 
24 #define DIRTY_SPACE(x) do { typeof(x) _x = (x); \
25 		c->free_size -= _x; c->dirty_size += _x; \
26 		jeb->free_size -= _x ; jeb->dirty_size += _x; \
27 		}while(0)
28 #define USED_SPACE(x) do { typeof(x) _x = (x); \
29 		c->free_size -= _x; c->used_size += _x; \
30 		jeb->free_size -= _x ; jeb->used_size += _x; \
31 		}while(0)
32 #define UNCHECKED_SPACE(x) do { typeof(x) _x = (x); \
33 		c->free_size -= _x; c->unchecked_size += _x; \
34 		jeb->free_size -= _x ; jeb->unchecked_size += _x; \
35 		}while(0)
36 
37 #define noisy_printk(noise, args...) do { \
38 	if (*(noise)) { \
39 		printk(KERN_NOTICE args); \
40 		 (*(noise))--; \
41 		 if (!(*(noise))) { \
42 			 printk(KERN_NOTICE "Further such events for this erase block will not be printed\n"); \
43 		 } \
44 	} \
45 } while(0)
46 
47 static uint32_t pseudo_random;
48 
49 static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
50 				  unsigned char *buf, uint32_t buf_size);
51 
52 /* These helper functions _must_ increase ofs and also do the dirty/used space accounting.
53  * Returning an error will abort the mount - bad checksums etc. should just mark the space
54  * as dirty.
55  */
56 static int jffs2_scan_inode_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
57 				 struct jffs2_raw_inode *ri, uint32_t ofs);
58 static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
59 				 struct jffs2_raw_dirent *rd, uint32_t ofs);
60 
61 #define BLK_STATE_ALLFF		0
62 #define BLK_STATE_CLEAN		1
63 #define BLK_STATE_PARTDIRTY	2
64 #define BLK_STATE_CLEANMARKER	3
65 #define BLK_STATE_ALLDIRTY	4
66 #define BLK_STATE_BADBLOCK	5
67 
68 static inline int min_free(struct jffs2_sb_info *c)
69 {
70 	uint32_t min = 2 * sizeof(struct jffs2_raw_inode);
71 #if defined CONFIG_JFFS2_FS_NAND || defined CONFIG_JFFS2_FS_NOR_ECC
72 	if (!jffs2_can_mark_obsolete(c) && min < c->wbuf_pagesize)
73 		return c->wbuf_pagesize;
74 #endif
75 	return min;
76 
77 }
78 
79 static inline uint32_t EMPTY_SCAN_SIZE(uint32_t sector_size) {
80 	if (sector_size < DEFAULT_EMPTY_SCAN_SIZE)
81 		return sector_size;
82 	else
83 		return DEFAULT_EMPTY_SCAN_SIZE;
84 }
85 
86 int jffs2_scan_medium(struct jffs2_sb_info *c)
87 {
88 	int i, ret;
89 	uint32_t empty_blocks = 0, bad_blocks = 0;
90 	unsigned char *flashbuf = NULL;
91 	uint32_t buf_size = 0;
92 #ifndef __ECOS
93 	size_t pointlen;
94 
95 	if (c->mtd->point) {
96 		ret = c->mtd->point (c->mtd, 0, c->mtd->size, &pointlen, &flashbuf);
97 		if (!ret && pointlen < c->mtd->size) {
98 			/* Don't muck about if it won't let us point to the whole flash */
99 			D1(printk(KERN_DEBUG "MTD point returned len too short: 0x%zx\n", pointlen));
100 			c->mtd->unpoint(c->mtd, flashbuf, 0, c->mtd->size);
101 			flashbuf = NULL;
102 		}
103 		if (ret)
104 			D1(printk(KERN_DEBUG "MTD point failed %d\n", ret));
105 	}
106 #endif
107 	if (!flashbuf) {
108 		/* For NAND it's quicker to read a whole eraseblock at a time,
109 		   apparently */
110 		if (jffs2_cleanmarker_oob(c))
111 			buf_size = c->sector_size;
112 		else
113 			buf_size = PAGE_SIZE;
114 
115 		/* Respect kmalloc limitations */
116 		if (buf_size > 128*1024)
117 			buf_size = 128*1024;
118 
119 		D1(printk(KERN_DEBUG "Allocating readbuf of %d bytes\n", buf_size));
120 		flashbuf = kmalloc(buf_size, GFP_KERNEL);
121 		if (!flashbuf)
122 			return -ENOMEM;
123 	}
124 
125 	for (i=0; i<c->nr_blocks; i++) {
126 		struct jffs2_eraseblock *jeb = &c->blocks[i];
127 
128 		ret = jffs2_scan_eraseblock(c, jeb, buf_size?flashbuf:(flashbuf+jeb->offset), buf_size);
129 
130 		if (ret < 0)
131 			goto out;
132 
133 		ACCT_PARANOIA_CHECK(jeb);
134 
135 		/* Now decide which list to put it on */
136 		switch(ret) {
137 		case BLK_STATE_ALLFF:
138 			/*
139 			 * Empty block.   Since we can't be sure it
140 			 * was entirely erased, we just queue it for erase
141 			 * again.  It will be marked as such when the erase
142 			 * is complete.  Meanwhile we still count it as empty
143 			 * for later checks.
144 			 */
145 			empty_blocks++;
146 			list_add(&jeb->list, &c->erase_pending_list);
147 			c->nr_erasing_blocks++;
148 			break;
149 
150 		case BLK_STATE_CLEANMARKER:
151 			/* Only a CLEANMARKER node is valid */
152 			if (!jeb->dirty_size) {
153 				/* It's actually free */
154 				list_add(&jeb->list, &c->free_list);
155 				c->nr_free_blocks++;
156 			} else {
157 				/* Dirt */
158 				D1(printk(KERN_DEBUG "Adding all-dirty block at 0x%08x to erase_pending_list\n", jeb->offset));
159 				list_add(&jeb->list, &c->erase_pending_list);
160 				c->nr_erasing_blocks++;
161 			}
162 			break;
163 
164 		case BLK_STATE_CLEAN:
165                         /* Full (or almost full) of clean data. Clean list */
166                         list_add(&jeb->list, &c->clean_list);
167 			break;
168 
169 		case BLK_STATE_PARTDIRTY:
170                         /* Some data, but not full. Dirty list. */
171                         /* We want to remember the block with most free space
172                            and stick it in the 'nextblock' position to start writing to it. */
173                         if (jeb->free_size > min_free(c) &&
174 			    (!c->nextblock || c->nextblock->free_size < jeb->free_size)) {
175                                 /* Better candidate for the next writes to go to */
176                                 if (c->nextblock) {
177 					c->nextblock->dirty_size += c->nextblock->free_size + c->nextblock->wasted_size;
178 					c->dirty_size += c->nextblock->free_size + c->nextblock->wasted_size;
179 					c->free_size -= c->nextblock->free_size;
180 					c->wasted_size -= c->nextblock->wasted_size;
181 					c->nextblock->free_size = c->nextblock->wasted_size = 0;
182 					if (VERYDIRTY(c, c->nextblock->dirty_size)) {
183 						list_add(&c->nextblock->list, &c->very_dirty_list);
184 					} else {
185 						list_add(&c->nextblock->list, &c->dirty_list);
186 					}
187 				}
188                                 c->nextblock = jeb;
189                         } else {
190 				jeb->dirty_size += jeb->free_size + jeb->wasted_size;
191 				c->dirty_size += jeb->free_size + jeb->wasted_size;
192 				c->free_size -= jeb->free_size;
193 				c->wasted_size -= jeb->wasted_size;
194 				jeb->free_size = jeb->wasted_size = 0;
195 				if (VERYDIRTY(c, jeb->dirty_size)) {
196 					list_add(&jeb->list, &c->very_dirty_list);
197 				} else {
198 					list_add(&jeb->list, &c->dirty_list);
199 				}
200                         }
201 			break;
202 
203 		case BLK_STATE_ALLDIRTY:
204 			/* Nothing valid - not even a clean marker. Needs erasing. */
205                         /* For now we just put it on the erasing list. We'll start the erases later */
206 			D1(printk(KERN_NOTICE "JFFS2: Erase block at 0x%08x is not formatted. It will be erased\n", jeb->offset));
207                         list_add(&jeb->list, &c->erase_pending_list);
208 			c->nr_erasing_blocks++;
209 			break;
210 
211 		case BLK_STATE_BADBLOCK:
212 			D1(printk(KERN_NOTICE "JFFS2: Block at 0x%08x is bad\n", jeb->offset));
213                         list_add(&jeb->list, &c->bad_list);
214 			c->bad_size += c->sector_size;
215 			c->free_size -= c->sector_size;
216 			bad_blocks++;
217 			break;
218 		default:
219 			printk(KERN_WARNING "jffs2_scan_medium(): unknown block state\n");
220 			BUG();
221 		}
222 	}
223 
224 	/* Nextblock dirty is always seen as wasted, because we cannot recycle it now */
225 	if (c->nextblock && (c->nextblock->dirty_size)) {
226 		c->nextblock->wasted_size += c->nextblock->dirty_size;
227 		c->wasted_size += c->nextblock->dirty_size;
228 		c->dirty_size -= c->nextblock->dirty_size;
229 		c->nextblock->dirty_size = 0;
230 	}
231 #if defined CONFIG_JFFS2_FS_NAND || defined CONFIG_JFFS2_FS_NOR_ECC
232 	if (!jffs2_can_mark_obsolete(c) && c->nextblock && (c->nextblock->free_size & (c->wbuf_pagesize-1))) {
233 		/* If we're going to start writing into a block which already
234 		   contains data, and the end of the data isn't page-aligned,
235 		   skip a little and align it. */
236 
237 		uint32_t skip = c->nextblock->free_size & (c->wbuf_pagesize-1);
238 
239 		D1(printk(KERN_DEBUG "jffs2_scan_medium(): Skipping %d bytes in nextblock to ensure page alignment\n",
240 			  skip));
241 		c->nextblock->wasted_size += skip;
242 		c->wasted_size += skip;
243 
244 		c->nextblock->free_size -= skip;
245 		c->free_size -= skip;
246 	}
247 #endif
248 	if (c->nr_erasing_blocks) {
249 		if ( !c->used_size && ((c->nr_free_blocks+empty_blocks+bad_blocks)!= c->nr_blocks || bad_blocks == c->nr_blocks) ) {
250 			printk(KERN_NOTICE "Cowardly refusing to erase blocks on filesystem with no valid JFFS2 nodes\n");
251 			printk(KERN_NOTICE "empty_blocks %d, bad_blocks %d, c->nr_blocks %d\n",empty_blocks,bad_blocks,c->nr_blocks);
252 			ret = -EIO;
253 			goto out;
254 		}
255 		jffs2_erase_pending_trigger(c);
256 	}
257 	ret = 0;
258  out:
259 	if (buf_size)
260 		kfree(flashbuf);
261 #ifndef __ECOS
262 	else
263 		c->mtd->unpoint(c->mtd, flashbuf, 0, c->mtd->size);
264 #endif
265 	return ret;
266 }
267 
268 static int jffs2_fill_scan_buf (struct jffs2_sb_info *c, unsigned char *buf,
269 				uint32_t ofs, uint32_t len)
270 {
271 	int ret;
272 	size_t retlen;
273 
274 	ret = jffs2_flash_read(c, ofs, len, &retlen, buf);
275 	if (ret) {
276 		D1(printk(KERN_WARNING "mtd->read(0x%x bytes from 0x%x) returned %d\n", len, ofs, ret));
277 		return ret;
278 	}
279 	if (retlen < len) {
280 		D1(printk(KERN_WARNING "Read at 0x%x gave only 0x%zx bytes\n", ofs, retlen));
281 		return -EIO;
282 	}
283 	D2(printk(KERN_DEBUG "Read 0x%x bytes from 0x%08x into buf\n", len, ofs));
284 	D2(printk(KERN_DEBUG "000: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
285 		  buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7], buf[8], buf[9], buf[10], buf[11], buf[12], buf[13], buf[14], buf[15]));
286 	return 0;
287 }
288 
289 static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
290 				  unsigned char *buf, uint32_t buf_size) {
291 	struct jffs2_unknown_node *node;
292 	struct jffs2_unknown_node crcnode;
293 	uint32_t ofs, prevofs;
294 	uint32_t hdr_crc, buf_ofs, buf_len;
295 	int err;
296 	int noise = 0;
297 #ifdef CONFIG_JFFS2_FS_NAND
298 	int cleanmarkerfound = 0;
299 #endif
300 
301 	ofs = jeb->offset;
302 	prevofs = jeb->offset - 1;
303 
304 	D1(printk(KERN_DEBUG "jffs2_scan_eraseblock(): Scanning block at 0x%x\n", ofs));
305 
306 #ifdef CONFIG_JFFS2_FS_NAND
307 	if (jffs2_cleanmarker_oob(c)) {
308 		int ret = jffs2_check_nand_cleanmarker(c, jeb);
309 		D2(printk(KERN_NOTICE "jffs_check_nand_cleanmarker returned %d\n",ret));
310 		/* Even if it's not found, we still scan to see
311 		   if the block is empty. We use this information
312 		   to decide whether to erase it or not. */
313 		switch (ret) {
314 		case 0:		cleanmarkerfound = 1; break;
315 		case 1: 	break;
316 		case 2: 	return BLK_STATE_BADBLOCK;
317 		case 3:		return BLK_STATE_ALLDIRTY; /* Block has failed to erase min. once */
318 		default: 	return ret;
319 		}
320 	}
321 #endif
322 	buf_ofs = jeb->offset;
323 
324 	if (!buf_size) {
325 		buf_len = c->sector_size;
326 	} else {
327 		buf_len = EMPTY_SCAN_SIZE(c->sector_size);
328 		err = jffs2_fill_scan_buf(c, buf, buf_ofs, buf_len);
329 		if (err)
330 			return err;
331 	}
332 
333 	/* We temporarily use 'ofs' as a pointer into the buffer/jeb */
334 	ofs = 0;
335 
336 	/* Scan only 4KiB of 0xFF before declaring it's empty */
337 	while(ofs < EMPTY_SCAN_SIZE(c->sector_size) && *(uint32_t *)(&buf[ofs]) == 0xFFFFFFFF)
338 		ofs += 4;
339 
340 	if (ofs == EMPTY_SCAN_SIZE(c->sector_size)) {
341 #ifdef CONFIG_JFFS2_FS_NAND
342 		if (jffs2_cleanmarker_oob(c)) {
343 			/* scan oob, take care of cleanmarker */
344 			int ret = jffs2_check_oob_empty(c, jeb, cleanmarkerfound);
345 			D2(printk(KERN_NOTICE "jffs2_check_oob_empty returned %d\n",ret));
346 			switch (ret) {
347 			case 0:		return cleanmarkerfound ? BLK_STATE_CLEANMARKER : BLK_STATE_ALLFF;
348 			case 1: 	return BLK_STATE_ALLDIRTY;
349 			default: 	return ret;
350 			}
351 		}
352 #endif
353 		D1(printk(KERN_DEBUG "Block at 0x%08x is empty (erased)\n", jeb->offset));
354 		return BLK_STATE_ALLFF;	/* OK to erase if all blocks are like this */
355 	}
356 	if (ofs) {
357 		D1(printk(KERN_DEBUG "Free space at %08x ends at %08x\n", jeb->offset,
358 			  jeb->offset + ofs));
359 		DIRTY_SPACE(ofs);
360 	}
361 
362 	/* Now ofs is a complete physical flash offset as it always was... */
363 	ofs += jeb->offset;
364 
365 	noise = 10;
366 
367 scan_more:
368 	while(ofs < jeb->offset + c->sector_size) {
369 
370 		D1(ACCT_PARANOIA_CHECK(jeb));
371 
372 		cond_resched();
373 
374 		if (ofs & 3) {
375 			printk(KERN_WARNING "Eep. ofs 0x%08x not word-aligned!\n", ofs);
376 			ofs = PAD(ofs);
377 			continue;
378 		}
379 		if (ofs == prevofs) {
380 			printk(KERN_WARNING "ofs 0x%08x has already been seen. Skipping\n", ofs);
381 			DIRTY_SPACE(4);
382 			ofs += 4;
383 			continue;
384 		}
385 		prevofs = ofs;
386 
387 		if (jeb->offset + c->sector_size < ofs + sizeof(*node)) {
388 			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),
389 				  jeb->offset, c->sector_size, ofs, sizeof(*node)));
390 			DIRTY_SPACE((jeb->offset + c->sector_size)-ofs);
391 			break;
392 		}
393 
394 		if (buf_ofs + buf_len < ofs + sizeof(*node)) {
395 			buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
396 			D1(printk(KERN_DEBUG "Fewer than %zd bytes (node header) left to end of buf. Reading 0x%x at 0x%08x\n",
397 				  sizeof(struct jffs2_unknown_node), buf_len, ofs));
398 			err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
399 			if (err)
400 				return err;
401 			buf_ofs = ofs;
402 		}
403 
404 		node = (struct jffs2_unknown_node *)&buf[ofs-buf_ofs];
405 
406 		if (*(uint32_t *)(&buf[ofs-buf_ofs]) == 0xffffffff) {
407 			uint32_t inbuf_ofs;
408 			uint32_t empty_start;
409 
410 			empty_start = ofs;
411 			ofs += 4;
412 
413 			D1(printk(KERN_DEBUG "Found empty flash at 0x%08x\n", ofs));
414 		more_empty:
415 			inbuf_ofs = ofs - buf_ofs;
416 			while (inbuf_ofs < buf_len) {
417 				if (*(uint32_t *)(&buf[inbuf_ofs]) != 0xffffffff) {
418 					printk(KERN_WARNING "Empty flash at 0x%08x ends at 0x%08x\n",
419 					       empty_start, ofs);
420 					DIRTY_SPACE(ofs-empty_start);
421 					goto scan_more;
422 				}
423 
424 				inbuf_ofs+=4;
425 				ofs += 4;
426 			}
427 			/* Ran off end. */
428 			D1(printk(KERN_DEBUG "Empty flash to end of buffer at 0x%08x\n", ofs));
429 
430 			/* If we're only checking the beginning of a block with a cleanmarker,
431 			   bail now */
432 			if (buf_ofs == jeb->offset && jeb->used_size == PAD(c->cleanmarker_size) &&
433 			    c->cleanmarker_size && !jeb->dirty_size && !jeb->first_node->next_in_ino) {
434 				D1(printk(KERN_DEBUG "%d bytes at start of block seems clean... assuming all clean\n", EMPTY_SCAN_SIZE(c->sector_size)));
435 				return BLK_STATE_CLEANMARKER;
436 			}
437 
438 			/* See how much more there is to read in this eraseblock... */
439 			buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
440 			if (!buf_len) {
441 				/* No more to read. Break out of main loop without marking
442 				   this range of empty space as dirty (because it's not) */
443 				D1(printk(KERN_DEBUG "Empty flash at %08x runs to end of block. Treating as free_space\n",
444 					  empty_start));
445 				break;
446 			}
447 			D1(printk(KERN_DEBUG "Reading another 0x%x at 0x%08x\n", buf_len, ofs));
448 			err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
449 			if (err)
450 				return err;
451 			buf_ofs = ofs;
452 			goto more_empty;
453 		}
454 
455 		if (ofs == jeb->offset && je16_to_cpu(node->magic) == KSAMTIB_CIGAM_2SFFJ) {
456 			printk(KERN_WARNING "Magic bitmask is backwards at offset 0x%08x. Wrong endian filesystem?\n", ofs);
457 			DIRTY_SPACE(4);
458 			ofs += 4;
459 			continue;
460 		}
461 		if (je16_to_cpu(node->magic) == JFFS2_DIRTY_BITMASK) {
462 			D1(printk(KERN_DEBUG "Dirty bitmask at 0x%08x\n", ofs));
463 			DIRTY_SPACE(4);
464 			ofs += 4;
465 			continue;
466 		}
467 		if (je16_to_cpu(node->magic) == JFFS2_OLD_MAGIC_BITMASK) {
468 			printk(KERN_WARNING "Old JFFS2 bitmask found at 0x%08x\n", ofs);
469 			printk(KERN_WARNING "You cannot use older JFFS2 filesystems with newer kernels\n");
470 			DIRTY_SPACE(4);
471 			ofs += 4;
472 			continue;
473 		}
474 		if (je16_to_cpu(node->magic) != JFFS2_MAGIC_BITMASK) {
475 			/* OK. We're out of possibilities. Whinge and move on */
476 			noisy_printk(&noise, "jffs2_scan_eraseblock(): Magic bitmask 0x%04x not found at 0x%08x: 0x%04x instead\n",
477 				     JFFS2_MAGIC_BITMASK, ofs,
478 				     je16_to_cpu(node->magic));
479 			DIRTY_SPACE(4);
480 			ofs += 4;
481 			continue;
482 		}
483 		/* We seem to have a node of sorts. Check the CRC */
484 		crcnode.magic = node->magic;
485 		crcnode.nodetype = cpu_to_je16( je16_to_cpu(node->nodetype) | JFFS2_NODE_ACCURATE);
486 		crcnode.totlen = node->totlen;
487 		hdr_crc = crc32(0, &crcnode, sizeof(crcnode)-4);
488 
489 		if (hdr_crc != je32_to_cpu(node->hdr_crc)) {
490 			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",
491 				     ofs, je16_to_cpu(node->magic),
492 				     je16_to_cpu(node->nodetype),
493 				     je32_to_cpu(node->totlen),
494 				     je32_to_cpu(node->hdr_crc),
495 				     hdr_crc);
496 			DIRTY_SPACE(4);
497 			ofs += 4;
498 			continue;
499 		}
500 
501 		if (ofs + je32_to_cpu(node->totlen) >
502 		    jeb->offset + c->sector_size) {
503 			/* Eep. Node goes over the end of the erase block. */
504 			printk(KERN_WARNING "Node at 0x%08x with length 0x%08x would run over the end of the erase block\n",
505 			       ofs, je32_to_cpu(node->totlen));
506 			printk(KERN_WARNING "Perhaps the file system was created with the wrong erase size?\n");
507 			DIRTY_SPACE(4);
508 			ofs += 4;
509 			continue;
510 		}
511 
512 		if (!(je16_to_cpu(node->nodetype) & JFFS2_NODE_ACCURATE)) {
513 			/* Wheee. This is an obsoleted node */
514 			D2(printk(KERN_DEBUG "Node at 0x%08x is obsolete. Skipping\n", ofs));
515 			DIRTY_SPACE(PAD(je32_to_cpu(node->totlen)));
516 			ofs += PAD(je32_to_cpu(node->totlen));
517 			continue;
518 		}
519 
520 		switch(je16_to_cpu(node->nodetype)) {
521 		case JFFS2_NODETYPE_INODE:
522 			if (buf_ofs + buf_len < ofs + sizeof(struct jffs2_raw_inode)) {
523 				buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
524 				D1(printk(KERN_DEBUG "Fewer than %zd bytes (inode node) left to end of buf. Reading 0x%x at 0x%08x\n",
525 					  sizeof(struct jffs2_raw_inode), buf_len, ofs));
526 				err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
527 				if (err)
528 					return err;
529 				buf_ofs = ofs;
530 				node = (void *)buf;
531 			}
532 			err = jffs2_scan_inode_node(c, jeb, (void *)node, ofs);
533 			if (err) return err;
534 			ofs += PAD(je32_to_cpu(node->totlen));
535 			break;
536 
537 		case JFFS2_NODETYPE_DIRENT:
538 			if (buf_ofs + buf_len < ofs + je32_to_cpu(node->totlen)) {
539 				buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
540 				D1(printk(KERN_DEBUG "Fewer than %d bytes (dirent node) left to end of buf. Reading 0x%x at 0x%08x\n",
541 					  je32_to_cpu(node->totlen), buf_len, ofs));
542 				err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
543 				if (err)
544 					return err;
545 				buf_ofs = ofs;
546 				node = (void *)buf;
547 			}
548 			err = jffs2_scan_dirent_node(c, jeb, (void *)node, ofs);
549 			if (err) return err;
550 			ofs += PAD(je32_to_cpu(node->totlen));
551 			break;
552 
553 		case JFFS2_NODETYPE_CLEANMARKER:
554 			D1(printk(KERN_DEBUG "CLEANMARKER node found at 0x%08x\n", ofs));
555 			if (je32_to_cpu(node->totlen) != c->cleanmarker_size) {
556 				printk(KERN_NOTICE "CLEANMARKER node found at 0x%08x has totlen 0x%x != normal 0x%x\n",
557 				       ofs, je32_to_cpu(node->totlen), c->cleanmarker_size);
558 				DIRTY_SPACE(PAD(sizeof(struct jffs2_unknown_node)));
559 				ofs += PAD(sizeof(struct jffs2_unknown_node));
560 			} else if (jeb->first_node) {
561 				printk(KERN_NOTICE "CLEANMARKER node found at 0x%08x, not first node in block (0x%08x)\n", ofs, jeb->offset);
562 				DIRTY_SPACE(PAD(sizeof(struct jffs2_unknown_node)));
563 				ofs += PAD(sizeof(struct jffs2_unknown_node));
564 			} else {
565 				struct jffs2_raw_node_ref *marker_ref = jffs2_alloc_raw_node_ref();
566 				if (!marker_ref) {
567 					printk(KERN_NOTICE "Failed to allocate node ref for clean marker\n");
568 					return -ENOMEM;
569 				}
570 				marker_ref->next_in_ino = NULL;
571 				marker_ref->next_phys = NULL;
572 				marker_ref->flash_offset = ofs | REF_NORMAL;
573 				marker_ref->__totlen = c->cleanmarker_size;
574 				jeb->first_node = jeb->last_node = marker_ref;
575 
576 				USED_SPACE(PAD(c->cleanmarker_size));
577 				ofs += PAD(c->cleanmarker_size);
578 			}
579 			break;
580 
581 		case JFFS2_NODETYPE_PADDING:
582 			DIRTY_SPACE(PAD(je32_to_cpu(node->totlen)));
583 			ofs += PAD(je32_to_cpu(node->totlen));
584 			break;
585 
586 		default:
587 			switch (je16_to_cpu(node->nodetype) & JFFS2_COMPAT_MASK) {
588 			case JFFS2_FEATURE_ROCOMPAT:
589 				printk(KERN_NOTICE "Read-only compatible feature node (0x%04x) found at offset 0x%08x\n", je16_to_cpu(node->nodetype), ofs);
590 			        c->flags |= JFFS2_SB_FLAG_RO;
591 				if (!(jffs2_is_readonly(c)))
592 					return -EROFS;
593 				DIRTY_SPACE(PAD(je32_to_cpu(node->totlen)));
594 				ofs += PAD(je32_to_cpu(node->totlen));
595 				break;
596 
597 			case JFFS2_FEATURE_INCOMPAT:
598 				printk(KERN_NOTICE "Incompatible feature node (0x%04x) found at offset 0x%08x\n", je16_to_cpu(node->nodetype), ofs);
599 				return -EINVAL;
600 
601 			case JFFS2_FEATURE_RWCOMPAT_DELETE:
602 				D1(printk(KERN_NOTICE "Unknown but compatible feature node (0x%04x) found at offset 0x%08x\n", je16_to_cpu(node->nodetype), ofs));
603 				DIRTY_SPACE(PAD(je32_to_cpu(node->totlen)));
604 				ofs += PAD(je32_to_cpu(node->totlen));
605 				break;
606 
607 			case JFFS2_FEATURE_RWCOMPAT_COPY:
608 				D1(printk(KERN_NOTICE "Unknown but compatible feature node (0x%04x) found at offset 0x%08x\n", je16_to_cpu(node->nodetype), ofs));
609 				USED_SPACE(PAD(je32_to_cpu(node->totlen)));
610 				ofs += PAD(je32_to_cpu(node->totlen));
611 				break;
612 			}
613 		}
614 	}
615 
616 
617 	D1(printk(KERN_DEBUG "Block at 0x%08x: free 0x%08x, dirty 0x%08x, unchecked 0x%08x, used 0x%08x\n", jeb->offset,
618 		  jeb->free_size, jeb->dirty_size, jeb->unchecked_size, jeb->used_size));
619 
620 	/* mark_node_obsolete can add to wasted !! */
621 	if (jeb->wasted_size) {
622 		jeb->dirty_size += jeb->wasted_size;
623 		c->dirty_size += jeb->wasted_size;
624 		c->wasted_size -= jeb->wasted_size;
625 		jeb->wasted_size = 0;
626 	}
627 
628 	if ((jeb->used_size + jeb->unchecked_size) == PAD(c->cleanmarker_size) && !jeb->dirty_size
629 		&& (!jeb->first_node || !jeb->first_node->next_in_ino) )
630 		return BLK_STATE_CLEANMARKER;
631 
632 	/* move blocks with max 4 byte dirty space to cleanlist */
633 	else if (!ISDIRTY(c->sector_size - (jeb->used_size + jeb->unchecked_size))) {
634 		c->dirty_size -= jeb->dirty_size;
635 		c->wasted_size += jeb->dirty_size;
636 		jeb->wasted_size += jeb->dirty_size;
637 		jeb->dirty_size = 0;
638 		return BLK_STATE_CLEAN;
639 	} else if (jeb->used_size || jeb->unchecked_size)
640 		return BLK_STATE_PARTDIRTY;
641 	else
642 		return BLK_STATE_ALLDIRTY;
643 }
644 
645 static struct jffs2_inode_cache *jffs2_scan_make_ino_cache(struct jffs2_sb_info *c, uint32_t ino)
646 {
647 	struct jffs2_inode_cache *ic;
648 
649 	ic = jffs2_get_ino_cache(c, ino);
650 	if (ic)
651 		return ic;
652 
653 	if (ino > c->highest_ino)
654 		c->highest_ino = ino;
655 
656 	ic = jffs2_alloc_inode_cache();
657 	if (!ic) {
658 		printk(KERN_NOTICE "jffs2_scan_make_inode_cache(): allocation of inode cache failed\n");
659 		return NULL;
660 	}
661 	memset(ic, 0, sizeof(*ic));
662 
663 	ic->ino = ino;
664 	ic->nodes = (void *)ic;
665 	jffs2_add_ino_cache(c, ic);
666 	if (ino == 1)
667 		ic->nlink = 1;
668 	return ic;
669 }
670 
671 static int jffs2_scan_inode_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
672 				 struct jffs2_raw_inode *ri, uint32_t ofs)
673 {
674 	struct jffs2_raw_node_ref *raw;
675 	struct jffs2_inode_cache *ic;
676 	uint32_t ino = je32_to_cpu(ri->ino);
677 
678 	D1(printk(KERN_DEBUG "jffs2_scan_inode_node(): Node at 0x%08x\n", ofs));
679 
680 	/* We do very little here now. Just check the ino# to which we should attribute
681 	   this node; we can do all the CRC checking etc. later. There's a tradeoff here --
682 	   we used to scan the flash once only, reading everything we want from it into
683 	   memory, then building all our in-core data structures and freeing the extra
684 	   information. Now we allow the first part of the mount to complete a lot quicker,
685 	   but we have to go _back_ to the flash in order to finish the CRC checking, etc.
686 	   Which means that the _full_ amount of time to get to proper write mode with GC
687 	   operational may actually be _longer_ than before. Sucks to be me. */
688 
689 	raw = jffs2_alloc_raw_node_ref();
690 	if (!raw) {
691 		printk(KERN_NOTICE "jffs2_scan_inode_node(): allocation of node reference failed\n");
692 		return -ENOMEM;
693 	}
694 
695 	ic = jffs2_get_ino_cache(c, ino);
696 	if (!ic) {
697 		/* Inocache get failed. Either we read a bogus ino# or it's just genuinely the
698 		   first node we found for this inode. Do a CRC check to protect against the former
699 		   case */
700 		uint32_t crc = crc32(0, ri, sizeof(*ri)-8);
701 
702 		if (crc != je32_to_cpu(ri->node_crc)) {
703 			printk(KERN_NOTICE "jffs2_scan_inode_node(): CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
704 			       ofs, je32_to_cpu(ri->node_crc), crc);
705 			/* We believe totlen because the CRC on the node _header_ was OK, just the node itself failed. */
706 			DIRTY_SPACE(PAD(je32_to_cpu(ri->totlen)));
707 			jffs2_free_raw_node_ref(raw);
708 			return 0;
709 		}
710 		ic = jffs2_scan_make_ino_cache(c, ino);
711 		if (!ic) {
712 			jffs2_free_raw_node_ref(raw);
713 			return -ENOMEM;
714 		}
715 	}
716 
717 	/* Wheee. It worked */
718 
719 	raw->flash_offset = ofs | REF_UNCHECKED;
720 	raw->__totlen = PAD(je32_to_cpu(ri->totlen));
721 	raw->next_phys = NULL;
722 	raw->next_in_ino = ic->nodes;
723 
724 	ic->nodes = raw;
725 	if (!jeb->first_node)
726 		jeb->first_node = raw;
727 	if (jeb->last_node)
728 		jeb->last_node->next_phys = raw;
729 	jeb->last_node = raw;
730 
731 	D1(printk(KERN_DEBUG "Node is ino #%u, version %d. Range 0x%x-0x%x\n",
732 		  je32_to_cpu(ri->ino), je32_to_cpu(ri->version),
733 		  je32_to_cpu(ri->offset),
734 		  je32_to_cpu(ri->offset)+je32_to_cpu(ri->dsize)));
735 
736 	pseudo_random += je32_to_cpu(ri->version);
737 
738 	UNCHECKED_SPACE(PAD(je32_to_cpu(ri->totlen)));
739 	return 0;
740 }
741 
742 static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
743 				  struct jffs2_raw_dirent *rd, uint32_t ofs)
744 {
745 	struct jffs2_raw_node_ref *raw;
746 	struct jffs2_full_dirent *fd;
747 	struct jffs2_inode_cache *ic;
748 	uint32_t crc;
749 
750 	D1(printk(KERN_DEBUG "jffs2_scan_dirent_node(): Node at 0x%08x\n", ofs));
751 
752 	/* We don't get here unless the node is still valid, so we don't have to
753 	   mask in the ACCURATE bit any more. */
754 	crc = crc32(0, rd, sizeof(*rd)-8);
755 
756 	if (crc != je32_to_cpu(rd->node_crc)) {
757 		printk(KERN_NOTICE "jffs2_scan_dirent_node(): Node CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
758 		       ofs, je32_to_cpu(rd->node_crc), crc);
759 		/* We believe totlen because the CRC on the node _header_ was OK, just the node itself failed. */
760 		DIRTY_SPACE(PAD(je32_to_cpu(rd->totlen)));
761 		return 0;
762 	}
763 
764 	pseudo_random += je32_to_cpu(rd->version);
765 
766 	fd = jffs2_alloc_full_dirent(rd->nsize+1);
767 	if (!fd) {
768 		return -ENOMEM;
769 	}
770 	memcpy(&fd->name, rd->name, rd->nsize);
771 	fd->name[rd->nsize] = 0;
772 
773 	crc = crc32(0, fd->name, rd->nsize);
774 	if (crc != je32_to_cpu(rd->name_crc)) {
775 		printk(KERN_NOTICE "jffs2_scan_dirent_node(): Name CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
776 		       ofs, je32_to_cpu(rd->name_crc), crc);
777 		D1(printk(KERN_NOTICE "Name for which CRC failed is (now) '%s', ino #%d\n", fd->name, je32_to_cpu(rd->ino)));
778 		jffs2_free_full_dirent(fd);
779 		/* FIXME: Why do we believe totlen? */
780 		/* We believe totlen because the CRC on the node _header_ was OK, just the name failed. */
781 		DIRTY_SPACE(PAD(je32_to_cpu(rd->totlen)));
782 		return 0;
783 	}
784 	raw = jffs2_alloc_raw_node_ref();
785 	if (!raw) {
786 		jffs2_free_full_dirent(fd);
787 		printk(KERN_NOTICE "jffs2_scan_dirent_node(): allocation of node reference failed\n");
788 		return -ENOMEM;
789 	}
790 	ic = jffs2_scan_make_ino_cache(c, je32_to_cpu(rd->pino));
791 	if (!ic) {
792 		jffs2_free_full_dirent(fd);
793 		jffs2_free_raw_node_ref(raw);
794 		return -ENOMEM;
795 	}
796 
797 	raw->__totlen = PAD(je32_to_cpu(rd->totlen));
798 	raw->flash_offset = ofs | REF_PRISTINE;
799 	raw->next_phys = NULL;
800 	raw->next_in_ino = ic->nodes;
801 	ic->nodes = raw;
802 	if (!jeb->first_node)
803 		jeb->first_node = raw;
804 	if (jeb->last_node)
805 		jeb->last_node->next_phys = raw;
806 	jeb->last_node = raw;
807 
808 	fd->raw = raw;
809 	fd->next = NULL;
810 	fd->version = je32_to_cpu(rd->version);
811 	fd->ino = je32_to_cpu(rd->ino);
812 	fd->nhash = full_name_hash(fd->name, rd->nsize);
813 	fd->type = rd->type;
814 	USED_SPACE(PAD(je32_to_cpu(rd->totlen)));
815 	jffs2_add_fd_to_list(c, fd, &ic->scan_dents);
816 
817 	return 0;
818 }
819 
820 static int count_list(struct list_head *l)
821 {
822 	uint32_t count = 0;
823 	struct list_head *tmp;
824 
825 	list_for_each(tmp, l) {
826 		count++;
827 	}
828 	return count;
829 }
830 
831 /* Note: This breaks if list_empty(head). I don't care. You
832    might, if you copy this code and use it elsewhere :) */
833 static void rotate_list(struct list_head *head, uint32_t count)
834 {
835 	struct list_head *n = head->next;
836 
837 	list_del(head);
838 	while(count--) {
839 		n = n->next;
840 	}
841 	list_add(head, n);
842 }
843 
844 void jffs2_rotate_lists(struct jffs2_sb_info *c)
845 {
846 	uint32_t x;
847 	uint32_t rotateby;
848 
849 	x = count_list(&c->clean_list);
850 	if (x) {
851 		rotateby = pseudo_random % x;
852 		D1(printk(KERN_DEBUG "Rotating clean_list by %d\n", rotateby));
853 
854 		rotate_list((&c->clean_list), rotateby);
855 
856 		D1(printk(KERN_DEBUG "Erase block at front of clean_list is at %08x\n",
857 			  list_entry(c->clean_list.next, struct jffs2_eraseblock, list)->offset));
858 	} else {
859 		D1(printk(KERN_DEBUG "Not rotating empty clean_list\n"));
860 	}
861 
862 	x = count_list(&c->very_dirty_list);
863 	if (x) {
864 		rotateby = pseudo_random % x;
865 		D1(printk(KERN_DEBUG "Rotating very_dirty_list by %d\n", rotateby));
866 
867 		rotate_list((&c->very_dirty_list), rotateby);
868 
869 		D1(printk(KERN_DEBUG "Erase block at front of very_dirty_list is at %08x\n",
870 			  list_entry(c->very_dirty_list.next, struct jffs2_eraseblock, list)->offset));
871 	} else {
872 		D1(printk(KERN_DEBUG "Not rotating empty very_dirty_list\n"));
873 	}
874 
875 	x = count_list(&c->dirty_list);
876 	if (x) {
877 		rotateby = pseudo_random % x;
878 		D1(printk(KERN_DEBUG "Rotating dirty_list by %d\n", rotateby));
879 
880 		rotate_list((&c->dirty_list), rotateby);
881 
882 		D1(printk(KERN_DEBUG "Erase block at front of dirty_list is at %08x\n",
883 			  list_entry(c->dirty_list.next, struct jffs2_eraseblock, list)->offset));
884 	} else {
885 		D1(printk(KERN_DEBUG "Not rotating empty dirty_list\n"));
886 	}
887 
888 	x = count_list(&c->erasable_list);
889 	if (x) {
890 		rotateby = pseudo_random % x;
891 		D1(printk(KERN_DEBUG "Rotating erasable_list by %d\n", rotateby));
892 
893 		rotate_list((&c->erasable_list), rotateby);
894 
895 		D1(printk(KERN_DEBUG "Erase block at front of erasable_list is at %08x\n",
896 			  list_entry(c->erasable_list.next, struct jffs2_eraseblock, list)->offset));
897 	} else {
898 		D1(printk(KERN_DEBUG "Not rotating empty erasable_list\n"));
899 	}
900 
901 	if (c->nr_erasing_blocks) {
902 		rotateby = pseudo_random % c->nr_erasing_blocks;
903 		D1(printk(KERN_DEBUG "Rotating erase_pending_list by %d\n", rotateby));
904 
905 		rotate_list((&c->erase_pending_list), rotateby);
906 
907 		D1(printk(KERN_DEBUG "Erase block at front of erase_pending_list is at %08x\n",
908 			  list_entry(c->erase_pending_list.next, struct jffs2_eraseblock, list)->offset));
909 	} else {
910 		D1(printk(KERN_DEBUG "Not rotating empty erase_pending_list\n"));
911 	}
912 
913 	if (c->nr_free_blocks) {
914 		rotateby = pseudo_random % c->nr_free_blocks;
915 		D1(printk(KERN_DEBUG "Rotating free_list by %d\n", rotateby));
916 
917 		rotate_list((&c->free_list), rotateby);
918 
919 		D1(printk(KERN_DEBUG "Erase block at front of free_list is at %08x\n",
920 			  list_entry(c->free_list.next, struct jffs2_eraseblock, list)->offset));
921 	} else {
922 		D1(printk(KERN_DEBUG "Not rotating empty free_list\n"));
923 	}
924 }
925