io.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. /**
  79. * nfs_end_io_write - declare that the buffered write operation is done
  80. * @inode: file inode
  81. *
  82. * Declare that a buffered write operation is done, and release the
  83. * lock on inode->i_rwsem.
  84. */
  85. void
  86. nfs_end_io_write(struct inode *inode)
  87. {
  88. up_write(&inode->i_rwsem);
  89. }
  90. /* Call with exclusively locked inode->i_rwsem */
  91. static void nfs_block_buffered(struct nfs_inode *nfsi, struct inode *inode)
  92. {
  93. if (!test_bit(NFS_INO_ODIRECT, &nfsi->flags)) {
  94. set_bit(NFS_INO_ODIRECT, &nfsi->flags);
  95. nfs_sync_mapping(inode->i_mapping);
  96. }
  97. }
  98. /**
  99. * nfs_start_io_direct - declare the file is being used for direct i/o
  100. * @inode: file inode
  101. *
  102. * Declare that a direct I/O operation is about to start, and ensure
  103. * that we block all buffered I/O.
  104. * On exit, the function ensures that the NFS_INO_ODIRECT flag is set,
  105. * and holds a shared lock on inode->i_rwsem to ensure that the flag
  106. * cannot be changed.
  107. * In practice, this means that direct I/O operations are allowed to
  108. * execute in parallel, thanks to the shared lock, whereas buffered I/O
  109. * operations need to wait to grab an exclusive lock in order to clear
  110. * NFS_INO_ODIRECT.
  111. * Note that buffered writes and truncates both take a write lock on
  112. * inode->i_rwsem, meaning that those are serialised w.r.t. O_DIRECT.
  113. */
  114. int
  115. nfs_start_io_direct(struct inode *inode)
  116. {
  117. struct nfs_inode *nfsi = NFS_I(inode);
  118. int err;
  119. /* Be an optimist! */
  120. err = down_read_killable(&inode->i_rwsem);
  121. if (err)
  122. return err;
  123. if (test_bit(NFS_INO_ODIRECT, &nfsi->flags) != 0)
  124. return 0;
  125. up_read(&inode->i_rwsem);
  126. /* Slow path.... */
  127. err = down_write_killable(&inode->i_rwsem);
  128. if (err)
  129. return err;
  130. nfs_block_buffered(nfsi, inode);
  131. downgrade_write(&inode->i_rwsem);
  132. return 0;
  133. }
  134. /**
  135. * nfs_end_io_direct - declare that the direct i/o operation is done
  136. * @inode: file inode
  137. *
  138. * Declare that a direct I/O operation is done, and release the shared
  139. * lock on inode->i_rwsem.
  140. */
  141. void
  142. nfs_end_io_direct(struct inode *inode)
  143. {
  144. up_read(&inode->i_rwsem);
  145. }