[08/10] btrfs: dax add read mmap path
diff mbox series

Message ID 20181205122835.19290-9-rgoldwyn@suse.de
State New
Headers show
Series
  • btrfs: Support for DAX devices
Related show

Commit Message

Goldwyn Rodrigues Dec. 5, 2018, 12:28 p.m. UTC
From: Goldwyn Rodrigues <rgoldwyn@suse.com>

Signed-off-by: Goldwyn Rodrigues <rgoldwyn@suse.com>
---
 fs/btrfs/ctree.h |  1 +
 fs/btrfs/dax.c   | 43 +++++++++++++++++++++++++++++++++++++++++++
 fs/btrfs/file.c  | 12 +++++++++++-
 3 files changed, 55 insertions(+), 1 deletion(-)

Patch
diff mbox series

diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index d91ff283a966..33648121ca52 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -3694,6 +3694,7 @@  int btree_readahead_hook(struct extent_buffer *eb, int err);
 /* dax.c */
 ssize_t btrfs_file_dax_read(struct kiocb *iocb, struct iov_iter *to);
 ssize_t btrfs_file_dax_write(struct kiocb *iocb, struct iov_iter *from);
+vm_fault_t btrfs_dax_fault(struct vm_fault *vmf);
 #endif /* CONFIG_FS_DAX */
 
 static inline int is_fstree(u64 rootid)
diff --git a/fs/btrfs/dax.c b/fs/btrfs/dax.c
index 4000259a426c..88017f8799d1 100644
--- a/fs/btrfs/dax.c
+++ b/fs/btrfs/dax.c
@@ -190,5 +190,48 @@  ssize_t btrfs_file_dax_write(struct kiocb *iocb, struct iov_iter *from)
 				count - done, true);
 	extent_changeset_free(data_reserved);
         return done ? done : ret;
+}
+
+/* As copied from dax_iomap_pte_fault() */
+vm_fault_t btrfs_dax_fault(struct vm_fault *vmf)
+{
+	pfn_t pfn;
+	struct address_space *mapping = vmf->vma->vm_file->f_mapping;
+	XA_STATE(xas, &mapping->i_pages, vmf->pgoff);
+	struct inode *inode = mapping->host;
+	loff_t pos = (loff_t)vmf->pgoff << PAGE_SHIFT;
+	void *entry = NULL;
+	vm_fault_t ret = 0;
+
+	if (pos > i_size_read(inode)) {
+		ret = VM_FAULT_SIGBUS;
+		goto out;
+	}
 
+	entry = grab_mapping_entry(&xas, mapping, 0);
+	if (IS_ERR(entry)) {
+		ret = dax_fault_return(PTR_ERR(entry));
+		goto out;
+	}
+
+	if (!vmf->cow_page) {
+        	sector_t sector;
+		struct extent_map *em;
+                em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, pos, PAGE_SIZE, 0);
+		if (em->block_start == EXTENT_MAP_HOLE) {
+			ret = dax_load_hole(&xas, mapping, entry, vmf);
+			goto out;
+		}
+	        sector = ((get_start_sect(em->bdev) << 9) +
+			  (em->block_start + (pos - em->start))) >> 9;
+		ret = dax_pfn(fs_dax_get_by_bdev(em->bdev), em->bdev, sector, PAGE_SIZE, &pfn);
+		if (ret)
+			goto out;
+		dax_insert_entry(&xas, mapping, vmf, entry, pfn, 0, false);
+		ret = vmf_insert_mixed(vmf->vma, vmf->address, pfn);
+	}
+out:
+	if (entry)
+		dax_unlock_entry(&xas, entry);
+	return ret;
 }
diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c
index 29a3b12e6660..38b494686fb2 100644
--- a/fs/btrfs/file.c
+++ b/fs/btrfs/file.c
@@ -2227,8 +2227,18 @@  int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
 	return ret > 0 ? -EIO : ret;
 }
 
+static vm_fault_t btrfs_fault(struct vm_fault *vmf)
+{
+	struct inode *inode = vmf->vma->vm_file->f_mapping->host;
+#ifdef CONFIG_FS_DAX
+	if (IS_DAX(inode))
+		return btrfs_dax_fault(vmf);
+#endif
+	return filemap_fault(vmf);
+}
+
 static const struct vm_operations_struct btrfs_file_vm_ops = {
-	.fault		= filemap_fault,
+	.fault		= btrfs_fault,
 	.map_pages	= filemap_map_pages,
 	.page_mkwrite	= btrfs_page_mkwrite,
 };