ceph_common.c 14 KB

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