scan.c 30 KB

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