Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * mmap.c |
| 3 | * |
| 4 | * Copyright (C) 1995, 1996 by Volker Lendecke |
| 5 | * Modified 1997 Peter Waltenberg, Bill Hawes, David Woodhouse for 2.1 dcache |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #include <linux/stat.h> |
| 10 | #include <linux/time.h> |
| 11 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/mm.h> |
| 14 | #include <linux/shm.h> |
| 15 | #include <linux/errno.h> |
| 16 | #include <linux/mman.h> |
| 17 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/fcntl.h> |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 19 | #include <linux/memcontrol.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <asm/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Al Viro | 32c419d | 2011-01-12 17:37:47 -0500 | [diff] [blame] | 23 | #include "ncp_fs.h" |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | /* |
| 26 | * Fill in the supplied page for mmap |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 27 | * XXX: how are we excluding truncate/invalidate here? Maybe need to lock |
| 28 | * page? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | */ |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 30 | static int ncp_file_mmap_fault(struct vm_area_struct *area, |
| 31 | struct vm_fault *vmf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | { |
Al Viro | a67f797 | 2014-10-31 02:41:28 -0400 | [diff] [blame] | 33 | struct inode *inode = file_inode(area->vm_file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | char *pg_addr; |
| 35 | unsigned int already_read; |
| 36 | unsigned int count; |
| 37 | int bufsize; |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 38 | int pos; /* XXX: loff_t ? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 40 | /* |
| 41 | * ncpfs has nothing against high pages as long |
| 42 | * as recvmsg and memset works on it |
| 43 | */ |
| 44 | vmf->page = alloc_page(GFP_HIGHUSER); |
| 45 | if (!vmf->page) |
| 46 | return VM_FAULT_OOM; |
| 47 | pg_addr = kmap(vmf->page); |
| 48 | pos = vmf->pgoff << PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
| 50 | count = PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | /* what we can read in one go */ |
| 52 | bufsize = NCP_SERVER(inode)->buffer_size; |
| 53 | |
| 54 | already_read = 0; |
| 55 | if (ncp_make_open(inode, O_RDONLY) >= 0) { |
| 56 | while (already_read < count) { |
| 57 | int read_this_time; |
| 58 | int to_read; |
| 59 | |
| 60 | to_read = bufsize - (pos % bufsize); |
| 61 | |
| 62 | to_read = min_t(unsigned int, to_read, count - already_read); |
| 63 | |
| 64 | if (ncp_read_kernel(NCP_SERVER(inode), |
| 65 | NCP_FINFO(inode)->file_handle, |
| 66 | pos, to_read, |
| 67 | pg_addr + already_read, |
| 68 | &read_this_time) != 0) { |
| 69 | read_this_time = 0; |
| 70 | } |
| 71 | pos += read_this_time; |
| 72 | already_read += read_this_time; |
| 73 | |
| 74 | if (read_this_time < to_read) { |
| 75 | break; |
| 76 | } |
| 77 | } |
| 78 | ncp_inode_close(inode); |
| 79 | |
| 80 | } |
| 81 | |
| 82 | if (already_read < PAGE_SIZE) |
| 83 | memset(pg_addr + already_read, 0, PAGE_SIZE - already_read); |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 84 | flush_dcache_page(vmf->page); |
| 85 | kunmap(vmf->page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
| 87 | /* |
| 88 | * If I understand ncp_read_kernel() properly, the above always |
| 89 | * fetches from the network, here the analogue of disk. |
Nadia Yvette Chambers | 6d49e35 | 2012-12-06 10:39:54 +0100 | [diff] [blame] | 90 | * -- nyc |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | */ |
Christoph Lameter | f8891e5 | 2006-06-30 01:55:45 -0700 | [diff] [blame] | 92 | count_vm_event(PGMAJFAULT); |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 93 | mem_cgroup_count_vm_event(area->vm_mm, PGMAJFAULT); |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 94 | return VM_FAULT_MAJOR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Alexey Dobriyan | f0f37e2 | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 97 | static const struct vm_operations_struct ncp_file_mmap = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | { |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 99 | .fault = ncp_file_mmap_fault, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | |
| 103 | /* This is used for a general mmap of a ncp file */ |
| 104 | int ncp_mmap(struct file *file, struct vm_area_struct *vma) |
| 105 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 106 | struct inode *inode = file_inode(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
Joe Perches | d3b73ca | 2014-04-08 16:04:15 -0700 | [diff] [blame] | 108 | ncp_dbg(1, "called\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
| 110 | if (!ncp_conn_valid(NCP_SERVER(inode))) |
| 111 | return -EIO; |
| 112 | |
| 113 | /* only PAGE_COW or read-only supported now */ |
| 114 | if (vma->vm_flags & VM_SHARED) |
| 115 | return -EINVAL; |
| 116 | /* we do not support files bigger than 4GB... We eventually |
| 117 | supports just 4GB... */ |
Libin | ef9f515 | 2013-07-03 15:01:26 -0700 | [diff] [blame] | 118 | if (vma_pages(vma) + vma->vm_pgoff |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | > (1U << (32 - PAGE_SHIFT))) |
| 120 | return -EFBIG; |
| 121 | |
| 122 | vma->vm_ops = &ncp_file_mmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | file_accessed(file); |
| 124 | return 0; |
| 125 | } |