]> git.proxmox.com Git - ceph.git/blob - ceph/src/rgw/rgw_rados.h
73fb4682e0d52924f5f8aac1f69fc2275e541415
[ceph.git] / ceph / src / rgw / rgw_rados.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #ifndef CEPH_RGWRADOS_H
5 #define CEPH_RGWRADOS_H
6
7 #include <functional>
8
9 #include "include/rados/librados.hpp"
10 #include "include/Context.h"
11 #include "common/RefCountedObj.h"
12 #include "common/RWLock.h"
13 #include "common/ceph_time.h"
14 #include "common/lru_map.h"
15 #include "rgw_common.h"
16 #include "cls/rgw/cls_rgw_types.h"
17 #include "cls/version/cls_version_types.h"
18 #include "cls/log/cls_log_types.h"
19 #include "cls/statelog/cls_statelog_types.h"
20 #include "cls/timeindex/cls_timeindex_types.h"
21 #include "rgw_log.h"
22 #include "rgw_metadata.h"
23 #include "rgw_meta_sync_status.h"
24 #include "rgw_period_puller.h"
25 #include "rgw_sync_module.h"
26
27 class RGWWatcher;
28 class SafeTimer;
29 class ACLOwner;
30 class RGWGC;
31 class RGWMetaNotifier;
32 class RGWDataNotifier;
33 class RGWLC;
34 class RGWObjectExpirer;
35 class RGWMetaSyncProcessorThread;
36 class RGWDataSyncProcessorThread;
37 class RGWSyncLogTrimThread;
38 class RGWRESTConn;
39 struct RGWZoneGroup;
40 struct RGWZoneParams;
41 class RGWReshard;
42 class RGWReshardWait;
43
44 /* flags for put_obj_meta() */
45 #define PUT_OBJ_CREATE 0x01
46 #define PUT_OBJ_EXCL 0x02
47 #define PUT_OBJ_CREATE_EXCL (PUT_OBJ_CREATE | PUT_OBJ_EXCL)
48
49 #define RGW_OBJ_NS_MULTIPART "multipart"
50 #define RGW_OBJ_NS_SHADOW "shadow"
51
52 #define RGW_BUCKET_INSTANCE_MD_PREFIX ".bucket.meta."
53
54 #define RGW_NO_SHARD -1
55
56 #define RGW_SHARDS_PRIME_0 7877
57 #define RGW_SHARDS_PRIME_1 65521
58
59 static inline int rgw_shards_mod(unsigned hval, int max_shards)
60 {
61 if (max_shards <= RGW_SHARDS_PRIME_0) {
62 return hval % RGW_SHARDS_PRIME_0 % max_shards;
63 }
64 return hval % RGW_SHARDS_PRIME_1 % max_shards;
65 }
66
67 static inline int rgw_shards_hash(const string& key, int max_shards)
68 {
69 return rgw_shards_mod(ceph_str_hash_linux(key.c_str(), key.size()), max_shards);
70 }
71
72 static inline int rgw_shards_max()
73 {
74 return RGW_SHARDS_PRIME_1;
75 }
76
77 static inline void prepend_bucket_marker(const rgw_bucket& bucket, const string& orig_oid, string& oid)
78 {
79 if (bucket.marker.empty() || orig_oid.empty()) {
80 oid = orig_oid;
81 } else {
82 oid = bucket.marker;
83 oid.append("_");
84 oid.append(orig_oid);
85 }
86 }
87
88 static inline void get_obj_bucket_and_oid_loc(const rgw_obj& obj, string& oid, string& locator)
89 {
90 const rgw_bucket& bucket = obj.bucket;
91 prepend_bucket_marker(bucket, obj.get_oid(), oid);
92 const string& loc = obj.key.get_loc();
93 if (!loc.empty()) {
94 prepend_bucket_marker(bucket, loc, locator);
95 } else {
96 locator.clear();
97 }
98 }
99
100 int rgw_init_ioctx(librados::Rados *rados, const rgw_pool& pool, librados::IoCtx& ioctx, bool create = false);
101
102 int rgw_policy_from_attrset(CephContext *cct, map<string, bufferlist>& attrset, RGWAccessControlPolicy *policy);
103
104 static inline bool rgw_raw_obj_to_obj(const rgw_bucket& bucket, const rgw_raw_obj& raw_obj, rgw_obj *obj)
105 {
106 ssize_t pos = raw_obj.oid.find('_');
107 if (pos < 0) {
108 return false;
109 }
110
111 if (!rgw_obj_key::parse_raw_oid(raw_obj.oid.substr(pos + 1), &obj->key)) {
112 return false;
113 }
114 obj->bucket = bucket;
115
116 return true;
117 }
118
119 struct rgw_bucket_placement {
120 string placement_rule;
121 rgw_bucket bucket;
122
123 void dump(Formatter *f) const;
124 };
125
126 class rgw_obj_select {
127 string placement_rule;
128 rgw_obj obj;
129 rgw_raw_obj raw_obj;
130 bool is_raw;
131
132 public:
133 rgw_obj_select() : is_raw(false) {}
134 rgw_obj_select(const rgw_obj& _obj) : obj(_obj), is_raw(false) {}
135 rgw_obj_select(const rgw_raw_obj& _raw_obj) : raw_obj(_raw_obj), is_raw(true) {}
136 rgw_obj_select(const rgw_obj_select& rhs) {
137 is_raw = rhs.is_raw;
138 if (is_raw) {
139 raw_obj = rhs.raw_obj;
140 } else {
141 obj = rhs.obj;
142 }
143 }
144
145 rgw_raw_obj get_raw_obj(const RGWZoneGroup& zonegroup, const RGWZoneParams& zone_params) const;
146 rgw_raw_obj get_raw_obj(RGWRados *store) const;
147
148 rgw_obj_select& operator=(const rgw_obj& rhs) {
149 obj = rhs;
150 is_raw = false;
151 return *this;
152 }
153
154 rgw_obj_select& operator=(const rgw_raw_obj& rhs) {
155 raw_obj = rhs;
156 is_raw = true;
157 return *this;
158 }
159
160 void set_placement_rule(const string& rule) {
161 placement_rule = rule;
162 }
163 };
164
165 struct compression_block {
166 uint64_t old_ofs;
167 uint64_t new_ofs;
168 uint64_t len;
169
170 void encode(bufferlist& bl) const {
171 ENCODE_START(1, 1, bl);
172 ::encode(old_ofs, bl);
173 ::encode(new_ofs, bl);
174 ::encode(len, bl);
175 ENCODE_FINISH(bl);
176 }
177
178 void decode(bufferlist::iterator& bl) {
179 DECODE_START(1, bl);
180 ::decode(old_ofs, bl);
181 ::decode(new_ofs, bl);
182 ::decode(len, bl);
183 DECODE_FINISH(bl);
184 }
185 };
186 WRITE_CLASS_ENCODER(compression_block)
187
188 struct RGWCompressionInfo {
189 string compression_type;
190 uint64_t orig_size;
191 vector<compression_block> blocks;
192
193 RGWCompressionInfo() : compression_type("none"), orig_size(0) {}
194 RGWCompressionInfo(const RGWCompressionInfo& cs_info) : compression_type(cs_info.compression_type),
195 orig_size(cs_info.orig_size),
196 blocks(cs_info.blocks) {}
197
198 void encode(bufferlist& bl) const {
199 ENCODE_START(1, 1, bl);
200 ::encode(compression_type, bl);
201 ::encode(orig_size, bl);
202 ::encode(blocks, bl);
203 ENCODE_FINISH(bl);
204 }
205
206 void decode(bufferlist::iterator& bl) {
207 DECODE_START(1, bl);
208 ::decode(compression_type, bl);
209 ::decode(orig_size, bl);
210 ::decode(blocks, bl);
211 DECODE_FINISH(bl);
212 }
213 };
214 WRITE_CLASS_ENCODER(RGWCompressionInfo)
215
216 int rgw_compression_info_from_attrset(map<string, bufferlist>& attrs, bool& need_decompress, RGWCompressionInfo& cs_info);
217
218 struct RGWOLHInfo {
219 rgw_obj target;
220 bool removed;
221
222 RGWOLHInfo() : removed(false) {}
223
224 void encode(bufferlist& bl) const {
225 ENCODE_START(1, 1, bl);
226 ::encode(target, bl);
227 ::encode(removed, bl);
228 ENCODE_FINISH(bl);
229 }
230
231 void decode(bufferlist::iterator& bl) {
232 DECODE_START(1, bl);
233 ::decode(target, bl);
234 ::decode(removed, bl);
235 DECODE_FINISH(bl);
236 }
237 static void generate_test_instances(list<RGWOLHInfo*>& o);
238 void dump(Formatter *f) const;
239 };
240 WRITE_CLASS_ENCODER(RGWOLHInfo)
241
242 struct RGWOLHPendingInfo {
243 ceph::real_time time;
244
245 RGWOLHPendingInfo() {}
246
247 void encode(bufferlist& bl) const {
248 ENCODE_START(1, 1, bl);
249 ::encode(time, bl);
250 ENCODE_FINISH(bl);
251 }
252
253 void decode(bufferlist::iterator& bl) {
254 DECODE_START(1, bl);
255 ::decode(time, bl);
256 DECODE_FINISH(bl);
257 }
258
259 void dump(Formatter *f) const;
260 };
261 WRITE_CLASS_ENCODER(RGWOLHPendingInfo)
262
263 struct RGWUsageBatch {
264 map<ceph::real_time, rgw_usage_log_entry> m;
265
266 void insert(ceph::real_time& t, rgw_usage_log_entry& entry, bool *account) {
267 bool exists = m.find(t) != m.end();
268 *account = !exists;
269 m[t].aggregate(entry);
270 }
271 };
272
273 struct RGWUsageIter {
274 string read_iter;
275 uint32_t index;
276
277 RGWUsageIter() : index(0) {}
278 };
279
280 class RGWGetDataCB {
281 protected:
282 uint64_t extra_data_len;
283 public:
284 virtual int handle_data(bufferlist& bl, off_t bl_ofs, off_t bl_len) = 0;
285 RGWGetDataCB() : extra_data_len(0) {}
286 virtual ~RGWGetDataCB() {}
287 virtual void set_extra_data_len(uint64_t len) {
288 extra_data_len = len;
289 }
290 /**
291 * Flushes any cached data. Used by RGWGetObjFilter.
292 * Return logic same as handle_data.
293 */
294 virtual int flush() {
295 return 0;
296 }
297 /**
298 * Allows to extend fetch range of RGW object. Used by RGWGetObjFilter.
299 */
300 virtual int fixup_range(off_t& bl_ofs, off_t& bl_end) {
301 return 0;
302 }
303 };
304
305 class RGWAccessListFilter {
306 public:
307 virtual ~RGWAccessListFilter() {}
308 virtual bool filter(string& name, string& key) = 0;
309 };
310
311 struct RGWCloneRangeInfo {
312 rgw_obj src;
313 off_t src_ofs;
314 off_t dst_ofs;
315 uint64_t len;
316 };
317
318 struct RGWObjManifestPart {
319 rgw_obj loc; /* the object where the data is located */
320 uint64_t loc_ofs; /* the offset at that object where the data is located */
321 uint64_t size; /* the part size */
322
323 RGWObjManifestPart() : loc_ofs(0), size(0) {}
324
325 void encode(bufferlist& bl) const {
326 ENCODE_START(2, 2, bl);
327 ::encode(loc, bl);
328 ::encode(loc_ofs, bl);
329 ::encode(size, bl);
330 ENCODE_FINISH(bl);
331 }
332
333 void decode(bufferlist::iterator& bl) {
334 DECODE_START_LEGACY_COMPAT_LEN_32(2, 2, 2, bl);
335 ::decode(loc, bl);
336 ::decode(loc_ofs, bl);
337 ::decode(size, bl);
338 DECODE_FINISH(bl);
339 }
340
341 void dump(Formatter *f) const;
342 static void generate_test_instances(list<RGWObjManifestPart*>& o);
343 };
344 WRITE_CLASS_ENCODER(RGWObjManifestPart)
345
346 /*
347 The manifest defines a set of rules for structuring the object parts.
348 There are a few terms to note:
349 - head: the head part of the object, which is the part that contains
350 the first chunk of data. An object might not have a head (as in the
351 case of multipart-part objects).
352 - stripe: data portion of a single rgw object that resides on a single
353 rados object.
354 - part: a collection of stripes that make a contiguous part of an
355 object. A regular object will only have one part (although might have
356 many stripes), a multipart object might have many parts. Each part
357 has a fixed stripe size, although the last stripe of a part might
358 be smaller than that. Consecutive parts may be merged if their stripe
359 value is the same.
360 */
361
362 struct RGWObjManifestRule {
363 uint32_t start_part_num;
364 uint64_t start_ofs;
365 uint64_t part_size; /* each part size, 0 if there's no part size, meaning it's unlimited */
366 uint64_t stripe_max_size; /* underlying obj max size */
367 string override_prefix;
368
369 RGWObjManifestRule() : start_part_num(0), start_ofs(0), part_size(0), stripe_max_size(0) {}
370 RGWObjManifestRule(uint32_t _start_part_num, uint64_t _start_ofs, uint64_t _part_size, uint64_t _stripe_max_size) :
371 start_part_num(_start_part_num), start_ofs(_start_ofs), part_size(_part_size), stripe_max_size(_stripe_max_size) {}
372
373 void encode(bufferlist& bl) const {
374 ENCODE_START(2, 1, bl);
375 ::encode(start_part_num, bl);
376 ::encode(start_ofs, bl);
377 ::encode(part_size, bl);
378 ::encode(stripe_max_size, bl);
379 ::encode(override_prefix, bl);
380 ENCODE_FINISH(bl);
381 }
382
383 void decode(bufferlist::iterator& bl) {
384 DECODE_START(2, bl);
385 ::decode(start_part_num, bl);
386 ::decode(start_ofs, bl);
387 ::decode(part_size, bl);
388 ::decode(stripe_max_size, bl);
389 if (struct_v >= 2)
390 ::decode(override_prefix, bl);
391 DECODE_FINISH(bl);
392 }
393 void dump(Formatter *f) const;
394 };
395 WRITE_CLASS_ENCODER(RGWObjManifestRule)
396
397 class RGWObjManifest {
398 protected:
399 bool explicit_objs; /* old manifest? */
400 map<uint64_t, RGWObjManifestPart> objs;
401
402 uint64_t obj_size;
403
404 rgw_obj obj;
405 uint64_t head_size;
406 string head_placement_rule;
407
408 uint64_t max_head_size;
409 string prefix;
410 rgw_bucket_placement tail_placement; /* might be different than the original bucket,
411 as object might have been copied across pools */
412 map<uint64_t, RGWObjManifestRule> rules;
413
414 string tail_instance; /* tail object's instance */
415
416 void convert_to_explicit(const RGWZoneGroup& zonegroup, const RGWZoneParams& zone_params);
417 int append_explicit(RGWObjManifest& m, const RGWZoneGroup& zonegroup, const RGWZoneParams& zone_params);
418 void append_rules(RGWObjManifest& m, map<uint64_t, RGWObjManifestRule>::iterator& iter, string *override_prefix);
419
420 void update_iterators() {
421 begin_iter.seek(0);
422 end_iter.seek(obj_size);
423 }
424 public:
425
426 RGWObjManifest() : explicit_objs(false), obj_size(0), head_size(0), max_head_size(0),
427 begin_iter(this), end_iter(this) {}
428 RGWObjManifest(const RGWObjManifest& rhs) {
429 *this = rhs;
430 }
431 RGWObjManifest& operator=(const RGWObjManifest& rhs) {
432 explicit_objs = rhs.explicit_objs;
433 objs = rhs.objs;
434 obj_size = rhs.obj_size;
435 obj = rhs.obj;
436 head_size = rhs.head_size;
437 max_head_size = rhs.max_head_size;
438 prefix = rhs.prefix;
439 tail_placement = rhs.tail_placement;
440 rules = rhs.rules;
441 tail_instance = rhs.tail_instance;
442
443 begin_iter.set_manifest(this);
444 end_iter.set_manifest(this);
445
446 begin_iter.seek(rhs.begin_iter.get_ofs());
447 end_iter.seek(rhs.end_iter.get_ofs());
448
449 return *this;
450 }
451
452 map<uint64_t, RGWObjManifestPart>& get_explicit_objs() {
453 return objs;
454 }
455
456
457 void set_explicit(uint64_t _size, map<uint64_t, RGWObjManifestPart>& _objs) {
458 explicit_objs = true;
459 obj_size = _size;
460 objs.swap(_objs);
461 }
462
463 void get_implicit_location(uint64_t cur_part_id, uint64_t cur_stripe, uint64_t ofs, string *override_prefix, rgw_obj_select *location);
464
465 void set_trivial_rule(uint64_t tail_ofs, uint64_t stripe_max_size) {
466 RGWObjManifestRule rule(0, tail_ofs, 0, stripe_max_size);
467 rules[0] = rule;
468 max_head_size = tail_ofs;
469 }
470
471 void set_multipart_part_rule(uint64_t stripe_max_size, uint64_t part_num) {
472 RGWObjManifestRule rule(0, 0, 0, stripe_max_size);
473 rule.start_part_num = part_num;
474 rules[0] = rule;
475 max_head_size = 0;
476 }
477
478 void encode(bufferlist& bl) const {
479 ENCODE_START(7, 6, bl);
480 ::encode(obj_size, bl);
481 ::encode(objs, bl);
482 ::encode(explicit_objs, bl);
483 ::encode(obj, bl);
484 ::encode(head_size, bl);
485 ::encode(max_head_size, bl);
486 ::encode(prefix, bl);
487 ::encode(rules, bl);
488 bool encode_tail_bucket = !(tail_placement.bucket == obj.bucket);
489 ::encode(encode_tail_bucket, bl);
490 if (encode_tail_bucket) {
491 ::encode(tail_placement.bucket, bl);
492 }
493 bool encode_tail_instance = (tail_instance != obj.key.instance);
494 ::encode(encode_tail_instance, bl);
495 if (encode_tail_instance) {
496 ::encode(tail_instance, bl);
497 }
498 ::encode(head_placement_rule, bl);
499 ::encode(tail_placement.placement_rule, bl);
500 ENCODE_FINISH(bl);
501 }
502
503 void decode(bufferlist::iterator& bl) {
504 DECODE_START_LEGACY_COMPAT_LEN_32(7, 2, 2, bl);
505 ::decode(obj_size, bl);
506 ::decode(objs, bl);
507 if (struct_v >= 3) {
508 ::decode(explicit_objs, bl);
509 ::decode(obj, bl);
510 ::decode(head_size, bl);
511 ::decode(max_head_size, bl);
512 ::decode(prefix, bl);
513 ::decode(rules, bl);
514 } else {
515 explicit_objs = true;
516 if (!objs.empty()) {
517 map<uint64_t, RGWObjManifestPart>::iterator iter = objs.begin();
518 obj = iter->second.loc;
519 head_size = iter->second.size;
520 max_head_size = head_size;
521 }
522 }
523
524 if (explicit_objs && head_size > 0 && !objs.empty()) {
525 /* patch up manifest due to issue 16435:
526 * the first object in the explicit objs list might not be the one we need to access, use the
527 * head object instead if set. This would happen if we had an old object that was created
528 * when the explicit objs manifest was around, and it got copied.
529 */
530 rgw_obj& obj_0 = objs[0].loc;
531 if (!obj_0.get_oid().empty() && obj_0.key.ns.empty()) {
532 objs[0].loc = obj;
533 objs[0].size = head_size;
534 }
535 }
536
537 if (struct_v >= 4) {
538 if (struct_v < 6) {
539 ::decode(tail_placement.bucket, bl);
540 } else {
541 bool need_to_decode;
542 ::decode(need_to_decode, bl);
543 if (need_to_decode) {
544 ::decode(tail_placement.bucket, bl);
545 } else {
546 tail_placement.bucket = obj.bucket;
547 }
548 }
549 }
550
551 if (struct_v >= 5) {
552 if (struct_v < 6) {
553 ::decode(tail_instance, bl);
554 } else {
555 bool need_to_decode;
556 ::decode(need_to_decode, bl);
557 if (need_to_decode) {
558 ::decode(tail_instance, bl);
559 } else {
560 tail_instance = obj.key.instance;
561 }
562 }
563 } else { // old object created before 'tail_instance' field added to manifest
564 tail_instance = obj.key.instance;
565 }
566
567 if (struct_v >= 7) {
568 ::decode(head_placement_rule, bl);
569 ::decode(tail_placement.placement_rule, bl);
570 }
571
572 update_iterators();
573 DECODE_FINISH(bl);
574 }
575
576 void dump(Formatter *f) const;
577 static void generate_test_instances(list<RGWObjManifest*>& o);
578
579 int append(RGWObjManifest& m, RGWZoneGroup& zonegroup, RGWZoneParams& zone_params);
580 int append(RGWObjManifest& m, RGWRados *store);
581
582 bool get_rule(uint64_t ofs, RGWObjManifestRule *rule);
583
584 bool empty() {
585 if (explicit_objs)
586 return objs.empty();
587 return rules.empty();
588 }
589
590 bool has_explicit_objs() {
591 return explicit_objs;
592 }
593
594 bool has_tail() {
595 if (explicit_objs) {
596 if (objs.size() == 1) {
597 map<uint64_t, RGWObjManifestPart>::iterator iter = objs.begin();
598 rgw_obj& o = iter->second.loc;
599 return !(obj == o);
600 }
601 return (objs.size() >= 2);
602 }
603 return (obj_size > head_size);
604 }
605
606 void set_head(const string& placement_rule, const rgw_obj& _o, uint64_t _s) {
607 head_placement_rule = placement_rule;
608 obj = _o;
609 head_size = _s;
610
611 if (explicit_objs && head_size > 0) {
612 objs[0].loc = obj;
613 objs[0].size = head_size;
614 }
615 }
616
617 const rgw_obj& get_obj() {
618 return obj;
619 }
620
621 void set_tail_placement(const string& placement_rule, const rgw_bucket& _b) {
622 tail_placement.placement_rule = placement_rule;
623 tail_placement.bucket = _b;
624 }
625
626 const rgw_bucket_placement& get_tail_placement() {
627 return tail_placement;
628 }
629
630 const string& get_head_placement_rule() {
631 return head_placement_rule;
632 }
633
634 void set_prefix(const string& _p) {
635 prefix = _p;
636 }
637
638 const string& get_prefix() {
639 return prefix;
640 }
641
642 void set_tail_instance(const string& _ti) {
643 tail_instance = _ti;
644 }
645
646 const string& get_tail_instance() {
647 return tail_instance;
648 }
649
650 void set_head_size(uint64_t _s) {
651 head_size = _s;
652 }
653
654 void set_obj_size(uint64_t s) {
655 obj_size = s;
656
657 update_iterators();
658 }
659
660 uint64_t get_obj_size() {
661 return obj_size;
662 }
663
664 uint64_t get_head_size() {
665 return head_size;
666 }
667
668 void set_max_head_size(uint64_t s) {
669 max_head_size = s;
670 }
671
672 uint64_t get_max_head_size() {
673 return max_head_size;
674 }
675
676 class obj_iterator {
677 RGWObjManifest *manifest;
678 uint64_t part_ofs; /* where current part starts */
679 uint64_t stripe_ofs; /* where current stripe starts */
680 uint64_t ofs; /* current position within the object */
681 uint64_t stripe_size; /* current part size */
682
683 int cur_part_id;
684 int cur_stripe;
685 string cur_override_prefix;
686
687 rgw_obj_select location;
688
689 map<uint64_t, RGWObjManifestRule>::iterator rule_iter;
690 map<uint64_t, RGWObjManifestRule>::iterator next_rule_iter;
691
692 map<uint64_t, RGWObjManifestPart>::iterator explicit_iter;
693
694 void init() {
695 part_ofs = 0;
696 stripe_ofs = 0;
697 ofs = 0;
698 stripe_size = 0;
699 cur_part_id = 0;
700 cur_stripe = 0;
701 }
702
703 void update_explicit_pos();
704
705
706 protected:
707
708 void set_manifest(RGWObjManifest *m) {
709 manifest = m;
710 }
711
712 public:
713 obj_iterator() : manifest(NULL) {
714 init();
715 }
716 explicit obj_iterator(RGWObjManifest *_m) : manifest(_m) {
717 init();
718 if (!manifest->empty()) {
719 seek(0);
720 }
721 }
722 obj_iterator(RGWObjManifest *_m, uint64_t _ofs) : manifest(_m) {
723 init();
724 if (!manifest->empty()) {
725 seek(_ofs);
726 }
727 }
728 void seek(uint64_t ofs);
729
730 void operator++();
731 bool operator==(const obj_iterator& rhs) {
732 return (ofs == rhs.ofs);
733 }
734 bool operator!=(const obj_iterator& rhs) {
735 return (ofs != rhs.ofs);
736 }
737 const rgw_obj_select& get_location() {
738 return location;
739 }
740
741 /* start of current stripe */
742 uint64_t get_stripe_ofs() {
743 if (manifest->explicit_objs) {
744 return explicit_iter->first;
745 }
746 return stripe_ofs;
747 }
748
749 /* current ofs relative to start of rgw object */
750 uint64_t get_ofs() const {
751 return ofs;
752 }
753
754 /* stripe number */
755 int get_cur_stripe() const {
756 return cur_stripe;
757 }
758
759 /* current stripe size */
760 uint64_t get_stripe_size() {
761 if (manifest->explicit_objs) {
762 return explicit_iter->second.size;
763 }
764 return stripe_size;
765 }
766
767 /* offset where data starts within current stripe */
768 uint64_t location_ofs() {
769 if (manifest->explicit_objs) {
770 return explicit_iter->second.loc_ofs;
771 }
772 return 0; /* all stripes start at zero offset */
773 }
774
775 void update_location();
776
777 friend class RGWObjManifest;
778 };
779
780 const obj_iterator& obj_begin();
781 const obj_iterator& obj_end();
782 obj_iterator obj_find(uint64_t ofs);
783
784 obj_iterator begin_iter;
785 obj_iterator end_iter;
786
787 /*
788 * simple object generator. Using a simple single rule manifest.
789 */
790 class generator {
791 RGWObjManifest *manifest;
792 uint64_t last_ofs;
793 uint64_t cur_part_ofs;
794 int cur_part_id;
795 int cur_stripe;
796 uint64_t cur_stripe_size;
797 string cur_oid;
798
799 string oid_prefix;
800
801 rgw_obj_select cur_obj;
802 rgw_pool pool;
803
804
805 RGWObjManifestRule rule;
806
807 public:
808 generator() : manifest(NULL), last_ofs(0), cur_part_ofs(0), cur_part_id(0),
809 cur_stripe(0), cur_stripe_size(0) {}
810 int create_begin(CephContext *cct, RGWObjManifest *manifest, const string& placement_rule, rgw_bucket& bucket, rgw_obj& obj);
811
812 int create_next(uint64_t ofs);
813
814 rgw_raw_obj get_cur_obj(RGWZoneGroup& zonegroup, RGWZoneParams& zone_params) { return cur_obj.get_raw_obj(zonegroup, zone_params); }
815 rgw_raw_obj get_cur_obj(RGWRados *store) { return cur_obj.get_raw_obj(store); }
816
817 /* total max size of current stripe (including head obj) */
818 uint64_t cur_stripe_max_size() {
819 return cur_stripe_size;
820 }
821 };
822 };
823 WRITE_CLASS_ENCODER(RGWObjManifest)
824
825 struct RGWUploadPartInfo {
826 uint32_t num;
827 uint64_t size;
828 uint64_t accounted_size{0};
829 string etag;
830 ceph::real_time modified;
831 RGWObjManifest manifest;
832 RGWCompressionInfo cs_info;
833
834 RGWUploadPartInfo() : num(0), size(0) {}
835
836 void encode(bufferlist& bl) const {
837 ENCODE_START(4, 2, bl);
838 ::encode(num, bl);
839 ::encode(size, bl);
840 ::encode(etag, bl);
841 ::encode(modified, bl);
842 ::encode(manifest, bl);
843 ::encode(cs_info, bl);
844 ::encode(accounted_size, bl);
845 ENCODE_FINISH(bl);
846 }
847 void decode(bufferlist::iterator& bl) {
848 DECODE_START_LEGACY_COMPAT_LEN(4, 2, 2, bl);
849 ::decode(num, bl);
850 ::decode(size, bl);
851 ::decode(etag, bl);
852 ::decode(modified, bl);
853 if (struct_v >= 3)
854 ::decode(manifest, bl);
855 if (struct_v >= 4) {
856 ::decode(cs_info, bl);
857 ::decode(accounted_size, bl);
858 } else {
859 accounted_size = size;
860 }
861 DECODE_FINISH(bl);
862 }
863 void dump(Formatter *f) const;
864 static void generate_test_instances(list<RGWUploadPartInfo*>& o);
865 };
866 WRITE_CLASS_ENCODER(RGWUploadPartInfo)
867
868 struct RGWObjState {
869 rgw_obj obj;
870 bool is_atomic;
871 bool has_attrs;
872 bool exists;
873 uint64_t size; //< size of raw object
874 uint64_t accounted_size{0}; //< size before compression, encryption
875 ceph::real_time mtime;
876 uint64_t epoch;
877 bufferlist obj_tag;
878 string write_tag;
879 bool fake_tag;
880 RGWObjManifest manifest;
881 bool has_manifest;
882 string shadow_obj;
883 bool has_data;
884 bufferlist data;
885 bool prefetch_data;
886 bool keep_tail;
887 bool is_olh;
888 bufferlist olh_tag;
889 uint64_t pg_ver;
890 uint32_t zone_short_id;
891
892 /* important! don't forget to update copy constructor */
893
894 RGWObjVersionTracker objv_tracker;
895
896 map<string, bufferlist> attrset;
897 RGWObjState() : is_atomic(false), has_attrs(0), exists(false),
898 size(0), epoch(0), fake_tag(false), has_manifest(false),
899 has_data(false), prefetch_data(false), keep_tail(false), is_olh(false),
900 pg_ver(0), zone_short_id(0) {}
901 RGWObjState(const RGWObjState& rhs) : obj (rhs.obj) {
902 is_atomic = rhs.is_atomic;
903 has_attrs = rhs.has_attrs;
904 exists = rhs.exists;
905 size = rhs.size;
906 accounted_size = rhs.accounted_size;
907 mtime = rhs.mtime;
908 epoch = rhs.epoch;
909 if (rhs.obj_tag.length()) {
910 obj_tag = rhs.obj_tag;
911 }
912 write_tag = rhs.write_tag;
913 fake_tag = rhs.fake_tag;
914 if (rhs.has_manifest) {
915 manifest = rhs.manifest;
916 }
917 has_manifest = rhs.has_manifest;
918 shadow_obj = rhs.shadow_obj;
919 has_data = rhs.has_data;
920 if (rhs.data.length()) {
921 data = rhs.data;
922 }
923 prefetch_data = rhs.prefetch_data;
924 keep_tail = rhs.keep_tail;
925 is_olh = rhs.is_olh;
926 objv_tracker = rhs.objv_tracker;
927 pg_ver = rhs.pg_ver;
928 }
929
930 bool get_attr(string name, bufferlist& dest) {
931 map<string, bufferlist>::iterator iter = attrset.find(name);
932 if (iter != attrset.end()) {
933 dest = iter->second;
934 return true;
935 }
936 return false;
937 }
938 };
939
940 struct RGWRawObjState {
941 rgw_raw_obj obj;
942 bool has_attrs{false};
943 bool exists{false};
944 uint64_t size{0};
945 ceph::real_time mtime;
946 uint64_t epoch;
947 bufferlist obj_tag;
948 bool has_data{false};
949 bufferlist data;
950 bool prefetch_data{false};
951 uint64_t pg_ver{0};
952
953 /* important! don't forget to update copy constructor */
954
955 RGWObjVersionTracker objv_tracker;
956
957 map<string, bufferlist> attrset;
958 RGWRawObjState() {}
959 RGWRawObjState(const RGWRawObjState& rhs) : obj (rhs.obj) {
960 has_attrs = rhs.has_attrs;
961 exists = rhs.exists;
962 size = rhs.size;
963 mtime = rhs.mtime;
964 epoch = rhs.epoch;
965 if (rhs.obj_tag.length()) {
966 obj_tag = rhs.obj_tag;
967 }
968 has_data = rhs.has_data;
969 if (rhs.data.length()) {
970 data = rhs.data;
971 }
972 prefetch_data = rhs.prefetch_data;
973 pg_ver = rhs.pg_ver;
974 objv_tracker = rhs.objv_tracker;
975 }
976 };
977
978 struct RGWPoolIterCtx {
979 librados::IoCtx io_ctx;
980 librados::NObjectIterator iter;
981 };
982
983 struct RGWListRawObjsCtx {
984 bool initialized;
985 RGWPoolIterCtx iter_ctx;
986
987 RGWListRawObjsCtx() : initialized(false) {}
988 };
989
990 struct RGWDefaultSystemMetaObjInfo {
991 string default_id;
992
993 void encode(bufferlist& bl) const {
994 ENCODE_START(1, 1, bl);
995 ::encode(default_id, bl);
996 ENCODE_FINISH(bl);
997 }
998
999 void decode(bufferlist::iterator& bl) {
1000 DECODE_START(1, bl);
1001 ::decode(default_id, bl);
1002 DECODE_FINISH(bl);
1003 }
1004
1005 void dump(Formatter *f) const;
1006 void decode_json(JSONObj *obj);
1007 };
1008 WRITE_CLASS_ENCODER(RGWDefaultSystemMetaObjInfo)
1009
1010 struct RGWNameToId {
1011 string obj_id;
1012
1013 void encode(bufferlist& bl) const {
1014 ENCODE_START(1, 1, bl);
1015 ::encode(obj_id, bl);
1016 ENCODE_FINISH(bl);
1017 }
1018
1019 void decode(bufferlist::iterator& bl) {
1020 DECODE_START(1, bl);
1021 ::decode(obj_id, bl);
1022 DECODE_FINISH(bl);
1023 }
1024
1025 void dump(Formatter *f) const;
1026 void decode_json(JSONObj *obj);
1027 };
1028 WRITE_CLASS_ENCODER(RGWNameToId)
1029
1030 class RGWSystemMetaObj {
1031 protected:
1032 string id;
1033 string name;
1034
1035 CephContext *cct;
1036 RGWRados *store;
1037
1038 int store_name(bool exclusive);
1039 int store_info(bool exclusive);
1040 int read_info(const string& obj_id, bool old_format = false);
1041 int read_id(const string& obj_name, string& obj_id);
1042 int read_default(RGWDefaultSystemMetaObjInfo& default_info,
1043 const string& oid);
1044 /* read and use default id */
1045 int use_default(bool old_format = false);
1046
1047 public:
1048 RGWSystemMetaObj() : cct(NULL), store(NULL) {}
1049 RGWSystemMetaObj(const string& _name): name(_name), cct(NULL), store(NULL) {}
1050 RGWSystemMetaObj(const string& _id, const string& _name) : id(_id), name(_name), cct(NULL), store(NULL) {}
1051 RGWSystemMetaObj(CephContext *_cct, RGWRados *_store): cct(_cct), store(_store){}
1052 RGWSystemMetaObj(const string& _name, CephContext *_cct, RGWRados *_store): name(_name), cct(_cct), store(_store){}
1053 const string& get_name() const { return name; }
1054 const string& get_id() const { return id; }
1055
1056 void set_name(const string& _name) { name = _name;}
1057 void set_id(const string& _id) { id = _id;}
1058 void clear_id() { id.clear(); }
1059
1060 virtual ~RGWSystemMetaObj() {}
1061
1062 virtual void encode(bufferlist& bl) const {
1063 ENCODE_START(1, 1, bl);
1064 ::encode(id, bl);
1065 ::encode(name, bl);
1066 ENCODE_FINISH(bl);
1067 }
1068
1069 virtual void decode(bufferlist::iterator& bl) {
1070 DECODE_START(1, bl);
1071 ::decode(id, bl);
1072 ::decode(name, bl);
1073 DECODE_FINISH(bl);
1074 }
1075
1076 void reinit_instance(CephContext *_cct, RGWRados *_store) {
1077 cct = _cct;
1078 store = _store;
1079 }
1080 int init(CephContext *_cct, RGWRados *_store, bool setup_obj = true, bool old_format = false);
1081 virtual int read_default_id(string& default_id, bool old_format = false);
1082 virtual int set_as_default(bool exclusive = false);
1083 int delete_default();
1084 virtual int create(bool exclusive = true);
1085 int delete_obj(bool old_format = false);
1086 int rename(const string& new_name);
1087 int update() { return store_info(false);}
1088 int update_name() { return store_name(false);}
1089 int read();
1090 int write(bool exclusive);
1091
1092 virtual rgw_pool get_pool(CephContext *cct) = 0;
1093 virtual const string get_default_oid(bool old_format = false) = 0;
1094 virtual const string& get_names_oid_prefix() = 0;
1095 virtual const string& get_info_oid_prefix(bool old_format = false) = 0;
1096 virtual const string& get_predefined_name(CephContext *cct) = 0;
1097
1098 void dump(Formatter *f) const;
1099 void decode_json(JSONObj *obj);
1100 };
1101 WRITE_CLASS_ENCODER(RGWSystemMetaObj)
1102
1103 struct RGWZonePlacementInfo {
1104 rgw_pool index_pool;
1105 rgw_pool data_pool;
1106 rgw_pool data_extra_pool; /* if not set we should use data_pool */
1107 RGWBucketIndexType index_type;
1108 std::string compression_type;
1109
1110 RGWZonePlacementInfo() : index_type(RGWBIType_Normal) {}
1111
1112 void encode(bufferlist& bl) const {
1113 ENCODE_START(6, 1, bl);
1114 ::encode(index_pool.to_str(), bl);
1115 ::encode(data_pool.to_str(), bl);
1116 ::encode(data_extra_pool.to_str(), bl);
1117 ::encode((uint32_t)index_type, bl);
1118 ::encode(compression_type, bl);
1119 ENCODE_FINISH(bl);
1120 }
1121
1122 void decode(bufferlist::iterator& bl) {
1123 DECODE_START(6, bl);
1124 string index_pool_str;
1125 string data_pool_str;
1126 ::decode(index_pool_str, bl);
1127 index_pool = rgw_pool(index_pool_str);
1128 ::decode(data_pool_str, bl);
1129 data_pool = rgw_pool(data_pool_str);
1130 if (struct_v >= 4) {
1131 string data_extra_pool_str;
1132 ::decode(data_extra_pool_str, bl);
1133 data_extra_pool = rgw_pool(data_extra_pool_str);
1134 }
1135 if (struct_v >= 5) {
1136 uint32_t it;
1137 ::decode(it, bl);
1138 index_type = (RGWBucketIndexType)it;
1139 }
1140 if (struct_v >= 6) {
1141 ::decode(compression_type, bl);
1142 }
1143 DECODE_FINISH(bl);
1144 }
1145 const rgw_pool& get_data_extra_pool() const {
1146 if (data_extra_pool.empty()) {
1147 return data_pool;
1148 }
1149 return data_extra_pool;
1150 }
1151 void dump(Formatter *f) const;
1152 void decode_json(JSONObj *obj);
1153 };
1154 WRITE_CLASS_ENCODER(RGWZonePlacementInfo)
1155
1156 struct RGWZoneParams : RGWSystemMetaObj {
1157 rgw_pool domain_root;
1158 rgw_pool metadata_heap;
1159 rgw_pool control_pool;
1160 rgw_pool gc_pool;
1161 rgw_pool lc_pool;
1162 rgw_pool log_pool;
1163 rgw_pool intent_log_pool;
1164 rgw_pool usage_log_pool;
1165
1166 rgw_pool user_keys_pool;
1167 rgw_pool user_email_pool;
1168 rgw_pool user_swift_pool;
1169 rgw_pool user_uid_pool;
1170 rgw_pool roles_pool;
1171 rgw_pool reshard_pool;
1172
1173 RGWAccessKey system_key;
1174
1175 map<string, RGWZonePlacementInfo> placement_pools;
1176
1177 string realm_id;
1178
1179 map<string, string, ltstr_nocase> tier_config;
1180
1181 RGWZoneParams() : RGWSystemMetaObj() {}
1182 RGWZoneParams(const string& name) : RGWSystemMetaObj(name){}
1183 RGWZoneParams(const string& id, const string& name) : RGWSystemMetaObj(id, name) {}
1184 RGWZoneParams(const string& id, const string& name, const string& _realm_id)
1185 : RGWSystemMetaObj(id, name), realm_id(_realm_id) {}
1186
1187 rgw_pool get_pool(CephContext *cct);
1188 const string get_default_oid(bool old_format = false) override;
1189 const string& get_names_oid_prefix() override;
1190 const string& get_info_oid_prefix(bool old_format = false) override;
1191 const string& get_predefined_name(CephContext *cct) override;
1192
1193 int init(CephContext *_cct, RGWRados *_store, bool setup_obj = true,
1194 bool old_format = false);
1195 using RGWSystemMetaObj::init;
1196 int read_default_id(string& default_id, bool old_format = false) override;
1197 int set_as_default(bool exclusive = false) override;
1198 int create_default(bool old_format = false);
1199 int create(bool exclusive = true) override;
1200 int fix_pool_names();
1201
1202 const string& get_compression_type(const string& placement_rule) const;
1203
1204 void encode(bufferlist& bl) const override {
1205 ENCODE_START(10, 1, bl);
1206 ::encode(domain_root, bl);
1207 ::encode(control_pool, bl);
1208 ::encode(gc_pool, bl);
1209 ::encode(log_pool, bl);
1210 ::encode(intent_log_pool, bl);
1211 ::encode(usage_log_pool, bl);
1212 ::encode(user_keys_pool, bl);
1213 ::encode(user_email_pool, bl);
1214 ::encode(user_swift_pool, bl);
1215 ::encode(user_uid_pool, bl);
1216 RGWSystemMetaObj::encode(bl);
1217 ::encode(system_key, bl);
1218 ::encode(placement_pools, bl);
1219 ::encode(metadata_heap, bl);
1220 ::encode(realm_id, bl);
1221 ::encode(lc_pool, bl);
1222 ::encode(tier_config, bl);
1223 ::encode(roles_pool, bl);
1224 ::encode(reshard_pool, bl);
1225 ENCODE_FINISH(bl);
1226 }
1227
1228 void decode(bufferlist::iterator& bl) override {
1229 DECODE_START(10, bl);
1230 ::decode(domain_root, bl);
1231 ::decode(control_pool, bl);
1232 ::decode(gc_pool, bl);
1233 ::decode(log_pool, bl);
1234 ::decode(intent_log_pool, bl);
1235 ::decode(usage_log_pool, bl);
1236 ::decode(user_keys_pool, bl);
1237 ::decode(user_email_pool, bl);
1238 ::decode(user_swift_pool, bl);
1239 ::decode(user_uid_pool, bl);
1240 if (struct_v >= 6) {
1241 RGWSystemMetaObj::decode(bl);
1242 } else if (struct_v >= 2) {
1243 ::decode(name, bl);
1244 id = name;
1245 }
1246 if (struct_v >= 3)
1247 ::decode(system_key, bl);
1248 if (struct_v >= 4)
1249 ::decode(placement_pools, bl);
1250 if (struct_v >= 5)
1251 ::decode(metadata_heap, bl);
1252 if (struct_v >= 6) {
1253 ::decode(realm_id, bl);
1254 }
1255 if (struct_v >= 7) {
1256 ::decode(lc_pool, bl);
1257 } else {
1258 lc_pool.init(name + ".rgw.lc");
1259 }
1260 if (struct_v >= 8) {
1261 ::decode(tier_config, bl);
1262 }
1263 if (struct_v >= 9) {
1264 ::decode(roles_pool, bl);
1265 } else {
1266 roles_pool = name + ".rgw.roles";
1267 }
1268 if (struct_v >= 10) {
1269 ::decode(reshard_pool, bl);
1270 } else {
1271 reshard_pool = name + ".rgw.reshard";
1272 }
1273 DECODE_FINISH(bl);
1274 }
1275 void dump(Formatter *f) const;
1276 void decode_json(JSONObj *obj);
1277 static void generate_test_instances(list<RGWZoneParams*>& o);
1278
1279 bool find_placement(const rgw_data_placement_target& placement, string *placement_id) {
1280 for (const auto& pp : placement_pools) {
1281 const RGWZonePlacementInfo& info = pp.second;
1282 if (info.index_pool == placement.index_pool.to_str() &&
1283 info.data_pool == placement.data_pool.to_str() &&
1284 info.data_extra_pool == placement.data_extra_pool.to_str()) {
1285 *placement_id = pp.first;
1286 return true;
1287 }
1288 }
1289 return false;
1290 }
1291
1292 bool get_placement(const string& placement_id, RGWZonePlacementInfo *placement) const {
1293 auto iter = placement_pools.find(placement_id);
1294 if (iter == placement_pools.end()) {
1295 return false;
1296 }
1297 *placement = iter->second;
1298 return true;
1299 }
1300
1301 /*
1302 * return data pool of the head object
1303 */
1304 bool get_head_data_pool(const string& placement_id, const rgw_obj& obj, rgw_pool *pool) const {
1305 const rgw_data_placement_target& explicit_placement = obj.bucket.explicit_placement;
1306 if (!explicit_placement.data_pool.empty()) {
1307 if (!obj.in_extra_data) {
1308 *pool = explicit_placement.data_pool;
1309 } else {
1310 *pool = explicit_placement.get_data_extra_pool();
1311 }
1312 return true;
1313 }
1314 if (placement_id.empty()) {
1315 return false;
1316 }
1317 auto iter = placement_pools.find(placement_id);
1318 if (iter == placement_pools.end()) {
1319 return false;
1320 }
1321 if (!obj.in_extra_data) {
1322 *pool = iter->second.data_pool;
1323 } else {
1324 *pool = iter->second.get_data_extra_pool();
1325 }
1326 return true;
1327 }
1328 };
1329 WRITE_CLASS_ENCODER(RGWZoneParams)
1330
1331 struct RGWZone {
1332 string id;
1333 string name;
1334 list<string> endpoints;
1335 bool log_meta;
1336 bool log_data;
1337 bool read_only;
1338 string tier_type;
1339
1340 /**
1341 * Represents the number of shards for the bucket index object, a value of zero
1342 * indicates there is no sharding. By default (no sharding, the name of the object
1343 * is '.dir.{marker}', with sharding, the name is '.dir.{marker}.{sharding_id}',
1344 * sharding_id is zero-based value. It is not recommended to set a too large value
1345 * (e.g. thousand) as it increases the cost for bucket listing.
1346 */
1347 uint32_t bucket_index_max_shards;
1348
1349 bool sync_from_all;
1350 set<string> sync_from; /* list of zones to sync from */
1351
1352 RGWZone() : log_meta(false), log_data(false), read_only(false), bucket_index_max_shards(0),
1353 sync_from_all(true) {}
1354
1355 void encode(bufferlist& bl) const {
1356 ENCODE_START(6, 1, bl);
1357 ::encode(name, bl);
1358 ::encode(endpoints, bl);
1359 ::encode(log_meta, bl);
1360 ::encode(log_data, bl);
1361 ::encode(bucket_index_max_shards, bl);
1362 ::encode(id, bl);
1363 ::encode(read_only, bl);
1364 ::encode(tier_type, bl);
1365 ::encode(sync_from_all, bl);
1366 ::encode(sync_from, bl);
1367 ENCODE_FINISH(bl);
1368 }
1369
1370 void decode(bufferlist::iterator& bl) {
1371 DECODE_START(6, bl);
1372 ::decode(name, bl);
1373 if (struct_v < 4) {
1374 id = name;
1375 }
1376 ::decode(endpoints, bl);
1377 if (struct_v >= 2) {
1378 ::decode(log_meta, bl);
1379 ::decode(log_data, bl);
1380 }
1381 if (struct_v >= 3) {
1382 ::decode(bucket_index_max_shards, bl);
1383 }
1384 if (struct_v >= 4) {
1385 ::decode(id, bl);
1386 ::decode(read_only, bl);
1387 }
1388 if (struct_v >= 5) {
1389 ::decode(tier_type, bl);
1390 }
1391 if (struct_v >= 6) {
1392 ::decode(sync_from_all, bl);
1393 ::decode(sync_from, bl);
1394 }
1395 DECODE_FINISH(bl);
1396 }
1397 void dump(Formatter *f) const;
1398 void decode_json(JSONObj *obj);
1399 static void generate_test_instances(list<RGWZone*>& o);
1400
1401 bool is_read_only() { return read_only; }
1402
1403 bool syncs_from(const string& zone_id) {
1404 return (sync_from_all || sync_from.find(zone_id) != sync_from.end());
1405 }
1406 };
1407 WRITE_CLASS_ENCODER(RGWZone)
1408
1409 struct RGWDefaultZoneGroupInfo {
1410 string default_zonegroup;
1411
1412 void encode(bufferlist& bl) const {
1413 ENCODE_START(1, 1, bl);
1414 ::encode(default_zonegroup, bl);
1415 ENCODE_FINISH(bl);
1416 }
1417
1418 void decode(bufferlist::iterator& bl) {
1419 DECODE_START(1, bl);
1420 ::decode(default_zonegroup, bl);
1421 DECODE_FINISH(bl);
1422 }
1423 void dump(Formatter *f) const;
1424 void decode_json(JSONObj *obj);
1425 //todo: implement ceph-dencoder
1426 };
1427 WRITE_CLASS_ENCODER(RGWDefaultZoneGroupInfo)
1428
1429 struct RGWZoneGroupPlacementTarget {
1430 string name;
1431 set<string> tags;
1432
1433 bool user_permitted(list<string>& user_tags) {
1434 if (tags.empty()) {
1435 return true;
1436 }
1437 for (auto& rule : user_tags) {
1438 if (tags.find(rule) != tags.end()) {
1439 return true;
1440 }
1441 }
1442 return false;
1443 }
1444
1445 void encode(bufferlist& bl) const {
1446 ENCODE_START(1, 1, bl);
1447 ::encode(name, bl);
1448 ::encode(tags, bl);
1449 ENCODE_FINISH(bl);
1450 }
1451
1452 void decode(bufferlist::iterator& bl) {
1453 DECODE_START(1, bl);
1454 ::decode(name, bl);
1455 ::decode(tags, bl);
1456 DECODE_FINISH(bl);
1457 }
1458 void dump(Formatter *f) const;
1459 void decode_json(JSONObj *obj);
1460 };
1461 WRITE_CLASS_ENCODER(RGWZoneGroupPlacementTarget)
1462
1463
1464 struct RGWZoneGroup : public RGWSystemMetaObj {
1465 string api_name;
1466 list<string> endpoints;
1467 bool is_master;
1468
1469 string master_zone;
1470 map<string, RGWZone> zones;
1471
1472 map<string, RGWZoneGroupPlacementTarget> placement_targets;
1473 string default_placement;
1474
1475 list<string> hostnames;
1476 list<string> hostnames_s3website;
1477 // TODO: Maybe convert hostnames to a map<string,list<string>> for
1478 // endpoint_type->hostnames
1479 /*
1480 20:05 < _robbat21irssi> maybe I do someting like: if (hostname_map.empty()) { populate all map keys from hostnames; };
1481 20:05 < _robbat21irssi> but that's a later compatability migration planning bit
1482 20:06 < yehudasa> more like if (!hostnames.empty()) {
1483 20:06 < yehudasa> for (list<string>::iterator iter = hostnames.begin(); iter != hostnames.end(); ++iter) {
1484 20:06 < yehudasa> hostname_map["s3"].append(iter->second);
1485 20:07 < yehudasa> hostname_map["s3website"].append(iter->second);
1486 20:07 < yehudasa> s/append/push_back/g
1487 20:08 < _robbat21irssi> inner loop over APIs
1488 20:08 < yehudasa> yeah, probably
1489 20:08 < _robbat21irssi> s3, s3website, swift, swith_auth, swift_website
1490 */
1491 map<string, list<string> > api_hostname_map;
1492 map<string, list<string> > api_endpoints_map;
1493
1494 string realm_id;
1495
1496 RGWZoneGroup(): is_master(false){}
1497 RGWZoneGroup(const std::string &id, const std::string &name):RGWSystemMetaObj(id, name) {}
1498 RGWZoneGroup(const std::string &_name):RGWSystemMetaObj(_name) {}
1499 RGWZoneGroup(const std::string &_name, bool _is_master, CephContext *cct, RGWRados* store,
1500 const string& _realm_id, const list<string>& _endpoints)
1501 : RGWSystemMetaObj(_name, cct , store), endpoints(_endpoints), is_master(_is_master),
1502 realm_id(_realm_id) {}
1503
1504 bool is_master_zonegroup() const { return is_master;}
1505 void update_master(bool _is_master) {
1506 is_master = _is_master;
1507 post_process_params();
1508 }
1509 void post_process_params();
1510
1511 void encode(bufferlist& bl) const override {
1512 ENCODE_START(4, 1, bl);
1513 ::encode(name, bl);
1514 ::encode(api_name, bl);
1515 ::encode(is_master, bl);
1516 ::encode(endpoints, bl);
1517 ::encode(master_zone, bl);
1518 ::encode(zones, bl);
1519 ::encode(placement_targets, bl);
1520 ::encode(default_placement, bl);
1521 ::encode(hostnames, bl);
1522 ::encode(hostnames_s3website, bl);
1523 RGWSystemMetaObj::encode(bl);
1524 ::encode(realm_id, bl);
1525 ENCODE_FINISH(bl);
1526 }
1527
1528 void decode(bufferlist::iterator& bl) override {
1529 DECODE_START(4, bl);
1530 ::decode(name, bl);
1531 ::decode(api_name, bl);
1532 ::decode(is_master, bl);
1533 ::decode(endpoints, bl);
1534 ::decode(master_zone, bl);
1535 ::decode(zones, bl);
1536 ::decode(placement_targets, bl);
1537 ::decode(default_placement, bl);
1538 if (struct_v >= 2) {
1539 ::decode(hostnames, bl);
1540 }
1541 if (struct_v >= 3) {
1542 ::decode(hostnames_s3website, bl);
1543 }
1544 if (struct_v >= 4) {
1545 RGWSystemMetaObj::decode(bl);
1546 ::decode(realm_id, bl);
1547 } else {
1548 id = name;
1549 }
1550 DECODE_FINISH(bl);
1551 }
1552
1553 int read_default_id(string& default_id, bool old_format = false) override;
1554 int set_as_default(bool exclusive = false) override;
1555 int create_default(bool old_format = false);
1556 int equals(const string& other_zonegroup) const;
1557 int add_zone(const RGWZoneParams& zone_params, bool *is_master, bool *read_only,
1558 const list<string>& endpoints, const string *ptier_type,
1559 bool *psync_from_all, list<string>& sync_from, list<string>& sync_from_rm);
1560 int remove_zone(const std::string& zone_id);
1561 int rename_zone(const RGWZoneParams& zone_params);
1562 rgw_pool get_pool(CephContext *cct);
1563 const string get_default_oid(bool old_region_format = false) override;
1564 const string& get_info_oid_prefix(bool old_region_format = false) override;
1565 const string& get_names_oid_prefix() override;
1566 const string& get_predefined_name(CephContext *cct) override;
1567
1568 void dump(Formatter *f) const;
1569 void decode_json(JSONObj *obj);
1570 static void generate_test_instances(list<RGWZoneGroup*>& o);
1571 };
1572 WRITE_CLASS_ENCODER(RGWZoneGroup)
1573
1574 struct RGWPeriodMap
1575 {
1576 string id;
1577 map<string, RGWZoneGroup> zonegroups;
1578 map<string, RGWZoneGroup> zonegroups_by_api;
1579 map<string, uint32_t> short_zone_ids;
1580
1581 string master_zonegroup;
1582
1583 void encode(bufferlist& bl) const;
1584 void decode(bufferlist::iterator& bl);
1585
1586 int update(const RGWZoneGroup& zonegroup, CephContext *cct);
1587
1588 void dump(Formatter *f) const;
1589 void decode_json(JSONObj *obj);
1590
1591 void reset() {
1592 zonegroups.clear();
1593 zonegroups_by_api.clear();
1594 master_zonegroup.clear();
1595 }
1596
1597 uint32_t get_zone_short_id(const string& zone_id) const;
1598 };
1599 WRITE_CLASS_ENCODER(RGWPeriodMap)
1600
1601 struct RGWPeriodConfig
1602 {
1603 RGWQuotaInfo bucket_quota;
1604 RGWQuotaInfo user_quota;
1605
1606 void encode(bufferlist& bl) const {
1607 ENCODE_START(1, 1, bl);
1608 ::encode(bucket_quota, bl);
1609 ::encode(user_quota, bl);
1610 ENCODE_FINISH(bl);
1611 }
1612
1613 void decode(bufferlist::iterator& bl) {
1614 DECODE_START(1, bl);
1615 ::decode(bucket_quota, bl);
1616 ::decode(user_quota, bl);
1617 DECODE_FINISH(bl);
1618 }
1619
1620 void dump(Formatter *f) const;
1621 void decode_json(JSONObj *obj);
1622
1623 // the period config must be stored in a local object outside of the period,
1624 // so that it can be used in a default configuration where no realm/period
1625 // exists
1626 int read(RGWRados *store, const std::string& realm_id);
1627 int write(RGWRados *store, const std::string& realm_id);
1628
1629 static std::string get_oid(const std::string& realm_id);
1630 static rgw_pool get_pool(CephContext *cct);
1631 };
1632 WRITE_CLASS_ENCODER(RGWPeriodConfig)
1633
1634 /* for backward comaptability */
1635 struct RGWRegionMap {
1636
1637 map<string, RGWZoneGroup> regions;
1638
1639 string master_region;
1640
1641 RGWQuotaInfo bucket_quota;
1642 RGWQuotaInfo user_quota;
1643
1644 void encode(bufferlist& bl) const;
1645 void decode(bufferlist::iterator& bl);
1646
1647 void dump(Formatter *f) const;
1648 void decode_json(JSONObj *obj);
1649 };
1650 WRITE_CLASS_ENCODER(RGWRegionMap)
1651
1652 struct RGWZoneGroupMap {
1653
1654 map<string, RGWZoneGroup> zonegroups;
1655 map<string, RGWZoneGroup> zonegroups_by_api;
1656
1657 string master_zonegroup;
1658
1659 RGWQuotaInfo bucket_quota;
1660 RGWQuotaInfo user_quota;
1661
1662 /* constract the map */
1663 int read(CephContext *cct, RGWRados *store);
1664
1665 void encode(bufferlist& bl) const;
1666 void decode(bufferlist::iterator& bl);
1667
1668 void dump(Formatter *f) const;
1669 void decode_json(JSONObj *obj);
1670 };
1671 WRITE_CLASS_ENCODER(RGWZoneGroupMap)
1672
1673 class RGWRealm;
1674
1675 struct objexp_hint_entry {
1676 string tenant;
1677 string bucket_name;
1678 string bucket_id;
1679 rgw_obj_key obj_key;
1680 ceph::real_time exp_time;
1681
1682 void encode(bufferlist& bl) const {
1683 ENCODE_START(2, 1, bl);
1684 ::encode(bucket_name, bl);
1685 ::encode(bucket_id, bl);
1686 ::encode(obj_key, bl);
1687 ::encode(exp_time, bl);
1688 ::encode(tenant, bl);
1689 ENCODE_FINISH(bl);
1690 }
1691
1692 void decode(bufferlist::iterator& bl) {
1693 // XXX Do we want DECODE_START_LEGACY_COMPAT_LEN(2, 1, 1, bl); ?
1694 DECODE_START(2, bl);
1695 ::decode(bucket_name, bl);
1696 ::decode(bucket_id, bl);
1697 ::decode(obj_key, bl);
1698 ::decode(exp_time, bl);
1699 if (struct_v >= 2) {
1700 ::decode(tenant, bl);
1701 } else {
1702 tenant.clear();
1703 }
1704 DECODE_FINISH(bl);
1705 }
1706 };
1707 WRITE_CLASS_ENCODER(objexp_hint_entry)
1708
1709 class RGWPeriod;
1710
1711 class RGWRealm : public RGWSystemMetaObj
1712 {
1713 string current_period;
1714 epoch_t epoch{0}; //< realm epoch, incremented for each new period
1715
1716 int create_control(bool exclusive);
1717 int delete_control();
1718 public:
1719 RGWRealm() {}
1720 RGWRealm(const string& _id, const string& _name = "") : RGWSystemMetaObj(_id, _name) {}
1721 RGWRealm(CephContext *_cct, RGWRados *_store): RGWSystemMetaObj(_cct, _store) {}
1722 RGWRealm(const string& _name, CephContext *_cct, RGWRados *_store): RGWSystemMetaObj(_name, _cct, _store){}
1723
1724 void encode(bufferlist& bl) const override {
1725 ENCODE_START(1, 1, bl);
1726 RGWSystemMetaObj::encode(bl);
1727 ::encode(current_period, bl);
1728 ::encode(epoch, bl);
1729 ENCODE_FINISH(bl);
1730 }
1731
1732 void decode(bufferlist::iterator& bl) override {
1733 DECODE_START(1, bl);
1734 RGWSystemMetaObj::decode(bl);
1735 ::decode(current_period, bl);
1736 ::decode(epoch, bl);
1737 DECODE_FINISH(bl);
1738 }
1739
1740 int create(bool exclusive = true) override;
1741 int delete_obj();
1742 rgw_pool get_pool(CephContext *cct);
1743 const string get_default_oid(bool old_format = false) override;
1744 const string& get_names_oid_prefix() override;
1745 const string& get_info_oid_prefix(bool old_format = false) override;
1746 const string& get_predefined_name(CephContext *cct) override;
1747
1748 using RGWSystemMetaObj::read_id; // expose as public for radosgw-admin
1749
1750 void dump(Formatter *f) const;
1751 void decode_json(JSONObj *obj);
1752
1753 const string& get_current_period() const {
1754 return current_period;
1755 }
1756 int set_current_period(RGWPeriod& period);
1757 void clear_current_period_and_epoch() {
1758 current_period.clear();
1759 epoch = 0;
1760 }
1761 epoch_t get_epoch() const { return epoch; }
1762
1763 string get_control_oid();
1764 /// send a notify on the realm control object
1765 int notify_zone(bufferlist& bl);
1766 /// notify the zone of a new period
1767 int notify_new_period(const RGWPeriod& period);
1768 };
1769 WRITE_CLASS_ENCODER(RGWRealm)
1770
1771 struct RGWPeriodLatestEpochInfo {
1772 epoch_t epoch;
1773
1774 void encode(bufferlist& bl) const {
1775 ENCODE_START(1, 1, bl);
1776 ::encode(epoch, bl);
1777 ENCODE_FINISH(bl);
1778 }
1779
1780 void decode(bufferlist::iterator& bl) {
1781 DECODE_START(1, bl);
1782 ::decode(epoch, bl);
1783 DECODE_FINISH(bl);
1784 }
1785
1786 void dump(Formatter *f) const;
1787 void decode_json(JSONObj *obj);
1788 };
1789 WRITE_CLASS_ENCODER(RGWPeriodLatestEpochInfo)
1790
1791 class RGWPeriod
1792 {
1793 string id;
1794 epoch_t epoch;
1795 string predecessor_uuid;
1796 std::vector<std::string> sync_status;
1797 RGWPeriodMap period_map;
1798 RGWPeriodConfig period_config;
1799 string master_zonegroup;
1800 string master_zone;
1801
1802 string realm_id;
1803 string realm_name;
1804 epoch_t realm_epoch{1}; //< realm epoch when period was made current
1805
1806 CephContext *cct;
1807 RGWRados *store;
1808
1809 int read_info();
1810 int read_latest_epoch(RGWPeriodLatestEpochInfo& epoch_info);
1811 int use_latest_epoch();
1812 int use_current_period();
1813
1814 const string get_period_oid();
1815 const string get_period_oid_prefix();
1816
1817 // gather the metadata sync status for each shard; only for use on master zone
1818 int update_sync_status(const RGWPeriod &current_period,
1819 std::ostream& error_stream, bool force_if_stale);
1820
1821 public:
1822 RGWPeriod() : epoch(0), cct(NULL), store(NULL) {}
1823
1824 RGWPeriod(const string& period_id, epoch_t _epoch = 0)
1825 : id(period_id), epoch(_epoch),
1826 cct(NULL), store(NULL) {}
1827
1828 const string& get_id() const { return id; }
1829 epoch_t get_epoch() const { return epoch; }
1830 epoch_t get_realm_epoch() const { return realm_epoch; }
1831 const string& get_predecessor() const { return predecessor_uuid; }
1832 const string& get_master_zone() const { return master_zone; }
1833 const string& get_master_zonegroup() const { return master_zonegroup; }
1834 const string& get_realm() const { return realm_id; }
1835 const RGWPeriodMap& get_map() const { return period_map; }
1836 RGWPeriodConfig& get_config() { return period_config; }
1837 const RGWPeriodConfig& get_config() const { return period_config; }
1838 const std::vector<std::string>& get_sync_status() const { return sync_status; }
1839 rgw_pool get_pool(CephContext *cct);
1840 const string& get_latest_epoch_oid();
1841 const string& get_info_oid_prefix();
1842
1843 void set_user_quota(RGWQuotaInfo& user_quota) {
1844 period_config.user_quota = user_quota;
1845 }
1846
1847 void set_bucket_quota(RGWQuotaInfo& bucket_quota) {
1848 period_config.bucket_quota = bucket_quota;
1849 }
1850
1851 void set_id(const string& id) {
1852 this->id = id;
1853 period_map.id = id;
1854 }
1855 void set_epoch(epoch_t epoch) { this->epoch = epoch; }
1856 void set_realm_epoch(epoch_t epoch) { realm_epoch = epoch; }
1857
1858 void set_predecessor(const string& predecessor)
1859 {
1860 predecessor_uuid = predecessor;
1861 }
1862
1863 void set_realm_id(const string& _realm_id) {
1864 realm_id = _realm_id;
1865 }
1866
1867 int reflect();
1868
1869 int get_zonegroup(RGWZoneGroup& zonegroup,
1870 const string& zonegroup_id);
1871
1872 bool is_single_zonegroup(CephContext *cct, RGWRados *store);
1873
1874 int get_latest_epoch(epoch_t& epoch);
1875 int set_latest_epoch(epoch_t epoch, bool exclusive = false);
1876
1877 int init(CephContext *_cct, RGWRados *_store, const string &period_realm_id, const string &period_realm_name = "",
1878 bool setup_obj = true);
1879 int init(CephContext *_cct, RGWRados *_store, bool setup_obj = true);
1880 int use_next_epoch();
1881
1882 int create(bool exclusive = true);
1883 int delete_obj();
1884 int store_info(bool exclusive);
1885 int add_zonegroup(const RGWZoneGroup& zonegroup);
1886
1887 void fork();
1888 int update();
1889
1890 // commit a staging period; only for use on master zone
1891 int commit(RGWRealm& realm, const RGWPeriod &current_period,
1892 std::ostream& error_stream, bool force_if_stale = false);
1893
1894 void encode(bufferlist& bl) const {
1895 ENCODE_START(1, 1, bl);
1896 ::encode(id, bl);
1897 ::encode(epoch, bl);
1898 ::encode(realm_epoch, bl);
1899 ::encode(predecessor_uuid, bl);
1900 ::encode(sync_status, bl);
1901 ::encode(period_map, bl);
1902 ::encode(master_zone, bl);
1903 ::encode(master_zonegroup, bl);
1904 ::encode(period_config, bl);
1905 ::encode(realm_id, bl);
1906 ::encode(realm_name, bl);
1907 ENCODE_FINISH(bl);
1908 }
1909
1910 void decode(bufferlist::iterator& bl) {
1911 DECODE_START(1, bl);
1912 ::decode(id, bl);
1913 ::decode(epoch, bl);
1914 ::decode(realm_epoch, bl);
1915 ::decode(predecessor_uuid, bl);
1916 ::decode(sync_status, bl);
1917 ::decode(period_map, bl);
1918 ::decode(master_zone, bl);
1919 ::decode(master_zonegroup, bl);
1920 ::decode(period_config, bl);
1921 ::decode(realm_id, bl);
1922 ::decode(realm_name, bl);
1923 DECODE_FINISH(bl);
1924 }
1925 void dump(Formatter *f) const;
1926 void decode_json(JSONObj *obj);
1927
1928 static string get_staging_id(const string& realm_id) {
1929 return realm_id + ":staging";
1930 }
1931 };
1932 WRITE_CLASS_ENCODER(RGWPeriod)
1933
1934 class RGWDataChangesLog;
1935 class RGWMetaSyncStatusManager;
1936 class RGWDataSyncStatusManager;
1937 class RGWReplicaLogger;
1938 class RGWCoroutinesManagerRegistry;
1939
1940 class RGWStateLog {
1941 RGWRados *store;
1942 int num_shards;
1943 string module_name;
1944
1945 void oid_str(int shard, string& oid);
1946 int get_shard_num(const string& object);
1947 string get_oid(const string& object);
1948 int open_ioctx(librados::IoCtx& ioctx);
1949
1950 struct list_state {
1951 int cur_shard;
1952 int max_shard;
1953 string marker;
1954 string client_id;
1955 string op_id;
1956 string object;
1957
1958 list_state() : cur_shard(0), max_shard(0) {}
1959 };
1960
1961 protected:
1962 virtual bool dump_entry_internal(const cls_statelog_entry& entry, Formatter *f) {
1963 return false;
1964 }
1965
1966 public:
1967 RGWStateLog(RGWRados *_store, int _num_shards, const string& _module_name) :
1968 store(_store), num_shards(_num_shards), module_name(_module_name) {}
1969 virtual ~RGWStateLog() {}
1970
1971 int store_entry(const string& client_id, const string& op_id, const string& object,
1972 uint32_t state, bufferlist *bl, uint32_t *check_state);
1973
1974 int remove_entry(const string& client_id, const string& op_id, const string& object);
1975
1976 void init_list_entries(const string& client_id, const string& op_id, const string& object,
1977 void **handle);
1978
1979 int list_entries(void *handle, int max_entries, list<cls_statelog_entry>& entries, bool *done);
1980
1981 void finish_list_entries(void *handle);
1982
1983 virtual void dump_entry(const cls_statelog_entry& entry, Formatter *f);
1984 };
1985
1986 /*
1987 * state transitions:
1988 *
1989 * unknown -> in-progress -> complete
1990 * -> error
1991 *
1992 * user can try setting the 'abort' state, and it can only succeed if state is
1993 * in-progress.
1994 *
1995 * state renewal cannot switch state (stays in the same state)
1996 *
1997 * rgw can switch from in-progress to complete
1998 * rgw can switch from in-progress to error
1999 *
2000 * rgw can switch from abort to cancelled
2001 *
2002 */
2003
2004 class RGWOpState : public RGWStateLog {
2005 protected:
2006 bool dump_entry_internal(const cls_statelog_entry& entry, Formatter *f) override;
2007 public:
2008
2009 enum OpState {
2010 OPSTATE_UNKNOWN = 0,
2011 OPSTATE_IN_PROGRESS = 1,
2012 OPSTATE_COMPLETE = 2,
2013 OPSTATE_ERROR = 3,
2014 OPSTATE_ABORT = 4,
2015 OPSTATE_CANCELLED = 5,
2016 };
2017
2018 explicit RGWOpState(RGWRados *_store);
2019
2020 int state_from_str(const string& s, OpState *state);
2021 int set_state(const string& client_id, const string& op_id, const string& object, OpState state);
2022 int renew_state(const string& client_id, const string& op_id, const string& object, OpState state);
2023 };
2024
2025 class RGWOpStateSingleOp
2026 {
2027 RGWOpState os;
2028 string client_id;
2029 string op_id;
2030 string object;
2031
2032 CephContext *cct;
2033
2034 RGWOpState::OpState cur_state;
2035 ceph::real_time last_update;
2036
2037 public:
2038 RGWOpStateSingleOp(RGWRados *store, const string& cid, const string& oid, const string& obj);
2039
2040 int set_state(RGWOpState::OpState state);
2041 int renew_state();
2042 };
2043
2044 class RGWGetBucketStats_CB : public RefCountedObject {
2045 protected:
2046 rgw_bucket bucket;
2047 map<RGWObjCategory, RGWStorageStats> *stats;
2048 public:
2049 explicit RGWGetBucketStats_CB(rgw_bucket& _bucket) : bucket(_bucket), stats(NULL) {}
2050 ~RGWGetBucketStats_CB() override {}
2051 virtual void handle_response(int r) = 0;
2052 virtual void set_response(map<RGWObjCategory, RGWStorageStats> *_stats) {
2053 stats = _stats;
2054 }
2055 };
2056
2057 class RGWGetUserStats_CB : public RefCountedObject {
2058 protected:
2059 rgw_user user;
2060 RGWStorageStats stats;
2061 public:
2062 explicit RGWGetUserStats_CB(const rgw_user& _user) : user(_user) {}
2063 ~RGWGetUserStats_CB() override {}
2064 virtual void handle_response(int r) = 0;
2065 virtual void set_response(RGWStorageStats& _stats) {
2066 stats = _stats;
2067 }
2068 };
2069
2070 class RGWGetDirHeader_CB;
2071 class RGWGetUserHeader_CB;
2072
2073 struct rgw_rados_ref {
2074 rgw_pool pool;
2075 string oid;
2076 string key;
2077 librados::IoCtx ioctx;
2078 };
2079
2080 class RGWChainedCache {
2081 public:
2082 virtual ~RGWChainedCache() {}
2083 virtual void chain_cb(const string& key, void *data) = 0;
2084 virtual void invalidate(const string& key) = 0;
2085 virtual void invalidate_all() = 0;
2086
2087 struct Entry {
2088 RGWChainedCache *cache;
2089 const string& key;
2090 void *data;
2091
2092 Entry(RGWChainedCache *_c, const string& _k, void *_d) : cache(_c), key(_k), data(_d) {}
2093 };
2094 };
2095
2096 template <class T, class S>
2097 class RGWObjectCtxImpl {
2098 RGWRados *store;
2099 std::map<T, S> objs_state;
2100 RWLock lock;
2101
2102 public:
2103 RGWObjectCtxImpl(RGWRados *_store) : store(_store), lock("RGWObjectCtxImpl") {}
2104
2105 S *get_state(const T& obj) {
2106 S *result;
2107 typename std::map<T, S>::iterator iter;
2108 lock.get_read();
2109 assert (!obj.empty());
2110 iter = objs_state.find(obj);
2111 if (iter != objs_state.end()) {
2112 result = &iter->second;
2113 lock.unlock();
2114 } else {
2115 lock.unlock();
2116 lock.get_write();
2117 result = &objs_state[obj];
2118 lock.unlock();
2119 }
2120 return result;
2121 }
2122
2123 void set_atomic(T& obj) {
2124 RWLock::WLocker wl(lock);
2125 assert (!obj.empty());
2126 objs_state[obj].is_atomic = true;
2127 }
2128 void set_prefetch_data(T& obj) {
2129 RWLock::WLocker wl(lock);
2130 assert (!obj.empty());
2131 objs_state[obj].prefetch_data = true;
2132 }
2133 void invalidate(T& obj) {
2134 RWLock::WLocker wl(lock);
2135 auto iter = objs_state.find(obj);
2136 if (iter == objs_state.end()) {
2137 return;
2138 }
2139 bool is_atomic = iter->second.is_atomic;
2140 bool prefetch_data = iter->second.prefetch_data;
2141
2142 objs_state.erase(iter);
2143
2144 if (is_atomic || prefetch_data) {
2145 auto& s = objs_state[obj];
2146 s.is_atomic = is_atomic;
2147 s.prefetch_data = prefetch_data;
2148 }
2149 }
2150 };
2151
2152 template<>
2153 void RGWObjectCtxImpl<rgw_obj, RGWObjState>::invalidate(rgw_obj& obj);
2154
2155 template<>
2156 void RGWObjectCtxImpl<rgw_raw_obj, RGWRawObjState>::invalidate(rgw_raw_obj& obj);
2157
2158 struct RGWObjectCtx {
2159 RGWRados *store;
2160 void *user_ctx;
2161
2162 RGWObjectCtxImpl<rgw_obj, RGWObjState> obj;
2163 RGWObjectCtxImpl<rgw_raw_obj, RGWRawObjState> raw;
2164
2165 explicit RGWObjectCtx(RGWRados *_store) : store(_store), user_ctx(NULL), obj(store), raw(store) { }
2166 RGWObjectCtx(RGWRados *_store, void *_user_ctx) : store(_store), user_ctx(_user_ctx), obj(store), raw(store) { }
2167 };
2168
2169 class Finisher;
2170 class RGWAsyncRadosProcessor;
2171
2172 template <class T>
2173 class RGWChainedCacheImpl;
2174
2175 struct bucket_info_entry {
2176 RGWBucketInfo info;
2177 real_time mtime;
2178 map<string, bufferlist> attrs;
2179 };
2180
2181 struct tombstone_entry {
2182 ceph::real_time mtime;
2183 uint32_t zone_short_id;
2184 uint64_t pg_ver;
2185
2186 tombstone_entry() = default;
2187 tombstone_entry(const RGWObjState& state)
2188 : mtime(state.mtime), zone_short_id(state.zone_short_id),
2189 pg_ver(state.pg_ver) {}
2190 };
2191
2192 class RGWIndexCompletionManager;
2193
2194 class RGWRados
2195 {
2196 friend class RGWGC;
2197 friend class RGWMetaNotifier;
2198 friend class RGWDataNotifier;
2199 friend class RGWLC;
2200 friend class RGWObjectExpirer;
2201 friend class RGWMetaSyncProcessorThread;
2202 friend class RGWDataSyncProcessorThread;
2203 friend class RGWStateLog;
2204 friend class RGWReplicaLogger;
2205 friend class RGWReshard;
2206 friend class RGWBucketReshard;
2207 friend class BucketIndexLockGuard;
2208
2209 /** Open the pool used as root for this gateway */
2210 int open_root_pool_ctx();
2211 int open_gc_pool_ctx();
2212 int open_lc_pool_ctx();
2213 int open_objexp_pool_ctx();
2214 int open_reshard_pool_ctx();
2215
2216 int open_pool_ctx(const rgw_pool& pool, librados::IoCtx& io_ctx);
2217 int open_bucket_index_ctx(const RGWBucketInfo& bucket_info, librados::IoCtx& index_ctx);
2218 int open_bucket_index(const RGWBucketInfo& bucket_info, librados::IoCtx& index_ctx, string& bucket_oid);
2219 int open_bucket_index_base(const RGWBucketInfo& bucket_info, librados::IoCtx& index_ctx,
2220 string& bucket_oid_base);
2221 int open_bucket_index_shard(const RGWBucketInfo& bucket_info, librados::IoCtx& index_ctx,
2222 const string& obj_key, string *bucket_obj, int *shard_id);
2223 int open_bucket_index_shard(const RGWBucketInfo& bucket_info, librados::IoCtx& index_ctx,
2224 int shard_id, string *bucket_obj);
2225 int open_bucket_index(const RGWBucketInfo& bucket_info, librados::IoCtx& index_ctx,
2226 map<int, string>& bucket_objs, int shard_id = -1, map<int, string> *bucket_instance_ids = NULL);
2227 template<typename T>
2228 int open_bucket_index(const RGWBucketInfo& bucket_info, librados::IoCtx& index_ctx,
2229 map<int, string>& oids, map<int, T>& bucket_objs,
2230 int shard_id = -1, map<int, string> *bucket_instance_ids = NULL);
2231 void build_bucket_index_marker(const string& shard_id_str, const string& shard_marker,
2232 string *marker);
2233
2234 void get_bucket_instance_ids(const RGWBucketInfo& bucket_info, int shard_id, map<int, string> *result);
2235
2236 std::atomic<int64_t> max_req_id = { 0 };
2237 Mutex lock;
2238 Mutex watchers_lock;
2239 SafeTimer *timer;
2240
2241 RGWGC *gc;
2242 RGWLC *lc;
2243 RGWObjectExpirer *obj_expirer;
2244 bool use_gc_thread;
2245 bool use_lc_thread;
2246 bool quota_threads;
2247 bool run_sync_thread;
2248 bool run_reshard_thread;
2249
2250 RGWAsyncRadosProcessor* async_rados;
2251
2252 RGWMetaNotifier *meta_notifier;
2253 RGWDataNotifier *data_notifier;
2254 RGWMetaSyncProcessorThread *meta_sync_processor_thread;
2255 map<string, RGWDataSyncProcessorThread *> data_sync_processor_threads;
2256
2257 RGWSyncLogTrimThread *sync_log_trimmer{nullptr};
2258
2259 Mutex meta_sync_thread_lock;
2260 Mutex data_sync_thread_lock;
2261
2262 int num_watchers;
2263 RGWWatcher **watchers;
2264 std::set<int> watchers_set;
2265 librados::IoCtx root_pool_ctx; // .rgw
2266 librados::IoCtx control_pool_ctx; // .rgw.control
2267 bool watch_initialized;
2268
2269 friend class RGWWatcher;
2270
2271 Mutex bucket_id_lock;
2272
2273 // This field represents the number of bucket index object shards
2274 uint32_t bucket_index_max_shards;
2275
2276 int get_obj_head_ioctx(const RGWBucketInfo& bucket_info, const rgw_obj& obj, librados::IoCtx *ioctx);
2277 int get_obj_head_ref(const RGWBucketInfo& bucket_info, const rgw_obj& obj, rgw_rados_ref *ref);
2278 int get_system_obj_ref(const rgw_raw_obj& obj, rgw_rados_ref *ref, rgw_pool *pool = NULL);
2279 uint64_t max_bucket_id;
2280
2281 int get_olh_target_state(RGWObjectCtx& rctx, const RGWBucketInfo& bucket_info, const rgw_obj& obj,
2282 RGWObjState *olh_state, RGWObjState **target_state);
2283 int get_system_obj_state_impl(RGWObjectCtx *rctx, rgw_raw_obj& obj, RGWRawObjState **state, RGWObjVersionTracker *objv_tracker);
2284 int get_obj_state_impl(RGWObjectCtx *rctx, const RGWBucketInfo& bucket_info, const rgw_obj& obj, RGWObjState **state,
2285 bool follow_olh, bool assume_noent = false);
2286 int append_atomic_test(RGWObjectCtx *rctx, const RGWBucketInfo& bucket_info, const rgw_obj& obj,
2287 librados::ObjectOperation& op, RGWObjState **state);
2288
2289 int update_placement_map();
2290 int store_bucket_info(RGWBucketInfo& info, map<string, bufferlist> *pattrs, RGWObjVersionTracker *objv_tracker, bool exclusive);
2291
2292 void remove_rgw_head_obj(librados::ObjectWriteOperation& op);
2293 void cls_obj_check_prefix_exist(librados::ObjectOperation& op, const string& prefix, bool fail_if_exist);
2294 void cls_obj_check_mtime(librados::ObjectOperation& op, const real_time& mtime, bool high_precision_time, RGWCheckMTimeType type);
2295 protected:
2296 CephContext *cct;
2297
2298 std::vector<librados::Rados> rados;
2299 uint32_t next_rados_handle;
2300 RWLock handle_lock;
2301 std::map<pthread_t, int> rados_map;
2302
2303 using RGWChainedCacheImpl_bucket_info_entry = RGWChainedCacheImpl<bucket_info_entry>;
2304 RGWChainedCacheImpl_bucket_info_entry *binfo_cache;
2305
2306 using tombstone_cache_t = lru_map<rgw_obj, tombstone_entry>;
2307 tombstone_cache_t *obj_tombstone_cache;
2308
2309 librados::IoCtx gc_pool_ctx; // .rgw.gc
2310 librados::IoCtx lc_pool_ctx; // .rgw.lc
2311 librados::IoCtx objexp_pool_ctx;
2312 librados::IoCtx reshard_pool_ctx;
2313
2314 bool pools_initialized;
2315
2316 string zonegroup_id;
2317 string zone_name;
2318 string trans_id_suffix;
2319
2320 RGWQuotaHandler *quota_handler;
2321
2322 Finisher *finisher;
2323
2324 RGWCoroutinesManagerRegistry *cr_registry;
2325
2326 RGWSyncModulesManager *sync_modules_manager{nullptr};
2327 RGWSyncModuleInstanceRef sync_module;
2328 bool writeable_zone{false};
2329
2330 RGWZoneGroup zonegroup;
2331 RGWZone zone_public_config; /* external zone params, e.g., entrypoints, log flags, etc. */
2332 RGWZoneParams zone_params; /* internal zone params, e.g., rados pools */
2333 uint32_t zone_short_id;
2334
2335 RGWPeriod current_period;
2336
2337 RGWIndexCompletionManager *index_completion_manager{nullptr};
2338 public:
2339 RGWRados() : lock("rados_timer_lock"), watchers_lock("watchers_lock"), timer(NULL),
2340 gc(NULL), lc(NULL), obj_expirer(NULL), use_gc_thread(false), use_lc_thread(false), quota_threads(false),
2341 run_sync_thread(false), run_reshard_thread(false), async_rados(nullptr), meta_notifier(NULL),
2342 data_notifier(NULL), meta_sync_processor_thread(NULL),
2343 meta_sync_thread_lock("meta_sync_thread_lock"), data_sync_thread_lock("data_sync_thread_lock"),
2344 num_watchers(0), watchers(NULL),
2345 watch_initialized(false),
2346 bucket_id_lock("rados_bucket_id"),
2347 bucket_index_max_shards(0),
2348 max_bucket_id(0), cct(NULL),
2349 next_rados_handle(0),
2350 handle_lock("rados_handle_lock"),
2351 binfo_cache(NULL), obj_tombstone_cache(nullptr),
2352 pools_initialized(false),
2353 quota_handler(NULL),
2354 finisher(NULL),
2355 cr_registry(NULL),
2356 zone_short_id(0),
2357 rest_master_conn(NULL),
2358 meta_mgr(NULL), data_log(NULL), reshard(NULL) {}
2359
2360 uint64_t get_new_req_id() {
2361 return ++max_req_id;
2362 }
2363
2364 librados::IoCtx* get_lc_pool_ctx() {
2365 return &lc_pool_ctx;
2366 }
2367 void set_context(CephContext *_cct) {
2368 cct = _cct;
2369 }
2370
2371 /**
2372 * AmazonS3 errors contain a HostId string, but is an opaque base64 blob; we
2373 * try to be more transparent. This has a wrapper so we can update it when zonegroup/zone are changed.
2374 */
2375 void init_host_id() {
2376 /* uint64_t needs 16, two '-' separators and a trailing null */
2377 const string& zone_name = get_zone().name;
2378 const string& zonegroup_name = zonegroup.get_name();
2379 char charbuf[16 + zone_name.size() + zonegroup_name.size() + 2 + 1];
2380 snprintf(charbuf, sizeof(charbuf), "%llx-%s-%s", (unsigned long long)instance_id(), zone_name.c_str(), zonegroup_name.c_str());
2381 string s(charbuf);
2382 host_id = s;
2383 }
2384
2385 string host_id;
2386
2387 RGWRealm realm;
2388
2389 RGWRESTConn *rest_master_conn;
2390 map<string, RGWRESTConn *> zone_conn_map;
2391 map<string, RGWRESTConn *> zone_data_sync_from_map;
2392 map<string, RGWRESTConn *> zone_data_notify_to_map;
2393 map<string, RGWRESTConn *> zonegroup_conn_map;
2394
2395 map<string, string> zone_id_by_name;
2396 map<string, RGWZone> zone_by_id;
2397
2398 RGWRESTConn *get_zone_conn_by_id(const string& id) {
2399 auto citer = zone_conn_map.find(id);
2400 if (citer == zone_conn_map.end()) {
2401 return NULL;
2402 }
2403
2404 return citer->second;
2405 }
2406
2407 RGWRESTConn *get_zone_conn_by_name(const string& name) {
2408 auto i = zone_id_by_name.find(name);
2409 if (i == zone_id_by_name.end()) {
2410 return NULL;
2411 }
2412
2413 return get_zone_conn_by_id(i->second);
2414 }
2415
2416 bool find_zone_id_by_name(const string& name, string *id) {
2417 auto i = zone_id_by_name.find(name);
2418 if (i == zone_id_by_name.end()) {
2419 return false;
2420 }
2421 *id = i->second;
2422 return true;
2423 }
2424
2425 int get_zonegroup(const string& id, RGWZoneGroup& zonegroup) {
2426 int ret = 0;
2427 if (id == get_zonegroup().get_id()) {
2428 zonegroup = get_zonegroup();
2429 } else if (!current_period.get_id().empty()) {
2430 ret = current_period.get_zonegroup(zonegroup, id);
2431 }
2432 return ret;
2433 }
2434
2435 RGWRealm& get_realm() {
2436 return realm;
2437 }
2438
2439 RGWZoneParams& get_zone_params() { return zone_params; }
2440 RGWZoneGroup& get_zonegroup() {
2441 return zonegroup;
2442 }
2443 RGWZone& get_zone() {
2444 return zone_public_config;
2445 }
2446
2447 bool zone_is_writeable() {
2448 return writeable_zone && !get_zone().is_read_only();
2449 }
2450
2451 uint32_t get_zone_short_id() const {
2452 return zone_short_id;
2453 }
2454
2455 bool zone_syncs_from(RGWZone& target_zone, RGWZone& source_zone);
2456
2457 const RGWQuotaInfo& get_bucket_quota() {
2458 return current_period.get_config().bucket_quota;
2459 }
2460
2461 const RGWQuotaInfo& get_user_quota() {
2462 return current_period.get_config().user_quota;
2463 }
2464
2465 const string& get_current_period_id() {
2466 return current_period.get_id();
2467 }
2468
2469 bool has_zonegroup_api(const std::string& api) const {
2470 if (!current_period.get_id().empty()) {
2471 const auto& zonegroups_by_api = current_period.get_map().zonegroups_by_api;
2472 if (zonegroups_by_api.find(api) != zonegroups_by_api.end())
2473 return true;
2474 }
2475 return false;
2476 }
2477
2478 // pulls missing periods for period_history
2479 std::unique_ptr<RGWPeriodPuller> period_puller;
2480 // maintains a connected history of periods
2481 std::unique_ptr<RGWPeriodHistory> period_history;
2482
2483 RGWAsyncRadosProcessor* get_async_rados() const { return async_rados; };
2484
2485 RGWMetadataManager *meta_mgr;
2486
2487 RGWDataChangesLog *data_log;
2488
2489 RGWReshard *reshard;
2490 std::shared_ptr<RGWReshardWait> reshard_wait;
2491
2492 virtual ~RGWRados() = default;
2493
2494 tombstone_cache_t *get_tombstone_cache() {
2495 return obj_tombstone_cache;
2496 }
2497
2498 RGWSyncModulesManager *get_sync_modules_manager() {
2499 return sync_modules_manager;
2500 }
2501 const RGWSyncModuleInstanceRef& get_sync_module() {
2502 return sync_module;
2503 }
2504
2505 int get_required_alignment(const rgw_pool& pool, uint64_t *alignment);
2506 int get_max_chunk_size(const rgw_pool& pool, uint64_t *max_chunk_size);
2507 int get_max_chunk_size(const string& placement_rule, const rgw_obj& obj, uint64_t *max_chunk_size);
2508
2509 uint32_t get_max_bucket_shards() {
2510 return rgw_shards_max();
2511 }
2512
2513 int get_raw_obj_ref(const rgw_raw_obj& obj, rgw_rados_ref *ref, rgw_pool *pool = NULL);
2514
2515 int list_raw_objects(const rgw_pool& pool, const string& prefix_filter, int max,
2516 RGWListRawObjsCtx& ctx, list<string>& oids,
2517 bool *is_truncated);
2518
2519 int list_raw_prefixed_objs(const rgw_pool& pool, const string& prefix, list<string>& result);
2520 int list_zonegroups(list<string>& zonegroups);
2521 int list_regions(list<string>& regions);
2522 int list_zones(list<string>& zones);
2523 int list_realms(list<string>& realms);
2524 int list_periods(list<string>& periods);
2525 int list_periods(const string& current_period, list<string>& periods);
2526 void tick();
2527
2528 CephContext *ctx() { return cct; }
2529 /** do all necessary setup of the storage device */
2530 int initialize(CephContext *_cct, bool _use_gc_thread, bool _use_lc_thread, bool _quota_threads, bool _run_sync_thread, bool _run_reshard_thread) {
2531 set_context(_cct);
2532 use_gc_thread = _use_gc_thread;
2533 use_lc_thread = _use_lc_thread;
2534 quota_threads = _quota_threads;
2535 run_sync_thread = _run_sync_thread;
2536 run_reshard_thread = _run_reshard_thread;
2537 return initialize();
2538 }
2539 /** Initialize the RADOS instance and prepare to do other ops */
2540 virtual int init_rados();
2541 int init_zg_from_period(bool *initialized);
2542 int init_zg_from_local(bool *creating_defaults);
2543 int init_complete();
2544 int replace_region_with_zonegroup();
2545 int convert_regionmap();
2546 int initialize();
2547 void finalize();
2548
2549 void schedule_context(Context *c);
2550
2551 /** set up a bucket listing. handle is filled in. */
2552 int list_buckets_init(RGWAccessHandle *handle);
2553 /**
2554 * get the next bucket in the listing. obj is filled in,
2555 * handle is updated.
2556 */
2557 int list_buckets_next(rgw_bucket_dir_entry& obj, RGWAccessHandle *handle);
2558
2559 /// list logs
2560 int log_list_init(const string& prefix, RGWAccessHandle *handle);
2561 int log_list_next(RGWAccessHandle handle, string *name);
2562
2563 /// remove log
2564 int log_remove(const string& name);
2565
2566 /// show log
2567 int log_show_init(const string& name, RGWAccessHandle *handle);
2568 int log_show_next(RGWAccessHandle handle, rgw_log_entry *entry);
2569
2570 // log bandwidth info
2571 int log_usage(map<rgw_user_bucket, RGWUsageBatch>& usage_info);
2572 int read_usage(const rgw_user& user, uint64_t start_epoch, uint64_t end_epoch, uint32_t max_entries,
2573 bool *is_truncated, RGWUsageIter& read_iter, map<rgw_user_bucket, rgw_usage_log_entry>& usage);
2574 int trim_usage(rgw_user& user, uint64_t start_epoch, uint64_t end_epoch);
2575
2576 int create_pool(const rgw_pool& pool);
2577
2578 /**
2579 * create a bucket with name bucket and the given list of attrs
2580 * returns 0 on success, -ERR# otherwise.
2581 */
2582 int init_bucket_index(RGWBucketInfo& bucket_info, int num_shards);
2583 int select_bucket_placement(RGWUserInfo& user_info, const string& zonegroup_id, const string& rule,
2584 string *pselected_rule_name, RGWZonePlacementInfo *rule_info);
2585 int select_legacy_bucket_placement(RGWZonePlacementInfo *rule_info);
2586 int select_new_bucket_location(RGWUserInfo& user_info, const string& zonegroup_id, const string& rule,
2587 string *pselected_rule_name, RGWZonePlacementInfo *rule_info);
2588 int select_bucket_location_by_rule(const string& location_rule, RGWZonePlacementInfo *rule_info);
2589 void create_bucket_id(string *bucket_id);
2590
2591 bool get_obj_data_pool(const string& placement_rule, const rgw_obj& obj, rgw_pool *pool);
2592 bool obj_to_raw(const string& placement_rule, const rgw_obj& obj, rgw_raw_obj *raw_obj);
2593
2594 int create_bucket(RGWUserInfo& owner, rgw_bucket& bucket,
2595 const string& zonegroup_id,
2596 const string& placement_rule,
2597 const string& swift_ver_location,
2598 const RGWQuotaInfo * pquota_info,
2599 map<std::string,bufferlist>& attrs,
2600 RGWBucketInfo& bucket_info,
2601 obj_version *pobjv,
2602 obj_version *pep_objv,
2603 ceph::real_time creation_time,
2604 rgw_bucket *master_bucket,
2605 uint32_t *master_num_shards,
2606 bool exclusive = true);
2607 int add_bucket_placement(const rgw_pool& new_pool);
2608 int remove_bucket_placement(const rgw_pool& new_pool);
2609 int list_placement_set(set<rgw_pool>& names);
2610 int create_pools(vector<rgw_pool>& pools, vector<int>& retcodes);
2611
2612 RGWCoroutinesManagerRegistry *get_cr_registry() { return cr_registry; }
2613
2614 class SystemObject {
2615 RGWRados *store;
2616 RGWObjectCtx& ctx;
2617 rgw_raw_obj obj;
2618
2619 RGWObjState *state;
2620
2621 protected:
2622 int get_state(RGWRawObjState **pstate, RGWObjVersionTracker *objv_tracker);
2623
2624 public:
2625 SystemObject(RGWRados *_store, RGWObjectCtx& _ctx, rgw_raw_obj& _obj) : store(_store), ctx(_ctx), obj(_obj), state(NULL) {}
2626
2627 void invalidate_state();
2628
2629 RGWRados *get_store() { return store; }
2630 rgw_raw_obj& get_obj() { return obj; }
2631 RGWObjectCtx& get_ctx() { return ctx; }
2632
2633 struct Read {
2634 RGWRados::SystemObject *source;
2635
2636 struct GetObjState {
2637 rgw_rados_ref ref;
2638 bool has_ref{false};
2639 uint64_t last_ver{0};
2640
2641 GetObjState() {}
2642
2643 int get_ref(RGWRados *store, rgw_raw_obj& obj, rgw_rados_ref **pref);
2644 } state;
2645
2646 struct StatParams {
2647 ceph::real_time *lastmod;
2648 uint64_t *obj_size;
2649 map<string, bufferlist> *attrs;
2650
2651 StatParams() : lastmod(NULL), obj_size(NULL), attrs(NULL) {}
2652 } stat_params;
2653
2654 struct ReadParams {
2655 rgw_cache_entry_info *cache_info;
2656 map<string, bufferlist> *attrs;
2657
2658 ReadParams() : attrs(NULL) {}
2659 } read_params;
2660
2661 explicit Read(RGWRados::SystemObject *_source) : source(_source) {}
2662
2663 int stat(RGWObjVersionTracker *objv_tracker);
2664 int read(int64_t ofs, int64_t end, bufferlist& bl, RGWObjVersionTracker *objv_tracker);
2665 int get_attr(const char *name, bufferlist& dest);
2666 };
2667 };
2668
2669 struct BucketShard {
2670 RGWRados *store;
2671 rgw_bucket bucket;
2672 int shard_id;
2673 librados::IoCtx index_ctx;
2674 string bucket_obj;
2675
2676 explicit BucketShard(RGWRados *_store) : store(_store), shard_id(-1) {}
2677 int init(const rgw_bucket& _bucket, const rgw_obj& obj);
2678 int init(const rgw_bucket& _bucket, int sid);
2679 };
2680
2681 class Object {
2682 RGWRados *store;
2683 RGWBucketInfo bucket_info;
2684 RGWObjectCtx& ctx;
2685 rgw_obj obj;
2686
2687 BucketShard bs;
2688
2689 RGWObjState *state;
2690
2691 bool versioning_disabled;
2692
2693 bool bs_initialized;
2694
2695 protected:
2696 int get_state(RGWObjState **pstate, bool follow_olh, bool assume_noent = false);
2697 void invalidate_state();
2698
2699 int prepare_atomic_modification(librados::ObjectWriteOperation& op, bool reset_obj, const string *ptag,
2700 const char *ifmatch, const char *ifnomatch, bool removal_op);
2701 int complete_atomic_modification();
2702
2703 public:
2704 Object(RGWRados *_store, const RGWBucketInfo& _bucket_info, RGWObjectCtx& _ctx, const rgw_obj& _obj) : store(_store), bucket_info(_bucket_info),
2705 ctx(_ctx), obj(_obj), bs(store),
2706 state(NULL), versioning_disabled(false),
2707 bs_initialized(false) {}
2708
2709 RGWRados *get_store() { return store; }
2710 rgw_obj& get_obj() { return obj; }
2711 RGWObjectCtx& get_ctx() { return ctx; }
2712 RGWBucketInfo& get_bucket_info() { return bucket_info; }
2713 int get_manifest(RGWObjManifest **pmanifest);
2714
2715 int get_bucket_shard(BucketShard **pbs) {
2716 if (!bs_initialized) {
2717 int r = bs.init(bucket_info.bucket, obj);
2718 if (r < 0) {
2719 return r;
2720 }
2721 bs_initialized = true;
2722 }
2723 *pbs = &bs;
2724 return 0;
2725 }
2726
2727 void set_versioning_disabled(bool status) {
2728 versioning_disabled = status;
2729 }
2730
2731 bool versioning_enabled() {
2732 return (!versioning_disabled && bucket_info.versioning_enabled());
2733 }
2734
2735 struct Read {
2736 RGWRados::Object *source;
2737
2738 struct GetObjState {
2739 librados::IoCtx io_ctx;
2740 rgw_obj obj;
2741 rgw_raw_obj head_obj;
2742 } state;
2743
2744 struct ConditionParams {
2745 const ceph::real_time *mod_ptr;
2746 const ceph::real_time *unmod_ptr;
2747 bool high_precision_time;
2748 uint32_t mod_zone_id;
2749 uint64_t mod_pg_ver;
2750 const char *if_match;
2751 const char *if_nomatch;
2752
2753 ConditionParams() :
2754 mod_ptr(NULL), unmod_ptr(NULL), high_precision_time(false), mod_zone_id(0), mod_pg_ver(0),
2755 if_match(NULL), if_nomatch(NULL) {}
2756 } conds;
2757
2758 struct Params {
2759 ceph::real_time *lastmod;
2760 uint64_t *obj_size;
2761 map<string, bufferlist> *attrs;
2762
2763 Params() : lastmod(NULL), obj_size(NULL), attrs(NULL) {}
2764 } params;
2765
2766 explicit Read(RGWRados::Object *_source) : source(_source) {}
2767
2768 int prepare();
2769 static int range_to_ofs(uint64_t obj_size, int64_t &ofs, int64_t &end);
2770 int read(int64_t ofs, int64_t end, bufferlist& bl);
2771 int iterate(int64_t ofs, int64_t end, RGWGetDataCB *cb);
2772 int get_attr(const char *name, bufferlist& dest);
2773 };
2774
2775 struct Write {
2776 RGWRados::Object *target;
2777
2778 struct MetaParams {
2779 ceph::real_time *mtime;
2780 map<std::string, bufferlist>* rmattrs;
2781 const bufferlist *data;
2782 RGWObjManifest *manifest;
2783 const string *ptag;
2784 list<rgw_obj_index_key> *remove_objs;
2785 ceph::real_time set_mtime;
2786 rgw_user owner;
2787 RGWObjCategory category;
2788 int flags;
2789 const char *if_match;
2790 const char *if_nomatch;
2791 uint64_t olh_epoch;
2792 ceph::real_time delete_at;
2793 bool canceled;
2794 const string *user_data;
2795 rgw_zone_set *zones_trace;
2796
2797 MetaParams() : mtime(NULL), rmattrs(NULL), data(NULL), manifest(NULL), ptag(NULL),
2798 remove_objs(NULL), category(RGW_OBJ_CATEGORY_MAIN), flags(0),
2799 if_match(NULL), if_nomatch(NULL), olh_epoch(0), canceled(false), user_data(nullptr), zones_trace(nullptr) {}
2800 } meta;
2801
2802 explicit Write(RGWRados::Object *_target) : target(_target) {}
2803
2804 int _do_write_meta(uint64_t size, uint64_t accounted_size,
2805 map<std::string, bufferlist>& attrs,
2806 bool assume_noent,
2807 void *index_op);
2808 int write_meta(uint64_t size, uint64_t accounted_size,
2809 map<std::string, bufferlist>& attrs);
2810 int write_data(const char *data, uint64_t ofs, uint64_t len, bool exclusive);
2811 };
2812
2813 struct Delete {
2814 RGWRados::Object *target;
2815
2816 struct DeleteParams {
2817 rgw_user bucket_owner;
2818 int versioning_status;
2819 ACLOwner obj_owner; /* needed for creation of deletion marker */
2820 uint64_t olh_epoch;
2821 string marker_version_id;
2822 uint32_t bilog_flags;
2823 list<rgw_obj_index_key> *remove_objs;
2824 ceph::real_time expiration_time;
2825 ceph::real_time unmod_since;
2826 ceph::real_time mtime; /* for setting delete marker mtime */
2827 bool high_precision_time;
2828 rgw_zone_set *zones_trace;
2829
2830 DeleteParams() : versioning_status(0), olh_epoch(0), bilog_flags(0), remove_objs(NULL), high_precision_time(false), zones_trace(nullptr) {}
2831 } params;
2832
2833 struct DeleteResult {
2834 bool delete_marker;
2835 string version_id;
2836
2837 DeleteResult() : delete_marker(false) {}
2838 } result;
2839
2840 explicit Delete(RGWRados::Object *_target) : target(_target) {}
2841
2842 int delete_obj();
2843 };
2844
2845 struct Stat {
2846 RGWRados::Object *source;
2847
2848 struct Result {
2849 rgw_obj obj;
2850 RGWObjManifest manifest;
2851 bool has_manifest;
2852 uint64_t size;
2853 struct timespec mtime;
2854 map<string, bufferlist> attrs;
2855
2856 Result() : has_manifest(false), size(0) {}
2857 } result;
2858
2859 struct State {
2860 librados::IoCtx io_ctx;
2861 librados::AioCompletion *completion;
2862 int ret;
2863
2864 State() : completion(NULL), ret(0) {}
2865 } state;
2866
2867
2868 explicit Stat(RGWRados::Object *_source) : source(_source) {}
2869
2870 int stat_async();
2871 int wait();
2872 int stat();
2873 private:
2874 int finish();
2875 };
2876 };
2877
2878 class Bucket {
2879 RGWRados *store;
2880 RGWBucketInfo bucket_info;
2881 rgw_bucket& bucket;
2882 int shard_id;
2883
2884 public:
2885 Bucket(RGWRados *_store, const RGWBucketInfo& _bucket_info) : store(_store), bucket_info(_bucket_info), bucket(bucket_info.bucket),
2886 shard_id(RGW_NO_SHARD) {}
2887 RGWRados *get_store() { return store; }
2888 rgw_bucket& get_bucket() { return bucket; }
2889 RGWBucketInfo& get_bucket_info() { return bucket_info; }
2890
2891 int update_bucket_id(const string& new_bucket_id);
2892
2893 int get_shard_id() { return shard_id; }
2894 void set_shard_id(int id) {
2895 shard_id = id;
2896 }
2897
2898 class UpdateIndex {
2899 RGWRados::Bucket *target;
2900 string optag;
2901 rgw_obj obj;
2902 uint16_t bilog_flags{0};
2903 BucketShard bs;
2904 bool bs_initialized{false};
2905 bool blind;
2906 bool prepared{false};
2907 rgw_zone_set *zones_trace{nullptr};
2908
2909 int init_bs() {
2910 int r = bs.init(target->get_bucket(), obj);
2911 if (r < 0) {
2912 return r;
2913 }
2914 bs_initialized = true;
2915 return 0;
2916 }
2917
2918 void invalidate_bs() {
2919 bs_initialized = false;
2920 }
2921
2922 int guard_reshard(BucketShard **pbs, std::function<int(BucketShard *)> call);
2923 public:
2924
2925 UpdateIndex(RGWRados::Bucket *_target, const rgw_obj& _obj) : target(_target), obj(_obj),
2926 bs(target->get_store()) {
2927 blind = (target->get_bucket_info().index_type == RGWBIType_Indexless);
2928 }
2929
2930 int get_bucket_shard(BucketShard **pbs) {
2931 if (!bs_initialized) {
2932 int r = init_bs();
2933 if (r < 0) {
2934 return r;
2935 }
2936 }
2937 *pbs = &bs;
2938 return 0;
2939 }
2940
2941 void set_bilog_flags(uint16_t flags) {
2942 bilog_flags = flags;
2943 }
2944
2945 void set_zones_trace(rgw_zone_set *_zones_trace) {
2946 zones_trace = _zones_trace;
2947 }
2948
2949 int prepare(RGWModifyOp, const string *write_tag);
2950 int complete(int64_t poolid, uint64_t epoch, uint64_t size,
2951 uint64_t accounted_size, ceph::real_time& ut,
2952 const string& etag, const string& content_type,
2953 bufferlist *acl_bl, RGWObjCategory category,
2954 list<rgw_obj_index_key> *remove_objs, const string *user_data = nullptr);
2955 int complete_del(int64_t poolid, uint64_t epoch,
2956 ceph::real_time& removed_mtime, /* mtime of removed object */
2957 list<rgw_obj_index_key> *remove_objs);
2958 int cancel();
2959
2960 const string *get_optag() { return &optag; }
2961
2962 bool is_prepared() { return prepared; }
2963 };
2964
2965 struct List {
2966 RGWRados::Bucket *target;
2967 rgw_obj_key next_marker;
2968
2969 struct Params {
2970 string prefix;
2971 string delim;
2972 rgw_obj_key marker;
2973 rgw_obj_key end_marker;
2974 string ns;
2975 bool enforce_ns;
2976 RGWAccessListFilter *filter;
2977 bool list_versions;
2978
2979 Params() : enforce_ns(true), filter(NULL), list_versions(false) {}
2980 } params;
2981
2982 public:
2983 explicit List(RGWRados::Bucket *_target) : target(_target) {}
2984
2985 int list_objects(int max, vector<rgw_bucket_dir_entry> *result, map<string, bool> *common_prefixes, bool *is_truncated);
2986 rgw_obj_key& get_next_marker() {
2987 return next_marker;
2988 }
2989 };
2990 };
2991
2992 /** Write/overwrite an object to the bucket storage. */
2993 virtual int put_system_obj_impl(rgw_raw_obj& obj, uint64_t size, ceph::real_time *mtime,
2994 map<std::string, bufferlist>& attrs, int flags,
2995 bufferlist& data,
2996 RGWObjVersionTracker *objv_tracker,
2997 ceph::real_time set_mtime /* 0 for don't set */);
2998
2999 virtual int put_system_obj_data(void *ctx, rgw_raw_obj& obj, bufferlist& bl,
3000 off_t ofs, bool exclusive,
3001 RGWObjVersionTracker *objv_tracker = nullptr);
3002 int aio_put_obj_data(void *ctx, rgw_raw_obj& obj, bufferlist& bl,
3003 off_t ofs, bool exclusive, void **handle);
3004
3005 int put_system_obj(void *ctx, rgw_raw_obj& obj, const char *data, size_t len, bool exclusive,
3006 ceph::real_time *mtime, map<std::string, bufferlist>& attrs, RGWObjVersionTracker *objv_tracker,
3007 ceph::real_time set_mtime) {
3008 bufferlist bl;
3009 bl.append(data, len);
3010 int flags = PUT_OBJ_CREATE;
3011 if (exclusive)
3012 flags |= PUT_OBJ_EXCL;
3013
3014 return put_system_obj_impl(obj, len, mtime, attrs, flags, bl, objv_tracker, set_mtime);
3015 }
3016 int aio_wait(void *handle);
3017 bool aio_completed(void *handle);
3018
3019 int on_last_entry_in_listing(RGWBucketInfo& bucket_info,
3020 const std::string& obj_prefix,
3021 const std::string& obj_delim,
3022 std::function<int(const rgw_bucket_dir_entry&)> handler);
3023
3024 bool swift_versioning_enabled(const RGWBucketInfo& bucket_info) const {
3025 return bucket_info.has_swift_versioning() &&
3026 bucket_info.swift_ver_location.size();
3027 }
3028
3029 int swift_versioning_copy(RGWObjectCtx& obj_ctx, /* in/out */
3030 const rgw_user& user, /* in */
3031 RGWBucketInfo& bucket_info, /* in */
3032 rgw_obj& obj); /* in */
3033 int swift_versioning_restore(RGWObjectCtx& obj_ctx, /* in/out */
3034 const rgw_user& user, /* in */
3035 RGWBucketInfo& bucket_info, /* in */
3036 rgw_obj& obj, /* in */
3037 bool& restored); /* out */
3038 int copy_obj_to_remote_dest(RGWObjState *astate,
3039 map<string, bufferlist>& src_attrs,
3040 RGWRados::Object::Read& read_op,
3041 const rgw_user& user_id,
3042 rgw_obj& dest_obj,
3043 ceph::real_time *mtime);
3044
3045 enum AttrsMod {
3046 ATTRSMOD_NONE = 0,
3047 ATTRSMOD_REPLACE = 1,
3048 ATTRSMOD_MERGE = 2
3049 };
3050
3051 int rewrite_obj(RGWBucketInfo& dest_bucket_info, rgw_obj& obj);
3052
3053 int stat_remote_obj(RGWObjectCtx& obj_ctx,
3054 const rgw_user& user_id,
3055 const string& client_id,
3056 req_info *info,
3057 const string& source_zone,
3058 rgw_obj& src_obj,
3059 RGWBucketInfo& src_bucket_info,
3060 real_time *src_mtime,
3061 uint64_t *psize,
3062 const real_time *mod_ptr,
3063 const real_time *unmod_ptr,
3064 bool high_precision_time,
3065 const char *if_match,
3066 const char *if_nomatch,
3067 map<string, bufferlist> *pattrs,
3068 string *version_id,
3069 string *ptag,
3070 string *petag);
3071
3072 int fetch_remote_obj(RGWObjectCtx& obj_ctx,
3073 const rgw_user& user_id,
3074 const string& client_id,
3075 const string& op_id,
3076 bool record_op_state,
3077 req_info *info,
3078 const string& source_zone,
3079 rgw_obj& dest_obj,
3080 rgw_obj& src_obj,
3081 RGWBucketInfo& dest_bucket_info,
3082 RGWBucketInfo& src_bucket_info,
3083 ceph::real_time *src_mtime,
3084 ceph::real_time *mtime,
3085 const ceph::real_time *mod_ptr,
3086 const ceph::real_time *unmod_ptr,
3087 bool high_precision_time,
3088 const char *if_match,
3089 const char *if_nomatch,
3090 AttrsMod attrs_mod,
3091 bool copy_if_newer,
3092 map<string, bufferlist>& attrs,
3093 RGWObjCategory category,
3094 uint64_t olh_epoch,
3095 ceph::real_time delete_at,
3096 string *version_id,
3097 string *ptag,
3098 ceph::buffer::list *petag,
3099 void (*progress_cb)(off_t, void *),
3100 void *progress_data,
3101 rgw_zone_set *zones_trace= nullptr);
3102 /**
3103 * Copy an object.
3104 * dest_obj: the object to copy into
3105 * src_obj: the object to copy from
3106 * attrs: usage depends on attrs_mod parameter
3107 * attrs_mod: the modification mode of the attrs, may have the following values:
3108 * ATTRSMOD_NONE - the attributes of the source object will be
3109 * copied without modifications, attrs parameter is ignored;
3110 * ATTRSMOD_REPLACE - new object will have the attributes provided by attrs
3111 * parameter, source object attributes are not copied;
3112 * ATTRSMOD_MERGE - any conflicting meta keys on the source object's attributes
3113 * are overwritten by values contained in attrs parameter.
3114 * Returns: 0 on success, -ERR# otherwise.
3115 */
3116 int copy_obj(RGWObjectCtx& obj_ctx,
3117 const rgw_user& user_id,
3118 const string& client_id,
3119 const string& op_id,
3120 req_info *info,
3121 const string& source_zone,
3122 rgw_obj& dest_obj,
3123 rgw_obj& src_obj,
3124 RGWBucketInfo& dest_bucket_info,
3125 RGWBucketInfo& src_bucket_info,
3126 ceph::real_time *src_mtime,
3127 ceph::real_time *mtime,
3128 const ceph::real_time *mod_ptr,
3129 const ceph::real_time *unmod_ptr,
3130 bool high_precision_time,
3131 const char *if_match,
3132 const char *if_nomatch,
3133 AttrsMod attrs_mod,
3134 bool copy_if_newer,
3135 map<std::string, bufferlist>& attrs,
3136 RGWObjCategory category,
3137 uint64_t olh_epoch,
3138 ceph::real_time delete_at,
3139 string *version_id,
3140 string *ptag,
3141 ceph::buffer::list *petag,
3142 void (*progress_cb)(off_t, void *),
3143 void *progress_data);
3144
3145 int copy_obj_data(RGWObjectCtx& obj_ctx,
3146 RGWBucketInfo& dest_bucket_info,
3147 RGWRados::Object::Read& read_op, off_t end,
3148 rgw_obj& dest_obj,
3149 rgw_obj& src_obj,
3150 uint64_t max_chunk_size,
3151 ceph::real_time *mtime,
3152 ceph::real_time set_mtime,
3153 map<string, bufferlist>& attrs,
3154 RGWObjCategory category,
3155 uint64_t olh_epoch,
3156 ceph::real_time delete_at,
3157 string *version_id,
3158 string *ptag,
3159 ceph::buffer::list *petag);
3160
3161 int check_bucket_empty(RGWBucketInfo& bucket_info);
3162
3163 /**
3164 * Delete a bucket.
3165 * bucket: the name of the bucket to delete
3166 * Returns 0 on success, -ERR# otherwise.
3167 */
3168 int delete_bucket(RGWBucketInfo& bucket_info, RGWObjVersionTracker& objv_tracker, bool check_empty = true);
3169
3170 bool is_meta_master();
3171
3172 /**
3173 * Check to see if the bucket metadata is synced
3174 */
3175 bool is_syncing_bucket_meta(const rgw_bucket& bucket);
3176 void wakeup_meta_sync_shards(set<int>& shard_ids);
3177 void wakeup_data_sync_shards(const string& source_zone, map<int, set<string> >& shard_ids);
3178
3179 RGWMetaSyncStatusManager* get_meta_sync_manager();
3180 RGWDataSyncStatusManager* get_data_sync_manager(const std::string& source_zone);
3181
3182 int set_bucket_owner(rgw_bucket& bucket, ACLOwner& owner);
3183 int set_buckets_enabled(std::vector<rgw_bucket>& buckets, bool enabled);
3184 int bucket_suspended(rgw_bucket& bucket, bool *suspended);
3185
3186 /** Delete an object.*/
3187 int delete_obj(RGWObjectCtx& obj_ctx,
3188 const RGWBucketInfo& bucket_owner,
3189 const rgw_obj& src_obj,
3190 int versioning_status,
3191 uint16_t bilog_flags = 0,
3192 const ceph::real_time& expiration_time = ceph::real_time(),
3193 rgw_zone_set *zones_trace = nullptr);
3194
3195 /** Delete a raw object.*/
3196 int delete_raw_obj(const rgw_raw_obj& obj);
3197
3198 /* Delete a system object */
3199 virtual int delete_system_obj(rgw_raw_obj& src_obj, RGWObjVersionTracker *objv_tracker = NULL);
3200
3201 /** Remove an object from the bucket index */
3202 int delete_obj_index(const rgw_obj& obj);
3203
3204 /**
3205 * Get an attribute for a system object.
3206 * obj: the object to get attr
3207 * name: name of the attr to retrieve
3208 * dest: bufferlist to store the result in
3209 * Returns: 0 on success, -ERR# otherwise.
3210 */
3211 virtual int system_obj_get_attr(rgw_raw_obj& obj, const char *name, bufferlist& dest);
3212
3213 int system_obj_set_attr(void *ctx, rgw_raw_obj& obj, const char *name, bufferlist& bl,
3214 RGWObjVersionTracker *objv_tracker);
3215 virtual int system_obj_set_attrs(void *ctx, rgw_raw_obj& obj,
3216 map<string, bufferlist>& attrs,
3217 map<string, bufferlist>* rmattrs,
3218 RGWObjVersionTracker *objv_tracker);
3219
3220 /**
3221 * Set an attr on an object.
3222 * bucket: name of the bucket holding the object
3223 * obj: name of the object to set the attr on
3224 * name: the attr to set
3225 * bl: the contents of the attr
3226 * Returns: 0 on success, -ERR# otherwise.
3227 */
3228 int set_attr(void *ctx, const RGWBucketInfo& bucket_info, rgw_obj& obj, const char *name, bufferlist& bl);
3229
3230 int set_attrs(void *ctx, const RGWBucketInfo& bucket_info, rgw_obj& obj,
3231 map<string, bufferlist>& attrs,
3232 map<string, bufferlist>* rmattrs);
3233
3234 int get_system_obj_state(RGWObjectCtx *rctx, rgw_raw_obj& obj, RGWRawObjState **state, RGWObjVersionTracker *objv_tracker);
3235 int get_obj_state(RGWObjectCtx *rctx, const RGWBucketInfo& bucket_info, const rgw_obj& obj, RGWObjState **state,
3236 bool follow_olh, bool assume_noent = false);
3237 int get_obj_state(RGWObjectCtx *rctx, const RGWBucketInfo& bucket_info, const rgw_obj& obj, RGWObjState **state) {
3238 return get_obj_state(rctx, bucket_info, obj, state, true);
3239 }
3240
3241 virtual int stat_system_obj(RGWObjectCtx& obj_ctx,
3242 RGWRados::SystemObject::Read::GetObjState& state,
3243 rgw_raw_obj& obj,
3244 map<string, bufferlist> *attrs,
3245 ceph::real_time *lastmod,
3246 uint64_t *obj_size,
3247 RGWObjVersionTracker *objv_tracker);
3248
3249 virtual int get_system_obj(RGWObjectCtx& obj_ctx, RGWRados::SystemObject::Read::GetObjState& read_state,
3250 RGWObjVersionTracker *objv_tracker, rgw_raw_obj& obj,
3251 bufferlist& bl, off_t ofs, off_t end,
3252 map<string, bufferlist> *attrs,
3253 rgw_cache_entry_info *cache_info);
3254
3255 virtual void register_chained_cache(RGWChainedCache *cache) {}
3256 virtual bool chain_cache_entry(list<rgw_cache_entry_info *>& cache_info_entries, RGWChainedCache::Entry *chained_entry) { return false; }
3257
3258 int iterate_obj(RGWObjectCtx& ctx,
3259 const RGWBucketInfo& bucket_info, const rgw_obj& obj,
3260 off_t ofs, off_t end,
3261 uint64_t max_chunk_size,
3262 int (*iterate_obj_cb)(const RGWBucketInfo& bucket_info, const rgw_obj& obj, const rgw_raw_obj&, off_t, off_t, off_t, bool, RGWObjState *, void *),
3263 void *arg);
3264
3265 int flush_read_list(struct get_obj_data *d);
3266
3267 int get_obj_iterate_cb(RGWObjectCtx *ctx, RGWObjState *astate,
3268 const RGWBucketInfo& bucket_info, const rgw_obj& obj,
3269 const rgw_raw_obj& read_obj,
3270 off_t obj_ofs, off_t read_ofs, off_t len,
3271 bool is_head_obj, void *arg);
3272
3273 void get_obj_aio_completion_cb(librados::completion_t cb, void *arg);
3274
3275 /**
3276 * a simple object read without keeping state
3277 */
3278
3279 virtual int raw_obj_stat(rgw_raw_obj& obj, uint64_t *psize, ceph::real_time *pmtime, uint64_t *epoch,
3280 map<string, bufferlist> *attrs, bufferlist *first_chunk,
3281 RGWObjVersionTracker *objv_tracker);
3282
3283 int obj_operate(const RGWBucketInfo& bucket_info, const rgw_obj& obj, librados::ObjectWriteOperation *op);
3284 int obj_operate(const RGWBucketInfo& bucket_info, const rgw_obj& obj, librados::ObjectReadOperation *op);
3285
3286 int guard_reshard(BucketShard *bs, const rgw_obj& obj_instance, std::function<int(BucketShard *)> call);
3287 int block_while_resharding(RGWRados::BucketShard *bs, string *new_bucket_id);
3288
3289 void bucket_index_guard_olh_op(RGWObjState& olh_state, librados::ObjectOperation& op);
3290 int olh_init_modification(const RGWBucketInfo& bucket_info, RGWObjState& state, const rgw_obj& olh_obj, string *op_tag);
3291 int olh_init_modification_impl(const RGWBucketInfo& bucket_info, RGWObjState& state, const rgw_obj& olh_obj, string *op_tag);
3292 int bucket_index_link_olh(const RGWBucketInfo& bucket_info, RGWObjState& olh_state,
3293 const rgw_obj& obj_instance, bool delete_marker,
3294 const string& op_tag, struct rgw_bucket_dir_entry_meta *meta,
3295 uint64_t olh_epoch,
3296 ceph::real_time unmod_since, bool high_precision_time, rgw_zone_set *zones_trace = nullptr);
3297 int bucket_index_unlink_instance(const RGWBucketInfo& bucket_info, const rgw_obj& obj_instance, const string& op_tag, const string& olh_tag, uint64_t olh_epoch, rgw_zone_set *zones_trace = nullptr);
3298 int bucket_index_read_olh_log(const RGWBucketInfo& bucket_info, RGWObjState& state, const rgw_obj& obj_instance, uint64_t ver_marker,
3299 map<uint64_t, vector<rgw_bucket_olh_log_entry> > *log, bool *is_truncated);
3300 int bucket_index_trim_olh_log(const RGWBucketInfo& bucket_info, RGWObjState& obj_state, const rgw_obj& obj_instance, uint64_t ver);
3301 int bucket_index_clear_olh(const RGWBucketInfo& bucket_info, RGWObjState& state, const rgw_obj& obj_instance);
3302 int apply_olh_log(RGWObjectCtx& ctx, RGWObjState& obj_state, const RGWBucketInfo& bucket_info, const rgw_obj& obj,
3303 bufferlist& obj_tag, map<uint64_t, vector<rgw_bucket_olh_log_entry> >& log,
3304 uint64_t *plast_ver, rgw_zone_set *zones_trace = nullptr);
3305 int update_olh(RGWObjectCtx& obj_ctx, RGWObjState *state, const RGWBucketInfo& bucket_info, const rgw_obj& obj, rgw_zone_set *zones_trace = nullptr);
3306 int set_olh(RGWObjectCtx& obj_ctx, RGWBucketInfo& bucket_info, const rgw_obj& target_obj, bool delete_marker, rgw_bucket_dir_entry_meta *meta,
3307 uint64_t olh_epoch, ceph::real_time unmod_since, bool high_precision_time, rgw_zone_set *zones_trace = nullptr);
3308 int unlink_obj_instance(RGWObjectCtx& obj_ctx, RGWBucketInfo& bucket_info, const rgw_obj& target_obj,
3309 uint64_t olh_epoch, rgw_zone_set *zones_trace = nullptr);
3310
3311 void check_pending_olh_entries(map<string, bufferlist>& pending_entries, map<string, bufferlist> *rm_pending_entries);
3312 int remove_olh_pending_entries(const RGWBucketInfo& bucket_info, RGWObjState& state, const rgw_obj& olh_obj, map<string, bufferlist>& pending_attrs);
3313 int follow_olh(const RGWBucketInfo& bucket_info, RGWObjectCtx& ctx, RGWObjState *state, const rgw_obj& olh_obj, rgw_obj *target);
3314 int get_olh(const RGWBucketInfo& bucket_info, const rgw_obj& obj, RGWOLHInfo *olh);
3315
3316 void gen_rand_obj_instance_name(rgw_obj *target);
3317
3318 int omap_get_vals(rgw_raw_obj& obj, bufferlist& header, const std::string& marker, uint64_t count, std::map<string, bufferlist>& m);
3319 int omap_get_all(rgw_raw_obj& obj, bufferlist& header, std::map<string, bufferlist>& m);
3320 int omap_set(rgw_raw_obj& obj, const std::string& key, bufferlist& bl);
3321 int omap_set(rgw_raw_obj& obj, map<std::string, bufferlist>& m);
3322 int omap_del(rgw_raw_obj& obj, const std::string& key);
3323 int update_containers_stats(map<string, RGWBucketEnt>& m);
3324 int append_async(rgw_raw_obj& obj, size_t size, bufferlist& bl);
3325
3326 int watch(const string& oid, uint64_t *watch_handle, librados::WatchCtx2 *ctx);
3327 int unwatch(uint64_t watch_handle);
3328 void add_watcher(int i);
3329 void remove_watcher(int i);
3330 virtual bool need_watch_notify() { return false; }
3331 int init_watch();
3332 void finalize_watch();
3333 int distribute(const string& key, bufferlist& bl);
3334 virtual int watch_cb(uint64_t notify_id,
3335 uint64_t cookie,
3336 uint64_t notifier_id,
3337 bufferlist& bl) { return 0; }
3338 void pick_control_oid(const string& key, string& notify_oid);
3339
3340 virtual void set_cache_enabled(bool state) {}
3341
3342 void set_atomic(void *ctx, rgw_obj& obj) {
3343 RGWObjectCtx *rctx = static_cast<RGWObjectCtx *>(ctx);
3344 rctx->obj.set_atomic(obj);
3345 }
3346 void set_prefetch_data(void *ctx, rgw_obj& obj) {
3347 RGWObjectCtx *rctx = static_cast<RGWObjectCtx *>(ctx);
3348 rctx->obj.set_prefetch_data(obj);
3349 }
3350 void set_prefetch_data(void *ctx, rgw_raw_obj& obj) {
3351 RGWObjectCtx *rctx = static_cast<RGWObjectCtx *>(ctx);
3352 rctx->raw.set_prefetch_data(obj);
3353 }
3354
3355 int decode_policy(bufferlist& bl, ACLOwner *owner);
3356 int get_bucket_stats(RGWBucketInfo& bucket_info, int shard_id, string *bucket_ver, string *master_ver,
3357 map<RGWObjCategory, RGWStorageStats>& stats, string *max_marker);
3358 int get_bucket_stats_async(RGWBucketInfo& bucket_info, int shard_id, RGWGetBucketStats_CB *cb);
3359 int get_user_stats(const rgw_user& user, RGWStorageStats& stats);
3360 int get_user_stats_async(const rgw_user& user, RGWGetUserStats_CB *cb);
3361 void get_bucket_instance_obj(const rgw_bucket& bucket, rgw_raw_obj& obj);
3362 void get_bucket_meta_oid(const rgw_bucket& bucket, string& oid);
3363
3364 int put_bucket_entrypoint_info(const string& tenant_name, const string& bucket_name, RGWBucketEntryPoint& entry_point,
3365 bool exclusive, RGWObjVersionTracker& objv_tracker, ceph::real_time mtime,
3366 map<string, bufferlist> *pattrs);
3367 int put_bucket_instance_info(RGWBucketInfo& info, bool exclusive, ceph::real_time mtime, map<string, bufferlist> *pattrs);
3368 int get_bucket_entrypoint_info(RGWObjectCtx& obj_ctx, const string& tenant_name, const string& bucket_name,
3369 RGWBucketEntryPoint& entry_point, RGWObjVersionTracker *objv_tracker,
3370 ceph::real_time *pmtime, map<string, bufferlist> *pattrs, rgw_cache_entry_info *cache_info = NULL);
3371 int get_bucket_instance_info(RGWObjectCtx& obj_ctx, const string& meta_key, RGWBucketInfo& info, ceph::real_time *pmtime, map<string, bufferlist> *pattrs);
3372 int get_bucket_instance_info(RGWObjectCtx& obj_ctx, const rgw_bucket& bucket, RGWBucketInfo& info, ceph::real_time *pmtime, map<string, bufferlist> *pattrs);
3373 int get_bucket_instance_from_oid(RGWObjectCtx& obj_ctx, const string& oid, RGWBucketInfo& info, ceph::real_time *pmtime, map<string, bufferlist> *pattrs,
3374 rgw_cache_entry_info *cache_info = NULL);
3375
3376 int convert_old_bucket_info(RGWObjectCtx& obj_ctx, const string& tenant_name, const string& bucket_name);
3377 static void make_bucket_entry_name(const string& tenant_name, const string& bucket_name, string& bucket_entry);
3378 int get_bucket_info(RGWObjectCtx& obj_ctx,
3379 const string& tenant_name, const string& bucket_name,
3380 RGWBucketInfo& info,
3381 ceph::real_time *pmtime, map<string, bufferlist> *pattrs = NULL);
3382 int put_linked_bucket_info(RGWBucketInfo& info, bool exclusive, ceph::real_time mtime, obj_version *pep_objv,
3383 map<string, bufferlist> *pattrs, bool create_entry_point);
3384
3385 int cls_rgw_init_index(librados::IoCtx& io_ctx, librados::ObjectWriteOperation& op, string& oid);
3386 int cls_obj_prepare_op(BucketShard& bs, RGWModifyOp op, string& tag, rgw_obj& obj, uint16_t bilog_flags, rgw_zone_set *zones_trace = nullptr);
3387 int cls_obj_complete_op(BucketShard& bs, const rgw_obj& obj, RGWModifyOp op, string& tag, int64_t pool, uint64_t epoch,
3388 rgw_bucket_dir_entry& ent, RGWObjCategory category, list<rgw_obj_index_key> *remove_objs, uint16_t bilog_flags, rgw_zone_set *zones_trace = nullptr);
3389 int cls_obj_complete_add(BucketShard& bs, const rgw_obj& obj, string& tag, int64_t pool, uint64_t epoch, rgw_bucket_dir_entry& ent,
3390 RGWObjCategory category, list<rgw_obj_index_key> *remove_objs, uint16_t bilog_flags, rgw_zone_set *zones_trace = nullptr);
3391 int cls_obj_complete_del(BucketShard& bs, string& tag, int64_t pool, uint64_t epoch, rgw_obj& obj,
3392 ceph::real_time& removed_mtime, list<rgw_obj_index_key> *remove_objs, uint16_t bilog_flags, rgw_zone_set *zones_trace = nullptr);
3393 int cls_obj_complete_cancel(BucketShard& bs, string& tag, rgw_obj& obj, uint16_t bilog_flags, rgw_zone_set *zones_trace = nullptr);
3394 int cls_obj_set_bucket_tag_timeout(RGWBucketInfo& bucket_info, uint64_t timeout);
3395 int cls_bucket_list(RGWBucketInfo& bucket_info, int shard_id, rgw_obj_index_key& start, const string& prefix,
3396 uint32_t num_entries, bool list_versions, map<string, rgw_bucket_dir_entry>& m,
3397 bool *is_truncated, rgw_obj_index_key *last_entry,
3398 bool (*force_check_filter)(const string& name) = NULL);
3399 int cls_bucket_head(const RGWBucketInfo& bucket_info, int shard_id, map<string, struct rgw_bucket_dir_header>& headers, map<int, string> *bucket_instance_ids = NULL);
3400 int cls_bucket_head_async(const RGWBucketInfo& bucket_info, int shard_id, RGWGetDirHeader_CB *ctx, int *num_aio);
3401 int list_bi_log_entries(RGWBucketInfo& bucket_info, int shard_id, string& marker, uint32_t max, std::list<rgw_bi_log_entry>& result, bool *truncated);
3402 int trim_bi_log_entries(RGWBucketInfo& bucket_info, int shard_id, string& marker, string& end_marker);
3403 int get_bi_log_status(RGWBucketInfo& bucket_info, int shard_id, map<int, string>& max_marker);
3404
3405 int bi_get_instance(const RGWBucketInfo& bucket_info, rgw_obj& obj, rgw_bucket_dir_entry *dirent);
3406 int bi_get(rgw_bucket& bucket, rgw_obj& obj, BIIndexType index_type, rgw_cls_bi_entry *entry);
3407 void bi_put(librados::ObjectWriteOperation& op, BucketShard& bs, rgw_cls_bi_entry& entry);
3408 int bi_put(BucketShard& bs, rgw_cls_bi_entry& entry);
3409 int bi_put(rgw_bucket& bucket, rgw_obj& obj, rgw_cls_bi_entry& entry);
3410 int bi_list(rgw_bucket& bucket, int shard_id, const string& filter_obj, const string& marker, uint32_t max, list<rgw_cls_bi_entry> *entries, bool *is_truncated);
3411 int bi_list(BucketShard& bs, const string& filter_obj, const string& marker, uint32_t max, list<rgw_cls_bi_entry> *entries, bool *is_truncated);
3412 int bi_list(rgw_bucket& bucket, const string& obj_name, const string& marker, uint32_t max,
3413 list<rgw_cls_bi_entry> *entries, bool *is_truncated);
3414 int bi_remove(BucketShard& bs);
3415
3416 int cls_obj_usage_log_add(const string& oid, rgw_usage_log_info& info);
3417 int cls_obj_usage_log_read(string& oid, string& user, uint64_t start_epoch, uint64_t end_epoch, uint32_t max_entries,
3418 string& read_iter, map<rgw_user_bucket, rgw_usage_log_entry>& usage, bool *is_truncated);
3419 int cls_obj_usage_log_trim(string& oid, string& user, uint64_t start_epoch, uint64_t end_epoch);
3420
3421 int key_to_shard_id(const string& key, int max_shards);
3422 void shard_name(const string& prefix, unsigned max_shards, const string& key, string& name, int *shard_id);
3423 void shard_name(const string& prefix, unsigned max_shards, const string& section, const string& key, string& name);
3424 void shard_name(const string& prefix, unsigned shard_id, string& name);
3425 int get_target_shard_id(const RGWBucketInfo& bucket_info, const string& obj_key, int *shard_id);
3426 void time_log_prepare_entry(cls_log_entry& entry, const ceph::real_time& ut, const string& section, const string& key, bufferlist& bl);
3427 int time_log_add_init(librados::IoCtx& io_ctx);
3428 int time_log_add(const string& oid, list<cls_log_entry>& entries,
3429 librados::AioCompletion *completion, bool monotonic_inc = true);
3430 int time_log_add(const string& oid, const ceph::real_time& ut, const string& section, const string& key, bufferlist& bl);
3431 int time_log_list(const string& oid, const ceph::real_time& start_time, const ceph::real_time& end_time,
3432 int max_entries, list<cls_log_entry>& entries,
3433 const string& marker, string *out_marker, bool *truncated);
3434 int time_log_info(const string& oid, cls_log_header *header);
3435 int time_log_info_async(librados::IoCtx& io_ctx, const string& oid, cls_log_header *header, librados::AioCompletion *completion);
3436 int time_log_trim(const string& oid, const ceph::real_time& start_time, const ceph::real_time& end_time,
3437 const string& from_marker, const string& to_marker,
3438 librados::AioCompletion *completion = nullptr);
3439
3440 string objexp_hint_get_shardname(int shard_num);
3441 int objexp_key_shard(const rgw_obj_index_key& key);
3442 void objexp_get_shard(int shard_num,
3443 string& shard); /* out */
3444 int objexp_hint_add(const ceph::real_time& delete_at,
3445 const string& tenant_name,
3446 const string& bucket_name,
3447 const string& bucket_id,
3448 const rgw_obj_index_key& obj_key);
3449 int objexp_hint_list(const string& oid,
3450 const ceph::real_time& start_time,
3451 const ceph::real_time& end_time,
3452 const int max_entries,
3453 const string& marker,
3454 list<cls_timeindex_entry>& entries, /* out */
3455 string *out_marker, /* out */
3456 bool *truncated); /* out */
3457 int objexp_hint_parse(cls_timeindex_entry &ti_entry,
3458 objexp_hint_entry& hint_entry); /* out */
3459 int objexp_hint_trim(const string& oid,
3460 const ceph::real_time& start_time,
3461 const ceph::real_time& end_time,
3462 const string& from_marker = std::string(),
3463 const string& to_marker = std::string());
3464
3465 int lock_exclusive(rgw_pool& pool, const string& oid, ceph::timespan& duration, string& zone_id, string& owner_id);
3466 int unlock(rgw_pool& pool, const string& oid, string& zone_id, string& owner_id);
3467
3468 void update_gc_chain(rgw_obj& head_obj, RGWObjManifest& manifest, cls_rgw_obj_chain *chain);
3469 int send_chain_to_gc(cls_rgw_obj_chain& chain, const string& tag, bool sync);
3470 int gc_operate(string& oid, librados::ObjectWriteOperation *op);
3471 int gc_aio_operate(string& oid, librados::ObjectWriteOperation *op);
3472 int gc_operate(string& oid, librados::ObjectReadOperation *op, bufferlist *pbl);
3473
3474 int list_gc_objs(int *index, string& marker, uint32_t max, bool expired_only, std::list<cls_rgw_gc_obj_info>& result, bool *truncated);
3475 int process_gc();
3476 int process_expire_objects();
3477 int defer_gc(void *ctx, const RGWBucketInfo& bucket_info, const rgw_obj& obj);
3478
3479 int process_lc();
3480 int list_lc_progress(const string& marker, uint32_t max_entries, map<string, int> *progress_map);
3481
3482 int bucket_check_index(RGWBucketInfo& bucket_info,
3483 map<RGWObjCategory, RGWStorageStats> *existing_stats,
3484 map<RGWObjCategory, RGWStorageStats> *calculated_stats);
3485 int bucket_rebuild_index(RGWBucketInfo& bucket_info);
3486 int bucket_set_reshard(RGWBucketInfo& bucket_info, const cls_rgw_bucket_instance_entry& entry);
3487 int remove_objs_from_index(RGWBucketInfo& bucket_info, list<rgw_obj_index_key>& oid_list);
3488 int move_rados_obj(librados::IoCtx& src_ioctx,
3489 const string& src_oid, const string& src_locator,
3490 librados::IoCtx& dst_ioctx,
3491 const string& dst_oid, const string& dst_locator);
3492 int fix_head_obj_locator(const RGWBucketInfo& bucket_info, bool copy_obj, bool remove_bad, rgw_obj_key& key);
3493 int fix_tail_obj_locator(const RGWBucketInfo& bucket_info, rgw_obj_key& key, bool fix, bool *need_fix);
3494
3495 int cls_user_get_header(const string& user_id, cls_user_header *header);
3496 int cls_user_get_header_async(const string& user_id, RGWGetUserHeader_CB *ctx);
3497 int cls_user_sync_bucket_stats(rgw_raw_obj& user_obj, const RGWBucketInfo& bucket_info);
3498 int cls_user_list_buckets(rgw_raw_obj& obj,
3499 const string& in_marker,
3500 const string& end_marker,
3501 int max_entries,
3502 list<cls_user_bucket_entry>& entries,
3503 string *out_marker,
3504 bool *truncated);
3505 int cls_user_add_bucket(rgw_raw_obj& obj, const cls_user_bucket_entry& entry);
3506 int cls_user_update_buckets(rgw_raw_obj& obj, list<cls_user_bucket_entry>& entries, bool add);
3507 int cls_user_complete_stats_sync(rgw_raw_obj& obj);
3508 int complete_sync_user_stats(const rgw_user& user_id);
3509 int cls_user_add_bucket(rgw_raw_obj& obj, list<cls_user_bucket_entry>& entries);
3510 int cls_user_remove_bucket(rgw_raw_obj& obj, const cls_user_bucket& bucket);
3511
3512 int check_quota(const rgw_user& bucket_owner, rgw_bucket& bucket,
3513 RGWQuotaInfo& user_quota, RGWQuotaInfo& bucket_quota, uint64_t obj_size);
3514
3515 int check_bucket_shards(const RGWBucketInfo& bucket_info, rgw_bucket& bucket,
3516 RGWQuotaInfo& bucket_quota);
3517
3518 int add_bucket_to_reshard(const RGWBucketInfo& bucket_info, uint32_t new_num_shards);
3519
3520 uint64_t instance_id();
3521 const string& zone_id() {
3522 return get_zone_params().get_id();
3523 }
3524 string unique_id(uint64_t unique_num) {
3525 char buf[32];
3526 snprintf(buf, sizeof(buf), ".%llu.%llu", (unsigned long long)instance_id(), (unsigned long long)unique_num);
3527 string s = get_zone_params().get_id() + buf;
3528 return s;
3529 }
3530
3531 void init_unique_trans_id_deps() {
3532 char buf[16 + 2 + 1]; /* uint64_t needs 16, 2 hyphens add further 2 */
3533
3534 snprintf(buf, sizeof(buf), "-%llx-", (unsigned long long)instance_id());
3535 url_encode(string(buf) + get_zone_params().get_name(), trans_id_suffix);
3536 }
3537
3538 /* In order to preserve compability with Swift API, transaction ID
3539 * should contain at least 32 characters satisfying following spec:
3540 * - first 21 chars must be in range [0-9a-f]. Swift uses this
3541 * space for storing fragment of UUID obtained through a call to
3542 * uuid4() function of Python's uuid module;
3543 * - char no. 22 must be a hyphen;
3544 * - at least 10 next characters constitute hex-formatted timestamp
3545 * padded with zeroes if necessary. All bytes must be in [0-9a-f]
3546 * range;
3547 * - last, optional part of transaction ID is any url-encoded string
3548 * without restriction on length. */
3549 string unique_trans_id(const uint64_t unique_num) {
3550 char buf[41]; /* 2 + 21 + 1 + 16 (timestamp can consume up to 16) + 1 */
3551 time_t timestamp = time(NULL);
3552
3553 snprintf(buf, sizeof(buf), "tx%021llx-%010llx",
3554 (unsigned long long)unique_num,
3555 (unsigned long long)timestamp);
3556
3557 return string(buf) + trans_id_suffix;
3558 }
3559
3560 void get_log_pool(rgw_pool& pool) {
3561 pool = get_zone_params().log_pool;
3562 }
3563
3564 bool need_to_log_data() {
3565 return get_zone().log_data;
3566 }
3567
3568 bool need_to_log_metadata() {
3569 return is_meta_master() && get_zone().log_meta;
3570 }
3571
3572 librados::Rados* get_rados_handle();
3573
3574 int delete_raw_obj_aio(const rgw_raw_obj& obj, list<librados::AioCompletion *>& handles);
3575 int delete_obj_aio(const rgw_obj& obj, RGWBucketInfo& info, RGWObjState *astate,
3576 list<librados::AioCompletion *>& handles, bool keep_index_consistent);
3577 private:
3578 /**
3579 * This is a helper method, it generates a list of bucket index objects with the given
3580 * bucket base oid and number of shards.
3581 *
3582 * bucket_oid_base [in] - base name of the bucket index object;
3583 * num_shards [in] - number of bucket index object shards.
3584 * bucket_objs [out] - filled by this method, a list of bucket index objects.
3585 */
3586 void get_bucket_index_objects(const string& bucket_oid_base, uint32_t num_shards,
3587 map<int, string>& bucket_objs, int shard_id = -1);
3588
3589 /**
3590 * Get the bucket index object with the given base bucket index object and object key,
3591 * and the number of bucket index shards.
3592 *
3593 * bucket_oid_base [in] - bucket object base name.
3594 * obj_key [in] - object key.
3595 * num_shards [in] - number of bucket index shards.
3596 * hash_type [in] - type of hash to find the shard ID.
3597 * bucket_obj [out] - the bucket index object for the given object.
3598 *
3599 * Return 0 on success, a failure code otherwise.
3600 */
3601 int get_bucket_index_object(const string& bucket_oid_base, const string& obj_key,
3602 uint32_t num_shards, RGWBucketInfo::BIShardsHashType hash_type, string *bucket_obj, int *shard);
3603
3604 void get_bucket_index_object(const string& bucket_oid_base, uint32_t num_shards,
3605 int shard_id, string *bucket_obj);
3606
3607 /**
3608 * Check the actual on-disk state of the object specified
3609 * by list_state, and fill in the time and size of object.
3610 * Then append any changes to suggested_updates for
3611 * the rgw class' dir_suggest_changes function.
3612 *
3613 * Note that this can maul list_state; don't use it afterwards. Also
3614 * it expects object to already be filled in from list_state; it only
3615 * sets the size and mtime.
3616 *
3617 * Returns 0 on success, -ENOENT if the object doesn't exist on disk,
3618 * and -errno on other failures. (-ENOENT is not a failure, and it
3619 * will encode that info as a suggested update.)
3620 */
3621 int check_disk_state(librados::IoCtx io_ctx,
3622 const RGWBucketInfo& bucket_info,
3623 rgw_bucket_dir_entry& list_state,
3624 rgw_bucket_dir_entry& object,
3625 bufferlist& suggested_updates);
3626
3627 /**
3628 * Init pool iteration
3629 * pool: pool to use for the ctx initialization
3630 * ctx: context object to use for the iteration
3631 * Returns: 0 on success, -ERR# otherwise.
3632 */
3633 int pool_iterate_begin(const rgw_pool& pool, RGWPoolIterCtx& ctx);
3634
3635 /**
3636 * Iterate over pool return object names, use optional filter
3637 * ctx: iteration context, initialized with pool_iterate_begin()
3638 * num: max number of objects to return
3639 * objs: a vector that the results will append into
3640 * is_truncated: if not NULL, will hold true iff iteration is complete
3641 * filter: if not NULL, will be used to filter returned objects
3642 * Returns: 0 on success, -ERR# otherwise.
3643 */
3644 int pool_iterate(RGWPoolIterCtx& ctx, uint32_t num, vector<rgw_bucket_dir_entry>& objs,
3645 bool *is_truncated, RGWAccessListFilter *filter);
3646
3647 uint64_t next_bucket_id();
3648 };
3649
3650 class RGWStoreManager {
3651 public:
3652 RGWStoreManager() {}
3653 static RGWRados *get_storage(CephContext *cct, bool use_gc_thread, bool use_lc_thread, bool quota_threads, bool run_sync_thread, bool run_reshard_thread) {
3654 RGWRados *store = init_storage_provider(cct, use_gc_thread, use_lc_thread, quota_threads, run_sync_thread,
3655 run_reshard_thread);
3656 return store;
3657 }
3658 static RGWRados *get_raw_storage(CephContext *cct) {
3659 RGWRados *store = init_raw_storage_provider(cct);
3660 return store;
3661 }
3662 static RGWRados *init_storage_provider(CephContext *cct, bool use_gc_thread, bool use_lc_thread, bool quota_threads, bool run_sync_thread, bool run_reshard_thread);
3663 static RGWRados *init_raw_storage_provider(CephContext *cct);
3664 static void close_storage(RGWRados *store);
3665
3666 };
3667
3668 template <class T>
3669 class RGWChainedCacheImpl : public RGWChainedCache {
3670 RWLock lock;
3671
3672 map<string, T> entries;
3673
3674 public:
3675 RGWChainedCacheImpl() : lock("RGWChainedCacheImpl::lock") {}
3676
3677 void init(RGWRados *store) {
3678 store->register_chained_cache(this);
3679 }
3680
3681 bool find(const string& key, T *entry) {
3682 RWLock::RLocker rl(lock);
3683 typename map<string, T>::iterator iter = entries.find(key);
3684 if (iter == entries.end()) {
3685 return false;
3686 }
3687
3688 *entry = iter->second;
3689 return true;
3690 }
3691
3692 bool put(RGWRados *store, const string& key, T *entry, list<rgw_cache_entry_info *>& cache_info_entries) {
3693 Entry chain_entry(this, key, entry);
3694
3695 /* we need the store cache to call us under its lock to maintain lock ordering */
3696 return store->chain_cache_entry(cache_info_entries, &chain_entry);
3697 }
3698
3699 void chain_cb(const string& key, void *data) override {
3700 T *entry = static_cast<T *>(data);
3701 RWLock::WLocker wl(lock);
3702 entries[key] = *entry;
3703 }
3704
3705 void invalidate(const string& key) override {
3706 RWLock::WLocker wl(lock);
3707 entries.erase(key);
3708 }
3709
3710 void invalidate_all() override {
3711 RWLock::WLocker wl(lock);
3712 entries.clear();
3713 }
3714 }; /* RGWChainedCacheImpl */
3715
3716 /**
3717 * Base of PUT operation.
3718 * Allow to create chained data transformers like compresors and encryptors.
3719 */
3720 class RGWPutObjDataProcessor
3721 {
3722 public:
3723 RGWPutObjDataProcessor(){}
3724 virtual ~RGWPutObjDataProcessor(){}
3725 virtual int handle_data(bufferlist& bl, off_t ofs, void **phandle, rgw_raw_obj *pobj, bool *again) = 0;
3726 virtual int throttle_data(void *handle, const rgw_raw_obj& obj, uint64_t size, bool need_to_wait) = 0;
3727 }; /* RGWPutObjDataProcessor */
3728
3729
3730 class RGWPutObjProcessor : public RGWPutObjDataProcessor
3731 {
3732 protected:
3733 RGWRados *store;
3734 RGWObjectCtx& obj_ctx;
3735 bool is_complete;
3736 RGWBucketInfo bucket_info;
3737 bool canceled;
3738
3739 virtual int do_complete(size_t accounted_size, const string& etag,
3740 ceph::real_time *mtime, ceph::real_time set_mtime,
3741 map<string, bufferlist>& attrs, ceph::real_time delete_at,
3742 const char *if_match, const char *if_nomatch, const string *user_data,
3743 rgw_zone_set* zones_trace = nullptr) = 0;
3744
3745 public:
3746 RGWPutObjProcessor(RGWObjectCtx& _obj_ctx, RGWBucketInfo& _bi) : store(NULL),
3747 obj_ctx(_obj_ctx),
3748 is_complete(false),
3749 bucket_info(_bi),
3750 canceled(false) {}
3751 ~RGWPutObjProcessor() override {}
3752 virtual int prepare(RGWRados *_store, string *oid_rand) {
3753 store = _store;
3754 return 0;
3755 }
3756
3757 int complete(size_t accounted_size, const string& etag,
3758 ceph::real_time *mtime, ceph::real_time set_mtime,
3759 map<string, bufferlist>& attrs, ceph::real_time delete_at,
3760 const char *if_match = NULL, const char *if_nomatch = NULL, const string *user_data = nullptr,
3761 rgw_zone_set *zones_trace = nullptr);
3762
3763 CephContext *ctx();
3764
3765 bool is_canceled() { return canceled; }
3766 }; /* RGWPutObjProcessor */
3767
3768 struct put_obj_aio_info {
3769 void *handle;
3770 rgw_raw_obj obj;
3771 uint64_t size;
3772 };
3773
3774 #define RGW_PUT_OBJ_MIN_WINDOW_SIZE_DEFAULT (16 * 1024 * 1024)
3775
3776 class RGWPutObjProcessor_Aio : public RGWPutObjProcessor
3777 {
3778 list<struct put_obj_aio_info> pending;
3779 uint64_t window_size{RGW_PUT_OBJ_MIN_WINDOW_SIZE_DEFAULT};
3780 uint64_t pending_size{0};
3781
3782 struct put_obj_aio_info pop_pending();
3783 int wait_pending_front();
3784 bool pending_has_completed();
3785
3786 rgw_raw_obj last_written_obj;
3787
3788 protected:
3789 uint64_t obj_len{0};
3790
3791 set<rgw_raw_obj> written_objs;
3792 rgw_obj head_obj;
3793
3794 void add_written_obj(const rgw_raw_obj& obj) {
3795 written_objs.insert(obj);
3796 }
3797
3798 int drain_pending();
3799 int handle_obj_data(rgw_raw_obj& obj, bufferlist& bl, off_t ofs, off_t abs_ofs, void **phandle, bool exclusive);
3800
3801 public:
3802 int prepare(RGWRados *store, string *oid_rand) override;
3803 int throttle_data(void *handle, const rgw_raw_obj& obj, uint64_t size, bool need_to_wait) override;
3804
3805 RGWPutObjProcessor_Aio(RGWObjectCtx& obj_ctx, RGWBucketInfo& bucket_info) : RGWPutObjProcessor(obj_ctx, bucket_info) {}
3806 ~RGWPutObjProcessor_Aio() override;
3807 }; /* RGWPutObjProcessor_Aio */
3808
3809 class RGWPutObjProcessor_Atomic : public RGWPutObjProcessor_Aio
3810 {
3811 bufferlist first_chunk;
3812 uint64_t part_size;
3813 off_t cur_part_ofs;
3814 off_t next_part_ofs;
3815 int cur_part_id;
3816 off_t data_ofs;
3817
3818 bufferlist pending_data_bl;
3819 uint64_t max_chunk_size;
3820
3821 bool versioned_object;
3822 uint64_t olh_epoch;
3823 string version_id;
3824
3825 protected:
3826 rgw_bucket bucket;
3827 string obj_str;
3828
3829 string unique_tag;
3830
3831 rgw_raw_obj cur_obj;
3832 RGWObjManifest manifest;
3833 RGWObjManifest::generator manifest_gen;
3834
3835 int write_data(bufferlist& bl, off_t ofs, void **phandle, rgw_raw_obj *pobj, bool exclusive);
3836 int do_complete(size_t accounted_size, const string& etag,
3837 ceph::real_time *mtime, ceph::real_time set_mtime,
3838 map<string, bufferlist>& attrs, ceph::real_time delete_at,
3839 const char *if_match, const char *if_nomatch, const string *user_data, rgw_zone_set *zones_trace) override;
3840
3841 int prepare_next_part(off_t ofs);
3842 int complete_parts();
3843 int complete_writing_data();
3844
3845 int prepare_init(RGWRados *store, string *oid_rand);
3846
3847 public:
3848 ~RGWPutObjProcessor_Atomic() override {}
3849 RGWPutObjProcessor_Atomic(RGWObjectCtx& obj_ctx, RGWBucketInfo& bucket_info,
3850 rgw_bucket& _b, const string& _o, uint64_t _p, const string& _t, bool versioned) :
3851 RGWPutObjProcessor_Aio(obj_ctx, bucket_info),
3852 part_size(_p),
3853 cur_part_ofs(0),
3854 next_part_ofs(_p),
3855 cur_part_id(0),
3856 data_ofs(0),
3857 max_chunk_size(0),
3858 versioned_object(versioned),
3859 olh_epoch(0),
3860 bucket(_b),
3861 obj_str(_o),
3862 unique_tag(_t) {}
3863 int prepare(RGWRados *store, string *oid_rand) override;
3864 virtual bool immutable_head() { return false; }
3865 int handle_data(bufferlist& bl, off_t ofs, void **phandle, rgw_raw_obj *pobj, bool *again) override;
3866
3867 void set_olh_epoch(uint64_t epoch) {
3868 olh_epoch = epoch;
3869 }
3870
3871 void set_version_id(const string& vid) {
3872 version_id = vid;
3873 }
3874 }; /* RGWPutObjProcessor_Atomic */
3875
3876 #define MP_META_SUFFIX ".meta"
3877
3878 class RGWMPObj {
3879 string oid;
3880 string prefix;
3881 string meta;
3882 string upload_id;
3883 public:
3884 RGWMPObj() {}
3885 RGWMPObj(const string& _oid, const string& _upload_id) {
3886 init(_oid, _upload_id, _upload_id);
3887 }
3888 void init(const string& _oid, const string& _upload_id) {
3889 init(_oid, _upload_id, _upload_id);
3890 }
3891 void init(const string& _oid, const string& _upload_id, const string& part_unique_str) {
3892 if (_oid.empty()) {
3893 clear();
3894 return;
3895 }
3896 oid = _oid;
3897 upload_id = _upload_id;
3898 prefix = oid + ".";
3899 meta = prefix + upload_id + MP_META_SUFFIX;
3900 prefix.append(part_unique_str);
3901 }
3902 string& get_meta() { return meta; }
3903 string get_part(int num) {
3904 char buf[16];
3905 snprintf(buf, 16, ".%d", num);
3906 string s = prefix;
3907 s.append(buf);
3908 return s;
3909 }
3910 string get_part(string& part) {
3911 string s = prefix;
3912 s.append(".");
3913 s.append(part);
3914 return s;
3915 }
3916 string& get_upload_id() {
3917 return upload_id;
3918 }
3919 string& get_key() {
3920 return oid;
3921 }
3922 bool from_meta(string& meta) {
3923 int end_pos = meta.rfind('.'); // search for ".meta"
3924 if (end_pos < 0)
3925 return false;
3926 int mid_pos = meta.rfind('.', end_pos - 1); // <key>.<upload_id>
3927 if (mid_pos < 0)
3928 return false;
3929 oid = meta.substr(0, mid_pos);
3930 upload_id = meta.substr(mid_pos + 1, end_pos - mid_pos - 1);
3931 init(oid, upload_id, upload_id);
3932 return true;
3933 }
3934 void clear() {
3935 oid = "";
3936 prefix = "";
3937 meta = "";
3938 upload_id = "";
3939 }
3940 };
3941
3942 class RGWPutObjProcessor_Multipart : public RGWPutObjProcessor_Atomic
3943 {
3944 string part_num;
3945 RGWMPObj mp;
3946 req_state *s;
3947 string upload_id;
3948
3949 protected:
3950 int prepare(RGWRados *store, string *oid_rand);
3951 int do_complete(size_t accounted_size, const string& etag,
3952 ceph::real_time *mtime, ceph::real_time set_mtime,
3953 map<string, bufferlist>& attrs, ceph::real_time delete_at,
3954 const char *if_match, const char *if_nomatch, const string *user_data,
3955 rgw_zone_set *zones_trace) override;
3956 public:
3957 bool immutable_head() { return true; }
3958 RGWPutObjProcessor_Multipart(RGWObjectCtx& obj_ctx, RGWBucketInfo& bucket_info, uint64_t _p, req_state *_s) :
3959 RGWPutObjProcessor_Atomic(obj_ctx, bucket_info, _s->bucket, _s->object.name, _p, _s->req_id, false), s(_s) {}
3960 void get_mp(RGWMPObj** _mp);
3961 }; /* RGWPutObjProcessor_Multipart */
3962 #endif