Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * JFFS2 -- Journalling Flash File System, Version 2.
0003  *
0004  * Copyright © 2001-2007 Red Hat, Inc.
0005  *
0006  * Created by David Woodhouse <dwmw2@infradead.org>
0007  *
0008  * For licensing information, see the file 'LICENCE' in this directory.
0009  *
0010  */
0011 
0012 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0013 
0014 #include <linux/kernel.h>
0015 #include <linux/sched.h>
0016 #include <linux/slab.h>
0017 #include <linux/mtd/mtd.h>
0018 #include <linux/pagemap.h>
0019 #include <linux/crc32.h>
0020 #include <linux/compiler.h>
0021 #include "nodelist.h"
0022 #include "summary.h"
0023 #include "debug.h"
0024 
0025 #define DEFAULT_EMPTY_SCAN_SIZE 256
0026 
0027 #define noisy_printk(noise, fmt, ...)                   \
0028 do {                                    \
0029     if (*(noise)) {                         \
0030         pr_notice(fmt, ##__VA_ARGS__);              \
0031         (*(noise))--;                       \
0032         if (!(*(noise)))                    \
0033             pr_notice("Further such events for this erase block will not be printed\n"); \
0034     }                               \
0035 } while (0)
0036 
0037 static uint32_t pseudo_random;
0038 
0039 static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
0040                   unsigned char *buf, uint32_t buf_size, struct jffs2_summary *s);
0041 
0042 /* These helper functions _must_ increase ofs and also do the dirty/used space accounting.
0043  * Returning an error will abort the mount - bad checksums etc. should just mark the space
0044  * as dirty.
0045  */
0046 static int jffs2_scan_inode_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
0047                  struct jffs2_raw_inode *ri, uint32_t ofs, struct jffs2_summary *s);
0048 static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
0049                  struct jffs2_raw_dirent *rd, uint32_t ofs, struct jffs2_summary *s);
0050 
0051 static inline int min_free(struct jffs2_sb_info *c)
0052 {
0053     uint32_t min = 2 * sizeof(struct jffs2_raw_inode);
0054 #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
0055     if (!jffs2_can_mark_obsolete(c) && min < c->wbuf_pagesize)
0056         return c->wbuf_pagesize;
0057 #endif
0058     return min;
0059 
0060 }
0061 
0062 static inline uint32_t EMPTY_SCAN_SIZE(uint32_t sector_size) {
0063     if (sector_size < DEFAULT_EMPTY_SCAN_SIZE)
0064         return sector_size;
0065     else
0066         return DEFAULT_EMPTY_SCAN_SIZE;
0067 }
0068 
0069 static int file_dirty(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
0070 {
0071     int ret;
0072 
0073     if ((ret = jffs2_prealloc_raw_node_refs(c, jeb, 1)))
0074         return ret;
0075     if ((ret = jffs2_scan_dirty_space(c, jeb, jeb->free_size)))
0076         return ret;
0077     /* Turned wasted size into dirty, since we apparently 
0078        think it's recoverable now. */
0079     jeb->dirty_size += jeb->wasted_size;
0080     c->dirty_size += jeb->wasted_size;
0081     c->wasted_size -= jeb->wasted_size;
0082     jeb->wasted_size = 0;
0083     if (VERYDIRTY(c, jeb->dirty_size)) {
0084         list_add(&jeb->list, &c->very_dirty_list);
0085     } else {
0086         list_add(&jeb->list, &c->dirty_list);
0087     }
0088     return 0;
0089 }
0090 
0091 int jffs2_scan_medium(struct jffs2_sb_info *c)
0092 {
0093     int i, ret;
0094     uint32_t empty_blocks = 0, bad_blocks = 0;
0095     unsigned char *flashbuf = NULL;
0096     uint32_t buf_size = 0;
0097     struct jffs2_summary *s = NULL; /* summary info collected by the scan process */
0098 #ifndef __ECOS
0099     size_t pointlen, try_size;
0100 
0101     ret = mtd_point(c->mtd, 0, c->mtd->size, &pointlen,
0102             (void **)&flashbuf, NULL);
0103     if (!ret && pointlen < c->mtd->size) {
0104         /* Don't muck about if it won't let us point to the whole flash */
0105         jffs2_dbg(1, "MTD point returned len too short: 0x%zx\n",
0106               pointlen);
0107         mtd_unpoint(c->mtd, 0, pointlen);
0108         flashbuf = NULL;
0109     }
0110     if (ret && ret != -EOPNOTSUPP)
0111         jffs2_dbg(1, "MTD point failed %d\n", ret);
0112 #endif
0113     if (!flashbuf) {
0114         /* For NAND it's quicker to read a whole eraseblock at a time,
0115            apparently */
0116         if (jffs2_cleanmarker_oob(c))
0117             try_size = c->sector_size;
0118         else
0119             try_size = PAGE_SIZE;
0120 
0121         jffs2_dbg(1, "Trying to allocate readbuf of %zu "
0122               "bytes\n", try_size);
0123 
0124         flashbuf = mtd_kmalloc_up_to(c->mtd, &try_size);
0125         if (!flashbuf)
0126             return -ENOMEM;
0127 
0128         jffs2_dbg(1, "Allocated readbuf of %zu bytes\n",
0129               try_size);
0130 
0131         buf_size = (uint32_t)try_size;
0132     }
0133 
0134     if (jffs2_sum_active()) {
0135         s = kzalloc(sizeof(struct jffs2_summary), GFP_KERNEL);
0136         if (!s) {
0137             JFFS2_WARNING("Can't allocate memory for summary\n");
0138             ret = -ENOMEM;
0139             goto out_buf;
0140         }
0141     }
0142 
0143     for (i=0; i<c->nr_blocks; i++) {
0144         struct jffs2_eraseblock *jeb = &c->blocks[i];
0145 
0146         cond_resched();
0147 
0148         /* reset summary info for next eraseblock scan */
0149         jffs2_sum_reset_collected(s);
0150 
0151         ret = jffs2_scan_eraseblock(c, jeb, buf_size?flashbuf:(flashbuf+jeb->offset),
0152                         buf_size, s);
0153 
0154         if (ret < 0)
0155             goto out;
0156 
0157         jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
0158 
0159         /* Now decide which list to put it on */
0160         switch(ret) {
0161         case BLK_STATE_ALLFF:
0162             /*
0163              * Empty block.   Since we can't be sure it
0164              * was entirely erased, we just queue it for erase
0165              * again.  It will be marked as such when the erase
0166              * is complete.  Meanwhile we still count it as empty
0167              * for later checks.
0168              */
0169             empty_blocks++;
0170             list_add(&jeb->list, &c->erase_pending_list);
0171             c->nr_erasing_blocks++;
0172             break;
0173 
0174         case BLK_STATE_CLEANMARKER:
0175             /* Only a CLEANMARKER node is valid */
0176             if (!jeb->dirty_size) {
0177                 /* It's actually free */
0178                 list_add(&jeb->list, &c->free_list);
0179                 c->nr_free_blocks++;
0180             } else {
0181                 /* Dirt */
0182                 jffs2_dbg(1, "Adding all-dirty block at 0x%08x to erase_pending_list\n",
0183                       jeb->offset);
0184                 list_add(&jeb->list, &c->erase_pending_list);
0185                 c->nr_erasing_blocks++;
0186             }
0187             break;
0188 
0189         case BLK_STATE_CLEAN:
0190             /* Full (or almost full) of clean data. Clean list */
0191             list_add(&jeb->list, &c->clean_list);
0192             break;
0193 
0194         case BLK_STATE_PARTDIRTY:
0195             /* Some data, but not full. Dirty list. */
0196             /* We want to remember the block with most free space
0197             and stick it in the 'nextblock' position to start writing to it. */
0198             if (jeb->free_size > min_free(c) &&
0199                     (!c->nextblock || c->nextblock->free_size < jeb->free_size)) {
0200                 /* Better candidate for the next writes to go to */
0201                 if (c->nextblock) {
0202                     ret = file_dirty(c, c->nextblock);
0203                     if (ret)
0204                         goto out;
0205                     /* deleting summary information of the old nextblock */
0206                     jffs2_sum_reset_collected(c->summary);
0207                 }
0208                 /* update collected summary information for the current nextblock */
0209                 jffs2_sum_move_collected(c, s);
0210                 jffs2_dbg(1, "%s(): new nextblock = 0x%08x\n",
0211                       __func__, jeb->offset);
0212                 c->nextblock = jeb;
0213             } else {
0214                 ret = file_dirty(c, jeb);
0215                 if (ret)
0216                     goto out;
0217             }
0218             break;
0219 
0220         case BLK_STATE_ALLDIRTY:
0221             /* Nothing valid - not even a clean marker. Needs erasing. */
0222             /* For now we just put it on the erasing list. We'll start the erases later */
0223             jffs2_dbg(1, "Erase block at 0x%08x is not formatted. It will be erased\n",
0224                   jeb->offset);
0225             list_add(&jeb->list, &c->erase_pending_list);
0226             c->nr_erasing_blocks++;
0227             break;
0228 
0229         case BLK_STATE_BADBLOCK:
0230             jffs2_dbg(1, "Block at 0x%08x is bad\n", jeb->offset);
0231             list_add(&jeb->list, &c->bad_list);
0232             c->bad_size += c->sector_size;
0233             c->free_size -= c->sector_size;
0234             bad_blocks++;
0235             break;
0236         default:
0237             pr_warn("%s(): unknown block state\n", __func__);
0238             BUG();
0239         }
0240     }
0241 
0242     /* Nextblock dirty is always seen as wasted, because we cannot recycle it now */
0243     if (c->nextblock && (c->nextblock->dirty_size)) {
0244         c->nextblock->wasted_size += c->nextblock->dirty_size;
0245         c->wasted_size += c->nextblock->dirty_size;
0246         c->dirty_size -= c->nextblock->dirty_size;
0247         c->nextblock->dirty_size = 0;
0248     }
0249 #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
0250     if (!jffs2_can_mark_obsolete(c) && c->wbuf_pagesize && c->nextblock && (c->nextblock->free_size % c->wbuf_pagesize)) {
0251         /* If we're going to start writing into a block which already
0252            contains data, and the end of the data isn't page-aligned,
0253            skip a little and align it. */
0254 
0255         uint32_t skip = c->nextblock->free_size % c->wbuf_pagesize;
0256 
0257         jffs2_dbg(1, "%s(): Skipping %d bytes in nextblock to ensure page alignment\n",
0258               __func__, skip);
0259         jffs2_prealloc_raw_node_refs(c, c->nextblock, 1);
0260         jffs2_scan_dirty_space(c, c->nextblock, skip);
0261     }
0262 #endif
0263     if (c->nr_erasing_blocks) {
0264         if (!c->used_size && !c->unchecked_size &&
0265             ((c->nr_free_blocks+empty_blocks+bad_blocks) != c->nr_blocks || bad_blocks == c->nr_blocks)) {
0266             pr_notice("Cowardly refusing to erase blocks on filesystem with no valid JFFS2 nodes\n");
0267             pr_notice("empty_blocks %d, bad_blocks %d, c->nr_blocks %d\n",
0268                   empty_blocks, bad_blocks, c->nr_blocks);
0269             ret = -EIO;
0270             goto out;
0271         }
0272         spin_lock(&c->erase_completion_lock);
0273         jffs2_garbage_collect_trigger(c);
0274         spin_unlock(&c->erase_completion_lock);
0275     }
0276     ret = 0;
0277  out:
0278     jffs2_sum_reset_collected(s);
0279     kfree(s);
0280  out_buf:
0281     if (buf_size)
0282         kfree(flashbuf);
0283 #ifndef __ECOS
0284     else
0285         mtd_unpoint(c->mtd, 0, c->mtd->size);
0286 #endif
0287     return ret;
0288 }
0289 
0290 static int jffs2_fill_scan_buf(struct jffs2_sb_info *c, void *buf,
0291                    uint32_t ofs, uint32_t len)
0292 {
0293     int ret;
0294     size_t retlen;
0295 
0296     ret = jffs2_flash_read(c, ofs, len, &retlen, buf);
0297     if (ret) {
0298         jffs2_dbg(1, "mtd->read(0x%x bytes from 0x%x) returned %d\n",
0299               len, ofs, ret);
0300         return ret;
0301     }
0302     if (retlen < len) {
0303         jffs2_dbg(1, "Read at 0x%x gave only 0x%zx bytes\n",
0304               ofs, retlen);
0305         return -EIO;
0306     }
0307     return 0;
0308 }
0309 
0310 int jffs2_scan_classify_jeb(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
0311 {
0312     if ((jeb->used_size + jeb->unchecked_size) == PAD(c->cleanmarker_size) && !jeb->dirty_size
0313         && (!jeb->first_node || !ref_next(jeb->first_node)) )
0314         return BLK_STATE_CLEANMARKER;
0315 
0316     /* move blocks with max 4 byte dirty space to cleanlist */
0317     else if (!ISDIRTY(c->sector_size - (jeb->used_size + jeb->unchecked_size))) {
0318         c->dirty_size -= jeb->dirty_size;
0319         c->wasted_size += jeb->dirty_size;
0320         jeb->wasted_size += jeb->dirty_size;
0321         jeb->dirty_size = 0;
0322         return BLK_STATE_CLEAN;
0323     } else if (jeb->used_size || jeb->unchecked_size)
0324         return BLK_STATE_PARTDIRTY;
0325     else
0326         return BLK_STATE_ALLDIRTY;
0327 }
0328 
0329 #ifdef CONFIG_JFFS2_FS_XATTR
0330 static int jffs2_scan_xattr_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
0331                  struct jffs2_raw_xattr *rx, uint32_t ofs,
0332                  struct jffs2_summary *s)
0333 {
0334     struct jffs2_xattr_datum *xd;
0335     uint32_t xid, version, totlen, crc;
0336     int err;
0337 
0338     crc = crc32(0, rx, sizeof(struct jffs2_raw_xattr) - 4);
0339     if (crc != je32_to_cpu(rx->node_crc)) {
0340         JFFS2_WARNING("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
0341                   ofs, je32_to_cpu(rx->node_crc), crc);
0342         if ((err = jffs2_scan_dirty_space(c, jeb, je32_to_cpu(rx->totlen))))
0343             return err;
0344         return 0;
0345     }
0346 
0347     xid = je32_to_cpu(rx->xid);
0348     version = je32_to_cpu(rx->version);
0349 
0350     totlen = PAD(sizeof(struct jffs2_raw_xattr)
0351             + rx->name_len + 1 + je16_to_cpu(rx->value_len));
0352     if (totlen != je32_to_cpu(rx->totlen)) {
0353         JFFS2_WARNING("node length mismatch at %#08x, read=%u, calc=%u\n",
0354                   ofs, je32_to_cpu(rx->totlen), totlen);
0355         if ((err = jffs2_scan_dirty_space(c, jeb, je32_to_cpu(rx->totlen))))
0356             return err;
0357         return 0;
0358     }
0359 
0360     xd = jffs2_setup_xattr_datum(c, xid, version);
0361     if (IS_ERR(xd))
0362         return PTR_ERR(xd);
0363 
0364     if (xd->version > version) {
0365         struct jffs2_raw_node_ref *raw
0366             = jffs2_link_node_ref(c, jeb, ofs | REF_PRISTINE, totlen, NULL);
0367         raw->next_in_ino = xd->node->next_in_ino;
0368         xd->node->next_in_ino = raw;
0369     } else {
0370         xd->version = version;
0371         xd->xprefix = rx->xprefix;
0372         xd->name_len = rx->name_len;
0373         xd->value_len = je16_to_cpu(rx->value_len);
0374         xd->data_crc = je32_to_cpu(rx->data_crc);
0375 
0376         jffs2_link_node_ref(c, jeb, ofs | REF_PRISTINE, totlen, (void *)xd);
0377     }
0378 
0379     if (jffs2_sum_active())
0380         jffs2_sum_add_xattr_mem(s, rx, ofs - jeb->offset);
0381     dbg_xattr("scanning xdatum at %#08x (xid=%u, version=%u)\n",
0382           ofs, xd->xid, xd->version);
0383     return 0;
0384 }
0385 
0386 static int jffs2_scan_xref_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
0387                 struct jffs2_raw_xref *rr, uint32_t ofs,
0388                 struct jffs2_summary *s)
0389 {
0390     struct jffs2_xattr_ref *ref;
0391     uint32_t crc;
0392     int err;
0393 
0394     crc = crc32(0, rr, sizeof(*rr) - 4);
0395     if (crc != je32_to_cpu(rr->node_crc)) {
0396         JFFS2_WARNING("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
0397                   ofs, je32_to_cpu(rr->node_crc), crc);
0398         if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(rr->totlen)))))
0399             return err;
0400         return 0;
0401     }
0402 
0403     if (PAD(sizeof(struct jffs2_raw_xref)) != je32_to_cpu(rr->totlen)) {
0404         JFFS2_WARNING("node length mismatch at %#08x, read=%u, calc=%zd\n",
0405                   ofs, je32_to_cpu(rr->totlen),
0406                   PAD(sizeof(struct jffs2_raw_xref)));
0407         if ((err = jffs2_scan_dirty_space(c, jeb, je32_to_cpu(rr->totlen))))
0408             return err;
0409         return 0;
0410     }
0411 
0412     ref = jffs2_alloc_xattr_ref();
0413     if (!ref)
0414         return -ENOMEM;
0415 
0416     /* BEFORE jffs2_build_xattr_subsystem() called, 
0417      * and AFTER xattr_ref is marked as a dead xref,
0418      * ref->xid is used to store 32bit xid, xd is not used
0419      * ref->ino is used to store 32bit inode-number, ic is not used
0420      * Thoes variables are declared as union, thus using those
0421      * are exclusive. In a similar way, ref->next is temporarily
0422      * used to chain all xattr_ref object. It's re-chained to
0423      * jffs2_inode_cache in jffs2_build_xattr_subsystem() correctly.
0424      */
0425     ref->ino = je32_to_cpu(rr->ino);
0426     ref->xid = je32_to_cpu(rr->xid);
0427     ref->xseqno = je32_to_cpu(rr->xseqno);
0428     if (ref->xseqno > c->highest_xseqno)
0429         c->highest_xseqno = (ref->xseqno & ~XREF_DELETE_MARKER);
0430     ref->next = c->xref_temp;
0431     c->xref_temp = ref;
0432 
0433     jffs2_link_node_ref(c, jeb, ofs | REF_PRISTINE, PAD(je32_to_cpu(rr->totlen)), (void *)ref);
0434 
0435     if (jffs2_sum_active())
0436         jffs2_sum_add_xref_mem(s, rr, ofs - jeb->offset);
0437     dbg_xattr("scan xref at %#08x (xid=%u, ino=%u)\n",
0438           ofs, ref->xid, ref->ino);
0439     return 0;
0440 }
0441 #endif
0442 
0443 /* Called with 'buf_size == 0' if buf is in fact a pointer _directly_ into
0444    the flash, XIP-style */
0445 static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
0446                   unsigned char *buf, uint32_t buf_size, struct jffs2_summary *s) {
0447     struct jffs2_unknown_node *node;
0448     struct jffs2_unknown_node crcnode;
0449     uint32_t ofs, prevofs, max_ofs;
0450     uint32_t hdr_crc, buf_ofs, buf_len;
0451     int err;
0452     int noise = 0;
0453 
0454 
0455 #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
0456     int cleanmarkerfound = 0;
0457 #endif
0458 
0459     ofs = jeb->offset;
0460     prevofs = jeb->offset - 1;
0461 
0462     jffs2_dbg(1, "%s(): Scanning block at 0x%x\n", __func__, ofs);
0463 
0464 #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
0465     if (jffs2_cleanmarker_oob(c)) {
0466         int ret;
0467 
0468         if (mtd_block_isbad(c->mtd, jeb->offset))
0469             return BLK_STATE_BADBLOCK;
0470 
0471         ret = jffs2_check_nand_cleanmarker(c, jeb);
0472         jffs2_dbg(2, "jffs_check_nand_cleanmarker returned %d\n", ret);
0473 
0474         /* Even if it's not found, we still scan to see
0475            if the block is empty. We use this information
0476            to decide whether to erase it or not. */
0477         switch (ret) {
0478         case 0:     cleanmarkerfound = 1; break;
0479         case 1:     break;
0480         default:    return ret;
0481         }
0482     }
0483 #endif
0484 
0485     if (jffs2_sum_active()) {
0486         struct jffs2_sum_marker *sm;
0487         void *sumptr = NULL;
0488         uint32_t sumlen;
0489           
0490         if (!buf_size) {
0491             /* XIP case. Just look, point at the summary if it's there */
0492             sm = (void *)buf + c->sector_size - sizeof(*sm);
0493             if (je32_to_cpu(sm->magic) == JFFS2_SUM_MAGIC) {
0494                 sumptr = buf + je32_to_cpu(sm->offset);
0495                 sumlen = c->sector_size - je32_to_cpu(sm->offset);
0496             }
0497         } else {
0498             /* If NAND flash, read a whole page of it. Else just the end */
0499             if (c->wbuf_pagesize)
0500                 buf_len = c->wbuf_pagesize;
0501             else
0502                 buf_len = sizeof(*sm);
0503 
0504             /* Read as much as we want into the _end_ of the preallocated buffer */
0505             err = jffs2_fill_scan_buf(c, buf + buf_size - buf_len, 
0506                           jeb->offset + c->sector_size - buf_len,
0507                           buf_len);             
0508             if (err)
0509                 return err;
0510 
0511             sm = (void *)buf + buf_size - sizeof(*sm);
0512             if (je32_to_cpu(sm->magic) == JFFS2_SUM_MAGIC) {
0513                 sumlen = c->sector_size - je32_to_cpu(sm->offset);
0514                 sumptr = buf + buf_size - sumlen;
0515 
0516                 /* sm->offset maybe wrong but MAGIC maybe right */
0517                 if (sumlen > c->sector_size)
0518                     goto full_scan;
0519 
0520                 /* Now, make sure the summary itself is available */
0521                 if (sumlen > buf_size) {
0522                     /* Need to kmalloc for this. */
0523                     sumptr = kmalloc(sumlen, GFP_KERNEL);
0524                     if (!sumptr)
0525                         return -ENOMEM;
0526                     memcpy(sumptr + sumlen - buf_len, buf + buf_size - buf_len, buf_len);
0527                 }
0528                 if (buf_len < sumlen) {
0529                     /* Need to read more so that the entire summary node is present */
0530                     err = jffs2_fill_scan_buf(c, sumptr, 
0531                                   jeb->offset + c->sector_size - sumlen,
0532                                   sumlen - buf_len);                
0533                     if (err) {
0534                         if (sumlen > buf_size)
0535                             kfree(sumptr);
0536                         return err;
0537                     }
0538                 }
0539             }
0540 
0541         }
0542 
0543         if (sumptr) {
0544             err = jffs2_sum_scan_sumnode(c, jeb, sumptr, sumlen, &pseudo_random);
0545 
0546             if (buf_size && sumlen > buf_size)
0547                 kfree(sumptr);
0548             /* If it returns with a real error, bail. 
0549                If it returns positive, that's a block classification
0550                (i.e. BLK_STATE_xxx) so return that too.
0551                If it returns zero, fall through to full scan. */
0552             if (err)
0553                 return err;
0554         }
0555     }
0556 
0557 full_scan:
0558     buf_ofs = jeb->offset;
0559 
0560     if (!buf_size) {
0561         /* This is the XIP case -- we're reading _directly_ from the flash chip */
0562         buf_len = c->sector_size;
0563     } else {
0564         buf_len = EMPTY_SCAN_SIZE(c->sector_size);
0565         err = jffs2_fill_scan_buf(c, buf, buf_ofs, buf_len);
0566         if (err)
0567             return err;
0568     }
0569 
0570     /* We temporarily use 'ofs' as a pointer into the buffer/jeb */
0571     ofs = 0;
0572     max_ofs = EMPTY_SCAN_SIZE(c->sector_size);
0573     /* Scan only EMPTY_SCAN_SIZE of 0xFF before declaring it's empty */
0574     while(ofs < max_ofs && *(uint32_t *)(&buf[ofs]) == 0xFFFFFFFF)
0575         ofs += 4;
0576 
0577     if (ofs == max_ofs) {
0578 #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
0579         if (jffs2_cleanmarker_oob(c)) {
0580             /* scan oob, take care of cleanmarker */
0581             int ret = jffs2_check_oob_empty(c, jeb, cleanmarkerfound);
0582             jffs2_dbg(2, "jffs2_check_oob_empty returned %d\n",
0583                   ret);
0584             switch (ret) {
0585             case 0:     return cleanmarkerfound ? BLK_STATE_CLEANMARKER : BLK_STATE_ALLFF;
0586             case 1:     return BLK_STATE_ALLDIRTY;
0587             default:    return ret;
0588             }
0589         }
0590 #endif
0591         jffs2_dbg(1, "Block at 0x%08x is empty (erased)\n",
0592               jeb->offset);
0593         if (c->cleanmarker_size == 0)
0594             return BLK_STATE_CLEANMARKER;   /* don't bother with re-erase */
0595         else
0596             return BLK_STATE_ALLFF; /* OK to erase if all blocks are like this */
0597     }
0598     if (ofs) {
0599         jffs2_dbg(1, "Free space at %08x ends at %08x\n", jeb->offset,
0600               jeb->offset + ofs);
0601         if ((err = jffs2_prealloc_raw_node_refs(c, jeb, 1)))
0602             return err;
0603         if ((err = jffs2_scan_dirty_space(c, jeb, ofs)))
0604             return err;
0605     }
0606 
0607     /* Now ofs is a complete physical flash offset as it always was... */
0608     ofs += jeb->offset;
0609 
0610     noise = 10;
0611 
0612     dbg_summary("no summary found in jeb 0x%08x. Apply original scan.\n",jeb->offset);
0613 
0614 scan_more:
0615     while(ofs < jeb->offset + c->sector_size) {
0616 
0617         jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
0618 
0619         /* Make sure there are node refs available for use */
0620         err = jffs2_prealloc_raw_node_refs(c, jeb, 2);
0621         if (err)
0622             return err;
0623 
0624         cond_resched();
0625 
0626         if (ofs & 3) {
0627             pr_warn("Eep. ofs 0x%08x not word-aligned!\n", ofs);
0628             ofs = PAD(ofs);
0629             continue;
0630         }
0631         if (ofs == prevofs) {
0632             pr_warn("ofs 0x%08x has already been seen. Skipping\n",
0633                 ofs);
0634             if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
0635                 return err;
0636             ofs += 4;
0637             continue;
0638         }
0639         prevofs = ofs;
0640 
0641         if (jeb->offset + c->sector_size < ofs + sizeof(*node)) {
0642             jffs2_dbg(1, "Fewer than %zd bytes left to end of block. (%x+%x<%x+%zx) Not reading\n",
0643                   sizeof(struct jffs2_unknown_node),
0644                   jeb->offset, c->sector_size, ofs,
0645                   sizeof(*node));
0646             if ((err = jffs2_scan_dirty_space(c, jeb, (jeb->offset + c->sector_size)-ofs)))
0647                 return err;
0648             break;
0649         }
0650 
0651         if (buf_ofs + buf_len < ofs + sizeof(*node)) {
0652             buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
0653             jffs2_dbg(1, "Fewer than %zd bytes (node header) left to end of buf. Reading 0x%x at 0x%08x\n",
0654                   sizeof(struct jffs2_unknown_node),
0655                   buf_len, ofs);
0656             err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
0657             if (err)
0658                 return err;
0659             buf_ofs = ofs;
0660         }
0661 
0662         node = (struct jffs2_unknown_node *)&buf[ofs-buf_ofs];
0663 
0664         if (*(uint32_t *)(&buf[ofs-buf_ofs]) == 0xffffffff) {
0665             uint32_t inbuf_ofs;
0666             uint32_t empty_start, scan_end;
0667 
0668             empty_start = ofs;
0669             ofs += 4;
0670             scan_end = min_t(uint32_t, EMPTY_SCAN_SIZE(c->sector_size)/8, buf_len);
0671 
0672             jffs2_dbg(1, "Found empty flash at 0x%08x\n", ofs);
0673         more_empty:
0674             inbuf_ofs = ofs - buf_ofs;
0675             while (inbuf_ofs < scan_end) {
0676                 if (unlikely(*(uint32_t *)(&buf[inbuf_ofs]) != 0xffffffff)) {
0677                     pr_warn("Empty flash at 0x%08x ends at 0x%08x\n",
0678                         empty_start, ofs);
0679                     if ((err = jffs2_scan_dirty_space(c, jeb, ofs-empty_start)))
0680                         return err;
0681                     goto scan_more;
0682                 }
0683 
0684                 inbuf_ofs+=4;
0685                 ofs += 4;
0686             }
0687             /* Ran off end. */
0688             jffs2_dbg(1, "Empty flash to end of buffer at 0x%08x\n",
0689                   ofs);
0690 
0691             /* If we're only checking the beginning of a block with a cleanmarker,
0692                bail now */
0693             if (buf_ofs == jeb->offset && jeb->used_size == PAD(c->cleanmarker_size) &&
0694                 c->cleanmarker_size && !jeb->dirty_size && !ref_next(jeb->first_node)) {
0695                 jffs2_dbg(1, "%d bytes at start of block seems clean... assuming all clean\n",
0696                       EMPTY_SCAN_SIZE(c->sector_size));
0697                 return BLK_STATE_CLEANMARKER;
0698             }
0699             if (!buf_size && (scan_end != buf_len)) {/* XIP/point case */
0700                 scan_end = buf_len;
0701                 goto more_empty;
0702             }
0703             
0704             /* See how much more there is to read in this eraseblock... */
0705             buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
0706             if (!buf_len) {
0707                 /* No more to read. Break out of main loop without marking
0708                    this range of empty space as dirty (because it's not) */
0709                 jffs2_dbg(1, "Empty flash at %08x runs to end of block. Treating as free_space\n",
0710                       empty_start);
0711                 break;
0712             }
0713             /* point never reaches here */
0714             scan_end = buf_len;
0715             jffs2_dbg(1, "Reading another 0x%x at 0x%08x\n",
0716                   buf_len, ofs);
0717             err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
0718             if (err)
0719                 return err;
0720             buf_ofs = ofs;
0721             goto more_empty;
0722         }
0723 
0724         if (ofs == jeb->offset && je16_to_cpu(node->magic) == KSAMTIB_CIGAM_2SFFJ) {
0725             pr_warn("Magic bitmask is backwards at offset 0x%08x. Wrong endian filesystem?\n",
0726                 ofs);
0727             if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
0728                 return err;
0729             ofs += 4;
0730             continue;
0731         }
0732         if (je16_to_cpu(node->magic) == JFFS2_DIRTY_BITMASK) {
0733             jffs2_dbg(1, "Dirty bitmask at 0x%08x\n", ofs);
0734             if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
0735                 return err;
0736             ofs += 4;
0737             continue;
0738         }
0739         if (je16_to_cpu(node->magic) == JFFS2_OLD_MAGIC_BITMASK) {
0740             pr_warn("Old JFFS2 bitmask found at 0x%08x\n", ofs);
0741             pr_warn("You cannot use older JFFS2 filesystems with newer kernels\n");
0742             if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
0743                 return err;
0744             ofs += 4;
0745             continue;
0746         }
0747         if (je16_to_cpu(node->magic) != JFFS2_MAGIC_BITMASK) {
0748             /* OK. We're out of possibilities. Whinge and move on */
0749             noisy_printk(&noise, "%s(): Magic bitmask 0x%04x not found at 0x%08x: 0x%04x instead\n",
0750                      __func__,
0751                      JFFS2_MAGIC_BITMASK, ofs,
0752                      je16_to_cpu(node->magic));
0753             if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
0754                 return err;
0755             ofs += 4;
0756             continue;
0757         }
0758         /* We seem to have a node of sorts. Check the CRC */
0759         crcnode.magic = node->magic;
0760         crcnode.nodetype = cpu_to_je16( je16_to_cpu(node->nodetype) | JFFS2_NODE_ACCURATE);
0761         crcnode.totlen = node->totlen;
0762         hdr_crc = crc32(0, &crcnode, sizeof(crcnode)-4);
0763 
0764         if (hdr_crc != je32_to_cpu(node->hdr_crc)) {
0765             noisy_printk(&noise, "%s(): Node at 0x%08x {0x%04x, 0x%04x, 0x%08x) has invalid CRC 0x%08x (calculated 0x%08x)\n",
0766                      __func__,
0767                      ofs, je16_to_cpu(node->magic),
0768                      je16_to_cpu(node->nodetype),
0769                      je32_to_cpu(node->totlen),
0770                      je32_to_cpu(node->hdr_crc),
0771                      hdr_crc);
0772             if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
0773                 return err;
0774             ofs += 4;
0775             continue;
0776         }
0777 
0778         if (ofs + je32_to_cpu(node->totlen) > jeb->offset + c->sector_size) {
0779             /* Eep. Node goes over the end of the erase block. */
0780             pr_warn("Node at 0x%08x with length 0x%08x would run over the end of the erase block\n",
0781                 ofs, je32_to_cpu(node->totlen));
0782             pr_warn("Perhaps the file system was created with the wrong erase size?\n");
0783             if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
0784                 return err;
0785             ofs += 4;
0786             continue;
0787         }
0788 
0789         if (!(je16_to_cpu(node->nodetype) & JFFS2_NODE_ACCURATE)) {
0790             /* Wheee. This is an obsoleted node */
0791             jffs2_dbg(2, "Node at 0x%08x is obsolete. Skipping\n",
0792                   ofs);
0793             if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen)))))
0794                 return err;
0795             ofs += PAD(je32_to_cpu(node->totlen));
0796             continue;
0797         }
0798 
0799         switch(je16_to_cpu(node->nodetype)) {
0800         case JFFS2_NODETYPE_INODE:
0801             if (buf_ofs + buf_len < ofs + sizeof(struct jffs2_raw_inode)) {
0802                 buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
0803                 jffs2_dbg(1, "Fewer than %zd bytes (inode node) left to end of buf. Reading 0x%x at 0x%08x\n",
0804                       sizeof(struct jffs2_raw_inode),
0805                       buf_len, ofs);
0806                 err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
0807                 if (err)
0808                     return err;
0809                 buf_ofs = ofs;
0810                 node = (void *)buf;
0811             }
0812             err = jffs2_scan_inode_node(c, jeb, (void *)node, ofs, s);
0813             if (err) return err;
0814             ofs += PAD(je32_to_cpu(node->totlen));
0815             break;
0816 
0817         case JFFS2_NODETYPE_DIRENT:
0818             if (buf_ofs + buf_len < ofs + je32_to_cpu(node->totlen)) {
0819                 buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
0820                 jffs2_dbg(1, "Fewer than %d bytes (dirent node) left to end of buf. Reading 0x%x at 0x%08x\n",
0821                       je32_to_cpu(node->totlen), buf_len,
0822                       ofs);
0823                 err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
0824                 if (err)
0825                     return err;
0826                 buf_ofs = ofs;
0827                 node = (void *)buf;
0828             }
0829             err = jffs2_scan_dirent_node(c, jeb, (void *)node, ofs, s);
0830             if (err) return err;
0831             ofs += PAD(je32_to_cpu(node->totlen));
0832             break;
0833 
0834 #ifdef CONFIG_JFFS2_FS_XATTR
0835         case JFFS2_NODETYPE_XATTR:
0836             if (buf_ofs + buf_len < ofs + je32_to_cpu(node->totlen)) {
0837                 buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
0838                 jffs2_dbg(1, "Fewer than %d bytes (xattr node) left to end of buf. Reading 0x%x at 0x%08x\n",
0839                       je32_to_cpu(node->totlen), buf_len,
0840                       ofs);
0841                 err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
0842                 if (err)
0843                     return err;
0844                 buf_ofs = ofs;
0845                 node = (void *)buf;
0846             }
0847             err = jffs2_scan_xattr_node(c, jeb, (void *)node, ofs, s);
0848             if (err)
0849                 return err;
0850             ofs += PAD(je32_to_cpu(node->totlen));
0851             break;
0852         case JFFS2_NODETYPE_XREF:
0853             if (buf_ofs + buf_len < ofs + je32_to_cpu(node->totlen)) {
0854                 buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
0855                 jffs2_dbg(1, "Fewer than %d bytes (xref node) left to end of buf. Reading 0x%x at 0x%08x\n",
0856                       je32_to_cpu(node->totlen), buf_len,
0857                       ofs);
0858                 err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
0859                 if (err)
0860                     return err;
0861                 buf_ofs = ofs;
0862                 node = (void *)buf;
0863             }
0864             err = jffs2_scan_xref_node(c, jeb, (void *)node, ofs, s);
0865             if (err)
0866                 return err;
0867             ofs += PAD(je32_to_cpu(node->totlen));
0868             break;
0869 #endif  /* CONFIG_JFFS2_FS_XATTR */
0870 
0871         case JFFS2_NODETYPE_CLEANMARKER:
0872             jffs2_dbg(1, "CLEANMARKER node found at 0x%08x\n", ofs);
0873             if (je32_to_cpu(node->totlen) != c->cleanmarker_size) {
0874                 pr_notice("CLEANMARKER node found at 0x%08x has totlen 0x%x != normal 0x%x\n",
0875                       ofs, je32_to_cpu(node->totlen),
0876                       c->cleanmarker_size);
0877                 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(sizeof(struct jffs2_unknown_node)))))
0878                     return err;
0879                 ofs += PAD(sizeof(struct jffs2_unknown_node));
0880             } else if (jeb->first_node) {
0881                 pr_notice("CLEANMARKER node found at 0x%08x, not first node in block (0x%08x)\n",
0882                       ofs, jeb->offset);
0883                 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(sizeof(struct jffs2_unknown_node)))))
0884                     return err;
0885                 ofs += PAD(sizeof(struct jffs2_unknown_node));
0886             } else {
0887                 jffs2_link_node_ref(c, jeb, ofs | REF_NORMAL, c->cleanmarker_size, NULL);
0888 
0889                 ofs += PAD(c->cleanmarker_size);
0890             }
0891             break;
0892 
0893         case JFFS2_NODETYPE_PADDING:
0894             if (jffs2_sum_active())
0895                 jffs2_sum_add_padding_mem(s, je32_to_cpu(node->totlen));
0896             if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen)))))
0897                 return err;
0898             ofs += PAD(je32_to_cpu(node->totlen));
0899             break;
0900 
0901         default:
0902             switch (je16_to_cpu(node->nodetype) & JFFS2_COMPAT_MASK) {
0903             case JFFS2_FEATURE_ROCOMPAT:
0904                 pr_notice("Read-only compatible feature node (0x%04x) found at offset 0x%08x\n",
0905                       je16_to_cpu(node->nodetype), ofs);
0906                 c->flags |= JFFS2_SB_FLAG_RO;
0907                 if (!(jffs2_is_readonly(c)))
0908                     return -EROFS;
0909                 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen)))))
0910                     return err;
0911                 ofs += PAD(je32_to_cpu(node->totlen));
0912                 break;
0913 
0914             case JFFS2_FEATURE_INCOMPAT:
0915                 pr_notice("Incompatible feature node (0x%04x) found at offset 0x%08x\n",
0916                       je16_to_cpu(node->nodetype), ofs);
0917                 return -EINVAL;
0918 
0919             case JFFS2_FEATURE_RWCOMPAT_DELETE:
0920                 jffs2_dbg(1, "Unknown but compatible feature node (0x%04x) found at offset 0x%08x\n",
0921                       je16_to_cpu(node->nodetype), ofs);
0922                 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen)))))
0923                     return err;
0924                 ofs += PAD(je32_to_cpu(node->totlen));
0925                 break;
0926 
0927             case JFFS2_FEATURE_RWCOMPAT_COPY: {
0928                 jffs2_dbg(1, "Unknown but compatible feature node (0x%04x) found at offset 0x%08x\n",
0929                       je16_to_cpu(node->nodetype), ofs);
0930 
0931                 jffs2_link_node_ref(c, jeb, ofs | REF_PRISTINE, PAD(je32_to_cpu(node->totlen)), NULL);
0932 
0933                 /* We can't summarise nodes we don't grok */
0934                 jffs2_sum_disable_collecting(s);
0935                 ofs += PAD(je32_to_cpu(node->totlen));
0936                 break;
0937                 }
0938             }
0939         }
0940     }
0941 
0942     if (jffs2_sum_active()) {
0943         if (PAD(s->sum_size + JFFS2_SUMMARY_FRAME_SIZE) > jeb->free_size) {
0944             dbg_summary("There is not enough space for "
0945                 "summary information, disabling for this jeb!\n");
0946             jffs2_sum_disable_collecting(s);
0947         }
0948     }
0949 
0950     jffs2_dbg(1, "Block at 0x%08x: free 0x%08x, dirty 0x%08x, unchecked 0x%08x, used 0x%08x, wasted 0x%08x\n",
0951           jeb->offset, jeb->free_size, jeb->dirty_size,
0952           jeb->unchecked_size, jeb->used_size, jeb->wasted_size);
0953     
0954     /* mark_node_obsolete can add to wasted !! */
0955     if (jeb->wasted_size) {
0956         jeb->dirty_size += jeb->wasted_size;
0957         c->dirty_size += jeb->wasted_size;
0958         c->wasted_size -= jeb->wasted_size;
0959         jeb->wasted_size = 0;
0960     }
0961 
0962     return jffs2_scan_classify_jeb(c, jeb);
0963 }
0964 
0965 struct jffs2_inode_cache *jffs2_scan_make_ino_cache(struct jffs2_sb_info *c, uint32_t ino)
0966 {
0967     struct jffs2_inode_cache *ic;
0968 
0969     ic = jffs2_get_ino_cache(c, ino);
0970     if (ic)
0971         return ic;
0972 
0973     if (ino > c->highest_ino)
0974         c->highest_ino = ino;
0975 
0976     ic = jffs2_alloc_inode_cache();
0977     if (!ic) {
0978         pr_notice("%s(): allocation of inode cache failed\n", __func__);
0979         return NULL;
0980     }
0981     memset(ic, 0, sizeof(*ic));
0982 
0983     ic->ino = ino;
0984     ic->nodes = (void *)ic;
0985     jffs2_add_ino_cache(c, ic);
0986     if (ino == 1)
0987         ic->pino_nlink = 1;
0988     return ic;
0989 }
0990 
0991 static int jffs2_scan_inode_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
0992                  struct jffs2_raw_inode *ri, uint32_t ofs, struct jffs2_summary *s)
0993 {
0994     struct jffs2_inode_cache *ic;
0995     uint32_t crc, ino = je32_to_cpu(ri->ino);
0996 
0997     jffs2_dbg(1, "%s(): Node at 0x%08x\n", __func__, ofs);
0998 
0999     /* We do very little here now. Just check the ino# to which we should attribute
1000        this node; we can do all the CRC checking etc. later. There's a tradeoff here --
1001        we used to scan the flash once only, reading everything we want from it into
1002        memory, then building all our in-core data structures and freeing the extra
1003        information. Now we allow the first part of the mount to complete a lot quicker,
1004        but we have to go _back_ to the flash in order to finish the CRC checking, etc.
1005        Which means that the _full_ amount of time to get to proper write mode with GC
1006        operational may actually be _longer_ than before. Sucks to be me. */
1007 
1008     /* Check the node CRC in any case. */
1009     crc = crc32(0, ri, sizeof(*ri)-8);
1010     if (crc != je32_to_cpu(ri->node_crc)) {
1011         pr_notice("%s(): CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
1012               __func__, ofs, je32_to_cpu(ri->node_crc), crc);
1013         /*
1014          * We believe totlen because the CRC on the node
1015          * _header_ was OK, just the node itself failed.
1016          */
1017         return jffs2_scan_dirty_space(c, jeb,
1018                           PAD(je32_to_cpu(ri->totlen)));
1019     }
1020 
1021     ic = jffs2_get_ino_cache(c, ino);
1022     if (!ic) {
1023         ic = jffs2_scan_make_ino_cache(c, ino);
1024         if (!ic)
1025             return -ENOMEM;
1026     }
1027 
1028     /* Wheee. It worked */
1029     jffs2_link_node_ref(c, jeb, ofs | REF_UNCHECKED, PAD(je32_to_cpu(ri->totlen)), ic);
1030 
1031     jffs2_dbg(1, "Node is ino #%u, version %d. Range 0x%x-0x%x\n",
1032           je32_to_cpu(ri->ino), je32_to_cpu(ri->version),
1033           je32_to_cpu(ri->offset),
1034           je32_to_cpu(ri->offset)+je32_to_cpu(ri->dsize));
1035 
1036     pseudo_random += je32_to_cpu(ri->version);
1037 
1038     if (jffs2_sum_active()) {
1039         jffs2_sum_add_inode_mem(s, ri, ofs - jeb->offset);
1040     }
1041 
1042     return 0;
1043 }
1044 
1045 static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
1046                   struct jffs2_raw_dirent *rd, uint32_t ofs, struct jffs2_summary *s)
1047 {
1048     struct jffs2_full_dirent *fd;
1049     struct jffs2_inode_cache *ic;
1050     uint32_t checkedlen;
1051     uint32_t crc;
1052     int err;
1053 
1054     jffs2_dbg(1, "%s(): Node at 0x%08x\n", __func__, ofs);
1055 
1056     /* We don't get here unless the node is still valid, so we don't have to
1057        mask in the ACCURATE bit any more. */
1058     crc = crc32(0, rd, sizeof(*rd)-8);
1059 
1060     if (crc != je32_to_cpu(rd->node_crc)) {
1061         pr_notice("%s(): Node CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
1062               __func__, ofs, je32_to_cpu(rd->node_crc), crc);
1063         /* We believe totlen because the CRC on the node _header_ was OK, just the node itself failed. */
1064         if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(rd->totlen)))))
1065             return err;
1066         return 0;
1067     }
1068 
1069     pseudo_random += je32_to_cpu(rd->version);
1070 
1071     /* Should never happen. Did. (OLPC trac #4184)*/
1072     checkedlen = strnlen(rd->name, rd->nsize);
1073     if (checkedlen < rd->nsize) {
1074         pr_err("Dirent at %08x has zeroes in name. Truncating to %d chars\n",
1075                ofs, checkedlen);
1076     }
1077     fd = jffs2_alloc_full_dirent(checkedlen+1);
1078     if (!fd) {
1079         return -ENOMEM;
1080     }
1081     memcpy(&fd->name, rd->name, checkedlen);
1082     fd->name[checkedlen] = 0;
1083 
1084     crc = crc32(0, fd->name, checkedlen);
1085     if (crc != je32_to_cpu(rd->name_crc)) {
1086         pr_notice("%s(): Name CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
1087               __func__, ofs, je32_to_cpu(rd->name_crc), crc);
1088         jffs2_dbg(1, "Name for which CRC failed is (now) '%s', ino #%d\n",
1089               fd->name, je32_to_cpu(rd->ino));
1090         jffs2_free_full_dirent(fd);
1091         /* FIXME: Why do we believe totlen? */
1092         /* We believe totlen because the CRC on the node _header_ was OK, just the name failed. */
1093         if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(rd->totlen)))))
1094             return err;
1095         return 0;
1096     }
1097     ic = jffs2_scan_make_ino_cache(c, je32_to_cpu(rd->pino));
1098     if (!ic) {
1099         jffs2_free_full_dirent(fd);
1100         return -ENOMEM;
1101     }
1102 
1103     fd->raw = jffs2_link_node_ref(c, jeb, ofs | dirent_node_state(rd),
1104                       PAD(je32_to_cpu(rd->totlen)), ic);
1105 
1106     fd->next = NULL;
1107     fd->version = je32_to_cpu(rd->version);
1108     fd->ino = je32_to_cpu(rd->ino);
1109     fd->nhash = full_name_hash(NULL, fd->name, checkedlen);
1110     fd->type = rd->type;
1111     jffs2_add_fd_to_list(c, fd, &ic->scan_dents);
1112 
1113     if (jffs2_sum_active()) {
1114         jffs2_sum_add_dirent_mem(s, rd, ofs - jeb->offset);
1115     }
1116 
1117     return 0;
1118 }
1119 
1120 static int count_list(struct list_head *l)
1121 {
1122     uint32_t count = 0;
1123     struct list_head *tmp;
1124 
1125     list_for_each(tmp, l) {
1126         count++;
1127     }
1128     return count;
1129 }
1130 
1131 /* Note: This breaks if list_empty(head). I don't care. You
1132    might, if you copy this code and use it elsewhere :) */
1133 static void rotate_list(struct list_head *head, uint32_t count)
1134 {
1135     struct list_head *n = head->next;
1136 
1137     list_del(head);
1138     while(count--) {
1139         n = n->next;
1140     }
1141     list_add(head, n);
1142 }
1143 
1144 void jffs2_rotate_lists(struct jffs2_sb_info *c)
1145 {
1146     uint32_t x;
1147     uint32_t rotateby;
1148 
1149     x = count_list(&c->clean_list);
1150     if (x) {
1151         rotateby = pseudo_random % x;
1152         rotate_list((&c->clean_list), rotateby);
1153     }
1154 
1155     x = count_list(&c->very_dirty_list);
1156     if (x) {
1157         rotateby = pseudo_random % x;
1158         rotate_list((&c->very_dirty_list), rotateby);
1159     }
1160 
1161     x = count_list(&c->dirty_list);
1162     if (x) {
1163         rotateby = pseudo_random % x;
1164         rotate_list((&c->dirty_list), rotateby);
1165     }
1166 
1167     x = count_list(&c->erasable_list);
1168     if (x) {
1169         rotateby = pseudo_random % x;
1170         rotate_list((&c->erasable_list), rotateby);
1171     }
1172 
1173     if (c->nr_erasing_blocks) {
1174         rotateby = pseudo_random % c->nr_erasing_blocks;
1175         rotate_list((&c->erase_pending_list), rotateby);
1176     }
1177 
1178     if (c->nr_free_blocks) {
1179         rotateby = pseudo_random % c->nr_free_blocks;
1180         rotate_list((&c->free_list), rotateby);
1181     }
1182 }