]> git.proxmox.com Git - ceph.git/blame - ceph/src/rgw/rgw_tools.h
import ceph pacific 16.2.5
[ceph.git] / ceph / src / rgw / rgw_tools.h
CommitLineData
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
16class RGWSI_SysObj;
17
7c673cae 18class RGWRados;
11fdf7f2 19class RGWSysObjectCtx;
7c673cae 20struct RGWObjVersionTracker;
11fdf7f2 21class optional_yield;
9f95a23c
TL
22namespace rgw { namespace sal {
23 class RGWRadosStore;
24} }
7c673cae
FG
25
26struct obj_version;
27
9f95a23c 28
b3b6e05e
TL
29int rgw_init_ioctx(const DoutPrefixProvider *dpp,
30 librados::Rados *rados, const rgw_pool& pool,
494da23a
TL
31 librados::IoCtx& ioctx,
32 bool create = false,
33 bool mostly_omap = false);
34
9f95a23c
TL
35#define RGW_NO_SHARD -1
36
37#define RGW_SHARDS_PRIME_0 7877
38#define RGW_SHARDS_PRIME_1 65521
39
40extern const std::string MP_META_SUFFIX;
41
f67539c2 42inline int rgw_shards_max()
9f95a23c
TL
43{
44 return RGW_SHARDS_PRIME_1;
45}
46
47// only called by rgw_shard_id and rgw_bucket_shard_index
48static inline int rgw_shards_mod(unsigned hval, int max_shards)
49{
50 if (max_shards <= RGW_SHARDS_PRIME_0) {
51 return hval % RGW_SHARDS_PRIME_0 % max_shards;
52 }
53 return hval % RGW_SHARDS_PRIME_1 % max_shards;
54}
55
56// used for logging and tagging
f67539c2 57inline int rgw_shard_id(const string& key, int max_shards)
9f95a23c
TL
58{
59 return rgw_shards_mod(ceph_str_hash_linux(key.c_str(), key.size()),
60 max_shards);
61}
62
63void rgw_shard_name(const string& prefix, unsigned max_shards, const string& key, string& name, int *shard_id);
64void rgw_shard_name(const string& prefix, unsigned max_shards, const string& section, const string& key, string& name);
65void rgw_shard_name(const string& prefix, unsigned shard_id, string& name);
66
67struct rgw_name_to_flag {
68 const char *type_name;
69 uint32_t flag;
70};
71
72int rgw_parse_list_of_flags(struct rgw_name_to_flag *mapping,
73 const string& str, uint32_t *perm);
74
b3b6e05e 75int rgw_put_system_obj(const DoutPrefixProvider *dpp, RGWSysObjectCtx& obj_ctx, const rgw_pool& pool, const string& oid, bufferlist& data, bool exclusive,
9f95a23c
TL
76 RGWObjVersionTracker *objv_tracker, real_time set_mtime, optional_yield y, map<string, bufferlist> *pattrs = NULL);
77int rgw_get_system_obj(RGWSysObjectCtx& obj_ctx, const rgw_pool& pool, const string& key, bufferlist& bl,
b3b6e05e 78 RGWObjVersionTracker *objv_tracker, real_time *pmtime, optional_yield y, const DoutPrefixProvider *dpp, map<string, bufferlist> *pattrs = NULL,
b32b8144
FG
79 rgw_cache_entry_info *cache_info = NULL,
80 boost::optional<obj_version> refresh_version = boost::none);
b3b6e05e
TL
81int rgw_delete_system_obj(const DoutPrefixProvider *dpp,
82 RGWSI_SysObj *sysobj_svc, const rgw_pool& pool, const string& oid,
f67539c2 83 RGWObjVersionTracker *objv_tracker, optional_yield y);
7c673cae 84
11fdf7f2
TL
85const char *rgw_find_mime_by_ext(string& ext);
86
87void 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
92extern thread_local bool is_asio_thread;
93
94/// perform the rados operation, using the yield context when given
b3b6e05e 95int rgw_rados_operate(const DoutPrefixProvider *dpp, librados::IoCtx& ioctx, const std::string& oid,
11fdf7f2 96 librados::ObjectReadOperation *op, bufferlist* pbl,
f6b5b4d7 97 optional_yield y, int flags = 0);
b3b6e05e 98int rgw_rados_operate(const DoutPrefixProvider *dpp, librados::IoCtx& ioctx, const std::string& oid,
f6b5b4d7
TL
99 librados::ObjectWriteOperation *op, optional_yield y,
100 int flags = 0);
b3b6e05e 101int rgw_rados_notify(const DoutPrefixProvider *dpp, librados::IoCtx& ioctx, const std::string& oid,
9f95a23c
TL
102 bufferlist& bl, uint64_t timeout_ms, bufferlist* pbl,
103 optional_yield y);
11fdf7f2 104
7c673cae
FG
105int rgw_tools_init(CephContext *cct);
106void rgw_tools_cleanup();
11fdf7f2
TL
107
108template<class H, size_t S>
109class RGWEtag
110{
111 H hash;
112
113public:
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
143using RGWMD5Etag = RGWEtag<MD5, CEPH_CRYPTO_MD5_DIGESTSIZE>;
144
145class RGWDataAccess
146{
9f95a23c 147 rgw::sal::RGWRadosStore *store;
11fdf7f2
TL
148 std::unique_ptr<RGWSysObjectCtx> sysobj_ctx;
149
150public:
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) {}
b3b6e05e 182 int init(const DoutPrefixProvider *dpp, optional_yield y);
11fdf7f2
TL
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
b3b6e05e
TL
237 int get_bucket(const DoutPrefixProvider *dpp,
238 const string& tenant,
11fdf7f2
TL
239 const string name,
240 const string bucket_id,
f67539c2
TL
241 BucketRef *bucket,
242 optional_yield y) {
11fdf7f2 243 bucket->reset(new Bucket(this, tenant, name, bucket_id));
b3b6e05e 244 return (*bucket)->init(dpp, y);
11fdf7f2
TL
245 }
246
247 int get_bucket(const RGWBucketInfo& bucket_info,
248 const map<string, bufferlist>& attrs,
249 BucketRef *bucket) {
250 bucket->reset(new Bucket(this));
251 return (*bucket)->init(bucket_info, attrs);
252 }
253 friend class Bucket;
254 friend class Object;
255};
256
257using RGWDataAccessRef = std::shared_ptr<RGWDataAccess>;
7c673cae 258
f67539c2
TL
259/// Complete an AioCompletion. To return error values or otherwise
260/// satisfy the caller. Useful for making complicated asynchronous
261/// calls and error handling.
262void rgw_complete_aio_completion(librados::AioCompletion* c, int r);
263
7c673cae 264#endif