scan.c 35 KB

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