vserver 1.9.5.x5
[linux-2.6.git] / mm / msync.c
1 /*
2  *      linux/mm/msync.c
3  *
4  * Copyright (C) 1994-1999  Linus Torvalds
5  */
6
7 /*
8  * The msync() system call.
9  */
10 #include <linux/slab.h>
11 #include <linux/pagemap.h>
12 #include <linux/mm.h>
13 #include <linux/mman.h>
14 #include <linux/hugetlb.h>
15 #include <linux/syscalls.h>
16
17 #include <asm/pgtable.h>
18 #include <asm/tlbflush.h>
19
20 /*
21  * Called with mm->page_table_lock held to protect against other
22  * threads/the swapper from ripping pte's out from under us.
23  */
24 static int filemap_sync_pte(pte_t *ptep, struct vm_area_struct *vma,
25         unsigned long address, unsigned int flags)
26 {
27         pte_t pte = *ptep;
28         unsigned long pfn = pte_pfn(pte);
29         struct page *page;
30
31         if (pte_present(pte) && pfn_valid(pfn)) {
32                 page = pfn_to_page(pfn);
33                 if (!PageReserved(page) &&
34                     (ptep_clear_flush_dirty(vma, address, ptep) ||
35                      page_test_and_clear_dirty(page)))
36                         set_page_dirty(page);
37         }
38         return 0;
39 }
40
41 static int filemap_sync_pte_range(pmd_t * pmd,
42         unsigned long address, unsigned long end, 
43         struct vm_area_struct *vma, unsigned int flags)
44 {
45         pte_t *pte;
46         int error;
47
48         if (pmd_none(*pmd))
49                 return 0;
50         if (pmd_bad(*pmd)) {
51                 pmd_ERROR(*pmd);
52                 pmd_clear(pmd);
53                 return 0;
54         }
55         pte = pte_offset_map(pmd, address);
56         if ((address & PMD_MASK) != (end & PMD_MASK))
57                 end = (address & PMD_MASK) + PMD_SIZE;
58         error = 0;
59         do {
60                 error |= filemap_sync_pte(pte, vma, address, flags);
61                 address += PAGE_SIZE;
62                 pte++;
63         } while (address && (address < end));
64
65         pte_unmap(pte - 1);
66
67         return error;
68 }
69
70 static inline int filemap_sync_pmd_range(pud_t * pud,
71         unsigned long address, unsigned long end, 
72         struct vm_area_struct *vma, unsigned int flags)
73 {
74         pmd_t * pmd;
75         int error;
76
77         if (pud_none(*pud))
78                 return 0;
79         if (pud_bad(*pud)) {
80                 pud_ERROR(*pud);
81                 pud_clear(pud);
82                 return 0;
83         }
84         pmd = pmd_offset(pud, address);
85         if ((address & PUD_MASK) != (end & PUD_MASK))
86                 end = (address & PUD_MASK) + PUD_SIZE;
87         error = 0;
88         do {
89                 error |= filemap_sync_pte_range(pmd, address, end, vma, flags);
90                 address = (address + PMD_SIZE) & PMD_MASK;
91                 pmd++;
92         } while (address && (address < end));
93         return error;
94 }
95
96 static inline int filemap_sync_pud_range(pgd_t *pgd,
97         unsigned long address, unsigned long end,
98         struct vm_area_struct *vma, unsigned int flags)
99 {
100         pud_t *pud;
101         int error;
102
103         if (pgd_none(*pgd))
104                 return 0;
105         if (pgd_bad(*pgd)) {
106                 pgd_ERROR(*pgd);
107                 pgd_clear(pgd);
108                 return 0;
109         }
110         pud = pud_offset(pgd, address);
111         if ((address & PGDIR_MASK) != (end & PGDIR_MASK))
112                 end = (address & PGDIR_MASK) + PGDIR_SIZE;
113         error = 0;
114         do {
115                 error |= filemap_sync_pmd_range(pud, address, end, vma, flags);
116                 address = (address + PUD_SIZE) & PUD_MASK;
117                 pud++;
118         } while (address && (address < end));
119         return error;
120 }
121
122 static int __filemap_sync(struct vm_area_struct *vma, unsigned long address,
123                         size_t size, unsigned int flags)
124 {
125         pgd_t *pgd;
126         unsigned long end = address + size;
127         unsigned long next;
128         int i;
129         int error = 0;
130
131         /* Aquire the lock early; it may be possible to avoid dropping
132          * and reaquiring it repeatedly.
133          */
134         spin_lock(&vma->vm_mm->page_table_lock);
135
136         pgd = pgd_offset(vma->vm_mm, address);
137         flush_cache_range(vma, address, end);
138
139         /* For hugepages we can't go walking the page table normally,
140          * but that's ok, hugetlbfs is memory based, so we don't need
141          * to do anything more on an msync() */
142         if (is_vm_hugetlb_page(vma))
143                 goto out;
144
145         if (address >= end)
146                 BUG();
147         for (i = pgd_index(address); i <= pgd_index(end-1); i++) {
148                 next = (address + PGDIR_SIZE) & PGDIR_MASK;
149                 if (next <= address || next > end)
150                         next = end;
151                 error |= filemap_sync_pud_range(pgd, address, next, vma, flags);
152                 address = next;
153                 pgd++;
154         }
155         /*
156          * Why flush ? filemap_sync_pte already flushed the tlbs with the
157          * dirty bits.
158          */
159         flush_tlb_range(vma, end - size, end);
160  out:
161         spin_unlock(&vma->vm_mm->page_table_lock);
162
163         return error;
164 }
165
166 #ifdef CONFIG_PREEMPT
167 static int filemap_sync(struct vm_area_struct *vma, unsigned long address,
168                         size_t size, unsigned int flags)
169 {
170         const size_t chunk = 64 * 1024; /* bytes */
171         int error = 0;
172
173         while (size) {
174                 size_t sz = min(size, chunk);
175
176                 error |= __filemap_sync(vma, address, sz, flags);
177                 cond_resched();
178                 address += sz;
179                 size -= sz;
180         }
181         return error;
182 }
183 #else
184 static int filemap_sync(struct vm_area_struct *vma, unsigned long address,
185                         size_t size, unsigned int flags)
186 {
187         return __filemap_sync(vma, address, size, flags);
188 }
189 #endif
190
191 /*
192  * MS_SYNC syncs the entire file - including mappings.
193  *
194  * MS_ASYNC does not start I/O (it used to, up to 2.5.67).  Instead, it just
195  * marks the relevant pages dirty.  The application may now run fsync() to
196  * write out the dirty pages and wait on the writeout and check the result.
197  * Or the application may run fadvise(FADV_DONTNEED) against the fd to start
198  * async writeout immediately.
199  * So my _not_ starting I/O in MS_ASYNC we provide complete flexibility to
200  * applications.
201  */
202 static int msync_interval(struct vm_area_struct * vma,
203         unsigned long start, unsigned long end, int flags)
204 {
205         int ret = 0;
206         struct file * file = vma->vm_file;
207
208         if ((flags & MS_INVALIDATE) && (vma->vm_flags & VM_LOCKED))
209                 return -EBUSY;
210
211         if (file && (vma->vm_flags & VM_SHARED)) {
212                 ret = filemap_sync(vma, start, end-start, flags);
213
214                 if (!ret && (flags & MS_SYNC)) {
215                         struct address_space *mapping = file->f_mapping;
216                         int err;
217
218                         ret = filemap_fdatawrite(mapping);
219                         if (file->f_op && file->f_op->fsync) {
220                                 /*
221                                  * We don't take i_sem here because mmap_sem
222                                  * is already held.
223                                  */
224                                 err = file->f_op->fsync(file,file->f_dentry,1);
225                                 if (err && !ret)
226                                         ret = err;
227                         }
228                         err = filemap_fdatawait(mapping);
229                         if (!ret)
230                                 ret = err;
231                 }
232         }
233         return ret;
234 }
235
236 asmlinkage long sys_msync(unsigned long start, size_t len, int flags)
237 {
238         unsigned long end;
239         struct vm_area_struct * vma;
240         int unmapped_error, error = -EINVAL;
241
242         if (flags & MS_SYNC)
243                 current->flags |= PF_SYNCWRITE;
244
245         down_read(&current->mm->mmap_sem);
246         if (flags & ~(MS_ASYNC | MS_INVALIDATE | MS_SYNC))
247                 goto out;
248         if (start & ~PAGE_MASK)
249                 goto out;
250         if ((flags & MS_ASYNC) && (flags & MS_SYNC))
251                 goto out;
252         error = -ENOMEM;
253         len = (len + ~PAGE_MASK) & PAGE_MASK;
254         end = start + len;
255         if (end < start)
256                 goto out;
257         error = 0;
258         if (end == start)
259                 goto out;
260         /*
261          * If the interval [start,end) covers some unmapped address ranges,
262          * just ignore them, but return -ENOMEM at the end.
263          */
264         vma = find_vma(current->mm, start);
265         unmapped_error = 0;
266         for (;;) {
267                 /* Still start < end. */
268                 error = -ENOMEM;
269                 if (!vma)
270                         goto out;
271                 /* Here start < vma->vm_end. */
272                 if (start < vma->vm_start) {
273                         unmapped_error = -ENOMEM;
274                         start = vma->vm_start;
275                 }
276                 /* Here vma->vm_start <= start < vma->vm_end. */
277                 if (end <= vma->vm_end) {
278                         if (start < end) {
279                                 error = msync_interval(vma, start, end, flags);
280                                 if (error)
281                                         goto out;
282                         }
283                         error = unmapped_error;
284                         goto out;
285                 }
286                 /* Here vma->vm_start <= start < vma->vm_end < end. */
287                 error = msync_interval(vma, start, vma->vm_end, flags);
288                 if (error)
289                         goto out;
290                 start = vma->vm_end;
291                 vma = vma->vm_next;
292         }
293 out:
294         up_read(&current->mm->mmap_sem);
295         current->flags &= ~PF_SYNCWRITE;
296         return error;
297 }