1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2016 Trond Myklebust
5 * I/O and data path helper functionality.
8 #include <linux/types.h>
9 #include <linux/kernel.h>
10 #include <linux/bitops.h>
11 #include <linux/rwsem.h>
13 #include <linux/nfs_fs.h>
17 /* Call with exclusively locked inode->i_rwsem */
18 static void nfs_block_o_direct(struct nfs_inode
*nfsi
, struct inode
*inode
)
20 if (test_bit(NFS_INO_ODIRECT
, &nfsi
->flags
)) {
21 clear_bit(NFS_INO_ODIRECT
, &nfsi
->flags
);
22 inode_dio_wait(inode
);
27 * nfs_start_io_read - declare the file is being used for buffered reads
30 * Declare that a buffered read operation is about to start, and ensure
31 * that we block all direct I/O.
32 * On exit, the function ensures that the NFS_INO_ODIRECT flag is unset,
33 * and holds a shared lock on inode->i_rwsem to ensure that the flag
35 * In practice, this means that buffered read operations are allowed to
36 * execute in parallel, thanks to the shared lock, whereas direct I/O
37 * operations need to wait to grab an exclusive lock in order to set
39 * Note that buffered writes and truncates both take a write lock on
40 * inode->i_rwsem, meaning that those are serialised w.r.t. the reads.
43 nfs_start_io_read(struct inode
*inode
)
45 struct nfs_inode
*nfsi
= NFS_I(inode
);
49 err
= down_read_killable(&inode
->i_rwsem
);
52 if (test_bit(NFS_INO_ODIRECT
, &nfsi
->flags
) == 0)
54 up_read(&inode
->i_rwsem
);
57 err
= down_write_killable(&inode
->i_rwsem
);
60 nfs_block_o_direct(nfsi
, inode
);
61 downgrade_write(&inode
->i_rwsem
);
67 * nfs_end_io_read - declare that the buffered read operation is done
70 * Declare that a buffered read operation is done, and release the shared
71 * lock on inode->i_rwsem.
74 nfs_end_io_read(struct inode
*inode
)
76 up_read(&inode
->i_rwsem
);
80 * nfs_start_io_write - declare the file is being used for buffered writes
83 * Declare that a buffered read operation is about to start, and ensure
84 * that we block all direct I/O.
87 nfs_start_io_write(struct inode
*inode
)
91 err
= down_write_killable(&inode
->i_rwsem
);
93 nfs_block_o_direct(NFS_I(inode
), inode
);
98 * nfs_end_io_write - declare that the buffered write operation is done
101 * Declare that a buffered write operation is done, and release the
102 * lock on inode->i_rwsem.
105 nfs_end_io_write(struct inode
*inode
)
107 up_write(&inode
->i_rwsem
);
110 /* Call with exclusively locked inode->i_rwsem */
111 static void nfs_block_buffered(struct nfs_inode
*nfsi
, struct inode
*inode
)
113 if (!test_bit(NFS_INO_ODIRECT
, &nfsi
->flags
)) {
114 set_bit(NFS_INO_ODIRECT
, &nfsi
->flags
);
115 nfs_sync_mapping(inode
->i_mapping
);
120 * nfs_start_io_direct - declare the file is being used for direct i/o
123 * Declare that a direct I/O operation is about to start, and ensure
124 * that we block all buffered I/O.
125 * On exit, the function ensures that the NFS_INO_ODIRECT flag is set,
126 * and holds a shared lock on inode->i_rwsem to ensure that the flag
128 * In practice, this means that direct I/O operations are allowed to
129 * execute in parallel, thanks to the shared lock, whereas buffered I/O
130 * operations need to wait to grab an exclusive lock in order to clear
132 * Note that buffered writes and truncates both take a write lock on
133 * inode->i_rwsem, meaning that those are serialised w.r.t. O_DIRECT.
136 nfs_start_io_direct(struct inode
*inode
)
138 struct nfs_inode
*nfsi
= NFS_I(inode
);
141 /* Be an optimist! */
142 err
= down_read_killable(&inode
->i_rwsem
);
145 if (test_bit(NFS_INO_ODIRECT
, &nfsi
->flags
) != 0)
147 up_read(&inode
->i_rwsem
);
150 err
= down_write_killable(&inode
->i_rwsem
);
153 nfs_block_buffered(nfsi
, inode
);
154 downgrade_write(&inode
->i_rwsem
);
160 * nfs_end_io_direct - declare that the direct i/o operation is done
163 * Declare that a direct I/O operation is done, and release the shared
164 * lock on inode->i_rwsem.
167 nfs_end_io_direct(struct inode
*inode
)
169 up_read(&inode
->i_rwsem
);