mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@barebox.org>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@barebox.org>
Subject: [PATCH 22/26] fs: fat: support larger block device sectors
Date: Fri, 26 Jun 2026 10:42:33 +0200	[thread overview]
Message-ID: <20260626084608.1388806-23-a.fatoum@barebox.org> (raw)
In-Reply-To: <20260626084608.1388806-1-a.fatoum@barebox.org>

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




  parent reply	other threads:[~2026-06-26  8:51 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-26  8:42 [PATCH 00/26] block: add support for non-512 byte sectors Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 01/26] nvme: use barebox-appropriate 64-bit type for timeouts Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 02/26] nvme: fix buffer advancement when chunking due to max_hw_sectors Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 03/26] nvme: allow flush opcode Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 04/26] nvme: honor namespace block size for I/O Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 05/26] bootscan: fix detection of GPT Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 06/26] block: clarify that writebuffer_io_len returns sector counts Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 07/26] block: fix wrong type for discard_start/size byte ranges Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 08/26] block: fix discard zeroing too little memory Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 09/26] block: use logical block size for reparse checks Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 10/26] block: require lower bound of sector size to be 512 bytes Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 11/26] filetype: don't hardcode sector size in file_detect_partition_table Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 12/26] block: define helpers for non-512-byte sector support Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 13/26] bootscan: use block size for partition table probe Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 14/26] ramdisk: validate exported sector size Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 15/26] efi: block: fix sector size mismatch in block device registration and ops Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 16/26] efi: loader: disk: report block device size in Block I/O Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 17/26] efi: loader: file: report cdev block size in file info Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 18/26] partitions: use byte offset for first partition policy Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 19/26] partitions: dos: allocate correctly sized buffer for dos_partition_desc Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 20/26] partition: support non-512 byte sectors Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 21/26] fs: fat: fix garbage read when writing with bigger block size Ahmad Fatoum
2026-06-26  8:42 ` Ahmad Fatoum [this message]
2026-06-26  8:42 ` [PATCH 23/26] usb-storage: preserve READ CAPACITY sector size Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 24/26] fuzz: add 4K-sector partition ramdisk target Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 25/26] commands: parted: prepare use of non-512-byte sectors Ahmad Fatoum
2026-06-26  8:42 ` [PATCH 26/26] commands: parted: exit if block size if not 512 Ahmad Fatoum

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260626084608.1388806-23-a.fatoum@barebox.org \
    --to=a.fatoum@barebox.org \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox