io.c 4.0 KB

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