Blame 0002-linux-user-Add-support-for-a-group-of-btrfs-ioctls-u.patch

Daniel P. Berrangé dc03f3
From e0eab99255d34c9dbaac7b73a1a2a79f823a7da9 Mon Sep 17 00:00:00 2001
Daniel P. Berrangé dc03f3
From: Filip Bozuta <Filip.Bozuta@syrmia.com>
Daniel P. Berrangé dc03f3
Date: Thu, 3 Sep 2020 01:26:54 +0200
Daniel P. Berrangé dc03f3
Subject: [PATCH 2/9] linux-user: Add support for a group of btrfs ioctls used
Daniel P. Berrangé dc03f3
 for subvolumes
Daniel P. Berrangé dc03f3
MIME-Version: 1.0
Daniel P. Berrangé dc03f3
Content-Type: text/plain; charset=UTF-8
Daniel P. Berrangé dc03f3
Content-Transfer-Encoding: 8bit
Daniel P. Berrangé dc03f3
Daniel P. Berrangé dc03f3
This patch implements functionality of following ioctls:
Daniel P. Berrangé dc03f3
Daniel P. Berrangé dc03f3
BTRFS_IOC_SUBVOL_CREATE - Creating a btrfs subvolume
Daniel P. Berrangé dc03f3
Daniel P. Berrangé dc03f3
    Create a btrfs subvolume. The subvolume is created using the ioctl's
Daniel P. Berrangé dc03f3
    third argument which represents a pointer to a following structure
Daniel P. Berrangé dc03f3
    type:
Daniel P. Berrangé dc03f3
Daniel P. Berrangé dc03f3
    struct btrfs_ioctl_vol_args {
Daniel P. Berrangé dc03f3
	__s64 fd;
Daniel P. Berrangé dc03f3
	char name[BTRFS_PATH_NAME_MAX + 1];
Daniel P. Berrangé dc03f3
    };
Daniel P. Berrangé dc03f3
Daniel P. Berrangé dc03f3
    Before calling this ioctl, the fields of this structure should be filled
Daniel P. Berrangé dc03f3
    with aproppriate values. The fd field represents the file descriptor
Daniel P. Berrangé dc03f3
    value of the subvolume and the name field represents the subvolume
Daniel P. Berrangé dc03f3
    path.
Daniel P. Berrangé dc03f3
Daniel P. Berrangé dc03f3
BTRFS_IOC_SUBVOL_GETFLAGS - Getting subvolume flags
Daniel P. Berrangé dc03f3
Daniel P. Berrangé dc03f3
    Read the flags of the btrfs subvolume. The flags are read using
Daniel P. Berrangé dc03f3
    the ioctl's third argument that is a pointer of __u64 (unsigned long).
Daniel P. Berrangé dc03f3
    The third argument represents a bit mask that can be composed of following
Daniel P. Berrangé dc03f3
    values:
Daniel P. Berrangé dc03f3
    BTRFS_SUBVOL_RDONLY           (1ULL << 1)
Daniel P. Berrangé dc03f3
    BTRFS_SUBVOL_QGROUP_INHERIT   (1ULL << 2)
Daniel P. Berrangé dc03f3
    BTRFS_DEVICE_SPEC_BY_ID       (1ULL << 3)
Daniel P. Berrangé dc03f3
    BTRFS_SUBVOL_SPEC_BY_ID       (1ULL << 4)
Daniel P. Berrangé dc03f3
Daniel P. Berrangé dc03f3
BTRFS_IOC_SUBVOL_SETFLAGS - Setting subvolume flags
Daniel P. Berrangé dc03f3
Daniel P. Berrangé dc03f3
    Set the flags of the btrfs subvolume. The flags are set using the
Daniel P. Berrangé dc03f3
    ioctl's third argument that is a pointer of __u64 (unsigned long).
Daniel P. Berrangé dc03f3
    The third argument represents a bit mask that can be composed of same
Daniel P. Berrangé dc03f3
    values as in the case of previous ioctl (BTRFS_IOC_SUBVOL_GETFLAGS).
