io.c 4.1 KB

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