fadvise.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * mm/fadvise.c
  4. *
  5. * Copyright (C) 2002, Linus Torvalds
  6. *
  7. * 11Jan2003 Andrew Morton
  8. * Initial version.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/file.h>
  12. #include <linux/fs.h>
  13. #include <linux/mm.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/backing-dev.h>
  16. #include <linux/pagevec.h>
  17. #include <linux/fadvise.h>
  18. #include <linux/writeback.h>
  19. #include <linux/syscalls.h>
  20. #include <linux/swap.h>
  21. #include <asm/unistd.h>
  22. #include "internal.h"
  23. /*
  24. * POSIX_FADV_WILLNEED could set PG_Referenced, and POSIX_FADV_NOREUSE could
  25. * deactivate the pages and clear PG_Referenced.
  26. */
  27. int generic_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
  28. {
  29. struct inode *inode;
  30. struct address_space *mapping;
  31. struct backing_dev_info *bdi;
  32. loff_t endbyte; /* inclusive */
  33. pgoff_t start_index;
  34. pgoff_t end_index;
  35. unsigned long nrpages;
  36. inode = file_inode(file);
  37. if (S_ISFIFO(inode->i_mode))
  38. return -ESPIPE;
  39. mapping = file->f_mapping;
  40. if (!mapping || len < 0)
  41. return -EINVAL;
  42. bdi = inode_to_bdi(mapping->host);
  43. if (IS_DAX(inode) || (bdi == &noop_backing_dev_info)) {
  44. switch (advice) {
  45. case POSIX_FADV_NORMAL:
  46. case POSIX_FADV_RANDOM:
  47. case POSIX_FADV_SEQUENTIAL:
  48. case POSIX_FADV_WILLNEED:
  49. case POSIX_FADV_NOREUSE:
  50. case POSIX_FADV_DONTNEED:
  51. /* no bad return value, but ignore advice */
  52. break;
  53. default:
  54. return -EINVAL;
  55. }
  56. return 0;
  57. }
  58. /*
  59. * Careful about overflows. Len == 0 means "as much as possible". Use
  60. * unsigned math because signed overflows are undefined and UBSan
  61. * complains.
  62. */
  63. endbyte = (u64)offset + (u64)len;
  64. if (!len || endbyte < len)
  65. endbyte = -1;
  66. else
  67. endbyte--; /* inclusive */
  68. switch (advice) {
  69. case POSIX_FADV_NORMAL:
  70. file->f_ra.ra_pages = bdi->ra_pages;
  71. spin_lock(&file->f_lock);
  72. file->f_mode &= ~(FMODE_RANDOM | FMODE_NOREUSE);
  73. spin_unlock(&file->f_lock);
  74. break;
  75. case POSIX_FADV_RANDOM:
  76. spin_lock(&file->f_lock);
  77. file->f_mode |= FMODE_RANDOM;
  78. spin_unlock(&file->f_lock);
  79. break;
  80. case POSIX_FADV_SEQUENTIAL:
  81. file->f_ra.ra_pages = bdi->ra_pages * 2;
  82. spin_lock(&file->f_lock);
  83. file->f_mode &= ~FMODE_RANDOM;
  84. spin_unlock(&file->f_lock);
  85. break;
  86. case POSIX_FADV_WILLNEED:
  87. /* First and last PARTIAL page! */
  88. start_index = offset >> PAGE_SHIFT;
  89. end_index = endbyte >> PAGE_SHIFT;
  90. /* Careful about overflow on the "+1" */
  91. nrpages = end_index - start_index + 1;
  92. if (!nrpages)
  93. nrpages = ~0UL;
  94. force_page_cache_readahead(mapping, file, start_index, nrpages);
  95. break;
  96. case POSIX_FADV_NOREUSE:
  97. spin_lock(&file->f_lock);
  98. file->f_mode |= FMODE_NOREUSE;
  99. spin_unlock(&file->f_lock);
  100. break;
  101. case POSIX_FADV_DONTNEED:
  102. __filemap_fdatawrite_range(mapping, offset, endbyte,
  103. WB_SYNC_NONE);
  104. /*
  105. * First and last FULL page! Partial pages are deliberately
  106. * preserved on the expectation that it is better to preserve
  107. * needed memory than to discard unneeded memory.
  108. */
  109. start_index = (offset+(PAGE_SIZE-1)) >> PAGE_SHIFT;
  110. end_index = (endbyte >> PAGE_SHIFT);
  111. /*
  112. * The page at end_index will be inclusively discarded according
  113. * by invalidate_mapping_pages(), so subtracting 1 from
  114. * end_index means we will skip the last page. But if endbyte
  115. * is page aligned or is at the end of file, we should not skip
  116. * that page - discarding the last page is safe enough.
  117. */
  118. if ((endbyte & ~PAGE_MASK) != ~PAGE_MASK &&
  119. endbyte != inode->i_size - 1) {
  120. /* First page is tricky as 0 - 1 = -1, but pgoff_t
  121. * is unsigned, so the end_index >= start_index
  122. * check below would be true and we'll discard the whole
  123. * file cache which is not what was asked.
  124. */
  125. if (end_index == 0)
  126. break;
  127. end_index--;
  128. }
  129. if (end_index >= start_index) {
  130. unsigned long nr_pagevec = 0;
  131. /*
  132. * It's common to FADV_DONTNEED right after
  133. * the read or write that instantiates the
  134. * pages, in which case there will be some
  135. * sitting on the local LRU cache. Try to
  136. * avoid the expensive remote drain and the
  137. * second cache tree walk below by flushing
  138. * them out right away.
  139. */
  140. lru_add_drain();
  141. invalidate_mapping_pagevec(mapping,
  142. start_index, end_index,
  143. &nr_pagevec);
  144. /*
  145. * If fewer pages were invalidated than expected then
  146. * it is possible that some of the pages were on
  147. * a per-cpu pagevec for a remote CPU. Drain all
  148. * pagevecs and try again.
  149. */
  150. if (nr_pagevec) {
  151. lru_add_drain_all();
  152. invalidate_mapping_pages(mapping, start_index,
  153. end_index);
  154. }
  155. }
  156. break;
  157. default:
  158. return -EINVAL;
  159. }
  160. return 0;
  161. }
  162. EXPORT_SYMBOL(generic_fadvise);
  163. int vfs_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
  164. {
  165. if (file->f_op->fadvise)
  166. return file->f_op->fadvise(file, offset, len, advice);
  167. return generic_fadvise(file, offset, len, advice);
  168. }
  169. EXPORT_SYMBOL(vfs_fadvise);
  170. #ifdef CONFIG_ADVISE_SYSCALLS
  171. int ksys_fadvise64_64(int fd, loff_t offset, loff_t len, int advice)
  172. {
  173. struct fd f = fdget(fd);
  174. int ret;
  175. if (!f.file)
  176. return -EBADF;
  177. ret = vfs_fadvise(f.file, offset, len, advice);
  178. fdput(f);
  179. return ret;
  180. }
  181. SYSCALL_DEFINE4(fadvise64_64, int, fd, loff_t, offset, loff_t, len, int, advice)
  182. {
  183. return ksys_fadvise64_64(fd, offset, len, advice);
  184. }
  185. #ifdef __ARCH_WANT_SYS_FADVISE64
  186. SYSCALL_DEFINE4(fadvise64, int, fd, loff_t, offset, size_t, len, int, advice)
  187. {
  188. return ksys_fadvise64_64(fd, offset, len, advice);
  189. }
  190. #endif
  191. #if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_FADVISE64_64)
  192. COMPAT_SYSCALL_DEFINE6(fadvise64_64, int, fd, compat_arg_u64_dual(offset),
  193. compat_arg_u64_dual(len), int, advice)
  194. {
  195. return ksys_fadvise64_64(fd, compat_arg_u64_glue(offset),
  196. compat_arg_u64_glue(len), advice);
  197. }
  198. #endif
  199. #endif