xref: /linux/mm/msync.c (revision 1b78070aaef63512688aebfbc82365ef9d6660f1)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *	linux/mm/msync.c
4  *
5  * Copyright (C) 1994-1999  Linus Torvalds
6  */
7 
8 /*
9  * The msync() system call.
10  */
11 #include <linux/fs.h>
12 #include <linux/mm.h>
13 #include <linux/mman.h>
14 #include <linux/file.h>
15 #include <linux/pagemap.h>
16 #include <linux/syscalls.h>
17 #include <linux/sched.h>
18 
19 /*
20  * MS_SYNC syncs the entire file - including mappings.
21  *
22  * MS_ASYNC does not start I/O (it used to, up to 2.5.67).
23  * Nor does it marks the relevant pages dirty (it used to up to 2.6.17).
24  * Now it doesn't do anything, since dirty pages are properly tracked.
25  *
26  * The application may now run fsync() to
27  * write out the dirty pages and wait on the writeout and check the result.
28  * Or the application may run fadvise(FADV_DONTNEED) against the fd to start
29  * async writeout immediately.
30  * So by _not_ starting I/O in MS_ASYNC we provide complete flexibility to
31  * applications.
32  */
33 SYSCALL_DEFINE3(msync, unsigned long, start, size_t, len, int, flags)
34 {
35 	unsigned long end;
36 	struct mm_struct *mm = current->mm;
37 	struct vm_area_struct *vma;
38 	int unmapped_error = 0;
39 	int error = -EINVAL;
40 
41 	start = untagged_addr(start);
42 
43 	if (flags & ~(MS_ASYNC | MS_INVALIDATE | MS_SYNC))
44 		goto out;
45 	if (offset_in_page(start))
46 		goto out;
47 	if ((flags & MS_ASYNC) && (flags & MS_SYNC))
48 		goto out;
49 	error = -ENOMEM;
50 	len = (len + ~PAGE_MASK) & PAGE_MASK;
51 	end = start + len;
52 	if (end < start)
53 		goto out;
54 	error = 0;
55 	if (end == start)
56 		goto out;
57 	/*
58 	 * If the interval [start,end) covers some unmapped address ranges,
59 	 * just ignore them, but return -ENOMEM at the end. Besides, if the
60 	 * flag is MS_ASYNC (w/o MS_INVALIDATE) the result would be -ENOMEM
61 	 * anyway and there is nothing left to do, so return immediately.
62 	 */
63 	mmap_read_lock(mm);
64 	vma = find_vma(mm, start);
65 	for (;;) {
66 		struct file *file;
67 		loff_t fstart, fend;
68 
69 		/* Still start < end. */
70 		error = -ENOMEM;
71 		if (!vma)
72 			goto out_unlock;
73 		/* Here start < vma->vm_end. */
74 		if (start < vma->vm_start) {
75 			if (flags == MS_ASYNC)
76 				goto out_unlock;
77 			start = vma->vm_start;
78 			if (start >= end)
79 				goto out_unlock;
80 			unmapped_error = -ENOMEM;
81 		}
82 		/* Here vma->vm_start <= start < vma->vm_end. */
83 		if ((flags & MS_INVALIDATE) &&
84 				(vma->vm_flags & VM_LOCKED)) {
85 			error = -EBUSY;
86 			goto out_unlock;
87 		}
88 		file = vma->vm_file;
89 		fstart = (loff_t)linear_page_index(vma, start) << PAGE_SHIFT;
90 		fend = fstart + (min(end, vma->vm_end) - start) - 1;
91 		start = vma->vm_end;
92 		if ((flags & MS_SYNC) && file &&
93 				(vma->vm_flags & VM_SHARED)) {
94 			get_file(file);
95 			mmap_read_unlock(mm);
96 			error = vfs_fsync_range(file, fstart, fend, 1);
97 			fput(file);
98 			if (error || start >= end)
99 				goto out;
100 			mmap_read_lock(mm);
101 			vma = find_vma(mm, start);
102 		} else {
103 			if (start >= end) {
104 				error = 0;
105 				goto out_unlock;
106 			}
107 			vma = find_vma(mm, vma->vm_end);
108 		}
109 	}
110 out_unlock:
111 	mmap_read_unlock(mm);
112 out:
113 	return error ? : unmapped_error;
114 }
115