// SPDX-License-Identifier: GPL-2.01/*2* Copyright (c) 2016 Trond Myklebust3* Copyright (c) 2019 Jeff Layton4*5* I/O and data path helper functionality.6*7* Heavily borrowed from equivalent code in fs/nfs/io.c8*/910#include <linux/ceph/ceph_debug.h>1112#include <linux/types.h>13#include <linux/kernel.h>14#include <linux/rwsem.h>15#include <linux/fs.h>1617#include "super.h"18#include "io.h"1920/* Call with exclusively locked inode->i_rwsem */21static void ceph_block_o_direct(struct ceph_inode_info *ci, struct inode *inode)22{23lockdep_assert_held_write(&inode->i_rwsem);2425if (READ_ONCE(ci->i_ceph_flags) & CEPH_I_ODIRECT) {26spin_lock(&ci->i_ceph_lock);27ci->i_ceph_flags &= ~CEPH_I_ODIRECT;28spin_unlock(&ci->i_ceph_lock);29inode_dio_wait(inode);30}31}3233/**34* ceph_start_io_read - declare the file is being used for buffered reads35* @inode: file inode36*37* Declare that a buffered read operation is about to start, and ensure38* that we block all direct I/O.39* On exit, the function ensures that the CEPH_I_ODIRECT flag is unset,40* and holds a shared lock on inode->i_rwsem to ensure that the flag41* cannot be changed.42* In practice, this means that buffered read operations are allowed to43* execute in parallel, thanks to the shared lock, whereas direct I/O44* operations need to wait to grab an exclusive lock in order to set45* CEPH_I_ODIRECT.46* Note that buffered writes and truncates both take a write lock on47* inode->i_rwsem, meaning that those are serialised w.r.t. the reads.48*/49void50ceph_start_io_read(struct inode *inode)51{52struct ceph_inode_info *ci = ceph_inode(inode);5354/* Be an optimist! */55down_read(&inode->i_rwsem);56if (!(READ_ONCE(ci->i_ceph_flags) & CEPH_I_ODIRECT))57return;58up_read(&inode->i_rwsem);59/* Slow path.... */60down_write(&inode->i_rwsem);61ceph_block_o_direct(ci, inode);62downgrade_write(&inode->i_rwsem);63}6465/**66* ceph_end_io_read - declare that the buffered read operation is done67* @inode: file inode68*69* Declare that a buffered read operation is done, and release the shared70* lock on inode->i_rwsem.71*/72void73ceph_end_io_read(struct inode *inode)74{75up_read(&inode->i_rwsem);76}7778/**79* ceph_start_io_write - declare the file is being used for buffered writes80* @inode: file inode81*82* Declare that a buffered write operation is about to start, and ensure83* that we block all direct I/O.84*/85void86ceph_start_io_write(struct inode *inode)87{88down_write(&inode->i_rwsem);89ceph_block_o_direct(ceph_inode(inode), inode);90}9192/**93* ceph_end_io_write - declare that the buffered write operation is done94* @inode: file inode95*96* Declare that a buffered write operation is done, and release the97* lock on inode->i_rwsem.98*/99void100ceph_end_io_write(struct inode *inode)101{102up_write(&inode->i_rwsem);103}104105/* Call with exclusively locked inode->i_rwsem */106static void ceph_block_buffered(struct ceph_inode_info *ci, struct inode *inode)107{108lockdep_assert_held_write(&inode->i_rwsem);109110if (!(READ_ONCE(ci->i_ceph_flags) & CEPH_I_ODIRECT)) {111spin_lock(&ci->i_ceph_lock);112ci->i_ceph_flags |= CEPH_I_ODIRECT;113spin_unlock(&ci->i_ceph_lock);114/* FIXME: unmap_mapping_range? */115filemap_write_and_wait(inode->i_mapping);116}117}118119/**120* ceph_start_io_direct - declare the file is being used for direct i/o121* @inode: file inode122*123* Declare that a direct I/O operation is about to start, and ensure124* that we block all buffered I/O.125* On exit, the function ensures that the CEPH_I_ODIRECT flag is set,126* and holds a shared lock on inode->i_rwsem to ensure that the flag127* cannot be changed.128* In practice, this means that direct I/O operations are allowed to129* execute in parallel, thanks to the shared lock, whereas buffered I/O130* operations need to wait to grab an exclusive lock in order to clear131* CEPH_I_ODIRECT.132* Note that buffered writes and truncates both take a write lock on133* inode->i_rwsem, meaning that those are serialised w.r.t. O_DIRECT.134*/135void136ceph_start_io_direct(struct inode *inode)137{138struct ceph_inode_info *ci = ceph_inode(inode);139140/* Be an optimist! */141down_read(&inode->i_rwsem);142if (READ_ONCE(ci->i_ceph_flags) & CEPH_I_ODIRECT)143return;144up_read(&inode->i_rwsem);145/* Slow path.... */146down_write(&inode->i_rwsem);147ceph_block_buffered(ci, inode);148downgrade_write(&inode->i_rwsem);149}150151/**152* ceph_end_io_direct - declare that the direct i/o operation is done153* @inode: file inode154*155* Declare that a direct I/O operation is done, and release the shared156* lock on inode->i_rwsem.157*/158void159ceph_end_io_direct(struct inode *inode)160{161up_read(&inode->i_rwsem);162}163164165