diff mbox series

[v2,5/7] erofs: implement .mmap for page cache sharing

Message ID 20230111083158.23462-6-jefflexu@linux.alibaba.com (mailing list archive)
State New, archived
Headers show
Series erofs: support page cache sharing between EROFS images in fscache mode | expand

Commit Message

Jingbo Xu Jan. 11, 2023, 8:31 a.m. UTC
In mmap(2), replace vma->vm_file with the anonymous file associated with
the blob, so that the vma will be linked to the address_space of the
blob.

One thing worth noting is that, we return error early in mmap(2) if
users attempt to map beyond the file size.  Normally filesystems won't
restrict this in mmap(2).  The checking is done in the fault handler,
and SIGBUS will be signaled to users if they actually attempt to access
the area beyond the end of the file.  However since vma->vm_file has
been changed to the anonymous file in mmap(2), we can no way derive the
file size of the original file.  As file size is immutable in ro
filesystem, let's fail early in mmap(2) in this case.

Signed-off-by: Jingbo Xu <jefflexu@linux.alibaba.com>
---
 fs/erofs/fscache.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
diff mbox series

Patch

diff --git a/fs/erofs/fscache.c b/fs/erofs/fscache.c
index 4075d9519a7d..e3a65041c9b8 100644
--- a/fs/erofs/fscache.c
+++ b/fs/erofs/fscache.c
@@ -431,9 +431,40 @@  static ssize_t erofs_fscache_share_file_read_iter(struct kiocb *iocb,
 	return already_read ? already_read : ret;
 }
 
+vm_fault_t erofs_fscache_share_fault(struct vm_fault *vmf)
+{
+	struct file *realfile = vmf->vma->vm_file;
+	struct erofs_fscache_share_file_info *finfo = realfile->private_data;
+	pgoff_t index = vmf->pgoff;
+
+	if (unlikely(index >= finfo->max_idx))
+		return VM_FAULT_SIGBUS;
+
+	return filemap_fault(vmf);
+}
+
+static const struct vm_operations_struct erofs_fscache_share_file_vm_ops = {
+	.fault = erofs_fscache_share_fault,
+};
+
+static int erofs_fscache_share_file_mmap(struct file *file,
+					 struct vm_area_struct *vma)
+{
+	struct file *realfile = file->private_data;
+	struct erofs_fscache_share_file_info *finfo = realfile->private_data;
+
+	vma_set_file(vma, realfile);
+	vma->vm_pgoff = (finfo->pa >> PAGE_SHIFT) + vma->vm_pgoff;
+	vma->vm_ops = &erofs_fscache_share_file_vm_ops;
+
+	file_accessed(file);
+	return 0;
+}
+
 const struct file_operations erofs_fscache_share_file_fops = {
 	.llseek		= generic_file_llseek,
 	.read_iter	= erofs_fscache_share_file_read_iter,
+	.mmap		= erofs_fscache_share_file_mmap,
 	.open		= erofs_fscache_share_file_open,
 	.release	= erofs_fscache_share_file_release,
 };