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/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/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  */
ncp_file_mmap_nopage(struct vm_area_struct * area,unsigned long address,int write_access)28 static struct page* ncp_file_mmap_nopage(struct vm_area_struct *area,
29 				     unsigned long address, int write_access)
30 {
31 	struct file *file = area->vm_file;
32 	struct dentry *dentry = file->f_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 		return page;
45 	pg_addr = kmap(page);
46 	address &= PAGE_MASK;
47 	pos = address - area->vm_start + (area->vm_pgoff << PAGE_SHIFT);
48 
49 	count = PAGE_SIZE;
50 	/* what we can read in one go */
51 	bufsize = NCP_SERVER(inode)->buffer_size;
52 
53 	already_read = 0;
54 	if (ncp_make_open(inode, O_RDONLY) >= 0) {
55 		while (already_read < count) {
56 			int read_this_time;
57 			int to_read;
58 
59 			to_read = bufsize - (pos % bufsize);
60 
61 			to_read = min_t(unsigned int, to_read, count - already_read);
62 
63 			if (ncp_read_kernel(NCP_SERVER(inode),
64 				     NCP_FINFO(inode)->file_handle,
65 				     pos, to_read,
66 				     pg_addr + already_read,
67 				     &read_this_time) != 0) {
68 				read_this_time = 0;
69 			}
70 			pos += read_this_time;
71 			already_read += read_this_time;
72 
73 			if (read_this_time < to_read) {
74 				break;
75 			}
76 		}
77 		ncp_inode_close(inode);
78 
79 	}
80 
81 	if (already_read < PAGE_SIZE)
82 		memset(pg_addr + already_read, 0, PAGE_SIZE - already_read);
83 	flush_dcache_page(page);
84 	kunmap(page);
85 	return page;
86 }
87 
88 static struct vm_operations_struct ncp_file_mmap =
89 {
90 	nopage:	ncp_file_mmap_nopage,
91 };
92 
93 
94 /* This is used for a general mmap of a ncp file */
ncp_mmap(struct file * file,struct vm_area_struct * vma)95 int ncp_mmap(struct file *file, struct vm_area_struct *vma)
96 {
97 	struct inode *inode = file->f_dentry->d_inode;
98 
99 	DPRINTK("ncp_mmap: called\n");
100 
101 	if (!ncp_conn_valid(NCP_SERVER(inode))) {
102 		return -EIO;
103 	}
104 	/* only PAGE_COW or read-only supported now */
105 	if (vma->vm_flags & VM_SHARED)
106 		return -EINVAL;
107 	if (!inode->i_sb || !S_ISREG(inode->i_mode))
108 		return -EACCES;
109 	/* we do not support files bigger than 4GB... We eventually
110 	   supports just 4GB... */
111 	if (((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff
112 	   > (1U << (32 - PAGE_SHIFT)))
113 		return -EFBIG;
114 	if (!IS_RDONLY(inode)) {
115 		inode->i_atime = CURRENT_TIME;
116 	}
117 
118 	vma->vm_ops = &ncp_file_mmap;
119 	return 0;
120 }
121