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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
|
#include "common/async/completion.h"
#include "common/errno.h"
#include "rgw_ssd_driver.h"
#if defined(__linux__)
#include <features.h>
#include <sys/xattr.h>
#endif
#if __has_include(<filesystem>)
#include <filesystem>
namespace efs = std::filesystem;
#else
#include <experimental/filesystem>
namespace efs = std::experimental::filesystem;
#endif
namespace rgw { namespace cache {
constexpr std::string_view ATTR_PREFIX = "user.rgw.";
std::optional<Partition> SSDDriver::get_partition_info(const DoutPrefixProvider* dpp, const std::string& name, const std::string& type)
{
std::string key = name + type;
auto iter = partitions.find(key);
if (iter != partitions.end()) {
return iter->second;
}
return std::nullopt;
}
std::vector<Partition> SSDDriver::list_partitions(const DoutPrefixProvider* dpp)
{
std::vector<Partition> partitions_v;
for (auto& it : SSDDriver::partitions) {
partitions_v.emplace_back(it.second);
}
return partitions_v;
}
int SSDDriver::add_partition_info(Partition& info)
{
std::string key = info.name + info.type;
auto ret = partitions.emplace(key, info);
return ret.second;
}
int SSDDriver::remove_partition_info(Partition& info)
{
std::string key = info.name + info.type;
return partitions.erase(key);
}
SSDDriver::SSDDriver(Partition& partition_info) : partition_info(partition_info),
free_space(partition_info.size)
{
add_partition_info(partition_info);
}
SSDDriver::~SSDDriver()
{
remove_partition_info(partition_info);
}
int SSDDriver::initialize(CephContext* cct, const DoutPrefixProvider* dpp)
{
this->cct = cct;
if(partition_info.location.back() != '/') {
partition_info.location += "/";
}
try {
if (efs::exists(partition_info.location)) {
if (cct->_conf->rgw_d4n_l1_evict_cache_on_start) {
ldpp_dout(dpp, 5) << "initialize: evicting the persistent storage directory on start" << dendl;
for (auto& p : efs::directory_iterator(partition_info.location)) {
efs::remove_all(p.path());
}
}
} else {
ldpp_dout(dpp, 5) << "initialize:: creating the persistent storage directory on start" << dendl;
efs::create_directories(partition_info.location);
}
} catch (const efs::filesystem_error& e) {
ldpp_dout(dpp, 0) << "initialize::: ERROR initializing the cache storage directory '" << partition_info.location <<
"' : " << e.what() << dendl;
//return -EINVAL; Should return error from here?
}
#if defined(HAVE_LIBAIO) && defined(__GLIBC__)
// libaio setup
struct aioinit ainit{0};
ainit.aio_threads = cct->_conf.get_val<int64_t>("rgw_d4n_libaio_aio_threads");
ainit.aio_num = cct->_conf.get_val<int64_t>("rgw_d4n_libaio_aio_num");
ainit.aio_idle_time = 120;
aio_init(&ainit);
#endif
return 0;
}
int SSDDriver::put(const DoutPrefixProvider* dpp, const std::string& key, bufferlist& bl, uint64_t len, rgw::sal::Attrs& attrs, optional_yield y)
{
std::string location = partition_info.location + key;
ldpp_dout(dpp, 20) << __func__ << "(): location=" << location << dendl;
FILE *cache_file = nullptr;
int r = 0;
size_t nbytes = 0;
cache_file = fopen(location.c_str(), "w+");
if (cache_file == nullptr) {
ldpp_dout(dpp, 0) << "ERROR: put::fopen file has return error, errno=" << errno << dendl;
return -errno;
}
nbytes = fwrite(bl.c_str(), 1, len, cache_file);
if (nbytes != len) {
ldpp_dout(dpp, 0) << "ERROR: put::io_write: fwrite has returned error: nbytes!=len, nbytes=" << nbytes << ", len=" << len << dendl;
return -EIO;
}
r = fclose(cache_file);
if (r != 0) {
ldpp_dout(dpp, 0) << "ERROR: put::fclose file has return error, errno=" << errno << dendl;
return -errno;
}
efs::space_info space = efs::space(partition_info.location);
this->free_space = space.available;
if (attrs.size() > 0) {
r = set_attrs(dpp, key, attrs, y);
if (r < 0) {
ldpp_dout(dpp, 0) << "ERROR: put::set_attrs: failed to set attrs, r = " << r << dendl;
return r;
}
}
return 0;
}
int SSDDriver::get(const DoutPrefixProvider* dpp, const std::string& key, off_t offset, uint64_t len, bufferlist& bl, rgw::sal::Attrs& attrs, optional_yield y)
{
char buffer[len];
std::string location = partition_info.location + key;
ldpp_dout(dpp, 20) << __func__ << "(): location=" << location << dendl;
FILE *cache_file = nullptr;
int r = 0;
size_t nbytes = 0;
cache_file = fopen(location.c_str(), "r+");
if (cache_file == nullptr) {
ldpp_dout(dpp, 0) << "ERROR: put::fopen file has return error, errno=" << errno << dendl;
return -errno;
}
nbytes = fread(buffer, sizeof(buffer), 1 , cache_file);
if (nbytes != len) {
ldpp_dout(dpp, 0) << "ERROR: put::io_read: fread has returned error: nbytes!=len, nbytes=" << nbytes << ", len=" << len << dendl;
return -EIO;
}
r = fclose(cache_file);
if (r != 0) {
ldpp_dout(dpp, 0) << "ERROR: put::fclose file has return error, errno=" << errno << dendl;
return -errno;
}
ceph::encode(buffer, bl);
if (attrs.size() > 0) {
r = get_attrs(dpp, key, attrs, y);
if (r < 0) {
ldpp_dout(dpp, 0) << "ERROR: put::get_attrs: failed to get attrs, r = " << r << dendl;
return r;
}
}
return 0;
}
int SSDDriver::append_data(const DoutPrefixProvider* dpp, const::std::string& key, bufferlist& bl_data, optional_yield y)
{
std::string location = partition_info.location + key;
ldpp_dout(dpp, 20) << __func__ << "(): location=" << location << dendl;
FILE *cache_file = nullptr;
int r = 0;
size_t nbytes = 0;
cache_file = fopen(location.c_str(), "a+");
if (cache_file == nullptr) {
ldpp_dout(dpp, 0) << "ERROR: put::fopen file has return error, errno=" << errno << dendl;
return -errno;
}
nbytes = fwrite(bl_data.c_str(), 1, bl_data.length(), cache_file);
if (nbytes != bl_data.length()) {
ldpp_dout(dpp, 0) << "ERROR: append_data: fwrite has returned error: nbytes!=len, nbytes=" << nbytes << ", len=" << bl_data.length() << dendl;
return -EIO;
}
r = fclose(cache_file);
if (r != 0) {
ldpp_dout(dpp, 0) << "ERROR: append_data::fclose file has return error, errno=" << errno << dendl;
return -errno;
}
efs::space_info space = efs::space(partition_info.location);
this->free_space = space.available;
return 0;
}
template <typename Executor1, typename CompletionHandler>
auto SSDDriver::AsyncReadOp::create(const Executor1& ex1, CompletionHandler&& handler)
{
auto p = Completion::create(ex1, std::move(handler));
return p;
}
template <typename ExecutionContext, typename CompletionToken>
auto SSDDriver::get_async(const DoutPrefixProvider *dpp, ExecutionContext& ctx, const std::string& key,
off_t read_ofs, off_t read_len, CompletionToken&& token)
{
std::string location = partition_info.location + key;
ldpp_dout(dpp, 20) << "SSDCache: " << __func__ << "(): location=" << location << dendl;
using Op = AsyncReadOp;
using Signature = typename Op::Signature;
boost::asio::async_completion<CompletionToken, Signature> init(token);
auto p = Op::create(ctx.get_executor(), init.completion_handler);
auto& op = p->user_data;
int ret = op.init(dpp, cct, location, read_ofs, read_len, p.get());
if(0 == ret) {
ret = ::aio_read(op.aio_cb.get());
}
ldpp_dout(dpp, 20) << "SSDCache: " << __func__ << "(): ::aio_read(), ret=" << ret << dendl;
if(ret < 0) {
auto ec = boost::system::error_code{-ret, boost::system::system_category()};
ceph::async::post(std::move(p), ec, bufferlist{});
} else {
(void)p.release();
}
return init.result.get();
}
rgw::Aio::OpFunc SSDDriver::ssd_cache_read_op(const DoutPrefixProvider *dpp, optional_yield y, rgw::cache::CacheDriver* cache_driver,
off_t read_ofs, off_t read_len, const std::string& key) {
return [this, dpp, y, read_ofs, read_len, key] (Aio* aio, AioResult& r) mutable {
ceph_assert(y);
ldpp_dout(dpp, 20) << "SSDCache: cache_read_op(): Read From Cache, oid=" << r.obj.oid << dendl;
using namespace boost::asio;
spawn::yield_context yield = y.get_yield_context();
async_completion<spawn::yield_context, void()> init(yield);
auto ex = get_associated_executor(init.completion_handler);
ldpp_dout(dpp, 20) << "SSDCache: " << __func__ << "(): key=" << key << dendl;
this->get_async(dpp, y.get_io_context(), key, read_ofs, read_len, bind_executor(ex, SSDDriver::libaio_handler{aio, r}));
};
}
rgw::AioResultList SSDDriver::get_async(const DoutPrefixProvider* dpp, optional_yield y, rgw::Aio* aio, const std::string& key, off_t ofs, uint64_t len, uint64_t cost, uint64_t id)
{
rgw_raw_obj r_obj;
r_obj.oid = key;
return aio->get(r_obj, ssd_cache_read_op(dpp, y, this, ofs, len, key), cost, id);
}
void SSDDriver::libaio_write_completion_cb(AsyncWriteRequest* c)
{
efs::space_info space = efs::space(partition_info.location);
this->free_space = space.available;
}
int SSDDriver::put_async(const DoutPrefixProvider* dpp, const std::string& key, bufferlist& bl, uint64_t len, rgw::sal::Attrs& attrs)
{
ldpp_dout(dpp, 20) << "SSDCache: " << __func__ << "(): Write To Cache, oid=" << key << ", len=" << len << dendl;
struct AsyncWriteRequest* wr = new struct AsyncWriteRequest(dpp);
int r = 0;
if ((r = wr->prepare_libaio_write_op(dpp, bl, len, key, partition_info.location)) < 0) {
ldpp_dout(dpp, 0) << "ERROR: SSDCache: " << __func__ << "() prepare libaio write op r=" << r << dendl;
return r;
}
wr->cb->aio_sigevent.sigev_notify = SIGEV_THREAD;
wr->cb->aio_sigevent.sigev_notify_function = SSDDriver::AsyncWriteRequest::libaio_write_cb;
wr->cb->aio_sigevent.sigev_notify_attributes = nullptr;
wr->cb->aio_sigevent.sigev_value.sival_ptr = (void*)wr;
wr->key = key;
wr->priv_data = this;
if ((r = ::aio_write(wr->cb)) != 0) {
ldpp_dout(dpp, 0) << "ERROR: SSDCache: " << __func__ << "() aio_write r=" << r << dendl;
delete wr;
return r;
}
return 0;
}
int SSDDriver::delete_data(const DoutPrefixProvider* dpp, const::std::string& key, optional_yield y)
{
std::string location = partition_info.location + key;
if (!efs::remove(location)) {
ldpp_dout(dpp, 0) << "ERROR: delete_data::remove has failed to remove the file: " << location << dendl;
return -EIO;
}
efs::space_info space = efs::space(partition_info.location);
this->free_space = space.available;
return 0;
}
int SSDDriver::AsyncWriteRequest::prepare_libaio_write_op(const DoutPrefixProvider *dpp, bufferlist& bl, unsigned int len, std::string key, std::string cache_location)
{
std::string location = cache_location + key;
int r = 0;
ldpp_dout(dpp, 20) << "SSDCache: " << __func__ << "(): Write To Cache, location=" << location << dendl;
cb = new struct aiocb;
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
memset(cb, 0, sizeof(struct aiocb));
r = fd = ::open(location.c_str(), O_WRONLY | O_CREAT | O_TRUNC, mode);
if (fd < 0) {
ldpp_dout(dpp, 0) << "ERROR: AsyncWriteRequest::prepare_libaio_write_op: open file failed, errno=" << errno << ", location='" << location.c_str() << "'" << dendl;
return r;
}
if (dpp->get_cct()->_conf->rgw_d4n_l1_fadvise != POSIX_FADV_NORMAL)
posix_fadvise(fd, 0, 0, dpp->get_cct()->_conf->rgw_d4n_l1_fadvise);
cb->aio_fildes = fd;
data = malloc(len);
if (!data) {
ldpp_dout(dpp, 0) << "ERROR: AsyncWriteRequest::prepare_libaio_write_op: memory allocation failed" << dendl;
::close(fd);
return r;
}
cb->aio_buf = data;
memcpy((void*)data, bl.c_str(), len);
cb->aio_nbytes = len;
return r;
}
void SSDDriver::AsyncWriteRequest::libaio_write_cb(sigval sigval)
{
SSDDriver::AsyncWriteRequest* c = static_cast<SSDDriver::AsyncWriteRequest*>(sigval.sival_ptr);
c->priv_data->libaio_write_completion_cb(c);
}
int SSDDriver::AsyncReadOp::init(const DoutPrefixProvider *dpp, CephContext* cct, const std::string& file_path, off_t read_ofs, off_t read_len, void* arg)
{
ldpp_dout(dpp, 20) << "SSDCache: " << __func__ << "(): file_path=" << file_path << dendl;
aio_cb.reset(new struct aiocb);
memset(aio_cb.get(), 0, sizeof(struct aiocb));
aio_cb->aio_fildes = TEMP_FAILURE_RETRY(::open(file_path.c_str(), O_RDONLY|O_CLOEXEC|O_BINARY));
if(aio_cb->aio_fildes < 0) {
int err = errno;
ldpp_dout(dpp, 1) << "ERROR: SSDCache: " << __func__ << "(): can't open " << file_path << " : " << " error: " << err << dendl;
return -err;
}
if (cct->_conf->rgw_d4n_l1_fadvise != POSIX_FADV_NORMAL) {
posix_fadvise(aio_cb->aio_fildes, 0, 0, g_conf()->rgw_d4n_l1_fadvise);
}
bufferptr bp(read_len);
aio_cb->aio_buf = bp.c_str();
result.append(std::move(bp));
aio_cb->aio_nbytes = read_len;
aio_cb->aio_offset = read_ofs;
aio_cb->aio_sigevent.sigev_notify = SIGEV_THREAD;
aio_cb->aio_sigevent.sigev_notify_function = libaio_cb_aio_dispatch;
aio_cb->aio_sigevent.sigev_notify_attributes = nullptr;
aio_cb->aio_sigevent.sigev_value.sival_ptr = arg;
return 0;
}
void SSDDriver::AsyncReadOp::libaio_cb_aio_dispatch(sigval sigval)
{
auto p = std::unique_ptr<Completion>{static_cast<Completion*>(sigval.sival_ptr)};
auto op = std::move(p->user_data);
const int ret = -aio_error(op.aio_cb.get());
boost::system::error_code ec;
if (ret < 0) {
ec.assign(-ret, boost::system::system_category());
}
ceph::async::dispatch(std::move(p), ec, std::move(op.result));
}
int SSDDriver::update_attrs(const DoutPrefixProvider* dpp, const std::string& key, rgw::sal::Attrs& attrs, optional_yield y)
{
std::string location = partition_info.location + key;
ldpp_dout(dpp, 20) << "SSDCache: " << __func__ << "(): location=" << location << dendl;
for (auto& it : attrs) {
std::string attr_name, attr_val;
attr_name = it.first;
attr_val = it.second.c_str();
std::string old_attr_val = get_attr(dpp, key, attr_name, y);
int ret;
if (old_attr_val.empty()) {
ret = setxattr(location.c_str(), attr_name.c_str(), attr_val.c_str(), attr_val.size(), XATTR_CREATE);
} else {
ret = setxattr(location.c_str(), attr_name.c_str(), attr_val.c_str(), attr_val.size(), XATTR_REPLACE);
}
if (ret < 0) {
auto e = errno;
ldpp_dout(dpp, 0) << "SSDCache: " << __func__ << "(): could not modify attr value for attr name: " << attr_name << " key: " << key << " ERROR: " << cpp_strerror(e) <<dendl;
return ret;
}
}
return 0;
}
int SSDDriver::delete_attrs(const DoutPrefixProvider* dpp, const std::string& key, rgw::sal::Attrs& del_attrs, optional_yield y)
{
std::string location = partition_info.location + key;
ldpp_dout(dpp, 20) << "SSDCache: " << __func__ << "(): location=" << location << dendl;
for (auto& it : del_attrs) {
auto ret = delete_attr(dpp, key, it.first);
if (ret < 0) {
ldpp_dout(dpp, 0) << "SSDCache: " << __func__ << "(): could not remove attr value for attr name: " << it.first << " key: " << key << dendl;
return ret;
}
}
return 0;
}
int SSDDriver::get_attrs(const DoutPrefixProvider* dpp, const std::string& key, rgw::sal::Attrs& attrs, optional_yield y)
{
std::string location = partition_info.location + key;
ldpp_dout(dpp, 20) << "SSDCache: " << __func__ << "(): location=" << location << dendl;
char namebuf[64 * 1024];
int ret;
ssize_t buflen = listxattr(location.c_str(), namebuf, sizeof(namebuf));
if (buflen < 0) {
ret = errno;
ldpp_dout(dpp, 0) << "ERROR: could not get attributes for key: " << key << ": " << ret << dendl;
return -ret;
}
char *keyptr = namebuf;
while (buflen > 0) {
ssize_t keylen;
keylen = strlen(keyptr) + 1;
std::string attr_name(keyptr);
std::string::size_type prefixloc = key.find(ATTR_PREFIX);
if (prefixloc == std::string::npos) {
buflen -= keylen;
keyptr += keylen;
continue;
}
std::string attr_value = get_attr(dpp, key, attr_name, y);
bufferlist bl_value;
ceph::encode(attr_value, bl_value);
attrs.emplace(std::move(attr_name), std::move(bl_value));
}
return 0;
}
int SSDDriver::set_attrs(const DoutPrefixProvider* dpp, const std::string& key, rgw::sal::Attrs& attrs, optional_yield y)
{
std::string location = partition_info.location + key;
ldpp_dout(dpp, 20) << "SSDCache: " << __func__ << "(): location=" << location << dendl;
for (auto& [attr_name, attr_val_bl] : attrs) {
ldpp_dout(dpp, 20) << "SSDCache: " << __func__ << "(): attr_name = " << attr_name << " attr_val_bl length: " << attr_val_bl.length() << dendl;
if (attr_val_bl.length() != 0) {
auto ret = set_attr(dpp, key, attr_name, attr_val_bl.c_str(), y);
if (ret < 0) {
ldpp_dout(dpp, 0) << "SSDCache: " << __func__ << "(): could not set attr value for attr name: " << attr_name << " key: " << key << dendl;
return ret;
}
}
}
return 0;
}
std::string SSDDriver::get_attr(const DoutPrefixProvider* dpp, const std::string& key, const std::string& attr_name, optional_yield y)
{
std::string location = partition_info.location + key;
std::string attr_val;
ldpp_dout(dpp, 20) << "SSDCache: " << __func__ << "(): location=" << location << dendl;
ldpp_dout(dpp, 20) << "SSDCache: " << __func__ << "(): get_attr: key: " << attr_name << dendl;
int attr_size = getxattr(location.c_str(), attr_name.c_str(), nullptr, 0);
if (attr_size < 0) {
auto ret = errno;
ldpp_dout(dpp, 0) << "ERROR: could not get attribute " << attr_name << ": " << cpp_strerror(ret) << dendl;
return attr_val;
}
if (attr_size == 0) {
ldpp_dout(dpp, 0) << "ERROR: no attribute value found for attr_name: " << attr_name << dendl;
return attr_val;
}
attr_val.reserve(attr_size + 1);
char* attr_val_ptr = &attr_val[0];
attr_size = getxattr(location.c_str(), attr_name.c_str(), attr_val_ptr, attr_size);
if (attr_size < 0) {
ldpp_dout(dpp, 0) << "SSDCache: " << __func__ << "(): could not get attr value for attr name: " << attr_name << " key: " << key << dendl;
}
return attr_val;
}
int SSDDriver::set_attr(const DoutPrefixProvider* dpp, const std::string& key, const std::string& attr_name, const std::string& attr_val, optional_yield y)
{
std::string location = partition_info.location + key;
ldpp_dout(dpp, 20) << "SSDCache: " << __func__ << "(): location=" << location << dendl;
ldpp_dout(dpp, 20) << "SSDCache: " << __func__ << "(): set_attr: key: " << attr_name << " val: " << attr_val << dendl;
return setxattr(location.c_str(), attr_name.c_str(), attr_val.c_str(), attr_val.size(), 0);
}
int SSDDriver::delete_attr(const DoutPrefixProvider* dpp, const std::string& key, const std::string& attr_name)
{
std::string location = partition_info.location + key;
ldpp_dout(dpp, 20) << "SSDCache: " << __func__ << "(): location=" << location << dendl;
return removexattr(location.c_str(), attr_name.c_str());
}
} } // namespace rgw::cache
|