ceph_common.c 14 KB

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