mon_client.c 27 KB

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