1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2016 Trond Myklebust
4 * Copyright (c) 2019 Jeff Layton
6 * I/O and data path helper functionality.
8 * Heavily borrowed from equivalent code in fs/nfs/io.c
11 #include <linux/ceph/ceph_debug.h>
13 #include <linux/types.h>
14 #include <linux/kernel.h>
15 #include <linux/rwsem.h>
21 /* Call with exclusively locked inode->i_rwsem */
22 static void ceph_block_o_direct(struct ceph_inode_info
*ci
, struct inode
*inode
)
24 lockdep_assert_held_write(&inode
->i_rwsem
);
26 if (READ_ONCE(ci
->i_ceph_flags
) & CEPH_I_ODIRECT
) {
27 spin_lock(&ci
->i_ceph_lock
);
28 ci
->i_ceph_flags
&= ~CEPH_I_ODIRECT
;
29 spin_unlock(&ci
->i_ceph_lock
);
30 inode_dio_wait(inode
);
35 * ceph_start_io_read - declare the file is being used for buffered reads
38 * Declare that a buffered read operation is about to start, and ensure
39 * that we block all direct I/O.
40 * On exit, the function ensures that the CEPH_I_ODIRECT flag is unset,
41 * and holds a shared lock on inode->i_rwsem to ensure that the flag
43 * In practice, this means that buffered read operations are allowed to
44 * execute in parallel, thanks to the shared lock, whereas direct I/O
45 * operations need to wait to grab an exclusive lock in order to set
47 * Note that buffered writes and truncates both take a write lock on
48 * inode->i_rwsem, meaning that those are serialised w.r.t. the reads.
51 ceph_start_io_read(struct inode
*inode
)
53 struct ceph_inode_info
*ci
= ceph_inode(inode
);
56 down_read(&inode
->i_rwsem
);
57 if (!(READ_ONCE(ci
->i_ceph_flags
) & CEPH_I_ODIRECT
))
59 up_read(&inode
->i_rwsem
);
61 down_write(&inode
->i_rwsem
);
62 ceph_block_o_direct(ci
, inode
);
63 downgrade_write(&inode
->i_rwsem
);
67 * ceph_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 ceph_end_io_read(struct inode
*inode
)
76 up_read(&inode
->i_rwsem
);
80 * ceph_start_io_write - declare the file is being used for buffered writes
83 * Declare that a buffered write operation is about to start, and ensure
84 * that we block all direct I/O.
87 ceph_start_io_write(struct inode
*inode
)
89 down_write(&inode
->i_rwsem
);
90 ceph_block_o_direct(ceph_inode(inode
), inode
);
94 * ceph_end_io_write - declare that the buffered write operation is done
97 * Declare that a buffered write operation is done, and release the
98 * lock on inode->i_rwsem.
101 ceph_end_io_write(struct inode
*inode
)
103 up_write(&inode
->i_rwsem
);
106 /* Call with exclusively locked inode->i_rwsem */
107 static void ceph_block_buffered(struct ceph_inode_info
*ci
, struct inode
*inode
)
109 lockdep_assert_held_write(&inode
->i_rwsem
);
111 if (!(READ_ONCE(ci
->i_ceph_flags
) & CEPH_I_ODIRECT
)) {
112 spin_lock(&ci
->i_ceph_lock
);
113 ci
->i_ceph_flags
|= CEPH_I_ODIRECT
;
114 spin_unlock(&ci
->i_ceph_lock
);
115 /* FIXME: unmap_mapping_range? */
116 filemap_write_and_wait(inode
->i_mapping
);
121 * ceph_end_io_direct - declare the file is being used for direct i/o
124 * Declare that a direct I/O operation is about to start, and ensure
125 * that we block all buffered I/O.
126 * On exit, the function ensures that the CEPH_I_ODIRECT flag is set,
127 * and holds a shared lock on inode->i_rwsem to ensure that the flag
129 * In practice, this means that direct I/O operations are allowed to
130 * execute in parallel, thanks to the shared lock, whereas buffered I/O
131 * operations need to wait to grab an exclusive lock in order to clear
133 * Note that buffered writes and truncates both take a write lock on
134 * inode->i_rwsem, meaning that those are serialised w.r.t. O_DIRECT.
137 ceph_start_io_direct(struct inode
*inode
)
139 struct ceph_inode_info
*ci
= ceph_inode(inode
);
141 /* Be an optimist! */
142 down_read(&inode
->i_rwsem
);
143 if (READ_ONCE(ci
->i_ceph_flags
) & CEPH_I_ODIRECT
)
145 up_read(&inode
->i_rwsem
);
147 down_write(&inode
->i_rwsem
);
148 ceph_block_buffered(ci
, inode
);
149 downgrade_write(&inode
->i_rwsem
);
153 * ceph_end_io_direct - declare that the direct i/o operation is done
156 * Declare that a direct I/O operation is done, and release the shared
157 * lock on inode->i_rwsem.
160 ceph_end_io_direct(struct inode
*inode
)
162 up_read(&inode
->i_rwsem
);