blob: 4083209b7f02e068b94ac50c16d0739a539bc47b [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;
61
Peter Zijlstra204ec842006-09-25 23:31:01 -070062 /* Still start < end. */
63 error = -ENOMEM;
64 if (!vma)
65 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 /* Here start < vma->vm_end. */
67 if (start < vma->vm_start) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 start = vma->vm_start;
Peter Zijlstra204ec842006-09-25 23:31:01 -070069 if (start >= end)
70 goto out_unlock;
71 unmapped_error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 }
73 /* Here vma->vm_start <= start < vma->vm_end. */
Peter Zijlstra204ec842006-09-25 23:31:01 -070074 if ((flags & MS_INVALIDATE) &&
75 (vma->vm_flags & VM_LOCKED)) {
76 error = -EBUSY;
77 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 }
Andrew Morton9c50823e2006-03-24 03:18:12 -080079 file = vma->vm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 start = vma->vm_end;
Peter Zijlstra204ec842006-09-25 23:31:01 -070081 if ((flags & MS_SYNC) && file &&
Andrew Morton707c21c2006-03-24 03:18:13 -080082 (vma->vm_flags & VM_SHARED)) {
Andrew Morton707c21c2006-03-24 03:18:13 -080083 get_file(file);
Peter Zijlstra204ec842006-09-25 23:31:01 -070084 up_read(&mm->mmap_sem);
Christoph Hellwig4c728ef2008-12-22 21:11:15 +010085 error = vfs_fsync(file, file->f_path.dentry, 0);
Andrew Morton707c21c2006-03-24 03:18:13 -080086 fput(file);
Peter Zijlstra204ec842006-09-25 23:31:01 -070087 if (error || start >= end)
88 goto out;
89 down_read(&mm->mmap_sem);
90 vma = find_vma(mm, start);
Andrew Morton9c50823e2006-03-24 03:18:12 -080091 } else {
Peter Zijlstra204ec842006-09-25 23:31:01 -070092 if (start >= end) {
93 error = 0;
94 goto out_unlock;
95 }
Andrew Morton9c50823e2006-03-24 03:18:12 -080096 vma = vma->vm_next;
97 }
Peter Zijlstra204ec842006-09-25 23:31:01 -070098 }
Andrew Morton9c50823e2006-03-24 03:18:12 -080099out_unlock:
Peter Zijlstra204ec842006-09-25 23:31:01 -0700100 up_read(&mm->mmap_sem);
Andrew Morton9c50823e2006-03-24 03:18:12 -0800101out:
Peter Zijlstra204ec842006-09-25 23:31:01 -0700102 return error ? : unmapped_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}