fadvise.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * mm/fadvise.c
  3. *
  4. * Copyright (C) 2002, Linus Torvalds
  5. *
  6. * 11Jan2003 Andrew Morton
  7. * Initial version.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/file.h>
  11. #include <linux/fs.h>
  12. #include <linux/mm.h>
  13. #include <linux/pagemap.h>
  14. #include <linux/backing-dev.h>
  15. #include <linux/pagevec.h>
  16. #include <linux/fadvise.h>
  17. #include <linux/writeback.h>
  18. #include <linux/syscalls.h>
  19. #include <linux/swap.h>
  20. #include <asm/unistd.h>
  21. /*
  22. * POSIX_FADV_WILLNEED could set PG_Referenced, and POSIX_FADV_NOREUSE could
  23. * deactivate the pages and clear PG_Referenced.
  24. */
  25. SYSCALL_DEFINE(fadvise64_64)(int fd, loff_t offset, loff_t len, int advice)
  26. {
  27. int fput_needed;
  28. struct file *file = fget_light(fd, &fput_needed);
  29. struct address_space *mapping;
  30. struct backing_dev_info *bdi;
  31. loff_t endbyte; /* inclusive */
  32. pgoff_t start_index;
  33. pgoff_t end_index;
  34. unsigned long nrpages;
  35. int ret = 0;
  36. if (!file)
  37. return -EBADF;
  38. if (S_ISFIFO(file->f_path.dentry->d_inode->i_mode)) {
  39. ret = -ESPIPE;
  40. goto out;
  41. }
  42. mapping = file->f_mapping;
  43. if (!mapping || len < 0) {
  44. ret = -EINVAL;
  45. goto out;
  46. }
  47. if (mapping->a_ops->get_xip_mem) {
  48. switch (advice) {
  49. case POSIX_FADV_NORMAL:
  50. case POSIX_FADV_RANDOM:
  51. case POSIX_FADV_SEQUENTIAL:
  52. case POSIX_FADV_WILLNEED:
  53. case POSIX_FADV_NOREUSE:
  54. case POSIX_FADV_DONTNEED:
  55. /* no bad return value, but ignore advice */
  56. break;
  57. default:
  58. ret = -EINVAL;
  59. }
  60. goto out;
  61. }
  62. /* Careful about overflows. Len == 0 means "as much as possible" */
  63. endbyte = offset + len;
  64. if (!len || endbyte < len)
  65. endbyte = -1;
  66. else
  67. endbyte--; /* inclusive */
  68. bdi = mapping->backing_dev_info;
  69. switch (advice) {
  70. case POSIX_FADV_NORMAL:
  71. file->f_ra.ra_pages = bdi->ra_pages;
  72. spin_lock(&file->f_lock);
  73. file->f_mode &= ~FMODE_RANDOM;
  74. spin_unlock(&file->f_lock);
  75. break;
  76. case POSIX_FADV_RANDOM:
  77. spin_lock(&file->f_lock);
  78. file->f_mode |= FMODE_RANDOM;
  79. spin_unlock(&file->f_lock);
  80. break;
  81. case POSIX_FADV_SEQUENTIAL:
  82. file->f_ra.ra_pages = bdi->ra_pages * 2;
  83. spin_lock(&file->f_lock);
  84. file->f_mode &= ~FMODE_RANDOM;
  85. spin_unlock(&file->f_lock);
  86. break;
  87. case POSIX_FADV_WILLNEED:
  88. if (!mapping->a_ops->readpage) {
  89. ret = -EINVAL;
  90. break;
  91. }
  92. /* First and last PARTIAL page! */
  93. start_index = offset >> PAGE_CACHE_SHIFT;
  94. end_index = endbyte >> PAGE_CACHE_SHIFT;
  95. /* Careful about overflow on the "+1" */
  96. nrpages = end_index - start_index + 1;
  97. if (!nrpages)
  98. nrpages = ~0UL;
  99. ret = force_page_cache_readahead(mapping, file,
  100. start_index,
  101. nrpages);
  102. if (ret > 0)
  103. ret = 0;
  104. break;
  105. case POSIX_FADV_NOREUSE:
  106. break;
  107. case POSIX_FADV_DONTNEED:
  108. if (!bdi_write_congested(mapping->backing_dev_info))
  109. __filemap_fdatawrite_range(mapping, offset, endbyte,
  110. WB_SYNC_NONE);
  111. /*
  112. * First and last FULL page! Partial pages are deliberately
  113. * preserved on the expectation that it is better to preserve
  114. * needed memory than to discard unneeded memory.
  115. */
  116. start_index = (offset+(PAGE_CACHE_SIZE-1)) >> PAGE_CACHE_SHIFT;
  117. end_index = (endbyte >> PAGE_CACHE_SHIFT);
  118. if (end_index >= start_index) {
  119. unsigned long count = invalidate_mapping_pages(mapping,
  120. start_index, end_index);
  121. /*
  122. * If fewer pages were invalidated than expected then
  123. * it is possible that some of the pages were on
  124. * a per-cpu pagevec for a remote CPU. Drain all
  125. * pagevecs and try again.
  126. */
  127. if (count < (end_index - start_index + 1)) {
  128. lru_add_drain_all();
  129. invalidate_mapping_pages(mapping, start_index,
  130. end_index);
  131. }
  132. }
  133. break;
  134. default:
  135. ret = -EINVAL;
  136. }
  137. out:
  138. fput_light(file, fput_needed);
  139. return ret;
  140. }
  141. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  142. asmlinkage long SyS_fadvise64_64(long fd, loff_t offset, loff_t len, long advice)
  143. {
  144. return SYSC_fadvise64_64((int) fd, offset, len, (int) advice);
  145. }
  146. SYSCALL_ALIAS(sys_fadvise64_64, SyS_fadvise64_64);
  147. #endif
  148. #ifdef __ARCH_WANT_SYS_FADVISE64
  149. SYSCALL_DEFINE(fadvise64)(int fd, loff_t offset, size_t len, int advice)
  150. {
  151. return sys_fadvise64_64(fd, offset, len, advice);
  152. }
  153. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  154. asmlinkage long SyS_fadvise64(long fd, loff_t offset, long len, long advice)
  155. {
  156. return SYSC_fadvise64((int) fd, offset, (size_t)len, (int)advice);
  157. }
  158. SYSCALL_ALIAS(sys_fadvise64, SyS_fadvise64);
  159. #endif
  160. #endif