4 * Copyright (C) 1994-1999 Linus Torvalds
8 * The msync() system call.
12 #include <linux/mman.h>
13 #include <linux/file.h>
14 #include <linux/syscalls.h>
15 #include <linux/sched.h>
18 * MS_SYNC syncs the entire file - including mappings.
20 * MS_ASYNC does not start I/O (it used to, up to 2.5.67).
21 * Nor does it marks the relevant pages dirty (it used to up to 2.6.17).
22 * Now it doesn't do anything, since dirty pages are properly tracked.
24 * The application may now run fsync() to
25 * write out the dirty pages and wait on the writeout and check the result.
26 * Or the application may run fadvise(FADV_DONTNEED) against the fd to start
27 * async writeout immediately.
28 * So by _not_ starting I/O in MS_ASYNC we provide complete flexibility to
31 SYSCALL_DEFINE3(msync
, unsigned long, start
, size_t, len
, int, flags
)
34 struct mm_struct
*mm
= current
->mm
;
35 struct vm_area_struct
*vma
;
36 int unmapped_error
= 0;
39 if (flags
& ~(MS_ASYNC
| MS_INVALIDATE
| MS_SYNC
))
41 if (start
& ~PAGE_MASK
)
43 if ((flags
& MS_ASYNC
) && (flags
& MS_SYNC
))
46 len
= (len
+ ~PAGE_MASK
) & PAGE_MASK
;
54 * If the interval [start,end) covers some unmapped address ranges,
55 * just ignore them, but return -ENOMEM at the end.
57 down_read(&mm
->mmap_sem
);
58 vma
= find_vma(mm
, start
);
62 /* Still start < end. */
66 /* Here start < vma->vm_end. */
67 if (start
< vma
->vm_start
) {
68 start
= vma
->vm_start
;
71 unmapped_error
= -ENOMEM
;
73 /* Here vma->vm_start <= start < vma->vm_end. */
74 if ((flags
& MS_INVALIDATE
) &&
75 (vma
->vm_flags
& VM_LOCKED
)) {
81 if ((flags
& MS_SYNC
) && file
&&
82 (vma
->vm_flags
& VM_SHARED
)) {
84 up_read(&mm
->mmap_sem
);
85 error
= vfs_fsync(file
, file
->f_path
.dentry
, 0);
87 if (error
|| start
>= end
)
89 down_read(&mm
->mmap_sem
);
90 vma
= find_vma(mm
, start
);
100 up_read(&mm
->mmap_sem
);
102 return error
? : unmapped_error
;