]> git.proxmox.com Git - ceph.git/blame - ceph/src/ceph_mds.cc
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / ceph_mds.cc
CommitLineData
7c673cae
FG
1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2// vim: ts=8 sw=2 smarttab
3/*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14
15#include <sys/types.h>
16#include <sys/stat.h>
17#include <fcntl.h>
18#include <pthread.h>
19
20#include <iostream>
21#include <string>
7c673cae
FG
22
23#include "include/ceph_features.h"
24#include "include/compat.h"
11fdf7f2 25#include "include/random.h"
7c673cae
FG
26
27#include "common/config.h"
28#include "common/strtol.h"
29
30#include "mon/MonMap.h"
31#include "mds/MDSDaemon.h"
32
33#include "msg/Messenger.h"
34
35#include "common/Timer.h"
36#include "common/ceph_argparse.h"
37#include "common/pick_address.h"
11fdf7f2 38#include "common/Preforker.h"
7c673cae
FG
39
40#include "global/global_init.h"
41#include "global/signal_handler.h"
42#include "global/pidfile.h"
43
44#include "mon/MonClient.h"
45
46#include "auth/KeyRing.h"
47
48#include "perfglue/heap_profiler.h"
49
11fdf7f2 50#include "include/ceph_assert.h"
7c673cae
FG
51
52#define dout_context g_ceph_context
53#define dout_subsys ceph_subsys_mds
54
55static void usage()
56{
11fdf7f2 57 cout << "usage: ceph-mds -i <ID> [flags]\n"
7c673cae
FG
58 << " -m monitorip:port\n"
59 << " connect to monitor at given address\n"
60 << " --debug_mds n\n"
61 << " debug MDS level (e.g. 10)\n"
7c673cae
FG
62 << std::endl;
63 generic_server_usage();
64}
65
66
7c673cae
FG
67MDSDaemon *mds = NULL;
68
69
70static void handle_mds_signal(int signum)
71{
72 if (mds)
73 mds->handle_signal(signum);
74}
75
7c673cae 76int main(int argc, const char **argv)
7c673cae
FG
77{
78 ceph_pthread_setname(pthread_self(), "ceph-mds");
79
80 vector<const char*> args;
81 argv_to_vec(argc, argv, args);
11fdf7f2
TL
82 if (args.empty()) {
83 cerr << argv[0] << ": -h or --help for usage" << std::endl;
84 exit(1);
85 }
86 if (ceph_argparse_need_usage(args)) {
87 usage();
88 exit(0);
89 }
7c673cae
FG
90
91 auto cct = global_init(NULL, args,
92 CEPH_ENTITY_TYPE_MDS, CODE_ENVIRONMENT_DAEMON,
93 0, "mds_data");
94 ceph_heap_profiler_init();
95
96 std::string val, action;
97 for (std::vector<const char*>::iterator i = args.begin(); i != args.end(); ) {
98 if (ceph_argparse_double_dash(args, i)) {
99 break;
100 }
7c673cae 101 else if (ceph_argparse_witharg(args, i, &val, "--hot-standby", (char*)NULL)) {
11fdf7f2 102 dout(0) << "--hot-standby is obsolete and has no effect" << dendl;
7c673cae
FG
103 }
104 else {
105 derr << "Error: can't understand argument: " << *i << "\n" << dendl;
11fdf7f2 106 exit(1);
7c673cae
FG
107 }
108 }
109
11fdf7f2
TL
110 Preforker forker;
111
112 entity_addrvec_t addrs;
113 pick_addresses(g_ceph_context, CEPH_PICK_ADDRESS_PUBLIC, &addrs);
7c673cae
FG
114
115 // Normal startup
11fdf7f2 116 if (g_conf()->name.has_default_id()) {
7c673cae 117 derr << "must specify '-i name' with the ceph-mds instance name" << dendl;
11fdf7f2 118 exit(1);
7c673cae
FG
119 }
120
11fdf7f2
TL
121 if (g_conf()->name.get_id().empty() ||
122 (g_conf()->name.get_id()[0] >= '0' && g_conf()->name.get_id()[0] <= '9')) {
123 derr << "MDS id '" << g_conf()->name << "' is invalid. "
7c673cae 124 "MDS names may not start with a numeric digit." << dendl;
11fdf7f2
TL
125 exit(1);
126 }
127
128 if (global_init_prefork(g_ceph_context) >= 0) {
129 std::string err;
130 int r = forker.prefork(err);
131 if (r < 0) {
132 cerr << err << std::endl;
133 return r;
134 }
135 if (forker.is_parent()) {
136 if (forker.parent_wait(err) != 0) {
137 return -ENXIO;
138 }
139 return 0;
140 }
141 global_init_postfork_start(g_ceph_context);
7c673cae 142 }
11fdf7f2
TL
143 common_init_finish(g_ceph_context);
144 global_init_chdir(g_ceph_context);
7c673cae 145
11fdf7f2 146 auto nonce = ceph::util::generate_random_number<uint64_t>();
7c673cae 147
11fdf7f2 148 std::string public_msgr_type = g_conf()->ms_public_type.empty() ? g_conf().get_val<std::string>("ms_type") : g_conf()->ms_public_type;
7c673cae
FG
149 Messenger *msgr = Messenger::create(g_ceph_context, public_msgr_type,
150 entity_name_t::MDS(-1), "mds",
151 nonce, Messenger::HAS_MANY_CONNECTIONS);
152 if (!msgr)
11fdf7f2 153 forker.exit(1);
7c673cae
FG
154 msgr->set_cluster_protocol(CEPH_MDS_PROTOCOL);
155
11fdf7f2 156 cout << "starting " << g_conf()->name << " at " << msgr->get_myaddrs()
7c673cae
FG
157 << std::endl;
158 uint64_t required =
159 CEPH_FEATURE_OSDREPLYMUX;
160
161 msgr->set_default_policy(Messenger::Policy::lossy_client(required));
162 msgr->set_policy(entity_name_t::TYPE_MON,
163 Messenger::Policy::lossy_client(CEPH_FEATURE_UID |
164 CEPH_FEATURE_PGID64));
165 msgr->set_policy(entity_name_t::TYPE_MDS,
166 Messenger::Policy::lossless_peer(CEPH_FEATURE_UID));
167 msgr->set_policy(entity_name_t::TYPE_CLIENT,
168 Messenger::Policy::stateful_server(0));
169
11fdf7f2 170 int r = msgr->bindv(addrs);
7c673cae 171 if (r < 0)
11fdf7f2 172 forker.exit(1);
7c673cae 173
11fdf7f2
TL
174 // set up signal handlers, now that we've daemonized/forked.
175 init_async_signal_handler();
176 register_async_signal_handler(SIGHUP, sighup_handler);
177
7c673cae
FG
178 // get monmap
179 MonClient mc(g_ceph_context);
180 if (mc.build_initial_monmap() < 0)
11fdf7f2 181 forker.exit(1);
7c673cae
FG
182 global_init_chdir(g_ceph_context);
183
184 msgr->start();
185
186 // start mds
11fdf7f2 187 mds = new MDSDaemon(g_conf()->name.get_id().c_str(), msgr, &mc);
7c673cae
FG
188
189 // in case we have to respawn...
190 mds->orig_argc = argc;
191 mds->orig_argv = argv;
192
11fdf7f2
TL
193 if (g_conf()->daemonize) {
194 global_init_postfork_finish(g_ceph_context);
195 forker.daemonize();
196 }
197
7c673cae
FG
198 r = mds->init();
199 if (r < 0) {
200 msgr->wait();
201 goto shutdown;
202 }
203
7c673cae
FG
204 register_async_signal_handler_oneshot(SIGINT, handle_mds_signal);
205 register_async_signal_handler_oneshot(SIGTERM, handle_mds_signal);
206
11fdf7f2 207 if (g_conf()->inject_early_sigterm)
7c673cae
FG
208 kill(getpid(), SIGTERM);
209
210 msgr->wait();
211
212 unregister_async_signal_handler(SIGHUP, sighup_handler);
213 unregister_async_signal_handler(SIGINT, handle_mds_signal);
214 unregister_async_signal_handler(SIGTERM, handle_mds_signal);
215 shutdown_async_signal_handler();
216
217 shutdown:
218 // yuck: grab the mds lock, so we can be sure that whoever in *mds
219 // called shutdown finishes what they were doing.
220 mds->mds_lock.Lock();
221 mds->mds_lock.Unlock();
222
223 pidfile_remove();
224
225 // only delete if it was a clean shutdown (to aid memory leak
226 // detection, etc.). don't bother if it was a suicide.
227 if (mds->is_clean_shutdown()) {
228 delete mds;
229 delete msgr;
230 }
231
232 // cd on exit, so that gmon.out (if any) goes into a separate directory for each node.
233 char s[20];
234 snprintf(s, sizeof(s), "gmon/%d", getpid());
235 if ((mkdir(s, 0755) == 0) && (chdir(s) == 0)) {
236 cerr << "ceph-mds: gmon.out should be in " << s << std::endl;
237 }
238
239 return 0;
240}
241