mon_client.c 20 KB

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