1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
/*
* Copyright 2018 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "manager.hpp"
#include <algorithm>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
namespace blobs
{
void BlobManager::incrementOpen(const std::string& path)
{
if (path.empty())
{
return;
}
openFiles[path] += 1;
}
void BlobManager::decrementOpen(const std::string& path)
{
if (path.empty())
{
return;
}
/* TODO(venture): Check into the iterator from find, does it makes sense
* to just update it directly? */
auto entry = openFiles.find(path);
if (entry != openFiles.end())
{
/* found it, decrement it and remove it if 0. */
openFiles[path] -= 1;
if (openFiles[path] == 0)
{
openFiles.erase(path);
}
}
}
int BlobManager::getOpen(const std::string& path) const
{
/* No need to input check on the read-only call. */
auto entry = openFiles.find(path);
if (entry != openFiles.end())
{
return entry->second;
}
return 0;
}
void BlobManager::eraseSession(GenericBlobInterface* handler, uint16_t session)
{
sessions.erase(session);
/* Ok for openSessions[handler] to be an empty set */
openSessions[handler].erase(session);
decrementOpen(getPath(session));
}
void BlobManager::cleanUpStaleSessions(GenericBlobInterface* handler)
{
if (openSessions.count(handler) == 0)
{
return;
}
auto timeNow = std::chrono::steady_clock::now();
std::set<uint16_t> expiredSet;
for (auto sessionId : openSessions[handler])
{
if (timeNow - sessions[sessionId].lastActionTime >= sessionTimeout)
{
expiredSet.insert(sessionId);
}
}
for (auto sessionId : expiredSet)
{
std::cerr << "phosphor-ipmi-blobs: expiring stale session " << sessionId
<< std::endl;
/* We do a best case recovery by issuing an expire call. If it fails
* don't erase sessions since the handler side might be still tracking
* it as open. */
if (handler->expire(sessionId))
{
eraseSession(handler, sessionId);
}
else
{
std::cerr << "phosphor-ipmi-blobs: failed to expire session "
<< sessionId << std::endl;
}
}
}
bool BlobManager::registerHandler(std::unique_ptr<GenericBlobInterface> handler)
{
if (!handler)
{
return false;
}
handlers.push_back(std::move(handler));
return true;
}
uint32_t BlobManager::buildBlobList()
{
/* Clear out the current list (IPMI handler is presently single-threaded).
*/
ids.clear();
/* Grab the list of blobs and extend the local list */
for (const auto& h : handlers)
{
std::vector<std::string> blobs = h->getBlobIds();
ids.insert(ids.end(), blobs.begin(), blobs.end());
}
return ids.size();
}
std::string BlobManager::getBlobId(uint32_t index)
{
/* Range check. */
if (index >= ids.size())
{
return "";
}
return ids[index];
}
bool BlobManager::open(uint16_t flags, const std::string& path,
uint16_t* session)
{
GenericBlobInterface* handler = getHandler(path);
/* No handler found. */
if (!handler)
{
return false;
}
/* No sessions available... */
if (!getSession(session))
{
return false;
}
/* Verify flags - must be at least read or write */
if (!(flags & (OpenFlags::read | OpenFlags::write)))
{
/* Neither read not write set, which means calls to Read/Write will
* reject. */
return false;
}
/* Try to clean up anything that's falling out of cleanup timeout for this
* handler */
cleanUpStaleSessions(handler);
if (!handler->open(*session, flags, path))
{
return false;
}
/* Associate session with handler */
sessions[*session] = SessionInfo(path, handler, flags);
openSessions[handler].insert(*session);
incrementOpen(path);
return true;
}
GenericBlobInterface* BlobManager::getHandler(const std::string& path)
{
/* Find a handler. */
auto h = std::find_if(
handlers.begin(), handlers.end(),
[&path](const auto& iter) { return (iter->canHandleBlob(path)); });
if (h != handlers.end())
{
return h->get();
}
return nullptr;
}
std::string BlobManager::getPath(uint16_t session) const
{
auto item = sessions.find(session);
if (item == sessions.end())
{
return "";
}
return item->second.blobId;
}
bool BlobManager::stat(const std::string& path, BlobMeta* meta)
{
/* meta should never be NULL. */
GenericBlobInterface* handler = getHandler(path);
/* No handler found. */
if (!handler)
{
return false;
}
return handler->stat(path, meta);
}
bool BlobManager::stat(uint16_t session, BlobMeta* meta)
{
if (auto handler = getActionHandle(session))
{
return handler->stat(session, meta);
}
return false;
}
bool BlobManager::commit(uint16_t session, const std::vector<uint8_t>& data)
{
if (auto handler = getActionHandle(session))
{
return handler->commit(session, data);
}
return false;
}
bool BlobManager::close(uint16_t session)
{
if (auto handler = getActionHandle(session))
{
if (!handler->close(session))
{
return false;
}
eraseSession(handler, session);
return true;
}
return false;
}
std::vector<uint8_t> BlobManager::read(uint16_t session, uint32_t offset,
uint32_t requestedSize)
{
/* TODO: Currently, configure_ac isn't finding libuserlayer, w.r.t the
* symbols I need.
*/
/** The channel to use for now.
* TODO: We will receive this information through the IPMI message call.
*/
// const int ipmiChannel = ipmi::currentChNum;
/** This information is transport specific.
* TODO: We need a way to know this dynamically.
* on BT, 4 bytes of header, and 1 reply code.
*/
// uint32_t maxTransportSize = ipmi::getChannelMaxTransferSize(ipmiChannel);
if (auto handler = getActionHandle(session, OpenFlags::read))
{
return handler->read(session, offset,
std::min(maximumReadSize, requestedSize));
}
return {};
}
bool BlobManager::write(uint16_t session, uint32_t offset,
const std::vector<uint8_t>& data)
{
if (auto handler = getActionHandle(session, OpenFlags::write))
{
return handler->write(session, offset, data);
}
return {};
}
bool BlobManager::deleteBlob(const std::string& path)
{
GenericBlobInterface* handler = getHandler(path);
/* No handler found. */
if (!handler)
{
return false;
}
/* Check if the file has any open handles. */
if (getOpen(path) > 0)
{
return false;
}
/* Try deleting it. */
return handler->deleteBlob(path);
}
bool BlobManager::writeMeta(uint16_t session, uint32_t offset,
const std::vector<uint8_t>& data)
{
if (auto handler = getActionHandle(session))
{
return handler->writeMeta(session, offset, data);
}
return false;
}
bool BlobManager::getSession(uint16_t* sess)
{
uint16_t tries = 0;
if (!sess)
{
return false;
}
/* This is not meant to fail as you have 64KiB values available. */
/* TODO(venture): We could just count the keys in the session map to know
* if it's full.
*/
do
{
uint16_t lsess = next++;
if (!sessions.count(lsess))
{
/* value not in use, return it. */
(*sess) = lsess;
return true;
}
} while (++tries < 0xffff);
return false;
}
static std::unique_ptr<BlobManager> manager;
ManagerInterface* getBlobManager()
{
if (manager == nullptr)
{
manager = std::make_unique<BlobManager>();
}
return manager.get();
}
} // namespace blobs
|