ceph_common.c 14 KB

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