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>
14 #include <linux/hugetlb.h>
15 #include <linux/syscalls.h>
17 #include <asm/pgtable.h>
18 #include <asm/tlbflush.h>
21 * Called with mm->page_table_lock held to protect against other
22 * threads/the swapper from ripping pte's out from under us.
24 static int filemap_sync_pte(pte_t *ptep, struct vm_area_struct *vma,
25 unsigned long address, unsigned int flags)
28 unsigned long pfn = pte_pfn(pte);
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)))
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)
55 pte = pte_offset_map(pmd, address);
56 if ((address & PMD_MASK) != (end & PMD_MASK))
57 end = (address & PMD_MASK) + PMD_SIZE;
60 error |= filemap_sync_pte(pte, vma, address, flags);
63 } while (address && (address < end));
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)
84 pmd = pmd_offset(pgd, address);
85 if ((address & PGDIR_MASK) != (end & PGDIR_MASK))
86 end = (address & PGDIR_MASK) + PGDIR_SIZE;
89 error |= filemap_sync_pte_range(pmd, address, end, vma, flags);
90 address = (address + PMD_SIZE) & PMD_MASK;
92 } while (address && (address < end));
96 static int filemap_sync(struct vm_area_struct * vma, unsigned long address,
97 size_t size, unsigned int flags)
100 unsigned long end = address + size;
103 /* Aquire the lock early; it may be possible to avoid dropping
104 * and reaquiring it repeatedly.
106 spin_lock(&vma->vm_mm->page_table_lock);
108 dir = pgd_offset(vma->vm_mm, address);
109 flush_cache_range(vma, address, end);
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))
120 error |= filemap_sync_pmd_range(dir, address, end, vma, flags);
121 address = (address + PGDIR_SIZE) & PGDIR_MASK;
123 } while (address && (address < end));
125 * Why flush ? filemap_sync_pte already flushed the tlbs with the
128 flush_tlb_range(vma, end - size, end);
130 spin_unlock(&vma->vm_mm->page_table_lock);
136 * MS_SYNC syncs the entire file - including mappings.
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
146 static int msync_interval(struct vm_area_struct * vma,
147 unsigned long start, unsigned long end, int flags)
150 struct file * file = vma->vm_file;
152 if ((flags & MS_INVALIDATE) && (vma->vm_flags & VM_LOCKED))
155 if (file && (vma->vm_flags & VM_SHARED)) {
156 ret = filemap_sync(vma, start, end-start, flags);
158 if (!ret && (flags & MS_SYNC)) {
159 struct address_space *mapping = file->f_mapping;
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);
169 err = filemap_fdatawait(mapping);
172 up(&mapping->host->i_sem);
178 asmlinkage long sys_msync(unsigned long start, size_t len, int flags)
181 struct vm_area_struct * vma;
182 int unmapped_error, error = -EINVAL;
184 down_read(¤t->mm->mmap_sem);
185 if (flags & ~(MS_ASYNC | MS_INVALIDATE | MS_SYNC))
187 if (start & ~PAGE_MASK)
189 if ((flags & MS_ASYNC) && (flags & MS_SYNC))
192 len = (len + ~PAGE_MASK) & PAGE_MASK;
200 * If the interval [start,end) covers some unmapped address ranges,
201 * just ignore them, but return -ENOMEM at the end.
203 vma = find_vma(current->mm, start);
206 /* Still start < end. */
210 /* Here start < vma->vm_end. */
211 if (start < vma->vm_start) {
212 unmapped_error = -ENOMEM;
213 start = vma->vm_start;
215 /* Here vma->vm_start <= start < vma->vm_end. */
216 if (end <= vma->vm_end) {
218 error = msync_interval(vma, start, end, flags);
222 error = unmapped_error;
225 /* Here vma->vm_start <= start < vma->vm_end < end. */
226 error = msync_interval(vma, start, vma->vm_end, flags);
233 up_read(¤t->mm->mmap_sem);