Daniel P. Berrangé dc03f3
Daniel P. Berrangé dc03f3
BTRFS_IOC_SUBVOL_GETINFO - Getting subvolume information
Daniel P. Berrangé dc03f3
Daniel P. Berrangé dc03f3
    Read information about the subvolume. The subvolume information is
Daniel P. Berrangé dc03f3
    returned in the ioctl's third argument which represents a pointer to
Daniel P. Berrangé dc03f3
    a following structure type:
Daniel P. Berrangé dc03f3
Daniel P. Berrangé dc03f3
    struct btrfs_ioctl_get_subvol_info_args {
Daniel P. Berrangé dc03f3
	/* Id of this subvolume */
Daniel P. Berrangé dc03f3
	__u64 treeid;
Daniel P. Berrangé dc03f3
Daniel P. Berrangé dc03f3
	/* Name of this subvolume, used to get the real name at mount point */
Daniel P. Berrangé dc03f3
	char name[BTRFS_VOL_NAME_MAX + 1];
Daniel P. Berrangé dc03f3
Daniel P. Berrangé dc03f3
	/*
Daniel P. Berrangé dc03f3
	 * Id of the subvolume which contains this subvolume.
Daniel P. Berrangé dc03f3
	 * Zero for top-level subvolume or a deleted subvolume.
Daniel P. Berrangé dc03f3
	 */
Daniel P. Berrangé dc03f3
	__u64 parent_id;
Daniel P. Berrangé dc03f3
Daniel P. Berrangé dc03f3
	/*
Daniel P. Berrangé dc03f3
	 * Inode number of the directory which contains this subvolume.
Daniel P. Berrangé dc03f3
	 * Zero for top-level subvolume or a deleted subvolume
Daniel P. Berrangé dc03f3
	 */
Daniel P. Berrangé dc03f3
	__u64 dirid;
Daniel P. Berrangé dc03f3
Daniel P. Berrangé dc03f3
	/* Latest transaction id of this subvolume */
Daniel P. Berrangé dc03f3
	__u64 generation;
Daniel P. Berrangé dc03f3
Daniel P. Berrangé dc03f3
	/* Flags of this subvolume */
Daniel P. Berrangé dc03f3
	__u64 flags;
Daniel P. Berrangé dc03f3
Daniel P. Berrangé dc03f3
	/* UUID of this subvolume */
Daniel P. Berrangé dc03f3
	__u8 uuid[BTRFS_UUID_SIZE];
Daniel P. Berrangé dc03f3
Daniel P. Berrangé dc03f3
	/*
Daniel P. Berrangé dc03f3
	 * UUID of the subvolume of which this subvolume is a snapshot.
Daniel P. Berrangé dc03f3
	 * All zero for a non-snapshot subvolume.
Daniel P. Berrangé dc03f3
	 */
Daniel P. Berrangé dc03f3
	__u8 parent_uuid[BTRFS_UUID_SIZE];
Daniel P. Berrangé dc03f3
Daniel P. Berrangé dc03f3
	/*
Daniel P. Berrangé dc03f3
	 * UUID of the subvolume from which this subvolume was received.
Daniel P. Berrangé dc03f3
	 * All zero for non-received subvolume.
Daniel P. Berrangé dc03f3
	 */
Daniel P. Berrangé dc03f3
	__u8 received_uuid[BTRFS_UUID_SIZE];
Daniel P. Berrangé dc03f3
Daniel P. Berrangé dc03f3
	/* Transaction id indicating when change/create/send/receive happened */
Daniel P. Berrangé dc03f3
	__u64 ctransid;
Daniel P. Berrangé dc03f3
	__u64 otransid;
Daniel P. Berrangé dc03f3
	__u64 stransid;
Daniel P. Berrangé dc03f3
	__u64 rtransid;
Daniel P. Berrangé dc03f3
	/* Time corresponding to c/o/s/rtransid */
Daniel P. Berrangé dc03f3
	struct btrfs_ioctl_timespec ctime;
Daniel P. Berrangé dc03f3
	struct btrfs_ioctl_timespec otime;
Daniel P. Berrangé dc03f3
	struct btrfs_ioctl_timespec stime;
Daniel P. Berrangé dc03f3
	struct btrfs_ioctl_timespec rtime;
Daniel P. Berrangé dc03f3
Daniel P. Berrangé dc03f3
	/* Must be zero */
Daniel P. Berrangé dc03f3
	__u64 reserved[8];
Daniel P. Berrangé dc03f3
     };
