fadvise.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. /*
  23. * POSIX_FADV_WILLNEED could set PG_Referenced, and POSIX_FADV_NOREUSE could
  24. * deactivate the pages and clear PG_Referenced.
  25. */
  26. int ksys_fadvise64_64(int fd, loff_t offset, loff_t len, int advice)
  27. {
  28. struct fd f = fdget(fd);
  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. int ret = 0;
  37. if (!f.file)
  38. return -EBADF;
  39. inode = file_inode(f.file);
  40. if (S_ISFIFO(inode->i_mode)) {
  41. ret = -ESPIPE;
  42. goto out;
  43. }
  44. mapping = f.file->f_mapping;
  45. if (!mapping || len < 0) {
  46. ret = -EINVAL;
  47. goto out;
  48. }
  49. bdi = inode_to_bdi(mapping->host);
  50. if (IS_DAX(inode) || (bdi == &noop_backing_dev_info)) {
  51. switch (advice) {
  52. case POSIX_FADV_NORMAL:
  53. case POSIX_FADV_RANDOM:
  54. case POSIX_FADV_SEQUENTIAL:
  55. case POSIX_FADV_WILLNEED:
  56. case POSIX_FADV_NOREUSE:
  57. case POSIX_FADV_DONTNEED:
  58. /* no bad return value, but ignore advice */
  59. break;
  60. default:
  61. ret = -EINVAL;
  62. }
  63. goto out;
  64. }
  65. /* Careful about overflows. Len == 0 means "as much as possible" */
  66. endbyte = offset + len;
  67. if (!len || endbyte < len)
  68. endbyte = -1;
  69. else
  70. endbyte--; /* inclusive */
  71. switch (advice) {
  72. case POSIX_FADV_NORMAL:
  73. f.file->f_ra.ra_pages = bdi->ra_pages;
  74. spin_lock(&f.file->f_lock);
  75. f.file->f_mode &= ~FMODE_RANDOM;
  76. spin_unlock(&f.file->f_lock);
  77. break;
  78. case POSIX_FADV_RANDOM:
  79. spin_lock(&f.file->f_lock);
  80. f.file->f_mode |= FMODE_RANDOM;
  81. spin_unlock(&f.file->f_lock);
  82. break;
  83. case POSIX_FADV_SEQUENTIAL:
  84. f.file->f_ra.ra_pages = bdi->ra_pages * 2;
  85. spin_lock(&f.file->f_lock);
  86. f.file->f_mode &= ~FMODE_RANDOM;
  87. spin_unlock(&f.file->f_lock);
  88. break;
  89. case POSIX_FADV_WILLNEED:
  90. /* First and last PARTIAL page! */
  91. start_index = offset >> PAGE_SHIFT;
  92. end_index = endbyte >> PAGE_SHIFT;
  93. /* Careful about overflow on the "+1" */
  94. nrpages = end_index - start_index + 1;
  95. if (!nrpages)
  96. nrpages = ~0UL;
  97. /*
  98. * Ignore return value because fadvise() shall return
  99. * success even if filesystem can't retrieve a hint,
  100. */
  101. force_page_cache_readahead(mapping, f.file, start_index,
  102. nrpages);
  103. break;
  104. case POSIX_FADV_NOREUSE:
  105. break;
  106. case POSIX_FADV_DONTNEED:
  107. if (!inode_write_congested(mapping->host))
  108. __filemap_fdatawrite_range(mapping, offset, endbyte,
  109. WB_SYNC_NONE);
  110. /*
  111. * First and last FULL page! Partial pages are deliberately
  112. * preserved on the expectation that it is better to preserve
  113. * needed memory than to discard unneeded memory.
  114. */
  115. start_index = (offset+(PAGE_SIZE-1)) >> PAGE_SHIFT;
  116. end_index = (endbyte >> PAGE_SHIFT);
  117. /*
  118. * The page at end_index will be inclusively discarded according
  119. * by invalidate_mapping_pages(), so subtracting 1 from
  120. * end_index means we will skip the last page. But if endbyte
  121. * is page aligned or is at the end of file, we should not skip
  122. * that page - discarding the last page is safe enough.
  123. */
  124. if ((endbyte & ~PAGE_MASK) != ~PAGE_MASK &&
  125. endbyte != inode->i_size - 1) {
  126. /* First page is tricky as 0 - 1 = -1, but pgoff_t
  127. * is unsigned, so the end_index >= start_index
  128. * check below would be true and we'll discard the whole
  129. * file cache which is not what was asked.
  130. */
  131. if (end_index == 0)
  132. break;
  133. end_index--;
  134. }
  135. if (end_index >= start_index) {
  136. unsigned long count;
  137. /*
  138. * It's common to FADV_DONTNEED right after
  139. * the read or write that instantiates the
  140. * pages, in which case there will be some
  141. * sitting on the local LRU cache. Try to
  142. * avoid the expensive remote drain and the
  143. * second cache tree walk below by flushing
  144. * them out right away.
  145. */
  146. lru_add_drain();
  147. count = invalidate_mapping_pages(mapping,
  148. start_index, end_index);
  149. /*
  150. * If fewer pages were invalidated than expected then
  151. * it is possible that some of the pages were on
  152. * a per-cpu pagevec for a remote CPU. Drain all
  153. * pagevecs and try again.
  154. */
  155. if (count < (end_index - start_index + 1)) {
  156. lru_add_drain_all();
  157. invalidate_mapping_pages(mapping, start_index,
  158. end_index);
  159. }
  160. }
  161. break;
  162. default:
  163. ret = -EINVAL;
  164. }
  165. out:
  166. fdput(f);
  167. return ret;
  168. }
  169. SYSCALL_DEFINE4(fadvise64_64, int, fd, loff_t, offset, loff_t, len, int, advice)
  170. {
  171. return ksys_fadvise64_64(fd, offset, len, advice);
  172. }
  173. #ifdef __ARCH_WANT_SYS_FADVISE64
  174. SYSCALL_DEFINE4(fadvise64, int, fd, loff_t, offset, size_t, len, int, advice)
  175. {
  176. return ksys_fadvise64_64(fd, offset, len, advice);
  177. }
  178. #endif