fadvise.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. * LINUX_FADV_WRITE_WAIT, LINUX_FADV_ASYNC_WRITE: push all of the currently
  36. * dirty pages at the disk.
  37. *
  38. * LINUX_FADV_WRITE_WAIT, LINUX_FADV_ASYNC_WRITE, LINUX_FADV_WRITE_WAIT: push
  39. * all of the currently dirty pages at the disk, wait until they have been
  40. * written.
  41. *
  42. * It should be noted that none of these operations write out the file's
  43. * metadata. So unless the application is strictly performing overwrites of
  44. * already-instantiated disk blocks, there are no guarantees here that the data
  45. * will be available after a crash.
  46. */
  47. asmlinkage long sys_fadvise64_64(int fd, loff_t offset, loff_t len, int advice)
  48. {
  49. struct file *file = fget(fd);
  50. struct address_space *mapping;
  51. struct backing_dev_info *bdi;
  52. loff_t endbyte; /* inclusive */
  53. pgoff_t start_index;
  54. pgoff_t end_index;
  55. unsigned long nrpages;
  56. int ret = 0;
  57. if (!file)
  58. return -EBADF;
  59. if (S_ISFIFO(file->f_dentry->d_inode->i_mode)) {
  60. ret = -ESPIPE;
  61. goto out;
  62. }
  63. mapping = file->f_mapping;
  64. if (!mapping || len < 0) {
  65. ret = -EINVAL;
  66. goto out;
  67. }
  68. if (mapping->a_ops->get_xip_page)
  69. /* no bad return value, but ignore advice */
  70. goto out;
  71. /* Careful about overflows. Len == 0 means "as much as possible" */
  72. endbyte = offset + len;
  73. if (!len || endbyte < len)
  74. endbyte = -1;
  75. else
  76. endbyte--; /* inclusive */
  77. bdi = mapping->backing_dev_info;
  78. switch (advice) {
  79. case POSIX_FADV_NORMAL:
  80. file->f_ra.ra_pages = bdi->ra_pages;
  81. break;
  82. case POSIX_FADV_RANDOM:
  83. file->f_ra.ra_pages = 0;
  84. break;
  85. case POSIX_FADV_SEQUENTIAL:
  86. file->f_ra.ra_pages = bdi->ra_pages * 2;
  87. break;
  88. case POSIX_FADV_WILLNEED:
  89. case POSIX_FADV_NOREUSE:
  90. if (!mapping->a_ops->readpage) {
  91. ret = -EINVAL;
  92. break;
  93. }
  94. /* First and last PARTIAL page! */
  95. start_index = offset >> PAGE_CACHE_SHIFT;
  96. end_index = endbyte >> PAGE_CACHE_SHIFT;
  97. /* Careful about overflow on the "+1" */
  98. nrpages = end_index - start_index + 1;
  99. if (!nrpages)
  100. nrpages = ~0UL;
  101. ret = force_page_cache_readahead(mapping, file,
  102. start_index,
  103. max_sane_readahead(nrpages));
  104. if (ret > 0)
  105. ret = 0;
  106. break;
  107. case POSIX_FADV_DONTNEED:
  108. if (!bdi_write_congested(mapping->backing_dev_info))
  109. filemap_flush(mapping);
  110. /* First and last FULL page! */
  111. start_index = (offset+(PAGE_CACHE_SIZE-1)) >> PAGE_CACHE_SHIFT;
  112. end_index = (endbyte >> PAGE_CACHE_SHIFT);
  113. if (end_index >= start_index)
  114. invalidate_mapping_pages(mapping, start_index,
  115. end_index);
  116. break;
  117. case LINUX_FADV_ASYNC_WRITE:
  118. ret = __filemap_fdatawrite_range(mapping, offset, endbyte,
  119. WB_SYNC_NONE);
  120. break;
  121. case LINUX_FADV_WRITE_WAIT:
  122. ret = wait_on_page_writeback_range(mapping,
  123. offset >> PAGE_CACHE_SHIFT,
  124. endbyte >> PAGE_CACHE_SHIFT);
  125. break;
  126. default:
  127. ret = -EINVAL;
  128. }
  129. out:
  130. fput(file);
  131. return ret;
  132. }
  133. #ifdef __ARCH_WANT_SYS_FADVISE64
  134. asmlinkage long sys_fadvise64(int fd, loff_t offset, size_t len, int advice)
  135. {
  136. return sys_fadvise64_64(fd, offset, len, advice);
  137. }
  138. #endif