ceph_common.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/ceph/ceph_debug.h>
  3. #include <linux/backing-dev.h>
  4. #include <linux/ctype.h>
  5. #include <linux/fs.h>
  6. #include <linux/inet.h>
  7. #include <linux/in6.h>
  8. #include <linux/key.h>
  9. #include <keys/ceph-type.h>
  10. #include <linux/module.h>
  11. #include <linux/mount.h>
  12. #include <linux/nsproxy.h>
  13. #include <linux/fs_parser.h>
  14. #include <linux/sched.h>
  15. #include <linux/sched/mm.h>
  16. #include <linux/seq_file.h>
  17. #include <linux/slab.h>
  18. #include <linux/statfs.h>
  19. #include <linux/string.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/ceph/ceph_features.h>
  22. #include <linux/ceph/libceph.h>
  23. #include <linux/ceph/debugfs.h>
  24. #include <linux/ceph/decode.h>
  25. #include <linux/ceph/mon_client.h>
  26. #include <linux/ceph/auth.h>
  27. #include "crypto.h"
  28. /*
  29. * Module compatibility interface. For now it doesn't do anything,
  30. * but its existence signals a certain level of functionality.
  31. *
  32. * The data buffer is used to pass information both to and from
  33. * libceph. The return value indicates whether libceph determines
  34. * it is compatible with the caller (from another kernel module),
  35. * given the provided data.
  36. *
  37. * The data pointer can be null.
  38. */
  39. bool libceph_compatible(void *data)
  40. {
  41. return true;
  42. }
  43. EXPORT_SYMBOL(libceph_compatible);
  44. static int param_get_supported_features(char *buffer,
  45. const struct kernel_param *kp)
  46. {
  47. return sprintf(buffer, "0x%llx", CEPH_FEATURES_SUPPORTED_DEFAULT);
  48. }
  49. static const struct kernel_param_ops param_ops_supported_features = {
  50. .get = param_get_supported_features,
  51. };
  52. module_param_cb(supported_features, &param_ops_supported_features, NULL,
  53. 0444);
  54. const char *ceph_msg_type_name(int type)
  55. {
  56. switch (type) {
  57. case CEPH_MSG_SHUTDOWN: return "shutdown";
  58. case CEPH_MSG_PING: return "ping";
  59. case CEPH_MSG_AUTH: return "auth";
  60. case CEPH_MSG_AUTH_REPLY: return "auth_reply";
  61. case CEPH_MSG_MON_MAP: return "mon_map";
  62. case CEPH_MSG_MON_GET_MAP: return "mon_get_map";
  63. case CEPH_MSG_MON_SUBSCRIBE: return "mon_subscribe";
  64. case CEPH_MSG_MON_SUBSCRIBE_ACK: return "mon_subscribe_ack";
  65. case CEPH_MSG_STATFS: return "statfs";
  66. case CEPH_MSG_STATFS_REPLY: return "statfs_reply";
  67. case CEPH_MSG_MON_GET_VERSION: return "mon_get_version";
  68. case CEPH_MSG_MON_GET_VERSION_REPLY: return "mon_get_version_reply";
  69. case CEPH_MSG_MDS_MAP: return "mds_map";
  70. case CEPH_MSG_FS_MAP_USER: return "fs_map_user";
  71. case CEPH_MSG_CLIENT_SESSION: return "client_session";
  72. case CEPH_MSG_CLIENT_RECONNECT: return "client_reconnect";
  73. case CEPH_MSG_CLIENT_REQUEST: return "client_request";
  74. case CEPH_MSG_CLIENT_REQUEST_FORWARD: return "client_request_forward";
  75. case CEPH_MSG_CLIENT_REPLY: return "client_reply";
  76. case CEPH_MSG_CLIENT_CAPS: return "client_caps";
  77. case CEPH_MSG_CLIENT_CAPRELEASE: return "client_cap_release";
  78. case CEPH_MSG_CLIENT_QUOTA: return "client_quota";
  79. case CEPH_MSG_CLIENT_SNAP: return "client_snap";
  80. case CEPH_MSG_CLIENT_LEASE: return "client_lease";
  81. case CEPH_MSG_POOLOP_REPLY: return "poolop_reply";
  82. case CEPH_MSG_POOLOP: return "poolop";
  83. case CEPH_MSG_MON_COMMAND: return "mon_command";
  84. case CEPH_MSG_MON_COMMAND_ACK: return "mon_command_ack";
  85. case CEPH_MSG_OSD_MAP: return "osd_map";
  86. case CEPH_MSG_OSD_OP: return "osd_op";
  87. case CEPH_MSG_OSD_OPREPLY: return "osd_opreply";
  88. case CEPH_MSG_WATCH_NOTIFY: return "watch_notify";
  89. case CEPH_MSG_OSD_BACKOFF: return "osd_backoff";
  90. default: return "unknown";
  91. }
  92. }
  93. EXPORT_SYMBOL(ceph_msg_type_name);
  94. /*
  95. * Initially learn our fsid, or verify an fsid matches.
  96. */
  97. int ceph_check_fsid(struct ceph_client *client, struct ceph_fsid *fsid)
  98. {
  99. if (client->have_fsid) {
  100. if (ceph_fsid_compare(&client->fsid, fsid)) {
  101. pr_err("bad fsid, had %pU got %pU",
  102. &client->fsid, fsid);
  103. return -1;
  104. }
  105. } else {
  106. memcpy(&client->fsid, fsid, sizeof(*fsid));
  107. }
  108. return 0;
  109. }
  110. EXPORT_SYMBOL(ceph_check_fsid);
  111. static int strcmp_null(const char *s1, const char *s2)
  112. {
  113. if (!s1 && !s2)
  114. return 0;
  115. if (s1 && !s2)
  116. return -1;
  117. if (!s1 && s2)
  118. return 1;
  119. return strcmp(s1, s2);
  120. }
  121. int ceph_compare_options(struct ceph_options *new_opt,
  122. struct ceph_client *client)
  123. {
  124. struct ceph_options *opt1 = new_opt;
  125. struct ceph_options *opt2 = client->options;
  126. int ofs = offsetof(struct ceph_options, mon_addr);
  127. int i;
  128. int ret;
  129. /*
  130. * Don't bother comparing options if network namespaces don't
  131. * match.
  132. */
  133. if (!net_eq(current->nsproxy->net_ns, read_pnet(&client->msgr.net)))
  134. return -1;
  135. ret = memcmp(opt1, opt2, ofs);
  136. if (ret)
  137. return ret;
  138. ret = strcmp_null(opt1->name, opt2->name);
  139. if (ret)
  140. return ret;
  141. if (opt1->key && !opt2->key)
  142. return -1;
  143. if (!opt1->key && opt2->key)
  144. return 1;
  145. if (opt1->key && opt2->key) {
  146. if (opt1->key->type != opt2->key->type)
  147. return -1;
  148. if (opt1->key->created.tv_sec != opt2->key->created.tv_sec)
  149. return -1;
  150. if (opt1->key->created.tv_nsec != opt2->key->created.tv_nsec)
  151. return -1;
  152. if (opt1->key->len != opt2->key->len)
  153. return -1;
  154. if (opt1->key->key && !opt2->key->key)
  155. return -1;
  156. if (!opt1->key->key && opt2->key->key)
  157. return 1;
  158. if (opt1->key->key && opt2->key->key) {
  159. ret = memcmp(opt1->key->key, opt2->key->key, opt1->key->len);
  160. if (ret)
  161. return ret;
  162. }
  163. }
  164. ret = ceph_compare_crush_locs(&opt1->crush_locs, &opt2->crush_locs);
  165. if (ret)
  166. return ret;
  167. /* any matching mon ip implies a match */
  168. for (i = 0; i < opt1->num_mon; i++) {
  169. if (ceph_monmap_contains(client->monc.monmap,
  170. &opt1->mon_addr[i]))
  171. return 0;
  172. }
  173. return -1;
  174. }
  175. EXPORT_SYMBOL(ceph_compare_options);
  176. int ceph_parse_fsid(const char *str, struct ceph_fsid *fsid)
  177. {
  178. int i = 0;
  179. char tmp[3];
  180. int err = -EINVAL;
  181. int d;
  182. dout("%s '%s'\n", __func__, str);
  183. tmp[2] = 0;
  184. while (*str && i < 16) {
  185. if (ispunct(*str)) {
  186. str++;
  187. continue;
  188. }
  189. if (!isxdigit(str[0]) || !isxdigit(str[1]))
  190. break;
  191. tmp[0] = str[0];
  192. tmp[1] = str[1];
  193. if (sscanf(tmp, "%x", &d) < 1)
  194. break;
  195. fsid->fsid[i] = d & 0xff;
  196. i++;
  197. str += 2;
  198. }
  199. if (i == 16)
  200. err = 0;
  201. dout("%s ret %d got fsid %pU\n", __func__, err, fsid);
  202. return err;
  203. }
  204. EXPORT_SYMBOL(ceph_parse_fsid);
  205. /*
  206. * ceph options
  207. */
  208. enum {
  209. Opt_osdkeepalivetimeout,
  210. Opt_mount_timeout,
  211. Opt_osd_idle_ttl,
  212. Opt_osd_request_timeout,
  213. /* int args above */
  214. Opt_fsid,
  215. Opt_name,
  216. Opt_secret,
  217. Opt_key,
  218. Opt_ip,
  219. Opt_crush_location,
  220. Opt_read_from_replica,
  221. Opt_ms_mode,
  222. /* string args above */
  223. Opt_share,
  224. Opt_crc,
  225. Opt_cephx_require_signatures,
  226. Opt_cephx_sign_messages,
  227. Opt_tcp_nodelay,
  228. Opt_abort_on_full,
  229. Opt_rxbounce,
  230. };
  231. enum {
  232. Opt_read_from_replica_no,
  233. Opt_read_from_replica_balance,
  234. Opt_read_from_replica_localize,
  235. };
  236. static const struct constant_table ceph_param_read_from_replica[] = {
  237. {"no", Opt_read_from_replica_no},
  238. {"balance", Opt_read_from_replica_balance},
  239. {"localize", Opt_read_from_replica_localize},
  240. {}
  241. };
  242. enum ceph_ms_mode {
  243. Opt_ms_mode_legacy,
  244. Opt_ms_mode_crc,
  245. Opt_ms_mode_secure,
  246. Opt_ms_mode_prefer_crc,
  247. Opt_ms_mode_prefer_secure
  248. };
  249. static const struct constant_table ceph_param_ms_mode[] = {
  250. {"legacy", Opt_ms_mode_legacy},
  251. {"crc", Opt_ms_mode_crc},
  252. {"secure", Opt_ms_mode_secure},
  253. {"prefer-crc", Opt_ms_mode_prefer_crc},
  254. {"prefer-secure", Opt_ms_mode_prefer_secure},
  255. {}
  256. };
  257. static const struct fs_parameter_spec ceph_parameters[] = {
  258. fsparam_flag ("abort_on_full", Opt_abort_on_full),
  259. __fsparam (NULL, "cephx_require_signatures", Opt_cephx_require_signatures,
  260. fs_param_neg_with_no|fs_param_deprecated, NULL),
  261. fsparam_flag_no ("cephx_sign_messages", Opt_cephx_sign_messages),
  262. fsparam_flag_no ("crc", Opt_crc),
  263. fsparam_string ("crush_location", Opt_crush_location),
  264. fsparam_string ("fsid", Opt_fsid),
  265. fsparam_string ("ip", Opt_ip),
  266. fsparam_string ("key", Opt_key),
  267. fsparam_u32 ("mount_timeout", Opt_mount_timeout),
  268. fsparam_string ("name", Opt_name),
  269. fsparam_u32 ("osd_idle_ttl", Opt_osd_idle_ttl),
  270. fsparam_u32 ("osd_request_timeout", Opt_osd_request_timeout),
  271. fsparam_u32 ("osdkeepalive", Opt_osdkeepalivetimeout),
  272. fsparam_enum ("read_from_replica", Opt_read_from_replica,
  273. ceph_param_read_from_replica),
  274. fsparam_flag ("rxbounce", Opt_rxbounce),
  275. fsparam_enum ("ms_mode", Opt_ms_mode,
  276. ceph_param_ms_mode),
  277. fsparam_string ("secret", Opt_secret),
  278. fsparam_flag_no ("share", Opt_share),
  279. fsparam_flag_no ("tcp_nodelay", Opt_tcp_nodelay),
  280. {}
  281. };
  282. struct ceph_options *ceph_alloc_options(void)
  283. {
  284. struct ceph_options *opt;
  285. opt = kzalloc(sizeof(*opt), GFP_KERNEL);
  286. if (!opt)
  287. return NULL;
  288. opt->crush_locs = RB_ROOT;
  289. opt->mon_addr = kcalloc(CEPH_MAX_MON, sizeof(*opt->mon_addr),
  290. GFP_KERNEL);
  291. if (!opt->mon_addr) {
  292. kfree(opt);
  293. return NULL;
  294. }
  295. opt->flags = CEPH_OPT_DEFAULT;
  296. opt->osd_keepalive_timeout = CEPH_OSD_KEEPALIVE_DEFAULT;
  297. opt->mount_timeout = CEPH_MOUNT_TIMEOUT_DEFAULT;
  298. opt->osd_idle_ttl = CEPH_OSD_IDLE_TTL_DEFAULT;
  299. opt->osd_request_timeout = CEPH_OSD_REQUEST_TIMEOUT_DEFAULT;
  300. opt->read_from_replica = CEPH_READ_FROM_REPLICA_DEFAULT;
  301. opt->con_modes[0] = CEPH_CON_MODE_UNKNOWN;
  302. opt->con_modes[1] = CEPH_CON_MODE_UNKNOWN;
  303. return opt;
  304. }
  305. EXPORT_SYMBOL(ceph_alloc_options);
  306. void ceph_destroy_options(struct ceph_options *opt)
  307. {
  308. dout("destroy_options %p\n", opt);
  309. if (!opt)
  310. return;
  311. ceph_clear_crush_locs(&opt->crush_locs);
  312. kfree(opt->name);
  313. if (opt->key) {
  314. ceph_crypto_key_destroy(opt->key);
  315. kfree(opt->key);
  316. }
  317. kfree(opt->mon_addr);
  318. kfree(opt);
  319. }
  320. EXPORT_SYMBOL(ceph_destroy_options);
  321. /* get secret from key store */
  322. static int get_secret(struct ceph_crypto_key *dst, const char *name,
  323. struct p_log *log)
  324. {
  325. struct key *ukey;
  326. int key_err;
  327. int err = 0;
  328. struct ceph_crypto_key *ckey;
  329. ukey = request_key(&key_type_ceph, name, NULL);
  330. if (IS_ERR(ukey)) {
  331. /* request_key errors don't map nicely to mount(2)
  332. errors; don't even try, but still printk */
  333. key_err = PTR_ERR(ukey);
  334. switch (key_err) {
  335. case -ENOKEY:
  336. error_plog(log, "Failed due to key not found: %s",
  337. name);
  338. break;
  339. case -EKEYEXPIRED:
  340. error_plog(log, "Failed due to expired key: %s",
  341. name);
  342. break;
  343. case -EKEYREVOKED:
  344. error_plog(log, "Failed due to revoked key: %s",
  345. name);
  346. break;
  347. default:
  348. error_plog(log, "Failed due to key error %d: %s",
  349. key_err, name);
  350. }
  351. err = -EPERM;
  352. goto out;
  353. }
  354. ckey = ukey->payload.data[0];
  355. err = ceph_crypto_key_clone(dst, ckey);
  356. if (err)
  357. goto out_key;
  358. /* pass through, err is 0 */
  359. out_key:
  360. key_put(ukey);
  361. out:
  362. return err;
  363. }
  364. int ceph_parse_mon_ips(const char *buf, size_t len, struct ceph_options *opt,
  365. struct fc_log *l, char delim)
  366. {
  367. struct p_log log = {.prefix = "libceph", .log = l};
  368. int ret;
  369. /* ip1[:port1][<delim>ip2[:port2]...] */
  370. ret = ceph_parse_ips(buf, buf + len, opt->mon_addr, CEPH_MAX_MON,
  371. &opt->num_mon, delim);
  372. if (ret) {
  373. error_plog(&log, "Failed to parse monitor IPs: %d", ret);
  374. return ret;
  375. }
  376. return 0;
  377. }
  378. EXPORT_SYMBOL(ceph_parse_mon_ips);
  379. int ceph_parse_param(struct fs_parameter *param, struct ceph_options *opt,
  380. struct fc_log *l)
  381. {
  382. struct fs_parse_result result;
  383. int token, err;
  384. struct p_log log = {.prefix = "libceph", .log = l};
  385. token = __fs_parse(&log, ceph_parameters, param, &result);
  386. dout("%s fs_parse '%s' token %d\n", __func__, param->key, token);
  387. if (token < 0)
  388. return token;
  389. switch (token) {
  390. case Opt_ip:
  391. err = ceph_parse_ips(param->string,
  392. param->string + param->size,
  393. &opt->my_addr, 1, NULL, ',');
  394. if (err) {
  395. error_plog(&log, "Failed to parse ip: %d", err);
  396. return err;
  397. }
  398. opt->flags |= CEPH_OPT_MYIP;
  399. break;
  400. case Opt_fsid:
  401. err = ceph_parse_fsid(param->string, &opt->fsid);
  402. if (err) {
  403. error_plog(&log, "Failed to parse fsid: %d", err);
  404. return err;
  405. }
  406. opt->flags |= CEPH_OPT_FSID;
  407. break;
  408. case Opt_name:
  409. kfree(opt->name);
  410. opt->name = param->string;
  411. param->string = NULL;
  412. break;
  413. case Opt_secret:
  414. ceph_crypto_key_destroy(opt->key);
  415. kfree(opt->key);
  416. opt->key = kzalloc(sizeof(*opt->key), GFP_KERNEL);
  417. if (!opt->key)
  418. return -ENOMEM;
  419. err = ceph_crypto_key_unarmor(opt->key, param->string);
  420. if (err) {
  421. error_plog(&log, "Failed to parse secret: %d", err);
  422. return err;
  423. }
  424. break;
  425. case Opt_key:
  426. ceph_crypto_key_destroy(opt->key);
  427. kfree(opt->key);
  428. opt->key = kzalloc(sizeof(*opt->key), GFP_KERNEL);
  429. if (!opt->key)
  430. return -ENOMEM;
  431. return get_secret(opt->key, param->string, &log);
  432. case Opt_crush_location:
  433. ceph_clear_crush_locs(&opt->crush_locs);
  434. err = ceph_parse_crush_location(param->string,
  435. &opt->crush_locs);
  436. if (err) {
  437. error_plog(&log, "Failed to parse CRUSH location: %d",
  438. err);
  439. return err;
  440. }
  441. break;
  442. case Opt_read_from_replica:
  443. switch (result.uint_32) {
  444. case Opt_read_from_replica_no:
  445. opt->read_from_replica = 0;
  446. break;
  447. case Opt_read_from_replica_balance:
  448. opt->read_from_replica = CEPH_OSD_FLAG_BALANCE_READS;
  449. break;
  450. case Opt_read_from_replica_localize:
  451. opt->read_from_replica = CEPH_OSD_FLAG_LOCALIZE_READS;
  452. break;
  453. default:
  454. BUG();
  455. }
  456. break;
  457. case Opt_ms_mode:
  458. switch (result.uint_32) {
  459. case Opt_ms_mode_legacy:
  460. opt->con_modes[0] = CEPH_CON_MODE_UNKNOWN;
  461. opt->con_modes[1] = CEPH_CON_MODE_UNKNOWN;
  462. break;
  463. case Opt_ms_mode_crc:
  464. opt->con_modes[0] = CEPH_CON_MODE_CRC;
  465. opt->con_modes[1] = CEPH_CON_MODE_UNKNOWN;
  466. break;
  467. case Opt_ms_mode_secure:
  468. opt->con_modes[0] = CEPH_CON_MODE_SECURE;
  469. opt->con_modes[1] = CEPH_CON_MODE_UNKNOWN;
  470. break;
  471. case Opt_ms_mode_prefer_crc:
  472. opt->con_modes[0] = CEPH_CON_MODE_CRC;
  473. opt->con_modes[1] = CEPH_CON_MODE_SECURE;
  474. break;
  475. case Opt_ms_mode_prefer_secure:
  476. opt->con_modes[0] = CEPH_CON_MODE_SECURE;
  477. opt->con_modes[1] = CEPH_CON_MODE_CRC;
  478. break;
  479. default:
  480. BUG();
  481. }
  482. break;
  483. case Opt_osdkeepalivetimeout:
  484. /* 0 isn't well defined right now, reject it */
  485. if (result.uint_32 < 1 || result.uint_32 > INT_MAX / 1000)
  486. goto out_of_range;
  487. opt->osd_keepalive_timeout =
  488. msecs_to_jiffies(result.uint_32 * 1000);
  489. break;
  490. case Opt_osd_idle_ttl:
  491. /* 0 isn't well defined right now, reject it */
  492. if (result.uint_32 < 1 || result.uint_32 > INT_MAX / 1000)
  493. goto out_of_range;
  494. opt->osd_idle_ttl = msecs_to_jiffies(result.uint_32 * 1000);
  495. break;
  496. case Opt_mount_timeout:
  497. /* 0 is "wait forever" (i.e. infinite timeout) */
  498. if (result.uint_32 > INT_MAX / 1000)
  499. goto out_of_range;
  500. opt->mount_timeout = msecs_to_jiffies(result.uint_32 * 1000);
  501. break;
  502. case Opt_osd_request_timeout:
  503. /* 0 is "wait forever" (i.e. infinite timeout) */
  504. if (result.uint_32 > INT_MAX / 1000)
  505. goto out_of_range;
  506. opt->osd_request_timeout =
  507. msecs_to_jiffies(result.uint_32 * 1000);
  508. break;
  509. case Opt_share:
  510. if (!result.negated)
  511. opt->flags &= ~CEPH_OPT_NOSHARE;
  512. else
  513. opt->flags |= CEPH_OPT_NOSHARE;
  514. break;
  515. case Opt_crc:
  516. if (!result.negated)
  517. opt->flags &= ~CEPH_OPT_NOCRC;
  518. else
  519. opt->flags |= CEPH_OPT_NOCRC;
  520. break;
  521. case Opt_cephx_require_signatures:
  522. if (!result.negated)
  523. warn_plog(&log, "Ignoring cephx_require_signatures");
  524. else
  525. warn_plog(&log, "Ignoring nocephx_require_signatures, use nocephx_sign_messages");
  526. break;
  527. case Opt_cephx_sign_messages:
  528. if (!result.negated)
  529. opt->flags &= ~CEPH_OPT_NOMSGSIGN;
  530. else
  531. opt->flags |= CEPH_OPT_NOMSGSIGN;
  532. break;
  533. case Opt_tcp_nodelay:
  534. if (!result.negated)
  535. opt->flags |= CEPH_OPT_TCP_NODELAY;
  536. else
  537. opt->flags &= ~CEPH_OPT_TCP_NODELAY;
  538. break;
  539. case Opt_abort_on_full:
  540. opt->flags |= CEPH_OPT_ABORT_ON_FULL;
  541. break;
  542. case Opt_rxbounce:
  543. opt->flags |= CEPH_OPT_RXBOUNCE;
  544. break;
  545. default:
  546. BUG();
  547. }
  548. return 0;
  549. out_of_range:
  550. return inval_plog(&log, "%s out of range", param->key);
  551. }
  552. EXPORT_SYMBOL(ceph_parse_param);
  553. int ceph_print_client_options(struct seq_file *m, struct ceph_client *client,
  554. bool show_all)
  555. {
  556. struct ceph_options *opt = client->options;
  557. size_t pos = m->count;
  558. struct rb_node *n;
  559. if (opt->name) {
  560. seq_puts(m, "name=");
  561. seq_escape(m, opt->name, ", \t\n\\");
  562. seq_putc(m, ',');
  563. }
  564. if (opt->key)
  565. seq_puts(m, "secret=<hidden>,");
  566. if (!RB_EMPTY_ROOT(&opt->crush_locs)) {
  567. seq_puts(m, "crush_location=");
  568. for (n = rb_first(&opt->crush_locs); ; ) {
  569. struct crush_loc_node *loc =
  570. rb_entry(n, struct crush_loc_node, cl_node);
  571. seq_printf(m, "%s:%s", loc->cl_loc.cl_type_name,
  572. loc->cl_loc.cl_name);
  573. n = rb_next(n);
  574. if (!n)
  575. break;
  576. seq_putc(m, '|');
  577. }
  578. seq_putc(m, ',');
  579. }
  580. if (opt->read_from_replica == CEPH_OSD_FLAG_BALANCE_READS) {
  581. seq_puts(m, "read_from_replica=balance,");
  582. } else if (opt->read_from_replica == CEPH_OSD_FLAG_LOCALIZE_READS) {
  583. seq_puts(m, "read_from_replica=localize,");
  584. }
  585. if (opt->con_modes[0] != CEPH_CON_MODE_UNKNOWN) {
  586. if (opt->con_modes[0] == CEPH_CON_MODE_CRC &&
  587. opt->con_modes[1] == CEPH_CON_MODE_UNKNOWN) {
  588. seq_puts(m, "ms_mode=crc,");
  589. } else if (opt->con_modes[0] == CEPH_CON_MODE_SECURE &&
  590. opt->con_modes[1] == CEPH_CON_MODE_UNKNOWN) {
  591. seq_puts(m, "ms_mode=secure,");
  592. } else if (opt->con_modes[0] == CEPH_CON_MODE_CRC &&
  593. opt->con_modes[1] == CEPH_CON_MODE_SECURE) {
  594. seq_puts(m, "ms_mode=prefer-crc,");
  595. } else if (opt->con_modes[0] == CEPH_CON_MODE_SECURE &&
  596. opt->con_modes[1] == CEPH_CON_MODE_CRC) {
  597. seq_puts(m, "ms_mode=prefer-secure,");
  598. }
  599. }
  600. if (opt->flags & CEPH_OPT_FSID)
  601. seq_printf(m, "fsid=%pU,", &opt->fsid);
  602. if (opt->flags & CEPH_OPT_NOSHARE)
  603. seq_puts(m, "noshare,");
  604. if (opt->flags & CEPH_OPT_NOCRC)
  605. seq_puts(m, "nocrc,");
  606. if (opt->flags & CEPH_OPT_NOMSGSIGN)
  607. seq_puts(m, "nocephx_sign_messages,");
  608. if ((opt->flags & CEPH_OPT_TCP_NODELAY) == 0)
  609. seq_puts(m, "notcp_nodelay,");
  610. if (show_all && (opt->flags & CEPH_OPT_ABORT_ON_FULL))
  611. seq_puts(m, "abort_on_full,");
  612. if (opt->flags & CEPH_OPT_RXBOUNCE)
  613. seq_puts(m, "rxbounce,");
  614. if (opt->mount_timeout != CEPH_MOUNT_TIMEOUT_DEFAULT)
  615. seq_printf(m, "mount_timeout=%d,",
  616. jiffies_to_msecs(opt->mount_timeout) / 1000);
  617. if (opt->osd_idle_ttl != CEPH_OSD_IDLE_TTL_DEFAULT)
  618. seq_printf(m, "osd_idle_ttl=%d,",
  619. jiffies_to_msecs(opt->osd_idle_ttl) / 1000);
  620. if (opt->osd_keepalive_timeout != CEPH_OSD_KEEPALIVE_DEFAULT)
  621. seq_printf(m, "osdkeepalivetimeout=%d,",
  622. jiffies_to_msecs(opt->osd_keepalive_timeout) / 1000);
  623. if (opt->osd_request_timeout != CEPH_OSD_REQUEST_TIMEOUT_DEFAULT)
  624. seq_printf(m, "osd_request_timeout=%d,",
  625. jiffies_to_msecs(opt->osd_request_timeout) / 1000);
  626. /* drop redundant comma */
  627. if (m->count != pos)
  628. m->count--;
  629. return 0;
  630. }
  631. EXPORT_SYMBOL(ceph_print_client_options);
  632. struct ceph_entity_addr *ceph_client_addr(struct ceph_client *client)
  633. {
  634. return &client->msgr.inst.addr;
  635. }
  636. EXPORT_SYMBOL(ceph_client_addr);
  637. u64 ceph_client_gid(struct ceph_client *client)
  638. {
  639. return client->monc.auth->global_id;
  640. }
  641. EXPORT_SYMBOL(ceph_client_gid);
  642. /*
  643. * create a fresh client instance
  644. */
  645. struct ceph_client *ceph_create_client(struct ceph_options *opt, void *private)
  646. {
  647. struct ceph_client *client;
  648. struct ceph_entity_addr *myaddr = NULL;
  649. int err;
  650. err = wait_for_random_bytes();
  651. if (err < 0)
  652. return ERR_PTR(err);
  653. client = kzalloc(sizeof(*client), GFP_KERNEL);
  654. if (client == NULL)
  655. return ERR_PTR(-ENOMEM);
  656. client->private = private;
  657. client->options = opt;
  658. mutex_init(&client->mount_mutex);
  659. init_waitqueue_head(&client->auth_wq);
  660. client->auth_err = 0;
  661. client->extra_mon_dispatch = NULL;
  662. client->supported_features = CEPH_FEATURES_SUPPORTED_DEFAULT;
  663. client->required_features = CEPH_FEATURES_REQUIRED_DEFAULT;
  664. if (!ceph_test_opt(client, NOMSGSIGN))
  665. client->required_features |= CEPH_FEATURE_MSG_AUTH;
  666. /* msgr */
  667. if (ceph_test_opt(client, MYIP))
  668. myaddr = &client->options->my_addr;
  669. ceph_messenger_init(&client->msgr, myaddr);
  670. /* subsystems */
  671. err = ceph_monc_init(&client->monc, client);
  672. if (err < 0)
  673. goto fail;
  674. err = ceph_osdc_init(&client->osdc, client);
  675. if (err < 0)
  676. goto fail_monc;
  677. return client;
  678. fail_monc:
  679. ceph_monc_stop(&client->monc);
  680. fail:
  681. ceph_messenger_fini(&client->msgr);
  682. kfree(client);
  683. return ERR_PTR(err);
  684. }
  685. EXPORT_SYMBOL(ceph_create_client);
  686. void ceph_destroy_client(struct ceph_client *client)
  687. {
  688. dout("destroy_client %p\n", client);
  689. atomic_set(&client->msgr.stopping, 1);
  690. /* unmount */
  691. ceph_osdc_stop(&client->osdc);
  692. ceph_monc_stop(&client->monc);
  693. ceph_messenger_fini(&client->msgr);
  694. ceph_debugfs_client_cleanup(client);
  695. ceph_destroy_options(client->options);
  696. kfree(client);
  697. dout("destroy_client %p done\n", client);
  698. }
  699. EXPORT_SYMBOL(ceph_destroy_client);
  700. void ceph_reset_client_addr(struct ceph_client *client)
  701. {
  702. ceph_messenger_reset_nonce(&client->msgr);
  703. ceph_monc_reopen_session(&client->monc);
  704. ceph_osdc_reopen_osds(&client->osdc);
  705. }
  706. EXPORT_SYMBOL(ceph_reset_client_addr);
  707. /*
  708. * true if we have the mon map (and have thus joined the cluster)
  709. */
  710. static bool have_mon_and_osd_map(struct ceph_client *client)
  711. {
  712. return client->monc.monmap && client->monc.monmap->epoch &&
  713. client->osdc.osdmap && client->osdc.osdmap->epoch;
  714. }
  715. /*
  716. * mount: join the ceph cluster, and open root directory.
  717. */
  718. int __ceph_open_session(struct ceph_client *client, unsigned long started)
  719. {
  720. unsigned long timeout = client->options->mount_timeout;
  721. long err;
  722. /* open session, and wait for mon and osd maps */
  723. err = ceph_monc_open_session(&client->monc);
  724. if (err < 0)
  725. return err;
  726. while (!have_mon_and_osd_map(client)) {
  727. if (timeout && time_after_eq(jiffies, started + timeout))
  728. return -ETIMEDOUT;
  729. /* wait */
  730. dout("mount waiting for mon_map\n");
  731. err = wait_event_interruptible_timeout(client->auth_wq,
  732. have_mon_and_osd_map(client) || (client->auth_err < 0),
  733. ceph_timeout_jiffies(timeout));
  734. if (err < 0)
  735. return err;
  736. if (client->auth_err < 0)
  737. return client->auth_err;
  738. }
  739. pr_info("client%llu fsid %pU\n", ceph_client_gid(client),
  740. &client->fsid);
  741. ceph_debugfs_client_init(client);
  742. return 0;
  743. }
  744. EXPORT_SYMBOL(__ceph_open_session);
  745. int ceph_open_session(struct ceph_client *client)
  746. {
  747. int ret;
  748. unsigned long started = jiffies; /* note the start time */
  749. dout("open_session start\n");
  750. mutex_lock(&client->mount_mutex);
  751. ret = __ceph_open_session(client, started);
  752. mutex_unlock(&client->mount_mutex);
  753. return ret;
  754. }
  755. EXPORT_SYMBOL(ceph_open_session);
  756. int ceph_wait_for_latest_osdmap(struct ceph_client *client,
  757. unsigned long timeout)
  758. {
  759. u64 newest_epoch;
  760. int ret;
  761. ret = ceph_monc_get_version(&client->monc, "osdmap", &newest_epoch);
  762. if (ret)
  763. return ret;
  764. if (client->osdc.osdmap->epoch >= newest_epoch)
  765. return 0;
  766. ceph_osdc_maybe_request_map(&client->osdc);
  767. return ceph_monc_wait_osdmap(&client->monc, newest_epoch, timeout);
  768. }
  769. EXPORT_SYMBOL(ceph_wait_for_latest_osdmap);
  770. static int __init init_ceph_lib(void)
  771. {
  772. int ret = 0;
  773. ceph_debugfs_init();
  774. ret = ceph_crypto_init();
  775. if (ret < 0)
  776. goto out_debugfs;
  777. ret = ceph_msgr_init();
  778. if (ret < 0)
  779. goto out_crypto;
  780. ret = ceph_osdc_setup();
  781. if (ret < 0)
  782. goto out_msgr;
  783. pr_info("loaded (mon/osd proto %d/%d)\n",
  784. CEPH_MONC_PROTOCOL, CEPH_OSDC_PROTOCOL);
  785. return 0;
  786. out_msgr:
  787. ceph_msgr_exit();
  788. out_crypto:
  789. ceph_crypto_shutdown();
  790. out_debugfs:
  791. ceph_debugfs_cleanup();
  792. return ret;
  793. }
  794. static void __exit exit_ceph_lib(void)
  795. {
  796. dout("exit_ceph_lib\n");
  797. WARN_ON(!ceph_strings_empty());
  798. ceph_osdc_cleanup();
  799. ceph_msgr_exit();
  800. ceph_crypto_shutdown();
  801. ceph_debugfs_cleanup();
  802. }
  803. module_init(init_ceph_lib);
  804. module_exit(exit_ceph_lib);
  805. MODULE_AUTHOR("Sage Weil <[email protected]>");
  806. MODULE_AUTHOR("Yehuda Sadeh <[email protected]>");
  807. MODULE_AUTHOR("Patience Warnick <[email protected]>");
  808. MODULE_DESCRIPTION("Ceph core library");
  809. MODULE_LICENSE("GPL");