2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright © 2001-2007 Red Hat, Inc.
6 * Created by David Woodhouse <dwmw2@infradead.org>
8 * For licensing information, see the file 'LICENCE' in this directory.
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>
23 #define DEFAULT_EMPTY_SCAN_SIZE 256
25 #define noisy_printk(noise, args...) do { \
27 printk(KERN_NOTICE args); \
30 printk(KERN_NOTICE "Further such events for this erase block will not be printed\n"); \
35 static uint32_t pseudo_random;
37 static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
38 unsigned char *buf, uint32_t buf_size, struct jffs2_summary *s);
40 /* These helper functions _must_ increase ofs and also do the dirty/used space accounting.
41 * Returning an error will abort the mount - bad checksums etc. should just mark the space
44 static int jffs2_scan_inode_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
45 struct jffs2_raw_inode *ri, uint32_t ofs, struct jffs2_summary *s);
46 static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
47 struct jffs2_raw_dirent *rd, uint32_t ofs, struct jffs2_summary *s);
49 static inline int min_free(struct jffs2_sb_info *c)
51 uint32_t min = 2 * sizeof(struct jffs2_raw_inode);
52 #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
53 if (!jffs2_can_mark_obsolete(c) && min < c->wbuf_pagesize)
54 return c->wbuf_pagesize;
60 static inline uint32_t EMPTY_SCAN_SIZE(uint32_t sector_size) {
61 if (sector_size < DEFAULT_EMPTY_SCAN_SIZE)
64 return DEFAULT_EMPTY_SCAN_SIZE;
67 static int file_dirty(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
71 if ((ret = jffs2_prealloc_raw_node_refs(c, jeb, 1)))
73 if ((ret = jffs2_scan_dirty_space(c, jeb, jeb->free_size)))
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;
81 if (VERYDIRTY(c, jeb->dirty_size)) {
82 list_add(&jeb->list, &c->very_dirty_list);
84 list_add(&jeb->list, &c->dirty_list);
89 int jffs2_scan_medium(struct jffs2_sb_info *c)
92 uint32_t empty_blocks = 0, bad_blocks = 0;
93 unsigned char *flashbuf = NULL;
94 uint32_t buf_size = 0;
95 struct jffs2_summary *s = NULL; /* summary info collected by the scan process */
97 size_t pointlen, try_size;
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);
107 if (ret && ret != -EOPNOTSUPP)
108 D1(printk(KERN_DEBUG "MTD point failed %d\n", ret));
111 /* For NAND it's quicker to read a whole eraseblock at a time,
113 if (jffs2_cleanmarker_oob(c))
114 try_size = c->sector_size;
116 try_size = PAGE_SIZE;
118 D1(printk(KERN_DEBUG "Trying to allocate readbuf of %zu "
119 "bytes\n", try_size));
121 flashbuf = mtd_kmalloc_up_to(c->mtd, &try_size);
125 D1(printk(KERN_DEBUG "Allocated readbuf of %zu bytes\n",
128 buf_size = (uint32_t)try_size;
131 if (jffs2_sum_active()) {
132 s = kzalloc(sizeof(struct jffs2_summary), GFP_KERNEL);
134 JFFS2_WARNING("Can't allocate memory for summary\n");
140 for (i=0; i<c->nr_blocks; i++) {
141 struct jffs2_eraseblock *jeb = &c->blocks[i];
145 /* reset summary info for next eraseblock scan */
146 jffs2_sum_reset_collected(s);
148 ret = jffs2_scan_eraseblock(c, jeb, buf_size?flashbuf:(flashbuf+jeb->offset),
154 jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
156 /* Now decide which list to put it on */
158 case BLK_STATE_ALLFF:
160 * Empty block. Since we can't be sure it
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
167 list_add(&jeb->list, &c->erase_pending_list);
168 c->nr_erasing_blocks++;
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);
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++;
185 case BLK_STATE_CLEAN:
186 /* Full (or almost full) of clean data. Clean list */
187 list_add(&jeb->list, &c->clean_list);
190 case BLK_STATE_PARTDIRTY:
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 */
198 ret = file_dirty(c, c->nextblock);
201 /* deleting summary information of the old nextblock */
202 jffs2_sum_reset_collected(c->summary);
204 /* update collected summary information for the current nextblock */
205 jffs2_sum_move_collected(c, s);
206 D1(printk(KERN_DEBUG "jffs2_scan_medium(): new nextblock = 0x%08x\n", jeb->offset));
209 ret = file_dirty(c, jeb);
215 case BLK_STATE_ALLDIRTY:
216 /* Nothing valid - not even a clean marker. Needs erasing. */
217 /* For now we just put it on the erasing list. We'll start the erases later */
218 D1(printk(KERN_NOTICE "JFFS2: Erase block at 0x%08x is not formatted. It will be erased\n", jeb->offset));
219 list_add(&jeb->list, &c->erase_pending_list);
220 c->nr_erasing_blocks++;
223 case BLK_STATE_BADBLOCK:
224 D1(printk(KERN_NOTICE "JFFS2: Block at 0x%08x is bad\n", jeb->offset));
225 list_add(&jeb->list, &c->bad_list);
226 c->bad_size += c->sector_size;
227 c->free_size -= c->sector_size;
231 printk(KERN_WARNING "jffs2_scan_medium(): unknown block state\n");
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;
243 #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
244 if (!jffs2_can_mark_obsolete(c) && c->wbuf_pagesize && c->nextblock && (c->nextblock->free_size % c->wbuf_pagesize)) {
245 /* If we're going to start writing into a block which already
246 contains data, and the end of the data isn't page-aligned,
247 skip a little and align it. */
249 uint32_t skip = c->nextblock->free_size % c->wbuf_pagesize;
251 D1(printk(KERN_DEBUG "jffs2_scan_medium(): Skipping %d bytes in nextblock to ensure page alignment\n",
253 jffs2_prealloc_raw_node_refs(c, c->nextblock, 1);
254 jffs2_scan_dirty_space(c, c->nextblock, skip);
257 if (c->nr_erasing_blocks) {
258 if ( !c->used_size && ((c->nr_free_blocks+empty_blocks+bad_blocks)!= c->nr_blocks || bad_blocks == c->nr_blocks) ) {
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);
264 spin_lock(&c->erase_completion_lock);
265 jffs2_garbage_collect_trigger(c);
266 spin_unlock(&c->erase_completion_lock);
274 mtd_unpoint(c->mtd, 0, c->mtd->size);
280 static int jffs2_fill_scan_buf(struct jffs2_sb_info *c, void *buf,
281 uint32_t ofs, uint32_t len)
286 ret = jffs2_flash_read(c, ofs, len, &retlen, buf);
288 D1(printk(KERN_WARNING "mtd->read(0x%x bytes from 0x%x) returned %d\n", len, ofs, ret));
292 D1(printk(KERN_WARNING "Read at 0x%x gave only 0x%zx bytes\n", ofs, retlen));
298 int jffs2_scan_classify_jeb(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
300 if ((jeb->used_size + jeb->unchecked_size) == PAD(c->cleanmarker_size) && !jeb->dirty_size
301 && (!jeb->first_node || !ref_next(jeb->first_node)) )
302 return BLK_STATE_CLEANMARKER;
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;
310 return BLK_STATE_CLEAN;
311 } else if (jeb->used_size || jeb->unchecked_size)
312 return BLK_STATE_PARTDIRTY;
314 return BLK_STATE_ALLDIRTY;
317 #ifdef CONFIG_JFFS2_FS_XATTR
318 static 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)
322 struct jffs2_xattr_datum *xd;
323 uint32_t xid, version, totlen, crc;
326 crc = crc32(0, rx, sizeof(struct jffs2_raw_xattr) - 4);
327 if (crc != je32_to_cpu(rx->node_crc)) {
328 JFFS2_WARNING("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
329 ofs, je32_to_cpu(rx->node_crc), crc);
330 if ((err = jffs2_scan_dirty_space(c, jeb, je32_to_cpu(rx->totlen))))
335 xid = je32_to_cpu(rx->xid);
336 version = je32_to_cpu(rx->version);
338 totlen = PAD(sizeof(struct jffs2_raw_xattr)
339 + rx->name_len + 1 + je16_to_cpu(rx->value_len));
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);
343 if ((err = jffs2_scan_dirty_space(c, jeb, je32_to_cpu(rx->totlen))))
348 xd = jffs2_setup_xattr_datum(c, xid, version);
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;
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);
364 jffs2_link_node_ref(c, jeb, ofs | REF_PRISTINE, totlen, (void *)xd);
367 if (jffs2_sum_active())
368 jffs2_sum_add_xattr_mem(s, rx, ofs - jeb->offset);
369 dbg_xattr("scanning xdatum at %#08x (xid=%u, version=%u)\n",
370 ofs, xd->xid, xd->version);
374 static 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)
378 struct jffs2_xattr_ref *ref;
382 crc = crc32(0, rr, sizeof(*rr) - 4);
383 if (crc != je32_to_cpu(rr->node_crc)) {
384 JFFS2_WARNING("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
385 ofs, je32_to_cpu(rr->node_crc), crc);
386 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(rr->totlen)))))
391 if (PAD(sizeof(struct jffs2_raw_xref)) != je32_to_cpu(rr->totlen)) {
392 JFFS2_WARNING("node length mismatch at %#08x, read=%u, calc=%zd\n",
393 ofs, je32_to_cpu(rr->totlen),
394 PAD(sizeof(struct jffs2_raw_xref)));
395 if ((err = jffs2_scan_dirty_space(c, jeb, je32_to_cpu(rr->totlen))))
400 ref = jffs2_alloc_xattr_ref();
404 /* BEFORE jffs2_build_xattr_subsystem() called,
405 * and AFTER xattr_ref is marked as a dead xref,
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
409 * are exclusive. In a similar way, ref->next is temporarily
410 * used to chain all xattr_ref object. It's re-chained to
411 * jffs2_inode_cache in jffs2_build_xattr_subsystem() correctly.
413 ref->ino = je32_to_cpu(rr->ino);
414 ref->xid = je32_to_cpu(rr->xid);
415 ref->xseqno = je32_to_cpu(rr->xseqno);
416 if (ref->xseqno > c->highest_xseqno)
417 c->highest_xseqno = (ref->xseqno & ~XREF_DELETE_MARKER);
418 ref->next = c->xref_temp;
421 jffs2_link_node_ref(c, jeb, ofs | REF_PRISTINE, PAD(je32_to_cpu(rr->totlen)), (void *)ref);
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);
431 /* Called with 'buf_size == 0' if buf is in fact a pointer _directly_ into
432 the flash, XIP-style */
433 static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
434 unsigned char *buf, uint32_t buf_size, struct jffs2_summary *s) {
435 struct jffs2_unknown_node *node;
436 struct jffs2_unknown_node crcnode;
437 uint32_t ofs, prevofs, max_ofs;
438 uint32_t hdr_crc, buf_ofs, buf_len;
443 #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
444 int cleanmarkerfound = 0;
448 prevofs = jeb->offset - 1;
450 D1(printk(KERN_DEBUG "jffs2_scan_eraseblock(): Scanning block at 0x%x\n", ofs));
452 #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
453 if (jffs2_cleanmarker_oob(c)) {
456 if (mtd_block_isbad(c->mtd, jeb->offset))
457 return BLK_STATE_BADBLOCK;
459 ret = jffs2_check_nand_cleanmarker(c, jeb);
460 D2(printk(KERN_NOTICE "jffs_check_nand_cleanmarker returned %d\n",ret));
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. */
466 case 0: cleanmarkerfound = 1; break;
473 if (jffs2_sum_active()) {
474 struct jffs2_sum_marker *sm;
479 /* XIP case. Just look, point at the summary if it's there */
480 sm = (void *)buf + c->sector_size - sizeof(*sm);
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);
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;
490 buf_len = sizeof(*sm);
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,
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;
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);
510 memcpy(sumptr + sumlen - buf_len, buf + buf_size - buf_len, buf_len);
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,
525 err = jffs2_sum_scan_sumnode(c, jeb, sumptr, sumlen, &pseudo_random);
527 if (buf_size && sumlen > buf_size)
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. */
538 buf_ofs = jeb->offset;
541 /* This is the XIP case -- we're reading _directly_ from the flash chip */
542 buf_len = c->sector_size;
544 buf_len = EMPTY_SCAN_SIZE(c->sector_size);
545 err = jffs2_fill_scan_buf(c, buf, buf_ofs, buf_len);
550 /* We temporarily use 'ofs' as a pointer into the buffer/jeb */
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)
557 if (ofs == max_ofs) {
558 #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
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));
564 case 0: return cleanmarkerfound ? BLK_STATE_CLEANMARKER : BLK_STATE_ALLFF;
565 case 1: return BLK_STATE_ALLDIRTY;
570 D1(printk(KERN_DEBUG "Block at 0x%08x is empty (erased)\n", jeb->offset));
571 if (c->cleanmarker_size == 0)
572 return BLK_STATE_CLEANMARKER; /* don't bother with re-erase */
574 return BLK_STATE_ALLFF; /* OK to erase if all blocks are like this */
577 D1(printk(KERN_DEBUG "Free space at %08x ends at %08x\n", jeb->offset,
579 if ((err = jffs2_prealloc_raw_node_refs(c, jeb, 1)))
581 if ((err = jffs2_scan_dirty_space(c, jeb, ofs)))
585 /* Now ofs is a complete physical flash offset as it always was... */
590 dbg_summary("no summary found in jeb 0x%08x. Apply original scan.\n",jeb->offset);
593 while(ofs < jeb->offset + c->sector_size) {
595 jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
597 /* Make sure there are node refs available for use */
598 err = jffs2_prealloc_raw_node_refs(c, jeb, 2);
605 printk(KERN_WARNING "Eep. ofs 0x%08x not word-aligned!\n", ofs);
609 if (ofs == prevofs) {
610 printk(KERN_WARNING "ofs 0x%08x has already been seen. Skipping\n", ofs);
611 if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
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)));
621 if ((err = jffs2_scan_dirty_space(c, jeb, (jeb->offset + c->sector_size)-ofs)))
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);
636 node = (struct jffs2_unknown_node *)&buf[ofs-buf_ofs];
638 if (*(uint32_t *)(&buf[ofs-buf_ofs]) == 0xffffffff) {
640 uint32_t empty_start, scan_end;
644 scan_end = min_t(uint32_t, EMPTY_SCAN_SIZE(c->sector_size)/8, buf_len);
646 D1(printk(KERN_DEBUG "Found empty flash at 0x%08x\n", ofs));
648 inbuf_ofs = ofs - buf_ofs;
649 while (inbuf_ofs < scan_end) {
650 if (unlikely(*(uint32_t *)(&buf[inbuf_ofs]) != 0xffffffff)) {
651 printk(KERN_WARNING "Empty flash at 0x%08x ends at 0x%08x\n",
653 if ((err = jffs2_scan_dirty_space(c, jeb, ofs-empty_start)))
662 D1(printk(KERN_DEBUG "Empty flash to end of buffer at 0x%08x\n", ofs));
664 /* If we're only checking the beginning of a block with a cleanmarker,
666 if (buf_ofs == jeb->offset && jeb->used_size == PAD(c->cleanmarker_size) &&
667 c->cleanmarker_size && !jeb->dirty_size && !ref_next(jeb->first_node)) {
668 D1(printk(KERN_DEBUG "%d bytes at start of block seems clean... assuming all clean\n", EMPTY_SCAN_SIZE(c->sector_size)));
669 return BLK_STATE_CLEANMARKER;
671 if (!buf_size && (scan_end != buf_len)) {/* XIP/point case */
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);
679 /* No more to read. Break out of main loop without marking
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",
685 /* point never reaches here */
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);
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);
697 if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
702 if (je16_to_cpu(node->magic) == JFFS2_DIRTY_BITMASK) {
703 D1(printk(KERN_DEBUG "Dirty bitmask at 0x%08x\n", ofs));
704 if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
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");
712 if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
717 if (je16_to_cpu(node->magic) != JFFS2_MAGIC_BITMASK) {
718 /* OK. We're out of possibilities. Whinge and move on */
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,
721 je16_to_cpu(node->magic));
722 if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
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);
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),
736 je16_to_cpu(node->nodetype),
737 je32_to_cpu(node->totlen),
738 je32_to_cpu(node->hdr_crc),
740 if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
746 if (ofs + je32_to_cpu(node->totlen) > jeb->offset + c->sector_size) {
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");
751 if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
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));
760 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen)))))
762 ofs += PAD(je32_to_cpu(node->totlen));
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);
778 err = jffs2_scan_inode_node(c, jeb, (void *)node, ofs, s);
780 ofs += PAD(je32_to_cpu(node->totlen));
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);
794 err = jffs2_scan_dirent_node(c, jeb, (void *)node, ofs, s);
796 ofs += PAD(je32_to_cpu(node->totlen));
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);
812 err = jffs2_scan_xattr_node(c, jeb, (void *)node, ofs, s);
815 ofs += PAD(je32_to_cpu(node->totlen));
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);
829 err = jffs2_scan_xref_node(c, jeb, (void *)node, ofs, s);
832 ofs += PAD(je32_to_cpu(node->totlen));
834 #endif /* CONFIG_JFFS2_FS_XATTR */
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) {
839 printk(KERN_NOTICE "CLEANMARKER node found at 0x%08x has totlen 0x%x != normal 0x%x\n",
840 ofs, je32_to_cpu(node->totlen), c->cleanmarker_size);
841 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(sizeof(struct jffs2_unknown_node)))))
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);
846 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(sizeof(struct jffs2_unknown_node)))))
848 ofs += PAD(sizeof(struct jffs2_unknown_node));
850 jffs2_link_node_ref(c, jeb, ofs | REF_NORMAL, c->cleanmarker_size, NULL);
852 ofs += PAD(c->cleanmarker_size);
856 case JFFS2_NODETYPE_PADDING:
857 if (jffs2_sum_active())
858 jffs2_sum_add_padding_mem(s, je32_to_cpu(node->totlen));
859 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen)))))
861 ofs += PAD(je32_to_cpu(node->totlen));
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);
868 c->flags |= JFFS2_SB_FLAG_RO;
869 if (!(jffs2_is_readonly(c)))
871 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen)))))
873 ofs += PAD(je32_to_cpu(node->totlen));
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);
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));
882 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen)))))
884 ofs += PAD(je32_to_cpu(node->totlen));
887 case JFFS2_FEATURE_RWCOMPAT_COPY: {
888 D1(printk(KERN_NOTICE "Unknown but compatible feature node (0x%04x) found at offset 0x%08x\n", je16_to_cpu(node->nodetype), ofs));
890 jffs2_link_node_ref(c, jeb, ofs | REF_PRISTINE, PAD(je32_to_cpu(node->totlen)), NULL);
892 /* We can't summarise nodes we don't grok */
893 jffs2_sum_disable_collecting(s);
894 ofs += PAD(je32_to_cpu(node->totlen));
901 if (jffs2_sum_active()) {
902 if (PAD(s->sum_size + JFFS2_SUMMARY_FRAME_SIZE) > jeb->free_size) {
903 dbg_summary("There is not enough space for "
904 "summary information, disabling for this jeb!\n");
905 jffs2_sum_disable_collecting(s);
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));
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;
920 return jffs2_scan_classify_jeb(c, jeb);
923 struct jffs2_inode_cache *jffs2_scan_make_ino_cache(struct jffs2_sb_info *c, uint32_t ino)
925 struct jffs2_inode_cache *ic;
927 ic = jffs2_get_ino_cache(c, ino);
931 if (ino > c->highest_ino)
932 c->highest_ino = ino;
934 ic = jffs2_alloc_inode_cache();
936 printk(KERN_NOTICE "jffs2_scan_make_inode_cache(): allocation of inode cache failed\n");
939 memset(ic, 0, sizeof(*ic));
942 ic->nodes = (void *)ic;
943 jffs2_add_ino_cache(c, ic);
949 static int jffs2_scan_inode_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
950 struct jffs2_raw_inode *ri, uint32_t ofs, struct jffs2_summary *s)
952 struct jffs2_inode_cache *ic;
953 uint32_t crc, ino = je32_to_cpu(ri->ino);
955 D1(printk(KERN_DEBUG "jffs2_scan_inode_node(): Node at 0x%08x\n", ofs));
957 /* We do very little here now. Just check the ino# to which we should attribute
958 this node; we can do all the CRC checking etc. later. There's a tradeoff here --
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,
962 but we have to go _back_ to the flash in order to finish the CRC checking, etc.
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. */
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);
973 * We believe totlen because the CRC on the node
974 * _header_ was OK, just the node itself failed.
976 return jffs2_scan_dirty_space(c, jeb,
977 PAD(je32_to_cpu(ri->totlen)));
980 ic = jffs2_get_ino_cache(c, ino);
982 ic = jffs2_scan_make_ino_cache(c, ino);
987 /* Wheee. It worked */
988 jffs2_link_node_ref(c, jeb, ofs | REF_UNCHECKED, PAD(je32_to_cpu(ri->totlen)), ic);
990 D1(printk(KERN_DEBUG "Node is ino #%u, version %d. Range 0x%x-0x%x\n",
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)));
995 pseudo_random += je32_to_cpu(ri->version);
997 if (jffs2_sum_active()) {
998 jffs2_sum_add_inode_mem(s, ri, ofs - jeb->offset);
1004 static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
1005 struct jffs2_raw_dirent *rd, uint32_t ofs, struct jffs2_summary *s)
1007 struct jffs2_full_dirent *fd;
1008 struct jffs2_inode_cache *ic;
1009 uint32_t checkedlen;
1013 D1(printk(KERN_DEBUG "jffs2_scan_dirent_node(): Node at 0x%08x\n", ofs));
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);
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. */
1023 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(rd->totlen)))))
1028 pseudo_random += je32_to_cpu(rd->version);
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",
1036 fd = jffs2_alloc_full_dirent(checkedlen+1);
1040 memcpy(&fd->name, rd->name, checkedlen);
1041 fd->name[checkedlen] = 0;
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",
1046 ofs, je32_to_cpu(rd->name_crc), crc);
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. */
1051 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(rd->totlen)))))
1055 ic = jffs2_scan_make_ino_cache(c, je32_to_cpu(rd->pino));
1057 jffs2_free_full_dirent(fd);
1061 fd->raw = jffs2_link_node_ref(c, jeb, ofs | dirent_node_state(rd),
1062 PAD(je32_to_cpu(rd->totlen)), ic);
1065 fd->version = je32_to_cpu(rd->version);
1066 fd->ino = je32_to_cpu(rd->ino);
1067 fd->nhash = full_name_hash(fd->name, checkedlen);
1068 fd->type = rd->type;
1069 jffs2_add_fd_to_list(c, fd, &ic->scan_dents);
1071 if (jffs2_sum_active()) {
1072 jffs2_sum_add_dirent_mem(s, rd, ofs - jeb->offset);
1078 static int count_list(struct list_head *l)
1081 struct list_head *tmp;
1083 list_for_each(tmp, l) {
1089 /* Note: This breaks if list_empty(head). I don't care. You
1090 might, if you copy this code and use it elsewhere :) */
1091 static void rotate_list(struct list_head *head, uint32_t count)
1093 struct list_head *n = head->next;
1102 void jffs2_rotate_lists(struct jffs2_sb_info *c)
1107 x = count_list(&c->clean_list);
1109 rotateby = pseudo_random % x;
1110 rotate_list((&c->clean_list), rotateby);
1113 x = count_list(&c->very_dirty_list);
1115 rotateby = pseudo_random % x;
1116 rotate_list((&c->very_dirty_list), rotateby);
1119 x = count_list(&c->dirty_list);
1121 rotateby = pseudo_random % x;
1122 rotate_list((&c->dirty_list), rotateby);
1125 x = count_list(&c->erasable_list);
1127 rotateby = pseudo_random % x;
1128 rotate_list((&c->erasable_list), rotateby);
1131 if (c->nr_erasing_blocks) {
1132 rotateby = pseudo_random % c->nr_erasing_blocks;
1133 rotate_list((&c->erase_pending_list), rotateby);
1136 if (c->nr_free_blocks) {
1137 rotateby = pseudo_random % c->nr_free_blocks;
1138 rotate_list((&c->free_list), rotateby);