summaryrefslogtreecommitdiff
path: root/fs
diff options
context:
space:
mode:
authorHolger Assmann <h.assmann@pengutronix.de>2022-02-07 18:32:39 +0100
committerSascha Hauer <s.hauer@pengutronix.de>2022-02-08 09:19:29 +0100
commit9ef38876713229f7d0e267866c667d0b65c406da (patch)
tree052f9a64b8784ae5fdc53e627832c311983f8faa /fs
parente4a7f8e21cc55e633ca7997819d8ebdb94d305af (diff)
downloadbarebox-9ef38876713229f7d0e267866c667d0b65c406da.tar.gz
fs: jffs2: fix error when reading blocks with offset
This bug resulted in a panic when trying to read a file partially with an offset, e.g. when starting Linux. In such a case a header is loaded by a separate call of jffs2_read(), which then copies only the first n bytes out of the respective block. When the remaining data of the block is then subsequently going to to be read the system deviates in its behaviour from that under Linux by not calling jffs2_read_inode_range() in a 4k-alignment, but with an offset. jffs2_read_inode_range() originates from the Linux jffs2 driver. When being called with an offset it still reads 4096 bytes of data and eventually returns fragments of two consecutive blocks. jffs2_read() then reads this result whilst again applying the offset, therefore returning faulty data. We fix that problem by calling jffs2_get_block() without an offset and therefore reading the whole block. The offset is then applied when we actually perform memcpy with the returned buffer. This fix might also increase the performance since the respective block is likely to be cached from the previous call. Signed-off-by: Holger Assmann <h.assmann@pengutronix.de> Reviewed-by: Ahmad Fatoum <a.fatoum@pengutronix.de> Link: https://lore.barebox.org/20220207173239.772421-1-h.assmann@pengutronix.de Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'fs')
-rw-r--r--fs/jffs2/fs.c6
1 files changed, 5 insertions, 1 deletions
diff --git a/fs/jffs2/fs.c b/fs/jffs2/fs.c
index f3f472d687..15bb63990b 100644
--- a/fs/jffs2/fs.c
+++ b/fs/jffs2/fs.c
@@ -72,6 +72,9 @@ static int jffs2_get_block(struct jffs2_file *jf, unsigned int pos)
struct jffs2_inode_info *f = JFFS2_INODE_INFO(jf->inode);
int ret;
+ /* pos always has to be 4096 bytes aligned here */
+ WARN_ON(pos % JFFS2_BLOCK_SIZE != 0);
+
if (pos != jf->offset) {
ret = jffs2_read_inode_range(c, f, jf->buf, pos,
JFFS2_BLOCK_SIZE);
@@ -96,12 +99,13 @@ static int jffs2_read(struct device_d *_dev, FILE *f, void *buf,
/* Read till end of current block */
ofs = f->pos % JFFS2_BLOCK_SIZE;
if (ofs) {
- ret = jffs2_get_block(jf, pos);
+ ret = jffs2_get_block(jf, f->pos - ofs); /* Align down block */
if (ret)
return ret;
now = min(size, JFFS2_BLOCK_SIZE - ofs);
+ /* Complete block has been read, re-apply ofset now */
memcpy(buf, jf->buf + ofs, now);
size -= now;
pos += now;