mon_client.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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_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. ceph_con_open(&monc->con,
  125. CEPH_ENTITY_TYPE_MON, monc->cur_mon,
  126. &monc->monmap->mon_inst[monc->cur_mon].addr);
  127. /* initiatiate authentication handshake */
  128. ret = ceph_auth_build_hello(monc->auth,
  129. monc->m_auth->front.iov_base,
  130. monc->m_auth->front_max);
  131. __send_prepared_auth_request(monc, ret);
  132. } else {
  133. dout("open_session mon%d already open\n", monc->cur_mon);
  134. }
  135. return 0;
  136. }
  137. static bool __sub_expired(struct ceph_mon_client *monc)
  138. {
  139. return time_after_eq(jiffies, monc->sub_renew_after);
  140. }
  141. /*
  142. * Reschedule delayed work timer.
  143. */
  144. static void __schedule_delayed(struct ceph_mon_client *monc)
  145. {
  146. unsigned int delay;
  147. if (monc->cur_mon < 0 || __sub_expired(monc))
  148. delay = 10 * HZ;
  149. else
  150. delay = 20 * HZ;
  151. dout("__schedule_delayed after %u\n", delay);
  152. schedule_delayed_work(&monc->delayed_work, delay);
  153. }
  154. /*
  155. * Send subscribe request for mdsmap and/or osdmap.
  156. */
  157. static void __send_subscribe(struct ceph_mon_client *monc)
  158. {
  159. dout("__send_subscribe sub_sent=%u exp=%u want_osd=%d\n",
  160. (unsigned int)monc->sub_sent, __sub_expired(monc),
  161. monc->want_next_osdmap);
  162. if ((__sub_expired(monc) && !monc->sub_sent) ||
  163. monc->want_next_osdmap == 1) {
  164. struct ceph_msg *msg = monc->m_subscribe;
  165. struct ceph_mon_subscribe_item *i;
  166. void *p, *end;
  167. int num;
  168. p = msg->front.iov_base;
  169. end = p + msg->front_max;
  170. num = 1 + !!monc->want_next_osdmap + !!monc->want_mdsmap;
  171. ceph_encode_32(&p, num);
  172. if (monc->want_next_osdmap) {
  173. dout("__send_subscribe to 'osdmap' %u\n",
  174. (unsigned int)monc->have_osdmap);
  175. ceph_encode_string(&p, end, "osdmap", 6);
  176. i = p;
  177. i->have = cpu_to_le64(monc->have_osdmap);
  178. i->onetime = 1;
  179. p += sizeof(*i);
  180. monc->want_next_osdmap = 2; /* requested */
  181. }
  182. if (monc->want_mdsmap) {
  183. dout("__send_subscribe to 'mdsmap' %u+\n",
  184. (unsigned int)monc->have_mdsmap);
  185. ceph_encode_string(&p, end, "mdsmap", 6);
  186. i = p;
  187. i->have = cpu_to_le64(monc->have_mdsmap);
  188. i->onetime = 0;
  189. p += sizeof(*i);
  190. }
  191. ceph_encode_string(&p, end, "monmap", 6);
  192. i = p;
  193. i->have = 0;
  194. i->onetime = 0;
  195. p += sizeof(*i);
  196. msg->front.iov_len = p - msg->front.iov_base;
  197. msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
  198. ceph_msg_revoke(msg);
  199. ceph_con_send(&monc->con, ceph_msg_get(msg));
  200. monc->sub_sent = jiffies | 1; /* never 0 */
  201. }
  202. }
  203. static void handle_subscribe_ack(struct ceph_mon_client *monc,
  204. struct ceph_msg *msg)
  205. {
  206. unsigned int seconds;
  207. struct ceph_mon_subscribe_ack *h = msg->front.iov_base;
  208. if (msg->front.iov_len < sizeof(*h))
  209. goto bad;
  210. seconds = le32_to_cpu(h->duration);
  211. mutex_lock(&monc->mutex);
  212. if (monc->hunting) {
  213. pr_info("mon%d %s session established\n",
  214. monc->cur_mon,
  215. ceph_pr_addr(&monc->con.peer_addr.in_addr));
  216. monc->hunting = false;
  217. }
  218. dout("handle_subscribe_ack after %d seconds\n", seconds);
  219. monc->sub_renew_after = monc->sub_sent + (seconds >> 1)*HZ - 1;
  220. monc->sub_sent = 0;
  221. mutex_unlock(&monc->mutex);
  222. return;
  223. bad:
  224. pr_err("got corrupt subscribe-ack msg\n");
  225. ceph_msg_dump(msg);
  226. }
  227. /*
  228. * Keep track of which maps we have
  229. */
  230. int ceph_monc_got_mdsmap(struct ceph_mon_client *monc, u32 got)
  231. {
  232. mutex_lock(&monc->mutex);
  233. monc->have_mdsmap = got;
  234. mutex_unlock(&monc->mutex);
  235. return 0;
  236. }
  237. EXPORT_SYMBOL(ceph_monc_got_mdsmap);
  238. int ceph_monc_got_osdmap(struct ceph_mon_client *monc, u32 got)
  239. {
  240. mutex_lock(&monc->mutex);
  241. monc->have_osdmap = got;
  242. monc->want_next_osdmap = 0;
  243. mutex_unlock(&monc->mutex);
  244. return 0;
  245. }
  246. /*
  247. * Register interest in the next osdmap
  248. */
  249. void ceph_monc_request_next_osdmap(struct ceph_mon_client *monc)
  250. {
  251. dout("request_next_osdmap have %u\n", monc->have_osdmap);
  252. mutex_lock(&monc->mutex);
  253. if (!monc->want_next_osdmap)
  254. monc->want_next_osdmap = 1;
  255. if (monc->want_next_osdmap < 2)
  256. __send_subscribe(monc);
  257. mutex_unlock(&monc->mutex);
  258. }
  259. /*
  260. *
  261. */
  262. int ceph_monc_open_session(struct ceph_mon_client *monc)
  263. {
  264. mutex_lock(&monc->mutex);
  265. __open_session(monc);
  266. __schedule_delayed(monc);
  267. mutex_unlock(&monc->mutex);
  268. return 0;
  269. }
  270. EXPORT_SYMBOL(ceph_monc_open_session);
  271. /*
  272. * The monitor responds with mount ack indicate mount success. The
  273. * included client ticket allows the client to talk to MDSs and OSDs.
  274. */
  275. static void ceph_monc_handle_map(struct ceph_mon_client *monc,
  276. struct ceph_msg *msg)
  277. {
  278. struct ceph_client *client = monc->client;
  279. struct ceph_monmap *monmap = NULL, *old = monc->monmap;
  280. void *p, *end;
  281. mutex_lock(&monc->mutex);
  282. dout("handle_monmap\n");
  283. p = msg->front.iov_base;
  284. end = p + msg->front.iov_len;
  285. monmap = ceph_monmap_decode(p, end);
  286. if (IS_ERR(monmap)) {
  287. pr_err("problem decoding monmap, %d\n",
  288. (int)PTR_ERR(monmap));
  289. goto out;
  290. }
  291. if (ceph_check_fsid(monc->client, &monmap->fsid) < 0) {
  292. kfree(monmap);
  293. goto out;
  294. }
  295. client->monc.monmap = monmap;
  296. kfree(old);
  297. if (!client->have_fsid) {
  298. client->have_fsid = true;
  299. mutex_unlock(&monc->mutex);
  300. /*
  301. * do debugfs initialization without mutex to avoid
  302. * creating a locking dependency
  303. */
  304. ceph_debugfs_client_init(client);
  305. goto out_unlocked;
  306. }
  307. out:
  308. mutex_unlock(&monc->mutex);
  309. out_unlocked:
  310. wake_up_all(&client->auth_wq);
  311. }
  312. /*
  313. * generic requests (e.g., statfs, poolop)
  314. */
  315. static struct ceph_mon_generic_request *__lookup_generic_req(
  316. struct ceph_mon_client *monc, u64 tid)
  317. {
  318. struct ceph_mon_generic_request *req;
  319. struct rb_node *n = monc->generic_request_tree.rb_node;
  320. while (n) {
  321. req = rb_entry(n, struct ceph_mon_generic_request, node);
  322. if (tid < req->tid)
  323. n = n->rb_left;
  324. else if (tid > req->tid)
  325. n = n->rb_right;
  326. else
  327. return req;
  328. }
  329. return NULL;
  330. }
  331. static void __insert_generic_request(struct ceph_mon_client *monc,
  332. struct ceph_mon_generic_request *new)
  333. {
  334. struct rb_node **p = &monc->generic_request_tree.rb_node;
  335. struct rb_node *parent = NULL;
  336. struct ceph_mon_generic_request *req = NULL;
  337. while (*p) {
  338. parent = *p;
  339. req = rb_entry(parent, struct ceph_mon_generic_request, node);
  340. if (new->tid < req->tid)
  341. p = &(*p)->rb_left;
  342. else if (new->tid > req->tid)
  343. p = &(*p)->rb_right;
  344. else
  345. BUG();
  346. }
  347. rb_link_node(&new->node, parent, p);
  348. rb_insert_color(&new->node, &monc->generic_request_tree);
  349. }
  350. static void release_generic_request(struct kref *kref)
  351. {
  352. struct ceph_mon_generic_request *req =
  353. container_of(kref, struct ceph_mon_generic_request, kref);
  354. if (req->reply)
  355. ceph_msg_put(req->reply);
  356. if (req->request)
  357. ceph_msg_put(req->request);
  358. kfree(req);
  359. }
  360. static void put_generic_request(struct ceph_mon_generic_request *req)
  361. {
  362. kref_put(&req->kref, release_generic_request);
  363. }
  364. static void get_generic_request(struct ceph_mon_generic_request *req)
  365. {
  366. kref_get(&req->kref);
  367. }
  368. static struct ceph_msg *get_generic_reply(struct ceph_connection *con,
  369. struct ceph_msg_header *hdr,
  370. int *skip)
  371. {
  372. struct ceph_mon_client *monc = con->private;
  373. struct ceph_mon_generic_request *req;
  374. u64 tid = le64_to_cpu(hdr->tid);
  375. struct ceph_msg *m;
  376. mutex_lock(&monc->mutex);
  377. req = __lookup_generic_req(monc, tid);
  378. if (!req) {
  379. dout("get_generic_reply %lld dne\n", tid);
  380. *skip = 1;
  381. m = NULL;
  382. } else {
  383. dout("get_generic_reply %lld got %p\n", tid, req->reply);
  384. *skip = 0;
  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_msg_revoke(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. /* authentication */
  660. monc->auth = ceph_auth_init(cl->options->name,
  661. cl->options->key);
  662. if (IS_ERR(monc->auth)) {
  663. err = PTR_ERR(monc->auth);
  664. goto out_monmap;
  665. }
  666. monc->auth->want_keys =
  667. CEPH_ENTITY_TYPE_AUTH | CEPH_ENTITY_TYPE_MON |
  668. CEPH_ENTITY_TYPE_OSD | CEPH_ENTITY_TYPE_MDS;
  669. /* msgs */
  670. err = -ENOMEM;
  671. monc->m_subscribe_ack = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE_ACK,
  672. sizeof(struct ceph_mon_subscribe_ack),
  673. GFP_NOFS, true);
  674. if (!monc->m_subscribe_ack)
  675. goto out_auth;
  676. monc->m_subscribe = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE, 96, GFP_NOFS,
  677. true);
  678. if (!monc->m_subscribe)
  679. goto out_subscribe_ack;
  680. monc->m_auth_reply = ceph_msg_new(CEPH_MSG_AUTH_REPLY, 4096, GFP_NOFS,
  681. true);
  682. if (!monc->m_auth_reply)
  683. goto out_subscribe;
  684. monc->m_auth = ceph_msg_new(CEPH_MSG_AUTH, 4096, GFP_NOFS, true);
  685. monc->pending_auth = 0;
  686. if (!monc->m_auth)
  687. goto out_auth_reply;
  688. ceph_con_init(&monc->con, monc, &mon_con_ops,
  689. &monc->client->msgr);
  690. monc->cur_mon = -1;
  691. monc->hunting = true;
  692. monc->sub_renew_after = jiffies;
  693. monc->sub_sent = 0;
  694. INIT_DELAYED_WORK(&monc->delayed_work, delayed_work);
  695. monc->generic_request_tree = RB_ROOT;
  696. monc->num_generic_requests = 0;
  697. monc->last_tid = 0;
  698. monc->have_mdsmap = 0;
  699. monc->have_osdmap = 0;
  700. monc->want_next_osdmap = 1;
  701. return 0;
  702. out_auth_reply:
  703. ceph_msg_put(monc->m_auth_reply);
  704. out_subscribe:
  705. ceph_msg_put(monc->m_subscribe);
  706. out_subscribe_ack:
  707. ceph_msg_put(monc->m_subscribe_ack);
  708. out_auth:
  709. ceph_auth_destroy(monc->auth);
  710. out_monmap:
  711. kfree(monc->monmap);
  712. out:
  713. return err;
  714. }
  715. EXPORT_SYMBOL(ceph_monc_init);
  716. void ceph_monc_stop(struct ceph_mon_client *monc)
  717. {
  718. dout("stop\n");
  719. cancel_delayed_work_sync(&monc->delayed_work);
  720. mutex_lock(&monc->mutex);
  721. __close_session(monc);
  722. mutex_unlock(&monc->mutex);
  723. /*
  724. * flush msgr queue before we destroy ourselves to ensure that:
  725. * - any work that references our embedded con is finished.
  726. * - any osd_client or other work that may reference an authorizer
  727. * finishes before we shut down the auth subsystem.
  728. */
  729. ceph_msgr_flush();
  730. ceph_auth_destroy(monc->auth);
  731. ceph_msg_put(monc->m_auth);
  732. ceph_msg_put(monc->m_auth_reply);
  733. ceph_msg_put(monc->m_subscribe);
  734. ceph_msg_put(monc->m_subscribe_ack);
  735. kfree(monc->monmap);
  736. }
  737. EXPORT_SYMBOL(ceph_monc_stop);
  738. static void handle_auth_reply(struct ceph_mon_client *monc,
  739. struct ceph_msg *msg)
  740. {
  741. int ret;
  742. int was_auth = 0;
  743. mutex_lock(&monc->mutex);
  744. if (monc->auth->ops)
  745. was_auth = monc->auth->ops->is_authenticated(monc->auth);
  746. monc->pending_auth = 0;
  747. ret = ceph_handle_auth_reply(monc->auth, msg->front.iov_base,
  748. msg->front.iov_len,
  749. monc->m_auth->front.iov_base,
  750. monc->m_auth->front_max);
  751. if (ret < 0) {
  752. monc->client->auth_err = ret;
  753. wake_up_all(&monc->client->auth_wq);
  754. } else if (ret > 0) {
  755. __send_prepared_auth_request(monc, ret);
  756. } else if (!was_auth && monc->auth->ops->is_authenticated(monc->auth)) {
  757. dout("authenticated, starting session\n");
  758. monc->client->msgr.inst.name.type = CEPH_ENTITY_TYPE_CLIENT;
  759. monc->client->msgr.inst.name.num =
  760. cpu_to_le64(monc->auth->global_id);
  761. __send_subscribe(monc);
  762. __resend_generic_request(monc);
  763. }
  764. mutex_unlock(&monc->mutex);
  765. }
  766. static int __validate_auth(struct ceph_mon_client *monc)
  767. {
  768. int ret;
  769. if (monc->pending_auth)
  770. return 0;
  771. ret = ceph_build_auth(monc->auth, monc->m_auth->front.iov_base,
  772. monc->m_auth->front_max);
  773. if (ret <= 0)
  774. return ret; /* either an error, or no need to authenticate */
  775. __send_prepared_auth_request(monc, ret);
  776. return 0;
  777. }
  778. int ceph_monc_validate_auth(struct ceph_mon_client *monc)
  779. {
  780. int ret;
  781. mutex_lock(&monc->mutex);
  782. ret = __validate_auth(monc);
  783. mutex_unlock(&monc->mutex);
  784. return ret;
  785. }
  786. EXPORT_SYMBOL(ceph_monc_validate_auth);
  787. /*
  788. * handle incoming message
  789. */
  790. static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
  791. {
  792. struct ceph_mon_client *monc = con->private;
  793. int type = le16_to_cpu(msg->hdr.type);
  794. if (!monc)
  795. return;
  796. switch (type) {
  797. case CEPH_MSG_AUTH_REPLY:
  798. handle_auth_reply(monc, msg);
  799. break;
  800. case CEPH_MSG_MON_SUBSCRIBE_ACK:
  801. handle_subscribe_ack(monc, msg);
  802. break;
  803. case CEPH_MSG_STATFS_REPLY:
  804. handle_statfs_reply(monc, msg);
  805. break;
  806. case CEPH_MSG_POOLOP_REPLY:
  807. handle_poolop_reply(monc, msg);
  808. break;
  809. case CEPH_MSG_MON_MAP:
  810. ceph_monc_handle_map(monc, msg);
  811. break;
  812. case CEPH_MSG_OSD_MAP:
  813. ceph_osdc_handle_map(&monc->client->osdc, msg);
  814. break;
  815. default:
  816. /* can the chained handler handle it? */
  817. if (monc->client->extra_mon_dispatch &&
  818. monc->client->extra_mon_dispatch(monc->client, msg) == 0)
  819. break;
  820. pr_err("received unknown message type %d %s\n", type,
  821. ceph_msg_type_name(type));
  822. }
  823. ceph_msg_put(msg);
  824. }
  825. /*
  826. * Allocate memory for incoming message
  827. */
  828. static struct ceph_msg *mon_alloc_msg(struct ceph_connection *con,
  829. struct ceph_msg_header *hdr,
  830. int *skip)
  831. {
  832. struct ceph_mon_client *monc = con->private;
  833. int type = le16_to_cpu(hdr->type);
  834. int front_len = le32_to_cpu(hdr->front_len);
  835. struct ceph_msg *m = NULL;
  836. *skip = 0;
  837. switch (type) {
  838. case CEPH_MSG_MON_SUBSCRIBE_ACK:
  839. m = ceph_msg_get(monc->m_subscribe_ack);
  840. break;
  841. case CEPH_MSG_POOLOP_REPLY:
  842. case CEPH_MSG_STATFS_REPLY:
  843. return get_generic_reply(con, hdr, skip);
  844. case CEPH_MSG_AUTH_REPLY:
  845. m = ceph_msg_get(monc->m_auth_reply);
  846. break;
  847. case CEPH_MSG_MON_MAP:
  848. case CEPH_MSG_MDS_MAP:
  849. case CEPH_MSG_OSD_MAP:
  850. m = ceph_msg_new(type, front_len, GFP_NOFS, false);
  851. if (!m)
  852. return NULL; /* ENOMEM--return skip == 0 */
  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. /*
  891. * We can ignore refcounting on the connection struct, as all references
  892. * will come from the messenger workqueue, which is drained prior to
  893. * mon_client destruction.
  894. */
  895. static struct ceph_connection *con_get(struct ceph_connection *con)
  896. {
  897. return con;
  898. }
  899. static void con_put(struct ceph_connection *con)
  900. {
  901. }
  902. static const struct ceph_connection_operations mon_con_ops = {
  903. .get = con_get,
  904. .put = con_put,
  905. .dispatch = dispatch,
  906. .fault = mon_fault,
  907. .alloc_msg = mon_alloc_msg,
  908. };