readinode.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433
  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. return -EIO;
  588. if (unlikely(err)) {
  589. JFFS2_ERROR("read remainder of name: error %d\n", err);
  590. jffs2_free_full_dirent(fd);
  591. return -EIO;
  592. }
  593. }
  594. fd->nhash = full_name_hash(fd->name, rd->nsize);
  595. fd->next = NULL;
  596. fd->name[rd->nsize] = '\0';
  597. /*
  598. * Wheee. We now have a complete jffs2_full_dirent structure, with
  599. * the name in it and everything. Link it into the list
  600. */
  601. jffs2_add_fd_to_list(c, fd, &rii->fds);
  602. return 0;
  603. }
  604. /*
  605. * Helper function for jffs2_get_inode_nodes().
  606. * It is called every time an inode node is found.
  607. *
  608. * Returns: 0 on success (possibly after marking a bad node obsolete);
  609. * negative error code on failure.
  610. */
  611. static inline int read_dnode(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref,
  612. struct jffs2_raw_inode *rd, int rdlen,
  613. struct jffs2_readinode_info *rii)
  614. {
  615. struct jffs2_tmp_dnode_info *tn;
  616. uint32_t len, csize;
  617. int ret = 0;
  618. uint32_t crc;
  619. /* Obsoleted. This cannot happen, surely? dwmw2 20020308 */
  620. BUG_ON(ref_obsolete(ref));
  621. crc = crc32(0, rd, sizeof(*rd) - 8);
  622. if (unlikely(crc != je32_to_cpu(rd->node_crc))) {
  623. JFFS2_NOTICE("node CRC failed on dnode at %#08x: read %#08x, calculated %#08x\n",
  624. ref_offset(ref), je32_to_cpu(rd->node_crc), crc);
  625. jffs2_mark_node_obsolete(c, ref);
  626. return 0;
  627. }
  628. tn = jffs2_alloc_tmp_dnode_info();
  629. if (!tn) {
  630. JFFS2_ERROR("failed to allocate tn (%zu bytes).\n", sizeof(*tn));
  631. return -ENOMEM;
  632. }
  633. tn->partial_crc = 0;
  634. csize = je32_to_cpu(rd->csize);
  635. /* If we've never checked the CRCs on this node, check them now */
  636. if (ref_flags(ref) == REF_UNCHECKED) {
  637. /* Sanity checks */
  638. if (unlikely(je32_to_cpu(rd->offset) > je32_to_cpu(rd->isize)) ||
  639. unlikely(PAD(je32_to_cpu(rd->csize) + sizeof(*rd)) != PAD(je32_to_cpu(rd->totlen)))) {
  640. JFFS2_WARNING("inode node header CRC is corrupted at %#08x\n", ref_offset(ref));
  641. jffs2_dbg_dump_node(c, ref_offset(ref));
  642. jffs2_mark_node_obsolete(c, ref);
  643. goto free_out;
  644. }
  645. if (jffs2_is_writebuffered(c) && csize != 0) {
  646. /* At this point we are supposed to check the data CRC
  647. * of our unchecked node. But thus far, we do not
  648. * know whether the node is valid or obsolete. To
  649. * figure this out, we need to walk all the nodes of
  650. * the inode and build the inode fragtree. We don't
  651. * want to spend time checking data of nodes which may
  652. * later be found to be obsolete. So we put off the full
  653. * data CRC checking until we have read all the inode
  654. * nodes and have started building the fragtree.
  655. *
  656. * The fragtree is being built starting with nodes
  657. * having the highest version number, so we'll be able
  658. * to detect whether a node is valid (i.e., it is not
  659. * overlapped by a node with higher version) or not.
  660. * And we'll be able to check only those nodes, which
  661. * are not obsolete.
  662. *
  663. * Of course, this optimization only makes sense in case
  664. * of NAND flashes (or other flashes with
  665. * !jffs2_can_mark_obsolete()), since on NOR flashes
  666. * nodes are marked obsolete physically.
  667. *
  668. * Since NAND flashes (or other flashes with
  669. * jffs2_is_writebuffered(c)) are anyway read by
  670. * fractions of c->wbuf_pagesize, and we have just read
  671. * the node header, it is likely that the starting part
  672. * of the node data is also read when we read the
  673. * header. So we don't mind to check the CRC of the
  674. * starting part of the data of the node now, and check
  675. * the second part later (in jffs2_check_node_data()).
  676. * Of course, we will not need to re-read and re-check
  677. * the NAND page which we have just read. This is why we
  678. * read the whole NAND page at jffs2_get_inode_nodes(),
  679. * while we needed only the node header.
  680. */
  681. unsigned char *buf;
  682. /* 'buf' will point to the start of data */
  683. buf = (unsigned char *)rd + sizeof(*rd);
  684. /* len will be the read data length */
  685. len = min_t(uint32_t, rdlen - sizeof(*rd), csize);
  686. tn->partial_crc = crc32(0, buf, len);
  687. dbg_readinode("Calculates CRC (%#08x) for %d bytes, csize %d\n", tn->partial_crc, len, csize);
  688. /* If we actually calculated the whole data CRC
  689. * and it is wrong, drop the node. */
  690. if (len >= csize && unlikely(tn->partial_crc != je32_to_cpu(rd->data_crc))) {
  691. JFFS2_NOTICE("wrong data CRC in data node at 0x%08x: read %#08x, calculated %#08x.\n",
  692. ref_offset(ref), tn->partial_crc, je32_to_cpu(rd->data_crc));
  693. jffs2_mark_node_obsolete(c, ref);
  694. goto free_out;
  695. }
  696. } else if (csize == 0) {
  697. /*
  698. * We checked the header CRC. If the node has no data, adjust
  699. * the space accounting now. For other nodes this will be done
  700. * later either when the node is marked obsolete or when its
  701. * data is checked.
  702. */
  703. struct jffs2_eraseblock *jeb;
  704. dbg_readinode("the node has no data.\n");
  705. jeb = &c->blocks[ref->flash_offset / c->sector_size];
  706. len = ref_totlen(c, jeb, ref);
  707. spin_lock(&c->erase_completion_lock);
  708. jeb->used_size += len;
  709. jeb->unchecked_size -= len;
  710. c->used_size += len;
  711. c->unchecked_size -= len;
  712. ref->flash_offset = ref_offset(ref) | REF_NORMAL;
  713. spin_unlock(&c->erase_completion_lock);
  714. }
  715. }
  716. tn->fn = jffs2_alloc_full_dnode();
  717. if (!tn->fn) {
  718. JFFS2_ERROR("alloc fn failed\n");
  719. ret = -ENOMEM;
  720. goto free_out;
  721. }
  722. tn->version = je32_to_cpu(rd->version);
  723. tn->fn->ofs = je32_to_cpu(rd->offset);
  724. tn->data_crc = je32_to_cpu(rd->data_crc);
  725. tn->csize = csize;
  726. tn->fn->raw = ref;
  727. tn->overlapped = 0;
  728. if (tn->version > rii->highest_version)
  729. rii->highest_version = tn->version;
  730. /* There was a bug where we wrote hole nodes out with
  731. csize/dsize swapped. Deal with it */
  732. if (rd->compr == JFFS2_COMPR_ZERO && !je32_to_cpu(rd->dsize) && csize)
  733. tn->fn->size = csize;
  734. else // normal case...
  735. tn->fn->size = je32_to_cpu(rd->dsize);
  736. dbg_readinode2("dnode @%08x: ver %u, offset %#04x, dsize %#04x, csize %#04x\n",
  737. ref_offset(ref), je32_to_cpu(rd->version),
  738. je32_to_cpu(rd->offset), je32_to_cpu(rd->dsize), csize);
  739. ret = jffs2_add_tn_to_tree(c, rii, tn);
  740. if (ret) {
  741. jffs2_free_full_dnode(tn->fn);
  742. free_out:
  743. jffs2_free_tmp_dnode_info(tn);
  744. return ret;
  745. }
  746. #ifdef JFFS2_DBG_READINODE2_MESSAGES
  747. dbg_readinode2("After adding ver %d:\n", je32_to_cpu(rd->version));
  748. tn = tn_first(&rii->tn_root);
  749. while (tn) {
  750. dbg_readinode2("%p: v %d r 0x%x-0x%x ov %d\n",
  751. tn, tn->version, tn->fn->ofs,
  752. tn->fn->ofs+tn->fn->size, tn->overlapped);
  753. tn = tn_next(tn);
  754. }
  755. #endif
  756. return 0;
  757. }
  758. /*
  759. * Helper function for jffs2_get_inode_nodes().
  760. * It is called every time an unknown node is found.
  761. *
  762. * Returns: 0 on success;
  763. * negative error code on failure.
  764. */
  765. static inline int read_unknown(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref, struct jffs2_unknown_node *un)
  766. {
  767. /* We don't mark unknown nodes as REF_UNCHECKED */
  768. if (ref_flags(ref) == REF_UNCHECKED) {
  769. JFFS2_ERROR("REF_UNCHECKED but unknown node at %#08x\n",
  770. ref_offset(ref));
  771. JFFS2_ERROR("Node is {%04x,%04x,%08x,%08x}. Please report this error.\n",
  772. je16_to_cpu(un->magic), je16_to_cpu(un->nodetype),
  773. je32_to_cpu(un->totlen), je32_to_cpu(un->hdr_crc));
  774. jffs2_mark_node_obsolete(c, ref);
  775. return 0;
  776. }
  777. un->nodetype = cpu_to_je16(JFFS2_NODE_ACCURATE | je16_to_cpu(un->nodetype));
  778. switch(je16_to_cpu(un->nodetype) & JFFS2_COMPAT_MASK) {
  779. case JFFS2_FEATURE_INCOMPAT:
  780. JFFS2_ERROR("unknown INCOMPAT nodetype %#04X at %#08x\n",
  781. je16_to_cpu(un->nodetype), ref_offset(ref));
  782. /* EEP */
  783. BUG();
  784. break;
  785. case JFFS2_FEATURE_ROCOMPAT:
  786. JFFS2_ERROR("unknown ROCOMPAT nodetype %#04X at %#08x\n",
  787. je16_to_cpu(un->nodetype), ref_offset(ref));
  788. BUG_ON(!(c->flags & JFFS2_SB_FLAG_RO));
  789. break;
  790. case JFFS2_FEATURE_RWCOMPAT_COPY:
  791. JFFS2_NOTICE("unknown RWCOMPAT_COPY nodetype %#04X at %#08x\n",
  792. je16_to_cpu(un->nodetype), ref_offset(ref));
  793. break;
  794. case JFFS2_FEATURE_RWCOMPAT_DELETE:
  795. JFFS2_NOTICE("unknown RWCOMPAT_DELETE nodetype %#04X at %#08x\n",
  796. je16_to_cpu(un->nodetype), ref_offset(ref));
  797. jffs2_mark_node_obsolete(c, ref);
  798. return 0;
  799. }
  800. return 0;
  801. }
  802. /*
  803. * Helper function for jffs2_get_inode_nodes().
  804. * The function detects whether more data should be read and reads it if yes.
  805. *
  806. * Returns: 0 on success;
  807. * negative error code on failure.
  808. */
  809. static int read_more(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref,
  810. int needed_len, int *rdlen, unsigned char *buf)
  811. {
  812. int err, to_read = needed_len - *rdlen;
  813. size_t retlen;
  814. uint32_t offs;
  815. if (jffs2_is_writebuffered(c)) {
  816. int rem = to_read % c->wbuf_pagesize;
  817. if (rem)
  818. to_read += c->wbuf_pagesize - rem;
  819. }
  820. /* We need to read more data */
  821. offs = ref_offset(ref) + *rdlen;
  822. dbg_readinode("read more %d bytes\n", to_read);
  823. err = jffs2_flash_read(c, offs, to_read, &retlen, buf + *rdlen);
  824. if (err) {
  825. JFFS2_ERROR("can not read %d bytes from 0x%08x, "
  826. "error code: %d.\n", to_read, offs, err);
  827. return err;
  828. }
  829. if (retlen < to_read) {
  830. JFFS2_ERROR("short read at %#08x: %zu instead of %d.\n",
  831. offs, retlen, to_read);
  832. return -EIO;
  833. }
  834. *rdlen += to_read;
  835. return 0;
  836. }
  837. /* Get tmp_dnode_info and full_dirent for all non-obsolete nodes associated
  838. with this ino. Perform a preliminary ordering on data nodes, throwing away
  839. those which are completely obsoleted by newer ones. The naïve approach we
  840. use to take of just returning them _all_ in version order will cause us to
  841. run out of memory in certain degenerate cases. */
  842. static int jffs2_get_inode_nodes(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
  843. struct jffs2_readinode_info *rii)
  844. {
  845. struct jffs2_raw_node_ref *ref, *valid_ref;
  846. unsigned char *buf = NULL;
  847. union jffs2_node_union *node;
  848. size_t retlen;
  849. int len, err;
  850. rii->mctime_ver = 0;
  851. dbg_readinode("ino #%u\n", f->inocache->ino);
  852. /* FIXME: in case of NOR and available ->point() this
  853. * needs to be fixed. */
  854. len = sizeof(union jffs2_node_union) + c->wbuf_pagesize;
  855. buf = kmalloc(len, GFP_KERNEL);
  856. if (!buf)
  857. return -ENOMEM;
  858. spin_lock(&c->erase_completion_lock);
  859. valid_ref = jffs2_first_valid_node(f->inocache->nodes);
  860. if (!valid_ref && f->inocache->ino != 1)
  861. JFFS2_WARNING("Eep. No valid nodes for ino #%u.\n", f->inocache->ino);
  862. while (valid_ref) {
  863. /* We can hold a pointer to a non-obsolete node without the spinlock,
  864. but _obsolete_ nodes may disappear at any time, if the block
  865. they're in gets erased. So if we mark 'ref' obsolete while we're
  866. not holding the lock, it can go away immediately. For that reason,
  867. we find the next valid node first, before processing 'ref'.
  868. */
  869. ref = valid_ref;
  870. valid_ref = jffs2_first_valid_node(ref->next_in_ino);
  871. spin_unlock(&c->erase_completion_lock);
  872. cond_resched();
  873. /*
  874. * At this point we don't know the type of the node we're going
  875. * to read, so we do not know the size of its header. In order
  876. * to minimize the amount of flash IO we assume the header is
  877. * of size = JFFS2_MIN_NODE_HEADER.
  878. */
  879. len = JFFS2_MIN_NODE_HEADER;
  880. if (jffs2_is_writebuffered(c)) {
  881. int end, rem;
  882. /*
  883. * We are about to read JFFS2_MIN_NODE_HEADER bytes,
  884. * but this flash has some minimal I/O unit. It is
  885. * possible that we'll need to read more soon, so read
  886. * up to the next min. I/O unit, in order not to
  887. * re-read the same min. I/O unit twice.
  888. */
  889. end = ref_offset(ref) + len;
  890. rem = end % c->wbuf_pagesize;
  891. if (rem)
  892. end += c->wbuf_pagesize - rem;
  893. len = end - ref_offset(ref);
  894. }
  895. dbg_readinode("read %d bytes at %#08x(%d).\n", len, ref_offset(ref), ref_flags(ref));
  896. /* FIXME: point() */
  897. err = jffs2_flash_read(c, ref_offset(ref), len, &retlen, buf);
  898. if (err) {
  899. JFFS2_ERROR("can not read %d bytes from 0x%08x, error code: %d.\n", len, ref_offset(ref), err);
  900. goto free_out;
  901. }
  902. if (retlen < len) {
  903. JFFS2_ERROR("short read at %#08x: %zu instead of %d.\n", ref_offset(ref), retlen, len);
  904. err = -EIO;
  905. goto free_out;
  906. }
  907. node = (union jffs2_node_union *)buf;
  908. /* No need to mask in the valid bit; it shouldn't be invalid */
  909. if (je32_to_cpu(node->u.hdr_crc) != crc32(0, node, sizeof(node->u)-4)) {
  910. JFFS2_NOTICE("Node header CRC failed at %#08x. {%04x,%04x,%08x,%08x}\n",
  911. ref_offset(ref), je16_to_cpu(node->u.magic),
  912. je16_to_cpu(node->u.nodetype),
  913. je32_to_cpu(node->u.totlen),
  914. je32_to_cpu(node->u.hdr_crc));
  915. jffs2_dbg_dump_node(c, ref_offset(ref));
  916. jffs2_mark_node_obsolete(c, ref);
  917. goto cont;
  918. }
  919. if (je16_to_cpu(node->u.magic) != JFFS2_MAGIC_BITMASK) {
  920. /* Not a JFFS2 node, whinge and move on */
  921. JFFS2_NOTICE("Wrong magic bitmask 0x%04x in node header at %#08x.\n",
  922. je16_to_cpu(node->u.magic), ref_offset(ref));
  923. jffs2_mark_node_obsolete(c, ref);
  924. goto cont;
  925. }
  926. switch (je16_to_cpu(node->u.nodetype)) {
  927. case JFFS2_NODETYPE_DIRENT:
  928. if (JFFS2_MIN_NODE_HEADER < sizeof(struct jffs2_raw_dirent) &&
  929. len < sizeof(struct jffs2_raw_dirent)) {
  930. err = read_more(c, ref, sizeof(struct jffs2_raw_dirent), &len, buf);
  931. if (unlikely(err))
  932. goto free_out;
  933. }
  934. err = read_direntry(c, ref, &node->d, retlen, rii);
  935. if (unlikely(err))
  936. goto free_out;
  937. break;
  938. case JFFS2_NODETYPE_INODE:
  939. if (JFFS2_MIN_NODE_HEADER < sizeof(struct jffs2_raw_inode) &&
  940. len < sizeof(struct jffs2_raw_inode)) {
  941. err = read_more(c, ref, sizeof(struct jffs2_raw_inode), &len, buf);
  942. if (unlikely(err))
  943. goto free_out;
  944. }
  945. err = read_dnode(c, ref, &node->i, len, rii);
  946. if (unlikely(err))
  947. goto free_out;
  948. break;
  949. default:
  950. if (JFFS2_MIN_NODE_HEADER < sizeof(struct jffs2_unknown_node) &&
  951. len < sizeof(struct jffs2_unknown_node)) {
  952. err = read_more(c, ref, sizeof(struct jffs2_unknown_node), &len, buf);
  953. if (unlikely(err))
  954. goto free_out;
  955. }
  956. err = read_unknown(c, ref, &node->u);
  957. if (unlikely(err))
  958. goto free_out;
  959. }
  960. cont:
  961. spin_lock(&c->erase_completion_lock);
  962. }
  963. spin_unlock(&c->erase_completion_lock);
  964. kfree(buf);
  965. f->highest_version = rii->highest_version;
  966. dbg_readinode("nodes of inode #%u were read, the highest version is %u, latest_mctime %u, mctime_ver %u.\n",
  967. f->inocache->ino, rii->highest_version, rii->latest_mctime,
  968. rii->mctime_ver);
  969. return 0;
  970. free_out:
  971. jffs2_free_tmp_dnode_info_list(&rii->tn_root);
  972. jffs2_free_full_dirent_list(rii->fds);
  973. rii->fds = NULL;
  974. kfree(buf);
  975. return err;
  976. }
  977. static int jffs2_do_read_inode_internal(struct jffs2_sb_info *c,
  978. struct jffs2_inode_info *f,
  979. struct jffs2_raw_inode *latest_node)
  980. {
  981. struct jffs2_readinode_info rii;
  982. uint32_t crc, new_size;
  983. size_t retlen;
  984. int ret;
  985. dbg_readinode("ino #%u pino/nlink is %d\n", f->inocache->ino,
  986. f->inocache->pino_nlink);
  987. memset(&rii, 0, sizeof(rii));
  988. /* Grab all nodes relevant to this ino */
  989. ret = jffs2_get_inode_nodes(c, f, &rii);
  990. if (ret) {
  991. JFFS2_ERROR("cannot read nodes for ino %u, returned error is %d\n", f->inocache->ino, ret);
  992. if (f->inocache->state == INO_STATE_READING)
  993. jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
  994. return ret;
  995. }
  996. ret = jffs2_build_inode_fragtree(c, f, &rii);
  997. if (ret) {
  998. JFFS2_ERROR("Failed to build final fragtree for inode #%u: error %d\n",
  999. f->inocache->ino, ret);
  1000. if (f->inocache->state == INO_STATE_READING)
  1001. jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
  1002. jffs2_free_tmp_dnode_info_list(&rii.tn_root);
  1003. /* FIXME: We could at least crc-check them all */
  1004. if (rii.mdata_tn) {
  1005. jffs2_free_full_dnode(rii.mdata_tn->fn);
  1006. jffs2_free_tmp_dnode_info(rii.mdata_tn);
  1007. rii.mdata_tn = NULL;
  1008. }
  1009. return ret;
  1010. }
  1011. if (rii.mdata_tn) {
  1012. if (rii.mdata_tn->fn->raw == rii.latest_ref) {
  1013. f->metadata = rii.mdata_tn->fn;
  1014. jffs2_free_tmp_dnode_info(rii.mdata_tn);
  1015. } else {
  1016. jffs2_kill_tn(c, rii.mdata_tn);
  1017. }
  1018. rii.mdata_tn = NULL;
  1019. }
  1020. f->dents = rii.fds;
  1021. jffs2_dbg_fragtree_paranoia_check_nolock(f);
  1022. if (unlikely(!rii.latest_ref)) {
  1023. /* No data nodes for this inode. */
  1024. if (f->inocache->ino != 1) {
  1025. JFFS2_WARNING("no data nodes found for ino #%u\n", f->inocache->ino);
  1026. if (!rii.fds) {
  1027. if (f->inocache->state == INO_STATE_READING)
  1028. jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
  1029. return -EIO;
  1030. }
  1031. JFFS2_NOTICE("but it has children so we fake some modes for it\n");
  1032. }
  1033. latest_node->mode = cpu_to_jemode(S_IFDIR|S_IRUGO|S_IWUSR|S_IXUGO);
  1034. latest_node->version = cpu_to_je32(0);
  1035. latest_node->atime = latest_node->ctime = latest_node->mtime = cpu_to_je32(0);
  1036. latest_node->isize = cpu_to_je32(0);
  1037. latest_node->gid = cpu_to_je16(0);
  1038. latest_node->uid = cpu_to_je16(0);
  1039. if (f->inocache->state == INO_STATE_READING)
  1040. jffs2_set_inocache_state(c, f->inocache, INO_STATE_PRESENT);
  1041. return 0;
  1042. }
  1043. ret = jffs2_flash_read(c, ref_offset(rii.latest_ref), sizeof(*latest_node), &retlen, (void *)latest_node);
  1044. if (ret || retlen != sizeof(*latest_node)) {
  1045. JFFS2_ERROR("failed to read from flash: error %d, %zd of %zd bytes read\n",
  1046. ret, retlen, sizeof(*latest_node));
  1047. /* FIXME: If this fails, there seems to be a memory leak. Find it. */
  1048. return ret ? ret : -EIO;
  1049. }
  1050. crc = crc32(0, latest_node, sizeof(*latest_node)-8);
  1051. if (crc != je32_to_cpu(latest_node->node_crc)) {
  1052. JFFS2_ERROR("CRC failed for read_inode of inode %u at physical location 0x%x\n",
  1053. f->inocache->ino, ref_offset(rii.latest_ref));
  1054. return -EIO;
  1055. }
  1056. switch(jemode_to_cpu(latest_node->mode) & S_IFMT) {
  1057. case S_IFDIR:
  1058. if (rii.mctime_ver > je32_to_cpu(latest_node->version)) {
  1059. /* The times in the latest_node are actually older than
  1060. mctime in the latest dirent. Cheat. */
  1061. latest_node->ctime = latest_node->mtime = cpu_to_je32(rii.latest_mctime);
  1062. }
  1063. break;
  1064. case S_IFREG:
  1065. /* If it was a regular file, truncate it to the latest node's isize */
  1066. new_size = jffs2_truncate_fragtree(c, &f->fragtree, je32_to_cpu(latest_node->isize));
  1067. if (new_size != je32_to_cpu(latest_node->isize)) {
  1068. JFFS2_WARNING("Truncating ino #%u to %d bytes failed because it only had %d bytes to start with!\n",
  1069. f->inocache->ino, je32_to_cpu(latest_node->isize), new_size);
  1070. latest_node->isize = cpu_to_je32(new_size);
  1071. }
  1072. break;
  1073. case S_IFLNK:
  1074. /* Hack to work around broken isize in old symlink code.
  1075. Remove this when dwmw2 comes to his senses and stops
  1076. symlinks from being an entirely gratuitous special
  1077. case. */
  1078. if (!je32_to_cpu(latest_node->isize))
  1079. latest_node->isize = latest_node->dsize;
  1080. if (f->inocache->state != INO_STATE_CHECKING) {
  1081. /* Symlink's inode data is the target path. Read it and
  1082. * keep in RAM to facilitate quick follow symlink
  1083. * operation. */
  1084. uint32_t csize = je32_to_cpu(latest_node->csize);
  1085. if (csize > JFFS2_MAX_NAME_LEN)
  1086. return -ENAMETOOLONG;
  1087. f->target = kmalloc(csize + 1, GFP_KERNEL);
  1088. if (!f->target) {
  1089. JFFS2_ERROR("can't allocate %u bytes of memory for the symlink target path cache\n", csize);
  1090. return -ENOMEM;
  1091. }
  1092. ret = jffs2_flash_read(c, ref_offset(rii.latest_ref) + sizeof(*latest_node),
  1093. csize, &retlen, (char *)f->target);
  1094. if (ret || retlen != csize) {
  1095. if (retlen != csize)
  1096. ret = -EIO;
  1097. kfree(f->target);
  1098. f->target = NULL;
  1099. return ret;
  1100. }
  1101. f->target[csize] = '\0';
  1102. dbg_readinode("symlink's target '%s' cached\n", f->target);
  1103. }
  1104. /* fall through... */
  1105. case S_IFBLK:
  1106. case S_IFCHR:
  1107. /* Certain inode types should have only one data node, and it's
  1108. kept as the metadata node */
  1109. if (f->metadata) {
  1110. JFFS2_ERROR("Argh. Special inode #%u with mode 0%o had metadata node\n",
  1111. f->inocache->ino, jemode_to_cpu(latest_node->mode));
  1112. return -EIO;
  1113. }
  1114. if (!frag_first(&f->fragtree)) {
  1115. JFFS2_ERROR("Argh. Special inode #%u with mode 0%o has no fragments\n",
  1116. f->inocache->ino, jemode_to_cpu(latest_node->mode));
  1117. return -EIO;
  1118. }
  1119. /* ASSERT: f->fraglist != NULL */
  1120. if (frag_next(frag_first(&f->fragtree))) {
  1121. JFFS2_ERROR("Argh. Special inode #%u with mode 0x%x had more than one node\n",
  1122. f->inocache->ino, jemode_to_cpu(latest_node->mode));
  1123. /* FIXME: Deal with it - check crc32, check for duplicate node, check times and discard the older one */
  1124. return -EIO;
  1125. }
  1126. /* OK. We're happy */
  1127. f->metadata = frag_first(&f->fragtree)->node;
  1128. jffs2_free_node_frag(frag_first(&f->fragtree));
  1129. f->fragtree = RB_ROOT;
  1130. break;
  1131. }
  1132. if (f->inocache->state == INO_STATE_READING)
  1133. jffs2_set_inocache_state(c, f->inocache, INO_STATE_PRESENT);
  1134. return 0;
  1135. }
  1136. /* Scan the list of all nodes present for this ino, build map of versions, etc. */
  1137. int jffs2_do_read_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
  1138. uint32_t ino, struct jffs2_raw_inode *latest_node)
  1139. {
  1140. dbg_readinode("read inode #%u\n", ino);
  1141. retry_inocache:
  1142. spin_lock(&c->inocache_lock);
  1143. f->inocache = jffs2_get_ino_cache(c, ino);
  1144. if (f->inocache) {
  1145. /* Check its state. We may need to wait before we can use it */
  1146. switch(f->inocache->state) {
  1147. case INO_STATE_UNCHECKED:
  1148. case INO_STATE_CHECKEDABSENT:
  1149. f->inocache->state = INO_STATE_READING;
  1150. break;
  1151. case INO_STATE_CHECKING:
  1152. case INO_STATE_GC:
  1153. /* If it's in either of these states, we need
  1154. to wait for whoever's got it to finish and
  1155. put it back. */
  1156. dbg_readinode("waiting for ino #%u in state %d\n", ino, f->inocache->state);
  1157. sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
  1158. goto retry_inocache;
  1159. case INO_STATE_READING:
  1160. case INO_STATE_PRESENT:
  1161. /* Eep. This should never happen. It can
  1162. happen if Linux calls read_inode() again
  1163. before clear_inode() has finished though. */
  1164. JFFS2_ERROR("Eep. Trying to read_inode #%u when it's already in state %d!\n", ino, f->inocache->state);
  1165. /* Fail. That's probably better than allowing it to succeed */
  1166. f->inocache = NULL;
  1167. break;
  1168. default:
  1169. BUG();
  1170. }
  1171. }
  1172. spin_unlock(&c->inocache_lock);
  1173. if (!f->inocache && ino == 1) {
  1174. /* Special case - no root inode on medium */
  1175. f->inocache = jffs2_alloc_inode_cache();
  1176. if (!f->inocache) {
  1177. JFFS2_ERROR("cannot allocate inocache for root inode\n");
  1178. return -ENOMEM;
  1179. }
  1180. dbg_readinode("creating inocache for root inode\n");
  1181. memset(f->inocache, 0, sizeof(struct jffs2_inode_cache));
  1182. f->inocache->ino = f->inocache->pino_nlink = 1;
  1183. f->inocache->nodes = (struct jffs2_raw_node_ref *)f->inocache;
  1184. f->inocache->state = INO_STATE_READING;
  1185. jffs2_add_ino_cache(c, f->inocache);
  1186. }
  1187. if (!f->inocache) {
  1188. JFFS2_ERROR("requestied to read an nonexistent ino %u\n", ino);
  1189. return -ENOENT;
  1190. }
  1191. return jffs2_do_read_inode_internal(c, f, latest_node);
  1192. }
  1193. int jffs2_do_crccheck_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
  1194. {
  1195. struct jffs2_raw_inode n;
  1196. struct jffs2_inode_info *f = kzalloc(sizeof(*f), GFP_KERNEL);
  1197. int ret;
  1198. if (!f)
  1199. return -ENOMEM;
  1200. mutex_init(&f->sem);
  1201. mutex_lock(&f->sem);
  1202. f->inocache = ic;
  1203. ret = jffs2_do_read_inode_internal(c, f, &n);
  1204. mutex_unlock(&f->sem);
  1205. jffs2_do_clear_inode(c, f);
  1206. jffs2_xattr_do_crccheck_inode(c, ic);
  1207. kfree (f);
  1208. return ret;
  1209. }
  1210. void jffs2_do_clear_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f)
  1211. {
  1212. struct jffs2_full_dirent *fd, *fds;
  1213. int deleted;
  1214. jffs2_xattr_delete_inode(c, f->inocache);
  1215. mutex_lock(&f->sem);
  1216. deleted = f->inocache && !f->inocache->pino_nlink;
  1217. if (f->inocache && f->inocache->state != INO_STATE_CHECKING)
  1218. jffs2_set_inocache_state(c, f->inocache, INO_STATE_CLEARING);
  1219. if (f->metadata) {
  1220. if (deleted)
  1221. jffs2_mark_node_obsolete(c, f->metadata->raw);
  1222. jffs2_free_full_dnode(f->metadata);
  1223. }
  1224. jffs2_kill_fragtree(&f->fragtree, deleted?c:NULL);
  1225. if (f->target) {
  1226. kfree(f->target);
  1227. f->target = NULL;
  1228. }
  1229. fds = f->dents;
  1230. while(fds) {
  1231. fd = fds;
  1232. fds = fd->next;
  1233. jffs2_free_full_dirent(fd);
  1234. }
  1235. if (f->inocache && f->inocache->state != INO_STATE_CHECKING) {
  1236. jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
  1237. if (f->inocache->nodes == (void *)f->inocache)
  1238. jffs2_del_ino_cache(c, f->inocache);
  1239. }
  1240. mutex_unlock(&f->sem);
  1241. }