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