]> git.proxmox.com Git - ceph.git/blob - ceph/src/cls/rgw/cls_rgw.cc
import quincy beta 17.1.0
[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 real_time cur_time = real_clock::now();
2209 auto iter = cur_disk.pending_map.begin();
2210 while(iter != cur_disk.pending_map.end()) {
2211 auto cur_iter = iter++;
2212 if (cur_time > (cur_iter->second.timestamp + timespan(tag_timeout))) {
2213 cur_disk.pending_map.erase(cur_iter);
2214 }
2215 }
2216 }
2217
2218 CLS_LOG(20, "cur_disk.pending_map.empty()=%d op=%d cur_disk.exists=%d cur_change.pending_map.size()=%d cur_change.exists=%d",
2219 cur_disk.pending_map.empty(), (int)op, cur_disk.exists,
2220 (int)cur_change.pending_map.size(), cur_change.exists);
2221
2222 if (cur_disk.pending_map.empty()) {
2223 if (cur_disk.exists) {
2224 rgw_bucket_category_stats& old_stats = header.stats[cur_disk.meta.category];
2225 CLS_LOG(10, "total_entries: %" PRId64 " -> %" PRId64 "", old_stats.num_entries, old_stats.num_entries - 1);
2226 old_stats.num_entries--;
2227 old_stats.total_size -= cur_disk.meta.accounted_size;
2228 old_stats.total_size_rounded -= cls_rgw_get_rounded_size(cur_disk.meta.accounted_size);
2229 old_stats.actual_size -= cur_disk.meta.size;
2230 header_changed = true;
2231 }
2232 rgw_bucket_category_stats& stats = header.stats[cur_change.meta.category];
2233 bool log_op = (op & CEPH_RGW_DIR_SUGGEST_LOG_OP) != 0;
2234 op &= CEPH_RGW_DIR_SUGGEST_OP_MASK;
2235 switch(op) {
2236 case CEPH_RGW_REMOVE:
2237 CLS_LOG(10, "CEPH_RGW_REMOVE name=%s instance=%s", cur_change.key.name.c_str(), cur_change.key.instance.c_str());
2238 ret = cls_cxx_map_remove_key(hctx, cur_change_key);
2239 if (ret < 0)
2240 return ret;
2241 if (log_op && cur_disk.exists && !header.syncstopped) {
2242 ret = log_index_operation(hctx, cur_disk.key, CLS_RGW_OP_DEL, cur_disk.tag, cur_disk.meta.mtime,
2243 cur_disk.ver, CLS_RGW_STATE_COMPLETE, header.ver, header.max_marker, 0, NULL, NULL, NULL);
2244 if (ret < 0) {
2245 CLS_LOG(0, "ERROR: %s: failed to log operation ret=%d", __func__, ret);
2246 return ret;
2247 }
2248 }
2249 break;
2250 case CEPH_RGW_UPDATE:
2251 CLS_LOG(10, "CEPH_RGW_UPDATE name=%s instance=%s total_entries: %" PRId64 " -> %" PRId64 "",
2252 cur_change.key.name.c_str(), cur_change.key.instance.c_str(), stats.num_entries, stats.num_entries + 1);
2253
2254 stats.num_entries++;
2255 stats.total_size += cur_change.meta.accounted_size;
2256 stats.total_size_rounded += cls_rgw_get_rounded_size(cur_change.meta.accounted_size);
2257 stats.actual_size += cur_change.meta.size;
2258 header_changed = true;
2259 cur_change.index_ver = header.ver;
2260 bufferlist cur_state_bl;
2261 encode(cur_change, cur_state_bl);
2262 ret = cls_cxx_map_set_val(hctx, cur_change_key, &cur_state_bl);
2263 if (ret < 0)
2264 return ret;
2265 if (log_op && !header.syncstopped) {
2266 ret = log_index_operation(hctx, cur_change.key, CLS_RGW_OP_ADD, cur_change.tag, cur_change.meta.mtime,
2267 cur_change.ver, CLS_RGW_STATE_COMPLETE, header.ver, header.max_marker, 0, NULL, NULL, NULL);
2268 if (ret < 0) {
2269 CLS_LOG(0, "ERROR: %s: failed to log operation ret=%d", __func__, ret);
2270 return ret;
2271 }
2272 }
2273 break;
2274 } // switch(op)
2275 } // if (cur_disk.pending_map.empty())
2276 } // while (!in_iter.end())
2277
2278 if (header_changed) {
2279 return write_bucket_header(hctx, &header);
2280 }
2281 return 0;
2282 }
2283
2284 static int rgw_obj_remove(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
2285 {
2286 CLS_LOG(10, "entered %s", __func__);
2287 // decode request
2288 rgw_cls_obj_remove_op op;
2289 auto iter = in->cbegin();
2290 try {
2291 decode(op, iter);
2292 } catch (ceph::buffer::error& err) {
2293 CLS_LOG(0, "ERROR: %s: failed to decode request", __func__);
2294 return -EINVAL;
2295 }
2296
2297 if (op.keep_attr_prefixes.empty()) {
2298 return cls_cxx_remove(hctx);
2299 }
2300
2301 map<string, bufferlist> attrset;
2302 int ret = cls_cxx_getxattrs(hctx, &attrset);
2303 if (ret < 0 && ret != -ENOENT) {
2304 CLS_LOG(0, "ERROR: %s: cls_cxx_getxattrs() returned %d", __func__, ret);
2305 return ret;
2306 }
2307
2308 map<string, bufferlist> new_attrs;
2309 for (auto iter = op.keep_attr_prefixes.begin();
2310 iter != op.keep_attr_prefixes.end(); ++iter) {
2311 auto& check_prefix = *iter;
2312
2313 for (auto aiter = attrset.lower_bound(check_prefix);
2314 aiter != attrset.end(); ++aiter) {
2315 const string& attr = aiter->first;
2316
2317 if (attr.substr(0, check_prefix.size()) > check_prefix) {
2318 break;
2319 }
2320
2321 new_attrs[attr] = aiter->second;
2322 }
2323 }
2324
2325 CLS_LOG(20, "%s: removing object", __func__);
2326 ret = cls_cxx_remove(hctx);
2327 if (ret < 0) {
2328 CLS_LOG(0, "ERROR: %s: cls_cxx_remove returned %d", __func__, ret);
2329 return ret;
2330 }
2331
2332 if (new_attrs.empty()) {
2333 /* no data to keep */
2334 return 0;
2335 }
2336
2337 ret = cls_cxx_create(hctx, false);
2338 if (ret < 0) {
2339 CLS_LOG(0, "ERROR: %s: cls_cxx_create returned %d", __func__, ret);
2340 return ret;
2341 }
2342
2343 for (auto aiter = new_attrs.begin();
2344 aiter != new_attrs.end(); ++aiter) {
2345 const auto& attr = aiter->first;
2346
2347 ret = cls_cxx_setxattr(hctx, attr.c_str(), &aiter->second);
2348 CLS_LOG(20, "%s: setting attr: %s", __func__, attr.c_str());
2349 if (ret < 0) {
2350 CLS_LOG(0, "ERROR: %s: cls_cxx_setxattr (attr=%s) returned %d", __func__, attr.c_str(), ret);
2351 return ret;
2352 }
2353 }
2354
2355 return 0;
2356 }
2357
2358 static int rgw_obj_store_pg_ver(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
2359 {
2360 CLS_LOG(10, "entered %s", __func__);
2361 // decode request
2362 rgw_cls_obj_store_pg_ver_op op;
2363 auto iter = in->cbegin();
2364 try {
2365 decode(op, iter);
2366 } catch (ceph::buffer::error& err) {
2367 CLS_LOG(0, "ERROR: %s: failed to decode request", __func__);
2368 return -EINVAL;
2369 }
2370
2371 bufferlist bl;
2372 uint64_t ver = cls_current_version(hctx);
2373 encode(ver, bl);
2374 int ret = cls_cxx_setxattr(hctx, op.attr.c_str(), &bl);
2375 if (ret < 0) {
2376 CLS_LOG(0, "ERROR: %s: cls_cxx_setxattr (attr=%s) returned %d", __func__, op.attr.c_str(), ret);
2377 return ret;
2378 }
2379
2380 return 0;
2381 }
2382
2383 static int rgw_obj_check_attrs_prefix(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
2384 {
2385 CLS_LOG(10, "entered %s", __func__);
2386 // decode request
2387 rgw_cls_obj_check_attrs_prefix op;
2388 auto iter = in->cbegin();
2389 try {
2390 decode(op, iter);
2391 } catch (ceph::buffer::error& err) {
2392 CLS_LOG(0, "ERROR: %s: failed to decode request", __func__);
2393 return -EINVAL;
2394 }
2395
2396 if (op.check_prefix.empty()) {
2397 return -EINVAL;
2398 }
2399
2400 map<string, bufferlist> attrset;
2401 int ret = cls_cxx_getxattrs(hctx, &attrset);
2402 if (ret < 0 && ret != -ENOENT) {
2403 CLS_LOG(0, "ERROR: %s: cls_cxx_getxattrs() returned %d", __func__, ret);
2404 return ret;
2405 }
2406
2407 bool exist = false;
2408
2409 for (auto aiter = attrset.lower_bound(op.check_prefix);
2410 aiter != attrset.end(); ++aiter) {
2411 const auto& attr = aiter->first;
2412
2413 if (attr.substr(0, op.check_prefix.size()) > op.check_prefix) {
2414 break;
2415 }
2416
2417 exist = true;
2418 }
2419
2420 if (exist == op.fail_if_exist) {
2421 return -ECANCELED;
2422 }
2423
2424 return 0;
2425 }
2426
2427 static int rgw_obj_check_mtime(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
2428 {
2429 CLS_LOG(10, "entered %s", __func__);
2430 // decode request
2431 rgw_cls_obj_check_mtime op;
2432 auto iter = in->cbegin();
2433 try {
2434 decode(op, iter);
2435 } catch (ceph::buffer::error& err) {
2436 CLS_LOG(0, "ERROR: %s: failed to decode request", __func__);
2437 return -EINVAL;
2438 }
2439
2440 real_time obj_ut;
2441 int ret = cls_cxx_stat2(hctx, NULL, &obj_ut);
2442 if (ret < 0 && ret != -ENOENT) {
2443 CLS_LOG(0, "ERROR: %s: cls_cxx_stat() returned %d", __func__, ret);
2444 return ret;
2445 }
2446 if (ret == -ENOENT) {
2447 CLS_LOG(10, "object does not exist, skipping check");
2448 }
2449
2450 ceph_timespec obj_ts = ceph::real_clock::to_ceph_timespec(obj_ut);
2451 ceph_timespec op_ts = ceph::real_clock::to_ceph_timespec(op.mtime);
2452
2453 if (!op.high_precision_time) {
2454 obj_ts.tv_nsec = 0;
2455 op_ts.tv_nsec = 0;
2456 }
2457
2458 CLS_LOG(10, "%s: obj_ut=%lld.%06lld op.mtime=%lld.%06lld", __func__,
2459 (long long)obj_ts.tv_sec, (long long)obj_ts.tv_nsec,
2460 (long long)op_ts.tv_sec, (long long)op_ts.tv_nsec);
2461
2462 bool check;
2463
2464 switch (op.type) {
2465 case CLS_RGW_CHECK_TIME_MTIME_EQ:
2466 check = (obj_ts == op_ts);
2467 break;
2468 case CLS_RGW_CHECK_TIME_MTIME_LT:
2469 check = (obj_ts < op_ts);
2470 break;
2471 case CLS_RGW_CHECK_TIME_MTIME_LE:
2472 check = (obj_ts <= op_ts);
2473 break;
2474 case CLS_RGW_CHECK_TIME_MTIME_GT:
2475 check = (obj_ts > op_ts);
2476 break;
2477 case CLS_RGW_CHECK_TIME_MTIME_GE:
2478 check = (obj_ts >= op_ts);
2479 break;
2480 default:
2481 return -EINVAL;
2482 };
2483
2484 if (!check) {
2485 return -ECANCELED;
2486 }
2487
2488 return 0;
2489 }
2490
2491 static int rgw_bi_get_op(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
2492 {
2493 CLS_LOG(10, "entered %s", __func__);
2494 // decode request
2495 rgw_cls_bi_get_op op;
2496 auto iter = in->cbegin();
2497 try {
2498 decode(op, iter);
2499 } catch (ceph::buffer::error& err) {
2500 CLS_LOG(0, "ERROR: %s: failed to decode request", __func__);
2501 return -EINVAL;
2502 }
2503
2504 string idx;
2505
2506 switch (op.type) {
2507 case BIIndexType::Plain:
2508 idx = op.key.name;
2509 break;
2510 case BIIndexType::Instance:
2511 encode_obj_index_key(op.key, &idx);
2512 break;
2513 case BIIndexType::OLH:
2514 encode_olh_data_key(op.key, &idx);
2515 break;
2516 default:
2517 CLS_LOG(10, "%s: invalid key type encoding: %d",
2518 __func__, int(op.type));
2519 return -EINVAL;
2520 }
2521
2522 rgw_cls_bi_get_ret op_ret;
2523
2524 rgw_cls_bi_entry& entry = op_ret.entry;
2525
2526 entry.type = op.type;
2527 entry.idx = idx;
2528
2529 int r = cls_cxx_map_get_val(hctx, idx, &entry.data);
2530 if (r < 0) {
2531 CLS_LOG(10, "%s: cls_cxx_map_get_val() returned %d", __func__, r);
2532 return r;
2533 }
2534
2535 encode(op_ret, *out);
2536
2537 return 0;
2538 }
2539
2540 static int rgw_bi_put_op(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
2541 {
2542 CLS_LOG(10, "entered %s", __func__);
2543 // decode request
2544 rgw_cls_bi_put_op op;
2545 auto iter = in->cbegin();
2546 try {
2547 decode(op, iter);
2548 } catch (ceph::buffer::error& err) {
2549 CLS_LOG(0, "ERROR: %s: failed to decode request", __func__);
2550 return -EINVAL;
2551 }
2552
2553 rgw_cls_bi_entry& entry = op.entry;
2554
2555 int r = cls_cxx_map_set_val(hctx, entry.idx, &entry.data);
2556 if (r < 0) {
2557 CLS_LOG(0, "ERROR: %s: cls_cxx_map_set_val() returned r=%d", __func__, r);
2558 }
2559
2560 return 0;
2561 }
2562
2563
2564 /* The plain entries in the bucket index are divided into two regions
2565 * divided by the special entries that begin with 0x80. Those below
2566 * ("Low") are ascii entries. Those above ("High") bring in unicode
2567 * entries. This enum allows either or both regions to be listed in
2568 * list_plain_entries(). It's convenient that "Both" be in between the
2569 * others so we can use "<= Both" or ">= Both" logic.
2570 */
2571 enum class PlainEntriesRegion {
2572 Low, Both, High
2573 };
2574
2575
2576 /* Queries the omap for plain entries in the range of start_after_key
2577 * to end_key, non-inclusive. Both of those values must either be
2578 * before the "ugly namespace" or after it.
2579 *
2580 * Negative return values indicate errors. Non-negative return values
2581 * indicate number of entries retrieved. */
2582 static int list_plain_entries_help(cls_method_context_t hctx,
2583 const std::string& name_filter,
2584 const std::string& start_after_key, // exclusive
2585 const std::string& end_key, // exclusive
2586 uint32_t max,
2587 std::list<rgw_cls_bi_entry>* entries,
2588 bool& end_key_reached,
2589 bool& more)
2590 {
2591 CLS_LOG(10, "Entered %s: name_filter=\"%s\", start_after_key=\"%s\", end_key=\"%s\", max=%d",
2592 __func__, escape_str(name_filter).c_str(), escape_str(start_after_key).c_str(),
2593 escape_str(end_key).c_str(), max);
2594 int count = 0;
2595 std::map<std::string, bufferlist> raw_entries;
2596 int ret = cls_cxx_map_get_vals(hctx, start_after_key, name_filter, max,
2597 &raw_entries, &more);
2598 CLS_LOG(20, "%s: cls_cxx_map_get_vals ret=%d, raw_entries.size()=%lu, more=%d",
2599 __func__, ret, raw_entries.size(), more);
2600 if (ret < 0) {
2601 return ret;
2602 }
2603
2604 end_key_reached = false;
2605 for (auto iter : raw_entries) {
2606 if (!end_key.empty() && iter.first >= end_key) {
2607 CLS_LOG(20, "%s: end key reached at \"%s\"",
2608 __func__, escape_str(iter.first).c_str());
2609 end_key_reached = true;
2610 more = false;
2611 return count;
2612 }
2613
2614 rgw_bucket_dir_entry e;
2615 auto biter = iter.second.cbegin();
2616 try {
2617 decode(e, biter);
2618 } catch (ceph::buffer::error& err) {
2619 CLS_LOG(0, "ERROR: %s: failed to decode buffer for plain bucket index entry \"%s\"",
2620 __func__, escape_str(iter.first).c_str());
2621 return -EIO;
2622 }
2623
2624 if (!name_filter.empty() && e.key.name > name_filter) {
2625 CLS_LOG(20, "%s: due to filter \"%s\", skipping entry.idx=\"%s\" e.key.name=\"%s\"",
2626 __func__,
2627 escape_str(name_filter).c_str(),
2628 escape_str(iter.first).c_str(),
2629 escape_str(e.key.name).c_str());
2630 // skip the rest of the entries
2631 more = false;
2632 end_key_reached = true;
2633 return count;
2634 }
2635
2636 rgw_cls_bi_entry entry;
2637 entry.type = BIIndexType::Plain;
2638 entry.idx = iter.first;
2639 entry.data = iter.second;
2640
2641 entries->push_back(entry);
2642 count++;
2643
2644 CLS_LOG(20, "%s: adding entry %d entry.idx=\"%s\" e.key.name=\"%s\"",
2645 __func__,
2646 count,
2647 escape_str(entry.idx).c_str(),
2648 escape_str(e.key.name).c_str());
2649
2650 if (count >= int(max)) {
2651 // NB: this looks redundant, but leave in for time being
2652 return count;
2653 }
2654 } // iter for loop
2655
2656 return count;
2657 } // list_plain_entries_help
2658
2659 /*
2660 * Lists plain entries in either or both regions, the region of those
2661 * beginning with an ASCII character or a non-ASCII character, which
2662 * surround the "ugly" namespace used by special entries for versioned
2663 * buckets.
2664 *
2665 * The entries parameter is not cleared and additional entries are
2666 * appended to it.
2667 */
2668 static int list_plain_entries(cls_method_context_t hctx,
2669 const std::string& name_filter,
2670 const std::string& marker,
2671 uint32_t max,
2672 std::list<rgw_cls_bi_entry>* entries,
2673 bool* pmore,
2674 const PlainEntriesRegion region = PlainEntriesRegion::Both)
2675 {
2676 CLS_LOG(10, "entered %s: name_filter=\"%s\", marker=\"%s\", max=%d, region=%d",
2677 __func__, escape_str(name_filter).c_str(), escape_str(marker).c_str(), max, static_cast<int>(region));
2678 int r = 0;
2679 bool end_key_reached = false;
2680 bool more = false;
2681 const size_t start_size = entries->size();
2682
2683 if (region <= PlainEntriesRegion::Both && marker < BI_PREFIX_BEGIN) {
2684 // listing ascii plain namespace
2685 int r = list_plain_entries_help(hctx, name_filter, marker, BI_PREFIX_BEGIN, max,
2686 entries, end_key_reached, more);
2687 CLS_LOG(20, "%s: first list_plain_entries_help r=%d, end_key_reached=%d, more=%d",
2688 __func__, r, end_key_reached, more);
2689 if (r < 0) {
2690 return r;
2691 }
2692
2693 // see if we're done for this call (there may be more for a later call)
2694 if (r >= int(max) || !end_key_reached || (!more && region == PlainEntriesRegion::Low)) {
2695 if (pmore) {
2696 *pmore = more;
2697 }
2698
2699 return int(entries->size() - start_size);
2700 }
2701
2702 max = max - r;
2703 }
2704
2705 if (region >= PlainEntriesRegion::Both) {
2706 const std::string start_after_key = std::max(marker, BI_PREFIX_END);
2707
2708 // listing non-ascii plain namespace
2709 r = list_plain_entries_help(hctx, name_filter, start_after_key, {}, max,
2710 entries, end_key_reached, more);
2711 CLS_LOG(20, "%s: second list_plain_entries_help r=%d, end_key_reached=%d, more=%d",
2712 __func__, r, end_key_reached, more);
2713 if (r < 0) {
2714 return r;
2715 }
2716 }
2717
2718 if (pmore) {
2719 *pmore = more;
2720 }
2721
2722 return int(entries->size() - start_size);
2723 }
2724
2725 static int list_instance_entries(cls_method_context_t hctx,
2726 const string& name,
2727 const string& marker,
2728 uint32_t max,
2729 list<rgw_cls_bi_entry> *entries,
2730 bool *pmore)
2731 {
2732 cls_rgw_obj_key key(name);
2733 string first_instance_idx;
2734 encode_obj_versioned_data_key(key, &first_instance_idx);
2735 string start_after_key;
2736
2737 if (!name.empty()) {
2738 start_after_key = first_instance_idx;
2739 } else {
2740 start_after_key = BI_PREFIX_CHAR;
2741 start_after_key.append(bucket_index_prefixes[BI_BUCKET_OBJ_INSTANCE_INDEX]);
2742 }
2743 string filter = start_after_key;
2744 if (bi_entry_gt(marker, start_after_key)) {
2745 start_after_key = marker;
2746 }
2747 int count = 0;
2748 map<string, bufferlist> keys;
2749 bufferlist k;
2750 int ret = cls_cxx_map_get_val(hctx, start_after_key, &k);
2751 if (ret < 0 && ret != -ENOENT) {
2752 return ret;
2753 }
2754 bool found_first = (ret == 0);
2755 if (found_first) {
2756 --max;
2757 }
2758 if (max > 0) {
2759 ret = cls_cxx_map_get_vals(hctx, start_after_key, string(), max,
2760 &keys, pmore);
2761 CLS_LOG(20, "%s: start_after_key=\"%s\" first_instance_idx=\"%s\" keys.size()=%d",
2762 __func__, escape_str(start_after_key).c_str(),
2763 escape_str(first_instance_idx).c_str(), (int)keys.size());
2764 if (ret < 0) {
2765 return ret;
2766 }
2767 }
2768 if (found_first) {
2769 keys[start_after_key] = std::move(k);
2770 }
2771
2772 for (auto iter = keys.begin(); iter != keys.end(); ++iter) {
2773 rgw_cls_bi_entry entry;
2774 entry.type = BIIndexType::Instance;
2775 entry.idx = iter->first;
2776 entry.data = iter->second;
2777
2778 if (!filter.empty() && entry.idx.compare(0, filter.size(), filter) != 0) {
2779 /* we are skipping the rest of the entries */
2780 if (pmore) {
2781 *pmore = false;
2782 }
2783 return count;
2784 }
2785
2786 CLS_LOG(20, "%s: entry.idx=\"%s\"", __func__, escape_str(entry.idx).c_str());
2787
2788 auto biter = entry.data.cbegin();
2789
2790 rgw_bucket_dir_entry e;
2791 try {
2792 decode(e, biter);
2793 } catch (ceph::buffer::error& err) {
2794 CLS_LOG(0, "ERROR: %s: failed to decode buffer (size=%d)", __func__, entry.data.length());
2795 return -EIO;
2796 }
2797
2798 if (!name.empty() && e.key.name != name) {
2799 /* we are skipping the rest of the entries */
2800 if (pmore) {
2801 *pmore = false;
2802 }
2803 return count;
2804 }
2805
2806 entries->push_back(entry);
2807 count++;
2808 start_after_key = entry.idx;
2809 }
2810
2811 return count;
2812 }
2813
2814 static int list_olh_entries(cls_method_context_t hctx,
2815 const string& name,
2816 const string& marker,
2817 uint32_t max,
2818 list<rgw_cls_bi_entry> *entries,
2819 bool *pmore)
2820 {
2821 cls_rgw_obj_key key(name);
2822 string first_instance_idx;
2823 encode_olh_data_key(key, &first_instance_idx);
2824 string start_after_key;
2825
2826 if (!name.empty()) {
2827 start_after_key = first_instance_idx;
2828 } else {
2829 start_after_key = BI_PREFIX_CHAR;
2830 start_after_key.append(bucket_index_prefixes[BI_BUCKET_OLH_DATA_INDEX]);
2831 }
2832 string filter = start_after_key;
2833 if (bi_entry_gt(marker, start_after_key)) {
2834 start_after_key = marker;
2835 }
2836 int count = 0;
2837 map<string, bufferlist> keys;
2838 int ret;
2839 bufferlist k;
2840 ret = cls_cxx_map_get_val(hctx, start_after_key, &k);
2841 if (ret < 0 && ret != -ENOENT) {
2842 return ret;
2843 }
2844 bool found_first = (ret == 0);
2845 if (found_first) {
2846 --max;
2847 }
2848 if (max > 0) {
2849 ret = cls_cxx_map_get_vals(hctx, start_after_key, string(), max,
2850 &keys, pmore);
2851 CLS_LOG(20, "%s: start_after_key=\"%s\", first_instance_idx=\"%s\", keys.size()=%d",
2852 __func__, escape_str(start_after_key).c_str(),
2853 escape_str(first_instance_idx).c_str(), (int)keys.size());
2854 if (ret < 0) {
2855 return ret;
2856 }
2857 }
2858
2859 if (found_first) {
2860 keys[start_after_key] = std::move(k);
2861 }
2862
2863 for (auto iter = keys.begin(); iter != keys.end(); ++iter) {
2864 rgw_cls_bi_entry entry;
2865 entry.type = BIIndexType::OLH;
2866 entry.idx = iter->first;
2867 entry.data = iter->second;
2868
2869 if (!filter.empty() && entry.idx.compare(0, filter.size(), filter) != 0) {
2870 /* we are skipping the rest of the entries */
2871 if (pmore) {
2872 *pmore = false;
2873 }
2874 return count;
2875 }
2876
2877 CLS_LOG(20, "%s: entry.idx=\"%s\"", __func__, escape_str(entry.idx).c_str());
2878
2879 auto biter = entry.data.cbegin();
2880
2881 rgw_bucket_olh_entry e;
2882 try {
2883 decode(e, biter);
2884 } catch (ceph::buffer::error& err) {
2885 CLS_LOG(0, "ERROR: %s: failed to decode buffer (size=%d)", __func__, entry.data.length());
2886 return -EIO;
2887 }
2888
2889 if (!name.empty() && e.key.name != name) {
2890 /* we are skipping the rest of the entries */
2891 if (pmore) {
2892 *pmore = false;
2893 }
2894 return count;
2895 }
2896
2897 entries->push_back(entry);
2898 count++;
2899 start_after_key = entry.idx;
2900 }
2901
2902 return count;
2903 }
2904
2905 /* Lists all the entries that appear in a bucket index listing.
2906 *
2907 * It may not be obvious why this function calls three other "segment"
2908 * functions (list_plain_entries (twice), list_instance_entries,
2909 * list_olh_entries) that each list segments of the index space rather
2910 * than just move a marker through the space from start to end. The
2911 * reason is that a name filter may be provided in the op, and in that
2912 * case most entries will be skipped over, and small segments within
2913 * each larger segment will be listed.
2914 *
2915 * Ideally, each of the three segment functions should be able to
2916 * handle a marker and filter, if either/both is provided,
2917 * efficiently. So, for example, if the marker is after the segment,
2918 * ideally return quickly rather than iterating through entries in the
2919 * segment.
2920 *
2921 * Additionally, each of the three segment functions, if successful,
2922 * is expected to return the number of entries added to the output
2923 * list as a non-negative value. As per usual, negative return values
2924 * indicate error condtions.
2925 */
2926 static int rgw_bi_list_op(cls_method_context_t hctx,
2927 bufferlist *in,
2928 bufferlist *out)
2929 {
2930 CLS_LOG(10, "entered %s", __func__);
2931 // decode request
2932 rgw_cls_bi_list_op op;
2933 auto iter = in->cbegin();
2934 try {
2935 decode(op, iter);
2936 } catch (ceph::buffer::error& err) {
2937 CLS_LOG(0, "ERROR: %s: failed to decode request", __func__);
2938 return -EINVAL;
2939 }
2940
2941 constexpr uint32_t MAX_BI_LIST_ENTRIES = 1000;
2942 const uint32_t max = std::min(op.max, MAX_BI_LIST_ENTRIES);
2943
2944 CLS_LOG(20, "%s: op.marker=\"%s\", op.name_filter=\"%s\", op.max=%u max=%u",
2945 __func__, escape_str(op.marker).c_str(), escape_str(op.name_filter).c_str(),
2946 op.max, max);
2947
2948 int ret;
2949 uint32_t count = 0;
2950 bool more = false;
2951 rgw_cls_bi_list_ret op_ret;
2952
2953 ret = list_plain_entries(hctx, op.name_filter, op.marker, max,
2954 &op_ret.entries, &more, PlainEntriesRegion::Low);
2955 if (ret < 0) {
2956 CLS_LOG(0, "ERROR: %s: list_plain_entries (low) returned ret=%d, marker=\"%s\", filter=\"%s\", max=%d",
2957 __func__, ret, escape_str(op.marker).c_str(), escape_str(op.name_filter).c_str(), max);
2958 return ret;
2959 }
2960
2961 count = ret;
2962 CLS_LOG(20, "%s: found %d plain ascii (low) entries, count=%u", __func__, ret, count);
2963
2964 if (!more) {
2965 ret = list_instance_entries(hctx, op.name_filter, op.marker, max - count, &op_ret.entries, &more);
2966 if (ret < 0) {
2967 CLS_LOG(0, "ERROR: %s: list_instance_entries returned ret=%d", __func__, ret);
2968 return ret;
2969 }
2970
2971 count += ret;
2972 CLS_LOG(20, "%s: found %d instance entries, count=%u", __func__, ret, count);
2973 }
2974
2975 if (!more) {
2976 ret = list_olh_entries(hctx, op.name_filter, op.marker, max - count, &op_ret.entries, &more);
2977 if (ret < 0) {
2978 CLS_LOG(0, "ERROR: %s: list_olh_entries returned ret=%d", __func__, ret);
2979 return ret;
2980 }
2981
2982 count += ret;
2983 CLS_LOG(20, "%s: found %d olh entries, count=%u", __func__, ret, count);
2984 }
2985
2986 if (!more) {
2987 ret = list_plain_entries(hctx, op.name_filter, op.marker, max - count,
2988 &op_ret.entries, &more, PlainEntriesRegion::High);
2989 if (ret < 0) {
2990 CLS_LOG(0, "ERROR: %s: list_plain_entries (high) returned ret=%d, marker=\"%s\", filter=\"%s\", max=%d",
2991 __func__, ret, escape_str(op.marker).c_str(), escape_str(op.name_filter).c_str(), max);
2992 return ret;
2993 }
2994
2995 count += ret;
2996 CLS_LOG(20, "%s: found %d non-ascii (high) plain entries, count=%u", __func__, ret, count);
2997 }
2998
2999 op_ret.is_truncated = (count > max) || more;
3000 while (count > max) {
3001 op_ret.entries.pop_back();
3002 count--;
3003 }
3004
3005 CLS_LOG(20, "%s: returning %lu entries, is_truncated=%d", __func__, op_ret.entries.size(), op_ret.is_truncated);
3006 encode(op_ret, *out);
3007
3008 return 0;
3009 } // rgw_bi_list_op
3010
3011
3012 int bi_log_record_decode(bufferlist& bl, rgw_bi_log_entry& e)
3013 {
3014 auto iter = bl.cbegin();
3015 try {
3016 decode(e, iter);
3017 } catch (ceph::buffer::error& err) {
3018 CLS_LOG(0, "ERROR: failed to decode rgw_bi_log_entry");
3019 return -EIO;
3020 }
3021 return 0;
3022 }
3023
3024
3025 static int bi_log_iterate_entries(cls_method_context_t hctx,
3026 const string& marker,
3027 const string& end_marker,
3028 string& key_iter,
3029 uint32_t max_entries,
3030 bool *truncated,
3031 int (*cb)(cls_method_context_t, const string&, rgw_bi_log_entry&, void *),
3032 void *param)
3033 {
3034 CLS_LOG(10, "bi_log_iterate_range");
3035
3036 map<string, bufferlist> keys;
3037 string filter_prefix, end_key;
3038 uint32_t i = 0;
3039 string key;
3040
3041 if (truncated)
3042 *truncated = false;
3043
3044 string start_after_key;
3045 if (key_iter.empty()) {
3046 key = BI_PREFIX_CHAR;
3047 key.append(bucket_index_prefixes[BI_BUCKET_LOG_INDEX]);
3048 key.append(marker);
3049
3050 start_after_key = key;
3051 } else {
3052 start_after_key = key_iter;
3053 }
3054
3055 if (end_marker.empty()) {
3056 end_key = BI_PREFIX_CHAR;
3057 end_key.append(bucket_index_prefixes[BI_BUCKET_LOG_INDEX + 1]);
3058 } else {
3059 end_key = BI_PREFIX_CHAR;
3060 end_key.append(bucket_index_prefixes[BI_BUCKET_LOG_INDEX]);
3061 end_key.append(end_marker);
3062 }
3063
3064 CLS_LOG(10, "bi_log_iterate_entries start_after_key=%s end_key=%s",
3065 start_after_key.c_str(), end_key.c_str());
3066
3067 string filter;
3068
3069 int ret = cls_cxx_map_get_vals(hctx, start_after_key, filter, max_entries,
3070 &keys, truncated);
3071 if (ret < 0)
3072 return ret;
3073
3074 auto iter = keys.begin();
3075 if (iter == keys.end())
3076 return 0;
3077
3078 uint32_t num_keys = keys.size();
3079
3080 for (; iter != keys.end(); ++iter,++i) {
3081 const string& key = iter->first;
3082 rgw_bi_log_entry e;
3083
3084 CLS_LOG(10, "bi_log_iterate_entries key=%s bl.length=%d", key.c_str(), (int)iter->second.length());
3085
3086 if (key.compare(end_key) > 0) {
3087 key_iter = key;
3088 if (truncated) {
3089 *truncated = false;
3090 }
3091 return 0;
3092 }
3093
3094 ret = bi_log_record_decode(iter->second, e);
3095 if (ret < 0)
3096 return ret;
3097
3098 ret = cb(hctx, key, e, param);
3099 if (ret < 0)
3100 return ret;
3101
3102 if (i == num_keys - 1) {
3103 key_iter = key;
3104 }
3105 }
3106
3107 return 0;
3108 }
3109
3110 static int bi_log_list_cb(cls_method_context_t hctx, const string& key, rgw_bi_log_entry& info, void *param)
3111 {
3112 list<rgw_bi_log_entry> *l = (list<rgw_bi_log_entry> *)param;
3113 l->push_back(info);
3114 return 0;
3115 }
3116
3117 static int bi_log_list_entries(cls_method_context_t hctx, const string& marker,
3118 uint32_t max, list<rgw_bi_log_entry>& entries, bool *truncated)
3119 {
3120 string key_iter;
3121 string end_marker;
3122 int ret = bi_log_iterate_entries(hctx, marker, end_marker,
3123 key_iter, max, truncated,
3124 bi_log_list_cb, &entries);
3125 return ret;
3126 }
3127
3128 static int rgw_bi_log_list(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3129 {
3130 CLS_LOG(10, "entered %s", __func__);
3131 auto in_iter = in->cbegin();
3132
3133 cls_rgw_bi_log_list_op op;
3134 try {
3135 decode(op, in_iter);
3136 } catch (ceph::buffer::error& err) {
3137 CLS_LOG(1, "ERROR: rgw_bi_log_list(): failed to decode entry\n");
3138 return -EINVAL;
3139 }
3140
3141 cls_rgw_bi_log_list_ret op_ret;
3142 int ret = bi_log_list_entries(hctx, op.marker, op.max, op_ret.entries, &op_ret.truncated);
3143 if (ret < 0)
3144 return ret;
3145
3146 encode(op_ret, *out);
3147
3148 return 0;
3149 }
3150
3151 static int rgw_bi_log_trim(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3152 {
3153 CLS_LOG(10, "entered %s", __func__);
3154 auto in_iter = in->cbegin();
3155
3156 cls_rgw_bi_log_trim_op op;
3157 try {
3158 decode(op, in_iter);
3159 } catch (ceph::buffer::error& err) {
3160 CLS_LOG(1, "ERROR: rgw_bi_log_list(): failed to decode entry\n");
3161 return -EINVAL;
3162 }
3163
3164 string key_begin(1, BI_PREFIX_CHAR);
3165 key_begin.append(bucket_index_prefixes[BI_BUCKET_LOG_INDEX]);
3166 key_begin.append(op.start_marker);
3167
3168 string key_end;
3169 if (op.end_marker.empty()) {
3170 key_end = BI_PREFIX_CHAR;
3171 key_end.append(bucket_index_prefixes[BI_BUCKET_LOG_INDEX + 1]);
3172 } else {
3173 key_end = BI_PREFIX_CHAR;
3174 key_end.append(bucket_index_prefixes[BI_BUCKET_LOG_INDEX]);
3175 key_end.append(op.end_marker);
3176 // cls_cxx_map_remove_range() expects one-past-end
3177 key_end.append(1, '\0');
3178 }
3179
3180 // list a single key to detect whether the range is empty
3181 const size_t max_entries = 1;
3182 std::set<std::string> keys;
3183 bool more = false;
3184
3185 int rc = cls_cxx_map_get_keys(hctx, key_begin, max_entries, &keys, &more);
3186 if (rc < 0) {
3187 CLS_LOG(1, "ERROR: cls_cxx_map_get_keys failed rc=%d", rc);
3188 return rc;
3189 }
3190
3191 if (keys.empty()) {
3192 CLS_LOG(20, "range is empty key_begin=%s", key_begin.c_str());
3193 return -ENODATA;
3194 }
3195
3196 const std::string& first_key = *keys.begin();
3197 if (key_end < first_key) {
3198 CLS_LOG(20, "listed key %s past key_end=%s", first_key.c_str(), key_end.c_str());
3199 return -ENODATA;
3200 }
3201
3202 CLS_LOG(20, "listed key %s, removing through %s",
3203 first_key.c_str(), key_end.c_str());
3204
3205 rc = cls_cxx_map_remove_range(hctx, first_key, key_end);
3206 if (rc < 0) {
3207 CLS_LOG(1, "ERROR: cls_cxx_map_remove_range failed rc=%d", rc);
3208 return rc;
3209 }
3210 return 0;
3211 }
3212
3213 static int rgw_bi_log_resync(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3214 {
3215 CLS_LOG(10, "entered %s", __func__);
3216 rgw_bucket_dir_header header;
3217 int rc = read_bucket_header(hctx, &header);
3218 if (rc < 0) {
3219 CLS_LOG(1, "ERROR: rgw_bucket_complete_op(): failed to read header\n");
3220 return rc;
3221 }
3222
3223 bufferlist bl;
3224
3225 rgw_bi_log_entry entry;
3226
3227 entry.timestamp = real_clock::now();
3228 entry.op = RGWModifyOp::CLS_RGW_OP_RESYNC;
3229 entry.state = RGWPendingState::CLS_RGW_STATE_COMPLETE;
3230
3231 string key;
3232 bi_log_index_key(hctx, key, entry.id, header.ver);
3233
3234 encode(entry, bl);
3235
3236 if (entry.id > header.max_marker)
3237 header.max_marker = entry.id;
3238
3239 header.syncstopped = false;
3240
3241 rc = cls_cxx_map_set_val(hctx, key, &bl);
3242 if (rc < 0)
3243 return rc;
3244
3245 return write_bucket_header(hctx, &header);
3246 }
3247
3248 static int rgw_bi_log_stop(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3249 {
3250 CLS_LOG(10, "entered %s", __func__);
3251 rgw_bucket_dir_header header;
3252 int rc = read_bucket_header(hctx, &header);
3253 if (rc < 0) {
3254 CLS_LOG(1, "ERROR: rgw_bucket_complete_op(): failed to read header\n");
3255 return rc;
3256 }
3257
3258 bufferlist bl;
3259
3260 rgw_bi_log_entry entry;
3261
3262 entry.timestamp = real_clock::now();
3263 entry.op = RGWModifyOp::CLS_RGW_OP_SYNCSTOP;
3264 entry.state = RGWPendingState::CLS_RGW_STATE_COMPLETE;
3265
3266 string key;
3267 bi_log_index_key(hctx, key, entry.id, header.ver);
3268
3269 encode(entry, bl);
3270
3271 if (entry.id > header.max_marker)
3272 header.max_marker = entry.id;
3273 header.syncstopped = true;
3274
3275 rc = cls_cxx_map_set_val(hctx, key, &bl);
3276 if (rc < 0)
3277 return rc;
3278
3279 return write_bucket_header(hctx, &header);
3280 }
3281
3282
3283 static void usage_record_prefix_by_time(uint64_t epoch, string& key)
3284 {
3285 char buf[32];
3286 snprintf(buf, sizeof(buf), "%011llu", (long long unsigned)epoch);
3287 key = buf;
3288 }
3289
3290 static void usage_record_prefix_by_user(const string& user, uint64_t epoch, string& key)
3291 {
3292 char buf[user.size() + 32];
3293 snprintf(buf, sizeof(buf), "%s_%011llu_", user.c_str(), (long long unsigned)epoch);
3294 key = buf;
3295 }
3296
3297 static void usage_record_name_by_time(uint64_t epoch, const string& user, const string& bucket, string& key)
3298 {
3299 char buf[32 + user.size() + bucket.size()];
3300 snprintf(buf, sizeof(buf), "%011llu_%s_%s", (long long unsigned)epoch, user.c_str(), bucket.c_str());
3301 key = buf;
3302 }
3303
3304 static void usage_record_name_by_user(const string& user, uint64_t epoch, const string& bucket, string& key)
3305 {
3306 char buf[32 + user.size() + bucket.size()];
3307 snprintf(buf, sizeof(buf), "%s_%011llu_%s", user.c_str(), (long long unsigned)epoch, bucket.c_str());
3308 key = buf;
3309 }
3310
3311 static int usage_record_decode(bufferlist& record_bl, rgw_usage_log_entry& e)
3312 {
3313 auto kiter = record_bl.cbegin();
3314 try {
3315 decode(e, kiter);
3316 } catch (ceph::buffer::error& err) {
3317 CLS_LOG(1, "ERROR: usage_record_decode(): failed to decode record_bl\n");
3318 return -EINVAL;
3319 }
3320
3321 return 0;
3322 }
3323
3324 int rgw_user_usage_log_add(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3325 {
3326 CLS_LOG(10, "entered %s", __func__);
3327
3328 auto in_iter = in->cbegin();
3329 rgw_cls_usage_log_add_op op;
3330
3331 try {
3332 decode(op, in_iter);
3333 } catch (ceph::buffer::error& err) {
3334 CLS_LOG(1, "ERROR: rgw_user_usage_log_add(): failed to decode request\n");
3335 return -EINVAL;
3336 }
3337
3338 rgw_usage_log_info& info = op.info;
3339
3340 for (auto iter = info.entries.begin(); iter != info.entries.end(); ++iter) {
3341 rgw_usage_log_entry& entry = *iter;
3342 string key_by_time;
3343
3344 rgw_user *puser = (entry.payer.empty() ? &entry.owner : &entry.payer);
3345
3346 usage_record_name_by_time(entry.epoch, puser->to_str(), entry.bucket, key_by_time);
3347
3348 CLS_LOG(10, "rgw_user_usage_log_add user=%s bucket=%s", puser->to_str().c_str(), entry.bucket.c_str());
3349
3350 bufferlist record_bl;
3351 int ret = cls_cxx_map_get_val(hctx, key_by_time, &record_bl);
3352 if (ret < 0 && ret != -ENOENT) {
3353 CLS_LOG(1, "ERROR: rgw_user_usage_log_add(): cls_cxx_map_read_key returned %d", ret);
3354 return -EINVAL;
3355 }
3356 if (ret >= 0) {
3357 rgw_usage_log_entry e;
3358 ret = usage_record_decode(record_bl, e);
3359 if (ret < 0)
3360 return ret;
3361 CLS_LOG(10, "rgw_user_usage_log_add aggregating existing bucket\n");
3362 entry.aggregate(e);
3363 }
3364
3365 bufferlist new_record_bl;
3366 encode(entry, new_record_bl);
3367 ret = cls_cxx_map_set_val(hctx, key_by_time, &new_record_bl);
3368 if (ret < 0)
3369 return ret;
3370
3371 string key_by_user;
3372 usage_record_name_by_user(puser->to_str(), entry.epoch, entry.bucket, key_by_user);
3373 ret = cls_cxx_map_set_val(hctx, key_by_user, &new_record_bl);
3374 if (ret < 0)
3375 return ret;
3376 }
3377
3378 return 0;
3379 }
3380
3381 static int usage_iterate_range(cls_method_context_t hctx, uint64_t start, uint64_t end, const string& user,
3382 const string& bucket, string& key_iter, uint32_t max_entries, bool *truncated,
3383 int (*cb)(cls_method_context_t, const string&, rgw_usage_log_entry&, void *),
3384 void *param)
3385 {
3386 CLS_LOG(10, "entered %s", __func__);
3387
3388 map<string, bufferlist> keys;
3389 string filter_prefix;
3390 string start_key, end_key;
3391 bool by_user = !user.empty();
3392 string user_key;
3393 bool truncated_status = false;
3394
3395 ceph_assert(truncated != nullptr);
3396
3397 if (!by_user) {
3398 usage_record_prefix_by_time(end, end_key);
3399 } else {
3400 user_key = user;
3401 user_key.append("_");
3402 }
3403
3404 if (key_iter.empty()) {
3405 if (by_user) {
3406 usage_record_prefix_by_user(user, start, start_key);
3407 } else {
3408 usage_record_prefix_by_time(start, start_key);
3409 }
3410 } else {
3411 start_key = key_iter;
3412 }
3413
3414 CLS_LOG(20, "usage_iterate_range start_key=%s", start_key.c_str());
3415 int ret = cls_cxx_map_get_vals(hctx, start_key, filter_prefix, max_entries, &keys, &truncated_status);
3416 if (ret < 0)
3417 return ret;
3418
3419 *truncated = truncated_status;
3420
3421 auto iter = keys.begin();
3422 if (iter == keys.end())
3423 return 0;
3424
3425 for (; iter != keys.end(); ++iter) {
3426 const string& key = iter->first;
3427 rgw_usage_log_entry e;
3428
3429 key_iter = key;
3430 if (!by_user && key.compare(end_key) >= 0) {
3431 CLS_LOG(20, "usage_iterate_range reached key=%s, done", key.c_str());
3432 *truncated = false;
3433 key_iter = key;
3434 return 0;
3435 }
3436
3437 if (by_user && key.compare(0, user_key.size(), user_key) != 0) {
3438 CLS_LOG(20, "usage_iterate_range reached key=%s, done", key.c_str());
3439 *truncated = false;
3440 key_iter = key;
3441 return 0;
3442 }
3443
3444 ret = usage_record_decode(iter->second, e);
3445 if (ret < 0)
3446 return ret;
3447
3448 if (!bucket.empty() && bucket.compare(e.bucket))
3449 continue;
3450
3451 if (e.epoch < start)
3452 continue;
3453
3454 /* keys are sorted by epoch, so once we're past end we're done */
3455 if (e.epoch >= end) {
3456 *truncated = false;
3457 return 0;
3458 }
3459
3460 ret = cb(hctx, key, e, param);
3461 if (ret < 0)
3462 return ret;
3463 }
3464 return 0;
3465 }
3466
3467 static int usage_log_read_cb(cls_method_context_t hctx, const string& key, rgw_usage_log_entry& entry, void *param)
3468 {
3469 map<rgw_user_bucket, rgw_usage_log_entry> *usage = (map<rgw_user_bucket, rgw_usage_log_entry> *)param;
3470 rgw_user *puser;
3471 if (!entry.payer.empty()) {
3472 puser = &entry.payer;
3473 } else {
3474 puser = &entry.owner;
3475 }
3476 rgw_user_bucket ub(puser->to_str(), entry.bucket);
3477 rgw_usage_log_entry& le = (*usage)[ub];
3478 le.aggregate(entry);
3479
3480 return 0;
3481 }
3482
3483 int rgw_user_usage_log_read(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3484 {
3485 CLS_LOG(10, "entered %s", __func__);
3486
3487 auto in_iter = in->cbegin();
3488 rgw_cls_usage_log_read_op op;
3489
3490 try {
3491 decode(op, in_iter);
3492 } catch (ceph::buffer::error& err) {
3493 CLS_LOG(1, "ERROR: rgw_user_usage_log_read(): failed to decode request\n");
3494 return -EINVAL;
3495 }
3496
3497 rgw_cls_usage_log_read_ret ret_info;
3498 map<rgw_user_bucket, rgw_usage_log_entry> *usage = &ret_info.usage;
3499 string iter = op.iter;
3500 #define MAX_ENTRIES 1000
3501 uint32_t max_entries = (op.max_entries ? op.max_entries : MAX_ENTRIES);
3502 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);
3503 if (ret < 0)
3504 return ret;
3505
3506 if (ret_info.truncated)
3507 ret_info.next_iter = iter;
3508
3509 encode(ret_info, *out);
3510 return 0;
3511 }
3512
3513 static int usage_log_trim_cb(cls_method_context_t hctx, const string& key, rgw_usage_log_entry& entry, void *param)
3514 {
3515 bool *found = (bool *)param;
3516 if (found) {
3517 *found = true;
3518 }
3519 string key_by_time;
3520 string key_by_user;
3521
3522 string o = entry.owner.to_str();
3523 usage_record_name_by_time(entry.epoch, o, entry.bucket, key_by_time);
3524 usage_record_name_by_user(o, entry.epoch, entry.bucket, key_by_user);
3525
3526 int ret = cls_cxx_map_remove_key(hctx, key_by_time);
3527 if (ret < 0)
3528 return ret;
3529
3530 return cls_cxx_map_remove_key(hctx, key_by_user);
3531 }
3532
3533 int rgw_user_usage_log_trim(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3534 {
3535 CLS_LOG(10, "entered %s", __func__);
3536
3537 /* only continue if object exists! */
3538 int ret = cls_cxx_stat(hctx, NULL, NULL);
3539 if (ret < 0)
3540 return ret;
3541
3542 auto in_iter = in->cbegin();
3543 rgw_cls_usage_log_trim_op op;
3544
3545 try {
3546 decode(op, in_iter);
3547 } catch (ceph::buffer::error& err) {
3548 CLS_LOG(1, "ERROR: rgw_user_log_usage_log_trim(): failed to decode request\n");
3549 return -EINVAL;
3550 }
3551
3552 string iter;
3553 bool more;
3554 bool found = false;
3555 #define MAX_USAGE_TRIM_ENTRIES 1000
3556 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);
3557 if (ret < 0)
3558 return ret;
3559
3560 if (!more && !found)
3561 return -ENODATA;
3562
3563 return 0;
3564 }
3565
3566 int rgw_usage_log_clear(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3567 {
3568 CLS_LOG(10, "entered %s", __func__);
3569
3570 int ret = cls_cxx_map_clear(hctx);
3571 /* if object doesn't exist all the logs are cleared anyway */
3572 if (ret == -ENOENT)
3573 ret = 0;
3574
3575 return ret;
3576 }
3577
3578 /*
3579 * We hold the garbage collection chain data under two different
3580 * indexes: the first 'name' index keeps them under a unique tag that
3581 * represents the chains, and a second 'time' index keeps them by
3582 * their expiration timestamp. Each is prefixed differently (see
3583 * gc_index_prefixes below).
3584 *
3585 * Since key-value data is listed in lexical order by keys, generally
3586 * the name entries are retrieved first and then the time entries.
3587 * When listing the entries via `gc_iterate_entries` one parameter is
3588 * a marker, and if we were to pass "1_" (i.e.,
3589 * gc_index_prefixes[GC_OBJ_TIME_INDEX]), the listing would skip over
3590 * the 'name' entries and begin with the 'time' entries.
3591 *
3592 * Furthermore, the times are converted to strings such that lexical
3593 * order correlates with chronological order, so the entries are
3594 * returned chronologically from the earliest expiring to the latest
3595 * expiring. This allows for starting at "1_" and to keep retrieving
3596 * chunks of entries, and as long as they are prior to the current
3597 * time, they're expired and processing can continue.
3598 */
3599 #define GC_OBJ_NAME_INDEX 0
3600 #define GC_OBJ_TIME_INDEX 1
3601
3602 static string gc_index_prefixes[] = { "0_",
3603 "1_" };
3604
3605 static void prepend_index_prefix(const string& src, int index, string *dest)
3606 {
3607 *dest = gc_index_prefixes[index];
3608 dest->append(src);
3609 }
3610
3611 static int gc_omap_get(cls_method_context_t hctx, int type, const string& key, cls_rgw_gc_obj_info *info)
3612 {
3613 string index;
3614 prepend_index_prefix(key, type, &index);
3615
3616 int ret = read_omap_entry(hctx, index, info);
3617 if (ret < 0)
3618 return ret;
3619
3620 return 0;
3621 }
3622
3623 static int gc_omap_set(cls_method_context_t hctx, int type, const string& key, const cls_rgw_gc_obj_info *info)
3624 {
3625 bufferlist bl;
3626 encode(*info, bl);
3627
3628 string index = gc_index_prefixes[type];
3629 index.append(key);
3630
3631 int ret = cls_cxx_map_set_val(hctx, index, &bl);
3632 if (ret < 0)
3633 return ret;
3634
3635 return 0;
3636 }
3637
3638 static int gc_omap_remove(cls_method_context_t hctx, int type, const string& key)
3639 {
3640 string index = gc_index_prefixes[type];
3641 index.append(key);
3642
3643 int ret = cls_cxx_map_remove_key(hctx, index);
3644 if (ret < 0)
3645 return ret;
3646
3647 return 0;
3648 }
3649
3650 static bool key_in_index(const string& key, int index_type)
3651 {
3652 const string& prefix = gc_index_prefixes[index_type];
3653 return (key.compare(0, prefix.size(), prefix) == 0);
3654 }
3655
3656
3657 static int gc_update_entry(cls_method_context_t hctx, uint32_t expiration_secs,
3658 cls_rgw_gc_obj_info& info)
3659 {
3660 cls_rgw_gc_obj_info old_info;
3661 int ret = gc_omap_get(hctx, GC_OBJ_NAME_INDEX, info.tag, &old_info);
3662 if (ret == 0) {
3663 string key;
3664 get_time_key(old_info.time, &key);
3665 ret = gc_omap_remove(hctx, GC_OBJ_TIME_INDEX, key);
3666 if (ret < 0 && ret != -ENOENT) {
3667 CLS_LOG(0, "ERROR: failed to remove key=%s", key.c_str());
3668 return ret;
3669 }
3670 }
3671
3672 // calculate time and time key
3673 info.time = ceph::real_clock::now();
3674 info.time += make_timespan(expiration_secs);
3675 string time_key;
3676 get_time_key(info.time, &time_key);
3677
3678 if (info.chain.objs.empty()) {
3679 CLS_LOG(0,
3680 "WARNING: %s setting GC log entry with zero-length chain, "
3681 "tag='%s', timekey='%s'",
3682 __func__, info.tag.c_str(), time_key.c_str());
3683 }
3684
3685 ret = gc_omap_set(hctx, GC_OBJ_NAME_INDEX, info.tag, &info);
3686 if (ret < 0)
3687 return ret;
3688
3689 ret = gc_omap_set(hctx, GC_OBJ_TIME_INDEX, time_key, &info);
3690 if (ret < 0)
3691 goto done_err;
3692
3693 return 0;
3694
3695 done_err:
3696
3697 CLS_LOG(0, "ERROR: gc_set_entry error info.tag=%s, ret=%d",
3698 info.tag.c_str(), ret);
3699 gc_omap_remove(hctx, GC_OBJ_NAME_INDEX, info.tag);
3700
3701 return ret;
3702 }
3703
3704 static int gc_defer_entry(cls_method_context_t hctx, const string& tag, uint32_t expiration_secs)
3705 {
3706 cls_rgw_gc_obj_info info;
3707 int ret = gc_omap_get(hctx, GC_OBJ_NAME_INDEX, tag, &info);
3708 if (ret < 0)
3709 return ret;
3710 return gc_update_entry(hctx, expiration_secs, info);
3711 }
3712
3713 int gc_record_decode(bufferlist& bl, cls_rgw_gc_obj_info& e)
3714 {
3715 auto iter = bl.cbegin();
3716 try {
3717 decode(e, iter);
3718 } catch (ceph::buffer::error& err) {
3719 CLS_LOG(0, "ERROR: failed to decode cls_rgw_gc_obj_info");
3720 return -EIO;
3721 }
3722 return 0;
3723 }
3724
3725 static int rgw_cls_gc_set_entry(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3726 {
3727 CLS_LOG(10, "entered %s", __func__);
3728 auto in_iter = in->cbegin();
3729
3730 cls_rgw_gc_set_entry_op op;
3731 try {
3732 decode(op, in_iter);
3733 } catch (ceph::buffer::error& err) {
3734 CLS_LOG(1, "ERROR: rgw_cls_gc_set_entry(): failed to decode entry\n");
3735 return -EINVAL;
3736 }
3737
3738 return gc_update_entry(hctx, op.expiration_secs, op.info);
3739 }
3740
3741 static int rgw_cls_gc_defer_entry(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3742 {
3743 CLS_LOG(10, "entered %s", __func__);
3744 auto in_iter = in->cbegin();
3745
3746 cls_rgw_gc_defer_entry_op op;
3747 try {
3748 decode(op, in_iter);
3749 } catch (ceph::buffer::error& err) {
3750 CLS_LOG(1, "ERROR: rgw_cls_gc_defer_entry(): failed to decode entry\n");
3751 return -EINVAL;
3752 }
3753
3754 return gc_defer_entry(hctx, op.tag, op.expiration_secs);
3755 }
3756
3757 static int gc_iterate_entries(cls_method_context_t hctx,
3758 const string& marker,
3759 bool expired_only,
3760 string& out_marker,
3761 uint32_t max_entries,
3762 bool *truncated,
3763 int (*cb)(cls_method_context_t,
3764 const string&,
3765 cls_rgw_gc_obj_info&,
3766 void *),
3767 void *param)
3768 {
3769 CLS_LOG(10, "gc_iterate_entries");
3770
3771 map<string, bufferlist> keys;
3772 string filter_prefix, end_key;
3773 string key;
3774
3775 if (truncated)
3776 *truncated = false;
3777
3778 string start_key;
3779 if (marker.empty()) {
3780 prepend_index_prefix(marker, GC_OBJ_TIME_INDEX, &start_key);
3781 } else {
3782 start_key = marker;
3783 }
3784
3785 if (expired_only) {
3786 real_time now = ceph::real_clock::now();
3787 string now_str;
3788 get_time_key(now, &now_str);
3789 prepend_index_prefix(now_str, GC_OBJ_TIME_INDEX, &end_key);
3790
3791 CLS_LOG(10, "gc_iterate_entries end_key=%s", end_key.c_str());
3792 }
3793
3794 string filter;
3795
3796 int ret = cls_cxx_map_get_vals(hctx, start_key, filter, max_entries,
3797 &keys, truncated);
3798 if (ret < 0)
3799 return ret;
3800
3801 auto iter = keys.begin();
3802 if (iter == keys.end()) {
3803 // if keys empty must not come back as truncated
3804 ceph_assert(!truncated || !(*truncated));
3805 return 0;
3806 }
3807
3808 const string* last_key = nullptr; // last key processed, for end-marker
3809 for (; iter != keys.end(); ++iter) {
3810 const string& key = iter->first;
3811 cls_rgw_gc_obj_info e;
3812
3813 CLS_LOG(10, "gc_iterate_entries key=%s", key.c_str());
3814
3815 if (!end_key.empty() && key.compare(end_key) >= 0) {
3816 if (truncated)
3817 *truncated = false;
3818 return 0;
3819 }
3820
3821 if (!key_in_index(key, GC_OBJ_TIME_INDEX)) {
3822 if (truncated)
3823 *truncated = false;
3824 return 0;
3825 }
3826
3827 ret = gc_record_decode(iter->second, e);
3828 if (ret < 0)
3829 return ret;
3830
3831 ret = cb(hctx, key, e, param);
3832 if (ret < 0)
3833 return ret;
3834 last_key = &(iter->first); // update when callback successful
3835 }
3836
3837 // set the out marker if either caller does not capture truncated or
3838 // if they do capture and we are truncated
3839 if (!truncated || *truncated) {
3840 assert(last_key);
3841 out_marker = *last_key;
3842 }
3843
3844 return 0;
3845 }
3846
3847 static int gc_list_cb(cls_method_context_t hctx, const string& key, cls_rgw_gc_obj_info& info, void *param)
3848 {
3849 list<cls_rgw_gc_obj_info> *l = (list<cls_rgw_gc_obj_info> *)param;
3850 l->push_back(info);
3851 return 0;
3852 }
3853
3854 static int gc_list_entries(cls_method_context_t hctx, const string& marker,
3855 uint32_t max, bool expired_only,
3856 list<cls_rgw_gc_obj_info>& entries, bool *truncated, string& next_marker)
3857 {
3858 int ret = gc_iterate_entries(hctx, marker, expired_only,
3859 next_marker, max, truncated,
3860 gc_list_cb, &entries);
3861 return ret;
3862 }
3863
3864 static int rgw_cls_gc_list(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3865 {
3866 CLS_LOG(10, "entered %s", __func__);
3867 auto in_iter = in->cbegin();
3868
3869 cls_rgw_gc_list_op op;
3870 try {
3871 decode(op, in_iter);
3872 } catch (ceph::buffer::error& err) {
3873 CLS_LOG(1, "ERROR: rgw_cls_gc_list(): failed to decode entry\n");
3874 return -EINVAL;
3875 }
3876
3877 cls_rgw_gc_list_ret op_ret;
3878 #define GC_LIST_ENTRIES_DEFAULT 128
3879 int ret = gc_list_entries(hctx, op.marker, (op.max ? op.max : GC_LIST_ENTRIES_DEFAULT), op.expired_only,
3880 op_ret.entries, &op_ret.truncated, op_ret.next_marker);
3881 if (ret < 0)
3882 return ret;
3883
3884 encode(op_ret, *out);
3885
3886 return 0;
3887 }
3888
3889 static int gc_remove(cls_method_context_t hctx, vector<string>& tags)
3890 {
3891 for (auto iter = tags.begin(); iter != tags.end(); ++iter) {
3892 string& tag = *iter;
3893 cls_rgw_gc_obj_info info;
3894 int ret = gc_omap_get(hctx, GC_OBJ_NAME_INDEX, tag, &info);
3895 if (ret == -ENOENT) {
3896 CLS_LOG(0, "couldn't find tag in name index tag=%s", tag.c_str());
3897 continue;
3898 }
3899
3900 if (ret < 0)
3901 return ret;
3902
3903 string time_key;
3904 get_time_key(info.time, &time_key);
3905 ret = gc_omap_remove(hctx, GC_OBJ_TIME_INDEX, time_key);
3906 if (ret < 0 && ret != -ENOENT)
3907 return ret;
3908 if (ret == -ENOENT) {
3909 CLS_LOG(0, "couldn't find key in time index key=%s", time_key.c_str());
3910 }
3911
3912 ret = gc_omap_remove(hctx, GC_OBJ_NAME_INDEX, tag);
3913 if (ret < 0 && ret != -ENOENT)
3914 return ret;
3915 }
3916
3917 return 0;
3918 }
3919
3920 static int rgw_cls_gc_remove(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3921 {
3922 CLS_LOG(10, "entered %s", __func__);
3923 auto in_iter = in->cbegin();
3924
3925 cls_rgw_gc_remove_op op;
3926 try {
3927 decode(op, in_iter);
3928 } catch (ceph::buffer::error& err) {
3929 CLS_LOG(1, "ERROR: rgw_cls_gc_remove(): failed to decode entry\n");
3930 return -EINVAL;
3931 }
3932
3933 return gc_remove(hctx, op.tags);
3934 }
3935
3936 static int rgw_cls_lc_get_entry(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3937 {
3938 CLS_LOG(10, "entered %s", __func__);
3939 auto in_iter = in->cbegin();
3940
3941 cls_rgw_lc_get_entry_op op;
3942 try {
3943 decode(op, in_iter);
3944 } catch (ceph::buffer::error& err) {
3945 CLS_LOG(1, "ERROR: rgw_cls_lc_set_entry(): failed to decode entry\n");
3946 return -EINVAL;
3947 }
3948
3949 cls_rgw_lc_entry lc_entry;
3950 int ret = read_omap_entry(hctx, op.marker, &lc_entry);
3951 if (ret < 0)
3952 return ret;
3953
3954 cls_rgw_lc_get_entry_ret op_ret(std::move(lc_entry));
3955 encode(op_ret, *out);
3956 return 0;
3957 }
3958
3959
3960 static int rgw_cls_lc_set_entry(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3961 {
3962 CLS_LOG(10, "entered %s", __func__);
3963 auto in_iter = in->cbegin();
3964
3965 cls_rgw_lc_set_entry_op op;
3966 try {
3967 decode(op, in_iter);
3968 } catch (ceph::buffer::error& err) {
3969 CLS_LOG(1, "ERROR: rgw_cls_lc_set_entry(): failed to decode entry\n");
3970 return -EINVAL;
3971 }
3972
3973 bufferlist bl;
3974 encode(op.entry, bl);
3975
3976 int ret = cls_cxx_map_set_val(hctx, op.entry.bucket, &bl);
3977 return ret;
3978 }
3979
3980 static int rgw_cls_lc_rm_entry(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3981 {
3982 CLS_LOG(10, "entered %s", __func__);
3983 auto in_iter = in->cbegin();
3984
3985 cls_rgw_lc_rm_entry_op op;
3986 try {
3987 decode(op, in_iter);
3988 } catch (ceph::buffer::error& err) {
3989 CLS_LOG(1, "ERROR: rgw_cls_lc_rm_entry(): failed to decode entry\n");
3990 return -EINVAL;
3991 }
3992
3993 int ret = cls_cxx_map_remove_key(hctx, op.entry.bucket);
3994 return ret;
3995 }
3996
3997 static int rgw_cls_lc_get_next_entry(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3998 {
3999 CLS_LOG(10, "entered %s", __func__);
4000 auto in_iter = in->cbegin();
4001 cls_rgw_lc_get_next_entry_ret op_ret;
4002 cls_rgw_lc_get_next_entry_op op;
4003 try {
4004 decode(op, in_iter);
4005 } catch (ceph::buffer::error& err) {
4006 CLS_LOG(1, "ERROR: rgw_cls_lc_get_next_entry: failed to decode op\n");
4007 return -EINVAL;
4008 }
4009
4010 map<string, bufferlist> vals;
4011 string filter_prefix;
4012 bool more;
4013 int ret = cls_cxx_map_get_vals(hctx, op.marker, filter_prefix, 1, &vals, &more);
4014 if (ret < 0)
4015 return ret;
4016 cls_rgw_lc_entry entry;
4017 if (!vals.empty()) {
4018 auto it = vals.begin();
4019 in_iter = it->second.begin();
4020 try {
4021 decode(entry, in_iter);
4022 } catch (ceph::buffer::error& err) {
4023 CLS_LOG(1, "ERROR: rgw_cls_lc_get_next_entry(): failed to decode entry\n");
4024 return -EIO;
4025 }
4026 }
4027 op_ret.entry = entry;
4028 encode(op_ret, *out);
4029 return 0;
4030 }
4031
4032 static int rgw_cls_lc_list_entries(cls_method_context_t hctx, bufferlist *in,
4033 bufferlist *out)
4034 {
4035 CLS_LOG(10, "entered %s", __func__);
4036 cls_rgw_lc_list_entries_op op;
4037 auto in_iter = in->cbegin();
4038 try {
4039 decode(op, in_iter);
4040 } catch (ceph::buffer::error& err) {
4041 CLS_LOG(1, "ERROR: rgw_cls_lc_list_entries(): failed to decode op\n");
4042 return -EINVAL;
4043 }
4044
4045 cls_rgw_lc_list_entries_ret op_ret(op.compat_v);
4046 map<string, bufferlist> vals;
4047 string filter_prefix;
4048 int ret = cls_cxx_map_get_vals(hctx, op.marker, filter_prefix, op.max_entries,
4049 &vals, &op_ret.is_truncated);
4050 if (ret < 0)
4051 return ret;
4052 for (auto it = vals.begin(); it != vals.end(); ++it) {
4053 cls_rgw_lc_entry entry;
4054 auto iter = it->second.cbegin();
4055 try {
4056 decode(entry, iter);
4057 } catch (buffer::error& err) {
4058 /* try backward compat */
4059 pair<string, int> oe;
4060 try {
4061 iter = it->second.begin();
4062 decode(oe, iter);
4063 entry = {oe.first, 0 /* start */, uint32_t(oe.second)};
4064 } catch(buffer::error& err) {
4065 CLS_LOG(
4066 1, "ERROR: rgw_cls_lc_list_entries(): failed to decode entry\n");
4067 return -EIO;
4068 }
4069 }
4070 op_ret.entries.push_back(entry);
4071 }
4072 encode(op_ret, *out);
4073 return 0;
4074 }
4075
4076 static int rgw_cls_lc_put_head(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
4077 {
4078 CLS_LOG(10, "entered %s", __func__);
4079 auto in_iter = in->cbegin();
4080
4081 cls_rgw_lc_put_head_op op;
4082 try {
4083 decode(op, in_iter);
4084 } catch (ceph::buffer::error& err) {
4085 CLS_LOG(1, "ERROR: rgw_cls_lc_put_head(): failed to decode entry\n");
4086 return -EINVAL;
4087 }
4088
4089 bufferlist bl;
4090 encode(op.head, bl);
4091 int ret = cls_cxx_map_write_header(hctx,&bl);
4092 return ret;
4093 }
4094
4095 static int rgw_cls_lc_get_head(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
4096 {
4097 CLS_LOG(10, "entered %s", __func__);
4098 bufferlist bl;
4099 int ret = cls_cxx_map_read_header(hctx, &bl);
4100 if (ret < 0)
4101 return ret;
4102 cls_rgw_lc_obj_head head;
4103 if (bl.length() != 0) {
4104 auto iter = bl.cbegin();
4105 try {
4106 decode(head, iter);
4107 } catch (ceph::buffer::error& err) {
4108 CLS_LOG(0, "ERROR: rgw_cls_lc_get_head(): failed to decode entry %s",err.what());
4109 return -EINVAL;
4110 }
4111 } else {
4112 head.start_date = 0;
4113 head.marker.clear();
4114 }
4115 cls_rgw_lc_get_head_ret op_ret;
4116 op_ret.head = head;
4117 encode(op_ret, *out);
4118 return 0;
4119 }
4120
4121 static int rgw_reshard_add(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
4122 {
4123 CLS_LOG(10, "entered %s", __func__);
4124 auto in_iter = in->cbegin();
4125
4126 cls_rgw_reshard_add_op op;
4127 try {
4128 decode(op, in_iter);
4129 } catch (ceph::buffer::error& err) {
4130 CLS_LOG(1, "ERROR: rgw_reshard_add: failed to decode entry\n");
4131 return -EINVAL;
4132 }
4133
4134
4135 string key;
4136 op.entry.get_key(&key);
4137
4138 bufferlist bl;
4139 encode(op.entry, bl);
4140 int ret = cls_cxx_map_set_val(hctx, key, &bl);
4141 if (ret < 0) {
4142 CLS_ERR("error adding reshard job for bucket %s with key %s",op.entry.bucket_name.c_str(), key.c_str());
4143 return ret;
4144 }
4145
4146 return ret;
4147 }
4148
4149 static int rgw_reshard_list(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
4150 {
4151 CLS_LOG(10, "entered %s", __func__);
4152 cls_rgw_reshard_list_op op;
4153 auto in_iter = in->cbegin();
4154 try {
4155 decode(op, in_iter);
4156 } catch (ceph::buffer::error& err) {
4157 CLS_LOG(1, "ERROR: rgw_cls_rehard_list(): failed to decode entry\n");
4158 return -EINVAL;
4159 }
4160 cls_rgw_reshard_list_ret op_ret;
4161 map<string, bufferlist> vals;
4162 string filter_prefix;
4163 #define MAX_RESHARD_LIST_ENTRIES 1000
4164 /* one extra entry for identifying truncation */
4165 int32_t max = (op.max && (op.max < MAX_RESHARD_LIST_ENTRIES) ? op.max : MAX_RESHARD_LIST_ENTRIES);
4166 int ret = cls_cxx_map_get_vals(hctx, op.marker, filter_prefix, max, &vals, &op_ret.is_truncated);
4167 if (ret < 0)
4168 return ret;
4169 cls_rgw_reshard_entry entry;
4170 int i = 0;
4171 for (auto it = vals.begin(); i < (int)op.max && it != vals.end(); ++it, ++i) {
4172 auto iter = it->second.cbegin();
4173 try {
4174 decode(entry, iter);
4175 } catch (ceph::buffer::error& err) {
4176 CLS_LOG(1, "ERROR: rgw_cls_rehard_list(): failed to decode entry\n");
4177 return -EIO;
4178 }
4179 op_ret.entries.push_back(entry);
4180 }
4181 encode(op_ret, *out);
4182 return 0;
4183 }
4184
4185 static int rgw_reshard_get(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
4186 {
4187 CLS_LOG(10, "entered %s", __func__);
4188 auto in_iter = in->cbegin();
4189
4190 cls_rgw_reshard_get_op op;
4191 try {
4192 decode(op, in_iter);
4193 } catch (ceph::buffer::error& err) {
4194 CLS_LOG(1, "ERROR: rgw_reshard_get: failed to decode entry\n");
4195 return -EINVAL;
4196 }
4197
4198 string key;
4199 cls_rgw_reshard_entry entry;
4200 op.entry.get_key(&key);
4201 int ret = read_omap_entry(hctx, key, &entry);
4202 if (ret < 0) {
4203 return ret;
4204 }
4205
4206 cls_rgw_reshard_get_ret op_ret;
4207 op_ret.entry = entry;
4208 encode(op_ret, *out);
4209 return 0;
4210 }
4211
4212 static int rgw_reshard_remove(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
4213 {
4214 CLS_LOG(10, "entered %s", __func__);
4215 auto in_iter = in->cbegin();
4216
4217 cls_rgw_reshard_remove_op op;
4218 try {
4219 decode(op, in_iter);
4220 } catch (ceph::buffer::error& err) {
4221 CLS_LOG(1, "ERROR: rgw_cls_rehard_remove: failed to decode entry\n");
4222 return -EINVAL;
4223 }
4224
4225 string key;
4226 cls_rgw_reshard_entry entry;
4227 cls_rgw_reshard_entry::generate_key(op.tenant, op.bucket_name, &key);
4228 int ret = read_omap_entry(hctx, key, &entry);
4229 if (ret < 0) {
4230 return ret;
4231 }
4232
4233 if (!op.bucket_id.empty() &&
4234 entry.bucket_id != op.bucket_id) {
4235 return 0;
4236 }
4237
4238 ret = cls_cxx_map_remove_key(hctx, key);
4239 if (ret < 0) {
4240 CLS_LOG(0, "ERROR: failed to remove key: key=%s ret=%d", key.c_str(), ret);
4241 return 0;
4242 }
4243 return ret;
4244 }
4245
4246 static int rgw_set_bucket_resharding(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
4247 {
4248 CLS_LOG(10, "entered %s", __func__);
4249 cls_rgw_set_bucket_resharding_op op;
4250
4251 auto in_iter = in->cbegin();
4252 try {
4253 decode(op, in_iter);
4254 } catch (ceph::buffer::error& err) {
4255 CLS_LOG(1, "ERROR: cls_rgw_set_bucket_resharding: failed to decode entry\n");
4256 return -EINVAL;
4257 }
4258
4259 rgw_bucket_dir_header header;
4260 int rc = read_bucket_header(hctx, &header);
4261 if (rc < 0) {
4262 CLS_LOG(1, "ERROR: %s: failed to read header", __func__);
4263 return rc;
4264 }
4265
4266 header.new_instance.set_status(op.entry.new_bucket_instance_id, op.entry.num_shards, op.entry.reshard_status);
4267
4268 return write_bucket_header(hctx, &header);
4269 }
4270
4271 static int rgw_clear_bucket_resharding(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
4272 {
4273 CLS_LOG(10, "entered %s", __func__);
4274 cls_rgw_clear_bucket_resharding_op op;
4275
4276 auto in_iter = in->cbegin();
4277 try {
4278 decode(op, in_iter);
4279 } catch (ceph::buffer::error& err) {
4280 CLS_LOG(1, "ERROR: cls_rgw_clear_bucket_resharding: failed to decode entry\n");
4281 return -EINVAL;
4282 }
4283
4284 rgw_bucket_dir_header header;
4285 int rc = read_bucket_header(hctx, &header);
4286 if (rc < 0) {
4287 CLS_LOG(1, "ERROR: %s: failed to read header", __func__);
4288 return rc;
4289 }
4290 header.new_instance.clear();
4291
4292 return write_bucket_header(hctx, &header);
4293 }
4294
4295 static int rgw_guard_bucket_resharding(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
4296 {
4297 CLS_LOG(10, "entered %s", __func__);
4298 cls_rgw_guard_bucket_resharding_op op;
4299
4300 auto in_iter = in->cbegin();
4301 try {
4302 decode(op, in_iter);
4303 } catch (ceph::buffer::error& err) {
4304 CLS_LOG(1, "ERROR: %s: failed to decode entry", __func__);
4305 return -EINVAL;
4306 }
4307
4308 rgw_bucket_dir_header header;
4309 int rc = read_bucket_header(hctx, &header);
4310 if (rc < 0) {
4311 CLS_LOG(1, "ERROR: %s: failed to read header", __func__);
4312 return rc;
4313 }
4314
4315 if (header.resharding()) {
4316 return op.ret_err;
4317 }
4318
4319 return 0;
4320 }
4321
4322 static int rgw_get_bucket_resharding(cls_method_context_t hctx,
4323 bufferlist *in, bufferlist *out)
4324 {
4325 CLS_LOG(10, "entered %s", __func__);
4326 cls_rgw_get_bucket_resharding_op op;
4327
4328 auto in_iter = in->cbegin();
4329 try {
4330 decode(op, in_iter);
4331 } catch (ceph::buffer::error& err) {
4332 CLS_LOG(1, "ERROR: %s: failed to decode entry", __func__);
4333 return -EINVAL;
4334 }
4335
4336 rgw_bucket_dir_header header;
4337 int rc = read_bucket_header(hctx, &header);
4338 if (rc < 0) {
4339 CLS_LOG(1, "ERROR: %s: failed to read header", __func__);
4340 return rc;
4341 }
4342
4343 cls_rgw_get_bucket_resharding_ret op_ret;
4344 op_ret.new_instance = header.new_instance;
4345
4346 encode(op_ret, *out);
4347
4348 return 0;
4349 }
4350
4351 CLS_INIT(rgw)
4352 {
4353 CLS_LOG(1, "Loaded rgw class!");
4354
4355 cls_handle_t h_class;
4356 cls_method_handle_t h_rgw_bucket_init_index;
4357 cls_method_handle_t h_rgw_bucket_set_tag_timeout;
4358 cls_method_handle_t h_rgw_bucket_list;
4359 cls_method_handle_t h_rgw_bucket_check_index;
4360 cls_method_handle_t h_rgw_bucket_rebuild_index;
4361 cls_method_handle_t h_rgw_bucket_update_stats;
4362 cls_method_handle_t h_rgw_bucket_prepare_op;
4363 cls_method_handle_t h_rgw_bucket_complete_op;
4364 cls_method_handle_t h_rgw_bucket_link_olh;
4365 cls_method_handle_t h_rgw_bucket_unlink_instance_op;
4366 cls_method_handle_t h_rgw_bucket_read_olh_log;
4367 cls_method_handle_t h_rgw_bucket_trim_olh_log;
4368 cls_method_handle_t h_rgw_bucket_clear_olh;
4369 cls_method_handle_t h_rgw_obj_remove;
4370 cls_method_handle_t h_rgw_obj_store_pg_ver;
4371 cls_method_handle_t h_rgw_obj_check_attrs_prefix;
4372 cls_method_handle_t h_rgw_obj_check_mtime;
4373 cls_method_handle_t h_rgw_bi_get_op;
4374 cls_method_handle_t h_rgw_bi_put_op;
4375 cls_method_handle_t h_rgw_bi_list_op;
4376 cls_method_handle_t h_rgw_bi_log_list_op;
4377 cls_method_handle_t h_rgw_bi_log_resync_op;
4378 cls_method_handle_t h_rgw_bi_log_stop_op;
4379 cls_method_handle_t h_rgw_dir_suggest_changes;
4380 cls_method_handle_t h_rgw_user_usage_log_add;
4381 cls_method_handle_t h_rgw_user_usage_log_read;
4382 cls_method_handle_t h_rgw_user_usage_log_trim;
4383 cls_method_handle_t h_rgw_usage_log_clear;
4384 cls_method_handle_t h_rgw_gc_set_entry;
4385 cls_method_handle_t h_rgw_gc_list;
4386 cls_method_handle_t h_rgw_gc_remove;
4387 cls_method_handle_t h_rgw_lc_get_entry;
4388 cls_method_handle_t h_rgw_lc_set_entry;
4389 cls_method_handle_t h_rgw_lc_rm_entry;
4390 cls_method_handle_t h_rgw_lc_get_next_entry;
4391 cls_method_handle_t h_rgw_lc_put_head;
4392 cls_method_handle_t h_rgw_lc_get_head;
4393 cls_method_handle_t h_rgw_lc_list_entries;
4394 cls_method_handle_t h_rgw_reshard_add;
4395 cls_method_handle_t h_rgw_reshard_list;
4396 cls_method_handle_t h_rgw_reshard_get;
4397 cls_method_handle_t h_rgw_reshard_remove;
4398 cls_method_handle_t h_rgw_set_bucket_resharding;
4399 cls_method_handle_t h_rgw_clear_bucket_resharding;
4400 cls_method_handle_t h_rgw_guard_bucket_resharding;
4401 cls_method_handle_t h_rgw_get_bucket_resharding;
4402
4403 cls_register(RGW_CLASS, &h_class);
4404
4405 /* bucket index */
4406 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);
4407 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);
4408 cls_register_cxx_method(h_class, RGW_BUCKET_LIST, CLS_METHOD_RD, rgw_bucket_list, &h_rgw_bucket_list);
4409 cls_register_cxx_method(h_class, RGW_BUCKET_CHECK_INDEX, CLS_METHOD_RD, rgw_bucket_check_index, &h_rgw_bucket_check_index);
4410 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);
4411 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);
4412 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);
4413 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);
4414 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);
4415 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);
4416 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);
4417 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);
4418 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);
4419
4420 cls_register_cxx_method(h_class, RGW_OBJ_REMOVE, CLS_METHOD_RD | CLS_METHOD_WR, rgw_obj_remove, &h_rgw_obj_remove);
4421 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);
4422 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);
4423 cls_register_cxx_method(h_class, RGW_OBJ_CHECK_MTIME, CLS_METHOD_RD, rgw_obj_check_mtime, &h_rgw_obj_check_mtime);
4424
4425 cls_register_cxx_method(h_class, RGW_BI_GET, CLS_METHOD_RD, rgw_bi_get_op, &h_rgw_bi_get_op);
4426 cls_register_cxx_method(h_class, RGW_BI_PUT, CLS_METHOD_RD | CLS_METHOD_WR, rgw_bi_put_op, &h_rgw_bi_put_op);
4427 cls_register_cxx_method(h_class, RGW_BI_LIST, CLS_METHOD_RD, rgw_bi_list_op, &h_rgw_bi_list_op);
4428
4429 cls_register_cxx_method(h_class, RGW_BI_LOG_LIST, CLS_METHOD_RD, rgw_bi_log_list, &h_rgw_bi_log_list_op);
4430 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);
4431 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);
4432
4433 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);
4434 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);
4435
4436 /* usage logging */
4437 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);
4438 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);
4439 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);
4440 cls_register_cxx_method(h_class, RGW_USAGE_LOG_CLEAR, CLS_METHOD_WR, rgw_usage_log_clear, &h_rgw_usage_log_clear);
4441
4442 /* garbage collection */
4443 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);
4444 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);
4445 cls_register_cxx_method(h_class, RGW_GC_LIST, CLS_METHOD_RD, rgw_cls_gc_list, &h_rgw_gc_list);
4446 cls_register_cxx_method(h_class, RGW_GC_REMOVE, CLS_METHOD_RD | CLS_METHOD_WR, rgw_cls_gc_remove, &h_rgw_gc_remove);
4447
4448 /* lifecycle bucket list */
4449 cls_register_cxx_method(h_class, RGW_LC_GET_ENTRY, CLS_METHOD_RD, rgw_cls_lc_get_entry, &h_rgw_lc_get_entry);
4450 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);
4451 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);
4452 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);
4453 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);
4454 cls_register_cxx_method(h_class, RGW_LC_GET_HEAD, CLS_METHOD_RD, rgw_cls_lc_get_head, &h_rgw_lc_get_head);
4455 cls_register_cxx_method(h_class, RGW_LC_LIST_ENTRIES, CLS_METHOD_RD, rgw_cls_lc_list_entries, &h_rgw_lc_list_entries);
4456
4457 /* resharding */
4458 cls_register_cxx_method(h_class, RGW_RESHARD_ADD, CLS_METHOD_RD | CLS_METHOD_WR, rgw_reshard_add, &h_rgw_reshard_add);
4459 cls_register_cxx_method(h_class, RGW_RESHARD_LIST, CLS_METHOD_RD, rgw_reshard_list, &h_rgw_reshard_list);
4460 cls_register_cxx_method(h_class, RGW_RESHARD_GET, CLS_METHOD_RD,rgw_reshard_get, &h_rgw_reshard_get);
4461 cls_register_cxx_method(h_class, RGW_RESHARD_REMOVE, CLS_METHOD_RD | CLS_METHOD_WR, rgw_reshard_remove, &h_rgw_reshard_remove);
4462
4463 /* resharding attribute */
4464 cls_register_cxx_method(h_class, RGW_SET_BUCKET_RESHARDING, CLS_METHOD_RD | CLS_METHOD_WR,
4465 rgw_set_bucket_resharding, &h_rgw_set_bucket_resharding);
4466 cls_register_cxx_method(h_class, RGW_CLEAR_BUCKET_RESHARDING, CLS_METHOD_RD | CLS_METHOD_WR,
4467 rgw_clear_bucket_resharding, &h_rgw_clear_bucket_resharding);
4468 cls_register_cxx_method(h_class, RGW_GUARD_BUCKET_RESHARDING, CLS_METHOD_RD ,
4469 rgw_guard_bucket_resharding, &h_rgw_guard_bucket_resharding);
4470 cls_register_cxx_method(h_class, RGW_GET_BUCKET_RESHARDING, CLS_METHOD_RD ,
4471 rgw_get_bucket_resharding, &h_rgw_get_bucket_resharding);
4472
4473 return;
4474 }