summary.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. /*
  2. * JFFS2 -- Journalling Flash File System, Version 2.
  3. *
  4. * Copyright (C) 2004 Ferenc Havasi <havasi@inf.u-szeged.hu>,
  5. * Zoltan Sogor <weth@inf.u-szeged.hu>,
  6. * Patrik Kluba <pajko@halom.u-szeged.hu>,
  7. * University of Szeged, Hungary
  8. * 2006 KaiGai Kohei <kaigai@ak.jp.nec.com>
  9. *
  10. * For licensing information, see the file 'LICENCE' in this directory.
  11. *
  12. * $Id: summary.c,v 1.1.1.1 2007/06/12 07:27:13 eyryu Exp $
  13. *
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/slab.h>
  17. #include <linux/mtd/mtd.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/crc32.h>
  20. #include <linux/compiler.h>
  21. #include <linux/vmalloc.h>
  22. #include "nodelist.h"
  23. #include "debug.h"
  24. int jffs2_sum_init(struct jffs2_sb_info *c)
  25. {
  26. c->summary = kzalloc(sizeof(struct jffs2_summary), GFP_KERNEL);
  27. if (!c->summary) {
  28. JFFS2_WARNING("Can't allocate memory for summary information!\n");
  29. return -ENOMEM;
  30. }
  31. c->summary->sum_buf = vmalloc(c->sector_size);
  32. if (!c->summary->sum_buf) {
  33. JFFS2_WARNING("Can't allocate buffer for writing out summary information!\n");
  34. kfree(c->summary);
  35. return -ENOMEM;
  36. }
  37. dbg_summary("returned successfully\n");
  38. return 0;
  39. }
  40. void jffs2_sum_exit(struct jffs2_sb_info *c)
  41. {
  42. dbg_summary("called\n");
  43. jffs2_sum_disable_collecting(c->summary);
  44. vfree(c->summary->sum_buf);
  45. c->summary->sum_buf = NULL;
  46. kfree(c->summary);
  47. c->summary = NULL;
  48. }
  49. static int jffs2_sum_add_mem(struct jffs2_summary *s, union jffs2_sum_mem *item)
  50. {
  51. if (!s->sum_list_head)
  52. s->sum_list_head = (union jffs2_sum_mem *) item;
  53. if (s->sum_list_tail)
  54. s->sum_list_tail->u.next = (union jffs2_sum_mem *) item;
  55. s->sum_list_tail = (union jffs2_sum_mem *) item;
  56. switch (je16_to_cpu(item->u.nodetype)) {
  57. case JFFS2_NODETYPE_INODE:
  58. s->sum_size += JFFS2_SUMMARY_INODE_SIZE;
  59. s->sum_num++;
  60. dbg_summary("inode (%u) added to summary\n",
  61. je32_to_cpu(item->i.inode));
  62. break;
  63. case JFFS2_NODETYPE_DIRENT:
  64. s->sum_size += JFFS2_SUMMARY_DIRENT_SIZE(item->d.nsize);
  65. s->sum_num++;
  66. dbg_summary("dirent (%u) added to summary\n",
  67. je32_to_cpu(item->d.ino));
  68. break;
  69. #ifdef CONFIG_JFFS2_FS_XATTR
  70. case JFFS2_NODETYPE_XATTR:
  71. s->sum_size += JFFS2_SUMMARY_XATTR_SIZE;
  72. s->sum_num++;
  73. dbg_summary("xattr (xid=%u, version=%u) added to summary\n",
  74. je32_to_cpu(item->x.xid), je32_to_cpu(item->x.version));
  75. break;
  76. case JFFS2_NODETYPE_XREF:
  77. s->sum_size += JFFS2_SUMMARY_XREF_SIZE;
  78. s->sum_num++;
  79. dbg_summary("xref added to summary\n");
  80. break;
  81. #endif
  82. default:
  83. JFFS2_WARNING("UNKNOWN node type %u\n",
  84. je16_to_cpu(item->u.nodetype));
  85. return 1;
  86. }
  87. return 0;
  88. }
  89. /* The following 3 functions are called from scan.c to collect summary info for not closed jeb */
  90. int jffs2_sum_add_padding_mem(struct jffs2_summary *s, uint32_t size)
  91. {
  92. dbg_summary("called with %u\n", size);
  93. s->sum_padded += size;
  94. return 0;
  95. }
  96. int jffs2_sum_add_inode_mem(struct jffs2_summary *s, struct jffs2_raw_inode *ri,
  97. uint32_t ofs)
  98. {
  99. struct jffs2_sum_inode_mem *temp = kmalloc(sizeof(struct jffs2_sum_inode_mem), GFP_KERNEL);
  100. if (!temp)
  101. return -ENOMEM;
  102. temp->nodetype = ri->nodetype;
  103. temp->inode = ri->ino;
  104. temp->version = ri->version;
  105. temp->offset = cpu_to_je32(ofs); /* relative offset from the begining of the jeb */
  106. temp->totlen = ri->totlen;
  107. temp->next = NULL;
  108. return jffs2_sum_add_mem(s, (union jffs2_sum_mem *)temp);
  109. }
  110. int jffs2_sum_add_dirent_mem(struct jffs2_summary *s, struct jffs2_raw_dirent *rd,
  111. uint32_t ofs)
  112. {
  113. struct jffs2_sum_dirent_mem *temp =
  114. kmalloc(sizeof(struct jffs2_sum_dirent_mem) + rd->nsize, GFP_KERNEL);
  115. if (!temp)
  116. return -ENOMEM;
  117. temp->nodetype = rd->nodetype;
  118. temp->totlen = rd->totlen;
  119. temp->offset = cpu_to_je32(ofs); /* relative from the begining of the jeb */
  120. temp->pino = rd->pino;
  121. temp->version = rd->version;
  122. temp->ino = rd->ino;
  123. temp->nsize = rd->nsize;
  124. temp->type = rd->type;
  125. temp->next = NULL;
  126. memcpy(temp->name, rd->name, rd->nsize);
  127. return jffs2_sum_add_mem(s, (union jffs2_sum_mem *)temp);
  128. }
  129. #ifdef CONFIG_JFFS2_FS_XATTR
  130. int jffs2_sum_add_xattr_mem(struct jffs2_summary *s, struct jffs2_raw_xattr *rx, uint32_t ofs)
  131. {
  132. struct jffs2_sum_xattr_mem *temp;
  133. temp = kmalloc(sizeof(struct jffs2_sum_xattr_mem), GFP_KERNEL);
  134. if (!temp)
  135. return -ENOMEM;
  136. temp->nodetype = rx->nodetype;
  137. temp->xid = rx->xid;
  138. temp->version = rx->version;
  139. temp->offset = cpu_to_je32(ofs);
  140. temp->totlen = rx->totlen;
  141. temp->next = NULL;
  142. return jffs2_sum_add_mem(s, (union jffs2_sum_mem *)temp);
  143. }
  144. int jffs2_sum_add_xref_mem(struct jffs2_summary *s, struct jffs2_raw_xref *rr, uint32_t ofs)
  145. {
  146. struct jffs2_sum_xref_mem *temp;
  147. temp = kmalloc(sizeof(struct jffs2_sum_xref_mem), GFP_KERNEL);
  148. if (!temp)
  149. return -ENOMEM;
  150. temp->nodetype = rr->nodetype;
  151. temp->offset = cpu_to_je32(ofs);
  152. temp->next = NULL;
  153. return jffs2_sum_add_mem(s, (union jffs2_sum_mem *)temp);
  154. }
  155. #endif
  156. /* Cleanup every collected summary information */
  157. static void jffs2_sum_clean_collected(struct jffs2_summary *s)
  158. {
  159. union jffs2_sum_mem *temp;
  160. if (!s->sum_list_head) {
  161. dbg_summary("already empty\n");
  162. }
  163. while (s->sum_list_head) {
  164. temp = s->sum_list_head;
  165. s->sum_list_head = s->sum_list_head->u.next;
  166. kfree(temp);
  167. }
  168. s->sum_list_tail = NULL;
  169. s->sum_padded = 0;
  170. s->sum_num = 0;
  171. }
  172. void jffs2_sum_reset_collected(struct jffs2_summary *s)
  173. {
  174. dbg_summary("called\n");
  175. jffs2_sum_clean_collected(s);
  176. s->sum_size = 0;
  177. }
  178. void jffs2_sum_disable_collecting(struct jffs2_summary *s)
  179. {
  180. dbg_summary("called\n");
  181. jffs2_sum_clean_collected(s);
  182. s->sum_size = JFFS2_SUMMARY_NOSUM_SIZE;
  183. }
  184. int jffs2_sum_is_disabled(struct jffs2_summary *s)
  185. {
  186. return (s->sum_size == JFFS2_SUMMARY_NOSUM_SIZE);
  187. }
  188. /* Move the collected summary information into sb (called from scan.c) */
  189. void jffs2_sum_move_collected(struct jffs2_sb_info *c, struct jffs2_summary *s)
  190. {
  191. dbg_summary("oldsize=0x%x oldnum=%u => newsize=0x%x newnum=%u\n",
  192. c->summary->sum_size, c->summary->sum_num,
  193. s->sum_size, s->sum_num);
  194. c->summary->sum_size = s->sum_size;
  195. c->summary->sum_num = s->sum_num;
  196. c->summary->sum_padded = s->sum_padded;
  197. c->summary->sum_list_head = s->sum_list_head;
  198. c->summary->sum_list_tail = s->sum_list_tail;
  199. s->sum_list_head = s->sum_list_tail = NULL;
  200. }
  201. /* Called from wbuf.c to collect writed node info */
  202. int jffs2_sum_add_kvec(struct jffs2_sb_info *c, const struct kvec *invecs,
  203. unsigned long count, uint32_t ofs)
  204. {
  205. union jffs2_node_union *node;
  206. struct jffs2_eraseblock *jeb;
  207. if (c->summary->sum_size == JFFS2_SUMMARY_NOSUM_SIZE) {
  208. dbg_summary("Summary is disabled for this jeb! Skipping summary info!\n");
  209. return 0;
  210. }
  211. node = invecs[0].iov_base;
  212. jeb = &c->blocks[ofs / c->sector_size];
  213. ofs -= jeb->offset;
  214. switch (je16_to_cpu(node->u.nodetype)) {
  215. case JFFS2_NODETYPE_INODE: {
  216. struct jffs2_sum_inode_mem *temp =
  217. kmalloc(sizeof(struct jffs2_sum_inode_mem), GFP_KERNEL);
  218. if (!temp)
  219. goto no_mem;
  220. temp->nodetype = node->i.nodetype;
  221. temp->inode = node->i.ino;
  222. temp->version = node->i.version;
  223. temp->offset = cpu_to_je32(ofs);
  224. temp->totlen = node->i.totlen;
  225. temp->next = NULL;
  226. return jffs2_sum_add_mem(c->summary, (union jffs2_sum_mem *)temp);
  227. }
  228. case JFFS2_NODETYPE_DIRENT: {
  229. struct jffs2_sum_dirent_mem *temp =
  230. kmalloc(sizeof(struct jffs2_sum_dirent_mem) + node->d.nsize, GFP_KERNEL);
  231. if (!temp)
  232. goto no_mem;
  233. temp->nodetype = node->d.nodetype;
  234. temp->totlen = node->d.totlen;
  235. temp->offset = cpu_to_je32(ofs);
  236. temp->pino = node->d.pino;
  237. temp->version = node->d.version;
  238. temp->ino = node->d.ino;
  239. temp->nsize = node->d.nsize;
  240. temp->type = node->d.type;
  241. temp->next = NULL;
  242. switch (count) {
  243. case 1:
  244. memcpy(temp->name,node->d.name,node->d.nsize);
  245. break;
  246. case 2:
  247. memcpy(temp->name,invecs[1].iov_base,node->d.nsize);
  248. break;
  249. default:
  250. BUG(); /* impossible count value */
  251. break;
  252. }
  253. return jffs2_sum_add_mem(c->summary, (union jffs2_sum_mem *)temp);
  254. }
  255. #ifdef CONFIG_JFFS2_FS_XATTR
  256. case JFFS2_NODETYPE_XATTR: {
  257. struct jffs2_sum_xattr_mem *temp;
  258. temp = kmalloc(sizeof(struct jffs2_sum_xattr_mem), GFP_KERNEL);
  259. if (!temp)
  260. goto no_mem;
  261. temp->nodetype = node->x.nodetype;
  262. temp->xid = node->x.xid;
  263. temp->version = node->x.version;
  264. temp->totlen = node->x.totlen;
  265. temp->offset = cpu_to_je32(ofs);
  266. temp->next = NULL;
  267. return jffs2_sum_add_mem(c->summary, (union jffs2_sum_mem *)temp);
  268. }
  269. case JFFS2_NODETYPE_XREF: {
  270. struct jffs2_sum_xref_mem *temp;
  271. temp = kmalloc(sizeof(struct jffs2_sum_xref_mem), GFP_KERNEL);
  272. if (!temp)
  273. goto no_mem;
  274. temp->nodetype = node->r.nodetype;
  275. temp->offset = cpu_to_je32(ofs);
  276. temp->next = NULL;
  277. return jffs2_sum_add_mem(c->summary, (union jffs2_sum_mem *)temp);
  278. }
  279. #endif
  280. case JFFS2_NODETYPE_PADDING:
  281. dbg_summary("node PADDING\n");
  282. c->summary->sum_padded += je32_to_cpu(node->u.totlen);
  283. break;
  284. case JFFS2_NODETYPE_CLEANMARKER:
  285. dbg_summary("node CLEANMARKER\n");
  286. break;
  287. case JFFS2_NODETYPE_SUMMARY:
  288. dbg_summary("node SUMMARY\n");
  289. break;
  290. default:
  291. /* If you implement a new node type you should also implement
  292. summary support for it or disable summary.
  293. */
  294. BUG();
  295. break;
  296. }
  297. return 0;
  298. no_mem:
  299. JFFS2_WARNING("MEMORY ALLOCATION ERROR!");
  300. return -ENOMEM;
  301. }
  302. static struct jffs2_raw_node_ref *sum_link_node_ref(struct jffs2_sb_info *c,
  303. struct jffs2_eraseblock *jeb,
  304. uint32_t ofs, uint32_t len,
  305. struct jffs2_inode_cache *ic)
  306. {
  307. /* If there was a gap, mark it dirty */
  308. if ((ofs & ~3) > c->sector_size - jeb->free_size) {
  309. /* Ew. Summary doesn't actually tell us explicitly about dirty space */
  310. jffs2_scan_dirty_space(c, jeb, (ofs & ~3) - (c->sector_size - jeb->free_size));
  311. }
  312. return jffs2_link_node_ref(c, jeb, jeb->offset + ofs, len, ic);
  313. }
  314. /* Process the stored summary information - helper function for jffs2_sum_scan_sumnode() */
  315. static int jffs2_sum_process_sum_data(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
  316. struct jffs2_raw_summary *summary, uint32_t *pseudo_random)
  317. {
  318. struct jffs2_inode_cache *ic;
  319. struct jffs2_full_dirent *fd;
  320. void *sp;
  321. int i, ino;
  322. int err;
  323. sp = summary->sum;
  324. for (i=0; i<je32_to_cpu(summary->sum_num); i++) {
  325. dbg_summary("processing summary index %d\n", i);
  326. cond_resched();
  327. /* Make sure there's a spare ref for dirty space */
  328. err = jffs2_prealloc_raw_node_refs(c, jeb, 2);
  329. if (err)
  330. return err;
  331. switch (je16_to_cpu(((struct jffs2_sum_unknown_flash *)sp)->nodetype)) {
  332. case JFFS2_NODETYPE_INODE: {
  333. struct jffs2_sum_inode_flash *spi;
  334. spi = sp;
  335. ino = je32_to_cpu(spi->inode);
  336. dbg_summary("Inode at 0x%08x-0x%08x\n",
  337. jeb->offset + je32_to_cpu(spi->offset),
  338. jeb->offset + je32_to_cpu(spi->offset) + je32_to_cpu(spi->totlen));
  339. ic = jffs2_scan_make_ino_cache(c, ino);
  340. if (!ic) {
  341. JFFS2_NOTICE("scan_make_ino_cache failed\n");
  342. return -ENOMEM;
  343. }
  344. sum_link_node_ref(c, jeb, je32_to_cpu(spi->offset) | REF_UNCHECKED,
  345. PAD(je32_to_cpu(spi->totlen)), ic);
  346. *pseudo_random += je32_to_cpu(spi->version);
  347. sp += JFFS2_SUMMARY_INODE_SIZE;
  348. break;
  349. }
  350. case JFFS2_NODETYPE_DIRENT: {
  351. struct jffs2_sum_dirent_flash *spd;
  352. spd = sp;
  353. dbg_summary("Dirent at 0x%08x-0x%08x\n",
  354. jeb->offset + je32_to_cpu(spd->offset),
  355. jeb->offset + je32_to_cpu(spd->offset) + je32_to_cpu(spd->totlen));
  356. fd = jffs2_alloc_full_dirent(spd->nsize+1);
  357. if (!fd)
  358. return -ENOMEM;
  359. memcpy(&fd->name, spd->name, spd->nsize);
  360. fd->name[spd->nsize] = 0;
  361. ic = jffs2_scan_make_ino_cache(c, je32_to_cpu(spd->pino));
  362. if (!ic) {
  363. jffs2_free_full_dirent(fd);
  364. return -ENOMEM;
  365. }
  366. fd->raw = sum_link_node_ref(c, jeb, je32_to_cpu(spd->offset) | REF_UNCHECKED,
  367. PAD(je32_to_cpu(spd->totlen)), ic);
  368. fd->next = NULL;
  369. fd->version = je32_to_cpu(spd->version);
  370. fd->ino = je32_to_cpu(spd->ino);
  371. fd->nhash = full_name_hash(fd->name, spd->nsize);
  372. fd->type = spd->type;
  373. jffs2_add_fd_to_list(c, fd, &ic->scan_dents);
  374. *pseudo_random += je32_to_cpu(spd->version);
  375. sp += JFFS2_SUMMARY_DIRENT_SIZE(spd->nsize);
  376. break;
  377. }
  378. #ifdef CONFIG_JFFS2_FS_XATTR
  379. case JFFS2_NODETYPE_XATTR: {
  380. struct jffs2_xattr_datum *xd;
  381. struct jffs2_sum_xattr_flash *spx;
  382. spx = (struct jffs2_sum_xattr_flash *)sp;
  383. dbg_summary("xattr at %#08x-%#08x (xid=%u, version=%u)\n",
  384. jeb->offset + je32_to_cpu(spx->offset),
  385. jeb->offset + je32_to_cpu(spx->offset) + je32_to_cpu(spx->totlen),
  386. je32_to_cpu(spx->xid), je32_to_cpu(spx->version));
  387. xd = jffs2_setup_xattr_datum(c, je32_to_cpu(spx->xid),
  388. je32_to_cpu(spx->version));
  389. if (IS_ERR(xd))
  390. return PTR_ERR(xd);
  391. if (xd->version > je32_to_cpu(spx->version)) {
  392. /* node is not the newest one */
  393. struct jffs2_raw_node_ref *raw
  394. = sum_link_node_ref(c, jeb, je32_to_cpu(spx->offset) | REF_UNCHECKED,
  395. PAD(je32_to_cpu(spx->totlen)), NULL);
  396. raw->next_in_ino = xd->node->next_in_ino;
  397. xd->node->next_in_ino = raw;
  398. } else {
  399. xd->version = je32_to_cpu(spx->version);
  400. sum_link_node_ref(c, jeb, je32_to_cpu(spx->offset) | REF_UNCHECKED,
  401. PAD(je32_to_cpu(spx->totlen)), (void *)xd);
  402. }
  403. *pseudo_random += je32_to_cpu(spx->xid);
  404. sp += JFFS2_SUMMARY_XATTR_SIZE;
  405. break;
  406. }
  407. case JFFS2_NODETYPE_XREF: {
  408. struct jffs2_xattr_ref *ref;
  409. struct jffs2_sum_xref_flash *spr;
  410. spr = (struct jffs2_sum_xref_flash *)sp;
  411. dbg_summary("xref at %#08x-%#08x\n",
  412. jeb->offset + je32_to_cpu(spr->offset),
  413. jeb->offset + je32_to_cpu(spr->offset) +
  414. (uint32_t)PAD(sizeof(struct jffs2_raw_xref)));
  415. ref = jffs2_alloc_xattr_ref();
  416. if (!ref) {
  417. JFFS2_NOTICE("allocation of xattr_datum failed\n");
  418. return -ENOMEM;
  419. }
  420. ref->next = c->xref_temp;
  421. c->xref_temp = ref;
  422. sum_link_node_ref(c, jeb, je32_to_cpu(spr->offset) | REF_UNCHECKED,
  423. PAD(sizeof(struct jffs2_raw_xref)), (void *)ref);
  424. *pseudo_random += ref->node->flash_offset;
  425. sp += JFFS2_SUMMARY_XREF_SIZE;
  426. break;
  427. }
  428. #endif
  429. default : {
  430. uint16_t nodetype = je16_to_cpu(((struct jffs2_sum_unknown_flash *)sp)->nodetype);
  431. JFFS2_WARNING("Unsupported node type %x found in summary! Exiting...\n", nodetype);
  432. if ((nodetype & JFFS2_COMPAT_MASK) == JFFS2_FEATURE_INCOMPAT)
  433. return -EIO;
  434. /* For compatible node types, just fall back to the full scan */
  435. c->wasted_size -= jeb->wasted_size;
  436. c->free_size += c->sector_size - jeb->free_size;
  437. c->used_size -= jeb->used_size;
  438. c->dirty_size -= jeb->dirty_size;
  439. jeb->wasted_size = jeb->used_size = jeb->dirty_size = 0;
  440. jeb->free_size = c->sector_size;
  441. jffs2_free_jeb_node_refs(c, jeb);
  442. return -ENOTRECOVERABLE;
  443. }
  444. }
  445. }
  446. return 0;
  447. }
  448. /* Process the summary node - called from jffs2_scan_eraseblock() */
  449. int jffs2_sum_scan_sumnode(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
  450. struct jffs2_raw_summary *summary, uint32_t sumsize,
  451. uint32_t *pseudo_random)
  452. {
  453. struct jffs2_unknown_node crcnode;
  454. int ret, ofs;
  455. uint32_t crc;
  456. ofs = c->sector_size - sumsize;
  457. dbg_summary("summary found for 0x%08x at 0x%08x (0x%x bytes)\n",
  458. jeb->offset, jeb->offset + ofs, sumsize);
  459. /* OK, now check for node validity and CRC */
  460. crcnode.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  461. crcnode.nodetype = cpu_to_je16(JFFS2_NODETYPE_SUMMARY);
  462. crcnode.totlen = summary->totlen;
  463. crc = crc32(0, &crcnode, sizeof(crcnode)-4);
  464. if (je32_to_cpu(summary->hdr_crc) != crc) {
  465. dbg_summary("Summary node header is corrupt (bad CRC or "
  466. "no summary at all)\n");
  467. goto crc_err;
  468. }
  469. if (je32_to_cpu(summary->totlen) != sumsize) {
  470. dbg_summary("Summary node is corrupt (wrong erasesize?)\n");
  471. goto crc_err;
  472. }
  473. crc = crc32(0, summary, sizeof(struct jffs2_raw_summary)-8);
  474. if (je32_to_cpu(summary->node_crc) != crc) {
  475. dbg_summary("Summary node is corrupt (bad CRC)\n");
  476. goto crc_err;
  477. }
  478. crc = crc32(0, summary->sum, sumsize - sizeof(struct jffs2_raw_summary));
  479. if (je32_to_cpu(summary->sum_crc) != crc) {
  480. dbg_summary("Summary node data is corrupt (bad CRC)\n");
  481. goto crc_err;
  482. }
  483. if ( je32_to_cpu(summary->cln_mkr) ) {
  484. dbg_summary("Summary : CLEANMARKER node \n");
  485. ret = jffs2_prealloc_raw_node_refs(c, jeb, 1);
  486. if (ret)
  487. return ret;
  488. if (je32_to_cpu(summary->cln_mkr) != c->cleanmarker_size) {
  489. dbg_summary("CLEANMARKER node has totlen 0x%x != normal 0x%x\n",
  490. je32_to_cpu(summary->cln_mkr), c->cleanmarker_size);
  491. if ((ret = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(summary->cln_mkr)))))
  492. return ret;
  493. } else if (jeb->first_node) {
  494. dbg_summary("CLEANMARKER node not first node in block "
  495. "(0x%08x)\n", jeb->offset);
  496. if ((ret = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(summary->cln_mkr)))))
  497. return ret;
  498. } else {
  499. jffs2_link_node_ref(c, jeb, jeb->offset | REF_NORMAL,
  500. je32_to_cpu(summary->cln_mkr), NULL);
  501. }
  502. }
  503. ret = jffs2_sum_process_sum_data(c, jeb, summary, pseudo_random);
  504. /* -ENOTRECOVERABLE isn't a fatal error -- it means we should do a full
  505. scan of this eraseblock. So return zero */
  506. if (ret == -ENOTRECOVERABLE)
  507. return 0;
  508. if (ret)
  509. return ret; /* real error */
  510. /* for PARANOIA_CHECK */
  511. ret = jffs2_prealloc_raw_node_refs(c, jeb, 2);
  512. if (ret)
  513. return ret;
  514. sum_link_node_ref(c, jeb, ofs | REF_NORMAL, sumsize, NULL);
  515. if (unlikely(jeb->free_size)) {
  516. JFFS2_WARNING("Free size 0x%x bytes in eraseblock @0x%08x with summary?\n",
  517. jeb->free_size, jeb->offset);
  518. jeb->wasted_size += jeb->free_size;
  519. c->wasted_size += jeb->free_size;
  520. c->free_size -= jeb->free_size;
  521. jeb->free_size = 0;
  522. }
  523. return jffs2_scan_classify_jeb(c, jeb);
  524. crc_err:
  525. JFFS2_WARNING("Summary node crc error, skipping summary information.\n");
  526. return 0;
  527. }
  528. /* Write summary data to flash - helper function for jffs2_sum_write_sumnode() */
  529. static int jffs2_sum_write_data(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
  530. uint32_t infosize, uint32_t datasize, int padsize)
  531. {
  532. struct jffs2_raw_summary isum;
  533. union jffs2_sum_mem *temp;
  534. struct jffs2_sum_marker *sm;
  535. struct kvec vecs[2];
  536. uint32_t sum_ofs;
  537. void *wpage;
  538. int ret;
  539. size_t retlen;
  540. memset(c->summary->sum_buf, 0xff, datasize);
  541. memset(&isum, 0, sizeof(isum));
  542. isum.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  543. isum.nodetype = cpu_to_je16(JFFS2_NODETYPE_SUMMARY);
  544. isum.totlen = cpu_to_je32(infosize);
  545. isum.hdr_crc = cpu_to_je32(crc32(0, &isum, sizeof(struct jffs2_unknown_node) - 4));
  546. isum.padded = cpu_to_je32(c->summary->sum_padded);
  547. isum.cln_mkr = cpu_to_je32(c->cleanmarker_size);
  548. isum.sum_num = cpu_to_je32(c->summary->sum_num);
  549. wpage = c->summary->sum_buf;
  550. while (c->summary->sum_num) {
  551. temp = c->summary->sum_list_head;
  552. switch (je16_to_cpu(temp->u.nodetype)) {
  553. case JFFS2_NODETYPE_INODE: {
  554. struct jffs2_sum_inode_flash *sino_ptr = wpage;
  555. sino_ptr->nodetype = temp->i.nodetype;
  556. sino_ptr->inode = temp->i.inode;
  557. sino_ptr->version = temp->i.version;
  558. sino_ptr->offset = temp->i.offset;
  559. sino_ptr->totlen = temp->i.totlen;
  560. wpage += JFFS2_SUMMARY_INODE_SIZE;
  561. break;
  562. }
  563. case JFFS2_NODETYPE_DIRENT: {
  564. struct jffs2_sum_dirent_flash *sdrnt_ptr = wpage;
  565. sdrnt_ptr->nodetype = temp->d.nodetype;
  566. sdrnt_ptr->totlen = temp->d.totlen;
  567. sdrnt_ptr->offset = temp->d.offset;
  568. sdrnt_ptr->pino = temp->d.pino;
  569. sdrnt_ptr->version = temp->d.version;
  570. sdrnt_ptr->ino = temp->d.ino;
  571. sdrnt_ptr->nsize = temp->d.nsize;
  572. sdrnt_ptr->type = temp->d.type;
  573. memcpy(sdrnt_ptr->name, temp->d.name,
  574. temp->d.nsize);
  575. wpage += JFFS2_SUMMARY_DIRENT_SIZE(temp->d.nsize);
  576. break;
  577. }
  578. #ifdef CONFIG_JFFS2_FS_XATTR
  579. case JFFS2_NODETYPE_XATTR: {
  580. struct jffs2_sum_xattr_flash *sxattr_ptr = wpage;
  581. temp = c->summary->sum_list_head;
  582. sxattr_ptr->nodetype = temp->x.nodetype;
  583. sxattr_ptr->xid = temp->x.xid;
  584. sxattr_ptr->version = temp->x.version;
  585. sxattr_ptr->offset = temp->x.offset;
  586. sxattr_ptr->totlen = temp->x.totlen;
  587. wpage += JFFS2_SUMMARY_XATTR_SIZE;
  588. break;
  589. }
  590. case JFFS2_NODETYPE_XREF: {
  591. struct jffs2_sum_xref_flash *sxref_ptr = wpage;
  592. temp = c->summary->sum_list_head;
  593. sxref_ptr->nodetype = temp->r.nodetype;
  594. sxref_ptr->offset = temp->r.offset;
  595. wpage += JFFS2_SUMMARY_XREF_SIZE;
  596. break;
  597. }
  598. #endif
  599. default : {
  600. if ((je16_to_cpu(temp->u.nodetype) & JFFS2_COMPAT_MASK)
  601. == JFFS2_FEATURE_RWCOMPAT_COPY) {
  602. dbg_summary("Writing unknown RWCOMPAT_COPY node type %x\n",
  603. je16_to_cpu(temp->u.nodetype));
  604. jffs2_sum_disable_collecting(c->summary);
  605. } else {
  606. BUG(); /* unknown node in summary information */
  607. }
  608. }
  609. }
  610. c->summary->sum_list_head = temp->u.next;
  611. kfree(temp);
  612. c->summary->sum_num--;
  613. }
  614. jffs2_sum_reset_collected(c->summary);
  615. wpage += padsize;
  616. sm = wpage;
  617. sm->offset = cpu_to_je32(c->sector_size - jeb->free_size);
  618. sm->magic = cpu_to_je32(JFFS2_SUM_MAGIC);
  619. isum.sum_crc = cpu_to_je32(crc32(0, c->summary->sum_buf, datasize));
  620. isum.node_crc = cpu_to_je32(crc32(0, &isum, sizeof(isum) - 8));
  621. vecs[0].iov_base = &isum;
  622. vecs[0].iov_len = sizeof(isum);
  623. vecs[1].iov_base = c->summary->sum_buf;
  624. vecs[1].iov_len = datasize;
  625. sum_ofs = jeb->offset + c->sector_size - jeb->free_size;
  626. dbg_summary("JFFS2: writing out data to flash to pos : 0x%08x\n",
  627. sum_ofs);
  628. ret = jffs2_flash_writev(c, vecs, 2, sum_ofs, &retlen, 0);
  629. if (ret || (retlen != infosize)) {
  630. JFFS2_WARNING("Write of %u bytes at 0x%08x failed. returned %d, retlen %zd\n",
  631. infosize, sum_ofs, ret, retlen);
  632. if (retlen) {
  633. /* Waste remaining space */
  634. spin_lock(&c->erase_completion_lock);
  635. jffs2_link_node_ref(c, jeb, sum_ofs | REF_OBSOLETE, infosize, NULL);
  636. spin_unlock(&c->erase_completion_lock);
  637. }
  638. c->summary->sum_size = JFFS2_SUMMARY_NOSUM_SIZE;
  639. return 0;
  640. }
  641. spin_lock(&c->erase_completion_lock);
  642. jffs2_link_node_ref(c, jeb, sum_ofs | REF_NORMAL, infosize, NULL);
  643. spin_unlock(&c->erase_completion_lock);
  644. return 0;
  645. }
  646. /* Write out summary information - called from jffs2_do_reserve_space */
  647. int jffs2_sum_write_sumnode(struct jffs2_sb_info *c)
  648. {
  649. int datasize, infosize, padsize;
  650. struct jffs2_eraseblock *jeb;
  651. int ret;
  652. dbg_summary("called\n");
  653. spin_unlock(&c->erase_completion_lock);
  654. jeb = c->nextblock;
  655. jffs2_prealloc_raw_node_refs(c, jeb, 1);
  656. if (!c->summary->sum_num || !c->summary->sum_list_head) {
  657. JFFS2_WARNING("Empty summary info!!!\n");
  658. BUG();
  659. }
  660. datasize = c->summary->sum_size + sizeof(struct jffs2_sum_marker);
  661. infosize = sizeof(struct jffs2_raw_summary) + datasize;
  662. padsize = jeb->free_size - infosize;
  663. infosize += padsize;
  664. datasize += padsize;
  665. /* Is there enough space for summary? */
  666. if (padsize < 0) {
  667. /* don't try to write out summary for this jeb */
  668. jffs2_sum_disable_collecting(c->summary);
  669. JFFS2_WARNING("Not enough space for summary, padsize = %d\n", padsize);
  670. spin_lock(&c->erase_completion_lock);
  671. return 0;
  672. }
  673. ret = jffs2_sum_write_data(c, jeb, infosize, datasize, padsize);
  674. spin_lock(&c->erase_completion_lock);
  675. return ret;
  676. }