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