Daniel P. Berrangé dc03f3
Daniel P. Berrangé dc03f3
     All of the fields of this structure are filled after the ioctl call.
Daniel P. Berrangé dc03f3
Daniel P. Berrangé dc03f3
Implementation notes:
Daniel P. Berrangé dc03f3
Daniel P. Berrangé dc03f3
    Ioctls BTRFS_IOC_SUBVOL_CREATE and BTRFS_IOC_SUBVOL_GETINFO have structure
Daniel P. Berrangé dc03f3
    types as third arguments. That is the reason why a corresponding definition
Daniel P. Berrangé dc03f3
    are added in file 'linux-user/syscall_types.h'.
Daniel P. Berrangé dc03f3
Daniel P. Berrangé dc03f3
    The line '#include <linux/btrfs.h>' is added in file 'linux-user/syscall.c' to
Daniel P. Berrangé dc03f3
    recognise preprocessor definitions for these ioctls. Since the file "linux/btrfs.h"
Daniel P. Berrangé dc03f3
    was added in the kernel version 3.9, it is enwrapped in an #ifdef statement
Daniel P. Berrangé dc03f3
    with parameter CONFIG_BTRFS which is defined in 'configure' if the
Daniel P. Berrangé dc03f3
    header file is present.
Daniel P. Berrangé dc03f3
Daniel P. Berrangé dc03f3
Signed-off-by: Filip Bozuta <Filip.Bozuta@syrmia.com>
Daniel P. Berrangé dc03f3
Tested-by: Daniel P. Berrangé <berrange@redhat.com>
Daniel P. Berrangé dc03f3
Message-Id: <20200823195014.116226-2-Filip.Bozuta@syrmia.com>
Daniel P. Berrangé dc03f3
Signed-off-by: Laurent Vivier <laurent@vivier.eu>
Daniel P. Berrangé dc03f3
---
Daniel P. Berrangé dc03f3
 configure                  |  9 +++++++++
Daniel P. Berrangé dc03f3
 linux-user/ioctls.h        | 15 +++++++++++++++
Daniel P. Berrangé dc03f3
 linux-user/syscall.c       |  3 +++
Daniel P. Berrangé dc03f3
 linux-user/syscall_defs.h  |  8 ++++++++
Daniel P. Berrangé dc03f3
 linux-user/syscall_types.h | 32 ++++++++++++++++++++++++++++++++
Daniel P. Berrangé dc03f3
 5 files changed, 67 insertions(+)
Daniel P. Berrangé dc03f3
Daniel P. Berrangé dc03f3
diff --git a/configure b/configure
Daniel P. Berrangé dc03f3
index 2acc4d1465..1cba4e0b80 100755
Daniel P. Berrangé dc03f3
--- a/configure
Daniel P. Berrangé dc03f3
+++ b/configure
Daniel P. Berrangé dc03f3
@@ -5079,6 +5079,12 @@ if check_include sys/kcov.h ; then
Daniel P. Berrangé dc03f3
     kcov=yes
Daniel P. Berrangé dc03f3
 fi
Daniel P. Berrangé dc03f3
 
Daniel P. Berrangé dc03f3
+# check for btrfs filesystem support (kernel must be 3.9+)
Daniel P. Berrangé dc03f3
+btrfs=no
Daniel P. Berrangé dc03f3
+if check_include linux/btrfs.h ; then
Daniel P. Berrangé dc03f3
+    btrfs=yes
Daniel P. Berrangé dc03f3
+fi
Daniel P. Berrangé dc03f3
+
Daniel P. Berrangé dc03f3
 # If we're making warnings fatal, apply this to Sphinx runs as well
