io.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2016 Trond Myklebust
  4. *
  5. * I/O and data path helper functionality.
  6. */
  7. #include <linux/types.h>
  8. #include <linux/kernel.h>
  9. #include <linux/bitops.h>
  10. #include <linux/rwsem.h>
  11. #include <linux/fs.h>
  12. #include <linux/nfs_fs.h>
  13. #include "internal.h"
  14. /* Call with exclusively locked inode->i_rwsem */
  15. static void nfs_block_o_direct(struct nfs_inode *nfsi, struct inode *inode)
  16. {
  17. if (test_bit(NFS_INO_ODIRECT, &nfsi->flags)) {
  18. clear_bit(NFS_INO_ODIRECT, &nfsi->flags);
  19. inode_dio_wait(inode);
  20. }
  21. }
  22. /**
  23. * nfs_start_io_read - declare the file is being used for buffered reads
  24. * @inode: file inode
  25. *
  26. * Declare that a buffered read operation is about to start, and ensure
  27. * that we block all direct I/O.
  28. * On exit, the function ensures that the NFS_INO_ODIRECT flag is unset,
  29. * and holds a shared lock on inode->i_rwsem to ensure that the flag
  30. * cannot be changed.
  31. * In practice, this means that buffered read operations are allowed to
  32. * execute in parallel, thanks to the shared lock, whereas direct I/O
  33. * operations need to wait to grab an exclusive lock in order to set
  34. * NFS_INO_ODIRECT.
  35. * Note that buffered writes and truncates both take a write lock on
  36. * inode->i_rwsem, meaning that those are serialised w.r.t. the reads.
  37. */
  38. void
  39. nfs_start_io_read(struct inode *inode)
  40. {
  41. struct nfs_inode *nfsi = NFS_I(inode);
  42. /* Be an optimist! */
  43. down_read(&inode->i_rwsem);
  44. if (test_bit(NFS_INO_ODIRECT, &nfsi->flags) == 0)
  45. return;
  46. up_read(&inode->i_rwsem);
  47. /* Slow path.... */
  48. down_write(&inode->i_rwsem);
  49. nfs_block_o_direct(nfsi, inode);
  50. downgrade_write(&inode->i_rwsem);
  51. }
  52. /**
  53. * nfs_end_io_read - declare that the buffered read operation is done
  54. * @inode: file inode
  55. *
  56. * Declare that a buffered read operation is done, and release the shared
  57. * lock on inode->i_rwsem.
  58. */
  59. void
  60. nfs_end_io_read(struct inode *inode)
  61. {
  62. up_read(&inode->i_rwsem);
  63. }
  64. /**
  65. * nfs_start_io_write - declare the file is being used for buffered writes
  66. * @inode: file inode
  67. *
  68. * Declare that a buffered read operation is about to start, and ensure
  69. * that we block all direct I/O.
  70. */
  71. void
  72. nfs_start_io_write(struct inode *inode)
  73. {
  74. down_write(&inode->i_rwsem);
  75. nfs_block_o_direct(NFS_I(inode), inode);
  76. }
  77. /**
  78. * nfs_end_io_write - declare that the buffered write operation is done
  79. * @inode: file inode
  80. *
  81. * Declare that a buffered write operation is done, and release the
  82. * lock on inode->i_rwsem.
  83. */
  84. void
  85. nfs_end_io_write(struct inode *inode)
  86. {
  87. up_write(&inode->i_rwsem);
  88. }
  89. /* Call with exclusively locked inode->i_rwsem */
  90. static void nfs_block_buffered(struct nfs_inode *nfsi, struct inode *inode)
  91. {
  92. if (!test_bit(NFS_INO_ODIRECT, &nfsi->flags)) {
  93. set_bit(NFS_INO_ODIRECT, &nfsi->flags);
  94. nfs_sync_mapping(inode->i_mapping);
  95. }
  96. }
  97. /**
  98. * nfs_end_io_direct - declare the file is being used for direct i/o
  99. * @inode: file inode
  100. *
  101. * Declare that a direct I/O operation is about to start, and ensure
  102. * that we block all buffered I/O.
  103. * On exit, the function ensures that the NFS_INO_ODIRECT flag is set,
  104. * and holds a shared lock on inode->i_rwsem to ensure that the flag
  105. * cannot be changed.
  106. * In practice, this means that direct I/O operations are allowed to
  107. * execute in parallel, thanks to the shared lock, whereas buffered I/O
  108. * operations need to wait to grab an exclusive lock in order to clear
  109. * NFS_INO_ODIRECT.
  110. * Note that buffered writes and truncates both take a write lock on
  111. * inode->i_rwsem, meaning that those are serialised w.r.t. O_DIRECT.
  112. */
  113. void
  114. nfs_start_io_direct(struct inode *inode)
  115. {
  116. struct nfs_inode *nfsi = NFS_I(inode);
  117. /* Be an optimist! */
  118. down_read(&inode->i_rwsem);
  119. if (test_bit(NFS_INO_ODIRECT, &nfsi->flags) != 0)
  120. return;
  121. up_read(&inode->i_rwsem);
  122. /* Slow path.... */
  123. down_write(&inode->i_rwsem);
  124. nfs_block_buffered(nfsi, inode);
  125. downgrade_write(&inode->i_rwsem);
  126. }
  127. /**
  128. * nfs_end_io_direct - declare that the direct i/o operation is done
  129. * @inode: file inode
  130. *
  131. * Declare that a direct I/O operation is done, and release the shared
  132. * lock on inode->i_rwsem.
  133. */
  134. void
  135. nfs_end_io_direct(struct inode *inode)
  136. {
  137. up_read(&inode->i_rwsem);
  138. }