]> git.proxmox.com Git - ceph.git/blob - ceph/src/os/ObjectStore.cc
import ceph quincy 17.2.6
[ceph.git] / ceph / src / os / ObjectStore.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14 #include <ctype.h>
15 #include <sstream>
16 #include "ObjectStore.h"
17 #include "common/Formatter.h"
18 #include "common/safe_io.h"
19
20 #ifndef WITH_SEASTAR
21 #include "filestore/FileStore.h"
22 #endif
23 #include "memstore/MemStore.h"
24 #if defined(WITH_BLUESTORE)
25 #include "bluestore/BlueStore.h"
26 #endif
27 #ifndef WITH_SEASTAR
28 #include "kstore/KStore.h"
29 #endif
30
31 using std::string;
32
33 std::unique_ptr<ObjectStore> ObjectStore::create(
34 CephContext *cct,
35 const string& type,
36 const string& data)
37 {
38 if (type == "memstore") {
39 return std::make_unique<MemStore>(cct, data);
40 }
41 #if defined(WITH_BLUESTORE)
42 if (type == "bluestore" || type == "random") {
43 return std::make_unique<BlueStore>(cct, data);
44 }
45 #endif
46 return nullptr;
47 }
48
49 #ifndef WITH_SEASTAR
50 std::unique_ptr<ObjectStore> ObjectStore::create(
51 CephContext *cct,
52 const string& type,
53 const string& data,
54 const string& journal,
55 osflagbits_t flags)
56 {
57 if (type == "filestore" || (type == "random" && rand() % 2)) {
58 return std::make_unique<FileStore>(cct, data, journal, flags);
59 }
60 if (type == "kstore" &&
61 cct->check_experimental_feature_enabled("kstore")) {
62 return std::make_unique<KStore>(cct, data);
63 }
64 return create(cct, type, data);
65 }
66 #endif
67
68 int ObjectStore::probe_block_device_fsid(
69 CephContext *cct,
70 const string& path,
71 uuid_d *fsid)
72 {
73 int r;
74
75 #if defined(WITH_BLUESTORE)
76 // first try bluestore -- it has a crc on its header and will fail
77 // reliably.
78 r = BlueStore::get_block_device_fsid(cct, path, fsid);
79 if (r == 0) {
80 lgeneric_dout(cct, 0) << __func__ << " " << path << " is bluestore, "
81 << *fsid << dendl;
82 return r;
83 }
84 #endif
85
86 #ifndef WITH_SEASTAR
87 // okay, try FileStore (journal).
88 r = FileStore::get_block_device_fsid(cct, path, fsid);
89 if (r == 0) {
90 lgeneric_dout(cct, 0) << __func__ << " " << path << " is filestore, "
91 << *fsid << dendl;
92 return r;
93 }
94 #endif
95
96 return -EINVAL;
97 }
98
99 int ObjectStore::write_meta(const std::string& key,
100 const std::string& value)
101 {
102 string v = value;
103 v += "\n";
104 int r = safe_write_file(path.c_str(), key.c_str(),
105 v.c_str(), v.length(), 0600);
106 if (r < 0)
107 return r;
108 return 0;
109 }
110
111 int ObjectStore::read_meta(const std::string& key,
112 std::string *value)
113 {
114 char buf[4096];
115 int r = safe_read_file(path.c_str(), key.c_str(),
116 buf, sizeof(buf));
117 if (r <= 0)
118 return r;
119 // drop trailing newlines
120 while (r && isspace(buf[r-1])) {
121 --r;
122 }
123 *value = string(buf, r);
124 return 0;
125 }