mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 0/4] ext4 fixes
@ 2019-10-29  9:10 Sascha Hauer
  2019-10-29  9:10 ` [PATCH 1/4] fs: ext4: Fix out of bounds memset Sascha Hauer
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Sascha Hauer @ 2019-10-29  9:10 UTC (permalink / raw)
  To: Barebox List

This series fixes some issues with the ext4 code regarding to sparse
files.

Sascha

Sascha Hauer (4):
  fs: ext4: Fix out of bounds memset
  fs: ext4: remove unnecessarily clever file read
  fs: ext4: Fix handling of sparse files
  fs: ext4: return NULL instead of 0

 fs/ext4/ext4_common.c | 32 +++++++++++------------
 fs/ext4/ext4fs.c      | 61 +++++--------------------------------------
 2 files changed, 23 insertions(+), 70 deletions(-)

-- 
2.24.0.rc1


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

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

* [PATCH 1/4] fs: ext4: Fix out of bounds memset
  2019-10-29  9:10 [PATCH 0/4] ext4 fixes Sascha Hauer
@ 2019-10-29  9:10 ` Sascha Hauer
  2019-10-29  9:10 ` [PATCH 2/4] fs: ext4: remove unnecessarily clever file read Sascha Hauer
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2019-10-29  9:10 UTC (permalink / raw)
  To: Barebox List

When a block we read is a sparse block, we memset the corresponding
output buffer to zero. If that block is the last block we read, we
may not memset the whole block, but only up to the length of the output
buffer, which may be shorter than a full block.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 fs/ext4/ext4fs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/ext4/ext4fs.c b/fs/ext4/ext4fs.c
index bfc5f27cc3..acecccd6b9 100644
--- a/fs/ext4/ext4fs.c
+++ b/fs/ext4/ext4fs.c
@@ -135,7 +135,7 @@ int ext4fs_read_file(struct ext2fs_node *node, int pos,
 					return ret;
 				previous_block_number = -1;
 			}
-			memset(buf, 0, blocksize - skipfirst);
+			memset(buf, 0, blockend);
 		}
 		buf += blocksize - skipfirst;
 	}
-- 
2.24.0.rc1


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

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

* [PATCH 2/4] fs: ext4: remove unnecessarily clever file read
  2019-10-29  9:10 [PATCH 0/4] ext4 fixes Sascha Hauer
  2019-10-29  9:10 ` [PATCH 1/4] fs: ext4: Fix out of bounds memset Sascha Hauer
