ceph_common.c 12 KB

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