Reviewed-by: Konstantin Kostiuk <kkost...@redhat.com>

On Tue, Nov 15, 2022 at 8:46 PM Alexander Ivanov <
alexander.iva...@virtuozzo.com> wrote:

> In the next patch ZFS TRIM support for FreeBSD will be added. Move
> Linux-specific TRIM code to commands-linux.c file.
>
> Signed-off-by: Alexander Ivanov <alexander.iva...@virtuozzo.com>
> ---
>  qga/commands-linux.c | 73 ++++++++++++++++++++++++++++++++++++++++++++
>  qga/commands-posix.c | 72 -------------------------------------------
>  2 files changed, 73 insertions(+), 72 deletions(-)
>
> diff --git a/qga/commands-linux.c b/qga/commands-linux.c
> index 214e408fcd..fb01114153 100644
> --- a/qga/commands-linux.c
> +++ b/qga/commands-linux.c
> @@ -13,6 +13,7 @@
>
>  #include "qemu/osdep.h"
>  #include "qapi/error.h"
> +#include "qga-qapi-commands.h"
>  #include "commands-common.h"
>  #include "cutils.h"
>  #include <mntent.h>
> @@ -284,3 +285,75 @@ int qmp_guest_fsfreeze_do_thaw(Error **errp)
>      return i;
>  }
>  #endif /* CONFIG_FSFREEZE */
> +
> +#if defined(CONFIG_FSTRIM)
> +/*
> + * Walk list of mounted file systems in the guest, and trim them.
> + */
> +GuestFilesystemTrimResponse *
> +qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **errp)
> +{
> +    GuestFilesystemTrimResponse *response;
> +    GuestFilesystemTrimResult *result;
> +    int ret = 0;
> +    FsMountList mounts;
> +    struct FsMount *mount;
> +    int fd;
> +    struct fstrim_range r;
> +
> +    slog("guest-fstrim called");
> +
> +    QTAILQ_INIT(&mounts);
> +    if (!build_fs_mount_list(&mounts, errp)) {
> +        return NULL;
> +    }
> +
> +    response = g_malloc0(sizeof(*response));
> +
> +    QTAILQ_FOREACH(mount, &mounts, next) {
> +        result = g_malloc0(sizeof(*result));
> +        result->path = g_strdup(mount->dirname);
> +
> +        QAPI_LIST_PREPEND(response->paths, result);
> +
> +        fd = qga_open_cloexec(mount->dirname, O_RDONLY, 0);
> +        if (fd == -1) {
> +            result->error = g_strdup_printf("failed to open: %s",
> +                                            strerror(errno));
> +            result->has_error = true;
> +            continue;
> +        }
> +
> +        /* We try to cull filesystems we know won't work in advance, but
> other
> +         * filesystems may not implement fstrim for less obvious reasons.
> +         * These will report EOPNOTSUPP; while in some other cases ENOTTY
> +         * will be reported (e.g. CD-ROMs).
> +         * Any other error means an unexpected error.
> +         */
> +        r.start = 0;
> +        r.len = -1;
> +        r.minlen = has_minimum ? minimum : 0;
> +        ret = ioctl(fd, FITRIM, &r);
> +        if (ret == -1) {
> +            result->has_error = true;
> +            if (errno == ENOTTY || errno == EOPNOTSUPP) {
> +                result->error = g_strdup("trim not supported");
> +            } else {
> +                result->error = g_strdup_printf("failed to trim: %s",
> +                                                strerror(errno));
> +            }
> +            close(fd);
> +            continue;
> +        }
> +
> +        result->has_minimum = true;
> +        result->minimum = r.minlen;
> +        result->has_trimmed = true;
> +        result->trimmed = r.len;
> +        close(fd);
> +    }
> +
> +    free_fs_mount_list(&mounts);
> +    return response;
> +}
> +#endif /* CONFIG_FSTRIM */
> diff --git a/qga/commands-posix.c b/qga/commands-posix.c
> index 32493d6383..b2a6d8b227 100644
> --- a/qga/commands-posix.c
> +++ b/qga/commands-posix.c
> @@ -1607,78 +1607,6 @@ GuestFilesystemInfoList *qmp_guest_get_fsinfo(Error
> **errp)
>  }
>  #endif /* CONFIG_FSFREEZE */
>
> -#if defined(CONFIG_FSTRIM)
> -/*
> - * Walk list of mounted file systems in the guest, and trim them.
> - */
> -GuestFilesystemTrimResponse *
> -qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **errp)
> -{
> -    GuestFilesystemTrimResponse *response;
> -    GuestFilesystemTrimResult *result;
> -    int ret = 0;
> -    FsMountList mounts;
> -    struct FsMount *mount;
> -    int fd;
> -    struct fstrim_range r;
> -
> -    slog("guest-fstrim called");
> -
> -    QTAILQ_INIT(&mounts);
> -    if (!build_fs_mount_list(&mounts, errp)) {
> -        return NULL;
> -    }
> -
> -    response = g_malloc0(sizeof(*response));
> -
> -    QTAILQ_FOREACH(mount, &mounts, next) {
> -        result = g_malloc0(sizeof(*result));
> -        result->path = g_strdup(mount->dirname);
> -
> -        QAPI_LIST_PREPEND(response->paths, result);
> -
> -        fd = qga_open_cloexec(mount->dirname, O_RDONLY, 0);
> -        if (fd == -1) {
> -            result->error = g_strdup_printf("failed to open: %s",
> -                                            strerror(errno));
> -            result->has_error = true;
> -            continue;
> -        }
> -
> -        /* We try to cull filesystems we know won't work in advance, but
> other
> -         * filesystems may not implement fstrim for less obvious reasons.
> -         * These will report EOPNOTSUPP; while in some other cases ENOTTY
> -         * will be reported (e.g. CD-ROMs).
> -         * Any other error means an unexpected error.
> -         */
> -        r.start = 0;
> -        r.len = -1;
> -        r.minlen = has_minimum ? minimum : 0;
> -        ret = ioctl(fd, FITRIM, &r);
> -        if (ret == -1) {
> -            result->has_error = true;
> -            if (errno == ENOTTY || errno == EOPNOTSUPP) {
> -                result->error = g_strdup("trim not supported");
> -            } else {
> -                result->error = g_strdup_printf("failed to trim: %s",
> -                                                strerror(errno));
> -            }
> -            close(fd);
> -            continue;
> -        }
> -
> -        result->has_minimum = true;
> -        result->minimum = r.minlen;
> -        result->has_trimmed = true;
> -        result->trimmed = r.len;
> -        close(fd);
> -    }
> -
> -    free_fs_mount_list(&mounts);
> -    return response;
> -}
> -#endif /* CONFIG_FSTRIM */
> -
>
>  #define LINUX_SYS_STATE_FILE "/sys/power/state"
>  #define SUSPEND_SUPPORTED 0
> --
> 2.34.1
>
>

Reply via email to