Merge branch 'master'
[sfrench/cifs-2.6.git] / fs / jffs2 / scan.c
1 /*
2  * JFFS2 -- Journalling Flash File System, Version 2.
3  *
4  * Copyright (C) 2001-2003 Red Hat, Inc.
5  *
6  * Created by David Woodhouse <dwmw2@infradead.org>
7  *
8  * For licensing information, see the file 'LICENCE' in this directory.
9  *
10  * $Id: scan.c,v 1.125 2005/09/30 13:59:13 dedekind Exp $
11  *
12  */
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/slab.h>
16 #include <linux/mtd/mtd.h>
17 #include <linux/pagemap.h>
18 #include <linux/crc32.h>
19 #include <linux/compiler.h>
20 #include "nodelist.h"
21 #include "summary.h"
22 #include "debug.h"
23
24 #define DEFAULT_EMPTY_SCAN_SIZE 1024
25
26 #define noisy_printk(noise, args...) do { \
27         if (*(noise)) { \
28                 printk(KERN_NOTICE args); \
29                  (*(noise))--; \
30                  if (!(*(noise))) { \
31                          printk(KERN_NOTICE "Further such events for this erase block will not be printed\n"); \
32                  } \
33         } \
34 } while(0)
35
36 static uint32_t pseudo_random;
37
38 static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
39                                   unsigned char *buf, uint32_t buf_size, struct jffs2_summary *s);
40
41 /* These helper functions _must_ increase ofs and also do the dirty/used space accounting.
42  * Returning an error will abort the mount - bad checksums etc. should just mark the space
43  * as dirty.
44  */
45 static int jffs2_scan_inode_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
46                                  struct jffs2_raw_inode *ri, uint32_t ofs, struct jffs2_summary *s);
47 static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
48                                  struct jffs2_raw_dirent *rd, uint32_t ofs, struct jffs2_summary *s);
49
50 static inline int min_free(struct jffs2_sb_info *c)
51 {
52         uint32_t min = 2 * sizeof(struct jffs2_raw_inode);
53 #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
54         if (!jffs2_can_mark_obsolete(c) && min < c->wbuf_pagesize)
55                 return c->wbuf_pagesize;
56 #endif
57         return min;
58
59 }
60
61 static inline uint32_t EMPTY_SCAN_SIZE(uint32_t sector_size) {
62         if (sector_size < DEFAULT_EMPTY_SCAN_SIZE)
63                 return sector_size;
64         else
65                 return DEFAULT_EMPTY_SCAN_SIZE;
66 }
67
68 int jffs2_scan_medium(struct jffs2_sb_info *c)
69 {
70         int i, ret;
71         uint32_t empty_blocks = 0, bad_blocks = 0;
72         unsigned char *flashbuf = NULL;
73         uint32_t buf_size = 0;
74         struct jffs2_summary *s = NULL; /* summary info collected by the scan process */
75 #ifndef __ECOS
76         size_t pointlen;
77
78         if (c->mtd->point) {
79                 ret = c->mtd->point (c->mtd, 0, c->mtd->size, &pointlen, &flashbuf);
80                 if (!ret && pointlen < c->mtd->size) {
81                         /* Don't muck about if it won't let us point to the whole flash */
82                         D1(printk(KERN_DEBUG "MTD point returned len too short: 0x%zx\n", pointlen));
83                         c->mtd->unpoint(c->mtd, flashbuf, 0, c->mtd->size);
84                         flashbuf = NULL;
85                 }
86                 if (ret)
87                         D1(printk(KERN_DEBUG "MTD point failed %d\n", ret));
88         }
89 #endif
90         if (!flashbuf) {
91                 /* For NAND it's quicker to read a whole eraseblock at a time,
92                    apparently */
93                 if (jffs2_cleanmarker_oob(c))
94                         buf_size = c->sector_size;
95                 else
96                         buf_size = PAGE_SIZE;
97
98                 /* Respect kmalloc limitations */
99                 if (buf_size > 128*1024)
100                         buf_size = 128*1024;
101
102                 D1(printk(KERN_DEBUG "Allocating readbuf of %d bytes\n", buf_size));
103                 flashbuf = kmalloc(buf_size, GFP_KERNEL);
104                 if (!flashbuf)
105                         return -ENOMEM;
106         }
107
108         if (jffs2_sum_active()) {
109                 s = kmalloc(sizeof(struct jffs2_summary), GFP_KERNEL);
110                 if (!s) {
111                         JFFS2_WARNING("Can't allocate memory for summary\n");
112                         return -ENOMEM;
113                 }
114                 memset(s, 0, sizeof(struct jffs2_summary));
115         }
116
117         for (i=0; i<c->nr_blocks; i++) {
118                 struct jffs2_eraseblock *jeb = &c->blocks[i];
119
120                 /* reset summary info for next eraseblock scan */
121                 jffs2_sum_reset_collected(s);
122
123                 ret = jffs2_scan_eraseblock(c, jeb, buf_size?flashbuf:(flashbuf+jeb->offset),
124                                                 buf_size, s);
125
126                 if (ret < 0)
127                         goto out;
128
129                 jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
130
131                 /* Now decide which list to put it on */
132                 switch(ret) {
133                 case BLK_STATE_ALLFF:
134                         /*
135                          * Empty block.   Since we can't be sure it
136                          * was entirely erased, we just queue it for erase
137                          * again.  It will be marked as such when the erase
138                          * is complete.  Meanwhile we still count it as empty
139                          * for later checks.
140                          */
141                         empty_blocks++;
142                         list_add(&jeb->list, &c->erase_pending_list);
143                         c->nr_erasing_blocks++;
144                         break;
145
146                 case BLK_STATE_CLEANMARKER:
147                         /* Only a CLEANMARKER node is valid */
148                         if (!jeb->dirty_size) {
149                                 /* It's actually free */
150                                 list_add(&jeb->list, &c->free_list);
151                                 c->nr_free_blocks++;
152                         } else {
153                                 /* Dirt */
154                                 D1(printk(KERN_DEBUG "Adding all-dirty block at 0x%08x to erase_pending_list\n", jeb->offset));
155                                 list_add(&jeb->list, &c->erase_pending_list);
156                                 c->nr_erasing_blocks++;
157                         }
158                         break;
159
160                 case BLK_STATE_CLEAN:
161                         /* Full (or almost full) of clean data. Clean list */
162                         list_add(&jeb->list, &c->clean_list);
163                         break;
164
165                 case BLK_STATE_PARTDIRTY:
166                         /* Some data, but not full. Dirty list. */
167                         /* We want to remember the block with most free space
168                         and stick it in the 'nextblock' position to start writing to it. */
169                         if (jeb->free_size > min_free(c) &&
170                                         (!c->nextblock || c->nextblock->free_size < jeb->free_size)) {
171                                 /* Better candidate for the next writes to go to */
172                                 if (c->nextblock) {
173                                         c->nextblock->dirty_size += c->nextblock->free_size + c->nextblock->wasted_size;
174                                         c->dirty_size += c->nextblock->free_size + c->nextblock->wasted_size;
175                                         c->free_size -= c->nextblock->free_size;
176                                         c->wasted_size -= c->nextblock->wasted_size;
177                                         c->nextblock->free_size = c->nextblock->wasted_size = 0;
178                                         if (VERYDIRTY(c, c->nextblock->dirty_size)) {
179                                                 list_add(&c->nextblock->list, &c->very_dirty_list);
180                                         } else {
181                                                 list_add(&c->nextblock->list, &c->dirty_list);
182                                         }
183                                         /* deleting summary information of the old nextblock */
184                                         jffs2_sum_reset_collected(c->summary);
185                                 }
186                                 /* update collected summary infromation for the current nextblock */
187                                 jffs2_sum_move_collected(c, s);
188                                 D1(printk(KERN_DEBUG "jffs2_scan_medium(): new nextblock = 0x%08x\n", jeb->offset));
189                                 c->nextblock = jeb;
190                         } else {
191                                 jeb->dirty_size += jeb->free_size + jeb->wasted_size;
192                                 c->dirty_size += jeb->free_size + jeb->wasted_size;
193                                 c->free_size -= jeb->free_size;
194                                 c->wasted_size -= jeb->wasted_size;
195                                 jeb->free_size = jeb->wasted_size = 0;
196                                 if (VERYDIRTY(c, jeb->dirty_size)) {
197                                         list_add(&jeb->list, &c->very_dirty_list);
198                                 } else {
199                                         list_add(&jeb->list, &c->dirty_list);
200                                 }
201                         }
202                         break;
203
204                 case BLK_STATE_ALLDIRTY:
205                         /* Nothing valid - not even a clean marker. Needs erasing. */
206                         /* For now we just put it on the erasing list. We'll start the erases later */
207                         D1(printk(KERN_NOTICE "JFFS2: Erase block at 0x%08x is not formatted. It will be erased\n", jeb->offset));
208                         list_add(&jeb->list, &c->erase_pending_list);
209                         c->nr_erasing_blocks++;
210                         break;
211
212                 case BLK_STATE_BADBLOCK:
213                         D1(printk(KERN_NOTICE "JFFS2: Block at 0x%08x is bad\n", jeb->offset));
214                         list_add(&jeb->list, &c->bad_list);
215                         c->bad_size += c->sector_size;
216                         c->free_size -= c->sector_size;
217                         bad_blocks++;
218                         break;
219                 default:
220                         printk(KERN_WARNING "jffs2_scan_medium(): unknown block state\n");
221                         BUG();
222                 }
223         }
224
225         if (jffs2_sum_active() && s)
226                 kfree(s);
227
228         /* Nextblock dirty is always seen as wasted, because we cannot recycle it now */
229         if (c->nextblock && (c->nextblock->dirty_size)) {
230                 c->nextblock->wasted_size += c->nextblock->dirty_size;
231                 c->wasted_size += c->nextblock->dirty_size;
232                 c->dirty_size -= c->nextblock->dirty_size;
233                 c->nextblock->dirty_size = 0;
234         }
235 #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
236         if (!jffs2_can_mark_obsolete(c) && c->nextblock && (c->nextblock->free_size % c->wbuf_pagesize)) {
237                 /* If we're going to start writing into a block which already
238                    contains data, and the end of the data isn't page-aligned,
239                    skip a little and align it. */
240
241                 uint32_t skip = c->nextblock->free_size % c->wbuf_pagesize;
242
243                 D1(printk(KERN_DEBUG "jffs2_scan_medium(): Skipping %d bytes in nextblock to ensure page alignment\n",
244                           skip));
245                 c->nextblock->wasted_size += skip;
246                 c->wasted_size += skip;
247
248                 c->nextblock->free_size -= skip;
249                 c->free_size -= skip;
250         }
251 #endif
252         if (c->nr_erasing_blocks) {
253                 if ( !c->used_size && ((c->nr_free_blocks+empty_blocks+bad_blocks)!= c->nr_blocks || bad_blocks == c->nr_blocks) ) {
254                         printk(KERN_NOTICE "Cowardly refusing to erase blocks on filesystem with no valid JFFS2 nodes\n");
255                         printk(KERN_NOTICE "empty_blocks %d, bad_blocks %d, c->nr_blocks %d\n",empty_blocks,bad_blocks,c->nr_blocks);
256                         ret = -EIO;
257                         goto out;
258                 }
259                 jffs2_erase_pending_trigger(c);
260         }
261         ret = 0;
262  out:
263         if (buf_size)
264                 kfree(flashbuf);
265 #ifndef __ECOS
266         else
267                 c->mtd->unpoint(c->mtd, flashbuf, 0, c->mtd->size);
268 #endif
269         return ret;
270 }
271
272 int jffs2_fill_scan_buf (struct jffs2_sb_info *c, void *buf,
273                                 uint32_t ofs, uint32_t len)
274 {
275         int ret;
276         size_t retlen;
277
278         ret = jffs2_flash_read(c, ofs, len, &retlen, buf);
279         if (ret) {
280                 D1(printk(KERN_WARNING "mtd->read(0x%x bytes from 0x%x) returned %d\n", len, ofs, ret));
281                 return ret;
282         }
283         if (retlen < len) {
284                 D1(printk(KERN_WARNING "Read at 0x%x gave only 0x%zx bytes\n", ofs, retlen));
285                 return -EIO;
286         }
287         D2(printk(KERN_DEBUG "Read 0x%x bytes from 0x%08x into buf\n", len, ofs));
288         D2(printk(KERN_DEBUG "000: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
289                   buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7], buf[8], buf[9], buf[10], buf[11], buf[12], buf[13], buf[14], buf[15]));
290         return 0;
291 }
292
293 int jffs2_scan_classify_jeb(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
294 {
295         if ((jeb->used_size + jeb->unchecked_size) == PAD(c->cleanmarker_size) && !jeb->dirty_size
296                 && (!jeb->first_node || !jeb->first_node->next_phys) )
297                 return BLK_STATE_CLEANMARKER;
298
299         /* move blocks with max 4 byte dirty space to cleanlist */
300         else if (!ISDIRTY(c->sector_size - (jeb->used_size + jeb->unchecked_size))) {
301                 c->dirty_size -= jeb->dirty_size;
302                 c->wasted_size += jeb->dirty_size;
303                 jeb->wasted_size += jeb->dirty_size;
304                 jeb->dirty_size = 0;
305                 return BLK_STATE_CLEAN;
306         } else if (jeb->used_size || jeb->unchecked_size)
307                 return BLK_STATE_PARTDIRTY;
308         else
309                 return BLK_STATE_ALLDIRTY;
310 }
311
312 static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
313                                 unsigned char *buf, uint32_t buf_size, struct jffs2_summary *s) {
314         struct jffs2_unknown_node *node;
315         struct jffs2_unknown_node crcnode;
316         struct jffs2_sum_marker *sm;
317         uint32_t ofs, prevofs;
318         uint32_t hdr_crc, buf_ofs, buf_len;
319         int err;
320         int noise = 0;
321
322
323 #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
324         int cleanmarkerfound = 0;
325 #endif
326
327         ofs = jeb->offset;
328         prevofs = jeb->offset - 1;
329
330         D1(printk(KERN_DEBUG "jffs2_scan_eraseblock(): Scanning block at 0x%x\n", ofs));
331
332 #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
333         if (jffs2_cleanmarker_oob(c)) {
334                 int ret = jffs2_check_nand_cleanmarker(c, jeb);
335                 D2(printk(KERN_NOTICE "jffs_check_nand_cleanmarker returned %d\n",ret));
336                 /* Even if it's not found, we still scan to see
337                    if the block is empty. We use this information
338                    to decide whether to erase it or not. */
339                 switch (ret) {
340                 case 0:         cleanmarkerfound = 1; break;
341                 case 1:         break;
342                 case 2:         return BLK_STATE_BADBLOCK;
343                 case 3:         return BLK_STATE_ALLDIRTY; /* Block has failed to erase min. once */
344                 default:        return ret;
345                 }
346         }
347 #endif
348
349         if (jffs2_sum_active()) {
350                 sm = kmalloc(sizeof(struct jffs2_sum_marker), GFP_KERNEL);
351                 if (!sm) {
352                         return -ENOMEM;
353                 }
354
355                 err = jffs2_fill_scan_buf(c, (unsigned char *) sm, jeb->offset + c->sector_size -
356                                         sizeof(struct jffs2_sum_marker), sizeof(struct jffs2_sum_marker));
357                 if (err) {
358                         kfree(sm);
359                         return err;
360                 }
361
362                 if (je32_to_cpu(sm->magic) == JFFS2_SUM_MAGIC ) {
363                         err = jffs2_sum_scan_sumnode(c, jeb, je32_to_cpu(sm->offset), &pseudo_random);
364                         if (err) {
365                                 kfree(sm);
366                                 return err;
367                         }
368                 }
369
370                 kfree(sm);
371
372                 ofs = jeb->offset;
373                 prevofs = jeb->offset - 1;
374         }
375
376         buf_ofs = jeb->offset;
377
378         if (!buf_size) {
379                 buf_len = c->sector_size;
380
381                 if (jffs2_sum_active()) {
382                         /* must reread because of summary test */
383                         err = jffs2_fill_scan_buf(c, buf, buf_ofs, buf_len);
384                         if (err)
385                                 return err;
386                 }
387
388         } else {
389                 buf_len = EMPTY_SCAN_SIZE(c->sector_size);
390                 err = jffs2_fill_scan_buf(c, buf, buf_ofs, buf_len);
391                 if (err)
392                         return err;
393         }
394
395         /* We temporarily use 'ofs' as a pointer into the buffer/jeb */
396         ofs = 0;
397
398         /* Scan only 4KiB of 0xFF before declaring it's empty */
399         while(ofs < EMPTY_SCAN_SIZE(c->sector_size) && *(uint32_t *)(&buf[ofs]) == 0xFFFFFFFF)
400                 ofs += 4;
401
402         if (ofs == EMPTY_SCAN_SIZE(c->sector_size)) {
403 #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
404                 if (jffs2_cleanmarker_oob(c)) {
405                         /* scan oob, take care of cleanmarker */
406                         int ret = jffs2_check_oob_empty(c, jeb, cleanmarkerfound);
407                         D2(printk(KERN_NOTICE "jffs2_check_oob_empty returned %d\n",ret));
408                         switch (ret) {
409                         case 0:         return cleanmarkerfound ? BLK_STATE_CLEANMARKER : BLK_STATE_ALLFF;
410                         case 1:         return BLK_STATE_ALLDIRTY;
411                         default:        return ret;
412                         }
413                 }
414 #endif
415                 D1(printk(KERN_DEBUG "Block at 0x%08x is empty (erased)\n", jeb->offset));
416                 if (c->cleanmarker_size == 0)
417                         return BLK_STATE_CLEANMARKER;   /* don't bother with re-erase */
418                 else
419                         return BLK_STATE_ALLFF; /* OK to erase if all blocks are like this */
420         }
421         if (ofs) {
422                 D1(printk(KERN_DEBUG "Free space at %08x ends at %08x\n", jeb->offset,
423                           jeb->offset + ofs));
424                 DIRTY_SPACE(ofs);
425         }
426
427         /* Now ofs is a complete physical flash offset as it always was... */
428         ofs += jeb->offset;
429
430         noise = 10;
431
432         dbg_summary("no summary found in jeb 0x%08x. Apply original scan.\n",jeb->offset);
433
434 scan_more:
435         while(ofs < jeb->offset + c->sector_size) {
436
437                 jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
438
439                 cond_resched();
440
441                 if (ofs & 3) {
442                         printk(KERN_WARNING "Eep. ofs 0x%08x not word-aligned!\n", ofs);
443                         ofs = PAD(ofs);
444                         continue;
445                 }
446                 if (ofs == prevofs) {
447                         printk(KERN_WARNING "ofs 0x%08x has already been seen. Skipping\n", ofs);
448                         DIRTY_SPACE(4);
449                         ofs += 4;
450                         continue;
451                 }
452                 prevofs = ofs;
453
454                 if (jeb->offset + c->sector_size < ofs + sizeof(*node)) {
455                         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),
456                                   jeb->offset, c->sector_size, ofs, sizeof(*node)));
457                         DIRTY_SPACE((jeb->offset + c->sector_size)-ofs);
458                         break;
459                 }
460
461                 if (buf_ofs + buf_len < ofs + sizeof(*node)) {
462                         buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
463                         D1(printk(KERN_DEBUG "Fewer than %zd bytes (node header) left to end of buf. Reading 0x%x at 0x%08x\n",
464                                   sizeof(struct jffs2_unknown_node), buf_len, ofs));
465                         err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
466                         if (err)
467                                 return err;
468                         buf_ofs = ofs;
469                 }
470
471                 node = (struct jffs2_unknown_node *)&buf[ofs-buf_ofs];
472
473                 if (*(uint32_t *)(&buf[ofs-buf_ofs]) == 0xffffffff) {
474                         uint32_t inbuf_ofs;
475                         uint32_t empty_start;
476
477                         empty_start = ofs;
478                         ofs += 4;
479
480                         D1(printk(KERN_DEBUG "Found empty flash at 0x%08x\n", ofs));
481                 more_empty:
482                         inbuf_ofs = ofs - buf_ofs;
483                         while (inbuf_ofs < buf_len) {
484                                 if (*(uint32_t *)(&buf[inbuf_ofs]) != 0xffffffff) {
485                                         printk(KERN_WARNING "Empty flash at 0x%08x ends at 0x%08x\n",
486                                                empty_start, ofs);
487                                         DIRTY_SPACE(ofs-empty_start);
488                                         goto scan_more;
489                                 }
490
491                                 inbuf_ofs+=4;
492                                 ofs += 4;
493                         }
494                         /* Ran off end. */
495                         D1(printk(KERN_DEBUG "Empty flash to end of buffer at 0x%08x\n", ofs));
496
497                         /* If we're only checking the beginning of a block with a cleanmarker,
498                            bail now */
499                         if (buf_ofs == jeb->offset && jeb->used_size == PAD(c->cleanmarker_size) &&
500                             c->cleanmarker_size && !jeb->dirty_size && !jeb->first_node->next_phys) {
501                                 D1(printk(KERN_DEBUG "%d bytes at start of block seems clean... assuming all clean\n", EMPTY_SCAN_SIZE(c->sector_size)));
502                                 return BLK_STATE_CLEANMARKER;
503                         }
504
505                         /* See how much more there is to read in this eraseblock... */
506                         buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
507                         if (!buf_len) {
508                                 /* No more to read. Break out of main loop without marking
509                                    this range of empty space as dirty (because it's not) */
510                                 D1(printk(KERN_DEBUG "Empty flash at %08x runs to end of block. Treating as free_space\n",
511                                           empty_start));
512                                 break;
513                         }
514                         D1(printk(KERN_DEBUG "Reading another 0x%x at 0x%08x\n", buf_len, ofs));
515                         err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
516                         if (err)
517                                 return err;
518                         buf_ofs = ofs;
519                         goto more_empty;
520                 }
521
522                 if (ofs == jeb->offset && je16_to_cpu(node->magic) == KSAMTIB_CIGAM_2SFFJ) {
523                         printk(KERN_WARNING "Magic bitmask is backwards at offset 0x%08x. Wrong endian filesystem?\n", ofs);
524                         DIRTY_SPACE(4);
525                         ofs += 4;
526                         continue;
527                 }
528                 if (je16_to_cpu(node->magic) == JFFS2_DIRTY_BITMASK) {
529                         D1(printk(KERN_DEBUG "Dirty bitmask at 0x%08x\n", ofs));
530                         DIRTY_SPACE(4);
531                         ofs += 4;
532                         continue;
533                 }
534                 if (je16_to_cpu(node->magic) == JFFS2_OLD_MAGIC_BITMASK) {
535                         printk(KERN_WARNING "Old JFFS2 bitmask found at 0x%08x\n", ofs);
536                         printk(KERN_WARNING "You cannot use older JFFS2 filesystems with newer kernels\n");
537                         DIRTY_SPACE(4);
538                         ofs += 4;
539                         continue;
540                 }
541                 if (je16_to_cpu(node->magic) != JFFS2_MAGIC_BITMASK) {
542                         /* OK. We're out of possibilities. Whinge and move on */
543                         noisy_printk(&noise, "jffs2_scan_eraseblock(): Magic bitmask 0x%04x not found at 0x%08x: 0x%04x instead\n",
544                                      JFFS2_MAGIC_BITMASK, ofs,
545                                      je16_to_cpu(node->magic));
546                         DIRTY_SPACE(4);
547                         ofs += 4;
548                         continue;
549                 }
550                 /* We seem to have a node of sorts. Check the CRC */
551                 crcnode.magic = node->magic;
552                 crcnode.nodetype = cpu_to_je16( je16_to_cpu(node->nodetype) | JFFS2_NODE_ACCURATE);
553                 crcnode.totlen = node->totlen;
554                 hdr_crc = crc32(0, &crcnode, sizeof(crcnode)-4);
555
556                 if (hdr_crc != je32_to_cpu(node->hdr_crc)) {
557                         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",
558                                      ofs, je16_to_cpu(node->magic),
559                                      je16_to_cpu(node->nodetype),
560                                      je32_to_cpu(node->totlen),
561                                      je32_to_cpu(node->hdr_crc),
562                                      hdr_crc);
563                         DIRTY_SPACE(4);
564                         ofs += 4;
565                         continue;
566                 }
567
568                 if (ofs + je32_to_cpu(node->totlen) >
569                     jeb->offset + c->sector_size) {
570                         /* Eep. Node goes over the end of the erase block. */
571                         printk(KERN_WARNING "Node at 0x%08x with length 0x%08x would run over the end of the erase block\n",
572                                ofs, je32_to_cpu(node->totlen));
573                         printk(KERN_WARNING "Perhaps the file system was created with the wrong erase size?\n");
574                         DIRTY_SPACE(4);
575                         ofs += 4;
576                         continue;
577                 }
578
579                 if (!(je16_to_cpu(node->nodetype) & JFFS2_NODE_ACCURATE)) {
580                         /* Wheee. This is an obsoleted node */
581                         D2(printk(KERN_DEBUG "Node at 0x%08x is obsolete. Skipping\n", ofs));
582                         DIRTY_SPACE(PAD(je32_to_cpu(node->totlen)));
583                         ofs += PAD(je32_to_cpu(node->totlen));
584                         continue;
585                 }
586
587                 switch(je16_to_cpu(node->nodetype)) {
588                 case JFFS2_NODETYPE_INODE:
589                         if (buf_ofs + buf_len < ofs + sizeof(struct jffs2_raw_inode)) {
590                                 buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
591                                 D1(printk(KERN_DEBUG "Fewer than %zd bytes (inode node) left to end of buf. Reading 0x%x at 0x%08x\n",
592                                           sizeof(struct jffs2_raw_inode), buf_len, ofs));
593                                 err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
594                                 if (err)
595                                         return err;
596                                 buf_ofs = ofs;
597                                 node = (void *)buf;
598                         }
599                         err = jffs2_scan_inode_node(c, jeb, (void *)node, ofs, s);
600                         if (err) return err;
601                         ofs += PAD(je32_to_cpu(node->totlen));
602                         break;
603
604                 case JFFS2_NODETYPE_DIRENT:
605                         if (buf_ofs + buf_len < ofs + je32_to_cpu(node->totlen)) {
606                                 buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
607                                 D1(printk(KERN_DEBUG "Fewer than %d bytes (dirent node) left to end of buf. Reading 0x%x at 0x%08x\n",
608                                           je32_to_cpu(node->totlen), buf_len, ofs));
609                                 err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
610                                 if (err)
611                                         return err;
612                                 buf_ofs = ofs;
613                                 node = (void *)buf;
614                         }
615                         err = jffs2_scan_dirent_node(c, jeb, (void *)node, ofs, s);
616                         if (err) return err;
617                         ofs += PAD(je32_to_cpu(node->totlen));
618                         break;
619
620                 case JFFS2_NODETYPE_CLEANMARKER:
621                         D1(printk(KERN_DEBUG "CLEANMARKER node found at 0x%08x\n", ofs));
622                         if (je32_to_cpu(node->totlen) != c->cleanmarker_size) {
623                                 printk(KERN_NOTICE "CLEANMARKER node found at 0x%08x has totlen 0x%x != normal 0x%x\n",
624                                        ofs, je32_to_cpu(node->totlen), c->cleanmarker_size);
625                                 DIRTY_SPACE(PAD(sizeof(struct jffs2_unknown_node)));
626                                 ofs += PAD(sizeof(struct jffs2_unknown_node));
627                         } else if (jeb->first_node) {
628                                 printk(KERN_NOTICE "CLEANMARKER node found at 0x%08x, not first node in block (0x%08x)\n", ofs, jeb->offset);
629                                 DIRTY_SPACE(PAD(sizeof(struct jffs2_unknown_node)));
630                                 ofs += PAD(sizeof(struct jffs2_unknown_node));
631                         } else {
632                                 struct jffs2_raw_node_ref *marker_ref = jffs2_alloc_raw_node_ref();
633                                 if (!marker_ref) {
634                                         printk(KERN_NOTICE "Failed to allocate node ref for clean marker\n");
635                                         return -ENOMEM;
636                                 }
637                                 marker_ref->next_in_ino = NULL;
638                                 marker_ref->next_phys = NULL;
639                                 marker_ref->flash_offset = ofs | REF_NORMAL;
640                                 marker_ref->__totlen = c->cleanmarker_size;
641                                 jeb->first_node = jeb->last_node = marker_ref;
642
643                                 USED_SPACE(PAD(c->cleanmarker_size));
644                                 ofs += PAD(c->cleanmarker_size);
645                         }
646                         break;
647
648                 case JFFS2_NODETYPE_PADDING:
649                         if (jffs2_sum_active())
650                                 jffs2_sum_add_padding_mem(s, je32_to_cpu(node->totlen));
651                         DIRTY_SPACE(PAD(je32_to_cpu(node->totlen)));
652                         ofs += PAD(je32_to_cpu(node->totlen));
653                         break;
654
655                 default:
656                         switch (je16_to_cpu(node->nodetype) & JFFS2_COMPAT_MASK) {
657                         case JFFS2_FEATURE_ROCOMPAT:
658                                 printk(KERN_NOTICE "Read-only compatible feature node (0x%04x) found at offset 0x%08x\n", je16_to_cpu(node->nodetype), ofs);
659                                 c->flags |= JFFS2_SB_FLAG_RO;
660                                 if (!(jffs2_is_readonly(c)))
661                                         return -EROFS;
662                                 DIRTY_SPACE(PAD(je32_to_cpu(node->totlen)));
663                                 ofs += PAD(je32_to_cpu(node->totlen));
664                                 break;
665
666                         case JFFS2_FEATURE_INCOMPAT:
667                                 printk(KERN_NOTICE "Incompatible feature node (0x%04x) found at offset 0x%08x\n", je16_to_cpu(node->nodetype), ofs);
668                                 return -EINVAL;
669
670                         case JFFS2_FEATURE_RWCOMPAT_DELETE:
671                                 D1(printk(KERN_NOTICE "Unknown but compatible feature node (0x%04x) found at offset 0x%08x\n", je16_to_cpu(node->nodetype), ofs));
672                                 DIRTY_SPACE(PAD(je32_to_cpu(node->totlen)));
673                                 ofs += PAD(je32_to_cpu(node->totlen));
674                                 break;
675
676                         case JFFS2_FEATURE_RWCOMPAT_COPY:
677                                 D1(printk(KERN_NOTICE "Unknown but compatible feature node (0x%04x) found at offset 0x%08x\n", je16_to_cpu(node->nodetype), ofs));
678                                 USED_SPACE(PAD(je32_to_cpu(node->totlen)));
679                                 ofs += PAD(je32_to_cpu(node->totlen));
680                                 break;
681                         }
682                 }
683         }
684
685         if (jffs2_sum_active()) {
686                 if (PAD(s->sum_size + JFFS2_SUMMARY_FRAME_SIZE) > jeb->free_size) {
687                         dbg_summary("There is not enough space for "
688                                 "summary information, disabling for this jeb!\n");
689                         jffs2_sum_disable_collecting(s);
690                 }
691         }
692
693         D1(printk(KERN_DEBUG "Block at 0x%08x: free 0x%08x, dirty 0x%08x, unchecked 0x%08x, used 0x%08x\n", jeb->offset,
694                   jeb->free_size, jeb->dirty_size, jeb->unchecked_size, jeb->used_size));
695
696         /* mark_node_obsolete can add to wasted !! */
697         if (jeb->wasted_size) {
698                 jeb->dirty_size += jeb->wasted_size;
699                 c->dirty_size += jeb->wasted_size;
700                 c->wasted_size -= jeb->wasted_size;
701                 jeb->wasted_size = 0;
702         }
703
704         return jffs2_scan_classify_jeb(c, jeb);
705 }
706
707 struct jffs2_inode_cache *jffs2_scan_make_ino_cache(struct jffs2_sb_info *c, uint32_t ino)
708 {
709         struct jffs2_inode_cache *ic;
710
711         ic = jffs2_get_ino_cache(c, ino);
712         if (ic)
713                 return ic;
714
715         if (ino > c->highest_ino)
716                 c->highest_ino = ino;
717
718         ic = jffs2_alloc_inode_cache();
719         if (!ic) {
720                 printk(KERN_NOTICE "jffs2_scan_make_inode_cache(): allocation of inode cache failed\n");
721                 return NULL;
722         }
723         memset(ic, 0, sizeof(*ic));
724
725         ic->ino = ino;
726         ic->nodes = (void *)ic;
727         jffs2_add_ino_cache(c, ic);
728         if (ino == 1)
729                 ic->nlink = 1;
730         return ic;
731 }
732
733 static int jffs2_scan_inode_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
734                                  struct jffs2_raw_inode *ri, uint32_t ofs, struct jffs2_summary *s)
735 {
736         struct jffs2_raw_node_ref *raw;
737         struct jffs2_inode_cache *ic;
738         uint32_t ino = je32_to_cpu(ri->ino);
739
740         D1(printk(KERN_DEBUG "jffs2_scan_inode_node(): Node at 0x%08x\n", ofs));
741
742         /* We do very little here now. Just check the ino# to which we should attribute
743            this node; we can do all the CRC checking etc. later. There's a tradeoff here --
744            we used to scan the flash once only, reading everything we want from it into
745            memory, then building all our in-core data structures and freeing the extra
746            information. Now we allow the first part of the mount to complete a lot quicker,
747            but we have to go _back_ to the flash in order to finish the CRC checking, etc.
748            Which means that the _full_ amount of time to get to proper write mode with GC
749            operational may actually be _longer_ than before. Sucks to be me. */
750
751         raw = jffs2_alloc_raw_node_ref();
752         if (!raw) {
753                 printk(KERN_NOTICE "jffs2_scan_inode_node(): allocation of node reference failed\n");
754                 return -ENOMEM;
755         }
756
757         ic = jffs2_get_ino_cache(c, ino);
758         if (!ic) {
759                 /* Inocache get failed. Either we read a bogus ino# or it's just genuinely the
760                    first node we found for this inode. Do a CRC check to protect against the former
761                    case */
762                 uint32_t crc = crc32(0, ri, sizeof(*ri)-8);
763
764                 if (crc != je32_to_cpu(ri->node_crc)) {
765                         printk(KERN_NOTICE "jffs2_scan_inode_node(): CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
766                                ofs, je32_to_cpu(ri->node_crc), crc);
767                         /* We believe totlen because the CRC on the node _header_ was OK, just the node itself failed. */
768                         DIRTY_SPACE(PAD(je32_to_cpu(ri->totlen)));
769                         jffs2_free_raw_node_ref(raw);
770                         return 0;
771                 }
772                 ic = jffs2_scan_make_ino_cache(c, ino);
773                 if (!ic) {
774                         jffs2_free_raw_node_ref(raw);
775                         return -ENOMEM;
776                 }
777         }
778
779         /* Wheee. It worked */
780
781         raw->flash_offset = ofs | REF_UNCHECKED;
782         raw->__totlen = PAD(je32_to_cpu(ri->totlen));
783         raw->next_phys = NULL;
784         raw->next_in_ino = ic->nodes;
785
786         ic->nodes = raw;
787         if (!jeb->first_node)
788                 jeb->first_node = raw;
789         if (jeb->last_node)
790                 jeb->last_node->next_phys = raw;
791         jeb->last_node = raw;
792
793         D1(printk(KERN_DEBUG "Node is ino #%u, version %d. Range 0x%x-0x%x\n",
794                   je32_to_cpu(ri->ino), je32_to_cpu(ri->version),
795                   je32_to_cpu(ri->offset),
796                   je32_to_cpu(ri->offset)+je32_to_cpu(ri->dsize)));
797
798         pseudo_random += je32_to_cpu(ri->version);
799
800         UNCHECKED_SPACE(PAD(je32_to_cpu(ri->totlen)));
801
802         if (jffs2_sum_active()) {
803                 jffs2_sum_add_inode_mem(s, ri, ofs - jeb->offset);
804         }
805
806         return 0;
807 }
808
809 static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
810                                   struct jffs2_raw_dirent *rd, uint32_t ofs, struct jffs2_summary *s)
811 {
812         struct jffs2_raw_node_ref *raw;
813         struct jffs2_full_dirent *fd;
814         struct jffs2_inode_cache *ic;
815         uint32_t crc;
816
817         D1(printk(KERN_DEBUG "jffs2_scan_dirent_node(): Node at 0x%08x\n", ofs));
818
819         /* We don't get here unless the node is still valid, so we don't have to
820            mask in the ACCURATE bit any more. */
821         crc = crc32(0, rd, sizeof(*rd)-8);
822
823         if (crc != je32_to_cpu(rd->node_crc)) {
824                 printk(KERN_NOTICE "jffs2_scan_dirent_node(): Node CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
825                        ofs, je32_to_cpu(rd->node_crc), crc);
826                 /* We believe totlen because the CRC on the node _header_ was OK, just the node itself failed. */
827                 DIRTY_SPACE(PAD(je32_to_cpu(rd->totlen)));
828                 return 0;
829         }
830
831         pseudo_random += je32_to_cpu(rd->version);
832
833         fd = jffs2_alloc_full_dirent(rd->nsize+1);
834         if (!fd) {
835                 return -ENOMEM;
836         }
837         memcpy(&fd->name, rd->name, rd->nsize);
838         fd->name[rd->nsize] = 0;
839
840         crc = crc32(0, fd->name, rd->nsize);
841         if (crc != je32_to_cpu(rd->name_crc)) {
842                 printk(KERN_NOTICE "jffs2_scan_dirent_node(): Name CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
843                        ofs, je32_to_cpu(rd->name_crc), crc);
844                 D1(printk(KERN_NOTICE "Name for which CRC failed is (now) '%s', ino #%d\n", fd->name, je32_to_cpu(rd->ino)));
845                 jffs2_free_full_dirent(fd);
846                 /* FIXME: Why do we believe totlen? */
847                 /* We believe totlen because the CRC on the node _header_ was OK, just the name failed. */
848                 DIRTY_SPACE(PAD(je32_to_cpu(rd->totlen)));
849                 return 0;
850         }
851         raw = jffs2_alloc_raw_node_ref();
852         if (!raw) {
853                 jffs2_free_full_dirent(fd);
854                 printk(KERN_NOTICE "jffs2_scan_dirent_node(): allocation of node reference failed\n");
855                 return -ENOMEM;
856         }
857         ic = jffs2_scan_make_ino_cache(c, je32_to_cpu(rd->pino));
858         if (!ic) {
859                 jffs2_free_full_dirent(fd);
860                 jffs2_free_raw_node_ref(raw);
861                 return -ENOMEM;
862         }
863
864         raw->__totlen = PAD(je32_to_cpu(rd->totlen));
865         raw->flash_offset = ofs | REF_PRISTINE;
866         raw->next_phys = NULL;
867         raw->next_in_ino = ic->nodes;
868         ic->nodes = raw;
869         if (!jeb->first_node)
870                 jeb->first_node = raw;
871         if (jeb->last_node)
872                 jeb->last_node->next_phys = raw;
873         jeb->last_node = raw;
874
875         fd->raw = raw;
876         fd->next = NULL;
877         fd->version = je32_to_cpu(rd->version);
878         fd->ino = je32_to_cpu(rd->ino);
879         fd->nhash = full_name_hash(fd->name, rd->nsize);
880         fd->type = rd->type;
881         USED_SPACE(PAD(je32_to_cpu(rd->totlen)));
882         jffs2_add_fd_to_list(c, fd, &ic->scan_dents);
883
884         if (jffs2_sum_active()) {
885                 jffs2_sum_add_dirent_mem(s, rd, ofs - jeb->offset);
886         }
887
888         return 0;
889 }
890
891 static int count_list(struct list_head *l)
892 {
893         uint32_t count = 0;
894         struct list_head *tmp;
895
896         list_for_each(tmp, l) {
897                 count++;
898         }
899         return count;
900 }
901
902 /* Note: This breaks if list_empty(head). I don't care. You
903    might, if you copy this code and use it elsewhere :) */
904 static void rotate_list(struct list_head *head, uint32_t count)
905 {
906         struct list_head *n = head->next;
907
908         list_del(head);
909         while(count--) {
910                 n = n->next;
911         }
912         list_add(head, n);
913 }
914
915 void jffs2_rotate_lists(struct jffs2_sb_info *c)
916 {
917         uint32_t x;
918         uint32_t rotateby;
919
920         x = count_list(&c->clean_list);
921         if (x) {
922                 rotateby = pseudo_random % x;
923                 rotate_list((&c->clean_list), rotateby);
924         }
925
926         x = count_list(&c->very_dirty_list);
927         if (x) {
928                 rotateby = pseudo_random % x;
929                 rotate_list((&c->very_dirty_list), rotateby);
930         }
931
932         x = count_list(&c->dirty_list);
933         if (x) {
934                 rotateby = pseudo_random % x;
935                 rotate_list((&c->dirty_list), rotateby);
936         }
937
938         x = count_list(&c->erasable_list);
939         if (x) {
940                 rotateby = pseudo_random % x;
941                 rotate_list((&c->erasable_list), rotateby);
942         }
943
944         if (c->nr_erasing_blocks) {
945                 rotateby = pseudo_random % c->nr_erasing_blocks;
946                 rotate_list((&c->erase_pending_list), rotateby);
947         }
948
949         if (c->nr_free_blocks) {
950                 rotateby = pseudo_random % c->nr_free_blocks;
951                 rotate_list((&c->free_list), rotateby);
952         }
953 }