mon_client.c 28 KB

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