]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob - releases/3.4.92/nbd-fsync-and-kill-block-device-on-shutdown.patch
Linux 5.1.5
[thirdparty/kernel/stable-queue.git] / releases / 3.4.92 / nbd-fsync-and-kill-block-device-on-shutdown.patch
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
5
6 From: Paolo Bonzini <pbonzini@redhat.com>
7
8 commit 3a2d63f87989e01437ba994df5f297528c353d7d upstream.
9
10 There are two problems with shutdown in the NBD driver.
11
12 1: Receiving the NBD_DISCONNECT ioctl does not sync the filesystem.
13
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,
18 either).
19
20 2: Once we clear the socket we have no guarantee that later reads will
21 come from the same backing storage.
22
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.
26
27 Example:
28
29 # qemu-nbd -r -c/dev/nbd0 /dev/sr0
30 # file -s /dev/nbd0
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
34 # file -s /dev/nbd0
35 /dev/stdin: # UDF filesystem data (version 1.5) etc.
36
37 While /dev/sda has:
38
39 # file -s /dev/sda
40 /dev/sda: x86 boot sector; etc.
41
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:
48 - Adjusted context
49 - s/\bnbd\b/lo/
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>
56 ---
57 drivers/block/nbd.c | 9 +++++++++
58 1 file changed, 9 insertions(+)
59
60 --- a/drivers/block/nbd.c
61 +++ b/drivers/block/nbd.c
62 @@ -584,10 +584,17 @@ static int __nbd_ioctl(struct block_devi
63 struct request sreq;
64
65 dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n");
66 + if (!nbd->sock)
67 + return -EINVAL;
68
69 + mutex_unlock(&nbd->tx_lock);
70 + fsync_bdev(bdev);
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;
75 +
76 + /* Check again after getting mutex back. */
77 if (!nbd->sock)
78 return -EINVAL;
79
80 @@ -606,6 +613,7 @@ static int __nbd_ioctl(struct block_devi
81 nbd_clear_que(nbd);
82 BUG_ON(!list_empty(&nbd->queue_head));
83 BUG_ON(!list_empty(&nbd->waiting_queue));
84 + kill_bdev(bdev);
85 if (file)
86 fput(file);
87 return 0;
88 @@ -688,6 +696,7 @@ static int __nbd_ioctl(struct block_devi
89 nbd->file = NULL;
90 nbd_clear_que(nbd);
91 dev_warn(disk_to_dev(nbd->disk), "queue cleared\n");
92 + kill_bdev(bdev);
93 if (file)
94 fput(file);
95 nbd->bytesize = 0;