Linux 2.2.3pre3
[davej-history.git] / fs / ncpfs / mmap.c
blob6b321e6c984a571d57414a0d235efae57043557d
1 /*
2 * mmap.c
4 * Copyright (C) 1995, 1996 by Volker Lendecke
5 * Modified 1997 Peter Waltenberg, Bill Hawes, David Woodhouse for 2.1 dcache
7 */
9 #include <linux/stat.h>
10 #include <linux/sched.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/malloc.h>
18 #include <linux/fcntl.h>
19 #include <linux/ncp_fs.h>
21 #include "ncplib_kernel.h"
22 #include <asm/uaccess.h>
23 #include <asm/system.h>
25 static inline int min(int a, int b)
27 return a < b ? a : b;
31 * Fill in the supplied page for mmap
33 static unsigned long ncp_file_mmap_nopage(struct vm_area_struct *area,
34 unsigned long address, int no_share)
36 struct file *file = area->vm_file;
37 struct dentry *dentry = file->f_dentry;
38 struct inode *inode = dentry->d_inode;
39 unsigned long page;
40 unsigned int clear;
41 unsigned long tmp;
42 int bufsize;
43 int pos;
44 mm_segment_t fs;
46 page = __get_free_page(GFP_KERNEL);
47 if (!page)
48 return page;
49 address &= PAGE_MASK;
50 pos = address - area->vm_start + area->vm_offset;
52 clear = 0;
53 if (address + PAGE_SIZE > area->vm_end) {
54 clear = address + PAGE_SIZE - area->vm_end;
56 /* what we can read in one go */
57 bufsize = NCP_SERVER(inode)->buffer_size;
59 fs = get_fs();
60 set_fs(get_ds());
62 if (ncp_make_open(inode, O_RDONLY) < 0) {
63 clear = PAGE_SIZE;
64 } else {
65 int already_read = 0;
66 int count = PAGE_SIZE - clear;
67 int to_read;
69 while (already_read < count) {
70 int read_this_time;
72 if ((pos % bufsize) != 0) {
73 to_read = bufsize - (pos % bufsize);
74 } else {
75 to_read = bufsize;
78 to_read = min(to_read, count - already_read);
80 if (ncp_read(NCP_SERVER(inode),
81 NCP_FINFO(inode)->file_handle,
82 pos, to_read,
83 (char *) (page + already_read),
84 &read_this_time) != 0) {
85 read_this_time = 0;
87 pos += read_this_time;
88 already_read += read_this_time;
90 if (read_this_time < to_read) {
91 break;
97 set_fs(fs);
99 tmp = page + PAGE_SIZE;
100 while (clear--) {
101 *(char *) --tmp = 0;
103 return page;
106 struct vm_operations_struct ncp_file_mmap =
108 NULL, /* open */
109 NULL, /* close */
110 NULL, /* unmap */
111 NULL, /* protect */
112 NULL, /* sync */
113 NULL, /* advise */
114 ncp_file_mmap_nopage, /* nopage */
115 NULL, /* wppage */
116 NULL, /* swapout */
117 NULL, /* swapin */
121 /* This is used for a general mmap of a ncp file */
122 int ncp_mmap(struct file *file, struct vm_area_struct *vma)
124 struct inode *inode = file->f_dentry->d_inode;
126 DPRINTK(KERN_DEBUG "ncp_mmap: called\n");
128 if (!ncp_conn_valid(NCP_SERVER(inode))) {
129 return -EIO;
131 /* only PAGE_COW or read-only supported now */
132 if (vma->vm_flags & VM_SHARED)
133 return -EINVAL;
134 if (!inode->i_sb || !S_ISREG(inode->i_mode))
135 return -EACCES;
136 if (!IS_RDONLY(inode)) {
137 inode->i_atime = CURRENT_TIME;
140 vma->vm_ops = &ncp_file_mmap;
141 return 0;