mon_client.c 30 KB

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