mon_client.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  1. #include <linux/ceph/ceph_debug.h>
  2. #include <linux/module.h>
  3. #include <linux/types.h>
  4. #include <linux/slab.h>
  5. #include <linux/random.h>
  6. #include <linux/sched.h>
  7. #include <linux/ceph/mon_client.h>
  8. #include <linux/ceph/libceph.h>
  9. #include <linux/ceph/debugfs.h>
  10. #include <linux/ceph/decode.h>
  11. #include <linux/ceph/auth.h>
  12. /*
  13. * Interact with Ceph monitor cluster. Handle requests for new map
  14. * versions, and periodically resend as needed. Also implement
  15. * statfs() and umount().
  16. *
  17. * A small cluster of Ceph "monitors" are responsible for managing critical
  18. * cluster configuration and state information. An odd number (e.g., 3, 5)
  19. * of cmon daemons use a modified version of the Paxos part-time parliament
  20. * algorithm to manage the MDS map (mds cluster membership), OSD map, and
  21. * list of clients who have mounted the file system.
  22. *
  23. * We maintain an open, active session with a monitor at all times in order to
  24. * receive timely MDSMap updates. We periodically send a keepalive byte on the
  25. * TCP socket to ensure we detect a failure. If the connection does break, we
  26. * randomly hunt for a new monitor. Once the connection is reestablished, we
  27. * resend any outstanding requests.
  28. */
  29. static const struct ceph_connection_operations mon_con_ops;
  30. static int __validate_auth(struct ceph_mon_client *monc);
  31. /*
  32. * Decode a monmap blob (e.g., during mount).
  33. */
  34. struct ceph_monmap *ceph_monmap_decode(void *p, void *end)
  35. {
  36. struct ceph_monmap *m = NULL;
  37. int i, err = -EINVAL;
  38. struct ceph_fsid fsid;
  39. u32 epoch, num_mon;
  40. u16 version;
  41. u32 len;
  42. ceph_decode_32_safe(&p, end, len, bad);
  43. ceph_decode_need(&p, end, len, bad);
  44. dout("monmap_decode %p %p len %d\n", p, end, (int)(end-p));
  45. ceph_decode_16_safe(&p, end, version, bad);
  46. ceph_decode_need(&p, end, sizeof(fsid) + 2*sizeof(u32), bad);
  47. ceph_decode_copy(&p, &fsid, sizeof(fsid));
  48. epoch = ceph_decode_32(&p);
  49. num_mon = ceph_decode_32(&p);
  50. ceph_decode_need(&p, end, num_mon*sizeof(m->mon_inst[0]), bad);
  51. if (num_mon >= CEPH_MAX_MON)
  52. goto bad;
  53. m = kmalloc(sizeof(*m) + sizeof(m->mon_inst[0])*num_mon, GFP_NOFS);
  54. if (m == NULL)
  55. return ERR_PTR(-ENOMEM);
  56. m->fsid = fsid;
  57. m->epoch = epoch;
  58. m->num_mon = num_mon;
  59. ceph_decode_copy(&p, m->mon_inst, num_mon*sizeof(m->mon_inst[0]));
  60. for (i = 0; i < num_mon; i++)
  61. ceph_decode_addr(&m->mon_inst[i].addr);
  62. dout("monmap_decode epoch %d, num_mon %d\n", m->epoch,
  63. m->num_mon);
  64. for (i = 0; i < m->num_mon; i++)
  65. dout("monmap_decode mon%d is %s\n", i,
  66. ceph_pr_addr(&m->mon_inst[i].addr.in_addr));
  67. return m;
  68. bad:
  69. dout("monmap_decode failed with %d\n", err);
  70. kfree(m);
  71. return ERR_PTR(err);
  72. }
  73. /*
  74. * return true if *addr is included in the monmap.
  75. */
  76. int ceph_monmap_contains(struct ceph_monmap *m, struct ceph_entity_addr *addr)
  77. {
  78. int i;
  79. for (i = 0; i < m->num_mon; i++)
  80. if (memcmp(addr, &m->mon_inst[i].addr, sizeof(*addr)) == 0)
  81. return 1;
  82. return 0;
  83. }
  84. /*
  85. * Send an auth request.
  86. */
  87. static void __send_prepared_auth_request(struct ceph_mon_client *monc, int len)
  88. {
  89. monc->pending_auth = 1;
  90. monc->m_auth->front.iov_len = len;
  91. monc->m_auth->hdr.front_len = cpu_to_le32(len);
  92. ceph_msg_revoke(monc->m_auth);
  93. ceph_msg_get(monc->m_auth); /* keep our ref */
  94. ceph_con_send(&monc->con, monc->m_auth);
  95. }
  96. /*
  97. * Close monitor session, if any.
  98. */
  99. static void __close_session(struct ceph_mon_client *monc)
  100. {
  101. dout("__close_session closing mon%d\n", monc->cur_mon);
  102. ceph_msg_revoke(monc->m_auth);
  103. ceph_msg_revoke_incoming(monc->m_auth_reply);
  104. ceph_msg_revoke(monc->m_subscribe);
  105. ceph_msg_revoke_incoming(monc->m_subscribe_ack);
  106. ceph_con_close(&monc->con);
  107. monc->cur_mon = -1;
  108. monc->pending_auth = 0;
  109. ceph_auth_reset(monc->auth);
  110. }
  111. /*
  112. * Open a session with a (new) monitor.
  113. */
  114. static int __open_session(struct ceph_mon_client *monc)
  115. {
  116. char r;
  117. int ret;
  118. if (monc->cur_mon < 0) {
  119. get_random_bytes(&r, 1);
  120. monc->cur_mon = r % monc->monmap->num_mon;
  121. dout("open_session num=%d r=%d -> mon%d\n",
  122. monc->monmap->num_mon, r, monc->cur_mon);
  123. monc->sub_renew_after = jiffies; /* i.e., expired */
  124. monc->sub_renew_sent = 0;
  125. dout("open_session mon%d opening\n", monc->cur_mon);
  126. ceph_con_open(&monc->con,
  127. CEPH_ENTITY_TYPE_MON, monc->cur_mon,
  128. &monc->monmap->mon_inst[monc->cur_mon].addr);
  129. /* send an initial keepalive to ensure our timestamp is
  130. * valid by the time we are in an OPENED state */
  131. ceph_con_keepalive(&monc->con);
  132. /* initiatiate authentication handshake */
  133. ret = ceph_auth_build_hello(monc->auth,
  134. monc->m_auth->front.iov_base,
  135. monc->m_auth->front_alloc_len);
  136. __send_prepared_auth_request(monc, ret);
  137. } else {
  138. dout("open_session mon%d already open\n", monc->cur_mon);
  139. }
  140. return 0;
  141. }
  142. static bool __sub_expired(struct ceph_mon_client *monc)
  143. {
  144. return time_after_eq(jiffies, monc->sub_renew_after);
  145. }
  146. /*
  147. * Reschedule delayed work timer.
  148. */
  149. static void __schedule_delayed(struct ceph_mon_client *monc)
  150. {
  151. struct ceph_options *opt = monc->client->options;
  152. unsigned long delay;
  153. if (monc->cur_mon < 0 || __sub_expired(monc)) {
  154. delay = 10 * HZ;
  155. } else {
  156. delay = 20 * HZ;
  157. if (opt->monc_ping_timeout > 0)
  158. delay = min(delay, opt->monc_ping_timeout / 3);
  159. }
  160. dout("__schedule_delayed after %lu\n", delay);
  161. schedule_delayed_work(&monc->delayed_work,
  162. round_jiffies_relative(delay));
  163. }
  164. const char *ceph_sub_str[] = {
  165. [CEPH_SUB_MDSMAP] = "mdsmap",
  166. [CEPH_SUB_MONMAP] = "monmap",
  167. [CEPH_SUB_OSDMAP] = "osdmap",
  168. };
  169. /*
  170. * Send subscribe request for one or more maps, according to
  171. * monc->subs.
  172. */
  173. static void __send_subscribe(struct ceph_mon_client *monc)
  174. {
  175. struct ceph_msg *msg = monc->m_subscribe;
  176. void *p = msg->front.iov_base;
  177. void *const end = p + msg->front_alloc_len;
  178. int num = 0;
  179. int i;
  180. dout("%s sent %lu\n", __func__, monc->sub_renew_sent);
  181. BUG_ON(monc->cur_mon < 0);
  182. if (!monc->sub_renew_sent)
  183. monc->sub_renew_sent = jiffies | 1; /* never 0 */
  184. msg->hdr.version = cpu_to_le16(2);
  185. for (i = 0; i < ARRAY_SIZE(monc->subs); i++) {
  186. if (monc->subs[i].want)
  187. num++;
  188. }
  189. BUG_ON(num < 1); /* monmap sub is always there */
  190. ceph_encode_32(&p, num);
  191. for (i = 0; i < ARRAY_SIZE(monc->subs); i++) {
  192. const char *s = ceph_sub_str[i];
  193. if (!monc->subs[i].want)
  194. continue;
  195. dout("%s %s start %llu flags 0x%x\n", __func__, s,
  196. le64_to_cpu(monc->subs[i].item.start),
  197. monc->subs[i].item.flags);
  198. ceph_encode_string(&p, end, s, strlen(s));
  199. memcpy(p, &monc->subs[i].item, sizeof(monc->subs[i].item));
  200. p += sizeof(monc->subs[i].item);
  201. }
  202. BUG_ON(p != (end - 35 - (ARRAY_SIZE(monc->subs) - num) * 19));
  203. msg->front.iov_len = p - msg->front.iov_base;
  204. msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
  205. ceph_msg_revoke(msg);
  206. ceph_con_send(&monc->con, ceph_msg_get(msg));
  207. }
  208. static void handle_subscribe_ack(struct ceph_mon_client *monc,
  209. struct ceph_msg *msg)
  210. {
  211. unsigned int seconds;
  212. struct ceph_mon_subscribe_ack *h = msg->front.iov_base;
  213. if (msg->front.iov_len < sizeof(*h))
  214. goto bad;
  215. seconds = le32_to_cpu(h->duration);
  216. mutex_lock(&monc->mutex);
  217. if (monc->sub_renew_sent) {
  218. monc->sub_renew_after = monc->sub_renew_sent +
  219. (seconds >> 1) * HZ - 1;
  220. dout("%s sent %lu duration %d renew after %lu\n", __func__,
  221. monc->sub_renew_sent, seconds, monc->sub_renew_after);
  222. monc->sub_renew_sent = 0;
  223. } else {
  224. dout("%s sent %lu renew after %lu, ignoring\n", __func__,
  225. monc->sub_renew_sent, monc->sub_renew_after);
  226. }
  227. mutex_unlock(&monc->mutex);
  228. return;
  229. bad:
  230. pr_err("got corrupt subscribe-ack msg\n");
  231. ceph_msg_dump(msg);
  232. }
  233. /*
  234. * Register interest in a map
  235. *
  236. * @sub: one of CEPH_SUB_*
  237. * @epoch: X for "every map since X", or 0 for "just the latest"
  238. */
  239. static bool __ceph_monc_want_map(struct ceph_mon_client *monc, int sub,
  240. u32 epoch, bool continuous)
  241. {
  242. __le64 start = cpu_to_le64(epoch);
  243. u8 flags = !continuous ? CEPH_SUBSCRIBE_ONETIME : 0;
  244. dout("%s %s epoch %u continuous %d\n", __func__, ceph_sub_str[sub],
  245. epoch, continuous);
  246. if (monc->subs[sub].want &&
  247. monc->subs[sub].item.start == start &&
  248. monc->subs[sub].item.flags == flags)
  249. return false;
  250. monc->subs[sub].item.start = start;
  251. monc->subs[sub].item.flags = flags;
  252. monc->subs[sub].want = true;
  253. return true;
  254. }
  255. bool ceph_monc_want_map(struct ceph_mon_client *monc, int sub, u32 epoch,
  256. bool continuous)
  257. {
  258. bool need_request;
  259. mutex_lock(&monc->mutex);
  260. need_request = __ceph_monc_want_map(monc, sub, epoch, continuous);
  261. mutex_unlock(&monc->mutex);
  262. return need_request;
  263. }
  264. EXPORT_SYMBOL(ceph_monc_want_map);
  265. /*
  266. * Keep track of which maps we have
  267. *
  268. * @sub: one of CEPH_SUB_*
  269. */
  270. static void __ceph_monc_got_map(struct ceph_mon_client *monc, int sub,
  271. u32 epoch)
  272. {
  273. dout("%s %s epoch %u\n", __func__, ceph_sub_str[sub], epoch);
  274. if (monc->subs[sub].want) {
  275. if (monc->subs[sub].item.flags & CEPH_SUBSCRIBE_ONETIME)
  276. monc->subs[sub].want = false;
  277. else
  278. monc->subs[sub].item.start = cpu_to_le64(epoch + 1);
  279. }
  280. monc->subs[sub].have = epoch;
  281. }
  282. void ceph_monc_got_map(struct ceph_mon_client *monc, int sub, u32 epoch)
  283. {
  284. mutex_lock(&monc->mutex);
  285. __ceph_monc_got_map(monc, sub, epoch);
  286. mutex_unlock(&monc->mutex);
  287. }
  288. EXPORT_SYMBOL(ceph_monc_got_map);
  289. /*
  290. * Register interest in the next osdmap
  291. */
  292. void ceph_monc_request_next_osdmap(struct ceph_mon_client *monc)
  293. {
  294. dout("%s have %u\n", __func__, monc->subs[CEPH_SUB_OSDMAP].have);
  295. mutex_lock(&monc->mutex);
  296. if (__ceph_monc_want_map(monc, CEPH_SUB_OSDMAP,
  297. monc->subs[CEPH_SUB_OSDMAP].have + 1, false))
  298. __send_subscribe(monc);
  299. mutex_unlock(&monc->mutex);
  300. }
  301. EXPORT_SYMBOL(ceph_monc_request_next_osdmap);
  302. /*
  303. * Wait for an osdmap with a given epoch.
  304. *
  305. * @epoch: epoch to wait for
  306. * @timeout: in jiffies, 0 means "wait forever"
  307. */
  308. int ceph_monc_wait_osdmap(struct ceph_mon_client *monc, u32 epoch,
  309. unsigned long timeout)
  310. {
  311. unsigned long started = jiffies;
  312. long ret;
  313. mutex_lock(&monc->mutex);
  314. while (monc->subs[CEPH_SUB_OSDMAP].have < epoch) {
  315. mutex_unlock(&monc->mutex);
  316. if (timeout && time_after_eq(jiffies, started + timeout))
  317. return -ETIMEDOUT;
  318. ret = wait_event_interruptible_timeout(monc->client->auth_wq,
  319. monc->subs[CEPH_SUB_OSDMAP].have >= epoch,
  320. ceph_timeout_jiffies(timeout));
  321. if (ret < 0)
  322. return ret;
  323. mutex_lock(&monc->mutex);
  324. }
  325. mutex_unlock(&monc->mutex);
  326. return 0;
  327. }
  328. EXPORT_SYMBOL(ceph_monc_wait_osdmap);
  329. /*
  330. * Open a session with a random monitor. Request monmap and osdmap,
  331. * which are waited upon in __ceph_open_session().
  332. */
  333. int ceph_monc_open_session(struct ceph_mon_client *monc)
  334. {
  335. mutex_lock(&monc->mutex);
  336. __ceph_monc_want_map(monc, CEPH_SUB_MONMAP, 0, true);
  337. __ceph_monc_want_map(monc, CEPH_SUB_OSDMAP, 0, false);
  338. __open_session(monc);
  339. __schedule_delayed(monc);
  340. mutex_unlock(&monc->mutex);
  341. return 0;
  342. }
  343. EXPORT_SYMBOL(ceph_monc_open_session);
  344. static void ceph_monc_handle_map(struct ceph_mon_client *monc,
  345. struct ceph_msg *msg)
  346. {
  347. struct ceph_client *client = monc->client;
  348. struct ceph_monmap *monmap = NULL, *old = monc->monmap;
  349. void *p, *end;
  350. mutex_lock(&monc->mutex);
  351. dout("handle_monmap\n");
  352. p = msg->front.iov_base;
  353. end = p + msg->front.iov_len;
  354. monmap = ceph_monmap_decode(p, end);
  355. if (IS_ERR(monmap)) {
  356. pr_err("problem decoding monmap, %d\n",
  357. (int)PTR_ERR(monmap));
  358. goto out;
  359. }
  360. if (ceph_check_fsid(monc->client, &monmap->fsid) < 0) {
  361. kfree(monmap);
  362. goto out;
  363. }
  364. client->monc.monmap = monmap;
  365. kfree(old);
  366. __ceph_monc_got_map(monc, CEPH_SUB_MONMAP, monc->monmap->epoch);
  367. client->have_fsid = true;
  368. out:
  369. mutex_unlock(&monc->mutex);
  370. wake_up_all(&client->auth_wq);
  371. }
  372. /*
  373. * generic requests (currently statfs, mon_get_version)
  374. */
  375. static struct ceph_mon_generic_request *__lookup_generic_req(
  376. struct ceph_mon_client *monc, u64 tid)
  377. {
  378. struct ceph_mon_generic_request *req;
  379. struct rb_node *n = monc->generic_request_tree.rb_node;
  380. while (n) {
  381. req = rb_entry(n, struct ceph_mon_generic_request, node);
  382. if (tid < req->tid)
  383. n = n->rb_left;
  384. else if (tid > req->tid)
  385. n = n->rb_right;
  386. else
  387. return req;
  388. }
  389. return NULL;
  390. }
  391. static void __insert_generic_request(struct ceph_mon_client *monc,
  392. struct ceph_mon_generic_request *new)
  393. {
  394. struct rb_node **p = &monc->generic_request_tree.rb_node;
  395. struct rb_node *parent = NULL;
  396. struct ceph_mon_generic_request *req = NULL;
  397. while (*p) {
  398. parent = *p;
  399. req = rb_entry(parent, struct ceph_mon_generic_request, node);
  400. if (new->tid < req->tid)
  401. p = &(*p)->rb_left;
  402. else if (new->tid > req->tid)
  403. p = &(*p)->rb_right;
  404. else
  405. BUG();
  406. }
  407. rb_link_node(&new->node, parent, p);
  408. rb_insert_color(&new->node, &monc->generic_request_tree);
  409. }
  410. static void release_generic_request(struct kref *kref)
  411. {
  412. struct ceph_mon_generic_request *req =
  413. container_of(kref, struct ceph_mon_generic_request, kref);
  414. if (req->reply)
  415. ceph_msg_put(req->reply);
  416. if (req->request)
  417. ceph_msg_put(req->request);
  418. kfree(req);
  419. }
  420. static void put_generic_request(struct ceph_mon_generic_request *req)
  421. {
  422. kref_put(&req->kref, release_generic_request);
  423. }
  424. static void get_generic_request(struct ceph_mon_generic_request *req)
  425. {
  426. kref_get(&req->kref);
  427. }
  428. static struct ceph_msg *get_generic_reply(struct ceph_connection *con,
  429. struct ceph_msg_header *hdr,
  430. int *skip)
  431. {
  432. struct ceph_mon_client *monc = con->private;
  433. struct ceph_mon_generic_request *req;
  434. u64 tid = le64_to_cpu(hdr->tid);
  435. struct ceph_msg *m;
  436. mutex_lock(&monc->mutex);
  437. req = __lookup_generic_req(monc, tid);
  438. if (!req) {
  439. dout("get_generic_reply %lld dne\n", tid);
  440. *skip = 1;
  441. m = NULL;
  442. } else {
  443. dout("get_generic_reply %lld got %p\n", tid, req->reply);
  444. *skip = 0;
  445. m = ceph_msg_get(req->reply);
  446. /*
  447. * we don't need to track the connection reading into
  448. * this reply because we only have one open connection
  449. * at a time, ever.
  450. */
  451. }
  452. mutex_unlock(&monc->mutex);
  453. return m;
  454. }
  455. static int __do_generic_request(struct ceph_mon_client *monc, u64 tid,
  456. struct ceph_mon_generic_request *req)
  457. {
  458. int err;
  459. /* register request */
  460. req->tid = tid != 0 ? tid : ++monc->last_tid;
  461. req->request->hdr.tid = cpu_to_le64(req->tid);
  462. __insert_generic_request(monc, req);
  463. monc->num_generic_requests++;
  464. ceph_con_send(&monc->con, ceph_msg_get(req->request));
  465. mutex_unlock(&monc->mutex);
  466. err = wait_for_completion_interruptible(&req->completion);
  467. mutex_lock(&monc->mutex);
  468. rb_erase(&req->node, &monc->generic_request_tree);
  469. monc->num_generic_requests--;
  470. if (!err)
  471. err = req->result;
  472. return err;
  473. }
  474. static int do_generic_request(struct ceph_mon_client *monc,
  475. struct ceph_mon_generic_request *req)
  476. {
  477. int err;
  478. mutex_lock(&monc->mutex);
  479. err = __do_generic_request(monc, 0, req);
  480. mutex_unlock(&monc->mutex);
  481. return err;
  482. }
  483. /*
  484. * statfs
  485. */
  486. static void handle_statfs_reply(struct ceph_mon_client *monc,
  487. struct ceph_msg *msg)
  488. {
  489. struct ceph_mon_generic_request *req;
  490. struct ceph_mon_statfs_reply *reply = msg->front.iov_base;
  491. u64 tid = le64_to_cpu(msg->hdr.tid);
  492. if (msg->front.iov_len != sizeof(*reply))
  493. goto bad;
  494. dout("handle_statfs_reply %p tid %llu\n", msg, tid);
  495. mutex_lock(&monc->mutex);
  496. req = __lookup_generic_req(monc, tid);
  497. if (req) {
  498. *(struct ceph_statfs *)req->buf = reply->st;
  499. req->result = 0;
  500. get_generic_request(req);
  501. }
  502. mutex_unlock(&monc->mutex);
  503. if (req) {
  504. complete_all(&req->completion);
  505. put_generic_request(req);
  506. }
  507. return;
  508. bad:
  509. pr_err("corrupt statfs reply, tid %llu\n", tid);
  510. ceph_msg_dump(msg);
  511. }
  512. /*
  513. * Do a synchronous statfs().
  514. */
  515. int ceph_monc_do_statfs(struct ceph_mon_client *monc, struct ceph_statfs *buf)
  516. {
  517. struct ceph_mon_generic_request *req;
  518. struct ceph_mon_statfs *h;
  519. int err;
  520. req = kzalloc(sizeof(*req), GFP_NOFS);
  521. if (!req)
  522. return -ENOMEM;
  523. kref_init(&req->kref);
  524. req->buf = buf;
  525. init_completion(&req->completion);
  526. err = -ENOMEM;
  527. req->request = ceph_msg_new(CEPH_MSG_STATFS, sizeof(*h), GFP_NOFS,
  528. true);
  529. if (!req->request)
  530. goto out;
  531. req->reply = ceph_msg_new(CEPH_MSG_STATFS_REPLY, 1024, GFP_NOFS,
  532. true);
  533. if (!req->reply)
  534. goto out;
  535. /* fill out request */
  536. h = req->request->front.iov_base;
  537. h->monhdr.have_version = 0;
  538. h->monhdr.session_mon = cpu_to_le16(-1);
  539. h->monhdr.session_mon_tid = 0;
  540. h->fsid = monc->monmap->fsid;
  541. err = do_generic_request(monc, req);
  542. out:
  543. put_generic_request(req);
  544. return err;
  545. }
  546. EXPORT_SYMBOL(ceph_monc_do_statfs);
  547. static void handle_get_version_reply(struct ceph_mon_client *monc,
  548. struct ceph_msg *msg)
  549. {
  550. struct ceph_mon_generic_request *req;
  551. u64 tid = le64_to_cpu(msg->hdr.tid);
  552. void *p = msg->front.iov_base;
  553. void *end = p + msg->front_alloc_len;
  554. u64 handle;
  555. dout("%s %p tid %llu\n", __func__, msg, tid);
  556. ceph_decode_need(&p, end, 2*sizeof(u64), bad);
  557. handle = ceph_decode_64(&p);
  558. if (tid != 0 && tid != handle)
  559. goto bad;
  560. mutex_lock(&monc->mutex);
  561. req = __lookup_generic_req(monc, handle);
  562. if (req) {
  563. *(u64 *)req->buf = ceph_decode_64(&p);
  564. req->result = 0;
  565. get_generic_request(req);
  566. }
  567. mutex_unlock(&monc->mutex);
  568. if (req) {
  569. complete_all(&req->completion);
  570. put_generic_request(req);
  571. }
  572. return;
  573. bad:
  574. pr_err("corrupt mon_get_version reply, tid %llu\n", tid);
  575. ceph_msg_dump(msg);
  576. }
  577. /*
  578. * Send MMonGetVersion and wait for the reply.
  579. *
  580. * @what: one of "mdsmap", "osdmap" or "monmap"
  581. */
  582. int ceph_monc_do_get_version(struct ceph_mon_client *monc, const char *what,
  583. u64 *newest)
  584. {
  585. struct ceph_mon_generic_request *req;
  586. void *p, *end;
  587. u64 tid;
  588. int err;
  589. req = kzalloc(sizeof(*req), GFP_NOFS);
  590. if (!req)
  591. return -ENOMEM;
  592. kref_init(&req->kref);
  593. req->buf = newest;
  594. init_completion(&req->completion);
  595. req->request = ceph_msg_new(CEPH_MSG_MON_GET_VERSION,
  596. sizeof(u64) + sizeof(u32) + strlen(what),
  597. GFP_NOFS, true);
  598. if (!req->request) {
  599. err = -ENOMEM;
  600. goto out;
  601. }
  602. req->reply = ceph_msg_new(CEPH_MSG_MON_GET_VERSION_REPLY, 1024,
  603. GFP_NOFS, true);
  604. if (!req->reply) {
  605. err = -ENOMEM;
  606. goto out;
  607. }
  608. p = req->request->front.iov_base;
  609. end = p + req->request->front_alloc_len;
  610. /* fill out request */
  611. mutex_lock(&monc->mutex);
  612. tid = ++monc->last_tid;
  613. ceph_encode_64(&p, tid); /* handle */
  614. ceph_encode_string(&p, end, what, strlen(what));
  615. err = __do_generic_request(monc, tid, req);
  616. mutex_unlock(&monc->mutex);
  617. out:
  618. put_generic_request(req);
  619. return err;
  620. }
  621. EXPORT_SYMBOL(ceph_monc_do_get_version);
  622. /*
  623. * Resend pending generic requests.
  624. */
  625. static void __resend_generic_request(struct ceph_mon_client *monc)
  626. {
  627. struct ceph_mon_generic_request *req;
  628. struct rb_node *p;
  629. for (p = rb_first(&monc->generic_request_tree); p; p = rb_next(p)) {
  630. req = rb_entry(p, struct ceph_mon_generic_request, node);
  631. ceph_msg_revoke(req->request);
  632. ceph_msg_revoke_incoming(req->reply);
  633. ceph_con_send(&monc->con, ceph_msg_get(req->request));
  634. }
  635. }
  636. /*
  637. * Delayed work. If we haven't mounted yet, retry. Otherwise,
  638. * renew/retry subscription as needed (in case it is timing out, or we
  639. * got an ENOMEM). And keep the monitor connection alive.
  640. */
  641. static void delayed_work(struct work_struct *work)
  642. {
  643. struct ceph_mon_client *monc =
  644. container_of(work, struct ceph_mon_client, delayed_work.work);
  645. dout("monc delayed_work\n");
  646. mutex_lock(&monc->mutex);
  647. if (monc->hunting) {
  648. __close_session(monc);
  649. __open_session(monc); /* continue hunting */
  650. } else {
  651. struct ceph_options *opt = monc->client->options;
  652. int is_auth = ceph_auth_is_authenticated(monc->auth);
  653. if (ceph_con_keepalive_expired(&monc->con,
  654. opt->monc_ping_timeout)) {
  655. dout("monc keepalive timeout\n");
  656. is_auth = 0;
  657. __close_session(monc);
  658. monc->hunting = true;
  659. __open_session(monc);
  660. }
  661. if (!monc->hunting) {
  662. ceph_con_keepalive(&monc->con);
  663. __validate_auth(monc);
  664. }
  665. if (is_auth) {
  666. unsigned long now = jiffies;
  667. dout("%s renew subs? now %lu renew after %lu\n",
  668. __func__, now, monc->sub_renew_after);
  669. if (time_after_eq(now, monc->sub_renew_after))
  670. __send_subscribe(monc);
  671. }
  672. }
  673. __schedule_delayed(monc);
  674. mutex_unlock(&monc->mutex);
  675. }
  676. /*
  677. * On startup, we build a temporary monmap populated with the IPs
  678. * provided by mount(2).
  679. */
  680. static int build_initial_monmap(struct ceph_mon_client *monc)
  681. {
  682. struct ceph_options *opt = monc->client->options;
  683. struct ceph_entity_addr *mon_addr = opt->mon_addr;
  684. int num_mon = opt->num_mon;
  685. int i;
  686. /* build initial monmap */
  687. monc->monmap = kzalloc(sizeof(*monc->monmap) +
  688. num_mon*sizeof(monc->monmap->mon_inst[0]),
  689. GFP_KERNEL);
  690. if (!monc->monmap)
  691. return -ENOMEM;
  692. for (i = 0; i < num_mon; i++) {
  693. monc->monmap->mon_inst[i].addr = mon_addr[i];
  694. monc->monmap->mon_inst[i].addr.nonce = 0;
  695. monc->monmap->mon_inst[i].name.type =
  696. CEPH_ENTITY_TYPE_MON;
  697. monc->monmap->mon_inst[i].name.num = cpu_to_le64(i);
  698. }
  699. monc->monmap->num_mon = num_mon;
  700. return 0;
  701. }
  702. int ceph_monc_init(struct ceph_mon_client *monc, struct ceph_client *cl)
  703. {
  704. int err = 0;
  705. dout("init\n");
  706. memset(monc, 0, sizeof(*monc));
  707. monc->client = cl;
  708. monc->monmap = NULL;
  709. mutex_init(&monc->mutex);
  710. err = build_initial_monmap(monc);
  711. if (err)
  712. goto out;
  713. /* connection */
  714. /* authentication */
  715. monc->auth = ceph_auth_init(cl->options->name,
  716. cl->options->key);
  717. if (IS_ERR(monc->auth)) {
  718. err = PTR_ERR(monc->auth);
  719. goto out_monmap;
  720. }
  721. monc->auth->want_keys =
  722. CEPH_ENTITY_TYPE_AUTH | CEPH_ENTITY_TYPE_MON |
  723. CEPH_ENTITY_TYPE_OSD | CEPH_ENTITY_TYPE_MDS;
  724. /* msgs */
  725. err = -ENOMEM;
  726. monc->m_subscribe_ack = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE_ACK,
  727. sizeof(struct ceph_mon_subscribe_ack),
  728. GFP_NOFS, true);
  729. if (!monc->m_subscribe_ack)
  730. goto out_auth;
  731. monc->m_subscribe = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE, 96, GFP_NOFS,
  732. true);
  733. if (!monc->m_subscribe)
  734. goto out_subscribe_ack;
  735. monc->m_auth_reply = ceph_msg_new(CEPH_MSG_AUTH_REPLY, 4096, GFP_NOFS,
  736. true);
  737. if (!monc->m_auth_reply)
  738. goto out_subscribe;
  739. monc->m_auth = ceph_msg_new(CEPH_MSG_AUTH, 4096, GFP_NOFS, true);
  740. monc->pending_auth = 0;
  741. if (!monc->m_auth)
  742. goto out_auth_reply;
  743. ceph_con_init(&monc->con, monc, &mon_con_ops,
  744. &monc->client->msgr);
  745. monc->cur_mon = -1;
  746. monc->hunting = true;
  747. monc->sub_renew_after = jiffies;
  748. monc->sub_renew_sent = 0;
  749. INIT_DELAYED_WORK(&monc->delayed_work, delayed_work);
  750. monc->generic_request_tree = RB_ROOT;
  751. monc->num_generic_requests = 0;
  752. monc->last_tid = 0;
  753. return 0;
  754. out_auth_reply:
  755. ceph_msg_put(monc->m_auth_reply);
  756. out_subscribe:
  757. ceph_msg_put(monc->m_subscribe);
  758. out_subscribe_ack:
  759. ceph_msg_put(monc->m_subscribe_ack);
  760. out_auth:
  761. ceph_auth_destroy(monc->auth);
  762. out_monmap:
  763. kfree(monc->monmap);
  764. out:
  765. return err;
  766. }
  767. EXPORT_SYMBOL(ceph_monc_init);
  768. void ceph_monc_stop(struct ceph_mon_client *monc)
  769. {
  770. dout("stop\n");
  771. cancel_delayed_work_sync(&monc->delayed_work);
  772. mutex_lock(&monc->mutex);
  773. __close_session(monc);
  774. mutex_unlock(&monc->mutex);
  775. /*
  776. * flush msgr queue before we destroy ourselves to ensure that:
  777. * - any work that references our embedded con is finished.
  778. * - any osd_client or other work that may reference an authorizer
  779. * finishes before we shut down the auth subsystem.
  780. */
  781. ceph_msgr_flush();
  782. ceph_auth_destroy(monc->auth);
  783. ceph_msg_put(monc->m_auth);
  784. ceph_msg_put(monc->m_auth_reply);
  785. ceph_msg_put(monc->m_subscribe);
  786. ceph_msg_put(monc->m_subscribe_ack);
  787. kfree(monc->monmap);
  788. }
  789. EXPORT_SYMBOL(ceph_monc_stop);
  790. static void finish_hunting(struct ceph_mon_client *monc)
  791. {
  792. if (monc->hunting) {
  793. dout("%s found mon%d\n", __func__, monc->cur_mon);
  794. monc->hunting = false;
  795. }
  796. }
  797. static void handle_auth_reply(struct ceph_mon_client *monc,
  798. struct ceph_msg *msg)
  799. {
  800. int ret;
  801. int was_auth = 0;
  802. mutex_lock(&monc->mutex);
  803. was_auth = ceph_auth_is_authenticated(monc->auth);
  804. monc->pending_auth = 0;
  805. ret = ceph_handle_auth_reply(monc->auth, msg->front.iov_base,
  806. msg->front.iov_len,
  807. monc->m_auth->front.iov_base,
  808. monc->m_auth->front_alloc_len);
  809. if (ret > 0) {
  810. __send_prepared_auth_request(monc, ret);
  811. goto out;
  812. }
  813. finish_hunting(monc);
  814. if (ret < 0) {
  815. monc->client->auth_err = ret;
  816. } else if (!was_auth && ceph_auth_is_authenticated(monc->auth)) {
  817. dout("authenticated, starting session\n");
  818. monc->client->msgr.inst.name.type = CEPH_ENTITY_TYPE_CLIENT;
  819. monc->client->msgr.inst.name.num =
  820. cpu_to_le64(monc->auth->global_id);
  821. __send_subscribe(monc);
  822. __resend_generic_request(monc);
  823. pr_info("mon%d %s session established\n", monc->cur_mon,
  824. ceph_pr_addr(&monc->con.peer_addr.in_addr));
  825. }
  826. out:
  827. mutex_unlock(&monc->mutex);
  828. if (monc->client->auth_err < 0)
  829. wake_up_all(&monc->client->auth_wq);
  830. }
  831. static int __validate_auth(struct ceph_mon_client *monc)
  832. {
  833. int ret;
  834. if (monc->pending_auth)
  835. return 0;
  836. ret = ceph_build_auth(monc->auth, monc->m_auth->front.iov_base,
  837. monc->m_auth->front_alloc_len);
  838. if (ret <= 0)
  839. return ret; /* either an error, or no need to authenticate */
  840. __send_prepared_auth_request(monc, ret);
  841. return 0;
  842. }
  843. int ceph_monc_validate_auth(struct ceph_mon_client *monc)
  844. {
  845. int ret;
  846. mutex_lock(&monc->mutex);
  847. ret = __validate_auth(monc);
  848. mutex_unlock(&monc->mutex);
  849. return ret;
  850. }
  851. EXPORT_SYMBOL(ceph_monc_validate_auth);
  852. /*
  853. * handle incoming message
  854. */
  855. static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
  856. {
  857. struct ceph_mon_client *monc = con->private;
  858. int type = le16_to_cpu(msg->hdr.type);
  859. if (!monc)
  860. return;
  861. switch (type) {
  862. case CEPH_MSG_AUTH_REPLY:
  863. handle_auth_reply(monc, msg);
  864. break;
  865. case CEPH_MSG_MON_SUBSCRIBE_ACK:
  866. handle_subscribe_ack(monc, msg);
  867. break;
  868. case CEPH_MSG_STATFS_REPLY:
  869. handle_statfs_reply(monc, msg);
  870. break;
  871. case CEPH_MSG_MON_GET_VERSION_REPLY:
  872. handle_get_version_reply(monc, msg);
  873. break;
  874. case CEPH_MSG_MON_MAP:
  875. ceph_monc_handle_map(monc, msg);
  876. break;
  877. case CEPH_MSG_OSD_MAP:
  878. ceph_osdc_handle_map(&monc->client->osdc, msg);
  879. break;
  880. default:
  881. /* can the chained handler handle it? */
  882. if (monc->client->extra_mon_dispatch &&
  883. monc->client->extra_mon_dispatch(monc->client, msg) == 0)
  884. break;
  885. pr_err("received unknown message type %d %s\n", type,
  886. ceph_msg_type_name(type));
  887. }
  888. ceph_msg_put(msg);
  889. }
  890. /*
  891. * Allocate memory for incoming message
  892. */
  893. static struct ceph_msg *mon_alloc_msg(struct ceph_connection *con,
  894. struct ceph_msg_header *hdr,
  895. int *skip)
  896. {
  897. struct ceph_mon_client *monc = con->private;
  898. int type = le16_to_cpu(hdr->type);
  899. int front_len = le32_to_cpu(hdr->front_len);
  900. struct ceph_msg *m = NULL;
  901. *skip = 0;
  902. switch (type) {
  903. case CEPH_MSG_MON_SUBSCRIBE_ACK:
  904. m = ceph_msg_get(monc->m_subscribe_ack);
  905. break;
  906. case CEPH_MSG_STATFS_REPLY:
  907. return get_generic_reply(con, hdr, skip);
  908. case CEPH_MSG_AUTH_REPLY:
  909. m = ceph_msg_get(monc->m_auth_reply);
  910. break;
  911. case CEPH_MSG_MON_GET_VERSION_REPLY:
  912. if (le64_to_cpu(hdr->tid) != 0)
  913. return get_generic_reply(con, hdr, skip);
  914. /*
  915. * Older OSDs don't set reply tid even if the orignal
  916. * request had a non-zero tid. Workaround this weirdness
  917. * by falling through to the allocate case.
  918. */
  919. case CEPH_MSG_MON_MAP:
  920. case CEPH_MSG_MDS_MAP:
  921. case CEPH_MSG_OSD_MAP:
  922. m = ceph_msg_new(type, front_len, GFP_NOFS, false);
  923. if (!m)
  924. return NULL; /* ENOMEM--return skip == 0 */
  925. break;
  926. }
  927. if (!m) {
  928. pr_info("alloc_msg unknown type %d\n", type);
  929. *skip = 1;
  930. } else if (front_len > m->front_alloc_len) {
  931. pr_warn("mon_alloc_msg front %d > prealloc %d (%u#%llu)\n",
  932. front_len, m->front_alloc_len,
  933. (unsigned int)con->peer_name.type,
  934. le64_to_cpu(con->peer_name.num));
  935. ceph_msg_put(m);
  936. m = ceph_msg_new(type, front_len, GFP_NOFS, false);
  937. }
  938. return m;
  939. }
  940. /*
  941. * If the monitor connection resets, pick a new monitor and resubmit
  942. * any pending requests.
  943. */
  944. static void mon_fault(struct ceph_connection *con)
  945. {
  946. struct ceph_mon_client *monc = con->private;
  947. if (!monc)
  948. return;
  949. dout("mon_fault\n");
  950. mutex_lock(&monc->mutex);
  951. if (!con->private)
  952. goto out;
  953. if (!monc->hunting)
  954. pr_info("mon%d %s session lost, "
  955. "hunting for new mon\n", monc->cur_mon,
  956. ceph_pr_addr(&monc->con.peer_addr.in_addr));
  957. __close_session(monc);
  958. if (!monc->hunting) {
  959. /* start hunting */
  960. monc->hunting = true;
  961. __open_session(monc);
  962. } else {
  963. /* already hunting, let's wait a bit */
  964. __schedule_delayed(monc);
  965. }
  966. out:
  967. mutex_unlock(&monc->mutex);
  968. }
  969. /*
  970. * We can ignore refcounting on the connection struct, as all references
  971. * will come from the messenger workqueue, which is drained prior to
  972. * mon_client destruction.
  973. */
  974. static struct ceph_connection *con_get(struct ceph_connection *con)
  975. {
  976. return con;
  977. }
  978. static void con_put(struct ceph_connection *con)
  979. {
  980. }
  981. static const struct ceph_connection_operations mon_con_ops = {
  982. .get = con_get,
  983. .put = con_put,
  984. .dispatch = dispatch,
  985. .fault = mon_fault,
  986. .alloc_msg = mon_alloc_msg,
  987. };