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