38091290aa25378994e8462939d1b1707829ed21
[openwrt/openwrt.git] / target / linux / generic / hack-5.4 / 551-loop-Better-discard-support-for-block-devices.patch
1 From 3117c3f45edbcc269baaebd3d13f39b7bf884aa6 Mon Sep 17 00:00:00 2001
2 From: Evan Green <evgreen@chromium.org>
3 Date: Thu, 14 Nov 2019 15:50:08 -0800
4 Subject: loop: Better discard support for block devices
5
6 If the backing device for a loop device is itself a block device,
7 then mirror the "write zeroes" capabilities of the underlying
8 block device into the loop device. Copy this capability into both
9 max_write_zeroes_sectors and max_discard_sectors of the loop device.
10
11 The reason for this is that REQ_OP_DISCARD on a loop device translates
12 into blkdev_issue_zeroout(), rather than blkdev_issue_discard(). This
13 presents a consistent interface for loop devices (that discarded data
14 is zeroed), regardless of the backing device type of the loop device.
15 There should be no behavior change for loop devices backed by regular
16 files.
17
18 This change fixes blktest block/003, and removes an extraneous
19 error print in block/013 when testing on a loop device backed
20 by a block device that does not support discard.
21
22 Signed-off-by: Evan Green <evgreen@chromium.org>
23 Reviewed-by: Gwendal Grignou <gwendal@chromium.org>
24 Reviewed-by: Chaitanya Kulkarni <chaitanya.kulkarni@wdc.com>
25 ---
26 drivers/block/loop.c | 40 +++++++++++++++++++++++++++++-----------
27 1 file changed, 29 insertions(+), 11 deletions(-)
28
29 --- a/drivers/block/loop.c
30 +++ b/drivers/block/loop.c
31 @@ -427,11 +427,12 @@ static int lo_fallocate(struct loop_devi
32 * information.
33 */
34 struct file *file = lo->lo_backing_file;
35 + struct request_queue *q = lo->lo_queue;
36 int ret;
37
38 mode |= FALLOC_FL_KEEP_SIZE;
39
40 - if ((!file->f_op->fallocate) || lo->lo_encrypt_key_size) {
41 + if (!blk_queue_discard(q)) {
42 ret = -EOPNOTSUPP;
43 goto out;
44 }
45 @@ -862,6 +863,21 @@ static void loop_config_discard(struct l
46 struct file *file = lo->lo_backing_file;
47 struct inode *inode = file->f_mapping->host;
48 struct request_queue *q = lo->lo_queue;
49 + struct request_queue *backingq;
50 +
51 + /*
52 + * If the backing device is a block device, mirror its zeroing
53 + * capability. REQ_OP_DISCARD translates to a zero-out even when backed
54 + * by block devices to keep consistent behavior with file-backed loop
55 + * devices.
56 + */
57 + if (S_ISBLK(inode->i_mode) && !lo->lo_encrypt_key_size) {
58 + backingq = bdev_get_queue(inode->i_bdev);
59 + blk_queue_max_discard_sectors(q,
60 + backingq->limits.max_write_zeroes_sectors);
61 +
62 + blk_queue_max_write_zeroes_sectors(q,
63 + backingq->limits.max_write_zeroes_sectors);
64
65 /*
66 * We use punch hole to reclaim the free space used by the
67 @@ -869,22 +885,24 @@ static void loop_config_discard(struct l
68 * encryption is enabled, because it may give an attacker
69 * useful information.
70 */
71 - if ((!file->f_op->fallocate) ||
72 - lo->lo_encrypt_key_size) {
73 + } else if ((!file->f_op->fallocate) || lo->lo_encrypt_key_size) {
74 q->limits.discard_granularity = 0;
75 q->limits.discard_alignment = 0;
76 blk_queue_max_discard_sectors(q, 0);
77 blk_queue_max_write_zeroes_sectors(q, 0);
78 - blk_queue_flag_clear(QUEUE_FLAG_DISCARD, q);
79 - return;
80 - }
81
82 - q->limits.discard_granularity = inode->i_sb->s_blocksize;
83 - q->limits.discard_alignment = 0;
84 + } else {
85 + q->limits.discard_granularity = inode->i_sb->s_blocksize;
86 + q->limits.discard_alignment = 0;
87
88 - blk_queue_max_discard_sectors(q, UINT_MAX >> 9);
89 - blk_queue_max_write_zeroes_sectors(q, UINT_MAX >> 9);
90 - blk_queue_flag_set(QUEUE_FLAG_DISCARD, q);
91 + blk_queue_max_discard_sectors(q, UINT_MAX >> 9);
92 + blk_queue_max_write_zeroes_sectors(q, UINT_MAX >> 9);
93 + }
94 +
95 + if (q->limits.max_write_zeroes_sectors)
96 + blk_queue_flag_set(QUEUE_FLAG_DISCARD, q);
97 + else
98 + blk_queue_flag_clear(QUEUE_FLAG_DISCARD, q);
99 }
100
101 static void loop_unprepare_queue(struct loop_device *lo)