From patchwork Thu Sep 29 10:29:26 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Denis V. Lunev" X-Patchwork-Id: 9356107 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id BA0076077A for ; Thu, 29 Sep 2016 10:30:09 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A99B329930 for ; Thu, 29 Sep 2016 10:30:09 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9ADFE29932; Thu, 29 Sep 2016 10:30:09 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id D489A29930 for ; Thu, 29 Sep 2016 10:30:08 +0000 (UTC) Received: from localhost ([::1]:36199 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bpYbQ-0001WW-3K for patchwork-qemu-devel@patchwork.kernel.org; Thu, 29 Sep 2016 06:30:08 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52024) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bpYaz-0001UW-Nw for qemu-devel@nongnu.org; Thu, 29 Sep 2016 06:29:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bpYaw-0005iS-F9 for qemu-devel@nongnu.org; Thu, 29 Sep 2016 06:29:41 -0400 Received: from mailhub.sw.ru ([195.214.232.25]:28348 helo=relay.sw.ru) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bpYaw-0005g5-2W for qemu-devel@nongnu.org; Thu, 29 Sep 2016 06:29:38 -0400 Received: from iris.sw.ru ([10.30.2.139]) by relay.sw.ru (8.13.4/8.13.4) with ESMTP id u8TATQQm006596; Thu, 29 Sep 2016 13:29:27 +0300 (MSK) From: "Denis V. Lunev" To: qemu-devel@nongnu.org Date: Thu, 29 Sep 2016 13:29:26 +0300 Message-Id: <1475144966-11344-1-git-send-email-den@openvz.org> X-Mailer: git-send-email 2.7.4 X-detected-operating-system: by eggs.gnu.org: OpenBSD 3.x X-Received-From: 195.214.232.25 Subject: [Qemu-devel] [PATCH 1/1] qga: minimal support for fstrim for Windows guests X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: den@openvz.org, Denis Plotnikov , Michael Roth , Stefan Weil Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Unfortunately, there is no public Windows API to start trimming the filesystem. The only viable way here is to call 'defrag.exe /L' for each volume. This is working since Win8 and Win2k12. Signed-off-by: Denis V. Lunev Signed-off-by: Denis Plotnikov CC: Michael Roth CC: Stefan Weil --- qga/commands-win32.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 91 insertions(+), 3 deletions(-) diff --git a/qga/commands-win32.c b/qga/commands-win32.c index 9c9be12..7144bac 100644 --- a/qga/commands-win32.c +++ b/qga/commands-win32.c @@ -840,8 +840,96 @@ static void guest_fsfreeze_cleanup(void) GuestFilesystemTrimResponse * qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **errp) { - error_setg(errp, QERR_UNSUPPORTED); - return NULL; + GuestFilesystemTrimResponse *resp = g_new0(GuestFilesystemTrimResponse, 1); + HANDLE handle; + WCHAR guid[MAX_PATH] = L""; + + handle = FindFirstVolumeW(guid, ARRAYSIZE(guid)); + if (handle == INVALID_HANDLE_VALUE) { + return resp; + } + + do { + GuestFilesystemTrimResult *res; + GuestFilesystemTrimResultList *list; + PWCHAR uc_path; + DWORD char_count = 0; + char *path, *out; + GError *gerr = NULL; + gchar *argv[4]; + + GetVolumePathNamesForVolumeNameW(guid, NULL, 0, &char_count); + + if (GetLastError() != ERROR_MORE_DATA) { + continue; + } + if (GetDriveTypeW(guid) != DRIVE_FIXED) { + continue; + } + + uc_path = g_malloc0(sizeof(WCHAR) * char_count); + if (!GetVolumePathNamesForVolumeNameW( + guid, uc_path, char_count, &char_count) || !*uc_path) { + // strange, but this condition could be faced even with size == 2 */ + g_free(uc_path); + continue; + } + + res = g_new0(GuestFilesystemTrimResult, 1); + + path = g_utf16_to_utf8(uc_path, char_count, NULL, NULL, &gerr); + + g_free(uc_path); + + if (gerr != NULL && gerr->code) { + res->has_error = true; + res->error = g_strdup(gerr->message); + g_error_free(gerr); + break; + } + + res->path = path; + + list = g_new0(GuestFilesystemTrimResultList, 1); + list->value = res; + list->next = resp->paths; + + resp->paths = list; + + memset(argv, 0, sizeof(argv)); + argv[0] = (gchar *)"defrag.exe"; + argv[1] = (gchar *)"/L"; + argv[2] = path; + + if (!g_spawn_sync(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, + &out /* stdout */, NULL /* stdin */, + NULL, &gerr)) { + res->has_error = true; + res->error = g_strdup(gerr->message); + g_error_free(gerr); + } else { + /* defrag.exe is UGLY. Exit code is ALWAYS zero. + Error is reported in the output with something like + (x89000020) etc code in the stdout */ + + int i; + gchar **lines = g_strsplit(out, "\r\n", 0); + g_free(out); + + for (i = 0; lines[i] != NULL; i++) { + if (g_strstr_len(lines[i], -1, "(0x") == NULL) { + continue; + } + res->has_error = true; + res->error = g_strdup(lines[i]); + break; + } + g_strfreev(lines); + } + } while (FindNextVolumeW(handle, guid, ARRAYSIZE(guid))); + + FindVolumeClose(handle); + return resp; } typedef enum { @@ -1416,7 +1504,7 @@ GList *ga_command_blacklist_init(GList *blacklist) "guest-get-memory-blocks", "guest-set-memory-blocks", "guest-get-memory-block-size", "guest-fsfreeze-freeze-list", - "guest-fstrim", NULL}; + NULL}; char **p = (char **)list_unsupported; while (*p) {