Daniel P. Berrangé dc03f3
 sphinx_werror=""
Daniel P. Berrangé dc03f3
 if test "$werror" = "yes"; then
Daniel P. Berrangé dc03f3
@@ -7330,6 +7336,9 @@ fi
Daniel P. Berrangé dc03f3
 if test "$kcov" = "yes" ; then
Daniel P. Berrangé dc03f3
   echo "CONFIG_KCOV=y" >> $config_host_mak
Daniel P. Berrangé dc03f3
 fi
Daniel P. Berrangé dc03f3
+if test "$btrfs" = "yes" ; then
Daniel P. Berrangé dc03f3
+  echo "CONFIG_BTRFS=y" >> $config_host_mak
Daniel P. Berrangé dc03f3
+fi
Daniel P. Berrangé dc03f3
 if test "$inotify" = "yes" ; then
Daniel P. Berrangé dc03f3
   echo "CONFIG_INOTIFY=y" >> $config_host_mak
Daniel P. Berrangé dc03f3
 fi
Daniel P. Berrangé dc03f3
diff --git a/linux-user/ioctls.h b/linux-user/ioctls.h
Daniel P. Berrangé dc03f3
index 0713ae1311..12d1444224 100644
Daniel P. Berrangé dc03f3
--- a/linux-user/ioctls.h
Daniel P. Berrangé dc03f3
+++ b/linux-user/ioctls.h
Daniel P. Berrangé dc03f3
@@ -174,6 +174,21 @@
Daniel P. Berrangé dc03f3
      IOCTL(FS_IOC32_GETVERSION, IOC_R, MK_PTR(TYPE_INT))
Daniel P. Berrangé dc03f3
      IOCTL(FS_IOC32_SETVERSION, IOC_W, MK_PTR(TYPE_INT))
Daniel P. Berrangé dc03f3
 
Daniel P. Berrangé dc03f3
+#ifdef BTRFS_IOC_SUBVOL_CREATE
Daniel P. Berrangé dc03f3
+     IOCTL(BTRFS_IOC_SUBVOL_CREATE, IOC_W,
Daniel P. Berrangé dc03f3
+           MK_PTR(MK_STRUCT(STRUCT_btrfs_ioctl_vol_args)))
Daniel P. Berrangé dc03f3
+#endif
Daniel P. Berrangé dc03f3
+#ifdef BTRFS_IOC_SUBVOL_GETFLAGS
Daniel P. Berrangé dc03f3
+     IOCTL(BTRFS_IOC_SUBVOL_GETFLAGS, IOC_R, MK_PTR(TYPE_ULONGLONG))
Daniel P. Berrangé dc03f3
+#endif
Daniel P. Berrangé dc03f3
+#ifdef BTRFS_IOC_SUBVOL_SETFLAGS
Daniel P. Berrangé dc03f3
+     IOCTL(BTRFS_IOC_SUBVOL_SETFLAGS, IOC_W, MK_PTR(TYPE_ULONGLONG))
Daniel P. Berrangé dc03f3
+#endif
Daniel P. Berrangé dc03f3
+#ifdef BTRFS_IOC_GET_SUBVOL_INFO
Daniel P. Berrangé dc03f3
+     IOCTL(BTRFS_IOC_GET_SUBVOL_INFO, IOC_R,
Daniel P. Berrangé dc03f3
+           MK_PTR(MK_STRUCT(STRUCT_btrfs_ioctl_get_subvol_info_args)))
Daniel P. Berrangé dc03f3
+#endif
Daniel P. Berrangé dc03f3
+
Daniel P. Berrangé dc03f3
 #ifdef CONFIG_USBFS
Daniel P. Berrangé dc03f3
   /* USB ioctls */
