read.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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: read.c,v 1.42 2005/11/07 11:14:41 gleixner Exp $
  11. *
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/slab.h>
  15. #include <linux/crc32.h>
  16. #include <linux/pagemap.h>
  17. #include <linux/mtd/mtd.h>
  18. #include <linux/compiler.h>
  19. #include "nodelist.h"
  20. #include "compr.h"
  21. int jffs2_read_dnode(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
  22. struct jffs2_full_dnode *fd, unsigned char *buf,
  23. int ofs, int len)
  24. {
  25. struct jffs2_raw_inode *ri;
  26. size_t readlen;
  27. uint32_t crc;
  28. unsigned char *decomprbuf = NULL;
  29. unsigned char *readbuf = NULL;
  30. int ret = 0;
  31. ri = jffs2_alloc_raw_inode();
  32. if (!ri)
  33. return -ENOMEM;
  34. ret = jffs2_flash_read(c, ref_offset(fd->raw), sizeof(*ri), &readlen, (char *)ri);
  35. if (ret) {
  36. jffs2_free_raw_inode(ri);
  37. printk(KERN_WARNING "Error reading node from 0x%08x: %d\n", ref_offset(fd->raw), ret);
  38. return ret;
  39. }
  40. if (readlen != sizeof(*ri)) {
  41. jffs2_free_raw_inode(ri);
  42. printk(KERN_WARNING "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. D1(printk(KERN_DEBUG "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. printk(KERN_WARNING "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. printk(KERN_WARNING "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. D2(printk(KERN_DEBUG "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. printk(KERN_WARNING "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. D2(printk(KERN_DEBUG "Data CRC matches calculated CRC %08x\n", crc));
  118. if (ri->compr != JFFS2_COMPR_NONE) {
  119. D2(printk(KERN_DEBUG "Decompress %d bytes from %p to %d bytes at %p\n",
  120. je32_to_cpu(ri->csize), readbuf, je32_to_cpu(ri->dsize), decomprbuf));
  121. ret = jffs2_decompress(c, f, ri->compr | (ri->usercompr << 8), readbuf, decomprbuf, je32_to_cpu(ri->csize), je32_to_cpu(ri->dsize));
  122. if (ret) {
  123. printk(KERN_WARNING "Error: jffs2_decompress returned %d\n", ret);
  124. goto out_decomprbuf;
  125. }
  126. }
  127. if (len < je32_to_cpu(ri->dsize)) {
  128. memcpy(buf, decomprbuf+ofs, len);
  129. }
  130. out_decomprbuf:
  131. if(decomprbuf != buf && decomprbuf != readbuf)
  132. kfree(decomprbuf);
  133. out_readbuf:
  134. if(readbuf != buf)
  135. kfree(readbuf);
  136. out_ri:
  137. jffs2_free_raw_inode(ri);
  138. return ret;
  139. }
  140. int jffs2_read_inode_range(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
  141. unsigned char *buf, uint32_t offset, uint32_t len)
  142. {
  143. uint32_t end = offset + len;
  144. struct jffs2_node_frag *frag;
  145. int ret;
  146. D1(printk(KERN_DEBUG "jffs2_read_inode_range: ino #%u, range 0x%08x-0x%08x\n",
  147. f->inocache->ino, offset, offset+len));
  148. frag = jffs2_lookup_node_frag(&f->fragtree, offset);
  149. /* XXX FIXME: Where a single physical node actually shows up in two
  150. frags, we read it twice. Don't do that. */
  151. /* Now we're pointing at the first frag which overlaps our page */
  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. uint32_t holesize = end - offset;
  156. if (frag) {
  157. D1(printk(KERN_NOTICE "Eep. Hole in ino #%u fraglist. frag->ofs = 0x%08x, offset = 0x%08x\n", f->inocache->ino, frag->ofs, offset));
  158. holesize = min(holesize, frag->ofs - offset);
  159. }
  160. D1(printk(KERN_DEBUG "Filling non-frag hole from %d-%d\n", offset, offset+holesize));
  161. memset(buf, 0, holesize);
  162. buf += holesize;
  163. offset += holesize;
  164. continue;
  165. } else if (unlikely(!frag->node)) {
  166. uint32_t holeend = min(end, frag->ofs + frag->size);
  167. D1(printk(KERN_DEBUG "Filling frag hole from %d-%d (frag 0x%x 0x%x)\n", offset, holeend, frag->ofs, frag->ofs + frag->size));
  168. memset(buf, 0, holeend - offset);
  169. buf += holeend - offset;
  170. offset = holeend;
  171. frag = frag_next(frag);
  172. continue;
  173. } else {
  174. uint32_t readlen;
  175. uint32_t fragofs; /* offset within the frag to start reading */
  176. fragofs = offset - frag->ofs;
  177. readlen = min(frag->size - fragofs, end - offset);
  178. D1(printk(KERN_DEBUG "Reading %d-%d from node at 0x%08x (%d)\n",
  179. frag->ofs+fragofs, frag->ofs+fragofs+readlen,
  180. ref_offset(frag->node->raw), ref_flags(frag->node->raw)));
  181. ret = jffs2_read_dnode(c, f, frag->node, buf, fragofs + frag->ofs - frag->node->ofs, readlen);
  182. D2(printk(KERN_DEBUG "node read done\n"));
  183. if (ret) {
  184. D1(printk(KERN_DEBUG"jffs2_read_inode_range error %d\n",ret));
  185. memset(buf, 0, readlen);
  186. return ret;
  187. }
  188. buf += readlen;
  189. offset += readlen;
  190. frag = frag_next(frag);
  191. D2(printk(KERN_DEBUG "node read was OK. Looping\n"));
  192. }
  193. }
  194. return 0;
  195. }