]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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> | |
7c673cae FG |
16 | #include "ObjectStore.h" |
17 | #include "common/Formatter.h" | |
18 | #include "common/safe_io.h" | |
19 | ||
9f95a23c | 20 | #ifndef WITH_SEASTAR |
7c673cae FG |
21 | #include "filestore/FileStore.h" |
22 | #include "memstore/MemStore.h" | |
9f95a23c | 23 | #endif |
11fdf7f2 | 24 | #if defined(WITH_BLUESTORE) |
7c673cae FG |
25 | #include "bluestore/BlueStore.h" |
26 | #endif | |
9f95a23c | 27 | #ifndef WITH_SEASTAR |
7c673cae | 28 | #include "kstore/KStore.h" |
9f95a23c | 29 | #endif |
7c673cae FG |
30 | |
31 | ObjectStore *ObjectStore::create(CephContext *cct, | |
32 | const string& type, | |
33 | const string& data, | |
34 | const string& journal, | |
35 | osflagbits_t flags) | |
36 | { | |
9f95a23c | 37 | #ifndef WITH_SEASTAR |
7c673cae FG |
38 | if (type == "filestore") { |
39 | return new FileStore(cct, data, journal, flags); | |
40 | } | |
41 | if (type == "memstore") { | |
42 | return new MemStore(cct, data); | |
43 | } | |
9f95a23c | 44 | #endif |
11fdf7f2 | 45 | #if defined(WITH_BLUESTORE) |
31f18b77 | 46 | if (type == "bluestore") { |
7c673cae FG |
47 | return new BlueStore(cct, data); |
48 | } | |
9f95a23c | 49 | #ifndef WITH_SEASTAR |
31f18b77 | 50 | if (type == "random") { |
7c673cae FG |
51 | if (rand() % 2) { |
52 | return new FileStore(cct, data, journal, flags); | |
53 | } else { | |
54 | return new BlueStore(cct, data); | |
55 | } | |
56 | } | |
9f95a23c | 57 | #endif |
7c673cae | 58 | #else |
9f95a23c | 59 | #ifndef WITH_SEASTAR |
7c673cae FG |
60 | if (type == "random") { |
61 | return new FileStore(cct, data, journal, flags); | |
62 | } | |
63 | #endif | |
9f95a23c TL |
64 | #endif |
65 | #ifndef WITH_SEASTAR | |
7c673cae FG |
66 | if (type == "kstore" && |
67 | cct->check_experimental_feature_enabled("kstore")) { | |
68 | return new KStore(cct, data); | |
69 | } | |
9f95a23c | 70 | #endif |
7c673cae FG |
71 | return NULL; |
72 | } | |
73 | ||
74 | int ObjectStore::probe_block_device_fsid( | |
75 | CephContext *cct, | |
76 | const string& path, | |
77 | uuid_d *fsid) | |
78 | { | |
79 | int r; | |
80 | ||
11fdf7f2 | 81 | #if defined(WITH_BLUESTORE) |
7c673cae FG |
82 | // first try bluestore -- it has a crc on its header and will fail |
83 | // reliably. | |
84 | r = BlueStore::get_block_device_fsid(cct, path, fsid); | |
85 | if (r == 0) { | |
86 | lgeneric_dout(cct, 0) << __func__ << " " << path << " is bluestore, " | |
87 | << *fsid << dendl; | |
88 | return r; | |
89 | } | |
90 | #endif | |
91 | ||
9f95a23c | 92 | #ifndef WITH_SEASTAR |
7c673cae FG |
93 | // okay, try FileStore (journal). |
94 | r = FileStore::get_block_device_fsid(cct, path, fsid); | |
95 | if (r == 0) { | |
96 | lgeneric_dout(cct, 0) << __func__ << " " << path << " is filestore, " | |
97 | << *fsid << dendl; | |
98 | return r; | |
99 | } | |
9f95a23c | 100 | #endif |
7c673cae FG |
101 | |
102 | return -EINVAL; | |
103 | } | |
104 | ||
105 | int ObjectStore::write_meta(const std::string& key, | |
106 | const std::string& value) | |
107 | { | |
108 | string v = value; | |
109 | v += "\n"; | |
110 | int r = safe_write_file(path.c_str(), key.c_str(), | |
eafe8130 | 111 | v.c_str(), v.length(), 0600); |
7c673cae FG |
112 | if (r < 0) |
113 | return r; | |
114 | return 0; | |
115 | } | |
116 | ||
117 | int ObjectStore::read_meta(const std::string& key, | |
118 | std::string *value) | |
119 | { | |
120 | char buf[4096]; | |
121 | int r = safe_read_file(path.c_str(), key.c_str(), | |
122 | buf, sizeof(buf)); | |
123 | if (r <= 0) | |
124 | return r; | |
125 | // drop trailing newlines | |
126 | while (r && isspace(buf[r-1])) { | |
127 | --r; | |
128 | } | |
129 | *value = string(buf, r); | |
130 | return 0; | |
131 | } |