fadvise.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. struct file *file = fget(fd);
  28. struct address_space *mapping;
  29. struct backing_dev_info *bdi;
  30. loff_t endbyte; /* inclusive */
  31. pgoff_t start_index;
  32. pgoff_t end_index;
  33. unsigned long nrpages;
  34. int ret = 0;
  35. if (!file)
  36. return -EBADF;
  37. if (S_ISFIFO(file->f_path.dentry->d_inode->i_mode)) {
  38. ret = -ESPIPE;
  39. goto out;
  40. }
  41. mapping = file->f_mapping;
  42. if (!mapping || len < 0) {
  43. ret = -EINVAL;
  44. goto out;
  45. }
  46. if (mapping->a_ops->get_xip_mem) {
  47. switch (advice) {
  48. case POSIX_FADV_NORMAL:
  49. case POSIX_FADV_RANDOM:
  50. case POSIX_FADV_SEQUENTIAL:
  51. case POSIX_FADV_WILLNEED:
  52. case POSIX_FADV_NOREUSE:
  53. case POSIX_FADV_DONTNEED:
  54. /* no bad return value, but ignore advice */
  55. break;
  56. default:
  57. ret = -EINVAL;
  58. }
  59. goto out;
  60. }
  61. /* Careful about overflows. Len == 0 means "as much as possible" */
  62. endbyte = offset + len;
  63. if (!len || endbyte < len)
  64. endbyte = -1;
  65. else
  66. endbyte--; /* inclusive */
  67. bdi = mapping->backing_dev_info;
  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;
  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. if (!mapping->a_ops->readpage) {
  88. ret = -EINVAL;
  89. break;
  90. }
  91. /* First and last PARTIAL page! */
  92. start_index = offset >> PAGE_CACHE_SHIFT;
  93. end_index = endbyte >> PAGE_CACHE_SHIFT;
  94. /* Careful about overflow on the "+1" */
  95. nrpages = end_index - start_index + 1;
  96. if (!nrpages)
  97. nrpages = ~0UL;
  98. ret = force_page_cache_readahead(mapping, file,
  99. start_index,
  100. nrpages);
  101. if (ret > 0)
  102. ret = 0;
  103. break;
  104. case POSIX_FADV_NOREUSE:
  105. break;
  106. case POSIX_FADV_DONTNEED:
  107. if (!bdi_write_congested(mapping->backing_dev_info))
  108. __filemap_fdatawrite_range(mapping, offset, endbyte,
  109. WB_SYNC_NONE);
  110. /* First and last FULL page! */
  111. start_index = (offset+(PAGE_CACHE_SIZE-1)) >> PAGE_CACHE_SHIFT;
  112. end_index = (endbyte >> PAGE_CACHE_SHIFT);
  113. if (end_index >= start_index) {
  114. unsigned long count = invalidate_mapping_pages(mapping,
  115. start_index, end_index);
  116. /*
  117. * If fewer pages were invalidated than expected then
  118. * it is possible that some of the pages were on
  119. * a per-cpu pagevec for a remote CPU. Drain all
  120. * pagevecs and try again.
  121. */
  122. if (count < (end_index - start_index + 1)) {
  123. lru_add_drain_all();
  124. invalidate_mapping_pages(mapping, start_index,
  125. end_index);
  126. }
  127. }
  128. break;
  129. default:
  130. ret = -EINVAL;
  131. }
  132. out:
  133. fput(file);
  134. return ret;
  135. }
  136. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  137. asmlinkage long SyS_fadvise64_64(long fd, loff_t offset, loff_t len, long advice)
  138. {
  139. return SYSC_fadvise64_64((int) fd, offset, len, (int) advice);
  140. }
  141. SYSCALL_ALIAS(sys_fadvise64_64, SyS_fadvise64_64);
  142. #endif
  143. #ifdef __ARCH_WANT_SYS_FADVISE64
  144. SYSCALL_DEFINE(fadvise64)(int fd, loff_t offset, size_t len, int advice)
  145. {
  146. return sys_fadvise64_64(fd, offset, len, advice);
  147. }
  148. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  149. asmlinkage long SyS_fadvise64(long fd, loff_t offset, long len, long advice)
  150. {
  151. return SYSC_fadvise64((int) fd, offset, (size_t)len, (int)advice);
  152. }
  153. SYSCALL_ALIAS(sys_fadvise64, SyS_fadvise64);
  154. #endif
  155. #endif