diff mbox series

[v2,1/4] util/mmap-alloc: qemu_fd_getfs()

Message ID 20230419161739.1129988-2-peterx@redhat.com (mailing list archive)
State New, archived
Headers show
Series migration/hostmem: Allow to fail early for postcopy on specific fs type | expand

Commit Message

Peter Xu April 19, 2023, 4:17 p.m. UTC
This new helper fetches file system type for a fd.  Only Linux is
implemented so far.  Currently only tmpfs and hugetlbfs is defined, but it
can grow per need.

Signed-off-by: Peter Xu <peterx@redhat.com>
---
 include/qemu/mmap-alloc.h |  7 +++++++
 util/mmap-alloc.c         | 28 ++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+)

Comments

David Hildenbrand April 19, 2023, 4:31 p.m. UTC | #1
On 19.04.23 18:17, Peter Xu wrote:
> This new helper fetches file system type for a fd.  Only Linux is
> implemented so far.  Currently only tmpfs and hugetlbfs is defined, but it
> can grow per need.
> 
> Signed-off-by: Peter Xu <peterx@redhat.com>
> ---
>   include/qemu/mmap-alloc.h |  7 +++++++
>   util/mmap-alloc.c         | 28 ++++++++++++++++++++++++++++
>   2 files changed, 35 insertions(+)
> 
> diff --git a/include/qemu/mmap-alloc.h b/include/qemu/mmap-alloc.h
> index 2825e231a7..8344daaa03 100644
> --- a/include/qemu/mmap-alloc.h
> +++ b/include/qemu/mmap-alloc.h
> @@ -1,8 +1,15 @@
>   #ifndef QEMU_MMAP_ALLOC_H
>   #define QEMU_MMAP_ALLOC_H
>   
> +typedef enum {
> +    QEMU_FS_TYPE_UNKNOWN = 0,
> +    QEMU_FS_TYPE_TMPFS,
> +    QEMU_FS_TYPE_HUGETLBFS,
> +    QEMU_FS_TYPE_NUM,
> +} QemuFsType;
>   
>   size_t qemu_fd_getpagesize(int fd);
> +QemuFsType qemu_fd_getfs(int fd);
>   
>   /**
>    * qemu_ram_mmap: mmap anonymous memory, the specified file or device.
> diff --git a/util/mmap-alloc.c b/util/mmap-alloc.c
> index 5ed7d29183..ed14f9c64d 100644
> --- a/util/mmap-alloc.c
> +++ b/util/mmap-alloc.c
> @@ -27,8 +27,36 @@
>   
>   #ifdef CONFIG_LINUX
>   #include <sys/vfs.h>
> +#include <linux/magic.h>
>   #endif
>   
> +QemuFsType qemu_fd_getfs(int fd)
> +{
> +#ifdef CONFIG_LINUX
> +    struct statfs fs;
> +    int ret;
> +
> +    if (fd < 0) {
> +        return QEMU_FS_TYPE_UNKNOWN;
> +    }
> +
> +    do {
> +        ret = fstatfs(fd, &fs);
> +    } while (ret != 0 && errno == EINTR);
> +
> +    switch (fs.f_type) {
> +    case TMPFS_MAGIC:
> +        return QEMU_FS_TYPE_TMPFS;
> +    case HUGETLBFS_MAGIC:
> +        return QEMU_FS_TYPE_HUGETLBFS;
> +    default:
> +        return QEMU_FS_TYPE_UNKNOWN;
> +    }
> +#else
> +    return QEMU_FS_TYPE_UNKNOWN;
> +#endif
> +}

I guess you could do

#ifdef CONFIG_LINUX
...
     default:
         break
     }
#endif
     return QEMU_FS_TYPE_UNKNOWN;

To avoid duplicating the default handling. Whatever you prefer:


Reviewed-by: David Hildenbrand <david@redhat.com>
Juan Quintela April 19, 2023, 7:34 p.m. UTC | #2
Peter Xu <peterx@redhat.com> wrote:
> This new helper fetches file system type for a fd.  Only Linux is
> implemented so far.  Currently only tmpfs and hugetlbfs is defined, but it

s/is/are/

> can grow per need.
>
> Signed-off-by: Peter Xu <peterx@redhat.com>

Reviewed-by: Juan Quintela <quintela@redhat.com>

...

> +#include <linux/magic.h>

...

> +    case TMPFS_MAGIC:

...

> +    case HUGETLBFS_MAGIC:

Everybody loves magic, right? O:-)
diff mbox series

Patch

diff --git a/include/qemu/mmap-alloc.h b/include/qemu/mmap-alloc.h
index 2825e231a7..8344daaa03 100644
--- a/include/qemu/mmap-alloc.h
+++ b/include/qemu/mmap-alloc.h
@@ -1,8 +1,15 @@ 
 #ifndef QEMU_MMAP_ALLOC_H
 #define QEMU_MMAP_ALLOC_H
 
+typedef enum {
+    QEMU_FS_TYPE_UNKNOWN = 0,
+    QEMU_FS_TYPE_TMPFS,
+    QEMU_FS_TYPE_HUGETLBFS,
+    QEMU_FS_TYPE_NUM,
+} QemuFsType;
 
 size_t qemu_fd_getpagesize(int fd);
+QemuFsType qemu_fd_getfs(int fd);
 
 /**
  * qemu_ram_mmap: mmap anonymous memory, the specified file or device.
diff --git a/util/mmap-alloc.c b/util/mmap-alloc.c
index 5ed7d29183..ed14f9c64d 100644
--- a/util/mmap-alloc.c
+++ b/util/mmap-alloc.c
@@ -27,8 +27,36 @@ 
 
 #ifdef CONFIG_LINUX
 #include <sys/vfs.h>
+#include <linux/magic.h>
 #endif
 
+QemuFsType qemu_fd_getfs(int fd)
+{
+#ifdef CONFIG_LINUX
+    struct statfs fs;
+    int ret;
+
+    if (fd < 0) {
+        return QEMU_FS_TYPE_UNKNOWN;
+    }
+
+    do {
+        ret = fstatfs(fd, &fs);
+    } while (ret != 0 && errno == EINTR);
+
+    switch (fs.f_type) {
+    case TMPFS_MAGIC:
+        return QEMU_FS_TYPE_TMPFS;
+    case HUGETLBFS_MAGIC:
+        return QEMU_FS_TYPE_HUGETLBFS;
+    default:
+        return QEMU_FS_TYPE_UNKNOWN;
+    }
+#else
+    return QEMU_FS_TYPE_UNKNOWN;
+#endif
+}
+
 size_t qemu_fd_getpagesize(int fd)
 {
 #ifdef CONFIG_LINUX