read.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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/slab.h>
  14. #include <linux/crc32.h>
  15. #include <linux/pagemap.h>
  16. #include <linux/mtd/mtd.h>
  17. #include <linux/compiler.h>
  18. #include "nodelist.h"
  19. #include "compr.h"
  20. int jffs2_read_dnode(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
  21. struct jffs2_full_dnode *fd, unsigned char *buf,
  22. int ofs, int len)
  23. {
  24. struct jffs2_raw_inode *ri;
  25. size_t readlen;
  26. uint32_t crc;
  27. unsigned char *decomprbuf = NULL;
  28. unsigned char *readbuf = NULL;
  29. int ret = 0;
  30. ri = jffs2_alloc_raw_inode();
  31. if (!ri)
  32. return -ENOMEM;
  33. ret = jffs2_flash_read(c, ref_offset(fd->raw), sizeof(*ri), &readlen, (char *)ri);
  34. if (ret) {
  35. jffs2_free_raw_inode(ri);
  36. pr_warn("Error reading node from 0x%08x: %d\n",
  37. ref_offset(fd->raw), ret);
  38. return ret;
  39. }
  40. if (readlen != sizeof(*ri)) {
  41. jffs2_free_raw_inode(ri);
  42. pr_warn("Short read from 0x%08x: wanted 0x%zx bytes, got 0x%zx\n",
  43. ref_offset(fd->raw), sizeof(*ri), readlen);
  44. return -EIO;
  45. }
  46. crc = crc32(0, ri, sizeof(*ri)-8);
  47. jffs2_dbg(1, "Node read from %08x: node_crc %08x, calculated CRC %08x. dsize %x, csize %x, offset %x, buf %p\n",
  48. ref_offset(fd->raw), je32_to_cpu(ri->node_crc),
  49. crc, je32_to_cpu(ri->dsize), je32_to_cpu(ri->csize),
  50. je32_to_cpu(ri->offset), buf);
  51. if (crc != je32_to_cpu(ri->node_crc)) {
  52. pr_warn("Node CRC %08x != calculated CRC %08x for node at %08x\n",
  53. je32_to_cpu(ri->node_crc), crc, ref_offset(fd->raw));
  54. ret = -EIO;
  55. goto out_ri;
  56. }
  57. /* There was a bug where we wrote hole nodes out with csize/dsize
  58. swapped. Deal with it */
  59. if (ri->compr == JFFS2_COMPR_ZERO && !je32_to_cpu(ri->dsize) &&
  60. je32_to_cpu(ri->csize)) {
  61. ri->dsize = ri->csize;
  62. ri->csize = cpu_to_je32(0);
  63. }
  64. D1(if(ofs + len > je32_to_cpu(ri->dsize)) {
  65. pr_warn("jffs2_read_dnode() asked for %d bytes at %d from %d-byte node\n",
  66. len, ofs, je32_to_cpu(ri->dsize));
  67. ret = -EINVAL;
  68. goto out_ri;
  69. });
  70. if (ri->compr == JFFS2_COMPR_ZERO) {
  71. memset(buf, 0, len);
  72. goto out_ri;
  73. }
  74. /* Cases:
  75. Reading whole node and it's uncompressed - read directly to buffer provided, check CRC.
  76. Reading whole node and it's compressed - read into comprbuf, check CRC and decompress to buffer provided
  77. Reading partial node and it's uncompressed - read into readbuf, check CRC, and copy
  78. Reading partial node and it's compressed - read into readbuf, check checksum, decompress to decomprbuf and copy
  79. */
  80. if (ri->compr == JFFS2_COMPR_NONE && len == je32_to_cpu(ri->dsize)) {
  81. readbuf = buf;
  82. } else {
  83. readbuf = kmalloc(je32_to_cpu(ri->csize), GFP_KERNEL);
  84. if (!readbuf) {
  85. ret = -ENOMEM;
  86. goto out_ri;
  87. }
  88. }
  89. if (ri->compr != JFFS2_COMPR_NONE) {
  90. if (len < je32_to_cpu(ri->dsize)) {
  91. decomprbuf = kmalloc(je32_to_cpu(ri->dsize), GFP_KERNEL);
  92. if (!decomprbuf) {
  93. ret = -ENOMEM;
  94. goto out_readbuf;
  95. }
  96. } else {
  97. decomprbuf = buf;
  98. }
  99. } else {
  100. decomprbuf = readbuf;
  101. }
  102. jffs2_dbg(2, "Read %d bytes to %p\n", je32_to_cpu(ri->csize),
  103. readbuf);
  104. ret = jffs2_flash_read(c, (ref_offset(fd->raw)) + sizeof(*ri),
  105. je32_to_cpu(ri->csize), &readlen, readbuf);
  106. if (!ret && readlen != je32_to_cpu(ri->csize))
  107. ret = -EIO;
  108. if (ret)
  109. goto out_decomprbuf;
  110. crc = crc32(0, readbuf, je32_to_cpu(ri->csize));
  111. if (crc != je32_to_cpu(ri->data_crc)) {
  112. pr_warn("Data CRC %08x != calculated CRC %08x for node at %08x\n",
  113. je32_to_cpu(ri->data_crc), crc, ref_offset(fd->raw));
  114. ret = -EIO;
  115. goto out_decomprbuf;
  116. }
  117. jffs2_dbg(2, "Data CRC matches calculated CRC %08x\n", crc);
  118. if (ri->compr != JFFS2_COMPR_NONE) {
  119. jffs2_dbg(2, "Decompress %d bytes from %p to %d bytes at %p\n",
  120. je32_to_cpu(ri->csize), readbuf,
  121. je32_to_cpu(ri->dsize), decomprbuf);
  122. ret = jffs2_decompress(c, f, ri->compr | (ri->usercompr << 8), readbuf, decomprbuf, je32_to_cpu(ri->csize), je32_to_cpu(ri->dsize));
  123. if (ret) {
  124. pr_warn("Error: jffs2_decompress returned %d\n", ret);
  125. goto out_decomprbuf;
  126. }
  127. }
  128. if (len < je32_to_cpu(ri->dsize)) {
  129. memcpy(buf, decomprbuf+ofs, len);
  130. }
  131. out_decomprbuf:
  132. if(decomprbuf != buf && decomprbuf != readbuf)
  133. kfree(decomprbuf);
  134. out_readbuf:
  135. if(readbuf != buf)
  136. kfree(readbuf);
  137. out_ri:
  138. jffs2_free_raw_inode(ri);
  139. return ret;
  140. }
  141. int jffs2_read_inode_range(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
  142. unsigned char *buf, uint32_t offset, uint32_t len)
  143. {
  144. uint32_t end = offset + len;
  145. struct jffs2_node_frag *frag;
  146. int ret;
  147. jffs2_dbg(1, "%s(): ino #%u, range 0x%08x-0x%08x\n",
  148. __func__, f->inocache->ino, offset, offset + len);
  149. frag = jffs2_lookup_node_frag(&f->fragtree, offset);
  150. /* XXX FIXME: Where a single physical node actually shows up in two
  151. frags, we read it twice. Don't do that. */
  152. /* Now we're pointing at the first frag which overlaps our page
  153. * (or perhaps is before it, if we've been asked to read off the
  154. * end of the file). */
  155. while(offset < end) {
  156. jffs2_dbg(2, "%s(): offset %d, end %d\n",
  157. __func__, offset, end);
  158. if (unlikely(!frag || frag->ofs > offset ||
  159. frag->ofs + frag->size <= offset)) {
  160. uint32_t holesize = end - offset;
  161. if (frag && frag->ofs > offset) {
  162. jffs2_dbg(1, "Eep. Hole in ino #%u fraglist. frag->ofs = 0x%08x, offset = 0x%08x\n",
  163. f->inocache->ino, frag->ofs, offset);
  164. holesize = min(holesize, frag->ofs - offset);
  165. }
  166. jffs2_dbg(1, "Filling non-frag hole from %d-%d\n",
  167. offset, offset + holesize);
  168. memset(buf, 0, holesize);
  169. buf += holesize;
  170. offset += holesize;
  171. continue;
  172. } else if (unlikely(!frag->node)) {
  173. uint32_t holeend = min(end, frag->ofs + frag->size);
  174. jffs2_dbg(1, "Filling frag hole from %d-%d (frag 0x%x 0x%x)\n",
  175. offset, holeend, frag->ofs,
  176. frag->ofs + frag->size);
  177. memset(buf, 0, holeend - offset);
  178. buf += holeend - offset;
  179. offset = holeend;
  180. frag = frag_next(frag);
  181. continue;
  182. } else {
  183. uint32_t readlen;
  184. uint32_t fragofs; /* offset within the frag to start reading */
  185. fragofs = offset - frag->ofs;
  186. readlen = min(frag->size - fragofs, end - offset);
  187. jffs2_dbg(1, "Reading %d-%d from node at 0x%08x (%d)\n",
  188. frag->ofs+fragofs,
  189. frag->ofs + fragofs+readlen,
  190. ref_offset(frag->node->raw),
  191. ref_flags(frag->node->raw));
  192. ret = jffs2_read_dnode(c, f, frag->node, buf, fragofs + frag->ofs - frag->node->ofs, readlen);
  193. jffs2_dbg(2, "node read done\n");
  194. if (ret) {
  195. jffs2_dbg(1, "%s(): error %d\n",
  196. __func__, ret);
  197. memset(buf, 0, readlen);
  198. return ret;
  199. }
  200. buf += readlen;
  201. offset += readlen;
  202. frag = frag_next(frag);
  203. jffs2_dbg(2, "node read was OK. Looping\n");
  204. }
  205. }
  206. return 0;
  207. }