@ 2019-10-29  9:10 ` Sascha Hauer
  2019-10-29  9:10 ` [PATCH 3/4] fs: ext4: Fix handling of sparse files Sascha Hauer
  2019-10-29  9:10 ` [PATCH 4/4] fs: ext4: return NULL instead of 0 Sascha Hauer
  3 siblings, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2019-10-29  9:10 UTC (permalink / raw)
  To: Barebox List

ext4fs_read_file() tries to tries to bundle contiguous block reads into
longer reads from the device. In barebox we cache in the block layer
already, so this is unnecessary. Simplify the code by removing the
bundled reads.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 fs/ext4/ext4fs.c | 59 +++++-------------------------------------------
 1 file changed, 6 insertions(+), 53 deletions(-)

diff --git a/fs/ext4/ext4fs.c b/fs/ext4/ext4fs.c
index acecccd6b9..2d231d273a 100644
--- a/fs/ext4/ext4fs.c
+++ b/fs/ext4/ext4fs.c
@@ -55,12 +55,6 @@ int ext4fs_read_file(struct ext2fs_node *node, int pos,
 	int log2blocksize = LOG2_EXT2_BLOCK_SIZE(node->data);
 	int blocksize = 1 << (log2blocksize + DISK_SECTOR_BITS);
 	unsigned int filesize = le32_to_cpu(node->inode.size);
-	int previous_block_number = -1;
-	int delayed_start = 0;
-	int delayed_extent = 0;
-	int delayed_skipfirst = 0;
-	int delayed_next = 0;
-	char *delayed_buf = NULL;
 	short ret;
 	struct ext_filesystem *fs = node->data->fs;
 
@@ -75,6 +69,7 @@ int ext4fs_read_file(struct ext2fs_node *node, int pos,
 		int blockoff = pos % blocksize;
 		int blockend = blocksize;
 		int skipfirst = 0;
+
 		blknr = read_allocated_block(node, i);
 		if (blknr < 0)
 			return blknr;
@@ -95,59 +90,17 @@ int ext4fs_read_file(struct ext2fs_node *node, int pos,
 			skipfirst = blockoff;
 			blockend -= skipfirst;
 		}
+
 		if (blknr) {
-			if (previous_block_number != -1) {
-				if (delayed_next == blknr) {
-					delayed_extent += blockend;
-					delayed_next += blockend >> SECTOR_BITS;
-				} else {	/* spill */
-					ret = ext4fs_devread(fs, delayed_start,
-							delayed_skipfirst,
-							delayed_extent,
-							delayed_buf);
-					if (ret)
-						return ret;
-					previous_block_number = blknr;
-					delayed_start = blknr;
-					delayed_extent = blockend;
-					delayed_skipfirst = skipfirst;
-					delayed_buf = buf;
-					delayed_next = blknr +
-						(blockend >> SECTOR_BITS);
-				}
-			} else {
-				previous_block_number = blknr;
-				delayed_start = blknr;
-				delayed_extent = blockend;
-				delayed_skipfirst = skipfirst;
-				delayed_buf = buf;
-				delayed_next = blknr +
-					(blockend >> SECTOR_BITS);
-			}
+			ret = ext4fs_devread(fs, blknr, skipfirst, blockend, buf);
+			if (ret)
+				return ret;
 		} else {
-			if (previous_block_number != -1) {
-				/* spill */
-				ret = ext4fs_devread(fs, delayed_start,
-							delayed_skipfirst,
-							delayed_extent,
-							delayed_buf);
-				if (ret)
-					return ret;
-				previous_block_number = -1;
-			}
 			memset(buf, 0, blockend);
 		}
+
 		buf += blocksize - skipfirst;
 	}
-	if (previous_block_number != -1) {
-		/* spill */
-		ret = ext4fs_devread(fs, delayed_start,
-					delayed_skipfirst, delayed_extent,
-					delayed_buf);
-		if (ret)
-			return ret;
-		previous_block_number = -1;
-	}
 
 	return len;
 }
-- 
2.24.0.rc1


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

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

* [PATCH 3/4] fs: ext4: Fix handling of sparse files
  2019-10-29  9:10 [PATCH 0/4] ext4 fixes Sascha Hauer
  2019-10-29  9:10 ` [PATCH 1/4] fs: ext4: Fix out of bounds memset Sascha Hauer
  2019-10-29  9:10 ` [PATCH 2/4] fs: ext4: remove unnecessarily clever file read Sascha Hauer
@ 2019-10-29  9:10 ` Sascha Hauer
  2019-10-29  9:10 ` [PATCH 4/4] fs: ext4: return NULL instead of 0 Sascha Hauer
  3 siblings, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2019-10-29  9:10 UTC (permalink / raw)
  To: Barebox List

Adoption of U-Boot commit:

| commit f81db56f2fd6dc16efeaec2961121244765a1f11
| Author: Stefan Brüns <stefan.bruens@rwth-aachen.de>
| Date:   Sat Nov 5 22:17:14 2016 +0100
|
|     ext4: Fix handling of sparse files
|
|     A sparse file may have regions not mapped by any extents, at the start
|     or at the end of the file, or anywhere between, thus not finding a
|     matching extent region is never an error.
|
|     Found by python filesystem tests.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 fs/ext4/ext4_common.c | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)

diff --git a/fs/ext4/ext4_common.c b/fs/ext4/ext4_common.c
index 55e837b4bd..2703b55e99 100644
--- a/fs/ext4/ext4_common.c
+++ b/fs/ext4/ext4_common.c
@@ -167,10 +167,11 @@ long int read_allocated_block(struct ext2fs_node *node, int fileblock)
 	log2_blksz = LOG2_EXT2_BLOCK_SIZE(node->data);
 
 	if (le32_to_cpu(inode->flags) & EXT4_EXTENTS_FL) {
+		long int startblock, endblock;
 		char *buf = zalloc(blksz);
 		struct ext4_extent_header *ext_block;
 		struct ext4_extent *extent;
-		int i = -1;
+		int i;
 
 		if (!buf)
 			return -ENOMEM;
@@ -186,28 +187,27 @@ long int read_allocated_block(struct ext2fs_node *node, int fileblock)
 
 		extent = (struct ext4_extent *)(ext_block + 1);
 
-		do {
-			i++;
-			if (i >= le16_to_cpu(ext_block->eh_entries))
-				break;
-		} while (fileblock >= le32_to_cpu(extent[i].ee_block));
+		for (i = 0; i < le16_to_cpu(ext_block->eh_entries); i++) {
+			startblock = le32_to_cpu(extent[i].ee_block);
+			endblock = startblock + le16_to_cpu(extent[i].ee_len);
 
-		if (--i >= 0) {
-			fileblock -= le32_to_cpu(extent[i].ee_block);
-			if (fileblock >= le16_to_cpu(extent[i].ee_len)) {
+			if (startblock > fileblock) {
+				/* Sparse file */
 				free(buf);
 				return 0;
 			}
 
-			start = le16_to_cpu(extent[i].ee_start_hi);
-			start = (start << 32) +
+			if (fileblock < endblock) {
+				start = le16_to_cpu(extent[i].ee_start_hi);
+				start = (start << 32) +
 					le32_to_cpu(extent[i].ee_start_lo);
-			free(buf);
-			return fileblock + start;
+				free(buf);
+				return (fileblock - startblock) + start;
+			}
 		}
 
 		free(buf);
-		return -EIO;
+		return 0;
 	}
 
 	if (fileblock < INDIRECT_BLOCKS) {
-- 
2.24.0.rc1


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

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

* [PATCH 4/4] fs: ext4: return NULL instead of 0
  2019-10-29  9:10 [PATCH 0/4] ext4 fixes Sascha Hauer
                   ` (2 preceding siblings ...)
  2019-10-29  9:10 ` [PATCH 3/4] fs: ext4: Fix handling of sparse files Sascha Hauer
@ 2019-10-29  9:10 ` Sascha Hauer
  3 siblings, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2019-10-29  9:10 UTC (permalink / raw)
  To: Barebox List

