scan.c 30 KB

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