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