fs/buffer.c: Add generic_buffers_fsync*() implementation
authorRitesh Harjani (IBM) <ritesh.list@gmail.com>
Fri, 21 Apr 2023 09:46:12 +0000 (15:16 +0530)
committerJan Kara <jack@suse.cz>
Tue, 16 May 2023 09:32:42 +0000 (11:32 +0200)
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>

fs/buffer.c
include/linux/buffer_head.h

index a7fc561..00cad26 100644 (file)
@@ -592,6 +592,76 @@ int sync_mapping_buffers(struct address_space *mapping)
 }
 EXPORT_SYMBOL(sync_mapping_buffers);
 
+/**
+ * generic_buffers_fsync_noflush - generic buffer fsync implementation
+ * for simple filesystems with no inode lock
+ *
+ * @file:      file to synchronize
+ * @start:     start offset in bytes
+ * @end:       end offset in bytes (inclusive)
+ * @datasync:  only synchronize essential metadata if true
+ *
+ * This is a generic implementation of the fsync method for simple
+ * filesystems which track all non-inode metadata in the buffers list
+ * hanging off the address_space structure.
+ */
+int generic_buffers_fsync_noflush(struct file *file, loff_t start, loff_t end,
+                                 bool datasync)
+{
+       struct inode *inode = file->f_mapping->host;
+       int err;
+       int ret;
+
+       err = file_write_and_wait_range(file, start, end);
+       if (err)
+               return err;
+
+       ret = sync_mapping_buffers(inode->i_mapping);
+       if (!(inode->i_state & I_DIRTY_ALL))
+               goto out;
+       if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
+               goto out;
+
+       err = sync_inode_metadata(inode, 1);
+       if (ret == 0)
+               ret = err;
+
+out:
+       /* check and advance again to catch errors after syncing out buffers */
+       err = file_check_and_advance_wb_err(file);
+       if (ret == 0)
+               ret = err;
+       return ret;
+}
+EXPORT_SYMBOL(generic_buffers_fsync_noflush);
+
+/**
+ * generic_buffers_fsync - generic buffer fsync implementation
+ * for simple filesystems with no inode lock
+ *
+ * @file:      file to synchronize
+ * @start:     start offset in bytes
+ * @end:       end offset in bytes (inclusive)
+ * @datasync:  only synchronize essential metadata if true
+ *
+ * This is a generic implementation of the fsync method for simple
+ * filesystems which track all non-inode metadata in the buffers list
+ * hanging off the address_space structure. This also makes sure that
+ * a device cache flush operation is called at the end.
+ */
+int generic_buffers_fsync(struct file *file, loff_t start, loff_t end,
+                         bool datasync)
+{
+       struct inode *inode = file->f_mapping->host;
+       int ret;
+
+       ret = generic_buffers_fsync_noflush(file, start, end, datasync);
+       if (!ret)
+               ret = blkdev_issue_flush(inode->i_sb->s_bdev);
+       return ret;
+}
+EXPORT_SYMBOL(generic_buffers_fsync);
+
 /*
  * Called when we've recently written block `bblock', and it is known that
  * `bblock' was for a buffer_boundary() buffer.  This means that the block at
index 1520793..1bd73ce 100644 (file)
@@ -217,6 +217,10 @@ int inode_has_buffers(struct inode *);
 void invalidate_inode_buffers(struct inode *);
 int remove_inode_buffers(struct inode *inode);
 int sync_mapping_buffers(struct address_space *mapping);
+int generic_buffers_fsync_noflush(struct file *file, loff_t start, loff_t end,
+                                 bool datasync);
+int generic_buffers_fsync(struct file *file, loff_t start, loff_t end,
+                         bool datasync);
 void clean_bdev_aliases(struct block_device *bdev, sector_t block,
                        sector_t len);
 static inline void clean_bdev_bh_alias(struct buffer_head *bh)