Commit 382497ad authored by Matthew Wilcox (Oracle)'s avatar Matthew Wilcox (Oracle) Committed by Andrew Morton

buffer: calculate block number inside folio_init_buffers()

The calculation of block from index doesn't work for devices with a block
size larger than PAGE_SIZE as we end up shifting by a negative number. 
Instead, calculate the number of the first block from the folio's position
in the block device.  We no longer need to pass sizebits to
grow_dev_folio().

Link: https://lkml.kernel.org/r/20231109210608.2252323-3-willy@infradead.orgSigned-off-by: default avatarMatthew Wilcox (Oracle) <willy@infradead.org>
Reviewed-by: default avatarPankaj Raghav <p.raghav@samsung.com>
Cc: Hannes Reinecke <hare@suse.de>
Cc: Luis Chamberlain <mcgrof@kernel.org>
Cc: Ryusuke Konishi <konishi.ryusuke@gmail.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
parent 6d840a18
...@@ -995,11 +995,12 @@ static sector_t blkdev_max_block(struct block_device *bdev, unsigned int size) ...@@ -995,11 +995,12 @@ static sector_t blkdev_max_block(struct block_device *bdev, unsigned int size)
* Initialise the state of a blockdev folio's buffers. * Initialise the state of a blockdev folio's buffers.
*/ */
static sector_t folio_init_buffers(struct folio *folio, static sector_t folio_init_buffers(struct folio *folio,
struct block_device *bdev, sector_t block, int size) struct block_device *bdev, unsigned size)
{ {
struct buffer_head *head = folio_buffers(folio); struct buffer_head *head = folio_buffers(folio);
struct buffer_head *bh = head; struct buffer_head *bh = head;
bool uptodate = folio_test_uptodate(folio); bool uptodate = folio_test_uptodate(folio);
sector_t block = div_u64(folio_pos(folio), size);
sector_t end_block = blkdev_max_block(bdev, size); sector_t end_block = blkdev_max_block(bdev, size);
do { do {
...@@ -1032,7 +1033,7 @@ static sector_t folio_init_buffers(struct folio *folio, ...@@ -1032,7 +1033,7 @@ static sector_t folio_init_buffers(struct folio *folio,
* we succeeded, or the caller should retry. * we succeeded, or the caller should retry.
*/ */
static bool grow_dev_folio(struct block_device *bdev, sector_t block, static bool grow_dev_folio(struct block_device *bdev, sector_t block,
pgoff_t index, unsigned size, int sizebits, gfp_t gfp) pgoff_t index, unsigned size, gfp_t gfp)
{ {
struct inode *inode = bdev->bd_inode; struct inode *inode = bdev->bd_inode;
struct folio *folio; struct folio *folio;
...@@ -1047,8 +1048,7 @@ static bool grow_dev_folio(struct block_device *bdev, sector_t block, ...@@ -1047,8 +1048,7 @@ static bool grow_dev_folio(struct block_device *bdev, sector_t block,
bh = folio_buffers(folio); bh = folio_buffers(folio);
if (bh) { if (bh) {
if (bh->b_size == size) { if (bh->b_size == size) {
end_block = folio_init_buffers(folio, bdev, end_block = folio_init_buffers(folio, bdev, size);
(sector_t)index << sizebits, size);
goto unlock; goto unlock;
} }
...@@ -1069,8 +1069,7 @@ static bool grow_dev_folio(struct block_device *bdev, sector_t block, ...@@ -1069,8 +1069,7 @@ static bool grow_dev_folio(struct block_device *bdev, sector_t block,
*/ */
spin_lock(&inode->i_mapping->private_lock); spin_lock(&inode->i_mapping->private_lock);
link_dev_buffers(folio, bh); link_dev_buffers(folio, bh);
end_block = folio_init_buffers(folio, bdev, end_block = folio_init_buffers(folio, bdev, size);
(sector_t)index << sizebits, size);
spin_unlock(&inode->i_mapping->private_lock); spin_unlock(&inode->i_mapping->private_lock);
unlock: unlock:
folio_unlock(folio); folio_unlock(folio);
...@@ -1105,7 +1104,7 @@ static bool grow_buffers(struct block_device *bdev, sector_t block, ...@@ -1105,7 +1104,7 @@ static bool grow_buffers(struct block_device *bdev, sector_t block,
} }
/* Create a folio with the proper size buffers */ /* Create a folio with the proper size buffers */
return grow_dev_folio(bdev, block, index, size, sizebits, gfp); return grow_dev_folio(bdev, block, index, size, gfp);
} }
static struct buffer_head * static struct buffer_head *
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment