scan.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  1. /*
  2. * JFFS2 -- Journalling Flash File System, Version 2.
  3. *
  4. * Copyright © 2001-2007 Red Hat, Inc.
  5. *
  6. * Created by David Woodhouse <dwmw2@infradead.org>
  7. *
  8. * For licensing information, see the file 'LICENCE' in this directory.
  9. *
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/sched.h>
  13. #include <linux/slab.h>
  14. #include <linux/mtd/mtd.h>
  15. #include <linux/pagemap.h>
  16. #include <linux/crc32.h>
  17. #include <linux/compiler.h>
  18. #include "nodelist.h"
  19. #include "summary.h"
  20. #include "debug.h"
  21. #define DEFAULT_EMPTY_SCAN_SIZE 1024
  22. #define noisy_printk(noise, args...) do { \
  23. if (*(noise)) { \
  24. printk(KERN_NOTICE args); \
  25. (*(noise))--; \
  26. if (!(*(noise))) { \
  27. printk(KERN_NOTICE "Further such events for this erase block will not be printed\n"); \
  28. } \
  29. } \
  30. } while(0)
  31. static uint32_t pseudo_random;
  32. static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
  33. unsigned char *buf, uint32_t buf_size, struct jffs2_summary *s);
  34. /* These helper functions _must_ increase ofs and also do the dirty/used space accounting.
  35. * Returning an error will abort the mount - bad checksums etc. should just mark the space
  36. * as dirty.
  37. */
  38. static int jffs2_scan_inode_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
  39. struct jffs2_raw_inode *ri, uint32_t ofs, struct jffs2_summary *s);
  40. static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
  41. struct jffs2_raw_dirent *rd, uint32_t ofs, struct jffs2_summary *s);
  42. static inline int min_free(struct jffs2_sb_info *c)
  43. {
  44. uint32_t min = 2 * sizeof(struct jffs2_raw_inode);
  45. #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
  46. if (!jffs2_can_mark_obsolete(c) && min < c->wbuf_pagesize)
  47. return c->wbuf_pagesize;
  48. #endif
  49. return min;
  50. }
  51. static inline uint32_t EMPTY_SCAN_SIZE(uint32_t sector_size) {
  52. if (sector_size < DEFAULT_EMPTY_SCAN_SIZE)
  53. return sector_size;
  54. else
  55. return DEFAULT_EMPTY_SCAN_SIZE;
  56. }
  57. static int file_dirty(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
  58. {
  59. int ret;
  60. if ((ret = jffs2_prealloc_raw_node_refs(c, jeb, 1)))
  61. return ret;
  62. if ((ret = jffs2_scan_dirty_space(c, jeb, jeb->free_size)))
  63. return ret;
  64. /* Turned wasted size into dirty, since we apparently
  65. think it's recoverable now. */
  66. jeb->dirty_size += jeb->wasted_size;
  67. c->dirty_size += jeb->wasted_size;
  68. c->wasted_size -= jeb->wasted_size;
  69. jeb->wasted_size = 0;
  70. if (VERYDIRTY(c, jeb->dirty_size)) {
  71. list_add(&jeb->list, &c->very_dirty_list);
  72. } else {
  73. list_add(&jeb->list, &c->dirty_list);
  74. }
  75. return 0;
  76. }
  77. int jffs2_scan_medium(struct jffs2_sb_info *c)
  78. {
  79. int i, ret;
  80. uint32_t empty_blocks = 0, bad_blocks = 0;
  81. unsigned char *flashbuf = NULL;
  82. uint32_t buf_size = 0;
  83. struct jffs2_summary *s = NULL; /* summary info collected by the scan process */
  84. #ifndef __ECOS
  85. size_t pointlen;
  86. if (c->mtd->point) {
  87. ret = c->mtd->point(c->mtd, 0, c->mtd->size, &pointlen,
  88. (void **)&flashbuf, NULL);
  89. if (!ret && pointlen < c->mtd->size) {
  90. /* Don't muck about if it won't let us point to the whole flash */
  91. D1(printk(KERN_DEBUG "MTD point returned len too short: 0x%zx\n", pointlen));
  92. c->mtd->unpoint(c->mtd, 0, pointlen);
  93. flashbuf = NULL;
  94. }
  95. if (ret)
  96. D1(printk(KERN_DEBUG "MTD point failed %d\n", ret));
  97. }
  98. #endif
  99. if (!flashbuf) {
  100. /* For NAND it's quicker to read a whole eraseblock at a time,
  101. apparently */
  102. if (jffs2_cleanmarker_oob(c))
  103. buf_size = c->sector_size;
  104. else
  105. buf_size = PAGE_SIZE;
  106. /* Respect kmalloc limitations */
  107. if (buf_size > 128*1024)
  108. buf_size = 128*1024;
  109. D1(printk(KERN_DEBUG "Allocating readbuf of %d bytes\n", buf_size));
  110. flashbuf = kmalloc(buf_size, GFP_KERNEL);
  111. if (!flashbuf)
  112. return -ENOMEM;
  113. }
  114. if (jffs2_sum_active()) {
  115. s = kzalloc(sizeof(struct jffs2_summary), GFP_KERNEL);
  116. if (!s) {
  117. kfree(flashbuf);
  118. JFFS2_WARNING("Can't allocate memory for summary\n");
  119. return -ENOMEM;
  120. }
  121. }
  122. for (i=0; i<c->nr_blocks; i++) {
  123. struct jffs2_eraseblock *jeb = &c->blocks[i];
  124. cond_resched();
  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. goto out;
  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. goto out;
  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, 0, c->mtd->size);
  241. #endif
  242. if (s)
  243. kfree(s);
  244. return ret;
  245. }
  246. static int jffs2_fill_scan_buf(struct jffs2_sb_info *c, void *buf,
  247. uint32_t ofs, uint32_t len)
  248. {
  249. int ret;
  250. size_t retlen;
  251. ret = jffs2_flash_read(c, ofs, len, &retlen, buf);
  252. if (ret) {
  253. D1(printk(KERN_WARNING "mtd->read(0x%x bytes from 0x%x) returned %d\n", len, ofs, ret));
  254. return ret;
  255. }
  256. if (retlen < len) {
  257. D1(printk(KERN_WARNING "Read at 0x%x gave only 0x%zx bytes\n", ofs, retlen));
  258. return -EIO;
  259. }
  260. return 0;
  261. }
  262. int jffs2_scan_classify_jeb(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
  263. {
  264. if ((jeb->used_size + jeb->unchecked_size) == PAD(c->cleanmarker_size) && !jeb->dirty_size
  265. && (!jeb->first_node || !ref_next(jeb->first_node)) )
  266. return BLK_STATE_CLEANMARKER;
  267. /* move blocks with max 4 byte dirty space to cleanlist */
  268. else if (!ISDIRTY(c->sector_size - (jeb->used_size + jeb->unchecked_size))) {
  269. c->dirty_size -= jeb->dirty_size;
  270. c->wasted_size += jeb->dirty_size;
  271. jeb->wasted_size += jeb->dirty_size;
  272. jeb->dirty_size = 0;
  273. return BLK_STATE_CLEAN;
  274. } else if (jeb->used_size || jeb->unchecked_size)
  275. return BLK_STATE_PARTDIRTY;
  276. else
  277. return BLK_STATE_ALLDIRTY;
  278. }
  279. #ifdef CONFIG_JFFS2_FS_XATTR
  280. static int jffs2_scan_xattr_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
  281. struct jffs2_raw_xattr *rx, uint32_t ofs,
  282. struct jffs2_summary *s)
  283. {
  284. struct jffs2_xattr_datum *xd;
  285. uint32_t xid, version, totlen, crc;
  286. int err;
  287. crc = crc32(0, rx, sizeof(struct jffs2_raw_xattr) - 4);
  288. if (crc != je32_to_cpu(rx->node_crc)) {
  289. JFFS2_WARNING("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
  290. ofs, je32_to_cpu(rx->node_crc), crc);
  291. if ((err = jffs2_scan_dirty_space(c, jeb, je32_to_cpu(rx->totlen))))
  292. return err;
  293. return 0;
  294. }
  295. xid = je32_to_cpu(rx->xid);
  296. version = je32_to_cpu(rx->version);
  297. totlen = PAD(sizeof(struct jffs2_raw_xattr)
  298. + rx->name_len + 1 + je16_to_cpu(rx->value_len));
  299. if (totlen != je32_to_cpu(rx->totlen)) {
  300. JFFS2_WARNING("node length mismatch at %#08x, read=%u, calc=%u\n",
  301. ofs, je32_to_cpu(rx->totlen), totlen);
  302. if ((err = jffs2_scan_dirty_space(c, jeb, je32_to_cpu(rx->totlen))))
  303. return err;
  304. return 0;
  305. }
  306. xd = jffs2_setup_xattr_datum(c, xid, version);
  307. if (IS_ERR(xd))
  308. return PTR_ERR(xd);
  309. if (xd->version > version) {
  310. struct jffs2_raw_node_ref *raw
  311. = jffs2_link_node_ref(c, jeb, ofs | REF_PRISTINE, totlen, NULL);
  312. raw->next_in_ino = xd->node->next_in_ino;
  313. xd->node->next_in_ino = raw;
  314. } else {
  315. xd->version = version;
  316. xd->xprefix = rx->xprefix;
  317. xd->name_len = rx->name_len;
  318. xd->value_len = je16_to_cpu(rx->value_len);
  319. xd->data_crc = je32_to_cpu(rx->data_crc);
  320. jffs2_link_node_ref(c, jeb, ofs | REF_PRISTINE, totlen, (void *)xd);
  321. }
  322. if (jffs2_sum_active())
  323. jffs2_sum_add_xattr_mem(s, rx, ofs - jeb->offset);
  324. dbg_xattr("scaning xdatum at %#08x (xid=%u, version=%u)\n",
  325. ofs, xd->xid, xd->version);
  326. return 0;
  327. }
  328. static int jffs2_scan_xref_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
  329. struct jffs2_raw_xref *rr, uint32_t ofs,
  330. struct jffs2_summary *s)
  331. {
  332. struct jffs2_xattr_ref *ref;
  333. uint32_t crc;
  334. int err;
  335. crc = crc32(0, rr, sizeof(*rr) - 4);
  336. if (crc != je32_to_cpu(rr->node_crc)) {
  337. JFFS2_WARNING("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
  338. ofs, je32_to_cpu(rr->node_crc), crc);
  339. if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(rr->totlen)))))
  340. return err;
  341. return 0;
  342. }
  343. if (PAD(sizeof(struct jffs2_raw_xref)) != je32_to_cpu(rr->totlen)) {
  344. JFFS2_WARNING("node length mismatch at %#08x, read=%u, calc=%zd\n",
  345. ofs, je32_to_cpu(rr->totlen),
  346. PAD(sizeof(struct jffs2_raw_xref)));
  347. if ((err = jffs2_scan_dirty_space(c, jeb, je32_to_cpu(rr->totlen))))
  348. return err;
  349. return 0;
  350. }
  351. ref = jffs2_alloc_xattr_ref();
  352. if (!ref)
  353. return -ENOMEM;
  354. /* BEFORE jffs2_build_xattr_subsystem() called,
  355. * and AFTER xattr_ref is marked as a dead xref,
  356. * ref->xid is used to store 32bit xid, xd is not used
  357. * ref->ino is used to store 32bit inode-number, ic is not used
  358. * Thoes variables are declared as union, thus using those
  359. * are exclusive. In a similar way, ref->next is temporarily
  360. * used to chain all xattr_ref object. It's re-chained to
  361. * jffs2_inode_cache in jffs2_build_xattr_subsystem() correctly.
  362. */
  363. ref->ino = je32_to_cpu(rr->ino);
  364. ref->xid = je32_to_cpu(rr->xid);
  365. ref->xseqno = je32_to_cpu(rr->xseqno);
  366. if (ref->xseqno > c->highest_xseqno)
  367. c->highest_xseqno = (ref->xseqno & ~XREF_DELETE_MARKER);
  368. ref->next = c->xref_temp;
  369. c->xref_temp = ref;
  370. jffs2_link_node_ref(c, jeb, ofs | REF_PRISTINE, PAD(je32_to_cpu(rr->totlen)), (void *)ref);
  371. if (jffs2_sum_active())
  372. jffs2_sum_add_xref_mem(s, rr, ofs - jeb->offset);
  373. dbg_xattr("scan xref at %#08x (xid=%u, ino=%u)\n",
  374. ofs, ref->xid, ref->ino);
  375. return 0;
  376. }
  377. #endif
  378. /* Called with 'buf_size == 0' if buf is in fact a pointer _directly_ into
  379. the flash, XIP-style */
  380. static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
  381. unsigned char *buf, uint32_t buf_size, struct jffs2_summary *s) {
  382. struct jffs2_unknown_node *node;
  383. struct jffs2_unknown_node crcnode;
  384. uint32_t ofs, prevofs;
  385. uint32_t hdr_crc, buf_ofs, buf_len;
  386. int err;
  387. int noise = 0;
  388. #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
  389. int cleanmarkerfound = 0;
  390. #endif
  391. ofs = jeb->offset;
  392. prevofs = jeb->offset - 1;
  393. D1(printk(KERN_DEBUG "jffs2_scan_eraseblock(): Scanning block at 0x%x\n", ofs));
  394. #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
  395. if (jffs2_cleanmarker_oob(c)) {
  396. int ret;
  397. if (c->mtd->block_isbad(c->mtd, jeb->offset))
  398. return BLK_STATE_BADBLOCK;
  399. ret = jffs2_check_nand_cleanmarker(c, jeb);
  400. D2(printk(KERN_NOTICE "jffs_check_nand_cleanmarker returned %d\n",ret));
  401. /* Even if it's not found, we still scan to see
  402. if the block is empty. We use this information
  403. to decide whether to erase it or not. */
  404. switch (ret) {
  405. case 0: cleanmarkerfound = 1; break;
  406. case 1: break;
  407. default: return ret;
  408. }
  409. }
  410. #endif
  411. if (jffs2_sum_active()) {
  412. struct jffs2_sum_marker *sm;
  413. void *sumptr = NULL;
  414. uint32_t sumlen;
  415. if (!buf_size) {
  416. /* XIP case. Just look, point at the summary if it's there */
  417. sm = (void *)buf + c->sector_size - sizeof(*sm);
  418. if (je32_to_cpu(sm->magic) == JFFS2_SUM_MAGIC) {
  419. sumptr = buf + je32_to_cpu(sm->offset);
  420. sumlen = c->sector_size - je32_to_cpu(sm->offset);
  421. }
  422. } else {
  423. /* If NAND flash, read a whole page of it. Else just the end */
  424. if (c->wbuf_pagesize)
  425. buf_len = c->wbuf_pagesize;
  426. else
  427. buf_len = sizeof(*sm);
  428. /* Read as much as we want into the _end_ of the preallocated buffer */
  429. err = jffs2_fill_scan_buf(c, buf + buf_size - buf_len,
  430. jeb->offset + c->sector_size - buf_len,
  431. buf_len);
  432. if (err)
  433. return err;
  434. sm = (void *)buf + buf_size - sizeof(*sm);
  435. if (je32_to_cpu(sm->magic) == JFFS2_SUM_MAGIC) {
  436. sumlen = c->sector_size - je32_to_cpu(sm->offset);
  437. sumptr = buf + buf_size - sumlen;
  438. /* Now, make sure the summary itself is available */
  439. if (sumlen > buf_size) {
  440. /* Need to kmalloc for this. */
  441. sumptr = kmalloc(sumlen, GFP_KERNEL);
  442. if (!sumptr)
  443. return -ENOMEM;
  444. memcpy(sumptr + sumlen - buf_len, buf + buf_size - buf_len, buf_len);
  445. }
  446. if (buf_len < sumlen) {
  447. /* Need to read more so that the entire summary node is present */
  448. err = jffs2_fill_scan_buf(c, sumptr,
  449. jeb->offset + c->sector_size - sumlen,
  450. sumlen - buf_len);
  451. if (err)
  452. return err;
  453. }
  454. }
  455. }
  456. if (sumptr) {
  457. err = jffs2_sum_scan_sumnode(c, jeb, sumptr, sumlen, &pseudo_random);
  458. if (buf_size && sumlen > buf_size)
  459. kfree(sumptr);
  460. /* If it returns with a real error, bail.
  461. If it returns positive, that's a block classification
  462. (i.e. BLK_STATE_xxx) so return that too.
  463. If it returns zero, fall through to full scan. */
  464. if (err)
  465. return err;
  466. }
  467. }
  468. buf_ofs = jeb->offset;
  469. if (!buf_size) {
  470. /* This is the XIP case -- we're reading _directly_ from the flash chip */
  471. buf_len = c->sector_size;
  472. } else {
  473. buf_len = EMPTY_SCAN_SIZE(c->sector_size);
  474. err = jffs2_fill_scan_buf(c, buf, buf_ofs, buf_len);
  475. if (err)
  476. return err;
  477. }
  478. /* We temporarily use 'ofs' as a pointer into the buffer/jeb */
  479. ofs = 0;
  480. /* Scan only 4KiB of 0xFF before declaring it's empty */
  481. while(ofs < EMPTY_SCAN_SIZE(c->sector_size) && *(uint32_t *)(&buf[ofs]) == 0xFFFFFFFF)
  482. ofs += 4;
  483. if (ofs == EMPTY_SCAN_SIZE(c->sector_size)) {
  484. #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
  485. if (jffs2_cleanmarker_oob(c)) {
  486. /* scan oob, take care of cleanmarker */
  487. int ret = jffs2_check_oob_empty(c, jeb, cleanmarkerfound);
  488. D2(printk(KERN_NOTICE "jffs2_check_oob_empty returned %d\n",ret));
  489. switch (ret) {
  490. case 0: return cleanmarkerfound ? BLK_STATE_CLEANMARKER : BLK_STATE_ALLFF;
  491. case 1: return BLK_STATE_ALLDIRTY;
  492. default: return ret;
  493. }
  494. }
  495. #endif
  496. D1(printk(KERN_DEBUG "Block at 0x%08x is empty (erased)\n", jeb->offset));
  497. if (c->cleanmarker_size == 0)
  498. return BLK_STATE_CLEANMARKER; /* don't bother with re-erase */
  499. else
  500. return BLK_STATE_ALLFF; /* OK to erase if all blocks are like this */
  501. }
  502. if (ofs) {
  503. D1(printk(KERN_DEBUG "Free space at %08x ends at %08x\n", jeb->offset,
  504. jeb->offset + ofs));
  505. if ((err = jffs2_prealloc_raw_node_refs(c, jeb, 1)))
  506. return err;
  507. if ((err = jffs2_scan_dirty_space(c, jeb, ofs)))
  508. return err;
  509. }
  510. /* Now ofs is a complete physical flash offset as it always was... */
  511. ofs += jeb->offset;
  512. noise = 10;
  513. dbg_summary("no summary found in jeb 0x%08x. Apply original scan.\n",jeb->offset);
  514. scan_more:
  515. while(ofs < jeb->offset + c->sector_size) {
  516. jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
  517. /* Make sure there are node refs available for use */
  518. err = jffs2_prealloc_raw_node_refs(c, jeb, 2);
  519. if (err)
  520. return err;
  521. cond_resched();
  522. if (ofs & 3) {
  523. printk(KERN_WARNING "Eep. ofs 0x%08x not word-aligned!\n", ofs);
  524. ofs = PAD(ofs);
  525. continue;
  526. }
  527. if (ofs == prevofs) {
  528. printk(KERN_WARNING "ofs 0x%08x has already been seen. Skipping\n", ofs);
  529. if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
  530. return err;
  531. ofs += 4;
  532. continue;
  533. }
  534. prevofs = ofs;
  535. if (jeb->offset + c->sector_size < ofs + sizeof(*node)) {
  536. 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),
  537. jeb->offset, c->sector_size, ofs, sizeof(*node)));
  538. if ((err = jffs2_scan_dirty_space(c, jeb, (jeb->offset + c->sector_size)-ofs)))
  539. return err;
  540. break;
  541. }
  542. if (buf_ofs + buf_len < ofs + sizeof(*node)) {
  543. buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
  544. D1(printk(KERN_DEBUG "Fewer than %zd bytes (node header) left to end of buf. Reading 0x%x at 0x%08x\n",
  545. sizeof(struct jffs2_unknown_node), buf_len, ofs));
  546. err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
  547. if (err)
  548. return err;
  549. buf_ofs = ofs;
  550. }
  551. node = (struct jffs2_unknown_node *)&buf[ofs-buf_ofs];
  552. if (*(uint32_t *)(&buf[ofs-buf_ofs]) == 0xffffffff) {
  553. uint32_t inbuf_ofs;
  554. uint32_t empty_start, scan_end;
  555. empty_start = ofs;
  556. ofs += 4;
  557. scan_end = min_t(uint32_t, EMPTY_SCAN_SIZE(c->sector_size)/8, buf_len);
  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 < scan_end) {
  562. if (unlikely(*(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. if (!buf_size && (scan_end != buf_len)) {/* XIP/point case */
  582. scan_end = buf_len;
  583. goto more_empty;
  584. }
  585. /* See how much more there is to read in this eraseblock... */
  586. buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
  587. if (!buf_len) {
  588. /* No more to read. Break out of main loop without marking
  589. this range of empty space as dirty (because it's not) */
  590. D1(printk(KERN_DEBUG "Empty flash at %08x runs to end of block. Treating as free_space\n",
  591. empty_start));
  592. break;
  593. }
  594. /* point never reaches here */
  595. scan_end = buf_len;
  596. D1(printk(KERN_DEBUG "Reading another 0x%x at 0x%08x\n", buf_len, ofs));
  597. err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
  598. if (err)
  599. return err;
  600. buf_ofs = ofs;
  601. goto more_empty;
  602. }
  603. if (ofs == jeb->offset && je16_to_cpu(node->magic) == KSAMTIB_CIGAM_2SFFJ) {
  604. printk(KERN_WARNING "Magic bitmask is backwards at offset 0x%08x. Wrong endian filesystem?\n", ofs);
  605. if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
  606. return err;
  607. ofs += 4;
  608. continue;
  609. }
  610. if (je16_to_cpu(node->magic) == JFFS2_DIRTY_BITMASK) {
  611. D1(printk(KERN_DEBUG "Dirty bitmask at 0x%08x\n", ofs));
  612. if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
  613. return err;
  614. ofs += 4;
  615. continue;
  616. }
  617. if (je16_to_cpu(node->magic) == JFFS2_OLD_MAGIC_BITMASK) {
  618. printk(KERN_WARNING "Old JFFS2 bitmask found at 0x%08x\n", ofs);
  619. printk(KERN_WARNING "You cannot use older JFFS2 filesystems with newer kernels\n");
  620. if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
  621. return err;
  622. ofs += 4;
  623. continue;
  624. }
  625. if (je16_to_cpu(node->magic) != JFFS2_MAGIC_BITMASK) {
  626. /* OK. We're out of possibilities. Whinge and move on */
  627. noisy_printk(&noise, "jffs2_scan_eraseblock(): Magic bitmask 0x%04x not found at 0x%08x: 0x%04x instead\n",
  628. JFFS2_MAGIC_BITMASK, ofs,
  629. je16_to_cpu(node->magic));
  630. if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
  631. return err;
  632. ofs += 4;
  633. continue;
  634. }
  635. /* We seem to have a node of sorts. Check the CRC */
  636. crcnode.magic = node->magic;
  637. crcnode.nodetype = cpu_to_je16( je16_to_cpu(node->nodetype) | JFFS2_NODE_ACCURATE);
  638. crcnode.totlen = node->totlen;
  639. hdr_crc = crc32(0, &crcnode, sizeof(crcnode)-4);
  640. if (hdr_crc != je32_to_cpu(node->hdr_crc)) {
  641. 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",
  642. ofs, je16_to_cpu(node->magic),
  643. je16_to_cpu(node->nodetype),
  644. je32_to_cpu(node->totlen),
  645. je32_to_cpu(node->hdr_crc),
  646. hdr_crc);
  647. if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
  648. return err;
  649. ofs += 4;
  650. continue;
  651. }
  652. if (ofs + je32_to_cpu(node->totlen) > jeb->offset + c->sector_size) {
  653. /* Eep. Node goes over the end of the erase block. */
  654. printk(KERN_WARNING "Node at 0x%08x with length 0x%08x would run over the end of the erase block\n",
  655. ofs, je32_to_cpu(node->totlen));
  656. printk(KERN_WARNING "Perhaps the file system was created with the wrong erase size?\n");
  657. if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
  658. return err;
  659. ofs += 4;
  660. continue;
  661. }
  662. if (!(je16_to_cpu(node->nodetype) & JFFS2_NODE_ACCURATE)) {
  663. /* Wheee. This is an obsoleted node */
  664. D2(printk(KERN_DEBUG "Node at 0x%08x is obsolete. Skipping\n", ofs));
  665. if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen)))))
  666. return err;
  667. ofs += PAD(je32_to_cpu(node->totlen));
  668. continue;
  669. }
  670. switch(je16_to_cpu(node->nodetype)) {
  671. case JFFS2_NODETYPE_INODE:
  672. if (buf_ofs + buf_len < ofs + sizeof(struct jffs2_raw_inode)) {
  673. buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
  674. D1(printk(KERN_DEBUG "Fewer than %zd bytes (inode node) left to end of buf. Reading 0x%x at 0x%08x\n",
  675. sizeof(struct jffs2_raw_inode), buf_len, ofs));
  676. err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
  677. if (err)
  678. return err;
  679. buf_ofs = ofs;
  680. node = (void *)buf;
  681. }
  682. err = jffs2_scan_inode_node(c, jeb, (void *)node, ofs, s);
  683. if (err) return err;
  684. ofs += PAD(je32_to_cpu(node->totlen));
  685. break;
  686. case JFFS2_NODETYPE_DIRENT:
  687. if (buf_ofs + buf_len < ofs + je32_to_cpu(node->totlen)) {
  688. buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
  689. D1(printk(KERN_DEBUG "Fewer than %d bytes (dirent node) left to end of buf. Reading 0x%x at 0x%08x\n",
  690. je32_to_cpu(node->totlen), buf_len, ofs));
  691. err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
  692. if (err)
  693. return err;
  694. buf_ofs = ofs;
  695. node = (void *)buf;
  696. }
  697. err = jffs2_scan_dirent_node(c, jeb, (void *)node, ofs, s);
  698. if (err) return err;
  699. ofs += PAD(je32_to_cpu(node->totlen));
  700. break;
  701. #ifdef CONFIG_JFFS2_FS_XATTR
  702. case JFFS2_NODETYPE_XATTR:
  703. if (buf_ofs + buf_len < ofs + je32_to_cpu(node->totlen)) {
  704. buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
  705. D1(printk(KERN_DEBUG "Fewer than %d bytes (xattr node)"
  706. " left to end of buf. Reading 0x%x at 0x%08x\n",
  707. je32_to_cpu(node->totlen), buf_len, ofs));
  708. err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
  709. if (err)
  710. return err;
  711. buf_ofs = ofs;
  712. node = (void *)buf;
  713. }
  714. err = jffs2_scan_xattr_node(c, jeb, (void *)node, ofs, s);
  715. if (err)
  716. return err;
  717. ofs += PAD(je32_to_cpu(node->totlen));
  718. break;
  719. case JFFS2_NODETYPE_XREF:
  720. if (buf_ofs + buf_len < ofs + je32_to_cpu(node->totlen)) {
  721. buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
  722. D1(printk(KERN_DEBUG "Fewer than %d bytes (xref node)"
  723. " left to end of buf. Reading 0x%x at 0x%08x\n",
  724. je32_to_cpu(node->totlen), buf_len, ofs));
  725. err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
  726. if (err)
  727. return err;
  728. buf_ofs = ofs;
  729. node = (void *)buf;
  730. }
  731. err = jffs2_scan_xref_node(c, jeb, (void *)node, ofs, s);
  732. if (err)
  733. return err;
  734. ofs += PAD(je32_to_cpu(node->totlen));
  735. break;
  736. #endif /* CONFIG_JFFS2_FS_XATTR */
  737. case JFFS2_NODETYPE_CLEANMARKER:
  738. D1(printk(KERN_DEBUG "CLEANMARKER node found at 0x%08x\n", ofs));
  739. if (je32_to_cpu(node->totlen) != c->cleanmarker_size) {
  740. printk(KERN_NOTICE "CLEANMARKER node found at 0x%08x has totlen 0x%x != normal 0x%x\n",
  741. ofs, je32_to_cpu(node->totlen), c->cleanmarker_size);
  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 if (jeb->first_node) {
  746. printk(KERN_NOTICE "CLEANMARKER node found at 0x%08x, not first node in block (0x%08x)\n", ofs, jeb->offset);
  747. if ((err = jffs2_scan_dirty_space(c, jeb, PAD(sizeof(struct jffs2_unknown_node)))))
  748. return err;
  749. ofs += PAD(sizeof(struct jffs2_unknown_node));
  750. } else {
  751. jffs2_link_node_ref(c, jeb, ofs | REF_NORMAL, c->cleanmarker_size, NULL);
  752. ofs += PAD(c->cleanmarker_size);
  753. }
  754. break;
  755. case JFFS2_NODETYPE_PADDING:
  756. if (jffs2_sum_active())
  757. jffs2_sum_add_padding_mem(s, je32_to_cpu(node->totlen));
  758. if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen)))))
  759. return err;
  760. ofs += PAD(je32_to_cpu(node->totlen));
  761. break;
  762. default:
  763. switch (je16_to_cpu(node->nodetype) & JFFS2_COMPAT_MASK) {
  764. case JFFS2_FEATURE_ROCOMPAT:
  765. printk(KERN_NOTICE "Read-only compatible feature node (0x%04x) found at offset 0x%08x\n", je16_to_cpu(node->nodetype), ofs);
  766. c->flags |= JFFS2_SB_FLAG_RO;
  767. if (!(jffs2_is_readonly(c)))
  768. return -EROFS;
  769. if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen)))))
  770. return err;
  771. ofs += PAD(je32_to_cpu(node->totlen));
  772. break;
  773. case JFFS2_FEATURE_INCOMPAT:
  774. printk(KERN_NOTICE "Incompatible feature node (0x%04x) found at offset 0x%08x\n", je16_to_cpu(node->nodetype), ofs);
  775. return -EINVAL;
  776. case JFFS2_FEATURE_RWCOMPAT_DELETE:
  777. D1(printk(KERN_NOTICE "Unknown but compatible feature node (0x%04x) found at offset 0x%08x\n", je16_to_cpu(node->nodetype), ofs));
  778. if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen)))))
  779. return err;
  780. ofs += PAD(je32_to_cpu(node->totlen));
  781. break;
  782. case JFFS2_FEATURE_RWCOMPAT_COPY: {
  783. D1(printk(KERN_NOTICE "Unknown but compatible feature node (0x%04x) found at offset 0x%08x\n", je16_to_cpu(node->nodetype), ofs));
  784. jffs2_link_node_ref(c, jeb, ofs | REF_PRISTINE, PAD(je32_to_cpu(node->totlen)), NULL);
  785. /* We can't summarise nodes we don't grok */
  786. jffs2_sum_disable_collecting(s);
  787. ofs += PAD(je32_to_cpu(node->totlen));
  788. break;
  789. }
  790. }
  791. }
  792. }
  793. if (jffs2_sum_active()) {
  794. if (PAD(s->sum_size + JFFS2_SUMMARY_FRAME_SIZE) > jeb->free_size) {
  795. dbg_summary("There is not enough space for "
  796. "summary information, disabling for this jeb!\n");
  797. jffs2_sum_disable_collecting(s);
  798. }
  799. }
  800. D1(printk(KERN_DEBUG "Block at 0x%08x: free 0x%08x, dirty 0x%08x, unchecked 0x%08x, used 0x%08x, wasted 0x%08x\n",
  801. jeb->offset,jeb->free_size, jeb->dirty_size, jeb->unchecked_size, jeb->used_size, jeb->wasted_size));
  802. /* mark_node_obsolete can add to wasted !! */
  803. if (jeb->wasted_size) {
  804. jeb->dirty_size += jeb->wasted_size;
  805. c->dirty_size += jeb->wasted_size;
  806. c->wasted_size -= jeb->wasted_size;
  807. jeb->wasted_size = 0;
  808. }
  809. return jffs2_scan_classify_jeb(c, jeb);
  810. }
  811. struct jffs2_inode_cache *jffs2_scan_make_ino_cache(struct jffs2_sb_info *c, uint32_t ino)
  812. {
  813. struct jffs2_inode_cache *ic;
  814. ic = jffs2_get_ino_cache(c, ino);
  815. if (ic)
  816. return ic;
  817. if (ino > c->highest_ino)
  818. c->highest_ino = ino;
  819. ic = jffs2_alloc_inode_cache();
  820. if (!ic) {
  821. printk(KERN_NOTICE "jffs2_scan_make_inode_cache(): allocation of inode cache failed\n");
  822. return NULL;
  823. }
  824. memset(ic, 0, sizeof(*ic));
  825. ic->ino = ino;
  826. ic->nodes = (void *)ic;
  827. jffs2_add_ino_cache(c, ic);
  828. if (ino == 1)
  829. ic->pino_nlink = 1;
  830. return ic;
  831. }
  832. static int jffs2_scan_inode_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
  833. struct jffs2_raw_inode *ri, uint32_t ofs, struct jffs2_summary *s)
  834. {
  835. struct jffs2_inode_cache *ic;
  836. uint32_t crc, ino = je32_to_cpu(ri->ino);
  837. D1(printk(KERN_DEBUG "jffs2_scan_inode_node(): Node at 0x%08x\n", ofs));
  838. /* We do very little here now. Just check the ino# to which we should attribute
  839. this node; we can do all the CRC checking etc. later. There's a tradeoff here --
  840. we used to scan the flash once only, reading everything we want from it into
  841. memory, then building all our in-core data structures and freeing the extra
  842. information. Now we allow the first part of the mount to complete a lot quicker,
  843. but we have to go _back_ to the flash in order to finish the CRC checking, etc.
  844. Which means that the _full_ amount of time to get to proper write mode with GC
  845. operational may actually be _longer_ than before. Sucks to be me. */
  846. /* Check the node CRC in any case. */
  847. 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 "
  850. "node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
  851. ofs, je32_to_cpu(ri->node_crc), crc);
  852. /*
  853. * We believe totlen because the CRC on the node
  854. * _header_ was OK, just the node itself failed.
  855. */
  856. return jffs2_scan_dirty_space(c, jeb,
  857. PAD(je32_to_cpu(ri->totlen)));
  858. }
  859. ic = jffs2_get_ino_cache(c, ino);
  860. if (!ic) {
  861. ic = jffs2_scan_make_ino_cache(c, ino);
  862. if (!ic)
  863. return -ENOMEM;
  864. }
  865. /* Wheee. It worked */
  866. jffs2_link_node_ref(c, jeb, ofs | REF_UNCHECKED, PAD(je32_to_cpu(ri->totlen)), ic);
  867. D1(printk(KERN_DEBUG "Node is ino #%u, version %d. Range 0x%x-0x%x\n",
  868. je32_to_cpu(ri->ino), je32_to_cpu(ri->version),
  869. je32_to_cpu(ri->offset),
  870. je32_to_cpu(ri->offset)+je32_to_cpu(ri->dsize)));
  871. pseudo_random += je32_to_cpu(ri->version);
  872. if (jffs2_sum_active()) {
  873. jffs2_sum_add_inode_mem(s, ri, ofs - jeb->offset);
  874. }
  875. return 0;
  876. }
  877. static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
  878. struct jffs2_raw_dirent *rd, uint32_t ofs, struct jffs2_summary *s)
  879. {
  880. struct jffs2_full_dirent *fd;
  881. struct jffs2_inode_cache *ic;
  882. uint32_t checkedlen;
  883. uint32_t crc;
  884. int err;
  885. D1(printk(KERN_DEBUG "jffs2_scan_dirent_node(): Node at 0x%08x\n", ofs));
  886. /* We don't get here unless the node is still valid, so we don't have to
  887. mask in the ACCURATE bit any more. */
  888. crc = crc32(0, rd, sizeof(*rd)-8);
  889. if (crc != je32_to_cpu(rd->node_crc)) {
  890. printk(KERN_NOTICE "jffs2_scan_dirent_node(): Node CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
  891. ofs, je32_to_cpu(rd->node_crc), crc);
  892. /* We believe totlen because the CRC on the node _header_ was OK, just the node itself failed. */
  893. if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(rd->totlen)))))
  894. return err;
  895. return 0;
  896. }
  897. pseudo_random += je32_to_cpu(rd->version);
  898. /* Should never happen. Did. (OLPC trac #4184)*/
  899. checkedlen = strnlen(rd->name, rd->nsize);
  900. if (checkedlen < rd->nsize) {
  901. printk(KERN_ERR "Dirent at %08x has zeroes in name. Truncating to %d chars\n",
  902. ofs, checkedlen);
  903. }
  904. fd = jffs2_alloc_full_dirent(checkedlen+1);
  905. if (!fd) {
  906. return -ENOMEM;
  907. }
  908. memcpy(&fd->name, rd->name, checkedlen);
  909. fd->name[checkedlen] = 0;
  910. crc = crc32(0, fd->name, rd->nsize);
  911. if (crc != je32_to_cpu(rd->name_crc)) {
  912. printk(KERN_NOTICE "jffs2_scan_dirent_node(): Name CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
  913. ofs, je32_to_cpu(rd->name_crc), crc);
  914. D1(printk(KERN_NOTICE "Name for which CRC failed is (now) '%s', ino #%d\n", fd->name, je32_to_cpu(rd->ino)));
  915. jffs2_free_full_dirent(fd);
  916. /* FIXME: Why do we believe totlen? */
  917. /* We believe totlen because the CRC on the node _header_ was OK, just the name failed. */
  918. if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(rd->totlen)))))
  919. return err;
  920. return 0;
  921. }
  922. ic = jffs2_scan_make_ino_cache(c, je32_to_cpu(rd->pino));
  923. if (!ic) {
  924. jffs2_free_full_dirent(fd);
  925. return -ENOMEM;
  926. }
  927. fd->raw = jffs2_link_node_ref(c, jeb, ofs | dirent_node_state(rd),
  928. PAD(je32_to_cpu(rd->totlen)), ic);
  929. fd->next = NULL;
  930. fd->version = je32_to_cpu(rd->version);
  931. fd->ino = je32_to_cpu(rd->ino);
  932. fd->nhash = full_name_hash(fd->name, checkedlen);
  933. fd->type = rd->type;
  934. jffs2_add_fd_to_list(c, fd, &ic->scan_dents);
  935. if (jffs2_sum_active()) {
  936. jffs2_sum_add_dirent_mem(s, rd, ofs - jeb->offset);
  937. }
  938. return 0;
  939. }
  940. static int count_list(struct list_head *l)
  941. {
  942. uint32_t count = 0;
  943. struct list_head *tmp;
  944. list_for_each(tmp, l) {
  945. count++;
  946. }
  947. return count;
  948. }
  949. /* Note: This breaks if list_empty(head). I don't care. You
  950. might, if you copy this code and use it elsewhere :) */
  951. static void rotate_list(struct list_head *head, uint32_t count)
  952. {
  953. struct list_head *n = head->next;
  954. list_del(head);
  955. while(count--) {
  956. n = n->next;
  957. }
  958. list_add(head, n);
  959. }
  960. void jffs2_rotate_lists(struct jffs2_sb_info *c)
  961. {
  962. uint32_t x;
  963. uint32_t rotateby;
  964. x = count_list(&c->clean_list);
  965. if (x) {
  966. rotateby = pseudo_random % x;
  967. rotate_list((&c->clean_list), rotateby);
  968. }
  969. x = count_list(&c->very_dirty_list);
  970. if (x) {
  971. rotateby = pseudo_random % x;
  972. rotate_list((&c->very_dirty_list), rotateby);
  973. }
  974. x = count_list(&c->dirty_list);
  975. if (x) {
  976. rotateby = pseudo_random % x;
  977. rotate_list((&c->dirty_list), rotateby);
  978. }
  979. x = count_list(&c->erasable_list);
  980. if (x) {
  981. rotateby = pseudo_random % x;
  982. rotate_list((&c->erasable_list), rotateby);
  983. }
  984. if (c->nr_erasing_blocks) {
  985. rotateby = pseudo_random % c->nr_erasing_blocks;
  986. rotate_list((&c->erase_pending_list), rotateby);
  987. }
  988. if (c->nr_free_blocks) {
  989. rotateby = pseudo_random % c->nr_free_blocks;
  990. rotate_list((&c->free_list), rotateby);
  991. }
  992. }