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