From patchwork Fri Jun 3 08:48:58 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fam Zheng X-Patchwork-Id: 9152297 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 7CB9E6072B for ; Fri, 3 Jun 2016 09:00:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7075E28309 for ; Fri, 3 Jun 2016 09:00:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 63E4328327; Fri, 3 Jun 2016 09:00:45 +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 EF32E282E8 for ; Fri, 3 Jun 2016 09:00:44 +0000 (UTC) Received: from localhost ([::1]:53453 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b8kyC-0000TX-50 for patchwork-qemu-devel@patchwork.kernel.org; Fri, 03 Jun 2016 05:00:44 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54885) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b8knb-0006hO-AD for qemu-devel@nongnu.org; Fri, 03 Jun 2016 04:49:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b8knZ-0001q3-6u for qemu-devel@nongnu.org; Fri, 03 Jun 2016 04:49:46 -0400 Received: from mx1.redhat.com ([209.132.183.28]:60721) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b8knW-0001pF-CP; Fri, 03 Jun 2016 04:49:42 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id DF9A45C6; Fri, 3 Jun 2016 08:49:41 +0000 (UTC) Received: from ad.usersys.redhat.com (dhcp-15-133.nay.redhat.com [10.66.15.133]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u538nKXo023508; Fri, 3 Jun 2016 04:49:38 -0400 From: Fam Zheng To: qemu-devel@nongnu.org Date: Fri, 3 Jun 2016 16:48:58 +0800 Message-Id: <1464943756-14143-5-git-send-email-famz@redhat.com> In-Reply-To: <1464943756-14143-1-git-send-email-famz@redhat.com> References: <1464943756-14143-1-git-send-email-famz@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Fri, 03 Jun 2016 08:49:41 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v6 04/22] block: Introduce image file locking 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: Kevin Wolf , qemu-block@nongnu.org, rjones@redhat.com, John Snow , Jeff Cody , Markus Armbruster , Max Reitz , stefanha@redhat.com, den@openvz.org, pbonzini@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Block drivers can implement this new operation .bdrv_lockf to actually lock the image in the protocol specific way. Signed-off-by: Fam Zheng --- block.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++ include/block/block.h | 11 ++++++++- include/block/block_int.h | 5 +++++ 3 files changed, 72 insertions(+), 1 deletion(-) diff --git a/block.c b/block.c index 736432f..4c2a3ff 100644 --- a/block.c +++ b/block.c @@ -854,6 +854,50 @@ out: g_free(gen_node_name); } +BdrvLockfCmd bdrv_get_locking_cmd(int flags) +{ + if (flags & BDRV_O_NO_LOCK) { + return BDRV_LOCKF_UNLOCK; + } else if (flags & BDRV_O_SHARED_LOCK) { + return BDRV_LOCKF_SHARED; + } else if (flags & BDRV_O_RDWR) { + return BDRV_LOCKF_EXCLUSIVE; + } else { + return BDRV_LOCKF_SHARED; + } +} + +static int bdrv_lock_unlock_image_do(BlockDriverState *bs, BdrvLockfCmd cmd) +{ + int ret; + + if (bs->cur_lock == cmd) { + return 0; + } else if (!bs->drv) { + return -ENOMEDIUM; + } else if (!bs->drv->bdrv_lockf) { + return 0; + } + ret = bs->drv->bdrv_lockf(bs, cmd); + if (ret == -ENOTSUP) { + /* Handle it the same way as !bs->drv->bdrv_lockf */ + ret = 0; + } else if (ret == 0) { + bs->cur_lock = cmd; + } + return ret; +} + +static int bdrv_lock_image(BlockDriverState *bs) +{ + return bdrv_lock_unlock_image_do(bs, bdrv_get_locking_cmd(bs->open_flags)); +} + +static int bdrv_unlock_image(BlockDriverState *bs) +{ + return bdrv_lock_unlock_image_do(bs, BDRV_LOCKF_UNLOCK); +} + static QemuOptsList bdrv_runtime_opts = { .name = "bdrv_common", .head = QTAILQ_HEAD_INITIALIZER(bdrv_runtime_opts.head), @@ -1003,6 +1047,14 @@ static int bdrv_open_common(BlockDriverState *bs, BdrvChild *file, goto free_and_fail; } + if (!(open_flags & (BDRV_O_NO_LOCK | BDRV_O_INACTIVE))) { + ret = bdrv_lock_image(bs); + if (ret) { + error_setg(errp, "Failed to lock image"); + goto free_and_fail; + } + } + ret = refresh_total_sectors(bs, bs->total_sectors); if (ret < 0) { error_setg_errno(errp, -ret, "Could not refresh total sector count"); @@ -2164,6 +2216,7 @@ static void bdrv_close(BlockDriverState *bs) if (bs->drv) { BdrvChild *child, *next; + bdrv_unlock_image(bs); bs->drv->bdrv_close(bs); bs->drv = NULL; @@ -3187,6 +3240,9 @@ void bdrv_invalidate_cache(BlockDriverState *bs, Error **errp) error_setg_errno(errp, -ret, "Could not refresh total sector count"); return; } + if (!(bs->open_flags & BDRV_O_NO_LOCK)) { + bdrv_lock_image(bs); + } } void bdrv_invalidate_cache_all(Error **errp) @@ -3229,6 +3285,7 @@ static int bdrv_inactivate_recurse(BlockDriverState *bs, } if (setting_flag) { + ret = bdrv_unlock_image(bs); bs->open_flags |= BDRV_O_INACTIVE; } return 0; diff --git a/include/block/block.h b/include/block/block.h index 02b598b..782ef2f 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -183,6 +183,15 @@ typedef enum BlockOpType { BLOCK_OP_TYPE_MAX, } BlockOpType; +typedef enum { + /* The values are ordered so that lower number implies higher restriction. + * Starting from 1 to make 0 an invalid value. + * */ + BDRV_LOCKF_EXCLUSIVE = 1, + BDRV_LOCKF_SHARED, + BDRV_LOCKF_UNLOCK, +} BdrvLockfCmd; + void bdrv_info_print(Monitor *mon, const QObject *data); void bdrv_info(Monitor *mon, QObject **ret_data); void bdrv_stats_print(Monitor *mon, const QObject *data); @@ -274,7 +283,7 @@ int bdrv_drop_intermediate(BlockDriverState *active, BlockDriverState *top, BlockDriverState *bdrv_find_overlay(BlockDriverState *active, BlockDriverState *bs); BlockDriverState *bdrv_find_base(BlockDriverState *bs); - +BdrvLockfCmd bdrv_get_locking_cmd(int flags); typedef struct BdrvCheckResult { int corruptions; diff --git a/include/block/block_int.h b/include/block/block_int.h index 30a9717..29cc632 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -317,6 +317,10 @@ struct BlockDriver { Error **errp); void (*bdrv_del_child)(BlockDriverState *parent, BdrvChild *child, Error **errp); + /** + * Lock/unlock the image. + */ + int (*bdrv_lockf)(BlockDriverState *bs, BdrvLockfCmd cmd); QLIST_ENTRY(BlockDriver) list; }; @@ -500,6 +504,7 @@ struct BlockDriverState { unsigned io_plug_disabled; int quiesce_counter; + BdrvLockfCmd cur_lock; }; struct BlockBackendRootState {