[PATCH] core remove PageReserved
[safe/jmp/linux-2.6] / 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
25 static void msync_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
26                                 unsigned long addr, unsigned long end)
27 {
28         struct mm_struct *mm = vma->vm_mm;
29         pte_t *pte;
30         int progress = 0;
31
32 again:
33         pte = pte_offset_map(pmd, addr);
34         do {
35                 unsigned long pfn;
36                 struct page *page;
37
38                 if (progress >= 64) {
39                         progress = 0;
40                         if (need_resched() ||
41                             need_lockbreak(&mm->page_table_lock))
42                                 break;
43                 }
44                 progress++;
45                 if (!pte_present(*pte))
46                         continue;
47                 if (!pte_maybe_dirty(*pte))
48                         continue;
49                 pfn = pte_pfn(*pte);
50                 if (unlikely(!pfn_valid(pfn))) {
51                         print_bad_pte(vma, *pte, addr);
52                         continue;
53                 }
54                 page = pfn_to_page(pfn);
55
56                 if (ptep_clear_flush_dirty(vma, addr, pte) ||
57                     page_test_and_clear_dirty(page))
58                         set_page_dirty(page);
59                 progress += 3;
60         } while (pte++, addr += PAGE_SIZE, addr != end);
61         pte_unmap(pte - 1);
62         cond_resched_lock(&mm->page_table_lock);
63         if (addr != end)
64                 goto again;
65 }
66
67 static inline void msync_pmd_range(struct vm_area_struct *vma, pud_t *pud,
68                                 unsigned long addr, unsigned long end)
69 {
70         pmd_t *pmd;
71         unsigned long next;
72
73         pmd = pmd_offset(pud, addr);
74         do {
75                 next = pmd_addr_end(addr, end);
76                 if (pmd_none_or_clear_bad(pmd))
77                         continue;
78                 msync_pte_range(vma, pmd, addr, next);
79         } while (pmd++, addr = next, addr != end);
80 }
81
82 static inline void msync_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
83                                 unsigned long addr, unsigned long end)
84 {
85         pud_t *pud;
86         unsigned long next;
87
88         pud = pud_offset(pgd, addr);
89         do {
90                 next = pud_addr_end(addr, end);
91                 if (pud_none_or_clear_bad(pud))
92                         continue;
93                 msync_pmd_range(vma, pud, addr, next);
94         } while (pud++, addr = next, addr != end);
95 }
96
97 static void msync_page_range(struct vm_area_struct *vma,
98                                 unsigned long addr, unsigned long end)
99 {
100         struct mm_struct *mm = vma->vm_mm;
101         pgd_t *pgd;
102         unsigned long next;
103
104         /* For hugepages we can't go walking the page table normally,
105          * but that's ok, hugetlbfs is memory based, so we don't need
106          * to do anything more on an msync().
107          * Can't do anything with VM_RESERVED regions either.
108          */
109         if (vma->vm_flags & (VM_HUGETLB|VM_RESERVED))
110                 return;
111
112         BUG_ON(addr >= end);
113         pgd = pgd_offset(mm, addr);
114         flush_cache_range(vma, addr, end);
115         spin_lock(&mm->page_table_lock);
116         do {
117                 next = pgd_addr_end(addr, end);
118                 if (pgd_none_or_clear_bad(pgd))
119                         continue;
120                 msync_pud_range(vma, pgd, addr, next);
121         } while (pgd++, addr = next, addr != end);
122         spin_unlock(&mm->page_table_lock);
123 }
124
125 /*
126  * MS_SYNC syncs the entire file - including mappings.
127  *
128  * MS_ASYNC does not start I/O (it used to, up to 2.5.67).  Instead, it just
129  * marks the relevant pages dirty.  The application may now run fsync() to
130  * write out the dirty pages and wait on the writeout and check the result.
131  * Or the application may run fadvise(FADV_DONTNEED) against the fd to start
132  * async writeout immediately.
133  * So my _not_ starting I/O in MS_ASYNC we provide complete flexibility to
134  * applications.
135  */
136 static int msync_interval(struct vm_area_struct *vma,
137                         unsigned long addr, unsigned long end, int flags)
138 {
139         int ret = 0;
140         struct file *file = vma->vm_file;
141
142         if ((flags & MS_INVALIDATE) && (vma->vm_flags & VM_LOCKED))
143                 return -EBUSY;
144
145         if (file && (vma->vm_flags & VM_SHARED)) {
146                 msync_page_range(vma, addr, end);
147
148                 if (flags & MS_SYNC) {
149                         struct address_space *mapping = file->f_mapping;
150                         int err;
151
152                         ret = filemap_fdatawrite(mapping);
153                         if (file->f_op && file->f_op->fsync) {
154                                 /*
155                                  * We don't take i_sem here because mmap_sem
156                                  * is already held.
157                                  */
158                                 err = file->f_op->fsync(file,file->f_dentry,1);
159                                 if (err && !ret)
160                                         ret = err;
161                         }
162                         err = filemap_fdatawait(mapping);
163                         if (!ret)
164                                 ret = err;
165                 }
166         }
167         return ret;
168 }
169
170 asmlinkage long sys_msync(unsigned long start, size_t len, int flags)
171 {
172         unsigned long end;
173         struct vm_area_struct *vma;
174         int unmapped_error, error = -EINVAL;
175
176         if (flags & MS_SYNC)
177                 current->flags |= PF_SYNCWRITE;
178
179         down_read(&current->mm->mmap_sem);
180         if (flags & ~(MS_ASYNC | MS_INVALIDATE | MS_SYNC))
181                 goto out;
182         if (start & ~PAGE_MASK)
183                 goto out;
184         if ((flags & MS_ASYNC) && (flags & MS_SYNC))
185                 goto out;
186         error = -ENOMEM;
187         len = (len + ~PAGE_MASK) & PAGE_MASK;
188         end = start + len;
189         if (end < start)
190                 goto out;
191         error = 0;
192         if (end == start)
193                 goto out;
194         /*
195          * If the interval [start,end) covers some unmapped address ranges,
196          * just ignore them, but return -ENOMEM at the end.
197          */
198         vma = find_vma(current->mm, start);
199         unmapped_error = 0;
200         for (;;) {
201                 /* Still start < end. */
202                 error = -ENOMEM;
203                 if (!vma)
204                         goto out;
205                 /* Here start < vma->vm_end. */
206                 if (start < vma->vm_start) {
207                         unmapped_error = -ENOMEM;
208                         start = vma->vm_start;
209                 }
210                 /* Here vma->vm_start <= start < vma->vm_end. */
211                 if (end <= vma->vm_end) {
212                         if (start < end) {
213                                 error = msync_interval(vma, start, end, flags);
214                                 if (error)
215                                         goto out;
216                         }
217                         error = unmapped_error;
218                         goto out;
219                 }
220                 /* Here vma->vm_start <= start < vma->vm_end < end. */
221                 error = msync_interval(vma, start, vma->vm_end, flags);
222                 if (error)
223                         goto out;
224                 start = vma->vm_end;
225                 vma = vma->vm_next;
226         }
227 out:
228         up_read(&current->mm->mmap_sem);
229         current->flags &= ~PF_SYNCWRITE;
230         return error;
231 }