From b73861aaa9780388dd1229f626d6d5f7c58a9aeb Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Mon, 1 Feb 2010 11:56:01 -0800 Subject: [PATCH] journal: warn if we detect disk write cache is on --- src/os/FileJournal.cc | 44 ++++++++++++++++++++++++++++++++++++++++++- src/os/FileJournal.h | 3 ++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/os/FileJournal.cc b/src/os/FileJournal.cc index 796db99da45f1..2beb4fa138c82 100644 --- a/src/os/FileJournal.cc +++ b/src/os/FileJournal.cc @@ -14,6 +14,7 @@ #include "config.h" #include "FileJournal.h" +#include "include/color.h" #include #include @@ -81,7 +82,48 @@ int FileJournal::_open(bool forwrite, bool create) max_size = sectors * 512ULL; # endif #endif - } + is_bdev = true; + } + + // try to check if the disk write cache is on + if (is_bdev) { + if (geteuid() == 0) { + char cmd[4096]; + snprintf(cmd, sizeof(cmd), "/sbin/hdparm -W %s > /tmp/out.%d", fn.c_str(), getpid()); + int r = system(cmd); + if (r == 0) { + snprintf(cmd, sizeof(cmd), "/tmp/out.%d", getpid()); + FILE *f = fopen(cmd, "r"); + if (f) { + while (!feof(f)) { + char s[100]; + fgets(s, sizeof(s), f); + int on; + if (sscanf(s, " write-caching = %d", &on) == 1) { + if (on) { + dout(0) << "WARNING: disk write cache is ON, journaling will not be reliable" << dendl; + dout(0) << " disable with 'hdparm -W 0 " << fn << "'" << dendl; + cout << TEXT_RED + << " ** WARNING: disk write cache is ON on " << fn << ".\n" + << " Journaling will not be reliable. Disable write cache with\n" + << " 'hdparm -W 0 " << fn << "'" + << TEXT_NORMAL + << std::endl; + } else { + dout(10) << "_open disk write cache is off (good) on " << fn << dendl; + } + break; + } + } + fclose(f); + } + ::unlink(cmd); + } else + dout(10) << "_open failed to run '" << cmd << "', NOT checking disk write cache on " << fn << dendl; + } else + dout(10) << "_open not root, NOT checking disk write cache on " << fn << dendl; + } else + dout(10) << "_open journal is not a block device, NOT checking disk write cache on " << fn << dendl; // static zeroed buffer for alignment padding zero_buf = new char[header.alignment]; diff --git a/src/os/FileJournal.h b/src/os/FileJournal.h index 60ba57bfe6b1b..aa90153298388 100644 --- a/src/os/FileJournal.h +++ b/src/os/FileJournal.h @@ -73,6 +73,7 @@ private: off64_t max_size; size_t block_size; + bool is_bdev; bool directio; bool writing, must_write_header; off64_t write_pos; // byte where the next entry to be written will go @@ -140,7 +141,7 @@ private: Journal(fsid, fin, sync_cond), fn(f), zero_buf(NULL), max_size(0), block_size(0), - directio(dio), + is_bdev(false),directio(dio), writing(false), must_write_header(false), write_pos(0), read_pos(0), last_committed_seq(0), -- 2.39.5