Skip to content

Commit f4e420d

Browse files
Christoph HellwigAl Viro
authored andcommitted
clean up write_begin usage for directories in pagecache
For filesystem that implement directories in pagecache we call block_write_begin with an already allocated page for this code, while the normal regular file write path uses the default block_write_begin behaviour. Get rid of the __foofs_write_begin helper and opencode the normal write_begin call in foofs_write_begin, while adding a new foofs_prepare_chunk helper for the directory code. The added benefit is that foofs_prepare_chunk has a much saner calling convention. Note that the interruptible flag passed into block_write_begin is always ignored if we already pass in a page (see next patch for details), and we never were doing truncations of exessive blocks for this case either so we can switch directly to block_write_begin_newtrunc. Signed-off-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 282dc17 commit f4e420d

13 files changed

Lines changed: 57 additions & 107 deletions

File tree

‎fs/ext2/dir.c‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,12 @@ ino_t ext2_inode_by_name(struct inode *dir, struct qstr *child)
448448
return res;
449449
}
450450

451+
static int ext2_prepare_chunk(struct page *page, loff_t pos, unsigned len)
452+
{
453+
return block_write_begin_newtrunc(NULL, page->mapping, pos, len, 0,
454+
&page, NULL, ext2_get_block);
455+
}
456+
451457
/* Releases the page */
452458
void ext2_set_link(struct inode *dir, struct ext2_dir_entry_2 *de,
453459
struct page *page, struct inode *inode, int update_times)
@@ -458,8 +464,7 @@ void ext2_set_link(struct inode *dir, struct ext2_dir_entry_2 *de,
458464
int err;
459465

460466
lock_page(page);
461-
err = __ext2_write_begin(NULL, page->mapping, pos, len,
462-
AOP_FLAG_UNINTERRUPTIBLE, &page, NULL);
467+
err = ext2_prepare_chunk(page, pos, len);
463468
BUG_ON(err);
464469
de->inode = cpu_to_le32(inode->i_ino);
465470
ext2_set_de_type(de, inode);
@@ -542,8 +547,7 @@ int ext2_add_link (struct dentry *dentry, struct inode *inode)
542547
got_it:
543548
pos = page_offset(page) +
544549
(char*)de - (char*)page_address(page);
545-
err = __ext2_write_begin(NULL, page->mapping, pos, rec_len, 0,
546-
&page, NULL);
550+
err = ext2_prepare_chunk(page, pos, rec_len);
547551
if (err)
548552
goto out_unlock;
549553
if (de->inode) {
@@ -576,8 +580,7 @@ int ext2_add_link (struct dentry *dentry, struct inode *inode)
576580
*/
577581
int ext2_delete_entry (struct ext2_dir_entry_2 * dir, struct page * page )
578582
{
579-
struct address_space *mapping = page->mapping;
580-
struct inode *inode = mapping->host;
583+
struct inode *inode = page->mapping->host;
581584
char *kaddr = page_address(page);
582585
unsigned from = ((char*)dir - kaddr) & ~(ext2_chunk_size(inode)-1);
583586
unsigned to = ((char *)dir - kaddr) +
@@ -601,8 +604,7 @@ int ext2_delete_entry (struct ext2_dir_entry_2 * dir, struct page * page )
601604
from = (char*)pde - (char*)page_address(page);
602605
pos = page_offset(page) + from;
603606
lock_page(page);
604-
err = __ext2_write_begin(NULL, page->mapping, pos, to - from, 0,
605-
&page, NULL);
607+
err = ext2_prepare_chunk(page, pos, to - from);
606608
BUG_ON(err);
607609
if (pde)
608610
pde->rec_len = ext2_rec_len_to_disk(to - from);
@@ -621,8 +623,7 @@ int ext2_delete_entry (struct ext2_dir_entry_2 * dir, struct page * page )
621623
*/
622624
int ext2_make_empty(struct inode *inode, struct inode *parent)
623625
{
624-
struct address_space *mapping = inode->i_mapping;
625-
struct page *page = grab_cache_page(mapping, 0);
626+
struct page *page = grab_cache_page(inode->i_mapping, 0);
626627
unsigned chunk_size = ext2_chunk_size(inode);
627628
struct ext2_dir_entry_2 * de;
628629
int err;
@@ -631,8 +632,7 @@ int ext2_make_empty(struct inode *inode, struct inode *parent)
631632
if (!page)
632633
return -ENOMEM;
633634

634-
err = __ext2_write_begin(NULL, page->mapping, 0, chunk_size, 0,
635-
&page, NULL);
635+
err = ext2_prepare_chunk(page, 0, chunk_size);
636636
if (err) {
637637
unlock_page(page);
638638
goto fail;

‎fs/ext2/ext2.h‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,6 @@ extern void ext2_set_inode_flags(struct inode *inode);
127127
extern void ext2_get_inode_flags(struct ext2_inode_info *);
128128
extern int ext2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
129129
u64 start, u64 len);
130-
int __ext2_write_begin(struct file *file, struct address_space *mapping,
131-
loff_t pos, unsigned len, unsigned flags,
132-
struct page **pagep, void **fsdata);
133130

134131
/* ioctl.c */
135132
extern long ext2_ioctl(struct file *, unsigned int, unsigned long);

‎fs/ext2/inode.c‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -765,14 +765,6 @@ ext2_readpages(struct file *file, struct address_space *mapping,
765765
return mpage_readpages(mapping, pages, nr_pages, ext2_get_block);
766766
}
767767

768-
int __ext2_write_begin(struct file *file, struct address_space *mapping,
769-
loff_t pos, unsigned len, unsigned flags,
770-
struct page **pagep, void **fsdata)
771-
{
772-
return block_write_begin_newtrunc(file, mapping, pos, len, flags,
773-
pagep, fsdata, ext2_get_block);
774-
}
775-
776768
static int
777769
ext2_write_begin(struct file *file, struct address_space *mapping,
778770
loff_t pos, unsigned len, unsigned flags,
@@ -781,7 +773,8 @@ ext2_write_begin(struct file *file, struct address_space *mapping,
781773
int ret;
782774

783775
*pagep = NULL;
784-
ret = __ext2_write_begin(file, mapping, pos, len, flags, pagep, fsdata);
776+
ret = block_write_begin_newtrunc(file, mapping, pos, len, flags,
777+
pagep, fsdata, ext2_get_block);
785778
if (ret < 0)
786779
ext2_write_failed(mapping, pos + len);
787780
return ret;

‎fs/minix/dir.c‎

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,7 @@ int minix_add_link(struct dentry *dentry, struct inode *inode)
271271

272272
got_it:
273273
pos = page_offset(page) + p - (char *)page_address(page);
274-
err = __minix_write_begin(NULL, page->mapping, pos, sbi->s_dirsize,
275-
AOP_FLAG_UNINTERRUPTIBLE, &page, NULL);
274+
err = minix_prepare_chunk(page, pos, sbi->s_dirsize);
276275
if (err)
277276
goto out_unlock;
278277
memcpy (namx, name, namelen);
@@ -297,17 +296,15 @@ int minix_add_link(struct dentry *dentry, struct inode *inode)
297296

298297
int minix_delete_entry(struct minix_dir_entry *de, struct page *page)
299298
{
300-
struct address_space *mapping = page->mapping;
301-
struct inode *inode = (struct inode*)mapping->host;
299+
struct inode *inode = page->mapping->host;
302300
char *kaddr = page_address(page);
303301
loff_t pos = page_offset(page) + (char*)de - kaddr;
304302
struct minix_sb_info *sbi = minix_sb(inode->i_sb);
305303
unsigned len = sbi->s_dirsize;
306304
int err;
307305

308306
lock_page(page);
309-
err = __minix_write_begin(NULL, mapping, pos, len,
310-
AOP_FLAG_UNINTERRUPTIBLE, &page, NULL);
307+
err = minix_prepare_chunk(page, pos, len);
311308
if (err == 0) {
312309
if (sbi->s_version == MINIX_V3)
313310
((minix3_dirent *) de)->inode = 0;
@@ -325,16 +322,14 @@ int minix_delete_entry(struct minix_dir_entry *de, struct page *page)
325322

326323
int minix_make_empty(struct inode *inode, struct inode *dir)
327324
{
328-
struct address_space *mapping = inode->i_mapping;
329-
struct page *page = grab_cache_page(mapping, 0);
325+
struct page *page = grab_cache_page(inode->i_mapping, 0);
330326
struct minix_sb_info *sbi = minix_sb(inode->i_sb);
331327
char *kaddr;
332328
int err;
333329

334330
if (!page)
335331
return -ENOMEM;
336-
err = __minix_write_begin(NULL, mapping, 0, 2 * sbi->s_dirsize,
337-
AOP_FLAG_UNINTERRUPTIBLE, &page, NULL);
332+
err = minix_prepare_chunk(page, 0, 2 * sbi->s_dirsize);
338333
if (err) {
339334
unlock_page(page);
340335
goto fail;
@@ -425,17 +420,15 @@ int minix_empty_dir(struct inode * inode)
425420
void minix_set_link(struct minix_dir_entry *de, struct page *page,
426421
struct inode *inode)
427422
{
428-
struct address_space *mapping = page->mapping;
429-
struct inode *dir = mapping->host;
423+
struct inode *dir = page->mapping->host;
430424
struct minix_sb_info *sbi = minix_sb(dir->i_sb);
431425
loff_t pos = page_offset(page) +
432426
(char *)de-(char*)page_address(page);
433427
int err;
434428

435429
lock_page(page);
436430

437-
err = __minix_write_begin(NULL, mapping, pos, sbi->s_dirsize,
438-
AOP_FLAG_UNINTERRUPTIBLE, &page, NULL);
431+
err = minix_prepare_chunk(page, pos, sbi->s_dirsize);
439432
if (err == 0) {
440433
if (sbi->s_version == MINIX_V3)
441434
((minix3_dirent *) de)->inode = inode->i_ino;

‎fs/minix/inode.c‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -357,20 +357,19 @@ static int minix_readpage(struct file *file, struct page *page)
357357
return block_read_full_page(page,minix_get_block);
358358
}
359359

360-
int __minix_write_begin(struct file *file, struct address_space *mapping,
361-
loff_t pos, unsigned len, unsigned flags,
362-
struct page **pagep, void **fsdata)
360+
int minix_prepare_chunk(struct page *page, loff_t pos, unsigned len)
363361
{
364-
return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
365-
minix_get_block);
362+
return block_write_begin_newtrunc(NULL, page->mapping, pos, len, 0,
363+
&page, NULL, minix_get_block);
366364
}
367365

368366
static int minix_write_begin(struct file *file, struct address_space *mapping,
369367
loff_t pos, unsigned len, unsigned flags,
370368
struct page **pagep, void **fsdata)
371369
{
372370
*pagep = NULL;
373-
return __minix_write_begin(file, mapping, pos, len, flags, pagep, fsdata);
371+
return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
372+
minix_get_block);
374373
}
375374

376375
static sector_t minix_bmap(struct address_space *mapping, sector_t block)

‎fs/minix/minix.h‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ extern int minix_new_block(struct inode * inode);
5353
extern void minix_free_block(struct inode *inode, unsigned long block);
5454
extern unsigned long minix_count_free_blocks(struct minix_sb_info *sbi);
5555
extern int minix_getattr(struct vfsmount *, struct dentry *, struct kstat *);
56-
extern int __minix_write_begin(struct file *file, struct address_space *mapping,
57-
loff_t pos, unsigned len, unsigned flags,
58-
struct page **pagep, void **fsdata);
56+
extern int minix_prepare_chunk(struct page *page, loff_t pos, unsigned len);
5957

6058
extern void V1_minix_truncate(struct inode *);
6159
extern void V2_minix_truncate(struct inode *);

‎fs/nilfs2/dir.c‎

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -80,23 +80,11 @@ static unsigned nilfs_last_byte(struct inode *inode, unsigned long page_nr)
8080
return last_byte;
8181
}
8282

83-
static int nilfs_prepare_chunk_uninterruptible(struct page *page,
84-
struct address_space *mapping,
85-
unsigned from, unsigned to)
83+
static int nilfs_prepare_chunk(struct page *page, unsigned from, unsigned to)
8684
{
8785
loff_t pos = page_offset(page) + from;
88-
return block_write_begin(NULL, mapping, pos, to - from,
89-
AOP_FLAG_UNINTERRUPTIBLE, &page,
90-
NULL, nilfs_get_block);
91-
}
92-
93-
static int nilfs_prepare_chunk(struct page *page,
94-
struct address_space *mapping,
95-
unsigned from, unsigned to)
96-
{
97-
loff_t pos = page_offset(page) + from;
98-
return block_write_begin(NULL, mapping, pos, to - from, 0, &page,
99-
NULL, nilfs_get_block);
86+
return block_write_begin_newtrunc(NULL, page->mapping, pos, to - from,
87+
0, &page, NULL, nilfs_get_block);
10088
}
10189

10290
static void nilfs_commit_chunk(struct page *page,
@@ -449,7 +437,7 @@ void nilfs_set_link(struct inode *dir, struct nilfs_dir_entry *de,
449437
int err;
450438

451439
lock_page(page);
452-
err = nilfs_prepare_chunk_uninterruptible(page, mapping, from, to);
440+
err = nilfs_prepare_chunk(page, from, to);
453441
BUG_ON(err);
454442
de->inode = cpu_to_le64(inode->i_ino);
455443
nilfs_set_de_type(de, inode);
@@ -530,7 +518,7 @@ int nilfs_add_link(struct dentry *dentry, struct inode *inode)
530518
got_it:
531519
from = (char *)de - (char *)page_address(page);
532520
to = from + rec_len;
533-
err = nilfs_prepare_chunk(page, page->mapping, from, to);
521+
err = nilfs_prepare_chunk(page, from, to);
534522
if (err)
535523
goto out_unlock;
536524
if (de->inode) {
@@ -587,7 +575,7 @@ int nilfs_delete_entry(struct nilfs_dir_entry *dir, struct page *page)
587575
if (pde)
588576
from = (char *)pde - (char *)page_address(page);
589577
lock_page(page);
590-
err = nilfs_prepare_chunk(page, mapping, from, to);
578+
err = nilfs_prepare_chunk(page, from, to);
591579
BUG_ON(err);
592580
if (pde)
593581
pde->rec_len = cpu_to_le16(to - from);
@@ -615,7 +603,7 @@ int nilfs_make_empty(struct inode *inode, struct inode *parent)
615603
if (!page)
616604
return -ENOMEM;
617605

618-
err = nilfs_prepare_chunk(page, mapping, 0, chunk_size);
606+
err = nilfs_prepare_chunk(page, 0, chunk_size);
619607
if (unlikely(err)) {
620608
unlock_page(page);
621609
goto fail;

‎fs/sysv/dir.c‎

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,7 @@ int sysv_add_link(struct dentry *dentry, struct inode *inode)
218218
pos = page_offset(page) +
219219
(char*)de - (char*)page_address(page);
220220
lock_page(page);
221-
err = __sysv_write_begin(NULL, page->mapping, pos, SYSV_DIRSIZE,
222-
AOP_FLAG_UNINTERRUPTIBLE, &page, NULL);
221+
err = sysv_prepare_chunk(page, pos, SYSV_DIRSIZE);
223222
if (err)
224223
goto out_unlock;
225224
memcpy (de->name, name, namelen);
@@ -239,15 +238,13 @@ int sysv_add_link(struct dentry *dentry, struct inode *inode)
239238

240239
int sysv_delete_entry(struct sysv_dir_entry *de, struct page *page)
241240
{
242-
struct address_space *mapping = page->mapping;
243-
struct inode *inode = (struct inode*)mapping->host;
241+
struct inode *inode = page->mapping->host;
244242
char *kaddr = (char*)page_address(page);
245243
loff_t pos = page_offset(page) + (char *)de - kaddr;
246244
int err;
247245

248246
lock_page(page);
249-
err = __sysv_write_begin(NULL, mapping, pos, SYSV_DIRSIZE,
250-
AOP_FLAG_UNINTERRUPTIBLE, &page, NULL);
247+
err = sysv_prepare_chunk(page, pos, SYSV_DIRSIZE);
251248
BUG_ON(err);
252249
de->inode = 0;
253250
err = dir_commit_chunk(page, pos, SYSV_DIRSIZE);
@@ -259,16 +256,14 @@ int sysv_delete_entry(struct sysv_dir_entry *de, struct page *page)
259256

260257
int sysv_make_empty(struct inode *inode, struct inode *dir)
261258
{
262-
struct address_space *mapping = inode->i_mapping;
263-
struct page *page = grab_cache_page(mapping, 0);
259+
struct page *page = grab_cache_page(inode->i_mapping, 0);
264260
struct sysv_dir_entry * de;
265261
char *base;
266262
int err;
267263

268264
if (!page)
269265
return -ENOMEM;
270-
err = __sysv_write_begin(NULL, mapping, 0, 2 * SYSV_DIRSIZE,
271-
AOP_FLAG_UNINTERRUPTIBLE, &page, NULL);
266+
err = sysv_prepare_chunk(page, 0, 2 * SYSV_DIRSIZE);
272267
if (err) {
273268
unlock_page(page);
274269
goto fail;
@@ -341,15 +336,13 @@ int sysv_empty_dir(struct inode * inode)
341336
void sysv_set_link(struct sysv_dir_entry *de, struct page *page,
342337
struct inode *inode)
343338
{
344-
struct address_space *mapping = page->mapping;
345-
struct inode *dir = mapping->host;
339+
struct inode *dir = page->mapping->host;
346340
loff_t pos = page_offset(page) +
347341
(char *)de-(char*)page_address(page);
348342
int err;
349343

350344
lock_page(page);
351-
err = __sysv_write_begin(NULL, mapping, pos, SYSV_DIRSIZE,
352-
AOP_FLAG_UNINTERRUPTIBLE, &page, NULL);
345+
err = sysv_prepare_chunk(page, pos, SYSV_DIRSIZE);
353346
BUG_ON(err);
354347
de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), inode->i_ino);
355348
err = dir_commit_chunk(page, pos, SYSV_DIRSIZE);

‎fs/sysv/itree.c‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -459,20 +459,19 @@ static int sysv_readpage(struct file *file, struct page *page)
459459
return block_read_full_page(page,get_block);
460460
}
461461

462-
int __sysv_write_begin(struct file *file, struct address_space *mapping,
463-
loff_t pos, unsigned len, unsigned flags,
464-
struct page **pagep, void **fsdata)
462+
int sysv_prepare_chunk(struct page *page, loff_t pos, unsigned len)
465463
{
466-
return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
467-
get_block);
464+
return block_write_begin_newtrunc(NULL, page->mapping, pos, len, 0,
465+
&page, NULL, get_block);
468466
}
469467

470468
static int sysv_write_begin(struct file *file, struct address_space *mapping,
471469
loff_t pos, unsigned len, unsigned flags,
472470
struct page **pagep, void **fsdata)
473471
{
474472
*pagep = NULL;
475-
return __sysv_write_begin(file, mapping, pos, len, flags, pagep, fsdata);
473+
return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
474+
get_block);
476475
}
477476

478477
static sector_t sysv_bmap(struct address_space *mapping, sector_t block)

‎fs/sysv/sysv.h‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,7 @@ extern unsigned long sysv_count_free_blocks(struct super_block *);
136136

137137
/* itree.c */
138138
extern void sysv_truncate(struct inode *);
139-
extern int __sysv_write_begin(struct file *file, struct address_space *mapping,
140-
loff_t pos, unsigned len, unsigned flags,
141-
struct page **pagep, void **fsdata);
139+
extern int sysv_prepare_chunk(struct page *page, loff_t pos, unsigned len);
142140

143141
/* inode.c */
144142
extern struct inode *sysv_iget(struct super_block *, unsigned int);

0 commit comments

Comments
 (0)