1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
10 #include <boost/utility/string_ref.hpp>
11 #include <boost/format.hpp>
13 #include "common/errno.h"
14 #include "common/ceph_json.h"
15 #include "rgw_rados.h"
17 #include "rgw_acl_s3.h"
19 #include "include/types.h"
20 #include "rgw_bucket.h"
22 #include "rgw_string.h"
24 #include "include/rados/librados.hpp"
25 // until everything is moved from rgw_common
26 #include "rgw_common.h"
28 #include "cls/user/cls_user_types.h"
30 #define dout_context g_ceph_context
31 #define dout_subsys ceph_subsys_rgw
33 #define BUCKET_TAG_TIMEOUT 30
37 static RGWMetadataHandler
*bucket_meta_handler
= NULL
;
38 static RGWMetadataHandler
*bucket_instance_meta_handler
= NULL
;
40 // define as static when RGWBucket implementation compete
41 void rgw_get_buckets_obj(const rgw_user
& user_id
, string
& buckets_obj_id
)
43 buckets_obj_id
= user_id
.to_str();
44 buckets_obj_id
+= RGW_BUCKETS_OBJ_SUFFIX
;
48 * Note that this is not a reversal of parse_bucket(). That one deals
49 * with the syntax we need in metadata and such. This one deals with
50 * the representation in RADOS pools. We chose '/' because it's not
51 * acceptable in bucket names and thus qualified buckets cannot conflict
52 * with the legacy or S3 buckets.
54 std::string
rgw_make_bucket_entry_name(const std::string
& tenant_name
,
55 const std::string
& bucket_name
) {
56 std::string bucket_entry
;
58 if (bucket_name
.empty()) {
60 } else if (tenant_name
.empty()) {
61 bucket_entry
= bucket_name
;
63 bucket_entry
= tenant_name
+ "/" + bucket_name
;
70 * Tenants are separated from buckets in URLs by a colon in S3.
71 * This function is not to be used on Swift URLs, not even for COPY arguments.
73 void rgw_parse_url_bucket(const string
&bucket
, const string
& auth_tenant
,
74 string
&tenant_name
, string
&bucket_name
) {
76 int pos
= bucket
.find(':');
79 * N.B.: We allow ":bucket" syntax with explicit empty tenant in order
80 * to refer to the legacy tenant, in case users in new named tenants
81 * want to access old global buckets.
83 tenant_name
= bucket
.substr(0, pos
);
84 bucket_name
= bucket
.substr(pos
+ 1);
86 tenant_name
= auth_tenant
;
92 * Get all the buckets owned by a user and fill up an RGWUserBuckets with them.
93 * Returns: 0 on success, -ERR# on failure.
95 int rgw_read_user_buckets(RGWRados
* store
,
96 const rgw_user
& user_id
,
97 RGWUserBuckets
& buckets
,
99 const string
& end_marker
,
103 uint64_t default_amount
)
107 string buckets_obj_id
;
108 rgw_get_buckets_obj(user_id
, buckets_obj_id
);
109 rgw_raw_obj
obj(store
->get_zone_params().user_uid_pool
, buckets_obj_id
);
110 list
<cls_user_bucket_entry
> entries
;
112 bool truncated
= false;
118 max
= default_amount
;
122 ret
= store
->cls_user_list_buckets(obj
, m
, end_marker
, max
- total
, entries
, &m
, &truncated
);
129 for (const auto& entry
: entries
) {
130 buckets
.add(RGWBucketEnt(user_id
, entry
));
134 } while (truncated
&& total
< max
);
136 if (is_truncated
!= nullptr) {
137 *is_truncated
= truncated
;
141 map
<string
, RGWBucketEnt
>& m
= buckets
.get_buckets();
142 ret
= store
->update_containers_stats(m
);
143 if (ret
< 0 && ret
!= -ENOENT
) {
144 ldout(store
->ctx(), 0) << "ERROR: could not get stats for buckets" << dendl
;
151 int rgw_bucket_sync_user_stats(RGWRados
*store
, const rgw_user
& user_id
, const RGWBucketInfo
& bucket_info
)
153 string buckets_obj_id
;
154 rgw_get_buckets_obj(user_id
, buckets_obj_id
);
155 rgw_raw_obj
obj(store
->get_zone_params().user_uid_pool
, buckets_obj_id
);
157 return store
->cls_user_sync_bucket_stats(obj
, bucket_info
);
160 int rgw_bucket_sync_user_stats(RGWRados
*store
, const string
& tenant_name
, const string
& bucket_name
)
162 RGWBucketInfo bucket_info
;
163 RGWObjectCtx
obj_ctx(store
);
164 int ret
= store
->get_bucket_info(obj_ctx
, tenant_name
, bucket_name
, bucket_info
, NULL
);
166 ldout(store
->ctx(), 0) << "ERROR: could not fetch bucket info: ret=" << ret
<< dendl
;
170 ret
= rgw_bucket_sync_user_stats(store
, bucket_info
.owner
, bucket_info
);
172 ldout(store
->ctx(), 0) << "ERROR: could not sync user stats for bucket " << bucket_name
<< ": ret=" << ret
<< dendl
;
179 int rgw_link_bucket(RGWRados
*store
, const rgw_user
& user_id
, rgw_bucket
& bucket
, real_time creation_time
, bool update_entrypoint
)
182 string
& tenant_name
= bucket
.tenant
;
183 string
& bucket_name
= bucket
.name
;
185 cls_user_bucket_entry new_bucket
;
187 RGWBucketEntryPoint ep
;
188 RGWObjVersionTracker ot
;
190 bucket
.convert(&new_bucket
.bucket
);
192 if (real_clock::is_zero(creation_time
))
193 new_bucket
.creation_time
= real_clock::now();
195 new_bucket
.creation_time
= creation_time
;
197 map
<string
, bufferlist
> attrs
;
198 RGWObjectCtx
obj_ctx(store
);
200 if (update_entrypoint
) {
201 ret
= store
->get_bucket_entrypoint_info(obj_ctx
, tenant_name
, bucket_name
, ep
, &ot
, NULL
, &attrs
);
202 if (ret
< 0 && ret
!= -ENOENT
) {
203 ldout(store
->ctx(), 0) << "ERROR: store->get_bucket_entrypoint_info() returned: "
204 << cpp_strerror(-ret
) << dendl
;
208 string buckets_obj_id
;
209 rgw_get_buckets_obj(user_id
, buckets_obj_id
);
211 rgw_raw_obj
obj(store
->get_zone_params().user_uid_pool
, buckets_obj_id
);
212 ret
= store
->cls_user_add_bucket(obj
, new_bucket
);
214 ldout(store
->ctx(), 0) << "ERROR: error adding bucket to directory: "
215 << cpp_strerror(-ret
) << dendl
;
219 if (!update_entrypoint
)
225 ret
= store
->put_bucket_entrypoint_info(tenant_name
, bucket_name
, ep
, false, ot
, real_time(), &attrs
);
231 int r
= rgw_unlink_bucket(store
, user_id
, bucket
.tenant
, bucket
.name
);
233 ldout(store
->ctx(), 0) << "ERROR: failed unlinking bucket on error cleanup: "
234 << cpp_strerror(-r
) << dendl
;
239 int rgw_unlink_bucket(RGWRados
*store
, const rgw_user
& user_id
, const string
& tenant_name
, const string
& bucket_name
, bool update_entrypoint
)
243 string buckets_obj_id
;
244 rgw_get_buckets_obj(user_id
, buckets_obj_id
);
246 cls_user_bucket bucket
;
247 bucket
.name
= bucket_name
;
248 rgw_raw_obj
obj(store
->get_zone_params().user_uid_pool
, buckets_obj_id
);
249 ret
= store
->cls_user_remove_bucket(obj
, bucket
);
251 ldout(store
->ctx(), 0) << "ERROR: error removing bucket from directory: "
252 << cpp_strerror(-ret
)<< dendl
;
255 if (!update_entrypoint
)
258 RGWBucketEntryPoint ep
;
259 RGWObjVersionTracker ot
;
260 map
<string
, bufferlist
> attrs
;
261 RGWObjectCtx
obj_ctx(store
);
262 ret
= store
->get_bucket_entrypoint_info(obj_ctx
, tenant_name
, bucket_name
, ep
, &ot
, NULL
, &attrs
);
271 if (ep
.owner
!= user_id
) {
272 ldout(store
->ctx(), 0) << "bucket entry point user mismatch, can't unlink bucket: " << ep
.owner
<< " != " << user_id
<< dendl
;
277 return store
->put_bucket_entrypoint_info(tenant_name
, bucket_name
, ep
, false, ot
, real_time(), &attrs
);
280 int rgw_bucket_store_info(RGWRados
*store
, const string
& bucket_name
, bufferlist
& bl
, bool exclusive
,
281 map
<string
, bufferlist
> *pattrs
, RGWObjVersionTracker
*objv_tracker
,
283 return store
->meta_mgr
->put_entry(bucket_meta_handler
, bucket_name
, bl
, exclusive
, objv_tracker
, mtime
, pattrs
);
286 int rgw_bucket_instance_store_info(RGWRados
*store
, string
& entry
, bufferlist
& bl
, bool exclusive
,
287 map
<string
, bufferlist
> *pattrs
, RGWObjVersionTracker
*objv_tracker
,
289 return store
->meta_mgr
->put_entry(bucket_instance_meta_handler
, entry
, bl
, exclusive
, objv_tracker
, mtime
, pattrs
);
292 int rgw_bucket_instance_remove_entry(RGWRados
*store
, string
& entry
, RGWObjVersionTracker
*objv_tracker
) {
293 return store
->meta_mgr
->remove_entry(bucket_instance_meta_handler
, entry
, objv_tracker
);
296 // 'tenant/' is used in bucket instance keys for sync to avoid parsing ambiguity
297 // with the existing instance[:shard] format. once we parse the shard, the / is
298 // replaced with a : to match the [tenant:]instance format
299 void rgw_bucket_instance_key_to_oid(string
& key
)
301 // replace tenant/ with tenant:
302 auto c
= key
.find('/');
303 if (c
!= string::npos
) {
308 // convert bucket instance oids back to the tenant/ format for metadata keys.
309 // it's safe to parse 'tenant:' only for oids, because they won't contain the
310 // optional :shard at the end
311 void rgw_bucket_instance_oid_to_key(string
& oid
)
313 // find first : (could be tenant:bucket or bucket:instance)
314 auto c
= oid
.find(':');
315 if (c
!= string::npos
) {
316 // if we find another :, the first one was for tenant
317 if (oid
.find(':', c
+ 1) != string::npos
) {
323 int rgw_bucket_parse_bucket_instance(const string
& bucket_instance
, string
*target_bucket_instance
, int *shard_id
)
325 ssize_t pos
= bucket_instance
.rfind(':');
330 string first
= bucket_instance
.substr(0, pos
);
331 string second
= bucket_instance
.substr(pos
+ 1);
333 if (first
.find(':') == string::npos
) {
335 *target_bucket_instance
= bucket_instance
;
339 *target_bucket_instance
= first
;
341 *shard_id
= strict_strtol(second
.c_str(), 10, &err
);
349 // parse key in format: [tenant/]name:instance[:shard_id]
350 int rgw_bucket_parse_bucket_key(CephContext
*cct
, const string
& key
,
351 rgw_bucket
*bucket
, int *shard_id
)
353 boost::string_ref name
{key
};
354 boost::string_ref instance
;
357 auto pos
= name
.find('/');
358 if (pos
!= boost::string_ref::npos
) {
359 auto tenant
= name
.substr(0, pos
);
360 bucket
->tenant
.assign(tenant
.begin(), tenant
.end());
361 name
= name
.substr(pos
+ 1);
364 // split name:instance
365 pos
= name
.find(':');
366 if (pos
!= boost::string_ref::npos
) {
367 instance
= name
.substr(pos
+ 1);
368 name
= name
.substr(0, pos
);
370 bucket
->name
.assign(name
.begin(), name
.end());
372 // split instance:shard
373 pos
= instance
.find(':');
374 if (pos
== boost::string_ref::npos
) {
375 bucket
->bucket_id
.assign(instance
.begin(), instance
.end());
381 auto shard
= instance
.substr(pos
+ 1);
383 auto id
= strict_strtol(shard
.data(), 10, &err
);
385 ldout(cct
, 0) << "ERROR: failed to parse bucket shard '"
386 << instance
.data() << "': " << err
<< dendl
;
391 instance
= instance
.substr(0, pos
);
392 bucket
->bucket_id
.assign(instance
.begin(), instance
.end());
396 int rgw_bucket_set_attrs(RGWRados
*store
, RGWBucketInfo
& bucket_info
,
397 map
<string
, bufferlist
>& attrs
,
398 RGWObjVersionTracker
*objv_tracker
)
400 rgw_bucket
& bucket
= bucket_info
.bucket
;
402 if (!bucket_info
.has_instance_obj
) {
403 /* an old bucket object, need to convert it */
404 RGWObjectCtx
obj_ctx(store
);
405 int ret
= store
->convert_old_bucket_info(obj_ctx
, bucket
.tenant
, bucket
.name
);
407 ldout(store
->ctx(), 0) << "ERROR: failed converting old bucket info: " << ret
<< dendl
;
412 /* we want the bucket instance name without the oid prefix cruft */
413 string key
= bucket
.get_key();
416 ::encode(bucket_info
, bl
);
418 return rgw_bucket_instance_store_info(store
, key
, bl
, false, &attrs
, objv_tracker
, real_time());
421 static void dump_mulipart_index_results(list
<rgw_obj_index_key
>& objs_to_unlink
,
424 // make sure that an appropiately titled header has been opened previously
425 auto oiter
= objs_to_unlink
.begin();
427 f
->open_array_section("invalid_multipart_entries");
429 for ( ; oiter
!= objs_to_unlink
.end(); ++oiter
) {
430 f
->dump_string("object", oiter
->name
);
436 void check_bad_user_bucket_mapping(RGWRados
*store
, const rgw_user
& user_id
,
439 RGWUserBuckets user_buckets
;
440 bool is_truncated
= false;
443 CephContext
*cct
= store
->ctx();
445 size_t max_entries
= cct
->_conf
->rgw_list_buckets_max_chunk
;
448 int ret
= rgw_read_user_buckets(store
, user_id
, user_buckets
, marker
,
449 string(), max_entries
, false,
452 ldout(store
->ctx(), 0) << "failed to read user buckets: "
453 << cpp_strerror(-ret
) << dendl
;
457 map
<string
, RGWBucketEnt
>& buckets
= user_buckets
.get_buckets();
458 for (map
<string
, RGWBucketEnt
>::iterator i
= buckets
.begin();
463 RGWBucketEnt
& bucket_ent
= i
->second
;
464 rgw_bucket
& bucket
= bucket_ent
.bucket
;
466 RGWBucketInfo bucket_info
;
468 RGWObjectCtx
obj_ctx(store
);
469 int r
= store
->get_bucket_info(obj_ctx
, user_id
.tenant
, bucket
.name
, bucket_info
, &mtime
);
471 ldout(store
->ctx(), 0) << "could not get bucket info for bucket=" << bucket
<< dendl
;
475 rgw_bucket
& actual_bucket
= bucket_info
.bucket
;
477 if (actual_bucket
.name
.compare(bucket
.name
) != 0 ||
478 actual_bucket
.tenant
.compare(bucket
.tenant
) != 0 ||
479 actual_bucket
.marker
.compare(bucket
.marker
) != 0 ||
480 actual_bucket
.bucket_id
.compare(bucket
.bucket_id
) != 0) {
481 cout
<< "bucket info mismatch: expected " << actual_bucket
<< " got " << bucket
<< std::endl
;
483 cout
<< "fixing" << std::endl
;
484 r
= rgw_link_bucket(store
, user_id
, actual_bucket
, bucket_info
.creation_time
);
486 cerr
<< "failed to fix bucket: " << cpp_strerror(-r
) << std::endl
;
491 } while (is_truncated
);
494 static bool bucket_object_check_filter(const string
& oid
)
498 return rgw_obj_key::oid_to_key_in_ns(oid
, &key
, ns
);
501 int rgw_remove_object(RGWRados
*store
, RGWBucketInfo
& bucket_info
, rgw_bucket
& bucket
, rgw_obj_key
& key
)
503 RGWObjectCtx
rctx(store
);
505 if (key
.instance
.empty()) {
506 key
.instance
= "null";
509 rgw_obj
obj(bucket
, key
);
511 return store
->delete_obj(rctx
, bucket_info
, obj
, bucket_info
.versioning_status());
514 int rgw_remove_bucket(RGWRados
*store
, rgw_bucket
& bucket
, bool delete_children
)
517 map
<RGWObjCategory
, RGWStorageStats
> stats
;
518 std::vector
<rgw_bucket_dir_entry
> objs
;
519 map
<string
, bool> common_prefixes
;
521 RGWObjectCtx
obj_ctx(store
);
523 string bucket_ver
, master_ver
;
525 ret
= store
->get_bucket_info(obj_ctx
, bucket
.tenant
, bucket
.name
, info
, NULL
);
529 ret
= store
->get_bucket_stats(info
, RGW_NO_SHARD
, &bucket_ver
, &master_ver
, stats
, NULL
);
534 RGWRados::Bucket
target(store
, info
);
535 RGWRados::Bucket::List
list_op(&target
);
537 list_op
.params
.list_versions
= true;
539 if (delete_children
) {
545 ret
= list_op
.list_objects(max
, &objs
, &common_prefixes
, NULL
);
549 std::vector
<rgw_bucket_dir_entry
>::iterator it
= objs
.begin();
550 for (; it
!= objs
.end(); ++it
) {
551 rgw_obj_key
key(it
->key
);
552 ret
= rgw_remove_object(store
, info
, bucket
, key
);
557 } while (!objs
.empty());
561 ret
= rgw_bucket_sync_user_stats(store
, bucket
.tenant
, info
);
563 dout(1) << "WARNING: failed sync user stats before bucket delete. ret=" << ret
<< dendl
;
566 RGWObjVersionTracker objv_tracker
;
568 ret
= store
->delete_bucket(info
, objv_tracker
);
570 lderr(store
->ctx()) << "ERROR: could not remove bucket " << bucket
.name
<< dendl
;
574 ret
= rgw_unlink_bucket(store
, info
.owner
, bucket
.tenant
, bucket
.name
, false);
576 lderr(store
->ctx()) << "ERROR: unable to remove user bucket information" << dendl
;
582 static int aio_wait(librados::AioCompletion
*handle
)
584 librados::AioCompletion
*c
= (librados::AioCompletion
*)handle
;
586 int ret
= c
->get_return_value();
591 static int drain_handles(list
<librados::AioCompletion
*>& pending
)
594 while (!pending
.empty()) {
595 librados::AioCompletion
*handle
= pending
.front();
597 int r
= aio_wait(handle
);
605 int rgw_remove_bucket_bypass_gc(RGWRados
*store
, rgw_bucket
& bucket
,
606 int concurrent_max
, bool keep_index_consistent
)
609 map
<RGWObjCategory
, RGWStorageStats
> stats
;
610 std::vector
<rgw_bucket_dir_entry
> objs
;
611 map
<string
, bool> common_prefixes
;
613 RGWObjectCtx
obj_ctx(store
);
615 string bucket_ver
, master_ver
;
617 ret
= store
->get_bucket_info(obj_ctx
, bucket
.tenant
, bucket
.name
, info
, NULL
);
621 ret
= store
->get_bucket_stats(info
, RGW_NO_SHARD
, &bucket_ver
, &master_ver
, stats
, NULL
);
626 RGWRados::Bucket
target(store
, info
);
627 RGWRados::Bucket::List
list_op(&target
);
629 list_op
.params
.list_versions
= true;
631 std::list
<librados::AioCompletion
*> handles
;
634 int max_aio
= concurrent_max
;
635 ret
= list_op
.list_objects(max
, &objs
, &common_prefixes
, NULL
);
639 while (!objs
.empty()) {
640 std::vector
<rgw_bucket_dir_entry
>::iterator it
= objs
.begin();
641 for (; it
!= objs
.end(); ++it
) {
642 RGWObjState
*astate
= NULL
;
643 rgw_obj
obj(bucket
, (*it
).key
);
645 ret
= store
->get_obj_state(&obj_ctx
, info
, obj
, &astate
, false);
646 if (ret
== -ENOENT
) {
647 dout(1) << "WARNING: cannot find obj state for obj " << obj
.get_oid() << dendl
;
651 lderr(store
->ctx()) << "ERROR: get obj state returned with error " << ret
<< dendl
;
655 if (astate
->has_manifest
) {
656 RGWObjManifest
& manifest
= astate
->manifest
;
657 RGWObjManifest::obj_iterator miter
= manifest
.obj_begin();
658 rgw_obj head_obj
= manifest
.get_obj();
659 rgw_raw_obj raw_head_obj
;
660 store
->obj_to_raw(info
.placement_rule
, head_obj
, &raw_head_obj
);
663 for (; miter
!= manifest
.obj_end() && max_aio
--; ++miter
) {
665 ret
= drain_handles(handles
);
667 lderr(store
->ctx()) << "ERROR: could not drain handles as aio completion returned with " << ret
<< dendl
;
670 max_aio
= concurrent_max
;
673 rgw_raw_obj last_obj
= miter
.get_location().get_raw_obj(store
);
674 if (last_obj
== raw_head_obj
) {
675 // have the head obj deleted at the end
679 ret
= store
->delete_raw_obj_aio(last_obj
, handles
);
681 lderr(store
->ctx()) << "ERROR: delete obj aio failed with " << ret
<< dendl
;
684 } // for all shadow objs
686 ret
= store
->delete_obj_aio(head_obj
, info
, astate
, handles
, keep_index_consistent
);
688 lderr(store
->ctx()) << "ERROR: delete obj aio failed with " << ret
<< dendl
;
694 ret
= drain_handles(handles
);
696 lderr(store
->ctx()) << "ERROR: could not drain handles as aio completion returned with " << ret
<< dendl
;
699 max_aio
= concurrent_max
;
701 } // for all RGW objects
704 ret
= list_op
.list_objects(max
, &objs
, &common_prefixes
, NULL
);
709 ret
= drain_handles(handles
);
711 lderr(store
->ctx()) << "ERROR: could not drain handles as aio completion returned with " << ret
<< dendl
;
715 ret
= rgw_bucket_sync_user_stats(store
, bucket
.tenant
, info
);
717 dout(1) << "WARNING: failed sync user stats before bucket delete. ret=" << ret
<< dendl
;
720 RGWObjVersionTracker objv_tracker
;
722 ret
= rgw_bucket_delete_bucket_obj(store
, bucket
.tenant
, bucket
.name
, objv_tracker
);
724 lderr(store
->ctx()) << "ERROR: could not remove bucket " << bucket
.name
<< "with ret as " << ret
<< dendl
;
728 if (!store
->is_syncing_bucket_meta(bucket
)) {
729 RGWObjVersionTracker objv_tracker
;
730 string entry
= bucket
.get_key();
731 ret
= rgw_bucket_instance_remove_entry(store
, entry
, &objv_tracker
);
733 lderr(store
->ctx()) << "ERROR: could not remove bucket instance entry" << bucket
.name
<< "with ret as " << ret
<< dendl
;
738 ret
= rgw_unlink_bucket(store
, info
.owner
, bucket
.tenant
, bucket
.name
, false);
740 lderr(store
->ctx()) << "ERROR: unable to remove user bucket information" << dendl
;
746 int rgw_bucket_delete_bucket_obj(RGWRados
*store
,
747 const string
& tenant_name
,
748 const string
& bucket_name
,
749 RGWObjVersionTracker
& objv_tracker
)
753 rgw_make_bucket_entry_name(tenant_name
, bucket_name
, key
);
754 return store
->meta_mgr
->remove_entry(bucket_meta_handler
, key
, &objv_tracker
);
757 static void set_err_msg(std::string
*sink
, std::string msg
)
759 if (sink
&& !msg
.empty())
763 int RGWBucket::init(RGWRados
*storage
, RGWBucketAdminOpState
& op_state
)
770 rgw_user user_id
= op_state
.get_user_id();
771 tenant
= user_id
.tenant
;
772 bucket_name
= op_state
.get_bucket_name();
773 RGWUserBuckets user_buckets
;
774 RGWObjectCtx
obj_ctx(store
);
776 if (bucket_name
.empty() && user_id
.empty())
779 if (!bucket_name
.empty()) {
780 int r
= store
->get_bucket_info(obj_ctx
, tenant
, bucket_name
, bucket_info
, NULL
);
782 ldout(store
->ctx(), 0) << "could not get bucket info for bucket=" << bucket_name
<< dendl
;
786 op_state
.set_bucket(bucket_info
.bucket
);
789 if (!user_id
.empty()) {
790 int r
= rgw_get_user_info_by_uid(store
, user_id
, user_info
);
794 op_state
.display_name
= user_info
.display_name
;
801 int RGWBucket::link(RGWBucketAdminOpState
& op_state
, std::string
*err_msg
)
803 if (!op_state
.is_user_op()) {
804 set_err_msg(err_msg
, "empty user id");
808 string bucket_id
= op_state
.get_bucket_id();
809 if (bucket_id
.empty()) {
810 set_err_msg(err_msg
, "empty bucket instance id");
814 std::string display_name
= op_state
.get_user_display_name();
815 rgw_bucket bucket
= op_state
.get_bucket();
817 const rgw_pool
& root_pool
= store
->get_zone_params().domain_root
;
818 rgw_raw_obj
obj(root_pool
, bucket
.name
);
819 RGWObjVersionTracker objv_tracker
;
821 map
<string
, bufferlist
> attrs
;
822 RGWBucketInfo bucket_info
;
824 string key
= bucket
.name
+ ":" + bucket_id
;
825 RGWObjectCtx
obj_ctx(store
);
826 int r
= store
->get_bucket_instance_info(obj_ctx
, key
, bucket_info
, NULL
, &attrs
);
831 rgw_user user_id
= op_state
.get_user_id();
833 map
<string
, bufferlist
>::iterator aiter
= attrs
.find(RGW_ATTR_ACL
);
834 if (aiter
!= attrs
.end()) {
835 bufferlist aclbl
= aiter
->second
;
836 RGWAccessControlPolicy policy
;
839 bufferlist::iterator iter
= aclbl
.begin();
840 ::decode(policy
, iter
);
841 owner
= policy
.get_owner();
842 } catch (buffer::error
& err
) {
843 set_err_msg(err_msg
, "couldn't decode policy");
847 r
= rgw_unlink_bucket(store
, owner
.get_id(), bucket
.tenant
, bucket
.name
, false);
849 set_err_msg(err_msg
, "could not unlink policy from user " + owner
.get_id().to_str());
853 // now update the user for the bucket...
854 if (display_name
.empty()) {
855 ldout(store
->ctx(), 0) << "WARNING: user " << user_info
.user_id
<< " has no display name set" << dendl
;
857 policy
.create_default(user_info
.user_id
, display_name
);
859 owner
= policy
.get_owner();
860 r
= store
->set_bucket_owner(bucket_info
.bucket
, owner
);
862 set_err_msg(err_msg
, "failed to set bucket owner: " + cpp_strerror(-r
));
866 // ...and encode the acl
868 policy
.encode(aclbl
);
870 r
= store
->system_obj_set_attr(NULL
, obj
, RGW_ATTR_ACL
, aclbl
, &objv_tracker
);
874 RGWAccessControlPolicy policy_instance
;
875 policy_instance
.create_default(user_info
.user_id
, display_name
);
877 policy_instance
.encode(aclbl
);
879 string oid_bucket_instance
= RGW_BUCKET_INSTANCE_MD_PREFIX
+ key
;
880 rgw_raw_obj
obj_bucket_instance(root_pool
, oid_bucket_instance
);
881 r
= store
->system_obj_set_attr(NULL
, obj_bucket_instance
, RGW_ATTR_ACL
, aclbl
, &objv_tracker
);
883 r
= rgw_link_bucket(store
, user_info
.user_id
, bucket_info
.bucket
, real_time());
891 int RGWBucket::unlink(RGWBucketAdminOpState
& op_state
, std::string
*err_msg
)
893 rgw_bucket bucket
= op_state
.get_bucket();
895 if (!op_state
.is_user_op()) {
896 set_err_msg(err_msg
, "could not fetch user or user bucket info");
900 int r
= rgw_unlink_bucket(store
, user_info
.user_id
, bucket
.tenant
, bucket
.name
);
902 set_err_msg(err_msg
, "error unlinking bucket" + cpp_strerror(-r
));
908 int RGWBucket::remove(RGWBucketAdminOpState
& op_state
, bool bypass_gc
,
909 bool keep_index_consistent
, std::string
*err_msg
)
911 bool delete_children
= op_state
.will_delete_children();
912 rgw_bucket bucket
= op_state
.get_bucket();
916 if (delete_children
) {
917 ret
= rgw_remove_bucket_bypass_gc(store
, bucket
, op_state
.get_max_aio(), keep_index_consistent
);
919 set_err_msg(err_msg
, "purge objects should be set for gc to be bypassed");
923 ret
= rgw_remove_bucket(store
, bucket
, delete_children
);
927 set_err_msg(err_msg
, "unable to remove bucket" + cpp_strerror(-ret
));
934 int RGWBucket::remove_object(RGWBucketAdminOpState
& op_state
, std::string
*err_msg
)
936 rgw_bucket bucket
= op_state
.get_bucket();
937 std::string object_name
= op_state
.get_object_name();
939 rgw_obj_key
key(object_name
);
941 int ret
= rgw_remove_object(store
, bucket_info
, bucket
, key
);
943 set_err_msg(err_msg
, "unable to remove object" + cpp_strerror(-ret
));
950 static void dump_bucket_index(map
<string
, rgw_bucket_dir_entry
> result
, Formatter
*f
)
952 map
<string
, rgw_bucket_dir_entry
>::iterator iter
;
953 for (iter
= result
.begin(); iter
!= result
.end(); ++iter
) {
954 f
->dump_string("object", iter
->first
);
958 static void dump_bucket_usage(map
<RGWObjCategory
, RGWStorageStats
>& stats
, Formatter
*formatter
)
960 map
<RGWObjCategory
, RGWStorageStats
>::iterator iter
;
962 formatter
->open_object_section("usage");
963 for (iter
= stats
.begin(); iter
!= stats
.end(); ++iter
) {
964 RGWStorageStats
& s
= iter
->second
;
965 const char *cat_name
= rgw_obj_category_name(iter
->first
);
966 formatter
->open_object_section(cat_name
);
968 formatter
->close_section();
970 formatter
->close_section();
973 static void dump_index_check(map
<RGWObjCategory
, RGWStorageStats
> existing_stats
,
974 map
<RGWObjCategory
, RGWStorageStats
> calculated_stats
,
975 Formatter
*formatter
)
977 formatter
->open_object_section("check_result");
978 formatter
->open_object_section("existing_header");
979 dump_bucket_usage(existing_stats
, formatter
);
980 formatter
->close_section();
981 formatter
->open_object_section("calculated_header");
982 dump_bucket_usage(calculated_stats
, formatter
);
983 formatter
->close_section();
984 formatter
->close_section();
987 int RGWBucket::check_bad_index_multipart(RGWBucketAdminOpState
& op_state
,
988 list
<rgw_obj_index_key
>& objs_to_unlink
, std::string
*err_msg
)
990 bool fix_index
= op_state
.will_fix_index();
991 rgw_bucket bucket
= op_state
.get_bucket();
995 map
<string
, bool> common_prefixes
;
997 string ns
= "multipart";
999 map
<string
, bool> meta_objs
;
1000 map
<rgw_obj_index_key
, string
> all_objs
;
1002 RGWBucketInfo bucket_info
;
1003 RGWObjectCtx
obj_ctx(store
);
1004 int r
= store
->get_bucket_instance_info(obj_ctx
, bucket
, bucket_info
, nullptr, nullptr);
1006 ldout(store
->ctx(), 0) << "ERROR: " << __func__
<< "(): get_bucket_instance_info(bucket=" << bucket
<< ") returned r=" << r
<< dendl
;
1010 RGWRados::Bucket
target(store
, bucket_info
);
1011 RGWRados::Bucket::List
list_op(&target
);
1013 list_op
.params
.list_versions
= true;
1014 list_op
.params
.ns
= ns
;
1017 vector
<rgw_bucket_dir_entry
> result
;
1018 int r
= list_op
.list_objects(max
, &result
, &common_prefixes
, &is_truncated
);
1020 set_err_msg(err_msg
, "failed to list objects in bucket=" + bucket
.name
+
1021 " err=" + cpp_strerror(-r
));
1026 vector
<rgw_bucket_dir_entry
>::iterator iter
;
1027 for (iter
= result
.begin(); iter
!= result
.end(); ++iter
) {
1028 rgw_obj_index_key key
= iter
->key
;
1029 rgw_obj
obj(bucket
, key
);
1030 string oid
= obj
.get_oid();
1032 int pos
= oid
.find_last_of('.');
1034 /* obj has no suffix */
1035 all_objs
[key
] = oid
;
1037 /* obj has suffix */
1038 string name
= oid
.substr(0, pos
);
1039 string suffix
= oid
.substr(pos
+ 1);
1041 if (suffix
.compare("meta") == 0) {
1042 meta_objs
[name
] = true;
1044 all_objs
[key
] = name
;
1049 } while (is_truncated
);
1051 for (auto aiter
= all_objs
.begin(); aiter
!= all_objs
.end(); ++aiter
) {
1052 string
& name
= aiter
->second
;
1054 if (meta_objs
.find(name
) == meta_objs
.end()) {
1055 objs_to_unlink
.push_back(aiter
->first
);
1059 if (objs_to_unlink
.empty())
1063 int r
= store
->remove_objs_from_index(bucket_info
, objs_to_unlink
);
1065 set_err_msg(err_msg
, "ERROR: remove_obj_from_index() returned error: " +
1075 int RGWBucket::check_object_index(RGWBucketAdminOpState
& op_state
,
1076 RGWFormatterFlusher
& flusher
,
1077 std::string
*err_msg
)
1080 bool fix_index
= op_state
.will_fix_index();
1082 rgw_bucket bucket
= op_state
.get_bucket();
1085 set_err_msg(err_msg
, "check-objects flag requires fix index enabled");
1089 store
->cls_obj_set_bucket_tag_timeout(bucket_info
, BUCKET_TAG_TIMEOUT
);
1092 rgw_obj_index_key marker
;
1093 bool is_truncated
= true;
1095 Formatter
*formatter
= flusher
.get_formatter();
1096 formatter
->open_object_section("objects");
1097 while (is_truncated
) {
1098 map
<string
, rgw_bucket_dir_entry
> result
;
1100 int r
= store
->cls_bucket_list(bucket_info
, RGW_NO_SHARD
, marker
, prefix
, 1000, true,
1101 result
, &is_truncated
, &marker
,
1102 bucket_object_check_filter
);
1105 } else if (r
< 0 && r
!= -ENOENT
) {
1106 set_err_msg(err_msg
, "ERROR: failed operation r=" + cpp_strerror(-r
));
1110 dump_bucket_index(result
, formatter
);
1115 formatter
->close_section();
1117 store
->cls_obj_set_bucket_tag_timeout(bucket_info
, 0);
1123 int RGWBucket::check_index(RGWBucketAdminOpState
& op_state
,
1124 map
<RGWObjCategory
, RGWStorageStats
>& existing_stats
,
1125 map
<RGWObjCategory
, RGWStorageStats
>& calculated_stats
,
1126 std::string
*err_msg
)
1128 rgw_bucket bucket
= op_state
.get_bucket();
1129 bool fix_index
= op_state
.will_fix_index();
1131 int r
= store
->bucket_check_index(bucket_info
, &existing_stats
, &calculated_stats
);
1133 set_err_msg(err_msg
, "failed to check index error=" + cpp_strerror(-r
));
1138 r
= store
->bucket_rebuild_index(bucket_info
);
1140 set_err_msg(err_msg
, "failed to rebuild index err=" + cpp_strerror(-r
));
1149 int RGWBucket::policy_bl_to_stream(bufferlist
& bl
, ostream
& o
)
1151 RGWAccessControlPolicy_S3
policy(g_ceph_context
);
1152 bufferlist::iterator iter
= bl
.begin();
1154 policy
.decode(iter
);
1155 } catch (buffer::error
& err
) {
1156 dout(0) << "ERROR: caught buffer::error, could not decode policy" << dendl
;
1163 static int policy_decode(RGWRados
*store
, bufferlist
& bl
, RGWAccessControlPolicy
& policy
)
1165 bufferlist::iterator iter
= bl
.begin();
1167 policy
.decode(iter
);
1168 } catch (buffer::error
& err
) {
1169 ldout(store
->ctx(), 0) << "ERROR: caught buffer::error, could not decode policy" << dendl
;
1175 int RGWBucket::get_policy(RGWBucketAdminOpState
& op_state
, RGWAccessControlPolicy
& policy
)
1177 std::string object_name
= op_state
.get_object_name();
1178 rgw_bucket bucket
= op_state
.get_bucket();
1179 RGWObjectCtx
obj_ctx(store
);
1181 RGWBucketInfo bucket_info
;
1182 map
<string
, bufferlist
> attrs
;
1183 int ret
= store
->get_bucket_info(obj_ctx
, bucket
.tenant
, bucket
.name
, bucket_info
, NULL
, &attrs
);
1188 if (!object_name
.empty()) {
1190 rgw_obj
obj(bucket
, object_name
);
1192 RGWRados::Object
op_target(store
, bucket_info
, obj_ctx
, obj
);
1193 RGWRados::Object::Read
rop(&op_target
);
1195 int ret
= rop
.get_attr(RGW_ATTR_ACL
, bl
);
1199 return policy_decode(store
, bl
, policy
);
1202 map
<string
, bufferlist
>::iterator aiter
= attrs
.find(RGW_ATTR_ACL
);
1203 if (aiter
== attrs
.end()) {
1207 return policy_decode(store
, aiter
->second
, policy
);
1211 int RGWBucketAdminOp::get_policy(RGWRados
*store
, RGWBucketAdminOpState
& op_state
,
1212 RGWAccessControlPolicy
& policy
)
1216 int ret
= bucket
.init(store
, op_state
);
1220 ret
= bucket
.get_policy(op_state
, policy
);
1227 /* Wrappers to facilitate RESTful interface */
1230 int RGWBucketAdminOp::get_policy(RGWRados
*store
, RGWBucketAdminOpState
& op_state
,
1231 RGWFormatterFlusher
& flusher
)
1233 RGWAccessControlPolicy
policy(store
->ctx());
1235 int ret
= get_policy(store
, op_state
, policy
);
1239 Formatter
*formatter
= flusher
.get_formatter();
1243 formatter
->open_object_section("policy");
1244 policy
.dump(formatter
);
1245 formatter
->close_section();
1252 int RGWBucketAdminOp::dump_s3_policy(RGWRados
*store
, RGWBucketAdminOpState
& op_state
,
1255 RGWAccessControlPolicy_S3
policy(store
->ctx());
1257 int ret
= get_policy(store
, op_state
, policy
);
1266 int RGWBucketAdminOp::unlink(RGWRados
*store
, RGWBucketAdminOpState
& op_state
)
1270 int ret
= bucket
.init(store
, op_state
);
1274 return bucket
.unlink(op_state
);
1277 int RGWBucketAdminOp::link(RGWRados
*store
, RGWBucketAdminOpState
& op_state
, string
*err
)
1281 int ret
= bucket
.init(store
, op_state
);
1285 return bucket
.link(op_state
, err
);
1289 int RGWBucketAdminOp::check_index(RGWRados
*store
, RGWBucketAdminOpState
& op_state
,
1290 RGWFormatterFlusher
& flusher
)
1293 map
<string
, rgw_bucket_dir_entry
> result
;
1294 map
<RGWObjCategory
, RGWStorageStats
> existing_stats
;
1295 map
<RGWObjCategory
, RGWStorageStats
> calculated_stats
;
1296 list
<rgw_obj_index_key
> objs_to_unlink
;
1300 ret
= bucket
.init(store
, op_state
);
1304 Formatter
*formatter
= flusher
.get_formatter();
1307 ret
= bucket
.check_bad_index_multipart(op_state
, objs_to_unlink
);
1311 dump_mulipart_index_results(objs_to_unlink
, formatter
);
1314 ret
= bucket
.check_object_index(op_state
, flusher
);
1318 ret
= bucket
.check_index(op_state
, existing_stats
, calculated_stats
);
1322 dump_index_check(existing_stats
, calculated_stats
, formatter
);
1328 int RGWBucketAdminOp::remove_bucket(RGWRados
*store
, RGWBucketAdminOpState
& op_state
,
1329 bool bypass_gc
, bool keep_index_consistent
)
1333 int ret
= bucket
.init(store
, op_state
);
1337 return bucket
.remove(op_state
, bypass_gc
, keep_index_consistent
);
1340 int RGWBucketAdminOp::remove_object(RGWRados
*store
, RGWBucketAdminOpState
& op_state
)
1344 int ret
= bucket
.init(store
, op_state
);
1348 return bucket
.remove_object(op_state
);
1351 static int bucket_stats(RGWRados
*store
, const std::string
& tenant_name
, std::string
& bucket_name
, Formatter
*formatter
)
1353 RGWBucketInfo bucket_info
;
1354 map
<RGWObjCategory
, RGWStorageStats
> stats
;
1357 RGWObjectCtx
obj_ctx(store
);
1358 int r
= store
->get_bucket_info(obj_ctx
, tenant_name
, bucket_name
, bucket_info
, &mtime
);
1362 rgw_bucket
& bucket
= bucket_info
.bucket
;
1364 string bucket_ver
, master_ver
;
1366 int ret
= store
->get_bucket_stats(bucket_info
, RGW_NO_SHARD
, &bucket_ver
, &master_ver
, stats
, &max_marker
);
1368 cerr
<< "error getting bucket stats ret=" << ret
<< std::endl
;
1374 formatter
->open_object_section("stats");
1375 formatter
->dump_string("bucket", bucket
.name
);
1376 formatter
->dump_string("zonegroup", bucket_info
.zonegroup
);
1377 formatter
->dump_string("placement_rule", bucket_info
.placement_rule
);
1378 ::encode_json("explicit_placement", bucket
.explicit_placement
, formatter
);
1379 formatter
->dump_string("id", bucket
.bucket_id
);
1380 formatter
->dump_string("marker", bucket
.marker
);
1381 formatter
->dump_stream("index_type") << bucket_info
.index_type
;
1382 ::encode_json("owner", bucket_info
.owner
, formatter
);
1383 formatter
->dump_string("ver", bucket_ver
);
1384 formatter
->dump_string("master_ver", master_ver
);
1385 formatter
->dump_stream("mtime") << ut
;
1386 formatter
->dump_string("max_marker", max_marker
);
1387 dump_bucket_usage(stats
, formatter
);
1388 encode_json("bucket_quota", bucket_info
.quota
, formatter
);
1389 formatter
->close_section();
1394 int RGWBucketAdminOp::limit_check(RGWRados
*store
,
1395 RGWBucketAdminOpState
& op_state
,
1396 const std::list
<std::string
>& user_ids
,
1397 RGWFormatterFlusher
& flusher
,
1401 const size_t max_entries
=
1402 store
->ctx()->_conf
->rgw_list_buckets_max_chunk
;
1404 const size_t safe_max_objs_per_shard
=
1405 store
->ctx()->_conf
->rgw_safe_max_objects_per_shard
;
1407 uint16_t shard_warn_pct
=
1408 store
->ctx()->_conf
->rgw_shard_warning_threshold
;
1409 if (shard_warn_pct
> 100)
1410 shard_warn_pct
= 90;
1412 Formatter
*formatter
= flusher
.get_formatter();
1415 formatter
->open_array_section("users");
1417 for (const auto& user_id
: user_ids
) {
1418 formatter
->open_object_section("user");
1419 formatter
->dump_string("user_id", user_id
);
1421 formatter
->open_array_section("buckets");
1423 RGWUserBuckets buckets
;
1427 ret
= rgw_read_user_buckets(store
, user_id
, buckets
,
1428 marker
, string(), max_entries
, false,
1433 map
<string
, RGWBucketEnt
>& m_buckets
= buckets
.get_buckets();
1435 for (const auto& iter
: m_buckets
) {
1436 auto& bucket
= iter
.second
.bucket
;
1437 uint32_t num_shards
= 1;
1438 uint64_t num_objects
= 0;
1440 /* need info for num_shards */
1442 RGWObjectCtx
obj_ctx(store
);
1444 marker
= bucket
.name
; /* Casey's location for marker update,
1445 * as we may now not reach the end of
1448 ret
= store
->get_bucket_info(obj_ctx
, bucket
.tenant
, bucket
.name
,
1453 /* need stats for num_entries */
1454 string bucket_ver
, master_ver
;
1455 std::map
<RGWObjCategory
, RGWStorageStats
> stats
;
1456 ret
= store
->get_bucket_stats(info
, RGW_NO_SHARD
, &bucket_ver
,
1457 &master_ver
, stats
, nullptr);
1462 for (const auto& s
: stats
) {
1463 num_objects
+= s
.second
.num_objects
;
1466 num_shards
= info
.num_shards
;
1467 uint64_t objs_per_shard
=
1468 (num_shards
) ? num_objects
/num_shards
: num_objects
;
1472 if (objs_per_shard
> safe_max_objs_per_shard
) {
1474 100 - (safe_max_objs_per_shard
/objs_per_shard
* 100);
1475 ss
<< boost::format("OVER %4f%%") % over
;
1479 objs_per_shard
/ safe_max_objs_per_shard
* 100;
1480 if (fill_pct
>= shard_warn_pct
) {
1481 ss
<< boost::format("WARN %4f%%") % fill_pct
;
1488 if (warn
|| (! warnings_only
)) {
1489 formatter
->open_object_section("bucket");
1490 formatter
->dump_string("bucket", bucket
.name
);
1491 formatter
->dump_string("tenant", bucket
.tenant
);
1492 formatter
->dump_int("num_objects", num_objects
);
1493 formatter
->dump_int("num_shards", num_shards
);
1494 formatter
->dump_int("objects_per_shard", objs_per_shard
);
1495 formatter
->dump_string("fill_status", ss
.str());
1496 formatter
->close_section();
1501 done
= (m_buckets
.size() < max_entries
);
1502 } while (!done
); /* foreach: bucket */
1504 formatter
->close_section();
1505 formatter
->close_section();
1506 formatter
->flush(cout
);
1508 } /* foreach: user_id */
1510 formatter
->close_section();
1511 formatter
->flush(cout
);
1514 } /* RGWBucketAdminOp::limit_check */
1516 int RGWBucketAdminOp::info(RGWRados
*store
, RGWBucketAdminOpState
& op_state
,
1517 RGWFormatterFlusher
& flusher
)
1522 string bucket_name
= op_state
.get_bucket_name();
1524 if (!bucket_name
.empty()) {
1525 ret
= bucket
.init(store
, op_state
);
1530 Formatter
*formatter
= flusher
.get_formatter();
1533 CephContext
*cct
= store
->ctx();
1535 const size_t max_entries
= cct
->_conf
->rgw_list_buckets_max_chunk
;
1537 bool show_stats
= op_state
.will_fetch_stats();
1538 rgw_user user_id
= op_state
.get_user_id();
1539 if (op_state
.is_user_op()) {
1540 formatter
->open_array_section("buckets");
1542 RGWUserBuckets buckets
;
1544 bool is_truncated
= false;
1547 ret
= rgw_read_user_buckets(store
, op_state
.get_user_id(), buckets
,
1548 marker
, string(), max_entries
, false,
1553 map
<string
, RGWBucketEnt
>& m
= buckets
.get_buckets();
1554 map
<string
, RGWBucketEnt
>::iterator iter
;
1556 for (iter
= m
.begin(); iter
!= m
.end(); ++iter
) {
1557 std::string obj_name
= iter
->first
;
1559 bucket_stats(store
, user_id
.tenant
, obj_name
, formatter
);
1561 formatter
->dump_string("bucket", obj_name
);
1567 } while (is_truncated
);
1569 formatter
->close_section();
1570 } else if (!bucket_name
.empty()) {
1571 bucket_stats(store
, user_id
.tenant
, bucket_name
, formatter
);
1573 RGWAccessHandle handle
;
1575 formatter
->open_array_section("buckets");
1576 if (store
->list_buckets_init(&handle
) >= 0) {
1577 rgw_bucket_dir_entry obj
;
1578 while (store
->list_buckets_next(obj
, &handle
) >= 0) {
1580 bucket_stats(store
, user_id
.tenant
, obj
.key
.name
, formatter
);
1582 formatter
->dump_string("bucket", obj
.key
.name
);
1586 formatter
->close_section();
1595 void rgw_data_change::dump(Formatter
*f
) const
1598 switch (entity_type
) {
1599 case ENTITY_TYPE_BUCKET
:
1605 encode_json("entity_type", type
, f
);
1606 encode_json("key", key
, f
);
1607 utime_t
ut(timestamp
);
1608 encode_json("timestamp", ut
, f
);
1611 void rgw_data_change::decode_json(JSONObj
*obj
) {
1613 JSONDecoder::decode_json("entity_type", s
, obj
);
1614 if (s
== "bucket") {
1615 entity_type
= ENTITY_TYPE_BUCKET
;
1617 entity_type
= ENTITY_TYPE_UNKNOWN
;
1619 JSONDecoder::decode_json("key", key
, obj
);
1621 JSONDecoder::decode_json("timestamp", ut
, obj
);
1622 timestamp
= ut
.to_real_time();
1625 void rgw_data_change_log_entry::dump(Formatter
*f
) const
1627 encode_json("log_id", log_id
, f
);
1628 utime_t
ut(log_timestamp
);
1629 encode_json("log_timestamp", ut
, f
);
1630 encode_json("entry", entry
, f
);
1633 void rgw_data_change_log_entry::decode_json(JSONObj
*obj
) {
1634 JSONDecoder::decode_json("log_id", log_id
, obj
);
1636 JSONDecoder::decode_json("log_timestamp", ut
, obj
);
1637 log_timestamp
= ut
.to_real_time();
1638 JSONDecoder::decode_json("entry", entry
, obj
);
1641 int RGWDataChangesLog::choose_oid(const rgw_bucket_shard
& bs
) {
1642 const string
& name
= bs
.bucket
.name
;
1643 int shard_shift
= (bs
.shard_id
> 0 ? bs
.shard_id
: 0);
1644 uint32_t r
= (ceph_str_hash_linux(name
.c_str(), name
.size()) + shard_shift
) % num_shards
;
1649 int RGWDataChangesLog::renew_entries()
1651 if (!store
->need_to_log_data())
1654 /* we can't keep the bucket name as part of the cls_log_entry, and we need
1655 * it later, so we keep two lists under the map */
1656 map
<int, pair
<list
<rgw_bucket_shard
>, list
<cls_log_entry
> > > m
;
1659 map
<rgw_bucket_shard
, bool> entries
;
1660 entries
.swap(cur_cycle
);
1663 map
<rgw_bucket_shard
, bool>::iterator iter
;
1665 real_time ut
= real_clock::now();
1666 for (iter
= entries
.begin(); iter
!= entries
.end(); ++iter
) {
1667 const rgw_bucket_shard
& bs
= iter
->first
;
1669 int index
= choose_oid(bs
);
1671 cls_log_entry entry
;
1673 rgw_data_change change
;
1675 change
.entity_type
= ENTITY_TYPE_BUCKET
;
1676 change
.key
= bs
.get_key();
1677 change
.timestamp
= ut
;
1678 ::encode(change
, bl
);
1680 store
->time_log_prepare_entry(entry
, ut
, section
, change
.key
, bl
);
1682 m
[index
].first
.push_back(bs
);
1683 m
[index
].second
.emplace_back(std::move(entry
));
1686 map
<int, pair
<list
<rgw_bucket_shard
>, list
<cls_log_entry
> > >::iterator miter
;
1687 for (miter
= m
.begin(); miter
!= m
.end(); ++miter
) {
1688 list
<cls_log_entry
>& entries
= miter
->second
.second
;
1690 real_time now
= real_clock::now();
1692 int ret
= store
->time_log_add(oids
[miter
->first
], entries
, NULL
);
1694 /* we don't really need to have a special handling for failed cases here,
1695 * as this is just an optimization. */
1696 lderr(cct
) << "ERROR: store->time_log_add() returned " << ret
<< dendl
;
1700 real_time expiration
= now
;
1701 expiration
+= make_timespan(cct
->_conf
->rgw_data_log_window
);
1703 list
<rgw_bucket_shard
>& buckets
= miter
->second
.first
;
1704 list
<rgw_bucket_shard
>::iterator liter
;
1705 for (liter
= buckets
.begin(); liter
!= buckets
.end(); ++liter
) {
1706 update_renewed(*liter
, expiration
);
1713 void RGWDataChangesLog::_get_change(const rgw_bucket_shard
& bs
, ChangeStatusPtr
& status
)
1715 assert(lock
.is_locked());
1716 if (!changes
.find(bs
, status
)) {
1717 status
= ChangeStatusPtr(new ChangeStatus
);
1718 changes
.add(bs
, status
);
1722 void RGWDataChangesLog::register_renew(rgw_bucket_shard
& bs
)
1724 Mutex::Locker
l(lock
);
1725 cur_cycle
[bs
] = true;
1728 void RGWDataChangesLog::update_renewed(rgw_bucket_shard
& bs
, real_time
& expiration
)
1730 Mutex::Locker
l(lock
);
1731 ChangeStatusPtr status
;
1732 _get_change(bs
, status
);
1734 ldout(cct
, 20) << "RGWDataChangesLog::update_renewd() bucket_name=" << bs
.bucket
.name
<< " shard_id=" << bs
.shard_id
<< " expiration=" << expiration
<< dendl
;
1735 status
->cur_expiration
= expiration
;
1738 int RGWDataChangesLog::get_log_shard_id(rgw_bucket
& bucket
, int shard_id
) {
1739 rgw_bucket_shard
bs(bucket
, shard_id
);
1741 return choose_oid(bs
);
1744 int RGWDataChangesLog::add_entry(rgw_bucket
& bucket
, int shard_id
) {
1745 if (!store
->need_to_log_data())
1748 rgw_bucket_shard
bs(bucket
, shard_id
);
1750 int index
= choose_oid(bs
);
1751 mark_modified(index
, bs
);
1755 ChangeStatusPtr status
;
1756 _get_change(bs
, status
);
1760 real_time now
= real_clock::now();
1762 status
->lock
->Lock();
1764 ldout(cct
, 20) << "RGWDataChangesLog::add_entry() bucket.name=" << bucket
.name
<< " shard_id=" << shard_id
<< " now=" << now
<< " cur_expiration=" << status
->cur_expiration
<< dendl
;
1766 if (now
< status
->cur_expiration
) {
1767 /* no need to send, recently completed */
1768 status
->lock
->Unlock();
1774 RefCountedCond
*cond
;
1776 if (status
->pending
) {
1777 cond
= status
->cond
;
1781 status
->cond
->get();
1782 status
->lock
->Unlock();
1784 int ret
= cond
->wait();
1792 status
->cond
= new RefCountedCond
;
1793 status
->pending
= true;
1795 string
& oid
= oids
[index
];
1796 real_time expiration
;
1801 status
->cur_sent
= now
;
1804 expiration
+= ceph::make_timespan(cct
->_conf
->rgw_data_log_window
);
1806 status
->lock
->Unlock();
1809 rgw_data_change change
;
1810 change
.entity_type
= ENTITY_TYPE_BUCKET
;
1811 change
.key
= bs
.get_key();
1812 change
.timestamp
= now
;
1813 ::encode(change
, bl
);
1816 ldout(cct
, 20) << "RGWDataChangesLog::add_entry() sending update with now=" << now
<< " cur_expiration=" << expiration
<< dendl
;
1818 ret
= store
->time_log_add(oid
, now
, section
, change
.key
, bl
);
1820 now
= real_clock::now();
1822 status
->lock
->Lock();
1824 } while (!ret
&& real_clock::now() > expiration
);
1826 cond
= status
->cond
;
1828 status
->pending
= false;
1829 status
->cur_expiration
= status
->cur_sent
; /* time of when operation started, not completed */
1830 status
->cur_expiration
+= make_timespan(cct
->_conf
->rgw_data_log_window
);
1831 status
->cond
= NULL
;
1832 status
->lock
->Unlock();
1840 int RGWDataChangesLog::list_entries(int shard
, const real_time
& start_time
, const real_time
& end_time
, int max_entries
,
1841 list
<rgw_data_change_log_entry
>& entries
,
1842 const string
& marker
,
1845 if (shard
>= num_shards
)
1848 list
<cls_log_entry
> log_entries
;
1850 int ret
= store
->time_log_list(oids
[shard
], start_time
, end_time
,
1851 max_entries
, log_entries
, marker
,
1852 out_marker
, truncated
);
1856 list
<cls_log_entry
>::iterator iter
;
1857 for (iter
= log_entries
.begin(); iter
!= log_entries
.end(); ++iter
) {
1858 rgw_data_change_log_entry log_entry
;
1859 log_entry
.log_id
= iter
->id
;
1860 real_time rt
= iter
->timestamp
.to_real_time();
1861 log_entry
.log_timestamp
= rt
;
1862 bufferlist::iterator liter
= iter
->data
.begin();
1864 ::decode(log_entry
.entry
, liter
);
1865 } catch (buffer::error
& err
) {
1866 lderr(cct
) << "ERROR: failed to decode data changes log entry" << dendl
;
1869 entries
.push_back(log_entry
);
1875 int RGWDataChangesLog::list_entries(const real_time
& start_time
, const real_time
& end_time
, int max_entries
,
1876 list
<rgw_data_change_log_entry
>& entries
, LogMarker
& marker
, bool *ptruncated
) {
1880 for (; marker
.shard
< num_shards
&& (int)entries
.size() < max_entries
;
1881 marker
.shard
++, marker
.marker
.clear()) {
1882 int ret
= list_entries(marker
.shard
, start_time
, end_time
, max_entries
- entries
.size(), entries
,
1883 marker
.marker
, NULL
, &truncated
);
1884 if (ret
== -ENOENT
) {
1896 *ptruncated
= (marker
.shard
< num_shards
);
1901 int RGWDataChangesLog::get_info(int shard_id
, RGWDataChangesLogInfo
*info
)
1903 if (shard_id
>= num_shards
)
1906 string oid
= oids
[shard_id
];
1908 cls_log_header header
;
1910 int ret
= store
->time_log_info(oid
, &header
);
1911 if ((ret
< 0) && (ret
!= -ENOENT
))
1914 info
->marker
= header
.max_marker
;
1915 info
->last_update
= header
.max_time
.to_real_time();
1920 int RGWDataChangesLog::trim_entries(int shard_id
, const real_time
& start_time
, const real_time
& end_time
,
1921 const string
& start_marker
, const string
& end_marker
)
1925 if (shard_id
> num_shards
)
1928 ret
= store
->time_log_trim(oids
[shard_id
], start_time
, end_time
, start_marker
, end_marker
);
1930 if (ret
== -ENOENT
|| ret
== -ENODATA
)
1936 int RGWDataChangesLog::trim_entries(const real_time
& start_time
, const real_time
& end_time
,
1937 const string
& start_marker
, const string
& end_marker
)
1939 for (int shard
= 0; shard
< num_shards
; shard
++) {
1940 int ret
= store
->time_log_trim(oids
[shard
], start_time
, end_time
, start_marker
, end_marker
);
1941 if (ret
== -ENOENT
|| ret
== -ENODATA
) {
1951 bool RGWDataChangesLog::going_down()
1956 RGWDataChangesLog::~RGWDataChangesLog() {
1958 renew_thread
->stop();
1959 renew_thread
->join();
1960 delete renew_thread
;
1964 void *RGWDataChangesLog::ChangesRenewThread::entry() {
1966 dout(2) << "RGWDataChangesLog::ChangesRenewThread: start" << dendl
;
1967 int r
= log
->renew_entries();
1969 dout(0) << "ERROR: RGWDataChangesLog::renew_entries returned error r=" << r
<< dendl
;
1972 if (log
->going_down())
1975 int interval
= cct
->_conf
->rgw_data_log_window
* 3 / 4;
1977 cond
.WaitInterval(lock
, utime_t(interval
, 0));
1979 } while (!log
->going_down());
1984 void RGWDataChangesLog::ChangesRenewThread::stop()
1986 Mutex::Locker
l(lock
);
1990 void RGWDataChangesLog::mark_modified(int shard_id
, const rgw_bucket_shard
& bs
)
1992 auto key
= bs
.get_key();
1993 modified_lock
.get_read();
1994 map
<int, set
<string
> >::iterator iter
= modified_shards
.find(shard_id
);
1995 if (iter
!= modified_shards
.end()) {
1996 set
<string
>& keys
= iter
->second
;
1997 if (keys
.find(key
) != keys
.end()) {
1998 modified_lock
.unlock();
2002 modified_lock
.unlock();
2004 RWLock::WLocker
wl(modified_lock
);
2005 modified_shards
[shard_id
].insert(key
);
2008 void RGWDataChangesLog::read_clear_modified(map
<int, set
<string
> > &modified
)
2010 RWLock::WLocker
wl(modified_lock
);
2011 modified
.swap(modified_shards
);
2012 modified_shards
.clear();
2015 void RGWBucketCompleteInfo::dump(Formatter
*f
) const {
2016 encode_json("bucket_info", info
, f
);
2017 encode_json("attrs", attrs
, f
);
2020 void RGWBucketCompleteInfo::decode_json(JSONObj
*obj
) {
2021 JSONDecoder::decode_json("bucket_info", info
, obj
);
2022 JSONDecoder::decode_json("attrs", attrs
, obj
);
2025 class RGWBucketMetadataHandler
: public RGWMetadataHandler
{
2028 string
get_type() override
{ return "bucket"; }
2030 int get(RGWRados
*store
, string
& entry
, RGWMetadataObject
**obj
) override
{
2031 RGWObjVersionTracker ot
;
2032 RGWBucketEntryPoint be
;
2035 map
<string
, bufferlist
> attrs
;
2036 RGWObjectCtx
obj_ctx(store
);
2038 string tenant_name
, bucket_name
;
2039 parse_bucket(entry
, &tenant_name
, &bucket_name
);
2040 int ret
= store
->get_bucket_entrypoint_info(obj_ctx
, tenant_name
, bucket_name
, be
, &ot
, &mtime
, &attrs
);
2044 RGWBucketEntryMetadataObject
*mdo
= new RGWBucketEntryMetadataObject(be
, ot
.read_version
, mtime
);
2051 int put(RGWRados
*store
, string
& entry
, RGWObjVersionTracker
& objv_tracker
,
2052 real_time mtime
, JSONObj
*obj
, sync_type_t sync_type
) override
{
2053 RGWBucketEntryPoint be
, old_be
;
2055 decode_json_obj(be
, obj
);
2056 } catch (JSONDecoder::err
& e
) {
2060 real_time orig_mtime
;
2061 map
<string
, bufferlist
> attrs
;
2063 RGWObjVersionTracker old_ot
;
2064 RGWObjectCtx
obj_ctx(store
);
2066 string tenant_name
, bucket_name
;
2067 parse_bucket(entry
, &tenant_name
, &bucket_name
);
2068 int ret
= store
->get_bucket_entrypoint_info(obj_ctx
, tenant_name
, bucket_name
, old_be
, &old_ot
, &orig_mtime
, &attrs
);
2069 if (ret
< 0 && ret
!= -ENOENT
)
2072 // are we actually going to perform this put, or is it too old?
2073 if (ret
!= -ENOENT
&&
2074 !check_versions(old_ot
.read_version
, orig_mtime
,
2075 objv_tracker
.write_version
, mtime
, sync_type
)) {
2076 return STATUS_NO_APPLY
;
2079 objv_tracker
.read_version
= old_ot
.read_version
; /* maintain the obj version we just read */
2081 ret
= store
->put_bucket_entrypoint_info(tenant_name
, bucket_name
, be
, false, objv_tracker
, mtime
, &attrs
);
2087 ret
= rgw_link_bucket(store
, be
.owner
, be
.bucket
, be
.creation_time
, false);
2089 ret
= rgw_unlink_bucket(store
, be
.owner
, be
.bucket
.tenant
, be
.bucket
.name
, false);
2095 struct list_keys_info
{
2097 RGWListRawObjsCtx ctx
;
2100 int remove(RGWRados
*store
, string
& entry
, RGWObjVersionTracker
& objv_tracker
) override
{
2101 RGWBucketEntryPoint be
;
2102 RGWObjectCtx
obj_ctx(store
);
2104 string tenant_name
, bucket_name
;
2105 parse_bucket(entry
, &tenant_name
, &bucket_name
);
2106 int ret
= store
->get_bucket_entrypoint_info(obj_ctx
, tenant_name
, bucket_name
, be
, &objv_tracker
, NULL
, NULL
);
2111 * We're unlinking the bucket but we don't want to update the entrypoint here - we're removing
2112 * it immediately and don't want to invalidate our cached objv_version or the bucket obj removal
2113 * will incorrectly fail.
2115 ret
= rgw_unlink_bucket(store
, be
.owner
, tenant_name
, bucket_name
, false);
2117 lderr(store
->ctx()) << "could not unlink bucket=" << entry
<< " owner=" << be
.owner
<< dendl
;
2120 ret
= rgw_bucket_delete_bucket_obj(store
, tenant_name
, bucket_name
, objv_tracker
);
2122 lderr(store
->ctx()) << "could not delete bucket=" << entry
<< dendl
;
2128 void get_pool_and_oid(RGWRados
*store
, const string
& key
, rgw_pool
& pool
, string
& oid
) override
{
2130 pool
= store
->get_zone_params().domain_root
;
2133 int list_keys_init(RGWRados
*store
, void **phandle
) override
2135 list_keys_info
*info
= new list_keys_info
;
2137 info
->store
= store
;
2139 *phandle
= (void *)info
;
2144 int list_keys_next(void *handle
, int max
, list
<string
>& keys
, bool *truncated
) override
{
2145 list_keys_info
*info
= static_cast<list_keys_info
*>(handle
);
2151 RGWRados
*store
= info
->store
;
2153 list
<string
> unfiltered_keys
;
2155 int ret
= store
->list_raw_objects(store
->get_zone_params().domain_root
, no_filter
,
2156 max
, info
->ctx
, unfiltered_keys
, truncated
);
2157 if (ret
< 0 && ret
!= -ENOENT
)
2159 if (ret
== -ENOENT
) {
2165 // now filter out the system entries
2166 list
<string
>::iterator iter
;
2167 for (iter
= unfiltered_keys
.begin(); iter
!= unfiltered_keys
.end(); ++iter
) {
2178 void list_keys_complete(void *handle
) override
{
2179 list_keys_info
*info
= static_cast<list_keys_info
*>(handle
);
2184 class RGWBucketInstanceMetadataHandler
: public RGWMetadataHandler
{
2187 string
get_type() override
{ return "bucket.instance"; }
2189 int get(RGWRados
*store
, string
& oid
, RGWMetadataObject
**obj
) override
{
2190 RGWBucketCompleteInfo bci
;
2193 RGWObjectCtx
obj_ctx(store
);
2195 int ret
= store
->get_bucket_instance_info(obj_ctx
, oid
, bci
.info
, &mtime
, &bci
.attrs
);
2199 RGWBucketInstanceMetadataObject
*mdo
= new RGWBucketInstanceMetadataObject(bci
, bci
.info
.objv_tracker
.read_version
, mtime
);
2206 int put(RGWRados
*store
, string
& entry
, RGWObjVersionTracker
& objv_tracker
,
2207 real_time mtime
, JSONObj
*obj
, sync_type_t sync_type
) override
{
2208 RGWBucketCompleteInfo bci
, old_bci
;
2210 decode_json_obj(bci
, obj
);
2211 } catch (JSONDecoder::err
& e
) {
2215 real_time orig_mtime
;
2216 RGWObjectCtx
obj_ctx(store
);
2218 int ret
= store
->get_bucket_instance_info(obj_ctx
, entry
, old_bci
.info
,
2219 &orig_mtime
, &old_bci
.attrs
);
2220 bool exists
= (ret
!= -ENOENT
);
2221 if (ret
< 0 && exists
)
2224 if (!exists
|| old_bci
.info
.bucket
.bucket_id
!= bci
.info
.bucket
.bucket_id
) {
2225 /* a new bucket, we need to select a new bucket placement for it */
2227 rgw_bucket_instance_oid_to_key(key
);
2230 string bucket_instance
;
2231 parse_bucket(key
, &tenant_name
, &bucket_name
, &bucket_instance
);
2233 RGWZonePlacementInfo rule_info
;
2234 bci
.info
.bucket
.name
= bucket_name
;
2235 bci
.info
.bucket
.bucket_id
= bucket_instance
;
2236 bci
.info
.bucket
.tenant
= tenant_name
;
2237 ret
= store
->select_bucket_location_by_rule(bci
.info
.placement_rule
, &rule_info
);
2239 ldout(store
->ctx(), 0) << "ERROR: select_bucket_placement() returned " << ret
<< dendl
;
2242 bci
.info
.index_type
= rule_info
.index_type
;
2244 /* existing bucket, keep its placement */
2245 bci
.info
.bucket
.explicit_placement
= old_bci
.info
.bucket
.explicit_placement
;
2246 bci
.info
.placement_rule
= old_bci
.info
.placement_rule
;
2249 // are we actually going to perform this put, or is it too old?
2251 !check_versions(old_bci
.info
.objv_tracker
.read_version
, orig_mtime
,
2252 objv_tracker
.write_version
, mtime
, sync_type
)) {
2253 objv_tracker
.read_version
= old_bci
.info
.objv_tracker
.read_version
;
2254 return STATUS_NO_APPLY
;
2257 /* record the read version (if any), store the new version */
2258 bci
.info
.objv_tracker
.read_version
= old_bci
.info
.objv_tracker
.read_version
;
2259 bci
.info
.objv_tracker
.write_version
= objv_tracker
.write_version
;
2261 ret
= store
->put_bucket_instance_info(bci
.info
, false, mtime
, &bci
.attrs
);
2265 objv_tracker
= bci
.info
.objv_tracker
;
2267 ret
= store
->init_bucket_index(bci
.info
, bci
.info
.num_shards
);
2271 return STATUS_APPLIED
;
2274 struct list_keys_info
{
2276 RGWListRawObjsCtx ctx
;
2279 int remove(RGWRados
*store
, string
& entry
, RGWObjVersionTracker
& objv_tracker
) override
{
2281 RGWObjectCtx
obj_ctx(store
);
2283 int ret
= store
->get_bucket_instance_info(obj_ctx
, entry
, info
, NULL
, NULL
);
2284 if (ret
< 0 && ret
!= -ENOENT
)
2287 return rgw_bucket_instance_remove_entry(store
, entry
, &info
.objv_tracker
);
2290 void get_pool_and_oid(RGWRados
*store
, const string
& key
, rgw_pool
& pool
, string
& oid
) override
{
2291 oid
= RGW_BUCKET_INSTANCE_MD_PREFIX
+ key
;
2292 rgw_bucket_instance_key_to_oid(oid
);
2293 pool
= store
->get_zone_params().domain_root
;
2296 int list_keys_init(RGWRados
*store
, void **phandle
) override
2298 list_keys_info
*info
= new list_keys_info
;
2300 info
->store
= store
;
2302 *phandle
= (void *)info
;
2307 int list_keys_next(void *handle
, int max
, list
<string
>& keys
, bool *truncated
) override
{
2308 list_keys_info
*info
= static_cast<list_keys_info
*>(handle
);
2314 RGWRados
*store
= info
->store
;
2316 list
<string
> unfiltered_keys
;
2318 int ret
= store
->list_raw_objects(store
->get_zone_params().domain_root
, no_filter
,
2319 max
, info
->ctx
, unfiltered_keys
, truncated
);
2320 if (ret
< 0 && ret
!= -ENOENT
)
2322 if (ret
== -ENOENT
) {
2328 constexpr int prefix_size
= sizeof(RGW_BUCKET_INSTANCE_MD_PREFIX
) - 1;
2329 // now filter in the relevant entries
2330 list
<string
>::iterator iter
;
2331 for (iter
= unfiltered_keys
.begin(); iter
!= unfiltered_keys
.end(); ++iter
) {
2334 if (k
.compare(0, prefix_size
, RGW_BUCKET_INSTANCE_MD_PREFIX
) == 0) {
2335 auto oid
= k
.substr(prefix_size
);
2336 rgw_bucket_instance_oid_to_key(oid
);
2337 keys
.emplace_back(std::move(oid
));
2344 void list_keys_complete(void *handle
) override
{
2345 list_keys_info
*info
= static_cast<list_keys_info
*>(handle
);
2350 * hash entry for mdlog placement. Use the same hash key we'd have for the bucket entry
2351 * point, so that the log entries end up at the same log shard, so that we process them
2354 void get_hash_key(const string
& section
, const string
& key
, string
& hash_key
) override
{
2356 int pos
= key
.find(':');
2360 k
= key
.substr(0, pos
);
2361 hash_key
= "bucket:" + k
;
2365 void rgw_bucket_init(RGWMetadataManager
*mm
)
2367 bucket_meta_handler
= new RGWBucketMetadataHandler
;
2368 mm
->register_handler(bucket_meta_handler
);
2369 bucket_instance_meta_handler
= new RGWBucketInstanceMetadataHandler
;
2370 mm
->register_handler(bucket_instance_meta_handler
);