From 8a534253bc52f453db21af02299efacd12f40fda Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= <thomas@t-8ch.de>
Date: Sat, 30 Sep 2023 23:59:44 +0200
Subject: [PATCH] libblkid: (probe) read data in chunks
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>
---
 libblkid/src/blkidP.h |  1 +
 libblkid/src/probe.c  | 39 ++++++++++++++++++++++++++++++++++-----
 2 files changed, 35 insertions(+), 5 deletions(-)

diff --git a/libblkid/src/blkidP.h b/libblkid/src/blkidP.h
index 5fbab82..af949c0 100644
--- a/libblkid/src/blkidP.h
+++ b/libblkid/src/blkidP.h
@@ -201,6 +201,7 @@ struct blkid_struct_probe
 	int			fd;		/* device file descriptor */
 	uint64_t		off;		/* begin of data on the device */
 	uint64_t		size;		/* end of data on the device */
+	uint64_t		io_size;	/* optimal size of IO */
 
 	dev_t			devno;		/* device number (st.st_rdev) */
 	dev_t			disk_devno;	/* devno of the whole-disk or 0 */
diff --git a/libblkid/src/probe.c b/libblkid/src/probe.c
index 2e36274..fee5b55 100644
--- a/libblkid/src/probe.c
+++ b/libblkid/src/probe.c
@@ -173,6 +173,7 @@ blkid_probe blkid_clone_probe(blkid_probe parent)
 	pr->fd = parent->fd;
 	pr->off = parent->off;
 	pr->size = parent->size;
+	pr->io_size = parent->io_size;
 	pr->devno = parent->devno;
 	pr->disk_devno = parent->disk_devno;
 	pr->blkssz = parent->blkssz;
@@ -647,13 +648,21 @@ static int hide_buffer(blkid_probe pr, uint64_t off, uint64_t len)
 unsigned char *blkid_probe_get_buffer(blkid_probe pr, uint64_t off, uint64_t len)
 {
 	struct blkid_bufinfo *bf = NULL;
-	uint64_t real_off = pr->off + off;
+	uint64_t real_off, bias;
+
+	bias = off % pr->io_size;
+	off -= bias;
+	len += bias;
+	if (len % pr->io_size)
+		len += pr->io_size - (len % pr->io_size);
+
+	real_off = pr->off + off;
 
 	/*
 	DBG(BUFFER, ul_debug("\t>>>> off=%ju, real-off=%ju (probe <%ju..%ju>, len=%ju",
 				off, real_off, pr->off, pr->off + pr->size, len));
 	*/
-	if (pr->size == 0) {
+	if (pr->size == 0 || pr->io_size == 0) {
 		errno = EINVAL;
 		return NULL;
 	}
@@ -700,7 +709,7 @@ unsigned char *blkid_probe_get_buffer(blkid_probe pr, uint64_t off, uint64_t len
 	assert(bf->off + bf->len >= real_off + len);
 
 	errno = 0;
-	return real_off ? bf->data + (real_off - bf->off) : bf->data;
+	return real_off ? bf->data + (real_off - bf->off + bias) : bf->data + bias;
 }
 
 /**
@@ -861,6 +870,22 @@ failed:
 
 #endif
 
+static uint64_t blkid_get_io_size(int fd)
+{
+	static const int ioctls[] = { BLKIOOPT, BLKIOMIN, BLKBSZGET };
+	unsigned int s;
+	size_t i;
+	int r;
+
+	for (i = 0; i < ARRAY_SIZE(ioctls); i++) {
+		r = ioctl(fd, ioctls[i], &s);
+		if (r == 0 && is_power_of_2(s) && s >= DEFAULT_SECTOR_SIZE)
+			return min(s, 1U << 16);
+	}
+
+	return DEFAULT_SECTOR_SIZE;
+}
+
 /**
  * blkid_probe_set_device:
  * @pr: probe
@@ -906,6 +931,7 @@ int blkid_probe_set_device(blkid_probe pr, int fd,
 	pr->fd = fd;
 	pr->off = (uint64_t) off;
 	pr->size = 0;
+	pr->io_size = DEFAULT_SECTOR_SIZE;
 	pr->devno = 0;
 	pr->disk_devno = 0;
 	pr->mode = 0;
@@ -1012,8 +1038,11 @@ int blkid_probe_set_device(blkid_probe pr, int fd,
 #endif
 	free(dm_uuid);
 
-	DBG(LOWPROBE, ul_debug("ready for low-probing, offset=%"PRIu64", size=%"PRIu64"",
-				pr->off, pr->size));
+	if (S_ISBLK(sb.st_mode) && !blkid_probe_is_tiny(pr))
+		pr->io_size = blkid_get_io_size(fd);
+
+	DBG(LOWPROBE, ul_debug("ready for low-probing, offset=%"PRIu64", size=%"PRIu64", iosize=%"PRIu64,
+				pr->off, pr->size, pr->io_size));
 	DBG(LOWPROBE, ul_debug("whole-disk: %s, regfile: %s",
 		blkid_probe_is_wholedisk(pr) ?"YES" : "NO",
 		S_ISREG(pr->mode) ? "YES" : "NO"));
-- 
2.33.0

