Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/mm/msync.c |
| 3 | * |
| 4 | * Copyright (C) 1994-1999 Linus Torvalds |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * The msync() system call. |
| 9 | */ |
Andrew Morton | 8f2e9f1 | 2006-03-24 03:18:15 -0800 | [diff] [blame] | 10 | #include <linux/fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/mm.h> |
| 12 | #include <linux/mman.h> |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 13 | #include <linux/file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/syscalls.h> |
| 15 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | /* |
| 17 | * MS_SYNC syncs the entire file - including mappings. |
| 18 | * |
Peter Zijlstra | 204ec84 | 2006-09-25 23:31:01 -0700 | [diff] [blame] | 19 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | * 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 Waterland | 16538c4 | 2006-03-24 18:30:53 +0100 | [diff] [blame] | 27 | * 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] | 28 | * applications. |
| 29 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | asmlinkage long sys_msync(unsigned long start, size_t len, int flags) |
| 31 | { |
| 32 | unsigned long end; |
Peter Zijlstra | 204ec84 | 2006-09-25 23:31:01 -0700 | [diff] [blame] | 33 | struct mm_struct *mm = current->mm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | struct vm_area_struct *vma; |
Andrew Morton | 676758b | 2006-03-24 03:18:14 -0800 | [diff] [blame] | 35 | int unmapped_error = 0; |
| 36 | int error = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | 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 Zijlstra | 204ec84 | 2006-09-25 23:31:01 -0700 | [diff] [blame] | 56 | down_read(&mm->mmap_sem); |
| 57 | vma = find_vma(mm, start); |
| 58 | for (;;) { |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 59 | struct file *file; |
| 60 | |
Peter Zijlstra | 204ec84 | 2006-09-25 23:31:01 -0700 | [diff] [blame] | 61 | /* Still start < end. */ |
| 62 | error = -ENOMEM; |
| 63 | if (!vma) |
| 64 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | /* Here start < vma->vm_end. */ |
| 66 | if (start < vma->vm_start) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | start = vma->vm_start; |
Peter Zijlstra | 204ec84 | 2006-09-25 23:31:01 -0700 | [diff] [blame] | 68 | if (start >= end) |
| 69 | goto out_unlock; |
| 70 | unmapped_error = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | } |
| 72 | /* Here vma->vm_start <= start < vma->vm_end. */ |
Peter Zijlstra | 204ec84 | 2006-09-25 23:31:01 -0700 | [diff] [blame] | 73 | if ((flags & MS_INVALIDATE) && |
| 74 | (vma->vm_flags & VM_LOCKED)) { |
| 75 | error = -EBUSY; |
| 76 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | } |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 78 | file = vma->vm_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | start = vma->vm_end; |
Peter Zijlstra | 204ec84 | 2006-09-25 23:31:01 -0700 | [diff] [blame] | 80 | if ((flags & MS_SYNC) && file && |
Andrew Morton | 707c21c | 2006-03-24 03:18:13 -0800 | [diff] [blame] | 81 | (vma->vm_flags & VM_SHARED)) { |
Andrew Morton | 707c21c | 2006-03-24 03:18:13 -0800 | [diff] [blame] | 82 | get_file(file); |
Peter Zijlstra | 204ec84 | 2006-09-25 23:31:01 -0700 | [diff] [blame] | 83 | up_read(&mm->mmap_sem); |
Andrew Morton | 8f2e9f1 | 2006-03-24 03:18:15 -0800 | [diff] [blame] | 84 | error = do_fsync(file, 0); |
Andrew Morton | 707c21c | 2006-03-24 03:18:13 -0800 | [diff] [blame] | 85 | fput(file); |
Peter Zijlstra | 204ec84 | 2006-09-25 23:31:01 -0700 | [diff] [blame] | 86 | if (error || start >= end) |
| 87 | goto out; |
| 88 | down_read(&mm->mmap_sem); |
| 89 | vma = find_vma(mm, start); |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 90 | } else { |
Peter Zijlstra | 204ec84 | 2006-09-25 23:31:01 -0700 | [diff] [blame] | 91 | if (start >= end) { |
| 92 | error = 0; |
| 93 | goto out_unlock; |
| 94 | } |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 95 | vma = vma->vm_next; |
| 96 | } |
Peter Zijlstra | 204ec84 | 2006-09-25 23:31:01 -0700 | [diff] [blame] | 97 | } |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 98 | out_unlock: |
Peter Zijlstra | 204ec84 | 2006-09-25 23:31:01 -0700 | [diff] [blame] | 99 | up_read(&mm->mmap_sem); |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 100 | out: |
Peter Zijlstra | 204ec84 | 2006-09-25 23:31:01 -0700 | [diff] [blame] | 101 | return error ? : unmapped_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | } |