ceph_common.c 14 KB

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