From patchwork Thu Apr 28 12:57:23 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fam Zheng X-Patchwork-Id: 8969801 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id D6B0BBF29F for ; Thu, 28 Apr 2016 13:02:43 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 27E70202AE for ; Thu, 28 Apr 2016 13:02:42 +0000 (UTC) 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.kernel.org (Postfix) with ESMTPS id 62CFF20295 for ; Thu, 28 Apr 2016 13:02:41 +0000 (UTC) Received: from localhost ([::1]:48780 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1avlaa-0002Eq-Me for patchwork-qemu-devel@patchwork.kernel.org; Thu, 28 Apr 2016 09:02:40 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60681) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1avlWG-00019y-0m for qemu-devel@nongnu.org; Thu, 28 Apr 2016 08:58:15 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1avlWA-0000GR-KO for qemu-devel@nongnu.org; Thu, 28 Apr 2016 08:58:11 -0400 Received: from mx1.redhat.com ([209.132.183.28]:55296) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1avlW5-0000Ee-33; Thu, 28 Apr 2016 08:58:01 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (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 9E419C062CE7; Thu, 28 Apr 2016 12:58:00 +0000 (UTC) Received: from lemon.redhat.com (vpn1-5-154.pek2.redhat.com [10.72.5.154]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u3SCvWN7001193; Thu, 28 Apr 2016 08:57:56 -0400 From: Fam Zheng To: qemu-devel@nongnu.org Date: Thu, 28 Apr 2016 20:57:23 +0800 Message-Id: <1461848266-32119-5-git-send-email-famz@redhat.com> In-Reply-To: <1461848266-32119-1-git-send-email-famz@redhat.com> References: <1461848266-32119-1-git-send-email-famz@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 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 v3 04/27] 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, Jeff Cody , Markus Armbruster , Max Reitz , stefanha@redhat.com, den@openvz.org, pbonzini@redhat.com, John Snow Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org 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 | 47 +++++++++++++++++++++++++++++++++++++++++++++++ include/block/block_int.h | 12 ++++++++++++ 2 files changed, 59 insertions(+) diff --git a/block.c b/block.c index 9a84ed1..6058d7c 100644 --- a/block.c +++ b/block.c @@ -846,6 +846,40 @@ out: g_free(gen_node_name); } +static int bdrv_lock_unlock_image_do(BlockDriverState *bs, bool lock_image) +{ + int cmd = BDRV_LOCKF_UNLOCK; + int ret; + + if (bs->image_locked == lock_image) { + return 0; + } else if (!bs->drv) { + return -ENOMEDIUM; + } else if (!bs->drv->bdrv_lockf) { + return 0; + } + if (lock_image) { + cmd = bs->open_flags & BDRV_O_RDWR ? BDRV_LOCKF_RWLOCK : + BDRV_LOCKF_ROLOCK; + } + ret = bs->drv->bdrv_lockf(bs, cmd); + if (ret == -ENOTSUP) { + /* Handle it the same way as !bs->drv->bdrv_lockf */ + ret = 0; + } + return ret; +} + +static int bdrv_lock_image(BlockDriverState *bs) +{ + return bdrv_lock_unlock_image_do(bs, true); +} + +static int bdrv_unlock_image(BlockDriverState *bs) +{ + return bdrv_lock_unlock_image_do(bs, false); +} + static QemuOptsList bdrv_runtime_opts = { .name = "bdrv_common", .head = QTAILQ_HEAD_INITIALIZER(bdrv_runtime_opts.head), @@ -995,6 +1029,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"); @@ -2144,6 +2186,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; @@ -3238,6 +3281,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) @@ -3277,6 +3323,7 @@ static int bdrv_inactivate(BlockDriverState *bs) } } + ret = bdrv_unlock_image(bs); bs->open_flags |= BDRV_O_INACTIVE; return 0; } diff --git a/include/block/block_int.h b/include/block/block_int.h index 10d8759..ffa30b0 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -85,6 +85,12 @@ typedef struct BdrvTrackedRequest { struct BdrvTrackedRequest *waiting_for; } BdrvTrackedRequest; +typedef enum { + BDRV_LOCKF_RWLOCK, + BDRV_LOCKF_ROLOCK, + BDRV_LOCKF_UNLOCK, +} BdrvLockfCmd; + struct BlockDriver { const char *format_name; int instance_size; @@ -317,6 +323,11 @@ struct BlockDriver { */ void (*bdrv_drain)(BlockDriverState *bs); + /** + * Lock/unlock the image. + */ + int (*bdrv_lockf)(BlockDriverState *bs, BdrvLockfCmd cmd); + QLIST_ENTRY(BlockDriver) list; }; @@ -485,6 +496,7 @@ struct BlockDriverState { NotifierWithReturn write_threshold_notifier; int quiesce_counter; + bool image_locked; }; struct BlockBackendRootState {