source: rtems/cpukit/libfs/src/jffs2/src/read.c @ 6ac6a5c8

5
Last change on this file since 6ac6a5c8 was 6ac6a5c8, checked in by Sebastian Huber <sebastian.huber@…>, on 03/27/18 at 11:01:56

jffs2: Do not use command line defines

Update #3375.

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