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