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