scan.c 35 KB

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