diff mbox series

[RFC,5/5] btrfs: function to convert file offset to device offset

Message ID 2be14d6e2e1e888f2a0f1f272c1fd6cc0b681e97.1634933122.git.rgoldwyn@suse.com (mailing list archive)
State New, archived
Headers show
Series Shared memory for shared extents | expand

Commit Message

Goldwyn Rodrigues Oct. 22, 2021, 8:15 p.m. UTC
From: Goldwyn Rodrigues <rgoldwyn@suse.com>

btrfs_file_to_device_offset() converts a file offset to device offset.
It also calculates the last_index which represents the last page in the
range which is within the extent.

btrfs_file_to_device_offset() is only passed conditionally based on if
BTRFS_SHAREDEXT is set in the mount flag.

Signed-off-by: Goldwyn Rodrigues <rgoldwyn@suse.com>
---
 fs/btrfs/file.c | 42 ++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 40 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c
index e171d822a05e..f0b97d020575 100644
--- a/fs/btrfs/file.c
+++ b/fs/btrfs/file.c
@@ -3643,18 +3643,56 @@  static ssize_t btrfs_direct_read(struct kiocb *iocb, struct iov_iter *to)
 	return ret;
 }
 
+static pgoff_t btrfs_file_offset_to_device(struct file *filp, loff_t pos,
+		size_t len, pgoff_t *last_index)
+{
+	struct extent_map *em;
+	struct btrfs_inode *inode = BTRFS_I(file_inode(filp));
+	u64 device_offset;
+	u64 device_len;
+
+	if (inode->flags & BTRFS_INODE_NODATACOW)
+		return 0;
+
+	em = btrfs_get_extent(inode, NULL, 0, pos, len);
+
+	device_offset = em->block_start;
+	if (device_offset == EXTENT_MAP_HOLE) {
+		free_extent_map(em);
+		return 0;
+	}
+
+	/* Delalloc should be in file's pagecache */
+	BUG_ON(device_offset == EXTENT_MAP_DELALLOC);
+
+	device_offset = (device_offset + (pos - em->start)) >> PAGE_SHIFT;
+	device_len = (em->len - (pos - em->start)) >> PAGE_SHIFT;
+	*last_index = device_offset + device_len;
+
+	free_extent_map(em);
+
+	return device_offset;
+}
+
 static ssize_t btrfs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
 {
 	ssize_t ret = 0;
+	struct inode *inode = file_inode(iocb->ki_filp);
+	struct btrfs_fs_info *fs_info;
+	file_offset_to_device_t file_offset_to_device = NULL;
 
 	if (iocb->ki_flags & IOCB_DIRECT) {
 		ret = btrfs_direct_read(iocb, to);
 		if (ret < 0 || !iov_iter_count(to) ||
-		    iocb->ki_pos >= i_size_read(file_inode(iocb->ki_filp)))
+		    iocb->ki_pos >= i_size_read(inode))
 			return ret;
 	}
 
-	return filemap_read(iocb, to, ret, NULL);
+	fs_info = btrfs_sb(inode->i_sb);
+	if (btrfs_test_opt(fs_info, SHAREDEXT))
+		file_offset_to_device = btrfs_file_offset_to_device;
+
+	return filemap_read(iocb, to, ret, file_offset_to_device);
 }
 
 const struct file_operations btrfs_file_operations = {