msync.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 (offset_in_page(start))
  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. loff_t fstart, fend;
  59. /* Still start < end. */
  60. error = -ENOMEM;
  61. if (!vma)
  62. goto out_unlock;
  63. /* Here start < vma->vm_end. */
  64. if (start < vma->vm_start) {
  65. start = vma->vm_start;
  66. if (start >= end)
  67. goto out_unlock;
  68. unmapped_error = -ENOMEM;
  69. }
  70. /* Here vma->vm_start <= start < vma->vm_end. */
  71. if ((flags & MS_INVALIDATE) &&
  72. (vma->vm_flags & VM_LOCKED)) {
  73. error = -EBUSY;
  74. goto out_unlock;
  75. }
  76. file = vma->vm_file;
  77. fstart = (start - vma->vm_start) +
  78. ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
  79. fend = fstart + (min(end, vma->vm_end) - start) - 1;
  80. start = vma->vm_end;
  81. if ((flags & MS_SYNC) && file &&
  82. (vma->vm_flags & VM_SHARED)) {
  83. get_file(file);
  84. up_read(&mm->mmap_sem);
  85. error = vfs_fsync_range(file, fstart, fend, 1);
  86. fput(file);
  87. if (error || start >= end)
  88. goto out;
  89. down_read(&mm->mmap_sem);
  90. vma = find_vma(mm, start);
  91. } else {
  92. if (start >= end) {
  93. error = 0;
  94. goto out_unlock;
  95. }
  96. vma = vma->vm_next;
  97. }
  98. }
  99. out_unlock:
  100. up_read(&mm->mmap_sem);
  101. out:
  102. return error ? : unmapped_error;
  103. }