]> git.proxmox.com Git - ceph.git/blob - ceph/src/cls/rgw/cls_rgw.cc
cba9a2d73492ad029614bd3b5a83d91cd134c091
[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, but we need to preserve its name in case this entry
1799 // gets resharded, because this key is used for hash placement
1800 next_key.name = dest_key.name;
1801 olh.update(next_key, false);
1802 olh.update_log(CLS_RGW_OLH_OP_UNLINK_OLH, op.op_tag, next_key, false);
1803 olh.set_exists(false);
1804 olh.set_pending_removal(true);
1805 }
1806 }
1807
1808 if (!obj.is_delete_marker()) {
1809 olh.update_log(CLS_RGW_OLH_OP_REMOVE_INSTANCE, op.op_tag, op.key, false);
1810 } else {
1811 /* this is a delete marker, it's our responsibility to remove its
1812 * instance entry */
1813 ret = obj.unlink();
1814 if (ret < 0) {
1815 return ret;
1816 }
1817 }
1818
1819 ret = obj.unlink_list_entry();
1820 if (ret < 0) {
1821 return ret;
1822 }
1823
1824 ret = olh.write();
1825 if (ret < 0) {
1826 return ret;
1827 }
1828
1829 rgw_bucket_dir_header header;
1830 ret = read_bucket_header(hctx, &header);
1831 if (ret < 0) {
1832 CLS_LOG(1, "ERROR: rgw_bucket_unlink_instance(): failed to read header\n");
1833 return ret;
1834 }
1835
1836 if (op.log_op && !header.syncstopped) {
1837 rgw_bucket_entry_ver ver;
1838 ver.epoch = (op.olh_epoch ? op.olh_epoch : olh.get_epoch());
1839
1840 real_time mtime = obj.mtime(); /* mtime has no real meaning in
1841 * instance removal context */
1842 ret = log_index_operation(hctx, op.key, CLS_RGW_OP_UNLINK_INSTANCE, op.op_tag,
1843 mtime, ver,
1844 CLS_RGW_STATE_COMPLETE, header.ver, header.max_marker,
1845 op.bilog_flags | RGW_BILOG_FLAG_VERSIONED_OP, NULL, NULL, &op.zones_trace);
1846 if (ret < 0)
1847 return ret;
1848
1849 return write_bucket_header(hctx, &header); /* updates header version */
1850 }
1851
1852 return 0;
1853 }
1854
1855 static int rgw_bucket_read_olh_log(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
1856 {
1857 // decode request
1858 rgw_cls_read_olh_log_op op;
1859 auto iter = in->cbegin();
1860 try {
1861 decode(op, iter);
1862 } catch (buffer::error& err) {
1863 CLS_LOG(0, "ERROR: rgw_bucket_read_olh_log(): failed to decode request\n");
1864 return -EINVAL;
1865 }
1866
1867 if (!op.olh.instance.empty()) {
1868 CLS_LOG(1, "bad key passed in (non empty instance)");
1869 return -EINVAL;
1870 }
1871
1872 rgw_bucket_olh_entry olh_data_entry;
1873 string olh_data_key;
1874 encode_olh_data_key(op.olh, &olh_data_key);
1875 int ret = read_index_entry(hctx, olh_data_key, &olh_data_entry);
1876 if (ret < 0 && ret != -ENOENT) {
1877 CLS_LOG(0, "ERROR: read_index_entry() olh_key=%s ret=%d", olh_data_key.c_str(), ret);
1878 return ret;
1879 }
1880
1881 if (olh_data_entry.tag != op.olh_tag) {
1882 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());
1883 return -ECANCELED;
1884 }
1885
1886 rgw_cls_read_olh_log_ret op_ret;
1887
1888 #define MAX_OLH_LOG_ENTRIES 1000
1889 map<uint64_t, vector<rgw_bucket_olh_log_entry> >& log = olh_data_entry.pending_log;
1890
1891 if (log.begin()->first > op.ver_marker && log.size() <= MAX_OLH_LOG_ENTRIES) {
1892 op_ret.log = log;
1893 op_ret.is_truncated = false;
1894 } else {
1895 map<uint64_t, vector<rgw_bucket_olh_log_entry> >::iterator iter = log.upper_bound(op.ver_marker);
1896
1897 for (int i = 0; i < MAX_OLH_LOG_ENTRIES && iter != log.end(); ++i, ++iter) {
1898 op_ret.log[iter->first] = iter->second;
1899 }
1900 op_ret.is_truncated = (iter != log.end());
1901 }
1902
1903 encode(op_ret, *out);
1904
1905 return 0;
1906 }
1907
1908 static int rgw_bucket_trim_olh_log(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
1909 {
1910 // decode request
1911 rgw_cls_trim_olh_log_op op;
1912 auto iter = in->cbegin();
1913 try {
1914 decode(op, iter);
1915 } catch (buffer::error& err) {
1916 CLS_LOG(0, "ERROR: rgw_bucket_trim_olh_log(): failed to decode request\n");
1917 return -EINVAL;
1918 }
1919
1920 if (!op.olh.instance.empty()) {
1921 CLS_LOG(1, "bad key passed in (non empty instance)");
1922 return -EINVAL;
1923 }
1924
1925 /* read olh entry */
1926 rgw_bucket_olh_entry olh_data_entry;
1927 string olh_data_key;
1928 encode_olh_data_key(op.olh, &olh_data_key);
1929 int ret = read_index_entry(hctx, olh_data_key, &olh_data_entry);
1930 if (ret < 0 && ret != -ENOENT) {
1931 CLS_LOG(0, "ERROR: read_index_entry() olh_key=%s ret=%d", olh_data_key.c_str(), ret);
1932 return ret;
1933 }
1934
1935 if (olh_data_entry.tag != op.olh_tag) {
1936 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());
1937 return -ECANCELED;
1938 }
1939
1940 /* remove all versions up to and including ver from the pending map */
1941 map<uint64_t, vector<rgw_bucket_olh_log_entry> >& log = olh_data_entry.pending_log;
1942 map<uint64_t, vector<rgw_bucket_olh_log_entry> >::iterator liter = log.begin();
1943 while (liter != log.end() && liter->first <= op.ver) {
1944 map<uint64_t, vector<rgw_bucket_olh_log_entry> >::iterator rm_iter = liter;
1945 ++liter;
1946 log.erase(rm_iter);
1947 }
1948
1949 /* write the olh data entry */
1950 ret = write_entry(hctx, olh_data_entry, olh_data_key);
1951 if (ret < 0) {
1952 CLS_LOG(0, "ERROR: write_entry() olh_key=%s ret=%d", olh_data_key.c_str(), ret);
1953 return ret;
1954 }
1955
1956 return 0;
1957 }
1958
1959 static int rgw_bucket_clear_olh(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
1960 {
1961 // decode request
1962 rgw_cls_bucket_clear_olh_op op;
1963 auto iter = in->cbegin();
1964 try {
1965 decode(op, iter);
1966 } catch (buffer::error& err) {
1967 CLS_LOG(0, "ERROR: rgw_bucket_clear_olh(): failed to decode request\n");
1968 return -EINVAL;
1969 }
1970
1971 if (!op.key.instance.empty()) {
1972 CLS_LOG(1, "bad key passed in (non empty instance)");
1973 return -EINVAL;
1974 }
1975
1976 /* read olh entry */
1977 rgw_bucket_olh_entry olh_data_entry;
1978 string olh_data_key;
1979 encode_olh_data_key(op.key, &olh_data_key);
1980 int ret = read_index_entry(hctx, olh_data_key, &olh_data_entry);
1981 if (ret < 0 && ret != -ENOENT) {
1982 CLS_LOG(0, "ERROR: read_index_entry() olh_key=%s ret=%d", olh_data_key.c_str(), ret);
1983 return ret;
1984 }
1985
1986 if (olh_data_entry.tag != op.olh_tag) {
1987 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());
1988 return -ECANCELED;
1989 }
1990
1991 ret = cls_cxx_map_remove_key(hctx, olh_data_key);
1992 if (ret < 0) {
1993 CLS_LOG(1, "NOTICE: %s(): can't remove key %s ret=%d", __func__, olh_data_key.c_str(), ret);
1994 return ret;
1995 }
1996
1997 rgw_bucket_dir_entry plain_entry;
1998
1999 /* read plain entry, make sure it's a versioned place holder */
2000 ret = read_index_entry(hctx, op.key.name, &plain_entry);
2001 if (ret == -ENOENT) {
2002 /* we're done, no entry existing */
2003 return 0;
2004 }
2005 if (ret < 0) {
2006 CLS_LOG(0, "ERROR: read_index_entry key=%s ret=%d", op.key.name.c_str(), ret);
2007 return ret;
2008 }
2009
2010 if ((plain_entry.flags & rgw_bucket_dir_entry::FLAG_VER_MARKER) == 0) {
2011 /* it's not a version marker, don't remove it */
2012 return 0;
2013 }
2014
2015 ret = cls_cxx_map_remove_key(hctx, op.key.name);
2016 if (ret < 0) {
2017 CLS_LOG(1, "NOTICE: %s(): can't remove key %s ret=%d", __func__, op.key.name.c_str(), ret);
2018 return ret;
2019 }
2020
2021 return 0;
2022 }
2023
2024 int rgw_dir_suggest_changes(cls_method_context_t hctx,
2025 bufferlist *in, bufferlist *out)
2026 {
2027 CLS_LOG(1, "rgw_dir_suggest_changes()");
2028
2029 bufferlist header_bl;
2030 rgw_bucket_dir_header header;
2031 bool header_changed = false;
2032
2033 int rc = read_bucket_header(hctx, &header);
2034 if (rc < 0) {
2035 CLS_LOG(1, "ERROR: rgw_dir_suggest_changes(): failed to read header\n");
2036 return rc;
2037 }
2038
2039 timespan tag_timeout(
2040 std::chrono::seconds(
2041 header.tag_timeout ? header.tag_timeout : CEPH_RGW_TAG_TIMEOUT));
2042
2043 auto in_iter = in->cbegin();
2044
2045 while (!in_iter.end()) {
2046 __u8 op;
2047 rgw_bucket_dir_entry cur_change;
2048 rgw_bucket_dir_entry cur_disk;
2049 try {
2050 decode(op, in_iter);
2051 decode(cur_change, in_iter);
2052 } catch (buffer::error& err) {
2053 CLS_LOG(1, "ERROR: rgw_dir_suggest_changes(): failed to decode request\n");
2054 return -EINVAL;
2055 }
2056
2057 bufferlist cur_disk_bl;
2058 string cur_change_key;
2059 encode_obj_index_key(cur_change.key, &cur_change_key);
2060 int ret = cls_cxx_map_get_val(hctx, cur_change_key, &cur_disk_bl);
2061 if (ret < 0 && ret != -ENOENT)
2062 return -EINVAL;
2063
2064 if (ret == -ENOENT) {
2065 continue;
2066 }
2067
2068 if (cur_disk_bl.length()) {
2069 auto cur_disk_iter = cur_disk_bl.cbegin();
2070 try {
2071 decode(cur_disk, cur_disk_iter);
2072 } catch (buffer::error& error) {
2073 CLS_LOG(1, "ERROR: rgw_dir_suggest_changes(): failed to decode cur_disk\n");
2074 return -EINVAL;
2075 }
2076
2077 real_time cur_time = real_clock::now();
2078 map<string, rgw_bucket_pending_info>::iterator iter =
2079 cur_disk.pending_map.begin();
2080 while(iter != cur_disk.pending_map.end()) {
2081 map<string, rgw_bucket_pending_info>::iterator cur_iter=iter++;
2082 if (cur_time > (cur_iter->second.timestamp + timespan(tag_timeout))) {
2083 cur_disk.pending_map.erase(cur_iter);
2084 }
2085 }
2086 }
2087
2088 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",
2089 cur_disk.pending_map.empty(), (int)op, cur_disk.exists,
2090 (int)cur_change.pending_map.size(), cur_change.exists);
2091
2092 if (cur_disk.pending_map.empty()) {
2093 if (cur_disk.exists) {
2094 rgw_bucket_category_stats& old_stats = header.stats[cur_disk.meta.category];
2095 CLS_LOG(10, "total_entries: %" PRId64 " -> %" PRId64 "\n", old_stats.num_entries, old_stats.num_entries - 1);
2096 old_stats.num_entries--;
2097 old_stats.total_size -= cur_disk.meta.accounted_size;
2098 old_stats.total_size_rounded -= cls_rgw_get_rounded_size(cur_disk.meta.accounted_size);
2099 old_stats.actual_size -= cur_disk.meta.size;
2100 header_changed = true;
2101 }
2102 rgw_bucket_category_stats& stats = header.stats[cur_change.meta.category];
2103 bool log_op = (op & CEPH_RGW_DIR_SUGGEST_LOG_OP) != 0;
2104 op &= CEPH_RGW_DIR_SUGGEST_OP_MASK;
2105 switch(op) {
2106 case CEPH_RGW_REMOVE:
2107 CLS_LOG(10, "CEPH_RGW_REMOVE name=%s instance=%s\n", cur_change.key.name.c_str(), cur_change.key.instance.c_str());
2108 ret = cls_cxx_map_remove_key(hctx, cur_change_key);
2109 if (ret < 0)
2110 return ret;
2111 if (log_op && cur_disk.exists && !header.syncstopped) {
2112 ret = log_index_operation(hctx, cur_disk.key, CLS_RGW_OP_DEL, cur_disk.tag, cur_disk.meta.mtime,
2113 cur_disk.ver, CLS_RGW_STATE_COMPLETE, header.ver, header.max_marker, 0, NULL, NULL, NULL);
2114 if (ret < 0) {
2115 CLS_LOG(0, "ERROR: %s(): failed to log operation ret=%d", __func__, ret);
2116 return ret;
2117 }
2118 }
2119 break;
2120 case CEPH_RGW_UPDATE:
2121 CLS_LOG(10, "CEPH_RGW_UPDATE name=%s instance=%s total_entries: %" PRId64 " -> %" PRId64 "\n",
2122 cur_change.key.name.c_str(), cur_change.key.instance.c_str(), stats.num_entries, stats.num_entries + 1);
2123
2124 stats.num_entries++;
2125 stats.total_size += cur_change.meta.accounted_size;
2126 stats.total_size_rounded += cls_rgw_get_rounded_size(cur_change.meta.accounted_size);
2127 stats.actual_size += cur_change.meta.size;
2128 header_changed = true;
2129 cur_change.index_ver = header.ver;
2130 bufferlist cur_state_bl;
2131 encode(cur_change, cur_state_bl);
2132 ret = cls_cxx_map_set_val(hctx, cur_change_key, &cur_state_bl);
2133 if (ret < 0)
2134 return ret;
2135 if (log_op && !header.syncstopped) {
2136 ret = log_index_operation(hctx, cur_change.key, CLS_RGW_OP_ADD, cur_change.tag, cur_change.meta.mtime,
2137 cur_change.ver, CLS_RGW_STATE_COMPLETE, header.ver, header.max_marker, 0, NULL, NULL, NULL);
2138 if (ret < 0) {
2139 CLS_LOG(0, "ERROR: %s(): failed to log operation ret=%d", __func__, ret);
2140 return ret;
2141 }
2142 }
2143 break;
2144 } // switch(op)
2145 } // if (cur_disk.pending_map.empty())
2146 } // while (!in_iter.end())
2147
2148 if (header_changed) {
2149 return write_bucket_header(hctx, &header);
2150 }
2151 return 0;
2152 }
2153
2154 static int rgw_obj_remove(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
2155 {
2156 // decode request
2157 rgw_cls_obj_remove_op op;
2158 auto iter = in->cbegin();
2159 try {
2160 decode(op, iter);
2161 } catch (buffer::error& err) {
2162 CLS_LOG(0, "ERROR: %s(): failed to decode request", __func__);
2163 return -EINVAL;
2164 }
2165
2166 if (op.keep_attr_prefixes.empty()) {
2167 return cls_cxx_remove(hctx);
2168 }
2169
2170 map<string, bufferlist> attrset;
2171 int ret = cls_cxx_getxattrs(hctx, &attrset);
2172 if (ret < 0 && ret != -ENOENT) {
2173 CLS_LOG(0, "ERROR: %s(): cls_cxx_getxattrs() returned %d", __func__, ret);
2174 return ret;
2175 }
2176
2177 map<string, bufferlist> new_attrs;
2178 for (list<string>::iterator iter = op.keep_attr_prefixes.begin();
2179 iter != op.keep_attr_prefixes.end(); ++iter) {
2180 string& check_prefix = *iter;
2181
2182 for (map<string, bufferlist>::iterator aiter = attrset.lower_bound(check_prefix);
2183 aiter != attrset.end(); ++aiter) {
2184 const string& attr = aiter->first;
2185
2186 if (attr.substr(0, check_prefix.size()) > check_prefix) {
2187 break;
2188 }
2189
2190 new_attrs[attr] = aiter->second;
2191 }
2192 }
2193
2194 CLS_LOG(20, "%s(): removing object", __func__);
2195 ret = cls_cxx_remove(hctx);
2196 if (ret < 0) {
2197 CLS_LOG(0, "ERROR: %s(): cls_cxx_remove returned %d", __func__, ret);
2198 return ret;
2199 }
2200
2201 if (new_attrs.empty()) {
2202 /* no data to keep */
2203 return 0;
2204 }
2205
2206 ret = cls_cxx_create(hctx, false);
2207 if (ret < 0) {
2208 CLS_LOG(0, "ERROR: %s(): cls_cxx_create returned %d", __func__, ret);
2209 return ret;
2210 }
2211
2212 for (map<string, bufferlist>::iterator aiter = new_attrs.begin();
2213 aiter != new_attrs.end(); ++aiter) {
2214 const string& attr = aiter->first;
2215
2216 ret = cls_cxx_setxattr(hctx, attr.c_str(), &aiter->second);
2217 CLS_LOG(20, "%s(): setting attr: %s", __func__, attr.c_str());
2218 if (ret < 0) {
2219 CLS_LOG(0, "ERROR: %s(): cls_cxx_setxattr (attr=%s) returned %d", __func__, attr.c_str(), ret);
2220 return ret;
2221 }
2222 }
2223
2224 return 0;
2225 }
2226
2227 static int rgw_obj_store_pg_ver(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
2228 {
2229 // decode request
2230 rgw_cls_obj_store_pg_ver_op op;
2231 auto iter = in->cbegin();
2232 try {
2233 decode(op, iter);
2234 } catch (buffer::error& err) {
2235 CLS_LOG(0, "ERROR: %s(): failed to decode request", __func__);
2236 return -EINVAL;
2237 }
2238
2239 bufferlist bl;
2240 uint64_t ver = cls_current_version(hctx);
2241 encode(ver, bl);
2242 int ret = cls_cxx_setxattr(hctx, op.attr.c_str(), &bl);
2243 if (ret < 0) {
2244 CLS_LOG(0, "ERROR: %s(): cls_cxx_setxattr (attr=%s) returned %d", __func__, op.attr.c_str(), ret);
2245 return ret;
2246 }
2247
2248 return 0;
2249 }
2250
2251 static int rgw_obj_check_attrs_prefix(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
2252 {
2253 // decode request
2254 rgw_cls_obj_check_attrs_prefix op;
2255 auto iter = in->cbegin();
2256 try {
2257 decode(op, iter);
2258 } catch (buffer::error& err) {
2259 CLS_LOG(0, "ERROR: %s(): failed to decode request", __func__);
2260 return -EINVAL;
2261 }
2262
2263 if (op.check_prefix.empty()) {
2264 return -EINVAL;
2265 }
2266
2267 map<string, bufferlist> attrset;
2268 int ret = cls_cxx_getxattrs(hctx, &attrset);
2269 if (ret < 0 && ret != -ENOENT) {
2270 CLS_LOG(0, "ERROR: %s(): cls_cxx_getxattrs() returned %d", __func__, ret);
2271 return ret;
2272 }
2273
2274 bool exist = false;
2275
2276 for (map<string, bufferlist>::iterator aiter = attrset.lower_bound(op.check_prefix);
2277 aiter != attrset.end(); ++aiter) {
2278 const string& attr = aiter->first;
2279
2280 if (attr.substr(0, op.check_prefix.size()) > op.check_prefix) {
2281 break;
2282 }
2283
2284 exist = true;
2285 }
2286
2287 if (exist == op.fail_if_exist) {
2288 return -ECANCELED;
2289 }
2290
2291 return 0;
2292 }
2293
2294 static int rgw_obj_check_mtime(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
2295 {
2296 // decode request
2297 rgw_cls_obj_check_mtime op;
2298 auto iter = in->cbegin();
2299 try {
2300 decode(op, iter);
2301 } catch (buffer::error& err) {
2302 CLS_LOG(0, "ERROR: %s(): failed to decode request", __func__);
2303 return -EINVAL;
2304 }
2305
2306 real_time obj_ut;
2307 int ret = cls_cxx_stat2(hctx, NULL, &obj_ut);
2308 if (ret < 0 && ret != -ENOENT) {
2309 CLS_LOG(0, "ERROR: %s(): cls_cxx_stat() returned %d", __func__, ret);
2310 return ret;
2311 }
2312 if (ret == -ENOENT) {
2313 CLS_LOG(10, "object does not exist, skipping check");
2314 }
2315
2316 ceph_timespec obj_ts = ceph::real_clock::to_ceph_timespec(obj_ut);
2317 ceph_timespec op_ts = ceph::real_clock::to_ceph_timespec(op.mtime);
2318
2319 if (!op.high_precision_time) {
2320 obj_ts.tv_nsec = 0;
2321 op_ts.tv_nsec = 0;
2322 }
2323
2324 CLS_LOG(10, "%s: obj_ut=%lld.%06lld op.mtime=%lld.%06lld", __func__,
2325 (long long)obj_ts.tv_sec, (long long)obj_ts.tv_nsec,
2326 (long long)op_ts.tv_sec, (long long)op_ts.tv_nsec);
2327
2328 bool check;
2329
2330 switch (op.type) {
2331 case CLS_RGW_CHECK_TIME_MTIME_EQ:
2332 check = (obj_ts == op_ts);
2333 break;
2334 case CLS_RGW_CHECK_TIME_MTIME_LT:
2335 check = (obj_ts < op_ts);
2336 break;
2337 case CLS_RGW_CHECK_TIME_MTIME_LE:
2338 check = (obj_ts <= op_ts);
2339 break;
2340 case CLS_RGW_CHECK_TIME_MTIME_GT:
2341 check = (obj_ts > op_ts);
2342 break;
2343 case CLS_RGW_CHECK_TIME_MTIME_GE:
2344 check = (obj_ts >= op_ts);
2345 break;
2346 default:
2347 return -EINVAL;
2348 };
2349
2350 if (!check) {
2351 return -ECANCELED;
2352 }
2353
2354 return 0;
2355 }
2356
2357 static int rgw_bi_get_op(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
2358 {
2359 // decode request
2360 rgw_cls_bi_get_op op;
2361 auto iter = in->cbegin();
2362 try {
2363 decode(op, iter);
2364 } catch (buffer::error& err) {
2365 CLS_LOG(0, "ERROR: %s(): failed to decode request", __func__);
2366 return -EINVAL;
2367 }
2368
2369 string idx;
2370
2371 switch (op.type) {
2372 case BIIndexType::Plain:
2373 idx = op.key.name;
2374 break;
2375 case BIIndexType::Instance:
2376 encode_obj_index_key(op.key, &idx);
2377 break;
2378 case BIIndexType::OLH:
2379 encode_olh_data_key(op.key, &idx);
2380 break;
2381 default:
2382 CLS_LOG(10, "%s(): invalid key type encoding: %d",
2383 __func__, int(op.type));
2384 return -EINVAL;
2385 }
2386
2387 rgw_cls_bi_get_ret op_ret;
2388
2389 rgw_cls_bi_entry& entry = op_ret.entry;
2390
2391 entry.type = op.type;
2392 entry.idx = idx;
2393
2394 int r = cls_cxx_map_get_val(hctx, idx, &entry.data);
2395 if (r < 0) {
2396 CLS_LOG(10, "%s(): cls_cxx_map_get_val() returned %d", __func__, r);
2397 return r;
2398 }
2399
2400 encode(op_ret, *out);
2401
2402 return 0;
2403 }
2404
2405 static int rgw_bi_put_op(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
2406 {
2407 // decode request
2408 rgw_cls_bi_put_op op;
2409 auto iter = in->cbegin();
2410 try {
2411 decode(op, iter);
2412 } catch (buffer::error& err) {
2413 CLS_LOG(0, "ERROR: %s(): failed to decode request", __func__);
2414 return -EINVAL;
2415 }
2416
2417 rgw_cls_bi_entry& entry = op.entry;
2418
2419 int r = cls_cxx_map_set_val(hctx, entry.idx, &entry.data);
2420 if (r < 0) {
2421 CLS_LOG(0, "ERROR: %s(): cls_cxx_map_set_val() returned r=%d", __func__, r);
2422 }
2423
2424 return 0;
2425 }
2426
2427 static int list_plain_entries(cls_method_context_t hctx,
2428 const string& name,
2429 const string& marker,
2430 uint32_t max,
2431 list<rgw_cls_bi_entry> *entries,
2432 bool *pmore)
2433 {
2434 string filter = name;
2435 string start_after_key = marker;
2436
2437 string end_key; // stop listing at bi_log_prefix
2438 bi_log_prefix(end_key);
2439
2440 int count = 0;
2441 map<string, bufferlist> keys;
2442 int ret = cls_cxx_map_get_vals(hctx, start_after_key, filter, max,
2443 &keys, pmore);
2444 if (ret < 0) {
2445 return ret;
2446 }
2447
2448 map<string, bufferlist>::iterator iter;
2449 for (iter = keys.begin(); iter != keys.end(); ++iter) {
2450 if (iter->first >= end_key) {
2451 /* past the end of plain namespace */
2452 if (pmore) {
2453 *pmore = false;
2454 }
2455 return count;
2456 }
2457
2458 rgw_cls_bi_entry entry;
2459 entry.type = BIIndexType::Plain;
2460 entry.idx = iter->first;
2461 entry.data = iter->second;
2462
2463 auto biter = entry.data.cbegin();
2464
2465 rgw_bucket_dir_entry e;
2466 try {
2467 decode(e, biter);
2468 } catch (buffer::error& err) {
2469 CLS_LOG(0, "ERROR: %s(): failed to decode buffer", __func__);
2470 return -EIO;
2471 }
2472
2473 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());
2474
2475 if (!name.empty() && e.key.name != name) {
2476 /* we are skipping the rest of the entries */
2477 if (pmore) {
2478 *pmore = false;
2479 }
2480 return count;
2481 }
2482
2483 entries->push_back(entry);
2484 count++;
2485 if (count >= (int)max) {
2486 return count;
2487 }
2488 start_after_key = entry.idx;
2489 }
2490
2491 return count;
2492 }
2493
2494 static int list_instance_entries(cls_method_context_t hctx,
2495 const string& name,
2496 const string& marker,
2497 uint32_t max,
2498 list<rgw_cls_bi_entry> *entries,
2499 bool *pmore)
2500 {
2501 cls_rgw_obj_key key(name);
2502 string first_instance_idx;
2503 encode_obj_versioned_data_key(key, &first_instance_idx);
2504 string start_after_key;
2505
2506 if (!name.empty()) {
2507 start_after_key = first_instance_idx;
2508 } else {
2509 start_after_key = BI_PREFIX_CHAR;
2510 start_after_key.append(bucket_index_prefixes[BI_BUCKET_OBJ_INSTANCE_INDEX]);
2511 }
2512 string filter = start_after_key;
2513 if (bi_entry_gt(marker, start_after_key)) {
2514 start_after_key = marker;
2515 }
2516 int count = 0;
2517 map<string, bufferlist> keys;
2518 bufferlist k;
2519 int ret = cls_cxx_map_get_val(hctx, start_after_key, &k);
2520 if (ret < 0 && ret != -ENOENT) {
2521 return ret;
2522 }
2523 bool found_first = (ret == 0);
2524 if (found_first) {
2525 --max;
2526 }
2527 if (max > 0) {
2528 ret = cls_cxx_map_get_vals(hctx, start_after_key, string(), max,
2529 &keys, pmore);
2530 CLS_LOG(20, "%s(): start_after_key=%s first_instance_idx=%s keys.size()=%d",
2531 __func__, escape_str(start_after_key).c_str(),
2532 escape_str(first_instance_idx).c_str(), (int)keys.size());
2533 if (ret < 0) {
2534 return ret;
2535 }
2536 }
2537 if (found_first) {
2538 keys[start_after_key].claim(k);
2539 }
2540
2541 map<string, bufferlist>::iterator iter;
2542 for (iter = keys.begin(); iter != keys.end(); ++iter) {
2543 rgw_cls_bi_entry entry;
2544 entry.type = BIIndexType::Instance;
2545 entry.idx = iter->first;
2546 entry.data = iter->second;
2547
2548 if (!filter.empty() && entry.idx.compare(0, filter.size(), filter) != 0) {
2549 /* we are skipping the rest of the entries */
2550 if (pmore) {
2551 *pmore = false;
2552 }
2553 return count;
2554 }
2555
2556 CLS_LOG(20, "%s(): entry.idx=%s", __func__, escape_str(entry.idx).c_str());
2557
2558 auto biter = entry.data.cbegin();
2559
2560 rgw_bucket_dir_entry e;
2561 try {
2562 decode(e, biter);
2563 } catch (buffer::error& err) {
2564 CLS_LOG(0, "ERROR: %s(): failed to decode buffer (size=%d)", __func__, entry.data.length());
2565 return -EIO;
2566 }
2567
2568 if (!name.empty() && e.key.name != name) {
2569 /* we are skipping the rest of the entries */
2570 if (pmore) {
2571 *pmore = false;
2572 }
2573 return count;
2574 }
2575
2576 entries->push_back(entry);
2577 count++;
2578 start_after_key = entry.idx;
2579 }
2580
2581 return count;
2582 }
2583
2584 static int list_olh_entries(cls_method_context_t hctx,
2585 const string& name,
2586 const string& marker,
2587 uint32_t max,
2588 list<rgw_cls_bi_entry> *entries,
2589 bool *pmore)
2590 {
2591 cls_rgw_obj_key key(name);
2592 string first_instance_idx;
2593 encode_olh_data_key(key, &first_instance_idx);
2594 string start_after_key;
2595
2596 if (!name.empty()) {
2597 start_after_key = first_instance_idx;
2598 } else {
2599 start_after_key = BI_PREFIX_CHAR;
2600 start_after_key.append(bucket_index_prefixes[BI_BUCKET_OLH_DATA_INDEX]);
2601 }
2602 string filter = start_after_key;
2603 if (bi_entry_gt(marker, start_after_key)) {
2604 start_after_key = marker;
2605 }
2606 int count = 0;
2607 map<string, bufferlist> keys;
2608 int ret;
2609 bufferlist k;
2610 ret = cls_cxx_map_get_val(hctx, start_after_key, &k);
2611 if (ret < 0 && ret != -ENOENT) {
2612 return ret;
2613 }
2614 bool found_first = (ret == 0);
2615 if (found_first) {
2616 --max;
2617 }
2618 if (max > 0) {
2619 ret = cls_cxx_map_get_vals(hctx, start_after_key, string(), max,
2620 &keys, pmore);
2621 CLS_LOG(20, "%s(): start_after_key=%s first_instance_idx=%s keys.size()=%d",
2622 __func__, escape_str(start_after_key).c_str(),
2623 escape_str(first_instance_idx).c_str(), (int)keys.size());
2624 if (ret < 0) {
2625 return ret;
2626 }
2627 }
2628
2629 if (found_first) {
2630 keys[start_after_key].claim(k);
2631 }
2632
2633 map<string, bufferlist>::iterator iter;
2634 for (iter = keys.begin(); iter != keys.end(); ++iter) {
2635 rgw_cls_bi_entry entry;
2636 entry.type = BIIndexType::OLH;
2637 entry.idx = iter->first;
2638 entry.data = iter->second;
2639
2640 if (!filter.empty() && entry.idx.compare(0, filter.size(), filter) != 0) {
2641 /* we are skipping the rest of the entries */
2642 if (pmore) {
2643 *pmore = false;
2644 }
2645 return count;
2646 }
2647
2648 CLS_LOG(20, "%s(): entry.idx=%s", __func__, escape_str(entry.idx).c_str());
2649
2650 auto biter = entry.data.cbegin();
2651
2652 rgw_bucket_olh_entry e;
2653 try {
2654 decode(e, biter);
2655 } catch (buffer::error& err) {
2656 CLS_LOG(0, "ERROR: %s(): failed to decode buffer (size=%d)", __func__, entry.data.length());
2657 return -EIO;
2658 }
2659
2660 if (!name.empty() && e.key.name != name) {
2661 /* we are skipping the rest of the entries */
2662 if (pmore) {
2663 *pmore = false;
2664 }
2665 return count;
2666 }
2667
2668 entries->push_back(entry);
2669 count++;
2670 start_after_key = entry.idx;
2671 }
2672
2673 return count;
2674 }
2675
2676 static int rgw_bi_list_op(cls_method_context_t hctx,
2677 bufferlist *in,
2678 bufferlist *out)
2679 {
2680 // decode request
2681 rgw_cls_bi_list_op op;
2682 auto iter = in->cbegin();
2683 try {
2684 decode(op, iter);
2685 } catch (buffer::error& err) {
2686 CLS_LOG(0, "ERROR: %s(): failed to decode request", __func__);
2687 return -EINVAL;
2688 }
2689
2690 rgw_cls_bi_list_ret op_ret;
2691
2692 string filter = op.name;
2693 #define MAX_BI_LIST_ENTRIES 1000
2694 int32_t max = (op.max < MAX_BI_LIST_ENTRIES ? op.max : MAX_BI_LIST_ENTRIES);
2695 bool more;
2696 int ret = list_plain_entries(hctx, op.name, op.marker, max,
2697 &op_ret.entries, &more);
2698 if (ret < 0) {
2699 CLS_LOG(0, "ERROR: %s(): list_plain_entries returned ret=%d", __func__, ret);
2700 return ret;
2701 }
2702 int count = ret;
2703
2704 CLS_LOG(20, "found %d plain entries", count);
2705
2706 if (!more) {
2707 ret = list_instance_entries(hctx, op.name, op.marker, max - count, &op_ret.entries, &more);
2708 if (ret < 0) {
2709 CLS_LOG(0, "ERROR: %s(): list_instance_entries returned ret=%d", __func__, ret);
2710 return ret;
2711 }
2712
2713 count += ret;
2714 }
2715
2716 if (!more) {
2717 ret = list_olh_entries(hctx, op.name, op.marker, max - count, &op_ret.entries, &more);
2718 if (ret < 0) {
2719 CLS_LOG(0, "ERROR: %s(): list_olh_entries returned ret=%d", __func__, ret);
2720 return ret;
2721 }
2722
2723 count += ret;
2724 }
2725
2726 op_ret.is_truncated = (count >= max) || more;
2727 while (count > max) {
2728 op_ret.entries.pop_back();
2729 count--;
2730 }
2731
2732 encode(op_ret, *out);
2733
2734 return 0;
2735 }
2736
2737 int bi_log_record_decode(bufferlist& bl, rgw_bi_log_entry& e)
2738 {
2739 auto iter = bl.cbegin();
2740 try {
2741 decode(e, iter);
2742 } catch (buffer::error& err) {
2743 CLS_LOG(0, "ERROR: failed to decode rgw_bi_log_entry");
2744 return -EIO;
2745 }
2746 return 0;
2747 }
2748
2749 static int bi_log_iterate_entries(cls_method_context_t hctx,
2750 const string& marker,
2751 const string& end_marker,
2752 string& key_iter,
2753 uint32_t max_entries,
2754 bool *truncated,
2755 int (*cb)(cls_method_context_t, const string&, rgw_bi_log_entry&, void *),
2756 void *param)
2757 {
2758 CLS_LOG(10, "bi_log_iterate_range");
2759
2760 map<string, bufferlist> keys;
2761 string filter_prefix, end_key;
2762 uint32_t i = 0;
2763 string key;
2764
2765 if (truncated)
2766 *truncated = false;
2767
2768 string start_after_key;
2769 if (key_iter.empty()) {
2770 key = BI_PREFIX_CHAR;
2771 key.append(bucket_index_prefixes[BI_BUCKET_LOG_INDEX]);
2772 key.append(marker);
2773
2774 start_after_key = key;
2775 } else {
2776 start_after_key = key_iter;
2777 }
2778
2779 if (end_marker.empty()) {
2780 end_key = BI_PREFIX_CHAR;
2781 end_key.append(bucket_index_prefixes[BI_BUCKET_LOG_INDEX + 1]);
2782 } else {
2783 end_key = BI_PREFIX_CHAR;
2784 end_key.append(bucket_index_prefixes[BI_BUCKET_LOG_INDEX]);
2785 end_key.append(end_marker);
2786 }
2787
2788 CLS_LOG(10, "bi_log_iterate_entries start_after_key=%s end_key=%s\n",
2789 start_after_key.c_str(), end_key.c_str());
2790
2791 string filter;
2792
2793 int ret = cls_cxx_map_get_vals(hctx, start_after_key, filter, max_entries,
2794 &keys, truncated);
2795 if (ret < 0)
2796 return ret;
2797
2798 map<string, bufferlist>::iterator iter = keys.begin();
2799 if (iter == keys.end())
2800 return 0;
2801
2802 uint32_t num_keys = keys.size();
2803
2804 for (; iter != keys.end(); ++iter,++i) {
2805 const string& key = iter->first;
2806 rgw_bi_log_entry e;
2807
2808 CLS_LOG(10, "bi_log_iterate_entries key=%s bl.length=%d\n", key.c_str(), (int)iter->second.length());
2809
2810 if (key.compare(end_key) > 0) {
2811 key_iter = key;
2812 if (truncated) {
2813 *truncated = false;
2814 }
2815 return 0;
2816 }
2817
2818 ret = bi_log_record_decode(iter->second, e);
2819 if (ret < 0)
2820 return ret;
2821
2822 ret = cb(hctx, key, e, param);
2823 if (ret < 0)
2824 return ret;
2825
2826 if (i == num_keys - 1) {
2827 key_iter = key;
2828 }
2829 }
2830
2831 return 0;
2832 }
2833
2834 static int bi_log_list_cb(cls_method_context_t hctx, const string& key, rgw_bi_log_entry& info, void *param)
2835 {
2836 list<rgw_bi_log_entry> *l = (list<rgw_bi_log_entry> *)param;
2837 l->push_back(info);
2838 return 0;
2839 }
2840
2841 static int bi_log_list_entries(cls_method_context_t hctx, const string& marker,
2842 uint32_t max, list<rgw_bi_log_entry>& entries, bool *truncated)
2843 {
2844 string key_iter;
2845 string end_marker;
2846 int ret = bi_log_iterate_entries(hctx, marker, end_marker,
2847 key_iter, max, truncated,
2848 bi_log_list_cb, &entries);
2849 return ret;
2850 }
2851
2852 static int rgw_bi_log_list(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
2853 {
2854 auto in_iter = in->cbegin();
2855
2856 cls_rgw_bi_log_list_op op;
2857 try {
2858 decode(op, in_iter);
2859 } catch (buffer::error& err) {
2860 CLS_LOG(1, "ERROR: rgw_bi_log_list(): failed to decode entry\n");
2861 return -EINVAL;
2862 }
2863
2864 cls_rgw_bi_log_list_ret op_ret;
2865 int ret = bi_log_list_entries(hctx, op.marker, op.max, op_ret.entries, &op_ret.truncated);
2866 if (ret < 0)
2867 return ret;
2868
2869 encode(op_ret, *out);
2870
2871 return 0;
2872 }
2873
2874 static int rgw_bi_log_trim(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
2875 {
2876 auto in_iter = in->cbegin();
2877
2878 cls_rgw_bi_log_trim_op op;
2879 try {
2880 decode(op, in_iter);
2881 } catch (buffer::error& err) {
2882 CLS_LOG(1, "ERROR: rgw_bi_log_list(): failed to decode entry\n");
2883 return -EINVAL;
2884 }
2885
2886 string key_begin(1, BI_PREFIX_CHAR);
2887 key_begin.append(bucket_index_prefixes[BI_BUCKET_LOG_INDEX]);
2888 key_begin.append(op.start_marker);
2889
2890 string key_end;
2891 if (op.end_marker.empty()) {
2892 key_end = BI_PREFIX_CHAR;
2893 key_end.append(bucket_index_prefixes[BI_BUCKET_LOG_INDEX + 1]);
2894 } else {
2895 key_end = BI_PREFIX_CHAR;
2896 key_end.append(bucket_index_prefixes[BI_BUCKET_LOG_INDEX]);
2897 key_end.append(op.end_marker);
2898 // cls_cxx_map_remove_range() expects one-past-end
2899 key_end.append(1, '\0');
2900 }
2901
2902 // list a single key to detect whether the range is empty
2903 const size_t max_entries = 1;
2904 std::set<std::string> keys;
2905 bool more = false;
2906
2907 int rc = cls_cxx_map_get_keys(hctx, key_begin, max_entries, &keys, &more);
2908 if (rc < 0) {
2909 CLS_LOG(1, "ERROR: cls_cxx_map_get_keys failed rc=%d", rc);
2910 return rc;
2911 }
2912
2913 if (keys.empty()) {
2914 CLS_LOG(20, "range is empty key_begin=%s", key_begin.c_str());
2915 return -ENODATA;
2916 }
2917
2918 const std::string& first_key = *keys.begin();
2919 if (key_end < first_key) {
2920 CLS_LOG(20, "listed key %s past key_end=%s", first_key.c_str(), key_end.c_str());
2921 return -ENODATA;
2922 }
2923
2924 CLS_LOG(20, "listed key %s, removing through %s",
2925 first_key.c_str(), key_end.c_str());
2926
2927 rc = cls_cxx_map_remove_range(hctx, first_key, key_end);
2928 if (rc < 0) {
2929 CLS_LOG(1, "ERROR: cls_cxx_map_remove_range failed rc=%d", rc);
2930 return rc;
2931 }
2932 return 0;
2933 }
2934
2935 static int rgw_bi_log_resync(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
2936 {
2937 rgw_bucket_dir_header header;
2938 int rc = read_bucket_header(hctx, &header);
2939 if (rc < 0) {
2940 CLS_LOG(1, "ERROR: rgw_bucket_complete_op(): failed to read header\n");
2941 return rc;
2942 }
2943
2944 bufferlist bl;
2945
2946 rgw_bi_log_entry entry;
2947
2948 entry.timestamp = real_clock::now();
2949 entry.op = RGWModifyOp::CLS_RGW_OP_RESYNC;
2950 entry.state = RGWPendingState::CLS_RGW_STATE_COMPLETE;
2951
2952 string key;
2953 bi_log_index_key(hctx, key, entry.id, header.ver);
2954
2955 encode(entry, bl);
2956
2957 if (entry.id > header.max_marker)
2958 header.max_marker = entry.id;
2959
2960 header.syncstopped = false;
2961
2962 rc = cls_cxx_map_set_val(hctx, key, &bl);
2963 if (rc < 0)
2964 return rc;
2965
2966 return write_bucket_header(hctx, &header);
2967 }
2968
2969 static int rgw_bi_log_stop(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
2970 {
2971 rgw_bucket_dir_header header;
2972 int rc = read_bucket_header(hctx, &header);
2973 if (rc < 0) {
2974 CLS_LOG(1, "ERROR: rgw_bucket_complete_op(): failed to read header\n");
2975 return rc;
2976 }
2977
2978 bufferlist bl;
2979
2980 rgw_bi_log_entry entry;
2981
2982 entry.timestamp = real_clock::now();
2983 entry.op = RGWModifyOp::CLS_RGW_OP_SYNCSTOP;
2984 entry.state = RGWPendingState::CLS_RGW_STATE_COMPLETE;
2985
2986 string key;
2987 bi_log_index_key(hctx, key, entry.id, header.ver);
2988
2989 encode(entry, bl);
2990
2991 if (entry.id > header.max_marker)
2992 header.max_marker = entry.id;
2993 header.syncstopped = true;
2994
2995 rc = cls_cxx_map_set_val(hctx, key, &bl);
2996 if (rc < 0)
2997 return rc;
2998
2999 return write_bucket_header(hctx, &header);
3000 }
3001
3002
3003 static void usage_record_prefix_by_time(uint64_t epoch, string& key)
3004 {
3005 char buf[32];
3006 snprintf(buf, sizeof(buf), "%011llu", (long long unsigned)epoch);
3007 key = buf;
3008 }
3009
3010 static void usage_record_prefix_by_user(const string& user, uint64_t epoch, string& key)
3011 {
3012 char buf[user.size() + 32];
3013 snprintf(buf, sizeof(buf), "%s_%011llu_", user.c_str(), (long long unsigned)epoch);
3014 key = buf;
3015 }
3016
3017 static void usage_record_name_by_time(uint64_t epoch, const string& user, const string& bucket, string& key)
3018 {
3019 char buf[32 + user.size() + bucket.size()];
3020 snprintf(buf, sizeof(buf), "%011llu_%s_%s", (long long unsigned)epoch, user.c_str(), bucket.c_str());
3021 key = buf;
3022 }
3023
3024 static void usage_record_name_by_user(const string& user, uint64_t epoch, const string& bucket, string& key)
3025 {
3026 char buf[32 + user.size() + bucket.size()];
3027 snprintf(buf, sizeof(buf), "%s_%011llu_%s", user.c_str(), (long long unsigned)epoch, bucket.c_str());
3028 key = buf;
3029 }
3030
3031 static int usage_record_decode(bufferlist& record_bl, rgw_usage_log_entry& e)
3032 {
3033 auto kiter = record_bl.cbegin();
3034 try {
3035 decode(e, kiter);
3036 } catch (buffer::error& err) {
3037 CLS_LOG(1, "ERROR: usage_record_decode(): failed to decode record_bl\n");
3038 return -EINVAL;
3039 }
3040
3041 return 0;
3042 }
3043
3044 int rgw_user_usage_log_add(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3045 {
3046 CLS_LOG(10, "rgw_user_usage_log_add()");
3047
3048 auto in_iter = in->cbegin();
3049 rgw_cls_usage_log_add_op op;
3050
3051 try {
3052 decode(op, in_iter);
3053 } catch (buffer::error& err) {
3054 CLS_LOG(1, "ERROR: rgw_user_usage_log_add(): failed to decode request\n");
3055 return -EINVAL;
3056 }
3057
3058 rgw_usage_log_info& info = op.info;
3059 vector<rgw_usage_log_entry>::iterator iter;
3060
3061 for (iter = info.entries.begin(); iter != info.entries.end(); ++iter) {
3062 rgw_usage_log_entry& entry = *iter;
3063 string key_by_time;
3064
3065 rgw_user *puser = (entry.payer.empty() ? &entry.owner : &entry.payer);
3066
3067 usage_record_name_by_time(entry.epoch, puser->to_str(), entry.bucket, key_by_time);
3068
3069 CLS_LOG(10, "rgw_user_usage_log_add user=%s bucket=%s\n", puser->to_str().c_str(), entry.bucket.c_str());
3070
3071 bufferlist record_bl;
3072 int ret = cls_cxx_map_get_val(hctx, key_by_time, &record_bl);
3073 if (ret < 0 && ret != -ENOENT) {
3074 CLS_LOG(1, "ERROR: rgw_user_usage_log_add(): cls_cxx_map_read_key returned %d\n", ret);
3075 return -EINVAL;
3076 }
3077 if (ret >= 0) {
3078 rgw_usage_log_entry e;
3079 ret = usage_record_decode(record_bl, e);
3080 if (ret < 0)
3081 return ret;
3082 CLS_LOG(10, "rgw_user_usage_log_add aggregating existing bucket\n");
3083 entry.aggregate(e);
3084 }
3085
3086 bufferlist new_record_bl;
3087 encode(entry, new_record_bl);
3088 ret = cls_cxx_map_set_val(hctx, key_by_time, &new_record_bl);
3089 if (ret < 0)
3090 return ret;
3091
3092 string key_by_user;
3093 usage_record_name_by_user(puser->to_str(), entry.epoch, entry.bucket, key_by_user);
3094 ret = cls_cxx_map_set_val(hctx, key_by_user, &new_record_bl);
3095 if (ret < 0)
3096 return ret;
3097 }
3098
3099 return 0;
3100 }
3101
3102 static int usage_iterate_range(cls_method_context_t hctx, uint64_t start, uint64_t end, const string& user,
3103 const string& bucket, string& key_iter, uint32_t max_entries, bool *truncated,
3104 int (*cb)(cls_method_context_t, const string&, rgw_usage_log_entry&, void *),
3105 void *param)
3106 {
3107 CLS_LOG(10, "usage_iterate_range");
3108
3109 map<string, bufferlist> keys;
3110 string filter_prefix;
3111 string start_key, end_key;
3112 bool by_user = !user.empty();
3113 string user_key;
3114 bool truncated_status = false;
3115
3116 ceph_assert(truncated != nullptr);
3117
3118 if (!by_user) {
3119 usage_record_prefix_by_time(end, end_key);
3120 } else {
3121 user_key = user;
3122 user_key.append("_");
3123 }
3124
3125 if (key_iter.empty()) {
3126 if (by_user) {
3127 usage_record_prefix_by_user(user, start, start_key);
3128 } else {
3129 usage_record_prefix_by_time(start, start_key);
3130 }
3131 } else {
3132 start_key = key_iter;
3133 }
3134
3135 CLS_LOG(20, "usage_iterate_range start_key=%s", start_key.c_str());
3136 int ret = cls_cxx_map_get_vals(hctx, start_key, filter_prefix, max_entries, &keys, &truncated_status);
3137 if (ret < 0)
3138 return ret;
3139
3140 *truncated = truncated_status;
3141
3142 map<string, bufferlist>::iterator iter = keys.begin();
3143 if (iter == keys.end())
3144 return 0;
3145
3146 for (; iter != keys.end(); ++iter) {
3147 const string& key = iter->first;
3148 rgw_usage_log_entry e;
3149
3150 key_iter = key;
3151 if (!by_user && key.compare(end_key) >= 0) {
3152 CLS_LOG(20, "usage_iterate_range reached key=%s, done", key.c_str());
3153 *truncated = false;
3154 key_iter = key;
3155 return 0;
3156 }
3157
3158 if (by_user && key.compare(0, user_key.size(), user_key) != 0) {
3159 CLS_LOG(20, "usage_iterate_range reached key=%s, done", key.c_str());
3160 *truncated = false;
3161 key_iter = key;
3162 return 0;
3163 }
3164
3165 ret = usage_record_decode(iter->second, e);
3166 if (ret < 0)
3167 return ret;
3168
3169 if (!bucket.empty() && bucket.compare(e.bucket))
3170 continue;
3171
3172 if (e.epoch < start)
3173 continue;
3174
3175 /* keys are sorted by epoch, so once we're past end we're done */
3176 if (e.epoch >= end) {
3177 *truncated = false;
3178 return 0;
3179 }
3180
3181 ret = cb(hctx, key, e, param);
3182 if (ret < 0)
3183 return ret;
3184 }
3185 return 0;
3186 }
3187
3188 static int usage_log_read_cb(cls_method_context_t hctx, const string& key, rgw_usage_log_entry& entry, void *param)
3189 {
3190 map<rgw_user_bucket, rgw_usage_log_entry> *usage = (map<rgw_user_bucket, rgw_usage_log_entry> *)param;
3191 rgw_user *puser;
3192 if (!entry.payer.empty()) {
3193 puser = &entry.payer;
3194 } else {
3195 puser = &entry.owner;
3196 }
3197 rgw_user_bucket ub(puser->to_str(), entry.bucket);
3198 rgw_usage_log_entry& le = (*usage)[ub];
3199 le.aggregate(entry);
3200
3201 return 0;
3202 }
3203
3204 int rgw_user_usage_log_read(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3205 {
3206 CLS_LOG(10, "rgw_user_usage_log_read()");
3207
3208 auto in_iter = in->cbegin();
3209 rgw_cls_usage_log_read_op op;
3210
3211 try {
3212 decode(op, in_iter);
3213 } catch (buffer::error& err) {
3214 CLS_LOG(1, "ERROR: rgw_user_usage_log_read(): failed to decode request\n");
3215 return -EINVAL;
3216 }
3217
3218 rgw_cls_usage_log_read_ret ret_info;
3219 map<rgw_user_bucket, rgw_usage_log_entry> *usage = &ret_info.usage;
3220 string iter = op.iter;
3221 #define MAX_ENTRIES 1000
3222 uint32_t max_entries = (op.max_entries ? op.max_entries : MAX_ENTRIES);
3223 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);
3224 if (ret < 0)
3225 return ret;
3226
3227 if (ret_info.truncated)
3228 ret_info.next_iter = iter;
3229
3230 encode(ret_info, *out);
3231 return 0;
3232 }
3233
3234 static int usage_log_trim_cb(cls_method_context_t hctx, const string& key, rgw_usage_log_entry& entry, void *param)
3235 {
3236 bool *found = (bool *)param;
3237 if (found) {
3238 *found = true;
3239 }
3240 string key_by_time;
3241 string key_by_user;
3242
3243 string o = entry.owner.to_str();
3244 usage_record_name_by_time(entry.epoch, o, entry.bucket, key_by_time);
3245 usage_record_name_by_user(o, entry.epoch, entry.bucket, key_by_user);
3246
3247 int ret = cls_cxx_map_remove_key(hctx, key_by_time);
3248 if (ret < 0)
3249 return ret;
3250
3251 return cls_cxx_map_remove_key(hctx, key_by_user);
3252 }
3253
3254 int rgw_user_usage_log_trim(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3255 {
3256 CLS_LOG(10, "rgw_user_usage_log_trim()");
3257
3258 /* only continue if object exists! */
3259 int ret = cls_cxx_stat(hctx, NULL, NULL);
3260 if (ret < 0)
3261 return ret;
3262
3263 auto in_iter = in->cbegin();
3264 rgw_cls_usage_log_trim_op op;
3265
3266 try {
3267 decode(op, in_iter);
3268 } catch (buffer::error& err) {
3269 CLS_LOG(1, "ERROR: rgw_user_log_usage_log_trim(): failed to decode request\n");
3270 return -EINVAL;
3271 }
3272
3273 string iter;
3274 bool more;
3275 bool found = false;
3276 #define MAX_USAGE_TRIM_ENTRIES 1000
3277 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);
3278 if (ret < 0)
3279 return ret;
3280
3281 if (!more && !found)
3282 return -ENODATA;
3283
3284 return 0;
3285 }
3286
3287 int rgw_usage_log_clear(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3288 {
3289 CLS_LOG(10,"%s", __func__);
3290
3291 int ret = cls_cxx_map_clear(hctx);
3292 /* if object doesn't exist all the logs are cleared anyway */
3293 if (ret == -ENOENT)
3294 ret = 0;
3295
3296 return ret;
3297 }
3298
3299 /*
3300 * We hold the garbage collection chain data under two different
3301 * indexes: the first 'name' index keeps them under a unique tag that
3302 * represents the chains, and a second 'time' index keeps them by
3303 * their expiration timestamp. Each is prefixed differently (see
3304 * gc_index_prefixes below).
3305 *
3306 * Since key-value data is listed in lexical order by keys, generally
3307 * the name entries are retrieved first and then the time entries.
3308 * When listing the entries via `gc_iterate_entries` one parameter is
3309 * a marker, and if we were to pass "1_" (i.e.,
3310 * gc_index_prefixes[GC_OBJ_TIME_INDEX]), the listing would skip over
3311 * the 'name' entries and begin with the 'time' entries.
3312 *
3313 * Furthermore, the times are converted to strings such that lexical
3314 * order correlates with chronological order, so the entries are
3315 * returned chronologically from the earliest expiring to the latest
3316 * expiring. This allows for starting at "1_" and to keep retrieving
3317 * chunks of entries, and as long as they are prior to the current
3318 * time, they're expired and processing can continue.
3319 */
3320 #define GC_OBJ_NAME_INDEX 0
3321 #define GC_OBJ_TIME_INDEX 1
3322
3323 static string gc_index_prefixes[] = { "0_",
3324 "1_" };
3325
3326 static void prepend_index_prefix(const string& src, int index, string *dest)
3327 {
3328 *dest = gc_index_prefixes[index];
3329 dest->append(src);
3330 }
3331
3332 static int gc_omap_get(cls_method_context_t hctx, int type, const string& key, cls_rgw_gc_obj_info *info)
3333 {
3334 string index;
3335 prepend_index_prefix(key, type, &index);
3336
3337 int ret = read_omap_entry(hctx, index, info);
3338 if (ret < 0)
3339 return ret;
3340
3341 return 0;
3342 }
3343
3344 static int gc_omap_set(cls_method_context_t hctx, int type, const string& key, const cls_rgw_gc_obj_info *info)
3345 {
3346 bufferlist bl;
3347 encode(*info, bl);
3348
3349 string index = gc_index_prefixes[type];
3350 index.append(key);
3351
3352 int ret = cls_cxx_map_set_val(hctx, index, &bl);
3353 if (ret < 0)
3354 return ret;
3355
3356 return 0;
3357 }
3358
3359 static int gc_omap_remove(cls_method_context_t hctx, int type, const string& key)
3360 {
3361 string index = gc_index_prefixes[type];
3362 index.append(key);
3363
3364 int ret = cls_cxx_map_remove_key(hctx, index);
3365 if (ret < 0)
3366 return ret;
3367
3368 return 0;
3369 }
3370
3371 static bool key_in_index(const string& key, int index_type)
3372 {
3373 const string& prefix = gc_index_prefixes[index_type];
3374 return (key.compare(0, prefix.size(), prefix) == 0);
3375 }
3376
3377
3378 static int gc_update_entry(cls_method_context_t hctx, uint32_t expiration_secs,
3379 cls_rgw_gc_obj_info& info)
3380 {
3381 cls_rgw_gc_obj_info old_info;
3382 int ret = gc_omap_get(hctx, GC_OBJ_NAME_INDEX, info.tag, &old_info);
3383 if (ret == 0) {
3384 string key;
3385 get_time_key(old_info.time, &key);
3386 ret = gc_omap_remove(hctx, GC_OBJ_TIME_INDEX, key);
3387 if (ret < 0 && ret != -ENOENT) {
3388 CLS_LOG(0, "ERROR: failed to remove key=%s\n", key.c_str());
3389 return ret;
3390 }
3391 }
3392
3393 // calculate time and time key
3394 info.time = ceph::real_clock::now();
3395 info.time += make_timespan(expiration_secs);
3396 string time_key;
3397 get_time_key(info.time, &time_key);
3398
3399 if (info.chain.objs.empty()) {
3400 CLS_LOG(0,
3401 "WARNING: %s setting GC log entry with zero-length chain, "
3402 "tag='%s', timekey='%s'",
3403 __func__, info.tag.c_str(), time_key.c_str());
3404 }
3405
3406 ret = gc_omap_set(hctx, GC_OBJ_NAME_INDEX, info.tag, &info);
3407 if (ret < 0)
3408 return ret;
3409
3410 ret = gc_omap_set(hctx, GC_OBJ_TIME_INDEX, time_key, &info);
3411 if (ret < 0)
3412 goto done_err;
3413
3414 return 0;
3415
3416 done_err:
3417
3418 CLS_LOG(0, "ERROR: gc_set_entry error info.tag=%s, ret=%d\n",
3419 info.tag.c_str(), ret);
3420 gc_omap_remove(hctx, GC_OBJ_NAME_INDEX, info.tag);
3421
3422 return ret;
3423 }
3424
3425 static int gc_defer_entry(cls_method_context_t hctx, const string& tag, uint32_t expiration_secs)
3426 {
3427 cls_rgw_gc_obj_info info;
3428 int ret = gc_omap_get(hctx, GC_OBJ_NAME_INDEX, tag, &info);
3429 if (ret < 0)
3430 return ret;
3431 return gc_update_entry(hctx, expiration_secs, info);
3432 }
3433
3434 int gc_record_decode(bufferlist& bl, cls_rgw_gc_obj_info& e)
3435 {
3436 auto iter = bl.cbegin();
3437 try {
3438 decode(e, iter);
3439 } catch (buffer::error& err) {
3440 CLS_LOG(0, "ERROR: failed to decode cls_rgw_gc_obj_info");
3441 return -EIO;
3442 }
3443 return 0;
3444 }
3445
3446 static int rgw_cls_gc_set_entry(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3447 {
3448 auto in_iter = in->cbegin();
3449
3450 cls_rgw_gc_set_entry_op op;
3451 try {
3452 decode(op, in_iter);
3453 } catch (buffer::error& err) {
3454 CLS_LOG(1, "ERROR: rgw_cls_gc_set_entry(): failed to decode entry\n");
3455 return -EINVAL;
3456 }
3457
3458 return gc_update_entry(hctx, op.expiration_secs, op.info);
3459 }
3460
3461 static int rgw_cls_gc_defer_entry(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3462 {
3463 auto in_iter = in->cbegin();
3464
3465 cls_rgw_gc_defer_entry_op op;
3466 try {
3467 decode(op, in_iter);
3468 } catch (buffer::error& err) {
3469 CLS_LOG(1, "ERROR: rgw_cls_gc_defer_entry(): failed to decode entry\n");
3470 return -EINVAL;
3471 }
3472
3473 return gc_defer_entry(hctx, op.tag, op.expiration_secs);
3474 }
3475
3476 static int gc_iterate_entries(cls_method_context_t hctx,
3477 const string& marker,
3478 bool expired_only,
3479 string& out_marker,
3480 uint32_t max_entries,
3481 bool *truncated,
3482 int (*cb)(cls_method_context_t,
3483 const string&,
3484 cls_rgw_gc_obj_info&,
3485 void *),
3486 void *param)
3487 {
3488 CLS_LOG(10, "gc_iterate_entries");
3489
3490 map<string, bufferlist> keys;
3491 string filter_prefix, end_key;
3492 string key;
3493
3494 if (truncated)
3495 *truncated = false;
3496
3497 string start_key;
3498 if (marker.empty()) {
3499 prepend_index_prefix(marker, GC_OBJ_TIME_INDEX, &start_key);
3500 } else {
3501 start_key = marker;
3502 }
3503
3504 if (expired_only) {
3505 real_time now = ceph::real_clock::now();
3506 string now_str;
3507 get_time_key(now, &now_str);
3508 prepend_index_prefix(now_str, GC_OBJ_TIME_INDEX, &end_key);
3509
3510 CLS_LOG(10, "gc_iterate_entries end_key=%s\n", end_key.c_str());
3511 }
3512
3513 string filter;
3514
3515 int ret = cls_cxx_map_get_vals(hctx, start_key, filter, max_entries,
3516 &keys, truncated);
3517 if (ret < 0)
3518 return ret;
3519
3520 map<string, bufferlist>::iterator iter = keys.begin();
3521 if (iter == keys.end()) {
3522 // if keys empty must not come back as truncated
3523 ceph_assert(!truncated || !(*truncated));
3524 return 0;
3525 }
3526
3527 const string* last_key = nullptr; // last key processed, for end-marker
3528 for (; iter != keys.end(); ++iter) {
3529 const string& key = iter->first;
3530 cls_rgw_gc_obj_info e;
3531
3532 CLS_LOG(10, "gc_iterate_entries key=%s\n", key.c_str());
3533
3534 if (!end_key.empty() && key.compare(end_key) >= 0) {
3535 if (truncated)
3536 *truncated = false;
3537 return 0;
3538 }
3539
3540 if (!key_in_index(key, GC_OBJ_TIME_INDEX)) {
3541 if (truncated)
3542 *truncated = false;
3543 return 0;
3544 }
3545
3546 ret = gc_record_decode(iter->second, e);
3547 if (ret < 0)
3548 return ret;
3549
3550 ret = cb(hctx, key, e, param);
3551 if (ret < 0)
3552 return ret;
3553 last_key = &(iter->first); // update when callback successful
3554 }
3555
3556 // set the out marker if either caller does not capture truncated or
3557 // if they do capture and we are truncated
3558 if (!truncated || *truncated) {
3559 assert(last_key);
3560 out_marker = *last_key;
3561 }
3562
3563 return 0;
3564 }
3565
3566 static int gc_list_cb(cls_method_context_t hctx, const string& key, cls_rgw_gc_obj_info& info, void *param)
3567 {
3568 list<cls_rgw_gc_obj_info> *l = (list<cls_rgw_gc_obj_info> *)param;
3569 l->push_back(info);
3570 return 0;
3571 }
3572
3573 static int gc_list_entries(cls_method_context_t hctx, const string& marker,
3574 uint32_t max, bool expired_only,
3575 list<cls_rgw_gc_obj_info>& entries, bool *truncated, string& next_marker)
3576 {
3577 int ret = gc_iterate_entries(hctx, marker, expired_only,
3578 next_marker, max, truncated,
3579 gc_list_cb, &entries);
3580 return ret;
3581 }
3582
3583 static int rgw_cls_gc_list(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3584 {
3585 auto in_iter = in->cbegin();
3586
3587 cls_rgw_gc_list_op op;
3588 try {
3589 decode(op, in_iter);
3590 } catch (buffer::error& err) {
3591 CLS_LOG(1, "ERROR: rgw_cls_gc_list(): failed to decode entry\n");
3592 return -EINVAL;
3593 }
3594
3595 cls_rgw_gc_list_ret op_ret;
3596 #define GC_LIST_ENTRIES_DEFAULT 128
3597 int ret = gc_list_entries(hctx, op.marker, (op.max ? op.max : GC_LIST_ENTRIES_DEFAULT), op.expired_only,
3598 op_ret.entries, &op_ret.truncated, op_ret.next_marker);
3599 if (ret < 0)
3600 return ret;
3601
3602 encode(op_ret, *out);
3603
3604 return 0;
3605 }
3606
3607 static int gc_remove(cls_method_context_t hctx, vector<string>& tags)
3608 {
3609 for (auto iter = tags.begin(); iter != tags.end(); ++iter) {
3610 string& tag = *iter;
3611 cls_rgw_gc_obj_info info;
3612 int ret = gc_omap_get(hctx, GC_OBJ_NAME_INDEX, tag, &info);
3613 if (ret == -ENOENT) {
3614 CLS_LOG(0, "couldn't find tag in name index tag=%s\n", tag.c_str());
3615 continue;
3616 }
3617
3618 if (ret < 0)
3619 return ret;
3620
3621 string time_key;
3622 get_time_key(info.time, &time_key);
3623 ret = gc_omap_remove(hctx, GC_OBJ_TIME_INDEX, time_key);
3624 if (ret < 0 && ret != -ENOENT)
3625 return ret;
3626 if (ret == -ENOENT) {
3627 CLS_LOG(0, "couldn't find key in time index key=%s\n", time_key.c_str());
3628 }
3629
3630 ret = gc_omap_remove(hctx, GC_OBJ_NAME_INDEX, tag);
3631 if (ret < 0 && ret != -ENOENT)
3632 return ret;
3633 }
3634
3635 return 0;
3636 }
3637
3638 static int rgw_cls_gc_remove(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3639 {
3640 auto in_iter = in->cbegin();
3641
3642 cls_rgw_gc_remove_op op;
3643 try {
3644 decode(op, in_iter);
3645 } catch (buffer::error& err) {
3646 CLS_LOG(1, "ERROR: rgw_cls_gc_remove(): failed to decode entry\n");
3647 return -EINVAL;
3648 }
3649
3650 return gc_remove(hctx, op.tags);
3651 }
3652
3653 static int rgw_cls_lc_get_entry(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3654 {
3655 auto in_iter = in->cbegin();
3656
3657 cls_rgw_lc_get_entry_op op;
3658 try {
3659 decode(op, in_iter);
3660 } catch (buffer::error& err) {
3661 CLS_LOG(1, "ERROR: rgw_cls_lc_set_entry(): failed to decode entry\n");
3662 return -EINVAL;
3663 }
3664
3665 cls_rgw_lc_entry lc_entry;
3666 int ret = read_omap_entry(hctx, op.marker, &lc_entry);
3667 if (ret < 0)
3668 return ret;
3669
3670 cls_rgw_lc_get_entry_ret op_ret(std::move(lc_entry));
3671 encode(op_ret, *out);
3672 return 0;
3673 }
3674
3675
3676 static int rgw_cls_lc_set_entry(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3677 {
3678 auto in_iter = in->cbegin();
3679
3680 cls_rgw_lc_set_entry_op op;
3681 try {
3682 decode(op, in_iter);
3683 } catch (buffer::error& err) {
3684 CLS_LOG(1, "ERROR: rgw_cls_lc_set_entry(): failed to decode entry\n");
3685 return -EINVAL;
3686 }
3687
3688 bufferlist bl;
3689 encode(op.entry, bl);
3690
3691 int ret = cls_cxx_map_set_val(hctx, op.entry.bucket, &bl);
3692 return ret;
3693 }
3694
3695 static int rgw_cls_lc_rm_entry(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3696 {
3697 auto in_iter = in->cbegin();
3698
3699 cls_rgw_lc_rm_entry_op op;
3700 try {
3701 decode(op, in_iter);
3702 } catch (buffer::error& err) {
3703 CLS_LOG(1, "ERROR: rgw_cls_lc_rm_entry(): failed to decode entry\n");
3704 return -EINVAL;
3705 }
3706
3707 int ret = cls_cxx_map_remove_key(hctx, op.entry.bucket);
3708 return ret;
3709 }
3710
3711 static int rgw_cls_lc_get_next_entry(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3712 {
3713 auto in_iter = in->cbegin();
3714 cls_rgw_lc_get_next_entry_ret op_ret;
3715 cls_rgw_lc_get_next_entry_op op;
3716 try {
3717 decode(op, in_iter);
3718 } catch (buffer::error& err) {
3719 CLS_LOG(1, "ERROR: rgw_cls_lc_get_next_entry: failed to decode op\n");
3720 return -EINVAL;
3721 }
3722
3723 map<string, bufferlist> vals;
3724 string filter_prefix;
3725 bool more;
3726 int ret = cls_cxx_map_get_vals(hctx, op.marker, filter_prefix, 1, &vals, &more);
3727 if (ret < 0)
3728 return ret;
3729 map<string, bufferlist>::iterator it;
3730 cls_rgw_lc_entry entry;
3731 if (!vals.empty()) {
3732 it=vals.begin();
3733 in_iter = it->second.begin();
3734 try {
3735 decode(entry, in_iter);
3736 } catch (buffer::error& err) {
3737 CLS_LOG(1, "ERROR: rgw_cls_lc_get_next_entry(): failed to decode entry\n");
3738 return -EIO;
3739 }
3740 }
3741 op_ret.entry = entry;
3742 encode(op_ret, *out);
3743 return 0;
3744 }
3745
3746 static int rgw_cls_lc_list_entries(cls_method_context_t hctx, bufferlist *in,
3747 bufferlist *out)
3748 {
3749 cls_rgw_lc_list_entries_op op;
3750 auto in_iter = in->cbegin();
3751 try {
3752 decode(op, in_iter);
3753 } catch (buffer::error& err) {
3754 CLS_LOG(1, "ERROR: rgw_cls_lc_list_entries(): failed to decode op\n");
3755 return -EINVAL;
3756 }
3757
3758 cls_rgw_lc_list_entries_ret op_ret(op.compat_v);
3759 bufferlist::const_iterator iter;
3760 map<string, bufferlist> vals;
3761 string filter_prefix;
3762 int ret = cls_cxx_map_get_vals(hctx, op.marker, filter_prefix, op.max_entries,
3763 &vals, &op_ret.is_truncated);
3764 if (ret < 0)
3765 return ret;
3766 map<string, bufferlist>::iterator it;
3767 for (auto it = vals.begin(); it != vals.end(); ++it) {
3768 cls_rgw_lc_entry entry;
3769 iter = it->second.cbegin();
3770 try {
3771 decode(entry, iter);
3772 } catch (buffer::error& err) {
3773 /* try backward compat */
3774 pair<string, int> oe;
3775 try {
3776 iter = it->second.begin();
3777 decode(oe, iter);
3778 entry = {oe.first, 0 /* start */, uint32_t(oe.second)};
3779 } catch(buffer::error& err) {
3780 CLS_LOG(
3781 1, "ERROR: rgw_cls_lc_list_entries(): failed to decode entry\n");
3782 return -EIO;
3783 }
3784 }
3785 op_ret.entries.push_back(entry);
3786 }
3787 encode(op_ret, *out);
3788 return 0;
3789 }
3790
3791 static int rgw_cls_lc_put_head(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3792 {
3793 auto in_iter = in->cbegin();
3794
3795 cls_rgw_lc_put_head_op op;
3796 try {
3797 decode(op, in_iter);
3798 } catch (buffer::error& err) {
3799 CLS_LOG(1, "ERROR: rgw_cls_lc_put_head(): failed to decode entry\n");
3800 return -EINVAL;
3801 }
3802
3803 bufferlist bl;
3804 encode(op.head, bl);
3805 int ret = cls_cxx_map_write_header(hctx,&bl);
3806 return ret;
3807 }
3808
3809 static int rgw_cls_lc_get_head(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3810 {
3811 bufferlist bl;
3812 int ret = cls_cxx_map_read_header(hctx, &bl);
3813 if (ret < 0)
3814 return ret;
3815 cls_rgw_lc_obj_head head;
3816 if (bl.length() != 0) {
3817 auto iter = bl.cbegin();
3818 try {
3819 decode(head, iter);
3820 } catch (buffer::error& err) {
3821 CLS_LOG(0, "ERROR: rgw_cls_lc_get_head(): failed to decode entry %s\n",err.what());
3822 return -EINVAL;
3823 }
3824 } else {
3825 head.start_date = 0;
3826 head.marker.clear();
3827 }
3828 cls_rgw_lc_get_head_ret op_ret;
3829 op_ret.head = head;
3830 encode(op_ret, *out);
3831 return 0;
3832 }
3833
3834 static int rgw_reshard_add(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3835 {
3836 auto in_iter = in->cbegin();
3837
3838 cls_rgw_reshard_add_op op;
3839 try {
3840 decode(op, in_iter);
3841 } catch (buffer::error& err) {
3842 CLS_LOG(1, "ERROR: rgw_reshard_add: failed to decode entry\n");
3843 return -EINVAL;
3844 }
3845
3846
3847 string key;
3848 op.entry.get_key(&key);
3849
3850 bufferlist bl;
3851 encode(op.entry, bl);
3852 int ret = cls_cxx_map_set_val(hctx, key, &bl);
3853 if (ret < 0) {
3854 CLS_ERR("error adding reshard job for bucket %s with key %s",op.entry.bucket_name.c_str(), key.c_str());
3855 return ret;
3856 }
3857
3858 return ret;
3859 }
3860
3861 static int rgw_reshard_list(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3862 {
3863 cls_rgw_reshard_list_op op;
3864 auto in_iter = in->cbegin();
3865 try {
3866 decode(op, in_iter);
3867 } catch (buffer::error& err) {
3868 CLS_LOG(1, "ERROR: rgw_cls_rehard_list(): failed to decode entry\n");
3869 return -EINVAL;
3870 }
3871 cls_rgw_reshard_list_ret op_ret;
3872 bufferlist::const_iterator iter;
3873 map<string, bufferlist> vals;
3874 string filter_prefix;
3875 #define MAX_RESHARD_LIST_ENTRIES 1000
3876 /* one extra entry for identifying truncation */
3877 int32_t max = (op.max && (op.max < MAX_RESHARD_LIST_ENTRIES) ? op.max : MAX_RESHARD_LIST_ENTRIES);
3878 int ret = cls_cxx_map_get_vals(hctx, op.marker, filter_prefix, max, &vals, &op_ret.is_truncated);
3879 if (ret < 0)
3880 return ret;
3881 map<string, bufferlist>::iterator it;
3882 cls_rgw_reshard_entry entry;
3883 int i = 0;
3884 for (it = vals.begin(); i < (int)op.max && it != vals.end(); ++it, ++i) {
3885 iter = it->second.cbegin();
3886 try {
3887 decode(entry, iter);
3888 } catch (buffer::error& err) {
3889 CLS_LOG(1, "ERROR: rgw_cls_rehard_list(): failed to decode entry\n");
3890 return -EIO;
3891 }
3892 op_ret.entries.push_back(entry);
3893 }
3894 encode(op_ret, *out);
3895 return 0;
3896 }
3897
3898 static int rgw_reshard_get(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3899 {
3900 auto in_iter = in->cbegin();
3901
3902 cls_rgw_reshard_get_op op;
3903 try {
3904 decode(op, in_iter);
3905 } catch (buffer::error& err) {
3906 CLS_LOG(1, "ERROR: rgw_reshard_get: failed to decode entry\n");
3907 return -EINVAL;
3908 }
3909
3910 string key;
3911 cls_rgw_reshard_entry entry;
3912 op.entry.get_key(&key);
3913 int ret = read_omap_entry(hctx, key, &entry);
3914 if (ret < 0) {
3915 return ret;
3916 }
3917
3918 cls_rgw_reshard_get_ret op_ret;
3919 op_ret.entry = entry;
3920 encode(op_ret, *out);
3921 return 0;
3922 }
3923
3924 static int rgw_reshard_remove(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3925 {
3926 auto in_iter = in->cbegin();
3927
3928 cls_rgw_reshard_remove_op op;
3929 try {
3930 decode(op, in_iter);
3931 } catch (buffer::error& err) {
3932 CLS_LOG(1, "ERROR: rgw_cls_rehard_remove: failed to decode entry\n");
3933 return -EINVAL;
3934 }
3935
3936 string key;
3937 cls_rgw_reshard_entry entry;
3938 cls_rgw_reshard_entry::generate_key(op.tenant, op.bucket_name, &key);
3939 int ret = read_omap_entry(hctx, key, &entry);
3940 if (ret < 0) {
3941 return ret;
3942 }
3943
3944 if (!op.bucket_id.empty() &&
3945 entry.bucket_id != op.bucket_id) {
3946 return 0;
3947 }
3948
3949 ret = cls_cxx_map_remove_key(hctx, key);
3950 if (ret < 0) {
3951 CLS_LOG(0, "ERROR: failed to remove key: key=%s ret=%d", key.c_str(), ret);
3952 return 0;
3953 }
3954 return ret;
3955 }
3956
3957 static int rgw_set_bucket_resharding(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3958 {
3959 cls_rgw_set_bucket_resharding_op op;
3960
3961 auto in_iter = in->cbegin();
3962 try {
3963 decode(op, in_iter);
3964 } catch (buffer::error& err) {
3965 CLS_LOG(1, "ERROR: cls_rgw_set_bucket_resharding: failed to decode entry\n");
3966 return -EINVAL;
3967 }
3968
3969 rgw_bucket_dir_header header;
3970 int rc = read_bucket_header(hctx, &header);
3971 if (rc < 0) {
3972 CLS_LOG(1, "ERROR: %s(): failed to read header\n", __func__);
3973 return rc;
3974 }
3975
3976 header.new_instance.set_status(op.entry.new_bucket_instance_id, op.entry.num_shards, op.entry.reshard_status);
3977
3978 return write_bucket_header(hctx, &header);
3979 }
3980
3981 static int rgw_clear_bucket_resharding(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
3982 {
3983 cls_rgw_clear_bucket_resharding_op op;
3984
3985 auto in_iter = in->cbegin();
3986 try {
3987 decode(op, in_iter);
3988 } catch (buffer::error& err) {
3989 CLS_LOG(1, "ERROR: cls_rgw_clear_bucket_resharding: failed to decode entry\n");
3990 return -EINVAL;
3991 }
3992
3993 rgw_bucket_dir_header header;
3994 int rc = read_bucket_header(hctx, &header);
3995 if (rc < 0) {
3996 CLS_LOG(1, "ERROR: %s(): failed to read header\n", __func__);
3997 return rc;
3998 }
3999 header.new_instance.clear();
4000
4001 return write_bucket_header(hctx, &header);
4002 }
4003
4004 static int rgw_guard_bucket_resharding(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
4005 {
4006 cls_rgw_guard_bucket_resharding_op op;
4007
4008 auto in_iter = in->cbegin();
4009 try {
4010 decode(op, in_iter);
4011 } catch (buffer::error& err) {
4012 CLS_LOG(1, "ERROR: %s(): failed to decode entry\n", __func__);
4013 return -EINVAL;
4014 }
4015
4016 rgw_bucket_dir_header header;
4017 int rc = read_bucket_header(hctx, &header);
4018 if (rc < 0) {
4019 CLS_LOG(1, "ERROR: %s(): failed to read header\n", __func__);
4020 return rc;
4021 }
4022
4023 if (header.resharding()) {
4024 return op.ret_err;
4025 }
4026
4027 return 0;
4028 }
4029
4030 static int rgw_get_bucket_resharding(cls_method_context_t hctx,
4031 bufferlist *in, bufferlist *out)
4032 {
4033 cls_rgw_get_bucket_resharding_op op;
4034
4035 auto in_iter = in->cbegin();
4036 try {
4037 decode(op, in_iter);
4038 } catch (buffer::error& err) {
4039 CLS_LOG(1, "ERROR: %s(): failed to decode entry\n", __func__);
4040 return -EINVAL;
4041 }
4042
4043 rgw_bucket_dir_header header;
4044 int rc = read_bucket_header(hctx, &header);
4045 if (rc < 0) {
4046 CLS_LOG(1, "ERROR: %s(): failed to read header\n", __func__);
4047 return rc;
4048 }
4049
4050 cls_rgw_get_bucket_resharding_ret op_ret;
4051 op_ret.new_instance = header.new_instance;
4052
4053 encode(op_ret, *out);
4054
4055 return 0;
4056 }
4057
4058 CLS_INIT(rgw)
4059 {
4060 CLS_LOG(1, "Loaded rgw class!");
4061
4062 cls_handle_t h_class;
4063 cls_method_handle_t h_rgw_bucket_init_index;
4064 cls_method_handle_t h_rgw_bucket_set_tag_timeout;
4065 cls_method_handle_t h_rgw_bucket_list;
4066 cls_method_handle_t h_rgw_bucket_check_index;
4067 cls_method_handle_t h_rgw_bucket_rebuild_index;
4068 cls_method_handle_t h_rgw_bucket_update_stats;
4069 cls_method_handle_t h_rgw_bucket_prepare_op;
4070 cls_method_handle_t h_rgw_bucket_complete_op;
4071 cls_method_handle_t h_rgw_bucket_link_olh;
4072 cls_method_handle_t h_rgw_bucket_unlink_instance_op;
4073 cls_method_handle_t h_rgw_bucket_read_olh_log;
4074 cls_method_handle_t h_rgw_bucket_trim_olh_log;
4075 cls_method_handle_t h_rgw_bucket_clear_olh;
4076 cls_method_handle_t h_rgw_obj_remove;
4077 cls_method_handle_t h_rgw_obj_store_pg_ver;
4078 cls_method_handle_t h_rgw_obj_check_attrs_prefix;
4079 cls_method_handle_t h_rgw_obj_check_mtime;
4080 cls_method_handle_t h_rgw_bi_get_op;
4081 cls_method_handle_t h_rgw_bi_put_op;
4082 cls_method_handle_t h_rgw_bi_list_op;
4083 cls_method_handle_t h_rgw_bi_log_list_op;
4084 cls_method_handle_t h_rgw_bi_log_resync_op;
4085 cls_method_handle_t h_rgw_bi_log_stop_op;
4086 cls_method_handle_t h_rgw_dir_suggest_changes;
4087 cls_method_handle_t h_rgw_user_usage_log_add;
4088 cls_method_handle_t h_rgw_user_usage_log_read;
4089 cls_method_handle_t h_rgw_user_usage_log_trim;
4090 cls_method_handle_t h_rgw_usage_log_clear;
4091 cls_method_handle_t h_rgw_gc_set_entry;
4092 cls_method_handle_t h_rgw_gc_list;
4093 cls_method_handle_t h_rgw_gc_remove;
4094 cls_method_handle_t h_rgw_lc_get_entry;
4095 cls_method_handle_t h_rgw_lc_set_entry;
4096 cls_method_handle_t h_rgw_lc_rm_entry;
4097 cls_method_handle_t h_rgw_lc_get_next_entry;
4098 cls_method_handle_t h_rgw_lc_put_head;
4099 cls_method_handle_t h_rgw_lc_get_head;
4100 cls_method_handle_t h_rgw_lc_list_entries;
4101 cls_method_handle_t h_rgw_reshard_add;
4102 cls_method_handle_t h_rgw_reshard_list;
4103 cls_method_handle_t h_rgw_reshard_get;
4104 cls_method_handle_t h_rgw_reshard_remove;
4105 cls_method_handle_t h_rgw_set_bucket_resharding;
4106 cls_method_handle_t h_rgw_clear_bucket_resharding;
4107 cls_method_handle_t h_rgw_guard_bucket_resharding;
4108 cls_method_handle_t h_rgw_get_bucket_resharding;
4109
4110 cls_register(RGW_CLASS, &h_class);
4111
4112 /* bucket index */
4113 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);
4114 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);
4115 cls_register_cxx_method(h_class, RGW_BUCKET_LIST, CLS_METHOD_RD, rgw_bucket_list, &h_rgw_bucket_list);
4116 cls_register_cxx_method(h_class, RGW_BUCKET_CHECK_INDEX, CLS_METHOD_RD, rgw_bucket_check_index, &h_rgw_bucket_check_index);
4117 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);
4118 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);
4119 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);
4120 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);
4121 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);
4122 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);
4123 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);
4124 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);
4125 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);
4126
4127 cls_register_cxx_method(h_class, RGW_OBJ_REMOVE, CLS_METHOD_RD | CLS_METHOD_WR, rgw_obj_remove, &h_rgw_obj_remove);
4128 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);
4129 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);
4130 cls_register_cxx_method(h_class, RGW_OBJ_CHECK_MTIME, CLS_METHOD_RD, rgw_obj_check_mtime, &h_rgw_obj_check_mtime);
4131
4132 cls_register_cxx_method(h_class, RGW_BI_GET, CLS_METHOD_RD, rgw_bi_get_op, &h_rgw_bi_get_op);
4133 cls_register_cxx_method(h_class, RGW_BI_PUT, CLS_METHOD_RD | CLS_METHOD_WR, rgw_bi_put_op, &h_rgw_bi_put_op);
4134 cls_register_cxx_method(h_class, RGW_BI_LIST, CLS_METHOD_RD, rgw_bi_list_op, &h_rgw_bi_list_op);
4135
4136 cls_register_cxx_method(h_class, RGW_BI_LOG_LIST, CLS_METHOD_RD, rgw_bi_log_list, &h_rgw_bi_log_list_op);
4137 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);
4138 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);
4139
4140 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);
4141 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);
4142
4143 /* usage logging */
4144 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);
4145 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);
4146 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);
4147 cls_register_cxx_method(h_class, RGW_USAGE_LOG_CLEAR, CLS_METHOD_WR, rgw_usage_log_clear, &h_rgw_usage_log_clear);
4148
4149 /* garbage collection */
4150 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);
4151 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);
4152 cls_register_cxx_method(h_class, RGW_GC_LIST, CLS_METHOD_RD, rgw_cls_gc_list, &h_rgw_gc_list);
4153 cls_register_cxx_method(h_class, RGW_GC_REMOVE, CLS_METHOD_RD | CLS_METHOD_WR, rgw_cls_gc_remove, &h_rgw_gc_remove);
4154
4155 /* lifecycle bucket list */
4156 cls_register_cxx_method(h_class, RGW_LC_GET_ENTRY, CLS_METHOD_RD, rgw_cls_lc_get_entry, &h_rgw_lc_get_entry);
4157 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);
4158 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);
4159 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);
4160 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);
4161 cls_register_cxx_method(h_class, RGW_LC_GET_HEAD, CLS_METHOD_RD, rgw_cls_lc_get_head, &h_rgw_lc_get_head);
4162 cls_register_cxx_method(h_class, RGW_LC_LIST_ENTRIES, CLS_METHOD_RD, rgw_cls_lc_list_entries, &h_rgw_lc_list_entries);
4163
4164 /* resharding */
4165 cls_register_cxx_method(h_class, RGW_RESHARD_ADD, CLS_METHOD_RD | CLS_METHOD_WR, rgw_reshard_add, &h_rgw_reshard_add);
4166 cls_register_cxx_method(h_class, RGW_RESHARD_LIST, CLS_METHOD_RD, rgw_reshard_list, &h_rgw_reshard_list);
4167 cls_register_cxx_method(h_class, RGW_RESHARD_GET, CLS_METHOD_RD,rgw_reshard_get, &h_rgw_reshard_get);
4168 cls_register_cxx_method(h_class, RGW_RESHARD_REMOVE, CLS_METHOD_RD | CLS_METHOD_WR, rgw_reshard_remove, &h_rgw_reshard_remove);
4169
4170 /* resharding attribute */
4171 cls_register_cxx_method(h_class, RGW_SET_BUCKET_RESHARDING, CLS_METHOD_RD | CLS_METHOD_WR,
4172 rgw_set_bucket_resharding, &h_rgw_set_bucket_resharding);
4173 cls_register_cxx_method(h_class, RGW_CLEAR_BUCKET_RESHARDING, CLS_METHOD_RD | CLS_METHOD_WR,
4174 rgw_clear_bucket_resharding, &h_rgw_clear_bucket_resharding);
4175 cls_register_cxx_method(h_class, RGW_GUARD_BUCKET_RESHARDING, CLS_METHOD_RD ,
4176 rgw_guard_bucket_resharding, &h_rgw_guard_bucket_resharding);
4177 cls_register_cxx_method(h_class, RGW_GET_BUCKET_RESHARDING, CLS_METHOD_RD ,
4178 rgw_get_bucket_resharding, &h_rgw_get_bucket_resharding);
4179
4180 return;
4181 }