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