mon_client.c 25 KB

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