scan.c 35 KB

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