read.c 6.8 KB

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