scan.c 35 KB

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