scan.c 35 KB

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