Cleanup syscall code to look more like it's mips64 equivalent.
[linux-2.6/linux-mips.git] / mm / msync.c
blob5e4d464c7efe1ec2f8f7e26e0786a7f9a2ff3dd3
1 /*
2 * linux/mm/msync.c
4 * Copyright (C) 1994-1999 Linus Torvalds
5 */
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>
15 #include <asm/pgtable.h>
16 #include <asm/pgalloc.h>
17 #include <asm/tlbflush.h>
20 * Called with mm->page_table_lock held to protect against other
21 * threads/the swapper from ripping pte's out from under us.
23 static int filemap_sync_pte(pte_t *ptep, struct vm_area_struct *vma,
24 unsigned long address, unsigned int flags)
26 pte_t pte = *ptep;
28 if (pte_present(pte) && pte_dirty(pte)) {
29 struct page *page;
30 unsigned long pfn = pte_pfn(pte);
31 if (pfn_valid(pfn)) {
32 page = pfn_to_page(pfn);
33 if (!PageReserved(page) && ptep_test_and_clear_dirty(ptep)) {
34 flush_tlb_page(vma, address);
35 set_page_dirty(page);
39 return 0;
42 static int filemap_sync_pte_range(pmd_t * pmd,
43 unsigned long address, unsigned long end,
44 struct vm_area_struct *vma, unsigned int flags)
46 pte_t *pte;
47 int error;
49 if (pmd_none(*pmd))
50 return 0;
51 if (pmd_bad(*pmd)) {
52 pmd_ERROR(*pmd);
53 pmd_clear(pmd);
54 return 0;
56 pte = pte_offset_map(pmd, address);
57 if ((address & PMD_MASK) != (end & PMD_MASK))
58 end = (address & PMD_MASK) + PMD_SIZE;
59 error = 0;
60 do {
61 error |= filemap_sync_pte(pte, vma, address, flags);
62 address += PAGE_SIZE;
63 pte++;
64 } while (address && (address < end));
66 pte_unmap(pte - 1);
68 return error;
71 static inline int filemap_sync_pmd_range(pgd_t * pgd,
72 unsigned long address, unsigned long end,
73 struct vm_area_struct *vma, unsigned int flags)
75 pmd_t * pmd;
76 int error;
78 if (pgd_none(*pgd))
79 return 0;
80 if (pgd_bad(*pgd)) {
81 pgd_ERROR(*pgd);
82 pgd_clear(pgd);
83 return 0;
85 pmd = pmd_offset(pgd, address);
86 if ((address & PGDIR_MASK) != (end & PGDIR_MASK))
87 end = (address & PGDIR_MASK) + PGDIR_SIZE;
88 error = 0;
89 do {
90 error |= filemap_sync_pte_range(pmd, address, end, vma, flags);
91 address = (address + PMD_SIZE) & PMD_MASK;
92 pmd++;
93 } while (address && (address < end));
94 return error;
97 static int filemap_sync(struct vm_area_struct * vma, unsigned long address,
98 size_t size, unsigned int flags)
100 pgd_t * dir;
101 unsigned long end = address + size;
102 int error = 0;
104 /* Aquire the lock early; it may be possible to avoid dropping
105 * and reaquiring it repeatedly.
107 spin_lock(&vma->vm_mm->page_table_lock);
109 dir = pgd_offset(vma->vm_mm, address);
110 flush_cache_range(vma, address, end);
111 if (address >= end)
112 BUG();
113 do {
114 error |= filemap_sync_pmd_range(dir, address, end, vma, flags);
115 address = (address + PGDIR_SIZE) & PGDIR_MASK;
116 dir++;
117 } while (address && (address < end));
118 flush_tlb_range(vma, end - size, end);
120 spin_unlock(&vma->vm_mm->page_table_lock);
122 return error;
126 * MS_SYNC syncs the entire file - including mappings.
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.
136 static int msync_interval(struct vm_area_struct * vma,
137 unsigned long start, unsigned long end, int flags)
139 int ret = 0;
140 struct file * file = vma->vm_file;
142 if ((flags & MS_INVALIDATE) && (vma->vm_flags & VM_LOCKED))
143 return -EBUSY;
145 if (file && (vma->vm_flags & VM_SHARED)) {
146 ret = filemap_sync(vma, start, end-start, flags);
148 if (!ret && (flags & MS_SYNC)) {
149 struct inode *inode = file->f_dentry->d_inode;
150 int err;
152 down(&inode->i_sem);
153 ret = filemap_fdatawrite(inode->i_mapping);
154 if (file->f_op && file->f_op->fsync) {
155 err = file->f_op->fsync(file,file->f_dentry,1);
156 if (err && !ret)
157 ret = err;
159 err = filemap_fdatawait(inode->i_mapping);
160 if (!ret)
161 ret = err;
162 up(&inode->i_sem);
165 return ret;
168 asmlinkage long sys_msync(unsigned long start, size_t len, int flags)
170 unsigned long end;
171 struct vm_area_struct * vma;
172 int unmapped_error, error = -EINVAL;
174 down_read(&current->mm->mmap_sem);
175 if (flags & ~(MS_ASYNC | MS_INVALIDATE | MS_SYNC))
176 goto out;
177 if (start & ~PAGE_MASK)
178 goto out;
179 if ((flags & MS_ASYNC) && (flags & MS_SYNC))
180 goto out;
181 error = -ENOMEM;
182 len = (len + ~PAGE_MASK) & PAGE_MASK;
183 end = start + len;
184 if (end < start)
185 goto out;
186 error = 0;
187 if (end == start)
188 goto out;
190 * If the interval [start,end) covers some unmapped address ranges,
191 * just ignore them, but return -ENOMEM at the end.
193 vma = find_vma(current->mm, start);
194 unmapped_error = 0;
195 for (;;) {
196 /* Still start < end. */
197 error = -ENOMEM;
198 if (!vma)
199 goto out;
200 /* Here start < vma->vm_end. */
201 if (start < vma->vm_start) {
202 unmapped_error = -ENOMEM;
203 start = vma->vm_start;
205 /* Here vma->vm_start <= start < vma->vm_end. */
206 if (end <= vma->vm_end) {
207 if (start < end) {
208 error = msync_interval(vma, start, end, flags);
209 if (error)
210 goto out;
212 error = unmapped_error;
213 goto out;
215 /* Here vma->vm_start <= start < vma->vm_end < end. */
216 error = msync_interval(vma, start, vma->vm_end, flags);
217 if (error)
218 goto out;
219 start = vma->vm_end;
220 vma = vma->vm_next;
222 out:
223 up_read(&current->mm->mmap_sem);
224 return error;