blob: bb04d53ae8529597d3b0ca6636c49d5243762875 [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;
Namjae Jeon496a8e62014-07-02 15:22:36 -070081 fstart = (start - vma->vm_start) +
82 ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
Matthew Wilcox7fc34a62014-06-04 16:10:44 -070083 fend = fstart + (min(end, vma->vm_end) - start) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 start = vma->vm_end;
Peter Zijlstra204ec842006-09-25 23:31:01 -070085 if ((flags & MS_SYNC) && file &&
Andrew Morton707c21c2006-03-24 03:18:13 -080086 (vma->vm_flags & VM_SHARED)) {
Andrew Morton707c21c2006-03-24 03:18:13 -080087 get_file(file);
Peter Zijlstra204ec842006-09-25 23:31:01 -070088 up_read(&mm->mmap_sem);
Kirill A. Shutemov0661a332015-02-10 14:10:04 -080089 error = vfs_fsync_range(file, fstart, fend, 1);
Andrew Morton707c21c2006-03-24 03:18:13 -080090 fput(file);
Peter Zijlstra204ec842006-09-25 23:31:01 -070091 if (error || start >= end)
92 goto out;
93 down_read(&mm->mmap_sem);
94 vma = find_vma(mm, start);
Andrew Morton9c50823e2006-03-24 03:18:12 -080095 } else {
Peter Zijlstra204ec842006-09-25 23:31:01 -070096 if (start >= end) {
97 error = 0;
98 goto out_unlock;
99 }
Andrew Morton9c50823e2006-03-24 03:18:12 -0800100 vma = vma->vm_next;
101 }
Peter Zijlstra204ec842006-09-25 23:31:01 -0700102 }
Andrew Morton9c50823e2006-03-24 03:18:12 -0800103out_unlock:
Peter Zijlstra204ec842006-09-25 23:31:01 -0700104 up_read(&mm->mmap_sem);
Andrew Morton9c50823e2006-03-24 03:18:12 -0800105out:
Peter Zijlstra204ec842006-09-25 23:31:01 -0700106 return error ? : unmapped_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}