Skip to content

Commit 6e1db88

Browse files
Christoph HellwigAl Viro
authored andcommitted
introduce __block_write_begin
Split up the block_write_begin implementation - __block_write_begin is a new trivial wrapper for block_prepare_write that always takes an already allocated page and can be either called from block_write_begin or filesystem code that already has a page allocated. Remove the handling of already allocated pages from block_write_begin after switching all callers that do it to __block_write_begin. Signed-off-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent f4e420d commit 6e1db88

10 files changed

Lines changed: 39 additions & 64 deletions

File tree

‎fs/buffer.c‎

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1833,9 +1833,10 @@ void page_zero_new_buffers(struct page *page, unsigned from, unsigned to)
18331833
}
18341834
EXPORT_SYMBOL(page_zero_new_buffers);
18351835

1836-
static int __block_prepare_write(struct inode *inode, struct page *page,
1837-
unsigned from, unsigned to, get_block_t *get_block)
1836+
int block_prepare_write(struct page *page, unsigned from, unsigned to,
1837+
get_block_t *get_block)
18381838
{
1839+
struct inode *inode = page->mapping->host;
18391840
unsigned block_start, block_end;
18401841
sector_t block;
18411842
int err = 0;
@@ -1908,10 +1909,13 @@ static int __block_prepare_write(struct inode *inode, struct page *page,
19081909
if (!buffer_uptodate(*wait_bh))
19091910
err = -EIO;
19101911
}
1911-
if (unlikely(err))
1912+
if (unlikely(err)) {
19121913
page_zero_new_buffers(page, from, to);
1914+
ClearPageUptodate(page);
1915+
}
19131916
return err;
19141917
}
1918+
EXPORT_SYMBOL(block_prepare_write);
19151919

19161920
static int __block_commit_write(struct inode *inode, struct page *page,
19171921
unsigned from, unsigned to)
@@ -1948,6 +1952,15 @@ static int __block_commit_write(struct inode *inode, struct page *page,
19481952
return 0;
19491953
}
19501954

1955+
int __block_write_begin(struct page *page, loff_t pos, unsigned len,
1956+
get_block_t *get_block)
1957+
{
1958+
unsigned start = pos & (PAGE_CACHE_SIZE - 1);
1959+
1960+
return block_prepare_write(page, start, start + len, get_block);
1961+
}
1962+
EXPORT_SYMBOL(__block_write_begin);
1963+
19511964
/*
19521965
* Filesystems implementing the new truncate sequence should use the
19531966
* _newtrunc postfix variant which won't incorrectly call vmtruncate.
@@ -1958,41 +1971,22 @@ int block_write_begin_newtrunc(struct file *file, struct address_space *mapping,
19581971
struct page **pagep, void **fsdata,
19591972
get_block_t *get_block)
19601973
{
1961-
struct inode *inode = mapping->host;
1962-
int status = 0;
1974+
pgoff_t index = pos >> PAGE_CACHE_SHIFT;
19631975
struct page *page;
1964-
pgoff_t index;
1965-
unsigned start, end;
1966-
int ownpage = 0;
1976+
int status;
19671977

1968-
index = pos >> PAGE_CACHE_SHIFT;
1969-
start = pos & (PAGE_CACHE_SIZE - 1);
1970-
end = start + len;
1971-
1972-
page = *pagep;
1973-
if (page == NULL) {
1974-
ownpage = 1;
1975-
page = grab_cache_page_write_begin(mapping, index, flags);
1976-
if (!page) {
1977-
status = -ENOMEM;
1978-
goto out;
1979-
}
1980-
*pagep = page;
1981-
} else
1982-
BUG_ON(!PageLocked(page));
1978+
page = grab_cache_page_write_begin(mapping, index, flags);
1979+
if (!page)
1980+
return -ENOMEM;
19831981

1984-
status = __block_prepare_write(inode, page, start, end, get_block);
1982+
status = __block_write_begin(page, pos, len, get_block);
19851983
if (unlikely(status)) {
1986-
ClearPageUptodate(page);
1987-
1988-
if (ownpage) {
1989-
unlock_page(page);
1990-
page_cache_release(page);
1991-
*pagep = NULL;
1992-
}
1984+
unlock_page(page);
1985+
page_cache_release(page);
1986+
page = NULL;
19931987
}
19941988

1995-
out:
1989+
*pagep = page;
19961990
return status;
19971991
}
19981992
EXPORT_SYMBOL(block_write_begin_newtrunc);
@@ -2379,17 +2373,6 @@ int cont_write_begin(struct file *file, struct address_space *mapping,
23792373
}
23802374
EXPORT_SYMBOL(cont_write_begin);
23812375

2382-
int block_prepare_write(struct page *page, unsigned from, unsigned to,
2383-
get_block_t *get_block)
2384-
{
2385-
struct inode *inode = page->mapping->host;
2386-
int err = __block_prepare_write(inode, page, from, to, get_block);
2387-
if (err)
2388-
ClearPageUptodate(page);
2389-
return err;
2390-
}
2391-
EXPORT_SYMBOL(block_prepare_write);
2392-
23932376
int block_commit_write(struct page *page, unsigned from, unsigned to)
23942377
{
23952378
struct inode *inode = page->mapping->host;

‎fs/ext2/dir.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,8 +450,7 @@ ino_t ext2_inode_by_name(struct inode *dir, struct qstr *child)
450450

451451
static int ext2_prepare_chunk(struct page *page, loff_t pos, unsigned len)
452452
{
453-
return block_write_begin_newtrunc(NULL, page->mapping, pos, len, 0,
454-
&page, NULL, ext2_get_block);
453+
return __block_write_begin(page, pos, len, ext2_get_block);
455454
}
456455

457456
/* Releases the page */

