1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab ft=cpp
5 * Ceph - scalable distributed file system
7 * Copyright (C) 2011 New Dream Network
9 * This is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License version 2.1, as published by the Free Software
12 * Foundation. See file COPYING.
16 #include "include/compat.h"
17 #include <sys/types.h>
21 #include "include/types.h"
22 #include "include/rados/librgw.h"
23 #include "rgw/rgw_acl_s3.h"
26 #include "include/str_list.h"
27 #include "include/stringify.h"
28 #include "global/global_init.h"
29 #include "global/signal_handler.h"
30 #include "common/config.h"
31 #include "common/errno.h"
32 #include "common/Timer.h"
33 #include "common/Throttle.h"
34 #include "common/WorkQueue.h"
35 #include "common/ceph_argparse.h"
36 #include "common/ceph_context.h"
37 #include "common/common_init.h"
38 #include "common/dout.h"
40 #include "rgw_rados.h"
41 #include "rgw_resolve.h"
44 #include "rgw_frontend.h"
45 #include "rgw_request.h"
46 #include "rgw_process.h"
47 #include "rgw_rest_user.h"
48 #include "rgw_rest_s3.h"
49 #include "rgw_os_lib.h"
51 #include "rgw_auth_s3.h"
53 #include "rgw_lib_frontend.h"
54 #include "rgw_http_client.h"
55 #include "rgw_http_client_curl.h"
56 #include "rgw_perf_counters.h"
58 #include "services/svc_zone.h"
66 #define dout_subsys ceph_subsys_rgw
68 bool global_stop
= false;
70 static void handle_sigterm(int signum
)
72 dout(20) << __func__
<< " SIGUSR1 ignored" << dendl
;
79 static std::mutex librgw_mtx
;
83 class C_InitTimeout
: public Context
{
86 void finish(int r
) override
{
87 derr
<< "Initialization timeout, failed to initialize" << dendl
;
92 void RGWLibProcess::checkpoint()
97 #define MIN_EXPIRE_S 120
99 void RGWLibProcess::run()
101 /* write completion interval */
102 RGWLibFS::write_completion_interval_s
=
103 cct
->_conf
->rgw_nfs_write_completion_interval_s
;
105 /* start write timer */
106 RGWLibFS::write_timer
.resume();
110 lsubdout(cct
, rgw
, 5) << "RGWLibProcess GC" << dendl
;
112 /* dirent invalidate timeout--basically, the upper-bound on
113 * inconsistency with the S3 namespace */
114 auto expire_s
= cct
->_conf
->rgw_nfs_namespace_expire_secs
;
116 /* delay between gc cycles */
117 auto delay_s
= std::max(int64_t(1), std::min(int64_t(MIN_EXPIRE_S
), expire_s
/2));
119 unique_lock
uniq(mtx
);
122 for (auto iter
= mounted_fs
.begin(); iter
!= mounted_fs
.end();
124 RGWLibFS
* fs
= iter
->first
->ref();
131 goto restart
; /* invalidated */
134 std::this_thread::sleep_for(std::chrono::seconds(delay_s
));
138 void RGWLibProcess::handle_request(RGWRequest
* r
)
141 * invariant: valid requests are derived from RGWLibRequst
143 RGWLibRequest
* req
= static_cast<RGWLibRequest
*>(r
);
145 // XXX move RGWLibIO and timing setup into process_request
148 utime_t tm
= ceph_clock_now();
153 int ret
= process_request(req
, &io_ctx
);
155 /* we don't really care about return code */
156 dout(20) << "process_request() returned " << ret
<< dendl
;
160 } /* handle_request */
162 int RGWLibProcess::process_request(RGWLibRequest
* req
)
164 // XXX move RGWLibIO and timing setup into process_request
167 utime_t tm
= ceph_clock_now();
172 int ret
= process_request(req
, &io_ctx
);
174 /* we don't really care about return code */
175 dout(20) << "process_request() returned " << ret
<< dendl
;
178 } /* process_request */
180 static inline void abort_req(struct req_state
*s
, RGWOp
*op
, int err_no
)
185 /* XXX the dump_errno and dump_bucket_from_state behaviors in
186 * the abort_early (rgw_rest.cc) might be valuable, but aren't
187 * safe to call presently as they return HTTP data */
189 perfcounter
->inc(l_rgw_failed_req
);
192 int RGWLibProcess::process_request(RGWLibRequest
* req
, RGWLibIO
* io
)
195 bool should_log
= true; // XXX
197 dout(1) << "====== " << __func__
198 << " starting new request req=" << hex
<< req
<< dec
199 << " ======" << dendl
;
202 * invariant: valid requests are derived from RGWOp--well-formed
203 * requests should have assigned RGWRequest::op in their descendant
204 * constructor--if not, the compiler can find it, at the cost of
207 RGWOp
*op
= (req
->op
) ? req
->op
: dynamic_cast<RGWOp
*>(req
);
209 dout(1) << "failed to derive cognate RGWOp (invalid op?)" << dendl
;
215 perfcounter
->inc(l_rgw_req
);
217 RGWEnv
& rgw_env
= io
->get_env();
220 * until major refactoring of req_state and req_info, we need
221 * to build their RGWEnv boilerplate from the RGWLibRequest,
222 * pre-staging any strings (HTTP_HOST) that provoke a crash when
226 /* XXX for now, use ""; could be a legit hostname, or, in future,
227 * perhaps a tenant (Yehuda) */
228 rgw_env
.set("HTTP_HOST", "");
230 /* XXX and -then- bloat up req_state with string copies from it */
231 struct req_state
rstate(req
->cct
, &rgw_env
, req
->get_user(), req
->id
);
232 struct req_state
*s
= &rstate
;
237 RGWObjectCtx
rados_ctx(store
, s
); // XXX holds std::map
239 auto sysobj_ctx
= store
->svc()->sysobj
->init_obj_ctx();
240 s
->sysobj_ctx
= &sysobj_ctx
;
242 /* XXX and -then- stash req_state pointers everywhere they are needed */
243 ret
= req
->init(rgw_env
, &rados_ctx
, io
, s
);
245 dout(10) << "failed to initialize request" << dendl
;
246 abort_req(s
, op
, ret
);
250 /* req is-a RGWOp, currently initialized separately */
251 ret
= req
->op_init();
253 dout(10) << "failed to initialize RGWOp" << dendl
;
254 abort_req(s
, op
, ret
);
258 /* now expected by rgw_log_op() */
259 rgw_env
.set("REQUEST_METHOD", s
->info
.method
);
260 rgw_env
.set("REQUEST_URI", s
->info
.request_uri
);
261 rgw_env
.set("QUERY_STRING", "");
264 /* XXX authorize does less here then in the REST path, e.g.,
265 * the user's info is cached, but still incomplete */
266 ldpp_dout(s
, 2) << "authorizing" << dendl
;
267 ret
= req
->authorize(op
);
269 dout(10) << "failed to authorize request" << dendl
;
270 abort_req(s
, op
, ret
);
274 /* FIXME: remove this after switching all handlers to the new
275 * authentication infrastructure. */
276 if (! s
->auth
.identity
) {
277 s
->auth
.identity
= rgw::auth::transform_old_authinfo(s
);
280 ldpp_dout(s
, 2) << "reading op permissions" << dendl
;
281 ret
= req
->read_permissions(op
);
283 abort_req(s
, op
, ret
);
287 ldpp_dout(s
, 2) << "init op" << dendl
;
288 ret
= op
->init_processing();
290 abort_req(s
, op
, ret
);
294 ldpp_dout(s
, 2) << "verifying op mask" << dendl
;
295 ret
= op
->verify_op_mask();
297 abort_req(s
, op
, ret
);
301 ldpp_dout(s
, 2) << "verifying op permissions" << dendl
;
302 ret
= op
->verify_permission();
304 if (s
->system_request
) {
305 dout(2) << "overriding permissions due to system operation" << dendl
;
306 } else if (s
->auth
.identity
->is_admin_of(s
->user
->get_id())) {
307 dout(2) << "overriding permissions due to admin operation" << dendl
;
309 abort_req(s
, op
, ret
);
314 ldpp_dout(s
, 2) << "verifying op params" << dendl
;
315 ret
= op
->verify_params();
317 abort_req(s
, op
, ret
);
321 ldpp_dout(s
, 2) << "executing" << dendl
;
326 } catch (const ceph::crypto::DigestException
& e
) {
327 dout(0) << "authentication failed" << e
.what() << dendl
;
328 abort_req(s
, op
, -ERR_INVALID_SECRET_KEY
);
333 io
->complete_request();
334 } catch (rgw::io::Exception
& e
) {
335 dout(0) << "ERROR: io->complete_request() returned "
336 << e
.what() << dendl
;
339 rgw_log_op(store
->getRados(), nullptr /* !rest */, s
,
340 (op
? op
->name() : "unknown"), olog
);
343 int http_ret
= s
->err
.http_ret
;
345 ldpp_dout(s
, 2) << "http status=" << http_ret
<< dendl
;
347 dout(1) << "====== " << __func__
348 << " req done req=" << hex
<< req
<< dec
<< " http_status="
350 << " ======" << dendl
;
352 return (ret
< 0 ? ret
: s
->err
.ret
);
353 } /* process_request */
355 int RGWLibProcess::start_request(RGWLibContinuedReq
* req
)
358 dout(1) << "====== " << __func__
359 << " starting new continued request req=" << hex
<< req
<< dec
360 << " ======" << dendl
;
363 * invariant: valid requests are derived from RGWOp--well-formed
364 * requests should have assigned RGWRequest::op in their descendant
365 * constructor--if not, the compiler can find it, at the cost of
368 RGWOp
*op
= (req
->op
) ? req
->op
: dynamic_cast<RGWOp
*>(req
);
370 dout(1) << "failed to derive cognate RGWOp (invalid op?)" << dendl
;
374 struct req_state
* s
= req
->get_state();
376 /* req is-a RGWOp, currently initialized separately */
377 int ret
= req
->op_init();
379 dout(10) << "failed to initialize RGWOp" << dendl
;
380 abort_req(s
, op
, ret
);
384 /* XXX authorize does less here then in the REST path, e.g.,
385 * the user's info is cached, but still incomplete */
386 ldpp_dout(s
, 2) << "authorizing" << dendl
;
387 ret
= req
->authorize(op
);
389 dout(10) << "failed to authorize request" << dendl
;
390 abort_req(s
, op
, ret
);
394 /* FIXME: remove this after switching all handlers to the new authentication
396 if (! s
->auth
.identity
) {
397 s
->auth
.identity
= rgw::auth::transform_old_authinfo(s
);
400 ldpp_dout(s
, 2) << "reading op permissions" << dendl
;
401 ret
= req
->read_permissions(op
);
403 abort_req(s
, op
, ret
);
407 ldpp_dout(s
, 2) << "init op" << dendl
;
408 ret
= op
->init_processing();
410 abort_req(s
, op
, ret
);
414 ldpp_dout(s
, 2) << "verifying op mask" << dendl
;
415 ret
= op
->verify_op_mask();
417 abort_req(s
, op
, ret
);
421 ldpp_dout(s
, 2) << "verifying op permissions" << dendl
;
422 ret
= op
->verify_permission();
424 if (s
->system_request
) {
425 dout(2) << "overriding permissions due to system operation" << dendl
;
426 } else if (s
->auth
.identity
->is_admin_of(s
->user
->get_id())) {
427 dout(2) << "overriding permissions due to admin operation" << dendl
;
429 abort_req(s
, op
, ret
);
434 ldpp_dout(s
, 2) << "verifying op params" << dendl
;
435 ret
= op
->verify_params();
437 abort_req(s
, op
, ret
);
445 return (ret
< 0 ? ret
: s
->err
.ret
);
448 int RGWLibProcess::finish_request(RGWLibContinuedReq
* req
)
450 RGWOp
*op
= (req
->op
) ? req
->op
: dynamic_cast<RGWOp
*>(req
);
452 dout(1) << "failed to derive cognate RGWOp (invalid op?)" << dendl
;
456 int ret
= req
->exec_finish();
457 int op_ret
= op
->get_ret();
459 dout(1) << "====== " << __func__
460 << " finishing continued request req=" << hex
<< req
<< dec
461 << " op status=" << op_ret
462 << " ======" << dendl
;
467 int RGWLibFrontend::init()
469 pprocess
= new RGWLibProcess(g_ceph_context
, &env
,
470 g_conf()->rgw_thread_pool_size
, conf
);
476 vector
<const char*> args
;
480 int RGWLib::init(vector
<const char*>& args
)
484 /* alternative default for module */
485 map
<string
,string
> defaults
= {
486 { "debug_rgw", "1/5" },
487 { "keyring", "$rgw_data/keyring" },
488 { "log_file", "/var/log/radosgw/$cluster-$name.log" }
491 cct
= global_init(&defaults
, args
,
492 CEPH_ENTITY_TYPE_CLIENT
,
493 CODE_ENVIRONMENT_DAEMON
,
494 CINIT_FLAG_UNPRIVILEGED_DAEMON_DEFAULTS
);
496 ceph::mutex mutex
= ceph::make_mutex("main");
497 SafeTimer
init_timer(g_ceph_context
, mutex
);
500 init_timer
.add_event_after(g_conf()->rgw_init_timeout
, new C_InitTimeout
);
503 common_init_finish(g_ceph_context
);
505 rgw_tools_init(g_ceph_context
);
508 rgw::curl::setup_curl(boost::none
);
509 rgw_http_client_init(g_ceph_context
);
511 store
= RGWStoreManager::get_storage(g_ceph_context
,
512 g_conf()->rgw_enable_gc_threads
,
513 g_conf()->rgw_enable_lc_threads
,
514 g_conf()->rgw_enable_quota_threads
,
515 g_conf()->rgw_run_sync_thread
,
516 g_conf().get_val
<bool>("rgw_dynamic_resharding"));
520 init_timer
.cancel_all_events();
521 init_timer
.shutdown();
524 derr
<< "Couldn't init storage provider (RADOS)" << dendl
;
528 r
= rgw_perf_start(g_ceph_context
);
530 rgw_rest_init(g_ceph_context
, store
->svc()->zone
->get_zonegroup());
533 init_timer
.cancel_all_events();
534 init_timer
.shutdown();
540 const string
& ldap_uri
= store
->ctx()->_conf
->rgw_ldap_uri
;
541 const string
& ldap_binddn
= store
->ctx()->_conf
->rgw_ldap_binddn
;
542 const string
& ldap_searchdn
= store
->ctx()->_conf
->rgw_ldap_searchdn
;
543 const string
& ldap_searchfilter
= store
->ctx()->_conf
->rgw_ldap_searchfilter
;
544 const string
& ldap_dnattr
=
545 store
->ctx()->_conf
->rgw_ldap_dnattr
;
546 std::string ldap_bindpw
= parse_rgw_ldap_bindpw(store
->ctx());
548 ldh
= new rgw::LDAPHelper(ldap_uri
, ldap_binddn
, ldap_bindpw
.c_str(),
549 ldap_searchdn
, ldap_searchfilter
, ldap_dnattr
);
553 rgw_log_usage_init(g_ceph_context
, store
->getRados());
555 // XXX ex-RGWRESTMgr_lib, mgr->set_logging(true)
557 if (!g_conf()->rgw_ops_log_socket_path
.empty()) {
558 olog
= new OpsLogSocket(g_ceph_context
, g_conf()->rgw_ops_log_data_backlog
);
559 olog
->init(g_conf()->rgw_ops_log_socket_path
);
563 RGWProcessEnv env
= { store
, &rest
, olog
, port
};
565 string fe_count
{"0"};
566 fec
= new RGWFrontendConfig("rgwlib");
567 fe
= new RGWLibFrontend(env
, fec
);
569 init_async_signal_handler();
570 register_async_signal_handler(SIGUSR1
, handle_sigterm
);
572 map
<string
, string
> service_map_meta
;
573 service_map_meta
["pid"] = stringify(getpid());
574 service_map_meta
["frontend_type#" + fe_count
] = "rgw-nfs";
575 service_map_meta
["frontend_config#" + fe_count
] = fec
->get_config();
579 derr
<< "ERROR: failed initializing frontend" << dendl
;
585 r
= store
->getRados()->register_to_service_map("rgw-nfs", service_map_meta
);
587 derr
<< "ERROR: failed to register to service map: " << cpp_strerror(-r
) << dendl
;
592 } /* RGWLib::init() */
596 derr
<< "shutting down" << dendl
;
606 unregister_async_signal_handler(SIGUSR1
, handle_sigterm
);
607 shutdown_async_signal_handler();
609 rgw_log_usage_finalize();
613 RGWStoreManager::close_storage(store
);
616 rgw_shutdown_resolver();
617 rgw_http_client_cleanup();
618 rgw::curl::cleanup_curl();
620 rgw_perf_stop(g_ceph_context
);
622 dout(1) << "final shutdown" << dendl
;
626 } /* RGWLib::stop() */
628 int RGWLibIO::set_uid(rgw::sal::RGWRadosStore
*store
, const rgw_user
& uid
)
630 int ret
= store
->ctl()->user
->get_info_by_uid(uid
, &user_info
, null_yield
);
632 derr
<< "ERROR: failed reading user info: uid=" << uid
<< " ret="
638 int RGWLibRequest::read_permissions(RGWOp
* op
) {
639 /* bucket and object ops */
641 rgw_build_bucket_policies(rgwlib
.get_store(), get_state());
643 ldout(get_state()->cct
, 10) << "read_permissions (bucket policy) on "
644 << get_state()->bucket
<< ":"
645 << get_state()->object
646 << " only_bucket=" << only_bucket()
647 << " ret=" << ret
<< dendl
;
650 } else if (! only_bucket()) {
652 ret
= rgw_build_object_policies(rgwlib
.get_store(), get_state(),
653 op
->prefetch_data());
655 ldout(get_state()->cct
, 10) << "read_permissions (object policy) on"
656 << get_state()->bucket
<< ":"
657 << get_state()->object
658 << " ret=" << ret
<< dendl
;
664 } /* RGWLibRequest::read_permissions */
666 int RGWHandler_Lib::authorize(const DoutPrefixProvider
*dpp
)
670 * 2. anonymous access
674 * Much or all of this depends on handling the cached authorization
675 * correctly (e.g., dealing with keystone) at mount time.
677 s
->perm_mask
= RGW_PERM_FULL_CONTROL
;
679 // populate the owner info
680 s
->owner
.set_id(s
->user
->get_id());
681 s
->owner
.set_name(s
->user
->get_display_name());
684 } /* RGWHandler_Lib::authorize */
686 } /* namespace rgw */
690 int librgw_create(librgw_t
* rgw
, int argc
, char **argv
)
696 if (! g_ceph_context
) {
697 std::lock_guard
<std::mutex
> lg(librgw_mtx
);
698 if (! g_ceph_context
) {
699 vector
<const char*> args
;
700 std::vector
<std::string
> spl_args
;
701 // last non-0 argument will be split and consumed
703 const std::string spl_arg
{argv
[(--argc
)]};
704 get_str_vec(spl_arg
, " \t", spl_args
);
706 argv_to_vec(argc
, const_cast<const char**>(argv
), args
);
707 // append split args, if any
708 for (const auto& elt
: spl_args
) {
709 args
.push_back(elt
.c_str());
711 rc
= rgwlib
.init(args
);
715 *rgw
= g_ceph_context
->get();
720 void librgw_shutdown(librgw_t rgw
)
724 CephContext
* cct
= static_cast<CephContext
*>(rgw
);