blob: a5c673669ca654b8ca1c1e8508e511345484bb27 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/mm/msync.c
3 *
4 * Copyright (C) 1994-1999 Linus Torvalds
5 */
6
7/*
8 * The msync() system call.
9 */
Andrew Morton8f2e9f12006-03-24 03:18:15 -080010#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/mm.h>
12#include <linux/mman.h>
Andrew Morton9c50823e2006-03-24 03:18:12 -080013#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/syscalls.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040015#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Linus Torvalds1da177e2005-04-16 15:20:36 -070017/*
18 * MS_SYNC syncs the entire file - including mappings.
19 *
Peter Zijlstra204ec842006-09-25 23:31:01 -070020 * 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.
23 *
24 * The application may now run fsync() to
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * 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.
Amos Waterland16538c42006-03-24 18:30:53 +010028 * So by _not_ starting I/O in MS_ASYNC we provide complete flexibility to
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 * applications.
30 */
Heiko Carstens6a6160a2009-01-14 14:14:15 +010031SYSCALL_DEFINE3(msync, unsigned long, start, size_t, len, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
33 unsigned long end;
Peter Zijlstra204ec842006-09-25 23:31:01 -070034 struct mm_struct *mm = current->mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 struct vm_area_struct *vma;
Andrew Morton676758b2006-03-24 03:18:14 -080036 int unmapped_error = 0;
37 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 if (flags & ~(MS_ASYNC | MS_INVALIDATE | MS_SYNC))
40 goto out;
41 if (start & ~PAGE_MASK)
42 goto out;
43 if ((flags & MS_ASYNC) && (flags & MS_SYNC))
44 goto out;
45 error = -ENOMEM;
46 len = (len + ~PAGE_MASK) & PAGE_MASK;
47 end = start + len;
48 if (end < start)
49 goto out;
50 error = 0;
51 if (end == start)
52 goto out;
53 /*
54 * If the interval [start,end) covers some unmapped address ranges,
55 * just ignore them, but return -ENOMEM at the end.
56 */
Peter Zijlstra204ec842006-09-25 23:31:01 -070057 down_read(&mm->mmap_sem);
58 vma = find_vma(mm, start);
59 for (;;) {
Andrew Morton9c50823e2006-03-24 03:18:12 -080060 struct file *file;
Matthew Wilcox7fc34a62014-06-04 16:10:44 -070061 loff_t fstart, fend;
Andrew Morton9c50823e2006-03-24 03:18:12 -080062
Peter Zijlstra204ec842006-09-25 23:31:01 -070063 /* Still start < end. */
64 error = -ENOMEM;
65 if (!vma)
66 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 /* Here start < vma->vm_end. */
68 if (start < vma->vm_start) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 start = vma->vm_start;
Peter Zijlstra204ec842006-09-25 23:31:01 -070070 if (start >= end)
71 goto out_unlock;
72 unmapped_error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 }
74 /* Here vma->vm_start <= start < vma->vm_end. */
Peter Zijlstra204ec842006-09-25 23:31:01 -070075 if ((flags & MS_INVALIDATE) &&
76 (vma->vm_flags & VM_LOCKED)) {
77 error = -EBUSY;
78 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 }
Andrew Morton9c50823e2006-03-24 03:18:12 -080080 file = vma->vm_file;
Matthew Wilcox7fc34a62014-06-04 16:10:44 -070081 fstart = start + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
82 fend = fstart + (min(end, vma->vm_end) - start) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 start = vma->vm_end;
Peter Zijlstra204ec842006-09-25 23:31:01 -070084 if ((flags & MS_SYNC) && file &&
Andrew Morton707c21c2006-03-24 03:18:13 -080085 (vma->vm_flags & VM_SHARED)) {
Andrew Morton707c21c2006-03-24 03:18:13 -080086 get_file(file);
Peter Zijlstra204ec842006-09-25 23:31:01 -070087 up_read(&mm->mmap_sem);
Matthew Wilcox7fc34a62014-06-04 16:10:44 -070088 if (vma->vm_flags & VM_NONLINEAR)
89 error = vfs_fsync(file, 1);
90 else
91 error = vfs_fsync_range(file, fstart, fend, 1);
Andrew Morton707c21c2006-03-24 03:18:13 -080092 fput(file);
Peter Zijlstra204ec842006-09-25 23:31:01 -070093 if (error || start >= end)
94 goto out;
95 down_read(&mm->mmap_sem);
96 vma = find_vma(mm, start);
Andrew Morton9c50823e2006-03-24 03:18:12 -080097 } else {
Peter Zijlstra204ec842006-09-25 23:31:01 -070098 if (start >= end) {
99 error = 0;
100 goto out_unlock;
101 }
Andrew Morton9c50823e2006-03-24 03:18:12 -0800102 vma = vma->vm_next;
103 }
Peter Zijlstra204ec842006-09-25 23:31:01 -0700104 }
Andrew Morton9c50823e2006-03-24 03:18:12 -0800105out_unlock:
Peter Zijlstra204ec842006-09-25 23:31:01 -0700106 up_read(&mm->mmap_sem);
Andrew Morton9c50823e2006-03-24 03:18:12 -0800107out:
Peter Zijlstra204ec842006-09-25 23:31:01 -0700108 return error ? : unmapped_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}