From: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
To: qemu-devel@nongnu.org
Cc: den@virtuozzo.com, michael.roth@amd.com, kkostiuk@redhat.com,
marcandre.lureau@gmail.com
Subject: [PATCH 2/2] qga: Add ZFS TRIM support for FreeBSD
Date: Tue, 15 Nov 2022 19:46:00 +0100 [thread overview]
Message-ID: <20221115184600.747961-3-alexander.ivanov@virtuozzo.com> (raw)
In-Reply-To: <20221115184600.747961-1-alexander.ivanov@virtuozzo.com>
Use zpool tool for ZFS pools trimming in FreeBSD.
Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
---
qga/commands-bsd.c | 109 ++++++++++++++++++++++++++++++++++++++++++
qga/commands-common.h | 1 +
2 files changed, 110 insertions(+)
diff --git a/qga/commands-bsd.c b/qga/commands-bsd.c
index 15cade2d4c..960c4209e5 100644
--- a/qga/commands-bsd.c
+++ b/qga/commands-bsd.c
@@ -170,6 +170,115 @@ GuestCpuStatsList *qmp_guest_get_cpustats(Error **errp)
}
#endif /* CONFIG_FSFREEZE */
+#if defined(CONFIG_FSTRIM)
+typedef struct FsPool {
+ char *name;
+ QTAILQ_ENTRY(FsPool) next;
+} FsPool;
+
+typedef QTAILQ_HEAD(FsPoolList, FsPool) FsPoolList;
+
+static void build_fs_pool_list(FsPoolList *pools, Error **errp)
+{
+ FILE *fp;
+ char *line = NULL, *p;
+ size_t linecap = 0;
+ ssize_t linelen;
+ FsPool *pool;
+
+ fp = popen("/sbin/zpool list -H", "r");
+ if (fp == NULL) {
+ error_setg_errno(errp, errno, "failed to run zpool");
+ return;
+ }
+
+ while ((linelen = getline(&line, &linecap, fp)) > 0) {
+ p = strchr(line, '\t');
+ if (!p) {
+ continue;
+ }
+
+ *p = '\0';
+
+ pool = g_new0(FsPool, 1);
+ pool->name = g_strdup(line);
+ QTAILQ_INSERT_TAIL(pools, pool, next);
+ }
+
+ free(line);
+ pclose(fp);
+}
+
+static void free_fs_pool_list(FsPoolList *pools)
+{
+ FsPool *pool, *temp;
+
+ if (!pools) {
+ return;
+ }
+
+ QTAILQ_FOREACH_SAFE(pool, pools, next, temp) {
+ QTAILQ_REMOVE(pools, pool, next);
+ g_free(pool->name);
+ g_free(pool);
+ }
+}
+
+/*
+ * Walk the list of ZFS pools in the guest, and trim them.
+ */
+GuestFilesystemTrimResponse *
+qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **errp)
+{
+ GuestFilesystemTrimResponse *response;
+ GuestFilesystemTrimResultList *list;
+ GuestFilesystemTrimResult *result;
+ int ret;
+ FsPoolList pools;
+ FsPool *pool;
+ char cmd[256];
+ Error *local_err = NULL;
+
+ slog("guest-fstrim called");
+
+ QTAILQ_INIT(&pools);
+ build_fs_pool_list(&pools, &local_err);
+ if (local_err) {
+ error_propagate(errp, local_err);
+ return NULL;
+ }
+
+ response = g_malloc0(sizeof(*response));
+
+ QTAILQ_FOREACH(pool, &pools, next) {
+ result = g_malloc0(sizeof(*result));
+ result->path = g_strdup(pool->name);
+
+ list = g_malloc0(sizeof(*list));
+ list->value = result;
+ list->next = response->paths;
+ response->paths = list;
+
+ snprintf(cmd, sizeof(cmd), "/sbin/zpool trim %s", pool->name);
+ ret = system(cmd);
+ if (ret != 0) {
+ result->error = g_strdup_printf("failed to trim %s: %s",
+ pool->name, strerror(errno));
+ result->has_error = true;
+ continue;
+ }
+
+ result->has_minimum = true;
+ result->minimum = 0;
+ result->has_trimmed = true;
+ result->trimmed = 0;
+ }
+
+ free_fs_pool_list(&pools);
+ return response;
+}
+#endif /* CONFIG_FSTRIM */
+
#ifdef HAVE_GETIFADDRS
/*
* Fill "buf" with MAC address by ifaddrs. Pointer buf must point to a
diff --git a/qga/commands-common.h b/qga/commands-common.h
index 8c1c56aac9..922f9c479b 100644
--- a/qga/commands-common.h
+++ b/qga/commands-common.h
@@ -28,6 +28,7 @@
#ifdef UFSSUSPEND
#define CONFIG_FSFREEZE
#endif
+#define CONFIG_FSTRIM
#endif /* __FreeBSD__ */
#if defined(CONFIG_FSFREEZE) || defined(CONFIG_FSTRIM)
--
2.34.1
next prev parent reply other threads:[~2022-11-15 18:47 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-15 18:45 [PATCH 0/2] qga: Add ZFS TRIM support for FreeBSD Alexander Ivanov
2022-11-15 18:45 ` [PATCH 1/2] qga: Move FS TRIM code to commands-linux.c Alexander Ivanov
2022-11-17 8:54 ` Konstantin Kostiuk
2022-11-15 18:46 ` Alexander Ivanov [this message]
2022-11-17 8:55 ` [PATCH 2/2] qga: Add ZFS TRIM support for FreeBSD Konstantin Kostiuk
2022-12-26 14:05 ` [PATCH 0/2] " Konstantin Kostiuk
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20221115184600.747961-3-alexander.ivanov@virtuozzo.com \
--to=alexander.ivanov@virtuozzo.com \
--cc=den@virtuozzo.com \
--cc=kkostiuk@redhat.com \
--cc=marcandre.lureau@gmail.com \
--cc=michael.roth@amd.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).