readinode.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  1. /*
  2. * JFFS2 -- Journalling Flash File System, Version 2.
  3. *
  4. * Copyright (C) 2001-2003 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. * $Id: readinode.c,v 1.1.1.1 2007/06/12 07:27:13 eyryu Exp $
  11. *
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/sched.h>
  15. #include <linux/slab.h>
  16. #include <linux/fs.h>
  17. #include <linux/crc32.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/mtd/mtd.h>
  20. #include <linux/compiler.h>
  21. #include "nodelist.h"
  22. /*
  23. * Put a new tmp_dnode_info into the temporaty RB-tree, keeping the list in
  24. * order of increasing version.
  25. */
  26. static void jffs2_add_tn_to_tree(struct jffs2_tmp_dnode_info *tn, struct rb_root *list)
  27. {
  28. struct rb_node **p = &list->rb_node;
  29. struct rb_node * parent = NULL;
  30. struct jffs2_tmp_dnode_info *this;
  31. while (*p) {
  32. parent = *p;
  33. this = rb_entry(parent, struct jffs2_tmp_dnode_info, rb);
  34. /* There may actually be a collision here, but it doesn't
  35. actually matter. As long as the two nodes with the same
  36. version are together, it's all fine. */
  37. if (tn->version > this->version)
  38. p = &(*p)->rb_left;
  39. else
  40. p = &(*p)->rb_right;
  41. }
  42. rb_link_node(&tn->rb, parent, p);
  43. rb_insert_color(&tn->rb, list);
  44. }
  45. static void jffs2_free_tmp_dnode_info_list(struct rb_root *list)
  46. {
  47. struct rb_node *this;
  48. struct jffs2_tmp_dnode_info *tn;
  49. this = list->rb_node;
  50. /* Now at bottom of tree */
  51. while (this) {
  52. if (this->rb_left)
  53. this = this->rb_left;
  54. else if (this->rb_right)
  55. this = this->rb_right;
  56. else {
  57. tn = rb_entry(this, struct jffs2_tmp_dnode_info, rb);
  58. jffs2_free_full_dnode(tn->fn);
  59. jffs2_free_tmp_dnode_info(tn);
  60. this = rb_parent(this);
  61. if (!this)
  62. break;
  63. if (this->rb_left == &tn->rb)
  64. this->rb_left = NULL;
  65. else if (this->rb_right == &tn->rb)
  66. this->rb_right = NULL;
  67. else BUG();
  68. }
  69. }
  70. list->rb_node = NULL;
  71. }
  72. static void jffs2_free_full_dirent_list(struct jffs2_full_dirent *fd)
  73. {
  74. struct jffs2_full_dirent *next;
  75. while (fd) {
  76. next = fd->next;
  77. jffs2_free_full_dirent(fd);
  78. fd = next;
  79. }
  80. }
  81. /* Returns first valid node after 'ref'. May return 'ref' */
  82. static struct jffs2_raw_node_ref *jffs2_first_valid_node(struct jffs2_raw_node_ref *ref)
  83. {
  84. while (ref && ref->next_in_ino) {
  85. if (!ref_obsolete(ref))
  86. return ref;
  87. dbg_noderef("node at 0x%08x is obsoleted. Ignoring.\n", ref_offset(ref));
  88. ref = ref->next_in_ino;
  89. }
  90. return NULL;
  91. }
  92. /*
  93. * Helper function for jffs2_get_inode_nodes().
  94. * It is called every time an directory entry node is found.
  95. *
  96. * Returns: 0 on succes;
  97. * 1 if the node should be marked obsolete;
  98. * negative error code on failure.
  99. */
  100. static inline int read_direntry(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref,
  101. struct jffs2_raw_dirent *rd, size_t read, struct jffs2_full_dirent **fdp,
  102. uint32_t *latest_mctime, uint32_t *mctime_ver)
  103. {
  104. struct jffs2_full_dirent *fd;
  105. uint32_t crc;
  106. /* Obsoleted. This cannot happen, surely? dwmw2 20020308 */
  107. BUG_ON(ref_obsolete(ref));
  108. crc = crc32(0, rd, sizeof(*rd) - 8);
  109. if (unlikely(crc != je32_to_cpu(rd->node_crc))) {
  110. JFFS2_NOTICE("header CRC failed on dirent node at %#08x: read %#08x, calculated %#08x\n",
  111. ref_offset(ref), je32_to_cpu(rd->node_crc), crc);
  112. return 1;
  113. }
  114. /* If we've never checked the CRCs on this node, check them now */
  115. if (ref_flags(ref) == REF_UNCHECKED) {
  116. struct jffs2_eraseblock *jeb;
  117. int len;
  118. /* Sanity check */
  119. if (unlikely(PAD((rd->nsize + sizeof(*rd))) != PAD(je32_to_cpu(rd->totlen)))) {
  120. JFFS2_ERROR("illegal nsize in node at %#08x: nsize %#02x, totlen %#04x\n",
  121. ref_offset(ref), rd->nsize, je32_to_cpu(rd->totlen));
  122. return 1;
  123. }
  124. jeb = &c->blocks[ref->flash_offset / c->sector_size];
  125. len = ref_totlen(c, jeb, ref);
  126. spin_lock(&c->erase_completion_lock);
  127. jeb->used_size += len;
  128. jeb->unchecked_size -= len;
  129. c->used_size += len;
  130. c->unchecked_size -= len;
  131. ref->flash_offset = ref_offset(ref) | REF_PRISTINE;
  132. spin_unlock(&c->erase_completion_lock);
  133. }
  134. fd = jffs2_alloc_full_dirent(rd->nsize + 1);
  135. if (unlikely(!fd))
  136. return -ENOMEM;
  137. fd->raw = ref;
  138. fd->version = je32_to_cpu(rd->version);
  139. fd->ino = je32_to_cpu(rd->ino);
  140. fd->type = rd->type;
  141. /* Pick out the mctime of the latest dirent */
  142. if(fd->version > *mctime_ver && je32_to_cpu(rd->mctime)) {
  143. *mctime_ver = fd->version;
  144. *latest_mctime = je32_to_cpu(rd->mctime);
  145. }
  146. /*
  147. * Copy as much of the name as possible from the raw
  148. * dirent we've already read from the flash.
  149. */
  150. if (read > sizeof(*rd))
  151. memcpy(&fd->name[0], &rd->name[0],
  152. min_t(uint32_t, rd->nsize, (read - sizeof(*rd)) ));
  153. /* Do we need to copy any more of the name directly from the flash? */
  154. if (rd->nsize + sizeof(*rd) > read) {
  155. /* FIXME: point() */
  156. int err;
  157. int already = read - sizeof(*rd);
  158. err = jffs2_flash_read(c, (ref_offset(ref)) + read,
  159. rd->nsize - already, &read, &fd->name[already]);
  160. if (unlikely(read != rd->nsize - already) && likely(!err))
  161. return -EIO;
  162. if (unlikely(err)) {
  163. JFFS2_ERROR("read remainder of name: error %d\n", err);
  164. jffs2_free_full_dirent(fd);
  165. return -EIO;
  166. }
  167. }
  168. fd->nhash = full_name_hash(fd->name, rd->nsize);
  169. fd->next = NULL;
  170. fd->name[rd->nsize] = '\0';
  171. /*
  172. * Wheee. We now have a complete jffs2_full_dirent structure, with
  173. * the name in it and everything. Link it into the list
  174. */
  175. jffs2_add_fd_to_list(c, fd, fdp);
  176. return 0;
  177. }
  178. /*
  179. * Helper function for jffs2_get_inode_nodes().
  180. * It is called every time an inode node is found.
  181. *
  182. * Returns: 0 on succes;
  183. * 1 if the node should be marked obsolete;
  184. * negative error code on failure.
  185. */
  186. static inline int read_dnode(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref,
  187. struct jffs2_raw_inode *rd, struct rb_root *tnp, int rdlen,
  188. uint32_t *latest_mctime, uint32_t *mctime_ver)
  189. {
  190. struct jffs2_tmp_dnode_info *tn;
  191. uint32_t len, csize;
  192. int ret = 1;
  193. uint32_t crc;
  194. /* Obsoleted. This cannot happen, surely? dwmw2 20020308 */
  195. BUG_ON(ref_obsolete(ref));
  196. crc = crc32(0, rd, sizeof(*rd) - 8);
  197. if (unlikely(crc != je32_to_cpu(rd->node_crc))) {
  198. JFFS2_NOTICE("node CRC failed on dnode at %#08x: read %#08x, calculated %#08x\n",
  199. ref_offset(ref), je32_to_cpu(rd->node_crc), crc);
  200. return 1;
  201. }
  202. tn = jffs2_alloc_tmp_dnode_info();
  203. if (!tn) {
  204. JFFS2_ERROR("failed to allocate tn (%zu bytes).\n", sizeof(*tn));
  205. return -ENOMEM;
  206. }
  207. tn->partial_crc = 0;
  208. csize = je32_to_cpu(rd->csize);
  209. /* If we've never checked the CRCs on this node, check them now */
  210. if (ref_flags(ref) == REF_UNCHECKED) {
  211. /* Sanity checks */
  212. if (unlikely(je32_to_cpu(rd->offset) > je32_to_cpu(rd->isize)) ||
  213. unlikely(PAD(je32_to_cpu(rd->csize) + sizeof(*rd)) != PAD(je32_to_cpu(rd->totlen)))) {
  214. JFFS2_WARNING("inode node header CRC is corrupted at %#08x\n", ref_offset(ref));
  215. jffs2_dbg_dump_node(c, ref_offset(ref));
  216. goto free_out;
  217. }
  218. if (jffs2_is_writebuffered(c) && csize != 0) {
  219. /* At this point we are supposed to check the data CRC
  220. * of our unchecked node. But thus far, we do not
  221. * know whether the node is valid or obsolete. To
  222. * figure this out, we need to walk all the nodes of
  223. * the inode and build the inode fragtree. We don't
  224. * want to spend time checking data of nodes which may
  225. * later be found to be obsolete. So we put off the full
  226. * data CRC checking until we have read all the inode
  227. * nodes and have started building the fragtree.
  228. *
  229. * The fragtree is being built starting with nodes
  230. * having the highest version number, so we'll be able
  231. * to detect whether a node is valid (i.e., it is not
  232. * overlapped by a node with higher version) or not.
  233. * And we'll be able to check only those nodes, which
  234. * are not obsolete.
  235. *
  236. * Of course, this optimization only makes sense in case
  237. * of NAND flashes (or other flashes whith
  238. * !jffs2_can_mark_obsolete()), since on NOR flashes
  239. * nodes are marked obsolete physically.
  240. *
  241. * Since NAND flashes (or other flashes with
  242. * jffs2_is_writebuffered(c)) are anyway read by
  243. * fractions of c->wbuf_pagesize, and we have just read
  244. * the node header, it is likely that the starting part
  245. * of the node data is also read when we read the
  246. * header. So we don't mind to check the CRC of the
  247. * starting part of the data of the node now, and check
  248. * the second part later (in jffs2_check_node_data()).
  249. * Of course, we will not need to re-read and re-check
  250. * the NAND page which we have just read. This is why we
  251. * read the whole NAND page at jffs2_get_inode_nodes(),
  252. * while we needed only the node header.
  253. */
  254. unsigned char *buf;
  255. /* 'buf' will point to the start of data */
  256. buf = (unsigned char *)rd + sizeof(*rd);
  257. /* len will be the read data length */
  258. len = min_t(uint32_t, rdlen - sizeof(*rd), csize);
  259. tn->partial_crc = crc32(0, buf, len);
  260. dbg_readinode("Calculates CRC (%#08x) for %d bytes, csize %d\n", tn->partial_crc, len, csize);
  261. /* If we actually calculated the whole data CRC
  262. * and it is wrong, drop the node. */
  263. if (len >= csize && unlikely(tn->partial_crc != je32_to_cpu(rd->data_crc))) {
  264. JFFS2_NOTICE("wrong data CRC in data node at 0x%08x: read %#08x, calculated %#08x.\n",
  265. ref_offset(ref), tn->partial_crc, je32_to_cpu(rd->data_crc));
  266. goto free_out;
  267. }
  268. } else if (csize == 0) {
  269. /*
  270. * We checked the header CRC. If the node has no data, adjust
  271. * the space accounting now. For other nodes this will be done
  272. * later either when the node is marked obsolete or when its
  273. * data is checked.
  274. */
  275. struct jffs2_eraseblock *jeb;
  276. dbg_readinode("the node has no data.\n");
  277. jeb = &c->blocks[ref->flash_offset / c->sector_size];
  278. len = ref_totlen(c, jeb, ref);
  279. spin_lock(&c->erase_completion_lock);
  280. jeb->used_size += len;
  281. jeb->unchecked_size -= len;
  282. c->used_size += len;
  283. c->unchecked_size -= len;
  284. ref->flash_offset = ref_offset(ref) | REF_NORMAL;
  285. spin_unlock(&c->erase_completion_lock);
  286. }
  287. }
  288. tn->fn = jffs2_alloc_full_dnode();
  289. if (!tn->fn) {
  290. JFFS2_ERROR("alloc fn failed\n");
  291. ret = -ENOMEM;
  292. goto free_out;
  293. }
  294. tn->version = je32_to_cpu(rd->version);
  295. tn->fn->ofs = je32_to_cpu(rd->offset);
  296. tn->data_crc = je32_to_cpu(rd->data_crc);
  297. tn->csize = csize;
  298. tn->fn->raw = ref;
  299. /* There was a bug where we wrote hole nodes out with
  300. csize/dsize swapped. Deal with it */
  301. if (rd->compr == JFFS2_COMPR_ZERO && !je32_to_cpu(rd->dsize) && csize)
  302. tn->fn->size = csize;
  303. else // normal case...
  304. tn->fn->size = je32_to_cpu(rd->dsize);
  305. dbg_readinode("dnode @%08x: ver %u, offset %#04x, dsize %#04x, csize %#04x\n",
  306. ref_offset(ref), je32_to_cpu(rd->version), je32_to_cpu(rd->offset), je32_to_cpu(rd->dsize), csize);
  307. jffs2_add_tn_to_tree(tn, tnp);
  308. return 0;
  309. free_out:
  310. jffs2_free_tmp_dnode_info(tn);
  311. return ret;
  312. }
  313. /*
  314. * Helper function for jffs2_get_inode_nodes().
  315. * It is called every time an unknown node is found.
  316. *
  317. * Returns: 0 on success;
  318. * 1 if the node should be marked obsolete;
  319. * negative error code on failure.
  320. */
  321. static inline int read_unknown(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref, struct jffs2_unknown_node *un)
  322. {
  323. /* We don't mark unknown nodes as REF_UNCHECKED */
  324. if (ref_flags(ref) == REF_UNCHECKED) {
  325. JFFS2_ERROR("REF_UNCHECKED but unknown node at %#08x\n",
  326. ref_offset(ref));
  327. JFFS2_ERROR("Node is {%04x,%04x,%08x,%08x}. Please report this error.\n",
  328. je16_to_cpu(un->magic), je16_to_cpu(un->nodetype),
  329. je32_to_cpu(un->totlen), je32_to_cpu(un->hdr_crc));
  330. return 1;
  331. }
  332. un->nodetype = cpu_to_je16(JFFS2_NODE_ACCURATE | je16_to_cpu(un->nodetype));
  333. switch(je16_to_cpu(un->nodetype) & JFFS2_COMPAT_MASK) {
  334. case JFFS2_FEATURE_INCOMPAT:
  335. JFFS2_ERROR("unknown INCOMPAT nodetype %#04X at %#08x\n",
  336. je16_to_cpu(un->nodetype), ref_offset(ref));
  337. /* EEP */
  338. BUG();
  339. break;
  340. case JFFS2_FEATURE_ROCOMPAT:
  341. JFFS2_ERROR("unknown ROCOMPAT nodetype %#04X at %#08x\n",
  342. je16_to_cpu(un->nodetype), ref_offset(ref));
  343. BUG_ON(!(c->flags & JFFS2_SB_FLAG_RO));
  344. break;
  345. case JFFS2_FEATURE_RWCOMPAT_COPY:
  346. JFFS2_NOTICE("unknown RWCOMPAT_COPY nodetype %#04X at %#08x\n",
  347. je16_to_cpu(un->nodetype), ref_offset(ref));
  348. break;
  349. case JFFS2_FEATURE_RWCOMPAT_DELETE:
  350. JFFS2_NOTICE("unknown RWCOMPAT_DELETE nodetype %#04X at %#08x\n",
  351. je16_to_cpu(un->nodetype), ref_offset(ref));
  352. return 1;
  353. }
  354. return 0;
  355. }
  356. /*
  357. * Helper function for jffs2_get_inode_nodes().
  358. * The function detects whether more data should be read and reads it if yes.
  359. *
  360. * Returns: 0 on succes;
  361. * negative error code on failure.
  362. */
  363. static int read_more(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref,
  364. int right_size, int *rdlen, unsigned char *buf, unsigned char *bufstart)
  365. {
  366. int right_len, err, len;
  367. size_t retlen;
  368. uint32_t offs;
  369. if (jffs2_is_writebuffered(c)) {
  370. right_len = c->wbuf_pagesize - (bufstart - buf);
  371. if (right_size + (int)(bufstart - buf) > c->wbuf_pagesize)
  372. right_len += c->wbuf_pagesize;
  373. } else
  374. right_len = right_size;
  375. if (*rdlen == right_len)
  376. return 0;
  377. /* We need to read more data */
  378. offs = ref_offset(ref) + *rdlen;
  379. if (jffs2_is_writebuffered(c)) {
  380. bufstart = buf + c->wbuf_pagesize;
  381. len = c->wbuf_pagesize;
  382. } else {
  383. bufstart = buf + *rdlen;
  384. len = right_size - *rdlen;
  385. }
  386. dbg_readinode("read more %d bytes\n", len);
  387. err = jffs2_flash_read(c, offs, len, &retlen, bufstart);
  388. if (err) {
  389. JFFS2_ERROR("can not read %d bytes from 0x%08x, "
  390. "error code: %d.\n", len, offs, err);
  391. return err;
  392. }
  393. if (retlen < len) {
  394. JFFS2_ERROR("short read at %#08x: %zu instead of %d.\n",
  395. offs, retlen, len);
  396. return -EIO;
  397. }
  398. *rdlen = right_len;
  399. return 0;
  400. }
  401. /* Get tmp_dnode_info and full_dirent for all non-obsolete nodes associated
  402. with this ino, returning the former in order of version */
  403. static int jffs2_get_inode_nodes(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
  404. struct rb_root *tnp, struct jffs2_full_dirent **fdp,
  405. uint32_t *highest_version, uint32_t *latest_mctime,
  406. uint32_t *mctime_ver)
  407. {
  408. struct jffs2_raw_node_ref *ref, *valid_ref;
  409. struct rb_root ret_tn = RB_ROOT;
  410. struct jffs2_full_dirent *ret_fd = NULL;
  411. unsigned char *buf = NULL;
  412. union jffs2_node_union *node;
  413. size_t retlen;
  414. int len, err;
  415. *mctime_ver = 0;
  416. dbg_readinode("ino #%u\n", f->inocache->ino);
  417. if (jffs2_is_writebuffered(c)) {
  418. /*
  419. * If we have the write buffer, we assume the minimal I/O unit
  420. * is c->wbuf_pagesize. We implement some optimizations which in
  421. * this case and we need a temporary buffer of size =
  422. * 2*c->wbuf_pagesize bytes (see comments in read_dnode()).
  423. * Basically, we want to read not only the node header, but the
  424. * whole wbuf (NAND page in case of NAND) or 2, if the node
  425. * header overlaps the border between the 2 wbufs.
  426. */
  427. len = 2*c->wbuf_pagesize;
  428. } else {
  429. /*
  430. * When there is no write buffer, the size of the temporary
  431. * buffer is the size of the larges node header.
  432. */
  433. len = sizeof(union jffs2_node_union);
  434. }
  435. /* FIXME: in case of NOR and available ->point() this
  436. * needs to be fixed. */
  437. buf = kmalloc(len, GFP_KERNEL);
  438. if (!buf)
  439. return -ENOMEM;
  440. spin_lock(&c->erase_completion_lock);
  441. valid_ref = jffs2_first_valid_node(f->inocache->nodes);
  442. if (!valid_ref && f->inocache->ino != 1)
  443. JFFS2_WARNING("Eep. No valid nodes for ino #%u.\n", f->inocache->ino);
  444. while (valid_ref) {
  445. unsigned char *bufstart;
  446. /* We can hold a pointer to a non-obsolete node without the spinlock,
  447. but _obsolete_ nodes may disappear at any time, if the block
  448. they're in gets erased. So if we mark 'ref' obsolete while we're
  449. not holding the lock, it can go away immediately. For that reason,
  450. we find the next valid node first, before processing 'ref'.
  451. */
  452. ref = valid_ref;
  453. valid_ref = jffs2_first_valid_node(ref->next_in_ino);
  454. spin_unlock(&c->erase_completion_lock);
  455. cond_resched();
  456. /*
  457. * At this point we don't know the type of the node we're going
  458. * to read, so we do not know the size of its header. In order
  459. * to minimize the amount of flash IO we assume the node has
  460. * size = JFFS2_MIN_NODE_HEADER.
  461. */
  462. if (jffs2_is_writebuffered(c)) {
  463. /*
  464. * We treat 'buf' as 2 adjacent wbufs. We want to
  465. * adjust bufstart such as it points to the
  466. * beginning of the node within this wbuf.
  467. */
  468. bufstart = buf + (ref_offset(ref) % c->wbuf_pagesize);
  469. /* We will read either one wbuf or 2 wbufs. */
  470. len = c->wbuf_pagesize - (bufstart - buf);
  471. if (JFFS2_MIN_NODE_HEADER + (int)(bufstart - buf) > c->wbuf_pagesize) {
  472. /* The header spans the border of the first wbuf */
  473. len += c->wbuf_pagesize;
  474. }
  475. } else {
  476. bufstart = buf;
  477. len = JFFS2_MIN_NODE_HEADER;
  478. }
  479. dbg_readinode("read %d bytes at %#08x(%d).\n", len, ref_offset(ref), ref_flags(ref));
  480. /* FIXME: point() */
  481. err = jffs2_flash_read(c, ref_offset(ref), len,
  482. &retlen, bufstart);
  483. if (err) {
  484. JFFS2_ERROR("can not read %d bytes from 0x%08x, " "error code: %d.\n", len, ref_offset(ref), err);
  485. goto free_out;
  486. }
  487. if (retlen < len) {
  488. JFFS2_ERROR("short read at %#08x: %zu instead of %d.\n", ref_offset(ref), retlen, len);
  489. err = -EIO;
  490. goto free_out;
  491. }
  492. node = (union jffs2_node_union *)bufstart;
  493. /* No need to mask in the valid bit; it shouldn't be invalid */
  494. if (je32_to_cpu(node->u.hdr_crc) != crc32(0, node, sizeof(node->u)-4)) {
  495. JFFS2_NOTICE("Node header CRC failed at %#08x. {%04x,%04x,%08x,%08x}\n",
  496. ref_offset(ref), je16_to_cpu(node->u.magic),
  497. je16_to_cpu(node->u.nodetype),
  498. je32_to_cpu(node->u.totlen),
  499. je32_to_cpu(node->u.hdr_crc));
  500. jffs2_dbg_dump_node(c, ref_offset(ref));
  501. jffs2_mark_node_obsolete(c, ref);
  502. goto cont;
  503. }
  504. /* Due to poor choice of crc32 seed, an all-zero node will have a correct CRC */
  505. if (!je32_to_cpu(node->u.hdr_crc) && !je16_to_cpu(node->u.nodetype) &&
  506. !je16_to_cpu(node->u.magic) && !je32_to_cpu(node->u.totlen)) {
  507. JFFS2_NOTICE("All zero node header at %#08x.\n", ref_offset(ref));
  508. jffs2_mark_node_obsolete(c, ref);
  509. goto cont;
  510. }
  511. switch (je16_to_cpu(node->u.nodetype)) {
  512. case JFFS2_NODETYPE_DIRENT:
  513. if (JFFS2_MIN_NODE_HEADER < sizeof(struct jffs2_raw_dirent)) {
  514. err = read_more(c, ref, sizeof(struct jffs2_raw_dirent), &len, buf, bufstart);
  515. if (unlikely(err))
  516. goto free_out;
  517. }
  518. err = read_direntry(c, ref, &node->d, retlen, &ret_fd, latest_mctime, mctime_ver);
  519. if (err == 1) {
  520. jffs2_mark_node_obsolete(c, ref);
  521. break;
  522. } else if (unlikely(err))
  523. goto free_out;
  524. if (je32_to_cpu(node->d.version) > *highest_version)
  525. *highest_version = je32_to_cpu(node->d.version);
  526. break;
  527. case JFFS2_NODETYPE_INODE:
  528. if (JFFS2_MIN_NODE_HEADER < sizeof(struct jffs2_raw_inode)) {
  529. err = read_more(c, ref, sizeof(struct jffs2_raw_inode), &len, buf, bufstart);
  530. if (unlikely(err))
  531. goto free_out;
  532. }
  533. err = read_dnode(c, ref, &node->i, &ret_tn, len, latest_mctime, mctime_ver);
  534. if (err == 1) {
  535. jffs2_mark_node_obsolete(c, ref);
  536. break;
  537. } else if (unlikely(err))
  538. goto free_out;
  539. if (je32_to_cpu(node->i.version) > *highest_version)
  540. *highest_version = je32_to_cpu(node->i.version);
  541. break;
  542. default:
  543. if (JFFS2_MIN_NODE_HEADER < sizeof(struct jffs2_unknown_node)) {
  544. err = read_more(c, ref, sizeof(struct jffs2_unknown_node), &len, buf, bufstart);
  545. if (unlikely(err))
  546. goto free_out;
  547. }
  548. err = read_unknown(c, ref, &node->u);
  549. if (err == 1) {
  550. jffs2_mark_node_obsolete(c, ref);
  551. break;
  552. } else if (unlikely(err))
  553. goto free_out;
  554. }
  555. cont:
  556. spin_lock(&c->erase_completion_lock);
  557. }
  558. spin_unlock(&c->erase_completion_lock);
  559. *tnp = ret_tn;
  560. *fdp = ret_fd;
  561. kfree(buf);
  562. dbg_readinode("nodes of inode #%u were read, the highest version is %u, latest_mctime %u, mctime_ver %u.\n",
  563. f->inocache->ino, *highest_version, *latest_mctime, *mctime_ver);
  564. return 0;
  565. free_out:
  566. jffs2_free_tmp_dnode_info_list(&ret_tn);
  567. jffs2_free_full_dirent_list(ret_fd);
  568. kfree(buf);
  569. return err;
  570. }
  571. static int jffs2_do_read_inode_internal(struct jffs2_sb_info *c,
  572. struct jffs2_inode_info *f,
  573. struct jffs2_raw_inode *latest_node)
  574. {
  575. struct jffs2_tmp_dnode_info *tn;
  576. struct rb_root tn_list;
  577. struct rb_node *rb, *repl_rb;
  578. struct jffs2_full_dirent *fd_list;
  579. struct jffs2_full_dnode *fn, *first_fn = NULL;
  580. uint32_t crc;
  581. uint32_t latest_mctime, mctime_ver;
  582. size_t retlen;
  583. int ret;
  584. dbg_readinode("ino #%u nlink is %d\n", f->inocache->ino, f->inocache->nlink);
  585. /* Grab all nodes relevant to this ino */
  586. ret = jffs2_get_inode_nodes(c, f, &tn_list, &fd_list, &f->highest_version, &latest_mctime, &mctime_ver);
  587. if (ret) {
  588. JFFS2_ERROR("cannot read nodes for ino %u, returned error is %d\n", f->inocache->ino, ret);
  589. if (f->inocache->state == INO_STATE_READING)
  590. jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
  591. return ret;
  592. }
  593. f->dents = fd_list;
  594. rb = rb_first(&tn_list);
  595. while (rb) {
  596. cond_resched();
  597. tn = rb_entry(rb, struct jffs2_tmp_dnode_info, rb);
  598. fn = tn->fn;
  599. ret = 1;
  600. dbg_readinode("consider node ver %u, phys offset "
  601. "%#08x(%d), range %u-%u.\n", tn->version,
  602. ref_offset(fn->raw), ref_flags(fn->raw),
  603. fn->ofs, fn->ofs + fn->size);
  604. if (fn->size) {
  605. ret = jffs2_add_older_frag_to_fragtree(c, f, tn);
  606. /* TODO: the error code isn't checked, check it */
  607. jffs2_dbg_fragtree_paranoia_check_nolock(f);
  608. BUG_ON(ret < 0);
  609. if (!first_fn && ret == 0)
  610. first_fn = fn;
  611. } else if (!first_fn) {
  612. first_fn = fn;
  613. f->metadata = fn;
  614. ret = 0; /* Prevent freeing the metadata update node */
  615. } else
  616. jffs2_mark_node_obsolete(c, fn->raw);
  617. BUG_ON(rb->rb_left);
  618. if (rb_parent(rb) && rb_parent(rb)->rb_left == rb) {
  619. /* We were then left-hand child of our parent. We need
  620. * to move our own right-hand child into our place. */
  621. repl_rb = rb->rb_right;
  622. if (repl_rb)
  623. rb_set_parent(repl_rb, rb_parent(rb));
  624. } else
  625. repl_rb = NULL;
  626. rb = rb_next(rb);
  627. /* Remove the spent tn from the tree; don't bother rebalancing
  628. * but put our right-hand child in our own place. */
  629. if (rb_parent(&tn->rb)) {
  630. if (rb_parent(&tn->rb)->rb_left == &tn->rb)
  631. rb_parent(&tn->rb)->rb_left = repl_rb;
  632. else if (rb_parent(&tn->rb)->rb_right == &tn->rb)
  633. rb_parent(&tn->rb)->rb_right = repl_rb;
  634. else BUG();
  635. } else if (tn->rb.rb_right)
  636. rb_set_parent(tn->rb.rb_right, NULL);
  637. jffs2_free_tmp_dnode_info(tn);
  638. if (ret) {
  639. dbg_readinode("delete dnode %u-%u.\n",
  640. fn->ofs, fn->ofs + fn->size);
  641. jffs2_free_full_dnode(fn);
  642. }
  643. }
  644. jffs2_dbg_fragtree_paranoia_check_nolock(f);
  645. BUG_ON(first_fn && ref_obsolete(first_fn->raw));
  646. fn = first_fn;
  647. if (unlikely(!first_fn)) {
  648. /* No data nodes for this inode. */
  649. if (f->inocache->ino != 1) {
  650. JFFS2_WARNING("no data nodes found for ino #%u\n", f->inocache->ino);
  651. if (!fd_list) {
  652. if (f->inocache->state == INO_STATE_READING)
  653. jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
  654. return -EIO;
  655. }
  656. JFFS2_NOTICE("but it has children so we fake some modes for it\n");
  657. }
  658. latest_node->mode = cpu_to_jemode(S_IFDIR|S_IRUGO|S_IWUSR|S_IXUGO);
  659. latest_node->version = cpu_to_je32(0);
  660. latest_node->atime = latest_node->ctime = latest_node->mtime = cpu_to_je32(0);
  661. latest_node->isize = cpu_to_je32(0);
  662. latest_node->gid = cpu_to_je16(0);
  663. latest_node->uid = cpu_to_je16(0);
  664. if (f->inocache->state == INO_STATE_READING)
  665. jffs2_set_inocache_state(c, f->inocache, INO_STATE_PRESENT);
  666. return 0;
  667. }
  668. ret = jffs2_flash_read(c, ref_offset(fn->raw), sizeof(*latest_node), &retlen, (void *)latest_node);
  669. if (ret || retlen != sizeof(*latest_node)) {
  670. JFFS2_ERROR("failed to read from flash: error %d, %zd of %zd bytes read\n",
  671. ret, retlen, sizeof(*latest_node));
  672. /* FIXME: If this fails, there seems to be a memory leak. Find it. */
  673. up(&f->sem);
  674. jffs2_do_clear_inode(c, f);
  675. return ret?ret:-EIO;
  676. }
  677. crc = crc32(0, latest_node, sizeof(*latest_node)-8);
  678. if (crc != je32_to_cpu(latest_node->node_crc)) {
  679. JFFS2_ERROR("CRC failed for read_inode of inode %u at physical location 0x%x\n",
  680. f->inocache->ino, ref_offset(fn->raw));
  681. up(&f->sem);
  682. jffs2_do_clear_inode(c, f);
  683. return -EIO;
  684. }
  685. switch(jemode_to_cpu(latest_node->mode) & S_IFMT) {
  686. case S_IFDIR:
  687. if (mctime_ver > je32_to_cpu(latest_node->version)) {
  688. /* The times in the latest_node are actually older than
  689. mctime in the latest dirent. Cheat. */
  690. latest_node->ctime = latest_node->mtime = cpu_to_je32(latest_mctime);
  691. }
  692. break;
  693. case S_IFREG:
  694. /* If it was a regular file, truncate it to the latest node's isize */
  695. jffs2_truncate_fragtree(c, &f->fragtree, je32_to_cpu(latest_node->isize));
  696. break;
  697. case S_IFLNK:
  698. /* Hack to work around broken isize in old symlink code.
  699. Remove this when dwmw2 comes to his senses and stops
  700. symlinks from being an entirely gratuitous special
  701. case. */
  702. if (!je32_to_cpu(latest_node->isize))
  703. latest_node->isize = latest_node->dsize;
  704. if (f->inocache->state != INO_STATE_CHECKING) {
  705. /* Symlink's inode data is the target path. Read it and
  706. * keep in RAM to facilitate quick follow symlink
  707. * operation. */
  708. f->target = kmalloc(je32_to_cpu(latest_node->csize) + 1, GFP_KERNEL);
  709. if (!f->target) {
  710. JFFS2_ERROR("can't allocate %d bytes of memory for the symlink target path cache\n", je32_to_cpu(latest_node->csize));
  711. up(&f->sem);
  712. jffs2_do_clear_inode(c, f);
  713. return -ENOMEM;
  714. }
  715. ret = jffs2_flash_read(c, ref_offset(fn->raw) + sizeof(*latest_node),
  716. je32_to_cpu(latest_node->csize), &retlen, (char *)f->target);
  717. if (ret || retlen != je32_to_cpu(latest_node->csize)) {
  718. if (retlen != je32_to_cpu(latest_node->csize))
  719. ret = -EIO;
  720. kfree(f->target);
  721. f->target = NULL;
  722. up(&f->sem);
  723. jffs2_do_clear_inode(c, f);
  724. return -ret;
  725. }
  726. f->target[je32_to_cpu(latest_node->csize)] = '\0';
  727. dbg_readinode("symlink's target '%s' cached\n", f->target);
  728. }
  729. /* fall through... */
  730. case S_IFBLK:
  731. case S_IFCHR:
  732. /* Certain inode types should have only one data node, and it's
  733. kept as the metadata node */
  734. if (f->metadata) {
  735. JFFS2_ERROR("Argh. Special inode #%u with mode 0%o had metadata node\n",
  736. f->inocache->ino, jemode_to_cpu(latest_node->mode));
  737. up(&f->sem);
  738. jffs2_do_clear_inode(c, f);
  739. return -EIO;
  740. }
  741. if (!frag_first(&f->fragtree)) {
  742. JFFS2_ERROR("Argh. Special inode #%u with mode 0%o has no fragments\n",
  743. f->inocache->ino, jemode_to_cpu(latest_node->mode));
  744. up(&f->sem);
  745. jffs2_do_clear_inode(c, f);
  746. return -EIO;
  747. }
  748. /* ASSERT: f->fraglist != NULL */
  749. if (frag_next(frag_first(&f->fragtree))) {
  750. JFFS2_ERROR("Argh. Special inode #%u with mode 0x%x had more than one node\n",
  751. f->inocache->ino, jemode_to_cpu(latest_node->mode));
  752. /* FIXME: Deal with it - check crc32, check for duplicate node, check times and discard the older one */
  753. up(&f->sem);
  754. jffs2_do_clear_inode(c, f);
  755. return -EIO;
  756. }
  757. /* OK. We're happy */
  758. f->metadata = frag_first(&f->fragtree)->node;
  759. jffs2_free_node_frag(frag_first(&f->fragtree));
  760. f->fragtree = RB_ROOT;
  761. break;
  762. }
  763. if (f->inocache->state == INO_STATE_READING)
  764. jffs2_set_inocache_state(c, f->inocache, INO_STATE_PRESENT);
  765. return 0;
  766. }
  767. /* Scan the list of all nodes present for this ino, build map of versions, etc. */
  768. int jffs2_do_read_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
  769. uint32_t ino, struct jffs2_raw_inode *latest_node)
  770. {
  771. dbg_readinode("read inode #%u\n", ino);
  772. retry_inocache:
  773. spin_lock(&c->inocache_lock);
  774. f->inocache = jffs2_get_ino_cache(c, ino);
  775. if (f->inocache) {
  776. /* Check its state. We may need to wait before we can use it */
  777. switch(f->inocache->state) {
  778. case INO_STATE_UNCHECKED:
  779. case INO_STATE_CHECKEDABSENT:
  780. f->inocache->state = INO_STATE_READING;
  781. break;
  782. case INO_STATE_CHECKING:
  783. case INO_STATE_GC:
  784. /* If it's in either of these states, we need
  785. to wait for whoever's got it to finish and
  786. put it back. */
  787. dbg_readinode("waiting for ino #%u in state %d\n", ino, f->inocache->state);
  788. sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
  789. goto retry_inocache;
  790. case INO_STATE_READING:
  791. case INO_STATE_PRESENT:
  792. /* Eep. This should never happen. It can
  793. happen if Linux calls read_inode() again
  794. before clear_inode() has finished though. */
  795. JFFS2_ERROR("Eep. Trying to read_inode #%u when it's already in state %d!\n", ino, f->inocache->state);
  796. /* Fail. That's probably better than allowing it to succeed */
  797. f->inocache = NULL;
  798. break;
  799. default:
  800. BUG();
  801. }
  802. }
  803. spin_unlock(&c->inocache_lock);
  804. if (!f->inocache && ino == 1) {
  805. /* Special case - no root inode on medium */
  806. f->inocache = jffs2_alloc_inode_cache();
  807. if (!f->inocache) {
  808. JFFS2_ERROR("cannot allocate inocache for root inode\n");
  809. return -ENOMEM;
  810. }
  811. dbg_readinode("creating inocache for root inode\n");
  812. memset(f->inocache, 0, sizeof(struct jffs2_inode_cache));
  813. f->inocache->ino = f->inocache->nlink = 1;
  814. f->inocache->nodes = (struct jffs2_raw_node_ref *)f->inocache;
  815. f->inocache->state = INO_STATE_READING;
  816. jffs2_add_ino_cache(c, f->inocache);
  817. }
  818. if (!f->inocache) {
  819. JFFS2_ERROR("requestied to read an nonexistent ino %u\n", ino);
  820. return -ENOENT;
  821. }
  822. return jffs2_do_read_inode_internal(c, f, latest_node);
  823. }
  824. int jffs2_do_crccheck_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
  825. {
  826. struct jffs2_raw_inode n;
  827. struct jffs2_inode_info *f = kzalloc(sizeof(*f), GFP_KERNEL);
  828. int ret;
  829. if (!f)
  830. return -ENOMEM;
  831. init_MUTEX_LOCKED(&f->sem);
  832. f->inocache = ic;
  833. ret = jffs2_do_read_inode_internal(c, f, &n);
  834. if (!ret) {
  835. up(&f->sem);
  836. jffs2_do_clear_inode(c, f);
  837. }
  838. kfree (f);
  839. return ret;
  840. }
  841. void jffs2_do_clear_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f)
  842. {
  843. struct jffs2_full_dirent *fd, *fds;
  844. int deleted;
  845. jffs2_clear_acl(f);
  846. jffs2_xattr_delete_inode(c, f->inocache);
  847. down(&f->sem);
  848. deleted = f->inocache && !f->inocache->nlink;
  849. if (f->inocache && f->inocache->state != INO_STATE_CHECKING)
  850. jffs2_set_inocache_state(c, f->inocache, INO_STATE_CLEARING);
  851. if (f->metadata) {
  852. if (deleted)
  853. jffs2_mark_node_obsolete(c, f->metadata->raw);
  854. jffs2_free_full_dnode(f->metadata);
  855. }
  856. jffs2_kill_fragtree(&f->fragtree, deleted?c:NULL);
  857. if (f->target) {
  858. kfree(f->target);
  859. f->target = NULL;
  860. }
  861. fds = f->dents;
  862. while(fds) {
  863. fd = fds;
  864. fds = fd->next;
  865. jffs2_free_full_dirent(fd);
  866. }
  867. if (f->inocache && f->inocache->state != INO_STATE_CHECKING) {
  868. jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
  869. if (f->inocache->nodes == (void *)f->inocache)
  870. jffs2_del_ino_cache(c, f->inocache);
  871. }
  872. up(&f->sem);
  873. }