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> |
| 22 | #include <asm/system.h> |
| 23 | |
Al Viro | 32c419d | 2011-01-12 17:37:47 -0500 | [diff] [blame] | 24 | #include "ncp_fs.h" |
| 25 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | /* |
| 27 | * Fill in the supplied page for mmap |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 28 | * XXX: how are we excluding truncate/invalidate here? Maybe need to lock |
| 29 | * page? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | */ |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 31 | static int ncp_file_mmap_fault(struct vm_area_struct *area, |
| 32 | struct vm_fault *vmf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | { |
| 34 | struct file *file = area->vm_file; |
Josef Sipek | 92e5bae | 2006-12-08 02:37:22 -0800 | [diff] [blame] | 35 | struct dentry *dentry = file->f_path.dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | struct inode *inode = dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | char *pg_addr; |
| 38 | unsigned int already_read; |
| 39 | unsigned int count; |
| 40 | int bufsize; |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 41 | int pos; /* XXX: loff_t ? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 43 | /* |
| 44 | * ncpfs has nothing against high pages as long |
| 45 | * as recvmsg and memset works on it |
| 46 | */ |
| 47 | vmf->page = alloc_page(GFP_HIGHUSER); |
| 48 | if (!vmf->page) |
| 49 | return VM_FAULT_OOM; |
| 50 | pg_addr = kmap(vmf->page); |
| 51 | pos = vmf->pgoff << PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
| 53 | count = PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | /* what we can read in one go */ |
| 55 | bufsize = NCP_SERVER(inode)->buffer_size; |
| 56 | |
| 57 | already_read = 0; |
| 58 | if (ncp_make_open(inode, O_RDONLY) >= 0) { |
| 59 | while (already_read < count) { |
| 60 | int read_this_time; |
| 61 | int to_read; |
| 62 | |
| 63 | to_read = bufsize - (pos % bufsize); |
| 64 | |
| 65 | to_read = min_t(unsigned int, to_read, count - already_read); |
| 66 | |
| 67 | if (ncp_read_kernel(NCP_SERVER(inode), |
| 68 | NCP_FINFO(inode)->file_handle, |
| 69 | pos, to_read, |
| 70 | pg_addr + already_read, |
| 71 | &read_this_time) != 0) { |
| 72 | read_this_time = 0; |
| 73 | } |
| 74 | pos += read_this_time; |
| 75 | already_read += read_this_time; |
| 76 | |
| 77 | if (read_this_time < to_read) { |
| 78 | break; |
| 79 | } |
| 80 | } |
| 81 | ncp_inode_close(inode); |
| 82 | |
| 83 | } |
| 84 | |
| 85 | if (already_read < PAGE_SIZE) |
| 86 | memset(pg_addr + already_read, 0, PAGE_SIZE - already_read); |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 87 | flush_dcache_page(vmf->page); |
| 88 | kunmap(vmf->page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
| 90 | /* |
| 91 | * If I understand ncp_read_kernel() properly, the above always |
| 92 | * fetches from the network, here the analogue of disk. |
| 93 | * -- wli |
| 94 | */ |
Christoph Lameter | f8891e5 | 2006-06-30 01:55:45 -0700 | [diff] [blame] | 95 | count_vm_event(PGMAJFAULT); |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame^] | 96 | mem_cgroup_count_vm_event(area->vm_mm, PGMAJFAULT); |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 97 | return VM_FAULT_MAJOR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Alexey Dobriyan | f0f37e2 | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 100 | static const struct vm_operations_struct ncp_file_mmap = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | { |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 102 | .fault = ncp_file_mmap_fault, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | |
| 106 | /* This is used for a general mmap of a ncp file */ |
| 107 | int ncp_mmap(struct file *file, struct vm_area_struct *vma) |
| 108 | { |
Josef Sipek | 92e5bae | 2006-12-08 02:37:22 -0800 | [diff] [blame] | 109 | struct inode *inode = file->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
| 111 | DPRINTK("ncp_mmap: called\n"); |
| 112 | |
| 113 | if (!ncp_conn_valid(NCP_SERVER(inode))) |
| 114 | return -EIO; |
| 115 | |
| 116 | /* only PAGE_COW or read-only supported now */ |
| 117 | if (vma->vm_flags & VM_SHARED) |
| 118 | return -EINVAL; |
| 119 | /* we do not support files bigger than 4GB... We eventually |
| 120 | supports just 4GB... */ |
| 121 | if (((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff |
| 122 | > (1U << (32 - PAGE_SHIFT))) |
| 123 | return -EFBIG; |
| 124 | |
| 125 | vma->vm_ops = &ncp_file_mmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | file_accessed(file); |
| 127 | return 0; |
| 128 | } |