From nobody Tue Feb 10 12:42:54 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1666807116; cv=none; d=zohomail.com; s=zohoarc; b=M8Wb3tyQ3ysziIjT0Y/uf8VmoThrWjizHDHWD7pWHrxCDuyTZRuyyLbwatRKYRjV85yePEJ/KTAKiHoAbSX7IlncH689WNzJAKd1Zrz/2E2YRjKz0ZPx0nNUAnPHiJTZF1fQBbfu2Azyc1XgNEwgtHpy1DZgf/gS1wAC/tVg4RA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1666807116; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=mnv0neNcobe/ntnl4axxcr5ar9MYcckbikluPly7YVg=; b=AyuAzHsv6Ypcw8daZqZDtK2tqhRDhOgWXF75enQykesD+bQat0wMZCHbyqOWM2pWypNwSkdlf9/YD/PjpJBq+9wQE3kViHzXSNdNuCmSnxSiVCD3mqbfH71rca+cDe3Vxx0geiI/hytUDmLOjcE6t9KYPkDgIvAg9LpWvet4m88= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1666807116374796.6141456392561; Wed, 26 Oct 2022 10:58:36 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1onkct-0008QC-Oh; Wed, 26 Oct 2022 13:55:39 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1onkcq-00086b-7L for qemu-devel@nongnu.org; Wed, 26 Oct 2022 13:55:36 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1onkcn-0005e4-A0 for qemu-devel@nongnu.org; Wed, 26 Oct 2022 13:55:35 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-168-W7seIXaJM4CFE39OZI7W1g-1; Wed, 26 Oct 2022 13:55:30 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0BF5D8828C9; Wed, 26 Oct 2022 17:55:30 +0000 (UTC) Received: from kostyanf14nb.redhat.com (ovpn-192-17.brq.redhat.com [10.40.192.17]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2CC792166B4C; Wed, 26 Oct 2022 17:55:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666806932; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=mnv0neNcobe/ntnl4axxcr5ar9MYcckbikluPly7YVg=; b=GwanLVBMch3vA5Z5xjw1OWINHtKagKXtEOoRKh82BPy2qMZ+FKzyd25gTWmGZm3Jo89qY0 ENX4GrwgoszlRwJsY9GXPKwUcQx8NaO9WqPbbaonLxzo9vWvMeHNKpOni0jJZAUEhbzqU9 iqkSEnFk5XbfS873ZERvlFIIjgoS3g8= X-MC-Unique: W7seIXaJM4CFE39OZI7W1g-1 From: Konstantin Kostiuk To: qemu-devel@nongnu.org, Peter Maydell , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= Subject: [PULL 2/8] qga: Move Linux-specific FS freeze/thaw code to a separate file Date: Wed, 26 Oct 2022 20:55:12 +0300 Message-Id: <20221026175518.2636846-3-kkostiuk@redhat.com> In-Reply-To: <20221026175518.2636846-1-kkostiuk@redhat.com> References: <20221026175518.2636846-1-kkostiuk@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=kkostiuk@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.515, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Qemu-devel" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1666807117767100001 From: Alexander Ivanov In the next patches we are going to add FreeBSD support for QEMU Guest Agent. In the result, code in commands-posix.c will be too cumbersome. Move Linux-specific FS freeze/thaw code to a separate file commands-linux.c keeping common POSIX code in commands-posix.c. Reviewed-by: Konstantin Kostiuk Reviewed-by: Marc-Andr=C3=A9 Lureau Signed-off-by: Alexander Ivanov Signed-off-by: Konstantin Kostiuk --- qga/commands-common.h | 34 +++++ qga/commands-linux.c | 286 +++++++++++++++++++++++++++++++++++++++++ qga/commands-posix.c | 287 +++--------------------------------------- qga/meson.build | 3 + 4 files changed, 338 insertions(+), 272 deletions(-) create mode 100644 qga/commands-linux.c diff --git a/qga/commands-common.h b/qga/commands-common.h index d0e4a9696f..cb51b1c6e9 100644 --- a/qga/commands-common.h +++ b/qga/commands-common.h @@ -10,6 +10,40 @@ #define QGA_COMMANDS_COMMON_H =20 #include "qga-qapi-types.h" +#include "guest-agent-core.h" +#include "qemu/queue.h" + +#if defined(__linux__) +#include +#ifdef FIFREEZE +#define CONFIG_FSFREEZE +#endif +#ifdef FITRIM +#define CONFIG_FSTRIM +#endif +#endif /* __linux__ */ + +#if defined(CONFIG_FSFREEZE) || defined(CONFIG_FSTRIM) +typedef struct FsMount { + char *dirname; + char *devtype; + unsigned int devmajor, devminor; + QTAILQ_ENTRY(FsMount) next; +} FsMount; + +typedef QTAILQ_HEAD(FsMountList, FsMount) FsMountList; + +bool build_fs_mount_list(FsMountList *mounts, Error **errp); +void free_fs_mount_list(FsMountList *mounts); +#endif /* CONFIG_FSFREEZE || CONFIG_FSTRIM */ + +#if defined(CONFIG_FSFREEZE) +int64_t qmp_guest_fsfreeze_do_freeze_list(bool has_mountpoints, + strList *mountpoints, + FsMountList mounts, + Error **errp); +int qmp_guest_fsfreeze_do_thaw(Error **errp); +#endif /* CONFIG_FSFREEZE */ =20 typedef struct GuestFileHandle GuestFileHandle; =20 diff --git a/qga/commands-linux.c b/qga/commands-linux.c new file mode 100644 index 0000000000..214e408fcd --- /dev/null +++ b/qga/commands-linux.c @@ -0,0 +1,286 @@ +/* + * QEMU Guest Agent Linux-specific command implementations + * + * Copyright IBM Corp. 2011 + * + * Authors: + * Michael Roth + * Michal Privoznik + * + * This work is licensed under the terms of the GNU GPL, version 2 or late= r. + * See the COPYING file in the top-level directory. + */ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "commands-common.h" +#include "cutils.h" +#include +#include + +#if defined(CONFIG_FSFREEZE) || defined(CONFIG_FSTRIM) +static int dev_major_minor(const char *devpath, + unsigned int *devmajor, unsigned int *devminor) +{ + struct stat st; + + *devmajor =3D 0; + *devminor =3D 0; + + if (stat(devpath, &st) < 0) { + slog("failed to stat device file '%s': %s", devpath, strerror(errn= o)); + return -1; + } + if (S_ISDIR(st.st_mode)) { + /* It is bind mount */ + return -2; + } + if (S_ISBLK(st.st_mode)) { + *devmajor =3D major(st.st_rdev); + *devminor =3D minor(st.st_rdev); + return 0; + } + return -1; +} + +static bool build_fs_mount_list_from_mtab(FsMountList *mounts, Error **err= p) +{ + struct mntent *ment; + FsMount *mount; + char const *mtab =3D "/proc/self/mounts"; + FILE *fp; + unsigned int devmajor, devminor; + + fp =3D setmntent(mtab, "r"); + if (!fp) { + error_setg(errp, "failed to open mtab file: '%s'", mtab); + return false; + } + + while ((ment =3D getmntent(fp))) { + /* + * An entry which device name doesn't start with a '/' is + * either a dummy file system or a network file system. + * Add special handling for smbfs and cifs as is done by + * coreutils as well. + */ + if ((ment->mnt_fsname[0] !=3D '/') || + (strcmp(ment->mnt_type, "smbfs") =3D=3D 0) || + (strcmp(ment->mnt_type, "cifs") =3D=3D 0)) { + continue; + } + if (dev_major_minor(ment->mnt_fsname, &devmajor, &devminor) =3D=3D= -2) { + /* Skip bind mounts */ + continue; + } + + mount =3D g_new0(FsMount, 1); + mount->dirname =3D g_strdup(ment->mnt_dir); + mount->devtype =3D g_strdup(ment->mnt_type); + mount->devmajor =3D devmajor; + mount->devminor =3D devminor; + + QTAILQ_INSERT_TAIL(mounts, mount, next); + } + + endmntent(fp); + return true; +} + +static void decode_mntname(char *name, int len) +{ + int i, j =3D 0; + for (i =3D 0; i <=3D len; i++) { + if (name[i] !=3D '\\') { + name[j++] =3D name[i]; + } else if (name[i + 1] =3D=3D '\\') { + name[j++] =3D '\\'; + i++; + } else if (name[i + 1] >=3D '0' && name[i + 1] <=3D '3' && + name[i + 2] >=3D '0' && name[i + 2] <=3D '7' && + name[i + 3] >=3D '0' && name[i + 3] <=3D '7') { + name[j++] =3D (name[i + 1] - '0') * 64 + + (name[i + 2] - '0') * 8 + + (name[i + 3] - '0'); + i +=3D 3; + } else { + name[j++] =3D name[i]; + } + } +} + +/* + * Walk the mount table and build a list of local file systems + */ +bool build_fs_mount_list(FsMountList *mounts, Error **errp) +{ + FsMount *mount; + char const *mountinfo =3D "/proc/self/mountinfo"; + FILE *fp; + char *line =3D NULL, *dash; + size_t n; + char check; + unsigned int devmajor, devminor; + int ret, dir_s, dir_e, type_s, type_e, dev_s, dev_e; + + fp =3D fopen(mountinfo, "r"); + if (!fp) { + return build_fs_mount_list_from_mtab(mounts, errp); + } + + while (getline(&line, &n, fp) !=3D -1) { + ret =3D sscanf(line, "%*u %*u %u:%u %*s %n%*s%n%c", + &devmajor, &devminor, &dir_s, &dir_e, &check); + if (ret < 3) { + continue; + } + dash =3D strstr(line + dir_e, " - "); + if (!dash) { + continue; + } + ret =3D sscanf(dash, " - %n%*s%n %n%*s%n%c", + &type_s, &type_e, &dev_s, &dev_e, &check); + if (ret < 1) { + continue; + } + line[dir_e] =3D 0; + dash[type_e] =3D 0; + dash[dev_e] =3D 0; + decode_mntname(line + dir_s, dir_e - dir_s); + decode_mntname(dash + dev_s, dev_e - dev_s); + if (devmajor =3D=3D 0) { + /* btrfs reports major number =3D 0 */ + if (strcmp("btrfs", dash + type_s) !=3D 0 || + dev_major_minor(dash + dev_s, &devmajor, &devminor) < 0) { + continue; + } + } + + mount =3D g_new0(FsMount, 1); + mount->dirname =3D g_strdup(line + dir_s); + mount->devtype =3D g_strdup(dash + type_s); + mount->devmajor =3D devmajor; + mount->devminor =3D devminor; + + QTAILQ_INSERT_TAIL(mounts, mount, next); + } + free(line); + + fclose(fp); + return true; +} +#endif /* CONFIG_FSFREEZE || CONFIG_FSTRIM */ + +#ifdef CONFIG_FSFREEZE +/* + * Walk list of mounted file systems in the guest, and freeze the ones whi= ch + * are real local file systems. + */ +int64_t qmp_guest_fsfreeze_do_freeze_list(bool has_mountpoints, + strList *mountpoints, + FsMountList mounts, + Error **errp) +{ + struct FsMount *mount; + strList *list; + int fd, ret, i =3D 0; + + QTAILQ_FOREACH_REVERSE(mount, &mounts, next) { + /* To issue fsfreeze in the reverse order of mounts, check if the + * mount is listed in the list here */ + if (has_mountpoints) { + for (list =3D mountpoints; list; list =3D list->next) { + if (strcmp(list->value, mount->dirname) =3D=3D 0) { + break; + } + } + if (!list) { + continue; + } + } + + fd =3D qga_open_cloexec(mount->dirname, O_RDONLY, 0); + if (fd =3D=3D -1) { + error_setg_errno(errp, errno, "failed to open %s", mount->dirn= ame); + return -1; + } + + /* we try to cull filesystems we know won't work in advance, but o= ther + * filesystems may not implement fsfreeze for less obvious reasons. + * these will report EOPNOTSUPP. we simply ignore these when tally= ing + * the number of frozen filesystems. + * if a filesystem is mounted more than once (aka bind mount) a + * consecutive attempt to freeze an already frozen filesystem will + * return EBUSY. + * + * any other error means a failure to freeze a filesystem we + * expect to be freezable, so return an error in those cases + * and return system to thawed state. + */ + ret =3D ioctl(fd, FIFREEZE); + if (ret =3D=3D -1) { + if (errno !=3D EOPNOTSUPP && errno !=3D EBUSY) { + error_setg_errno(errp, errno, "failed to freeze %s", + mount->dirname); + close(fd); + return -1; + } + } else { + i++; + } + close(fd); + } + return i; +} + +int qmp_guest_fsfreeze_do_thaw(Error **errp) +{ + int ret; + FsMountList mounts; + FsMount *mount; + int fd, i =3D 0, logged; + Error *local_err =3D NULL; + + QTAILQ_INIT(&mounts); + if (!build_fs_mount_list(&mounts, &local_err)) { + error_propagate(errp, local_err); + return -1; + } + + QTAILQ_FOREACH(mount, &mounts, next) { + logged =3D false; + fd =3D qga_open_cloexec(mount->dirname, O_RDONLY, 0); + if (fd =3D=3D -1) { + continue; + } + /* we have no way of knowing whether a filesystem was actually unf= rozen + * as a result of a successful call to FITHAW, only that if an err= or + * was returned the filesystem was *not* unfrozen by that particul= ar + * call. + * + * since multiple preceding FIFREEZEs require multiple calls to FI= THAW + * to unfreeze, continuing issuing FITHAW until an error is return= ed, + * in which case either the filesystem is in an unfreezable state,= or, + * more likely, it was thawed previously (and remains so afterward= ). + * + * also, since the most recent successful call is the one that did + * the actual unfreeze, we can use this to provide an accurate cou= nt + * of the number of filesystems unfrozen by guest-fsfreeze-thaw, w= hich + * may * be useful for determining whether a filesystem was unfroz= en + * during the freeze/thaw phase by a process other than qemu-ga. + */ + do { + ret =3D ioctl(fd, FITHAW); + if (ret =3D=3D 0 && !logged) { + i++; + logged =3D true; + } + } while (ret =3D=3D 0); + close(fd); + } + + free_fs_mount_list(&mounts); + + return i; +} +#endif /* CONFIG_FSFREEZE */ diff --git a/qga/commands-posix.c b/qga/commands-posix.c index 16d67e9f6d..d24f2fafd8 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -16,11 +16,9 @@ #include #include #include -#include "guest-agent-core.h" #include "qga-qapi-commands.h" #include "qapi/error.h" #include "qapi/qmp/qerror.h" -#include "qemu/queue.h" #include "qemu/host-utils.h" #include "qemu/sockets.h" #include "qemu/base64.h" @@ -629,16 +627,7 @@ void qmp_guest_file_flush(int64_t handle, Error **errp) #if defined(__linux__) =20 #if defined(CONFIG_FSFREEZE) || defined(CONFIG_FSTRIM) -typedef struct FsMount { - char *dirname; - char *devtype; - unsigned int devmajor, devminor; - QTAILQ_ENTRY(FsMount) next; -} FsMount; - -typedef QTAILQ_HEAD(FsMountList, FsMount) FsMountList; - -static void free_fs_mount_list(FsMountList *mounts) +void free_fs_mount_list(FsMountList *mounts) { FsMount *mount, *temp; =20 @@ -653,157 +642,6 @@ static void free_fs_mount_list(FsMountList *mounts) g_free(mount); } } - -static int dev_major_minor(const char *devpath, - unsigned int *devmajor, unsigned int *devminor) -{ - struct stat st; - - *devmajor =3D 0; - *devminor =3D 0; - - if (stat(devpath, &st) < 0) { - slog("failed to stat device file '%s': %s", devpath, strerror(errn= o)); - return -1; - } - if (S_ISDIR(st.st_mode)) { - /* It is bind mount */ - return -2; - } - if (S_ISBLK(st.st_mode)) { - *devmajor =3D major(st.st_rdev); - *devminor =3D minor(st.st_rdev); - return 0; - } - return -1; -} - -/* - * Walk the mount table and build a list of local file systems - */ -static bool build_fs_mount_list_from_mtab(FsMountList *mounts, Error **err= p) -{ - struct mntent *ment; - FsMount *mount; - char const *mtab =3D "/proc/self/mounts"; - FILE *fp; - unsigned int devmajor, devminor; - - fp =3D setmntent(mtab, "r"); - if (!fp) { - error_setg(errp, "failed to open mtab file: '%s'", mtab); - return false; - } - - while ((ment =3D getmntent(fp))) { - /* - * An entry which device name doesn't start with a '/' is - * either a dummy file system or a network file system. - * Add special handling for smbfs and cifs as is done by - * coreutils as well. - */ - if ((ment->mnt_fsname[0] !=3D '/') || - (strcmp(ment->mnt_type, "smbfs") =3D=3D 0) || - (strcmp(ment->mnt_type, "cifs") =3D=3D 0)) { - continue; - } - if (dev_major_minor(ment->mnt_fsname, &devmajor, &devminor) =3D=3D= -2) { - /* Skip bind mounts */ - continue; - } - - mount =3D g_new0(FsMount, 1); - mount->dirname =3D g_strdup(ment->mnt_dir); - mount->devtype =3D g_strdup(ment->mnt_type); - mount->devmajor =3D devmajor; - mount->devminor =3D devminor; - - QTAILQ_INSERT_TAIL(mounts, mount, next); - } - - endmntent(fp); - return true; -} - -static void decode_mntname(char *name, int len) -{ - int i, j =3D 0; - for (i =3D 0; i <=3D len; i++) { - if (name[i] !=3D '\\') { - name[j++] =3D name[i]; - } else if (name[i + 1] =3D=3D '\\') { - name[j++] =3D '\\'; - i++; - } else if (name[i + 1] >=3D '0' && name[i + 1] <=3D '3' && - name[i + 2] >=3D '0' && name[i + 2] <=3D '7' && - name[i + 3] >=3D '0' && name[i + 3] <=3D '7') { - name[j++] =3D (name[i + 1] - '0') * 64 + - (name[i + 2] - '0') * 8 + - (name[i + 3] - '0'); - i +=3D 3; - } else { - name[j++] =3D name[i]; - } - } -} - -static bool build_fs_mount_list(FsMountList *mounts, Error **errp) -{ - FsMount *mount; - char const *mountinfo =3D "/proc/self/mountinfo"; - FILE *fp; - char *line =3D NULL, *dash; - size_t n; - char check; - unsigned int devmajor, devminor; - int ret, dir_s, dir_e, type_s, type_e, dev_s, dev_e; - - fp =3D fopen(mountinfo, "r"); - if (!fp) { - return build_fs_mount_list_from_mtab(mounts, errp); - } - - while (getline(&line, &n, fp) !=3D -1) { - ret =3D sscanf(line, "%*u %*u %u:%u %*s %n%*s%n%c", - &devmajor, &devminor, &dir_s, &dir_e, &check); - if (ret < 3) { - continue; - } - dash =3D strstr(line + dir_e, " - "); - if (!dash) { - continue; - } - ret =3D sscanf(dash, " - %n%*s%n %n%*s%n%c", - &type_s, &type_e, &dev_s, &dev_e, &check); - if (ret < 1) { - continue; - } - line[dir_e] =3D 0; - dash[type_e] =3D 0; - dash[dev_e] =3D 0; - decode_mntname(line + dir_s, dir_e - dir_s); - decode_mntname(dash + dev_s, dev_e - dev_s); - if (devmajor =3D=3D 0) { - /* btrfs reports major number =3D 0 */ - if (strcmp("btrfs", dash + type_s) !=3D 0 || - dev_major_minor(dash + dev_s, &devmajor, &devminor) < 0) { - continue; - } - } - - mount =3D g_new0(FsMount, 1); - mount->dirname =3D g_strdup(line + dir_s); - mount->devtype =3D g_strdup(dash + type_s); - mount->devmajor =3D devmajor; - mount->devminor =3D devminor; - - QTAILQ_INSERT_TAIL(mounts, mount, next); - } - free(line); - - fclose(fp); - return true; -} #endif =20 #if defined(CONFIG_FSFREEZE) @@ -1708,20 +1546,13 @@ int64_t qmp_guest_fsfreeze_freeze(Error **errp) return qmp_guest_fsfreeze_freeze_list(false, NULL, errp); } =20 -/* - * Walk list of mounted file systems in the guest, and freeze the ones whi= ch - * are real local file systems. - */ int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints, strList *mountpoints, Error **errp) { - int ret =3D 0, i =3D 0; - strList *list; + int ret; FsMountList mounts; - struct FsMount *mount; Error *local_err =3D NULL; - int fd; =20 slog("guest-fsfreeze called"); =20 @@ -1740,122 +1571,34 @@ int64_t qmp_guest_fsfreeze_freeze_list(bool has_mo= untpoints, /* cannot risk guest agent blocking itself on a write in this state */ ga_set_frozen(ga_state); =20 - QTAILQ_FOREACH_REVERSE(mount, &mounts, next) { - /* To issue fsfreeze in the reverse order of mounts, check if the - * mount is listed in the list here */ - if (has_mountpoints) { - for (list =3D mountpoints; list; list =3D list->next) { - if (strcmp(list->value, mount->dirname) =3D=3D 0) { - break; - } - } - if (!list) { - continue; - } - } - - fd =3D qga_open_cloexec(mount->dirname, O_RDONLY, 0); - if (fd =3D=3D -1) { - error_setg_errno(errp, errno, "failed to open %s", mount->dirn= ame); - goto error; - } - - /* we try to cull filesystems we know won't work in advance, but o= ther - * filesystems may not implement fsfreeze for less obvious reasons. - * these will report EOPNOTSUPP. we simply ignore these when tally= ing - * the number of frozen filesystems. - * if a filesystem is mounted more than once (aka bind mount) a - * consecutive attempt to freeze an already frozen filesystem will - * return EBUSY. - * - * any other error means a failure to freeze a filesystem we - * expect to be freezable, so return an error in those cases - * and return system to thawed state. - */ - ret =3D ioctl(fd, FIFREEZE); - if (ret =3D=3D -1) { - if (errno !=3D EOPNOTSUPP && errno !=3D EBUSY) { - error_setg_errno(errp, errno, "failed to freeze %s", - mount->dirname); - close(fd); - goto error; - } - } else { - i++; - } - close(fd); - } + ret =3D qmp_guest_fsfreeze_do_freeze_list(has_mountpoints, mountpoints, + mounts, errp); =20 free_fs_mount_list(&mounts); /* We may not issue any FIFREEZE here. * Just unset ga_state here and ready for the next call. */ - if (i =3D=3D 0) { + if (ret =3D=3D 0) { ga_unset_frozen(ga_state); + } else if (ret < 0) { + qmp_guest_fsfreeze_thaw(NULL); } - return i; - -error: - free_fs_mount_list(&mounts); - qmp_guest_fsfreeze_thaw(NULL); - return 0; + return ret; } =20 -/* - * Walk list of frozen file systems in the guest, and thaw them. - */ int64_t qmp_guest_fsfreeze_thaw(Error **errp) { int ret; - FsMountList mounts; - FsMount *mount; - int fd, i =3D 0, logged; - Error *local_err =3D NULL; =20 - QTAILQ_INIT(&mounts); - if (!build_fs_mount_list(&mounts, &local_err)) { - error_propagate(errp, local_err); - return 0; - } - - QTAILQ_FOREACH(mount, &mounts, next) { - logged =3D false; - fd =3D qga_open_cloexec(mount->dirname, O_RDONLY, 0); - if (fd =3D=3D -1) { - continue; - } - /* we have no way of knowing whether a filesystem was actually unf= rozen - * as a result of a successful call to FITHAW, only that if an err= or - * was returned the filesystem was *not* unfrozen by that particul= ar - * call. - * - * since multiple preceding FIFREEZEs require multiple calls to FI= THAW - * to unfreeze, continuing issuing FITHAW until an error is return= ed, - * in which case either the filesystem is in an unfreezable state,= or, - * more likely, it was thawed previously (and remains so afterward= ). - * - * also, since the most recent successful call is the one that did - * the actual unfreeze, we can use this to provide an accurate cou= nt - * of the number of filesystems unfrozen by guest-fsfreeze-thaw, w= hich - * may * be useful for determining whether a filesystem was unfroz= en - * during the freeze/thaw phase by a process other than qemu-ga. - */ - do { - ret =3D ioctl(fd, FITHAW); - if (ret =3D=3D 0 && !logged) { - i++; - logged =3D true; - } - } while (ret =3D=3D 0); - close(fd); + ret =3D qmp_guest_fsfreeze_do_thaw(errp); + if (ret >=3D 0) { + ga_unset_frozen(ga_state); + execute_fsfreeze_hook(FSFREEZE_HOOK_THAW, errp); + } else { + ret =3D 0; } =20 - ga_unset_frozen(ga_state); - free_fs_mount_list(&mounts); - - execute_fsfreeze_hook(FSFREEZE_HOOK_THAW, errp); - - return i; + return ret; } =20 static void guest_fsfreeze_cleanup(void) diff --git a/qga/meson.build b/qga/meson.build index a0ffd6d268..932b4e7ca8 100644 --- a/qga/meson.build +++ b/qga/meson.build @@ -72,6 +72,9 @@ qga_ss.add(when: 'CONFIG_POSIX', if_true: files( 'commands-posix.c', 'commands-posix-ssh.c', )) +qga_ss.add(when: 'CONFIG_LINUX', if_true: files( + 'commands-linux.c', +)) qga_ss.add(when: 'CONFIG_WIN32', if_true: files( 'channel-win32.c', 'commands-win32.c', --=20 2.25.1