Skip to content

Commit

Permalink
zbdlib: move zbdlib backend into separate files
Browse files Browse the repository at this point in the history
Declare empty code body for virtual destructor in header
and code motion from zbd_zenfs.cc to zbdlib_zenfs.[cc|h].

Signed-off-by: Jorgen Hansen <[email protected]>
  • Loading branch information
jsvaerke authored and yhr committed Aug 29, 2022
1 parent ebac128 commit 67abe24
Show file tree
Hide file tree
Showing 6 changed files with 281 additions and 230 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
cmake_minimum_required(VERSION 3.4)

set(zenfs_SOURCES "fs/fs_zenfs.cc" "fs/zbd_zenfs.cc" "fs/io_zenfs.cc" "fs/zonefs_zenfs.cc"
PARENT_SCOPE)
"fs/zbdlib_zenfs.cc" PARENT_SCOPE)
set(zenfs_HEADERS "fs/fs_zenfs.h" "fs/zbd_zenfs.h" "fs/io_zenfs.h" "fs/version.h" "fs/metrics.h"
"fs/snapshot.h" "fs/filesystem_utility.h" "fs/zonefs_zenfs.h" PARENT_SCOPE)
"fs/snapshot.h" "fs/filesystem_utility.h" "fs/zonefs_zenfs.h" "fs/zbdlib_zenfs.h" PARENT_SCOPE)
set(zenfs_LIBS "zbd" PARENT_SCOPE)
set(zenfs_CMAKE_EXE_LINKER_FLAGS "-u zenfs_filesystems_reg" PARENT_SCOPE)

Expand Down
226 changes: 1 addition & 225 deletions fs/zbd_zenfs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "rocksdb/env.h"
#include "rocksdb/io_status.h"
#include "snapshot.h"
#include "zbdlib_zenfs.h"
#include "zonefs_zenfs.h"

#define KB (1024)
Expand All @@ -47,231 +48,6 @@

