string magic;
uuid_d cluster_fsid, osd_fsid;
int w;
- int r = OSD::peek_meta(g_conf->osd_data, magic, cluster_fsid, osd_fsid, w);
+ int r = OSD::peek_meta(store, magic, cluster_fsid, osd_fsid, w);
if (r < 0) {
derr << TEXT_RED << " ** ERROR: unable to open OSD superblock on "
<< g_conf->osd_data << ": " << cpp_strerror(-r)
}
FileStore::FileStore(const std::string &base, const std::string &jdev, const char *name, bool do_update) :
+ JournalingObjectStore(base),
internal_name(name),
basedir(base), journalpath(jdev),
blk_size(0),
}
public:
- JournalingObjectStore() : journal(NULL), finisher(g_ceph_context),
- apply_manager(journal, finisher),
- replaying(false) {}
+ JournalingObjectStore(const std::string& path)
+ : ObjectStore(path),
+ journal(NULL),
+ finisher(g_ceph_context),
+ apply_manager(journal, finisher),
+ replaying(false) {}
};
* Foundation. See file COPYING.
*
*/
+#include <ctype.h>
#include <sstream>
#include <tr1/memory>
#include "ObjectStore.h"
#include "common/Formatter.h"
#include "FileStore.h"
+#include "common/safe_io.h"
ObjectStore *ObjectStore::create(const string& type,
const string& data,
return NULL;
}
+int ObjectStore::write_meta(const std::string& key,
+ const std::string& value)
+{
+ string v = value;
+ v += "\n";
+ int r = safe_write_file(path.c_str(), key.c_str(),
+ v.c_str(), v.length());
+ if (r < 0)
+ return r;
+ return 0;
+}
+
+int ObjectStore::read_meta(const std::string& key,
+ std::string *value)
+{
+ char buf[4096];
+ int r = safe_read_file(path.c_str(), key.c_str(),
+ buf, sizeof(buf));
+ if (r <= 0)
+ return r;
+ // drop trailing newlines
+ while (r && isspace(buf[r-1])) {
+ --r;
+ }
+ *value = string(buf, r);
+ return 0;
+}
+
+
+
+
ostream& operator<<(ostream& out, const ObjectStore::Sequencer& s)
{
return out << "osr(" << s.get_name() << " " << &s << ")";
}
class ObjectStore {
+private:
+ string path;
+
public:
/**
* create - create an ObjectStore instance
}
public:
- ObjectStore() : logger(NULL) {}
+ ObjectStore(const std::string& path_) : path(path_), logger(NULL) {}
virtual ~ObjectStore() {}
// mgmt
*/
virtual int peek_journal_fsid(uuid_d *fsid) = 0;
+ /**
+ * write_meta - write a simple configuration key out-of-band
+ *
+ * Write a simple key/value pair for basic store configuration
+ * (e.g., a uuid or magic number) to an unopened/unmounted store.
+ * The default implementation writes this to a plaintext file in the
+ * path.
+ *
+ * A newline is appended.
+ *
+ * @param key key name (e.g., "fsid")
+ * @param value value (e.g., a uuid rendered as a string)
+ * @returns 0 for success, or an error code
+ */
+ virtual int write_meta(const std::string& key,
+ const std::string& value);
+
+ /**
+ * read_meta - read a simple configuration key out-of-band
+ *
+ * Read a simple key value to an unopened/mounted store.
+ *
+ * Trailing whitespace is stripped off.
+ *
+ * @param key key name
+ * @param value pointer to value string
+ * @returns 0 for success, or an error code
+ */
+ virtual int read_meta(const std::string& key,
+ std::string *value);
+
/**
* get ideal min value for collection_list_partial()
*
#include "common/perf_counters.h"
#include "common/Timer.h"
#include "common/LogClient.h"
-#include "common/safe_io.h"
#include "common/HeartbeatMap.h"
#include "common/admin_socket.h"
store->sync_and_flush();
- ret = write_meta(dev, sb.cluster_fsid, sb.osd_fsid, whoami);
+ ret = write_meta(store, sb.cluster_fsid, sb.osd_fsid, whoami);
if (ret) {
derr << "OSD::mkfs: failed to write fsid file: error " << ret << dendl;
goto umount_store;
}
- ret = safe_write_file(dev.c_str(), "ready", "ready\n", 6);
- if (ret) {
- derr << "OSD::mkfs: failed to write ready file: error " << ret << dendl;
- goto umount_store;
- }
-
}
catch (const std::exception &se) {
derr << "OSD::mkfs: caught exception " << se.what() << dendl;
return ret;
}
-int OSD::write_meta(const std::string &base, uuid_d& cluster_fsid, uuid_d& osd_fsid, int whoami)
+int OSD::write_meta(ObjectStore *store, uuid_d& cluster_fsid, uuid_d& osd_fsid, int whoami)
{
char val[80];
+ int r;
- snprintf(val, sizeof(val), "%s\n", CEPH_OSD_ONDISK_MAGIC);
- safe_write_file(base.c_str(), "magic", val, strlen(val));
+ snprintf(val, sizeof(val), "%s", CEPH_OSD_ONDISK_MAGIC);
+ r = store->write_meta("magic", val);
+ if (r < 0)
+ return r;
- snprintf(val, sizeof(val), "%d\n", whoami);
- safe_write_file(base.c_str(), "whoami", val, strlen(val));
+ snprintf(val, sizeof(val), "%d", whoami);
+ r = store->write_meta("whoami", val);
+ if (r < 0)
+ return r;
cluster_fsid.print(val);
- strcat(val, "\n");
- safe_write_file(base.c_str(), "ceph_fsid", val, strlen(val));
+ r = store->write_meta("ceph_fsid", val);
+ if (r < 0)
+ return r;
+
+ r = store->write_meta("ready", "ready");
+ if (r < 0)
+ return r;
return 0;
}
-int OSD::peek_meta(const std::string &dev, std::string& magic,
+int OSD::peek_meta(ObjectStore *store, std::string& magic,
uuid_d& cluster_fsid, uuid_d& osd_fsid, int& whoami)
{
- char val[80] = { 0 };
+ string val;
- if (safe_read_file(dev.c_str(), "magic", val, sizeof(val)) < 0)
- return -errno;
- int l = strlen(val);
- if (l && val[l-1] == '\n')
- val[l-1] = 0;
+ int r = store->read_meta("magic", &magic);
+ if (r < 0)
+ return r;
magic = val;
- if (safe_read_file(dev.c_str(), "whoami", val, sizeof(val)) < 0)
- return -errno;
- whoami = atoi(val);
+ r = store->read_meta("whoami", &val);
+ if (r < 0)
+ return r;
+ whoami = atoi(val.c_str());
- if (safe_read_file(dev.c_str(), "ceph_fsid", val, sizeof(val)) < 0)
- return -errno;
- if (strlen(val) > 36)
- val[36] = 0;
- cluster_fsid.parse(val);
+ r = store->read_meta("ceph_fsid", &val);
+ if (r < 0)
+ return r;
+ r = cluster_fsid.parse(val.c_str());
+ if (r < 0)
+ return r;
- if (safe_read_file(dev.c_str(), "fsid", val, sizeof(val)) < 0)
+ r = store->read_meta("fsid", &val);
+ if (r < 0) {
osd_fsid = uuid_d();
- else {
- if (strlen(val) > 36)
- val[36] = 0;
- osd_fsid.parse(val);
+ } else {
+ r = osd_fsid.parse(val.c_str());
+ if (r < 0)
+ return r;
}
return 0;
}
private:
- static int write_meta(const std::string &base,
+ static int write_meta(ObjectStore *store,
uuid_d& cluster_fsid, uuid_d& osd_fsid, int whoami);
public:
- static int peek_meta(const std::string &dev, string& magic,
+ static int peek_meta(ObjectStore *store, string& magic,
uuid_d& cluster_fsid, uuid_d& osd_fsid, int& whoami);