u-boot.lists.denx.de archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] fs: fat: check for buffer size before reading blocks
@ 2021-09-26 18:36 Oleksandr Suvorov
  2021-10-13 14:16 ` Tom Rini
  0 siblings, 1 reply; 2+ messages in thread
From: Oleksandr Suvorov @ 2021-09-26 18:36 UTC (permalink / raw)
  To: u-boot
  Cc: Ricardo Salveti, Oleksandr Suvorov, Christian Gmeiner,
	Heinrich Schuchardt, Jason Wessel, Simon Glass

From: Ricardo Salveti <ricardo@foundries.io>

This patch optimizes the commit mentioned below by avoiding running
a set of commands which are useless in the case when
size < mydata->sect_size and sect_count would be 0.

Fixes: 5b3ddb17ba ("fs/fat/fat.c: Do not perform zero block reads if there are no blocks left")

Signed-off-by: Ricardo Salveti <ricardo@foundries.io>
Co-developed-by: Oleksandr Suvorov <oleksandr.suvorov@foundries.io>
Signed-off-by: Oleksandr Suvorov <oleksandr.suvorov@foundries.io>
---

Changes in v2:
- rename the variable idx to sect_count;
- add bytes_read variable, making the code more readable.

 fs/fat/fat.c | 21 +++++++++------------
 1 file changed, 9 insertions(+), 12 deletions(-)

diff --git a/fs/fat/fat.c b/fs/fat/fat.c
index 7021138b98..65f77c4f75 100644
--- a/fs/fat/fat.c
+++ b/fs/fat/fat.c
@@ -275,22 +275,19 @@ get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
 			buffer += mydata->sect_size;
 			size -= mydata->sect_size;
 		}
-	} else {
-		__u32 idx;
+	} else if (size >= mydata->sect_size) {
+		__u32 bytes_read;
+		__u32 sect_count = size / mydata->sect_size;
 
-		idx = size / mydata->sect_size;
-		if (idx == 0)
-			ret = 0;
-		else
-			ret = disk_read(startsect, idx, buffer);
-		if (ret != idx) {
+		ret = disk_read(startsect, sect_count, buffer);
+		if (ret != sect_count) {
 			debug("Error reading data (got %d)\n", ret);
 			return -1;
 		}
-		startsect += idx;
-		idx *= mydata->sect_size;
-		buffer += idx;
-		size -= idx;
+		bytes_read = sect_count * mydata->sect_size;
+		startsect += sect_count;
+		buffer += bytes_read;
+		size -= bytes_read;
 	}
 	if (size) {
 		ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
-- 
2.31.1


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

* Re: [PATCH v2] fs: fat: check for buffer size before reading blocks
  2021-09-26 18:36 [PATCH v2] fs: fat: check for buffer size before reading blocks Oleksandr Suvorov
@ 2021-10-13 14:16 ` Tom Rini
  0 siblings, 0 replies; 2+ messages in thread
From: Tom Rini @ 2021-10-13 14:16 UTC (permalink / raw)
  To: Oleksandr Suvorov
  Cc: u-boot, Ricardo Salveti, Christian Gmeiner, Heinrich Schuchardt,
	Jason Wessel, Simon Glass

[-- Attachment #1: Type: text/plain, Size: 657 bytes --]

On Sun, Sep 26, 2021 at 09:36:04PM +0300, Oleksandr Suvorov wrote:

> From: Ricardo Salveti <ricardo@foundries.io>
> 
> This patch optimizes the commit mentioned below by avoiding running
> a set of commands which are useless in the case when
> size < mydata->sect_size and sect_count would be 0.
> 
> Fixes: 5b3ddb17ba ("fs/fat/fat.c: Do not perform zero block reads if there are no blocks left")
> 
> Signed-off-by: Ricardo Salveti <ricardo@foundries.io>
> Co-developed-by: Oleksandr Suvorov <oleksandr.suvorov@foundries.io>
> Signed-off-by: Oleksandr Suvorov <oleksandr.suvorov@foundries.io>

Applied to u-boot/master, thanks!

-- 
Tom

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 659 bytes --]

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

end of thread, other threads:[~2021-10-13 14:17 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-26 18:36 [PATCH v2] fs: fat: check for buffer size before reading blocks Oleksandr Suvorov
2021-10-13 14:16 ` Tom Rini

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).