source: rtems/cpukit/libfs/src/jffs2/src/scan.c @ 0282e83

4.115
Last change on this file since 0282e83 was 3c96bee, checked in by Sebastian Huber <sebastian.huber@…>, on 09/12/13 at 13:32:07

JFFS2: Add RTEMS support

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