1 From 3a2d63f87989e01437ba994df5f297528c353d7d Mon Sep 17 00:00:00 2001
2 From: Paolo Bonzini <pbonzini@redhat.com>
3 Date: Wed, 27 Feb 2013 17:05:25 -0800
4 Subject: nbd: fsync and kill block device on shutdown
6 From: Paolo Bonzini <pbonzini@redhat.com>
8 commit 3a2d63f87989e01437ba994df5f297528c353d7d upstream.
10 There are two problems with shutdown in the NBD driver.
12 1: Receiving the NBD_DISCONNECT ioctl does not sync the filesystem.
14 This patch adds the sync operation into __nbd_ioctl()'s
15 NBD_DISCONNECT handler. This is useful because BLKFLSBUF is restricted
16 to processes that have CAP_SYS_ADMIN, and the NBD client may not
17 possess it (fsync of the block device does not sync the filesystem,
20 2: Once we clear the socket we have no guarantee that later reads will
21 come from the same backing storage.
23 The patch adds calls to kill_bdev() in __nbd_ioctl()'s socket
24 clearing code so the page cache is cleaned, lest reads that hit on the
25 page cache will return stale data from the previously-accessible disk.
29 # qemu-nbd -r -c/dev/nbd0 /dev/sr0
31 /dev/stdin: # UDF filesystem data (version 1.5) etc.
32 # qemu-nbd -d /dev/nbd0
33 # qemu-nbd -r -c/dev/nbd0 /dev/sda
35 /dev/stdin: # UDF filesystem data (version 1.5) etc.
40 /dev/sda: x86 boot sector; etc.
42 Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
43 Acked-by: Paul Clements <Paul.Clements@steeleye.com>
44 Cc: Alex Bligh <alex@alex.org.uk>
45 Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
46 Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
47 [bwh: Backported to 3.2:
50 - Incorporate export of kill_bdev() from commit ff01bb483265
51 ('fs: move code out of buffer.c')]
52 Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
53 [hq: Backported to 3.4: Adjusted context]
54 Signed-off-by: Qiang Huang <h.huangqiang@huawei.com>
55 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
57 drivers/block/nbd.c | 9 +++++++++
58 1 file changed, 9 insertions(+)
60 --- a/drivers/block/nbd.c
61 +++ b/drivers/block/nbd.c
62 @@ -584,10 +584,17 @@ static int __nbd_ioctl(struct block_devi
65 dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n");
69 + mutex_unlock(&nbd->tx_lock);
71 + mutex_lock(&nbd->tx_lock);
72 blk_rq_init(NULL, &sreq);
73 sreq.cmd_type = REQ_TYPE_SPECIAL;
74 nbd_cmd(&sreq) = NBD_CMD_DISC;
76 + /* Check again after getting mutex back. */
80 @@ -606,6 +613,7 @@ static int __nbd_ioctl(struct block_devi
82 BUG_ON(!list_empty(&nbd->queue_head));
83 BUG_ON(!list_empty(&nbd->waiting_queue));
88 @@ -688,6 +696,7 @@ static int __nbd_ioctl(struct block_devi
91 dev_warn(disk_to_dev(nbd->disk), "queue cleared\n");