scan.c 35 KB

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