fadvise.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. spin_lock(&file->f_lock);
  71. file->f_mode &= ~FMODE_RANDOM;
  72. spin_unlock(&file->f_lock);
  73. break;
  74. case POSIX_FADV_RANDOM:
  75. spin_lock(&file->f_lock);
  76. file->f_mode |= FMODE_RANDOM;
  77. spin_unlock(&file->f_lock);
  78. break;
  79. case POSIX_FADV_SEQUENTIAL:
  80. file->f_ra.ra_pages = bdi->ra_pages * 2;
  81. spin_lock(&file->f_lock);
  82. file->f_mode &= ~FMODE_RANDOM;
  83. spin_unlock(&file->f_lock);
  84. break;
  85. case POSIX_FADV_WILLNEED:
  86. if (!mapping->a_ops->readpage) {
  87. ret = -EINVAL;
  88. break;
  89. }
  90. /* First and last PARTIAL page! */
  91. start_index = offset >> PAGE_CACHE_SHIFT;
  92. end_index = endbyte >> PAGE_CACHE_SHIFT;
  93. /* Careful about overflow on the "+1" */
  94. nrpages = end_index - start_index + 1;
  95. if (!nrpages)
  96. nrpages = ~0UL;
  97. ret = force_page_cache_readahead(mapping, file,
  98. start_index,
  99. nrpages);
  100. if (ret > 0)
  101. ret = 0;
  102. break;
  103. case POSIX_FADV_NOREUSE:
  104. break;
  105. case POSIX_FADV_DONTNEED:
  106. if (!bdi_write_congested(mapping->backing_dev_info))
  107. filemap_flush(mapping);
  108. /* First and last FULL page! */
  109. start_index = (offset+(PAGE_CACHE_SIZE-1)) >> PAGE_CACHE_SHIFT;
  110. end_index = (endbyte >> PAGE_CACHE_SHIFT);
  111. if (end_index >= start_index)
  112. invalidate_mapping_pages(mapping, start_index,
  113. end_index);
  114. break;
  115. default:
  116. ret = -EINVAL;
  117. }
  118. out:
  119. fput(file);
  120. return ret;
  121. }
  122. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  123. asmlinkage long SyS_fadvise64_64(long fd, loff_t offset, loff_t len, long advice)
  124. {
  125. return SYSC_fadvise64_64((int) fd, offset, len, (int) advice);
  126. }
  127. SYSCALL_ALIAS(sys_fadvise64_64, SyS_fadvise64_64);
  128. #endif
  129. #ifdef __ARCH_WANT_SYS_FADVISE64
  130. SYSCALL_DEFINE(fadvise64)(int fd, loff_t offset, size_t len, int advice)
  131. {
  132. return sys_fadvise64_64(fd, offset, len, advice);
  133. }
  134. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  135. asmlinkage long SyS_fadvise64(long fd, loff_t offset, long len, long advice)
  136. {
  137. return SYSC_fadvise64((int) fd, offset, (size_t)len, (int)advice);
  138. }
  139. SYSCALL_ALIAS(sys_fadvise64, SyS_fadvise64);
  140. #endif
  141. #endif