mon_client.c 18 KB

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