From 2c7556e536fe56b0f7ddb4efc8cd0ae3d2d137df Mon Sep 17 00:00:00 2001 From: Vitaliy Filippov Date: Sat, 11 Dec 2021 22:03:16 +0000 Subject: [PATCH] Allow to run with 4k sector size. Natural, but it was forbidden --- src/blockstore_impl.h | 1 + src/blockstore_open.cpp | 44 ++++++++++++++++++++++++++++++++--------- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/blockstore_impl.h b/src/blockstore_impl.h index 51ac5b0d1..4a1edfe44 100644 --- a/src/blockstore_impl.h +++ b/src/blockstore_impl.h @@ -251,6 +251,7 @@ class blockstore_impl_t int data_fd; uint64_t meta_size, meta_area, meta_len; uint64_t data_size, data_len; + uint64_t data_device_sect, meta_device_sect, journal_device_sect; void *metadata_buffer = NULL; diff --git a/src/blockstore_open.cpp b/src/blockstore_open.cpp index 766e3ad07..afda9e0db 100644 --- a/src/blockstore_open.cpp +++ b/src/blockstore_open.cpp @@ -295,9 +295,9 @@ void blockstore_impl_t::calc_lengths() } } -void check_size(int fd, uint64_t *size, std::string name) +static void check_size(int fd, uint64_t *size, uint64_t *sectsize, std::string name) { - int sectsize; + int sect; struct stat st; if (fstat(fd, &st) < 0) { @@ -309,11 +309,14 @@ void check_size(int fd, uint64_t *size, std::string name) } else if (S_ISBLK(st.st_mode)) { - if (ioctl(fd, BLKSSZGET, §size) < 0 || - ioctl(fd, BLKGETSIZE64, size) < 0 || - sectsize != 512) + if (ioctl(fd, BLKGETSIZE64, size) < 0 || + ioctl(fd, BLKSSZGET, §) < 0) { - throw std::runtime_error(name+" sector is not equal to 512 bytes"); + throw std::runtime_error("failed to get "+name+" size or block size: "+strerror(errno)); + } + if (sectsize) + { + *sectsize = sect; } } else @@ -329,7 +332,14 @@ void blockstore_impl_t::open_data() { throw std::runtime_error("Failed to open data device"); } - check_size(data_fd, &data_size, "data device"); + check_size(data_fd, &data_size, &data_device_sect, "data device"); + if (disk_alignment % data_device_sect) + { + throw std::runtime_error( + "disk_alignment ("+std::to_string(disk_alignment)+ + ") is not a multiple of data device sector size ("+std::to_string(data_device_sect)+")" + ); + } if (data_offset >= data_size) { throw std::runtime_error("data_offset exceeds device size = "+std::to_string(data_size)); @@ -350,7 +360,7 @@ void blockstore_impl_t::open_meta() { throw std::runtime_error("Failed to open metadata device"); } - check_size(meta_fd, &meta_size, "metadata device"); + check_size(meta_fd, &meta_size, &meta_device_sect, "metadata device"); if (meta_offset >= meta_size) { throw std::runtime_error("meta_offset exceeds device size = "+std::to_string(meta_size)); @@ -363,12 +373,20 @@ void blockstore_impl_t::open_meta() else { meta_fd = data_fd; + meta_device_sect = data_device_sect; meta_size = 0; if (meta_offset >= data_size) { throw std::runtime_error("meta_offset exceeds device size = "+std::to_string(data_size)); } } + if (meta_block_size % meta_device_sect) + { + throw std::runtime_error( + "meta_block_size ("+std::to_string(meta_block_size)+ + ") is not a multiple of data device sector size ("+std::to_string(meta_device_sect)+")" + ); + } } void blockstore_impl_t::open_journal() @@ -380,7 +398,7 @@ void blockstore_impl_t::open_journal() { throw std::runtime_error("Failed to open journal device"); } - check_size(journal.fd, &journal.device_size, "journal device"); + check_size(journal.fd, &journal.device_size, &journal_device_sect, "journal device"); if (!disable_flock && flock(journal.fd, LOCK_EX|LOCK_NB) != 0) { throw std::runtime_error(std::string("Failed to lock journal device: ") + strerror(errno)); @@ -389,6 +407,7 @@ void blockstore_impl_t::open_journal() else { journal.fd = meta_fd; + journal_device_sect = meta_device_sect; journal.device_size = 0; if (journal.offset >= data_size) { @@ -406,4 +425,11 @@ void blockstore_impl_t::open_journal() if (!journal.sector_buf) throw std::bad_alloc(); } + if (journal_block_size % journal_device_sect) + { + throw std::runtime_error( + "journal_block_size ("+std::to_string(journal_block_size)+ + ") is not a multiple of journal device sector size ("+std::to_string(journal_device_sect)+")" + ); + } }