xref: /openbmc/linux/fs/jffs2/scan.c (revision 9c261b33)
1 /*
2  * JFFS2 -- Journalling Flash File System, Version 2.
3  *
4  * Copyright © 2001-2007 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  */
11 
12 #include <linux/kernel.h>
13 #include <linux/sched.h>
14 #include <linux/slab.h>
15 #include <linux/mtd/mtd.h>
16 #include <linux/pagemap.h>
17 #include <linux/crc32.h>
18 #include <linux/compiler.h>
19 #include "nodelist.h"
20 #include "summary.h"
21 #include "debug.h"
22 
23 #define DEFAULT_EMPTY_SCAN_SIZE 256
24 
25 #define noisy_printk(noise, args...) do { \
26 	if (*(noise)) { \
27 		printk(KERN_NOTICE args); \
28 		 (*(noise))--; \
29 		 if (!(*(noise))) { \
30 			 printk(KERN_NOTICE "Further such events for this erase block will not be printed\n"); \
31 		 } \
32 	} \
33 } while(0)
34 
35 static uint32_t pseudo_random;
36 
37 static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
38 				  unsigned char *buf, uint32_t buf_size, struct jffs2_summary *s);
39 
40 /* These helper functions _must_ increase ofs and also do the dirty/used space accounting.
41  * Returning an error will abort the mount - bad checksums etc. should just mark the space
42  * as dirty.
43  */
44 static int jffs2_scan_inode_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
45 				 struct jffs2_raw_inode *ri, uint32_t ofs, struct jffs2_summary *s);
46 static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
47 				 struct jffs2_raw_dirent *rd, uint32_t ofs, struct jffs2_summary *s);
48 
49 static inline int min_free(struct jffs2_sb_info *c)
50 {
51 	uint32_t min = 2 * sizeof(struct jffs2_raw_inode);
52 #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
53 	if (!jffs2_can_mark_obsolete(c) && min < c->wbuf_pagesize)
54 		return c->wbuf_pagesize;
55 #endif
56 	return min;
57 
58 }
59 
60 static inline uint32_t EMPTY_SCAN_SIZE(uint32_t sector_size) {
61 	if (sector_size < DEFAULT_EMPTY_SCAN_SIZE)
62 		return sector_size;
63 	else
64 		return DEFAULT_EMPTY_SCAN_SIZE;
65 }
66 
67 static int file_dirty(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
68 {
69 	int ret;
70 
71 	if ((ret = jffs2_prealloc_raw_node_refs(c, jeb, 1)))
72 		return ret;
73 	if ((ret = jffs2_scan_dirty_space(c, jeb, jeb->free_size)))
74 		return ret;
75 	/* Turned wasted size into dirty, since we apparently
76 	   think it's recoverable now. */
77 	jeb->dirty_size += jeb->wasted_size;
78 	c->dirty_size += jeb->wasted_size;
79 	c->wasted_size -= jeb->wasted_size;
80 	jeb->wasted_size = 0;
81 	if (VERYDIRTY(c, jeb->dirty_size)) {
82 		list_add(&jeb->list, &c->very_dirty_list);
83 	} else {
84 		list_add(&jeb->list, &c->dirty_list);
85 	}
86 	return 0;
87 }
88 
89 int jffs2_scan_medium(struct jffs2_sb_info *c)
90 {
91 	int i, ret;
92 	uint32_t empty_blocks = 0, bad_blocks = 0;
93 	unsigned char *flashbuf = NULL;
94 	uint32_t buf_size = 0;
95 	struct jffs2_summary *s = NULL; /* summary info collected by the scan process */
96 #ifndef __ECOS
97 	size_t pointlen, try_size;
98 
99 	ret = mtd_point(c->mtd, 0, c->mtd->size, &pointlen,
100 			(void **)&flashbuf, NULL);
101 	if (!ret && pointlen < c->mtd->size) {
102 		/* Don't muck about if it won't let us point to the whole flash */
103 		jffs2_dbg(1, "MTD point returned len too short: 0x%zx\n",
104 			  pointlen);
105 		mtd_unpoint(c->mtd, 0, pointlen);
106 		flashbuf = NULL;
107 	}
108 	if (ret && ret != -EOPNOTSUPP)
109 		jffs2_dbg(1, "MTD point failed %d\n", ret);
110 #endif
111 	if (!flashbuf) {
112 		/* For NAND it's quicker to read a whole eraseblock at a time,
113 		   apparently */
114 		if (jffs2_cleanmarker_oob(c))
115 			try_size = c->sector_size;
116 		else
117 			try_size = PAGE_SIZE;
118 
119 		jffs2_dbg(1, "Trying to allocate readbuf of %zu "
120 			  "bytes\n", try_size);
121 
122 		flashbuf = mtd_kmalloc_up_to(c->mtd, &try_size);
123 		if (!flashbuf)
124 			return -ENOMEM;
125 
126 		jffs2_dbg(1, "Allocated readbuf of %zu bytes\n",
127 			  try_size);
128 
129 		buf_size = (uint32_t)try_size;
130 	}
131 
132 	if (jffs2_sum_active()) {
133 		s = kzalloc(sizeof(struct jffs2_summary), GFP_KERNEL);
134 		if (!s) {
135 			JFFS2_WARNING("Can't allocate memory for summary\n");
136 			ret = -ENOMEM;
137 			goto out;
138 		}
139 	}
140 
141 	for (i=0; i<c->nr_blocks; i++) {
142 		struct jffs2_eraseblock *jeb = &c->blocks[i];
143 
144 		cond_resched();
145 
146 		/* reset summary info for next eraseblock scan */
147 		jffs2_sum_reset_collected(s);
148 
149 		ret = jffs2_scan_eraseblock(c, jeb, buf_size?flashbuf:(flashbuf+jeb->offset),
150 						buf_size, s);
151 
152 		if (ret < 0)
153 			goto out;
154 
155 		jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
156 
157 		/* Now decide which list to put it on */
158 		switch(ret) {
159 		case BLK_STATE_ALLFF:
160 			/*
161 			 * Empty block.   Since we can't be sure it
162 			 * was entirely erased, we just queue it for erase
163 			 * again.  It will be marked as such when the erase
164 			 * is complete.  Meanwhile we still count it as empty
165 			 * for later checks.
166 			 */
167 			empty_blocks++;
168 			list_add(&jeb->list, &c->erase_pending_list);
169 			c->nr_erasing_blocks++;
170 			break;
171 
172 		case BLK_STATE_CLEANMARKER:
173 			/* Only a CLEANMARKER node is valid */
174 			if (!jeb->dirty_size) {
175 				/* It's actually free */
176 				list_add(&jeb->list, &c->free_list);
177 				c->nr_free_blocks++;
178 			} else {
179 				/* Dirt */
180 				jffs2_dbg(1, "Adding all-dirty block at 0x%08x to erase_pending_list\n",
181 					  jeb->offset);
182 				list_add(&jeb->list, &c->erase_pending_list);
183 				c->nr_erasing_blocks++;
184 			}
185 			break;
186 
187 		case BLK_STATE_CLEAN:
188 			/* Full (or almost full) of clean data. Clean list */
189 			list_add(&jeb->list, &c->clean_list);
190 			break;
191 
192 		case BLK_STATE_PARTDIRTY:
193 			/* Some data, but not full. Dirty list. */
194 			/* We want to remember the block with most free space
195 			and stick it in the 'nextblock' position to start writing to it. */
196 			if (jeb->free_size > min_free(c) &&
197 					(!c->nextblock || c->nextblock->free_size < jeb->free_size)) {
198 				/* Better candidate for the next writes to go to */
199 				if (c->nextblock) {
200 					ret = file_dirty(c, c->nextblock);
201 					if (ret)
202 						goto out;
203 					/* deleting summary information of the old nextblock */
204 					jffs2_sum_reset_collected(c->summary);
205 				}
206 				/* update collected summary information for the current nextblock */
207 				jffs2_sum_move_collected(c, s);
208 				jffs2_dbg(1, "%s(): new nextblock = 0x%08x\n",
209 					  __func__, jeb->offset);
210 				c->nextblock = jeb;
211 			} else {
212 				ret = file_dirty(c, jeb);
213 				if (ret)
214 					goto out;
215 			}
216 			break;
217 
218 		case BLK_STATE_ALLDIRTY:
219 			/* Nothing valid - not even a clean marker. Needs erasing. */
220 			/* For now we just put it on the erasing list. We'll start the erases later */
221 			jffs2_dbg(1, "JFFS2: Erase block at 0x%08x is not formatted. It will be erased\n",
222 				  jeb->offset);
223 			list_add(&jeb->list, &c->erase_pending_list);
224 			c->nr_erasing_blocks++;
225 			break;
226 
227 		case BLK_STATE_BADBLOCK:
228 			jffs2_dbg(1, "JFFS2: Block at 0x%08x is bad\n",
229 				  jeb->offset);
230 			list_add(&jeb->list, &c->bad_list);
231 			c->bad_size += c->sector_size;
232 			c->free_size -= c->sector_size;
233 			bad_blocks++;
234 			break;
235 		default:
236 			printk(KERN_WARNING "jffs2_scan_medium(): unknown block state\n");
237 			BUG();
238 		}
239 	}
240 
241 	/* Nextblock dirty is always seen as wasted, because we cannot recycle it now */
242 	if (c->nextblock && (c->nextblock->dirty_size)) {
243 		c->nextblock->wasted_size += c->nextblock->dirty_size;
244 		c->wasted_size += c->nextblock->dirty_size;
245 		c->dirty_size -= c->nextblock->dirty_size;
246 		c->nextblock->dirty_size = 0;
247 	}
248 #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
249 	if (!jffs2_can_mark_obsolete(c) && c->wbuf_pagesize && c->nextblock && (c->nextblock->free_size % c->wbuf_pagesize)) {
250 		/* If we're going to start writing into a block which already
251 		   contains data, and the end of the data isn't page-aligned,
252 		   skip a little and align it. */
253 
254 		uint32_t skip = c->nextblock->free_size % c->wbuf_pagesize;
255 
256 		jffs2_dbg(1, "%s(): Skipping %d bytes in nextblock to ensure page alignment\n",
257 			  __func__, skip);
258 		jffs2_prealloc_raw_node_refs(c, c->nextblock, 1);
259 		jffs2_scan_dirty_space(c, c->nextblock, skip);
260 	}
261 #endif
262 	if (c->nr_erasing_blocks) {
263 		if ( !c->used_size && ((c->nr_free_blocks+empty_blocks+bad_blocks)!= c->nr_blocks || bad_blocks == c->nr_blocks) ) {
264 			printk(KERN_NOTICE "Cowardly refusing to erase blocks on filesystem with no valid JFFS2 nodes\n");
265 			printk(KERN_NOTICE "empty_blocks %d, bad_blocks %d, c->nr_blocks %d\n",empty_blocks,bad_blocks,c->nr_blocks);
266 			ret = -EIO;
267 			goto out;
268 		}
269 		spin_lock(&c->erase_completion_lock);
270 		jffs2_garbage_collect_trigger(c);
271 		spin_unlock(&c->erase_completion_lock);
272 	}
273 	ret = 0;
274  out:
275 	if (buf_size)
276 		kfree(flashbuf);
277 #ifndef __ECOS
278 	else
279 		mtd_unpoint(c->mtd, 0, c->mtd->size);
280 #endif
281 	kfree(s);
282 	return ret;
283 }
284 
285 static int jffs2_fill_scan_buf(struct jffs2_sb_info *c, void *buf,
286 			       uint32_t ofs, uint32_t len)
287 {
288 	int ret;
289 	size_t retlen;
290 
291 	ret = jffs2_flash_read(c, ofs, len, &retlen, buf);
292 	if (ret) {
293 		jffs2_dbg(1, "mtd->read(0x%x bytes from 0x%x) returned %d\n",
294 			  len, ofs, ret);
295 		return ret;
296 	}
297 	if (retlen < len) {
298 		jffs2_dbg(1, "Read at 0x%x gave only 0x%zx bytes\n",
299 			  ofs, retlen);
300 		return -EIO;
301 	}
302 	return 0;
303 }
304 
305 int jffs2_scan_classify_jeb(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
306 {
307 	if ((jeb->used_size + jeb->unchecked_size) == PAD(c->cleanmarker_size) && !jeb->dirty_size
308 	    && (!jeb->first_node || !ref_next(jeb->first_node)) )
309 		return BLK_STATE_CLEANMARKER;
310 
311 	/* move blocks with max 4 byte dirty space to cleanlist */
312 	else if (!ISDIRTY(c->sector_size - (jeb->used_size + jeb->unchecked_size))) {
313 		c->dirty_size -= jeb->dirty_size;
314 		c->wasted_size += jeb->dirty_size;
315 		jeb->wasted_size += jeb->dirty_size;
316 		jeb->dirty_size = 0;
317 		return BLK_STATE_CLEAN;
318 	} else if (jeb->used_size || jeb->unchecked_size)
319 		return BLK_STATE_PARTDIRTY;
320 	else
321 		return BLK_STATE_ALLDIRTY;
322 }
323 
324 #ifdef CONFIG_JFFS2_FS_XATTR
325 static int jffs2_scan_xattr_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
326 				 struct jffs2_raw_xattr *rx, uint32_t ofs,
327 				 struct jffs2_summary *s)
328 {
329 	struct jffs2_xattr_datum *xd;
330 	uint32_t xid, version, totlen, crc;
331 	int err;
332 
333 	crc = crc32(0, rx, sizeof(struct jffs2_raw_xattr) - 4);
334 	if (crc != je32_to_cpu(rx->node_crc)) {
335 		JFFS2_WARNING("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
336 			      ofs, je32_to_cpu(rx->node_crc), crc);
337 		if ((err = jffs2_scan_dirty_space(c, jeb, je32_to_cpu(rx->totlen))))
338 			return err;
339 		return 0;
340 	}
341 
342 	xid = je32_to_cpu(rx->xid);
343 	version = je32_to_cpu(rx->version);
344 
345 	totlen = PAD(sizeof(struct jffs2_raw_xattr)
346 			+ rx->name_len + 1 + je16_to_cpu(rx->value_len));
347 	if (totlen != je32_to_cpu(rx->totlen)) {
348 		JFFS2_WARNING("node length mismatch at %#08x, read=%u, calc=%u\n",
349 			      ofs, je32_to_cpu(rx->totlen), totlen);
350 		if ((err = jffs2_scan_dirty_space(c, jeb, je32_to_cpu(rx->totlen))))
351 			return err;
352 		return 0;
353 	}
354 
355 	xd = jffs2_setup_xattr_datum(c, xid, version);
356 	if (IS_ERR(xd))
357 		return PTR_ERR(xd);
358 
359 	if (xd->version > version) {
360 		struct jffs2_raw_node_ref *raw
361 			= jffs2_link_node_ref(c, jeb, ofs | REF_PRISTINE, totlen, NULL);
362 		raw->next_in_ino = xd->node->next_in_ino;
363 		xd->node->next_in_ino = raw;
364 	} else {
365 		xd->version = version;
366 		xd->xprefix = rx->xprefix;
367 		xd->name_len = rx->name_len;
368 		xd->value_len = je16_to_cpu(rx->value_len);
369 		xd->data_crc = je32_to_cpu(rx->data_crc);
370 
371 		jffs2_link_node_ref(c, jeb, ofs | REF_PRISTINE, totlen, (void *)xd);
372 	}
373 
374 	if (jffs2_sum_active())
375 		jffs2_sum_add_xattr_mem(s, rx, ofs - jeb->offset);
376 	dbg_xattr("scanning xdatum at %#08x (xid=%u, version=%u)\n",
377 		  ofs, xd->xid, xd->version);
378 	return 0;
379 }
380 
381 static int jffs2_scan_xref_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
382 				struct jffs2_raw_xref *rr, uint32_t ofs,
383 				struct jffs2_summary *s)
384 {
385 	struct jffs2_xattr_ref *ref;
386 	uint32_t crc;
387 	int err;
388 
389 	crc = crc32(0, rr, sizeof(*rr) - 4);
390 	if (crc != je32_to_cpu(rr->node_crc)) {
391 		JFFS2_WARNING("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
392 			      ofs, je32_to_cpu(rr->node_crc), crc);
393 		if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(rr->totlen)))))
394 			return err;
395 		return 0;
396 	}
397 
398 	if (PAD(sizeof(struct jffs2_raw_xref)) != je32_to_cpu(rr->totlen)) {
399 		JFFS2_WARNING("node length mismatch at %#08x, read=%u, calc=%zd\n",
400 			      ofs, je32_to_cpu(rr->totlen),
401 			      PAD(sizeof(struct jffs2_raw_xref)));
402 		if ((err = jffs2_scan_dirty_space(c, jeb, je32_to_cpu(rr->totlen))))
403 			return err;
404 		return 0;
405 	}
406 
407 	ref = jffs2_alloc_xattr_ref();
408 	if (!ref)
409 		return -ENOMEM;
410 
411 	/* BEFORE jffs2_build_xattr_subsystem() called,
412 	 * and AFTER xattr_ref is marked as a dead xref,
413 	 * ref->xid is used to store 32bit xid, xd is not used
414 	 * ref->ino is used to store 32bit inode-number, ic is not used
415 	 * Thoes variables are declared as union, thus using those
416 	 * are exclusive. In a similar way, ref->next is temporarily
417 	 * used to chain all xattr_ref object. It's re-chained to
418 	 * jffs2_inode_cache in jffs2_build_xattr_subsystem() correctly.
419 	 */
420 	ref->ino = je32_to_cpu(rr->ino);
421 	ref->xid = je32_to_cpu(rr->xid);
422 	ref->xseqno = je32_to_cpu(rr->xseqno);
423 	if (ref->xseqno > c->highest_xseqno)
424 		c->highest_xseqno = (ref->xseqno & ~XREF_DELETE_MARKER);
425 	ref->next = c->xref_temp;
426 	c->xref_temp = ref;
427 
428 	jffs2_link_node_ref(c, jeb, ofs | REF_PRISTINE, PAD(je32_to_cpu(rr->totlen)), (void *)ref);
429 
430 	if (jffs2_sum_active())
431 		jffs2_sum_add_xref_mem(s, rr, ofs - jeb->offset);
432 	dbg_xattr("scan xref at %#08x (xid=%u, ino=%u)\n",
433 		  ofs, ref->xid, ref->ino);
434 	return 0;
435 }
436 #endif
437 
438 /* Called with 'buf_size == 0' if buf is in fact a pointer _directly_ into
439    the flash, XIP-style */
440 static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
441 				  unsigned char *buf, uint32_t buf_size, struct jffs2_summary *s) {
442 	struct jffs2_unknown_node *node;
443 	struct jffs2_unknown_node crcnode;
444 	uint32_t ofs, prevofs, max_ofs;
445 	uint32_t hdr_crc, buf_ofs, buf_len;
446 	int err;
447 	int noise = 0;
448 
449 
450 #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
451 	int cleanmarkerfound = 0;
452 #endif
453 
454 	ofs = jeb->offset;
455 	prevofs = jeb->offset - 1;
456 
457 	jffs2_dbg(1, "%s(): Scanning block at 0x%x\n", __func__, ofs);
458 
459 #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
460 	if (jffs2_cleanmarker_oob(c)) {
461 		int ret;
462 
463 		if (mtd_block_isbad(c->mtd, jeb->offset))
464 			return BLK_STATE_BADBLOCK;
465 
466 		ret = jffs2_check_nand_cleanmarker(c, jeb);
467 		jffs2_dbg(2, "jffs_check_nand_cleanmarker returned %d\n", ret);
468 
469 		/* Even if it's not found, we still scan to see
470 		   if the block is empty. We use this information
471 		   to decide whether to erase it or not. */
472 		switch (ret) {
473 		case 0:		cleanmarkerfound = 1; break;
474 		case 1: 	break;
475 		default: 	return ret;
476 		}
477 	}
478 #endif
479 
480 	if (jffs2_sum_active()) {
481 		struct jffs2_sum_marker *sm;
482 		void *sumptr = NULL;
483 		uint32_t sumlen;
484 
485 		if (!buf_size) {
486 			/* XIP case. Just look, point at the summary if it's there */
487 			sm = (void *)buf + c->sector_size - sizeof(*sm);
488 			if (je32_to_cpu(sm->magic) == JFFS2_SUM_MAGIC) {
489 				sumptr = buf + je32_to_cpu(sm->offset);
490 				sumlen = c->sector_size - je32_to_cpu(sm->offset);
491 			}
492 		} else {
493 			/* If NAND flash, read a whole page of it. Else just the end */
494 			if (c->wbuf_pagesize)
495 				buf_len = c->wbuf_pagesize;
496 			else
497 				buf_len = sizeof(*sm);
498 
499 			/* Read as much as we want into the _end_ of the preallocated buffer */
500 			err = jffs2_fill_scan_buf(c, buf + buf_size - buf_len,
501 						  jeb->offset + c->sector_size - buf_len,
502 						  buf_len);
503 			if (err)
504 				return err;
505 
506 			sm = (void *)buf + buf_size - sizeof(*sm);
507 			if (je32_to_cpu(sm->magic) == JFFS2_SUM_MAGIC) {
508 				sumlen = c->sector_size - je32_to_cpu(sm->offset);
509 				sumptr = buf + buf_size - sumlen;
510 
511 				/* Now, make sure the summary itself is available */
512 				if (sumlen > buf_size) {
513 					/* Need to kmalloc for this. */
514 					sumptr = kmalloc(sumlen, GFP_KERNEL);
515 					if (!sumptr)
516 						return -ENOMEM;
517 					memcpy(sumptr + sumlen - buf_len, buf + buf_size - buf_len, buf_len);
518 				}
519 				if (buf_len < sumlen) {
520 					/* Need to read more so that the entire summary node is present */
521 					err = jffs2_fill_scan_buf(c, sumptr,
522 								  jeb->offset + c->sector_size - sumlen,
523 								  sumlen - buf_len);
524 					if (err)
525 						return err;
526 				}
527 			}
528 
529 		}
530 
531 		if (sumptr) {
532 			err = jffs2_sum_scan_sumnode(c, jeb, sumptr, sumlen, &pseudo_random);
533 
534 			if (buf_size && sumlen > buf_size)
535 				kfree(sumptr);
536 			/* If it returns with a real error, bail.
537 			   If it returns positive, that's a block classification
538 			   (i.e. BLK_STATE_xxx) so return that too.
539 			   If it returns zero, fall through to full scan. */
540 			if (err)
541 				return err;
542 		}
543 	}
544 
545 	buf_ofs = jeb->offset;
546 
547 	if (!buf_size) {
548 		/* This is the XIP case -- we're reading _directly_ from the flash chip */
549 		buf_len = c->sector_size;
550 	} else {
551 		buf_len = EMPTY_SCAN_SIZE(c->sector_size);
552 		err = jffs2_fill_scan_buf(c, buf, buf_ofs, buf_len);
553 		if (err)
554 			return err;
555 	}
556 
557 	/* We temporarily use 'ofs' as a pointer into the buffer/jeb */
558 	ofs = 0;
559 	max_ofs = EMPTY_SCAN_SIZE(c->sector_size);
560 	/* Scan only EMPTY_SCAN_SIZE of 0xFF before declaring it's empty */
561 	while(ofs < max_ofs && *(uint32_t *)(&buf[ofs]) == 0xFFFFFFFF)
562 		ofs += 4;
563 
564 	if (ofs == max_ofs) {
565 #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
566 		if (jffs2_cleanmarker_oob(c)) {
567 			/* scan oob, take care of cleanmarker */
568 			int ret = jffs2_check_oob_empty(c, jeb, cleanmarkerfound);
569 			jffs2_dbg(2, "jffs2_check_oob_empty returned %d\n",
570 				  ret);
571 			switch (ret) {
572 			case 0:		return cleanmarkerfound ? BLK_STATE_CLEANMARKER : BLK_STATE_ALLFF;
573 			case 1: 	return BLK_STATE_ALLDIRTY;
574 			default: 	return ret;
575 			}
576 		}
577 #endif
578 		jffs2_dbg(1, "Block at 0x%08x is empty (erased)\n",
579 			  jeb->offset);
580 		if (c->cleanmarker_size == 0)
581 			return BLK_STATE_CLEANMARKER;	/* don't bother with re-erase */
582 		else
583 			return BLK_STATE_ALLFF;	/* OK to erase if all blocks are like this */
584 	}
585 	if (ofs) {
586 		jffs2_dbg(1, "Free space at %08x ends at %08x\n", jeb->offset,
587 			  jeb->offset + ofs);
588 		if ((err = jffs2_prealloc_raw_node_refs(c, jeb, 1)))
589 			return err;
590 		if ((err = jffs2_scan_dirty_space(c, jeb, ofs)))
591 			return err;
592 	}
593 
594 	/* Now ofs is a complete physical flash offset as it always was... */
595 	ofs += jeb->offset;
596 
597 	noise = 10;
598 
599 	dbg_summary("no summary found in jeb 0x%08x. Apply original scan.\n",jeb->offset);
600 
601 scan_more:
602 	while(ofs < jeb->offset + c->sector_size) {
603 
604 		jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
605 
606 		/* Make sure there are node refs available for use */
607 		err = jffs2_prealloc_raw_node_refs(c, jeb, 2);
608 		if (err)
609 			return err;
610 
611 		cond_resched();
612 
613 		if (ofs & 3) {
614 			printk(KERN_WARNING "Eep. ofs 0x%08x not word-aligned!\n", ofs);
615 			ofs = PAD(ofs);
616 			continue;
617 		}
618 		if (ofs == prevofs) {
619 			printk(KERN_WARNING "ofs 0x%08x has already been seen. Skipping\n", ofs);
620 			if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
621 				return err;
622 			ofs += 4;
623 			continue;
624 		}
625 		prevofs = ofs;
626 
627 		if (jeb->offset + c->sector_size < ofs + sizeof(*node)) {
628 			jffs2_dbg(1, "Fewer than %zd bytes left to end of block. (%x+%x<%x+%zx) Not reading\n",
629 				  sizeof(struct jffs2_unknown_node),
630 				  jeb->offset, c->sector_size, ofs,
631 				  sizeof(*node));
632 			if ((err = jffs2_scan_dirty_space(c, jeb, (jeb->offset + c->sector_size)-ofs)))
633 				return err;
634 			break;
635 		}
636 
637 		if (buf_ofs + buf_len < ofs + sizeof(*node)) {
638 			buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
639 			jffs2_dbg(1, "Fewer than %zd bytes (node header) left to end of buf. Reading 0x%x at 0x%08x\n",
640 				  sizeof(struct jffs2_unknown_node),
641 				  buf_len, ofs);
642 			err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
643 			if (err)
644 				return err;
645 			buf_ofs = ofs;
646 		}
647 
648 		node = (struct jffs2_unknown_node *)&buf[ofs-buf_ofs];
649 
650 		if (*(uint32_t *)(&buf[ofs-buf_ofs]) == 0xffffffff) {
651 			uint32_t inbuf_ofs;
652 			uint32_t empty_start, scan_end;
653 
654 			empty_start = ofs;
655 			ofs += 4;
656 			scan_end = min_t(uint32_t, EMPTY_SCAN_SIZE(c->sector_size)/8, buf_len);
657 
658 			jffs2_dbg(1, "Found empty flash at 0x%08x\n", ofs);
659 		more_empty:
660 			inbuf_ofs = ofs - buf_ofs;
661 			while (inbuf_ofs < scan_end) {
662 				if (unlikely(*(uint32_t *)(&buf[inbuf_ofs]) != 0xffffffff)) {
663 					printk(KERN_WARNING "Empty flash at 0x%08x ends at 0x%08x\n",
664 					       empty_start, ofs);
665 					if ((err = jffs2_scan_dirty_space(c, jeb, ofs-empty_start)))
666 						return err;
667 					goto scan_more;
668 				}
669 
670 				inbuf_ofs+=4;
671 				ofs += 4;
672 			}
673 			/* Ran off end. */
674 			jffs2_dbg(1, "Empty flash to end of buffer at 0x%08x\n",
675 				  ofs);
676 
677 			/* If we're only checking the beginning of a block with a cleanmarker,
678 			   bail now */
679 			if (buf_ofs == jeb->offset && jeb->used_size == PAD(c->cleanmarker_size) &&
680 			    c->cleanmarker_size && !jeb->dirty_size && !ref_next(jeb->first_node)) {
681 				jffs2_dbg(1, "%d bytes at start of block seems clean... assuming all clean\n",
682 					  EMPTY_SCAN_SIZE(c->sector_size));
683 				return BLK_STATE_CLEANMARKER;
684 			}
685 			if (!buf_size && (scan_end != buf_len)) {/* XIP/point case */
686 				scan_end = buf_len;
687 				goto more_empty;
688 			}
689 
690 			/* See how much more there is to read in this eraseblock... */
691 			buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
692 			if (!buf_len) {
693 				/* No more to read. Break out of main loop without marking
694 				   this range of empty space as dirty (because it's not) */
695 				jffs2_dbg(1, "Empty flash at %08x runs to end of block. Treating as free_space\n",
696 					  empty_start);
697 				break;
698 			}
699 			/* point never reaches here */
700 			scan_end = buf_len;
701 			jffs2_dbg(1, "Reading another 0x%x at 0x%08x\n",
702 				  buf_len, ofs);
703 			err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
704 			if (err)
705 				return err;
706 			buf_ofs = ofs;
707 			goto more_empty;
708 		}
709 
710 		if (ofs == jeb->offset && je16_to_cpu(node->magic) == KSAMTIB_CIGAM_2SFFJ) {
711 			printk(KERN_WARNING "Magic bitmask is backwards at offset 0x%08x. Wrong endian filesystem?\n", ofs);
712 			if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
713 				return err;
714 			ofs += 4;
715 			continue;
716 		}
717 		if (je16_to_cpu(node->magic) == JFFS2_DIRTY_BITMASK) {
718 			jffs2_dbg(1, "Dirty bitmask at 0x%08x\n", ofs);
719 			if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
720 				return err;
721 			ofs += 4;
722 			continue;
723 		}
724 		if (je16_to_cpu(node->magic) == JFFS2_OLD_MAGIC_BITMASK) {
725 			printk(KERN_WARNING "Old JFFS2 bitmask found at 0x%08x\n", ofs);
726 			printk(KERN_WARNING "You cannot use older JFFS2 filesystems with newer kernels\n");
727 			if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
728 				return err;
729 			ofs += 4;
730 			continue;
731 		}
732 		if (je16_to_cpu(node->magic) != JFFS2_MAGIC_BITMASK) {
733 			/* OK. We're out of possibilities. Whinge and move on */
734 			noisy_printk(&noise, "jffs2_scan_eraseblock(): Magic bitmask 0x%04x not found at 0x%08x: 0x%04x instead\n",
735 				     JFFS2_MAGIC_BITMASK, ofs,
736 				     je16_to_cpu(node->magic));
737 			if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
738 				return err;
739 			ofs += 4;
740 			continue;
741 		}
742 		/* We seem to have a node of sorts. Check the CRC */
743 		crcnode.magic = node->magic;
744 		crcnode.nodetype = cpu_to_je16( je16_to_cpu(node->nodetype) | JFFS2_NODE_ACCURATE);
745 		crcnode.totlen = node->totlen;
746 		hdr_crc = crc32(0, &crcnode, sizeof(crcnode)-4);
747 
748 		if (hdr_crc != je32_to_cpu(node->hdr_crc)) {
749 			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",
750 				     ofs, je16_to_cpu(node->magic),
751 				     je16_to_cpu(node->nodetype),
752 				     je32_to_cpu(node->totlen),
753 				     je32_to_cpu(node->hdr_crc),
754 				     hdr_crc);
755 			if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
756 				return err;
757 			ofs += 4;
758 			continue;
759 		}
760 
761 		if (ofs + je32_to_cpu(node->totlen) > jeb->offset + c->sector_size) {
762 			/* Eep. Node goes over the end of the erase block. */
763 			printk(KERN_WARNING "Node at 0x%08x with length 0x%08x would run over the end of the erase block\n",
764 			       ofs, je32_to_cpu(node->totlen));
765 			printk(KERN_WARNING "Perhaps the file system was created with the wrong erase size?\n");
766 			if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
767 				return err;
768 			ofs += 4;
769 			continue;
770 		}
771 
772 		if (!(je16_to_cpu(node->nodetype) & JFFS2_NODE_ACCURATE)) {
773 			/* Wheee. This is an obsoleted node */
774 			jffs2_dbg(2, "Node at 0x%08x is obsolete. Skipping\n",
775 				  ofs);
776 			if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen)))))
777 				return err;
778 			ofs += PAD(je32_to_cpu(node->totlen));
779 			continue;
780 		}
781 
782 		switch(je16_to_cpu(node->nodetype)) {
783 		case JFFS2_NODETYPE_INODE:
784 			if (buf_ofs + buf_len < ofs + sizeof(struct jffs2_raw_inode)) {
785 				buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
786 				jffs2_dbg(1, "Fewer than %zd bytes (inode node) left to end of buf. Reading 0x%x at 0x%08x\n",
787 					  sizeof(struct jffs2_raw_inode),
788 					  buf_len, ofs);
789 				err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
790 				if (err)
791 					return err;
792 				buf_ofs = ofs;
793 				node = (void *)buf;
794 			}
795 			err = jffs2_scan_inode_node(c, jeb, (void *)node, ofs, s);
796 			if (err) return err;
797 			ofs += PAD(je32_to_cpu(node->totlen));
798 			break;
799 
800 		case JFFS2_NODETYPE_DIRENT:
801 			if (buf_ofs + buf_len < ofs + je32_to_cpu(node->totlen)) {
802 				buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
803 				jffs2_dbg(1, "Fewer than %d bytes (dirent node) left to end of buf. Reading 0x%x at 0x%08x\n",
804 					  je32_to_cpu(node->totlen), buf_len,
805 					  ofs);
806 				err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
807 				if (err)
808 					return err;
809 				buf_ofs = ofs;
810 				node = (void *)buf;
811 			}
812 			err = jffs2_scan_dirent_node(c, jeb, (void *)node, ofs, s);
813 			if (err) return err;
814 			ofs += PAD(je32_to_cpu(node->totlen));
815 			break;
816 
817 #ifdef CONFIG_JFFS2_FS_XATTR
818 		case JFFS2_NODETYPE_XATTR:
819 			if (buf_ofs + buf_len < ofs + je32_to_cpu(node->totlen)) {
820 				buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
821 				jffs2_dbg(1, "Fewer than %d bytes (xattr node) left to end of buf. Reading 0x%x at 0x%08x\n",
822 					  je32_to_cpu(node->totlen), buf_len,
823 					  ofs);
824 				err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
825 				if (err)
826 					return err;
827 				buf_ofs = ofs;
828 				node = (void *)buf;
829 			}
830 			err = jffs2_scan_xattr_node(c, jeb, (void *)node, ofs, s);
831 			if (err)
832 				return err;
833 			ofs += PAD(je32_to_cpu(node->totlen));
834 			break;
835 		case JFFS2_NODETYPE_XREF:
836 			if (buf_ofs + buf_len < ofs + je32_to_cpu(node->totlen)) {
837 				buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
838 				jffs2_dbg(1, "Fewer than %d bytes (xref node) left to end of buf. Reading 0x%x at 0x%08x\n",
839 					  je32_to_cpu(node->totlen), buf_len,
840 					  ofs);
841 				err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
842 				if (err)
843 					return err;
844 				buf_ofs = ofs;
845 				node = (void *)buf;
846 			}
847 			err = jffs2_scan_xref_node(c, jeb, (void *)node, ofs, s);
848 			if (err)
849 				return err;
850 			ofs += PAD(je32_to_cpu(node->totlen));
851 			break;
852 #endif	/* CONFIG_JFFS2_FS_XATTR */
853 
854 		case JFFS2_NODETYPE_CLEANMARKER:
855 			jffs2_dbg(1, "CLEANMARKER node found at 0x%08x\n", ofs);
856 			if (je32_to_cpu(node->totlen) != c->cleanmarker_size) {
857 				printk(KERN_NOTICE "CLEANMARKER node found at 0x%08x has totlen 0x%x != normal 0x%x\n",
858 				       ofs, je32_to_cpu(node->totlen), c->cleanmarker_size);
859 				if ((err = jffs2_scan_dirty_space(c, jeb, PAD(sizeof(struct jffs2_unknown_node)))))
860 					return err;
861 				ofs += PAD(sizeof(struct jffs2_unknown_node));
862 			} else if (jeb->first_node) {
863 				printk(KERN_NOTICE "CLEANMARKER node found at 0x%08x, not first node in block (0x%08x)\n", ofs, jeb->offset);
864 				if ((err = jffs2_scan_dirty_space(c, jeb, PAD(sizeof(struct jffs2_unknown_node)))))
865 					return err;
866 				ofs += PAD(sizeof(struct jffs2_unknown_node));
867 			} else {
868 				jffs2_link_node_ref(c, jeb, ofs | REF_NORMAL, c->cleanmarker_size, NULL);
869 
870 				ofs += PAD(c->cleanmarker_size);
871 			}
872 			break;
873 
874 		case JFFS2_NODETYPE_PADDING:
875 			if (jffs2_sum_active())
876 				jffs2_sum_add_padding_mem(s, je32_to_cpu(node->totlen));
877 			if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen)))))
878 				return err;
879 			ofs += PAD(je32_to_cpu(node->totlen));
880 			break;
881 
882 		default:
883 			switch (je16_to_cpu(node->nodetype) & JFFS2_COMPAT_MASK) {
884 			case JFFS2_FEATURE_ROCOMPAT:
885 				printk(KERN_NOTICE "Read-only compatible feature node (0x%04x) found at offset 0x%08x\n", je16_to_cpu(node->nodetype), ofs);
886 				c->flags |= JFFS2_SB_FLAG_RO;
887 				if (!(jffs2_is_readonly(c)))
888 					return -EROFS;
889 				if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen)))))
890 					return err;
891 				ofs += PAD(je32_to_cpu(node->totlen));
892 				break;
893 
894 			case JFFS2_FEATURE_INCOMPAT:
895 				printk(KERN_NOTICE "Incompatible feature node (0x%04x) found at offset 0x%08x\n", je16_to_cpu(node->nodetype), ofs);
896 				return -EINVAL;
897 
898 			case JFFS2_FEATURE_RWCOMPAT_DELETE:
899 				jffs2_dbg(1, "Unknown but compatible feature node (0x%04x) found at offset 0x%08x\n",
900 					  je16_to_cpu(node->nodetype), ofs);
901 				if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen)))))
902 					return err;
903 				ofs += PAD(je32_to_cpu(node->totlen));
904 				break;
905 
906 			case JFFS2_FEATURE_RWCOMPAT_COPY: {
907 				jffs2_dbg(1, "Unknown but compatible feature node (0x%04x) found at offset 0x%08x\n",
908 					  je16_to_cpu(node->nodetype), ofs);
909 
910 				jffs2_link_node_ref(c, jeb, ofs | REF_PRISTINE, PAD(je32_to_cpu(node->totlen)), NULL);
911 
912 				/* We can't summarise nodes we don't grok */
913 				jffs2_sum_disable_collecting(s);
914 				ofs += PAD(je32_to_cpu(node->totlen));
915 				break;
916 				}
917 			}
918 		}
919 	}
920 
921 	if (jffs2_sum_active()) {
922 		if (PAD(s->sum_size + JFFS2_SUMMARY_FRAME_SIZE) > jeb->free_size) {
923 			dbg_summary("There is not enough space for "
924 				"summary information, disabling for this jeb!\n");
925 			jffs2_sum_disable_collecting(s);
926 		}
927 	}
928 
929 	jffs2_dbg(1, "Block at 0x%08x: free 0x%08x, dirty 0x%08x, unchecked 0x%08x, used 0x%08x, wasted 0x%08x\n",
930 		  jeb->offset, jeb->free_size, jeb->dirty_size,
931 		  jeb->unchecked_size, jeb->used_size, jeb->wasted_size);
932 
933 	/* mark_node_obsolete can add to wasted !! */
934 	if (jeb->wasted_size) {
935 		jeb->dirty_size += jeb->wasted_size;
936 		c->dirty_size += jeb->wasted_size;
937 		c->wasted_size -= jeb->wasted_size;
938 		jeb->wasted_size = 0;
939 	}
940 
941 	return jffs2_scan_classify_jeb(c, jeb);
942 }
943 
944 struct jffs2_inode_cache *jffs2_scan_make_ino_cache(struct jffs2_sb_info *c, uint32_t ino)
945 {
946 	struct jffs2_inode_cache *ic;
947 
948 	ic = jffs2_get_ino_cache(c, ino);
949 	if (ic)
950 		return ic;
951 
952 	if (ino > c->highest_ino)
953 		c->highest_ino = ino;
954 
955 	ic = jffs2_alloc_inode_cache();
956 	if (!ic) {
957 		printk(KERN_NOTICE "jffs2_scan_make_inode_cache(): allocation of inode cache failed\n");
958 		return NULL;
959 	}
960 	memset(ic, 0, sizeof(*ic));
961 
962 	ic->ino = ino;
963 	ic->nodes = (void *)ic;
964 	jffs2_add_ino_cache(c, ic);
965 	if (ino == 1)
966 		ic->pino_nlink = 1;
967 	return ic;
968 }
969 
970 static int jffs2_scan_inode_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
971 				 struct jffs2_raw_inode *ri, uint32_t ofs, struct jffs2_summary *s)
972 {
973 	struct jffs2_inode_cache *ic;
974 	uint32_t crc, ino = je32_to_cpu(ri->ino);
975 
976 	jffs2_dbg(1, "%s(): Node at 0x%08x\n", __func__, ofs);
977 
978 	/* We do very little here now. Just check the ino# to which we should attribute
979 	   this node; we can do all the CRC checking etc. later. There's a tradeoff here --
980 	   we used to scan the flash once only, reading everything we want from it into
981 	   memory, then building all our in-core data structures and freeing the extra
982 	   information. Now we allow the first part of the mount to complete a lot quicker,
983 	   but we have to go _back_ to the flash in order to finish the CRC checking, etc.
984 	   Which means that the _full_ amount of time to get to proper write mode with GC
985 	   operational may actually be _longer_ than before. Sucks to be me. */
986 
987 	/* Check the node CRC in any case. */
988 	crc = crc32(0, ri, sizeof(*ri)-8);
989 	if (crc != je32_to_cpu(ri->node_crc)) {
990 		printk(KERN_NOTICE "jffs2_scan_inode_node(): CRC failed on "
991 		       "node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
992 		       ofs, je32_to_cpu(ri->node_crc), crc);
993 		/*
994 		 * We believe totlen because the CRC on the node
995 		 * _header_ was OK, just the node itself failed.
996 		 */
997 		return jffs2_scan_dirty_space(c, jeb,
998 					      PAD(je32_to_cpu(ri->totlen)));
999 	}
1000 
1001 	ic = jffs2_get_ino_cache(c, ino);
1002 	if (!ic) {
1003 		ic = jffs2_scan_make_ino_cache(c, ino);
1004 		if (!ic)
1005 			return -ENOMEM;
1006 	}
1007 
1008 	/* Wheee. It worked */
1009 	jffs2_link_node_ref(c, jeb, ofs | REF_UNCHECKED, PAD(je32_to_cpu(ri->totlen)), ic);
1010 
1011 	jffs2_dbg(1, "Node is ino #%u, version %d. Range 0x%x-0x%x\n",
1012 		  je32_to_cpu(ri->ino), je32_to_cpu(ri->version),
1013 		  je32_to_cpu(ri->offset),
1014 		  je32_to_cpu(ri->offset)+je32_to_cpu(ri->dsize));
1015 
1016 	pseudo_random += je32_to_cpu(ri->version);
1017 
1018 	if (jffs2_sum_active()) {
1019 		jffs2_sum_add_inode_mem(s, ri, ofs - jeb->offset);
1020 	}
1021 
1022 	return 0;
1023 }
1024 
1025 static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
1026 				  struct jffs2_raw_dirent *rd, uint32_t ofs, struct jffs2_summary *s)
1027 {
1028 	struct jffs2_full_dirent *fd;
1029 	struct jffs2_inode_cache *ic;
1030 	uint32_t checkedlen;
1031 	uint32_t crc;
1032 	int err;
1033 
1034 	jffs2_dbg(1, "%s(): Node at 0x%08x\n", __func__, ofs);
1035 
1036 	/* We don't get here unless the node is still valid, so we don't have to
1037 	   mask in the ACCURATE bit any more. */
1038 	crc = crc32(0, rd, sizeof(*rd)-8);
1039 
1040 	if (crc != je32_to_cpu(rd->node_crc)) {
1041 		printk(KERN_NOTICE "jffs2_scan_dirent_node(): Node CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
1042 		       ofs, je32_to_cpu(rd->node_crc), crc);
1043 		/* We believe totlen because the CRC on the node _header_ was OK, just the node itself failed. */
1044 		if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(rd->totlen)))))
1045 			return err;
1046 		return 0;
1047 	}
1048 
1049 	pseudo_random += je32_to_cpu(rd->version);
1050 
1051 	/* Should never happen. Did. (OLPC trac #4184)*/
1052 	checkedlen = strnlen(rd->name, rd->nsize);
1053 	if (checkedlen < rd->nsize) {
1054 		printk(KERN_ERR "Dirent at %08x has zeroes in name. Truncating to %d chars\n",
1055 		       ofs, checkedlen);
1056 	}
1057 	fd = jffs2_alloc_full_dirent(checkedlen+1);
1058 	if (!fd) {
1059 		return -ENOMEM;
1060 	}
1061 	memcpy(&fd->name, rd->name, checkedlen);
1062 	fd->name[checkedlen] = 0;
1063 
1064 	crc = crc32(0, fd->name, rd->nsize);
1065 	if (crc != je32_to_cpu(rd->name_crc)) {
1066 		printk(KERN_NOTICE "jffs2_scan_dirent_node(): Name CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
1067 		       ofs, je32_to_cpu(rd->name_crc), crc);
1068 		jffs2_dbg(1, "Name for which CRC failed is (now) '%s', ino #%d\n",
1069 			  fd->name, je32_to_cpu(rd->ino));
1070 		jffs2_free_full_dirent(fd);
1071 		/* FIXME: Why do we believe totlen? */
1072 		/* We believe totlen because the CRC on the node _header_ was OK, just the name failed. */
1073 		if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(rd->totlen)))))
1074 			return err;
1075 		return 0;
1076 	}
1077 	ic = jffs2_scan_make_ino_cache(c, je32_to_cpu(rd->pino));
1078 	if (!ic) {
1079 		jffs2_free_full_dirent(fd);
1080 		return -ENOMEM;
1081 	}
1082 
1083 	fd->raw = jffs2_link_node_ref(c, jeb, ofs | dirent_node_state(rd),
1084 				      PAD(je32_to_cpu(rd->totlen)), ic);
1085 
1086 	fd->next = NULL;
1087 	fd->version = je32_to_cpu(rd->version);
1088 	fd->ino = je32_to_cpu(rd->ino);
1089 	fd->nhash = full_name_hash(fd->name, checkedlen);
1090 	fd->type = rd->type;
1091 	jffs2_add_fd_to_list(c, fd, &ic->scan_dents);
1092 
1093 	if (jffs2_sum_active()) {
1094 		jffs2_sum_add_dirent_mem(s, rd, ofs - jeb->offset);
1095 	}
1096 
1097 	return 0;
1098 }
1099 
1100 static int count_list(struct list_head *l)
1101 {
1102 	uint32_t count = 0;
1103 	struct list_head *tmp;
1104 
1105 	list_for_each(tmp, l) {
1106 		count++;
1107 	}
1108 	return count;
1109 }
1110 
1111 /* Note: This breaks if list_empty(head). I don't care. You
1112    might, if you copy this code and use it elsewhere :) */
1113 static void rotate_list(struct list_head *head, uint32_t count)
1114 {
1115 	struct list_head *n = head->next;
1116 
1117 	list_del(head);
1118 	while(count--) {
1119 		n = n->next;
1120 	}
1121 	list_add(head, n);
1122 }
1123 
1124 void jffs2_rotate_lists(struct jffs2_sb_info *c)
1125 {
1126 	uint32_t x;
1127 	uint32_t rotateby;
1128 
1129 	x = count_list(&c->clean_list);
1130 	if (x) {
1131 		rotateby = pseudo_random % x;
1132 		rotate_list((&c->clean_list), rotateby);
1133 	}
1134 
1135 	x = count_list(&c->very_dirty_list);
1136 	if (x) {
1137 		rotateby = pseudo_random % x;
1138 		rotate_list((&c->very_dirty_list), rotateby);
1139 	}
1140 
1141 	x = count_list(&c->dirty_list);
1142 	if (x) {
1143 		rotateby = pseudo_random % x;
1144 		rotate_list((&c->dirty_list), rotateby);
1145 	}
1146 
1147 	x = count_list(&c->erasable_list);
1148 	if (x) {
1149 		rotateby = pseudo_random % x;
1150 		rotate_list((&c->erasable_list), rotateby);
1151 	}
1152 
1153 	if (c->nr_erasing_blocks) {
1154 		rotateby = pseudo_random % c->nr_erasing_blocks;
1155 		rotate_list((&c->erase_pending_list), rotateby);
1156 	}
1157 
1158 	if (c->nr_free_blocks) {
1159 		rotateby = pseudo_random % c->nr_free_blocks;
1160 		rotate_list((&c->free_list), rotateby);
1161 	}
1162 }
1163