Skip to content

Commit 45bce8f

Browse files
committed
fs/buffer.c: make block-size be per-page and protected by the page lock
This makes the buffer size handling be a per-page thing, which allows us to not have to worry about locking too much when changing the buffer size. If a page doesn't have buffers, we still need to read the block size from the inode, but we can do that with ACCESS_ONCE(), so that even if the size is changing, we get a consistent value. This doesn't convert all functions - many of the buffer functions are used purely by filesystems, which in turn results in the buffer size being fixed at mount-time. So they don't have the same consistency issues that the raw device access can have. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 9489e9d commit 45bce8f

1 file changed

Lines changed: 48 additions & 31 deletions

File tree

‎fs/buffer.c‎

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,6 +1552,28 @@ void unmap_underlying_metadata(struct block_device *bdev, sector_t block)
15521552
}
15531553
EXPORT_SYMBOL(unmap_underlying_metadata);
15541554

1555+
/*
1556+
* Size is a power-of-two in the range 512..PAGE_SIZE,
1557+
* and the case we care about most is PAGE_SIZE.
1558+
*
1559+
* So this *could* possibly be written with those
1560+
* constraints in mind (relevant mostly if some
1561+
* architecture has a slow bit-scan instruction)
1562+
*/
1563+
static inline int block_size_bits(unsigned int blocksize)
1564+
{
1565+
return ilog2(blocksize);
1566+
}
1567+
1568+
static struct buffer_head *create_page_buffers(struct page *page, struct inode *inode, unsigned int b_state)
1569+
{
1570+
BUG_ON(!PageLocked(page));
1571+
1572+
if (!page_has_buffers(page))
1573+
create_empty_buffers(page, 1 << ACCESS_ONCE(inode->i_blkbits), b_state);
1574+
return page_buffers(page);
1575+
}
1576+
15551577
/*
15561578
* NOTE! All mapped/uptodate combinations are valid:
15571579
*
@@ -1589,19 +1611,13 @@ static int __block_write_full_page(struct inode *inode, struct page *page,
15891611
sector_t block;
15901612
sector_t last_block;
15911613
struct buffer_head *bh, *head;
1592-
const unsigned blocksize = 1 << inode->i_blkbits;
1614+
unsigned int blocksize, bbits;
15931615
int nr_underway = 0;
15941616
int write_op = (wbc->sync_mode == WB_SYNC_ALL ?
15951617
WRITE_SYNC : WRITE);
15961618

1597-
BUG_ON(!PageLocked(page));
1598-
1599-
last_block = (i_size_read(inode) - 1) >> inode->i_blkbits;
1600-
1601-
if (!page_has_buffers(page)) {
1602-
create_empty_buffers(page, blocksize,
1619+
head = create_page_buffers(page, inode,
16031620
(1 << BH_Dirty)|(1 << BH_Uptodate));
1604-
}
16051621

16061622
/*
16071623
* Be very careful. We have no exclusion from __set_page_dirty_buffers
@@ -1613,9 +1629,12 @@ static int __block_write_full_page(struct inode *inode, struct page *page,
16131629
* handle that here by just cleaning them.
16141630
*/
16151631

1616-
block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1617-
head = page_buffers(page);
16181632
bh = head;
1633+
blocksize = bh->b_size;
1634+
bbits = block_size_bits(blocksize);
1635+
1636+
block = (sector_t)page->index << (PAGE_CACHE_SHIFT - bbits);
1637+
last_block = (i_size_read(inode) - 1) >> bbits;
16191638

