blob: b6c3d883b7d7e110d856baa05ce09f81336223c9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * JFFS2 -- Journalling Flash File System, Version 2.
3 *
David Woodhousec00c3102007-04-25 14:16:47 +01004 * Copyright © 2001-2007 Red Hat, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Created by David Woodhouse <dwmw2@infradead.org>
7 *
8 * For licensing information, see the file 'LICENCE' in this directory.
9 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
David Woodhousec00c3102007-04-25 14:16:47 +010011
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/kernel.h>
13#include <linux/sched.h>
14#include <linux/slab.h>
15#include <linux/mtd/mtd.h>
16#include <linux/pagemap.h>
17#include <linux/crc32.h>
18#include <linux/compiler.h>
19#include "nodelist.h"
Ferenc Havasie631ddb2005-09-07 09:35:26 +010020#include "summary.h"
21#include "debug.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Joakim Tjernlund41bdc602010-10-07 19:09:34 +020023#define DEFAULT_EMPTY_SCAN_SIZE 256
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#define noisy_printk(noise, args...) do { \
26 if (*(noise)) { \
27 printk(KERN_NOTICE args); \
28 (*(noise))--; \
29 if (!(*(noise))) { \
30 printk(KERN_NOTICE "Further such events for this erase block will not be printed\n"); \
31 } \
32 } \
33} while(0)
34
35static uint32_t pseudo_random;
36
37static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
Ferenc Havasie631ddb2005-09-07 09:35:26 +010038 unsigned char *buf, uint32_t buf_size, struct jffs2_summary *s);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000040/* These helper functions _must_ increase ofs and also do the dirty/used space accounting.
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 * Returning an error will abort the mount - bad checksums etc. should just mark the space
42 * as dirty.
43 */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000044static int jffs2_scan_inode_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
Ferenc Havasie631ddb2005-09-07 09:35:26 +010045 struct jffs2_raw_inode *ri, uint32_t ofs, struct jffs2_summary *s);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
Ferenc Havasie631ddb2005-09-07 09:35:26 +010047 struct jffs2_raw_dirent *rd, uint32_t ofs, struct jffs2_summary *s);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49static inline int min_free(struct jffs2_sb_info *c)
50{
51 uint32_t min = 2 * sizeof(struct jffs2_raw_inode);
Andrew Victor2f82ce12005-02-09 09:24:26 +000052#ifdef CONFIG_JFFS2_FS_WRITEBUFFER
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 if (!jffs2_can_mark_obsolete(c) && min < c->wbuf_pagesize)
54 return c->wbuf_pagesize;
55#endif
56 return min;
57
58}
Andrew Victor3be36672005-02-09 09:09:05 +000059
60static inline uint32_t EMPTY_SCAN_SIZE(uint32_t sector_size) {
61 if (sector_size < DEFAULT_EMPTY_SCAN_SIZE)
62 return sector_size;
63 else
64 return DEFAULT_EMPTY_SCAN_SIZE;
65}
66
David Woodhouse25090a62006-05-21 03:57:56 +010067static int file_dirty(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
68{
David Woodhousea6a8bef2006-05-29 00:41:11 +010069 int ret;
70
71 if ((ret = jffs2_prealloc_raw_node_refs(c, jeb, 1)))
72 return ret;
73 if ((ret = jffs2_scan_dirty_space(c, jeb, jeb->free_size)))
David Woodhouse25090a62006-05-21 03:57:56 +010074 return ret;
75 /* Turned wasted size into dirty, since we apparently
76 think it's recoverable now. */
77 jeb->dirty_size += jeb->wasted_size;
78 c->dirty_size += jeb->wasted_size;
79 c->wasted_size -= jeb->wasted_size;
80 jeb->wasted_size = 0;
81 if (VERYDIRTY(c, jeb->dirty_size)) {
82 list_add(&jeb->list, &c->very_dirty_list);
83 } else {
84 list_add(&jeb->list, &c->dirty_list);
85 }
86 return 0;
87}
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089int jffs2_scan_medium(struct jffs2_sb_info *c)
90{
91 int i, ret;
92 uint32_t empty_blocks = 0, bad_blocks = 0;
93 unsigned char *flashbuf = NULL;
94 uint32_t buf_size = 0;
Ferenc Havasie631ddb2005-09-07 09:35:26 +010095 struct jffs2_summary *s = NULL; /* summary info collected by the scan process */
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#ifndef __ECOS
Grant Erickson1ddd0d92011-04-08 08:51:34 -070097 size_t pointlen, try_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Artem Bityutskiyf0265452012-01-30 15:08:26 +020099 ret = mtd_point(c->mtd, 0, c->mtd->size, &pointlen,
100 (void **)&flashbuf, NULL);
101 if (!ret && pointlen < c->mtd->size) {
102 /* Don't muck about if it won't let us point to the whole flash */
Joe Perches9c261b32012-02-15 15:56:43 -0800103 jffs2_dbg(1, "MTD point returned len too short: 0x%zx\n",
104 pointlen);
Artem Bityutskiyf0265452012-01-30 15:08:26 +0200105 mtd_unpoint(c->mtd, 0, pointlen);
106 flashbuf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
Artem Bityutskiyf0265452012-01-30 15:08:26 +0200108 if (ret && ret != -EOPNOTSUPP)
Joe Perches9c261b32012-02-15 15:56:43 -0800109 jffs2_dbg(1, "MTD point failed %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#endif
111 if (!flashbuf) {
112 /* For NAND it's quicker to read a whole eraseblock at a time,
113 apparently */
114 if (jffs2_cleanmarker_oob(c))
Grant Erickson1ddd0d92011-04-08 08:51:34 -0700115 try_size = c->sector_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 else
Grant Erickson1ddd0d92011-04-08 08:51:34 -0700117 try_size = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Joe Perches9c261b32012-02-15 15:56:43 -0800119 jffs2_dbg(1, "Trying to allocate readbuf of %zu "
120 "bytes\n", try_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Grant Erickson1ddd0d92011-04-08 08:51:34 -0700122 flashbuf = mtd_kmalloc_up_to(c->mtd, &try_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 if (!flashbuf)
124 return -ENOMEM;
Grant Erickson1ddd0d92011-04-08 08:51:34 -0700125
Joe Perches9c261b32012-02-15 15:56:43 -0800126 jffs2_dbg(1, "Allocated readbuf of %zu bytes\n",
127 try_size);
Grant Erickson1ddd0d92011-04-08 08:51:34 -0700128
129 buf_size = (uint32_t)try_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
131
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100132 if (jffs2_sum_active()) {
Yan Burman3d375d92006-12-04 15:03:01 -0800133 s = kzalloc(sizeof(struct jffs2_summary), GFP_KERNEL);
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100134 if (!s) {
135 JFFS2_WARNING("Can't allocate memory for summary\n");
David Woodhouse48396412009-06-23 01:34:19 +0100136 ret = -ENOMEM;
137 goto out;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100138 }
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100139 }
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 for (i=0; i<c->nr_blocks; i++) {
142 struct jffs2_eraseblock *jeb = &c->blocks[i];
143
Artem Bityutskiya2166b92006-12-28 12:01:41 +0200144 cond_resched();
145
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100146 /* reset summary info for next eraseblock scan */
147 jffs2_sum_reset_collected(s);
148
149 ret = jffs2_scan_eraseblock(c, jeb, buf_size?flashbuf:(flashbuf+jeb->offset),
150 buf_size, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 if (ret < 0)
153 goto out;
154
Artem B. Bityutskiye0c8e422005-07-24 16:14:17 +0100155 jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 /* Now decide which list to put it on */
158 switch(ret) {
159 case BLK_STATE_ALLFF:
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000160 /*
161 * Empty block. Since we can't be sure it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 * was entirely erased, we just queue it for erase
163 * again. It will be marked as such when the erase
164 * is complete. Meanwhile we still count it as empty
165 * for later checks.
166 */
167 empty_blocks++;
168 list_add(&jeb->list, &c->erase_pending_list);
169 c->nr_erasing_blocks++;
170 break;
171
172 case BLK_STATE_CLEANMARKER:
173 /* Only a CLEANMARKER node is valid */
174 if (!jeb->dirty_size) {
175 /* It's actually free */
176 list_add(&jeb->list, &c->free_list);
177 c->nr_free_blocks++;
178 } else {
179 /* Dirt */
Joe Perches9c261b32012-02-15 15:56:43 -0800180 jffs2_dbg(1, "Adding all-dirty block at 0x%08x to erase_pending_list\n",
181 jeb->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 list_add(&jeb->list, &c->erase_pending_list);
183 c->nr_erasing_blocks++;
184 }
185 break;
186
187 case BLK_STATE_CLEAN:
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100188 /* Full (or almost full) of clean data. Clean list */
189 list_add(&jeb->list, &c->clean_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 break;
191
192 case BLK_STATE_PARTDIRTY:
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100193 /* Some data, but not full. Dirty list. */
194 /* We want to remember the block with most free space
195 and stick it in the 'nextblock' position to start writing to it. */
196 if (jeb->free_size > min_free(c) &&
197 (!c->nextblock || c->nextblock->free_size < jeb->free_size)) {
198 /* Better candidate for the next writes to go to */
199 if (c->nextblock) {
David Woodhouse25090a62006-05-21 03:57:56 +0100200 ret = file_dirty(c, c->nextblock);
201 if (ret)
Christian Engelmayera2ab0ce2009-06-13 23:06:29 +0200202 goto out;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100203 /* deleting summary information of the old nextblock */
204 jffs2_sum_reset_collected(c->summary);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
David Woodhouse25090a62006-05-21 03:57:56 +0100206 /* update collected summary information for the current nextblock */
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100207 jffs2_sum_move_collected(c, s);
Joe Perches9c261b32012-02-15 15:56:43 -0800208 jffs2_dbg(1, "%s(): new nextblock = 0x%08x\n",
209 __func__, jeb->offset);
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100210 c->nextblock = jeb;
211 } else {
David Woodhouse25090a62006-05-21 03:57:56 +0100212 ret = file_dirty(c, jeb);
213 if (ret)
Christian Engelmayera2ab0ce2009-06-13 23:06:29 +0200214 goto out;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 break;
217
218 case BLK_STATE_ALLDIRTY:
219 /* Nothing valid - not even a clean marker. Needs erasing. */
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100220 /* For now we just put it on the erasing list. We'll start the erases later */
Joe Perches9c261b32012-02-15 15:56:43 -0800221 jffs2_dbg(1, "JFFS2: Erase block at 0x%08x is not formatted. It will be erased\n",
222 jeb->offset);
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100223 list_add(&jeb->list, &c->erase_pending_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 c->nr_erasing_blocks++;
225 break;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 case BLK_STATE_BADBLOCK:
Joe Perches9c261b32012-02-15 15:56:43 -0800228 jffs2_dbg(1, "JFFS2: Block at 0x%08x is bad\n",
229 jeb->offset);
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100230 list_add(&jeb->list, &c->bad_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 c->bad_size += c->sector_size;
232 c->free_size -= c->sector_size;
233 bad_blocks++;
234 break;
235 default:
236 printk(KERN_WARNING "jffs2_scan_medium(): unknown block state\n");
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100237 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
239 }
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 /* Nextblock dirty is always seen as wasted, because we cannot recycle it now */
242 if (c->nextblock && (c->nextblock->dirty_size)) {
243 c->nextblock->wasted_size += c->nextblock->dirty_size;
244 c->wasted_size += c->nextblock->dirty_size;
245 c->dirty_size -= c->nextblock->dirty_size;
246 c->nextblock->dirty_size = 0;
247 }
Andrew Victor2f82ce12005-02-09 09:24:26 +0000248#ifdef CONFIG_JFFS2_FS_WRITEBUFFER
David Woodhousee96fb232006-03-07 21:55:36 -0800249 if (!jffs2_can_mark_obsolete(c) && c->wbuf_pagesize && c->nextblock && (c->nextblock->free_size % c->wbuf_pagesize)) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000250 /* If we're going to start writing into a block which already
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 contains data, and the end of the data isn't page-aligned,
252 skip a little and align it. */
253
Artem B. Bityutskiydaba5cc2005-09-30 14:59:17 +0100254 uint32_t skip = c->nextblock->free_size % c->wbuf_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Joe Perches9c261b32012-02-15 15:56:43 -0800256 jffs2_dbg(1, "%s(): Skipping %d bytes in nextblock to ensure page alignment\n",
257 __func__, skip);
David Woodhouse046b8b92006-05-25 01:50:35 +0100258 jffs2_prealloc_raw_node_refs(c, c->nextblock, 1);
David Woodhousef5609282006-05-25 01:37:28 +0100259 jffs2_scan_dirty_space(c, c->nextblock, skip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261#endif
262 if (c->nr_erasing_blocks) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000263 if ( !c->used_size && ((c->nr_free_blocks+empty_blocks+bad_blocks)!= c->nr_blocks || bad_blocks == c->nr_blocks) ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 printk(KERN_NOTICE "Cowardly refusing to erase blocks on filesystem with no valid JFFS2 nodes\n");
265 printk(KERN_NOTICE "empty_blocks %d, bad_blocks %d, c->nr_blocks %d\n",empty_blocks,bad_blocks,c->nr_blocks);
266 ret = -EIO;
267 goto out;
268 }
David Woodhouseae3b6ba2010-05-19 17:05:14 +0100269 spin_lock(&c->erase_completion_lock);
270 jffs2_garbage_collect_trigger(c);
271 spin_unlock(&c->erase_completion_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
273 ret = 0;
274 out:
275 if (buf_size)
276 kfree(flashbuf);
277#ifndef __ECOS
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000278 else
Artem Bityutskiy72197782011-12-23 17:05:52 +0200279 mtd_unpoint(c->mtd, 0, c->mtd->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280#endif
Jesper Juhle8a0e412011-06-13 22:16:44 +0200281 kfree(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return ret;
283}
284
Adrian Bunkc05d52c2006-06-22 12:03:35 +0200285static int jffs2_fill_scan_buf(struct jffs2_sb_info *c, void *buf,
286 uint32_t ofs, uint32_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 int ret;
289 size_t retlen;
290
291 ret = jffs2_flash_read(c, ofs, len, &retlen, buf);
292 if (ret) {
Joe Perches9c261b32012-02-15 15:56:43 -0800293 jffs2_dbg(1, "mtd->read(0x%x bytes from 0x%x) returned %d\n",
294 len, ofs, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return ret;
296 }
297 if (retlen < len) {
Joe Perches9c261b32012-02-15 15:56:43 -0800298 jffs2_dbg(1, "Read at 0x%x gave only 0x%zx bytes\n",
299 ofs, retlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return -EIO;
301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return 0;
303}
304
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100305int jffs2_scan_classify_jeb(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
306{
307 if ((jeb->used_size + jeb->unchecked_size) == PAD(c->cleanmarker_size) && !jeb->dirty_size
David Woodhouse99988f72006-05-24 09:04:17 +0100308 && (!jeb->first_node || !ref_next(jeb->first_node)) )
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100309 return BLK_STATE_CLEANMARKER;
310
311 /* move blocks with max 4 byte dirty space to cleanlist */
312 else if (!ISDIRTY(c->sector_size - (jeb->used_size + jeb->unchecked_size))) {
313 c->dirty_size -= jeb->dirty_size;
314 c->wasted_size += jeb->dirty_size;
315 jeb->wasted_size += jeb->dirty_size;
316 jeb->dirty_size = 0;
317 return BLK_STATE_CLEAN;
318 } else if (jeb->used_size || jeb->unchecked_size)
319 return BLK_STATE_PARTDIRTY;
320 else
321 return BLK_STATE_ALLDIRTY;
322}
323
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900324#ifdef CONFIG_JFFS2_FS_XATTR
325static int jffs2_scan_xattr_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
326 struct jffs2_raw_xattr *rx, uint32_t ofs,
327 struct jffs2_summary *s)
328{
329 struct jffs2_xattr_datum *xd;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900330 uint32_t xid, version, totlen, crc;
David Woodhouse68270992006-05-21 03:46:05 +0100331 int err;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900332
333 crc = crc32(0, rx, sizeof(struct jffs2_raw_xattr) - 4);
334 if (crc != je32_to_cpu(rx->node_crc)) {
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900335 JFFS2_WARNING("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
336 ofs, je32_to_cpu(rx->node_crc), crc);
David Woodhouse68270992006-05-21 03:46:05 +0100337 if ((err = jffs2_scan_dirty_space(c, jeb, je32_to_cpu(rx->totlen))))
338 return err;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900339 return 0;
340 }
341
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900342 xid = je32_to_cpu(rx->xid);
343 version = je32_to_cpu(rx->version);
344
KaiGai Kohei8a136952006-06-24 09:14:13 +0900345 totlen = PAD(sizeof(struct jffs2_raw_xattr)
346 + rx->name_len + 1 + je16_to_cpu(rx->value_len));
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900347 if (totlen != je32_to_cpu(rx->totlen)) {
348 JFFS2_WARNING("node length mismatch at %#08x, read=%u, calc=%u\n",
349 ofs, je32_to_cpu(rx->totlen), totlen);
David Woodhouse68270992006-05-21 03:46:05 +0100350 if ((err = jffs2_scan_dirty_space(c, jeb, je32_to_cpu(rx->totlen))))
351 return err;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900352 return 0;
353 }
354
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900355 xd = jffs2_setup_xattr_datum(c, xid, version);
356 if (IS_ERR(xd))
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900357 return PTR_ERR(xd);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900358
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900359 if (xd->version > version) {
360 struct jffs2_raw_node_ref *raw
361 = jffs2_link_node_ref(c, jeb, ofs | REF_PRISTINE, totlen, NULL);
362 raw->next_in_ino = xd->node->next_in_ino;
363 xd->node->next_in_ino = raw;
364 } else {
365 xd->version = version;
366 xd->xprefix = rx->xprefix;
367 xd->name_len = rx->name_len;
368 xd->value_len = je16_to_cpu(rx->value_len);
369 xd->data_crc = je32_to_cpu(rx->data_crc);
370
371 jffs2_link_node_ref(c, jeb, ofs | REF_PRISTINE, totlen, (void *)xd);
372 }
David Woodhousef1f96712006-05-20 19:45:26 +0100373
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900374 if (jffs2_sum_active())
375 jffs2_sum_add_xattr_mem(s, rx, ofs - jeb->offset);
Masanari Iida3e341742012-01-28 00:17:28 +0900376 dbg_xattr("scanning xdatum at %#08x (xid=%u, version=%u)\n",
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900377 ofs, xd->xid, xd->version);
378 return 0;
379}
380
381static int jffs2_scan_xref_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
382 struct jffs2_raw_xref *rr, uint32_t ofs,
383 struct jffs2_summary *s)
384{
385 struct jffs2_xattr_ref *ref;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900386 uint32_t crc;
David Woodhouse68270992006-05-21 03:46:05 +0100387 int err;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900388
389 crc = crc32(0, rr, sizeof(*rr) - 4);
390 if (crc != je32_to_cpu(rr->node_crc)) {
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900391 JFFS2_WARNING("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
392 ofs, je32_to_cpu(rr->node_crc), crc);
David Woodhouse68270992006-05-21 03:46:05 +0100393 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(rr->totlen)))))
394 return err;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900395 return 0;
396 }
397
398 if (PAD(sizeof(struct jffs2_raw_xref)) != je32_to_cpu(rr->totlen)) {
David Woodhouse89291a92006-05-25 13:30:24 +0100399 JFFS2_WARNING("node length mismatch at %#08x, read=%u, calc=%zd\n",
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900400 ofs, je32_to_cpu(rr->totlen),
401 PAD(sizeof(struct jffs2_raw_xref)));
David Woodhouse68270992006-05-21 03:46:05 +0100402 if ((err = jffs2_scan_dirty_space(c, jeb, je32_to_cpu(rr->totlen))))
403 return err;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900404 return 0;
405 }
406
407 ref = jffs2_alloc_xattr_ref();
408 if (!ref)
409 return -ENOMEM;
410
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900411 /* BEFORE jffs2_build_xattr_subsystem() called,
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900412 * and AFTER xattr_ref is marked as a dead xref,
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900413 * ref->xid is used to store 32bit xid, xd is not used
414 * ref->ino is used to store 32bit inode-number, ic is not used
415 * Thoes variables are declared as union, thus using those
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900416 * are exclusive. In a similar way, ref->next is temporarily
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900417 * used to chain all xattr_ref object. It's re-chained to
418 * jffs2_inode_cache in jffs2_build_xattr_subsystem() correctly.
419 */
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900420 ref->ino = je32_to_cpu(rr->ino);
421 ref->xid = je32_to_cpu(rr->xid);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900422 ref->xseqno = je32_to_cpu(rr->xseqno);
423 if (ref->xseqno > c->highest_xseqno)
424 c->highest_xseqno = (ref->xseqno & ~XREF_DELETE_MARKER);
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900425 ref->next = c->xref_temp;
426 c->xref_temp = ref;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900427
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900428 jffs2_link_node_ref(c, jeb, ofs | REF_PRISTINE, PAD(je32_to_cpu(rr->totlen)), (void *)ref);
David Woodhousef1f96712006-05-20 19:45:26 +0100429
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900430 if (jffs2_sum_active())
431 jffs2_sum_add_xref_mem(s, rr, ofs - jeb->offset);
432 dbg_xattr("scan xref at %#08x (xid=%u, ino=%u)\n",
433 ofs, ref->xid, ref->ino);
434 return 0;
435}
436#endif
437
David Woodhouse9641b782006-05-20 16:13:34 +0100438/* Called with 'buf_size == 0' if buf is in fact a pointer _directly_ into
439 the flash, XIP-style */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
David Woodhouse9641b782006-05-20 16:13:34 +0100441 unsigned char *buf, uint32_t buf_size, struct jffs2_summary *s) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 struct jffs2_unknown_node *node;
443 struct jffs2_unknown_node crcnode;
Joakim Tjernlund41bdc602010-10-07 19:09:34 +0200444 uint32_t ofs, prevofs, max_ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 uint32_t hdr_crc, buf_ofs, buf_len;
446 int err;
447 int noise = 0;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100448
449
Andrew Victor2f82ce12005-02-09 09:24:26 +0000450#ifdef CONFIG_JFFS2_FS_WRITEBUFFER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 int cleanmarkerfound = 0;
452#endif
453
454 ofs = jeb->offset;
455 prevofs = jeb->offset - 1;
456
Joe Perches9c261b32012-02-15 15:56:43 -0800457 jffs2_dbg(1, "%s(): Scanning block at 0x%x\n", __func__, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Andrew Victor2f82ce12005-02-09 09:24:26 +0000459#ifdef CONFIG_JFFS2_FS_WRITEBUFFER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (jffs2_cleanmarker_oob(c)) {
Artem Bityutskiya7a6ace12007-01-31 11:38:53 +0200461 int ret;
462
Artem Bityutskiy7086c192011-12-23 19:35:30 +0200463 if (mtd_block_isbad(c->mtd, jeb->offset))
Artem Bityutskiya7a6ace12007-01-31 11:38:53 +0200464 return BLK_STATE_BADBLOCK;
465
466 ret = jffs2_check_nand_cleanmarker(c, jeb);
Joe Perches9c261b32012-02-15 15:56:43 -0800467 jffs2_dbg(2, "jffs_check_nand_cleanmarker returned %d\n", ret);
Artem Bityutskiya7a6ace12007-01-31 11:38:53 +0200468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 /* Even if it's not found, we still scan to see
470 if the block is empty. We use this information
471 to decide whether to erase it or not. */
472 switch (ret) {
473 case 0: cleanmarkerfound = 1; break;
474 case 1: break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 default: return ret;
476 }
477 }
478#endif
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100479
480 if (jffs2_sum_active()) {
David Woodhouse9641b782006-05-20 16:13:34 +0100481 struct jffs2_sum_marker *sm;
482 void *sumptr = NULL;
483 uint32_t sumlen;
484
485 if (!buf_size) {
486 /* XIP case. Just look, point at the summary if it's there */
David Woodhouse13ba42d2006-05-30 08:59:34 +0100487 sm = (void *)buf + c->sector_size - sizeof(*sm);
David Woodhouse9641b782006-05-20 16:13:34 +0100488 if (je32_to_cpu(sm->magic) == JFFS2_SUM_MAGIC) {
489 sumptr = buf + je32_to_cpu(sm->offset);
490 sumlen = c->sector_size - je32_to_cpu(sm->offset);
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100491 }
David Woodhouse9641b782006-05-20 16:13:34 +0100492 } else {
493 /* If NAND flash, read a whole page of it. Else just the end */
494 if (c->wbuf_pagesize)
495 buf_len = c->wbuf_pagesize;
496 else
497 buf_len = sizeof(*sm);
498
499 /* Read as much as we want into the _end_ of the preallocated buffer */
500 err = jffs2_fill_scan_buf(c, buf + buf_size - buf_len,
501 jeb->offset + c->sector_size - buf_len,
502 buf_len);
503 if (err)
504 return err;
505
506 sm = (void *)buf + buf_size - sizeof(*sm);
507 if (je32_to_cpu(sm->magic) == JFFS2_SUM_MAGIC) {
508 sumlen = c->sector_size - je32_to_cpu(sm->offset);
509 sumptr = buf + buf_size - sumlen;
510
511 /* Now, make sure the summary itself is available */
512 if (sumlen > buf_size) {
513 /* Need to kmalloc for this. */
514 sumptr = kmalloc(sumlen, GFP_KERNEL);
515 if (!sumptr)
516 return -ENOMEM;
517 memcpy(sumptr + sumlen - buf_len, buf + buf_size - buf_len, buf_len);
518 }
519 if (buf_len < sumlen) {
520 /* Need to read more so that the entire summary node is present */
521 err = jffs2_fill_scan_buf(c, sumptr,
522 jeb->offset + c->sector_size - sumlen,
523 sumlen - buf_len);
524 if (err)
525 return err;
526 }
527 }
528
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100529 }
530
David Woodhouse9641b782006-05-20 16:13:34 +0100531 if (sumptr) {
532 err = jffs2_sum_scan_sumnode(c, jeb, sumptr, sumlen, &pseudo_random);
David Woodhouse35601602006-05-21 01:28:05 +0100533
David Woodhouse9641b782006-05-20 16:13:34 +0100534 if (buf_size && sumlen > buf_size)
535 kfree(sumptr);
David Woodhouse35601602006-05-21 01:28:05 +0100536 /* If it returns with a real error, bail.
537 If it returns positive, that's a block classification
538 (i.e. BLK_STATE_xxx) so return that too.
539 If it returns zero, fall through to full scan. */
540 if (err)
541 return err;
David Woodhouse9641b782006-05-20 16:13:34 +0100542 }
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100543 }
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 buf_ofs = jeb->offset;
546
547 if (!buf_size) {
David Woodhouse9641b782006-05-20 16:13:34 +0100548 /* This is the XIP case -- we're reading _directly_ from the flash chip */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 buf_len = c->sector_size;
550 } else {
Andrew Victor3be36672005-02-09 09:09:05 +0000551 buf_len = EMPTY_SCAN_SIZE(c->sector_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 err = jffs2_fill_scan_buf(c, buf, buf_ofs, buf_len);
553 if (err)
554 return err;
555 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 /* We temporarily use 'ofs' as a pointer into the buffer/jeb */
558 ofs = 0;
Joakim Tjernlund41bdc602010-10-07 19:09:34 +0200559 max_ofs = EMPTY_SCAN_SIZE(c->sector_size);
560 /* Scan only EMPTY_SCAN_SIZE of 0xFF before declaring it's empty */
561 while(ofs < max_ofs && *(uint32_t *)(&buf[ofs]) == 0xFFFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 ofs += 4;
563
Joakim Tjernlund41bdc602010-10-07 19:09:34 +0200564 if (ofs == max_ofs) {
Andrew Victor2f82ce12005-02-09 09:24:26 +0000565#ifdef CONFIG_JFFS2_FS_WRITEBUFFER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (jffs2_cleanmarker_oob(c)) {
567 /* scan oob, take care of cleanmarker */
568 int ret = jffs2_check_oob_empty(c, jeb, cleanmarkerfound);
Joe Perches9c261b32012-02-15 15:56:43 -0800569 jffs2_dbg(2, "jffs2_check_oob_empty returned %d\n",
570 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 switch (ret) {
572 case 0: return cleanmarkerfound ? BLK_STATE_CLEANMARKER : BLK_STATE_ALLFF;
573 case 1: return BLK_STATE_ALLDIRTY;
574 default: return ret;
575 }
576 }
577#endif
Joe Perches9c261b32012-02-15 15:56:43 -0800578 jffs2_dbg(1, "Block at 0x%08x is empty (erased)\n",
579 jeb->offset);
Andrew Victor8f15fd52005-02-09 09:17:45 +0000580 if (c->cleanmarker_size == 0)
581 return BLK_STATE_CLEANMARKER; /* don't bother with re-erase */
582 else
583 return BLK_STATE_ALLFF; /* OK to erase if all blocks are like this */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585 if (ofs) {
Joe Perches9c261b32012-02-15 15:56:43 -0800586 jffs2_dbg(1, "Free space at %08x ends at %08x\n", jeb->offset,
587 jeb->offset + ofs);
David Woodhousea6a8bef2006-05-29 00:41:11 +0100588 if ((err = jffs2_prealloc_raw_node_refs(c, jeb, 1)))
589 return err;
David Woodhouse68270992006-05-21 03:46:05 +0100590 if ((err = jffs2_scan_dirty_space(c, jeb, ofs)))
591 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
593
594 /* Now ofs is a complete physical flash offset as it always was... */
595 ofs += jeb->offset;
596
597 noise = 10;
598
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100599 dbg_summary("no summary found in jeb 0x%08x. Apply original scan.\n",jeb->offset);
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100600
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000601scan_more:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 while(ofs < jeb->offset + c->sector_size) {
603
Artem B. Bityutskiye0c8e422005-07-24 16:14:17 +0100604 jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
David Woodhouse2f785402006-05-24 02:04:45 +0100606 /* Make sure there are node refs available for use */
David Woodhouse046b8b92006-05-25 01:50:35 +0100607 err = jffs2_prealloc_raw_node_refs(c, jeb, 2);
David Woodhouse2f785402006-05-24 02:04:45 +0100608 if (err)
609 return err;
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 cond_resched();
612
613 if (ofs & 3) {
614 printk(KERN_WARNING "Eep. ofs 0x%08x not word-aligned!\n", ofs);
615 ofs = PAD(ofs);
616 continue;
617 }
618 if (ofs == prevofs) {
619 printk(KERN_WARNING "ofs 0x%08x has already been seen. Skipping\n", ofs);
David Woodhouse68270992006-05-21 03:46:05 +0100620 if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
621 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 ofs += 4;
623 continue;
624 }
625 prevofs = ofs;
626
627 if (jeb->offset + c->sector_size < ofs + sizeof(*node)) {
Joe Perches9c261b32012-02-15 15:56:43 -0800628 jffs2_dbg(1, "Fewer than %zd bytes left to end of block. (%x+%x<%x+%zx) Not reading\n",
629 sizeof(struct jffs2_unknown_node),
630 jeb->offset, c->sector_size, ofs,
631 sizeof(*node));
David Woodhouse68270992006-05-21 03:46:05 +0100632 if ((err = jffs2_scan_dirty_space(c, jeb, (jeb->offset + c->sector_size)-ofs)))
633 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 break;
635 }
636
637 if (buf_ofs + buf_len < ofs + sizeof(*node)) {
638 buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
Joe Perches9c261b32012-02-15 15:56:43 -0800639 jffs2_dbg(1, "Fewer than %zd bytes (node header) left to end of buf. Reading 0x%x at 0x%08x\n",
640 sizeof(struct jffs2_unknown_node),
641 buf_len, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
643 if (err)
644 return err;
645 buf_ofs = ofs;
646 }
647
648 node = (struct jffs2_unknown_node *)&buf[ofs-buf_ofs];
649
650 if (*(uint32_t *)(&buf[ofs-buf_ofs]) == 0xffffffff) {
651 uint32_t inbuf_ofs;
Joakim Tjernlundc2aecda2007-03-27 13:32:09 +0200652 uint32_t empty_start, scan_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 empty_start = ofs;
655 ofs += 4;
Joakim Tjernlundc2aecda2007-03-27 13:32:09 +0200656 scan_end = min_t(uint32_t, EMPTY_SCAN_SIZE(c->sector_size)/8, buf_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Joe Perches9c261b32012-02-15 15:56:43 -0800658 jffs2_dbg(1, "Found empty flash at 0x%08x\n", ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 more_empty:
660 inbuf_ofs = ofs - buf_ofs;
Joakim Tjernlundc2aecda2007-03-27 13:32:09 +0200661 while (inbuf_ofs < scan_end) {
662 if (unlikely(*(uint32_t *)(&buf[inbuf_ofs]) != 0xffffffff)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 printk(KERN_WARNING "Empty flash at 0x%08x ends at 0x%08x\n",
664 empty_start, ofs);
David Woodhouse68270992006-05-21 03:46:05 +0100665 if ((err = jffs2_scan_dirty_space(c, jeb, ofs-empty_start)))
666 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 goto scan_more;
668 }
669
670 inbuf_ofs+=4;
671 ofs += 4;
672 }
673 /* Ran off end. */
Joe Perches9c261b32012-02-15 15:56:43 -0800674 jffs2_dbg(1, "Empty flash to end of buffer at 0x%08x\n",
675 ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 /* If we're only checking the beginning of a block with a cleanmarker,
678 bail now */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000679 if (buf_ofs == jeb->offset && jeb->used_size == PAD(c->cleanmarker_size) &&
David Woodhouse99988f72006-05-24 09:04:17 +0100680 c->cleanmarker_size && !jeb->dirty_size && !ref_next(jeb->first_node)) {
Joe Perches9c261b32012-02-15 15:56:43 -0800681 jffs2_dbg(1, "%d bytes at start of block seems clean... assuming all clean\n",
682 EMPTY_SCAN_SIZE(c->sector_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return BLK_STATE_CLEANMARKER;
684 }
Joakim Tjernlundc2aecda2007-03-27 13:32:09 +0200685 if (!buf_size && (scan_end != buf_len)) {/* XIP/point case */
686 scan_end = buf_len;
687 goto more_empty;
688 }
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 /* See how much more there is to read in this eraseblock... */
691 buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
692 if (!buf_len) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000693 /* No more to read. Break out of main loop without marking
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 this range of empty space as dirty (because it's not) */
Joe Perches9c261b32012-02-15 15:56:43 -0800695 jffs2_dbg(1, "Empty flash at %08x runs to end of block. Treating as free_space\n",
696 empty_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 break;
698 }
Joakim Tjernlundc2aecda2007-03-27 13:32:09 +0200699 /* point never reaches here */
700 scan_end = buf_len;
Joe Perches9c261b32012-02-15 15:56:43 -0800701 jffs2_dbg(1, "Reading another 0x%x at 0x%08x\n",
702 buf_len, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
704 if (err)
705 return err;
706 buf_ofs = ofs;
707 goto more_empty;
708 }
709
710 if (ofs == jeb->offset && je16_to_cpu(node->magic) == KSAMTIB_CIGAM_2SFFJ) {
711 printk(KERN_WARNING "Magic bitmask is backwards at offset 0x%08x. Wrong endian filesystem?\n", ofs);
David Woodhouse68270992006-05-21 03:46:05 +0100712 if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
713 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 ofs += 4;
715 continue;
716 }
717 if (je16_to_cpu(node->magic) == JFFS2_DIRTY_BITMASK) {
Joe Perches9c261b32012-02-15 15:56:43 -0800718 jffs2_dbg(1, "Dirty bitmask at 0x%08x\n", ofs);
David Woodhouse68270992006-05-21 03:46:05 +0100719 if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
720 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 ofs += 4;
722 continue;
723 }
724 if (je16_to_cpu(node->magic) == JFFS2_OLD_MAGIC_BITMASK) {
725 printk(KERN_WARNING "Old JFFS2 bitmask found at 0x%08x\n", ofs);
726 printk(KERN_WARNING "You cannot use older JFFS2 filesystems with newer kernels\n");
David Woodhouse68270992006-05-21 03:46:05 +0100727 if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
728 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 ofs += 4;
730 continue;
731 }
732 if (je16_to_cpu(node->magic) != JFFS2_MAGIC_BITMASK) {
733 /* OK. We're out of possibilities. Whinge and move on */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000734 noisy_printk(&noise, "jffs2_scan_eraseblock(): Magic bitmask 0x%04x not found at 0x%08x: 0x%04x instead\n",
735 JFFS2_MAGIC_BITMASK, ofs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 je16_to_cpu(node->magic));
David Woodhouse68270992006-05-21 03:46:05 +0100737 if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
738 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 ofs += 4;
740 continue;
741 }
742 /* We seem to have a node of sorts. Check the CRC */
743 crcnode.magic = node->magic;
744 crcnode.nodetype = cpu_to_je16( je16_to_cpu(node->nodetype) | JFFS2_NODE_ACCURATE);
745 crcnode.totlen = node->totlen;
746 hdr_crc = crc32(0, &crcnode, sizeof(crcnode)-4);
747
748 if (hdr_crc != je32_to_cpu(node->hdr_crc)) {
749 noisy_printk(&noise, "jffs2_scan_eraseblock(): Node at 0x%08x {0x%04x, 0x%04x, 0x%08x) has invalid CRC 0x%08x (calculated 0x%08x)\n",
750 ofs, je16_to_cpu(node->magic),
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000751 je16_to_cpu(node->nodetype),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 je32_to_cpu(node->totlen),
753 je32_to_cpu(node->hdr_crc),
754 hdr_crc);
David Woodhouse68270992006-05-21 03:46:05 +0100755 if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
756 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 ofs += 4;
758 continue;
759 }
760
Joakim Tjernlund0dec4c82007-03-10 17:08:44 +0100761 if (ofs + je32_to_cpu(node->totlen) > jeb->offset + c->sector_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 /* Eep. Node goes over the end of the erase block. */
763 printk(KERN_WARNING "Node at 0x%08x with length 0x%08x would run over the end of the erase block\n",
764 ofs, je32_to_cpu(node->totlen));
765 printk(KERN_WARNING "Perhaps the file system was created with the wrong erase size?\n");
David Woodhouse68270992006-05-21 03:46:05 +0100766 if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
767 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 ofs += 4;
769 continue;
770 }
771
772 if (!(je16_to_cpu(node->nodetype) & JFFS2_NODE_ACCURATE)) {
773 /* Wheee. This is an obsoleted node */
Joe Perches9c261b32012-02-15 15:56:43 -0800774 jffs2_dbg(2, "Node at 0x%08x is obsolete. Skipping\n",
775 ofs);
David Woodhouse68270992006-05-21 03:46:05 +0100776 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen)))))
777 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 ofs += PAD(je32_to_cpu(node->totlen));
779 continue;
780 }
781
782 switch(je16_to_cpu(node->nodetype)) {
783 case JFFS2_NODETYPE_INODE:
784 if (buf_ofs + buf_len < ofs + sizeof(struct jffs2_raw_inode)) {
785 buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
Joe Perches9c261b32012-02-15 15:56:43 -0800786 jffs2_dbg(1, "Fewer than %zd bytes (inode node) left to end of buf. Reading 0x%x at 0x%08x\n",
787 sizeof(struct jffs2_raw_inode),
788 buf_len, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
790 if (err)
791 return err;
792 buf_ofs = ofs;
793 node = (void *)buf;
794 }
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100795 err = jffs2_scan_inode_node(c, jeb, (void *)node, ofs, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (err) return err;
797 ofs += PAD(je32_to_cpu(node->totlen));
798 break;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 case JFFS2_NODETYPE_DIRENT:
801 if (buf_ofs + buf_len < ofs + je32_to_cpu(node->totlen)) {
802 buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
Joe Perches9c261b32012-02-15 15:56:43 -0800803 jffs2_dbg(1, "Fewer than %d bytes (dirent node) left to end of buf. Reading 0x%x at 0x%08x\n",
804 je32_to_cpu(node->totlen), buf_len,
805 ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
807 if (err)
808 return err;
809 buf_ofs = ofs;
810 node = (void *)buf;
811 }
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100812 err = jffs2_scan_dirent_node(c, jeb, (void *)node, ofs, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 if (err) return err;
814 ofs += PAD(je32_to_cpu(node->totlen));
815 break;
816
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900817#ifdef CONFIG_JFFS2_FS_XATTR
818 case JFFS2_NODETYPE_XATTR:
819 if (buf_ofs + buf_len < ofs + je32_to_cpu(node->totlen)) {
820 buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
Joe Perches9c261b32012-02-15 15:56:43 -0800821 jffs2_dbg(1, "Fewer than %d bytes (xattr node) left to end of buf. Reading 0x%x at 0x%08x\n",
822 je32_to_cpu(node->totlen), buf_len,
823 ofs);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900824 err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
825 if (err)
826 return err;
827 buf_ofs = ofs;
828 node = (void *)buf;
829 }
830 err = jffs2_scan_xattr_node(c, jeb, (void *)node, ofs, s);
831 if (err)
832 return err;
833 ofs += PAD(je32_to_cpu(node->totlen));
834 break;
835 case JFFS2_NODETYPE_XREF:
836 if (buf_ofs + buf_len < ofs + je32_to_cpu(node->totlen)) {
837 buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
Joe Perches9c261b32012-02-15 15:56:43 -0800838 jffs2_dbg(1, "Fewer than %d bytes (xref node) left to end of buf. Reading 0x%x at 0x%08x\n",
839 je32_to_cpu(node->totlen), buf_len,
840 ofs);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900841 err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
842 if (err)
843 return err;
844 buf_ofs = ofs;
845 node = (void *)buf;
846 }
847 err = jffs2_scan_xref_node(c, jeb, (void *)node, ofs, s);
848 if (err)
849 return err;
850 ofs += PAD(je32_to_cpu(node->totlen));
851 break;
852#endif /* CONFIG_JFFS2_FS_XATTR */
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 case JFFS2_NODETYPE_CLEANMARKER:
Joe Perches9c261b32012-02-15 15:56:43 -0800855 jffs2_dbg(1, "CLEANMARKER node found at 0x%08x\n", ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (je32_to_cpu(node->totlen) != c->cleanmarker_size) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000857 printk(KERN_NOTICE "CLEANMARKER node found at 0x%08x has totlen 0x%x != normal 0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 ofs, je32_to_cpu(node->totlen), c->cleanmarker_size);
David Woodhouse68270992006-05-21 03:46:05 +0100859 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(sizeof(struct jffs2_unknown_node)))))
860 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 ofs += PAD(sizeof(struct jffs2_unknown_node));
862 } else if (jeb->first_node) {
863 printk(KERN_NOTICE "CLEANMARKER node found at 0x%08x, not first node in block (0x%08x)\n", ofs, jeb->offset);
David Woodhouse68270992006-05-21 03:46:05 +0100864 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(sizeof(struct jffs2_unknown_node)))))
865 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 ofs += PAD(sizeof(struct jffs2_unknown_node));
867 } else {
David Woodhouse2f785402006-05-24 02:04:45 +0100868 jffs2_link_node_ref(c, jeb, ofs | REF_NORMAL, c->cleanmarker_size, NULL);
David Woodhousef1f96712006-05-20 19:45:26 +0100869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 ofs += PAD(c->cleanmarker_size);
871 }
872 break;
873
874 case JFFS2_NODETYPE_PADDING:
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100875 if (jffs2_sum_active())
876 jffs2_sum_add_padding_mem(s, je32_to_cpu(node->totlen));
David Woodhouse68270992006-05-21 03:46:05 +0100877 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen)))))
878 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 ofs += PAD(je32_to_cpu(node->totlen));
880 break;
881
882 default:
883 switch (je16_to_cpu(node->nodetype) & JFFS2_COMPAT_MASK) {
884 case JFFS2_FEATURE_ROCOMPAT:
885 printk(KERN_NOTICE "Read-only compatible feature node (0x%04x) found at offset 0x%08x\n", je16_to_cpu(node->nodetype), ofs);
David Woodhouseef53cb02007-07-10 10:01:22 +0100886 c->flags |= JFFS2_SB_FLAG_RO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 if (!(jffs2_is_readonly(c)))
888 return -EROFS;
David Woodhouse68270992006-05-21 03:46:05 +0100889 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen)))))
890 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 ofs += PAD(je32_to_cpu(node->totlen));
892 break;
893
894 case JFFS2_FEATURE_INCOMPAT:
895 printk(KERN_NOTICE "Incompatible feature node (0x%04x) found at offset 0x%08x\n", je16_to_cpu(node->nodetype), ofs);
896 return -EINVAL;
897
898 case JFFS2_FEATURE_RWCOMPAT_DELETE:
Joe Perches9c261b32012-02-15 15:56:43 -0800899 jffs2_dbg(1, "Unknown but compatible feature node (0x%04x) found at offset 0x%08x\n",
900 je16_to_cpu(node->nodetype), ofs);
David Woodhouse68270992006-05-21 03:46:05 +0100901 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen)))))
902 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 ofs += PAD(je32_to_cpu(node->totlen));
904 break;
905
David Woodhouse61715862006-05-21 00:02:06 +0100906 case JFFS2_FEATURE_RWCOMPAT_COPY: {
Joe Perches9c261b32012-02-15 15:56:43 -0800907 jffs2_dbg(1, "Unknown but compatible feature node (0x%04x) found at offset 0x%08x\n",
908 je16_to_cpu(node->nodetype), ofs);
David Woodhouse61715862006-05-21 00:02:06 +0100909
David Woodhouse2f785402006-05-24 02:04:45 +0100910 jffs2_link_node_ref(c, jeb, ofs | REF_PRISTINE, PAD(je32_to_cpu(node->totlen)), NULL);
David Woodhouse61715862006-05-21 00:02:06 +0100911
912 /* We can't summarise nodes we don't grok */
913 jffs2_sum_disable_collecting(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 ofs += PAD(je32_to_cpu(node->totlen));
915 break;
David Woodhouse61715862006-05-21 00:02:06 +0100916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
918 }
919 }
920
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100921 if (jffs2_sum_active()) {
922 if (PAD(s->sum_size + JFFS2_SUMMARY_FRAME_SIZE) > jeb->free_size) {
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100923 dbg_summary("There is not enough space for "
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100924 "summary information, disabling for this jeb!\n");
925 jffs2_sum_disable_collecting(s);
926 }
927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Joe Perches9c261b32012-02-15 15:56:43 -0800929 jffs2_dbg(1, "Block at 0x%08x: free 0x%08x, dirty 0x%08x, unchecked 0x%08x, used 0x%08x, wasted 0x%08x\n",
930 jeb->offset, jeb->free_size, jeb->dirty_size,
931 jeb->unchecked_size, jeb->used_size, jeb->wasted_size);
David Woodhouse8b9e9fe2006-05-25 01:53:09 +0100932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 /* mark_node_obsolete can add to wasted !! */
934 if (jeb->wasted_size) {
935 jeb->dirty_size += jeb->wasted_size;
936 c->dirty_size += jeb->wasted_size;
937 c->wasted_size -= jeb->wasted_size;
938 jeb->wasted_size = 0;
939 }
940
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100941 return jffs2_scan_classify_jeb(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100944struct jffs2_inode_cache *jffs2_scan_make_ino_cache(struct jffs2_sb_info *c, uint32_t ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
946 struct jffs2_inode_cache *ic;
947
948 ic = jffs2_get_ino_cache(c, ino);
949 if (ic)
950 return ic;
951
952 if (ino > c->highest_ino)
953 c->highest_ino = ino;
954
955 ic = jffs2_alloc_inode_cache();
956 if (!ic) {
957 printk(KERN_NOTICE "jffs2_scan_make_inode_cache(): allocation of inode cache failed\n");
958 return NULL;
959 }
960 memset(ic, 0, sizeof(*ic));
961
962 ic->ino = ino;
963 ic->nodes = (void *)ic;
964 jffs2_add_ino_cache(c, ic);
965 if (ino == 1)
David Woodhouse27c72b02008-05-01 18:47:17 +0100966 ic->pino_nlink = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return ic;
968}
969
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000970static int jffs2_scan_inode_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100971 struct jffs2_raw_inode *ri, uint32_t ofs, struct jffs2_summary *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 struct jffs2_inode_cache *ic;
Thomas Gleixner53043002007-04-05 11:09:01 +0200974 uint32_t crc, ino = je32_to_cpu(ri->ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Joe Perches9c261b32012-02-15 15:56:43 -0800976 jffs2_dbg(1, "%s(): Node at 0x%08x\n", __func__, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 /* We do very little here now. Just check the ino# to which we should attribute
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000979 this node; we can do all the CRC checking etc. later. There's a tradeoff here --
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 we used to scan the flash once only, reading everything we want from it into
981 memory, then building all our in-core data structures and freeing the extra
982 information. Now we allow the first part of the mount to complete a lot quicker,
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000983 but we have to go _back_ to the flash in order to finish the CRC checking, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 Which means that the _full_ amount of time to get to proper write mode with GC
985 operational may actually be _longer_ than before. Sucks to be me. */
986
Thomas Gleixner53043002007-04-05 11:09:01 +0200987 /* Check the node CRC in any case. */
988 crc = crc32(0, ri, sizeof(*ri)-8);
989 if (crc != je32_to_cpu(ri->node_crc)) {
990 printk(KERN_NOTICE "jffs2_scan_inode_node(): CRC failed on "
991 "node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
992 ofs, je32_to_cpu(ri->node_crc), crc);
993 /*
994 * We believe totlen because the CRC on the node
995 * _header_ was OK, just the node itself failed.
996 */
997 return jffs2_scan_dirty_space(c, jeb,
998 PAD(je32_to_cpu(ri->totlen)));
999 }
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 ic = jffs2_get_ino_cache(c, ino);
1002 if (!ic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 ic = jffs2_scan_make_ino_cache(c, ino);
David Woodhouse2f785402006-05-24 02:04:45 +01001004 if (!ic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 }
1007
1008 /* Wheee. It worked */
David Woodhouse2f785402006-05-24 02:04:45 +01001009 jffs2_link_node_ref(c, jeb, ofs | REF_UNCHECKED, PAD(je32_to_cpu(ri->totlen)), ic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Joe Perches9c261b32012-02-15 15:56:43 -08001011 jffs2_dbg(1, "Node is ino #%u, version %d. Range 0x%x-0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 je32_to_cpu(ri->ino), je32_to_cpu(ri->version),
1013 je32_to_cpu(ri->offset),
Joe Perches9c261b32012-02-15 15:56:43 -08001014 je32_to_cpu(ri->offset)+je32_to_cpu(ri->dsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 pseudo_random += je32_to_cpu(ri->version);
1017
Ferenc Havasie631ddb2005-09-07 09:35:26 +01001018 if (jffs2_sum_active()) {
1019 jffs2_sum_add_inode_mem(s, ri, ofs - jeb->offset);
1020 }
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return 0;
1023}
1024
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001025static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
Ferenc Havasie631ddb2005-09-07 09:35:26 +01001026 struct jffs2_raw_dirent *rd, uint32_t ofs, struct jffs2_summary *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 struct jffs2_full_dirent *fd;
1029 struct jffs2_inode_cache *ic;
David Woodhouseb534e702007-10-13 11:35:58 +01001030 uint32_t checkedlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 uint32_t crc;
David Woodhouse68270992006-05-21 03:46:05 +01001032 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Joe Perches9c261b32012-02-15 15:56:43 -08001034 jffs2_dbg(1, "%s(): Node at 0x%08x\n", __func__, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 /* We don't get here unless the node is still valid, so we don't have to
1037 mask in the ACCURATE bit any more. */
1038 crc = crc32(0, rd, sizeof(*rd)-8);
1039
1040 if (crc != je32_to_cpu(rd->node_crc)) {
1041 printk(KERN_NOTICE "jffs2_scan_dirent_node(): Node CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
1042 ofs, je32_to_cpu(rd->node_crc), crc);
1043 /* We believe totlen because the CRC on the node _header_ was OK, just the node itself failed. */
David Woodhouse68270992006-05-21 03:46:05 +01001044 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(rd->totlen)))))
1045 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 return 0;
1047 }
1048
1049 pseudo_random += je32_to_cpu(rd->version);
1050
David Woodhouseb534e702007-10-13 11:35:58 +01001051 /* Should never happen. Did. (OLPC trac #4184)*/
1052 checkedlen = strnlen(rd->name, rd->nsize);
1053 if (checkedlen < rd->nsize) {
1054 printk(KERN_ERR "Dirent at %08x has zeroes in name. Truncating to %d chars\n",
1055 ofs, checkedlen);
1056 }
1057 fd = jffs2_alloc_full_dirent(checkedlen+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 if (!fd) {
1059 return -ENOMEM;
1060 }
David Woodhouseb534e702007-10-13 11:35:58 +01001061 memcpy(&fd->name, rd->name, checkedlen);
1062 fd->name[checkedlen] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 crc = crc32(0, fd->name, rd->nsize);
1065 if (crc != je32_to_cpu(rd->name_crc)) {
1066 printk(KERN_NOTICE "jffs2_scan_dirent_node(): Name CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001067 ofs, je32_to_cpu(rd->name_crc), crc);
Joe Perches9c261b32012-02-15 15:56:43 -08001068 jffs2_dbg(1, "Name for which CRC failed is (now) '%s', ino #%d\n",
1069 fd->name, je32_to_cpu(rd->ino));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 jffs2_free_full_dirent(fd);
1071 /* FIXME: Why do we believe totlen? */
1072 /* We believe totlen because the CRC on the node _header_ was OK, just the name failed. */
David Woodhouse68270992006-05-21 03:46:05 +01001073 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(rd->totlen)))))
1074 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return 0;
1076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 ic = jffs2_scan_make_ino_cache(c, je32_to_cpu(rd->pino));
1078 if (!ic) {
1079 jffs2_free_full_dirent(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 return -ENOMEM;
1081 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001082
David Woodhouse43dfa072007-06-29 13:39:57 +01001083 fd->raw = jffs2_link_node_ref(c, jeb, ofs | dirent_node_state(rd),
1084 PAD(je32_to_cpu(rd->totlen)), ic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 fd->next = NULL;
1087 fd->version = je32_to_cpu(rd->version);
1088 fd->ino = je32_to_cpu(rd->ino);
David Woodhouseb534e702007-10-13 11:35:58 +01001089 fd->nhash = full_name_hash(fd->name, checkedlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 fd->type = rd->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 jffs2_add_fd_to_list(c, fd, &ic->scan_dents);
1092
Ferenc Havasie631ddb2005-09-07 09:35:26 +01001093 if (jffs2_sum_active()) {
1094 jffs2_sum_add_dirent_mem(s, rd, ofs - jeb->offset);
1095 }
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return 0;
1098}
1099
1100static int count_list(struct list_head *l)
1101{
1102 uint32_t count = 0;
1103 struct list_head *tmp;
1104
1105 list_for_each(tmp, l) {
1106 count++;
1107 }
1108 return count;
1109}
1110
1111/* Note: This breaks if list_empty(head). I don't care. You
1112 might, if you copy this code and use it elsewhere :) */
1113static void rotate_list(struct list_head *head, uint32_t count)
1114{
1115 struct list_head *n = head->next;
1116
1117 list_del(head);
1118 while(count--) {
1119 n = n->next;
1120 }
1121 list_add(head, n);
1122}
1123
1124void jffs2_rotate_lists(struct jffs2_sb_info *c)
1125{
1126 uint32_t x;
1127 uint32_t rotateby;
1128
1129 x = count_list(&c->clean_list);
1130 if (x) {
1131 rotateby = pseudo_random % x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 rotate_list((&c->clean_list), rotateby);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 }
1134
1135 x = count_list(&c->very_dirty_list);
1136 if (x) {
1137 rotateby = pseudo_random % x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 rotate_list((&c->very_dirty_list), rotateby);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
1140
1141 x = count_list(&c->dirty_list);
1142 if (x) {
1143 rotateby = pseudo_random % x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 rotate_list((&c->dirty_list), rotateby);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 }
1146
1147 x = count_list(&c->erasable_list);
1148 if (x) {
1149 rotateby = pseudo_random % x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 rotate_list((&c->erasable_list), rotateby);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 }
1152
1153 if (c->nr_erasing_blocks) {
1154 rotateby = pseudo_random % c->nr_erasing_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 rotate_list((&c->erase_pending_list), rotateby);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
1157
1158 if (c->nr_free_blocks) {
1159 rotateby = pseudo_random % c->nr_free_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 rotate_list((&c->free_list), rotateby);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
1162}