mon_client.c 25 KB

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