util/mmap-alloc: qemu_fd_getfs()

This new helper fetches file system type for a fd.  Only Linux is
implemented so far.  Currently only tmpfs and hugetlbfs are defined,
but it can grow as needed.

Signed-off-by: Peter Xu <peterx@redhat.com>
Reviewed-by: David Hildenbrand <david@redhat.com>
Reviewed-by: Juan Quintela <quintela@redhat.com>
Signed-off-by: Juan Quintela <quintela@redhat.com>
This commit is contained in:
Peter Xu 2023-04-19 12:17:36 -04:00 committed by Juan Quintela
parent 403d18ae38
commit fa45f8dab9
2 changed files with 35 additions and 0 deletions

View File

@ -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.

View File

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