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 "librados/AioCompletionImpl.h"
16 #include "rgw_common.h"
17 #include "rgw_tools.h"
18 #include "rgw_acl_s3.h"
20 #include "rgw_putobj_processor.h"
21 #include "rgw_aio_throttle.h"
22 #include "rgw_compression.h"
24 #include "rgw_sal_rados.h"
25 #include "osd/osd_types.h"
27 #include "services/svc_sys_obj.h"
28 #include "services/svc_zone.h"
29 #include "services/svc_zone_utils.h"
31 #define dout_subsys ceph_subsys_rgw
32 #define dout_context g_ceph_context
34 #define READ_CHUNK_LEN (512 * 1024)
36 static std::map
<std::string
, std::string
>* ext_mime_map
;
38 int rgw_init_ioctx(const DoutPrefixProvider
*dpp
,
39 librados::Rados
*rados
, const rgw_pool
& pool
,
40 librados::IoCtx
& ioctx
, bool create
,
43 int r
= rados
->ioctx_create(pool
.name
.c_str(), ioctx
);
44 if (r
== -ENOENT
&& create
) {
45 r
= rados
->pool_create(pool
.name
.c_str());
49 << " ERROR: librados::Rados::pool_create returned " << cpp_strerror(-r
)
50 << " (this can be due to a pool or placement group misconfiguration, e.g."
51 << " pg_num < pgp_num or mon_max_pg_per_osd exceeded)"
54 if (r
< 0 && r
!= -EEXIST
) {
58 r
= rados
->ioctx_create(pool
.name
.c_str(), ioctx
);
63 r
= ioctx
.application_enable(pg_pool_t::APPLICATION_NAME_RGW
, false);
64 if (r
< 0 && r
!= -EOPNOTSUPP
) {
69 // set pg_autoscale_bias
71 float bias
= g_conf().get_val
<double>("rgw_rados_pool_autoscale_bias");
72 int r
= rados
->mon_command(
73 "{\"prefix\": \"osd pool set\", \"pool\": \"" +
74 pool
.name
+ "\", \"var\": \"pg_autoscale_bias\", \"val\": \"" +
75 stringify(bias
) + "\"}",
78 ldpp_dout(dpp
, 10) << __func__
<< " warning: failed to set pg_autoscale_bias on "
79 << pool
.name
<< dendl
;
82 int min
= g_conf().get_val
<uint64_t>("rgw_rados_pool_pg_num_min");
83 r
= rados
->mon_command(
84 "{\"prefix\": \"osd pool set\", \"pool\": \"" +
85 pool
.name
+ "\", \"var\": \"pg_num_min\", \"val\": \"" +
86 stringify(min
) + "\"}",
89 ldpp_dout(dpp
, 10) << __func__
<< " warning: failed to set pg_num_min on "
90 << pool
.name
<< dendl
;
92 // set recovery_priority
93 int p
= g_conf().get_val
<uint64_t>("rgw_rados_pool_recovery_priority");
94 r
= rados
->mon_command(
95 "{\"prefix\": \"osd pool set\", \"pool\": \"" +
96 pool
.name
+ "\", \"var\": \"recovery_priority\": \"" +
100 ldpp_dout(dpp
, 10) << __func__
<< " warning: failed to set recovery_priority on "
101 << pool
.name
<< dendl
;
107 if (!pool
.ns
.empty()) {
108 ioctx
.set_namespace(pool
.ns
);
113 void rgw_shard_name(const string
& prefix
, unsigned max_shards
, const string
& key
, string
& name
, int *shard_id
)
115 uint32_t val
= ceph_str_hash_linux(key
.c_str(), key
.size());
118 *shard_id
= val
% max_shards
;
120 snprintf(buf
, sizeof(buf
), "%u", (unsigned)(val
% max_shards
));
124 void rgw_shard_name(const string
& prefix
, unsigned max_shards
, const string
& section
, const string
& key
, string
& name
)
126 uint32_t val
= ceph_str_hash_linux(key
.c_str(), key
.size());
127 val
^= ceph_str_hash_linux(section
.c_str(), section
.size());
129 snprintf(buf
, sizeof(buf
), "%u", (unsigned)(val
% max_shards
));
133 void rgw_shard_name(const string
& prefix
, unsigned shard_id
, string
& name
)
136 snprintf(buf
, sizeof(buf
), "%u", shard_id
);
140 int rgw_parse_list_of_flags(struct rgw_name_to_flag
*mapping
,
141 const string
& str
, uint32_t *perm
)
144 get_str_list(str
, strs
);
145 list
<string
>::iterator iter
;
147 for (iter
= strs
.begin(); iter
!= strs
.end(); ++iter
) {
149 for (int i
= 0; mapping
[i
].type_name
; i
++) {
150 if (s
.compare(mapping
[i
].type_name
) == 0)
151 v
|= mapping
[i
].flag
;
159 int rgw_put_system_obj(const DoutPrefixProvider
*dpp
,
160 RGWSysObjectCtx
& obj_ctx
, const rgw_pool
& pool
, const string
& oid
, bufferlist
& data
, bool exclusive
,
161 RGWObjVersionTracker
*objv_tracker
, real_time set_mtime
, optional_yield y
, map
<string
, bufferlist
> *pattrs
)
163 map
<string
,bufferlist
> no_attrs
;
168 rgw_raw_obj
obj(pool
, oid
);
170 auto sysobj
= obj_ctx
.get_obj(obj
);
171 int ret
= sysobj
.wop()
172 .set_objv_tracker(objv_tracker
)
173 .set_exclusive(exclusive
)
174 .set_mtime(set_mtime
)
176 .write(dpp
, data
, y
);
181 int rgw_get_system_obj(RGWSysObjectCtx
& obj_ctx
, const rgw_pool
& pool
, const string
& key
, bufferlist
& bl
,
182 RGWObjVersionTracker
*objv_tracker
, real_time
*pmtime
, optional_yield y
, const DoutPrefixProvider
*dpp
, map
<string
, bufferlist
> *pattrs
,
183 rgw_cache_entry_info
*cache_info
,
184 boost::optional
<obj_version
> refresh_version
)
186 bufferlist::iterator iter
;
187 int request_len
= READ_CHUNK_LEN
;
188 rgw_raw_obj
obj(pool
, key
);
190 obj_version original_readv
;
191 if (objv_tracker
&& !objv_tracker
->read_version
.empty()) {
192 original_readv
= objv_tracker
->read_version
;
196 auto sysobj
= obj_ctx
.get_obj(obj
);
197 auto rop
= sysobj
.rop();
199 int ret
= rop
.set_attrs(pattrs
)
200 .set_last_mod(pmtime
)
201 .set_objv_tracker(objv_tracker
)
206 ret
= rop
.set_cache_info(cache_info
)
207 .set_refresh_version(refresh_version
)
209 if (ret
== -ECANCELED
) {
211 if (!original_readv
.empty()) {
212 /* we were asked to read a specific obj_version, failed */
216 objv_tracker
->read_version
.clear();
224 if (ret
< request_len
)
233 int rgw_delete_system_obj(const DoutPrefixProvider
*dpp
,
234 RGWSI_SysObj
*sysobj_svc
, const rgw_pool
& pool
, const string
& oid
,
235 RGWObjVersionTracker
*objv_tracker
, optional_yield y
)
237 auto obj_ctx
= sysobj_svc
->init_obj_ctx();
238 auto sysobj
= obj_ctx
.get_obj(rgw_raw_obj
{pool
, oid
});
239 rgw_raw_obj
obj(pool
, oid
);
241 .set_objv_tracker(objv_tracker
)
245 thread_local
bool is_asio_thread
= false;
247 int rgw_rados_operate(const DoutPrefixProvider
*dpp
, librados::IoCtx
& ioctx
, const std::string
& oid
,
248 librados::ObjectReadOperation
*op
, bufferlist
* pbl
,
249 optional_yield y
, int flags
)
251 // given a yield_context, call async_operate() to yield the coroutine instead
254 auto& context
= y
.get_io_context();
255 auto& yield
= y
.get_yield_context();
256 boost::system::error_code ec
;
257 auto bl
= librados::async_operate(
258 context
, ioctx
, oid
, op
, flags
, yield
[ec
]);
260 *pbl
= std::move(bl
);
264 // work on asio threads should be asynchronous, so warn when they block
265 if (is_asio_thread
) {
266 ldpp_dout(dpp
, 20) << "WARNING: blocking librados call" << dendl
;
268 return ioctx
.operate(oid
, op
, nullptr, flags
);
271 int rgw_rados_operate(const DoutPrefixProvider
*dpp
, librados::IoCtx
& ioctx
, const std::string
& oid
,
272 librados::ObjectWriteOperation
*op
, optional_yield y
,
276 auto& context
= y
.get_io_context();
277 auto& yield
= y
.get_yield_context();
278 boost::system::error_code ec
;
279 librados::async_operate(context
, ioctx
, oid
, op
, flags
, yield
[ec
]);
282 if (is_asio_thread
) {
283 ldpp_dout(dpp
, 20) << "WARNING: blocking librados call" << dendl
;
285 return ioctx
.operate(oid
, op
, flags
);
288 int rgw_rados_notify(const DoutPrefixProvider
*dpp
, librados::IoCtx
& ioctx
, const std::string
& oid
,
289 bufferlist
& bl
, uint64_t timeout_ms
, bufferlist
* pbl
,
293 auto& context
= y
.get_io_context();
294 auto& yield
= y
.get_yield_context();
295 boost::system::error_code ec
;
296 auto reply
= librados::async_notify(context
, ioctx
, oid
,
297 bl
, timeout_ms
, yield
[ec
]);
299 *pbl
= std::move(reply
);
303 if (is_asio_thread
) {
304 ldpp_dout(dpp
, 20) << "WARNING: blocking librados call" << dendl
;
306 return ioctx
.notify2(oid
, bl
, timeout_ms
, pbl
);
309 void parse_mime_map_line(const char *start
, const char *end
)
311 char line
[end
- start
+ 1];
312 strncpy(line
, start
, end
- start
);
313 line
[end
- start
] = '\0';
315 #define DELIMS " \t\n\r"
320 char *mime
= strsep(&l
, DELIMS
);
326 ext
= strsep(&l
, DELIMS
);
328 (*ext_mime_map
)[ext
] = mime
;
334 void parse_mime_map(const char *buf
)
336 const char *start
= buf
, *end
= buf
;
338 while (*end
&& *end
!= '\n') {
341 parse_mime_map_line(start
, end
);
347 static int ext_mime_map_init(CephContext
*cct
, const char *ext_map
)
349 int fd
= open(ext_map
, O_RDONLY
);
354 ldout(cct
, 0) << __func__
<< " failed to open file=" << ext_map
355 << " : " << cpp_strerror(-ret
) << dendl
;
360 ret
= fstat(fd
, &st
);
363 ldout(cct
, 0) << __func__
<< " failed to stat file=" << ext_map
364 << " : " << cpp_strerror(-ret
) << dendl
;
368 buf
= (char *)malloc(st
.st_size
+ 1);
371 ldout(cct
, 0) << __func__
<< " failed to allocate buf" << dendl
;
375 ret
= safe_read(fd
, buf
, st
.st_size
+ 1);
376 if (ret
!= st
.st_size
) {
377 // huh? file size has changed?
378 ldout(cct
, 0) << __func__
<< " raced! will retry.." << dendl
;
381 return ext_mime_map_init(cct
, ext_map
);
383 buf
[st
.st_size
] = '\0';
393 const char *rgw_find_mime_by_ext(string
& ext
)
395 map
<string
, string
>::iterator iter
= ext_mime_map
->find(ext
);
396 if (iter
== ext_mime_map
->end())
399 return iter
->second
.c_str();
402 void rgw_filter_attrset(map
<string
, bufferlist
>& unfiltered_attrset
, const string
& check_prefix
,
403 map
<string
, bufferlist
> *attrset
)
406 map
<string
, bufferlist
>::iterator iter
;
407 for (iter
= unfiltered_attrset
.lower_bound(check_prefix
);
408 iter
!= unfiltered_attrset
.end(); ++iter
) {
409 if (!boost::algorithm::starts_with(iter
->first
, check_prefix
))
411 (*attrset
)[iter
->first
] = iter
->second
;
415 RGWDataAccess::RGWDataAccess(rgw::sal::RGWRadosStore
*_store
) : store(_store
)
417 sysobj_ctx
= std::make_unique
<RGWSysObjectCtx
>(store
->svc()->sysobj
->init_obj_ctx());
421 int RGWDataAccess::Bucket::finish_init()
423 auto iter
= attrs
.find(RGW_ATTR_ACL
);
424 if (iter
== attrs
.end()) {
428 bufferlist::const_iterator bliter
= iter
->second
.begin();
430 policy
.decode(bliter
);
431 } catch (buffer::error
& err
) {
438 int RGWDataAccess::Bucket::init(const DoutPrefixProvider
*dpp
, optional_yield y
)
440 int ret
= sd
->store
->getRados()->get_bucket_info(sd
->store
->svc(),
451 return finish_init();
454 int RGWDataAccess::Bucket::init(const RGWBucketInfo
& _bucket_info
,
455 const map
<string
, bufferlist
>& _attrs
)
457 bucket_info
= _bucket_info
;
460 return finish_init();
463 int RGWDataAccess::Bucket::get_object(const rgw_obj_key
& key
,
465 obj
->reset(new Object(sd
, shared_from_this(), key
));
469 int RGWDataAccess::Object::put(bufferlist
& data
,
470 map
<string
, bufferlist
>& attrs
,
471 const DoutPrefixProvider
*dpp
,
474 rgw::sal::RGWRadosStore
*store
= sd
->store
;
475 CephContext
*cct
= store
->ctx();
478 append_rand_alpha(cct
, tag
, tag
, 32);
480 RGWBucketInfo
& bucket_info
= bucket
->bucket_info
;
482 rgw::BlockingAioThrottle
aio(store
->ctx()->_conf
->rgw_put_obj_min_window_size
);
484 RGWObjectCtx
obj_ctx(store
);
485 std::unique_ptr
<rgw::sal::RGWBucket
> b
;
486 store
->get_bucket(NULL
, bucket_info
, &b
);
487 std::unique_ptr
<rgw::sal::RGWObject
> obj
= b
->get_object(key
);
489 auto& owner
= bucket
->policy
.get_owner();
491 string req_id
= store
->svc()->zone_utils
->unique_id(store
->getRados()->get_new_req_id());
493 using namespace rgw::putobj
;
494 AtomicObjectProcessor
processor(&aio
, store
, b
.get(), nullptr,
495 owner
.get_id(), obj_ctx
, std::move(obj
), olh_epoch
,
498 int ret
= processor
.prepare(y
);
502 DataProcessor
*filter
= &processor
;
504 CompressorRef plugin
;
505 boost::optional
<RGWPutObj_Compress
> compressor
;
507 const auto& compression_type
= store
->svc()->zone
->get_zone_params().get_compression_type(bucket_info
.placement_rule
);
508 if (compression_type
!= "none") {
509 plugin
= Compressor::create(store
->ctx(), compression_type
);
511 ldout(store
->ctx(), 1) << "Cannot load plugin for compression type "
512 << compression_type
<< dendl
;
514 compressor
.emplace(store
->ctx(), plugin
, filter
);
515 filter
= &*compressor
;
520 auto obj_size
= data
.length();
522 RGWMD5Etag etag_calc
;
525 size_t read_len
= std::min(data
.length(), (unsigned int)cct
->_conf
->rgw_max_chunk_size
);
529 data
.splice(0, read_len
, &bl
);
530 etag_calc
.update(bl
);
532 ret
= filter
->process(std::move(bl
), ofs
);
537 } while (data
.length() > 0);
539 ret
= filter
->process({}, ofs
);
543 bool has_etag_attr
= false;
544 auto iter
= attrs
.find(RGW_ATTR_ETAG
);
545 if (iter
!= attrs
.end()) {
546 bufferlist
& bl
= iter
->second
;
548 has_etag_attr
= true;
552 RGWAccessControlPolicy_S3
policy(cct
);
554 policy
.create_canned(bucket
->policy
.get_owner(), bucket
->policy
.get_owner(), string()); /* default private policy */
556 policy
.encode(aclbl
.emplace());
560 etag_calc
.finish(&etag
);
563 if (!has_etag_attr
) {
566 attrs
[RGW_ATTR_ETAG
] = etagbl
;
568 attrs
[RGW_ATTR_ACL
] = *aclbl
;
570 string
*puser_data
= nullptr;
572 puser_data
= &(*user_data
);
575 return processor
.complete(obj_size
, etag
,
580 nullptr, nullptr, y
);
583 void RGWDataAccess::Object::set_policy(const RGWAccessControlPolicy
& policy
)
585 policy
.encode(aclbl
.emplace());
588 int rgw_tools_init(CephContext
*cct
)
590 ext_mime_map
= new std::map
<std::string
, std::string
>;
591 ext_mime_map_init(cct
, cct
->_conf
->rgw_mime_types_file
.c_str());
592 // ignore errors; missing mime.types is not fatal
596 void rgw_tools_cleanup()
599 ext_mime_map
= nullptr;
602 void rgw_complete_aio_completion(librados::AioCompletion
* c
, int r
) {
604 librados::CB_AioCompleteAndSafe
cb(pc
);