[PATCH 22/26] fs: fat: support larger block device sectors

Ahmad Fatoum a.fatoum at barebox.org
Fri Jun 26 01:42:33 PDT 2026


The FAT filesystem implementation already has variable sector size support,
but we kept _MAX_SS fixed at the minimum of 512 bytes, which disabled it.

Further, the glue between the filesystem and the cdev layer still hardcoded
512-byte.

Set _MAX_SS as 4096 and fix the glue to be able to use the underlying
block device's block size.

PBL is kept at the existing fixed 512-byte sector size.

Signed-off-by: Ahmad Fatoum <a.fatoum at barebox.org>
---
 fs/fat/diskio.h     |  9 ++++++++
 fs/fat/fat-diskio.c | 10 ++++++++-
 fs/fat/fat.c        | 51 +++++++++++++++++++++++++++++++++------------
 fs/fat/ffconf.h     |  6 +++++-
 4 files changed, 61 insertions(+), 15 deletions(-)

diff --git a/fs/fat/diskio.h b/fs/fat/diskio.h
index 57626d2fbd7b..04a587e3bc47 100644
--- a/fs/fat/diskio.h
+++ b/fs/fat/diskio.h
@@ -14,6 +14,7 @@
 
 #define _USE_IOCTL	1	/* 1: Use disk_ioctl fucntion */
 
+#include "disks.h"
 #include "integer.h"
 
 
@@ -42,6 +43,14 @@ DRESULT disk_write (FATFS *fatfs, const BYTE*, DWORD, BYTE);
 #endif
 DRESULT disk_ioctl (FATFS *fatfs, BYTE, void*);
 
+#if IN_PROPER
+unsigned int disk_sector_size(FATFS *fat);
+#else
+static inline unsigned int disk_sector_size(FATFS *fat)
+{
+	return SECTOR_SIZE;
+}
+#endif
 
 
 /* Disk Status Bits (DSTATUS) */
diff --git a/fs/fat/fat-diskio.c b/fs/fat/fat-diskio.c
index aa16d3bdc44b..6ba18993b870 100644
--- a/fs/fat/fat-diskio.c
+++ b/fs/fat/fat-diskio.c
@@ -23,7 +23,15 @@ DWORD get_fattime(void)
 
 DRESULT disk_ioctl (FATFS *fat, BYTE command, void *buf)
 {
-	return 0;
+	switch (command) {
+	case GET_SECTOR_SIZE:
+		*(WORD *)buf = disk_sector_size(fat);
+		return RES_OK;
+	case CTRL_SYNC:
+		return RES_OK;
+	default:
+		return RES_PARERR;
+	}
 }
 
 WCHAR ff_convert(WCHAR src, UINT dir)
diff --git a/fs/fat/fat.c b/fs/fat/fat.c
index 93d1e08b8456..0402db1d945a 100644
--- a/fs/fat/fat.c
+++ b/fs/fat/fat.c
@@ -25,6 +25,8 @@
 #include <linux/ctype.h>
 #include <xfuncs.h>
 #include <fcntl.h>
+#include <block.h>
+#include <linux/sizes.h>
 #include "ff.h"
 #include "integer.h"
 #include "diskio.h"
@@ -36,33 +38,44 @@ struct fat_priv {
 
 /* ---------------------------------------------------------------*/
 
+unsigned int disk_sector_size(FATFS *fat)
+{
+	struct fat_priv *priv = fat->userdata;
+
+	return cdev_blocksize(priv->cdev);
+}
+
 DRESULT disk_read(FATFS *fat, BYTE *buf, DWORD sector, BYTE count)
 {
 	struct fat_priv *priv = fat->userdata;
+	unsigned int sector_size = disk_sector_size(fat);
+	size_t len = count * sector_size;
 	int ret;
 
 	debug("%s: sector: %ld count: %d\n", __func__, sector, count);
 
-	ret = cdev_read(priv->cdev, buf, count << 9, (loff_t)sector * 512, 0);
-	if (ret != count << 9)
-		return ret;
+	ret = cdev_read(priv->cdev, buf, len, (loff_t)sector * sector_size, 0);
+	if (ret != len)
+		return RES_ERROR;
 
-	return 0;
+	return RES_OK;
 }
 
 DRESULT disk_write(FATFS *fat, const BYTE *buf, DWORD sector, BYTE count)
 {
 	struct fat_priv *priv = fat->userdata;
+	unsigned int sector_size = disk_sector_size(fat);
+	size_t len = count * sector_size;
 	int ret;
 
 	debug("%s: buf: %p sector: %ld count: %d\n",
 			__func__, buf, sector, count);
 
-	ret = cdev_write(priv->cdev, buf, count << 9, (loff_t)sector * 512, 0);
-	if (ret != count << 9)
-		return ret;
+	ret = cdev_write(priv->cdev, buf, len, (loff_t)sector * sector_size, 0);
+	if (ret != len)
+		return RES_ERROR;
 
-	return 0;
+	return RES_OK;
 }
 
 /* ---------------------------------------------------------------*/
@@ -342,14 +355,27 @@ static int fat_stat(struct device *dev, const char *filename, struct stat *s)
 static int fat_probe(struct device *dev)
 {
 	struct fs_device *fsdev = dev_to_fs_device(dev);
-	struct fat_priv *priv = xzalloc(sizeof(struct fat_priv));
+	struct fat_priv *priv;
+	unsigned int blocksize;
 	int ret;
 
-	dev->priv = priv;
-
 	ret = fsdev_open_cdev(fsdev);
 	if (ret)
-		goto err_open;
+		return ret;
+
+	/*
+	 * No need to cleanup fsdev_open_cdev(), only way to invoke this
+	 * probe function is via mount() and that will already take care
+	 * of calling fs_remove for us.
+	 */
+	blocksize = cdev_blocksize(fsdev->cdev);
+	if (blocksize > SZ_4K) {
+		dev_err(dev, "FAT on %u-byte block devices is unsupported\n",
+			blocksize);
+		return -ENOSYS;
+	}
+
+	priv = dev->priv = xzalloc(sizeof(struct fat_priv));
 
 	priv->cdev = fsdev->cdev;
 	fsdev->sb.s_casefold = true;
@@ -362,7 +388,6 @@ static int fat_probe(struct device *dev)
 	return 0;
 
 err_mount:
-err_open:
 	free(priv);
 
 	return ret;
diff --git a/fs/fat/ffconf.h b/fs/fat/ffconf.h
index abf7d1e92e0b..b7dbf39efbf7 100644
--- a/fs/fat/ffconf.h
+++ b/fs/fat/ffconf.h
@@ -103,7 +103,11 @@
 /* Number of volumes (logical drives) to be used. */
 
 
-#define	_MAX_SS		512		/* 512, 1024, 2048 or 4096 */
+#if IN_PROPER
+#define	_MAX_SS		4096		/* 512, 1024, 2048 or 4096 */
+#else
+#define	_MAX_SS		512
+#endif
 /* Maximum sector size to be handled.
 /  Always set 512 for memory card and hard disk but a larger value may be
 /  required for on-board flash memory, floppy disk and optical disk.
-- 
2.47.3




More information about the barebox mailing list