fadvise.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 <asm/unistd.h>
  20. /*
  21. * POSIX_FADV_WILLNEED could set PG_Referenced, and POSIX_FADV_NOREUSE could
  22. * deactivate the pages and clear PG_Referenced.
  23. */
  24. SYSCALL_DEFINE(fadvise64_64)(int fd, loff_t offset, loff_t len, int advice)
  25. {
  26. struct file *file = fget(fd);
  27. struct address_space *mapping;
  28. struct backing_dev_info *bdi;
  29. loff_t endbyte; /* inclusive */
  30. pgoff_t start_index;
  31. pgoff_t end_index;
  32. unsigned long nrpages;
  33. int ret = 0;
  34. if (!file)
  35. return -EBADF;
  36. if (S_ISFIFO(file->f_path.dentry->d_inode->i_mode)) {
  37. ret = -ESPIPE;
  38. goto out;
  39. }
  40. mapping = file->f_mapping;
  41. if (!mapping || len < 0) {
  42. ret = -EINVAL;
  43. goto out;
  44. }
  45. if (mapping->a_ops->get_xip_mem) {
  46. switch (advice) {
  47. case POSIX_FADV_NORMAL:
  48. case POSIX_FADV_RANDOM:
  49. case POSIX_FADV_SEQUENTIAL:
  50. case POSIX_FADV_WILLNEED:
  51. case POSIX_FADV_NOREUSE:
  52. case POSIX_FADV_DONTNEED:
  53. /* no bad return value, but ignore advice */
  54. break;
  55. default:
  56. ret = -EINVAL;
  57. }
  58. goto out;
  59. }
  60. /* Careful about overflows. Len == 0 means "as much as possible" */
  61. endbyte = offset + len;
  62. if (!len || endbyte < len)
  63. endbyte = -1;
  64. else
  65. endbyte--; /* inclusive */
  66. bdi = mapping->backing_dev_info;
  67. switch (advice) {
  68. case POSIX_FADV_NORMAL:
  69. file->f_ra.ra_pages = bdi->ra_pages;
  70. break;
  71. case POSIX_FADV_RANDOM:
  72. file->f_ra.ra_pages = 0;
  73. break;
  74. case POSIX_FADV_SEQUENTIAL:
  75. file->f_ra.ra_pages = bdi->ra_pages * 2;
  76. break;
  77. case POSIX_FADV_WILLNEED:
  78. if (!mapping->a_ops->readpage) {
  79. ret = -EINVAL;
  80. break;
  81. }
  82. /* First and last PARTIAL page! */
  83. start_index = offset >> PAGE_CACHE_SHIFT;
  84. end_index = endbyte >> PAGE_CACHE_SHIFT;
  85. /* Careful about overflow on the "+1" */
  86. nrpages = end_index - start_index + 1;
  87. if (!nrpages)
  88. nrpages = ~0UL;
  89. ret = force_page_cache_readahead(mapping, file,
  90. start_index,
  91. max_sane_readahead(nrpages));
  92. if (ret > 0)
  93. ret = 0;
  94. break;
  95. case POSIX_FADV_NOREUSE:
  96. break;
  97. case POSIX_FADV_DONTNEED:
  98. if (!bdi_write_congested(mapping->backing_dev_info))
  99. filemap_flush(mapping);
  100. /* First and last FULL page! */
  101. start_index = (offset+(PAGE_CACHE_SIZE-1)) >> PAGE_CACHE_SHIFT;
  102. end_index = (endbyte >> PAGE_CACHE_SHIFT);
  103. if (end_index >= start_index)
  104. invalidate_mapping_pages(mapping, start_index,
  105. end_index);
  106. break;
  107. default:
  108. ret = -EINVAL;
  109. }
  110. out:
  111. fput(file);
  112. return ret;
  113. }
  114. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  115. asmlinkage long SyS_fadvise64_64(long fd, loff_t offset, loff_t len, long advice)
  116. {
  117. return SYSC_fadvise64_64((int) fd, offset, len, (int) advice);
  118. }
  119. SYSCALL_ALIAS(sys_fadvise64_64, SyS_fadvise64_64);
  120. #endif
  121. #ifdef __ARCH_WANT_SYS_FADVISE64
  122. SYSCALL_DEFINE(fadvise64)(int fd, loff_t offset, size_t len, int advice)
  123. {
  124. return sys_fadvise64_64(fd, offset, len, advice);
  125. }
  126. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  127. asmlinkage long SyS_fadvise64(long fd, loff_t offset, long len, long advice)
  128. {
  129. return SYSC_fadvise64((int) fd, offset, (size_t)len, (int)advice);
  130. }
  131. SYSCALL_ALIAS(sys_fadvise64, SyS_fadvise64);
  132. #endif
  133. #endif