1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab ft=cpp
4 #ifndef CEPH_RGW_TOOLS_H
5 #define CEPH_RGW_TOOLS_H
9 #include "include/types.h"
10 #include "include/ceph_hash.h"
12 #include "common/ceph_time.h"
14 #include "rgw_common.h"
19 class RGWSysObjectCtx
;
20 struct RGWObjVersionTracker
;
22 namespace rgw
{ namespace sal
{
29 int rgw_init_ioctx(const DoutPrefixProvider
*dpp
,
30 librados::Rados
*rados
, const rgw_pool
& pool
,
31 librados::IoCtx
& ioctx
,
33 bool mostly_omap
= false);
35 #define RGW_NO_SHARD -1
37 #define RGW_SHARDS_PRIME_0 7877
38 #define RGW_SHARDS_PRIME_1 65521
40 extern const std::string MP_META_SUFFIX
;
42 inline int rgw_shards_max()
44 return RGW_SHARDS_PRIME_1
;
47 // only called by rgw_shard_id and rgw_bucket_shard_index
48 static inline int rgw_shards_mod(unsigned hval
, int max_shards
)
50 if (max_shards
<= RGW_SHARDS_PRIME_0
) {
51 return hval
% RGW_SHARDS_PRIME_0
% max_shards
;
53 return hval
% RGW_SHARDS_PRIME_1
% max_shards
;
56 // used for logging and tagging
57 inline int rgw_shard_id(const std::string
& key
, int max_shards
)
59 return rgw_shards_mod(ceph_str_hash_linux(key
.c_str(), key
.size()),
63 void rgw_shard_name(const std::string
& prefix
, unsigned max_shards
, const std::string
& key
, std::string
& name
, int *shard_id
);
64 void rgw_shard_name(const std::string
& prefix
, unsigned max_shards
, const std::string
& section
, const std::string
& key
, std::string
& name
);
65 void rgw_shard_name(const std::string
& prefix
, unsigned shard_id
, std::string
& name
);
67 struct rgw_name_to_flag
{
68 const char *type_name
;
72 int rgw_parse_list_of_flags(struct rgw_name_to_flag
*mapping
,
73 const std::string
& str
, uint32_t *perm
);
75 int rgw_put_system_obj(const DoutPrefixProvider
*dpp
, RGWSysObjectCtx
& obj_ctx
, const rgw_pool
& pool
, const std::string
& oid
, bufferlist
& data
, bool exclusive
,
76 RGWObjVersionTracker
*objv_tracker
, real_time set_mtime
, optional_yield y
, std::map
<std::string
, bufferlist
> *pattrs
= NULL
);
77 int rgw_get_system_obj(RGWSysObjectCtx
& obj_ctx
, const rgw_pool
& pool
, const std::string
& key
, bufferlist
& bl
,
78 RGWObjVersionTracker
*objv_tracker
, real_time
*pmtime
, optional_yield y
, const DoutPrefixProvider
*dpp
, std::map
<std::string
, bufferlist
> *pattrs
= NULL
,
79 rgw_cache_entry_info
*cache_info
= NULL
,
80 boost::optional
<obj_version
> refresh_version
= boost::none
, bool raw_attrs
=false);
81 int rgw_delete_system_obj(const DoutPrefixProvider
*dpp
,
82 RGWSI_SysObj
*sysobj_svc
, const rgw_pool
& pool
, const std::string
& oid
,
83 RGWObjVersionTracker
*objv_tracker
, optional_yield y
);
85 const char *rgw_find_mime_by_ext(std::string
& ext
);
87 void rgw_filter_attrset(std::map
<std::string
, bufferlist
>& unfiltered_attrset
, const std::string
& check_prefix
,
88 std::map
<std::string
, bufferlist
> *attrset
);
90 /// indicates whether the current thread is in boost::asio::io_context::run(),
91 /// used to log warnings if synchronous librados calls are made
92 extern thread_local
bool is_asio_thread
;
94 /// perform the rados operation, using the yield context when given
95 int rgw_rados_operate(const DoutPrefixProvider
*dpp
, librados::IoCtx
& ioctx
, const std::string
& oid
,
96 librados::ObjectReadOperation
*op
, bufferlist
* pbl
,
97 optional_yield y
, int flags
= 0);
98 int rgw_rados_operate(const DoutPrefixProvider
*dpp
, librados::IoCtx
& ioctx
, const std::string
& oid
,
99 librados::ObjectWriteOperation
*op
, optional_yield y
,
101 int rgw_rados_notify(const DoutPrefixProvider
*dpp
, librados::IoCtx
& ioctx
, const std::string
& oid
,
102 bufferlist
& bl
, uint64_t timeout_ms
, bufferlist
* pbl
,
105 int rgw_tools_init(const DoutPrefixProvider
*dpp
, CephContext
*cct
);
106 void rgw_tools_cleanup();
108 template<class H
, size_t S
>
115 if constexpr (std::is_same_v
<H
, MD5
>) {
116 // Allow use of MD5 digest in FIPS mode for non-cryptographic purposes
117 hash
.SetFlags(EVP_MD_CTX_FLAG_NON_FIPS_ALLOW
);
121 void update(const char *buf
, size_t len
) {
122 hash
.Update((const unsigned char *)buf
, len
);
125 void update(bufferlist
& bl
) {
126 if (bl
.length() > 0) {
127 update(bl
.c_str(), bl
.length());
131 void update(const std::string
& s
) {
133 update(s
.c_str(), s
.size());
136 void finish(std::string
*etag
) {
138 char etag_buf_str
[S
* 2 + 16];
140 hash
.Final((unsigned char *)etag_buf
);
141 buf_to_hex((const unsigned char *)etag_buf
, S
,
144 *etag
= etag_buf_str
;
148 using RGWMD5Etag
= RGWEtag
<MD5
, CEPH_CRYPTO_MD5_DIGESTSIZE
>;
152 rgw::sal::Store
* store
;
155 RGWDataAccess(rgw::sal::Store
* _store
);
160 using BucketRef
= std::shared_ptr
<Bucket
>;
161 using ObjectRef
= std::shared_ptr
<Object
>;
163 class Bucket
: public std::enable_shared_from_this
<Bucket
> {
164 friend class RGWDataAccess
;
167 RGWDataAccess
*sd
{nullptr};
168 RGWBucketInfo bucket_info
;
171 std::string bucket_id
;
172 ceph::real_time mtime
;
173 std::map
<std::string
, bufferlist
> attrs
;
175 RGWAccessControlPolicy policy
;
178 Bucket(RGWDataAccess
*_sd
,
179 const std::string
& _tenant
,
180 const std::string
& _name
,
181 const std::string
& _bucket_id
) : sd(_sd
),
184 bucket_id(_bucket_id
) {}
185 Bucket(RGWDataAccess
*_sd
) : sd(_sd
) {}
186 int init(const DoutPrefixProvider
*dpp
, optional_yield y
);
187 int init(const RGWBucketInfo
& _bucket_info
, const std::map
<std::string
, bufferlist
>& _attrs
);
189 int get_object(const rgw_obj_key
& key
,
196 RGWDataAccess
*sd
{nullptr};
200 ceph::real_time mtime
;
202 uint64_t olh_epoch
{0};
203 ceph::real_time delete_at
;
204 std::optional
<std::string
> user_data
;
206 std::optional
<bufferlist
> aclbl
;
208 Object(RGWDataAccess
*_sd
,
210 const rgw_obj_key
& _key
) : sd(_sd
),
214 int put(bufferlist
& data
, std::map
<std::string
, bufferlist
>& attrs
, const DoutPrefixProvider
*dpp
, optional_yield y
); /* might modify attrs */
216 void set_mtime(const ceph::real_time
& _mtime
) {
220 void set_etag(const std::string
& _etag
) {
224 void set_olh_epoch(uint64_t epoch
) {
228 void set_delete_at(ceph::real_time _delete_at
) {
229 delete_at
= _delete_at
;
232 void set_user_data(const std::string
& _user_data
) {
233 user_data
= _user_data
;
236 void set_policy(const RGWAccessControlPolicy
& policy
);
241 int get_bucket(const DoutPrefixProvider
*dpp
,
242 const std::string
& tenant
,
243 const std::string name
,
244 const std::string bucket_id
,
247 bucket
->reset(new Bucket(this, tenant
, name
, bucket_id
));
248 return (*bucket
)->init(dpp
, y
);
251 int get_bucket(const RGWBucketInfo
& bucket_info
,
252 const std::map
<std::string
, bufferlist
>& attrs
,
254 bucket
->reset(new Bucket(this));
255 return (*bucket
)->init(bucket_info
, attrs
);
261 using RGWDataAccessRef
= std::shared_ptr
<RGWDataAccess
>;
263 /// Complete an AioCompletion. To return error values or otherwise
264 /// satisfy the caller. Useful for making complicated asynchronous
265 /// calls and error handling.
266 void rgw_complete_aio_completion(librados::AioCompletion
* c
, int r
);