fadvise.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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_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. case POSIX_FADV_NOREUSE:
  67. if (!mapping->a_ops->readpage) {
  68. ret = -EINVAL;
  69. break;
  70. }
  71. /* First and last PARTIAL page! */
  72. start_index = offset >> PAGE_CACHE_SHIFT;
  73. end_index = endbyte >> PAGE_CACHE_SHIFT;
  74. /* Careful about overflow on the "+1" */
  75. nrpages = end_index - start_index + 1;
  76. if (!nrpages)
  77. nrpages = ~0UL;
  78. ret = force_page_cache_readahead(mapping, file,
  79. start_index,
  80. max_sane_readahead(nrpages));
  81. if (ret > 0)
  82. ret = 0;
  83. break;
  84. case POSIX_FADV_DONTNEED:
  85. if (!bdi_write_congested(mapping->backing_dev_info))
  86. filemap_flush(mapping);
  87. /* First and last FULL page! */
  88. start_index = (offset+(PAGE_CACHE_SIZE-1)) >> PAGE_CACHE_SHIFT;
  89. end_index = (endbyte >> PAGE_CACHE_SHIFT);
  90. if (end_index >= start_index)
  91. invalidate_mapping_pages(mapping, start_index,
  92. end_index);
  93. break;
  94. default:
  95. ret = -EINVAL;
  96. }
  97. out:
  98. fput(file);
  99. return ret;
  100. }
  101. #ifdef __ARCH_WANT_SYS_FADVISE64
  102. asmlinkage long sys_fadvise64(int fd, loff_t offset, size_t len, int advice)
  103. {
  104. return sys_fadvise64_64(fd, offset, len, advice);
  105. }
  106. #endif