ext4fs_get_extent_block() returns a pointer, so return NULL instead of
integer 0.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 fs/ext4/ext4_common.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/fs/ext4/ext4_common.c b/fs/ext4/ext4_common.c
index 2703b55e99..c9f27f1278 100644
--- a/fs/ext4/ext4_common.c
+++ b/fs/ext4/ext4_common.c
@@ -52,7 +52,7 @@ static struct ext4_extent_header *ext4fs_get_extent_block(struct ext2_data *data
 		index = (struct ext4_extent_idx *)(ext_block + 1);
 
 		if (le16_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC)
-			return 0;
+			return NULL;
 
 		if (ext_block->eh_depth == 0)
 			return ext_block;
@@ -64,7 +64,7 @@ static struct ext4_extent_header *ext4fs_get_extent_block(struct ext2_data *data
 		} while (fileblock >= le32_to_cpu(index[i].ei_block));
 
 		if (--i < 0)
-			return 0;
+			return NULL;
 
 		block = le16_to_cpu(index[i].ei_leaf_hi);
 		block = (block << 32) + le32_to_cpu(index[i].ei_leaf_lo);
-- 
2.24.0.rc1


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

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

end of thread, other threads:[~2019-10-29  9:10 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-29  9:10 [PATCH 0/4] ext4 fixes Sascha Hauer
2019-10-29  9:10 ` [PATCH 1/4] fs: ext4: Fix out of bounds memset Sascha Hauer
2019-10-29  9:10 ` [PATCH 2/4] fs: ext4: remove unnecessarily clever file read Sascha Hauer
2019-10-29  9:10 ` [PATCH 3/4] fs: ext4: Fix handling of sparse files Sascha Hauer
2019-10-29  9:10 ` [PATCH 4/4] fs: ext4: return NULL instead of 0 Sascha Hauer

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