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

Darrick J. Wong
2019-11-21T21:57:07Z

On Thu, Nov 21, 2019 at 10:44:44PM +0100, Pavel Reichl wrote:
> 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;

You don't need the tabs after the variable name, e.g.

/* Maximal chunk of bytes to discard is 2GB */
const uint64_t step = 2ULL << 30;

> + /* Sector size is 512 bytes */
> + const uint64_t count = nsectors << 9;

count = BBTOB(nsectors)?

>
> - /*
> - * 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;

tmp_step = min(step, count - offset); ?

Otherwise seems reasonable to me, if nothing else to avoid the problem
where you ask mkfs to discard and can't cancel it....

--D

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