ceph_common.c 14 KB

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