mon_client.c 33 KB

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