scan.c 35 KB

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