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