mon_client.c 33 KB

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