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