scan.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  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. if (jffs2_sum_active() && s)
  201. kfree(s);
  202. /* Nextblock dirty is always seen as wasted, because we cannot recycle it now */
  203. if (c->nextblock && (c->nextblock->dirty_size)) {
  204. c->nextblock->wasted_size += c->nextblock->dirty_size;
  205. c->wasted_size += c->nextblock->dirty_size;
  206. c->dirty_size -= c->nextblock->dirty_size;
  207. c->nextblock->dirty_size = 0;
  208. }
  209. #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
  210. if (!jffs2_can_mark_obsolete(c) && c->nextblock && (c->nextblock->free_size % c->wbuf_pagesize)) {
  211. /* If we're going to start writing into a block which already
  212. contains data, and the end of the data isn't page-aligned,
  213. skip a little and align it. */
  214. uint32_t skip = c->nextblock->free_size % c->wbuf_pagesize;
  215. D1(printk(KERN_DEBUG "jffs2_scan_medium(): Skipping %d bytes in nextblock to ensure page alignment\n",
  216. skip));
  217. c->nextblock->wasted_size += skip;
  218. c->wasted_size += skip;
  219. c->nextblock->free_size -= skip;
  220. c->free_size -= skip;
  221. }
  222. #endif
  223. if (c->nr_erasing_blocks) {
  224. if ( !c->used_size && ((c->nr_free_blocks+empty_blocks+bad_blocks)!= c->nr_blocks || bad_blocks == c->nr_blocks) ) {
  225. printk(KERN_NOTICE "Cowardly refusing to erase blocks on filesystem with no valid JFFS2 nodes\n");
  226. printk(KERN_NOTICE "empty_blocks %d, bad_blocks %d, c->nr_blocks %d\n",empty_blocks,bad_blocks,c->nr_blocks);
  227. ret = -EIO;
  228. goto out;
  229. }
  230. jffs2_erase_pending_trigger(c);
  231. }
  232. ret = 0;
  233. out:
  234. if (buf_size)
  235. kfree(flashbuf);
  236. #ifndef __ECOS
  237. else
  238. c->mtd->unpoint(c->mtd, flashbuf, 0, c->mtd->size);
  239. #endif
  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. ofs = jeb->offset;
  326. prevofs = jeb->offset - 1;
  327. }
  328. buf_ofs = jeb->offset;
  329. if (!buf_size) {
  330. buf_len = c->sector_size;
  331. if (jffs2_sum_active()) {
  332. /* must reread because of summary test */
  333. err = jffs2_fill_scan_buf(c, buf, buf_ofs, buf_len);
  334. if (err)
  335. return err;
  336. }
  337. } else {
  338. buf_len = EMPTY_SCAN_SIZE(c->sector_size);
  339. err = jffs2_fill_scan_buf(c, buf, buf_ofs, buf_len);
  340. if (err)
  341. return err;
  342. }
  343. /* We temporarily use 'ofs' as a pointer into the buffer/jeb */
  344. ofs = 0;
  345. /* Scan only 4KiB of 0xFF before declaring it's empty */
  346. while(ofs < EMPTY_SCAN_SIZE(c->sector_size) && *(uint32_t *)(&buf[ofs]) == 0xFFFFFFFF)
  347. ofs += 4;
  348. if (ofs == EMPTY_SCAN_SIZE(c->sector_size)) {
  349. #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
  350. if (jffs2_cleanmarker_oob(c)) {
  351. /* scan oob, take care of cleanmarker */
  352. int ret = jffs2_check_oob_empty(c, jeb, cleanmarkerfound);
  353. D2(printk(KERN_NOTICE "jffs2_check_oob_empty returned %d\n",ret));
  354. switch (ret) {
  355. case 0: return cleanmarkerfound ? BLK_STATE_CLEANMARKER : BLK_STATE_ALLFF;
  356. case 1: return BLK_STATE_ALLDIRTY;
  357. default: return ret;
  358. }
  359. }
  360. #endif
  361. D1(printk(KERN_DEBUG "Block at 0x%08x is empty (erased)\n", jeb->offset));
  362. if (c->cleanmarker_size == 0)
  363. return BLK_STATE_CLEANMARKER; /* don't bother with re-erase */
  364. else
  365. return BLK_STATE_ALLFF; /* OK to erase if all blocks are like this */
  366. }
  367. if (ofs) {
  368. D1(printk(KERN_DEBUG "Free space at %08x ends at %08x\n", jeb->offset,
  369. jeb->offset + ofs));
  370. DIRTY_SPACE(ofs);
  371. }
  372. /* Now ofs is a complete physical flash offset as it always was... */
  373. ofs += jeb->offset;
  374. noise = 10;
  375. dbg_summary("no summary found in jeb 0x%08x. Apply original scan.\n",jeb->offset);
  376. scan_more:
  377. while(ofs < jeb->offset + c->sector_size) {
  378. jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
  379. cond_resched();
  380. if (ofs & 3) {
  381. printk(KERN_WARNING "Eep. ofs 0x%08x not word-aligned!\n", ofs);
  382. ofs = PAD(ofs);
  383. continue;
  384. }
  385. if (ofs == prevofs) {
  386. printk(KERN_WARNING "ofs 0x%08x has already been seen. Skipping\n", ofs);
  387. DIRTY_SPACE(4);
  388. ofs += 4;
  389. continue;
  390. }
  391. prevofs = ofs;
  392. if (jeb->offset + c->sector_size < ofs + sizeof(*node)) {
  393. 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),
  394. jeb->offset, c->sector_size, ofs, sizeof(*node)));
  395. DIRTY_SPACE((jeb->offset + c->sector_size)-ofs);
  396. break;
  397. }
  398. if (buf_ofs + buf_len < ofs + sizeof(*node)) {
  399. buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
  400. D1(printk(KERN_DEBUG "Fewer than %zd bytes (node header) left to end of buf. Reading 0x%x at 0x%08x\n",
  401. sizeof(struct jffs2_unknown_node), buf_len, ofs));
  402. err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
  403. if (err)
  404. return err;
  405. buf_ofs = ofs;
  406. }
  407. node = (struct jffs2_unknown_node *)&buf[ofs-buf_ofs];
  408. if (*(uint32_t *)(&buf[ofs-buf_ofs]) == 0xffffffff) {
  409. uint32_t inbuf_ofs;
  410. uint32_t empty_start;
  411. empty_start = ofs;
  412. ofs += 4;
  413. D1(printk(KERN_DEBUG "Found empty flash at 0x%08x\n", ofs));
  414. more_empty:
  415. inbuf_ofs = ofs - buf_ofs;
  416. while (inbuf_ofs < buf_len) {
  417. if (*(uint32_t *)(&buf[inbuf_ofs]) != 0xffffffff) {
  418. printk(KERN_WARNING "Empty flash at 0x%08x ends at 0x%08x\n",
  419. empty_start, ofs);
  420. DIRTY_SPACE(ofs-empty_start);
  421. goto scan_more;
  422. }
  423. inbuf_ofs+=4;
  424. ofs += 4;
  425. }
  426. /* Ran off end. */
  427. D1(printk(KERN_DEBUG "Empty flash to end of buffer at 0x%08x\n", ofs));
  428. /* If we're only checking the beginning of a block with a cleanmarker,
  429. bail now */
  430. if (buf_ofs == jeb->offset && jeb->used_size == PAD(c->cleanmarker_size) &&
  431. c->cleanmarker_size && !jeb->dirty_size && !jeb->first_node->next_phys) {
  432. D1(printk(KERN_DEBUG "%d bytes at start of block seems clean... assuming all clean\n", EMPTY_SCAN_SIZE(c->sector_size)));
  433. return BLK_STATE_CLEANMARKER;
  434. }
  435. /* See how much more there is to read in this eraseblock... */
  436. buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
  437. if (!buf_len) {
  438. /* No more to read. Break out of main loop without marking
  439. this range of empty space as dirty (because it's not) */
  440. D1(printk(KERN_DEBUG "Empty flash at %08x runs to end of block. Treating as free_space\n",
  441. empty_start));
  442. break;
  443. }
  444. D1(printk(KERN_DEBUG "Reading another 0x%x at 0x%08x\n", buf_len, ofs));
  445. err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
  446. if (err)
  447. return err;
  448. buf_ofs = ofs;
  449. goto more_empty;
  450. }
  451. if (ofs == jeb->offset && je16_to_cpu(node->magic) == KSAMTIB_CIGAM_2SFFJ) {
  452. printk(KERN_WARNING "Magic bitmask is backwards at offset 0x%08x. Wrong endian filesystem?\n", ofs);
  453. DIRTY_SPACE(4);
  454. ofs += 4;
  455. continue;
  456. }
  457. if (je16_to_cpu(node->magic) == JFFS2_DIRTY_BITMASK) {
  458. D1(printk(KERN_DEBUG "Dirty bitmask at 0x%08x\n", ofs));
  459. DIRTY_SPACE(4);
  460. ofs += 4;
  461. continue;
  462. }
  463. if (je16_to_cpu(node->magic) == JFFS2_OLD_MAGIC_BITMASK) {
  464. printk(KERN_WARNING "Old JFFS2 bitmask found at 0x%08x\n", ofs);
  465. printk(KERN_WARNING "You cannot use older JFFS2 filesystems with newer kernels\n");
  466. DIRTY_SPACE(4);
  467. ofs += 4;
  468. continue;
  469. }
  470. if (je16_to_cpu(node->magic) != JFFS2_MAGIC_BITMASK) {
  471. /* OK. We're out of possibilities. Whinge and move on */
  472. noisy_printk(&noise, "jffs2_scan_eraseblock(): Magic bitmask 0x%04x not found at 0x%08x: 0x%04x instead\n",
  473. JFFS2_MAGIC_BITMASK, ofs,
  474. je16_to_cpu(node->magic));
  475. DIRTY_SPACE(4);
  476. ofs += 4;
  477. continue;
  478. }
  479. /* We seem to have a node of sorts. Check the CRC */
  480. crcnode.magic = node->magic;
  481. crcnode.nodetype = cpu_to_je16( je16_to_cpu(node->nodetype) | JFFS2_NODE_ACCURATE);
  482. crcnode.totlen = node->totlen;
  483. hdr_crc = crc32(0, &crcnode, sizeof(crcnode)-4);
  484. if (hdr_crc != je32_to_cpu(node->hdr_crc)) {
  485. noisy_printk(&noise, "jffs2_scan_eraseblock(): Node at 0x%08x {0x%04x, 0x%04x, 0x%08x) has invalid CRC 0x%08x (calculated 0x%08x)\n",
  486. ofs, je16_to_cpu(node->magic),
  487. je16_to_cpu(node->nodetype),
  488. je32_to_cpu(node->totlen),
  489. je32_to_cpu(node->hdr_crc),
  490. hdr_crc);
  491. DIRTY_SPACE(4);
  492. ofs += 4;
  493. continue;
  494. }
  495. if (ofs + je32_to_cpu(node->totlen) >
  496. jeb->offset + c->sector_size) {
  497. /* Eep. Node goes over the end of the erase block. */
  498. printk(KERN_WARNING "Node at 0x%08x with length 0x%08x would run over the end of the erase block\n",
  499. ofs, je32_to_cpu(node->totlen));
  500. printk(KERN_WARNING "Perhaps the file system was created with the wrong erase size?\n");
  501. DIRTY_SPACE(4);
  502. ofs += 4;
  503. continue;
  504. }
  505. if (!(je16_to_cpu(node->nodetype) & JFFS2_NODE_ACCURATE)) {
  506. /* Wheee. This is an obsoleted node */
  507. D2(printk(KERN_DEBUG "Node at 0x%08x is obsolete. Skipping\n", ofs));
  508. DIRTY_SPACE(PAD(je32_to_cpu(node->totlen)));
  509. ofs += PAD(je32_to_cpu(node->totlen));
  510. continue;
  511. }
  512. switch(je16_to_cpu(node->nodetype)) {
  513. case JFFS2_NODETYPE_INODE:
  514. if (buf_ofs + buf_len < ofs + sizeof(struct jffs2_raw_inode)) {
  515. buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
  516. D1(printk(KERN_DEBUG "Fewer than %zd bytes (inode node) left to end of buf. Reading 0x%x at 0x%08x\n",
  517. sizeof(struct jffs2_raw_inode), buf_len, ofs));
  518. err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
  519. if (err)
  520. return err;
  521. buf_ofs = ofs;
  522. node = (void *)buf;
  523. }
  524. err = jffs2_scan_inode_node(c, jeb, (void *)node, ofs, s);
  525. if (err) return err;
  526. ofs += PAD(je32_to_cpu(node->totlen));
  527. break;
  528. case JFFS2_NODETYPE_DIRENT:
  529. if (buf_ofs + buf_len < ofs + je32_to_cpu(node->totlen)) {
  530. buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
  531. D1(printk(KERN_DEBUG "Fewer than %d bytes (dirent node) left to end of buf. Reading 0x%x at 0x%08x\n",
  532. je32_to_cpu(node->totlen), buf_len, ofs));
  533. err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
  534. if (err)
  535. return err;
  536. buf_ofs = ofs;
  537. node = (void *)buf;
  538. }
  539. err = jffs2_scan_dirent_node(c, jeb, (void *)node, ofs, s);
  540. if (err) return err;
  541. ofs += PAD(je32_to_cpu(node->totlen));
  542. break;
  543. case JFFS2_NODETYPE_CLEANMARKER:
  544. D1(printk(KERN_DEBUG "CLEANMARKER node found at 0x%08x\n", ofs));
  545. if (je32_to_cpu(node->totlen) != c->cleanmarker_size) {
  546. printk(KERN_NOTICE "CLEANMARKER node found at 0x%08x has totlen 0x%x != normal 0x%x\n",
  547. ofs, je32_to_cpu(node->totlen), c->cleanmarker_size);
  548. DIRTY_SPACE(PAD(sizeof(struct jffs2_unknown_node)));
  549. ofs += PAD(sizeof(struct jffs2_unknown_node));
  550. } else if (jeb->first_node) {
  551. printk(KERN_NOTICE "CLEANMARKER node found at 0x%08x, not first node in block (0x%08x)\n", ofs, jeb->offset);
  552. DIRTY_SPACE(PAD(sizeof(struct jffs2_unknown_node)));
  553. ofs += PAD(sizeof(struct jffs2_unknown_node));
  554. } else {
  555. struct jffs2_raw_node_ref *marker_ref = jffs2_alloc_raw_node_ref();
  556. if (!marker_ref) {
  557. printk(KERN_NOTICE "Failed to allocate node ref for clean marker\n");
  558. return -ENOMEM;
  559. }
  560. marker_ref->next_in_ino = NULL;
  561. marker_ref->next_phys = NULL;
  562. marker_ref->flash_offset = ofs | REF_NORMAL;
  563. marker_ref->__totlen = c->cleanmarker_size;
  564. jeb->first_node = jeb->last_node = marker_ref;
  565. USED_SPACE(PAD(c->cleanmarker_size));
  566. ofs += PAD(c->cleanmarker_size);
  567. }
  568. break;
  569. case JFFS2_NODETYPE_PADDING:
  570. if (jffs2_sum_active())
  571. jffs2_sum_add_padding_mem(s, je32_to_cpu(node->totlen));
  572. DIRTY_SPACE(PAD(je32_to_cpu(node->totlen)));
  573. ofs += PAD(je32_to_cpu(node->totlen));
  574. break;
  575. default:
  576. switch (je16_to_cpu(node->nodetype) & JFFS2_COMPAT_MASK) {
  577. case JFFS2_FEATURE_ROCOMPAT:
  578. printk(KERN_NOTICE "Read-only compatible feature node (0x%04x) found at offset 0x%08x\n", je16_to_cpu(node->nodetype), ofs);
  579. c->flags |= JFFS2_SB_FLAG_RO;
  580. if (!(jffs2_is_readonly(c)))
  581. return -EROFS;
  582. DIRTY_SPACE(PAD(je32_to_cpu(node->totlen)));
  583. ofs += PAD(je32_to_cpu(node->totlen));
  584. break;
  585. case JFFS2_FEATURE_INCOMPAT:
  586. printk(KERN_NOTICE "Incompatible feature node (0x%04x) found at offset 0x%08x\n", je16_to_cpu(node->nodetype), ofs);
  587. return -EINVAL;
  588. case JFFS2_FEATURE_RWCOMPAT_DELETE:
  589. D1(printk(KERN_NOTICE "Unknown but compatible feature node (0x%04x) found at offset 0x%08x\n", je16_to_cpu(node->nodetype), ofs));
  590. DIRTY_SPACE(PAD(je32_to_cpu(node->totlen)));
  591. ofs += PAD(je32_to_cpu(node->totlen));
  592. break;
  593. case JFFS2_FEATURE_RWCOMPAT_COPY:
  594. D1(printk(KERN_NOTICE "Unknown but compatible feature node (0x%04x) found at offset 0x%08x\n", je16_to_cpu(node->nodetype), ofs));
  595. USED_SPACE(PAD(je32_to_cpu(node->totlen)));
  596. ofs += PAD(je32_to_cpu(node->totlen));
  597. break;
  598. }
  599. }
  600. }
  601. if (jffs2_sum_active()) {
  602. if (PAD(s->sum_size + JFFS2_SUMMARY_FRAME_SIZE) > jeb->free_size) {
  603. dbg_summary("There is not enough space for "
  604. "summary information, disabling for this jeb!\n");
  605. jffs2_sum_disable_collecting(s);
  606. }
  607. }
  608. D1(printk(KERN_DEBUG "Block at 0x%08x: free 0x%08x, dirty 0x%08x, unchecked 0x%08x, used 0x%08x\n", jeb->offset,
  609. jeb->free_size, jeb->dirty_size, jeb->unchecked_size, jeb->used_size));
  610. /* mark_node_obsolete can add to wasted !! */
  611. if (jeb->wasted_size) {
  612. jeb->dirty_size += jeb->wasted_size;
  613. c->dirty_size += jeb->wasted_size;
  614. c->wasted_size -= jeb->wasted_size;
  615. jeb->wasted_size = 0;
  616. }
  617. return jffs2_scan_classify_jeb(c, jeb);
  618. }
  619. struct jffs2_inode_cache *jffs2_scan_make_ino_cache(struct jffs2_sb_info *c, uint32_t ino)
  620. {
  621. struct jffs2_inode_cache *ic;
  622. ic = jffs2_get_ino_cache(c, ino);
  623. if (ic)
  624. return ic;
  625. if (ino > c->highest_ino)
  626. c->highest_ino = ino;
  627. ic = jffs2_alloc_inode_cache();
  628. if (!ic) {
  629. printk(KERN_NOTICE "jffs2_scan_make_inode_cache(): allocation of inode cache failed\n");
  630. return NULL;
  631. }
  632. memset(ic, 0, sizeof(*ic));
  633. ic->ino = ino;
  634. ic->nodes = (void *)ic;
  635. jffs2_add_ino_cache(c, ic);
  636. if (ino == 1)
  637. ic->nlink = 1;
  638. return ic;
  639. }
  640. static int jffs2_scan_inode_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
  641. struct jffs2_raw_inode *ri, uint32_t ofs, struct jffs2_summary *s)
  642. {
  643. struct jffs2_raw_node_ref *raw;
  644. struct jffs2_inode_cache *ic;
  645. uint32_t ino = je32_to_cpu(ri->ino);
  646. D1(printk(KERN_DEBUG "jffs2_scan_inode_node(): Node at 0x%08x\n", ofs));
  647. /* We do very little here now. Just check the ino# to which we should attribute
  648. this node; we can do all the CRC checking etc. later. There's a tradeoff here --
  649. we used to scan the flash once only, reading everything we want from it into
  650. memory, then building all our in-core data structures and freeing the extra
  651. information. Now we allow the first part of the mount to complete a lot quicker,
  652. but we have to go _back_ to the flash in order to finish the CRC checking, etc.
  653. Which means that the _full_ amount of time to get to proper write mode with GC
  654. operational may actually be _longer_ than before. Sucks to be me. */
  655. raw = jffs2_alloc_raw_node_ref();
  656. if (!raw) {
  657. printk(KERN_NOTICE "jffs2_scan_inode_node(): allocation of node reference failed\n");
  658. return -ENOMEM;
  659. }
  660. ic = jffs2_get_ino_cache(c, ino);
  661. if (!ic) {
  662. /* Inocache get failed. Either we read a bogus ino# or it's just genuinely the
  663. first node we found for this inode. Do a CRC check to protect against the former
  664. case */
  665. uint32_t crc = crc32(0, ri, sizeof(*ri)-8);
  666. if (crc != je32_to_cpu(ri->node_crc)) {
  667. printk(KERN_NOTICE "jffs2_scan_inode_node(): CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
  668. ofs, je32_to_cpu(ri->node_crc), crc);
  669. /* We believe totlen because the CRC on the node _header_ was OK, just the node itself failed. */
  670. DIRTY_SPACE(PAD(je32_to_cpu(ri->totlen)));
  671. jffs2_free_raw_node_ref(raw);
  672. return 0;
  673. }
  674. ic = jffs2_scan_make_ino_cache(c, ino);
  675. if (!ic) {
  676. jffs2_free_raw_node_ref(raw);
  677. return -ENOMEM;
  678. }
  679. }
  680. /* Wheee. It worked */
  681. raw->flash_offset = ofs | REF_UNCHECKED;
  682. raw->__totlen = PAD(je32_to_cpu(ri->totlen));
  683. raw->next_phys = NULL;
  684. raw->next_in_ino = ic->nodes;
  685. ic->nodes = raw;
  686. if (!jeb->first_node)
  687. jeb->first_node = raw;
  688. if (jeb->last_node)
  689. jeb->last_node->next_phys = raw;
  690. jeb->last_node = raw;
  691. D1(printk(KERN_DEBUG "Node is ino #%u, version %d. Range 0x%x-0x%x\n",
  692. je32_to_cpu(ri->ino), je32_to_cpu(ri->version),
  693. je32_to_cpu(ri->offset),
  694. je32_to_cpu(ri->offset)+je32_to_cpu(ri->dsize)));
  695. pseudo_random += je32_to_cpu(ri->version);
  696. UNCHECKED_SPACE(PAD(je32_to_cpu(ri->totlen)));
  697. if (jffs2_sum_active()) {
  698. jffs2_sum_add_inode_mem(s, ri, ofs - jeb->offset);
  699. }
  700. return 0;
  701. }
  702. static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
  703. struct jffs2_raw_dirent *rd, uint32_t ofs, struct jffs2_summary *s)
  704. {
  705. struct jffs2_raw_node_ref *raw;
  706. struct jffs2_full_dirent *fd;
  707. struct jffs2_inode_cache *ic;
  708. uint32_t crc;
  709. D1(printk(KERN_DEBUG "jffs2_scan_dirent_node(): Node at 0x%08x\n", ofs));
  710. /* We don't get here unless the node is still valid, so we don't have to
  711. mask in the ACCURATE bit any more. */
  712. crc = crc32(0, rd, sizeof(*rd)-8);
  713. if (crc != je32_to_cpu(rd->node_crc)) {
  714. printk(KERN_NOTICE "jffs2_scan_dirent_node(): Node CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
  715. ofs, je32_to_cpu(rd->node_crc), crc);
  716. /* We believe totlen because the CRC on the node _header_ was OK, just the node itself failed. */
  717. DIRTY_SPACE(PAD(je32_to_cpu(rd->totlen)));
  718. return 0;
  719. }
  720. pseudo_random += je32_to_cpu(rd->version);
  721. fd = jffs2_alloc_full_dirent(rd->nsize+1);
  722. if (!fd) {
  723. return -ENOMEM;
  724. }
  725. memcpy(&fd->name, rd->name, rd->nsize);
  726. fd->name[rd->nsize] = 0;
  727. crc = crc32(0, fd->name, rd->nsize);
  728. if (crc != je32_to_cpu(rd->name_crc)) {
  729. printk(KERN_NOTICE "jffs2_scan_dirent_node(): Name CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
  730. ofs, je32_to_cpu(rd->name_crc), crc);
  731. D1(printk(KERN_NOTICE "Name for which CRC failed is (now) '%s', ino #%d\n", fd->name, je32_to_cpu(rd->ino)));
  732. jffs2_free_full_dirent(fd);
  733. /* FIXME: Why do we believe totlen? */
  734. /* We believe totlen because the CRC on the node _header_ was OK, just the name failed. */
  735. DIRTY_SPACE(PAD(je32_to_cpu(rd->totlen)));
  736. return 0;
  737. }
  738. raw = jffs2_alloc_raw_node_ref();
  739. if (!raw) {
  740. jffs2_free_full_dirent(fd);
  741. printk(KERN_NOTICE "jffs2_scan_dirent_node(): allocation of node reference failed\n");
  742. return -ENOMEM;
  743. }
  744. ic = jffs2_scan_make_ino_cache(c, je32_to_cpu(rd->pino));
  745. if (!ic) {
  746. jffs2_free_full_dirent(fd);
  747. jffs2_free_raw_node_ref(raw);
  748. return -ENOMEM;
  749. }
  750. raw->__totlen = PAD(je32_to_cpu(rd->totlen));
  751. raw->flash_offset = ofs | REF_PRISTINE;
  752. raw->next_phys = NULL;
  753. raw->next_in_ino = ic->nodes;
  754. ic->nodes = raw;
  755. if (!jeb->first_node)
  756. jeb->first_node = raw;
  757. if (jeb->last_node)
  758. jeb->last_node->next_phys = raw;
  759. jeb->last_node = raw;
  760. fd->raw = raw;
  761. fd->next = NULL;
  762. fd->version = je32_to_cpu(rd->version);
  763. fd->ino = je32_to_cpu(rd->ino);
  764. fd->nhash = full_name_hash(fd->name, rd->nsize);
  765. fd->type = rd->type;
  766. USED_SPACE(PAD(je32_to_cpu(rd->totlen)));
  767. jffs2_add_fd_to_list(c, fd, &ic->scan_dents);
  768. if (jffs2_sum_active()) {
  769. jffs2_sum_add_dirent_mem(s, rd, ofs - jeb->offset);
  770. }
  771. return 0;
  772. }
  773. static int count_list(struct list_head *l)
  774. {
  775. uint32_t count = 0;
  776. struct list_head *tmp;
  777. list_for_each(tmp, l) {
  778. count++;
  779. }
  780. return count;
  781. }
  782. /* Note: This breaks if list_empty(head). I don't care. You
  783. might, if you copy this code and use it elsewhere :) */
  784. static void rotate_list(struct list_head *head, uint32_t count)
  785. {
  786. struct list_head *n = head->next;
  787. list_del(head);
  788. while(count--) {
  789. n = n->next;
  790. }
  791. list_add(head, n);
  792. }
  793. void jffs2_rotate_lists(struct jffs2_sb_info *c)
  794. {
  795. uint32_t x;
  796. uint32_t rotateby;
  797. x = count_list(&c->clean_list);
  798. if (x) {
  799. rotateby = pseudo_random % x;
  800. rotate_list((&c->clean_list), rotateby);
  801. }
  802. x = count_list(&c->very_dirty_list);
  803. if (x) {
  804. rotateby = pseudo_random % x;
  805. rotate_list((&c->very_dirty_list), rotateby);
  806. }
  807. x = count_list(&c->dirty_list);
  808. if (x) {
  809. rotateby = pseudo_random % x;
  810. rotate_list((&c->dirty_list), rotateby);
  811. }
  812. x = count_list(&c->erasable_list);
  813. if (x) {
  814. rotateby = pseudo_random % x;
  815. rotate_list((&c->erasable_list), rotateby);
  816. }
  817. if (c->nr_erasing_blocks) {
  818. rotateby = pseudo_random % c->nr_erasing_blocks;
  819. rotate_list((&c->erase_pending_list), rotateby);
  820. }
  821. if (c->nr_free_blocks) {
  822. rotateby = pseudo_random % c->nr_free_blocks;
  823. rotate_list((&c->free_list), rotateby);
  824. }
  825. }