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