Skip to content

Commit 384d87e

Browse files
jankaraaxboe
authored andcommitted
block: Do not discard buffers under a mounted filesystem
Discarding blocks and buffers under a mounted filesystem is hardly anything admin wants to do. Usually it will confuse the filesystem and sometimes the loss of buffer_head state (including b_private field) can even cause crashes like: BUG: unable to handle kernel NULL pointer dereference at 0000000000000008 PGD 0 P4D 0 Oops: 0002 [raspberrypi#1] SMP PTI CPU: 4 PID: 203778 Comm: jbd2/dm-3-8 Kdump: loaded Tainted: G O --------- - - 4.18.0-147.5.0.5.h126.eulerosv2r9.x86_64 raspberrypi#1 Hardware name: Huawei RH2288H V3/BC11HGSA0, BIOS 1.57 08/11/2015 RIP: 0010:jbd2_journal_grab_journal_head+0x1b/0x40 [jbd2] ... Call Trace: __jbd2_journal_insert_checkpoint+0x23/0x70 [jbd2] jbd2_journal_commit_transaction+0x155f/0x1b60 [jbd2] kjournald2+0xbd/0x270 [jbd2] So if we don't have block device open with O_EXCL already, claim the block device while we truncate buffer cache. This makes sure any exclusive block device user (such as filesystem) cannot operate on the device while we are discarding buffer cache. Reported-by: Ye Bin <yebin10@huawei.com> Signed-off-by: Jan Kara <jack@suse.cz> Reviewed-by: Christoph Hellwig <hch@lst.de> [axboe: fix !CONFIG_BLOCK error in truncate_bdev_range()] Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 6dbf7bb commit 384d87e

3 files changed

Lines changed: 50 additions & 10 deletions

File tree

‎block/ioctl.c‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ static int blk_ioctl_discard(struct block_device *bdev, fmode_t mode,
112112
uint64_t range[2];
113113
uint64_t start, len;
114114
struct request_queue *q = bdev_get_queue(bdev);
115-
struct address_space *mapping = bdev->bd_inode->i_mapping;
116-
115+
int err;
117116

118117
if (!(mode & FMODE_WRITE))
119118
return -EBADF;
@@ -134,7 +133,11 @@ static int blk_ioctl_discard(struct block_device *bdev, fmode_t mode,
134133

135134
if (start + len > i_size_read(bdev->bd_inode))
136135
return -EINVAL;
137-
truncate_inode_pages_range(mapping, start, start + len - 1);
136+
137+
err = truncate_bdev_range(bdev, mode, start, start + len - 1);
138+
if (err)
139+
return err;
140+
138141
return blkdev_issue_discard(bdev, start >> 9, len >> 9,
139142
GFP_KERNEL, flags);
140143
}
@@ -143,8 +146,8 @@ static int blk_ioctl_zeroout(struct block_device *bdev, fmode_t mode,
143146
unsigned long arg)
144147
{
145148
uint64_t range[2];
146-
struct address_space *mapping;
147149
uint64_t start, end, len;
150+
int err;
148151

149152
if (!(mode & FMODE_WRITE))
150153
return -EBADF;
@@ -166,8 +169,9 @@ static int blk_ioctl_zeroout(struct block_device *bdev, fmode_t mode,
166169
return -EINVAL;
167170

168171
/* Invalidate the page cache, including dirty pages */
169-
mapping = bdev->bd_inode->i_mapping;
170-
truncate_inode_pages_range(mapping, start, end);
172+
err = truncate_bdev_range(bdev, mode, start, end);
173+
if (err)
174+
return err;
171175

172176
return blkdev_issue_zeroout(bdev, start >> 9, len >> 9, GFP_KERNEL,
173177
BLKDEV_ZERO_NOUNMAP);

‎fs/block_dev.c‎

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,35 @@ void invalidate_bdev(struct block_device *bdev)
103103
}
104104
EXPORT_SYMBOL(invalidate_bdev);
105105

106+
/*
107+
* Drop all buffers & page cache for given bdev range. This function bails
108+
* with error if bdev has other exclusive owner (such as filesystem).
109+
*/
110+
int truncate_bdev_range(struct block_device *bdev, fmode_t mode,
111+
loff_t lstart, loff_t lend)
112+
{
113+
struct block_device *claimed_bdev = NULL;
114+
int err;
115+
116+
/*
117+
* If we don't hold exclusive handle for the device, upgrade to it
118+
* while we discard the buffer cache to avoid discarding buffers
119+
* under live filesystem.
120+
*/
121+
if (!(mode & FMODE_EXCL)) {
122+
claimed_bdev = bdev->bd_contains;
123+
err = bd_prepare_to_claim(bdev, claimed_bdev,
124+
truncate_bdev_range);
125+
if (err)
126+
return err;
127+
}
128+
truncate_inode_pages_range(bdev->bd_inode->i_mapping, lstart, lend);
129+
if (claimed_bdev)
130+
bd_abort_claiming(bdev, claimed_bdev, truncate_bdev_range);
131+
return 0;
132+
}
133+
EXPORT_SYMBOL(truncate_bdev_range);
134+
106135
static void set_init_blocksize(struct block_device *bdev)
107136
{
108137
bdev->bd_inode->i_blkbits = blksize_bits(bdev_logical_block_size(bdev));
@@ -1968,7 +1997,6 @@ static long blkdev_fallocate(struct file *file, int mode, loff_t start,
19681997
loff_t len)
19691998
{
19701999
struct block_device *bdev = I_BDEV(bdev_file_inode(file));
1971-
struct address_space *mapping;
19722000
loff_t end = start + len - 1;
19732001
loff_t isize;
19742002
int error;
@@ -1996,8 +2024,9 @@ static long blkdev_fallocate(struct file *file, int mode, loff_t start,
19962024
return -EINVAL;
19972025

19982026
/* Invalidate the page cache, including dirty pages. */
1999-
mapping = bdev->bd_inode->i_mapping;
2000-
truncate_inode_pages_range(mapping, start, end);
2027+
error = truncate_bdev_range(bdev, file->f_mode, start, end);
2028+
if (error)
2029+
return error;
20012030

20022031
switch (mode) {
20032032
case FALLOC_FL_ZERO_RANGE:
@@ -2024,7 +2053,7 @@ static long blkdev_fallocate(struct file *file, int mode, loff_t start,
20242053
* the caller will be given -EBUSY. The third argument is
20252054
* inclusive, so the rounding here is safe.
20262055
*/
2027-
return invalidate_inode_pages2_range(mapping,
2056+
return invalidate_inode_pages2_range(bdev->bd_inode->i_mapping,
20282057
start >> PAGE_SHIFT,
20292058
end >> PAGE_SHIFT);
20302059
}

‎include/linux/blkdev.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,11 +1987,18 @@ void bdput(struct block_device *);
19871987

19881988
#ifdef CONFIG_BLOCK
19891989
void invalidate_bdev(struct block_device *bdev);
1990+
int truncate_bdev_range(struct block_device *bdev, fmode_t mode, loff_t lstart,
1991+
loff_t lend);
19901992
int sync_blockdev(struct block_device *bdev);
19911993
#else
19921994
static inline void invalidate_bdev(struct block_device *bdev)
19931995
{
19941996
}
1997+
static inline int truncate_bdev_range(struct block_device *bdev, fmode_t mode,
1998+
loff_t lstart, loff_t lend)
1999+
{
2000+
return 0;
2001+
}
19952002
static inline int sync_blockdev(struct block_device *bdev)
19962003
{
19972004
return 0;

0 commit comments

Comments
 (0)