mon_client.c 27 KB

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