mon_client.c 25 KB

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