mon_client.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  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. * generic requests (e.g., statfs, poolop)
  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 int do_generic_request(struct ceph_mon_client *monc,
  388. struct ceph_mon_generic_request *req)
  389. {
  390. int err;
  391. /* register request */
  392. mutex_lock(&monc->mutex);
  393. req->tid = ++monc->last_tid;
  394. req->request->hdr.tid = cpu_to_le64(req->tid);
  395. __insert_generic_request(monc, req);
  396. monc->num_generic_requests++;
  397. ceph_con_send(monc->con, ceph_msg_get(req->request));
  398. mutex_unlock(&monc->mutex);
  399. err = wait_for_completion_interruptible(&req->completion);
  400. mutex_lock(&monc->mutex);
  401. rb_erase(&req->node, &monc->generic_request_tree);
  402. monc->num_generic_requests--;
  403. mutex_unlock(&monc->mutex);
  404. if (!err)
  405. err = req->result;
  406. return err;
  407. }
  408. /*
  409. * statfs
  410. */
  411. static void handle_statfs_reply(struct ceph_mon_client *monc,
  412. struct ceph_msg *msg)
  413. {
  414. struct ceph_mon_generic_request *req;
  415. struct ceph_mon_statfs_reply *reply = msg->front.iov_base;
  416. u64 tid = le64_to_cpu(msg->hdr.tid);
  417. if (msg->front.iov_len != sizeof(*reply))
  418. goto bad;
  419. dout("handle_statfs_reply %p tid %llu\n", msg, tid);
  420. mutex_lock(&monc->mutex);
  421. req = __lookup_generic_req(monc, tid);
  422. if (req) {
  423. *(struct ceph_statfs *)req->buf = reply->st;
  424. req->result = 0;
  425. get_generic_request(req);
  426. }
  427. mutex_unlock(&monc->mutex);
  428. if (req) {
  429. complete_all(&req->completion);
  430. put_generic_request(req);
  431. }
  432. return;
  433. bad:
  434. pr_err("corrupt generic reply, tid %llu\n", tid);
  435. ceph_msg_dump(msg);
  436. }
  437. /*
  438. * Do a synchronous statfs().
  439. */
  440. int ceph_monc_do_statfs(struct ceph_mon_client *monc, struct ceph_statfs *buf)
  441. {
  442. struct ceph_mon_generic_request *req;
  443. struct ceph_mon_statfs *h;
  444. int err;
  445. req = kzalloc(sizeof(*req), GFP_NOFS);
  446. if (!req)
  447. return -ENOMEM;
  448. kref_init(&req->kref);
  449. req->buf = buf;
  450. req->buf_len = sizeof(*buf);
  451. init_completion(&req->completion);
  452. err = -ENOMEM;
  453. req->request = ceph_msg_new(CEPH_MSG_STATFS, sizeof(*h), GFP_NOFS);
  454. if (!req->request)
  455. goto out;
  456. req->reply = ceph_msg_new(CEPH_MSG_STATFS_REPLY, 1024, GFP_NOFS);
  457. if (!req->reply)
  458. goto out;
  459. /* fill out request */
  460. h = req->request->front.iov_base;
  461. h->monhdr.have_version = 0;
  462. h->monhdr.session_mon = cpu_to_le16(-1);
  463. h->monhdr.session_mon_tid = 0;
  464. h->fsid = monc->monmap->fsid;
  465. err = do_generic_request(monc, req);
  466. out:
  467. kref_put(&req->kref, release_generic_request);
  468. return err;
  469. }
  470. /*
  471. * pool ops
  472. */
  473. static int get_poolop_reply_buf(const char *src, size_t src_len,
  474. char *dst, size_t dst_len)
  475. {
  476. u32 buf_len;
  477. if (src_len != sizeof(u32) + dst_len)
  478. return -EINVAL;
  479. buf_len = le32_to_cpu(*(u32 *)src);
  480. if (buf_len != dst_len)
  481. return -EINVAL;
  482. memcpy(dst, src + sizeof(u32), dst_len);
  483. return 0;
  484. }
  485. static void handle_poolop_reply(struct ceph_mon_client *monc,
  486. struct ceph_msg *msg)
  487. {
  488. struct ceph_mon_generic_request *req;
  489. struct ceph_mon_poolop_reply *reply = msg->front.iov_base;
  490. u64 tid = le64_to_cpu(msg->hdr.tid);
  491. if (msg->front.iov_len < sizeof(*reply))
  492. goto bad;
  493. dout("handle_poolop_reply %p tid %llu\n", msg, tid);
  494. mutex_lock(&monc->mutex);
  495. req = __lookup_generic_req(monc, tid);
  496. if (req) {
  497. if (req->buf_len &&
  498. get_poolop_reply_buf(msg->front.iov_base + sizeof(*reply),
  499. msg->front.iov_len - sizeof(*reply),
  500. req->buf, req->buf_len) < 0) {
  501. mutex_unlock(&monc->mutex);
  502. goto bad;
  503. }
  504. req->result = le32_to_cpu(reply->reply_code);
  505. get_generic_request(req);
  506. }
  507. mutex_unlock(&monc->mutex);
  508. if (req) {
  509. complete(&req->completion);
  510. put_generic_request(req);
  511. }
  512. return;
  513. bad:
  514. pr_err("corrupt generic reply, tid %llu\n", tid);
  515. ceph_msg_dump(msg);
  516. }
  517. /*
  518. * Do a synchronous pool op.
  519. */
  520. int ceph_monc_do_poolop(struct ceph_mon_client *monc, u32 op,
  521. u32 pool, u64 snapid,
  522. char *buf, int len)
  523. {
  524. struct ceph_mon_generic_request *req;
  525. struct ceph_mon_poolop *h;
  526. int err;
  527. req = kzalloc(sizeof(*req), GFP_NOFS);
  528. if (!req)
  529. return -ENOMEM;
  530. kref_init(&req->kref);
  531. req->buf = buf;
  532. req->buf_len = len;
  533. init_completion(&req->completion);
  534. err = -ENOMEM;
  535. req->request = ceph_msg_new(CEPH_MSG_POOLOP, sizeof(*h), GFP_NOFS);
  536. if (!req->request)
  537. goto out;
  538. req->reply = ceph_msg_new(CEPH_MSG_POOLOP_REPLY, 1024, GFP_NOFS);
  539. if (!req->reply)
  540. goto out;
  541. /* fill out request */
  542. req->request->hdr.version = cpu_to_le16(2);
  543. h = req->request->front.iov_base;
  544. h->monhdr.have_version = 0;
  545. h->monhdr.session_mon = cpu_to_le16(-1);
  546. h->monhdr.session_mon_tid = 0;
  547. h->fsid = monc->monmap->fsid;
  548. h->pool = cpu_to_le32(pool);
  549. h->op = cpu_to_le32(op);
  550. h->auid = 0;
  551. h->snapid = cpu_to_le64(snapid);
  552. h->name_len = 0;
  553. err = do_generic_request(monc, req);
  554. out:
  555. kref_put(&req->kref, release_generic_request);
  556. return err;
  557. }
  558. int ceph_monc_create_snapid(struct ceph_mon_client *monc,
  559. u32 pool, u64 *snapid)
  560. {
  561. return ceph_monc_do_poolop(monc, POOL_OP_CREATE_UNMANAGED_SNAP,
  562. pool, 0, (char *)snapid, sizeof(*snapid));
  563. }
  564. int ceph_monc_delete_snapid(struct ceph_mon_client *monc,
  565. u32 pool, u64 snapid)
  566. {
  567. return ceph_monc_do_poolop(monc, POOL_OP_CREATE_UNMANAGED_SNAP,
  568. pool, snapid, 0, 0);
  569. }
  570. /*
  571. * Resend pending generic requests.
  572. */
  573. static void __resend_generic_request(struct ceph_mon_client *monc)
  574. {
  575. struct ceph_mon_generic_request *req;
  576. struct rb_node *p;
  577. for (p = rb_first(&monc->generic_request_tree); p; p = rb_next(p)) {
  578. req = rb_entry(p, struct ceph_mon_generic_request, node);
  579. ceph_con_revoke(monc->con, req->request);
  580. ceph_con_send(monc->con, ceph_msg_get(req->request));
  581. }
  582. }
  583. /*
  584. * Delayed work. If we haven't mounted yet, retry. Otherwise,
  585. * renew/retry subscription as needed (in case it is timing out, or we
  586. * got an ENOMEM). And keep the monitor connection alive.
  587. */
  588. static void delayed_work(struct work_struct *work)
  589. {
  590. struct ceph_mon_client *monc =
  591. container_of(work, struct ceph_mon_client, delayed_work.work);
  592. dout("monc delayed_work\n");
  593. mutex_lock(&monc->mutex);
  594. if (monc->hunting) {
  595. __close_session(monc);
  596. __open_session(monc); /* continue hunting */
  597. } else {
  598. ceph_con_keepalive(monc->con);
  599. __validate_auth(monc);
  600. if (monc->auth->ops->is_authenticated(monc->auth))
  601. __send_subscribe(monc);
  602. }
  603. __schedule_delayed(monc);
  604. mutex_unlock(&monc->mutex);
  605. }
  606. /*
  607. * On startup, we build a temporary monmap populated with the IPs
  608. * provided by mount(2).
  609. */
  610. static int build_initial_monmap(struct ceph_mon_client *monc)
  611. {
  612. struct ceph_mount_args *args = monc->client->mount_args;
  613. struct ceph_entity_addr *mon_addr = args->mon_addr;
  614. int num_mon = args->num_mon;
  615. int i;
  616. /* build initial monmap */
  617. monc->monmap = kzalloc(sizeof(*monc->monmap) +
  618. num_mon*sizeof(monc->monmap->mon_inst[0]),
  619. GFP_KERNEL);
  620. if (!monc->monmap)
  621. return -ENOMEM;
  622. for (i = 0; i < num_mon; i++) {
  623. monc->monmap->mon_inst[i].addr = mon_addr[i];
  624. monc->monmap->mon_inst[i].addr.nonce = 0;
  625. monc->monmap->mon_inst[i].name.type =
  626. CEPH_ENTITY_TYPE_MON;
  627. monc->monmap->mon_inst[i].name.num = cpu_to_le64(i);
  628. }
  629. monc->monmap->num_mon = num_mon;
  630. monc->have_fsid = false;
  631. /* release addr memory */
  632. kfree(args->mon_addr);
  633. args->mon_addr = NULL;
  634. args->num_mon = 0;
  635. return 0;
  636. }
  637. int ceph_monc_init(struct ceph_mon_client *monc, struct ceph_client *cl)
  638. {
  639. int err = 0;
  640. dout("init\n");
  641. memset(monc, 0, sizeof(*monc));
  642. monc->client = cl;
  643. monc->monmap = NULL;
  644. mutex_init(&monc->mutex);
  645. err = build_initial_monmap(monc);
  646. if (err)
  647. goto out;
  648. monc->con = NULL;
  649. /* authentication */
  650. monc->auth = ceph_auth_init(cl->mount_args->name,
  651. cl->mount_args->secret);
  652. if (IS_ERR(monc->auth))
  653. return PTR_ERR(monc->auth);
  654. monc->auth->want_keys =
  655. CEPH_ENTITY_TYPE_AUTH | CEPH_ENTITY_TYPE_MON |
  656. CEPH_ENTITY_TYPE_OSD | CEPH_ENTITY_TYPE_MDS;
  657. /* msgs */
  658. err = -ENOMEM;
  659. monc->m_subscribe_ack = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE_ACK,
  660. sizeof(struct ceph_mon_subscribe_ack),
  661. GFP_NOFS);
  662. if (!monc->m_subscribe_ack)
  663. goto out_monmap;
  664. monc->m_subscribe = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE, 96, GFP_NOFS);
  665. if (!monc->m_subscribe)
  666. goto out_subscribe_ack;
  667. monc->m_auth_reply = ceph_msg_new(CEPH_MSG_AUTH_REPLY, 4096, GFP_NOFS);
  668. if (!monc->m_auth_reply)
  669. goto out_subscribe;
  670. monc->m_auth = ceph_msg_new(CEPH_MSG_AUTH, 4096, GFP_NOFS);
  671. monc->pending_auth = 0;
  672. if (!monc->m_auth)
  673. goto out_auth_reply;
  674. monc->cur_mon = -1;
  675. monc->hunting = true;
  676. monc->sub_renew_after = jiffies;
  677. monc->sub_sent = 0;
  678. INIT_DELAYED_WORK(&monc->delayed_work, delayed_work);
  679. monc->generic_request_tree = RB_ROOT;
  680. monc->num_generic_requests = 0;
  681. monc->last_tid = 0;
  682. monc->have_mdsmap = 0;
  683. monc->have_osdmap = 0;
  684. monc->want_next_osdmap = 1;
  685. return 0;
  686. out_auth_reply:
  687. ceph_msg_put(monc->m_auth_reply);
  688. out_subscribe:
  689. ceph_msg_put(monc->m_subscribe);
  690. out_subscribe_ack:
  691. ceph_msg_put(monc->m_subscribe_ack);
  692. out_monmap:
  693. kfree(monc->monmap);
  694. out:
  695. return err;
  696. }
  697. void ceph_monc_stop(struct ceph_mon_client *monc)
  698. {
  699. dout("stop\n");
  700. cancel_delayed_work_sync(&monc->delayed_work);
  701. mutex_lock(&monc->mutex);
  702. __close_session(monc);
  703. if (monc->con) {
  704. monc->con->private = NULL;
  705. monc->con->ops->put(monc->con);
  706. monc->con = NULL;
  707. }
  708. mutex_unlock(&monc->mutex);
  709. ceph_auth_destroy(monc->auth);
  710. ceph_msg_put(monc->m_auth);
  711. ceph_msg_put(monc->m_auth_reply);
  712. ceph_msg_put(monc->m_subscribe);
  713. ceph_msg_put(monc->m_subscribe_ack);
  714. kfree(monc->monmap);
  715. }
  716. static void handle_auth_reply(struct ceph_mon_client *monc,
  717. struct ceph_msg *msg)
  718. {
  719. int ret;
  720. int was_auth = 0;
  721. mutex_lock(&monc->mutex);
  722. if (monc->auth->ops)
  723. was_auth = monc->auth->ops->is_authenticated(monc->auth);
  724. monc->pending_auth = 0;
  725. ret = ceph_handle_auth_reply(monc->auth, msg->front.iov_base,
  726. msg->front.iov_len,
  727. monc->m_auth->front.iov_base,
  728. monc->m_auth->front_max);
  729. if (ret < 0) {
  730. monc->client->auth_err = ret;
  731. wake_up_all(&monc->client->auth_wq);
  732. } else if (ret > 0) {
  733. __send_prepared_auth_request(monc, ret);
  734. } else if (!was_auth && monc->auth->ops->is_authenticated(monc->auth)) {
  735. dout("authenticated, starting session\n");
  736. monc->client->msgr->inst.name.type = CEPH_ENTITY_TYPE_CLIENT;
  737. monc->client->msgr->inst.name.num =
  738. cpu_to_le64(monc->auth->global_id);
  739. __send_subscribe(monc);
  740. __resend_generic_request(monc);
  741. }
  742. mutex_unlock(&monc->mutex);
  743. }
  744. static int __validate_auth(struct ceph_mon_client *monc)
  745. {
  746. int ret;
  747. if (monc->pending_auth)
  748. return 0;
  749. ret = ceph_build_auth(monc->auth, monc->m_auth->front.iov_base,
  750. monc->m_auth->front_max);
  751. if (ret <= 0)
  752. return ret; /* either an error, or no need to authenticate */
  753. __send_prepared_auth_request(monc, ret);
  754. return 0;
  755. }
  756. int ceph_monc_validate_auth(struct ceph_mon_client *monc)
  757. {
  758. int ret;
  759. mutex_lock(&monc->mutex);
  760. ret = __validate_auth(monc);
  761. mutex_unlock(&monc->mutex);
  762. return ret;
  763. }
  764. /*
  765. * handle incoming message
  766. */
  767. static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
  768. {
  769. struct ceph_mon_client *monc = con->private;
  770. int type = le16_to_cpu(msg->hdr.type);
  771. if (!monc)
  772. return;
  773. switch (type) {
  774. case CEPH_MSG_AUTH_REPLY:
  775. handle_auth_reply(monc, msg);
  776. break;
  777. case CEPH_MSG_MON_SUBSCRIBE_ACK:
  778. handle_subscribe_ack(monc, msg);
  779. break;
  780. case CEPH_MSG_STATFS_REPLY:
  781. handle_statfs_reply(monc, msg);
  782. break;
  783. case CEPH_MSG_POOLOP_REPLY:
  784. handle_poolop_reply(monc, msg);
  785. break;
  786. case CEPH_MSG_MON_MAP:
  787. ceph_monc_handle_map(monc, msg);
  788. break;
  789. case CEPH_MSG_MDS_MAP:
  790. ceph_mdsc_handle_map(&monc->client->mdsc, msg);
  791. break;
  792. case CEPH_MSG_OSD_MAP:
  793. ceph_osdc_handle_map(&monc->client->osdc, msg);
  794. break;
  795. default:
  796. pr_err("received unknown message type %d %s\n", type,
  797. ceph_msg_type_name(type));
  798. }
  799. ceph_msg_put(msg);
  800. }
  801. /*
  802. * Allocate memory for incoming message
  803. */
  804. static struct ceph_msg *mon_alloc_msg(struct ceph_connection *con,
  805. struct ceph_msg_header *hdr,
  806. int *skip)
  807. {
  808. struct ceph_mon_client *monc = con->private;
  809. int type = le16_to_cpu(hdr->type);
  810. int front_len = le32_to_cpu(hdr->front_len);
  811. struct ceph_msg *m = NULL;
  812. *skip = 0;
  813. switch (type) {
  814. case CEPH_MSG_MON_SUBSCRIBE_ACK:
  815. m = ceph_msg_get(monc->m_subscribe_ack);
  816. break;
  817. case CEPH_MSG_POOLOP_REPLY:
  818. case CEPH_MSG_STATFS_REPLY:
  819. return get_generic_reply(con, hdr, skip);
  820. case CEPH_MSG_AUTH_REPLY:
  821. m = ceph_msg_get(monc->m_auth_reply);
  822. break;
  823. case CEPH_MSG_MON_MAP:
  824. case CEPH_MSG_MDS_MAP:
  825. case CEPH_MSG_OSD_MAP:
  826. m = ceph_msg_new(type, front_len, GFP_NOFS);
  827. break;
  828. }
  829. if (!m) {
  830. pr_info("alloc_msg unknown type %d\n", type);
  831. *skip = 1;
  832. }
  833. return m;
  834. }
  835. /*
  836. * If the monitor connection resets, pick a new monitor and resubmit
  837. * any pending requests.
  838. */
  839. static void mon_fault(struct ceph_connection *con)
  840. {
  841. struct ceph_mon_client *monc = con->private;
  842. if (!monc)
  843. return;
  844. dout("mon_fault\n");
  845. mutex_lock(&monc->mutex);
  846. if (!con->private)
  847. goto out;
  848. if (monc->con && !monc->hunting)
  849. pr_info("mon%d %s session lost, "
  850. "hunting for new mon\n", monc->cur_mon,
  851. pr_addr(&monc->con->peer_addr.in_addr));
  852. __close_session(monc);
  853. if (!monc->hunting) {
  854. /* start hunting */
  855. monc->hunting = true;
  856. __open_session(monc);
  857. } else {
  858. /* already hunting, let's wait a bit */
  859. __schedule_delayed(monc);
  860. }
  861. out:
  862. mutex_unlock(&monc->mutex);
  863. }
  864. static const struct ceph_connection_operations mon_con_ops = {
  865. .get = ceph_con_get,
  866. .put = ceph_con_put,
  867. .dispatch = dispatch,
  868. .fault = mon_fault,
  869. .alloc_msg = mon_alloc_msg,
  870. };