read.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. D1(printk(KERN_DEBUG "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. D2(printk(KERN_DEBUG "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. D2(printk(KERN_DEBUG "Data CRC matches calculated CRC %08x\n", crc));
  116. if (ri->compr != JFFS2_COMPR_NONE) {
  117. D2(printk(KERN_DEBUG "Decompress %d bytes from %p to %d bytes at %p\n",
  118. je32_to_cpu(ri->csize), readbuf, je32_to_cpu(ri->dsize), decomprbuf));
  119. ret = jffs2_decompress(c, f, ri->compr | (ri->usercompr << 8), readbuf, decomprbuf, je32_to_cpu(ri->csize), je32_to_cpu(ri->dsize));
  120. if (ret) {
  121. printk(KERN_WARNING "Error: jffs2_decompress returned %d\n", ret);
  122. goto out_decomprbuf;
  123. }
  124. }
  125. if (len < je32_to_cpu(ri->dsize)) {
  126. memcpy(buf, decomprbuf+ofs, len);
  127. }
  128. out_decomprbuf:
  129. if(decomprbuf != buf && decomprbuf != readbuf)
  130. kfree(decomprbuf);
  131. out_readbuf:
  132. if(readbuf != buf)
  133. kfree(readbuf);
  134. out_ri:
  135. jffs2_free_raw_inode(ri);
  136. return ret;
  137. }
  138. int jffs2_read_inode_range(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
  139. unsigned char *buf, uint32_t offset, uint32_t len)
  140. {
  141. uint32_t end = offset + len;
  142. struct jffs2_node_frag *frag;
  143. int ret;
  144. D1(printk(KERN_DEBUG "jffs2_read_inode_range: ino #%u, range 0x%08x-0x%08x\n",
  145. f->inocache->ino, offset, offset+len));
  146. frag = jffs2_lookup_node_frag(&f->fragtree, offset);
  147. /* XXX FIXME: Where a single physical node actually shows up in two
  148. frags, we read it twice. Don't do that. */
  149. /* Now we're pointing at the first frag which overlaps our page
  150. * (or perhaps is before it, if we've been asked to read off the
  151. * end of the file). */
  152. while(offset < end) {
  153. D2(printk(KERN_DEBUG "jffs2_read_inode_range: offset %d, end %d\n", offset, end));
  154. if (unlikely(!frag || frag->ofs > offset ||
  155. frag->ofs + frag->size <= offset)) {
  156. uint32_t holesize = end - offset;
  157. if (frag && frag->ofs > offset) {
  158. D1(printk(KERN_NOTICE "Eep. Hole in ino #%u fraglist. frag->ofs = 0x%08x, offset = 0x%08x\n", f->inocache->ino, frag->ofs, offset));
  159. holesize = min(holesize, frag->ofs - offset);
  160. }
  161. D1(printk(KERN_DEBUG "Filling non-frag hole from %d-%d\n", offset, offset+holesize));
  162. memset(buf, 0, holesize);
  163. buf += holesize;
  164. offset += holesize;
  165. continue;
  166. } else if (unlikely(!frag->node)) {
  167. uint32_t holeend = min(end, frag->ofs + frag->size);
  168. D1(printk(KERN_DEBUG "Filling frag hole from %d-%d (frag 0x%x 0x%x)\n", offset, holeend, frag->ofs, frag->ofs + frag->size));
  169. memset(buf, 0, holeend - offset);
  170. buf += holeend - offset;
  171. offset = holeend;
  172. frag = frag_next(frag);
  173. continue;
  174. } else {
  175. uint32_t readlen;
  176. uint32_t fragofs; /* offset within the frag to start reading */
  177. fragofs = offset - frag->ofs;
  178. readlen = min(frag->size - fragofs, end - offset);
  179. D1(printk(KERN_DEBUG "Reading %d-%d from node at 0x%08x (%d)\n",
  180. frag->ofs+fragofs, frag->ofs+fragofs+readlen,
  181. ref_offset(frag->node->raw), ref_flags(frag->node->raw)));
  182. ret = jffs2_read_dnode(c, f, frag->node, buf, fragofs + frag->ofs - frag->node->ofs, readlen);
  183. D2(printk(KERN_DEBUG "node read done\n"));
  184. if (ret) {
  185. D1(printk(KERN_DEBUG"jffs2_read_inode_range error %d\n",ret));
  186. memset(buf, 0, readlen);
  187. return ret;
  188. }
  189. buf += readlen;
  190. offset += readlen;
  191. frag = frag_next(frag);
  192. D2(printk(KERN_DEBUG "node read was OK. Looping\n"));
  193. }
  194. }
  195. return 0;
  196. }