diff mbox series

libswap: add Btrfs noCOW attribute setting for swap files

Message ID 20240120082725.2550695-1-liwang@redhat.com
State Superseded
Headers show
Series libswap: add Btrfs noCOW attribute setting for swap files | expand

Commit Message

Li Wang Jan. 20, 2024, 8:27 a.m. UTC
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?

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

Comments

Petr Vorel Jan. 22, 2024, 7:13 a.m. UTC | #1
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";
Li Wang Jan. 22, 2024, 7:28 a.m. UTC | #2
On Mon, Jan 22, 2024 at 3:13 PM Petr Vorel <pvorel@suse.cz> wrote:

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

Sure, let's format them in a set.

Sorry for the confusion, I was just developing them one by one and sent
directly.



>
> 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";
>
>
diff mbox series

Patch

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";