Skip to content

Commit 31b2ebc

Browse files
riteshharjanijankara
authored andcommitted
fs/buffer.c: Add generic_buffers_fsync*() implementation
Some of the higher layers like iomap takes inode_lock() when calling generic_write_sync(). Also writeback already happens from other paths without inode lock, so it's difficult to say that we really need sync_mapping_buffers() to take any inode locking here. Having said that, let's add generic_buffers_fsync/_noflush() implementation in buffer.c with no inode_lock/unlock() for now so that filesystems like ext2 and ext4's nojournal mode can use it. Ext4 when got converted to iomap for direct-io already copied it's own variant of __generic_file_fsync() without lock. This patch adds generic_buffers_fsync() & generic_buffers_fsync_noflush() implementations for use in filesystems like ext2 & ext4 respectively. Later we can review other filesystems as well to see if we can make generic_buffers_fsync/_noflush() which does not take any inode_lock() as the default path. Tested-by: Disha Goel <disgoel@linux.ibm.com> Reviewed-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Ritesh Harjani (IBM) <ritesh.list@gmail.com> Signed-off-by: Jan Kara <jack@suse.cz> Message-Id: <d573408ac8408627d23a3d2d166e748c172c4c9e.1682069716.git.ritesh.list@gmail.com>
1 parent fcced95 commit 31b2ebc

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

fs/buffer.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,76 @@ int sync_mapping_buffers(struct address_space *mapping)
592592
}
593593
EXPORT_SYMBOL(sync_mapping_buffers);
594594

595+
/**
596+
* generic_buffers_fsync_noflush - generic buffer fsync implementation
597+
* for simple filesystems with no inode lock
598+
*
599+
* @file: file to synchronize
600+
* @start: start offset in bytes
601+
* @end: end offset in bytes (inclusive)
602+
* @datasync: only synchronize essential metadata if true
603+
*
604+
* This is a generic implementation of the fsync method for simple
605+
* filesystems which track all non-inode metadata in the buffers list
606+
* hanging off the address_space structure.
607+
*/
608+
int generic_buffers_fsync_noflush(struct file *file, loff_t start, loff_t end,
609+
bool datasync)
610+
{
611+
struct inode *inode = file->f_mapping->host;
612+
int err;
613+
int ret;
614+
615+
err = file_write_and_wait_range(file, start, end);
616+
if (err)
617+
return err;
618+
619+
ret = sync_mapping_buffers(inode->i_mapping);
620+
if (!(inode->i_state & I_DIRTY_ALL))
621+
goto out;
622+
if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
623+
goto out;
624+
625+
err = sync_inode_metadata(inode, 1);
626+
if (ret == 0)
627+
ret = err;
628+
629+
out:
630+
/* check and advance again to catch errors after syncing out buffers */
631+
err = file_check_and_advance_wb_err(file);
632+
if (ret == 0)
633+
ret = err;
634+
return ret;
635+
}
636+
EXPORT_SYMBOL(generic_buffers_fsync_noflush);
637+
638+
/**
639+
* generic_buffers_fsync - generic buffer fsync implementation
640+
* for simple filesystems with no inode lock
641+
*
642+
* @file: file to synchronize
643+
* @start: start offset in bytes
644+
* @end: end offset in bytes (inclusive)
645+
* @datasync: only synchronize essential metadata if true
646+
*
647+
* This is a generic implementation of the fsync method for simple
648+
* filesystems which track all non-inode metadata in the buffers list
649+
* hanging off the address_space structure. This also makes sure that
650+
* a device cache flush operation is called at the end.
651+
*/
652+
int generic_buffers_fsync(struct file *file, loff_t start, loff_t end,
653+
bool datasync)
654+
{
655+
struct inode *inode = file->f_mapping->host;
656+
int ret;
657+
658+
ret = generic_buffers_fsync_noflush(file, start, end, datasync);
659+
if (!ret)
660+
ret = blkdev_issue_flush(inode->i_sb->s_bdev);
661+
return ret;
662+
}
663+
EXPORT_SYMBOL(generic_buffers_fsync);
664+
595665
/*
596666
* Called when we've recently written block `bblock', and it is known that
597667
* `bblock' was for a buffer_boundary() buffer. This means that the block at

include/linux/buffer_head.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@ int inode_has_buffers(struct inode *);
217217
void invalidate_inode_buffers(struct inode *);
218218
int remove_inode_buffers(struct inode *inode);
219219
int sync_mapping_buffers(struct address_space *mapping);
220+
int generic_buffers_fsync_noflush(struct file *file, loff_t start, loff_t end,
221+
bool datasync);
222+
int generic_buffers_fsync(struct file *file, loff_t start, loff_t end,
223+
bool datasync);
220224
void clean_bdev_aliases(struct block_device *bdev, sector_t block,
221225
sector_t len);
222226
static inline void clean_bdev_bh_alias(struct buffer_head *bh)

0 commit comments

Comments
 (0)