mon_client.c 27 KB

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