mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH v3] fs: jffs2: fix error when reading blocks with offset
@ 2022-02-07 17:32 Holger Assmann
  2022-02-08  8:19 ` Sascha Hauer
  0 siblings, 1 reply; 2+ messages in thread
From: Holger Assmann @ 2022-02-07 17:32 UTC (permalink / raw)
  To: barebox; +Cc: Holger Assmann, Ahmad Fatoum

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>
---

Changes v1 -> v2:
 - removed stray spaces
 - added review acknowledgement
Changes v2 -> v3:
 - use actual "JFFS2_BLOCK_SIZE" instead of "4096"

 fs/jffs2/fs.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/fs/jffs2/fs.c b/fs/jffs2/fs.c
index a267ec0669..0c57f1d07f 100644
--- a/fs/jffs2/fs.c
+++ b/fs/jffs2/fs.c
@@ -74,6 +74,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);
@@ -98,12 +101,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;
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH v3] fs: jffs2: fix error when reading blocks with offset
  2022-02-07 17:32 [PATCH v3] fs: jffs2: fix error when reading blocks with offset Holger Assmann
@ 2022-02-08  8:19 ` Sascha Hauer
  0 siblings, 0 replies; 2+ messages in thread
From: Sascha Hauer @ 2022-02-08  8:19 UTC (permalink / raw)
  To: Holger Assmann; +Cc: barebox, Ahmad Fatoum

On Mon, Feb 07, 2022 at 06:32:39PM +0100, Holger Assmann wrote:
> 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>
> ---

Applied, thanks

Sascha

> 
> Changes v1 -> v2:
>  - removed stray spaces
>  - added review acknowledgement
> Changes v2 -> v3:
>  - use actual "JFFS2_BLOCK_SIZE" instead of "4096"
> 
>  fs/jffs2/fs.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/jffs2/fs.c b/fs/jffs2/fs.c
> index a267ec0669..0c57f1d07f 100644
> --- a/fs/jffs2/fs.c
> +++ b/fs/jffs2/fs.c
> @@ -74,6 +74,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);
> @@ -98,12 +101,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;
> -- 
> 2.30.2
> 
> 
> _______________________________________________
> barebox mailing list
> barebox@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/barebox
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-02-08  8:24 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-07 17:32 [PATCH v3] fs: jffs2: fix error when reading blocks with offset Holger Assmann
2022-02-08  8:19 ` Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox