From: Pranay Srivastava <pranjas@gmail.com>
To: Markus Pargmann <mpa@pengutronix.de>
Cc: nbd-general@lists.sourceforge.net, linux-kernel@vger.kernel.org,
Wouter Verhelst <w@uter.be>
Subject: Re: [PATCH 1/2] nbd: make nbd device wait for its users
Date: Fri, 24 Jun 2016 15:09:50 +0530 [thread overview]
Message-ID: <CA+aCy1G9Z-bhdEpWxNpwxFSPxXOPqLW+fPV+Q44DGDib9KDeTA@mail.gmail.com> (raw)
In-Reply-To: <1466760574-1916-1-git-send-email-mpa@pengutronix.de>
Hey Markus,
On Fri, Jun 24, 2016 at 2:59 PM, Markus Pargmann <mpa@pengutronix.de> wrote:
> From: "Pranay Kr. Srivastava" <pranjas@gmail.com>
>
> When a timeout occurs or a recv fails, then instead of abruplty killing
> nbd block device wait for it's users to finish.
>
> This is more required when filesystem(s) like ext2 or ext3 don't expect
> their buffer heads to disappear while the filesystem is mounted.
>
> Each open is counted. The blockdevice is kept open until the last user
> closes the block device. This offers the possibility as well to open a
> new socket to be used while the filesystems are mounted.
>
> Signed-off-by: Pranay Kr. Srivastava <pranjas@gmail.com>
>
> [mpa: Keep the blockdevice open until all users left]
> Signed-off-by: Markus Pargmann <mpa@pengutronix.de>
> ---
> Hi,
>
> I used your patch and changed it a bit based on my ideas. The general
> difference is that this keeps the block device open. After all users left, the
> device is reset.
>
> The followup patch then restricts access to ioctls after a disconnect. I wanted
> to avoid that anyone sets up anything new without all the old users leaving.
>
> Please let me know what you think about this.
>
> Best Regards,
>
> Markus
>
> drivers/block/nbd.c | 62 ++++++++++++++++++++++++++++++++++++++---------------
> 1 file changed, 45 insertions(+), 17 deletions(-)
>
> diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
> index 1efc26bf1d21..620660f3ff0f 100644
> --- a/drivers/block/nbd.c
> +++ b/drivers/block/nbd.c
> @@ -69,6 +69,8 @@ struct nbd_device {
> #if IS_ENABLED(CONFIG_DEBUG_FS)
> struct dentry *dbg_dir;
> #endif
> + atomic_t users; /* Users that opened the block device */
> + struct block_device *bdev;
> };
>
> #if IS_ENABLED(CONFIG_DEBUG_FS)
> @@ -655,9 +657,26 @@ static int nbd_set_socket(struct nbd_device *nbd, struct socket *sock)
> return ret;
> }
>
> +static void nbd_bdev_reset(struct block_device *bdev)
> +{
> + set_device_ro(bdev, false);
> + bdev->bd_inode->i_size = 0;
> + if (max_part > 0) {
> + blkdev_reread_part(bdev);
> + bdev->bd_invalidated = 1;
> + }
> +}
> +
> /* Reset all properties of an NBD device */
> static void nbd_reset(struct nbd_device *nbd)
> {
> + sock_shutdown(nbd);
> + nbd_clear_que(nbd);
> + if (nbd->bdev) {
> + kill_bdev(nbd->bdev);
> + nbd_bdev_reset(nbd->bdev);
> + }
> +
> nbd->disconnect = false;
> nbd->timedout = false;
> nbd->blksize = 1024;
> @@ -669,16 +688,6 @@ static void nbd_reset(struct nbd_device *nbd)
> del_timer_sync(&nbd->timeout_timer);
> }
>
> -static void nbd_bdev_reset(struct block_device *bdev)
> -{
> - set_device_ro(bdev, false);
> - bdev->bd_inode->i_size = 0;
> - if (max_part > 0) {
> - blkdev_reread_part(bdev);
> - bdev->bd_invalidated = 1;
> - }
> -}
> -
> static void nbd_parse_flags(struct nbd_device *nbd, struct block_device *bdev)
> {
> if (nbd->flags & NBD_FLAG_READ_ONLY)
> @@ -803,18 +812,11 @@ static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
> mutex_lock(&nbd->tx_lock);
> nbd->task_recv = NULL;
>
> - sock_shutdown(nbd);
> - nbd_clear_que(nbd);
> - kill_bdev(bdev);
> - nbd_bdev_reset(bdev);
> -
> if (nbd->disconnect) /* user requested, ignore socket errors */
> error = 0;
> if (nbd->timedout)
> error = -ETIMEDOUT;
>
> - nbd_reset(nbd);
> -
> return error;
> }
>
> @@ -853,10 +855,35 @@ static int nbd_ioctl(struct block_device *bdev, fmode_t mode,
> return error;
> }
>
> +static int nbd_open(struct block_device *bdev, fmode_t mode)
> +{
> + struct nbd_device *nbd = bdev->bd_disk->private_data;
> +
> + atomic_inc(&nbd->users);
> +
> + if (!nbd->bdev)
> + nbd->bdev = bdev;
> +
> + return 0;
> +}
> +
> +static void nbd_release(struct gendisk *disk, fmode_t mode)
> +{
> + struct nbd_device *nbd = disk->private_data;
> +
> + if (atomic_dec_and_test(&nbd->users)) {
> + mutex_lock(&nbd->tx_lock);
> + nbd_reset(nbd);
> + mutex_unlock(&nbd->tx_lock);
> + }
> +}
> +
> static const struct block_device_operations nbd_fops = {
> .owner = THIS_MODULE,
> .ioctl = nbd_ioctl,
> .compat_ioctl = nbd_ioctl,
> + .open = nbd_open,
> + .release = nbd_release,
> };
>
> #if IS_ENABLED(CONFIG_DEBUG_FS)
> @@ -1087,6 +1114,7 @@ static int __init nbd_init(void)
> disk->private_data = &nbd_dev[i];
> sprintf(disk->disk_name, "nbd%d", i);
> nbd_reset(&nbd_dev[i]);
> + atomic_set(&nbd_dev[i].users, 0);
> add_disk(disk);
> }
>
> --
> 2.1.4
>
I'll be sending out rebased patch today, by next hour or so. There are
modifications to it as well like you've suggested.
--
---P.K.S
next prev parent reply other threads:[~2016-06-24 9:41 UTC|newest]
Thread overview: 48+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-05-24 11:26 [PATCH 0/4]nbd: fixes for nbd Pranay Kr. Srivastava
2016-05-24 11:26 ` [PATCH 1/4] fix might_sleep warning on socket shutdown Pranay Kr. Srivastava
2016-05-30 12:06 ` Markus Pargmann
2016-05-24 11:26 ` [PATCH 2/4] fix various coding standard warnings Pranay Kr. Srivastava
2016-05-24 11:26 ` [PATCH 3/4] make nbd device wait for its users Pranay Kr. Srivastava
2016-05-30 10:44 ` Markus Pargmann
2016-05-24 11:26 ` [PATCH 4/4] use device_attr macros for sysfs attribute Pranay Kr. Srivastava
2016-05-30 3:35 ` [PATCH 0/4]nbd: fixes for nbd Pranay Srivastava
2016-05-30 12:27 ` Markus Pargmann
2016-06-02 10:24 ` [PATCH v2 0/5] nbd: " Pranay Kr. Srivastava
2016-06-02 10:24 ` [PATCH v2 1/5] nbd: fix might_sleep warning on socket shutdown Pranay Kr. Srivastava
2016-06-09 10:03 ` Pranay Srivastava
2016-06-14 5:13 ` Pranay Srivastava
2016-06-14 8:52 ` Markus Pargmann
2016-06-14 9:50 ` Pranay Srivastava
2016-06-24 10:09 ` [PATCH v3 0/3] nbd: resolve bugs and limitations Pranay Kr. Srivastava
2016-06-24 10:09 ` [PATCH v3 1/3]nbd: fix might_sleep warning on socket shutdown Pranay Kr. Srivastava
2016-06-28 5:42 ` Pranay Srivastava
2016-06-29 7:18 ` Markus Pargmann
2016-06-24 10:09 ` [PATCH v3 2/3]nbd: cleanup nbd_set_socket Pranay Kr. Srivastava
2016-06-24 10:09 ` [PATCH 3/3]nbd: make nbd device wait for its users Pranay Kr. Srivastava
2016-06-24 13:42 ` [Nbd] " Eric Blake
2016-06-25 17:56 ` Pranay Srivastava
2016-06-25 18:01 ` Pranay Srivastava
2016-06-29 7:06 ` Markus Pargmann
2016-06-29 7:15 ` Pranay Srivastava
2016-06-02 10:24 ` [PATCH v2 2/5]nbd: cleanup nbd_set_socket Pranay Kr. Srivastava
2016-06-02 10:24 ` [PATCH v2 3/5]nbd: fix various coding standard warnings Pranay Kr. Srivastava
2016-06-02 10:25 ` [PATCH v2 4/5]nbd: make nbd device wait for its users Pranay Kr. Srivastava
2016-06-14 8:59 ` Markus Pargmann
2016-06-14 9:33 ` Pranay Srivastava
2016-06-15 6:30 ` Markus Pargmann
2016-06-15 7:00 ` [Nbd] " Wouter Verhelst
2016-06-15 9:18 ` Pranay Srivastava
2016-06-15 9:17 ` Pranay Srivastava
2016-06-24 9:29 ` [PATCH 1/2] nbd: " Markus Pargmann
2016-06-24 9:29 ` [PATCH 2/2] nbd: Disallow ioctls on disconnected block device Markus Pargmann
2016-07-16 7:42 ` Pranay Srivastava
2016-07-16 9:32 ` [Nbd] " Alex Bligh
2016-07-16 10:08 ` Pranay Srivastava
2016-07-16 11:26 ` Wouter Verhelst
2016-07-16 13:31 ` Pranay Srivastava
2016-06-24 9:39 ` Pranay Srivastava [this message]
2016-06-24 13:40 ` [Nbd] [PATCH 1/2] nbd: make nbd device wait for its users Eric Blake
2016-06-25 17:52 ` Pranay Srivastava
2016-06-29 6:57 ` Markus Pargmann
2016-06-02 10:25 ` [PATCH v2 5/5]nbd: use device_attr macros for sysfs attribute Pranay Kr. Srivastava
[not found] ` <CA+aCy1E0S4ofa04xcO9qxQmuipaF5wdnrv3ubSvETn-rBYYisA@mail.gmail.com>
2016-06-06 11:07 ` [PATCH 0/4]nbd: fixes for nbd Pranay Srivastava
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CA+aCy1G9Z-bhdEpWxNpwxFSPxXOPqLW+fPV+Q44DGDib9KDeTA@mail.gmail.com \
--to=pranjas@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mpa@pengutronix.de \
--cc=nbd-general@lists.sourceforge.net \
--cc=w@uter.be \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).