Daniel P. Berrangé dc03f3
   IOCTL(USBDEVFS_CONTROL, IOC_RW,
Daniel P. Berrangé dc03f3
diff --git a/linux-user/syscall.c b/linux-user/syscall.c
Daniel P. Berrangé dc03f3
index 945fc25279..36777c91b0 100644
Daniel P. Berrangé dc03f3
--- a/linux-user/syscall.c
Daniel P. Berrangé dc03f3
+++ b/linux-user/syscall.c
Daniel P. Berrangé dc03f3
@@ -112,6 +112,9 @@
Daniel P. Berrangé dc03f3
 #include <linux/if_alg.h>
Daniel P. Berrangé dc03f3
 #include <linux/rtc.h>
Daniel P. Berrangé dc03f3
 #include <sound/asound.h>
Daniel P. Berrangé dc03f3
+#ifdef CONFIG_BTRFS
Daniel P. Berrangé dc03f3
+#include <linux/btrfs.h>
Daniel P. Berrangé dc03f3
+#endif
Daniel P. Berrangé dc03f3
 #ifdef HAVE_DRM_H
Daniel P. Berrangé dc03f3
 #include <libdrm/drm.h>
Daniel P. Berrangé dc03f3
 #endif
Daniel P. Berrangé dc03f3
diff --git a/linux-user/syscall_defs.h b/linux-user/syscall_defs.h
Daniel P. Berrangé dc03f3
index 3c261cff0e..2757956dfa 100644
Daniel P. Berrangé dc03f3
--- a/linux-user/syscall_defs.h
Daniel P. Berrangé dc03f3
+++ b/linux-user/syscall_defs.h
Daniel P. Berrangé dc03f3
@@ -967,6 +967,14 @@ struct target_rtc_pll_info {
Daniel P. Berrangé dc03f3
 #define TARGET_FS_IOC32_GETVERSION TARGET_IOR('v', 1, int)
Daniel P. Berrangé dc03f3
 #define TARGET_FS_IOC32_SETVERSION TARGET_IOW('v', 2, int)
Daniel P. Berrangé dc03f3
 
Daniel P. Berrangé dc03f3
+/* btrfs ioctls */
Daniel P. Berrangé dc03f3
+#define TARGET_BTRFS_IOC_SUBVOL_CREATE          TARGET_IOWU(BTRFS_IOCTL_MAGIC, 14)
Daniel P. Berrangé dc03f3
+#define TARGET_BTRFS_IOC_SUBVOL_GETFLAGS        TARGET_IOR(BTRFS_IOCTL_MAGIC, 25,\
Daniel P. Berrangé dc03f3
+                                                           abi_ullong)
Daniel P. Berrangé dc03f3
+#define TARGET_BTRFS_IOC_SUBVOL_SETFLAGS        TARGET_IOW(BTRFS_IOCTL_MAGIC, 26,\
Daniel P. Berrangé dc03f3
+                                                           abi_ullong)
Daniel P. Berrangé dc03f3
+#define TARGET_BTRFS_IOC_GET_SUBVOL_INFO        TARGET_IORU(BTRFS_IOCTL_MAGIC, 60)
Daniel P. Berrangé dc03f3
+
Daniel P. Berrangé dc03f3
 /* usb ioctls */
Daniel P. Berrangé dc03f3
 #define TARGET_USBDEVFS_CONTROL TARGET_IOWRU('U', 0)
Daniel P. Berrangé dc03f3
 #define TARGET_USBDEVFS_BULK TARGET_IOWRU('U', 2)
Daniel P. Berrangé dc03f3
diff --git a/linux-user/syscall_types.h b/linux-user/syscall_types.h
Daniel P. Berrangé dc03f3
index 3f1f033464..db61dbc1b3 100644
Daniel P. Berrangé dc03f3
--- a/linux-user/syscall_types.h
Daniel P. Berrangé dc03f3
+++ b/linux-user/syscall_types.h
Daniel P. Berrangé dc03f3
@@ -354,6 +354,38 @@ STRUCT(blkpg_partition,
Daniel P. Berrangé dc03f3
        MK_ARRAY(TYPE_CHAR, BLKPG_DEVNAMELTH), /* devname */
Daniel P. Berrangé dc03f3
        MK_ARRAY(TYPE_CHAR, BLKPG_VOLNAMELTH)) /* volname */
