ceph_common.c 19 KB

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