scan.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/kernel.h>
  13. #include <linux/sched.h>
  14. #include <linux/slab.h>
  15. #include <linux/mtd/mtd.h>
  16. #include <linux/pagemap.h>
  17. #include <linux/crc32.h>
  18. #include <linux/compiler.h>
  19. #include "nodelist.h"
  20. #include "summary.h"
  21. #include "debug.h"
  22. #define DEFAULT_EMPTY_SCAN_SIZE 256
  23. #define noisy_printk(noise, fmt, ...) \
  24. do { \
  25. if (*(noise)) { \
  26. pr_notice(fmt, ##__VA_ARGS__); \
  27. (*(noise))--; \
  28. if (!(*(noise))) \
  29. pr_notice("Further such events for this erase block will not be printed\n"); \
  30. } \
  31. } while (0)
  32. static uint32_t pseudo_random;
  33. static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
  34. unsigned char *buf, uint32_t buf_size, struct jffs2_summary *s);
  35. /* These helper functions _must_ increase ofs and also do the dirty/used space accounting.
  36. * Returning an error will abort the mount - bad checksums etc. should just mark the space
  37. * as dirty.
  38. */
  39. static int jffs2_scan_inode_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
  40. struct jffs2_raw_inode *ri, uint32_t ofs, struct jffs2_summary *s);
  41. static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
  42. struct jffs2_raw_dirent *rd, uint32_t ofs, struct jffs2_summary *s);
  43. static inline int min_free(struct jffs2_sb_info *c)
  44. {
  45. uint32_t min = 2 * sizeof(struct jffs2_raw_inode);
  46. #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
  47. if (!jffs2_can_mark_obsolete(c) && min < c->wbuf_pagesize)
  48. return c->wbuf_pagesize;
  49. #endif
  50. return min;
  51. }
  52. static inline uint32_t EMPTY_SCAN_SIZE(uint32_t sector_size) {
  53. if (sector_size < DEFAULT_EMPTY_SCAN_SIZE)
  54. return sector_size;
  55. else
  56. return DEFAULT_EMPTY_SCAN_SIZE;
  57. }
  58. static int file_dirty(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
  59. {
  60. int ret;
  61. if ((ret = jffs2_prealloc_raw_node_refs(c, jeb, 1)))
  62. return ret;
  63. if ((ret = jffs2_scan_dirty_space(c, jeb, jeb->free_size)))
  64. return ret;
  65. /* Turned wasted size into dirty, since we apparently
  66. think it's recoverable now. */
  67. jeb->dirty_size += jeb->wasted_size;
  68. c->dirty_size += jeb->wasted_size;
  69. c->wasted_size -= jeb->wasted_size;
  70. jeb->wasted_size = 0;
  71. if (VERYDIRTY(c, jeb->dirty_size)) {
  72. list_add(&jeb->list, &c->very_dirty_list);
  73. } else {
  74. list_add(&jeb->list, &c->dirty_list);
  75. }
  76. return 0;
  77. }
  78. int jffs2_scan_medium(struct jffs2_sb_info *c)
  79. {
  80. int i, ret;
  81. uint32_t empty_blocks = 0, bad_blocks = 0;
  82. unsigned char *flashbuf = NULL;
  83. uint32_t buf_size = 0;
  84. struct jffs2_summary *s = NULL; /* summary info collected by the scan process */
  85. #ifndef __ECOS
  86. size_t pointlen, try_size;
  87. ret = mtd_point(c->mtd, 0, c->mtd->size, &pointlen,
  88. (void **)&flashbuf, NULL);
  89. if (!ret && pointlen < c->mtd->size) {
  90. /* Don't muck about if it won't let us point to the whole flash */
  91. jffs2_dbg(1, "MTD point returned len too short: 0x%zx\n",
  92. pointlen);
  93. mtd_unpoint(c->mtd, 0, pointlen);
  94. flashbuf = NULL;
  95. }
  96. if (ret && ret != -EOPNOTSUPP)
  97. jffs2_dbg(1, "MTD point failed %d\n", ret);
  98. #endif
  99. if (!flashbuf) {
  100. /* For NAND it's quicker to read a whole eraseblock at a time,
  101. apparently */
  102. if (jffs2_cleanmarker_oob(c))
  103. try_size = c->sector_size;
  104. else
  105. try_size = PAGE_SIZE;
  106. jffs2_dbg(1, "Trying to allocate readbuf of %zu "
  107. "bytes\n", try_size);
  108. flashbuf = mtd_kmalloc_up_to(c->mtd, &try_size);
  109. if (!flashbuf)
  110. return -ENOMEM;
  111. jffs2_dbg(1, "Allocated readbuf of %zu bytes\n",
  112. try_size);
  113. buf_size = (uint32_t)try_size;
  114. }
  115. if (jffs2_sum_active()) {
  116. s = kzalloc(sizeof(struct jffs2_summary), GFP_KERNEL);
  117. if (!s) {
  118. JFFS2_WARNING("Can't allocate memory for summary\n");
  119. ret = -ENOMEM;
  120. goto out;
  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. jffs2_dbg(1, "Adding all-dirty block at 0x%08x to erase_pending_list\n",
  156. jeb->offset);
  157. list_add(&jeb->list, &c->erase_pending_list);
  158. c->nr_erasing_blocks++;
  159. }
  160. break;
  161. case BLK_STATE_CLEAN:
  162. /* Full (or almost full) of clean data. Clean list */
  163. list_add(&jeb->list, &c->clean_list);
  164. break;
  165. case BLK_STATE_PARTDIRTY:
  166. /* Some data, but not full. Dirty list. */
  167. /* We want to remember the block with most free space
  168. and stick it in the 'nextblock' position to start writing to it. */
  169. if (jeb->free_size > min_free(c) &&
  170. (!c->nextblock || c->nextblock->free_size < jeb->free_size)) {
  171. /* Better candidate for the next writes to go to */
  172. if (c->nextblock) {
  173. ret = file_dirty(c, c->nextblock);
  174. if (ret)
  175. goto out;
  176. /* deleting summary information of the old nextblock */
  177. jffs2_sum_reset_collected(c->summary);
  178. }
  179. /* update collected summary information for the current nextblock */
  180. jffs2_sum_move_collected(c, s);
  181. jffs2_dbg(1, "%s(): new nextblock = 0x%08x\n",
  182. __func__, jeb->offset);
  183. c->nextblock = jeb;
  184. } else {
  185. ret = file_dirty(c, jeb);
  186. if (ret)
  187. goto out;
  188. }
  189. break;
  190. case BLK_STATE_ALLDIRTY:
  191. /* Nothing valid - not even a clean marker. Needs erasing. */
  192. /* For now we just put it on the erasing list. We'll start the erases later */
  193. jffs2_dbg(1, "Erase block at 0x%08x is not formatted. It will be erased\n",
  194. jeb->offset);
  195. list_add(&jeb->list, &c->erase_pending_list);
  196. c->nr_erasing_blocks++;
  197. break;
  198. case BLK_STATE_BADBLOCK:
  199. jffs2_dbg(1, "Block at 0x%08x is bad\n", 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. /* sm->offset maybe wrong but MAGIC maybe right */
  447. if (sumlen > c->sector_size)
  448. goto full_scan;
  449. /* Now, make sure the summary itself is available */
  450. if (sumlen > buf_size) {
  451. /* Need to kmalloc for this. */
  452. sumptr = kmalloc(sumlen, GFP_KERNEL);
  453. if (!sumptr)
  454. return -ENOMEM;
  455. memcpy(sumptr + sumlen - buf_len, buf + buf_size - buf_len, buf_len);
  456. }
  457. if (buf_len < sumlen) {
  458. /* Need to read more so that the entire summary node is present */
  459. err = jffs2_fill_scan_buf(c, sumptr,
  460. jeb->offset + c->sector_size - sumlen,
  461. sumlen - buf_len);
  462. if (err)
  463. return err;
  464. }
  465. }
  466. }
  467. if (sumptr) {
  468. err = jffs2_sum_scan_sumnode(c, jeb, sumptr, sumlen, &pseudo_random);
  469. if (buf_size && sumlen > buf_size)
  470. kfree(sumptr);
  471. /* If it returns with a real error, bail.
  472. If it returns positive, that's a block classification
  473. (i.e. BLK_STATE_xxx) so return that too.
  474. If it returns zero, fall through to full scan. */
  475. if (err)
  476. return err;
  477. }
  478. }
  479. full_scan:
  480. buf_ofs = jeb->offset;
  481. if (!buf_size) {
  482. /* This is the XIP case -- we're reading _directly_ from the flash chip */
  483. buf_len = c->sector_size;
  484. } else {
  485. buf_len = EMPTY_SCAN_SIZE(c->sector_size);
  486. err = jffs2_fill_scan_buf(c, buf, buf_ofs, buf_len);
  487. if (err)
  488. return err;
  489. }
  490. /* We temporarily use 'ofs' as a pointer into the buffer/jeb */
  491. ofs = 0;
  492. max_ofs = EMPTY_SCAN_SIZE(c->sector_size);
  493. /* Scan only EMPTY_SCAN_SIZE of 0xFF before declaring it's empty */
  494. while(ofs < max_ofs && *(uint32_t *)(&buf[ofs]) == 0xFFFFFFFF)
  495. ofs += 4;
  496. if (ofs == max_ofs) {
  497. #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
  498. if (jffs2_cleanmarker_oob(c)) {
  499. /* scan oob, take care of cleanmarker */
  500. int ret = jffs2_check_oob_empty(c, jeb, cleanmarkerfound);
  501. jffs2_dbg(2, "jffs2_check_oob_empty returned %d\n",
  502. ret);
  503. switch (ret) {
  504. case 0: return cleanmarkerfound ? BLK_STATE_CLEANMARKER : BLK_STATE_ALLFF;
  505. case 1: return BLK_STATE_ALLDIRTY;
  506. default: return ret;
  507. }
  508. }
  509. #endif
  510. jffs2_dbg(1, "Block at 0x%08x is empty (erased)\n",
  511. jeb->offset);
  512. if (c->cleanmarker_size == 0)
  513. return BLK_STATE_CLEANMARKER; /* don't bother with re-erase */
  514. else
  515. return BLK_STATE_ALLFF; /* OK to erase if all blocks are like this */
  516. }
  517. if (ofs) {
  518. jffs2_dbg(1, "Free space at %08x ends at %08x\n", jeb->offset,
  519. jeb->offset + ofs);
  520. if ((err = jffs2_prealloc_raw_node_refs(c, jeb, 1)))
  521. return err;
  522. if ((err = jffs2_scan_dirty_space(c, jeb, ofs)))
  523. return err;
  524. }
  525. /* Now ofs is a complete physical flash offset as it always was... */
  526. ofs += jeb->offset;
  527. noise = 10;
  528. dbg_summary("no summary found in jeb 0x%08x. Apply original scan.\n",jeb->offset);
  529. scan_more:
  530. while(ofs < jeb->offset + c->sector_size) {
  531. jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
  532. /* Make sure there are node refs available for use */
  533. err = jffs2_prealloc_raw_node_refs(c, jeb, 2);
  534. if (err)
  535. return err;
  536. cond_resched();
  537. if (ofs & 3) {
  538. pr_warn("Eep. ofs 0x%08x not word-aligned!\n", ofs);
  539. ofs = PAD(ofs);
  540. continue;
  541. }
  542. if (ofs == prevofs) {
  543. pr_warn("ofs 0x%08x has already been seen. Skipping\n",
  544. ofs);
  545. if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
  546. return err;
  547. ofs += 4;
  548. continue;
  549. }
  550. prevofs = ofs;
  551. if (jeb->offset + c->sector_size < ofs + sizeof(*node)) {
  552. jffs2_dbg(1, "Fewer than %zd bytes left to end of block. (%x+%x<%x+%zx) Not reading\n",
  553. sizeof(struct jffs2_unknown_node),
  554. jeb->offset, c->sector_size, ofs,
  555. sizeof(*node));
  556. if ((err = jffs2_scan_dirty_space(c, jeb, (jeb->offset + c->sector_size)-ofs)))
  557. return err;
  558. break;
  559. }
  560. if (buf_ofs + buf_len < ofs + sizeof(*node)) {
  561. buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
  562. jffs2_dbg(1, "Fewer than %zd bytes (node header) left to end of buf. Reading 0x%x at 0x%08x\n",
  563. sizeof(struct jffs2_unknown_node),
  564. buf_len, ofs);
  565. err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
  566. if (err)
  567. return err;
  568. buf_ofs = ofs;
  569. }
  570. node = (struct jffs2_unknown_node *)&buf[ofs-buf_ofs];
  571. if (*(uint32_t *)(&buf[ofs-buf_ofs]) == 0xffffffff) {
  572. uint32_t inbuf_ofs;
  573. uint32_t empty_start, scan_end;
  574. empty_start = ofs;
  575. ofs += 4;
  576. scan_end = min_t(uint32_t, EMPTY_SCAN_SIZE(c->sector_size)/8, buf_len);
  577. jffs2_dbg(1, "Found empty flash at 0x%08x\n", ofs);
  578. more_empty:
  579. inbuf_ofs = ofs - buf_ofs;
  580. while (inbuf_ofs < scan_end) {
  581. if (unlikely(*(uint32_t *)(&buf[inbuf_ofs]) != 0xffffffff)) {
  582. pr_warn("Empty flash at 0x%08x ends at 0x%08x\n",
  583. empty_start, ofs);
  584. if ((err = jffs2_scan_dirty_space(c, jeb, ofs-empty_start)))
  585. return err;
  586. goto scan_more;
  587. }
  588. inbuf_ofs+=4;
  589. ofs += 4;
  590. }
  591. /* Ran off end. */
  592. jffs2_dbg(1, "Empty flash to end of buffer at 0x%08x\n",
  593. ofs);
  594. /* If we're only checking the beginning of a block with a cleanmarker,
  595. bail now */
  596. if (buf_ofs == jeb->offset && jeb->used_size == PAD(c->cleanmarker_size) &&
  597. c->cleanmarker_size && !jeb->dirty_size && !ref_next(jeb->first_node)) {
  598. jffs2_dbg(1, "%d bytes at start of block seems clean... assuming all clean\n",
  599. EMPTY_SCAN_SIZE(c->sector_size));
  600. return BLK_STATE_CLEANMARKER;
  601. }
  602. if (!buf_size && (scan_end != buf_len)) {/* XIP/point case */
  603. scan_end = buf_len;
  604. goto more_empty;
  605. }
  606. /* See how much more there is to read in this eraseblock... */
  607. buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
  608. if (!buf_len) {
  609. /* No more to read. Break out of main loop without marking
  610. this range of empty space as dirty (because it's not) */
  611. jffs2_dbg(1, "Empty flash at %08x runs to end of block. Treating as free_space\n",
  612. empty_start);
  613. break;
  614. }
  615. /* point never reaches here */
  616. scan_end = buf_len;
  617. jffs2_dbg(1, "Reading another 0x%x at 0x%08x\n",
  618. buf_len, ofs);
  619. err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
  620. if (err)
  621. return err;
  622. buf_ofs = ofs;
  623. goto more_empty;
  624. }
  625. if (ofs == jeb->offset && je16_to_cpu(node->magic) == KSAMTIB_CIGAM_2SFFJ) {
  626. pr_warn("Magic bitmask is backwards at offset 0x%08x. Wrong endian filesystem?\n",
  627. ofs);
  628. if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
  629. return err;
  630. ofs += 4;
  631. continue;
  632. }
  633. if (je16_to_cpu(node->magic) == JFFS2_DIRTY_BITMASK) {
  634. jffs2_dbg(1, "Dirty bitmask at 0x%08x\n", ofs);
  635. if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
  636. return err;
  637. ofs += 4;
  638. continue;
  639. }
  640. if (je16_to_cpu(node->magic) == JFFS2_OLD_MAGIC_BITMASK) {
  641. pr_warn("Old JFFS2 bitmask found at 0x%08x\n", ofs);
  642. pr_warn("You cannot use older JFFS2 filesystems with newer kernels\n");
  643. if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
  644. return err;
  645. ofs += 4;
  646. continue;
  647. }
  648. if (je16_to_cpu(node->magic) != JFFS2_MAGIC_BITMASK) {
  649. /* OK. We're out of possibilities. Whinge and move on */
  650. noisy_printk(&noise, "%s(): Magic bitmask 0x%04x not found at 0x%08x: 0x%04x instead\n",
  651. __func__,
  652. JFFS2_MAGIC_BITMASK, ofs,
  653. je16_to_cpu(node->magic));
  654. if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
  655. return err;
  656. ofs += 4;
  657. continue;
  658. }
  659. /* We seem to have a node of sorts. Check the CRC */
  660. crcnode.magic = node->magic;
  661. crcnode.nodetype = cpu_to_je16( je16_to_cpu(node->nodetype) | JFFS2_NODE_ACCURATE);
  662. crcnode.totlen = node->totlen;
  663. hdr_crc = crc32(0, &crcnode, sizeof(crcnode)-4);
  664. if (hdr_crc != je32_to_cpu(node->hdr_crc)) {
  665. noisy_printk(&noise, "%s(): Node at 0x%08x {0x%04x, 0x%04x, 0x%08x) has invalid CRC 0x%08x (calculated 0x%08x)\n",
  666. __func__,
  667. ofs, je16_to_cpu(node->magic),
  668. je16_to_cpu(node->nodetype),
  669. je32_to_cpu(node->totlen),
  670. je32_to_cpu(node->hdr_crc),
  671. hdr_crc);
  672. if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
  673. return err;
  674. ofs += 4;
  675. continue;
  676. }
  677. if (ofs + je32_to_cpu(node->totlen) > jeb->offset + c->sector_size) {
  678. /* Eep. Node goes over the end of the erase block. */
  679. pr_warn("Node at 0x%08x with length 0x%08x would run over the end of the erase block\n",
  680. ofs, je32_to_cpu(node->totlen));
  681. pr_warn("Perhaps the file system was created with the wrong erase size?\n");
  682. if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
  683. return err;
  684. ofs += 4;
  685. continue;
  686. }
  687. if (!(je16_to_cpu(node->nodetype) & JFFS2_NODE_ACCURATE)) {
  688. /* Wheee. This is an obsoleted node */
  689. jffs2_dbg(2, "Node at 0x%08x is obsolete. Skipping\n",
  690. ofs);
  691. if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen)))))
  692. return err;
  693. ofs += PAD(je32_to_cpu(node->totlen));
  694. continue;
  695. }
  696. switch(je16_to_cpu(node->nodetype)) {
  697. case JFFS2_NODETYPE_INODE:
  698. if (buf_ofs + buf_len < ofs + sizeof(struct jffs2_raw_inode)) {
  699. buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
  700. jffs2_dbg(1, "Fewer than %zd bytes (inode node) left to end of buf. Reading 0x%x at 0x%08x\n",
  701. sizeof(struct jffs2_raw_inode),
  702. 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_inode_node(c, jeb, (void *)node, ofs, s);
  710. if (err) return err;
  711. ofs += PAD(je32_to_cpu(node->totlen));
  712. break;
  713. case JFFS2_NODETYPE_DIRENT:
  714. if (buf_ofs + buf_len < ofs + je32_to_cpu(node->totlen)) {
  715. buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
  716. jffs2_dbg(1, "Fewer than %d bytes (dirent node) left to end of buf. Reading 0x%x at 0x%08x\n",
  717. je32_to_cpu(node->totlen), buf_len,
  718. ofs);
  719. err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
  720. if (err)
  721. return err;
  722. buf_ofs = ofs;
  723. node = (void *)buf;
  724. }
  725. err = jffs2_scan_dirent_node(c, jeb, (void *)node, ofs, s);
  726. if (err) return err;
  727. ofs += PAD(je32_to_cpu(node->totlen));
  728. break;
  729. #ifdef CONFIG_JFFS2_FS_XATTR
  730. case JFFS2_NODETYPE_XATTR:
  731. if (buf_ofs + buf_len < ofs + je32_to_cpu(node->totlen)) {
  732. buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
  733. jffs2_dbg(1, "Fewer than %d bytes (xattr node) left to end of buf. Reading 0x%x at 0x%08x\n",
  734. je32_to_cpu(node->totlen), buf_len,
  735. ofs);
  736. err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
  737. if (err)
  738. return err;
  739. buf_ofs = ofs;
  740. node = (void *)buf;
  741. }
  742. err = jffs2_scan_xattr_node(c, jeb, (void *)node, ofs, s);
  743. if (err)
  744. return err;
  745. ofs += PAD(je32_to_cpu(node->totlen));
  746. break;
  747. case JFFS2_NODETYPE_XREF:
  748. if (buf_ofs + buf_len < ofs + je32_to_cpu(node->totlen)) {
  749. buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
  750. jffs2_dbg(1, "Fewer than %d bytes (xref node) left to end of buf. Reading 0x%x at 0x%08x\n",
  751. je32_to_cpu(node->totlen), buf_len,
  752. ofs);
  753. err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
  754. if (err)
  755. return err;
  756. buf_ofs = ofs;
  757. node = (void *)buf;
  758. }
  759. err = jffs2_scan_xref_node(c, jeb, (void *)node, ofs, s);
  760. if (err)
  761. return err;
  762. ofs += PAD(je32_to_cpu(node->totlen));
  763. break;
  764. #endif /* CONFIG_JFFS2_FS_XATTR */
  765. case JFFS2_NODETYPE_CLEANMARKER:
  766. jffs2_dbg(1, "CLEANMARKER node found at 0x%08x\n", ofs);
  767. if (je32_to_cpu(node->totlen) != c->cleanmarker_size) {
  768. pr_notice("CLEANMARKER node found at 0x%08x has totlen 0x%x != normal 0x%x\n",
  769. ofs, je32_to_cpu(node->totlen),
  770. c->cleanmarker_size);
  771. if ((err = jffs2_scan_dirty_space(c, jeb, PAD(sizeof(struct jffs2_unknown_node)))))
  772. return err;
  773. ofs += PAD(sizeof(struct jffs2_unknown_node));
  774. } else if (jeb->first_node) {
  775. pr_notice("CLEANMARKER node found at 0x%08x, not first node in block (0x%08x)\n",
  776. ofs, jeb->offset);
  777. if ((err = jffs2_scan_dirty_space(c, jeb, PAD(sizeof(struct jffs2_unknown_node)))))
  778. return err;
  779. ofs += PAD(sizeof(struct jffs2_unknown_node));
  780. } else {
  781. jffs2_link_node_ref(c, jeb, ofs | REF_NORMAL, c->cleanmarker_size, NULL);
  782. ofs += PAD(c->cleanmarker_size);
  783. }
  784. break;
  785. case JFFS2_NODETYPE_PADDING:
  786. if (jffs2_sum_active())
  787. jffs2_sum_add_padding_mem(s, je32_to_cpu(node->totlen));
  788. if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen)))))
  789. return err;
  790. ofs += PAD(je32_to_cpu(node->totlen));
  791. break;
  792. default:
  793. switch (je16_to_cpu(node->nodetype) & JFFS2_COMPAT_MASK) {
  794. case JFFS2_FEATURE_ROCOMPAT:
  795. pr_notice("Read-only compatible feature node (0x%04x) found at offset 0x%08x\n",
  796. je16_to_cpu(node->nodetype), ofs);
  797. c->flags |= JFFS2_SB_FLAG_RO;
  798. if (!(jffs2_is_readonly(c)))
  799. return -EROFS;
  800. if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen)))))
  801. return err;
  802. ofs += PAD(je32_to_cpu(node->totlen));
  803. break;
  804. case JFFS2_FEATURE_INCOMPAT:
  805. pr_notice("Incompatible feature node (0x%04x) found at offset 0x%08x\n",
  806. je16_to_cpu(node->nodetype), ofs);
  807. return -EINVAL;
  808. case JFFS2_FEATURE_RWCOMPAT_DELETE:
  809. jffs2_dbg(1, "Unknown but compatible feature node (0x%04x) found at offset 0x%08x\n",
  810. je16_to_cpu(node->nodetype), ofs);
  811. if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen)))))
  812. return err;
  813. ofs += PAD(je32_to_cpu(node->totlen));
  814. break;
  815. case JFFS2_FEATURE_RWCOMPAT_COPY: {
  816. jffs2_dbg(1, "Unknown but compatible feature node (0x%04x) found at offset 0x%08x\n",
  817. je16_to_cpu(node->nodetype), ofs);
  818. jffs2_link_node_ref(c, jeb, ofs | REF_PRISTINE, PAD(je32_to_cpu(node->totlen)), NULL);
  819. /* We can't summarise nodes we don't grok */
  820. jffs2_sum_disable_collecting(s);
  821. ofs += PAD(je32_to_cpu(node->totlen));
  822. break;
  823. }
  824. }
  825. }
  826. }
  827. if (jffs2_sum_active()) {
  828. if (PAD(s->sum_size + JFFS2_SUMMARY_FRAME_SIZE) > jeb->free_size) {
  829. dbg_summary("There is not enough space for "
  830. "summary information, disabling for this jeb!\n");
  831. jffs2_sum_disable_collecting(s);
  832. }
  833. }
  834. jffs2_dbg(1, "Block at 0x%08x: free 0x%08x, dirty 0x%08x, unchecked 0x%08x, used 0x%08x, wasted 0x%08x\n",
  835. jeb->offset, jeb->free_size, jeb->dirty_size,
  836. jeb->unchecked_size, jeb->used_size, jeb->wasted_size);
  837. /* mark_node_obsolete can add to wasted !! */
  838. if (jeb->wasted_size) {
  839. jeb->dirty_size += jeb->wasted_size;
  840. c->dirty_size += jeb->wasted_size;
  841. c->wasted_size -= jeb->wasted_size;
  842. jeb->wasted_size = 0;
  843. }
  844. return jffs2_scan_classify_jeb(c, jeb);
  845. }
  846. struct jffs2_inode_cache *jffs2_scan_make_ino_cache(struct jffs2_sb_info *c, uint32_t ino)
  847. {
  848. struct jffs2_inode_cache *ic;
  849. ic = jffs2_get_ino_cache(c, ino);
  850. if (ic)
  851. return ic;
  852. if (ino > c->highest_ino)
  853. c->highest_ino = ino;
  854. ic = jffs2_alloc_inode_cache();
  855. if (!ic) {
  856. pr_notice("%s(): allocation of inode cache failed\n", __func__);
  857. return NULL;
  858. }
  859. memset(ic, 0, sizeof(*ic));
  860. ic->ino = ino;
  861. ic->nodes = (void *)ic;
  862. jffs2_add_ino_cache(c, ic);
  863. if (ino == 1)
  864. ic->pino_nlink = 1;
  865. return ic;
  866. }
  867. static int jffs2_scan_inode_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
  868. struct jffs2_raw_inode *ri, uint32_t ofs, struct jffs2_summary *s)
  869. {
  870. struct jffs2_inode_cache *ic;
  871. uint32_t crc, ino = je32_to_cpu(ri->ino);
  872. jffs2_dbg(1, "%s(): Node at 0x%08x\n", __func__, ofs);
  873. /* We do very little here now. Just check the ino# to which we should attribute
  874. this node; we can do all the CRC checking etc. later. There's a tradeoff here --
  875. we used to scan the flash once only, reading everything we want from it into
  876. memory, then building all our in-core data structures and freeing the extra
  877. information. Now we allow the first part of the mount to complete a lot quicker,
  878. but we have to go _back_ to the flash in order to finish the CRC checking, etc.
  879. Which means that the _full_ amount of time to get to proper write mode with GC
  880. operational may actually be _longer_ than before. Sucks to be me. */
  881. /* Check the node CRC in any case. */
  882. crc = crc32(0, ri, sizeof(*ri)-8);
  883. if (crc != je32_to_cpu(ri->node_crc)) {
  884. pr_notice("%s(): CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
  885. __func__, ofs, je32_to_cpu(ri->node_crc), crc);
  886. /*
  887. * We believe totlen because the CRC on the node
  888. * _header_ was OK, just the node itself failed.
  889. */
  890. return jffs2_scan_dirty_space(c, jeb,
  891. PAD(je32_to_cpu(ri->totlen)));
  892. }
  893. ic = jffs2_get_ino_cache(c, ino);
  894. if (!ic) {
  895. ic = jffs2_scan_make_ino_cache(c, ino);
  896. if (!ic)
  897. return -ENOMEM;
  898. }
  899. /* Wheee. It worked */
  900. jffs2_link_node_ref(c, jeb, ofs | REF_UNCHECKED, PAD(je32_to_cpu(ri->totlen)), ic);
  901. jffs2_dbg(1, "Node is ino #%u, version %d. Range 0x%x-0x%x\n",
  902. je32_to_cpu(ri->ino), je32_to_cpu(ri->version),
  903. je32_to_cpu(ri->offset),
  904. je32_to_cpu(ri->offset)+je32_to_cpu(ri->dsize));
  905. pseudo_random += je32_to_cpu(ri->version);
  906. if (jffs2_sum_active()) {
  907. jffs2_sum_add_inode_mem(s, ri, ofs - jeb->offset);
  908. }
  909. return 0;
  910. }
  911. static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
  912. struct jffs2_raw_dirent *rd, uint32_t ofs, struct jffs2_summary *s)
  913. {
  914. struct jffs2_full_dirent *fd;
  915. struct jffs2_inode_cache *ic;
  916. uint32_t checkedlen;
  917. uint32_t crc;
  918. int err;
  919. jffs2_dbg(1, "%s(): Node at 0x%08x\n", __func__, ofs);
  920. /* We don't get here unless the node is still valid, so we don't have to
  921. mask in the ACCURATE bit any more. */
  922. crc = crc32(0, rd, sizeof(*rd)-8);
  923. if (crc != je32_to_cpu(rd->node_crc)) {
  924. pr_notice("%s(): Node CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
  925. __func__, ofs, je32_to_cpu(rd->node_crc), crc);
  926. /* We believe totlen because the CRC on the node _header_ was OK, just the node itself failed. */
  927. if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(rd->totlen)))))
  928. return err;
  929. return 0;
  930. }
  931. pseudo_random += je32_to_cpu(rd->version);
  932. /* Should never happen. Did. (OLPC trac #4184)*/
  933. checkedlen = strnlen(rd->name, rd->nsize);
  934. if (checkedlen < rd->nsize) {
  935. pr_err("Dirent at %08x has zeroes in name. Truncating to %d chars\n",
  936. ofs, checkedlen);
  937. }
  938. fd = jffs2_alloc_full_dirent(checkedlen+1);
  939. if (!fd) {
  940. return -ENOMEM;
  941. }
  942. memcpy(&fd->name, rd->name, checkedlen);
  943. fd->name[checkedlen] = 0;
  944. crc = crc32(0, fd->name, rd->nsize);
  945. if (crc != je32_to_cpu(rd->name_crc)) {
  946. pr_notice("%s(): Name CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
  947. __func__, ofs, je32_to_cpu(rd->name_crc), crc);
  948. jffs2_dbg(1, "Name for which CRC failed is (now) '%s', ino #%d\n",
  949. fd->name, je32_to_cpu(rd->ino));
  950. jffs2_free_full_dirent(fd);
  951. /* FIXME: Why do we believe totlen? */
  952. /* We believe totlen because the CRC on the node _header_ was OK, just the name failed. */
  953. if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(rd->totlen)))))
  954. return err;
  955. return 0;
  956. }
  957. ic = jffs2_scan_make_ino_cache(c, je32_to_cpu(rd->pino));
  958. if (!ic) {
  959. jffs2_free_full_dirent(fd);
  960. return -ENOMEM;
  961. }
  962. fd->raw = jffs2_link_node_ref(c, jeb, ofs | dirent_node_state(rd),
  963. PAD(je32_to_cpu(rd->totlen)), ic);
  964. fd->next = NULL;
  965. fd->version = je32_to_cpu(rd->version);
  966. fd->ino = je32_to_cpu(rd->ino);
  967. fd->nhash = full_name_hash(fd->name, checkedlen);
  968. fd->type = rd->type;
  969. jffs2_add_fd_to_list(c, fd, &ic->scan_dents);
  970. if (jffs2_sum_active()) {
  971. jffs2_sum_add_dirent_mem(s, rd, ofs - jeb->offset);
  972. }
  973. return 0;
  974. }
  975. static int count_list(struct list_head *l)
  976. {
  977. uint32_t count = 0;
  978. struct list_head *tmp;
  979. list_for_each(tmp, l) {
  980. count++;
  981. }
  982. return count;
  983. }
  984. /* Note: This breaks if list_empty(head). I don't care. You
  985. might, if you copy this code and use it elsewhere :) */
  986. static void rotate_list(struct list_head *head, uint32_t count)
  987. {
  988. struct list_head *n = head->next;
  989. list_del(head);
  990. while(count--) {
  991. n = n->next;
  992. }
  993. list_add(head, n);
  994. }
  995. void jffs2_rotate_lists(struct jffs2_sb_info *c)
  996. {
  997. uint32_t x;
  998. uint32_t rotateby;
  999. x = count_list(&c->clean_list);
  1000. if (x) {
  1001. rotateby = pseudo_random % x;
  1002. rotate_list((&c->clean_list), rotateby);
  1003. }
  1004. x = count_list(&c->very_dirty_list);
  1005. if (x) {
  1006. rotateby = pseudo_random % x;
  1007. rotate_list((&c->very_dirty_list), rotateby);
  1008. }
  1009. x = count_list(&c->dirty_list);
  1010. if (x) {
  1011. rotateby = pseudo_random % x;
  1012. rotate_list((&c->dirty_list), rotateby);
  1013. }
  1014. x = count_list(&c->erasable_list);
  1015. if (x) {
  1016. rotateby = pseudo_random % x;
  1017. rotate_list((&c->erasable_list), rotateby);
  1018. }
  1019. if (c->nr_erasing_blocks) {
  1020. rotateby = pseudo_random % c->nr_erasing_blocks;
  1021. rotate_list((&c->erase_pending_list), rotateby);
  1022. }
  1023. if (c->nr_free_blocks) {
  1024. rotateby = pseudo_random % c->nr_free_blocks;
  1025. rotate_list((&c->free_list), rotateby);
  1026. }
  1027. }