2 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
3 // vim: ts=8 sw=2 smarttab
5 #include "include/compat.h"
9 #include <boost/algorithm/string.hpp>
11 #include <boost/format.hpp>
12 #include <boost/optional.hpp>
13 #include <boost/utility/in_place_factory.hpp>
15 #include "common/ceph_json.h"
16 #include "common/utf8.h"
18 #include "common/errno.h"
19 #include "common/Formatter.h"
20 #include "common/Throttle.h"
21 #include "common/Finisher.h"
23 #include "rgw_rados.h"
24 #include "rgw_cache.h"
26 #include "rgw_acl_s3.h" /* for dumping s3policy in debug log */
27 #include "rgw_metadata.h"
28 #include "rgw_bucket.h"
29 #include "rgw_rest_conn.h"
30 #include "rgw_cr_rados.h"
31 #include "rgw_cr_rest.h"
33 #include "cls/rgw/cls_rgw_ops.h"
34 #include "cls/rgw/cls_rgw_types.h"
35 #include "cls/rgw/cls_rgw_client.h"
36 #include "cls/rgw/cls_rgw_const.h"
37 #include "cls/refcount/cls_refcount_client.h"
38 #include "cls/version/cls_version_client.h"
39 #include "cls/log/cls_log_client.h"
40 #include "cls/statelog/cls_statelog_client.h"
41 #include "cls/timeindex/cls_timeindex_client.h"
42 #include "cls/lock/cls_lock_client.h"
43 #include "cls/user/cls_user_client.h"
44 #include "osd/osd_types.h"
46 #include "rgw_tools.h"
47 #include "rgw_coroutine.h"
48 #include "rgw_compression.h"
50 #undef fork // fails to compile RGWPeriod::fork() below
52 #include "common/Clock.h"
54 #include "include/rados/librados.hpp"
55 using namespace librados
;
63 #include "auth/Crypto.h" // get_random_bytes()
70 #include "rgw_object_expirer_core.h"
72 #include "rgw_data_sync.h"
73 #include "rgw_realm_watcher.h"
74 #include "rgw_reshard.h"
76 #include "compressor/Compressor.h"
78 #define dout_context g_ceph_context
79 #define dout_subsys ceph_subsys_rgw
83 static string notify_oid_prefix
= "notify";
84 static string
*notify_oids
= NULL
;
85 static string shadow_ns
= "shadow";
86 static string dir_oid_prefix
= ".dir.";
87 static string default_storage_pool_suffix
= "rgw.buckets.data";
88 static string default_bucket_index_pool_suffix
= "rgw.buckets.index";
89 static string default_storage_extra_pool_suffix
= "rgw.buckets.non-ec";
90 static string avail_pools
= ".pools.avail";
92 static string zone_info_oid_prefix
= "zone_info.";
93 static string zone_names_oid_prefix
= "zone_names.";
94 static string region_info_oid_prefix
= "region_info.";
95 static string zone_group_info_oid_prefix
= "zonegroup_info.";
96 static string realm_names_oid_prefix
= "realms_names.";
97 static string realm_info_oid_prefix
= "realms.";
98 static string default_region_info_oid
= "default.region";
99 static string default_zone_group_info_oid
= "default.zonegroup";
100 static string period_info_oid_prefix
= "periods.";
101 static string period_latest_epoch_info_oid
= ".latest_epoch";
102 static string region_map_oid
= "region_map";
103 static string zonegroup_map_oid
= "zonegroup_map";
104 static string log_lock_name
= "rgw_log_lock";
105 static string default_realm_info_oid
= "default.realm";
106 const string default_zonegroup_name
= "default";
107 const string default_zone_name
= "default";
108 static string zonegroup_names_oid_prefix
= "zonegroups_names.";
109 static RGWObjCategory main_category
= RGW_OBJ_CATEGORY_MAIN
;
110 #define RGW_USAGE_OBJ_PREFIX "usage."
111 #define FIRST_EPOCH 1
112 static string RGW_DEFAULT_ZONE_ROOT_POOL
= "rgw.root";
113 static string RGW_DEFAULT_ZONEGROUP_ROOT_POOL
= "rgw.root";
114 static string RGW_DEFAULT_REALM_ROOT_POOL
= "rgw.root";
115 static string RGW_DEFAULT_PERIOD_ROOT_POOL
= "rgw.root";
117 #define RGW_STATELOG_OBJ_PREFIX "statelog."
119 #define dout_subsys ceph_subsys_rgw
122 static bool rgw_get_obj_data_pool(const RGWZoneGroup
& zonegroup
, const RGWZoneParams
& zone_params
,
123 const string
& placement_id
, const rgw_obj
& obj
, rgw_pool
*pool
)
125 if (!zone_params
.get_head_data_pool(placement_id
, obj
, pool
)) {
126 RGWZonePlacementInfo placement
;
127 if (!zone_params
.get_placement(zonegroup
.default_placement
, &placement
)) {
131 if (!obj
.in_extra_data
) {
132 *pool
= placement
.data_pool
;
134 *pool
= placement
.get_data_extra_pool();
141 static bool rgw_obj_to_raw(const RGWZoneGroup
& zonegroup
, const RGWZoneParams
& zone_params
,
142 const string
& placement_id
, const rgw_obj
& obj
, rgw_raw_obj
*raw_obj
)
144 get_obj_bucket_and_oid_loc(obj
, raw_obj
->oid
, raw_obj
->loc
);
146 return rgw_get_obj_data_pool(zonegroup
, zone_params
, placement_id
, obj
, &raw_obj
->pool
);
149 rgw_raw_obj
rgw_obj_select::get_raw_obj(const RGWZoneGroup
& zonegroup
, const RGWZoneParams
& zone_params
) const
153 rgw_obj_to_raw(zonegroup
, zone_params
, placement_rule
, obj
, &r
);
159 rgw_raw_obj
rgw_obj_select::get_raw_obj(RGWRados
*store
) const
163 store
->obj_to_raw(placement_rule
, obj
, &r
);
169 int rgw_init_ioctx(librados::Rados
*rados
, const rgw_pool
& pool
, IoCtx
& ioctx
, bool create
)
171 int r
= rados
->ioctx_create(pool
.name
.c_str(), ioctx
);
172 if (r
== -ENOENT
&& create
) {
173 r
= rados
->pool_create(pool
.name
.c_str());
174 if (r
< 0 && r
!= -EEXIST
) {
178 r
= rados
->ioctx_create(pool
.name
.c_str(), ioctx
);
183 r
= ioctx
.application_enable(pg_pool_t::APPLICATION_NAME_RGW
, false);
184 if (r
< 0 && r
!= -EOPNOTSUPP
) {
190 if (!pool
.ns
.empty()) {
191 ioctx
.set_namespace(pool
.ns
);
197 void RGWObjectCtxImpl
<rgw_obj
, RGWObjState
>::invalidate(rgw_obj
& obj
) {
198 RWLock::WLocker
wl(lock
);
199 auto iter
= objs_state
.find(obj
);
200 if (iter
== objs_state
.end()) {
203 bool is_atomic
= iter
->second
.is_atomic
;
204 bool prefetch_data
= iter
->second
.prefetch_data
;
206 objs_state
.erase(iter
);
208 if (is_atomic
|| prefetch_data
) {
209 auto& s
= objs_state
[obj
];
210 s
.is_atomic
= is_atomic
;
211 s
.prefetch_data
= prefetch_data
;
216 void RGWObjectCtxImpl
<rgw_raw_obj
, RGWRawObjState
>::invalidate(rgw_raw_obj
& obj
) {
217 RWLock::WLocker
wl(lock
);
218 auto iter
= objs_state
.find(obj
);
219 if (iter
== objs_state
.end()) {
223 objs_state
.erase(iter
);
226 void RGWDefaultZoneGroupInfo::dump(Formatter
*f
) const {
227 encode_json("default_zonegroup", default_zonegroup
, f
);
230 void RGWDefaultZoneGroupInfo::decode_json(JSONObj
*obj
) {
232 JSONDecoder::decode_json("default_zonegroup", default_zonegroup
, obj
);
233 /* backward compatability with region */
234 if (default_zonegroup
.empty()) {
235 JSONDecoder::decode_json("default_region", default_zonegroup
, obj
);
239 rgw_pool
RGWZoneGroup::get_pool(CephContext
*cct_
)
241 if (cct_
->_conf
->rgw_zonegroup_root_pool
.empty()) {
242 return rgw_pool(RGW_DEFAULT_ZONEGROUP_ROOT_POOL
);
245 return rgw_pool(cct_
->_conf
->rgw_zonegroup_root_pool
);
248 int RGWZoneGroup::create_default(bool old_format
)
250 name
= default_zonegroup_name
;
253 RGWZoneGroupPlacementTarget placement_target
;
254 placement_target
.name
= "default-placement";
255 placement_targets
[placement_target
.name
] = placement_target
;
256 default_placement
= "default-placement";
258 RGWZoneParams
zone_params(default_zone_name
);
260 int r
= zone_params
.init(cct
, store
, false);
262 ldout(cct
, 0) << "create_default: error initializing zone params: " << cpp_strerror(-r
) << dendl
;
266 r
= zone_params
.create_default();
267 if (r
< 0 && r
!= -EEXIST
) {
268 ldout(cct
, 0) << "create_default: error in create_default zone params: " << cpp_strerror(-r
) << dendl
;
270 } else if (r
== -EEXIST
) {
271 ldout(cct
, 10) << "zone_params::create_default() returned -EEXIST, we raced with another default zone_params creation" << dendl
;
272 zone_params
.clear_id();
273 r
= zone_params
.init(cct
, store
);
275 ldout(cct
, 0) << "create_default: error in init existing zone params: " << cpp_strerror(-r
) << dendl
;
278 ldout(cct
, 20) << "zone_params::create_default() " << zone_params
.get_name() << " id " << zone_params
.get_id()
282 RGWZone
& default_zone
= zones
[zone_params
.get_id()];
283 default_zone
.name
= zone_params
.get_name();
284 default_zone
.id
= zone_params
.get_id();
285 master_zone
= default_zone
.id
;
288 if (r
< 0 && r
!= -EEXIST
) {
289 ldout(cct
, 0) << "error storing zone group info: " << cpp_strerror(-r
) << dendl
;
294 ldout(cct
, 10) << "create_default() returned -EEXIST, we raced with another zonegroup creation" << dendl
;
296 r
= init(cct
, store
);
306 post_process_params();
311 const string
RGWZoneGroup::get_default_oid(bool old_region_format
)
313 if (old_region_format
) {
314 if (cct
->_conf
->rgw_default_region_info_oid
.empty()) {
315 return default_region_info_oid
;
317 return cct
->_conf
->rgw_default_region_info_oid
;
320 string default_oid
= cct
->_conf
->rgw_default_zonegroup_info_oid
;
322 if (cct
->_conf
->rgw_default_zonegroup_info_oid
.empty()) {
323 default_oid
= default_zone_group_info_oid
;
326 default_oid
+= "." + realm_id
;
331 const string
& RGWZoneGroup::get_info_oid_prefix(bool old_region_format
)
333 if (old_region_format
) {
334 return region_info_oid_prefix
;
336 return zone_group_info_oid_prefix
;
339 const string
& RGWZoneGroup::get_names_oid_prefix()
341 return zonegroup_names_oid_prefix
;
344 const string
& RGWZoneGroup::get_predefined_name(CephContext
*cct
) {
345 return cct
->_conf
->rgw_zonegroup
;
348 int RGWZoneGroup::equals(const string
& other_zonegroup
) const
350 if (is_master
&& other_zonegroup
.empty())
353 return (id
== other_zonegroup
);
356 int RGWZoneGroup::add_zone(const RGWZoneParams
& zone_params
, bool *is_master
, bool *read_only
,
357 const list
<string
>& endpoints
, const string
*ptier_type
,
358 bool *psync_from_all
, list
<string
>& sync_from
, list
<string
>& sync_from_rm
)
360 auto& zone_id
= zone_params
.get_id();
361 auto& zone_name
= zone_params
.get_name();
363 // check for duplicate zone name on insert
364 if (!zones
.count(zone_id
)) {
365 for (const auto& zone
: zones
) {
366 if (zone
.second
.name
== zone_name
) {
367 ldout(cct
, 0) << "ERROR: found existing zone name " << zone_name
368 << " (" << zone
.first
<< ") in zonegroup " << get_name() << dendl
;
376 if (!master_zone
.empty() && master_zone
!= zone_params
.get_id()) {
377 ldout(cct
, 0) << "NOTICE: overriding master zone: " << master_zone
<< dendl
;
379 master_zone
= zone_params
.get_id();
380 } else if (master_zone
== zone_params
.get_id()) {
385 RGWZone
& zone
= zones
[zone_params
.get_id()];
386 zone
.name
= zone_params
.get_name();
387 zone
.id
= zone_params
.get_id();
388 if (!endpoints
.empty()) {
389 zone
.endpoints
= endpoints
;
392 zone
.read_only
= *read_only
;
395 zone
.tier_type
= *ptier_type
;
398 if (psync_from_all
) {
399 zone
.sync_from_all
= *psync_from_all
;
402 for (auto add
: sync_from
) {
403 zone
.sync_from
.insert(add
);
406 for (auto rm
: sync_from_rm
) {
407 zone
.sync_from
.erase(rm
);
410 post_process_params();
416 int RGWZoneGroup::rename_zone(const RGWZoneParams
& zone_params
)
418 RGWZone
& zone
= zones
[zone_params
.get_id()];
419 zone
.name
= zone_params
.get_name();
424 void RGWZoneGroup::post_process_params()
426 bool log_data
= zones
.size() > 1;
428 if (master_zone
.empty()) {
429 map
<string
, RGWZone
>::iterator iter
= zones
.begin();
430 if (iter
!= zones
.end()) {
431 master_zone
= iter
->first
;
435 for (map
<string
, RGWZone
>::iterator iter
= zones
.begin(); iter
!= zones
.end(); ++iter
) {
436 RGWZone
& zone
= iter
->second
;
437 zone
.log_data
= log_data
;
439 RGWZoneParams
zone_params(zone
.id
, zone
.name
);
440 int ret
= zone_params
.init(cct
, store
);
442 ldout(cct
, 0) << "WARNING: could not read zone params for zone id=" << zone
.id
<< " name=" << zone
.name
<< dendl
;
446 for (map
<string
, RGWZonePlacementInfo
>::iterator iter
= zone_params
.placement_pools
.begin();
447 iter
!= zone_params
.placement_pools
.end(); ++iter
) {
448 const string
& placement_name
= iter
->first
;
449 if (placement_targets
.find(placement_name
) == placement_targets
.end()) {
450 RGWZoneGroupPlacementTarget placement_target
;
451 placement_target
.name
= placement_name
;
452 placement_targets
[placement_name
] = placement_target
;
457 if (default_placement
.empty() && !placement_targets
.empty()) {
458 default_placement
= placement_targets
.begin()->first
;
462 int RGWZoneGroup::remove_zone(const std::string
& zone_id
)
464 map
<string
, RGWZone
>::iterator iter
= zones
.find(zone_id
);
465 if (iter
== zones
.end()) {
466 ldout(cct
, 0) << "zone id " << zone_id
<< " is not a part of zonegroup "
473 post_process_params();
478 int RGWZoneGroup::read_default_id(string
& default_id
, bool old_format
)
480 if (realm_id
.empty()) {
481 /* try using default realm */
483 int ret
= realm
.init(cct
, store
);
485 ldout(cct
, 10) << "could not read realm id: " << cpp_strerror(-ret
) << dendl
;
488 realm_id
= realm
.get_id();
491 return RGWSystemMetaObj::read_default_id(default_id
, old_format
);
494 int RGWZoneGroup::set_as_default(bool exclusive
)
496 if (realm_id
.empty()) {
497 /* try using default realm */
499 int ret
= realm
.init(cct
, store
);
501 ldout(cct
, 10) << "could not read realm id: " << cpp_strerror(-ret
) << dendl
;
504 realm_id
= realm
.get_id();
507 return RGWSystemMetaObj::set_as_default(exclusive
);
510 int RGWSystemMetaObj::init(CephContext
*_cct
, RGWRados
*_store
, bool setup_obj
, bool old_format
)
518 if (old_format
&& id
.empty()) {
525 name
= get_predefined_name(cct
);
528 r
= use_default(old_format
);
532 } else if (!old_format
) {
533 r
= read_id(name
, id
);
536 ldout(cct
, 0) << "error in read_id for object name: " << name
<< " : " << cpp_strerror(-r
) << dendl
;
543 return read_info(id
, old_format
);
546 int RGWSystemMetaObj::read_default(RGWDefaultSystemMetaObjInfo
& default_info
, const string
& oid
)
548 auto pool
= get_pool(cct
);
550 RGWObjectCtx
obj_ctx(store
);
551 int ret
= rgw_get_system_obj(store
, obj_ctx
, pool
, oid
, bl
, NULL
, NULL
);
556 bufferlist::iterator iter
= bl
.begin();
557 ::decode(default_info
, iter
);
558 } catch (buffer::error
& err
) {
559 ldout(cct
, 0) << "error decoding data from " << pool
<< ":" << oid
<< dendl
;
566 int RGWSystemMetaObj::read_default_id(string
& default_id
, bool old_format
)
568 RGWDefaultSystemMetaObjInfo default_info
;
570 int ret
= read_default(default_info
, get_default_oid(old_format
));
575 default_id
= default_info
.default_id
;
580 int RGWSystemMetaObj::use_default(bool old_format
)
582 return read_default_id(id
, old_format
);
585 int RGWSystemMetaObj::set_as_default(bool exclusive
)
587 string oid
= get_default_oid();
589 rgw_pool
pool(get_pool(cct
));
592 RGWDefaultSystemMetaObjInfo default_info
;
593 default_info
.default_id
= id
;
595 ::encode(default_info
, bl
);
597 int ret
= rgw_put_system_obj(store
, pool
, oid
, bl
.c_str(), bl
.length(),
598 exclusive
, NULL
, real_time(), NULL
);
605 int RGWSystemMetaObj::read_id(const string
& obj_name
, string
& object_id
)
607 rgw_pool
pool(get_pool(cct
));
610 string oid
= get_names_oid_prefix() + obj_name
;
612 RGWObjectCtx
obj_ctx(store
);
613 int ret
= rgw_get_system_obj(store
, obj_ctx
, pool
, oid
, bl
, NULL
, NULL
);
618 RGWNameToId nameToId
;
620 bufferlist::iterator iter
= bl
.begin();
621 ::decode(nameToId
, iter
);
622 } catch (buffer::error
& err
) {
623 ldout(cct
, 0) << "ERROR: failed to decode obj from " << pool
<< ":" << oid
<< dendl
;
626 object_id
= nameToId
.obj_id
;
630 int RGWSystemMetaObj::delete_obj(bool old_format
)
632 rgw_pool
pool(get_pool(cct
));
634 /* check to see if obj is the default */
635 RGWDefaultSystemMetaObjInfo default_info
;
636 int ret
= read_default(default_info
, get_default_oid(old_format
));
637 if (ret
< 0 && ret
!= -ENOENT
)
639 if (default_info
.default_id
== id
|| (old_format
&& default_info
.default_id
== name
)) {
640 string oid
= get_default_oid(old_format
);
641 rgw_raw_obj
default_named_obj(pool
, oid
);
642 ret
= store
->delete_system_obj(default_named_obj
);
644 ldout(cct
, 0) << "Error delete default obj name " << name
<< ": " << cpp_strerror(-ret
) << dendl
;
649 string oid
= get_names_oid_prefix() + name
;
650 rgw_raw_obj
object_name(pool
, oid
);
651 ret
= store
->delete_system_obj(object_name
);
653 ldout(cct
, 0) << "Error delete obj name " << name
<< ": " << cpp_strerror(-ret
) << dendl
;
658 string oid
= get_info_oid_prefix(old_format
);
665 rgw_raw_obj
object_id(pool
, oid
);
666 ret
= store
->delete_system_obj(object_id
);
668 ldout(cct
, 0) << "Error delete object id " << id
<< ": " << cpp_strerror(-ret
) << dendl
;
674 int RGWSystemMetaObj::store_name(bool exclusive
)
676 rgw_pool
pool(get_pool(cct
));
677 string oid
= get_names_oid_prefix() + name
;
679 RGWNameToId nameToId
;
680 nameToId
.obj_id
= id
;
683 ::encode(nameToId
, bl
);
684 return rgw_put_system_obj(store
, pool
, oid
, bl
.c_str(), bl
.length(), exclusive
, NULL
, real_time(), NULL
);
687 int RGWSystemMetaObj::rename(const string
& new_name
)
690 int ret
= read_id(new_name
, new_id
);
694 if (ret
< 0 && ret
!= -ENOENT
) {
695 ldout(cct
, 0) << "Error read_id " << new_name
<< ": " << cpp_strerror(-ret
) << dendl
;
698 string old_name
= name
;
702 ldout(cct
, 0) << "Error storing new obj info " << new_name
<< ": " << cpp_strerror(-ret
) << dendl
;
705 ret
= store_name(true);
707 ldout(cct
, 0) << "Error storing new name " << new_name
<< ": " << cpp_strerror(-ret
) << dendl
;
710 /* delete old name */
711 rgw_pool
pool(get_pool(cct
));
712 string oid
= get_names_oid_prefix() + old_name
;
713 rgw_raw_obj
old_name_obj(pool
, oid
);
714 ret
= store
->delete_system_obj(old_name_obj
);
716 ldout(cct
, 0) << "Error delete old obj name " << old_name
<< ": " << cpp_strerror(-ret
) << dendl
;
723 int RGWSystemMetaObj::read_info(const string
& obj_id
, bool old_format
)
725 rgw_pool
pool(get_pool(cct
));
729 string oid
= get_info_oid_prefix(old_format
) + obj_id
;
731 RGWObjectCtx
obj_ctx(store
);
732 int ret
= rgw_get_system_obj(store
, obj_ctx
, pool
, oid
, bl
, NULL
, NULL
);
734 ldout(cct
, 0) << "failed reading obj info from " << pool
<< ":" << oid
<< ": " << cpp_strerror(-ret
) << dendl
;
739 bufferlist::iterator iter
= bl
.begin();
740 ::decode(*this, iter
);
741 } catch (buffer::error
& err
) {
742 ldout(cct
, 0) << "ERROR: failed to decode obj from " << pool
<< ":" << oid
<< dendl
;
749 int RGWSystemMetaObj::read()
751 int ret
= read_id(name
, id
);
756 return read_info(id
);
759 int RGWSystemMetaObj::create(bool exclusive
)
763 /* check to see the name is not used */
764 ret
= read_id(name
, id
);
765 if (exclusive
&& ret
== 0) {
766 ldout(cct
, 10) << "ERROR: name " << name
<< " already in use for obj id " << id
<< dendl
;
768 } else if ( ret
< 0 && ret
!= -ENOENT
) {
769 ldout(cct
, 0) << "failed reading obj id " << id
<< ": " << cpp_strerror(-ret
) << dendl
;
774 /* create unique id */
777 new_uuid
.generate_random();
778 new_uuid
.print(uuid_str
);
782 ret
= store_info(exclusive
);
784 ldout(cct
, 0) << "ERROR: storing info for " << id
<< ": " << cpp_strerror(-ret
) << dendl
;
788 return store_name(exclusive
);
791 int RGWSystemMetaObj::store_info(bool exclusive
)
793 rgw_pool
pool(get_pool(cct
));
795 string oid
= get_info_oid_prefix() + id
;
799 return rgw_put_system_obj(store
, pool
, oid
, bl
.c_str(), bl
.length(), exclusive
, NULL
, real_time(), NULL
);
802 int RGWSystemMetaObj::write(bool exclusive
)
804 int ret
= store_info(exclusive
);
806 ldout(cct
, 20) << __func__
<< "(): store_info() returned ret=" << ret
<< dendl
;
809 ret
= store_name(exclusive
);
811 ldout(cct
, 20) << __func__
<< "(): store_name() returned ret=" << ret
<< dendl
;
818 const string
& RGWRealm::get_predefined_name(CephContext
*cct
) {
819 return cct
->_conf
->rgw_realm
;
822 int RGWRealm::create(bool exclusive
)
824 int ret
= RGWSystemMetaObj::create(exclusive
);
826 ldout(cct
, 0) << "ERROR creating new realm object " << name
<< ": " << cpp_strerror(-ret
) << dendl
;
829 // create the control object for watch/notify
830 ret
= create_control(exclusive
);
832 ldout(cct
, 0) << "ERROR creating control for new realm " << name
<< ": " << cpp_strerror(-ret
) << dendl
;
836 if (current_period
.empty()) {
837 /* create new period for the realm */
838 ret
= period
.init(cct
, store
, id
, name
, false);
842 ret
= period
.create(true);
844 ldout(cct
, 0) << "ERROR: creating new period for realm " << name
<< ": " << cpp_strerror(-ret
) << dendl
;
848 period
= RGWPeriod(current_period
, 0);
849 int ret
= period
.init(cct
, store
, id
, name
);
851 ldout(cct
, 0) << "ERROR: failed to init period " << current_period
<< dendl
;
855 ret
= set_current_period(period
);
857 ldout(cct
, 0) << "ERROR: failed set current period " << current_period
<< dendl
;
860 // try to set as default. may race with another create, so pass exclusive=true
861 // so we don't override an existing default
862 ret
= set_as_default(true);
863 if (ret
< 0 && ret
!= -EEXIST
) {
864 ldout(cct
, 0) << "WARNING: failed to set realm as default realm, ret=" << ret
<< dendl
;
870 int RGWRealm::delete_obj()
872 int ret
= RGWSystemMetaObj::delete_obj();
876 return delete_control();
879 int RGWRealm::create_control(bool exclusive
)
881 auto pool
= rgw_pool
{get_pool(cct
)};
882 auto oid
= get_control_oid();
883 return rgw_put_system_obj(store
, pool
, oid
, nullptr, 0, exclusive
,
884 nullptr, real_time(), nullptr);
887 int RGWRealm::delete_control()
889 auto pool
= rgw_pool
{get_pool(cct
)};
890 auto obj
= rgw_raw_obj
{pool
, get_control_oid()};
891 return store
->delete_system_obj(obj
);
894 rgw_pool
RGWRealm::get_pool(CephContext
*cct
)
896 if (cct
->_conf
->rgw_realm_root_pool
.empty()) {
897 return rgw_pool(RGW_DEFAULT_REALM_ROOT_POOL
);
899 return rgw_pool(cct
->_conf
->rgw_realm_root_pool
);
902 const string
RGWRealm::get_default_oid(bool old_format
)
904 if (cct
->_conf
->rgw_default_realm_info_oid
.empty()) {
905 return default_realm_info_oid
;
907 return cct
->_conf
->rgw_default_realm_info_oid
;
910 const string
& RGWRealm::get_names_oid_prefix()
912 return realm_names_oid_prefix
;
915 const string
& RGWRealm::get_info_oid_prefix(bool old_format
)
917 return realm_info_oid_prefix
;
920 int RGWRealm::set_current_period(RGWPeriod
& period
)
922 // update realm epoch to match the period's
923 if (epoch
> period
.get_realm_epoch()) {
924 ldout(cct
, 0) << "ERROR: set_current_period with old realm epoch "
925 << period
.get_realm_epoch() << ", current epoch=" << epoch
<< dendl
;
928 if (epoch
== period
.get_realm_epoch() && current_period
!= period
.get_id()) {
929 ldout(cct
, 0) << "ERROR: set_current_period with same realm epoch "
930 << period
.get_realm_epoch() << ", but different period id "
931 << period
.get_id() << " != " << current_period
<< dendl
;
935 epoch
= period
.get_realm_epoch();
936 current_period
= period
.get_id();
940 ldout(cct
, 0) << "ERROR: period update: " << cpp_strerror(-ret
) << dendl
;
944 ret
= period
.reflect();
946 ldout(cct
, 0) << "ERROR: period.reflect(): " << cpp_strerror(-ret
) << dendl
;
953 string
RGWRealm::get_control_oid()
955 return get_info_oid_prefix() + id
+ ".control";
958 int RGWRealm::notify_zone(bufferlist
& bl
)
960 // open a context on the realm's pool
961 rgw_pool pool
{get_pool(cct
)};
963 int r
= rgw_init_ioctx(store
->get_rados_handle(), pool
, ctx
);
965 ldout(cct
, 0) << "Failed to open pool " << pool
<< dendl
;
968 // send a notify on the realm object
969 r
= ctx
.notify2(get_control_oid(), bl
, 0, nullptr);
971 ldout(cct
, 0) << "Realm notify failed with " << r
<< dendl
;
977 int RGWRealm::notify_new_period(const RGWPeriod
& period
)
980 // push the period to dependent zonegroups/zones
981 ::encode(RGWRealmNotify::ZonesNeedPeriod
, bl
);
982 ::encode(period
, bl
);
983 // reload the gateway with the new period
984 ::encode(RGWRealmNotify::Reload
, bl
);
986 return notify_zone(bl
);
989 std::string
RGWPeriodConfig::get_oid(const std::string
& realm_id
)
991 if (realm_id
.empty()) {
992 return "period_config.default";
994 return "period_config." + realm_id
;
997 rgw_pool
RGWPeriodConfig::get_pool(CephContext
*cct
)
999 const auto& pool_name
= cct
->_conf
->rgw_period_root_pool
;
1000 if (pool_name
.empty()) {
1001 return {RGW_DEFAULT_PERIOD_ROOT_POOL
};
1006 int RGWPeriodConfig::read(RGWRados
*store
, const std::string
& realm_id
)
1008 RGWObjectCtx
obj_ctx(store
);
1009 const auto& pool
= get_pool(store
->ctx());
1010 const auto& oid
= get_oid(realm_id
);
1013 int ret
= rgw_get_system_obj(store
, obj_ctx
, pool
, oid
, bl
, nullptr, nullptr);
1018 bufferlist::iterator iter
= bl
.begin();
1019 ::decode(*this, iter
);
1020 } catch (buffer::error
& err
) {
1026 int RGWPeriodConfig::write(RGWRados
*store
, const std::string
& realm_id
)
1028 const auto& pool
= get_pool(store
->ctx());
1029 const auto& oid
= get_oid(realm_id
);
1031 ::encode(*this, bl
);
1032 return rgw_put_system_obj(store
, pool
, oid
, bl
.c_str(), bl
.length(),
1033 false, nullptr, real_time(), nullptr);
1036 int RGWPeriod::init(CephContext
*_cct
, RGWRados
*_store
, const string
& period_realm_id
,
1037 const string
& period_realm_name
, bool setup_obj
)
1041 realm_id
= period_realm_id
;
1042 realm_name
= period_realm_name
;
1047 return init(_cct
, _store
, setup_obj
);
1051 int RGWPeriod::init(CephContext
*_cct
, RGWRados
*_store
, bool setup_obj
)
1060 RGWRealm
realm(realm_id
, realm_name
);
1061 int ret
= realm
.init(cct
, store
);
1063 ldout(cct
, 0) << "RGWPeriod::init failed to init realm " << realm_name
<< " id " << realm_id
<< " : " <<
1064 cpp_strerror(-ret
) << dendl
;
1067 id
= realm
.get_current_period();
1068 realm_id
= realm
.get_id();
1072 int ret
= use_latest_epoch();
1074 ldout(cct
, 0) << "failed to use_latest_epoch period id " << id
<< " realm " << realm_name
<< " id " << realm_id
1075 << " : " << cpp_strerror(-ret
) << dendl
;
1084 int RGWPeriod::get_zonegroup(RGWZoneGroup
& zonegroup
, const string
& zonegroup_id
) {
1085 map
<string
, RGWZoneGroup
>::const_iterator iter
;
1086 if (!zonegroup_id
.empty()) {
1087 iter
= period_map
.zonegroups
.find(zonegroup_id
);
1089 iter
= period_map
.zonegroups
.find("default");
1091 if (iter
!= period_map
.zonegroups
.end()) {
1092 zonegroup
= iter
->second
;
1099 const string
& RGWPeriod::get_latest_epoch_oid()
1101 if (cct
->_conf
->rgw_period_latest_epoch_info_oid
.empty()) {
1102 return period_latest_epoch_info_oid
;
1104 return cct
->_conf
->rgw_period_latest_epoch_info_oid
;
1107 const string
& RGWPeriod::get_info_oid_prefix()
1109 return period_info_oid_prefix
;
1112 const string
RGWPeriod::get_period_oid_prefix()
1114 return get_info_oid_prefix() + id
;
1117 const string
RGWPeriod::get_period_oid()
1119 std::ostringstream oss
;
1120 oss
<< get_period_oid_prefix();
1121 // skip the epoch for the staging period
1122 if (id
!= get_staging_id(realm_id
))
1123 oss
<< "." << epoch
;
1127 int RGWPeriod::read_latest_epoch(RGWPeriodLatestEpochInfo
& info
,
1128 RGWObjVersionTracker
*objv
)
1130 string oid
= get_period_oid_prefix() + get_latest_epoch_oid();
1132 rgw_pool
pool(get_pool(cct
));
1134 RGWObjectCtx
obj_ctx(store
);
1135 int ret
= rgw_get_system_obj(store
, obj_ctx
, pool
, oid
, bl
, objv
, nullptr);
1137 ldout(cct
, 1) << "error read_lastest_epoch " << pool
<< ":" << oid
<< dendl
;
1141 bufferlist::iterator iter
= bl
.begin();
1142 ::decode(info
, iter
);
1143 } catch (buffer::error
& err
) {
1144 ldout(cct
, 0) << "error decoding data from " << pool
<< ":" << oid
<< dendl
;
1151 int RGWPeriod::get_latest_epoch(epoch_t
& latest_epoch
)
1153 RGWPeriodLatestEpochInfo info
;
1155 int ret
= read_latest_epoch(info
);
1160 latest_epoch
= info
.epoch
;
1165 int RGWPeriod::use_latest_epoch()
1167 RGWPeriodLatestEpochInfo info
;
1168 int ret
= read_latest_epoch(info
);
1178 int RGWPeriod::set_latest_epoch(epoch_t epoch
, bool exclusive
,
1179 RGWObjVersionTracker
*objv
)
1181 string oid
= get_period_oid_prefix() + get_latest_epoch_oid();
1183 rgw_pool
pool(get_pool(cct
));
1186 RGWPeriodLatestEpochInfo info
;
1191 return rgw_put_system_obj(store
, pool
, oid
, bl
.c_str(), bl
.length(),
1192 exclusive
, objv
, real_time(), nullptr);
1195 int RGWPeriod::update_latest_epoch(epoch_t epoch
)
1197 static constexpr int MAX_RETRIES
= 20;
1199 for (int i
= 0; i
< MAX_RETRIES
; i
++) {
1200 RGWPeriodLatestEpochInfo info
;
1201 RGWObjVersionTracker objv
;
1202 bool exclusive
= false;
1204 // read existing epoch
1205 int r
= read_latest_epoch(info
, &objv
);
1207 // use an exclusive create to set the epoch atomically
1209 ldout(cct
, 20) << "creating initial latest_epoch=" << epoch
1210 << " for period=" << id
<< dendl
;
1212 ldout(cct
, 0) << "ERROR: failed to read latest_epoch" << dendl
;
1214 } else if (epoch
<= info
.epoch
) {
1215 r
= -EEXIST
; // fail with EEXIST if epoch is not newer
1216 ldout(cct
, 1) << "found existing latest_epoch " << info
.epoch
1217 << " >= given epoch " << epoch
<< ", returning r=" << r
<< dendl
;
1220 ldout(cct
, 20) << "updating latest_epoch from " << info
.epoch
1221 << " -> " << epoch
<< " on period=" << id
<< dendl
;
1224 r
= set_latest_epoch(epoch
, exclusive
, &objv
);
1226 continue; // exclusive create raced with another update, retry
1227 } else if (r
== -ECANCELED
) {
1228 continue; // write raced with a conflicting version, retry
1231 ldout(cct
, 0) << "ERROR: failed to write latest_epoch" << dendl
;
1234 return 0; // return success
1237 return -ECANCELED
; // fail after max retries
1240 int RGWPeriod::delete_obj()
1242 rgw_pool
pool(get_pool(cct
));
1244 // delete the object for each period epoch
1245 for (epoch_t e
= 1; e
<= epoch
; e
++) {
1246 RGWPeriod p
{get_id(), e
};
1247 rgw_raw_obj oid
{pool
, p
.get_period_oid()};
1248 int ret
= store
->delete_system_obj(oid
);
1250 ldout(cct
, 0) << "WARNING: failed to delete period object " << oid
1251 << ": " << cpp_strerror(-ret
) << dendl
;
1255 // delete the .latest_epoch object
1256 rgw_raw_obj oid
{pool
, get_period_oid_prefix() + get_latest_epoch_oid()};
1257 int ret
= store
->delete_system_obj(oid
);
1259 ldout(cct
, 0) << "WARNING: failed to delete period object " << oid
1260 << ": " << cpp_strerror(-ret
) << dendl
;
1265 int RGWPeriod::read_info()
1267 rgw_pool
pool(get_pool(cct
));
1271 RGWObjectCtx
obj_ctx(store
);
1272 int ret
= rgw_get_system_obj(store
, obj_ctx
, pool
, get_period_oid(), bl
, NULL
, NULL
);
1274 ldout(cct
, 0) << "failed reading obj info from " << pool
<< ":" << get_period_oid() << ": " << cpp_strerror(-ret
) << dendl
;
1279 bufferlist::iterator iter
= bl
.begin();
1280 ::decode(*this, iter
);
1281 } catch (buffer::error
& err
) {
1282 ldout(cct
, 0) << "ERROR: failed to decode obj from " << pool
<< ":" << get_period_oid() << dendl
;
1289 int RGWPeriod::create(bool exclusive
)
1293 /* create unique id */
1296 new_uuid
.generate_random();
1297 new_uuid
.print(uuid_str
);
1300 epoch
= FIRST_EPOCH
;
1304 ret
= store_info(exclusive
);
1306 ldout(cct
, 0) << "ERROR: storing info for " << id
<< ": " << cpp_strerror(-ret
) << dendl
;
1310 ret
= set_latest_epoch(epoch
);
1312 ldout(cct
, 0) << "ERROR: setting latest epoch " << id
<< ": " << cpp_strerror(-ret
) << dendl
;
1318 int RGWPeriod::store_info(bool exclusive
)
1320 rgw_pool
pool(get_pool(cct
));
1322 string oid
= get_period_oid();
1324 ::encode(*this, bl
);
1326 return rgw_put_system_obj(store
, pool
, oid
, bl
.c_str(), bl
.length(),
1327 exclusive
, NULL
, real_time(), NULL
);
1330 rgw_pool
RGWPeriod::get_pool(CephContext
*cct
)
1332 if (cct
->_conf
->rgw_period_root_pool
.empty()) {
1333 return rgw_pool(RGW_DEFAULT_PERIOD_ROOT_POOL
);
1335 return rgw_pool(cct
->_conf
->rgw_period_root_pool
);
1338 int RGWPeriod::add_zonegroup(const RGWZoneGroup
& zonegroup
)
1340 if (zonegroup
.realm_id
!= realm_id
) {
1343 int ret
= period_map
.update(zonegroup
, cct
);
1345 ldout(cct
, 0) << "ERROR: updating period map: " << cpp_strerror(-ret
) << dendl
;
1349 return store_info(false);
1352 int RGWPeriod::update()
1354 ldout(cct
, 20) << __func__
<< " realm " << realm_id
<< " period " << get_id() << dendl
;
1355 list
<string
> zonegroups
;
1356 int ret
= store
->list_zonegroups(zonegroups
);
1358 ldout(cct
, 0) << "ERROR: failed to list zonegroups: " << cpp_strerror(-ret
) << dendl
;
1362 // clear zone short ids of removed zones. period_map.update() will add the
1363 // remaining zones back
1364 period_map
.short_zone_ids
.clear();
1366 for (auto& iter
: zonegroups
) {
1367 RGWZoneGroup
zg(string(), iter
);
1368 ret
= zg
.init(cct
, store
);
1370 ldout(cct
, 0) << "WARNING: zg.init() failed: " << cpp_strerror(-ret
) << dendl
;
1374 if (zg
.realm_id
!= realm_id
) {
1375 ldout(cct
, 20) << "skipping zonegroup " << zg
.get_name() << " zone realm id " << zg
.realm_id
<< ", not on our realm " << realm_id
<< dendl
;
1379 if (zg
.master_zone
.empty()) {
1380 ldout(cct
, 0) << "ERROR: zonegroup " << zg
.get_name() << " should have a master zone " << dendl
;
1384 if (zg
.is_master_zonegroup()) {
1385 master_zonegroup
= zg
.get_id();
1386 master_zone
= zg
.master_zone
;
1389 int ret
= period_map
.update(zg
, cct
);
1395 ret
= period_config
.read(store
, realm_id
);
1396 if (ret
< 0 && ret
!= -ENOENT
) {
1397 ldout(cct
, 0) << "ERROR: failed to read period config: "
1398 << cpp_strerror(ret
) << dendl
;
1404 int RGWPeriod::reflect()
1406 for (auto& iter
: period_map
.zonegroups
) {
1407 RGWZoneGroup
& zg
= iter
.second
;
1408 zg
.reinit_instance(cct
, store
);
1409 int r
= zg
.write(false);
1411 ldout(cct
, 0) << "ERROR: failed to store zonegroup info for zonegroup=" << iter
.first
<< ": " << cpp_strerror(-r
) << dendl
;
1414 if (zg
.is_master_zonegroup()) {
1415 // set master as default if no default exists
1416 r
= zg
.set_as_default(true);
1418 ldout(cct
, 1) << "Set the period's master zonegroup " << zg
.get_id()
1419 << " as the default" << dendl
;
1424 int r
= period_config
.write(store
, realm_id
);
1426 ldout(cct
, 0) << "ERROR: failed to store period config: "
1427 << cpp_strerror(-r
) << dendl
;
1433 void RGWPeriod::fork()
1435 ldout(cct
, 20) << __func__
<< " realm " << realm_id
<< " period " << id
<< dendl
;
1436 predecessor_uuid
= id
;
1437 id
= get_staging_id(realm_id
);
1442 static int read_sync_status(RGWRados
*store
, rgw_meta_sync_status
*sync_status
)
1444 // initialize a sync status manager to read the status
1445 RGWMetaSyncStatusManager
mgr(store
, store
->get_async_rados());
1450 r
= mgr
.read_sync_status(sync_status
);
1455 int RGWPeriod::update_sync_status(const RGWPeriod
¤t_period
,
1456 std::ostream
& error_stream
,
1457 bool force_if_stale
)
1459 rgw_meta_sync_status status
;
1460 int r
= read_sync_status(store
, &status
);
1462 ldout(cct
, 0) << "period failed to read sync status: "
1463 << cpp_strerror(-r
) << dendl
;
1467 std::vector
<std::string
> markers
;
1469 const auto current_epoch
= current_period
.get_realm_epoch();
1470 if (current_epoch
!= status
.sync_info
.realm_epoch
) {
1471 // no sync status markers for the current period
1472 assert(current_epoch
> status
.sync_info
.realm_epoch
);
1473 const int behind
= current_epoch
- status
.sync_info
.realm_epoch
;
1474 if (!force_if_stale
&& current_epoch
> 1) {
1475 error_stream
<< "ERROR: This zone is " << behind
<< " period(s) behind "
1476 "the current master zone in metadata sync. If this zone is promoted "
1477 "to master, any metadata changes during that time are likely to "
1479 "Waiting for this zone to catch up on metadata sync (see "
1480 "'radosgw-admin sync status') is recommended.\n"
1481 "To promote this zone to master anyway, add the flag "
1482 "--yes-i-really-mean-it." << std::endl
;
1485 // empty sync status markers - other zones will skip this period during
1486 // incremental metadata sync
1487 markers
.resize(status
.sync_info
.num_shards
);
1489 markers
.reserve(status
.sync_info
.num_shards
);
1490 for (auto& i
: status
.sync_markers
) {
1491 auto& marker
= i
.second
;
1492 // filter out markers from other periods
1493 if (marker
.realm_epoch
!= current_epoch
) {
1494 marker
.marker
.clear();
1496 markers
.emplace_back(std::move(marker
.marker
));
1500 std::swap(sync_status
, markers
);
1504 int RGWPeriod::commit(RGWRealm
& realm
, const RGWPeriod
& current_period
,
1505 std::ostream
& error_stream
, bool force_if_stale
)
1507 ldout(cct
, 20) << __func__
<< " realm " << realm
.get_id() << " period " << current_period
.get_id() << dendl
;
1508 // gateway must be in the master zone to commit
1509 if (master_zone
!= store
->get_zone_params().get_id()) {
1510 error_stream
<< "Cannot commit period on zone "
1511 << store
->get_zone_params().get_id() << ", it must be sent to "
1512 "the period's master zone " << master_zone
<< '.' << std::endl
;
1515 // period predecessor must match current period
1516 if (predecessor_uuid
!= current_period
.get_id()) {
1517 error_stream
<< "Period predecessor " << predecessor_uuid
1518 << " does not match current period " << current_period
.get_id()
1519 << ". Use 'period pull' to get the latest period from the master, "
1520 "reapply your changes, and try again." << std::endl
;
1523 // realm epoch must be 1 greater than current period
1524 if (realm_epoch
!= current_period
.get_realm_epoch() + 1) {
1525 error_stream
<< "Period's realm epoch " << realm_epoch
1526 << " does not come directly after current realm epoch "
1527 << current_period
.get_realm_epoch() << ". Use 'realm pull' to get the "
1528 "latest realm and period from the master zone, reapply your changes, "
1529 "and try again." << std::endl
;
1532 // did the master zone change?
1533 if (master_zone
!= current_period
.get_master_zone()) {
1534 // store the current metadata sync status in the period
1535 int r
= update_sync_status(current_period
, error_stream
, force_if_stale
);
1537 ldout(cct
, 0) << "failed to update metadata sync status: "
1538 << cpp_strerror(-r
) << dendl
;
1541 // create an object with a new period id
1544 ldout(cct
, 0) << "failed to create new period: " << cpp_strerror(-r
) << dendl
;
1547 // set as current period
1548 r
= realm
.set_current_period(*this);
1550 ldout(cct
, 0) << "failed to update realm's current period: "
1551 << cpp_strerror(-r
) << dendl
;
1554 ldout(cct
, 4) << "Promoted to master zone and committed new period "
1556 realm
.notify_new_period(*this);
1559 // period must be based on current epoch
1560 if (epoch
!= current_period
.get_epoch()) {
1561 error_stream
<< "Period epoch " << epoch
<< " does not match "
1562 "predecessor epoch " << current_period
.get_epoch()
1563 << ". Use 'period pull' to get the latest epoch from the master zone, "
1564 "reapply your changes, and try again." << std::endl
;
1567 // set period as next epoch
1568 set_id(current_period
.get_id());
1569 set_epoch(current_period
.get_epoch() + 1);
1570 set_predecessor(current_period
.get_predecessor());
1571 realm_epoch
= current_period
.get_realm_epoch();
1572 // write the period to rados
1573 int r
= store_info(false);
1575 ldout(cct
, 0) << "failed to store period: " << cpp_strerror(-r
) << dendl
;
1578 // set as latest epoch
1579 r
= update_latest_epoch(epoch
);
1581 // already have this epoch (or a more recent one)
1585 ldout(cct
, 0) << "failed to set latest epoch: " << cpp_strerror(-r
) << dendl
;
1590 ldout(cct
, 0) << "failed to update local objects: " << cpp_strerror(-r
) << dendl
;
1593 ldout(cct
, 4) << "Committed new epoch " << epoch
1594 << " for period " << id
<< dendl
;
1595 realm
.notify_new_period(*this);
1599 int RGWZoneParams::create_default(bool old_format
)
1601 name
= default_zone_name
;
1616 int get_zones_pool_set(CephContext
* cct
,
1618 const list
<string
>& zones
,
1619 const string
& my_zone_id
,
1620 set
<rgw_pool
>& pool_names
)
1622 for(auto const& iter
: zones
) {
1623 RGWZoneParams
zone(iter
);
1624 int r
= zone
.init(cct
, store
);
1626 ldout(cct
, 0) << "Error: init zone " << iter
<< ":" << cpp_strerror(-r
) << dendl
;
1629 if (zone
.get_id() != my_zone_id
) {
1630 pool_names
.insert(zone
.domain_root
);
1631 pool_names
.insert(zone
.metadata_heap
);
1632 pool_names
.insert(zone
.control_pool
);
1633 pool_names
.insert(zone
.gc_pool
);
1634 pool_names
.insert(zone
.log_pool
);
1635 pool_names
.insert(zone
.intent_log_pool
);
1636 pool_names
.insert(zone
.usage_log_pool
);
1637 pool_names
.insert(zone
.user_keys_pool
);
1638 pool_names
.insert(zone
.user_email_pool
);
1639 pool_names
.insert(zone
.user_swift_pool
);
1640 pool_names
.insert(zone
.user_uid_pool
);
1641 pool_names
.insert(zone
.roles_pool
);
1642 pool_names
.insert(zone
.reshard_pool
);
1643 for(auto& iter
: zone
.placement_pools
) {
1644 pool_names
.insert(iter
.second
.index_pool
);
1645 pool_names
.insert(iter
.second
.data_pool
);
1646 pool_names
.insert(iter
.second
.data_extra_pool
);
1653 rgw_pool
fix_zone_pool_dup(set
<rgw_pool
> pools
,
1654 const string
& default_prefix
,
1655 const string
& default_suffix
,
1656 const rgw_pool
& suggested_pool
)
1658 string suggested_name
= suggested_pool
.to_str();
1660 string prefix
= default_prefix
;
1661 string suffix
= default_suffix
;
1663 if (!suggested_pool
.empty()) {
1664 prefix
= suggested_name
.substr(0, suggested_name
.find("."));
1665 suffix
= suggested_name
.substr(prefix
.length());
1668 rgw_pool
pool(prefix
+ suffix
);
1670 if (pools
.find(pool
) == pools
.end()) {
1674 pool
= prefix
+ "_" + std::to_string(std::rand()) + suffix
;
1675 if (pools
.find(pool
) == pools
.end()) {
1682 int RGWZoneParams::fix_pool_names()
1686 int r
= store
->list_zones(zones
);
1688 ldout(cct
, 10) << "WARNING: store->list_zones() returned r=" << r
<< dendl
;
1691 set
<rgw_pool
> pools
;
1692 r
= get_zones_pool_set(cct
, store
, zones
, id
, pools
);
1694 ldout(cct
, 0) << "Error: get_zones_pool_names" << r
<< dendl
;
1698 domain_root
= fix_zone_pool_dup(pools
, name
, ".rgw.meta:root", domain_root
);
1699 if (!metadata_heap
.name
.empty()) {
1700 metadata_heap
= fix_zone_pool_dup(pools
, name
, ".rgw.meta:heap", metadata_heap
);
1702 control_pool
= fix_zone_pool_dup(pools
, name
, ".rgw.control", control_pool
);
1703 gc_pool
= fix_zone_pool_dup(pools
, name
,".rgw.log:gc", gc_pool
);
1704 lc_pool
= fix_zone_pool_dup(pools
, name
,".rgw.log:lc", lc_pool
);
1705 log_pool
= fix_zone_pool_dup(pools
, name
, ".rgw.log", log_pool
);
1706 intent_log_pool
= fix_zone_pool_dup(pools
, name
, ".rgw.log:intent", intent_log_pool
);
1707 usage_log_pool
= fix_zone_pool_dup(pools
, name
, ".rgw.log:usage", usage_log_pool
);
1708 user_keys_pool
= fix_zone_pool_dup(pools
, name
, ".rgw.meta:users.keys", user_keys_pool
);
1709 user_email_pool
= fix_zone_pool_dup(pools
, name
, ".rgw.meta:users.email", user_email_pool
);
1710 user_swift_pool
= fix_zone_pool_dup(pools
, name
, ".rgw.meta:users.swift", user_swift_pool
);
1711 user_uid_pool
= fix_zone_pool_dup(pools
, name
, ".rgw.meta:users.uid", user_uid_pool
);
1712 roles_pool
= fix_zone_pool_dup(pools
, name
, ".rgw.meta:roles", roles_pool
);
1713 reshard_pool
= fix_zone_pool_dup(pools
, name
, ".rgw.log:reshard", reshard_pool
);
1715 for(auto& iter
: placement_pools
) {
1716 iter
.second
.index_pool
= fix_zone_pool_dup(pools
, name
, "." + default_bucket_index_pool_suffix
,
1717 iter
.second
.index_pool
);
1718 iter
.second
.data_pool
= fix_zone_pool_dup(pools
, name
, "." + default_storage_pool_suffix
,
1719 iter
.second
.data_pool
);
1720 iter
.second
.data_extra_pool
= fix_zone_pool_dup(pools
, name
, "." + default_storage_extra_pool_suffix
,
1721 iter
.second
.data_extra_pool
);
1727 int RGWZoneParams::create(bool exclusive
)
1729 /* check for old pools config */
1730 rgw_raw_obj
obj(domain_root
, avail_pools
);
1731 int r
= store
->raw_obj_stat(obj
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
1733 ldout(store
->ctx(), 10) << "couldn't find old data placement pools config, setting up new ones for the zone" << dendl
;
1734 /* a new system, let's set new placement info */
1735 RGWZonePlacementInfo default_placement
;
1736 default_placement
.index_pool
= name
+ "." + default_bucket_index_pool_suffix
;
1737 default_placement
.data_pool
= name
+ "." + default_storage_pool_suffix
;
1738 default_placement
.data_extra_pool
= name
+ "." + default_storage_extra_pool_suffix
;
1739 placement_pools
["default-placement"] = default_placement
;
1742 r
= fix_pool_names();
1744 ldout(cct
, 0) << "ERROR: fix_pool_names returned r=" << r
<< dendl
;
1748 r
= RGWSystemMetaObj::create(exclusive
);
1753 // try to set as default. may race with another create, so pass exclusive=true
1754 // so we don't override an existing default
1755 r
= set_as_default(true);
1756 if (r
< 0 && r
!= -EEXIST
) {
1757 ldout(cct
, 10) << "WARNING: failed to set zone as default, r=" << r
<< dendl
;
1763 rgw_pool
RGWZoneParams::get_pool(CephContext
*cct
)
1765 if (cct
->_conf
->rgw_zone_root_pool
.empty()) {
1766 return rgw_pool(RGW_DEFAULT_ZONE_ROOT_POOL
);
1769 return rgw_pool(cct
->_conf
->rgw_zone_root_pool
);
1772 const string
RGWZoneParams::get_default_oid(bool old_format
)
1775 return cct
->_conf
->rgw_default_zone_info_oid
;
1778 return cct
->_conf
->rgw_default_zone_info_oid
+ "." + realm_id
;
1781 const string
& RGWZoneParams::get_names_oid_prefix()
1783 return zone_names_oid_prefix
;
1786 const string
& RGWZoneParams::get_info_oid_prefix(bool old_format
)
1788 return zone_info_oid_prefix
;
1791 const string
& RGWZoneParams::get_predefined_name(CephContext
*cct
) {
1792 return cct
->_conf
->rgw_zone
;
1795 int RGWZoneParams::init(CephContext
*cct
, RGWRados
*store
, bool setup_obj
, bool old_format
)
1798 name
= cct
->_conf
->rgw_zone
;
1801 return RGWSystemMetaObj::init(cct
, store
, setup_obj
, old_format
);
1804 int RGWZoneParams::read_default_id(string
& default_id
, bool old_format
)
1806 if (realm_id
.empty()) {
1807 /* try using default realm */
1809 int ret
= realm
.init(cct
, store
);
1811 ldout(cct
, 10) << "could not read realm id: " << cpp_strerror(-ret
) << dendl
;
1814 realm_id
= realm
.get_id();
1817 return RGWSystemMetaObj::read_default_id(default_id
, old_format
);
1821 int RGWZoneParams::set_as_default(bool exclusive
)
1823 if (realm_id
.empty()) {
1824 /* try using default realm */
1826 int ret
= realm
.init(cct
, store
);
1828 ldout(cct
, 10) << "could not read realm id: " << cpp_strerror(-ret
) << dendl
;
1831 realm_id
= realm
.get_id();
1834 return RGWSystemMetaObj::set_as_default(exclusive
);
1837 const string
& RGWZoneParams::get_compression_type(const string
& placement_rule
) const
1839 static const std::string NONE
{"none"};
1840 auto p
= placement_pools
.find(placement_rule
);
1841 if (p
== placement_pools
.end()) {
1844 const auto& type
= p
->second
.compression_type
;
1845 return !type
.empty() ? type
: NONE
;
1848 void RGWPeriodMap::encode(bufferlist
& bl
) const {
1849 ENCODE_START(2, 1, bl
);
1851 ::encode(zonegroups
, bl
);
1852 ::encode(master_zonegroup
, bl
);
1853 ::encode(short_zone_ids
, bl
);
1857 void RGWPeriodMap::decode(bufferlist::iterator
& bl
) {
1858 DECODE_START(2, bl
);
1860 ::decode(zonegroups
, bl
);
1861 ::decode(master_zonegroup
, bl
);
1862 if (struct_v
>= 2) {
1863 ::decode(short_zone_ids
, bl
);
1867 zonegroups_by_api
.clear();
1868 for (map
<string
, RGWZoneGroup
>::iterator iter
= zonegroups
.begin();
1869 iter
!= zonegroups
.end(); ++iter
) {
1870 RGWZoneGroup
& zonegroup
= iter
->second
;
1871 zonegroups_by_api
[zonegroup
.api_name
] = zonegroup
;
1872 if (zonegroup
.is_master_zonegroup()) {
1873 master_zonegroup
= zonegroup
.get_id();
1878 // run an MD5 hash on the zone_id and return the first 32 bits
1879 static uint32_t gen_short_zone_id(const std::string zone_id
)
1881 unsigned char md5
[CEPH_CRYPTO_MD5_DIGESTSIZE
];
1883 hash
.Update((const byte
*)zone_id
.c_str(), zone_id
.size());
1887 memcpy((char *)&short_id
, md5
, sizeof(short_id
));
1888 return std::max(short_id
, 1u);
1891 int RGWPeriodMap::update(const RGWZoneGroup
& zonegroup
, CephContext
*cct
)
1893 if (zonegroup
.is_master_zonegroup() && (!master_zonegroup
.empty() && zonegroup
.get_id() != master_zonegroup
)) {
1894 ldout(cct
,0) << "Error updating periodmap, multiple master zonegroups configured "<< dendl
;
1895 ldout(cct
,0) << "master zonegroup: " << master_zonegroup
<< " and " << zonegroup
.get_id() <<dendl
;
1898 map
<string
, RGWZoneGroup
>::iterator iter
= zonegroups
.find(zonegroup
.get_id());
1899 if (iter
!= zonegroups
.end()) {
1900 RGWZoneGroup
& old_zonegroup
= iter
->second
;
1901 if (!old_zonegroup
.api_name
.empty()) {
1902 zonegroups_by_api
.erase(old_zonegroup
.api_name
);
1905 zonegroups
[zonegroup
.get_id()] = zonegroup
;
1907 if (!zonegroup
.api_name
.empty()) {
1908 zonegroups_by_api
[zonegroup
.api_name
] = zonegroup
;
1911 if (zonegroup
.is_master_zonegroup()) {
1912 master_zonegroup
= zonegroup
.get_id();
1913 } else if (master_zonegroup
== zonegroup
.get_id()) {
1914 master_zonegroup
= "";
1917 for (auto& i
: zonegroup
.zones
) {
1918 auto& zone
= i
.second
;
1919 if (short_zone_ids
.find(zone
.id
) != short_zone_ids
.end()) {
1922 // calculate the zone's short id
1923 uint32_t short_id
= gen_short_zone_id(zone
.id
);
1925 // search for an existing zone with the same short id
1926 for (auto& s
: short_zone_ids
) {
1927 if (s
.second
== short_id
) {
1928 ldout(cct
, 0) << "New zone '" << zone
.name
<< "' (" << zone
.id
1929 << ") generates the same short_zone_id " << short_id
1930 << " as existing zone id " << s
.first
<< dendl
;
1935 short_zone_ids
[zone
.id
] = short_id
;
1941 uint32_t RGWPeriodMap::get_zone_short_id(const string
& zone_id
) const
1943 auto i
= short_zone_ids
.find(zone_id
);
1944 if (i
== short_zone_ids
.end()) {
1950 int RGWZoneGroupMap::read(CephContext
*cct
, RGWRados
*store
)
1954 int ret
= period
.init(cct
, store
);
1956 cerr
<< "failed to read current period info: " << cpp_strerror(ret
);
1960 bucket_quota
= period
.get_config().bucket_quota
;
1961 user_quota
= period
.get_config().user_quota
;
1962 zonegroups
= period
.get_map().zonegroups
;
1963 zonegroups_by_api
= period
.get_map().zonegroups_by_api
;
1964 master_zonegroup
= period
.get_map().master_zonegroup
;
1969 void RGWRegionMap::encode(bufferlist
& bl
) const {
1970 ENCODE_START( 3, 1, bl
);
1971 ::encode(regions
, bl
);
1972 ::encode(master_region
, bl
);
1973 ::encode(bucket_quota
, bl
);
1974 ::encode(user_quota
, bl
);
1978 void RGWRegionMap::decode(bufferlist::iterator
& bl
) {
1979 DECODE_START(3, bl
);
1980 ::decode(regions
, bl
);
1981 ::decode(master_region
, bl
);
1983 ::decode(bucket_quota
, bl
);
1985 ::decode(user_quota
, bl
);
1989 void RGWZoneGroupMap::encode(bufferlist
& bl
) const {
1990 ENCODE_START( 3, 1, bl
);
1991 ::encode(zonegroups
, bl
);
1992 ::encode(master_zonegroup
, bl
);
1993 ::encode(bucket_quota
, bl
);
1994 ::encode(user_quota
, bl
);
1998 void RGWZoneGroupMap::decode(bufferlist::iterator
& bl
) {
1999 DECODE_START(3, bl
);
2000 ::decode(zonegroups
, bl
);
2001 ::decode(master_zonegroup
, bl
);
2003 ::decode(bucket_quota
, bl
);
2005 ::decode(user_quota
, bl
);
2008 zonegroups_by_api
.clear();
2009 for (map
<string
, RGWZoneGroup
>::iterator iter
= zonegroups
.begin();
2010 iter
!= zonegroups
.end(); ++iter
) {
2011 RGWZoneGroup
& zonegroup
= iter
->second
;
2012 zonegroups_by_api
[zonegroup
.api_name
] = zonegroup
;
2013 if (zonegroup
.is_master_zonegroup()) {
2014 master_zonegroup
= zonegroup
.get_name();
2019 void RGWObjVersionTracker::prepare_op_for_read(ObjectReadOperation
*op
)
2021 obj_version
*check_objv
= version_for_check();
2024 cls_version_check(*op
, *check_objv
, VER_COND_EQ
);
2027 cls_version_read(*op
, &read_version
);
2030 void RGWObjVersionTracker::prepare_op_for_write(ObjectWriteOperation
*op
)
2032 obj_version
*check_objv
= version_for_check();
2033 obj_version
*modify_version
= version_for_write();
2036 cls_version_check(*op
, *check_objv
, VER_COND_EQ
);
2039 if (modify_version
) {
2040 cls_version_set(*op
, *modify_version
);
2042 cls_version_inc(*op
);
2046 void RGWObjManifest::obj_iterator::operator++()
2048 if (manifest
->explicit_objs
) {
2051 if (explicit_iter
== manifest
->objs
.end()) {
2052 ofs
= manifest
->obj_size
;
2056 update_explicit_pos();
2062 uint64_t obj_size
= manifest
->get_obj_size();
2063 uint64_t head_size
= manifest
->get_head_size();
2065 if (ofs
== obj_size
) {
2069 if (manifest
->rules
.empty()) {
2073 /* are we still pointing at the head? */
2074 if (ofs
< head_size
) {
2075 rule_iter
= manifest
->rules
.begin();
2076 RGWObjManifestRule
*rule
= &rule_iter
->second
;
2077 ofs
= MIN(head_size
, obj_size
);
2080 stripe_size
= MIN(obj_size
- ofs
, rule
->stripe_max_size
);
2081 if (rule
->part_size
> 0) {
2082 stripe_size
= MIN(stripe_size
, rule
->part_size
);
2088 RGWObjManifestRule
*rule
= &rule_iter
->second
;
2090 stripe_ofs
+= rule
->stripe_max_size
;
2092 dout(20) << "RGWObjManifest::operator++(): rule->part_size=" << rule
->part_size
<< " rules.size()=" << manifest
->rules
.size() << dendl
;
2094 if (rule
->part_size
> 0) {
2095 /* multi part, multi stripes object */
2097 dout(20) << "RGWObjManifest::operator++(): stripe_ofs=" << stripe_ofs
<< " part_ofs=" << part_ofs
<< " rule->part_size=" << rule
->part_size
<< dendl
;
2099 if (stripe_ofs
>= part_ofs
+ rule
->part_size
) {
2100 /* moved to the next part */
2102 part_ofs
+= rule
->part_size
;
2103 stripe_ofs
= part_ofs
;
2105 bool last_rule
= (next_rule_iter
== manifest
->rules
.end());
2106 /* move to the next rule? */
2107 if (!last_rule
&& stripe_ofs
>= next_rule_iter
->second
.start_ofs
) {
2108 rule_iter
= next_rule_iter
;
2109 last_rule
= (next_rule_iter
== manifest
->rules
.end());
2113 cur_part_id
= rule_iter
->second
.start_part_num
;
2118 rule
= &rule_iter
->second
;
2121 stripe_size
= MIN(rule
->part_size
- (stripe_ofs
- part_ofs
), rule
->stripe_max_size
);
2124 cur_override_prefix
= rule
->override_prefix
;
2127 if (ofs
> obj_size
) {
2133 dout(20) << "RGWObjManifest::operator++(): result: ofs=" << ofs
<< " stripe_ofs=" << stripe_ofs
<< " part_ofs=" << part_ofs
<< " rule->part_size=" << rule
->part_size
<< dendl
;
2137 int RGWObjManifest::generator::create_begin(CephContext
*cct
, RGWObjManifest
*_m
, const string
& placement_rule
, rgw_bucket
& _b
, rgw_obj
& _obj
)
2141 manifest
->set_tail_placement(placement_rule
, _b
);
2142 manifest
->set_head(placement_rule
, _obj
, 0);
2145 if (manifest
->get_prefix().empty()) {
2147 gen_rand_alphanumeric(cct
, buf
, sizeof(buf
) - 1);
2149 string oid_prefix
= ".";
2150 oid_prefix
.append(buf
);
2151 oid_prefix
.append("_");
2153 manifest
->set_prefix(oid_prefix
);
2156 bool found
= manifest
->get_rule(0, &rule
);
2158 derr
<< "ERROR: manifest->get_rule() could not find rule" << dendl
;
2162 uint64_t head_size
= manifest
->get_head_size();
2164 if (head_size
> 0) {
2165 cur_stripe_size
= head_size
;
2167 cur_stripe_size
= rule
.stripe_max_size
;
2170 cur_part_id
= rule
.start_part_num
;
2172 manifest
->get_implicit_location(cur_part_id
, cur_stripe
, 0, NULL
, &cur_obj
);
2174 // Normal object which not generated through copy operation
2175 manifest
->set_tail_instance(_obj
.key
.instance
);
2177 manifest
->update_iterators();
2182 int RGWObjManifest::generator::create_next(uint64_t ofs
)
2184 if (ofs
< last_ofs
) /* only going forward */
2187 uint64_t max_head_size
= manifest
->get_max_head_size();
2189 if (ofs
< max_head_size
) {
2190 manifest
->set_head_size(ofs
);
2193 if (ofs
>= max_head_size
) {
2194 manifest
->set_head_size(max_head_size
);
2195 cur_stripe
= (ofs
- max_head_size
) / rule
.stripe_max_size
;
2196 cur_stripe_size
= rule
.stripe_max_size
;
2198 if (cur_part_id
== 0 && max_head_size
> 0) {
2204 manifest
->set_obj_size(ofs
);
2206 manifest
->get_implicit_location(cur_part_id
, cur_stripe
, ofs
, NULL
, &cur_obj
);
2208 manifest
->update_iterators();
2213 const RGWObjManifest::obj_iterator
& RGWObjManifest::obj_begin()
2218 const RGWObjManifest::obj_iterator
& RGWObjManifest::obj_end()
2223 RGWObjManifest::obj_iterator
RGWObjManifest::obj_find(uint64_t ofs
)
2225 if (ofs
> obj_size
) {
2228 RGWObjManifest::obj_iterator
iter(this);
2233 int RGWObjManifest::append(RGWObjManifest
& m
, RGWZoneGroup
& zonegroup
, RGWZoneParams
& zone_params
)
2235 if (explicit_objs
|| m
.explicit_objs
) {
2236 return append_explicit(m
, zonegroup
, zone_params
);
2239 if (rules
.empty()) {
2244 string override_prefix
;
2246 if (prefix
.empty()) {
2250 if (prefix
!= m
.prefix
) {
2251 override_prefix
= m
.prefix
;
2254 map
<uint64_t, RGWObjManifestRule
>::iterator miter
= m
.rules
.begin();
2255 if (miter
== m
.rules
.end()) {
2256 return append_explicit(m
, zonegroup
, zone_params
);
2259 for (; miter
!= m
.rules
.end(); ++miter
) {
2260 map
<uint64_t, RGWObjManifestRule
>::reverse_iterator last_rule
= rules
.rbegin();
2262 RGWObjManifestRule
& rule
= last_rule
->second
;
2264 if (rule
.part_size
== 0) {
2265 rule
.part_size
= obj_size
- rule
.start_ofs
;
2268 RGWObjManifestRule
& next_rule
= miter
->second
;
2269 if (!next_rule
.part_size
) {
2270 next_rule
.part_size
= m
.obj_size
- next_rule
.start_ofs
;
2273 string rule_prefix
= prefix
;
2274 if (!rule
.override_prefix
.empty()) {
2275 rule_prefix
= rule
.override_prefix
;
2278 string next_rule_prefix
= m
.prefix
;
2279 if (!next_rule
.override_prefix
.empty()) {
2280 next_rule_prefix
= next_rule
.override_prefix
;
2283 if (rule
.part_size
!= next_rule
.part_size
||
2284 rule
.stripe_max_size
!= next_rule
.stripe_max_size
||
2285 rule_prefix
!= next_rule_prefix
) {
2286 if (next_rule_prefix
!= prefix
) {
2287 append_rules(m
, miter
, &next_rule_prefix
);
2289 append_rules(m
, miter
, NULL
);
2294 uint64_t expected_part_num
= rule
.start_part_num
+ 1;
2295 if (rule
.part_size
> 0) {
2296 expected_part_num
= rule
.start_part_num
+ (obj_size
+ next_rule
.start_ofs
- rule
.start_ofs
) / rule
.part_size
;
2299 if (expected_part_num
!= next_rule
.start_part_num
) {
2300 append_rules(m
, miter
, NULL
);
2305 set_obj_size(obj_size
+ m
.obj_size
);
2310 int RGWObjManifest::append(RGWObjManifest
& m
, RGWRados
*store
)
2312 return append(m
, store
->get_zonegroup(), store
->get_zone_params());
2315 void RGWObjManifest::append_rules(RGWObjManifest
& m
, map
<uint64_t, RGWObjManifestRule
>::iterator
& miter
,
2316 string
*override_prefix
)
2318 for (; miter
!= m
.rules
.end(); ++miter
) {
2319 RGWObjManifestRule rule
= miter
->second
;
2320 rule
.start_ofs
+= obj_size
;
2321 if (override_prefix
)
2322 rule
.override_prefix
= *override_prefix
;
2323 rules
[rule
.start_ofs
] = rule
;
2327 void RGWObjManifest::convert_to_explicit(const RGWZoneGroup
& zonegroup
, const RGWZoneParams
& zone_params
)
2329 if (explicit_objs
) {
2332 obj_iterator iter
= obj_begin();
2334 while (iter
!= obj_end()) {
2335 RGWObjManifestPart
& part
= objs
[iter
.get_stripe_ofs()];
2336 const rgw_obj_select
& os
= iter
.get_location();
2337 const rgw_raw_obj
& raw_loc
= os
.get_raw_obj(zonegroup
, zone_params
);
2340 uint64_t ofs
= iter
.get_stripe_ofs();
2345 rgw_raw_obj_to_obj(tail_placement
.bucket
, raw_loc
, &part
.loc
);
2348 uint64_t next_ofs
= iter
.get_stripe_ofs();
2350 part
.size
= next_ofs
- ofs
;
2353 explicit_objs
= true;
2358 int RGWObjManifest::append_explicit(RGWObjManifest
& m
, const RGWZoneGroup
& zonegroup
, const RGWZoneParams
& zone_params
)
2360 if (!explicit_objs
) {
2361 convert_to_explicit(zonegroup
, zone_params
);
2363 if (!m
.explicit_objs
) {
2364 m
.convert_to_explicit(zonegroup
, zone_params
);
2366 map
<uint64_t, RGWObjManifestPart
>::iterator iter
;
2367 uint64_t base
= obj_size
;
2368 for (iter
= m
.objs
.begin(); iter
!= m
.objs
.end(); ++iter
) {
2369 RGWObjManifestPart
& part
= iter
->second
;
2370 objs
[base
+ iter
->first
] = part
;
2372 obj_size
+= m
.obj_size
;
2377 bool RGWObjManifest::get_rule(uint64_t ofs
, RGWObjManifestRule
*rule
)
2379 if (rules
.empty()) {
2383 map
<uint64_t, RGWObjManifestRule
>::iterator iter
= rules
.upper_bound(ofs
);
2384 if (iter
!= rules
.begin()) {
2388 *rule
= iter
->second
;
2393 void RGWObjVersionTracker::generate_new_write_ver(CephContext
*cct
)
2395 write_version
.ver
= 1;
2398 write_version
.tag
.clear();
2399 append_rand_alpha(cct
, write_version
.tag
, write_version
.tag
, TAG_LEN
);
2402 int RGWPutObjProcessor::complete(size_t accounted_size
, const string
& etag
,
2403 real_time
*mtime
, real_time set_mtime
,
2404 map
<string
, bufferlist
>& attrs
, real_time delete_at
,
2405 const char *if_match
, const char *if_nomatch
, const string
*user_data
,
2406 rgw_zone_set
*zones_trace
)
2408 int r
= do_complete(accounted_size
, etag
, mtime
, set_mtime
, attrs
, delete_at
, if_match
, if_nomatch
, user_data
, zones_trace
);
2412 is_complete
= !canceled
;
2416 CephContext
*RGWPutObjProcessor::ctx()
2418 return store
->ctx();
2421 RGWPutObjProcessor_Aio::~RGWPutObjProcessor_Aio()
2428 set
<rgw_raw_obj
>::iterator iter
;
2429 bool need_to_remove_head
= false;
2430 rgw_raw_obj raw_head
;
2432 if (!head_obj
.empty()) {
2433 store
->obj_to_raw(bucket_info
.placement_rule
, head_obj
, &raw_head
);
2437 * We should delete the object in the "multipart" namespace to avoid race condition.
2438 * Such race condition is caused by the fact that the multipart object is the gatekeeper of a multipart
2439 * upload, when it is deleted, a second upload would start with the same suffix("2/"), therefore, objects
2440 * written by the second upload may be deleted by the first upload.
2441 * details is describled on #11749
2443 * The above comment still stands, but instead of searching for a specific object in the multipart
2444 * namespace, we just make sure that we remove the object that is marked as the head object after
2445 * we remove all the other raw objects. Note that we use different call to remove the head object,
2446 * as this one needs to go via the bucket index prepare/complete 2-phase commit scheme.
2448 for (iter
= written_objs
.begin(); iter
!= written_objs
.end(); ++iter
) {
2449 const rgw_raw_obj
& obj
= *iter
;
2450 if (!head_obj
.empty() && obj
== raw_head
) {
2451 ldout(store
->ctx(), 5) << "NOTE: we should not process the head object (" << obj
<< ") here" << dendl
;
2452 need_to_remove_head
= true;
2456 int r
= store
->delete_raw_obj(obj
);
2457 if (r
< 0 && r
!= -ENOENT
) {
2458 ldout(store
->ctx(), 5) << "WARNING: failed to remove obj (" << obj
<< "), leaked" << dendl
;
2462 if (need_to_remove_head
) {
2463 ldout(store
->ctx(), 5) << "NOTE: we are going to process the head obj (" << raw_head
<< ")" << dendl
;
2464 int r
= store
->delete_obj(obj_ctx
, bucket_info
, head_obj
, 0, 0);
2465 if (r
< 0 && r
!= -ENOENT
) {
2466 ldout(store
->ctx(), 0) << "WARNING: failed to remove obj (" << raw_head
<< "), leaked" << dendl
;
2471 int RGWPutObjProcessor_Aio::handle_obj_data(rgw_raw_obj
& obj
, bufferlist
& bl
, off_t ofs
, off_t abs_ofs
, void **phandle
, bool exclusive
)
2473 if ((uint64_t)abs_ofs
+ bl
.length() > obj_len
)
2474 obj_len
= abs_ofs
+ bl
.length();
2476 if (!(obj
== last_written_obj
)) {
2477 last_written_obj
= obj
;
2480 // For the first call pass -1 as the offset to
2482 return store
->aio_put_obj_data(NULL
, obj
, bl
, ((ofs
!= 0) ? ofs
: -1), exclusive
, phandle
);
2485 struct put_obj_aio_info
RGWPutObjProcessor_Aio::pop_pending()
2487 struct put_obj_aio_info info
;
2488 info
= pending
.front();
2489 pending
.pop_front();
2490 pending_size
-= info
.size
;
2494 int RGWPutObjProcessor_Aio::wait_pending_front()
2496 if (pending
.empty()) {
2499 struct put_obj_aio_info info
= pop_pending();
2500 int ret
= store
->aio_wait(info
.handle
);
2503 add_written_obj(info
.obj
);
2509 bool RGWPutObjProcessor_Aio::pending_has_completed()
2511 if (pending
.empty())
2514 struct put_obj_aio_info
& info
= pending
.front();
2515 return store
->aio_completed(info
.handle
);
2518 int RGWPutObjProcessor_Aio::drain_pending()
2521 while (!pending
.empty()) {
2522 int r
= wait_pending_front();
2529 int RGWPutObjProcessor_Aio::throttle_data(void *handle
, const rgw_raw_obj
& obj
, uint64_t size
, bool need_to_wait
)
2531 bool _wait
= need_to_wait
;
2534 struct put_obj_aio_info info
;
2535 info
.handle
= handle
;
2538 pending_size
+= size
;
2539 pending
.push_back(info
);
2541 size_t orig_size
= pending_size
;
2543 /* first drain complete IOs */
2544 while (pending_has_completed()) {
2545 int r
= wait_pending_front();
2552 /* resize window in case messages are draining too fast */
2553 if (orig_size
- pending_size
>= window_size
) {
2554 window_size
+= store
->ctx()->_conf
->rgw_max_chunk_size
;
2555 uint64_t max_window_size
= store
->ctx()->_conf
->rgw_put_obj_max_window_size
;
2556 if (window_size
> max_window_size
) {
2557 window_size
= max_window_size
;
2561 /* now throttle. Note that need_to_wait should only affect the first IO operation */
2562 if (pending_size
> window_size
|| _wait
) {
2563 int r
= wait_pending_front();
2570 int RGWPutObjProcessor_Atomic::write_data(bufferlist
& bl
, off_t ofs
, void **phandle
, rgw_raw_obj
*pobj
, bool exclusive
)
2572 if (ofs
>= next_part_ofs
) {
2573 int r
= prepare_next_part(ofs
);
2586 return RGWPutObjProcessor_Aio::handle_obj_data(cur_obj
, bl
, ofs
- cur_part_ofs
, ofs
, phandle
, exclusive
);
2589 int RGWPutObjProcessor_Aio::prepare(RGWRados
*store
, string
*oid_rand
)
2591 RGWPutObjProcessor::prepare(store
, oid_rand
);
2593 window_size
= store
->ctx()->_conf
->rgw_put_obj_min_window_size
;
2598 int RGWPutObjProcessor_Atomic::handle_data(bufferlist
& bl
, off_t ofs
, void **phandle
, rgw_raw_obj
*pobj
, bool *again
)
2601 uint64_t max_write_size
= MIN(max_chunk_size
, (uint64_t)next_part_ofs
- data_ofs
);
2603 pending_data_bl
.claim_append(bl
);
2604 if (pending_data_bl
.length() < max_write_size
) {
2609 pending_data_bl
.splice(0, max_write_size
, &bl
);
2611 /* do we have enough data pending accumulated that needs to be written? */
2612 *again
= (pending_data_bl
.length() >= max_chunk_size
);
2614 if (!data_ofs
&& !immutable_head()) {
2615 first_chunk
.claim(bl
);
2616 obj_len
= (uint64_t)first_chunk
.length();
2617 int r
= prepare_next_part(obj_len
);
2624 off_t write_ofs
= data_ofs
;
2625 data_ofs
= write_ofs
+ bl
.length();
2626 bool exclusive
= (!write_ofs
&& immutable_head()); /* immutable head object, need to verify nothing exists there
2627 we could be racing with another upload, to the same
2628 object and cleanup can be messy */
2629 int ret
= write_data(bl
, write_ofs
, phandle
, pobj
, exclusive
);
2630 if (ret
>= 0) { /* we might return, need to clear bl as it was already sent */
2637 int RGWPutObjProcessor_Atomic::prepare_init(RGWRados
*store
, string
*oid_rand
)
2639 RGWPutObjProcessor_Aio::prepare(store
, oid_rand
);
2641 int r
= store
->get_max_chunk_size(bucket_info
.placement_rule
, head_obj
, &max_chunk_size
);
2649 int RGWPutObjProcessor_Atomic::prepare(RGWRados
*store
, string
*oid_rand
)
2651 head_obj
.init(bucket
, obj_str
);
2653 int r
= prepare_init(store
, oid_rand
);
2658 if (!version_id
.empty()) {
2659 head_obj
.key
.set_instance(version_id
);
2660 } else if (versioned_object
) {
2661 store
->gen_rand_obj_instance_name(&head_obj
);
2664 manifest
.set_trivial_rule(max_chunk_size
, store
->ctx()->_conf
->rgw_obj_stripe_size
);
2666 r
= manifest_gen
.create_begin(store
->ctx(), &manifest
, bucket_info
.placement_rule
, head_obj
.bucket
, head_obj
);
2674 int RGWPutObjProcessor_Atomic::prepare_next_part(off_t ofs
) {
2676 int ret
= manifest_gen
.create_next(ofs
);
2678 lderr(store
->ctx()) << "ERROR: manifest_gen.create_next() returned ret=" << ret
<< dendl
;
2682 next_part_ofs
= ofs
+ manifest_gen
.cur_stripe_max_size();
2683 cur_obj
= manifest_gen
.get_cur_obj(store
);
2688 int RGWPutObjProcessor_Atomic::complete_parts()
2690 if (obj_len
> (uint64_t)cur_part_ofs
) {
2691 return prepare_next_part(obj_len
);
2696 int RGWPutObjProcessor_Atomic::complete_writing_data()
2698 if (!data_ofs
&& !immutable_head()) {
2699 /* only claim if pending_data_bl() is not empty. This is needed because we might be called twice
2700 * (e.g., when a retry due to race happens). So a second call to first_chunk.claim() would
2701 * clobber first_chunk
2703 if (pending_data_bl
.length() > 0) {
2704 first_chunk
.claim(pending_data_bl
);
2706 obj_len
= (uint64_t)first_chunk
.length();
2708 while (pending_data_bl
.length()) {
2709 void *handle
= nullptr;
2711 uint64_t max_write_size
= MIN(max_chunk_size
, (uint64_t)next_part_ofs
- data_ofs
);
2712 if (max_write_size
> pending_data_bl
.length()) {
2713 max_write_size
= pending_data_bl
.length();
2716 pending_data_bl
.splice(0, max_write_size
, &bl
);
2717 uint64_t write_len
= bl
.length();
2718 int r
= write_data(bl
, data_ofs
, &handle
, &obj
, false);
2720 ldout(store
->ctx(), 0) << "ERROR: write_data() returned " << r
<< dendl
;
2723 data_ofs
+= write_len
;
2724 r
= throttle_data(handle
, obj
, write_len
, false);
2726 ldout(store
->ctx(), 0) << "ERROR: throttle_data() returned " << r
<< dendl
;
2730 if (data_ofs
>= next_part_ofs
) {
2731 r
= prepare_next_part(data_ofs
);
2733 ldout(store
->ctx(), 0) << "ERROR: prepare_next_part() returned " << r
<< dendl
;
2738 int r
= complete_parts();
2743 r
= drain_pending();
2750 int RGWPutObjProcessor_Atomic::do_complete(size_t accounted_size
, const string
& etag
,
2751 real_time
*mtime
, real_time set_mtime
,
2752 map
<string
, bufferlist
>& attrs
,
2753 real_time delete_at
,
2754 const char *if_match
,
2755 const char *if_nomatch
, const string
*user_data
,
2756 rgw_zone_set
*zones_trace
) {
2757 int r
= complete_writing_data();
2761 obj_ctx
.obj
.set_atomic(head_obj
);
2763 RGWRados::Object
op_target(store
, bucket_info
, obj_ctx
, head_obj
);
2765 /* some object types shouldn't be versioned, e.g., multipart parts */
2766 op_target
.set_versioning_disabled(!versioned_object
);
2768 RGWRados::Object::Write
obj_op(&op_target
);
2770 obj_op
.meta
.data
= &first_chunk
;
2771 obj_op
.meta
.manifest
= &manifest
;
2772 obj_op
.meta
.ptag
= &unique_tag
; /* use req_id as operation tag */
2773 obj_op
.meta
.if_match
= if_match
;
2774 obj_op
.meta
.if_nomatch
= if_nomatch
;
2775 obj_op
.meta
.mtime
= mtime
;
2776 obj_op
.meta
.set_mtime
= set_mtime
;
2777 obj_op
.meta
.owner
= bucket_info
.owner
;
2778 obj_op
.meta
.flags
= PUT_OBJ_CREATE
;
2779 obj_op
.meta
.olh_epoch
= olh_epoch
;
2780 obj_op
.meta
.delete_at
= delete_at
;
2781 obj_op
.meta
.user_data
= user_data
;
2782 obj_op
.meta
.zones_trace
= zones_trace
;
2783 obj_op
.meta
.modify_tail
= true;
2785 r
= obj_op
.write_meta(obj_len
, accounted_size
, attrs
);
2790 canceled
= obj_op
.meta
.canceled
;
2795 int RGWRados::watch(const string
& oid
, uint64_t *watch_handle
, librados::WatchCtx2
*ctx
) {
2796 int r
= control_pool_ctx
.watch2(oid
, watch_handle
, ctx
);
2802 int RGWRados::unwatch(uint64_t watch_handle
)
2804 int r
= control_pool_ctx
.unwatch2(watch_handle
);
2806 ldout(cct
, 0) << "ERROR: rados->unwatch2() returned r=" << r
<< dendl
;
2809 r
= rados
[0].watch_flush();
2811 ldout(cct
, 0) << "ERROR: rados->watch_flush() returned r=" << r
<< dendl
;
2817 void RGWRados::add_watcher(int i
)
2819 ldout(cct
, 20) << "add_watcher() i=" << i
<< dendl
;
2820 Mutex::Locker
l(watchers_lock
);
2821 watchers_set
.insert(i
);
2822 if (watchers_set
.size() == (size_t)num_watchers
) {
2823 ldout(cct
, 2) << "all " << num_watchers
<< " watchers are set, enabling cache" << dendl
;
2824 set_cache_enabled(true);
2828 void RGWRados::remove_watcher(int i
)
2830 ldout(cct
, 20) << "remove_watcher() i=" << i
<< dendl
;
2831 Mutex::Locker
l(watchers_lock
);
2832 size_t orig_size
= watchers_set
.size();
2833 watchers_set
.erase(i
);
2834 if (orig_size
== (size_t)num_watchers
&&
2835 watchers_set
.size() < orig_size
) { /* actually removed */
2836 ldout(cct
, 2) << "removed watcher, disabling cache" << dendl
;
2837 set_cache_enabled(false);
2841 class RGWWatcher
: public librados::WatchCtx2
{
2845 uint64_t watch_handle
;
2847 class C_ReinitWatch
: public Context
{
2848 RGWWatcher
*watcher
;
2850 explicit C_ReinitWatch(RGWWatcher
*_watcher
) : watcher(_watcher
) {}
2851 void finish(int r
) override
{
2856 RGWWatcher(RGWRados
*r
, int i
, const string
& o
) : rados(r
), index(i
), oid(o
), watch_handle(0) {}
2857 void handle_notify(uint64_t notify_id
,
2859 uint64_t notifier_id
,
2860 bufferlist
& bl
) override
{
2861 ldout(rados
->ctx(), 10) << "RGWWatcher::handle_notify() "
2862 << " notify_id " << notify_id
2863 << " cookie " << cookie
2864 << " notifier " << notifier_id
2865 << " bl.length()=" << bl
.length() << dendl
;
2866 rados
->watch_cb(notify_id
, cookie
, notifier_id
, bl
);
2868 bufferlist reply_bl
; // empty reply payload
2869 rados
->control_pool_ctx
.notify_ack(oid
, notify_id
, cookie
, reply_bl
);
2871 void handle_error(uint64_t cookie
, int err
) override
{
2872 lderr(rados
->ctx()) << "RGWWatcher::handle_error cookie " << cookie
2873 << " err " << cpp_strerror(err
) << dendl
;
2874 rados
->remove_watcher(index
);
2875 rados
->schedule_context(new C_ReinitWatch(this));
2879 int ret
= unregister_watch();
2881 ldout(rados
->ctx(), 0) << "ERROR: unregister_watch() returned ret=" << ret
<< dendl
;
2884 ret
= register_watch();
2886 ldout(rados
->ctx(), 0) << "ERROR: register_watch() returned ret=" << ret
<< dendl
;
2891 int unregister_watch() {
2892 int r
= rados
->unwatch(watch_handle
);
2896 rados
->remove_watcher(index
);
2900 int register_watch() {
2901 int r
= rados
->watch(oid
, &watch_handle
, this);
2905 rados
->add_watcher(index
);
2910 class RGWMetaNotifierManager
: public RGWCoroutinesManager
{
2912 RGWHTTPManager http_manager
;
2915 RGWMetaNotifierManager(RGWRados
*_store
) : RGWCoroutinesManager(_store
->ctx(), _store
->get_cr_registry()), store(_store
),
2916 http_manager(store
->ctx(), completion_mgr
) {
2917 http_manager
.set_threaded();
2920 int notify_all(map
<string
, RGWRESTConn
*>& conn_map
, set
<int>& shards
) {
2921 rgw_http_param_pair pairs
[] = { { "type", "metadata" },
2925 list
<RGWCoroutinesStack
*> stacks
;
2926 for (map
<string
, RGWRESTConn
*>::iterator iter
= conn_map
.begin(); iter
!= conn_map
.end(); ++iter
) {
2927 RGWRESTConn
*conn
= iter
->second
;
2928 RGWCoroutinesStack
*stack
= new RGWCoroutinesStack(store
->ctx(), this);
2929 stack
->call(new RGWPostRESTResourceCR
<set
<int>, int>(store
->ctx(), conn
, &http_manager
, "/admin/log", pairs
, shards
, NULL
));
2931 stacks
.push_back(stack
);
2937 class RGWDataNotifierManager
: public RGWCoroutinesManager
{
2939 RGWHTTPManager http_manager
;
2942 RGWDataNotifierManager(RGWRados
*_store
) : RGWCoroutinesManager(_store
->ctx(), _store
->get_cr_registry()), store(_store
),
2943 http_manager(store
->ctx(), completion_mgr
) {
2944 http_manager
.set_threaded();
2947 int notify_all(map
<string
, RGWRESTConn
*>& conn_map
, map
<int, set
<string
> >& shards
) {
2948 rgw_http_param_pair pairs
[] = { { "type", "data" },
2950 { "source-zone", store
->get_zone_params().get_id().c_str() },
2953 list
<RGWCoroutinesStack
*> stacks
;
2954 for (map
<string
, RGWRESTConn
*>::iterator iter
= conn_map
.begin(); iter
!= conn_map
.end(); ++iter
) {
2955 RGWRESTConn
*conn
= iter
->second
;
2956 RGWCoroutinesStack
*stack
= new RGWCoroutinesStack(store
->ctx(), this);
2957 stack
->call(new RGWPostRESTResourceCR
<map
<int, set
<string
> >, int>(store
->ctx(), conn
, &http_manager
, "/admin/log", pairs
, shards
, NULL
));
2959 stacks
.push_back(stack
);
2965 class RGWRadosThread
{
2966 class Worker
: public Thread
{
2968 RGWRadosThread
*processor
;
2973 Mutex::Locker
l(lock
);
2977 void wait_interval(const utime_t
& wait_time
) {
2978 Mutex::Locker
l(lock
);
2979 cond
.WaitInterval(lock
, wait_time
);
2983 Worker(CephContext
*_cct
, RGWRadosThread
*_p
) : cct(_cct
), processor(_p
), lock("RGWRadosThread::Worker") {}
2984 void *entry() override
;
2986 Mutex::Locker
l(lock
);
2997 std::atomic
<bool> down_flag
= { false };
3001 virtual uint64_t interval_msec() = 0;
3002 virtual void stop_process() {}
3004 RGWRadosThread(RGWRados
*_store
, const string
& thread_name
= "radosgw")
3005 : worker(NULL
), cct(_store
->ctx()), store(_store
), thread_name(thread_name
) {}
3006 virtual ~RGWRadosThread() {
3010 virtual int init() { return 0; }
3011 virtual int process() = 0;
3013 bool going_down() { return down_flag
; }
3025 void RGWRadosThread::start()
3027 worker
= new Worker(cct
, this);
3028 worker
->create(thread_name
.c_str());
3031 void RGWRadosThread::stop()
3043 void *RGWRadosThread::Worker::entry() {
3044 uint64_t msec
= processor
->interval_msec();
3045 utime_t interval
= utime_t(msec
/ 1000, (msec
% 1000) * 1000000);
3048 utime_t start
= ceph_clock_now();
3049 int r
= processor
->process();
3051 dout(0) << "ERROR: processor->process() returned error r=" << r
<< dendl
;
3054 if (processor
->going_down())
3057 utime_t end
= ceph_clock_now();
3060 uint64_t cur_msec
= processor
->interval_msec();
3061 if (cur_msec
!= msec
) { /* was it reconfigured? */
3063 interval
= utime_t(msec
/ 1000, (msec
% 1000) * 1000000);
3067 if (interval
<= end
)
3068 continue; // next round
3070 utime_t wait_time
= interval
;
3073 wait_interval(wait_time
);
3077 } while (!processor
->going_down());
3082 class RGWMetaNotifier
: public RGWRadosThread
{
3083 RGWMetaNotifierManager notify_mgr
;
3084 RGWMetadataLog
*const log
;
3086 uint64_t interval_msec() override
{
3087 return cct
->_conf
->rgw_md_notify_interval_msec
;
3090 RGWMetaNotifier(RGWRados
*_store
, RGWMetadataLog
* log
)
3091 : RGWRadosThread(_store
, "meta-notifier"), notify_mgr(_store
), log(log
) {}
3093 int process() override
;
3096 int RGWMetaNotifier::process()
3100 log
->read_clear_modified(shards
);
3102 if (shards
.empty()) {
3106 for (set
<int>::iterator iter
= shards
.begin(); iter
!= shards
.end(); ++iter
) {
3107 ldout(cct
, 20) << __func__
<< "(): notifying mdlog change, shard_id=" << *iter
<< dendl
;
3110 notify_mgr
.notify_all(store
->zone_conn_map
, shards
);
3115 class RGWDataNotifier
: public RGWRadosThread
{
3116 RGWDataNotifierManager notify_mgr
;
3118 uint64_t interval_msec() override
{
3119 return cct
->_conf
->get_val
<int64_t>("rgw_data_notify_interval_msec");
3122 RGWDataNotifier(RGWRados
*_store
) : RGWRadosThread(_store
, "data-notifier"), notify_mgr(_store
) {}
3124 int process() override
;
3127 int RGWDataNotifier::process()
3129 if (!store
->data_log
) {
3133 map
<int, set
<string
> > shards
;
3135 store
->data_log
->read_clear_modified(shards
);
3137 if (shards
.empty()) {
3141 for (map
<int, set
<string
> >::iterator iter
= shards
.begin(); iter
!= shards
.end(); ++iter
) {
3142 ldout(cct
, 20) << __func__
<< "(): notifying datalog change, shard_id=" << iter
->first
<< ": " << iter
->second
<< dendl
;
3145 notify_mgr
.notify_all(store
->zone_data_notify_to_map
, shards
);
3150 class RGWSyncProcessorThread
: public RGWRadosThread
{
3152 RGWSyncProcessorThread(RGWRados
*_store
, const string
& thread_name
= "radosgw") : RGWRadosThread(_store
, thread_name
) {}
3153 RGWSyncProcessorThread(RGWRados
*_store
) : RGWRadosThread(_store
) {}
3154 ~RGWSyncProcessorThread() override
{}
3155 int init() override
= 0 ;
3156 int process() override
= 0;
3159 class RGWMetaSyncProcessorThread
: public RGWSyncProcessorThread
3161 RGWMetaSyncStatusManager sync
;
3163 uint64_t interval_msec() override
{
3164 return 0; /* no interval associated, it'll run once until stopped */
3166 void stop_process() override
{
3170 RGWMetaSyncProcessorThread(RGWRados
*_store
, RGWAsyncRadosProcessor
*async_rados
)
3171 : RGWSyncProcessorThread(_store
, "meta-sync"), sync(_store
, async_rados
) {}
3173 void wakeup_sync_shards(set
<int>& shard_ids
) {
3174 for (set
<int>::iterator iter
= shard_ids
.begin(); iter
!= shard_ids
.end(); ++iter
) {
3178 RGWMetaSyncStatusManager
* get_manager() { return &sync
; }
3180 int init() override
{
3181 int ret
= sync
.init();
3183 ldout(store
->ctx(), 0) << "ERROR: sync.init() returned " << ret
<< dendl
;
3189 int process() override
{
3195 class RGWDataSyncProcessorThread
: public RGWSyncProcessorThread
3197 RGWDataSyncStatusManager sync
;
3200 uint64_t interval_msec() override
{
3202 return 0; /* no interval associated, it'll run once until stopped */
3204 #define DATA_SYNC_INIT_WAIT_SEC 20
3205 return DATA_SYNC_INIT_WAIT_SEC
* 1000;
3208 void stop_process() override
{
3212 RGWDataSyncProcessorThread(RGWRados
*_store
, RGWAsyncRadosProcessor
*async_rados
,
3213 const string
& _source_zone
)
3214 : RGWSyncProcessorThread(_store
, "data-sync"), sync(_store
, async_rados
, _source_zone
),
3215 initialized(false) {}
3217 void wakeup_sync_shards(map
<int, set
<string
> >& shard_ids
) {
3218 for (map
<int, set
<string
> >::iterator iter
= shard_ids
.begin(); iter
!= shard_ids
.end(); ++iter
) {
3219 sync
.wakeup(iter
->first
, iter
->second
);
3222 RGWDataSyncStatusManager
* get_manager() { return &sync
; }
3224 int init() override
{
3228 int process() override
{
3229 while (!initialized
) {
3233 int ret
= sync
.init();
3238 /* we'll be back! */
3246 class RGWSyncLogTrimThread
: public RGWSyncProcessorThread
3248 RGWCoroutinesManager crs
;
3250 RGWHTTPManager http
;
3251 const utime_t trim_interval
;
3253 uint64_t interval_msec() override
{ return 0; }
3254 void stop_process() override
{ crs
.stop(); }
3256 RGWSyncLogTrimThread(RGWRados
*store
, int interval
)
3257 : RGWSyncProcessorThread(store
, "sync-log-trim"),
3258 crs(store
->ctx(), store
->get_cr_registry()), store(store
),
3259 http(store
->ctx(), crs
.get_completion_mgr()),
3260 trim_interval(interval
, 0)
3263 int init() override
{
3264 return http
.set_threaded();
3266 int process() override
{
3267 list
<RGWCoroutinesStack
*> stacks
;
3268 auto meta
= new RGWCoroutinesStack(store
->ctx(), &crs
);
3269 meta
->call(create_meta_log_trim_cr(store
, &http
,
3270 cct
->_conf
->rgw_md_log_max_shards
,
3272 stacks
.push_back(meta
);
3274 auto data
= new RGWCoroutinesStack(store
->ctx(), &crs
);
3275 data
->call(create_data_log_trim_cr(store
, &http
,
3276 cct
->_conf
->rgw_data_log_num_shards
,
3278 stacks
.push_back(data
);
3285 void RGWRados::wakeup_meta_sync_shards(set
<int>& shard_ids
)
3287 Mutex::Locker
l(meta_sync_thread_lock
);
3288 if (meta_sync_processor_thread
) {
3289 meta_sync_processor_thread
->wakeup_sync_shards(shard_ids
);
3293 void RGWRados::wakeup_data_sync_shards(const string
& source_zone
, map
<int, set
<string
> >& shard_ids
)
3295 ldout(ctx(), 20) << __func__
<< ": source_zone=" << source_zone
<< ", shard_ids=" << shard_ids
<< dendl
;
3296 Mutex::Locker
l(data_sync_thread_lock
);
3297 map
<string
, RGWDataSyncProcessorThread
*>::iterator iter
= data_sync_processor_threads
.find(source_zone
);
3298 if (iter
== data_sync_processor_threads
.end()) {
3299 ldout(ctx(), 10) << __func__
<< ": couldn't find sync thread for zone " << source_zone
<< ", skipping async data sync processing" << dendl
;
3303 RGWDataSyncProcessorThread
*thread
= iter
->second
;
3305 thread
->wakeup_sync_shards(shard_ids
);
3308 RGWMetaSyncStatusManager
* RGWRados::get_meta_sync_manager()
3310 Mutex::Locker
l(meta_sync_thread_lock
);
3311 if (meta_sync_processor_thread
) {
3312 return meta_sync_processor_thread
->get_manager();
3317 RGWDataSyncStatusManager
* RGWRados::get_data_sync_manager(const std::string
& source_zone
)
3319 Mutex::Locker
l(data_sync_thread_lock
);
3320 auto thread
= data_sync_processor_threads
.find(source_zone
);
3321 if (thread
== data_sync_processor_threads
.end()) {
3324 return thread
->second
->get_manager();
3327 int RGWRados::get_required_alignment(const rgw_pool
& pool
, uint64_t *alignment
)
3330 int r
= open_pool_ctx(pool
, ioctx
);
3332 ldout(cct
, 0) << "ERROR: open_pool_ctx() returned " << r
<< dendl
;
3337 r
= ioctx
.pool_requires_alignment2(&requires
);
3339 ldout(cct
, 0) << "ERROR: ioctx.pool_requires_alignment2() returned "
3350 r
= ioctx
.pool_required_alignment2(&align
);
3352 ldout(cct
, 0) << "ERROR: ioctx.pool_required_alignment2() returned "
3357 ldout(cct
, 20) << "required alignment=" << align
<< dendl
;
3363 int RGWRados::get_max_chunk_size(const rgw_pool
& pool
, uint64_t *max_chunk_size
)
3365 uint64_t alignment
= 0;
3366 int r
= get_required_alignment(pool
, &alignment
);
3371 uint64_t config_chunk_size
= cct
->_conf
->rgw_max_chunk_size
;
3373 if (alignment
== 0) {
3374 *max_chunk_size
= config_chunk_size
;
3378 if (config_chunk_size
<= alignment
) {
3379 *max_chunk_size
= alignment
;
3383 *max_chunk_size
= config_chunk_size
- (config_chunk_size
% alignment
);
3385 ldout(cct
, 20) << "max_chunk_size=" << *max_chunk_size
<< dendl
;
3390 int RGWRados::get_max_chunk_size(const string
& placement_rule
, const rgw_obj
& obj
, uint64_t *max_chunk_size
)
3393 if (!get_obj_data_pool(placement_rule
, obj
, &pool
)) {
3394 ldout(cct
, 0) << "ERROR: failed to get data pool for object " << obj
<< dendl
;
3397 return get_max_chunk_size(pool
, max_chunk_size
);
3400 class RGWIndexCompletionManager
;
3402 struct complete_op_data
{
3403 Mutex lock
{"complete_op_data"};
3404 AioCompletion
*rados_completion
{nullptr};
3405 int manager_shard_id
{-1};
3406 RGWIndexCompletionManager
*manager
{nullptr};
3410 rgw_bucket_entry_ver ver
;
3411 cls_rgw_obj_key key
;
3412 rgw_bucket_dir_entry_meta dir_meta
;
3413 list
<cls_rgw_obj_key
> remove_objs
;
3416 rgw_zone_set zones_trace
;
3418 bool stopped
{false};
3421 Mutex::Locker
l(lock
);
3426 class RGWIndexCompletionThread
: public RGWRadosThread
{
3429 uint64_t interval_msec() override
{
3433 list
<complete_op_data
*> completions
;
3435 Mutex completions_lock
;
3437 RGWIndexCompletionThread(RGWRados
*_store
)
3438 : RGWRadosThread(_store
, "index-complete"), store(_store
), completions_lock("RGWIndexCompletionThread::completions_lock") {}
3440 int process() override
;
3442 void add_completion(complete_op_data
*completion
) {
3444 Mutex::Locker
l(completions_lock
);
3445 completions
.push_back(completion
);
3452 int RGWIndexCompletionThread::process()
3454 list
<complete_op_data
*> comps
;
3457 Mutex::Locker
l(completions_lock
);
3458 completions
.swap(comps
);
3461 for (auto c
: comps
) {
3462 std::unique_ptr
<complete_op_data
> up
{c
};
3467 ldout(store
->ctx(), 20) << __func__
<< "(): handling completion for key=" << c
->key
<< dendl
;
3469 RGWRados::BucketShard
bs(store
);
3471 int r
= bs
.init(c
->obj
.bucket
, c
->obj
);
3473 ldout(cct
, 0) << "ERROR: " << __func__
<< "(): failed to initialize BucketShard, obj=" << c
->obj
<< " r=" << r
<< dendl
;
3474 /* not much to do */
3478 r
= store
->guard_reshard(&bs
, c
->obj
, [&](RGWRados::BucketShard
*bs
) -> int {
3479 librados::ObjectWriteOperation o
;
3480 cls_rgw_guard_bucket_resharding(o
, -ERR_BUSY_RESHARDING
);
3481 cls_rgw_bucket_complete_op(o
, c
->op
, c
->tag
, c
->ver
, c
->key
, c
->dir_meta
, &c
->remove_objs
,
3482 c
->log_op
, c
->bilog_op
, &c
->zones_trace
);
3484 return bs
->index_ctx
.operate(bs
->bucket_obj
, &o
);
3487 ldout(cct
, 0) << "ERROR: " << __func__
<< "(): bucket index completion failed, obj=" << c
->obj
<< " r=" << r
<< dendl
;
3488 /* ignoring error, can't do anything about it */
3491 r
= store
->data_log
->add_entry(bs
.bucket
, bs
.shard_id
);
3493 lderr(store
->ctx()) << "ERROR: failed writing data log" << dendl
;
3500 class RGWIndexCompletionManager
{
3501 RGWRados
*store
{nullptr};
3502 vector
<Mutex
*> locks
;
3503 vector
<set
<complete_op_data
*> > completions
;
3505 RGWIndexCompletionThread
*completion_thread
{nullptr};
3509 std::atomic
<int> cur_shard
{0};
3513 RGWIndexCompletionManager(RGWRados
*_store
) : store(_store
) {
3514 num_shards
= store
->ctx()->_conf
->rgw_thread_pool_size
;
3516 for (int i
= 0; i
< num_shards
; i
++) {
3518 snprintf(buf
, sizeof(buf
), "RGWIndexCompletionManager::lock::%d", i
);
3519 locks
.push_back(new Mutex(buf
));
3522 completions
.resize(num_shards
);
3524 ~RGWIndexCompletionManager() {
3527 for (auto l
: locks
) {
3533 int result
= cur_shard
% num_shards
;
3538 void create_completion(const rgw_obj
& obj
,
3539 RGWModifyOp op
, string
& tag
,
3540 rgw_bucket_entry_ver
& ver
,
3541 const cls_rgw_obj_key
& key
,
3542 rgw_bucket_dir_entry_meta
& dir_meta
,
3543 list
<cls_rgw_obj_key
> *remove_objs
, bool log_op
,
3545 rgw_zone_set
*zones_trace
,
3546 complete_op_data
**result
);
3547 bool handle_completion(completion_t cb
, complete_op_data
*arg
);
3550 completion_thread
= new RGWIndexCompletionThread(store
);
3551 int ret
= completion_thread
->init();
3555 completion_thread
->start();
3559 if (completion_thread
) {
3560 completion_thread
->stop();
3561 delete completion_thread
;
3564 for (int i
= 0; i
< num_shards
; ++i
) {
3565 Mutex::Locker
l(*locks
[i
]);
3566 for (auto c
: completions
[i
]) {
3567 Mutex::Locker
cl(c
->lock
);
3571 completions
.clear();
3575 static void obj_complete_cb(completion_t cb
, void *arg
)
3577 complete_op_data
*completion
= (complete_op_data
*)arg
;
3578 completion
->lock
.Lock();
3579 if (completion
->stopped
) {
3580 completion
->lock
.Unlock(); /* can drop lock, no one else is referencing us */
3584 bool need_delete
= completion
->manager
->handle_completion(cb
, completion
);
3585 completion
->lock
.Unlock();
3592 void RGWIndexCompletionManager::create_completion(const rgw_obj
& obj
,
3593 RGWModifyOp op
, string
& tag
,
3594 rgw_bucket_entry_ver
& ver
,
3595 const cls_rgw_obj_key
& key
,
3596 rgw_bucket_dir_entry_meta
& dir_meta
,
3597 list
<cls_rgw_obj_key
> *remove_objs
, bool log_op
,
3599 rgw_zone_set
*zones_trace
,
3600 complete_op_data
**result
)
3602 complete_op_data
*entry
= new complete_op_data
;
3604 int shard_id
= next_shard();
3606 entry
->manager_shard_id
= shard_id
;
3607 entry
->manager
= this;
3613 entry
->dir_meta
= dir_meta
;
3614 entry
->log_op
= log_op
;
3615 entry
->bilog_op
= bilog_op
;
3618 for (auto iter
= remove_objs
->begin(); iter
!= remove_objs
->end(); ++iter
) {
3619 entry
->remove_objs
.push_back(*iter
);
3624 entry
->zones_trace
= *zones_trace
;
3626 entry
->zones_trace
.insert(store
->get_zone().id
);
3631 entry
->rados_completion
= librados::Rados::aio_create_completion(entry
, NULL
, obj_complete_cb
);
3633 Mutex::Locker
l(*locks
[shard_id
]);
3634 completions
[shard_id
].insert(entry
);
3637 bool RGWIndexCompletionManager::handle_completion(completion_t cb
, complete_op_data
*arg
)
3639 int shard_id
= arg
->manager_shard_id
;
3641 Mutex::Locker
l(*locks
[shard_id
]);
3643 auto& comps
= completions
[shard_id
];
3645 auto iter
= comps
.find(arg
);
3646 if (iter
== comps
.end()) {
3653 int r
= rados_aio_get_return_value(cb
);
3654 if (r
!= -ERR_BUSY_RESHARDING
) {
3657 completion_thread
->add_completion(arg
);
3661 void RGWRados::finalize()
3663 if (run_sync_thread
) {
3664 Mutex::Locker
l(meta_sync_thread_lock
);
3665 meta_sync_processor_thread
->stop();
3667 Mutex::Locker
dl(data_sync_thread_lock
);
3668 for (auto iter
: data_sync_processor_threads
) {
3669 RGWDataSyncProcessorThread
*thread
= iter
.second
;
3672 if (sync_log_trimmer
) {
3673 sync_log_trimmer
->stop();
3677 async_rados
->stop();
3679 if (run_sync_thread
) {
3680 delete meta_sync_processor_thread
;
3681 meta_sync_processor_thread
= NULL
;
3682 Mutex::Locker
dl(data_sync_thread_lock
);
3683 for (auto iter
: data_sync_processor_threads
) {
3684 RGWDataSyncProcessorThread
*thread
= iter
.second
;
3687 data_sync_processor_threads
.clear();
3688 delete sync_log_trimmer
;
3689 sync_log_trimmer
= nullptr;
3694 if (need_watch_notify()) {
3698 /* delete finisher only after cleaning up watches, as watch error path might call
3699 * into finisher. We stop finisher before finalizing watch to make sure we don't
3700 * actually handle any racing work
3704 if (meta_notifier
) {
3705 meta_notifier
->stop();
3706 delete meta_notifier
;
3708 if (data_notifier
) {
3709 data_notifier
->stop();
3710 delete data_notifier
;
3726 delete rest_master_conn
;
3728 map
<string
, RGWRESTConn
*>::iterator iter
;
3729 for (iter
= zone_conn_map
.begin(); iter
!= zone_conn_map
.end(); ++iter
) {
3730 RGWRESTConn
*conn
= iter
->second
;
3734 for (iter
= zonegroup_conn_map
.begin(); iter
!= zonegroup_conn_map
.end(); ++iter
) {
3735 RGWRESTConn
*conn
= iter
->second
;
3738 RGWQuotaHandler::free_handler(quota_handler
);
3744 delete obj_tombstone_cache
;
3745 delete sync_modules_manager
;
3747 if (reshard_wait
.get()) {
3748 reshard_wait
->stop();
3749 reshard_wait
.reset();
3752 if (run_reshard_thread
) {
3753 reshard
->stop_processor();
3756 delete index_completion_manager
;
3760 * Initialize the RADOS instance and prepare to do other ops
3761 * Returns 0 on success, -ERR# on failure.
3763 int RGWRados::init_rados()
3766 auto handles
= std::vector
<librados::Rados
>{cct
->_conf
->rgw_num_rados_handles
};
3768 for (auto& r
: handles
) {
3769 ret
= r
.init_with_context(cct
);
3779 sync_modules_manager
= new RGWSyncModulesManager();
3781 rgw_register_sync_modules(sync_modules_manager
);
3783 auto crs
= std::unique_ptr
<RGWCoroutinesManagerRegistry
>{
3784 new RGWCoroutinesManagerRegistry(cct
)};
3785 ret
= crs
->hook_to_admin_command("cr dump");
3790 meta_mgr
= new RGWMetadataManager(cct
, this);
3791 data_log
= new RGWDataChangesLog(cct
, this);
3792 cr_registry
= crs
.release();
3794 std::swap(handles
, rados
);
3799 int RGWRados::register_to_service_map(const string
& daemon_type
, const map
<string
, string
>& meta
)
3801 map
<string
,string
> metadata
= meta
;
3802 metadata
["num_handles"] = stringify(rados
.size());
3803 metadata
["zonegroup_id"] = zonegroup
.get_id();
3804 metadata
["zonegroup_name"] = zonegroup
.get_name();
3805 metadata
["zone_name"] = zone_name();
3806 metadata
["zone_id"] = zone_id();;
3807 string name
= cct
->_conf
->name
.get_id();
3808 if (name
.find("rgw.") == 0) {
3809 name
= name
.substr(4);
3811 int ret
= rados
[0].service_daemon_register(daemon_type
, name
, metadata
);
3813 ldout(cct
, 0) << "ERROR: service_daemon_register() returned ret=" << ret
<< ": " << cpp_strerror(-ret
) << dendl
;
3821 * Add new connection to connections map
3822 * @param zonegroup_conn_map map which new connection will be added to
3823 * @param zonegroup zonegroup which new connection will connect to
3824 * @param new_connection pointer to new connection instance
3826 static void add_new_connection_to_map(map
<string
, RGWRESTConn
*> &zonegroup_conn_map
,
3827 const RGWZoneGroup
&zonegroup
, RGWRESTConn
*new_connection
)
3829 // Delete if connection is already exists
3830 map
<string
, RGWRESTConn
*>::iterator iterZoneGroup
= zonegroup_conn_map
.find(zonegroup
.get_id());
3831 if (iterZoneGroup
!= zonegroup_conn_map
.end()) {
3832 delete iterZoneGroup
->second
;
3835 // Add new connection to connections map
3836 zonegroup_conn_map
[zonegroup
.get_id()] = new_connection
;
3839 int RGWRados::convert_regionmap()
3841 RGWZoneGroupMap zonegroupmap
;
3843 string pool_name
= cct
->_conf
->rgw_zone_root_pool
;
3844 if (pool_name
.empty()) {
3845 pool_name
= RGW_DEFAULT_ZONE_ROOT_POOL
;
3847 string oid
= region_map_oid
;
3849 rgw_pool
pool(pool_name
);
3851 RGWObjectCtx
obj_ctx(this);
3852 int ret
= rgw_get_system_obj(this, obj_ctx
, pool
, oid
, bl
, NULL
, NULL
);
3853 if (ret
< 0 && ret
!= -ENOENT
) {
3855 } else if (ret
== -ENOENT
) {
3860 bufferlist::iterator iter
= bl
.begin();
3861 ::decode(zonegroupmap
, iter
);
3862 } catch (buffer::error
& err
) {
3863 ldout(cct
, 0) << "error decoding regionmap from " << pool
<< ":" << oid
<< dendl
;
3867 for (map
<string
, RGWZoneGroup
>::iterator iter
= zonegroupmap
.zonegroups
.begin();
3868 iter
!= zonegroupmap
.zonegroups
.end(); ++iter
) {
3869 RGWZoneGroup
& zonegroup
= iter
->second
;
3870 ret
= zonegroup
.init(cct
, this, false);
3871 ret
= zonegroup
.update();
3872 if (ret
< 0 && ret
!= -ENOENT
) {
3873 ldout(cct
, 0) << "Error could not update zonegroup " << zonegroup
.get_name() << ": " <<
3874 cpp_strerror(-ret
) << dendl
;
3876 } else if (ret
== -ENOENT
) {
3877 ret
= zonegroup
.create();
3879 ldout(cct
, 0) << "Error could not create " << zonegroup
.get_name() << ": " <<
3880 cpp_strerror(-ret
) << dendl
;
3886 current_period
.set_user_quota(zonegroupmap
.user_quota
);
3887 current_period
.set_bucket_quota(zonegroupmap
.bucket_quota
);
3889 // remove the region_map so we don't try to convert again
3890 rgw_raw_obj
obj(pool
, oid
);
3891 ret
= delete_system_obj(obj
);
3893 ldout(cct
, 0) << "Error could not remove " << obj
3894 << " after upgrading to zonegroup map: " << cpp_strerror(ret
) << dendl
;
3902 * Replace all region configuration with zonegroup for
3903 * backward compatability
3904 * Returns 0 on success, -ERR# on failure.
3906 int RGWRados::replace_region_with_zonegroup()
3908 /* copy default region */
3909 /* convert default region to default zonegroup */
3910 string default_oid
= cct
->_conf
->rgw_default_region_info_oid
;
3911 if (default_oid
.empty()) {
3912 default_oid
= default_region_info_oid
;
3916 RGWZoneGroup default_zonegroup
;
3917 rgw_pool pool
{default_zonegroup
.get_pool(cct
)};
3918 string oid
= "converted";
3920 RGWObjectCtx
obj_ctx(this);
3922 int ret
= rgw_get_system_obj(this, obj_ctx
, pool
,oid
, bl
, NULL
, NULL
);
3923 if (ret
< 0 && ret
!= -ENOENT
) {
3924 ldout(cct
, 0) << __func__
<< " failed to read converted: ret "<< ret
<< " " << cpp_strerror(-ret
)
3927 } else if (ret
!= -ENOENT
) {
3928 ldout(cct
, 20) << "System already converted " << dendl
;
3932 string default_region
;
3933 ret
= default_zonegroup
.init(cct
, this, false, true);
3935 ldout(cct
, 0) << __func__
<< " failed init default region: ret "<< ret
<< " " << cpp_strerror(-ret
) << dendl
;
3938 ret
= default_zonegroup
.read_default_id(default_region
, true);
3939 if (ret
< 0 && ret
!= -ENOENT
) {
3940 ldout(cct
, 0) << __func__
<< " failed reading old default region: ret "<< ret
<< " " << cpp_strerror(-ret
) << dendl
;
3944 /* convert regions to zonegroups */
3945 list
<string
> regions
;
3946 ret
= list_regions(regions
);
3947 if (ret
< 0 && ret
!= -ENOENT
) {
3948 ldout(cct
, 0) << __func__
<< " failed to list regions: ret "<< ret
<< " " << cpp_strerror(-ret
) << dendl
;
3950 } else if (ret
== -ENOENT
|| regions
.empty()) {
3951 RGWZoneParams
zoneparams(default_zone_name
);
3952 int ret
= zoneparams
.init(cct
, this);
3953 if (ret
< 0 && ret
!= -ENOENT
) {
3954 ldout(cct
, 0) << __func__
<< ": error initializing default zone params: " << cpp_strerror(-ret
) << dendl
;
3957 /* update master zone */
3958 RGWZoneGroup
default_zg(default_zonegroup_name
);
3959 ret
= default_zg
.init(cct
, this);
3960 if (ret
< 0 && ret
!= -ENOENT
) {
3961 ldout(cct
, 0) << __func__
<< ": error in initializing default zonegroup: " << cpp_strerror(-ret
) << dendl
;
3964 if (ret
!= -ENOENT
&& default_zg
.master_zone
.empty()) {
3965 default_zg
.master_zone
= zoneparams
.get_id();
3966 return default_zg
.update();
3971 string master_region
, master_zone
;
3972 for (list
<string
>::iterator iter
= regions
.begin(); iter
!= regions
.end(); ++iter
) {
3973 if (*iter
!= default_zonegroup_name
){
3974 RGWZoneGroup
region(*iter
);
3975 int ret
= region
.init(cct
, this, true, true);
3977 ldout(cct
, 0) << __func__
<< " failed init region "<< *iter
<< ": " << cpp_strerror(-ret
) << dendl
;
3980 if (region
.is_master_zonegroup()) {
3981 master_region
= region
.get_id();
3982 master_zone
= region
.master_zone
;
3987 /* create realm if there is none.
3988 The realm name will be the region and zone concatenated
3989 realm id will be mds of its name */
3990 if (realm
.get_id().empty() && !master_region
.empty() && !master_zone
.empty()) {
3991 string new_realm_name
= master_region
+ "." + master_zone
;
3992 unsigned char md5
[CEPH_CRYPTO_MD5_DIGESTSIZE
];
3993 char md5_str
[CEPH_CRYPTO_MD5_DIGESTSIZE
* 2 + 1];
3995 hash
.Update((const byte
*)new_realm_name
.c_str(), new_realm_name
.length());
3997 buf_to_hex(md5
, CEPH_CRYPTO_MD5_DIGESTSIZE
, md5_str
);
3998 string
new_realm_id(md5_str
);
3999 RGWRealm
new_realm(new_realm_id
,new_realm_name
);
4000 ret
= new_realm
.init(cct
, this, false);
4002 ldout(cct
, 0) << __func__
<< " Error initing new realm: " << cpp_strerror(-ret
) << dendl
;
4005 ret
= new_realm
.create();
4006 if (ret
< 0 && ret
!= -EEXIST
) {
4007 ldout(cct
, 0) << __func__
<< " Error creating new realm: " << cpp_strerror(-ret
) << dendl
;
4010 ret
= new_realm
.set_as_default();
4012 ldout(cct
, 0) << __func__
<< " Error setting realm as default: " << cpp_strerror(-ret
) << dendl
;
4015 ret
= realm
.init(cct
, this);
4017 ldout(cct
, 0) << __func__
<< " Error initing realm: " << cpp_strerror(-ret
) << dendl
;
4020 ret
= current_period
.init(cct
, this, realm
.get_id(), realm
.get_name());
4022 ldout(cct
, 0) << __func__
<< " Error initing current period: " << cpp_strerror(-ret
) << dendl
;
4027 list
<string
>::iterator iter
;
4028 /* create zonegroups */
4029 for (iter
= regions
.begin(); iter
!= regions
.end(); ++iter
)
4031 ldout(cct
, 0) << __func__
<< "Converting " << *iter
<< dendl
;
4032 /* check to see if we don't have already a zonegroup with this name */
4033 RGWZoneGroup
new_zonegroup(*iter
);
4034 ret
= new_zonegroup
.init(cct
, this);
4035 if (ret
== 0 && new_zonegroup
.get_id() != *iter
) {
4036 ldout(cct
, 0) << __func__
<< " zonegroup "<< *iter
<< " already exists id " << new_zonegroup
.get_id () <<
4037 " skipping conversion " << dendl
;
4040 RGWZoneGroup
zonegroup(*iter
);
4041 zonegroup
.set_id(*iter
);
4042 int ret
= zonegroup
.init(cct
, this, true, true);
4044 ldout(cct
, 0) << __func__
<< " failed init zonegroup: ret "<< ret
<< " " << cpp_strerror(-ret
) << dendl
;
4047 zonegroup
.realm_id
= realm
.get_id();
4048 /* fix default region master zone */
4049 if (*iter
== default_zonegroup_name
&& zonegroup
.master_zone
.empty()) {
4050 ldout(cct
, 0) << __func__
<< " Setting default zone as master for default region" << dendl
;
4051 zonegroup
.master_zone
= default_zone_name
;
4053 ret
= zonegroup
.update();
4054 if (ret
< 0 && ret
!= -EEXIST
) {
4055 ldout(cct
, 0) << __func__
<< " failed to update zonegroup " << *iter
<< ": ret "<< ret
<< " " << cpp_strerror(-ret
)
4059 ret
= zonegroup
.update_name();
4060 if (ret
< 0 && ret
!= -EEXIST
) {
4061 ldout(cct
, 0) << __func__
<< " failed to update_name for zonegroup " << *iter
<< ": ret "<< ret
<< " " << cpp_strerror(-ret
)
4065 if (zonegroup
.get_name() == default_region
) {
4066 ret
= zonegroup
.set_as_default();
4068 ldout(cct
, 0) << __func__
<< " failed to set_as_default " << *iter
<< ": ret "<< ret
<< " " << cpp_strerror(-ret
)
4073 for (map
<string
, RGWZone
>::const_iterator iter
= zonegroup
.zones
.begin(); iter
!= zonegroup
.zones
.end();
4075 ldout(cct
, 0) << __func__
<< " Converting zone" << iter
->first
<< dendl
;
4076 RGWZoneParams
zoneparams(iter
->first
, iter
->first
);
4077 zoneparams
.set_id(iter
->first
);
4078 zoneparams
.realm_id
= realm
.get_id();
4079 ret
= zoneparams
.init(cct
, this);
4080 if (ret
< 0 && ret
!= -ENOENT
) {
4081 ldout(cct
, 0) << __func__
<< " failed to init zoneparams " << iter
->first
<< ": " << cpp_strerror(-ret
) << dendl
;
4083 } else if (ret
== -ENOENT
) {
4084 ldout(cct
, 0) << __func__
<< " zone is part of another cluster " << iter
->first
<< " skipping " << dendl
;
4087 zonegroup
.realm_id
= realm
.get_id();
4088 ret
= zoneparams
.update();
4089 if (ret
< 0 && ret
!= -EEXIST
) {
4090 ldout(cct
, 0) << __func__
<< " failed to update zoneparams " << iter
->first
<< ": " << cpp_strerror(-ret
) << dendl
;
4093 ret
= zoneparams
.update_name();
4094 if (ret
< 0 && ret
!= -EEXIST
) {
4095 ldout(cct
, 0) << __func__
<< " failed to init zoneparams " << iter
->first
<< ": " << cpp_strerror(-ret
) << dendl
;
4100 if (!current_period
.get_id().empty()) {
4101 ret
= current_period
.add_zonegroup(zonegroup
);
4103 ldout(cct
, 0) << __func__
<< " failed to add zonegroup to current_period: " << cpp_strerror(-ret
) << dendl
;
4109 if (!current_period
.get_id().empty()) {
4110 ret
= current_period
.update();
4112 ldout(cct
, 0) << __func__
<< " failed to update new period: " << cpp_strerror(-ret
) << dendl
;
4115 ret
= current_period
.store_info(false);
4117 ldout(cct
, 0) << __func__
<< " failed to store new period: " << cpp_strerror(-ret
) << dendl
;
4120 ret
= current_period
.reflect();
4122 ldout(cct
, 0) << __func__
<< " failed to update local objects: " << cpp_strerror(-ret
) << dendl
;
4127 for (auto const& iter
: regions
) {
4128 RGWZoneGroup
zonegroup(iter
);
4129 int ret
= zonegroup
.init(cct
, this, true, true);
4131 ldout(cct
, 0) << __func__
<< " failed init zonegroup" << iter
<< ": ret "<< ret
<< " " << cpp_strerror(-ret
) << dendl
;
4134 ret
= zonegroup
.delete_obj(true);
4135 if (ret
< 0 && ret
!= -ENOENT
) {
4136 ldout(cct
, 0) << __func__
<< " failed to delete region " << iter
<< ": ret "<< ret
<< " " << cpp_strerror(-ret
)
4142 /* mark as converted */
4143 ret
= rgw_put_system_obj(this, pool
, oid
, bl
.c_str(), bl
.length(),
4144 true, NULL
, real_time(), NULL
);
4146 ldout(cct
, 0) << __func__
<< " failed to mark cluster as converted: ret "<< ret
<< " " << cpp_strerror(-ret
)
4154 int RGWRados::init_zg_from_period(bool *initialized
)
4156 *initialized
= false;
4158 if (current_period
.get_id().empty()) {
4162 int ret
= zonegroup
.init(cct
, this);
4163 ldout(cct
, 20) << "period zonegroup init ret " << ret
<< dendl
;
4164 if (ret
== -ENOENT
) {
4168 ldout(cct
, 0) << "failed reading zonegroup info: " << cpp_strerror(-ret
) << dendl
;
4171 ldout(cct
, 20) << "period zonegroup name " << zonegroup
.get_name() << dendl
;
4173 map
<string
, RGWZoneGroup
>::const_iterator iter
=
4174 current_period
.get_map().zonegroups
.find(zonegroup
.get_id());
4176 if (iter
!= current_period
.get_map().zonegroups
.end()) {
4177 ldout(cct
, 20) << "using current period zonegroup " << zonegroup
.get_name() << dendl
;
4178 zonegroup
= iter
->second
;
4179 ret
= zonegroup
.init(cct
, this, false);
4181 ldout(cct
, 0) << "failed init zonegroup: " << " " << cpp_strerror(-ret
) << dendl
;
4184 ret
= zone_params
.init(cct
, this);
4185 if (ret
< 0 && ret
!= -ENOENT
) {
4186 ldout(cct
, 0) << "failed reading zone params info: " << " " << cpp_strerror(-ret
) << dendl
;
4188 } if (ret
==-ENOENT
&& zonegroup
.get_name() == default_zonegroup_name
) {
4189 ldout(cct
, 10) << " Using default name "<< default_zone_name
<< dendl
;
4190 zone_params
.set_name(default_zone_name
);
4191 ret
= zone_params
.init(cct
, this);
4192 if (ret
< 0 && ret
!= -ENOENT
) {
4193 ldout(cct
, 0) << "failed reading zone params info: " << " " << cpp_strerror(-ret
) << dendl
;
4198 for (iter
= current_period
.get_map().zonegroups
.begin();
4199 iter
!= current_period
.get_map().zonegroups
.end(); ++iter
){
4200 const RGWZoneGroup
& zg
= iter
->second
;
4201 // use endpoints from the zonegroup's master zone
4202 auto master
= zg
.zones
.find(zg
.master_zone
);
4203 if (master
== zg
.zones
.end()) {
4204 // fix missing master zone for a single zone zonegroup
4205 if (zg
.master_zone
.empty() && zg
.zones
.size() == 1) {
4206 master
= zg
.zones
.begin();
4207 ldout(cct
, 0) << "zonegroup " << zg
.get_name() << " missing master_zone, setting zone " <<
4208 master
->second
.name
<< " id:" << master
->second
.id
<< " as master" << dendl
;
4209 if (zonegroup
.get_id() == zg
.get_id()) {
4210 zonegroup
.master_zone
= master
->second
.id
;
4211 ret
= zonegroup
.update();
4213 ldout(cct
, 0) << "error updating zonegroup : " << cpp_strerror(-ret
) << dendl
;
4217 RGWZoneGroup
fixed_zg(zg
.get_id(),zg
.get_name());
4218 ret
= fixed_zg
.init(cct
, this);
4220 ldout(cct
, 0) << "error initializing zonegroup : " << cpp_strerror(-ret
) << dendl
;
4223 fixed_zg
.master_zone
= master
->second
.id
;
4224 ret
= fixed_zg
.update();
4226 ldout(cct
, 0) << "error initializing zonegroup : " << cpp_strerror(-ret
) << dendl
;
4231 ldout(cct
, 0) << "zonegroup " << zg
.get_name() << " missing zone for master_zone=" <<
4232 zg
.master_zone
<< dendl
;
4236 const auto& endpoints
= master
->second
.endpoints
;
4237 add_new_connection_to_map(zonegroup_conn_map
, zg
, new RGWRESTConn(cct
, this, zg
.get_id(), endpoints
));
4238 if (!current_period
.get_master_zonegroup().empty() &&
4239 zg
.get_id() == current_period
.get_master_zonegroup()) {
4240 rest_master_conn
= new RGWRESTConn(cct
, this, zg
.get_id(), endpoints
);
4244 *initialized
= true;
4249 int RGWRados::init_zg_from_local(bool *creating_defaults
)
4251 int ret
= zonegroup
.init(cct
, this);
4252 if ( (ret
< 0 && ret
!= -ENOENT
) || (ret
== -ENOENT
&& !cct
->_conf
->rgw_zonegroup
.empty())) {
4253 ldout(cct
, 0) << "failed reading zonegroup info: ret "<< ret
<< " " << cpp_strerror(-ret
) << dendl
;
4255 } else if (ret
== -ENOENT
) {
4256 *creating_defaults
= true;
4257 ldout(cct
, 10) << "Creating default zonegroup " << dendl
;
4258 ret
= zonegroup
.create_default();
4260 ldout(cct
, 0) << "failure in zonegroup create_default: ret "<< ret
<< " " << cpp_strerror(-ret
)
4264 ret
= zonegroup
.init(cct
, this);
4266 ldout(cct
, 0) << "failure in zonegroup create_default: ret "<< ret
<< " " << cpp_strerror(-ret
)
4271 ldout(cct
, 20) << "zonegroup " << zonegroup
.get_name() << dendl
;
4272 if (zonegroup
.is_master_zonegroup()) {
4273 // use endpoints from the zonegroup's master zone
4274 auto master
= zonegroup
.zones
.find(zonegroup
.master_zone
);
4275 if (master
== zonegroup
.zones
.end()) {
4276 // fix missing master zone for a single zone zonegroup
4277 if (zonegroup
.master_zone
.empty() && zonegroup
.zones
.size() == 1) {
4278 master
= zonegroup
.zones
.begin();
4279 ldout(cct
, 0) << "zonegroup " << zonegroup
.get_name() << " missing master_zone, setting zone " <<
4280 master
->second
.name
<< " id:" << master
->second
.id
<< " as master" << dendl
;
4281 zonegroup
.master_zone
= master
->second
.id
;
4282 ret
= zonegroup
.update();
4284 ldout(cct
, 0) << "error initializing zonegroup : " << cpp_strerror(-ret
) << dendl
;
4288 ldout(cct
, 0) << "zonegroup " << zonegroup
.get_name() << " missing zone for "
4289 "master_zone=" << zonegroup
.master_zone
<< dendl
;
4293 const auto& endpoints
= master
->second
.endpoints
;
4294 rest_master_conn
= new RGWRESTConn(cct
, this, zonegroup
.get_id(), endpoints
);
4301 bool RGWRados::zone_syncs_from(RGWZone
& target_zone
, RGWZone
& source_zone
)
4303 return target_zone
.syncs_from(source_zone
.name
) &&
4304 sync_modules_manager
->supports_data_export(source_zone
.tier_type
);
4308 * Initialize the RADOS instance and prepare to do other ops
4309 * Returns 0 on success, -ERR# on failure.
4311 int RGWRados::init_complete()
4313 int ret
= realm
.init(cct
, this);
4314 if (ret
< 0 && ret
!= -ENOENT
) {
4315 ldout(cct
, 0) << "failed reading realm info: ret "<< ret
<< " " << cpp_strerror(-ret
) << dendl
;
4317 } else if (ret
!= -ENOENT
) {
4318 ldout(cct
, 20) << "realm " << realm
.get_name() << " " << realm
.get_id() << dendl
;
4319 ret
= current_period
.init(cct
, this, realm
.get_id(), realm
.get_name());
4320 if (ret
< 0 && ret
!= -ENOENT
) {
4321 ldout(cct
, 0) << "failed reading current period info: " << " " << cpp_strerror(-ret
) << dendl
;
4324 ldout(cct
, 20) << "current period " << current_period
.get_id() << dendl
;
4327 ret
= replace_region_with_zonegroup();
4329 lderr(cct
) << "failed converting region to zonegroup : ret "<< ret
<< " " << cpp_strerror(-ret
) << dendl
;
4333 ret
= convert_regionmap();
4335 lderr(cct
) << "failed converting regionmap: " << cpp_strerror(-ret
) << dendl
;
4339 bool zg_initialized
= false;
4341 if (!current_period
.get_id().empty()) {
4342 ret
= init_zg_from_period(&zg_initialized
);
4348 bool creating_defaults
= false;
4349 bool using_local
= (!zg_initialized
);
4351 ldout(cct
, 10) << " cannot find current period zonegroup using local zonegroup" << dendl
;
4352 ret
= init_zg_from_local(&creating_defaults
);
4356 // read period_config into current_period
4357 auto& period_config
= current_period
.get_config();
4358 ret
= period_config
.read(this, zonegroup
.realm_id
);
4359 if (ret
< 0 && ret
!= -ENOENT
) {
4360 ldout(cct
, 0) << "ERROR: failed to read period config: "
4361 << cpp_strerror(ret
) << dendl
;
4366 ldout(cct
, 10) << "Cannot find current period zone using local zone" << dendl
;
4367 if (creating_defaults
&& cct
->_conf
->rgw_zone
.empty()) {
4368 ldout(cct
, 10) << " Using default name "<< default_zone_name
<< dendl
;
4369 zone_params
.set_name(default_zone_name
);
4372 ret
= zone_params
.init(cct
, this);
4373 if (ret
< 0 && ret
!= -ENOENT
) {
4374 lderr(cct
) << "failed reading zone info: ret "<< ret
<< " " << cpp_strerror(-ret
) << dendl
;
4377 map
<string
, RGWZone
>::iterator zone_iter
= get_zonegroup().zones
.find(zone_params
.get_id());
4378 if (zone_iter
== get_zonegroup().zones
.end()) {
4380 lderr(cct
) << "Cannot find zone id=" << zone_params
.get_id() << " (name=" << zone_params
.get_name() << ")" << dendl
;
4383 ldout(cct
, 1) << "Cannot find zone id=" << zone_params
.get_id() << " (name=" << zone_params
.get_name() << "), switching to local zonegroup configuration" << dendl
;
4384 ret
= init_zg_from_local(&creating_defaults
);
4388 zone_iter
= get_zonegroup().zones
.find(zone_params
.get_id());
4390 if (zone_iter
!= get_zonegroup().zones
.end()) {
4391 zone_public_config
= zone_iter
->second
;
4392 ldout(cct
, 20) << "zone " << zone_params
.get_name() << dendl
;
4394 lderr(cct
) << "Cannot find zone id=" << zone_params
.get_id() << " (name=" << zone_params
.get_name() << ")" << dendl
;
4398 zone_short_id
= current_period
.get_map().get_zone_short_id(zone_params
.get_id());
4400 if (run_sync_thread
) {
4401 ret
= sync_modules_manager
->create_instance(cct
, zone_public_config
.tier_type
, zone_params
.tier_config
, &sync_module
);
4403 lderr(cct
) << "ERROR: failed to init sync module instance, ret=" << ret
<< dendl
;
4408 writeable_zone
= (zone_public_config
.tier_type
.empty() || zone_public_config
.tier_type
== "rgw");
4410 init_unique_trans_id_deps();
4412 finisher
= new Finisher(cct
);
4415 period_puller
.reset(new RGWPeriodPuller(this));
4416 period_history
.reset(new RGWPeriodHistory(cct
, period_puller
.get(),
4419 if (need_watch_notify()) {
4422 lderr(cct
) << "ERROR: failed to initialize watch: " << cpp_strerror(-ret
) << dendl
;
4427 /* first build all zones index */
4428 for (auto ziter
: get_zonegroup().zones
) {
4429 const string
& id
= ziter
.first
;
4430 RGWZone
& z
= ziter
.second
;
4431 zone_id_by_name
[z
.name
] = id
;
4435 if (zone_by_id
.find(zone_id()) == zone_by_id
.end()) {
4436 ldout(cct
, 0) << "WARNING: could not find zone config in zonegroup for local zone (" << zone_id() << "), will use defaults" << dendl
;
4438 zone_public_config
= zone_by_id
[zone_id()];
4439 for (auto ziter
: get_zonegroup().zones
) {
4440 const string
& id
= ziter
.first
;
4441 RGWZone
& z
= ziter
.second
;
4442 if (id
== zone_id()) {
4445 if (z
.endpoints
.empty()) {
4446 ldout(cct
, 0) << "WARNING: can't generate connection for zone " << z
.id
<< " id " << z
.name
<< ": no endpoints defined" << dendl
;
4449 ldout(cct
, 20) << "generating connection object for zone " << z
.name
<< " id " << z
.id
<< dendl
;
4450 RGWRESTConn
*conn
= new RGWRESTConn(cct
, this, z
.id
, z
.endpoints
);
4451 zone_conn_map
[id
] = conn
;
4452 if (zone_syncs_from(zone_public_config
, z
) ||
4453 zone_syncs_from(z
, zone_public_config
)) {
4454 if (zone_syncs_from(zone_public_config
, z
)) {
4455 zone_data_sync_from_map
[id
] = conn
;
4457 if (zone_syncs_from(z
, zone_public_config
)) {
4458 zone_data_notify_to_map
[id
] = conn
;
4461 ldout(cct
, 20) << "NOTICE: not syncing to/from zone " << z
.name
<< " id " << z
.id
<< dendl
;
4465 ret
= open_root_pool_ctx();
4469 ret
= open_gc_pool_ctx();
4473 ret
= open_lc_pool_ctx();
4477 ret
= open_objexp_pool_ctx();
4481 ret
= open_reshard_pool_ctx();
4485 pools_initialized
= true;
4488 gc
->initialize(cct
, this);
4490 obj_expirer
= new RGWObjectExpirer(this);
4492 if (use_gc_thread
) {
4493 gc
->start_processor();
4494 obj_expirer
->start_processor();
4497 if (run_sync_thread
) {
4498 // initialize the log period history. we want to do this any time we're not
4499 // running under radosgw-admin, so we check run_sync_thread here before
4500 // disabling it based on the zone/zonegroup setup
4501 meta_mgr
->init_oldest_log_period();
4504 /* no point of running sync thread if we don't have a master zone configured
4505 or there is no rest_master_conn */
4506 if (get_zonegroup().master_zone
.empty() || !rest_master_conn
4507 || current_period
.get_id().empty()) {
4508 run_sync_thread
= false;
4511 async_rados
= new RGWAsyncRadosProcessor(this, cct
->_conf
->rgw_num_async_rados_threads
);
4512 async_rados
->start();
4514 ret
= meta_mgr
->init(current_period
.get_id());
4516 lderr(cct
) << "ERROR: failed to initialize metadata log: "
4517 << cpp_strerror(-ret
) << dendl
;
4521 if (is_meta_master()) {
4522 auto md_log
= meta_mgr
->get_log(current_period
.get_id());
4523 meta_notifier
= new RGWMetaNotifier(this, md_log
);
4524 meta_notifier
->start();
4527 if (run_sync_thread
) {
4528 Mutex::Locker
l(meta_sync_thread_lock
);
4529 meta_sync_processor_thread
= new RGWMetaSyncProcessorThread(this, async_rados
);
4530 ret
= meta_sync_processor_thread
->init();
4532 ldout(cct
, 0) << "ERROR: failed to initialize meta sync thread" << dendl
;
4535 meta_sync_processor_thread
->start();
4537 Mutex::Locker
dl(data_sync_thread_lock
);
4538 for (auto iter
: zone_data_sync_from_map
) {
4539 ldout(cct
, 5) << "starting data sync thread for zone " << iter
.first
<< dendl
;
4540 RGWDataSyncProcessorThread
*thread
= new RGWDataSyncProcessorThread(this, async_rados
, iter
.first
);
4541 ret
= thread
->init();
4543 ldout(cct
, 0) << "ERROR: failed to initialize data sync thread" << dendl
;
4547 data_sync_processor_threads
[iter
.first
] = thread
;
4549 auto interval
= cct
->_conf
->rgw_sync_log_trim_interval
;
4551 sync_log_trimmer
= new RGWSyncLogTrimThread(this, interval
);
4552 ret
= sync_log_trimmer
->init();
4554 ldout(cct
, 0) << "ERROR: failed to initialize sync log trim thread" << dendl
;
4557 sync_log_trimmer
->start();
4560 data_notifier
= new RGWDataNotifier(this);
4561 data_notifier
->start();
4564 lc
->initialize(cct
, this);
4567 lc
->start_processor();
4569 quota_handler
= RGWQuotaHandler::generate_handler(this, quota_threads
);
4571 bucket_index_max_shards
= (cct
->_conf
->rgw_override_bucket_index_max_shards
? cct
->_conf
->rgw_override_bucket_index_max_shards
:
4572 get_zone().bucket_index_max_shards
);
4573 if (bucket_index_max_shards
> get_max_bucket_shards()) {
4574 bucket_index_max_shards
= get_max_bucket_shards();
4575 ldout(cct
, 1) << __func__
<< " bucket index max shards is too large, reset to value: "
4576 << get_max_bucket_shards() << dendl
;
4578 ldout(cct
, 20) << __func__
<< " bucket index max shards: " << bucket_index_max_shards
<< dendl
;
4580 binfo_cache
= new RGWChainedCacheImpl
<bucket_info_entry
>;
4581 binfo_cache
->init(this);
4583 bool need_tombstone_cache
= !zone_data_notify_to_map
.empty(); /* have zones syncing from us */
4585 if (need_tombstone_cache
) {
4586 obj_tombstone_cache
= new tombstone_cache_t(cct
->_conf
->rgw_obj_tombstone_cache_size
);
4589 reshard_wait
= std::make_shared
<RGWReshardWait
>(this);
4591 reshard
= new RGWReshard(this);
4593 /* only the master zone in the zonegroup reshards buckets */
4594 run_reshard_thread
= run_reshard_thread
&& (get_zonegroup().master_zone
== zone_public_config
.id
);
4595 if (run_reshard_thread
) {
4596 reshard
->start_processor();
4599 index_completion_manager
= new RGWIndexCompletionManager(this);
4600 ret
= index_completion_manager
->start();
4606 * Initialize the RADOS instance and prepare to do other ops
4607 * Returns 0 on success, -ERR# on failure.
4609 int RGWRados::initialize()
4617 return init_complete();
4620 void RGWRados::finalize_watch()
4622 for (int i
= 0; i
< num_watchers
; i
++) {
4623 RGWWatcher
*watcher
= watchers
[i
];
4624 watcher
->unregister_watch();
4628 delete[] notify_oids
;
4632 void RGWRados::schedule_context(Context
*c
) {
4636 int RGWRados::list_raw_prefixed_objs(const rgw_pool
& pool
, const string
& prefix
, list
<string
>& result
)
4639 RGWListRawObjsCtx ctx
;
4642 int r
= list_raw_objects(pool
, prefix
, 1000,
4643 ctx
, oids
, &is_truncated
);
4647 list
<string
>::iterator iter
;
4648 for (iter
= oids
.begin(); iter
!= oids
.end(); ++iter
) {
4649 string
& val
= *iter
;
4650 if (val
.size() > prefix
.size())
4651 result
.push_back(val
.substr(prefix
.size()));
4653 } while (is_truncated
);
4658 int RGWRados::list_regions(list
<string
>& regions
)
4660 RGWZoneGroup zonegroup
;
4662 return list_raw_prefixed_objs(zonegroup
.get_pool(cct
), region_info_oid_prefix
, regions
);
4665 int RGWRados::list_zonegroups(list
<string
>& zonegroups
)
4667 RGWZoneGroup zonegroup
;
4669 return list_raw_prefixed_objs(zonegroup
.get_pool(cct
), zonegroup_names_oid_prefix
, zonegroups
);
4672 int RGWRados::list_zones(list
<string
>& zones
)
4674 RGWZoneParams zoneparams
;
4676 return list_raw_prefixed_objs(zoneparams
.get_pool(cct
), zone_names_oid_prefix
, zones
);
4679 int RGWRados::list_realms(list
<string
>& realms
)
4681 RGWRealm
realm(cct
, this);
4682 return list_raw_prefixed_objs(realm
.get_pool(cct
), realm_names_oid_prefix
, realms
);
4685 int RGWRados::list_periods(list
<string
>& periods
)
4688 list
<string
> raw_periods
;
4689 int ret
= list_raw_prefixed_objs(period
.get_pool(cct
), period
.get_info_oid_prefix(), raw_periods
);
4693 for (const auto& oid
: raw_periods
) {
4694 size_t pos
= oid
.find(".");
4695 if (pos
!= std::string::npos
) {
4696 periods
.push_back(oid
.substr(0, pos
));
4698 periods
.push_back(oid
);
4701 periods
.sort(); // unique() only detects duplicates if they're adjacent
4707 int RGWRados::list_periods(const string
& current_period
, list
<string
>& periods
)
4710 string period_id
= current_period
;
4711 while(!period_id
.empty()) {
4712 RGWPeriod
period(period_id
);
4713 ret
= period
.init(cct
, this);
4717 periods
.push_back(period
.get_id());
4718 period_id
= period
.get_predecessor();
4725 * Open the pool used as root for this gateway
4726 * Returns: 0 on success, -ERR# otherwise.
4728 int RGWRados::open_root_pool_ctx()
4730 return rgw_init_ioctx(get_rados_handle(), get_zone_params().domain_root
, root_pool_ctx
, true);
4733 int RGWRados::open_gc_pool_ctx()
4735 return rgw_init_ioctx(get_rados_handle(), get_zone_params().gc_pool
, gc_pool_ctx
, true);
4738 int RGWRados::open_lc_pool_ctx()
4740 return rgw_init_ioctx(get_rados_handle(), get_zone_params().lc_pool
, lc_pool_ctx
, true);
4743 int RGWRados::open_objexp_pool_ctx()
4745 return rgw_init_ioctx(get_rados_handle(), get_zone_params().log_pool
, objexp_pool_ctx
, true);
4748 int RGWRados::open_reshard_pool_ctx()
4750 return rgw_init_ioctx(get_rados_handle(), get_zone_params().reshard_pool
, reshard_pool_ctx
, true);
4753 int RGWRados::init_watch()
4755 int r
= rgw_init_ioctx(&rados
[0], get_zone_params().control_pool
, control_pool_ctx
, true);
4760 num_watchers
= cct
->_conf
->rgw_num_control_oids
;
4762 bool compat_oid
= (num_watchers
== 0);
4764 if (num_watchers
<= 0)
4767 notify_oids
= new string
[num_watchers
];
4768 watchers
= new RGWWatcher
*[num_watchers
];
4770 for (int i
=0; i
< num_watchers
; i
++) {
4771 string
& notify_oid
= notify_oids
[i
];
4772 notify_oid
= notify_oid_prefix
;
4775 snprintf(buf
, sizeof(buf
), ".%d", i
);
4776 notify_oid
.append(buf
);
4778 r
= control_pool_ctx
.create(notify_oid
, false);
4779 if (r
< 0 && r
!= -EEXIST
)
4782 RGWWatcher
*watcher
= new RGWWatcher(this, i
, notify_oid
);
4783 watchers
[i
] = watcher
;
4785 r
= watcher
->register_watch();
4790 watch_initialized
= true;
4792 set_cache_enabled(true);
4797 void RGWRados::pick_control_oid(const string
& key
, string
& notify_oid
)
4799 uint32_t r
= ceph_str_hash_linux(key
.c_str(), key
.size());
4801 int i
= r
% num_watchers
;
4803 snprintf(buf
, sizeof(buf
), ".%d", i
);
4805 notify_oid
= notify_oid_prefix
;
4806 notify_oid
.append(buf
);
4809 int RGWRados::open_pool_ctx(const rgw_pool
& pool
, librados::IoCtx
& io_ctx
)
4811 librados::Rados
*rad
= get_rados_handle();
4812 int r
= rgw_init_ioctx(rad
, pool
, io_ctx
);
4816 if (!pools_initialized
)
4819 r
= rad
->pool_create(pool
.name
.c_str());
4820 if (r
< 0 && r
!= -EEXIST
)
4823 r
= rgw_init_ioctx(rad
, pool
, io_ctx
);
4827 r
= io_ctx
.application_enable(pg_pool_t::APPLICATION_NAME_RGW
, false);
4828 if (r
< 0 && r
!= -EOPNOTSUPP
)
4833 void RGWRados::build_bucket_index_marker(const string
& shard_id_str
, const string
& shard_marker
,
4836 *marker
= shard_id_str
;
4837 marker
->append(BucketIndexShardsManager::KEY_VALUE_SEPARATOR
);
4838 marker
->append(shard_marker
);
4842 int RGWRados::open_bucket_index_ctx(const RGWBucketInfo
& bucket_info
, librados::IoCtx
& index_ctx
)
4844 const string
*rule
= &bucket_info
.placement_rule
;
4845 if (rule
->empty()) {
4846 rule
= &zonegroup
.default_placement
;
4848 auto iter
= zone_params
.placement_pools
.find(*rule
);
4849 if (iter
== zone_params
.placement_pools
.end()) {
4850 ldout(cct
, 0) << "could not find placement rule " << *rule
<< " within zonegroup " << dendl
;
4854 int r
= open_pool_ctx(iter
->second
.index_pool
, index_ctx
);
4862 * set up a bucket listing.
4863 * handle is filled in.
4864 * Returns 0 on success, -ERR# otherwise.
4866 int RGWRados::list_buckets_init(RGWAccessHandle
*handle
)
4868 librados::NObjectIterator
*state
= new librados::NObjectIterator(root_pool_ctx
.nobjects_begin());
4869 *handle
= (RGWAccessHandle
)state
;
4874 * get the next bucket in the listing.
4876 * handle is updated.
4877 * returns 0 on success, -ERR# otherwise.
4879 int RGWRados::list_buckets_next(rgw_bucket_dir_entry
& obj
, RGWAccessHandle
*handle
)
4881 librados::NObjectIterator
*state
= (librados::NObjectIterator
*)*handle
;
4884 if (*state
== root_pool_ctx
.nobjects_end()) {
4889 obj
.key
.name
= (*state
)->get_oid();
4890 if (obj
.key
.name
[0] == '_') {
4891 obj
.key
.name
= obj
.key
.name
.substr(1);
4895 } while (obj
.key
.name
[0] == '.'); /* skip all entries starting with '.' */
4903 struct log_list_state
{
4905 librados::IoCtx io_ctx
;
4906 librados::NObjectIterator obit
;
4909 int RGWRados::log_list_init(const string
& prefix
, RGWAccessHandle
*handle
)
4911 log_list_state
*state
= new log_list_state
;
4912 int r
= rgw_init_ioctx(get_rados_handle(), get_zone_params().log_pool
, state
->io_ctx
);
4917 state
->prefix
= prefix
;
4918 state
->obit
= state
->io_ctx
.nobjects_begin();
4919 *handle
= (RGWAccessHandle
)state
;
4923 int RGWRados::log_list_next(RGWAccessHandle handle
, string
*name
)
4925 log_list_state
*state
= static_cast<log_list_state
*>(handle
);
4927 if (state
->obit
== state
->io_ctx
.nobjects_end()) {
4931 if (state
->prefix
.length() &&
4932 state
->obit
->get_oid().find(state
->prefix
) != 0) {
4936 *name
= state
->obit
->get_oid();
4943 int RGWRados::log_remove(const string
& name
)
4945 librados::IoCtx io_ctx
;
4946 int r
= rgw_init_ioctx(get_rados_handle(), get_zone_params().log_pool
, io_ctx
);
4949 return io_ctx
.remove(name
);
4952 struct log_show_state
{
4953 librados::IoCtx io_ctx
;
4955 bufferlist::iterator p
;
4959 log_show_state() : pos(0), eof(false) {}
4962 int RGWRados::log_show_init(const string
& name
, RGWAccessHandle
*handle
)
4964 log_show_state
*state
= new log_show_state
;
4965 int r
= rgw_init_ioctx(get_rados_handle(), get_zone_params().log_pool
, state
->io_ctx
);
4971 *handle
= (RGWAccessHandle
)state
;
4975 int RGWRados::log_show_next(RGWAccessHandle handle
, rgw_log_entry
*entry
)
4977 log_show_state
*state
= static_cast<log_show_state
*>(handle
);
4978 off_t off
= state
->p
.get_off();
4980 ldout(cct
, 10) << "log_show_next pos " << state
->pos
<< " bl " << state
->bl
.length()
4982 << " eof " << (int)state
->eof
4985 unsigned chunk
= 1024*1024;
4986 if ((state
->bl
.length() - off
) < chunk
/2 && !state
->eof
) {
4988 int r
= state
->io_ctx
.read(state
->name
, more
, chunk
, state
->pos
);
4994 old
.substr_of(state
->bl
, off
, state
->bl
.length() - off
);
4995 } catch (buffer::error
& err
) {
4999 state
->bl
.claim(old
);
5000 state
->bl
.claim_append(more
);
5001 state
->p
= state
->bl
.begin();
5002 if ((unsigned)r
< chunk
)
5004 ldout(cct
, 10) << " read " << r
<< dendl
;
5008 return 0; // end of file
5010 ::decode(*entry
, state
->p
);
5012 catch (const buffer::error
&e
) {
5019 * usage_log_hash: get usage log key hash, based on name and index
5021 * Get the usage object name. Since a user may have more than 1
5022 * object holding that info (multiple shards), we use index to
5023 * specify that shard number. Once index exceeds max shards it
5025 * If name is not being set, results for all users will be returned
5026 * and index will wrap only after total shards number.
5028 * @param cct [in] ceph context
5029 * @param name [in] user name
5030 * @param hash [out] hash value
5031 * @param index [in] shard index number
5033 static void usage_log_hash(CephContext
*cct
, const string
& name
, string
& hash
, uint32_t index
)
5035 uint32_t val
= index
;
5037 if (!name
.empty()) {
5038 int max_user_shards
= cct
->_conf
->rgw_usage_max_user_shards
;
5039 val
%= max_user_shards
;
5040 val
+= ceph_str_hash_linux(name
.c_str(), name
.size());
5043 int max_shards
= cct
->_conf
->rgw_usage_max_shards
;
5044 snprintf(buf
, sizeof(buf
), RGW_USAGE_OBJ_PREFIX
"%u", (unsigned)(val
% max_shards
));
5048 int RGWRados::log_usage(map
<rgw_user_bucket
, RGWUsageBatch
>& usage_info
)
5052 map
<string
, rgw_usage_log_info
> log_objs
;
5057 /* restructure usage map, zone by object hash */
5058 map
<rgw_user_bucket
, RGWUsageBatch
>::iterator iter
;
5059 for (iter
= usage_info
.begin(); iter
!= usage_info
.end(); ++iter
) {
5060 const rgw_user_bucket
& ub
= iter
->first
;
5061 RGWUsageBatch
& info
= iter
->second
;
5063 if (ub
.user
.empty()) {
5064 ldout(cct
, 0) << "WARNING: RGWRados::log_usage(): user name empty (bucket=" << ub
.bucket
<< "), skipping" << dendl
;
5068 if (ub
.user
!= last_user
) {
5069 /* index *should* be random, but why waste extra cycles
5070 in most cases max user shards is not going to exceed 1,
5071 so just incrementing it */
5072 usage_log_hash(cct
, ub
.user
, hash
, index
++);
5074 last_user
= ub
.user
;
5075 vector
<rgw_usage_log_entry
>& v
= log_objs
[hash
].entries
;
5077 for (auto miter
= info
.m
.begin(); miter
!= info
.m
.end(); ++miter
) {
5078 v
.push_back(miter
->second
);
5082 map
<string
, rgw_usage_log_info
>::iterator liter
;
5084 for (liter
= log_objs
.begin(); liter
!= log_objs
.end(); ++liter
) {
5085 int r
= cls_obj_usage_log_add(liter
->first
, liter
->second
);
5092 int RGWRados::read_usage(const rgw_user
& user
, uint64_t start_epoch
, uint64_t end_epoch
, uint32_t max_entries
,
5093 bool *is_truncated
, RGWUsageIter
& usage_iter
, map
<rgw_user_bucket
, rgw_usage_log_entry
>& usage
)
5095 uint32_t num
= max_entries
;
5096 string hash
, first_hash
;
5097 string user_str
= user
.to_str();
5098 usage_log_hash(cct
, user_str
, first_hash
, 0);
5100 if (usage_iter
.index
) {
5101 usage_log_hash(cct
, user_str
, hash
, usage_iter
.index
);
5109 map
<rgw_user_bucket
, rgw_usage_log_entry
> ret_usage
;
5110 map
<rgw_user_bucket
, rgw_usage_log_entry
>::iterator iter
;
5112 int ret
= cls_obj_usage_log_read(hash
, user_str
, start_epoch
, end_epoch
, num
,
5113 usage_iter
.read_iter
, ret_usage
, is_truncated
);
5120 num
-= ret_usage
.size();
5122 for (iter
= ret_usage
.begin(); iter
!= ret_usage
.end(); ++iter
) {
5123 usage
[iter
->first
].aggregate(iter
->second
);
5127 if (!*is_truncated
) {
5128 usage_iter
.read_iter
.clear();
5129 usage_log_hash(cct
, user_str
, hash
, ++usage_iter
.index
);
5131 } while (num
&& !*is_truncated
&& hash
!= first_hash
);
5135 int RGWRados::trim_usage(rgw_user
& user
, uint64_t start_epoch
, uint64_t end_epoch
)
5138 string hash
, first_hash
;
5139 string user_str
= user
.to_str();
5140 usage_log_hash(cct
, user_str
, first_hash
, index
);
5145 int ret
= cls_obj_usage_log_trim(hash
, user_str
, start_epoch
, end_epoch
);
5153 usage_log_hash(cct
, user_str
, hash
, ++index
);
5154 } while (hash
!= first_hash
);
5159 int RGWRados::key_to_shard_id(const string
& key
, int max_shards
)
5161 return rgw_shards_hash(key
, max_shards
);
5164 void RGWRados::shard_name(const string
& prefix
, unsigned max_shards
, const string
& key
, string
& name
, int *shard_id
)
5166 uint32_t val
= ceph_str_hash_linux(key
.c_str(), key
.size());
5169 *shard_id
= val
% max_shards
;
5171 snprintf(buf
, sizeof(buf
), "%u", (unsigned)(val
% max_shards
));
5172 name
= prefix
+ buf
;
5175 void RGWRados::shard_name(const string
& prefix
, unsigned max_shards
, const string
& section
, const string
& key
, string
& name
)
5177 uint32_t val
= ceph_str_hash_linux(key
.c_str(), key
.size());
5178 val
^= ceph_str_hash_linux(section
.c_str(), section
.size());
5180 snprintf(buf
, sizeof(buf
), "%u", (unsigned)(val
% max_shards
));
5181 name
= prefix
+ buf
;
5184 void RGWRados::shard_name(const string
& prefix
, unsigned shard_id
, string
& name
)
5187 snprintf(buf
, sizeof(buf
), "%u", shard_id
);
5188 name
= prefix
+ buf
;
5192 void RGWRados::time_log_prepare_entry(cls_log_entry
& entry
, const real_time
& ut
, const string
& section
, const string
& key
, bufferlist
& bl
)
5194 cls_log_add_prepare_entry(entry
, utime_t(ut
), section
, key
, bl
);
5197 int RGWRados::time_log_add_init(librados::IoCtx
& io_ctx
)
5199 return rgw_init_ioctx(get_rados_handle(), get_zone_params().log_pool
, io_ctx
, true);
5203 int RGWRados::time_log_add(const string
& oid
, const real_time
& ut
, const string
& section
, const string
& key
, bufferlist
& bl
)
5205 librados::IoCtx io_ctx
;
5207 int r
= time_log_add_init(io_ctx
);
5212 ObjectWriteOperation op
;
5214 cls_log_add(op
, t
, section
, key
, bl
);
5216 return io_ctx
.operate(oid
, &op
);
5219 int RGWRados::time_log_add(const string
& oid
, list
<cls_log_entry
>& entries
,
5220 librados::AioCompletion
*completion
, bool monotonic_inc
)
5222 librados::IoCtx io_ctx
;
5224 int r
= time_log_add_init(io_ctx
);
5229 ObjectWriteOperation op
;
5230 cls_log_add(op
, entries
, monotonic_inc
);
5233 r
= io_ctx
.operate(oid
, &op
);
5235 r
= io_ctx
.aio_operate(oid
, completion
, &op
);
5240 int RGWRados::time_log_list(const string
& oid
, const real_time
& start_time
, const real_time
& end_time
,
5241 int max_entries
, list
<cls_log_entry
>& entries
,
5242 const string
& marker
,
5246 librados::IoCtx io_ctx
;
5248 int r
= rgw_init_ioctx(get_rados_handle(), get_zone_params().log_pool
, io_ctx
);
5251 librados::ObjectReadOperation op
;
5253 utime_t
st(start_time
);
5254 utime_t
et(end_time
);
5256 cls_log_list(op
, st
, et
, marker
, max_entries
, entries
,
5257 out_marker
, truncated
);
5261 int ret
= io_ctx
.operate(oid
, &op
, &obl
);
5268 int RGWRados::time_log_info(const string
& oid
, cls_log_header
*header
)
5270 librados::IoCtx io_ctx
;
5272 int r
= rgw_init_ioctx(get_rados_handle(), get_zone_params().log_pool
, io_ctx
);
5275 librados::ObjectReadOperation op
;
5277 cls_log_info(op
, header
);
5281 int ret
= io_ctx
.operate(oid
, &op
, &obl
);
5288 int RGWRados::time_log_info_async(librados::IoCtx
& io_ctx
, const string
& oid
, cls_log_header
*header
, librados::AioCompletion
*completion
)
5290 int r
= rgw_init_ioctx(get_rados_handle(), get_zone_params().log_pool
, io_ctx
);
5294 librados::ObjectReadOperation op
;
5296 cls_log_info(op
, header
);
5298 int ret
= io_ctx
.aio_operate(oid
, completion
, &op
, NULL
);
5305 int RGWRados::time_log_trim(const string
& oid
, const real_time
& start_time
, const real_time
& end_time
,
5306 const string
& from_marker
, const string
& to_marker
,
5307 librados::AioCompletion
*completion
)
5309 librados::IoCtx io_ctx
;
5311 int r
= rgw_init_ioctx(get_rados_handle(), get_zone_params().log_pool
, io_ctx
);
5315 utime_t
st(start_time
);
5316 utime_t
et(end_time
);
5318 ObjectWriteOperation op
;
5319 cls_log_trim(op
, st
, et
, from_marker
, to_marker
);
5322 r
= io_ctx
.operate(oid
, &op
);
5324 r
= io_ctx
.aio_operate(oid
, completion
, &op
);
5329 string
RGWRados::objexp_hint_get_shardname(int shard_num
)
5332 snprintf(buf
, sizeof(buf
), "%010u", (unsigned)shard_num
);
5334 string
objname("obj_delete_at_hint.");
5335 return objname
+ buf
;
5338 int RGWRados::objexp_key_shard(const rgw_obj_index_key
& key
)
5340 string obj_key
= key
.name
+ key
.instance
;
5341 int num_shards
= cct
->_conf
->rgw_objexp_hints_num_shards
;
5342 uint32_t sid
= ceph_str_hash_linux(obj_key
.c_str(), obj_key
.size());
5343 uint32_t sid2
= sid
^ ((sid
& 0xFF) << 24);
5344 sid
= rgw_shards_mod(sid2
, num_shards
);
5348 static string
objexp_hint_get_keyext(const string
& tenant_name
,
5349 const string
& bucket_name
,
5350 const string
& bucket_id
,
5351 const rgw_obj_key
& obj_key
)
5353 return tenant_name
+ (tenant_name
.empty() ? "" : ":") + bucket_name
+ ":" + bucket_id
+
5354 ":" + obj_key
.name
+ ":" + obj_key
.instance
;
5357 int RGWRados::objexp_hint_add(const ceph::real_time
& delete_at
,
5358 const string
& tenant_name
,
5359 const string
& bucket_name
,
5360 const string
& bucket_id
,
5361 const rgw_obj_index_key
& obj_key
)
5363 const string keyext
= objexp_hint_get_keyext(tenant_name
, bucket_name
,
5364 bucket_id
, obj_key
);
5365 objexp_hint_entry he
= {
5366 .tenant
= tenant_name
,
5367 .bucket_name
= bucket_name
,
5368 .bucket_id
= bucket_id
,
5370 .exp_time
= delete_at
};
5373 ObjectWriteOperation op
;
5374 cls_timeindex_add(op
, utime_t(delete_at
), keyext
, hebl
);
5376 string shard_name
= objexp_hint_get_shardname(objexp_key_shard(obj_key
));
5377 return objexp_pool_ctx
.operate(shard_name
, &op
);
5380 void RGWRados::objexp_get_shard(int shard_num
,
5381 string
& shard
) /* out */
5383 shard
= objexp_hint_get_shardname(shard_num
);
5386 int RGWRados::objexp_hint_list(const string
& oid
,
5387 const ceph::real_time
& start_time
,
5388 const ceph::real_time
& end_time
,
5389 const int max_entries
,
5390 const string
& marker
,
5391 list
<cls_timeindex_entry
>& entries
, /* out */
5392 string
*out_marker
, /* out */
5393 bool *truncated
) /* out */
5395 librados::ObjectReadOperation op
;
5396 cls_timeindex_list(op
, utime_t(start_time
), utime_t(end_time
), marker
, max_entries
, entries
,
5397 out_marker
, truncated
);
5400 int ret
= objexp_pool_ctx
.operate(oid
, &op
, &obl
);
5402 if ((ret
< 0 ) && (ret
!= -ENOENT
)) {
5406 if ((ret
== -ENOENT
) && truncated
) {
5413 int RGWRados::objexp_hint_parse(cls_timeindex_entry
&ti_entry
, /* in */
5414 objexp_hint_entry
& hint_entry
) /* out */
5417 bufferlist::iterator iter
= ti_entry
.value
.begin();
5418 ::decode(hint_entry
, iter
);
5419 } catch (buffer::error
& err
) {
5420 ldout(cct
, 0) << "ERROR: couldn't decode avail_pools" << dendl
;
5426 int RGWRados::objexp_hint_trim(const string
& oid
,
5427 const ceph::real_time
& start_time
,
5428 const ceph::real_time
& end_time
,
5429 const string
& from_marker
,
5430 const string
& to_marker
)
5432 int ret
= cls_timeindex_trim(objexp_pool_ctx
, oid
, utime_t(start_time
), utime_t(end_time
),
5433 from_marker
, to_marker
);
5434 if ((ret
< 0 ) && (ret
!= -ENOENT
)) {
5441 int RGWRados::lock_exclusive(rgw_pool
& pool
, const string
& oid
, timespan
& duration
,
5442 string
& zone_id
, string
& owner_id
) {
5443 librados::IoCtx io_ctx
;
5445 int r
= rgw_init_ioctx(get_rados_handle(), pool
, io_ctx
);
5449 uint64_t msec
= std::chrono::duration_cast
<std::chrono::milliseconds
>(duration
).count();
5450 utime_t
ut(msec
/ 1000, msec
% 1000);
5452 rados::cls::lock::Lock
l(log_lock_name
);
5454 l
.set_cookie(owner_id
);
5458 return l
.lock_exclusive(&io_ctx
, oid
);
5461 int RGWRados::unlock(rgw_pool
& pool
, const string
& oid
, string
& zone_id
, string
& owner_id
) {
5462 librados::IoCtx io_ctx
;
5464 int r
= rgw_init_ioctx(get_rados_handle(), pool
, io_ctx
);
5469 rados::cls::lock::Lock
l(log_lock_name
);
5471 l
.set_cookie(owner_id
);
5473 return l
.unlock(&io_ctx
, oid
);
5476 int RGWRados::decode_policy(bufferlist
& bl
, ACLOwner
*owner
)
5478 bufferlist::iterator i
= bl
.begin();
5479 RGWAccessControlPolicy
policy(cct
);
5481 policy
.decode_owner(i
);
5482 } catch (buffer::error
& err
) {
5483 ldout(cct
, 0) << "ERROR: could not decode policy, caught buffer::error" << dendl
;
5486 *owner
= policy
.get_owner();
5490 int rgw_policy_from_attrset(CephContext
*cct
, map
<string
, bufferlist
>& attrset
, RGWAccessControlPolicy
*policy
)
5492 map
<string
, bufferlist
>::iterator aiter
= attrset
.find(RGW_ATTR_ACL
);
5493 if (aiter
== attrset
.end())
5496 bufferlist
& bl
= aiter
->second
;
5497 bufferlist::iterator iter
= bl
.begin();
5499 policy
->decode(iter
);
5500 } catch (buffer::error
& err
) {
5501 ldout(cct
, 0) << "ERROR: could not decode policy, caught buffer::error" << dendl
;
5504 if (cct
->_conf
->subsys
.should_gather(ceph_subsys_rgw
, 15)) {
5505 RGWAccessControlPolicy_S3
*s3policy
= static_cast<RGWAccessControlPolicy_S3
*>(policy
);
5506 ldout(cct
, 15) << __func__
<< " Read AccessControlPolicy";
5507 s3policy
->to_xml(*_dout
);
5514 int RGWRados::Bucket::update_bucket_id(const string
& new_bucket_id
)
5516 rgw_bucket bucket
= bucket_info
.bucket
;
5517 bucket
.update_bucket_id(new_bucket_id
);
5519 RGWObjectCtx
obj_ctx(store
);
5521 int ret
= store
->get_bucket_instance_info(obj_ctx
, bucket
, bucket_info
, nullptr, nullptr);
5530 * get listing of the objects in a bucket.
5532 * max: maximum number of results to return
5533 * bucket: bucket to list contents of
5534 * prefix: only return results that match this prefix
5535 * delim: do not include results that match this string.
5536 * Any skipped results will have the matching portion of their name
5537 * inserted in common_prefixes with a "true" mark.
5538 * marker: if filled in, begin the listing with this object.
5539 * end_marker: if filled in, end the listing with this object.
5540 * result: the objects are put in here.
5541 * common_prefixes: if delim is filled in, any matching prefixes are placed here.
5542 * is_truncated: if number of objects in the bucket is bigger than max, then truncated.
5544 int RGWRados::Bucket::List::list_objects(int64_t max
,
5545 vector
<rgw_bucket_dir_entry
> *result
,
5546 map
<string
, bool> *common_prefixes
,
5549 RGWRados
*store
= target
->get_store();
5550 CephContext
*cct
= store
->ctx();
5551 int shard_id
= target
->get_shard_id();
5554 bool truncated
= true;
5555 int read_ahead
= std::max(cct
->_conf
->rgw_list_bucket_min_readahead
,max
);
5559 rgw_obj_key
marker_obj(params
.marker
.name
, params
.marker
.instance
, params
.ns
);
5561 rgw_obj_key end_marker_obj
;
5562 rgw_obj_index_key cur_end_marker
;
5563 if (!params
.ns
.empty()) {
5564 end_marker_obj
= rgw_obj_key(params
.end_marker
.name
, params
.end_marker
.instance
, params
.ns
);
5565 end_marker_obj
.ns
= params
.ns
;
5566 end_marker_obj
.get_index_key(&cur_end_marker
);
5568 rgw_obj_index_key cur_marker
;
5569 marker_obj
.get_index_key(&cur_marker
);
5571 const bool cur_end_marker_valid
= !params
.end_marker
.empty();
5573 rgw_obj_key
prefix_obj(params
.prefix
);
5574 prefix_obj
.ns
= params
.ns
;
5575 string cur_prefix
= prefix_obj
.get_index_key_name();
5577 string bigger_than_delim
;
5579 if (!params
.delim
.empty()) {
5580 unsigned long val
= decode_utf8((unsigned char *)params
.delim
.c_str(), params
.delim
.size());
5581 char buf
[params
.delim
.size() + 16];
5582 int r
= encode_utf8(val
+ 1, (unsigned char *)buf
);
5584 ldout(cct
,0) << "ERROR: encode_utf8() failed" << dendl
;
5589 bigger_than_delim
= buf
;
5591 /* if marker points at a common prefix, fast forward it into its upperbound string */
5592 int delim_pos
= cur_marker
.name
.find(params
.delim
, cur_prefix
.size());
5593 if (delim_pos
>= 0) {
5594 string s
= cur_marker
.name
.substr(0, delim_pos
);
5595 s
.append(bigger_than_delim
);
5600 string skip_after_delim
;
5601 while (truncated
&& count
<= max
) {
5602 if (skip_after_delim
> cur_marker
.name
) {
5603 cur_marker
= skip_after_delim
;
5604 ldout(cct
, 20) << "setting cur_marker=" << cur_marker
.name
<< "[" << cur_marker
.instance
<< "]" << dendl
;
5606 std::map
<string
, rgw_bucket_dir_entry
> ent_map
;
5607 int r
= store
->cls_bucket_list(target
->get_bucket_info(), shard_id
, cur_marker
, cur_prefix
,
5608 read_ahead
+ 1 - count
, params
.list_versions
, ent_map
,
5609 &truncated
, &cur_marker
);
5613 std::map
<string
, rgw_bucket_dir_entry
>::iterator eiter
;
5614 for (eiter
= ent_map
.begin(); eiter
!= ent_map
.end(); ++eiter
) {
5615 rgw_bucket_dir_entry
& entry
= eiter
->second
;
5616 rgw_obj_index_key index_key
= entry
.key
;
5618 rgw_obj_key
obj(index_key
);
5620 /* note that parse_raw_oid() here will not set the correct object's instance, as
5621 * rgw_obj_index_key encodes that separately. We don't need to set the instance because it's
5622 * not needed for the checks here and we end up using the raw entry for the return vector
5624 bool valid
= rgw_obj_key::parse_raw_oid(index_key
.name
, &obj
);
5626 ldout(cct
, 0) << "ERROR: could not parse object name: " << obj
.name
<< dendl
;
5629 bool check_ns
= (obj
.ns
== params
.ns
);
5630 if (!params
.list_versions
&& !entry
.is_visible()) {
5634 if (params
.enforce_ns
&& !check_ns
) {
5635 if (!params
.ns
.empty()) {
5636 /* we've iterated past the namespace we're searching -- done now */
5641 /* we're not looking at the namespace this object is in, next! */
5645 if (cur_end_marker_valid
&& cur_end_marker
<= index_key
) {
5651 params
.marker
= index_key
;
5652 next_marker
= index_key
;
5655 if (params
.filter
&& !params
.filter
->filter(obj
.name
, index_key
.name
))
5658 if (params
.prefix
.size() && (obj
.name
.compare(0, params
.prefix
.size(), params
.prefix
) != 0))
5661 if (!params
.delim
.empty()) {
5662 int delim_pos
= obj
.name
.find(params
.delim
, params
.prefix
.size());
5664 if (delim_pos
>= 0) {
5665 string prefix_key
= obj
.name
.substr(0, delim_pos
+ 1);
5667 if (common_prefixes
&&
5668 common_prefixes
->find(prefix_key
) == common_prefixes
->end()) {
5673 next_marker
= prefix_key
;
5674 (*common_prefixes
)[prefix_key
] = true;
5676 int marker_delim_pos
= cur_marker
.name
.find(params
.delim
, cur_prefix
.size());
5678 skip_after_delim
= cur_marker
.name
.substr(0, marker_delim_pos
);
5679 skip_after_delim
.append(bigger_than_delim
);
5681 ldout(cct
, 20) << "skip_after_delim=" << skip_after_delim
<< dendl
;
5695 result
->emplace_back(std::move(entry
));
5699 // Either the back-end telling us truncated, or we don't consume all
5700 // items returned per the amount caller request
5701 truncated
= (truncated
|| eiter
!= ent_map
.end());
5706 *is_truncated
= truncated
;
5712 * create a rados pool, associated meta info
5713 * returns 0 on success, -ERR# otherwise.
5715 int RGWRados::create_pool(const rgw_pool
& pool
)
5719 librados::Rados
*rad
= get_rados_handle();
5720 ret
= rad
->pool_create(pool
.name
.c_str(), 0);
5723 else if (ret
== -ERANGE
) {
5726 << " ERROR: librados::Rados::pool_create returned " << cpp_strerror(-ret
)
5727 << " (this can be due to a pool or placement group misconfiguration, e.g., pg_num < pgp_num)"
5733 librados::IoCtx io_ctx
;
5734 ret
= rad
->ioctx_create(pool
.name
.c_str(), io_ctx
);
5738 ret
= io_ctx
.application_enable(pg_pool_t::APPLICATION_NAME_RGW
, false);
5739 if (ret
< 0 && ret
!= -EOPNOTSUPP
)
5744 int RGWRados::init_bucket_index(RGWBucketInfo
& bucket_info
, int num_shards
)
5746 librados::IoCtx index_ctx
; // context for new bucket
5748 string dir_oid
= dir_oid_prefix
;
5749 int r
= open_bucket_index_ctx(bucket_info
, index_ctx
);
5754 dir_oid
.append(bucket_info
.bucket
.bucket_id
);
5756 map
<int, string
> bucket_objs
;
5757 get_bucket_index_objects(dir_oid
, num_shards
, bucket_objs
);
5759 return CLSRGWIssueBucketIndexInit(index_ctx
, bucket_objs
, cct
->_conf
->rgw_bucket_index_max_aio
)();
5762 void RGWRados::create_bucket_id(string
*bucket_id
)
5764 uint64_t iid
= instance_id();
5765 uint64_t bid
= next_bucket_id();
5766 char buf
[get_zone_params().get_id().size() + 48];
5767 snprintf(buf
, sizeof(buf
), "%s.%llu.%llu", get_zone_params().get_id().c_str(), (long long)iid
, (long long)bid
);
5771 int RGWRados::create_bucket(RGWUserInfo
& owner
, rgw_bucket
& bucket
,
5772 const string
& zonegroup_id
,
5773 const string
& placement_rule
,
5774 const string
& swift_ver_location
,
5775 const RGWQuotaInfo
* pquota_info
,
5776 map
<std::string
, bufferlist
>& attrs
,
5777 RGWBucketInfo
& info
,
5779 obj_version
*pep_objv
,
5780 real_time creation_time
,
5781 rgw_bucket
*pmaster_bucket
,
5782 uint32_t *pmaster_num_shards
,
5785 #define MAX_CREATE_RETRIES 20 /* need to bound retries */
5786 string selected_placement_rule_name
;
5787 RGWZonePlacementInfo rule_info
;
5789 for (int i
= 0; i
< MAX_CREATE_RETRIES
; i
++) {
5791 ret
= select_bucket_placement(owner
, zonegroup_id
, placement_rule
,
5792 &selected_placement_rule_name
, &rule_info
);
5796 if (!pmaster_bucket
) {
5797 create_bucket_id(&bucket
.marker
);
5798 bucket
.bucket_id
= bucket
.marker
;
5800 bucket
.marker
= pmaster_bucket
->marker
;
5801 bucket
.bucket_id
= pmaster_bucket
->bucket_id
;
5804 RGWObjVersionTracker
& objv_tracker
= info
.objv_tracker
;
5807 objv_tracker
.write_version
= *pobjv
;
5809 objv_tracker
.generate_new_write_ver(cct
);
5812 info
.bucket
= bucket
;
5813 info
.owner
= owner
.user_id
;
5814 info
.zonegroup
= zonegroup_id
;
5815 info
.placement_rule
= selected_placement_rule_name
;
5816 info
.index_type
= rule_info
.index_type
;
5817 info
.swift_ver_location
= swift_ver_location
;
5818 info
.swift_versioning
= (!swift_ver_location
.empty());
5819 if (pmaster_num_shards
) {
5820 info
.num_shards
= *pmaster_num_shards
;
5822 info
.num_shards
= bucket_index_max_shards
;
5824 info
.bucket_index_shard_hash_type
= RGWBucketInfo::MOD
;
5825 info
.requester_pays
= false;
5826 if (real_clock::is_zero(creation_time
)) {
5827 info
.creation_time
= ceph::real_clock::now();
5829 info
.creation_time
= creation_time
;
5832 info
.quota
= *pquota_info
;
5835 int r
= init_bucket_index(info
, info
.num_shards
);
5840 ret
= put_linked_bucket_info(info
, exclusive
, ceph::real_time(), pep_objv
, &attrs
, true);
5841 if (ret
== -EEXIST
) {
5842 librados::IoCtx index_ctx
;
5843 map
<int, string
> bucket_objs
;
5844 int r
= open_bucket_index(info
, index_ctx
, bucket_objs
);
5848 /* we need to reread the info and return it, caller will have a use for it */
5849 RGWObjVersionTracker instance_ver
= info
.objv_tracker
;
5850 info
.objv_tracker
.clear();
5851 RGWObjectCtx
obj_ctx(this);
5852 r
= get_bucket_info(obj_ctx
, bucket
.tenant
, bucket
.name
, info
, NULL
, NULL
);
5857 ldout(cct
, 0) << "get_bucket_info returned " << r
<< dendl
;
5861 /* only remove it if it's a different bucket instance */
5862 if (info
.bucket
.bucket_id
!= bucket
.bucket_id
) {
5863 /* remove bucket meta instance */
5864 string entry
= bucket
.get_key();
5865 r
= rgw_bucket_instance_remove_entry(this, entry
, &instance_ver
);
5869 map
<int, string
>::const_iterator biter
;
5870 for (biter
= bucket_objs
.begin(); biter
!= bucket_objs
.end(); ++biter
) {
5871 // Do best effort removal
5872 index_ctx
.remove(biter
->second
);
5875 /* ret == -ENOENT here */
5880 /* this is highly unlikely */
5881 ldout(cct
, 0) << "ERROR: could not create bucket, continuously raced with bucket creation and removal" << dendl
;
5885 int RGWRados::select_new_bucket_location(RGWUserInfo
& user_info
, const string
& zonegroup_id
, const string
& request_rule
,
5886 string
*pselected_rule_name
, RGWZonePlacementInfo
*rule_info
)
5889 /* first check that zonegroup exists within current period. */
5890 RGWZoneGroup zonegroup
;
5891 int ret
= get_zonegroup(zonegroup_id
, zonegroup
);
5893 ldout(cct
, 0) << "could not find zonegroup " << zonegroup_id
<< " in current period" << dendl
;
5897 /* find placement rule. Hierarchy: request rule > user default rule > zonegroup default rule */
5898 std::map
<std::string
, RGWZoneGroupPlacementTarget
>::const_iterator titer
;
5900 if (!request_rule
.empty()) {
5901 titer
= zonegroup
.placement_targets
.find(request_rule
);
5902 if (titer
== zonegroup
.placement_targets
.end()) {
5903 ldout(cct
, 0) << "could not find requested placement id " << request_rule
5904 << " within zonegroup " << dendl
;
5905 return -ERR_INVALID_LOCATION_CONSTRAINT
;
5907 } else if (!user_info
.default_placement
.empty()) {
5908 titer
= zonegroup
.placement_targets
.find(user_info
.default_placement
);
5909 if (titer
== zonegroup
.placement_targets
.end()) {
5910 ldout(cct
, 0) << "could not find user default placement id " << user_info
.default_placement
5911 << " within zonegroup " << dendl
;
5912 return -ERR_INVALID_LOCATION_CONSTRAINT
;
5915 if (zonegroup
.default_placement
.empty()) { // zonegroup default rule as fallback, it should not be empty.
5916 ldout(cct
, 0) << "misconfiguration, zonegroup default placement id should not be empty." << dendl
;
5917 return -ERR_ZONEGROUP_DEFAULT_PLACEMENT_MISCONFIGURATION
;
5919 titer
= zonegroup
.placement_targets
.find(zonegroup
.default_placement
);
5920 if (titer
== zonegroup
.placement_targets
.end()) {
5921 ldout(cct
, 0) << "could not find zonegroup default placement id " << zonegroup
.default_placement
5922 << " within zonegroup " << dendl
;
5923 return -ERR_INVALID_LOCATION_CONSTRAINT
;
5928 /* now check tag for the rule, whether user is permitted to use rule */
5929 const auto& target_rule
= titer
->second
;
5930 if (!target_rule
.user_permitted(user_info
.placement_tags
)) {
5931 ldout(cct
, 0) << "user not permitted to use placement rule " << titer
->first
<< dendl
;
5935 if (pselected_rule_name
)
5936 *pselected_rule_name
= titer
->first
;
5938 return select_bucket_location_by_rule(titer
->first
, rule_info
);
5941 int RGWRados::select_bucket_location_by_rule(const string
& location_rule
, RGWZonePlacementInfo
*rule_info
)
5943 if (location_rule
.empty()) {
5944 /* we can only reach here if we're trying to set a bucket location from a bucket
5945 * created on a different zone, using a legacy / default pool configuration
5947 return select_legacy_bucket_placement(rule_info
);
5951 * make sure that zone has this rule configured. We're
5952 * checking it for the local zone, because that's where this bucket object is going to
5955 map
<string
, RGWZonePlacementInfo
>::iterator piter
= get_zone_params().placement_pools
.find(location_rule
);
5956 if (piter
== get_zone_params().placement_pools
.end()) {
5957 /* couldn't find, means we cannot really place data for this bucket in this zone */
5958 if (get_zonegroup().equals(zonegroup
.get_id())) {
5959 /* that's a configuration error, zone should have that rule, as we're within the requested
5963 /* oh, well, data is not going to be placed here, bucket object is just a placeholder */
5968 RGWZonePlacementInfo
& placement_info
= piter
->second
;
5971 *rule_info
= placement_info
;
5977 int RGWRados::select_bucket_placement(RGWUserInfo
& user_info
, const string
& zonegroup_id
, const string
& placement_rule
,
5978 string
*pselected_rule_name
, RGWZonePlacementInfo
*rule_info
)
5980 if (!get_zone_params().placement_pools
.empty()) {
5981 return select_new_bucket_location(user_info
, zonegroup_id
, placement_rule
,
5982 pselected_rule_name
, rule_info
);
5985 if (pselected_rule_name
) {
5986 pselected_rule_name
->clear();
5989 return select_legacy_bucket_placement(rule_info
);
5992 int RGWRados::select_legacy_bucket_placement(RGWZonePlacementInfo
*rule_info
)
5995 map
<string
, bufferlist
> m
;
5997 bool write_map
= false;
5999 rgw_raw_obj
obj(get_zone_params().domain_root
, avail_pools
);
6001 RGWObjectCtx
obj_ctx(this);
6002 int ret
= rgw_get_system_obj(this, obj_ctx
, get_zone_params().domain_root
, avail_pools
, map_bl
, NULL
, NULL
);
6008 bufferlist::iterator iter
= map_bl
.begin();
6010 } catch (buffer::error
& err
) {
6011 ldout(cct
, 0) << "ERROR: couldn't decode avail_pools" << dendl
;
6017 ret
= omap_get_all(obj
, header
, m
);
6022 if (ret
< 0 || m
.empty()) {
6023 vector
<rgw_pool
> pools
;
6024 string s
= string("default.") + default_storage_pool_suffix
;
6025 pools
.push_back(rgw_pool(s
));
6026 vector
<int> retcodes
;
6028 ret
= create_pools(pools
, retcodes
);
6031 ret
= omap_set(obj
, s
, bl
);
6039 ::encode(m
, new_bl
);
6040 ret
= put_system_obj_data(NULL
, obj
, new_bl
, -1, false);
6042 ldout(cct
, 0) << "WARNING: could not save avail pools map info ret=" << ret
<< dendl
;
6046 map
<string
, bufferlist
>::iterator miter
;
6049 for (miter
= m
.begin(); miter
!= m
.end(); ++miter
) {
6050 v
.push_back(miter
->first
);
6054 ret
= get_random_bytes((char *)&r
, sizeof(r
));
6058 int i
= r
% v
.size();
6062 pool_name
= miter
->first
;
6065 rule_info
->data_pool
= pool_name
;
6066 rule_info
->data_extra_pool
= pool_name
;
6067 rule_info
->index_pool
= pool_name
;
6068 rule_info
->index_type
= RGWBIType_Normal
;
6073 bool RGWRados::get_obj_data_pool(const string
& placement_rule
, const rgw_obj
& obj
, rgw_pool
*pool
)
6075 return rgw_get_obj_data_pool(zonegroup
, zone_params
, placement_rule
, obj
, pool
);
6078 bool RGWRados::obj_to_raw(const string
& placement_rule
, const rgw_obj
& obj
, rgw_raw_obj
*raw_obj
)
6080 get_obj_bucket_and_oid_loc(obj
, raw_obj
->oid
, raw_obj
->loc
);
6082 return get_obj_data_pool(placement_rule
, obj
, &raw_obj
->pool
);
6085 int RGWRados::update_placement_map()
6088 map
<string
, bufferlist
> m
;
6089 rgw_raw_obj
obj(get_zone_params().domain_root
, avail_pools
);
6090 int ret
= omap_get_all(obj
, header
, m
);
6095 ::encode(m
, new_bl
);
6096 ret
= put_system_obj_data(NULL
, obj
, new_bl
, -1, false);
6098 ldout(cct
, 0) << "WARNING: could not save avail pools map info ret=" << ret
<< dendl
;
6104 int RGWRados::add_bucket_placement(const rgw_pool
& new_pool
)
6106 librados::Rados
*rad
= get_rados_handle();
6107 int ret
= rad
->pool_lookup(new_pool
.name
.c_str());
6108 if (ret
< 0) // DNE, or something
6111 rgw_raw_obj
obj(get_zone_params().domain_root
, avail_pools
);
6112 bufferlist empty_bl
;
6113 ret
= omap_set(obj
, new_pool
.to_str(), empty_bl
);
6115 // don't care about return value
6116 update_placement_map();
6121 int RGWRados::remove_bucket_placement(const rgw_pool
& old_pool
)
6123 rgw_raw_obj
obj(get_zone_params().domain_root
, avail_pools
);
6124 int ret
= omap_del(obj
, old_pool
.to_str());
6126 // don't care about return value
6127 update_placement_map();
6132 int RGWRados::list_placement_set(set
<rgw_pool
>& names
)
6135 map
<string
, bufferlist
> m
;
6137 rgw_raw_obj
obj(get_zone_params().domain_root
, avail_pools
);
6138 int ret
= omap_get_all(obj
, header
, m
);
6143 map
<string
, bufferlist
>::iterator miter
;
6144 for (miter
= m
.begin(); miter
!= m
.end(); ++miter
) {
6145 names
.insert(rgw_pool(miter
->first
));
6148 return names
.size();
6151 int RGWRados::create_pools(vector
<rgw_pool
>& pools
, vector
<int>& retcodes
)
6153 vector
<librados::PoolAsyncCompletion
*> completions
;
6156 librados::Rados
*rad
= get_rados_handle();
6157 for (auto iter
= pools
.begin(); iter
!= pools
.end(); ++iter
) {
6158 librados::PoolAsyncCompletion
*c
= librados::Rados::pool_async_create_completion();
6159 completions
.push_back(c
);
6160 rgw_pool
& pool
= *iter
;
6161 int ret
= rad
->pool_create_async(pool
.name
.c_str(), c
);
6162 rets
.push_back(ret
);
6165 vector
<int>::iterator riter
;
6166 vector
<librados::PoolAsyncCompletion
*>::iterator citer
;
6169 assert(rets
.size() == completions
.size());
6170 for (riter
= rets
.begin(), citer
= completions
.begin(); riter
!= rets
.end(); ++riter
, ++citer
) {
6172 PoolAsyncCompletion
*c
= *citer
;
6175 r
= c
->get_return_value();
6177 ldout(cct
, 0) << "WARNING: async pool_create returned " << r
<< dendl
;
6182 retcodes
.push_back(r
);
6188 std::vector
<librados::IoCtx
> io_ctxs
;
6190 for (auto pool
: pools
) {
6191 io_ctxs
.emplace_back();
6192 int ret
= rad
->ioctx_create(pool
.name
.c_str(), io_ctxs
.back());
6194 ldout(cct
, 0) << "WARNING: ioctx_create returned " << ret
<< dendl
;
6197 retcodes
.push_back(ret
);
6203 completions
.clear();
6204 for (auto &io_ctx
: io_ctxs
) {
6205 librados::PoolAsyncCompletion
*c
=
6206 librados::Rados::pool_async_create_completion();
6207 completions
.push_back(c
);
6208 int ret
= io_ctx
.application_enable_async(pg_pool_t::APPLICATION_NAME_RGW
,
6214 for (auto c
: completions
) {
6216 int ret
= c
->get_return_value();
6217 if (ret
== -EOPNOTSUPP
) {
6219 } else if (ret
< 0) {
6220 ldout(cct
, 0) << "WARNING: async application_enable returned " << ret
6225 retcodes
.push_back(ret
);
6230 int RGWRados::get_obj_head_ioctx(const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
, librados::IoCtx
*ioctx
)
6233 get_obj_bucket_and_oid_loc(obj
, oid
, key
);
6236 if (!get_obj_data_pool(bucket_info
.placement_rule
, obj
, &pool
)) {
6237 ldout(cct
, 0) << "ERROR: cannot get data pool for obj=" << obj
<< ", probably misconfiguration" << dendl
;
6241 int r
= open_pool_ctx(pool
, *ioctx
);
6246 ioctx
->locator_set_key(key
);
6251 int RGWRados::get_obj_head_ref(const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
, rgw_rados_ref
*ref
)
6253 get_obj_bucket_and_oid_loc(obj
, ref
->oid
, ref
->key
);
6256 if (!get_obj_data_pool(bucket_info
.placement_rule
, obj
, &pool
)) {
6257 ldout(cct
, 0) << "ERROR: cannot get data pool for obj=" << obj
<< ", probably misconfiguration" << dendl
;
6261 int r
= open_pool_ctx(pool
, ref
->ioctx
);
6266 ref
->ioctx
.locator_set_key(ref
->key
);
6271 int RGWRados::get_raw_obj_ref(const rgw_raw_obj
& obj
, rgw_rados_ref
*ref
)
6278 if (ref
->oid
.empty()) {
6279 ref
->oid
= obj
.pool
.to_str();
6280 ref
->pool
= get_zone_params().domain_root
;
6282 ref
->pool
= obj
.pool
;
6284 r
= open_pool_ctx(ref
->pool
, ref
->ioctx
);
6288 ref
->ioctx
.locator_set_key(ref
->key
);
6293 int RGWRados::get_system_obj_ref(const rgw_raw_obj
& obj
, rgw_rados_ref
*ref
)
6295 return get_raw_obj_ref(obj
, ref
);
6299 * fixes an issue where head objects were supposed to have a locator created, but ended
6302 int RGWRados::fix_head_obj_locator(const RGWBucketInfo
& bucket_info
, bool copy_obj
, bool remove_bad
, rgw_obj_key
& key
)
6304 const rgw_bucket
& bucket
= bucket_info
.bucket
;
6308 rgw_obj
obj(bucket
, key
);
6310 get_obj_bucket_and_oid_loc(obj
, oid
, locator
);
6312 if (locator
.empty()) {
6313 ldout(cct
, 20) << "object does not have a locator, nothing to fix" << dendl
;
6317 librados::IoCtx ioctx
;
6319 int ret
= get_obj_head_ioctx(bucket_info
, obj
, &ioctx
);
6321 cerr
<< "ERROR: get_obj_head_ioctx() returned ret=" << ret
<< std::endl
;
6324 ioctx
.locator_set_key(string()); /* override locator for this object, use empty locator */
6329 struct timespec mtime_ts
;
6330 map
<string
, bufferlist
> attrs
;
6331 librados::ObjectReadOperation op
;
6332 op
.getxattrs(&attrs
, NULL
);
6333 op
.stat2(&size
, &mtime_ts
, NULL
);
6334 #define HEAD_SIZE 512 * 1024
6335 op
.read(0, HEAD_SIZE
, &data
, NULL
);
6337 ret
= ioctx
.operate(oid
, &op
, NULL
);
6339 lderr(cct
) << "ERROR: ioctx.operate(oid=" << oid
<< ") returned ret=" << ret
<< dendl
;
6343 if (size
> HEAD_SIZE
) {
6344 lderr(cct
) << "ERROR: returned object size (" << size
<< ") > HEAD_SIZE (" << HEAD_SIZE
<< ")" << dendl
;
6348 if (size
!= data
.length()) {
6349 lderr(cct
) << "ERROR: returned object size (" << size
<< ") != data.length() (" << data
.length() << ")" << dendl
;
6354 librados::ObjectWriteOperation wop
;
6356 wop
.mtime2(&mtime_ts
);
6358 map
<string
, bufferlist
>::iterator iter
;
6359 for (iter
= attrs
.begin(); iter
!= attrs
.end(); ++iter
) {
6360 wop
.setxattr(iter
->first
.c_str(), iter
->second
);
6365 ioctx
.locator_set_key(locator
);
6366 ioctx
.operate(oid
, &wop
);
6370 ioctx
.locator_set_key(string());
6372 ret
= ioctx
.remove(oid
);
6374 lderr(cct
) << "ERROR: failed to remove original bad object" << dendl
;
6382 int RGWRados::move_rados_obj(librados::IoCtx
& src_ioctx
,
6383 const string
& src_oid
, const string
& src_locator
,
6384 librados::IoCtx
& dst_ioctx
,
6385 const string
& dst_oid
, const string
& dst_locator
)
6388 #define COPY_BUF_SIZE (4 * 1024 * 1024)
6390 uint64_t chunk_size
= COPY_BUF_SIZE
;
6394 struct timespec mtime_ts
;
6397 if (src_oid
== dst_oid
&& src_locator
== dst_locator
) {
6401 src_ioctx
.locator_set_key(src_locator
);
6402 dst_ioctx
.locator_set_key(dst_locator
);
6406 ObjectReadOperation rop
;
6407 ObjectWriteOperation wop
;
6410 rop
.stat2(&size
, &mtime_ts
, NULL
);
6411 mtime
= real_clock::from_timespec(mtime_ts
);
6413 rop
.read(ofs
, chunk_size
, &data
, NULL
);
6414 ret
= src_ioctx
.operate(src_oid
, &rop
, NULL
);
6419 if (data
.length() == 0) {
6424 wop
.create(true); /* make it exclusive */
6425 wop
.mtime2(&mtime_ts
);
6426 mtime
= real_clock::from_timespec(mtime_ts
);
6428 wop
.write(ofs
, data
);
6429 ret
= dst_ioctx
.operate(dst_oid
, &wop
);
6430 ofs
+= data
.length();
6431 done
= data
.length() != chunk_size
;
6435 lderr(cct
) << "ERROR: " << __func__
<< ": copying " << src_oid
<< " -> " << dst_oid
6436 << ": expected " << size
<< " bytes to copy, ended up with " << ofs
<< dendl
;
6441 src_ioctx
.remove(src_oid
);
6446 lderr(cct
) << "ERROR: failed to copy " << src_oid
<< " -> " << dst_oid
<< dendl
;
6451 * fixes an issue where head objects were supposed to have a locator created, but ended
6454 int RGWRados::fix_tail_obj_locator(const RGWBucketInfo
& bucket_info
, rgw_obj_key
& key
, bool fix
, bool *need_fix
)
6456 const rgw_bucket
& bucket
= bucket_info
.bucket
;
6457 rgw_obj
obj(bucket
, key
);
6464 int r
= get_obj_head_ref(bucket_info
, obj
, &ref
);
6469 RGWObjState
*astate
= NULL
;
6470 RGWObjectCtx
rctx(this);
6471 r
= get_obj_state(&rctx
, bucket_info
, obj
, &astate
, false);
6475 if (astate
->has_manifest
) {
6476 RGWObjManifest::obj_iterator miter
;
6477 RGWObjManifest
& manifest
= astate
->manifest
;
6478 for (miter
= manifest
.obj_begin(); miter
!= manifest
.obj_end(); ++miter
) {
6479 rgw_raw_obj raw_loc
= miter
.get_location().get_raw_obj(this);
6484 rgw_raw_obj_to_obj(manifest
.get_tail_placement().bucket
, raw_loc
, &loc
);
6486 if (loc
.key
.ns
.empty()) {
6487 /* continue, we're only interested in tail objects */
6491 get_obj_bucket_and_oid_loc(loc
, oid
, locator
);
6492 ref
.ioctx
.locator_set_key(locator
);
6494 ldout(cct
, 20) << __func__
<< ": key=" << key
<< " oid=" << oid
<< " locator=" << locator
<< dendl
;
6496 r
= ref
.ioctx
.stat(oid
, NULL
, NULL
);
6502 prepend_bucket_marker(bucket
, loc
.key
.name
, bad_loc
);
6504 /* create a new ioctx with the bad locator */
6505 librados::IoCtx src_ioctx
;
6506 src_ioctx
.dup(ref
.ioctx
);
6507 src_ioctx
.locator_set_key(bad_loc
);
6509 r
= src_ioctx
.stat(oid
, NULL
, NULL
);
6511 /* cannot find a broken part */
6514 ldout(cct
, 20) << __func__
<< ": found bad object part: " << loc
<< dendl
;
6519 r
= move_rados_obj(src_ioctx
, oid
, bad_loc
, ref
.ioctx
, oid
, locator
);
6521 lderr(cct
) << "ERROR: copy_rados_obj() on oid=" << oid
<< " returned r=" << r
<< dendl
;
6530 int RGWRados::BucketShard::init(const rgw_bucket
& _bucket
, const rgw_obj
& obj
)
6534 RGWObjectCtx
obj_ctx(store
);
6536 RGWBucketInfo bucket_info
;
6537 int ret
= store
->get_bucket_instance_info(obj_ctx
, bucket
, bucket_info
, NULL
, NULL
);
6542 ret
= store
->open_bucket_index_shard(bucket_info
, index_ctx
, obj
.get_hash_object(), &bucket_obj
, &shard_id
);
6544 ldout(store
->ctx(), 0) << "ERROR: open_bucket_index_shard() returned ret=" << ret
<< dendl
;
6547 ldout(store
->ctx(), 20) << " bucket index object: " << bucket_obj
<< dendl
;
6552 int RGWRados::BucketShard::init(const rgw_bucket
& _bucket
, int sid
)
6557 RGWObjectCtx
obj_ctx(store
);
6559 RGWBucketInfo bucket_info
;
6560 int ret
= store
->get_bucket_instance_info(obj_ctx
, bucket
, bucket_info
, NULL
, NULL
);
6565 ret
= store
->open_bucket_index_shard(bucket_info
, index_ctx
, shard_id
, &bucket_obj
);
6567 ldout(store
->ctx(), 0) << "ERROR: open_bucket_index_shard() returned ret=" << ret
<< dendl
;
6570 ldout(store
->ctx(), 20) << " bucket index object: " << bucket_obj
<< dendl
;
6576 /* Execute @handler on last item in bucket listing for bucket specified
6577 * in @bucket_info. @obj_prefix and @obj_delim narrow down the listing
6578 * to objects matching these criterias. */
6579 int RGWRados::on_last_entry_in_listing(RGWBucketInfo
& bucket_info
,
6580 const std::string
& obj_prefix
,
6581 const std::string
& obj_delim
,
6582 std::function
<int(const rgw_bucket_dir_entry
&)> handler
)
6584 RGWRados::Bucket
target(this, bucket_info
);
6585 RGWRados::Bucket::List
list_op(&target
);
6587 list_op
.params
.prefix
= obj_prefix
;
6588 list_op
.params
.delim
= obj_delim
;
6590 ldout(cct
, 20) << "iterating listing for bucket=" << bucket_info
.bucket
.name
6591 << ", obj_prefix=" << obj_prefix
6592 << ", obj_delim=" << obj_delim
6595 bool is_truncated
= false;
6597 boost::optional
<rgw_bucket_dir_entry
> last_entry
;
6598 /* We need to rewind to the last object in a listing. */
6600 /* List bucket entries in chunks. */
6601 static constexpr int MAX_LIST_OBJS
= 100;
6602 std::vector
<rgw_bucket_dir_entry
> entries(MAX_LIST_OBJS
);
6604 int ret
= list_op
.list_objects(MAX_LIST_OBJS
, &entries
, nullptr,
6608 } else if (!entries
.empty()) {
6609 last_entry
= entries
.back();
6611 } while (is_truncated
);
6614 return handler(*last_entry
);
6617 /* Empty listing - no items we can run handler on. */
6622 int RGWRados::swift_versioning_copy(RGWObjectCtx
& obj_ctx
,
6623 const rgw_user
& user
,
6624 RGWBucketInfo
& bucket_info
,
6627 if (! swift_versioning_enabled(bucket_info
)) {
6631 obj_ctx
.obj
.set_atomic(obj
);
6633 RGWObjState
* state
= nullptr;
6634 int r
= get_obj_state(&obj_ctx
, bucket_info
, obj
, &state
, false);
6639 if (!state
->exists
) {
6646 const string
& src_name
= obj
.get_oid();
6647 char buf
[src_name
.size() + 32];
6648 struct timespec ts
= ceph::real_clock::to_timespec(state
->mtime
);
6649 snprintf(buf
, sizeof(buf
), "%03x%s/%lld.%06ld", (int)src_name
.size(),
6650 src_name
.c_str(), (long long)ts
.tv_sec
, ts
.tv_nsec
/ 1000);
6652 RGWBucketInfo dest_bucket_info
;
6654 r
= get_bucket_info(obj_ctx
, bucket_info
.bucket
.tenant
, bucket_info
.swift_ver_location
, dest_bucket_info
, NULL
, NULL
);
6656 ldout(cct
, 10) << "failed to read dest bucket info: r=" << r
<< dendl
;
6658 return -ERR_PRECONDITION_FAILED
;
6663 if (dest_bucket_info
.owner
!= bucket_info
.owner
) {
6664 return -ERR_PRECONDITION_FAILED
;
6667 rgw_obj
dest_obj(dest_bucket_info
.bucket
, buf
);
6668 obj_ctx
.obj
.set_atomic(dest_obj
);
6672 r
= copy_obj(obj_ctx
,
6676 NULL
, /* req_info *info */
6682 NULL
, /* time_t *src_mtime */
6683 NULL
, /* time_t *mtime */
6684 NULL
, /* const time_t *mod_ptr */
6685 NULL
, /* const time_t *unmod_ptr */
6686 false, /* bool high_precision_time */
6687 NULL
, /* const char *if_match */
6688 NULL
, /* const char *if_nomatch */
6689 RGWRados::ATTRSMOD_NONE
,
6690 true, /* bool copy_if_newer */
6692 RGW_OBJ_CATEGORY_MAIN
,
6693 0, /* uint64_t olh_epoch */
6694 real_time(), /* time_t delete_at */
6695 NULL
, /* string *version_id */
6696 NULL
, /* string *ptag */
6697 NULL
, /* string *petag */
6698 NULL
, /* void (*progress_cb)(off_t, void *) */
6699 NULL
); /* void *progress_data */
6700 if (r
== -ECANCELED
|| r
== -ENOENT
) {
6701 /* Has already been overwritten, meaning another rgw process already
6709 int RGWRados::swift_versioning_restore(RGWObjectCtx
& obj_ctx
,
6710 const rgw_user
& user
,
6711 RGWBucketInfo
& bucket_info
,
6713 bool& restored
) /* out */
6715 if (! swift_versioning_enabled(bucket_info
)) {
6719 /* Bucket info of the bucket that stores previous versions of our object. */
6720 RGWBucketInfo archive_binfo
;
6722 int ret
= get_bucket_info(obj_ctx
, bucket_info
.bucket
.tenant
,
6723 bucket_info
.swift_ver_location
, archive_binfo
,
6729 /* Abort the operation if the bucket storing our archive belongs to someone
6730 * else. This is a limitation in comparison to Swift as we aren't taking ACLs
6731 * into consideration. For we can live with that.
6733 * TODO: delegate this check to un upper layer and compare with ACLs. */
6734 if (bucket_info
.owner
!= archive_binfo
.owner
) {
6738 /* This code will be executed on latest version of the object. */
6739 const auto handler
= [&](const rgw_bucket_dir_entry
& entry
) -> int {
6740 std::string no_client_id
;
6741 std::string no_op_id
;
6742 std::string no_zone
;
6744 /* We don't support object versioning of Swift API on those buckets that
6745 * are already versioned using the S3 mechanism. This affects also bucket
6746 * storing archived objects. Otherwise the delete operation would create
6747 * a deletion marker. */
6748 if (archive_binfo
.versioned()) {
6750 return -ERR_PRECONDITION_FAILED
;
6753 /* We are requesting ATTRSMOD_NONE so the attr attribute is perfectly
6754 * irrelevant and may be safely skipped. */
6755 std::map
<std::string
, ceph::bufferlist
> no_attrs
;
6757 rgw_obj
archive_obj(archive_binfo
.bucket
, entry
.key
);
6758 obj_ctx
.obj
.set_atomic(archive_obj
);
6759 obj_ctx
.obj
.set_atomic(obj
);
6761 int ret
= copy_obj(obj_ctx
,
6765 nullptr, /* req_info *info */
6768 archive_obj
, /* src obj */
6769 bucket_info
, /* dest bucket info */
6770 archive_binfo
, /* src bucket info */
6771 nullptr, /* time_t *src_mtime */
6772 nullptr, /* time_t *mtime */
6773 nullptr, /* const time_t *mod_ptr */
6774 nullptr, /* const time_t *unmod_ptr */
6775 false, /* bool high_precision_time */
6776 nullptr, /* const char *if_match */
6777 nullptr, /* const char *if_nomatch */
6778 RGWRados::ATTRSMOD_NONE
,
6779 true, /* bool copy_if_newer */
6781 RGW_OBJ_CATEGORY_MAIN
,
6782 0, /* uint64_t olh_epoch */
6783 real_time(), /* time_t delete_at */
6784 nullptr, /* string *version_id */
6785 nullptr, /* string *ptag */
6786 nullptr, /* string *petag */
6787 nullptr, /* void (*progress_cb)(off_t, void *) */
6788 nullptr); /* void *progress_data */
6789 if (ret
== -ECANCELED
|| ret
== -ENOENT
) {
6790 /* Has already been overwritten, meaning another rgw process already
6793 } else if (ret
< 0) {
6799 /* Need to remove the archived copy. */
6800 ret
= delete_obj(obj_ctx
, archive_binfo
, archive_obj
,
6801 archive_binfo
.versioning_status());
6806 const std::string
& obj_name
= obj
.get_oid();
6807 const auto prefix
= boost::str(boost::format("%03x%s") % obj_name
.size()
6810 return on_last_entry_in_listing(archive_binfo
, prefix
, std::string(),
6815 * Write/overwrite an object to the bucket storage.
6816 * bucket: the bucket to store the object in
6817 * obj: the object name/key
6818 * data: the object contents/value
6819 * size: the amount of data to write (data must be this long)
6820 * accounted_size: original size of data before compression, encryption
6821 * mtime: if non-NULL, writes the given mtime to the bucket storage
6822 * attrs: all the given attrs are written to bucket storage for the given object
6823 * exclusive: create object exclusively
6824 * Returns: 0 on success, -ERR# otherwise.
6826 int RGWRados::Object::Write::_do_write_meta(uint64_t size
, uint64_t accounted_size
,
6827 map
<string
, bufferlist
>& attrs
,
6828 bool assume_noent
, bool modify_tail
,
6831 RGWRados::Bucket::UpdateIndex
*index_op
= static_cast<RGWRados::Bucket::UpdateIndex
*>(_index_op
);
6832 RGWRados
*store
= target
->get_store();
6834 ObjectWriteOperation op
;
6837 int r
= target
->get_state(&state
, false, assume_noent
);
6841 rgw_obj
& obj
= target
->get_obj();
6843 if (obj
.get_oid().empty()) {
6844 ldout(store
->ctx(), 0) << "ERROR: " << __func__
<< "(): cannot write object with empty name" << dendl
;
6849 r
= store
->get_obj_head_ref(target
->get_bucket_info(), obj
, &ref
);
6853 bool is_olh
= state
->is_olh
;
6855 bool reset_obj
= (meta
.flags
& PUT_OBJ_CREATE
) != 0;
6857 const string
*ptag
= meta
.ptag
;
6858 if (!ptag
&& !index_op
->get_optag()->empty()) {
6859 ptag
= index_op
->get_optag();
6861 r
= target
->prepare_atomic_modification(op
, reset_obj
, ptag
, meta
.if_match
, meta
.if_nomatch
, false, modify_tail
);
6865 if (real_clock::is_zero(meta
.set_mtime
)) {
6866 meta
.set_mtime
= real_clock::now();
6869 if (state
->is_olh
) {
6870 op
.setxattr(RGW_ATTR_OLH_ID_TAG
, state
->olh_tag
);
6873 struct timespec mtime_ts
= real_clock::to_timespec(meta
.set_mtime
);
6874 op
.mtime2(&mtime_ts
);
6877 /* if we want to overwrite the data, we also want to overwrite the
6878 xattrs, so just remove the object */
6879 op
.write_full(*meta
.data
);
6883 string content_type
;
6886 map
<string
, bufferlist
>::iterator iter
;
6888 for (iter
= meta
.rmattrs
->begin(); iter
!= meta
.rmattrs
->end(); ++iter
) {
6889 const string
& name
= iter
->first
;
6890 op
.rmxattr(name
.c_str());
6894 if (meta
.manifest
) {
6895 /* remove existing manifest attr */
6896 iter
= attrs
.find(RGW_ATTR_MANIFEST
);
6897 if (iter
!= attrs
.end())
6901 ::encode(*meta
.manifest
, bl
);
6902 op
.setxattr(RGW_ATTR_MANIFEST
, bl
);
6905 for (iter
= attrs
.begin(); iter
!= attrs
.end(); ++iter
) {
6906 const string
& name
= iter
->first
;
6907 bufferlist
& bl
= iter
->second
;
6912 op
.setxattr(name
.c_str(), bl
);
6914 if (name
.compare(RGW_ATTR_ETAG
) == 0) {
6916 } else if (name
.compare(RGW_ATTR_CONTENT_TYPE
) == 0) {
6917 content_type
= bl
.c_str();
6918 } else if (name
.compare(RGW_ATTR_ACL
) == 0) {
6922 if (attrs
.find(RGW_ATTR_PG_VER
) == attrs
.end()) {
6923 cls_rgw_obj_store_pg_ver(op
, RGW_ATTR_PG_VER
);
6926 if (attrs
.find(RGW_ATTR_SOURCE_ZONE
) == attrs
.end()) {
6928 ::encode(store
->get_zone_short_id(), bl
);
6929 op
.setxattr(RGW_ATTR_SOURCE_ZONE
, bl
);
6940 if (!reset_obj
) { //Multipart upload, it has immutable head.
6941 orig_exists
= false;
6944 orig_exists
= state
->exists
;
6945 orig_size
= state
->accounted_size
;
6948 bool versioned_target
= (meta
.olh_epoch
> 0 || !obj
.key
.instance
.empty());
6950 bool versioned_op
= (target
->versioning_enabled() || is_olh
|| versioned_target
);
6953 index_op
->set_bilog_flags(RGW_BILOG_FLAG_VERSIONED_OP
);
6956 if (!index_op
->is_prepared()) {
6957 r
= index_op
->prepare(CLS_RGW_OP_ADD
, &state
->write_tag
);
6962 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
6963 if (r
< 0) { /* we can expect to get -ECANCELED if object was replaced under,
6964 or -ENOENT if was removed, or -EEXIST if it did not exist
6965 before and now it does */
6966 if (r
== -EEXIST
&& assume_noent
) {
6967 target
->invalidate_state();
6973 epoch
= ref
.ioctx
.get_last_version();
6974 poolid
= ref
.ioctx
.get_id();
6976 r
= target
->complete_atomic_modification();
6978 ldout(store
->ctx(), 0) << "ERROR: complete_atomic_modification returned r=" << r
<< dendl
;
6981 r
= index_op
->complete(poolid
, epoch
, size
, accounted_size
,
6982 meta
.set_mtime
, etag
, content_type
, &acl_bl
,
6983 meta
.category
, meta
.remove_objs
, meta
.user_data
);
6988 *meta
.mtime
= meta
.set_mtime
;
6991 /* note that index_op was using state so we couldn't invalidate it earlier */
6992 target
->invalidate_state();
6996 r
= store
->set_olh(target
->get_ctx(), target
->get_bucket_info(), obj
, false, NULL
, meta
.olh_epoch
, real_time(), false, meta
.zones_trace
);
7002 if (!real_clock::is_zero(meta
.delete_at
)) {
7003 rgw_obj_index_key obj_key
;
7004 obj
.key
.get_index_key(&obj_key
);
7006 r
= store
->objexp_hint_add(meta
.delete_at
,
7007 obj
.bucket
.tenant
, obj
.bucket
.name
, obj
.bucket
.bucket_id
, obj_key
);
7009 ldout(store
->ctx(), 0) << "ERROR: objexp_hint_add() returned r=" << r
<< ", object will not get removed" << dendl
;
7010 /* ignoring error, nothing we can do at this point */
7013 meta
.canceled
= false;
7015 /* update quota cache */
7016 store
->quota_handler
->update_stats(meta
.owner
, obj
.bucket
, (orig_exists
? 0 : 1),
7017 accounted_size
, orig_size
);
7021 int ret
= index_op
->cancel();
7023 ldout(store
->ctx(), 0) << "ERROR: index_op.cancel()() returned ret=" << ret
<< dendl
;
7026 meta
.canceled
= true;
7028 /* we lost in a race. There are a few options:
7029 * - existing object was rewritten (ECANCELED)
7030 * - non existing object was created (EEXIST)
7031 * - object was removed (ENOENT)
7032 * should treat it as a success
7034 if (meta
.if_match
== NULL
&& meta
.if_nomatch
== NULL
) {
7035 if (r
== -ECANCELED
|| r
== -ENOENT
|| r
== -EEXIST
) {
7039 if (meta
.if_match
!= NULL
) {
7040 // only overwrite existing object
7041 if (strcmp(meta
.if_match
, "*") == 0) {
7043 r
= -ERR_PRECONDITION_FAILED
;
7044 } else if (r
== -ECANCELED
) {
7050 if (meta
.if_nomatch
!= NULL
) {
7051 // only create a new object
7052 if (strcmp(meta
.if_nomatch
, "*") == 0) {
7054 r
= -ERR_PRECONDITION_FAILED
;
7055 } else if (r
== -ENOENT
) {
7065 int RGWRados::Object::Write::write_meta(uint64_t size
, uint64_t accounted_size
,
7066 map
<string
, bufferlist
>& attrs
)
7068 RGWBucketInfo
& bucket_info
= target
->get_bucket_info();
7070 RGWRados::Bucket
bop(target
->get_store(), bucket_info
);
7071 RGWRados::Bucket::UpdateIndex
index_op(&bop
, target
->get_obj());
7072 index_op
.set_zones_trace(meta
.zones_trace
);
7074 bool assume_noent
= (meta
.if_match
== NULL
&& meta
.if_nomatch
== NULL
);
7077 r
= _do_write_meta(size
, accounted_size
, attrs
, assume_noent
, meta
.modify_tail
, (void *)&index_op
);
7079 assume_noent
= false;
7082 if (!assume_noent
) {
7083 r
= _do_write_meta(size
, accounted_size
, attrs
, assume_noent
, meta
.modify_tail
, (void *)&index_op
);
7088 /** Write/overwrite a system object. */
7089 int RGWRados::put_system_obj_impl(rgw_raw_obj
& obj
, uint64_t size
, real_time
*mtime
,
7090 map
<std::string
, bufferlist
>& attrs
, int flags
,
7092 RGWObjVersionTracker
*objv_tracker
,
7093 real_time set_mtime
/* 0 for don't set */)
7096 int r
= get_system_obj_ref(obj
, &ref
);
7100 ObjectWriteOperation op
;
7102 if (flags
& PUT_OBJ_EXCL
) {
7103 if (!(flags
& PUT_OBJ_CREATE
))
7105 op
.create(true); // exclusive create
7108 op
.set_op_flags2(LIBRADOS_OP_FLAG_FAILOK
);
7113 objv_tracker
->prepare_op_for_write(&op
);
7116 if (real_clock::is_zero(set_mtime
)) {
7117 set_mtime
= real_clock::now();
7120 struct timespec mtime_ts
= real_clock::to_timespec(set_mtime
);
7121 op
.mtime2(&mtime_ts
);
7122 op
.write_full(data
);
7126 for (map
<string
, bufferlist
>::iterator iter
= attrs
.begin(); iter
!= attrs
.end(); ++iter
) {
7127 const string
& name
= iter
->first
;
7128 bufferlist
& bl
= iter
->second
;
7133 op
.setxattr(name
.c_str(), bl
);
7136 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
7142 objv_tracker
->apply_write();
7152 int RGWRados::put_system_obj_data(void *ctx
, rgw_raw_obj
& obj
, bufferlist
& bl
,
7153 off_t ofs
, bool exclusive
,
7154 RGWObjVersionTracker
*objv_tracker
)
7157 int r
= get_system_obj_ref(obj
, &ref
);
7162 ObjectWriteOperation op
;
7168 objv_tracker
->prepare_op_for_write(&op
);
7175 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
7180 objv_tracker
->apply_write();
7186 * Write/overwrite an object to the bucket storage.
7187 * bucket: the bucket to store the object in
7188 * obj: the object name/key
7189 * data: the object contents/value
7190 * offset: the offet to write to in the object
7191 * If this is -1, we will overwrite the whole object.
7192 * size: the amount of data to write (data must be this long)
7193 * attrs: all the given attrs are written to bucket storage for the given object
7194 * Returns: 0 on success, -ERR# otherwise.
7197 int RGWRados::aio_put_obj_data(void *ctx
, rgw_raw_obj
& obj
, bufferlist
& bl
,
7198 off_t ofs
, bool exclusive
,
7202 int r
= get_raw_obj_ref(obj
, &ref
);
7207 AioCompletion
*c
= librados::Rados::aio_create_completion(NULL
, NULL
, NULL
);
7210 ObjectWriteOperation op
;
7220 r
= ref
.ioctx
.aio_operate(ref
.oid
, c
, &op
);
7227 int RGWRados::aio_wait(void *handle
)
7229 AioCompletion
*c
= (AioCompletion
*)handle
;
7231 int ret
= c
->get_return_value();
7236 bool RGWRados::aio_completed(void *handle
)
7238 AioCompletion
*c
= (AioCompletion
*)handle
;
7239 return c
->is_safe();
7242 class RGWRadosPutObj
: public RGWGetDataCB
7246 RGWPutObjDataProcessor
*filter
;
7247 boost::optional
<RGWPutObj_Compress
>& compressor
;
7248 CompressorRef
& plugin
;
7249 RGWPutObjProcessor_Atomic
*processor
;
7250 RGWOpStateSingleOp
*opstate
;
7251 void (*progress_cb
)(off_t
, void *);
7252 void *progress_data
;
7253 bufferlist extra_data_bl
;
7254 uint64_t extra_data_len
;
7256 map
<string
, bufferlist
> src_attrs
;
7258 RGWRadosPutObj(CephContext
* cct
,
7259 CompressorRef
& plugin
,
7260 boost::optional
<RGWPutObj_Compress
>& compressor
,
7261 RGWPutObjProcessor_Atomic
*p
,
7262 RGWOpStateSingleOp
*_ops
,
7263 void (*_progress_cb
)(off_t
, void *),
7264 void *_progress_data
) :
7267 compressor(compressor
),
7271 progress_cb(_progress_cb
),
7272 progress_data(_progress_data
),
7276 int process_attrs(void) {
7277 if (extra_data_bl
.length()) {
7279 if (!jp
.parse(extra_data_bl
.c_str(), extra_data_bl
.length())) {
7280 ldout(cct
, 0) << "failed to parse response extra data. len=" << extra_data_bl
.length() << " data=" << extra_data_bl
.c_str() << dendl
;
7284 JSONDecoder::decode_json("attrs", src_attrs
, &jp
);
7286 src_attrs
.erase(RGW_ATTR_COMPRESSION
);
7287 src_attrs
.erase(RGW_ATTR_MANIFEST
); // not interested in original object layout
7290 if (plugin
&& src_attrs
.find(RGW_ATTR_CRYPT_MODE
) == src_attrs
.end()) {
7291 //do not compress if object is encrypted
7292 compressor
= boost::in_place(cct
, plugin
, filter
);
7293 filter
= &*compressor
;
7298 int handle_data(bufferlist
& bl
, off_t ofs
, off_t len
) override
{
7300 progress_cb(ofs
, progress_data
);
7302 if (extra_data_len
) {
7303 size_t extra_len
= bl
.length();
7304 if (extra_len
> extra_data_len
)
7305 extra_len
= extra_data_len
;
7308 bl
.splice(0, extra_len
, &extra
);
7309 extra_data_bl
.append(extra
);
7311 extra_data_len
-= extra_len
;
7312 if (extra_data_len
== 0) {
7313 int res
= process_attrs();
7317 if (bl
.length() == 0) {
7321 data_len
+= bl
.length();
7324 bool need_opstate
= true;
7327 void *handle
= NULL
;
7329 uint64_t size
= bl
.length();
7330 int ret
= filter
->handle_data(bl
, ofs
, &handle
, &obj
, &again
);
7334 if (need_opstate
&& opstate
) {
7335 /* need to update opstate repository with new state. This is ratelimited, so we're not
7336 * really doing it every time
7338 ret
= opstate
->renew_state();
7340 ldout(cct
, 0) << "ERROR: RGWRadosPutObj::handle_data(): failed to renew op state ret=" << ret
<< dendl
;
7341 int r
= filter
->throttle_data(handle
, obj
, size
, false);
7343 ldout(cct
, 0) << "ERROR: RGWRadosPutObj::handle_data(): processor->throttle_data() returned " << r
<< dendl
;
7345 /* could not renew state! might have been marked as cancelled */
7348 need_opstate
= false;
7351 ret
= filter
->throttle_data(handle
, obj
, size
, false);
7359 bufferlist
& get_extra_data() { return extra_data_bl
; }
7361 map
<string
, bufferlist
>& get_attrs() { return src_attrs
; }
7363 void set_extra_data_len(uint64_t len
) override
{
7364 extra_data_len
= len
;
7367 uint64_t get_data_len() {
7371 int complete(const string
& etag
, real_time
*mtime
, real_time set_mtime
,
7372 map
<string
, bufferlist
>& attrs
, real_time delete_at
, rgw_zone_set
*zones_trace
) {
7373 return processor
->complete(data_len
, etag
, mtime
, set_mtime
, attrs
, delete_at
, NULL
, NULL
, NULL
, zones_trace
);
7376 bool is_canceled() {
7377 return processor
->is_canceled();
7382 * prepare attrset depending on attrs_mod.
7384 static void set_copy_attrs(map
<string
, bufferlist
>& src_attrs
,
7385 map
<string
, bufferlist
>& attrs
,
7386 RGWRados::AttrsMod attrs_mod
)
7388 switch (attrs_mod
) {
7389 case RGWRados::ATTRSMOD_NONE
:
7392 case RGWRados::ATTRSMOD_REPLACE
:
7393 if (!attrs
[RGW_ATTR_ETAG
].length()) {
7394 attrs
[RGW_ATTR_ETAG
] = src_attrs
[RGW_ATTR_ETAG
];
7396 if (!attrs
[RGW_ATTR_TAIL_TAG
].length()) {
7397 auto ttiter
= src_attrs
.find(RGW_ATTR_TAIL_TAG
);
7398 if (ttiter
!= src_attrs
.end()) {
7399 attrs
[RGW_ATTR_TAIL_TAG
] = src_attrs
[RGW_ATTR_TAIL_TAG
];
7403 case RGWRados::ATTRSMOD_MERGE
:
7404 for (map
<string
, bufferlist
>::iterator it
= src_attrs
.begin(); it
!= src_attrs
.end(); ++it
) {
7405 if (attrs
.find(it
->first
) == attrs
.end()) {
7406 attrs
[it
->first
] = it
->second
;
7413 int RGWRados::rewrite_obj(RGWBucketInfo
& dest_bucket_info
, rgw_obj
& obj
)
7415 map
<string
, bufferlist
> attrset
;
7419 RGWObjectCtx
rctx(this);
7421 RGWRados::Object
op_target(this, dest_bucket_info
, rctx
, obj
);
7422 RGWRados::Object::Read
read_op(&op_target
);
7424 read_op
.params
.attrs
= &attrset
;
7425 read_op
.params
.lastmod
= &mtime
;
7426 read_op
.params
.obj_size
= &obj_size
;
7428 int ret
= read_op
.prepare();
7432 attrset
.erase(RGW_ATTR_ID_TAG
);
7433 attrset
.erase(RGW_ATTR_TAIL_TAG
);
7435 uint64_t max_chunk_size
;
7437 ret
= get_max_chunk_size(dest_bucket_info
.placement_rule
, obj
, &max_chunk_size
);
7439 ldout(cct
, 0) << "ERROR: failed to get max_chunk_size() for bucket " << obj
.bucket
<< dendl
;
7443 return copy_obj_data(rctx
, dest_bucket_info
, read_op
, obj_size
- 1, obj
, obj
, max_chunk_size
, NULL
, mtime
, attrset
,
7444 RGW_OBJ_CATEGORY_MAIN
, 0, real_time(), NULL
, NULL
, NULL
);
7447 struct obj_time_weight
{
7449 uint32_t zone_short_id
;
7451 bool high_precision
;
7453 obj_time_weight() : zone_short_id(0), pg_ver(0), high_precision(false) {}
7455 bool compare_low_precision(const obj_time_weight
& rhs
) {
7456 struct timespec l
= ceph::real_clock::to_timespec(mtime
);
7457 struct timespec r
= ceph::real_clock::to_timespec(rhs
.mtime
);
7466 if (zone_short_id
!= rhs
.zone_short_id
) {
7467 return (zone_short_id
< rhs
.zone_short_id
);
7469 return (pg_ver
< rhs
.pg_ver
);
7473 bool operator<(const obj_time_weight
& rhs
) {
7474 if (!high_precision
|| !rhs
.high_precision
) {
7475 return compare_low_precision(rhs
);
7477 if (mtime
> rhs
.mtime
) {
7480 if (mtime
< rhs
.mtime
) {
7483 if (zone_short_id
!= rhs
.zone_short_id
) {
7484 return (zone_short_id
< rhs
.zone_short_id
);
7486 return (pg_ver
< rhs
.pg_ver
);
7489 void init(const real_time
& _mtime
, uint32_t _short_id
, uint64_t _pg_ver
) {
7491 zone_short_id
= _short_id
;
7495 void init(RGWObjState
*state
) {
7496 mtime
= state
->mtime
;
7497 zone_short_id
= state
->zone_short_id
;
7498 pg_ver
= state
->pg_ver
;
7502 inline ostream
& operator<<(ostream
& out
, const obj_time_weight
&o
) {
7505 if (o
.zone_short_id
!= 0 || o
.pg_ver
!= 0) {
7506 out
<< "[zid=" << o
.zone_short_id
<< ", pgv=" << o
.pg_ver
<< "]";
7512 class RGWGetExtraDataCB
: public RGWGetDataCB
{
7513 bufferlist extra_data
;
7515 RGWGetExtraDataCB() {}
7516 int handle_data(bufferlist
& bl
, off_t bl_ofs
, off_t bl_len
) override
{
7517 if (extra_data
.length() < extra_data_len
) {
7518 off_t max
= extra_data_len
- extra_data
.length();
7522 bl
.splice(0, max
, &extra_data
);
7527 bufferlist
& get_extra_data() {
7532 int RGWRados::stat_remote_obj(RGWObjectCtx
& obj_ctx
,
7533 const rgw_user
& user_id
,
7534 const string
& client_id
,
7536 const string
& source_zone
,
7538 RGWBucketInfo
& src_bucket_info
,
7539 real_time
*src_mtime
,
7541 const real_time
*mod_ptr
,
7542 const real_time
*unmod_ptr
,
7543 bool high_precision_time
,
7544 const char *if_match
,
7545 const char *if_nomatch
,
7546 map
<string
, bufferlist
> *pattrs
,
7551 /* source is in a different zonegroup, copy from there */
7553 RGWRESTStreamRWRequest
*in_stream_req
;
7555 map
<string
, bufferlist
> src_attrs
;
7556 append_rand_alpha(cct
, tag
, tag
, 32);
7557 obj_time_weight set_mtime_weight
;
7558 set_mtime_weight
.high_precision
= high_precision_time
;
7561 if (source_zone
.empty()) {
7562 if (src_bucket_info
.zonegroup
.empty()) {
7563 /* source is in the master zonegroup */
7564 conn
= rest_master_conn
;
7566 map
<string
, RGWRESTConn
*>::iterator iter
= zonegroup_conn_map
.find(src_bucket_info
.zonegroup
);
7567 if (iter
== zonegroup_conn_map
.end()) {
7568 ldout(cct
, 0) << "could not find zonegroup connection to zonegroup: " << source_zone
<< dendl
;
7571 conn
= iter
->second
;
7574 map
<string
, RGWRESTConn
*>::iterator iter
= zone_conn_map
.find(source_zone
);
7575 if (iter
== zone_conn_map
.end()) {
7576 ldout(cct
, 0) << "could not find zone connection to zone: " << source_zone
<< dendl
;
7579 conn
= iter
->second
;
7582 RGWGetExtraDataCB cb
;
7584 map
<string
, string
> req_headers
;
7585 real_time set_mtime
;
7587 const real_time
*pmod
= mod_ptr
;
7589 obj_time_weight dest_mtime_weight
;
7591 constexpr bool prepend_meta
= true;
7592 constexpr bool get_op
= true;
7593 constexpr bool rgwx_stat
= true;
7594 constexpr bool sync_manifest
= true;
7595 constexpr bool skip_decrypt
= true;
7596 int ret
= conn
->get_obj(user_id
, info
, src_obj
, pmod
, unmod_ptr
,
7597 dest_mtime_weight
.zone_short_id
, dest_mtime_weight
.pg_ver
,
7598 prepend_meta
, get_op
, rgwx_stat
,
7599 sync_manifest
, skip_decrypt
, &cb
, &in_stream_req
);
7604 ret
= conn
->complete_request(in_stream_req
, etag
, &set_mtime
, psize
, req_headers
);
7609 bufferlist
& extra_data_bl
= cb
.get_extra_data();
7610 if (extra_data_bl
.length()) {
7612 if (!jp
.parse(extra_data_bl
.c_str(), extra_data_bl
.length())) {
7613 ldout(cct
, 0) << "failed to parse response extra data. len=" << extra_data_bl
.length() << " data=" << extra_data_bl
.c_str() << dendl
;
7617 JSONDecoder::decode_json("attrs", src_attrs
, &jp
);
7619 src_attrs
.erase(RGW_ATTR_MANIFEST
); // not interested in original object layout
7623 *src_mtime
= set_mtime
;
7627 map
<string
, bufferlist
>::iterator iter
= src_attrs
.find(RGW_ATTR_ETAG
);
7628 if (iter
!= src_attrs
.end()) {
7629 bufferlist
& etagbl
= iter
->second
;
7630 *petag
= etagbl
.to_str();
7635 *pattrs
= src_attrs
;
7641 int RGWRados::fetch_remote_obj(RGWObjectCtx
& obj_ctx
,
7642 const rgw_user
& user_id
,
7643 const string
& client_id
,
7644 const string
& op_id
,
7645 bool record_op_state
,
7647 const string
& source_zone
,
7650 RGWBucketInfo
& dest_bucket_info
,
7651 RGWBucketInfo
& src_bucket_info
,
7652 real_time
*src_mtime
,
7654 const real_time
*mod_ptr
,
7655 const real_time
*unmod_ptr
,
7656 bool high_precision_time
,
7657 const char *if_match
,
7658 const char *if_nomatch
,
7661 map
<string
, bufferlist
>& attrs
,
7662 RGWObjCategory category
,
7664 real_time delete_at
,
7667 ceph::buffer::list
*petag
,
7668 void (*progress_cb
)(off_t
, void *),
7669 void *progress_data
,
7670 rgw_zone_set
*zones_trace
)
7672 /* source is in a different zonegroup, copy from there */
7674 RGWRESTStreamRWRequest
*in_stream_req
;
7677 append_rand_alpha(cct
, tag
, tag
, 32);
7678 obj_time_weight set_mtime_weight
;
7679 set_mtime_weight
.high_precision
= high_precision_time
;
7681 RGWPutObjProcessor_Atomic
processor(obj_ctx
,
7682 dest_bucket_info
, dest_obj
.bucket
, dest_obj
.key
.name
,
7683 cct
->_conf
->rgw_obj_stripe_size
, tag
, dest_bucket_info
.versioning_enabled());
7684 if (version_id
&& *version_id
!= "null") {
7685 processor
.set_version_id(*version_id
);
7687 processor
.set_olh_epoch(olh_epoch
);
7688 int ret
= processor
.prepare(this, NULL
);
7694 if (source_zone
.empty()) {
7695 if (dest_bucket_info
.zonegroup
.empty()) {
7696 /* source is in the master zonegroup */
7697 conn
= rest_master_conn
;
7699 map
<string
, RGWRESTConn
*>::iterator iter
= zonegroup_conn_map
.find(src_bucket_info
.zonegroup
);
7700 if (iter
== zonegroup_conn_map
.end()) {
7701 ldout(cct
, 0) << "could not find zonegroup connection to zonegroup: " << source_zone
<< dendl
;
7704 conn
= iter
->second
;
7707 map
<string
, RGWRESTConn
*>::iterator iter
= zone_conn_map
.find(source_zone
);
7708 if (iter
== zone_conn_map
.end()) {
7709 ldout(cct
, 0) << "could not find zone connection to zone: " << source_zone
<< dendl
;
7712 conn
= iter
->second
;
7715 string obj_name
= dest_obj
.bucket
.name
+ "/" + dest_obj
.get_oid();
7717 RGWOpStateSingleOp
*opstate
= NULL
;
7719 if (record_op_state
) {
7720 opstate
= new RGWOpStateSingleOp(this, client_id
, op_id
, obj_name
);
7722 ret
= opstate
->set_state(RGWOpState::OPSTATE_IN_PROGRESS
);
7724 ldout(cct
, 0) << "ERROR: failed to set opstate ret=" << ret
<< dendl
;
7730 boost::optional
<RGWPutObj_Compress
> compressor
;
7731 CompressorRef plugin
;
7733 const auto& compression_type
= zone_params
.get_compression_type(
7734 dest_bucket_info
.placement_rule
);
7735 if (compression_type
!= "none") {
7736 plugin
= Compressor::create(cct
, compression_type
);
7738 ldout(cct
, 1) << "Cannot load plugin for compression type "
7739 << compression_type
<< dendl
;
7743 RGWRadosPutObj
cb(cct
, plugin
, compressor
, &processor
, opstate
, progress_cb
, progress_data
);
7746 map
<string
, string
> req_headers
;
7747 real_time set_mtime
;
7749 RGWObjState
*dest_state
= NULL
;
7751 const real_time
*pmod
= mod_ptr
;
7753 obj_time_weight dest_mtime_weight
;
7755 if (copy_if_newer
) {
7756 /* need to get mtime for destination */
7757 ret
= get_obj_state(&obj_ctx
, dest_bucket_info
, dest_obj
, &dest_state
, false);
7761 if (!real_clock::is_zero(dest_state
->mtime
)) {
7762 dest_mtime_weight
.init(dest_state
);
7763 pmod
= &dest_mtime_weight
.mtime
;
7767 static constexpr bool prepend_meta
= true;
7768 static constexpr bool get_op
= true;
7769 static constexpr bool rgwx_stat
= false;
7770 static constexpr bool sync_manifest
= true;
7771 static constexpr bool skip_decrypt
= true;
7772 ret
= conn
->get_obj(user_id
, info
, src_obj
, pmod
, unmod_ptr
,
7773 dest_mtime_weight
.zone_short_id
, dest_mtime_weight
.pg_ver
,
7774 prepend_meta
, get_op
, rgwx_stat
,
7775 sync_manifest
, skip_decrypt
, &cb
, &in_stream_req
);
7780 ret
= conn
->complete_request(in_stream_req
, etag
, &set_mtime
, nullptr, req_headers
);
7784 if (compressor
&& compressor
->is_compressed()) {
7786 RGWCompressionInfo cs_info
;
7787 cs_info
.compression_type
= plugin
->get_type_name();
7788 cs_info
.orig_size
= cb
.get_data_len();
7789 cs_info
.blocks
= move(compressor
->get_compression_blocks());
7790 ::encode(cs_info
, tmp
);
7791 cb
.get_attrs()[RGW_ATTR_COMPRESSION
] = tmp
;
7794 if (source_zone
.empty()) { /* need to preserve expiration if copy in the same zonegroup */
7795 cb
.get_attrs().erase(RGW_ATTR_DELETE_AT
);
7797 map
<string
, bufferlist
>::iterator iter
= cb
.get_attrs().find(RGW_ATTR_DELETE_AT
);
7798 if (iter
!= cb
.get_attrs().end()) {
7800 ::decode(delete_at
, iter
->second
);
7801 } catch (buffer::error
& err
) {
7802 ldout(cct
, 0) << "ERROR: failed to decode delete_at field in intra zone copy" << dendl
;
7808 *src_mtime
= set_mtime
;
7812 const auto iter
= cb
.get_attrs().find(RGW_ATTR_ETAG
);
7813 if (iter
!= cb
.get_attrs().end()) {
7814 *petag
= iter
->second
;
7818 if (source_zone
.empty()) {
7819 set_copy_attrs(cb
.get_attrs(), attrs
, attrs_mod
);
7821 attrs
= cb
.get_attrs();
7824 if (copy_if_newer
) {
7825 uint64_t pg_ver
= 0;
7826 auto i
= attrs
.find(RGW_ATTR_PG_VER
);
7827 if (i
!= attrs
.end() && i
->second
.length() > 0) {
7828 bufferlist::iterator iter
= i
->second
.begin();
7830 ::decode(pg_ver
, iter
);
7831 } catch (buffer::error
& err
) {
7832 ldout(ctx(), 0) << "ERROR: failed to decode pg ver attribute, ignoring" << dendl
;
7833 /* non critical error */
7836 set_mtime_weight
.init(set_mtime
, get_zone_short_id(), pg_ver
);
7839 #define MAX_COMPLETE_RETRY 100
7840 for (i
= 0; i
< MAX_COMPLETE_RETRY
; i
++) {
7841 ret
= cb
.complete(etag
, mtime
, set_mtime
, attrs
, delete_at
, zones_trace
);
7845 if (copy_if_newer
&& cb
.is_canceled()) {
7846 ldout(cct
, 20) << "raced with another write of obj: " << dest_obj
<< dendl
;
7847 obj_ctx
.obj
.invalidate(dest_obj
); /* object was overwritten */
7848 ret
= get_obj_state(&obj_ctx
, dest_bucket_info
, dest_obj
, &dest_state
, false);
7850 ldout(cct
, 0) << "ERROR: " << __func__
<< ": get_err_state() returned ret=" << ret
<< dendl
;
7853 dest_mtime_weight
.init(dest_state
);
7854 dest_mtime_weight
.high_precision
= high_precision_time
;
7855 if (!dest_state
->exists
||
7856 dest_mtime_weight
< set_mtime_weight
) {
7857 ldout(cct
, 20) << "retrying writing object mtime=" << set_mtime
<< " dest_state->mtime=" << dest_state
->mtime
<< " dest_state->exists=" << dest_state
->exists
<< dendl
;
7860 ldout(cct
, 20) << "not retrying writing object mtime=" << set_mtime
<< " dest_state->mtime=" << dest_state
->mtime
<< " dest_state->exists=" << dest_state
->exists
<< dendl
;
7866 if (i
== MAX_COMPLETE_RETRY
) {
7867 ldout(cct
, 0) << "ERROR: retried object completion too many times, something is wrong!" << dendl
;
7873 ret
= opstate
->set_state(RGWOpState::OPSTATE_COMPLETE
);
7875 ldout(cct
, 0) << "ERROR: failed to set opstate ret=" << ret
<< dendl
;
7882 if (copy_if_newer
&& ret
== -ERR_NOT_MODIFIED
) {
7886 RGWOpState::OpState state
;
7888 state
= RGWOpState::OPSTATE_ERROR
;
7890 state
= RGWOpState::OPSTATE_COMPLETE
;
7892 int r
= opstate
->set_state(state
);
7894 ldout(cct
, 0) << "ERROR: failed to set opstate r=" << ret
<< dendl
;
7902 int RGWRados::copy_obj_to_remote_dest(RGWObjState
*astate
,
7903 map
<string
, bufferlist
>& src_attrs
,
7904 RGWRados::Object::Read
& read_op
,
7905 const rgw_user
& user_id
,
7911 RGWRESTStreamWriteRequest
*out_stream_req
;
7913 int ret
= rest_master_conn
->put_obj_init(user_id
, dest_obj
, astate
->size
, src_attrs
, &out_stream_req
);
7918 ret
= read_op
.iterate(0, astate
->size
- 1, out_stream_req
->get_out_cb());
7920 delete out_stream_req
;
7924 ret
= rest_master_conn
->complete_request(out_stream_req
, etag
, mtime
);
7933 * dest_obj: the object to copy into
7934 * src_obj: the object to copy from
7935 * attrs: usage depends on attrs_mod parameter
7936 * attrs_mod: the modification mode of the attrs, may have the following values:
7937 * ATTRSMOD_NONE - the attributes of the source object will be
7938 * copied without modifications, attrs parameter is ignored;
7939 * ATTRSMOD_REPLACE - new object will have the attributes provided by attrs
7940 * parameter, source object attributes are not copied;
7941 * ATTRSMOD_MERGE - any conflicting meta keys on the source object's attributes
7942 * are overwritten by values contained in attrs parameter.
7943 * err: stores any errors resulting from the get of the original object
7944 * Returns: 0 on success, -ERR# otherwise.
7946 int RGWRados::copy_obj(RGWObjectCtx
& obj_ctx
,
7947 const rgw_user
& user_id
,
7948 const string
& client_id
,
7949 const string
& op_id
,
7951 const string
& source_zone
,
7954 RGWBucketInfo
& dest_bucket_info
,
7955 RGWBucketInfo
& src_bucket_info
,
7956 real_time
*src_mtime
,
7958 const real_time
*mod_ptr
,
7959 const real_time
*unmod_ptr
,
7960 bool high_precision_time
,
7961 const char *if_match
,
7962 const char *if_nomatch
,
7965 map
<string
, bufferlist
>& attrs
,
7966 RGWObjCategory category
,
7968 real_time delete_at
,
7971 ceph::buffer::list
*petag
,
7972 void (*progress_cb
)(off_t
, void *),
7973 void *progress_data
)
7977 rgw_obj shadow_obj
= dest_obj
;
7983 append_rand_alpha(cct
, dest_obj
.get_oid(), shadow_oid
, 32);
7984 shadow_obj
.init_ns(dest_obj
.bucket
, shadow_oid
, shadow_ns
);
7986 remote_dest
= !get_zonegroup().equals(dest_bucket_info
.zonegroup
);
7987 remote_src
= !get_zonegroup().equals(src_bucket_info
.zonegroup
);
7989 if (remote_src
&& remote_dest
) {
7990 ldout(cct
, 0) << "ERROR: can't copy object when both src and dest buckets are remote" << dendl
;
7994 ldout(cct
, 5) << "Copy object " << src_obj
.bucket
<< ":" << src_obj
.get_oid() << " => " << dest_obj
.bucket
<< ":" << dest_obj
.get_oid() << dendl
;
7996 if (remote_src
|| !source_zone
.empty()) {
7997 return fetch_remote_obj(obj_ctx
, user_id
, client_id
, op_id
, true, info
, source_zone
,
7998 dest_obj
, src_obj
, dest_bucket_info
, src_bucket_info
, src_mtime
, mtime
, mod_ptr
,
7999 unmod_ptr
, high_precision_time
,
8000 if_match
, if_nomatch
, attrs_mod
, copy_if_newer
, attrs
, category
,
8001 olh_epoch
, delete_at
, version_id
, ptag
, petag
, progress_cb
, progress_data
);
8004 map
<string
, bufferlist
> src_attrs
;
8005 RGWRados::Object
src_op_target(this, src_bucket_info
, obj_ctx
, src_obj
);
8006 RGWRados::Object::Read
read_op(&src_op_target
);
8008 read_op
.conds
.mod_ptr
= mod_ptr
;
8009 read_op
.conds
.unmod_ptr
= unmod_ptr
;
8010 read_op
.conds
.high_precision_time
= high_precision_time
;
8011 read_op
.conds
.if_match
= if_match
;
8012 read_op
.conds
.if_nomatch
= if_nomatch
;
8013 read_op
.params
.attrs
= &src_attrs
;
8014 read_op
.params
.lastmod
= src_mtime
;
8015 read_op
.params
.obj_size
= &obj_size
;
8017 ret
= read_op
.prepare();
8022 src_attrs
[RGW_ATTR_ACL
] = attrs
[RGW_ATTR_ACL
];
8023 src_attrs
.erase(RGW_ATTR_DELETE_AT
);
8025 set_copy_attrs(src_attrs
, attrs
, attrs_mod
);
8026 attrs
.erase(RGW_ATTR_ID_TAG
);
8027 attrs
.erase(RGW_ATTR_PG_VER
);
8028 attrs
.erase(RGW_ATTR_SOURCE_ZONE
);
8029 map
<string
, bufferlist
>::iterator cmp
= src_attrs
.find(RGW_ATTR_COMPRESSION
);
8030 if (cmp
!= src_attrs
.end())
8031 attrs
[RGW_ATTR_COMPRESSION
] = cmp
->second
;
8033 RGWObjManifest manifest
;
8034 RGWObjState
*astate
= NULL
;
8036 ret
= get_obj_state(&obj_ctx
, src_bucket_info
, src_obj
, &astate
);
8041 vector
<rgw_raw_obj
> ref_objs
;
8044 /* dest is in a different zonegroup, copy it there */
8045 return copy_obj_to_remote_dest(astate
, attrs
, read_op
, user_id
, dest_obj
, mtime
);
8047 uint64_t max_chunk_size
;
8049 ret
= get_max_chunk_size(dest_bucket_info
.placement_rule
, dest_obj
, &max_chunk_size
);
8051 ldout(cct
, 0) << "ERROR: failed to get max_chunk_size() for bucket " << dest_obj
.bucket
<< dendl
;
8057 if (!get_obj_data_pool(src_bucket_info
.placement_rule
, src_obj
, &src_pool
)) {
8058 ldout(cct
, 0) << "ERROR: failed to locate data pool for " << src_obj
<< dendl
;
8061 if (!get_obj_data_pool(dest_bucket_info
.placement_rule
, dest_obj
, &dest_pool
)) {
8062 ldout(cct
, 0) << "ERROR: failed to locate data pool for " << dest_obj
<< dendl
;
8067 bool copy_data
= !astate
->has_manifest
|| (src_pool
!= dest_pool
);
8068 bool copy_first
= false;
8069 if (astate
->has_manifest
) {
8070 if (!astate
->manifest
.has_tail()) {
8073 uint64_t head_size
= astate
->manifest
.get_head_size();
8075 if (head_size
> 0) {
8076 if (head_size
> max_chunk_size
) {
8086 const auto iter
= attrs
.find(RGW_ATTR_ETAG
);
8087 if (iter
!= attrs
.end()) {
8088 *petag
= iter
->second
;
8092 if (copy_data
) { /* refcounting tail wouldn't work here, just copy the data */
8093 return copy_obj_data(obj_ctx
, dest_bucket_info
, read_op
, obj_size
- 1, dest_obj
, src_obj
,
8094 max_chunk_size
, mtime
, real_time(), attrs
, category
, olh_epoch
, delete_at
,
8095 version_id
, ptag
, petag
);
8098 RGWObjManifest::obj_iterator miter
= astate
->manifest
.obj_begin();
8100 if (copy_first
) { // we need to copy first chunk, not increase refcount
8105 ret
= get_raw_obj_ref(miter
.get_location().get_raw_obj(this), &ref
);
8110 bool versioned_dest
= dest_bucket_info
.versioning_enabled();
8112 if (version_id
&& !version_id
->empty()) {
8113 versioned_dest
= true;
8114 dest_obj
.key
.set_instance(*version_id
);
8115 } else if (versioned_dest
) {
8116 gen_rand_obj_instance_name(&dest_obj
);
8119 bufferlist first_chunk
;
8121 bool copy_itself
= (dest_obj
== src_obj
);
8122 RGWObjManifest
*pmanifest
;
8123 ldout(cct
, 20) << "dest_obj=" << dest_obj
<< " src_obj=" << src_obj
<< " copy_itself=" << (int)copy_itself
<< dendl
;
8125 RGWRados::Object
dest_op_target(this, dest_bucket_info
, obj_ctx
, dest_obj
);
8126 RGWRados::Object::Write
write_op(&dest_op_target
);
8135 append_rand_alpha(cct
, tag
, tag
, 32);
8139 attrs
.erase(RGW_ATTR_TAIL_TAG
);
8140 manifest
= astate
->manifest
;
8141 const rgw_bucket_placement
& tail_placement
= manifest
.get_tail_placement();
8142 if (tail_placement
.bucket
.name
.empty()) {
8143 manifest
.set_tail_placement(tail_placement
.placement_rule
, src_obj
.bucket
);
8145 for (; miter
!= astate
->manifest
.obj_end(); ++miter
) {
8146 ObjectWriteOperation op
;
8147 cls_refcount_get(op
, tag
, true);
8148 const rgw_raw_obj
& loc
= miter
.get_location().get_raw_obj(this);
8149 ref
.ioctx
.locator_set_key(loc
.loc
);
8151 ret
= ref
.ioctx
.operate(loc
.oid
, &op
);
8156 ref_objs
.push_back(loc
);
8159 pmanifest
= &manifest
;
8161 pmanifest
= &astate
->manifest
;
8162 /* don't send the object's tail for garbage collection */
8163 astate
->keep_tail
= true;
8167 ret
= read_op
.read(0, max_chunk_size
, first_chunk
);
8172 pmanifest
->set_head(dest_bucket_info
.placement_rule
, dest_obj
, first_chunk
.length());
8174 pmanifest
->set_head(dest_bucket_info
.placement_rule
, dest_obj
, 0);
8177 write_op
.meta
.data
= &first_chunk
;
8178 write_op
.meta
.manifest
= pmanifest
;
8179 write_op
.meta
.ptag
= &tag
;
8180 write_op
.meta
.owner
= dest_bucket_info
.owner
;
8181 write_op
.meta
.mtime
= mtime
;
8182 write_op
.meta
.flags
= PUT_OBJ_CREATE
;
8183 write_op
.meta
.category
= category
;
8184 write_op
.meta
.olh_epoch
= olh_epoch
;
8185 write_op
.meta
.delete_at
= delete_at
;
8186 write_op
.meta
.modify_tail
= !copy_itself
;
8188 ret
= write_op
.write_meta(obj_size
, astate
->accounted_size
, attrs
);
8197 vector
<rgw_raw_obj
>::iterator riter
;
8199 /* rollback reference */
8200 for (riter
= ref_objs
.begin(); riter
!= ref_objs
.end(); ++riter
) {
8201 ObjectWriteOperation op
;
8202 cls_refcount_put(op
, tag
, true);
8204 ref
.ioctx
.locator_set_key(riter
->loc
);
8206 int r
= ref
.ioctx
.operate(riter
->oid
, &op
);
8208 ldout(cct
, 0) << "ERROR: cleanup after error failed to drop reference on obj=" << *riter
<< dendl
;
8216 int RGWRados::copy_obj_data(RGWObjectCtx
& obj_ctx
,
8217 RGWBucketInfo
& dest_bucket_info
,
8218 RGWRados::Object::Read
& read_op
, off_t end
,
8221 uint64_t max_chunk_size
,
8223 real_time set_mtime
,
8224 map
<string
, bufferlist
>& attrs
,
8225 RGWObjCategory category
,
8227 real_time delete_at
,
8230 ceph::buffer::list
*petag
)
8232 bufferlist first_chunk
;
8233 RGWObjManifest manifest
;
8236 append_rand_alpha(cct
, tag
, tag
, 32);
8238 RGWPutObjProcessor_Atomic
processor(obj_ctx
,
8239 dest_bucket_info
, dest_obj
.bucket
, dest_obj
.get_oid(),
8240 cct
->_conf
->rgw_obj_stripe_size
, tag
, dest_bucket_info
.versioning_enabled());
8242 processor
.set_version_id(*version_id
);
8244 processor
.set_olh_epoch(olh_epoch
);
8245 int ret
= processor
.prepare(this, NULL
);
8253 ret
= read_op
.read(ofs
, end
, bl
);
8255 uint64_t read_len
= ret
;
8262 ret
= processor
.handle_data(bl
, ofs
, &handle
, &obj
, &again
);
8266 ret
= processor
.throttle_data(handle
, obj
, read_len
, false);
8272 } while (ofs
<= end
);
8275 auto iter
= attrs
.find(RGW_ATTR_ETAG
);
8276 if (iter
!= attrs
.end()) {
8277 bufferlist
& bl
= iter
->second
;
8278 etag
= string(bl
.c_str(), bl
.length());
8284 uint64_t accounted_size
;
8286 bool compressed
{false};
8287 RGWCompressionInfo cs_info
;
8288 ret
= rgw_compression_info_from_attrset(attrs
, compressed
, cs_info
);
8290 ldout(cct
, 0) << "ERROR: failed to read compression info" << dendl
;
8293 // pass original size if compressed
8294 accounted_size
= compressed
? cs_info
.orig_size
: ofs
;
8297 return processor
.complete(accounted_size
, etag
, mtime
, set_mtime
, attrs
, delete_at
);
8300 bool RGWRados::is_meta_master()
8302 if (!get_zonegroup().is_master_zonegroup()) {
8306 return (get_zonegroup().master_zone
== zone_public_config
.id
);
8310 * Check to see if the bucket metadata could be synced
8311 * bucket: the bucket to check
8312 * Returns false is the bucket is not synced
8314 bool RGWRados::is_syncing_bucket_meta(const rgw_bucket
& bucket
)
8317 /* no current period */
8318 if (current_period
.get_id().empty()) {
8322 /* zonegroup is not master zonegroup */
8323 if (!get_zonegroup().is_master_zonegroup()) {
8327 /* single zonegroup and a single zone */
8328 if (current_period
.is_single_zonegroup() && get_zonegroup().zones
.size() == 1) {
8332 /* zone is not master */
8333 if (get_zonegroup().master_zone
.compare(zone_public_config
.id
) != 0) {
8340 int RGWRados::check_bucket_empty(RGWBucketInfo
& bucket_info
)
8342 std::map
<string
, rgw_bucket_dir_entry
> ent_map
;
8343 rgw_obj_index_key marker
;
8348 #define NUM_ENTRIES 1000
8349 int r
= cls_bucket_list(bucket_info
, RGW_NO_SHARD
, marker
, prefix
, NUM_ENTRIES
, true, ent_map
,
8350 &is_truncated
, &marker
);
8355 std::map
<string
, rgw_bucket_dir_entry
>::iterator eiter
;
8356 for (eiter
= ent_map
.begin(); eiter
!= ent_map
.end(); ++eiter
) {
8359 if (rgw_obj_key::oid_to_key_in_ns(eiter
->second
.key
.name
, &obj
, ns
))
8362 } while (is_truncated
);
8368 * bucket: the name of the bucket to delete
8369 * Returns 0 on success, -ERR# otherwise.
8371 int RGWRados::delete_bucket(RGWBucketInfo
& bucket_info
, RGWObjVersionTracker
& objv_tracker
, bool check_empty
)
8373 const rgw_bucket
& bucket
= bucket_info
.bucket
;
8374 librados::IoCtx index_ctx
;
8375 map
<int, string
> bucket_objs
;
8376 int r
= open_bucket_index(bucket_info
, index_ctx
, bucket_objs
);
8381 r
= check_bucket_empty(bucket_info
);
8387 r
= rgw_bucket_delete_bucket_obj(this, bucket
.tenant
, bucket
.name
, objv_tracker
);
8391 /* if the bucket is not synced we can remove the meta file */
8392 if (!is_syncing_bucket_meta(bucket
)) {
8393 RGWObjVersionTracker objv_tracker
;
8394 string entry
= bucket
.get_key();
8395 r
= rgw_bucket_instance_remove_entry(this, entry
, &objv_tracker
);
8399 /* remove bucket index objects*/
8400 map
<int, string
>::const_iterator biter
;
8401 for (biter
= bucket_objs
.begin(); biter
!= bucket_objs
.end(); ++biter
) {
8402 index_ctx
.remove(biter
->second
);
8408 int RGWRados::set_bucket_owner(rgw_bucket
& bucket
, ACLOwner
& owner
)
8411 map
<string
, bufferlist
> attrs
;
8412 RGWObjectCtx
obj_ctx(this);
8414 if (bucket
.bucket_id
.empty()) {
8415 r
= get_bucket_info(obj_ctx
, bucket
.tenant
, bucket
.name
, info
, NULL
, &attrs
);
8417 r
= get_bucket_instance_info(obj_ctx
, bucket
, info
, nullptr, &attrs
);
8420 ldout(cct
, 0) << "NOTICE: get_bucket_info on bucket=" << bucket
.name
<< " returned err=" << r
<< dendl
;
8424 info
.owner
= owner
.get_id();
8426 r
= put_bucket_instance_info(info
, false, real_time(), &attrs
);
8428 ldout(cct
, 0) << "NOTICE: put_bucket_info on bucket=" << bucket
.name
<< " returned err=" << r
<< dendl
;
8436 int RGWRados::set_buckets_enabled(vector
<rgw_bucket
>& buckets
, bool enabled
)
8440 vector
<rgw_bucket
>::iterator iter
;
8442 for (iter
= buckets
.begin(); iter
!= buckets
.end(); ++iter
) {
8443 rgw_bucket
& bucket
= *iter
;
8445 ldout(cct
, 20) << "enabling bucket name=" << bucket
.name
<< dendl
;
8447 ldout(cct
, 20) << "disabling bucket name=" << bucket
.name
<< dendl
;
8450 map
<string
, bufferlist
> attrs
;
8451 RGWObjectCtx
obj_ctx(this);
8452 int r
= get_bucket_info(obj_ctx
, bucket
.tenant
, bucket
.name
, info
, NULL
, &attrs
);
8454 ldout(cct
, 0) << "NOTICE: get_bucket_info on bucket=" << bucket
.name
<< " returned err=" << r
<< ", skipping bucket" << dendl
;
8459 info
.flags
&= ~BUCKET_SUSPENDED
;
8461 info
.flags
|= BUCKET_SUSPENDED
;
8464 r
= put_bucket_instance_info(info
, false, real_time(), &attrs
);
8466 ldout(cct
, 0) << "NOTICE: put_bucket_info on bucket=" << bucket
.name
<< " returned err=" << r
<< ", skipping bucket" << dendl
;
8474 int RGWRados::bucket_suspended(rgw_bucket
& bucket
, bool *suspended
)
8476 RGWBucketInfo bucket_info
;
8477 RGWObjectCtx
obj_ctx(this);
8478 int ret
= get_bucket_info(obj_ctx
, bucket
.tenant
, bucket
.name
, bucket_info
, NULL
);
8483 *suspended
= ((bucket_info
.flags
& BUCKET_SUSPENDED
) != 0);
8487 int RGWRados::Object::complete_atomic_modification()
8489 if (!state
->has_manifest
|| state
->keep_tail
)
8492 cls_rgw_obj_chain chain
;
8493 store
->update_gc_chain(obj
, state
->manifest
, &chain
);
8495 if (chain
.empty()) {
8499 string tag
= (state
->tail_tag
.length() > 0 ? state
->tail_tag
.to_str() : state
->obj_tag
.to_str());
8500 return store
->gc
->send_chain(chain
, tag
, false); // do it async
8503 void RGWRados::update_gc_chain(rgw_obj
& head_obj
, RGWObjManifest
& manifest
, cls_rgw_obj_chain
*chain
)
8505 RGWObjManifest::obj_iterator iter
;
8506 rgw_raw_obj raw_head
;
8507 obj_to_raw(manifest
.get_head_placement_rule(), head_obj
, &raw_head
);
8508 for (iter
= manifest
.obj_begin(); iter
!= manifest
.obj_end(); ++iter
) {
8509 const rgw_raw_obj
& mobj
= iter
.get_location().get_raw_obj(this);
8510 if (mobj
== raw_head
)
8512 cls_rgw_obj_key
key(mobj
.oid
);
8513 chain
->push_obj(mobj
.pool
.to_str(), key
, mobj
.loc
);
8517 int RGWRados::send_chain_to_gc(cls_rgw_obj_chain
& chain
, const string
& tag
, bool sync
)
8519 return gc
->send_chain(chain
, tag
, sync
);
8522 int RGWRados::open_bucket_index(const RGWBucketInfo
& bucket_info
, librados::IoCtx
& index_ctx
, string
& bucket_oid
)
8524 const rgw_bucket
& bucket
= bucket_info
.bucket
;
8525 int r
= open_bucket_index_ctx(bucket_info
, index_ctx
);
8529 if (bucket
.bucket_id
.empty()) {
8530 ldout(cct
, 0) << "ERROR: empty bucket id for bucket operation" << dendl
;
8534 bucket_oid
= dir_oid_prefix
;
8535 bucket_oid
.append(bucket
.bucket_id
);
8540 int RGWRados::open_bucket_index_base(const RGWBucketInfo
& bucket_info
, librados::IoCtx
& index_ctx
,
8541 string
& bucket_oid_base
) {
8542 const rgw_bucket
& bucket
= bucket_info
.bucket
;
8543 int r
= open_bucket_index_ctx(bucket_info
, index_ctx
);
8547 if (bucket
.bucket_id
.empty()) {
8548 ldout(cct
, 0) << "ERROR: empty bucket_id for bucket operation" << dendl
;
8552 bucket_oid_base
= dir_oid_prefix
;
8553 bucket_oid_base
.append(bucket
.bucket_id
);
8559 int RGWRados::open_bucket_index(const RGWBucketInfo
& bucket_info
, librados::IoCtx
& index_ctx
,
8560 map
<int, string
>& bucket_objs
, int shard_id
, map
<int, string
> *bucket_instance_ids
) {
8561 string bucket_oid_base
;
8562 int ret
= open_bucket_index_base(bucket_info
, index_ctx
, bucket_oid_base
);
8567 get_bucket_index_objects(bucket_oid_base
, bucket_info
.num_shards
, bucket_objs
, shard_id
);
8568 if (bucket_instance_ids
) {
8569 get_bucket_instance_ids(bucket_info
, shard_id
, bucket_instance_ids
);
8574 template<typename T
>
8575 int RGWRados::open_bucket_index(const RGWBucketInfo
& bucket_info
, librados::IoCtx
& index_ctx
,
8576 map
<int, string
>& oids
, map
<int, T
>& bucket_objs
,
8577 int shard_id
, map
<int, string
> *bucket_instance_ids
)
8579 int ret
= open_bucket_index(bucket_info
, index_ctx
, oids
, shard_id
, bucket_instance_ids
);
8583 map
<int, string
>::const_iterator iter
= oids
.begin();
8584 for (; iter
!= oids
.end(); ++iter
) {
8585 bucket_objs
[iter
->first
] = T();
8590 int RGWRados::open_bucket_index_shard(const RGWBucketInfo
& bucket_info
, librados::IoCtx
& index_ctx
,
8591 const string
& obj_key
, string
*bucket_obj
, int *shard_id
)
8593 string bucket_oid_base
;
8594 int ret
= open_bucket_index_base(bucket_info
, index_ctx
, bucket_oid_base
);
8598 RGWObjectCtx
obj_ctx(this);
8600 ret
= get_bucket_index_object(bucket_oid_base
, obj_key
, bucket_info
.num_shards
,
8601 (RGWBucketInfo::BIShardsHashType
)bucket_info
.bucket_index_shard_hash_type
, bucket_obj
, shard_id
);
8603 ldout(cct
, 10) << "get_bucket_index_object() returned ret=" << ret
<< dendl
;
8609 int RGWRados::open_bucket_index_shard(const RGWBucketInfo
& bucket_info
, librados::IoCtx
& index_ctx
,
8610 int shard_id
, string
*bucket_obj
)
8612 string bucket_oid_base
;
8613 int ret
= open_bucket_index_base(bucket_info
, index_ctx
, bucket_oid_base
);
8617 RGWObjectCtx
obj_ctx(this);
8619 get_bucket_index_object(bucket_oid_base
, bucket_info
.num_shards
,
8620 shard_id
, bucket_obj
);
8624 static void accumulate_raw_stats(const rgw_bucket_dir_header
& header
,
8625 map
<RGWObjCategory
, RGWStorageStats
>& stats
)
8627 for (const auto& pair
: header
.stats
) {
8628 const RGWObjCategory category
= static_cast<RGWObjCategory
>(pair
.first
);
8629 const rgw_bucket_category_stats
& header_stats
= pair
.second
;
8631 RGWStorageStats
& s
= stats
[category
];
8633 s
.category
= category
;
8634 s
.size
+= header_stats
.total_size
;
8635 s
.size_rounded
+= header_stats
.total_size_rounded
;
8636 s
.size_utilized
+= header_stats
.actual_size
;
8637 s
.num_objects
+= header_stats
.num_entries
;
8641 int RGWRados::bucket_check_index(RGWBucketInfo
& bucket_info
,
8642 map
<RGWObjCategory
, RGWStorageStats
> *existing_stats
,
8643 map
<RGWObjCategory
, RGWStorageStats
> *calculated_stats
)
8645 librados::IoCtx index_ctx
;
8646 // key - bucket index object id
8647 // value - bucket index check OP returned result with the given bucket index object (shard)
8648 map
<int, string
> oids
;
8649 map
<int, struct rgw_cls_check_index_ret
> bucket_objs_ret
;
8651 int ret
= open_bucket_index(bucket_info
, index_ctx
, oids
, bucket_objs_ret
);
8656 ret
= CLSRGWIssueBucketCheck(index_ctx
, oids
, bucket_objs_ret
, cct
->_conf
->rgw_bucket_index_max_aio
)();
8661 // Aggregate results (from different shards if there is any)
8662 map
<int, struct rgw_cls_check_index_ret
>::iterator iter
;
8663 for (iter
= bucket_objs_ret
.begin(); iter
!= bucket_objs_ret
.end(); ++iter
) {
8664 accumulate_raw_stats(iter
->second
.existing_header
, *existing_stats
);
8665 accumulate_raw_stats(iter
->second
.calculated_header
, *calculated_stats
);
8671 int RGWRados::bucket_rebuild_index(RGWBucketInfo
& bucket_info
)
8673 librados::IoCtx index_ctx
;
8674 map
<int, string
> bucket_objs
;
8676 int r
= open_bucket_index(bucket_info
, index_ctx
, bucket_objs
);
8681 return CLSRGWIssueBucketRebuild(index_ctx
, bucket_objs
, cct
->_conf
->rgw_bucket_index_max_aio
)();
8684 int RGWRados::bucket_set_reshard(RGWBucketInfo
& bucket_info
, const cls_rgw_bucket_instance_entry
& entry
)
8686 librados::IoCtx index_ctx
;
8687 map
<int, string
> bucket_objs
;
8689 int r
= open_bucket_index(bucket_info
, index_ctx
, bucket_objs
);
8694 return CLSRGWIssueSetBucketResharding(index_ctx
, bucket_objs
, entry
, cct
->_conf
->rgw_bucket_index_max_aio
)();
8697 int RGWRados::defer_gc(void *ctx
, const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
)
8699 RGWObjectCtx
*rctx
= static_cast<RGWObjectCtx
*>(ctx
);
8700 std::string oid
, key
;
8701 get_obj_bucket_and_oid_loc(obj
, oid
, key
);
8705 RGWObjState
*state
= NULL
;
8707 int r
= get_obj_state(rctx
, bucket_info
, obj
, &state
, false);
8711 if (!state
->is_atomic
) {
8712 ldout(cct
, 20) << "state for obj=" << obj
<< " is not atomic, not deferring gc operation" << dendl
;
8718 if (state
->tail_tag
.length() > 0) {
8719 tag
= state
->tail_tag
.c_str();
8720 } else if (state
->obj_tag
.length() > 0) {
8721 tag
= state
->obj_tag
.c_str();
8723 ldout(cct
, 20) << "state->obj_tag is empty, not deferring gc operation" << dendl
;
8727 ldout(cct
, 0) << "defer chain tag=" << tag
<< dendl
;
8729 return gc
->defer_chain(tag
, false);
8732 void RGWRados::remove_rgw_head_obj(ObjectWriteOperation
& op
)
8734 list
<string
> prefixes
;
8735 prefixes
.push_back(RGW_ATTR_OLH_PREFIX
);
8736 cls_rgw_remove_obj(op
, prefixes
);
8739 void RGWRados::cls_obj_check_prefix_exist(ObjectOperation
& op
, const string
& prefix
, bool fail_if_exist
)
8741 cls_rgw_obj_check_attrs_prefix(op
, prefix
, fail_if_exist
);
8744 void RGWRados::cls_obj_check_mtime(ObjectOperation
& op
, const real_time
& mtime
, bool high_precision_time
, RGWCheckMTimeType type
)
8746 cls_rgw_obj_check_mtime(op
, mtime
, high_precision_time
, type
);
8752 * bucket: name of the bucket storing the object
8753 * obj: name of the object to delete
8754 * Returns: 0 on success, -ERR# otherwise.
8756 int RGWRados::Object::Delete::delete_obj()
8758 RGWRados
*store
= target
->get_store();
8759 rgw_obj
& src_obj
= target
->get_obj();
8760 const string
& instance
= src_obj
.key
.instance
;
8761 rgw_obj obj
= src_obj
;
8763 if (instance
== "null") {
8764 obj
.key
.instance
.clear();
8767 bool explicit_marker_version
= (!params
.marker_version_id
.empty());
8769 if (params
.versioning_status
& BUCKET_VERSIONED
|| explicit_marker_version
) {
8770 if (instance
.empty() || explicit_marker_version
) {
8771 rgw_obj marker
= obj
;
8773 if (!params
.marker_version_id
.empty()) {
8774 if (params
.marker_version_id
!= "null") {
8775 marker
.key
.set_instance(params
.marker_version_id
);
8777 } else if ((params
.versioning_status
& BUCKET_VERSIONS_SUSPENDED
) == 0) {
8778 store
->gen_rand_obj_instance_name(&marker
);
8781 result
.version_id
= marker
.key
.instance
;
8782 result
.delete_marker
= true;
8784 struct rgw_bucket_dir_entry_meta meta
;
8786 meta
.owner
= params
.obj_owner
.get_id().to_str();
8787 meta
.owner_display_name
= params
.obj_owner
.get_display_name();
8789 if (real_clock::is_zero(params
.mtime
)) {
8790 meta
.mtime
= real_clock::now();
8792 meta
.mtime
= params
.mtime
;
8795 int r
= store
->set_olh(target
->get_ctx(), target
->get_bucket_info(), marker
, true, &meta
, params
.olh_epoch
, params
.unmod_since
, params
.high_precision_time
, params
.zones_trace
);
8800 rgw_bucket_dir_entry dirent
;
8802 int r
= store
->bi_get_instance(target
->get_bucket_info(), obj
, &dirent
);
8806 result
.delete_marker
= dirent
.is_delete_marker();
8807 r
= store
->unlink_obj_instance(target
->get_ctx(), target
->get_bucket_info(), obj
, params
.olh_epoch
, params
.zones_trace
);
8811 result
.version_id
= instance
;
8815 int r
= target
->get_bucket_shard(&bs
);
8817 ldout(store
->ctx(), 5) << "failed to get BucketShard object: r=" << r
<< dendl
;
8821 if (target
->bucket_info
.datasync_flag_enabled()) {
8822 r
= store
->data_log
->add_entry(bs
->bucket
, bs
->shard_id
);
8824 lderr(store
->ctx()) << "ERROR: failed writing data log" << dendl
;
8833 int r
= store
->get_obj_head_ref(target
->get_bucket_info(), obj
, &ref
);
8839 r
= target
->get_state(&state
, false);
8843 ObjectWriteOperation op
;
8845 if (!real_clock::is_zero(params
.unmod_since
)) {
8846 struct timespec ctime
= ceph::real_clock::to_timespec(state
->mtime
);
8847 struct timespec unmod
= ceph::real_clock::to_timespec(params
.unmod_since
);
8848 if (!params
.high_precision_time
) {
8853 ldout(store
->ctx(), 10) << "If-UnModified-Since: " << params
.unmod_since
<< " Last-Modified: " << ctime
<< dendl
;
8854 if (ctime
> unmod
) {
8855 return -ERR_PRECONDITION_FAILED
;
8858 /* only delete object if mtime is less than or equal to params.unmod_since */
8859 store
->cls_obj_check_mtime(op
, params
.unmod_since
, params
.high_precision_time
, CLS_RGW_CHECK_TIME_MTIME_LE
);
8861 uint64_t obj_size
= state
->size
;
8863 if (!real_clock::is_zero(params
.expiration_time
)) {
8865 real_time delete_at
;
8867 if (state
->get_attr(RGW_ATTR_DELETE_AT
, bl
)) {
8869 bufferlist::iterator iter
= bl
.begin();
8870 ::decode(delete_at
, iter
);
8871 } catch (buffer::error
& err
) {
8872 ldout(store
->ctx(), 0) << "ERROR: couldn't decode RGW_ATTR_DELETE_AT" << dendl
;
8876 if (params
.expiration_time
!= delete_at
) {
8877 return -ERR_PRECONDITION_FAILED
;
8880 return -ERR_PRECONDITION_FAILED
;
8884 if (!state
->exists
) {
8885 target
->invalidate_state();
8889 r
= target
->prepare_atomic_modification(op
, false, NULL
, NULL
, NULL
, true, false);
8893 RGWBucketInfo
& bucket_info
= target
->get_bucket_info();
8895 RGWRados::Bucket
bop(store
, bucket_info
);
8896 RGWRados::Bucket::UpdateIndex
index_op(&bop
, obj
);
8898 index_op
.set_zones_trace(params
.zones_trace
);
8899 index_op
.set_bilog_flags(params
.bilog_flags
);
8902 r
= index_op
.prepare(CLS_RGW_OP_DEL
, &state
->write_tag
);
8906 store
->remove_rgw_head_obj(op
);
8907 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
8908 bool need_invalidate
= false;
8909 if (r
== -ECANCELED
) {
8910 /* raced with another operation, we can regard it as removed */
8911 need_invalidate
= true;
8915 int64_t poolid
= ref
.ioctx
.get_id();
8917 tombstone_cache_t
*obj_tombstone_cache
= store
->get_tombstone_cache();
8918 if (obj_tombstone_cache
) {
8919 tombstone_entry entry
{*state
};
8920 obj_tombstone_cache
->add(obj
, entry
);
8922 r
= index_op
.complete_del(poolid
, ref
.ioctx
.get_last_version(), state
->mtime
, params
.remove_objs
);
8924 int ret
= target
->complete_atomic_modification();
8926 ldout(store
->ctx(), 0) << "ERROR: complete_atomic_modification returned ret=" << ret
<< dendl
;
8928 /* other than that, no need to propagate error */
8930 int ret
= index_op
.cancel();
8932 ldout(store
->ctx(), 0) << "ERROR: index_op.cancel() returned ret=" << ret
<< dendl
;
8936 if (need_invalidate
) {
8937 target
->invalidate_state();
8943 /* update quota cache */
8944 store
->quota_handler
->update_stats(params
.bucket_owner
, obj
.bucket
, -1, 0, obj_size
);
8949 int RGWRados::delete_obj(RGWObjectCtx
& obj_ctx
,
8950 const RGWBucketInfo
& bucket_info
,
8952 int versioning_status
,
8953 uint16_t bilog_flags
,
8954 const real_time
& expiration_time
,
8955 rgw_zone_set
*zones_trace
)
8957 RGWRados::Object
del_target(this, bucket_info
, obj_ctx
, obj
);
8958 RGWRados::Object::Delete
del_op(&del_target
);
8960 del_op
.params
.bucket_owner
= bucket_info
.owner
;
8961 del_op
.params
.versioning_status
= versioning_status
;
8962 del_op
.params
.bilog_flags
= bilog_flags
;
8963 del_op
.params
.expiration_time
= expiration_time
;
8964 del_op
.params
.zones_trace
= zones_trace
;
8966 return del_op
.delete_obj();
8969 int RGWRados::delete_raw_obj(const rgw_raw_obj
& obj
)
8972 int r
= get_raw_obj_ref(obj
, &ref
);
8977 ObjectWriteOperation op
;
8980 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
8987 int RGWRados::delete_system_obj(rgw_raw_obj
& obj
, RGWObjVersionTracker
*objv_tracker
)
8990 ldout(cct
, 1) << "delete_system_obj got empty object name "
8991 << obj
<< ", returning EINVAL" << dendl
;
8995 int r
= get_raw_obj_ref(obj
, &ref
);
9000 ObjectWriteOperation op
;
9003 objv_tracker
->prepare_op_for_write(&op
);
9007 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
9014 int RGWRados::delete_obj_index(const rgw_obj
& obj
)
9016 std::string oid
, key
;
9017 get_obj_bucket_and_oid_loc(obj
, oid
, key
);
9019 RGWObjectCtx
obj_ctx(this);
9021 RGWBucketInfo bucket_info
;
9022 int ret
= get_bucket_instance_info(obj_ctx
, obj
.bucket
, bucket_info
, NULL
, NULL
);
9024 ldout(cct
, 0) << "ERROR: " << __func__
<< "() get_bucket_instance_info(bucket=" << obj
.bucket
<< ") returned ret=" << ret
<< dendl
;
9028 RGWRados::Bucket
bop(this, bucket_info
);
9029 RGWRados::Bucket::UpdateIndex
index_op(&bop
, obj
);
9031 real_time removed_mtime
;
9032 int r
= index_op
.complete_del(-1 /* pool */, 0, removed_mtime
, NULL
);
9037 static void generate_fake_tag(RGWRados
*store
, map
<string
, bufferlist
>& attrset
, RGWObjManifest
& manifest
, bufferlist
& manifest_bl
, bufferlist
& tag_bl
)
9041 RGWObjManifest::obj_iterator mi
= manifest
.obj_begin();
9042 if (mi
!= manifest
.obj_end()) {
9043 if (manifest
.has_tail()) // first object usually points at the head, let's skip to a more unique part
9045 tag
= mi
.get_location().get_raw_obj(store
).oid
;
9049 unsigned char md5
[CEPH_CRYPTO_MD5_DIGESTSIZE
];
9050 char md5_str
[CEPH_CRYPTO_MD5_DIGESTSIZE
* 2 + 1];
9052 hash
.Update((const byte
*)manifest_bl
.c_str(), manifest_bl
.length());
9054 map
<string
, bufferlist
>::iterator iter
= attrset
.find(RGW_ATTR_ETAG
);
9055 if (iter
!= attrset
.end()) {
9056 bufferlist
& bl
= iter
->second
;
9057 hash
.Update((const byte
*)bl
.c_str(), bl
.length());
9061 buf_to_hex(md5
, CEPH_CRYPTO_MD5_DIGESTSIZE
, md5_str
);
9062 tag
.append(md5_str
);
9064 ldout(store
->ctx(), 10) << "generate_fake_tag new tag=" << tag
<< dendl
;
9066 tag_bl
.append(tag
.c_str(), tag
.size() + 1);
9069 static bool is_olh(map
<string
, bufferlist
>& attrs
)
9071 map
<string
, bufferlist
>::iterator iter
= attrs
.find(RGW_ATTR_OLH_INFO
);
9072 return (iter
!= attrs
.end());
9075 static bool has_olh_tag(map
<string
, bufferlist
>& attrs
)
9077 map
<string
, bufferlist
>::iterator iter
= attrs
.find(RGW_ATTR_OLH_ID_TAG
);
9078 return (iter
!= attrs
.end());
9081 int RGWRados::get_olh_target_state(RGWObjectCtx
& obj_ctx
, const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
,
9082 RGWObjState
*olh_state
, RGWObjState
**target_state
)
9084 assert(olh_state
->is_olh
);
9087 int r
= RGWRados::follow_olh(bucket_info
, obj_ctx
, olh_state
, obj
, &target
); /* might return -EAGAIN */
9091 r
= get_obj_state(&obj_ctx
, bucket_info
, target
, target_state
, false);
9099 int RGWRados::get_system_obj_state_impl(RGWObjectCtx
*rctx
, rgw_raw_obj
& obj
, RGWRawObjState
**state
, RGWObjVersionTracker
*objv_tracker
)
9105 RGWRawObjState
*s
= rctx
->raw
.get_state(obj
);
9106 ldout(cct
, 20) << "get_system_obj_state: rctx=" << (void *)rctx
<< " obj=" << obj
<< " state=" << (void *)s
<< " s->prefetch_data=" << s
->prefetch_data
<< dendl
;
9114 int r
= raw_obj_stat(obj
, &s
->size
, &s
->mtime
, &s
->epoch
, &s
->attrset
, (s
->prefetch_data
? &s
->data
: NULL
), objv_tracker
);
9117 s
->has_attrs
= true;
9118 s
->mtime
= real_time();
9125 s
->has_attrs
= true;
9126 s
->obj_tag
= s
->attrset
[RGW_ATTR_ID_TAG
];
9128 if (s
->obj_tag
.length())
9129 ldout(cct
, 20) << "get_system_obj_state: setting s->obj_tag to "
9130 << s
->obj_tag
.c_str() << dendl
;
9132 ldout(cct
, 20) << "get_system_obj_state: s->obj_tag was set empty" << dendl
;
9137 int RGWRados::get_system_obj_state(RGWObjectCtx
*rctx
, rgw_raw_obj
& obj
, RGWRawObjState
**state
, RGWObjVersionTracker
*objv_tracker
)
9142 ret
= get_system_obj_state_impl(rctx
, obj
, state
, objv_tracker
);
9143 } while (ret
== -EAGAIN
);
9148 int RGWRados::get_obj_state_impl(RGWObjectCtx
*rctx
, const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
,
9149 RGWObjState
**state
, bool follow_olh
, bool assume_noent
)
9155 bool need_follow_olh
= follow_olh
&& obj
.key
.instance
.empty();
9157 RGWObjState
*s
= rctx
->obj
.get_state(obj
);
9158 ldout(cct
, 20) << "get_obj_state: rctx=" << (void *)rctx
<< " obj=" << obj
<< " state=" << (void *)s
<< " s->prefetch_data=" << s
->prefetch_data
<< dendl
;
9161 if (s
->is_olh
&& need_follow_olh
) {
9162 return get_olh_target_state(*rctx
, bucket_info
, obj
, s
, state
);
9169 rgw_raw_obj raw_obj
;
9170 obj_to_raw(bucket_info
.placement_rule
, obj
, &raw_obj
);
9174 if (!assume_noent
) {
9175 r
= RGWRados::raw_obj_stat(raw_obj
, &s
->size
, &s
->mtime
, &s
->epoch
, &s
->attrset
, (s
->prefetch_data
? &s
->data
: NULL
), NULL
);
9180 s
->has_attrs
= true;
9181 tombstone_entry entry
;
9182 if (obj_tombstone_cache
&& obj_tombstone_cache
->find(obj
, entry
)) {
9183 s
->mtime
= entry
.mtime
;
9184 s
->zone_short_id
= entry
.zone_short_id
;
9185 s
->pg_ver
= entry
.pg_ver
;
9186 ldout(cct
, 20) << __func__
<< "(): found obj in tombstone cache: obj=" << obj
9187 << " mtime=" << s
->mtime
<< " pgv=" << s
->pg_ver
<< dendl
;
9189 s
->mtime
= real_time();
9197 s
->has_attrs
= true;
9198 s
->accounted_size
= s
->size
;
9200 auto iter
= s
->attrset
.find(RGW_ATTR_COMPRESSION
);
9201 const bool compressed
= (iter
!= s
->attrset
.end());
9203 // use uncompressed size for accounted_size
9205 RGWCompressionInfo info
;
9206 auto p
= iter
->second
.begin();
9208 s
->accounted_size
= info
.orig_size
;
9209 } catch (buffer::error
&) {
9210 dout(0) << "ERROR: could not decode compression info for object: " << obj
<< dendl
;
9215 iter
= s
->attrset
.find(RGW_ATTR_SHADOW_OBJ
);
9216 if (iter
!= s
->attrset
.end()) {
9217 bufferlist bl
= iter
->second
;
9218 bufferlist::iterator it
= bl
.begin();
9219 it
.copy(bl
.length(), s
->shadow_obj
);
9220 s
->shadow_obj
[bl
.length()] = '\0';
9222 s
->obj_tag
= s
->attrset
[RGW_ATTR_ID_TAG
];
9223 auto ttiter
= s
->attrset
.find(RGW_ATTR_TAIL_TAG
);
9224 if (ttiter
!= s
->attrset
.end()) {
9225 s
->tail_tag
= s
->attrset
[RGW_ATTR_TAIL_TAG
];
9228 bufferlist manifest_bl
= s
->attrset
[RGW_ATTR_MANIFEST
];
9229 if (manifest_bl
.length()) {
9230 bufferlist::iterator miter
= manifest_bl
.begin();
9232 ::decode(s
->manifest
, miter
);
9233 s
->has_manifest
= true;
9234 s
->manifest
.set_head(bucket_info
.placement_rule
, obj
, s
->size
); /* patch manifest to reflect the head we just read, some manifests might be
9235 broken due to old bugs */
9236 s
->size
= s
->manifest
.get_obj_size();
9238 s
->accounted_size
= s
->size
;
9239 } catch (buffer::error
& err
) {
9240 ldout(cct
, 0) << "ERROR: couldn't decode manifest" << dendl
;
9243 ldout(cct
, 10) << "manifest: total_size = " << s
->manifest
.get_obj_size() << dendl
;
9244 if (cct
->_conf
->subsys
.should_gather(ceph_subsys_rgw
, 20) && s
->manifest
.has_explicit_objs()) {
9245 RGWObjManifest::obj_iterator mi
;
9246 for (mi
= s
->manifest
.obj_begin(); mi
!= s
->manifest
.obj_end(); ++mi
) {
9247 ldout(cct
, 20) << "manifest: ofs=" << mi
.get_ofs() << " loc=" << mi
.get_location().get_raw_obj(this) << dendl
;
9251 if (!s
->obj_tag
.length()) {
9253 * Uh oh, something's wrong, object with manifest should have tag. Let's
9254 * create one out of the manifest, would be unique
9256 generate_fake_tag(this, s
->attrset
, s
->manifest
, manifest_bl
, s
->obj_tag
);
9260 map
<string
, bufferlist
>::iterator aiter
= s
->attrset
.find(RGW_ATTR_PG_VER
);
9261 if (aiter
!= s
->attrset
.end()) {
9262 bufferlist
& pg_ver_bl
= aiter
->second
;
9263 if (pg_ver_bl
.length()) {
9264 bufferlist::iterator pgbl
= pg_ver_bl
.begin();
9266 ::decode(s
->pg_ver
, pgbl
);
9267 } catch (buffer::error
& err
) {
9268 ldout(cct
, 0) << "ERROR: couldn't decode pg ver attr for object " << s
->obj
<< ", non-critical error, ignoring" << dendl
;
9272 aiter
= s
->attrset
.find(RGW_ATTR_SOURCE_ZONE
);
9273 if (aiter
!= s
->attrset
.end()) {
9274 bufferlist
& zone_short_id_bl
= aiter
->second
;
9275 if (zone_short_id_bl
.length()) {
9276 bufferlist::iterator zbl
= zone_short_id_bl
.begin();
9278 ::decode(s
->zone_short_id
, zbl
);
9279 } catch (buffer::error
& err
) {
9280 ldout(cct
, 0) << "ERROR: couldn't decode zone short id attr for object " << s
->obj
<< ", non-critical error, ignoring" << dendl
;
9284 if (s
->obj_tag
.length())
9285 ldout(cct
, 20) << "get_obj_state: setting s->obj_tag to " << s
->obj_tag
.c_str() << dendl
;
9287 ldout(cct
, 20) << "get_obj_state: s->obj_tag was set empty" << dendl
;
9289 /* an object might not be olh yet, but could have olh id tag, so we should set it anyway if
9290 * it exist, and not only if is_olh() returns true
9292 iter
= s
->attrset
.find(RGW_ATTR_OLH_ID_TAG
);
9293 if (iter
!= s
->attrset
.end()) {
9294 s
->olh_tag
= iter
->second
;
9297 if (is_olh(s
->attrset
)) {
9300 ldout(cct
, 20) << __func__
<< ": setting s->olh_tag to " << string(s
->olh_tag
.c_str(), s
->olh_tag
.length()) << dendl
;
9302 if (need_follow_olh
) {
9303 return get_olh_target_state(*rctx
, bucket_info
, obj
, s
, state
);
9310 int RGWRados::get_obj_state(RGWObjectCtx
*rctx
, const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
, RGWObjState
**state
,
9311 bool follow_olh
, bool assume_noent
)
9316 ret
= get_obj_state_impl(rctx
, bucket_info
, obj
, state
, follow_olh
, assume_noent
);
9317 } while (ret
== -EAGAIN
);
9322 int RGWRados::Object::get_manifest(RGWObjManifest
**pmanifest
)
9324 RGWObjState
*astate
;
9325 int r
= get_state(&astate
, true);
9330 *pmanifest
= &astate
->manifest
;
9335 int RGWRados::Object::Read::get_attr(const char *name
, bufferlist
& dest
)
9338 int r
= source
->get_state(&state
, true);
9343 if (!state
->get_attr(name
, dest
))
9350 int RGWRados::Object::Stat::stat_async()
9352 RGWObjectCtx
& ctx
= source
->get_ctx();
9353 rgw_obj
& obj
= source
->get_obj();
9354 RGWRados
*store
= source
->get_store();
9356 RGWObjState
*s
= ctx
.obj
.get_state(obj
); /* calling this one directly because otherwise a sync request will be sent */
9360 result
.size
= s
->size
;
9361 result
.mtime
= ceph::real_clock::to_timespec(s
->mtime
);
9362 result
.attrs
= s
->attrset
;
9363 result
.has_manifest
= s
->has_manifest
;
9364 result
.manifest
= s
->manifest
;
9370 get_obj_bucket_and_oid_loc(obj
, oid
, loc
);
9372 int r
= store
->get_obj_head_ioctx(source
->get_bucket_info(), obj
, &state
.io_ctx
);
9377 librados::ObjectReadOperation op
;
9378 op
.stat2(&result
.size
, &result
.mtime
, NULL
);
9379 op
.getxattrs(&result
.attrs
, NULL
);
9380 state
.completion
= librados::Rados::aio_create_completion(NULL
, NULL
, NULL
);
9381 state
.io_ctx
.locator_set_key(loc
);
9382 r
= state
.io_ctx
.aio_operate(oid
, state
.completion
, &op
, NULL
);
9384 ldout(store
->ctx(), 5) << __func__
9385 << ": ERROR: aio_operate() returned ret=" << r
9394 int RGWRados::Object::Stat::wait()
9396 if (!state
.completion
) {
9400 state
.completion
->wait_for_safe();
9401 state
.ret
= state
.completion
->get_return_value();
9402 state
.completion
->release();
9404 if (state
.ret
!= 0) {
9411 int RGWRados::Object::Stat::finish()
9413 map
<string
, bufferlist
>::iterator iter
= result
.attrs
.find(RGW_ATTR_MANIFEST
);
9414 if (iter
!= result
.attrs
.end()) {
9415 bufferlist
& bl
= iter
->second
;
9416 bufferlist::iterator biter
= bl
.begin();
9418 ::decode(result
.manifest
, biter
);
9419 } catch (buffer::error
& err
) {
9420 RGWRados
*store
= source
->get_store();
9421 ldout(store
->ctx(), 0) << "ERROR: " << __func__
<< ": failed to decode manifest" << dendl
;
9424 result
.has_manifest
= true;
9431 * Get an attribute for a system object.
9432 * obj: the object to get attr
9433 * name: name of the attr to retrieve
9434 * dest: bufferlist to store the result in
9435 * Returns: 0 on success, -ERR# otherwise.
9437 int RGWRados::system_obj_get_attr(rgw_raw_obj
& obj
, const char *name
, bufferlist
& dest
)
9440 int r
= get_system_obj_ref(obj
, &ref
);
9445 ObjectReadOperation op
;
9448 op
.getxattr(name
, &dest
, &rval
);
9450 r
= ref
.ioctx
.operate(ref
.oid
, &op
, NULL
);
9457 int RGWRados::append_atomic_test(RGWObjectCtx
*rctx
,
9458 const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
,
9459 ObjectOperation
& op
, RGWObjState
**pstate
)
9464 int r
= get_obj_state(rctx
, bucket_info
, obj
, pstate
, false);
9468 RGWObjState
*state
= *pstate
;
9470 if (!state
->is_atomic
) {
9471 ldout(cct
, 20) << "state for obj=" << obj
<< " is not atomic, not appending atomic test" << dendl
;
9475 if (state
->obj_tag
.length() > 0 && !state
->fake_tag
) {// check for backward compatibility
9476 op
.cmpxattr(RGW_ATTR_ID_TAG
, LIBRADOS_CMPXATTR_OP_EQ
, state
->obj_tag
);
9478 ldout(cct
, 20) << "state->obj_tag is empty, not appending atomic test" << dendl
;
9483 int RGWRados::Object::get_state(RGWObjState
**pstate
, bool follow_olh
, bool assume_noent
)
9485 return store
->get_obj_state(&ctx
, bucket_info
, obj
, pstate
, follow_olh
, assume_noent
);
9488 void RGWRados::Object::invalidate_state()
9490 ctx
.obj
.invalidate(obj
);
9493 void RGWRados::SystemObject::invalidate_state()
9495 ctx
.raw
.invalidate(obj
);
9498 int RGWRados::Object::prepare_atomic_modification(ObjectWriteOperation
& op
, bool reset_obj
, const string
*ptag
,
9499 const char *if_match
, const char *if_nomatch
, bool removal_op
,
9502 int r
= get_state(&state
, false);
9506 bool need_guard
= (state
->has_manifest
|| (state
->obj_tag
.length() != 0) ||
9507 if_match
!= NULL
|| if_nomatch
!= NULL
) &&
9510 if (!state
->is_atomic
) {
9511 ldout(store
->ctx(), 20) << "prepare_atomic_modification: state is not atomic. state=" << (void *)state
<< dendl
;
9515 store
->remove_rgw_head_obj(op
); // we're not dropping reference here, actually removing object
9522 /* first verify that the object wasn't replaced under */
9523 if (if_nomatch
== NULL
|| strcmp(if_nomatch
, "*") != 0) {
9524 op
.cmpxattr(RGW_ATTR_ID_TAG
, LIBRADOS_CMPXATTR_OP_EQ
, state
->obj_tag
);
9525 // FIXME: need to add FAIL_NOTEXIST_OK for racing deletion
9529 if (strcmp(if_match
, "*") == 0) {
9530 // test the object is existing
9531 if (!state
->exists
) {
9532 return -ERR_PRECONDITION_FAILED
;
9536 if (!state
->get_attr(RGW_ATTR_ETAG
, bl
) ||
9537 strncmp(if_match
, bl
.c_str(), bl
.length()) != 0) {
9538 return -ERR_PRECONDITION_FAILED
;
9544 if (strcmp(if_nomatch
, "*") == 0) {
9545 // test the object is NOT existing
9546 if (state
->exists
) {
9547 return -ERR_PRECONDITION_FAILED
;
9551 if (!state
->get_attr(RGW_ATTR_ETAG
, bl
) ||
9552 strncmp(if_nomatch
, bl
.c_str(), bl
.length()) == 0) {
9553 return -ERR_PRECONDITION_FAILED
;
9560 if (state
->exists
) {
9562 store
->remove_rgw_head_obj(op
);
9569 /* the object is being removed, no need to update its tag */
9574 state
->write_tag
= *ptag
;
9576 append_rand_alpha(store
->ctx(), state
->write_tag
, state
->write_tag
, 32);
9579 bl
.append(state
->write_tag
.c_str(), state
->write_tag
.size() + 1);
9581 ldout(store
->ctx(), 10) << "setting object write_tag=" << state
->write_tag
<< dendl
;
9583 op
.setxattr(RGW_ATTR_ID_TAG
, bl
);
9585 op
.setxattr(RGW_ATTR_TAIL_TAG
, bl
);
9591 int RGWRados::system_obj_set_attr(void *ctx
, rgw_raw_obj
& obj
, const char *name
, bufferlist
& bl
,
9592 RGWObjVersionTracker
*objv_tracker
)
9594 map
<string
, bufferlist
> attrs
;
9596 return system_obj_set_attrs(ctx
, obj
, attrs
, NULL
, objv_tracker
);
9599 int RGWRados::system_obj_set_attrs(void *ctx
, rgw_raw_obj
& obj
,
9600 map
<string
, bufferlist
>& attrs
,
9601 map
<string
, bufferlist
>* rmattrs
,
9602 RGWObjVersionTracker
*objv_tracker
)
9605 int r
= get_system_obj_ref(obj
, &ref
);
9609 ObjectWriteOperation op
;
9612 objv_tracker
->prepare_op_for_write(&op
);
9615 map
<string
, bufferlist
>::iterator iter
;
9617 for (iter
= rmattrs
->begin(); iter
!= rmattrs
->end(); ++iter
) {
9618 const string
& name
= iter
->first
;
9619 op
.rmxattr(name
.c_str());
9623 for (iter
= attrs
.begin(); iter
!= attrs
.end(); ++iter
) {
9624 const string
& name
= iter
->first
;
9625 bufferlist
& bl
= iter
->second
;
9630 op
.setxattr(name
.c_str(), bl
);
9638 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
9646 * Set an attr on an object.
9647 * bucket: name of the bucket holding the object
9648 * obj: name of the object to set the attr on
9649 * name: the attr to set
9650 * bl: the contents of the attr
9651 * Returns: 0 on success, -ERR# otherwise.
9653 int RGWRados::set_attr(void *ctx
, const RGWBucketInfo
& bucket_info
, rgw_obj
& obj
, const char *name
, bufferlist
& bl
)
9655 map
<string
, bufferlist
> attrs
;
9657 return set_attrs(ctx
, bucket_info
, obj
, attrs
, NULL
);
9660 int RGWRados::set_attrs(void *ctx
, const RGWBucketInfo
& bucket_info
, rgw_obj
& obj
,
9661 map
<string
, bufferlist
>& attrs
,
9662 map
<string
, bufferlist
>* rmattrs
)
9665 int r
= get_obj_head_ref(bucket_info
, obj
, &ref
);
9669 RGWObjectCtx
*rctx
= static_cast<RGWObjectCtx
*>(ctx
);
9671 ObjectWriteOperation op
;
9672 RGWObjState
*state
= NULL
;
9674 r
= append_atomic_test(rctx
, bucket_info
, obj
, op
, &state
);
9678 map
<string
, bufferlist
>::iterator iter
;
9680 for (iter
= rmattrs
->begin(); iter
!= rmattrs
->end(); ++iter
) {
9681 const string
& name
= iter
->first
;
9682 op
.rmxattr(name
.c_str());
9686 const rgw_bucket
& bucket
= obj
.bucket
;
9688 for (iter
= attrs
.begin(); iter
!= attrs
.end(); ++iter
) {
9689 const string
& name
= iter
->first
;
9690 bufferlist
& bl
= iter
->second
;
9695 op
.setxattr(name
.c_str(), bl
);
9697 if (name
.compare(RGW_ATTR_DELETE_AT
) == 0) {
9702 rgw_obj_index_key obj_key
;
9703 obj
.key
.get_index_key(&obj_key
);
9705 objexp_hint_add(ts
, bucket
.tenant
, bucket
.name
, bucket
.bucket_id
, obj_key
);
9706 } catch (buffer::error
& err
) {
9707 ldout(cct
, 0) << "ERROR: failed to decode " RGW_ATTR_DELETE_AT
<< " attr" << dendl
;
9715 RGWObjectCtx
obj_ctx(this);
9718 RGWRados::Bucket
bop(this, bucket_info
);
9719 RGWRados::Bucket::UpdateIndex
index_op(&bop
, obj
);
9723 append_rand_alpha(cct
, tag
, tag
, 32);
9724 state
->write_tag
= tag
;
9725 r
= index_op
.prepare(CLS_RGW_OP_ADD
, &state
->write_tag
);
9730 bl
.append(tag
.c_str(), tag
.size() + 1);
9732 op
.setxattr(RGW_ATTR_ID_TAG
, bl
);
9735 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
9738 bufferlist acl_bl
= attrs
[RGW_ATTR_ACL
];
9739 bufferlist etag_bl
= attrs
[RGW_ATTR_ETAG
];
9740 bufferlist content_type_bl
= attrs
[RGW_ATTR_CONTENT_TYPE
];
9741 string
etag(etag_bl
.c_str(), etag_bl
.length());
9742 string
content_type(content_type_bl
.c_str(), content_type_bl
.length());
9743 uint64_t epoch
= ref
.ioctx
.get_last_version();
9744 int64_t poolid
= ref
.ioctx
.get_id();
9745 real_time mtime
= real_clock::now();
9746 r
= index_op
.complete(poolid
, epoch
, state
->size
, state
->accounted_size
,
9747 mtime
, etag
, content_type
, &acl_bl
,
9748 RGW_OBJ_CATEGORY_MAIN
, NULL
);
9750 int ret
= index_op
.cancel();
9752 ldout(cct
, 0) << "ERROR: complete_update_index_cancel() returned ret=" << ret
<< dendl
;
9760 state
->obj_tag
.swap(bl
);
9762 for (iter
= rmattrs
->begin(); iter
!= rmattrs
->end(); ++iter
) {
9763 state
->attrset
.erase(iter
->first
);
9766 for (iter
= attrs
.begin(); iter
!= attrs
.end(); ++iter
) {
9767 state
->attrset
[iter
->first
] = iter
->second
;
9774 int RGWRados::Object::Read::prepare()
9776 RGWRados
*store
= source
->get_store();
9777 CephContext
*cct
= store
->ctx();
9781 map
<string
, bufferlist
>::iterator iter
;
9783 RGWObjState
*astate
;
9784 int r
= source
->get_state(&astate
, true);
9788 if (!astate
->exists
) {
9792 const RGWBucketInfo
& bucket_info
= source
->get_bucket_info();
9794 state
.obj
= astate
->obj
;
9795 store
->obj_to_raw(bucket_info
.placement_rule
, state
.obj
, &state
.head_obj
);
9797 r
= store
->get_obj_head_ioctx(bucket_info
, state
.obj
, &state
.io_ctx
);
9802 *params
.attrs
= astate
->attrset
;
9803 if (cct
->_conf
->subsys
.should_gather(ceph_subsys_rgw
, 20)) {
9804 for (iter
= params
.attrs
->begin(); iter
!= params
.attrs
->end(); ++iter
) {
9805 ldout(cct
, 20) << "Read xattr: " << iter
->first
<< dendl
;
9810 /* Convert all times go GMT to make them compatible */
9811 if (conds
.mod_ptr
|| conds
.unmod_ptr
) {
9812 obj_time_weight src_weight
;
9813 src_weight
.init(astate
);
9814 src_weight
.high_precision
= conds
.high_precision_time
;
9816 obj_time_weight dest_weight
;
9817 dest_weight
.high_precision
= conds
.high_precision_time
;
9819 if (conds
.mod_ptr
) {
9820 dest_weight
.init(*conds
.mod_ptr
, conds
.mod_zone_id
, conds
.mod_pg_ver
);
9821 ldout(cct
, 10) << "If-Modified-Since: " << dest_weight
<< " Last-Modified: " << src_weight
<< dendl
;
9822 if (!(dest_weight
< src_weight
)) {
9823 return -ERR_NOT_MODIFIED
;
9827 if (conds
.unmod_ptr
) {
9828 dest_weight
.init(*conds
.unmod_ptr
, conds
.mod_zone_id
, conds
.mod_pg_ver
);
9829 ldout(cct
, 10) << "If-UnModified-Since: " << dest_weight
<< " Last-Modified: " << src_weight
<< dendl
;
9830 if (dest_weight
< src_weight
) {
9831 return -ERR_PRECONDITION_FAILED
;
9835 if (conds
.if_match
|| conds
.if_nomatch
) {
9836 r
= get_attr(RGW_ATTR_ETAG
, etag
);
9840 if (conds
.if_match
) {
9841 string if_match_str
= rgw_string_unquote(conds
.if_match
);
9842 ldout(cct
, 10) << "ETag: " << etag
.c_str() << " " << " If-Match: " << if_match_str
<< dendl
;
9843 if (if_match_str
.compare(etag
.c_str()) != 0) {
9844 return -ERR_PRECONDITION_FAILED
;
9848 if (conds
.if_nomatch
) {
9849 string if_nomatch_str
= rgw_string_unquote(conds
.if_nomatch
);
9850 ldout(cct
, 10) << "ETag: " << etag
.c_str() << " " << " If-NoMatch: " << if_nomatch_str
<< dendl
;
9851 if (if_nomatch_str
.compare(etag
.c_str()) == 0) {
9852 return -ERR_NOT_MODIFIED
;
9857 if (params
.obj_size
)
9858 *params
.obj_size
= astate
->size
;
9860 *params
.lastmod
= astate
->mtime
;
9865 int RGWRados::Object::Read::range_to_ofs(uint64_t obj_size
, int64_t &ofs
, int64_t &end
)
9872 } else if (end
< 0) {
9877 if (ofs
>= (off_t
)obj_size
) {
9880 if (end
>= (off_t
)obj_size
) {
9887 int RGWRados::SystemObject::get_state(RGWRawObjState
**pstate
, RGWObjVersionTracker
*objv_tracker
)
9889 return store
->get_system_obj_state(&ctx
, obj
, pstate
, objv_tracker
);
9892 int RGWRados::stat_system_obj(RGWObjectCtx
& obj_ctx
,
9893 RGWRados::SystemObject::Read::GetObjState
& state
,
9895 map
<string
, bufferlist
> *attrs
,
9898 RGWObjVersionTracker
*objv_tracker
)
9900 RGWRawObjState
*astate
= NULL
;
9902 int r
= get_system_obj_state(&obj_ctx
, obj
, &astate
, objv_tracker
);
9906 if (!astate
->exists
) {
9911 *attrs
= astate
->attrset
;
9912 if (cct
->_conf
->subsys
.should_gather(ceph_subsys_rgw
, 20)) {
9913 map
<string
, bufferlist
>::iterator iter
;
9914 for (iter
= attrs
->begin(); iter
!= attrs
->end(); ++iter
) {
9915 ldout(cct
, 20) << "Read xattr: " << iter
->first
<< dendl
;
9921 *obj_size
= astate
->size
;
9923 *lastmod
= astate
->mtime
;
9929 int RGWRados::Bucket::UpdateIndex::guard_reshard(BucketShard
**pbs
, std::function
<int(BucketShard
*)> call
)
9931 RGWRados
*store
= target
->get_store();
9935 #define NUM_RESHARD_RETRIES 10
9936 for (int i
= 0; i
< NUM_RESHARD_RETRIES
; ++i
) {
9937 int ret
= get_bucket_shard(&bs
);
9939 ldout(store
->ctx(), 5) << "failed to get BucketShard object: ret=" << ret
<< dendl
;
9943 if (r
!= -ERR_BUSY_RESHARDING
) {
9946 ldout(store
->ctx(), 0) << "NOTICE: resharding operation on bucket index detected, blocking" << dendl
;
9947 string new_bucket_id
;
9948 r
= store
->block_while_resharding(bs
, &new_bucket_id
);
9949 if (r
== -ERR_BUSY_RESHARDING
) {
9955 ldout(store
->ctx(), 20) << "reshard completion identified, new_bucket_id=" << new_bucket_id
<< dendl
;
9956 i
= 0; /* resharding is finished, make sure we can retry */
9957 r
= target
->update_bucket_id(new_bucket_id
);
9959 ldout(store
->ctx(), 0) << "ERROR: update_bucket_id() new_bucket_id=" << new_bucket_id
<< " returned r=" << r
<< dendl
;
9976 int RGWRados::SystemObject::Read::stat(RGWObjVersionTracker
*objv_tracker
)
9978 RGWRados
*store
= source
->get_store();
9979 rgw_raw_obj
& obj
= source
->get_obj();
9981 return store
->stat_system_obj(source
->get_ctx(), state
, obj
, stat_params
.attrs
,
9982 stat_params
.lastmod
, stat_params
.obj_size
, objv_tracker
);
9985 int RGWRados::Bucket::UpdateIndex::prepare(RGWModifyOp op
, const string
*write_tag
)
9990 RGWRados
*store
= target
->get_store();
9992 if (write_tag
&& write_tag
->length()) {
9993 optag
= string(write_tag
->c_str(), write_tag
->length());
9995 if (optag
.empty()) {
9996 append_rand_alpha(store
->ctx(), optag
, optag
, 32);
10000 int r
= guard_reshard(nullptr, [&](BucketShard
*bs
) -> int {
10001 return store
->cls_obj_prepare_op(*bs
, op
, optag
, obj
, bilog_flags
, zones_trace
);
10012 int RGWRados::Bucket::UpdateIndex::complete(int64_t poolid
, uint64_t epoch
,
10013 uint64_t size
, uint64_t accounted_size
,
10014 ceph::real_time
& ut
, const string
& etag
,
10015 const string
& content_type
,
10016 bufferlist
*acl_bl
,
10017 RGWObjCategory category
,
10018 list
<rgw_obj_index_key
> *remove_objs
, const string
*user_data
)
10023 RGWRados
*store
= target
->get_store();
10026 int ret
= get_bucket_shard(&bs
);
10028 ldout(store
->ctx(), 5) << "failed to get BucketShard object: ret=" << ret
<< dendl
;
10032 rgw_bucket_dir_entry ent
;
10033 obj
.key
.get_index_key(&ent
.key
);
10034 ent
.meta
.size
= size
;
10035 ent
.meta
.accounted_size
= accounted_size
;
10036 ent
.meta
.mtime
= ut
;
10037 ent
.meta
.etag
= etag
;
10039 ent
.meta
.user_data
= *user_data
;
10042 if (acl_bl
&& acl_bl
->length()) {
10043 int ret
= store
->decode_policy(*acl_bl
, &owner
);
10045 ldout(store
->ctx(), 0) << "WARNING: could not decode policy ret=" << ret
<< dendl
;
10048 ent
.meta
.owner
= owner
.get_id().to_str();
10049 ent
.meta
.owner_display_name
= owner
.get_display_name();
10050 ent
.meta
.content_type
= content_type
;
10052 ret
= store
->cls_obj_complete_add(*bs
, obj
, optag
, poolid
, epoch
, ent
, category
, remove_objs
, bilog_flags
, zones_trace
);
10054 if (target
->bucket_info
.datasync_flag_enabled()) {
10055 int r
= store
->data_log
->add_entry(bs
->bucket
, bs
->shard_id
);
10057 lderr(store
->ctx()) << "ERROR: failed writing data log" << dendl
;
10064 int RGWRados::Bucket::UpdateIndex::complete_del(int64_t poolid
, uint64_t epoch
,
10065 real_time
& removed_mtime
,
10066 list
<rgw_obj_index_key
> *remove_objs
)
10071 RGWRados
*store
= target
->get_store();
10074 int ret
= get_bucket_shard(&bs
);
10076 ldout(store
->ctx(), 5) << "failed to get BucketShard object: ret=" << ret
<< dendl
;
10080 ret
= store
->cls_obj_complete_del(*bs
, optag
, poolid
, epoch
, obj
, removed_mtime
, remove_objs
, bilog_flags
, zones_trace
);
10082 if (target
->bucket_info
.datasync_flag_enabled()) {
10083 int r
= store
->data_log
->add_entry(bs
->bucket
, bs
->shard_id
);
10085 lderr(store
->ctx()) << "ERROR: failed writing data log" << dendl
;
10093 int RGWRados::Bucket::UpdateIndex::cancel()
10098 RGWRados
*store
= target
->get_store();
10101 int ret
= guard_reshard(&bs
, [&](BucketShard
*bs
) -> int {
10102 return store
->cls_obj_complete_cancel(*bs
, optag
, obj
, bilog_flags
, zones_trace
);
10106 * need to update data log anyhow, so that whoever follows needs to update its internal markers
10107 * for following the specific bucket shard log. Otherwise they end up staying behind, and users
10108 * have no way to tell that they're all caught up
10110 if (target
->bucket_info
.datasync_flag_enabled()) {
10111 int r
= store
->data_log
->add_entry(bs
->bucket
, bs
->shard_id
);
10113 lderr(store
->ctx()) << "ERROR: failed writing data log" << dendl
;
10120 int RGWRados::Object::Read::read(int64_t ofs
, int64_t end
, bufferlist
& bl
)
10122 RGWRados
*store
= source
->get_store();
10123 CephContext
*cct
= store
->ctx();
10125 rgw_raw_obj read_obj
;
10126 uint64_t read_ofs
= ofs
;
10127 uint64_t len
, read_len
;
10128 bool reading_from_head
= true;
10129 ObjectReadOperation op
;
10131 bool merge_bl
= false;
10132 bufferlist
*pbl
= &bl
;
10133 bufferlist read_bl
;
10134 uint64_t max_chunk_size
;
10136 RGWObjState
*astate
;
10137 int r
= source
->get_state(&astate
, true);
10144 len
= end
- ofs
+ 1;
10146 if (astate
->has_manifest
&& astate
->manifest
.has_tail()) {
10147 /* now get the relevant object part */
10148 RGWObjManifest::obj_iterator iter
= astate
->manifest
.obj_find(ofs
);
10150 uint64_t stripe_ofs
= iter
.get_stripe_ofs();
10151 read_obj
= iter
.get_location().get_raw_obj(store
);
10152 len
= min(len
, iter
.get_stripe_size() - (ofs
- stripe_ofs
));
10153 read_ofs
= iter
.location_ofs() + (ofs
- stripe_ofs
);
10154 reading_from_head
= (read_obj
== state
.head_obj
);
10156 read_obj
= state
.head_obj
;
10159 r
= store
->get_max_chunk_size(read_obj
.pool
, &max_chunk_size
);
10161 ldout(cct
, 0) << "ERROR: failed to get max_chunk_size() for pool " << read_obj
.pool
<< dendl
;
10165 if (len
> max_chunk_size
)
10166 len
= max_chunk_size
;
10169 state
.io_ctx
.locator_set_key(read_obj
.loc
);
10173 if (reading_from_head
) {
10174 /* only when reading from the head object do we need to do the atomic test */
10175 r
= store
->append_atomic_test(&source
->get_ctx(), source
->get_bucket_info(), state
.obj
, op
, &astate
);
10179 if (astate
&& astate
->prefetch_data
) {
10180 if (!ofs
&& astate
->data
.length() >= len
) {
10182 return bl
.length();
10185 if (ofs
< astate
->data
.length()) {
10186 unsigned copy_len
= min((uint64_t)astate
->data
.length() - ofs
, len
);
10187 astate
->data
.copy(ofs
, copy_len
, bl
);
10188 read_len
-= copy_len
;
10189 read_ofs
+= copy_len
;
10191 return bl
.length();
10199 ldout(cct
, 20) << "rados->read obj-ofs=" << ofs
<< " read_ofs=" << read_ofs
<< " read_len=" << read_len
<< dendl
;
10200 op
.read(read_ofs
, read_len
, pbl
, NULL
);
10202 r
= state
.io_ctx
.operate(read_obj
.oid
, &op
, NULL
);
10203 ldout(cct
, 20) << "rados->read r=" << r
<< " bl.length=" << bl
.length() << dendl
;
10210 bl
.append(read_bl
);
10213 return bl
.length();
10216 int RGWRados::SystemObject::Read::GetObjState::get_ref(RGWRados
*store
, rgw_raw_obj
& obj
, rgw_rados_ref
**pref
)
10219 int r
= store
->get_raw_obj_ref(obj
, &ref
);
10230 int RGWRados::get_system_obj(RGWObjectCtx
& obj_ctx
, RGWRados::SystemObject::Read::GetObjState
& read_state
,
10231 RGWObjVersionTracker
*objv_tracker
, rgw_raw_obj
& obj
,
10232 bufferlist
& bl
, off_t ofs
, off_t end
,
10233 map
<string
, bufferlist
> *attrs
,
10234 rgw_cache_entry_info
*cache_info
)
10237 ObjectReadOperation op
;
10242 len
= end
- ofs
+ 1;
10244 if (objv_tracker
) {
10245 objv_tracker
->prepare_op_for_read(&op
);
10248 ldout(cct
, 20) << "rados->read ofs=" << ofs
<< " len=" << len
<< dendl
;
10249 op
.read(ofs
, len
, &bl
, NULL
);
10252 op
.getxattrs(attrs
, NULL
);
10255 rgw_rados_ref
*ref
;
10256 int r
= read_state
.get_ref(this, obj
, &ref
);
10258 ldout(cct
, 20) << "read_state.get_ref() on obj=" << obj
<< " returned " << r
<< dendl
;
10261 r
= ref
->ioctx
.operate(ref
->oid
, &op
, NULL
);
10263 ldout(cct
, 20) << "rados->read r=" << r
<< " bl.length=" << bl
.length() << dendl
;
10266 ldout(cct
, 20) << "rados->read r=" << r
<< " bl.length=" << bl
.length() << dendl
;
10268 uint64_t op_ver
= ref
->ioctx
.get_last_version();
10270 if (read_state
.last_ver
> 0 &&
10271 read_state
.last_ver
!= op_ver
) {
10272 ldout(cct
, 5) << "raced with an object write, abort" << dendl
;
10276 read_state
.last_ver
= op_ver
;
10278 return bl
.length();
10281 int RGWRados::SystemObject::Read::read(int64_t ofs
, int64_t end
, bufferlist
& bl
, RGWObjVersionTracker
*objv_tracker
)
10283 RGWRados
*store
= source
->get_store();
10284 rgw_raw_obj
& obj
= source
->get_obj();
10286 return store
->get_system_obj(source
->get_ctx(), state
, objv_tracker
, obj
, bl
, ofs
, end
, read_params
.attrs
, read_params
.cache_info
);
10289 int RGWRados::SystemObject::Read::get_attr(const char *name
, bufferlist
& dest
)
10291 RGWRados
*store
= source
->get_store();
10292 rgw_raw_obj
& obj
= source
->get_obj();
10294 return store
->system_obj_get_attr(obj
, name
, dest
);
10297 struct get_obj_data
;
10299 struct get_obj_aio_data
{
10300 struct get_obj_data
*op_data
;
10305 struct get_obj_io
{
10310 static void _get_obj_aio_completion_cb(completion_t cb
, void *arg
);
10312 struct get_obj_data
: public RefCountedObject
{
10317 map
<off_t
, get_obj_io
> io_map
;
10318 map
<off_t
, librados::AioCompletion
*> completion_map
;
10319 uint64_t total_read
;
10322 list
<get_obj_aio_data
> aio_data
;
10323 RGWGetDataCB
*client_cb
;
10324 std::atomic
<bool> cancelled
= { false };
10325 std::atomic
<int64_t> err_code
= { 0 };
10327 list
<bufferlist
> read_list
;
10329 explicit get_obj_data(CephContext
*_cct
)
10331 rados(NULL
), ctx(NULL
),
10332 total_read(0), lock("get_obj_data"), data_lock("get_obj_data::data_lock"),
10334 throttle(cct
, "get_obj_data", cct
->_conf
->rgw_get_obj_window_size
, false) {}
10335 ~get_obj_data() override
{ }
10336 void set_cancelled(int r
) {
10341 bool is_cancelled() {
10345 int get_err_code() {
10349 int wait_next_io(bool *done
) {
10351 map
<off_t
, librados::AioCompletion
*>::iterator iter
= completion_map
.begin();
10352 if (iter
== completion_map
.end()) {
10357 off_t cur_ofs
= iter
->first
;
10358 librados::AioCompletion
*c
= iter
->second
;
10361 c
->wait_for_safe_and_cb();
10362 int r
= c
->get_return_value();
10365 completion_map
.erase(cur_ofs
);
10367 if (completion_map
.empty()) {
10377 void add_io(off_t ofs
, off_t len
, bufferlist
**pbl
, AioCompletion
**pc
) {
10378 Mutex::Locker
l(lock
);
10380 const auto& io_iter
= io_map
.insert(
10381 map
<off_t
, get_obj_io
>::value_type(ofs
, get_obj_io()));
10383 assert(io_iter
.second
); // assert new insertion
10385 get_obj_io
& io
= (io_iter
.first
)->second
;
10388 struct get_obj_aio_data aio
;
10391 aio
.op_data
= this;
10393 aio_data
.push_back(aio
);
10395 struct get_obj_aio_data
*paio_data
= &aio_data
.back(); /* last element */
10397 librados::AioCompletion
*c
= librados::Rados::aio_create_completion((void *)paio_data
, NULL
, _get_obj_aio_completion_cb
);
10398 completion_map
[ofs
] = c
;
10402 /* we have a reference per IO, plus one reference for the calling function.
10403 * reference is dropped for each callback, plus when we're done iterating
10404 * over the parts */
10408 void cancel_io(off_t ofs
) {
10409 ldout(cct
, 20) << "get_obj_data::cancel_io() ofs=" << ofs
<< dendl
;
10411 map
<off_t
, AioCompletion
*>::iterator iter
= completion_map
.find(ofs
);
10412 if (iter
!= completion_map
.end()) {
10413 AioCompletion
*c
= iter
->second
;
10415 completion_map
.erase(ofs
);
10420 /* we don't drop a reference here -- e.g., not calling d->put(), because we still
10421 * need IoCtx to live, as io callback may still be called
10425 void cancel_all_io() {
10426 ldout(cct
, 20) << "get_obj_data::cancel_all_io()" << dendl
;
10427 Mutex::Locker
l(lock
);
10428 for (map
<off_t
, librados::AioCompletion
*>::iterator iter
= completion_map
.begin();
10429 iter
!= completion_map
.end(); ++iter
) {
10430 librados::AioCompletion
*c
= iter
->second
;
10435 int get_complete_ios(off_t ofs
, list
<bufferlist
>& bl_list
) {
10436 Mutex::Locker
l(lock
);
10438 map
<off_t
, get_obj_io
>::iterator liter
= io_map
.begin();
10440 if (liter
== io_map
.end() ||
10441 liter
->first
!= ofs
) {
10445 map
<off_t
, librados::AioCompletion
*>::iterator aiter
;
10446 aiter
= completion_map
.find(ofs
);
10447 if (aiter
== completion_map
.end()) {
10448 /* completion map does not hold this io, it was cancelled */
10452 AioCompletion
*completion
= aiter
->second
;
10453 int r
= completion
->get_return_value();
10457 for (; aiter
!= completion_map
.end(); ++aiter
) {
10458 completion
= aiter
->second
;
10459 if (!completion
->is_safe()) {
10460 /* reached a request that is not yet complete, stop */
10464 r
= completion
->get_return_value();
10466 set_cancelled(r
); /* mark it as cancelled, so that we don't continue processing next operations */
10472 map
<off_t
, get_obj_io
>::iterator old_liter
= liter
++;
10473 bl_list
.push_back(old_liter
->second
.bl
);
10474 io_map
.erase(old_liter
);
10481 static int _get_obj_iterate_cb(const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
, const rgw_raw_obj
& read_obj
, off_t obj_ofs
, off_t read_ofs
, off_t len
, bool is_head_obj
, RGWObjState
*astate
, void *arg
)
10483 struct get_obj_data
*d
= (struct get_obj_data
*)arg
;
10485 return d
->rados
->get_obj_iterate_cb(d
->ctx
, astate
, bucket_info
, obj
, read_obj
, obj_ofs
, read_ofs
, len
, is_head_obj
, arg
);
10488 static void _get_obj_aio_completion_cb(completion_t cb
, void *arg
)
10490 struct get_obj_aio_data
*aio_data
= (struct get_obj_aio_data
*)arg
;
10491 struct get_obj_data
*d
= aio_data
->op_data
;
10493 d
->rados
->get_obj_aio_completion_cb(cb
, arg
);
10497 void RGWRados::get_obj_aio_completion_cb(completion_t c
, void *arg
)
10499 struct get_obj_aio_data
*aio_data
= (struct get_obj_aio_data
*)arg
;
10500 struct get_obj_data
*d
= aio_data
->op_data
;
10501 off_t ofs
= aio_data
->ofs
;
10502 off_t len
= aio_data
->len
;
10504 list
<bufferlist
> bl_list
;
10505 list
<bufferlist
>::iterator iter
;
10508 ldout(cct
, 20) << "get_obj_aio_completion_cb: io completion ofs=" << ofs
<< " len=" << len
<< dendl
;
10509 d
->throttle
.put(len
);
10511 r
= rados_aio_get_return_value(c
);
10513 ldout(cct
, 0) << "ERROR: got unexpected error when trying to read object: " << r
<< dendl
;
10514 d
->set_cancelled(r
);
10518 if (d
->is_cancelled()) {
10522 d
->data_lock
.Lock();
10524 r
= d
->get_complete_ios(ofs
, bl_list
);
10529 d
->read_list
.splice(d
->read_list
.end(), bl_list
);
10532 d
->data_lock
.Unlock();
10538 int RGWRados::flush_read_list(struct get_obj_data
*d
)
10540 d
->data_lock
.Lock();
10541 list
<bufferlist
> l
;
10542 l
.swap(d
->read_list
);
10544 d
->read_list
.clear();
10546 d
->data_lock
.Unlock();
10550 list
<bufferlist
>::iterator iter
;
10551 for (iter
= l
.begin(); iter
!= l
.end(); ++iter
) {
10552 bufferlist
& bl
= *iter
;
10553 r
= d
->client_cb
->handle_data(bl
, 0, bl
.length());
10555 dout(0) << "ERROR: flush_read_list(): d->client_cb->handle_data() returned " << r
<< dendl
;
10560 d
->data_lock
.Lock();
10563 d
->set_cancelled(r
);
10565 d
->data_lock
.Unlock();
10569 int RGWRados::get_obj_iterate_cb(RGWObjectCtx
*ctx
, RGWObjState
*astate
,
10570 const RGWBucketInfo
& bucket_info
,
10571 const rgw_obj
& obj
,
10572 const rgw_raw_obj
& read_obj
,
10574 off_t read_ofs
, off_t len
,
10575 bool is_head_obj
, void *arg
)
10577 RGWObjectCtx
*rctx
= static_cast<RGWObjectCtx
*>(ctx
);
10578 ObjectReadOperation op
;
10579 struct get_obj_data
*d
= (struct get_obj_data
*)arg
;
10587 /* only when reading from the head object do we need to do the atomic test */
10588 r
= append_atomic_test(rctx
, bucket_info
, obj
, op
, &astate
);
10593 obj_ofs
< astate
->data
.length()) {
10594 unsigned chunk_len
= min((uint64_t)astate
->data
.length() - obj_ofs
, (uint64_t)len
);
10596 d
->data_lock
.Lock();
10597 r
= d
->client_cb
->handle_data(astate
->data
, obj_ofs
, chunk_len
);
10598 d
->data_lock
.Unlock();
10603 d
->total_read
+= chunk_len
;
10607 read_ofs
+= chunk_len
;
10608 obj_ofs
+= chunk_len
;
10614 d
->throttle
.get(len
);
10615 if (d
->is_cancelled()) {
10616 return d
->get_err_code();
10619 /* add io after we check that we're not cancelled, otherwise we're going to have trouble
10622 d
->add_io(obj_ofs
, len
, &pbl
, &c
);
10624 ldout(cct
, 20) << "rados->get_obj_iterate_cb oid=" << read_obj
.oid
<< " obj-ofs=" << obj_ofs
<< " read_ofs=" << read_ofs
<< " len=" << len
<< dendl
;
10625 op
.read(read_ofs
, len
, pbl
, NULL
);
10627 librados::IoCtx
io_ctx(d
->io_ctx
);
10628 io_ctx
.locator_set_key(read_obj
.loc
);
10630 r
= io_ctx
.aio_operate(read_obj
.oid
, c
, &op
, NULL
);
10632 ldout(cct
, 0) << "rados->aio_operate r=" << r
<< dendl
;
10636 // Flush data to client if there is any
10637 r
= flush_read_list(d
);
10644 ldout(cct
, 20) << "cancelling io r=" << r
<< " obj_ofs=" << obj_ofs
<< dendl
;
10645 d
->set_cancelled(r
);
10646 d
->cancel_io(obj_ofs
);
10651 int RGWRados::Object::Read::iterate(int64_t ofs
, int64_t end
, RGWGetDataCB
*cb
)
10653 RGWRados
*store
= source
->get_store();
10654 CephContext
*cct
= store
->ctx();
10656 struct get_obj_data
*data
= new get_obj_data(cct
);
10659 RGWObjectCtx
& obj_ctx
= source
->get_ctx();
10661 data
->rados
= store
;
10662 data
->io_ctx
.dup(state
.io_ctx
);
10663 data
->client_cb
= cb
;
10665 int r
= store
->iterate_obj(obj_ctx
, source
->get_bucket_info(), state
.obj
, ofs
, end
, cct
->_conf
->rgw_get_obj_max_req_size
, _get_obj_iterate_cb
, (void *)data
);
10667 data
->cancel_all_io();
10672 r
= data
->wait_next_io(&done
);
10674 dout(10) << "get_obj_iterate() r=" << r
<< ", canceling all io" << dendl
;
10675 data
->cancel_all_io();
10678 r
= store
->flush_read_list(data
);
10680 dout(10) << "get_obj_iterate() r=" << r
<< ", canceling all io" << dendl
;
10681 data
->cancel_all_io();
10691 int RGWRados::iterate_obj(RGWObjectCtx
& obj_ctx
,
10692 const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
,
10693 off_t ofs
, off_t end
,
10694 uint64_t max_chunk_size
,
10695 int (*iterate_obj_cb
)(const RGWBucketInfo
&, const rgw_obj
& obj
,
10696 const rgw_raw_obj
&, off_t
, off_t
, off_t
, bool,
10697 RGWObjState
*, void *),
10700 rgw_raw_obj head_obj
;
10701 rgw_raw_obj read_obj
;
10702 uint64_t read_ofs
= ofs
;
10704 bool reading_from_head
= true;
10705 RGWObjState
*astate
= NULL
;
10707 obj_to_raw(bucket_info
.placement_rule
, obj
, &head_obj
);
10709 int r
= get_obj_state(&obj_ctx
, bucket_info
, obj
, &astate
, false);
10717 len
= end
- ofs
+ 1;
10719 if (astate
->has_manifest
) {
10720 /* now get the relevant object stripe */
10721 RGWObjManifest::obj_iterator iter
= astate
->manifest
.obj_find(ofs
);
10723 RGWObjManifest::obj_iterator obj_end
= astate
->manifest
.obj_end();
10725 for (; iter
!= obj_end
&& ofs
<= end
; ++iter
) {
10726 off_t stripe_ofs
= iter
.get_stripe_ofs();
10727 off_t next_stripe_ofs
= stripe_ofs
+ iter
.get_stripe_size();
10729 while (ofs
< next_stripe_ofs
&& ofs
<= end
) {
10730 read_obj
= iter
.get_location().get_raw_obj(this);
10731 uint64_t read_len
= min(len
, iter
.get_stripe_size() - (ofs
- stripe_ofs
));
10732 read_ofs
= iter
.location_ofs() + (ofs
- stripe_ofs
);
10734 if (read_len
> max_chunk_size
) {
10735 read_len
= max_chunk_size
;
10738 reading_from_head
= (read_obj
== head_obj
);
10739 r
= iterate_obj_cb(bucket_info
, obj
, read_obj
, ofs
, read_ofs
, read_len
, reading_from_head
, astate
, arg
);
10749 while (ofs
<= end
) {
10750 read_obj
= head_obj
;
10751 uint64_t read_len
= min(len
, max_chunk_size
);
10753 r
= iterate_obj_cb(bucket_info
, obj
, read_obj
, ofs
, ofs
, read_len
, reading_from_head
, astate
, arg
);
10766 int RGWRados::obj_operate(const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
, ObjectWriteOperation
*op
)
10769 int r
= get_obj_head_ref(bucket_info
, obj
, &ref
);
10774 return ref
.ioctx
.operate(ref
.oid
, op
);
10777 int RGWRados::obj_operate(const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
, ObjectReadOperation
*op
)
10780 int r
= get_obj_head_ref(bucket_info
, obj
, &ref
);
10787 return ref
.ioctx
.operate(ref
.oid
, op
, &outbl
);
10790 int RGWRados::olh_init_modification_impl(const RGWBucketInfo
& bucket_info
, RGWObjState
& state
, const rgw_obj
& olh_obj
, string
*op_tag
)
10792 ObjectWriteOperation op
;
10794 assert(olh_obj
.key
.instance
.empty());
10796 bool has_tag
= (state
.exists
&& has_olh_tag(state
.attrset
));
10798 if (!state
.exists
) {
10801 op
.assert_exists();
10805 * 3 possible cases: olh object doesn't exist, it exists as an olh, it exists as a regular object.
10806 * If it exists as a regular object we'll need to transform it into an olh. We'll do it in two
10807 * steps, first change its tag and set the olh pending attrs. Once write is done we'll need to
10808 * truncate it, remove extra attrs, and send it to the garbage collection. The bucket index olh
10809 * log will reflect that.
10811 * Need to generate separate olh and obj tags, as olh can be colocated with object data. obj_tag
10812 * is used for object data instance, olh_tag for olh instance.
10815 /* guard against racing writes */
10816 bucket_index_guard_olh_op(state
, op
);
10822 int ret
= gen_rand_alphanumeric_lower(cct
, &obj_tag
, 32);
10824 ldout(cct
, 0) << "ERROR: gen_rand_alphanumeric_lower() returned ret=" << ret
<< dendl
;
10828 bl
.append(obj_tag
.c_str(), obj_tag
.size());
10829 op
.setxattr(RGW_ATTR_ID_TAG
, bl
);
10831 state
.attrset
[RGW_ATTR_ID_TAG
] = bl
;
10832 state
.obj_tag
= bl
;
10836 ret
= gen_rand_alphanumeric_lower(cct
, &olh_tag
, 32);
10838 ldout(cct
, 0) << "ERROR: gen_rand_alphanumeric_lower() returned ret=" << ret
<< dendl
;
10842 olh_bl
.append(olh_tag
.c_str(), olh_tag
.size());
10843 op
.setxattr(RGW_ATTR_OLH_ID_TAG
, olh_bl
);
10845 state
.attrset
[RGW_ATTR_OLH_ID_TAG
] = olh_bl
;
10846 state
.olh_tag
= olh_bl
;
10847 state
.is_olh
= true;
10850 op
.setxattr(RGW_ATTR_OLH_VER
, verbl
);
10854 RGWOLHPendingInfo pending_info
;
10855 pending_info
.time
= real_clock::now();
10856 ::encode(pending_info
, bl
);
10858 #define OLH_PENDING_TAG_LEN 32
10859 /* tag will start with current time epoch, this so that entries are sorted by time */
10861 utime_t
ut(pending_info
.time
);
10862 snprintf(buf
, sizeof(buf
), "%016llx", (unsigned long long)ut
.sec());
10866 int ret
= gen_rand_alphanumeric_lower(cct
, &s
, OLH_PENDING_TAG_LEN
- op_tag
->size());
10868 ldout(cct
, 0) << "ERROR: gen_rand_alphanumeric_lower() returned ret=" << ret
<< dendl
;
10873 string attr_name
= RGW_ATTR_OLH_PENDING_PREFIX
;
10874 attr_name
.append(*op_tag
);
10876 op
.setxattr(attr_name
.c_str(), bl
);
10878 ret
= obj_operate(bucket_info
, olh_obj
, &op
);
10883 state
.exists
= true;
10884 state
.attrset
[attr_name
] = bl
;
10889 int RGWRados::olh_init_modification(const RGWBucketInfo
& bucket_info
, RGWObjState
& state
, const rgw_obj
& obj
, string
*op_tag
)
10893 ret
= olh_init_modification_impl(bucket_info
, state
, obj
, op_tag
);
10894 if (ret
== -EEXIST
) {
10901 int RGWRados::guard_reshard(BucketShard
*bs
, const rgw_obj
& obj_instance
, std::function
<int(BucketShard
*)> call
)
10904 const rgw_obj
*pobj
= &obj_instance
;
10907 for (int i
= 0; i
< NUM_RESHARD_RETRIES
; ++i
) {
10908 r
= bs
->init(pobj
->bucket
, *pobj
);
10910 ldout(cct
, 5) << "bs.init() returned ret=" << r
<< dendl
;
10914 if (r
!= -ERR_BUSY_RESHARDING
) {
10917 ldout(cct
, 0) << "NOTICE: resharding operation on bucket index detected, blocking" << dendl
;
10918 string new_bucket_id
;
10919 r
= block_while_resharding(bs
, &new_bucket_id
);
10920 if (r
== -ERR_BUSY_RESHARDING
) {
10926 ldout(cct
, 20) << "reshard completion identified, new_bucket_id=" << new_bucket_id
<< dendl
;
10927 i
= 0; /* resharding is finished, make sure we can retry */
10930 obj
.bucket
.update_bucket_id(new_bucket_id
);
10941 int RGWRados::block_while_resharding(RGWRados::BucketShard
*bs
, string
*new_bucket_id
)
10943 std::shared_ptr
<RGWReshardWait
> waiter
= reshard_wait
;
10945 return waiter
->block_while_resharding(bs
, new_bucket_id
);
10948 int RGWRados::bucket_index_link_olh(const RGWBucketInfo
& bucket_info
, RGWObjState
& olh_state
, const rgw_obj
& obj_instance
,
10949 bool delete_marker
,
10950 const string
& op_tag
,
10951 struct rgw_bucket_dir_entry_meta
*meta
,
10952 uint64_t olh_epoch
,
10953 real_time unmod_since
, bool high_precision_time
, rgw_zone_set
*_zones_trace
)
10956 int r
= get_obj_head_ref(bucket_info
, obj_instance
, &ref
);
10961 rgw_zone_set zones_trace
;
10962 if (_zones_trace
) {
10963 zones_trace
= *_zones_trace
;
10965 zones_trace
.insert(get_zone().id
);
10968 BucketShard
bs(this);
10970 cls_rgw_obj_key
key(obj_instance
.key
.get_index_key_name(), obj_instance
.key
.instance
);
10971 r
= guard_reshard(&bs
, obj_instance
, [&](BucketShard
*bs
) -> int {
10972 librados::ObjectWriteOperation op
;
10973 cls_rgw_guard_bucket_resharding(op
, -ERR_BUSY_RESHARDING
);
10974 return cls_rgw_bucket_link_olh(bs
->index_ctx
, op
,
10975 bs
->bucket_obj
, key
, olh_state
.olh_tag
, delete_marker
, op_tag
, meta
, olh_epoch
,
10976 unmod_since
, high_precision_time
,
10977 get_zone().log_data
, zones_trace
);
10980 ldout(cct
, 20) << "cls_rgw_bucket_link_olh() returned r=" << r
<< dendl
;
10987 void RGWRados::bucket_index_guard_olh_op(RGWObjState
& olh_state
, ObjectOperation
& op
)
10989 ldout(cct
, 20) << __func__
<< "(): olh_state.olh_tag=" << string(olh_state
.olh_tag
.c_str(), olh_state
.olh_tag
.length()) << dendl
;
10990 op
.cmpxattr(RGW_ATTR_OLH_ID_TAG
, CEPH_OSD_CMPXATTR_OP_EQ
, olh_state
.olh_tag
);
10993 int RGWRados::bucket_index_unlink_instance(const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj_instance
,
10994 const string
& op_tag
, const string
& olh_tag
, uint64_t olh_epoch
, rgw_zone_set
*_zones_trace
)
10997 int r
= get_obj_head_ref(bucket_info
, obj_instance
, &ref
);
11002 rgw_zone_set zones_trace
;
11003 if (_zones_trace
) {
11004 zones_trace
= *_zones_trace
;
11006 zones_trace
.insert(get_zone().id
);
11008 BucketShard
bs(this);
11010 cls_rgw_obj_key
key(obj_instance
.key
.get_index_key_name(), obj_instance
.key
.instance
);
11011 r
= guard_reshard(&bs
, obj_instance
, [&](BucketShard
*bs
) -> int {
11012 librados::ObjectWriteOperation op
;
11013 cls_rgw_guard_bucket_resharding(op
, -ERR_BUSY_RESHARDING
);
11014 return cls_rgw_bucket_unlink_instance(bs
->index_ctx
, op
, bs
->bucket_obj
, key
, op_tag
,
11015 olh_tag
, olh_epoch
, get_zone().log_data
, zones_trace
);
11018 ldout(cct
, 20) << "cls_rgw_bucket_link_olh() returned r=" << r
<< dendl
;
11025 int RGWRados::bucket_index_read_olh_log(const RGWBucketInfo
& bucket_info
, RGWObjState
& state
,
11026 const rgw_obj
& obj_instance
, uint64_t ver_marker
,
11027 map
<uint64_t, vector
<rgw_bucket_olh_log_entry
> > *log
,
11028 bool *is_truncated
)
11031 int r
= get_obj_head_ref(bucket_info
, obj_instance
, &ref
);
11036 BucketShard
bs(this);
11037 int ret
= bs
.init(obj_instance
.bucket
, obj_instance
);
11039 ldout(cct
, 5) << "bs.init() returned ret=" << ret
<< dendl
;
11043 string
olh_tag(state
.olh_tag
.c_str(), state
.olh_tag
.length());
11045 cls_rgw_obj_key
key(obj_instance
.key
.get_index_key_name(), string());
11047 ret
= guard_reshard(&bs
, obj_instance
, [&](BucketShard
*bs
) -> int {
11048 ObjectReadOperation op
;
11049 cls_rgw_guard_bucket_resharding(op
, -ERR_BUSY_RESHARDING
);
11050 return cls_rgw_get_olh_log(bs
->index_ctx
, bs
->bucket_obj
, op
,
11051 key
, ver_marker
, olh_tag
, log
, is_truncated
);
11054 ldout(cct
, 20) << "cls_rgw_get_olh_log() returned r=" << r
<< dendl
;
11061 int RGWRados::bucket_index_trim_olh_log(const RGWBucketInfo
& bucket_info
, RGWObjState
& state
, const rgw_obj
& obj_instance
, uint64_t ver
)
11064 int r
= get_obj_head_ref(bucket_info
, obj_instance
, &ref
);
11069 BucketShard
bs(this);
11070 int ret
= bs
.init(obj_instance
.bucket
, obj_instance
);
11072 ldout(cct
, 5) << "bs.init() returned ret=" << ret
<< dendl
;
11076 string
olh_tag(state
.olh_tag
.c_str(), state
.olh_tag
.length());
11078 cls_rgw_obj_key
key(obj_instance
.key
.get_index_key_name(), string());
11080 ret
= guard_reshard(&bs
, obj_instance
, [&](BucketShard
*pbs
) -> int {
11081 ObjectWriteOperation op
;
11082 cls_rgw_guard_bucket_resharding(op
, -ERR_BUSY_RESHARDING
);
11083 cls_rgw_trim_olh_log(op
, key
, ver
, olh_tag
);
11084 return pbs
->index_ctx
.operate(pbs
->bucket_obj
, &op
);
11087 ldout(cct
, 20) << "cls_rgw_trim_olh_log() returned r=" << ret
<< dendl
;
11094 int RGWRados::bucket_index_clear_olh(const RGWBucketInfo
& bucket_info
, RGWObjState
& state
, const rgw_obj
& obj_instance
)
11097 int r
= get_obj_head_ref(bucket_info
, obj_instance
, &ref
);
11102 BucketShard
bs(this);
11104 string
olh_tag(state
.olh_tag
.c_str(), state
.olh_tag
.length());
11106 cls_rgw_obj_key
key(obj_instance
.key
.get_index_key_name(), string());
11108 int ret
= guard_reshard(&bs
, obj_instance
, [&](BucketShard
*pbs
) -> int {
11109 ObjectWriteOperation op
;
11110 cls_rgw_guard_bucket_resharding(op
, -ERR_BUSY_RESHARDING
);
11111 return cls_rgw_clear_olh(pbs
->index_ctx
, op
, pbs
->bucket_obj
, key
, olh_tag
);
11114 ldout(cct
, 5) << "cls_rgw_clear_olh() returned ret=" << ret
<< dendl
;
11121 int RGWRados::apply_olh_log(RGWObjectCtx
& obj_ctx
, RGWObjState
& state
, const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
,
11122 bufferlist
& olh_tag
, map
<uint64_t, vector
<rgw_bucket_olh_log_entry
> >& log
,
11123 uint64_t *plast_ver
, rgw_zone_set
* zones_trace
)
11129 librados::ObjectWriteOperation op
;
11131 uint64_t last_ver
= log
.rbegin()->first
;
11132 *plast_ver
= last_ver
;
11134 map
<uint64_t, vector
<rgw_bucket_olh_log_entry
> >::iterator iter
= log
.begin();
11136 op
.cmpxattr(RGW_ATTR_OLH_ID_TAG
, CEPH_OSD_CMPXATTR_OP_EQ
, olh_tag
);
11137 op
.cmpxattr(RGW_ATTR_OLH_VER
, CEPH_OSD_CMPXATTR_OP_GT
, last_ver
);
11139 bool need_to_link
= false;
11140 cls_rgw_obj_key key
;
11141 bool delete_marker
= false;
11142 list
<cls_rgw_obj_key
> remove_instances
;
11143 bool need_to_remove
= false;
11145 for (iter
= log
.begin(); iter
!= log
.end(); ++iter
) {
11146 vector
<rgw_bucket_olh_log_entry
>::iterator viter
= iter
->second
.begin();
11147 for (; viter
!= iter
->second
.end(); ++viter
) {
11148 rgw_bucket_olh_log_entry
& entry
= *viter
;
11150 ldout(cct
, 20) << "olh_log_entry: op=" << (int)entry
.op
11151 << " key=" << entry
.key
.name
<< "[" << entry
.key
.instance
<< "] "
11152 << (entry
.delete_marker
? "(delete)" : "") << dendl
;
11153 switch (entry
.op
) {
11154 case CLS_RGW_OLH_OP_REMOVE_INSTANCE
:
11155 remove_instances
.push_back(entry
.key
);
11157 case CLS_RGW_OLH_OP_LINK_OLH
:
11158 need_to_link
= true;
11159 need_to_remove
= false;
11161 delete_marker
= entry
.delete_marker
;
11163 case CLS_RGW_OLH_OP_UNLINK_OLH
:
11164 need_to_remove
= true;
11165 need_to_link
= false;
11168 ldout(cct
, 0) << "ERROR: apply_olh_log: invalid op: " << (int)entry
.op
<< dendl
;
11171 string attr_name
= RGW_ATTR_OLH_PENDING_PREFIX
;
11172 attr_name
.append(entry
.op_tag
);
11173 op
.rmxattr(attr_name
.c_str());
11178 int r
= get_obj_head_ref(bucket_info
, obj
, &ref
);
11183 const rgw_bucket
& bucket
= obj
.bucket
;
11185 if (need_to_link
) {
11186 rgw_obj
target(bucket
, key
);
11188 info
.target
= target
;
11189 info
.removed
= delete_marker
;
11191 ::encode(info
, bl
);
11192 op
.setxattr(RGW_ATTR_OLH_INFO
, bl
);
11195 /* first remove object instances */
11196 for (list
<cls_rgw_obj_key
>::iterator liter
= remove_instances
.begin();
11197 liter
!= remove_instances
.end(); ++liter
) {
11198 cls_rgw_obj_key
& key
= *liter
;
11199 rgw_obj
obj_instance(bucket
, key
);
11200 int ret
= delete_obj(obj_ctx
, bucket_info
, obj_instance
, 0, RGW_BILOG_FLAG_VERSIONED_OP
, ceph::real_time(), zones_trace
);
11201 if (ret
< 0 && ret
!= -ENOENT
) {
11202 ldout(cct
, 0) << "ERROR: delete_obj() returned " << ret
<< " obj_instance=" << obj_instance
<< dendl
;
11207 /* update olh object */
11208 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
11209 if (r
== -ECANCELED
) {
11213 ldout(cct
, 0) << "ERROR: could not apply olh update, r=" << r
<< dendl
;
11217 r
= bucket_index_trim_olh_log(bucket_info
, state
, obj
, last_ver
);
11219 ldout(cct
, 0) << "ERROR: could not trim olh log, r=" << r
<< dendl
;
11223 if (need_to_remove
) {
11224 ObjectWriteOperation rm_op
;
11226 rm_op
.cmpxattr(RGW_ATTR_OLH_ID_TAG
, CEPH_OSD_CMPXATTR_OP_EQ
, olh_tag
);
11227 rm_op
.cmpxattr(RGW_ATTR_OLH_VER
, CEPH_OSD_CMPXATTR_OP_GT
, last_ver
);
11228 cls_obj_check_prefix_exist(rm_op
, RGW_ATTR_OLH_PENDING_PREFIX
, true); /* fail if found one of these, pending modification */
11231 r
= ref
.ioctx
.operate(ref
.oid
, &rm_op
);
11232 if (r
== -ECANCELED
) {
11233 return 0; /* someone else won this race */
11236 * only clear if was successful, otherwise we might clobber pending operations on this object
11238 r
= bucket_index_clear_olh(bucket_info
, state
, obj
);
11240 ldout(cct
, 0) << "ERROR: could not clear bucket index olh entries r=" << r
<< dendl
;
11250 * read olh log and apply it
11252 int RGWRados::update_olh(RGWObjectCtx
& obj_ctx
, RGWObjState
*state
, const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
, rgw_zone_set
*zones_trace
)
11254 map
<uint64_t, vector
<rgw_bucket_olh_log_entry
> > log
;
11256 uint64_t ver_marker
= 0;
11259 int ret
= bucket_index_read_olh_log(bucket_info
, *state
, obj
, ver_marker
, &log
, &is_truncated
);
11263 ret
= apply_olh_log(obj_ctx
, *state
, bucket_info
, obj
, state
->olh_tag
, log
, &ver_marker
, zones_trace
);
11267 } while (is_truncated
);
11272 int RGWRados::set_olh(RGWObjectCtx
& obj_ctx
, RGWBucketInfo
& bucket_info
, const rgw_obj
& target_obj
, bool delete_marker
, rgw_bucket_dir_entry_meta
*meta
,
11273 uint64_t olh_epoch
, real_time unmod_since
, bool high_precision_time
, rgw_zone_set
*zones_trace
)
11277 rgw_obj olh_obj
= target_obj
;
11278 olh_obj
.key
.instance
.clear();
11280 RGWObjState
*state
= NULL
;
11285 #define MAX_ECANCELED_RETRY 100
11286 for (i
= 0; i
< MAX_ECANCELED_RETRY
; i
++) {
11287 if (ret
== -ECANCELED
) {
11288 obj_ctx
.obj
.invalidate(olh_obj
);
11291 ret
= get_obj_state(&obj_ctx
, bucket_info
, olh_obj
, &state
, false); /* don't follow olh */
11296 ret
= olh_init_modification(bucket_info
, *state
, olh_obj
, &op_tag
);
11298 ldout(cct
, 20) << "olh_init_modification() target_obj=" << target_obj
<< " delete_marker=" << (int)delete_marker
<< " returned " << ret
<< dendl
;
11299 if (ret
== -ECANCELED
) {
11304 ret
= bucket_index_link_olh(bucket_info
, *state
, target_obj
, delete_marker
, op_tag
, meta
, olh_epoch
, unmod_since
, high_precision_time
, zones_trace
);
11306 ldout(cct
, 20) << "bucket_index_link_olh() target_obj=" << target_obj
<< " delete_marker=" << (int)delete_marker
<< " returned " << ret
<< dendl
;
11307 if (ret
== -ECANCELED
) {
11315 if (i
== MAX_ECANCELED_RETRY
) {
11316 ldout(cct
, 0) << "ERROR: exceeded max ECANCELED retries, aborting (EIO)" << dendl
;
11320 ret
= update_olh(obj_ctx
, state
, bucket_info
, olh_obj
);
11321 if (ret
== -ECANCELED
) { /* already did what we needed, no need to retry, raced with another user */
11325 ldout(cct
, 20) << "update_olh() target_obj=" << target_obj
<< " returned " << ret
<< dendl
;
11332 int RGWRados::unlink_obj_instance(RGWObjectCtx
& obj_ctx
, RGWBucketInfo
& bucket_info
, const rgw_obj
& target_obj
,
11333 uint64_t olh_epoch
, rgw_zone_set
*zones_trace
)
11337 rgw_obj olh_obj
= target_obj
;
11338 olh_obj
.key
.instance
.clear();
11340 RGWObjState
*state
= NULL
;
11345 for (i
= 0; i
< MAX_ECANCELED_RETRY
; i
++) {
11346 if (ret
== -ECANCELED
) {
11347 obj_ctx
.obj
.invalidate(olh_obj
);
11350 ret
= get_obj_state(&obj_ctx
, bucket_info
, olh_obj
, &state
, false); /* don't follow olh */
11354 ret
= olh_init_modification(bucket_info
, *state
, olh_obj
, &op_tag
);
11356 ldout(cct
, 20) << "olh_init_modification() target_obj=" << target_obj
<< " returned " << ret
<< dendl
;
11357 if (ret
== -ECANCELED
) {
11363 string
olh_tag(state
->olh_tag
.c_str(), state
->olh_tag
.length());
11365 ret
= bucket_index_unlink_instance(bucket_info
, target_obj
, op_tag
, olh_tag
, olh_epoch
, zones_trace
);
11367 ldout(cct
, 20) << "bucket_index_unlink_instance() target_obj=" << target_obj
<< " returned " << ret
<< dendl
;
11368 if (ret
== -ECANCELED
) {
11376 if (i
== MAX_ECANCELED_RETRY
) {
11377 ldout(cct
, 0) << "ERROR: exceeded max ECANCELED retries, aborting (EIO)" << dendl
;
11381 ret
= update_olh(obj_ctx
, state
, bucket_info
, olh_obj
, zones_trace
);
11382 if (ret
== -ECANCELED
) { /* already did what we needed, no need to retry, raced with another user */
11386 ldout(cct
, 20) << "update_olh() target_obj=" << target_obj
<< " returned " << ret
<< dendl
;
11393 void RGWRados::gen_rand_obj_instance_name(rgw_obj
*target_obj
)
11395 #define OBJ_INSTANCE_LEN 32
11396 char buf
[OBJ_INSTANCE_LEN
+ 1];
11398 gen_rand_alphanumeric_no_underscore(cct
, buf
, OBJ_INSTANCE_LEN
); /* don't want it to get url escaped,
11399 no underscore for instance name due to the way we encode the raw keys */
11401 target_obj
->key
.set_instance(buf
);
11404 static void filter_attrset(map
<string
, bufferlist
>& unfiltered_attrset
, const string
& check_prefix
,
11405 map
<string
, bufferlist
> *attrset
)
11408 map
<string
, bufferlist
>::iterator iter
;
11409 for (iter
= unfiltered_attrset
.lower_bound(check_prefix
);
11410 iter
!= unfiltered_attrset
.end(); ++iter
) {
11411 if (!boost::algorithm::starts_with(iter
->first
, check_prefix
))
11413 (*attrset
)[iter
->first
] = iter
->second
;
11417 int RGWRados::get_olh(const RGWBucketInfo
& bucket_info
, const rgw_obj
& obj
, RGWOLHInfo
*olh
)
11419 map
<string
, bufferlist
> unfiltered_attrset
;
11421 ObjectReadOperation op
;
11422 op
.getxattrs(&unfiltered_attrset
, NULL
);
11425 int r
= obj_operate(bucket_info
, obj
, &op
);
11430 map
<string
, bufferlist
> attrset
;
11432 filter_attrset(unfiltered_attrset
, RGW_ATTR_OLH_PREFIX
, &attrset
);
11434 map
<string
, bufferlist
>::iterator iter
= attrset
.find(RGW_ATTR_OLH_INFO
);
11435 if (iter
== attrset
.end()) { /* not an olh */
11440 bufferlist::iterator biter
= iter
->second
.begin();
11441 ::decode(*olh
, biter
);
11442 } catch (buffer::error
& err
) {
11443 ldout(cct
, 0) << "ERROR: failed to decode olh info" << dendl
;
11450 void RGWRados::check_pending_olh_entries(map
<string
, bufferlist
>& pending_entries
,
11451 map
<string
, bufferlist
> *rm_pending_entries
)
11453 map
<string
, bufferlist
>::iterator iter
= pending_entries
.begin();
11455 real_time now
= real_clock::now();
11457 while (iter
!= pending_entries
.end()) {
11458 bufferlist::iterator biter
= iter
->second
.begin();
11459 RGWOLHPendingInfo pending_info
;
11461 ::decode(pending_info
, biter
);
11462 } catch (buffer::error
& err
) {
11463 /* skipping bad entry, we could remove it but it might hide a bug */
11464 ldout(cct
, 0) << "ERROR: failed to decode pending entry " << iter
->first
<< dendl
;
11469 map
<string
, bufferlist
>::iterator cur_iter
= iter
;
11471 if (now
- pending_info
.time
>= make_timespan(cct
->_conf
->rgw_olh_pending_timeout_sec
)) {
11472 (*rm_pending_entries
)[cur_iter
->first
] = cur_iter
->second
;
11473 pending_entries
.erase(cur_iter
);
11475 /* entries names are sorted by time (rounded to a second) */
11481 int RGWRados::remove_olh_pending_entries(const RGWBucketInfo
& bucket_info
, RGWObjState
& state
, const rgw_obj
& olh_obj
, map
<string
, bufferlist
>& pending_attrs
)
11483 ObjectWriteOperation op
;
11485 bucket_index_guard_olh_op(state
, op
);
11487 for (map
<string
, bufferlist
>::iterator iter
= pending_attrs
.begin(); iter
!= pending_attrs
.end(); ++iter
) {
11488 op
.rmxattr(iter
->first
.c_str());
11492 int r
= get_obj_head_ref(bucket_info
, olh_obj
, &ref
);
11497 /* update olh object */
11498 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
11499 if (r
== -ENOENT
|| r
== -ECANCELED
) {
11500 /* raced with some other change, shouldn't sweat about it */
11504 ldout(cct
, 0) << "ERROR: could not apply olh update, r=" << r
<< dendl
;
11511 int RGWRados::follow_olh(const RGWBucketInfo
& bucket_info
, RGWObjectCtx
& obj_ctx
, RGWObjState
*state
, const rgw_obj
& olh_obj
, rgw_obj
*target
)
11513 map
<string
, bufferlist
> pending_entries
;
11514 filter_attrset(state
->attrset
, RGW_ATTR_OLH_PENDING_PREFIX
, &pending_entries
);
11516 map
<string
, bufferlist
> rm_pending_entries
;
11517 check_pending_olh_entries(pending_entries
, &rm_pending_entries
);
11519 if (!rm_pending_entries
.empty()) {
11520 int ret
= remove_olh_pending_entries(bucket_info
, *state
, olh_obj
, rm_pending_entries
);
11522 ldout(cct
, 20) << "ERROR: rm_pending_entries returned ret=" << ret
<< dendl
;
11526 if (!pending_entries
.empty()) {
11527 ldout(cct
, 20) << __func__
<< "(): found pending entries, need to update_olh() on bucket=" << olh_obj
.bucket
<< dendl
;
11529 int ret
= update_olh(obj_ctx
, state
, bucket_info
, olh_obj
);
11535 map
<string
, bufferlist
>::iterator iter
= state
->attrset
.find(RGW_ATTR_OLH_INFO
);
11536 assert(iter
!= state
->attrset
.end());
11539 bufferlist::iterator biter
= iter
->second
.begin();
11540 ::decode(olh
, biter
);
11541 } catch (buffer::error
& err
) {
11542 ldout(cct
, 0) << "ERROR: failed to decode olh info" << dendl
;
11550 *target
= olh
.target
;
11555 int RGWRados::raw_obj_stat(rgw_raw_obj
& obj
, uint64_t *psize
, real_time
*pmtime
, uint64_t *epoch
,
11556 map
<string
, bufferlist
> *attrs
, bufferlist
*first_chunk
,
11557 RGWObjVersionTracker
*objv_tracker
)
11560 int r
= get_raw_obj_ref(obj
, &ref
);
11565 map
<string
, bufferlist
> unfiltered_attrset
;
11567 struct timespec mtime_ts
;
11569 ObjectReadOperation op
;
11570 if (objv_tracker
) {
11571 objv_tracker
->prepare_op_for_read(&op
);
11574 op
.getxattrs(&unfiltered_attrset
, NULL
);
11576 if (psize
|| pmtime
) {
11577 op
.stat2(&size
, &mtime_ts
, NULL
);
11580 op
.read(0, cct
->_conf
->rgw_max_chunk_size
, first_chunk
, NULL
);
11583 r
= ref
.ioctx
.operate(ref
.oid
, &op
, &outbl
);
11586 *epoch
= ref
.ioctx
.get_last_version();
11595 *pmtime
= ceph::real_clock::from_timespec(mtime_ts
);
11597 filter_attrset(unfiltered_attrset
, RGW_ATTR_PREFIX
, attrs
);
11603 int RGWRados::get_bucket_stats(RGWBucketInfo
& bucket_info
, int shard_id
, string
*bucket_ver
, string
*master_ver
,
11604 map
<RGWObjCategory
, RGWStorageStats
>& stats
, string
*max_marker
, bool *syncstopped
)
11606 map
<string
, rgw_bucket_dir_header
> headers
;
11607 map
<int, string
> bucket_instance_ids
;
11608 int r
= cls_bucket_head(bucket_info
, shard_id
, headers
, &bucket_instance_ids
);
11613 assert(headers
.size() == bucket_instance_ids
.size());
11615 map
<string
, rgw_bucket_dir_header
>::iterator iter
= headers
.begin();
11616 map
<int, string
>::iterator viter
= bucket_instance_ids
.begin();
11617 BucketIndexShardsManager ver_mgr
;
11618 BucketIndexShardsManager master_ver_mgr
;
11619 BucketIndexShardsManager marker_mgr
;
11621 for(; iter
!= headers
.end(); ++iter
, ++viter
) {
11622 accumulate_raw_stats(iter
->second
, stats
);
11623 snprintf(buf
, sizeof(buf
), "%lu", (unsigned long)iter
->second
.ver
);
11624 ver_mgr
.add(viter
->first
, string(buf
));
11625 snprintf(buf
, sizeof(buf
), "%lu", (unsigned long)iter
->second
.master_ver
);
11626 master_ver_mgr
.add(viter
->first
, string(buf
));
11627 if (shard_id
>= 0) {
11628 *max_marker
= iter
->second
.max_marker
;
11630 marker_mgr
.add(viter
->first
, iter
->second
.max_marker
);
11632 if (syncstopped
!= NULL
)
11633 *syncstopped
= iter
->second
.syncstopped
;
11635 ver_mgr
.to_string(bucket_ver
);
11636 master_ver_mgr
.to_string(master_ver
);
11637 if (shard_id
< 0) {
11638 marker_mgr
.to_string(max_marker
);
11643 int RGWRados::get_bi_log_status(RGWBucketInfo
& bucket_info
, int shard_id
,
11644 map
<int, string
>& markers
)
11646 map
<string
, rgw_bucket_dir_header
> headers
;
11647 map
<int, string
> bucket_instance_ids
;
11648 int r
= cls_bucket_head(bucket_info
, shard_id
, headers
, &bucket_instance_ids
);
11652 assert(headers
.size() == bucket_instance_ids
.size());
11654 map
<string
, rgw_bucket_dir_header
>::iterator iter
= headers
.begin();
11655 map
<int, string
>::iterator viter
= bucket_instance_ids
.begin();
11657 for(; iter
!= headers
.end(); ++iter
, ++viter
) {
11658 if (shard_id
>= 0) {
11659 markers
[shard_id
] = iter
->second
.max_marker
;
11661 markers
[viter
->first
] = iter
->second
.max_marker
;
11667 class RGWGetBucketStatsContext
: public RGWGetDirHeader_CB
{
11668 RGWGetBucketStats_CB
*cb
;
11670 map
<RGWObjCategory
, RGWStorageStats
> stats
;
11676 RGWGetBucketStatsContext(RGWGetBucketStats_CB
*_cb
, uint32_t _pendings
)
11677 : cb(_cb
), pendings(_pendings
), stats(), ret_code(0), should_cb(true),
11678 lock("RGWGetBucketStatsContext") {}
11680 void handle_response(int r
, rgw_bucket_dir_header
& header
) override
{
11681 Mutex::Locker
l(lock
);
11684 accumulate_raw_stats(header
, stats
);
11689 // Are we all done?
11690 if (--pendings
== 0) {
11692 cb
->set_response(&stats
);
11694 cb
->handle_response(ret_code
);
11701 Mutex::Locker
l(lock
);
11706 int RGWRados::get_bucket_stats_async(RGWBucketInfo
& bucket_info
, int shard_id
, RGWGetBucketStats_CB
*ctx
)
11709 RGWGetBucketStatsContext
*get_ctx
= new RGWGetBucketStatsContext(ctx
, bucket_info
.num_shards
? : 1);
11711 int r
= cls_bucket_head_async(bucket_info
, shard_id
, get_ctx
, &num_aio
);
11715 get_ctx
->unset_cb();
11722 class RGWGetUserStatsContext
: public RGWGetUserHeader_CB
{
11723 RGWGetUserStats_CB
*cb
;
11726 explicit RGWGetUserStatsContext(RGWGetUserStats_CB
* const cb
)
11729 void handle_response(int r
, cls_user_header
& header
) override
{
11730 const cls_user_stats
& hs
= header
.stats
;
11732 RGWStorageStats stats
;
11734 stats
.size
= hs
.total_bytes
;
11735 stats
.size_rounded
= hs
.total_bytes_rounded
;
11736 stats
.num_objects
= hs
.total_entries
;
11738 cb
->set_response(stats
);
11741 cb
->handle_response(r
);
11747 int RGWRados::get_user_stats(const rgw_user
& user
, RGWStorageStats
& stats
)
11749 string user_str
= user
.to_str();
11751 cls_user_header header
;
11752 int r
= cls_user_get_header(user_str
, &header
);
11756 const cls_user_stats
& hs
= header
.stats
;
11758 stats
.size
= hs
.total_bytes
;
11759 stats
.size_rounded
= hs
.total_bytes_rounded
;
11760 stats
.num_objects
= hs
.total_entries
;
11765 int RGWRados::get_user_stats_async(const rgw_user
& user
, RGWGetUserStats_CB
*ctx
)
11767 string user_str
= user
.to_str();
11769 RGWGetUserStatsContext
*get_ctx
= new RGWGetUserStatsContext(ctx
);
11770 int r
= cls_user_get_header_async(user_str
, get_ctx
);
11780 void RGWRados::get_bucket_meta_oid(const rgw_bucket
& bucket
, string
& oid
)
11782 oid
= RGW_BUCKET_INSTANCE_MD_PREFIX
+ bucket
.get_key(':');
11785 void RGWRados::get_bucket_instance_obj(const rgw_bucket
& bucket
, rgw_raw_obj
& obj
)
11787 if (!bucket
.oid
.empty()) {
11788 obj
.init(get_zone_params().domain_root
, bucket
.oid
);
11791 get_bucket_meta_oid(bucket
, oid
);
11792 obj
.init(get_zone_params().domain_root
, oid
);
11796 int RGWRados::get_bucket_instance_info(RGWObjectCtx
& obj_ctx
, const string
& meta_key
, RGWBucketInfo
& info
,
11797 real_time
*pmtime
, map
<string
, bufferlist
> *pattrs
)
11799 size_t pos
= meta_key
.find(':');
11800 if (pos
== string::npos
) {
11803 string oid
= RGW_BUCKET_INSTANCE_MD_PREFIX
+ meta_key
;
11804 rgw_bucket_instance_key_to_oid(oid
);
11806 return get_bucket_instance_from_oid(obj_ctx
, oid
, info
, pmtime
, pattrs
);
11809 int RGWRados::get_bucket_instance_info(RGWObjectCtx
& obj_ctx
, const rgw_bucket
& bucket
, RGWBucketInfo
& info
,
11810 real_time
*pmtime
, map
<string
, bufferlist
> *pattrs
)
11813 if (bucket
.oid
.empty()) {
11814 get_bucket_meta_oid(bucket
, oid
);
11819 return get_bucket_instance_from_oid(obj_ctx
, oid
, info
, pmtime
, pattrs
);
11822 int RGWRados::get_bucket_instance_from_oid(RGWObjectCtx
& obj_ctx
, const string
& oid
, RGWBucketInfo
& info
,
11823 real_time
*pmtime
, map
<string
, bufferlist
> *pattrs
,
11824 rgw_cache_entry_info
*cache_info
)
11826 ldout(cct
, 20) << "reading from " << get_zone_params().domain_root
<< ":" << oid
<< dendl
;
11830 int ret
= rgw_get_system_obj(this, obj_ctx
, get_zone_params().domain_root
, oid
, epbl
, &info
.objv_tracker
, pmtime
, pattrs
, cache_info
);
11835 bufferlist::iterator iter
= epbl
.begin();
11837 ::decode(info
, iter
);
11838 } catch (buffer::error
& err
) {
11839 ldout(cct
, 0) << "ERROR: could not decode buffer info, caught buffer::error" << dendl
;
11842 info
.bucket
.oid
= oid
;
11846 int RGWRados::get_bucket_entrypoint_info(RGWObjectCtx
& obj_ctx
,
11847 const string
& tenant_name
,
11848 const string
& bucket_name
,
11849 RGWBucketEntryPoint
& entry_point
,
11850 RGWObjVersionTracker
*objv_tracker
,
11852 map
<string
, bufferlist
> *pattrs
,
11853 rgw_cache_entry_info
*cache_info
)
11856 string bucket_entry
;
11858 rgw_make_bucket_entry_name(tenant_name
, bucket_name
, bucket_entry
);
11859 int ret
= rgw_get_system_obj(this, obj_ctx
, get_zone_params().domain_root
, bucket_entry
, bl
, objv_tracker
, pmtime
, pattrs
, cache_info
);
11864 bufferlist::iterator iter
= bl
.begin();
11866 ::decode(entry_point
, iter
);
11867 } catch (buffer::error
& err
) {
11868 ldout(cct
, 0) << "ERROR: could not decode buffer info, caught buffer::error" << dendl
;
11874 int RGWRados::convert_old_bucket_info(RGWObjectCtx
& obj_ctx
,
11875 const string
& tenant_name
,
11876 const string
& bucket_name
)
11878 RGWBucketEntryPoint entry_point
;
11879 real_time ep_mtime
;
11880 RGWObjVersionTracker ot
;
11881 map
<string
, bufferlist
> attrs
;
11882 RGWBucketInfo info
;
11884 ldout(cct
, 10) << "RGWRados::convert_old_bucket_info(): bucket=" << bucket_name
<< dendl
;
11886 int ret
= get_bucket_entrypoint_info(obj_ctx
, tenant_name
, bucket_name
, entry_point
, &ot
, &ep_mtime
, &attrs
);
11888 ldout(cct
, 0) << "ERROR: get_bucket_entrypoint_info() returned " << ret
<< " bucket=" << bucket_name
<< dendl
;
11892 if (!entry_point
.has_bucket_info
) {
11893 /* already converted! */
11897 info
= entry_point
.old_bucket_info
;
11898 info
.bucket
.oid
= bucket_name
;
11899 info
.ep_objv
= ot
.read_version
;
11901 ot
.generate_new_write_ver(cct
);
11903 ret
= put_linked_bucket_info(info
, false, ep_mtime
, &ot
.write_version
, &attrs
, true);
11905 ldout(cct
, 0) << "ERROR: failed to put_linked_bucket_info(): " << ret
<< dendl
;
11912 int RGWRados::get_bucket_info(RGWObjectCtx
& obj_ctx
,
11913 const string
& tenant
, const string
& bucket_name
, RGWBucketInfo
& info
,
11914 real_time
*pmtime
, map
<string
, bufferlist
> *pattrs
)
11916 bucket_info_entry e
;
11917 string bucket_entry
;
11918 rgw_make_bucket_entry_name(tenant
, bucket_name
, bucket_entry
);
11920 if (binfo_cache
->find(bucket_entry
, &e
)) {
11929 RGWBucketEntryPoint entry_point
;
11930 real_time ep_mtime
;
11931 RGWObjVersionTracker ot
;
11932 rgw_cache_entry_info entry_cache_info
;
11933 int ret
= get_bucket_entrypoint_info(obj_ctx
, tenant
, bucket_name
, entry_point
, &ot
, &ep_mtime
, pattrs
, &entry_cache_info
);
11935 /* only init these fields */
11936 info
.bucket
.tenant
= tenant
;
11937 info
.bucket
.name
= bucket_name
;
11941 if (entry_point
.has_bucket_info
) {
11942 info
= entry_point
.old_bucket_info
;
11943 info
.bucket
.oid
= bucket_name
;
11944 info
.bucket
.tenant
= tenant
;
11945 info
.ep_objv
= ot
.read_version
;
11946 ldout(cct
, 20) << "rgw_get_bucket_info: old bucket info, bucket=" << info
.bucket
<< " owner " << info
.owner
<< dendl
;
11950 /* data is in the bucket instance object, we need to get attributes from there, clear everything
11957 ldout(cct
, 20) << "rgw_get_bucket_info: bucket instance: " << entry_point
.bucket
<< dendl
;
11960 /* read bucket instance info */
11963 get_bucket_meta_oid(entry_point
.bucket
, oid
);
11965 rgw_cache_entry_info cache_info
;
11967 ret
= get_bucket_instance_from_oid(obj_ctx
, oid
, e
.info
, &e
.mtime
, &e
.attrs
, &cache_info
);
11968 e
.info
.ep_objv
= ot
.read_version
;
11971 info
.bucket
.tenant
= tenant
;
11972 info
.bucket
.name
= bucket_name
;
11973 // XXX and why return anything in case of an error anyway?
11982 list
<rgw_cache_entry_info
*> cache_info_entries
;
11983 cache_info_entries
.push_back(&entry_cache_info
);
11984 cache_info_entries
.push_back(&cache_info
);
11987 /* chain to both bucket entry point and bucket instance */
11988 if (!binfo_cache
->put(this, bucket_entry
, &e
, cache_info_entries
)) {
11989 ldout(cct
, 20) << "couldn't put binfo cache entry, might have raced with data changes" << dendl
;
11995 int RGWRados::put_bucket_entrypoint_info(const string
& tenant_name
, const string
& bucket_name
, RGWBucketEntryPoint
& entry_point
,
11996 bool exclusive
, RGWObjVersionTracker
& objv_tracker
, real_time mtime
,
11997 map
<string
, bufferlist
> *pattrs
)
12000 ::encode(entry_point
, epbl
);
12001 string bucket_entry
;
12002 rgw_make_bucket_entry_name(tenant_name
, bucket_name
, bucket_entry
);
12003 return rgw_bucket_store_info(this, bucket_entry
, epbl
, exclusive
, pattrs
, &objv_tracker
, mtime
);
12006 int RGWRados::put_bucket_instance_info(RGWBucketInfo
& info
, bool exclusive
,
12007 real_time mtime
, map
<string
, bufferlist
> *pattrs
)
12009 info
.has_instance_obj
= true;
12012 ::encode(info
, bl
);
12014 string key
= info
.bucket
.get_key(); /* when we go through meta api, we don't use oid directly */
12015 int ret
= rgw_bucket_instance_store_info(this, key
, bl
, exclusive
, pattrs
, &info
.objv_tracker
, mtime
);
12016 if (ret
== -EEXIST
) {
12017 /* well, if it's exclusive we shouldn't overwrite it, because we might race with another
12018 * bucket operation on this specific bucket (e.g., being synced from the master), but
12019 * since bucket instace meta object is unique for this specific bucket instace, we don't
12020 * need to return an error.
12021 * A scenario where we'd get -EEXIST here, is in a multi-zone config, we're not on the
12022 * master, creating a bucket, sending bucket creation to the master, we create the bucket
12023 * locally, while in the sync thread we sync the new bucket.
12030 int RGWRados::put_linked_bucket_info(RGWBucketInfo
& info
, bool exclusive
, real_time mtime
, obj_version
*pep_objv
,
12031 map
<string
, bufferlist
> *pattrs
, bool create_entry_point
)
12033 bool create_head
= !info
.has_instance_obj
|| create_entry_point
;
12035 int ret
= put_bucket_instance_info(info
, exclusive
, mtime
, pattrs
);
12041 return 0; /* done! */
12043 RGWBucketEntryPoint entry_point
;
12044 entry_point
.bucket
= info
.bucket
;
12045 entry_point
.owner
= info
.owner
;
12046 entry_point
.creation_time
= info
.creation_time
;
12047 entry_point
.linked
= true;
12048 RGWObjVersionTracker ot
;
12049 if (pep_objv
&& !pep_objv
->tag
.empty()) {
12050 ot
.write_version
= *pep_objv
;
12052 ot
.generate_new_write_ver(cct
);
12054 *pep_objv
= ot
.write_version
;
12057 ret
= put_bucket_entrypoint_info(info
.bucket
.tenant
, info
.bucket
.name
, entry_point
, exclusive
, ot
, mtime
, NULL
);
12064 int RGWRados::omap_get_vals(rgw_raw_obj
& obj
, bufferlist
& header
, const string
& marker
, uint64_t count
, std::map
<string
, bufferlist
>& m
)
12067 int r
= get_raw_obj_ref(obj
, &ref
);
12072 r
= ref
.ioctx
.omap_get_vals(ref
.oid
, marker
, count
, &m
);
12080 int RGWRados::omap_get_all(rgw_raw_obj
& obj
, bufferlist
& header
,
12081 std::map
<string
, bufferlist
>& m
)
12084 int r
= get_raw_obj_ref(obj
, &ref
);
12089 #define MAX_OMAP_GET_ENTRIES 1024
12090 const int count
= MAX_OMAP_GET_ENTRIES
;
12091 string start_after
;
12094 std::map
<string
, bufferlist
> t
;
12095 r
= ref
.ioctx
.omap_get_vals(ref
.oid
, start_after
, count
, &t
);
12102 start_after
= t
.rbegin()->first
;
12103 m
.insert(t
.begin(), t
.end());
12108 int RGWRados::omap_set(rgw_raw_obj
& obj
, const std::string
& key
, bufferlist
& bl
)
12111 int r
= get_raw_obj_ref(obj
, &ref
);
12115 ldout(cct
, 15) << "omap_set obj=" << obj
<< " key=" << key
<< dendl
;
12117 map
<string
, bufferlist
> m
;
12120 r
= ref
.ioctx
.omap_set(ref
.oid
, m
);
12125 int RGWRados::omap_set(rgw_raw_obj
& obj
, std::map
<std::string
, bufferlist
>& m
)
12128 int r
= get_raw_obj_ref(obj
, &ref
);
12133 r
= ref
.ioctx
.omap_set(ref
.oid
, m
);
12138 int RGWRados::omap_del(rgw_raw_obj
& obj
, const std::string
& key
)
12141 int r
= get_raw_obj_ref(obj
, &ref
);
12149 r
= ref
.ioctx
.omap_rm_keys(ref
.oid
, k
);
12153 int RGWRados::update_containers_stats(map
<string
, RGWBucketEnt
>& m
)
12155 RGWObjectCtx
obj_ctx(this);
12157 map
<string
, RGWBucketEnt
>::iterator iter
;
12158 for (iter
= m
.begin(); iter
!= m
.end(); ++iter
) {
12159 RGWBucketEnt
& ent
= iter
->second
;
12160 rgw_bucket
& bucket
= ent
.bucket
;
12163 ent
.size_rounded
= 0;
12165 map
<string
, rgw_bucket_dir_header
> headers
;
12167 RGWBucketInfo bucket_info
;
12168 int ret
= get_bucket_instance_info(obj_ctx
, bucket
, bucket_info
, NULL
, NULL
);
12173 int r
= cls_bucket_head(bucket_info
, RGW_NO_SHARD
, headers
);
12177 map
<string
, rgw_bucket_dir_header
>::iterator hiter
= headers
.begin();
12178 for (; hiter
!= headers
.end(); ++hiter
) {
12179 RGWObjCategory category
= main_category
;
12180 map
<uint8_t, struct rgw_bucket_category_stats
>::iterator iter
= (hiter
->second
.stats
).find((uint8_t)category
);
12181 if (iter
!= hiter
->second
.stats
.end()) {
12182 struct rgw_bucket_category_stats
& stats
= iter
->second
;
12183 ent
.count
+= stats
.num_entries
;
12184 ent
.size
+= stats
.total_size
;
12185 ent
.size_rounded
+= stats
.total_size_rounded
;
12193 int RGWRados::append_async(rgw_raw_obj
& obj
, size_t size
, bufferlist
& bl
)
12196 int r
= get_raw_obj_ref(obj
, &ref
);
12200 librados::Rados
*rad
= get_rados_handle();
12201 librados::AioCompletion
*completion
= rad
->aio_create_completion(NULL
, NULL
, NULL
);
12203 r
= ref
.ioctx
.aio_append(ref
.oid
, completion
, bl
, size
);
12204 completion
->release();
12208 int RGWRados::distribute(const string
& key
, bufferlist
& bl
)
12211 * we were called before watch was initialized. This can only happen if we're updating some system
12212 * config object (e.g., zone info) during init. Don't try to distribute the cache info for these
12213 * objects, they're currently only read on startup anyway.
12215 if (!watch_initialized
)
12219 pick_control_oid(key
, notify_oid
);
12221 ldout(cct
, 10) << "distributing notification oid=" << notify_oid
<< " bl.length()=" << bl
.length() << dendl
;
12222 return control_pool_ctx
.notify2(notify_oid
, bl
, 0, NULL
);
12225 int RGWRados::pool_iterate_begin(const rgw_pool
& pool
, RGWPoolIterCtx
& ctx
)
12227 librados::IoCtx
& io_ctx
= ctx
.io_ctx
;
12228 librados::NObjectIterator
& iter
= ctx
.iter
;
12230 int r
= open_pool_ctx(pool
, io_ctx
);
12234 iter
= io_ctx
.nobjects_begin();
12239 int RGWRados::pool_iterate_begin(const rgw_pool
& pool
, const string
& cursor
, RGWPoolIterCtx
& ctx
)
12241 librados::IoCtx
& io_ctx
= ctx
.io_ctx
;
12242 librados::NObjectIterator
& iter
= ctx
.iter
;
12244 int r
= open_pool_ctx(pool
, io_ctx
);
12248 librados::ObjectCursor oc
;
12249 if (!oc
.from_str(cursor
)) {
12250 ldout(cct
, 10) << "failed to parse cursor: " << cursor
<< dendl
;
12254 iter
= io_ctx
.nobjects_begin(oc
);
12259 string
RGWRados::pool_iterate_get_cursor(RGWPoolIterCtx
& ctx
)
12261 return ctx
.iter
.get_cursor().to_str();
12264 int RGWRados::pool_iterate(RGWPoolIterCtx
& ctx
, uint32_t num
, vector
<rgw_bucket_dir_entry
>& objs
,
12265 bool *is_truncated
, RGWAccessListFilter
*filter
)
12267 librados::IoCtx
& io_ctx
= ctx
.io_ctx
;
12268 librados::NObjectIterator
& iter
= ctx
.iter
;
12270 if (iter
== io_ctx
.nobjects_end())
12275 for (i
= 0; i
< num
&& iter
!= io_ctx
.nobjects_end(); ++i
, ++iter
) {
12276 rgw_bucket_dir_entry e
;
12278 string oid
= iter
->get_oid();
12279 ldout(cct
, 20) << "RGWRados::pool_iterate: got " << oid
<< dendl
;
12281 // fill it in with initial values; we may correct later
12282 if (filter
&& !filter
->filter(oid
, oid
))
12290 *is_truncated
= (iter
!= io_ctx
.nobjects_end());
12292 return objs
.size();
12294 struct RGWAccessListFilterPrefix
: public RGWAccessListFilter
{
12297 explicit RGWAccessListFilterPrefix(const string
& _prefix
) : prefix(_prefix
) {}
12298 bool filter(string
& name
, string
& key
) override
{
12299 return (prefix
.compare(key
.substr(0, prefix
.size())) == 0);
12303 int RGWRados::list_raw_objects_init(const rgw_pool
& pool
, const string
& marker
, RGWListRawObjsCtx
*ctx
)
12305 if (!ctx
->initialized
) {
12306 int r
= pool_iterate_begin(pool
, marker
, ctx
->iter_ctx
);
12308 ldout(cct
, 10) << "failed to list objects pool_iterate_begin() returned r=" << r
<< dendl
;
12311 ctx
->initialized
= true;
12316 int RGWRados::list_raw_objects_next(const string
& prefix_filter
, int max
,
12317 RGWListRawObjsCtx
& ctx
, list
<string
>& oids
,
12318 bool *is_truncated
)
12320 if (!ctx
.initialized
) {
12323 RGWAccessListFilterPrefix
filter(prefix_filter
);
12324 vector
<rgw_bucket_dir_entry
> objs
;
12325 int r
= pool_iterate(ctx
.iter_ctx
, max
, objs
, is_truncated
, &filter
);
12328 ldout(cct
, 10) << "failed to list objects pool_iterate returned r=" << r
<< dendl
;
12332 vector
<rgw_bucket_dir_entry
>::iterator iter
;
12333 for (iter
= objs
.begin(); iter
!= objs
.end(); ++iter
) {
12334 oids
.push_back(iter
->key
.name
);
12337 return oids
.size();
12340 int RGWRados::list_raw_objects(const rgw_pool
& pool
, const string
& prefix_filter
,
12341 int max
, RGWListRawObjsCtx
& ctx
, list
<string
>& oids
,
12342 bool *is_truncated
)
12344 if (!ctx
.initialized
) {
12345 int r
= list_raw_objects_init(pool
, string(), &ctx
);
12351 return list_raw_objects_next(prefix_filter
, max
, ctx
, oids
, is_truncated
);
12354 string
RGWRados::list_raw_objs_get_cursor(RGWListRawObjsCtx
& ctx
)
12356 return pool_iterate_get_cursor(ctx
.iter_ctx
);
12359 int RGWRados::list_bi_log_entries(RGWBucketInfo
& bucket_info
, int shard_id
, string
& marker
, uint32_t max
,
12360 std::list
<rgw_bi_log_entry
>& result
, bool *truncated
)
12362 ldout(cct
, 20) << __func__
<< ": " << bucket_info
.bucket
<< " marker " << marker
<< " shard_id=" << shard_id
<< " max " << max
<< dendl
;
12365 librados::IoCtx index_ctx
;
12366 map
<int, string
> oids
;
12367 map
<int, cls_rgw_bi_log_list_ret
> bi_log_lists
;
12368 map
<int, string
> bucket_instance_ids
;
12369 int r
= open_bucket_index(bucket_info
, index_ctx
, oids
, shard_id
, &bucket_instance_ids
);
12373 BucketIndexShardsManager marker_mgr
;
12374 bool has_shards
= (oids
.size() > 1 || shard_id
>= 0);
12375 // If there are multiple shards for the bucket index object, the marker
12376 // should have the pattern '{shard_id_1}#{shard_marker_1},{shard_id_2}#
12377 // {shard_marker_2}...', if there is no sharding, the bi_log_list should
12378 // only contain one record, and the key is the bucket instance id.
12379 r
= marker_mgr
.from_string(marker
, shard_id
);
12383 r
= CLSRGWIssueBILogList(index_ctx
, marker_mgr
, max
, oids
, bi_log_lists
, cct
->_conf
->rgw_bucket_index_max_aio
)();
12387 map
<int, list
<rgw_bi_log_entry
>::iterator
> vcurrents
;
12388 map
<int, list
<rgw_bi_log_entry
>::iterator
> vends
;
12390 *truncated
= false;
12392 map
<int, cls_rgw_bi_log_list_ret
>::iterator miter
= bi_log_lists
.begin();
12393 for (; miter
!= bi_log_lists
.end(); ++miter
) {
12394 int shard_id
= miter
->first
;
12395 vcurrents
[shard_id
] = miter
->second
.entries
.begin();
12396 vends
[shard_id
] = miter
->second
.entries
.end();
12398 *truncated
= (*truncated
|| miter
->second
.truncated
);
12403 bool has_more
= true;
12404 map
<int, list
<rgw_bi_log_entry
>::iterator
>::iterator viter
;
12405 map
<int, list
<rgw_bi_log_entry
>::iterator
>::iterator eiter
;
12406 while (total
< max
&& has_more
) {
12409 viter
= vcurrents
.begin();
12410 eiter
= vends
.begin();
12412 for (; total
< max
&& viter
!= vcurrents
.end(); ++viter
, ++eiter
) {
12413 assert (eiter
!= vends
.end());
12415 int shard_id
= viter
->first
;
12416 list
<rgw_bi_log_entry
>::iterator
& liter
= viter
->second
;
12418 if (liter
== eiter
->second
){
12421 rgw_bi_log_entry
& entry
= *(liter
);
12424 snprintf(buf
, sizeof(buf
), "%d", shard_id
);
12426 build_bucket_index_marker(buf
, entry
.id
, &tmp_id
);
12427 entry
.id
.swap(tmp_id
);
12429 marker_mgr
.add(shard_id
, entry
.id
);
12430 result
.push_back(entry
);
12438 for (viter
= vcurrents
.begin(), eiter
= vends
.begin(); viter
!= vcurrents
.end(); ++viter
, ++eiter
) {
12439 assert (eiter
!= vends
.end());
12440 *truncated
= (*truncated
|| (viter
->second
!= eiter
->second
));
12444 // Refresh marker, if there are multiple shards, the output will look like
12445 // '{shard_oid_1}#{shard_marker_1},{shard_oid_2}#{shard_marker_2}...',
12446 // if there is no sharding, the simply marker (without oid) is returned
12448 marker_mgr
.to_string(&marker
);
12450 if (!result
.empty()) {
12451 marker
= result
.rbegin()->id
;
12458 int RGWRados::trim_bi_log_entries(RGWBucketInfo
& bucket_info
, int shard_id
, string
& start_marker
, string
& end_marker
)
12460 librados::IoCtx index_ctx
;
12461 map
<int, string
> bucket_objs
;
12463 BucketIndexShardsManager start_marker_mgr
;
12464 BucketIndexShardsManager end_marker_mgr
;
12466 int r
= open_bucket_index(bucket_info
, index_ctx
, bucket_objs
, shard_id
);
12471 r
= start_marker_mgr
.from_string(start_marker
, shard_id
);
12476 r
= end_marker_mgr
.from_string(end_marker
, shard_id
);
12481 return CLSRGWIssueBILogTrim(index_ctx
, start_marker_mgr
, end_marker_mgr
, bucket_objs
,
12482 cct
->_conf
->rgw_bucket_index_max_aio
)();
12487 int RGWRados::resync_bi_log_entries(RGWBucketInfo
& bucket_info
, int shard_id
)
12489 librados::IoCtx index_ctx
;
12490 map
<int, string
> bucket_objs
;
12491 int r
= open_bucket_index(bucket_info
, index_ctx
, bucket_objs
, shard_id
);
12495 return CLSRGWIssueResyncBucketBILog(index_ctx
, bucket_objs
, cct
->_conf
->rgw_bucket_index_max_aio
)();
12498 int RGWRados::stop_bi_log_entries(RGWBucketInfo
& bucket_info
, int shard_id
)
12500 librados::IoCtx index_ctx
;
12501 map
<int, string
> bucket_objs
;
12502 int r
= open_bucket_index(bucket_info
, index_ctx
, bucket_objs
, shard_id
);
12506 return CLSRGWIssueBucketBILogStop(index_ctx
, bucket_objs
, cct
->_conf
->rgw_bucket_index_max_aio
)();
12509 int RGWRados::bi_get_instance(const RGWBucketInfo
& bucket_info
, rgw_obj
& obj
, rgw_bucket_dir_entry
*dirent
)
12512 int r
= get_obj_head_ref(bucket_info
, obj
, &ref
);
12517 rgw_cls_bi_entry bi_entry
;
12518 r
= bi_get(obj
.bucket
, obj
, InstanceIdx
, &bi_entry
);
12519 if (r
< 0 && r
!= -ENOENT
) {
12520 ldout(cct
, 0) << "ERROR: bi_get() returned r=" << r
<< dendl
;
12525 bufferlist::iterator iter
= bi_entry
.data
.begin();
12527 ::decode(*dirent
, iter
);
12528 } catch (buffer::error
& err
) {
12529 ldout(cct
, 0) << "ERROR: failed to decode bi_entry()" << dendl
;
12536 int RGWRados::bi_get(rgw_bucket
& bucket
, rgw_obj
& obj
, BIIndexType index_type
, rgw_cls_bi_entry
*entry
)
12538 BucketShard
bs(this);
12539 int ret
= bs
.init(bucket
, obj
);
12541 ldout(cct
, 5) << "bs.init() returned ret=" << ret
<< dendl
;
12545 cls_rgw_obj_key
key(obj
.key
.get_index_key_name(), obj
.key
.instance
);
12547 ret
= cls_rgw_bi_get(bs
.index_ctx
, bs
.bucket_obj
, index_type
, key
, entry
);
12554 void RGWRados::bi_put(ObjectWriteOperation
& op
, BucketShard
& bs
, rgw_cls_bi_entry
& entry
)
12556 cls_rgw_bi_put(op
, bs
.bucket_obj
, entry
);
12559 int RGWRados::bi_put(BucketShard
& bs
, rgw_cls_bi_entry
& entry
)
12561 int ret
= cls_rgw_bi_put(bs
.index_ctx
, bs
.bucket_obj
, entry
);
12568 int RGWRados::bi_put(rgw_bucket
& bucket
, rgw_obj
& obj
, rgw_cls_bi_entry
& entry
)
12570 BucketShard
bs(this);
12571 int ret
= bs
.init(bucket
, obj
);
12573 ldout(cct
, 5) << "bs.init() returned ret=" << ret
<< dendl
;
12577 return bi_put(bs
, entry
);
12580 int RGWRados::bi_list(rgw_bucket
& bucket
, const string
& obj_name
, const string
& marker
, uint32_t max
, list
<rgw_cls_bi_entry
> *entries
, bool *is_truncated
)
12582 rgw_obj
obj(bucket
, obj_name
);
12583 BucketShard
bs(this);
12584 int ret
= bs
.init(bucket
, obj
);
12586 ldout(cct
, 5) << "bs.init() returned ret=" << ret
<< dendl
;
12590 ret
= cls_rgw_bi_list(bs
.index_ctx
, bs
.bucket_obj
, obj_name
, marker
, max
, entries
, is_truncated
);
12591 if (ret
== -ENOENT
) {
12592 *is_truncated
= false;
12600 int RGWRados::bi_list(BucketShard
& bs
, const string
& filter_obj
, const string
& marker
, uint32_t max
, list
<rgw_cls_bi_entry
> *entries
, bool *is_truncated
)
12602 int ret
= cls_rgw_bi_list(bs
.index_ctx
, bs
.bucket_obj
, filter_obj
, marker
, max
, entries
, is_truncated
);
12609 int RGWRados::bi_remove(BucketShard
& bs
)
12611 int ret
= bs
.index_ctx
.remove(bs
.bucket_obj
);
12612 if (ret
== -ENOENT
) {
12616 ldout(cct
, 5) << "bs.index_ctx.remove(" << bs
.bucket_obj
<< ") returned ret=" << ret
<< dendl
;
12623 int RGWRados::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
)
12625 BucketShard
bs(this);
12626 int ret
= bs
.init(bucket
, shard_id
);
12628 ldout(cct
, 5) << "bs.init() returned ret=" << ret
<< dendl
;
12632 return bi_list(bs
, filter_obj
, marker
, max
, entries
, is_truncated
);
12635 int RGWRados::gc_operate(string
& oid
, librados::ObjectWriteOperation
*op
)
12637 return gc_pool_ctx
.operate(oid
, op
);
12640 int RGWRados::gc_aio_operate(string
& oid
, librados::ObjectWriteOperation
*op
)
12642 AioCompletion
*c
= librados::Rados::aio_create_completion(NULL
, NULL
, NULL
);
12643 int r
= gc_pool_ctx
.aio_operate(oid
, c
, op
);
12648 int RGWRados::gc_operate(string
& oid
, librados::ObjectReadOperation
*op
, bufferlist
*pbl
)
12650 return gc_pool_ctx
.operate(oid
, op
, pbl
);
12653 int RGWRados::list_gc_objs(int *index
, string
& marker
, uint32_t max
, bool expired_only
, std::list
<cls_rgw_gc_obj_info
>& result
, bool *truncated
)
12655 return gc
->list(index
, marker
, max
, expired_only
, result
, truncated
);
12658 int RGWRados::process_gc()
12660 return gc
->process();
12663 int RGWRados::list_lc_progress(const string
& marker
, uint32_t max_entries
, map
<string
, int> *progress_map
)
12665 return lc
->list_lc_progress(marker
, max_entries
, progress_map
);
12668 int RGWRados::process_lc()
12670 return lc
->process();
12673 int RGWRados::process_expire_objects()
12675 obj_expirer
->inspect_all_shards(utime_t(), ceph_clock_now());
12679 int RGWRados::cls_rgw_init_index(librados::IoCtx
& index_ctx
, librados::ObjectWriteOperation
& op
, string
& oid
)
12682 cls_rgw_bucket_init(op
);
12683 return index_ctx
.operate(oid
, &op
);
12686 int RGWRados::cls_obj_prepare_op(BucketShard
& bs
, RGWModifyOp op
, string
& tag
,
12687 rgw_obj
& obj
, uint16_t bilog_flags
, rgw_zone_set
*_zones_trace
)
12689 rgw_zone_set zones_trace
;
12690 if (_zones_trace
) {
12691 zones_trace
= *_zones_trace
;
12694 zones_trace
.insert(get_zone().id
);
12697 ObjectWriteOperation o
;
12698 cls_rgw_obj_key
key(obj
.key
.get_index_key_name(), obj
.key
.instance
);
12699 cls_rgw_guard_bucket_resharding(o
, -ERR_BUSY_RESHARDING
);
12700 cls_rgw_bucket_prepare_op(o
, op
, tag
, key
, obj
.key
.get_loc(), get_zone().log_data
, bilog_flags
, zones_trace
);
12701 return bs
.index_ctx
.operate(bs
.bucket_obj
, &o
);
12704 int RGWRados::cls_obj_complete_op(BucketShard
& bs
, const rgw_obj
& obj
, RGWModifyOp op
, string
& tag
,
12705 int64_t pool
, uint64_t epoch
,
12706 rgw_bucket_dir_entry
& ent
, RGWObjCategory category
,
12707 list
<rgw_obj_index_key
> *remove_objs
, uint16_t bilog_flags
, rgw_zone_set
*_zones_trace
)
12709 ObjectWriteOperation o
;
12710 rgw_bucket_dir_entry_meta dir_meta
;
12711 dir_meta
= ent
.meta
;
12712 dir_meta
.category
= category
;
12714 rgw_bucket_entry_ver ver
;
12717 cls_rgw_obj_key
key(ent
.key
.name
, ent
.key
.instance
);
12718 cls_rgw_guard_bucket_resharding(o
, -ERR_BUSY_RESHARDING
);
12719 cls_rgw_bucket_complete_op(o
, op
, tag
, ver
, key
, dir_meta
, remove_objs
,
12720 get_zone().log_data
, bilog_flags
, _zones_trace
);
12721 complete_op_data
*arg
;
12722 index_completion_manager
->create_completion(obj
, op
, tag
, ver
, key
, dir_meta
, remove_objs
,
12723 get_zone().log_data
, bilog_flags
, _zones_trace
, &arg
);
12724 librados::AioCompletion
*completion
= arg
->rados_completion
;
12725 int ret
= bs
.index_ctx
.aio_operate(bs
.bucket_obj
, arg
->rados_completion
, &o
);
12726 completion
->release(); /* can't reference arg here, as it might have already been released */
12730 int RGWRados::cls_obj_complete_add(BucketShard
& bs
, const rgw_obj
& obj
, string
& tag
,
12731 int64_t pool
, uint64_t epoch
,
12732 rgw_bucket_dir_entry
& ent
, RGWObjCategory category
,
12733 list
<rgw_obj_index_key
> *remove_objs
, uint16_t bilog_flags
, rgw_zone_set
*zones_trace
)
12735 return cls_obj_complete_op(bs
, obj
, CLS_RGW_OP_ADD
, tag
, pool
, epoch
, ent
, category
, remove_objs
, bilog_flags
, zones_trace
);
12738 int RGWRados::cls_obj_complete_del(BucketShard
& bs
, string
& tag
,
12739 int64_t pool
, uint64_t epoch
,
12741 real_time
& removed_mtime
,
12742 list
<rgw_obj_index_key
> *remove_objs
,
12743 uint16_t bilog_flags
,
12744 rgw_zone_set
*zones_trace
)
12746 rgw_bucket_dir_entry ent
;
12747 ent
.meta
.mtime
= removed_mtime
;
12748 obj
.key
.get_index_key(&ent
.key
);
12749 return cls_obj_complete_op(bs
, obj
, CLS_RGW_OP_DEL
, tag
, pool
, epoch
, ent
, RGW_OBJ_CATEGORY_NONE
, remove_objs
, bilog_flags
, zones_trace
);
12752 int RGWRados::cls_obj_complete_cancel(BucketShard
& bs
, string
& tag
, rgw_obj
& obj
, uint16_t bilog_flags
, rgw_zone_set
*zones_trace
)
12754 rgw_bucket_dir_entry ent
;
12755 obj
.key
.get_index_key(&ent
.key
);
12756 return cls_obj_complete_op(bs
, obj
, CLS_RGW_OP_CANCEL
, tag
, -1 /* pool id */, 0, ent
, RGW_OBJ_CATEGORY_NONE
, NULL
, bilog_flags
, zones_trace
);
12759 int RGWRados::cls_obj_set_bucket_tag_timeout(RGWBucketInfo
& bucket_info
, uint64_t timeout
)
12761 librados::IoCtx index_ctx
;
12762 map
<int, string
> bucket_objs
;
12763 int r
= open_bucket_index(bucket_info
, index_ctx
, bucket_objs
);
12767 return CLSRGWIssueSetTagTimeout(index_ctx
, bucket_objs
, cct
->_conf
->rgw_bucket_index_max_aio
, timeout
)();
12770 int RGWRados::cls_bucket_list(RGWBucketInfo
& bucket_info
, int shard_id
, rgw_obj_index_key
& start
, const string
& prefix
,
12771 uint32_t num_entries
, bool list_versions
, map
<string
, rgw_bucket_dir_entry
>& m
,
12772 bool *is_truncated
, rgw_obj_index_key
*last_entry
,
12773 bool (*force_check_filter
)(const string
& name
))
12775 ldout(cct
, 10) << "cls_bucket_list " << bucket_info
.bucket
<< " start " << start
.name
<< "[" << start
.instance
<< "] num_entries " << num_entries
<< dendl
;
12777 librados::IoCtx index_ctx
;
12778 // key - oid (for different shards if there is any)
12779 // value - list result for the corresponding oid (shard), it is filled by the AIO callback
12780 map
<int, string
> oids
;
12781 map
<int, struct rgw_cls_list_ret
> list_results
;
12782 int r
= open_bucket_index(bucket_info
, index_ctx
, oids
, shard_id
);
12786 cls_rgw_obj_key
start_key(start
.name
, start
.instance
);
12787 r
= CLSRGWIssueBucketList(index_ctx
, start_key
, prefix
, num_entries
, list_versions
,
12788 oids
, list_results
, cct
->_conf
->rgw_bucket_index_max_aio
)();
12792 // Create a list of iterators that are used to iterate each shard
12793 vector
<map
<string
, struct rgw_bucket_dir_entry
>::iterator
> vcurrents(list_results
.size());
12794 vector
<map
<string
, struct rgw_bucket_dir_entry
>::iterator
> vends(list_results
.size());
12795 vector
<string
> vnames(list_results
.size());
12796 map
<int, struct rgw_cls_list_ret
>::iterator iter
= list_results
.begin();
12797 *is_truncated
= false;
12798 for (; iter
!= list_results
.end(); ++iter
) {
12799 vcurrents
.push_back(iter
->second
.dir
.m
.begin());
12800 vends
.push_back(iter
->second
.dir
.m
.end());
12801 vnames
.push_back(oids
[iter
->first
]);
12802 *is_truncated
= (*is_truncated
|| iter
->second
.is_truncated
);
12805 // Create a map to track the next candidate entry from each shard, if the entry
12806 // from a specified shard is selected/erased, the next entry from that shard will
12807 // be inserted for next round selection
12808 map
<string
, size_t> candidates
;
12809 for (size_t i
= 0; i
< vcurrents
.size(); ++i
) {
12810 if (vcurrents
[i
] != vends
[i
]) {
12811 candidates
[vcurrents
[i
]->first
] = i
;
12815 map
<string
, bufferlist
> updates
;
12816 uint32_t count
= 0;
12817 while (count
< num_entries
&& !candidates
.empty()) {
12819 // Select the next one
12820 int pos
= candidates
.begin()->second
;
12821 const string
& name
= vcurrents
[pos
]->first
;
12822 struct rgw_bucket_dir_entry
& dirent
= vcurrents
[pos
]->second
;
12824 bool force_check
= force_check_filter
&& force_check_filter(dirent
.key
.name
);
12825 if ((!dirent
.exists
&& !dirent
.is_delete_marker()) || !dirent
.pending_map
.empty() || force_check
) {
12826 /* there are uncommitted ops. We need to check the current state,
12827 * and if the tags are old we need to do cleanup as well. */
12828 librados::IoCtx sub_ctx
;
12829 sub_ctx
.dup(index_ctx
);
12830 r
= check_disk_state(sub_ctx
, bucket_info
, dirent
, dirent
, updates
[vnames
[pos
]]);
12831 if (r
< 0 && r
!= -ENOENT
) {
12836 ldout(cct
, 10) << "RGWRados::cls_bucket_list: got " << dirent
.key
.name
<< "[" << dirent
.key
.instance
<< "]" << dendl
;
12837 m
[name
] = std::move(dirent
);
12841 // Refresh the candidates map
12842 candidates
.erase(candidates
.begin());
12844 if (vcurrents
[pos
] != vends
[pos
]) {
12845 candidates
[vcurrents
[pos
]->first
] = pos
;
12849 // Suggest updates if there is any
12850 map
<string
, bufferlist
>::iterator miter
= updates
.begin();
12851 for (; miter
!= updates
.end(); ++miter
) {
12852 if (miter
->second
.length()) {
12853 ObjectWriteOperation o
;
12854 cls_rgw_suggest_changes(o
, miter
->second
);
12855 // we don't care if we lose suggested updates, send them off blindly
12856 AioCompletion
*c
= librados::Rados::aio_create_completion(NULL
, NULL
, NULL
);
12857 index_ctx
.aio_operate(miter
->first
, c
, &o
);
12862 // Check if all the returned entries are consumed or not
12863 for (size_t i
= 0; i
< vcurrents
.size(); ++i
) {
12864 if (vcurrents
[i
] != vends
[i
])
12865 *is_truncated
= true;
12868 *last_entry
= m
.rbegin()->first
;
12873 int RGWRados::cls_obj_usage_log_add(const string
& oid
, rgw_usage_log_info
& info
)
12875 rgw_raw_obj
obj(get_zone_params().usage_log_pool
, oid
);
12878 int r
= get_raw_obj_ref(obj
, &ref
);
12883 ObjectWriteOperation op
;
12884 cls_rgw_usage_log_add(op
, info
);
12886 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
12890 int RGWRados::cls_obj_usage_log_read(string
& oid
, string
& user
, uint64_t start_epoch
, uint64_t end_epoch
, uint32_t max_entries
,
12891 string
& read_iter
, map
<rgw_user_bucket
, rgw_usage_log_entry
>& usage
, bool *is_truncated
)
12893 rgw_raw_obj
obj(get_zone_params().usage_log_pool
, oid
);
12896 int r
= get_raw_obj_ref(obj
, &ref
);
12901 *is_truncated
= false;
12903 r
= cls_rgw_usage_log_read(ref
.ioctx
, ref
.oid
, user
, start_epoch
, end_epoch
,
12904 max_entries
, read_iter
, usage
, is_truncated
);
12909 int RGWRados::cls_obj_usage_log_trim(string
& oid
, string
& user
, uint64_t start_epoch
, uint64_t end_epoch
)
12911 rgw_raw_obj
obj(get_zone_params().usage_log_pool
, oid
);
12914 int r
= get_raw_obj_ref(obj
, &ref
);
12919 ObjectWriteOperation op
;
12920 cls_rgw_usage_log_trim(op
, user
, start_epoch
, end_epoch
);
12922 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
12926 int RGWRados::remove_objs_from_index(RGWBucketInfo
& bucket_info
, list
<rgw_obj_index_key
>& oid_list
)
12928 librados::IoCtx index_ctx
;
12931 uint8_t suggest_flag
= (get_zone().log_data
? CEPH_RGW_DIR_SUGGEST_LOG_OP
: 0);
12933 int r
= open_bucket_index(bucket_info
, index_ctx
, dir_oid
);
12937 bufferlist updates
;
12939 for (auto iter
= oid_list
.begin(); iter
!= oid_list
.end(); ++iter
) {
12940 rgw_bucket_dir_entry entry
;
12942 dout(2) << "RGWRados::remove_objs_from_index bucket=" << bucket_info
.bucket
<< " obj=" << entry
.key
.name
<< ":" << entry
.key
.instance
<< dendl
;
12943 entry
.ver
.epoch
= (uint64_t)-1; // ULLONG_MAX, needed to that objclass doesn't skip out request
12944 updates
.append(CEPH_RGW_REMOVE
| suggest_flag
);
12945 ::encode(entry
, updates
);
12950 r
= index_ctx
.exec(dir_oid
, RGW_CLASS
, RGW_DIR_SUGGEST_CHANGES
, updates
, out
);
12955 int RGWRados::check_disk_state(librados::IoCtx io_ctx
,
12956 const RGWBucketInfo
& bucket_info
,
12957 rgw_bucket_dir_entry
& list_state
,
12958 rgw_bucket_dir_entry
& object
,
12959 bufferlist
& suggested_updates
)
12961 const rgw_bucket
& bucket
= bucket_info
.bucket
;
12962 uint8_t suggest_flag
= (get_zone().log_data
? CEPH_RGW_DIR_SUGGEST_LOG_OP
: 0);
12966 rgw_obj
obj(bucket
, list_state
.key
);
12969 get_obj_bucket_and_oid_loc(obj
, oid
, loc
);
12971 if (loc
!= list_state
.locator
) {
12972 ldout(cct
, 0) << "WARNING: generated locator (" << loc
<< ") is different from listed locator (" << list_state
.locator
<< ")" << dendl
;
12975 io_ctx
.locator_set_key(list_state
.locator
);
12977 RGWObjState
*astate
= NULL
;
12978 RGWObjectCtx
rctx(this);
12979 int r
= get_obj_state(&rctx
, bucket_info
, obj
, &astate
, false);
12983 list_state
.pending_map
.clear(); // we don't need this and it inflates size
12984 if (!astate
->exists
) {
12985 /* object doesn't exist right now -- hopefully because it's
12986 * marked as !exists and got deleted */
12987 if (list_state
.exists
) {
12988 /* FIXME: what should happen now? Work out if there are any
12989 * non-bad ways this could happen (there probably are, but annoying
12992 // encode a suggested removal of that key
12993 list_state
.ver
.epoch
= io_ctx
.get_last_version();
12994 list_state
.ver
.pool
= io_ctx
.get_id();
12995 cls_rgw_encode_suggestion(CEPH_RGW_REMOVE
, list_state
, suggested_updates
);
13000 string content_type
;
13003 object
.meta
.size
= astate
->size
;
13004 object
.meta
.accounted_size
= astate
->accounted_size
;
13005 object
.meta
.mtime
= astate
->mtime
;
13007 map
<string
, bufferlist
>::iterator iter
= astate
->attrset
.find(RGW_ATTR_ETAG
);
13008 if (iter
!= astate
->attrset
.end()) {
13009 etag
= iter
->second
.c_str();
13011 iter
= astate
->attrset
.find(RGW_ATTR_CONTENT_TYPE
);
13012 if (iter
!= astate
->attrset
.end()) {
13013 content_type
= iter
->second
.c_str();
13015 iter
= astate
->attrset
.find(RGW_ATTR_ACL
);
13016 if (iter
!= astate
->attrset
.end()) {
13017 r
= decode_policy(iter
->second
, &owner
);
13019 dout(0) << "WARNING: could not decode policy for object: " << obj
<< dendl
;
13023 if (astate
->has_manifest
) {
13024 RGWObjManifest::obj_iterator miter
;
13025 RGWObjManifest
& manifest
= astate
->manifest
;
13026 for (miter
= manifest
.obj_begin(); miter
!= manifest
.obj_end(); ++miter
) {
13027 const rgw_raw_obj
& raw_loc
= miter
.get_location().get_raw_obj(this);
13029 rgw_raw_obj_to_obj(manifest
.get_obj().bucket
, raw_loc
, &loc
);
13031 if (loc
.key
.ns
== RGW_OBJ_NS_MULTIPART
) {
13032 dout(10) << "check_disk_state(): removing manifest part from index: " << loc
<< dendl
;
13033 r
= delete_obj_index(loc
);
13035 dout(0) << "WARNING: delete_obj_index() returned r=" << r
<< dendl
;
13041 object
.meta
.etag
= etag
;
13042 object
.meta
.content_type
= content_type
;
13043 object
.meta
.owner
= owner
.get_id().to_str();
13044 object
.meta
.owner_display_name
= owner
.get_display_name();
13046 // encode suggested updates
13047 list_state
.ver
.pool
= io_ctx
.get_id();
13048 list_state
.ver
.epoch
= astate
->epoch
;
13049 list_state
.meta
.size
= object
.meta
.size
;
13050 list_state
.meta
.accounted_size
= object
.meta
.accounted_size
;
13051 list_state
.meta
.mtime
= object
.meta
.mtime
;
13052 list_state
.meta
.category
= main_category
;
13053 list_state
.meta
.etag
= etag
;
13054 list_state
.meta
.content_type
= content_type
;
13055 if (astate
->obj_tag
.length() > 0)
13056 list_state
.tag
= astate
->obj_tag
.c_str();
13057 list_state
.meta
.owner
= owner
.get_id().to_str();
13058 list_state
.meta
.owner_display_name
= owner
.get_display_name();
13060 list_state
.exists
= true;
13061 cls_rgw_encode_suggestion(CEPH_RGW_UPDATE
| suggest_flag
, list_state
, suggested_updates
);
13065 int RGWRados::cls_bucket_head(const RGWBucketInfo
& bucket_info
, int shard_id
, map
<string
, struct rgw_bucket_dir_header
>& headers
, map
<int, string
> *bucket_instance_ids
)
13067 librados::IoCtx index_ctx
;
13068 map
<int, string
> oids
;
13069 map
<int, struct rgw_cls_list_ret
> list_results
;
13070 int r
= open_bucket_index(bucket_info
, index_ctx
, oids
, list_results
, shard_id
, bucket_instance_ids
);
13074 r
= CLSRGWIssueGetDirHeader(index_ctx
, oids
, list_results
, cct
->_conf
->rgw_bucket_index_max_aio
)();
13078 map
<int, struct rgw_cls_list_ret
>::iterator iter
= list_results
.begin();
13079 for(; iter
!= list_results
.end(); ++iter
) {
13080 headers
[oids
[iter
->first
]] = iter
->second
.dir
.header
;
13085 int RGWRados::cls_bucket_head_async(const RGWBucketInfo
& bucket_info
, int shard_id
, RGWGetDirHeader_CB
*ctx
, int *num_aio
)
13087 librados::IoCtx index_ctx
;
13088 map
<int, string
> bucket_objs
;
13089 int r
= open_bucket_index(bucket_info
, index_ctx
, bucket_objs
, shard_id
);
13093 map
<int, string
>::iterator iter
= bucket_objs
.begin();
13094 for (; iter
!= bucket_objs
.end(); ++iter
) {
13095 r
= cls_rgw_get_dir_header_async(index_ctx
, iter
->second
, static_cast<RGWGetDirHeader_CB
*>(ctx
->get()));
13106 int RGWRados::cls_user_get_header(const string
& user_id
, cls_user_header
*header
)
13108 string buckets_obj_id
;
13109 rgw_get_buckets_obj(user_id
, buckets_obj_id
);
13110 rgw_raw_obj
obj(get_zone_params().user_uid_pool
, buckets_obj_id
);
13113 int r
= get_raw_obj_ref(obj
, &ref
);
13118 librados::ObjectReadOperation op
;
13120 ::cls_user_get_header(op
, header
, &rc
);
13122 r
= ref
.ioctx
.operate(ref
.oid
, &op
, &ibl
);
13131 int RGWRados::cls_user_get_header_async(const string
& user_id
, RGWGetUserHeader_CB
*ctx
)
13133 string buckets_obj_id
;
13134 rgw_get_buckets_obj(user_id
, buckets_obj_id
);
13135 rgw_raw_obj
obj(get_zone_params().user_uid_pool
, buckets_obj_id
);
13138 int r
= get_raw_obj_ref(obj
, &ref
);
13143 r
= ::cls_user_get_header_async(ref
.ioctx
, ref
.oid
, ctx
);
13150 int RGWRados::cls_user_sync_bucket_stats(rgw_raw_obj
& user_obj
, const RGWBucketInfo
& bucket_info
)
13152 map
<string
, struct rgw_bucket_dir_header
> headers
;
13153 int r
= cls_bucket_head(bucket_info
, RGW_NO_SHARD
, headers
);
13155 ldout(cct
, 20) << "cls_bucket_header() returned " << r
<< dendl
;
13159 cls_user_bucket_entry entry
;
13161 bucket_info
.bucket
.convert(&entry
.bucket
);
13163 for (const auto& hiter
: headers
) {
13164 for (const auto& iter
: hiter
.second
.stats
) {
13165 const struct rgw_bucket_category_stats
& header_stats
= iter
.second
;
13166 entry
.size
+= header_stats
.total_size
;
13167 entry
.size_rounded
+= header_stats
.total_size_rounded
;
13168 entry
.count
+= header_stats
.num_entries
;
13172 list
<cls_user_bucket_entry
> entries
;
13173 entries
.push_back(entry
);
13175 r
= cls_user_update_buckets(user_obj
, entries
, false);
13177 ldout(cct
, 20) << "cls_user_update_buckets() returned " << r
<< dendl
;
13184 int RGWRados::cls_user_get_bucket_stats(const rgw_bucket
& bucket
, cls_user_bucket_entry
& entry
)
13186 map
<string
, struct rgw_bucket_dir_header
> headers
;
13187 RGWBucketInfo bucket_info
;
13188 RGWObjectCtx
obj_ctx(this);
13189 int ret
= get_bucket_instance_info(obj_ctx
, bucket
, bucket_info
, NULL
, NULL
);
13194 ret
= cls_bucket_head(bucket_info
, RGW_NO_SHARD
, headers
);
13196 ldout(cct
, 20) << "cls_bucket_header() returned " << ret
<< dendl
;
13200 bucket
.convert(&entry
.bucket
);
13202 for (const auto& hiter
: headers
) {
13203 for (const auto& iter
: hiter
.second
.stats
) {
13204 const struct rgw_bucket_category_stats
& header_stats
= iter
.second
;
13205 entry
.size
+= header_stats
.total_size
;
13206 entry
.size_rounded
+= header_stats
.total_size_rounded
;
13207 entry
.count
+= header_stats
.num_entries
;
13214 int RGWRados::cls_user_list_buckets(rgw_raw_obj
& obj
,
13215 const string
& in_marker
,
13216 const string
& end_marker
,
13217 const int max_entries
,
13218 list
<cls_user_bucket_entry
>& entries
,
13219 string
* const out_marker
,
13220 bool * const truncated
)
13223 int r
= get_raw_obj_ref(obj
, &ref
);
13228 librados::ObjectReadOperation op
;
13231 cls_user_bucket_list(op
, in_marker
, end_marker
, max_entries
, entries
, out_marker
, truncated
, &rc
);
13233 r
= ref
.ioctx
.operate(ref
.oid
, &op
, &ibl
);
13242 int RGWRados::cls_user_update_buckets(rgw_raw_obj
& obj
, list
<cls_user_bucket_entry
>& entries
, bool add
)
13245 int r
= get_raw_obj_ref(obj
, &ref
);
13250 librados::ObjectWriteOperation op
;
13251 cls_user_set_buckets(op
, entries
, add
);
13252 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
13259 int RGWRados::complete_sync_user_stats(const rgw_user
& user_id
)
13261 string buckets_obj_id
;
13262 rgw_get_buckets_obj(user_id
, buckets_obj_id
);
13263 rgw_raw_obj
obj(get_zone_params().user_uid_pool
, buckets_obj_id
);
13264 return cls_user_complete_stats_sync(obj
);
13267 int RGWRados::cls_user_complete_stats_sync(rgw_raw_obj
& obj
)
13270 int r
= get_raw_obj_ref(obj
, &ref
);
13275 librados::ObjectWriteOperation op
;
13276 ::cls_user_complete_stats_sync(op
);
13277 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
13284 int RGWRados::cls_user_add_bucket(rgw_raw_obj
& obj
, const cls_user_bucket_entry
& entry
)
13286 list
<cls_user_bucket_entry
> l
;
13287 l
.push_back(entry
);
13289 return cls_user_update_buckets(obj
, l
, true);
13292 int RGWRados::cls_user_remove_bucket(rgw_raw_obj
& obj
, const cls_user_bucket
& bucket
)
13295 int r
= get_system_obj_ref(obj
, &ref
);
13300 librados::ObjectWriteOperation op
;
13301 ::cls_user_remove_bucket(op
, bucket
);
13302 r
= ref
.ioctx
.operate(ref
.oid
, &op
);
13309 int RGWRados::check_bucket_shards(const RGWBucketInfo
& bucket_info
, const rgw_bucket
& bucket
,
13310 RGWQuotaInfo
& bucket_quota
)
13312 if (!cct
->_conf
->rgw_dynamic_resharding
) {
13316 bool need_resharding
= false;
13317 int num_source_shards
= (bucket_info
.num_shards
> 0 ? bucket_info
.num_shards
: 1);
13318 uint32_t suggested_num_shards
;
13320 int ret
= quota_handler
->check_bucket_shards((uint64_t)cct
->_conf
->rgw_max_objs_per_shard
,
13321 num_source_shards
, bucket_info
.owner
, bucket
, bucket_quota
,
13322 1, need_resharding
, &suggested_num_shards
);
13327 if (need_resharding
) {
13328 ldout(cct
, 20) << __func__
<< " bucket " << bucket
.name
<< " need resharding " <<
13329 " old num shards " << bucket_info
.num_shards
<< " new num shards " << suggested_num_shards
<<
13331 return add_bucket_to_reshard(bucket_info
, suggested_num_shards
);
13337 int RGWRados::add_bucket_to_reshard(const RGWBucketInfo
& bucket_info
, uint32_t new_num_shards
)
13339 RGWReshard
reshard(this);
13341 uint32_t num_source_shards
= (bucket_info
.num_shards
> 0 ? bucket_info
.num_shards
: 1);
13343 new_num_shards
= min(new_num_shards
, get_max_bucket_shards());
13344 if (new_num_shards
<= num_source_shards
) {
13345 ldout(cct
, 20) << "not resharding bucket name=" << bucket_info
.bucket
.name
<< ", orig_num=" << num_source_shards
<< ", new_num_shards=" << new_num_shards
<< dendl
;
13349 cls_rgw_reshard_entry entry
;
13350 entry
.time
= real_clock::now();
13351 entry
.tenant
= bucket_info
.owner
.tenant
;
13352 entry
.bucket_name
= bucket_info
.bucket
.name
;
13353 entry
.bucket_id
= bucket_info
.bucket
.bucket_id
;
13354 entry
.old_num_shards
= num_source_shards
;
13355 entry
.new_num_shards
= new_num_shards
;
13357 return reshard
.add(entry
);
13360 int RGWRados::check_quota(const rgw_user
& bucket_owner
, rgw_bucket
& bucket
,
13361 RGWQuotaInfo
& user_quota
, RGWQuotaInfo
& bucket_quota
, uint64_t obj_size
)
13363 return quota_handler
->check_quota(bucket_owner
, bucket
, user_quota
, bucket_quota
, 1, obj_size
);
13366 void RGWRados::get_bucket_index_objects(const string
& bucket_oid_base
,
13367 uint32_t num_shards
, map
<int, string
>& bucket_objects
, int shard_id
)
13370 bucket_objects
[0] = bucket_oid_base
;
13372 char buf
[bucket_oid_base
.size() + 32];
13373 if (shard_id
< 0) {
13374 for (uint32_t i
= 0; i
< num_shards
; ++i
) {
13375 snprintf(buf
, sizeof(buf
), "%s.%d", bucket_oid_base
.c_str(), i
);
13376 bucket_objects
[i
] = buf
;
13379 if ((uint32_t)shard_id
> num_shards
) {
13382 snprintf(buf
, sizeof(buf
), "%s.%d", bucket_oid_base
.c_str(), shard_id
);
13383 bucket_objects
[shard_id
] = buf
;
13388 void RGWRados::get_bucket_instance_ids(const RGWBucketInfo
& bucket_info
, int shard_id
, map
<int, string
> *result
)
13390 const rgw_bucket
& bucket
= bucket_info
.bucket
;
13391 string plain_id
= bucket
.name
+ ":" + bucket
.bucket_id
;
13392 if (!bucket_info
.num_shards
) {
13393 (*result
)[0] = plain_id
;
13396 if (shard_id
< 0) {
13397 for (uint32_t i
= 0; i
< bucket_info
.num_shards
; ++i
) {
13398 snprintf(buf
, sizeof(buf
), ":%d", i
);
13399 (*result
)[i
] = plain_id
+ buf
;
13402 if ((uint32_t)shard_id
> bucket_info
.num_shards
) {
13405 snprintf(buf
, sizeof(buf
), ":%d", shard_id
);
13406 (*result
)[shard_id
] = plain_id
+ buf
;
13411 int RGWRados::get_target_shard_id(const RGWBucketInfo
& bucket_info
, const string
& obj_key
,
13415 switch (bucket_info
.bucket_index_shard_hash_type
) {
13416 case RGWBucketInfo::MOD
:
13417 if (!bucket_info
.num_shards
) {
13422 uint32_t sid
= ceph_str_hash_linux(obj_key
.c_str(), obj_key
.size());
13423 uint32_t sid2
= sid
^ ((sid
& 0xFF) << 24);
13424 sid
= rgw_shards_mod(sid2
, bucket_info
.num_shards
);
13426 *shard_id
= (int)sid
;
13436 void RGWRados::get_bucket_index_object(const string
& bucket_oid_base
, uint32_t num_shards
,
13437 int shard_id
, string
*bucket_obj
)
13440 // By default with no sharding, we use the bucket oid as itself
13441 (*bucket_obj
) = bucket_oid_base
;
13443 char buf
[bucket_oid_base
.size() + 32];
13444 snprintf(buf
, sizeof(buf
), "%s.%d", bucket_oid_base
.c_str(), shard_id
);
13445 (*bucket_obj
) = buf
;
13449 int RGWRados::get_bucket_index_object(const string
& bucket_oid_base
, const string
& obj_key
,
13450 uint32_t num_shards
, RGWBucketInfo::BIShardsHashType hash_type
, string
*bucket_obj
, int *shard_id
)
13453 switch (hash_type
) {
13454 case RGWBucketInfo::MOD
:
13456 // By default with no sharding, we use the bucket oid as itself
13457 (*bucket_obj
) = bucket_oid_base
;
13462 uint32_t sid
= ceph_str_hash_linux(obj_key
.c_str(), obj_key
.size());
13463 uint32_t sid2
= sid
^ ((sid
& 0xFF) << 24);
13464 sid
= rgw_shards_mod(sid2
, num_shards
);
13465 char buf
[bucket_oid_base
.size() + 32];
13466 snprintf(buf
, sizeof(buf
), "%s.%d", bucket_oid_base
.c_str(), sid
);
13467 (*bucket_obj
) = buf
;
13469 *shard_id
= (int)sid
;
13479 void RGWStateLog::oid_str(int shard
, string
& oid
) {
13480 oid
= RGW_STATELOG_OBJ_PREFIX
+ module_name
+ ".";
13482 snprintf(buf
, sizeof(buf
), "%d", shard
);
13486 int RGWStateLog::get_shard_num(const string
& object
) {
13487 uint32_t val
= ceph_str_hash_linux(object
.c_str(), object
.length());
13488 return val
% num_shards
;
13491 string
RGWStateLog::get_oid(const string
& object
) {
13492 int shard
= get_shard_num(object
);
13494 oid_str(shard
, oid
);
13498 int RGWStateLog::open_ioctx(librados::IoCtx
& ioctx
) {
13500 store
->get_log_pool(pool
);
13501 int r
= rgw_init_ioctx(store
->get_rados_handle(), pool
, ioctx
);
13503 lderr(store
->ctx()) << "ERROR: could not open rados pool" << dendl
;
13509 int RGWStateLog::store_entry(const string
& client_id
, const string
& op_id
, const string
& object
,
13510 uint32_t state
, bufferlist
*bl
, uint32_t *check_state
)
13512 if (client_id
.empty() ||
13515 ldout(store
->ctx(), 0) << "client_id / op_id / object is empty" << dendl
;
13518 librados::IoCtx ioctx
;
13519 int r
= open_ioctx(ioctx
);
13523 string oid
= get_oid(object
);
13525 librados::ObjectWriteOperation op
;
13527 cls_statelog_check_state(op
, client_id
, op_id
, object
, *check_state
);
13529 utime_t ts
= ceph_clock_now();
13531 cls_statelog_add(op
, client_id
, op_id
, object
, ts
, state
, (bl
? *bl
: nobl
));
13532 r
= ioctx
.operate(oid
, &op
);
13540 int RGWStateLog::remove_entry(const string
& client_id
, const string
& op_id
, const string
& object
)
13542 if (client_id
.empty() ||
13545 ldout(store
->ctx(), 0) << "client_id / op_id / object is empty" << dendl
;
13548 librados::IoCtx ioctx
;
13549 int r
= open_ioctx(ioctx
);
13553 string oid
= get_oid(object
);
13555 librados::ObjectWriteOperation op
;
13556 cls_statelog_remove_by_object(op
, object
, op_id
);
13557 r
= ioctx
.operate(oid
, &op
);
13565 void RGWStateLog::init_list_entries(const string
& client_id
, const string
& op_id
, const string
& object
,
13568 list_state
*state
= new list_state
;
13569 state
->client_id
= client_id
;
13570 state
->op_id
= op_id
;
13571 state
->object
= object
;
13572 if (object
.empty()) {
13573 state
->cur_shard
= 0;
13574 state
->max_shard
= num_shards
- 1;
13576 state
->cur_shard
= state
->max_shard
= get_shard_num(object
);
13578 *handle
= (void *)state
;
13581 int RGWStateLog::list_entries(void *handle
, int max_entries
,
13582 list
<cls_statelog_entry
>& entries
,
13585 list_state
*state
= static_cast<list_state
*>(handle
);
13587 librados::IoCtx ioctx
;
13588 int r
= open_ioctx(ioctx
);
13594 for (; state
->cur_shard
<= state
->max_shard
&& max_entries
> 0; ++state
->cur_shard
) {
13596 oid_str(state
->cur_shard
, oid
);
13598 librados::ObjectReadOperation op
;
13599 list
<cls_statelog_entry
> ents
;
13601 cls_statelog_list(op
, state
->client_id
, state
->op_id
, state
->object
, state
->marker
,
13602 max_entries
, ents
, &state
->marker
, &truncated
);
13604 r
= ioctx
.operate(oid
, &op
, &ibl
);
13605 if (r
== -ENOENT
) {
13610 ldout(store
->ctx(), 0) << "cls_statelog_list returned " << r
<< dendl
;
13615 state
->marker
.clear();
13618 max_entries
-= ents
.size();
13620 entries
.splice(entries
.end(), ents
);
13626 *done
= (state
->cur_shard
> state
->max_shard
);
13631 void RGWStateLog::finish_list_entries(void *handle
)
13633 list_state
*state
= static_cast<list_state
*>(handle
);
13637 void RGWStateLog::dump_entry(const cls_statelog_entry
& entry
, Formatter
*f
)
13639 f
->open_object_section("statelog_entry");
13640 f
->dump_string("client_id", entry
.client_id
);
13641 f
->dump_string("op_id", entry
.op_id
);
13642 f
->dump_string("object", entry
.object
);
13643 entry
.timestamp
.gmtime_nsec(f
->dump_stream("timestamp"));
13644 if (!dump_entry_internal(entry
, f
)) {
13645 f
->dump_int("state", entry
.state
);
13647 f
->close_section();
13650 RGWOpState::RGWOpState(RGWRados
*_store
) : RGWStateLog(_store
, _store
->ctx()->_conf
->rgw_num_zone_opstate_shards
, string("obj_opstate"))
13654 bool RGWOpState::dump_entry_internal(const cls_statelog_entry
& entry
, Formatter
*f
)
13657 switch ((OpState
)entry
.state
) {
13658 case OPSTATE_UNKNOWN
:
13661 case OPSTATE_IN_PROGRESS
:
13664 case OPSTATE_COMPLETE
:
13667 case OPSTATE_ERROR
:
13670 case OPSTATE_ABORT
:
13673 case OPSTATE_CANCELLED
:
13679 f
->dump_string("state", s
);
13683 int RGWOpState::state_from_str(const string
& s
, OpState
*state
)
13685 if (s
== "unknown") {
13686 *state
= OPSTATE_UNKNOWN
;
13687 } else if (s
== "in-progress") {
13688 *state
= OPSTATE_IN_PROGRESS
;
13689 } else if (s
== "complete") {
13690 *state
= OPSTATE_COMPLETE
;
13691 } else if (s
== "error") {
13692 *state
= OPSTATE_ERROR
;
13693 } else if (s
== "abort") {
13694 *state
= OPSTATE_ABORT
;
13695 } else if (s
== "cancelled") {
13696 *state
= OPSTATE_CANCELLED
;
13704 int RGWOpState::set_state(const string
& client_id
, const string
& op_id
, const string
& object
, OpState state
)
13706 uint32_t s
= (uint32_t)state
;
13707 return store_entry(client_id
, op_id
, object
, s
, NULL
, NULL
);
13710 int RGWOpState::renew_state(const string
& client_id
, const string
& op_id
, const string
& object
, OpState state
)
13712 uint32_t s
= (uint32_t)state
;
13713 return store_entry(client_id
, op_id
, object
, s
, NULL
, &s
);
13716 RGWOpStateSingleOp::RGWOpStateSingleOp(RGWRados
*store
, const string
& cid
, const string
& oid
,
13717 const string
& obj
) : os(store
), client_id(cid
), op_id(oid
), object(obj
)
13719 cct
= store
->ctx();
13720 cur_state
= RGWOpState::OPSTATE_UNKNOWN
;
13723 int RGWOpStateSingleOp::set_state(RGWOpState::OpState state
) {
13724 last_update
= real_clock::now();
13726 return os
.set_state(client_id
, op_id
, object
, state
);
13729 int RGWOpStateSingleOp::renew_state() {
13730 real_time now
= real_clock::now();
13732 int rate_limit_sec
= cct
->_conf
->rgw_opstate_ratelimit_sec
;
13734 if (rate_limit_sec
&& now
- last_update
< make_timespan(rate_limit_sec
)) {
13739 return os
.renew_state(client_id
, op_id
, object
, cur_state
);
13743 uint64_t RGWRados::instance_id()
13745 return get_rados_handle()->get_instance_id();
13748 uint64_t RGWRados::next_bucket_id()
13750 Mutex::Locker
l(bucket_id_lock
);
13751 return ++max_bucket_id
;
13754 RGWRados
*RGWStoreManager::init_storage_provider(CephContext
*cct
, bool use_gc_thread
, bool use_lc_thread
, bool quota_threads
, bool run_sync_thread
, bool run_reshard_thread
)
13756 int use_cache
= cct
->_conf
->rgw_cache_enabled
;
13757 RGWRados
*store
= NULL
;
13759 store
= new RGWRados
;
13761 store
= new RGWCache
<RGWRados
>;
13764 if (store
->initialize(cct
, use_gc_thread
, use_lc_thread
, quota_threads
, run_sync_thread
, run_reshard_thread
) < 0) {
13772 RGWRados
*RGWStoreManager::init_raw_storage_provider(CephContext
*cct
)
13774 RGWRados
*store
= NULL
;
13775 store
= new RGWRados
;
13777 store
->set_context(cct
);
13779 if (store
->init_rados() < 0) {
13787 void RGWStoreManager::close_storage(RGWRados
*store
)
13797 librados::Rados
* RGWRados::get_rados_handle()
13799 if (rados
.size() == 1) {
13802 handle_lock
.get_read();
13803 pthread_t id
= pthread_self();
13804 std::map
<pthread_t
, int>:: iterator it
= rados_map
.find(id
);
13806 if (it
!= rados_map
.end()) {
13807 handle_lock
.put_read();
13808 return &rados
[it
->second
];
13810 handle_lock
.put_read();
13811 handle_lock
.get_write();
13812 const uint32_t handle
= next_rados_handle
;
13813 rados_map
[id
] = handle
;
13814 if (++next_rados_handle
== rados
.size()) {
13815 next_rados_handle
= 0;
13817 handle_lock
.put_write();
13818 return &rados
[handle
];
13823 int RGWRados::delete_raw_obj_aio(const rgw_raw_obj
& obj
, list
<librados::AioCompletion
*>& handles
)
13826 int ret
= get_raw_obj_ref(obj
, &ref
);
13828 lderr(cct
) << "ERROR: failed to get obj ref with ret=" << ret
<< dendl
;
13832 ObjectWriteOperation op
;
13833 list
<string
> prefixes
;
13834 cls_rgw_remove_obj(op
, prefixes
);
13836 AioCompletion
*c
= librados::Rados::aio_create_completion(NULL
, NULL
, NULL
);
13837 ret
= ref
.ioctx
.aio_operate(ref
.oid
, c
, &op
);
13839 lderr(cct
) << "ERROR: AioOperate failed with ret=" << ret
<< dendl
;
13844 handles
.push_back(c
);
13849 int RGWRados::delete_obj_aio(const rgw_obj
& obj
,
13850 RGWBucketInfo
& bucket_info
, RGWObjState
*astate
,
13851 list
<librados::AioCompletion
*>& handles
, bool keep_index_consistent
)
13854 int ret
= get_obj_head_ref(bucket_info
, obj
, &ref
);
13856 lderr(cct
) << "ERROR: failed to get obj ref with ret=" << ret
<< dendl
;
13860 if (keep_index_consistent
) {
13861 RGWRados::Bucket
bop(this, bucket_info
);
13862 RGWRados::Bucket::UpdateIndex
index_op(&bop
, obj
);
13864 ret
= index_op
.prepare(CLS_RGW_OP_DEL
, &astate
->write_tag
);
13866 lderr(cct
) << "ERROR: failed to prepare index op with ret=" << ret
<< dendl
;
13871 ObjectWriteOperation op
;
13872 list
<string
> prefixes
;
13873 cls_rgw_remove_obj(op
, prefixes
);
13875 AioCompletion
*c
= librados::Rados::aio_create_completion(NULL
, NULL
, NULL
);
13876 ret
= ref
.ioctx
.aio_operate(ref
.oid
, c
, &op
);
13878 lderr(cct
) << "ERROR: AioOperate failed with ret=" << ret
<< dendl
;
13883 handles
.push_back(c
);
13885 if (keep_index_consistent
) {
13886 ret
= delete_obj_index(obj
);
13888 lderr(cct
) << "ERROR: failed to delete obj index with ret=" << ret
<< dendl
;
13895 int rgw_compression_info_from_attrset(map
<string
, bufferlist
>& attrs
, bool& need_decompress
, RGWCompressionInfo
& cs_info
) {
13896 map
<string
, bufferlist
>::iterator value
= attrs
.find(RGW_ATTR_COMPRESSION
);
13897 if (value
!= attrs
.end()) {
13898 bufferlist::iterator bliter
= value
->second
.begin();
13900 ::decode(cs_info
, bliter
);
13901 } catch (buffer::error
& err
) {
13904 if (cs_info
.blocks
.size() == 0) {
13907 if (cs_info
.compression_type
!= "none")
13908 need_decompress
= true;
13910 need_decompress
= false;
13913 need_decompress
= false;