4 * Copyright (C) 1994-1999 Linus Torvalds
8 * The msync() system call.
10 #include <linux/slab.h>
11 #include <linux/pagemap.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)
28 if (pte_present(pte) && pte_dirty(pte)) {
30 unsigned long pfn = pte_pfn(pte);
32 page = pfn_to_page(pfn);
33 if (!PageReserved(page) && ptep_test_and_clear_dirty(ptep)) {
34 flush_tlb_page(vma, address);
42 static inline int filemap_sync_pte_range(pmd_t * pmd,
43 unsigned long address, unsigned long end,
44 struct vm_area_struct *vma, unsigned int flags)
56 pte = pte_offset_map(pmd, address);
57 if ((address & PMD_MASK) != (end & PMD_MASK))
58 end = (address & PMD_MASK) + PMD_SIZE;
61 error |= filemap_sync_pte(pte, vma, address, flags);
64 } while (address && (address < end));
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)
85 pmd = pmd_offset(pgd, address);
86 if ((address & PGDIR_MASK) != (end & PGDIR_MASK))
87 end = (address & PGDIR_MASK) + PGDIR_SIZE;
90 error |= filemap_sync_pte_range(pmd, address, end, vma, flags);
91 address = (address + PMD_SIZE) & PMD_MASK;
93 } while (address && (address < end));
97 int filemap_sync(struct vm_area_struct * vma, unsigned long address,
98 size_t size, unsigned int flags)
101 unsigned long end = address + size;
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);
114 error |= filemap_sync_pmd_range(dir, address, end, vma, flags);
115 address = (address + PGDIR_SIZE) & PGDIR_MASK;
117 } while (address && (address < end));
118 flush_tlb_range(vma, end - size, end);
120 spin_unlock(&vma->vm_mm->page_table_lock);
126 * MS_SYNC syncs the entire file - including mappings.
128 * MS_ASYNC initiates writeout of just the dirty mapped data.
129 * This provides no guarantee of file integrity - things like indirect
130 * blocks may not have started writeout. MS_ASYNC is primarily useful
131 * where the application knows that it has finished with the data and
132 * wishes to intelligently schedule its own I/O traffic.
134 static int msync_interval(struct vm_area_struct * vma,
135 unsigned long start, unsigned long end, int flags)
138 struct file * file = vma->vm_file;
140 if (file && (vma->vm_flags & VM_SHARED)) {
141 ret = filemap_sync(vma, start, end-start, flags);
143 if (!ret && (flags & (MS_SYNC|MS_ASYNC))) {
144 struct inode * inode = file->f_dentry->d_inode;
148 ret = filemap_fdatawait(inode->i_mapping);
149 err = filemap_fdatawrite(inode->i_mapping);
152 if (flags & MS_SYNC) {
153 if (file->f_op && file->f_op->fsync) {
154 err = file->f_op->fsync(file, file->f_dentry, 1);
158 err = filemap_fdatawait(inode->i_mapping);
168 asmlinkage long sys_msync(unsigned long start, size_t len, int flags)
171 struct vm_area_struct * vma;
172 int unmapped_error, error = -EINVAL;
174 down_read(¤t->mm->mmap_sem);
175 if (start & ~PAGE_MASK)
177 len = (len + ~PAGE_MASK) & PAGE_MASK;
181 if (flags & ~(MS_ASYNC | MS_INVALIDATE | MS_SYNC))
187 * If the interval [start,end) covers some unmapped address ranges,
188 * just ignore them, but return -EFAULT at the end.
190 vma = find_vma(current->mm, start);
193 /* Still start < end. */
197 /* Here start < vma->vm_end. */
198 if (start < vma->vm_start) {
199 unmapped_error = -EFAULT;
200 start = vma->vm_start;
202 /* Here vma->vm_start <= start < vma->vm_end. */
203 if (end <= vma->vm_end) {
205 error = msync_interval(vma, start, end, flags);
209 error = unmapped_error;
212 /* Here vma->vm_start <= start < vma->vm_end < end. */
213 error = msync_interval(vma, start, vma->vm_end, flags);
220 up_read(¤t->mm->mmap_sem);