1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab ft=cpp
6 #include "common/errno.h"
7 #include "common/safe_io.h"
8 #include "librados/librados_asio.h"
9 #include "common/async/yield_context.h"
11 #include "include/types.h"
12 #include "include/stringify.h"
14 #include "rgw_common.h"
15 #include "rgw_rados.h"
16 #include "rgw_tools.h"
17 #include "rgw_acl_s3.h"
19 #include "rgw_putobj_processor.h"
20 #include "rgw_aio_throttle.h"
21 #include "rgw_compression.h"
23 #include "osd/osd_types.h"
25 #include "services/svc_sys_obj.h"
26 #include "services/svc_zone.h"
27 #include "services/svc_zone_utils.h"
29 #define dout_subsys ceph_subsys_rgw
30 #define dout_context g_ceph_context
32 #define READ_CHUNK_LEN (512 * 1024)
34 static std::map
<std::string
, std::string
>* ext_mime_map
;
36 int rgw_init_ioctx(librados::Rados
*rados
, const rgw_pool
& pool
,
37 librados::IoCtx
& ioctx
, bool create
,
40 int r
= rados
->ioctx_create(pool
.name
.c_str(), ioctx
);
41 if (r
== -ENOENT
&& create
) {
42 r
= rados
->pool_create(pool
.name
.c_str());
46 << " ERROR: librados::Rados::pool_create returned " << cpp_strerror(-r
)
47 << " (this can be due to a pool or placement group misconfiguration, e.g."
48 << " pg_num < pgp_num or mon_max_pg_per_osd exceeded)"
51 if (r
< 0 && r
!= -EEXIST
) {
55 r
= rados
->ioctx_create(pool
.name
.c_str(), ioctx
);
60 r
= ioctx
.application_enable(pg_pool_t::APPLICATION_NAME_RGW
, false);
61 if (r
< 0 && r
!= -EOPNOTSUPP
) {
66 // set pg_autoscale_bias
68 float bias
= g_conf().get_val
<double>("rgw_rados_pool_autoscale_bias");
69 int r
= rados
->mon_command(
70 "{\"prefix\": \"osd pool set\", \"pool\": \"" +
71 pool
.name
+ "\", \"var\": \"pg_autoscale_bias\", \"val\": \"" +
72 stringify(bias
) + "\"}",
75 dout(10) << __func__
<< " warning: failed to set pg_autoscale_bias on "
76 << pool
.name
<< dendl
;
79 int min
= g_conf().get_val
<uint64_t>("rgw_rados_pool_pg_num_min");
80 r
= rados
->mon_command(
81 "{\"prefix\": \"osd pool set\", \"pool\": \"" +
82 pool
.name
+ "\", \"var\": \"pg_num_min\", \"val\": \"" +
83 stringify(min
) + "\"}",
86 dout(10) << __func__
<< " warning: failed to set pg_num_min on "
87 << pool
.name
<< dendl
;
89 // set recovery_priority
90 int p
= g_conf().get_val
<uint64_t>("rgw_rados_pool_recovery_priority");
91 r
= rados
->mon_command(
92 "{\"prefix\": \"osd pool set\", \"pool\": \"" +
93 pool
.name
+ "\", \"var\": \"recovery_priority\": \"" +
97 dout(10) << __func__
<< " warning: failed to set recovery_priority on "
98 << pool
.name
<< dendl
;
104 if (!pool
.ns
.empty()) {
105 ioctx
.set_namespace(pool
.ns
);
110 void rgw_shard_name(const string
& prefix
, unsigned max_shards
, const string
& key
, string
& name
, int *shard_id
)
112 uint32_t val
= ceph_str_hash_linux(key
.c_str(), key
.size());
115 *shard_id
= val
% max_shards
;
117 snprintf(buf
, sizeof(buf
), "%u", (unsigned)(val
% max_shards
));
121 void rgw_shard_name(const string
& prefix
, unsigned max_shards
, const string
& section
, const string
& key
, string
& name
)
123 uint32_t val
= ceph_str_hash_linux(key
.c_str(), key
.size());
124 val
^= ceph_str_hash_linux(section
.c_str(), section
.size());
126 snprintf(buf
, sizeof(buf
), "%u", (unsigned)(val
% max_shards
));
130 void rgw_shard_name(const string
& prefix
, unsigned shard_id
, string
& name
)
133 snprintf(buf
, sizeof(buf
), "%u", shard_id
);
137 int rgw_parse_list_of_flags(struct rgw_name_to_flag
*mapping
,
138 const string
& str
, uint32_t *perm
)
141 get_str_list(str
, strs
);
142 list
<string
>::iterator iter
;
144 for (iter
= strs
.begin(); iter
!= strs
.end(); ++iter
) {
146 for (int i
= 0; mapping
[i
].type_name
; i
++) {
147 if (s
.compare(mapping
[i
].type_name
) == 0)
148 v
|= mapping
[i
].flag
;
156 int rgw_put_system_obj(RGWSysObjectCtx
& obj_ctx
, const rgw_pool
& pool
, const string
& oid
, bufferlist
& data
, bool exclusive
,
157 RGWObjVersionTracker
*objv_tracker
, real_time set_mtime
, optional_yield y
, map
<string
, bufferlist
> *pattrs
)
159 map
<string
,bufferlist
> no_attrs
;
164 rgw_raw_obj
obj(pool
, oid
);
166 auto sysobj
= obj_ctx
.get_obj(obj
);
167 int ret
= sysobj
.wop()
168 .set_objv_tracker(objv_tracker
)
169 .set_exclusive(exclusive
)
170 .set_mtime(set_mtime
)
177 int rgw_put_system_obj(RGWSysObjectCtx
& obj_ctx
, const rgw_pool
& pool
, const string
& oid
, bufferlist
& data
, bool exclusive
,
178 RGWObjVersionTracker
*objv_tracker
, real_time set_mtime
, map
<string
, bufferlist
> *pattrs
)
180 return rgw_put_system_obj(obj_ctx
, pool
, oid
, data
, exclusive
,
181 objv_tracker
, set_mtime
, null_yield
, pattrs
);
184 int rgw_get_system_obj(RGWSysObjectCtx
& obj_ctx
, const rgw_pool
& pool
, const string
& key
, bufferlist
& bl
,
185 RGWObjVersionTracker
*objv_tracker
, real_time
*pmtime
, optional_yield y
, map
<string
, bufferlist
> *pattrs
,
186 rgw_cache_entry_info
*cache_info
,
187 boost::optional
<obj_version
> refresh_version
)
189 bufferlist::iterator iter
;
190 int request_len
= READ_CHUNK_LEN
;
191 rgw_raw_obj
obj(pool
, key
);
193 obj_version original_readv
;
194 if (objv_tracker
&& !objv_tracker
->read_version
.empty()) {
195 original_readv
= objv_tracker
->read_version
;
199 auto sysobj
= obj_ctx
.get_obj(obj
);
200 auto rop
= sysobj
.rop();
202 int ret
= rop
.set_attrs(pattrs
)
203 .set_last_mod(pmtime
)
204 .set_objv_tracker(objv_tracker
)
209 ret
= rop
.set_cache_info(cache_info
)
210 .set_refresh_version(refresh_version
)
212 if (ret
== -ECANCELED
) {
214 if (!original_readv
.empty()) {
215 /* we were asked to read a specific obj_version, failed */
219 objv_tracker
->read_version
.clear();
227 if (ret
< request_len
)
236 int rgw_delete_system_obj(RGWSI_SysObj
*sysobj_svc
, const rgw_pool
& pool
, const string
& oid
,
237 RGWObjVersionTracker
*objv_tracker
)
239 auto obj_ctx
= sysobj_svc
->init_obj_ctx();
240 auto sysobj
= obj_ctx
.get_obj(rgw_raw_obj
{pool
, oid
});
241 rgw_raw_obj
obj(pool
, oid
);
243 .set_objv_tracker(objv_tracker
)
247 thread_local
bool is_asio_thread
= false;
249 int rgw_rados_operate(librados::IoCtx
& ioctx
, const std::string
& oid
,
250 librados::ObjectReadOperation
*op
, bufferlist
* pbl
,
253 #ifdef HAVE_BOOST_CONTEXT
254 // given a yield_context, call async_operate() to yield the coroutine instead
257 auto& context
= y
.get_io_context();
258 auto& yield
= y
.get_yield_context();
259 boost::system::error_code ec
;
260 auto bl
= librados::async_operate(context
, ioctx
, oid
, op
, 0, yield
[ec
]);
262 *pbl
= std::move(bl
);
266 // work on asio threads should be asynchronous, so warn when they block
267 if (is_asio_thread
) {
268 dout(20) << "WARNING: blocking librados call" << dendl
;
271 return ioctx
.operate(oid
, op
, nullptr);
274 int rgw_rados_operate(librados::IoCtx
& ioctx
, const std::string
& oid
,
275 librados::ObjectWriteOperation
*op
, optional_yield y
)
277 #ifdef HAVE_BOOST_CONTEXT
279 auto& context
= y
.get_io_context();
280 auto& yield
= y
.get_yield_context();
281 boost::system::error_code ec
;
282 librados::async_operate(context
, ioctx
, oid
, op
, 0, yield
[ec
]);
285 if (is_asio_thread
) {
286 dout(20) << "WARNING: blocking librados call" << dendl
;
289 return ioctx
.operate(oid
, op
);
292 int rgw_rados_notify(librados::IoCtx
& ioctx
, const std::string
& oid
,
293 bufferlist
& bl
, uint64_t timeout_ms
, bufferlist
* pbl
,
296 #ifdef HAVE_BOOST_CONTEXT
298 auto& context
= y
.get_io_context();
299 auto& yield
= y
.get_yield_context();
300 boost::system::error_code ec
;
301 auto reply
= librados::async_notify(context
, ioctx
, oid
,
302 bl
, timeout_ms
, yield
[ec
]);
304 *pbl
= std::move(reply
);
308 if (is_asio_thread
) {
309 dout(20) << "WARNING: blocking librados call" << dendl
;
312 return ioctx
.notify2(oid
, bl
, timeout_ms
, pbl
);
315 void parse_mime_map_line(const char *start
, const char *end
)
317 char line
[end
- start
+ 1];
318 strncpy(line
, start
, end
- start
);
319 line
[end
- start
] = '\0';
321 #define DELIMS " \t\n\r"
326 char *mime
= strsep(&l
, DELIMS
);
332 ext
= strsep(&l
, DELIMS
);
334 (*ext_mime_map
)[ext
] = mime
;
340 void parse_mime_map(const char *buf
)
342 const char *start
= buf
, *end
= buf
;
344 while (*end
&& *end
!= '\n') {
347 parse_mime_map_line(start
, end
);
353 static int ext_mime_map_init(CephContext
*cct
, const char *ext_map
)
355 int fd
= open(ext_map
, O_RDONLY
);
360 ldout(cct
, 0) << __func__
<< " failed to open file=" << ext_map
361 << " : " << cpp_strerror(-ret
) << dendl
;
366 ret
= fstat(fd
, &st
);
369 ldout(cct
, 0) << __func__
<< " failed to stat file=" << ext_map
370 << " : " << cpp_strerror(-ret
) << dendl
;
374 buf
= (char *)malloc(st
.st_size
+ 1);
377 ldout(cct
, 0) << __func__
<< " failed to allocate buf" << dendl
;
381 ret
= safe_read(fd
, buf
, st
.st_size
+ 1);
382 if (ret
!= st
.st_size
) {
383 // huh? file size has changed?
384 ldout(cct
, 0) << __func__
<< " raced! will retry.." << dendl
;
387 return ext_mime_map_init(cct
, ext_map
);
389 buf
[st
.st_size
] = '\0';
399 const char *rgw_find_mime_by_ext(string
& ext
)
401 map
<string
, string
>::iterator iter
= ext_mime_map
->find(ext
);
402 if (iter
== ext_mime_map
->end())
405 return iter
->second
.c_str();
408 void rgw_filter_attrset(map
<string
, bufferlist
>& unfiltered_attrset
, const string
& check_prefix
,
409 map
<string
, bufferlist
> *attrset
)
412 map
<string
, bufferlist
>::iterator iter
;
413 for (iter
= unfiltered_attrset
.lower_bound(check_prefix
);
414 iter
!= unfiltered_attrset
.end(); ++iter
) {
415 if (!boost::algorithm::starts_with(iter
->first
, check_prefix
))
417 (*attrset
)[iter
->first
] = iter
->second
;
421 RGWDataAccess::RGWDataAccess(rgw::sal::RGWRadosStore
*_store
) : store(_store
)
423 sysobj_ctx
= std::make_unique
<RGWSysObjectCtx
>(store
->svc()->sysobj
->init_obj_ctx());
427 int RGWDataAccess::Bucket::finish_init()
429 auto iter
= attrs
.find(RGW_ATTR_ACL
);
430 if (iter
== attrs
.end()) {
434 bufferlist::const_iterator bliter
= iter
->second
.begin();
436 policy
.decode(bliter
);
437 } catch (buffer::error
& err
) {
444 int RGWDataAccess::Bucket::init()
446 int ret
= sd
->store
->getRados()->get_bucket_info(sd
->store
->svc(),
456 return finish_init();
459 int RGWDataAccess::Bucket::init(const RGWBucketInfo
& _bucket_info
,
460 const map
<string
, bufferlist
>& _attrs
)
462 bucket_info
= _bucket_info
;
465 return finish_init();
468 int RGWDataAccess::Bucket::get_object(const rgw_obj_key
& key
,
470 obj
->reset(new Object(sd
, shared_from_this(), key
));
474 int RGWDataAccess::Object::put(bufferlist
& data
,
475 map
<string
, bufferlist
>& attrs
,
476 const DoutPrefixProvider
*dpp
,
479 rgw::sal::RGWRadosStore
*store
= sd
->store
;
480 CephContext
*cct
= store
->ctx();
483 append_rand_alpha(cct
, tag
, tag
, 32);
485 RGWBucketInfo
& bucket_info
= bucket
->bucket_info
;
487 rgw::BlockingAioThrottle
aio(store
->ctx()->_conf
->rgw_put_obj_min_window_size
);
489 RGWObjectCtx
obj_ctx(store
);
490 rgw_obj
obj(bucket_info
.bucket
, key
);
492 auto& owner
= bucket
->policy
.get_owner();
494 string req_id
= store
->svc()->zone_utils
->unique_id(store
->getRados()->get_new_req_id());
496 using namespace rgw::putobj
;
497 AtomicObjectProcessor
processor(&aio
, store
, bucket_info
, nullptr,
498 owner
.get_id(), obj_ctx
, obj
, olh_epoch
,
501 int ret
= processor
.prepare(y
);
505 DataProcessor
*filter
= &processor
;
507 CompressorRef plugin
;
508 boost::optional
<RGWPutObj_Compress
> compressor
;
510 const auto& compression_type
= store
->svc()->zone
->get_zone_params().get_compression_type(bucket_info
.placement_rule
);
511 if (compression_type
!= "none") {
512 plugin
= Compressor::create(store
->ctx(), compression_type
);
514 ldout(store
->ctx(), 1) << "Cannot load plugin for compression type "
515 << compression_type
<< dendl
;
517 compressor
.emplace(store
->ctx(), plugin
, filter
);
518 filter
= &*compressor
;
523 auto obj_size
= data
.length();
525 RGWMD5Etag etag_calc
;
528 size_t read_len
= std::min(data
.length(), (unsigned int)cct
->_conf
->rgw_max_chunk_size
);
532 data
.splice(0, read_len
, &bl
);
533 etag_calc
.update(bl
);
535 ret
= filter
->process(std::move(bl
), ofs
);
540 } while (data
.length() > 0);
542 ret
= filter
->process({}, ofs
);
546 bool has_etag_attr
= false;
547 auto iter
= attrs
.find(RGW_ATTR_ETAG
);
548 if (iter
!= attrs
.end()) {
549 bufferlist
& bl
= iter
->second
;
551 has_etag_attr
= true;
555 RGWAccessControlPolicy_S3
policy(cct
);
557 policy
.create_canned(bucket
->policy
.get_owner(), bucket
->policy
.get_owner(), string()); /* default private policy */
559 policy
.encode(aclbl
.emplace());
563 etag_calc
.finish(&etag
);
566 if (!has_etag_attr
) {
569 attrs
[RGW_ATTR_ETAG
] = etagbl
;
571 attrs
[RGW_ATTR_ACL
] = *aclbl
;
573 string
*puser_data
= nullptr;
575 puser_data
= &(*user_data
);
578 return processor
.complete(obj_size
, etag
,
583 nullptr, nullptr, y
);
586 void RGWDataAccess::Object::set_policy(const RGWAccessControlPolicy
& policy
)
588 policy
.encode(aclbl
.emplace());
591 int rgw_tools_init(CephContext
*cct
)
593 ext_mime_map
= new std::map
<std::string
, std::string
>;
594 ext_mime_map_init(cct
, cct
->_conf
->rgw_mime_types_file
.c_str());
595 // ignore errors; missing mime.types is not fatal
599 void rgw_tools_cleanup()
602 ext_mime_map
= nullptr;