1
2
3
4
5
6
7
8
9#include <linux/stat.h>
10#include <linux/time.h>
11#include <linux/kernel.h>
12#include <linux/mm.h>
13#include <linux/shm.h>
14#include <linux/errno.h>
15#include <linux/mman.h>
16#include <linux/string.h>
17#include <linux/slab.h>
18#include <linux/fcntl.h>
19#include <linux/ncp_fs.h>
20
21#include "ncplib_kernel.h"
22#include <asm/uaccess.h>
23#include <asm/system.h>
24
25
26
27
28
29
30static int ncp_file_mmap_fault(struct vm_area_struct *area,
31 struct vm_fault *vmf)
32{
33 struct file *file = area->vm_file;
34 struct dentry *dentry = file->f_path.dentry;
35 struct inode *inode = dentry->d_inode;
36 char *pg_addr;
37 unsigned int already_read;
38 unsigned int count;
39 int bufsize;
40 int pos;
41
42
43
44
45
46 vmf->page = alloc_page(GFP_HIGHUSER);
47 if (!vmf->page)
48 return VM_FAULT_OOM;
49 pg_addr = kmap(vmf->page);
50 pos = vmf->pgoff << PAGE_SHIFT;
51
52 count = PAGE_SIZE;
53
54 bufsize = NCP_SERVER(inode)->buffer_size;
55
56 already_read = 0;
57 if (ncp_make_open(inode, O_RDONLY) >= 0) {
58 while (already_read < count) {
59 int read_this_time;
60 int to_read;
61
62 to_read = bufsize - (pos % bufsize);
63
64 to_read = min_t(unsigned int, to_read, count - already_read);
65
66 if (ncp_read_kernel(NCP_SERVER(inode),
67 NCP_FINFO(inode)->file_handle,
68 pos, to_read,
69 pg_addr + already_read,
70 &read_this_time) != 0) {
71 read_this_time = 0;
72 }
73 pos += read_this_time;
74 already_read += read_this_time;
75
76 if (read_this_time < to_read) {
77 break;
78 }
79 }
80 ncp_inode_close(inode);
81
82 }
83
84 if (already_read < PAGE_SIZE)
85 memset(pg_addr + already_read, 0, PAGE_SIZE - already_read);
86 flush_dcache_page(vmf->page);
87 kunmap(vmf->page);
88
89
90
91
92
93
94 count_vm_event(PGMAJFAULT);
95 return VM_FAULT_MAJOR;
96}
97
98static const struct vm_operations_struct ncp_file_mmap =
99{
100 .fault = ncp_file_mmap_fault,
101};
102
103
104
105int ncp_mmap(struct file *file, struct vm_area_struct *vma)
106{
107 struct inode *inode = file->f_path.dentry->d_inode;
108
109 DPRINTK("ncp_mmap: called\n");
110
111 if (!ncp_conn_valid(NCP_SERVER(inode)))
112 return -EIO;
113
114
115 if (vma->vm_flags & VM_SHARED)
116 return -EINVAL;
117
118
119 if (((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff
120 > (1U << (32 - PAGE_SHIFT)))
121 return -EFBIG;
122
123 vma->vm_ops = &ncp_file_mmap;
124 file_accessed(file);
125 return 0;
126}
127