Skip to contentSkip to conversation

fs/fat/fat.c: Do not perform zero block reads if there are no blocks left

PATCH Superseded Jason Wessel 2 participants 2 messages Jun 19, 2020 VFAT/FAT/msdos filesystem

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fs/fat/fat.c: Do not perform zero block reads if there are no blocks left

v2: Fix indentation and remove unneeded braces.

While using u-boot with qemu's virtio driver I stumbled across a problem reading files less than sector size. On the real hardware the block reader seems ok with reading zero blocks, and while we could fix the virtio host side of qemu to deal with a zero block read instead of crashing, the u-boot fat driver should not be doing zero block reads in the first place. If you ask hardware to read zero blocks you are just going to get zero data. There may also be other hardware that responds similarly to the virtio interface so this is worth fixing.

Without the patch I get the following and have to restart qemu because it dies.
---------------------------------
=> fatls virtio 0:1
30 cmdline.txt
=> fatload virtio 0:1 ${loadaddr} cmdline.txt
qemu-system-aarch64: virtio: zero sized buffers are not allowed
---------------------------------

With the patch I get the expected results.
---------------------------------
=> fatls virtio 0:1
30 cmdline.txt
=> fatload virtio 0:1 ${loadaddr} cmdline.txt
30 bytes read in 11 ms (2 KiB/s)
=> md.b ${loadaddr} 0x1E
40080000: 64 77 63 5f 6f 74 67 2e 6c 70 6d 5f 65 6e 61 62 dwc_otg.lpm_enab
40080010: 6c 65 3d 30 20 72 6f 6f 74 77 61 69 74 0a le=0 rootwait.

---------------------------------

---
 fs/fat/fat.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
fs/fat/fat.c +5 -1
index 7fd29470c1..8233a74620 100644
@@ -278,7 +278,11 @@ get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
 		}
 	} else {
 		idx = size / mydata->sect_size;
-		ret = disk_read(startsect, idx, buffer);
+		if (idx == 0) {
+		  ret = 0;
+		} else {
+		  ret = disk_read(startsect, idx, buffer);
+		}
 		if (ret != idx) {
 			debug("Error reading data (got %d)\n", ret);
 			return -1;
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/1 fs/fat/fat.c: Do not perform zero block reads if there are no blocks left

Quoted 1 line · Jason Wessel

v2: Fix indentation and remove unneeded braces.

Series notes go below the '--- '

Quoted 43 lines

While using u-boot with qemu's virtio driver I stumbled across a problem reading files less than sector size. On the real hardware the block reader seems ok with reading zero blocks, and while we could fix the virtio host side of qemu to deal with a zero block read instead of crashing, the u-boot fat driver should not be doing zero block reads in the first place. If you ask hardware to read zero blocks you are just going to get zero data. There may also be other hardware that responds similarly to the virtio interface so this is worth fixing.

Without the patch I get the following and have to restart qemu because it dies.
---------------------------------
=> fatls virtio 0:1
30 cmdline.txt
=> fatload virtio 0:1 ${loadaddr} cmdline.txt
qemu-system-aarch64: virtio: zero sized buffers are not allowed
---------------------------------

With the patch I get the expected results.
---------------------------------
=> fatls virtio 0:1
30 cmdline.txt
=> fatload virtio 0:1 ${loadaddr} cmdline.txt
30 bytes read in 11 ms (2 KiB/s)
=> md.b ${loadaddr} 0x1E
40080000: 64 77 63 5f 6f 74 67 2e 6c 70 6d 5f 65 6e 61 62 dwc_otg.lpm_enab
40080010: 6c 65 3d 30 20 72 6f 6f 74 77 61 69 74 0a le=0 rootwait.

---------------------------------

---
 fs/fat/fat.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
fs/fat/fat.c +0 -0
index 7fd29470c1..8233a74620 100644
@@ -278,7 +278,11 @@ get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
 		}
 	} else {

fs/fat/fat.c get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size) · line 288

Quoted 10 lines
fs/fat/fat.c +5 -1
 		idx = size / mydata->sect_size;
-		ret = disk_read(startsect, idx, buffer);
+		if (idx == 0) {
+		  ret = 0;
+		} else {
+		  ret = disk_read(startsect, idx, buffer);
+		}
 		if (ret != idx) {
 			debug("Error reading data (got %d)\n", ret);
 			return -1;

Spacing is still off and still extra braces. Code looks fine tho, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20200619/78604ff6/attachment.sig>