blob: ef30a429623a2f8191d9364a0c9a8b4aab0ddc8e [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/mm/msync.c
4 *
5 * Copyright (C) 1994-1999 Linus Torvalds
6 */
7
8/*
9 * The msync() system call.
10 */
Andrew Morton8f2e9f12006-03-24 03:18:15 -080011#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/mm.h>
13#include <linux/mman.h>
Andrew Morton9c50823e2006-03-24 03:18:12 -080014#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/syscalls.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040016#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Linus Torvalds1da177e2005-04-16 15:20:36 -070018/*
19 * MS_SYNC syncs the entire file - including mappings.
20 *
Peter Zijlstra204ec842006-09-25 23:31:01 -070021 * MS_ASYNC does not start I/O (it used to, up to 2.5.67).
22 * Nor does it marks the relevant pages dirty (it used to up to 2.6.17).
23 * Now it doesn't do anything, since dirty pages are properly tracked.
24 *
25 * The application may now run fsync() to
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 * write out the dirty pages and wait on the writeout and check the result.
27 * Or the application may run fadvise(FADV_DONTNEED) against the fd to start
28 * async writeout immediately.
Amos Waterland16538c42006-03-24 18:30:53 +010029 * So by _not_ starting I/O in MS_ASYNC we provide complete flexibility to
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * applications.
31 */
Heiko Carstens6a6160a2009-01-14 14:14:15 +010032SYSCALL_DEFINE3(msync, unsigned long, start, size_t, len, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
34 unsigned long end;
Peter Zijlstra204ec842006-09-25 23:31:01 -070035 struct mm_struct *mm = current->mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 struct vm_area_struct *vma;
Andrew Morton676758b2006-03-24 03:18:14 -080037 int unmapped_error = 0;
38 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 if (flags & ~(MS_ASYNC | MS_INVALIDATE | MS_SYNC))
41 goto out;
Alexander Kuleshovb0d61c72015-11-05 18:46:32 -080042 if (offset_in_page(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 goto out;
44 if ((flags & MS_ASYNC) && (flags & MS_SYNC))
45 goto out;
46 error = -ENOMEM;
47 len = (len + ~PAGE_MASK) & PAGE_MASK;
48 end = start + len;
49 if (end < start)
50 goto out;
51 error = 0;
52 if (end == start)
53 goto out;
54 /*
55 * If the interval [start,end) covers some unmapped address ranges,
56 * just ignore them, but return -ENOMEM at the end.
57 */
Peter Zijlstra204ec842006-09-25 23:31:01 -070058 down_read(&mm->mmap_sem);
59 vma = find_vma(mm, start);
60 for (;;) {
Andrew Morton9c50823e2006-03-24 03:18:12 -080061 struct file *file;
Matthew Wilcox7fc34a62014-06-04 16:10:44 -070062 loff_t fstart, fend;
Andrew Morton9c50823e2006-03-24 03:18:12 -080063
Peter Zijlstra204ec842006-09-25 23:31:01 -070064 /* Still start < end. */
65 error = -ENOMEM;
66 if (!vma)
67 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 /* Here start < vma->vm_end. */
69 if (start < vma->vm_start) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 start = vma->vm_start;
Peter Zijlstra204ec842006-09-25 23:31:01 -070071 if (start >= end)
72 goto out_unlock;
73 unmapped_error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 }
75 /* Here vma->vm_start <= start < vma->vm_end. */
Peter Zijlstra204ec842006-09-25 23:31:01 -070076 if ((flags & MS_INVALIDATE) &&
77 (vma->vm_flags & VM_LOCKED)) {
78 error = -EBUSY;
79 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 }
Andrew Morton9c50823e2006-03-24 03:18:12 -080081 file = vma->vm_file;
Namjae Jeon496a8e62014-07-02 15:22:36 -070082 fstart = (start - vma->vm_start) +
83 ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
Matthew Wilcox7fc34a62014-06-04 16:10:44 -070084 fend = fstart + (min(end, vma->vm_end) - start) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 start = vma->vm_end;
Peter Zijlstra204ec842006-09-25 23:31:01 -070086 if ((flags & MS_SYNC) && file &&
Andrew Morton707c21c2006-03-24 03:18:13 -080087 (vma->vm_flags & VM_SHARED)) {
Andrew Morton707c21c2006-03-24 03:18:13 -080088 get_file(file);
Peter Zijlstra204ec842006-09-25 23:31:01 -070089 up_read(&mm->mmap_sem);
Kirill A. Shutemov0661a332015-02-10 14:10:04 -080090 error = vfs_fsync_range(file, fstart, fend, 1);
Andrew Morton707c21c2006-03-24 03:18:13 -080091 fput(file);
Peter Zijlstra204ec842006-09-25 23:31:01 -070092 if (error || start >= end)
93 goto out;
94 down_read(&mm->mmap_sem);
95 vma = find_vma(mm, start);
Andrew Morton9c50823e2006-03-24 03:18:12 -080096 } else {
Peter Zijlstra204ec842006-09-25 23:31:01 -070097 if (start >= end) {
98 error = 0;
99 goto out_unlock;
100 }
Andrew Morton9c50823e2006-03-24 03:18:12 -0800101 vma = vma->vm_next;
102 }
Peter Zijlstra204ec842006-09-25 23:31:01 -0700103 }
Andrew Morton9c50823e2006-03-24 03:18:12 -0800104out_unlock:
Peter Zijlstra204ec842006-09-25 23:31:01 -0700105 up_read(&mm->mmap_sem);
Andrew Morton9c50823e2006-03-24 03:18:12 -0800106out:
Peter Zijlstra204ec842006-09-25 23:31:01 -0700107 return error ? : unmapped_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}