Skip to content

Commit 1cf7a15

Browse files
committed
afs: Implement shared-writeable mmap
Implement shared-writeable mmap for AFS. Signed-off-by: David Howells <[email protected]>
1 parent 4343d00 commit 1cf7a15

File tree

3 files changed

+54
-9
lines changed

3 files changed

+54
-9
lines changed

fs/afs/file.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <linux/task_io_accounting_ops.h>
2020
#include "internal.h"
2121

22+
static int afs_file_mmap(struct file *file, struct vm_area_struct *vma);
2223
static int afs_readpage(struct file *file, struct page *page);
2324
static void afs_invalidatepage(struct page *page, unsigned int offset,
2425
unsigned int length);
@@ -34,7 +35,7 @@ const struct file_operations afs_file_operations = {
3435
.llseek = generic_file_llseek,
3536
.read_iter = generic_file_read_iter,
3637
.write_iter = afs_file_write,
37-
.mmap = generic_file_readonly_mmap,
38+
.mmap = afs_file_mmap,
3839
.splice_read = generic_file_splice_read,
3940
.fsync = afs_fsync,
4041
.lock = afs_lock,
@@ -61,6 +62,12 @@ const struct address_space_operations afs_fs_aops = {
6162
.writepages = afs_writepages,
6263
};
6364

65+
static const struct vm_operations_struct afs_vm_ops = {
66+
.fault = filemap_fault,
67+
.map_pages = filemap_map_pages,
68+
.page_mkwrite = afs_page_mkwrite,
69+
};
70+
6471
/*
6572
* Discard a pin on a writeback key.
6673
*/
@@ -629,3 +636,16 @@ static int afs_releasepage(struct page *page, gfp_t gfp_flags)
629636
_leave(" = T");
630637
return 1;
631638
}
639+
640+
/*
641+
* Handle setting up a memory mapping on an AFS file.
642+
*/
643+
static int afs_file_mmap(struct file *file, struct vm_area_struct *vma)
644+
{
645+
int ret;
646+
647+
ret = generic_file_mmap(file, vma);
648+
if (ret == 0)
649+
vma->vm_ops = &afs_vm_ops;
650+
return ret;
651+
}

fs/afs/internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,7 @@ extern void afs_pages_written_back(struct afs_vnode *, struct afs_call *);
886886
extern ssize_t afs_file_write(struct kiocb *, struct iov_iter *);
887887
extern int afs_flush(struct file *, fl_owner_t);
888888
extern int afs_fsync(struct file *, loff_t, loff_t, int);
889+
extern int afs_page_mkwrite(struct vm_fault *);
889890
extern void afs_prune_wb_keys(struct afs_vnode *);
890891
extern int afs_launder_page(struct page *);
891892

fs/afs/write.c

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -749,21 +749,45 @@ int afs_flush(struct file *file, fl_owner_t id)
749749
* notification that a previously read-only page is about to become writable
750750
* - if it returns an error, the caller will deliver a bus error signal
751751
*/
752-
int afs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
752+
int afs_page_mkwrite(struct vm_fault *vmf)
753753
{
754-
struct afs_vnode *vnode = AFS_FS_I(vma->vm_file->f_mapping->host);
754+
struct file *file = vmf->vma->vm_file;
755+
struct inode *inode = file_inode(file);
756+
struct afs_vnode *vnode = AFS_FS_I(inode);
757+
unsigned long priv;
755758

756759
_enter("{{%x:%u}},{%lx}",
757-
vnode->fid.vid, vnode->fid.vnode, page->index);
760+
vnode->fid.vid, vnode->fid.vnode, vmf->page->index);
758761

759-
/* wait for the page to be written to the cache before we allow it to
760-
* be modified */
762+
sb_start_pagefault(inode->i_sb);
763+
764+
/* Wait for the page to be written to the cache before we allow it to
765+
* be modified. We then assume the entire page will need writing back.
766+
*/
761767
#ifdef CONFIG_AFS_FSCACHE
762-
fscache_wait_on_page_write(vnode->cache, page);
768+
fscache_wait_on_page_write(vnode->cache, vmf->page);
763769
#endif
764770

765-
_leave(" = 0");
766-
return 0;
771+
if (PageWriteback(vmf->page) &&
772+
wait_on_page_bit_killable(vmf->page, PG_writeback) < 0)
773+
return VM_FAULT_RETRY;
774+
775+
if (lock_page_killable(vmf->page) < 0)
776+
return VM_FAULT_RETRY;
777+
778+
/* We mustn't change page->private until writeback is complete as that
779+
* details the portion of the page we need to write back and we might
780+
* need to redirty the page if there's a problem.
781+
*/
782+
wait_on_page_writeback(vmf->page);
783+
784+
priv = (unsigned long)PAGE_SIZE << AFS_PRIV_SHIFT; /* To */
785+
priv |= 0; /* From */
786+
SetPagePrivate(vmf->page);
787+
set_page_private(vmf->page, priv);
788+
789+
sb_end_pagefault(inode->i_sb);
790+
return VM_FAULT_LOCKED;
767791
}
768792

769793
/*

0 commit comments

Comments
 (0)