Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * linux/mm/msync.c |
| 4 | * |
| 5 | * Copyright (C) 1994-1999 Linus Torvalds |
| 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * The msync() system call. |
| 10 | */ |
Andrew Morton | 8f2e9f1 | 2006-03-24 03:18:15 -0800 | [diff] [blame] | 11 | #include <linux/fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/mm.h> |
| 13 | #include <linux/mman.h> |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 14 | #include <linux/file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/syscalls.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 16 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | /* |
| 19 | * MS_SYNC syncs the entire file - including mappings. |
| 20 | * |
Peter Zijlstra | 204ec84 | 2006-09-25 23:31:01 -0700 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
Amos Waterland | 16538c4 | 2006-03-24 18:30:53 +0100 | [diff] [blame] | 29 | * So by _not_ starting I/O in MS_ASYNC we provide complete flexibility to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | * applications. |
| 31 | */ |
Heiko Carstens | 6a6160a | 2009-01-14 14:14:15 +0100 | [diff] [blame] | 32 | SYSCALL_DEFINE3(msync, unsigned long, start, size_t, len, int, flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | { |
| 34 | unsigned long end; |
Peter Zijlstra | 204ec84 | 2006-09-25 23:31:01 -0700 | [diff] [blame] | 35 | struct mm_struct *mm = current->mm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | struct vm_area_struct *vma; |
Andrew Morton | 676758b | 2006-03-24 03:18:14 -0800 | [diff] [blame] | 37 | int unmapped_error = 0; |
| 38 | int error = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | if (flags & ~(MS_ASYNC | MS_INVALIDATE | MS_SYNC)) |
| 41 | goto out; |
Alexander Kuleshov | b0d61c7 | 2015-11-05 18:46:32 -0800 | [diff] [blame] | 42 | if (offset_in_page(start)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | 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 Zijlstra | 204ec84 | 2006-09-25 23:31:01 -0700 | [diff] [blame] | 58 | down_read(&mm->mmap_sem); |
| 59 | vma = find_vma(mm, start); |
| 60 | for (;;) { |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 61 | struct file *file; |
Matthew Wilcox | 7fc34a6 | 2014-06-04 16:10:44 -0700 | [diff] [blame] | 62 | loff_t fstart, fend; |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 63 | |
Peter Zijlstra | 204ec84 | 2006-09-25 23:31:01 -0700 | [diff] [blame] | 64 | /* Still start < end. */ |
| 65 | error = -ENOMEM; |
| 66 | if (!vma) |
| 67 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | /* Here start < vma->vm_end. */ |
| 69 | if (start < vma->vm_start) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | start = vma->vm_start; |
Peter Zijlstra | 204ec84 | 2006-09-25 23:31:01 -0700 | [diff] [blame] | 71 | if (start >= end) |
| 72 | goto out_unlock; |
| 73 | unmapped_error = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | } |
| 75 | /* Here vma->vm_start <= start < vma->vm_end. */ |
Peter Zijlstra | 204ec84 | 2006-09-25 23:31:01 -0700 | [diff] [blame] | 76 | if ((flags & MS_INVALIDATE) && |
| 77 | (vma->vm_flags & VM_LOCKED)) { |
| 78 | error = -EBUSY; |
| 79 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 81 | file = vma->vm_file; |
Namjae Jeon | 496a8e6 | 2014-07-02 15:22:36 -0700 | [diff] [blame] | 82 | fstart = (start - vma->vm_start) + |
| 83 | ((loff_t)vma->vm_pgoff << PAGE_SHIFT); |
Matthew Wilcox | 7fc34a6 | 2014-06-04 16:10:44 -0700 | [diff] [blame] | 84 | fend = fstart + (min(end, vma->vm_end) - start) - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | start = vma->vm_end; |
Peter Zijlstra | 204ec84 | 2006-09-25 23:31:01 -0700 | [diff] [blame] | 86 | if ((flags & MS_SYNC) && file && |
Andrew Morton | 707c21c | 2006-03-24 03:18:13 -0800 | [diff] [blame] | 87 | (vma->vm_flags & VM_SHARED)) { |
Andrew Morton | 707c21c | 2006-03-24 03:18:13 -0800 | [diff] [blame] | 88 | get_file(file); |
Peter Zijlstra | 204ec84 | 2006-09-25 23:31:01 -0700 | [diff] [blame] | 89 | up_read(&mm->mmap_sem); |
Kirill A. Shutemov | 0661a33 | 2015-02-10 14:10:04 -0800 | [diff] [blame] | 90 | error = vfs_fsync_range(file, fstart, fend, 1); |
Andrew Morton | 707c21c | 2006-03-24 03:18:13 -0800 | [diff] [blame] | 91 | fput(file); |
Peter Zijlstra | 204ec84 | 2006-09-25 23:31:01 -0700 | [diff] [blame] | 92 | if (error || start >= end) |
| 93 | goto out; |
| 94 | down_read(&mm->mmap_sem); |
| 95 | vma = find_vma(mm, start); |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 96 | } else { |
Peter Zijlstra | 204ec84 | 2006-09-25 23:31:01 -0700 | [diff] [blame] | 97 | if (start >= end) { |
| 98 | error = 0; |
| 99 | goto out_unlock; |
| 100 | } |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 101 | vma = vma->vm_next; |
| 102 | } |
Peter Zijlstra | 204ec84 | 2006-09-25 23:31:01 -0700 | [diff] [blame] | 103 | } |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 104 | out_unlock: |
Peter Zijlstra | 204ec84 | 2006-09-25 23:31:01 -0700 | [diff] [blame] | 105 | up_read(&mm->mmap_sem); |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 106 | out: |
Peter Zijlstra | 204ec84 | 2006-09-25 23:31:01 -0700 | [diff] [blame] | 107 | return error ? : unmapped_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | } |