mon_client.c 19 KB

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