scan.c 35 KB

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