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