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