mon_client.c 29 KB

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