1 // SPDX-License-Identifier: GPL-2.0 1 // SPDX-License-Identifier: GPL-2.0 2 /* 2 /* 3 * Copyright (c) 2016 Trond Myklebust 3 * Copyright (c) 2016 Trond Myklebust 4 * Copyright (c) 2019 Jeff Layton 4 * Copyright (c) 2019 Jeff Layton 5 * 5 * 6 * I/O and data path helper functionality. 6 * I/O and data path helper functionality. 7 * 7 * 8 * Heavily borrowed from equivalent code in fs 8 * Heavily borrowed from equivalent code in fs/nfs/io.c 9 */ 9 */ 10 10 11 #include <linux/ceph/ceph_debug.h> 11 #include <linux/ceph/ceph_debug.h> 12 12 13 #include <linux/types.h> 13 #include <linux/types.h> 14 #include <linux/kernel.h> 14 #include <linux/kernel.h> 15 #include <linux/rwsem.h> 15 #include <linux/rwsem.h> 16 #include <linux/fs.h> 16 #include <linux/fs.h> 17 17 18 #include "super.h" 18 #include "super.h" 19 #include "io.h" 19 #include "io.h" 20 20 21 /* Call with exclusively locked inode->i_rwsem 21 /* Call with exclusively locked inode->i_rwsem */ 22 static void ceph_block_o_direct(struct ceph_in 22 static void ceph_block_o_direct(struct ceph_inode_info *ci, struct inode *inode) 23 { 23 { 24 lockdep_assert_held_write(&inode->i_rw 24 lockdep_assert_held_write(&inode->i_rwsem); 25 25 26 if (READ_ONCE(ci->i_ceph_flags) & CEPH 26 if (READ_ONCE(ci->i_ceph_flags) & CEPH_I_ODIRECT) { 27 spin_lock(&ci->i_ceph_lock); 27 spin_lock(&ci->i_ceph_lock); 28 ci->i_ceph_flags &= ~CEPH_I_OD 28 ci->i_ceph_flags &= ~CEPH_I_ODIRECT; 29 spin_unlock(&ci->i_ceph_lock); 29 spin_unlock(&ci->i_ceph_lock); 30 inode_dio_wait(inode); 30 inode_dio_wait(inode); 31 } 31 } 32 } 32 } 33 33 34 /** 34 /** 35 * ceph_start_io_read - declare the file is be 35 * ceph_start_io_read - declare the file is being used for buffered reads 36 * @inode: file inode 36 * @inode: file inode 37 * 37 * 38 * Declare that a buffered read operation is a 38 * Declare that a buffered read operation is about to start, and ensure 39 * that we block all direct I/O. 39 * that we block all direct I/O. 40 * On exit, the function ensures that the CEPH 40 * On exit, the function ensures that the CEPH_I_ODIRECT flag is unset, 41 * and holds a shared lock on inode->i_rwsem t 41 * and holds a shared lock on inode->i_rwsem to ensure that the flag 42 * cannot be changed. 42 * cannot be changed. 43 * In practice, this means that buffered read 43 * In practice, this means that buffered read operations are allowed to 44 * execute in parallel, thanks to the shared l 44 * execute in parallel, thanks to the shared lock, whereas direct I/O 45 * operations need to wait to grab an exclusiv 45 * operations need to wait to grab an exclusive lock in order to set 46 * CEPH_I_ODIRECT. 46 * CEPH_I_ODIRECT. 47 * Note that buffered writes and truncates bot 47 * Note that buffered writes and truncates both take a write lock on 48 * inode->i_rwsem, meaning that those are seri 48 * inode->i_rwsem, meaning that those are serialised w.r.t. the reads. 49 */ 49 */ 50 void 50 void 51 ceph_start_io_read(struct inode *inode) 51 ceph_start_io_read(struct inode *inode) 52 { 52 { 53 struct ceph_inode_info *ci = ceph_inod 53 struct ceph_inode_info *ci = ceph_inode(inode); 54 54 55 /* Be an optimist! */ 55 /* Be an optimist! */ 56 down_read(&inode->i_rwsem); 56 down_read(&inode->i_rwsem); 57 if (!(READ_ONCE(ci->i_ceph_flags) & CE 57 if (!(READ_ONCE(ci->i_ceph_flags) & CEPH_I_ODIRECT)) 58 return; 58 return; 59 up_read(&inode->i_rwsem); 59 up_read(&inode->i_rwsem); 60 /* Slow path.... */ 60 /* Slow path.... */ 61 down_write(&inode->i_rwsem); 61 down_write(&inode->i_rwsem); 62 ceph_block_o_direct(ci, inode); 62 ceph_block_o_direct(ci, inode); 63 downgrade_write(&inode->i_rwsem); 63 downgrade_write(&inode->i_rwsem); 64 } 64 } 65 65 66 /** 66 /** 67 * ceph_end_io_read - declare that the buffere 67 * ceph_end_io_read - declare that the buffered read operation is done 68 * @inode: file inode 68 * @inode: file inode 69 * 69 * 70 * Declare that a buffered read operation is d 70 * Declare that a buffered read operation is done, and release the shared 71 * lock on inode->i_rwsem. 71 * lock on inode->i_rwsem. 72 */ 72 */ 73 void 73 void 74 ceph_end_io_read(struct inode *inode) 74 ceph_end_io_read(struct inode *inode) 75 { 75 { 76 up_read(&inode->i_rwsem); 76 up_read(&inode->i_rwsem); 77 } 77 } 78 78 79 /** 79 /** 80 * ceph_start_io_write - declare the file is b 80 * ceph_start_io_write - declare the file is being used for buffered writes 81 * @inode: file inode 81 * @inode: file inode 82 * 82 * 83 * Declare that a buffered write operation is 83 * Declare that a buffered write operation is about to start, and ensure 84 * that we block all direct I/O. 84 * that we block all direct I/O. 85 */ 85 */ 86 void 86 void 87 ceph_start_io_write(struct inode *inode) 87 ceph_start_io_write(struct inode *inode) 88 { 88 { 89 down_write(&inode->i_rwsem); 89 down_write(&inode->i_rwsem); 90 ceph_block_o_direct(ceph_inode(inode), 90 ceph_block_o_direct(ceph_inode(inode), inode); 91 } 91 } 92 92 93 /** 93 /** 94 * ceph_end_io_write - declare that the buffer 94 * ceph_end_io_write - declare that the buffered write operation is done 95 * @inode: file inode 95 * @inode: file inode 96 * 96 * 97 * Declare that a buffered write operation is 97 * Declare that a buffered write operation is done, and release the 98 * lock on inode->i_rwsem. 98 * lock on inode->i_rwsem. 99 */ 99 */ 100 void 100 void 101 ceph_end_io_write(struct inode *inode) 101 ceph_end_io_write(struct inode *inode) 102 { 102 { 103 up_write(&inode->i_rwsem); 103 up_write(&inode->i_rwsem); 104 } 104 } 105 105 106 /* Call with exclusively locked inode->i_rwsem 106 /* Call with exclusively locked inode->i_rwsem */ 107 static void ceph_block_buffered(struct ceph_in 107 static void ceph_block_buffered(struct ceph_inode_info *ci, struct inode *inode) 108 { 108 { 109 lockdep_assert_held_write(&inode->i_rw 109 lockdep_assert_held_write(&inode->i_rwsem); 110 110 111 if (!(READ_ONCE(ci->i_ceph_flags) & CE 111 if (!(READ_ONCE(ci->i_ceph_flags) & CEPH_I_ODIRECT)) { 112 spin_lock(&ci->i_ceph_lock); 112 spin_lock(&ci->i_ceph_lock); 113 ci->i_ceph_flags |= CEPH_I_ODI 113 ci->i_ceph_flags |= CEPH_I_ODIRECT; 114 spin_unlock(&ci->i_ceph_lock); 114 spin_unlock(&ci->i_ceph_lock); 115 /* FIXME: unmap_mapping_range? 115 /* FIXME: unmap_mapping_range? */ 116 filemap_write_and_wait(inode-> 116 filemap_write_and_wait(inode->i_mapping); 117 } 117 } 118 } 118 } 119 119 120 /** 120 /** 121 * ceph_start_io_direct - declare the file is !! 121 * ceph_end_io_direct - declare the file is being used for direct i/o 122 * @inode: file inode 122 * @inode: file inode 123 * 123 * 124 * Declare that a direct I/O operation is abou 124 * Declare that a direct I/O operation is about to start, and ensure 125 * that we block all buffered I/O. 125 * that we block all buffered I/O. 126 * On exit, the function ensures that the CEPH 126 * On exit, the function ensures that the CEPH_I_ODIRECT flag is set, 127 * and holds a shared lock on inode->i_rwsem t 127 * and holds a shared lock on inode->i_rwsem to ensure that the flag 128 * cannot be changed. 128 * cannot be changed. 129 * In practice, this means that direct I/O ope 129 * In practice, this means that direct I/O operations are allowed to 130 * execute in parallel, thanks to the shared l 130 * execute in parallel, thanks to the shared lock, whereas buffered I/O 131 * operations need to wait to grab an exclusiv 131 * operations need to wait to grab an exclusive lock in order to clear 132 * CEPH_I_ODIRECT. 132 * CEPH_I_ODIRECT. 133 * Note that buffered writes and truncates bot 133 * Note that buffered writes and truncates both take a write lock on 134 * inode->i_rwsem, meaning that those are seri 134 * inode->i_rwsem, meaning that those are serialised w.r.t. O_DIRECT. 135 */ 135 */ 136 void 136 void 137 ceph_start_io_direct(struct inode *inode) 137 ceph_start_io_direct(struct inode *inode) 138 { 138 { 139 struct ceph_inode_info *ci = ceph_inod 139 struct ceph_inode_info *ci = ceph_inode(inode); 140 140 141 /* Be an optimist! */ 141 /* Be an optimist! */ 142 down_read(&inode->i_rwsem); 142 down_read(&inode->i_rwsem); 143 if (READ_ONCE(ci->i_ceph_flags) & CEPH 143 if (READ_ONCE(ci->i_ceph_flags) & CEPH_I_ODIRECT) 144 return; 144 return; 145 up_read(&inode->i_rwsem); 145 up_read(&inode->i_rwsem); 146 /* Slow path.... */ 146 /* Slow path.... */ 147 down_write(&inode->i_rwsem); 147 down_write(&inode->i_rwsem); 148 ceph_block_buffered(ci, inode); 148 ceph_block_buffered(ci, inode); 149 downgrade_write(&inode->i_rwsem); 149 downgrade_write(&inode->i_rwsem); 150 } 150 } 151 151 152 /** 152 /** 153 * ceph_end_io_direct - declare that the direc 153 * ceph_end_io_direct - declare that the direct i/o operation is done 154 * @inode: file inode 154 * @inode: file inode 155 * 155 * 156 * Declare that a 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. 157 * lock on inode->i_rwsem. 158 */ 158 */ 159 void 159 void 160 ceph_end_io_direct(struct inode *inode) 160 ceph_end_io_direct(struct inode *inode) 161 { 161 { 162 up_read(&inode->i_rwsem); 162 up_read(&inode->i_rwsem); 163 } 163 } 164 164
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.