]> git.proxmox.com Git - ceph.git/blame - ceph/src/cls/rgw/cls_rgw.cc
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / cls / rgw / cls_rgw.cc
CommitLineData
11fdf7f2 1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
7c673cae
FG
2// vim: ts=8 sw=2 smarttab
3
7c673cae
FG
4#include "include/types.h"
5
7c673cae
FG
6#include <errno.h>
7
7c673cae
FG
8#include "objclass/objclass.h"
9#include "cls/rgw/cls_rgw_ops.h"
10#include "cls/rgw/cls_rgw_const.h"
11#include "common/Clock.h"
12#include "common/strtol.h"
13#include "common/escape.h"
14
7c673cae
FG
15#include "include/compat.h"
16
17CLS_VER(1,0)
18CLS_NAME(rgw)
19
20
21#define BI_PREFIX_CHAR 0x80
22
23#define BI_BUCKET_OBJS_INDEX 0
24#define BI_BUCKET_LOG_INDEX 1
25#define BI_BUCKET_OBJ_INSTANCE_INDEX 2
26#define BI_BUCKET_OLH_DATA_INDEX 3
27
28#define BI_BUCKET_LAST_INDEX 4
29
11fdf7f2 30static std::string bucket_index_prefixes[] = { "", /* special handling for the objs list index */
7c673cae
FG
31 "0_", /* bucket log index */
32 "1000_", /* obj instance index */
33 "1001_", /* olh data index */
34
35 /* this must be the last index */
36 "9999_",};
37
38static bool bi_is_objs_index(const string& s) {
39 return ((unsigned char)s[0] != BI_PREFIX_CHAR);
40}
41
42int bi_entry_type(const string& s)
43{
44 if (bi_is_objs_index(s)) {
45 return BI_BUCKET_OBJS_INDEX;
46 }
47
48 for (size_t i = 1;
49 i < sizeof(bucket_index_prefixes) / sizeof(bucket_index_prefixes[0]);
50 ++i) {
51 const string& t = bucket_index_prefixes[i];
52
53 if (s.compare(1, t.size(), t) == 0) {
54 return i;
55 }
56 }
57
58 return -EINVAL;
59}
60
61static bool bi_entry_gt(const string& first, const string& second)
62{
63 int fi = bi_entry_type(first);
64 int si = bi_entry_type(second);
65
66 if (fi > si) {
67 return true;
68 } else if (fi < si) {
69 return false;
70 }
71
72 return first > second;
73}
74
75static void get_time_key(real_time& ut, string *key)
76{
77 char buf[32];
78 ceph_timespec ts = ceph::real_clock::to_ceph_timespec(ut);
79 snprintf(buf, 32, "%011llu.%09u", (unsigned long long)ts.tv_sec, (unsigned int)ts.tv_nsec);
80 *key = buf;
81}
82
83static void get_index_ver_key(cls_method_context_t hctx, uint64_t index_ver, string *key)
84{
85 char buf[48];
86 snprintf(buf, sizeof(buf), "%011llu.%llu.%d", (unsigned long long)index_ver,
87 (unsigned long long)cls_current_version(hctx),
88 cls_current_subop_num(hctx));
89 *key = buf;
90}
91
92static void bi_log_prefix(string& key)
93{
94 key = BI_PREFIX_CHAR;
95 key.append(bucket_index_prefixes[BI_BUCKET_LOG_INDEX]);
96}
97
98static void bi_log_index_key(cls_method_context_t hctx, string& key, string& id, uint64_t index_ver)
99{
100 bi_log_prefix(key);
101 get_index_ver_key(hctx, index_ver, &id);
102 key.append(id);
103}
104
105static int log_index_operation(cls_method_context_t hctx, cls_rgw_obj_key& obj_key, RGWModifyOp op,
106 string& tag, real_time& timestamp,
107 rgw_bucket_entry_ver& ver, RGWPendingState state, uint64_t index_ver,
31f18b77 108 string& max_marker, uint16_t bilog_flags, string *owner, string *owner_display_name, rgw_zone_set *zones_trace)
7c673cae
FG
109{
110 bufferlist bl;
111
11fdf7f2 112 rgw_bi_log_entry entry;
7c673cae
FG
113
114 entry.object = obj_key.name;
115 entry.instance = obj_key.instance;
116 entry.timestamp = timestamp;
117 entry.op = op;
118 entry.ver = ver;
119 entry.state = state;
120 entry.index_ver = index_ver;
121 entry.tag = tag;
122 entry.bilog_flags = bilog_flags;
123 if (owner) {
124 entry.owner = *owner;
125 }
126 if (owner_display_name) {
127 entry.owner_display_name = *owner_display_name;
128 }
31f18b77
FG
129 if (zones_trace) {
130 entry.zones_trace = std::move(*zones_trace);
131 }
7c673cae
FG
132
133 string key;
134 bi_log_index_key(hctx, key, entry.id, index_ver);
135
11fdf7f2 136 encode(entry, bl);
7c673cae
FG
137
138 if (entry.id > max_marker)
139 max_marker = entry.id;
140
141 return cls_cxx_map_set_val(hctx, key, &bl);
142}
143
144/*
145 * read list of objects, skips objects in the ugly namespace
146 */
147static int get_obj_vals(cls_method_context_t hctx, const string& start, const string& filter_prefix,
c07f9fc5 148 int num_entries, map<string, bufferlist> *pkeys, bool *pmore)
7c673cae 149{
c07f9fc5 150 int ret = cls_cxx_map_get_vals(hctx, start, filter_prefix, num_entries, pkeys, pmore);
7c673cae
FG
151 if (ret < 0)
152 return ret;
153
154 if (pkeys->empty())
155 return 0;
156
1adf2230 157 auto last_element = pkeys->rbegin();
7c673cae
FG
158 if ((unsigned char)last_element->first[0] < BI_PREFIX_CHAR) {
159 /* nothing to see here, move along */
160 return 0;
161 }
162
1adf2230 163 auto first_element = pkeys->begin();
7c673cae
FG
164 if ((unsigned char)first_element->first[0] > BI_PREFIX_CHAR) {
165 return 0;
166 }
167
168 /* let's rebuild the list, only keep entries we're interested in */
1adf2230
AA
169 auto comp = [](const pair<string, bufferlist>& l, const string &r) { return l.first < r; };
170 string new_start = {static_cast<char>(BI_PREFIX_CHAR + 1)};
7c673cae 171
1adf2230
AA
172 auto lower = pkeys->lower_bound(string{static_cast<char>(BI_PREFIX_CHAR)});
173 auto upper = std::lower_bound(lower, pkeys->end(), new_start, comp);
174 pkeys->erase(lower, upper);
7c673cae 175
81eedcae 176 if (num_entries == (int)pkeys->size() || !(*pmore))
7c673cae
FG
177 return 0;
178
81eedcae
TL
179 if (pkeys->size() && new_start < pkeys->rbegin()->first) {
180 new_start = pkeys->rbegin()->first;
181 }
182
7c673cae 183 map<string, bufferlist> new_keys;
7c673cae
FG
184
185 /* now get some more keys */
c07f9fc5 186 ret = cls_cxx_map_get_vals(hctx, new_start, filter_prefix, num_entries - pkeys->size(), &new_keys, pmore);
7c673cae
FG
187 if (ret < 0)
188 return ret;
189
1adf2230
AA
190 pkeys->insert(std::make_move_iterator(new_keys.begin()),
191 std::make_move_iterator(new_keys.end()));
7c673cae
FG
192 return 0;
193}
194
195/*
196 * get a monotonically decreasing string representation.
197 * For num = x, num = y, where x > y, str(x) < str(y)
198 * Another property is that string size starts short and grows as num increases
199 */
200static void decreasing_str(uint64_t num, string *str)
201{
202 char buf[32];
203 if (num < 0x10) { /* 16 */
204 snprintf(buf, sizeof(buf), "9%02lld", 15 - (long long)num);
205 } else if (num < 0x100) { /* 256 */
206 snprintf(buf, sizeof(buf), "8%03lld", 255 - (long long)num);
207 } else if (num < 0x1000) /* 4096 */ {
208 snprintf(buf, sizeof(buf), "7%04lld", 4095 - (long long)num);
209 } else if (num < 0x10000) /* 65536 */ {
210 snprintf(buf, sizeof(buf), "6%05lld", 65535 - (long long)num);
211 } else if (num < 0x100000000) /* 4G */ {
212 snprintf(buf, sizeof(buf), "5%010lld", 0xFFFFFFFF - (long long)num);
213 } else {
214 snprintf(buf, sizeof(buf), "4%020lld", (long long)-num);
215 }
216
217 *str = buf;
218}
219
220/*
221 * we now hold two different indexes for objects. The first one holds the list of objects in the
222 * order that we want them to be listed. The second one only holds the objects instances (for
223 * versioned objects), and they're not arranged in any particular order.
224 * When listing objects we'll use the first index, when doing operations on the objects themselves
225 * we'll use the second index. Note that regular objects only map to the first index anyway
226 */
227
11fdf7f2 228static void get_list_index_key(rgw_bucket_dir_entry& entry, string *index_key)
7c673cae
FG
229{
230 *index_key = entry.key.name;
231
232 string ver_str;
233 decreasing_str(entry.versioned_epoch, &ver_str);
234 string instance_delim("\0i", 2);
235 string ver_delim("\0v", 2);
236
237 index_key->append(ver_delim);
238 index_key->append(ver_str);
239 index_key->append(instance_delim);
240 index_key->append(entry.key.instance);
241}
242
243static void encode_obj_versioned_data_key(const cls_rgw_obj_key& key, string *index_key, bool append_delete_marker_suffix = false)
244{
245 *index_key = BI_PREFIX_CHAR;
246 index_key->append(bucket_index_prefixes[BI_BUCKET_OBJ_INSTANCE_INDEX]);
247 index_key->append(key.name);
248 string delim("\0i", 2);
249 index_key->append(delim);
250 index_key->append(key.instance);
251 if (append_delete_marker_suffix) {
252 string dm("\0d", 2);
253 index_key->append(dm);
254 }
255}
256
257static void encode_obj_index_key(const cls_rgw_obj_key& key, string *index_key)
258{
259 if (key.instance.empty()) {
260 *index_key = key.name;
261 } else {
262 encode_obj_versioned_data_key(key, index_key);
263 }
264}
265
266static void encode_olh_data_key(const cls_rgw_obj_key& key, string *index_key)
267{
268 *index_key = BI_PREFIX_CHAR;
269 index_key->append(bucket_index_prefixes[BI_BUCKET_OLH_DATA_INDEX]);
270 index_key->append(key.name);
271}
272
273template <class T>
274static int read_index_entry(cls_method_context_t hctx, string& name, T *entry);
275
276static int encode_list_index_key(cls_method_context_t hctx, const cls_rgw_obj_key& key, string *index_key)
277{
278 if (key.instance.empty()) {
279 *index_key = key.name;
280 return 0;
281 }
282
283 string obj_index_key;
92f5a8d4
TL
284 cls_rgw_obj_key tmp_key(key);
285 if (tmp_key.instance == "null") {
286 tmp_key.instance.clear();
287 }
288 encode_obj_versioned_data_key(tmp_key, &obj_index_key);
7c673cae
FG
289
290 rgw_bucket_dir_entry entry;
291
292 int ret = read_index_entry(hctx, obj_index_key, &entry);
293 if (ret == -ENOENT) {
294 /* couldn't find the entry, set key value after the current object */
295 char buf[2] = { 0x1, 0 };
296 string s(buf);
297 *index_key = key.name + s;
298 return 0;
299 }
300 if (ret < 0) {
301 CLS_LOG(1, "ERROR: encode_list_index_key(): cls_cxx_map_get_val returned %d\n", ret);
302 return ret;
303 }
304
305 get_list_index_key(entry, index_key);
306
307 return 0;
308}
309
310static void split_key(const string& key, list<string>& vals)
311{
312 size_t pos = 0;
313 const char *p = key.c_str();
314 while (pos < key.size()) {
315 size_t len = strlen(p);
316 vals.push_back(p);
317 pos += len + 1;
318 p += len + 1;
319 }
320}
321
91327a77
AA
322static string escape_str(const string& s)
323{
11fdf7f2
TL
324 int len = escape_json_attr_len(s.c_str(), s.size());
325 std::string escaped(len, 0);
326 escape_json_attr(s.c_str(), s.size(), escaped.data());
327 return escaped;
91327a77
AA
328}
329
7c673cae
FG
330/*
331 * list index key structure:
332 *
333 * <obj name>\0[v<ver>\0i<instance id>]
334 */
91327a77 335static int decode_list_index_key(const string& index_key, cls_rgw_obj_key *key, uint64_t *ver)
7c673cae
FG
336{
337 size_t len = strlen(index_key.c_str());
338
339 key->instance.clear();
340 *ver = 0;
341
342 if (len == index_key.size()) {
343 key->name = index_key;
91327a77 344 return 0;
7c673cae
FG
345 }
346
347 list<string> vals;
348 split_key(index_key, vals);
349
91327a77
AA
350 if (vals.empty()) {
351 CLS_LOG(0, "ERROR: %s(): bad index_key (%s): split_key() returned empty vals", __func__, escape_str(index_key).c_str());
352 return -EIO;
353 }
7c673cae
FG
354
355 list<string>::iterator iter = vals.begin();
356 key->name = *iter;
357 ++iter;
358
91327a77
AA
359 if (iter == vals.end()) {
360 CLS_LOG(0, "ERROR: %s(): bad index_key (%s): no vals", __func__, escape_str(index_key).c_str());
361 return -EIO;
362 }
7c673cae
FG
363
364 for (; iter != vals.end(); ++iter) {
365 string& val = *iter;
366 if (val[0] == 'i') {
367 key->instance = val.substr(1);
368 } else if (val[0] == 'v') {
369 string err;
370 const char *s = val.c_str() + 1;
371 *ver = strict_strtoll(s, 10, &err);
91327a77
AA
372 if (!err.empty()) {
373 CLS_LOG(0, "ERROR: %s(): bad index_key (%s): could not parse val (v=%s)", __func__, escape_str(index_key).c_str(), s);
374 return -EIO;
375 }
7c673cae
FG
376 }
377 }
91327a77
AA
378
379 return 0;
7c673cae
FG
380}
381
11fdf7f2
TL
382static int read_bucket_header(cls_method_context_t hctx,
383 rgw_bucket_dir_header *header)
7c673cae
FG
384{
385 bufferlist bl;
386 int rc = cls_cxx_map_read_header(hctx, &bl);
387 if (rc < 0)
388 return rc;
31f18b77
FG
389
390 if (bl.length() == 0) {
391 *header = rgw_bucket_dir_header();
392 return 0;
393 }
11fdf7f2 394 auto iter = bl.cbegin();
7c673cae 395 try {
11fdf7f2 396 decode(*header, iter);
7c673cae
FG
397 } catch (buffer::error& err) {
398 CLS_LOG(1, "ERROR: read_bucket_header(): failed to decode header\n");
399 return -EIO;
400 }
401
402 return 0;
403}
404
405int rgw_bucket_list(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
406{
11fdf7f2 407 auto iter = in->cbegin();
7c673cae 408
11fdf7f2 409 rgw_cls_list_op op;
7c673cae 410 try {
11fdf7f2 411 decode(op, iter);
7c673cae
FG
412 } catch (buffer::error& err) {
413 CLS_LOG(1, "ERROR: rgw_bucket_list(): failed to decode request\n");
414 return -EINVAL;
415 }
416
11fdf7f2
TL
417 rgw_cls_list_ret ret;
418 rgw_bucket_dir& new_dir = ret.dir;
7c673cae
FG
419 int rc = read_bucket_header(hctx, &new_dir.header);
420 if (rc < 0) {
421 CLS_LOG(1, "ERROR: rgw_bucket_list(): failed to read header\n");
422 return rc;
423 }
424
425 map<string, bufferlist> keys;
426 std::map<string, bufferlist>::iterator kiter;
427 string start_key;
428 encode_list_index_key(hctx, op.start_obj, &start_key);
429 bool done = false;
c07f9fc5
FG
430 uint32_t left_to_read = op.num_entries;
431 bool more;
7c673cae
FG
432
433 do {
c07f9fc5 434 rc = get_obj_vals(hctx, start_key, op.filter_prefix, left_to_read, &keys, &more);
7c673cae
FG
435 if (rc < 0)
436 return rc;
437
11fdf7f2 438 std::map<string, rgw_bucket_dir_entry>& m = new_dir.m;
7c673cae
FG
439
440 done = keys.empty();
441
442 for (kiter = keys.begin(); kiter != keys.end(); ++kiter) {
11fdf7f2 443 rgw_bucket_dir_entry entry;
7c673cae
FG
444
445 if (!bi_is_objs_index(kiter->first)) {
446 done = true;
447 break;
448 }
449
450 bufferlist& entrybl = kiter->second;
11fdf7f2 451 auto eiter = entrybl.cbegin();
7c673cae 452 try {
11fdf7f2 453 decode(entry, eiter);
7c673cae
FG
454 } catch (buffer::error& err) {
455 CLS_LOG(1, "ERROR: rgw_bucket_list(): failed to decode entry, key=%s\n", kiter->first.c_str());
456 return -EINVAL;
457 }
458
459 cls_rgw_obj_key key;
460 uint64_t ver;
7c673cae
FG
461
462 start_key = kiter->first;
463 CLS_LOG(20, "start_key=%s len=%zu", start_key.c_str(), start_key.size());
464
91327a77
AA
465 int ret = decode_list_index_key(kiter->first, &key, &ver);
466 if (ret < 0) {
467 CLS_LOG(0, "ERROR: failed to decode list index key (%s)\n", escape_str(kiter->first).c_str());
468 continue;
469 }
470
7c673cae
FG
471 if (!entry.is_valid()) {
472 CLS_LOG(20, "entry %s[%s] is not valid\n", key.name.c_str(), key.instance.c_str());
473 continue;
474 }
3efd9988
FG
475
476 // filter out noncurrent versions, delete markers, and initial marker
477 if (!op.list_versions && (!entry.is_visible() || op.start_obj.name == key.name)) {
7c673cae
FG
478 CLS_LOG(20, "entry %s[%s] is not visible\n", key.name.c_str(), key.instance.c_str());
479 continue;
480 }
481 if (m.size() < op.num_entries) {
482 m[kiter->first] = entry;
483 }
484 left_to_read--;
485
486 CLS_LOG(20, "got entry %s[%s] m.size()=%d\n", key.name.c_str(), key.instance.c_str(), (int)m.size());
487 }
488 } while (left_to_read > 0 && !done);
489
c07f9fc5 490 ret.is_truncated = more && !done;
7c673cae 491
11fdf7f2 492 encode(ret, *out);
7c673cae
FG
493 return 0;
494}
495
11fdf7f2
TL
496static int check_index(cls_method_context_t hctx,
497 rgw_bucket_dir_header *existing_header,
498 rgw_bucket_dir_header *calc_header)
7c673cae
FG
499{
500 int rc = read_bucket_header(hctx, existing_header);
501 if (rc < 0) {
502 CLS_LOG(1, "ERROR: check_index(): failed to read header\n");
503 return rc;
504 }
505
506 calc_header->tag_timeout = existing_header->tag_timeout;
507 calc_header->ver = existing_header->ver;
508
509 map<string, bufferlist> keys;
510 string start_obj;
511 string filter_prefix;
512
513#define CHECK_CHUNK_SIZE 1000
514 bool done = false;
c07f9fc5 515 bool more;
7c673cae
FG
516
517 do {
c07f9fc5 518 rc = get_obj_vals(hctx, start_obj, filter_prefix, CHECK_CHUNK_SIZE, &keys, &more);
7c673cae
FG
519 if (rc < 0)
520 return rc;
521
522 std::map<string, bufferlist>::iterator kiter = keys.begin();
523 for (; kiter != keys.end(); ++kiter) {
524 if (!bi_is_objs_index(kiter->first)) {
525 done = true;
526 break;
527 }
528
11fdf7f2
TL
529 rgw_bucket_dir_entry entry;
530 auto eiter = kiter->second.cbegin();
7c673cae 531 try {
11fdf7f2 532 decode(entry, eiter);
7c673cae
FG
533 } catch (buffer::error& err) {
534 CLS_LOG(1, "ERROR: rgw_bucket_list(): failed to decode entry, key=%s\n", kiter->first.c_str());
535 return -EIO;
536 }
11fdf7f2 537 rgw_bucket_category_stats& stats = calc_header->stats[entry.meta.category];
7c673cae
FG
538 stats.num_entries++;
539 stats.total_size += entry.meta.accounted_size;
540 stats.total_size_rounded += cls_rgw_get_rounded_size(entry.meta.accounted_size);
541 stats.actual_size += entry.meta.size;
542
543 start_obj = kiter->first;
544 }
545 } while (keys.size() == CHECK_CHUNK_SIZE && !done);
546
547 return 0;
548}
549
550int rgw_bucket_check_index(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
551{
11fdf7f2 552 rgw_cls_check_index_ret ret;
7c673cae
FG
553
554 int rc = check_index(hctx, &ret.existing_header, &ret.calculated_header);
555 if (rc < 0)
556 return rc;
557
11fdf7f2 558 encode(ret, *out);
7c673cae
FG
559
560 return 0;
561}
562
11fdf7f2 563static int write_bucket_header(cls_method_context_t hctx, rgw_bucket_dir_header *header)
7c673cae
FG
564{
565 header->ver++;
566
567 bufferlist header_bl;
11fdf7f2 568 encode(*header, header_bl);
7c673cae
FG
569 return cls_cxx_map_write_header(hctx, &header_bl);
570}
571
572
573int rgw_bucket_rebuild_index(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
574{
11fdf7f2
TL
575 rgw_bucket_dir_header existing_header;
576 rgw_bucket_dir_header calc_header;
7c673cae
FG
577 int rc = check_index(hctx, &existing_header, &calc_header);
578 if (rc < 0)
579 return rc;
580
581 return write_bucket_header(hctx, &calc_header);
582}
583
584int rgw_bucket_update_stats(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
585{
586 // decode request
587 rgw_cls_bucket_update_stats_op op;
11fdf7f2 588 auto iter = in->cbegin();
7c673cae 589 try {
11fdf7f2 590 decode(op, iter);
7c673cae
FG
591 } catch (buffer::error& err) {
592 CLS_LOG(1, "ERROR: %s(): failed to decode request\n", __func__);
593 return -EINVAL;
594 }
595
11fdf7f2 596 rgw_bucket_dir_header header;
7c673cae
FG
597 int rc = read_bucket_header(hctx, &header);
598 if (rc < 0) {
599 CLS_LOG(1, "ERROR: %s(): failed to read header\n", __func__);
600 return rc;
601 }
602
603 for (auto& s : op.stats) {
604 auto& dest = header.stats[s.first];
605 if (op.absolute) {
606 dest = s.second;
607 } else {
608 dest.total_size += s.second.total_size;
609 dest.total_size_rounded += s.second.total_size_rounded;
610 dest.num_entries += s.second.num_entries;
91327a77 611 dest.actual_size += s.second.actual_size;
7c673cae
FG
612 }
613 }
614
615 return write_bucket_header(hctx, &header);
616}
617
618int rgw_bucket_init_index(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
619{
7c673cae
FG
620 bufferlist header_bl;
621 int rc = cls_cxx_map_read_header(hctx, &header_bl);
622 if (rc < 0) {
623 switch (rc) {
624 case -ENODATA:
625 case -ENOENT:
626 break;
627 default:
628 return rc;
629 }
630 }
631
632 if (header_bl.length() != 0) {
633 CLS_LOG(1, "ERROR: index already initialized\n");
634 return -EINVAL;
635 }
636
637 rgw_bucket_dir dir;
638
639 return write_bucket_header(hctx, &dir.header);
640}
641
642int rgw_bucket_set_tag_timeout(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
643{
644 // decode request
645 rgw_cls_tag_timeout_op op;
11fdf7f2 646 auto iter = in->cbegin();
7c673cae 647 try {
11fdf7f2 648 decode(op, iter);
7c673cae
FG
649 } catch (buffer::error& err) {
650 CLS_LOG(1, "ERROR: rgw_bucket_set_tag_timeout(): failed to decode request\n");
651 return -EINVAL;
652 }
653
11fdf7f2 654 rgw_bucket_dir_header header;
7c673cae
FG
655 int rc = read_bucket_header(hctx, &header);
656 if (rc < 0) {
657 CLS_LOG(1, "ERROR: rgw_bucket_set_tag_timeout(): failed to read header\n");
658 return rc;
659 }
660
661 header.tag_timeout = op.tag_timeout;
662
663 return write_bucket_header(hctx, &header);
664}
665
11fdf7f2
TL
666static int read_key_entry(cls_method_context_t hctx, cls_rgw_obj_key& key,
667 string *idx, rgw_bucket_dir_entry *entry,
7c673cae
FG
668 bool special_delete_marker_name = false);
669
670int rgw_bucket_prepare_op(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
671{
672 // decode request
673 rgw_cls_obj_prepare_op op;
11fdf7f2 674 auto iter = in->cbegin();
7c673cae 675 try {
11fdf7f2 676 decode(op, iter);
7c673cae
FG
677 } catch (buffer::error& err) {
678 CLS_LOG(1, "ERROR: rgw_bucket_prepare_op(): failed to decode request\n");
679 return -EINVAL;
680 }
681
682 if (op.tag.empty()) {
683 CLS_LOG(1, "ERROR: tag is empty\n");
684 return -EINVAL;
685 }
686
687 CLS_LOG(1, "rgw_bucket_prepare_op(): request: op=%d name=%s instance=%s tag=%s\n",
688 op.op, op.key.name.c_str(), op.key.instance.c_str(), op.tag.c_str());
689
690 // get on-disk state
691 string idx;
692
11fdf7f2 693 rgw_bucket_dir_entry entry;
7c673cae
FG
694 int rc = read_key_entry(hctx, op.key, &idx, &entry);
695 if (rc < 0 && rc != -ENOENT)
696 return rc;
697
698 bool noent = (rc == -ENOENT);
699
700 rc = 0;
701
702 if (noent) { // no entry, initialize fields
703 entry.key = op.key;
704 entry.ver = rgw_bucket_entry_ver();
705 entry.exists = false;
706 entry.locator = op.locator;
707 }
708
709 // fill in proper state
11fdf7f2 710 rgw_bucket_pending_info info;
7c673cae
FG
711 info.timestamp = real_clock::now();
712 info.state = CLS_RGW_STATE_PENDING_MODIFY;
713 info.op = op.op;
714 entry.pending_map.insert(pair<string, rgw_bucket_pending_info>(op.tag, info));
715
11fdf7f2 716 rgw_bucket_dir_header header;
7c673cae
FG
717 rc = read_bucket_header(hctx, &header);
718 if (rc < 0) {
719 CLS_LOG(1, "ERROR: rgw_bucket_prepare_op(): failed to read header\n");
720 return rc;
721 }
722
c07f9fc5 723 if (op.log_op && !header.syncstopped) {
7c673cae 724 rc = log_index_operation(hctx, op.key, op.op, op.tag, entry.meta.mtime,
31f18b77 725 entry.ver, info.state, header.ver, header.max_marker, op.bilog_flags, NULL, NULL, &op.zones_trace);
7c673cae
FG
726 if (rc < 0)
727 return rc;
728 }
729
730 // write out new key to disk
731 bufferlist info_bl;
11fdf7f2 732 encode(entry, info_bl);
7c673cae
FG
733 rc = cls_cxx_map_set_val(hctx, idx, &info_bl);
734 if (rc < 0)
735 return rc;
736
11fdf7f2
TL
737 if (op.log_op && !header.syncstopped)
738 return write_bucket_header(hctx, &header);
739 return 0;
7c673cae
FG
740}
741
11fdf7f2
TL
742static void unaccount_entry(rgw_bucket_dir_header& header,
743 rgw_bucket_dir_entry& entry)
7c673cae 744{
11fdf7f2 745 rgw_bucket_category_stats& stats = header.stats[entry.meta.category];
7c673cae
FG
746 stats.num_entries--;
747 stats.total_size -= entry.meta.accounted_size;
748 stats.total_size_rounded -= cls_rgw_get_rounded_size(entry.meta.accounted_size);
749 stats.actual_size -= entry.meta.size;
750}
751
11fdf7f2 752static void log_entry(const char *func, const char *str, rgw_bucket_dir_entry *entry)
7c673cae
FG
753{
754 CLS_LOG(1, "%s(): %s: ver=%ld:%llu name=%s instance=%s locator=%s\n", func, str,
755 (long)entry->ver.pool, (unsigned long long)entry->ver.epoch,
756 entry->key.name.c_str(), entry->key.instance.c_str(), entry->locator.c_str());
757}
758
11fdf7f2 759static void log_entry(const char *func, const char *str, rgw_bucket_olh_entry *entry)
7c673cae
FG
760{
761 CLS_LOG(1, "%s(): %s: epoch=%llu name=%s instance=%s tag=%s\n", func, str,
762 (unsigned long long)entry->epoch, entry->key.name.c_str(), entry->key.instance.c_str(),
763 entry->tag.c_str());
764}
765
766template <class T>
11fdf7f2
TL
767static int read_omap_entry(cls_method_context_t hctx, const std::string& name,
768 T* entry)
7c673cae
FG
769{
770 bufferlist current_entry;
771 int rc = cls_cxx_map_get_val(hctx, name, &current_entry);
772 if (rc < 0) {
773 return rc;
774 }
775
11fdf7f2 776 auto cur_iter = current_entry.cbegin();
7c673cae 777 try {
11fdf7f2 778 decode(*entry, cur_iter);
7c673cae 779 } catch (buffer::error& err) {
11fdf7f2 780 CLS_LOG(1, "ERROR: %s(): failed to decode entry\n", __func__);
7c673cae
FG
781 return -EIO;
782 }
11fdf7f2
TL
783 return 0;
784}
785
786template <class T>
787static int read_index_entry(cls_method_context_t hctx, string& name, T* entry)
788{
789 int ret = read_omap_entry(hctx, name, entry);
790 if (ret < 0) {
791 return ret;
792 }
7c673cae
FG
793
794 log_entry(__func__, "existing entry", entry);
795 return 0;
796}
797
11fdf7f2
TL
798static int read_key_entry(cls_method_context_t hctx, cls_rgw_obj_key& key,
799 string *idx, rgw_bucket_dir_entry *entry,
7c673cae
FG
800 bool special_delete_marker_name)
801{
802 encode_obj_index_key(key, idx);
803 int rc = read_index_entry(hctx, *idx, entry);
804 if (rc < 0) {
805 return rc;
806 }
807
808 if (key.instance.empty() &&
809 entry->flags & RGW_BUCKET_DIRENT_FLAG_VER_MARKER) {
810 /* we only do it where key.instance is empty. In this case the delete marker will have a
811 * separate entry in the index to avoid collisions with the actual object, as it's mutable
812 */
813 if (special_delete_marker_name) {
814 encode_obj_versioned_data_key(key, idx, true);
815 rc = read_index_entry(hctx, *idx, entry);
816 if (rc == 0) {
817 return 0;
818 }
819 }
820 encode_obj_versioned_data_key(key, idx);
821 rc = read_index_entry(hctx, *idx, entry);
822 if (rc < 0) {
823 *entry = rgw_bucket_dir_entry(); /* need to reset entry because we initialized it earlier */
824 return rc;
825 }
826 }
827
828 return 0;
829}
830
831int rgw_bucket_complete_op(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
832{
833 // decode request
834 rgw_cls_obj_complete_op op;
11fdf7f2 835 auto iter = in->cbegin();
7c673cae 836 try {
11fdf7f2 837 decode(op, iter);
7c673cae
FG
838 } catch (buffer::error& err) {
839 CLS_LOG(1, "ERROR: rgw_bucket_complete_op(): failed to decode request\n");
840 return -EINVAL;
841 }
842 CLS_LOG(1, "rgw_bucket_complete_op(): request: op=%d name=%s instance=%s ver=%lu:%llu tag=%s\n",
843 op.op, op.key.name.c_str(), op.key.instance.c_str(),
844 (unsigned long)op.ver.pool, (unsigned long long)op.ver.epoch,
845 op.tag.c_str());
846
11fdf7f2 847 rgw_bucket_dir_header header;
7c673cae
FG
848 int rc = read_bucket_header(hctx, &header);
849 if (rc < 0) {
850 CLS_LOG(1, "ERROR: rgw_bucket_complete_op(): failed to read header\n");
851 return -EINVAL;
852 }
853
11fdf7f2 854 rgw_bucket_dir_entry entry;
7c673cae
FG
855 bool ondisk = true;
856
857 string idx;
858 rc = read_key_entry(hctx, op.key, &idx, &entry);
859 if (rc == -ENOENT) {
860 entry.key = op.key;
861 entry.ver = op.ver;
862 entry.meta = op.meta;
863 entry.locator = op.locator;
864 ondisk = false;
865 } else if (rc < 0) {
866 return rc;
867 }
868
869 entry.index_ver = header.ver;
870 entry.flags = (entry.key.instance.empty() ? 0 : RGW_BUCKET_DIRENT_FLAG_VER); /* resetting entry flags, entry might have been previously a delete marker */
871
872 if (op.tag.size()) {
11fdf7f2 873 map<string, rgw_bucket_pending_info>::iterator pinter = entry.pending_map.find(op.tag);
7c673cae
FG
874 if (pinter == entry.pending_map.end()) {
875 CLS_LOG(1, "ERROR: couldn't find tag for pending operation\n");
876 return -EINVAL;
877 }
878 entry.pending_map.erase(pinter);
879 }
880
881 bool cancel = false;
882 bufferlist update_bl;
883
884 if (op.tag.size() && op.op == CLS_RGW_OP_CANCEL) {
885 CLS_LOG(1, "rgw_bucket_complete_op(): cancel requested\n");
886 cancel = true;
887 } else if (op.ver.pool == entry.ver.pool &&
888 op.ver.epoch && op.ver.epoch <= entry.ver.epoch) {
889 CLS_LOG(1, "rgw_bucket_complete_op(): skipping request, old epoch\n");
890 cancel = true;
891 }
892
893 bufferlist op_bl;
894 if (cancel) {
c07f9fc5 895 if (op.log_op && !header.syncstopped) {
7c673cae 896 rc = log_index_operation(hctx, op.key, op.op, op.tag, entry.meta.mtime, entry.ver,
31f18b77 897 CLS_RGW_STATE_COMPLETE, header.ver, header.max_marker, op.bilog_flags, NULL, NULL, &op.zones_trace);
7c673cae
FG
898 if (rc < 0)
899 return rc;
900 }
901
902 if (op.tag.size()) {
903 bufferlist new_key_bl;
11fdf7f2
TL
904 encode(entry, new_key_bl);
905 rc = cls_cxx_map_set_val(hctx, idx, &new_key_bl);
906 if (rc < 0)
907 return rc;
908 }
909
910 if (op.log_op && !header.syncstopped) {
911 return write_bucket_header(hctx, &header);
7c673cae 912 }
11fdf7f2 913 return 0;
7c673cae
FG
914 }
915
916 if (entry.exists) {
917 unaccount_entry(header, entry);
918 }
919
920 entry.ver = op.ver;
921 switch ((int)op.op) {
922 case CLS_RGW_OP_DEL:
923 entry.meta = op.meta;
924 if (ondisk) {
925 if (!entry.pending_map.size()) {
926 int ret = cls_cxx_map_remove_key(hctx, idx);
927 if (ret < 0)
928 return ret;
929 } else {
930 entry.exists = false;
931 bufferlist new_key_bl;
11fdf7f2 932 encode(entry, new_key_bl);
7c673cae
FG
933 int ret = cls_cxx_map_set_val(hctx, idx, &new_key_bl);
934 if (ret < 0)
935 return ret;
936 }
937 } else {
938 return -ENOENT;
939 }
940 break;
941 case CLS_RGW_OP_ADD:
942 {
11fdf7f2
TL
943 rgw_bucket_dir_entry_meta& meta = op.meta;
944 rgw_bucket_category_stats& stats = header.stats[meta.category];
7c673cae
FG
945 entry.meta = meta;
946 entry.key = op.key;
947 entry.exists = true;
948 entry.tag = op.tag;
949 stats.num_entries++;
950 stats.total_size += meta.accounted_size;
951 stats.total_size_rounded += cls_rgw_get_rounded_size(meta.accounted_size);
952 stats.actual_size += meta.size;
953 bufferlist new_key_bl;
11fdf7f2 954 encode(entry, new_key_bl);
7c673cae
FG
955 int ret = cls_cxx_map_set_val(hctx, idx, &new_key_bl);
956 if (ret < 0)
957 return ret;
958 }
959 break;
960 }
961
c07f9fc5 962 if (op.log_op && !header.syncstopped) {
7c673cae 963 rc = log_index_operation(hctx, op.key, op.op, op.tag, entry.meta.mtime, entry.ver,
31f18b77 964 CLS_RGW_STATE_COMPLETE, header.ver, header.max_marker, op.bilog_flags, NULL, NULL, &op.zones_trace);
7c673cae
FG
965 if (rc < 0)
966 return rc;
967 }
968
969 list<cls_rgw_obj_key>::iterator remove_iter;
970 CLS_LOG(20, "rgw_bucket_complete_op(): remove_objs.size()=%d\n", (int)op.remove_objs.size());
971 for (remove_iter = op.remove_objs.begin(); remove_iter != op.remove_objs.end(); ++remove_iter) {
972 cls_rgw_obj_key& remove_key = *remove_iter;
973 CLS_LOG(1, "rgw_bucket_complete_op(): removing entries, read_index_entry name=%s instance=%s\n",
974 remove_key.name.c_str(), remove_key.instance.c_str());
11fdf7f2 975 rgw_bucket_dir_entry remove_entry;
7c673cae
FG
976 string k;
977 int ret = read_key_entry(hctx, remove_key, &k, &remove_entry);
978 if (ret < 0) {
979 CLS_LOG(1, "rgw_bucket_complete_op(): removing entries, read_index_entry name=%s instance=%s ret=%d\n",
980 remove_key.name.c_str(), remove_key.instance.c_str(), ret);
981 continue;
982 }
11fdf7f2
TL
983 CLS_LOG(0,
984 "rgw_bucket_complete_op(): entry.name=%s entry.instance=%s entry.meta.category=%d\n",
985 remove_entry.key.name.c_str(),
986 remove_entry.key.instance.c_str(),
987 int(remove_entry.meta.category));
7c673cae
FG
988 unaccount_entry(header, remove_entry);
989
c07f9fc5 990 if (op.log_op && !header.syncstopped) {
3efd9988 991 ++header.ver; // increment index version, or we'll overwrite keys previously written
7c673cae 992 rc = log_index_operation(hctx, remove_key, CLS_RGW_OP_DEL, op.tag, remove_entry.meta.mtime,
31f18b77 993 remove_entry.ver, CLS_RGW_STATE_COMPLETE, header.ver, header.max_marker, op.bilog_flags, NULL, NULL, &op.zones_trace);
7c673cae
FG
994 if (rc < 0)
995 continue;
996 }
997
998 ret = cls_cxx_map_remove_key(hctx, k);
999 if (ret < 0) {
1000 CLS_LOG(1, "rgw_bucket_complete_op(): cls_cxx_map_remove_key, failed to remove entry, name=%s instance=%s read_index_entry ret=%d\n", remove_key.name.c_str(), remove_key.instance.c_str(), rc);
1001 continue;
1002 }
1003 }
1004
1005 return write_bucket_header(hctx, &header);
1006}
1007
1008template <class T>
1009static int write_entry(cls_method_context_t hctx, T& entry, const string& key)
1010{
1011 bufferlist bl;
11fdf7f2 1012 encode(entry, bl);
7c673cae
FG
1013 return cls_cxx_map_set_val(hctx, key, &bl);
1014}
1015
11fdf7f2 1016static int read_olh(cls_method_context_t hctx,cls_rgw_obj_key& obj_key, rgw_bucket_olh_entry *olh_data_entry, string *index_key, bool *found)
7c673cae
FG
1017{
1018 cls_rgw_obj_key olh_key;
1019 olh_key.name = obj_key.name;
1020
1021 encode_olh_data_key(olh_key, index_key);
1022 int ret = read_index_entry(hctx, *index_key, olh_data_entry);
1023 if (ret < 0 && ret != -ENOENT) {
1024 CLS_LOG(0, "ERROR: read_index_entry() olh_key=%s ret=%d", olh_key.name.c_str(), ret);
1025 return ret;
1026 }
1027 if (found) {
1028 *found = (ret != -ENOENT);
1029 }
1030 return 0;
1031}
1032
11fdf7f2 1033static void update_olh_log(rgw_bucket_olh_entry& olh_data_entry, OLHLogOp op, const string& op_tag,
7c673cae
FG
1034 cls_rgw_obj_key& key, bool delete_marker, uint64_t epoch)
1035{
1036 vector<rgw_bucket_olh_log_entry>& log = olh_data_entry.pending_log[olh_data_entry.epoch];
1037 rgw_bucket_olh_log_entry log_entry;
1038 log_entry.epoch = epoch;
1039 log_entry.op = op;
1040 log_entry.op_tag = op_tag;
1041 log_entry.key = key;
1042 log_entry.delete_marker = delete_marker;
1043 log.push_back(log_entry);
1044}
1045
11fdf7f2 1046static int write_obj_instance_entry(cls_method_context_t hctx, rgw_bucket_dir_entry& instance_entry, const string& instance_idx)
7c673cae
FG
1047{
1048 CLS_LOG(20, "write_entry() instance=%s idx=%s flags=%d", escape_str(instance_entry.key.instance).c_str(), instance_idx.c_str(), instance_entry.flags);
1049 /* write the instance entry */
1050 int ret = write_entry(hctx, instance_entry, instance_idx);
1051 if (ret < 0) {
1052 CLS_LOG(0, "ERROR: write_entry() instance_key=%s ret=%d", escape_str(instance_idx).c_str(), ret);
1053 return ret;
1054 }
1055 return 0;
1056}
1057
1058/*
1059 * write object instance entry, and if needed also the list entry
1060 */
11fdf7f2 1061static int write_obj_entries(cls_method_context_t hctx, rgw_bucket_dir_entry& instance_entry, const string& instance_idx)
7c673cae
FG
1062{
1063 int ret = write_obj_instance_entry(hctx, instance_entry, instance_idx);
1064 if (ret < 0) {
1065 return ret;
1066 }
1067 string instance_list_idx;
1068 get_list_index_key(instance_entry, &instance_list_idx);
1069
1070 if (instance_idx != instance_list_idx) {
1071 CLS_LOG(20, "write_entry() idx=%s flags=%d", escape_str(instance_list_idx).c_str(), instance_entry.flags);
1072 /* write a new list entry for the object instance */
1073 ret = write_entry(hctx, instance_entry, instance_list_idx);
1074 if (ret < 0) {
1075 CLS_LOG(0, "ERROR: write_entry() instance=%s instance_list_idx=%s ret=%d", instance_entry.key.instance.c_str(), instance_list_idx.c_str(), ret);
1076 return ret;
1077 }
1078 }
1079 return 0;
1080}
1081
1082
1083class BIVerObjEntry {
1084 cls_method_context_t hctx;
1085 cls_rgw_obj_key key;
1086 string instance_idx;
1087
11fdf7f2 1088 rgw_bucket_dir_entry instance_entry;
7c673cae
FG
1089
1090 bool initialized;
1091
1092public:
1093 BIVerObjEntry(cls_method_context_t& _hctx, const cls_rgw_obj_key& _key) : hctx(_hctx), key(_key), initialized(false) {
1094 }
1095
1096 int init(bool check_delete_marker = true) {
1097 int ret = read_key_entry(hctx, key, &instance_idx, &instance_entry,
1098 check_delete_marker && key.instance.empty()); /* this is potentially a delete marker, for null objects we
1099 keep separate instance entry for the delete markers */
1100
1101 if (ret < 0) {
1102 CLS_LOG(0, "ERROR: read_key_entry() idx=%s ret=%d", instance_idx.c_str(), ret);
1103 return ret;
1104 }
1105 initialized = true;
1106 CLS_LOG(20, "read instance_entry key.name=%s key.instance=%s flags=%d", instance_entry.key.name.c_str(), instance_entry.key.instance.c_str(), instance_entry.flags);
1107 return 0;
1108 }
1109
1110 rgw_bucket_dir_entry& get_dir_entry() {
1111 return instance_entry;
1112 }
1113
1114 void init_as_delete_marker(rgw_bucket_dir_entry_meta& meta) {
1115 /* a deletion marker, need to initialize it, there's no instance entry for it yet */
1116 instance_entry.key = key;
1117 instance_entry.flags = RGW_BUCKET_DIRENT_FLAG_DELETE_MARKER;
1118 instance_entry.meta = meta;
1119 instance_entry.tag = "delete-marker";
1120
1121 initialized = true;
1122 }
1123
1124 void set_epoch(uint64_t epoch) {
1125 instance_entry.versioned_epoch = epoch;
1126 }
1127
1128 int unlink_list_entry() {
1129 string list_idx;
1130 /* this instance has a previous list entry, remove that entry */
1131 get_list_index_key(instance_entry, &list_idx);
1132 CLS_LOG(20, "unlink_list_entry() list_idx=%s", escape_str(list_idx).c_str());
1133 int ret = cls_cxx_map_remove_key(hctx, list_idx);
1134 if (ret < 0) {
1135 CLS_LOG(0, "ERROR: cls_cxx_map_remove_key() list_idx=%s ret=%d", list_idx.c_str(), ret);
1136 return ret;
1137 }
1138 return 0;
1139 }
1140
1141 int unlink() {
1142 /* remove the instance entry */
1143 CLS_LOG(20, "unlink() idx=%s", escape_str(instance_idx).c_str());
1144 int ret = cls_cxx_map_remove_key(hctx, instance_idx);
1145 if (ret < 0) {
1146 CLS_LOG(0, "ERROR: cls_cxx_map_remove_key() instance_idx=%s ret=%d", instance_idx.c_str(), ret);
1147 return ret;
1148 }
1149 return 0;
1150 }
1151
1152 int write_entries(uint64_t flags_set, uint64_t flags_reset) {
1153 if (!initialized) {
1154 int ret = init();
1155 if (ret < 0) {
1156 return ret;
1157 }
1158 }
1159 instance_entry.flags &= ~flags_reset;
1160 instance_entry.flags |= flags_set;
1161
1162 /* write the instance and list entries */
1163 bool special_delete_marker_key = (instance_entry.is_delete_marker() && instance_entry.key.instance.empty());
1164 encode_obj_versioned_data_key(key, &instance_idx, special_delete_marker_key);
1165 int ret = write_obj_entries(hctx, instance_entry, instance_idx);
1166 if (ret < 0) {
1167 CLS_LOG(0, "ERROR: write_obj_entries() instance_idx=%s ret=%d", instance_idx.c_str(), ret);
1168 return ret;
1169 }
1170
1171 return 0;
1172 }
1173
1174 int write(uint64_t epoch, bool current) {
1175 if (instance_entry.versioned_epoch > 0) {
1176 CLS_LOG(20, "%s(): instance_entry.versioned_epoch=%d epoch=%d", __func__, (int)instance_entry.versioned_epoch, (int)epoch);
1177 /* this instance has a previous list entry, remove that entry */
1178 int ret = unlink_list_entry();
1179 if (ret < 0) {
1180 return ret;
1181 }
1182 }
1183
1184 uint64_t flags = RGW_BUCKET_DIRENT_FLAG_VER;
1185 if (current) {
1186 flags |= RGW_BUCKET_DIRENT_FLAG_CURRENT;
1187 }
1188
1189 instance_entry.versioned_epoch = epoch;
1190 return write_entries(flags, 0);
1191 }
1192
1193 int demote_current() {
1194 return write_entries(0, RGW_BUCKET_DIRENT_FLAG_CURRENT);
1195 }
1196
1197 bool is_delete_marker() {
1198 return instance_entry.is_delete_marker();
1199 }
1200
1201 int find_next_key(cls_rgw_obj_key *next_key, bool *found) {
1202 string list_idx;
1203 /* this instance has a previous list entry, remove that entry */
1204 get_list_index_key(instance_entry, &list_idx);
1205 /* this is the current head, need to update! */
1206 map<string, bufferlist> keys;
c07f9fc5 1207 bool more;
7c673cae
FG
1208 string filter = key.name; /* list key starts with key name, filter it to avoid a case where we cross to
1209 different namespace */
c07f9fc5 1210 int ret = cls_cxx_map_get_vals(hctx, list_idx, filter, 1, &keys, &more);
7c673cae
FG
1211 if (ret < 0) {
1212 return ret;
1213 }
1214
1215 if (keys.size() < 1) {
1216 *found = false;
1217 return 0;
1218 }
1219
1220 rgw_bucket_dir_entry next_entry;
1221
1222 map<string, bufferlist>::reverse_iterator last = keys.rbegin();
1223 try {
11fdf7f2
TL
1224 auto iter = last->second.cbegin();
1225 decode(next_entry, iter);
7c673cae
FG
1226 } catch (buffer::error& err) {
1227 CLS_LOG(0, "ERROR; failed to decode entry: %s", last->first.c_str());
1228 return -EIO;
1229 }
1230
1231 *found = (key.name == next_entry.key.name);
1232 if (*found) {
1233 *next_key = next_entry.key;
1234 }
1235
1236 return 0;
1237 }
1238
1239 real_time mtime() {
1240 return instance_entry.meta.mtime;
1241 }
1242};
1243
1244
1245class BIOLHEntry {
1246 cls_method_context_t hctx;
1247 cls_rgw_obj_key key;
1248
1249 string olh_data_idx;
11fdf7f2 1250 rgw_bucket_olh_entry olh_data_entry;
7c673cae
FG
1251
1252 bool initialized;
1253public:
1254 BIOLHEntry(cls_method_context_t& _hctx, const cls_rgw_obj_key& _key) : hctx(_hctx), key(_key), initialized(false) { }
1255
1256 int init(bool *exists) {
1257 /* read olh */
1258 int ret = read_olh(hctx, key, &olh_data_entry, &olh_data_idx, exists);
1259 if (ret < 0) {
1260 return ret;
1261 }
1262
1263 initialized = true;
1264 return 0;
1265 }
1266
7c673cae
FG
1267 bool start_modify(uint64_t candidate_epoch) {
1268 if (candidate_epoch) {
1269 if (candidate_epoch < olh_data_entry.epoch) {
1270 return false; /* olh cannot be modified, old epoch */
1271 }
1272 olh_data_entry.epoch = candidate_epoch;
1273 } else {
1274 if (olh_data_entry.epoch == 0) {
1275 olh_data_entry.epoch = 2; /* versioned epoch should start with 2, 1 is reserved to converted plain entries */
1276 } else {
1277 olh_data_entry.epoch++;
1278 }
1279 }
1280 return true;
1281 }
1282
1283 uint64_t get_epoch() {
1284 return olh_data_entry.epoch;
1285 }
1286
1287 rgw_bucket_olh_entry& get_entry() {
1288 return olh_data_entry;
1289 }
1290
1291 void update(cls_rgw_obj_key& key, bool delete_marker) {
1292 olh_data_entry.delete_marker = delete_marker;
1293 olh_data_entry.key = key;
1294 }
1295
1296 int write() {
1297 /* write the olh data entry */
1298 int ret = write_entry(hctx, olh_data_entry, olh_data_idx);
1299 if (ret < 0) {
1300 CLS_LOG(0, "ERROR: write_entry() olh_key=%s ret=%d", olh_data_idx.c_str(), ret);
1301 return ret;
1302 }
1303
1304 return 0;
1305 }
1306
1307 void update_log(OLHLogOp op, const string& op_tag, cls_rgw_obj_key& key, bool delete_marker, uint64_t epoch = 0) {
1308 if (epoch == 0) {
1309 epoch = olh_data_entry.epoch;
1310 }
1311 update_olh_log(olh_data_entry, op, op_tag, key, delete_marker, epoch);
1312 }
1313
1314 bool exists() { return olh_data_entry.exists; }
1315
1316 void set_exists(bool exists) {
1317 olh_data_entry.exists = exists;
1318 }
1319
1320 bool pending_removal() { return olh_data_entry.pending_removal; }
1321
1322 void set_pending_removal(bool pending_removal) {
1323 olh_data_entry.pending_removal = pending_removal;
1324 }
1325
1326 const string& get_tag() { return olh_data_entry.tag; }
1327 void set_tag(const string& tag) {
1328 olh_data_entry.tag = tag;
1329 }
1330};
1331
1332static int write_version_marker(cls_method_context_t hctx, cls_rgw_obj_key& key)
1333{
11fdf7f2 1334 rgw_bucket_dir_entry entry;
7c673cae
FG
1335 entry.key = key;
1336 entry.flags = RGW_BUCKET_DIRENT_FLAG_VER_MARKER;
1337 int ret = write_entry(hctx, entry, key.name);
1338 if (ret < 0) {
1339 CLS_LOG(0, "ERROR: write_entry returned ret=%d", ret);
1340 return ret;
1341 }
1342 return 0;
1343}
1344
1345/*
1346 * plain entries are the ones who were created when bucket was not versioned,
1347 * if we override these objects, we need to convert these to versioned entries -- ones that have
1348 * both data entry, and listing key. Their version is going to be empty though
1349 */
1350static int convert_plain_entry_to_versioned(cls_method_context_t hctx, cls_rgw_obj_key& key, bool demote_current, bool instance_only)
1351{
1352 if (!key.instance.empty()) {
1353 return -EINVAL;
1354 }
1355
11fdf7f2 1356 rgw_bucket_dir_entry entry;
7c673cae
FG
1357
1358 string orig_idx;
1359 int ret = read_key_entry(hctx, key, &orig_idx, &entry);
1360 if (ret != -ENOENT) {
1361 if (ret < 0) {
1362 CLS_LOG(0, "ERROR: read_key_entry() returned ret=%d", ret);
1363 return ret;
1364 }
1365
1366 entry.versioned_epoch = 1; /* converted entries are always 1 */
1367 entry.flags |= RGW_BUCKET_DIRENT_FLAG_VER;
1368
1369 if (demote_current) {
1370 entry.flags &= ~RGW_BUCKET_DIRENT_FLAG_CURRENT;
1371 }
1372
1373 string new_idx;
1374 encode_obj_versioned_data_key(key, &new_idx);
1375
1376 if (instance_only) {
1377 ret = write_obj_instance_entry(hctx, entry, new_idx);
1378 } else {
1379 ret = write_obj_entries(hctx, entry, new_idx);
1380 }
1381 if (ret < 0) {
1382 CLS_LOG(0, "ERROR: write_obj_entries new_idx=%s returned %d", new_idx.c_str(), ret);
1383 return ret;
1384 }
1385 }
1386
1387 ret = write_version_marker(hctx, key);
1388 if (ret < 0) {
1389 return ret;
1390 }
1391
1392 return 0;
1393}
1394
1395/*
1396 * link an object version to an olh, update the relevant index entries. It will also handle the
1397 * deletion marker case. We have a few entries that we need to take care of. For object 'foo',
1398 * instance BAR, we'd update the following (not actual encoding):
1399 * - olh data: [BI_BUCKET_OLH_DATA_INDEX]foo
1400 * - object instance data: [BI_BUCKET_OBJ_INSTANCE_INDEX]foo,BAR
1401 * - object instance list entry: foo,123,BAR
1402 *
1403 * The instance list entry needs to be ordered by newer to older, so we generate an appropriate
1404 * number string that follows the name.
1405 * The top instance for each object is marked appropriately.
1406 * We generate instance entry for deletion markers here, as they are not created prior.
1407 */
1408static int rgw_bucket_link_olh(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
1409{
1410 string olh_data_idx;
1411 string instance_idx;
1412
1413 // decode request
1414 rgw_cls_link_olh_op op;
11fdf7f2 1415 auto iter = in->cbegin();
7c673cae 1416 try {
11fdf7f2 1417 decode(op, iter);
7c673cae
FG
1418 } catch (buffer::error& err) {
1419 CLS_LOG(0, "ERROR: rgw_bucket_link_olh_op(): failed to decode request\n");
1420 return -EINVAL;
1421 }
1422
1423 BIVerObjEntry obj(hctx, op.key);
1424 BIOLHEntry olh(hctx, op.key);
1425
1426 /* read instance entry */
1427 int ret = obj.init(op.delete_marker);
1428 bool existed = (ret == 0);
1429 if (ret == -ENOENT && op.delete_marker) {
1430 ret = 0;
1431 }
1432 if (ret < 0) {
1433 return ret;
1434 }
1435
1436 if (existed && !real_clock::is_zero(op.unmod_since)) {
11fdf7f2
TL
1437 timespec mtime = ceph::real_clock::to_timespec(obj.mtime());
1438 timespec unmod = ceph::real_clock::to_timespec(op.unmod_since);
7c673cae
FG
1439 if (!op.high_precision_time) {
1440 mtime.tv_nsec = 0;
1441 unmod.tv_nsec = 0;
1442 }
1443 if (mtime >= unmod) {
1444 return 0; /* no need to set error, we just return 0 and avoid writing to the bi log */
1445 }
1446 }
1447
1448 bool removing;
1449
1450 /*
1451 * Special handling for null instance object / delete-marker. For these objects we're going to
1452 * have separate instances for a data object vs. delete-marker to avoid collisions. We now check
1453 * if we got to overwrite a previous entry, and in that case we'll remove its list entry.
1454 */
1455 if (op.key.instance.empty()) {
1456 BIVerObjEntry other_obj(hctx, op.key);
1457 ret = other_obj.init(!op.delete_marker); /* try reading the other null versioned entry */
1458 existed = (ret >= 0 && !other_obj.is_delete_marker());
1459 if (ret >= 0 && other_obj.is_delete_marker() != op.delete_marker) {
1460 ret = other_obj.unlink_list_entry();
1461 if (ret < 0) {
1462 return ret;
1463 }
f64942e4
AA
1464 }
1465
1466 removing = existed && op.delete_marker;
1467 if (!removing) {
7c673cae
FG
1468 ret = other_obj.unlink();
1469 if (ret < 0) {
1470 return ret;
1471 }
1472 }
7c673cae
FG
1473 } else {
1474 removing = (existed && !obj.is_delete_marker() && op.delete_marker);
1475 }
1476
1477 if (op.delete_marker) {
1478 /* a deletion marker, need to initialize entry as such */
1479 obj.init_as_delete_marker(op.meta);
1480 }
1481
1482 /* read olh */
1483 bool olh_found;
1484 ret = olh.init(&olh_found);
1485 if (ret < 0) {
1486 return ret;
1487 }
92f5a8d4 1488 const uint64_t prev_epoch = olh.get_epoch();
7c673cae
FG
1489
1490 if (!olh.start_modify(op.olh_epoch)) {
1491 ret = obj.write(op.olh_epoch, false);
1492 if (ret < 0) {
1493 return ret;
1494 }
1495 if (removing) {
1496 olh.update_log(CLS_RGW_OLH_OP_REMOVE_INSTANCE, op.op_tag, op.key, false, op.olh_epoch);
1497 }
1498 return 0;
1499 }
1500
92f5a8d4
TL
1501 // promote this version to current if it's a newer epoch, or if it matches the
1502 // current epoch and sorts after the current instance
1503 const bool promote = (olh.get_epoch() > prev_epoch) ||
1504 (olh.get_epoch() == prev_epoch &&
1505 olh.get_entry().key.instance >= op.key.instance);
1506
7c673cae
FG
1507 if (olh_found) {
1508 const string& olh_tag = olh.get_tag();
1509 if (op.olh_tag != olh_tag) {
1510 if (!olh.pending_removal()) {
1511 CLS_LOG(5, "NOTICE: op.olh_tag (%s) != olh.tag (%s)", op.olh_tag.c_str(), olh_tag.c_str());
1512 return -ECANCELED;
1513 }
1514 /* if pending removal, this is a new olh instance */
1515 olh.set_tag(op.olh_tag);
1516 }
92f5a8d4 1517 if (promote && olh.exists()) {
7c673cae
FG
1518 rgw_bucket_olh_entry& olh_entry = olh.get_entry();
1519 /* found olh, previous instance is no longer the latest, need to update */
1520 if (!(olh_entry.key == op.key)) {
1521 BIVerObjEntry old_obj(hctx, olh_entry.key);
1522
1523 ret = old_obj.demote_current();
1524 if (ret < 0) {
1525 CLS_LOG(0, "ERROR: could not demote current on previous key ret=%d", ret);
1526 return ret;
1527 }
1528 }
1529 }
1530 olh.set_pending_removal(false);
1531 } else {
1532 bool instance_only = (op.key.instance.empty() && op.delete_marker);
1533 cls_rgw_obj_key key(op.key.name);
92f5a8d4 1534 ret = convert_plain_entry_to_versioned(hctx, key, promote, instance_only);
7c673cae
FG
1535 if (ret < 0) {
1536 CLS_LOG(0, "ERROR: convert_plain_entry_to_versioned ret=%d", ret);
1537 return ret;
1538 }
1539 olh.set_tag(op.olh_tag);
1540 }
1541
1542 /* update the olh log */
1543 olh.update_log(CLS_RGW_OLH_OP_LINK_OLH, op.op_tag, op.key, op.delete_marker);
1544 if (removing) {
1545 olh.update_log(CLS_RGW_OLH_OP_REMOVE_INSTANCE, op.op_tag, op.key, false);
1546 }
1547
92f5a8d4
TL
1548 if (promote) {
1549 olh.update(op.key, op.delete_marker);
1550 }
7c673cae
FG
1551 olh.set_exists(true);
1552
1553 ret = olh.write();
1554 if (ret < 0) {
1555 CLS_LOG(0, "ERROR: failed to update olh ret=%d", ret);
1556 return ret;
1557 }
1558
1559 /* write the instance and list entries */
92f5a8d4 1560 ret = obj.write(olh.get_epoch(), promote);
7c673cae
FG
1561 if (ret < 0) {
1562 return ret;
1563 }
1564
11fdf7f2 1565 rgw_bucket_dir_header header;
7c673cae
FG
1566 ret = read_bucket_header(hctx, &header);
1567 if (ret < 0) {
11fdf7f2 1568 CLS_LOG(1, "ERROR: rgw_bucket_link_olh(): failed to read header\n");
7c673cae
FG
1569 return ret;
1570 }
1571
c07f9fc5 1572 if (op.log_op && !header.syncstopped) {
7c673cae
FG
1573 rgw_bucket_dir_entry& entry = obj.get_dir_entry();
1574
1575 rgw_bucket_entry_ver ver;
1576 ver.epoch = (op.olh_epoch ? op.olh_epoch : olh.get_epoch());
1577
1578 string *powner = NULL;
1579 string *powner_display_name = NULL;
1580
1581 if (op.delete_marker) {
1582 powner = &entry.meta.owner;
1583 powner_display_name = &entry.meta.owner_display_name;
1584 }
1585
1586 RGWModifyOp operation = (op.delete_marker ? CLS_RGW_OP_LINK_OLH_DM : CLS_RGW_OP_LINK_OLH);
1587 ret = log_index_operation(hctx, op.key, operation, op.op_tag,
1588 entry.meta.mtime, ver,
1589 CLS_RGW_STATE_COMPLETE, header.ver, header.max_marker, op.bilog_flags | RGW_BILOG_FLAG_VERSIONED_OP,
31f18b77 1590 powner, powner_display_name, &op.zones_trace);
7c673cae
FG
1591 if (ret < 0)
1592 return ret;
11fdf7f2
TL
1593
1594 return write_bucket_header(hctx, &header); /* updates header version */
7c673cae
FG
1595 }
1596
11fdf7f2 1597 return 0;
7c673cae
FG
1598}
1599
1600static int rgw_bucket_unlink_instance(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
1601{
1602 string olh_data_idx;
1603 string instance_idx;
1604
1605 // decode request
1606 rgw_cls_unlink_instance_op op;
11fdf7f2 1607 auto iter = in->cbegin();
7c673cae 1608 try {
11fdf7f2 1609 decode(op, iter);
7c673cae
FG
1610 } catch (buffer::error& err) {
1611 CLS_LOG(0, "ERROR: rgw_bucket_rm_obj_instance_op(): failed to decode request\n");
1612 return -EINVAL;
1613 }
1614
1615 cls_rgw_obj_key dest_key = op.key;
1616 if (dest_key.instance == "null") {
1617 dest_key.instance.clear();
1618 }
1619
1620 BIVerObjEntry obj(hctx, dest_key);
1621 BIOLHEntry olh(hctx, dest_key);
1622
1623 int ret = obj.init();
1624 if (ret == -ENOENT) {
1625 return 0; /* already removed */
1626 }
1627 if (ret < 0) {
1628 CLS_LOG(0, "ERROR: obj.init() returned ret=%d", ret);
1629 return ret;
1630 }
1631
1632 bool olh_found;
1633 ret = olh.init(&olh_found);
1634 if (ret < 0) {
1635 CLS_LOG(0, "ERROR: olh.init() returned ret=%d", ret);
1636 return ret;
1637 }
1638
1639 if (!olh_found) {
1640 bool instance_only = false;
1641 cls_rgw_obj_key key(dest_key.name);
1642 ret = convert_plain_entry_to_versioned(hctx, key, true, instance_only);
1643 if (ret < 0) {
1644 CLS_LOG(0, "ERROR: convert_plain_entry_to_versioned ret=%d", ret);
1645 return ret;
1646 }
1647 olh.update(dest_key, false);
1648 olh.set_tag(op.olh_tag);
1649
1650 obj.set_epoch(1);
1651 }
1652
1653 if (!olh.start_modify(op.olh_epoch)) {
1654 ret = obj.unlink_list_entry();
1655 if (ret < 0) {
1656 return ret;
1657 }
1658
1659 if (!obj.is_delete_marker()) {
1660 olh.update_log(CLS_RGW_OLH_OP_REMOVE_INSTANCE, op.op_tag, op.key, false, op.olh_epoch);
1661 }
1662
1663 return 0;
1664 }
1665
1666 rgw_bucket_olh_entry& olh_entry = olh.get_entry();
1667 cls_rgw_obj_key& olh_key = olh_entry.key;
1668 CLS_LOG(20, "%s(): updating olh log: existing olh entry: %s[%s] (delete_marker=%d)", __func__,
1669 olh_key.name.c_str(), olh_key.instance.c_str(), olh_entry.delete_marker);
1670
1671 if (olh_key == dest_key) {
1672 /* this is the current head, need to update! */
1673 cls_rgw_obj_key next_key;
11fdf7f2 1674 bool found = false;
7c673cae
FG
1675 ret = obj.find_next_key(&next_key, &found);
1676 if (ret < 0) {
1677 CLS_LOG(0, "ERROR: obj.find_next_key() returned ret=%d", ret);
1678 return ret;
1679 }
1680
1681 if (found) {
1682 BIVerObjEntry next(hctx, next_key);
1683 ret = next.write(olh.get_epoch(), true);
1684 if (ret < 0) {
1685 CLS_LOG(0, "ERROR: next.write() returned ret=%d", ret);
1686 return ret;
1687 }
1688
1689 CLS_LOG(20, "%s(): updating olh log: link olh -> %s[%s] (is_delete=%d)", __func__,
1690 next_key.name.c_str(), next_key.instance.c_str(), (int)next.is_delete_marker());
1691
1692 olh.update(next_key, next.is_delete_marker());
1693 olh.update_log(CLS_RGW_OLH_OP_LINK_OLH, op.op_tag, next_key, next.is_delete_marker());
1694 } else {
1695 /* next_key is empty */
1696 olh.update(next_key, false);
1697 olh.update_log(CLS_RGW_OLH_OP_UNLINK_OLH, op.op_tag, next_key, false);
1698 olh.set_exists(false);
1699 olh.set_pending_removal(true);
1700 }
1701 }
1702
1703 if (!obj.is_delete_marker()) {
1704 olh.update_log(CLS_RGW_OLH_OP_REMOVE_INSTANCE, op.op_tag, op.key, false);
1705 } else {
1706 /* this is a delete marker, it's our responsibility to remove its instance entry */
1707 ret = obj.unlink();
1708 if (ret < 0) {
1709 return ret;
1710 }
1711 }
1712
1713 ret = obj.unlink_list_entry();
1714 if (ret < 0) {
1715 return ret;
1716 }
1717
1718 ret = olh.write();
1719 if (ret < 0) {
1720 return ret;
1721 }
1722
11fdf7f2 1723 rgw_bucket_dir_header header;
7c673cae
FG
1724 ret = read_bucket_header(hctx, &header);
1725 if (ret < 0) {
1726 CLS_LOG(1, "ERROR: rgw_bucket_unlink_instance(): failed to read header\n");
1727 return ret;
1728 }
1729
c07f9fc5 1730 if (op.log_op && !header.syncstopped) {
7c673cae
FG
1731 rgw_bucket_entry_ver ver;
1732 ver.epoch = (op.olh_epoch ? op.olh_epoch : olh.get_epoch());
1733
94b18763 1734 real_time mtime = obj.mtime(); /* mtime has no real meaning in instance removal context */
7c673cae
FG
1735 ret = log_index_operation(hctx, op.key, CLS_RGW_OP_UNLINK_INSTANCE, op.op_tag,
1736 mtime, ver,
1737 CLS_RGW_STATE_COMPLETE, header.ver, header.max_marker,
31f18b77 1738 op.bilog_flags | RGW_BILOG_FLAG_VERSIONED_OP, NULL, NULL, &op.zones_trace);
7c673cae
FG
1739 if (ret < 0)
1740 return ret;
11fdf7f2
TL
1741
1742 return write_bucket_header(hctx, &header); /* updates header version */
7c673cae
FG
1743 }
1744
11fdf7f2 1745 return 0;
7c673cae
FG
1746}
1747
1748static int rgw_bucket_read_olh_log(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
1749{
1750 // decode request
1751 rgw_cls_read_olh_log_op op;
11fdf7f2 1752 auto iter = in->cbegin();
7c673cae 1753 try {
11fdf7f2 1754 decode(op, iter);
7c673cae
FG
1755 } catch (buffer::error& err) {
1756 CLS_LOG(0, "ERROR: rgw_bucket_read_olh_log(): failed to decode request\n");
1757 return -EINVAL;
1758 }
1759
1760 if (!op.olh.instance.empty()) {
1761 CLS_LOG(1, "bad key passed in (non empty instance)");
1762 return -EINVAL;
1763 }
1764
11fdf7f2 1765 rgw_bucket_olh_entry olh_data_entry;
7c673cae
FG
1766 string olh_data_key;
1767 encode_olh_data_key(op.olh, &olh_data_key);
1768 int ret = read_index_entry(hctx, olh_data_key, &olh_data_entry);
1769 if (ret < 0 && ret != -ENOENT) {
1770 CLS_LOG(0, "ERROR: read_index_entry() olh_key=%s ret=%d", olh_data_key.c_str(), ret);
1771 return ret;
1772 }
1773
1774 if (olh_data_entry.tag != op.olh_tag) {
1775 CLS_LOG(1, "NOTICE: %s(): olh_tag_mismatch olh_data_entry.tag=%s op.olh_tag=%s", __func__, olh_data_entry.tag.c_str(), op.olh_tag.c_str());
1776 return -ECANCELED;
1777 }
1778
1779 rgw_cls_read_olh_log_ret op_ret;
1780
1781#define MAX_OLH_LOG_ENTRIES 1000
1782 map<uint64_t, vector<rgw_bucket_olh_log_entry> >& log = olh_data_entry.pending_log;
1783
1784 if (log.begin()->first > op.ver_marker && log.size() <= MAX_OLH_LOG_ENTRIES) {
1785 op_ret.log = log;
1786 op_ret.is_truncated = false;
1787 } else {
1788 map<uint64_t, vector<rgw_bucket_olh_log_entry> >::iterator iter = log.upper_bound(op.ver_marker);
1789
1790 for (int i = 0; i < MAX_OLH_LOG_ENTRIES && iter != log.end(); ++i, ++iter) {
1791 op_ret.log[iter->first] = iter->second;
1792 }
1793 op_ret.is_truncated = (iter != log.end());
1794 }
1795
11fdf7f2 1796 encode(op_ret, *out);
7c673cae
FG
1797
1798 return 0;
1799}
1800
1801static int rgw_bucket_trim_olh_log(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
1802{
1803 // decode request
1804 rgw_cls_trim_olh_log_op op;
11fdf7f2 1805 auto iter = in->cbegin();
7c673cae 1806 try {
11fdf7f2 1807 decode(op, iter);
7c673cae
FG
1808 } catch (buffer::error& err) {
1809 CLS_LOG(0, "ERROR: rgw_bucket_trim_olh_log(): failed to decode request\n");
1810 return -EINVAL;
1811 }
1812
1813 if (!op.olh.instance.empty()) {
1814 CLS_LOG(1, "bad key passed in (non empty instance)");
1815 return -EINVAL;
1816 }
1817
1818 /* read olh entry */
11fdf7f2 1819 rgw_bucket_olh_entry olh_data_entry;
7c673cae
FG
1820 string olh_data_key;
1821 encode_olh_data_key(op.olh, &olh_data_key);
1822 int ret = read_index_entry(hctx, olh_data_key, &olh_data_entry);
1823 if (ret < 0 && ret != -ENOENT) {
1824 CLS_LOG(0, "ERROR: read_index_entry() olh_key=%s ret=%d", olh_data_key.c_str(), ret);
1825 return ret;
1826 }
1827
1828 if (olh_data_entry.tag != op.olh_tag) {
1829 CLS_LOG(1, "NOTICE: %s(): olh_tag_mismatch olh_data_entry.tag=%s op.olh_tag=%s", __func__, olh_data_entry.tag.c_str(), op.olh_tag.c_str());
1830 return -ECANCELED;
1831 }
1832
1833 /* remove all versions up to and including ver from the pending map */
1834 map<uint64_t, vector<rgw_bucket_olh_log_entry> >& log = olh_data_entry.pending_log;
1835 map<uint64_t, vector<rgw_bucket_olh_log_entry> >::iterator liter = log.begin();
1836 while (liter != log.end() && liter->first <= op.ver) {
1837 map<uint64_t, vector<rgw_bucket_olh_log_entry> >::iterator rm_iter = liter;
1838 ++liter;
1839 log.erase(rm_iter);
1840 }
1841
1842 /* write the olh data entry */
1843 ret = write_entry(hctx, olh_data_entry, olh_data_key);
1844 if (ret < 0) {
1845 CLS_LOG(0, "ERROR: write_entry() olh_key=%s ret=%d", olh_data_key.c_str(), ret);
1846 return ret;
1847 }
1848
1849 return 0;
1850}
1851
1852static int rgw_bucket_clear_olh(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
1853{
1854 // decode request
1855 rgw_cls_bucket_clear_olh_op op;
11fdf7f2 1856 auto iter = in->cbegin();
7c673cae 1857 try {
11fdf7f2 1858 decode(op, iter);
7c673cae
FG
1859 } catch (buffer::error& err) {
1860 CLS_LOG(0, "ERROR: rgw_bucket_clear_olh(): failed to decode request\n");
1861 return -EINVAL;
1862 }
1863
1864 if (!op.key.instance.empty()) {
1865 CLS_LOG(1, "bad key passed in (non empty instance)");
1866 return -EINVAL;
1867 }
1868
1869 /* read olh entry */
11fdf7f2 1870 rgw_bucket_olh_entry olh_data_entry;
7c673cae
FG
1871 string olh_data_key;
1872 encode_olh_data_key(op.key, &olh_data_key);
1873 int ret = read_index_entry(hctx, olh_data_key, &olh_data_entry);
1874 if (ret < 0 && ret != -ENOENT) {
1875 CLS_LOG(0, "ERROR: read_index_entry() olh_key=%s ret=%d", olh_data_key.c_str(), ret);
1876 return ret;
1877 }
1878
1879 if (olh_data_entry.tag != op.olh_tag) {
1880 CLS_LOG(1, "NOTICE: %s(): olh_tag_mismatch olh_data_entry.tag=%s op.olh_tag=%s", __func__, olh_data_entry.tag.c_str(), op.olh_tag.c_str());
1881 return -ECANCELED;
1882 }
1883
1884 ret = cls_cxx_map_remove_key(hctx, olh_data_key);
1885 if (ret < 0) {
1886 CLS_LOG(1, "NOTICE: %s(): can't remove key %s ret=%d", __func__, olh_data_key.c_str(), ret);
1887 return ret;
1888 }
1889
1890 rgw_bucket_dir_entry plain_entry;
1891
1892 /* read plain entry, make sure it's a versioned place holder */
1893 ret = read_index_entry(hctx, op.key.name, &plain_entry);
1894 if (ret == -ENOENT) {
1895 /* we're done, no entry existing */
1896 return 0;
1897 }
1898 if (ret < 0) {
1899 CLS_LOG(0, "ERROR: read_index_entry key=%s ret=%d", op.key.name.c_str(), ret);
1900 return ret;
1901 }
1902
1903 if ((plain_entry.flags & RGW_BUCKET_DIRENT_FLAG_VER_MARKER) == 0) {
1904 /* it's not a version marker, don't remove it */
1905 return 0;
1906 }
1907
1908 ret = cls_cxx_map_remove_key(hctx, op.key.name);
1909 if (ret < 0) {
1910 CLS_LOG(1, "NOTICE: %s(): can't remove key %s ret=%d", __func__, op.key.name.c_str(), ret);
1911 return ret;
1912 }
1913
1914 return 0;
1915}
1916
3efd9988
FG
1917int rgw_dir_suggest_changes(cls_method_context_t hctx,
1918 bufferlist *in, bufferlist *out)
7c673cae
FG
1919{
1920 CLS_LOG(1, "rgw_dir_suggest_changes()");
1921
1922 bufferlist header_bl;
11fdf7f2 1923 rgw_bucket_dir_header header;
7c673cae
FG
1924 bool header_changed = false;
1925
1926 int rc = read_bucket_header(hctx, &header);
1927 if (rc < 0) {
1928 CLS_LOG(1, "ERROR: rgw_dir_suggest_changes(): failed to read header\n");
1929 return rc;
1930 }
1931
224ce89b
WB
1932 timespan tag_timeout(
1933 std::chrono::seconds(
1934 header.tag_timeout ? header.tag_timeout : CEPH_RGW_TAG_TIMEOUT));
7c673cae 1935
11fdf7f2 1936 auto in_iter = in->cbegin();
7c673cae
FG
1937
1938 while (!in_iter.end()) {
1939 __u8 op;
1940 rgw_bucket_dir_entry cur_change;
1941 rgw_bucket_dir_entry cur_disk;
1942 try {
11fdf7f2
TL
1943 decode(op, in_iter);
1944 decode(cur_change, in_iter);
7c673cae
FG
1945 } catch (buffer::error& err) {
1946 CLS_LOG(1, "ERROR: rgw_dir_suggest_changes(): failed to decode request\n");
1947 return -EINVAL;
1948 }
1949
1950 bufferlist cur_disk_bl;
1951 string cur_change_key;
1952 encode_obj_index_key(cur_change.key, &cur_change_key);
1953 int ret = cls_cxx_map_get_val(hctx, cur_change_key, &cur_disk_bl);
1954 if (ret < 0 && ret != -ENOENT)
1955 return -EINVAL;
1956
1adf2230
AA
1957 if (ret == -ENOENT) {
1958 continue;
1959 }
1960
7c673cae 1961 if (cur_disk_bl.length()) {
11fdf7f2 1962 auto cur_disk_iter = cur_disk_bl.cbegin();
7c673cae 1963 try {
11fdf7f2 1964 decode(cur_disk, cur_disk_iter);
7c673cae
FG
1965 } catch (buffer::error& error) {
1966 CLS_LOG(1, "ERROR: rgw_dir_suggest_changes(): failed to decode cur_disk\n");
1967 return -EINVAL;
1968 }
1969
1970 real_time cur_time = real_clock::now();
11fdf7f2 1971 map<string, rgw_bucket_pending_info>::iterator iter =
7c673cae
FG
1972 cur_disk.pending_map.begin();
1973 while(iter != cur_disk.pending_map.end()) {
11fdf7f2 1974 map<string, rgw_bucket_pending_info>::iterator cur_iter=iter++;
7c673cae
FG
1975 if (cur_time > (cur_iter->second.timestamp + timespan(tag_timeout))) {
1976 cur_disk.pending_map.erase(cur_iter);
1977 }
1978 }
1979 }
1980
1981 CLS_LOG(20, "cur_disk.pending_map.empty()=%d op=%d cur_disk.exists=%d cur_change.pending_map.size()=%d cur_change.exists=%d\n",
1982 cur_disk.pending_map.empty(), (int)op, cur_disk.exists,
1983 (int)cur_change.pending_map.size(), cur_change.exists);
1984
1985 if (cur_disk.pending_map.empty()) {
1986 if (cur_disk.exists) {
11fdf7f2 1987 rgw_bucket_category_stats& old_stats = header.stats[cur_disk.meta.category];
7c673cae
FG
1988 CLS_LOG(10, "total_entries: %" PRId64 " -> %" PRId64 "\n", old_stats.num_entries, old_stats.num_entries - 1);
1989 old_stats.num_entries--;
1990 old_stats.total_size -= cur_disk.meta.accounted_size;
1991 old_stats.total_size_rounded -= cls_rgw_get_rounded_size(cur_disk.meta.accounted_size);
1992 old_stats.actual_size -= cur_disk.meta.size;
1993 header_changed = true;
1994 }
11fdf7f2 1995 rgw_bucket_category_stats& stats = header.stats[cur_change.meta.category];
7c673cae
FG
1996 bool log_op = (op & CEPH_RGW_DIR_SUGGEST_LOG_OP) != 0;
1997 op &= CEPH_RGW_DIR_SUGGEST_OP_MASK;
1998 switch(op) {
1999 case CEPH_RGW_REMOVE:
2000 CLS_LOG(10, "CEPH_RGW_REMOVE name=%s instance=%s\n", cur_change.key.name.c_str(), cur_change.key.instance.c_str());
2001 ret = cls_cxx_map_remove_key(hctx, cur_change_key);
2002 if (ret < 0)
2003 return ret;
c07f9fc5 2004 if (log_op && cur_disk.exists && !header.syncstopped) {
7c673cae 2005 ret = log_index_operation(hctx, cur_disk.key, CLS_RGW_OP_DEL, cur_disk.tag, cur_disk.meta.mtime,
31f18b77 2006 cur_disk.ver, CLS_RGW_STATE_COMPLETE, header.ver, header.max_marker, 0, NULL, NULL, NULL);
7c673cae
FG
2007 if (ret < 0) {
2008 CLS_LOG(0, "ERROR: %s(): failed to log operation ret=%d", __func__, ret);
2009 return ret;
2010 }
2011 }
2012 break;
2013 case CEPH_RGW_UPDATE:
2014 CLS_LOG(10, "CEPH_RGW_UPDATE name=%s instance=%s total_entries: %" PRId64 " -> %" PRId64 "\n",
2015 cur_change.key.name.c_str(), cur_change.key.instance.c_str(), stats.num_entries, stats.num_entries + 1);
3efd9988 2016
7c673cae
FG
2017 stats.num_entries++;
2018 stats.total_size += cur_change.meta.accounted_size;
2019 stats.total_size_rounded += cls_rgw_get_rounded_size(cur_change.meta.accounted_size);
2020 stats.actual_size += cur_change.meta.size;
2021 header_changed = true;
2022 cur_change.index_ver = header.ver;
2023 bufferlist cur_state_bl;
11fdf7f2 2024 encode(cur_change, cur_state_bl);
7c673cae
FG
2025 ret = cls_cxx_map_set_val(hctx, cur_change_key, &cur_state_bl);
2026 if (ret < 0)
2027 return ret;
c07f9fc5 2028 if (log_op && !header.syncstopped) {
7c673cae 2029 ret = log_index_operation(hctx, cur_change.key, CLS_RGW_OP_ADD, cur_change.tag, cur_change.meta.mtime,
31f18b77 2030 cur_change.ver, CLS_RGW_STATE_COMPLETE, header.ver, header.max_marker, 0, NULL, NULL, NULL);
7c673cae
FG
2031 if (ret < 0) {
2032 CLS_LOG(0, "ERROR: %s(): failed to log operation ret=%d", __func__, ret);
2033 return ret;
2034 }
2035 }
2036 break;
3efd9988
FG
2037 } // switch(op)
2038 } // if (cur_disk.pending_map.empty())
2039 } // while (!in_iter.end())
7c673cae
FG
2040
2041 if (header_changed) {
2042 return write_bucket_header(hctx, &header);
2043 }
2044 return 0;
2045}
2046
2047static int rgw_obj_remove(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
2048{
2049 // decode request
2050 rgw_cls_obj_remove_op op;
11fdf7f2 2051 auto iter = in->cbegin();
7c673cae 2052 try {
11fdf7f2 2053 decode(op, iter);
7c673cae
FG
2054 } catch (buffer::error& err) {
2055 CLS_LOG(0, "ERROR: %s(): failed to decode request", __func__);
2056 return -EINVAL;
2057 }
2058
2059 if (op.keep_attr_prefixes.empty()) {
2060 return cls_cxx_remove(hctx);
2061 }
2062
2063 map<string, bufferlist> attrset;
2064 int ret = cls_cxx_getxattrs(hctx, &attrset);
2065 if (ret < 0 && ret != -ENOENT) {
2066 CLS_LOG(0, "ERROR: %s(): cls_cxx_getxattrs() returned %d", __func__, ret);
2067 return ret;
2068 }
2069
2070 map<string, bufferlist> new_attrs;
2071 for (list<string>::iterator iter = op.keep_attr_prefixes.begin();
2072 iter != op.keep_attr_prefixes.end(); ++iter) {
2073 string& check_prefix = *iter;
2074
2075 for (map<string, bufferlist>::iterator aiter = attrset.lower_bound(check_prefix);
2076 aiter != attrset.end(); ++aiter) {
2077 const string& attr = aiter->first;
2078
2079 if (attr.substr(0, check_prefix.size()) > check_prefix) {
2080 break;
2081 }
2082
2083 new_attrs[attr] = aiter->second;
2084 }
2085 }
2086
2087 CLS_LOG(20, "%s(): removing object", __func__);
2088 ret = cls_cxx_remove(hctx);
2089 if (ret < 0) {
2090 CLS_LOG(0, "ERROR: %s(): cls_cxx_remove returned %d", __func__, ret);
2091 return ret;
2092 }
2093
2094 if (new_attrs.empty()) {
2095 /* no data to keep */
2096 return 0;
2097 }
2098
2099 ret = cls_cxx_create(hctx, false);
2100 if (ret < 0) {
2101 CLS_LOG(0, "ERROR: %s(): cls_cxx_create returned %d", __func__, ret);
2102 return ret;
2103 }
2104
2105 for (map<string, bufferlist>::iterator aiter = new_attrs.begin();
2106 aiter != new_attrs.end(); ++aiter) {
2107 const string& attr = aiter->first;
2108
2109 ret = cls_cxx_setxattr(hctx, attr.c_str(), &aiter->second);
2110 CLS_LOG(20, "%s(): setting attr: %s", __func__, attr.c_str());
2111 if (ret < 0) {
2112 CLS_LOG(0, "ERROR: %s(): cls_cxx_setxattr (attr=%s) returned %d", __func__, attr.c_str(), ret);
2113 return ret;
2114 }
2115 }
2116
2117 return 0;
2118}
2119
2120static int rgw_obj_store_pg_ver(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
2121{
2122 // decode request
2123 rgw_cls_obj_store_pg_ver_op op;
11fdf7f2 2124 auto iter = in->cbegin();
7c673cae 2125 try {
11fdf7f2 2126 decode(op, iter);
7c673cae
FG
2127 } catch (buffer::error& err) {
2128 CLS_LOG(0, "ERROR: %s(): failed to decode request", __func__);
2129 return -EINVAL;
2130 }
2131
2132 bufferlist bl;
2133 uint64_t ver = cls_current_version(hctx);
11fdf7f2 2134 encode(ver, bl);
7c673cae
FG
2135 int ret = cls_cxx_setxattr(hctx, op.attr.c_str(), &bl);
2136 if (ret < 0) {
2137 CLS_LOG(0, "ERROR: %s(): cls_cxx_setxattr (attr=%s) returned %d", __func__, op.attr.c_str(), ret);
2138 return ret;
2139 }
2140
2141 return 0;
2142}
2143
2144static int rgw_obj_check_attrs_prefix(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
2145{
2146 // decode request
2147 rgw_cls_obj_check_attrs_prefix op;
11fdf7f2 2148 auto iter = in->cbegin();
7c673cae 2149 try {
11fdf7f2 2150 decode(op, iter);
7c673cae
FG
2151 } catch (buffer::error& err) {
2152 CLS_LOG(0, "ERROR: %s(): failed to decode request", __func__);
2153 return -EINVAL;
2154 }
2155
2156 if (op.check_prefix.empty()) {
2157 return -EINVAL;
2158 }
2159
2160 map<string, bufferlist> attrset;
2161 int ret = cls_cxx_getxattrs(hctx, &attrset);
2162 if (ret < 0 && ret != -ENOENT) {
2163 CLS_LOG(0, "ERROR: %s(): cls_cxx_getxattrs() returned %d", __func__, ret);
2164 return ret;
2165 }
2166
2167 bool exist = false;
2168
2169 for (map<string, bufferlist>::iterator aiter = attrset.lower_bound(op.check_prefix);
2170 aiter != attrset.end(); ++aiter) {
2171 const string& attr = aiter->first;
2172
2173 if (attr.substr(0, op.check_prefix.size()) > op.check_prefix) {
2174 break;
2175 }
2176
2177 exist = true;
2178 }
2179
2180 if (exist == op.fail_if_exist) {
2181 return -ECANCELED;
2182 }
2183
2184 return 0;
2185}
2186
2187static int rgw_obj_check_mtime(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
2188{
2189 // decode request
2190 rgw_cls_obj_check_mtime op;
11fdf7f2 2191 auto iter = in->cbegin();
7c673cae 2192 try {
11fdf7f2 2193 decode(op, iter);
7c673cae
FG
2194 } catch (buffer::error& err) {
2195 CLS_LOG(0, "ERROR: %s(): failed to decode request", __func__);
2196 return -EINVAL;
2197 }
2198
2199 real_time obj_ut;
2200 int ret = cls_cxx_stat2(hctx, NULL, &obj_ut);
2201 if (ret < 0 && ret != -ENOENT) {
2202 CLS_LOG(0, "ERROR: %s(): cls_cxx_stat() returned %d", __func__, ret);
2203 return ret;
2204 }
2205 if (ret == -ENOENT) {
2206 CLS_LOG(10, "object does not exist, skipping check");
2207 }
2208
2209 ceph_timespec obj_ts = ceph::real_clock::to_ceph_timespec(obj_ut);
2210 ceph_timespec op_ts = ceph::real_clock::to_ceph_timespec(op.mtime);
2211
2212 if (!op.high_precision_time) {
2213 obj_ts.tv_nsec = 0;
2214 op_ts.tv_nsec = 0;
2215 }
2216
2217 CLS_LOG(10, "%s: obj_ut=%lld.%06lld op.mtime=%lld.%06lld", __func__,
2218 (long long)obj_ts.tv_sec, (long long)obj_ts.tv_nsec,
2219 (long long)op_ts.tv_sec, (long long)op_ts.tv_nsec);
2220
2221 bool check;
2222
2223 switch (op.type) {
2224 case CLS_RGW_CHECK_TIME_MTIME_EQ:
2225 check = (obj_ts == op_ts);
2226 break;
2227 case CLS_RGW_CHECK_TIME_MTIME_LT:
2228 check = (obj_ts < op_ts);
2229 break;
2230 case CLS_RGW_CHECK_TIME_MTIME_LE:
2231 check = (obj_ts <= op_ts);
2232 break;
2233 case CLS_RGW_CHECK_TIME_MTIME_GT:
2234 check = (obj_ts > op_ts);
2235 break;
2236 case CLS_RGW_CHECK_TIME_MTIME_GE:
2237 check = (obj_ts >= op_ts);
2238 break;
2239 default:
2240 return -EINVAL;
2241 };
2242
2243 if (!check) {
2244 return -ECANCELED;
2245 }
2246
2247 return 0;
2248}
2249
2250static int rgw_bi_get_op(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
2251{
2252 // decode request
2253 rgw_cls_bi_get_op op;
11fdf7f2 2254 auto iter = in->cbegin();
7c673cae 2255 try {
11fdf7f2 2256 decode(op, iter);
7c673cae
FG
2257 } catch (buffer::error& err) {
2258 CLS_LOG(0, "ERROR: %s(): failed to decode request", __func__);
2259 return -EINVAL;
2260 }
2261
2262 string idx;
2263
2264 switch (op.type) {
11fdf7f2 2265 case BIIndexType::Plain:
7c673cae
FG
2266 idx = op.key.name;
2267 break;
11fdf7f2 2268 case BIIndexType::Instance:
7c673cae
FG
2269 encode_obj_index_key(op.key, &idx);
2270 break;
11fdf7f2 2271 case BIIndexType::OLH:
7c673cae
FG
2272 encode_olh_data_key(op.key, &idx);
2273 break;
2274 default:
11fdf7f2
TL
2275 CLS_LOG(10, "%s(): invalid key type encoding: %d",
2276 __func__, int(op.type));
7c673cae
FG
2277 return -EINVAL;
2278 }
2279
2280 rgw_cls_bi_get_ret op_ret;
2281
2282 rgw_cls_bi_entry& entry = op_ret.entry;
2283
2284 entry.type = op.type;
2285 entry.idx = idx;
2286
2287 int r = cls_cxx_map_get_val(hctx, idx, &entry.data);
2288 if (r < 0) {
2289 CLS_LOG(10, "%s(): cls_cxx_map_get_val() returned %d", __func__, r);
2290 return r;
2291 }
2292
11fdf7f2 2293 encode(op_ret, *out);
7c673cae
FG
2294
2295 return 0;
2296}
2297
2298static int rgw_bi_put_op(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
2299{
2300 // decode request
2301 rgw_cls_bi_put_op op;
11fdf7f2 2302 auto iter = in->cbegin();
7c673cae 2303 try {
11fdf7f2 2304 decode(op, iter);
7c673cae
FG
2305 } catch (buffer::error& err) {
2306 CLS_LOG(0, "ERROR: %s(): failed to decode request", __func__);
2307 return -EINVAL;
2308 }
2309
2310 rgw_cls_bi_entry& entry = op.entry;
2311
2312 int r = cls_cxx_map_set_val(hctx, entry.idx, &entry.data);
2313 if (r < 0) {
2314 CLS_LOG(0, "ERROR: %s(): cls_cxx_map_set_val() returned r=%d", __func__, r);
2315 }
2316
2317 return 0;
2318}
2319
2320static int list_plain_entries(cls_method_context_t hctx, const string& name, const string& marker, uint32_t max,
c07f9fc5 2321 list<rgw_cls_bi_entry> *entries, bool *pmore)
7c673cae
FG
2322{
2323 string filter = name;
2324 string start_key = marker;
2325
2326 string end_key; // stop listing at bi_log_prefix
2327 bi_log_prefix(end_key);
2328
2329 int count = 0;
2330 map<string, bufferlist> keys;
c07f9fc5
FG
2331 int ret = cls_cxx_map_get_vals(hctx, start_key, filter, max, &keys, pmore);
2332 if (ret < 0) {
2333 return ret;
2334 }
2335
2336 map<string, bufferlist>::iterator iter;
2337 for (iter = keys.begin(); iter != keys.end(); ++iter) {
2338 if (iter->first >= end_key) {
2339 /* past the end of plain namespace */
28e407b8
AA
2340 if (pmore) {
2341 *pmore = false;
2342 }
7c673cae
FG
2343 return count;
2344 }
7c673cae 2345
c07f9fc5 2346 rgw_cls_bi_entry entry;
11fdf7f2 2347 entry.type = BIIndexType::Plain;
c07f9fc5
FG
2348 entry.idx = iter->first;
2349 entry.data = iter->second;
7c673cae 2350
11fdf7f2 2351 auto biter = entry.data.cbegin();
7c673cae 2352
c07f9fc5
FG
2353 rgw_bucket_dir_entry e;
2354 try {
11fdf7f2 2355 decode(e, biter);
c07f9fc5
FG
2356 } catch (buffer::error& err) {
2357 CLS_LOG(0, "ERROR: %s(): failed to decode buffer", __func__);
2358 return -EIO;
2359 }
7c673cae 2360
c07f9fc5 2361 CLS_LOG(20, "%s(): entry.idx=%s e.key.name=%s", __func__, escape_str(entry.idx).c_str(), escape_str(e.key.name).c_str());
7c673cae 2362
c07f9fc5 2363 if (!name.empty() && e.key.name != name) {
28e407b8
AA
2364 /* we are skipping the rest of the entries */
2365 if (pmore) {
2366 *pmore = false;
2367 }
c07f9fc5
FG
2368 return count;
2369 }
7c673cae 2370
c07f9fc5
FG
2371 entries->push_back(entry);
2372 count++;
2373 if (count >= (int)max) {
2374 return count;
7c673cae 2375 }
c07f9fc5
FG
2376 start_key = entry.idx;
2377 }
7c673cae
FG
2378
2379 return count;
2380}
2381
2382static int list_instance_entries(cls_method_context_t hctx, const string& name, const string& marker, uint32_t max,
c07f9fc5 2383 list<rgw_cls_bi_entry> *entries, bool *pmore)
7c673cae
FG
2384{
2385 cls_rgw_obj_key key(name);
2386 string first_instance_idx;
2387 encode_obj_versioned_data_key(key, &first_instance_idx);
2388 string start_key;
2389
2390 if (!name.empty()) {
2391 start_key = first_instance_idx;
2392 } else {
2393 start_key = BI_PREFIX_CHAR;
2394 start_key.append(bucket_index_prefixes[BI_BUCKET_OBJ_INSTANCE_INDEX]);
2395 }
2396 string filter = start_key;
2397 if (bi_entry_gt(marker, start_key)) {
2398 start_key = marker;
2399 }
2400 int count = 0;
2401 map<string, bufferlist> keys;
c07f9fc5
FG
2402 bufferlist k;
2403 int ret = cls_cxx_map_get_val(hctx, start_key, &k);
2404 if (ret < 0 && ret != -ENOENT) {
2405 return ret;
2406 }
2407 bool found_first = (ret == 0);
2408 if (found_first) {
2409 --max;
2410 }
2411 if (max > 0) {
2412 ret = cls_cxx_map_get_vals(hctx, start_key, string(), max, &keys, pmore);
7c673cae
FG
2413 CLS_LOG(20, "%s(): start_key=%s first_instance_idx=%s keys.size()=%d", __func__, escape_str(start_key).c_str(), escape_str(first_instance_idx).c_str(), (int)keys.size());
2414 if (ret < 0) {
2415 return ret;
2416 }
c07f9fc5
FG
2417 }
2418 if (found_first) {
2419 keys[start_key].claim(k);
2420 }
7c673cae 2421
c07f9fc5
FG
2422 map<string, bufferlist>::iterator iter;
2423 for (iter = keys.begin(); iter != keys.end(); ++iter) {
2424 rgw_cls_bi_entry entry;
11fdf7f2 2425 entry.type = BIIndexType::Instance;
c07f9fc5
FG
2426 entry.idx = iter->first;
2427 entry.data = iter->second;
7c673cae 2428
c07f9fc5 2429 if (!filter.empty() && entry.idx.compare(0, filter.size(), filter) != 0) {
28e407b8
AA
2430 /* we are skipping the rest of the entries */
2431 if (pmore) {
2432 *pmore = false;
2433 }
c07f9fc5
FG
2434 return count;
2435 }
7c673cae 2436
c07f9fc5 2437 CLS_LOG(20, "%s(): entry.idx=%s", __func__, escape_str(entry.idx).c_str());
7c673cae 2438
11fdf7f2 2439 auto biter = entry.data.cbegin();
7c673cae 2440
c07f9fc5
FG
2441 rgw_bucket_dir_entry e;
2442 try {
11fdf7f2 2443 decode(e, biter);
c07f9fc5
FG
2444 } catch (buffer::error& err) {
2445 CLS_LOG(0, "ERROR: %s(): failed to decode buffer (size=%d)", __func__, entry.data.length());
2446 return -EIO;
2447 }
7c673cae 2448
c07f9fc5 2449 if (!name.empty() && e.key.name != name) {
28e407b8
AA
2450 /* we are skipping the rest of the entries */
2451 if (pmore) {
2452 *pmore = false;
2453 }
c07f9fc5 2454 return count;
7c673cae 2455 }
c07f9fc5
FG
2456
2457 entries->push_back(entry);
2458 count++;
2459 start_key = entry.idx;
2460 }
7c673cae
FG
2461
2462 return count;
2463}
2464
2465static int list_olh_entries(cls_method_context_t hctx, const string& name, const string& marker, uint32_t max,
c07f9fc5 2466 list<rgw_cls_bi_entry> *entries, bool *pmore)
7c673cae
FG
2467{
2468 cls_rgw_obj_key key(name);
2469 string first_instance_idx;
2470 encode_olh_data_key(key, &first_instance_idx);
2471 string start_key;
2472
2473 if (!name.empty()) {
2474 start_key = first_instance_idx;
2475 } else {
2476 start_key = BI_PREFIX_CHAR;
2477 start_key.append(bucket_index_prefixes[BI_BUCKET_OLH_DATA_INDEX]);
2478 }
2479 string filter = start_key;
2480 if (bi_entry_gt(marker, start_key)) {
2481 start_key = marker;
2482 }
2483 int count = 0;
2484 map<string, bufferlist> keys;
c07f9fc5
FG
2485 int ret;
2486 bufferlist k;
2487 ret = cls_cxx_map_get_val(hctx, start_key, &k);
2488 if (ret < 0 && ret != -ENOENT) {
2489 return ret;
2490 }
2491 bool found_first = (ret == 0);
2492 if (found_first) {
2493 --max;
2494 }
2495 if (max > 0) {
2496 ret = cls_cxx_map_get_vals(hctx, start_key, string(), max, &keys, pmore);
7c673cae
FG
2497 CLS_LOG(20, "%s(): start_key=%s first_instance_idx=%s keys.size()=%d", __func__, escape_str(start_key).c_str(), escape_str(first_instance_idx).c_str(), (int)keys.size());
2498 if (ret < 0) {
2499 return ret;
2500 }
c07f9fc5 2501 }
7c673cae 2502
c07f9fc5
FG
2503 if (found_first) {
2504 keys[start_key].claim(k);
2505 }
7c673cae 2506
c07f9fc5
FG
2507 map<string, bufferlist>::iterator iter;
2508 for (iter = keys.begin(); iter != keys.end(); ++iter) {
2509 rgw_cls_bi_entry entry;
11fdf7f2 2510 entry.type = BIIndexType::OLH;
c07f9fc5
FG
2511 entry.idx = iter->first;
2512 entry.data = iter->second;
7c673cae 2513
c07f9fc5 2514 if (!filter.empty() && entry.idx.compare(0, filter.size(), filter) != 0) {
28e407b8
AA
2515 /* we are skipping the rest of the entries */
2516 if (pmore) {
2517 *pmore = false;
2518 }
c07f9fc5
FG
2519 return count;
2520 }
7c673cae 2521
c07f9fc5 2522 CLS_LOG(20, "%s(): entry.idx=%s", __func__, escape_str(entry.idx).c_str());
7c673cae 2523
11fdf7f2 2524 auto biter = entry.data.cbegin();
7c673cae 2525
c07f9fc5
FG
2526 rgw_bucket_olh_entry e;
2527 try {
11fdf7f2 2528 decode(e, biter);
c07f9fc5
FG
2529 } catch (buffer::error& err) {
2530 CLS_LOG(0, "ERROR: %s(): failed to decode buffer (size=%d)", __func__, entry.data.length());
2531 return -EIO;
2532 }
7c673cae 2533
c07f9fc5 2534 if (!name.empty() && e.key.name != name) {
28e407b8
AA
2535 /* we are skipping the rest of the entries */
2536 if (pmore) {
2537 *pmore = false;
2538 }
c07f9fc5 2539 return count;
7c673cae 2540 }
c07f9fc5
FG
2541
2542 entries->push_back(entry);
2543 count++;
2544 start_key = entry.idx;
2545 }
7c673cae
FG
2546
2547 return count;
2548}
2549
2550static int rgw_bi_list_op(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
2551{
2552 // decode request
2553 rgw_cls_bi_list_op op;
11fdf7f2 2554 auto iter = in->cbegin();
7c673cae 2555 try {
11fdf7f2 2556 decode(op, iter);
7c673cae
FG
2557 } catch (buffer::error& err) {
2558 CLS_LOG(0, "ERROR: %s(): failed to decode request", __func__);
2559 return -EINVAL;
2560 }
2561
2562 rgw_cls_bi_list_ret op_ret;
2563
2564 string filter = op.name;
2565#define MAX_BI_LIST_ENTRIES 1000
c07f9fc5 2566 int32_t max = (op.max < MAX_BI_LIST_ENTRIES ? op.max : MAX_BI_LIST_ENTRIES);
7c673cae 2567 string start_key = op.marker;
c07f9fc5 2568 bool more;
28e407b8 2569 int ret = list_plain_entries(hctx, op.name, op.marker, max, &op_ret.entries, &more);
7c673cae 2570 if (ret < 0) {
11fdf7f2 2571 CLS_LOG(0, "ERROR: %s(): list_plain_entries returned ret=%d", __func__, ret);
7c673cae
FG
2572 return ret;
2573 }
2574 int count = ret;
2575
2576 CLS_LOG(20, "found %d plain entries", count);
2577
c07f9fc5
FG
2578 if (!more) {
2579 ret = list_instance_entries(hctx, op.name, op.marker, max - count, &op_ret.entries, &more);
2580 if (ret < 0) {
11fdf7f2 2581 CLS_LOG(0, "ERROR: %s(): list_instance_entries returned ret=%d", __func__, ret);
c07f9fc5
FG
2582 return ret;
2583 }
2584
2585 count += ret;
7c673cae
FG
2586 }
2587
c07f9fc5
FG
2588 if (!more) {
2589 ret = list_olh_entries(hctx, op.name, op.marker, max - count, &op_ret.entries, &more);
2590 if (ret < 0) {
11fdf7f2 2591 CLS_LOG(0, "ERROR: %s(): list_olh_entries returned ret=%d", __func__, ret);
c07f9fc5
FG
2592 return ret;
2593 }
7c673cae 2594
c07f9fc5 2595 count += ret;
7c673cae
FG
2596 }
2597
c07f9fc5 2598 op_ret.is_truncated = (count >= max) || more;
1adf2230 2599 while (count > max) {
7c673cae
FG
2600 op_ret.entries.pop_back();
2601 count--;
2602 }
2603
11fdf7f2 2604 encode(op_ret, *out);
7c673cae
FG
2605
2606 return 0;
2607}
2608
2609int bi_log_record_decode(bufferlist& bl, rgw_bi_log_entry& e)
2610{
11fdf7f2 2611 auto iter = bl.cbegin();
7c673cae 2612 try {
11fdf7f2 2613 decode(e, iter);
7c673cae
FG
2614 } catch (buffer::error& err) {
2615 CLS_LOG(0, "ERROR: failed to decode rgw_bi_log_entry");
2616 return -EIO;
2617 }
2618 return 0;
2619}
2620
2621static int bi_log_iterate_entries(cls_method_context_t hctx, const string& marker, const string& end_marker,
2622 string& key_iter, uint32_t max_entries, bool *truncated,
2623 int (*cb)(cls_method_context_t, const string&, rgw_bi_log_entry&, void *),
2624 void *param)
2625{
2626 CLS_LOG(10, "bi_log_iterate_range");
2627
2628 map<string, bufferlist> keys;
2629 string filter_prefix, end_key;
2630 uint32_t i = 0;
2631 string key;
2632
2633 if (truncated)
2634 *truncated = false;
2635
2636 string start_key;
2637 if (key_iter.empty()) {
2638 key = BI_PREFIX_CHAR;
2639 key.append(bucket_index_prefixes[BI_BUCKET_LOG_INDEX]);
2640 key.append(marker);
2641
2642 start_key = key;
2643 } else {
2644 start_key = key_iter;
2645 }
2646
2647 if (end_marker.empty()) {
2648 end_key = BI_PREFIX_CHAR;
2649 end_key.append(bucket_index_prefixes[BI_BUCKET_LOG_INDEX + 1]);
2650 } else {
2651 end_key = BI_PREFIX_CHAR;
2652 end_key.append(bucket_index_prefixes[BI_BUCKET_LOG_INDEX]);
2653 end_key.append(end_marker);
2654 }
2655
11fdf7f2 2656 CLS_LOG(10, "bi_log_iterate_entries start_key=%s end_key=%s\n", start_key.c_str(), end_key.c_str());
7c673cae
FG
2657
2658 string filter;
2659
c07f9fc5
FG
2660 int ret = cls_cxx_map_get_vals(hctx, start_key, filter, max_entries, &keys, truncated);
2661 if (ret < 0)
2662 return ret;
7c673cae 2663
c07f9fc5
FG
2664 map<string, bufferlist>::iterator iter = keys.begin();
2665 if (iter == keys.end())
2666 return 0;
7c673cae 2667
c07f9fc5 2668 uint32_t num_keys = keys.size();
7c673cae 2669
c07f9fc5
FG
2670 for (; iter != keys.end(); ++iter,++i) {
2671 const string& key = iter->first;
2672 rgw_bi_log_entry e;
7c673cae 2673
11fdf7f2 2674 CLS_LOG(10, "bi_log_iterate_entries key=%s bl.length=%d\n", key.c_str(), (int)iter->second.length());
7c673cae 2675
c07f9fc5
FG
2676 if (key.compare(end_key) > 0) {
2677 key_iter = key;
94b18763
FG
2678 if (truncated) {
2679 *truncated = false;
2680 }
c07f9fc5
FG
2681 return 0;
2682 }
7c673cae 2683
c07f9fc5
FG
2684 ret = bi_log_record_decode(iter->second, e);
2685 if (ret < 0)
2686 return ret;
7c673cae 2687
c07f9fc5
FG
2688 ret = cb(hctx, key, e, param);
2689 if (ret < 0)
2690 return ret;
7c673cae 2691
c07f9fc5
FG
2692 if (i == num_keys - 1) {
2693 key_iter = key;
7c673cae 2694 }
c07f9fc5
FG
2695 }
2696
7c673cae
FG
2697 return 0;
2698}
2699
2700static int bi_log_list_cb(cls_method_context_t hctx, const string& key, rgw_bi_log_entry& info, void *param)
2701{
2702 list<rgw_bi_log_entry> *l = (list<rgw_bi_log_entry> *)param;
2703 l->push_back(info);
2704 return 0;
2705}
2706
2707static int bi_log_list_entries(cls_method_context_t hctx, const string& marker,
2708 uint32_t max, list<rgw_bi_log_entry>& entries, bool *truncated)
2709{
2710 string key_iter;
2711 string end_marker;
2712 int ret = bi_log_iterate_entries(hctx, marker, end_marker,
2713 key_iter, max, truncated,
2714 bi_log_list_cb, &entries);
2715 return ret;
2716}
2717
2718static int rgw_bi_log_list(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
2719{
11fdf7f2 2720 auto in_iter = in->cbegin();
7c673cae
FG
2721
2722 cls_rgw_bi_log_list_op op;
2723 try {
11fdf7f2 2724 decode(op, in_iter);
7c673cae
FG
2725 } catch (buffer::error& err) {
2726 CLS_LOG(1, "ERROR: rgw_bi_log_list(): failed to decode entry\n");
2727 return -EINVAL;
2728 }
2729
2730 cls_rgw_bi_log_list_ret op_ret;
2731 int ret = bi_log_list_entries(hctx, op.marker, op.max, op_ret.entries, &op_ret.truncated);
2732 if (ret < 0)
2733 return ret;
2734
11fdf7f2 2735 encode(op_ret, *out);
7c673cae
FG
2736
2737 return 0;
2738}
2739
2740static int bi_log_list_trim_cb(cls_method_context_t hctx, const string& key, rgw_bi_log_entry& info, void *param)
2741{
2742 list<rgw_bi_log_entry> *entries = (list<rgw_bi_log_entry> *)param;
2743
2744 entries->push_back(info);
2745 return 0;
2746}
2747
2748static int bi_log_remove_entry(cls_method_context_t hctx, rgw_bi_log_entry& entry)
2749{
2750 string key;
2751 key = BI_PREFIX_CHAR;
2752 key.append(bucket_index_prefixes[BI_BUCKET_LOG_INDEX]);
2753 key.append(entry.id);
2754 return cls_cxx_map_remove_key(hctx, key);
2755}
2756
2757static int bi_log_list_trim_entries(cls_method_context_t hctx,
2758 const string& start_marker, const string& end_marker,
2759 list<rgw_bi_log_entry>& entries, bool *truncated)
2760{
2761 string key_iter;
2762#define MAX_TRIM_ENTRIES 1000 /* max entries to trim in a single operation */
2763 int ret = bi_log_iterate_entries(hctx, start_marker, end_marker,
2764 key_iter, MAX_TRIM_ENTRIES, truncated,
2765 bi_log_list_trim_cb, &entries);
2766 return ret;
2767}
2768
2769static int rgw_bi_log_trim(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
2770{
11fdf7f2 2771 auto in_iter = in->cbegin();
7c673cae
FG
2772
2773 cls_rgw_bi_log_trim_op op;
2774 try {
11fdf7f2 2775 decode(op, in_iter);
7c673cae
FG
2776 } catch (buffer::error& err) {
2777 CLS_LOG(1, "ERROR: rgw_bi_log_list(): failed to decode entry\n");
2778 return -EINVAL;
2779 }
2780
2781 cls_rgw_bi_log_list_ret op_ret;
2782 list<rgw_bi_log_entry> entries;
2783#define MAX_TRIM_ENTRIES 1000 /* don't do more than that in a single operation */
2784 bool truncated;
2785 int ret = bi_log_list_trim_entries(hctx, op.start_marker, op.end_marker, entries, &truncated);
2786 if (ret < 0)
2787 return ret;
2788
2789 if (entries.empty())
2790 return -ENODATA;
2791
2792 list<rgw_bi_log_entry>::iterator iter;
2793 for (iter = entries.begin(); iter != entries.end(); ++iter) {
2794 rgw_bi_log_entry& entry = *iter;
2795
2796 ret = bi_log_remove_entry(hctx, entry);
2797 if (ret < 0)
2798 return ret;
2799 }
2800
2801 return 0;
2802}
2803
c07f9fc5
FG
2804static int rgw_bi_log_resync(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
2805{
11fdf7f2 2806 rgw_bucket_dir_header header;
c07f9fc5
FG
2807 int rc = read_bucket_header(hctx, &header);
2808 if (rc < 0) {
2809 CLS_LOG(1, "ERROR: rgw_bucket_complete_op(): failed to read header\n");
2810 return rc;
2811 }
2812
2813 bufferlist bl;
2814
11fdf7f2 2815 rgw_bi_log_entry entry;
c07f9fc5
FG
2816
2817 entry.timestamp = real_clock::now();
2818 entry.op = RGWModifyOp::CLS_RGW_OP_RESYNC;
2819 entry.state = RGWPendingState::CLS_RGW_STATE_COMPLETE;
2820
2821 string key;
2822 bi_log_index_key(hctx, key, entry.id, header.ver);
2823
11fdf7f2 2824 encode(entry, bl);
c07f9fc5
FG
2825
2826 if (entry.id > header.max_marker)
2827 header.max_marker = entry.id;
2828
2829 header.syncstopped = false;
2830
2831 rc = cls_cxx_map_set_val(hctx, key, &bl);
2832 if (rc < 0)
2833 return rc;
2834
2835 return write_bucket_header(hctx, &header);
2836}
2837
2838static int rgw_bi_log_stop(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
2839{
11fdf7f2 2840 rgw_bucket_dir_header header;
c07f9fc5
FG
2841 int rc = read_bucket_header(hctx, &header);
2842 if (rc < 0) {
2843 CLS_LOG(1, "ERROR: rgw_bucket_complete_op(): failed to read header\n");
2844 return rc;
2845 }
2846
2847 bufferlist bl;
2848
11fdf7f2 2849 rgw_bi_log_entry entry;
c07f9fc5
FG
2850
2851 entry.timestamp = real_clock::now();
2852 entry.op = RGWModifyOp::CLS_RGW_OP_SYNCSTOP;
2853 entry.state = RGWPendingState::CLS_RGW_STATE_COMPLETE;
2854
2855 string key;
2856 bi_log_index_key(hctx, key, entry.id, header.ver);
2857
11fdf7f2 2858 encode(entry, bl);
c07f9fc5
FG
2859
2860 if (entry.id > header.max_marker)
2861 header.max_marker = entry.id;
2862 header.syncstopped = true;
2863
2864 rc = cls_cxx_map_set_val(hctx, key, &bl);
2865 if (rc < 0)
2866 return rc;
2867
2868 return write_bucket_header(hctx, &header);
2869}
2870
2871
7c673cae
FG
2872static void usage_record_prefix_by_time(uint64_t epoch, string& key)
2873{
2874 char buf[32];
2875 snprintf(buf, sizeof(buf), "%011llu", (long long unsigned)epoch);
2876 key = buf;
2877}
2878
11fdf7f2 2879static void usage_record_prefix_by_user(const string& user, uint64_t epoch, string& key)
7c673cae
FG
2880{
2881 char buf[user.size() + 32];
2882 snprintf(buf, sizeof(buf), "%s_%011llu_", user.c_str(), (long long unsigned)epoch);
2883 key = buf;
2884}
2885
11fdf7f2 2886static void usage_record_name_by_time(uint64_t epoch, const string& user, const string& bucket, string& key)
7c673cae
FG
2887{
2888 char buf[32 + user.size() + bucket.size()];
2889 snprintf(buf, sizeof(buf), "%011llu_%s_%s", (long long unsigned)epoch, user.c_str(), bucket.c_str());
2890 key = buf;
2891}
2892
11fdf7f2 2893static void usage_record_name_by_user(const string& user, uint64_t epoch, const string& bucket, string& key)
7c673cae
FG
2894{
2895 char buf[32 + user.size() + bucket.size()];
2896 snprintf(buf, sizeof(buf), "%s_%011llu_%s", user.c_str(), (long long unsigned)epoch, bucket.c_str());
2897 key = buf;
2898}
2899
2900static int usage_record_decode(bufferlist& record_bl, rgw_usage_log_entry& e)
2901{
11fdf7f2 2902 auto kiter = record_bl.cbegin();
7c673cae 2903 try {
11fdf7f2 2904 decode(e, kiter);
7c673cae
FG
2905 } catch (buffer::error& err) {
2906 CLS_LOG(1, "ERROR: usage_record_decode(): failed to decode record_bl\n");
2907 return -EINVAL;
2908 }
2909
2910 return 0;
2911}
2912
2913int rgw_user_usage_log_add(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
2914{
2915 CLS_LOG(10, "rgw_user_usage_log_add()");
2916
11fdf7f2 2917 auto in_iter = in->cbegin();
7c673cae
FG
2918 rgw_cls_usage_log_add_op op;
2919
2920 try {
11fdf7f2 2921 decode(op, in_iter);
7c673cae
FG
2922 } catch (buffer::error& err) {
2923 CLS_LOG(1, "ERROR: rgw_user_usage_log_add(): failed to decode request\n");
2924 return -EINVAL;
2925 }
2926
2927 rgw_usage_log_info& info = op.info;
2928 vector<rgw_usage_log_entry>::iterator iter;
2929
2930 for (iter = info.entries.begin(); iter != info.entries.end(); ++iter) {
2931 rgw_usage_log_entry& entry = *iter;
2932 string key_by_time;
2933
2934 rgw_user *puser = (entry.payer.empty() ? &entry.owner : &entry.payer);
2935
2936 usage_record_name_by_time(entry.epoch, puser->to_str(), entry.bucket, key_by_time);
2937
2938 CLS_LOG(10, "rgw_user_usage_log_add user=%s bucket=%s\n", puser->to_str().c_str(), entry.bucket.c_str());
2939
2940 bufferlist record_bl;
2941 int ret = cls_cxx_map_get_val(hctx, key_by_time, &record_bl);
2942 if (ret < 0 && ret != -ENOENT) {
2943 CLS_LOG(1, "ERROR: rgw_user_usage_log_add(): cls_cxx_map_read_key returned %d\n", ret);
2944 return -EINVAL;
2945 }
2946 if (ret >= 0) {
2947 rgw_usage_log_entry e;
2948 ret = usage_record_decode(record_bl, e);
2949 if (ret < 0)
2950 return ret;
2951 CLS_LOG(10, "rgw_user_usage_log_add aggregating existing bucket\n");
2952 entry.aggregate(e);
2953 }
2954
2955 bufferlist new_record_bl;
11fdf7f2 2956 encode(entry, new_record_bl);
7c673cae
FG
2957 ret = cls_cxx_map_set_val(hctx, key_by_time, &new_record_bl);
2958 if (ret < 0)
2959 return ret;
2960
2961 string key_by_user;
2962 usage_record_name_by_user(puser->to_str(), entry.epoch, entry.bucket, key_by_user);
2963 ret = cls_cxx_map_set_val(hctx, key_by_user, &new_record_bl);
2964 if (ret < 0)
2965 return ret;
2966 }
2967
2968 return 0;
2969}
2970
11fdf7f2
TL
2971static int usage_iterate_range(cls_method_context_t hctx, uint64_t start, uint64_t end, const string& user,
2972 const string& bucket, string& key_iter, uint32_t max_entries, bool *truncated,
7c673cae
FG
2973 int (*cb)(cls_method_context_t, const string&, rgw_usage_log_entry&, void *),
2974 void *param)
2975{
2976 CLS_LOG(10, "usage_iterate_range");
2977
2978 map<string, bufferlist> keys;
7c673cae
FG
2979 string filter_prefix;
2980 string start_key, end_key;
2981 bool by_user = !user.empty();
7c673cae 2982 string user_key;
3efd9988 2983 bool truncated_status = false;
7c673cae 2984
11fdf7f2 2985 ceph_assert(truncated != nullptr);
94b18763 2986
7c673cae
FG
2987 if (!by_user) {
2988 usage_record_prefix_by_time(end, end_key);
2989 } else {
2990 user_key = user;
2991 user_key.append("_");
2992 }
2993
2994 if (key_iter.empty()) {
2995 if (by_user) {
2996 usage_record_prefix_by_user(user, start, start_key);
2997 } else {
2998 usage_record_prefix_by_time(start, start_key);
2999 }
3000 } else {
3001 start_key = key_iter;
3002 }
3003
c07f9fc5 3004 CLS_LOG(20, "usage_iterate_range start_key=%s", start_key.c_str());
3efd9988 3005 int ret = cls_cxx_map_get_vals(hctx, start_key, filter_prefix, max_entries, &keys, &truncated_status);
c07f9fc5
FG
3006 if (ret < 0)
3007 return ret;
7c673cae 3008
94b18763 3009 *truncated = truncated_status;
3efd9988 3010
c07f9fc5
FG
3011 map<string, bufferlist>::iterator iter = keys.begin();
3012 if (iter == keys.end())
3013 return 0;
7c673cae 3014
92f5a8d4 3015 for (; iter != keys.end(); ++iter) {
c07f9fc5
FG
3016 const string& key = iter->first;
3017 rgw_usage_log_entry e;
7c673cae 3018
92f5a8d4 3019 key_iter = key;
c07f9fc5
FG
3020 if (!by_user && key.compare(end_key) >= 0) {
3021 CLS_LOG(20, "usage_iterate_range reached key=%s, done", key.c_str());
94b18763 3022 *truncated = false;
b32b8144 3023 key_iter = key;
c07f9fc5
FG
3024 return 0;
3025 }
7c673cae 3026
c07f9fc5
FG
3027 if (by_user && key.compare(0, user_key.size(), user_key) != 0) {
3028 CLS_LOG(20, "usage_iterate_range reached key=%s, done", key.c_str());
94b18763 3029 *truncated = false;
b32b8144 3030 key_iter = key;
c07f9fc5
FG
3031 return 0;
3032 }
7c673cae 3033
c07f9fc5
FG
3034 ret = usage_record_decode(iter->second, e);
3035 if (ret < 0)
3036 return ret;
7c673cae 3037
11fdf7f2
TL
3038 if (!bucket.empty() && bucket.compare(e.bucket))
3039 continue;
3040
c07f9fc5
FG
3041 if (e.epoch < start)
3042 continue;
7c673cae 3043
c07f9fc5 3044 /* keys are sorted by epoch, so once we're past end we're done */
94b18763
FG
3045 if (e.epoch >= end) {
3046 *truncated = false;
c07f9fc5 3047 return 0;
94b18763 3048 }
7c673cae 3049
c07f9fc5
FG
3050 ret = cb(hctx, key, e, param);
3051 if (ret < 0)
3052 return ret;
c07f9fc5 3053 }
7c673cae
FG
3054 return 0;
3055}
3056
3057static int usage_log_read_cb(cls_method_context_t hctx, const string& key, rgw_usage_log_entry& entry, void *param)
3058{
3059 map<rgw_user_bucket, rgw_usage_log_entry> *usage = (map<rgw_user_bucket, rgw_usage_log_entry> *)param;
3060 rgw_user *puser;
3061 if (!entry.payer.empty()) {
3062 puser = &entry.payer;
3063 } else {
3064 puser = &entry.owner;
3065 }
3066 rgw_user_bucket ub(puser->to_str(), entry.bucket);
3067 rgw_usage_log_entry& le = (*usage)[ub];
3068 le.aggregate(entry);
3069
3070 return 0;
3071}
3072
3073int rgw_user_usage_log_read(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3074{
3075 CLS_LOG(10, "rgw_user_usage_log_read()");
3076
11fdf7f2 3077 auto in_iter = in->cbegin();
7c673cae
FG
3078 rgw_cls_usage_log_read_op op;
3079
3080 try {
11fdf7f2 3081 decode(op, in_iter);
7c673cae
FG
3082 } catch (buffer::error& err) {
3083 CLS_LOG(1, "ERROR: rgw_user_usage_log_read(): failed to decode request\n");
3084 return -EINVAL;
3085 }
3086
3087 rgw_cls_usage_log_read_ret ret_info;
3088 map<rgw_user_bucket, rgw_usage_log_entry> *usage = &ret_info.usage;
3089 string iter = op.iter;
3090#define MAX_ENTRIES 1000
3091 uint32_t max_entries = (op.max_entries ? op.max_entries : MAX_ENTRIES);
11fdf7f2 3092 int ret = usage_iterate_range(hctx, op.start_epoch, op.end_epoch, op.owner, op.bucket, iter, max_entries, &ret_info.truncated, usage_log_read_cb, (void *)usage);
7c673cae
FG
3093 if (ret < 0)
3094 return ret;
3095
3096 if (ret_info.truncated)
3097 ret_info.next_iter = iter;
3098
11fdf7f2 3099 encode(ret_info, *out);
7c673cae
FG
3100 return 0;
3101}
3102
3103static int usage_log_trim_cb(cls_method_context_t hctx, const string& key, rgw_usage_log_entry& entry, void *param)
3104{
94b18763
FG
3105 bool *found = (bool *)param;
3106 if (found) {
3107 *found = true;
3108 }
7c673cae
FG
3109 string key_by_time;
3110 string key_by_user;
3111
3112 string o = entry.owner.to_str();
3113 usage_record_name_by_time(entry.epoch, o, entry.bucket, key_by_time);
3114 usage_record_name_by_user(o, entry.epoch, entry.bucket, key_by_user);
3115
3116 int ret = cls_cxx_map_remove_key(hctx, key_by_time);
3117 if (ret < 0)
3118 return ret;
3119
3120 return cls_cxx_map_remove_key(hctx, key_by_user);
3121}
3122
3123int rgw_user_usage_log_trim(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3124{
3125 CLS_LOG(10, "rgw_user_usage_log_trim()");
3126
3127 /* only continue if object exists! */
3128 int ret = cls_cxx_stat(hctx, NULL, NULL);
3129 if (ret < 0)
3130 return ret;
3131
11fdf7f2 3132 auto in_iter = in->cbegin();
7c673cae
FG
3133 rgw_cls_usage_log_trim_op op;
3134
3135 try {
11fdf7f2 3136 decode(op, in_iter);
7c673cae
FG
3137 } catch (buffer::error& err) {
3138 CLS_LOG(1, "ERROR: rgw_user_log_usage_log_trim(): failed to decode request\n");
3139 return -EINVAL;
3140 }
3141
3142 string iter;
c07f9fc5 3143 bool more;
94b18763 3144 bool found = false;
c07f9fc5 3145#define MAX_USAGE_TRIM_ENTRIES 128
11fdf7f2 3146 ret = usage_iterate_range(hctx, op.start_epoch, op.end_epoch, op.user, op.bucket, iter, MAX_USAGE_TRIM_ENTRIES, &more, usage_log_trim_cb, (void *)&found);
7c673cae
FG
3147 if (ret < 0)
3148 return ret;
3149
94b18763 3150 if (!more && !found)
b32b8144
FG
3151 return -ENODATA;
3152
7c673cae
FG
3153 return 0;
3154}
3155
11fdf7f2
TL
3156int rgw_usage_log_clear(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3157{
3158 CLS_LOG(10,"%s", __func__);
3159
3160 int ret = cls_cxx_map_clear(hctx);
3161 /* if object doesn't exist all the logs are cleared anyway */
3162 if (ret == -ENOENT)
3163 ret = 0;
3164
3165 return ret;
3166}
7c673cae
FG
3167/*
3168 * We hold the garbage collection chain data under two different indexes: the first 'name' index
3169 * keeps them under a unique tag that represents the chains, and a second 'time' index keeps
3170 * them by their expiration timestamp
3171 */
3172#define GC_OBJ_NAME_INDEX 0
3173#define GC_OBJ_TIME_INDEX 1
3174
3175static string gc_index_prefixes[] = { "0_",
3176 "1_" };
3177
3178static void prepend_index_prefix(const string& src, int index, string *dest)
3179{
3180 *dest = gc_index_prefixes[index];
3181 dest->append(src);
3182}
3183
3184static int gc_omap_get(cls_method_context_t hctx, int type, const string& key, cls_rgw_gc_obj_info *info)
3185{
3186 string index;
3187 prepend_index_prefix(key, type, &index);
3188
11fdf7f2 3189 int ret = read_omap_entry(hctx, index, info);
7c673cae
FG
3190 if (ret < 0)
3191 return ret;
3192
7c673cae
FG
3193 return 0;
3194}
3195
3196static int gc_omap_set(cls_method_context_t hctx, int type, const string& key, const cls_rgw_gc_obj_info *info)
3197{
3198 bufferlist bl;
11fdf7f2 3199 encode(*info, bl);
7c673cae
FG
3200
3201 string index = gc_index_prefixes[type];
3202 index.append(key);
3203
3204 int ret = cls_cxx_map_set_val(hctx, index, &bl);
3205 if (ret < 0)
3206 return ret;
3207
3208 return 0;
3209}
3210
3211static int gc_omap_remove(cls_method_context_t hctx, int type, const string& key)
3212{
3213 string index = gc_index_prefixes[type];
3214 index.append(key);
3215
3216 int ret = cls_cxx_map_remove_key(hctx, index);
3217 if (ret < 0)
3218 return ret;
3219
3220 return 0;
3221}
3222
3223static bool key_in_index(const string& key, int index_type)
3224{
3225 const string& prefix = gc_index_prefixes[index_type];
3226 return (key.compare(0, prefix.size(), prefix) == 0);
3227}
3228
3229
3230static int gc_update_entry(cls_method_context_t hctx, uint32_t expiration_secs,
3231 cls_rgw_gc_obj_info& info)
3232{
3233 cls_rgw_gc_obj_info old_info;
3234 int ret = gc_omap_get(hctx, GC_OBJ_NAME_INDEX, info.tag, &old_info);
3235 if (ret == 0) {
3236 string key;
3237 get_time_key(old_info.time, &key);
3238 ret = gc_omap_remove(hctx, GC_OBJ_TIME_INDEX, key);
3239 if (ret < 0 && ret != -ENOENT) {
3240 CLS_LOG(0, "ERROR: failed to remove key=%s\n", key.c_str());
3241 return ret;
3242 }
3243 }
11fdf7f2
TL
3244
3245 // calculate time and time key
7c673cae
FG
3246 info.time = ceph::real_clock::now();
3247 info.time += make_timespan(expiration_secs);
11fdf7f2
TL
3248 string time_key;
3249 get_time_key(info.time, &time_key);
3250
3251 if (info.chain.objs.empty()) {
3252 CLS_LOG(0,
3253 "WARNING: %s setting GC log entry with zero-length chain, "
3254 "tag='%s', timekey='%s'",
3255 __func__, info.tag.c_str(), time_key.c_str());
3256 }
3257
7c673cae
FG
3258 ret = gc_omap_set(hctx, GC_OBJ_NAME_INDEX, info.tag, &info);
3259 if (ret < 0)
3260 return ret;
3261
11fdf7f2 3262 ret = gc_omap_set(hctx, GC_OBJ_TIME_INDEX, time_key, &info);
7c673cae
FG
3263 if (ret < 0)
3264 goto done_err;
3265
3266 return 0;
3267
3268done_err:
11fdf7f2
TL
3269
3270 CLS_LOG(0, "ERROR: gc_set_entry error info.tag=%s, ret=%d\n",
3271 info.tag.c_str(), ret);
7c673cae 3272 gc_omap_remove(hctx, GC_OBJ_NAME_INDEX, info.tag);
11fdf7f2 3273
7c673cae
FG
3274 return ret;
3275}
3276
3277static int gc_defer_entry(cls_method_context_t hctx, const string& tag, uint32_t expiration_secs)
3278{
3279 cls_rgw_gc_obj_info info;
3280 int ret = gc_omap_get(hctx, GC_OBJ_NAME_INDEX, tag, &info);
3281 if (ret == -ENOENT)
3282 return 0;
3283 if (ret < 0)
3284 return ret;
3285 return gc_update_entry(hctx, expiration_secs, info);
3286}
3287
3288int gc_record_decode(bufferlist& bl, cls_rgw_gc_obj_info& e)
3289{
11fdf7f2 3290 auto iter = bl.cbegin();
7c673cae 3291 try {
11fdf7f2 3292 decode(e, iter);
7c673cae
FG
3293 } catch (buffer::error& err) {
3294 CLS_LOG(0, "ERROR: failed to decode cls_rgw_gc_obj_info");
3295 return -EIO;
3296 }
3297 return 0;
3298}
3299
3300static int rgw_cls_gc_set_entry(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3301{
11fdf7f2 3302 auto in_iter = in->cbegin();
7c673cae
FG
3303
3304 cls_rgw_gc_set_entry_op op;
3305 try {
11fdf7f2 3306 decode(op, in_iter);
7c673cae
FG
3307 } catch (buffer::error& err) {
3308 CLS_LOG(1, "ERROR: rgw_cls_gc_set_entry(): failed to decode entry\n");
3309 return -EINVAL;
3310 }
3311
3312 return gc_update_entry(hctx, op.expiration_secs, op.info);
3313}
3314
3315static int rgw_cls_gc_defer_entry(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3316{
11fdf7f2 3317 auto in_iter = in->cbegin();
7c673cae
FG
3318
3319 cls_rgw_gc_defer_entry_op op;
3320 try {
11fdf7f2 3321 decode(op, in_iter);
7c673cae
FG
3322 } catch (buffer::error& err) {
3323 CLS_LOG(1, "ERROR: rgw_cls_gc_defer_entry(): failed to decode entry\n");
3324 return -EINVAL;
3325 }
3326
3327 return gc_defer_entry(hctx, op.tag, op.expiration_secs);
3328}
3329
11fdf7f2
TL
3330static int gc_iterate_entries(cls_method_context_t hctx,
3331 const string& marker,
3332 bool expired_only,
3333 string& out_marker,
3334 uint32_t max_entries,
3335 bool *truncated,
3336 int (*cb)(cls_method_context_t,
3337 const string&,
3338 cls_rgw_gc_obj_info&,
3339 void *),
7c673cae
FG
3340 void *param)
3341{
11fdf7f2 3342 CLS_LOG(10, "gc_iterate_entries");
7c673cae
FG
3343
3344 map<string, bufferlist> keys;
3345 string filter_prefix, end_key;
7c673cae
FG
3346 string key;
3347
3348 if (truncated)
3349 *truncated = false;
3350
3351 string start_key;
31f18b77 3352 if (marker.empty()) {
7c673cae
FG
3353 prepend_index_prefix(marker, GC_OBJ_TIME_INDEX, &start_key);
3354 } else {
31f18b77 3355 start_key = marker;
7c673cae
FG
3356 }
3357
3358 if (expired_only) {
3359 real_time now = ceph::real_clock::now();
3360 string now_str;
3361 get_time_key(now, &now_str);
3362 prepend_index_prefix(now_str, GC_OBJ_TIME_INDEX, &end_key);
3363
91327a77 3364 CLS_LOG(10, "gc_iterate_entries end_key=%s\n", end_key.c_str());
7c673cae
FG
3365 }
3366
3367 string filter;
3368
11fdf7f2
TL
3369 int ret = cls_cxx_map_get_vals(hctx, start_key, filter, max_entries,
3370 &keys, truncated);
c07f9fc5
FG
3371 if (ret < 0)
3372 return ret;
7c673cae 3373
c07f9fc5 3374 map<string, bufferlist>::iterator iter = keys.begin();
11fdf7f2
TL
3375 if (iter == keys.end()) {
3376 // if keys empty must not come back as truncated
3377 ceph_assert(!truncated || !(*truncated));
c07f9fc5 3378 return 0;
11fdf7f2 3379 }
7c673cae 3380
11fdf7f2
TL
3381 const string* last_key = nullptr; // last key processed, for end-marker
3382 for (; iter != keys.end(); ++iter) {
c07f9fc5
FG
3383 const string& key = iter->first;
3384 cls_rgw_gc_obj_info e;
7c673cae 3385
c07f9fc5 3386 CLS_LOG(10, "gc_iterate_entries key=%s\n", key.c_str());
7c673cae 3387
1adf2230
AA
3388 if (!end_key.empty() && key.compare(end_key) >= 0) {
3389 if (truncated)
3390 *truncated = false;
c07f9fc5 3391 return 0;
1adf2230 3392 }
7c673cae 3393
11fdf7f2
TL
3394 if (!key_in_index(key, GC_OBJ_TIME_INDEX)) {
3395 if (truncated)
3396 *truncated = false;
c07f9fc5 3397 return 0;
11fdf7f2 3398 }
7c673cae 3399
c07f9fc5
FG
3400 ret = gc_record_decode(iter->second, e);
3401 if (ret < 0)
3402 return ret;
7c673cae 3403
c07f9fc5
FG
3404 ret = cb(hctx, key, e, param);
3405 if (ret < 0)
3406 return ret;
11fdf7f2
TL
3407 last_key = &(iter->first); // update when callback successful
3408 }
7c673cae 3409
11fdf7f2
TL
3410 // set the out marker if either caller does not capture truncated or
3411 // if they do capture and we are truncated
3412 if (!truncated || *truncated) {
3413 assert(last_key);
3414 out_marker = *last_key;
c07f9fc5
FG
3415 }
3416
7c673cae
FG
3417 return 0;
3418}
3419
3420static int gc_list_cb(cls_method_context_t hctx, const string& key, cls_rgw_gc_obj_info& info, void *param)
3421{
3422 list<cls_rgw_gc_obj_info> *l = (list<cls_rgw_gc_obj_info> *)param;
3423 l->push_back(info);
3424 return 0;
3425}
3426
3427static int gc_list_entries(cls_method_context_t hctx, const string& marker,
3428 uint32_t max, bool expired_only,
31f18b77 3429 list<cls_rgw_gc_obj_info>& entries, bool *truncated, string& next_marker)
7c673cae 3430{
7c673cae 3431 int ret = gc_iterate_entries(hctx, marker, expired_only,
31f18b77 3432 next_marker, max, truncated,
7c673cae
FG
3433 gc_list_cb, &entries);
3434 return ret;
3435}
3436
3437static int rgw_cls_gc_list(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3438{
11fdf7f2 3439 auto in_iter = in->cbegin();
7c673cae
FG
3440
3441 cls_rgw_gc_list_op op;
3442 try {
11fdf7f2 3443 decode(op, in_iter);
7c673cae
FG
3444 } catch (buffer::error& err) {
3445 CLS_LOG(1, "ERROR: rgw_cls_gc_list(): failed to decode entry\n");
3446 return -EINVAL;
3447 }
3448
3449 cls_rgw_gc_list_ret op_ret;
c07f9fc5
FG
3450#define GC_LIST_ENTRIES_DEFAULT 128
3451 int ret = gc_list_entries(hctx, op.marker, (op.max ? op.max : GC_LIST_ENTRIES_DEFAULT), op.expired_only,
31f18b77 3452 op_ret.entries, &op_ret.truncated, op_ret.next_marker);
7c673cae
FG
3453 if (ret < 0)
3454 return ret;
3455
11fdf7f2 3456 encode(op_ret, *out);
7c673cae
FG
3457
3458 return 0;
3459}
3460
11fdf7f2 3461static int gc_remove(cls_method_context_t hctx, vector<string>& tags)
7c673cae 3462{
11fdf7f2 3463 for (auto iter = tags.begin(); iter != tags.end(); ++iter) {
7c673cae
FG
3464 string& tag = *iter;
3465 cls_rgw_gc_obj_info info;
3466 int ret = gc_omap_get(hctx, GC_OBJ_NAME_INDEX, tag, &info);
3467 if (ret == -ENOENT) {
3468 CLS_LOG(0, "couldn't find tag in name index tag=%s\n", tag.c_str());
3469 continue;
3470 }
3471
3472 if (ret < 0)
3473 return ret;
3474
3475 string time_key;
3476 get_time_key(info.time, &time_key);
3477 ret = gc_omap_remove(hctx, GC_OBJ_TIME_INDEX, time_key);
3478 if (ret < 0 && ret != -ENOENT)
3479 return ret;
3480 if (ret == -ENOENT) {
3481 CLS_LOG(0, "couldn't find key in time index key=%s\n", time_key.c_str());
3482 }
3483
3484 ret = gc_omap_remove(hctx, GC_OBJ_NAME_INDEX, tag);
3485 if (ret < 0 && ret != -ENOENT)
3486 return ret;
3487 }
3488
3489 return 0;
3490}
3491
3492static int rgw_cls_gc_remove(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3493{
11fdf7f2 3494 auto in_iter = in->cbegin();
7c673cae
FG
3495
3496 cls_rgw_gc_remove_op op;
3497 try {
11fdf7f2 3498 decode(op, in_iter);
7c673cae
FG
3499 } catch (buffer::error& err) {
3500 CLS_LOG(1, "ERROR: rgw_cls_gc_remove(): failed to decode entry\n");
3501 return -EINVAL;
3502 }
3503
3504 return gc_remove(hctx, op.tags);
3505}
3506
11fdf7f2
TL
3507static int rgw_cls_lc_get_entry(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3508{
3509 auto in_iter = in->cbegin();
3510
3511 cls_rgw_lc_get_entry_op op;
3512 try {
3513 decode(op, in_iter);
3514 } catch (buffer::error& err) {
3515 CLS_LOG(1, "ERROR: rgw_cls_lc_set_entry(): failed to decode entry\n");
3516 return -EINVAL;
3517 }
3518
3519 rgw_lc_entry_t lc_entry;
3520 int ret = read_omap_entry(hctx, op.marker, &lc_entry);
3521 if (ret < 0)
3522 return ret;
3523
3524 cls_rgw_lc_get_entry_ret op_ret(std::move(lc_entry));
3525 encode(op_ret, *out);
3526 return 0;
3527}
3528
3529
7c673cae
FG
3530static int rgw_cls_lc_set_entry(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3531{
11fdf7f2 3532 auto in_iter = in->cbegin();
7c673cae
FG
3533
3534 cls_rgw_lc_set_entry_op op;
3535 try {
11fdf7f2 3536 decode(op, in_iter);
7c673cae
FG
3537 } catch (buffer::error& err) {
3538 CLS_LOG(1, "ERROR: rgw_cls_lc_set_entry(): failed to decode entry\n");
3539 return -EINVAL;
3540 }
3541
3542 bufferlist bl;
11fdf7f2 3543 encode(op.entry, bl);
7c673cae
FG
3544
3545 int ret = cls_cxx_map_set_val(hctx, op.entry.first, &bl);
3546 return ret;
3547}
3548
3549static int rgw_cls_lc_rm_entry(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3550{
11fdf7f2 3551 auto in_iter = in->cbegin();
7c673cae
FG
3552
3553 cls_rgw_lc_rm_entry_op op;
3554 try {
11fdf7f2 3555 decode(op, in_iter);
7c673cae
FG
3556 } catch (buffer::error& err) {
3557 CLS_LOG(1, "ERROR: rgw_cls_lc_rm_entry(): failed to decode entry\n");
3558 return -EINVAL;
3559 }
3560
7c673cae
FG
3561 int ret = cls_cxx_map_remove_key(hctx, op.entry.first);
3562 return ret;
3563}
3564
3565static int rgw_cls_lc_get_next_entry(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3566{
11fdf7f2 3567 auto in_iter = in->cbegin();
7c673cae
FG
3568 cls_rgw_lc_get_next_entry_ret op_ret;
3569 cls_rgw_lc_get_next_entry_op op;
3570 try {
11fdf7f2 3571 decode(op, in_iter);
7c673cae
FG
3572 } catch (buffer::error& err) {
3573 CLS_LOG(1, "ERROR: rgw_cls_lc_get_next_entry: failed to decode op\n");
3574 return -EINVAL;
3575 }
3576
3577 map<string, bufferlist> vals;
3578 string filter_prefix;
c07f9fc5
FG
3579 bool more;
3580 int ret = cls_cxx_map_get_vals(hctx, op.marker, filter_prefix, 1, &vals, &more);
7c673cae
FG
3581 if (ret < 0)
3582 return ret;
3583 map<string, bufferlist>::iterator it;
3584 pair<string, int> entry;
3585 if (!vals.empty()) {
3586 it=vals.begin();
3587 in_iter = it->second.begin();
3588 try {
11fdf7f2 3589 decode(entry, in_iter);
7c673cae
FG
3590 } catch (buffer::error& err) {
3591 CLS_LOG(1, "ERROR: rgw_cls_lc_get_next_entry(): failed to decode entry\n");
3592 return -EIO;
3593 }
3594 }
3595 op_ret.entry = entry;
11fdf7f2 3596 encode(op_ret, *out);
7c673cae
FG
3597 return 0;
3598}
3599
3600static int rgw_cls_lc_list_entries(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3601{
3602 cls_rgw_lc_list_entries_op op;
11fdf7f2 3603 auto in_iter = in->cbegin();
7c673cae 3604 try {
11fdf7f2 3605 decode(op, in_iter);
7c673cae
FG
3606 } catch (buffer::error& err) {
3607 CLS_LOG(1, "ERROR: rgw_cls_lc_list_entries(): failed to decode op\n");
3608 return -EINVAL;
3609 }
3610
3611 cls_rgw_lc_list_entries_ret op_ret;
11fdf7f2 3612 bufferlist::const_iterator iter;
7c673cae
FG
3613 map<string, bufferlist> vals;
3614 string filter_prefix;
c07f9fc5 3615 int ret = cls_cxx_map_get_vals(hctx, op.marker, filter_prefix, op.max_entries, &vals, &op_ret.is_truncated);
7c673cae
FG
3616 if (ret < 0)
3617 return ret;
3618 map<string, bufferlist>::iterator it;
3619 pair<string, int> entry;
3620 for (it = vals.begin(); it != vals.end(); ++it) {
11fdf7f2 3621 iter = it->second.cbegin();
7c673cae 3622 try {
11fdf7f2 3623 decode(entry, iter);
7c673cae
FG
3624 } catch (buffer::error& err) {
3625 CLS_LOG(1, "ERROR: rgw_cls_lc_list_entries(): failed to decode entry\n");
3626 return -EIO;
3627 }
3628 op_ret.entries.insert(entry);
3629 }
11fdf7f2 3630 encode(op_ret, *out);
7c673cae
FG
3631 return 0;
3632}
3633
3634static int rgw_cls_lc_put_head(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3635{
11fdf7f2 3636 auto in_iter = in->cbegin();
7c673cae
FG
3637
3638 cls_rgw_lc_put_head_op op;
3639 try {
11fdf7f2 3640 decode(op, in_iter);
7c673cae
FG
3641 } catch (buffer::error& err) {
3642 CLS_LOG(1, "ERROR: rgw_cls_lc_put_head(): failed to decode entry\n");
3643 return -EINVAL;
3644 }
3645
3646 bufferlist bl;
11fdf7f2 3647 encode(op.head, bl);
7c673cae
FG
3648 int ret = cls_cxx_map_write_header(hctx,&bl);
3649 return ret;
3650}
3651
3652static int rgw_cls_lc_get_head(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3653{
3654 bufferlist bl;
3655 int ret = cls_cxx_map_read_header(hctx, &bl);
3656 if (ret < 0)
3657 return ret;
3658 cls_rgw_lc_obj_head head;
3659 if (bl.length() != 0) {
11fdf7f2 3660 auto iter = bl.cbegin();
7c673cae 3661 try {
11fdf7f2 3662 decode(head, iter);
7c673cae
FG
3663 } catch (buffer::error& err) {
3664 CLS_LOG(0, "ERROR: rgw_cls_lc_get_head(): failed to decode entry %s\n",err.what());
3665 return -EINVAL;
3666 }
3667 } else {
3668 head.start_date = 0;
3669 head.marker.clear();
3670 }
3671 cls_rgw_lc_get_head_ret op_ret;
3672 op_ret.head = head;
11fdf7f2 3673 encode(op_ret, *out);
7c673cae
FG
3674 return 0;
3675}
3676
31f18b77
FG
3677static int rgw_reshard_add(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3678{
11fdf7f2 3679 auto in_iter = in->cbegin();
31f18b77
FG
3680
3681 cls_rgw_reshard_add_op op;
3682 try {
11fdf7f2 3683 decode(op, in_iter);
31f18b77
FG
3684 } catch (buffer::error& err) {
3685 CLS_LOG(1, "ERROR: rgw_reshard_add: failed to decode entry\n");
3686 return -EINVAL;
3687 }
3688
3689
3690 string key;
3691 op.entry.get_key(&key);
3692
3693 bufferlist bl;
11fdf7f2 3694 encode(op.entry, bl);
31f18b77
FG
3695 int ret = cls_cxx_map_set_val(hctx, key, &bl);
3696 if (ret < 0) {
3697 CLS_ERR("error adding reshard job for bucket %s with key %s",op.entry.bucket_name.c_str(), key.c_str());
3698 return ret;
3699 }
3700
3701 return ret;
3702}
3703
3704static int rgw_reshard_list(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3705{
3706 cls_rgw_reshard_list_op op;
11fdf7f2 3707 auto in_iter = in->cbegin();
31f18b77 3708 try {
11fdf7f2 3709 decode(op, in_iter);
31f18b77
FG
3710 } catch (buffer::error& err) {
3711 CLS_LOG(1, "ERROR: rgw_cls_rehard_list(): failed to decode entry\n");
3712 return -EINVAL;
3713 }
3714 cls_rgw_reshard_list_ret op_ret;
11fdf7f2 3715 bufferlist::const_iterator iter;
31f18b77
FG
3716 map<string, bufferlist> vals;
3717 string filter_prefix;
3718#define MAX_RESHARD_LIST_ENTRIES 1000
3719 /* one extra entry for identifying truncation */
c07f9fc5
FG
3720 int32_t max = (op.max && (op.max < MAX_RESHARD_LIST_ENTRIES) ? op.max : MAX_RESHARD_LIST_ENTRIES);
3721 int ret = cls_cxx_map_get_vals(hctx, op.marker, filter_prefix, max, &vals, &op_ret.is_truncated);
31f18b77
FG
3722 if (ret < 0)
3723 return ret;
3724 map<string, bufferlist>::iterator it;
3725 cls_rgw_reshard_entry entry;
3726 int i = 0;
3727 for (it = vals.begin(); i < (int)op.max && it != vals.end(); ++it, ++i) {
11fdf7f2 3728 iter = it->second.cbegin();
31f18b77 3729 try {
11fdf7f2 3730 decode(entry, iter);
31f18b77
FG
3731 } catch (buffer::error& err) {
3732 CLS_LOG(1, "ERROR: rgw_cls_rehard_list(): failed to decode entry\n");
3733 return -EIO;
3734 }
3735 op_ret.entries.push_back(entry);
3736 }
11fdf7f2 3737 encode(op_ret, *out);
31f18b77
FG
3738 return 0;
3739}
3740
3741static int rgw_reshard_get(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3742{
11fdf7f2 3743 auto in_iter = in->cbegin();
31f18b77
FG
3744
3745 cls_rgw_reshard_get_op op;
3746 try {
11fdf7f2 3747 decode(op, in_iter);
31f18b77
FG
3748 } catch (buffer::error& err) {
3749 CLS_LOG(1, "ERROR: rgw_reshard_get: failed to decode entry\n");
3750 return -EINVAL;
3751 }
3752
3753 string key;
3754 cls_rgw_reshard_entry entry;
3755 op.entry.get_key(&key);
11fdf7f2 3756 int ret = read_omap_entry(hctx, key, &entry);
31f18b77
FG
3757 if (ret < 0) {
3758 return ret;
3759 }
3760
3761 cls_rgw_reshard_get_ret op_ret;
3762 op_ret.entry = entry;
11fdf7f2 3763 encode(op_ret, *out);
31f18b77
FG
3764 return 0;
3765}
3766
3767static int rgw_reshard_remove(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3768{
11fdf7f2 3769 auto in_iter = in->cbegin();
31f18b77
FG
3770
3771 cls_rgw_reshard_remove_op op;
3772 try {
11fdf7f2 3773 decode(op, in_iter);
31f18b77
FG
3774 } catch (buffer::error& err) {
3775 CLS_LOG(1, "ERROR: rgw_cls_rehard_remove: failed to decode entry\n");
3776 return -EINVAL;
3777 }
3778
3779 string key;
3780 cls_rgw_reshard_entry entry;
3781 cls_rgw_reshard_entry::generate_key(op.tenant, op.bucket_name, &key);
11fdf7f2 3782 int ret = read_omap_entry(hctx, key, &entry);
31f18b77
FG
3783 if (ret < 0) {
3784 return ret;
3785 }
3786
3787 if (!op.bucket_id.empty() &&
3788 entry.bucket_id != op.bucket_id) {
3789 return 0;
3790 }
3791
3792 ret = cls_cxx_map_remove_key(hctx, key);
3793 if (ret < 0) {
3794 CLS_LOG(0, "ERROR: failed to remove key: key=%s ret=%d", key.c_str(), ret);
3795 return 0;
3796 }
3797 return ret;
3798}
3799
3800static int rgw_set_bucket_resharding(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3801{
3802 cls_rgw_set_bucket_resharding_op op;
3803
11fdf7f2 3804 auto in_iter = in->cbegin();
31f18b77 3805 try {
11fdf7f2 3806 decode(op, in_iter);
31f18b77
FG
3807 } catch (buffer::error& err) {
3808 CLS_LOG(1, "ERROR: cls_rgw_set_bucket_resharding: failed to decode entry\n");
3809 return -EINVAL;
3810 }
3811
11fdf7f2 3812 rgw_bucket_dir_header header;
31f18b77
FG
3813 int rc = read_bucket_header(hctx, &header);
3814 if (rc < 0) {
3815 CLS_LOG(1, "ERROR: %s(): failed to read header\n", __func__);
3816 return rc;
3817 }
3818
3819 header.new_instance.set_status(op.entry.new_bucket_instance_id, op.entry.num_shards, op.entry.reshard_status);
3820
3821 return write_bucket_header(hctx, &header);
3822}
3823
3824static int rgw_clear_bucket_resharding(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3825{
f64942e4 3826 cls_rgw_clear_bucket_resharding_op op;
31f18b77 3827
11fdf7f2 3828 auto in_iter = in->cbegin();
31f18b77 3829 try {
11fdf7f2 3830 decode(op, in_iter);
31f18b77
FG
3831 } catch (buffer::error& err) {
3832 CLS_LOG(1, "ERROR: cls_rgw_clear_bucket_resharding: failed to decode entry\n");
3833 return -EINVAL;
3834 }
3835
11fdf7f2 3836 rgw_bucket_dir_header header;
31f18b77
FG
3837 int rc = read_bucket_header(hctx, &header);
3838 if (rc < 0) {
3839 CLS_LOG(1, "ERROR: %s(): failed to read header\n", __func__);
3840 return rc;
3841 }
3842 header.new_instance.clear();
3843
3844 return write_bucket_header(hctx, &header);
3845}
3846
3847static int rgw_guard_bucket_resharding(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3848{
3849 cls_rgw_guard_bucket_resharding_op op;
3850
11fdf7f2 3851 auto in_iter = in->cbegin();
31f18b77 3852 try {
11fdf7f2 3853 decode(op, in_iter);
31f18b77
FG
3854 } catch (buffer::error& err) {
3855 CLS_LOG(1, "ERROR: cls_rgw_clear_bucket_resharding: failed to decode entry\n");
3856 return -EINVAL;
3857 }
3858
11fdf7f2 3859 rgw_bucket_dir_header header;
31f18b77
FG
3860 int rc = read_bucket_header(hctx, &header);
3861 if (rc < 0) {
3862 CLS_LOG(1, "ERROR: %s(): failed to read header\n", __func__);
3863 return rc;
3864 }
3865
3866 if (header.resharding()) {
3867 return op.ret_err;
3868 }
3869
3870 return 0;
3871}
3872
11fdf7f2
TL
3873static int rgw_get_bucket_resharding(cls_method_context_t hctx,
3874 bufferlist *in, bufferlist *out)
31f18b77
FG
3875{
3876 cls_rgw_get_bucket_resharding_op op;
3877
11fdf7f2 3878 auto in_iter = in->cbegin();
31f18b77 3879 try {
11fdf7f2 3880 decode(op, in_iter);
31f18b77
FG
3881 } catch (buffer::error& err) {
3882 CLS_LOG(1, "ERROR: cls_rgw_clear_bucket_resharding: failed to decode entry\n");
3883 return -EINVAL;
3884 }
3885
11fdf7f2 3886 rgw_bucket_dir_header header;
31f18b77
FG
3887 int rc = read_bucket_header(hctx, &header);
3888 if (rc < 0) {
3889 CLS_LOG(1, "ERROR: %s(): failed to read header\n", __func__);
3890 return rc;
3891 }
3892
3893 cls_rgw_get_bucket_resharding_ret op_ret;
3894 op_ret.new_instance = header.new_instance;
3895
11fdf7f2 3896 encode(op_ret, *out);
31f18b77
FG
3897
3898 return 0;
3899}
3900
7c673cae
FG
3901CLS_INIT(rgw)
3902{
3903 CLS_LOG(1, "Loaded rgw class!");
3904
3905 cls_handle_t h_class;
3906 cls_method_handle_t h_rgw_bucket_init_index;
3907 cls_method_handle_t h_rgw_bucket_set_tag_timeout;
3908 cls_method_handle_t h_rgw_bucket_list;
3909 cls_method_handle_t h_rgw_bucket_check_index;
3910 cls_method_handle_t h_rgw_bucket_rebuild_index;
3911 cls_method_handle_t h_rgw_bucket_update_stats;
3912 cls_method_handle_t h_rgw_bucket_prepare_op;
3913 cls_method_handle_t h_rgw_bucket_complete_op;
3914 cls_method_handle_t h_rgw_bucket_link_olh;
3915 cls_method_handle_t h_rgw_bucket_unlink_instance_op;
3916 cls_method_handle_t h_rgw_bucket_read_olh_log;
3917 cls_method_handle_t h_rgw_bucket_trim_olh_log;
3918 cls_method_handle_t h_rgw_bucket_clear_olh;
3919 cls_method_handle_t h_rgw_obj_remove;
3920 cls_method_handle_t h_rgw_obj_store_pg_ver;
3921 cls_method_handle_t h_rgw_obj_check_attrs_prefix;
3922 cls_method_handle_t h_rgw_obj_check_mtime;
3923 cls_method_handle_t h_rgw_bi_get_op;
3924 cls_method_handle_t h_rgw_bi_put_op;
3925 cls_method_handle_t h_rgw_bi_list_op;
3926 cls_method_handle_t h_rgw_bi_log_list_op;
c07f9fc5
FG
3927 cls_method_handle_t h_rgw_bi_log_resync_op;
3928 cls_method_handle_t h_rgw_bi_log_stop_op;
7c673cae
FG
3929 cls_method_handle_t h_rgw_dir_suggest_changes;
3930 cls_method_handle_t h_rgw_user_usage_log_add;
3931 cls_method_handle_t h_rgw_user_usage_log_read;
3932 cls_method_handle_t h_rgw_user_usage_log_trim;
11fdf7f2 3933 cls_method_handle_t h_rgw_usage_log_clear;
7c673cae
FG
3934 cls_method_handle_t h_rgw_gc_set_entry;
3935 cls_method_handle_t h_rgw_gc_list;
3936 cls_method_handle_t h_rgw_gc_remove;
11fdf7f2 3937 cls_method_handle_t h_rgw_lc_get_entry;
7c673cae
FG
3938 cls_method_handle_t h_rgw_lc_set_entry;
3939 cls_method_handle_t h_rgw_lc_rm_entry;
3940 cls_method_handle_t h_rgw_lc_get_next_entry;
3941 cls_method_handle_t h_rgw_lc_put_head;
3942 cls_method_handle_t h_rgw_lc_get_head;
3943 cls_method_handle_t h_rgw_lc_list_entries;
31f18b77
FG
3944 cls_method_handle_t h_rgw_reshard_add;
3945 cls_method_handle_t h_rgw_reshard_list;
3946 cls_method_handle_t h_rgw_reshard_get;
3947 cls_method_handle_t h_rgw_reshard_remove;
3948 cls_method_handle_t h_rgw_set_bucket_resharding;
3949 cls_method_handle_t h_rgw_clear_bucket_resharding;
3950 cls_method_handle_t h_rgw_guard_bucket_resharding;
3951 cls_method_handle_t h_rgw_get_bucket_resharding;
7c673cae
FG
3952
3953
3954 cls_register(RGW_CLASS, &h_class);
3955
3956 /* bucket index */
3957 cls_register_cxx_method(h_class, RGW_BUCKET_INIT_INDEX, CLS_METHOD_RD | CLS_METHOD_WR, rgw_bucket_init_index, &h_rgw_bucket_init_index);
3958 cls_register_cxx_method(h_class, RGW_BUCKET_SET_TAG_TIMEOUT, CLS_METHOD_RD | CLS_METHOD_WR, rgw_bucket_set_tag_timeout, &h_rgw_bucket_set_tag_timeout);
3959 cls_register_cxx_method(h_class, RGW_BUCKET_LIST, CLS_METHOD_RD, rgw_bucket_list, &h_rgw_bucket_list);
3960 cls_register_cxx_method(h_class, RGW_BUCKET_CHECK_INDEX, CLS_METHOD_RD, rgw_bucket_check_index, &h_rgw_bucket_check_index);
3961 cls_register_cxx_method(h_class, RGW_BUCKET_REBUILD_INDEX, CLS_METHOD_RD | CLS_METHOD_WR, rgw_bucket_rebuild_index, &h_rgw_bucket_rebuild_index);
3962 cls_register_cxx_method(h_class, RGW_BUCKET_UPDATE_STATS, CLS_METHOD_RD | CLS_METHOD_WR, rgw_bucket_update_stats, &h_rgw_bucket_update_stats);
3963 cls_register_cxx_method(h_class, RGW_BUCKET_PREPARE_OP, CLS_METHOD_RD | CLS_METHOD_WR, rgw_bucket_prepare_op, &h_rgw_bucket_prepare_op);
3964 cls_register_cxx_method(h_class, RGW_BUCKET_COMPLETE_OP, CLS_METHOD_RD | CLS_METHOD_WR, rgw_bucket_complete_op, &h_rgw_bucket_complete_op);
3965 cls_register_cxx_method(h_class, RGW_BUCKET_LINK_OLH, CLS_METHOD_RD | CLS_METHOD_WR, rgw_bucket_link_olh, &h_rgw_bucket_link_olh);
3966 cls_register_cxx_method(h_class, RGW_BUCKET_UNLINK_INSTANCE, CLS_METHOD_RD | CLS_METHOD_WR, rgw_bucket_unlink_instance, &h_rgw_bucket_unlink_instance_op);
3967 cls_register_cxx_method(h_class, RGW_BUCKET_READ_OLH_LOG, CLS_METHOD_RD, rgw_bucket_read_olh_log, &h_rgw_bucket_read_olh_log);
3968 cls_register_cxx_method(h_class, RGW_BUCKET_TRIM_OLH_LOG, CLS_METHOD_RD | CLS_METHOD_WR, rgw_bucket_trim_olh_log, &h_rgw_bucket_trim_olh_log);
3969 cls_register_cxx_method(h_class, RGW_BUCKET_CLEAR_OLH, CLS_METHOD_RD | CLS_METHOD_WR, rgw_bucket_clear_olh, &h_rgw_bucket_clear_olh);
3970
3971 cls_register_cxx_method(h_class, RGW_OBJ_REMOVE, CLS_METHOD_RD | CLS_METHOD_WR, rgw_obj_remove, &h_rgw_obj_remove);
3972 cls_register_cxx_method(h_class, RGW_OBJ_STORE_PG_VER, CLS_METHOD_WR, rgw_obj_store_pg_ver, &h_rgw_obj_store_pg_ver);
3973 cls_register_cxx_method(h_class, RGW_OBJ_CHECK_ATTRS_PREFIX, CLS_METHOD_RD, rgw_obj_check_attrs_prefix, &h_rgw_obj_check_attrs_prefix);
3974 cls_register_cxx_method(h_class, RGW_OBJ_CHECK_MTIME, CLS_METHOD_RD, rgw_obj_check_mtime, &h_rgw_obj_check_mtime);
3975
3976 cls_register_cxx_method(h_class, RGW_BI_GET, CLS_METHOD_RD, rgw_bi_get_op, &h_rgw_bi_get_op);
3977 cls_register_cxx_method(h_class, RGW_BI_PUT, CLS_METHOD_RD | CLS_METHOD_WR, rgw_bi_put_op, &h_rgw_bi_put_op);
3978 cls_register_cxx_method(h_class, RGW_BI_LIST, CLS_METHOD_RD, rgw_bi_list_op, &h_rgw_bi_list_op);
3979
3980 cls_register_cxx_method(h_class, RGW_BI_LOG_LIST, CLS_METHOD_RD, rgw_bi_log_list, &h_rgw_bi_log_list_op);
3981 cls_register_cxx_method(h_class, RGW_BI_LOG_TRIM, CLS_METHOD_RD | CLS_METHOD_WR, rgw_bi_log_trim, &h_rgw_bi_log_list_op);
3982 cls_register_cxx_method(h_class, RGW_DIR_SUGGEST_CHANGES, CLS_METHOD_RD | CLS_METHOD_WR, rgw_dir_suggest_changes, &h_rgw_dir_suggest_changes);
3983
11fdf7f2
TL
3984 cls_register_cxx_method(h_class, RGW_BI_LOG_RESYNC, CLS_METHOD_RD | CLS_METHOD_WR, rgw_bi_log_resync, &h_rgw_bi_log_resync_op);
3985 cls_register_cxx_method(h_class, RGW_BI_LOG_STOP, CLS_METHOD_RD | CLS_METHOD_WR, rgw_bi_log_stop, &h_rgw_bi_log_stop_op);
c07f9fc5 3986
7c673cae
FG
3987 /* usage logging */
3988 cls_register_cxx_method(h_class, RGW_USER_USAGE_LOG_ADD, CLS_METHOD_RD | CLS_METHOD_WR, rgw_user_usage_log_add, &h_rgw_user_usage_log_add);
3989 cls_register_cxx_method(h_class, RGW_USER_USAGE_LOG_READ, CLS_METHOD_RD, rgw_user_usage_log_read, &h_rgw_user_usage_log_read);
3990 cls_register_cxx_method(h_class, RGW_USER_USAGE_LOG_TRIM, CLS_METHOD_RD | CLS_METHOD_WR, rgw_user_usage_log_trim, &h_rgw_user_usage_log_trim);
11fdf7f2 3991 cls_register_cxx_method(h_class, RGW_USAGE_LOG_CLEAR, CLS_METHOD_WR, rgw_usage_log_clear, &h_rgw_usage_log_clear);
7c673cae
FG
3992
3993 /* garbage collection */
3994 cls_register_cxx_method(h_class, RGW_GC_SET_ENTRY, CLS_METHOD_RD | CLS_METHOD_WR, rgw_cls_gc_set_entry, &h_rgw_gc_set_entry);
3995 cls_register_cxx_method(h_class, RGW_GC_DEFER_ENTRY, CLS_METHOD_RD | CLS_METHOD_WR, rgw_cls_gc_defer_entry, &h_rgw_gc_set_entry);
3996 cls_register_cxx_method(h_class, RGW_GC_LIST, CLS_METHOD_RD, rgw_cls_gc_list, &h_rgw_gc_list);
3997 cls_register_cxx_method(h_class, RGW_GC_REMOVE, CLS_METHOD_RD | CLS_METHOD_WR, rgw_cls_gc_remove, &h_rgw_gc_remove);
3998
3999 /* lifecycle bucket list */
11fdf7f2 4000 cls_register_cxx_method(h_class, RGW_LC_GET_ENTRY, CLS_METHOD_RD, rgw_cls_lc_get_entry, &h_rgw_lc_get_entry);
7c673cae
FG
4001 cls_register_cxx_method(h_class, RGW_LC_SET_ENTRY, CLS_METHOD_RD | CLS_METHOD_WR, rgw_cls_lc_set_entry, &h_rgw_lc_set_entry);
4002 cls_register_cxx_method(h_class, RGW_LC_RM_ENTRY, CLS_METHOD_RD | CLS_METHOD_WR, rgw_cls_lc_rm_entry, &h_rgw_lc_rm_entry);
4003 cls_register_cxx_method(h_class, RGW_LC_GET_NEXT_ENTRY, CLS_METHOD_RD, rgw_cls_lc_get_next_entry, &h_rgw_lc_get_next_entry);
4004 cls_register_cxx_method(h_class, RGW_LC_PUT_HEAD, CLS_METHOD_RD| CLS_METHOD_WR, rgw_cls_lc_put_head, &h_rgw_lc_put_head);
4005 cls_register_cxx_method(h_class, RGW_LC_GET_HEAD, CLS_METHOD_RD, rgw_cls_lc_get_head, &h_rgw_lc_get_head);
4006 cls_register_cxx_method(h_class, RGW_LC_LIST_ENTRIES, CLS_METHOD_RD, rgw_cls_lc_list_entries, &h_rgw_lc_list_entries);
11fdf7f2
TL
4007
4008 /* resharding */
4009 cls_register_cxx_method(h_class, RGW_RESHARD_ADD, CLS_METHOD_RD | CLS_METHOD_WR, rgw_reshard_add, &h_rgw_reshard_add);
4010 cls_register_cxx_method(h_class, RGW_RESHARD_LIST, CLS_METHOD_RD, rgw_reshard_list, &h_rgw_reshard_list);
4011 cls_register_cxx_method(h_class, RGW_RESHARD_GET, CLS_METHOD_RD,rgw_reshard_get, &h_rgw_reshard_get);
4012 cls_register_cxx_method(h_class, RGW_RESHARD_REMOVE, CLS_METHOD_RD | CLS_METHOD_WR, rgw_reshard_remove, &h_rgw_reshard_remove);
4013
4014 /* resharding attribute */
4015 cls_register_cxx_method(h_class, RGW_SET_BUCKET_RESHARDING, CLS_METHOD_RD | CLS_METHOD_WR,
31f18b77 4016 rgw_set_bucket_resharding, &h_rgw_set_bucket_resharding);
11fdf7f2 4017 cls_register_cxx_method(h_class, RGW_CLEAR_BUCKET_RESHARDING, CLS_METHOD_RD | CLS_METHOD_WR,
31f18b77 4018 rgw_clear_bucket_resharding, &h_rgw_clear_bucket_resharding);
11fdf7f2 4019 cls_register_cxx_method(h_class, RGW_GUARD_BUCKET_RESHARDING, CLS_METHOD_RD ,
31f18b77 4020 rgw_guard_bucket_resharding, &h_rgw_guard_bucket_resharding);
11fdf7f2 4021 cls_register_cxx_method(h_class, RGW_GET_BUCKET_RESHARDING, CLS_METHOD_RD ,
31f18b77
FG
4022 rgw_get_bucket_resharding, &h_rgw_get_bucket_resharding);
4023
7c673cae
FG
4024 return;
4025}
4026