mon_client.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/ceph/ceph_debug.h>
  3. #include <linux/module.h>
  4. #include <linux/types.h>
  5. #include <linux/slab.h>
  6. #include <linux/random.h>
  7. #include <linux/sched.h>
  8. #include <linux/ceph/ceph_features.h>
  9. #include <linux/ceph/mon_client.h>
  10. #include <linux/ceph/libceph.h>
  11. #include <linux/ceph/debugfs.h>
  12. #include <linux/ceph/decode.h>
  13. #include <linux/ceph/auth.h>
  14. /*
  15. * Interact with Ceph monitor cluster. Handle requests for new map
  16. * versions, and periodically resend as needed. Also implement
  17. * statfs() and umount().
  18. *
  19. * A small cluster of Ceph "monitors" are responsible for managing critical
  20. * cluster configuration and state information. An odd number (e.g., 3, 5)
  21. * of cmon daemons use a modified version of the Paxos part-time parliament
  22. * algorithm to manage the MDS map (mds cluster membership), OSD map, and
  23. * list of clients who have mounted the file system.
  24. *
  25. * We maintain an open, active session with a monitor at all times in order to
  26. * receive timely MDSMap updates. We periodically send a keepalive byte on the
  27. * TCP socket to ensure we detect a failure. If the connection does break, we
  28. * randomly hunt for a new monitor. Once the connection is reestablished, we
  29. * resend any outstanding requests.
  30. */
  31. static const struct ceph_connection_operations mon_con_ops;
  32. static int __validate_auth(struct ceph_mon_client *monc);
  33. /*
  34. * Decode a monmap blob (e.g., during mount).
  35. */
  36. struct ceph_monmap *ceph_monmap_decode(void *p, void *end)
  37. {
  38. struct ceph_monmap *m = NULL;
  39. int i, err = -EINVAL;
  40. struct ceph_fsid fsid;
  41. u32 epoch, num_mon;
  42. u32 len;
  43. ceph_decode_32_safe(&p, end, len, bad);
  44. ceph_decode_need(&p, end, len, bad);
  45. dout("monmap_decode %p %p len %d\n", p, end, (int)(end-p));
  46. p += sizeof(u16); /* skip version */
  47. ceph_decode_need(&p, end, sizeof(fsid) + 2*sizeof(u32), bad);
  48. ceph_decode_copy(&p, &fsid, sizeof(fsid));
  49. epoch = ceph_decode_32(&p);
  50. num_mon = ceph_decode_32(&p);
  51. ceph_decode_need(&p, end, num_mon*sizeof(m->mon_inst[0]), bad);
  52. if (num_mon > CEPH_MAX_MON)
  53. goto bad;
  54. m = kmalloc(struct_size(m, mon_inst, num_mon), GFP_NOFS);
  55. if (m == NULL)
  56. return ERR_PTR(-ENOMEM);
  57. m->fsid = fsid;
  58. m->epoch = epoch;
  59. m->num_mon = num_mon;
  60. ceph_decode_copy(&p, m->mon_inst, num_mon*sizeof(m->mon_inst[0]));
  61. for (i = 0; i < num_mon; i++)
  62. ceph_decode_addr(&m->mon_inst[i].addr);
  63. dout("monmap_decode epoch %d, num_mon %d\n", m->epoch,
  64. m->num_mon);
  65. for (i = 0; i < m->num_mon; i++)
  66. dout("monmap_decode mon%d is %s\n", i,
  67. ceph_pr_addr(&m->mon_inst[i].addr.in_addr));
  68. return m;
  69. bad:
  70. dout("monmap_decode failed with %d\n", err);
  71. kfree(m);
  72. return ERR_PTR(err);
  73. }
  74. /*
  75. * return true if *addr is included in the monmap.
  76. */
  77. int ceph_monmap_contains(struct ceph_monmap *m, struct ceph_entity_addr *addr)
  78. {
  79. int i;
  80. for (i = 0; i < m->num_mon; i++)
  81. if (memcmp(addr, &m->mon_inst[i].addr, sizeof(*addr)) == 0)
  82. return 1;
  83. return 0;
  84. }
  85. /*
  86. * Send an auth request.
  87. */
  88. static void __send_prepared_auth_request(struct ceph_mon_client *monc, int len)
  89. {
  90. monc->pending_auth = 1;
  91. monc->m_auth->front.iov_len = len;
  92. monc->m_auth->hdr.front_len = cpu_to_le32(len);
  93. ceph_msg_revoke(monc->m_auth);
  94. ceph_msg_get(monc->m_auth); /* keep our ref */
  95. ceph_con_send(&monc->con, monc->m_auth);
  96. }
  97. /*
  98. * Close monitor session, if any.
  99. */
  100. static void __close_session(struct ceph_mon_client *monc)
  101. {
  102. dout("__close_session closing mon%d\n", monc->cur_mon);
  103. ceph_msg_revoke(monc->m_auth);
  104. ceph_msg_revoke_incoming(monc->m_auth_reply);
  105. ceph_msg_revoke(monc->m_subscribe);
  106. ceph_msg_revoke_incoming(monc->m_subscribe_ack);
  107. ceph_con_close(&monc->con);
  108. monc->pending_auth = 0;
  109. ceph_auth_reset(monc->auth);
  110. }
  111. /*
  112. * Pick a new monitor at random and set cur_mon. If we are repicking
  113. * (i.e. cur_mon is already set), be sure to pick a different one.
  114. */
  115. static void pick_new_mon(struct ceph_mon_client *monc)
  116. {
  117. int old_mon = monc->cur_mon;
  118. BUG_ON(monc->monmap->num_mon < 1);
  119. if (monc->monmap->num_mon == 1) {
  120. monc->cur_mon = 0;
  121. } else {
  122. int max = monc->monmap->num_mon;
  123. int o = -1;
  124. int n;
  125. if (monc->cur_mon >= 0) {
  126. if (monc->cur_mon < monc->monmap->num_mon)
  127. o = monc->cur_mon;
  128. if (o >= 0)
  129. max--;
  130. }
  131. n = prandom_u32() % max;
  132. if (o >= 0 && n >= o)
  133. n++;
  134. monc->cur_mon = n;
  135. }
  136. dout("%s mon%d -> mon%d out of %d mons\n", __func__, old_mon,
  137. monc->cur_mon, monc->monmap->num_mon);
  138. }
  139. /*
  140. * Open a session with a new monitor.
  141. */
  142. static void __open_session(struct ceph_mon_client *monc)
  143. {
  144. int ret;
  145. pick_new_mon(monc);
  146. monc->hunting = true;
  147. if (monc->had_a_connection) {
  148. monc->hunt_mult *= CEPH_MONC_HUNT_BACKOFF;
  149. if (monc->hunt_mult > CEPH_MONC_HUNT_MAX_MULT)
  150. monc->hunt_mult = CEPH_MONC_HUNT_MAX_MULT;
  151. }
  152. monc->sub_renew_after = jiffies; /* i.e., expired */
  153. monc->sub_renew_sent = 0;
  154. dout("%s opening mon%d\n", __func__, monc->cur_mon);
  155. ceph_con_open(&monc->con, CEPH_ENTITY_TYPE_MON, monc->cur_mon,
  156. &monc->monmap->mon_inst[monc->cur_mon].addr);
  157. /*
  158. * send an initial keepalive to ensure our timestamp is valid
  159. * by the time we are in an OPENED state
  160. */
  161. ceph_con_keepalive(&monc->con);
  162. /* initiate authentication handshake */
  163. ret = ceph_auth_build_hello(monc->auth,
  164. monc->m_auth->front.iov_base,
  165. monc->m_auth->front_alloc_len);
  166. BUG_ON(ret <= 0);
  167. __send_prepared_auth_request(monc, ret);
  168. }
  169. static void reopen_session(struct ceph_mon_client *monc)
  170. {
  171. if (!monc->hunting)
  172. pr_info("mon%d %s session lost, hunting for new mon\n",
  173. monc->cur_mon, ceph_pr_addr(&monc->con.peer_addr.in_addr));
  174. __close_session(monc);
  175. __open_session(monc);
  176. }
  177. static void un_backoff(struct ceph_mon_client *monc)
  178. {
  179. monc->hunt_mult /= 2; /* reduce by 50% */
  180. if (monc->hunt_mult < 1)
  181. monc->hunt_mult = 1;
  182. dout("%s hunt_mult now %d\n", __func__, monc->hunt_mult);
  183. }
  184. /*
  185. * Reschedule delayed work timer.
  186. */
  187. static void __schedule_delayed(struct ceph_mon_client *monc)
  188. {
  189. unsigned long delay;
  190. if (monc->hunting)
  191. delay = CEPH_MONC_HUNT_INTERVAL * monc->hunt_mult;
  192. else
  193. delay = CEPH_MONC_PING_INTERVAL;
  194. dout("__schedule_delayed after %lu\n", delay);
  195. mod_delayed_work(system_wq, &monc->delayed_work,
  196. round_jiffies_relative(delay));
  197. }
  198. const char *ceph_sub_str[] = {
  199. [CEPH_SUB_MONMAP] = "monmap",
  200. [CEPH_SUB_OSDMAP] = "osdmap",
  201. [CEPH_SUB_FSMAP] = "fsmap.user",
  202. [CEPH_SUB_MDSMAP] = "mdsmap",
  203. };
  204. /*
  205. * Send subscribe request for one or more maps, according to
  206. * monc->subs.
  207. */
  208. static void __send_subscribe(struct ceph_mon_client *monc)
  209. {
  210. struct ceph_msg *msg = monc->m_subscribe;
  211. void *p = msg->front.iov_base;
  212. void *const end = p + msg->front_alloc_len;
  213. int num = 0;
  214. int i;
  215. dout("%s sent %lu\n", __func__, monc->sub_renew_sent);
  216. BUG_ON(monc->cur_mon < 0);
  217. if (!monc->sub_renew_sent)
  218. monc->sub_renew_sent = jiffies | 1; /* never 0 */
  219. msg->hdr.version = cpu_to_le16(2);
  220. for (i = 0; i < ARRAY_SIZE(monc->subs); i++) {
  221. if (monc->subs[i].want)
  222. num++;
  223. }
  224. BUG_ON(num < 1); /* monmap sub is always there */
  225. ceph_encode_32(&p, num);
  226. for (i = 0; i < ARRAY_SIZE(monc->subs); i++) {
  227. char buf[32];
  228. int len;
  229. if (!monc->subs[i].want)
  230. continue;
  231. len = sprintf(buf, "%s", ceph_sub_str[i]);
  232. if (i == CEPH_SUB_MDSMAP &&
  233. monc->fs_cluster_id != CEPH_FS_CLUSTER_ID_NONE)
  234. len += sprintf(buf + len, ".%d", monc->fs_cluster_id);
  235. dout("%s %s start %llu flags 0x%x\n", __func__, buf,
  236. le64_to_cpu(monc->subs[i].item.start),
  237. monc->subs[i].item.flags);
  238. ceph_encode_string(&p, end, buf, len);
  239. memcpy(p, &monc->subs[i].item, sizeof(monc->subs[i].item));
  240. p += sizeof(monc->subs[i].item);
  241. }
  242. BUG_ON(p > end);
  243. msg->front.iov_len = p - msg->front.iov_base;
  244. msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
  245. ceph_msg_revoke(msg);
  246. ceph_con_send(&monc->con, ceph_msg_get(msg));
  247. }
  248. static void handle_subscribe_ack(struct ceph_mon_client *monc,
  249. struct ceph_msg *msg)
  250. {
  251. unsigned int seconds;
  252. struct ceph_mon_subscribe_ack *h = msg->front.iov_base;
  253. if (msg->front.iov_len < sizeof(*h))
  254. goto bad;
  255. seconds = le32_to_cpu(h->duration);
  256. mutex_lock(&monc->mutex);
  257. if (monc->sub_renew_sent) {
  258. /*
  259. * This is only needed for legacy (infernalis or older)
  260. * MONs -- see delayed_work().
  261. */
  262. monc->sub_renew_after = monc->sub_renew_sent +
  263. (seconds >> 1) * HZ - 1;
  264. dout("%s sent %lu duration %d renew after %lu\n", __func__,
  265. monc->sub_renew_sent, seconds, monc->sub_renew_after);
  266. monc->sub_renew_sent = 0;
  267. } else {
  268. dout("%s sent %lu renew after %lu, ignoring\n", __func__,
  269. monc->sub_renew_sent, monc->sub_renew_after);
  270. }
  271. mutex_unlock(&monc->mutex);
  272. return;
  273. bad:
  274. pr_err("got corrupt subscribe-ack msg\n");
  275. ceph_msg_dump(msg);
  276. }
  277. /*
  278. * Register interest in a map
  279. *
  280. * @sub: one of CEPH_SUB_*
  281. * @epoch: X for "every map since X", or 0 for "just the latest"
  282. */
  283. static bool __ceph_monc_want_map(struct ceph_mon_client *monc, int sub,
  284. u32 epoch, bool continuous)
  285. {
  286. __le64 start = cpu_to_le64(epoch);
  287. u8 flags = !continuous ? CEPH_SUBSCRIBE_ONETIME : 0;
  288. dout("%s %s epoch %u continuous %d\n", __func__, ceph_sub_str[sub],
  289. epoch, continuous);
  290. if (monc->subs[sub].want &&
  291. monc->subs[sub].item.start == start &&
  292. monc->subs[sub].item.flags == flags)
  293. return false;
  294. monc->subs[sub].item.start = start;
  295. monc->subs[sub].item.flags = flags;
  296. monc->subs[sub].want = true;
  297. return true;
  298. }
  299. bool ceph_monc_want_map(struct ceph_mon_client *monc, int sub, u32 epoch,
  300. bool continuous)
  301. {
  302. bool need_request;
  303. mutex_lock(&monc->mutex);
  304. need_request = __ceph_monc_want_map(monc, sub, epoch, continuous);
  305. mutex_unlock(&monc->mutex);
  306. return need_request;
  307. }
  308. EXPORT_SYMBOL(ceph_monc_want_map);
  309. /*
  310. * Keep track of which maps we have
  311. *
  312. * @sub: one of CEPH_SUB_*
  313. */
  314. static void __ceph_monc_got_map(struct ceph_mon_client *monc, int sub,
  315. u32 epoch)
  316. {
  317. dout("%s %s epoch %u\n", __func__, ceph_sub_str[sub], epoch);
  318. if (monc->subs[sub].want) {
  319. if (monc->subs[sub].item.flags & CEPH_SUBSCRIBE_ONETIME)
  320. monc->subs[sub].want = false;
  321. else
  322. monc->subs[sub].item.start = cpu_to_le64(epoch + 1);
  323. }
  324. monc->subs[sub].have = epoch;
  325. }
  326. void ceph_monc_got_map(struct ceph_mon_client *monc, int sub, u32 epoch)
  327. {
  328. mutex_lock(&monc->mutex);
  329. __ceph_monc_got_map(monc, sub, epoch);
  330. mutex_unlock(&monc->mutex);
  331. }
  332. EXPORT_SYMBOL(ceph_monc_got_map);
  333. void ceph_monc_renew_subs(struct ceph_mon_client *monc)
  334. {
  335. mutex_lock(&monc->mutex);
  336. __send_subscribe(monc);
  337. mutex_unlock(&monc->mutex);
  338. }
  339. EXPORT_SYMBOL(ceph_monc_renew_subs);
  340. /*
  341. * Wait for an osdmap with a given epoch.
  342. *
  343. * @epoch: epoch to wait for
  344. * @timeout: in jiffies, 0 means "wait forever"
  345. */
  346. int ceph_monc_wait_osdmap(struct ceph_mon_client *monc, u32 epoch,
  347. unsigned long timeout)
  348. {
  349. unsigned long started = jiffies;
  350. long ret;
  351. mutex_lock(&monc->mutex);
  352. while (monc->subs[CEPH_SUB_OSDMAP].have < epoch) {
  353. mutex_unlock(&monc->mutex);
  354. if (timeout && time_after_eq(jiffies, started + timeout))
  355. return -ETIMEDOUT;
  356. ret = wait_event_interruptible_timeout(monc->client->auth_wq,
  357. monc->subs[CEPH_SUB_OSDMAP].have >= epoch,
  358. ceph_timeout_jiffies(timeout));
  359. if (ret < 0)
  360. return ret;
  361. mutex_lock(&monc->mutex);
  362. }
  363. mutex_unlock(&monc->mutex);
  364. return 0;
  365. }
  366. EXPORT_SYMBOL(ceph_monc_wait_osdmap);
  367. /*
  368. * Open a session with a random monitor. Request monmap and osdmap,
  369. * which are waited upon in __ceph_open_session().
  370. */
  371. int ceph_monc_open_session(struct ceph_mon_client *monc)
  372. {
  373. mutex_lock(&monc->mutex);
  374. __ceph_monc_want_map(monc, CEPH_SUB_MONMAP, 0, true);
  375. __ceph_monc_want_map(monc, CEPH_SUB_OSDMAP, 0, false);
  376. __open_session(monc);
  377. __schedule_delayed(monc);
  378. mutex_unlock(&monc->mutex);
  379. return 0;
  380. }
  381. EXPORT_SYMBOL(ceph_monc_open_session);
  382. static void ceph_monc_handle_map(struct ceph_mon_client *monc,
  383. struct ceph_msg *msg)
  384. {
  385. struct ceph_client *client = monc->client;
  386. struct ceph_monmap *monmap = NULL, *old = monc->monmap;
  387. void *p, *end;
  388. mutex_lock(&monc->mutex);
  389. dout("handle_monmap\n");
  390. p = msg->front.iov_base;
  391. end = p + msg->front.iov_len;
  392. monmap = ceph_monmap_decode(p, end);
  393. if (IS_ERR(monmap)) {
  394. pr_err("problem decoding monmap, %d\n",
  395. (int)PTR_ERR(monmap));
  396. goto out;
  397. }
  398. if (ceph_check_fsid(monc->client, &monmap->fsid) < 0) {
  399. kfree(monmap);
  400. goto out;
  401. }
  402. client->monc.monmap = monmap;
  403. kfree(old);
  404. __ceph_monc_got_map(monc, CEPH_SUB_MONMAP, monc->monmap->epoch);
  405. client->have_fsid = true;
  406. out:
  407. mutex_unlock(&monc->mutex);
  408. wake_up_all(&client->auth_wq);
  409. }
  410. /*
  411. * generic requests (currently statfs, mon_get_version)
  412. */
  413. DEFINE_RB_FUNCS(generic_request, struct ceph_mon_generic_request, tid, node)
  414. static void release_generic_request(struct kref *kref)
  415. {
  416. struct ceph_mon_generic_request *req =
  417. container_of(kref, struct ceph_mon_generic_request, kref);
  418. dout("%s greq %p request %p reply %p\n", __func__, req, req->request,
  419. req->reply);
  420. WARN_ON(!RB_EMPTY_NODE(&req->node));
  421. if (req->reply)
  422. ceph_msg_put(req->reply);
  423. if (req->request)
  424. ceph_msg_put(req->request);
  425. kfree(req);
  426. }
  427. static void put_generic_request(struct ceph_mon_generic_request *req)
  428. {
  429. if (req)
  430. kref_put(&req->kref, release_generic_request);
  431. }
  432. static void get_generic_request(struct ceph_mon_generic_request *req)
  433. {
  434. kref_get(&req->kref);
  435. }
  436. static struct ceph_mon_generic_request *
  437. alloc_generic_request(struct ceph_mon_client *monc, gfp_t gfp)
  438. {
  439. struct ceph_mon_generic_request *req;
  440. req = kzalloc(sizeof(*req), gfp);
  441. if (!req)
  442. return NULL;
  443. req->monc = monc;
  444. kref_init(&req->kref);
  445. RB_CLEAR_NODE(&req->node);
  446. init_completion(&req->completion);
  447. dout("%s greq %p\n", __func__, req);
  448. return req;
  449. }
  450. static void register_generic_request(struct ceph_mon_generic_request *req)
  451. {
  452. struct ceph_mon_client *monc = req->monc;
  453. WARN_ON(req->tid);
  454. get_generic_request(req);
  455. req->tid = ++monc->last_tid;
  456. insert_generic_request(&monc->generic_request_tree, req);
  457. }
  458. static void send_generic_request(struct ceph_mon_client *monc,
  459. struct ceph_mon_generic_request *req)
  460. {
  461. WARN_ON(!req->tid);
  462. dout("%s greq %p tid %llu\n", __func__, req, req->tid);
  463. req->request->hdr.tid = cpu_to_le64(req->tid);
  464. ceph_con_send(&monc->con, ceph_msg_get(req->request));
  465. }
  466. static void __finish_generic_request(struct ceph_mon_generic_request *req)
  467. {
  468. struct ceph_mon_client *monc = req->monc;
  469. dout("%s greq %p tid %llu\n", __func__, req, req->tid);
  470. erase_generic_request(&monc->generic_request_tree, req);
  471. ceph_msg_revoke(req->request);
  472. ceph_msg_revoke_incoming(req->reply);
  473. }
  474. static void finish_generic_request(struct ceph_mon_generic_request *req)
  475. {
  476. __finish_generic_request(req);
  477. put_generic_request(req);
  478. }
  479. static void complete_generic_request(struct ceph_mon_generic_request *req)
  480. {
  481. if (req->complete_cb)
  482. req->complete_cb(req);
  483. else
  484. complete_all(&req->completion);
  485. put_generic_request(req);
  486. }
  487. static void cancel_generic_request(struct ceph_mon_generic_request *req)
  488. {
  489. struct ceph_mon_client *monc = req->monc;
  490. struct ceph_mon_generic_request *lookup_req;
  491. dout("%s greq %p tid %llu\n", __func__, req, req->tid);
  492. mutex_lock(&monc->mutex);
  493. lookup_req = lookup_generic_request(&monc->generic_request_tree,
  494. req->tid);
  495. if (lookup_req) {
  496. WARN_ON(lookup_req != req);
  497. finish_generic_request(req);
  498. }
  499. mutex_unlock(&monc->mutex);
  500. }
  501. static int wait_generic_request(struct ceph_mon_generic_request *req)
  502. {
  503. int ret;
  504. dout("%s greq %p tid %llu\n", __func__, req, req->tid);
  505. ret = wait_for_completion_interruptible(&req->completion);
  506. if (ret)
  507. cancel_generic_request(req);
  508. else
  509. ret = req->result; /* completed */
  510. return ret;
  511. }
  512. static struct ceph_msg *get_generic_reply(struct ceph_connection *con,
  513. struct ceph_msg_header *hdr,
  514. int *skip)
  515. {
  516. struct ceph_mon_client *monc = con->private;
  517. struct ceph_mon_generic_request *req;
  518. u64 tid = le64_to_cpu(hdr->tid);
  519. struct ceph_msg *m;
  520. mutex_lock(&monc->mutex);
  521. req = lookup_generic_request(&monc->generic_request_tree, tid);
  522. if (!req) {
  523. dout("get_generic_reply %lld dne\n", tid);
  524. *skip = 1;
  525. m = NULL;
  526. } else {
  527. dout("get_generic_reply %lld got %p\n", tid, req->reply);
  528. *skip = 0;
  529. m = ceph_msg_get(req->reply);
  530. /*
  531. * we don't need to track the connection reading into
  532. * this reply because we only have one open connection
  533. * at a time, ever.
  534. */
  535. }
  536. mutex_unlock(&monc->mutex);
  537. return m;
  538. }
  539. /*
  540. * statfs
  541. */
  542. static void handle_statfs_reply(struct ceph_mon_client *monc,
  543. struct ceph_msg *msg)
  544. {
  545. struct ceph_mon_generic_request *req;
  546. struct ceph_mon_statfs_reply *reply = msg->front.iov_base;
  547. u64 tid = le64_to_cpu(msg->hdr.tid);
  548. dout("%s msg %p tid %llu\n", __func__, msg, tid);
  549. if (msg->front.iov_len != sizeof(*reply))
  550. goto bad;
  551. mutex_lock(&monc->mutex);
  552. req = lookup_generic_request(&monc->generic_request_tree, tid);
  553. if (!req) {
  554. mutex_unlock(&monc->mutex);
  555. return;
  556. }
  557. req->result = 0;
  558. *req->u.st = reply->st; /* struct */
  559. __finish_generic_request(req);
  560. mutex_unlock(&monc->mutex);
  561. complete_generic_request(req);
  562. return;
  563. bad:
  564. pr_err("corrupt statfs reply, tid %llu\n", tid);
  565. ceph_msg_dump(msg);
  566. }
  567. /*
  568. * Do a synchronous statfs().
  569. */
  570. int ceph_monc_do_statfs(struct ceph_mon_client *monc, u64 data_pool,
  571. struct ceph_statfs *buf)
  572. {
  573. struct ceph_mon_generic_request *req;
  574. struct ceph_mon_statfs *h;
  575. int ret = -ENOMEM;
  576. req = alloc_generic_request(monc, GFP_NOFS);
  577. if (!req)
  578. goto out;
  579. req->request = ceph_msg_new(CEPH_MSG_STATFS, sizeof(*h), GFP_NOFS,
  580. true);
  581. if (!req->request)
  582. goto out;
  583. req->reply = ceph_msg_new(CEPH_MSG_STATFS_REPLY, 64, GFP_NOFS, true);
  584. if (!req->reply)
  585. goto out;
  586. req->u.st = buf;
  587. req->request->hdr.version = cpu_to_le16(2);
  588. mutex_lock(&monc->mutex);
  589. register_generic_request(req);
  590. /* fill out request */
  591. h = req->request->front.iov_base;
  592. h->monhdr.have_version = 0;
  593. h->monhdr.session_mon = cpu_to_le16(-1);
  594. h->monhdr.session_mon_tid = 0;
  595. h->fsid = monc->monmap->fsid;
  596. h->contains_data_pool = (data_pool != CEPH_NOPOOL);
  597. h->data_pool = cpu_to_le64(data_pool);
  598. send_generic_request(monc, req);
  599. mutex_unlock(&monc->mutex);
  600. ret = wait_generic_request(req);
  601. out:
  602. put_generic_request(req);
  603. return ret;
  604. }
  605. EXPORT_SYMBOL(ceph_monc_do_statfs);
  606. static void handle_get_version_reply(struct ceph_mon_client *monc,
  607. struct ceph_msg *msg)
  608. {
  609. struct ceph_mon_generic_request *req;
  610. u64 tid = le64_to_cpu(msg->hdr.tid);
  611. void *p = msg->front.iov_base;
  612. void *end = p + msg->front_alloc_len;
  613. u64 handle;
  614. dout("%s msg %p tid %llu\n", __func__, msg, tid);
  615. ceph_decode_need(&p, end, 2*sizeof(u64), bad);
  616. handle = ceph_decode_64(&p);
  617. if (tid != 0 && tid != handle)
  618. goto bad;
  619. mutex_lock(&monc->mutex);
  620. req = lookup_generic_request(&monc->generic_request_tree, handle);
  621. if (!req) {
  622. mutex_unlock(&monc->mutex);
  623. return;
  624. }
  625. req->result = 0;
  626. req->u.newest = ceph_decode_64(&p);
  627. __finish_generic_request(req);
  628. mutex_unlock(&monc->mutex);
  629. complete_generic_request(req);
  630. return;
  631. bad:
  632. pr_err("corrupt mon_get_version reply, tid %llu\n", tid);
  633. ceph_msg_dump(msg);
  634. }
  635. static struct ceph_mon_generic_request *
  636. __ceph_monc_get_version(struct ceph_mon_client *monc, const char *what,
  637. ceph_monc_callback_t cb, u64 private_data)
  638. {
  639. struct ceph_mon_generic_request *req;
  640. req = alloc_generic_request(monc, GFP_NOIO);
  641. if (!req)
  642. goto err_put_req;
  643. req->request = ceph_msg_new(CEPH_MSG_MON_GET_VERSION,
  644. sizeof(u64) + sizeof(u32) + strlen(what),
  645. GFP_NOIO, true);
  646. if (!req->request)
  647. goto err_put_req;
  648. req->reply = ceph_msg_new(CEPH_MSG_MON_GET_VERSION_REPLY, 32, GFP_NOIO,
  649. true);
  650. if (!req->reply)
  651. goto err_put_req;
  652. req->complete_cb = cb;
  653. req->private_data = private_data;
  654. mutex_lock(&monc->mutex);
  655. register_generic_request(req);
  656. {
  657. void *p = req->request->front.iov_base;
  658. void *const end = p + req->request->front_alloc_len;
  659. ceph_encode_64(&p, req->tid); /* handle */
  660. ceph_encode_string(&p, end, what, strlen(what));
  661. WARN_ON(p != end);
  662. }
  663. send_generic_request(monc, req);
  664. mutex_unlock(&monc->mutex);
  665. return req;
  666. err_put_req:
  667. put_generic_request(req);
  668. return ERR_PTR(-ENOMEM);
  669. }
  670. /*
  671. * Send MMonGetVersion and wait for the reply.
  672. *
  673. * @what: one of "mdsmap", "osdmap" or "monmap"
  674. */
  675. int ceph_monc_get_version(struct ceph_mon_client *monc, const char *what,
  676. u64 *newest)
  677. {
  678. struct ceph_mon_generic_request *req;
  679. int ret;
  680. req = __ceph_monc_get_version(monc, what, NULL, 0);
  681. if (IS_ERR(req))
  682. return PTR_ERR(req);
  683. ret = wait_generic_request(req);
  684. if (!ret)
  685. *newest = req->u.newest;
  686. put_generic_request(req);
  687. return ret;
  688. }
  689. EXPORT_SYMBOL(ceph_monc_get_version);
  690. /*
  691. * Send MMonGetVersion,
  692. *
  693. * @what: one of "mdsmap", "osdmap" or "monmap"
  694. */
  695. int ceph_monc_get_version_async(struct ceph_mon_client *monc, const char *what,
  696. ceph_monc_callback_t cb, u64 private_data)
  697. {
  698. struct ceph_mon_generic_request *req;
  699. req = __ceph_monc_get_version(monc, what, cb, private_data);
  700. if (IS_ERR(req))
  701. return PTR_ERR(req);
  702. put_generic_request(req);
  703. return 0;
  704. }
  705. EXPORT_SYMBOL(ceph_monc_get_version_async);
  706. static void handle_command_ack(struct ceph_mon_client *monc,
  707. struct ceph_msg *msg)
  708. {
  709. struct ceph_mon_generic_request *req;
  710. void *p = msg->front.iov_base;
  711. void *const end = p + msg->front_alloc_len;
  712. u64 tid = le64_to_cpu(msg->hdr.tid);
  713. dout("%s msg %p tid %llu\n", __func__, msg, tid);
  714. ceph_decode_need(&p, end, sizeof(struct ceph_mon_request_header) +
  715. sizeof(u32), bad);
  716. p += sizeof(struct ceph_mon_request_header);
  717. mutex_lock(&monc->mutex);
  718. req = lookup_generic_request(&monc->generic_request_tree, tid);
  719. if (!req) {
  720. mutex_unlock(&monc->mutex);
  721. return;
  722. }
  723. req->result = ceph_decode_32(&p);
  724. __finish_generic_request(req);
  725. mutex_unlock(&monc->mutex);
  726. complete_generic_request(req);
  727. return;
  728. bad:
  729. pr_err("corrupt mon_command ack, tid %llu\n", tid);
  730. ceph_msg_dump(msg);
  731. }
  732. int ceph_monc_blacklist_add(struct ceph_mon_client *monc,
  733. struct ceph_entity_addr *client_addr)
  734. {
  735. struct ceph_mon_generic_request *req;
  736. struct ceph_mon_command *h;
  737. int ret = -ENOMEM;
  738. int len;
  739. req = alloc_generic_request(monc, GFP_NOIO);
  740. if (!req)
  741. goto out;
  742. req->request = ceph_msg_new(CEPH_MSG_MON_COMMAND, 256, GFP_NOIO, true);
  743. if (!req->request)
  744. goto out;
  745. req->reply = ceph_msg_new(CEPH_MSG_MON_COMMAND_ACK, 512, GFP_NOIO,
  746. true);
  747. if (!req->reply)
  748. goto out;
  749. mutex_lock(&monc->mutex);
  750. register_generic_request(req);
  751. h = req->request->front.iov_base;
  752. h->monhdr.have_version = 0;
  753. h->monhdr.session_mon = cpu_to_le16(-1);
  754. h->monhdr.session_mon_tid = 0;
  755. h->fsid = monc->monmap->fsid;
  756. h->num_strs = cpu_to_le32(1);
  757. len = sprintf(h->str, "{ \"prefix\": \"osd blacklist\", \
  758. \"blacklistop\": \"add\", \
  759. \"addr\": \"%pISpc/%u\" }",
  760. &client_addr->in_addr, le32_to_cpu(client_addr->nonce));
  761. h->str_len = cpu_to_le32(len);
  762. send_generic_request(monc, req);
  763. mutex_unlock(&monc->mutex);
  764. ret = wait_generic_request(req);
  765. if (!ret)
  766. /*
  767. * Make sure we have the osdmap that includes the blacklist
  768. * entry. This is needed to ensure that the OSDs pick up the
  769. * new blacklist before processing any future requests from
  770. * this client.
  771. */
  772. ret = ceph_wait_for_latest_osdmap(monc->client, 0);
  773. out:
  774. put_generic_request(req);
  775. return ret;
  776. }
  777. EXPORT_SYMBOL(ceph_monc_blacklist_add);
  778. /*
  779. * Resend pending generic requests.
  780. */
  781. static void __resend_generic_request(struct ceph_mon_client *monc)
  782. {
  783. struct ceph_mon_generic_request *req;
  784. struct rb_node *p;
  785. for (p = rb_first(&monc->generic_request_tree); p; p = rb_next(p)) {
  786. req = rb_entry(p, struct ceph_mon_generic_request, node);
  787. ceph_msg_revoke(req->request);
  788. ceph_msg_revoke_incoming(req->reply);
  789. ceph_con_send(&monc->con, ceph_msg_get(req->request));
  790. }
  791. }
  792. /*
  793. * Delayed work. If we haven't mounted yet, retry. Otherwise,
  794. * renew/retry subscription as needed (in case it is timing out, or we
  795. * got an ENOMEM). And keep the monitor connection alive.
  796. */
  797. static void delayed_work(struct work_struct *work)
  798. {
  799. struct ceph_mon_client *monc =
  800. container_of(work, struct ceph_mon_client, delayed_work.work);
  801. dout("monc delayed_work\n");
  802. mutex_lock(&monc->mutex);
  803. if (monc->hunting) {
  804. dout("%s continuing hunt\n", __func__);
  805. reopen_session(monc);
  806. } else {
  807. int is_auth = ceph_auth_is_authenticated(monc->auth);
  808. if (ceph_con_keepalive_expired(&monc->con,
  809. CEPH_MONC_PING_TIMEOUT)) {
  810. dout("monc keepalive timeout\n");
  811. is_auth = 0;
  812. reopen_session(monc);
  813. }
  814. if (!monc->hunting) {
  815. ceph_con_keepalive(&monc->con);
  816. __validate_auth(monc);
  817. un_backoff(monc);
  818. }
  819. if (is_auth &&
  820. !(monc->con.peer_features & CEPH_FEATURE_MON_STATEFUL_SUB)) {
  821. unsigned long now = jiffies;
  822. dout("%s renew subs? now %lu renew after %lu\n",
  823. __func__, now, monc->sub_renew_after);
  824. if (time_after_eq(now, monc->sub_renew_after))
  825. __send_subscribe(monc);
  826. }
  827. }
  828. __schedule_delayed(monc);
  829. mutex_unlock(&monc->mutex);
  830. }
  831. /*
  832. * On startup, we build a temporary monmap populated with the IPs
  833. * provided by mount(2).
  834. */
  835. static int build_initial_monmap(struct ceph_mon_client *monc)
  836. {
  837. struct ceph_options *opt = monc->client->options;
  838. struct ceph_entity_addr *mon_addr = opt->mon_addr;
  839. int num_mon = opt->num_mon;
  840. int i;
  841. /* build initial monmap */
  842. monc->monmap = kzalloc(struct_size(monc->monmap, mon_inst, num_mon),
  843. GFP_KERNEL);
  844. if (!monc->monmap)
  845. return -ENOMEM;
  846. for (i = 0; i < num_mon; i++) {
  847. monc->monmap->mon_inst[i].addr = mon_addr[i];
  848. monc->monmap->mon_inst[i].addr.nonce = 0;
  849. monc->monmap->mon_inst[i].name.type =
  850. CEPH_ENTITY_TYPE_MON;
  851. monc->monmap->mon_inst[i].name.num = cpu_to_le64(i);
  852. }
  853. monc->monmap->num_mon = num_mon;
  854. return 0;
  855. }
  856. int ceph_monc_init(struct ceph_mon_client *monc, struct ceph_client *cl)
  857. {
  858. int err = 0;
  859. dout("init\n");
  860. memset(monc, 0, sizeof(*monc));
  861. monc->client = cl;
  862. monc->monmap = NULL;
  863. mutex_init(&monc->mutex);
  864. err = build_initial_monmap(monc);
  865. if (err)
  866. goto out;
  867. /* connection */
  868. /* authentication */
  869. monc->auth = ceph_auth_init(cl->options->name,
  870. cl->options->key);
  871. if (IS_ERR(monc->auth)) {
  872. err = PTR_ERR(monc->auth);
  873. goto out_monmap;
  874. }
  875. monc->auth->want_keys =
  876. CEPH_ENTITY_TYPE_AUTH | CEPH_ENTITY_TYPE_MON |
  877. CEPH_ENTITY_TYPE_OSD | CEPH_ENTITY_TYPE_MDS;
  878. /* msgs */
  879. err = -ENOMEM;
  880. monc->m_subscribe_ack = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE_ACK,
  881. sizeof(struct ceph_mon_subscribe_ack),
  882. GFP_KERNEL, true);
  883. if (!monc->m_subscribe_ack)
  884. goto out_auth;
  885. monc->m_subscribe = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE, 128,
  886. GFP_KERNEL, true);
  887. if (!monc->m_subscribe)
  888. goto out_subscribe_ack;
  889. monc->m_auth_reply = ceph_msg_new(CEPH_MSG_AUTH_REPLY, 4096,
  890. GFP_KERNEL, true);
  891. if (!monc->m_auth_reply)
  892. goto out_subscribe;
  893. monc->m_auth = ceph_msg_new(CEPH_MSG_AUTH, 4096, GFP_KERNEL, true);
  894. monc->pending_auth = 0;
  895. if (!monc->m_auth)
  896. goto out_auth_reply;
  897. ceph_con_init(&monc->con, monc, &mon_con_ops,
  898. &monc->client->msgr);
  899. monc->cur_mon = -1;
  900. monc->had_a_connection = false;
  901. monc->hunt_mult = 1;
  902. INIT_DELAYED_WORK(&monc->delayed_work, delayed_work);
  903. monc->generic_request_tree = RB_ROOT;
  904. monc->last_tid = 0;
  905. monc->fs_cluster_id = CEPH_FS_CLUSTER_ID_NONE;
  906. return 0;
  907. out_auth_reply:
  908. ceph_msg_put(monc->m_auth_reply);
  909. out_subscribe:
  910. ceph_msg_put(monc->m_subscribe);
  911. out_subscribe_ack:
  912. ceph_msg_put(monc->m_subscribe_ack);
  913. out_auth:
  914. ceph_auth_destroy(monc->auth);
  915. out_monmap:
  916. kfree(monc->monmap);
  917. out:
  918. return err;
  919. }
  920. EXPORT_SYMBOL(ceph_monc_init);
  921. void ceph_monc_stop(struct ceph_mon_client *monc)
  922. {
  923. dout("stop\n");
  924. cancel_delayed_work_sync(&monc->delayed_work);
  925. mutex_lock(&monc->mutex);
  926. __close_session(monc);
  927. monc->cur_mon = -1;
  928. mutex_unlock(&monc->mutex);
  929. /*
  930. * flush msgr queue before we destroy ourselves to ensure that:
  931. * - any work that references our embedded con is finished.
  932. * - any osd_client or other work that may reference an authorizer
  933. * finishes before we shut down the auth subsystem.
  934. */
  935. ceph_msgr_flush();
  936. ceph_auth_destroy(monc->auth);
  937. WARN_ON(!RB_EMPTY_ROOT(&monc->generic_request_tree));
  938. ceph_msg_put(monc->m_auth);
  939. ceph_msg_put(monc->m_auth_reply);
  940. ceph_msg_put(monc->m_subscribe);
  941. ceph_msg_put(monc->m_subscribe_ack);
  942. kfree(monc->monmap);
  943. }
  944. EXPORT_SYMBOL(ceph_monc_stop);
  945. static void finish_hunting(struct ceph_mon_client *monc)
  946. {
  947. if (monc->hunting) {
  948. dout("%s found mon%d\n", __func__, monc->cur_mon);
  949. monc->hunting = false;
  950. monc->had_a_connection = true;
  951. un_backoff(monc);
  952. __schedule_delayed(monc);
  953. }
  954. }
  955. static void handle_auth_reply(struct ceph_mon_client *monc,
  956. struct ceph_msg *msg)
  957. {
  958. int ret;
  959. int was_auth = 0;
  960. mutex_lock(&monc->mutex);
  961. was_auth = ceph_auth_is_authenticated(monc->auth);
  962. monc->pending_auth = 0;
  963. ret = ceph_handle_auth_reply(monc->auth, msg->front.iov_base,
  964. msg->front.iov_len,
  965. monc->m_auth->front.iov_base,
  966. monc->m_auth->front_alloc_len);
  967. if (ret > 0) {
  968. __send_prepared_auth_request(monc, ret);
  969. goto out;
  970. }
  971. finish_hunting(monc);
  972. if (ret < 0) {
  973. monc->client->auth_err = ret;
  974. } else if (!was_auth && ceph_auth_is_authenticated(monc->auth)) {
  975. dout("authenticated, starting session\n");
  976. monc->client->msgr.inst.name.type = CEPH_ENTITY_TYPE_CLIENT;
  977. monc->client->msgr.inst.name.num =
  978. cpu_to_le64(monc->auth->global_id);
  979. __send_subscribe(monc);
  980. __resend_generic_request(monc);
  981. pr_info("mon%d %s session established\n", monc->cur_mon,
  982. ceph_pr_addr(&monc->con.peer_addr.in_addr));
  983. }
  984. out:
  985. mutex_unlock(&monc->mutex);
  986. if (monc->client->auth_err < 0)
  987. wake_up_all(&monc->client->auth_wq);
  988. }
  989. static int __validate_auth(struct ceph_mon_client *monc)
  990. {
  991. int ret;
  992. if (monc->pending_auth)
  993. return 0;
  994. ret = ceph_build_auth(monc->auth, monc->m_auth->front.iov_base,
  995. monc->m_auth->front_alloc_len);
  996. if (ret <= 0)
  997. return ret; /* either an error, or no need to authenticate */
  998. __send_prepared_auth_request(monc, ret);
  999. return 0;
  1000. }
  1001. int ceph_monc_validate_auth(struct ceph_mon_client *monc)
  1002. {
  1003. int ret;
  1004. mutex_lock(&monc->mutex);
  1005. ret = __validate_auth(monc);
  1006. mutex_unlock(&monc->mutex);
  1007. return ret;
  1008. }
  1009. EXPORT_SYMBOL(ceph_monc_validate_auth);
  1010. /*
  1011. * handle incoming message
  1012. */
  1013. static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
  1014. {
  1015. struct ceph_mon_client *monc = con->private;
  1016. int type = le16_to_cpu(msg->hdr.type);
  1017. if (!monc)
  1018. return;
  1019. switch (type) {
  1020. case CEPH_MSG_AUTH_REPLY:
  1021. handle_auth_reply(monc, msg);
  1022. break;
  1023. case CEPH_MSG_MON_SUBSCRIBE_ACK:
  1024. handle_subscribe_ack(monc, msg);
  1025. break;
  1026. case CEPH_MSG_STATFS_REPLY:
  1027. handle_statfs_reply(monc, msg);
  1028. break;
  1029. case CEPH_MSG_MON_GET_VERSION_REPLY:
  1030. handle_get_version_reply(monc, msg);
  1031. break;
  1032. case CEPH_MSG_MON_COMMAND_ACK:
  1033. handle_command_ack(monc, msg);
  1034. break;
  1035. case CEPH_MSG_MON_MAP:
  1036. ceph_monc_handle_map(monc, msg);
  1037. break;
  1038. case CEPH_MSG_OSD_MAP:
  1039. ceph_osdc_handle_map(&monc->client->osdc, msg);
  1040. break;
  1041. default:
  1042. /* can the chained handler handle it? */
  1043. if (monc->client->extra_mon_dispatch &&
  1044. monc->client->extra_mon_dispatch(monc->client, msg) == 0)
  1045. break;
  1046. pr_err("received unknown message type %d %s\n", type,
  1047. ceph_msg_type_name(type));
  1048. }
  1049. ceph_msg_put(msg);
  1050. }
  1051. /*
  1052. * Allocate memory for incoming message
  1053. */
  1054. static struct ceph_msg *mon_alloc_msg(struct ceph_connection *con,
  1055. struct ceph_msg_header *hdr,
  1056. int *skip)
  1057. {
  1058. struct ceph_mon_client *monc = con->private;
  1059. int type = le16_to_cpu(hdr->type);
  1060. int front_len = le32_to_cpu(hdr->front_len);
  1061. struct ceph_msg *m = NULL;
  1062. *skip = 0;
  1063. switch (type) {
  1064. case CEPH_MSG_MON_SUBSCRIBE_ACK:
  1065. m = ceph_msg_get(monc->m_subscribe_ack);
  1066. break;
  1067. case CEPH_MSG_STATFS_REPLY:
  1068. case CEPH_MSG_MON_COMMAND_ACK:
  1069. return get_generic_reply(con, hdr, skip);
  1070. case CEPH_MSG_AUTH_REPLY:
  1071. m = ceph_msg_get(monc->m_auth_reply);
  1072. break;
  1073. case CEPH_MSG_MON_GET_VERSION_REPLY:
  1074. if (le64_to_cpu(hdr->tid) != 0)
  1075. return get_generic_reply(con, hdr, skip);
  1076. /*
  1077. * Older OSDs don't set reply tid even if the orignal
  1078. * request had a non-zero tid. Work around this weirdness
  1079. * by allocating a new message.
  1080. */
  1081. /* fall through */
  1082. case CEPH_MSG_MON_MAP:
  1083. case CEPH_MSG_MDS_MAP:
  1084. case CEPH_MSG_OSD_MAP:
  1085. case CEPH_MSG_FS_MAP_USER:
  1086. m = ceph_msg_new(type, front_len, GFP_NOFS, false);
  1087. if (!m)
  1088. return NULL; /* ENOMEM--return skip == 0 */
  1089. break;
  1090. }
  1091. if (!m) {
  1092. pr_info("alloc_msg unknown type %d\n", type);
  1093. *skip = 1;
  1094. } else if (front_len > m->front_alloc_len) {
  1095. pr_warn("mon_alloc_msg front %d > prealloc %d (%u#%llu)\n",
  1096. front_len, m->front_alloc_len,
  1097. (unsigned int)con->peer_name.type,
  1098. le64_to_cpu(con->peer_name.num));
  1099. ceph_msg_put(m);
  1100. m = ceph_msg_new(type, front_len, GFP_NOFS, false);
  1101. }
  1102. return m;
  1103. }
  1104. /*
  1105. * If the monitor connection resets, pick a new monitor and resubmit
  1106. * any pending requests.
  1107. */
  1108. static void mon_fault(struct ceph_connection *con)
  1109. {
  1110. struct ceph_mon_client *monc = con->private;
  1111. mutex_lock(&monc->mutex);
  1112. dout("%s mon%d\n", __func__, monc->cur_mon);
  1113. if (monc->cur_mon >= 0) {
  1114. if (!monc->hunting) {
  1115. dout("%s hunting for new mon\n", __func__);
  1116. reopen_session(monc);
  1117. __schedule_delayed(monc);
  1118. } else {
  1119. dout("%s already hunting\n", __func__);
  1120. }
  1121. }
  1122. mutex_unlock(&monc->mutex);
  1123. }
  1124. /*
  1125. * We can ignore refcounting on the connection struct, as all references
  1126. * will come from the messenger workqueue, which is drained prior to
  1127. * mon_client destruction.
  1128. */
  1129. static struct ceph_connection *con_get(struct ceph_connection *con)
  1130. {
  1131. return con;
  1132. }
  1133. static void con_put(struct ceph_connection *con)
  1134. {
  1135. }
  1136. static const struct ceph_connection_operations mon_con_ops = {
  1137. .get = con_get,
  1138. .put = con_put,
  1139. .dispatch = dispatch,
  1140. .fault = mon_fault,
  1141. .alloc_msg = mon_alloc_msg,
  1142. };