scan.c 29 KB

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