readinode.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  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/fs.h>
  16. #include <linux/crc32.h>
  17. #include <linux/pagemap.h>
  18. #include <linux/mtd/mtd.h>
  19. #include <linux/compiler.h>
  20. #include "nodelist.h"
  21. /*
  22. * Check the data CRC of the node.
  23. *
  24. * Returns: 0 if the data CRC is correct;
  25. * 1 - if incorrect;
  26. * error code if an error occurred.
  27. */
  28. static int check_node_data(struct jffs2_sb_info *c, struct jffs2_tmp_dnode_info *tn)
  29. {
  30. struct jffs2_raw_node_ref *ref = tn->fn->raw;
  31. int err = 0, pointed = 0;
  32. struct jffs2_eraseblock *jeb;
  33. unsigned char *buffer;
  34. uint32_t crc, ofs, len;
  35. size_t retlen;
  36. BUG_ON(tn->csize == 0);
  37. /* Calculate how many bytes were already checked */
  38. ofs = ref_offset(ref) + sizeof(struct jffs2_raw_inode);
  39. len = tn->csize;
  40. if (jffs2_is_writebuffered(c)) {
  41. int adj = ofs % c->wbuf_pagesize;
  42. if (likely(adj))
  43. adj = c->wbuf_pagesize - adj;
  44. if (adj >= tn->csize) {
  45. dbg_readinode("no need to check node at %#08x, data length %u, data starts at %#08x - it has already been checked.\n",
  46. ref_offset(ref), tn->csize, ofs);
  47. goto adj_acc;
  48. }
  49. ofs += adj;
  50. len -= adj;
  51. }
  52. dbg_readinode("check node at %#08x, data length %u, partial CRC %#08x, correct CRC %#08x, data starts at %#08x, start checking from %#08x - %u bytes.\n",
  53. ref_offset(ref), tn->csize, tn->partial_crc, tn->data_crc, ofs - len, ofs, len);
  54. #ifndef __ECOS
  55. /* TODO: instead, incapsulate point() stuff to jffs2_flash_read(),
  56. * adding and jffs2_flash_read_end() interface. */
  57. err = mtd_point(c->mtd, ofs, len, &retlen, (void **)&buffer, NULL);
  58. if (!err && retlen < len) {
  59. JFFS2_WARNING("MTD point returned len too short: %zu instead of %u.\n", retlen, tn->csize);
  60. mtd_unpoint(c->mtd, ofs, retlen);
  61. } else if (err) {
  62. if (err != -EOPNOTSUPP)
  63. JFFS2_WARNING("MTD point failed: error code %d.\n", err);
  64. } else
  65. pointed = 1; /* succefully pointed to device */
  66. #endif
  67. if (!pointed) {
  68. buffer = kmalloc(len, GFP_KERNEL);
  69. if (unlikely(!buffer))
  70. return -ENOMEM;
  71. /* TODO: this is very frequent pattern, make it a separate
  72. * routine */
  73. err = jffs2_flash_read(c, ofs, len, &retlen, buffer);
  74. if (err) {
  75. JFFS2_ERROR("can not read %d bytes from 0x%08x, error code: %d.\n", len, ofs, err);
  76. goto free_out;
  77. }
  78. if (retlen != len) {
  79. JFFS2_ERROR("short read at %#08x: %zd instead of %d.\n", ofs, retlen, len);
  80. err = -EIO;
  81. goto free_out;
  82. }
  83. }
  84. /* Continue calculating CRC */
  85. crc = crc32(tn->partial_crc, buffer, len);
  86. if(!pointed)
  87. kfree(buffer);
  88. #ifndef __ECOS
  89. else
  90. mtd_unpoint(c->mtd, ofs, len);
  91. #endif
  92. if (crc != tn->data_crc) {
  93. JFFS2_NOTICE("wrong data CRC in data node at 0x%08x: read %#08x, calculated %#08x.\n",
  94. ref_offset(ref), tn->data_crc, crc);
  95. return 1;
  96. }
  97. adj_acc:
  98. jeb = &c->blocks[ref->flash_offset / c->sector_size];
  99. len = ref_totlen(c, jeb, ref);
  100. /* If it should be REF_NORMAL, it'll get marked as such when
  101. we build the fragtree, shortly. No need to worry about GC
  102. moving it while it's marked REF_PRISTINE -- GC won't happen
  103. till we've finished checking every inode anyway. */
  104. ref->flash_offset |= REF_PRISTINE;
  105. /*
  106. * Mark the node as having been checked and fix the
  107. * accounting accordingly.
  108. */
  109. spin_lock(&c->erase_completion_lock);
  110. jeb->used_size += len;
  111. jeb->unchecked_size -= len;
  112. c->used_size += len;
  113. c->unchecked_size -= len;
  114. jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
  115. spin_unlock(&c->erase_completion_lock);
  116. return 0;
  117. free_out:
  118. if(!pointed)
  119. kfree(buffer);
  120. #ifndef __ECOS
  121. else
  122. mtd_unpoint(c->mtd, ofs, len);
  123. #endif
  124. return err;
  125. }
  126. /*
  127. * Helper function for jffs2_add_older_frag_to_fragtree().
  128. *
  129. * Checks the node if we are in the checking stage.
  130. */
  131. static int check_tn_node(struct jffs2_sb_info *c, struct jffs2_tmp_dnode_info *tn)
  132. {
  133. int ret;
  134. BUG_ON(ref_obsolete(tn->fn->raw));
  135. /* We only check the data CRC of unchecked nodes */
  136. if (ref_flags(tn->fn->raw) != REF_UNCHECKED)
  137. return 0;
  138. dbg_readinode("check node %#04x-%#04x, phys offs %#08x\n",
  139. tn->fn->ofs, tn->fn->ofs + tn->fn->size, ref_offset(tn->fn->raw));
  140. ret = check_node_data(c, tn);
  141. if (unlikely(ret < 0)) {
  142. JFFS2_ERROR("check_node_data() returned error: %d.\n",
  143. ret);
  144. } else if (unlikely(ret > 0)) {
  145. dbg_readinode("CRC error, mark it obsolete.\n");
  146. jffs2_mark_node_obsolete(c, tn->fn->raw);
  147. }
  148. return ret;
  149. }
  150. static struct jffs2_tmp_dnode_info *jffs2_lookup_tn(struct rb_root *tn_root, uint32_t offset)
  151. {
  152. struct rb_node *next;
  153. struct jffs2_tmp_dnode_info *tn = NULL;
  154. dbg_readinode("root %p, offset %d\n", tn_root, offset);
  155. next = tn_root->rb_node;
  156. while (next) {
  157. tn = rb_entry(next, struct jffs2_tmp_dnode_info, rb);
  158. if (tn->fn->ofs < offset)
  159. next = tn->rb.rb_right;
  160. else if (tn->fn->ofs >= offset)
  161. next = tn->rb.rb_left;
  162. else
  163. break;
  164. }
  165. return tn;
  166. }
  167. static void jffs2_kill_tn(struct jffs2_sb_info *c, struct jffs2_tmp_dnode_info *tn)
  168. {
  169. jffs2_mark_node_obsolete(c, tn->fn->raw);
  170. jffs2_free_full_dnode(tn->fn);
  171. jffs2_free_tmp_dnode_info(tn);
  172. }
  173. /*
  174. * This function is used when we read an inode. Data nodes arrive in
  175. * arbitrary order -- they may be older or newer than the nodes which
  176. * are already in the tree. Where overlaps occur, the older node can
  177. * be discarded as long as the newer passes the CRC check. We don't
  178. * bother to keep track of holes in this rbtree, and neither do we deal
  179. * with frags -- we can have multiple entries starting at the same
  180. * offset, and the one with the smallest length will come first in the
  181. * ordering.
  182. *
  183. * Returns 0 if the node was handled (including marking it obsolete)
  184. * < 0 an if error occurred
  185. */
  186. static int jffs2_add_tn_to_tree(struct jffs2_sb_info *c,
  187. struct jffs2_readinode_info *rii,
  188. struct jffs2_tmp_dnode_info *tn)
  189. {
  190. uint32_t fn_end = tn->fn->ofs + tn->fn->size;
  191. struct jffs2_tmp_dnode_info *this, *ptn;
  192. dbg_readinode("insert fragment %#04x-%#04x, ver %u at %08x\n", tn->fn->ofs, fn_end, tn->version, ref_offset(tn->fn->raw));
  193. /* If a node has zero dsize, we only have to keep it if it might be the
  194. node with highest version -- i.e. the one which will end up as f->metadata.
  195. Note that such nodes won't be REF_UNCHECKED since there are no data to
  196. check anyway. */
  197. if (!tn->fn->size) {
  198. if (rii->mdata_tn) {
  199. if (rii->mdata_tn->version < tn->version) {
  200. /* We had a candidate mdata node already */
  201. dbg_readinode("kill old mdata with ver %d\n", rii->mdata_tn->version);
  202. jffs2_kill_tn(c, rii->mdata_tn);
  203. } else {
  204. dbg_readinode("kill new mdata with ver %d (older than existing %d\n",
  205. tn->version, rii->mdata_tn->version);
  206. jffs2_kill_tn(c, tn);
  207. return 0;
  208. }
  209. }
  210. rii->mdata_tn = tn;
  211. dbg_readinode("keep new mdata with ver %d\n", tn->version);
  212. return 0;
  213. }
  214. /* Find the earliest node which _may_ be relevant to this one */
  215. this = jffs2_lookup_tn(&rii->tn_root, tn->fn->ofs);
  216. if (this) {
  217. /* If the node is coincident with another at a lower address,
  218. back up until the other node is found. It may be relevant */
  219. while (this->overlapped) {
  220. ptn = tn_prev(this);
  221. if (!ptn) {
  222. /*
  223. * We killed a node which set the overlapped
  224. * flags during the scan. Fix it up.
  225. */
  226. this->overlapped = 0;
  227. break;
  228. }
  229. this = ptn;
  230. }
  231. dbg_readinode("'this' found %#04x-%#04x (%s)\n", this->fn->ofs, this->fn->ofs + this->fn->size, this->fn ? "data" : "hole");
  232. }
  233. while (this) {
  234. if (this->fn->ofs > fn_end)
  235. break;
  236. dbg_readinode("Ponder this ver %d, 0x%x-0x%x\n",
  237. this->version, this->fn->ofs, this->fn->size);
  238. if (this->version == tn->version) {
  239. /* Version number collision means REF_PRISTINE GC. Accept either of them
  240. as long as the CRC is correct. Check the one we have already... */
  241. if (!check_tn_node(c, this)) {
  242. /* The one we already had was OK. Keep it and throw away the new one */
  243. dbg_readinode("Like old node. Throw away new\n");
  244. jffs2_kill_tn(c, tn);
  245. return 0;
  246. } else {
  247. /* Who cares if the new one is good; keep it for now anyway. */
  248. dbg_readinode("Like new node. Throw away old\n");
  249. rb_replace_node(&this->rb, &tn->rb, &rii->tn_root);
  250. jffs2_kill_tn(c, this);
  251. /* Same overlapping from in front and behind */
  252. return 0;
  253. }
  254. }
  255. if (this->version < tn->version &&
  256. this->fn->ofs >= tn->fn->ofs &&
  257. this->fn->ofs + this->fn->size <= fn_end) {
  258. /* New node entirely overlaps 'this' */
  259. if (check_tn_node(c, tn)) {
  260. dbg_readinode("new node bad CRC\n");
  261. jffs2_kill_tn(c, tn);
  262. return 0;
  263. }
  264. /* ... and is good. Kill 'this' and any subsequent nodes which are also overlapped */
  265. while (this && this->fn->ofs + this->fn->size <= fn_end) {
  266. struct jffs2_tmp_dnode_info *next = tn_next(this);
  267. if (this->version < tn->version) {
  268. tn_erase(this, &rii->tn_root);
  269. dbg_readinode("Kill overlapped ver %d, 0x%x-0x%x\n",
  270. this->version, this->fn->ofs,
  271. this->fn->ofs+this->fn->size);
  272. jffs2_kill_tn(c, this);
  273. }
  274. this = next;
  275. }
  276. dbg_readinode("Done killing overlapped nodes\n");
  277. continue;
  278. }
  279. if (this->version > tn->version &&
  280. this->fn->ofs <= tn->fn->ofs &&
  281. this->fn->ofs+this->fn->size >= fn_end) {
  282. /* New node entirely overlapped by 'this' */
  283. if (!check_tn_node(c, this)) {
  284. dbg_readinode("Good CRC on old node. Kill new\n");
  285. jffs2_kill_tn(c, tn);
  286. return 0;
  287. }
  288. /* ... but 'this' was bad. Replace it... */
  289. dbg_readinode("Bad CRC on old overlapping node. Kill it\n");
  290. tn_erase(this, &rii->tn_root);
  291. jffs2_kill_tn(c, this);
  292. break;
  293. }
  294. this = tn_next(this);
  295. }
  296. /* We neither completely obsoleted nor were completely
  297. obsoleted by an earlier node. Insert into the tree */
  298. {
  299. struct rb_node *parent;
  300. struct rb_node **link = &rii->tn_root.rb_node;
  301. struct jffs2_tmp_dnode_info *insert_point = NULL;
  302. while (*link) {
  303. parent = *link;
  304. insert_point = rb_entry(parent, struct jffs2_tmp_dnode_info, rb);
  305. if (tn->fn->ofs > insert_point->fn->ofs)
  306. link = &insert_point->rb.rb_right;
  307. else if (tn->fn->ofs < insert_point->fn->ofs ||
  308. tn->fn->size < insert_point->fn->size)
  309. link = &insert_point->rb.rb_left;
  310. else
  311. link = &insert_point->rb.rb_right;
  312. }
  313. rb_link_node(&tn->rb, &insert_point->rb, link);
  314. rb_insert_color(&tn->rb, &rii->tn_root);
  315. }
  316. /* If there's anything behind that overlaps us, note it */
  317. this = tn_prev(tn);
  318. if (this) {
  319. while (1) {
  320. if (this->fn->ofs + this->fn->size > tn->fn->ofs) {
  321. dbg_readinode("Node is overlapped by %p (v %d, 0x%x-0x%x)\n",
  322. this, this->version, this->fn->ofs,
  323. this->fn->ofs+this->fn->size);
  324. tn->overlapped = 1;
  325. break;
  326. }
  327. if (!this->overlapped)
  328. break;
  329. ptn = tn_prev(this);
  330. if (!ptn) {
  331. /*
  332. * We killed a node which set the overlapped
  333. * flags during the scan. Fix it up.
  334. */
  335. this->overlapped = 0;
  336. break;
  337. }
  338. this = ptn;
  339. }
  340. }
  341. /* If the new node overlaps anything ahead, note it */
  342. this = tn_next(tn);
  343. while (this && this->fn->ofs < fn_end) {
  344. this->overlapped = 1;
  345. dbg_readinode("Node ver %d, 0x%x-0x%x is overlapped\n",
  346. this->version, this->fn->ofs,
  347. this->fn->ofs+this->fn->size);
  348. this = tn_next(this);
  349. }
  350. return 0;
  351. }
  352. /* Trivial function to remove the last node in the tree. Which by definition
  353. has no right-hand child — so can be removed just by making its left-hand
  354. child (if any) take its place under its parent. Since this is only done
  355. when we're consuming the whole tree, there's no need to use rb_erase()
  356. and let it worry about adjusting colours and balancing the tree. That
  357. would just be a waste of time. */
  358. static void eat_last(struct rb_root *root, struct rb_node *node)
  359. {
  360. struct rb_node *parent = rb_parent(node);
  361. struct rb_node **link;
  362. /* LAST! */
  363. BUG_ON(node->rb_right);
  364. if (!parent)
  365. link = &root->rb_node;
  366. else if (node == parent->rb_left)
  367. link = &parent->rb_left;
  368. else
  369. link = &parent->rb_right;
  370. *link = node->rb_left;
  371. if (node->rb_left)
  372. node->rb_left->__rb_parent_color = node->__rb_parent_color;
  373. }
  374. /* We put the version tree in reverse order, so we can use the same eat_last()
  375. function that we use to consume the tmpnode tree (tn_root). */
  376. static void ver_insert(struct rb_root *ver_root, struct jffs2_tmp_dnode_info *tn)
  377. {
  378. struct rb_node **link = &ver_root->rb_node;
  379. struct rb_node *parent = NULL;
  380. struct jffs2_tmp_dnode_info *this_tn;
  381. while (*link) {
  382. parent = *link;
  383. this_tn = rb_entry(parent, struct jffs2_tmp_dnode_info, rb);
  384. if (tn->version > this_tn->version)
  385. link = &parent->rb_left;
  386. else
  387. link = &parent->rb_right;
  388. }
  389. dbg_readinode("Link new node at %p (root is %p)\n", link, ver_root);
  390. rb_link_node(&tn->rb, parent, link);
  391. rb_insert_color(&tn->rb, ver_root);
  392. }
  393. /* Build final, normal fragtree from tn tree. It doesn't matter which order
  394. we add nodes to the real fragtree, as long as they don't overlap. And
  395. having thrown away the majority of overlapped nodes as we went, there
  396. really shouldn't be many sets of nodes which do overlap. If we start at
  397. the end, we can use the overlap markers -- we can just eat nodes which
  398. aren't overlapped, and when we encounter nodes which _do_ overlap we
  399. sort them all into a temporary tree in version order before replaying them. */
  400. static int jffs2_build_inode_fragtree(struct jffs2_sb_info *c,
  401. struct jffs2_inode_info *f,
  402. struct jffs2_readinode_info *rii)
  403. {
  404. struct jffs2_tmp_dnode_info *pen, *last, *this;
  405. struct rb_root ver_root = RB_ROOT;
  406. uint32_t high_ver = 0;
  407. if (rii->mdata_tn) {
  408. dbg_readinode("potential mdata is ver %d at %p\n", rii->mdata_tn->version, rii->mdata_tn);
  409. high_ver = rii->mdata_tn->version;
  410. rii->latest_ref = rii->mdata_tn->fn->raw;
  411. }
  412. #ifdef JFFS2_DBG_READINODE_MESSAGES
  413. this = tn_last(&rii->tn_root);
  414. while (this) {
  415. dbg_readinode("tn %p ver %d range 0x%x-0x%x ov %d\n", this, this->version, this->fn->ofs,
  416. this->fn->ofs+this->fn->size, this->overlapped);
  417. this = tn_prev(this);
  418. }
  419. #endif
  420. pen = tn_last(&rii->tn_root);
  421. while ((last = pen)) {
  422. pen = tn_prev(last);
  423. eat_last(&rii->tn_root, &last->rb);
  424. ver_insert(&ver_root, last);
  425. if (unlikely(last->overlapped)) {
  426. if (pen)
  427. continue;
  428. /*
  429. * We killed a node which set the overlapped
  430. * flags during the scan. Fix it up.
  431. */
  432. last->overlapped = 0;
  433. }
  434. /* Now we have a bunch of nodes in reverse version
  435. order, in the tree at ver_root. Most of the time,
  436. there'll actually be only one node in the 'tree',
  437. in fact. */
  438. this = tn_last(&ver_root);
  439. while (this) {
  440. struct jffs2_tmp_dnode_info *vers_next;
  441. int ret;
  442. vers_next = tn_prev(this);
  443. eat_last(&ver_root, &this->rb);
  444. if (check_tn_node(c, this)) {
  445. dbg_readinode("node ver %d, 0x%x-0x%x failed CRC\n",
  446. this->version, this->fn->ofs,
  447. this->fn->ofs+this->fn->size);
  448. jffs2_kill_tn(c, this);
  449. } else {
  450. if (this->version > high_ver) {
  451. /* Note that this is different from the other
  452. highest_version, because this one is only
  453. counting _valid_ nodes which could give the
  454. latest inode metadata */
  455. high_ver = this->version;
  456. rii->latest_ref = this->fn->raw;
  457. }
  458. dbg_readinode("Add %p (v %d, 0x%x-0x%x, ov %d) to fragtree\n",
  459. this, this->version, this->fn->ofs,
  460. this->fn->ofs+this->fn->size, this->overlapped);
  461. ret = jffs2_add_full_dnode_to_inode(c, f, this->fn);
  462. if (ret) {
  463. /* Free the nodes in vers_root; let the caller
  464. deal with the rest */
  465. JFFS2_ERROR("Add node to tree failed %d\n", ret);
  466. while (1) {
  467. vers_next = tn_prev(this);
  468. if (check_tn_node(c, this))
  469. jffs2_mark_node_obsolete(c, this->fn->raw);
  470. jffs2_free_full_dnode(this->fn);
  471. jffs2_free_tmp_dnode_info(this);
  472. this = vers_next;
  473. if (!this)
  474. break;
  475. eat_last(&ver_root, &vers_next->rb);
  476. }
  477. return ret;
  478. }
  479. jffs2_free_tmp_dnode_info(this);
  480. }
  481. this = vers_next;
  482. }
  483. }
  484. return 0;
  485. }
  486. static void jffs2_free_tmp_dnode_info_list(struct rb_root *list)
  487. {
  488. struct jffs2_tmp_dnode_info *tn, *next;
  489. rbtree_postorder_for_each_entry_safe(tn, next, list, rb) {
  490. jffs2_free_full_dnode(tn->fn);
  491. jffs2_free_tmp_dnode_info(tn);
  492. }
  493. *list = RB_ROOT;
  494. }
  495. static void jffs2_free_full_dirent_list(struct jffs2_full_dirent *fd)
  496. {
  497. struct jffs2_full_dirent *next;
  498. while (fd) {
  499. next = fd->next;
  500. jffs2_free_full_dirent(fd);
  501. fd = next;
  502. }
  503. }
  504. /* Returns first valid node after 'ref'. May return 'ref' */
  505. static struct jffs2_raw_node_ref *jffs2_first_valid_node(struct jffs2_raw_node_ref *ref)
  506. {
  507. while (ref && ref->next_in_ino) {
  508. if (!ref_obsolete(ref))
  509. return ref;
  510. dbg_noderef("node at 0x%08x is obsoleted. Ignoring.\n", ref_offset(ref));
  511. ref = ref->next_in_ino;
  512. }
  513. return NULL;
  514. }
  515. /*
  516. * Helper function for jffs2_get_inode_nodes().
  517. * It is called every time an directory entry node is found.
  518. *
  519. * Returns: 0 on success;
  520. * negative error code on failure.
  521. */
  522. static inline int read_direntry(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref,
  523. struct jffs2_raw_dirent *rd, size_t read,
  524. struct jffs2_readinode_info *rii)
  525. {
  526. struct jffs2_full_dirent *fd;
  527. uint32_t crc;
  528. /* Obsoleted. This cannot happen, surely? dwmw2 20020308 */
  529. BUG_ON(ref_obsolete(ref));
  530. crc = crc32(0, rd, sizeof(*rd) - 8);
  531. if (unlikely(crc != je32_to_cpu(rd->node_crc))) {
  532. JFFS2_NOTICE("header CRC failed on dirent node at %#08x: read %#08x, calculated %#08x\n",
  533. ref_offset(ref), je32_to_cpu(rd->node_crc), crc);
  534. jffs2_mark_node_obsolete(c, ref);
  535. return 0;
  536. }
  537. /* If we've never checked the CRCs on this node, check them now */
  538. if (ref_flags(ref) == REF_UNCHECKED) {
  539. struct jffs2_eraseblock *jeb;
  540. int len;
  541. /* Sanity check */
  542. if (unlikely(PAD((rd->nsize + sizeof(*rd))) != PAD(je32_to_cpu(rd->totlen)))) {
  543. JFFS2_ERROR("illegal nsize in node at %#08x: nsize %#02x, totlen %#04x\n",
  544. ref_offset(ref), rd->nsize, je32_to_cpu(rd->totlen));
  545. jffs2_mark_node_obsolete(c, ref);
  546. return 0;
  547. }
  548. jeb = &c->blocks[ref->flash_offset / c->sector_size];
  549. len = ref_totlen(c, jeb, ref);
  550. spin_lock(&c->erase_completion_lock);
  551. jeb->used_size += len;
  552. jeb->unchecked_size -= len;
  553. c->used_size += len;
  554. c->unchecked_size -= len;
  555. ref->flash_offset = ref_offset(ref) | dirent_node_state(rd);
  556. spin_unlock(&c->erase_completion_lock);
  557. }
  558. fd = jffs2_alloc_full_dirent(rd->nsize + 1);
  559. if (unlikely(!fd))
  560. return -ENOMEM;
  561. fd->raw = ref;
  562. fd->version = je32_to_cpu(rd->version);
  563. fd->ino = je32_to_cpu(rd->ino);
  564. fd->type = rd->type;
  565. if (fd->version > rii->highest_version)
  566. rii->highest_version = fd->version;
  567. /* Pick out the mctime of the latest dirent */
  568. if(fd->version > rii->mctime_ver && je32_to_cpu(rd->mctime)) {
  569. rii->mctime_ver = fd->version;
  570. rii->latest_mctime = je32_to_cpu(rd->mctime);
  571. }
  572. /*
  573. * Copy as much of the name as possible from the raw
  574. * dirent we've already read from the flash.
  575. */
  576. if (read > sizeof(*rd))
  577. memcpy(&fd->name[0], &rd->name[0],
  578. min_t(uint32_t, rd->nsize, (read - sizeof(*rd)) ));
  579. /* Do we need to copy any more of the name directly from the flash? */
  580. if (rd->nsize + sizeof(*rd) > read) {
  581. /* FIXME: point() */
  582. int err;
  583. int already = read - sizeof(*rd);
  584. err = jffs2_flash_read(c, (ref_offset(ref)) + read,
  585. rd->nsize - already, &read, &fd->name[already]);
  586. if (unlikely(read != rd->nsize - already) && likely(!err)) {
  587. jffs2_free_full_dirent(fd);
  588. JFFS2_ERROR("short read: wanted %d bytes, got %zd\n",
  589. rd->nsize - already, read);
  590. return -EIO;
  591. }
  592. if (unlikely(err)) {
  593. JFFS2_ERROR("read remainder of name: error %d\n", err);
  594. jffs2_free_full_dirent(fd);
  595. return -EIO;
  596. }
  597. }
  598. fd->nhash = full_name_hash(fd->name, rd->nsize);
  599. fd->next = NULL;
  600. fd->name[rd->nsize] = '\0';
  601. /*
  602. * Wheee. We now have a complete jffs2_full_dirent structure, with
  603. * the name in it and everything. Link it into the list
  604. */
  605. jffs2_add_fd_to_list(c, fd, &rii->fds);
  606. return 0;
  607. }
  608. /*
  609. * Helper function for jffs2_get_inode_nodes().
  610. * It is called every time an inode node is found.
  611. *
  612. * Returns: 0 on success (possibly after marking a bad node obsolete);
  613. * negative error code on failure.
  614. */
  615. static inline int read_dnode(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref,
  616. struct jffs2_raw_inode *rd, int rdlen,
  617. struct jffs2_readinode_info *rii)
  618. {
  619. struct jffs2_tmp_dnode_info *tn;
  620. uint32_t len, csize;
  621. int ret = 0;
  622. uint32_t crc;
  623. /* Obsoleted. This cannot happen, surely? dwmw2 20020308 */
  624. BUG_ON(ref_obsolete(ref));
  625. crc = crc32(0, rd, sizeof(*rd) - 8);
  626. if (unlikely(crc != je32_to_cpu(rd->node_crc))) {
  627. JFFS2_NOTICE("node CRC failed on dnode at %#08x: read %#08x, calculated %#08x\n",
  628. ref_offset(ref), je32_to_cpu(rd->node_crc), crc);
  629. jffs2_mark_node_obsolete(c, ref);
  630. return 0;
  631. }
  632. tn = jffs2_alloc_tmp_dnode_info();
  633. if (!tn) {
  634. JFFS2_ERROR("failed to allocate tn (%zu bytes).\n", sizeof(*tn));
  635. return -ENOMEM;
  636. }
  637. tn->partial_crc = 0;
  638. csize = je32_to_cpu(rd->csize);
  639. /* If we've never checked the CRCs on this node, check them now */
  640. if (ref_flags(ref) == REF_UNCHECKED) {
  641. /* Sanity checks */
  642. if (unlikely(je32_to_cpu(rd->offset) > je32_to_cpu(rd->isize)) ||
  643. unlikely(PAD(je32_to_cpu(rd->csize) + sizeof(*rd)) != PAD(je32_to_cpu(rd->totlen)))) {
  644. JFFS2_WARNING("inode node header CRC is corrupted at %#08x\n", ref_offset(ref));
  645. jffs2_dbg_dump_node(c, ref_offset(ref));
  646. jffs2_mark_node_obsolete(c, ref);
  647. goto free_out;
  648. }
  649. if (jffs2_is_writebuffered(c) && csize != 0) {
  650. /* At this point we are supposed to check the data CRC
  651. * of our unchecked node. But thus far, we do not
  652. * know whether the node is valid or obsolete. To
  653. * figure this out, we need to walk all the nodes of
  654. * the inode and build the inode fragtree. We don't
  655. * want to spend time checking data of nodes which may
  656. * later be found to be obsolete. So we put off the full
  657. * data CRC checking until we have read all the inode
  658. * nodes and have started building the fragtree.
  659. *
  660. * The fragtree is being built starting with nodes
  661. * having the highest version number, so we'll be able
  662. * to detect whether a node is valid (i.e., it is not
  663. * overlapped by a node with higher version) or not.
  664. * And we'll be able to check only those nodes, which
  665. * are not obsolete.
  666. *
  667. * Of course, this optimization only makes sense in case
  668. * of NAND flashes (or other flashes with
  669. * !jffs2_can_mark_obsolete()), since on NOR flashes
  670. * nodes are marked obsolete physically.
  671. *
  672. * Since NAND flashes (or other flashes with
  673. * jffs2_is_writebuffered(c)) are anyway read by
  674. * fractions of c->wbuf_pagesize, and we have just read
  675. * the node header, it is likely that the starting part
  676. * of the node data is also read when we read the
  677. * header. So we don't mind to check the CRC of the
  678. * starting part of the data of the node now, and check
  679. * the second part later (in jffs2_check_node_data()).
  680. * Of course, we will not need to re-read and re-check
  681. * the NAND page which we have just read. This is why we
  682. * read the whole NAND page at jffs2_get_inode_nodes(),
  683. * while we needed only the node header.
  684. */
  685. unsigned char *buf;
  686. /* 'buf' will point to the start of data */
  687. buf = (unsigned char *)rd + sizeof(*rd);
  688. /* len will be the read data length */
  689. len = min_t(uint32_t, rdlen - sizeof(*rd), csize);
  690. tn->partial_crc = crc32(0, buf, len);
  691. dbg_readinode("Calculates CRC (%#08x) for %d bytes, csize %d\n", tn->partial_crc, len, csize);
  692. /* If we actually calculated the whole data CRC
  693. * and it is wrong, drop the node. */
  694. if (len >= csize && unlikely(tn->partial_crc != je32_to_cpu(rd->data_crc))) {
  695. JFFS2_NOTICE("wrong data CRC in data node at 0x%08x: read %#08x, calculated %#08x.\n",
  696. ref_offset(ref), tn->partial_crc, je32_to_cpu(rd->data_crc));
  697. jffs2_mark_node_obsolete(c, ref);
  698. goto free_out;
  699. }
  700. } else if (csize == 0) {
  701. /*
  702. * We checked the header CRC. If the node has no data, adjust
  703. * the space accounting now. For other nodes this will be done
  704. * later either when the node is marked obsolete or when its
  705. * data is checked.
  706. */
  707. struct jffs2_eraseblock *jeb;
  708. dbg_readinode("the node has no data.\n");
  709. jeb = &c->blocks[ref->flash_offset / c->sector_size];
  710. len = ref_totlen(c, jeb, ref);
  711. spin_lock(&c->erase_completion_lock);
  712. jeb->used_size += len;
  713. jeb->unchecked_size -= len;
  714. c->used_size += len;
  715. c->unchecked_size -= len;
  716. ref->flash_offset = ref_offset(ref) | REF_NORMAL;
  717. spin_unlock(&c->erase_completion_lock);
  718. }
  719. }
  720. tn->fn = jffs2_alloc_full_dnode();
  721. if (!tn->fn) {
  722. JFFS2_ERROR("alloc fn failed\n");
  723. ret = -ENOMEM;
  724. goto free_out;
  725. }
  726. tn->version = je32_to_cpu(rd->version);
  727. tn->fn->ofs = je32_to_cpu(rd->offset);
  728. tn->data_crc = je32_to_cpu(rd->data_crc);
  729. tn->csize = csize;
  730. tn->fn->raw = ref;
  731. tn->overlapped = 0;
  732. if (tn->version > rii->highest_version)
  733. rii->highest_version = tn->version;
  734. /* There was a bug where we wrote hole nodes out with
  735. csize/dsize swapped. Deal with it */
  736. if (rd->compr == JFFS2_COMPR_ZERO && !je32_to_cpu(rd->dsize) && csize)
  737. tn->fn->size = csize;
  738. else // normal case...
  739. tn->fn->size = je32_to_cpu(rd->dsize);
  740. dbg_readinode2("dnode @%08x: ver %u, offset %#04x, dsize %#04x, csize %#04x\n",
  741. ref_offset(ref), je32_to_cpu(rd->version),
  742. je32_to_cpu(rd->offset), je32_to_cpu(rd->dsize), csize);
  743. ret = jffs2_add_tn_to_tree(c, rii, tn);
  744. if (ret) {
  745. jffs2_free_full_dnode(tn->fn);
  746. free_out:
  747. jffs2_free_tmp_dnode_info(tn);
  748. return ret;
  749. }
  750. #ifdef JFFS2_DBG_READINODE2_MESSAGES
  751. dbg_readinode2("After adding ver %d:\n", je32_to_cpu(rd->version));
  752. tn = tn_first(&rii->tn_root);
  753. while (tn) {
  754. dbg_readinode2("%p: v %d r 0x%x-0x%x ov %d\n",
  755. tn, tn->version, tn->fn->ofs,
  756. tn->fn->ofs+tn->fn->size, tn->overlapped);
  757. tn = tn_next(tn);
  758. }
  759. #endif
  760. return 0;
  761. }
  762. /*
  763. * Helper function for jffs2_get_inode_nodes().
  764. * It is called every time an unknown node is found.
  765. *
  766. * Returns: 0 on success;
  767. * negative error code on failure.
  768. */
  769. static inline int read_unknown(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref, struct jffs2_unknown_node *un)
  770. {
  771. /* We don't mark unknown nodes as REF_UNCHECKED */
  772. if (ref_flags(ref) == REF_UNCHECKED) {
  773. JFFS2_ERROR("REF_UNCHECKED but unknown node at %#08x\n",
  774. ref_offset(ref));
  775. JFFS2_ERROR("Node is {%04x,%04x,%08x,%08x}. Please report this error.\n",
  776. je16_to_cpu(un->magic), je16_to_cpu(un->nodetype),
  777. je32_to_cpu(un->totlen), je32_to_cpu(un->hdr_crc));
  778. jffs2_mark_node_obsolete(c, ref);
  779. return 0;
  780. }
  781. un->nodetype = cpu_to_je16(JFFS2_NODE_ACCURATE | je16_to_cpu(un->nodetype));
  782. switch(je16_to_cpu(un->nodetype) & JFFS2_COMPAT_MASK) {
  783. case JFFS2_FEATURE_INCOMPAT:
  784. JFFS2_ERROR("unknown INCOMPAT nodetype %#04X at %#08x\n",
  785. je16_to_cpu(un->nodetype), ref_offset(ref));
  786. /* EEP */
  787. BUG();
  788. break;
  789. case JFFS2_FEATURE_ROCOMPAT:
  790. JFFS2_ERROR("unknown ROCOMPAT nodetype %#04X at %#08x\n",
  791. je16_to_cpu(un->nodetype), ref_offset(ref));
  792. BUG_ON(!(c->flags & JFFS2_SB_FLAG_RO));
  793. break;
  794. case JFFS2_FEATURE_RWCOMPAT_COPY:
  795. JFFS2_NOTICE("unknown RWCOMPAT_COPY nodetype %#04X at %#08x\n",
  796. je16_to_cpu(un->nodetype), ref_offset(ref));
  797. break;
  798. case JFFS2_FEATURE_RWCOMPAT_DELETE:
  799. JFFS2_NOTICE("unknown RWCOMPAT_DELETE nodetype %#04X at %#08x\n",
  800. je16_to_cpu(un->nodetype), ref_offset(ref));
  801. jffs2_mark_node_obsolete(c, ref);
  802. return 0;
  803. }
  804. return 0;
  805. }
  806. /*
  807. * Helper function for jffs2_get_inode_nodes().
  808. * The function detects whether more data should be read and reads it if yes.
  809. *
  810. * Returns: 0 on success;
  811. * negative error code on failure.
  812. */
  813. static int read_more(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref,
  814. int needed_len, int *rdlen, unsigned char *buf)
  815. {
  816. int err, to_read = needed_len - *rdlen;
  817. size_t retlen;
  818. uint32_t offs;
  819. if (jffs2_is_writebuffered(c)) {
  820. int rem = to_read % c->wbuf_pagesize;
  821. if (rem)
  822. to_read += c->wbuf_pagesize - rem;
  823. }
  824. /* We need to read more data */
  825. offs = ref_offset(ref) + *rdlen;
  826. dbg_readinode("read more %d bytes\n", to_read);
  827. err = jffs2_flash_read(c, offs, to_read, &retlen, buf + *rdlen);
  828. if (err) {
  829. JFFS2_ERROR("can not read %d bytes from 0x%08x, "
  830. "error code: %d.\n", to_read, offs, err);
  831. return err;
  832. }
  833. if (retlen < to_read) {
  834. JFFS2_ERROR("short read at %#08x: %zu instead of %d.\n",
  835. offs, retlen, to_read);
  836. return -EIO;
  837. }
  838. *rdlen += to_read;
  839. return 0;
  840. }
  841. /* Get tmp_dnode_info and full_dirent for all non-obsolete nodes associated
  842. with this ino. Perform a preliminary ordering on data nodes, throwing away
  843. those which are completely obsoleted by newer ones. The naïve approach we
  844. use to take of just returning them _all_ in version order will cause us to
  845. run out of memory in certain degenerate cases. */
  846. static int jffs2_get_inode_nodes(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
  847. struct jffs2_readinode_info *rii)
  848. {
  849. struct jffs2_raw_node_ref *ref, *valid_ref;
  850. unsigned char *buf = NULL;
  851. union jffs2_node_union *node;
  852. size_t retlen;
  853. int len, err;
  854. rii->mctime_ver = 0;
  855. dbg_readinode("ino #%u\n", f->inocache->ino);
  856. /* FIXME: in case of NOR and available ->point() this
  857. * needs to be fixed. */
  858. len = sizeof(union jffs2_node_union) + c->wbuf_pagesize;
  859. buf = kmalloc(len, GFP_KERNEL);
  860. if (!buf)
  861. return -ENOMEM;
  862. spin_lock(&c->erase_completion_lock);
  863. valid_ref = jffs2_first_valid_node(f->inocache->nodes);
  864. if (!valid_ref && f->inocache->ino != 1)
  865. JFFS2_WARNING("Eep. No valid nodes for ino #%u.\n", f->inocache->ino);
  866. while (valid_ref) {
  867. /* We can hold a pointer to a non-obsolete node without the spinlock,
  868. but _obsolete_ nodes may disappear at any time, if the block
  869. they're in gets erased. So if we mark 'ref' obsolete while we're
  870. not holding the lock, it can go away immediately. For that reason,
  871. we find the next valid node first, before processing 'ref'.
  872. */
  873. ref = valid_ref;
  874. valid_ref = jffs2_first_valid_node(ref->next_in_ino);
  875. spin_unlock(&c->erase_completion_lock);
  876. cond_resched();
  877. /*
  878. * At this point we don't know the type of the node we're going
  879. * to read, so we do not know the size of its header. In order
  880. * to minimize the amount of flash IO we assume the header is
  881. * of size = JFFS2_MIN_NODE_HEADER.
  882. */
  883. len = JFFS2_MIN_NODE_HEADER;
  884. if (jffs2_is_writebuffered(c)) {
  885. int end, rem;
  886. /*
  887. * We are about to read JFFS2_MIN_NODE_HEADER bytes,
  888. * but this flash has some minimal I/O unit. It is
  889. * possible that we'll need to read more soon, so read
  890. * up to the next min. I/O unit, in order not to
  891. * re-read the same min. I/O unit twice.
  892. */
  893. end = ref_offset(ref) + len;
  894. rem = end % c->wbuf_pagesize;
  895. if (rem)
  896. end += c->wbuf_pagesize - rem;
  897. len = end - ref_offset(ref);
  898. }
  899. dbg_readinode("read %d bytes at %#08x(%d).\n", len, ref_offset(ref), ref_flags(ref));
  900. /* FIXME: point() */
  901. err = jffs2_flash_read(c, ref_offset(ref), len, &retlen, buf);
  902. if (err) {
  903. JFFS2_ERROR("can not read %d bytes from 0x%08x, error code: %d.\n", len, ref_offset(ref), err);
  904. goto free_out;
  905. }
  906. if (retlen < len) {
  907. JFFS2_ERROR("short read at %#08x: %zu instead of %d.\n", ref_offset(ref), retlen, len);
  908. err = -EIO;
  909. goto free_out;
  910. }
  911. node = (union jffs2_node_union *)buf;
  912. /* No need to mask in the valid bit; it shouldn't be invalid */
  913. if (je32_to_cpu(node->u.hdr_crc) != crc32(0, node, sizeof(node->u)-4)) {
  914. JFFS2_NOTICE("Node header CRC failed at %#08x. {%04x,%04x,%08x,%08x}\n",
  915. ref_offset(ref), je16_to_cpu(node->u.magic),
  916. je16_to_cpu(node->u.nodetype),
  917. je32_to_cpu(node->u.totlen),
  918. je32_to_cpu(node->u.hdr_crc));
  919. jffs2_dbg_dump_node(c, ref_offset(ref));
  920. jffs2_mark_node_obsolete(c, ref);
  921. goto cont;
  922. }
  923. if (je16_to_cpu(node->u.magic) != JFFS2_MAGIC_BITMASK) {
  924. /* Not a JFFS2 node, whinge and move on */
  925. JFFS2_NOTICE("Wrong magic bitmask 0x%04x in node header at %#08x.\n",
  926. je16_to_cpu(node->u.magic), ref_offset(ref));
  927. jffs2_mark_node_obsolete(c, ref);
  928. goto cont;
  929. }
  930. switch (je16_to_cpu(node->u.nodetype)) {
  931. case JFFS2_NODETYPE_DIRENT:
  932. if (JFFS2_MIN_NODE_HEADER < sizeof(struct jffs2_raw_dirent) &&
  933. len < sizeof(struct jffs2_raw_dirent)) {
  934. err = read_more(c, ref, sizeof(struct jffs2_raw_dirent), &len, buf);
  935. if (unlikely(err))
  936. goto free_out;
  937. }
  938. err = read_direntry(c, ref, &node->d, retlen, rii);
  939. if (unlikely(err))
  940. goto free_out;
  941. break;
  942. case JFFS2_NODETYPE_INODE:
  943. if (JFFS2_MIN_NODE_HEADER < sizeof(struct jffs2_raw_inode) &&
  944. len < sizeof(struct jffs2_raw_inode)) {
  945. err = read_more(c, ref, sizeof(struct jffs2_raw_inode), &len, buf);
  946. if (unlikely(err))
  947. goto free_out;
  948. }
  949. err = read_dnode(c, ref, &node->i, len, rii);
  950. if (unlikely(err))
  951. goto free_out;
  952. break;
  953. default:
  954. if (JFFS2_MIN_NODE_HEADER < sizeof(struct jffs2_unknown_node) &&
  955. len < sizeof(struct jffs2_unknown_node)) {
  956. err = read_more(c, ref, sizeof(struct jffs2_unknown_node), &len, buf);
  957. if (unlikely(err))
  958. goto free_out;
  959. }
  960. err = read_unknown(c, ref, &node->u);
  961. if (unlikely(err))
  962. goto free_out;
  963. }
  964. cont:
  965. spin_lock(&c->erase_completion_lock);
  966. }
  967. spin_unlock(&c->erase_completion_lock);
  968. kfree(buf);
  969. f->highest_version = rii->highest_version;
  970. dbg_readinode("nodes of inode #%u were read, the highest version is %u, latest_mctime %u, mctime_ver %u.\n",
  971. f->inocache->ino, rii->highest_version, rii->latest_mctime,
  972. rii->mctime_ver);
  973. return 0;
  974. free_out:
  975. jffs2_free_tmp_dnode_info_list(&rii->tn_root);
  976. jffs2_free_full_dirent_list(rii->fds);
  977. rii->fds = NULL;
  978. kfree(buf);
  979. return err;
  980. }
  981. static int jffs2_do_read_inode_internal(struct jffs2_sb_info *c,
  982. struct jffs2_inode_info *f,
  983. struct jffs2_raw_inode *latest_node)
  984. {
  985. struct jffs2_readinode_info rii;
  986. uint32_t crc, new_size;
  987. size_t retlen;
  988. int ret;
  989. dbg_readinode("ino #%u pino/nlink is %d\n", f->inocache->ino,
  990. f->inocache->pino_nlink);
  991. memset(&rii, 0, sizeof(rii));
  992. /* Grab all nodes relevant to this ino */
  993. ret = jffs2_get_inode_nodes(c, f, &rii);
  994. if (ret) {
  995. JFFS2_ERROR("cannot read nodes for ino %u, returned error is %d\n", f->inocache->ino, ret);
  996. if (f->inocache->state == INO_STATE_READING)
  997. jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
  998. return ret;
  999. }
  1000. ret = jffs2_build_inode_fragtree(c, f, &rii);
  1001. if (ret) {
  1002. JFFS2_ERROR("Failed to build final fragtree for inode #%u: error %d\n",
  1003. f->inocache->ino, ret);
  1004. if (f->inocache->state == INO_STATE_READING)
  1005. jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
  1006. jffs2_free_tmp_dnode_info_list(&rii.tn_root);
  1007. /* FIXME: We could at least crc-check them all */
  1008. if (rii.mdata_tn) {
  1009. jffs2_free_full_dnode(rii.mdata_tn->fn);
  1010. jffs2_free_tmp_dnode_info(rii.mdata_tn);
  1011. rii.mdata_tn = NULL;
  1012. }
  1013. return ret;
  1014. }
  1015. if (rii.mdata_tn) {
  1016. if (rii.mdata_tn->fn->raw == rii.latest_ref) {
  1017. f->metadata = rii.mdata_tn->fn;
  1018. jffs2_free_tmp_dnode_info(rii.mdata_tn);
  1019. } else {
  1020. jffs2_kill_tn(c, rii.mdata_tn);
  1021. }
  1022. rii.mdata_tn = NULL;
  1023. }
  1024. f->dents = rii.fds;
  1025. jffs2_dbg_fragtree_paranoia_check_nolock(f);
  1026. if (unlikely(!rii.latest_ref)) {
  1027. /* No data nodes for this inode. */
  1028. if (f->inocache->ino != 1) {
  1029. JFFS2_WARNING("no data nodes found for ino #%u\n", f->inocache->ino);
  1030. if (!rii.fds) {
  1031. if (f->inocache->state == INO_STATE_READING)
  1032. jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
  1033. return -EIO;
  1034. }
  1035. JFFS2_NOTICE("but it has children so we fake some modes for it\n");
  1036. }
  1037. latest_node->mode = cpu_to_jemode(S_IFDIR|S_IRUGO|S_IWUSR|S_IXUGO);
  1038. latest_node->version = cpu_to_je32(0);
  1039. latest_node->atime = latest_node->ctime = latest_node->mtime = cpu_to_je32(0);
  1040. latest_node->isize = cpu_to_je32(0);
  1041. latest_node->gid = cpu_to_je16(0);
  1042. latest_node->uid = cpu_to_je16(0);
  1043. if (f->inocache->state == INO_STATE_READING)
  1044. jffs2_set_inocache_state(c, f->inocache, INO_STATE_PRESENT);
  1045. return 0;
  1046. }
  1047. ret = jffs2_flash_read(c, ref_offset(rii.latest_ref), sizeof(*latest_node), &retlen, (void *)latest_node);
  1048. if (ret || retlen != sizeof(*latest_node)) {
  1049. JFFS2_ERROR("failed to read from flash: error %d, %zd of %zd bytes read\n",
  1050. ret, retlen, sizeof(*latest_node));
  1051. /* FIXME: If this fails, there seems to be a memory leak. Find it. */
  1052. return ret ? ret : -EIO;
  1053. }
  1054. crc = crc32(0, latest_node, sizeof(*latest_node)-8);
  1055. if (crc != je32_to_cpu(latest_node->node_crc)) {
  1056. JFFS2_ERROR("CRC failed for read_inode of inode %u at physical location 0x%x\n",
  1057. f->inocache->ino, ref_offset(rii.latest_ref));
  1058. return -EIO;
  1059. }
  1060. switch(jemode_to_cpu(latest_node->mode) & S_IFMT) {
  1061. case S_IFDIR:
  1062. if (rii.mctime_ver > je32_to_cpu(latest_node->version)) {
  1063. /* The times in the latest_node are actually older than
  1064. mctime in the latest dirent. Cheat. */
  1065. latest_node->ctime = latest_node->mtime = cpu_to_je32(rii.latest_mctime);
  1066. }
  1067. break;
  1068. case S_IFREG:
  1069. /* If it was a regular file, truncate it to the latest node's isize */
  1070. new_size = jffs2_truncate_fragtree(c, &f->fragtree, je32_to_cpu(latest_node->isize));
  1071. if (new_size != je32_to_cpu(latest_node->isize)) {
  1072. JFFS2_WARNING("Truncating ino #%u to %d bytes failed because it only had %d bytes to start with!\n",
  1073. f->inocache->ino, je32_to_cpu(latest_node->isize), new_size);
  1074. latest_node->isize = cpu_to_je32(new_size);
  1075. }
  1076. break;
  1077. case S_IFLNK:
  1078. /* Hack to work around broken isize in old symlink code.
  1079. Remove this when dwmw2 comes to his senses and stops
  1080. symlinks from being an entirely gratuitous special
  1081. case. */
  1082. if (!je32_to_cpu(latest_node->isize))
  1083. latest_node->isize = latest_node->dsize;
  1084. if (f->inocache->state != INO_STATE_CHECKING) {
  1085. /* Symlink's inode data is the target path. Read it and
  1086. * keep in RAM to facilitate quick follow symlink
  1087. * operation. */
  1088. uint32_t csize = je32_to_cpu(latest_node->csize);
  1089. if (csize > JFFS2_MAX_NAME_LEN)
  1090. return -ENAMETOOLONG;
  1091. f->target = kmalloc(csize + 1, GFP_KERNEL);
  1092. if (!f->target) {
  1093. JFFS2_ERROR("can't allocate %u bytes of memory for the symlink target path cache\n", csize);
  1094. return -ENOMEM;
  1095. }
  1096. ret = jffs2_flash_read(c, ref_offset(rii.latest_ref) + sizeof(*latest_node),
  1097. csize, &retlen, (char *)f->target);
  1098. if (ret || retlen != csize) {
  1099. if (retlen != csize)
  1100. ret = -EIO;
  1101. kfree(f->target);
  1102. f->target = NULL;
  1103. return ret;
  1104. }
  1105. f->target[csize] = '\0';
  1106. dbg_readinode("symlink's target '%s' cached\n", f->target);
  1107. }
  1108. /* fall through... */
  1109. case S_IFBLK:
  1110. case S_IFCHR:
  1111. /* Certain inode types should have only one data node, and it's
  1112. kept as the metadata node */
  1113. if (f->metadata) {
  1114. JFFS2_ERROR("Argh. Special inode #%u with mode 0%o had metadata node\n",
  1115. f->inocache->ino, jemode_to_cpu(latest_node->mode));
  1116. return -EIO;
  1117. }
  1118. if (!frag_first(&f->fragtree)) {
  1119. JFFS2_ERROR("Argh. Special inode #%u with mode 0%o has no fragments\n",
  1120. f->inocache->ino, jemode_to_cpu(latest_node->mode));
  1121. return -EIO;
  1122. }
  1123. /* ASSERT: f->fraglist != NULL */
  1124. if (frag_next(frag_first(&f->fragtree))) {
  1125. JFFS2_ERROR("Argh. Special inode #%u with mode 0x%x had more than one node\n",
  1126. f->inocache->ino, jemode_to_cpu(latest_node->mode));
  1127. /* FIXME: Deal with it - check crc32, check for duplicate node, check times and discard the older one */
  1128. return -EIO;
  1129. }
  1130. /* OK. We're happy */
  1131. f->metadata = frag_first(&f->fragtree)->node;
  1132. jffs2_free_node_frag(frag_first(&f->fragtree));
  1133. f->fragtree = RB_ROOT;
  1134. break;
  1135. }
  1136. if (f->inocache->state == INO_STATE_READING)
  1137. jffs2_set_inocache_state(c, f->inocache, INO_STATE_PRESENT);
  1138. return 0;
  1139. }
  1140. /* Scan the list of all nodes present for this ino, build map of versions, etc. */
  1141. int jffs2_do_read_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
  1142. uint32_t ino, struct jffs2_raw_inode *latest_node)
  1143. {
  1144. dbg_readinode("read inode #%u\n", ino);
  1145. retry_inocache:
  1146. spin_lock(&c->inocache_lock);
  1147. f->inocache = jffs2_get_ino_cache(c, ino);
  1148. if (f->inocache) {
  1149. /* Check its state. We may need to wait before we can use it */
  1150. switch(f->inocache->state) {
  1151. case INO_STATE_UNCHECKED:
  1152. case INO_STATE_CHECKEDABSENT:
  1153. f->inocache->state = INO_STATE_READING;
  1154. break;
  1155. case INO_STATE_CHECKING:
  1156. case INO_STATE_GC:
  1157. /* If it's in either of these states, we need
  1158. to wait for whoever's got it to finish and
  1159. put it back. */
  1160. dbg_readinode("waiting for ino #%u in state %d\n", ino, f->inocache->state);
  1161. sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
  1162. goto retry_inocache;
  1163. case INO_STATE_READING:
  1164. case INO_STATE_PRESENT:
  1165. /* Eep. This should never happen. It can
  1166. happen if Linux calls read_inode() again
  1167. before clear_inode() has finished though. */
  1168. JFFS2_ERROR("Eep. Trying to read_inode #%u when it's already in state %d!\n", ino, f->inocache->state);
  1169. /* Fail. That's probably better than allowing it to succeed */
  1170. f->inocache = NULL;
  1171. break;
  1172. default:
  1173. BUG();
  1174. }
  1175. }
  1176. spin_unlock(&c->inocache_lock);
  1177. if (!f->inocache && ino == 1) {
  1178. /* Special case - no root inode on medium */
  1179. f->inocache = jffs2_alloc_inode_cache();
  1180. if (!f->inocache) {
  1181. JFFS2_ERROR("cannot allocate inocache for root inode\n");
  1182. return -ENOMEM;
  1183. }
  1184. dbg_readinode("creating inocache for root inode\n");
  1185. memset(f->inocache, 0, sizeof(struct jffs2_inode_cache));
  1186. f->inocache->ino = f->inocache->pino_nlink = 1;
  1187. f->inocache->nodes = (struct jffs2_raw_node_ref *)f->inocache;
  1188. f->inocache->state = INO_STATE_READING;
  1189. jffs2_add_ino_cache(c, f->inocache);
  1190. }
  1191. if (!f->inocache) {
  1192. JFFS2_ERROR("requestied to read an nonexistent ino %u\n", ino);
  1193. return -ENOENT;
  1194. }
  1195. return jffs2_do_read_inode_internal(c, f, latest_node);
  1196. }
  1197. int jffs2_do_crccheck_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
  1198. {
  1199. struct jffs2_raw_inode n;
  1200. struct jffs2_inode_info *f = kzalloc(sizeof(*f), GFP_KERNEL);
  1201. int ret;
  1202. if (!f)
  1203. return -ENOMEM;
  1204. mutex_init(&f->sem);
  1205. mutex_lock(&f->sem);
  1206. f->inocache = ic;
  1207. ret = jffs2_do_read_inode_internal(c, f, &n);
  1208. mutex_unlock(&f->sem);
  1209. jffs2_do_clear_inode(c, f);
  1210. jffs2_xattr_do_crccheck_inode(c, ic);
  1211. kfree (f);
  1212. return ret;
  1213. }
  1214. void jffs2_do_clear_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f)
  1215. {
  1216. struct jffs2_full_dirent *fd, *fds;
  1217. int deleted;
  1218. jffs2_xattr_delete_inode(c, f->inocache);
  1219. mutex_lock(&f->sem);
  1220. deleted = f->inocache && !f->inocache->pino_nlink;
  1221. if (f->inocache && f->inocache->state != INO_STATE_CHECKING)
  1222. jffs2_set_inocache_state(c, f->inocache, INO_STATE_CLEARING);
  1223. if (f->metadata) {
  1224. if (deleted)
  1225. jffs2_mark_node_obsolete(c, f->metadata->raw);
  1226. jffs2_free_full_dnode(f->metadata);
  1227. }
  1228. jffs2_kill_fragtree(&f->fragtree, deleted?c:NULL);
  1229. if (f->target) {
  1230. kfree(f->target);
  1231. f->target = NULL;
  1232. }
  1233. fds = f->dents;
  1234. while(fds) {
  1235. fd = fds;
  1236. fds = fd->next;
  1237. jffs2_free_full_dirent(fd);
  1238. }
  1239. if (f->inocache && f->inocache->state != INO_STATE_CHECKING) {
  1240. jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
  1241. if (f->inocache->nodes == (void *)f->inocache)
  1242. jffs2_del_ino_cache(c, f->inocache);
  1243. }
  1244. mutex_unlock(&f->sem);
  1245. }