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