Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Signed-off-by: gueFDF <[email protected]> #2289

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 38 additions & 43 deletions curvefs/src/volume/block_device_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,23 @@ BlockDeviceClientImpl::BlockDeviceClientImpl()
: fd_(-1), fileClient_(std::make_shared<FileClient>()) {}

BlockDeviceClientImpl::BlockDeviceClientImpl(
const std::shared_ptr<FileClient>& fileClient)
const std::shared_ptr<FileClient> &fileClient)
: fd_(-1), fileClient_(fileClient) {}

bool BlockDeviceClientImpl::Init(const BlockDeviceClientOptions& options) {
bool BlockDeviceClientImpl::Init(const BlockDeviceClientOptions &options) {
auto ret = fileClient_->Init(options.configPath);
if (ret != LIBCURVE_ERROR::OK) {
LOG(ERROR) << "Init file client error: " << ret;
LOG(ERROR) << "Init file client error: " << ret
<< LibCurveErrorName((LIBCURVE_ERROR)ret);
return false;
}

return true;
}
void BlockDeviceClientImpl::UnInit() { fileClient_->UnInit(); }

void BlockDeviceClientImpl::UnInit() {
fileClient_->UnInit();
}

bool BlockDeviceClientImpl::Open(const std::string& filename,
const std::string& owner) {
bool BlockDeviceClientImpl::Open(const std::string &filename,
const std::string &owner) {
UserInfo userInfo(owner);
curve::client::OpenFlags flags;
auto retCode = fileClient_->Open(filename, userInfo, flags);
Expand All @@ -93,36 +91,40 @@ bool BlockDeviceClientImpl::Close() {

int retCode;
if ((retCode = fileClient_->Close(fd_)) != LIBCURVE_ERROR::OK) {
LOG(ERROR) << "Close file failed, retCode = " << retCode;
LOG(ERROR) << "Close file failed, retCode = " << retCode
<< LibCurveErrorName((LIBCURVE_ERROR)retCode);
return false;
}

fd_ = -1;
return true;
}

bool BlockDeviceClientImpl::Stat(const std::string& filename,
const std::string& owner,
BlockDeviceStat* statInfo) {
bool BlockDeviceClientImpl::Stat(const std::string &filename,
const std::string &owner,
BlockDeviceStat *statInfo) {
FileStatInfo fileStatInfo;
UserInfo userInfo(owner);
auto retCode = fileClient_->StatFile(filename, userInfo, &fileStatInfo);
if (retCode != LIBCURVE_ERROR::OK) {
LOG(ERROR) << "Stat file failed, retCode = " << retCode;
LOG(ERROR) << "Stat file failed, retCode = " << retCode
<< LibCurveErrorName((LIBCURVE_ERROR)retCode);
return false;
}

statInfo->length = fileStatInfo.length;
if (!ConvertFileStatus(fileStatInfo.fileStatus, &statInfo->status)) {
LOG(ERROR) << "Stat file failed, unknown file status: "
<< fileStatInfo.fileStatus;
<< fileStatInfo.fileStatus
<< LibCurveErrorName((LIBCURVE_ERROR)retCode);

return false;
}

return true;
}

ssize_t BlockDeviceClientImpl::Read(char* buf, off_t offset, size_t length) {
ssize_t BlockDeviceClientImpl::Read(char *buf, off_t offset, size_t length) {
VLOG(9) << "read request, offset: " << offset << ", length: " << length;

LatencyUpdater updater(&g_read_latency);
Expand All @@ -138,7 +140,7 @@ ssize_t BlockDeviceClientImpl::Read(char* buf, off_t offset, size_t length) {
return request.Wait();
}

ssize_t BlockDeviceClientImpl::Readv(const std::vector<ReadPart>& iov) {
ssize_t BlockDeviceClientImpl::Readv(const std::vector<ReadPart> &iov) {
if (iov.size() == 1) {
VLOG(9) << "read block offset: " << iov[0].offset
<< ", length: " << iov[0].length;
Expand All @@ -148,7 +150,7 @@ ssize_t BlockDeviceClientImpl::Readv(const std::vector<ReadPart>& iov) {
std::vector<std::unique_ptr<AioRead>> requests;
requests.reserve(iov.size());

for (const auto& io : iov) {
for (const auto &io : iov) {
requests.push_back(absl::make_unique<AioRead>(
io.offset, io.length, io.data, fileClient_.get(), fd_));

Expand All @@ -157,7 +159,7 @@ ssize_t BlockDeviceClientImpl::Readv(const std::vector<ReadPart>& iov) {

bool error = false;
ssize_t total = 0;
for (const auto& r : requests) {
for (const auto &r : requests) {
auto nr = r->Wait();
if (nr < 0) {
error = true;
Expand All @@ -171,11 +173,8 @@ ssize_t BlockDeviceClientImpl::Readv(const std::vector<ReadPart>& iov) {
return error ? -1 : total;
}

ssize_t BlockDeviceClientImpl::Write(const char* buf,
off_t offset,
ssize_t BlockDeviceClientImpl::Write(const char *buf, off_t offset,
size_t length) {
VLOG(9) << "write request, offset: " << offset << ", length: " << length;

LatencyUpdater updater(&g_write_latency);

if (fd_ < 0) {
Expand All @@ -189,15 +188,15 @@ ssize_t BlockDeviceClientImpl::Write(const char* buf,
return request.Wait();
}

ssize_t BlockDeviceClientImpl::Writev(const std::vector<WritePart>& iov) {
ssize_t BlockDeviceClientImpl::Writev(const std::vector<WritePart> &iov) {
if (iov.size() == 1) {
return Write(iov[0].data, iov[0].offset, iov[0].length);
}

std::vector<std::unique_ptr<AioWrite>> requests;
requests.reserve(iov.size());

for (const auto& io : iov) {
for (const auto &io : iov) {
requests.push_back(absl::make_unique<AioWrite>(
io.offset, io.length, io.data, fileClient_.get(), fd_));

Expand All @@ -206,7 +205,7 @@ ssize_t BlockDeviceClientImpl::Writev(const std::vector<WritePart>& iov) {

bool error = false;
ssize_t total = 0;
for (const auto& r : requests) {
for (const auto &r : requests) {
auto nr = r->Wait();
if (nr < 0) {
error = true;
Expand All @@ -220,12 +219,11 @@ ssize_t BlockDeviceClientImpl::Writev(const std::vector<WritePart>& iov) {
return error ? -1 : total;
}

bool BlockDeviceClientImpl::WritePadding(char* writeBuffer,
off_t writeStart,
bool BlockDeviceClientImpl::WritePadding(char *writeBuffer, off_t writeStart,
off_t writeEnd,
off_t offset, // actual offset
size_t length) { // actual length
std::vector<std::pair<off_t, size_t>> readvec; // Align reads
std::vector<std::pair<off_t, size_t>> readvec; // Align reads
off_t readEnd = 0;

// Padding leading
Expand All @@ -244,7 +242,7 @@ bool BlockDeviceClientImpl::WritePadding(char* writeBuffer,
}
}

for (const auto& item : readvec) {
for (const auto &item : readvec) {
auto retCode = AlignRead(writeBuffer + item.first - writeStart,
item.first, item.second);
if (retCode != item.second) {
Expand All @@ -255,8 +253,7 @@ bool BlockDeviceClientImpl::WritePadding(char* writeBuffer,
return true;
}

ssize_t BlockDeviceClientImpl::AlignRead(char* buf,
off_t offset,
ssize_t BlockDeviceClientImpl::AlignRead(char *buf, off_t offset,
size_t length) {
auto ret = fileClient_->Read(fd_, buf, offset, length);
if (ret < 0) {
Expand All @@ -271,8 +268,7 @@ ssize_t BlockDeviceClientImpl::AlignRead(char* buf,
return length;
}

ssize_t BlockDeviceClientImpl::AlignWrite(const char* buf,
off_t offset,
ssize_t BlockDeviceClientImpl::AlignWrite(const char *buf, off_t offset,
size_t length) {
auto ret = fileClient_->Write(fd_, buf, offset, length);
if (ret < 0) {
Expand All @@ -288,15 +284,14 @@ ssize_t BlockDeviceClientImpl::AlignWrite(const char* buf,
}

bool BlockDeviceClientImpl::ConvertFileStatus(int fileStatus,
BlockDeviceStatus* bdStatus) {
static const std::map<int, BlockDeviceStatus> fileStatusMap {
{ 0, BlockDeviceStatus::CREATED },
{ 1, BlockDeviceStatus::DELETING },
{ 2, BlockDeviceStatus::CLONING },
{ 3, BlockDeviceStatus::CLONE_META_INSTALLED },
{ 4, BlockDeviceStatus::CLONED },
{ 5, BlockDeviceStatus::BEING_CLONED }
};
BlockDeviceStatus *bdStatus) {
static const std::map<int, BlockDeviceStatus> fileStatusMap{
{0, BlockDeviceStatus::CREATED},
{1, BlockDeviceStatus::DELETING},
{2, BlockDeviceStatus::CLONING},
{3, BlockDeviceStatus::CLONE_META_INSTALLED},
{4, BlockDeviceStatus::CLONED},
{5, BlockDeviceStatus::BEING_CLONED}};

auto iter = fileStatusMap.find(fileStatus);
if (iter == fileStatusMap.end()) {
Expand Down
102 changes: 51 additions & 51 deletions include/client/libcbd.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,77 +47,77 @@ extern "C" {
#define CBD_BACKEND_EXT4
#endif

#define CBD_MAX_FILE_PATH_LEN 1024
#define CBD_MAX_BUF_LEN 1024 * 1024 * 32
#define CBD_MAX_FILE_PATH_LEN 1024
#define CBD_MAX_BUF_LEN 1024 * 1024 * 32

typedef int CurveFd;

typedef struct CurveOptions {
bool inited;
char* conf;
bool inited;
char *conf;
#ifdef CBD_BACKEND_EXT4
char* datahome;
char *datahome;
#endif
} CurveOptions;

int cbd_ext4_init(const CurveOptions* options);
int cbd_ext4_init(const CurveOptions *options);
int cbd_ext4_fini(void);
int cbd_ext4_open(const char* filename);
int cbd_ext4_open(const char *filename);
int cbd_ext4_close(int fd);
int cbd_ext4_pread(int fd, void* buf, off_t offset, size_t length);
int cbd_ext4_pwrite(int fd, const void* buf, off_t offset, size_t length);
int cbd_ext4_pread(int fd, void *buf, off_t offset, size_t length);
int cbd_ext4_pwrite(int fd, const void *buf, off_t offset, size_t length);
int cbd_ext4_pdiscard(int fd, off_t offset, size_t length);
int cbd_ext4_aio_pread(int fd, CurveAioContext* context);
int cbd_ext4_aio_pwrite(int fd, CurveAioContext* context);
int cbd_ext4_aio_pdiscard(int fd, CurveAioContext* context);
int cbd_ext4_aio_pread(int fd, CurveAioContext *context);
int cbd_ext4_aio_pwrite(int fd, CurveAioContext *context);
int cbd_ext4_aio_pdiscard(int fd, CurveAioContext *context);
int cbd_ext4_sync(int fd);
int64_t cbd_ext4_filesize(const char* filename);
int cbd_ext4_increase_epoch(const char* filename);
int64_t cbd_ext4_filesize(const char *filename);
int cbd_ext4_increase_epoch(const char *filename);

int cbd_libcurve_init(const CurveOptions* options);
int cbd_libcurve_init(const CurveOptions *options);
int cbd_libcurve_fini(void);
int cbd_libcurve_open(const char* filename);
int cbd_libcurve_open(const char *filename);
int cbd_libcurve_close(int fd);
int cbd_libcurve_pread(int fd, void* buf, off_t offset, size_t length);
int cbd_libcurve_pwrite(int fd, const void* buf, off_t offset, size_t length);
int cbd_libcurve_pread(int fd, void *buf, off_t offset, size_t length);
int cbd_libcurve_pwrite(int fd, const void *buf, off_t offset, size_t length);
int cbd_libcurve_pdiscard(int fd, off_t offset, size_t length);
int cbd_libcurve_aio_pread(int fd, CurveAioContext* context);
int cbd_libcurve_aio_pwrite(int fd, CurveAioContext* context);
int cbd_libcurve_aio_pdiscard(int fd, CurveAioContext* context);
int cbd_libcurve_aio_pread(int fd, CurveAioContext *context);
int cbd_libcurve_aio_pwrite(int fd, CurveAioContext *context);
int cbd_libcurve_aio_pdiscard(int fd, CurveAioContext *context);
int cbd_libcurve_sync(int fd);
int64_t cbd_libcurve_filesize(const char* filename);
int cbd_libcurve_resize(const char* filename, int64_t size);
int cbd_libcurve_increase_epoch(const char* filename);
int64_t cbd_libcurve_filesize(const char *filename);
int cbd_libcurve_resize(const char *filename, int64_t size);
int cbd_libcurve_increase_epoch(const char *filename);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find that there have two type define styles in curve:
int* p; and int *p;
they are used everywhere, so I think w'd better leave them there, other than change them.
please fix them in other files.


#ifndef CBD_BACKEND_FAKE
#define cbd_lib_init cbd_libcurve_init
#define cbd_lib_fini cbd_libcurve_fini
#define cbd_lib_open cbd_libcurve_open
#define cbd_lib_close cbd_libcurve_close
#define cbd_lib_pread cbd_libcurve_pread
#define cbd_lib_pwrite cbd_libcurve_pwrite
#define cbd_lib_pdiscard cbd_libcurve_pdiscard
#define cbd_lib_aio_pread cbd_libcurve_aio_pread
#define cbd_lib_aio_pwrite cbd_libcurve_aio_pwrite
#define cbd_lib_aio_pdiscard cbd_libcurve_aio_pdiscard
#define cbd_lib_sync cbd_libcurve_sync
#define cbd_lib_filesize cbd_libcurve_filesize
#define cbd_lib_resize cbd_libcurve_resize
#define cbd_lib_increase_epoch cbd_libcurve_increase_epoch
#define cbd_lib_init cbd_libcurve_init
#define cbd_lib_fini cbd_libcurve_fini
#define cbd_lib_open cbd_libcurve_open
#define cbd_lib_close cbd_libcurve_close
#define cbd_lib_pread cbd_libcurve_pread
#define cbd_lib_pwrite cbd_libcurve_pwrite
#define cbd_lib_pdiscard cbd_libcurve_pdiscard
#define cbd_lib_aio_pread cbd_libcurve_aio_pread
#define cbd_lib_aio_pwrite cbd_libcurve_aio_pwrite
#define cbd_lib_aio_pdiscard cbd_libcurve_aio_pdiscard
#define cbd_lib_sync cbd_libcurve_sync
#define cbd_lib_filesize cbd_libcurve_filesize
#define cbd_lib_resize cbd_libcurve_resize
#define cbd_lib_increase_epoch cbd_libcurve_increase_epoch
#else
#define cbd_lib_init cbd_ext4_init
#define cbd_lib_fini cbd_ext4_fini
#define cbd_lib_open cbd_ext4_open
#define cbd_lib_close cbd_ext4_close
#define cbd_lib_pread cbd_ext4_pread
#define cbd_lib_pwrite cbd_ext4_pwrite
#define cbd_lib_pdiscard cbd_ext4_pdiscard
#define cbd_lib_aio_pread cbd_ext4_aio_pread
#define cbd_lib_aio_pwrite cbd_ext4_aio_pwrite
#define cbd_lib_aio_pdiscard cbd_ext4_aio_pdiscard
#define cbd_lib_sync cbd_ext4_sync
#define cbd_lib_filesize cbd_ext4_filesize
#define cbd_lib_increase_epoch cbd_ext4_increase_epoch
#define cbd_lib_init cbd_ext4_init
#define cbd_lib_fini cbd_ext4_fini
#define cbd_lib_open cbd_ext4_open
#define cbd_lib_close cbd_ext4_close
#define cbd_lib_pread cbd_ext4_pread
#define cbd_lib_pwrite cbd_ext4_pwrite
#define cbd_lib_pdiscard cbd_ext4_pdiscard
#define cbd_lib_aio_pread cbd_ext4_aio_pread
#define cbd_lib_aio_pwrite cbd_ext4_aio_pwrite
#define cbd_lib_aio_pdiscard cbd_ext4_aio_pdiscard
#define cbd_lib_sync cbd_ext4_sync
#define cbd_lib_filesize cbd_ext4_filesize
#define cbd_lib_increase_epoch cbd_ext4_increase_epoch
#endif

#ifdef __cplusplus
Expand Down
Loading