fadvise.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * mm/fadvise.c
  3. *
  4. * Copyright (C) 2002, Linus Torvalds
  5. *
  6. * 11Jan2003 akpm@digeo.com
  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. asmlinkage long sys_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_page)
  46. /* no bad return value, but ignore advice */
  47. goto out;
  48. /* Careful about overflows. Len == 0 means "as much as possible" */
  49. endbyte = offset + len;
  50. if (!len || endbyte < len)
  51. endbyte = -1;
  52. else
  53. endbyte--; /* inclusive */
  54. bdi = mapping->backing_dev_info;
  55. switch (advice) {
  56. case POSIX_FADV_NORMAL:
  57. file->f_ra.ra_pages = bdi->ra_pages;
  58. break;
  59. case POSIX_FADV_RANDOM:
  60. file->f_ra.ra_pages = 0;
  61. break;
  62. case POSIX_FADV_SEQUENTIAL:
  63. file->f_ra.ra_pages = bdi->ra_pages * 2;
  64. break;
  65. case POSIX_FADV_WILLNEED:
  66. if (!mapping->a_ops->readpage) {
  67. ret = -EINVAL;
  68. break;
  69. }
  70. /* First and last PARTIAL page! */
  71. start_index = offset >> PAGE_CACHE_SHIFT;
  72. end_index = endbyte >> PAGE_CACHE_SHIFT;
  73. /* Careful about overflow on the "+1" */
  74. nrpages = end_index - start_index + 1;
  75. if (!nrpages)
  76. nrpages = ~0UL;
  77. ret = force_page_cache_readahead(mapping, file,
  78. start_index,
  79. max_sane_readahead(nrpages));
  80. if (ret > 0)
  81. ret = 0;
  82. break;
  83. case POSIX_FADV_NOREUSE:
  84. break;
  85. case POSIX_FADV_DONTNEED:
  86. if (!bdi_write_congested(mapping->backing_dev_info))
  87. filemap_flush(mapping);
  88. /* First and last FULL page! */
  89. start_index = (offset+(PAGE_CACHE_SIZE-1)) >> PAGE_CACHE_SHIFT;
  90. end_index = (endbyte >> PAGE_CACHE_SHIFT);
  91. if (end_index >= start_index)
  92. invalidate_mapping_pages(mapping, start_index,
  93. end_index);
  94. break;
  95. default:
  96. ret = -EINVAL;
  97. }
  98. out:
  99. fput(file);
  100. return ret;
  101. }
  102. #ifdef __ARCH_WANT_SYS_FADVISE64
  103. asmlinkage long sys_fadvise64(int fd, loff_t offset, size_t len, int advice)
  104. {
  105. return sys_fadvise64_64(fd, offset, len, advice);
  106. }
  107. #endif