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