[PATCH 1/2] mkfs: Break block discard into chunks of 2 GB

Pavel Reichl
2019-11-21T21:44:57Z

Signed-off-by: Pavel Reichl <preichl@redhat.com>
---
mkfs/xfs_mkfs.c | 32 +++++++++++++++++++++++++-------
1 file changed, 25 insertions(+), 7 deletions(-)

diff --git a/mkfs/xfs_mkfs.c b/mkfs/xfs_mkfs.c
index 18338a61..a02d6f66 100644
--- a/mkfs/xfs_mkfs.c
+++ b/mkfs/xfs_mkfs.c
@@ -1242,15 +1242,33 @@ done:
static void
discard_blocks(dev_t dev, uint64_t nsectors)
{
- int fd;
+ int fd;
+ uint64_t offset = 0;
+ /* Maximal chunk of bytes to discard is 2GB */
+ const uint64_t step = (uint64_t)2<<30;
+ /* Sector size is 512 bytes */
+ const uint64_t count = nsectors << 9;

- /*
- * We intentionally ignore errors from the discard ioctl. It is
- * not necessary for the mkfs functionality but just an optimization.
- */
fd = libxfs_device_to_fd(dev);
- if (fd > 0)
- platform_discard_blocks(fd, 0, nsectors << 9);
+ if (fd <= 0)
+ return;
+
+ while (offset < count) {
+ uint64_t tmp_step = step;
+
+ if ((offset + step) > count)
+ tmp_step = count - offset;
+
+ /*
+ * We intentionally ignore errors from the discard ioctl. It is
+ * not necessary for the mkfs functionality but just an
+ * optimization. However we should stop on error.
+ */
+ if (platform_discard_blocks(fd, offset, tmp_step))
+ return;
+
+ offset += tmp_step;
+ }
}

static __attribute__((noreturn)) void
--
2.23.0