ceph_common.c 19 KB

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