ceph_common.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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 (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;
  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. err = match_int(&argstr[0], &intval);
  364. if (err < 0) {
  365. pr_err("bad option arg (not int) at '%s'\n", c);
  366. goto out;
  367. }
  368. dout("got int token %d val %d\n", token, intval);
  369. } else if (token > Opt_last_int && token < Opt_last_string) {
  370. dout("got string token %d val %s\n", token,
  371. argstr[0].from);
  372. } else {
  373. dout("got token %d\n", token);
  374. }
  375. switch (token) {
  376. case Opt_ip:
  377. err = ceph_parse_ips(argstr[0].from,
  378. argstr[0].to,
  379. &opt->my_addr,
  380. 1, NULL);
  381. if (err < 0)
  382. goto out;
  383. opt->flags |= CEPH_OPT_MYIP;
  384. break;
  385. case Opt_fsid:
  386. err = parse_fsid(argstr[0].from, &opt->fsid);
  387. if (err == 0)
  388. opt->flags |= CEPH_OPT_FSID;
  389. break;
  390. case Opt_name:
  391. kfree(opt->name);
  392. opt->name = kstrndup(argstr[0].from,
  393. argstr[0].to-argstr[0].from,
  394. GFP_KERNEL);
  395. if (!opt->name) {
  396. err = -ENOMEM;
  397. goto out;
  398. }
  399. break;
  400. case Opt_secret:
  401. ceph_crypto_key_destroy(opt->key);
  402. kfree(opt->key);
  403. opt->key = kzalloc(sizeof(*opt->key), GFP_KERNEL);
  404. if (!opt->key) {
  405. err = -ENOMEM;
  406. goto out;
  407. }
  408. err = ceph_crypto_key_unarmor(opt->key, argstr[0].from);
  409. if (err < 0)
  410. goto out;
  411. break;
  412. case Opt_key:
  413. ceph_crypto_key_destroy(opt->key);
  414. kfree(opt->key);
  415. opt->key = kzalloc(sizeof(*opt->key), GFP_KERNEL);
  416. if (!opt->key) {
  417. err = -ENOMEM;
  418. goto out;
  419. }
  420. err = get_secret(opt->key, argstr[0].from);
  421. if (err < 0)
  422. goto out;
  423. break;
  424. /* misc */
  425. case Opt_osdtimeout:
  426. pr_warn("ignoring deprecated osdtimeout option\n");
  427. break;
  428. case Opt_osdkeepalivetimeout:
  429. /* 0 isn't well defined right now, reject it */
  430. if (intval < 1 || intval > INT_MAX / 1000) {
  431. pr_err("osdkeepalive out of range\n");
  432. err = -EINVAL;
  433. goto out;
  434. }
  435. opt->osd_keepalive_timeout =
  436. msecs_to_jiffies(intval * 1000);
  437. break;
  438. case Opt_osd_idle_ttl:
  439. /* 0 isn't well defined right now, reject it */
  440. if (intval < 1 || intval > INT_MAX / 1000) {
  441. pr_err("osd_idle_ttl out of range\n");
  442. err = -EINVAL;
  443. goto out;
  444. }
  445. opt->osd_idle_ttl = msecs_to_jiffies(intval * 1000);
  446. break;
  447. case Opt_mount_timeout:
  448. /* 0 is "wait forever" (i.e. infinite timeout) */
  449. if (intval < 0 || intval > INT_MAX / 1000) {
  450. pr_err("mount_timeout out of range\n");
  451. err = -EINVAL;
  452. goto out;
  453. }
  454. opt->mount_timeout = msecs_to_jiffies(intval * 1000);
  455. break;
  456. case Opt_osd_request_timeout:
  457. /* 0 is "wait forever" (i.e. infinite timeout) */
  458. if (intval < 0 || intval > INT_MAX / 1000) {
  459. pr_err("osd_request_timeout out of range\n");
  460. err = -EINVAL;
  461. goto out;
  462. }
  463. opt->osd_request_timeout = msecs_to_jiffies(intval * 1000);
  464. break;
  465. case Opt_share:
  466. opt->flags &= ~CEPH_OPT_NOSHARE;
  467. break;
  468. case Opt_noshare:
  469. opt->flags |= CEPH_OPT_NOSHARE;
  470. break;
  471. case Opt_crc:
  472. opt->flags &= ~CEPH_OPT_NOCRC;
  473. break;
  474. case Opt_nocrc:
  475. opt->flags |= CEPH_OPT_NOCRC;
  476. break;
  477. case Opt_cephx_require_signatures:
  478. opt->flags &= ~CEPH_OPT_NOMSGAUTH;
  479. break;
  480. case Opt_nocephx_require_signatures:
  481. opt->flags |= CEPH_OPT_NOMSGAUTH;
  482. break;
  483. case Opt_cephx_sign_messages:
  484. opt->flags &= ~CEPH_OPT_NOMSGSIGN;
  485. break;
  486. case Opt_nocephx_sign_messages:
  487. opt->flags |= CEPH_OPT_NOMSGSIGN;
  488. break;
  489. case Opt_tcp_nodelay:
  490. opt->flags |= CEPH_OPT_TCP_NODELAY;
  491. break;
  492. case Opt_notcp_nodelay:
  493. opt->flags &= ~CEPH_OPT_TCP_NODELAY;
  494. break;
  495. default:
  496. BUG_ON(token);
  497. }
  498. }
  499. /* success */
  500. return opt;
  501. out:
  502. ceph_destroy_options(opt);
  503. return ERR_PTR(err);
  504. }
  505. EXPORT_SYMBOL(ceph_parse_options);
  506. int ceph_print_client_options(struct seq_file *m, struct ceph_client *client)
  507. {
  508. struct ceph_options *opt = client->options;
  509. size_t pos = m->count;
  510. if (opt->name) {
  511. seq_puts(m, "name=");
  512. seq_escape(m, opt->name, ", \t\n\\");
  513. seq_putc(m, ',');
  514. }
  515. if (opt->key)
  516. seq_puts(m, "secret=<hidden>,");
  517. if (opt->flags & CEPH_OPT_FSID)
  518. seq_printf(m, "fsid=%pU,", &opt->fsid);
  519. if (opt->flags & CEPH_OPT_NOSHARE)
  520. seq_puts(m, "noshare,");
  521. if (opt->flags & CEPH_OPT_NOCRC)
  522. seq_puts(m, "nocrc,");
  523. if (opt->flags & CEPH_OPT_NOMSGAUTH)
  524. seq_puts(m, "nocephx_require_signatures,");
  525. if (opt->flags & CEPH_OPT_NOMSGSIGN)
  526. seq_puts(m, "nocephx_sign_messages,");
  527. if ((opt->flags & CEPH_OPT_TCP_NODELAY) == 0)
  528. seq_puts(m, "notcp_nodelay,");
  529. if (opt->mount_timeout != CEPH_MOUNT_TIMEOUT_DEFAULT)
  530. seq_printf(m, "mount_timeout=%d,",
  531. jiffies_to_msecs(opt->mount_timeout) / 1000);
  532. if (opt->osd_idle_ttl != CEPH_OSD_IDLE_TTL_DEFAULT)
  533. seq_printf(m, "osd_idle_ttl=%d,",
  534. jiffies_to_msecs(opt->osd_idle_ttl) / 1000);
  535. if (opt->osd_keepalive_timeout != CEPH_OSD_KEEPALIVE_DEFAULT)
  536. seq_printf(m, "osdkeepalivetimeout=%d,",
  537. jiffies_to_msecs(opt->osd_keepalive_timeout) / 1000);
  538. if (opt->osd_request_timeout != CEPH_OSD_REQUEST_TIMEOUT_DEFAULT)
  539. seq_printf(m, "osd_request_timeout=%d,",
  540. jiffies_to_msecs(opt->osd_request_timeout) / 1000);
  541. /* drop redundant comma */
  542. if (m->count != pos)
  543. m->count--;
  544. return 0;
  545. }
  546. EXPORT_SYMBOL(ceph_print_client_options);
  547. struct ceph_entity_addr *ceph_client_addr(struct ceph_client *client)
  548. {
  549. return &client->msgr.inst.addr;
  550. }
  551. EXPORT_SYMBOL(ceph_client_addr);
  552. u64 ceph_client_gid(struct ceph_client *client)
  553. {
  554. return client->monc.auth->global_id;
  555. }
  556. EXPORT_SYMBOL(ceph_client_gid);
  557. /*
  558. * create a fresh client instance
  559. */
  560. struct ceph_client *ceph_create_client(struct ceph_options *opt, void *private)
  561. {
  562. struct ceph_client *client;
  563. struct ceph_entity_addr *myaddr = NULL;
  564. int err;
  565. err = wait_for_random_bytes();
  566. if (err < 0)
  567. return ERR_PTR(err);
  568. client = kzalloc(sizeof(*client), GFP_KERNEL);
  569. if (client == NULL)
  570. return ERR_PTR(-ENOMEM);
  571. client->private = private;
  572. client->options = opt;
  573. mutex_init(&client->mount_mutex);
  574. init_waitqueue_head(&client->auth_wq);
  575. client->auth_err = 0;
  576. client->extra_mon_dispatch = NULL;
  577. client->supported_features = CEPH_FEATURES_SUPPORTED_DEFAULT;
  578. client->required_features = CEPH_FEATURES_REQUIRED_DEFAULT;
  579. if (!ceph_test_opt(client, NOMSGAUTH))
  580. client->required_features |= CEPH_FEATURE_MSG_AUTH;
  581. /* msgr */
  582. if (ceph_test_opt(client, MYIP))
  583. myaddr = &client->options->my_addr;
  584. ceph_messenger_init(&client->msgr, myaddr);
  585. /* subsystems */
  586. err = ceph_monc_init(&client->monc, client);
  587. if (err < 0)
  588. goto fail;
  589. err = ceph_osdc_init(&client->osdc, client);
  590. if (err < 0)
  591. goto fail_monc;
  592. return client;
  593. fail_monc:
  594. ceph_monc_stop(&client->monc);
  595. fail:
  596. ceph_messenger_fini(&client->msgr);
  597. kfree(client);
  598. return ERR_PTR(err);
  599. }
  600. EXPORT_SYMBOL(ceph_create_client);
  601. void ceph_destroy_client(struct ceph_client *client)
  602. {
  603. dout("destroy_client %p\n", client);
  604. atomic_set(&client->msgr.stopping, 1);
  605. /* unmount */
  606. ceph_osdc_stop(&client->osdc);
  607. ceph_monc_stop(&client->monc);
  608. ceph_messenger_fini(&client->msgr);
  609. ceph_debugfs_client_cleanup(client);
  610. ceph_destroy_options(client->options);
  611. kfree(client);
  612. dout("destroy_client %p done\n", client);
  613. }
  614. EXPORT_SYMBOL(ceph_destroy_client);
  615. /*
  616. * true if we have the mon map (and have thus joined the cluster)
  617. */
  618. static bool have_mon_and_osd_map(struct ceph_client *client)
  619. {
  620. return client->monc.monmap && client->monc.monmap->epoch &&
  621. client->osdc.osdmap && client->osdc.osdmap->epoch;
  622. }
  623. /*
  624. * mount: join the ceph cluster, and open root directory.
  625. */
  626. int __ceph_open_session(struct ceph_client *client, unsigned long started)
  627. {
  628. unsigned long timeout = client->options->mount_timeout;
  629. long err;
  630. /* open session, and wait for mon and osd maps */
  631. err = ceph_monc_open_session(&client->monc);
  632. if (err < 0)
  633. return err;
  634. while (!have_mon_and_osd_map(client)) {
  635. if (timeout && time_after_eq(jiffies, started + timeout))
  636. return -ETIMEDOUT;
  637. /* wait */
  638. dout("mount waiting for mon_map\n");
  639. err = wait_event_interruptible_timeout(client->auth_wq,
  640. have_mon_and_osd_map(client) || (client->auth_err < 0),
  641. ceph_timeout_jiffies(timeout));
  642. if (err < 0)
  643. return err;
  644. if (client->auth_err < 0)
  645. return client->auth_err;
  646. }
  647. pr_info("client%llu fsid %pU\n", ceph_client_gid(client),
  648. &client->fsid);
  649. ceph_debugfs_client_init(client);
  650. return 0;
  651. }
  652. EXPORT_SYMBOL(__ceph_open_session);
  653. int ceph_open_session(struct ceph_client *client)
  654. {
  655. int ret;
  656. unsigned long started = jiffies; /* note the start time */
  657. dout("open_session start\n");
  658. mutex_lock(&client->mount_mutex);
  659. ret = __ceph_open_session(client, started);
  660. mutex_unlock(&client->mount_mutex);
  661. return ret;
  662. }
  663. EXPORT_SYMBOL(ceph_open_session);
  664. int ceph_wait_for_latest_osdmap(struct ceph_client *client,
  665. unsigned long timeout)
  666. {
  667. u64 newest_epoch;
  668. int ret;
  669. ret = ceph_monc_get_version(&client->monc, "osdmap", &newest_epoch);
  670. if (ret)
  671. return ret;
  672. if (client->osdc.osdmap->epoch >= newest_epoch)
  673. return 0;
  674. ceph_osdc_maybe_request_map(&client->osdc);
  675. return ceph_monc_wait_osdmap(&client->monc, newest_epoch, timeout);
  676. }
  677. EXPORT_SYMBOL(ceph_wait_for_latest_osdmap);
  678. static int __init init_ceph_lib(void)
  679. {
  680. int ret = 0;
  681. ret = ceph_debugfs_init();
  682. if (ret < 0)
  683. goto out;
  684. ret = ceph_crypto_init();
  685. if (ret < 0)
  686. goto out_debugfs;
  687. ret = ceph_msgr_init();
  688. if (ret < 0)
  689. goto out_crypto;
  690. ret = ceph_osdc_setup();
  691. if (ret < 0)
  692. goto out_msgr;
  693. pr_info("loaded (mon/osd proto %d/%d)\n",
  694. CEPH_MONC_PROTOCOL, CEPH_OSDC_PROTOCOL);
  695. return 0;
  696. out_msgr:
  697. ceph_msgr_exit();
  698. out_crypto:
  699. ceph_crypto_shutdown();
  700. out_debugfs:
  701. ceph_debugfs_cleanup();
  702. out:
  703. return ret;
  704. }
  705. static void __exit exit_ceph_lib(void)
  706. {
  707. dout("exit_ceph_lib\n");
  708. WARN_ON(!ceph_strings_empty());
  709. ceph_osdc_cleanup();
  710. ceph_msgr_exit();
  711. ceph_crypto_shutdown();
  712. ceph_debugfs_cleanup();
  713. }
  714. module_init(init_ceph_lib);
  715. module_exit(exit_ceph_lib);
  716. MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
  717. MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
  718. MODULE_AUTHOR("Patience Warnick <patience@newdream.net>");
  719. MODULE_DESCRIPTION("Ceph core library");
  720. MODULE_LICENSE("GPL");