af48b792ca04e2722297b8cac9305b347ef0e9c2
[safe/jmp/linux-2.6] / fs / ncpfs / mmap.c
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>
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  * Fill in the supplied page for mmap
27  */
28 static struct page* ncp_file_mmap_fault(struct vm_area_struct *area,
29                                                 struct fault_data *fdata)
30 {
31         struct file *file = area->vm_file;
32         struct dentry *dentry = file->f_path.dentry;
33         struct inode *inode = dentry->d_inode;
34         struct page* page;
35         char *pg_addr;
36         unsigned int already_read;
37         unsigned int count;
38         int bufsize;
39         int pos;
40
41         page = alloc_page(GFP_HIGHUSER); /* ncpfs has nothing against high pages
42                    as long as recvmsg and memset works on it */
43         if (!page) {
44                 fdata->type = VM_FAULT_OOM;
45                 return NULL;
46         }
47         pg_addr = kmap(page);
48         pos = fdata->pgoff << PAGE_SHIFT;
49
50         count = PAGE_SIZE;
51         if (fdata->address + PAGE_SIZE > area->vm_end) {
52                 WARN_ON(1); /* shouldn't happen? */
53                 count = area->vm_end - fdata->address;
54         }
55         /* what we can read in one go */
56         bufsize = NCP_SERVER(inode)->buffer_size;
57
58         already_read = 0;
59         if (ncp_make_open(inode, O_RDONLY) >= 0) {
60                 while (already_read < count) {
61                         int read_this_time;
62                         int to_read;
63
64                         to_read = bufsize - (pos % bufsize);
65
66                         to_read = min_t(unsigned int, to_read, count - already_read);
67
68                         if (ncp_read_kernel(NCP_SERVER(inode),
69                                      NCP_FINFO(inode)->file_handle,
70                                      pos, to_read,
71                                      pg_addr + already_read,
72                                      &read_this_time) != 0) {
73                                 read_this_time = 0;
74                         }
75                         pos += read_this_time;
76                         already_read += read_this_time;
77
78                         if (read_this_time < to_read) {
79                                 break;
80                         }
81                 }
82                 ncp_inode_close(inode);
83
84         }
85
86         if (already_read < PAGE_SIZE)
87                 memset(pg_addr + already_read, 0, PAGE_SIZE - already_read);
88         flush_dcache_page(page);
89         kunmap(page);
90
91         /*
92          * If I understand ncp_read_kernel() properly, the above always
93          * fetches from the network, here the analogue of disk.
94          * -- wli
95          */
96         fdata->type = VM_FAULT_MAJOR;
97         count_vm_event(PGMAJFAULT);
98         return page;
99 }
100
101 static struct vm_operations_struct ncp_file_mmap =
102 {
103         .fault = ncp_file_mmap_fault,
104 };
105
106
107 /* This is used for a general mmap of a ncp file */
108 int ncp_mmap(struct file *file, struct vm_area_struct *vma)
109 {
110         struct inode *inode = file->f_path.dentry->d_inode;
111         
112         DPRINTK("ncp_mmap: called\n");
113
114         if (!ncp_conn_valid(NCP_SERVER(inode)))
115                 return -EIO;
116
117         /* only PAGE_COW or read-only supported now */
118         if (vma->vm_flags & VM_SHARED)
119                 return -EINVAL;
120         /* we do not support files bigger than 4GB... We eventually 
121            supports just 4GB... */
122         if (((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff 
123            > (1U << (32 - PAGE_SHIFT)))
124                 return -EFBIG;
125
126         vma->vm_ops = &ncp_file_mmap;
127         vma->vm_flags |= VM_CAN_INVALIDATE;
128         file_accessed(file);
129         return 0;
130 }