From patchwork Wed Sep 18 06:35:51 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guangliang Zhao X-Patchwork-Id: 2904811 Return-Path: X-Original-To: patchwork-ceph-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 8858E9F1E1 for ; Wed, 18 Sep 2013 06:36:10 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 3497F20114 for ; Wed, 18 Sep 2013 06:36:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id CAB3F20111 for ; Wed, 18 Sep 2013 06:36:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751854Ab3IRGgG (ORCPT ); Wed, 18 Sep 2013 02:36:06 -0400 Received: from mail-pa0-f49.google.com ([209.85.220.49]:51583 "EHLO mail-pa0-f49.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751738Ab3IRGgF (ORCPT ); Wed, 18 Sep 2013 02:36:05 -0400 Received: by mail-pa0-f49.google.com with SMTP id ld10so7848701pab.22 for ; Tue, 17 Sep 2013 23:36:04 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=EwvTL13F/wfNJBcSck4buPA28hp9lnSQcWrZ4u9msWc=; b=Rpo5/+BwIqI4HYlqJDV0TNtm211K3mJlNKeAp7OeYSsGnoQyYZDdC0qE6P5xgNd4fi nUsUhHADOShWuWUPwGglf0wgP0wsFid0wW0eXdU1jzvN5n504/qIyZDog8drdq4aHs6v n3aVPhIbIklBbiNxDS6R1pBWGYCZaas3ECG9EDWruv5ssYuNUVa5FC3INbY6Nj5cTGCH I/ynP0kJQ94wpOgUkKH0/qbxF+mYZ7uZAsRQ831ibDAKyK4GrDlfdB3RejDVZKY0fMEc 7ZBckMVUSpEj+rHOM5OxJd4zZ3oGYtnTScafPbGFww+6o1Em4v8O944yed+se24+CM3c AZCQ== X-Gm-Message-State: ALoCoQnJJlL5fEB2MvENYwHp9vNNxUV9pt35tz8QeQSOsvRj410uo2tN5BBHq8q+5AiFwG9t6mQn X-Received: by 10.68.244.200 with SMTP id xi8mr12699809pbc.156.1379486163865; Tue, 17 Sep 2013 23:36:03 -0700 (PDT) Received: from localhost ([211.155.113.174]) by mx.google.com with ESMTPSA id gh9sm62332pbc.40.1969.12.31.16.00.00 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Tue, 17 Sep 2013 23:36:03 -0700 (PDT) From: Guangliang Zhao To: ceph-devel@vger.kernel.org Cc: sage@inktank.com, josh.durgin@inktank.com, alex.elder@linaro.org, lucienchao@gmail.com Subject: [PATCH v3] rbd: add ioctl for rbd Date: Wed, 18 Sep 2013 14:35:51 +0800 Message-Id: <1379486151-8939-1-git-send-email-guangliang@unitedstack.com> X-Mailer: git-send-email 1.7.9.5 Sender: ceph-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: ceph-devel@vger.kernel.org X-Spam-Status: No, score=-7.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham 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 When running the following commands: [root@ceph0 mnt]# blockdev --setro /dev/rbd1 [root@ceph0 mnt]# blockdev --getro /dev/rbd1 0 The block setro didn't take effect, it is because the rbd doesn't support ioctl of block driver. The results with this patch: root@ceph-client01:~# rbd map img root@ceph-client01:~# blockdev --getro /dev/rbd1 0 root@ceph-client01:~# cat /sys/block/rbd1/ro 0 root@ceph-client01:~# blockdev --setro /dev/rbd1 root@ceph-client01:~# blockdev --getro /dev/rbd1 1 root@ceph-client01:~# cat /sys/block/rbd1/ro 1 root@ceph-client01:~# dd if=/dev/zero of=/dev/rbd1 count=1 dd: opening `/dev/rbd1': Read-only file system` root@ceph-client01:~# blockdev --setrw /dev/rbd1 root@ceph-client01:~# blockdev --getro /dev/rbd1 0 root@ceph-client01:~# cat /sys/block/rbd1/ro 0 root@ceph-client01:~# dd if=/dev/zero of=/dev/rbd1 count=1 1+0 records in 1+0 records out 512 bytes (512 B) copied, 0.14989 s, 3.4 kB/s This resolves: http://tracker.ceph.com/issues/6265 Signed-off-by: Guangliang Zhao Reviewed-by: Alex Elder Reviewed-by: Josh Durgin --- drivers/block/rbd.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/drivers/block/rbd.c b/drivers/block/rbd.c index 2f00778..fea826d 100644 --- a/drivers/block/rbd.c +++ b/drivers/block/rbd.c @@ -508,10 +508,69 @@ static void rbd_release(struct gendisk *disk, fmode_t mode) put_device(&rbd_dev->dev); } +static int rbd_ioctl_set_ro(struct rbd_device *rbd_dev, unsigned long arg) +{ + int val; + bool ro; + + if (get_user(val, (int __user *)(arg))) + return -EFAULT; + + ro = val ? true : false; + /* Snapshot doesn't allow to write*/ + if (rbd_dev->spec->snap_id != CEPH_NOSNAP && !ro) + return -EROFS; + + if (rbd_dev->mapping.read_only != ro) { + rbd_dev->mapping.read_only = ro; + set_disk_ro(rbd_dev->disk, ro ? 1 : 0); + } + + return 0; +} + +static int rbd_ioctl(struct block_device *bdev, fmode_t mode, + unsigned int cmd, unsigned long arg) +{ + struct rbd_device *rbd_dev = bdev->bd_disk->private_data; + int ret = 0; + + spin_lock_irq(&rbd_dev->lock); + /* prevent others open this device */ + if (rbd_dev->open_count > 1) { + ret = -EBUSY; + goto out; + } + + switch (cmd) { + case BLKROSET: + ret = rbd_ioctl_set_ro(rbd_dev, arg); + break; + default: + ret = -ENOTTY; + } + +out: + spin_unlock_irq(&rbd_dev->lock); + return ret; +} + +#ifdef CONFIG_COMPAT +static int rbd_compat_ioctl(struct block_device *bdev, fmode_t mode, + unsigned int cmd, unsigned long arg) +{ + return rbd_ioctl(bdev, mode, cmd, arg); +} +#endif /* CONFIG_COMPAT */ + static const struct block_device_operations rbd_bd_ops = { .owner = THIS_MODULE, .open = rbd_open, .release = rbd_release, + .ioctl = rbd_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl = rbd_compat_ioctl, +#endif }; /*