]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
9f95a23c | 2 | // vim: ts=8 sw=2 smarttab ft=cpp |
7c673cae FG |
3 | |
4 | #ifndef CEPH_RGW_TOOLS_H | |
5 | #define CEPH_RGW_TOOLS_H | |
6 | ||
7 | #include <string> | |
8 | ||
9 | #include "include/types.h" | |
9f95a23c TL |
10 | #include "include/ceph_hash.h" |
11 | ||
7c673cae | 12 | #include "common/ceph_time.h" |
9f95a23c | 13 | |
7c673cae FG |
14 | #include "rgw_common.h" |
15 | ||
9f95a23c TL |
16 | class RGWSI_SysObj; |
17 | ||
7c673cae | 18 | class RGWRados; |
11fdf7f2 | 19 | class RGWSysObjectCtx; |
7c673cae | 20 | struct RGWObjVersionTracker; |
11fdf7f2 | 21 | class optional_yield; |
9f95a23c TL |
22 | namespace rgw { namespace sal { |
23 | class RGWRadosStore; | |
24 | } } | |
7c673cae FG |
25 | |
26 | struct obj_version; | |
27 | ||
9f95a23c | 28 | |
494da23a TL |
29 | int rgw_init_ioctx(librados::Rados *rados, const rgw_pool& pool, |
30 | librados::IoCtx& ioctx, | |
31 | bool create = false, | |
32 | bool mostly_omap = false); | |
33 | ||
9f95a23c TL |
34 | #define RGW_NO_SHARD -1 |
35 | ||
36 | #define RGW_SHARDS_PRIME_0 7877 | |
37 | #define RGW_SHARDS_PRIME_1 65521 | |
38 | ||
39 | extern const std::string MP_META_SUFFIX; | |
40 | ||
41 | static inline int rgw_shards_max() | |
42 | { | |
43 | return RGW_SHARDS_PRIME_1; | |
44 | } | |
45 | ||
46 | // only called by rgw_shard_id and rgw_bucket_shard_index | |
47 | static inline int rgw_shards_mod(unsigned hval, int max_shards) | |
48 | { | |
49 | if (max_shards <= RGW_SHARDS_PRIME_0) { | |
50 | return hval % RGW_SHARDS_PRIME_0 % max_shards; | |
51 | } | |
52 | return hval % RGW_SHARDS_PRIME_1 % max_shards; | |
53 | } | |
54 | ||
55 | // used for logging and tagging | |
56 | static inline int rgw_shard_id(const string& key, int max_shards) | |
57 | { | |
58 | return rgw_shards_mod(ceph_str_hash_linux(key.c_str(), key.size()), | |
59 | max_shards); | |
60 | } | |
61 | ||
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); | |
65 | ||
66 | struct rgw_name_to_flag { | |
67 | const char *type_name; | |
68 | uint32_t flag; | |
69 | }; | |
70 | ||
71 | int rgw_parse_list_of_flags(struct rgw_name_to_flag *mapping, | |
72 | const string& str, uint32_t *perm); | |
73 | ||
74 | int rgw_put_system_obj(RGWSysObjectCtx& obj_ctx, const rgw_pool& pool, const string& oid, bufferlist& data, bool exclusive, | |
7c673cae | 75 | RGWObjVersionTracker *objv_tracker, real_time set_mtime, map<string, bufferlist> *pattrs = NULL); |
9f95a23c TL |
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, | |
b32b8144 FG |
80 | rgw_cache_entry_info *cache_info = NULL, |
81 | boost::optional<obj_version> refresh_version = boost::none); | |
9f95a23c | 82 | int rgw_delete_system_obj(RGWSI_SysObj *sysobj_svc, const rgw_pool& pool, const string& oid, |
7c673cae FG |
83 | RGWObjVersionTracker *objv_tracker); |
84 | ||
11fdf7f2 TL |
85 | const char *rgw_find_mime_by_ext(string& ext); |
86 | ||
87 | void rgw_filter_attrset(map<string, bufferlist>& unfiltered_attrset, const string& check_prefix, | |
88 | map<string, bufferlist> *attrset); | |
89 | ||
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; | |
93 | ||
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, | |
f6b5b4d7 | 97 | optional_yield y, int flags = 0); |
11fdf7f2 | 98 | int rgw_rados_operate(librados::IoCtx& ioctx, const std::string& oid, |
f6b5b4d7 TL |
99 | librados::ObjectWriteOperation *op, optional_yield y, |
100 | int flags = 0); | |
9f95a23c TL |
101 | int rgw_rados_notify(librados::IoCtx& ioctx, const std::string& oid, |
102 | bufferlist& bl, uint64_t timeout_ms, bufferlist* pbl, | |
103 | optional_yield y); | |
11fdf7f2 | 104 | |
7c673cae FG |
105 | int rgw_tools_init(CephContext *cct); |
106 | void rgw_tools_cleanup(); | |
11fdf7f2 TL |
107 | |
108 | template<class H, size_t S> | |
109 | class RGWEtag | |
110 | { | |
111 | H hash; | |
112 | ||
113 | public: | |
114 | RGWEtag() {} | |
115 | ||
116 | void update(const char *buf, size_t len) { | |
117 | hash.Update((const unsigned char *)buf, len); | |
118 | } | |
119 | ||
120 | void update(bufferlist& bl) { | |
121 | if (bl.length() > 0) { | |
122 | update(bl.c_str(), bl.length()); | |
123 | } | |
124 | } | |
125 | ||
126 | void update(const string& s) { | |
127 | if (!s.empty()) { | |
128 | update(s.c_str(), s.size()); | |
129 | } | |
130 | } | |
131 | void finish(string *etag) { | |
132 | char etag_buf[S]; | |
133 | char etag_buf_str[S * 2 + 16]; | |
134 | ||
135 | hash.Final((unsigned char *)etag_buf); | |
136 | buf_to_hex((const unsigned char *)etag_buf, S, | |
137 | etag_buf_str); | |
138 | ||
139 | *etag = etag_buf_str; | |
140 | } | |
141 | }; | |
142 | ||
143 | using RGWMD5Etag = RGWEtag<MD5, CEPH_CRYPTO_MD5_DIGESTSIZE>; | |
144 | ||
145 | class RGWDataAccess | |
146 | { | |
9f95a23c | 147 | rgw::sal::RGWRadosStore *store; |
11fdf7f2 TL |
148 | std::unique_ptr<RGWSysObjectCtx> sysobj_ctx; |
149 | ||
150 | public: | |
9f95a23c | 151 | RGWDataAccess(rgw::sal::RGWRadosStore *_store); |
11fdf7f2 TL |
152 | |
153 | class Object; | |
154 | class Bucket; | |
155 | ||
156 | using BucketRef = std::shared_ptr<Bucket>; | |
157 | using ObjectRef = std::shared_ptr<Object>; | |
158 | ||
159 | class Bucket : public enable_shared_from_this<Bucket> { | |
160 | friend class RGWDataAccess; | |
161 | friend class Object; | |
162 | ||
163 | RGWDataAccess *sd{nullptr}; | |
164 | RGWBucketInfo bucket_info; | |
165 | string tenant; | |
166 | string name; | |
167 | string bucket_id; | |
168 | ceph::real_time mtime; | |
169 | map<std::string, bufferlist> attrs; | |
170 | ||
171 | RGWAccessControlPolicy policy; | |
172 | int finish_init(); | |
173 | ||
174 | Bucket(RGWDataAccess *_sd, | |
175 | const string& _tenant, | |
176 | const string& _name, | |
177 | const string& _bucket_id) : sd(_sd), | |
178 | tenant(_tenant), | |
179 | name(_name), | |
180 | bucket_id(_bucket_id) {} | |
181 | Bucket(RGWDataAccess *_sd) : sd(_sd) {} | |
182 | int init(); | |
183 | int init(const RGWBucketInfo& _bucket_info, const map<string, bufferlist>& _attrs); | |
184 | public: | |
185 | int get_object(const rgw_obj_key& key, | |
186 | ObjectRef *obj); | |
187 | ||
188 | }; | |
189 | ||
190 | ||
191 | class Object { | |
192 | RGWDataAccess *sd{nullptr}; | |
193 | BucketRef bucket; | |
194 | rgw_obj_key key; | |
195 | ||
196 | ceph::real_time mtime; | |
197 | string etag; | |
198 | std::optional<uint64_t> olh_epoch; | |
199 | ceph::real_time delete_at; | |
200 | std::optional<string> user_data; | |
201 | ||
202 | std::optional<bufferlist> aclbl; | |
203 | ||
204 | Object(RGWDataAccess *_sd, | |
205 | BucketRef&& _bucket, | |
206 | const rgw_obj_key& _key) : sd(_sd), | |
207 | bucket(_bucket), | |
208 | key(_key) {} | |
209 | public: | |
9f95a23c | 210 | int put(bufferlist& data, map<string, bufferlist>& attrs, const DoutPrefixProvider *dpp, optional_yield y); /* might modify attrs */ |
11fdf7f2 TL |
211 | |
212 | void set_mtime(const ceph::real_time& _mtime) { | |
213 | mtime = _mtime; | |
214 | } | |
215 | ||
216 | void set_etag(const string& _etag) { | |
217 | etag = _etag; | |
218 | } | |
219 | ||
220 | void set_olh_epoch(uint64_t epoch) { | |
221 | olh_epoch = epoch; | |
222 | } | |
223 | ||
224 | void set_delete_at(ceph::real_time _delete_at) { | |
225 | delete_at = _delete_at; | |
226 | } | |
227 | ||
228 | void set_user_data(const string& _user_data) { | |
229 | user_data = _user_data; | |
230 | } | |
231 | ||
232 | void set_policy(const RGWAccessControlPolicy& policy); | |
233 | ||
234 | friend class Bucket; | |
235 | }; | |
236 | ||
237 | int get_bucket(const string& tenant, | |
238 | const string name, | |
239 | const string bucket_id, | |
240 | BucketRef *bucket) { | |
241 | bucket->reset(new Bucket(this, tenant, name, bucket_id)); | |
242 | return (*bucket)->init(); | |
243 | } | |
244 | ||
245 | int get_bucket(const RGWBucketInfo& bucket_info, | |
246 | const map<string, bufferlist>& attrs, | |
247 | BucketRef *bucket) { | |
248 | bucket->reset(new Bucket(this)); | |
249 | return (*bucket)->init(bucket_info, attrs); | |
250 | } | |
251 | friend class Bucket; | |
252 | friend class Object; | |
253 | }; | |
254 | ||
255 | using RGWDataAccessRef = std::shared_ptr<RGWDataAccess>; | |
7c673cae FG |
256 | |
257 | #endif |