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>
16 #include <asm/pgtable.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
)
27 unsigned long pfn
= pte_pfn(pte
);
30 if (pte_present(pte
) && pfn_valid(pfn
)) {
31 page
= pfn_to_page(pfn
);
32 if (!PageReserved(page
) &&
33 (ptep_clear_flush_dirty(vma
, address
, ptep
) ||
34 page_test_and_clear_dirty(page
)))
40 static int filemap_sync_pte_range(pmd_t
* pmd
,
41 unsigned long address
, unsigned long end
,
42 struct vm_area_struct
*vma
, unsigned int flags
)
54 pte
= pte_offset_map(pmd
, address
);
55 if ((address
& PMD_MASK
) != (end
& PMD_MASK
))
56 end
= (address
& PMD_MASK
) + PMD_SIZE
;
59 error
|= filemap_sync_pte(pte
, vma
, address
, flags
);
62 } while (address
&& (address
< end
));
69 static inline int filemap_sync_pmd_range(pgd_t
* pgd
,
70 unsigned long address
, unsigned long end
,
71 struct vm_area_struct
*vma
, unsigned int flags
)
83 pmd
= pmd_offset(pgd
, address
);
84 if ((address
& PGDIR_MASK
) != (end
& PGDIR_MASK
))
85 end
= (address
& PGDIR_MASK
) + PGDIR_SIZE
;
88 error
|= filemap_sync_pte_range(pmd
, address
, end
, vma
, flags
);
89 address
= (address
+ PMD_SIZE
) & PMD_MASK
;
91 } while (address
&& (address
< end
));
95 static int filemap_sync(struct vm_area_struct
* vma
, unsigned long address
,
96 size_t size
, unsigned int flags
)
99 unsigned long end
= address
+ size
;
102 /* Aquire the lock early; it may be possible to avoid dropping
103 * and reaquiring it repeatedly.
105 spin_lock(&vma
->vm_mm
->page_table_lock
);
107 dir
= pgd_offset(vma
->vm_mm
, address
);
108 flush_cache_range(vma
, address
, end
);
110 /* For hugepages we can't go walking the page table normally,
111 * but that's ok, hugetlbfs is memory based, so we don't need
112 * to do anything more on an msync() */
113 if (is_vm_hugetlb_page(vma
))
119 error
|= filemap_sync_pmd_range(dir
, address
, end
, vma
, flags
);
120 address
= (address
+ PGDIR_SIZE
) & PGDIR_MASK
;
122 } while (address
&& (address
< end
));
124 * Why flush ? filemap_sync_pte already flushed the tlbs with the
127 flush_tlb_range(vma
, end
- size
, end
);
129 spin_unlock(&vma
->vm_mm
->page_table_lock
);
135 * MS_SYNC syncs the entire file - including mappings.
137 * MS_ASYNC does not start I/O (it used to, up to 2.5.67). Instead, it just
138 * marks the relevant pages dirty. The application may now run fsync() to
139 * write out the dirty pages and wait on the writeout and check the result.
140 * Or the application may run fadvise(FADV_DONTNEED) against the fd to start
141 * async writeout immediately.
142 * So my _not_ starting I/O in MS_ASYNC we provide complete flexibility to
145 static int msync_interval(struct vm_area_struct
* vma
,
146 unsigned long start
, unsigned long end
, int flags
)
149 struct file
* file
= vma
->vm_file
;
151 if ((flags
& MS_INVALIDATE
) && (vma
->vm_flags
& VM_LOCKED
))
154 if (file
&& (vma
->vm_flags
& VM_SHARED
)) {
155 ret
= filemap_sync(vma
, start
, end
-start
, flags
);
157 if (!ret
&& (flags
& MS_SYNC
)) {
158 struct address_space
*mapping
= file
->f_mapping
;
161 down(&mapping
->host
->i_sem
);
162 ret
= filemap_fdatawrite(mapping
);
163 if (file
->f_op
&& file
->f_op
->fsync
) {
164 err
= file
->f_op
->fsync(file
,file
->f_dentry
,1);
168 err
= filemap_fdatawait(mapping
);
171 up(&mapping
->host
->i_sem
);
177 asmlinkage
long sys_msync(unsigned long start
, size_t len
, int flags
)
180 struct vm_area_struct
* vma
;
181 int unmapped_error
, error
= -EINVAL
;
183 down_read(¤t
->mm
->mmap_sem
);
184 if (flags
& ~(MS_ASYNC
| MS_INVALIDATE
| MS_SYNC
))
186 if (start
& ~PAGE_MASK
)
188 if ((flags
& MS_ASYNC
) && (flags
& MS_SYNC
))
191 len
= (len
+ ~PAGE_MASK
) & PAGE_MASK
;
199 * If the interval [start,end) covers some unmapped address ranges,
200 * just ignore them, but return -ENOMEM at the end.
202 vma
= find_vma(current
->mm
, start
);
205 /* Still start < end. */
209 /* Here start < vma->vm_end. */
210 if (start
< vma
->vm_start
) {
211 unmapped_error
= -ENOMEM
;
212 start
= vma
->vm_start
;
214 /* Here vma->vm_start <= start < vma->vm_end. */
215 if (end
<= vma
->vm_end
) {
217 error
= msync_interval(vma
, start
, end
, flags
);
221 error
= unmapped_error
;
224 /* Here vma->vm_start <= start < vma->vm_end < end. */
225 error
= msync_interval(vma
, start
, vma
->vm_end
, flags
);
232 up_read(¤t
->mm
->mmap_sem
);