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