mon_client.c 32 KB

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