]> git.ipfire.org Git - thirdparty/linux.git/blame - mm/msync.c
cifs: pass a path to open_shroot and check if it is the root or not
[thirdparty/linux.git] / mm / msync.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1da177e4
LT
2/*
3 * linux/mm/msync.c
4 *
5 * Copyright (C) 1994-1999 Linus Torvalds
6 */
7
8/*
9 * The msync() system call.
10 */
8f2e9f15 11#include <linux/fs.h>
1da177e4
LT
12#include <linux/mm.h>
13#include <linux/mman.h>
9c50823e 14#include <linux/file.h>
1da177e4 15#include <linux/syscalls.h>
e8edc6e0 16#include <linux/sched.h>
1da177e4 17
1da177e4
LT
18/*
19 * MS_SYNC syncs the entire file - including mappings.
20 *
204ec841
PZ
21 * 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
1da177e4
LT
26 * 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.
16538c40 29 * So by _not_ starting I/O in MS_ASYNC we provide complete flexibility to
1da177e4
LT
30 * applications.
31 */
6a6160a7 32SYSCALL_DEFINE3(msync, unsigned long, start, size_t, len, int, flags)
1da177e4
LT
33{
34 unsigned long end;
204ec841 35 struct mm_struct *mm = current->mm;
1da177e4 36 struct vm_area_struct *vma;
676758bd
AM
37 int unmapped_error = 0;
38 int error = -EINVAL;
1da177e4 39
057d3389
AK
40 start = untagged_addr(start);
41
1da177e4
LT
42 if (flags & ~(MS_ASYNC | MS_INVALIDATE | MS_SYNC))
43 goto out;
b0d61c7e 44 if (offset_in_page(start))
1da177e4
LT
45 goto out;
46 if ((flags & MS_ASYNC) && (flags & MS_SYNC))
47 goto out;
48 error = -ENOMEM;
49 len = (len + ~PAGE_MASK) & PAGE_MASK;
50 end = start + len;
51 if (end < start)
52 goto out;
53 error = 0;
54 if (end == start)
55 goto out;
56 /*
57 * If the interval [start,end) covers some unmapped address ranges,
58 * just ignore them, but return -ENOMEM at the end.
59 */
d8ed45c5 60 mmap_read_lock(mm);
204ec841
PZ
61 vma = find_vma(mm, start);
62 for (;;) {
9c50823e 63 struct file *file;
7fc34a62 64 loff_t fstart, fend;
9c50823e 65
204ec841
PZ
66 /* Still start < end. */
67 error = -ENOMEM;
68 if (!vma)
69 goto out_unlock;
1da177e4
LT
70 /* Here start < vma->vm_end. */
71 if (start < vma->vm_start) {
1da177e4 72 start = vma->vm_start;
204ec841
PZ
73 if (start >= end)
74 goto out_unlock;
75 unmapped_error = -ENOMEM;
1da177e4
LT
76 }
77 /* Here vma->vm_start <= start < vma->vm_end. */
204ec841
PZ
78 if ((flags & MS_INVALIDATE) &&
79 (vma->vm_flags & VM_LOCKED)) {
80 error = -EBUSY;
81 goto out_unlock;
1da177e4 82 }
9c50823e 83 file = vma->vm_file;
496a8e68
NJ
84 fstart = (start - vma->vm_start) +
85 ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
7fc34a62 86 fend = fstart + (min(end, vma->vm_end) - start) - 1;
1da177e4 87 start = vma->vm_end;
204ec841 88 if ((flags & MS_SYNC) && file &&
707c21c8 89 (vma->vm_flags & VM_SHARED)) {
707c21c8 90 get_file(file);
d8ed45c5 91 mmap_read_unlock(mm);
0661a336 92 error = vfs_fsync_range(file, fstart, fend, 1);
707c21c8 93 fput(file);
204ec841
PZ
94 if (error || start >= end)
95 goto out;
d8ed45c5 96 mmap_read_lock(mm);
204ec841 97 vma = find_vma(mm, start);
9c50823e 98 } else {
204ec841
PZ
99 if (start >= end) {
100 error = 0;
101 goto out_unlock;
102 }
9c50823e
AM
103 vma = vma->vm_next;
104 }
204ec841 105 }
9c50823e 106out_unlock:
d8ed45c5 107 mmap_read_unlock(mm);
9c50823e 108out:
204ec841 109 return error ? : unmapped_error;
1da177e4 110}