ceph_common.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. #include <linux/ceph/ceph_debug.h>
  2. #include <linux/backing-dev.h>
  3. #include <linux/ctype.h>
  4. #include <linux/fs.h>
  5. #include <linux/inet.h>
  6. #include <linux/in6.h>
  7. #include <linux/key.h>
  8. #include <keys/ceph-type.h>
  9. #include <linux/module.h>
  10. #include <linux/mount.h>
  11. #include <linux/parser.h>
  12. #include <linux/sched.h>
  13. #include <linux/seq_file.h>
  14. #include <linux/slab.h>
  15. #include <linux/statfs.h>
  16. #include <linux/string.h>
  17. #include <linux/vmalloc.h>
  18. #include <linux/nsproxy.h>
  19. #include <net/net_namespace.h>
  20. #include <linux/ceph/ceph_features.h>
  21. #include <linux/ceph/libceph.h>
  22. #include <linux/ceph/debugfs.h>
  23. #include <linux/ceph/decode.h>
  24. #include <linux/ceph/mon_client.h>
  25. #include <linux/ceph/auth.h>
  26. #include "crypto.h"
  27. /*
  28. * Module compatibility interface. For now it doesn't do anything,
  29. * but its existence signals a certain level of functionality.
  30. *
  31. * The data buffer is used to pass information both to and from
  32. * libceph. The return value indicates whether libceph determines
  33. * it is compatible with the caller (from another kernel module),
  34. * given the provided data.
  35. *
  36. * The data pointer can be null.
  37. */
  38. bool libceph_compatible(void *data)
  39. {
  40. return true;
  41. }
  42. EXPORT_SYMBOL(libceph_compatible);
  43. /*
  44. * find filename portion of a path (/foo/bar/baz -> baz)
  45. */
  46. const char *ceph_file_part(const char *s, int len)
  47. {
  48. const char *e = s + len;
  49. while (e != s && *(e-1) != '/')
  50. e--;
  51. return e;
  52. }
  53. EXPORT_SYMBOL(ceph_file_part);
  54. const char *ceph_msg_type_name(int type)
  55. {
  56. switch (type) {
  57. case CEPH_MSG_SHUTDOWN: return "shutdown";
  58. case CEPH_MSG_PING: return "ping";
  59. case CEPH_MSG_AUTH: return "auth";
  60. case CEPH_MSG_AUTH_REPLY: return "auth_reply";
  61. case CEPH_MSG_MON_MAP: return "mon_map";
  62. case CEPH_MSG_MON_GET_MAP: return "mon_get_map";
  63. case CEPH_MSG_MON_SUBSCRIBE: return "mon_subscribe";
  64. case CEPH_MSG_MON_SUBSCRIBE_ACK: return "mon_subscribe_ack";
  65. case CEPH_MSG_STATFS: return "statfs";
  66. case CEPH_MSG_STATFS_REPLY: return "statfs_reply";
  67. case CEPH_MSG_MON_GET_VERSION: return "mon_get_version";
  68. case CEPH_MSG_MON_GET_VERSION_REPLY: return "mon_get_version_reply";
  69. case CEPH_MSG_MDS_MAP: return "mds_map";
  70. case CEPH_MSG_CLIENT_SESSION: return "client_session";
  71. case CEPH_MSG_CLIENT_RECONNECT: return "client_reconnect";
  72. case CEPH_MSG_CLIENT_REQUEST: return "client_request";
  73. case CEPH_MSG_CLIENT_REQUEST_FORWARD: return "client_request_forward";
  74. case CEPH_MSG_CLIENT_REPLY: return "client_reply";
  75. case CEPH_MSG_CLIENT_CAPS: return "client_caps";
  76. case CEPH_MSG_CLIENT_CAPRELEASE: return "client_cap_release";
  77. case CEPH_MSG_CLIENT_SNAP: return "client_snap";
  78. case CEPH_MSG_CLIENT_LEASE: return "client_lease";
  79. case CEPH_MSG_OSD_MAP: return "osd_map";
  80. case CEPH_MSG_OSD_OP: return "osd_op";
  81. case CEPH_MSG_OSD_OPREPLY: return "osd_opreply";
  82. case CEPH_MSG_WATCH_NOTIFY: return "watch_notify";
  83. default: return "unknown";
  84. }
  85. }
  86. EXPORT_SYMBOL(ceph_msg_type_name);
  87. /*
  88. * Initially learn our fsid, or verify an fsid matches.
  89. */
  90. int ceph_check_fsid(struct ceph_client *client, struct ceph_fsid *fsid)
  91. {
  92. if (client->have_fsid) {
  93. if (ceph_fsid_compare(&client->fsid, fsid)) {
  94. pr_err("bad fsid, had %pU got %pU",
  95. &client->fsid, fsid);
  96. return -1;
  97. }
  98. } else {
  99. memcpy(&client->fsid, fsid, sizeof(*fsid));
  100. }
  101. return 0;
  102. }
  103. EXPORT_SYMBOL(ceph_check_fsid);
  104. static int strcmp_null(const char *s1, const char *s2)
  105. {
  106. if (!s1 && !s2)
  107. return 0;
  108. if (s1 && !s2)
  109. return -1;
  110. if (!s1 && s2)
  111. return 1;
  112. return strcmp(s1, s2);
  113. }
  114. int ceph_compare_options(struct ceph_options *new_opt,
  115. struct ceph_client *client)
  116. {
  117. struct ceph_options *opt1 = new_opt;
  118. struct ceph_options *opt2 = client->options;
  119. int ofs = offsetof(struct ceph_options, mon_addr);
  120. int i;
  121. int ret;
  122. ret = memcmp(opt1, opt2, ofs);
  123. if (ret)
  124. return ret;
  125. ret = strcmp_null(opt1->name, opt2->name);
  126. if (ret)
  127. return ret;
  128. if (opt1->key && !opt2->key)
  129. return -1;
  130. if (!opt1->key && opt2->key)
  131. return 1;
  132. if (opt1->key && opt2->key) {
  133. if (opt1->key->type != opt2->key->type)
  134. return -1;
  135. if (opt1->key->created.tv_sec != opt2->key->created.tv_sec)
  136. return -1;
  137. if (opt1->key->created.tv_nsec != opt2->key->created.tv_nsec)
  138. return -1;
  139. if (opt1->key->len != opt2->key->len)
  140. return -1;
  141. if (opt1->key->key && !opt2->key->key)
  142. return -1;
  143. if (!opt1->key->key && opt2->key->key)
  144. return 1;
  145. if (opt1->key->key && opt2->key->key) {
  146. ret = memcmp(opt1->key->key, opt2->key->key, opt1->key->len);
  147. if (ret)
  148. return ret;
  149. }
  150. }
  151. /* any matching mon ip implies a match */
  152. for (i = 0; i < opt1->num_mon; i++) {
  153. if (ceph_monmap_contains(client->monc.monmap,
  154. &opt1->mon_addr[i]))
  155. return 0;
  156. }
  157. return -1;
  158. }
  159. EXPORT_SYMBOL(ceph_compare_options);
  160. void *ceph_kvmalloc(size_t size, gfp_t flags)
  161. {
  162. if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
  163. void *ptr = kmalloc(size, flags | __GFP_NOWARN);
  164. if (ptr)
  165. return ptr;
  166. }
  167. return __vmalloc(size, flags | __GFP_HIGHMEM, PAGE_KERNEL);
  168. }
  169. static int parse_fsid(const char *str, struct ceph_fsid *fsid)
  170. {
  171. int i = 0;
  172. char tmp[3];
  173. int err = -EINVAL;
  174. int d;
  175. dout("parse_fsid '%s'\n", str);
  176. tmp[2] = 0;
  177. while (*str && i < 16) {
  178. if (ispunct(*str)) {
  179. str++;
  180. continue;
  181. }
  182. if (!isxdigit(str[0]) || !isxdigit(str[1]))
  183. break;
  184. tmp[0] = str[0];
  185. tmp[1] = str[1];
  186. if (sscanf(tmp, "%x", &d) < 1)
  187. break;
  188. fsid->fsid[i] = d & 0xff;
  189. i++;
  190. str += 2;
  191. }
  192. if (i == 16)
  193. err = 0;
  194. dout("parse_fsid ret %d got fsid %pU", err, fsid);
  195. return err;
  196. }
  197. /*
  198. * ceph options
  199. */
  200. enum {
  201. Opt_osdtimeout,
  202. Opt_osdkeepalivetimeout,
  203. Opt_mount_timeout,
  204. Opt_osd_idle_ttl,
  205. Opt_last_int,
  206. /* int args above */
  207. Opt_fsid,
  208. Opt_name,
  209. Opt_secret,
  210. Opt_key,
  211. Opt_ip,
  212. Opt_last_string,
  213. /* string args above */
  214. Opt_share,
  215. Opt_noshare,
  216. Opt_crc,
  217. Opt_nocrc,
  218. Opt_cephx_require_signatures,
  219. Opt_nocephx_require_signatures,
  220. Opt_tcp_nodelay,
  221. Opt_notcp_nodelay,
  222. };
  223. static match_table_t opt_tokens = {
  224. {Opt_osdtimeout, "osdtimeout=%d"},
  225. {Opt_osdkeepalivetimeout, "osdkeepalive=%d"},
  226. {Opt_mount_timeout, "mount_timeout=%d"},
  227. {Opt_osd_idle_ttl, "osd_idle_ttl=%d"},
  228. /* int args above */
  229. {Opt_fsid, "fsid=%s"},
  230. {Opt_name, "name=%s"},
  231. {Opt_secret, "secret=%s"},
  232. {Opt_key, "key=%s"},
  233. {Opt_ip, "ip=%s"},
  234. /* string args above */
  235. {Opt_share, "share"},
  236. {Opt_noshare, "noshare"},
  237. {Opt_crc, "crc"},
  238. {Opt_nocrc, "nocrc"},
  239. {Opt_cephx_require_signatures, "cephx_require_signatures"},
  240. {Opt_nocephx_require_signatures, "nocephx_require_signatures"},
  241. {Opt_tcp_nodelay, "tcp_nodelay"},
  242. {Opt_notcp_nodelay, "notcp_nodelay"},
  243. {-1, NULL}
  244. };
  245. void ceph_destroy_options(struct ceph_options *opt)
  246. {
  247. dout("destroy_options %p\n", opt);
  248. kfree(opt->name);
  249. if (opt->key) {
  250. ceph_crypto_key_destroy(opt->key);
  251. kfree(opt->key);
  252. }
  253. kfree(opt->mon_addr);
  254. kfree(opt);
  255. }
  256. EXPORT_SYMBOL(ceph_destroy_options);
  257. /* get secret from key store */
  258. static int get_secret(struct ceph_crypto_key *dst, const char *name) {
  259. struct key *ukey;
  260. int key_err;
  261. int err = 0;
  262. struct ceph_crypto_key *ckey;
  263. ukey = request_key(&key_type_ceph, name, NULL);
  264. if (!ukey || IS_ERR(ukey)) {
  265. /* request_key errors don't map nicely to mount(2)
  266. errors; don't even try, but still printk */
  267. key_err = PTR_ERR(ukey);
  268. switch (key_err) {
  269. case -ENOKEY:
  270. pr_warn("ceph: Mount failed due to key not found: %s\n",
  271. name);
  272. break;
  273. case -EKEYEXPIRED:
  274. pr_warn("ceph: Mount failed due to expired key: %s\n",
  275. name);
  276. break;
  277. case -EKEYREVOKED:
  278. pr_warn("ceph: Mount failed due to revoked key: %s\n",
  279. name);
  280. break;
  281. default:
  282. pr_warn("ceph: Mount failed due to unknown key error %d: %s\n",
  283. key_err, name);
  284. }
  285. err = -EPERM;
  286. goto out;
  287. }
  288. ckey = ukey->payload.data;
  289. err = ceph_crypto_key_clone(dst, ckey);
  290. if (err)
  291. goto out_key;
  292. /* pass through, err is 0 */
  293. out_key:
  294. key_put(ukey);
  295. out:
  296. return err;
  297. }
  298. struct ceph_options *
  299. ceph_parse_options(char *options, const char *dev_name,
  300. const char *dev_name_end,
  301. int (*parse_extra_token)(char *c, void *private),
  302. void *private)
  303. {
  304. struct ceph_options *opt;
  305. const char *c;
  306. int err = -ENOMEM;
  307. substring_t argstr[MAX_OPT_ARGS];
  308. if (current->nsproxy->net_ns != &init_net)
  309. return ERR_PTR(-EINVAL);
  310. opt = kzalloc(sizeof(*opt), GFP_KERNEL);
  311. if (!opt)
  312. return ERR_PTR(-ENOMEM);
  313. opt->mon_addr = kcalloc(CEPH_MAX_MON, sizeof(*opt->mon_addr),
  314. GFP_KERNEL);
  315. if (!opt->mon_addr)
  316. goto out;
  317. dout("parse_options %p options '%s' dev_name '%s'\n", opt, options,
  318. dev_name);
  319. /* start with defaults */
  320. opt->flags = CEPH_OPT_DEFAULT;
  321. opt->osd_keepalive_timeout = CEPH_OSD_KEEPALIVE_DEFAULT;
  322. opt->mount_timeout = CEPH_MOUNT_TIMEOUT_DEFAULT;
  323. opt->osd_idle_ttl = CEPH_OSD_IDLE_TTL_DEFAULT;
  324. /* get mon ip(s) */
  325. /* ip1[:port1][,ip2[:port2]...] */
  326. err = ceph_parse_ips(dev_name, dev_name_end, opt->mon_addr,
  327. CEPH_MAX_MON, &opt->num_mon);
  328. if (err < 0)
  329. goto out;
  330. /* parse mount options */
  331. while ((c = strsep(&options, ",")) != NULL) {
  332. int token, intval, ret;
  333. if (!*c)
  334. continue;
  335. err = -EINVAL;
  336. token = match_token((char *)c, opt_tokens, argstr);
  337. if (token < 0 && parse_extra_token) {
  338. /* extra? */
  339. err = parse_extra_token((char *)c, private);
  340. if (err < 0) {
  341. pr_err("bad option at '%s'\n", c);
  342. goto out;
  343. }
  344. continue;
  345. }
  346. if (token < Opt_last_int) {
  347. ret = match_int(&argstr[0], &intval);
  348. if (ret < 0) {
  349. pr_err("bad mount option arg (not int) "
  350. "at '%s'\n", c);
  351. continue;
  352. }
  353. dout("got int token %d val %d\n", token, intval);
  354. } else if (token > Opt_last_int && token < Opt_last_string) {
  355. dout("got string token %d val %s\n", token,
  356. argstr[0].from);
  357. } else {
  358. dout("got token %d\n", token);
  359. }
  360. switch (token) {
  361. case Opt_ip:
  362. err = ceph_parse_ips(argstr[0].from,
  363. argstr[0].to,
  364. &opt->my_addr,
  365. 1, NULL);
  366. if (err < 0)
  367. goto out;
  368. opt->flags |= CEPH_OPT_MYIP;
  369. break;
  370. case Opt_fsid:
  371. err = parse_fsid(argstr[0].from, &opt->fsid);
  372. if (err == 0)
  373. opt->flags |= CEPH_OPT_FSID;
  374. break;
  375. case Opt_name:
  376. opt->name = kstrndup(argstr[0].from,
  377. argstr[0].to-argstr[0].from,
  378. GFP_KERNEL);
  379. break;
  380. case Opt_secret:
  381. opt->key = kzalloc(sizeof(*opt->key), GFP_KERNEL);
  382. if (!opt->key) {
  383. err = -ENOMEM;
  384. goto out;
  385. }
  386. err = ceph_crypto_key_unarmor(opt->key, argstr[0].from);
  387. if (err < 0)
  388. goto out;
  389. break;
  390. case Opt_key:
  391. opt->key = kzalloc(sizeof(*opt->key), GFP_KERNEL);
  392. if (!opt->key) {
  393. err = -ENOMEM;
  394. goto out;
  395. }
  396. err = get_secret(opt->key, argstr[0].from);
  397. if (err < 0)
  398. goto out;
  399. break;
  400. /* misc */
  401. case Opt_osdtimeout:
  402. pr_warn("ignoring deprecated osdtimeout option\n");
  403. break;
  404. case Opt_osdkeepalivetimeout:
  405. /* 0 isn't well defined right now, reject it */
  406. if (intval < 1 || intval > INT_MAX / 1000) {
  407. pr_err("osdkeepalive out of range\n");
  408. err = -EINVAL;
  409. goto out;
  410. }
  411. opt->osd_keepalive_timeout =
  412. msecs_to_jiffies(intval * 1000);
  413. break;
  414. case Opt_osd_idle_ttl:
  415. /* 0 isn't well defined right now, reject it */
  416. if (intval < 1 || intval > INT_MAX / 1000) {
  417. pr_err("osd_idle_ttl out of range\n");
  418. err = -EINVAL;
  419. goto out;
  420. }
  421. opt->osd_idle_ttl = msecs_to_jiffies(intval * 1000);
  422. break;
  423. case Opt_mount_timeout:
  424. /* 0 is "wait forever" (i.e. infinite timeout) */
  425. if (intval < 0 || intval > INT_MAX / 1000) {
  426. pr_err("mount_timeout out of range\n");
  427. err = -EINVAL;
  428. goto out;
  429. }
  430. opt->mount_timeout = msecs_to_jiffies(intval * 1000);
  431. break;
  432. case Opt_share:
  433. opt->flags &= ~CEPH_OPT_NOSHARE;
  434. break;
  435. case Opt_noshare:
  436. opt->flags |= CEPH_OPT_NOSHARE;
  437. break;
  438. case Opt_crc:
  439. opt->flags &= ~CEPH_OPT_NOCRC;
  440. break;
  441. case Opt_nocrc:
  442. opt->flags |= CEPH_OPT_NOCRC;
  443. break;
  444. case Opt_cephx_require_signatures:
  445. opt->flags &= ~CEPH_OPT_NOMSGAUTH;
  446. break;
  447. case Opt_nocephx_require_signatures:
  448. opt->flags |= CEPH_OPT_NOMSGAUTH;
  449. break;
  450. case Opt_tcp_nodelay:
  451. opt->flags |= CEPH_OPT_TCP_NODELAY;
  452. break;
  453. case Opt_notcp_nodelay:
  454. opt->flags &= ~CEPH_OPT_TCP_NODELAY;
  455. break;
  456. default:
  457. BUG_ON(token);
  458. }
  459. }
  460. /* success */
  461. return opt;
  462. out:
  463. ceph_destroy_options(opt);
  464. return ERR_PTR(err);
  465. }
  466. EXPORT_SYMBOL(ceph_parse_options);
  467. int ceph_print_client_options(struct seq_file *m, struct ceph_client *client)
  468. {
  469. struct ceph_options *opt = client->options;
  470. size_t pos = m->count;
  471. if (opt->name)
  472. seq_printf(m, "name=%s,", opt->name);
  473. if (opt->key)
  474. seq_puts(m, "secret=<hidden>,");
  475. if (opt->flags & CEPH_OPT_FSID)
  476. seq_printf(m, "fsid=%pU,", &opt->fsid);
  477. if (opt->flags & CEPH_OPT_NOSHARE)
  478. seq_puts(m, "noshare,");
  479. if (opt->flags & CEPH_OPT_NOCRC)
  480. seq_puts(m, "nocrc,");
  481. if (opt->flags & CEPH_OPT_NOMSGAUTH)
  482. seq_puts(m, "nocephx_require_signatures,");
  483. if ((opt->flags & CEPH_OPT_TCP_NODELAY) == 0)
  484. seq_puts(m, "notcp_nodelay,");
  485. if (opt->mount_timeout != CEPH_MOUNT_TIMEOUT_DEFAULT)
  486. seq_printf(m, "mount_timeout=%d,",
  487. jiffies_to_msecs(opt->mount_timeout) / 1000);
  488. if (opt->osd_idle_ttl != CEPH_OSD_IDLE_TTL_DEFAULT)
  489. seq_printf(m, "osd_idle_ttl=%d,",
  490. jiffies_to_msecs(opt->osd_idle_ttl) / 1000);
  491. if (opt->osd_keepalive_timeout != CEPH_OSD_KEEPALIVE_DEFAULT)
  492. seq_printf(m, "osdkeepalivetimeout=%d,",
  493. jiffies_to_msecs(opt->osd_keepalive_timeout) / 1000);
  494. /* drop redundant comma */
  495. if (m->count != pos)
  496. m->count--;
  497. return 0;
  498. }
  499. EXPORT_SYMBOL(ceph_print_client_options);
  500. u64 ceph_client_id(struct ceph_client *client)
  501. {
  502. return client->monc.auth->global_id;
  503. }
  504. EXPORT_SYMBOL(ceph_client_id);
  505. /*
  506. * create a fresh client instance
  507. */
  508. struct ceph_client *ceph_create_client(struct ceph_options *opt, void *private,
  509. u64 supported_features,
  510. u64 required_features)
  511. {
  512. struct ceph_client *client;
  513. struct ceph_entity_addr *myaddr = NULL;
  514. int err = -ENOMEM;
  515. client = kzalloc(sizeof(*client), GFP_KERNEL);
  516. if (client == NULL)
  517. return ERR_PTR(-ENOMEM);
  518. client->private = private;
  519. client->options = opt;
  520. mutex_init(&client->mount_mutex);
  521. init_waitqueue_head(&client->auth_wq);
  522. client->auth_err = 0;
  523. if (!ceph_test_opt(client, NOMSGAUTH))
  524. required_features |= CEPH_FEATURE_MSG_AUTH;
  525. client->extra_mon_dispatch = NULL;
  526. client->supported_features = CEPH_FEATURES_SUPPORTED_DEFAULT |
  527. supported_features;
  528. client->required_features = CEPH_FEATURES_REQUIRED_DEFAULT |
  529. required_features;
  530. /* msgr */
  531. if (ceph_test_opt(client, MYIP))
  532. myaddr = &client->options->my_addr;
  533. ceph_messenger_init(&client->msgr, myaddr,
  534. client->supported_features,
  535. client->required_features,
  536. ceph_test_opt(client, NOCRC),
  537. ceph_test_opt(client, TCP_NODELAY));
  538. /* subsystems */
  539. err = ceph_monc_init(&client->monc, client);
  540. if (err < 0)
  541. goto fail;
  542. err = ceph_osdc_init(&client->osdc, client);
  543. if (err < 0)
  544. goto fail_monc;
  545. return client;
  546. fail_monc:
  547. ceph_monc_stop(&client->monc);
  548. fail:
  549. kfree(client);
  550. return ERR_PTR(err);
  551. }
  552. EXPORT_SYMBOL(ceph_create_client);
  553. void ceph_destroy_client(struct ceph_client *client)
  554. {
  555. dout("destroy_client %p\n", client);
  556. atomic_set(&client->msgr.stopping, 1);
  557. /* unmount */
  558. ceph_osdc_stop(&client->osdc);
  559. ceph_monc_stop(&client->monc);
  560. ceph_debugfs_client_cleanup(client);
  561. ceph_destroy_options(client->options);
  562. kfree(client);
  563. dout("destroy_client %p done\n", client);
  564. }
  565. EXPORT_SYMBOL(ceph_destroy_client);
  566. /*
  567. * true if we have the mon map (and have thus joined the cluster)
  568. */
  569. static int have_mon_and_osd_map(struct ceph_client *client)
  570. {
  571. return client->monc.monmap && client->monc.monmap->epoch &&
  572. client->osdc.osdmap && client->osdc.osdmap->epoch;
  573. }
  574. /*
  575. * mount: join the ceph cluster, and open root directory.
  576. */
  577. int __ceph_open_session(struct ceph_client *client, unsigned long started)
  578. {
  579. unsigned long timeout = client->options->mount_timeout;
  580. long err;
  581. /* open session, and wait for mon and osd maps */
  582. err = ceph_monc_open_session(&client->monc);
  583. if (err < 0)
  584. return err;
  585. while (!have_mon_and_osd_map(client)) {
  586. if (timeout && time_after_eq(jiffies, started + timeout))
  587. return -ETIMEDOUT;
  588. /* wait */
  589. dout("mount waiting for mon_map\n");
  590. err = wait_event_interruptible_timeout(client->auth_wq,
  591. have_mon_and_osd_map(client) || (client->auth_err < 0),
  592. ceph_timeout_jiffies(timeout));
  593. if (err < 0)
  594. return err;
  595. if (client->auth_err < 0)
  596. return client->auth_err;
  597. }
  598. return 0;
  599. }
  600. EXPORT_SYMBOL(__ceph_open_session);
  601. int ceph_open_session(struct ceph_client *client)
  602. {
  603. int ret;
  604. unsigned long started = jiffies; /* note the start time */
  605. dout("open_session start\n");
  606. mutex_lock(&client->mount_mutex);
  607. ret = __ceph_open_session(client, started);
  608. mutex_unlock(&client->mount_mutex);
  609. return ret;
  610. }
  611. EXPORT_SYMBOL(ceph_open_session);
  612. static int __init init_ceph_lib(void)
  613. {
  614. int ret = 0;
  615. ret = ceph_debugfs_init();
  616. if (ret < 0)
  617. goto out;
  618. ret = ceph_crypto_init();
  619. if (ret < 0)
  620. goto out_debugfs;
  621. ret = ceph_msgr_init();
  622. if (ret < 0)
  623. goto out_crypto;
  624. ret = ceph_osdc_setup();
  625. if (ret < 0)
  626. goto out_msgr;
  627. pr_info("loaded (mon/osd proto %d/%d)\n",
  628. CEPH_MONC_PROTOCOL, CEPH_OSDC_PROTOCOL);
  629. return 0;
  630. out_msgr:
  631. ceph_msgr_exit();
  632. out_crypto:
  633. ceph_crypto_shutdown();
  634. out_debugfs:
  635. ceph_debugfs_cleanup();
  636. out:
  637. return ret;
  638. }
  639. static void __exit exit_ceph_lib(void)
  640. {
  641. dout("exit_ceph_lib\n");
  642. ceph_osdc_cleanup();
  643. ceph_msgr_exit();
  644. ceph_crypto_shutdown();
  645. ceph_debugfs_cleanup();
  646. }
  647. module_init(init_ceph_lib);
  648. module_exit(exit_ceph_lib);
  649. MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
  650. MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
  651. MODULE_AUTHOR("Patience Warnick <patience@newdream.net>");
  652. MODULE_DESCRIPTION("Ceph core library");
  653. MODULE_LICENSE("GPL");