[LTP] [PATCH] libswap: add Btrfs noCOW attribute setting for swap files

Petr Vorel pvorel@suse.cz
Mon Jan 22 08:13:37 CET 2024


Hi Li,

> The patch aims to ensure swap files on Btrfs filesystems are created
> with the appropriate FS_NOCOW_FL attribute, which is necessary to
> disable CoW (Copy-on-Write) for swap files, perthe btrfs(5) manual page.
> This change is gated behind a kernel version check to ensure compatibility
> with the system's capabilities.

> Signed-off-by: Li Wang <liwang@redhat.com>
> ---

> Notes:
>     Hi Petr,

>       I haven't gotten a chance to test this patch on any Btrfs platform,
>       but only compile successfully without error on my RHEL8/9(xfs).
>       Can you help test and guarantee it works for you?

I'm not able to apply this to the current master. I also got a bit confused by
number of patches for libswap / swapon01. Could you please resend all the
patches again (in a series)? Or push branch to your fork and point the location?
(whatever is easier to you)

Kind regards,
Petr

>  libs/libltpswap/libswap.c | 35 +++++++++++++++++++++++++++++++++++
>  1 file changed, 35 insertions(+)

> diff --git a/libs/libltpswap/libswap.c b/libs/libltpswap/libswap.c
> index 5f9622aca..8b180f288 100644
> --- a/libs/libltpswap/libswap.c
> +++ b/libs/libltpswap/libswap.c
> @@ -4,6 +4,7 @@
>   * Author: Stanislav Kholmanskikh <stanislav.kholmanskikh@oracle.com>
>   */

> +#include <linux/fs.h>
>  #include <errno.h>

>  #define TST_NO_DEFAULT_MAIN
> @@ -23,11 +24,37 @@ static const char *const swap_supported_fs[] = {
>  	NULL
>  };

> +static void set_nocow_attr(const char *filename)
> +{
> +	int fd;
> +	int attrs;
> +
> +	fd = SAFE_OPEN(filename, O_RDONLY);
> +
> +	if (ioctl(fd, FS_IOC_GETFLAGS, &attrs) == -1) {
> +		tst_res(TFAIL | TERRNO, "Error getting attributes");
> +		close(fd);
> +		return;
> +	}
> +
> +	attrs |= FS_NOCOW_FL;
> +
> +	if (ioctl(fd, FS_IOC_SETFLAGS, &attrs) == -1)
> +		tst_res(TFAIL | TERRNO, "Error setting FS_NOCOW_FL attribute");
> +	else
> +		tst_res(TINFO, "FS_NOCOW_FL attribute set on %s\n", filename);
> +
> +	close(fd);
> +}
> +
>  /*
>   * Make a swap file
>   */
>  int make_swapfile(const char *swapfile, int safe)
>  {
> +	long fs_type = tst_fs_type(swapfile);
> +	const char *fstype = tst_fs_type_name(fs_type);
> +
>  	if (!tst_fs_has_free(".", sysconf(_SC_PAGESIZE) * 10, TST_BYTES))
>  		tst_brk(TBROK, "Insufficient disk space to create swap file");

> @@ -35,6 +62,14 @@ int make_swapfile(const char *swapfile, int safe)
>  	if (tst_fill_file(swapfile, 0, sysconf(_SC_PAGESIZE), 10) != 0)
>  		tst_brk(TBROK, "Failed to create swapfile");

> +	/* Btrfs file need set 'nocow' attribute */
> +	if (strcmp(fstype, "btrfs") == 0) {
> +		if (tst_kvercmp(5, 0, 0) > 0)
> +			set_nocow_attr(swapfile);
> +		else
> +			tst_brk(TCONF, "Swapfile on %s not implemented", fstype);
> +	}
> +
>  	/* make the file swapfile */
>  	const char *argv[2 + 1];
>  	argv[0] = "mkswap";


More information about the ltp mailing list