msync.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * linux/mm/msync.c
  3. *
  4. * Copyright (C) 1994-1999 Linus Torvalds
  5. */
  6. /*
  7. * The msync() system call.
  8. */
  9. #include <linux/fs.h>
  10. #include <linux/mm.h>
  11. #include <linux/mman.h>
  12. #include <linux/file.h>
  13. #include <linux/syscalls.h>
  14. #include <linux/sched.h>
  15. /*
  16. * MS_SYNC syncs the entire file - including mappings.
  17. *
  18. * MS_ASYNC does not start I/O (it used to, up to 2.5.67).
  19. * Nor does it marks the relevant pages dirty (it used to up to 2.6.17).
  20. * Now it doesn't do anything, since dirty pages are properly tracked.
  21. *
  22. * The application may now run fsync() to
  23. * write out the dirty pages and wait on the writeout and check the result.
  24. * Or the application may run fadvise(FADV_DONTNEED) against the fd to start
  25. * async writeout immediately.
  26. * So by _not_ starting I/O in MS_ASYNC we provide complete flexibility to
  27. * applications.
  28. */
  29. SYSCALL_DEFINE3(msync, unsigned long, start, size_t, len, int, flags)
  30. {
  31. unsigned long end;
  32. struct mm_struct *mm = current->mm;
  33. struct vm_area_struct *vma;
  34. int unmapped_error = 0;
  35. int error = -EINVAL;
  36. if (flags & ~(MS_ASYNC | MS_INVALIDATE | MS_SYNC))
  37. goto out;
  38. if (start & ~PAGE_MASK)
  39. goto out;
  40. if ((flags & MS_ASYNC) && (flags & MS_SYNC))
  41. goto out;
  42. error = -ENOMEM;
  43. len = (len + ~PAGE_MASK) & PAGE_MASK;
  44. end = start + len;
  45. if (end < start)
  46. goto out;
  47. error = 0;
  48. if (end == start)
  49. goto out;
  50. /*
  51. * If the interval [start,end) covers some unmapped address ranges,
  52. * just ignore them, but return -ENOMEM at the end.
  53. */
  54. down_read(&mm->mmap_sem);
  55. vma = find_vma(mm, start);
  56. for (;;) {
  57. struct file *file;
  58. /* Still start < end. */
  59. error = -ENOMEM;
  60. if (!vma)
  61. goto out_unlock;
  62. /* Here start < vma->vm_end. */
  63. if (start < vma->vm_start) {
  64. start = vma->vm_start;
  65. if (start >= end)
  66. goto out_unlock;
  67. unmapped_error = -ENOMEM;
  68. }
  69. /* Here vma->vm_start <= start < vma->vm_end. */
  70. if ((flags & MS_INVALIDATE) &&
  71. (vma->vm_flags & VM_LOCKED)) {
  72. error = -EBUSY;
  73. goto out_unlock;
  74. }
  75. file = vma->vm_file;
  76. start = vma->vm_end;
  77. if ((flags & MS_SYNC) && file &&
  78. (vma->vm_flags & VM_SHARED)) {
  79. get_file(file);
  80. up_read(&mm->mmap_sem);
  81. error = vfs_fsync(file, 0);
  82. fput(file);
  83. if (error || start >= end)
  84. goto out;
  85. down_read(&mm->mmap_sem);
  86. vma = find_vma(mm, start);
  87. } else {
  88. if (start >= end) {
  89. error = 0;
  90. goto out_unlock;
  91. }
  92. vma = vma->vm_next;
  93. }
  94. }
  95. out_unlock:
  96. up_read(&mm->mmap_sem);
  97. out:
  98. return error ? : unmapped_error;
  99. }