mon_client.c 25 KB

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