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