]> git.proxmox.com Git - ceph.git/blame - ceph/src/tools/cephfs/MDSUtility.cc
update sources to v12.2.5
[ceph.git] / ceph / src / tools / cephfs / MDSUtility.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) 2014 John Spray <john.spray@inktank.com>
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#include "MDSUtility.h"
15#include "mon/MonClient.h"
16
17#define dout_context g_ceph_context
18#define dout_subsys ceph_subsys_mds
19
20
21MDSUtility::MDSUtility() :
22 Dispatcher(g_ceph_context),
23 objecter(NULL),
24 lock("MDSUtility::lock"),
25 finisher(g_ceph_context, "MDSUtility", "fn_mds_utility"),
94b18763
FG
26 waiting_for_mds_map(NULL),
27 inited(false)
7c673cae
FG
28{
29 monc = new MonClient(g_ceph_context);
30 messenger = Messenger::create_client_messenger(g_ceph_context, "mds");
31 fsmap = new FSMap();
32 objecter = new Objecter(g_ceph_context, messenger, monc, NULL, 0, 0);
33}
34
35
36MDSUtility::~MDSUtility()
37{
94b18763
FG
38 if (inited) {
39 shutdown();
40 }
7c673cae
FG
41 delete objecter;
42 delete monc;
43 delete messenger;
44 delete fsmap;
45 assert(waiting_for_mds_map == NULL);
46}
47
48
49int MDSUtility::init()
50{
51 // Initialize Messenger
52 int r = messenger->bind(g_conf->public_addr);
53 if (r < 0)
54 return r;
55
56 messenger->start();
57
58 objecter->set_client_incarnation(0);
59 objecter->init();
60
61 // Connect dispatchers before starting objecter
62 messenger->add_dispatcher_tail(objecter);
63 messenger->add_dispatcher_tail(this);
64
65 // Initialize MonClient
66 if (monc->build_initial_monmap() < 0) {
67 objecter->shutdown();
68 messenger->shutdown();
69 messenger->wait();
70 return -1;
71 }
72
73 monc->set_want_keys(CEPH_ENTITY_TYPE_MON|CEPH_ENTITY_TYPE_OSD|CEPH_ENTITY_TYPE_MDS);
74 monc->set_messenger(messenger);
75 monc->init();
76 r = monc->authenticate();
77 if (r < 0) {
78 derr << "Authentication failed, did you specify an MDS ID with a valid keyring?" << dendl;
79 monc->shutdown();
80 objecter->shutdown();
81 messenger->shutdown();
82 messenger->wait();
83 return r;
84 }
85
86 client_t whoami = monc->get_global_id();
87 messenger->set_myname(entity_name_t::CLIENT(whoami.v));
88
89 // Start Objecter and wait for OSD map
90 objecter->start();
91 objecter->wait_for_osd_map();
92
93 // Prepare to receive MDS map and request it
94 Mutex init_lock("MDSUtility:init");
95 Cond cond;
96 bool done = false;
97 assert(!fsmap->get_epoch());
98 lock.Lock();
99 waiting_for_mds_map = new C_SafeCond(&init_lock, &cond, &done, NULL);
100 lock.Unlock();
101 monc->sub_want("fsmap", 0, CEPH_SUBSCRIBE_ONETIME);
102 monc->renew_subs();
103
104 // Wait for MDS map
105 dout(4) << "waiting for MDS map..." << dendl;
106 init_lock.Lock();
107 while (!done)
108 cond.Wait(init_lock);
109 init_lock.Unlock();
110 dout(4) << "Got MDS map " << fsmap->get_epoch() << dendl;
111
112 finisher.start();
113
94b18763 114 inited = true;
7c673cae
FG
115 return 0;
116}
117
118
119void MDSUtility::shutdown()
120{
121 finisher.stop();
122
123 lock.Lock();
124 objecter->shutdown();
125 lock.Unlock();
126 monc->shutdown();
127 messenger->shutdown();
128 messenger->wait();
129}
130
131
132bool MDSUtility::ms_dispatch(Message *m)
133{
134 Mutex::Locker locker(lock);
135 switch (m->get_type()) {
136 case CEPH_MSG_FS_MAP:
137 handle_fs_map((MFSMap*)m);
138 break;
139 case CEPH_MSG_OSD_MAP:
140 break;
141 default:
142 return false;
143 }
144 m->put();
145 return true;
146}
147
148
149void MDSUtility::handle_fs_map(MFSMap* m)
150{
151 *fsmap = m->get_fsmap();
152 if (waiting_for_mds_map) {
153 waiting_for_mds_map->complete(0);
154 waiting_for_mds_map = NULL;
155 }
156}
157
158
159bool MDSUtility::ms_get_authorizer(int dest_type, AuthAuthorizer **authorizer,
160 bool force_new)
161{
162 if (dest_type == CEPH_ENTITY_TYPE_MON)
163 return true;
164
165 if (force_new) {
166 if (monc->wait_auth_rotating(10) < 0)
167 return false;
168 }
169
170 *authorizer = monc->build_authorizer(dest_type);
171 return *authorizer != NULL;
172}