namespace ROCKSDB_NAMESPACE {

ZonedBlockDeviceBackend::~ZonedBlockDeviceBackend() {}

class ZbdlibBackend : public ZonedBlockDeviceBackend {
private:
std::string filename_;
int read_f_;
int read_direct_f_;
int write_f_;

public:
explicit ZbdlibBackend(std::string bdevname);
~ZbdlibBackend() {
zbd_close(read_f_);
zbd_close(read_direct_f_);
zbd_close(write_f_);
}

IOStatus Open(bool readonly, bool exclusive, unsigned int *max_active_zones,
unsigned int *max_open_zones);
std::unique_ptr<ZoneList> ListZones();
IOStatus Reset(uint64_t start, bool *offline, uint64_t *max_capacity);
IOStatus Finish(uint64_t start);
IOStatus Close(uint64_t start);
int Read(char *buf, int size, uint64_t pos, bool direct);
int Write(char *data, uint32_t size, uint64_t pos);

bool ZoneIsSwr(std::unique_ptr<ZoneList> &zones, unsigned int idx) {
struct zbd_zone *z = &((struct zbd_zone *)zones->GetData())[idx];
return zbd_zone_type(z) == ZBD_ZONE_TYPE_SWR;
};

bool ZoneIsOffline(std::unique_ptr<ZoneList> &zones, unsigned int idx) {
struct zbd_zone *z = &((struct zbd_zone *)zones->GetData())[idx];
return zbd_zone_offline(z);
};

bool ZoneIsWritable(std::unique_ptr<ZoneList> &zones, unsigned int idx) {
struct zbd_zone *z = &((struct zbd_zone *)zones->GetData())[idx];
return !(zbd_zone_full(z) || zbd_zone_offline(z) || zbd_zone_rdonly(z));
};

bool ZoneIsActive(std::unique_ptr<ZoneList> &zones, unsigned int idx) {
struct zbd_zone *z = &((struct zbd_zone *)zones->GetData())[idx];
return zbd_zone_imp_open(z) || zbd_zone_exp_open(z) || zbd_zone_closed(z);
};

bool ZoneIsOpen(std::unique_ptr<ZoneList> &zones, unsigned int idx) {
struct zbd_zone *z = &((struct zbd_zone *)zones->GetData())[idx];
return zbd_zone_imp_open(z) || zbd_zone_exp_open(z);
};

uint64_t ZoneStart(std::unique_ptr<ZoneList> &zones, unsigned int idx) {
struct zbd_zone *z = &((struct zbd_zone *)zones->GetData())[idx];
return zbd_zone_start(z);
};

uint64_t ZoneMaxCapacity(std::unique_ptr<ZoneList> &zones, unsigned int idx) {
struct zbd_zone *z = &((struct zbd_zone *)zones->GetData())[idx];
return zbd_zone_capacity(z);
};

uint64_t ZoneWp(std::unique_ptr<ZoneList> &zones, unsigned int idx) {
struct zbd_zone *z = &((struct zbd_zone *)zones->GetData())[idx];
return zbd_zone_wp(z);
};

std::string GetFilename() { return filename_; }

private:
IOStatus CheckScheduler();
std::string ErrorToString(int err);
};

ZbdlibBackend::ZbdlibBackend(std::string bdevname)
: filename_("/dev/" + bdevname),
read_f_(-1),
read_direct_f_(-1),
write_f_(-1) {}

std::string ZbdlibBackend::ErrorToString(int err) {
char *err_str = strerror(err);
if (err_str != nullptr) return std::string(err_str);
return "";
}

IOStatus ZbdlibBackend::CheckScheduler() {
std::ostringstream path;
std::string s = filename_;
std::fstream f;

s.erase(0, 5); // Remove "/dev/" from /dev/nvmeXnY
path << "/sys/block/" << s << "/queue/scheduler";
f.open(path.str(), std::fstream::in);
if (!f.is_open()) {
return IOStatus::InvalidArgument("Failed to open " + path.str());
}

std::string buf;
getline(f, buf);
if (buf.find("[mq-deadline]") == std::string::npos) {
f.close();
return IOStatus::InvalidArgument(
"Current ZBD scheduler is not mq-deadline, set it to mq-deadline.");
}

f.close();
return IOStatus::OK();
}

IOStatus ZbdlibBackend::Open(bool readonly, bool exclusive,
unsigned int *max_active_zones,
unsigned int *max_open_zones) {
zbd_info info;

/* The non-direct file descriptor acts as an exclusive-use semaphore */
if (exclusive) {
read_f_ = zbd_open(filename_.c_str(), O_RDONLY | O_EXCL, &info);
} else {
read_f_ = zbd_open(filename_.c_str(), O_RDONLY, &info);
}

if (read_f_ < 0) {
return IOStatus::InvalidArgument(
"Failed to open zoned block device for read: " + ErrorToString(errno));
}

read_direct_f_ = zbd_open(filename_.c_str(), O_RDONLY | O_DIRECT, &info);
if (read_direct_f_ < 0) {
return IOStatus::InvalidArgument(
"Failed to open zoned block device for direct read: " +
ErrorToString(errno));
}

if (readonly) {
write_f_ = -1;
} else {
write_f_ = zbd_open(filename_.c_str(), O_WRONLY | O_DIRECT, &info);
if (write_f_ < 0) {
return IOStatus::InvalidArgument(
"Failed to open zoned block device for write: " +
ErrorToString(errno));
}
}

if (info.model != ZBD_DM_HOST_MANAGED) {
return IOStatus::NotSupported("Not a host managed block device");
}

IOStatus ios = CheckScheduler();
if (ios != IOStatus::OK()) return ios;

block_sz_ = info.pblock_size;
zone_sz_ = info.zone_size;
nr_zones_ = info.nr_zones;
*max_active_zones = info.max_nr_active_zones;
*max_open_zones = info.max_nr_open_zones;
return IOStatus::OK();
}

std::unique_ptr<ZoneList> ZbdlibBackend::ListZones() {
int ret;
void *zones;
unsigned int nr_zones;

ret = zbd_list_zones(read_f_, 0, zone_sz_ * nr_zones_, ZBD_RO_ALL,
(struct zbd_zone **)&zones, &nr_zones);
if (ret) {
return nullptr;
}

std::unique_ptr<ZoneList> zl(new ZoneList(zones, nr_zones));

return zl;
}

IOStatus ZbdlibBackend::Reset(uint64_t start, bool *offline,
uint64_t *max_capacity) {
unsigned int report = 1;
struct zbd_zone z;
int ret;

ret = zbd_reset_zones(write_f_, start, zone_sz_);
if (ret) return IOStatus::IOError("Zone reset failed\n");

ret = zbd_report_zones(read_f_, start, zone_sz_, ZBD_RO_ALL, &z, &report);

if (ret || (report != 1)) return IOStatus::IOError("Zone report failed\n");

if (zbd_zone_offline(&z)) {
*offline = true;
*max_capacity = 0;
} else {
*offline = false;
*max_capacity = zbd_zone_capacity(&z);
}

return IOStatus::OK();
}

IOStatus ZbdlibBackend::Finish(uint64_t start) {
int ret;

ret = zbd_finish_zones(write_f_, start, zone_sz_);
if (ret) return IOStatus::IOError("Zone finish failed\n");

return IOStatus::OK();
}

IOStatus ZbdlibBackend::Close(uint64_t start) {
int ret;

ret = zbd_close_zones(write_f_, start, zone_sz_);
if (ret) return IOStatus::IOError("Zone close failed\n");

return IOStatus::OK();
}

int ZbdlibBackend::Read(char *buf, int size, uint64_t pos, bool direct) {
return pread(direct ? read_direct_f_ : read_f_, buf, size, pos);
}

int ZbdlibBackend::Write(char *data, uint32_t size, uint64_t pos) {
return pwrite(write_f_, data, size, pos);
}

Zone::Zone(ZonedBlockDevice *zbd, ZonedBlockDeviceBackend *zbd_be,
std::unique_ptr<ZoneList> &zones, unsigned int idx)
: zbd_(zbd),
Expand Down
2 changes: 1 addition & 1 deletion fs/zbd_zenfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class ZonedBlockDeviceBackend {
uint32_t GetBlockSize() { return block_sz_; };
uint64_t GetZoneSize() { return zone_sz_; };
uint32_t GetNrZones() { return nr_zones_; };
virtual ~ZonedBlockDeviceBackend() = 0;
virtual ~ZonedBlockDeviceBackend(){};
};

enum class ZbdBackendType {
Expand Down
Loading

0 comments on commit 67abe24

Please sign in to comment.