msync.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/mm/msync.c
  4. *
  5. * Copyright (C) 1994-1999 Linus Torvalds
  6. */
  7. /*
  8. * The msync() system call.
  9. */
  10. #include <linux/fs.h>
  11. #include <linux/mm.h>
  12. #include <linux/mman.h>
  13. #include <linux/file.h>
  14. #include <linux/syscalls.h>
  15. #include <linux/sched.h>
  16. /*
  17. * MS_SYNC syncs the entire file - including mappings.
  18. *
  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
  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.
  27. * So by _not_ starting I/O in MS_ASYNC we provide complete flexibility to
  28. * applications.
  29. */
  30. SYSCALL_DEFINE3(msync, unsigned long, start, size_t, len, int, flags)
  31. {
  32. unsigned long end;
  33. struct mm_struct *mm = current->mm;
  34. struct vm_area_struct *vma;
  35. int unmapped_error = 0;
  36. int error = -EINVAL;
  37. if (flags & ~(MS_ASYNC | MS_INVALIDATE | MS_SYNC))
  38. goto out;
  39. if (offset_in_page(start))
  40. goto out;
  41. if ((flags & MS_ASYNC) && (flags & MS_SYNC))
  42. goto out;
  43. error = -ENOMEM;
  44. len = (len + ~PAGE_MASK) & PAGE_MASK;
  45. end = start + len;
  46. if (end < start)
  47. goto out;
  48. error = 0;
  49. if (end == start)
  50. goto out;
  51. /*
  52. * If the interval [start,end) covers some unmapped address ranges,
  53. * just ignore them, but return -ENOMEM at the end.
  54. */
  55. down_read(&mm->mmap_sem);
  56. vma = find_vma(mm, start);
  57. for (;;) {
  58. struct file *file;
  59. loff_t fstart, fend;
  60. /* Still start < end. */
  61. error = -ENOMEM;
  62. if (!vma)
  63. goto out_unlock;
  64. /* Here start < vma->vm_end. */
  65. if (start < vma->vm_start) {
  66. start = vma->vm_start;
  67. if (start >= end)
  68. goto out_unlock;
  69. unmapped_error = -ENOMEM;
  70. }
  71. /* Here vma->vm_start <= start < vma->vm_end. */
  72. if ((flags & MS_INVALIDATE) &&
  73. (vma->vm_flags & VM_LOCKED)) {
  74. error = -EBUSY;
  75. goto out_unlock;
  76. }
  77. file = vma->vm_file;
  78. fstart = (start - vma->vm_start) +
  79. ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
  80. fend = fstart + (min(end, vma->vm_end) - start) - 1;
  81. start = vma->vm_end;
  82. if ((flags & MS_SYNC) && file &&
  83. (vma->vm_flags & VM_SHARED)) {
  84. get_file(file);
  85. up_read(&mm->mmap_sem);
  86. error = vfs_fsync_range(file, fstart, fend, 1);
  87. fput(file);
  88. if (error || start >= end)
  89. goto out;
  90. down_read(&mm->mmap_sem);
  91. vma = find_vma(mm, start);
  92. } else {
  93. if (start >= end) {
  94. error = 0;
  95. goto out_unlock;
  96. }
  97. vma = vma->vm_next;
  98. }
  99. }
  100. out_unlock:
  101. up_read(&mm->mmap_sem);
  102. out:
  103. return error ? : unmapped_error;
  104. }