ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / jffs2 / read.c
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@redhat.com>
7  *
8  * For licensing information, see the file 'LICENCE' in this directory.
9  *
10  * $Id: read.c,v 1.34 2003/10/04 08:33:06 dwmw2 Exp $
11  *
12  */
13
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/crc32.h>
17 #include <linux/pagemap.h>
18 #include <linux/mtd/mtd.h>
19 #include <linux/compiler.h>
20 #include "nodelist.h"
21
22 int jffs2_read_dnode(struct jffs2_sb_info *c, struct jffs2_full_dnode *fd, unsigned char *buf, 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
31         ri = jffs2_alloc_raw_inode();
32         if (!ri)
33                 return -ENOMEM;
34
35         ret = jffs2_flash_read(c, ref_offset(fd->raw), sizeof(*ri), &readlen, (char *)ri);
36         if (ret) {
37                 jffs2_free_raw_inode(ri);
38                 printk(KERN_WARNING "Error reading node from 0x%08x: %d\n", ref_offset(fd->raw), ret);
39                 return ret;
40         }
41         if (readlen != sizeof(*ri)) {
42                 jffs2_free_raw_inode(ri);
43                 printk(KERN_WARNING "Short read from 0x%08x: wanted 0x%zx bytes, got 0x%zx\n", 
44                        ref_offset(fd->raw), sizeof(*ri), readlen);
45                 return -EIO;
46         }
47         crc = crc32(0, ri, sizeof(*ri)-8);
48
49         D1(printk(KERN_DEBUG "Node read from %08x: node_crc %08x, calculated CRC %08x. dsize %x, csize %x, offset %x, buf %p\n",
50                   ref_offset(fd->raw), je32_to_cpu(ri->node_crc),
51                   crc, je32_to_cpu(ri->dsize), je32_to_cpu(ri->csize),
52                   je32_to_cpu(ri->offset), buf));
53         if (crc != je32_to_cpu(ri->node_crc)) {
54                 printk(KERN_WARNING "Node CRC %08x != calculated CRC %08x for node at %08x\n",
55                        je32_to_cpu(ri->node_crc), crc, ref_offset(fd->raw));
56                 ret = -EIO;
57                 goto out_ri;
58         }
59         /* There was a bug where we wrote hole nodes out with csize/dsize
60            swapped. Deal with it */
61         if (ri->compr == JFFS2_COMPR_ZERO && !je32_to_cpu(ri->dsize) && 
62             je32_to_cpu(ri->csize)) {
63                 ri->dsize = ri->csize;
64                 ri->csize = cpu_to_je32(0);
65         }
66
67         D1(if(ofs + len > je32_to_cpu(ri->dsize)) {
68                 printk(KERN_WARNING "jffs2_read_dnode() asked for %d bytes at %d from %d-byte node\n",
69                        len, ofs, je32_to_cpu(ri->dsize));
70                 ret = -EINVAL;
71                 goto out_ri;
72         });
73
74         
75         if (ri->compr == JFFS2_COMPR_ZERO) {
76                 memset(buf, 0, len);
77                 goto out_ri;
78         }
79
80         /* Cases:
81            Reading whole node and it's uncompressed - read directly to buffer provided, check CRC.
82            Reading whole node and it's compressed - read into comprbuf, check CRC and decompress to buffer provided 
83            Reading partial node and it's uncompressed - read into readbuf, check CRC, and copy 
84            Reading partial node and it's compressed - read into readbuf, check checksum, decompress to decomprbuf and copy
85         */
86         if (ri->compr == JFFS2_COMPR_NONE && len == je32_to_cpu(ri->dsize)) {
87                 readbuf = buf;
88         } else {
89                 readbuf = kmalloc(je32_to_cpu(ri->csize), GFP_KERNEL);
90                 if (!readbuf) {
91                         ret = -ENOMEM;
92                         goto out_ri;
93                 }
94         }
95         if (ri->compr != JFFS2_COMPR_NONE) {
96                 if (len < je32_to_cpu(ri->dsize)) {
97                         decomprbuf = kmalloc(je32_to_cpu(ri->dsize), GFP_KERNEL);
98                         if (!decomprbuf) {
99                                 ret = -ENOMEM;
100                                 goto out_readbuf;
101                         }
102                 } else {
103                         decomprbuf = buf;
104                 }
105         } else {
106                 decomprbuf = readbuf;
107         }
108
109         D2(printk(KERN_DEBUG "Read %d bytes to %p\n", je32_to_cpu(ri->csize),
110                   readbuf));
111         ret = jffs2_flash_read(c, (ref_offset(fd->raw)) + sizeof(*ri),
112                                je32_to_cpu(ri->csize), &readlen, readbuf);
113
114         if (!ret && readlen != je32_to_cpu(ri->csize))
115                 ret = -EIO;
116         if (ret)
117                 goto out_decomprbuf;
118
119         crc = crc32(0, readbuf, je32_to_cpu(ri->csize));
120         if (crc != je32_to_cpu(ri->data_crc)) {
121                 printk(KERN_WARNING "Data CRC %08x != calculated CRC %08x for node at %08x\n",
122                        je32_to_cpu(ri->data_crc), crc, ref_offset(fd->raw));
123                 ret = -EIO;
124                 goto out_decomprbuf;
125         }
126         D2(printk(KERN_DEBUG "Data CRC matches calculated CRC %08x\n", crc));
127         if (ri->compr != JFFS2_COMPR_NONE) {
128                 D2(printk(KERN_DEBUG "Decompress %d bytes from %p to %d bytes at %p\n",
129                           je32_to_cpu(ri->csize), readbuf, je32_to_cpu(ri->dsize), decomprbuf)); 
130                 ret = jffs2_decompress(ri->compr, readbuf, decomprbuf, je32_to_cpu(ri->csize), je32_to_cpu(ri->dsize));
131                 if (ret) {
132                         printk(KERN_WARNING "Error: jffs2_decompress returned %d\n", ret);
133                         goto out_decomprbuf;
134                 }
135         }
136
137         if (len < je32_to_cpu(ri->dsize)) {
138                 memcpy(buf, decomprbuf+ofs, len);
139         }
140  out_decomprbuf:
141         if(decomprbuf != buf && decomprbuf != readbuf)
142                 kfree(decomprbuf);
143  out_readbuf:
144         if(readbuf != buf)
145                 kfree(readbuf);
146  out_ri:
147         jffs2_free_raw_inode(ri);
148
149         return ret;
150 }
151
152 int jffs2_read_inode_range(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
153                            unsigned char *buf, uint32_t offset, uint32_t len)
154 {
155         uint32_t end = offset + len;
156         struct jffs2_node_frag *frag;
157         int ret;
158
159         D1(printk(KERN_DEBUG "jffs2_read_inode_range: ino #%u, range 0x%08x-0x%08x\n",
160                   f->inocache->ino, offset, offset+len));
161
162         frag = jffs2_lookup_node_frag(&f->fragtree, offset);
163
164         /* XXX FIXME: Where a single physical node actually shows up in two
165            frags, we read it twice. Don't do that. */
166         /* Now we're pointing at the first frag which overlaps our page */
167         while(offset < end) {
168                 D2(printk(KERN_DEBUG "jffs2_read_inode_range: offset %d, end %d\n", offset, end));
169                 if (unlikely(!frag || frag->ofs > offset)) {
170                         uint32_t holesize = end - offset;
171                         if (frag) {
172                                 D1(printk(KERN_NOTICE "Eep. Hole in ino #%u fraglist. frag->ofs = 0x%08x, offset = 0x%08x\n", f->inocache->ino, frag->ofs, offset));
173                                 holesize = min(holesize, frag->ofs - offset);
174                                 D1(jffs2_print_frag_list(f));
175                         }
176                         D1(printk(KERN_DEBUG "Filling non-frag hole from %d-%d\n", offset, offset+holesize));
177                         memset(buf, 0, holesize);
178                         buf += holesize;
179                         offset += holesize;
180                         continue;
181                 } else if (unlikely(!frag->node)) {
182                         uint32_t holeend = min(end, frag->ofs + frag->size);
183                         D1(printk(KERN_DEBUG "Filling frag hole from %d-%d (frag 0x%x 0x%x)\n", offset, holeend, frag->ofs, frag->ofs + frag->size));
184                         memset(buf, 0, holeend - offset);
185                         buf += holeend - offset;
186                         offset = holeend;
187                         frag = frag_next(frag);
188                         continue;
189                 } else {
190                         uint32_t readlen;
191                         uint32_t fragofs; /* offset within the frag to start reading */
192                         
193                         fragofs = offset - frag->ofs;
194                         readlen = min(frag->size - fragofs, end - offset);
195                         D1(printk(KERN_DEBUG "Reading %d-%d from node at 0x%08x (%d)\n",
196                                   frag->ofs+fragofs, frag->ofs+fragofs+readlen,
197                                   ref_offset(frag->node->raw), ref_flags(frag->node->raw)));
198                         ret = jffs2_read_dnode(c, frag->node, buf, fragofs + frag->ofs - frag->node->ofs, readlen);
199                         D2(printk(KERN_DEBUG "node read done\n"));
200                         if (ret) {
201                                 D1(printk(KERN_DEBUG"jffs2_read_inode_range error %d\n",ret));
202                                 memset(buf, 0, readlen);
203                                 return ret;
204                         }
205                         buf += readlen;
206                         offset += readlen;
207                         frag = frag_next(frag);
208                         D2(printk(KERN_DEBUG "node read was OK. Looping\n"));
209                 }
210         }
211         return 0;
212 }
213
214 /* Core function to read symlink target. */
215 char *jffs2_getlink(struct jffs2_sb_info *c, struct jffs2_inode_info *f)
216 {
217         char *buf;
218         int ret;
219
220         down(&f->sem);
221
222         if (!f->metadata) {
223                 printk(KERN_NOTICE "No metadata for symlink inode #%u\n", f->inocache->ino);
224                 up(&f->sem);
225                 return ERR_PTR(-EINVAL);
226         }
227         buf = kmalloc(f->metadata->size+1, GFP_USER);
228         if (!buf) {
229                 up(&f->sem);
230                 return ERR_PTR(-ENOMEM);
231         }
232         buf[f->metadata->size]=0;
233
234         ret = jffs2_read_dnode(c, f->metadata, buf, 0, f->metadata->size);
235
236         up(&f->sem);
237
238         if (ret) {
239                 kfree(buf);
240                 return ERR_PTR(ret);
241         }
242         return buf;
243 }