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