1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
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"
64 #define dout_subsys ceph_subsys_rgw
66 bool global_stop
= false;
68 static void handle_sigterm(int signum
)
70 dout(20) << __func__
<< " SIGUSR1 ignored" << dendl
;
77 static std::mutex librgw_mtx
;
81 class C_InitTimeout
: public Context
{
84 void finish(int r
) override
{
85 derr
<< "Initialization timeout, failed to initialize" << dendl
;
90 void RGWLibProcess::checkpoint()
95 #define MIN_EXPIRE_S 120
97 void RGWLibProcess::run()
99 /* write completion interval */
100 RGWLibFS::write_completion_interval_s
=
101 cct
->_conf
->rgw_nfs_write_completion_interval_s
;
103 /* start write timer */
104 RGWLibFS::write_timer
.resume();
108 lsubdout(cct
, rgw
, 5) << "RGWLibProcess GC" << dendl
;
110 /* dirent invalidate timeout--basically, the upper-bound on
111 * inconsistency with the S3 namespace */
112 auto expire_s
= cct
->_conf
->rgw_nfs_namespace_expire_secs
;
114 /* delay between gc cycles */
115 auto delay_s
= std::max(int64_t(1), std::min(int64_t(MIN_EXPIRE_S
), expire_s
/2));
117 unique_lock
uniq(mtx
);
120 for (auto iter
= mounted_fs
.begin(); iter
!= mounted_fs
.end();
122 RGWLibFS
* fs
= iter
->first
->ref();
129 goto restart
; /* invalidated */
132 std::this_thread::sleep_for(std::chrono::seconds(delay_s
));
136 void RGWLibProcess::handle_request(RGWRequest
* r
)
139 * invariant: valid requests are derived from RGWLibRequst
141 RGWLibRequest
* req
= static_cast<RGWLibRequest
*>(r
);
143 // XXX move RGWLibIO and timing setup into process_request
146 utime_t tm
= ceph_clock_now();
151 int ret
= process_request(req
, &io_ctx
);
153 /* we don't really care about return code */
154 dout(20) << "process_request() returned " << ret
<< dendl
;
158 } /* handle_request */
160 int RGWLibProcess::process_request(RGWLibRequest
* req
)
162 // XXX move RGWLibIO and timing setup into process_request
165 utime_t tm
= ceph_clock_now();
170 int ret
= process_request(req
, &io_ctx
);
172 /* we don't really care about return code */
173 dout(20) << "process_request() returned " << ret
<< dendl
;
176 } /* process_request */
178 static inline void abort_req(struct req_state
*s
, RGWOp
*op
, int err_no
)
183 /* XXX the dump_errno and dump_bucket_from_state behaviors in
184 * the abort_early (rgw_rest.cc) might be valuable, but aren't
185 * safe to call presently as they return HTTP data */
187 perfcounter
->inc(l_rgw_failed_req
);
190 int RGWLibProcess::process_request(RGWLibRequest
* req
, RGWLibIO
* io
)
193 bool should_log
= true; // XXX
195 dout(1) << "====== " << __func__
196 << " starting new request req=" << hex
<< req
<< dec
197 << " ======" << dendl
;
200 * invariant: valid requests are derived from RGWOp--well-formed
201 * requests should have assigned RGWRequest::op in their descendant
202 * constructor--if not, the compiler can find it, at the cost of
205 RGWOp
*op
= (req
->op
) ? req
->op
: dynamic_cast<RGWOp
*>(req
);
207 dout(1) << "failed to derive cognate RGWOp (invalid op?)" << dendl
;
213 perfcounter
->inc(l_rgw_req
);
215 RGWEnv
& rgw_env
= io
->get_env();
218 * until major refactoring of req_state and req_info, we need
219 * to build their RGWEnv boilerplate from the RGWLibRequest,
220 * pre-staging any strings (HTTP_HOST) that provoke a crash when
224 /* XXX for now, use ""; could be a legit hostname, or, in future,
225 * perhaps a tenant (Yehuda) */
226 rgw_env
.set("HTTP_HOST", "");
228 /* XXX and -then- bloat up req_state with string copies from it */
229 struct req_state
rstate(req
->cct
, &rgw_env
, req
->get_user(), req
->id
);
230 struct req_state
*s
= &rstate
;
235 RGWObjectCtx
rados_ctx(store
, s
); // XXX holds std::map
237 auto sysobj_ctx
= store
->svc
.sysobj
->init_obj_ctx();
238 s
->sysobj_ctx
= &sysobj_ctx
;
240 /* XXX and -then- stash req_state pointers everywhere they are needed */
241 ret
= req
->init(rgw_env
, &rados_ctx
, io
, s
);
243 dout(10) << "failed to initialize request" << dendl
;
244 abort_req(s
, op
, ret
);
248 /* req is-a RGWOp, currently initialized separately */
249 ret
= req
->op_init();
251 dout(10) << "failed to initialize RGWOp" << dendl
;
252 abort_req(s
, op
, ret
);
256 /* now expected by rgw_log_op() */
257 rgw_env
.set("REQUEST_METHOD", s
->info
.method
);
258 rgw_env
.set("REQUEST_URI", s
->info
.request_uri
);
259 rgw_env
.set("QUERY_STRING", "");
262 /* XXX authorize does less here then in the REST path, e.g.,
263 * the user's info is cached, but still incomplete */
264 ldpp_dout(s
, 2) << "authorizing" << dendl
;
265 ret
= req
->authorize(op
);
267 dout(10) << "failed to authorize request" << dendl
;
268 abort_req(s
, op
, ret
);
272 /* FIXME: remove this after switching all handlers to the new
273 * authentication infrastructure. */
274 if (! s
->auth
.identity
) {
275 s
->auth
.identity
= rgw::auth::transform_old_authinfo(s
);
278 ldpp_dout(s
, 2) << "reading op permissions" << dendl
;
279 ret
= req
->read_permissions(op
);
281 abort_req(s
, op
, ret
);
285 ldpp_dout(s
, 2) << "init op" << dendl
;
286 ret
= op
->init_processing();
288 abort_req(s
, op
, ret
);
292 ldpp_dout(s
, 2) << "verifying op mask" << dendl
;
293 ret
= op
->verify_op_mask();
295 abort_req(s
, op
, ret
);
299 ldpp_dout(s
, 2) << "verifying op permissions" << dendl
;
300 ret
= op
->verify_permission();
302 if (s
->system_request
) {
303 dout(2) << "overriding permissions due to system operation" << dendl
;
304 } else if (s
->auth
.identity
->is_admin_of(s
->user
->user_id
)) {
305 dout(2) << "overriding permissions due to admin operation" << dendl
;
307 abort_req(s
, op
, ret
);
312 ldpp_dout(s
, 2) << "verifying op params" << dendl
;
313 ret
= op
->verify_params();
315 abort_req(s
, op
, ret
);
319 ldpp_dout(s
, 2) << "executing" << dendl
;
324 } catch (const ceph::crypto::DigestException
& e
) {
325 dout(0) << "authentication failed" << e
.what() << dendl
;
326 abort_req(s
, op
, -ERR_INVALID_SECRET_KEY
);
331 io
->complete_request();
332 } catch (rgw::io::Exception
& e
) {
333 dout(0) << "ERROR: io->complete_request() returned "
334 << e
.what() << dendl
;
337 rgw_log_op(store
, nullptr /* !rest */, s
,
338 (op
? op
->name() : "unknown"), olog
);
341 int http_ret
= s
->err
.http_ret
;
343 ldpp_dout(s
, 2) << "http status=" << http_ret
<< dendl
;
345 dout(1) << "====== " << __func__
346 << " req done req=" << hex
<< req
<< dec
<< " http_status="
348 << " ======" << dendl
;
350 return (ret
< 0 ? ret
: s
->err
.ret
);
351 } /* process_request */
353 int RGWLibProcess::start_request(RGWLibContinuedReq
* req
)
356 dout(1) << "====== " << __func__
357 << " starting new continued request req=" << hex
<< req
<< dec
358 << " ======" << dendl
;
361 * invariant: valid requests are derived from RGWOp--well-formed
362 * requests should have assigned RGWRequest::op in their descendant
363 * constructor--if not, the compiler can find it, at the cost of
366 RGWOp
*op
= (req
->op
) ? req
->op
: dynamic_cast<RGWOp
*>(req
);
368 dout(1) << "failed to derive cognate RGWOp (invalid op?)" << dendl
;
372 struct req_state
* s
= req
->get_state();
374 /* req is-a RGWOp, currently initialized separately */
375 int ret
= req
->op_init();
377 dout(10) << "failed to initialize RGWOp" << dendl
;
378 abort_req(s
, op
, ret
);
382 /* XXX authorize does less here then in the REST path, e.g.,
383 * the user's info is cached, but still incomplete */
384 ldpp_dout(s
, 2) << "authorizing" << dendl
;
385 ret
= req
->authorize(op
);
387 dout(10) << "failed to authorize request" << dendl
;
388 abort_req(s
, op
, ret
);
392 /* FIXME: remove this after switching all handlers to the new authentication
394 if (! s
->auth
.identity
) {
395 s
->auth
.identity
= rgw::auth::transform_old_authinfo(s
);
398 ldpp_dout(s
, 2) << "reading op permissions" << dendl
;
399 ret
= req
->read_permissions(op
);
401 abort_req(s
, op
, ret
);
405 ldpp_dout(s
, 2) << "init op" << dendl
;
406 ret
= op
->init_processing();
408 abort_req(s
, op
, ret
);
412 ldpp_dout(s
, 2) << "verifying op mask" << dendl
;
413 ret
= op
->verify_op_mask();
415 abort_req(s
, op
, ret
);
419 ldpp_dout(s
, 2) << "verifying op permissions" << dendl
;
420 ret
= op
->verify_permission();
422 if (s
->system_request
) {
423 dout(2) << "overriding permissions due to system operation" << dendl
;
424 } else if (s
->auth
.identity
->is_admin_of(s
->user
->user_id
)) {
425 dout(2) << "overriding permissions due to admin operation" << dendl
;
427 abort_req(s
, op
, ret
);
432 ldpp_dout(s
, 2) << "verifying op params" << dendl
;
433 ret
= op
->verify_params();
435 abort_req(s
, op
, ret
);
443 return (ret
< 0 ? ret
: s
->err
.ret
);
446 int RGWLibProcess::finish_request(RGWLibContinuedReq
* req
)
448 RGWOp
*op
= (req
->op
) ? req
->op
: dynamic_cast<RGWOp
*>(req
);
450 dout(1) << "failed to derive cognate RGWOp (invalid op?)" << dendl
;
454 int ret
= req
->exec_finish();
455 int op_ret
= op
->get_ret();
457 dout(1) << "====== " << __func__
458 << " finishing continued request req=" << hex
<< req
<< dec
459 << " op status=" << op_ret
460 << " ======" << dendl
;
465 int RGWLibFrontend::init()
467 pprocess
= new RGWLibProcess(g_ceph_context
, &env
,
468 g_conf()->rgw_thread_pool_size
, conf
);
474 vector
<const char*> args
;
478 int RGWLib::init(vector
<const char*>& args
)
482 /* alternative default for module */
483 map
<string
,string
> defaults
= {
484 { "debug_rgw", "1/5" },
485 { "keyring", "$rgw_data/keyring" },
486 { "log_file", "/var/log/radosgw/$cluster-$name.log" }
489 cct
= global_init(&defaults
, args
,
490 CEPH_ENTITY_TYPE_CLIENT
,
491 CODE_ENVIRONMENT_DAEMON
,
492 CINIT_FLAG_UNPRIVILEGED_DAEMON_DEFAULTS
);
495 SafeTimer
init_timer(g_ceph_context
, mutex
);
498 init_timer
.add_event_after(g_conf()->rgw_init_timeout
, new C_InitTimeout
);
501 common_init_finish(g_ceph_context
);
503 rgw_tools_init(g_ceph_context
);
506 rgw::curl::setup_curl(boost::none
);
507 rgw_http_client_init(g_ceph_context
);
509 store
= RGWStoreManager::get_storage(g_ceph_context
,
510 g_conf()->rgw_enable_gc_threads
,
511 g_conf()->rgw_enable_lc_threads
,
512 g_conf()->rgw_enable_quota_threads
,
513 g_conf()->rgw_run_sync_thread
,
514 g_conf().get_val
<bool>("rgw_dynamic_resharding"));
518 init_timer
.cancel_all_events();
519 init_timer
.shutdown();
522 derr
<< "Couldn't init storage provider (RADOS)" << dendl
;
526 r
= rgw_perf_start(g_ceph_context
);
528 rgw_rest_init(g_ceph_context
, store
, store
->svc
.zone
->get_zonegroup());
531 init_timer
.cancel_all_events();
532 init_timer
.shutdown();
538 const string
& ldap_uri
= store
->ctx()->_conf
->rgw_ldap_uri
;
539 const string
& ldap_binddn
= store
->ctx()->_conf
->rgw_ldap_binddn
;
540 const string
& ldap_searchdn
= store
->ctx()->_conf
->rgw_ldap_searchdn
;
541 const string
& ldap_searchfilter
= store
->ctx()->_conf
->rgw_ldap_searchfilter
;
542 const string
& ldap_dnattr
=
543 store
->ctx()->_conf
->rgw_ldap_dnattr
;
544 std::string ldap_bindpw
= parse_rgw_ldap_bindpw(store
->ctx());
546 ldh
= new rgw::LDAPHelper(ldap_uri
, ldap_binddn
, ldap_bindpw
.c_str(),
547 ldap_searchdn
, ldap_searchfilter
, ldap_dnattr
);
551 rgw_user_init(store
);
552 rgw_bucket_init(store
->meta_mgr
);
553 rgw_log_usage_init(g_ceph_context
, store
);
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
->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(RGWRados
*store
, const rgw_user
& uid
)
630 int ret
= rgw_get_user_info_by_uid(store
, uid
, user_info
, NULL
);
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
->user_id
);
681 s
->owner
.set_name(s
->user
->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
);