Daniel P. Berrangé dc03f3
 
Daniel P. Berrangé dc03f3
+#ifdef BTRFS_IOC_SUBVOL_CREATE
Daniel P. Berrangé dc03f3
+STRUCT(btrfs_ioctl_vol_args,
Daniel P. Berrangé dc03f3
+       TYPE_LONGLONG, /* fd */
Daniel P. Berrangé dc03f3
+       MK_ARRAY(TYPE_CHAR, BTRFS_PATH_NAME_MAX + 1)) /* name */
Daniel P. Berrangé dc03f3
+#endif
Daniel P. Berrangé dc03f3
+
Daniel P. Berrangé dc03f3
+#ifdef BTRFS_IOC_GET_SUBVOL_INFO
Daniel P. Berrangé dc03f3
+STRUCT(btrfs_ioctl_timespec,
Daniel P. Berrangé dc03f3
+       TYPE_ULONGLONG, /* sec */
Daniel P. Berrangé dc03f3
+       TYPE_INT) /* nsec */
Daniel P. Berrangé dc03f3
+
Daniel P. Berrangé dc03f3
+STRUCT(btrfs_ioctl_get_subvol_info_args,
Daniel P. Berrangé dc03f3
+       TYPE_ULONGLONG, /* treeid */
Daniel P. Berrangé dc03f3
+       MK_ARRAY(TYPE_CHAR, BTRFS_VOL_NAME_MAX + 1),
Daniel P. Berrangé dc03f3
+       TYPE_ULONGLONG, /* parentid */
Daniel P. Berrangé dc03f3
+       TYPE_ULONGLONG, /* dirid */
Daniel P. Berrangé dc03f3
+       TYPE_ULONGLONG, /* generation */
Daniel P. Berrangé dc03f3
+       TYPE_ULONGLONG, /* flags */
Daniel P. Berrangé dc03f3
+       MK_ARRAY(TYPE_CHAR, BTRFS_UUID_SIZE), /* uuid */
Daniel P. Berrangé dc03f3
+       MK_ARRAY(TYPE_CHAR, BTRFS_UUID_SIZE), /* parent_uuid */
Daniel P. Berrangé dc03f3
+       MK_ARRAY(TYPE_CHAR, BTRFS_UUID_SIZE), /* received_uuid */
Daniel P. Berrangé dc03f3
+       TYPE_ULONGLONG, /* ctransid */
Daniel P. Berrangé dc03f3
+       TYPE_ULONGLONG, /* otransid */
Daniel P. Berrangé dc03f3
+       TYPE_ULONGLONG, /* stransid */
Daniel P. Berrangé dc03f3
+       TYPE_ULONGLONG, /* rtransid */
Daniel P. Berrangé dc03f3
+       MK_STRUCT(STRUCT_btrfs_ioctl_timespec), /* ctime */
Daniel P. Berrangé dc03f3
+       MK_STRUCT(STRUCT_btrfs_ioctl_timespec), /* otime */
Daniel P. Berrangé dc03f3
+       MK_STRUCT(STRUCT_btrfs_ioctl_timespec), /* stime */
Daniel P. Berrangé dc03f3
+       MK_STRUCT(STRUCT_btrfs_ioctl_timespec), /* rtime */
Daniel P. Berrangé dc03f3
+       MK_ARRAY(TYPE_ULONGLONG, 8)) /* reserved */
Daniel P. Berrangé dc03f3
+#endif
Daniel P. Berrangé dc03f3
+
Daniel P. Berrangé dc03f3
 STRUCT(rtc_time,
Daniel P. Berrangé dc03f3
        TYPE_INT, /* tm_sec */
Daniel P. Berrangé dc03f3
        TYPE_INT, /* tm_min */
Daniel P. Berrangé dc03f3
-- 
Daniel P. Berrangé dc03f3
2.26.2
Daniel P. Berrangé dc03f3