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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
|
#include "common/async/completion.h"
#include "common/errno.h"
#include "common/async/blocked_completion.h"
#include "rgw_ssd_driver.h"
#if defined(__linux__)
#include <features.h>
#include <sys/xattr.h>
#endif
#include <filesystem>
#include <errno.h>
namespace efs = std::filesystem;
namespace rgw { namespace cache {
constexpr std::string_view ATTR_PREFIX = "user.rgw.";
int SSDDriver::initialize(const DoutPrefixProvider* dpp)
{
if(partition_info.location.back() != '/') {
partition_info.location += "/";
}
try {
if (efs::exists(partition_info.location)) {
if (dpp->get_cct()->_conf->rgw_d4n_l1_evict_cache_on_start) {
ldpp_dout(dpp, 5) << "initialize: evicting the persistent storage directory on start" << dendl;
uid_t uid = dpp->get_cct()->get_set_uid();
gid_t gid = dpp->get_cct()->get_set_gid();
ldpp_dout(dpp, 5) << "initialize:: uid is " << uid << " and gid is " << gid << dendl;
ldpp_dout(dpp, 5) << "initialize:: changing permissions for datacache directory." << dendl;
if (uid) {
if (chown(partition_info.location.c_str(), uid, gid) == -1) {
ldpp_dout(dpp, 5) << "initialize: chown return error: " << strerror(errno) << dendl;
}
if (chmod(partition_info.location.c_str(), S_IRWXU|S_IRWXG|S_IRWXO) == -1) {
ldpp_dout(dpp, 5) << "initialize: chmod return error: " << strerror(errno) << 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: " << partition_info.location << dendl;
std::error_code ec;
if (!efs::create_directories(partition_info.location, ec)) {
ldpp_dout(dpp, 0) << "initialize::: ERROR initializing the cache storage directory: '" << partition_info.location <<
"' : " << ec.value() << dendl;
} else {
uid_t uid = dpp->get_cct()->get_set_uid();
gid_t gid = dpp->get_cct()->get_set_gid();
ldpp_dout(dpp, 5) << "initialize:: uid is " << uid << " and gid is " << gid << dendl;
ldpp_dout(dpp, 5) << "initialize:: changing permissions for datacache directory." << dendl;
if (uid) {
if (chown(partition_info.location.c_str(), uid, gid) == -1) {
ldpp_dout(dpp, 5) << "initialize: chown return error: " << strerror(errno) << dendl;
}
if (chmod(partition_info.location.c_str(), S_IRWXU|S_IRWXG|S_IRWXO) == -1) {
ldpp_dout(dpp, 5) << "initialize: chmod return error: " << strerror(errno) << dendl;
}
}
}
}
} 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 = dpp->get_cct()->_conf.get_val<int64_t>("rgw_d4n_libaio_aio_threads");
ainit.aio_num = dpp->get_cct()->_conf.get_val<int64_t>("rgw_d4n_libaio_aio_num");
ainit.aio_idle_time = 120;
aio_init(&ainit);
#endif
efs::space_info space = efs::space(partition_info.location);
//currently partition_info.size is unused
this->free_space = space.available;
return 0;
}
int SSDDriver::put(const DoutPrefixProvider* dpp, const std::string& key, const bufferlist& bl, uint64_t len, const rgw::sal::Attrs& attrs, optional_yield y)
{
ldpp_dout(dpp, 20) << "SSDCache: " << __func__ << "(): key=" << key << dendl;
boost::system::error_code ec;
if (y) {
using namespace boost::asio;
yield_context yield = y.get_yield_context();
auto ex = yield.get_executor();
this->put_async(dpp, ex, key, bl, len, attrs, yield[ec]);
} else {
auto ex = boost::asio::system_executor{};
this->put_async(dpp, ex, key, bl, len, attrs, ceph::async::use_blocked[ec]);
}
if (ec) {
return ec.value();
}
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: get::fopen file has return error, errno=" << errno << dendl;
return -errno;
}
fseek(cache_file, offset, SEEK_SET);
nbytes = fread(buffer, 1, len, cache_file);
if (nbytes != len) {
fclose(cache_file);
ldpp_dout(dpp, 0) << "ERROR: get::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: get::fclose file has return error, errno=" << errno << dendl;
return -errno;
}
bl.append(buffer, len);
r = get_attrs(dpp, key, attrs, y);
if (r < 0) {
ldpp_dout(dpp, 0) << "ERROR: get::get_attrs: failed to get attrs, r = " << r << dendl;
return r;
}
return 0;
}
int SSDDriver::append_data(const DoutPrefixProvider* dpp, const::std::string& key, const bufferlist& bl_data, optional_yield y)
{
bufferlist src = bl_data;
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(src.c_str(), 1, src.length(), cache_file);
if (nbytes != src.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 Executor1, typename CompletionHandler>
auto SSDDriver::AsyncWriteRequest::create(const Executor1& ex1, CompletionHandler&& handler)
{
auto p = Completion::create(ex1, std::move(handler));
return p;
}
template <typename Executor, typename CompletionToken>
auto SSDDriver::get_async(const DoutPrefixProvider *dpp, const Executor& ex, const std::string& key,
off_t read_ofs, off_t read_len, CompletionToken&& token)
{
using Op = AsyncReadOp;
using Signature = typename Op::Signature;
return boost::asio::async_initiate<CompletionToken, Signature>(
[this] (auto handler, const DoutPrefixProvider *dpp,
const Executor& ex, const std::string& key,
off_t read_ofs, off_t read_len) {
auto p = Op::create(ex, handler);
auto& op = p->user_data;
std::string location = partition_info.location + key;
ldpp_dout(dpp, 20) << "SSDCache: " << __func__ << "(): location=" << location << dendl;
int ret = op.prepare_libaio_read_op(dpp, 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();
}
}, token, dpp, ex, key, read_ofs, read_len);
}
template <typename Executor, typename CompletionToken>
void SSDDriver::put_async(const DoutPrefixProvider *dpp, const Executor& ex, const std::string& key,
const bufferlist& bl, uint64_t len, const rgw::sal::Attrs& attrs, CompletionToken&& token)
{
using Op = AsyncWriteRequest;
using Signature = typename Op::Signature;
return boost::asio::async_initiate<CompletionToken, Signature>(
[this] (auto handler, const DoutPrefixProvider *dpp,
const Executor& ex, const std::string& key, const bufferlist& bl,
uint64_t len, const rgw::sal::Attrs& attrs) {
auto p = Op::create(ex, handler);
auto& op = p->user_data;
std::string location = partition_info.location + key;
ldpp_dout(dpp, 20) << "SSDCache: " << __func__ << "(): location=" << location << dendl;
int r = 0;
bufferlist src = bl;
std::string temp_key = key + "_" + std::to_string(index++);
ldpp_dout(dpp, 20) << "SSDCache: " << __func__ << "(): temp key=" << temp_key << dendl;
r = op.prepare_libaio_write_op(dpp, src, len, temp_key, partition_info.location);
op.cb->aio_sigevent.sigev_notify = SIGEV_THREAD;
op.cb->aio_sigevent.sigev_notify_function = SSDDriver::AsyncWriteRequest::libaio_write_cb;
op.cb->aio_sigevent.sigev_notify_attributes = nullptr;
op.cb->aio_sigevent.sigev_value.sival_ptr = (void*)p.get();
op.key = key;
op.temp_key = temp_key;
op.dpp = dpp;
op.priv_data = this;
op.attrs = std::move(attrs);
if (r >= 0) {
r = ::aio_write(op.cb.get());
} else {
ldpp_dout(dpp, 20) << "SSDCache: " << __func__ << "(): ::prepare_libaio_write_op(), r=" << r << dendl;
}
ldpp_dout(dpp, 20) << "SSDCache: " << __func__ << "(): ::aio_write(), r=" << r << dendl;
if(r < 0) {
auto ec = boost::system::error_code{-r, boost::system::system_category()};
ceph::async::post(std::move(p), ec);
} else {
(void)p.release();
}
}, token, dpp, ex, key, bl, len, attrs);
}
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;
yield_context yield = y.get_yield_context();
auto ex = yield.get_executor();
ldpp_dout(dpp, 20) << "SSDCache: " << __func__ << "(): key=" << key << dendl;
this->get_async(dpp, ex, key, read_ofs, read_len, bind_executor(ex, SSDDriver::libaio_read_handler{aio, r}));
};
}
rgw::Aio::OpFunc SSDDriver::ssd_cache_write_op(const DoutPrefixProvider *dpp, optional_yield y, rgw::cache::CacheDriver* cache_driver,
const bufferlist& bl, uint64_t len, const rgw::sal::Attrs& attrs, const std::string& key) {
return [this, dpp, y, bl, len, attrs, key] (Aio* aio, AioResult& r) mutable {
ceph_assert(y);
ldpp_dout(dpp, 20) << "SSDCache: cache_write_op(): Write to Cache, oid=" << r.obj.oid << dendl;
using namespace boost::asio;
yield_context yield = y.get_yield_context();
auto ex = yield.get_executor();
ldpp_dout(dpp, 20) << "SSDCache: " << __func__ << "(): key=" << key << dendl;
this->put_async(dpp, ex, key, bl, len, attrs, bind_executor(ex, SSDDriver::libaio_write_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);
}
rgw::AioResultList SSDDriver::put_async(const DoutPrefixProvider* dpp, optional_yield y, rgw::Aio* aio, const std::string& key, const bufferlist& bl, uint64_t len, const rgw::sal::Attrs& attrs, uint64_t cost, uint64_t id)
{
rgw_raw_obj r_obj;
r_obj.oid = key;
return aio->get(r_obj, ssd_cache_write_op(dpp, y, this, bl, len, attrs, key), cost, id);
}
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.reset(new struct aiocb);
memset(cb.get(), 0, sizeof(struct aiocb));
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
r = fd = TEMP_FAILURE_RETRY(::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) {
auto p = std::unique_ptr<Completion>{static_cast<Completion*>(sigval.sival_ptr)};
auto op = std::move(p->user_data);
ldpp_dout(op.dpp, 20) << "INFO: AsyncWriteRequest::libaio_write_cb: key: " << op.key << dendl;
int ret = -aio_error(op.cb.get());
boost::system::error_code ec;
if (ret < 0) {
ec.assign(-ret, boost::system::system_category());
ceph::async::dispatch(std::move(p), ec);
return;
}
int attr_ret = 0;
if (op.attrs.size() > 0) {
//TODO - fix yield_context
optional_yield y{null_yield};
attr_ret = op.priv_data->set_attrs(op.dpp, op.temp_key, op.attrs, y);
if (attr_ret < 0) {
ldpp_dout(op.dpp, 0) << "ERROR: AsyncWriteRequest::libaio_write_yield_cb::set_attrs: failed to set attrs, ret = " << attr_ret << dendl;
ec.assign(-ret, boost::system::system_category());
ceph::async::dispatch(std::move(p), ec);
return;
}
}
Partition partition_info = op.priv_data->get_current_partition_info(op.dpp);
efs::space_info space = efs::space(partition_info.location);
op.priv_data->set_free_space(op.dpp, space.available);
std::string new_path = partition_info.location + op.key;
std::string old_path = partition_info.location + op.temp_key;
ldpp_dout(op.dpp, 20) << "INFO: AsyncWriteRequest::libaio_write_yield_cb: temp_key: " << op.temp_key << dendl;
ret = rename(old_path.c_str(), new_path.c_str());
if (ret < 0) {
ret = errno;
ldpp_dout(op.dpp, 0) << "ERROR: put::rename: failed to rename file: " << ret << dendl;
ec.assign(-ret, boost::system::system_category());
}
ceph::async::dispatch(std::move(p), ec);
}
int SSDDriver::AsyncReadOp::prepare_libaio_read_op(const DoutPrefixProvider *dpp, 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 (dpp->get_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, const 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 = it.first;
std::string attr_val = it.second.to_str();
std::string old_attr_val;
auto ret = get_attr(dpp, key, attr_name, old_attr_val, y);
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) {
ldpp_dout(dpp, 0) << "SSDCache: " << __func__ << "(): could not modify attr value for attr name: " << attr_name << " key: " << key << " ERROR: " << cpp_strerror(errno) <<dendl;
return ret;
}
}
efs::space_info space = efs::space(partition_info.location);
this->free_space = space.available;
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 << cpp_strerror(errno) << dendl;
return ret;
}
}
efs::space_info space = efs::space(partition_info.location);
this->free_space = space.available;
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 = attr_name.find(ATTR_PREFIX);
buflen -= keylen;
keyptr += keylen;
if (prefixloc == std::string::npos) {
continue;
}
std::string attr_value;
get_attr(dpp, key, attr_name, attr_value, y);
bufferlist bl_value;
bl_value.append(attr_value);
attrs.emplace(std::move(attr_name), std::move(bl_value));
}
return 0;
}
int SSDDriver::set_attrs(const DoutPrefixProvider* dpp, const std::string& key, const 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.to_str(), y);
if (ret < 0) {
ldpp_dout(dpp, 0) << "SSDCache: " << __func__ << "(): could not set attr value for attr name: " << attr_name << " key: " << key << cpp_strerror(errno) << dendl;
return ret;
}
}
}
efs::space_info space = efs::space(partition_info.location);
this->free_space = space.available;
return 0;
}
int SSDDriver::get_attr(const DoutPrefixProvider* dpp, const std::string& key, const std::string& attr_name, 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__ << "(): get_attr: key: " << attr_name << dendl;
int attr_size = getxattr(location.c_str(), attr_name.c_str(), nullptr, 0);
if (attr_size < 0) {
ldpp_dout(dpp, 0) << "ERROR: could not get attribute " << attr_name << ": " << cpp_strerror(errno) << dendl;
attr_val = "";
return errno;
}
if (attr_size == 0) {
ldpp_dout(dpp, 0) << "ERROR: no attribute value found for attr_name: " << attr_name << dendl;
attr_val = "";
return 0;
}
attr_val.resize(attr_size);
attr_size = getxattr(location.c_str(), attr_name.c_str(), attr_val.data(), 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;
attr_val = "";
return errno;
}
return 0;
}
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;
auto ret = setxattr(location.c_str(), attr_name.c_str(), attr_val.c_str(), attr_val.size(), 0);
if (ret < 0) {
ldpp_dout(dpp, 0) << "SSDCache: " << __func__ << "(): could not set attr value for attr name: " << attr_name << " key: " << key << cpp_strerror(errno) << dendl;
return ret;
}
efs::space_info space = efs::space(partition_info.location);
this->free_space = space.available;
return 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;
auto ret = removexattr(location.c_str(), attr_name.c_str());
if (ret < 0) {
ldpp_dout(dpp, 0) << "SSDCache: " << __func__ << "(): could not remove attr value for attr name: " << attr_name << " key: " << key << cpp_strerror(errno) << dendl;
return ret;
}
efs::space_info space = efs::space(partition_info.location);
this->free_space = space.available;
return 0;
}
} } // namespace rgw::cache
|