embox

Форк
0
221 строка · 6.7 Кб
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.41 2005/07/22 10:32:08 dedekind 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
#include "compr.h"
22

23
int jffs2_read_dnode(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
24
		     struct jffs2_full_dnode *fd, unsigned char *buf,
25
		     int ofs, int len) {
26
	struct jffs2_raw_inode *ri;
27
	size_t readlen;
28
	uint32_t crc;
29
	unsigned char *decomprbuf = NULL;
30
	unsigned char *readbuf = NULL;
31
	int ret = 0;
32

33
	ri = jffs2_alloc_raw_inode();
34
	if (!ri) {
35
		return -ENOMEM;
36
	}
37

38
	ret = jffs2_flash_read(c, ref_offset(fd->raw),
39
			sizeof(*ri), &readlen, (unsigned char *)ri);
40
	if (ret) {
41
		jffs2_free_raw_inode(ri);
42
		printk(KERN_WARNING "Error reading node from 0x%08x: %d\n", ref_offset(fd->raw), ret);
43
		return ret;
44
	}
45
	if (readlen != sizeof(*ri)) {
46
		jffs2_free_raw_inode(ri);
47
		printk(KERN_WARNING "Short read from 0x%08x: wanted 0x%zx bytes, got 0x%zx\n",
48
		       ref_offset(fd->raw), sizeof(*ri), readlen);
49
		return -EIO;
50
	}
51
	crc = crc32(0, ri, sizeof(*ri)-8);
52

53
	D1(printk( "Node read from %08x: node_crc %08x, calculated CRC %08x. dsize %x, csize %x, offset %x, buf %p\n",
54
		  ref_offset(fd->raw), je32_to_cpu(ri->node_crc),
55
		  crc, je32_to_cpu(ri->dsize), je32_to_cpu(ri->csize),
56
		  je32_to_cpu(ri->offset), buf));
57
	if (crc != je32_to_cpu(ri->node_crc)) {
58
		printk(KERN_WARNING "Node CRC %08x != calculated CRC %08x for node at %08x\n",
59
		       je32_to_cpu(ri->node_crc), crc, ref_offset(fd->raw));
60
		ret = -EIO;
61
		goto out_ri;
62
	}
63
	/* There was a bug where we wrote hole nodes out with csize/dsize
64
	   swapped. Deal with it */
65
	if (ri->compr == JFFS2_COMPR_ZERO && !je32_to_cpu(ri->dsize) &&
66
	    je32_to_cpu(ri->csize)) {
67
		ri->dsize = ri->csize;
68
		ri->csize = cpu_to_je32(0);
69
	}
70

71
	D1(if(ofs + len > je32_to_cpu(ri->dsize)) {
72
		printk(KERN_WARNING "jffs2_read_dnode() asked for %d bytes at %d from %d-byte node\n",
73
		       len, ofs, je32_to_cpu(ri->dsize));
74
		ret = -EINVAL;
75
		goto out_ri;
76
	});
77

78

79
	if (ri->compr == JFFS2_COMPR_ZERO) {
80
		memset(buf, 0, len);
81
		goto out_ri;
82
	}
83

84
	/* Cases:
85
	   Reading whole node and it's uncompressed - read directly to buffer provided, check CRC.
86
	   Reading whole node and it's compressed - read into comprbuf, check CRC and decompress to buffer provided
87
	   Reading partial node and it's uncompressed - read into readbuf, check CRC, and copy
88
	   Reading partial node and it's compressed - read into readbuf, check checksum, decompress to decomprbuf and copy
89
	*/
90
	if (ri->compr == JFFS2_COMPR_NONE && len == je32_to_cpu(ri->dsize)) {
91
		readbuf = buf;
92
	} else {
93
		readbuf = kmalloc(je32_to_cpu(ri->csize), GFP_KERNEL);
94
		if (!readbuf) {
95
			ret = -ENOMEM;
96
			goto out_ri;
97
		}
98
	}
99
	if (ri->compr != JFFS2_COMPR_NONE) {
100
		if (len < je32_to_cpu(ri->dsize)) {
101
			decomprbuf = kmalloc(je32_to_cpu(ri->dsize), GFP_KERNEL);
102
			if (!decomprbuf) {
103
				ret = -ENOMEM;
104
				goto out_readbuf;
105
			}
106
		} else {
107
			decomprbuf = buf;
108
		}
109
	} else {
110
		decomprbuf = readbuf;
111
	}
112

113
	D2(printk( "Read %d bytes to %p\n", je32_to_cpu(ri->csize),
114
		  readbuf));
115
	ret = jffs2_flash_read(c, (ref_offset(fd->raw)) + sizeof(*ri),
116
			       je32_to_cpu(ri->csize), &readlen, readbuf);
117

118
	if (!ret && readlen != je32_to_cpu(ri->csize)) {
119
		ret = -EIO;
120
	}
121
	if (ret) {
122
		goto out_decomprbuf;
123
	}
124

125
	crc = crc32(0, readbuf, je32_to_cpu(ri->csize));
126
	if (crc != je32_to_cpu(ri->data_crc)) {
127
		printk(KERN_WARNING "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
	D2(printk( "Data CRC matches calculated CRC %08x\n", crc));
133
	if (ri->compr != JFFS2_COMPR_NONE) {
134
		D2(printk( "Decompress %d bytes from %p to %d bytes at %p\n",
135
			  je32_to_cpu(ri->csize), readbuf, je32_to_cpu(ri->dsize), decomprbuf));
136
		ret = jffs2_decompress(c, f, ri->compr | (ri->usercompr << 8), readbuf,
137
				decomprbuf, je32_to_cpu(ri->csize), je32_to_cpu(ri->dsize));
138
		if (ret) {
139
			printk(KERN_WARNING "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
	}
151
 out_readbuf:
152
	if(readbuf != buf) {
153
		kfree(readbuf);
154
	}
155
 out_ri:
156
	jffs2_free_raw_inode(ri);
157

158
	return ret;
159
}
160

161
int jffs2_read_inode_range(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
162
			   unsigned char *buf, uint32_t offset, uint32_t len)
163
{
164
	uint32_t end = offset + len;
165
	struct jffs2_node_frag *frag;
166
	int ret;
167

168
	D1(printk( "jffs2_read_inode_range: ino #%u, range 0x%08x-0x%08x\n",
169
		  f->inocache->ino, offset, offset + len));
170

171
	frag = jffs2_lookup_node_frag(&f->fragtree, offset);
172

173
	/* XXX FIXME: Where a single physical node actually shows up in two
174
	   frags, we read it twice. Don't do that. */
175
	/* Now we're pointing at the first frag which overlaps our page */
176
	while(offset < end) {
177
		D2(printk( "jffs2_read_inode_range: offset %d, end %d\n", offset, end));
178
		if (unlikely(!frag || frag->ofs > offset)) {
179
			uint32_t holesize = end - offset;
180
			if (frag) {
181
				D1(printk(KERN_NOTICE "Eep. Hole in ino #%u fraglist. frag->ofs = 0x%08x, offset = 0x%08x\n", f->inocache->ino, frag->ofs, offset));
182
				holesize = min(holesize, frag->ofs - offset);
183
			}
184
			D1(printk( "Filling non-frag hole from %d-%d\n", offset, offset+holesize));
185
			memset(buf, 0, holesize);
186
			buf += holesize;
187
			offset += holesize;
188
			continue;
189
		} else if (unlikely(!frag->node)) {
190
			uint32_t holeend = min(end, frag->ofs + frag->size);
191
			D1(printk( "Filling frag hole from %d-%d (frag 0x%x 0x%x)\n", offset, holeend, frag->ofs, frag->ofs + frag->size));
192
			memset(buf, 0, holeend - offset);
193
			buf += holeend - offset;
194
			offset = holeend;
195
			frag = frag_next(frag);
196
			continue;
197
		} else {
198
			uint32_t readlen;
199
			uint32_t fragofs; /* offset within the frag to start reading */
200

201
			fragofs = offset - frag->ofs;
202
			readlen = min(frag->size - fragofs, end - offset);
203
			D1(printk( "Reading %d-%d from node at 0x%08x (%d)\n",
204
				  frag->ofs + fragofs, frag->ofs + fragofs + readlen,
205
				  ref_offset(frag->node->raw), ref_flags(frag->node->raw)));
206
			ret = jffs2_read_dnode(c, f, frag->node, buf,
207
					fragofs + frag->ofs - frag->node->ofs, readlen);
208
			D2(printk( "node read done\n"));
209
			if (ret) {
210
				D1(printk("jffs2_read_inode_range error %d\n",ret));
211
				memset(buf, 0, readlen);
212
				return ret;
213
			}
214
			buf += readlen;
215
			offset += readlen;
216
			frag = frag_next(frag);
217
			D2(printk( "node read was OK. Looping\n"));
218
		}
219
	}
220
	return 0;
221
}
222

223

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.