fadvise.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. * LINUX_FADV_ASYNC_WRITE: start async writeout of any dirty pages between file
  25. * offsets `offset' and `offset+len' inclusive. Any pages which are currently
  26. * under writeout are skipped, whether or not they are dirty.
  27. *
  28. * LINUX_FADV_WRITE_WAIT: wait upon writeout of any dirty pages between file
  29. * offsets `offset' and `offset+len'.
  30. *
  31. * By combining these two operations the application may do several things:
  32. *
  33. * LINUX_FADV_ASYNC_WRITE: push some or all of the dirty pages at the disk.
  34. *
  35. */
  36. asmlinkage long sys_fadvise64_64(int fd, loff_t offset, loff_t len, int advice)
  37. {
  38. struct file *file = fget(fd);
  39. struct address_space *mapping;
  40. struct backing_dev_info *bdi;
  41. loff_t endbyte; /* inclusive */
  42. pgoff_t start_index;
  43. pgoff_t end_index;
  44. unsigned long nrpages;
  45. int ret = 0;
  46. if (!file)
  47. return -EBADF;
  48. if (S_ISFIFO(file->f_dentry->d_inode->i_mode)) {
  49. ret = -ESPIPE;
  50. goto out;
  51. }
  52. mapping = file->f_mapping;
  53. if (!mapping || len < 0) {
  54. ret = -EINVAL;
  55. goto out;
  56. }
  57. if (mapping->a_ops->get_xip_page)
  58. /* no bad return value, but ignore advice */
  59. goto out;
  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. case POSIX_FADV_NOREUSE:
  79. if (!mapping->a_ops->readpage) {
  80. ret = -EINVAL;
  81. break;
  82. }
  83. /* First and last PARTIAL page! */
  84. start_index = offset >> PAGE_CACHE_SHIFT;
  85. end_index = endbyte >> PAGE_CACHE_SHIFT;
  86. /* Careful about overflow on the "+1" */
  87. nrpages = end_index - start_index + 1;
  88. if (!nrpages)
  89. nrpages = ~0UL;
  90. ret = force_page_cache_readahead(mapping, file,
  91. start_index,
  92. max_sane_readahead(nrpages));
  93. if (ret > 0)
  94. ret = 0;
  95. break;
  96. case POSIX_FADV_DONTNEED:
  97. if (!bdi_write_congested(mapping->backing_dev_info))
  98. filemap_flush(mapping);
  99. /* First and last FULL page! */
  100. start_index = (offset+(PAGE_CACHE_SIZE-1)) >> PAGE_CACHE_SHIFT;
  101. end_index = (endbyte >> PAGE_CACHE_SHIFT);
  102. if (end_index >= start_index)
  103. invalidate_mapping_pages(mapping, start_index,
  104. end_index);
  105. break;
  106. default:
  107. ret = -EINVAL;
  108. }
  109. out:
  110. fput(file);
  111. return ret;
  112. }
  113. #ifdef __ARCH_WANT_SYS_FADVISE64
  114. asmlinkage long sys_fadvise64(int fd, loff_t offset, size_t len, int advice)
  115. {
  116. return sys_fadvise64_64(fd, offset, len, advice);
  117. }
  118. #endif