mm: per-cgroup memory reclaim stats
[linux-2.6-block.git] / fs / ncpfs / mmap.c
CommitLineData
1da177e4
LT
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>
5a0e3ad6 12#include <linux/gfp.h>
1da177e4
LT
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>
1da177e4 18#include <linux/fcntl.h>
456f998e 19#include <linux/memcontrol.h>
1da177e4 20
7c0f6ba6 21#include <linux/uaccess.h>
1da177e4 22
32c419d9
AV
23#include "ncp_fs.h"
24
1da177e4
LT
25/*
26 * Fill in the supplied page for mmap
d0217ac0
NP
27 * XXX: how are we excluding truncate/invalidate here? Maybe need to lock
28 * page?
1da177e4 29 */
11bac800 30static int ncp_file_mmap_fault(struct vm_fault *vmf)
1da177e4 31{
11bac800 32 struct inode *inode = file_inode(vmf->vma->vm_file);
1da177e4
LT
33 char *pg_addr;
34 unsigned int already_read;
35 unsigned int count;
36 int bufsize;
d0217ac0 37 int pos; /* XXX: loff_t ? */
1da177e4 38
d0217ac0
NP
39 /*
40 * ncpfs has nothing against high pages as long
41 * as recvmsg and memset works on it
42 */
43 vmf->page = alloc_page(GFP_HIGHUSER);
44 if (!vmf->page)
45 return VM_FAULT_OOM;
46 pg_addr = kmap(vmf->page);
47 pos = vmf->pgoff << PAGE_SHIFT;
1da177e4
LT
48
49 count = PAGE_SIZE;
1da177e4
LT
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);
d0217ac0
NP
83 flush_dcache_page(vmf->page);
84 kunmap(vmf->page);
1da177e4
LT
85
86 /*
87 * If I understand ncp_read_kernel() properly, the above always
88 * fetches from the network, here the analogue of disk.
6d49e352 89 * -- nyc
1da177e4 90 */
f8891e5e 91 count_vm_event(PGMAJFAULT);
2262185c 92 count_memcg_event_mm(vmf->vma->vm_mm, PGMAJFAULT);
d0217ac0 93 return VM_FAULT_MAJOR;
1da177e4
LT
94}
95
f0f37e2f 96static const struct vm_operations_struct ncp_file_mmap =
1da177e4 97{
54cb8821 98 .fault = ncp_file_mmap_fault,
1da177e4
LT
99};
100
101
102/* This is used for a general mmap of a ncp file */
103int ncp_mmap(struct file *file, struct vm_area_struct *vma)
104{
496ad9aa 105 struct inode *inode = file_inode(file);
1da177e4 106
d3b73ca1 107 ncp_dbg(1, "called\n");
1da177e4
LT
108
109 if (!ncp_conn_valid(NCP_SERVER(inode)))
110 return -EIO;
111
112 /* only PAGE_COW or read-only supported now */
113 if (vma->vm_flags & VM_SHARED)
114 return -EINVAL;
115 /* we do not support files bigger than 4GB... We eventually
116 supports just 4GB... */
ef9f515a 117 if (vma_pages(vma) + vma->vm_pgoff
1da177e4
LT
118 > (1U << (32 - PAGE_SHIFT)))
119 return -EFBIG;
120
121 vma->vm_ops = &ncp_file_mmap;
122 file_accessed(file);
123 return 0;
124}