mon_client.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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. return;
  283. }
  284. if (ceph_check_fsid(monc->client, &monmap->fsid) < 0) {
  285. kfree(monmap);
  286. return;
  287. }
  288. client->monc.monmap = monmap;
  289. kfree(old);
  290. mutex_unlock(&monc->mutex);
  291. wake_up(&client->mount_wq);
  292. }
  293. /*
  294. * statfs
  295. */
  296. static void handle_statfs_reply(struct ceph_mon_client *monc,
  297. struct ceph_msg *msg)
  298. {
  299. struct ceph_mon_statfs_request *req;
  300. struct ceph_mon_statfs_reply *reply = msg->front.iov_base;
  301. u64 tid;
  302. if (msg->front.iov_len != sizeof(*reply))
  303. goto bad;
  304. tid = le64_to_cpu(reply->tid);
  305. dout("handle_statfs_reply %p tid %llu\n", msg, tid);
  306. mutex_lock(&monc->mutex);
  307. req = radix_tree_lookup(&monc->statfs_request_tree, tid);
  308. if (req) {
  309. *req->buf = reply->st;
  310. req->result = 0;
  311. }
  312. mutex_unlock(&monc->mutex);
  313. if (req)
  314. complete(&req->completion);
  315. return;
  316. bad:
  317. pr_err("corrupt statfs reply, no tid\n");
  318. }
  319. /*
  320. * (re)send a statfs request
  321. */
  322. static int send_statfs(struct ceph_mon_client *monc,
  323. struct ceph_mon_statfs_request *req)
  324. {
  325. struct ceph_msg *msg;
  326. struct ceph_mon_statfs *h;
  327. dout("send_statfs tid %llu\n", req->tid);
  328. msg = ceph_msg_new(CEPH_MSG_STATFS, sizeof(*h), 0, 0, NULL);
  329. if (IS_ERR(msg))
  330. return PTR_ERR(msg);
  331. req->request = msg;
  332. h = msg->front.iov_base;
  333. h->monhdr.have_version = 0;
  334. h->monhdr.session_mon = cpu_to_le16(-1);
  335. h->monhdr.session_mon_tid = 0;
  336. h->fsid = monc->monmap->fsid;
  337. h->tid = cpu_to_le64(req->tid);
  338. ceph_con_send(monc->con, msg);
  339. return 0;
  340. }
  341. /*
  342. * Do a synchronous statfs().
  343. */
  344. int ceph_monc_do_statfs(struct ceph_mon_client *monc, struct ceph_statfs *buf)
  345. {
  346. struct ceph_mon_statfs_request req;
  347. int err;
  348. req.buf = buf;
  349. init_completion(&req.completion);
  350. /* allocate memory for reply */
  351. err = ceph_msgpool_resv(&monc->msgpool_statfs_reply, 1);
  352. if (err)
  353. return err;
  354. /* register request */
  355. mutex_lock(&monc->mutex);
  356. req.tid = ++monc->last_tid;
  357. req.last_attempt = jiffies;
  358. req.delay = BASE_DELAY_INTERVAL;
  359. if (radix_tree_insert(&monc->statfs_request_tree, req.tid, &req) < 0) {
  360. mutex_unlock(&monc->mutex);
  361. pr_err("ENOMEM in do_statfs\n");
  362. return -ENOMEM;
  363. }
  364. monc->num_statfs_requests++;
  365. mutex_unlock(&monc->mutex);
  366. /* send request and wait */
  367. err = send_statfs(monc, &req);
  368. if (!err)
  369. err = wait_for_completion_interruptible(&req.completion);
  370. mutex_lock(&monc->mutex);
  371. radix_tree_delete(&monc->statfs_request_tree, req.tid);
  372. monc->num_statfs_requests--;
  373. ceph_msgpool_resv(&monc->msgpool_statfs_reply, -1);
  374. mutex_unlock(&monc->mutex);
  375. if (!err)
  376. err = req.result;
  377. return err;
  378. }
  379. /*
  380. * Resend pending statfs requests.
  381. */
  382. static void __resend_statfs(struct ceph_mon_client *monc)
  383. {
  384. u64 next_tid = 0;
  385. int got;
  386. int did = 0;
  387. struct ceph_mon_statfs_request *req;
  388. while (1) {
  389. got = radix_tree_gang_lookup(&monc->statfs_request_tree,
  390. (void **)&req,
  391. next_tid, 1);
  392. if (got == 0)
  393. break;
  394. did++;
  395. next_tid = req->tid + 1;
  396. send_statfs(monc, req);
  397. }
  398. }
  399. /*
  400. * Delayed work. If we haven't mounted yet, retry. Otherwise,
  401. * renew/retry subscription as needed (in case it is timing out, or we
  402. * got an ENOMEM). And keep the monitor connection alive.
  403. */
  404. static void delayed_work(struct work_struct *work)
  405. {
  406. struct ceph_mon_client *monc =
  407. container_of(work, struct ceph_mon_client, delayed_work.work);
  408. dout("monc delayed_work\n");
  409. mutex_lock(&monc->mutex);
  410. if (monc->hunting) {
  411. __close_session(monc);
  412. __open_session(monc); /* continue hunting */
  413. } else {
  414. ceph_con_keepalive(monc->con);
  415. if (monc->auth->ops->is_authenticated(monc->auth))
  416. __send_subscribe(monc);
  417. }
  418. __schedule_delayed(monc);
  419. mutex_unlock(&monc->mutex);
  420. }
  421. /*
  422. * On startup, we build a temporary monmap populated with the IPs
  423. * provided by mount(2).
  424. */
  425. static int build_initial_monmap(struct ceph_mon_client *monc)
  426. {
  427. struct ceph_mount_args *args = monc->client->mount_args;
  428. struct ceph_entity_addr *mon_addr = args->mon_addr;
  429. int num_mon = args->num_mon;
  430. int i;
  431. /* build initial monmap */
  432. monc->monmap = kzalloc(sizeof(*monc->monmap) +
  433. num_mon*sizeof(monc->monmap->mon_inst[0]),
  434. GFP_KERNEL);
  435. if (!monc->monmap)
  436. return -ENOMEM;
  437. for (i = 0; i < num_mon; i++) {
  438. monc->monmap->mon_inst[i].addr = mon_addr[i];
  439. monc->monmap->mon_inst[i].addr.erank = 0;
  440. monc->monmap->mon_inst[i].addr.nonce = 0;
  441. monc->monmap->mon_inst[i].name.type =
  442. CEPH_ENTITY_TYPE_MON;
  443. monc->monmap->mon_inst[i].name.num = cpu_to_le64(i);
  444. }
  445. monc->monmap->num_mon = num_mon;
  446. monc->have_fsid = false;
  447. /* release addr memory */
  448. kfree(args->mon_addr);
  449. args->mon_addr = NULL;
  450. args->num_mon = 0;
  451. return 0;
  452. }
  453. int ceph_monc_init(struct ceph_mon_client *monc, struct ceph_client *cl)
  454. {
  455. int err = 0;
  456. dout("init\n");
  457. memset(monc, 0, sizeof(*monc));
  458. monc->client = cl;
  459. monc->monmap = NULL;
  460. mutex_init(&monc->mutex);
  461. err = build_initial_monmap(monc);
  462. if (err)
  463. goto out;
  464. monc->con = NULL;
  465. /* authentication */
  466. monc->auth = ceph_auth_init(cl->mount_args->name,
  467. cl->mount_args->secret);
  468. if (IS_ERR(monc->auth))
  469. return PTR_ERR(monc->auth);
  470. monc->auth->want_keys =
  471. CEPH_ENTITY_TYPE_AUTH | CEPH_ENTITY_TYPE_MON |
  472. CEPH_ENTITY_TYPE_OSD | CEPH_ENTITY_TYPE_MDS;
  473. /* msg pools */
  474. err = ceph_msgpool_init(&monc->msgpool_subscribe_ack,
  475. sizeof(struct ceph_mon_subscribe_ack), 1, false);
  476. if (err < 0)
  477. goto out_monmap;
  478. err = ceph_msgpool_init(&monc->msgpool_statfs_reply,
  479. sizeof(struct ceph_mon_statfs_reply), 0, false);
  480. if (err < 0)
  481. goto out_pool1;
  482. err = ceph_msgpool_init(&monc->msgpool_auth_reply, 4096, 1, false);
  483. if (err < 0)
  484. goto out_pool2;
  485. monc->m_auth = ceph_msg_new(CEPH_MSG_AUTH, 4096, 0, 0, NULL);
  486. if (IS_ERR(monc->m_auth)) {
  487. err = PTR_ERR(monc->m_auth);
  488. monc->m_auth = NULL;
  489. goto out_pool3;
  490. }
  491. monc->cur_mon = -1;
  492. monc->hunting = true;
  493. monc->sub_renew_after = jiffies;
  494. monc->sub_sent = 0;
  495. INIT_DELAYED_WORK(&monc->delayed_work, delayed_work);
  496. INIT_RADIX_TREE(&monc->statfs_request_tree, GFP_NOFS);
  497. monc->num_statfs_requests = 0;
  498. monc->last_tid = 0;
  499. monc->have_mdsmap = 0;
  500. monc->have_osdmap = 0;
  501. monc->want_next_osdmap = 1;
  502. return 0;
  503. out_pool3:
  504. ceph_msgpool_destroy(&monc->msgpool_auth_reply);
  505. out_pool2:
  506. ceph_msgpool_destroy(&monc->msgpool_subscribe_ack);
  507. out_pool1:
  508. ceph_msgpool_destroy(&monc->msgpool_statfs_reply);
  509. out_monmap:
  510. kfree(monc->monmap);
  511. out:
  512. return err;
  513. }
  514. void ceph_monc_stop(struct ceph_mon_client *monc)
  515. {
  516. dout("stop\n");
  517. cancel_delayed_work_sync(&monc->delayed_work);
  518. mutex_lock(&monc->mutex);
  519. __close_session(monc);
  520. if (monc->con) {
  521. monc->con->private = NULL;
  522. monc->con->ops->put(monc->con);
  523. monc->con = NULL;
  524. }
  525. mutex_unlock(&monc->mutex);
  526. ceph_auth_destroy(monc->auth);
  527. ceph_msg_put(monc->m_auth);
  528. ceph_msgpool_destroy(&monc->msgpool_subscribe_ack);
  529. ceph_msgpool_destroy(&monc->msgpool_statfs_reply);
  530. ceph_msgpool_destroy(&monc->msgpool_auth_reply);
  531. kfree(monc->monmap);
  532. }
  533. static void handle_auth_reply(struct ceph_mon_client *monc,
  534. struct ceph_msg *msg)
  535. {
  536. int ret;
  537. mutex_lock(&monc->mutex);
  538. ret = ceph_handle_auth_reply(monc->auth, msg->front.iov_base,
  539. msg->front.iov_len,
  540. monc->m_auth->front.iov_base,
  541. monc->m_auth->front_max);
  542. if (ret < 0) {
  543. monc->client->mount_err = ret;
  544. wake_up(&monc->client->mount_wq);
  545. } else if (ret > 0) {
  546. monc->m_auth->front.iov_len = ret;
  547. monc->m_auth->hdr.front_len = cpu_to_le32(ret);
  548. ceph_msg_get(monc->m_auth); /* keep our ref */
  549. ceph_con_send(monc->con, monc->m_auth);
  550. } else if (monc->auth->ops->is_authenticated(monc->auth)) {
  551. dout("authenticated, starting session\n");
  552. monc->client->msgr->inst.name.type = CEPH_ENTITY_TYPE_CLIENT;
  553. monc->client->msgr->inst.name.num = monc->auth->global_id;
  554. __send_subscribe(monc);
  555. __resend_statfs(monc);
  556. }
  557. mutex_unlock(&monc->mutex);
  558. }
  559. /*
  560. * handle incoming message
  561. */
  562. static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
  563. {
  564. struct ceph_mon_client *monc = con->private;
  565. int type = le16_to_cpu(msg->hdr.type);
  566. if (!monc)
  567. return;
  568. switch (type) {
  569. case CEPH_MSG_AUTH_REPLY:
  570. handle_auth_reply(monc, msg);
  571. break;
  572. case CEPH_MSG_MON_SUBSCRIBE_ACK:
  573. handle_subscribe_ack(monc, msg);
  574. break;
  575. case CEPH_MSG_STATFS_REPLY:
  576. handle_statfs_reply(monc, msg);
  577. break;
  578. case CEPH_MSG_MON_MAP:
  579. ceph_monc_handle_map(monc, msg);
  580. break;
  581. case CEPH_MSG_MDS_MAP:
  582. ceph_mdsc_handle_map(&monc->client->mdsc, msg);
  583. break;
  584. case CEPH_MSG_OSD_MAP:
  585. ceph_osdc_handle_map(&monc->client->osdc, msg);
  586. break;
  587. default:
  588. pr_err("received unknown message type %d %s\n", type,
  589. ceph_msg_type_name(type));
  590. }
  591. ceph_msg_put(msg);
  592. }
  593. /*
  594. * Allocate memory for incoming message
  595. */
  596. static struct ceph_msg *mon_alloc_msg(struct ceph_connection *con,
  597. struct ceph_msg_header *hdr)
  598. {
  599. struct ceph_mon_client *monc = con->private;
  600. int type = le16_to_cpu(hdr->type);
  601. int front = le32_to_cpu(hdr->front_len);
  602. switch (type) {
  603. case CEPH_MSG_MON_SUBSCRIBE_ACK:
  604. return ceph_msgpool_get(&monc->msgpool_subscribe_ack, front);
  605. case CEPH_MSG_STATFS_REPLY:
  606. return ceph_msgpool_get(&monc->msgpool_statfs_reply, front);
  607. case CEPH_MSG_AUTH_REPLY:
  608. return ceph_msgpool_get(&monc->msgpool_auth_reply, front);
  609. }
  610. return ceph_alloc_msg(con, hdr);
  611. }
  612. /*
  613. * If the monitor connection resets, pick a new monitor and resubmit
  614. * any pending requests.
  615. */
  616. static void mon_fault(struct ceph_connection *con)
  617. {
  618. struct ceph_mon_client *monc = con->private;
  619. if (!monc)
  620. return;
  621. dout("mon_fault\n");
  622. mutex_lock(&monc->mutex);
  623. if (!con->private)
  624. goto out;
  625. if (monc->con && !monc->hunting)
  626. pr_info("mon%d %s session lost, "
  627. "hunting for new mon\n", monc->cur_mon,
  628. pr_addr(&monc->con->peer_addr.in_addr));
  629. __close_session(monc);
  630. if (!monc->hunting) {
  631. /* start hunting */
  632. monc->hunting = true;
  633. __open_session(monc);
  634. } else {
  635. /* already hunting, let's wait a bit */
  636. __schedule_delayed(monc);
  637. }
  638. out:
  639. mutex_unlock(&monc->mutex);
  640. }
  641. const static struct ceph_connection_operations mon_con_ops = {
  642. .get = ceph_con_get,
  643. .put = ceph_con_put,
  644. .dispatch = dispatch,
  645. .fault = mon_fault,
  646. .alloc_msg = mon_alloc_msg,
  647. .alloc_middle = ceph_alloc_middle,
  648. };