db_lmdb: test for mmap support at init time

It'll make it clearer when a DB init failure is due to being
on a filesystem which does not support mmap
This commit is contained in:
moneromooo-monero 2020-05-15 18:39:06 +00:00
parent 77a008f714
commit bd96536637
No known key found for this signature in database
GPG Key ID: 686F07454D6CEFC3
2 changed files with 30 additions and 0 deletions

View File

@ -25,6 +25,13 @@
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef _WIN32
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>
#endif
#include "db_lmdb.h" #include "db_lmdb.h"
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
@ -1287,6 +1294,26 @@ BlockchainLMDB::BlockchainLMDB(bool batch_transactions): BlockchainDB()
m_hardfork = nullptr; m_hardfork = nullptr;
} }
void BlockchainLMDB::check_mmap_support()
{
#ifndef _WIN32
const boost::filesystem::path mmap_test_file = m_folder / boost::filesystem::unique_path();
int mmap_test_fd = ::open(mmap_test_file.string().c_str(), O_RDWR | O_CREAT, 0600);
if (mmap_test_fd < 0)
throw0(DB_ERROR((std::string("Failed to check for mmap support: open failed: ") + strerror(errno)).c_str()));
epee::misc_utils::auto_scope_leave_caller scope_exit_handler = epee::misc_utils::create_scope_leave_handler([mmap_test_fd, &mmap_test_file]() {
::close(mmap_test_fd);
boost::filesystem::remove(mmap_test_file.string());
});
if (write(mmap_test_fd, "mmaptest", 8) != 8)
throw0(DB_ERROR((std::string("Failed to check for mmap support: write failed: ") + strerror(errno)).c_str()));
void *mmap_res = mmap(NULL, 8, PROT_READ, MAP_SHARED, mmap_test_fd, 0);
if (mmap_res == MAP_FAILED)
throw0(DB_ERROR("This filesystem does not support mmap: use --data-dir to place the blockchain on a filesystem which does"));
munmap(mmap_res, 8);
#endif
}
void BlockchainLMDB::open(const std::string& filename, const int db_flags) void BlockchainLMDB::open(const std::string& filename, const int db_flags)
{ {
int result; int result;
@ -1328,6 +1355,8 @@ void BlockchainLMDB::open(const std::string& filename, const int db_flags)
m_folder = filename; m_folder = filename;
check_mmap_support();
#ifdef __OpenBSD__ #ifdef __OpenBSD__
if ((mdb_flags & MDB_WRITEMAP) == 0) { if ((mdb_flags & MDB_WRITEMAP) == 0) {
MCLOG_RED(el::Level::Info, "global", "Running on OpenBSD: forcing WRITEMAP"); MCLOG_RED(el::Level::Info, "global", "Running on OpenBSD: forcing WRITEMAP");

View File

@ -356,6 +356,7 @@ public:
static int compare_string(const MDB_val *a, const MDB_val *b); static int compare_string(const MDB_val *a, const MDB_val *b);
private: private:
void check_mmap_support();
void do_resize(uint64_t size_increase=0); void do_resize(uint64_t size_increase=0);
bool need_resize(uint64_t threshold_size=0) const; bool need_resize(uint64_t threshold_size=0) const;