‎fs/ext3/inode.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,8 +1196,7 @@ static int ext3_write_begin(struct file *file, struct address_space *mapping,
11961196
ret = PTR_ERR(handle);
11971197
goto out;
11981198
}
1199-
ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
1200-
ext3_get_block);
1199+
ret = __block_write_begin(page, pos, len, ext3_get_block);
12011200
if (ret)
12021201
goto write_begin_failed;
12031202

‎fs/ext4/inode.c‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,11 +1578,9 @@ static int ext4_write_begin(struct file *file, struct address_space *mapping,
15781578
*pagep = page;
15791579

15801580
if (ext4_should_dioread_nolock(inode))
1581-
ret = block_write_begin(file, mapping, pos, len, flags, pagep,
1582-
fsdata, ext4_get_block_write);
1581+
ret = __block_write_begin(page, pos, len, ext4_get_block_write);
15831582
else
1584-
ret = block_write_begin(file, mapping, pos, len, flags, pagep,
1585-
fsdata, ext4_get_block);
1583+
ret = __block_write_begin(page, pos, len, ext4_get_block);
15861584

15871585
if (!ret && ext4_should_journal_data(inode)) {
15881586
ret = walk_page_buffers(handle, page_buffers(page),
@@ -1593,7 +1591,7 @@ static int ext4_write_begin(struct file *file, struct address_space *mapping,
15931591
unlock_page(page);
15941592
page_cache_release(page);
15951593
/*
1596-
* block_write_begin may have instantiated a few blocks
1594+
* __block_write_begin may have instantiated a few blocks
15971595
* outside i_size. Trim these off again. Don't need
15981596
* i_size_read because we hold i_mutex.
15991597
*
@@ -3185,8 +3183,7 @@ static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
31853183
}
31863184
*pagep = page;
31873185

3188-
ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
3189-
ext4_da_get_block_prep);
3186+
ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep);
31903187
if (ret < 0) {
31913188
unlock_page(page);
31923189
ext4_journal_stop(handle);

‎fs/minix/inode.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,7 @@ static int minix_readpage(struct file *file, struct page *page)
359359

360360
int minix_prepare_chunk(struct page *page, loff_t pos, unsigned len)
361361
{
362-
return block_write_begin_newtrunc(NULL, page->mapping, pos, len, 0,
363-
&page, NULL, minix_get_block);
362+
return __block_write_begin(page, pos, len, minix_get_block);
364363
}
365364

366365
static int minix_write_begin(struct file *file, struct address_space *mapping,

‎fs/nilfs2/dir.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ static unsigned nilfs_last_byte(struct inode *inode, unsigned long page_nr)
8383
static int nilfs_prepare_chunk(struct page *page, unsigned from, unsigned to)
8484
{
8585
loff_t pos = page_offset(page) + from;
86-
return block_write_begin_newtrunc(NULL, page->mapping, pos, to - from,
87-
0, &page, NULL, nilfs_get_block);
86+
return __block_write_begin(page, pos, to - from, nilfs_get_block);
8887
}
8988

9089
static void nilfs_commit_chunk(struct page *page,

‎fs/reiserfs/inode.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2585,8 +2585,7 @@ static int reiserfs_write_begin(struct file *file,
25852585
old_ref = th->t_refcount;
25862586
th->t_refcount++;
25872587
}
2588-
ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
2589-
reiserfs_get_block);
2588+
ret = __block_write_begin(page, pos, len, reiserfs_get_block);
25902589
if (ret && reiserfs_transaction_running(inode->i_sb)) {
25912590
struct reiserfs_transaction_handle *th = current->journal_info;
25922591
/* this gets a little ugly. If reiserfs_get_block returned an

‎fs/sysv/itree.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,8 +461,7 @@ static int sysv_readpage(struct file *file, struct page *page)
461461

462462
int sysv_prepare_chunk(struct page *page, loff_t pos, unsigned len)
463463
{
464-
return block_write_begin_newtrunc(NULL, page->mapping, pos, len, 0,
465-
&page, NULL, get_block);
464+
return __block_write_begin(page, pos, len, get_block);
466465
}
467466

468467
static int sysv_write_begin(struct file *file, struct address_space *mapping,

‎fs/ufs/inode.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -560,8 +560,7 @@ static int ufs_readpage(struct file *file, struct page *page)
560560

561561
int ufs_prepare_chunk(struct page *page, loff_t pos, unsigned len)
562562
{
563-
return block_write_begin_newtrunc(NULL, page->mapping, pos, len, 0,
564-
&page, NULL, ufs_getfrag_block);
563+
return __block_write_begin(page, pos, len, ufs_getfrag_block);
565564
}
566565

567566
static int ufs_write_begin(struct file *file, struct address_space *mapping,

‎include/linux/buffer_head.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ int block_write_begin_newtrunc(struct file *, struct address_space *,
209209
int block_write_begin(struct file *, struct address_space *,
210210
loff_t, unsigned, unsigned,
211211
struct page **, void **, get_block_t*);
212+
int __block_write_begin(struct page *page, loff_t pos, unsigned len,
213+
get_block_t *get_block);
212214
int block_write_end(struct file *, struct address_space *,
213215
loff_t, unsigned, unsigned,
214216
struct page *, void *);

0 commit comments

Comments
 (0)