scan.c 35 KB

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