afs: Implement shared-writeable mmap
authorDavid Howells <dhowells@redhat.com>
Thu, 2 Nov 2017 15:27:52 +0000 (15:27 +0000)
committerDavid Howells <dhowells@redhat.com>
Mon, 13 Nov 2017 15:38:21 +0000 (15:38 +0000)
Implement shared-writeable mmap for AFS.

Signed-off-by: David Howells <dhowells@redhat.com>
fs/afs/file.c
fs/afs/internal.h
fs/afs/write.c

index c3a7bc1..675c5c2 100644 (file)
@@ -19,6 +19,7 @@
 #include <linux/task_io_accounting_ops.h>
 #include "internal.h"
 
+static int afs_file_mmap(struct file *file, struct vm_area_struct *vma);
 static int afs_readpage(struct file *file, struct page *page);
 static void afs_invalidatepage(struct page *page, unsigned int offset,
                               unsigned int length);
@@ -34,7 +35,7 @@ const struct file_operations afs_file_operations = {
        .llseek         = generic_file_llseek,
        .read_iter      = generic_file_read_iter,
        .write_iter     = afs_file_write,
-       .mmap           = generic_file_readonly_mmap,
+       .mmap           = afs_file_mmap,
        .splice_read    = generic_file_splice_read,
        .fsync          = afs_fsync,
        .lock           = afs_lock,
@@ -61,6 +62,12 @@ const struct address_space_operations afs_fs_aops = {
        .writepages     = afs_writepages,
 };
 
+static const struct vm_operations_struct afs_vm_ops = {
+       .fault          = filemap_fault,
+       .map_pages      = filemap_map_pages,
+       .page_mkwrite   = afs_page_mkwrite,
+};
+
 /*
  * Discard a pin on a writeback key.
  */
@@ -629,3 +636,16 @@ static int afs_releasepage(struct page *page, gfp_t gfp_flags)
        _leave(" = T");
        return 1;
 }
+
+/*
+ * Handle setting up a memory mapping on an AFS file.
+ */
+static int afs_file_mmap(struct file *file, struct vm_area_struct *vma)
+{
+       int ret;
+
+       ret = generic_file_mmap(file, vma);
+       if (ret == 0)
+               vma->vm_ops = &afs_vm_ops;
+       return ret;
+}
index 688562a..1de36e6 100644 (file)
@@ -886,6 +886,7 @@ extern void afs_pages_written_back(struct afs_vnode *, struct afs_call *);
 extern ssize_t afs_file_write(struct kiocb *, struct iov_iter *);
 extern int afs_flush(struct file *, fl_owner_t);
 extern int afs_fsync(struct file *, loff_t, loff_t, int);
+extern int afs_page_mkwrite(struct vm_fault *);
 extern void afs_prune_wb_keys(struct afs_vnode *);
 extern int afs_launder_page(struct page *);
 
index 4c13137..6807277 100644 (file)
@@ -749,21 +749,45 @@ int afs_flush(struct file *file, fl_owner_t id)
  * notification that a previously read-only page is about to become writable
  * - if it returns an error, the caller will deliver a bus error signal
  */
-int afs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
+int afs_page_mkwrite(struct vm_fault *vmf)
 {
-       struct afs_vnode *vnode = AFS_FS_I(vma->vm_file->f_mapping->host);
+       struct file *file = vmf->vma->vm_file;
+       struct inode *inode = file_inode(file);
+       struct afs_vnode *vnode = AFS_FS_I(inode);
+       unsigned long priv;
 
        _enter("{{%x:%u}},{%lx}",
-              vnode->fid.vid, vnode->fid.vnode, page->index);
+              vnode->fid.vid, vnode->fid.vnode, vmf->page->index);
 
-       /* wait for the page to be written to the cache before we allow it to
-        * be modified */
+       sb_start_pagefault(inode->i_sb);
+
+       /* Wait for the page to be written to the cache before we allow it to
+        * be modified.  We then assume the entire page will need writing back.
+        */
 #ifdef CONFIG_AFS_FSCACHE
-       fscache_wait_on_page_write(vnode->cache, page);
+       fscache_wait_on_page_write(vnode->cache, vmf->page);
 #endif
 
-       _leave(" = 0");
-       return 0;
+       if (PageWriteback(vmf->page) &&
+           wait_on_page_bit_killable(vmf->page, PG_writeback) < 0)
+               return VM_FAULT_RETRY;
+
+       if (lock_page_killable(vmf->page) < 0)
+               return VM_FAULT_RETRY;
+
+       /* We mustn't change page->private until writeback is complete as that
+        * details the portion of the page we need to write back and we might
+        * need to redirty the page if there's a problem.
+        */
+       wait_on_page_writeback(vmf->page);
+
+       priv = (unsigned long)PAGE_SIZE << AFS_PRIV_SHIFT; /* To */
+       priv |= 0; /* From */
+       SetPagePrivate(vmf->page);
+       set_page_private(vmf->page, priv);
+
+       sb_end_pagefault(inode->i_sb);
+       return VM_FAULT_LOCKED;
 }
 
 /*