mon_client.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. #include "ceph_debug.h"
  2. #include <linux/types.h>
  3. #include <linux/random.h>
  4. #include <linux/sched.h>
  5. #include "mon_client.h"
  6. #include "super.h"
  7. #include "auth.h"
  8. #include "decode.h"
  9. /*
  10. * Interact with Ceph monitor cluster. Handle requests for new map
  11. * versions, and periodically resend as needed. Also implement
  12. * statfs() and umount().
  13. *
  14. * A small cluster of Ceph "monitors" are responsible for managing critical
  15. * cluster configuration and state information. An odd number (e.g., 3, 5)
  16. * of cmon daemons use a modified version of the Paxos part-time parliament
  17. * algorithm to manage the MDS map (mds cluster membership), OSD map, and
  18. * list of clients who have mounted the file system.
  19. *
  20. * We maintain an open, active session with a monitor at all times in order to
  21. * receive timely MDSMap updates. We periodically send a keepalive byte on the
  22. * TCP socket to ensure we detect a failure. If the connection does break, we
  23. * randomly hunt for a new monitor. Once the connection is reestablished, we
  24. * resend any outstanding requests.
  25. */
  26. const static struct ceph_connection_operations mon_con_ops;
  27. /*
  28. * Decode a monmap blob (e.g., during mount).
  29. */
  30. struct ceph_monmap *ceph_monmap_decode(void *p, void *end)
  31. {
  32. struct ceph_monmap *m = NULL;
  33. int i, err = -EINVAL;
  34. struct ceph_fsid fsid;
  35. u32 epoch, num_mon;
  36. u16 version;
  37. u32 len;
  38. ceph_decode_32_safe(&p, end, len, bad);
  39. ceph_decode_need(&p, end, len, bad);
  40. dout("monmap_decode %p %p len %d\n", p, end, (int)(end-p));
  41. ceph_decode_16_safe(&p, end, version, bad);
  42. ceph_decode_need(&p, end, sizeof(fsid) + 2*sizeof(u32), bad);
  43. ceph_decode_copy(&p, &fsid, sizeof(fsid));
  44. epoch = ceph_decode_32(&p);
  45. num_mon = ceph_decode_32(&p);
  46. ceph_decode_need(&p, end, num_mon*sizeof(m->mon_inst[0]), bad);
  47. if (num_mon >= CEPH_MAX_MON)
  48. goto bad;
  49. m = kmalloc(sizeof(*m) + sizeof(m->mon_inst[0])*num_mon, GFP_NOFS);
  50. if (m == NULL)
  51. return ERR_PTR(-ENOMEM);
  52. m->fsid = fsid;
  53. m->epoch = epoch;
  54. m->num_mon = num_mon;
  55. ceph_decode_copy(&p, m->mon_inst, num_mon*sizeof(m->mon_inst[0]));
  56. for (i = 0; i < num_mon; i++)
  57. ceph_decode_addr(&m->mon_inst[i].addr);
  58. dout("monmap_decode epoch %d, num_mon %d\n", m->epoch,
  59. m->num_mon);
  60. for (i = 0; i < m->num_mon; i++)
  61. dout("monmap_decode mon%d is %s\n", i,
  62. pr_addr(&m->mon_inst[i].addr.in_addr));
  63. return m;
  64. bad:
  65. dout("monmap_decode failed with %d\n", err);
  66. kfree(m);
  67. return ERR_PTR(err);
  68. }
  69. /*
  70. * return true if *addr is included in the monmap.
  71. */
  72. int ceph_monmap_contains(struct ceph_monmap *m, struct ceph_entity_addr *addr)
  73. {
  74. int i;
  75. for (i = 0; i < m->num_mon; i++)
  76. if (ceph_entity_addr_equal(addr, &m->mon_inst[i].addr))
  77. return 1;
  78. return 0;
  79. }
  80. /*
  81. * Close monitor session, if any.
  82. */
  83. static void __close_session(struct ceph_mon_client *monc)
  84. {
  85. if (monc->con) {
  86. dout("__close_session closing mon%d\n", monc->cur_mon);
  87. ceph_con_revoke(monc->con, monc->m_auth);
  88. ceph_con_close(monc->con);
  89. monc->cur_mon = -1;
  90. ceph_auth_reset(monc->auth);
  91. }
  92. }
  93. /*
  94. * Open a session with a (new) monitor.
  95. */
  96. static int __open_session(struct ceph_mon_client *monc)
  97. {
  98. char r;
  99. int ret;
  100. if (monc->cur_mon < 0) {
  101. get_random_bytes(&r, 1);
  102. monc->cur_mon = r % monc->monmap->num_mon;
  103. dout("open_session num=%d r=%d -> mon%d\n",
  104. monc->monmap->num_mon, r, monc->cur_mon);
  105. monc->sub_sent = 0;
  106. monc->sub_renew_after = jiffies; /* i.e., expired */
  107. monc->want_next_osdmap = !!monc->want_next_osdmap;
  108. dout("open_session mon%d opening\n", monc->cur_mon);
  109. monc->con->peer_name.type = CEPH_ENTITY_TYPE_MON;
  110. monc->con->peer_name.num = cpu_to_le64(monc->cur_mon);
  111. ceph_con_open(monc->con,
  112. &monc->monmap->mon_inst[monc->cur_mon].addr);
  113. /* initiatiate authentication handshake */
  114. ret = ceph_auth_build_hello(monc->auth,
  115. monc->m_auth->front.iov_base,
  116. monc->m_auth->front_max);
  117. monc->m_auth->front.iov_len = ret;
  118. monc->m_auth->hdr.front_len = cpu_to_le32(ret);
  119. ceph_msg_get(monc->m_auth); /* keep our ref */
  120. ceph_con_send(monc->con, monc->m_auth);
  121. } else {
  122. dout("open_session mon%d already open\n", monc->cur_mon);
  123. }
  124. return 0;
  125. }
  126. static bool __sub_expired(struct ceph_mon_client *monc)
  127. {
  128. return time_after_eq(jiffies, monc->sub_renew_after);
  129. }
  130. /*
  131. * Reschedule delayed work timer.
  132. */
  133. static void __schedule_delayed(struct ceph_mon_client *monc)
  134. {
  135. unsigned delay;
  136. if (monc->cur_mon < 0 || __sub_expired(monc))
  137. delay = 10 * HZ;
  138. else
  139. delay = 20 * HZ;
  140. dout("__schedule_delayed after %u\n", delay);
  141. schedule_delayed_work(&monc->delayed_work, delay);
  142. }
  143. /*
  144. * Send subscribe request for mdsmap and/or osdmap.
  145. */
  146. static void __send_subscribe(struct ceph_mon_client *monc)
  147. {
  148. dout("__send_subscribe sub_sent=%u exp=%u want_osd=%d\n",
  149. (unsigned)monc->sub_sent, __sub_expired(monc),
  150. monc->want_next_osdmap);
  151. if ((__sub_expired(monc) && !monc->sub_sent) ||
  152. monc->want_next_osdmap == 1) {
  153. struct ceph_msg *msg;
  154. struct ceph_mon_subscribe_item *i;
  155. void *p, *end;
  156. msg = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE, 96, 0, 0, NULL);
  157. if (!msg)
  158. return;
  159. p = msg->front.iov_base;
  160. end = p + msg->front.iov_len;
  161. dout("__send_subscribe to 'mdsmap' %u+\n",
  162. (unsigned)monc->have_mdsmap);
  163. if (monc->want_next_osdmap) {
  164. dout("__send_subscribe to 'osdmap' %u\n",
  165. (unsigned)monc->have_osdmap);
  166. ceph_encode_32(&p, 3);
  167. ceph_encode_string(&p, end, "osdmap", 6);
  168. i = p;
  169. i->have = cpu_to_le64(monc->have_osdmap);
  170. i->onetime = 1;
  171. p += sizeof(*i);
  172. monc->want_next_osdmap = 2; /* requested */
  173. } else {
  174. ceph_encode_32(&p, 2);
  175. }
  176. ceph_encode_string(&p, end, "mdsmap", 6);
  177. i = p;
  178. i->have = cpu_to_le64(monc->have_mdsmap);
  179. i->onetime = 0;
  180. p += sizeof(*i);
  181. ceph_encode_string(&p, end, "monmap", 6);
  182. i = p;
  183. i->have = 0;
  184. i->onetime = 0;
  185. p += sizeof(*i);
  186. msg->front.iov_len = p - msg->front.iov_base;
  187. msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
  188. ceph_con_send(monc->con, msg);
  189. monc->sub_sent = jiffies | 1; /* never 0 */
  190. }
  191. }
  192. static void handle_subscribe_ack(struct ceph_mon_client *monc,
  193. struct ceph_msg *msg)
  194. {
  195. unsigned seconds;
  196. struct ceph_mon_subscribe_ack *h = msg->front.iov_base;
  197. if (msg->front.iov_len < sizeof(*h))
  198. goto bad;
  199. seconds = le32_to_cpu(h->duration);
  200. mutex_lock(&monc->mutex);
  201. if (monc->hunting) {
  202. pr_info("mon%d %s session established\n",
  203. monc->cur_mon, pr_addr(&monc->con->peer_addr.in_addr));
  204. monc->hunting = false;
  205. }
  206. dout("handle_subscribe_ack after %d seconds\n", seconds);
  207. monc->sub_renew_after = monc->sub_sent + (seconds >> 1)*HZ - 1;
  208. monc->sub_sent = 0;
  209. mutex_unlock(&monc->mutex);
  210. return;
  211. bad:
  212. pr_err("got corrupt subscribe-ack msg\n");
  213. }
  214. /*
  215. * Keep track of which maps we have
  216. */
  217. int ceph_monc_got_mdsmap(struct ceph_mon_client *monc, u32 got)
  218. {
  219. mutex_lock(&monc->mutex);
  220. monc->have_mdsmap = got;
  221. mutex_unlock(&monc->mutex);
  222. return 0;
  223. }
  224. int ceph_monc_got_osdmap(struct ceph_mon_client *monc, u32 got)
  225. {
  226. mutex_lock(&monc->mutex);
  227. monc->have_osdmap = got;
  228. monc->want_next_osdmap = 0;
  229. mutex_unlock(&monc->mutex);
  230. return 0;
  231. }
  232. /*
  233. * Register interest in the next osdmap
  234. */
  235. void ceph_monc_request_next_osdmap(struct ceph_mon_client *monc)
  236. {
  237. dout("request_next_osdmap have %u\n", monc->have_osdmap);
  238. mutex_lock(&monc->mutex);
  239. if (!monc->want_next_osdmap)
  240. monc->want_next_osdmap = 1;
  241. if (monc->want_next_osdmap < 2)
  242. __send_subscribe(monc);
  243. mutex_unlock(&monc->mutex);
  244. }
  245. /*
  246. *
  247. */
  248. int ceph_monc_open_session(struct ceph_mon_client *monc)
  249. {
  250. if (!monc->con) {
  251. monc->con = kmalloc(sizeof(*monc->con), GFP_KERNEL);
  252. if (!monc->con)
  253. return -ENOMEM;
  254. ceph_con_init(monc->client->msgr, monc->con);
  255. monc->con->private = monc;
  256. monc->con->ops = &mon_con_ops;
  257. }
  258. mutex_lock(&monc->mutex);
  259. __open_session(monc);
  260. __schedule_delayed(monc);
  261. mutex_unlock(&monc->mutex);
  262. return 0;
  263. }
  264. /*
  265. * The monitor responds with mount ack indicate mount success. The
  266. * included client ticket allows the client to talk to MDSs and OSDs.
  267. */
  268. static void ceph_monc_handle_map(struct ceph_mon_client *monc,
  269. struct ceph_msg *msg)
  270. {
  271. struct ceph_client *client = monc->client;
  272. struct ceph_monmap *monmap = NULL, *old = monc->monmap;
  273. void *p, *end;
  274. mutex_lock(&monc->mutex);
  275. dout("handle_monmap\n");
  276. p = msg->front.iov_base;
  277. end = p + msg->front.iov_len;
  278. monmap = ceph_monmap_decode(p, end);
  279. if (IS_ERR(monmap)) {
  280. pr_err("problem decoding monmap, %d\n",
  281. (int)PTR_ERR(monmap));
  282. goto out;
  283. }
  284. if (ceph_check_fsid(monc->client, &monmap->fsid) < 0) {
  285. kfree(monmap);
  286. goto out;
  287. }
  288. client->monc.monmap = monmap;
  289. kfree(old);
  290. out:
  291. mutex_unlock(&monc->mutex);
  292. wake_up(&client->mount_wq);
  293. }
  294. /*
  295. * statfs
  296. */
  297. static void handle_statfs_reply(struct ceph_mon_client *monc,
  298. struct ceph_msg *msg)
  299. {
  300. struct ceph_mon_statfs_request *req;
  301. struct ceph_mon_statfs_reply *reply = msg->front.iov_base;
  302. u64 tid;
  303. if (msg->front.iov_len != sizeof(*reply))
  304. goto bad;
  305. tid = le64_to_cpu(reply->tid);
  306. dout("handle_statfs_reply %p tid %llu\n", msg, tid);
  307. mutex_lock(&monc->mutex);
  308. req = radix_tree_lookup(&monc->statfs_request_tree, tid);
  309. if (req) {
  310. *req->buf = reply->st;
  311. req->result = 0;
  312. }
  313. mutex_unlock(&monc->mutex);
  314. if (req)
  315. complete(&req->completion);
  316. return;
  317. bad:
  318. pr_err("corrupt statfs reply, no tid\n");
  319. }
  320. /*
  321. * (re)send a statfs request
  322. */
  323. static int send_statfs(struct ceph_mon_client *monc,
  324. struct ceph_mon_statfs_request *req)
  325. {
  326. struct ceph_msg *msg;
  327. struct ceph_mon_statfs *h;
  328. dout("send_statfs tid %llu\n", req->tid);
  329. msg = ceph_msg_new(CEPH_MSG_STATFS, sizeof(*h), 0, 0, NULL);
  330. if (IS_ERR(msg))
  331. return PTR_ERR(msg);
  332. req->request = msg;
  333. h = msg->front.iov_base;
  334. h->monhdr.have_version = 0;
  335. h->monhdr.session_mon = cpu_to_le16(-1);
  336. h->monhdr.session_mon_tid = 0;
  337. h->fsid = monc->monmap->fsid;
  338. h->tid = cpu_to_le64(req->tid);
  339. ceph_con_send(monc->con, msg);
  340. return 0;
  341. }
  342. /*
  343. * Do a synchronous statfs().
  344. */
  345. int ceph_monc_do_statfs(struct ceph_mon_client *monc, struct ceph_statfs *buf)
  346. {
  347. struct ceph_mon_statfs_request req;
  348. int err;
  349. req.buf = buf;
  350. init_completion(&req.completion);
  351. /* allocate memory for reply */
  352. err = ceph_msgpool_resv(&monc->msgpool_statfs_reply, 1);
  353. if (err)
  354. return err;
  355. /* register request */
  356. mutex_lock(&monc->mutex);
  357. req.tid = ++monc->last_tid;
  358. req.last_attempt = jiffies;
  359. req.delay = BASE_DELAY_INTERVAL;
  360. if (radix_tree_insert(&monc->statfs_request_tree, req.tid, &req) < 0) {
  361. mutex_unlock(&monc->mutex);
  362. pr_err("ENOMEM in do_statfs\n");
  363. return -ENOMEM;
  364. }
  365. monc->num_statfs_requests++;
  366. mutex_unlock(&monc->mutex);
  367. /* send request and wait */
  368. err = send_statfs(monc, &req);
  369. if (!err)
  370. err = wait_for_completion_interruptible(&req.completion);
  371. mutex_lock(&monc->mutex);
  372. radix_tree_delete(&monc->statfs_request_tree, req.tid);
  373. monc->num_statfs_requests--;
  374. ceph_msgpool_resv(&monc->msgpool_statfs_reply, -1);
  375. mutex_unlock(&monc->mutex);
  376. if (!err)
  377. err = req.result;
  378. return err;
  379. }
  380. /*
  381. * Resend pending statfs requests.
  382. */
  383. static void __resend_statfs(struct ceph_mon_client *monc)
  384. {
  385. u64 next_tid = 0;
  386. int got;
  387. int did = 0;
  388. struct ceph_mon_statfs_request *req;
  389. while (1) {
  390. got = radix_tree_gang_lookup(&monc->statfs_request_tree,
  391. (void **)&req,
  392. next_tid, 1);
  393. if (got == 0)
  394. break;
  395. did++;
  396. next_tid = req->tid + 1;
  397. send_statfs(monc, req);
  398. }
  399. }
  400. /*
  401. * Delayed work. If we haven't mounted yet, retry. Otherwise,
  402. * renew/retry subscription as needed (in case it is timing out, or we
  403. * got an ENOMEM). And keep the monitor connection alive.
  404. */
  405. static void delayed_work(struct work_struct *work)
  406. {
  407. struct ceph_mon_client *monc =
  408. container_of(work, struct ceph_mon_client, delayed_work.work);
  409. dout("monc delayed_work\n");
  410. mutex_lock(&monc->mutex);
  411. if (monc->hunting) {
  412. __close_session(monc);
  413. __open_session(monc); /* continue hunting */
  414. } else {
  415. ceph_con_keepalive(monc->con);
  416. if (monc->auth->ops->is_authenticated(monc->auth))
  417. __send_subscribe(monc);
  418. }
  419. __schedule_delayed(monc);
  420. mutex_unlock(&monc->mutex);
  421. }
  422. /*
  423. * On startup, we build a temporary monmap populated with the IPs
  424. * provided by mount(2).
  425. */
  426. static int build_initial_monmap(struct ceph_mon_client *monc)
  427. {
  428. struct ceph_mount_args *args = monc->client->mount_args;
  429. struct ceph_entity_addr *mon_addr = args->mon_addr;
  430. int num_mon = args->num_mon;
  431. int i;
  432. /* build initial monmap */
  433. monc->monmap = kzalloc(sizeof(*monc->monmap) +
  434. num_mon*sizeof(monc->monmap->mon_inst[0]),
  435. GFP_KERNEL);
  436. if (!monc->monmap)
  437. return -ENOMEM;
  438. for (i = 0; i < num_mon; i++) {
  439. monc->monmap->mon_inst[i].addr = mon_addr[i];
  440. monc->monmap->mon_inst[i].addr.erank = 0;
  441. monc->monmap->mon_inst[i].addr.nonce = 0;
  442. monc->monmap->mon_inst[i].name.type =
  443. CEPH_ENTITY_TYPE_MON;
  444. monc->monmap->mon_inst[i].name.num = cpu_to_le64(i);
  445. }
  446. monc->monmap->num_mon = num_mon;
  447. monc->have_fsid = false;
  448. /* release addr memory */
  449. kfree(args->mon_addr);
  450. args->mon_addr = NULL;
  451. args->num_mon = 0;
  452. return 0;
  453. }
  454. int ceph_monc_init(struct ceph_mon_client *monc, struct ceph_client *cl)
  455. {
  456. int err = 0;
  457. dout("init\n");
  458. memset(monc, 0, sizeof(*monc));
  459. monc->client = cl;
  460. monc->monmap = NULL;
  461. mutex_init(&monc->mutex);
  462. err = build_initial_monmap(monc);
  463. if (err)
  464. goto out;
  465. monc->con = NULL;
  466. /* authentication */
  467. monc->auth = ceph_auth_init(cl->mount_args->name,
  468. cl->mount_args->secret);
  469. if (IS_ERR(monc->auth))
  470. return PTR_ERR(monc->auth);
  471. monc->auth->want_keys =
  472. CEPH_ENTITY_TYPE_AUTH | CEPH_ENTITY_TYPE_MON |
  473. CEPH_ENTITY_TYPE_OSD | CEPH_ENTITY_TYPE_MDS;
  474. /* msg pools */
  475. err = ceph_msgpool_init(&monc->msgpool_subscribe_ack,
  476. sizeof(struct ceph_mon_subscribe_ack), 1, false);
  477. if (err < 0)
  478. goto out_monmap;
  479. err = ceph_msgpool_init(&monc->msgpool_statfs_reply,
  480. sizeof(struct ceph_mon_statfs_reply), 0, false);
  481. if (err < 0)
  482. goto out_pool1;
  483. err = ceph_msgpool_init(&monc->msgpool_auth_reply, 4096, 1, false);
  484. if (err < 0)
  485. goto out_pool2;
  486. monc->m_auth = ceph_msg_new(CEPH_MSG_AUTH, 4096, 0, 0, NULL);
  487. if (IS_ERR(monc->m_auth)) {
  488. err = PTR_ERR(monc->m_auth);
  489. monc->m_auth = NULL;
  490. goto out_pool3;
  491. }
  492. monc->cur_mon = -1;
  493. monc->hunting = true;
  494. monc->sub_renew_after = jiffies;
  495. monc->sub_sent = 0;
  496. INIT_DELAYED_WORK(&monc->delayed_work, delayed_work);
  497. INIT_RADIX_TREE(&monc->statfs_request_tree, GFP_NOFS);
  498. monc->num_statfs_requests = 0;
  499. monc->last_tid = 0;
  500. monc->have_mdsmap = 0;
  501. monc->have_osdmap = 0;
  502. monc->want_next_osdmap = 1;
  503. return 0;
  504. out_pool3:
  505. ceph_msgpool_destroy(&monc->msgpool_auth_reply);
  506. out_pool2:
  507. ceph_msgpool_destroy(&monc->msgpool_subscribe_ack);
  508. out_pool1:
  509. ceph_msgpool_destroy(&monc->msgpool_statfs_reply);
  510. out_monmap:
  511. kfree(monc->monmap);
  512. out:
  513. return err;
  514. }
  515. void ceph_monc_stop(struct ceph_mon_client *monc)
  516. {
  517. dout("stop\n");
  518. cancel_delayed_work_sync(&monc->delayed_work);
  519. mutex_lock(&monc->mutex);
  520. __close_session(monc);
  521. if (monc->con) {
  522. monc->con->private = NULL;
  523. monc->con->ops->put(monc->con);
  524. monc->con = NULL;
  525. }
  526. mutex_unlock(&monc->mutex);
  527. ceph_auth_destroy(monc->auth);
  528. ceph_msg_put(monc->m_auth);
  529. ceph_msgpool_destroy(&monc->msgpool_subscribe_ack);
  530. ceph_msgpool_destroy(&monc->msgpool_statfs_reply);
  531. ceph_msgpool_destroy(&monc->msgpool_auth_reply);
  532. kfree(monc->monmap);
  533. }
  534. static void handle_auth_reply(struct ceph_mon_client *monc,
  535. struct ceph_msg *msg)
  536. {
  537. int ret;
  538. mutex_lock(&monc->mutex);
  539. ret = ceph_handle_auth_reply(monc->auth, msg->front.iov_base,
  540. msg->front.iov_len,
  541. monc->m_auth->front.iov_base,
  542. monc->m_auth->front_max);
  543. if (ret < 0) {
  544. monc->client->mount_err = ret;
  545. wake_up(&monc->client->mount_wq);
  546. } else if (ret > 0) {
  547. monc->m_auth->front.iov_len = ret;
  548. monc->m_auth->hdr.front_len = cpu_to_le32(ret);
  549. ceph_msg_get(monc->m_auth); /* keep our ref */
  550. ceph_con_send(monc->con, monc->m_auth);
  551. } else if (monc->auth->ops->is_authenticated(monc->auth)) {
  552. dout("authenticated, starting session\n");
  553. monc->client->msgr->inst.name.type = CEPH_ENTITY_TYPE_CLIENT;
  554. monc->client->msgr->inst.name.num = monc->auth->global_id;
  555. __send_subscribe(monc);
  556. __resend_statfs(monc);
  557. }
  558. mutex_unlock(&monc->mutex);
  559. }
  560. /*
  561. * handle incoming message
  562. */
  563. static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
  564. {
  565. struct ceph_mon_client *monc = con->private;
  566. int type = le16_to_cpu(msg->hdr.type);
  567. if (!monc)
  568. return;
  569. switch (type) {
  570. case CEPH_MSG_AUTH_REPLY:
  571. handle_auth_reply(monc, msg);
  572. break;
  573. case CEPH_MSG_MON_SUBSCRIBE_ACK:
  574. handle_subscribe_ack(monc, msg);
  575. break;
  576. case CEPH_MSG_STATFS_REPLY:
  577. handle_statfs_reply(monc, msg);
  578. break;
  579. case CEPH_MSG_MON_MAP:
  580. ceph_monc_handle_map(monc, msg);
  581. break;
  582. case CEPH_MSG_MDS_MAP:
  583. ceph_mdsc_handle_map(&monc->client->mdsc, msg);
  584. break;
  585. case CEPH_MSG_OSD_MAP:
  586. ceph_osdc_handle_map(&monc->client->osdc, msg);
  587. break;
  588. default:
  589. pr_err("received unknown message type %d %s\n", type,
  590. ceph_msg_type_name(type));
  591. }
  592. ceph_msg_put(msg);
  593. }
  594. /*
  595. * Allocate memory for incoming message
  596. */
  597. static struct ceph_msg *mon_alloc_msg(struct ceph_connection *con,
  598. struct ceph_msg_header *hdr)
  599. {
  600. struct ceph_mon_client *monc = con->private;
  601. int type = le16_to_cpu(hdr->type);
  602. int front = le32_to_cpu(hdr->front_len);
  603. switch (type) {
  604. case CEPH_MSG_MON_SUBSCRIBE_ACK:
  605. return ceph_msgpool_get(&monc->msgpool_subscribe_ack, front);
  606. case CEPH_MSG_STATFS_REPLY:
  607. return ceph_msgpool_get(&monc->msgpool_statfs_reply, front);
  608. case CEPH_MSG_AUTH_REPLY:
  609. return ceph_msgpool_get(&monc->msgpool_auth_reply, front);
  610. }
  611. return ceph_alloc_msg(con, hdr);
  612. }
  613. /*
  614. * If the monitor connection resets, pick a new monitor and resubmit
  615. * any pending requests.
  616. */
  617. static void mon_fault(struct ceph_connection *con)
  618. {
  619. struct ceph_mon_client *monc = con->private;
  620. if (!monc)
  621. return;
  622. dout("mon_fault\n");
  623. mutex_lock(&monc->mutex);
  624. if (!con->private)
  625. goto out;
  626. if (monc->con && !monc->hunting)
  627. pr_info("mon%d %s session lost, "
  628. "hunting for new mon\n", monc->cur_mon,
  629. pr_addr(&monc->con->peer_addr.in_addr));
  630. __close_session(monc);
  631. if (!monc->hunting) {
  632. /* start hunting */
  633. monc->hunting = true;
  634. __open_session(monc);
  635. } else {
  636. /* already hunting, let's wait a bit */
  637. __schedule_delayed(monc);
  638. }
  639. out:
  640. mutex_unlock(&monc->mutex);
  641. }
  642. const static struct ceph_connection_operations mon_con_ops = {
  643. .get = ceph_con_get,
  644. .put = ceph_con_put,
  645. .dispatch = dispatch,
  646. .fault = mon_fault,
  647. .alloc_msg = mon_alloc_msg,
  648. .alloc_middle = ceph_alloc_middle,
  649. };