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(librados::Rados
*rados
, const rgw_pool
& pool
,
30 librados::IoCtx
& ioctx
,
32 bool mostly_omap
= false);
34 #define RGW_NO_SHARD -1
36 #define RGW_SHARDS_PRIME_0 7877
37 #define RGW_SHARDS_PRIME_1 65521
39 extern const std::string MP_META_SUFFIX
;
41 static inline int rgw_shards_max()
43 return RGW_SHARDS_PRIME_1
;
46 // only called by rgw_shard_id and rgw_bucket_shard_index
47 static inline int rgw_shards_mod(unsigned hval
, int max_shards
)
49 if (max_shards
<= RGW_SHARDS_PRIME_0
) {
50 return hval
% RGW_SHARDS_PRIME_0
% max_shards
;
52 return hval
% RGW_SHARDS_PRIME_1
% max_shards
;
55 // used for logging and tagging
56 static inline int rgw_shard_id(const string
& key
, int max_shards
)
58 return rgw_shards_mod(ceph_str_hash_linux(key
.c_str(), key
.size()),
62 void rgw_shard_name(const string
& prefix
, unsigned max_shards
, const string
& key
, string
& name
, int *shard_id
);
63 void rgw_shard_name(const string
& prefix
, unsigned max_shards
, const string
& section
, const string
& key
, string
& name
);
64 void rgw_shard_name(const string
& prefix
, unsigned shard_id
, string
& name
);
66 struct rgw_name_to_flag
{
67 const char *type_name
;
71 int rgw_parse_list_of_flags(struct rgw_name_to_flag
*mapping
,
72 const string
& str
, uint32_t *perm
);
74 int rgw_put_system_obj(RGWSysObjectCtx
& obj_ctx
, const rgw_pool
& pool
, const string
& oid
, bufferlist
& data
, bool exclusive
,
75 RGWObjVersionTracker
*objv_tracker
, real_time set_mtime
, map
<string
, bufferlist
> *pattrs
= NULL
);
76 int rgw_put_system_obj(RGWSysObjectCtx
& obj_ctx
, const rgw_pool
& pool
, const string
& oid
, bufferlist
& data
, bool exclusive
,
77 RGWObjVersionTracker
*objv_tracker
, real_time set_mtime
, optional_yield y
, map
<string
, bufferlist
> *pattrs
= NULL
);
78 int rgw_get_system_obj(RGWSysObjectCtx
& obj_ctx
, const rgw_pool
& pool
, const string
& key
, bufferlist
& bl
,
79 RGWObjVersionTracker
*objv_tracker
, real_time
*pmtime
, optional_yield y
, map
<string
, bufferlist
> *pattrs
= NULL
,
80 rgw_cache_entry_info
*cache_info
= NULL
,
81 boost::optional
<obj_version
> refresh_version
= boost::none
);
82 int rgw_delete_system_obj(RGWSI_SysObj
*sysobj_svc
, const rgw_pool
& pool
, const string
& oid
,
83 RGWObjVersionTracker
*objv_tracker
);
85 const char *rgw_find_mime_by_ext(string
& ext
);
87 void rgw_filter_attrset(map
<string
, bufferlist
>& unfiltered_attrset
, const string
& check_prefix
,
88 map
<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(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(librados::IoCtx
& ioctx
, const std::string
& oid
,
99 librados::ObjectWriteOperation
*op
, optional_yield y
,
101 int rgw_rados_notify(librados::IoCtx
& ioctx
, const std::string
& oid
,
102 bufferlist
& bl
, uint64_t timeout_ms
, bufferlist
* pbl
,
105 int rgw_tools_init(CephContext
*cct
);
106 void rgw_tools_cleanup();
108 template<class H
, size_t S
>
116 void update(const char *buf
, size_t len
) {
117 hash
.Update((const unsigned char *)buf
, len
);
120 void update(bufferlist
& bl
) {
121 if (bl
.length() > 0) {
122 update(bl
.c_str(), bl
.length());
126 void update(const string
& s
) {
128 update(s
.c_str(), s
.size());
131 void finish(string
*etag
) {
133 char etag_buf_str
[S
* 2 + 16];
135 hash
.Final((unsigned char *)etag_buf
);
136 buf_to_hex((const unsigned char *)etag_buf
, S
,
139 *etag
= etag_buf_str
;
143 using RGWMD5Etag
= RGWEtag
<MD5
, CEPH_CRYPTO_MD5_DIGESTSIZE
>;
147 rgw::sal::RGWRadosStore
*store
;
148 std::unique_ptr
<RGWSysObjectCtx
> sysobj_ctx
;
151 RGWDataAccess(rgw::sal::RGWRadosStore
*_store
);
156 using BucketRef
= std::shared_ptr
<Bucket
>;
157 using ObjectRef
= std::shared_ptr
<Object
>;
159 class Bucket
: public enable_shared_from_this
<Bucket
> {
160 friend class RGWDataAccess
;
163 RGWDataAccess
*sd
{nullptr};
164 RGWBucketInfo bucket_info
;
168 ceph::real_time mtime
;
169 map
<std::string
, bufferlist
> attrs
;
171 RGWAccessControlPolicy policy
;
174 Bucket(RGWDataAccess
*_sd
,
175 const string
& _tenant
,
177 const string
& _bucket_id
) : sd(_sd
),
180 bucket_id(_bucket_id
) {}
181 Bucket(RGWDataAccess
*_sd
) : sd(_sd
) {}
183 int init(const RGWBucketInfo
& _bucket_info
, const map
<string
, bufferlist
>& _attrs
);
185 int get_object(const rgw_obj_key
& key
,
192 RGWDataAccess
*sd
{nullptr};
196 ceph::real_time mtime
;
198 std::optional
<uint64_t> olh_epoch
;
199 ceph::real_time delete_at
;
200 std::optional
<string
> user_data
;
202 std::optional
<bufferlist
> aclbl
;
204 Object(RGWDataAccess
*_sd
,
206 const rgw_obj_key
& _key
) : sd(_sd
),
210 int put(bufferlist
& data
, map
<string
, bufferlist
>& attrs
, const DoutPrefixProvider
*dpp
, optional_yield y
); /* might modify attrs */
212 void set_mtime(const ceph::real_time
& _mtime
) {
216 void set_etag(const string
& _etag
) {
220 void set_olh_epoch(uint64_t epoch
) {
224 void set_delete_at(ceph::real_time _delete_at
) {
225 delete_at
= _delete_at
;
228 void set_user_data(const string
& _user_data
) {
229 user_data
= _user_data
;
232 void set_policy(const RGWAccessControlPolicy
& policy
);
237 int get_bucket(const string
& tenant
,
239 const string bucket_id
,
241 bucket
->reset(new Bucket(this, tenant
, name
, bucket_id
));
242 return (*bucket
)->init();
245 int get_bucket(const RGWBucketInfo
& bucket_info
,
246 const map
<string
, bufferlist
>& attrs
,
248 bucket
->reset(new Bucket(this));
249 return (*bucket
)->init(bucket_info
, attrs
);
255 using RGWDataAccessRef
= std::shared_ptr
<RGWDataAccess
>;