From patchwork Mon Nov 19 23:42:49 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Elder X-Patchwork-Id: 1769671 Return-Path: X-Original-To: patchwork-ceph-devel@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork2.kernel.org (Postfix) with ESMTP id BC32EDF264 for ; Mon, 19 Nov 2012 23:42:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753215Ab2KSXmx (ORCPT ); Mon, 19 Nov 2012 18:42:53 -0500 Received: from mail-ie0-f174.google.com ([209.85.223.174]:41779 "EHLO mail-ie0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753203Ab2KSXmw (ORCPT ); Mon, 19 Nov 2012 18:42:52 -0500 Received: by mail-ie0-f174.google.com with SMTP id k13so7729715iea.19 for ; Mon, 19 Nov 2012 15:42:52 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding :x-gm-message-state; bh=46v2YOXKva3Q3/o4Jg0iWxFz4RvpgXzailyWluhvE6k=; b=c140x6i8qEvOLijsW7k4PcDAZbClBvMnD1LhNIhOyUiY2D+OIWf/lbChQ1itdclE34 38VtwekYbZvcCkPoI/gLcK/ywXmHQ98sUMxmwWqVqJPB3b7+xzdJROo8OPWfhDUf+6wG PF6zGDd3IU04J+bQ9fFzOjC7snNe9uYcRbWDZWkJoptwXjIeBBnwhkc4vlaNccHNDuu4 MbOS5a5LjnMMIsMzNGvcGN6OgTtN8n6ttu2nKPTG4Za2ZSxzpCkYIAs/YyWrrjc6wE9k q+6k8fao3WNtimV4Vvhy1LyG2LGrlRxuiRxzoNA1ufjnzSqWVq34jQqcQZhVO3rhS2Tf m4nA== Received: by 10.50.33.194 with SMTP id t2mr8269686igi.69.1353368572019; Mon, 19 Nov 2012 15:42:52 -0800 (PST) Received: from [172.22.22.4] (c-71-195-31-37.hsd1.mn.comcast.net. [71.195.31.37]) by mx.google.com with ESMTPS id ex10sm8965572igc.15.2012.11.19.15.42.50 (version=SSLv3 cipher=OTHER); Mon, 19 Nov 2012 15:42:50 -0800 (PST) Message-ID: <50AAC3F9.1040809@inktank.com> Date: Mon, 19 Nov 2012 17:42:49 -0600 From: Alex Elder User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121028 Thunderbird/16.0.2 MIME-Version: 1.0 To: ceph-devel@vger.kernel.org Subject: [PATCH, v2] rbd: do not allow remove of mounted-on image References: <50A65F15.6080807@inktank.com> In-Reply-To: <50A65F15.6080807@inktank.com> X-Gm-Message-State: ALoCoQnSacRaE7zxNfrkla02Zh2VR9aPu7ibPLeBy2ioqI9TzFSQhG2ncxt/dwLdLxQk83X9HMY7 Sender: ceph-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: ceph-devel@vger.kernel.org There is no check in rbd_remove() to see if anybody holds open the image being removed. That's not cool. Add a simple open count that goes up and down with opens and closes (releases) of the device, and don't allow an rbd image to be removed if the count is non-zero. Protect the updates of the open count value with ctl_mutex to ensure the underlying rbd device doesn't get removed while concurrently being opened. Signed-off-by: Alex Elder --- v2: added ctl_mutex locking for rbd_open() and rbd_release() drivers/block/rbd.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) -- To unsubscribe from this list: send the line "unsubscribe ceph-devel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Index: b/drivers/block/rbd.c =================================================================== --- a/drivers/block/rbd.c +++ b/drivers/block/rbd.c @@ -255,6 +255,7 @@ struct rbd_device { /* sysfs related */ struct device dev; + unsigned long open_count; }; static DEFINE_MUTEX(ctl_mutex); /* Serialize open/close/setup/teardown */ @@ -356,8 +357,11 @@ static int rbd_open(struct block_device if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only) return -EROFS; + mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); rbd_get_dev(rbd_dev); set_device_ro(bdev, rbd_dev->mapping.read_only); + rbd_dev->open_count++; + mutex_unlock(&ctl_mutex); return 0; } @@ -366,7 +370,11 @@ static int rbd_release(struct gendisk *d { struct rbd_device *rbd_dev = disk->private_data; + mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING); + rbd_assert(rbd_dev->open_count > 0); + rbd_dev->open_count--; rbd_put_dev(rbd_dev); + mutex_unlock(&ctl_mutex); return 0; } @@ -3764,6 +3772,11 @@ static ssize_t rbd_remove(struct bus_typ goto done; } + if (rbd_dev->open_count) { + ret = -EBUSY; + goto done; + } + rbd_remove_all_snaps(rbd_dev); rbd_bus_del_dev(rbd_dev);