mon_client.c 32 KB

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