patch-2_6_7-vs1_9_1_12
[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
16 #include <asm/pgtable.h>
17 #include <asm/pgalloc.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(pgd_t * pgd,
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 (pgd_none(*pgd))
78                 return 0;
79         if (pgd_bad(*pgd)) {
80                 pgd_ERROR(*pgd);
81                 pgd_clear(pgd);
82                 return 0;
83         }
84         pmd = pmd_offset(pgd, address);
85         if ((address & PGDIR_MASK) != (end & PGDIR_MASK))
86                 end = (address & PGDIR_MASK) + PGDIR_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 int filemap_sync(struct vm_area_struct * vma, unsigned long address,
97         size_t size, unsigned int flags)
98 {
99         pgd_t * dir;
100         unsigned long end = address + size;
101         int error = 0;
102
103         /* Aquire the lock early; it may be possible to avoid dropping
104          * and reaquiring it repeatedly.
105          */
106         spin_lock(&vma->vm_mm->page_table_lock);
107
108         dir = pgd_offset(vma->vm_mm, address);
109         flush_cache_range(vma, address, end);
110
111         /* For hugepages we can't go walking the page table normally,
112          * but that's ok, hugetlbfs is memory based, so we don't need
113          * to do anything more on an msync() */
114         if (is_vm_hugetlb_page(vma))
115                 goto out;
116
117         if (address >= end)
118                 BUG();
119         do {
120                 error |= filemap_sync_pmd_range(dir, address, end, vma, flags);
121                 address = (address + PGDIR_SIZE) & PGDIR_MASK;
122                 dir++;
123         } while (address && (address < end));
124         /*
125          * Why flush ? filemap_sync_pte already flushed the tlbs with the
126          * dirty bits.
127          */
128         flush_tlb_range(vma, end - size, end);
129  out:
130         spin_unlock(&vma->vm_mm->page_table_lock);
131
132         return error;
133 }
134
135 /*
136  * MS_SYNC syncs the entire file - including mappings.
137  *
138  * MS_ASYNC does not start I/O (it used to, up to 2.5.67).  Instead, it just
139  * marks the relevant pages dirty.  The application may now run fsync() to
140  * write out the dirty pages and wait on the writeout and check the result.
141  * Or the application may run fadvise(FADV_DONTNEED) against the fd to start
142  * async writeout immediately.
143  * So my _not_ starting I/O in MS_ASYNC we provide complete flexibility to
144  * applications.
145  */
146 static int msync_interval(struct vm_area_struct * vma,
147         unsigned long start, unsigned long end, int flags)
148 {
149         int ret = 0;
150         struct file * file = vma->vm_file;
151
152         if ((flags & MS_INVALIDATE) && (vma->vm_flags & VM_LOCKED))
153                 return -EBUSY;
154
155         if (file && (vma->vm_flags & VM_SHARED)) {
156                 ret = filemap_sync(vma, start, end-start, flags);
157
158                 if (!ret && (flags & MS_SYNC)) {
159                         struct address_space *mapping = file->f_mapping;
160                         int err;
161
162                         down(&mapping->host->i_sem);
163                         ret = filemap_fdatawrite(mapping);
164                         if (file->f_op && file->f_op->fsync) {
165                                 err = file->f_op->fsync(file,file->f_dentry,1);
166                                 if (err && !ret)
167                                         ret = err;
168                         }
169                         err = filemap_fdatawait(mapping);
170                         if (!ret)
171                                 ret = err;
172                         up(&mapping->host->i_sem);
173                 }
174         }
175         return ret;
176 }
177
178 asmlinkage long sys_msync(unsigned long start, size_t len, int flags)
179 {
180         unsigned long end;
181         struct vm_area_struct * vma;
182         int unmapped_error, error = -EINVAL;
183
184         down_read(&current->mm->mmap_sem);
185         if (flags & ~(MS_ASYNC | MS_INVALIDATE | MS_SYNC))
186                 goto out;
187         if (start & ~PAGE_MASK)
188                 goto out;
189         if ((flags & MS_ASYNC) && (flags & MS_SYNC))
190                 goto out;
191         error = -ENOMEM;
192         len = (len + ~PAGE_MASK) & PAGE_MASK;
193         end = start + len;
194         if (end < start)
195                 goto out;
196         error = 0;
197         if (end == start)
198                 goto out;
199         /*
200          * If the interval [start,end) covers some unmapped address ranges,
201          * just ignore them, but return -ENOMEM at the end.
202          */
203         vma = find_vma(current->mm, start);
204         unmapped_error = 0;
205         for (;;) {
206                 /* Still start < end. */
207                 error = -ENOMEM;
208                 if (!vma)
209                         goto out;
210                 /* Here start < vma->vm_end. */
211                 if (start < vma->vm_start) {
212                         unmapped_error = -ENOMEM;
213                         start = vma->vm_start;
214                 }
215                 /* Here vma->vm_start <= start < vma->vm_end. */
216                 if (end <= vma->vm_end) {
217                         if (start < end) {
218                                 error = msync_interval(vma, start, end, flags);
219                                 if (error)
220                                         goto out;
221                         }
222                         error = unmapped_error;
223                         goto out;
224                 }
225                 /* Here vma->vm_start <= start < vma->vm_end < end. */
226                 error = msync_interval(vma, start, vma->vm_end, flags);
227                 if (error)
228                         goto out;
229                 start = vma->vm_end;
230                 vma = vma->vm_next;
231         }
232 out:
233         up_read(&current->mm->mmap_sem);
234         return error;
235 }