scan.c 36 KB

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