ceph_common.c 12 KB

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