VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / mm / mincore.c
1 /*
2  *      linux/mm/mincore.c
3  *
4  * Copyright (C) 1994-1999  Linus Torvalds
5  */
6
7 /*
8  * The mincore() system call.
9  */
10 #include <linux/slab.h>
11 #include <linux/pagemap.h>
12 #include <linux/mm.h>
13 #include <linux/mman.h>
14
15 #include <asm/uaccess.h>
16 #include <asm/pgtable.h>
17
18 /*
19  * Later we can get more picky about what "in core" means precisely.
20  * For now, simply check to see if the page is in the page cache,
21  * and is up to date; i.e. that no page-in operation would be required
22  * at this time if an application were to map and access this page.
23  */
24 static unsigned char mincore_page(struct vm_area_struct * vma,
25         unsigned long pgoff)
26 {
27         unsigned char present = 0;
28         struct address_space * as = vma->vm_file->f_mapping;
29         struct page * page;
30
31         page = find_get_page(as, pgoff);
32         if (page) {
33                 present = PageUptodate(page);
34                 page_cache_release(page);
35         }
36
37         return present;
38 }
39
40 static long mincore_vma(struct vm_area_struct * vma,
41         unsigned long start, unsigned long end, unsigned char __user * vec)
42 {
43         long error, i, remaining;
44         unsigned char * tmp;
45
46         error = -ENOMEM;
47         if (!vma->vm_file)
48                 return error;
49
50         start = ((start - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
51         if (end > vma->vm_end)
52                 end = vma->vm_end;
53         end = ((end - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
54
55         error = -EAGAIN;
56         tmp = (unsigned char *) __get_free_page(GFP_KERNEL);
57         if (!tmp)
58                 return error;
59
60         /* (end - start) is # of pages, and also # of bytes in "vec */
61         remaining = (end - start),
62
63         error = 0;
64         for (i = 0; remaining > 0; remaining -= PAGE_SIZE, i++) {
65                 int j = 0;
66                 long thispiece = (remaining < PAGE_SIZE) ?
67                                                 remaining : PAGE_SIZE;
68
69                 while (j < thispiece)
70                         tmp[j++] = mincore_page(vma, start++);
71
72                 if (copy_to_user(vec + PAGE_SIZE * i, tmp, thispiece)) {
73                         error = -EFAULT;
74                         break;
75                 }
76         }
77
78         free_page((unsigned long) tmp);
79         return error;
80 }
81
82 /*
83  * The mincore(2) system call.
84  *
85  * mincore() returns the memory residency status of the pages in the
86  * current process's address space specified by [addr, addr + len).
87  * The status is returned in a vector of bytes.  The least significant
88  * bit of each byte is 1 if the referenced page is in memory, otherwise
89  * it is zero.
90  *
91  * Because the status of a page can change after mincore() checks it
92  * but before it returns to the application, the returned vector may
93  * contain stale information.  Only locked pages are guaranteed to
94  * remain in memory.
95  *
96  * return values:
97  *  zero    - success
98  *  -EFAULT - vec points to an illegal address
99  *  -EINVAL - addr is not a multiple of PAGE_CACHE_SIZE,
100  *              or len has a nonpositive value
101  *  -ENOMEM - Addresses in the range [addr, addr + len] are
102  *              invalid for the address space of this process, or
103  *              specify one or more pages which are not currently
104  *              mapped
105  *  -EAGAIN - A kernel resource was temporarily unavailable.
106  */
107 asmlinkage long sys_mincore(unsigned long start, size_t len,
108         unsigned char __user * vec)
109 {
110         int index = 0;
111         unsigned long end;
112         struct vm_area_struct * vma;
113         int unmapped_error = 0;
114         long error = -EINVAL;
115
116         down_read(&current->mm->mmap_sem);
117
118         if (start & ~PAGE_CACHE_MASK)
119                 goto out;
120         len = (len + ~PAGE_CACHE_MASK) & PAGE_CACHE_MASK;
121         end = start + len;
122         if (end < start)
123                 goto out;
124
125         error = -EFAULT;
126         if (!access_ok(VERIFY_WRITE, vec, len >> PAGE_SHIFT))
127                 goto out;
128
129         error = 0;
130         if (end == start)
131                 goto out;
132
133         /*
134          * If the interval [start,end) covers some unmapped address
135          * ranges, just ignore them, but return -ENOMEM at the end.
136          */
137         vma = find_vma(current->mm, start);
138         for (;;) {
139                 /* Still start < end. */
140                 error = -ENOMEM;
141                 if (!vma)
142                         goto out;
143
144                 /* Here start < vma->vm_end. */
145                 if (start < vma->vm_start) {
146                         unmapped_error = -ENOMEM;
147                         start = vma->vm_start;
148                 }
149
150                 /* Here vma->vm_start <= start < vma->vm_end. */
151                 if (end <= vma->vm_end) {
152                         if (start < end) {
153                                 error = mincore_vma(vma, start, end,
154                                                         &vec[index]);
155                                 if (error)
156                                         goto out;
157                         }
158                         error = unmapped_error;
159                         goto out;
160                 }
161
162                 /* Here vma->vm_start <= start < vma->vm_end < end. */
163                 error = mincore_vma(vma, start, vma->vm_end, &vec[index]);
164                 if (error)
165                         goto out;
166                 index += (vma->vm_end - start) >> PAGE_CACHE_SHIFT;
167                 start = vma->vm_end;
168                 vma = vma->vm_next;
169         }
170
171 out:
172         up_read(&current->mm->mmap_sem);
173         return error;
174 }