blob: 358d73cf7b78949c2e62567b1ec89d000dd2cdf7 [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>
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016/*
17 * MS_SYNC syncs the entire file - including mappings.
18 *
Peter Zijlstra204ec842006-09-25 23:31:01 -070019 * MS_ASYNC does not start I/O (it used to, up to 2.5.67).
20 * Nor does it marks the relevant pages dirty (it used to up to 2.6.17).
21 * Now it doesn't do anything, since dirty pages are properly tracked.
22 *
23 * The application may now run fsync() to
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * write out the dirty pages and wait on the writeout and check the result.
25 * Or the application may run fadvise(FADV_DONTNEED) against the fd to start
26 * async writeout immediately.
Amos Waterland16538c42006-03-24 18:30:53 +010027 * So by _not_ starting I/O in MS_ASYNC we provide complete flexibility to
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * applications.
29 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070030asmlinkage long sys_msync(unsigned long start, size_t len, int flags)
31{
32 unsigned long end;
Peter Zijlstra204ec842006-09-25 23:31:01 -070033 struct mm_struct *mm = current->mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 struct vm_area_struct *vma;
Andrew Morton676758b2006-03-24 03:18:14 -080035 int unmapped_error = 0;
36 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 if (flags & ~(MS_ASYNC | MS_INVALIDATE | MS_SYNC))
39 goto out;
40 if (start & ~PAGE_MASK)
41 goto out;
42 if ((flags & MS_ASYNC) && (flags & MS_SYNC))
43 goto out;
44 error = -ENOMEM;
45 len = (len + ~PAGE_MASK) & PAGE_MASK;
46 end = start + len;
47 if (end < start)
48 goto out;
49 error = 0;
50 if (end == start)
51 goto out;
52 /*
53 * If the interval [start,end) covers some unmapped address ranges,
54 * just ignore them, but return -ENOMEM at the end.
55 */
Peter Zijlstra204ec842006-09-25 23:31:01 -070056 down_read(&mm->mmap_sem);
57 vma = find_vma(mm, start);
58 for (;;) {
Andrew Morton9c50823e2006-03-24 03:18:12 -080059 struct file *file;
60
Peter Zijlstra204ec842006-09-25 23:31:01 -070061 /* Still start < end. */
62 error = -ENOMEM;
63 if (!vma)
64 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 /* Here start < vma->vm_end. */
66 if (start < vma->vm_start) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 start = vma->vm_start;
Peter Zijlstra204ec842006-09-25 23:31:01 -070068 if (start >= end)
69 goto out_unlock;
70 unmapped_error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 }
72 /* Here vma->vm_start <= start < vma->vm_end. */
Peter Zijlstra204ec842006-09-25 23:31:01 -070073 if ((flags & MS_INVALIDATE) &&
74 (vma->vm_flags & VM_LOCKED)) {
75 error = -EBUSY;
76 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 }
Andrew Morton9c50823e2006-03-24 03:18:12 -080078 file = vma->vm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 start = vma->vm_end;
Peter Zijlstra204ec842006-09-25 23:31:01 -070080 if ((flags & MS_SYNC) && file &&
Andrew Morton707c21c2006-03-24 03:18:13 -080081 (vma->vm_flags & VM_SHARED)) {
Andrew Morton707c21c2006-03-24 03:18:13 -080082 get_file(file);
Peter Zijlstra204ec842006-09-25 23:31:01 -070083 up_read(&mm->mmap_sem);
Andrew Morton8f2e9f12006-03-24 03:18:15 -080084 error = do_fsync(file, 0);
Andrew Morton707c21c2006-03-24 03:18:13 -080085 fput(file);
Peter Zijlstra204ec842006-09-25 23:31:01 -070086 if (error || start >= end)
87 goto out;
88 down_read(&mm->mmap_sem);
89 vma = find_vma(mm, start);
Andrew Morton9c50823e2006-03-24 03:18:12 -080090 } else {
Peter Zijlstra204ec842006-09-25 23:31:01 -070091 if (start >= end) {
92 error = 0;
93 goto out_unlock;
94 }
Andrew Morton9c50823e2006-03-24 03:18:12 -080095 vma = vma->vm_next;
96 }
Peter Zijlstra204ec842006-09-25 23:31:01 -070097 }
Andrew Morton9c50823e2006-03-24 03:18:12 -080098out_unlock:
Peter Zijlstra204ec842006-09-25 23:31:01 -070099 up_read(&mm->mmap_sem);
Andrew Morton9c50823e2006-03-24 03:18:12 -0800100out:
Peter Zijlstra204ec842006-09-25 23:31:01 -0700101 return error ? : unmapped_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}