mon_client.c 25 KB

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