From patchwork Wed Apr 13 09:09:53 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fam Zheng X-Patchwork-Id: 8819821 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 53D53C0553 for ; Wed, 13 Apr 2016 09:16:58 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id AFE8E20270 for ; Wed, 13 Apr 2016 09:16:57 +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 F0EA8202D1 for ; Wed, 13 Apr 2016 09:16:56 +0000 (UTC) Received: from localhost ([::1]:39601 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aqGuu-0003v6-6Q for patchwork-qemu-devel@patchwork.kernel.org; Wed, 13 Apr 2016 05:16:56 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48320) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aqGod-0006WP-EC for qemu-devel@nongnu.org; Wed, 13 Apr 2016 05:10:28 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aqGoZ-0001n1-AF for qemu-devel@nongnu.org; Wed, 13 Apr 2016 05:10:27 -0400 Received: from mx1.redhat.com ([209.132.183.28]:54953) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aqGoU-0001li-Af; Wed, 13 Apr 2016 05:10:18 -0400 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) (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 0C88A8111F; Wed, 13 Apr 2016 09:10:18 +0000 (UTC) Received: from lemon.nay.redhat.com (dhcp-14-170.nay.redhat.com [10.66.14.170]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u3D9A07Z003879; Wed, 13 Apr 2016 05:10:14 -0400 From: Fam Zheng To: qemu-devel@nongnu.org Date: Wed, 13 Apr 2016 17:09:53 +0800 Message-Id: <1460538604-12132-5-git-send-email-famz@redhat.com> In-Reply-To: <1460538604-12132-1-git-send-email-famz@redhat.com> References: <1460538604-12132-1-git-send-email-famz@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 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 for-2.7 04/15] block: Introduce image file locking interface 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 , 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 | 25 +++++++++++++++++++++++++ include/block/block.h | 8 ++++++++ include/block/block_int.h | 5 +++++ 3 files changed, 38 insertions(+) diff --git a/block.c b/block.c index d4939b4..7f6e903 100644 --- a/block.c +++ b/block.c @@ -995,6 +995,15 @@ static int bdrv_open_common(BlockDriverState *bs, BdrvChild *file, goto free_and_fail; } + if (!(open_flags & BDRV_O_NO_LOCK)) { + BdrvLockfCmd cmd = open_flags & BDRV_O_RDWR ? BDRV_LOCKF_RWLOCK : + BDRV_LOCKF_ROLOCK; + ret = bdrv_lockf(bs, cmd, errp); + if (ret != 0 && ret != -ENOTSUP) { + 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"); @@ -2141,6 +2150,10 @@ static void bdrv_close(BlockDriverState *bs) blk_dev_change_media_cb(bs->blk, false); } + if (bs->drv && !(bs->open_flags & BDRV_O_NO_LOCK)) { + bdrv_lockf(bs, BDRV_LOCKF_UNLOCK, &error_abort); + } + if (bs->drv) { BdrvChild *child, *next; @@ -3981,3 +3994,15 @@ void bdrv_refresh_filename(BlockDriverState *bs) QDECREF(json); } } + +int bdrv_lockf(BlockDriverState *bs, BdrvLockfCmd cmd, Error **errp) +{ + if (!bs->drv) { + error_setg(errp, "No medium to lock"); + return -ENOMEDIUM; + } else if (!bs->drv->bdrv_lockf) { + return -ENOTSUP; + } else { + return bs->drv->bdrv_lockf(bs, cmd, errp); + } +} diff --git a/include/block/block.h b/include/block/block.h index b803597..36e1271 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -542,4 +542,12 @@ void bdrv_drained_begin(BlockDriverState *bs); */ void bdrv_drained_end(BlockDriverState *bs); +typedef enum { + BDRV_LOCKF_RWLOCK, + BDRV_LOCKF_ROLOCK, + BDRV_LOCKF_UNLOCK, +} BdrvLockfCmd; + +int bdrv_lockf(BlockDriverState *bs, BdrvLockfCmd cmd, Error **errp); + #endif diff --git a/include/block/block_int.h b/include/block/block_int.h index 10d8759..0a79eba 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -317,6 +317,11 @@ struct BlockDriver { */ void (*bdrv_drain)(BlockDriverState *bs); + /** + * Lock/unlock the image. + */ + int (*bdrv_lockf)(BlockDriverState *bs, BdrvLockfCmd cmd, Error **errp); + QLIST_ENTRY(BlockDriver) list; };