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