mon_client.c 27 KB

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