From dfa88474b8e5405ad864256424853c4262ca1c17 Mon Sep 17 00:00:00 2001 From: mychoxin Date: Tue, 15 Aug 2017 19:33:12 +0800 Subject: [PATCH] os/bluestore: move assert of read/write to base class Signed-off-by: mychoxin --- src/os/bluestore/BlockDevice.h | 9 +++++++++ src/os/bluestore/KernelDevice.cc | 12 ++---------- src/os/bluestore/NVMEDevice.cc | 18 +++--------------- src/os/bluestore/PMEMDevice.cc | 13 ++++--------- src/os/bluestore/PMEMDevice.h | 7 +++++++ 5 files changed, 25 insertions(+), 34 deletions(-) diff --git a/src/os/bluestore/BlockDevice.h b/src/os/bluestore/BlockDevice.h index 45e4df8c5301..2f6dc17ad64f 100644 --- a/src/os/bluestore/BlockDevice.h +++ b/src/os/bluestore/BlockDevice.h @@ -148,6 +148,15 @@ public: virtual int invalidate_cache(uint64_t off, uint64_t len) = 0; virtual int open(const std::string& path) = 0; virtual void close() = 0; + +protected: + bool is_valid_io(uint64_t off, uint64_t len) const { + return (off % block_size == 0 && + len % block_size == 0 && + len > 0 && + off < size && + off + len <= size); + } }; #endif //CEPH_OS_BLUESTORE_BLOCKDEVICE_H diff --git a/src/os/bluestore/KernelDevice.cc b/src/os/bluestore/KernelDevice.cc index b1e9126326af..5ae6fae8f108 100644 --- a/src/os/bluestore/KernelDevice.cc +++ b/src/os/bluestore/KernelDevice.cc @@ -567,11 +567,7 @@ int KernelDevice::write( dout(20) << __func__ << " 0x" << std::hex << off << "~" << len << std::dec << (buffered ? " (buffered)" : " (direct)") << dendl; - assert(off % block_size == 0); - assert(len % block_size == 0); - assert(len > 0); - assert(off < size); - assert(off + len <= size); + assert(is_valid_io(off, len)); if ((!buffered || bl.get_num_buffers() >= IOV_MAX) && bl.rebuild_aligned_size_and_memory(block_size, block_size)) { @@ -653,11 +649,7 @@ int KernelDevice::read(uint64_t off, uint64_t len, bufferlist *pbl, dout(5) << __func__ << " 0x" << std::hex << off << "~" << len << std::dec << (buffered ? " (buffered)" : " (direct)") << dendl; - assert(off % block_size == 0); - assert(len % block_size == 0); - assert(len > 0); - assert(off < size); - assert(off + len <= size); + assert(is_valid_io(off, len)); _aio_log_start(ioc, off, len); diff --git a/src/os/bluestore/NVMEDevice.cc b/src/os/bluestore/NVMEDevice.cc index c30ecf61b6d1..95984d5f5ef8 100644 --- a/src/os/bluestore/NVMEDevice.cc +++ b/src/os/bluestore/NVMEDevice.cc @@ -988,11 +988,7 @@ int NVMEDevice::aio_write( uint64_t len = bl.length(); dout(20) << __func__ << " " << off << "~" << len << " ioc " << ioc << " buffered " << buffered << dendl; - assert(off % block_size == 0); - assert(len % block_size == 0); - assert(len > 0); - assert(off < size); - assert(off + len <= size); + assert(is_valid_io(off, len)); Task *t = new Task(this, IOCommand::WRITE_COMMAND, off, len); @@ -1036,11 +1032,7 @@ int NVMEDevice::read(uint64_t off, uint64_t len, bufferlist *pbl, bool buffered) { dout(5) << __func__ << " " << off << "~" << len << " ioc " << ioc << dendl; - assert(off % block_size == 0); - assert(len % block_size == 0); - assert(len > 0); - assert(off < size); - assert(off + len <= size); + assert(is_valid_io(off, len)); Task *t = new Task(this, IOCommand::READ_COMMAND, off, len, 1); bufferptr p = buffer::create_page_aligned(len); @@ -1071,11 +1063,7 @@ int NVMEDevice::aio_read( IOContext *ioc) { dout(20) << __func__ << " " << off << "~" << len << " ioc " << ioc << dendl; - assert(off % block_size == 0); - assert(len % block_size == 0); - assert(len > 0); - assert(off < size); - assert(off + len <= size); + assert(is_valid_io(off, len)); Task *t = new Task(this, IOCommand::READ_COMMAND, off, len); diff --git a/src/os/bluestore/PMEMDevice.cc b/src/os/bluestore/PMEMDevice.cc index b86ddc080b21..f094c56c1373 100644 --- a/src/os/bluestore/PMEMDevice.cc +++ b/src/os/bluestore/PMEMDevice.cc @@ -217,9 +217,7 @@ int PMEMDevice::write(uint64_t off, bufferlist& bl, bool buffered) { uint64_t len = bl.length(); dout(20) << __func__ << " " << off << "~" << len << dendl; - assert(len > 0); - assert(off < size); - assert(off + len <= size); + assert(is_valid_io(off, len)); dout(40) << "data: "; bl.hexdump(*_dout); @@ -261,9 +259,7 @@ int PMEMDevice::read(uint64_t off, uint64_t len, bufferlist *pbl, bool buffered) { dout(5) << __func__ << " " << off << "~" << len << dendl; - assert(len > 0); - assert(off < size); - assert(off + len <= size); + assert(is_valid_io(off, len)); bufferptr p = buffer::create_page_aligned(len); memcpy(p.c_str(), addr + off, len); @@ -286,9 +282,8 @@ int PMEMDevice::aio_read(uint64_t off, uint64_t len, bufferlist *pbl, int PMEMDevice::read_random(uint64_t off, uint64_t len, char *buf, bool buffered) { - assert(len > 0); - assert(off < size); - assert(off + len <= size); + dout(5) << __func__ << " " << off << "~" << len << dendl; + assert(is_valid_io(off, len)); memcpy(buf, addr + off, len); return 0; diff --git a/src/os/bluestore/PMEMDevice.h b/src/os/bluestore/PMEMDevice.h index ab3507192c88..89dba8a87840 100644 --- a/src/os/bluestore/PMEMDevice.h +++ b/src/os/bluestore/PMEMDevice.h @@ -60,6 +60,13 @@ public: int invalidate_cache(uint64_t off, uint64_t len) override; int open(const std::string &path) override; void close() override; + +private: + bool is_valid_io(uint64_t off, uint64_t len) const { + return (len > 0 && + off < size && + off + len <= size); + } }; #endif -- 2.47.3