16201639
/*
16211640
* Get all the dirty buffers mapped to disk addresses and
@@ -1806,12 +1825,10 @@ int __block_write_begin(struct page *page, loff_t pos, unsigned len,
18061825
BUG_ON(to > PAGE_CACHE_SIZE);
18071826
BUG_ON(from > to);
18081827

1809-
blocksize = 1 << inode->i_blkbits;
1810-
if (!page_has_buffers(page))
1811-
create_empty_buffers(page, blocksize, 0);
1812-
head = page_buffers(page);
1828+
head = create_page_buffers(page, inode, 0);
1829+
blocksize = head->b_size;
1830+
bbits = block_size_bits(blocksize);
18131831

1814-
bbits = inode->i_blkbits;
18151832
block = (sector_t)page->index << (PAGE_CACHE_SHIFT - bbits);
18161833

18171834
for(bh = head, block_start = 0; bh != head || !block_start;
@@ -1881,11 +1898,11 @@ static int __block_commit_write(struct inode *inode, struct page *page,
18811898
unsigned blocksize;
18821899
struct buffer_head *bh, *head;
18831900

1884-
blocksize = 1 << inode->i_blkbits;
1901+
bh = head = page_buffers(page);
1902+
blocksize = bh->b_size;
18851903

1886-
for(bh = head = page_buffers(page), block_start = 0;
1887-
bh != head || !block_start;
1888-
block_start=block_end, bh = bh->b_this_page) {
1904+
block_start = 0;
1905+
do {
18891906
block_end = block_start + blocksize;
18901907
if (block_end <= from || block_start >= to) {
18911908
if (!buffer_uptodate(bh))
@@ -1895,7 +1912,10 @@ static int __block_commit_write(struct inode *inode, struct page *page,
18951912
mark_buffer_dirty(bh);
18961913
}
18971914
clear_buffer_new(bh);
1898-
}
1915+
1916+
block_start = block_end;
1917+
bh = bh->b_this_page;
1918+
} while (bh != head);
18991919

19001920
/*
19011921
* If this is a partial write which happened to make all buffers
@@ -2020,7 +2040,6 @@ EXPORT_SYMBOL(generic_write_end);
20202040
int block_is_partially_uptodate(struct page *page, read_descriptor_t *desc,
20212041
unsigned long from)
20222042
{
2023-
struct inode *inode = page->mapping->host;
20242043
unsigned block_start, block_end, blocksize;
20252044
unsigned to;
20262045
struct buffer_head *bh, *head;
@@ -2029,13 +2048,13 @@ int block_is_partially_uptodate(struct page *page, read_descriptor_t *desc,
20292048
if (!page_has_buffers(page))
20302049
return 0;
20312050

2032-
blocksize = 1 << inode->i_blkbits;
2051+
head = page_buffers(page);
2052+
blocksize = head->b_size;
20332053
to = min_t(unsigned, PAGE_CACHE_SIZE - from, desc->count);
20342054
to = from + to;
20352055
if (from < blocksize && to > PAGE_CACHE_SIZE - blocksize)
20362056
return 0;
20372057

2038-
head = page_buffers(page);
20392058
bh = head;
20402059
block_start = 0;
20412060
do {
@@ -2068,18 +2087,16 @@ int block_read_full_page(struct page *page, get_block_t *get_block)
20682087
struct inode *inode = page->mapping->host;
20692088
sector_t iblock, lblock;
20702089
struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
2071-
unsigned int blocksize;
2090+
unsigned int blocksize, bbits;
20722091
int nr, i;
20732092
int fully_mapped = 1;
20742093

2075-
BUG_ON(!PageLocked(page));
2076-
blocksize = 1 << inode->i_blkbits;
2077-
if (!page_has_buffers(page))
2078-
create_empty_buffers(page, blocksize, 0);
2079-
head = page_buffers(page);
2094+
head = create_page_buffers(page, inode, 0);
2095+
blocksize = head->b_size;
2096+
bbits = block_size_bits(blocksize);
20802097

2081-
iblock = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
2082-
lblock = (i_size_read(inode)+blocksize-1) >> inode->i_blkbits;
2098+
iblock = (sector_t)page->index << (PAGE_CACHE_SHIFT - bbits);
2099+
lblock = (i_size_read(inode)+blocksize-1) >> bbits;
20832100
bh = head;
20842101
nr = 0;
20852102
i = 0;

0 commit comments

Comments
 (0)