mon_client.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. #include "ceph_debug.h"
  2. #include <linux/types.h>
  3. #include <linux/slab.h>
  4. #include <linux/random.h>
  5. #include <linux/sched.h>
  6. #include "mon_client.h"
  7. #include "super.h"
  8. #include "auth.h"
  9. #include "decode.h"
  10. /*
  11. * Interact with Ceph monitor cluster. Handle requests for new map
  12. * versions, and periodically resend as needed. Also implement
  13. * statfs() and umount().
  14. *
  15. * A small cluster of Ceph "monitors" are responsible for managing critical
  16. * cluster configuration and state information. An odd number (e.g., 3, 5)
  17. * of cmon daemons use a modified version of the Paxos part-time parliament
  18. * algorithm to manage the MDS map (mds cluster membership), OSD map, and
  19. * list of clients who have mounted the file system.
  20. *
  21. * We maintain an open, active session with a monitor at all times in order to
  22. * receive timely MDSMap updates. We periodically send a keepalive byte on the
  23. * TCP socket to ensure we detect a failure. If the connection does break, we
  24. * randomly hunt for a new monitor. Once the connection is reestablished, we
  25. * resend any outstanding requests.
  26. */
  27. static const struct ceph_connection_operations mon_con_ops;
  28. static int __validate_auth(struct ceph_mon_client *monc);
  29. /*
  30. * Decode a monmap blob (e.g., during mount).
  31. */
  32. struct ceph_monmap *ceph_monmap_decode(void *p, void *end)
  33. {
  34. struct ceph_monmap *m = NULL;
  35. int i, err = -EINVAL;
  36. struct ceph_fsid fsid;
  37. u32 epoch, num_mon;
  38. u16 version;
  39. u32 len;
  40. ceph_decode_32_safe(&p, end, len, bad);
  41. ceph_decode_need(&p, end, len, bad);
  42. dout("monmap_decode %p %p len %d\n", p, end, (int)(end-p));
  43. ceph_decode_16_safe(&p, end, version, bad);
  44. ceph_decode_need(&p, end, sizeof(fsid) + 2*sizeof(u32), bad);
  45. ceph_decode_copy(&p, &fsid, sizeof(fsid));
  46. epoch = ceph_decode_32(&p);
  47. num_mon = ceph_decode_32(&p);
  48. ceph_decode_need(&p, end, num_mon*sizeof(m->mon_inst[0]), bad);
  49. if (num_mon >= CEPH_MAX_MON)
  50. goto bad;
  51. m = kmalloc(sizeof(*m) + sizeof(m->mon_inst[0])*num_mon, GFP_NOFS);
  52. if (m == NULL)
  53. return ERR_PTR(-ENOMEM);
  54. m->fsid = fsid;
  55. m->epoch = epoch;
  56. m->num_mon = num_mon;
  57. ceph_decode_copy(&p, m->mon_inst, num_mon*sizeof(m->mon_inst[0]));
  58. for (i = 0; i < num_mon; i++)
  59. ceph_decode_addr(&m->mon_inst[i].addr);
  60. dout("monmap_decode epoch %d, num_mon %d\n", m->epoch,
  61. m->num_mon);
  62. for (i = 0; i < m->num_mon; i++)
  63. dout("monmap_decode mon%d is %s\n", i,
  64. pr_addr(&m->mon_inst[i].addr.in_addr));
  65. return m;
  66. bad:
  67. dout("monmap_decode failed with %d\n", err);
  68. kfree(m);
  69. return ERR_PTR(err);
  70. }
  71. /*
  72. * return true if *addr is included in the monmap.
  73. */
  74. int ceph_monmap_contains(struct ceph_monmap *m, struct ceph_entity_addr *addr)
  75. {
  76. int i;
  77. for (i = 0; i < m->num_mon; i++)
  78. if (memcmp(addr, &m->mon_inst[i].addr, sizeof(*addr)) == 0)
  79. return 1;
  80. return 0;
  81. }
  82. /*
  83. * Send an auth request.
  84. */
  85. static void __send_prepared_auth_request(struct ceph_mon_client *monc, int len)
  86. {
  87. monc->pending_auth = 1;
  88. monc->m_auth->front.iov_len = len;
  89. monc->m_auth->hdr.front_len = cpu_to_le32(len);
  90. ceph_con_revoke(monc->con, monc->m_auth);
  91. ceph_msg_get(monc->m_auth); /* keep our ref */
  92. ceph_con_send(monc->con, monc->m_auth);
  93. }
  94. /*
  95. * Close monitor session, if any.
  96. */
  97. static void __close_session(struct ceph_mon_client *monc)
  98. {
  99. if (monc->con) {
  100. dout("__close_session closing mon%d\n", monc->cur_mon);
  101. ceph_con_revoke(monc->con, monc->m_auth);
  102. ceph_con_close(monc->con);
  103. monc->cur_mon = -1;
  104. monc->pending_auth = 0;
  105. ceph_auth_reset(monc->auth);
  106. }
  107. }
  108. /*
  109. * Open a session with a (new) monitor.
  110. */
  111. static int __open_session(struct ceph_mon_client *monc)
  112. {
  113. char r;
  114. int ret;
  115. if (monc->cur_mon < 0) {
  116. get_random_bytes(&r, 1);
  117. monc->cur_mon = r % monc->monmap->num_mon;
  118. dout("open_session num=%d r=%d -> mon%d\n",
  119. monc->monmap->num_mon, r, monc->cur_mon);
  120. monc->sub_sent = 0;
  121. monc->sub_renew_after = jiffies; /* i.e., expired */
  122. monc->want_next_osdmap = !!monc->want_next_osdmap;
  123. dout("open_session mon%d opening\n", monc->cur_mon);
  124. monc->con->peer_name.type = CEPH_ENTITY_TYPE_MON;
  125. monc->con->peer_name.num = cpu_to_le64(monc->cur_mon);
  126. ceph_con_open(monc->con,
  127. &monc->monmap->mon_inst[monc->cur_mon].addr);
  128. /* initiatiate authentication handshake */
  129. ret = ceph_auth_build_hello(monc->auth,
  130. monc->m_auth->front.iov_base,
  131. monc->m_auth->front_max);
  132. __send_prepared_auth_request(monc, ret);
  133. } else {
  134. dout("open_session mon%d already open\n", monc->cur_mon);
  135. }
  136. return 0;
  137. }
  138. static bool __sub_expired(struct ceph_mon_client *monc)
  139. {
  140. return time_after_eq(jiffies, monc->sub_renew_after);
  141. }
  142. /*
  143. * Reschedule delayed work timer.
  144. */
  145. static void __schedule_delayed(struct ceph_mon_client *monc)
  146. {
  147. unsigned delay;
  148. if (monc->cur_mon < 0 || __sub_expired(monc))
  149. delay = 10 * HZ;
  150. else
  151. delay = 20 * HZ;
  152. dout("__schedule_delayed after %u\n", delay);
  153. schedule_delayed_work(&monc->delayed_work, delay);
  154. }
  155. /*
  156. * Send subscribe request for mdsmap and/or osdmap.
  157. */
  158. static void __send_subscribe(struct ceph_mon_client *monc)
  159. {
  160. dout("__send_subscribe sub_sent=%u exp=%u want_osd=%d\n",
  161. (unsigned)monc->sub_sent, __sub_expired(monc),
  162. monc->want_next_osdmap);
  163. if ((__sub_expired(monc) && !monc->sub_sent) ||
  164. monc->want_next_osdmap == 1) {
  165. struct ceph_msg *msg = monc->m_subscribe;
  166. struct ceph_mon_subscribe_item *i;
  167. void *p, *end;
  168. p = msg->front.iov_base;
  169. end = p + msg->front_max;
  170. dout("__send_subscribe to 'mdsmap' %u+\n",
  171. (unsigned)monc->have_mdsmap);
  172. if (monc->want_next_osdmap) {
  173. dout("__send_subscribe to 'osdmap' %u\n",
  174. (unsigned)monc->have_osdmap);
  175. ceph_encode_32(&p, 3);
  176. ceph_encode_string(&p, end, "osdmap", 6);
  177. i = p;
  178. i->have = cpu_to_le64(monc->have_osdmap);
  179. i->onetime = 1;
  180. p += sizeof(*i);
  181. monc->want_next_osdmap = 2; /* requested */
  182. } else {
  183. ceph_encode_32(&p, 2);
  184. }
  185. ceph_encode_string(&p, end, "mdsmap", 6);
  186. i = p;
  187. i->have = cpu_to_le64(monc->have_mdsmap);
  188. i->onetime = 0;
  189. p += sizeof(*i);
  190. ceph_encode_string(&p, end, "monmap", 6);
  191. i = p;
  192. i->have = 0;
  193. i->onetime = 0;
  194. p += sizeof(*i);
  195. msg->front.iov_len = p - msg->front.iov_base;
  196. msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
  197. ceph_con_revoke(monc->con, msg);
  198. ceph_con_send(monc->con, ceph_msg_get(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_all(&client->auth_wq);
  304. }
  305. /*
  306. * statfs
  307. */
  308. static struct ceph_mon_generic_request *__lookup_generic_req(
  309. struct ceph_mon_client *monc, u64 tid)
  310. {
  311. struct ceph_mon_generic_request *req;
  312. struct rb_node *n = monc->generic_request_tree.rb_node;
  313. while (n) {
  314. req = rb_entry(n, struct ceph_mon_generic_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_generic_request(struct ceph_mon_client *monc,
  325. struct ceph_mon_generic_request *new)
  326. {
  327. struct rb_node **p = &monc->generic_request_tree.rb_node;
  328. struct rb_node *parent = NULL;
  329. struct ceph_mon_generic_request *req = NULL;
  330. while (*p) {
  331. parent = *p;
  332. req = rb_entry(parent, struct ceph_mon_generic_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->generic_request_tree);
  342. }
  343. static void release_generic_request(struct kref *kref)
  344. {
  345. struct ceph_mon_generic_request *req =
  346. container_of(kref, struct ceph_mon_generic_request, kref);
  347. if (req->reply)
  348. ceph_msg_put(req->reply);
  349. if (req->request)
  350. ceph_msg_put(req->request);
  351. kfree(req);
  352. }
  353. static void put_generic_request(struct ceph_mon_generic_request *req)
  354. {
  355. kref_put(&req->kref, release_generic_request);
  356. }
  357. static void get_generic_request(struct ceph_mon_generic_request *req)
  358. {
  359. kref_get(&req->kref);
  360. }
  361. static struct ceph_msg *get_generic_reply(struct ceph_connection *con,
  362. struct ceph_msg_header *hdr,
  363. int *skip)
  364. {
  365. struct ceph_mon_client *monc = con->private;
  366. struct ceph_mon_generic_request *req;
  367. u64 tid = le64_to_cpu(hdr->tid);
  368. struct ceph_msg *m;
  369. mutex_lock(&monc->mutex);
  370. req = __lookup_generic_req(monc, tid);
  371. if (!req) {
  372. dout("get_generic_reply %lld dne\n", tid);
  373. *skip = 1;
  374. m = NULL;
  375. } else {
  376. dout("get_generic_reply %lld got %p\n", tid, req->reply);
  377. m = ceph_msg_get(req->reply);
  378. /*
  379. * we don't need to track the connection reading into
  380. * this reply because we only have one open connection
  381. * at a time, ever.
  382. */
  383. }
  384. mutex_unlock(&monc->mutex);
  385. return m;
  386. }
  387. static void handle_statfs_reply(struct ceph_mon_client *monc,
  388. struct ceph_msg *msg)
  389. {
  390. struct ceph_mon_generic_request *req;
  391. struct ceph_mon_statfs_reply *reply = msg->front.iov_base;
  392. u64 tid = le64_to_cpu(msg->hdr.tid);
  393. if (msg->front.iov_len != sizeof(*reply))
  394. goto bad;
  395. dout("handle_statfs_reply %p tid %llu\n", msg, tid);
  396. mutex_lock(&monc->mutex);
  397. req = __lookup_generic_req(monc, tid);
  398. if (req) {
  399. *(struct ceph_statfs *)req->buf = reply->st;
  400. req->result = 0;
  401. get_generic_request(req);
  402. }
  403. mutex_unlock(&monc->mutex);
  404. if (req) {
  405. complete_all(&req->completion);
  406. put_generic_request(req);
  407. }
  408. return;
  409. bad:
  410. pr_err("corrupt generic reply, no tid\n");
  411. ceph_msg_dump(msg);
  412. }
  413. /*
  414. * Do a synchronous statfs().
  415. */
  416. int ceph_monc_do_statfs(struct ceph_mon_client *monc, struct ceph_statfs *buf)
  417. {
  418. struct ceph_mon_generic_request *req;
  419. struct ceph_mon_statfs *h;
  420. int err;
  421. req = kzalloc(sizeof(*req), GFP_NOFS);
  422. if (!req)
  423. return -ENOMEM;
  424. kref_init(&req->kref);
  425. req->buf = buf;
  426. init_completion(&req->completion);
  427. err = -ENOMEM;
  428. req->request = ceph_msg_new(CEPH_MSG_STATFS, sizeof(*h), GFP_NOFS);
  429. if (!req->request)
  430. goto out;
  431. req->reply = ceph_msg_new(CEPH_MSG_STATFS_REPLY, 1024, GFP_NOFS);
  432. if (!req->reply)
  433. goto out;
  434. /* fill out request */
  435. h = req->request->front.iov_base;
  436. h->monhdr.have_version = 0;
  437. h->monhdr.session_mon = cpu_to_le16(-1);
  438. h->monhdr.session_mon_tid = 0;
  439. h->fsid = monc->monmap->fsid;
  440. /* register request */
  441. mutex_lock(&monc->mutex);
  442. req->tid = ++monc->last_tid;
  443. req->request->hdr.tid = cpu_to_le64(req->tid);
  444. __insert_generic_request(monc, req);
  445. monc->num_generic_requests++;
  446. mutex_unlock(&monc->mutex);
  447. /* send request and wait */
  448. ceph_con_send(monc->con, ceph_msg_get(req->request));
  449. err = wait_for_completion_interruptible(&req->completion);
  450. mutex_lock(&monc->mutex);
  451. rb_erase(&req->node, &monc->generic_request_tree);
  452. monc->num_generic_requests--;
  453. mutex_unlock(&monc->mutex);
  454. if (!err)
  455. err = req->result;
  456. out:
  457. kref_put(&req->kref, release_generic_request);
  458. return err;
  459. }
  460. /*
  461. * Resend pending statfs requests.
  462. */
  463. static void __resend_generic_request(struct ceph_mon_client *monc)
  464. {
  465. struct ceph_mon_generic_request *req;
  466. struct rb_node *p;
  467. for (p = rb_first(&monc->generic_request_tree); p; p = rb_next(p)) {
  468. req = rb_entry(p, struct ceph_mon_generic_request, node);
  469. ceph_con_revoke(monc->con, req->request);
  470. ceph_con_send(monc->con, ceph_msg_get(req->request));
  471. }
  472. }
  473. /*
  474. * Delayed work. If we haven't mounted yet, retry. Otherwise,
  475. * renew/retry subscription as needed (in case it is timing out, or we
  476. * got an ENOMEM). And keep the monitor connection alive.
  477. */
  478. static void delayed_work(struct work_struct *work)
  479. {
  480. struct ceph_mon_client *monc =
  481. container_of(work, struct ceph_mon_client, delayed_work.work);
  482. dout("monc delayed_work\n");
  483. mutex_lock(&monc->mutex);
  484. if (monc->hunting) {
  485. __close_session(monc);
  486. __open_session(monc); /* continue hunting */
  487. } else {
  488. ceph_con_keepalive(monc->con);
  489. __validate_auth(monc);
  490. if (monc->auth->ops->is_authenticated(monc->auth))
  491. __send_subscribe(monc);
  492. }
  493. __schedule_delayed(monc);
  494. mutex_unlock(&monc->mutex);
  495. }
  496. /*
  497. * On startup, we build a temporary monmap populated with the IPs
  498. * provided by mount(2).
  499. */
  500. static int build_initial_monmap(struct ceph_mon_client *monc)
  501. {
  502. struct ceph_mount_args *args = monc->client->mount_args;
  503. struct ceph_entity_addr *mon_addr = args->mon_addr;
  504. int num_mon = args->num_mon;
  505. int i;
  506. /* build initial monmap */
  507. monc->monmap = kzalloc(sizeof(*monc->monmap) +
  508. num_mon*sizeof(monc->monmap->mon_inst[0]),
  509. GFP_KERNEL);
  510. if (!monc->monmap)
  511. return -ENOMEM;
  512. for (i = 0; i < num_mon; i++) {
  513. monc->monmap->mon_inst[i].addr = mon_addr[i];
  514. monc->monmap->mon_inst[i].addr.nonce = 0;
  515. monc->monmap->mon_inst[i].name.type =
  516. CEPH_ENTITY_TYPE_MON;
  517. monc->monmap->mon_inst[i].name.num = cpu_to_le64(i);
  518. }
  519. monc->monmap->num_mon = num_mon;
  520. monc->have_fsid = false;
  521. /* release addr memory */
  522. kfree(args->mon_addr);
  523. args->mon_addr = NULL;
  524. args->num_mon = 0;
  525. return 0;
  526. }
  527. int ceph_monc_init(struct ceph_mon_client *monc, struct ceph_client *cl)
  528. {
  529. int err = 0;
  530. dout("init\n");
  531. memset(monc, 0, sizeof(*monc));
  532. monc->client = cl;
  533. monc->monmap = NULL;
  534. mutex_init(&monc->mutex);
  535. err = build_initial_monmap(monc);
  536. if (err)
  537. goto out;
  538. monc->con = NULL;
  539. /* authentication */
  540. monc->auth = ceph_auth_init(cl->mount_args->name,
  541. cl->mount_args->secret);
  542. if (IS_ERR(monc->auth))
  543. return PTR_ERR(monc->auth);
  544. monc->auth->want_keys =
  545. CEPH_ENTITY_TYPE_AUTH | CEPH_ENTITY_TYPE_MON |
  546. CEPH_ENTITY_TYPE_OSD | CEPH_ENTITY_TYPE_MDS;
  547. /* msgs */
  548. err = -ENOMEM;
  549. monc->m_subscribe_ack = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE_ACK,
  550. sizeof(struct ceph_mon_subscribe_ack),
  551. GFP_NOFS);
  552. if (!monc->m_subscribe_ack)
  553. goto out_monmap;
  554. monc->m_subscribe = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE, 96, GFP_NOFS);
  555. if (!monc->m_subscribe)
  556. goto out_subscribe_ack;
  557. monc->m_auth_reply = ceph_msg_new(CEPH_MSG_AUTH_REPLY, 4096, GFP_NOFS);
  558. if (!monc->m_auth_reply)
  559. goto out_subscribe;
  560. monc->m_auth = ceph_msg_new(CEPH_MSG_AUTH, 4096, GFP_NOFS);
  561. monc->pending_auth = 0;
  562. if (!monc->m_auth)
  563. goto out_auth_reply;
  564. monc->cur_mon = -1;
  565. monc->hunting = true;
  566. monc->sub_renew_after = jiffies;
  567. monc->sub_sent = 0;
  568. INIT_DELAYED_WORK(&monc->delayed_work, delayed_work);
  569. monc->generic_request_tree = RB_ROOT;
  570. monc->num_generic_requests = 0;
  571. monc->last_tid = 0;
  572. monc->have_mdsmap = 0;
  573. monc->have_osdmap = 0;
  574. monc->want_next_osdmap = 1;
  575. return 0;
  576. out_auth_reply:
  577. ceph_msg_put(monc->m_auth_reply);
  578. out_subscribe:
  579. ceph_msg_put(monc->m_subscribe);
  580. out_subscribe_ack:
  581. ceph_msg_put(monc->m_subscribe_ack);
  582. out_monmap:
  583. kfree(monc->monmap);
  584. out:
  585. return err;
  586. }
  587. void ceph_monc_stop(struct ceph_mon_client *monc)
  588. {
  589. dout("stop\n");
  590. cancel_delayed_work_sync(&monc->delayed_work);
  591. mutex_lock(&monc->mutex);
  592. __close_session(monc);
  593. if (monc->con) {
  594. monc->con->private = NULL;
  595. monc->con->ops->put(monc->con);
  596. monc->con = NULL;
  597. }
  598. mutex_unlock(&monc->mutex);
  599. ceph_auth_destroy(monc->auth);
  600. ceph_msg_put(monc->m_auth);
  601. ceph_msg_put(monc->m_auth_reply);
  602. ceph_msg_put(monc->m_subscribe);
  603. ceph_msg_put(monc->m_subscribe_ack);
  604. kfree(monc->monmap);
  605. }
  606. static void handle_auth_reply(struct ceph_mon_client *monc,
  607. struct ceph_msg *msg)
  608. {
  609. int ret;
  610. int was_auth = 0;
  611. mutex_lock(&monc->mutex);
  612. if (monc->auth->ops)
  613. was_auth = monc->auth->ops->is_authenticated(monc->auth);
  614. monc->pending_auth = 0;
  615. ret = ceph_handle_auth_reply(monc->auth, msg->front.iov_base,
  616. msg->front.iov_len,
  617. monc->m_auth->front.iov_base,
  618. monc->m_auth->front_max);
  619. if (ret < 0) {
  620. monc->client->auth_err = ret;
  621. wake_up_all(&monc->client->auth_wq);
  622. } else if (ret > 0) {
  623. __send_prepared_auth_request(monc, ret);
  624. } else if (!was_auth && monc->auth->ops->is_authenticated(monc->auth)) {
  625. dout("authenticated, starting session\n");
  626. monc->client->msgr->inst.name.type = CEPH_ENTITY_TYPE_CLIENT;
  627. monc->client->msgr->inst.name.num =
  628. cpu_to_le64(monc->auth->global_id);
  629. __send_subscribe(monc);
  630. __resend_generic_request(monc);
  631. }
  632. mutex_unlock(&monc->mutex);
  633. }
  634. static int __validate_auth(struct ceph_mon_client *monc)
  635. {
  636. int ret;
  637. if (monc->pending_auth)
  638. return 0;
  639. ret = ceph_build_auth(monc->auth, monc->m_auth->front.iov_base,
  640. monc->m_auth->front_max);
  641. if (ret <= 0)
  642. return ret; /* either an error, or no need to authenticate */
  643. __send_prepared_auth_request(monc, ret);
  644. return 0;
  645. }
  646. int ceph_monc_validate_auth(struct ceph_mon_client *monc)
  647. {
  648. int ret;
  649. mutex_lock(&monc->mutex);
  650. ret = __validate_auth(monc);
  651. mutex_unlock(&monc->mutex);
  652. return ret;
  653. }
  654. /*
  655. * handle incoming message
  656. */
  657. static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
  658. {
  659. struct ceph_mon_client *monc = con->private;
  660. int type = le16_to_cpu(msg->hdr.type);
  661. if (!monc)
  662. return;
  663. switch (type) {
  664. case CEPH_MSG_AUTH_REPLY:
  665. handle_auth_reply(monc, msg);
  666. break;
  667. case CEPH_MSG_MON_SUBSCRIBE_ACK:
  668. handle_subscribe_ack(monc, msg);
  669. break;
  670. case CEPH_MSG_STATFS_REPLY:
  671. handle_statfs_reply(monc, msg);
  672. break;
  673. case CEPH_MSG_MON_MAP:
  674. ceph_monc_handle_map(monc, msg);
  675. break;
  676. case CEPH_MSG_MDS_MAP:
  677. ceph_mdsc_handle_map(&monc->client->mdsc, msg);
  678. break;
  679. case CEPH_MSG_OSD_MAP:
  680. ceph_osdc_handle_map(&monc->client->osdc, msg);
  681. break;
  682. default:
  683. pr_err("received unknown message type %d %s\n", type,
  684. ceph_msg_type_name(type));
  685. }
  686. ceph_msg_put(msg);
  687. }
  688. /*
  689. * Allocate memory for incoming message
  690. */
  691. static struct ceph_msg *mon_alloc_msg(struct ceph_connection *con,
  692. struct ceph_msg_header *hdr,
  693. int *skip)
  694. {
  695. struct ceph_mon_client *monc = con->private;
  696. int type = le16_to_cpu(hdr->type);
  697. int front_len = le32_to_cpu(hdr->front_len);
  698. struct ceph_msg *m = NULL;
  699. *skip = 0;
  700. switch (type) {
  701. case CEPH_MSG_MON_SUBSCRIBE_ACK:
  702. m = ceph_msg_get(monc->m_subscribe_ack);
  703. break;
  704. case CEPH_MSG_STATFS_REPLY:
  705. return get_generic_reply(con, hdr, skip);
  706. case CEPH_MSG_AUTH_REPLY:
  707. m = ceph_msg_get(monc->m_auth_reply);
  708. break;
  709. case CEPH_MSG_MON_MAP:
  710. case CEPH_MSG_MDS_MAP:
  711. case CEPH_MSG_OSD_MAP:
  712. m = ceph_msg_new(type, front_len, GFP_NOFS);
  713. break;
  714. }
  715. if (!m) {
  716. pr_info("alloc_msg unknown type %d\n", type);
  717. *skip = 1;
  718. }
  719. return m;
  720. }
  721. /*
  722. * If the monitor connection resets, pick a new monitor and resubmit
  723. * any pending requests.
  724. */
  725. static void mon_fault(struct ceph_connection *con)
  726. {
  727. struct ceph_mon_client *monc = con->private;
  728. if (!monc)
  729. return;
  730. dout("mon_fault\n");
  731. mutex_lock(&monc->mutex);
  732. if (!con->private)
  733. goto out;
  734. if (monc->con && !monc->hunting)
  735. pr_info("mon%d %s session lost, "
  736. "hunting for new mon\n", monc->cur_mon,
  737. pr_addr(&monc->con->peer_addr.in_addr));
  738. __close_session(monc);
  739. if (!monc->hunting) {
  740. /* start hunting */
  741. monc->hunting = true;
  742. __open_session(monc);
  743. } else {
  744. /* already hunting, let's wait a bit */
  745. __schedule_delayed(monc);
  746. }
  747. out:
  748. mutex_unlock(&monc->mutex);
  749. }
  750. static const struct ceph_connection_operations mon_con_ops = {
  751. .get = ceph_con_get,
  752. .put = ceph_con_put,
  753. .dispatch = dispatch,
  754. .fault = mon_fault,
  755. .alloc_msg = mon_alloc_msg,
  756. };