From patchwork Thu Oct 13 09:23:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alexander Ivanov X-Patchwork-Id: 13005808 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 08A34C4332F for ; Thu, 13 Oct 2022 09:41:39 +0000 (UTC) Received: from localhost ([::1]:45908 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oiuif-0006Ga-PJ for qemu-devel@archiver.kernel.org; Thu, 13 Oct 2022 05:41:37 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33450) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oiuRc-0002M3-VX for qemu-devel@nongnu.org; Thu, 13 Oct 2022 05:24:02 -0400 Received: from mail-am6eur05on2109.outbound.protection.outlook.com ([40.107.22.109]:28065 helo=EUR05-AM6-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oiuRM-0004gX-OC for qemu-devel@nongnu.org; Thu, 13 Oct 2022 05:24:00 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=WVUelJG57jRrSrwavXm8qaoQsSfVNgZivebU/56rx5vBQ5l7jp9HZHl9Sq7QLuq9qLNkw5a1nuUpuw0bexDv6s3zz9sRPD6E0OuO7YbdtEQygXZcUBIKonHKWR/vLlE+Afcij94onTDtwcDSE9RBykAyQ308lDYX3K3OFjgwWb2+MLsDo3q31eBaafEtdfbAclNYNDnJT3MdiXVUAcYEIGHxaBvpTvnfOQjHW3qGAGN74PoQyV5PErYHw6AHGf/m3OyJd2CxfIb7BUMhSE7C+BkGN67cfrEpips/nUNwbuplt36lQImeVpKZIlH3V7VpJyNXwJHqguHI+HNFmcLClQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=DK7nD0sgWCX9/ZdHU/h1wq1s4gT+ZWz1xBxB/M+VB8Y=; b=bhu8m+j/qGwZ1jTyRyUWLbgrkCfnhG5JUxKbJitN6EUt3JOA4bKtsopLs8XqaU57DSZ5tNZ7ATslGYQrRNEBLOnoeNYKl6yBLZ/m2QkujjxY6eRmuWjRfNF6Yj94C7E5sM72IgncjUSZu58FPbA1vuHOqJWUhO3Oi8a499piKt0MDYIQsf4XKDHGzzeXgRdOBG6SekbIqyDYEI/m9VwpjE4wJaLQPnK82JXMZtP3BIs3eA5MhcQ/BBfUf0kWrqG9lrGH7CiZIuXBlWYPRd13tEgk/pI+TlzOcM4V40eKW8GjCUT7jHriEGDO+LzW2s1tlRYfgvVcCohYsZTW/WZM+A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=virtuozzo.com; dmarc=pass action=none header.from=virtuozzo.com; dkim=pass header.d=virtuozzo.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=virtuozzo.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=DK7nD0sgWCX9/ZdHU/h1wq1s4gT+ZWz1xBxB/M+VB8Y=; b=oXD7MCT4A2m+dfunaAQd+69/hrn4esDHXwpra2VediBQhjybq9lOGJidTPcDBWyDGVZJs5qlTuBnGE0qSWpf6k3PhlSVdJdiaIPm090/Z5gBPtLIljqGLvOxKwm6xlv6kBTw3joz/AOMKtOMPZzGZEo5C6G789+NWBRjtOZxgrGTnuTCJ3WK1RwHu9x6keapuI0mRxNhwT3ZwXS2Rrw5qxQSS+NW43nAksPgAfo5KJ/U/GYudTvt06hYSJHeY98B8g3XKB4uRR0e825tGsHT3poUw576bYRujMturlybspELQSdKTNjUkMclxNjeVDi55rle6j9nbAXGutWYiBsFgA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=virtuozzo.com; Received: from AS8PR08MB7095.eurprd08.prod.outlook.com (2603:10a6:20b:402::11) by AM8PR08MB6466.eurprd08.prod.outlook.com (2603:10a6:20b:363::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5709.21; Thu, 13 Oct 2022 09:23:37 +0000 Received: from AS8PR08MB7095.eurprd08.prod.outlook.com ([fe80::5174:25c7:6df8:741b]) by AS8PR08MB7095.eurprd08.prod.outlook.com ([fe80::5174:25c7:6df8:741b%8]) with mapi id 15.20.5676.040; Thu, 13 Oct 2022 09:23:37 +0000 From: Alexander Ivanov To: qemu-devel@nongnu.org Cc: den@virtuozzo.com, michael.roth@amd.com, kkostiuk@redhat.com, marcandre.lureau@gmail.com Subject: [PATCH v4 3/7] qga: Add UFS freeze/thaw support for FreeBSD Date: Thu, 13 Oct 2022 11:23:22 +0200 Message-Id: <20221013092326.2194646-4-alexander.ivanov@virtuozzo.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221013092326.2194646-1-alexander.ivanov@virtuozzo.com> References: <20221013092326.2194646-1-alexander.ivanov@virtuozzo.com> X-ClientProxiedBy: FR0P281CA0069.DEUP281.PROD.OUTLOOK.COM (2603:10a6:d10:49::22) To AS8PR08MB7095.eurprd08.prod.outlook.com (2603:10a6:20b:402::11) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AS8PR08MB7095:EE_|AM8PR08MB6466:EE_ X-MS-Office365-Filtering-Correlation-Id: 803d3e2f-ba73-4342-c740-08daacfc9bc3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 9MvsZcDZBc0277G9qUL8clY9uDDNDDOmtVZDzClMO/1OrtM2Mae53JMhr/id8lUD1l3Lubchho182QMGvHNa8as8BHCF8MemJ6hnVNXx2ZuR3DQ5Brciw5xgqVlsJyj10tUmtGrl3xsV5rNVMQ2PJHAQUXnTuVZ7qMwRcgp+qNBJ9Q5W8yCU4qhGZcjdIvmJpm3/IiRLbpOqIfB0f0pdcTdQe5MxWc7Q85ULwX5/jB6NAYPKBfmkIvQ4OvMiwbmv70NoJ1VySOGCz4oF14aZYwuJcn+E+mewTjF2hxfKZQWgp+kZogTBNRc7HI0ArUrRnF2zquv4q60LaMwFGyi38ITvWkWoSUpIK8OWv88pSOb/5bJKivAFi/kBRHFx9CN4tk6rQVR3UoN1A4vpdVX6PBfz7jnxRUtBD2bTxFVTGzKY0RQYW7xLa5rTCSTeo97vEXrurRrszu7iywJN2QoFTmzK2aL2cEUZdKGOgrCSRbWqZolK71xpEfxRzf98womzAr5cUpEb1Ar54L3sNQxPtoXmsCiPReGuLTC5fHDVJ3qCzsqBvZ08UR/YbkJWYUc+8u+fPT6FBmnwNMtD1Pk6eiXpK8ALHCx3zz8SQtzdGxiaI/OD+pNPizEsKvys5+sq5+6hbUHI03DdyBTZ/Cb18u6LP871Zy/3WDho+3/iC+zTCDI6BYK6QMKGcYqmUgO2SI2IzBreLfW8t+M09jHxK5kiJUZYo2nN5lJWpbulT9LovZUddSw5lhgdkuIPoo589wIfZu1XlLsRPHk8FbtYsQ== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:AS8PR08MB7095.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230022)(4636009)(366004)(376002)(346002)(136003)(39850400004)(396003)(451199015)(36756003)(2906002)(44832011)(4326008)(30864003)(6512007)(5660300002)(41300700001)(8676002)(66556008)(316002)(86362001)(6916009)(66946007)(66476007)(8936002)(38100700002)(478600001)(6486002)(6666004)(38350700002)(83380400001)(26005)(6506007)(52116002)(2616005)(186003)(1076003); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?XLCGs1/DpGau/uuzEkLInbCGAaDt?= =?utf-8?q?KmIw5J6R/eLkp3ZmHVrQvkB34aC1ybfKQl8SJlwRdt4WoYS7BfO2ebXCZqPyG+8n7?= =?utf-8?q?vWQB/O1Wnv+/F3vY9f8QvnOqKphxWXms6ZLGVNeS6ecHAJRA8dGaW5XGPPQgQQjUc?= =?utf-8?q?sl3i/QTPPVMwDit9yA/KG4gIW8eLW4cl5gl0Tqb4VLdcf8VCQ5jZnF+i8eKs3Nezu?= =?utf-8?q?2yNA2NfkBR9gI13EpN8K3MMBrfbpoOTsa+avnMacYnji4F3nq2Z6rc+AUSf2I4ddI?= =?utf-8?q?wFxox/w/IkYXS9nbY+QnECXmTSAUqkrcaCNF1ekfVRsBeiA1OVeERIxCcGDHxhon2?= =?utf-8?q?3pm8/04udBHI/Q+985GGk089ns9UmB9B1ELUBUw87BcDOdEAsg2uQi6C9LetvcgWj?= =?utf-8?q?QqyzaBw4FBfX4kfvmtuTqgLh/fndOYApSBhI9XjSwrOXw3qLJuxOL9EX7VKhFZ9Uf?= =?utf-8?q?i5Y9NH0cxtVbSFj88sbaiMWA2HO5XnY4YtGeHsurfcmGZJT6LIifEUOHVPLsBahdN?= =?utf-8?q?pDi/0CBMv4oxtaNjx9pYzI8CJcS1NSLvLM37N2FPh3WGTgWDGLtusGuKtgUIvxN9h?= =?utf-8?q?N47pLrqca39WxSJm4m0GYDlyqZcFWiKuiXkzf4TP5dofVAJXh072cyC90wUgfFjth?= =?utf-8?q?yWLluVX6w+s7cTbgPeYqqKjRLaocgsLfRNWe9zsdoxKat+IKzu09OIk/fbF4nx6Ek?= =?utf-8?q?M+DsvSrXjvSKxvuAuPY6i3Ew/OJYPB+t5Pdds5zQwoEII4i4frHQVvwvUzkMlSl0F?= =?utf-8?q?K4kfCxMhbj5bA3+8Ue4wRfrGdWT284YOzQ/+ekJH/8rZrPYJLBOII7M9eop4xP/65?= =?utf-8?q?g3/YcBHhfMosQ+suc2aReMtT70NkcoxyuEmQnZSBoRfk9/KWerCVFThr7kL7prsj3?= =?utf-8?q?H9TQu7QfOmr9rpa/a4rSn42BhJW759K0oAD+X4d/82lADOB7PzRGaT40OhrNo8H6U?= =?utf-8?q?iw6XYhUsS0c5Q4tvIXyGoYITQ1L5+94b89FiVzvn3UIPBfBnFJIJxTatc0LYt1yDz?= =?utf-8?q?e9df8O+BVq74I048+RmTx/DzXcbwzEJCXJfLxUK2nGTLnud1GTIH9wU+LEEKDojqu?= =?utf-8?q?K9p8S3ogTsHNsGLHuDc+3cMe6DngeVRPgdXnnh1sJ0DRlrgdfLWZdTmGgDY6VC4kz?= =?utf-8?q?wXgplfnpkckd/bcLqyyZmCFVx8OiF3ci0LJzcNm45rubkQHmrcxy1WZaOYhPasnc1?= =?utf-8?q?N9sY8ewPk22ZfXu0uQC+W90Yyd7teJsmWfV2WvxmLjdReEWWFlWipmhdtu0pICGfT?= =?utf-8?q?T7EsX/0tIvbVbOiZ9lAjRRnsamCC3GEu/wIITot/7qYCwoYHbRt2mFG75af2ubNbw?= =?utf-8?q?q1j1wb39AM9OAmpXucTbXyods071oTPCOX6gQ2ymgjMTGGJPUWYnQM0CKQBfHypBU?= =?utf-8?q?4z8nZ+aupGeTXguBagWfjntH9k4ARlCfWcLponAo/arPgetx8yKVeW5/4y9W4k54W?= =?utf-8?q?FcSE1ZyHib7G0A/5sHqoI0A028fiQLE7FFo1P3p3AV10aVVoAF0FfouKfaWKXlBCF?= =?utf-8?q?gbVSwSxyd0PSHmHOd4AUPL01Y7NHipzmqsHvGqwaMmSNFSnxXUVOlrA=3D?= X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-Network-Message-Id: 803d3e2f-ba73-4342-c740-08daacfc9bc3 X-MS-Exchange-CrossTenant-AuthSource: AS8PR08MB7095.eurprd08.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Oct 2022 09:23:37.2957 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 0bc7f26d-0264-416e-a6fc-8352af79c58f X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 0A5RhK+MIxa8BhP477zfLBguRZhdaqyZzuiL6FjDe7UORSwh4ihSETFGuFJKN3GpXP16WhgwM/OIHwVmWoSTV6nLU8f5FnQQFvbidWEQ0SU= X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM8PR08MB6466 Received-SPF: pass client-ip=40.107.22.109; envelope-from=alexander.ivanov@virtuozzo.com; helo=EUR05-AM6-obe.outbound.protection.outlook.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-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: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" UFS supports FS freezing through ioctl UFSSUSPEND on /dev/ufssuspend. Frozen FS can be thawed by closing /dev/ufssuspend file descriptior. Use getmntinfo to get a list of mounted FS. Reviewed-by: Konstantin Kostiuk Reviewed-by: Marc-André Lureau Signed-off-by: Alexander Ivanov --- qga/commands-bsd.c | 169 +++++++++++++++++++++++ qga/commands-common.h | 11 ++ qga/commands-posix.c | 308 ++++++++++++++++++++---------------------- qga/main.c | 7 +- qga/meson.build | 3 + 5 files changed, 334 insertions(+), 164 deletions(-) create mode 100644 qga/commands-bsd.c diff --git a/qga/commands-bsd.c b/qga/commands-bsd.c new file mode 100644 index 0000000000..ca06692179 --- /dev/null +++ b/qga/commands-bsd.c @@ -0,0 +1,169 @@ +/* + * QEMU Guest Agent BSD-specific command implementations + * + * Copyright (c) Virtuozzo International GmbH. + * + * Authors: + * Alexander Ivanov + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + */ + +#include "qemu/osdep.h" +#include "qga-qapi-commands.h" +#include "qapi/qmp/qerror.h" +#include "qapi/error.h" +#include "qemu/queue.h" +#include "commands-common.h" +#include +#include +#include +#include +#include + +#if defined(CONFIG_FSFREEZE) || defined(CONFIG_FSTRIM) +bool build_fs_mount_list(FsMountList *mounts, Error **errp) +{ + FsMount *mount; + struct statfs *mntbuf, *mntp; + struct stat statbuf; + int i, count, ret; + + count = getmntinfo(&mntbuf, MNT_NOWAIT); + if (count == 0) { + error_setg_errno(errp, errno, "getmntinfo failed"); + return false; + } + + for (i = 0; i < count; i++) { + mntp = &mntbuf[i]; + ret = stat(mntp->f_mntonname, &statbuf); + if (ret != 0) { + error_setg_errno(errp, errno, "stat failed on %s", + mntp->f_mntonname); + return false; + } + + mount = g_new0(FsMount, 1); + + mount->dirname = g_strdup(mntp->f_mntonname); + mount->devtype = g_strdup(mntp->f_fstypename); + mount->devmajor = major(mount->dev); + mount->devminor = minor(mount->dev); + mount->fsid = mntp->f_fsid; + mount->dev = statbuf.st_dev; + + QTAILQ_INSERT_TAIL(mounts, mount, next); + } + return true; +} +#endif /* CONFIG_FSFREEZE || CONFIG_FSTRIM */ + +#if defined(CONFIG_FSFREEZE) +static int ufssuspend_fd = -1; +static int ufssuspend_cnt; + +int64_t qmp_guest_fsfreeze_do_freeze_list(bool has_mountpoints, + strList *mountpoints, + FsMountList mounts, + Error **errp) +{ + int ret; + strList *list; + struct FsMount *mount; + + if (ufssuspend_fd != -1) { + error_setg(errp, "filesystems have already frozen"); + return -1; + } + + ufssuspend_cnt = 0; + ufssuspend_fd = qemu_open(_PATH_UFSSUSPEND, O_RDWR, errp); + if (ufssuspend_fd == -1) { + return -1; + } + + 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 = mountpoints; list; list = list->next) { + if (g_str_equal(list->value, mount->dirname)) { + break; + } + } + if (!list) { + continue; + } + } + + /* Only UFS supports suspend */ + if (!g_str_equal(mount->devtype, "ufs")) { + continue; + } + + ret = ioctl(ufssuspend_fd, UFSSUSPEND, &mount->fsid); + if (ret == -1) { + /* + * ioctl returns EBUSY for all the FS except the first one + * that was suspended + */ + if (errno == EBUSY) { + continue; + } + error_setg_errno(errp, errno, "failed to freeze %s", + mount->dirname); + goto error; + } + ufssuspend_cnt++; + } + return ufssuspend_cnt; +error: + close(ufssuspend_fd); + ufssuspend_fd = -1; + return -1; + +} + +/* + * We don't need to call UFSRESUME ioctl because all the frozen FS + * are thawed on /dev/ufssuspend closing. + */ +int qmp_guest_fsfreeze_do_thaw(Error **errp) +{ + int ret = ufssuspend_cnt; + ufssuspend_cnt = 0; + if (ufssuspend_fd != -1) { + close(ufssuspend_fd); + ufssuspend_fd = -1; + } + return ret; +} + +GuestFilesystemInfoList *qmp_guest_get_fsinfo(Error **errp) +{ + error_setg(errp, QERR_UNSUPPORTED); + return NULL; +} + +GuestDiskInfoList *qmp_guest_get_disks(Error **errp) +{ + error_setg(errp, QERR_UNSUPPORTED); + return NULL; +} + +GuestDiskStatsInfoList *qmp_guest_get_diskstats(Error **errp) +{ + error_setg(errp, QERR_UNSUPPORTED); + return NULL; +} + +GuestCpuStatsList *qmp_guest_get_cpustats(Error **errp) +{ + error_setg(errp, QERR_UNSUPPORTED); + return NULL; +} +#endif /* CONFIG_FSFREEZE */ diff --git a/qga/commands-common.h b/qga/commands-common.h index 181fc330aa..2d9878a634 100644 --- a/qga/commands-common.h +++ b/qga/commands-common.h @@ -23,11 +23,22 @@ #endif #endif /* __linux__ */ +#ifdef __FreeBSD__ +#include +#ifdef UFSSUSPEND +#define CONFIG_FSFREEZE +#endif +#endif /* __FreeBSD__ */ + #if defined(CONFIG_FSFREEZE) || defined(CONFIG_FSTRIM) typedef struct FsMount { char *dirname; char *devtype; unsigned int devmajor, devminor; +#if defined(__FreeBSD__) + dev_t dev; + fsid_t fsid; +#endif QTAILQ_ENTRY(FsMount) next; } FsMount; diff --git a/qga/commands-posix.c b/qga/commands-posix.c index 9574b83c92..49f9996a9c 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -33,20 +33,12 @@ #if defined(__linux__) #include -#include #include #include #ifdef CONFIG_LIBUDEV #include #endif - -#ifdef FIFREEZE -#define CONFIG_FSFREEZE -#endif -#ifdef FITRIM -#define CONFIG_FSTRIM -#endif #endif #ifdef __FreeBSD__ @@ -623,9 +615,6 @@ void qmp_guest_file_flush(int64_t handle, Error **errp) } } -/* linux-specific implementations. avoid this if at all possible. */ -#if defined(__linux__) - #if defined(CONFIG_FSFREEZE) || defined(CONFIG_FSTRIM) void free_fs_mount_list(FsMountList *mounts) { @@ -644,6 +633,156 @@ void free_fs_mount_list(FsMountList *mounts) } #endif +#if defined(CONFIG_FSFREEZE) +typedef enum { + FSFREEZE_HOOK_THAW = 0, + FSFREEZE_HOOK_FREEZE, +} FsfreezeHookArg; + +static const char *fsfreeze_hook_arg_string[] = { + "thaw", + "freeze", +}; + +static void execute_fsfreeze_hook(FsfreezeHookArg arg, Error **errp) +{ + int status; + pid_t pid; + const char *hook; + const char *arg_str = fsfreeze_hook_arg_string[arg]; + Error *local_err = NULL; + + hook = ga_fsfreeze_hook(ga_state); + if (!hook) { + return; + } + if (access(hook, X_OK) != 0) { + error_setg_errno(errp, errno, "can't access fsfreeze hook '%s'", hook); + return; + } + + slog("executing fsfreeze hook with arg '%s'", arg_str); + pid = fork(); + if (pid == 0) { + setsid(); + reopen_fd_to_null(0); + reopen_fd_to_null(1); + reopen_fd_to_null(2); + + execl(hook, hook, arg_str, NULL); + _exit(EXIT_FAILURE); + } else if (pid < 0) { + error_setg_errno(errp, errno, "failed to create child process"); + return; + } + + ga_wait_child(pid, &status, &local_err); + if (local_err) { + error_propagate(errp, local_err); + return; + } + + if (!WIFEXITED(status)) { + error_setg(errp, "fsfreeze hook has terminated abnormally"); + return; + } + + status = WEXITSTATUS(status); + if (status) { + error_setg(errp, "fsfreeze hook has failed with status %d", status); + return; + } +} + +/* + * Return status of freeze/thaw + */ +GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **errp) +{ + if (ga_is_frozen(ga_state)) { + return GUEST_FSFREEZE_STATUS_FROZEN; + } + + return GUEST_FSFREEZE_STATUS_THAWED; +} + +int64_t qmp_guest_fsfreeze_freeze(Error **errp) +{ + return qmp_guest_fsfreeze_freeze_list(false, NULL, errp); +} + +int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints, + strList *mountpoints, + Error **errp) +{ + int ret; + FsMountList mounts; + Error *local_err = NULL; + + slog("guest-fsfreeze called"); + + execute_fsfreeze_hook(FSFREEZE_HOOK_FREEZE, &local_err); + if (local_err) { + error_propagate(errp, local_err); + return -1; + } + + QTAILQ_INIT(&mounts); + if (!build_fs_mount_list(&mounts, &local_err)) { + error_propagate(errp, local_err); + return -1; + } + + /* cannot risk guest agent blocking itself on a write in this state */ + ga_set_frozen(ga_state); + + ret = qmp_guest_fsfreeze_do_freeze_list(has_mountpoints, mountpoints, + mounts, errp); + + free_fs_mount_list(&mounts); + /* We may not issue any FIFREEZE here. + * Just unset ga_state here and ready for the next call. + */ + if (ret == 0) { + ga_unset_frozen(ga_state); + } else if (ret < 0) { + qmp_guest_fsfreeze_thaw(NULL); + } + return ret; +} + +int64_t qmp_guest_fsfreeze_thaw(Error **errp) +{ + int ret; + + ret = qmp_guest_fsfreeze_do_thaw(errp); + if (ret >= 0) { + ga_unset_frozen(ga_state); + execute_fsfreeze_hook(FSFREEZE_HOOK_THAW, errp); + } else { + ret = 0; + } + + return ret; +} + +static void guest_fsfreeze_cleanup(void) +{ + Error *err = NULL; + + if (ga_is_frozen(ga_state) == GUEST_FSFREEZE_STATUS_FROZEN) { + qmp_guest_fsfreeze_thaw(&err); + if (err) { + slog("failed to clean up frozen filesystems: %s", + error_get_pretty(err)); + error_free(err); + } + } +} +#endif + +/* linux-specific implementations. avoid this if at all possible. */ +#if defined(__linux__) #if defined(CONFIG_FSFREEZE) static char *get_pci_driver(char const *syspath, int pathlen, Error **errp) @@ -1467,153 +1606,6 @@ GuestFilesystemInfoList *qmp_guest_get_fsinfo(Error **errp) free_fs_mount_list(&mounts); return ret; } - - -typedef enum { - FSFREEZE_HOOK_THAW = 0, - FSFREEZE_HOOK_FREEZE, -} FsfreezeHookArg; - -static const char *fsfreeze_hook_arg_string[] = { - "thaw", - "freeze", -}; - -static void execute_fsfreeze_hook(FsfreezeHookArg arg, Error **errp) -{ - int status; - pid_t pid; - const char *hook; - const char *arg_str = fsfreeze_hook_arg_string[arg]; - Error *local_err = NULL; - - hook = ga_fsfreeze_hook(ga_state); - if (!hook) { - return; - } - if (access(hook, X_OK) != 0) { - error_setg_errno(errp, errno, "can't access fsfreeze hook '%s'", hook); - return; - } - - slog("executing fsfreeze hook with arg '%s'", arg_str); - pid = fork(); - if (pid == 0) { - setsid(); - reopen_fd_to_null(0); - reopen_fd_to_null(1); - reopen_fd_to_null(2); - - execl(hook, hook, arg_str, NULL); - _exit(EXIT_FAILURE); - } else if (pid < 0) { - error_setg_errno(errp, errno, "failed to create child process"); - return; - } - - ga_wait_child(pid, &status, &local_err); - if (local_err) { - error_propagate(errp, local_err); - return; - } - - if (!WIFEXITED(status)) { - error_setg(errp, "fsfreeze hook has terminated abnormally"); - return; - } - - status = WEXITSTATUS(status); - if (status) { - error_setg(errp, "fsfreeze hook has failed with status %d", status); - return; - } -} - -/* - * Return status of freeze/thaw - */ -GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **errp) -{ - if (ga_is_frozen(ga_state)) { - return GUEST_FSFREEZE_STATUS_FROZEN; - } - - return GUEST_FSFREEZE_STATUS_THAWED; -} - -int64_t qmp_guest_fsfreeze_freeze(Error **errp) -{ - return qmp_guest_fsfreeze_freeze_list(false, NULL, errp); -} - -int64_t qmp_guest_fsfreeze_freeze_list(bool has_mountpoints, - strList *mountpoints, - Error **errp) -{ - int ret; - FsMountList mounts; - Error *local_err = NULL; - - slog("guest-fsfreeze called"); - - execute_fsfreeze_hook(FSFREEZE_HOOK_FREEZE, &local_err); - if (local_err) { - error_propagate(errp, local_err); - return -1; - } - - QTAILQ_INIT(&mounts); - if (!build_fs_mount_list(&mounts, &local_err)) { - error_propagate(errp, local_err); - return -1; - } - - /* cannot risk guest agent blocking itself on a write in this state */ - ga_set_frozen(ga_state); - - ret = qmp_guest_fsfreeze_do_freeze_list(has_mountpoints, mountpoints, - mounts, errp); - - free_fs_mount_list(&mounts); - /* We may not issue any FIFREEZE here. - * Just unset ga_state here and ready for the next call. - */ - if (ret == 0) { - ga_unset_frozen(ga_state); - } else if (ret < 0) { - qmp_guest_fsfreeze_thaw(NULL); - } - return ret; -} - -int64_t qmp_guest_fsfreeze_thaw(Error **errp) -{ - int ret; - - ret = qmp_guest_fsfreeze_do_thaw(errp); - if (ret >= 0) { - ga_unset_frozen(ga_state); - execute_fsfreeze_hook(FSFREEZE_HOOK_THAW, errp); - } else { - ret = 0; - } - - return ret; -} - -static void guest_fsfreeze_cleanup(void) -{ - Error *err = NULL; - - if (ga_is_frozen(ga_state) == GUEST_FSFREEZE_STATUS_FROZEN) { - qmp_guest_fsfreeze_thaw(&err); - if (err) { - slog("failed to clean up frozen filesystems: %s", - error_get_pretty(err)); - error_free(err); - } - } -} #endif /* CONFIG_FSFREEZE */ #if defined(CONFIG_FSTRIM) diff --git a/qga/main.c b/qga/main.c index 0d27c97d38..b3580508fa 100644 --- a/qga/main.c +++ b/qga/main.c @@ -37,12 +37,7 @@ #include "qga/service-win32.h" #include "qga/vss-win32.h" #endif -#ifdef __linux__ -#include -#ifdef FIFREEZE -#define CONFIG_FSFREEZE -#endif -#endif +#include "commands-common.h" #ifndef _WIN32 #ifdef __FreeBSD__ diff --git a/qga/meson.build b/qga/meson.build index 932b4e7ca8..3cfb9166e5 100644 --- a/qga/meson.build +++ b/qga/meson.build @@ -75,6 +75,9 @@ qga_ss.add(when: 'CONFIG_POSIX', if_true: files( qga_ss.add(when: 'CONFIG_LINUX', if_true: files( 'commands-linux.c', )) +qga_ss.add(when: 'CONFIG_BSD', if_true: files( + 'commands-bsd.c', +)) qga_ss.add(when: 'CONFIG_WIN32', if_true: files( 'channel-win32.c', 'commands-win32.c',