scan.c 36 KB

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