From patchwork Tue Feb 1 10:58:47 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jes Sorensen X-Patchwork-Id: 81299 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id AF88AB7109 for ; Tue, 1 Feb 2011 22:00:50 +1100 (EST) Received: from localhost ([127.0.0.1]:52461 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PkDyZ-0002Ga-4S for incoming@patchwork.ozlabs.org; Tue, 01 Feb 2011 06:00:47 -0500 Received: from [140.186.70.92] (port=42501 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PkDwl-0002Dp-Sf for qemu-devel@nongnu.org; Tue, 01 Feb 2011 05:58:58 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PkDwj-00066c-SK for qemu-devel@nongnu.org; Tue, 01 Feb 2011 05:58:55 -0500 Received: from mx1.redhat.com ([209.132.183.28]:54556) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PkDwj-00066U-Gw for qemu-devel@nongnu.org; Tue, 01 Feb 2011 05:58:53 -0500 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id p11Awp8o024786 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Tue, 1 Feb 2011 05:58:51 -0500 Received: from red-feather.redhat.com (ovpn-113-98.phx2.redhat.com [10.3.113.98]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id p11AwmEM025980; Tue, 1 Feb 2011 05:58:50 -0500 From: Jes.Sorensen@redhat.com To: qemu-devel@nongnu.org Date: Tue, 1 Feb 2011 11:58:47 +0100 Message-Id: <1296557928-30019-2-git-send-email-Jes.Sorensen@redhat.com> In-Reply-To: <1296557928-30019-1-git-send-email-Jes.Sorensen@redhat.com> References: <1296557928-30019-1-git-send-email-Jes.Sorensen@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.132.183.28 Cc: mdroth@linux.vnet.ibm.com, agl@us.ibm.com Subject: [Qemu-devel] [PATCH 1/2] Add virtagent file system freeze/thaw X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Jes Sorensen Implement freeze/thaw support in the guest, allowing the host to request the guest freezes all it's file systems before a live snapshot is performed. - fsfreeze(): Walk the list of mounted local real file systems, and freeze them. - fsthaw(): Walk the list of previously frozen file systems and thaw them. - fsstatus(): Return the current status of freeze/thaw. The host must poll this function, in case fsfreeze() returned with a timeout, to wait for the operation to finish. Signed-off-by: Jes Sorensen --- virtagent-common.h | 8 ++ virtagent-server.c | 196 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 204 insertions(+), 0 deletions(-) diff --git a/virtagent-common.h b/virtagent-common.h index 5d8f5c1..220a4b6 100644 --- a/virtagent-common.h +++ b/virtagent-common.h @@ -61,6 +61,14 @@ typedef struct VAContext { const char *channel_path; } VAContext; +enum vs_fsfreeze_status { + FREEZE_ERROR = -1, + FREEZE_THAWED = 0, + FREEZE_INPROGRESS = 1, + FREEZE_FROZEN = 2, + FREEZE_THAWINPROGRESS = 3, +}; + enum va_job_status { VA_JOB_STATUS_PENDING = 0, VA_JOB_STATUS_OK, diff --git a/virtagent-server.c b/virtagent-server.c index 7bb35b2..cf2a3f0 100644 --- a/virtagent-server.c +++ b/virtagent-server.c @@ -14,6 +14,13 @@ #include #include "qemu_socket.h" #include "virtagent-common.h" +#include +#include +#include +#include +#include +#include +#include static VAServerData *va_server_data; static bool va_enable_syslog = false; /* enable syslog'ing of RPCs */ @@ -217,6 +224,189 @@ static xmlrpc_value *va_hello(xmlrpc_env *env, return result; } + +/* + * Walk the mount table and build a list of local file systems + */ + +struct direntry { + char *dirname; + char *devtype; + struct direntry *next; +}; + +static struct direntry *mount_list; +static int fsfreeze_status; + +static int build_mount_list(void) +{ + struct mntent *mnt; + struct direntry *entry; + struct direntry *next; + char const *mtab = MOUNTED; + FILE *fp; + + fp = setmntent(mtab, "r"); + if (!fp) { + fprintf(stderr, "unable to read mtab\n"); + goto fail; + } + + while ((mnt = 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 ((mnt->mnt_fsname[0] != '/') || + (strcmp(mnt->mnt_type, "smbfs") == 0) || + (strcmp(mnt->mnt_type, "cifs") == 0)) { + continue; + } + + entry = qemu_malloc(sizeof(struct direntry)); + if (!entry) { + goto fail; + } + entry->dirname = qemu_strdup(mnt->mnt_dir); + entry->devtype = qemu_strdup(mnt->mnt_type); + entry->next = mount_list; + + mount_list = entry; + } + + endmntent(fp); + + return 0; + +fail: + while(mount_list) { + next = mount_list->next; + qemu_free(mount_list->dirname); + qemu_free(mount_list->devtype); + qemu_free(mount_list); + mount_list = next; + } + + return -1; +} + +/* + * va_fsfreeze(): Walk list of mounted file systems in the guest, and + * freeze the ones which are real local file systems. + * rpc return values: Number of file systems frozen, -1 on error. + */ +static xmlrpc_value *va_fsfreeze(xmlrpc_env *env, + xmlrpc_value *params, + void *user_data) +{ + xmlrpc_int32 ret = 0, i = 0; + xmlrpc_value *result; + struct direntry *entry; + int fd; + SLOG("va_fsfreeze()"); + + if (fsfreeze_status == FREEZE_FROZEN) { + ret = 0; + goto out; + } + + ret = build_mount_list(); + if (ret < 0) { + goto out; + } + + fsfreeze_status = FREEZE_INPROGRESS; + + entry = mount_list; + while(entry) { + fd = qemu_open(entry->dirname, O_RDONLY); + if (fd == -1) { + ret = errno; + goto error; + } + ret = ioctl(fd, FIFREEZE); + if (ret < 0 && ret != EOPNOTSUPP) { + goto error; + } + + close(fd); + entry = entry->next; + i++; + } + + fsfreeze_status = FREEZE_FROZEN; + ret = i; +out: + result = xmlrpc_build_value(env, "i", ret); + return result; +error: + if (i > 0) { + fsfreeze_status = FREEZE_ERROR; + } + goto out; +} + +/* + * va_fsthaw(): Walk list of frozen file systems in the guest, and + * thaw them. + * rpc return values: Number of file systems thawed on success, -1 on error. + */ +static xmlrpc_value *va_fsthaw(xmlrpc_env *env, + xmlrpc_value *params, + void *user_data) +{ + xmlrpc_int32 ret; + xmlrpc_value *result; + struct direntry *entry; + int fd, i = 0; + SLOG("va_fsthaw()"); + + if (fsfreeze_status == FREEZE_THAWED) { + ret = 0; + goto out; + } + + while((entry = mount_list)) { + fd = qemu_open(entry->dirname, O_RDONLY); + if (fd == -1) { + ret = -1; + goto out; + } + ret = ioctl(fd, FITHAW); + if (ret < 0 && ret != EOPNOTSUPP) { + ret = -1; + goto out; + } + close(fd); + + mount_list = entry->next; + qemu_free(entry->dirname); + qemu_free(entry->devtype); + qemu_free(entry); + i++; + } + + fsfreeze_status = FREEZE_THAWED; + ret = i; +out: + result = xmlrpc_build_value(env, "i", ret); + return result; +} + +/* va_fsstatus(): Return status of freeze/thaw + * rpc return values: fsfreeze_status + */ +static xmlrpc_value *va_fsstatus(xmlrpc_env *env, + xmlrpc_value *params, + void *user_data) +{ + xmlrpc_value *result = xmlrpc_build_value(env, "i", fsfreeze_status); + SLOG("va_fsstatus()"); + return result; +} + typedef struct RPCFunction { xmlrpc_value *(*func)(xmlrpc_env *env, xmlrpc_value *param, void *unused); const char *func_name; @@ -237,6 +427,12 @@ static RPCFunction guest_functions[] = { .func_name = "va.ping" }, { .func = va_capabilities, .func_name = "va.capabilities" }, + { .func = va_fsfreeze, + .func_name = "va.fsfreeze" }, + { .func = va_fsthaw, + .func_name = "va.fsthaw" }, + { .func = va_fsstatus, + .func_name = "va.fsstatus" }, { NULL, NULL } }; static RPCFunction host_functions[] = {